[PATCH] D77854: [CallGraphUpdater] Update the ExternalCallingNode for node replacements

Johannes Doerfert via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 9 20:08:27 PDT 2020


jdoerfert created this revision.
jdoerfert added reviewers: lebedev.ri, hfinkel, fhahn, probinson, wristow, loladiro.
Herald added subscribers: uenoku, bollu, hiraditya.
Herald added a reviewer: sstefan1.
Herald added a reviewer: uenoku.
Herald added a project: LLVM.

While it is uncommon that the ExternalCallingNode needs to be updated,
it can happen. It is uncommon because most functions listed as callees
have external linkage, modifying them is usually not allowed. That said,
there are also internal functions that have, or better had, their
"address taken" at construction time. We conservatively assume various
uses cause the address "to be taken". Furthermore, the user might have
become dead at some point. As a consequence, transformations, e.g., the
Attributor, might be able to replace a function that is listed
as callee of the ExternalCallingNode.

Since there is no function corresponding to the ExternalCallingNode, we
did just remove the node from the callee list if we replaced it (so
far). Now it would be preferable to replace it if needed and remove it
otherwise. However, removing the node has implications on the CGSCC
iteration. Locally, that caused some other nodes to be never visited
but it is for sure possible other (bad) side effects can occur. As it
seems conservatively safe to keep the new node in the callee list we
will do that for now.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D77854

Files:
  llvm/include/llvm/Analysis/CallGraph.h
  llvm/lib/Analysis/CallGraph.cpp
  llvm/lib/Transforms/Utils/CallGraphUpdater.cpp


Index: llvm/lib/Transforms/Utils/CallGraphUpdater.cpp
===================================================================
--- llvm/lib/Transforms/Utils/CallGraphUpdater.cpp
+++ llvm/lib/Transforms/Utils/CallGraphUpdater.cpp
@@ -114,6 +114,7 @@
     CallGraphNode *OldCGN = (*CG)[&OldFn];
     CallGraphNode *NewCGN = CG->getOrInsertFunction(&NewFn);
     NewCGN->stealCalledFunctionsFrom(OldCGN);
+    CG->ReplaceExternalCallEdge(OldCGN, NewCGN);
 
     // And update the SCC we're iterating as well.
     CGSCC->ReplaceNode(OldCGN, NewCGN);
Index: llvm/lib/Analysis/CallGraph.cpp
===================================================================
--- llvm/lib/Analysis/CallGraph.cpp
+++ llvm/lib/Analysis/CallGraph.cpp
@@ -127,6 +127,16 @@
 LLVM_DUMP_METHOD void CallGraph::dump() const { print(dbgs()); }
 #endif
 
+void CallGraph::ReplaceExternalCallEdge(CallGraphNode *Old,
+                                        CallGraphNode *New) {
+  for (auto &CR : ExternalCallingNode->CalledFunctions)
+    if (CR.second == Old) {
+      CR.second->DropRef();
+      CR.second = New;
+      CR.second->AddRef();
+    }
+}
+
 // removeFunctionFromModule - Unlink the function from this module, returning
 // it.  Because this removes the function from the module, the call graph node
 // is destroyed.  This is only valid if the function does not call any other
Index: llvm/include/llvm/Analysis/CallGraph.h
===================================================================
--- llvm/include/llvm/Analysis/CallGraph.h
+++ llvm/include/llvm/Analysis/CallGraph.h
@@ -138,6 +138,10 @@
     return CallsExternalNode.get();
   }
 
+  /// Old node has been deleted, and New is to be used in its place, update the
+  /// ExternalCallingNode.
+  void ReplaceExternalCallEdge(CallGraphNode *Old, CallGraphNode *New);
+
   //===---------------------------------------------------------------------
   // Functions to keep a call graph up to date with a function that has been
   // modified.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D77854.256491.patch
Type: text/x-patch
Size: 1979 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200410/806ba484/attachment-0001.bin>


More information about the llvm-commits mailing list