[llvm] [DeadStoreElimination] Optimize tautological assignments (PR #75744)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 21 12:06:39 PST 2023


================
@@ -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());
----------------
nikic wrote:

Yes, this is expected, but only possible because the conditional store to `@x` can be speculated in this case.

Try replacing the clobbering store with something like `call void @clobber()` that may or may not overwrite the global in some unanalyzable way.

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


More information about the llvm-commits mailing list