Minor cleanup related to my latest scheduler changes.
[oota-llvm.git] / lib / Transforms / InstCombine / InstCombineShifts.cpp
index 7969dbfec04b7abe7c7c8bb11f8a9fe1a20d0059..a0c5ef5338f68e054715ad84d3c43e0630f8c9c4 100644 (file)
@@ -54,19 +54,17 @@ Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) {
     if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
       return Res;
 
-  // X shift (A srem B) -> X shift (A urem B) iff B is positive.
+  // X shift (A srem B) -> X shift (A and B-1) iff B is a power of 2.
   // Because shifts by negative values are undefined.
   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op1))
-    if (BO->getOpcode() == Instruction::SRem && BO->getType()->isIntegerTy()) {
-      // Make sure the divisor's sign bit is zero.
-      APInt Mask = APInt::getSignBit(BO->getType()->getPrimitiveSizeInBits());
-      if (MaskedValueIsZero(BO->getOperand(1), Mask)) {
-        Value *URem = Builder->CreateURem(BO->getOperand(0), BO->getOperand(1),
-                                          BO->getName());
-        I.setOperand(1, URem);
-        return &I;
-      }
-    }
+    if (BO->hasOneUse() && BO->getOpcode() == Instruction::SRem)
+      if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1)))
+        if (CI->getValue().isPowerOf2()) {
+          Constant *C = ConstantInt::get(BO->getType(), CI->getValue()-1);
+          Value *Rem = Builder->CreateAnd(BO->getOperand(0), C, BO->getName());
+          I.setOperand(1, Rem);
+          return &I;
+        }
 
   return 0;
 }
@@ -172,7 +170,7 @@ static bool CanEvaluateShifted(Value *V, unsigned NumBits, bool isLeftShift,
     if (CI->getZExtValue() > NumBits) {
       unsigned LowBits = CI->getZExtValue() - NumBits;
       if (MaskedValueIsZero(I->getOperand(0),
-                          APInt::getLowBitsSet(TypeWidth, LowBits) << NumBits))
+                          APInt::getLowBitsSet(TypeWidth, NumBits) << LowBits))
         return true;
     }