Skip to content

Commit 40363d8

Browse files
committed
Optimizer: fix the InstructionRange utility for control flow graphs with unreachable blocks
`InstructionRange.contains` did yield a wrong result if the range ended in the begin-block and another instruction was inserted in an unreachable block.
1 parent 1f304e5 commit 40363d8

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

SwiftCompilerSources/Sources/Optimizer/DataStructures/BasicBlockRange.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,29 @@ struct BasicBlockRange : CustomStringConvertible, NoReflectionChildren {
7777
}
7878

7979
/// Insert a potential end block.
80-
mutating func insert(_ block: BasicBlock) {
80+
///
81+
/// Returns true if the begin-block is reached during backward propagation.
82+
/// Usually this is not relevant, but InstructionRange needs this information.
83+
@discardableResult
84+
mutating func insert(_ block: BasicBlock) -> Bool {
8185
if wasInserted.insert(block) {
8286
inserted.append(block)
8387
}
8488
worklist.pushIfNotVisited(block)
89+
var visitedBeginBlock = false
8590
while let b = worklist.pop() {
8691
inclusiveRange.append(b)
8792
if b != begin {
8893
for pred in b.predecessors {
94+
if pred == begin {
95+
visitedBeginBlock = true
96+
}
8997
worklist.pushIfNotVisited(pred)
9098
inExclusiveRange.insert(pred)
9199
}
92100
}
93101
}
102+
return visitedBeginBlock
94103
}
95104

96105
/// Insert a sequence of potential end blocks.

SwiftCompilerSources/Sources/Optimizer/DataStructures/InstructionRange.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ struct InstructionRange : CustomStringConvertible, NoReflectionChildren {
8080
mutating func insert(_ inst: Instruction) {
8181
insertedInsts.insert(inst)
8282
insertIntoRange(instructions: ReverseInstructionList(first: inst.previous))
83-
blockRange.insert(inst.parentBlock)
84-
if inst.parentBlock != blockRange.begin {
83+
if blockRange.insert(inst.parentBlock) {
8584
// The first time an instruction is inserted in another block than the begin-block we need to insert
8685
// instructions from the begin instruction to the end of the begin block.
8786
// For subsequent insertions this is a no-op: `insertIntoRange` will return immediately because those

0 commit comments

Comments
 (0)