CallGraphUpdater.cpp 5.9 KB

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