Converting i1 type to integer value
Asked Answered
R

2

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

Religieux answered 11/9, 2020 at 7:52 Comment(0)
R
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.

Religieux answered 13/9, 2020 at 5:2 Comment(0)
T
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.

Tinge answered 11/9, 2020 at 12:10 Comment(1)
I did not want to insert the instruction, just want to convert the value. I figured it out, I will post the answerReligieux

© 2022 - 2025 — McMap. All rights reserved.