1; RUN: opt -scalar-evolution-max-arith-depth=0 -scalar-evolution-max-cast-depth=0 -analyze -enable-new-pm=0 -scalar-evolution < %s | FileCheck %s
2; RUN: opt -scalar-evolution-max-arith-depth=0 -scalar-evolution-max-cast-depth=0 -disable-output "-passes=print<scalar-evolution>" < %s 2>&1 | FileCheck %s
3
4; Check that depth set to 0 prevents getAddExpr and getMulExpr from making
5; transformations in SCEV. We expect the result to be very straightforward.
6
7define void @test_add(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e, i32 %f) {
8; CHECK-LABEL: @test_add
9; CHECK:       %s2 = add i32 %s1, %p3
10; CHECK-NEXT:   -->  (%a + %a + %b + %b + %c + %c + %d + %d + %e + %e + %f + %f)
11  %tmp0 = add i32 %a, %b
12  %tmp1 = add i32 %b, %c
13  %tmp2 = add i32 %c, %d
14  %tmp3 = add i32 %d, %e
15  %tmp4 = add i32 %e, %f
16  %tmp5 = add i32 %f, %a
17
18  %p1 = add i32 %tmp0, %tmp3
19  %p2 = add i32 %tmp1, %tmp4
20  %p3 = add i32 %tmp2, %tmp5
21
22  %s1 = add i32 %p1, %p2
23  %s2 = add i32 %s1, %p3
24  ret void
25}
26
27; Constant factors still get folded together.
28define void @test_mul_consts(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e, i32 %f) {
29; CHECK-LABEL: @test_mul_consts
30; CHECK:       %s2 = mul i32 %s1, %p3
31; CHECK-NEXT:  -->  (5040 * %a * %b * %c * %d * %e * %f)
32  %tmp0 = mul i32 %a, 2
33  %tmp1 = mul i32 %b, 3
34  %tmp2 = mul i32 %c, 4
35  %tmp3 = mul i32 %d, 5
36  %tmp4 = mul i32 %e, 6
37  %tmp5 = mul i32 %f, 7
38
39  %p1 = mul i32 %tmp0, %tmp3
40  %p2 = mul i32 %tmp1, %tmp4
41  %p3 = mul i32 %tmp2, %tmp5
42
43  %s1 = mul i32 %p1, %p2
44  %s2 = mul i32 %s1, %p3
45  ret void
46}
47
48; The outer *5 gets distributed because it is at depth=0, but the resulting
49; nested multiply doesn't get flattened, because it is at depth=1.
50define void @test_mul(i32 %a, i32 %b) {
51; CHECK-LABEL: @test_mul
52; CHECK:       %tmp2 = mul i32 %tmp1, 5
53; CHECK-NEXT:  -->  (20 + (5 * (3 * %a)))
54  %tmp0 = mul i32 %a, 3
55  %tmp1 = add i32 %tmp0, 4
56  %tmp2 = mul i32 %tmp1, 5
57  ret void
58}
59
60define void @test_sext(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e, i32 %f) {
61; CHECK-LABEL: @test_sext
62; CHECK:        %se2 = sext i64 %iv2.inc to i128
63; CHECK-NEXT:   -->  {(1 + (sext i64 {(sext i32 (1 + %a) to i64),+,1}<nsw><%loop> to i128))<nsw>,+,1}<nsw><%loop2>
64entry:
65  br label %loop
66
67loop:
68  %iv = phi i32 [ %a, %entry ], [ %iv.inc, %loop ]
69  %iv.inc = add nsw i32 %iv, 1
70  %cond = icmp sle i32 %iv.inc, 50
71  br i1 %cond, label %loop, label %between
72
73between:
74  %se = sext i32 %iv.inc to i64
75  br label %loop2
76
77loop2:
78  %iv2 = phi i64 [ %se, %between ], [ %iv2.inc, %loop2 ]
79  %iv2.inc = add nsw i64 %iv2, 1
80  %cond2 = icmp sle i64 %iv2.inc, 50
81  br i1 %cond2, label %loop2, label %exit
82
83exit:
84  %se2 = sext i64 %iv2.inc to i128
85  ret void
86}
87
88define void @test_zext(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e, i32 %f) {
89; CHECK-LABEL: @test_zext
90; CHECK:          %ze2 = zext i64 %iv2.inc to i128
91; CHECK-NEXT:     -->  {(1 + (zext i64 {7,+,1}<nuw><nsw><%loop> to i128))<nuw><nsw>,+,1}<nuw><%loop2>
92entry:
93  br label %loop
94
95loop:
96  %iv = phi i32 [ 6, %entry ], [ %iv.inc, %loop ]
97  %iv.inc = add nsw i32 %iv, 1
98  %cond = icmp sle i32 %iv.inc, 50
99  br i1 %cond, label %loop, label %between
100
101between:
102  %ze = zext i32 %iv.inc to i64
103  br label %loop2
104
105loop2:
106  %iv2 = phi i64 [ %ze, %between ], [ %iv2.inc, %loop2 ]
107  %iv2.inc = add nuw i64 %iv2, 1
108  %cond2 = icmp sle i64 %iv2.inc, 50
109  br i1 %cond2, label %loop2, label %exit
110
111exit:
112  %ze2 = zext i64 %iv2.inc to i128
113  ret void
114}
115
116define void @test_trunc(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e, i32 %f) {
117; CHECK-LABEL: @test_trunc
118; CHECK:          %trunc2 = trunc i64 %iv2.inc to i32
119; CHECK-NEXT:     -->  {(trunc i64 (1 + {7,+,1}<%loop>) to i32),+,1}<%loop2>
120entry:
121  br label %loop
122
123loop:
124  %iv = phi i128 [ 6, %entry ], [ %iv.inc, %loop ]
125  %iv.inc = add nsw i128 %iv, 1
126  %cond = icmp sle i128 %iv.inc, 50
127  br i1 %cond, label %loop, label %between
128
129between:
130  %trunc = trunc i128 %iv.inc to i64
131  br label %loop2
132
133loop2:
134  %iv2 = phi i64 [ %trunc, %between ], [ %iv2.inc, %loop2 ]
135  %iv2.inc = add nuw i64 %iv2, 1
136  %cond2 = icmp sle i64 %iv2.inc, 50
137  br i1 %cond2, label %loop2, label %exit
138
139exit:
140  %trunc2 = trunc i64 %iv2.inc to i32
141  ret void
142}
143
144; Check that all constant SCEVs are folded regardless depth limit.
145define void @test_mul_const(i32 %a) {
146; CHECK-LABEL:  @test_mul_const
147; CHECK:          %test3 = mul i32 %test2, 3
148; CHECK-NEXT:     -->  (9 + (3 * (3 * %a)))
149; CHECK:          %test4 = mul i32 3, 3
150; CHECK-NEXT:     -->  9 U: [9,10) S: [9,10)
151  %test = mul i32 3, %a
152  %test2 = add i32 3, %test
153  %test3 = mul i32 %test2, 3
154  %test4 = mul i32 3, 3
155  ret void
156}
157