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/LLVMContext.h"
38 #include "llvm/Module.h"
39 #include "llvm/Constants.h"
40 #include "llvm/DerivedTypes.h"
41 #include "llvm/Instructions.h"
42 #include "llvm/ExecutionEngine/JIT.h"
43 #include "llvm/ExecutionEngine/Interpreter.h"
44 #include "llvm/ExecutionEngine/GenericValue.h"
45 #include "llvm/Target/TargetSelect.h"
46 #include "llvm/Support/ManagedStatic.h"
47 #include "llvm/Support/raw_ostream.h"
52 InitializeNativeTarget();
56 // Create some module to put our function into it.
57 Module
*M
= new Module("test", Context
);
59 // Create the add1 function entry and insert this entry into module M. The
60 // function will have a return type of "int" and take an argument of "int".
61 // The '0' terminates the list of argument types.
63 cast
<Function
>(M
->getOrInsertFunction("add1", Type::getInt32Ty(Context
),
64 Type::getInt32Ty(Context
),
67 // Add a basic block to the function. As before, it automatically inserts
68 // because of the last argument.
69 BasicBlock
*BB
= BasicBlock::Create(Context
, "EntryBlock", Add1F
);
71 // Get pointers to the constant `1'.
72 Value
*One
= ConstantInt::get(Type::getInt32Ty(Context
), 1);
74 // Get pointers to the integer argument of the add1 function...
75 assert(Add1F
->arg_begin() != Add1F
->arg_end()); // Make sure there's an arg
76 Argument
*ArgX
= Add1F
->arg_begin(); // Get the arg
77 ArgX
->setName("AnArg"); // Give it a nice symbolic name for fun.
79 // Create the add instruction, inserting it into the end of BB.
80 Instruction
*Add
= BinaryOperator::CreateAdd(One
, ArgX
, "addresult", BB
);
82 // Create the return instruction and add it to the basic block
83 ReturnInst::Create(Context
, Add
, BB
);
85 // Now, function add1 is ready.
88 // Now we going to create function `foo', which returns an int and takes no
91 cast
<Function
>(M
->getOrInsertFunction("foo", Type::getInt32Ty(Context
),
94 // Add a basic block to the FooF function.
95 BB
= BasicBlock::Create(Context
, "EntryBlock", FooF
);
97 // Get pointers to the constant `10'.
98 Value
*Ten
= ConstantInt::get(Type::getInt32Ty(Context
), 10);
100 // Pass Ten to the call call:
101 CallInst
*Add1CallRes
= CallInst::Create(Add1F
, Ten
, "add1", BB
);
102 Add1CallRes
->setTailCall(true);
104 // Create the return instruction and add it to the basic block.
105 ReturnInst::Create(Context
, Add1CallRes
, BB
);
107 // Now we create the JIT.
108 ExecutionEngine
* EE
= EngineBuilder(M
).create();
110 outs() << "We just constructed this LLVM module:\n\n" << *M
;
111 outs() << "\n\nRunning foo: ";
114 // Call the `foo' function with no arguments:
115 std::vector
<GenericValue
> noargs
;
116 GenericValue gv
= EE
->runFunction(FooF
, noargs
);
118 // Import result of execution:
119 outs() << "Result: " << gv
.IntVal
<< "\n";
120 EE
->freeMachineCodeForFunction(FooF
);