summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
diff options
context:
space:
mode:
authorRoman Lebedev <lebedev.ri@gmail.com>2019-07-19 08:26:25 +0000
committerRoman Lebedev <lebedev.ri@gmail.com>2019-07-19 08:26:25 +0000
commit2ebe57386d65a60906036cb04d66c84da82c8fff (patch)
treeb9818a9614e9c212058e5da9ad223b2afd13066f /llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
parent4422a1657c661575f4ab22d43b243184f3db4461 (diff)
downloadbcm5719-llvm-2ebe57386d65a60906036cb04d66c84da82c8fff.tar.gz
bcm5719-llvm-2ebe57386d65a60906036cb04d66c84da82c8fff.zip
[InstCombine] Dropping redundant masking before left-shift [2/5] (PR42563)
Summary: If we have some pattern that leaves only some low bits set, and then performs left-shift of those bits, if none of the bits that are left after the final shift are modified by the mask, we can omit the mask. There are many variants to this pattern: c. `(x & (-1 >> MaskShAmt)) << ShiftShAmt` All these patterns can be simplified to just: `x << ShiftShAmt` iff: c. `(ShiftShAmt-MaskShAmt) s>= 0` (i.e. `ShiftShAmt u>= MaskShAmt`) alive proofs: c: https://rise4fun.com/Alive/RgJh For now let's start with patterns where both shift amounts are variable, with trivial constant "offset" between them, since i believe this is both simplest to handle and i think this is most common. But again, there are likely other variants where we could use ValueTracking/ConstantRange to handle more cases. https://bugs.llvm.org/show_bug.cgi?id=42563 Differential Revision: https://reviews.llvm.org/D64517 llvm-svn: 366537
Diffstat (limited to 'llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp')
-rw-r--r--llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp48
1 files changed, 32 insertions, 16 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
index 8ffdb661e32..b94febf786e 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -72,10 +72,12 @@ reassociateShiftAmtsOfTwoSameDirectionShifts(BinaryOperator *Sh0,
// There are many variants to this pattern:
// a) (x & ((1 << MaskShAmt) - 1)) << ShiftShAmt
// b) (x & (~(-1 << MaskShAmt))) << ShiftShAmt
+// c) (x & (-1 >> MaskShAmt)) << ShiftShAmt
// All these patterns can be simplified to just:
// x << ShiftShAmt
// iff:
// a,b) (MaskShAmt+ShiftShAmt) u>= bitwidth(x)
+// c) (ShiftShAmt-MaskShAmt) s>= 0 (i.e. ShiftShAmt u>= MaskShAmt)
static Instruction *
dropRedundantMaskingOfLeftShiftInput(BinaryOperator *OuterShift,
const SimplifyQuery &SQ) {
@@ -91,24 +93,38 @@ dropRedundantMaskingOfLeftShiftInput(BinaryOperator *OuterShift,
auto MaskA = m_Add(m_Shl(m_One(), m_Value(MaskShAmt)), m_AllOnes());
// (~(-1 << maskNbits))
auto MaskB = m_Xor(m_Shl(m_AllOnes(), m_Value(MaskShAmt)), m_AllOnes());
+ // (-1 >> MaskShAmt)
+ auto MaskC = m_Shr(m_AllOnes(), m_Value(MaskShAmt));
Value *X;
- if (!match(Masked, m_c_And(m_CombineOr(MaskA, MaskB), m_Value(X))))
- return nullptr;
-
- // Can we simplify (MaskShAmt+ShiftShAmt) ?
- Value *SumOfShAmts =
- SimplifyAddInst(MaskShAmt, ShiftShAmt, /*IsNSW=*/false, /*IsNUW=*/false,
- SQ.getWithInstruction(OuterShift));
- if (!SumOfShAmts)
- return nullptr; // Did not simplify.
- // Is the total shift amount *not* smaller than the bit width?
- // FIXME: could also rely on ConstantRange.
- unsigned BitWidth = X->getType()->getScalarSizeInBits();
- if (!match(SumOfShAmts, m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE,
- APInt(BitWidth, BitWidth))))
- return nullptr;
- // All good, we can do this fold.
+ if (match(Masked, m_c_And(m_CombineOr(MaskA, MaskB), m_Value(X)))) {
+ // Can we simplify (MaskShAmt+ShiftShAmt) ?
+ Value *SumOfShAmts =
+ SimplifyAddInst(MaskShAmt, ShiftShAmt, /*IsNSW=*/false, /*IsNUW=*/false,
+ SQ.getWithInstruction(OuterShift));
+ if (!SumOfShAmts)
+ return nullptr; // Did not simplify.
+ // Is the total shift amount *not* smaller than the bit width?
+ // FIXME: could also rely on ConstantRange.
+ unsigned BitWidth = X->getType()->getScalarSizeInBits();
+ if (!match(SumOfShAmts, m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE,
+ APInt(BitWidth, BitWidth))))
+ return nullptr;
+ // All good, we can do this fold.
+ } else if (match(Masked, m_c_And(MaskC, m_Value(X)))) {
+ // Can we simplify (ShiftShAmt-MaskShAmt) ?
+ Value *ShAmtsDiff =
+ SimplifySubInst(ShiftShAmt, MaskShAmt, /*IsNSW=*/false, /*IsNUW=*/false,
+ SQ.getWithInstruction(OuterShift));
+ if (!ShAmtsDiff)
+ return nullptr; // Did not simplify.
+ // Is the difference non-negative? (is ShiftShAmt u>= MaskShAmt ?)
+ // FIXME: could also rely on ConstantRange.
+ if (!match(ShAmtsDiff, m_NonNegative()))
+ return nullptr;
+ // All good, we can do this fold.
+ } else
+ return nullptr; // Don't know anything about this pattern.
// No 'NUW'/'NSW'!
// We no longer know that we won't shift-out non-0 bits.
OpenPOWER on IntegriCloud