LazyCallGraph.cpp 73 KB

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