Skip to content

Commit 6c25816

Browse files
committed
[DSE] Look through memory PHI arguments when removing noop stores in MSSA.
Summary: Adds support for "following" memory through MSSA PHI arguments. This will help catch more noop stores that exist between blocks. Originally part of D79391. Reviewers: fhahn, jfb, asbirlea Differential Revision: https://reviews.llvm.org/D82588
1 parent a0119e5 commit 6c25816

File tree

3 files changed

+128
-30
lines changed

3 files changed

+128
-30
lines changed

llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2404,10 +2404,44 @@ struct DSEState {
24042404

24052405
if (auto *LoadI = dyn_cast<LoadInst>(Store->getOperand(0))) {
24062406
if (LoadI->getPointerOperand() == Store->getOperand(1)) {
2407+
// Get the defining access for the load.
24072408
auto *LoadAccess = MSSA.getMemoryAccess(LoadI)->getDefiningAccess();
2408-
// If both accesses share the same defining access, no instructions
2409-
// between them can modify the memory location.
2410-
return LoadAccess == Def->getDefiningAccess();
2409+
// Fast path: the defining accesses are the same.
2410+
if (LoadAccess == Def->getDefiningAccess())
2411+
return true;
2412+
2413+
// Look through phi accesses. Recursively scan all phi accesses by
2414+
// adding them to a worklist. Bail when we run into a memory def that
2415+
// does not match LoadAccess.
2416+
SetVector<MemoryAccess *> ToCheck;
2417+
MemoryAccess *Current = Def->getDefiningAccess();
2418+
// We don't want to bail when we run into the store memory def. But,
2419+
// the phi access may point to it. So, pretend like we've already
2420+
// checked it.
2421+
ToCheck.insert(Def);
2422+
ToCheck.insert(Current);
2423+
// Start at current (1) to simulate already having checked Def.
2424+
for (unsigned I = 1; I < ToCheck.size(); ++I) {
2425+
Current = ToCheck[I];
2426+
if (auto PhiAccess = dyn_cast<MemoryPhi>(Current)) {
2427+
// Check all the operands.
2428+
for (auto &Use : PhiAccess->incoming_values())
2429+
ToCheck.insert(cast<MemoryAccess>(&Use));
2430+
continue;
2431+
}
2432+
2433+
// If we found a memory def, bail. This happens when we have an
2434+
// unrelated write in between an otherwise noop store.
2435+
assert(isa<MemoryDef>(Current) &&
2436+
"Only MemoryDefs should reach here.");
2437+
// TODO: Skip no alias MemoryDefs that have no aliasing reads.
2438+
// We are searching for the definition of the store's destination.
2439+
// So, if that is the same definition as the load, then this is a
2440+
// noop. Otherwise, fail.
2441+
if (LoadAccess != Current)
2442+
return false;
2443+
}
2444+
return true;
24112445
}
24122446
}
24132447

llvm/test/Transforms/DeadStoreElimination/MSSA/noop-stores.ll

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,47 @@ bb3:
101101
ret i32 0
102102
}
103103

