[llvm] [ValueTracking] Infer `exact` for `udiv` and `sdiv` (PR #126438)

via llvm-commits llvm-commits at lists.llvm.org
Sun Feb 9 14:02:50 PST 2025


================
@@ -196,6 +197,35 @@ static bool optimizeDivRem(Function &F, const TargetTransformInfo &TTI,
     auto &DivInst = E.DivInst;
     auto &RemInst = E.RemInst;
 
+    // Before any transformation, analyze whether
+    // RemInst is known to be zero from assumes. If so,
+    // set `exact` flag on DivInst.
+    //
+    // This kind of flag inference is usually handled by
+    // InstCombine but it will not work for this scenario
+    // because DivRemPairs is run towards the end of the
+    // optimization pipeline and removes all poison generating
+    // flags if the target does not have an unified instruction
+    // for both division and remainder.
+    //
+    // So, we do this flag inference here to ensure that flags
+    // inferred from assumes are not removed regardless of
+    // `HasDivRemOp`.
+    //
+    // Doing this in the tail end of the pipeline does not
+    // affect any follow-up optimizations because
+    // the `exact` flag is mostly used by the backend to
+    // generate better code.
+
+    const DataLayout &DL = DivInst->getDataLayout();
+    AssumptionCache AC(F, &TTI);
+    SimplifyQuery SQ(DL, &DT, &AC, DivInst);
+    bool RemInstIsZero = llvm::isKnownToBeZeroFromAssumes(RemInst, SQ);
+
+    if (RemInstIsZero) {
+      DivInst->setIsExact();
+    }
----------------
goldsteinn wrote:

nit style (no braces for single expression in if).

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


More information about the llvm-commits mailing list