[clang] [llvm] [IndVarSimplify] Sink unused l-invariant loads in preheader. (PR #157559)
Sirish Pande via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 9 13:00:21 PDT 2025
================
@@ -1100,14 +1112,32 @@ bool IndVarSimplify::sinkUnusedInvariants(Loop *L) {
break;
// Don't move instructions which might have side effects, since the side
- // effects need to complete before instructions inside the loop. Also don't
- // move instructions which might read memory, since the loop may modify
- // memory. Note that it's okay if the instruction might have undefined
- // behavior: LoopSimplify guarantees that the preheader dominates the exit
- // block.
- if (I.mayHaveSideEffects() || I.mayReadFromMemory())
+ // effects need to complete before instructions inside the loop. Note that
+ // it's okay if the instruction might have undefined behavior: LoopSimplify
+ // guarantees that the preheader dominates the exit block.
+ if (I.mayHaveSideEffects())
continue;
+ // Don't sink read instruction which's memory might be modified in the loop.
+ if (I.mayReadFromMemory()) {
+ if (LoadInst *Load = dyn_cast<LoadInst>(&I)) {
+ MemoryLocation Loc = MemoryLocation::get(Load);
+ bool isModified = false;
+
+ // Check if any store instruction in the loop modifies the loaded memory
+ // location.
+ for (Instruction *S : Stores) {
+ if (isModSet(AA->getModRefInfo(S, Loc))) {
+ isModified = true;
+ break;
+ }
+ }
+ if (isModified)
+ continue;
+ } else {
----------------
srpande wrote:
Please add comment for clarity.
https://github.com/llvm/llvm-project/pull/157559
More information about the llvm-commits
mailing list