5

For the following branch instruction

br i1 %cmp, label %if.then, label %if.end, !dbg !35

Since llvm is SSA, I can directly access the operand 0, to determine whether the comparison is true or not. The type evaluates to i1 but I am having trouble extracting the value (true or false)

BranchInst &I;
Value *val = I.getOperand(0);

Type yields to i1 type but when I tried to cast to

ConstantInt *cint = dyn_cast<ConstantInt>(val) the casting does not seem to work? how do I go about it

webjockey
  • 1,647
  • 2
  • 20
  • 28

2 Answers2

10

Answering my own question

 BranchInst &I;
    Module* module;
    IRBuilder<> irbuilder(&I);
    Value* value = irbuilder.CreateIntCast(I.getCondition(),
Type::getInt32Ty(module->getContext()), false);

This should convert i1 to i32.

webjockey
  • 1,647
  • 2
  • 20
  • 28
1

You want a different kind of cast — cast<> casts within your code, while what you want is a cast within the generated code, CastInst. Boolean is a one-bit integer type, so what you want probably is a zero extension. CastInst::Create(CastInst::ZExt, I.getOperand(0), … should do.

arnt
  • 8,949
  • 5
  • 24
  • 32
  • I did not want to insert the instruction, just want to convert the value. I figured it out, I will post the answer – webjockey Sep 13 '20 at 04:54