LLVM Cast Instructions

I have the values โ€‹โ€‹of ConstantInt and ConstantFP that I want to add using fadd . However, it's hard for me to send ConstantInt to the floating point number that fadd will accept.

Here is an excerpt from the code:

 Value* left = ConstantInt::get(Type::getInt64Ty(getGlobalContext()), 12, true); Value* right = ConstantFP::get(Type::getFloatTy(getGlobalContext()), 11.6); Instruction* cast = CastInst::Create(Instruction::SIToFP, left, left->getType(), "", currentBlock()); left = cast->getOperand(0); BinaryOperator::Create(Instruction::FAdd, left, right, "", currentBlock()); 

where currentBlock() returns a BasicBlock . After trying to create an opcode for this, LLVM complains that it cannot add two values โ€‹โ€‹because they do not match.

I am new to LLVM, so I will advise if this code does not make sense.

+7
c ++ llvm llvm-c ++ - api
source share
2 answers

My usual approach with these things is what Clang generates - both LLVM IR and C ++ API calls (C ++ backend). You can use the online copy for simplicity. So, compile this C code:

 float foo(int a, float b) { return a + b; } 

Gives me this LLVM IR:

 define float @foo(i32 %a, float %b) #0 { entry: %conv = sitofp i32 %a to float %add = fadd float %conv, %b ret float %add } 

And these are the C ++ API calls needed to recreate this:

  // Function: foo (func_foo) { Function::arg_iterator args = func_foo->arg_begin(); Value* int32_a = args++; int32_a->setName("a"); Value* float_b = args++; float_b->setName("b"); BasicBlock* label_entry = BasicBlock::Create(mod->getContext(), "entry",func_foo,0); // Block entry (label_entry) CastInst* float_conv = new SIToFPInst(int32_a, Type::getFloatTy(mod->getContext()), "conv", label_entry); BinaryOperator* float_add = BinaryOperator::Create(Instruction::FAdd, float_conv, float_b, "add", label_entry); ReturnInst::Create(mod->getContext(), float_add, label_entry); } 

You can configure the C input code (for example, replace vars with constants, etc.) and see what Clang / LLVM emits. This is the best / fastest way to find your way around the IR and API when you are not too familiar with it.

+11
source share

The problem is here:

 Instruction* cast = CastInst::Create(Instruction::SIToFP, left, left->getType(), "", currentBlock()); 

You sent left to left->getType() , i.e. did nothing. Instead: right->getType() :

 Instruction* cast = CastInst::Create(Instruction::SIToFP, left, right->getType(), "", currentBlock()); 
0
source share

All Articles