Refactor this code a bit and make it more general. This now compiles:
authorChris Lattner <sabre@nondot.org>
Sun, 18 Sep 2005 07:22:02 +0000 (07:22 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 18 Sep 2005 07:22:02 +0000 (07:22 +0000)
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus2 (unsigned int x) { b.j += x; }

To:

_plus2:
        lis r2, ha16(L_b$non_lazy_ptr)
        lwz r2, lo16(L_b$non_lazy_ptr)(r2)
        lwz r4, 0(r2)
        slwi r3, r3, 6
        add r3, r4, r3
        rlwimi r3, r4, 0, 26, 14
        stw r3, 0(r2)
        blr

instead of:

_plus2:
        lis r2, ha16(L_b$non_lazy_ptr)
        lwz r2, lo16(L_b$non_lazy_ptr)(r2)
        lwz r4, 0(r2)
        rlwinm r5, r4, 26, 21, 31
        add r3, r5, r3
        rlwimi r4, r3, 6, 15, 25
        stw r4, 0(r2)
        blr

by eliminating an 'and'.

I'm pretty sure this is as small as we can go :)

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23386 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/InstructionCombining.cpp

index cd02410705dd4f5449fa180ccebc0141fd00eba4..d1a70c88fc8ac3677ce78d280e6ce87c9e814bbf 100644 (file)
@@ -1572,9 +1572,26 @@ Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi,
   return new SetCondInst(Instruction::SetGT, OffsetVal, AddCST);
 }
 
-/// FoldLogicalPlusAnd - We know that Mask is of the form 0+1+, and that this is
-/// part of an expression (LHS +/- RHS) & Mask, where isSub determines whether
-/// the operator is a sub.  If we can fold one of the following xforms:
+// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
+// any number of 0s on either side.  The 1s are allowed to wrap from LSB to
+// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.  0x0F0F0000 is
+// not, since all 1s are not contiguous.
+static bool isRunOfOnes(ConstantIntegral *Val, unsigned &MB, unsigned &ME) {
+  uint64_t V = Val->getRawValue();
+  if (!isShiftedMask_64(V)) return false;
+
+  // look for the first zero bit after the run of ones
+  MB = 64-CountLeadingZeros_64((V - 1) ^ V);
+  // look for the first non-zero bit
+  ME = 64-CountLeadingZeros_64(V);
+  return true;
+}
+
+
+
+/// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask,
+/// where isSub determines whether the operator is a sub.  If we can fold one of
+/// the following xforms:
 /// 
 /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask
 /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0
@@ -1594,12 +1611,30 @@ Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS,
   switch (LHSI->getOpcode()) {
   default: return 0;
   case Instruction::And:
-    if (ConstantExpr::getAnd(N, Mask) == Mask)
-      break;
+    if (ConstantExpr::getAnd(N, Mask) == Mask) {
+      // If the AndRHS is a power of two minus one (0+1+), this is simple.
+      if ((Mask->getRawValue() & Mask->getRawValue()+1) == 0)
+        break;
+
+      // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+
+      // part, we don't need any explicit masks to take them out of A.  If that
+      // is all N is, ignore it.
+      unsigned MB, ME;
+      if (isRunOfOnes(Mask, MB, ME)) {  // begin/end bit of run, inclusive
+        Constant *Mask = ConstantInt::getAllOnesValue(RHS->getType());
+        Mask = ConstantExpr::getUShr(Mask,
+                                     ConstantInt::get(Type::UByteTy,
+                                                      (64-MB+1)));
+        if (MaskedValueIsZero(RHS, cast<ConstantIntegral>(Mask)))
+          break;
+      }
+    }
     return 0;
   case Instruction::Or:
   case Instruction::Xor:
-    if (ConstantExpr::getAnd(N, Mask)->isNullValue())
+    // If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0
+    if ((Mask->getRawValue() & Mask->getRawValue()+1) == 0 &&
+        ConstantExpr::getAnd(N, Mask)->isNullValue())
       break;
     return 0;
   }
@@ -1683,27 +1718,21 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
           return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
         break;
       case Instruction::Add:
-        // If the AndRHS is a power of two minus one (0+1+).
-        if ((AndRHS->getRawValue() & AndRHS->getRawValue()+1) == 0) {
-          // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS.
-          // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
-          // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
-          if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I))
-            return BinaryOperator::createAnd(V, AndRHS);
-          if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I))
-            return BinaryOperator::createAnd(V, AndRHS);  // Add commutes
-        }
+        // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS.
+        // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
+        // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
+        if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I))
+          return BinaryOperator::createAnd(V, AndRHS);
+        if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I))
+          return BinaryOperator::createAnd(V, AndRHS);  // Add commutes
         break;
 
       case Instruction::Sub:
-        // If the AndRHS is a power of two minus one (0+1+).
-        if ((AndRHS->getRawValue() & AndRHS->getRawValue()+1) == 0) {
-          // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS.
-          // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
-          // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
-          if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I))
-            return BinaryOperator::createAnd(V, AndRHS);
-        }
+        // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS.
+        // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
+        // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
+        if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I))
+          return BinaryOperator::createAnd(V, AndRHS);
         break;
       }