Skip to content

Commit 99079ec

Browse files
committed
ValueDefUseWalker: fix a problem with cond_br
Don't crash when visiting the cond_br condition operand.
1 parent 4a60ea8 commit 99079ec

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/WalkUtils.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,14 @@ extension ValueDefUseWalker {
359359
return .continueWalk
360360
}
361361
case let cbr as CondBranchInst:
362-
let val = cbr.getArgument(for: operand)
363-
if let path = walkDownCache.needWalk(for: val, path: path) {
364-
return walkDownUses(ofValue: val, path: path)
362+
if let val = cbr.getArgument(for: operand) {
363+
if let path = walkDownCache.needWalk(for: val, path: path) {
364+
return walkDownUses(ofValue: val, path: path)
365+
} else {
366+
return .continueWalk
367+
}
365368
} else {
366-
return .continueWalk
369+
return leafUse(value: operand, path: path)
367370
}
368371
case let se as SwitchEnumInst:
369372
if let (caseIdx, path) = path.pop(kind: .enumCase),

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,7 @@ final public class TryApplyInst : TermInst, FullApplySite {
759759
final public class BranchInst : TermInst {
760760
public var targetBlock: BasicBlock { BranchInst_getTargetBlock(bridged).block }
761761

762+
/// Returns the target block argument for the cond_br `operand`.
762763
public func getArgument(for operand: Operand) -> Argument {
763764
return targetBlock.arguments[operand.index]
764765
}
@@ -776,8 +777,15 @@ final public class CondBranchInst : TermInst {
776777
return ops[(CondBranchInst_getNumTrueArgs(bridged) &+ 1)..<ops.count]
777778
}
778779

779-
public func getArgument(for operand: Operand) -> Argument {
780-
let argIdx = operand.index - 1
780+
/// Returns the true or false block argument for the cond_br `operand`.
781+
///
782+
/// Return nil if `operand` is the condition itself.
783+
public func getArgument(for operand: Operand) -> Argument? {
784+
let opIdx = operand.index
785+
if opIdx == 0 {
786+
return nil
787+
}
788+
let argIdx = opIdx - 1
781789
let numTrueArgs = CondBranchInst_getNumTrueArgs(bridged)
782790
if (0..<numTrueArgs).contains(argIdx) {
783791
return trueBlock.arguments[argIdx]

0 commit comments

Comments
 (0)