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