Search code examples
llvmllvm-clangllvm-irllvm-c++-api

Converting i1 type to integer value


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


Solution

  • 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.