[llvm] [SimpleLoopUnswitch] Allow trivial unswitch when exit LCSSA is a header PHI (PR #222962)
Ehsan Amiri via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 11 12:08:31 PDT 2026
================
@@ -647,10 +675,20 @@ static bool unswitchTrivialBranch(Loop &L, CondBrInst &BI, DominatorTree &DT,
}
auto *ContinueBB = BI.getSuccessor(1 - LoopExitSuccIdx);
auto *ParentBB = BI.getParent();
+
+ // If the exit incomings aren't loop-invariant, the unswitch is still trivial
+ // when ParentBB dominates the latch and every non-invariant incoming is a
+ // header PHI. Those incomings are repaired after unswitching.
+ bool TrivialFromHeader = false;
if (!ModifiedBranch &&
!areLoopExitPHIsLoopInvariant(L, *ParentBB, *LoopExitBB)) {
- LLVM_DEBUG(dbgs() << " Loop exit PHI's aren't loop-invariant!\n");
- return false;
+ TrivialFromHeader =
+ DT.dominates(ParentBB, L.getLoopLatch()) &&
----------------
amehsan wrote:
IIUC you don't need this condition. We never attempt to trivially unswitch a branch if it does not post dominate the header. So if it is not guaranteed that the branch will be visited in the first iteration of the loop, trivial unswitching is not even attempted. (The logic is in the function that loops over basic blocks. Caller of this function).
https://github.com/llvm/llvm-project/pull/222962
More information about the llvm-commits
mailing list