[llvm] [LICM] Rewrite pointerInvalidatedByLoop sinking path to walk MemorySSA graph (PR #191469)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 10 10:05:56 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Sebastian Pop (sebpop)
<details>
<summary>Changes</summary>
Instead of scanning all blocks for MemoryDefs that could invalidate a use we want to sink, walk the MemorySSA use-def graph from the loop header's MemoryPhi. For each def found, check aliasing with the use via getModRefInfo, only rejecting when an aliasing def doesn't dominate the use.
This removes: the tooManyMemoryAccesses bailout for sinking, the pointerInvalidatedByBlock helper, and the two FIXMEs about imprecise checking. Non-aliasing defs no longer block sinking.
---
Full diff: https://github.com/llvm/llvm-project/pull/191469.diff
1 Files Affected:
- (modified) llvm/lib/Transforms/Scalar/LICM.cpp (+49-33)
``````````diff
diff --git a/llvm/lib/Transforms/Scalar/LICM.cpp b/llvm/lib/Transforms/Scalar/LICM.cpp
index 733c35ec797cd..a81dfd08da96f 100644
--- a/llvm/lib/Transforms/Scalar/LICM.cpp
+++ b/llvm/lib/Transforms/Scalar/LICM.cpp
@@ -197,8 +197,6 @@ static bool pointerInvalidatedByLoop(MemorySSA *MSSA, MemoryUse *MU,
Loop *CurLoop, Instruction &I,
SinkAndHoistLICMFlags &Flags,
bool InvariantGroup);
-static bool pointerInvalidatedByBlock(BasicBlock &BB, MemorySSA &MSSA,
- MemoryUse &MU);
/// Aggregates various functions for hoisting computations out of loop.
static bool hoistArithmetics(Instruction &I, Loop &L,
ICFLoopSafetyInfo &SafetyInfo,
@@ -2393,43 +2391,61 @@ static bool pointerInvalidatedByLoop(MemorySSA *MSSA, MemoryUse *MU,
!(InvariantGroup && Source->getBlock() == CurLoop->getHeader() && isa<MemoryPhi>(Source));
}
- // For sinking, we'd need to check all Defs below this use. The getClobbering
- // call will look on the backedge of the loop, but will check aliasing with
- // the instructions on the previous iteration.
- // For example:
- // for (i ... )
- // load a[i] ( Use (LoE)
- // store a[i] ( 1 = Def (2), with 2 = Phi for the loop.
- // i++;
- // The load sees no clobbering inside the loop, as the backedge alias check
- // does phi translation, and will check aliasing against store a[i-1].
- // However sinking the load outside the loop, below the store is incorrect.
-
- // For now, only sink if there are no Defs in the loop, and the existing ones
- // precede the use and are in the same block.
- // FIXME: Increase precision: Safe to sink if Use post dominates the Def;
- // needs PostDominatorTreeAnalysis.
- // FIXME: More precise: no Defs that alias this Use.
- if (Flags.tooManyMemoryAccesses())
- return true;
- for (auto *BB : CurLoop->getBlocks())
- if (pointerInvalidatedByBlock(*BB, *MSSA, *MU))
- return true;
+ // For sinking, check that no aliasing MemoryDef in the loop could execute
+ // after the use. Walk the MemorySSA graph from the loop header's MemoryPhi
+ // to find all defs, and reject only those that alias with MU.
+ BatchAAResults BAA(MSSA->getAA());
+ Instruction *UseInst = MU->getMemoryInst();
+ auto UseLoc = MemoryLocation::getOrNone(UseInst);
+
+ // Walk the MemorySSA graph from the loop header's MemoryPhi (if any).
+ auto *HeaderPhi = MSSA->getMemoryAccess(CurLoop->getHeader());
+ if (HeaderPhi) {
+ SmallVector<const MemoryAccess *, 8> Worklist;
+ SmallPtrSet<const MemoryAccess *, 8> Visited;
+ Worklist.push_back(HeaderPhi);
+ Visited.insert(HeaderPhi);
+
+ while (!Worklist.empty()) {
+ const MemoryAccess *MA = Worklist.pop_back_val();
+ for (const User *U : MA->users()) {
+ const auto *UserMA = cast<MemoryAccess>(U);
+ if (!Visited.insert(UserMA).second)
+ continue;
+ if (!CurLoop->contains(UserMA->getBlock()))
+ continue;
+
+ if (const auto *MD = dyn_cast<MemoryDef>(UserMA)) {
+ Instruction *DefInst = MD->getMemoryInst();
+ // Check if this def aliases with the use we want to sink.
+ if (isModSet(BAA.getModRefInfo(DefInst, UseLoc)))
+ // An aliasing def exists — can't sink past it unless it dominates
+ // the use in the same block.
+ if (MU->getBlock() != MD->getBlock() ||
+ !MSSA->locallyDominates(MD, MU))
+ return true;
+ Worklist.push_back(MD);
+ }
+
+ if (isa<MemoryPhi>(UserMA))
+ Worklist.push_back(UserMA);
+ }
+ }
+ }
+
// When sinking, the source block may not be part of the loop so check it.
if (!CurLoop->contains(&I))
- return pointerInvalidatedByBlock(*I.getParent(), *MSSA, *MU);
+ if (const auto *Accesses = MSSA->getBlockDefs(I.getParent()))
+ for (const auto &MA : *Accesses)
+ if (const auto *MD = dyn_cast<MemoryDef>(&MA))
+ if (isModSet(BAA.getModRefInfo(MD->getMemoryInst(), UseLoc)))
+ if (MU->getBlock() != MD->getBlock() ||
+ !MSSA->locallyDominates(MD, MU))
+ return true;
return false;
}
-bool pointerInvalidatedByBlock(BasicBlock &BB, MemorySSA &MSSA, MemoryUse &MU) {
- if (const auto *Accesses = MSSA.getBlockDefs(&BB))
- for (const auto &MA : *Accesses)
- if (const auto *MD = dyn_cast<MemoryDef>(&MA))
- if (MU.getBlock() != MD->getBlock() || !MSSA.locallyDominates(MD, &MU))
- return true;
- return false;
-}
/// Try to simplify things like (A < INV_1 AND icmp A < INV_2) into (A <
/// min(INV_1, INV_2)), if INV_1 and INV_2 are both loop invariants and their
``````````
</details>
https://github.com/llvm/llvm-project/pull/191469
More information about the llvm-commits
mailing list