CallGraphUpdater.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //===- CallGraphUpdater.cpp - A (lazy) call graph update helper -----------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. /// \file
  9. ///
  10. /// This file provides interfaces used to manipulate a call graph, regardless
  11. /// if it is a "old style" CallGraph or an "new style" LazyCallGraph.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/Transforms/Utils/CallGraphUpdater.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/Transforms/Utils/ModuleUtils.h"
  17. using namespace llvm;
  18. bool CallGraphUpdater::finalize() {
  19. if (!DeadFunctionsInComdats.empty()) {
  20. filterDeadComdatFunctions(*DeadFunctionsInComdats.front()->getParent(),
  21. DeadFunctionsInComdats);
  22. DeadFunctions.append(DeadFunctionsInComdats.begin(),
  23. DeadFunctionsInComdats.end());
  24. }
  25. if (CG) {
  26. // First remove all references, e.g., outgoing via called functions. This is
  27. // necessary as we can delete functions that have circular references.
  28. for (Function *DeadFn : DeadFunctions) {
  29. DeadFn->removeDeadConstantUsers();
  30. CallGraphNode *DeadCGN = (*CG)[DeadFn];
  31. DeadCGN->removeAllCalledFunctions();
  32. CG->getExternalCallingNode()->removeAnyCallEdgeTo(DeadCGN);
  33. DeadFn->replaceAllUsesWith(UndefValue::get(DeadFn->getType()));
  34. }
  35. // Then remove the node and function from the module.
  36. for (Function *DeadFn : DeadFunctions) {
  37. CallGraphNode *DeadCGN = CG->getOrInsertFunction(DeadFn);
  38. assert(DeadCGN->getNumReferences() == 0 &&
  39. "References should have been handled by now");
  40. delete CG->removeFunctionFromModule(DeadCGN);
  41. }
  42. } else {
  43. // This is the code path for the new lazy call graph and for the case were
  44. // no call graph was provided.
  45. for (Function *DeadFn : DeadFunctions) {
  46. DeadFn->removeDeadConstantUsers();
  47. DeadFn->replaceAllUsesWith(UndefValue::get(DeadFn->getType()));
  48. if (LCG && !ReplacedFunctions.count(DeadFn)) {
  49. // Taken mostly from the inliner:
  50. LazyCallGraph::Node &N = LCG->get(*DeadFn);
  51. auto *DeadSCC = LCG->lookupSCC(N);
  52. assert(DeadSCC && DeadSCC->size() == 1 &&
  53. &DeadSCC->begin()->getFunction() == DeadFn);
  54. auto &DeadRC = DeadSCC->getOuterRefSCC();
  55. FunctionAnalysisManager &FAM =
  56. AM->getResult<FunctionAnalysisManagerCGSCCProxy>(*DeadSCC, *LCG)
  57. .getManager();
  58. FAM.clear(*DeadFn, DeadFn->getName());
  59. AM->clear(*DeadSCC, DeadSCC->getName());
  60. LCG->removeDeadFunction(*DeadFn);
  61. // Mark the relevant parts of the call graph as invalid so we don't
  62. // visit them.
  63. UR->InvalidatedSCCs.insert(DeadSCC);
  64. UR->InvalidatedRefSCCs.insert(&DeadRC);
  65. }
  66. // The function is now really dead and de-attached from everything.
  67. DeadFn->eraseFromParent();
  68. }
  69. }
  70. bool Changed = !DeadFunctions.empty();
  71. DeadFunctionsInComdats.clear();
  72. DeadFunctions.clear();
  73. return Changed;
  74. }
  75. void CallGraphUpdater::reanalyzeFunction(Function &Fn) {
  76. if (CG) {
  77. CallGraphNode *OldCGN = CG->getOrInsertFunction(&Fn);
  78. OldCGN->removeAllCalledFunctions();
  79. CG->populateCallGraphNode(OldCGN);
  80. } else if (LCG) {
  81. LazyCallGraph::Node &N = LCG->get(Fn);
  82. LazyCallGraph::SCC *C = LCG->lookupSCC(N);
  83. updateCGAndAnalysisManagerForCGSCCPass(*LCG, *C, N, *AM, *UR, *FAM);
  84. }
  85. }
  86. void CallGraphUpdater::registerOutlinedFunction(Function &OriginalFn,
  87. Function &NewFn) {
  88. if (CG)
  89. CG->addToCallGraph(&NewFn);
  90. else if (LCG)
  91. LCG->addSplitFunction(OriginalFn, NewFn);
  92. }
  93. void CallGraphUpdater::removeFunction(Function &DeadFn) {
  94. DeadFn.deleteBody();
  95. DeadFn.setLinkage(GlobalValue::ExternalLinkage);
  96. if (DeadFn.hasComdat())
  97. DeadFunctionsInComdats.push_back(&DeadFn);
  98. else
  99. DeadFunctions.push_back(&DeadFn);
  100. // For the old call graph we remove the function from the SCC right away.
  101. if (CG && !ReplacedFunctions.count(&DeadFn)) {
  102. CallGraphNode *DeadCGN = (*CG)[&DeadFn];
  103. DeadCGN->removeAllCalledFunctions();
  104. CGSCC->DeleteNode(DeadCGN);
  105. }
  106. }
  107. void CallGraphUpdater::replaceFunctionWith(Function &OldFn, Function &NewFn) {
  108. OldFn.removeDeadConstantUsers();
  109. ReplacedFunctions.insert(&OldFn);
  110. if (CG) {
  111. // Update the call graph for the newly promoted function.
  112. CallGraphNode *OldCGN = (*CG)[&OldFn];
  113. CallGraphNode *NewCGN = CG->getOrInsertFunction(&NewFn);
  114. NewCGN->stealCalledFunctionsFrom(OldCGN);
  115. CG->ReplaceExternalCallEdge(OldCGN, NewCGN);
  116. // And update the SCC we're iterating as well.
  117. CGSCC->ReplaceNode(OldCGN, NewCGN);
  118. } else if (LCG) {
  119. // Directly substitute the functions in the call graph.
  120. LazyCallGraph::Node &OldLCGN = LCG->get(OldFn);
  121. SCC->getOuterRefSCC().replaceNodeFunction(OldLCGN, NewFn);
  122. }
  123. removeFunction(OldFn);
  124. }
  125. bool CallGraphUpdater::replaceCallSite(CallBase &OldCS, CallBase &NewCS) {
  126. // This is only necessary in the (old) CG.
  127. if (!CG)
  128. return true;
  129. Function *Caller = OldCS.getCaller();
  130. CallGraphNode *NewCalleeNode =
  131. CG->getOrInsertFunction(NewCS.getCalledFunction());
  132. CallGraphNode *CallerNode = (*CG)[Caller];
  133. if (llvm::none_of(*CallerNode, [&OldCS](const CallGraphNode::CallRecord &CR) {
  134. return CR.first && *CR.first == &OldCS;
  135. }))
  136. return false;
  137. CallerNode->replaceCallEdge(OldCS, NewCS, NewCalleeNode);
  138. return true;
  139. }
  140. void CallGraphUpdater::removeCallSite(CallBase &CS) {
  141. // This is only necessary in the (old) CG.
  142. if (!CG)
  143. return;
  144. Function *Caller = CS.getCaller();
  145. CallGraphNode *CallerNode = (*CG)[Caller];
  146. CallerNode->removeCallEdgeFor(CS);
  147. }