diff --git a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp index cae5b9c41a37f..908b9110bd595 100644 --- a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp +++ b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp @@ -2116,7 +2116,9 @@ struct DSEState { return true; if (auto *LoadI = dyn_cast(Store->getOperand(0))) { - if (LoadI->getPointerOperand() == Store->getOperand(1)) { + if (LoadI->getPointerOperand() == Store->getOperand(1) || + AA.isMustAlias(MemoryLocation::get(LoadI), + MemoryLocation::get(Store))) { // Get the defining access for the load. auto *LoadAccess = MSSA.getMemoryAccess(LoadI)->getDefiningAccess(); // Fast path: the defining accesses are the same. diff --git a/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll b/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll index 9fc20d76da5eb..5bedc1ebe8281 100644 --- a/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll +++ b/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll @@ -1152,3 +1152,14 @@ if.else: end: ret void } + +define i32 @test_use_alias_analysis(ptr %Q) { +; CHECK-LABEL: @test_use_alias_analysis( +; CHECK-NEXT: [[A:%.*]] = load i32, ptr [[Q:%.*]], align 4 +; CHECK-NEXT: ret i32 [[A]] +; + %a = load i32, ptr %Q + %Q2 = addrspacecast ptr %Q to ptr addrspace(5) + store i32 %a, ptr addrspace(5) %Q2 + ret i32 %a +}