[llvm-commits] CVS: llvm/lib/Transforms/Scalar/InstructionCombining.cpp

Zhou Sheng zhousheng00 at gmail.com
Thu Mar 29 01:15:29 PDT 2007



Changes in directory llvm/lib/Transforms/Scalar:

InstructionCombining.cpp updated: 1.708 -> 1.709
---
Log message:

1. Make more use of APInt::getHighBitsSet/getLowBitsSet.
2. Let APInt variable do the binary operation stuff instead of using
   ConstantExpr::getXXX.


---
Diffs of the changes:  (+26 -22)

 InstructionCombining.cpp |   48 +++++++++++++++++++++++++----------------------
 1 files changed, 26 insertions(+), 22 deletions(-)


Index: llvm/lib/Transforms/Scalar/InstructionCombining.cpp
diff -u llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.708 llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.709
--- llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.708	Wed Mar 28 23:45:55 2007
+++ llvm/lib/Transforms/Scalar/InstructionCombining.cpp	Thu Mar 29 03:15:12 2007
@@ -1921,9 +1921,8 @@
           if ((RHSVal == CFF80Val && XorRHS->getValue() == C0080Val) ||
               (RHSVal == C0080Val && XorRHS->getValue() == CFF80Val)) {
             // This is a sign extend if the top bits are known zero.
-            APInt Mask(APInt::getAllOnesValue(TySizeBits));
-            Mask <<= Size;
-            if (!MaskedValueIsZero(XorLHS, Mask))
+            if (!MaskedValueIsZero(XorLHS, 
+                   APInt::getHighBitsSet(TySizeBits, TySizeBits - Size)))
               Size = 0;  // Not a sign ext, but can't be any others either.
             break;
           }
@@ -2984,11 +2983,14 @@
     // We know that the AND will not produce any of the bits shifted in, so if
     // the anded constant includes them, clear them now!
     //
-    Constant *AllOne = ConstantInt::getAllOnesValue(AndRHS->getType());
-    Constant *ShlMask = ConstantExpr::getShl(AllOne, OpRHS);
-    Constant *CI = ConstantExpr::getAnd(AndRHS, ShlMask);
+    uint32_t BitWidth = AndRHS->getType()->getBitWidth();
+    uint32_t OpRHSVal = OpRHS->getValue().getActiveBits() > 64 ?
+                        BitWidth : OpRHS->getZExtValue();
+    APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal));
+    ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShlMask);
 
-    if (CI == ShlMask) {   // Masking out bits that the shift already masks
+    if (CI->getValue() == ShlMask) { 
+    // Masking out bits that the shift already masks
       return ReplaceInstUsesWith(TheAnd, Op);   // No need for the and.
     } else if (CI != AndRHS) {                  // Reducing bits set in and.
       TheAnd.setOperand(1, CI);
@@ -3002,11 +3004,14 @@
     // the anded constant includes them, clear them now!  This only applies to
     // unsigned shifts, because a signed shr may bring in set bits!
     //
-    Constant *AllOne = ConstantInt::getAllOnesValue(AndRHS->getType());
-    Constant *ShrMask = ConstantExpr::getLShr(AllOne, OpRHS);
-    Constant *CI = ConstantExpr::getAnd(AndRHS, ShrMask);
+    uint32_t BitWidth = AndRHS->getType()->getBitWidth();
+    uint32_t OpRHSVal = OpRHS->getValue().getActiveBits() > 64 ?
+                        BitWidth : OpRHS->getZExtValue();
+    APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
+    ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShrMask);
 
-    if (CI == ShrMask) {   // Masking out bits that the shift already masks.
+    if (CI->getValue() == ShrMask) {   
+    // Masking out bits that the shift already masks.
       return ReplaceInstUsesWith(TheAnd, Op);
     } else if (CI != AndRHS) {
       TheAnd.setOperand(1, CI);  // Reduce bits set in and cst.
@@ -3019,9 +3024,11 @@
     // See if this is shifting in some sign extension, then masking it out
     // with an and.
     if (Op->hasOneUse()) {
-      Constant *AllOne = ConstantInt::getAllOnesValue(AndRHS->getType());
-      Constant *ShrMask = ConstantExpr::getLShr(AllOne, OpRHS);
-      Constant *C = ConstantExpr::getAnd(AndRHS, ShrMask);
+      uint32_t BitWidth = AndRHS->getType()->getBitWidth();
+      uint32_t OpRHSVal = OpRHS->getValue().getActiveBits() > 64 ?
+                          BitWidth : OpRHS->getZExtValue();
+      APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
+      Constant *C = ConstantInt::get(AndRHS->getValue() & ShrMask);
       if (C == AndRHS) {          // Masking out bits shifted in.
         // (Val ashr C1) & C2 -> (Val lshr C1) & C2
         // Make the argument unsigned.
@@ -3140,8 +3147,7 @@
       unsigned MB = 0, ME = 0;
       if (isRunOfOnes(Mask, MB, ME)) {  // begin/end bit of run, inclusive
         uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth();
-        APInt Mask(APInt::getAllOnesValue(BitWidth));
-        Mask = Mask.lshr(BitWidth-MB+1);
+        APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1));
         if (MaskedValueIsZero(RHS, Mask))
           break;
       }
@@ -4786,11 +4792,9 @@
               int ShAmtVal = Ty->getPrimitiveSizeInBits()-ShAmt->getZExtValue();
               if (ShAmtVal < 0) ShAmtVal = 0; // Out of range shift.
 
-              Constant *OShAmt = ConstantInt::get(AndTy, ShAmtVal);
-              Constant *ShVal =
-                ConstantExpr::getShl(ConstantInt::getAllOnesValue(AndTy), 
-                                     OShAmt);
-              if (ConstantExpr::getAnd(ShVal, AndCST)->isNullValue())
+              uint32_t BitWidth = AndTy->getPrimitiveSizeInBits();
+              if ((APInt::getHighBitsSet(BitWidth, BitWidth-ShAmtVal) & 
+                   AndCST->getValue()) == 0)
                 CanFold = true;
             }
 
@@ -4925,7 +4929,7 @@
               unsigned ShAmtVal = (unsigned)ShAmt->getZExtValue();
 
               // Otherwise strength reduce the shift into an and.
-              APInt Val(APInt::getAllOnesValue(TypeBits).shl(ShAmtVal));
+              APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal));
               Constant *Mask = ConstantInt::get(Val);
 
               Instruction *AndI =






More information about the llvm-commits mailing list