1 //===-- examples/HowToUseJIT/HowToUseJIT.cpp - An example use of the JIT --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This small program provides an example of how to quickly build a small
11 // module with two functions and execute it with the JIT.
14 // The goal of this snippet is to create in the memory
15 // the LLVM module consisting of two functions as follow:
25 // then compile the module via JIT, then execute the `foo'
26 // function and return result to a driver, i.e. to a "host program".
28 // Some remarks and questions:
30 // - could we invoke some code using noname functions too?
31 // e.g. evaluate "foo()+foo()" without fears to introduce
32 // conflict of temporary function name with some real
33 // existing function name?
35 //===----------------------------------------------------------------------===//
37 #include "llvm/ExecutionEngine/GenericValue.h"
38 #include "llvm/ExecutionEngine/Interpreter.h"
39 #include "llvm/IR/Constants.h"
40 #include "llvm/IR/DerivedTypes.h"
41 #include "llvm/IR/IRBuilder.h"
42 #include "llvm/IR/Instructions.h"
43 #include "llvm/IR/LLVMContext.h"
44 #include "llvm/IR/Module.h"
45 #include "llvm/Support/ManagedStatic.h"
46 #include "llvm/Support/TargetSelect.h"
47 #include "llvm/Support/raw_ostream.h"
53 InitializeNativeTarget();
57 // Create some module to put our function into it.
58 Module *M = new Module("test", Context);
60 // Create the add1 function entry and insert this entry into module M. The
61 // function will have a return type of "int" and take an argument of "int".
62 // The '0' terminates the list of argument types.
64 cast<Function>(M->getOrInsertFunction("add1", Type::getInt32Ty(Context),
65 Type::getInt32Ty(Context),
68 // Add a basic block to the function. As before, it automatically inserts
69 // because of the last argument.
70 BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", Add1F);
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);
76 // Get pointers to the constant `1'.
77 Value *One = builder.getInt32(1);
79 // Get pointers to the integer argument of the add1 function...
80 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
81 Argument *ArgX = Add1F->arg_begin(); // Get the arg
82 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
84 // Create the add instruction, inserting it into the end of BB.
85 Value *Add = builder.CreateAdd(One, ArgX);
87 // Create the return instruction and add it to the basic block
88 builder.CreateRet(Add);
90 // Now, function add1 is ready.
93 // Now we're going to create function `foo', which returns an int and takes no
96 cast<Function>(M->getOrInsertFunction("foo", Type::getInt32Ty(Context),
99 // Add a basic block to the FooF function.
100 BB = BasicBlock::Create(Context, "EntryBlock", FooF);
102 // Tell the basic block builder to attach itself to the new basic block
103 builder.SetInsertPoint(BB);
105 // Get pointer to the constant `10'.
106 Value *Ten = builder.getInt32(10);
108 // Pass Ten to the call to Add1F
109 CallInst *Add1CallRes = builder.CreateCall(Add1F, Ten);
110 Add1CallRes->setTailCall(true);
112 // Create the return instruction and add it to the basic block.
113 builder.CreateRet(Add1CallRes);
115 // Now we create the JIT.
116 ExecutionEngine* EE = EngineBuilder(M).create();
118 outs() << "We just constructed this LLVM module:\n\n" << *M;
119 outs() << "\n\nRunning foo: ";
122 // Call the `foo' function with no arguments:
123 std::vector<GenericValue> noargs;
124 GenericValue gv = EE->runFunction(FooF, noargs);
126 // Import result of execution:
127 outs() << "Result: " << gv.IntVal << "\n";