[PATCH] D114872: [MemoryLocation] Move DSE's logic to new MemLoc::getForDest helper (NFC).

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 1 06:20:47 PST 2021


fhahn created this revision.
fhahn added reviewers: fwolff, xbolva00, efriedma, reames, nikic.
Herald added a subscriber: hiraditya.
fhahn requested review of this revision.
Herald added a project: LLVM.

DSE has some extra logic to determine the write location of library
calls like str*cpy and str*cat. This patch moves the logic to a new
MemoryLocation:getForDest variant, which takes a call and TLI.

This patch should be NFC, because no other places take advantage of the
new helper yet.

Suggested by @reames post-commit 7eec832def571 <https://reviews.llvm.org/rG7eec832def5717b1bddb72c3b99c3df4f7a2f6da>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D114872

Files:
  llvm/include/llvm/Analysis/MemoryLocation.h
  llvm/lib/Analysis/MemoryLocation.cpp
  llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp


Index: llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
+++ llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
@@ -1026,32 +1026,12 @@
     if (!I->mayWriteToMemory())
       return None;
 
-    if (auto *MTI = dyn_cast<AnyMemIntrinsic>(I))
-      return {MemoryLocation::getForDest(MTI)};
-
     if (auto *CB = dyn_cast<CallBase>(I)) {
       // If the functions may write to memory we do not know about, bail out.
       if (!CB->onlyAccessesArgMemory() &&
           !CB->onlyAccessesInaccessibleMemOrArgMem())
         return None;
 
-      LibFunc LF;
-      if (TLI.getLibFunc(*CB, LF) && TLI.has(LF)) {
-        switch (LF) {
-        case LibFunc_strncpy:
-          if (const auto *Len = dyn_cast<ConstantInt>(CB->getArgOperand(2)))
-            return MemoryLocation(CB->getArgOperand(0),
-                                  LocationSize::precise(Len->getZExtValue()),
-                                  CB->getAAMetadata());
-          LLVM_FALLTHROUGH;
-        case LibFunc_strcpy:
-        case LibFunc_strcat:
-        case LibFunc_strncat:
-          return {MemoryLocation::getAfter(CB->getArgOperand(0))};
-        default:
-          break;
-        }
-      }
       switch (CB->getIntrinsicID()) {
       case Intrinsic::init_trampoline:
         return {MemoryLocation::getAfter(CB->getArgOperand(0))};
@@ -1060,7 +1040,8 @@
       default:
         break;
       }
-      return None;
+
+      return MemoryLocation::getForDest(CB, TLI);
     }
 
     return MemoryLocation::getOrNone(I);
Index: llvm/lib/Analysis/MemoryLocation.cpp
===================================================================
--- llvm/lib/Analysis/MemoryLocation.cpp
+++ llvm/lib/Analysis/MemoryLocation.cpp
@@ -128,6 +128,27 @@
   return MemoryLocation(MI->getRawDest(), Size, MI->getAAMetadata());
 }
 
+Optional<MemoryLocation>
+MemoryLocation::getForDest(const CallBase *CB, const TargetLibraryInfo &TLI) {
+  if (auto *MemInst = dyn_cast<AnyMemIntrinsic>(CB))
+    return getForDest(MemInst);
+
+  LibFunc LF;
+  if (TLI.getLibFunc(*CB, LF) && TLI.has(LF)) {
+    switch (LF) {
+    case LibFunc_strncpy:
+    case LibFunc_strcpy:
+    case LibFunc_strcat:
+    case LibFunc_strncat:
+      return getForArgument(CB, 0, &TLI);
+    default:
+      break;
+    }
+  }
+
+  return {};
+}
+
 MemoryLocation MemoryLocation::getForArgument(const CallBase *Call,
                                               unsigned ArgIdx,
                                               const TargetLibraryInfo *TLI) {
Index: llvm/include/llvm/Analysis/MemoryLocation.h
===================================================================
--- llvm/include/llvm/Analysis/MemoryLocation.h
+++ llvm/include/llvm/Analysis/MemoryLocation.h
@@ -253,6 +253,8 @@
   static MemoryLocation getForDest(const MemIntrinsic *MI);
   static MemoryLocation getForDest(const AtomicMemIntrinsic *MI);
   static MemoryLocation getForDest(const AnyMemIntrinsic *MI);
+  static Optional<MemoryLocation> getForDest(const CallBase *CI,
+                                             const TargetLibraryInfo &TLI);
 
   /// Return a location representing a particular argument of a call.
   static MemoryLocation getForArgument(const CallBase *Call, unsigned ArgIdx,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114872.391003.patch
Type: text/x-patch
Size: 3352 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211201/83dadea3/attachment.bin>


More information about the llvm-commits mailing list