| OLD | NEW |
| 1 //===- InstCombineCompares.cpp --------------------------------------------===// | 1 //===- InstCombineCompares.cpp --------------------------------------------===// |
| 2 // | 2 // |
| 3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 // | 9 // |
| 10 // This file implements the visitICmp and visitFCmp functions. | 10 // This file implements the visitICmp and visitFCmp functions. |
| 11 // | 11 // |
| 12 //===----------------------------------------------------------------------===// | 12 //===----------------------------------------------------------------------===// |
| 13 | 13 |
| 14 #include "InstCombine.h" | 14 #include "InstCombine.h" |
| 15 #include "llvm/ADT/Statistic.h" | 15 #include "llvm/ADT/Statistic.h" |
| 16 #include "llvm/ADT/Triple.h" // @LOCALMOD |
| 16 #include "llvm/Analysis/ConstantFolding.h" | 17 #include "llvm/Analysis/ConstantFolding.h" |
| 17 #include "llvm/Analysis/InstructionSimplify.h" | 18 #include "llvm/Analysis/InstructionSimplify.h" |
| 18 #include "llvm/Analysis/MemoryBuiltins.h" | 19 #include "llvm/Analysis/MemoryBuiltins.h" |
| 19 #include "llvm/IR/ConstantRange.h" | 20 #include "llvm/IR/ConstantRange.h" |
| 20 #include "llvm/IR/DataLayout.h" | 21 #include "llvm/IR/DataLayout.h" |
| 21 #include "llvm/IR/GetElementPtrTypeIterator.h" | 22 #include "llvm/IR/GetElementPtrTypeIterator.h" |
| 22 #include "llvm/IR/IntrinsicInst.h" | 23 #include "llvm/IR/IntrinsicInst.h" |
| 23 #include "llvm/IR/PatternMatch.h" | 24 #include "llvm/IR/PatternMatch.h" |
| 24 #include "llvm/Support/CommandLine.h" | 25 #include "llvm/Support/CommandLine.h" |
| 25 #include "llvm/Support/Debug.h" | 26 #include "llvm/Support/Debug.h" |
| (...skipping 1602 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1628 } | 1629 } |
| 1629 | 1630 |
| 1630 // Transform (icmp pred iM (shl iM %v, N), CI) | 1631 // Transform (icmp pred iM (shl iM %v, N), CI) |
| 1631 // -> (icmp pred i(M-N) (trunc %v iM to i(M-N)), (trunc (CI>>N)) | 1632 // -> (icmp pred i(M-N) (trunc %v iM to i(M-N)), (trunc (CI>>N)) |
| 1632 // Transform the shl to a trunc if (trunc (CI>>N)) has no loss and M-N. | 1633 // Transform the shl to a trunc if (trunc (CI>>N)) has no loss and M-N. |
| 1633 // This enables to get rid of the shift in favor of a trunc which can be | 1634 // This enables to get rid of the shift in favor of a trunc which can be |
| 1634 // free on the target. It has the additional benefit of comparing to a | 1635 // free on the target. It has the additional benefit of comparing to a |
| 1635 // smaller constant, which will be target friendly. | 1636 // smaller constant, which will be target friendly. |
| 1636 unsigned Amt = ShAmt->getLimitedValue(TypeBits-1); | 1637 unsigned Amt = ShAmt->getLimitedValue(TypeBits-1); |
| 1637 if (LHSI->hasOneUse() && | 1638 if (LHSI->hasOneUse() && |
| 1639 // @LOCALMOD-BEGIN |
| 1640 // We don't want to introduce non-power-of-two integer sizes for PNaCl's |
| 1641 // stable wire format, so modify this transformation for NaCl. |
| 1642 isPowerOf2_32(TypeBits - Amt) && (TypeBits - Amt) >= 8 && |
| 1643 // @LOCALMOD-END |
| 1638 Amt != 0 && RHSV.countTrailingZeros() >= Amt) { | 1644 Amt != 0 && RHSV.countTrailingZeros() >= Amt) { |
| 1639 Type *NTy = IntegerType::get(ICI.getContext(), TypeBits - Amt); | 1645 Type *NTy = IntegerType::get(ICI.getContext(), TypeBits - Amt); |
| 1640 Constant *NCI = ConstantExpr::getTrunc( | 1646 Constant *NCI = ConstantExpr::getTrunc( |
| 1641 ConstantExpr::getAShr(RHS, | 1647 ConstantExpr::getAShr(RHS, |
| 1642 ConstantInt::get(RHS->getType(), Amt)), | 1648 ConstantInt::get(RHS->getType(), Amt)), |
| 1643 NTy); | 1649 NTy); |
| 1644 return new ICmpInst(ICI.getPredicate(), | 1650 return new ICmpInst(ICI.getPredicate(), |
| 1645 Builder->CreateTrunc(LHSI->getOperand(0), NTy), | 1651 Builder->CreateTrunc(LHSI->getOperand(0), NTy), |
| 1646 NCI); | 1652 NCI); |
| 1647 } | 1653 } |
| (...skipping 1022 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2670 // addition in wider type, and explicitly checks for overflow using | 2676 // addition in wider type, and explicitly checks for overflow using |
| 2671 // comparisons against INT_MIN and INT_MAX. Simplify this by using the | 2677 // comparisons against INT_MIN and INT_MAX. Simplify this by using the |
| 2672 // sadd_with_overflow intrinsic. | 2678 // sadd_with_overflow intrinsic. |
| 2673 // | 2679 // |
| 2674 // TODO: This could probably be generalized to handle other overflow-safe | 2680 // TODO: This could probably be generalized to handle other overflow-safe |
| 2675 // operations if we worked out the formulas to compute the appropriate | 2681 // operations if we worked out the formulas to compute the appropriate |
| 2676 // magic constants. | 2682 // magic constants. |
| 2677 // | 2683 // |
| 2678 // sum = a + b | 2684 // sum = a + b |
| 2679 // if (sum+128 >u 255) ... -> llvm.sadd.with.overflow.i8 | 2685 // if (sum+128 >u 255) ... -> llvm.sadd.with.overflow.i8 |
| 2686 // @LOCALMOD-BEGIN |
| 2687 // This is disabled for PNaCl, because we don't support the |
| 2688 // with.overflow intrinsics in PNaCl's stable ABI. |
| 2689 Triple T(I.getParent()->getParent()->getParent()->getTargetTriple()); |
| 2690 if (T.getArch() != Triple::le32) |
| 2691 // @LOCALMOD-END |
| 2680 { | 2692 { |
| 2681 ConstantInt *CI2; // I = icmp ugt (add (add A, B), CI2), CI | 2693 ConstantInt *CI2; // I = icmp ugt (add (add A, B), CI2), CI |
| 2682 if (I.getPredicate() == ICmpInst::ICMP_UGT && | 2694 if (I.getPredicate() == ICmpInst::ICMP_UGT && |
| 2683 match(Op0, m_Add(m_Add(m_Value(A), m_Value(B)), m_ConstantInt(CI2)))) | 2695 match(Op0, m_Add(m_Add(m_Value(A), m_Value(B)), m_ConstantInt(CI2)))) |
| 2684 if (Instruction *Res = ProcessUGT_ADDCST_ADD(I, A, B, CI2, CI, *this)) | 2696 if (Instruction *Res = ProcessUGT_ADDCST_ADD(I, A, B, CI2, CI, *this)) |
| 2685 return Res; | 2697 return Res; |
| 2686 } | 2698 } |
| 2687 | 2699 |
| 2688 // (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B) | 2700 // (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B) |
| 2689 if (I.isEquality() && CI->isZero() && | 2701 if (I.isEquality() && CI->isZero() && |
| (...skipping 720 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3410 | 3422 |
| 3411 // ~x < ~y --> y < x | 3423 // ~x < ~y --> y < x |
| 3412 // ~x < cst --> ~cst < x | 3424 // ~x < cst --> ~cst < x |
| 3413 if (match(Op0, m_Not(m_Value(A)))) { | 3425 if (match(Op0, m_Not(m_Value(A)))) { |
| 3414 if (match(Op1, m_Not(m_Value(B)))) | 3426 if (match(Op1, m_Not(m_Value(B)))) |
| 3415 return new ICmpInst(I.getPredicate(), B, A); | 3427 return new ICmpInst(I.getPredicate(), B, A); |
| 3416 if (ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1)) | 3428 if (ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1)) |
| 3417 return new ICmpInst(I.getPredicate(), ConstantExpr::getNot(RHSC), A); | 3429 return new ICmpInst(I.getPredicate(), ConstantExpr::getNot(RHSC), A); |
| 3418 } | 3430 } |
| 3419 | 3431 |
| 3432 // @LOCALMOD-BEGIN |
| 3433 // This is disabled for PNaCl, because we don't support the |
| 3434 // with.overflow intrinsics in PNaCl's stable ABI. |
| 3435 Triple T(I.getParent()->getParent()->getParent()->getTargetTriple()); |
| 3436 if (T.getArch() != Triple::le32) { |
| 3420 // (a+b) <u a --> llvm.uadd.with.overflow. | 3437 // (a+b) <u a --> llvm.uadd.with.overflow. |
| 3421 // (a+b) <u b --> llvm.uadd.with.overflow. | 3438 // (a+b) <u b --> llvm.uadd.with.overflow. |
| 3422 if (I.getPredicate() == ICmpInst::ICMP_ULT && | 3439 if (I.getPredicate() == ICmpInst::ICMP_ULT && |
| 3423 match(Op0, m_Add(m_Value(A), m_Value(B))) && | 3440 match(Op0, m_Add(m_Value(A), m_Value(B))) && |
| 3424 (Op1 == A || Op1 == B)) | 3441 (Op1 == A || Op1 == B)) |
| 3425 if (Instruction *R = ProcessUAddIdiom(I, Op0, *this)) | 3442 if (Instruction *R = ProcessUAddIdiom(I, Op0, *this)) |
| 3426 return R; | 3443 return R; |
| 3427 | 3444 |
| 3428 // a >u (a+b) --> llvm.uadd.with.overflow. | 3445 // a >u (a+b) --> llvm.uadd.with.overflow. |
| 3429 // b >u (a+b) --> llvm.uadd.with.overflow. | 3446 // b >u (a+b) --> llvm.uadd.with.overflow. |
| 3430 if (I.getPredicate() == ICmpInst::ICMP_UGT && | 3447 if (I.getPredicate() == ICmpInst::ICMP_UGT && |
| 3431 match(Op1, m_Add(m_Value(A), m_Value(B))) && | 3448 match(Op1, m_Add(m_Value(A), m_Value(B))) && |
| 3432 (Op0 == A || Op0 == B)) | 3449 (Op0 == A || Op0 == B)) |
| 3433 if (Instruction *R = ProcessUAddIdiom(I, Op1, *this)) | 3450 if (Instruction *R = ProcessUAddIdiom(I, Op1, *this)) |
| 3434 return R; | 3451 return R; |
| 3435 | 3452 |
| 3436 // (zext a) * (zext b) --> llvm.umul.with.overflow. | 3453 // (zext a) * (zext b) --> llvm.umul.with.overflow. |
| 3437 if (match(Op0, m_Mul(m_ZExt(m_Value(A)), m_ZExt(m_Value(B))))) { | 3454 if (match(Op0, m_Mul(m_ZExt(m_Value(A)), m_ZExt(m_Value(B))))) { |
| 3438 if (Instruction *R = ProcessUMulZExtIdiom(I, Op0, Op1, *this)) | 3455 if (Instruction *R = ProcessUMulZExtIdiom(I, Op0, Op1, *this)) |
| 3439 return R; | 3456 return R; |
| 3440 } | 3457 } |
| 3441 if (match(Op1, m_Mul(m_ZExt(m_Value(A)), m_ZExt(m_Value(B))))) { | 3458 if (match(Op1, m_Mul(m_ZExt(m_Value(A)), m_ZExt(m_Value(B))))) { |
| 3442 if (Instruction *R = ProcessUMulZExtIdiom(I, Op1, Op0, *this)) | 3459 if (Instruction *R = ProcessUMulZExtIdiom(I, Op1, Op0, *this)) |
| 3443 return R; | 3460 return R; |
| 3444 } | 3461 } |
| 3462 } |
| 3463 // @LOCALMOD-END |
| 3445 } | 3464 } |
| 3446 | 3465 |
| 3447 if (I.isEquality()) { | 3466 if (I.isEquality()) { |
| 3448 Value *A, *B, *C, *D; | 3467 Value *A, *B, *C, *D; |
| 3449 | 3468 |
| 3450 if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) { | 3469 if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
| 3451 if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0 | 3470 if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0 |
| 3452 Value *OtherVal = A == Op1 ? B : A; | 3471 Value *OtherVal = A == Op1 ? B : A; |
| 3453 return new ICmpInst(I.getPredicate(), OtherVal, | 3472 return new ICmpInst(I.getPredicate(), OtherVal, |
| 3454 Constant::getNullValue(A->getType())); | 3473 Constant::getNullValue(A->getType())); |
| (...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3958 | 3977 |
| 3959 // fcmp (fpext x), (fpext y) -> fcmp x, y | 3978 // fcmp (fpext x), (fpext y) -> fcmp x, y |
| 3960 if (FPExtInst *LHSExt = dyn_cast<FPExtInst>(Op0)) | 3979 if (FPExtInst *LHSExt = dyn_cast<FPExtInst>(Op0)) |
| 3961 if (FPExtInst *RHSExt = dyn_cast<FPExtInst>(Op1)) | 3980 if (FPExtInst *RHSExt = dyn_cast<FPExtInst>(Op1)) |
| 3962 if (LHSExt->getSrcTy() == RHSExt->getSrcTy()) | 3981 if (LHSExt->getSrcTy() == RHSExt->getSrcTy()) |
| 3963 return new FCmpInst(I.getPredicate(), LHSExt->getOperand(0), | 3982 return new FCmpInst(I.getPredicate(), LHSExt->getOperand(0), |
| 3964 RHSExt->getOperand(0)); | 3983 RHSExt->getOperand(0)); |
| 3965 | 3984 |
| 3966 return Changed ? &I : nullptr; | 3985 return Changed ? &I : nullptr; |
| 3967 } | 3986 } |
| OLD | NEW |