LazyCallGraph.cpp 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059
  1. //===- LazyCallGraph.cpp - Analysis of a Module's call graph --------------===//
  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. #include "llvm/Analysis/LazyCallGraph.h"
  9. #include "llvm/ADT/ArrayRef.h"
  10. #include "llvm/ADT/STLExtras.h"
  11. #include "llvm/ADT/Sequence.h"
  12. #include "llvm/ADT/SmallPtrSet.h"
  13. #include "llvm/ADT/SmallVector.h"
  14. #include "llvm/ADT/iterator_range.h"
  15. #include "llvm/Analysis/TargetLibraryInfo.h"
  16. #include "llvm/IR/Constants.h"
  17. #include "llvm/IR/Function.h"
  18. #include "llvm/IR/GlobalVariable.h"
  19. #include "llvm/IR/InstIterator.h"
  20. #include "llvm/IR/Instruction.h"
  21. #include "llvm/IR/Module.h"
  22. #include "llvm/IR/PassManager.h"
  23. #include "llvm/Support/Casting.h"
  24. #include "llvm/Support/Compiler.h"
  25. #include "llvm/Support/Debug.h"
  26. #include "llvm/Support/GraphWriter.h"
  27. #include "llvm/Support/raw_ostream.h"
  28. #include <algorithm>
  29. #ifdef EXPENSIVE_CHECKS
  30. #include "llvm/ADT/ScopeExit.h"
  31. #endif
  32. using namespace llvm;
  33. #define DEBUG_TYPE "lcg"
  34. void LazyCallGraph::EdgeSequence::insertEdgeInternal(Node &TargetN,
  35. Edge::Kind EK) {
  36. EdgeIndexMap.try_emplace(&TargetN, Edges.size());
  37. Edges.emplace_back(TargetN, EK);
  38. }
  39. void LazyCallGraph::EdgeSequence::setEdgeKind(Node &TargetN, Edge::Kind EK) {
  40. Edges[EdgeIndexMap.find(&TargetN)->second].setKind(EK);
  41. }
  42. bool LazyCallGraph::EdgeSequence::removeEdgeInternal(Node &TargetN) {
  43. auto IndexMapI = EdgeIndexMap.find(&TargetN);
  44. if (IndexMapI == EdgeIndexMap.end())
  45. return false;
  46. Edges[IndexMapI->second] = Edge();
  47. EdgeIndexMap.erase(IndexMapI);
  48. return true;
  49. }
  50. static void addEdge(SmallVectorImpl<LazyCallGraph::Edge> &Edges,
  51. DenseMap<LazyCallGraph::Node *, int> &EdgeIndexMap,
  52. LazyCallGraph::Node &N, LazyCallGraph::Edge::Kind EK) {
  53. if (!EdgeIndexMap.try_emplace(&N, Edges.size()).second)
  54. return;
  55. LLVM_DEBUG(dbgs() << " Added callable function: " << N.getName() << "\n");
  56. Edges.emplace_back(LazyCallGraph::Edge(N, EK));
  57. }
  58. LazyCallGraph::EdgeSequence &LazyCallGraph::Node::populateSlow() {
  59. assert(!Edges && "Must not have already populated the edges for this node!");
  60. LLVM_DEBUG(dbgs() << " Adding functions called by '" << getName()
  61. << "' to the graph.\n");
  62. Edges = EdgeSequence();
  63. SmallVector<Constant *, 16> Worklist;
  64. SmallPtrSet<Function *, 4> Callees;
  65. SmallPtrSet<Constant *, 16> Visited;
  66. // Find all the potential call graph edges in this function. We track both
  67. // actual call edges and indirect references to functions. The direct calls
  68. // are trivially added, but to accumulate the latter we walk the instructions
  69. // and add every operand which is a constant to the worklist to process
  70. // afterward.
  71. //
  72. // Note that we consider *any* function with a definition to be a viable
  73. // edge. Even if the function's definition is subject to replacement by
  74. // some other module (say, a weak definition) there may still be
  75. // optimizations which essentially speculate based on the definition and
  76. // a way to check that the specific definition is in fact the one being
  77. // used. For example, this could be done by moving the weak definition to
  78. // a strong (internal) definition and making the weak definition be an
  79. // alias. Then a test of the address of the weak function against the new
  80. // strong definition's address would be an effective way to determine the
  81. // safety of optimizing a direct call edge.
  82. for (BasicBlock &BB : *F)
  83. for (Instruction &I : BB) {
  84. if (auto *CB = dyn_cast<CallBase>(&I))
  85. if (Function *Callee = CB->getCalledFunction())
  86. if (!Callee->isDeclaration())
  87. if (Callees.insert(Callee).second) {
  88. Visited.insert(Callee);
  89. addEdge(Edges->Edges, Edges->EdgeIndexMap, G->get(*Callee),
  90. LazyCallGraph::Edge::Call);
  91. }
  92. for (Value *Op : I.operand_values())
  93. if (Constant *C = dyn_cast<Constant>(Op))
  94. if (Visited.insert(C).second)
  95. Worklist.push_back(C);
  96. }
  97. // We've collected all the constant (and thus potentially function or
  98. // function containing) operands to all the instructions in the function.
  99. // Process them (recursively) collecting every function found.
  100. visitReferences(Worklist, Visited, [&](Function &F) {
  101. addEdge(Edges->Edges, Edges->EdgeIndexMap, G->get(F),
  102. LazyCallGraph::Edge::Ref);
  103. });
  104. // Add implicit reference edges to any defined libcall functions (if we
  105. // haven't found an explicit edge).
  106. for (auto *F : G->LibFunctions)
  107. if (!Visited.count(F))
  108. addEdge(Edges->Edges, Edges->EdgeIndexMap, G->get(*F),
  109. LazyCallGraph::Edge::Ref);
  110. return *Edges;
  111. }
  112. void LazyCallGraph::Node::replaceFunction(Function &NewF) {
  113. assert(F != &NewF && "Must not replace a function with itself!");
  114. F = &NewF;
  115. }
  116. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  117. LLVM_DUMP_METHOD void LazyCallGraph::Node::dump() const {
  118. dbgs() << *this << '\n';
  119. }
  120. #endif
  121. static bool isKnownLibFunction(Function &F, TargetLibraryInfo &TLI) {
  122. LibFunc LF;
  123. // Either this is a normal library function or a "vectorizable"
  124. // function. Not using the VFDatabase here because this query
  125. // is related only to libraries handled via the TLI.
  126. return TLI.getLibFunc(F, LF) ||
  127. TLI.isKnownVectorFunctionInLibrary(F.getName());
  128. }
  129. LazyCallGraph::LazyCallGraph(
  130. Module &M, function_ref<TargetLibraryInfo &(Function &)> GetTLI) {
  131. LLVM_DEBUG(dbgs() << "Building CG for module: " << M.getModuleIdentifier()
  132. << "\n");
  133. for (Function &F : M) {
  134. if (F.isDeclaration())
  135. continue;
  136. // If this function is a known lib function to LLVM then we want to
  137. // synthesize reference edges to it to model the fact that LLVM can turn
  138. // arbitrary code into a library function call.
  139. if (isKnownLibFunction(F, GetTLI(F)))
  140. LibFunctions.insert(&F);
  141. if (F.hasLocalLinkage())
  142. continue;
  143. // External linkage defined functions have edges to them from other
  144. // modules.
  145. LLVM_DEBUG(dbgs() << " Adding '" << F.getName()
  146. << "' to entry set of the graph.\n");
  147. addEdge(EntryEdges.Edges, EntryEdges.EdgeIndexMap, get(F), Edge::Ref);
  148. }
  149. // Externally visible aliases of internal functions are also viable entry
  150. // edges to the module.
  151. for (auto &A : M.aliases()) {
  152. if (A.hasLocalLinkage())
  153. continue;
  154. if (Function* F = dyn_cast<Function>(A.getAliasee())) {
  155. LLVM_DEBUG(dbgs() << " Adding '" << F->getName()
  156. << "' with alias '" << A.getName()
  157. << "' to entry set of the graph.\n");
  158. addEdge(EntryEdges.Edges, EntryEdges.EdgeIndexMap, get(*F), Edge::Ref);
  159. }
  160. }
  161. // Now add entry nodes for functions reachable via initializers to globals.
  162. SmallVector<Constant *, 16> Worklist;
  163. SmallPtrSet<Constant *, 16> Visited;
  164. for (GlobalVariable &GV : M.globals())
  165. if (GV.hasInitializer())
  166. if (Visited.insert(GV.getInitializer()).second)
  167. Worklist.push_back(GV.getInitializer());
  168. LLVM_DEBUG(
  169. dbgs() << " Adding functions referenced by global initializers to the "
  170. "entry set.\n");
  171. visitReferences(Worklist, Visited, [&](Function &F) {
  172. addEdge(EntryEdges.Edges, EntryEdges.EdgeIndexMap, get(F),
  173. LazyCallGraph::Edge::Ref);
  174. });
  175. }
  176. LazyCallGraph::LazyCallGraph(LazyCallGraph &&G)
  177. : BPA(std::move(G.BPA)), NodeMap(std::move(G.NodeMap)),
  178. EntryEdges(std::move(G.EntryEdges)), SCCBPA(std::move(G.SCCBPA)),
  179. SCCMap(std::move(G.SCCMap)), LibFunctions(std::move(G.LibFunctions)) {
  180. updateGraphPtrs();
  181. }
  182. bool LazyCallGraph::invalidate(Module &, const PreservedAnalyses &PA,
  183. ModuleAnalysisManager::Invalidator &) {
  184. // Check whether the analysis, all analyses on functions, or the function's
  185. // CFG have been preserved.
  186. auto PAC = PA.getChecker<llvm::LazyCallGraphAnalysis>();
  187. return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Module>>());
  188. }
  189. LazyCallGraph &LazyCallGraph::operator=(LazyCallGraph &&G) {
  190. BPA = std::move(G.BPA);
  191. NodeMap = std::move(G.NodeMap);
  192. EntryEdges = std::move(G.EntryEdges);
  193. SCCBPA = std::move(G.SCCBPA);
  194. SCCMap = std::move(G.SCCMap);
  195. LibFunctions = std::move(G.LibFunctions);
  196. updateGraphPtrs();
  197. return *this;
  198. }
  199. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  200. LLVM_DUMP_METHOD void LazyCallGraph::SCC::dump() const {
  201. dbgs() << *this << '\n';
  202. }
  203. #endif
  204. #if !defined(NDEBUG) || defined(EXPENSIVE_CHECKS)
  205. void LazyCallGraph::SCC::verify() {
  206. assert(OuterRefSCC && "Can't have a null RefSCC!");
  207. assert(!Nodes.empty() && "Can't have an empty SCC!");
  208. for (Node *N : Nodes) {
  209. assert(N && "Can't have a null node!");
  210. assert(OuterRefSCC->G->lookupSCC(*N) == this &&
  211. "Node does not map to this SCC!");
  212. assert(N->DFSNumber == -1 &&
  213. "Must set DFS numbers to -1 when adding a node to an SCC!");
  214. assert(N->LowLink == -1 &&
  215. "Must set low link to -1 when adding a node to an SCC!");
  216. for (Edge &E : **N)
  217. assert(E.getNode().isPopulated() && "Can't have an unpopulated node!");
  218. #ifdef EXPENSIVE_CHECKS
  219. // Verify that all nodes in this SCC can reach all other nodes.
  220. SmallVector<Node *, 4> Worklist;
  221. SmallPtrSet<Node *, 4> Visited;
  222. Worklist.push_back(N);
  223. while (!Worklist.empty()) {
  224. Node *VisitingNode = Worklist.pop_back_val();
  225. if (!Visited.insert(VisitingNode).second)
  226. continue;
  227. for (Edge &E : (*VisitingNode)->calls())
  228. Worklist.push_back(&E.getNode());
  229. }
  230. for (Node *NodeToVisit : Nodes) {
  231. assert(Visited.contains(NodeToVisit) &&
  232. "Cannot reach all nodes within SCC");
  233. }
  234. #endif
  235. }
  236. }
  237. #endif
  238. bool LazyCallGraph::SCC::isParentOf(const SCC &C) const {
  239. if (this == &C)
  240. return false;
  241. for (Node &N : *this)
  242. for (Edge &E : N->calls())
  243. if (OuterRefSCC->G->lookupSCC(E.getNode()) == &C)
  244. return true;
  245. // No edges found.
  246. return false;
  247. }
  248. bool LazyCallGraph::SCC::isAncestorOf(const SCC &TargetC) const {
  249. if (this == &TargetC)
  250. return false;
  251. LazyCallGraph &G = *OuterRefSCC->G;
  252. // Start with this SCC.
  253. SmallPtrSet<const SCC *, 16> Visited = {this};
  254. SmallVector<const SCC *, 16> Worklist = {this};
  255. // Walk down the graph until we run out of edges or find a path to TargetC.
  256. do {
  257. const SCC &C = *Worklist.pop_back_val();
  258. for (Node &N : C)
  259. for (Edge &E : N->calls()) {
  260. SCC *CalleeC = G.lookupSCC(E.getNode());
  261. if (!CalleeC)
  262. continue;
  263. // If the callee's SCC is the TargetC, we're done.
  264. if (CalleeC == &TargetC)
  265. return true;
  266. // If this is the first time we've reached this SCC, put it on the
  267. // worklist to recurse through.
  268. if (Visited.insert(CalleeC).second)
  269. Worklist.push_back(CalleeC);
  270. }
  271. } while (!Worklist.empty());
  272. // No paths found.
  273. return false;
  274. }
  275. LazyCallGraph::RefSCC::RefSCC(LazyCallGraph &G) : G(&G) {}
  276. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  277. LLVM_DUMP_METHOD void LazyCallGraph::RefSCC::dump() const {
  278. dbgs() << *this << '\n';
  279. }
  280. #endif
  281. #if !defined(NDEBUG) || defined(EXPENSIVE_CHECKS)
  282. void LazyCallGraph::RefSCC::verify() {
  283. assert(G && "Can't have a null graph!");
  284. assert(!SCCs.empty() && "Can't have an empty SCC!");
  285. // Verify basic properties of the SCCs.
  286. SmallPtrSet<SCC *, 4> SCCSet;
  287. for (SCC *C : SCCs) {
  288. assert(C && "Can't have a null SCC!");
  289. C->verify();
  290. assert(&C->getOuterRefSCC() == this &&
  291. "SCC doesn't think it is inside this RefSCC!");
  292. bool Inserted = SCCSet.insert(C).second;
  293. assert(Inserted && "Found a duplicate SCC!");
  294. auto IndexIt = SCCIndices.find(C);
  295. assert(IndexIt != SCCIndices.end() &&
  296. "Found an SCC that doesn't have an index!");
  297. }
  298. // Check that our indices map correctly.
  299. for (auto [C, I] : SCCIndices) {
  300. assert(C && "Can't have a null SCC in the indices!");
  301. assert(SCCSet.count(C) && "Found an index for an SCC not in the RefSCC!");
  302. assert(SCCs[I] == C && "Index doesn't point to SCC!");
  303. }
  304. // Check that the SCCs are in fact in post-order.
  305. for (int I = 0, Size = SCCs.size(); I < Size; ++I) {
  306. SCC &SourceSCC = *SCCs[I];
  307. for (Node &N : SourceSCC)
  308. for (Edge &E : *N) {
  309. if (!E.isCall())
  310. continue;
  311. SCC &TargetSCC = *G->lookupSCC(E.getNode());
  312. if (&TargetSCC.getOuterRefSCC() == this) {
  313. assert(SCCIndices.find(&TargetSCC)->second <= I &&
  314. "Edge between SCCs violates post-order relationship.");
  315. continue;
  316. }
  317. }
  318. }
  319. #ifdef EXPENSIVE_CHECKS
  320. // Verify that all nodes in this RefSCC can reach all other nodes.
  321. SmallVector<Node *> Nodes;
  322. for (SCC *C : SCCs) {
  323. for (Node &N : *C)
  324. Nodes.push_back(&N);
  325. }
  326. for (Node *N : Nodes) {
  327. SmallVector<Node *, 4> Worklist;
  328. SmallPtrSet<Node *, 4> Visited;
  329. Worklist.push_back(N);
  330. while (!Worklist.empty()) {
  331. Node *VisitingNode = Worklist.pop_back_val();
  332. if (!Visited.insert(VisitingNode).second)
  333. continue;
  334. for (Edge &E : **VisitingNode)
  335. Worklist.push_back(&E.getNode());
  336. }
  337. for (Node *NodeToVisit : Nodes) {
  338. assert(Visited.contains(NodeToVisit) &&
  339. "Cannot reach all nodes within RefSCC");
  340. }
  341. }
  342. #endif
  343. }
  344. #endif
  345. bool LazyCallGraph::RefSCC::isParentOf(const RefSCC &RC) const {
  346. if (&RC == this)
  347. return false;
  348. // Search all edges to see if this is a parent.
  349. for (SCC &C : *this)
  350. for (Node &N : C)
  351. for (Edge &E : *N)
  352. if (G->lookupRefSCC(E.getNode()) == &RC)
  353. return true;
  354. return false;
  355. }
  356. bool LazyCallGraph::RefSCC::isAncestorOf(const RefSCC &RC) const {
  357. if (&RC == this)
  358. return false;
  359. // For each descendant of this RefSCC, see if one of its children is the
  360. // argument. If not, add that descendant to the worklist and continue
  361. // searching.
  362. SmallVector<const RefSCC *, 4> Worklist;
  363. SmallPtrSet<const RefSCC *, 4> Visited;
  364. Worklist.push_back(this);
  365. Visited.insert(this);
  366. do {
  367. const RefSCC &DescendantRC = *Worklist.pop_back_val();
  368. for (SCC &C : DescendantRC)
  369. for (Node &N : C)
  370. for (Edge &E : *N) {
  371. auto *ChildRC = G->lookupRefSCC(E.getNode());
  372. if (ChildRC == &RC)
  373. return true;
  374. if (!ChildRC || !Visited.insert(ChildRC).second)
  375. continue;
  376. Worklist.push_back(ChildRC);
  377. }
  378. } while (!Worklist.empty());
  379. return false;
  380. }
  381. /// Generic helper that updates a postorder sequence of SCCs for a potentially
  382. /// cycle-introducing edge insertion.
  383. ///
  384. /// A postorder sequence of SCCs of a directed graph has one fundamental
  385. /// property: all deges in the DAG of SCCs point "up" the sequence. That is,
  386. /// all edges in the SCC DAG point to prior SCCs in the sequence.
  387. ///
  388. /// This routine both updates a postorder sequence and uses that sequence to
  389. /// compute the set of SCCs connected into a cycle. It should only be called to
  390. /// insert a "downward" edge which will require changing the sequence to
  391. /// restore it to a postorder.
  392. ///
  393. /// When inserting an edge from an earlier SCC to a later SCC in some postorder
  394. /// sequence, all of the SCCs which may be impacted are in the closed range of
  395. /// those two within the postorder sequence. The algorithm used here to restore
  396. /// the state is as follows:
  397. ///
  398. /// 1) Starting from the source SCC, construct a set of SCCs which reach the
  399. /// source SCC consisting of just the source SCC. Then scan toward the
  400. /// target SCC in postorder and for each SCC, if it has an edge to an SCC
  401. /// in the set, add it to the set. Otherwise, the source SCC is not
  402. /// a successor, move it in the postorder sequence to immediately before
  403. /// the source SCC, shifting the source SCC and all SCCs in the set one
  404. /// position toward the target SCC. Stop scanning after processing the
  405. /// target SCC.
  406. /// 2) If the source SCC is now past the target SCC in the postorder sequence,
  407. /// and thus the new edge will flow toward the start, we are done.
  408. /// 3) Otherwise, starting from the target SCC, walk all edges which reach an
  409. /// SCC between the source and the target, and add them to the set of
  410. /// connected SCCs, then recurse through them. Once a complete set of the
  411. /// SCCs the target connects to is known, hoist the remaining SCCs between
  412. /// the source and the target to be above the target. Note that there is no
  413. /// need to process the source SCC, it is already known to connect.
  414. /// 4) At this point, all of the SCCs in the closed range between the source
  415. /// SCC and the target SCC in the postorder sequence are connected,
  416. /// including the target SCC and the source SCC. Inserting the edge from
  417. /// the source SCC to the target SCC will form a cycle out of precisely
  418. /// these SCCs. Thus we can merge all of the SCCs in this closed range into
  419. /// a single SCC.
  420. ///
  421. /// This process has various important properties:
  422. /// - Only mutates the SCCs when adding the edge actually changes the SCC
  423. /// structure.
  424. /// - Never mutates SCCs which are unaffected by the change.
  425. /// - Updates the postorder sequence to correctly satisfy the postorder
  426. /// constraint after the edge is inserted.
  427. /// - Only reorders SCCs in the closed postorder sequence from the source to
  428. /// the target, so easy to bound how much has changed even in the ordering.
  429. /// - Big-O is the number of edges in the closed postorder range of SCCs from
  430. /// source to target.
  431. ///
  432. /// This helper routine, in addition to updating the postorder sequence itself
  433. /// will also update a map from SCCs to indices within that sequence.
  434. ///
  435. /// The sequence and the map must operate on pointers to the SCC type.
  436. ///
  437. /// Two callbacks must be provided. The first computes the subset of SCCs in
  438. /// the postorder closed range from the source to the target which connect to
  439. /// the source SCC via some (transitive) set of edges. The second computes the
  440. /// subset of the same range which the target SCC connects to via some
  441. /// (transitive) set of edges. Both callbacks should populate the set argument
  442. /// provided.
  443. template <typename SCCT, typename PostorderSequenceT, typename SCCIndexMapT,
  444. typename ComputeSourceConnectedSetCallableT,
  445. typename ComputeTargetConnectedSetCallableT>
  446. static iterator_range<typename PostorderSequenceT::iterator>
  447. updatePostorderSequenceForEdgeInsertion(
  448. SCCT &SourceSCC, SCCT &TargetSCC, PostorderSequenceT &SCCs,
  449. SCCIndexMapT &SCCIndices,
  450. ComputeSourceConnectedSetCallableT ComputeSourceConnectedSet,
  451. ComputeTargetConnectedSetCallableT ComputeTargetConnectedSet) {
  452. int SourceIdx = SCCIndices[&SourceSCC];
  453. int TargetIdx = SCCIndices[&TargetSCC];
  454. assert(SourceIdx < TargetIdx && "Cannot have equal indices here!");
  455. SmallPtrSet<SCCT *, 4> ConnectedSet;
  456. // Compute the SCCs which (transitively) reach the source.
  457. ComputeSourceConnectedSet(ConnectedSet);
  458. // Partition the SCCs in this part of the port-order sequence so only SCCs
  459. // connecting to the source remain between it and the target. This is
  460. // a benign partition as it preserves postorder.
  461. auto SourceI = std::stable_partition(
  462. SCCs.begin() + SourceIdx, SCCs.begin() + TargetIdx + 1,
  463. [&ConnectedSet](SCCT *C) { return !ConnectedSet.count(C); });
  464. for (int I = SourceIdx, E = TargetIdx + 1; I < E; ++I)
  465. SCCIndices.find(SCCs[I])->second = I;
  466. // If the target doesn't connect to the source, then we've corrected the
  467. // post-order and there are no cycles formed.
  468. if (!ConnectedSet.count(&TargetSCC)) {
  469. assert(SourceI > (SCCs.begin() + SourceIdx) &&
  470. "Must have moved the source to fix the post-order.");
  471. assert(*std::prev(SourceI) == &TargetSCC &&
  472. "Last SCC to move should have bene the target.");
  473. // Return an empty range at the target SCC indicating there is nothing to
  474. // merge.
  475. return make_range(std::prev(SourceI), std::prev(SourceI));
  476. }
  477. assert(SCCs[TargetIdx] == &TargetSCC &&
  478. "Should not have moved target if connected!");
  479. SourceIdx = SourceI - SCCs.begin();
  480. assert(SCCs[SourceIdx] == &SourceSCC &&
  481. "Bad updated index computation for the source SCC!");
  482. // See whether there are any remaining intervening SCCs between the source
  483. // and target. If so we need to make sure they all are reachable form the
  484. // target.
  485. if (SourceIdx + 1 < TargetIdx) {
  486. ConnectedSet.clear();
  487. ComputeTargetConnectedSet(ConnectedSet);
  488. // Partition SCCs so that only SCCs reached from the target remain between
  489. // the source and the target. This preserves postorder.
  490. auto TargetI = std::stable_partition(
  491. SCCs.begin() + SourceIdx + 1, SCCs.begin() + TargetIdx + 1,
  492. [&ConnectedSet](SCCT *C) { return ConnectedSet.count(C); });
  493. for (int I = SourceIdx + 1, E = TargetIdx + 1; I < E; ++I)
  494. SCCIndices.find(SCCs[I])->second = I;
  495. TargetIdx = std::prev(TargetI) - SCCs.begin();
  496. assert(SCCs[TargetIdx] == &TargetSCC &&
  497. "Should always end with the target!");
  498. }
  499. // At this point, we know that connecting source to target forms a cycle
  500. // because target connects back to source, and we know that all the SCCs
  501. // between the source and target in the postorder sequence participate in that
  502. // cycle.
  503. return make_range(SCCs.begin() + SourceIdx, SCCs.begin() + TargetIdx);
  504. }
  505. bool LazyCallGraph::RefSCC::switchInternalEdgeToCall(
  506. Node &SourceN, Node &TargetN,
  507. function_ref<void(ArrayRef<SCC *> MergeSCCs)> MergeCB) {
  508. assert(!(*SourceN)[TargetN].isCall() && "Must start with a ref edge!");
  509. SmallVector<SCC *, 1> DeletedSCCs;
  510. #ifdef EXPENSIVE_CHECKS
  511. verify();
  512. auto VerifyOnExit = make_scope_exit([&]() { verify(); });
  513. #endif
  514. SCC &SourceSCC = *G->lookupSCC(SourceN);
  515. SCC &TargetSCC = *G->lookupSCC(TargetN);
  516. // If the two nodes are already part of the same SCC, we're also done as
  517. // we've just added more connectivity.
  518. if (&SourceSCC == &TargetSCC) {
  519. SourceN->setEdgeKind(TargetN, Edge::Call);
  520. return false; // No new cycle.
  521. }
  522. // At this point we leverage the postorder list of SCCs to detect when the
  523. // insertion of an edge changes the SCC structure in any way.
  524. //
  525. // First and foremost, we can eliminate the need for any changes when the
  526. // edge is toward the beginning of the postorder sequence because all edges
  527. // flow in that direction already. Thus adding a new one cannot form a cycle.
  528. int SourceIdx = SCCIndices[&SourceSCC];
  529. int TargetIdx = SCCIndices[&TargetSCC];
  530. if (TargetIdx < SourceIdx) {
  531. SourceN->setEdgeKind(TargetN, Edge::Call);
  532. return false; // No new cycle.
  533. }
  534. // Compute the SCCs which (transitively) reach the source.
  535. auto ComputeSourceConnectedSet = [&](SmallPtrSetImpl<SCC *> &ConnectedSet) {
  536. #ifdef EXPENSIVE_CHECKS
  537. // Check that the RefSCC is still valid before computing this as the
  538. // results will be nonsensical of we've broken its invariants.
  539. verify();
  540. #endif
  541. ConnectedSet.insert(&SourceSCC);
  542. auto IsConnected = [&](SCC &C) {
  543. for (Node &N : C)
  544. for (Edge &E : N->calls())
  545. if (ConnectedSet.count(G->lookupSCC(E.getNode())))
  546. return true;
  547. return false;
  548. };
  549. for (SCC *C :
  550. make_range(SCCs.begin() + SourceIdx + 1, SCCs.begin() + TargetIdx + 1))
  551. if (IsConnected(*C))
  552. ConnectedSet.insert(C);
  553. };
  554. // Use a normal worklist to find which SCCs the target connects to. We still
  555. // bound the search based on the range in the postorder list we care about,
  556. // but because this is forward connectivity we just "recurse" through the
  557. // edges.
  558. auto ComputeTargetConnectedSet = [&](SmallPtrSetImpl<SCC *> &ConnectedSet) {
  559. #ifdef EXPENSIVE_CHECKS
  560. // Check that the RefSCC is still valid before computing this as the
  561. // results will be nonsensical of we've broken its invariants.
  562. verify();
  563. #endif
  564. ConnectedSet.insert(&TargetSCC);
  565. SmallVector<SCC *, 4> Worklist;
  566. Worklist.push_back(&TargetSCC);
  567. do {
  568. SCC &C = *Worklist.pop_back_val();
  569. for (Node &N : C)
  570. for (Edge &E : *N) {
  571. if (!E.isCall())
  572. continue;
  573. SCC &EdgeC = *G->lookupSCC(E.getNode());
  574. if (&EdgeC.getOuterRefSCC() != this)
  575. // Not in this RefSCC...
  576. continue;
  577. if (SCCIndices.find(&EdgeC)->second <= SourceIdx)
  578. // Not in the postorder sequence between source and target.
  579. continue;
  580. if (ConnectedSet.insert(&EdgeC).second)
  581. Worklist.push_back(&EdgeC);
  582. }
  583. } while (!Worklist.empty());
  584. };
  585. // Use a generic helper to update the postorder sequence of SCCs and return
  586. // a range of any SCCs connected into a cycle by inserting this edge. This
  587. // routine will also take care of updating the indices into the postorder
  588. // sequence.
  589. auto MergeRange = updatePostorderSequenceForEdgeInsertion(
  590. SourceSCC, TargetSCC, SCCs, SCCIndices, ComputeSourceConnectedSet,
  591. ComputeTargetConnectedSet);
  592. // Run the user's callback on the merged SCCs before we actually merge them.
  593. if (MergeCB)
  594. MergeCB(ArrayRef(MergeRange.begin(), MergeRange.end()));
  595. // If the merge range is empty, then adding the edge didn't actually form any
  596. // new cycles. We're done.
  597. if (MergeRange.empty()) {
  598. // Now that the SCC structure is finalized, flip the kind to call.
  599. SourceN->setEdgeKind(TargetN, Edge::Call);
  600. return false; // No new cycle.
  601. }
  602. #ifdef EXPENSIVE_CHECKS
  603. // Before merging, check that the RefSCC remains valid after all the
  604. // postorder updates.
  605. verify();
  606. #endif
  607. // Otherwise we need to merge all the SCCs in the cycle into a single result
  608. // SCC.
  609. //
  610. // NB: We merge into the target because all of these functions were already
  611. // reachable from the target, meaning any SCC-wide properties deduced about it
  612. // other than the set of functions within it will not have changed.
  613. for (SCC *C : MergeRange) {
  614. assert(C != &TargetSCC &&
  615. "We merge *into* the target and shouldn't process it here!");
  616. SCCIndices.erase(C);
  617. TargetSCC.Nodes.append(C->Nodes.begin(), C->Nodes.end());
  618. for (Node *N : C->Nodes)
  619. G->SCCMap[N] = &TargetSCC;
  620. C->clear();
  621. DeletedSCCs.push_back(C);
  622. }
  623. // Erase the merged SCCs from the list and update the indices of the
  624. // remaining SCCs.
  625. int IndexOffset = MergeRange.end() - MergeRange.begin();
  626. auto EraseEnd = SCCs.erase(MergeRange.begin(), MergeRange.end());
  627. for (SCC *C : make_range(EraseEnd, SCCs.end()))
  628. SCCIndices[C] -= IndexOffset;
  629. // Now that the SCC structure is finalized, flip the kind to call.
  630. SourceN->setEdgeKind(TargetN, Edge::Call);
  631. // And we're done, but we did form a new cycle.
  632. return true;
  633. }
  634. void LazyCallGraph::RefSCC::switchTrivialInternalEdgeToRef(Node &SourceN,
  635. Node &TargetN) {
  636. assert((*SourceN)[TargetN].isCall() && "Must start with a call edge!");
  637. #ifdef EXPENSIVE_CHECKS
  638. verify();
  639. auto VerifyOnExit = make_scope_exit([&]() { verify(); });
  640. #endif
  641. assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
  642. assert(G->lookupRefSCC(TargetN) == this && "Target must be in this RefSCC.");
  643. assert(G->lookupSCC(SourceN) != G->lookupSCC(TargetN) &&
  644. "Source and Target must be in separate SCCs for this to be trivial!");
  645. // Set the edge kind.
  646. SourceN->setEdgeKind(TargetN, Edge::Ref);
  647. }
  648. iterator_range<LazyCallGraph::RefSCC::iterator>
  649. LazyCallGraph::RefSCC::switchInternalEdgeToRef(Node &SourceN, Node &TargetN) {
  650. assert((*SourceN)[TargetN].isCall() && "Must start with a call edge!");
  651. #ifdef EXPENSIVE_CHECKS
  652. verify();
  653. auto VerifyOnExit = make_scope_exit([&]() { verify(); });
  654. #endif
  655. assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
  656. assert(G->lookupRefSCC(TargetN) == this && "Target must be in this RefSCC.");
  657. SCC &TargetSCC = *G->lookupSCC(TargetN);
  658. assert(G->lookupSCC(SourceN) == &TargetSCC && "Source and Target must be in "
  659. "the same SCC to require the "
  660. "full CG update.");
  661. // Set the edge kind.
  662. SourceN->setEdgeKind(TargetN, Edge::Ref);
  663. // Otherwise we are removing a call edge from a single SCC. This may break
  664. // the cycle. In order to compute the new set of SCCs, we need to do a small
  665. // DFS over the nodes within the SCC to form any sub-cycles that remain as
  666. // distinct SCCs and compute a postorder over the resulting SCCs.
  667. //
  668. // However, we specially handle the target node. The target node is known to
  669. // reach all other nodes in the original SCC by definition. This means that
  670. // we want the old SCC to be replaced with an SCC containing that node as it
  671. // will be the root of whatever SCC DAG results from the DFS. Assumptions
  672. // about an SCC such as the set of functions called will continue to hold,
  673. // etc.
  674. SCC &OldSCC = TargetSCC;
  675. SmallVector<std::pair<Node *, EdgeSequence::call_iterator>, 16> DFSStack;
  676. SmallVector<Node *, 16> PendingSCCStack;
  677. SmallVector<SCC *, 4> NewSCCs;
  678. // Prepare the nodes for a fresh DFS.
  679. SmallVector<Node *, 16> Worklist;
  680. Worklist.swap(OldSCC.Nodes);
  681. for (Node *N : Worklist) {
  682. N->DFSNumber = N->LowLink = 0;
  683. G->SCCMap.erase(N);
  684. }
  685. // Force the target node to be in the old SCC. This also enables us to take
  686. // a very significant short-cut in the standard Tarjan walk to re-form SCCs
  687. // below: whenever we build an edge that reaches the target node, we know
  688. // that the target node eventually connects back to all other nodes in our
  689. // walk. As a consequence, we can detect and handle participants in that
  690. // cycle without walking all the edges that form this connection, and instead
  691. // by relying on the fundamental guarantee coming into this operation (all
  692. // nodes are reachable from the target due to previously forming an SCC).
  693. TargetN.DFSNumber = TargetN.LowLink = -1;
  694. OldSCC.Nodes.push_back(&TargetN);
  695. G->SCCMap[&TargetN] = &OldSCC;
  696. // Scan down the stack and DFS across the call edges.
  697. for (Node *RootN : Worklist) {
  698. assert(DFSStack.empty() &&
  699. "Cannot begin a new root with a non-empty DFS stack!");
  700. assert(PendingSCCStack.empty() &&
  701. "Cannot begin a new root with pending nodes for an SCC!");
  702. // Skip any nodes we've already reached in the DFS.
  703. if (RootN->DFSNumber != 0) {
  704. assert(RootN->DFSNumber == -1 &&
  705. "Shouldn't have any mid-DFS root nodes!");
  706. continue;
  707. }
  708. RootN->DFSNumber = RootN->LowLink = 1;
  709. int NextDFSNumber = 2;
  710. DFSStack.emplace_back(RootN, (*RootN)->call_begin());
  711. do {
  712. auto [N, I] = DFSStack.pop_back_val();
  713. auto E = (*N)->call_end();
  714. while (I != E) {
  715. Node &ChildN = I->getNode();
  716. if (ChildN.DFSNumber == 0) {
  717. // We haven't yet visited this child, so descend, pushing the current
  718. // node onto the stack.
  719. DFSStack.emplace_back(N, I);
  720. assert(!G->SCCMap.count(&ChildN) &&
  721. "Found a node with 0 DFS number but already in an SCC!");
  722. ChildN.DFSNumber = ChildN.LowLink = NextDFSNumber++;
  723. N = &ChildN;
  724. I = (*N)->call_begin();
  725. E = (*N)->call_end();
  726. continue;
  727. }
  728. // Check for the child already being part of some component.
  729. if (ChildN.DFSNumber == -1) {
  730. if (G->lookupSCC(ChildN) == &OldSCC) {
  731. // If the child is part of the old SCC, we know that it can reach
  732. // every other node, so we have formed a cycle. Pull the entire DFS
  733. // and pending stacks into it. See the comment above about setting
  734. // up the old SCC for why we do this.
  735. int OldSize = OldSCC.size();
  736. OldSCC.Nodes.push_back(N);
  737. OldSCC.Nodes.append(PendingSCCStack.begin(), PendingSCCStack.end());
  738. PendingSCCStack.clear();
  739. while (!DFSStack.empty())
  740. OldSCC.Nodes.push_back(DFSStack.pop_back_val().first);
  741. for (Node &N : drop_begin(OldSCC, OldSize)) {
  742. N.DFSNumber = N.LowLink = -1;
  743. G->SCCMap[&N] = &OldSCC;
  744. }
  745. N = nullptr;
  746. break;
  747. }
  748. // If the child has already been added to some child component, it
  749. // couldn't impact the low-link of this parent because it isn't
  750. // connected, and thus its low-link isn't relevant so skip it.
  751. ++I;
  752. continue;
  753. }
  754. // Track the lowest linked child as the lowest link for this node.
  755. assert(ChildN.LowLink > 0 && "Must have a positive low-link number!");
  756. if (ChildN.LowLink < N->LowLink)
  757. N->LowLink = ChildN.LowLink;
  758. // Move to the next edge.
  759. ++I;
  760. }
  761. if (!N)
  762. // Cleared the DFS early, start another round.
  763. break;
  764. // We've finished processing N and its descendants, put it on our pending
  765. // SCC stack to eventually get merged into an SCC of nodes.
  766. PendingSCCStack.push_back(N);
  767. // If this node is linked to some lower entry, continue walking up the
  768. // stack.
  769. if (N->LowLink != N->DFSNumber)
  770. continue;
  771. // Otherwise, we've completed an SCC. Append it to our post order list of
  772. // SCCs.
  773. int RootDFSNumber = N->DFSNumber;
  774. // Find the range of the node stack by walking down until we pass the
  775. // root DFS number.
  776. auto SCCNodes = make_range(
  777. PendingSCCStack.rbegin(),
  778. find_if(reverse(PendingSCCStack), [RootDFSNumber](const Node *N) {
  779. return N->DFSNumber < RootDFSNumber;
  780. }));
  781. // Form a new SCC out of these nodes and then clear them off our pending
  782. // stack.
  783. NewSCCs.push_back(G->createSCC(*this, SCCNodes));
  784. for (Node &N : *NewSCCs.back()) {
  785. N.DFSNumber = N.LowLink = -1;
  786. G->SCCMap[&N] = NewSCCs.back();
  787. }
  788. PendingSCCStack.erase(SCCNodes.end().base(), PendingSCCStack.end());
  789. } while (!DFSStack.empty());
  790. }
  791. // Insert the remaining SCCs before the old one. The old SCC can reach all
  792. // other SCCs we form because it contains the target node of the removed edge
  793. // of the old SCC. This means that we will have edges into all the new SCCs,
  794. // which means the old one must come last for postorder.
  795. int OldIdx = SCCIndices[&OldSCC];
  796. SCCs.insert(SCCs.begin() + OldIdx, NewSCCs.begin(), NewSCCs.end());
  797. // Update the mapping from SCC* to index to use the new SCC*s, and remove the
  798. // old SCC from the mapping.
  799. for (int Idx = OldIdx, Size = SCCs.size(); Idx < Size; ++Idx)
  800. SCCIndices[SCCs[Idx]] = Idx;
  801. return make_range(SCCs.begin() + OldIdx,
  802. SCCs.begin() + OldIdx + NewSCCs.size());
  803. }
  804. void LazyCallGraph::RefSCC::switchOutgoingEdgeToCall(Node &SourceN,
  805. Node &TargetN) {
  806. assert(!(*SourceN)[TargetN].isCall() && "Must start with a ref edge!");
  807. assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
  808. assert(G->lookupRefSCC(TargetN) != this &&
  809. "Target must not be in this RefSCC.");
  810. #ifdef EXPENSIVE_CHECKS
  811. assert(G->lookupRefSCC(TargetN)->isDescendantOf(*this) &&
  812. "Target must be a descendant of the Source.");
  813. #endif
  814. // Edges between RefSCCs are the same regardless of call or ref, so we can
  815. // just flip the edge here.
  816. SourceN->setEdgeKind(TargetN, Edge::Call);
  817. #ifdef EXPENSIVE_CHECKS
  818. verify();
  819. #endif
  820. }
  821. void LazyCallGraph::RefSCC::switchOutgoingEdgeToRef(Node &SourceN,
  822. Node &TargetN) {
  823. assert((*SourceN)[TargetN].isCall() && "Must start with a call edge!");
  824. assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
  825. assert(G->lookupRefSCC(TargetN) != this &&
  826. "Target must not be in this RefSCC.");
  827. #ifdef EXPENSIVE_CHECKS
  828. assert(G->lookupRefSCC(TargetN)->isDescendantOf(*this) &&
  829. "Target must be a descendant of the Source.");
  830. #endif
  831. // Edges between RefSCCs are the same regardless of call or ref, so we can
  832. // just flip the edge here.
  833. SourceN->setEdgeKind(TargetN, Edge::Ref);
  834. #ifdef EXPENSIVE_CHECKS
  835. verify();
  836. #endif
  837. }
  838. void LazyCallGraph::RefSCC::insertInternalRefEdge(Node &SourceN,
  839. Node &TargetN) {
  840. assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
  841. assert(G->lookupRefSCC(TargetN) == this && "Target must be in this RefSCC.");
  842. SourceN->insertEdgeInternal(TargetN, Edge::Ref);
  843. #ifdef EXPENSIVE_CHECKS
  844. verify();
  845. #endif
  846. }
  847. void LazyCallGraph::RefSCC::insertOutgoingEdge(Node &SourceN, Node &TargetN,
  848. Edge::Kind EK) {
  849. // First insert it into the caller.
  850. SourceN->insertEdgeInternal(TargetN, EK);
  851. assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
  852. assert(G->lookupRefSCC(TargetN) != this &&
  853. "Target must not be in this RefSCC.");
  854. #ifdef EXPENSIVE_CHECKS
  855. assert(G->lookupRefSCC(TargetN)->isDescendantOf(*this) &&
  856. "Target must be a descendant of the Source.");
  857. #endif
  858. #ifdef EXPENSIVE_CHECKS
  859. verify();
  860. #endif
  861. }
  862. SmallVector<LazyCallGraph::RefSCC *, 1>
  863. LazyCallGraph::RefSCC::insertIncomingRefEdge(Node &SourceN, Node &TargetN) {
  864. assert(G->lookupRefSCC(TargetN) == this && "Target must be in this RefSCC.");
  865. RefSCC &SourceC = *G->lookupRefSCC(SourceN);
  866. assert(&SourceC != this && "Source must not be in this RefSCC.");
  867. #ifdef EXPENSIVE_CHECKS
  868. assert(SourceC.isDescendantOf(*this) &&
  869. "Source must be a descendant of the Target.");
  870. #endif
  871. SmallVector<RefSCC *, 1> DeletedRefSCCs;
  872. #ifdef EXPENSIVE_CHECKS
  873. verify();
  874. auto VerifyOnExit = make_scope_exit([&]() { verify(); });
  875. #endif
  876. int SourceIdx = G->RefSCCIndices[&SourceC];
  877. int TargetIdx = G->RefSCCIndices[this];
  878. assert(SourceIdx < TargetIdx &&
  879. "Postorder list doesn't see edge as incoming!");
  880. // Compute the RefSCCs which (transitively) reach the source. We do this by
  881. // working backwards from the source using the parent set in each RefSCC,
  882. // skipping any RefSCCs that don't fall in the postorder range. This has the
  883. // advantage of walking the sparser parent edge (in high fan-out graphs) but
  884. // more importantly this removes examining all forward edges in all RefSCCs
  885. // within the postorder range which aren't in fact connected. Only connected
  886. // RefSCCs (and their edges) are visited here.
  887. auto ComputeSourceConnectedSet = [&](SmallPtrSetImpl<RefSCC *> &Set) {
  888. Set.insert(&SourceC);
  889. auto IsConnected = [&](RefSCC &RC) {
  890. for (SCC &C : RC)
  891. for (Node &N : C)
  892. for (Edge &E : *N)
  893. if (Set.count(G->lookupRefSCC(E.getNode())))
  894. return true;
  895. return false;
  896. };
  897. for (RefSCC *C : make_range(G->PostOrderRefSCCs.begin() + SourceIdx + 1,
  898. G->PostOrderRefSCCs.begin() + TargetIdx + 1))
  899. if (IsConnected(*C))
  900. Set.insert(C);
  901. };
  902. // Use a normal worklist to find which SCCs the target connects to. We still
  903. // bound the search based on the range in the postorder list we care about,
  904. // but because this is forward connectivity we just "recurse" through the
  905. // edges.
  906. auto ComputeTargetConnectedSet = [&](SmallPtrSetImpl<RefSCC *> &Set) {
  907. Set.insert(this);
  908. SmallVector<RefSCC *, 4> Worklist;
  909. Worklist.push_back(this);
  910. do {
  911. RefSCC &RC = *Worklist.pop_back_val();
  912. for (SCC &C : RC)
  913. for (Node &N : C)
  914. for (Edge &E : *N) {
  915. RefSCC &EdgeRC = *G->lookupRefSCC(E.getNode());
  916. if (G->getRefSCCIndex(EdgeRC) <= SourceIdx)
  917. // Not in the postorder sequence between source and target.
  918. continue;
  919. if (Set.insert(&EdgeRC).second)
  920. Worklist.push_back(&EdgeRC);
  921. }
  922. } while (!Worklist.empty());
  923. };
  924. // Use a generic helper to update the postorder sequence of RefSCCs and return
  925. // a range of any RefSCCs connected into a cycle by inserting this edge. This
  926. // routine will also take care of updating the indices into the postorder
  927. // sequence.
  928. iterator_range<SmallVectorImpl<RefSCC *>::iterator> MergeRange =
  929. updatePostorderSequenceForEdgeInsertion(
  930. SourceC, *this, G->PostOrderRefSCCs, G->RefSCCIndices,
  931. ComputeSourceConnectedSet, ComputeTargetConnectedSet);
  932. // Build a set, so we can do fast tests for whether a RefSCC will end up as
  933. // part of the merged RefSCC.
  934. SmallPtrSet<RefSCC *, 16> MergeSet(MergeRange.begin(), MergeRange.end());
  935. // This RefSCC will always be part of that set, so just insert it here.
  936. MergeSet.insert(this);
  937. // Now that we have identified all the SCCs which need to be merged into
  938. // a connected set with the inserted edge, merge all of them into this SCC.
  939. SmallVector<SCC *, 16> MergedSCCs;
  940. int SCCIndex = 0;
  941. for (RefSCC *RC : MergeRange) {
  942. assert(RC != this && "We're merging into the target RefSCC, so it "
  943. "shouldn't be in the range.");
  944. // Walk the inner SCCs to update their up-pointer and walk all the edges to
  945. // update any parent sets.
  946. // FIXME: We should try to find a way to avoid this (rather expensive) edge
  947. // walk by updating the parent sets in some other manner.
  948. for (SCC &InnerC : *RC) {
  949. InnerC.OuterRefSCC = this;
  950. SCCIndices[&InnerC] = SCCIndex++;
  951. for (Node &N : InnerC)
  952. G->SCCMap[&N] = &InnerC;
  953. }
  954. // Now merge in the SCCs. We can actually move here so try to reuse storage
  955. // the first time through.
  956. if (MergedSCCs.empty())
  957. MergedSCCs = std::move(RC->SCCs);
  958. else
  959. MergedSCCs.append(RC->SCCs.begin(), RC->SCCs.end());
  960. RC->SCCs.clear();
  961. DeletedRefSCCs.push_back(RC);
  962. }
  963. // Append our original SCCs to the merged list and move it into place.
  964. for (SCC &InnerC : *this)
  965. SCCIndices[&InnerC] = SCCIndex++;
  966. MergedSCCs.append(SCCs.begin(), SCCs.end());
  967. SCCs = std::move(MergedSCCs);
  968. // Remove the merged away RefSCCs from the post order sequence.
  969. for (RefSCC *RC : MergeRange)
  970. G->RefSCCIndices.erase(RC);
  971. int IndexOffset = MergeRange.end() - MergeRange.begin();
  972. auto EraseEnd =
  973. G->PostOrderRefSCCs.erase(MergeRange.begin(), MergeRange.end());
  974. for (RefSCC *RC : make_range(EraseEnd, G->PostOrderRefSCCs.end()))
  975. G->RefSCCIndices[RC] -= IndexOffset;
  976. // At this point we have a merged RefSCC with a post-order SCCs list, just
  977. // connect the nodes to form the new edge.
  978. SourceN->insertEdgeInternal(TargetN, Edge::Ref);
  979. // We return the list of SCCs which were merged so that callers can
  980. // invalidate any data they have associated with those SCCs. Note that these
  981. // SCCs are no longer in an interesting state (they are totally empty) but
  982. // the pointers will remain stable for the life of the graph itself.
  983. return DeletedRefSCCs;
  984. }
  985. void LazyCallGraph::RefSCC::removeOutgoingEdge(Node &SourceN, Node &TargetN) {
  986. assert(G->lookupRefSCC(SourceN) == this &&
  987. "The source must be a member of this RefSCC.");
  988. assert(G->lookupRefSCC(TargetN) != this &&
  989. "The target must not be a member of this RefSCC");
  990. #ifdef EXPENSIVE_CHECKS
  991. verify();
  992. auto VerifyOnExit = make_scope_exit([&]() { verify(); });
  993. #endif
  994. // First remove it from the node.
  995. bool Removed = SourceN->removeEdgeInternal(TargetN);
  996. (void)Removed;
  997. assert(Removed && "Target not in the edge set for this caller?");
  998. }
  999. SmallVector<LazyCallGraph::RefSCC *, 1>
  1000. LazyCallGraph::RefSCC::removeInternalRefEdge(Node &SourceN,
  1001. ArrayRef<Node *> TargetNs) {
  1002. // We return a list of the resulting *new* RefSCCs in post-order.
  1003. SmallVector<RefSCC *, 1> Result;
  1004. #ifdef EXPENSIVE_CHECKS
  1005. // Verify the RefSCC is valid to start with and that either we return an empty
  1006. // list of result RefSCCs and this RefSCC remains valid, or we return new
  1007. // RefSCCs and this RefSCC is dead.
  1008. verify();
  1009. auto VerifyOnExit = make_scope_exit([&]() {
  1010. // If we didn't replace our RefSCC with new ones, check that this one
  1011. // remains valid.
  1012. if (G)
  1013. verify();
  1014. });
  1015. #endif
  1016. // First remove the actual edges.
  1017. for (Node *TargetN : TargetNs) {
  1018. assert(!(*SourceN)[*TargetN].isCall() &&
  1019. "Cannot remove a call edge, it must first be made a ref edge");
  1020. bool Removed = SourceN->removeEdgeInternal(*TargetN);
  1021. (void)Removed;
  1022. assert(Removed && "Target not in the edge set for this caller?");
  1023. }
  1024. // Direct self references don't impact the ref graph at all.
  1025. if (llvm::all_of(TargetNs,
  1026. [&](Node *TargetN) { return &SourceN == TargetN; }))
  1027. return Result;
  1028. // If all targets are in the same SCC as the source, because no call edges
  1029. // were removed there is no RefSCC structure change.
  1030. SCC &SourceC = *G->lookupSCC(SourceN);
  1031. if (llvm::all_of(TargetNs, [&](Node *TargetN) {
  1032. return G->lookupSCC(*TargetN) == &SourceC;
  1033. }))
  1034. return Result;
  1035. // We build somewhat synthetic new RefSCCs by providing a postorder mapping
  1036. // for each inner SCC. We store these inside the low-link field of the nodes
  1037. // rather than associated with SCCs because this saves a round-trip through
  1038. // the node->SCC map and in the common case, SCCs are small. We will verify
  1039. // that we always give the same number to every node in the SCC such that
  1040. // these are equivalent.
  1041. int PostOrderNumber = 0;
  1042. // Reset all the other nodes to prepare for a DFS over them, and add them to
  1043. // our worklist.
  1044. SmallVector<Node *, 8> Worklist;
  1045. for (SCC *C : SCCs) {
  1046. for (Node &N : *C)
  1047. N.DFSNumber = N.LowLink = 0;
  1048. Worklist.append(C->Nodes.begin(), C->Nodes.end());
  1049. }
  1050. // Track the number of nodes in this RefSCC so that we can quickly recognize
  1051. // an important special case of the edge removal not breaking the cycle of
  1052. // this RefSCC.
  1053. const int NumRefSCCNodes = Worklist.size();
  1054. SmallVector<std::pair<Node *, EdgeSequence::iterator>, 4> DFSStack;
  1055. SmallVector<Node *, 4> PendingRefSCCStack;
  1056. do {
  1057. assert(DFSStack.empty() &&
  1058. "Cannot begin a new root with a non-empty DFS stack!");
  1059. assert(PendingRefSCCStack.empty() &&
  1060. "Cannot begin a new root with pending nodes for an SCC!");
  1061. Node *RootN = Worklist.pop_back_val();
  1062. // Skip any nodes we've already reached in the DFS.
  1063. if (RootN->DFSNumber != 0) {
  1064. assert(RootN->DFSNumber == -1 &&
  1065. "Shouldn't have any mid-DFS root nodes!");
  1066. continue;
  1067. }
  1068. RootN->DFSNumber = RootN->LowLink = 1;
  1069. int NextDFSNumber = 2;
  1070. DFSStack.emplace_back(RootN, (*RootN)->begin());
  1071. do {
  1072. auto [N, I] = DFSStack.pop_back_val();
  1073. auto E = (*N)->end();
  1074. assert(N->DFSNumber != 0 && "We should always assign a DFS number "
  1075. "before processing a node.");
  1076. while (I != E) {
  1077. Node &ChildN = I->getNode();
  1078. if (ChildN.DFSNumber == 0) {
  1079. // Mark that we should start at this child when next this node is the
  1080. // top of the stack. We don't start at the next child to ensure this
  1081. // child's lowlink is reflected.
  1082. DFSStack.emplace_back(N, I);
  1083. // Continue, resetting to the child node.
  1084. ChildN.LowLink = ChildN.DFSNumber = NextDFSNumber++;
  1085. N = &ChildN;
  1086. I = ChildN->begin();
  1087. E = ChildN->end();
  1088. continue;
  1089. }
  1090. if (ChildN.DFSNumber == -1) {
  1091. // If this child isn't currently in this RefSCC, no need to process
  1092. // it.
  1093. ++I;
  1094. continue;
  1095. }
  1096. // Track the lowest link of the children, if any are still in the stack.
  1097. // Any child not on the stack will have a LowLink of -1.
  1098. assert(ChildN.LowLink != 0 &&
  1099. "Low-link must not be zero with a non-zero DFS number.");
  1100. if (ChildN.LowLink >= 0 && ChildN.LowLink < N->LowLink)
  1101. N->LowLink = ChildN.LowLink;
  1102. ++I;
  1103. }
  1104. // We've finished processing N and its descendants, put it on our pending
  1105. // stack to eventually get merged into a RefSCC.
  1106. PendingRefSCCStack.push_back(N);
  1107. // If this node is linked to some lower entry, continue walking up the
  1108. // stack.
  1109. if (N->LowLink != N->DFSNumber) {
  1110. assert(!DFSStack.empty() &&
  1111. "We never found a viable root for a RefSCC to pop off!");
  1112. continue;
  1113. }
  1114. // Otherwise, form a new RefSCC from the top of the pending node stack.
  1115. int RefSCCNumber = PostOrderNumber++;
  1116. int RootDFSNumber = N->DFSNumber;
  1117. // Find the range of the node stack by walking down until we pass the
  1118. // root DFS number. Update the DFS numbers and low link numbers in the
  1119. // process to avoid re-walking this list where possible.
  1120. auto StackRI = find_if(reverse(PendingRefSCCStack), [&](Node *N) {
  1121. if (N->DFSNumber < RootDFSNumber)
  1122. // We've found the bottom.
  1123. return true;
  1124. // Update this node and keep scanning.
  1125. N->DFSNumber = -1;
  1126. // Save the post-order number in the lowlink field so that we can use
  1127. // it to map SCCs into new RefSCCs after we finish the DFS.
  1128. N->LowLink = RefSCCNumber;
  1129. return false;
  1130. });
  1131. auto RefSCCNodes = make_range(StackRI.base(), PendingRefSCCStack.end());
  1132. // If we find a cycle containing all nodes originally in this RefSCC then
  1133. // the removal hasn't changed the structure at all. This is an important
  1134. // special case, and we can directly exit the entire routine more
  1135. // efficiently as soon as we discover it.
  1136. if (llvm::size(RefSCCNodes) == NumRefSCCNodes) {
  1137. // Clear out the low link field as we won't need it.
  1138. for (Node *N : RefSCCNodes)
  1139. N->LowLink = -1;
  1140. // Return the empty result immediately.
  1141. return Result;
  1142. }
  1143. // We've already marked the nodes internally with the RefSCC number so
  1144. // just clear them off the stack and continue.
  1145. PendingRefSCCStack.erase(RefSCCNodes.begin(), PendingRefSCCStack.end());
  1146. } while (!DFSStack.empty());
  1147. assert(DFSStack.empty() && "Didn't flush the entire DFS stack!");
  1148. assert(PendingRefSCCStack.empty() && "Didn't flush all pending nodes!");
  1149. } while (!Worklist.empty());
  1150. assert(PostOrderNumber > 1 &&
  1151. "Should never finish the DFS when the existing RefSCC remains valid!");
  1152. // Otherwise we create a collection of new RefSCC nodes and build
  1153. // a radix-sort style map from postorder number to these new RefSCCs. We then
  1154. // append SCCs to each of these RefSCCs in the order they occurred in the
  1155. // original SCCs container.
  1156. for (int I = 0; I < PostOrderNumber; ++I)
  1157. Result.push_back(G->createRefSCC(*G));
  1158. // Insert the resulting postorder sequence into the global graph postorder
  1159. // sequence before the current RefSCC in that sequence, and then remove the
  1160. // current one.
  1161. //
  1162. // FIXME: It'd be nice to change the APIs so that we returned an iterator
  1163. // range over the global postorder sequence and generally use that sequence
  1164. // rather than building a separate result vector here.
  1165. int Idx = G->getRefSCCIndex(*this);
  1166. G->PostOrderRefSCCs.erase(G->PostOrderRefSCCs.begin() + Idx);
  1167. G->PostOrderRefSCCs.insert(G->PostOrderRefSCCs.begin() + Idx, Result.begin(),
  1168. Result.end());
  1169. for (int I : seq<int>(Idx, G->PostOrderRefSCCs.size()))
  1170. G->RefSCCIndices[G->PostOrderRefSCCs[I]] = I;
  1171. for (SCC *C : SCCs) {
  1172. // We store the SCC number in the node's low-link field above.
  1173. int SCCNumber = C->begin()->LowLink;
  1174. // Clear out all the SCC's node's low-link fields now that we're done
  1175. // using them as side-storage.
  1176. for (Node &N : *C) {
  1177. assert(N.LowLink == SCCNumber &&
  1178. "Cannot have different numbers for nodes in the same SCC!");
  1179. N.LowLink = -1;
  1180. }
  1181. RefSCC &RC = *Result[SCCNumber];
  1182. int SCCIndex = RC.SCCs.size();
  1183. RC.SCCs.push_back(C);
  1184. RC.SCCIndices[C] = SCCIndex;
  1185. C->OuterRefSCC = &RC;
  1186. }
  1187. // Now that we've moved things into the new RefSCCs, clear out our current
  1188. // one.
  1189. G = nullptr;
  1190. SCCs.clear();
  1191. SCCIndices.clear();
  1192. #ifdef EXPENSIVE_CHECKS
  1193. // Verify the new RefSCCs we've built.
  1194. for (RefSCC *RC : Result)
  1195. RC->verify();
  1196. #endif
  1197. // Return the new list of SCCs.
  1198. return Result;
  1199. }
  1200. void LazyCallGraph::RefSCC::insertTrivialCallEdge(Node &SourceN,
  1201. Node &TargetN) {
  1202. #ifdef EXPENSIVE_CHECKS
  1203. auto ExitVerifier = make_scope_exit([this] { verify(); });
  1204. // Check that we aren't breaking some invariants of the SCC graph. Note that
  1205. // this is quadratic in the number of edges in the call graph!
  1206. SCC &SourceC = *G->lookupSCC(SourceN);
  1207. SCC &TargetC = *G->lookupSCC(TargetN);
  1208. if (&SourceC != &TargetC)
  1209. assert(SourceC.isAncestorOf(TargetC) &&
  1210. "Call edge is not trivial in the SCC graph!");
  1211. #endif
  1212. // First insert it into the source or find the existing edge.
  1213. auto [Iterator, Inserted] =
  1214. SourceN->EdgeIndexMap.try_emplace(&TargetN, SourceN->Edges.size());
  1215. if (!Inserted) {
  1216. // Already an edge, just update it.
  1217. Edge &E = SourceN->Edges[Iterator->second];
  1218. if (E.isCall())
  1219. return; // Nothing to do!
  1220. E.setKind(Edge::Call);
  1221. } else {
  1222. // Create the new edge.
  1223. SourceN->Edges.emplace_back(TargetN, Edge::Call);
  1224. }
  1225. }
  1226. void LazyCallGraph::RefSCC::insertTrivialRefEdge(Node &SourceN, Node &TargetN) {
  1227. #ifdef EXPENSIVE_CHECKS
  1228. auto ExitVerifier = make_scope_exit([this] { verify(); });
  1229. // Check that we aren't breaking some invariants of the RefSCC graph.
  1230. RefSCC &SourceRC = *G->lookupRefSCC(SourceN);
  1231. RefSCC &TargetRC = *G->lookupRefSCC(TargetN);
  1232. if (&SourceRC != &TargetRC)
  1233. assert(SourceRC.isAncestorOf(TargetRC) &&
  1234. "Ref edge is not trivial in the RefSCC graph!");
  1235. #endif
  1236. // First insert it into the source or find the existing edge.
  1237. auto [Iterator, Inserted] =
  1238. SourceN->EdgeIndexMap.try_emplace(&TargetN, SourceN->Edges.size());
  1239. (void)Iterator;
  1240. if (!Inserted)
  1241. // Already an edge, we're done.
  1242. return;
  1243. // Create the new edge.
  1244. SourceN->Edges.emplace_back(TargetN, Edge::Ref);
  1245. }
  1246. void LazyCallGraph::RefSCC::replaceNodeFunction(Node &N, Function &NewF) {
  1247. Function &OldF = N.getFunction();
  1248. #ifdef EXPENSIVE_CHECKS
  1249. auto ExitVerifier = make_scope_exit([this] { verify(); });
  1250. assert(G->lookupRefSCC(N) == this &&
  1251. "Cannot replace the function of a node outside this RefSCC.");
  1252. assert(G->NodeMap.find(&NewF) == G->NodeMap.end() &&
  1253. "Must not have already walked the new function!'");
  1254. // It is important that this replacement not introduce graph changes so we
  1255. // insist that the caller has already removed every use of the original
  1256. // function and that all uses of the new function correspond to existing
  1257. // edges in the graph. The common and expected way to use this is when
  1258. // replacing the function itself in the IR without changing the call graph
  1259. // shape and just updating the analysis based on that.
  1260. assert(&OldF != &NewF && "Cannot replace a function with itself!");
  1261. assert(OldF.use_empty() &&
  1262. "Must have moved all uses from the old function to the new!");
  1263. #endif
  1264. N.replaceFunction(NewF);
  1265. // Update various call graph maps.
  1266. G->NodeMap.erase(&OldF);
  1267. G->NodeMap[&NewF] = &N;
  1268. // Update lib functions.
  1269. if (G->isLibFunction(OldF)) {
  1270. G->LibFunctions.remove(&OldF);
  1271. G->LibFunctions.insert(&NewF);
  1272. }
  1273. }
  1274. void LazyCallGraph::insertEdge(Node &SourceN, Node &TargetN, Edge::Kind EK) {
  1275. assert(SCCMap.empty() &&
  1276. "This method cannot be called after SCCs have been formed!");
  1277. return SourceN->insertEdgeInternal(TargetN, EK);
  1278. }
  1279. void LazyCallGraph::removeEdge(Node &SourceN, Node &TargetN) {
  1280. assert(SCCMap.empty() &&
  1281. "This method cannot be called after SCCs have been formed!");
  1282. bool Removed = SourceN->removeEdgeInternal(TargetN);
  1283. (void)Removed;
  1284. assert(Removed && "Target not in the edge set for this caller?");
  1285. }
  1286. void LazyCallGraph::removeDeadFunction(Function &F) {
  1287. // FIXME: This is unnecessarily restrictive. We should be able to remove
  1288. // functions which recursively call themselves.
  1289. assert(F.hasZeroLiveUses() &&
  1290. "This routine should only be called on trivially dead functions!");
  1291. // We shouldn't remove library functions as they are never really dead while
  1292. // the call graph is in use -- every function definition refers to them.
  1293. assert(!isLibFunction(F) &&
  1294. "Must not remove lib functions from the call graph!");
  1295. auto NI = NodeMap.find(&F);
  1296. if (NI == NodeMap.end())
  1297. // Not in the graph at all!
  1298. return;
  1299. Node &N = *NI->second;
  1300. // Cannot remove a function which has yet to be visited in the DFS walk, so
  1301. // if we have a node at all then we must have an SCC and RefSCC.
  1302. auto CI = SCCMap.find(&N);
  1303. assert(CI != SCCMap.end() &&
  1304. "Tried to remove a node without an SCC after DFS walk started!");
  1305. SCC &C = *CI->second;
  1306. RefSCC *RC = &C.getOuterRefSCC();
  1307. // In extremely rare cases, we can delete a dead function which is still in a
  1308. // non-trivial RefSCC. This can happen due to spurious ref edges sticking
  1309. // around after an IR function reference is removed.
  1310. if (RC->size() != 1) {
  1311. SmallVector<Node *, 0> NodesInRC;
  1312. for (SCC &OtherC : *RC) {
  1313. for (Node &OtherN : OtherC)
  1314. NodesInRC.push_back(&OtherN);
  1315. }
  1316. for (Node *OtherN : NodesInRC) {
  1317. if ((*OtherN)->lookup(N)) {
  1318. auto NewRefSCCs =
  1319. RC->removeInternalRefEdge(*OtherN, ArrayRef<Node *>(&N));
  1320. // If we've split into multiple RefSCCs, RC is now invalid and the
  1321. // RefSCC containing C will be different.
  1322. if (!NewRefSCCs.empty())
  1323. RC = &C.getOuterRefSCC();
  1324. }
  1325. }
  1326. }
  1327. NodeMap.erase(NI);
  1328. EntryEdges.removeEdgeInternal(N);
  1329. SCCMap.erase(CI);
  1330. // This node must be the only member of its SCC as it has no callers, and
  1331. // that SCC must be the only member of a RefSCC as it has no references.
  1332. // Validate these properties first.
  1333. assert(C.size() == 1 && "Dead functions must be in a singular SCC");
  1334. assert(RC->size() == 1 && "Dead functions must be in a singular RefSCC");
  1335. // Finally clear out all the data structures from the node down through the
  1336. // components. postorder_ref_scc_iterator will skip empty RefSCCs, so no need
  1337. // to adjust LazyCallGraph data structures.
  1338. N.clear();
  1339. N.G = nullptr;
  1340. N.F = nullptr;
  1341. C.clear();
  1342. RC->clear();
  1343. RC->G = nullptr;
  1344. // Nothing to delete as all the objects are allocated in stable bump pointer
  1345. // allocators.
  1346. }
  1347. // Gets the Edge::Kind from one function to another by looking at the function's
  1348. // instructions. Asserts if there is no edge.
  1349. // Useful for determining what type of edge should exist between functions when
  1350. // the edge hasn't been created yet.
  1351. static LazyCallGraph::Edge::Kind getEdgeKind(Function &OriginalFunction,
  1352. Function &NewFunction) {
  1353. // In release builds, assume that if there are no direct calls to the new
  1354. // function, then there is a ref edge. In debug builds, keep track of
  1355. // references to assert that there is actually a ref edge if there is no call
  1356. // edge.
  1357. #ifndef NDEBUG
  1358. SmallVector<Constant *, 16> Worklist;
  1359. SmallPtrSet<Constant *, 16> Visited;
  1360. #endif
  1361. for (Instruction &I : instructions(OriginalFunction)) {
  1362. if (auto *CB = dyn_cast<CallBase>(&I)) {
  1363. if (Function *Callee = CB->getCalledFunction()) {
  1364. if (Callee == &NewFunction)
  1365. return LazyCallGraph::Edge::Kind::Call;
  1366. }
  1367. }
  1368. #ifndef NDEBUG
  1369. for (Value *Op : I.operand_values()) {
  1370. if (Constant *C = dyn_cast<Constant>(Op)) {
  1371. if (Visited.insert(C).second)
  1372. Worklist.push_back(C);
  1373. }
  1374. }
  1375. #endif
  1376. }
  1377. #ifndef NDEBUG
  1378. bool FoundNewFunction = false;
  1379. LazyCallGraph::visitReferences(Worklist, Visited, [&](Function &F) {
  1380. if (&F == &NewFunction)
  1381. FoundNewFunction = true;
  1382. });
  1383. assert(FoundNewFunction && "No edge from original function to new function");
  1384. #endif
  1385. return LazyCallGraph::Edge::Kind::Ref;
  1386. }
  1387. void LazyCallGraph::addSplitFunction(Function &OriginalFunction,
  1388. Function &NewFunction) {
  1389. assert(lookup(OriginalFunction) &&
  1390. "Original function's node should already exist");
  1391. Node &OriginalN = get(OriginalFunction);
  1392. SCC *OriginalC = lookupSCC(OriginalN);
  1393. RefSCC *OriginalRC = lookupRefSCC(OriginalN);
  1394. #ifdef EXPENSIVE_CHECKS
  1395. OriginalRC->verify();
  1396. auto VerifyOnExit = make_scope_exit([&]() { OriginalRC->verify(); });
  1397. #endif
  1398. assert(!lookup(NewFunction) &&
  1399. "New function's node should not already exist");
  1400. Node &NewN = initNode(NewFunction);
  1401. Edge::Kind EK = getEdgeKind(OriginalFunction, NewFunction);
  1402. SCC *NewC = nullptr;
  1403. for (Edge &E : *NewN) {
  1404. Node &EN = E.getNode();
  1405. if (EK == Edge::Kind::Call && E.isCall() && lookupSCC(EN) == OriginalC) {
  1406. // If the edge to the new function is a call edge and there is a call edge
  1407. // from the new function to any function in the original function's SCC,
  1408. // it is in the same SCC (and RefSCC) as the original function.
  1409. NewC = OriginalC;
  1410. NewC->Nodes.push_back(&NewN);
  1411. break;
  1412. }
  1413. }
  1414. if (!NewC) {
  1415. for (Edge &E : *NewN) {
  1416. Node &EN = E.getNode();
  1417. if (lookupRefSCC(EN) == OriginalRC) {
  1418. // If there is any edge from the new function to any function in the
  1419. // original function's RefSCC, it is in the same RefSCC as the original
  1420. // function but a new SCC.
  1421. RefSCC *NewRC = OriginalRC;
  1422. NewC = createSCC(*NewRC, SmallVector<Node *, 1>({&NewN}));
  1423. // The new function's SCC is not the same as the original function's
  1424. // SCC, since that case was handled earlier. If the edge from the
  1425. // original function to the new function was a call edge, then we need
  1426. // to insert the newly created function's SCC before the original
  1427. // function's SCC. Otherwise, either the new SCC comes after the
  1428. // original function's SCC, or it doesn't matter, and in both cases we
  1429. // can add it to the very end.
  1430. int InsertIndex = EK == Edge::Kind::Call ? NewRC->SCCIndices[OriginalC]
  1431. : NewRC->SCCIndices.size();
  1432. NewRC->SCCs.insert(NewRC->SCCs.begin() + InsertIndex, NewC);
  1433. for (int I = InsertIndex, Size = NewRC->SCCs.size(); I < Size; ++I)
  1434. NewRC->SCCIndices[NewRC->SCCs[I]] = I;
  1435. break;
  1436. }
  1437. }
  1438. }
  1439. if (!NewC) {
  1440. // We didn't find any edges back to the original function's RefSCC, so the
  1441. // new function belongs in a new RefSCC. The new RefSCC goes before the
  1442. // original function's RefSCC.
  1443. RefSCC *NewRC = createRefSCC(*this);
  1444. NewC = createSCC(*NewRC, SmallVector<Node *, 1>({&NewN}));
  1445. NewRC->SCCIndices[NewC] = 0;
  1446. NewRC->SCCs.push_back(NewC);
  1447. auto OriginalRCIndex = RefSCCIndices.find(OriginalRC)->second;
  1448. PostOrderRefSCCs.insert(PostOrderRefSCCs.begin() + OriginalRCIndex, NewRC);
  1449. for (int I = OriginalRCIndex, Size = PostOrderRefSCCs.size(); I < Size; ++I)
  1450. RefSCCIndices[PostOrderRefSCCs[I]] = I;
  1451. }
  1452. SCCMap[&NewN] = NewC;
  1453. OriginalN->insertEdgeInternal(NewN, EK);
  1454. }
  1455. void LazyCallGraph::addSplitRefRecursiveFunctions(
  1456. Function &OriginalFunction, ArrayRef<Function *> NewFunctions) {
  1457. assert(!NewFunctions.empty() && "Can't add zero functions");
  1458. assert(lookup(OriginalFunction) &&
  1459. "Original function's node should already exist");
  1460. Node &OriginalN = get(OriginalFunction);
  1461. RefSCC *OriginalRC = lookupRefSCC(OriginalN);
  1462. #ifdef EXPENSIVE_CHECKS
  1463. OriginalRC->verify();
  1464. auto VerifyOnExit = make_scope_exit([&]() {
  1465. OriginalRC->verify();
  1466. for (Function *NewFunction : NewFunctions)
  1467. lookupRefSCC(get(*NewFunction))->verify();
  1468. });
  1469. #endif
  1470. bool ExistsRefToOriginalRefSCC = false;
  1471. for (Function *NewFunction : NewFunctions) {
  1472. Node &NewN = initNode(*NewFunction);
  1473. OriginalN->insertEdgeInternal(NewN, Edge::Kind::Ref);
  1474. // Check if there is any edge from any new function back to any function in
  1475. // the original function's RefSCC.
  1476. for (Edge &E : *NewN) {
  1477. if (lookupRefSCC(E.getNode()) == OriginalRC) {
  1478. ExistsRefToOriginalRefSCC = true;
  1479. break;
  1480. }
  1481. }
  1482. }
  1483. RefSCC *NewRC;
  1484. if (ExistsRefToOriginalRefSCC) {
  1485. // If there is any edge from any new function to any function in the
  1486. // original function's RefSCC, all new functions will be in the same RefSCC
  1487. // as the original function.
  1488. NewRC = OriginalRC;
  1489. } else {
  1490. // Otherwise the new functions are in their own RefSCC.
  1491. NewRC = createRefSCC(*this);
  1492. // The new RefSCC goes before the original function's RefSCC in postorder
  1493. // since there are only edges from the original function's RefSCC to the new
  1494. // RefSCC.
  1495. auto OriginalRCIndex = RefSCCIndices.find(OriginalRC)->second;
  1496. PostOrderRefSCCs.insert(PostOrderRefSCCs.begin() + OriginalRCIndex, NewRC);
  1497. for (int I = OriginalRCIndex, Size = PostOrderRefSCCs.size(); I < Size; ++I)
  1498. RefSCCIndices[PostOrderRefSCCs[I]] = I;
  1499. }
  1500. for (Function *NewFunction : NewFunctions) {
  1501. Node &NewN = get(*NewFunction);
  1502. // Each new function is in its own new SCC. The original function can only
  1503. // have a ref edge to new functions, and no other existing functions can
  1504. // have references to new functions. Each new function only has a ref edge
  1505. // to the other new functions.
  1506. SCC *NewC = createSCC(*NewRC, SmallVector<Node *, 1>({&NewN}));
  1507. // The new SCCs are either sibling SCCs or parent SCCs to all other existing
  1508. // SCCs in the RefSCC. Either way, they can go at the back of the postorder
  1509. // SCC list.
  1510. auto Index = NewRC->SCCIndices.size();
  1511. NewRC->SCCIndices[NewC] = Index;
  1512. NewRC->SCCs.push_back(NewC);
  1513. SCCMap[&NewN] = NewC;
  1514. }
  1515. #ifndef NDEBUG
  1516. for (Function *F1 : NewFunctions) {
  1517. assert(getEdgeKind(OriginalFunction, *F1) == Edge::Kind::Ref &&
  1518. "Expected ref edges from original function to every new function");
  1519. Node &N1 = get(*F1);
  1520. for (Function *F2 : NewFunctions) {
  1521. if (F1 == F2)
  1522. continue;
  1523. Node &N2 = get(*F2);
  1524. assert(!N1->lookup(N2)->isCall() &&
  1525. "Edges between new functions must be ref edges");
  1526. }
  1527. }
  1528. #endif
  1529. }
  1530. LazyCallGraph::Node &LazyCallGraph::insertInto(Function &F, Node *&MappedN) {
  1531. return *new (MappedN = BPA.Allocate()) Node(*this, F);
  1532. }
  1533. void LazyCallGraph::updateGraphPtrs() {
  1534. // Walk the node map to update their graph pointers. While this iterates in
  1535. // an unstable order, the order has no effect, so it remains correct.
  1536. for (auto &FunctionNodePair : NodeMap)
  1537. FunctionNodePair.second->G = this;
  1538. for (auto *RC : PostOrderRefSCCs)
  1539. RC->G = this;
  1540. }
  1541. LazyCallGraph::Node &LazyCallGraph::initNode(Function &F) {
  1542. Node &N = get(F);
  1543. N.DFSNumber = N.LowLink = -1;
  1544. N.populate();
  1545. NodeMap[&F] = &N;
  1546. return N;
  1547. }
  1548. template <typename RootsT, typename GetBeginT, typename GetEndT,
  1549. typename GetNodeT, typename FormSCCCallbackT>
  1550. void LazyCallGraph::buildGenericSCCs(RootsT &&Roots, GetBeginT &&GetBegin,
  1551. GetEndT &&GetEnd, GetNodeT &&GetNode,
  1552. FormSCCCallbackT &&FormSCC) {
  1553. using EdgeItT = decltype(GetBegin(std::declval<Node &>()));
  1554. SmallVector<std::pair<Node *, EdgeItT>, 16> DFSStack;
  1555. SmallVector<Node *, 16> PendingSCCStack;
  1556. // Scan down the stack and DFS across the call edges.
  1557. for (Node *RootN : Roots) {
  1558. assert(DFSStack.empty() &&
  1559. "Cannot begin a new root with a non-empty DFS stack!");
  1560. assert(PendingSCCStack.empty() &&
  1561. "Cannot begin a new root with pending nodes for an SCC!");
  1562. // Skip any nodes we've already reached in the DFS.
  1563. if (RootN->DFSNumber != 0) {
  1564. assert(RootN->DFSNumber == -1 &&
  1565. "Shouldn't have any mid-DFS root nodes!");
  1566. continue;
  1567. }
  1568. RootN->DFSNumber = RootN->LowLink = 1;
  1569. int NextDFSNumber = 2;
  1570. DFSStack.emplace_back(RootN, GetBegin(*RootN));
  1571. do {
  1572. auto [N, I] = DFSStack.pop_back_val();
  1573. auto E = GetEnd(*N);
  1574. while (I != E) {
  1575. Node &ChildN = GetNode(I);
  1576. if (ChildN.DFSNumber == 0) {
  1577. // We haven't yet visited this child, so descend, pushing the current
  1578. // node onto the stack.
  1579. DFSStack.emplace_back(N, I);
  1580. ChildN.DFSNumber = ChildN.LowLink = NextDFSNumber++;
  1581. N = &ChildN;
  1582. I = GetBegin(*N);
  1583. E = GetEnd(*N);
  1584. continue;
  1585. }
  1586. // If the child has already been added to some child component, it
  1587. // couldn't impact the low-link of this parent because it isn't
  1588. // connected, and thus its low-link isn't relevant so skip it.
  1589. if (ChildN.DFSNumber == -1) {
  1590. ++I;
  1591. continue;
  1592. }
  1593. // Track the lowest linked child as the lowest link for this node.
  1594. assert(ChildN.LowLink > 0 && "Must have a positive low-link number!");
  1595. if (ChildN.LowLink < N->LowLink)
  1596. N->LowLink = ChildN.LowLink;
  1597. // Move to the next edge.
  1598. ++I;
  1599. }
  1600. // We've finished processing N and its descendants, put it on our pending
  1601. // SCC stack to eventually get merged into an SCC of nodes.
  1602. PendingSCCStack.push_back(N);
  1603. // If this node is linked to some lower entry, continue walking up the
  1604. // stack.
  1605. if (N->LowLink != N->DFSNumber)
  1606. continue;
  1607. // Otherwise, we've completed an SCC. Append it to our post order list of
  1608. // SCCs.
  1609. int RootDFSNumber = N->DFSNumber;
  1610. // Find the range of the node stack by walking down until we pass the
  1611. // root DFS number.
  1612. auto SCCNodes = make_range(
  1613. PendingSCCStack.rbegin(),
  1614. find_if(reverse(PendingSCCStack), [RootDFSNumber](const Node *N) {
  1615. return N->DFSNumber < RootDFSNumber;
  1616. }));
  1617. // Form a new SCC out of these nodes and then clear them off our pending
  1618. // stack.
  1619. FormSCC(SCCNodes);
  1620. PendingSCCStack.erase(SCCNodes.end().base(), PendingSCCStack.end());
  1621. } while (!DFSStack.empty());
  1622. }
  1623. }
  1624. /// Build the internal SCCs for a RefSCC from a sequence of nodes.
  1625. ///
  1626. /// Appends the SCCs to the provided vector and updates the map with their
  1627. /// indices. Both the vector and map must be empty when passed into this
  1628. /// routine.
  1629. void LazyCallGraph::buildSCCs(RefSCC &RC, node_stack_range Nodes) {
  1630. assert(RC.SCCs.empty() && "Already built SCCs!");
  1631. assert(RC.SCCIndices.empty() && "Already mapped SCC indices!");
  1632. for (Node *N : Nodes) {
  1633. assert(N->LowLink >= (*Nodes.begin())->LowLink &&
  1634. "We cannot have a low link in an SCC lower than its root on the "
  1635. "stack!");
  1636. // This node will go into the next RefSCC, clear out its DFS and low link
  1637. // as we scan.
  1638. N->DFSNumber = N->LowLink = 0;
  1639. }
  1640. // Each RefSCC contains a DAG of the call SCCs. To build these, we do
  1641. // a direct walk of the call edges using Tarjan's algorithm. We reuse the
  1642. // internal storage as we won't need it for the outer graph's DFS any longer.
  1643. buildGenericSCCs(
  1644. Nodes, [](Node &N) { return N->call_begin(); },
  1645. [](Node &N) { return N->call_end(); },
  1646. [](EdgeSequence::call_iterator I) -> Node & { return I->getNode(); },
  1647. [this, &RC](node_stack_range Nodes) {
  1648. RC.SCCs.push_back(createSCC(RC, Nodes));
  1649. for (Node &N : *RC.SCCs.back()) {
  1650. N.DFSNumber = N.LowLink = -1;
  1651. SCCMap[&N] = RC.SCCs.back();
  1652. }
  1653. });
  1654. // Wire up the SCC indices.
  1655. for (int I = 0, Size = RC.SCCs.size(); I < Size; ++I)
  1656. RC.SCCIndices[RC.SCCs[I]] = I;
  1657. }
  1658. void LazyCallGraph::buildRefSCCs() {
  1659. if (EntryEdges.empty() || !PostOrderRefSCCs.empty())
  1660. // RefSCCs are either non-existent or already built!
  1661. return;
  1662. assert(RefSCCIndices.empty() && "Already mapped RefSCC indices!");
  1663. SmallVector<Node *, 16> Roots;
  1664. for (Edge &E : *this)
  1665. Roots.push_back(&E.getNode());
  1666. // The roots will be iterated in order.
  1667. buildGenericSCCs(
  1668. Roots,
  1669. [](Node &N) {
  1670. // We need to populate each node as we begin to walk its edges.
  1671. N.populate();
  1672. return N->begin();
  1673. },
  1674. [](Node &N) { return N->end(); },
  1675. [](EdgeSequence::iterator I) -> Node & { return I->getNode(); },
  1676. [this](node_stack_range Nodes) {
  1677. RefSCC *NewRC = createRefSCC(*this);
  1678. buildSCCs(*NewRC, Nodes);
  1679. // Push the new node into the postorder list and remember its position
  1680. // in the index map.
  1681. bool Inserted =
  1682. RefSCCIndices.try_emplace(NewRC, PostOrderRefSCCs.size()).second;
  1683. (void)Inserted;
  1684. assert(Inserted && "Cannot already have this RefSCC in the index map!");
  1685. PostOrderRefSCCs.push_back(NewRC);
  1686. #ifdef EXPENSIVE_CHECKS
  1687. NewRC->verify();
  1688. #endif
  1689. });
  1690. }
  1691. void LazyCallGraph::visitReferences(SmallVectorImpl<Constant *> &Worklist,
  1692. SmallPtrSetImpl<Constant *> &Visited,
  1693. function_ref<void(Function &)> Callback) {
  1694. while (!Worklist.empty()) {
  1695. Constant *C = Worklist.pop_back_val();
  1696. if (Function *F = dyn_cast<Function>(C)) {
  1697. if (!F->isDeclaration())
  1698. Callback(*F);
  1699. continue;
  1700. }
  1701. // blockaddresses are weird and don't participate in the call graph anyway,
  1702. // skip them.
  1703. if (isa<BlockAddress>(C))
  1704. continue;
  1705. for (Value *Op : C->operand_values())
  1706. if (Visited.insert(cast<Constant>(Op)).second)
  1707. Worklist.push_back(cast<Constant>(Op));
  1708. }
  1709. }
  1710. AnalysisKey LazyCallGraphAnalysis::Key;
  1711. LazyCallGraphPrinterPass::LazyCallGraphPrinterPass(raw_ostream &OS) : OS(OS) {}
  1712. static void printNode(raw_ostream &OS, LazyCallGraph::Node &N) {
  1713. OS << " Edges in function: " << N.getFunction().getName() << "\n";
  1714. for (LazyCallGraph::Edge &E : N.populate())
  1715. OS << " " << (E.isCall() ? "call" : "ref ") << " -> "
  1716. << E.getFunction().getName() << "\n";
  1717. OS << "\n";
  1718. }
  1719. static void printSCC(raw_ostream &OS, LazyCallGraph::SCC &C) {
  1720. OS << " SCC with " << C.size() << " functions:\n";
  1721. for (LazyCallGraph::Node &N : C)
  1722. OS << " " << N.getFunction().getName() << "\n";
  1723. }
  1724. static void printRefSCC(raw_ostream &OS, LazyCallGraph::RefSCC &C) {
  1725. OS << " RefSCC with " << C.size() << " call SCCs:\n";
  1726. for (LazyCallGraph::SCC &InnerC : C)
  1727. printSCC(OS, InnerC);
  1728. OS << "\n";
  1729. }
  1730. PreservedAnalyses LazyCallGraphPrinterPass::run(Module &M,
  1731. ModuleAnalysisManager &AM) {
  1732. LazyCallGraph &G = AM.getResult<LazyCallGraphAnalysis>(M);
  1733. OS << "Printing the call graph for module: " << M.getModuleIdentifier()
  1734. << "\n\n";
  1735. for (Function &F : M)
  1736. printNode(OS, G.get(F));
  1737. G.buildRefSCCs();
  1738. for (LazyCallGraph::RefSCC &C : G.postorder_ref_sccs())
  1739. printRefSCC(OS, C);
  1740. return PreservedAnalyses::all();
  1741. }
  1742. LazyCallGraphDOTPrinterPass::LazyCallGraphDOTPrinterPass(raw_ostream &OS)
  1743. : OS(OS) {}
  1744. static void printNodeDOT(raw_ostream &OS, LazyCallGraph::Node &N) {
  1745. std::string Name =
  1746. "\"" + DOT::EscapeString(std::string(N.getFunction().getName())) + "\"";
  1747. for (LazyCallGraph::Edge &E : N.populate()) {
  1748. OS << " " << Name << " -> \""
  1749. << DOT::EscapeString(std::string(E.getFunction().getName())) << "\"";
  1750. if (!E.isCall()) // It is a ref edge.
  1751. OS << " [style=dashed,label=\"ref\"]";
  1752. OS << ";\n";
  1753. }
  1754. OS << "\n";
  1755. }
  1756. PreservedAnalyses LazyCallGraphDOTPrinterPass::run(Module &M,
  1757. ModuleAnalysisManager &AM) {
  1758. LazyCallGraph &G = AM.getResult<LazyCallGraphAnalysis>(M);
  1759. OS << "digraph \"" << DOT::EscapeString(M.getModuleIdentifier()) << "\" {\n";
  1760. for (Function &F : M)
  1761. printNodeDOT(OS, G.get(F));
  1762. OS << "}\n";
  1763. return PreservedAnalyses::all();
  1764. }