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

via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 8 10:21:49 PDT 2024


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

It seems that knowing that `computeNumSignBits` is greater than `ShCnt` is not enough. We need to shift `computeNumSignBits` by `ShCnt` and then check whether `DemandedBits` is still within the range of `SignBits`. 
If there is a way to check the range of `SignBits` and `DemandedBits` by checking `nsw`, please let me know.

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


More information about the llvm-commits mailing list