1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes='loop(indvars,loop-deletion),simplifycfg' -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck %s
4 ; Test that we can evaluate the exit values of various expression types. Since
5 ; these loops all have predictable exit values we can replace the use outside
6 ; of the loop with a closed-form computation.
8 define i32 @polynomial_constant() {
10 ; CHECK-LABEL: @polynomial_constant(
12 ; CHECK-NEXT: ret i32 500500
16 Loop: ; preds = %Loop, %0
17 %A1 = phi i32 [ 0, %0 ], [ %A2, %Loop ] ; <i32> [#uses=3]
18 %B1 = phi i32 [ 0, %0 ], [ %B2, %Loop ] ; <i32> [#uses=1]
19 %A2 = add i32 %A1, 1 ; <i32> [#uses=1]
20 %B2 = add i32 %B1, %A1 ; <i32> [#uses=2]
21 %C = icmp eq i32 %A1, 1000 ; <i1> [#uses=1]
22 br i1 %C, label %Out, label %Loop
28 define i32 @NSquare(i32 %N) {
30 ; CHECK-LABEL: @NSquare(
32 ; CHECK-NEXT: [[Y:%.*]] = mul i32 [[N:%.*]], [[N]]
33 ; CHECK-NEXT: ret i32 [[Y]]
37 Loop: ; preds = %Loop, %0
38 %X = phi i32 [ 0, %0 ], [ %X2, %Loop ] ; <i32> [#uses=4]
39 %X2 = add i32 %X, 1 ; <i32> [#uses=1]
40 %c = icmp eq i32 %X, %N ; <i1> [#uses=1]
41 br i1 %c, label %Out, label %Loop
44 %Y = mul i32 %X, %X ; <i32> [#uses=1]
48 define i32 @NSquareOver2(i32 %N) {
50 ; CHECK-LABEL: @NSquareOver2(
52 ; CHECK-NEXT: [[TMP0:%.*]] = zext i32 [[N:%.*]] to i33
53 ; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[N]], -1
54 ; CHECK-NEXT: [[TMP2:%.*]] = zext i32 [[TMP1]] to i33
55 ; CHECK-NEXT: [[TMP3:%.*]] = mul i33 [[TMP0]], [[TMP2]]
56 ; CHECK-NEXT: [[TMP4:%.*]] = lshr i33 [[TMP3]], 1
57 ; CHECK-NEXT: [[TMP5:%.*]] = trunc i33 [[TMP4]] to i32
58 ; CHECK-NEXT: [[TMP6:%.*]] = add i32 [[N]], [[TMP5]]
59 ; CHECK-NEXT: [[TMP7:%.*]] = add i32 [[TMP6]], 15
60 ; CHECK-NEXT: ret i32 [[TMP7]]
64 Loop: ; preds = %Loop, %0
65 %X = phi i32 [ 0, %0 ], [ %X2, %Loop ] ; <i32> [#uses=3]
66 %Y = phi i32 [ 15, %0 ], [ %Y2, %Loop ] ; <i32> [#uses=1]
67 %Y2 = add i32 %Y, %X ; <i32> [#uses=2]
68 %X2 = add i32 %X, 1 ; <i32> [#uses=1]
69 %c = icmp eq i32 %X, %N ; <i1> [#uses=1]
70 br i1 %c, label %Out, label %Loop
76 define i32 @strength_reduced() {
78 ; CHECK-LABEL: @strength_reduced(
80 ; CHECK-NEXT: ret i32 500500
84 Loop: ; preds = %Loop, %0
85 %A1 = phi i32 [ 0, %0 ], [ %A2, %Loop ] ; <i32> [#uses=3]
86 %B1 = phi i32 [ 0, %0 ], [ %B2, %Loop ] ; <i32> [#uses=1]
87 %A2 = add i32 %A1, 1 ; <i32> [#uses=1]
88 %B2 = add i32 %B1, %A1 ; <i32> [#uses=2]
89 %C = icmp eq i32 %A1, 1000 ; <i1> [#uses=1]
90 br i1 %C, label %Out, label %Loop
96 define i32 @chrec_equals() {
97 ; CHECK-LABEL: @chrec_equals(
99 ; CHECK-NEXT: ret i32 101
104 no_exit: ; preds = %no_exit, %entry
105 %i0 = phi i32 [ 0, %entry ], [ %i1, %no_exit ] ; <i32> [#uses=3]
106 %ISq = mul i32 %i0, %i0 ; <i32> [#uses=1]
107 %i1 = add i32 %i0, 1 ; <i32> [#uses=2]
108 %tmp.1 = icmp ne i32 %ISq, 10000 ; <i1> [#uses=1]
109 br i1 %tmp.1, label %no_exit, label %loopexit
111 loopexit: ; preds = %no_exit
115 define i16 @cast_chrec_test() {
117 ; CHECK-LABEL: @cast_chrec_test(
119 ; CHECK-NEXT: ret i16 1000
123 Loop: ; preds = %Loop, %0
124 %A1 = phi i32 [ 0, %0 ], [ %A2, %Loop ] ; <i32> [#uses=2]
125 %B1 = trunc i32 %A1 to i16 ; <i16> [#uses=2]
126 %A2 = add i32 %A1, 1 ; <i32> [#uses=1]
127 %C = icmp eq i16 %B1, 1000 ; <i1> [#uses=1]
128 br i1 %C, label %Out, label %Loop
134 define i32 @linear_div_fold() {
135 ; CHECK-LABEL: @linear_div_fold(
137 ; CHECK-NEXT: ret i32 34
142 loop: ; preds = %loop, %entry
143 %i = phi i32 [ 4, %entry ], [ %i.next, %loop ] ; <i32> [#uses=3]
144 %i.next = add i32 %i, 8 ; <i32> [#uses=1]
145 %RV = udiv i32 %i, 2 ; <i32> [#uses=1]
146 %c = icmp ne i32 %i, 68 ; <i1> [#uses=1]
147 br i1 %c, label %loop, label %loopexit
149 loopexit: ; preds = %loop
153 define i32 @unroll_phi_select_constant_nonzero(i32 %arg1, i32 %arg2) {
154 ; CHECK-LABEL: @unroll_phi_select_constant_nonzero(
156 ; CHECK-NEXT: ret i32 [[ARG2:%.*]]
162 %i = phi i32 [ 0, %entry ], [ %i.next, %loop ]
163 %selector = phi i32 [%arg1, %entry], [%arg2, %loop]
164 %i.next = add nsw nuw i32 %i, 1
165 %c = icmp ult i32 %i, 4
166 br i1 %c, label %loop, label %loopexit
172 define i32 @unroll_phi_select_constant_nonzero_large_btc(i32 %arg1, i32 %arg2) {
173 ; CHECK-LABEL: @unroll_phi_select_constant_nonzero_large_btc(
175 ; CHECK-NEXT: ret i32 [[ARG2:%.*]]
181 %i = phi i32 [ 0, %entry ], [ %i.next, %loop ]
182 %selector = phi i32 [%arg1, %entry], [%arg2, %loop]
183 %i.next = add nuw i32 %i, 1
184 %c = icmp ult i32 %i, -42
185 br i1 %c, label %loop, label %loopexit
193 ; After LCSSA formation, there's no LCSSA phi for %f since it isn't directly
194 ; used outside the loop, and thus we can't directly replace %selector w/ %f.
195 define i32 @neg_unroll_phi_select_constant_nonzero(i32 %arg) {
196 ; CHECK-LABEL: @neg_unroll_phi_select_constant_nonzero(
198 ; CHECK-NEXT: br label [[LOOP:%.*]]
200 ; CHECK-NEXT: [[I:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[I_NEXT:%.*]], [[LOOP]] ]
201 ; CHECK-NEXT: [[SELECTOR:%.*]] = phi i32 [ [[ARG:%.*]], [[ENTRY]] ], [ [[F:%.*]], [[LOOP]] ]
202 ; CHECK-NEXT: [[F]] = call i32 @f()
203 ; CHECK-NEXT: [[I_NEXT]] = add nuw nsw i32 [[I]], 1
204 ; CHECK-NEXT: [[C:%.*]] = icmp ult i32 [[I]], 4
205 ; CHECK-NEXT: br i1 [[C]], label [[LOOP]], label [[LOOPEXIT:%.*]]
207 ; CHECK-NEXT: [[SELECTOR_LCSSA:%.*]] = phi i32 [ [[SELECTOR]], [[LOOP]] ]
208 ; CHECK-NEXT: ret i32 [[SELECTOR_LCSSA]]
214 %i = phi i32 [ 0, %entry ], [ %i.next, %loop ]
215 %selector = phi i32 [%arg, %entry], [%f, %loop]
217 %i.next = add nsw nuw i32 %i, 1
218 %c = icmp ult i32 %i, 4
219 br i1 %c, label %loop, label %loopexit
226 define i32 @unroll_phi_select_constant_zero(i32 %arg1, i32 %arg2) {
227 ; CHECK-LABEL: @unroll_phi_select_constant_zero(
229 ; CHECK-NEXT: ret i32 [[ARG1:%.*]]
235 %i = phi i32 [ 0, %entry ], [ %i.next, %loop ]
236 %selector = phi i32 [%arg1, %entry], [%arg2, %loop]
237 %i.next = add i32 %i, 1
238 %c = icmp ne i32 %i, 0
239 br i1 %c, label %loop, label %loopexit
245 define i32 @unroll_phi_select(i32 %arg1, i32 %arg2, i16 %len) {
246 ; CHECK-LABEL: @unroll_phi_select(
248 ; CHECK-NEXT: ret i32 [[ARG2:%.*]]
251 %length = zext i16 %len to i32
255 %i = phi i32 [ -1, %entry ], [ %i.next, %loop ]
256 %selector = phi i32 [%arg1, %entry], [%arg2, %loop]
257 %i.next = add nsw i32 %i, 1
258 %c = icmp slt i32 %i, %length
259 br i1 %c, label %loop, label %loopexit