diff options
author | Craig Topper <craig.topper@intel.com> | 2017-09-01 21:27:34 +0000 |
---|---|---|
committer | Craig Topper <craig.topper@intel.com> | 2017-09-01 21:27:34 +0000 |
commit | 924f20262ba939ca4e14b2cba59cc9c95a9c5e09 (patch) | |
tree | 1a2f19315c7268d43e8e03c1350f993405509497 /llvm/lib | |
parent | d3b465606ab4d6f3966c110ad0626f162aaf5c97 (diff) | |
download | bcm5719-llvm-924f20262ba939ca4e14b2cba59cc9c95a9c5e09.tar.gz bcm5719-llvm-924f20262ba939ca4e14b2cba59cc9c95a9c5e09.zip |
[InstCombine][InstSimplify] Teach decomposeBitTestICmp to look through truncate instructions
This patch teaches decomposeBitTestICmp to look through truncate instructions on the input to the compare. If a truncate is found it will now return the pre-truncated Value and appropriately extend the APInt mask.
This allows some code to be removed from InstSimplify that was doing this functionality.
This allows InstCombine's bit test combining code to match a pre-truncate Value with the same Value appear with an 'and' on another icmp. Or it allows us to combine a truncate to i16 and a truncate to i8. This also required removing the type check from the beginning of getMaskedTypeForICmpPair, but I believe that's ok because we still have to find two values from the input to each icmp that are equal before we'll do any transformation. So the type check was really just serving as an early out.
There was one user of decomposeBitTestICmp that didn't want to look through truncates, so I've added a flag to prevent that behavior when necessary.
Differential Revision: https://reviews.llvm.org/D37158
llvm-svn: 312382
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/Analysis/CmpInstAnalysis.cpp | 13 | ||||
-rw-r--r-- | llvm/lib/Analysis/InstructionSimplify.cpp | 13 | ||||
-rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp | 2 |
3 files changed, 11 insertions, 17 deletions
diff --git a/llvm/lib/Analysis/CmpInstAnalysis.cpp b/llvm/lib/Analysis/CmpInstAnalysis.cpp index c879b8729dc..159c1a2d135 100644 --- a/llvm/lib/Analysis/CmpInstAnalysis.cpp +++ b/llvm/lib/Analysis/CmpInstAnalysis.cpp @@ -66,9 +66,11 @@ bool llvm::PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) { bool llvm::decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate &Pred, - Value *&X, APInt &Mask) { + Value *&X, APInt &Mask, bool LookThruTrunc) { + using namespace PatternMatch; + const APInt *C; - if (!match(RHS, PatternMatch::m_APInt(C))) + if (!match(RHS, m_APInt(C))) return false; switch (Pred) { @@ -132,6 +134,11 @@ bool llvm::decomposeBitTestICmp(Value *LHS, Value *RHS, break; } - X = LHS; + if (LookThruTrunc && match(LHS, m_Trunc(m_Value(X)))) { + Mask = Mask.zext(X->getType()->getScalarSizeInBits()); + } else { + X = LHS; + } + return true; } diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index d155f6b4803..94d71817049 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -3629,19 +3629,6 @@ static Value *simplifySelectWithFakeICmpEq(Value *CmpLHS, Value *CmpRHS, if (!decomposeBitTestICmp(CmpLHS, CmpRHS, Pred, X, Mask)) return nullptr; - unsigned BitWidth = TrueVal->getType()->getScalarSizeInBits(); - if (!BitWidth) - return nullptr; - - Value *ExtX; - if (match(X, m_Trunc(m_Value(ExtX))) && - (ExtX == TrueVal || ExtX == FalseVal)) { - // icmp slt (trunc X), 0 <--> icmp ne (and X, C), 0 - // icmp sgt (trunc X), -1 <--> icmp eq (and X, C), 0 - X = ExtX; - Mask = Mask.zext(BitWidth); - } - return simplifySelectBitTest(TrueVal, FalseVal, X, &Mask, Pred == ICmpInst::ICMP_EQ); } diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp index 11602ab101e..83aeda5935e 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -724,7 +724,7 @@ Instruction *InstCombiner::foldSelectInstWithICmp(SelectInst &SI, ICmpInst::Predicate Pred = ICI->getPredicate(); Value *X; APInt Mask; - if (decomposeBitTestICmp(CmpLHS, CmpRHS, Pred, X, Mask)) { + if (decomposeBitTestICmp(CmpLHS, CmpRHS, Pred, X, Mask, false)) { if (Mask.isSignMask()) { assert(X == CmpLHS && "Expected to use the compare input directly"); assert(ICmpInst::isEquality(Pred) && "Expected equality predicate"); |