[llvm] [IndVarSimplify] Allow predicateLoopExit on some loops with thread-local writes (PR #155901)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 23 01:18:54 PDT 2025
================
@@ -1840,6 +1880,23 @@ bool IndVarSimplify::predicateLoopExits(Loop *L, SCEVExpander &Rewriter) {
const SCEV *ExitCount = SE->getExitCount(L, ExitingBB);
auto *BI = cast<BranchInst>(ExitingBB->getTerminator());
+ if (HasThreadLocalSideEffects) {
+ const BasicBlock *Unreachable = nullptr;
+ const BasicBlock *InLoop = nullptr;
+ for (const BasicBlock *Succ : BI->successors()) {
+ if (isa<UnreachableInst>(Succ->getTerminator()))
+ Unreachable = Succ;
+ else if (L->contains(Succ))
+ InLoop = Succ;
+ }
+ // Exit BB which have one branch back into the loop and another one to
+ // a trap can still be optimized, because local side effects cannot
+ // be observed in the exit case (the trap). We could be smarter about
+ // this, but for now lets pattern match common cases that directly trap.
+ if (Unreachable == nullptr || InLoop == nullptr ||
+ !crashingBBWithoutEffect(*Unreachable))
+ return Changed;
+ }
----------------
nikic wrote:
Can you please add tests to check this does the right thing when there are multiple exits (some of which trap and some of which don't)?
https://github.com/llvm/llvm-project/pull/155901
More information about the llvm-commits
mailing list