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