Skip to content

Commit 31143bf

Browse files
committed
Swift Optimizer: add the Builder.insert(after: Instruction ...) utility
For inserting new instruction after another instruction. This is especially interesting if the insertion point is a terminator. In this case, the new instruction(s) are inserted in the successor block(s).
1 parent e028239 commit 31143bf

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,21 @@ extension Value {
1717
uses.lazy.filter { !($0.instruction is DebugValueInst) }
1818
}
1919
}
20+
21+
extension Builder {
22+
static func insert(after inst: Instruction, location: Location,
23+
_ context: PassContext, insertFunc: (Builder) -> ()) {
24+
if inst is TermInst {
25+
for succ in inst.block.successors {
26+
assert(succ.hasSinglePredecessor,
27+
"the terminator instruction must not have critical successors")
28+
let builder = Builder(at: succ.instructions.first!, location: location,
29+
context)
30+
insertFunc(builder)
31+
}
32+
} else {
33+
let builder = Builder(at: inst.next!, location: location, context)
34+
insertFunc(builder)
35+
}
36+
}
37+
}

SwiftCompilerSources/Sources/SIL/BasicBlock.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ final public class BasicBlock : ListNode, CustomStringConvertible, HasName {
5252
}
5353
return nil
5454
}
55+
56+
public var hasSinglePredecessor: Bool { singlePredecessor != nil }
5557

5658
/// The index of the basic block in its function.
5759
/// This has O(n) complexity. Only use it for debugging

0 commit comments

Comments
 (0)