xref: /llvm-project/llvm/test/Analysis/ScalarEvolution/min-max-exprs.ll (revision 8b5b294ec2cf876bc5eb5bd5fcb56ef487e36d60)
1; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py
2; RUN: opt "-passes=print<scalar-evolution>" -disable-output < %s 2>&1 | FileCheck %s
3;
4; This checks if the min and max expressions are properly recognized by
5; ScalarEvolution even though they the ICmpInst and SelectInst have different
6; types.
7;
8;    #define max(a, b) (a > b ? a : b)
9;    #define min(a, b) (a < b ? a : b)
10;
11;    void f(int *A, int N) {
12;      for (int i = 0; i < N; i++) {
13;        A[max(0, i - 3)] = Aptr 2;
14;      }
15;    }
16;
17target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
18
19define void @f(ptr %A, i32 %N) {
20; CHECK-LABEL: 'f'
21; CHECK-NEXT:  Classifying expressions for: @f
22; CHECK-NEXT:    %i.0 = phi i32 [ 0, %bb ], [ %tmp23, %bb2 ]
23; CHECK-NEXT:    --> {0,+,1}<nuw><nsw><%bb1> U: [0,-2147483648) S: [0,-2147483648) Exits: (0 smax %N) LoopDispositions: { %bb1: Computable }
24; CHECK-NEXT:    %i.0.1 = sext i32 %i.0 to i64
25; CHECK-NEXT:    --> {0,+,1}<nuw><nsw><%bb1> U: [0,2147483648) S: [0,2147483648) Exits: (zext i32 (0 smax %N) to i64) LoopDispositions: { %bb1: Computable }
26; CHECK-NEXT:    %tmp3 = add nuw nsw i32 %i.0, 3
27; CHECK-NEXT:    --> {3,+,1}<nuw><%bb1> U: [3,-2147483645) S: [3,-2147483645) Exits: (3 + (0 smax %N))<nuw> LoopDispositions: { %bb1: Computable }
28; CHECK-NEXT:    %tmp5 = sext i32 %tmp3 to i64
29; CHECK-NEXT:    --> (sext i32 {3,+,1}<nuw><%bb1> to i64) U: [-2147483648,2147483648) S: [-2147483648,2147483648) Exits: (sext i32 (3 + (0 smax %N))<nuw> to i64) LoopDispositions: { %bb1: Computable }
30; CHECK-NEXT:    %tmp6 = sext i32 %N to i64
31; CHECK-NEXT:    --> (sext i32 %N to i64) U: [-2147483648,2147483648) S: [-2147483648,2147483648) Exits: (sext i32 %N to i64) LoopDispositions: { %bb1: Invariant }
32; CHECK-NEXT:    %tmp9 = select i1 %tmp4, i64 %tmp5, i64 %tmp6
33; CHECK-NEXT:    --> ((sext i32 {3,+,1}<nuw><%bb1> to i64) smin (sext i32 %N to i64)) U: [-2147483648,2147483648) S: [-2147483648,2147483648) Exits: ((sext i32 (3 + (0 smax %N))<nuw> to i64) smin (sext i32 %N to i64)) LoopDispositions: { %bb1: Computable }
34; CHECK-NEXT:    %tmp11 = getelementptr inbounds i32, ptr %A, i64 %tmp9
35; CHECK-NEXT:    --> ((4 * ((sext i32 {3,+,1}<nuw><%bb1> to i64) smin (sext i32 %N to i64)))<nsw> + %A) U: full-set S: full-set Exits: ((4 * ((sext i32 (3 + (0 smax %N))<nuw> to i64) smin (sext i32 %N to i64)))<nsw> + %A) LoopDispositions: { %bb1: Computable }
36; CHECK-NEXT:    %tmp12 = load i32, ptr %tmp11, align 4
37; CHECK-NEXT:    --> %tmp12 U: full-set S: full-set Exits: <<Unknown>> LoopDispositions: { %bb1: Variant }
38; CHECK-NEXT:    %tmp13 = shl nsw i32 %tmp12, 1
39; CHECK-NEXT:    --> (2 * %tmp12) U: [0,-1) S: [-2147483648,2147483647) Exits: <<Unknown>> LoopDispositions: { %bb1: Variant }
40; CHECK-NEXT:    %tmp17 = add nsw i64 %i.0.1, -3
41; CHECK-NEXT:    --> {-3,+,1}<nsw><%bb1> U: [-3,2147483645) S: [-3,2147483645) Exits: (-3 + (zext i32 (0 smax %N) to i64))<nsw> LoopDispositions: { %bb1: Computable }
42; CHECK-NEXT:    %tmp19 = select i1 %tmp14, i64 0, i64 %tmp17
43; CHECK-NEXT:    --> (-3 + (3 smax {0,+,1}<nuw><nsw><%bb1>))<nsw> U: [0,2147483645) S: [0,2147483645) Exits: (-3 + (3 smax (zext i32 (0 smax %N) to i64)))<nsw> LoopDispositions: { %bb1: Computable }
44; CHECK-NEXT:    %tmp21 = getelementptr inbounds i32, ptr %A, i64 %tmp19
45; CHECK-NEXT:    --> (-12 + (4 * (3 smax {0,+,1}<nuw><nsw><%bb1>))<nuw><nsw> + %A) U: full-set S: full-set Exits: (-12 + (4 * (3 smax (zext i32 (0 smax %N) to i64)))<nuw><nsw> + %A) LoopDispositions: { %bb1: Computable }
46; CHECK-NEXT:    %tmp23 = add nuw nsw i32 %i.0, 1
47; CHECK-NEXT:    --> {1,+,1}<nuw><%bb1> U: [1,-2147483647) S: [1,-2147483647) Exits: (1 + (0 smax %N))<nuw> LoopDispositions: { %bb1: Computable }
48; CHECK-NEXT:  Determining loop execution counts for: @f
49; CHECK-NEXT:  Loop %bb1: backedge-taken count is (0 smax %N)
50; CHECK-NEXT:  Loop %bb1: constant max backedge-taken count is i32 2147483647
51; CHECK-NEXT:  Loop %bb1: symbolic max backedge-taken count is (0 smax %N)
52; CHECK-NEXT:  Loop %bb1: Trip multiple is 1
53;
54bb:
55  br label %bb1
56
57bb1:                                              ; preds = %bb2, %bb
58  %i.0 = phi i32 [ 0, %bb ], [ %tmp23, %bb2 ]
59  %i.0.1 = sext i32 %i.0 to i64
60  %tmp = icmp slt i32 %i.0, %N
61  br i1 %tmp, label %bb2, label %bb24
62
63bb2:                                              ; preds = %bb1
64  %tmp3 = add nuw nsw i32 %i.0, 3
65  %tmp4 = icmp slt i32 %tmp3, %N
66  %tmp5 = sext i32 %tmp3 to i64
67  %tmp6 = sext i32 %N to i64
68  %tmp9 = select i1 %tmp4, i64 %tmp5, i64 %tmp6
69;                  min(N, i+3)
70  %tmp11 = getelementptr inbounds i32, ptr %A, i64 %tmp9
71  %tmp12 = load i32, ptr %tmp11, align 4
72  %tmp13 = shl nsw i32 %tmp12, 1
73  %tmp14 = icmp sge i32 3, %i.0
74  %tmp17 = add nsw i64 %i.0.1, -3
75  %tmp19 = select i1 %tmp14, i64 0, i64 %tmp17
76;                  max(0, i - 3)
77  %tmp21 = getelementptr inbounds i32, ptr %A, i64 %tmp19
78  store i32 %tmp13, ptr %tmp21, align 4
79  %tmp23 = add nuw nsw i32 %i.0, 1
80  br label %bb1
81
82bb24:                                             ; preds = %bb1
83  ret void
84}
85
86define i8 @umax_basic_eq_off1(i8 %x, i8 %y) {
87; CHECK-LABEL: 'umax_basic_eq_off1'
88; CHECK-NEXT:  Classifying expressions for: @umax_basic_eq_off1
89; CHECK-NEXT:    %lhs = add i8 %y, 1
90; CHECK-NEXT:    --> (1 + %y) U: full-set S: full-set
91; CHECK-NEXT:    %rhs = add i8 %x, %y
92; CHECK-NEXT:    --> (%x + %y) U: full-set S: full-set
93; CHECK-NEXT:    %r = select i1 %x.is.zero, i8 %lhs, i8 %rhs
94; CHECK-NEXT:    --> ((1 umax %x) + %y) U: full-set S: full-set
95; CHECK-NEXT:  Determining loop execution counts for: @umax_basic_eq_off1
96;
97  %x.is.zero = icmp eq i8 %x, 0
98  %lhs = add i8 %y, 1
99  %rhs = add i8 %x, %y
100  %r = select i1 %x.is.zero, i8 %lhs, i8 %rhs
101  ret i8 %r
102}
103define i8 @umax_basic_ne_off1(i8 %x, i8 %y) {
104; CHECK-LABEL: 'umax_basic_ne_off1'
105; CHECK-NEXT:  Classifying expressions for: @umax_basic_ne_off1
106; CHECK-NEXT:    %lhs = add i8 %y, 1
107; CHECK-NEXT:    --> (1 + %y) U: full-set S: full-set
108; CHECK-NEXT:    %rhs = add i8 %x, %y
109; CHECK-NEXT:    --> (%x + %y) U: full-set S: full-set
110; CHECK-NEXT:    %r = select i1 %x.is.zero, i8 %rhs, i8 %lhs
111; CHECK-NEXT:    --> ((1 umax %x) + %y) U: full-set S: full-set
112; CHECK-NEXT:  Determining loop execution counts for: @umax_basic_ne_off1
113;
114  %x.is.zero = icmp ne i8 %x, 0
115  %lhs = add i8 %y, 1
116  %rhs = add i8 %x, %y
117  %r = select i1 %x.is.zero, i8 %rhs, i8 %lhs
118  ret i8 %r
119}
120
121define i8 @umax_basic_eq_off0(i8 %x, i8 %y) {
122; CHECK-LABEL: 'umax_basic_eq_off0'
123; CHECK-NEXT:  Classifying expressions for: @umax_basic_eq_off0
124; CHECK-NEXT:    %lhs = add i8 %y, 0
125; CHECK-NEXT:    --> %y U: full-set S: full-set
126; CHECK-NEXT:    %rhs = add i8 %x, %y
127; CHECK-NEXT:    --> (%x + %y) U: full-set S: full-set
128; CHECK-NEXT:    %r = select i1 %x.is.zero, i8 %lhs, i8 %rhs
129; CHECK-NEXT:    --> (%x + %y) U: full-set S: full-set
130; CHECK-NEXT:  Determining loop execution counts for: @umax_basic_eq_off0
131;
132  %x.is.zero = icmp eq i8 %x, 0
133  %lhs = add i8 %y, 0
134  %rhs = add i8 %x, %y
135  %r = select i1 %x.is.zero, i8 %lhs, i8 %rhs
136  ret i8 %r
137}
138
139define i8 @umax_basic_eq_off2(i8 %x, i8 %y) {
140; CHECK-LABEL: 'umax_basic_eq_off2'
141; CHECK-NEXT:  Classifying expressions for: @umax_basic_eq_off2
142; CHECK-NEXT:    %lhs = add i8 %y, 2
143; CHECK-NEXT:    --> (2 + %y) U: full-set S: full-set
144; CHECK-NEXT:    %rhs = add i8 %x, %y
145; CHECK-NEXT:    --> (%x + %y) U: full-set S: full-set
146; CHECK-NEXT:    %r = select i1 %x.is.zero, i8 %lhs, i8 %rhs
147; CHECK-NEXT:    --> %r U: full-set S: full-set
148; CHECK-NEXT:  Determining loop execution counts for: @umax_basic_eq_off2
149;
150  %x.is.zero = icmp eq i8 %x, 0
151  %lhs = add i8 %y, 2
152  %rhs = add i8 %x, %y
153  %r = select i1 %x.is.zero, i8 %lhs, i8 %rhs
154  ret i8 %r
155}
156
157define i8 @umax_basic_eq_var_off(i8 %x, i8 %y, i8 %c) {
158; CHECK-LABEL: 'umax_basic_eq_var_off'
159; CHECK-NEXT:  Classifying expressions for: @umax_basic_eq_var_off
160; CHECK-NEXT:    %lhs = add i8 %y, %c
161; CHECK-NEXT:    --> (%y + %c) U: full-set S: full-set
162; CHECK-NEXT:    %rhs = add i8 %x, %y
163; CHECK-NEXT:    --> (%x + %y) U: full-set S: full-set
164; CHECK-NEXT:    %r = select i1 %x.is.zero, i8 %lhs, i8 %rhs
165; CHECK-NEXT:    --> %r U: full-set S: full-set
166; CHECK-NEXT:  Determining loop execution counts for: @umax_basic_eq_var_off
167;
168  %x.is.zero = icmp eq i8 %x, 0
169  %lhs = add i8 %y, %c
170  %rhs = add i8 %x, %y
171  %r = select i1 %x.is.zero, i8 %lhs, i8 %rhs
172  ret i8 %r
173}
174
175define i8 @umax_basic_eq_narrow(i4 %x.narrow, i8 %y) {
176; CHECK-LABEL: 'umax_basic_eq_narrow'
177; CHECK-NEXT:  Classifying expressions for: @umax_basic_eq_narrow
178; CHECK-NEXT:    %x = zext i4 %x.narrow to i8
179; CHECK-NEXT:    --> (zext i4 %x.narrow to i8) U: [0,16) S: [0,16)
180; CHECK-NEXT:    %lhs = add i8 %y, 1
181; CHECK-NEXT:    --> (1 + %y) U: full-set S: full-set
182; CHECK-NEXT:    %rhs = add i8 %x, %y
183; CHECK-NEXT:    --> ((zext i4 %x.narrow to i8) + %y) U: full-set S: full-set
184; CHECK-NEXT:    %r = select i1 %x.is.zero, i8 %lhs, i8 %rhs
185; CHECK-NEXT:    --> ((1 umax (zext i4 %x.narrow to i8)) + %y) U: full-set S: full-set
186; CHECK-NEXT:  Determining loop execution counts for: @umax_basic_eq_narrow
187;
188  %x = zext i4 %x.narrow to i8
189  %x.is.zero = icmp eq i4 %x.narrow, 0
190  %lhs = add i8 %y, 1
191  %rhs = add i8 %x, %y
192  %r = select i1 %x.is.zero, i8 %lhs, i8 %rhs
193  ret i8 %r
194}
195define i8 @umax_basic_ne_narrow(i4 %x.narrow, i8 %y) {
196; CHECK-LABEL: 'umax_basic_ne_narrow'
197; CHECK-NEXT:  Classifying expressions for: @umax_basic_ne_narrow
198; CHECK-NEXT:    %x = zext i4 %x.narrow to i8
199; CHECK-NEXT:    --> (zext i4 %x.narrow to i8) U: [0,16) S: [0,16)
200; CHECK-NEXT:    %lhs = add i8 %y, 1
201; CHECK-NEXT:    --> (1 + %y) U: full-set S: full-set
202; CHECK-NEXT:    %rhs = add i8 %x, %y
203; CHECK-NEXT:    --> ((zext i4 %x.narrow to i8) + %y) U: full-set S: full-set
204; CHECK-NEXT:    %r = select i1 %x.is.zero, i8 %rhs, i8 %lhs
205; CHECK-NEXT:    --> ((1 umax (zext i4 %x.narrow to i8)) + %y) U: full-set S: full-set
206; CHECK-NEXT:  Determining loop execution counts for: @umax_basic_ne_narrow
207;
208  %x = zext i4 %x.narrow to i8
209  %x.is.zero = icmp ne i4 %x.narrow, 0
210  %lhs = add i8 %y, 1
211  %rhs = add i8 %x, %y
212  %r = select i1 %x.is.zero, i8 %rhs, i8 %lhs
213  ret i8 %r
214}
215