Skip to content

Update PHI node example in README to use more idiomatic structure #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 6, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ let function = builder.addFunction(
let entryBB = function.appendBasicBlock(named: "entry")
builder.positionAtEnd(of: entryBB)

// allocate space for a local value
// allocate space for a local value
let local = builder.buildAlloca(type: FloatType.double, name: "local")

// Compare to the condition
Expand All @@ -120,15 +120,13 @@ builder.buildCondBr(condition: test, then: thenBB, else: elseBB)
builder.positionAtEnd(of: thenBB)
// local = 1/89, the fibonacci series (sort of)
let thenVal = FloatType.double.constant(1/89)
builder.buildStore(thenVal, to: local)
// Branch to the merge block
builder.buildBr(mergeBB)

// MARK: Else Block
builder.positionAtEnd(of: elseBB)
// local = 1/109, the fibonacci series (sort of) backwards
let elseVal = FloatType.double.constant(1/109)
builder.buildStore(elseVal, to: local)
// Branch to the merge block
builder.buildBr(mergeBB)

Expand All @@ -139,7 +137,9 @@ phi.addIncoming([
(thenVal, thenBB),
(elseVal, elseBB),
])
builder.buildRet(phi)
builder.buildStore(phi, to: local)
let ret = builder.buildLoad(local, name: "ret")
builder.buildRet(ret)
```

This program generates the following IR:
Expand All @@ -152,16 +152,16 @@ entry:
br i1 %1, label %then, label %else

then: ; preds = %entry
store double 0x3F8702E05C0B8170, double* %local
br label %merge

else: ; preds = %entry
store double 0x3F82C9FB4D812CA0, double* %local
br label %merge

merge: ; preds = %else, %then
%phi_example = phi double [ 0x3F8702E05C0B8170, %then ], [ 0x3F82C9FB4D812CA0, %else ]
ret double %phi_example
store double %phi_example, double* %local
%ret = load double, double* %local
ret double %ret
}
```

Expand Down