[PATCH] D84295: [CallGraph] Preserve call records vector when replacing call edge

Sergey Dmitriev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 22 01:41:48 PDT 2020


sdmitriev created this revision.
sdmitriev added a reviewer: jdoerfert.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

Try not to resize vector of call records in a call graph node when
replacing call edge. That would prevent invalidation of iterators
stored in the CG SCC pass manager's scc_iterator.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D84295

Files:
  llvm/lib/Analysis/CallGraph.cpp


Index: llvm/lib/Analysis/CallGraph.cpp
===================================================================
--- llvm/lib/Analysis/CallGraph.cpp
+++ llvm/lib/Analysis/CallGraph.cpp
@@ -281,13 +281,37 @@
       I->second = NewNode;
       NewNode->AddRef();
 
-      // Refresh callback references.
-      forEachCallbackFunction(Call, [=](Function *CB) {
-        removeOneAbstractEdgeTo(CG->getOrInsertFunction(CB));
+      // Refresh callback references. Do not resize CalledFunctions if the
+      // number of callbacks is the same for new and old call sites.
+      SmallVector<CallGraphNode *, 4u> OldCBs;
+      SmallVector<CallGraphNode *, 4u> NewCBs;
+      forEachCallbackFunction(Call, [this, &OldCBs](Function *CB) {
+        OldCBs.push_back(CG->getOrInsertFunction(CB));
       });
-      forEachCallbackFunction(NewCall, [=](Function *CB) {
-        addCalledFunction(nullptr, CG->getOrInsertFunction(CB));
+      forEachCallbackFunction(NewCall, [this, &NewCBs](Function *CB) {
+        NewCBs.push_back(CG->getOrInsertFunction(CB));
       });
+      if (OldCBs.size() == NewCBs.size()) {
+        for (unsigned I = 0; I < OldCBs.size(); I++) {
+          CallGraphNode *OldNode = OldCBs[I];
+          CallGraphNode *NewNode = NewCBs[I];
+          for (auto I = CalledFunctions.begin();; ++I) {
+            assert(I != CalledFunctions.end() &&
+                   "Cannot find callsite to update!");
+            if (!I->first && I->second == OldNode) {
+              I->second = NewNode;
+              OldNode->DropRef();
+              NewNode->AddRef();
+              break;
+            }
+          }
+        }
+      } else {
+        for (auto *CGN : OldCBs)
+          removeOneAbstractEdgeTo(CGN);
+        for (auto *CGN : NewCBs)
+          addCalledFunction(nullptr, CGN);
+      }
       return;
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D84295.279723.patch
Type: text/x-patch
Size: 1843 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200722/bd166fef/attachment.bin>


More information about the llvm-commits mailing list