1; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py
2; RUN: opt -S -analyze -enable-new-pm=0 -scalar-evolution < %s | FileCheck %s
3; RUN: opt -S -disable-output "-passes=print<scalar-evolution>" < %s 2>&1 | FileCheck %s
4
5; The obvious case.
6define i32 @udiv_biggerLshr(i32 %val) nounwind {
7; CHECK-LABEL: 'udiv_biggerLshr'
8; CHECK-NEXT:  Classifying expressions for: @udiv_biggerLshr
9; CHECK-NEXT:    %tmp1 = udiv i32 %val, 64
10; CHECK-NEXT:    -->  (%val /u 64) U: [0,67108864) S: [0,67108864)
11; CHECK-NEXT:    %tmp2 = mul i32 %tmp1, 16
12; CHECK-NEXT:    -->  (16 * (%val /u 64))<nuw><nsw> U: [0,1073741809) S: [0,1073741809)
13; CHECK-NEXT:  Determining loop execution counts for: @udiv_biggerLshr
14;
15  %tmp1 = udiv i32 %val, 64
16  %tmp2 = mul i32 %tmp1, 16
17  ret i32 %tmp2
18}
19
20define i32 @udiv_biggerShl(i32 %val) nounwind {
21; CHECK-LABEL: 'udiv_biggerShl'
22; CHECK-NEXT:  Classifying expressions for: @udiv_biggerShl
23; CHECK-NEXT:    %tmp1 = udiv i32 %val, 16
24; CHECK-NEXT:    -->  (%val /u 16) U: [0,268435456) S: [0,268435456)
25; CHECK-NEXT:    %tmp2 = mul i32 %tmp1, 64
26; CHECK-NEXT:    -->  (64 * (%val /u 16)) U: [0,-63) S: [-2147483648,2147483585)
27; CHECK-NEXT:  Determining loop execution counts for: @udiv_biggerShl
28;
29  %tmp1 = udiv i32 %val, 16
30  %tmp2 = mul i32 %tmp1, 64
31  ret i32 %tmp2
32}
33
34; Or, it could have been transformed to shifts
35
36define i32 @shifty_biggerLshr(i32 %val) {
37; CHECK-LABEL: 'shifty_biggerLshr'
38; CHECK-NEXT:  Classifying expressions for: @shifty_biggerLshr
39; CHECK-NEXT:    %tmp1 = lshr i32 %val, 6
40; CHECK-NEXT:    -->  (%val /u 64) U: [0,67108864) S: [0,67108864)
41; CHECK-NEXT:    %tmp2 = shl i32 %tmp1, 4
42; CHECK-NEXT:    -->  (16 * (%val /u 64))<nuw><nsw> U: [0,1073741809) S: [0,1073741809)
43; CHECK-NEXT:  Determining loop execution counts for: @shifty_biggerLshr
44;
45  %tmp1 = lshr i32 %val, 6
46  %tmp2 = shl i32 %tmp1, 4
47  ret i32 %tmp2
48}
49
50define i32 @shifty_biggerLshr_lshrexact(i32 %val) {
51; CHECK-LABEL: 'shifty_biggerLshr_lshrexact'
52; CHECK-NEXT:  Classifying expressions for: @shifty_biggerLshr_lshrexact
53; CHECK-NEXT:    %tmp1 = lshr exact i32 %val, 6
54; CHECK-NEXT:    -->  (%val /u 64) U: [0,67108864) S: [0,67108864)
55; CHECK-NEXT:    %tmp2 = shl i32 %tmp1, 4
56; CHECK-NEXT:    -->  (16 * (%val /u 64))<nuw><nsw> U: [0,1073741809) S: [0,1073741809)
57; CHECK-NEXT:  Determining loop execution counts for: @shifty_biggerLshr_lshrexact
58;
59  %tmp1 = lshr exact i32 %val, 6
60  %tmp2 = shl i32 %tmp1, 4
61  ret i32 %tmp2
62}
63
64define i32 @shifty_biggerShr(i32 %val) {
65; CHECK-LABEL: 'shifty_biggerShr'
66; CHECK-NEXT:  Classifying expressions for: @shifty_biggerShr
67; CHECK-NEXT:    %tmp1 = lshr i32 %val, 4
68; CHECK-NEXT:    -->  (%val /u 16) U: [0,268435456) S: [0,268435456)
69; CHECK-NEXT:    %tmp2 = shl i32 %tmp1, 6
70; CHECK-NEXT:    -->  (64 * (%val /u 16)) U: [0,-63) S: [-2147483648,2147483585)
71; CHECK-NEXT:  Determining loop execution counts for: @shifty_biggerShr
72;
73  %tmp1 = lshr i32 %val, 4
74  %tmp2 = shl i32 %tmp1, 6
75  ret i32 %tmp2
76}
77
78define i32 @shifty_biggerShr_lshrexact(i32 %val) {
79; CHECK-LABEL: 'shifty_biggerShr_lshrexact'
80; CHECK-NEXT:  Classifying expressions for: @shifty_biggerShr_lshrexact
81; CHECK-NEXT:    %tmp1 = lshr exact i32 %val, 4
82; CHECK-NEXT:    -->  (%val /u 16) U: [0,268435456) S: [0,268435456)
83; CHECK-NEXT:    %tmp2 = shl i32 %tmp1, 6
84; CHECK-NEXT:    -->  (64 * (%val /u 16)) U: [0,-63) S: [-2147483648,2147483585)
85; CHECK-NEXT:  Determining loop execution counts for: @shifty_biggerShr_lshrexact
86;
87  %tmp1 = lshr exact i32 %val, 4
88  %tmp2 = shl i32 %tmp1, 6
89  ret i32 %tmp2
90}
91
92; Or, further folded into mask variant.
93
94define i32 @masky_biggerLshr(i32 %val) {
95; CHECK-LABEL: 'masky_biggerLshr'
96; CHECK-NEXT:  Classifying expressions for: @masky_biggerLshr
97; CHECK-NEXT:    %tmp1 = lshr i32 %val, 2
98; CHECK-NEXT:    -->  (%val /u 4) U: [0,1073741824) S: [0,1073741824)
99; CHECK-NEXT:    %tmp2 = and i32 %tmp1, -16
100; CHECK-NEXT:    -->  (16 * (%val /u 64))<nuw><nsw> U: [0,1073741809) S: [0,1073741809)
101; CHECK-NEXT:  Determining loop execution counts for: @masky_biggerLshr
102;
103  %tmp1 = lshr i32 %val, 2
104  %tmp2 = and i32 %tmp1, -16
105  ret i32 %tmp2
106}
107
108define i32 @masky_biggerLshr_lshrexact(i32 %val) {
109; CHECK-LABEL: 'masky_biggerLshr_lshrexact'
110; CHECK-NEXT:  Classifying expressions for: @masky_biggerLshr_lshrexact
111; CHECK-NEXT:    %tmp1 = lshr exact i32 %val, 2
112; CHECK-NEXT:    -->  (%val /u 4) U: [0,1073741824) S: [0,1073741824)
113; CHECK-NEXT:  Determining loop execution counts for: @masky_biggerLshr_lshrexact
114;
115  %tmp1 = lshr exact i32 %val, 2
116  ret i32 %tmp1
117}
118
119define i32 @masky_biggerShr(i32 %val) {
120; CHECK-LABEL: 'masky_biggerShr'
121; CHECK-NEXT:  Classifying expressions for: @masky_biggerShr
122; CHECK-NEXT:    %tmp1 = shl i32 %val, 2
123; CHECK-NEXT:    -->  (4 * %val) U: [0,-3) S: [-2147483648,2147483645)
124; CHECK-NEXT:    %tmp2 = and i32 %tmp1, -64
125; CHECK-NEXT:    -->  (64 * (zext i26 (trunc i32 (%val /u 16) to i26) to i32))<nuw> U: [0,-63) S: [-2147483648,2147483585)
126; CHECK-NEXT:  Determining loop execution counts for: @masky_biggerShr
127;
128  %tmp1 = shl i32 %val, 2
129  %tmp2 = and i32 %tmp1, -64
130  ret i32 %tmp2
131}
132
133define i32 @masky_biggerShr_lshrexact(i32 %val) {
134; CHECK-LABEL: 'masky_biggerShr_lshrexact'
135; CHECK-NEXT:  Classifying expressions for: @masky_biggerShr_lshrexact
136; CHECK-NEXT:    %tmp1 = shl i32 %val, 2
137; CHECK-NEXT:    -->  (4 * %val) U: [0,-3) S: [-2147483648,2147483645)
138; CHECK-NEXT:  Determining loop execution counts for: @masky_biggerShr_lshrexact
139;
140  %tmp1 = shl i32 %val, 2
141  ret i32 %tmp1
142}
143