Move llvm/Support/IRBuilder.h -> llvm/IRBuilder.h
[oota-llvm.git] / docs / tutorial / LangImpl7.html
index 211d14a3f5577b1a7b300b2954bb26c3c8f263fb..08c0c716b6fb7739e0ac7ec79371d8756d1d44fa 100644 (file)
@@ -7,12 +7,12 @@
          construction</title>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <meta name="author" content="Chris Lattner">
-  <link rel="stylesheet" href="../llvm.css" type="text/css">
+  <link rel="stylesheet" href="../_static/llvm.css" type="text/css">
 </head>
 
 <body>
 
-<div class="doc_title">Kaleidoscope: Extending the Language: Mutable Variables</div>
+<h1>Kaleidoscope: Extending the Language: Mutable Variables</h1>
 
 <ul>
 <li><a href="index.html">Up to Tutorial Index</a></li>
 </div>
 
 <!-- *********************************************************************** -->
-<div class="doc_section"><a name="intro">Chapter 7 Introduction</a></div>
+<h2><a name="intro">Chapter 7 Introduction</a></h2>
 <!-- *********************************************************************** -->
 
-<div class="doc_text">
+<div>
 
 <p>Welcome to Chapter 7 of the "<a href="index.html">Implementing a language
 with LLVM</a>" tutorial.  In chapters 1 through 6, we've built a very
@@ -66,10 +66,10 @@ support for this, though the way it works is a bit unexpected for some.</p>
 </div>
 
 <!-- *********************************************************************** -->
-<div class="doc_section"><a name="why">Why is this a hard problem?</a></div>
+<h2><a name="why">Why is this a hard problem?</a></h2>
 <!-- *********************************************************************** -->
 
-<div class="doc_text">
+<div>
 
 <p>
 To understand why mutable variables cause complexities in SSA construction, 
@@ -102,19 +102,19 @@ The LLVM IR that we want for this example looks like this:</p>
 
 define i32 @test(i1 %Condition) {
 entry:
-       br i1 %Condition, label %cond_true, label %cond_false
+  br i1 %Condition, label %cond_true, label %cond_false
 
 cond_true:
-       %X.0 = load i32* @G
-       br label %cond_next
+  %X.0 = load i32* @G
+  br label %cond_next
 
 cond_false:
-       %X.1 = load i32* @H
-       br label %cond_next
+  %X.1 = load i32* @H
+  br label %cond_next
 
 cond_next:
-       %X.2 = phi i32 [ %X.1, %cond_false ], [ %X.0, %cond_true ]
-       ret i32 %X.2
+  %X.2 = phi i32 [ %X.1, %cond_false ], [ %X.0, %cond_true ]
+  ret i32 %X.2
 }
 </pre>
 </div>
@@ -140,10 +140,10 @@ logic.</p>
 </div>
 
 <!-- *********************************************************************** -->
-<div class="doc_section"><a name="memory">Memory in LLVM</a></div>
+<h2><a name="memory">Memory in LLVM</a></h2>
 <!-- *********************************************************************** -->
 
-<div class="doc_text">
+<div>
 
 <p>The 'trick' here is that while LLVM does require all register values to be
 in SSA form, it does not require (or permit) memory objects to be in SSA form.
