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
4 ; This checks if the min and max expressions are properly recognized by
5 ; ScalarEvolution even though they the ICmpInst and SelectInst have different
8 ; #define max(a, b) (a > b ? a : b)
9 ; #define min(a, b) (a < b ? a : b)
11 ; void f(int *A, int N) {
12 ; for (int i = 0; i < N; i++) {
13 ; A[max(0, i - 3)] = Aptr 2;
17 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
19 define void @f(ptr %A, i32 %N) {
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 2147483647
51 ; CHECK-NEXT: Loop %bb1: symbolic max backedge-taken count is (0 smax %N)
52 ; CHECK-NEXT: Loop %bb1: Predicated backedge-taken count is (0 smax %N)
53 ; CHECK-NEXT: Predicates:
54 ; CHECK: Loop %bb1: Trip multiple is 1
59 bb1: ; preds = %bb2, %bb
60 %i.0 = phi i32 [ 0, %bb ], [ %tmp23, %bb2 ]
61 %i.0.1 = sext i32 %i.0 to i64
62 %tmp = icmp slt i32 %i.0, %N
63 br i1 %tmp, label %bb2, label %bb24
66 %tmp3 = add nuw nsw i32 %i.0, 3
67 %tmp4 = icmp slt i32 %tmp3, %N
68 %tmp5 = sext i32 %tmp3 to i64
69 %tmp6 = sext i32 %N to i64
70 %tmp9 = select i1 %tmp4, i64 %tmp5, i64 %tmp6
72 %tmp11 = getelementptr inbounds i32, ptr %A, i64 %tmp9
73 %tmp12 = load i32, ptr %tmp11, align 4
74 %tmp13 = shl nsw i32 %tmp12, 1
75 %tmp14 = icmp sge i32 3, %i.0
76 %tmp17 = add nsw i64 %i.0.1, -3
77 %tmp19 = select i1 %tmp14, i64 0, i64 %tmp17
79 %tmp21 = getelementptr inbounds i32, ptr %A, i64 %tmp19
80 store i32 %tmp13, ptr %tmp21, align 4
81 %tmp23 = add nuw nsw i32 %i.0, 1
88 define i8 @umax_basic_eq_off1(i8 %x, i8 %y) {
89 ; CHECK-LABEL: 'umax_basic_eq_off1'
90 ; CHECK-NEXT: Classifying expressions for: @umax_basic_eq_off1
91 ; CHECK-NEXT: %lhs = add i8 %y, 1
92 ; CHECK-NEXT: --> (1 + %y) U: full-set S: full-set
93 ; CHECK-NEXT: %rhs = add i8 %x, %y
94 ; CHECK-NEXT: --> (%x + %y) U: full-set S: full-set
95 ; CHECK-NEXT: %r = select i1 %x.is.zero, i8 %lhs, i8 %rhs
96 ; CHECK-NEXT: --> ((1 umax %x) + %y) U: full-set S: full-set
97 ; CHECK-NEXT: Determining loop execution counts for: @umax_basic_eq_off1
99 %x.is.zero = icmp eq i8 %x, 0
102 %r = select i1 %x.is.zero, i8 %lhs, i8 %rhs
105 define i8 @umax_basic_ne_off1(i8 %x, i8 %y) {
106 ; CHECK-LABEL: 'umax_basic_ne_off1'
107 ; CHECK-NEXT: Classifying expressions for: @umax_basic_ne_off1
108 ; CHECK-NEXT: %lhs = add i8 %y, 1
109 ; CHECK-NEXT: --> (1 + %y) U: full-set S: full-set
110 ; CHECK-NEXT: %rhs = add i8 %x, %y
111 ; CHECK-NEXT: --> (%x + %y) U: full-set S: full-set
112 ; CHECK-NEXT: %r = select i1 %x.is.zero, i8 %rhs, i8 %lhs
113 ; CHECK-NEXT: --> ((1 umax %x) + %y) U: full-set S: full-set
114 ; CHECK-NEXT: Determining loop execution counts for: @umax_basic_ne_off1
116 %x.is.zero = icmp ne i8 %x, 0
119 %r = select i1 %x.is.zero, i8 %rhs, i8 %lhs
123 define i8 @umax_basic_eq_off0(i8 %x, i8 %y) {
124 ; CHECK-LABEL: 'umax_basic_eq_off0'
125 ; CHECK-NEXT: Classifying expressions for: @umax_basic_eq_off0
126 ; CHECK-NEXT: %lhs = add i8 %y, 0
127 ; CHECK-NEXT: --> %y U: full-set S: full-set
128 ; CHECK-NEXT: %rhs = add i8 %x, %y
129 ; CHECK-NEXT: --> (%x + %y) U: full-set S: full-set
130 ; CHECK-NEXT: %r = select i1 %x.is.zero, i8 %lhs, i8 %rhs
131 ; CHECK-NEXT: --> (%x + %y) U: full-set S: full-set
132 ; CHECK-NEXT: Determining loop execution counts for: @umax_basic_eq_off0
134 %x.is.zero = icmp eq i8 %x, 0
137 %r = select i1 %x.is.zero, i8 %lhs, i8 %rhs
141 define i8 @umax_basic_eq_off2(i8 %x, i8 %y) {
142 ; CHECK-LABEL: 'umax_basic_eq_off2'
143 ; CHECK-NEXT: Classifying expressions for: @umax_basic_eq_off2
144 ; CHECK-NEXT: %lhs = add i8 %y, 2
145 ; CHECK-NEXT: --> (2 + %y) U: full-set S: full-set
146 ; CHECK-NEXT: %rhs = add i8 %x, %y
147 ; CHECK-NEXT: --> (%x + %y) U: full-set S: full-set
148 ; CHECK-NEXT: %r = select i1 %x.is.zero, i8 %lhs, i8 %rhs
149 ; CHECK-NEXT: --> %r U: full-set S: full-set
150 ; CHECK-NEXT: Determining loop execution counts for: @umax_basic_eq_off2
152 %x.is.zero = icmp eq i8 %x, 0
155 %r = select i1 %x.is.zero, i8 %lhs, i8 %rhs
159 define i8 @umax_basic_eq_var_off(i8 %x, i8 %y, i8 %c) {
160 ; CHECK-LABEL: 'umax_basic_eq_var_off'
161 ; CHECK-NEXT: Classifying expressions for: @umax_basic_eq_var_off
162 ; CHECK-NEXT: %lhs = add i8 %y, %c
163 ; CHECK-NEXT: --> (%y + %c) U: full-set S: full-set
164 ; CHECK-NEXT: %rhs = add i8 %x, %y
165 ; CHECK-NEXT: --> (%x + %y) U: full-set S: full-set
166 ; CHECK-NEXT: %r = select i1 %x.is.zero, i8 %lhs, i8 %rhs
167 ; CHECK-NEXT: --> %r U: full-set S: full-set
168 ; CHECK-NEXT: Determining loop execution counts for: @umax_basic_eq_var_off
170 %x.is.zero = icmp eq i8 %x, 0
173 %r = select i1 %x.is.zero, i8 %lhs, i8 %rhs
177 define i8 @umax_basic_eq_narrow(i4 %x.narrow, i8 %y) {
178 ; CHECK-LABEL: 'umax_basic_eq_narrow'
179 ; CHECK-NEXT: Classifying expressions for: @umax_basic_eq_narrow
180 ; CHECK-NEXT: %x = zext i4 %x.narrow to i8
181 ; CHECK-NEXT: --> (zext i4 %x.narrow to i8) U: [0,16) S: [0,16)
182 ; CHECK-NEXT: %lhs = add i8 %y, 1
183 ; CHECK-NEXT: --> (1 + %y) U: full-set S: full-set
184 ; CHECK-NEXT: %rhs = add i8 %x, %y
185 ; CHECK-NEXT: --> ((zext i4 %x.narrow to i8) + %y) U: full-set S: full-set
186 ; CHECK-NEXT: %r = select i1 %x.is.zero, i8 %lhs, i8 %rhs
187 ; CHECK-NEXT: --> ((1 umax (zext i4 %x.narrow to i8)) + %y) U: full-set S: full-set
188 ; CHECK-NEXT: Determining loop execution counts for: @umax_basic_eq_narrow
190 %x = zext i4 %x.narrow to i8
191 %x.is.zero = icmp eq i4 %x.narrow, 0
194 %r = select i1 %x.is.zero, i8 %lhs, i8 %rhs
197 define i8 @umax_basic_ne_narrow(i4 %x.narrow, i8 %y) {
198 ; CHECK-LABEL: 'umax_basic_ne_narrow'
199 ; CHECK-NEXT: Classifying expressions for: @umax_basic_ne_narrow
200 ; CHECK-NEXT: %x = zext i4 %x.narrow to i8
201 ; CHECK-NEXT: --> (zext i4 %x.narrow to i8) U: [0,16) S: [0,16)
202 ; CHECK-NEXT: %lhs = add i8 %y, 1
203 ; CHECK-NEXT: --> (1 + %y) U: full-set S: full-set
204 ; CHECK-NEXT: %rhs = add i8 %x, %y
205 ; CHECK-NEXT: --> ((zext i4 %x.narrow to i8) + %y) U: full-set S: full-set
206 ; CHECK-NEXT: %r = select i1 %x.is.zero, i8 %rhs, i8 %lhs
207 ; CHECK-NEXT: --> ((1 umax (zext i4 %x.narrow to i8)) + %y) U: full-set S: full-set
208 ; CHECK-NEXT: Determining loop execution counts for: @umax_basic_ne_narrow
210 %x = zext i4 %x.narrow to i8
211 %x.is.zero = icmp ne i4 %x.narrow, 0
214 %r = select i1 %x.is.zero, i8 %rhs, i8 %lhs