1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
2 ; RUN: opt -S -passes=indvars < %s | FileCheck %s
4 ; Check that SCEV is able to recognize and use guards to prove
5 ; conditions gaurding loop entries and backedges. This isn't intended
6 ; to be a comprehensive test of SCEV's simplification capabilities,
7 ; tests directly testing e.g. if SCEV can elide a sext should go
10 target datalayout = "n8:16:32:64"
12 declare void @llvm.experimental.guard(i1, ...)
14 declare void @use(i64 %x)
16 define void @test_1(ptr %cond_buf, ptr %len_buf) {
17 ; CHECK-LABEL: define void @test_1
18 ; CHECK-SAME: (ptr [[COND_BUF:%.*]], ptr [[LEN_BUF:%.*]]) {
20 ; CHECK-NEXT: [[LEN:%.*]] = load i32, ptr [[LEN_BUF]], align 4, !range [[RNG0:![0-9]+]]
21 ; CHECK-NEXT: br label [[LOOP:%.*]]
23 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_INC:%.*]], [[LOOP]] ]
24 ; CHECK-NEXT: [[IV_INC]] = add nsw i32 [[IV]], 1
25 ; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 true) [ "deopt"() ]
26 ; CHECK-NEXT: [[IV_INC_CMP:%.*]] = icmp ult i32 [[IV_INC]], [[LEN]]
27 ; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 [[IV_INC_CMP]]) [ "deopt"() ]
28 ; CHECK-NEXT: [[BECOND:%.*]] = load volatile i1, ptr [[COND_BUF]], align 1
29 ; CHECK-NEXT: br i1 [[BECOND]], label [[LOOP]], label [[LEAVE:%.*]]
31 ; CHECK-NEXT: ret void
34 %len = load i32, ptr %len_buf, !range !{i32 1, i32 2147483648}
39 %iv = phi i32 [ 0, %entry ], [ %iv.inc, %loop ]
40 %iv.inc = add i32 %iv, 1
42 %iv.cmp = icmp slt i32 %iv, %len
43 call void(i1, ...) @llvm.experimental.guard(i1 %iv.cmp) [ "deopt"() ]
45 %iv.inc.cmp = icmp slt i32 %iv.inc, %len
46 call void(i1, ...) @llvm.experimental.guard(i1 %iv.inc.cmp) [ "deopt"() ]
48 %becond = load volatile i1, ptr %cond_buf
49 br i1 %becond, label %loop, label %leave
55 define void @test_2(i32 %n, ptr %len_buf) {
56 ; CHECK-LABEL: define void @test_2
57 ; CHECK-SAME: (i32 [[N:%.*]], ptr [[LEN_BUF:%.*]]) {
59 ; CHECK-NEXT: [[LEN:%.*]] = load i32, ptr [[LEN_BUF]], align 4, !range [[RNG1:![0-9]+]]
60 ; CHECK-NEXT: [[TMP0:%.*]] = zext i32 [[LEN]] to i64
61 ; CHECK-NEXT: [[TMP1:%.*]] = zext i32 [[N]] to i64
62 ; CHECK-NEXT: br label [[LOOP:%.*]]
64 ; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ [[INDVARS_IV_NEXT:%.*]], [[LOOP]] ], [ 0, [[ENTRY:%.*]] ]
65 ; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
66 ; CHECK-NEXT: call void @use(i64 [[INDVARS_IV]])
67 ; CHECK-NEXT: [[IV_INC_CMP:%.*]] = icmp ult i64 [[INDVARS_IV_NEXT]], [[TMP0]]
68 ; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 [[IV_INC_CMP]]) [ "deopt"() ]
69 ; CHECK-NEXT: [[BECOND:%.*]] = icmp ne i64 [[INDVARS_IV]], [[TMP1]]
70 ; CHECK-NEXT: br i1 [[BECOND]], label [[LOOP]], label [[LEAVE:%.*]]
72 ; CHECK-NEXT: ret void
76 %len = load i32, ptr %len_buf, !range !{i32 0, i32 2147483648}
81 %iv = phi i32 [ 0, %entry ], [ %iv.inc, %loop ]
82 %iv.inc = add i32 %iv, 1
84 %iv.sext = sext i32 %iv to i64
85 call void @use(i64 %iv.sext)
87 %iv.inc.cmp = icmp slt i32 %iv.inc, %len
88 call void(i1, ...) @llvm.experimental.guard(i1 %iv.inc.cmp) [ "deopt"() ]
90 %becond = icmp ne i32 %iv, %n
91 br i1 %becond, label %loop, label %leave
97 define void @test_3(ptr %cond_buf, ptr %len_buf) {
98 ; CHECK-LABEL: define void @test_3
99 ; CHECK-SAME: (ptr [[COND_BUF:%.*]], ptr [[LEN_BUF:%.*]]) {
101 ; CHECK-NEXT: [[LEN:%.*]] = load i32, ptr [[LEN_BUF]], align 4
102 ; CHECK-NEXT: [[ENTRY_COND:%.*]] = icmp sgt i32 [[LEN]], 0
103 ; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 [[ENTRY_COND]]) [ "deopt"() ]
104 ; CHECK-NEXT: br label [[LOOP:%.*]]
106 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_INC:%.*]], [[LOOP]] ]
107 ; CHECK-NEXT: [[IV_INC]] = add nsw i32 [[IV]], 1
108 ; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 true) [ "deopt"() ]
109 ; CHECK-NEXT: [[IV_INC_CMP:%.*]] = icmp slt i32 [[IV_INC]], [[LEN]]
110 ; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 [[IV_INC_CMP]]) [ "deopt"() ]
111 ; CHECK-NEXT: [[BECOND:%.*]] = load volatile i1, ptr [[COND_BUF]], align 1
112 ; CHECK-NEXT: br i1 [[BECOND]], label [[LOOP]], label [[LEAVE:%.*]]
114 ; CHECK-NEXT: ret void
118 %len = load i32, ptr %len_buf
119 %entry.cond = icmp sgt i32 %len, 0
120 call void(i1, ...) @llvm.experimental.guard(i1 %entry.cond) [ "deopt"() ]
124 %iv = phi i32 [ 0, %entry ], [ %iv.inc, %loop ]
125 %iv.inc = add i32 %iv, 1
127 %iv.cmp = icmp slt i32 %iv, %len
128 call void(i1, ...) @llvm.experimental.guard(i1 %iv.cmp) [ "deopt"() ]
130 %iv.inc.cmp = icmp slt i32 %iv.inc, %len
131 call void(i1, ...) @llvm.experimental.guard(i1 %iv.inc.cmp) [ "deopt"() ]
133 %becond = load volatile i1, ptr %cond_buf
134 br i1 %becond, label %loop, label %leave
140 define void @test_4(ptr %cond_buf, ptr %len_buf) {
141 ; CHECK-LABEL: define void @test_4
142 ; CHECK-SAME: (ptr [[COND_BUF:%.*]], ptr [[LEN_BUF:%.*]]) {
144 ; CHECK-NEXT: [[LEN:%.*]] = load i32, ptr [[LEN_BUF]], align 4
145 ; CHECK-NEXT: [[ENTRY_COND:%.*]] = icmp sgt i32 [[LEN]], 0
146 ; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 [[ENTRY_COND]]) [ "deopt"() ]
147 ; CHECK-NEXT: br label [[LOOP:%.*]]
149 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_INC:%.*]], [[BE:%.*]] ]
150 ; CHECK-NEXT: [[IV_INC]] = add i32 [[IV]], 1
151 ; CHECK-NEXT: [[COND:%.*]] = load volatile i1, ptr [[COND_BUF]], align 1
152 ; CHECK-NEXT: br i1 [[COND]], label [[LEFT:%.*]], label [[BE]]
154 ; CHECK-NEXT: [[IV_INC_CMP:%.*]] = icmp slt i32 [[IV_INC]], [[LEN]]
155 ; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 [[IV_INC_CMP]]) [ "deopt"() ]
156 ; CHECK-NEXT: br label [[BE]]
158 ; CHECK-NEXT: [[IV_CMP:%.*]] = icmp slt i32 [[IV]], [[LEN]]
159 ; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 [[IV_CMP]]) [ "deopt"() ]
160 ; CHECK-NEXT: [[BECOND:%.*]] = load volatile i1, ptr [[COND_BUF]], align 1
161 ; CHECK-NEXT: br i1 [[BECOND]], label [[LOOP]], label [[LEAVE:%.*]]
163 ; CHECK-NEXT: ret void
167 %len = load i32, ptr %len_buf
168 %entry.cond = icmp sgt i32 %len, 0
169 call void(i1, ...) @llvm.experimental.guard(i1 %entry.cond) [ "deopt"() ]
173 %iv = phi i32 [ 0, %entry ], [ %iv.inc, %be ]
174 %iv.inc = add i32 %iv, 1
176 %cond = load volatile i1, ptr %cond_buf
177 br i1 %cond, label %left, label %be
180 ; Does not dominate the backedge, so cannot be used in the inductive proof
181 %iv.inc.cmp = icmp slt i32 %iv.inc, %len
182 call void(i1, ...) @llvm.experimental.guard(i1 %iv.inc.cmp) [ "deopt"() ]
187 %iv.cmp = icmp slt i32 %iv, %len
188 call void(i1, ...) @llvm.experimental.guard(i1 %iv.cmp) [ "deopt"() ]
190 %becond = load volatile i1, ptr %cond_buf
191 br i1 %becond, label %loop, label %leave