2004-09-11 22:30:11 +02:00
|
|
|
//===--- examples/Fibonacci/fibonacci.cpp - An example use of the JIT -----===//
|
2005-04-20 18:42:34 +02:00
|
|
|
//
|
2004-08-19 22:10:04 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:37:57 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-20 18:42:34 +02:00
|
|
|
//
|
2004-08-19 22:10:04 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2004-11-03 22:43:03 +01:00
|
|
|
// This small program provides an example of how to build quickly a small module
|
|
|
|
// with function Fibonacci and execute it with the JIT.
|
2004-08-19 22:10:04 +02:00
|
|
|
//
|
2004-11-03 22:43:03 +01:00
|
|
|
// The goal of this snippet is to create in the memory the LLVM module
|
|
|
|
// consisting of one function as follow:
|
2004-08-19 22:10:04 +02:00
|
|
|
//
|
2004-11-03 22:43:03 +01:00
|
|
|
// int fib(int x) {
|
|
|
|
// if(x<=2) return 1;
|
|
|
|
// return fib(x-1)+fib(x-2);
|
|
|
|
// }
|
2005-04-20 18:42:34 +02:00
|
|
|
//
|
2004-11-03 22:43:03 +01:00
|
|
|
// Once we have this, we compile the module via JIT, then execute the `fib'
|
2004-08-19 22:10:04 +02:00
|
|
|
// function and return result to a driver, i.e. to a "host program".
|
|
|
|
//
|
2004-11-03 22:43:03 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
2004-08-19 22:10:04 +02:00
|
|
|
|
2016-05-25 03:18:36 +02:00
|
|
|
#include "llvm/ADT/APInt.h"
|
2014-01-13 10:53:45 +01:00
|
|
|
#include "llvm/IR/Verifier.h"
|
2016-05-25 03:18:36 +02:00
|
|
|
#include "llvm/ExecutionEngine/ExecutionEngine.h"
|
2004-08-19 22:10:04 +02:00
|
|
|
#include "llvm/ExecutionEngine/GenericValue.h"
|
2016-06-11 07:47:04 +02:00
|
|
|
#include "llvm/ExecutionEngine/MCJIT.h"
|
2016-05-25 03:18:36 +02:00
|
|
|
#include "llvm/IR/Argument.h"
|
|
|
|
#include "llvm/IR/BasicBlock.h"
|
2013-01-02 12:56:33 +01:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
2016-05-25 03:18:36 +02:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/InstrTypes.h"
|
2013-01-02 12:56:33 +01:00
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2016-05-25 03:18:36 +02:00
|
|
|
#include "llvm/IR/Type.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
2011-08-24 20:08:43 +02:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2012-12-04 11:16:57 +01:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2016-05-25 03:18:36 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2015-09-29 20:02:48 +02:00
|
|
|
|
2004-08-19 22:10:04 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-07-15 01:09:55 +02:00
|
|
|
static Function *CreateFibFunction(Module *M, LLVMContext &Context) {
|
2012-10-23 18:03:18 +02:00
|
|
|
// Create the fib function and insert it into module M. This function is said
|
2004-11-03 22:43:03 +01:00
|
|
|
// to return an int and take an int parameter.
|
2007-01-07 08:40:09 +01:00
|
|
|
Function *FibF =
|
2012-06-22 00:26:01 +02:00
|
|
|
cast<Function>(M->getOrInsertFunction("fib", Type::getInt32Ty(Context),
|
2017-04-11 17:01:18 +02:00
|
|
|
Type::getInt32Ty(Context)));
|
2005-04-20 18:42:34 +02:00
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
// Add a basic block to the function.
|
2009-08-13 23:58:54 +02:00
|
|
|
BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", FibF);
|
2005-04-20 18:42:34 +02:00
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
// Get pointers to the constants.
|
2009-08-13 23:58:54 +02:00
|
|
|
Value *One = ConstantInt::get(Type::getInt32Ty(Context), 1);
|
|
|
|
Value *Two = ConstantInt::get(Type::getInt32Ty(Context), 2);
|
2004-08-19 22:10:04 +02:00
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
// Get pointer to the integer argument of the add1 function...
|
2015-11-07 01:55:46 +01:00
|
|
|
Argument *ArgX = &*FibF->arg_begin(); // Get the arg.
|
2004-11-03 22:43:03 +01:00
|
|
|
ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
|
2004-08-19 22:10:04 +02:00
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
// Create the true_block.
|
2009-08-13 23:58:54 +02:00
|
|
|
BasicBlock *RetBB = BasicBlock::Create(Context, "return", FibF);
|
2004-11-03 22:43:03 +01:00
|
|
|
// Create an exit block.
|
2009-08-13 23:58:54 +02:00
|
|
|
BasicBlock* RecurseBB = BasicBlock::Create(Context, "recurse", FibF);
|
2004-08-19 22:10:04 +02:00
|
|
|
|
2008-03-13 04:29:42 +01:00
|
|
|
// Create the "if (arg <= 2) goto exitbb"
|
2009-07-10 01:48:35 +02:00
|
|
|
Value *CondInst = new ICmpInst(*BB, ICmpInst::ICMP_SLE, ArgX, Two, "cond");
|
2008-04-06 22:25:17 +02:00
|
|
|
BranchInst::Create(RetBB, RecurseBB, CondInst, BB);
|
2004-08-19 22:10:04 +02:00
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
// Create: ret int 1
|
2009-08-13 23:58:54 +02:00
|
|
|
ReturnInst::Create(Context, One, RetBB);
|
2005-04-20 18:42:34 +02:00
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
// create fib(x-1)
|
2008-05-16 21:29:10 +02:00
|
|
|
Value *Sub = BinaryOperator::CreateSub(ArgX, One, "arg", RecurseBB);
|
2008-04-06 22:25:17 +02:00
|
|
|
CallInst *CallFibX1 = CallInst::Create(FibF, Sub, "fibx1", RecurseBB);
|
2005-05-06 08:21:59 +02:00
|
|
|
CallFibX1->setTailCall();
|
2005-04-20 18:42:34 +02:00
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
// create fib(x-2)
|
2008-05-16 21:29:10 +02:00
|
|
|
Sub = BinaryOperator::CreateSub(ArgX, Two, "arg", RecurseBB);
|
2008-04-06 22:25:17 +02:00
|
|
|
CallInst *CallFibX2 = CallInst::Create(FibF, Sub, "fibx2", RecurseBB);
|
2005-05-06 08:21:59 +02:00
|
|
|
CallFibX2->setTailCall();
|
2005-05-06 07:59:50 +02:00
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
// fib(x-1)+fib(x-2)
|
2008-05-16 21:29:10 +02:00
|
|
|
Value *Sum = BinaryOperator::CreateAdd(CallFibX1, CallFibX2,
|
2004-11-03 22:43:03 +01:00
|
|
|
"addresult", RecurseBB);
|
2005-04-20 18:42:34 +02:00
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
// Create the return instruction and add it to the basic block
|
2009-08-13 23:58:54 +02:00
|
|
|
ReturnInst::Create(Context, Sum, RecurseBB);
|
2004-08-19 22:10:04 +02:00
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
return FibF;
|
|
|
|
}
|
2004-08-19 22:10:04 +02:00
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
int n = argc > 1 ? atol(argv[1]) : 24;
|
2004-08-19 22:10:04 +02:00
|
|
|
|
2009-12-01 02:56:27 +01:00
|
|
|
InitializeNativeTarget();
|
2016-06-11 07:47:04 +02:00
|
|
|
InitializeNativeTargetAsmPrinter();
|
2009-07-01 18:58:40 +02:00
|
|
|
LLVMContext Context;
|
2012-06-22 00:26:01 +02:00
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
// Create some module to put our function into it.
|
2014-08-19 06:04:25 +02:00
|
|
|
std::unique_ptr<Module> Owner(new Module("test", Context));
|
|
|
|
Module *M = Owner.get();
|
2004-11-03 22:43:03 +01:00
|
|
|
|
|
|
|
// We are about to create the "fib" function:
|
2014-08-19 06:04:25 +02:00
|
|
|
Function *FibF = CreateFibFunction(M, Context);
|
2004-08-19 22:10:04 +02:00
|
|
|
|
2005-04-20 18:42:34 +02:00
|
|
|
// Now we going to create JIT
|
2009-12-01 02:56:27 +01:00
|
|
|
std::string errStr;
|
2010-09-06 01:09:30 +02:00
|
|
|
ExecutionEngine *EE =
|
2014-08-19 06:04:25 +02:00
|
|
|
EngineBuilder(std::move(Owner))
|
2010-09-06 01:09:30 +02:00
|
|
|
.setErrorStr(&errStr)
|
|
|
|
.create();
|
2009-12-01 02:56:27 +01:00
|
|
|
|
|
|
|
if (!EE) {
|
2010-09-06 01:09:30 +02:00
|
|
|
errs() << argv[0] << ": Failed to construct ExecutionEngine: " << errStr
|
|
|
|
<< "\n";
|
2009-12-01 02:56:27 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2004-08-19 22:10:04 +02:00
|
|
|
|
2008-08-24 00:23:09 +02:00
|
|
|
errs() << "verifying... ";
|
2004-08-19 22:10:04 +02:00
|
|
|
if (verifyModule(*M)) {
|
2008-08-24 00:23:09 +02:00
|
|
|
errs() << argv[0] << ": Error constructing function!\n";
|
2004-08-19 22:10:04 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-08-24 00:23:09 +02:00
|
|
|
errs() << "OK\n";
|
|
|
|
errs() << "We just constructed this LLVM module:\n\n---------\n" << *M;
|
|
|
|
errs() << "---------\nstarting fibonacci(" << n << ") with JIT...\n";
|
2004-08-19 22:10:04 +02:00
|
|
|
|
2004-11-05 05:11:40 +01:00
|
|
|
// Call the Fibonacci function with argument n:
|
2004-11-03 22:43:03 +01:00
|
|
|
std::vector<GenericValue> Args(1);
|
2007-03-06 18:24:31 +01:00
|
|
|
Args[0].IntVal = APInt(32, n);
|
2004-11-03 22:43:03 +01:00
|
|
|
GenericValue GV = EE->runFunction(FibF, Args);
|
2004-08-19 22:10:04 +02:00
|
|
|
|
2004-11-03 22:43:03 +01:00
|
|
|
// import result of execution
|
2008-08-24 00:23:09 +02:00
|
|
|
outs() << "Result: " << GV.IntVal << "\n";
|
2012-06-22 00:26:01 +02:00
|
|
|
|
2004-08-19 22:10:04 +02:00
|
|
|
return 0;
|
|
|
|
}
|