[InstCombine] Added support for (X >>s C) << C --> X & (-1 << C)

Differential Revision: https://reviews.llvm.org/D36743

llvm-svn: 310949
This commit is contained in:
Amjad Aboud 2017-08-15 19:33:14 +00:00
parent ab0400d5af
commit 0464c5d958
2 changed files with 18 additions and 2 deletions

View File

@ -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));
}

View File

@ -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.