[llvm] [DeadStoreElimination] Optimize tautological assignments (PR #75744)
Shreyansh Chouhan via llvm-commits
llvm-commits at lists.llvm.org
Sun Dec 17 12:59:00 PST 2023
https://github.com/BK1603 updated https://github.com/llvm/llvm-project/pull/75744
>From f9eed0d56c02d4cca895f3f19bdd65e11d5272f0 Mon Sep 17 00:00:00 2001
From: Shreyansh Chouhan <chouhan.shreyansh2702 at gmail.com>
Date: Sun, 17 Dec 2023 22:48:51 +0530
Subject: [PATCH] [DeadStoreElimination] Optimize tautological assignments
If a store is immediately dominated by a condition that ensures that the value
being stored in a memory location is already present at that memory location,
consider the store a noop.
Fixes #63419
---
.../Scalar/DeadStoreElimination.cpp | 37 +++++++++++++++++++
.../DeadStoreElimination/noop-stores.ll | 24 ++++++++++++
2 files changed, 61 insertions(+)
diff --git a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
index dd0a290252dae3..8ec2a47542a831 100644
--- a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
@@ -1926,6 +1926,43 @@ struct DSEState {
if (InitC && InitC == StoredConstant)
return MSSA.isLiveOnEntryDef(
MSSA.getSkipSelfWalker()->getClobberingMemoryAccess(Def, BatchAA));
+
+ if (!Store)
+ return false;
+
+ // If there is a dominating condition, that ensures that the value
+ // being stored in a memory location is already present at the
+ // memory location, the store is a noop.
+ BasicBlock *StoreBB = DefI->getParent();
+ auto *StorePtr = Store->getOperand(1);
+
+ DomTreeNode *IDom = DT.getNode(StoreBB)->getIDom();
+ if (!IDom)
+ return false;
+
+ auto *TI = IDom->getBlock()->getTerminator();
+ ICmpInst::Predicate Pred;
+ BasicBlock *TrueBB, *FalseBB;
+
+ if (!match(TI, m_Br(m_ICmp(Pred, m_Load(m_Specific(StorePtr)),
+ m_Specific(StoredConstant)),
+ TrueBB, FalseBB)))
+ return false;
+
+ MemoryAccess *LastMod =
+ MSSA.getSkipSelfWalker()->getClobberingMemoryAccess(Def, BatchAA);
+
+ DomTreeNode *CDom = DT.getNode(LastMod->getBlock());
+ if (!DT.dominates(CDom, IDom))
+ return false;
+
+ if (Pred == ICmpInst::ICMP_EQ && StoreBB == TrueBB)
+ return true;
+
+ if (Pred == ICmpInst::ICMP_NE && StoreBB == FalseBB)
+ return true;
+
+ return false;
}
if (!Store)
diff --git a/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll b/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll
index 3703b8d039ead0..0fce7c9ba6868c 100644
--- a/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll
+++ b/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll
@@ -795,3 +795,27 @@ join:
store i8 %v, ptr %q, align 1
ret void
}
+
+ at x = global i32 0, align 4
+define void @remove_tautological_store() {
+; CHECK-LABEL: @remove_tautological_store(
+; CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr @x, align 4
+; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i32 [[TMP1]], 4
+; CHECK-NEXT: br i1 [[TMP2]], label [[TMP3:%.*]], label [[TMP4:%.*]]
+; CHECK: 3:
+; CHECK-NEXT: br label [[TMP4]]
+; CHECK: 4:
+; CHECK-NEXT: ret void
+;
+ %1 = load i32, ptr @x, align 4
+ %2 = icmp eq i32 %1, 4
+ br i1 %2, label %3, label %4
+
+3:
+ store i32 4, ptr @x, align 4
+ br label %4
+
+4:
+ ret void
+}
+
More information about the llvm-commits
mailing list