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