diff options
author | Roman Lebedev <lebedev.ri@gmail.com> | 2019-09-23 17:04:28 +0000 |
---|---|---|
committer | Roman Lebedev <lebedev.ri@gmail.com> | 2019-09-23 17:04:28 +0000 |
commit | 0a51e1f66dd2fef772cc83c565e4319f0e017a43 (patch) | |
tree | 556d48b679b503a4a0ceb8e42d47aa4cf9fb80a1 /llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp | |
parent | b4a1d8a84ce0a825cb31f602e11913d06f69faf2 (diff) | |
download | bcm5719-llvm-0a51e1f66dd2fef772cc83c565e4319f0e017a43.tar.gz bcm5719-llvm-0a51e1f66dd2fef772cc83c565e4319f0e017a43.zip |
[InstCombine] dropRedundantMaskingOfLeftShiftInput(): pat. c/d/e with mask (PR42563)
Summary:
If we have a pattern `(x & (-1 >> maskNbits)) << shiftNbits`,
we already know (have a fold) that will drop the `& (-1 >> maskNbits)`
mask iff `(shiftNbits-maskNbits) s>= 0` (i.e. `shiftNbits u>= maskNbits`).
So even if `(shiftNbits-maskNbits) s< 0`, we can still
fold, we will just need to apply a **constant** mask afterwards:
```
Name: c, normal+mask
%t0 = lshr i32 -1, C1
%t1 = and i32 %t0, %x
%r = shl i32 %t1, C2
=>
%n0 = shl i32 %x, C2
%n1 = i32 ((-(C2-C1))+32)
%n2 = zext i32 %n1 to i64
%n3 = lshr i64 -1, %n2
%n4 = trunc i64 %n3 to i32
%r = and i32 %n0, %n4
```
https://rise4fun.com/Alive/gslRa
Naturally, old `%masked` will have to be one-use.
This is not valid for pattern f - where "masking" is done via `ashr`.
https://bugs.llvm.org/show_bug.cgi?id=42563
Reviewers: spatel, nikic, xbolva00
Reviewed By: spatel
Subscribers: hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D67725
llvm-svn: 372630
Diffstat (limited to 'llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp')
-rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp index 3f466495c5e..2dea4c45cbd 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp @@ -202,10 +202,35 @@ dropRedundantMaskingOfLeftShiftInput(BinaryOperator *OuterShift, // shall be unset in the root value (OuterShift). If ShAmtsDiff is negative, // we'll need to also produce a mask to unset ShAmtsDiff high bits. // So, does *any* channel need a mask? (is ShiftShAmt u>= MaskShAmt ?) - if (!match(ShAmtsDiff, m_NonNegative())) - return nullptr; // FIXME. + if (!match(ShAmtsDiff, m_NonNegative())) { + // This sub-fold (with mask) is invalid for 'ashr' "masking" instruction. + if (match(Masked, m_AShr(m_Value(), m_Value()))) + return nullptr; + // For a mask we need to get rid of old masking instruction. + if (!Masked->hasOneUse()) + return nullptr; // Else we can't perform the fold. + Type *Ty = X->getType(); + unsigned BitWidth = Ty->getScalarSizeInBits(); + // We should produce compute the mask in wider type, and truncate later! + // Get type twice as wide element-wise (same number of elements!). + Type *ExtendedScalarTy = Type::getIntNTy(Ty->getContext(), 2 * BitWidth); + Type *ExtendedTy = + Ty->isVectorTy() + ? VectorType::get(ExtendedScalarTy, Ty->getVectorNumElements()) + : ExtendedScalarTy; + auto *ExtendedNumHighBitsToClear = ConstantExpr::getZExt( + ConstantExpr::getAdd( + ConstantExpr::getNeg(ShAmtsDiff), + ConstantInt::get(Ty, BitWidth, /*isSigned=*/false)), + ExtendedTy); + // And compute the mask as usual: (-1 l>> (ShAmtsDiff)) + auto *ExtendedAllOnes = ConstantExpr::getAllOnesValue(ExtendedTy); + auto *ExtendedMask = + ConstantExpr::getLShr(ExtendedAllOnes, ExtendedNumHighBitsToClear); + NewMask = ConstantExpr::getTrunc(ExtendedMask, Ty); + } else + NewMask = nullptr; // No mask needed. // All good, we can do this fold. - NewMask = nullptr; // No mask needed. } else return nullptr; // Don't know anything about this pattern. |