@@ -174,12 +174,12 @@ being declared with global variable definitions, they are declared with the
 <pre>
 define i32 @example() {
 entry:
-       %X = alloca i32           ; type of %X is i32*.
-       ...
-       %tmp = load i32* %X       ; load the stack value %X from the stack.
-       %tmp2 = add i32 %tmp, 1   ; increment it
-       store i32 %tmp2, i32* %X  ; store it back
-       ...
+  %X = alloca i32           ; type of %X is i32*.
+  ...
+  %tmp = load i32* %X       ; load the stack value %X from the stack.
+  %tmp2 = add i32 %tmp, 1   ; increment it
+  store i32 %tmp2, i32* %X  ; store it back
+  ...
 </pre>
 </div>
 
@@ -196,22 +196,22 @@ example to use the alloca technique to avoid using a PHI node:</p>
 
 define i32 @test(i1 %Condition) {
 entry:
-       %X = alloca i32           ; type of %X is i32*.
-       br i1 %Condition, label %cond_true, label %cond_false
+  %X = alloca i32           ; type of %X is i32*.
+  br i1 %Condition, label %cond_true, label %cond_false
 
 cond_true:
-       %X.0 = load i32* @G
-        store i32 %X.0, i32* %X   ; Update X
-       br label %cond_next
+  %X.0 = load i32* @G
+  store i32 %X.0, i32* %X   ; Update X
+  br label %cond_next
 
 cond_false:
-       %X.1 = load i32* @H
-        store i32 %X.1, i32* %X   ; Update X
-       br label %cond_next
+  %X.1 = load i32* @H
+  store i32 %X.1, i32* %X   ; Update X
+  br label %cond_next
 
 cond_next:
-       %X.2 = load i32* %X       ; Read X
-       ret i32 %X.2
+  %X.2 = load i32* %X       ; Read X
+  ret i32 %X.2
 }
 </pre>
 </div>
@@ -242,19 +242,19 @@ $ <b>llvm-as &lt; example.ll | opt -mem2reg | llvm-dis</b>
 
 define i32 @test(i1 %Condition) {
 entry:
-       br i1 %Condition, label %cond_true, label %cond_false
+  br i1 %Condition, label %cond_true, label %cond_false
 
 cond_true:
-       %X.0 = load i32* @G
-       br label %cond_next
+  %X.0 = load i32* @G
+  br label %cond_next
 
 cond_false:
-       %X.1 = load i32* @H
-       br label %cond_next
+  %X.1 = load i32* @H
+  br label %cond_next
 
 cond_next:
-       %X.01 = phi i32 [ %X.1, %cond_false ], [ %X.0, %cond_true ]
-       ret i32 %X.01
+  %X.01 = phi i32 [ %X.1, %cond_false ], [ %X.0, %cond_true ]
+  ret i32 %X.01
 }
 </pre>
 </div>
@@ -321,11 +321,10 @@ variables now!
 </div>
 
 <!-- *********************************************************************** -->
-<div class="doc_section"><a name="kalvars">Mutable Variables in 
-Kaleidoscope</a></div>
+<h2><a name="kalvars">Mutable Variables in Kaleidoscope</a></h2>
 <!-- *********************************************************************** -->
 
-<div class="doc_text">
+<div>
 
 <p>Now that we know the sort of problem we want to tackle, lets see what this
 looks like in the context of our little Kaleidoscope language.  We're going to
@@ -378,11 +377,10 @@ Kaleidoscope to support new variable definitions.
 </div>
 
 <!-- *********************************************************************** -->
-<div class="doc_section"><a name="adjustments">Adjusting Existing Variables for
-Mutation</a></div>
+<h2><a name="adjustments">Adjusting Existing Variables for Mutation</a></h2>
 <!-- *********************************************************************** -->
 
-<div class="doc_text">
+<div>
 
 <p>
 The symbol table in Kaleidoscope is managed at code generation time by the 
@@ -480,7 +478,7 @@ the unabridged code):</p>
   <b>// Reload, increment, and restore the alloca.  This handles the case where
   // the body of the loop mutates the variable.
   Value *CurVar = Builder.CreateLoad(Alloca);
-  Value *NextVar = Builder.CreateAdd(CurVar, StepVal, "nextvar");
+  Value *NextVar = Builder.CreateFAdd(CurVar, StepVal, "nextvar");
   Builder.CreateStore(NextVar, Alloca);</b>
   ...
 </pre>
