llvm.org GIT mirror llvm / 6dc9e2b
Reapply commit 143214 with a fix: m_ICmp doesn't match conditions with the given predicate, it matches any condition and returns the predicate - d'oh! Original commit message: The expression icmp eq (select (icmp eq x, 0), 1, x), 0 folds to false. Spotted by my super-optimizer in 186.crafty and 450.soplex. We really need a proper infrastructure for handling generalizations of this kind of thing (which occur a lot), however this case is so simple that I decided to go ahead and implement it directly. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@143318 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan Sands 7 years ago
2 changed file(s) with 84 addition(s) and 30 deletion(s). Raw diff Collapse all Expand all
6565 cast(Ty)->getElementType()->isIntegerTy(1))) &&
6666 "Expected i1 type or a vector of i1!");
6767 return Constant::getAllOnesValue(Ty);
68 }
69
70 /// isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"?
71 static bool isSameCompare(Value *V, CmpInst::Predicate Pred, Value *LHS,
72 Value *RHS) {
73 CmpInst *Cmp = dyn_cast(V);
74 if (!Cmp)
75 return false;
76 CmpInst::Predicate CPred = Cmp->getPredicate();
77 Value *CLHS = Cmp->getOperand(0), *CRHS = Cmp->getOperand(1);
78 if (CPred == Pred && CLHS == LHS && CRHS == RHS)
79 return true;
80 return CPred == CmpInst::getSwappedPredicate(Pred) && CLHS == RHS &&
81 CRHS == LHS;
6882 }
6983
7084 /// ValueDominatesPHI - Does the given value dominate the specified phi node?
415429 }
416430 assert(isa(LHS) && "Not comparing with a select instruction!");
417431 SelectInst *SI = cast(LHS);
432 Value *Cond = SI->getCondition();
433 Value *TV = SI->getTrueValue();
434 Value *FV = SI->getFalseValue();
418435
419436 // Now that we have "cmp select(Cond, TV, FV), RHS", analyse it.
420437 // Does "cmp TV, RHS" simplify?
421 if (Value *TCmp = SimplifyCmpInst(Pred, SI->getTrueValue(), RHS, TD, DT,
422 MaxRecurse)) {
423 // It does! Does "cmp FV, RHS" simplify?
424 if (Value *FCmp = SimplifyCmpInst(Pred, SI->getFalseValue(), RHS, TD, DT,
425 MaxRecurse)) {
426 // It does! If they simplified to the same value, then use it as the
427 // result of the original comparison.
428 if (TCmp == FCmp)
429 return TCmp;
430 Value *Cond = SI->getCondition();
431 // If the false value simplified to false, then the result of the compare
432 // is equal to "Cond && TCmp". This also catches the case when the false
433 // value simplified to false and the true value to true, returning "Cond".
434 if (match(FCmp, m_Zero()))
435 if (Value *V = SimplifyAndInst(Cond, TCmp, TD, DT, MaxRecurse))
436 return V;
437 // If the true value simplified to true, then the result of the compare
438 // is equal to "Cond || FCmp".
439 if (match(TCmp, m_One()))
440 if (Value *V = SimplifyOrInst(Cond, FCmp, TD, DT, MaxRecurse))
441 return V;
442 // Finally, if the false value simplified to true and the true value to
443 // false, then the result of the compare is equal to "!Cond".
444 if (match(FCmp, m_One()) && match(TCmp, m_Zero()))
445 if (Value *V =
446 SimplifyXorInst(Cond, Constant::getAllOnesValue(Cond->getType()),
447 TD, DT, MaxRecurse))
448 return V;
449 }
450 }
438 Value *TCmp = SimplifyCmpInst(Pred, TV, RHS, TD, DT, MaxRecurse);
439 if (TCmp == Cond) {
440 // It not only simplified, it simplified to the select condition. Replace
441 // it with 'true'.
442 TCmp = getTrue(Cond->getType());
443 } else if (!TCmp) {
444 // It didn't simplify. However if "cmp TV, RHS" is equal to the select
445 // condition then we can replace it with 'true'. Otherwise give up.
446 if (!isSameCompare(Cond, Pred, TV, RHS))
447 return 0;
448 TCmp = getTrue(Cond->getType());
449 }
450
451 // Does "cmp FV, RHS" simplify?
452 Value *FCmp = SimplifyCmpInst(Pred, FV, RHS, TD, DT, MaxRecurse);
453 if (FCmp == Cond) {
454 // It not only simplified, it simplified to the select condition. Replace
455 // it with 'false'.
456 FCmp = getFalse(Cond->getType());
457 } else if (!FCmp) {
458 // It didn't simplify. However if "cmp FV, RHS" is equal to the select
459 // condition then we can replace it with 'false'. Otherwise give up.
460 if (!isSameCompare(Cond, Pred, FV, RHS))
461 return 0;
462 FCmp = getFalse(Cond->getType());
463 }
464
465 // If both sides simplified to the same value, then use it as the result of
466 // the original comparison.
467 if (TCmp == FCmp)
468 return TCmp;
469 // If the false value simplified to false, then the result of the compare
470 // is equal to "Cond && TCmp". This also catches the case when the false
471 // value simplified to false and the true value to true, returning "Cond".
472 if (match(FCmp, m_Zero()))
473 if (Value *V = SimplifyAndInst(Cond, TCmp, TD, DT, MaxRecurse))
474 return V;
475 // If the true value simplified to true, then the result of the compare
476 // is equal to "Cond || FCmp".
477 if (match(TCmp, m_One()))
478 if (Value *V = SimplifyOrInst(Cond, FCmp, TD, DT, MaxRecurse))
479 return V;
480 // Finally, if the false value simplified to true and the true value to
481 // false, then the result of the compare is equal to "!Cond".
482 if (match(FCmp, m_One()) && match(TCmp, m_Zero()))
483 if (Value *V =
484 SimplifyXorInst(Cond, Constant::getAllOnesValue(Cond->getType()),
485 TD, DT, MaxRecurse))
486 return V;
451487
452488 return 0;
453489 }
201201 %c = icmp ne i32 %s, 0
202202 ret i1 %c
203203 ; CHECK: ret i1 %cond
204 }
205
206 define i1 @select5(i32 %x) {
207 ; CHECK: @select5
208 %c = icmp eq i32 %x, 0
209 %s = select i1 %c, i32 1, i32 %x
210 %c2 = icmp eq i32 %s, 0
211 ret i1 %c2
212 ; CHECK: ret i1 false
213 }
214
215 define i1 @select6(i32 %x) {
216 ; CHECK: @select6
217 %c = icmp sgt i32 %x, 0
218 %s = select i1 %c, i32 %x, i32 4
219 %c2 = icmp eq i32 %s, 0
220 ret i1 %c2
221 ; CHECK: ret i1 %c2
204222 }
205223
206224 define i1 @urem1(i32 %X, i32 %Y) {