1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instsimplify -S | FileCheck %s
4 ; A == B implies A >u B is false.
6 define void @test1(i32 %a, i32 %b) {
8 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
9 ; CHECK-NEXT: br i1 [[CMP1]], label [[TAKEN:%.*]], label [[END:%.*]]
11 ; CHECK-NEXT: call void @foo(i32 10)
12 ; CHECK-NEXT: br label [[END]]
14 ; CHECK-NEXT: ret void
16 %cmp1 = icmp eq i32 %a, %b
17 br i1 %cmp1, label %taken, label %end
20 %cmp2 = icmp ugt i32 %a, %b
21 %c = select i1 %cmp2, i32 0, i32 10
22 call void @foo(i32 %c)
29 ; If A == B is false then A != B is true.
31 define void @test2(i32 %a, i32 %b) {
32 ; CHECK-LABEL: @test2(
33 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
34 ; CHECK-NEXT: br i1 [[CMP1]], label [[END:%.*]], label [[TAKEN:%.*]]
36 ; CHECK-NEXT: call void @foo(i32 20)
37 ; CHECK-NEXT: br label [[END]]
39 ; CHECK-NEXT: ret void
41 %cmp1 = icmp eq i32 %a, %b
42 br i1 %cmp1, label %end, label %taken
45 %cmp2 = icmp ne i32 %a, %b
46 %c = select i1 %cmp2, i32 20, i32 0
47 call void @foo(i32 %c)
54 ; A >u 10 implies A >u 10 is true.
56 define void @test3(i32 %a) {
57 ; CHECK-LABEL: @test3(
58 ; CHECK-NEXT: [[CMP1:%.*]] = icmp ugt i32 [[A:%.*]], 10
59 ; CHECK-NEXT: br i1 [[CMP1]], label [[TAKEN:%.*]], label [[END:%.*]]
61 ; CHECK-NEXT: call void @foo(i32 30)
62 ; CHECK-NEXT: br label [[END]]
64 ; CHECK-NEXT: ret void
66 %cmp1 = icmp ugt i32 %a, 10
67 br i1 %cmp1, label %taken, label %end
70 %cmp2 = icmp ugt i32 %a, 10
71 %c = select i1 %cmp2, i32 30, i32 0
72 call void @foo(i32 %c)
79 define i8 @PR23333(i8 addrspace(1)* %ptr) {
80 ; CHECK-LABEL: @PR23333(
81 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 addrspace(1)* [[PTR:%.*]], null
82 ; CHECK-NEXT: br i1 [[CMP]], label [[TAKEN:%.*]], label [[END:%.*]]
84 ; CHECK-NEXT: ret i8 1
86 ; CHECK-NEXT: ret i8 0
88 %cmp = icmp eq i8 addrspace(1)* %ptr, null
89 br i1 %cmp, label %taken, label %end
92 %cmp2 = icmp ne i8 addrspace(1)* %ptr, null
93 %res = select i1 %cmp2, i8 2, i8 1
100 ; We know the condition of the select is true based on a dominating condition.
101 ; Therefore, we can replace %cond with %len.
102 ; TODO: len == 8 is known false in bb. This is handled by other passes, but should it be handled here?
104 define void @test4(i32 %len) {
105 ; CHECK-LABEL: @test4(
107 ; CHECK-NEXT: [[TMP0:%.*]] = call i32 @bar(i32 [[LEN:%.*]])
108 ; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[LEN]], 4
109 ; CHECK-NEXT: br i1 [[CMP]], label [[BB:%.*]], label [[B1:%.*]]
111 ; CHECK-NEXT: [[CMP11:%.*]] = icmp eq i32 [[LEN]], 8
112 ; CHECK-NEXT: br i1 [[CMP11]], label [[B0:%.*]], label [[B1]]
114 ; CHECK-NEXT: call void @foo(i32 [[LEN]])
115 ; CHECK-NEXT: br label [[B1]]
117 ; CHECK-NEXT: [[TMP1:%.*]] = phi i32 [ [[LEN]], [[BB]] ], [ undef, [[B0]] ], [ [[TMP0]], [[ENTRY:%.*]] ]
118 ; CHECK-NEXT: br label [[RET:%.*]]
120 ; CHECK-NEXT: call void @foo(i32 [[TMP1]])
121 ; CHECK-NEXT: ret void
124 %0 = call i32 @bar(i32 %len);
125 %cmp = icmp ult i32 %len, 4
126 br i1 %cmp, label %bb, label %b1
128 %cond = select i1 %cmp, i32 %len, i32 8
129 %cmp11 = icmp eq i32 %cond, 8
130 br i1 %cmp11, label %b0, label %b1
133 call void @foo(i32 %len)
137 %1 = phi i32 [ %cond, %bb ], [ undef, %b0 ], [ %0, %entry ]
141 call void @foo(i32 %1)
145 ; A >u 10 implies A >u 9 is true.
147 define void @test5(i32 %a) {
148 ; CHECK-LABEL: @test5(
149 ; CHECK-NEXT: [[CMP1:%.*]] = icmp ugt i32 [[A:%.*]], 10
150 ; CHECK-NEXT: br i1 [[CMP1]], label [[TAKEN:%.*]], label [[END:%.*]]
152 ; CHECK-NEXT: call void @foo(i32 30)
153 ; CHECK-NEXT: br label [[END]]
155 ; CHECK-NEXT: ret void
157 %cmp1 = icmp ugt i32 %a, 10
158 br i1 %cmp1, label %taken, label %end
161 %cmp2 = icmp ugt i32 %a, 9
162 %c = select i1 %cmp2, i32 30, i32 0
163 call void @foo(i32 %c)
170 declare void @foo(i32)
171 declare i32 @bar(i32)
173 define i32 @test_and(i32 %a, i32 %b) {
174 ; CHECK-LABEL: @test_and(
176 ; CHECK-NEXT: [[CMP1:%.*]] = icmp ne i32 [[A:%.*]], 0
177 ; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i32 [[B:%.*]], 0
178 ; CHECK-NEXT: [[AND:%.*]] = and i1 [[CMP1]], [[CMP2]]
179 ; CHECK-NEXT: br i1 [[AND]], label [[TPATH:%.*]], label [[END:%.*]]
181 ; CHECK-NEXT: ret i32 313
183 ; CHECK-NEXT: ret i32 0
186 %cmp1 = icmp ne i32 %a, 0
187 %cmp2 = icmp ne i32 %b, 0
188 %and = and i1 %cmp1, %cmp2
189 br i1 %and, label %tpath, label %end
192 %cmp3 = icmp eq i32 %a, 0 ;; <-- implied false
193 %c = select i1 %cmp3, i32 0, i32 313
200 ; cmp1 and cmp2 are false on the 'fpath' path and thus cmp3 is true.
202 define i32 @test_or1(i32 %a, i32 %b) {
203 ; CHECK-LABEL: @test_or1(
205 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], 0
206 ; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i32 [[B:%.*]], 0
207 ; CHECK-NEXT: [[OR:%.*]] = or i1 [[CMP1]], [[CMP2]]
208 ; CHECK-NEXT: br i1 [[OR]], label [[END:%.*]], label [[FPATH:%.*]]
210 ; CHECK-NEXT: ret i32 37
212 ; CHECK-NEXT: ret i32 0
215 %cmp1 = icmp eq i32 %a, 0
216 %cmp2 = icmp eq i32 %b, 0
217 %or = or i1 %cmp1, %cmp2
218 br i1 %or, label %end, label %fpath
221 %cmp3 = icmp ne i32 %a, 0 ;; <-- implied true
222 %c = select i1 %cmp3, i32 37, i32 0
229 ; LHS ==> RHS by definition (true -> true)
231 define void @test6(i32 %a, i32 %b) {
232 ; CHECK-LABEL: @test6(
233 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
234 ; CHECK-NEXT: br i1 [[CMP1]], label [[TAKEN:%.*]], label [[END:%.*]]
236 ; CHECK-NEXT: call void @foo(i32 10)
237 ; CHECK-NEXT: br label [[END]]
239 ; CHECK-NEXT: ret void
241 %cmp1 = icmp eq i32 %a, %b
242 br i1 %cmp1, label %taken, label %end
245 %c = select i1 %cmp1, i32 10, i32 0
246 call void @foo(i32 %c)
253 ; LHS ==> RHS by definition (false -> false)
255 define void @test7(i32 %a, i32 %b) {
256 ; CHECK-LABEL: @test7(
257 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
258 ; CHECK-NEXT: br i1 [[CMP1]], label [[END:%.*]], label [[TAKEN:%.*]]
260 ; CHECK-NEXT: call void @foo(i32 11)
261 ; CHECK-NEXT: br label [[END]]
263 ; CHECK-NEXT: ret void
265 %cmp1 = icmp eq i32 %a, %b
266 br i1 %cmp1, label %end, label %taken
269 %c = select i1 %cmp1, i32 0, i32 11
270 call void @foo(i32 %c)