1 ; RUN: opt -S -guard-widening < %s | FileCheck %s
3 declare void @llvm.experimental.guard(i1,...)
5 define void @f_0(i32 %x, i32* %length_buf) {
7 ; CHECK-NOT: @llvm.experimental.guard
8 ; CHECK: %wide.chk2 = and i1 %chk3, %chk0
9 ; CHECK: call void (i1, ...) @llvm.experimental.guard(i1 %wide.chk2) [ "deopt"() ]
12 %length = load i32, i32* %length_buf, !range !0
13 %chk0 = icmp ult i32 %x, %length
14 call void(i1, ...) @llvm.experimental.guard(i1 %chk0) [ "deopt"() ]
16 %x.inc1 = add i32 %x, 1
17 %chk1 = icmp ult i32 %x.inc1, %length
18 call void(i1, ...) @llvm.experimental.guard(i1 %chk1) [ "deopt"() ]
20 %x.inc2 = add i32 %x, 2
21 %chk2 = icmp ult i32 %x.inc2, %length
22 call void(i1, ...) @llvm.experimental.guard(i1 %chk2) [ "deopt"() ]
24 %x.inc3 = add i32 %x, 3
25 %chk3 = icmp ult i32 %x.inc3, %length
26 call void(i1, ...) @llvm.experimental.guard(i1 %chk3) [ "deopt"() ]
30 define void @f_1(i32 %x, i32* %length_buf) {
32 ; CHECK-NOT: llvm.experimental.guard
33 ; CHECK: %wide.chk2 = and i1 %chk3, %chk0
34 ; CHECK: call void (i1, ...) @llvm.experimental.guard(i1 %wide.chk2) [ "deopt"() ]
37 %length = load i32, i32* %length_buf, !range !0
38 %chk0 = icmp ult i32 %x, %length
39 call void(i1, ...) @llvm.experimental.guard(i1 %chk0) [ "deopt"() ]
41 %x.inc1 = add i32 %x, 1
42 %chk1 = icmp ult i32 %x.inc1, %length
43 call void(i1, ...) @llvm.experimental.guard(i1 %chk1) [ "deopt"() ]
45 %x.inc2 = add i32 %x.inc1, 2
46 %chk2 = icmp ult i32 %x.inc2, %length
47 call void(i1, ...) @llvm.experimental.guard(i1 %chk2) [ "deopt"() ]
49 %x.inc3 = add i32 %x.inc2, 3
50 %chk3 = icmp ult i32 %x.inc3, %length
51 call void(i1, ...) @llvm.experimental.guard(i1 %chk3) [ "deopt"() ]
55 define void @f_2(i32 %a, i32* %length_buf) {
57 ; CHECK-NOT: llvm.experimental.guard
58 ; CHECK: %wide.chk2 = and i1 %chk3, %chk0
59 ; CHECK: call void (i1, ...) @llvm.experimental.guard(i1 %wide.chk2) [ "deopt"() ]
62 %x = and i32 %a, 4294967040 ;; 4294967040 == 0xffffff00
63 %length = load i32, i32* %length_buf, !range !0
64 %chk0 = icmp ult i32 %x, %length
65 call void(i1, ...) @llvm.experimental.guard(i1 %chk0) [ "deopt"() ]
67 %x.inc1 = or i32 %x, 1
68 %chk1 = icmp ult i32 %x.inc1, %length
69 call void(i1, ...) @llvm.experimental.guard(i1 %chk1) [ "deopt"() ]
71 %x.inc2 = or i32 %x, 2
72 %chk2 = icmp ult i32 %x.inc2, %length
73 call void(i1, ...) @llvm.experimental.guard(i1 %chk2) [ "deopt"() ]
75 %x.inc3 = or i32 %x, 3
76 %chk3 = icmp ult i32 %x.inc3, %length
77 call void(i1, ...) @llvm.experimental.guard(i1 %chk3) [ "deopt"() ]
81 define void @f_3(i32 %a, i32* %length_buf) {
83 ; CHECK-NOT: llvm.experimental.guard
84 ; CHECK: %wide.chk2 = and i1 %chk3, %chk0
85 ; CHECK: call void (i1, ...) @llvm.experimental.guard(i1 %wide.chk2) [ "deopt"() ]
88 %x = and i32 %a, 4294967040 ;; 4294967040 == 0xffffff00
89 %length = load i32, i32* %length_buf, !range !0
90 %chk0 = icmp ult i32 %x, %length
91 call void(i1, ...) @llvm.experimental.guard(i1 %chk0) [ "deopt"() ]
93 %x.inc1 = add i32 %x, 1
94 %chk1 = icmp ult i32 %x.inc1, %length
95 call void(i1, ...) @llvm.experimental.guard(i1 %chk1) [ "deopt"() ]
97 %x.inc2 = or i32 %x.inc1, 2
98 %chk2 = icmp ult i32 %x.inc2, %length
99 call void(i1, ...) @llvm.experimental.guard(i1 %chk2) [ "deopt"() ]
101 %x.inc3 = add i32 %x.inc2, 3
102 %chk3 = icmp ult i32 %x.inc3, %length
103 call void(i1, ...) @llvm.experimental.guard(i1 %chk3) [ "deopt"() ]
107 define void @f_4(i32 %x, i32* %length_buf) {
109 ; CHECK-NOT: llvm.experimental.guard
111 ; Note: we NOT guarding on "and i1 %chk3, %chk0", that would be incorrect.
112 ; CHECK: %wide.chk2 = and i1 %chk3, %chk1
113 ; CHECK: call void (i1, ...) @llvm.experimental.guard(i1 %wide.chk2) [ "deopt"() ]
116 %length = load i32, i32* %length_buf, !range !0
117 %chk0 = icmp ult i32 %x, %length
118 call void(i1, ...) @llvm.experimental.guard(i1 %chk0) [ "deopt"() ]
120 %x.inc1 = add i32 %x, -1024
121 %chk1 = icmp ult i32 %x.inc1, %length
122 call void(i1, ...) @llvm.experimental.guard(i1 %chk1) [ "deopt"() ]
124 %x.inc2 = add i32 %x, 2
125 %chk2 = icmp ult i32 %x.inc2, %length
126 call void(i1, ...) @llvm.experimental.guard(i1 %chk2) [ "deopt"() ]
128 %x.inc3 = add i32 %x, 3
129 %chk3 = icmp ult i32 %x.inc3, %length
130 call void(i1, ...) @llvm.experimental.guard(i1 %chk3) [ "deopt"() ]
134 define void @f_5(i32 %x, i32* %length_buf) {
136 ; CHECK-NOT: llvm.experimental.guard
137 ; CHECK: %wide.chk2 = and i1 %chk1, %chk2
138 ; CHECK: call void (i1, ...) @llvm.experimental.guard(i1 %wide.chk2) [ "deopt"() ]
141 %length = load i32, i32* %length_buf, !range !0
142 %chk0 = icmp ult i32 %x, %length
143 call void(i1, ...) @llvm.experimental.guard(i1 %chk0) [ "deopt"() ]
145 %x.inc1 = add i32 %x, 1
146 %chk1 = icmp ult i32 %x.inc1, %length
147 call void(i1, ...) @llvm.experimental.guard(i1 %chk1) [ "deopt"() ]
149 %x.inc2 = add i32 %x.inc1, -200
150 %chk2 = icmp ult i32 %x.inc2, %length
151 call void(i1, ...) @llvm.experimental.guard(i1 %chk2) [ "deopt"() ]
153 %x.inc3 = add i32 %x.inc2, 3
154 %chk3 = icmp ult i32 %x.inc3, %length
155 call void(i1, ...) @llvm.experimental.guard(i1 %chk3) [ "deopt"() ]
160 ; Negative test: we can't merge these checks into
162 ; (%x + -2147483647) u< L && (%x + 3) u< L
164 ; because if %length == INT_MAX and %x == -3 then
166 ; (%x + -2147483647) == i32 2147483646 u< L (L is 2147483647)
169 ; But (%x + 2) == -1 is not u< L
171 define void @f_6(i32 %x, i32* %length_buf) {
173 ; CHECK-NOT: llvm.experimental.guard
174 ; CHECK: %wide.chk = and i1 %chk0, %chk1
175 ; CHECK: %wide.chk1 = and i1 %wide.chk, %chk2
176 ; CHECK: %wide.chk2 = and i1 %wide.chk1, %chk3
177 ; CHECK: call void (i1, ...) @llvm.experimental.guard(i1 %wide.chk2) [ "deopt"() ]
179 %length = load i32, i32* %length_buf, !range !0
180 %chk0 = icmp ult i32 %x, %length
181 call void(i1, ...) @llvm.experimental.guard(i1 %chk0) [ "deopt"() ]
183 %x.inc1 = add i32 %x, -2147483647 ;; -2147483647 == (i32 INT_MIN)+1 == -(i32 INT_MAX)
184 %chk1 = icmp ult i32 %x.inc1, %length
185 call void(i1, ...) @llvm.experimental.guard(i1 %chk1) [ "deopt"() ]
187 %x.inc2 = add i32 %x, 2
188 %chk2 = icmp ult i32 %x.inc2, %length
189 call void(i1, ...) @llvm.experimental.guard(i1 %chk2) [ "deopt"() ]
191 %x.inc3 = add i32 %x, 3
192 %chk3 = icmp ult i32 %x.inc3, %length
193 call void(i1, ...) @llvm.experimental.guard(i1 %chk3) [ "deopt"() ]
198 define void @f_7(i32 %x, i32* %length_buf) {
201 ; CHECK: [[COND_0:%[^ ]+]] = and i1 %chk3.b, %chk0.b
202 ; CHECK: [[COND_1:%[^ ]+]] = and i1 %chk0.a, [[COND_0]]
203 ; CHECK: [[COND_2:%[^ ]+]] = and i1 %chk3.a, [[COND_1]]
204 ; CHECK: call void (i1, ...) @llvm.experimental.guard(i1 [[COND_2]]) [ "deopt"() ]
207 %length_a = load volatile i32, i32* %length_buf, !range !0
208 %length_b = load volatile i32, i32* %length_buf, !range !0
209 %chk0.a = icmp ult i32 %x, %length_a
210 %chk0.b = icmp ult i32 %x, %length_b
211 %chk0 = and i1 %chk0.a, %chk0.b
212 call void(i1, ...) @llvm.experimental.guard(i1 %chk0) [ "deopt"() ]
214 %x.inc1 = add i32 %x, 1
215 %chk1.a = icmp ult i32 %x.inc1, %length_a
216 %chk1.b = icmp ult i32 %x.inc1, %length_b
217 %chk1 = and i1 %chk1.a, %chk1.b
218 call void(i1, ...) @llvm.experimental.guard(i1 %chk1) [ "deopt"() ]
220 %x.inc2 = add i32 %x, 2
221 %chk2.a = icmp ult i32 %x.inc2, %length_a
222 %chk2.b = icmp ult i32 %x.inc2, %length_b
223 %chk2 = and i1 %chk2.a, %chk2.b
224 call void(i1, ...) @llvm.experimental.guard(i1 %chk2) [ "deopt"() ]
226 %x.inc3 = add i32 %x, 3
227 %chk3.a = icmp ult i32 %x.inc3, %length_a
228 %chk3.b = icmp ult i32 %x.inc3, %length_b
229 %chk3 = and i1 %chk3.a, %chk3.b
230 call void(i1, ...) @llvm.experimental.guard(i1 %chk3) [ "deopt"() ]
235 !0 = !{i32 0, i32 2147483648}