@@ -24,14 +24,9 @@ fn find_block_index_from_id(blocks: &[Block], id: &Word) -> usize {
2424 panic ! ( "Failed to find block from id" ) ;
2525}
2626
27- fn get_possible_merge_positions (
28- sess : Option < & Session > ,
29- blocks : & [ Block ] ,
30- start : Word ,
31- ) -> Vec < usize > {
27+ fn get_possible_merge_positions ( blocks : & [ Block ] , start : Word ) -> Vec < usize > {
3228 let mut retval = Vec :: new ( ) ;
3329 let mut next: VecDeque < Word > = VecDeque :: new ( ) ;
34- let mut processed = Vec :: new ( ) ;
3530 next. push_back ( start) ;
3631
3732 while let Some ( front) = next. pop_front ( ) {
@@ -43,19 +38,28 @@ fn get_possible_merge_positions(
4338 retval. push ( find_block_index_from_id ( blocks, & new_edges[ 0 ] ) ) ;
4439 }
4540
46- if processed. contains ( & front) {
47- if let Some ( sess) = sess {
48- sess. err ( "Loops are unsupported" ) ;
49- }
50- break ;
51- } else {
52- processed. push ( front) ;
41+ next. extend ( new_edges) ;
42+ }
43+
44+ retval
45+ }
46+
47+ fn block_is_loop ( blocks : & [ Block ] , start : Word ) -> bool {
48+ let mut next: VecDeque < Word > = VecDeque :: new ( ) ;
49+ next. push_back ( start) ;
50+
51+ while let Some ( front) = next. pop_front ( ) {
52+ let block_idx = find_block_index_from_id ( blocks, & front) ;
53+ let new_edges = outgoing_edges ( & blocks[ block_idx] ) ;
54+
55+ if new_edges. contains ( & start) {
56+ return true ;
5357 }
5458
5559 next. extend ( new_edges) ;
5660 }
5761
58- retval
62+ false
5963}
6064
6165fn ends_in_return ( block : & Block ) -> bool {
@@ -72,6 +76,12 @@ pub fn insert_selection_merge_on_conditional_branch(
7276
7377 // Find conditional branch
7478 for ( bi, block) in blocks. iter ( ) . enumerate ( ) {
79+ if block_is_loop ( blocks, block. label_id ( ) . unwrap ( ) ) {
80+ if let Some ( sess) = sess {
81+ sess. err ( "Loops are unsupported" ) ;
82+ }
83+ }
84+
7585 for ( ii, inst) in block. instructions . iter ( ) . enumerate ( ) {
7686 if inst. class . opcode == Op :: BranchConditional {
7787 branch_conditional_ops. push ( ( bi, ii) ) ;
@@ -85,8 +95,8 @@ pub fn insert_selection_merge_on_conditional_branch(
8595 if out. len ( ) != 2 {
8696 panic ! ( "Viktor missunderstood something" ) ;
8797 }
88- let a_nexts = get_possible_merge_positions ( sess , blocks, out[ 0 ] ) ;
89- let b_nexts = get_possible_merge_positions ( sess , blocks, out[ 1 ] ) ;
98+ let a_nexts = get_possible_merge_positions ( blocks, out[ 0 ] ) ;
99+ let b_nexts = get_possible_merge_positions ( blocks, out[ 1 ] ) ;
90100
91101 // Check for a matching possible merge position.
92102 let mut first_merge = None ;
0 commit comments