@@ -54,7 +54,7 @@ pub(crate) enum Terminator {
5454 } ,
5555
5656 /// TODO: doc
57- Return ,
57+ Return { end : BasicBlockKey } ,
5858}
5959
6060impl Terminator {
@@ -89,6 +89,7 @@ impl Terminator {
8989/// TODO: doc
9090pub struct ControlFlowGraph {
9191 basic_block_start : BasicBlockKey ,
92+ basic_block_end : BasicBlockKey ,
9293 basic_blocks : SlotMap < BasicBlockKey , BasicBlock > ,
9394}
9495
@@ -122,19 +123,26 @@ impl Debug for ControlFlowGraph {
122123 if basic_block. reachable( ) { "" } else { "not " }
123124 ) ?;
124125
125- if !basic_block. predecessors . is_empty ( ) {
126- write ! ( f, " -- predecessors " ) ?;
127- for predecessor in & basic_block. predecessors {
126+ if key == self . basic_block_start {
127+ write ! ( f, " -- start" ) ?;
128+ }
129+ if key == self . basic_block_end {
130+ write ! ( f, " -- end" ) ?;
131+ }
132+
133+ if !basic_block. previous . is_empty ( ) {
134+ write ! ( f, " -- previous " ) ?;
135+ for predecessor in & basic_block. previous {
128136 let index = index_from_basic_block ( * predecessor) ;
129137 write ! ( f, "B{index}, " ) ?;
130138 }
131139 }
132140
133- let successors = basic_block. successors ( ) ;
134- if !successors . is_empty ( ) {
135- write ! ( f, " -- successors " ) ?;
136- for successor in & successors {
137- let index = index_from_basic_block ( * successor ) ;
141+ let next = basic_block. next ( ) ;
142+ if !next . is_empty ( ) {
143+ write ! ( f, " -- next " ) ?;
144+ for bb in & next {
145+ let index = index_from_basic_block ( * bb ) ;
138146 write ! ( f, "B{index}, " ) ?;
139147 }
140148 }
@@ -171,8 +179,9 @@ impl Debug for ControlFlowGraph {
171179 let target = index_from_basic_block ( * yes) ;
172180 write ! ( f, "TemplateLookup B{target}" ) ?;
173181 }
174- Terminator :: Return => {
175- write ! ( f, "Return" ) ?;
182+ Terminator :: Return { end } => {
183+ let target = index_from_basic_block ( * end) ;
184+ write ! ( f, "Return B{target}" ) ?;
176185 }
177186 }
178187 writeln ! ( f) ?;
@@ -271,6 +280,8 @@ impl ControlFlowGraph {
271280 basic_block_keys[ index]
272281 } ;
273282
283+ let basic_block_end = basic_block_keys[ leaders. len ( ) - 1 ] ;
284+
274285 let mut iter = InstructionIterator :: new ( bytecode) ;
275286 for ( i, leader) in leaders
276287 . iter ( )
@@ -296,12 +307,14 @@ impl ControlFlowGraph {
296307 while let Some ( ( _, _, instruction) ) = iter. next ( ) {
297308 match instruction {
298309 Instruction :: Return => {
299- terminator = Terminator :: Return ;
310+ terminator = Terminator :: Return {
311+ end : basic_block_end,
312+ } ;
300313 }
301314 Instruction :: Jump { address } | Instruction :: Default { address } => {
302315 let target = basic_block_from_bytecode_position ( address) ;
303316
304- basic_blocks[ target] . predecessors . push ( key) ;
317+ basic_blocks[ target] . previous . push ( key) ;
305318
306319 terminator = Terminator :: JumpUnconditional {
307320 opcode : instruction. opcode ( ) ,
@@ -315,8 +328,8 @@ impl ControlFlowGraph {
315328 let yes = basic_block_from_bytecode_position ( address) ;
316329 let no = basic_block_keys[ i + 1 ] ;
317330
318- basic_blocks[ yes] . predecessors . push ( key) ;
319- basic_blocks[ no] . predecessors . push ( key) ;
331+ basic_blocks[ yes] . previous . push ( key) ;
332+ basic_blocks[ no] . previous . push ( key) ;
320333
321334 terminator = Terminator :: TemplateLookup { no, yes, site } ;
322335 }
@@ -325,8 +338,8 @@ impl ControlFlowGraph {
325338 let yes = basic_block_from_bytecode_position ( address) ;
326339 let no = basic_block_keys[ i + 1 ] ;
327340
328- basic_blocks[ yes] . predecessors . push ( key) ;
329- basic_blocks[ no] . predecessors . push ( key) ;
341+ basic_blocks[ yes] . previous . push ( key) ;
342+ basic_blocks[ no] . previous . push ( key) ;
330343
331344 terminator = Terminator :: JumpConditional {
332345 opcode : instruction. opcode ( ) ,
@@ -351,6 +364,7 @@ impl ControlFlowGraph {
351364
352365 Self {
353366 basic_block_start : basic_block_keys[ 0 ] ,
367+ basic_block_end,
354368 basic_blocks,
355369 }
356370 }
@@ -520,7 +534,7 @@ impl GraphEliminateUnreachableBasicBlocks {
520534 // "reachable basic blocks should not be eliminated"
521535 // );
522536
523- // basic_block.predecessors .clear();
537+ // basic_block.previous .clear();
524538 // basic_block.terminator = Terminator::None;
525539
526540 // changed |= true;
0 commit comments