104+
; Remove redundant store if loaded value is in another block inside a loop.
105+
define i32 @test31(i1 %c, i32* %p, i32 %i) {
106+
; CHECK-LABEL: @test31(
107+
; CHECK-NEXT: entry:
108+
; CHECK-NEXT: br label [[BB1:%.*]]
109+
; CHECK: bb1:
110+
; CHECK-NEXT: br i1 [[C:%.*]], label [[BB1]], label [[BB2:%.*]]
111+
; CHECK: bb2:
112+
; CHECK-NEXT: ret i32 0
113+
;
114+
entry:
115+
%v = load i32, i32* %p, align 4
116+
br label %bb1
117+
bb1:
118+
store i32 %v, i32* %p, align 4
119+
br i1 %c, label %bb1, label %bb2
120+
bb2:
121+
ret i32 0
122+
}
123+
124+
; Don't remove "redundant" store if %p is possibly stored to.
125+
define i32 @test46(i1 %c, i32* %p, i32* %p2, i32 %i) {
126+
; CHECK-LABEL: @test46(
127+
; CHECK: load
128+
; CHECK: store
129+
; CHECK: store
130+
; CHECK: ret i32 0
131+
;
132+
entry:
133+
%v = load i32, i32* %p, align 4
134+
br label %bb1
135+
bb1:
136+
store i32 %v, i32* %p, align 4
137+
br i1 %c, label %bb1, label %bb2
138+
bb2:
139+
store i32 0, i32* %p2, align 4
140+
br i1 %c, label %bb3, label %bb1
141+
bb3:
142+
ret i32 0
143+
}
144+
104145
declare void @unknown_func()
105146

106147
; Remove redundant store, which is in the lame loop as the load.
@@ -112,7 +153,7 @@ define i32 @test33(i1 %c, i32* %p, i32 %i) {
112153
; CHECK-NEXT: br label [[BB2:%.*]]
113154
; CHECK: bb2:
114155
; CHECK-NEXT: call void @unknown_func()
115-
; CHECK-NEXT: br i1 undef, label [[BB1]], label [[BB3:%.*]]
156+
; CHECK-NEXT: br i1 [[C:%.*]], label [[BB1]], label [[BB3:%.*]]
116157
; CHECK: bb3:
117158
; CHECK-NEXT: ret i32 0
118159
;
@@ -125,7 +166,7 @@ bb2:
125166
store i32 %v, i32* %p, align 4
126167
; Might read and overwrite value at %p, but doesn't matter.
127168
call void @unknown_func()
128-
br i1 undef, label %bb1, label %bb3
169+
br i1 %c, label %bb1, label %bb3
129170
bb3:
130171
ret i32 0
131172
}
@@ -168,4 +209,52 @@ define void @test45(i32* %Q) {
168209
ret void
169210
}
170211

212+
define i32 @test48(i1 %c, i32* %p) {
213+
; CHECK-LABEL: @test48(
214+
; CHECK: entry:
215+
; CHECK-NEXT: [[V:%.*]] = load
216+
; CHECK: store i32 0
217+
; CHECK: store i32 [[V]]
218+
; CHECK: ret i32 0
219+
entry:
220+
%v = load i32, i32* %p, align 4
221+
br i1 %c, label %bb0, label %bb0.0
222+
223+
bb0:
224+
store i32 0, i32* %p
225+
br i1 %c, label %bb1, label %bb2
226+
227+
bb0.0:
228+
br label %bb1
229+
230+
bb1:
231+
store i32 %v, i32* %p, align 4
232+
br i1 %c, label %bb2, label %bb0
233+
bb2:
234+
ret i32 0
235+
}
236+
237+
; TODO: Remove both redundant stores if loaded value is in another block inside a loop.
238+
define i32 @test47(i1 %c, i32* %p, i32 %i) {
239+
; X-CHECK-LABEL: @test47(
240+
; X-CHECK-NEXT: entry:
241+
; X-CHECK-NEXT: br label [[BB1:%.*]]
242+
; X-CHECK: bb1:
243+
; X-CHECK-NEXT: br i1 [[C:%.*]], label [[BB1]], label [[BB2:%.*]]
244+
; X-CHECK: bb2:
245+
; X-CHECK-NEXT: br i1 [[C]], label [[BB2]], label [[BB3:%.*]]
246+
; X-CHECK: bb3:
247+
; X-CHECK-NEXT: ret i32 0
248+
entry:
249+
%v = load i32, i32* %p, align 4
250+
br label %bb1
251+
bb1:
252+
store i32 %v, i32* %p, align 4
253+
br i1 %c, label %bb1, label %bb2
254+
bb2:
255+
store i32 %v, i32* %p, align 4
256+
br i1 %c, label %bb3, label %bb1
257+
bb3:
258+
ret i32 0
259+
}
171260

llvm/test/Transforms/DeadStoreElimination/MSSA/simple-todo.ll

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)