[llvm] [polly] [SCEV] Introduce SDiv expressions (PR #216862)
Antonio Frighetto via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 24 07:40:35 PDT 2026
================
@@ -729,20 +730,29 @@ Value *SCEVExpander::visitUDivExpr(SCEVUseT<const SCEVUDivExpr *> S) {
const SCEV *RHSExpr = S->getRHS();
Value *RHS = expand(RHSExpr);
if (SafeUDivMode) {
- bool GuaranteedNotPoison =
- ScalarEvolution::isGuaranteedNotToBePoison(RHSExpr);
- if (!GuaranteedNotPoison)
+ if (!ScalarEvolution::isGuaranteedNotToBePoison(RHSExpr))
RHS = Builder.CreateFreeze(RHS);
// We need an umax if either RHSExpr is not known to be zero, or if it is
// not guaranteed to be non-poison. In the later case, the frozen poison may
// be 0.
- if (!SE.isKnownNonZero(RHSExpr) || !GuaranteedNotPoison)
+ if (S->mayTriggerUB(SE))
RHS = Builder.CreateIntrinsic(RHS->getType(), Intrinsic::umax,
{RHS, ConstantInt::get(RHS->getType(), 1)});
}
return InsertBinop(Instruction::UDiv, LHS, RHS, SCEV::FlagAnyWrap,
- /*IsSafeToHoist*/ SE.isKnownNonZero(S->getRHS()));
+ /*IsSafeToHoist=*/!S->mayTriggerUB(SE));
+}
+
+Value *SCEVExpander::visitSDivExpr(SCEVUseT<const SCEVSDivExpr *> S) {
+ Value *LHS = expand(S->getLHS());
+ Value *RHS = expand(S->getRHS());
+ if (S->mayTriggerUB(SE)) {
+ LHS = Builder.CreateFreeze(LHS);
+ RHS = Builder.CreateFreeze(RHS);
+ }
----------------
antoniofrighetto wrote:
The expansion logic still seems to be incorrect: https://alive2.llvm.org/ce/z/D6KTDh (the entry branch in `tgt` is simplified to skip the vectorized path and prevent Alive2 from timing out). Freeze stops propagating poison, though it does not prevent arithmetic UB. So, if you hoist `sdiv freeze(LHS), freeze(RHS)`, where freeze(0) is still 0, you have immediate UB (when the divisor is zero).
https://github.com/llvm/llvm-project/pull/216862
More information about the llvm-commits
mailing list