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