fibonacci.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //===--- examples/Fibonacci/fibonacci.cpp - An example use of the JIT -----===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file was developed by Valery A. Khamenya and is distributed under the
  6. // University of Illinois Open Source License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This small program provides an example of how to build quickly a small module
  11. // with function Fibonacci and execute it with the JIT.
  12. //
  13. // The goal of this snippet is to create in the memory the LLVM module
  14. // consisting of one function as follow:
  15. //
  16. // int fib(int x) {
  17. // if(x<=2) return 1;
  18. // return fib(x-1)+fib(x-2);
  19. // }
  20. //
  21. // Once we have this, we compile the module via JIT, then execute the `fib'
  22. // function and return result to a driver, i.e. to a "host program".
  23. //
  24. //===----------------------------------------------------------------------===//
  25. #include "llvm/Module.h"
  26. #include "llvm/DerivedTypes.h"
  27. #include "llvm/Constants.h"
  28. #include "llvm/Instructions.h"
  29. #include "llvm/ModuleProvider.h"
  30. #include "llvm/Analysis/Verifier.h"
  31. #include "llvm/ExecutionEngine/ExecutionEngine.h"
  32. #include "llvm/ExecutionEngine/GenericValue.h"
  33. #include <iostream>
  34. using namespace llvm;
  35. static Function *CreateFibFunction(Module *M) {
  36. // Create the fib function and insert it into module M. This function is said
  37. // to return an int and take an int parameter.
  38. Function *FibF = M->getOrInsertFunction("fib", Type::IntTy, Type::IntTy,
  39. (Type *)0);
  40. // Add a basic block to the function.
  41. BasicBlock *BB = new BasicBlock("EntryBlock", FibF);
  42. // Get pointers to the constants.
  43. Value *One = ConstantSInt::get(Type::IntTy, 1);
  44. Value *Two = ConstantSInt::get(Type::IntTy, 2);
  45. // Get pointer to the integer argument of the add1 function...
  46. Argument *ArgX = FibF->arg_begin(); // Get the arg.
  47. ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
  48. // Create the true_block.
  49. BasicBlock *RetBB = new BasicBlock("return", FibF);
  50. // Create an exit block.
  51. BasicBlock* RecurseBB = new BasicBlock("recurse", FibF);
  52. // Create the "if (arg < 2) goto exitbb"
  53. Value *CondInst = BinaryOperator::createSetLE(ArgX, Two, "cond", BB);
  54. new BranchInst(RetBB, RecurseBB, CondInst, BB);
  55. // Create: ret int 1
  56. new ReturnInst(One, RetBB);
  57. // create fib(x-1)
  58. Value *Sub = BinaryOperator::createSub(ArgX, One, "arg", RecurseBB);
  59. CallInst *CallFibX1 = new CallInst(FibF, Sub, "fibx1", RecurseBB);
  60. CallFibX1->setTailCall();
  61. // create fib(x-2)
  62. Sub = BinaryOperator::createSub(ArgX, Two, "arg", RecurseBB);
  63. CallInst *CallFibX2 = new CallInst(FibF, Sub, "fibx2", RecurseBB);
  64. CallFibX2->setTailCall();
  65. // fib(x-1)+fib(x-2)
  66. Value *Sum = BinaryOperator::createAdd(CallFibX1, CallFibX2,
  67. "addresult", RecurseBB);
  68. // Create the return instruction and add it to the basic block
  69. new ReturnInst(Sum, RecurseBB);
  70. return FibF;
  71. }
  72. int main(int argc, char **argv) {
  73. int n = argc > 1 ? atol(argv[1]) : 24;
  74. // Create some module to put our function into it.
  75. Module *M = new Module("test");
  76. // We are about to create the "fib" function:
  77. Function *FibF = CreateFibFunction(M);
  78. // Now we going to create JIT
  79. ExistingModuleProvider *MP = new ExistingModuleProvider(M);
  80. ExecutionEngine *EE = ExecutionEngine::create(MP, false);
  81. std::cerr << "verifying... ";
  82. if (verifyModule(*M)) {
  83. std::cerr << argv[0] << ": Error constructing function!\n";
  84. return 1;
  85. }
  86. std::cerr << "OK\n";
  87. std::cerr << "We just constructed this LLVM module:\n\n---------\n" << *M;
  88. std::cerr << "---------\nstarting fibonacci(" << n << ") with JIT...\n";
  89. // Call the Fibonacci function with argument n:
  90. std::vector<GenericValue> Args(1);
  91. Args[0].IntVal = n;
  92. GenericValue GV = EE->runFunction(FibF, Args);
  93. // import result of execution
  94. std::cout << "Result: " << GV.IntVal << "\n";
  95. return 0;
  96. }