[llvm] [InstSimplify] Fold converted urem to 0 if there's no overlapping bits (PR #71528)

Graham Hunter via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 15 06:23:32 PST 2023


================
@@ -2103,6 +2103,22 @@ static Value *simplifyAndInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
       isKnownToBeAPowerOfTwo(Op0, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI, Q.DT))
     return Constant::getNullValue(Op0->getType());
 
+  // (x << N) & ((x << M) - 1) --> 0, where x is known to be a power of 2 and
+  // M <= N.
+  const APInt *Shift1, *Shift2;
+  if (match(Op0, m_Shl(m_Value(X), m_APInt(Shift1))) &&
+      match(Op1, m_Add(m_Shl(m_Specific(X), m_APInt(Shift2)), m_AllOnes())) &&
+      isKnownToBeAPowerOfTwo(X, Q.DL, /*OrZero*/ true, /*Depth*/ 0, Q.AC,
+                             Q.CxtI) &&
+      Shift1->uge(*Shift2))
----------------
huntergr-arm wrote:

I added a negative test in the second precommit to check for failure with a non-power-of-2 input, but I've now added tests with a right shift instead of left, with the larger shift being on the -1 side, and for an add of something other than -1. Are there any others you would like?

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


More information about the llvm-commits mailing list