From 545a2beb44e10e02f590f50053129935db54b9bf Mon Sep 17 00:00:00 2001 From: Bill Wendling Date: Sun, 16 Oct 2011 08:06:54 +0000 Subject: Update tutorial to reflect the current APIs. Also correct a small omission in LangImpl6.html (it needed to defined the 'binary :' operator). PR9052 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@142123 91177308-0d34-0410-b5e6-96231b3b80d8 --- docs/tutorial/LangImpl2.html | 8 +-- docs/tutorial/LangImpl3.html | 88 +++++++++++------------ docs/tutorial/LangImpl4.html | 64 +++++++++++------ docs/tutorial/LangImpl5.html | 71 ++++++++++--------- docs/tutorial/LangImpl6.html | 60 +++++++++------- docs/tutorial/LangImpl7.html | 164 +++++++++++++++++++++---------------------- 6 files changed, 240 insertions(+), 215 deletions(-) (limited to 'docs/tutorial') diff --git a/docs/tutorial/LangImpl2.html b/docs/tutorial/LangImpl2.html index acccd20a09..60e4f7f5e4 100644 --- a/docs/tutorial/LangImpl2.html +++ b/docs/tutorial/LangImpl2.html @@ -801,10 +801,10 @@ course.) To build this, just compile with:

-   # Compile
-   g++ -g -O3 toy.cpp 
-   # Run
-   ./a.out 
+# Compile
+clang++ -g -O3 toy.cpp
+# Run
+./a.out 
 
diff --git a/docs/tutorial/LangImpl3.html b/docs/tutorial/LangImpl3.html index c9f4cee42a..22ef26c2bd 100644 --- a/docs/tutorial/LangImpl3.html +++ b/docs/tutorial/LangImpl3.html @@ -266,7 +266,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"); } @@ -308,11 +308,11 @@ bodies and external function declarations. The code starts with:

 Function *PrototypeAST::Codegen() {
   // Make the function type:  double(double,double) etc.
-  std::vector<const Type*> Doubles(Args.size(),
-                                   Type::getDoubleTy(getGlobalContext()));
+  std::vector<Type*> Doubles(Args.size(),
+                             Type::getDoubleTy(getGlobalContext()));
   FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
                                        Doubles, false);
-  
+
   Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
 
@@ -532,9 +532,9 @@ functions. For example:
 ready> 4+5;
 Read top-level expression:
