[llvm] ValueTracking: simplify udiv/urem recurrences (PR #108973)

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 21 12:01:26 PDT 2024


================
@@ -1543,6 +1543,29 @@ static void computeKnownBitsFromOperator(const Operator *I,
         }
         break;
       }
+
+      case Instruction::UDiv: {
+        // For UDiv, the result can never exceed either the numerator, or the
+        // start value, whichever is greater. The case where the PHI is not
+        // the numerator of the UDiv is already handled by other code.
+        if (BO->getOperand(0) != P)
+          break;
+        [[fallthrough]];
+      }
+
+      case Instruction::URem: {
+        // For URem, the result can never exceed the start value.
+        SimplifyQuery RecQ = Q.getWithoutCondContext();
+
+        unsigned OpNum = P->getOperand(0) == R ? 0 : 1;
+        Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();
+
+        RecQ.CxtI = RInst;
+        computeKnownBits(R, DemandedElts, Known2, Depth + 1, RecQ);
+        Known.Zero.setHighBits(Known2.countMinLeadingZeros());
----------------
artagnon wrote:

Isn't the BTC of the loop unknown (since we can't use SCEV), and hence is possibly-zero? In this case, will the PHI not take the initial value?

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


More information about the llvm-commits mailing list