1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -unify-loop-exits -structurizecfg -S | FileCheck %s
4 ; The structurizer uses an RPO traversal over a region, along with a
5 ; manual hack that is meant to sort ensure that blocks within a loop
6 ; are all visited before visiting blocks outside the loop. But this
7 ; does not always work as expected. For example the results are
8 ; incorrect when multiple nested loops are involved.
10 ; The workaround for this is to unify loop exits. Each loop now
11 ; becomes an SESE region with a single header and a single exit. The
12 ; structurizer is a region pass, and it no longer sees the entire loop
13 ; nest in a single region. More importantly, for each loop, the only
14 ; block reachable outside the loop is the region exit, which avoids
15 ; any confusion in the hacked RPO traversal.
17 ; In the function below, B1 is an exiting block in outer loop H1. It's
18 ; successor inside the loop is the header of another loop H2. Due to
19 ; the incorrect traversal, B1 dominates all the blocks in the
20 ; structurized program, except the header H1.
22 define void @exiting-block(i1 %PredH1, i1 %PredB2, i1 %PredB1, i1 %PredH2) {
23 ; CHECK-LABEL: @exiting-block(
25 ; CHECK-NEXT: [[PREDH1_INV:%.*]] = xor i1 [[PREDH1:%.*]], true
26 ; CHECK-NEXT: [[PREDB2_INV:%.*]] = xor i1 [[PREDB2:%.*]], true
27 ; CHECK-NEXT: br label [[H1:%.*]]
29 ; CHECK-NEXT: br i1 [[PREDH1_INV]], label [[B1:%.*]], label [[FLOW3:%.*]]
31 ; CHECK-NEXT: [[TMP0:%.*]] = phi i1 [ true, [[B1]] ], [ undef, [[H1]] ]
32 ; CHECK-NEXT: [[TMP1:%.*]] = phi i1 [ [[PREDB1:%.*]], [[B1]] ], [ [[PREDH1]], [[H1]] ]
33 ; CHECK-NEXT: br i1 [[TMP1]], label [[H2:%.*]], label [[FLOW4:%.*]]
35 ; CHECK-NEXT: br i1 [[PREDH2:%.*]], label [[B2:%.*]], label [[FLOW:%.*]]
37 ; CHECK-NEXT: br i1 [[PREDB2_INV]], label [[L2:%.*]], label [[FLOW2:%.*]]
39 ; CHECK-NEXT: [[TMP2:%.*]] = phi i1 [ false, [[FLOW2]] ], [ true, [[H2]] ]
40 ; CHECK-NEXT: [[TMP3:%.*]] = phi i1 [ [[TMP5:%.*]], [[FLOW2]] ], [ true, [[H2]] ]
41 ; CHECK-NEXT: br i1 [[TMP3]], label [[LOOP_EXIT_GUARD1:%.*]], label [[H2]]
43 ; CHECK-NEXT: br label [[FLOW2]]
45 ; CHECK-NEXT: br label [[FLOW5:%.*]]
47 ; CHECK-NEXT: br label [[FLOW3]]
49 ; CHECK-NEXT: br label [[EXIT:%.*]]
51 ; CHECK-NEXT: ret void
53 ; CHECK-NEXT: [[TMP4:%.*]] = phi i1 [ false, [[L1:%.*]] ], [ true, [[LOOP_EXIT_GUARD1]] ]
54 ; CHECK-NEXT: br label [[FLOW4]]
55 ; CHECK: loop.exit.guard:
56 ; CHECK-NEXT: br i1 [[TMP6:%.*]], label [[C:%.*]], label [[EXIT]]
58 ; CHECK-NEXT: [[TMP5]] = phi i1 [ false, [[L2]] ], [ true, [[B2]] ]
59 ; CHECK-NEXT: br label [[FLOW]]
61 ; CHECK-NEXT: [[TMP6]] = phi i1 [ false, [[FLOW5]] ], [ [[TMP0]], [[FLOW3]] ]
62 ; CHECK-NEXT: [[TMP7:%.*]] = phi i1 [ [[TMP4]], [[FLOW5]] ], [ true, [[FLOW3]] ]
63 ; CHECK-NEXT: br i1 [[TMP7]], label [[LOOP_EXIT_GUARD:%.*]], label [[H1]]
64 ; CHECK: loop.exit.guard1:
65 ; CHECK-NEXT: br i1 [[TMP2]], label [[L1]], label [[FLOW5]]
70 H1: ; preds = %L1, %entry
71 br i1 %PredH1, label %H2, label %B1
73 H2: ; preds = %B1, %L2, %H1
74 br i1 %PredH2, label %B2, label %L1
77 br i1 %PredB2, label %exit, label %L2
86 br i1 %PredB1, label %H2, label %C
91 exit: ; preds = %C, %B2
95 ; The function below has three nested loops. Due to the incorrect
96 ; traversal, H2 dominates H3 in the structurized program, and the
97 ; backedge from L13 to H3 has no equivalent path.
99 define void @incorrect-backedge(i1 %PredH2, i1 %PredH3, i1 %PredL2, i1 %PredL13, i1 %PredL1)
100 ; CHECK-LABEL: @incorrect-backedge(
102 ; CHECK-NEXT: [[PREDH2_INV:%.*]] = xor i1 [[PREDH2:%.*]], true
103 ; CHECK-NEXT: [[PREDL2_INV:%.*]] = xor i1 [[PREDL2:%.*]], true
104 ; CHECK-NEXT: [[PREDH3_INV:%.*]] = xor i1 [[PREDH3:%.*]], true
105 ; CHECK-NEXT: [[PREDL13_INV:%.*]] = xor i1 [[PREDL13:%.*]], true
106 ; CHECK-NEXT: br label [[H1:%.*]]
108 ; CHECK-NEXT: br label [[H2:%.*]]
110 ; CHECK-NEXT: br i1 [[PREDH2_INV]], label [[H3:%.*]], label [[FLOW4:%.*]]
112 ; CHECK-NEXT: br i1 [[PREDH3_INV]], label [[L2:%.*]], label [[FLOW:%.*]]
114 ; CHECK-NEXT: br i1 [[PREDL2_INV]], label [[L13:%.*]], label [[FLOW3:%.*]]
116 ; CHECK-NEXT: [[TMP0:%.*]] = phi i1 [ [[TMP6:%.*]], [[FLOW3]] ], [ true, [[H3]] ]
117 ; CHECK-NEXT: [[TMP1:%.*]] = phi i1 [ false, [[FLOW3]] ], [ true, [[H3]] ]
118 ; CHECK-NEXT: [[TMP2:%.*]] = phi i1 [ [[TMP7:%.*]], [[FLOW3]] ], [ true, [[H3]] ]
119 ; CHECK-NEXT: br i1 [[TMP2]], label [[LOOP_EXIT_GUARD2:%.*]], label [[H3]]
121 ; CHECK-NEXT: br label [[FLOW3]]
123 ; CHECK-NEXT: [[TMP3:%.*]] = phi i1 [ [[TMP8:%.*]], [[LOOP_EXIT_GUARD1:%.*]] ], [ true, [[LOOP_EXIT_GUARD:%.*]] ]
124 ; CHECK-NEXT: [[TMP4:%.*]] = phi i1 [ false, [[LOOP_EXIT_GUARD1]] ], [ true, [[LOOP_EXIT_GUARD]] ]
125 ; CHECK-NEXT: br i1 [[TMP4]], label [[L1:%.*]], label [[FLOW6:%.*]]
127 ; CHECK-NEXT: br label [[FLOW6]]
129 ; CHECK-NEXT: [[TMP5:%.*]] = phi i1 [ [[PREDL1:%.*]], [[L1]] ], [ [[TMP3]], [[FLOW5:%.*]] ]
130 ; CHECK-NEXT: br i1 [[TMP5]], label [[EXIT:%.*]], label [[H1]]
132 ; CHECK-NEXT: ret void
133 ; CHECK: loop.exit.guard:
134 ; CHECK-NEXT: br i1 [[DOTINV:%.*]], label [[LOOP_EXIT_GUARD1]], label [[FLOW5]]
135 ; CHECK: loop.exit.guard1:
136 ; CHECK-NEXT: br label [[FLOW5]]
138 ; CHECK-NEXT: [[TMP6]] = phi i1 [ true, [[L13]] ], [ false, [[L2]] ]
139 ; CHECK-NEXT: [[TMP7]] = phi i1 [ [[PREDL13_INV]], [[L13]] ], [ true, [[L2]] ]
140 ; CHECK-NEXT: br label [[FLOW]]
142 ; CHECK-NEXT: [[TMP8]] = phi i1 [ [[TMP1]], [[LOOP_EXIT_GUARD2]] ], [ false, [[H2]] ]
143 ; CHECK-NEXT: [[TMP9:%.*]] = phi i1 [ false, [[LOOP_EXIT_GUARD2]] ], [ true, [[H2]] ]
144 ; CHECK-NEXT: [[TMP10:%.*]] = phi i1 [ [[TMP0]], [[LOOP_EXIT_GUARD2]] ], [ true, [[H2]] ]
145 ; CHECK-NEXT: [[DOTINV]] = xor i1 [[TMP9]], true
146 ; CHECK-NEXT: br i1 [[TMP10]], label [[LOOP_EXIT_GUARD]], label [[H2]]
147 ; CHECK: loop.exit.guard2:
148 ; CHECK-NEXT: br label [[FLOW4]]
158 br i1 %PredH2, label %L1, label %H3
161 br i1 %PredH3, label %exit, label %L2
164 br i1 %PredL2, label %H2, label %L13
167 br i1 %PredL13, label %H3, label %H1
170 br i1 %PredL1, label %exit, label %H1