From 0388fb65f3a015cb916d799e7a0a8a2e08144aa4 Mon Sep 17 00:00:00 2001 From: Amjad Aboud Date: Tue, 15 Aug 2017 19:33:14 +0000 Subject: [PATCH] [InstCombine] Added support for (X >>s C) << C --> X & (-1 << C) Differential Revision: https://reviews.llvm.org/D36743 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@310949 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/InstCombine/InstCombineShifts.cpp | 4 ++-- test/Transforms/InstCombine/shift.ll | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/Transforms/InstCombine/InstCombineShifts.cpp b/lib/Transforms/InstCombine/InstCombineShifts.cpp index a19cdf7ef6d..45541c9adc0 100644 --- a/lib/Transforms/InstCombine/InstCombineShifts.cpp +++ b/lib/Transforms/InstCombine/InstCombineShifts.cpp @@ -559,8 +559,8 @@ Instruction *InstCombiner::visitShl(BinaryOperator &I) { return new ZExtInst(Builder.CreateShl(X, ShAmt), Ty); } - // (X >>u C) << C --> X & (-1 << C) - if (match(Op0, m_LShr(m_Value(X), m_Specific(Op1)))) { + // (X >> C) << C --> X & (-1 << C) + if (match(Op0, m_Shr(m_Value(X), m_Specific(Op1)))) { APInt Mask(APInt::getHighBitsSet(BitWidth, BitWidth - ShAmt)); return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, Mask)); } diff --git a/test/Transforms/InstCombine/shift.ll b/test/Transforms/InstCombine/shift.ll index 68bbf35d1e6..cbb3d614db2 100644 --- a/test/Transforms/InstCombine/shift.ll +++ b/test/Transforms/InstCombine/shift.ll @@ -221,6 +221,22 @@ define i32 @test12(i32 %A) { ret i32 %C } +;; ((A >>s 6) << 6 === (A & FFFFFFC0) +define i8 @shishi(i8 %x) { +; CHECK-LABEL: @shishi( +; CHECK-NEXT: [[A:%.*]] = ashr i8 [[X:%.*]], 6 +; CHECK-NEXT: [[B:%.*]] = and i8 [[X]], -64 +; CHECK-NEXT: [[EXTRA_USE_OF_A:%.*]] = mul nsw i8 [[A]], 5 +; CHECK-NEXT: [[R:%.*]] = sdiv i8 [[EXTRA_USE_OF_A]], [[B]] +; CHECK-NEXT: ret i8 [[R]] +; + %a = ashr i8 %x, 6 + %b = shl i8 %a, 6 + %extra_use_of_a = mul i8 %a, 5 + %r = sdiv i8 %extra_use_of_a, %b + ret i8 %r +} + ;; This transformation is deferred to DAGCombine: ;; (A >> 3) << 4 === (A & -8) * 2 ;; The shl may be valuable to scalar evolution. -- 2.50.1