[llvm] [InstCombine] Remove shl if we only demand known signbits of shift source (PR #79014)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 29 07:29:52 PST 2024


================
@@ -640,25 +640,31 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
                                                     DemandedMask, Known))
             return R;
 
-      // TODO: If we only want bits that already match the signbit then we don't
+      uint64_t ShiftAmt = SA->getLimitedValue(BitWidth - 1);
+      // If we only want bits that already match the signbit then we don't
       // need to shift.
+      if (DemandedMask.countr_zero() >= ShiftAmt) {
+        unsigned NumLowDemandedBits = BitWidth - DemandedMask.countr_zero();
+        unsigned SignBits =
+            ComputeNumSignBits(I->getOperand(0), Depth + 1, CxtI);
+        if (SignBits > ShiftAmt && SignBits - ShiftAmt >= NumLowDemandedBits)
----------------
nikic wrote:

I think that @goldsteinn is in principle correct, but if `SignBits == ShiftAmt` then `SignBits - ShiftAmt` is 0, so the condition would become `0 >= NumHighDemandedBits`, which is the same as `NumHighDemandedBits == 0`, which is a degenerate case that gets handled earlier anyway. So this just doesn't matter either way.

https://github.com/llvm/llvm-project/pull/79014


More information about the llvm-commits mailing list