@@ -544,30 +542,30 @@ recursive fib function.  Before the optimization:</p>
 <pre>
 define double @fib(double %x) {
 entry:
-       <b>%x1 = alloca double
-       store double %x, double* %x1
-       %x2 = load double* %x1</b>
-       %cmptmp = fcmp ult double %x2, 3.000000e+00
-       %booltmp = uitofp i1 %cmptmp to double
-       %ifcond = fcmp one double %booltmp, 0.000000e+00
-       br i1 %ifcond, label %then, label %else
+  <b>%x1 = alloca double
+  store double %x, double* %x1
+  %x2 = load double* %x1</b>
+  %cmptmp = fcmp ult double %x2, 3.000000e+00
+  %booltmp = uitofp i1 %cmptmp to double
+  %ifcond = fcmp one double %booltmp, 0.000000e+00
+  br i1 %ifcond, label %then, label %else
 
 then:          ; preds = %entry
-       br label %ifcont
+  br label %ifcont
 
 else:          ; preds = %entry
-       <b>%x3 = load double* %x1</b>
-       %subtmp = sub double %x3, 1.000000e+00
-       %calltmp = call double @fib( double %subtmp )
-       <b>%x4 = load double* %x1</b>
-       %subtmp5 = sub double %x4, 2.000000e+00
-       %calltmp6 = call double @fib( double %subtmp5 )
-       %addtmp = add double %calltmp, %calltmp6
-       br label %ifcont
+  <b>%x3 = load double* %x1</b>
+  %subtmp = fsub double %x3, 1.000000e+00
+  %calltmp = call double @fib(double %subtmp)
+  <b>%x4 = load double* %x1</b>
+  %subtmp5 = fsub double %x4, 2.000000e+00
+  %calltmp6 = call double @fib(double %subtmp5)
+  %addtmp = fadd double %calltmp, %calltmp6
+  br label %ifcont
 
 ifcont:                ; preds = %else, %then
-       %iftmp = phi double [ 1.000000e+00, %then ], [ %addtmp, %else ]
-       ret double %iftmp
+  %iftmp = phi double [ 1.000000e+00, %then ], [ %addtmp, %else ]
+  ret double %iftmp
 }
 </pre>
 </div>
@@ -586,25 +584,25 @@ PHI node for it, so we still just make the PHI.</p>
 <pre>
 define double @fib(double %x) {
 entry:
-       %cmptmp = fcmp ult double <b>%x</b>, 3.000000e+00
-       %booltmp = uitofp i1 %cmptmp to double
-       %ifcond = fcmp one double %booltmp, 0.000000e+00
-       br i1 %ifcond, label %then, label %else
+  %cmptmp = fcmp ult double <b>%x</b>, 3.000000e+00
+  %booltmp = uitofp i1 %cmptmp to double
+  %ifcond = fcmp one double %booltmp, 0.000000e+00
+  br i1 %ifcond, label %then, label %else
 
 then:
-       br label %ifcont
+  br label %ifcont
 
 else:
-       %subtmp = sub double <b>%x</b>, 1.000000e+00
-       %calltmp = call double @fib( double %subtmp )
-       %subtmp5 = sub double <b>%x</b>, 2.000000e+00
-       %calltmp6 = call double @fib( double %subtmp5 )
-       %addtmp = add double %calltmp, %calltmp6
-       br label %ifcont
+  %subtmp = fsub double <b>%x</b>, 1.000000e+00
+  %calltmp = call double @fib(double %subtmp)
+  %subtmp5 = fsub double <b>%x</b>, 2.000000e+00
+  %calltmp6 = call double @fib(double %subtmp5)
+  %addtmp = fadd double %calltmp, %calltmp6
+  br label %ifcont
 
 ifcont:                ; preds = %else, %then
-       %iftmp = phi double [ 1.000000e+00, %then ], [ %addtmp, %else ]
-       ret double %iftmp
+  %iftmp = phi double [ 1.000000e+00, %then ], [ %addtmp, %else ]
+  ret double %iftmp
 }
 </pre>
 </div>
