LazyCallGraph.cpp 74 KB

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