]> granicus.if.org Git - llvm/commitdiff
[InstCombine] enable (X <<nsw C1) >>s C2 --> X <<nsw (C1-C2) for vectors with splat...
authorSanjay Patel <spatel@rotateright.com>
Mon, 30 Jan 2017 17:19:32 +0000 (17:19 +0000)
committerSanjay Patel <spatel@rotateright.com>
Mon, 30 Jan 2017 17:19:32 +0000 (17:19 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@293507 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/InstCombine/InstCombineShifts.cpp
test/Transforms/InstCombine/shift.ll

index 55b37f0fe62aa189834da4d40e8194d71e638517..5c86c3e28db1c62c1ef2cc4a0ceccdddd103dcb0 100644 (file)
@@ -370,24 +370,7 @@ foldShiftByConstOfShiftByConst(BinaryOperator &I, const APInt *COp1,
     return nullptr;
 
   IntegerType *Ty = cast<IntegerType>(I.getType());
-  if (ShiftAmt1 < ShiftAmt2) {
-    uint32_t ShiftDiff = ShiftAmt2 - ShiftAmt1;
-
-    // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. However,
-    // we can handle (X <<nsw C1) >>s C2 since it only shifts in sign bits.
-    if (I.getOpcode() == Instruction::AShr &&
-        ShiftOp->getOpcode() == Instruction::Shl) {
-      if (ShiftOp->hasNoSignedWrap()) {
-        // (X <<nsw C1) >>s C2 --> X >>s (C2-C1)
-        ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
-        BinaryOperator *NewAShr =
-            BinaryOperator::Create(Instruction::AShr, X, ShiftDiffCst);
-        NewAShr->setIsExact(I.isExact());
-        return NewAShr;
-      }
-    }
-  } else {
-    assert(ShiftAmt2 < ShiftAmt1);
+  if (ShiftAmt2 < ShiftAmt1) {
     uint32_t ShiftDiff = ShiftAmt1 - ShiftAmt2;
 
     // (X >>?exact C1) << C2 --> X >>?exact (C1-C2)
@@ -800,7 +783,8 @@ Instruction *InstCombiner::visitAShr(BinaryOperator &I) {
   if (Instruction *R = commonShiftTransforms(I))
     return R;
 
-  unsigned BitWidth = I.getType()->getScalarSizeInBits();
+  Type *Ty = I.getType();
+  unsigned BitWidth = Ty->getScalarSizeInBits();
   const APInt *ShAmtAPInt;
   if (match(Op1, m_APInt(ShAmtAPInt))) {
     unsigned ShAmt = ShAmtAPInt->getZExtValue();
@@ -811,7 +795,19 @@ Instruction *InstCombiner::visitAShr(BinaryOperator &I) {
     Value *X;
     if (match(Op0, m_Shl(m_ZExt(m_Value(X)), m_Specific(Op1))) &&
         ShAmt == BitWidth - X->getType()->getScalarSizeInBits())
-      return new SExtInst(X, I.getType());
+      return new SExtInst(X, Ty);
+
+    // We can't handle (X << C1) >>s C2. It shifts arbitrary bits in. However,
+    // we can handle (X <<nsw C1) >>s C2 since it only shifts in sign bits.
+    const APInt *ShlAmtAPInt;
+    if (match(Op0, m_NSWShl(m_Value(X), m_APInt(ShlAmtAPInt))) &&
+        ShlAmtAPInt->ult(*ShAmtAPInt)) {
+      // (X <<nsw C1) >>s C2 --> X >>s (C2 - C1)
+      Constant *ShiftDiff = ConstantInt::get(Ty, *ShAmtAPInt - *ShlAmtAPInt);
+      BinaryOperator *NewAShr = BinaryOperator::CreateAShr(X, ShiftDiff);
+      NewAShr->setIsExact(I.isExact());
+      return NewAShr;
+    }
 
     // If the shifted-out value is known-zero, then this is an exact shift.
     if (!I.isExact() &&
index 5e8b4cb3250f899b28192b23b4870e5eda68849c..459bd5862bdf61508b63493b2a202d97a20c479e 100644 (file)
@@ -895,15 +895,15 @@ define i32 @test50(i32 %x) {
 }
 
 ; (X <<nsw C1) >>s C2 --> X >>s (C2-C1)
+; Also, check that exact is propagated.
 
 define <2 x i32> @test50_splat_vec(<2 x i32> %x) {
 ; CHECK-LABEL: @test50_splat_vec(
-; CHECK-NEXT:    [[A:%.*]] = shl nsw <2 x i32> %x, <i32 1, i32 1>
-; CHECK-NEXT:    [[B:%.*]] = ashr <2 x i32> [[A]], <i32 3, i32 3>
+; CHECK-NEXT:    [[B:%.*]] = ashr exact <2 x i32> %x, <i32 2, i32 2>
 ; CHECK-NEXT:    ret <2 x i32> [[B]]
 ;
   %A = shl nsw <2 x i32> %x, <i32 1, i32 1>
-  %B = ashr <2 x i32> %A, <i32 3, i32 3>
+  %B = ashr exact <2 x i32> %A, <i32 3, i32 3>
   ret <2 x i32> %B
 }