[InstCombine] fix names in canEvaluateShiftedShift(); NFC

It's not clear what 'First' and 'Second' mean, so use 'Inner' and 'Outer'
to match foldShiftedShift() and add comments with formulas, so it's easier
to see what's going on.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@292153 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Sanjay Patel 2017-01-16 20:05:26 +00:00
parent 2222ec4bc9
commit 627c8074e7

View File

@ -65,44 +65,43 @@ Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) {
} }
/// Return true if we can simplify two logical (either left or right) shifts /// Return true if we can simplify two logical (either left or right) shifts
/// that have constant shift amounts. /// that have constant shift amounts: OuterShift (InnerShift X, C1), C2.
static bool canEvaluateShiftedShift(unsigned FirstShiftAmt, static bool canEvaluateShiftedShift(unsigned OuterShAmt, bool IsOuterShl,
bool IsFirstShiftLeft, Instruction *InnerShift, InstCombiner &IC,
Instruction *SecondShift, InstCombiner &IC,
Instruction *CxtI) { Instruction *CxtI) {
assert(SecondShift->isLogicalShift() && "Unexpected instruction type"); assert(InnerShift->isLogicalShift() && "Unexpected instruction type");
// We need constant scalar or constant splat shifts. // We need constant scalar or constant splat shifts.
const APInt *SecondShiftConst; const APInt *InnerShiftConst;
if (!match(SecondShift->getOperand(1), m_APInt(SecondShiftConst))) if (!match(InnerShift->getOperand(1), m_APInt(InnerShiftConst)))
return false; return false;
unsigned SecondShiftAmt = SecondShiftConst->getZExtValue(); // Two logical shifts in the same direction:
bool IsSecondShiftLeft = SecondShift->getOpcode() == Instruction::Shl; // shl (shl X, C1), C2 --> shl X, C1 + C2
// lshr (lshr X, C1), C2 --> lshr X, C1 + C2
// We can always fold shl(c1) + shl(c2) -> shl(c1+c2). bool IsInnerShl = InnerShift->getOpcode() == Instruction::Shl;
// We can always fold lshr(c1) + lshr(c2) -> lshr(c1+c2). if (IsInnerShl == IsOuterShl)
if (IsFirstShiftLeft == IsSecondShiftLeft)
return true; return true;
// We can always fold lshr(c) + shl(c) -> and(c2). // Equal shift amounts in opposite directions become bitwise 'and':
// We can always fold shl(c) + lshr(c) -> and(c2). // lshr (shl X, C), C --> and X, C'
if (FirstShiftAmt == SecondShiftAmt) // shl (lshr X, C), C --> and X, C'
unsigned InnerShAmt = InnerShiftConst->getZExtValue();
if (InnerShAmt == OuterShAmt)
return true; return true;
unsigned TypeWidth = SecondShift->getType()->getScalarSizeInBits();
// If the 2nd shift is bigger than the 1st, we can fold: // If the 2nd shift is bigger than the 1st, we can fold:
// lshr(c1) + shl(c2) -> shl(c3) + and(c4) or // lshr (shl X, C1), C2 --> and (shl X, C1 - C2), C3
// shl(c1) + lshr(c2) -> lshr(c3) + and(c4), // shl (lshr X, C1), C2 --> and (lshr X, C1 - C2), C3
// but it isn't profitable unless we know the and'd out bits are already zero. // but it isn't profitable unless we know the and'd out bits are already zero.
// Also check that the 2nd shift is valid (less than the type width) or we'll // Also, check that the inner shift is valid (less than the type width) or
// crash trying to produce the bit mask for the 'and'. // we'll crash trying to produce the bit mask for the 'and'.
if (SecondShiftAmt > FirstShiftAmt && SecondShiftAmt < TypeWidth) { unsigned TypeWidth = InnerShift->getType()->getScalarSizeInBits();
unsigned MaskShift = IsSecondShiftLeft ? TypeWidth - SecondShiftAmt if (InnerShAmt > OuterShAmt && InnerShAmt < TypeWidth) {
: SecondShiftAmt - FirstShiftAmt; unsigned MaskShift =
APInt Mask = APInt::getLowBitsSet(TypeWidth, FirstShiftAmt) << MaskShift; IsInnerShl ? TypeWidth - InnerShAmt : InnerShAmt - OuterShAmt;
if (IC.MaskedValueIsZero(SecondShift->getOperand(0), Mask, 0, CxtI)) APInt Mask = APInt::getLowBitsSet(TypeWidth, OuterShAmt) << MaskShift;
if (IC.MaskedValueIsZero(InnerShift->getOperand(0), Mask, 0, CxtI))
return true; return true;
} }