xref: /minix3/external/bsd/llvm/dist/llvm/examples/Fibonacci/fibonacci.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- examples/Fibonacci/fibonacci.cpp - An example use of the JIT -----===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This small program provides an example of how to build quickly a small module
11f4a2713aSLionel Sambuc // with function Fibonacci and execute it with the JIT.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc // The goal of this snippet is to create in the memory the LLVM module
14f4a2713aSLionel Sambuc // consisting of one function as follow:
15f4a2713aSLionel Sambuc //
16f4a2713aSLionel Sambuc //   int fib(int x) {
17f4a2713aSLionel Sambuc //     if(x<=2) return 1;
18f4a2713aSLionel Sambuc //     return fib(x-1)+fib(x-2);
19f4a2713aSLionel Sambuc //   }
20f4a2713aSLionel Sambuc //
21f4a2713aSLionel Sambuc // Once we have this, we compile the module via JIT, then execute the `fib'
22f4a2713aSLionel Sambuc // function and return result to a driver, i.e. to a "host program".
23f4a2713aSLionel Sambuc //
24f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
25f4a2713aSLionel Sambuc 
26*0a6a1f1dSLionel Sambuc #include "llvm/IR/Verifier.h"
27f4a2713aSLionel Sambuc #include "llvm/ExecutionEngine/GenericValue.h"
28f4a2713aSLionel Sambuc #include "llvm/ExecutionEngine/Interpreter.h"
29f4a2713aSLionel Sambuc #include "llvm/IR/Constants.h"
30f4a2713aSLionel Sambuc #include "llvm/IR/DerivedTypes.h"
31f4a2713aSLionel Sambuc #include "llvm/IR/Instructions.h"
32f4a2713aSLionel Sambuc #include "llvm/IR/LLVMContext.h"
33f4a2713aSLionel Sambuc #include "llvm/IR/Module.h"
34f4a2713aSLionel Sambuc #include "llvm/Support/TargetSelect.h"
35f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
36f4a2713aSLionel Sambuc using namespace llvm;
37f4a2713aSLionel Sambuc 
CreateFibFunction(Module * M,LLVMContext & Context)38f4a2713aSLionel Sambuc static Function *CreateFibFunction(Module *M, LLVMContext &Context) {
39f4a2713aSLionel Sambuc   // Create the fib function and insert it into module M. This function is said
40f4a2713aSLionel Sambuc   // to return an int and take an int parameter.
41f4a2713aSLionel Sambuc   Function *FibF =
42f4a2713aSLionel Sambuc     cast<Function>(M->getOrInsertFunction("fib", Type::getInt32Ty(Context),
43f4a2713aSLionel Sambuc                                           Type::getInt32Ty(Context),
44f4a2713aSLionel Sambuc                                           (Type *)0));
45f4a2713aSLionel Sambuc 
46f4a2713aSLionel Sambuc   // Add a basic block to the function.
47f4a2713aSLionel Sambuc   BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", FibF);
48f4a2713aSLionel Sambuc 
49f4a2713aSLionel Sambuc   // Get pointers to the constants.
50f4a2713aSLionel Sambuc   Value *One = ConstantInt::get(Type::getInt32Ty(Context), 1);
51f4a2713aSLionel Sambuc   Value *Two = ConstantInt::get(Type::getInt32Ty(Context), 2);
52f4a2713aSLionel Sambuc 
53f4a2713aSLionel Sambuc   // Get pointer to the integer argument of the add1 function...
54f4a2713aSLionel Sambuc   Argument *ArgX = FibF->arg_begin();   // Get the arg.
55f4a2713aSLionel Sambuc   ArgX->setName("AnArg");            // Give it a nice symbolic name for fun.
56f4a2713aSLionel Sambuc 
57f4a2713aSLionel Sambuc   // Create the true_block.
58f4a2713aSLionel Sambuc   BasicBlock *RetBB = BasicBlock::Create(Context, "return", FibF);
59f4a2713aSLionel Sambuc   // Create an exit block.
60f4a2713aSLionel Sambuc   BasicBlock* RecurseBB = BasicBlock::Create(Context, "recurse", FibF);
61f4a2713aSLionel Sambuc 
62f4a2713aSLionel Sambuc   // Create the "if (arg <= 2) goto exitbb"
63f4a2713aSLionel Sambuc   Value *CondInst = new ICmpInst(*BB, ICmpInst::ICMP_SLE, ArgX, Two, "cond");
64f4a2713aSLionel Sambuc   BranchInst::Create(RetBB, RecurseBB, CondInst, BB);
65f4a2713aSLionel Sambuc 
66f4a2713aSLionel Sambuc   // Create: ret int 1
67f4a2713aSLionel Sambuc   ReturnInst::Create(Context, One, RetBB);
68f4a2713aSLionel Sambuc 
69f4a2713aSLionel Sambuc   // create fib(x-1)
70f4a2713aSLionel Sambuc   Value *Sub = BinaryOperator::CreateSub(ArgX, One, "arg", RecurseBB);
71f4a2713aSLionel Sambuc   CallInst *CallFibX1 = CallInst::Create(FibF, Sub, "fibx1", RecurseBB);
72f4a2713aSLionel Sambuc   CallFibX1->setTailCall();
73f4a2713aSLionel Sambuc 
74f4a2713aSLionel Sambuc   // create fib(x-2)
75f4a2713aSLionel Sambuc   Sub = BinaryOperator::CreateSub(ArgX, Two, "arg", RecurseBB);
76f4a2713aSLionel Sambuc   CallInst *CallFibX2 = CallInst::Create(FibF, Sub, "fibx2", RecurseBB);
77f4a2713aSLionel Sambuc   CallFibX2->setTailCall();
78f4a2713aSLionel Sambuc 
79f4a2713aSLionel Sambuc 
80f4a2713aSLionel Sambuc   // fib(x-1)+fib(x-2)
81f4a2713aSLionel Sambuc   Value *Sum = BinaryOperator::CreateAdd(CallFibX1, CallFibX2,
82f4a2713aSLionel Sambuc                                          "addresult", RecurseBB);
83f4a2713aSLionel Sambuc 
84f4a2713aSLionel Sambuc   // Create the return instruction and add it to the basic block
85f4a2713aSLionel Sambuc   ReturnInst::Create(Context, Sum, RecurseBB);
86f4a2713aSLionel Sambuc 
87f4a2713aSLionel Sambuc   return FibF;
88f4a2713aSLionel Sambuc }
89f4a2713aSLionel Sambuc 
90f4a2713aSLionel Sambuc 
main(int argc,char ** argv)91f4a2713aSLionel Sambuc int main(int argc, char **argv) {
92f4a2713aSLionel Sambuc   int n = argc > 1 ? atol(argv[1]) : 24;
93f4a2713aSLionel Sambuc 
94f4a2713aSLionel Sambuc   InitializeNativeTarget();
95f4a2713aSLionel Sambuc   LLVMContext Context;
96f4a2713aSLionel Sambuc 
97f4a2713aSLionel Sambuc   // Create some module to put our function into it.
98*0a6a1f1dSLionel Sambuc   std::unique_ptr<Module> Owner(new Module("test", Context));
99*0a6a1f1dSLionel Sambuc   Module *M = Owner.get();
100f4a2713aSLionel Sambuc 
101f4a2713aSLionel Sambuc   // We are about to create the "fib" function:
102*0a6a1f1dSLionel Sambuc   Function *FibF = CreateFibFunction(M, Context);
103f4a2713aSLionel Sambuc 
104f4a2713aSLionel Sambuc   // Now we going to create JIT
105f4a2713aSLionel Sambuc   std::string errStr;
106f4a2713aSLionel Sambuc   ExecutionEngine *EE =
107*0a6a1f1dSLionel Sambuc     EngineBuilder(std::move(Owner))
108f4a2713aSLionel Sambuc     .setErrorStr(&errStr)
109f4a2713aSLionel Sambuc     .setEngineKind(EngineKind::JIT)
110f4a2713aSLionel Sambuc     .create();
111f4a2713aSLionel Sambuc 
112f4a2713aSLionel Sambuc   if (!EE) {
113f4a2713aSLionel Sambuc     errs() << argv[0] << ": Failed to construct ExecutionEngine: " << errStr
114f4a2713aSLionel Sambuc            << "\n";
115f4a2713aSLionel Sambuc     return 1;
116f4a2713aSLionel Sambuc   }
117f4a2713aSLionel Sambuc 
118f4a2713aSLionel Sambuc   errs() << "verifying... ";
119f4a2713aSLionel Sambuc   if (verifyModule(*M)) {
120f4a2713aSLionel Sambuc     errs() << argv[0] << ": Error constructing function!\n";
121f4a2713aSLionel Sambuc     return 1;
122f4a2713aSLionel Sambuc   }
123f4a2713aSLionel Sambuc 
124f4a2713aSLionel Sambuc   errs() << "OK\n";
125f4a2713aSLionel Sambuc   errs() << "We just constructed this LLVM module:\n\n---------\n" << *M;
126f4a2713aSLionel Sambuc   errs() << "---------\nstarting fibonacci(" << n << ") with JIT...\n";
127f4a2713aSLionel Sambuc 
128f4a2713aSLionel Sambuc   // Call the Fibonacci function with argument n:
129f4a2713aSLionel Sambuc   std::vector<GenericValue> Args(1);
130f4a2713aSLionel Sambuc   Args[0].IntVal = APInt(32, n);
131f4a2713aSLionel Sambuc   GenericValue GV = EE->runFunction(FibF, Args);
132f4a2713aSLionel Sambuc 
133f4a2713aSLionel Sambuc   // import result of execution
134f4a2713aSLionel Sambuc   outs() << "Result: " << GV.IntVal << "\n";
135f4a2713aSLionel Sambuc 
136f4a2713aSLionel Sambuc   return 0;
137f4a2713aSLionel Sambuc }
138