[llvm] [LICM] Improve LICM when calls only change Inaccessible memory (PR #169379)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 23 03:28:52 PDT 2026


================
@@ -2332,8 +2332,15 @@ static bool noConflictingReadWrites(Instruction *I, MemorySSA *MSSA,
       if (const auto *MU = dyn_cast<MemoryUse>(&MA)) {
         auto *MD = getClobberingMemoryAccess(*MSSA, BAA, Flags,
                                              const_cast<MemoryUse *>(MU));
-        if (!MSSA->isLiveOnEntryDef(MD) && CurLoop->contains(MD->getBlock()))
-          return false;
+        if (!MSSA->isLiveOnEntryDef(MD) && CurLoop->contains(MD->getBlock())) {
+          auto *MDI = dyn_cast_or_null<MemoryDef>(MD);
+          // It checks only if I is clobbering.
+          // If IMD is not the same as I (that it wants to hoist), assumes they
+          // clobber
+          // Sync store is not allowed, but store hoist is allowed
+          if (!Flags.getIsSink() && (MDI && MDI->getMemoryInst() != I))
----------------
nikic wrote:

Looking at this again, I think a cleaner way to phrase this is that we should not check memory uses that are dominated by I in the first place. So I think that in the the loop we can add something like `if (!Flags.getIsSink() && MSSA->dominates(IMD, MA)) continue;`, skipping the clobber check entirely. I think this might also implicitly fix the issue you solved with the extra AA calls? Not sure about that.

The reasoning is: We have already checked that IMD is the only access writing the location in the loop. This means that it's safe to hoist if all aliasing uses are dominated by it. It's not safe if there are non-dominated uses, as these see the pre-store value on the first iteration.

https://github.com/llvm/llvm-project/pull/169379


More information about the llvm-commits mailing list