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