[llvm] r334372 - [InstCombine] Fold (x >> y) << y -> x & (-1 << y)
Roman Lebedev via llvm-commits
llvm-commits at lists.llvm.org
Sun Jun 10 13:10:13 PDT 2018
Author: lebedevri
Date: Sun Jun 10 13:10:13 2018
New Revision: 334372
URL: http://llvm.org/viewvc/llvm-project?rev=334372&view=rev
Log:
[InstCombine] Fold (x >> y) << y -> x & (-1 << y)
Summary:
We already do it for matching splat constants, but not just values.
Further improvements for non-matching splat constants, as noted in
https://reviews.llvm.org/D46760#1123713 will be needed,
but i'd prefer to do that as a follow-up.
https://bugs.llvm.org/show_bug.cgi?id=37603
https://rise4fun.com/Alive/cplX
https://rise4fun.com/Alive/0HF
Reviewers: spatel, craig.topper
Reviewed By: spatel
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D47981
Modified:
llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp
llvm/trunk/test/Transforms/InstCombine/canonicalize-ashr-shl-to-masking.ll
llvm/trunk/test/Transforms/InstCombine/canonicalize-lshr-shl-to-masking.ll
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp?rev=334372&r1=334371&r2=334372&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp Sun Jun 10 13:10:13 2018
@@ -600,11 +600,11 @@ Instruction *InstCombiner::visitShl(Bina
if (Instruction *V = commonShiftTransforms(I))
return V;
+ Type *Ty = I.getType();
const APInt *ShAmtAPInt;
if (match(Op1, m_APInt(ShAmtAPInt))) {
unsigned ShAmt = ShAmtAPInt->getZExtValue();
unsigned BitWidth = I.getType()->getScalarSizeInBits();
- Type *Ty = I.getType();
// shl (zext X), ShAmt --> zext (shl X, ShAmt)
// This is only valid if X would have zeros shifted out.
@@ -670,6 +670,15 @@ Instruction *InstCombiner::visitShl(Bina
}
}
+ // Transform (x >> y) << y to x & (-1 << y)
+ // Valid for any type of right-shift.
+ Value *X;
+ if (match(Op0, m_OneUse(m_Shr(m_Value(X), m_Specific(Op1))))) {
+ Constant *AllOnes = ConstantInt::getAllOnesValue(Ty);
+ Value *Mask = Builder.CreateShl(AllOnes, Op1);
+ return BinaryOperator::CreateAnd(Mask, X);
+ }
+
Constant *C1;
if (match(Op1, m_Constant(C1))) {
Constant *C2;
Modified: llvm/trunk/test/Transforms/InstCombine/canonicalize-ashr-shl-to-masking.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/canonicalize-ashr-shl-to-masking.ll?rev=334372&r1=334371&r2=334372&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/canonicalize-ashr-shl-to-masking.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/canonicalize-ashr-shl-to-masking.ll Sun Jun 10 13:10:13 2018
@@ -15,8 +15,8 @@
define i32 @positive_samevar(i32 %x, i32 %y) {
; CHECK-LABEL: @positive_samevar(
-; CHECK-NEXT: [[TMP0:%.*]] = ashr i32 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT: [[RET:%.*]] = shl i32 [[TMP0]], [[Y]]
+; CHECK-NEXT: [[TMP1:%.*]] = shl i32 -1, [[Y:%.*]]
+; CHECK-NEXT: [[RET:%.*]] = and i32 [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret i32 [[RET]]
;
%tmp0 = ashr i32 %x, %y
@@ -114,8 +114,8 @@ define i32 @positive_biggershl_ashrexact
define <2 x i32> @positive_samevar_vec(<2 x i32> %x, <2 x i32> %y) {
; CHECK-LABEL: @positive_samevar_vec(
-; CHECK-NEXT: [[TMP0:%.*]] = ashr <2 x i32> [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT: [[RET:%.*]] = shl <2 x i32> [[TMP0]], [[Y]]
+; CHECK-NEXT: [[TMP1:%.*]] = shl <2 x i32> <i32 -1, i32 -1>, [[Y:%.*]]
+; CHECK-NEXT: [[RET:%.*]] = and <2 x i32> [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret <2 x i32> [[RET]]
;
%tmp0 = ashr <2 x i32> %x, %y
@@ -161,8 +161,7 @@ define <3 x i32> @positive_sameconst_vec
define <3 x i32> @positive_sameconst_vec_undef2(<3 x i32> %x) {
; CHECK-LABEL: @positive_sameconst_vec_undef2(
-; CHECK-NEXT: [[TMP0:%.*]] = ashr <3 x i32> [[X:%.*]], <i32 5, i32 undef, i32 5>
-; CHECK-NEXT: [[RET:%.*]] = shl <3 x i32> [[TMP0]], <i32 5, i32 undef, i32 5>
+; CHECK-NEXT: [[RET:%.*]] = and <3 x i32> [[X:%.*]], <i32 -32, i32 undef, i32 -32>
; CHECK-NEXT: ret <3 x i32> [[RET]]
;
%tmp0 = ashr <3 x i32> %x, <i32 5, i32 undef, i32 5>
Modified: llvm/trunk/test/Transforms/InstCombine/canonicalize-lshr-shl-to-masking.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/canonicalize-lshr-shl-to-masking.ll?rev=334372&r1=334371&r2=334372&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/canonicalize-lshr-shl-to-masking.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/canonicalize-lshr-shl-to-masking.ll Sun Jun 10 13:10:13 2018
@@ -15,8 +15,8 @@
define i32 @positive_samevar(i32 %x, i32 %y) {
; CHECK-LABEL: @positive_samevar(
-; CHECK-NEXT: [[TMP0:%.*]] = lshr i32 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT: [[RET:%.*]] = shl i32 [[TMP0]], [[Y]]
+; CHECK-NEXT: [[TMP1:%.*]] = shl i32 -1, [[Y:%.*]]
+; CHECK-NEXT: [[RET:%.*]] = and i32 [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret i32 [[RET]]
;
%tmp0 = lshr i32 %x, %y
@@ -114,8 +114,8 @@ define i32 @positive_biggershl_lshrexact
define <2 x i32> @positive_samevar_vec(<2 x i32> %x, <2 x i32> %y) {
; CHECK-LABEL: @positive_samevar_vec(
-; CHECK-NEXT: [[TMP0:%.*]] = lshr <2 x i32> [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT: [[RET:%.*]] = shl <2 x i32> [[TMP0]], [[Y]]
+; CHECK-NEXT: [[TMP1:%.*]] = shl <2 x i32> <i32 -1, i32 -1>, [[Y:%.*]]
+; CHECK-NEXT: [[RET:%.*]] = and <2 x i32> [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret <2 x i32> [[RET]]
;
%tmp0 = lshr <2 x i32> %x, %y
@@ -161,8 +161,7 @@ define <3 x i32> @positive_sameconst_vec
define <3 x i32> @positive_sameconst_vec_undef2(<3 x i32> %x) {
; CHECK-LABEL: @positive_sameconst_vec_undef2(
-; CHECK-NEXT: [[TMP0:%.*]] = lshr <3 x i32> [[X:%.*]], <i32 5, i32 undef, i32 5>
-; CHECK-NEXT: [[RET:%.*]] = shl <3 x i32> [[TMP0]], <i32 5, i32 undef, i32 5>
+; CHECK-NEXT: [[RET:%.*]] = and <3 x i32> [[X:%.*]], <i32 -32, i32 undef, i32 -32>
; CHECK-NEXT: ret <3 x i32> [[RET]]
;
%tmp0 = lshr <3 x i32> %x, <i32 5, i32 undef, i32 5>
More information about the llvm-commits
mailing list