Skip to content

Commit 4721e72

Browse files
authored
In PHI node example, store to local then return
1 parent b5d1f83 commit 4721e72

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ let function = builder.addFunction(
103103
let entryBB = function.appendBasicBlock(named: "entry")
104104
builder.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
107110
let 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

140145
This program generates the following IR:
141146

142147
```llvm
143148
define double @calculateFibs(i1) {
144149
entry:
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
154160
merge: ; 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

Comments
 (0)