title: LLVM IR for loop
body: <p>I'm using LLVM C++ api to generate LLVM IR and using JIT to run the function. 
  The function I want to simulate is  </p>
  
  <pre class="lang-c prettyprint-override"><code>double foofor(double a)
  {
      for (int i = 0; i &lt; 10; i++) {
          a += 2;
      }
      return a;
  }
  </code></pre>
  
  <p>The corresponding llvm IR code I generate is (using TheFunction->print)</p>
  
  <pre><code>define double @foofor(double %a) {
  entry:
    br label %loop
  
  loop:                                             ; preds = %loop, %entry
    %a1 = phi double [ 0.000000e+00, %entry ], [ %nextvar, %loop ]
    %addtmp = fadd double %a, 2.000000e+00
    %nextvar = fadd double 1.000000e+00, %a1
    %cmptmp = fcmp ult double %a1, 1.000000e+01
    br i1 %cmptmp, label %loop, label %afterloop
  
  afterloop:                                        ; preds = %loop
    ret double %addtmp
  }
  </code></pre>
  
  <p>When I call <code>foofor(40)</code>, I get 42 not 60(40 + 20).   </p>
  
  <p>The code I have write(without AST)</p>
  
  <pre><code>#include "./llvm-8.0.1.src/examples/Kaleidoscope/include/KaleidoscopeJIT.h"
  #include "llvm/ADT/APFloat.h"
  #include "llvm/ADT/STLExtras.h"
  #include "llvm/IR/BasicBlock.h"
  #include "llvm/IR/Constants.h"
  #include "llvm/IR/DerivedTypes.h"
  #include "llvm/IR/Function.h"
  #include "llvm/IR/IRBuilder.h"
  #include "llvm/IR/LLVMContext.h"
  #include "llvm/IR/LegacyPassManager.h"
  #include "llvm/IR/Module.h"
  #include "llvm/IR/Type.h"
  #include "llvm/IR/Verifier.h"
  #include "llvm/Support/TargetSelect.h"
  #include "llvm/Target/TargetMachine.h"
  #include "llvm/Transforms/InstCombine/InstCombine.h"
  #include "llvm/Transforms/Scalar.h"
  #include "llvm/Transforms/Scalar/GVN.h"
  #include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;iostream&gt;
  using namespace std;
  using namespace llvm;
  using namespace llvm::orc;
  
  //LLVM items
  static LLVMContext TheContext;
  static IRBuilder&lt;&gt; Builder(TheContext);
  static std::unique_ptr&lt;Module&gt; TheModule;
  //JIT
  static std::unique_ptr&lt;KaleidoscopeJIT&gt; TheJIT;
  
  
  
  /*
   *double foofor(double a)
   *{
   *    for(i = 0; i &lt; a; i++) {
   *      a = a + 1
   *    }
   *    return a
   *}
   *
  */
  int main()
  {
      InitializeNativeTarget();
      InitializeNativeTargetAsmPrinter();
  
      //init module
      TheModule = llvm::make_unique&lt;Module&gt;("myjit", TheContext);
  
      //used to be runned by jit later
      TheJIT = llvm::make_unique&lt;KaleidoscopeJIT&gt;();
      TheModule-&gt;setDataLayout(TheJIT-&gt;getTargetMachine().createDataLayout());
  
      //define the args
      vector&lt;std::string&gt; ArgNames;
  
      //foocfor has 1 args
      ArgNames.push_back(string("a"));
  
      //make the 1 args attach to LLVM Type::double
      std::vector&lt;Type *&gt; Doubles(ArgNames.size(), Type::getDoubleTy(TheContext));
  
      //generate llvm function type
      FunctionType *FT = FunctionType::get(Type::getDoubleTy(TheContext), Doubles, false);
  
      //Create function whose FunctionType is FT
      Function *TheFunction = Function::Create(FT, Function::ExternalLinkage, "foofor", TheModule.get());
  
      //give the name for Function args and save the args in innerargs
      unsigned Idx = 0;
  
      std::vector&lt;Value *&gt;innerargs;
  
      for (auto &amp;Arg : TheFunction-&gt;args()) {
          Arg.setName(ArgNames[Idx++]);
          innerargs.push_back(&amp;Arg);
      }
  
      //this function's basic block
      BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction);
  
      //create the loop BasicBlock
      BasicBlock *LoopBB = BasicBlock::Create(TheContext, "loop", TheFunction);
  
      //exit the block
      BasicBlock *AfterBB = BasicBlock::Create(TheContext, "afterloop", TheFunction);
  
      Builder.SetInsertPoint(BB);
      //add goto LoopBB  in BB
      Builder.CreateBr(LoopBB);
  
  
      //start with LoopBB
      Builder.SetInsertPoint(LoopBB);
  
      //start with 0
      Value *StartVal = ConstantFP::get(TheContext, APFloat(0.0));
      //step is 1
      Value *StepVal = ConstantFP::get(TheContext, APFloat(1.0));
  
      //local Variable which name is a
      PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(TheContext), 1, "a");
      //if it's from start block, set Variable 0
      Variable-&gt;addIncoming(StartVal, BB);
  
      //do the body then do cond
      //emit loop body in LoopBB
      //body: arg_a += 1
      Value *ret = Builder.CreateFAdd(innerargs[0], ConstantFP::get(TheContext, APFloat(2.0)), "addtmp");
  
  
      //do the cond,if Variable &gt;= 10 then break(goto AfterBB)
      Value *NextVar = Builder.CreateFAdd(StepVal, Variable, "nextvar");
  
      //if Variable &lt; 10 then goto LoopBB or gotot AfterBB
      Value *cond = Builder.CreateFCmpULT(Variable, ConstantFP::get(TheContext, APFloat(10.0)),  "cmptmp");
      Builder.CreateCondBr(cond, LoopBB, AfterBB);
  
      Builder.SetInsertPoint(AfterBB);
      Variable-&gt;addIncoming(NextVar, LoopBB);
  
      Builder.CreateRet(ret);
  
      TheFunction-&gt;print(errs());
  
      //using jit to run this code
      auto H = TheJIT-&gt;addModule(std::move(TheModule));
      auto ExprSymbol = TheJIT-&gt;findSymbol("foofor");
      double (*foofor)(double) = (double (*)(double))(intptr_t)cantFail(ExprSymbol.getAddress());
      cout &lt;&lt;foofor(40)&lt;&lt;endl;
  }
  </code></pre>