[PATCH] D32098: [InstCombine] Use less bitwise operations to handle Instruction::SExt in SimplifyDemandedUseBits. Other improvements.

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 14 15:10:37 PDT 2017


craig.topper created this revision.

The current code created a NewBits mask and used it as a mask several times. One of them just before a call to trunc making it unnecessary. A call to getActiveBits can get us the same information for the case. We also ORed with this mask later when we should have just sign extended the known bits.

We also called trunc on the guaranteed to be zero KnownZeros/Ones masks entering this code. Creating appropriately sized temporary APInts is probably better. (This needs to be done in the zero extend code too which I'll do as a follow up)

We also made a recursive call to SimplifyDemandedBits even if we were just going to replace it with ZExt due to upper bits not being used. Unfortunately, now we create the ZExt in two separate places and I didn't see a great way to merge the control flow.


https://reviews.llvm.org/D32098

Files:
  lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp


Index: lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
+++ lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
@@ -414,40 +414,37 @@
   }
   case Instruction::SExt: {
     // Compute the bits in the result that are not present in the input.
-    unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
+    unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
 
-    APInt InputDemandedBits = DemandedMask &
-                              APInt::getLowBitsSet(BitWidth, SrcBitWidth);
+    // If the extend bits are not demanded, convert this into a zero extension.
+    if (DemandedMask.getActiveBits() <= SrcBitWidth) {
+      // Convert to ZExt cast
+      CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName());
+      return InsertNewInstWith(NewCast, *I);
+    }
 
-    APInt NewBits(APInt::getBitsSetFrom(BitWidth, SrcBitWidth));
-    // If any of the sign extended bits are demanded, we know that the sign
-    // bit is demanded.
-    if ((NewBits & DemandedMask) != 0)
-      InputDemandedBits.setBit(SrcBitWidth-1);
+    APInt InputDemandedBits = DemandedMask.trunc(SrcBitWidth);
 
-    InputDemandedBits = InputDemandedBits.trunc(SrcBitWidth);
-    KnownZero = KnownZero.trunc(SrcBitWidth);
-    KnownOne = KnownOne.trunc(SrcBitWidth);
-    if (SimplifyDemandedBits(I, 0, InputDemandedBits, KnownZero, KnownOne,
-                             Depth + 1))
-      return I;
-    InputDemandedBits = InputDemandedBits.zext(BitWidth);
-    KnownZero = KnownZero.zext(BitWidth);
-    KnownOne = KnownOne.zext(BitWidth);
-    assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
+    // Sign extended bits are demanded, so the sign bit is demanded.
+    InputDemandedBits.setSignBit();
 
-    // If the sign bit of the input is known set or clear, then we know the
-    // top bits of the result.
+    APInt InputKnownZero(SrcBitWidth, 0), InputKnownOne(SrcBitWidth, 0);
+    if (SimplifyDemandedBits(I, 0, InputDemandedBits, InputKnownZero,
+                             InputKnownOne, Depth + 1))
+      return I;
 
-    // If the input sign bit is known zero, or if the NewBits are not demanded
-    // convert this into a zero extension.
-    if (KnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) {
+    // If the input sign bit is known zero, convert this into a zero extension.
+    if (InputKnownZero.isNegative()) {
       // Convert to ZExt cast
       CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName());
       return InsertNewInstWith(NewCast, *I);
-    } else if (KnownOne[SrcBitWidth-1]) {    // Input sign bit known set
-      KnownOne |= NewBits;
     }
+
+    // If the sign bit of the input is known set or clear, then we know the
+    // top bits of the result.
+    KnownZero = InputKnownZero.sext(BitWidth);
+    KnownOne = InputKnownOne.sext(BitWidth);
+    assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
     break;
   }
   case Instruction::Add:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D32098.95350.patch
Type: text/x-patch
Size: 3150 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170414/4be02b60/attachment.bin>


More information about the llvm-commits mailing list