Skip to content

Commit ca83e1c

Browse files
authored
Merge pull request #88 from Jumhyn/master
Update PHI node example in README to use more idiomatic structure
2 parents 0a357ec + 4721e72 commit ca83e1c

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ let function = builder.addFunction(
103103
let entryBB = function.appendBasicBlock(named: "entry")
104104
builder.positionAtEnd(of: entryBB)
105105

106-
// allocate space for a local value
106+
// allocate space for a local value
107107
let 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)
120120
builder.positionAtEnd(of: thenBB)
121121
// local = 1/89, the fibonacci series (sort of)
122122
let thenVal = FloatType.double.constant(1/89)
123-
builder.buildStore(thenVal, to: local)
124123
// Branch to the merge block
125124
builder.buildBr(mergeBB)
126125

127126
// MARK: Else Block
128127
builder.positionAtEnd(of: elseBB)
129128
// local = 1/109, the fibonacci series (sort of) backwards
130129
let elseVal = FloatType.double.constant(1/109)
131-
builder.buildStore(elseVal, to: local)
132130
// Branch to the merge block
133131
builder.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

145145
This program generates the following IR:
@@ -152,16 +152,16 @@ entry:
152152
br i1 %1, label %then, label %else
153153
154154
then: ; preds = %entry
155-
store double 0x3F8702E05C0B8170, double* %local
156155
br label %merge
157156
158157
else: ; preds = %entry
159-
store double 0x3F82C9FB4D812CA0, double* %local
160158
br label %merge
161159
162160
merge: ; 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

Comments
 (0)