@@ -103,7 +103,7 @@ let function = builder.addFunction(
103
103
let entryBB = function.appendBasicBlock (named : " entry" )
104
104
builder.positionAtEnd (of : entryBB)
105
105
106
- // allocate space for a local value
106
+ // allocate space for a local value
107
107
let local = builder.buildAlloca (type : FloatType.double , name : " local" )
108
108
109
109
// Compare to the condition
@@ -120,15 +120,13 @@ builder.buildCondBr(condition: test, then: thenBB, else: elseBB)
120
120
builder.positionAtEnd (of : thenBB)
121
121
// local = 1/89, the fibonacci series (sort of)
122
122
let thenVal = FloatType.double .constant (1 / 89 )
123
- builder.buildStore (thenVal, to : local)
124
123
// Branch to the merge block
125
124
builder.buildBr (mergeBB)
126
125
127
126
// MARK: Else Block
128
127
builder.positionAtEnd (of : elseBB)
129
128
// local = 1/109, the fibonacci series (sort of) backwards
130
129
let elseVal = FloatType.double .constant (1 / 109 )
131
- builder.buildStore (elseVal, to : local)
132
130
// Branch to the merge block
133
131
builder.buildBr (mergeBB)
134
132
@@ -139,7 +137,9 @@ phi.addIncoming([
139
137
(thenVal, thenBB),
140
138
(elseVal, elseBB),
141
139
])
142
- builder.buildRet (phi)
140
+ builder.buildStore (phi, to : local)
141
+ let ret = builder.buildLoad (local, name : " ret" )
142
+ builder.buildRet (ret)
143
143
```
144
144
145
145
This program generates the following IR:
@@ -152,16 +152,16 @@ entry:
152
152
br i1 %1, label %then, label %else
153
153
154
154
then: ; preds = %entry
155
- store double 0x3F8702E05C0B8170, double* %local
156
155
br label %merge
157
156
158
157
else: ; preds = %entry
159
- store double 0x3F82C9FB4D812CA0, double* %local
160
158
br label %merge
161
159
162
160
merge: ; preds = %else, %then
163
161
%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
165
165
}
166
166
```
167
167
0 commit comments