1 ; RUN: opt < %s -passes=indvars -S | FileCheck %s
3 ; These tests ensure that we can compute the trip count of various forms of
4 ; loops. If the trip count of the loop is computable, then we will know what
5 ; the exit value of the loop will be for some value, allowing us to substitute
6 ; it directly into users outside of the loop, making the loop dead.
8 ; CHECK-LABEL: @linear_setne(
11 define i32 @linear_setne() {
15 loop: ; preds = %loop, %entry
16 %i = phi i32 [ 0, %entry ], [ %i.next, %loop ] ; <i32> [#uses=3]
17 %i.next = add i32 %i, 1 ; <i32> [#uses=1]
18 %c = icmp ne i32 %i, 100 ; <i1> [#uses=1]
19 br i1 %c, label %loop, label %loopexit
21 loopexit: ; preds = %loop
25 ; CHECK-LABEL: @linear_setne_2(
28 define i32 @linear_setne_2() {
32 loop: ; preds = %loop, %entry
33 %i = phi i32 [ 0, %entry ], [ %i.next, %loop ] ; <i32> [#uses=3]
34 %i.next = add i32 %i, 2 ; <i32> [#uses=1]
35 %c = icmp ne i32 %i, 100 ; <i1> [#uses=1]
36 br i1 %c, label %loop, label %loopexit
38 loopexit: ; preds = %loop
42 ; CHECK-LABEL: @linear_setne_overflow(
45 define i32 @linear_setne_overflow() {
49 loop: ; preds = %loop, %entry
50 %i = phi i32 [ 1024, %entry ], [ %i.next, %loop ] ; <i32> [#uses=3]
51 %i.next = add i32 %i, 1024 ; <i32> [#uses=1]
52 %c = icmp ne i32 %i, 0 ; <i1> [#uses=1]
53 br i1 %c, label %loop, label %loopexit
55 loopexit: ; preds = %loop
59 ; CHECK-LABEL: @linear_setlt(
62 define i32 @linear_setlt() {
66 loop: ; preds = %loop, %entry
67 %i = phi i32 [ 0, %entry ], [ %i.next, %loop ] ; <i32> [#uses=3]
68 %i.next = add i32 %i, 1 ; <i32> [#uses=1]
69 %c = icmp slt i32 %i, 100 ; <i1> [#uses=1]
70 br i1 %c, label %loop, label %loopexit
72 loopexit: ; preds = %loop
76 ; CHECK-LABEL: @quadratic_setlt(
79 define i32 @quadratic_setlt() {
83 loop: ; preds = %loop, %entry
84 %i = phi i32 [ 7, %entry ], [ %i.next, %loop ] ; <i32> [#uses=4]
85 %i.next = add i32 %i, 3 ; <i32> [#uses=1]
86 %i2 = mul i32 %i, %i ; <i32> [#uses=1]
87 %c = icmp slt i32 %i2, 1000 ; <i1> [#uses=1]
88 br i1 %c, label %loop, label %loopexit
90 loopexit: ; preds = %loop
94 ; CHECK-LABEL: @chained(
97 define i32 @chained() {
101 loop: ; preds = %loop, %entry
102 %i = phi i32 [ 0, %entry ], [ %i.next, %loop ] ; <i32> [#uses=3]
103 %i.next = add i32 %i, 1 ; <i32> [#uses=1]
104 %c = icmp ne i32 %i, 100 ; <i1> [#uses=1]
105 br i1 %c, label %loop, label %loopexit
107 loopexit: ; preds = %loop
110 loop2: ; preds = %loop2, %loopexit
111 %j = phi i32 [ %i, %loopexit ], [ %j.next, %loop2 ] ; <i32> [#uses=3]
112 %j.next = add i32 %j, 1 ; <i32> [#uses=1]
113 %c2 = icmp ne i32 %j, 200 ; <i1> [#uses=1]
114 br i1 %c2, label %loop2, label %loopexit2
116 loopexit2: ; preds = %loop2
120 ; CHECK-LABEL: @chained4(
123 define i32 @chained4() {
127 loop: ; preds = %loop, %entry
128 %i = phi i32 [ 0, %entry ], [ %i.next, %loop ] ; <i32> [#uses=3]
129 %i.next = add i32 %i, 1 ; <i32> [#uses=1]
130 %c = icmp ne i32 %i.next, 100 ; <i1> [#uses=1]
131 br i1 %c, label %loop, label %loopexit
133 loopexit: ; preds = %loop
136 loop2: ; preds = %loop2, %loopexit
137 %j = phi i32 [ %i.next, %loopexit ], [ %j.next, %loop2 ] ; <i32> [#uses=3]
138 %j.next = add i32 %j, 1 ; <i32> [#uses=1]
139 %c2 = icmp ne i32 %j.next, 200 ; <i1> [#uses=1]
140 br i1 %c2, label %loop2, label %loopexit2
142 loopexit2: ; preds = %loop
145 loop8: ; preds = %loop2, %loopexit
146 %k = phi i32 [ %j.next, %loopexit2 ], [ %k.next, %loop8 ] ; <i32> [#uses=3]
147 %k.next = add i32 %k, 1 ; <i32> [#uses=1]
148 %c8 = icmp ne i32 %k.next, 300 ; <i1> [#uses=1]
149 br i1 %c8, label %loop8, label %loopexit8
151 loopexit8: ; preds = %loop2
154 loop9: ; preds = %loop2, %loopexit
155 %l = phi i32 [ %k.next, %loopexit8 ], [ %l.next, %loop9 ] ; <i32> [#uses=3]
156 %l.next = add i32 %l, 1 ; <i32> [#uses=1]
157 %c9 = icmp ne i32 %l.next, 400 ; <i1> [#uses=1]
158 br i1 %c9, label %loop9, label %loopexit9
160 loopexit9: ; preds = %loop2
164 ; PR18449. Check that the early exit is reduced to never taken.
166 ; CHECK-LABEL: @twoexit
172 define void @twoexit() {
173 "function top level":
176 loop: ; preds = %body, %"function top level"
177 %0 = phi i64 [ 0, %"function top level" ], [ %2, %body ]
178 %1 = icmp ugt i64 %0, 2
179 br i1 %1, label %fail, label %body
181 fail: ; preds = %loop
182 tail call void @bounds_fail()
185 body: ; preds = %loop
187 %3 = icmp slt i64 %2, 3
188 br i1 %3, label %loop, label %out
193 declare void @bounds_fail()