@@ -103,7 +103,7 @@ let function = builder.addFunction(
103103let entryBB = function.appendBasicBlock (named : " entry" )
104104builder.positionAtEnd (of : entryBB)
105105
106- // allocate space for a local value
106+ // allocate space for a local value
107107let local = builder.buildAlloca (type : FloatType.double , name : " local" )
108108
109109// Compare to the condition
@@ -120,15 +120,13 @@ builder.buildCondBr(condition: test, then: thenBB, else: elseBB)
120120builder.positionAtEnd (of : thenBB)
121121// local = 1/89, the fibonacci series (sort of)
122122let thenVal = FloatType.double .constant (1 / 89 )
123- builder.buildStore (thenVal, to : local)
124123// Branch to the merge block
125124builder.buildBr (mergeBB)
126125
127126// MARK: Else Block
128127builder.positionAtEnd (of : elseBB)
129128// local = 1/109, the fibonacci series (sort of) backwards
130129let elseVal = FloatType.double .constant (1 / 109 )
131- builder.buildStore (elseVal, to : local)
132130// Branch to the merge block
133131builder.buildBr (mergeBB)
134132
@@ -139,7 +137,9 @@ phi.addIncoming([
139137 (thenVal, thenBB),
140138 (elseVal, elseBB),
141139])
142- builder.buildRet (phi)
140+ builder.buildStore (phi, to : local)
141+ let ret = builder.buildLoad (local, name : " ret" )
142+ builder.buildRet (ret)
143143```
144144
145145This program generates the following IR:
@@ -152,16 +152,16 @@ entry:
152152 br i1 %1, label %then, label %else
153153
154154then: ; preds = %entry
155- store double 0x3F8702E05C0B8170, double* %local
156155 br label %merge
157156
158157else: ; preds = %entry
159- store double 0x3F82C9FB4D812CA0, double* %local
160158 br label %merge
161159
162160merge: ; preds = %else, %then
163161 %phi_example = phi double [ 0x3F8702E05C0B8170, %then ], [ 0x3F82C9FB4D812CA0, %else ]
164- ret double %phi_example
162+ store double %phi_example, double* %local
163+ %ret = load double, double* %local
164+ ret double %ret
165165}
166166```
167167
0 commit comments