Skip to content

Commit 6a726e9

Browse files
committed
[SCEV] Add more tests for MUL/UDIV folds.
Add additional test coverage for #157656 covering cases where the dividend is not a multiple of the divisor, causing the revert of 70012fd.
1 parent 6a16f1a commit 6a726e9

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

llvm/test/Analysis/ScalarEvolution/mul-udiv-folds.ll

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,68 @@ loop:
123123
exit:
124124
ret void
125125
}
126+
127+
declare void @use.i64(i64)
128+
129+
define void @dividend_not_known_multiple_of_divisor(i64 %x) {
130+
; CHECK-LABEL: 'dividend_not_known_multiple_of_divisor'
131+
; CHECK-NEXT: Classifying expressions for: @dividend_not_known_multiple_of_divisor
132+
; CHECK-NEXT: %mul.2 = shl i64 %x, 1
133+
; CHECK-NEXT: --> (2 * %x) U: [0,-1) S: [-9223372036854775808,9223372036854775807)
134+
; CHECK-NEXT: %div.16 = lshr exact i64 %mul.2, 4
135+
; CHECK-NEXT: --> ((2 * %x) /u 16) U: [0,1152921504606846976) S: [0,1152921504606846976)
136+
; CHECK-NEXT: %m2 = and i64 %div.16, 1152921504606846974
137+
; CHECK-NEXT: --> (2 * ((2 * %x) /u 32))<nuw><nsw> U: [0,1152921504606846975) S: [0,1152921504606846975)
138+
; CHECK-NEXT: %m3 = mul i64 %div.16, 2
139+
; CHECK-NEXT: --> (2 * ((2 * %x) /u 16))<nuw><nsw> U: [0,2305843009213693951) S: [0,2305843009213693951)
140+
; CHECK-NEXT: %m4 = udiv i64 %m3, 4
141+
; CHECK-NEXT: --> ((2 * ((2 * %x) /u 16))<nuw><nsw> /u 4) U: [0,576460752303423488) S: [0,576460752303423488)
142+
; CHECK-NEXT: Determining loop execution counts for: @dividend_not_known_multiple_of_divisor
143+
;
144+
entry:
145+
%mul.2 = shl i64 %x, 1
146+
%div.16 = lshr exact i64 %mul.2, 4
147+
%m2 = and i64 %div.16, 1152921504606846974
148+
call void @use.i64(i64 %m2)
149+
150+
%m3 = mul i64 %div.16, 2
151+
%m4 = udiv i64 %m3, 4
152+
call void @use.i64(i64 %m4)
153+
ret void
154+
}
155+
156+
define void @btc_depends_on_div_mul(i64 %x) {
157+
; CHECK-LABEL: 'btc_depends_on_div_mul'
158+
; CHECK-NEXT: Classifying expressions for: @btc_depends_on_div_mul
159+
; CHECK-NEXT: %mul.2 = shl i64 %x, 1
160+
; CHECK-NEXT: --> (2 * %x) U: [0,-1) S: [-9223372036854775808,9223372036854775807)
161+
; CHECK-NEXT: %div.16 = lshr exact i64 %mul.2, 4
162+
; CHECK-NEXT: --> ((2 * %x) /u 16) U: [0,1152921504606846976) S: [0,1152921504606846976)
163+
; CHECK-NEXT: %masked = and i64 %div.16, 1152921504606846974
164+
; CHECK-NEXT: --> (2 * ((2 * %x) /u 32))<nuw><nsw> U: [0,1152921504606846975) S: [0,1152921504606846975)
165+
; CHECK-NEXT: %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
166+
; CHECK-NEXT: --> {0,+,2}<%loop> U: [0,-1) S: [-9223372036854775808,9223372036854775807) Exits: (-2 + (2 * ((2 * %x) /u 32))<nuw><nsw>)<nsw> LoopDispositions: { %loop: Computable }
167+
; CHECK-NEXT: %iv.next = add i64 %iv, 2
168+
; CHECK-NEXT: --> {2,+,2}<%loop> U: [0,-1) S: [-9223372036854775808,9223372036854775807) Exits: (2 * ((2 * %x) /u 32))<nuw><nsw> LoopDispositions: { %loop: Computable }
169+
; CHECK-NEXT: Determining loop execution counts for: @btc_depends_on_div_mul
170+
; CHECK-NEXT: Loop %loop: backedge-taken count is ((-2 + (2 * ((2 * %x) /u 32))<nuw><nsw>)<nsw> /u 2)
171+
; CHECK-NEXT: Loop %loop: constant max backedge-taken count is i64 9223372036854775807
172+
; CHECK-NEXT: Loop %loop: symbolic max backedge-taken count is ((-2 + (2 * ((2 * %x) /u 32))<nuw><nsw>)<nsw> /u 2)
173+
; CHECK-NEXT: Loop %loop: Trip multiple is 1
174+
;
175+
entry:
176+
%mul.2 = shl i64 %x, 1
177+
%div.16 = lshr exact i64 %mul.2, 4
178+
%masked = and i64 %div.16, 1152921504606846974
179+
br label %loop
180+
181+
loop:
182+
%iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
183+
call void @use.i64(i64 %iv)
184+
%iv.next = add i64 %iv, 2
185+
%ec = icmp eq i64 %iv.next, %masked
186+
br i1 %ec, label %exit, label %loop
187+
188+
exit:
189+
ret void
190+
}

