llvm.org GIT mirror llvm / 39a7de7
Add some transforms of the kind X-Y>X -> 0>Y which are valid when there is no overflow. These subsume some existing equality transforms, so zap those. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@125843 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan Sands 8 years ago
3 changed file(s) with 143 addition(s) and 20 deletion(s). Raw diff Collapse all Expand all
22662266 return new ICmpInst(Pred, Constant::getNullValue(Op0->getType()),
22672267 C == Op0 ? D : C);
22682268
2269 // icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow.
2269 // icmp (X+Y), (X+Z) -> icmp Y, Z for equalities or if there is no overflow.
22702270 if (A && C && (A == C || A == D || B == C || B == D) &&
22712271 NoOp0WrapProblem && NoOp1WrapProblem &&
22722272 // Try not to increase register pressure.
22852285 if (BO1 && BO1->getOpcode() == Instruction::Sub)
22862286 C = BO1->getOperand(0), D = BO1->getOperand(1);
22872287
2288 // icmp (Y-X), (Z-X) -> icmp Y,Z for equalities or if there is no overflow.
2288 // icmp (X-Y), X -> icmp 0, Y for equalities or if there is no overflow.
2289 if (A == Op1 && NoOp0WrapProblem)
2290 return new ICmpInst(Pred, Constant::getNullValue(Op1->getType()), B);
2291
2292 // icmp X, (X-Y) -> icmp Y, 0 for equalities or if there is no overflow.
2293 if (C == Op0 && NoOp1WrapProblem)
2294 return new ICmpInst(Pred, D, Constant::getNullValue(Op0->getType()));
2295
2296 // icmp (Y-X), (Z-X) -> icmp Y, Z for equalities or if there is no overflow.
22892297 if (B && D && B == D && NoOp0WrapProblem && NoOp1WrapProblem &&
22902298 // Try not to increase register pressure.
22912299 BO0->hasOneUse() && BO1->hasOneUse())
22922300 return new ICmpInst(Pred, A, C);
2301
2302 // icmp (X-Y), (X-Z) -> icmp Z, Y for equalities or if there is no overflow.
2303 if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem &&
2304 // Try not to increase register pressure.
2305 BO0->hasOneUse() && BO1->hasOneUse())
2306 return new ICmpInst(Pred, D, B);
22932307
22942308 if (BO0 && BO1 && BO0->getOpcode() == BO1->getOpcode() &&
22952309 BO0->hasOneUse() && BO1->hasOneUse() &&
23742388
23752389 if (I.isEquality()) {
23762390 Value *A, *B, *C, *D;
2377
2378 // -x == -y --> x == y
2379 if (match(Op0, m_Neg(m_Value(A))) &&
2380 match(Op1, m_Neg(m_Value(B))))
2381 return new ICmpInst(I.getPredicate(), A, B);
2382
2391
23832392 if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
23842393 if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0
23852394 Value *OtherVal = A == Op1 ? B : A;
24132422 return new ICmpInst(I.getPredicate(), OtherVal,
24142423 Constant::getNullValue(A->getType()));
24152424 }
2416
2417 // (A-B) == A -> B == 0
2418 if (match(Op0, m_Sub(m_Specific(Op1), m_Value(B))))
2419 return new ICmpInst(I.getPredicate(), B,
2420 Constant::getNullValue(B->getType()));
2421
2422 // A == (A-B) -> B == 0
2423 if (match(Op1, m_Sub(m_Specific(Op0), m_Value(B))))
2424 return new ICmpInst(I.getPredicate(), B,
2425 Constant::getNullValue(B->getType()));
24262425
24272426 // (X&Z) == (Y&Z) -> (X^Y) & Z == 0
24282427 if (Op0->hasOneUse() && Op1->hasOneUse() &&
234234 }
235235
236236 ; CHECK: @test25
237 ; X + Z > Y + Z -> X > Y if there is no overflow.
237238 ; CHECK: %c = icmp sgt i32 %x, %y
238239 ; CHECK: ret i1 %c
239240 define i1 @test25(i32 %x, i32 %y, i32 %z) {
244245 }
245246
246247 ; CHECK: @test26
248 ; X + Z > Y + Z -> X > Y if there is no overflow.
249 ; CHECK: %c = icmp ugt i32 %x, %y
250 ; CHECK: ret i1 %c
251 define i1 @test26(i32 %x, i32 %y, i32 %z) {
252 %lhs = add nuw i32 %x, %z
253 %rhs = add nuw i32 %y, %z
254 %c = icmp ugt i32 %lhs, %rhs
255 ret i1 %c
256 }
257
258 ; CHECK: @test27
259 ; X - Z > Y - Z -> X > Y if there is no overflow.
247260 ; CHECK: %c = icmp sgt i32 %x, %y
248261 ; CHECK: ret i1 %c
249 define i1 @test26(i32 %x, i32 %y, i32 %z) {
262 define i1 @test27(i32 %x, i32 %y, i32 %z) {
250263 %lhs = sub nsw i32 %x, %z
251264 %rhs = sub nsw i32 %y, %z
252265 %c = icmp sgt i32 %lhs, %rhs
253266 ret i1 %c
254267 }
268
269 ; CHECK: @test28
270 ; X - Z > Y - Z -> X > Y if there is no overflow.
271 ; CHECK: %c = icmp ugt i32 %x, %y
272 ; CHECK: ret i1 %c
273 define i1 @test28(i32 %x, i32 %y, i32 %z) {
274 %lhs = sub nuw i32 %x, %z
275 %rhs = sub nuw i32 %y, %z
276 %c = icmp ugt i32 %lhs, %rhs
277 ret i1 %c
278 }
279
280 ; CHECK: @test29
281 ; X + Y > X -> Y > 0 if there is no overflow.
282 ; CHECK: %c = icmp sgt i32 %y, 0
283 ; CHECK: ret i1 %c
284 define i1 @test29(i32 %x, i32 %y) {
285 %lhs = add nsw i32 %x, %y
286 %c = icmp sgt i32 %lhs, %x
287 ret i1 %c
288 }
289
290 ; CHECK: @test30
291 ; X + Y > X -> Y > 0 if there is no overflow.
292 ; CHECK: %c = icmp ne i32 %y, 0
293 ; CHECK: ret i1 %c
294 define i1 @test30(i32 %x, i32 %y) {
295 %lhs = add nuw i32 %x, %y
296 %c = icmp ugt i32 %lhs, %x
297 ret i1 %c
298 }
299
300 ; CHECK: @test31
301 ; X > X + Y -> 0 > Y if there is no overflow.
302 ; CHECK: %c = icmp slt i32 %y, 0
303 ; CHECK: ret i1 %c
304 define i1 @test31(i32 %x, i32 %y) {
305 %rhs = add nsw i32 %x, %y
306 %c = icmp sgt i32 %x, %rhs
307 ret i1 %c
308 }
309
310 ; CHECK: @test32
311 ; X > X + Y -> 0 > Y if there is no overflow.
312 ; CHECK: ret i1 false
313 define i1 @test32(i32 %x, i32 %y) {
314 %rhs = add nuw i32 %x, %y
315 %c = icmp ugt i32 %x, %rhs
316 ret i1 %c
317 }
318
319 ; CHECK: @test33
320 ; X - Y > X -> 0 > Y if there is no overflow.
321 ; CHECK: %c = icmp slt i32 %y, 0
322 ; CHECK: ret i1 %c
323 define i1 @test33(i32 %x, i32 %y) {
324 %lhs = sub nsw i32 %x, %y
325 %c = icmp sgt i32 %lhs, %x
326 ret i1 %c
327 }
328
329 ; CHECK: @test34
330 ; X - Y > X -> 0 > Y if there is no overflow.
331 ; CHECK: ret i1 false
332 define i1 @test34(i32 %x, i32 %y) {
333 %lhs = sub nuw i32 %x, %y
334 %c = icmp ugt i32 %lhs, %x
335 ret i1 %c
336 }
337
338 ; CHECK: @test35
339 ; X > X - Y -> Y > 0 if there is no overflow.
340 ; CHECK: %c = icmp sgt i32 %y, 0
341 ; CHECK: ret i1 %c
342 define i1 @test35(i32 %x, i32 %y) {
343 %rhs = sub nsw i32 %x, %y
344 %c = icmp sgt i32 %x, %rhs
345 ret i1 %c
346 }
347
348 ; CHECK: @test36
349 ; X > X - Y -> Y > 0 if there is no overflow.
350 ; CHECK: %c = icmp ne i32 %y, 0
351 ; CHECK: ret i1 %c
352 define i1 @test36(i32 %x, i32 %y) {
353 %rhs = sub nuw i32 %x, %y
354 %c = icmp ugt i32 %x, %rhs
355 ret i1 %c
356 }
357
358 ; CHECK: @test37
359 ; X - Y > X - Z -> Z > Y if there is no overflow.
360 ; CHECK: %c = icmp sgt i32 %z, %y
361 ; CHECK: ret i1 %c
362 define i1 @test37(i32 %x, i32 %y, i32 %z) {
363 %lhs = sub nsw i32 %x, %y
364 %rhs = sub nsw i32 %x, %z
365 %c = icmp sgt i32 %lhs, %rhs
366 ret i1 %c
367 }
368
369 ; CHECK: @test38
370 ; X - Y > X - Z -> Z > Y if there is no overflow.
371 ; CHECK: %c = icmp ugt i32 %z, %y
372 ; CHECK: ret i1 %c
373 define i1 @test38(i32 %x, i32 %y, i32 %z) {
374 %lhs = sub nuw i32 %x, %y
375 %rhs = sub nuw i32 %x, %z
376 %c = icmp ugt i32 %lhs, %rhs
377 ret i1 %c
378 }
208208 %tmp5 = icmp eq i32 %tmp2, %tmp4
209209 ret i1 %tmp5
210210 ; CHECK: @test22
211 ; CHECK: %tmp5 = icmp eq i32 %a, %b
211 ; CHECK: %tmp5 = icmp eq i32 %b, %a
212212 ; CHECK: ret i1 %tmp5
213213 }
214214