#include "llvm/ADT/APInt.h"
#include "llvm/IR/Verifier.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/GenericValue.h"
#include "llvm/ExecutionEngine/MCJIT.h"
#include "llvm/IR/Argument.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cstdlib>
#include <memory>
#include <string>
#include <vector>
using namespace llvm;
static Function *CreateFibFunction(Module *M, LLVMContext &Context) {
FunctionType *FibFTy = FunctionType::get(Type::getInt32Ty(Context),
{Type::getInt32Ty(Context)}, false);
Function *FibF =
Function::Create(FibFTy, Function::ExternalLinkage, "fib", M);
BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", FibF);
Value *One = ConstantInt::get(Type::getInt32Ty(Context), 1);
Value *Two = ConstantInt::get(Type::getInt32Ty(Context), 2);
Argument *ArgX = &*FibF->arg_begin(); ArgX->setName("AnArg");
BasicBlock *RetBB = BasicBlock::Create(Context, "return", FibF);
BasicBlock* RecurseBB = BasicBlock::Create(Context, "recurse", FibF);
Value *CondInst = new ICmpInst(*BB, ICmpInst::ICMP_SLE, ArgX, Two, "cond");
BranchInst::Create(RetBB, RecurseBB, CondInst, BB);
ReturnInst::Create(Context, One, RetBB);
Value *Sub = BinaryOperator::CreateSub(ArgX, One, "arg", RecurseBB);
CallInst *CallFibX1 = CallInst::Create(FibF, Sub, "fibx1", RecurseBB);
CallFibX1->setTailCall();
Sub = BinaryOperator::CreateSub(ArgX, Two, "arg", RecurseBB);
CallInst *CallFibX2 = CallInst::Create(FibF, Sub, "fibx2", RecurseBB);
CallFibX2->setTailCall();
Value *Sum = BinaryOperator::CreateAdd(CallFibX1, CallFibX2,
"addresult", RecurseBB);
ReturnInst::Create(Context, Sum, RecurseBB);
return FibF;
}
int main(int argc, char **argv) {
int n = argc > 1 ? atol(argv[1]) : 24;
InitializeNativeTarget();
InitializeNativeTargetAsmPrinter();
LLVMContext Context;
std::unique_ptr<Module> Owner(new Module("test", Context));
Module *M = Owner.get();
Function *FibF = CreateFibFunction(M, Context);
std::string errStr;
ExecutionEngine *EE =
EngineBuilder(std::move(Owner))
.setErrorStr(&errStr)
.create();
if (!EE) {
errs() << argv[0] << ": Failed to construct ExecutionEngine: " << errStr
<< "\n";
return 1;
}
errs() << "verifying... ";
if (verifyModule(*M)) {
errs() << argv[0] << ": Error constructing function!\n";
return 1;
}
errs() << "OK\n";
errs() << "We just constructed this LLVM module:\n\n---------\n" << *M;
errs() << "---------\nstarting fibonacci(" << n << ") with JIT...\n";
std::vector<GenericValue> Args(1);
Args[0].IntVal = APInt(32, n);
GenericValue GV = EE->runFunction(FibF, Args);
outs() << "Result: " << GV.IntVal << "\n";
return 0;
}