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