CallGraphUpdater.cpp 5.8 KB

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