1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3
2 ; PR41445: This test checks the case when LSR split critical edge
3 ; and phi node has other pending fixup operands
5 ; RUN: opt -S -loop-reduce < %s | FileCheck %s
7 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
8 target triple = "x86_64-unknown-linux-gnu"
10 ; We have %indvars.iv.lcssa phi node where 4 input operands
11 ; need to be rewritten: %tmp1, %tmp2, %tmp3, %tmp4.
12 ; When we try to rewrite %tmp1, we first split the critical edge.
13 ; All the other PHI inputs besides %tmp1 go to a new phi node.
14 ; This test checks that LSR is still able to rewrite %tmp2, %tmp3, %tmp4.
15 define i32 @foo(ptr %A, i32 %t) {
16 ; CHECK-LABEL: define i32 @foo(
17 ; CHECK-SAME: ptr [[A:%.*]], i32 [[T:%.*]]) {
19 ; CHECK-NEXT: br label [[LOOP_32:%.*]]
20 ; CHECK: loop.exit.loopexitsplitsplitsplit:
21 ; CHECK-NEXT: [[TMP0:%.*]] = add i64 [[LSR_IV:%.*]], -1
22 ; CHECK-NEXT: br label [[LOOP_EXIT_LOOPEXITSPLITSPLIT:%.*]]
23 ; CHECK: ifmerge.38.loop.exit.loopexitsplitsplit_crit_edge:
24 ; CHECK-NEXT: [[LSR_IV_LCSSA10:%.*]] = phi i64 [ [[LSR_IV]], [[IFMERGE_38:%.*]] ]
25 ; CHECK-NEXT: br label [[LOOP_EXIT_LOOPEXITSPLITSPLIT]]
26 ; CHECK: loop.exit.loopexitsplitsplit:
27 ; CHECK-NEXT: [[INDVARS_IV_LCSSA_PH_PH_PH:%.*]] = phi i64 [ [[LSR_IV_LCSSA10]], [[IFMERGE_38_LOOP_EXIT_LOOPEXITSPLITSPLIT_CRIT_EDGE:%.*]] ], [ [[TMP0]], [[LOOP_EXIT_LOOPEXITSPLITSPLITSPLIT:%.*]] ]
28 ; CHECK-NEXT: br label [[LOOP_EXIT_LOOPEXITSPLIT:%.*]]
29 ; CHECK: ifmerge.42.loop.exit.loopexitsplit_crit_edge:
30 ; CHECK-NEXT: [[LSR_IV_LCSSA11:%.*]] = phi i64 [ [[LSR_IV]], [[IFMERGE_42:%.*]] ]
31 ; CHECK-NEXT: [[TMP1:%.*]] = add i64 [[LSR_IV_LCSSA11]], 1
32 ; CHECK-NEXT: br label [[LOOP_EXIT_LOOPEXITSPLIT]]
33 ; CHECK: loop.exit.loopexitsplit:
34 ; CHECK-NEXT: [[INDVARS_IV_LCSSA_PH_PH:%.*]] = phi i64 [ [[TMP1]], [[IFMERGE_42_LOOP_EXIT_LOOPEXITSPLIT_CRIT_EDGE:%.*]] ], [ [[INDVARS_IV_LCSSA_PH_PH_PH]], [[LOOP_EXIT_LOOPEXITSPLITSPLIT]] ]
35 ; CHECK-NEXT: br label [[LOOP_EXIT_LOOPEXIT:%.*]]
36 ; CHECK: then.34.loop.exit.loopexit_crit_edge:
37 ; CHECK-NEXT: [[LSR_IV_LCSSA:%.*]] = phi i64 [ [[LSR_IV]], [[THEN_34:%.*]] ]
38 ; CHECK-NEXT: [[TMP2:%.*]] = add i64 [[LSR_IV_LCSSA]], -2
39 ; CHECK-NEXT: br label [[LOOP_EXIT_LOOPEXIT]]
40 ; CHECK: loop.exit.loopexit:
41 ; CHECK-NEXT: [[INDVARS_IV_LCSSA_PH:%.*]] = phi i64 [ [[TMP2]], [[THEN_34_LOOP_EXIT_LOOPEXIT_CRIT_EDGE:%.*]] ], [ [[INDVARS_IV_LCSSA_PH_PH]], [[LOOP_EXIT_LOOPEXITSPLIT]] ]
42 ; CHECK-NEXT: br label [[LOOP_EXIT:%.*]]
44 ; CHECK-NEXT: [[INDVARS_IV_LCSSA:%.*]] = phi i64 [ 48, [[THEN_8:%.*]] ], [ 49, [[THEN_8_1:%.*]] ], [ [[INDVARS_IV_LCSSA_PH]], [[LOOP_EXIT_LOOPEXIT]] ]
45 ; CHECK-NEXT: [[TMP:%.*]] = trunc i64 [[INDVARS_IV_LCSSA]] to i32
46 ; CHECK-NEXT: br label [[FOR_END:%.*]]
48 ; CHECK-NEXT: [[I_0_LCSSA:%.*]] = phi i32 [ [[TMP]], [[LOOP_EXIT]] ], [ 50, [[THEN_8_1]] ], [ 50, [[IFMERGE_8:%.*]] ]
49 ; CHECK-NEXT: ret i32 [[I_0_LCSSA]]
51 ; CHECK-NEXT: [[LSR_IV]] = phi i64 [ [[LSR_IV_NEXT:%.*]], [[IFMERGE_46:%.*]] ], [ 2, [[ENTRY:%.*]] ]
52 ; CHECK-NEXT: [[I1_I64_0:%.*]] = phi i64 [ 0, [[ENTRY]] ], [ [[NEXTIVLOOP_32:%.*]], [[IFMERGE_46]] ]
53 ; CHECK-NEXT: [[TMP3:%.*]] = shl nuw nsw i64 [[LSR_IV]], 2
54 ; CHECK-NEXT: [[SCEVGEP7:%.*]] = getelementptr i8, ptr [[A]], i64 [[TMP3]]
55 ; CHECK-NEXT: [[SCEVGEP8:%.*]] = getelementptr i8, ptr [[SCEVGEP7]], i64 -4
56 ; CHECK-NEXT: [[GEPLOAD:%.*]] = load i32, ptr [[SCEVGEP8]], align 4
57 ; CHECK-NEXT: [[CMP_34:%.*]] = icmp sgt i32 [[GEPLOAD]], [[T]]
58 ; CHECK-NEXT: br i1 [[CMP_34]], label [[THEN_34]], label [[IFMERGE_34:%.*]]
60 ; CHECK-NEXT: [[TMP4:%.*]] = shl nuw nsw i64 [[LSR_IV]], 2
61 ; CHECK-NEXT: [[SCEVGEP5:%.*]] = getelementptr i8, ptr [[A]], i64 [[TMP4]]
62 ; CHECK-NEXT: [[SCEVGEP6:%.*]] = getelementptr i8, ptr [[SCEVGEP5]], i64 -8
63 ; CHECK-NEXT: [[GEPLOAD18:%.*]] = load i32, ptr [[SCEVGEP6]], align 4
64 ; CHECK-NEXT: [[CMP_35:%.*]] = icmp slt i32 [[GEPLOAD18]], [[T]]
65 ; CHECK-NEXT: br i1 [[CMP_35]], label [[THEN_34_LOOP_EXIT_LOOPEXIT_CRIT_EDGE]], label [[IFMERGE_34]]
67 ; CHECK-NEXT: [[TMP5:%.*]] = shl nuw nsw i64 [[LSR_IV]], 2
68 ; CHECK-NEXT: [[SCEVGEP4:%.*]] = getelementptr i8, ptr [[A]], i64 [[TMP5]]
69 ; CHECK-NEXT: [[GEPLOAD20:%.*]] = load i32, ptr [[SCEVGEP4]], align 4
70 ; CHECK-NEXT: [[CMP_38:%.*]] = icmp sgt i32 [[GEPLOAD20]], [[T]]
71 ; CHECK-NEXT: [[CMP_39:%.*]] = icmp slt i32 [[GEPLOAD]], [[T]]
72 ; CHECK-NEXT: [[OR_COND:%.*]] = and i1 [[CMP_38]], [[CMP_39]]
73 ; CHECK-NEXT: br i1 [[OR_COND]], label [[LOOP_EXIT_LOOPEXITSPLITSPLITSPLIT]], label [[IFMERGE_38]]
75 ; CHECK-NEXT: [[TMP6:%.*]] = shl nuw nsw i64 [[LSR_IV]], 2
76 ; CHECK-NEXT: [[SCEVGEP2:%.*]] = getelementptr i8, ptr [[A]], i64 [[TMP6]]
77 ; CHECK-NEXT: [[SCEVGEP3:%.*]] = getelementptr i8, ptr [[SCEVGEP2]], i64 4
78 ; CHECK-NEXT: [[GEPLOAD24:%.*]] = load i32, ptr [[SCEVGEP3]], align 4
79 ; CHECK-NEXT: [[CMP_42:%.*]] = icmp sgt i32 [[GEPLOAD24]], [[T]]
80 ; CHECK-NEXT: [[CMP_43:%.*]] = icmp slt i32 [[GEPLOAD20]], [[T]]
81 ; CHECK-NEXT: [[OR_COND55:%.*]] = and i1 [[CMP_42]], [[CMP_43]]
82 ; CHECK-NEXT: br i1 [[OR_COND55]], label [[IFMERGE_38_LOOP_EXIT_LOOPEXITSPLITSPLIT_CRIT_EDGE]], label [[IFMERGE_42]]
84 ; CHECK-NEXT: [[TMP7:%.*]] = shl nuw nsw i64 [[LSR_IV]], 2
85 ; CHECK-NEXT: [[SCEVGEP:%.*]] = getelementptr i8, ptr [[A]], i64 [[TMP7]]
86 ; CHECK-NEXT: [[SCEVGEP1:%.*]] = getelementptr i8, ptr [[SCEVGEP]], i64 8
87 ; CHECK-NEXT: [[GEPLOAD28:%.*]] = load i32, ptr [[SCEVGEP1]], align 4
88 ; CHECK-NEXT: [[CMP_46:%.*]] = icmp sgt i32 [[GEPLOAD28]], [[T]]
89 ; CHECK-NEXT: [[CMP_47:%.*]] = icmp slt i32 [[GEPLOAD24]], [[T]]
90 ; CHECK-NEXT: [[OR_COND56:%.*]] = and i1 [[CMP_46]], [[CMP_47]]
91 ; CHECK-NEXT: br i1 [[OR_COND56]], label [[IFMERGE_42_LOOP_EXIT_LOOPEXITSPLIT_CRIT_EDGE]], label [[IFMERGE_46]]
93 ; CHECK-NEXT: [[NEXTIVLOOP_32]] = add nuw nsw i64 [[I1_I64_0]], 1
94 ; CHECK-NEXT: [[LSR_IV_NEXT]] = add nuw nsw i64 [[LSR_IV]], 4
95 ; CHECK-NEXT: [[CONDLOOP_32:%.*]] = icmp ult i64 [[NEXTIVLOOP_32]], 12
96 ; CHECK-NEXT: br i1 [[CONDLOOP_32]], label [[LOOP_32]], label [[LOOP_25:%.*]]
98 ; CHECK-NEXT: [[ARRAYIDX31:%.*]] = getelementptr inbounds i32, ptr [[A]], i64 49
99 ; CHECK-NEXT: [[GEPLOAD32:%.*]] = load i32, ptr [[ARRAYIDX31]], align 4
100 ; CHECK-NEXT: [[CMP_8:%.*]] = icmp sgt i32 [[GEPLOAD32]], [[T]]
101 ; CHECK-NEXT: br i1 [[CMP_8]], label [[THEN_8]], label [[IFMERGE_8]]
103 ; CHECK-NEXT: [[ARRAYIDX33:%.*]] = getelementptr inbounds i32, ptr [[A]], i64 48
104 ; CHECK-NEXT: [[GEPLOAD34:%.*]] = load i32, ptr [[ARRAYIDX33]], align 4
105 ; CHECK-NEXT: [[CMP_15:%.*]] = icmp slt i32 [[GEPLOAD34]], [[T]]
106 ; CHECK-NEXT: br i1 [[CMP_15]], label [[LOOP_EXIT]], label [[IFMERGE_8]]
108 ; CHECK-NEXT: [[ARRAYIDX31_1:%.*]] = getelementptr inbounds i32, ptr [[A]], i64 50
109 ; CHECK-NEXT: [[GEPLOAD32_1:%.*]] = load i32, ptr [[ARRAYIDX31_1]], align 4
110 ; CHECK-NEXT: [[CMP_8_1:%.*]] = icmp sgt i32 [[GEPLOAD32_1]], [[T]]
111 ; CHECK-NEXT: br i1 [[CMP_8_1]], label [[THEN_8_1]], label [[FOR_END]]
113 ; CHECK-NEXT: [[ARRAYIDX33_1:%.*]] = getelementptr inbounds i32, ptr [[A]], i64 49
114 ; CHECK-NEXT: [[GEPLOAD34_1:%.*]] = load i32, ptr [[ARRAYIDX33_1]], align 4
115 ; CHECK-NEXT: [[CMP_15_1:%.*]] = icmp slt i32 [[GEPLOAD34_1]], [[T]]
116 ; CHECK-NEXT: br i1 [[CMP_15_1]], label [[LOOP_EXIT]], label [[FOR_END]]
121 loop.exit: ; preds = %then.8.1, %then.8, %ifmerge.42, %ifmerge.38, %ifmerge.34, %then.34
122 %indvars.iv.lcssa = phi i64 [ 48, %then.8 ], [ 49, %then.8.1 ], [ %tmp4, %ifmerge.42 ], [ %tmp3, %ifmerge.38 ], [ %tmp2, %ifmerge.34 ], [ %tmp1, %then.34 ]
123 %tmp = trunc i64 %indvars.iv.lcssa to i32
126 for.end: ; preds = %then.8.1, %ifmerge.8, %loop.exit
127 %i.0.lcssa = phi i32 [ %tmp, %loop.exit ], [ 50, %then.8.1 ], [ 50, %ifmerge.8 ]
130 ; shl instruction will be dead eliminated when all it's uses will be rewritten.
131 loop.32: ; preds = %ifmerge.46, %entry
132 %i1.i64.0 = phi i64 [ 0, %entry ], [ %nextivloop.32, %ifmerge.46 ]
133 %tmp1 = shl i64 %i1.i64.0, 2
134 %tmp2 = or disjoint i64 %tmp1, 1
135 %arrayIdx = getelementptr inbounds i32, ptr %A, i64 %tmp2
136 %gepload = load i32, ptr %arrayIdx, align 4
137 %cmp.34 = icmp sgt i32 %gepload, %t
138 br i1 %cmp.34, label %then.34, label %ifmerge.34
140 then.34: ; preds = %loop.32
141 %arrayIdx17 = getelementptr inbounds i32, ptr %A, i64 %tmp1
142 %gepload18 = load i32, ptr %arrayIdx17, align 4
143 %cmp.35 = icmp slt i32 %gepload18, %t
144 br i1 %cmp.35, label %loop.exit, label %ifmerge.34
146 ifmerge.34: ; preds = %then.34, %loop.32
147 %tmp3 = or disjoint i64 %tmp1, 2
148 %arrayIdx19 = getelementptr inbounds i32, ptr %A, i64 %tmp3
149 %gepload20 = load i32, ptr %arrayIdx19, align 4
150 %cmp.38 = icmp sgt i32 %gepload20, %t
151 %cmp.39 = icmp slt i32 %gepload, %t
152 %or.cond = and i1 %cmp.38, %cmp.39
153 br i1 %or.cond, label %loop.exit, label %ifmerge.38
155 ifmerge.38: ; preds = %ifmerge.34
156 %tmp4 = or disjoint i64 %tmp1, 3
157 %arrayIdx23 = getelementptr inbounds i32, ptr %A, i64 %tmp4
158 %gepload24 = load i32, ptr %arrayIdx23, align 4
159 %cmp.42 = icmp sgt i32 %gepload24, %t
160 %cmp.43 = icmp slt i32 %gepload20, %t
161 %or.cond55 = and i1 %cmp.42, %cmp.43
162 br i1 %or.cond55, label %loop.exit, label %ifmerge.42
164 ifmerge.42: ; preds = %ifmerge.38
165 %tmp5 = add i64 %tmp1, 4
166 %arrayIdx27 = getelementptr inbounds i32, ptr %A, i64 %tmp5
167 %gepload28 = load i32, ptr %arrayIdx27, align 4
168 %cmp.46 = icmp sgt i32 %gepload28, %t
169 %cmp.47 = icmp slt i32 %gepload24, %t
170 %or.cond56 = and i1 %cmp.46, %cmp.47
171 br i1 %or.cond56, label %loop.exit, label %ifmerge.46
173 ifmerge.46: ; preds = %ifmerge.42
174 %nextivloop.32 = add nuw nsw i64 %i1.i64.0, 1
175 %condloop.32 = icmp ult i64 %nextivloop.32, 12
176 br i1 %condloop.32, label %loop.32, label %loop.25
178 loop.25: ; preds = %ifmerge.46
179 %arrayIdx31 = getelementptr inbounds i32, ptr %A, i64 49
180 %gepload32 = load i32, ptr %arrayIdx31, align 4
181 %cmp.8 = icmp sgt i32 %gepload32, %t
182 br i1 %cmp.8, label %then.8, label %ifmerge.8
184 then.8: ; preds = %loop.25
185 %arrayIdx33 = getelementptr inbounds i32, ptr %A, i64 48
186 %gepload34 = load i32, ptr %arrayIdx33, align 4
187 %cmp.15 = icmp slt i32 %gepload34, %t
188 br i1 %cmp.15, label %loop.exit, label %ifmerge.8
190 ifmerge.8: ; preds = %then.8, %loop.25
191 %arrayIdx31.1 = getelementptr inbounds i32, ptr %A, i64 50
192 %gepload32.1 = load i32, ptr %arrayIdx31.1, align 4
193 %cmp.8.1 = icmp sgt i32 %gepload32.1, %t
194 br i1 %cmp.8.1, label %then.8.1, label %for.end
196 then.8.1: ; preds = %ifmerge.8
197 %arrayIdx33.1 = getelementptr inbounds i32, ptr %A, i64 49
198 %gepload34.1 = load i32, ptr %arrayIdx33.1, align 4
199 %cmp.15.1 = icmp slt i32 %gepload34.1, %t
200 br i1 %cmp.15.1, label %loop.exit, label %for.end