1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
4 target datalayout = "e-p:64:64:64-p1:16:16:16-p2:32:32:32-p3:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
6 declare void @usei8ptr(ptr %ptr)
8 ; Ensure that we do not crash when looking at such a weird bitcast.
9 define i1 @bitcast_from_single_element_pointer_vector_to_pointer(<1 x ptr> %ptr1vec, ptr %ptr2) {
10 ; CHECK-LABEL: @bitcast_from_single_element_pointer_vector_to_pointer(
11 ; CHECK-NEXT: [[PTR1:%.*]] = bitcast <1 x ptr> [[PTR1VEC:%.*]] to ptr
12 ; CHECK-NEXT: call void @usei8ptr(ptr [[PTR1]])
13 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[PTR1]], [[PTR2:%.*]]
14 ; CHECK-NEXT: ret i1 [[CMP]]
16 %ptr1 = bitcast <1 x ptr> %ptr1vec to ptr
17 call void @usei8ptr(ptr %ptr1)
18 %cmp = icmp eq ptr %ptr1, %ptr2
22 define i1 @poison(i32 %x) {
23 ; CHECK-LABEL: @poison(
24 ; CHECK-NEXT: ret i1 poison
26 %v = icmp eq i32 %x, poison
30 define i1 @poison2(i32 %x) {
31 ; CHECK-LABEL: @poison2(
32 ; CHECK-NEXT: ret i1 poison
34 %v = icmp slt i32 %x, poison
38 define i1 @mul_div_cmp_smaller(i8 %x) {
39 ; CHECK-LABEL: @mul_div_cmp_smaller(
40 ; CHECK-NEXT: ret i1 true
43 %div = udiv i8 %mul, 4
44 %cmp = icmp ule i8 %div, %x
48 define i1 @mul_div_cmp_equal(i8 %x) {
49 ; CHECK-LABEL: @mul_div_cmp_equal(
50 ; CHECK-NEXT: ret i1 true
53 %div = udiv i8 %mul, 3
54 %cmp = icmp ule i8 %div, %x
59 define i1 @mul_div_cmp_greater(i8 %x) {
60 ; CHECK-LABEL: @mul_div_cmp_greater(
61 ; CHECK-NEXT: [[MUL:%.*]] = mul i8 [[X:%.*]], 3
62 ; CHECK-NEXT: [[DIV:%.*]] = udiv i8 [[MUL]], 2
63 ; CHECK-NEXT: [[CMP:%.*]] = icmp ule i8 [[DIV]], [[X]]
64 ; CHECK-NEXT: ret i1 [[CMP]]
67 %div = udiv i8 %mul, 2
68 %cmp = icmp ule i8 %div, %x
71 define i1 @mul_div_cmp_ugt(i8 %x) {
72 ; CHECK-LABEL: @mul_div_cmp_ugt(
73 ; CHECK-NEXT: ret i1 false
76 %div = udiv i8 %mul, 4
77 %cmp = icmp ugt i8 %div, %x
81 ; Negative test: Wrong predicate
82 define i1 @mul_div_cmp_uge(i8 %x) {
83 ; CHECK-LABEL: @mul_div_cmp_uge(
84 ; CHECK-NEXT: [[MUL:%.*]] = mul i8 [[X:%.*]], 3
85 ; CHECK-NEXT: [[DIV:%.*]] = udiv i8 [[MUL]], 4
86 ; CHECK-NEXT: [[CMP:%.*]] = icmp uge i8 [[DIV]], [[X]]
87 ; CHECK-NEXT: ret i1 [[CMP]]
90 %div = udiv i8 %mul, 4
91 %cmp = icmp uge i8 %div, %x
95 ; Negative test: Wrong predicate
96 define i1 @mul_div_cmp_ult(i8 %x) {
97 ; CHECK-LABEL: @mul_div_cmp_ult(
98 ; CHECK-NEXT: [[MUL:%.*]] = mul i8 [[X:%.*]], 3
99 ; CHECK-NEXT: [[DIV:%.*]] = udiv i8 [[MUL]], 4
100 ; CHECK-NEXT: [[CMP:%.*]] = icmp ult i8 [[DIV]], [[X]]
101 ; CHECK-NEXT: ret i1 [[CMP]]
104 %div = udiv i8 %mul, 4
105 %cmp = icmp ult i8 %div, %x
109 ; Negative test: Wrong icmp operand
110 define i1 @mul_div_cmp_wrong_operand(i8 %x, i8 %y) {
111 ; CHECK-LABEL: @mul_div_cmp_wrong_operand(
112 ; CHECK-NEXT: [[MUL:%.*]] = mul i8 [[X:%.*]], 3
113 ; CHECK-NEXT: [[DIV:%.*]] = udiv i8 [[MUL]], 4
114 ; CHECK-NEXT: [[CMP:%.*]] = icmp ule i8 [[DIV]], [[Y:%.*]]
115 ; CHECK-NEXT: ret i1 [[CMP]]
118 %div = udiv i8 %mul, 4
119 %cmp = icmp ule i8 %div, %y
123 define i1 @mul_lshr_cmp_smaller(i8 %x) {
124 ; CHECK-LABEL: @mul_lshr_cmp_smaller(
125 ; CHECK-NEXT: ret i1 true
128 %div = lshr i8 %mul, 2
129 %cmp = icmp ule i8 %div, %x
133 define i1 @mul_lshr_cmp_equal(i8 %x) {
134 ; CHECK-LABEL: @mul_lshr_cmp_equal(
135 ; CHECK-NEXT: ret i1 true
138 %div = lshr i8 %mul, 2
139 %cmp = icmp ule i8 %div, %x
143 define i1 @mul_lshr_cmp_greater(i8 %x) {
144 ; CHECK-LABEL: @mul_lshr_cmp_greater(
145 ; CHECK-NEXT: [[MUL:%.*]] = mul i8 [[X:%.*]], 5
146 ; CHECK-NEXT: [[DIV:%.*]] = lshr i8 [[MUL]], 2
147 ; CHECK-NEXT: [[CMP:%.*]] = icmp ule i8 [[DIV]], [[X]]
148 ; CHECK-NEXT: ret i1 [[CMP]]
151 %div = lshr i8 %mul, 2
152 %cmp = icmp ule i8 %div, %x
156 define i1 @shl_div_cmp_smaller(i8 %x) {
157 ; CHECK-LABEL: @shl_div_cmp_smaller(
158 ; CHECK-NEXT: ret i1 true
161 %div = udiv i8 %mul, 5
162 %cmp = icmp ule i8 %div, %x
166 define i1 @shl_div_cmp_equal(i8 %x) {
167 ; CHECK-LABEL: @shl_div_cmp_equal(
168 ; CHECK-NEXT: ret i1 true
171 %div = udiv i8 %mul, 4
172 %cmp = icmp ule i8 %div, %x
176 define i1 @shl_div_cmp_greater(i8 %x) {
177 ; CHECK-LABEL: @shl_div_cmp_greater(
178 ; CHECK-NEXT: [[MUL:%.*]] = shl i8 [[X:%.*]], 2
179 ; CHECK-NEXT: [[DIV:%.*]] = udiv i8 [[MUL]], 3
180 ; CHECK-NEXT: [[CMP:%.*]] = icmp ule i8 [[DIV]], [[X]]
181 ; CHECK-NEXT: ret i1 [[CMP]]
184 %div = udiv i8 %mul, 3
185 %cmp = icmp ule i8 %div, %x
189 ; Don't crash matching recurrences/invertible ops.
191 define void @PR50191(i32 %x) {
192 ; CHECK-LABEL: @PR50191(
194 ; CHECK-NEXT: br label [[LOOP:%.*]]
196 ; CHECK-NEXT: [[P1:%.*]] = phi i32 [ [[X:%.*]], [[ENTRY:%.*]] ], [ [[SUB1:%.*]], [[LOOP]] ]
197 ; CHECK-NEXT: [[P2:%.*]] = phi i32 [ [[X]], [[ENTRY]] ], [ [[SUB2:%.*]], [[LOOP]] ]
198 ; CHECK-NEXT: [[SUB1]] = sub i32 [[P1]], [[P2]]
199 ; CHECK-NEXT: [[SUB2]] = sub i32 42, [[P2]]
200 ; CHECK-NEXT: br label [[LOOP]]
206 %p1 = phi i32 [ %x, %entry ], [ %sub1, %loop ]
207 %p2 = phi i32 [ %x, %entry ], [ %sub2, %loop ]
208 %cmp = icmp eq i32 %p1, %p2
209 %user = zext i1 %cmp to i32
210 %sub1 = sub i32 %p1, %p2
211 %sub2 = sub i32 42, %p2
215 define i1 @sub_false(i32 %x) {
216 ; CHECK-LABEL: @sub_false(
217 ; CHECK-NEXT: ret i1 false
220 %cmp = icmp eq i32 %sub, %x
224 define i1 @sub_swap(i8 %x) {
225 ; CHECK-LABEL: @sub_swap(
226 ; CHECK-NEXT: ret i1 true
229 %cmp = icmp ne i8 %x, %sub
233 define <2 x i1> @sub_odd(<2 x i8> %x) {
234 ; CHECK-LABEL: @sub_odd(
235 ; CHECK-NEXT: ret <2 x i1> <i1 true, i1 true>
237 %sub = sub <2 x i8> <i8 3, i8 3>, %x
238 %cmp = icmp ne <2 x i8> %sub, %x
242 define <2 x i1> @sub_odd_poison(<2 x i8> %x) {
243 ; CHECK-LABEL: @sub_odd_poison(
244 ; CHECK-NEXT: ret <2 x i1> <i1 true, i1 true>
246 %sub = sub <2 x i8> <i8 poison, i8 1>, %x
247 %cmp = icmp ne <2 x i8> %sub, %x
251 define i1 @sub_even(i8 %x) {
252 ; CHECK-LABEL: @sub_even(
253 ; CHECK-NEXT: [[SUB:%.*]] = sub i8 2, [[X:%.*]]
254 ; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[SUB]], [[X]]
255 ; CHECK-NEXT: ret i1 [[CMP]]
258 %cmp = icmp ne i8 %sub, %x
262 define i1 @load_ptr(ptr %p) {
263 ; CHECK-LABEL: @load_ptr(
264 ; CHECK-NEXT: ret i1 true
266 %load_p = load ptr, ptr %p, !dereferenceable !{i64 8}
267 %r = icmp ne ptr %load_p, null
271 define i1 @load_ptr_null_valid(ptr %p) null_pointer_is_valid {
272 ; CHECK-LABEL: @load_ptr_null_valid(
273 ; CHECK-NEXT: [[LOAD_P:%.*]] = load ptr, ptr [[P:%.*]], align 8, !dereferenceable !0
274 ; CHECK-NEXT: [[R:%.*]] = icmp ne ptr [[LOAD_P]], null
275 ; CHECK-NEXT: ret i1 [[R]]
277 %load_p = load ptr, ptr %p, !dereferenceable !{i64 8}
278 %r = icmp ne ptr %load_p, null