[llvm] [ConstraintElim] Add facts for SREM. (PR #213453)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 1 09:19:20 PDT 2026


================
@@ -2176,6 +2180,25 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT, LoopInfo &LI,
           AddFact(CmpInst::ICMP_ULE, BO, BO->getOperand(0));
           continue;
         }
+        if (BO->getOpcode() == Instruction::SRem) {
+          Value *X = BO->getOperand(0);
+          Value *N = BO->getOperand(1);
+          Constant *Zero = Constant::getNullValue(BO->getType());
+          if (Info.doesHold(CmpInst::ICMP_SGE, X, Zero) ||
+              isKnownNonNegative(X, F.getDataLayout())) {
+            // srem x, n: result >= 0, if x >= 0 (result has the sign of x)
+            AddFact(CmpInst::ICMP_SGE, BO, Zero);
+            // srem x, n: result <= x, if x >= 0 (|result| <= |x| and both are
+            // non-negative)
+            AddFact(CmpInst::ICMP_SLE, BO, X);
+          }
+          if (Info.doesHold(CmpInst::ICMP_SGT, N, Zero) ||
+              isKnownPositive(N, F.getDataLayout())) {
----------------
dtcxzyw wrote:

```suggestion
          if (Info.doesHold(CmpInst::ICMP_SGE, N, Zero) ||
              isKnownNonNegative(N, F.getDataLayout())) {
```
It also holds for `n = 0`. This should be fine to leverage the immediate UB.


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


More information about the llvm-commits mailing list