@@ -103,6 +103,9 @@ let function = builder.addFunction(
103103let entryBB = function.appendBasicBlock (named : " entry" )
104104builder.positionAtEnd (of : entryBB)
105105
106+ // allocate space for a local value
107+ let local = builder.buildAlloca (type : FloatType.double , name : " local" )
108+
106109// Compare to the condition
107110let test = builder.buildICmp (function.parameters [0 ], IntType.int1 .zero (), .notEqual )
108111
@@ -134,14 +137,17 @@ phi.addIncoming([
134137 (thenVal, thenBB),
135138 (elseVal, elseBB),
136139])
137- builder.buildRet (phi)
140+ builder.buildStore (phi, to : local)
141+ let ret = builder.buildLoad (local, name : " ret" )
142+ builder.buildRet (ret)
138143```
139144
140145This program generates the following IR:
141146
142147``` llvm
143148define double @calculateFibs(i1) {
144149entry:
150+ %local = alloca double
145151 %1 = icmp ne i1 %0, false
146152 br i1 %1, label %then, label %else
147153
@@ -153,7 +159,9 @@ else: ; preds = %entry
153159
154160merge: ; preds = %else, %then
155161 %phi_example = phi double [ 0x3F8702E05C0B8170, %then ], [ 0x3F82C9FB4D812CA0, %else ]
156- ret double %phi_example
162+ store double %phi_example, double* %local
163+ %ret = load double, double* %local
164+ ret double %ret
157165}
158166```
159167
0 commit comments