[llvm] [DeadStoreElimination] Optimize tautological assignments (PR #75744)
Shreyansh Chouhan via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 2 11:07:03 PST 2024
https://github.com/BK1603 updated https://github.com/llvm/llvm-project/pull/75744
>From ad7578eddb99c93705c70f89e09fa37ffb40e697 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 | 60 ++++++++++++++++++
.../DeadStoreElimination/noop-stores.ll | 62 +++++++++++++++++++
2 files changed, 122 insertions(+)
diff --git a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
index 008dcc53fd44fce..584e7e72908121e 100644
--- a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
@@ -1901,6 +1901,63 @@ struct DSEState {
return true;
}
+ // Check if there is a dominating condition, that implies that the value
+ // being stored in a ptr is already present in the ptr.
+ bool dominatingConditionImpliesValue(MemoryDef *Def) {
+ StoreInst *StoreI = dyn_cast<StoreInst>(Def->getMemoryInst());
+ BasicBlock *StoreBB = StoreI->getParent();
+ Value *StorePtr = StoreI->getPointerOperand();
+ Value *StoreVal = StoreI->getValueOperand();
+
+ DomTreeNode *IDom = DT.getNode(StoreBB)->getIDom();
+ if (!IDom)
+ return false;
+
+ auto *BI = dyn_cast<BranchInst>(IDom->getBlock()->getTerminator());
+ if (!BI || !BI->isConditional())
+ return false;
+
+ // In case both blocks are the same, we cannot optimize the FalseBB when the
+ // condition is true and vice versa. Return false in this case.
+ if (BI->getSuccessor(0) == BI->getSuccessor(1))
+ return false;
+
+ // Load can be either of the two operands
+ ICmpInst *CI = dyn_cast<ICmpInst>(BI->getCondition());
+ if (!CI || !CI->isEquality())
+ return false;
+ // Check if load is from the operand that we are storing to.
+ LoadInst *ICmpL = nullptr;
+ Value *ICmpV = nullptr;
+ for (Value *Op : CI->operands()) {
+ LLVM_DEBUG(dbgs() << "Operand: " << *Op << "\n");
+ auto *LI = dyn_cast<LoadInst>(Op);
+ if (LI && LI->getPointerOperand() == StorePtr)
+ ICmpL = LI;
+ if (Op == StoreVal)
+ ICmpV = Op;
+ }
+
+ if (!ICmpL || !ICmpV)
+ return false;
+
+ MemoryAccess *LoadAcc, *ClobAcc;
+ LoadAcc = MSSA.getMemoryAccess(ICmpL)->getDefiningAccess();
+ ClobAcc = MSSA.getSkipSelfWalker()->getClobberingMemoryAccess(Def, BatchAA);
+
+ if (!MSSA.dominates(ClobAcc, LoadAcc))
+ return false;
+
+ ICmpInst::Predicate Pred = CI->getPredicate();
+ if (Pred == ICmpInst::ICMP_EQ && StoreBB == BI->getSuccessor(0))
+ return true;
+
+ if (Pred == ICmpInst::ICMP_NE && StoreBB == BI->getSuccessor(1))
+ return true;
+
+ return false;
+ }
+
/// \returns true if \p Def is a no-op store, either because it
/// directly stores back a loaded value or stores zero to a calloced object.
bool storeIsNoop(MemoryDef *Def, const Value *DefUO) {
@@ -1931,6 +1988,9 @@ struct DSEState {
if (!Store)
return false;
+ if (dominatingConditionImpliesValue(Def))
+ return true;
+
if (auto *LoadI = dyn_cast<LoadInst>(Store->getOperand(0))) {
if (LoadI->getPointerOperand() == Store->getOperand(1)) {
// Get the defining access for the load.
diff --git a/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll b/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll
index 3703b8d039ead03..4457382f537c026 100644
--- a/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll
+++ b/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll
@@ -795,3 +795,65 @@ join:
store i8 %v, ptr %q, align 1
ret void
}
+
+; If the condition ensures that the value is already present in
+; in the variable, treat the store as noop and optimize. Should
+; not optimize stores if a clobber happens after the load that
+; is used in condition check.
+define void @remove_tautological_store(ptr %x, ptr %y, ptr %z) {
+; CHECK-LABEL: @remove_tautological_store(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[VALX:%.*]] = load i32, ptr [[X:%.*]], align 4
+; CHECK-NEXT: [[CMPX:%.*]] = icmp eq i32 [[VALX]], 4
+; CHECK-NEXT: br i1 [[CMPX]], label [[IF_X:%.*]], label [[END:%.*]]
+; CHECK: test.y:
+; CHECK-NEXT: [[VALY:%.*]] = load i32, ptr [[Y:%.*]], align 4
+; CHECK-NEXT: store i32 5, ptr [[Y]], align 4
+; CHECK-NEXT: [[CMPY:%.*]] = icmp eq i32 [[VALY]], 4
+; CHECK-NEXT: br i1 [[CMPY]], label [[IF_Y:%.*]], label [[END]]
+; CHECK: test.z:
+; CHECK-NEXT: [[VALZ:%.*]] = load i32, ptr [[Z:%.*]], align 4
+; CHECK-NEXT: [[CMPZ:%.*]] = icmp eq i32 [[VALZ]], 4
+; CHECK-NEXT: br i1 [[CMPZ]], label [[IF_Z:%.*]], label [[END]]
+; CHECK: if.x:
+; CHECK-NEXT: br label [[TEST_Y:%.*]]
+; CHECK: if.y:
+; CHECK-NEXT: store i32 4, ptr [[Y]], align 4
+; CHECK-NEXT: br label [[TEST_Z:%.*]]
+; CHECK: if.z:
+; CHECK-NEXT: store i32 5, ptr [[Z]], align 4
+; CHECK-NEXT: br label [[END]]
+; CHECK: end:
+; CHECK-NEXT: ret void
+;
+entry:
+ %valx = load i32, ptr %x, align 4
+ %cmpx = icmp eq i32 %valx, 4
+ br i1 %cmpx, label %if.x, label %end
+
+test.y:
+ %valy = load i32, ptr %y, align 4
+ store i32 5, ptr %y, align 4
+ %cmpy = icmp eq i32 %valy, 4
+ br i1 %cmpy, label %if.y, label %end
+
+test.z:
+ %valz = load i32, ptr %z, align 4
+ %cmpz = icmp eq i32 %valz, 4
+ br i1 %cmpz, label %if.z, label %end
+
+if.x:
+ store i32 4, ptr %x, align 4
+ br label %test.y
+
+if.y:
+ store i32 4, ptr %y, align 4
+ br label %test.z
+
+if.z:
+ store i32 5, ptr %z, align 4
+ br label %end
+
+end:
+ ret void
+}
More information about the llvm-commits
mailing list