IR output from CodegenMullAdd define i32 @mul_add(i32 %x, i32 %y, i32 %z) { entry: %tmp = mul i32 %x, %y %tmp2 = add i32 %tmp, %z ret i32 %tmp2 } Create a module with one function, which multiplies two arguments, add a third and then returns the result.
| 46 | // Create a module with one function, which multiplies two arguments, add a third and |
| 47 | // then returns the result. |
| 48 | llvm::Module* CodegenMulAdd(llvm::LLVMContext* context) { |
| 49 | llvm::Module* mod = new llvm::Module("test", *context); |
| 50 | llvm::Constant* c = mod->getOrInsertFunction("mul_add", |
| 51 | llvm::IntegerType::get(*context, 32), llvm::IntegerType::get(*context, 32), |
| 52 | llvm::IntegerType::get(*context, 32), llvm::IntegerType::get(*context, 32)); |
| 53 | llvm::Function* mul_add = llvm::cast<llvm::Function>(c); |
| 54 | mul_add->setCallingConv(llvm::CallingConv::C); |
| 55 | llvm::Function::arg_iterator args = mul_add->arg_begin(); |
| 56 | llvm::Value* x = &*args; |
| 57 | ++args; |
| 58 | x->setName("x"); |
| 59 | llvm::Value* y = &*args; |
| 60 | ++args; |
| 61 | y->setName("y"); |
| 62 | llvm::Value* z = &*args; |
| 63 | ++args; |
| 64 | z->setName("z"); |
| 65 | llvm::BasicBlock* block = llvm::BasicBlock::Create(*context, "entry", mul_add); |
| 66 | llvm::IRBuilder<> builder(block); |
| 67 | llvm::Value* tmp = builder.CreateBinOp(llvm::Instruction::Mul, x, y, "tmp"); |
| 68 | llvm::Value* tmp2 = builder.CreateBinOp(llvm::Instruction::Add, tmp, z, "tmp2"); |
| 69 | builder.CreateRet(tmp2); |
| 70 | return mod; |
| 71 | } |
| 72 | |
| 73 | TEST_F(InstructionCounterTest, Count) { |
| 74 | llvm::Module* MulAddModule = CodegenMulAdd(&context_); |