-define double @""() {
+define double @0() {
 entry:
-        ret double 9.000000e+00
+  ret double 9.000000e+00
 }
 
@@ -553,13 +553,13 @@ ready> def foo(a b) a*a + 2*a*b + b*b; Read function definition: define double @foo(double %a, double %b) { entry: - %multmp = fmul double %a, %a - %multmp1 = fmul double 2.000000e+00, %a - %multmp2 = fmul double %multmp1, %b - %addtmp = fadd double %multmp, %multmp2 - %multmp3 = fmul double %b, %b - %addtmp4 = fadd double %addtmp, %multmp3 - ret double %addtmp4 + %multmp = fmul double %a, %a + %multmp1 = fmul double 2.000000e+00, %a + %multmp2 = fmul double %multmp1, %b + %addtmp = fadd double %multmp, %multmp2 + %multmp3 = fmul double %b, %b + %addtmp4 = fadd double %addtmp, %multmp3 + ret double %addtmp4 } @@ -573,10 +573,10 @@ ready> def bar(a) foo(a, 4.0) + bar(31337); Read function definition: define double @bar(double %a) { entry: - %calltmp = call double @foo(double %a, double 4.000000e+00) - %calltmp1 = call double @bar(double 3.133700e+04) - %addtmp = fadd double %calltmp, %calltmp1 - ret double %addtmp + %calltmp = call double @foo(double %a, double 4.000000e+00) + %calltmp1 = call double @bar(double 3.133700e+04) + %addtmp = fadd double %calltmp, %calltmp1 + ret double %addtmp } @@ -593,10 +593,10 @@ declare double @cos(double) ready> cos(1.234); Read top-level expression: -define double @""() { +define double @1() { entry: - %calltmp = call double @cos(double 1.234000e+00) - ret double %calltmp + %calltmp = call double @cos(double 1.234000e+00) + ret double %calltmp } @@ -609,37 +609,37 @@ entry: ready> ^D ; ModuleID = 'my cool jit' -define double @""() { +define double @0() { entry: - %addtmp = fadd double 4.000000e+00, 5.000000e+00 - ret double %addtmp + %addtmp = fadd double 4.000000e+00, 5.000000e+00 + ret double %addtmp } define double @foo(double %a, double %b) { entry: - %multmp = fmul double %a, %a - %multmp1 = fmul double 2.000000e+00, %a - %multmp2 = fmul double %multmp1, %b - %addtmp = fadd double %multmp, %multmp2 - %multmp3 = fmul double %b, %b - %addtmp4 = fadd double %addtmp, %multmp3 - ret double %addtmp4 + %multmp = fmul double %a, %a + %multmp1 = fmul double 2.000000e+00, %a + %multmp2 = fmul double %multmp1, %b + %addtmp = fadd double %multmp, %multmp2 + %multmp3 = fmul double %b, %b + %addtmp4 = fadd double %addtmp, %multmp3 + ret double %addtmp4 } define double @bar(double %a) { entry: - %calltmp = call double @foo(double %a, double 4.000000e+00) - %calltmp1 = call double @bar(double 3.133700e+04) - %addtmp = fadd double %calltmp, %calltmp1 - ret double %addtmp + %calltmp = call double @foo(double %a, double 4.000000e+00) + %calltmp1 = call double @bar(double 3.133700e+04) + %addtmp = fadd double %calltmp, %calltmp1 + ret double %addtmp } declare double @cos(double) -define double @""() { +define double @1() { entry: - %calltmp = call double @cos(double 1.234000e+00) - ret double %calltmp + %calltmp = call double @cos(double 1.234000e+00) + ret double %calltmp } @@ -670,10 +670,10 @@ our makefile/command line about which options to use:

-   # Compile
-   g++ -g -O3 toy.cpp `llvm-config --cppflags --ldflags --libs core` -o toy
-   # Run
-   ./toy
+# Compile
+clang++ -g -O3 toy.cpp `llvm-config --cppflags --ldflags --libs core` -o toy
+# Run
+./toy
 
@@ -1081,13 +1081,13 @@ Value *CallExprAST::Codegen() { if (ArgsV.back() == 0) return 0; } - return Builder.CreateCall(CalleeF, ArgsV.begin(), ArgsV.end(), "calltmp"); + return Builder.CreateCall(CalleeF, ArgsV, "calltmp"); } Function *PrototypeAST::Codegen() { // Make the function type: double(double,double) etc. - std::vector<const Type*> Doubles(Args.size(), - Type::getDoubleTy(getGlobalContext())); + std::vector<Type*> Doubles(Args.size(), + Type::getDoubleTy(getGlobalContext())); FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false); diff --git a/docs/tutorial/LangImpl4.html b/docs/tutorial/LangImpl4.html index fe54fb5c01..77c9dbe0ad 100644 --- a/docs/tutorial/LangImpl4.html +++ b/docs/tutorial/LangImpl4.html @@ -343,9 +343,10 @@ code that is statically linked into your application.

 ready> 4+5;
-define double @""() {
+Read top-level expression:
+define double @0() {
 entry:
-        ret double 9.000000e+00
+  ret double 9.000000e+00
 }
 
 Evaluated to 9.000000
@@ -363,16 +364,17 @@ ready> def testfunc(x y) x + y*2; 
 Read function definition:
 define double @testfunc(double %x, double %y) {
 entry:
-        %multmp = fmul double %y, 2.000000e+00
-        %addtmp = fadd double %multmp, %x
-        ret double %addtmp
+  %multmp = fmul double %y, 2.000000e+00
+  %addtmp = fadd double %multmp, %x
+  ret double %addtmp
 }
 
 ready> testfunc(4, 10);
-define double @""() {
+Read top-level expression:
+define double @1() {
 entry:
-        %calltmp = call double @testfunc(double 4.000000e+00, double 1.000000e+01)
-        ret double %calltmp
+  %calltmp = call double @testfunc(double 4.000000e+00, double 1.000000e+01)
+  ret double %calltmp
 }
 
 Evaluated to 24.000000
@@ -404,21 +406,34 @@ Read extern:
 declare double @cos(double)
 
 ready> sin(1.0);
+Read top-level expression:
+define double @2() {
+entry:
+  ret double 0x3FEAED548F090CEE
+}
+
 Evaluated to 0.841471
 
 ready> def foo(x) sin(x)*sin(x) + cos(x)*cos(x);
 Read function definition:
 define double @foo(double %x) {
 entry:
-        %calltmp = call double @sin(double %x)
-        %multmp = fmul double %calltmp, %calltmp
-        %calltmp2 = call double @cos(double %x)
-        %multmp4 = fmul double %calltmp2, %calltmp2
-        %addtmp = fadd double %multmp, %multmp4
-        ret double %addtmp
+  %calltmp = call double @sin(double %x)
+  %multmp = fmul double %calltmp, %calltmp
+  %calltmp2 = call double @cos(double %x)
+  %multmp4 = fmul double %calltmp2, %calltmp2
+  %addtmp = fadd double %multmp, %multmp4
+  ret double %addtmp
 }
 
 ready> foo(4.0);
+Read top-level expression:
+define double @3() {
+entry:
+  %calltmp = call double @foo(double 4.000000e+00)
+  ret double %calltmp
+}
+
 Evaluated to 1.000000
 
@@ -484,10 +499,10 @@ LLVM JIT and optimizer. To build this example, use:
-   # 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
 
@@ -509,9 +524,9 @@ at runtime.

#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 <cstdio> #include <string> #include <map> @@ -905,13 +920,13 @@ Value *CallExprAST::Codegen() { if (ArgsV.back() == 0) return 0; } - return Builder.CreateCall(CalleeF, ArgsV.begin(), ArgsV.end(), "calltmp"); + return Builder.CreateCall(CalleeF, ArgsV, "calltmp"); } Function *PrototypeAST::Codegen() { // Make the function type: double(double,double) etc. - std::vector<const Type*> Doubles(Args.size(), - Type::getDoubleTy(getGlobalContext())); + std::vector<Type*> Doubles(Args.size(), + Type::getDoubleTy(getGlobalContext())); FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false); @@ -1013,6 +1028,9 @@ static void HandleTopLevelExpression() { // Evaluate a top-level expression into an anonymous function. if (FunctionAST *F = ParseTopLevelExpr()) { if (Function *LF = F->Codegen()) { + fprintf(stderr, "Read top-level expression:"); + LF->dump(); + // JIT the function, returning a function pointer. void *FPtr = TheExecutionEngine->getPointerToFunction(LF); @@ -1076,7 +1094,7 @@ int main() { // Create the JIT. This takes ownership of the module. std::string ErrStr; -TheExecutionEngine = EngineBuilder(TheModule).setErrorStr(&ErrStr).create(); + TheExecutionEngine = EngineBuilder(TheModule).setErrorStr(&ErrStr).create(); if (!TheExecutionEngine) { fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str()); exit(1); diff --git a/docs/tutorial/LangImpl5.html b/docs/tutorial/LangImpl5.html index e46ded13ae..a42636fe59 100644 --- a/docs/tutorial/LangImpl5.html +++ b/docs/tutorial/LangImpl5.html @@ -259,20 +259,20 @@ declare double @bar() define double @baz(double %x) { entry: - %ifcond = fcmp one double %x, 0.000000e+00 - br i1 %ifcond, label %then, label %else + %ifcond = fcmp one double %x, 0.000000e+00 + br i1 %ifcond, label %then, label %else then: ; preds = %entry - %calltmp = call double @foo() - br label %ifcont + %calltmp = call double @foo() + br label %ifcont else: ; preds = %entry - %calltmp1 = call double @bar() - br label %ifcont + %calltmp1 = call double @bar() + br label %ifcont ifcont: ; preds = %else, %then - %iftmp = phi double [ %calltmp, %then ], [ %calltmp1, %else ] - ret double %iftmp + %iftmp = phi double [ %calltmp, %then ], [ %calltmp1, %else ] + ret double %iftmp } @@ -660,25 +660,25 @@ declare double @putchard(double) define double @printstar(double %n) { entry: - ; initial value = 1.0 (inlined into phi) - br label %loop + ; initial value = 1.0 (inlined into phi) + br label %loop loop: ; preds = %loop, %entry - %i = phi double [ 1.000000e+00, %entry ], [ %nextvar, %loop ] - ; body - %calltmp = call double @putchard(double 4.200000e+01) - ; increment - %nextvar = fadd double %i, 1.000000e+00 - - ; termination test - %cmptmp = fcmp ult double %i, %n - %booltmp = uitofp i1 %cmptmp to double - %loopcond = fcmp one double %booltmp, 0.000000e+00 - br i1 %loopcond, label %loop, label %afterloop + %i = phi double [ 1.000000e+00, %entry ], [ %nextvar, %loop ] + ; body + %calltmp = call double @putchard(double 4.200000e+01) + ; increment + %nextvar = fadd double %i, 1.000000e+00 + + ; termination test + %cmptmp = fcmp ult double %i, %n + %booltmp = uitofp i1 %cmptmp to double + %loopcond = fcmp one double %booltmp, 0.000000e+00 + br i1 %loopcond, label %loop, label %afterloop afterloop: ; preds = %loop - ; loop always returns 0.0 - ret double 0.000000e+00 + ; loop always returns 0.0 + ret double 0.000000e+00 } @@ -829,10 +829,11 @@ statement.

With the code for the body of the loop complete, we just need to finish up -the control flow for it. This code remembers the end block (for the phi node), then creates the block for the loop exit ("afterloop"). Based on the value of the -exit condition, it creates a conditional branch that chooses between executing -the loop again and exiting the loop. Any future code is emitted in the -"afterloop" block, so it sets the insertion position to it.

+the control flow for it. This code remembers the end block (for the phi node), +then creates the block for the loop exit ("afterloop"). Based on the value of +the exit condition, it creates a conditional branch that chooses between +executing the loop again and exiting the loop. Any future code is emitted in +the "afterloop" block, so it sets the insertion position to it.

@@ -880,10 +881,10 @@ if/then/else and for expressions..  To build this example, use:
 
 
-   # 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
 
@@ -900,9 +901,9 @@ if/then/else and for expressions.. To build this example, use: #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 <cstdio> #include <string> #include <map> @@ -1397,7 +1398,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() { @@ -1546,8 +1547,8 @@ Value *ForExprAST::Codegen() { Function *PrototypeAST::Codegen() { // Make the function type: double(double,double) etc. - std::vector<const Type*> Doubles(Args.size(), - Type::getDoubleTy(getGlobalContext())); + std::vector<Type*> Doubles(Args.size(), + Type::getDoubleTy(getGlobalContext())); FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false); diff --git a/docs/tutorial/LangImpl6.html b/docs/tutorial/LangImpl6.html index 17ba301d4a..041af3b89c 100644 --- a/docs/tutorial/LangImpl6.html +++ b/docs/tutorial/LangImpl6.html @@ -293,8 +293,8 @@ Value *BinaryExprAST::Codegen() { Function *F = TheModule->getFunction(std::string("binary")+Op); assert(F && "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"); }
@@ -505,7 +505,9 @@ defined to print out the specified value and a newline):

 ready> extern printd(x);
-Read extern: declare double @printd(double)
+Read extern:
+declare double @printd(double)
+
 ready> def binary : 1 (x y) 0;  # Low-precedence operator that ignores operands.
 ..
 ready> printd(123) : printd(456) : printd(789);
@@ -555,6 +557,9 @@ def binary& 6 (LHS RHS)
 def binary = 9 (LHS RHS)
   !(LHS < RHS | LHS > RHS);
 
+# Define ':' for sequencing: as a low-precedence operator that ignores operands
+# and just returns the RHS.
+def binary : 1 (x y) y;
 
@@ -579,9 +584,10 @@ def printdensity(d) else putchard(42); # '*' ... -ready> printdensity(1): printdensity(2): printdensity(3) : - printdensity(4): printdensity(5): printdensity(9): putchard(10); -*++.. +ready> printdensity(1): printdensity(2): printdensity(3): + printdensity(4): printdensity(5): printdensity(9): + putchard(10); +**++. Evaluated to 0.000000
@@ -593,7 +599,7 @@ converge:

-# determine whether the specific location diverges.
+# Determine whether the specific location diverges.
 # Solve for z = z^2 + c in the complex plane.
 def mandleconverger(real imag iters creal cimag)
   if iters > 255 | (real*real + imag*imag > 4) then
@@ -603,25 +609,25 @@ def mandleconverger(real imag iters creal cimag)
                     2*real*imag + cimag,
                     iters+1, creal, cimag);
 
-# return the number of iterations required for the iteration to escape
+# Return the number of iterations required for the iteration to escape
 def mandleconverge(real imag)
   mandleconverger(real, imag, 0, real, imag);
 
-

This "z = z2 + c" function is a beautiful little creature that is the basis -for computation of the Mandelbrot Set. Our -mandelconverge function returns the number of iterations that it takes -for a complex orbit to escape, saturating to 255. This is not a very useful -function by itself, but if you plot its value over a two-dimensional plane, -you can see the Mandelbrot set. Given that we are limited to using putchard -here, our amazing graphical output is limited, but we can whip together +

This "z = z2 + c" function is a beautiful little +creature that is the basis for computation of +the Mandelbrot Set. +Our mandelconverge function returns the number of iterations that it +takes for a complex orbit to escape, saturating to 255. This is not a very +useful function by itself, but if you plot its value over a two-dimensional +plane, you can see the Mandelbrot set. Given that we are limited to using +putchard here, our amazing graphical output is limited, but we can whip together something using the density plotter above:

-# compute and plot the mandlebrot set with the specified 2 dimensional range
+# Compute and plot the mandlebrot set with the specified 2 dimensional range
 # info.
 def mandelhelp(xmin xmax xstep   ymin ymax ystep)
   for y = ymin, y < ymax, ystep in (
@@ -808,10 +814,10 @@ if/then/else and for expressions..  To build this example, use:
 
 
-   # 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
 
@@ -834,9 +840,9 @@ library, although doing that will cause problems on Windows.

#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 <cstdio> #include <string> #include <map> @@ -1415,8 +1421,8 @@ Value *BinaryExprAST::Codegen() { Function *F = TheModule->getFunction(std::string("binary")+Op); assert(F && "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() { @@ -1435,7 +1441,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() { @@ -1584,8 +1590,8 @@ Value *ForExprAST::Codegen() { Function *PrototypeAST::Codegen() { // Make the function type: double(double,double) etc. - std::vector<const Type*> Doubles(Args.size(), - Type::getDoubleTy(getGlobalContext())); + std::vector<Type*> Doubles(Args.size(), + Type::getDoubleTy(getGlobalContext())); FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false); diff --git a/docs/tutorial/LangImpl7.html b/docs/tutorial/LangImpl7.html index b2b26bdfa0..29b920c13b 100644 --- a/docs/tutorial/LangImpl7.html +++ b/docs/tutorial/LangImpl7.html @@ -102,19 +102,19 @@ The LLVM IR that we want for this example looks like this:

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 }
@@ -174,12 +174,12 @@ being declared with global variable definitions, they are declared with the
 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
+  ...
 
@@ -196,22 +196,22 @@ example to use the alloca technique to avoid using a PHI node:

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 } @@ -242,19 +242,19 @@ $ llvm-as < example.ll | opt -mem2reg | llvm-dis 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 } @@ -542,30 +542,30 @@ recursive fib function. Before the optimization:

 define double @fib(double %x) {
 entry:
-	%x1 = alloca double
-	store double %x, double* %x1
-	%x2 = load double* %x1
-	%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
+  %x1 = alloca double
+  store double %x, double* %x1
+  %x2 = load double* %x1
+  %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
-	%x3 = load double* %x1
-	%subtmp = fsub double %x3, 1.000000e+00
-	%calltmp = call double @fib(double %subtmp)
-	%x4 = load double* %x1
-	%subtmp5 = fsub double %x4, 2.000000e+00
-	%calltmp6 = call double @fib(double %subtmp5)
-	%addtmp = fadd double %calltmp, %calltmp6
-	br label %ifcont
+  %x3 = load double* %x1
+  %subtmp = fsub double %x3, 1.000000e+00
+  %calltmp = call double @fib(double %subtmp)
+  %x4 = load double* %x1
+  %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
 }
 
@@ -584,25 +584,25 @@ PHI node for it, so we still just make the PHI.

 define double @fib(double %x) {
 entry:
-	%cmptmp = fcmp ult double %x, 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 %x, 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 = 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
-	br label %ifcont
+  %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
+  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
 }
 
@@ -617,21 +617,21 @@ such blatent inefficiencies :).

 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 = 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
+  %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
 }
 
@@ -988,10 +988,10 @@ variables and var/in support. To build this example, use:
-   # 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
 
@@ -1008,9 +1008,9 @@ variables and var/in support. To build this example, use: #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 <cstdio> #include <string> #include <map> @@ -1686,8 +1686,8 @@ Value *BinaryExprAST::Codegen() { Function *F = TheModule->getFunction(std::string("binary")+Op); assert(F && "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() { @@ -1706,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() { @@ -1907,8 +1907,8 @@ Value *VarExprAST::Codegen() { Function *PrototypeAST::Codegen() { // Make the function type: double(double,double) etc. - std::vector<const Type*> Doubles(Args.size(), - Type::getDoubleTy(getGlobalContext())); + std::vector<Type*> Doubles(Args.size(), + Type::getDoubleTy(getGlobalContext())); FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false); -- cgit v1.2.3