1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -loop-reduce -S | FileCheck %s
3 ; REQUIRES: x86-registered-target
5 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128-ni:1-p2:32:8:8:32-ni:2"
6 target triple = "x86_64-unknown-linux-gnu"
8 ; FIXME: iv.next is supposed to be inserted in the backedge.
9 define i32 @test_01(i32* %p, i64 %len, i32 %x) {
10 ; CHECK-LABEL: @test_01(
12 ; CHECK-NEXT: [[SCEVGEP:%.*]] = getelementptr i32, i32* [[P:%.*]], i64 -1
13 ; CHECK-NEXT: br label [[LOOP:%.*]]
15 ; CHECK-NEXT: [[IV:%.*]] = phi i64 [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ], [ [[LEN:%.*]], [[ENTRY:%.*]] ]
16 ; CHECK-NEXT: [[COND_1:%.*]] = icmp eq i64 [[IV]], 0
17 ; CHECK-NEXT: br i1 [[COND_1]], label [[EXIT:%.*]], label [[BACKEDGE]]
19 ; CHECK-NEXT: [[SCEVGEP1:%.*]] = getelementptr i32, i32* [[SCEVGEP]], i64 [[IV]]
20 ; CHECK-NEXT: [[LOADED:%.*]] = load atomic i32, i32* [[SCEVGEP1]] unordered, align 4
21 ; CHECK-NEXT: [[COND_2:%.*]] = icmp eq i32 [[LOADED]], [[X:%.*]]
22 ; CHECK-NEXT: [[IV_NEXT]] = add nsw i64 [[IV]], -1
23 ; CHECK-NEXT: br i1 [[COND_2]], label [[FAILURE:%.*]], label [[LOOP]]
25 ; CHECK-NEXT: ret i32 -1
27 ; CHECK-NEXT: unreachable
32 loop: ; preds = %backedge, %entry
33 %iv = phi i64 [ %iv.next, %backedge ], [ %len, %entry ]
34 %iv.next = add nsw i64 %iv, -1
35 %cond_1 = icmp eq i64 %iv, 0
36 br i1 %cond_1, label %exit, label %backedge
38 backedge: ; preds = %loop
39 %addr = getelementptr inbounds i32, i32* %p, i64 %iv.next
40 %loaded = load atomic i32, i32* %addr unordered, align 4
41 %cond_2 = icmp eq i32 %loaded, %x
42 br i1 %cond_2, label %failure, label %loop
47 failure: ; preds = %backedge
51 define i32 @test_02(i32* %p, i64 %len, i32 %x) {
52 ; CHECK-LABEL: @test_02(
54 ; CHECK-NEXT: [[SCEVGEP:%.*]] = getelementptr i32, i32* [[P:%.*]], i64 -1
55 ; CHECK-NEXT: br label [[LOOP:%.*]]
57 ; CHECK-NEXT: [[LSR_IV:%.*]] = phi i64 [ [[LSR_IV_NEXT:%.*]], [[BACKEDGE:%.*]] ], [ [[LEN:%.*]], [[ENTRY:%.*]] ]
58 ; CHECK-NEXT: [[COND_1:%.*]] = icmp eq i64 [[LSR_IV]], 0
59 ; CHECK-NEXT: br i1 [[COND_1]], label [[EXIT:%.*]], label [[BACKEDGE]]
61 ; CHECK-NEXT: [[SCEVGEP1:%.*]] = getelementptr i32, i32* [[SCEVGEP]], i64 [[LSR_IV]]
62 ; CHECK-NEXT: [[LOADED:%.*]] = load atomic i32, i32* [[SCEVGEP1]] unordered, align 4
63 ; CHECK-NEXT: [[COND_2:%.*]] = icmp eq i32 [[LOADED]], [[X:%.*]]
64 ; CHECK-NEXT: [[LSR_IV_NEXT]] = add i64 [[LSR_IV]], -1
65 ; CHECK-NEXT: br i1 [[COND_2]], label [[FAILURE:%.*]], label [[LOOP]]
67 ; CHECK-NEXT: ret i32 -1
69 ; CHECK-NEXT: unreachable
72 %start = add i64 %len, -1
75 loop: ; preds = %backedge, %entry
76 %iv = phi i64 [ %iv.next, %backedge ], [ %start, %entry ]
77 %iv.next = add nsw i64 %iv, -1
78 %iv.offset = add i64 %iv, 1
79 %iv.next.offset = add i64 %iv.next, 1
80 %cond_1 = icmp eq i64 %iv.offset, 0
81 br i1 %cond_1, label %exit, label %backedge
83 backedge: ; preds = %loop
84 %addr = getelementptr inbounds i32, i32* %p, i64 %iv.next.offset
85 %loaded = load atomic i32, i32* %addr unordered, align 4
86 %cond_2 = icmp eq i32 %loaded, %x
87 br i1 %cond_2, label %failure, label %loop
92 failure: ; preds = %backedge
96 define i32 @test_03(i32* %p, i64 %len, i32 %x) {
97 ; CHECK-LABEL: @test_03(
99 ; CHECK-NEXT: [[SCEVGEP:%.*]] = getelementptr i32, i32* [[P:%.*]], i64 -1
100 ; CHECK-NEXT: br label [[LOOP:%.*]]
102 ; CHECK-NEXT: [[LSR_IV:%.*]] = phi i64 [ [[LSR_IV_NEXT:%.*]], [[BACKEDGE:%.*]] ], [ [[LEN:%.*]], [[ENTRY:%.*]] ]
103 ; CHECK-NEXT: [[COND_1:%.*]] = icmp eq i64 [[LSR_IV]], 0
104 ; CHECK-NEXT: br i1 [[COND_1]], label [[EXIT:%.*]], label [[BACKEDGE]]
106 ; CHECK-NEXT: [[SCEVGEP1:%.*]] = getelementptr i32, i32* [[SCEVGEP]], i64 [[LSR_IV]]
107 ; CHECK-NEXT: [[LOADED:%.*]] = load atomic i32, i32* [[SCEVGEP1]] unordered, align 4
108 ; CHECK-NEXT: [[COND_2:%.*]] = icmp eq i32 [[LOADED]], [[X:%.*]]
109 ; CHECK-NEXT: [[LSR_IV_NEXT]] = add i64 [[LSR_IV]], -1
110 ; CHECK-NEXT: br i1 [[COND_2]], label [[FAILURE:%.*]], label [[LOOP]]
112 ; CHECK-NEXT: ret i32 -1
114 ; CHECK-NEXT: unreachable
117 %start = add i64 %len, -100
120 loop: ; preds = %backedge, %entry
121 %iv = phi i64 [ %iv.next, %backedge ], [ %start, %entry ]
122 %iv.next = add nsw i64 %iv, -1
123 %iv.offset = add i64 %iv, 100
124 %iv.next.offset = add i64 %iv.next, 100
125 %cond_1 = icmp eq i64 %iv.offset, 0
126 br i1 %cond_1, label %exit, label %backedge
128 backedge: ; preds = %loop
129 %addr = getelementptr inbounds i32, i32* %p, i64 %iv.next.offset
130 %loaded = load atomic i32, i32* %addr unordered, align 4
131 %cond_2 = icmp eq i32 %loaded, %x
132 br i1 %cond_2, label %failure, label %loop
134 exit: ; preds = %loop
137 failure: ; preds = %backedge
141 define i32 @test_04(i32* %p, i64 %len, i32 %x) {
142 ; CHECK-LABEL: @test_04(
144 ; CHECK-NEXT: [[SCEVGEP:%.*]] = getelementptr i32, i32* [[P:%.*]], i64 -1
145 ; CHECK-NEXT: br label [[LOOP:%.*]]
147 ; CHECK-NEXT: [[IV:%.*]] = phi i64 [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ], [ [[LEN:%.*]], [[ENTRY:%.*]] ]
148 ; CHECK-NEXT: [[COND_1:%.*]] = icmp eq i64 [[IV]], 0
149 ; CHECK-NEXT: br i1 [[COND_1]], label [[EXIT:%.*]], label [[BACKEDGE]]
151 ; CHECK-NEXT: [[SCEVGEP1:%.*]] = getelementptr i32, i32* [[SCEVGEP]], i64 [[IV]]
152 ; CHECK-NEXT: [[LOADED:%.*]] = load atomic i32, i32* [[SCEVGEP1]] unordered, align 4
153 ; CHECK-NEXT: [[COND_2:%.*]] = icmp eq i32 [[LOADED]], [[X:%.*]]
154 ; CHECK-NEXT: [[IV_NEXT]] = sub i64 [[IV]], 1
155 ; CHECK-NEXT: br i1 [[COND_2]], label [[FAILURE:%.*]], label [[LOOP]]
157 ; CHECK-NEXT: ret i32 -1
159 ; CHECK-NEXT: unreachable
164 loop: ; preds = %backedge, %entry
165 %iv = phi i64 [ %iv.next, %backedge ], [ %len, %entry ]
166 %iv.next = sub i64 %iv, 1
167 %cond_1 = icmp eq i64 %iv, 0
168 br i1 %cond_1, label %exit, label %backedge
170 backedge: ; preds = %loop
171 %addr = getelementptr inbounds i32, i32* %p, i64 %iv.next
172 %loaded = load atomic i32, i32* %addr unordered, align 4
173 %cond_2 = icmp eq i32 %loaded, %x
174 br i1 %cond_2, label %failure, label %loop
176 exit: ; preds = %loop
179 failure: ; preds = %backedge
183 define i32 @test_05(i32* %p, i64 %len, i32 %x) {
184 ; CHECK-LABEL: @test_05(
186 ; CHECK-NEXT: br label [[LOOP:%.*]]
188 ; CHECK-NEXT: [[IV:%.*]] = phi i64 [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ], [ [[LEN:%.*]], [[ENTRY:%.*]] ]
189 ; CHECK-NEXT: [[IV_NEXT]] = mul i64 [[IV]], 2
190 ; CHECK-NEXT: [[COND_1:%.*]] = icmp eq i64 [[IV]], 0
191 ; CHECK-NEXT: br i1 [[COND_1]], label [[EXIT:%.*]], label [[BACKEDGE]]
193 ; CHECK-NEXT: [[ADDR:%.*]] = getelementptr inbounds i32, i32* [[P:%.*]], i64 [[IV_NEXT]]
194 ; CHECK-NEXT: [[LOADED:%.*]] = load atomic i32, i32* [[ADDR]] unordered, align 4
195 ; CHECK-NEXT: [[COND_2:%.*]] = icmp eq i32 [[LOADED]], [[X:%.*]]
196 ; CHECK-NEXT: br i1 [[COND_2]], label [[FAILURE:%.*]], label [[LOOP]]
198 ; CHECK-NEXT: ret i32 -1
200 ; CHECK-NEXT: unreachable
205 loop: ; preds = %backedge, %entry
206 %iv = phi i64 [ %iv.next, %backedge ], [ %len, %entry ]
207 %iv.next = mul i64 %iv, 2
208 %cond_1 = icmp eq i64 %iv, 0
209 br i1 %cond_1, label %exit, label %backedge
211 backedge: ; preds = %loop
212 %addr = getelementptr inbounds i32, i32* %p, i64 %iv.next
213 %loaded = load atomic i32, i32* %addr unordered, align 4
214 %cond_2 = icmp eq i32 %loaded, %x
215 br i1 %cond_2, label %failure, label %loop
217 exit: ; preds = %loop
220 failure: ; preds = %backedge
224 define i32 @test_06(i32* %p, i64 %len, i32 %x, i64 %step) {
225 ; CHECK-LABEL: @test_06(
227 ; CHECK-NEXT: [[SCEVGEP:%.*]] = getelementptr i32, i32* [[P:%.*]], i64 [[STEP:%.*]]
228 ; CHECK-NEXT: br label [[LOOP:%.*]]
230 ; CHECK-NEXT: [[IV:%.*]] = phi i64 [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ], [ [[LEN:%.*]], [[ENTRY:%.*]] ]
231 ; CHECK-NEXT: [[IV_NEXT]] = add i64 [[IV]], [[STEP]]
232 ; CHECK-NEXT: [[COND_1:%.*]] = icmp eq i64 [[STEP]], [[IV_NEXT]]
233 ; CHECK-NEXT: br i1 [[COND_1]], label [[EXIT:%.*]], label [[BACKEDGE]]
235 ; CHECK-NEXT: [[SCEVGEP1:%.*]] = getelementptr i32, i32* [[SCEVGEP]], i64 [[IV]]
236 ; CHECK-NEXT: [[LOADED:%.*]] = load atomic i32, i32* [[SCEVGEP1]] unordered, align 4
237 ; CHECK-NEXT: [[COND_2:%.*]] = icmp eq i32 [[LOADED]], [[X:%.*]]
238 ; CHECK-NEXT: br i1 [[COND_2]], label [[FAILURE:%.*]], label [[LOOP]]
240 ; CHECK-NEXT: ret i32 -1
242 ; CHECK-NEXT: unreachable
247 loop: ; preds = %backedge, %entry
248 %iv = phi i64 [ %iv.next, %backedge ], [ %len, %entry ]
249 %iv.next = add nsw i64 %iv, %step
250 %cond_1 = icmp eq i64 %iv, 0
251 br i1 %cond_1, label %exit, label %backedge
253 backedge: ; preds = %loop
254 %addr = getelementptr inbounds i32, i32* %p, i64 %iv.next
255 %loaded = load atomic i32, i32* %addr unordered, align 4
256 %cond_2 = icmp eq i32 %loaded, %x
257 br i1 %cond_2, label %failure, label %loop
259 exit: ; preds = %loop
262 failure: ; preds = %backedge