llvm.org GIT mirror llvm / c3a627d
Have the JIT tutorial use IRBuilder for the IR. Patch by Jake Waskett! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@132770 91177308-0d34-0410-b5e6-96231b3b80d8 Eric Christopher 8 years ago
1 changed file(s) with 17 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
4444 #include "llvm/Target/TargetSelect.h"
4545 #include "llvm/Support/ManagedStatic.h"
4646 #include "llvm/Support/raw_ostream.h"
47 #include "llvm/Support/IRBuilder.h"
48
4749 using namespace llvm;
4850
4951 int main() {
6769 // because of the last argument.
6870 BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", Add1F);
6971
72 // Create a basic block builder with default parameters. The builder will
73 // automatically append instructions to the basic block `BB'.
74 IRBuilder<> builder(BB);
75
7076 // Get pointers to the constant `1'.
71 Value *One = ConstantInt::get(Type::getInt32Ty(Context), 1);
77 Value *One = builder.getInt32(1);
7278
7379 // Get pointers to the integer argument of the add1 function...
7480 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
7682 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
7783
7884 // Create the add instruction, inserting it into the end of BB.
79 Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB);
85 Value *Add = builder.CreateAdd(One, ArgX);
8086
8187 // Create the return instruction and add it to the basic block
82 ReturnInst::Create(Context, Add, BB);
88 builder.CreateRet(Add);
8389
8490 // Now, function add1 is ready.
8591
9399 // Add a basic block to the FooF function.
94100 BB = BasicBlock::Create(Context, "EntryBlock", FooF);
95101
96 // Get pointers to the constant `10'.
97 Value *Ten = ConstantInt::get(Type::getInt32Ty(Context), 10);
102 // Tell the basic block builder to attach itself to the new basic block
103 builder.SetInsertPoint(BB);
98104
99 // Pass Ten to the call call:
100 CallInst *Add1CallRes = CallInst::Create(Add1F, Ten, "add1", BB);
105 // Get pointer to the constant `10'.
106 Value *Ten = builder.getInt32(10);
107
108 // Pass Ten to the call to Add1F
109 CallInst *Add1CallRes = builder.CreateCall(Add1F, Ten);
101110 Add1CallRes->setTailCall(true);
102111
103112 // Create the return instruction and add it to the basic block.
104 ReturnInst::Create(Context, Add1CallRes, BB);
113 builder.CreateRet(Add1CallRes);
105114
106115 // Now we create the JIT.
107116 ExecutionEngine* EE = EngineBuilder(M).create();