[llvm] [DSE] Use alias analysis to identify no-op stores (PR #122610)

via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 11 11:31:14 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Alex MacLean (AlexMaclean)

<details>
<summary>Changes</summary>



---
Full diff: https://github.com/llvm/llvm-project/pull/122610.diff


2 Files Affected:

- (modified) llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp (+3-1) 
- (modified) llvm/test/Transforms/DeadStoreElimination/noop-stores.ll (+11) 


``````````diff
diff --git a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
index cae5b9c41a37f1..908b9110bd595d 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<LoadInst>(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 9fc20d76da5eb4..5bedc1ebe8281e 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
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/122610


More information about the llvm-commits mailing list