[llvm] febf415 - [GVN] Handle value-preserving stores in MSSA-based load elimination (#223793)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 23 01:25:02 PDT 2026
Author: Antonio Frighetto
Date: 2026-09-23T10:24:54+02:00
New Revision: febf4155e9856cca9e7719b833e365e794e225bd
URL: https://github.com/llvm/llvm-project/commit/febf4155e9856cca9e7719b833e365e794e225bd
DIFF: https://github.com/llvm/llvm-project/commit/febf4155e9856cca9e7719b833e365e794e225bd.diff
LOG: [GVN] Handle value-preserving stores in MSSA-based load elimination (#223793)
Following up on #68322, extend the MemorySSA-based redundant load
elimination path to recognize stores that write back a value previously
loaded from the same location. If nothing modifies the memory location
in between, such stores may be treated as non-clobbering, and the
subsequent load may be redundant.
Added:
Modified:
llvm/include/llvm/Transforms/Scalar/GVN.h
llvm/lib/Transforms/Scalar/GVN.cpp
llvm/test/Transforms/GVN/rle-clobbering-store.ll
Removed:
################################################################################
diff --git a/llvm/include/llvm/Transforms/Scalar/GVN.h b/llvm/include/llvm/Transforms/Scalar/GVN.h
index 19a9113f7ffaa..3867afffbf550 100644
--- a/llvm/include/llvm/Transforms/Scalar/GVN.h
+++ b/llvm/include/llvm/Transforms/Scalar/GVN.h
@@ -424,8 +424,8 @@ class GVNPass : public OptionalPassInfoMixin<GVNPass> {
std::optional<GVNPass::ReachingMemVal>
accessMayModifyLocation(MemoryAccess *ClobberMA, const MemoryLocation &Loc,
- bool IsInvariantLoad, BasicBlock *BB, MemorySSA &MSSA,
- BatchAAResults &AA);
+ Align LoadAlign, bool IsInvariantLoad, BasicBlock *BB,
+ MemorySSA &MSSA, BatchAAResults &AA);
bool collectPredecessors(BasicBlock *BB, const PHITransAddr &Addr,
MemoryAccess *ClobberMA, DependencyBlockSet &Blocks,
diff --git a/llvm/lib/Transforms/Scalar/GVN.cpp b/llvm/lib/Transforms/Scalar/GVN.cpp
index 7eee9a7b164aa..a9392998ede75 100644
--- a/llvm/lib/Transforms/Scalar/GVN.cpp
+++ b/llvm/lib/Transforms/Scalar/GVN.cpp
@@ -2402,8 +2402,8 @@ std::optional<GVNPass::ReachingMemVal> GVNPass::scanMemoryAccessesUsers(
/// Check if a given MemoryAccess (usually a MemoryDef) actually modifies a
/// given location. Returns a ReachingMemVal describing the dependency.
std::optional<GVNPass::ReachingMemVal> GVNPass::accessMayModifyLocation(
- MemoryAccess *ClobberMA, const MemoryLocation &Loc, bool IsInvariantLoad,
- BasicBlock *BB, MemorySSA &MSSA, BatchAAResults &AA) {
+ MemoryAccess *ClobberMA, const MemoryLocation &Loc, Align LoadAlign,
+ bool IsInvariantLoad, BasicBlock *BB, MemorySSA &MSSA, BatchAAResults &AA) {
assert(ClobberMA->getBlock() == BB);
// If the clobbering access is the entry memory state, we cannot say anything
@@ -2450,6 +2450,13 @@ std::optional<GVNPass::ReachingMemVal> GVNPass::accessMayModifyLocation(
if (!ClobberI->isAtomic() && isa<LoadInst>(ClobberI))
return std::nullopt;
+ // A store that writes back a value already at the memory location leaves
+ // the latter unchanged.
+ if (auto *SI = dyn_cast<StoreInst>(ClobberI))
+ if (isStorePreservingMemoryLocation(SI, Loc, LoadAlign, AA,
+ MaxNumInsnsPerBlock))
+ return std::nullopt;
+
if (AR == AliasResult::MayAlias ||
(AR == AliasResult::PartialAlias &&
(!AR.hasOffset() || AR.getOffset() < 0)))
@@ -2648,8 +2655,9 @@ bool GVNPass::findReachingValuesForLoad(LoadInst *L,
break;
// Check if the clobber actually aliases the load location.
- if (auto RMV = accessMayModifyLocation(ClobberMA, Loc, IsInvariantLoad,
- StartBlock, MSSA, AA)) {
+ if (auto RMV =
+ accessMayModifyLocation(ClobberMA, Loc, L->getAlign(),
+ IsInvariantLoad, StartBlock, MSSA, AA)) {
Values.emplace_back(*RMV);
return true;
}
@@ -2696,9 +2704,10 @@ bool GVNPass::findReachingValuesForLoad(LoadInst *L,
// predecessors of this block further, continue with the blocks in the
// worklist.
if (Info.ClobberMA->getBlock() == BB && !isa<MemoryPhi>(Info.ClobberMA)) {
- if (auto RMV = accessMayModifyLocation(
- Info.ClobberMA, Loc.getWithNewPtr(Info.Addr.getAddr()),
- IsInvariantLoad, BB, MSSA, AA)) {
+ const MemoryLocation BBLoc = Loc.getWithNewPtr(Info.Addr.getAddr());
+ if (auto RMV =
+ accessMayModifyLocation(Info.ClobberMA, BBLoc, L->getAlign(),
+ IsInvariantLoad, BB, MSSA, AA)) {
Info.MemVal = RMV;
continue;
}
diff --git a/llvm/test/Transforms/GVN/rle-clobbering-store.ll b/llvm/test/Transforms/GVN/rle-clobbering-store.ll
index b60f6c323d42d..3bfa09643e3ca 100644
--- a/llvm/test/Transforms/GVN/rle-clobbering-store.ll
+++ b/llvm/test/Transforms/GVN/rle-clobbering-store.ll
@@ -1,5 +1,6 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3
; RUN: opt < %s -passes=gvn -S | FileCheck %s
+; RUN: opt < %s -passes='gvn<memoryssa>' -S | FileCheck %s
define i1 @test_i1(ptr %a, ptr %b, ptr %c) {
; CHECK-LABEL: define i1 @test_i1(
@@ -126,3 +127,28 @@ entry:
%1 = load atomic i32, ptr %a unordered, align 4
ret i32 %1
}
+
+define i32 @test_nonlocal(i1 %c, ptr %a, ptr %b) {
+; CHECK-LABEL: define i32 @test_nonlocal(
+; CHECK-SAME: i1 [[C:%.*]], ptr [[A:%.*]], ptr [[B:%.*]]) {
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[VAL:%.*]] = load i32, ptr [[A]], align 4
+; CHECK-NEXT: store i32 [[VAL]], ptr [[B]], align 4
+; CHECK-NEXT: br i1 [[C]], label [[OTHER:%.*]], label [[EXIT:%.*]]
+; CHECK: other:
+; CHECK-NEXT: ret i32 1
+; CHECK: exit:
+; CHECK-NEXT: ret i32 [[VAL]]
+;
+entry:
+ %val = load i32, ptr %a, align 4
+ store i32 %val, ptr %b, align 4
+ br i1 %c, label %other, label %exit
+
+other:
+ ret i32 1
+
+exit:
+ %reuse = load i32, ptr %a, align 4
+ ret i32 %reuse
+}
More information about the llvm-commits
mailing list