LazyCallGraph.cpp 73 KB

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