[llvm-commits] [llvm] r93019 - in /llvm/trunk: lib/Transforms/InstCombine/InstCombineShifts.cpp test/Transforms/InstCombine/shift-sra.ll

Chris Lattner sabre at nondot.org
Fri Jan 8 11:04:21 PST 2010


Author: lattner
Date: Fri Jan  8 13:04:21 2010
New Revision: 93019

URL: http://llvm.org/viewvc/llvm-project?rev=93019&view=rev
Log:
teach instcombine to delete sign extending shift pairs (sra(shl X, C), C) when
the input is already sign extended.

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp
    llvm/trunk/test/Transforms/InstCombine/shift-sra.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp?rev=93019&r1=93018&r2=93019&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp Fri Jan  8 13:04:21 2010
@@ -414,17 +414,28 @@
   if (Instruction *R = commonShiftTransforms(I))
     return R;
   
-  Value *Op0 = I.getOperand(0);
+  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
   
-  // ashr int -1, X = -1   (for any arithmetic shift rights of ~0)
-  if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0))
+  if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) {
+    // ashr int -1, X = -1   (for any arithmetic shift rights of ~0)
     if (CSI->isAllOnesValue())
       return ReplaceInstUsesWith(I, CSI);
+  }
+  
+  if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
+    // If the input is a SHL by the same constant (ashr (shl X, C), C), then we
+    // have a sign-extend idiom.  If the input value is known to already be sign
+    // extended enough, delete the extension.
+    Value *X;
+    if (match(Op0, m_Shl(m_Value(X), m_Specific(Op1))) &&
+        ComputeNumSignBits(X) > Op1C->getZExtValue())
+      return ReplaceInstUsesWith(I, X);
+  }            
   
   // See if we can turn a signed shr into an unsigned shr.
   if (MaskedValueIsZero(Op0,
                         APInt::getSignBit(I.getType()->getScalarSizeInBits())))
-    return BinaryOperator::CreateLShr(Op0, I.getOperand(1));
+    return BinaryOperator::CreateLShr(Op0, Op1);
   
   // Arithmetic shifting an all-sign-bit value is a no-op.
   unsigned NumSignBits = ComputeNumSignBits(Op0);

Modified: llvm/trunk/test/Transforms/InstCombine/shift-sra.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/shift-sra.ll?rev=93019&r1=93018&r2=93019&view=diff

==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/shift-sra.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/shift-sra.ll Fri Jan  8 13:04:21 2010
@@ -37,3 +37,22 @@
 ; CHECK: %P = phi i64
 ; CHECK-NEXT: ret i64 %P
 }
+
+define i64 @test4(i1 %X, i64 %Y, i1 %Cond) {
+  br i1 %Cond, label %T, label %F
+T:
+  %X2 = sext i1 %X to i64
+  br label %C
+F:
+  %Y2 = ashr i64 %Y, 63
+  br label %C
+C:
+  %P = phi i64 [%X2, %T], [%Y2, %F] 
+  %R = shl i64 %P, 12
+  %S = ashr i64 %R, 12
+  ret i64 %S
+  
+; CHECK: @test4
+; CHECK: %P = phi i64
+; CHECK-NEXT: ret i64 %P
+}





More information about the llvm-commits mailing list