Converting i1 type to integer value

3.5k Views Asked by At

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

2

There are 2 best solutions below

0
webjockey On BEST ANSWER

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.

1
arnt On

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.