@@ -619,21 +617,21 @@ such blatent inefficiencies :).</p>
 <pre>
 define double @fib(double %x) {
 entry:
-       %cmptmp = fcmp ult double %x, 3.000000e+00
-       %booltmp = uitofp i1 %cmptmp to double
-       %ifcond = fcmp ueq double %booltmp, 0.000000e+00
-       br i1 %ifcond, label %else, label %ifcont
+  %cmptmp = fcmp ult double %x, 3.000000e+00
+  %booltmp = uitofp i1 %cmptmp to double
+  %ifcond = fcmp ueq double %booltmp, 0.000000e+00
+  br i1 %ifcond, label %else, label %ifcont
 
 else:
-       %subtmp = sub double %x, 1.000000e+00
-       %calltmp = call double @fib( double %subtmp )
-       %subtmp5 = sub double %x, 2.000000e+00
-       %calltmp6 = call double @fib( double %subtmp5 )
-       %addtmp = add double %calltmp, %calltmp6
-       ret double %addtmp
+  %subtmp = fsub double %x, 1.000000e+00
+  %calltmp = call double @fib(double %subtmp)
+  %subtmp5 = fsub double %x, 2.000000e+00
+  %calltmp6 = call double @fib(double %subtmp5)
+  %addtmp = fadd double %calltmp, %calltmp6
+  ret double %addtmp
 
 ifcont:
-       ret double 1.000000e+00
+  ret double 1.000000e+00
 }
 </pre>
 </div>
@@ -648,10 +646,10 @@ we'll add the assignment operator.</p>
 </div>
 
 <!-- *********************************************************************** -->
-<div class="doc_section"><a name="assignment">New Assignment Operator</a></div>
+<h2><a name="assignment">New Assignment Operator</a></h2>
 <!-- *********************************************************************** -->
 
-<div class="doc_text">
+<div>
 
 <p>With our current framework, adding a new assignment operator is really
 simple.  We will parse it just like any other binary operator, but handle it
@@ -745,11 +743,10 @@ add this next!
 </div>
 
 <!-- *********************************************************************** -->
-<div class="doc_section"><a name="localvars">User-defined Local 
-Variables</a></div>
+<h2><a name="localvars">User-defined Local Variables</a></h2>
 <!-- *********************************************************************** -->
 
-<div class="doc_text">
+<div>
 
 <p>Adding var/in is just like any other other extensions we made to 
 Kaleidoscope: we extend the lexer, the parser, the AST and the code generator.
@@ -979,10 +976,10 @@ anywhere in sight.</p>
 </div>
 
 <!-- *********************************************************************** -->
-<div class="doc_section"><a name="code">Full Code Listing</a></div>
+<h2><a name="code">Full Code Listing</a></h2>
 <!-- *********************************************************************** -->
 
-<div class="doc_text">
+<div>
 
 <p>
 Here is the complete code listing for our running example, enhanced with mutable
@@ -991,10 +988,10 @@ variables and var/in support.  To build this example, use:
 
 <div class="doc_code">
 <pre>
-   # Compile
-   g++ -g toy.cpp `llvm-config --cppflags --ldflags --libs core jit native` -O3 -o toy
-   # Run
-   ./toy
+# Compile
+clang++ -g toy.cpp `llvm-config --cppflags --ldflags --libs core jit native` -O3 -o toy
+# Run
+./toy
 </pre>
 </div>
 
@@ -1004,16 +1001,16 @@ variables and var/in support.  To build this example, use:
 <pre>
 #include "llvm/DerivedTypes.h"
 #include "llvm/ExecutionEngine/ExecutionEngine.h"
-#include "llvm/ExecutionEngine/Interpreter.h"
 #include "llvm/ExecutionEngine/JIT.h"
+#include "llvm/IRBuilder.h"
 #include "llvm/LLVMContext.h"
 #include "llvm/Module.h"
 #include "llvm/PassManager.h"
 #include "llvm/Analysis/Verifier.h"
+#include "llvm/Analysis/Passes.h"
 #include "llvm/Target/TargetData.h"
-#include "llvm/Target/TargetSelect.h"
 #include "llvm/Transforms/Scalar.h"
-#include "llvm/Support/IRBuilder.h"
+#include "llvm/Support/TargetSelect.h"
 #include &lt;cstdio&gt;
 #include &lt;string&gt;
 #include &lt;map&gt;
