From b5d1f83b27c0d599789a5b64f83a8de20e47b5d0 Mon Sep 17 00:00:00 2001 From: Frederick Kellison-Linn Date: Fri, 5 May 2017 18:21:57 -0400 Subject: [PATCH 1/2] Update PHI node example to use proper semantics --- README.md | 8 -------- 1 file changed, 8 deletions(-) diff --git a/README.md b/README.md index a7122600..4393b32f 100644 --- a/README.md +++ b/README.md @@ -103,9 +103,6 @@ let function = builder.addFunction( let entryBB = function.appendBasicBlock(named: "entry") builder.positionAtEnd(of: entryBB) -// allocate space for a local value -let local = builder.buildAlloca(type: FloatType.double, name: "local") - // Compare to the condition let test = builder.buildICmp(function.parameters[0], IntType.int1.zero(), .notEqual) @@ -120,7 +117,6 @@ builder.buildCondBr(condition: test, then: thenBB, else: elseBB) builder.positionAtEnd(of: thenBB) // local = 1/89, the fibonacci series (sort of) let thenVal = FloatType.double.constant(1/89) -builder.buildStore(thenVal, to: local) // Branch to the merge block builder.buildBr(mergeBB) @@ -128,7 +124,6 @@ builder.buildBr(mergeBB) builder.positionAtEnd(of: elseBB) // local = 1/109, the fibonacci series (sort of) backwards let elseVal = FloatType.double.constant(1/109) -builder.buildStore(elseVal, to: local) // Branch to the merge block builder.buildBr(mergeBB) @@ -147,16 +142,13 @@ This program generates the following IR: ```llvm define double @calculateFibs(i1) { entry: - %local = alloca double %1 = icmp ne i1 %0, false br i1 %1, label %then, label %else then: ; preds = %entry - store double 0x3F8702E05C0B8170, double* %local br label %merge else: ; preds = %entry - store double 0x3F82C9FB4D812CA0, double* %local br label %merge merge: ; preds = %else, %then From 4721e7215377883ec21a2c7284092be56fc739be Mon Sep 17 00:00:00 2001 From: Frederick Kellison-Linn Date: Sat, 6 May 2017 03:14:00 -0400 Subject: [PATCH 2/2] In PHI node example, store to local then return --- README.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4393b32f..87eeca03 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,9 @@ let function = builder.addFunction( let entryBB = function.appendBasicBlock(named: "entry") builder.positionAtEnd(of: entryBB) +// allocate space for a local value +let local = builder.buildAlloca(type: FloatType.double, name: "local") + // Compare to the condition let test = builder.buildICmp(function.parameters[0], IntType.int1.zero(), .notEqual) @@ -134,7 +137,9 @@ phi.addIncoming([ (thenVal, thenBB), (elseVal, elseBB), ]) -builder.buildRet(phi) +builder.buildStore(phi, to: local) +let ret = builder.buildLoad(local, name: "ret") +builder.buildRet(ret) ``` This program generates the following IR: @@ -142,6 +147,7 @@ This program generates the following IR: ```llvm define double @calculateFibs(i1) { entry: + %local = alloca double %1 = icmp ne i1 %0, false br i1 %1, label %then, label %else @@ -153,7 +159,9 @@ else: ; preds = %entry merge: ; preds = %else, %then %phi_example = phi double [ 0x3F8702E05C0B8170, %then ], [ 0x3F82C9FB4D812CA0, %else ] - ret double %phi_example + store double %phi_example, double* %local + %ret = load double, double* %local + ret double %ret } ```