[llvm] a1bf4dd - [DSE] Generalize store null to calloc allocated memory [NFC-ish]

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 11 12:03:05 PST 2022


Author: Philip Reames
Date: 2022-01-11T12:02:51-08:00
New Revision: a1bf4ddac6e493e9010984649f78403f993e56b4

URL: https://github.com/llvm/llvm-project/commit/a1bf4ddac6e493e9010984649f78403f993e56b4
DIFF: https://github.com/llvm/llvm-project/commit/a1bf4ddac6e493e9010984649f78403f993e56b4.diff

LOG: [DSE] Generalize store null to calloc allocated memory [NFC-ish]

This change removes a direct check for calloc-like allocation functions, and instead handles the generic case where we're storing a constant to constant initialized memory.  This is mostly to remove the call to isCallocLike, but if someone downstream happens to have an initialized alloc which initializes to e.g. -1, this will also kick in for them.  (I don't know of such an example ftr.)

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
index d128884422f34..8eb9bbf924075 100644
--- a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
@@ -1708,19 +1708,23 @@ struct DSEState {
     if (!isRemovable(DefI))
       return false;
 
+    if (StoredConstant && isAllocationFn(DefUO, &TLI)) {
+      auto *CB = cast<CallBase>(DefUO);
+      auto *InitC = getInitialValueOfAllocation(CB, &TLI,
+                                                StoredConstant->getType());
+      if (InitC && InitC == StoredConstant) {
+        auto *UnderlyingDef = cast<MemoryDef>(MSSA.getMemoryAccess(CB));
+        // If UnderlyingDef is the clobbering access of Def, no instructions
+        // between them can modify the memory location.
+        auto *ClobberDef =
+          MSSA.getSkipSelfWalker()->getClobberingMemoryAccess(Def);
+        return UnderlyingDef == ClobberDef;
+      }
+    }
+
     if (StoredConstant && StoredConstant->isNullValue()) {
       auto *DefUOInst = dyn_cast<Instruction>(DefUO);
       if (DefUOInst) {
-        if (isCallocLikeFn(DefUOInst, &TLI)) {
-          auto *UnderlyingDef =
-              cast<MemoryDef>(MSSA.getMemoryAccess(DefUOInst));
-          // If UnderlyingDef is the clobbering access of Def, no instructions
-          // between them can modify the memory location.
-          auto *ClobberDef =
-              MSSA.getSkipSelfWalker()->getClobberingMemoryAccess(Def);
-          return UnderlyingDef == ClobberDef;
-        }
-
         if (MemSet) {
           if (F.hasFnAttribute(Attribute::SanitizeMemory) ||
               F.hasFnAttribute(Attribute::SanitizeAddress) ||


        


More information about the llvm-commits mailing list