@@ -1673,9 +1670,9 @@ Value *BinaryExprAST::Codegen() {
   if (L == 0 || R == 0) return 0;
   
   switch (Op) {
-  case '+': return Builder.CreateAdd(L, R, "addtmp");
-  case '-': return Builder.CreateSub(L, R, "subtmp");
-  case '*': return Builder.CreateMul(L, R, "multmp");
+  case '+': return Builder.CreateFAdd(L, R, "addtmp");
+  case '-': return Builder.CreateFSub(L, R, "subtmp");
+  case '*': return Builder.CreateFMul(L, R, "multmp");
   case '&lt;':
     L = Builder.CreateFCmpULT(L, R, "cmptmp");
     // Convert bool 0/1 to double 0.0 or 1.0
@@ -1689,8 +1686,8 @@ Value *BinaryExprAST::Codegen() {
   Function *F = TheModule-&gt;getFunction(std::string("binary")+Op);
   assert(F &amp;&amp; "binary operator not found!");
   
-  Value *Ops[] = { L, R };
-  return Builder.CreateCall(F, Ops, Ops+2, "binop");
+  Value *Ops[2] = { L, R };
+  return Builder.CreateCall(F, Ops, "binop");
 }
 
 Value *CallExprAST::Codegen() {
@@ -1709,7 +1706,7 @@ Value *CallExprAST::Codegen() {
     if (ArgsV.back() == 0) return 0;
   }
   
-  return Builder.CreateCall(CalleeF, ArgsV.begin(), ArgsV.end(), "calltmp");
+  return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
 }
 
 Value *IfExprAST::Codegen() {
@@ -1755,7 +1752,7 @@ Value *IfExprAST::Codegen() {
   // Emit merge block.
   TheFunction-&gt;getBasicBlockList().push_back(MergeBB);
   Builder.SetInsertPoint(MergeBB);
-  PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()),
+  PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2,
                                   "iftmp");
   
   PN-&gt;addIncoming(ThenV, ThenBB);
@@ -1834,7 +1831,7 @@ Value *ForExprAST::Codegen() {
   // Reload, increment, and restore the alloca.  This handles the case where
   // the body of the loop mutates the variable.
   Value *CurVar = Builder.CreateLoad(Alloca, VarName.c_str());
-  Value *NextVar = Builder.CreateAdd(CurVar, StepVal, "nextvar");
+  Value *NextVar = Builder.CreateFAdd(CurVar, StepVal, "nextvar");
   Builder.CreateStore(NextVar, Alloca);
   
   // Convert condition to a bool by comparing equal to 0.0.
@@ -1910,8 +1907,8 @@ Value *VarExprAST::Codegen() {
 
 Function *PrototypeAST::Codegen() {
   // Make the function type:  double(double,double) etc.
-  std::vector&lt;const Type*&gt; Doubles(Args.size(),
-                                   Type::getDoubleTy(getGlobalContext()));
+  std::vector&lt;Type*&gt; Doubles(Args.size(),
+                             Type::getDoubleTy(getGlobalContext()));
   FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
                                        Doubles, false);
   
@@ -2105,13 +2102,20 @@ int main() {
   TheModule = new Module("my cool jit", Context);
 
   // Create the JIT.  This takes ownership of the module.
-  TheExecutionEngine = EngineBuilder(TheModule).create();
+  std::string ErrStr;
+  TheExecutionEngine = EngineBuilder(TheModule).setErrorStr(&amp;ErrStr).create();
+  if (!TheExecutionEngine) {
+    fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
+    exit(1);
+  }
 
   FunctionPassManager OurFPM(TheModule);
 
   // Set up the optimizer pipeline.  Start with registering info about how the
   // target lays out data structures.
   OurFPM.add(new TargetData(*TheExecutionEngine-&gt;getTargetData()));
+  // Provide basic AliasAnalysis support for GVN.
+  OurFPM.add(createBasicAliasAnalysisPass());
   // Promote allocas to registers.
   OurFPM.add(createPromoteMemoryToRegisterPass());
   // Do simple "peephole" optimizations and bit-twiddling optzns.
@@ -2153,7 +2157,7 @@ int main() {
   src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
 
   <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
-  <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br>
+  <a href="http://llvm.org/">The LLVM Compiler Infrastructure</a><br>
   Last modified: $Date$
 </address>
 </body>