Skip to content

Commit bb70023

Browse files
authored
[MemoryLocation][DSE] Allow other read effects in MemoryLocation::getForDest() (#144343)
MemoryLocation::getForDest() returns a (potentially) written location, while still allowing other reads. Currently, this is limited to argmemonly functions. However, we can ignore other (non-argmem) read effects here for the same reason we can ignore argument reads. Fixes #144300. Proof: https://alive2.llvm.org/ce/z/LKq_dc
1 parent 80b79ce commit bb70023

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

llvm/lib/Analysis/MemoryLocation.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ MemoryLocation MemoryLocation::getForDest(const AnyMemIntrinsic *MI) {
111111

112112
std::optional<MemoryLocation>
113113
MemoryLocation::getForDest(const CallBase *CB, const TargetLibraryInfo &TLI) {
114-
if (!CB->onlyAccessesArgMemory())
114+
// Check that the only possible writes are to arguments.
115+
MemoryEffects WriteME = CB->getMemoryEffects() & MemoryEffects::writeOnly();
116+
if (!WriteME.onlyAccessesArgPointees())
115117
return std::nullopt;
116118

117119
if (CB->hasOperandBundles())

llvm/test/Transforms/DeadStoreElimination/trivial-dse-calls.ll

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,35 @@ define void @test_dse_non_alloca() {
286286
ret void
287287
}
288288

289+
define void @test_other_read_effects() {
290+
; CHECK-LABEL: @test_other_read_effects(
291+
; CHECK-NEXT: ret void
292+
;
293+
%a = alloca i32, align 4
294+
call void @f(ptr %a) memory(read, argmem: readwrite) nounwind willreturn
295+
ret void
296+
}
297+
298+
define i32 @test_other_read_effects_read_after() {
299+
; CHECK-LABEL: @test_other_read_effects_read_after(
300+
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
301+
; CHECK-NEXT: call void @f(ptr [[A]]) #[[ATTR5:[0-9]+]]
302+
; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[A]], align 4
303+
; CHECK-NEXT: ret i32 [[V]]
304+
;
305+
%a = alloca i32, align 4
306+
call void @f(ptr %a) memory(read, argmem: readwrite) nounwind willreturn
307+
%v = load i32, ptr %a
308+
ret i32 %v
309+
}
310+
311+
define void @test_other_write_effects() {
312+
; CHECK-LABEL: @test_other_write_effects(
313+
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
314+
; CHECK-NEXT: call void @f(ptr [[A]]) #[[ATTR6:[0-9]+]]
315+
; CHECK-NEXT: ret void
316+
;
317+
%a = alloca i32, align 4
318+
call void @f(ptr %a) memory(write, argmem: readwrite) nounwind willreturn
319+
ret void
320+
}

0 commit comments

Comments
 (0)