llvm/test/Transforms/LoopStrengthReduce/duplicated-phis.ll

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,41 @@ for.end:
8484
%res.0.lcssa = phi i64 [ 0, %entry ], [ %spec.select, %for.end.loopexit.unr-lcssa ]
8585
ret i64 %res.0.lcssa
8686
}
87+
88+
define i64 @duplicated_phis_compare_uses_mul_udiv(i64 %x) {
89+
; CHECK-LABEL: define i64 @duplicated_phis_compare_uses_mul_udiv(
90+
; CHECK-SAME: i64 [[X:%.*]]) {
91+
; CHECK-NEXT: [[ENTRY:.*]]:
92+
; CHECK-NEXT: [[MUL_2:%.*]] = shl i64 [[X]], 1
93+
; CHECK-NEXT: [[DIV_16:%.*]] = lshr i64 [[MUL_2]], 4
94+
; CHECK-NEXT: [[MASKED:%.*]] = and i64 [[DIV_16]], 1152921504606846974
95+
; CHECK-NEXT: br label %[[LOOP:.*]]
96+
; CHECK: [[LOOP]]:
97+
; CHECK-NEXT: [[IV_1:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[IV_1_NEXT:%.*]], %[[LOOP]] ]
98+
; CHECK-NEXT: call void @clobber()
99+
; CHECK-NEXT: [[IV_1_NEXT]] = add i64 [[IV_1]], 2
100+
; CHECK-NEXT: [[EC:%.*]] = icmp eq i64 [[MASKED]], [[IV_1_NEXT]]
101+
; CHECK-NEXT: br i1 [[EC]], label %[[EXIT:.*]], label %[[LOOP]]
102+
; CHECK: [[EXIT]]:
103+
; CHECK-NEXT: ret i64 [[IV_1_NEXT]]
104+
;
105+
entry:
106+
%mul.2 = shl i64 %x, 1
107+
%div.16 = lshr exact i64 %mul.2, 4
108+
%masked = and i64 %div.16, 1152921504606846974
109+
br label %loop
110+
111+
loop:
112+
%iv.1 = phi i64 [ 0, %entry ], [ %iv.1.next, %loop ]
113+
%iv.2 = phi i64 [ 0, %entry ], [ %iv.2.next, %loop ]
114+
call void @clobber()
115+
%iv.1.next = add i64 %iv.1, 2
116+
%iv.2.next = add i64 %iv.2, 2
117+
%ec = icmp eq i64 %iv.2.next, %masked
118+
br i1 %ec, label %exit, label %loop
119+
120+
exit:
121+
ret i64 %iv.1.next
122+
}
123+
124+
declare void @clobber()

0 commit comments

Comments
 (0)