@@ -665,6 +665,12 @@ Value *SCEVExpander::visitUDivExpr(const SCEVUDivExpr *S) {
665665 }
666666
667667 Value *RHS = expand (S->getRHS ());
668+ if (SafeUDivMode && !SE.isKnownNonZero (S->getRHS ())) {
669+ if (!isGuaranteedNotToBePoison (RHS))
670+ RHS = Builder.CreateFreeze (RHS);
671+ RHS = Builder.CreateIntrinsic (RHS->getType (), Intrinsic::umax,
672+ {RHS, ConstantInt::get (RHS->getType (), 1 )});
673+ }
668674 return InsertBinop (Instruction::UDiv, LHS, RHS, SCEV::FlagAnyWrap,
669675 /* IsSafeToHoist*/ SE.isKnownNonZero (S->getRHS ()));
670676}
@@ -2341,15 +2347,15 @@ struct SCEVFindUnsafe {
23412347};
23422348} // namespace
23432349
2344- static bool isSafeToExpand (const SCEV *S, bool CanonicalMode,
2345- ScalarEvolution &SE) {
2350+ bool SCEVExpander:: isSafeToExpand (const SCEV *S, bool CanonicalMode,
2351+ ScalarEvolution &SE) {
23462352 SCEVFindUnsafe Search (SE, CanonicalMode);
23472353 visitAll (S, Search);
23482354 return !Search.IsUnsafe ;
23492355}
23502356
23512357bool SCEVExpander::isSafeToExpand (const SCEV *S) const {
2352- return :: isSafeToExpand (S, CanonicalMode, SE);
2358+ return isSafeToExpand (S, CanonicalMode, SE);
23532359}
23542360
23552361bool SCEVExpander::isSafeToExpandAt (const SCEV *S,
@@ -2374,47 +2380,6 @@ bool SCEVExpander::isSafeToExpandAt(const SCEV *S,
23742380 return false ;
23752381}
23762382
2377- namespace {
2378- struct SCEVUDivRewriter : public SCEVRewriteVisitor <SCEVUDivRewriter> {
2379- SCEVUDivRewriter (ScalarEvolution &SE) : SCEVRewriteVisitor(SE) {}
2380-
2381- static const SCEV *rewrite (const SCEV *Scev, ScalarEvolution &SE) {
2382- SCEVUDivRewriter Rewriter (SE);
2383- return Rewriter.visit (Scev);
2384- }
2385-
2386- const SCEV *visitUDivExpr (const SCEVUDivExpr *Expr) {
2387- auto *LHS = visit (Expr->getLHS ());
2388- auto *RHS = visit (Expr->getRHS ());
2389- return SE.getUDivExpr (LHS, SE.getUMaxExpr (SE.getOne (RHS->getType ()), RHS));
2390- }
2391- };
2392- } // namespace
2393-
2394- const SCEV *SCEVExpander::rewriteExpressionToRemoveUB (const SCEV *Expr, Loop *L,
2395- ScalarEvolution &SE) {
2396- SmallVector<BasicBlock *> Exiting;
2397- L->getExitingBlocks (Exiting);
2398- // Check if exit count for any exit that may execute unconditionally may in
2399- // introduce UB. Note that we can skip checks in the header or if there's a
2400- // single exit, as in those cases we know that the exit count will be
2401- // evaluated in each loop iteration. There are other cases where the exiting
2402- // block executes on each loop iteration, but we don't have a cheap way to
2403- // check at the moment.
2404-
2405- if (Exiting.size () == 1 || all_of (Exiting, [L, &SE](BasicBlock *E) {
2406- if (L->getHeader () == E)
2407- return true ;
2408- const SCEV *EC = SE.getExitCount (L, E);
2409- if (isa<SCEVCouldNotCompute>(EC))
2410- return true ;
2411- return ::isSafeToExpand (EC, true , SE);
2412- }))
2413- return Expr;
2414-
2415- return SCEVUDivRewriter::rewrite (Expr, SE);
2416- }
2417-
24182383void SCEVExpanderCleaner::cleanup () {
24192384 // Result is used, nothing to remove.
24202385 if (ResultUsed)
0 commit comments