[llvm] ValueTracking: simplify udiv/urem recurrences (PR #108973)
Ramkumar Ramachandra via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 21 14:58:06 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:
Consider the following loop:
```llvm
loop:
%iv = phi i64 [9, %entry], [%iv.next, %loop]
%iv.next = urem i64 3, %iv
%exitcond = icmp eq %iv.next, 3
br i1 %exitcond, label %exit, label %loop
```
It has trip-count = 1, backedge-taken-count = 0. It is not UB. Do you agree that the maximum value of %iv is 9, and not 3?
https://github.com/llvm/llvm-project/pull/108973
More information about the llvm-commits
mailing list