@@ -103,6 +103,9 @@ 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
107
+ let local = builder.buildAlloca (type : FloatType.double , name : " local" )
108
+
106
109
// Compare to the condition
107
110
let test = builder.buildICmp (function.parameters [0 ], IntType.int1 .zero (), .notEqual )
108
111
@@ -134,14 +137,17 @@ phi.addIncoming([
134
137
(thenVal, thenBB),
135
138
(elseVal, elseBB),
136
139
])
137
- builder.buildRet (phi)
140
+ builder.buildStore (phi, to : local)
141
+ let ret = builder.buildLoad (local, name : " ret" )
142
+ builder.buildRet (ret)
138
143
```
139
144
140
145
This program generates the following IR:
141
146
142
147
``` llvm
143
148
define double @calculateFibs(i1) {
144
149
entry:
150
+ %local = alloca double
145
151
%1 = icmp ne i1 %0, false
146
152
br i1 %1, label %then, label %else
147
153
@@ -153,7 +159,9 @@ else: ; preds = %entry
153
159
154
160
merge: ; preds = %else, %then
155
161
%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
157
165
}
158
166
```
159
167
0 commit comments