@@ -59,6 +59,19 @@ struct InstructionRange : CustomStringConvertible, NoReflectionChildren {
5959 self . inExclusiveRange. insert ( beginInst)
6060 }
6161
62+ init ( for value: Value , _ context: some Context ) {
63+ var begin : Instruction
64+ if let def = value. definingInstruction {
65+ begin = def
66+ } else if let result = TerminatorResult ( value) {
67+ begin = result. terminator
68+ } else {
69+ assert ( Phi ( value) != nil || value is FunctionArgument )
70+ begin = value. parentBlock. instructions. first!
71+ }
72+ self = InstructionRange ( begin: begin, context)
73+ }
74+
6275 /// Insert a potential end instruction.
6376 mutating func insert( _ inst: Instruction ) {
6477 insertedInsts. insert ( inst)
@@ -103,12 +116,21 @@ struct InstructionRange : CustomStringConvertible, NoReflectionChildren {
103116 }
104117
105118 /// Returns the end instructions.
119+ ///
120+ /// Warning: this returns `begin` if no instructions were inserted.
106121 var ends : LazyMapSequence < LazyFilterSequence < Stack < BasicBlock > > , Instruction > {
107122 blockRange. ends. map {
108123 $0. instructions. reversed ( ) . first ( where: { insertedInsts. contains ( $0) } ) !
109124 }
110125 }
111126
127+ // Returns the exit blocks.
128+ var exitBlocks : LazySequence < FlattenSequence <
129+ LazyMapSequence < LazyFilterSequence < Stack < BasicBlock > > ,
130+ LazyFilterSequence < SuccessorArray > > > > {
131+ blockRange. exits
132+ }
133+
112134 /// Returns the exit instructions.
113135 var exits : LazyMapSequence < LazySequence < FlattenSequence <
114136 LazyMapSequence < LazyFilterSequence < Stack < BasicBlock > > ,
0 commit comments