1 //===- CGSCCPassManager.cpp - Managing & running CGSCC passes -------------===//
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/CGSCCPassManager.h"
10 #include "llvm/ADT/ArrayRef.h"
11 #include "llvm/ADT/Optional.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/ADT/SetVector.h"
14 #include "llvm/ADT/SmallPtrSet.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/iterator_range.h"
17 #include "llvm/Analysis/LazyCallGraph.h"
18 #include "llvm/IR/Constant.h"
19 #include "llvm/IR/InstIterator.h"
20 #include "llvm/IR/Instruction.h"
21 #include "llvm/IR/PassManager.h"
22 #include "llvm/IR/PassManagerImpl.h"
23 #include "llvm/IR/ValueHandle.h"
24 #include "llvm/Support/Casting.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/TimeProfiler.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include <algorithm>
31 #include <cassert>
32 #include <iterator>
33 
34 #define DEBUG_TYPE "cgscc"
35 
36 using namespace llvm;
37 
38 // Explicit template instantiations and specialization definitions for core
39 // template typedefs.
40 namespace llvm {
41 
42 static cl::opt<bool> AbortOnMaxDevirtIterationsReached(
43     "abort-on-max-devirt-iterations-reached",
44     cl::desc("Abort when the max iterations for devirtualization CGSCC repeat "
45              "pass is reached"));
46 
47 AnalysisKey FunctionStatusAnalysis::Key;
48 
49 // Explicit instantiations for the core proxy templates.
50 template class AllAnalysesOn<LazyCallGraph::SCC>;
51 template class AnalysisManager<LazyCallGraph::SCC, LazyCallGraph &>;
52 template class PassManager<LazyCallGraph::SCC, CGSCCAnalysisManager,
53                            LazyCallGraph &, CGSCCUpdateResult &>;
54 template class InnerAnalysisManagerProxy<CGSCCAnalysisManager, Module>;
55 template class OuterAnalysisManagerProxy<ModuleAnalysisManager,
56                                          LazyCallGraph::SCC, LazyCallGraph &>;
57 template class OuterAnalysisManagerProxy<CGSCCAnalysisManager, Function>;
58 
59 /// Explicitly specialize the pass manager run method to handle call graph
60 /// updates.
61 template <>
62 PreservedAnalyses
63 PassManager<LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &,
64             CGSCCUpdateResult &>::run(LazyCallGraph::SCC &InitialC,
65                                       CGSCCAnalysisManager &AM,
66                                       LazyCallGraph &G, CGSCCUpdateResult &UR) {
67   // Request PassInstrumentation from analysis manager, will use it to run
68   // instrumenting callbacks for the passes later.
69   PassInstrumentation PI =
70       AM.getResult<PassInstrumentationAnalysis>(InitialC, G);
71 
72   PreservedAnalyses PA = PreservedAnalyses::all();
73 
74   // The SCC may be refined while we are running passes over it, so set up
75   // a pointer that we can update.
76   LazyCallGraph::SCC *C = &InitialC;
77 
78   // Get Function analysis manager from its proxy.
79   FunctionAnalysisManager &FAM =
80       AM.getCachedResult<FunctionAnalysisManagerCGSCCProxy>(*C)->getManager();
81 
82   for (auto &Pass : Passes) {
83     // Check the PassInstrumentation's BeforePass callbacks before running the
84     // pass, skip its execution completely if asked to (callback returns false).
85     if (!PI.runBeforePass(*Pass, *C))
86       continue;
87 
88     PreservedAnalyses PassPA;
89     {
90       TimeTraceScope TimeScope(Pass->name());
91       PassPA = Pass->run(*C, AM, G, UR);
92     }
93 
94     if (UR.InvalidatedSCCs.count(C))
95       PI.runAfterPassInvalidated<LazyCallGraph::SCC>(*Pass, PassPA);
96     else
97       PI.runAfterPass<LazyCallGraph::SCC>(*Pass, *C, PassPA);
98 
99     // Update the SCC if necessary.
100     C = UR.UpdatedC ? UR.UpdatedC : C;
101     if (UR.UpdatedC) {
102       // If C is updated, also create a proxy and update FAM inside the result.
103       auto *ResultFAMCP =
104           &AM.getResult<FunctionAnalysisManagerCGSCCProxy>(*C, G);
105       ResultFAMCP->updateFAM(FAM);
106     }
107 
108     // If the CGSCC pass wasn't able to provide a valid updated SCC, the
109     // current SCC may simply need to be skipped if invalid.
110     if (UR.InvalidatedSCCs.count(C)) {
111       LLVM_DEBUG(dbgs() << "Skipping invalidated root or island SCC!\n");
112       break;
113     }
114     // Check that we didn't miss any update scenario.
115     assert(C->begin() != C->end() && "Cannot have an empty SCC!");
116 
117     // Update the analysis manager as each pass runs and potentially
118     // invalidates analyses.
119     AM.invalidate(*C, PassPA);
120 
121     // Finally, we intersect the final preserved analyses to compute the
122     // aggregate preserved set for this pass manager.
123     PA.intersect(std::move(PassPA));
124 
125     // FIXME: Historically, the pass managers all called the LLVM context's
126     // yield function here. We don't have a generic way to acquire the
127     // context and it isn't yet clear what the right pattern is for yielding
128     // in the new pass manager so it is currently omitted.
129     // ...getContext().yield();
130   }
131 
132   // Before we mark all of *this* SCC's analyses as preserved below, intersect
133   // this with the cross-SCC preserved analysis set. This is used to allow
134   // CGSCC passes to mutate ancestor SCCs and still trigger proper invalidation
135   // for them.
136   UR.CrossSCCPA.intersect(PA);
137 
138   // Invalidation was handled after each pass in the above loop for the current
139   // SCC. Therefore, the remaining analysis results in the AnalysisManager are
140   // preserved. We mark this with a set so that we don't need to inspect each
141   // one individually.
142   PA.preserveSet<AllAnalysesOn<LazyCallGraph::SCC>>();
143 
144   return PA;
145 }
146 
147 PreservedAnalyses
148 ModuleToPostOrderCGSCCPassAdaptor::run(Module &M, ModuleAnalysisManager &AM) {
149   // Setup the CGSCC analysis manager from its proxy.
150   CGSCCAnalysisManager &CGAM =
151       AM.getResult<CGSCCAnalysisManagerModuleProxy>(M).getManager();
152 
153   // Get the call graph for this module.
154   LazyCallGraph &CG = AM.getResult<LazyCallGraphAnalysis>(M);
155 
156   // Get Function analysis manager from its proxy.
157   FunctionAnalysisManager &FAM =
158       AM.getCachedResult<FunctionAnalysisManagerModuleProxy>(M)->getManager();
159 
160   // We keep worklists to allow us to push more work onto the pass manager as
161   // the passes are run.
162   SmallPriorityWorklist<LazyCallGraph::RefSCC *, 1> RCWorklist;
163   SmallPriorityWorklist<LazyCallGraph::SCC *, 1> CWorklist;
164 
165   // Keep sets for invalidated SCCs and RefSCCs that should be skipped when
166   // iterating off the worklists.
167   SmallPtrSet<LazyCallGraph::RefSCC *, 4> InvalidRefSCCSet;
168   SmallPtrSet<LazyCallGraph::SCC *, 4> InvalidSCCSet;
169 
170   SmallDenseSet<std::pair<LazyCallGraph::Node *, LazyCallGraph::SCC *>, 4>
171       InlinedInternalEdges;
172 
173   CGSCCUpdateResult UR = {
174       RCWorklist, CWorklist, InvalidRefSCCSet,         InvalidSCCSet,
175       nullptr,    nullptr,   PreservedAnalyses::all(), InlinedInternalEdges,
176       {}};
177 
178   // Request PassInstrumentation from analysis manager, will use it to run
179   // instrumenting callbacks for the passes later.
180   PassInstrumentation PI = AM.getResult<PassInstrumentationAnalysis>(M);
181 
182   PreservedAnalyses PA = PreservedAnalyses::all();
183   CG.buildRefSCCs();
184   for (auto RCI = CG.postorder_ref_scc_begin(),
185             RCE = CG.postorder_ref_scc_end();
186        RCI != RCE;) {
187     assert(RCWorklist.empty() &&
188            "Should always start with an empty RefSCC worklist");
189     // The postorder_ref_sccs range we are walking is lazily constructed, so
190     // we only push the first one onto the worklist. The worklist allows us
191     // to capture *new* RefSCCs created during transformations.
192     //
193     // We really want to form RefSCCs lazily because that makes them cheaper
194     // to update as the program is simplified and allows us to have greater
195     // cache locality as forming a RefSCC touches all the parts of all the
196     // functions within that RefSCC.
197     //
198     // We also eagerly increment the iterator to the next position because
199     // the CGSCC passes below may delete the current RefSCC.
200     RCWorklist.insert(&*RCI++);
201 
202     do {
203       LazyCallGraph::RefSCC *RC = RCWorklist.pop_back_val();
204       if (InvalidRefSCCSet.count(RC)) {
205         LLVM_DEBUG(dbgs() << "Skipping an invalid RefSCC...\n");
206         continue;
207       }
208 
209       assert(CWorklist.empty() &&
210              "Should always start with an empty SCC worklist");
211 
212       LLVM_DEBUG(dbgs() << "Running an SCC pass across the RefSCC: " << *RC
213                         << "\n");
214 
215       // The top of the worklist may *also* be the same SCC we just ran over
216       // (and invalidated for). Keep track of that last SCC we processed due
217       // to SCC update to avoid redundant processing when an SCC is both just
218       // updated itself and at the top of the worklist.
219       LazyCallGraph::SCC *LastUpdatedC = nullptr;
220 
221       // Push the initial SCCs in reverse post-order as we'll pop off the
222       // back and so see this in post-order.
223       for (LazyCallGraph::SCC &C : llvm::reverse(*RC))
224         CWorklist.insert(&C);
225 
226       do {
227         LazyCallGraph::SCC *C = CWorklist.pop_back_val();
228         // Due to call graph mutations, we may have invalid SCCs or SCCs from
229         // other RefSCCs in the worklist. The invalid ones are dead and the
230         // other RefSCCs should be queued above, so we just need to skip both
231         // scenarios here.
232         if (InvalidSCCSet.count(C)) {
233           LLVM_DEBUG(dbgs() << "Skipping an invalid SCC...\n");
234           continue;
235         }
236         if (LastUpdatedC == C) {
237           LLVM_DEBUG(dbgs() << "Skipping redundant run on SCC: " << *C << "\n");
238           continue;
239         }
240         if (&C->getOuterRefSCC() != RC) {
241           LLVM_DEBUG(dbgs() << "Skipping an SCC that is now part of some other "
242                                "RefSCC...\n");
243           continue;
244         }
245 
246         // Ensure we can proxy analysis updates from the CGSCC analysis manager
247         // into the the Function analysis manager by getting a proxy here.
248         // This also needs to update the FunctionAnalysisManager, as this may be
249         // the first time we see this SCC.
250         CGAM.getResult<FunctionAnalysisManagerCGSCCProxy>(*C, CG).updateFAM(
251             FAM);
252 
253         // Each time we visit a new SCC pulled off the worklist,
254         // a transformation of a child SCC may have also modified this parent
255         // and invalidated analyses. So we invalidate using the update record's
256         // cross-SCC preserved set. This preserved set is intersected by any
257         // CGSCC pass that handles invalidation (primarily pass managers) prior
258         // to marking its SCC as preserved. That lets us track everything that
259         // might need invalidation across SCCs without excessive invalidations
260         // on a single SCC.
261         //
262         // This essentially allows SCC passes to freely invalidate analyses
263         // of any ancestor SCC. If this becomes detrimental to successfully
264         // caching analyses, we could force each SCC pass to manually
265         // invalidate the analyses for any SCCs other than themselves which
266         // are mutated. However, that seems to lose the robustness of the
267         // pass-manager driven invalidation scheme.
268         CGAM.invalidate(*C, UR.CrossSCCPA);
269 
270         do {
271           // Check that we didn't miss any update scenario.
272           assert(!InvalidSCCSet.count(C) && "Processing an invalid SCC!");
273           assert(C->begin() != C->end() && "Cannot have an empty SCC!");
274           assert(&C->getOuterRefSCC() == RC &&
275                  "Processing an SCC in a different RefSCC!");
276 
277           LastUpdatedC = UR.UpdatedC;
278           UR.UpdatedRC = nullptr;
279           UR.UpdatedC = nullptr;
280 
281           // Check the PassInstrumentation's BeforePass callbacks before
282           // running the pass, skip its execution completely if asked to
283           // (callback returns false).
284           if (!PI.runBeforePass<LazyCallGraph::SCC>(*Pass, *C))
285             continue;
286 
287           PreservedAnalyses PassPA;
288           {
289             TimeTraceScope TimeScope(Pass->name());
290             PassPA = Pass->run(*C, CGAM, CG, UR);
291           }
292 
293           if (UR.InvalidatedSCCs.count(C))
294             PI.runAfterPassInvalidated<LazyCallGraph::SCC>(*Pass, PassPA);
295           else
296             PI.runAfterPass<LazyCallGraph::SCC>(*Pass, *C, PassPA);
297 
298           // Update the SCC and RefSCC if necessary.
299           C = UR.UpdatedC ? UR.UpdatedC : C;
300           RC = UR.UpdatedRC ? UR.UpdatedRC : RC;
301 
302           if (UR.UpdatedC) {
303             // If we're updating the SCC, also update the FAM inside the proxy's
304             // result.
305             CGAM.getResult<FunctionAnalysisManagerCGSCCProxy>(*C, CG).updateFAM(
306                 FAM);
307           }
308 
309           // If the CGSCC pass wasn't able to provide a valid updated SCC,
310           // the current SCC may simply need to be skipped if invalid.
311           if (UR.InvalidatedSCCs.count(C)) {
312             LLVM_DEBUG(dbgs() << "Skipping invalidated root or island SCC!\n");
313             break;
314           }
315           // Check that we didn't miss any update scenario.
316           assert(C->begin() != C->end() && "Cannot have an empty SCC!");
317 
318           // We handle invalidating the CGSCC analysis manager's information
319           // for the (potentially updated) SCC here. Note that any other SCCs
320           // whose structure has changed should have been invalidated by
321           // whatever was updating the call graph. This SCC gets invalidated
322           // late as it contains the nodes that were actively being
323           // processed.
324           CGAM.invalidate(*C, PassPA);
325 
326           // Then intersect the preserved set so that invalidation of module
327           // analyses will eventually occur when the module pass completes.
328           // Also intersect with the cross-SCC preserved set to capture any
329           // cross-SCC invalidation.
330           UR.CrossSCCPA.intersect(PassPA);
331           PA.intersect(std::move(PassPA));
332 
333           // The pass may have restructured the call graph and refined the
334           // current SCC and/or RefSCC. We need to update our current SCC and
335           // RefSCC pointers to follow these. Also, when the current SCC is
336           // refined, re-run the SCC pass over the newly refined SCC in order
337           // to observe the most precise SCC model available. This inherently
338           // cannot cycle excessively as it only happens when we split SCCs
339           // apart, at most converging on a DAG of single nodes.
340           // FIXME: If we ever start having RefSCC passes, we'll want to
341           // iterate there too.
342           if (UR.UpdatedC)
343             LLVM_DEBUG(dbgs()
344                        << "Re-running SCC passes after a refinement of the "
345                           "current SCC: "
346                        << *UR.UpdatedC << "\n");
347 
348           // Note that both `C` and `RC` may at this point refer to deleted,
349           // invalid SCC and RefSCCs respectively. But we will short circuit
350           // the processing when we check them in the loop above.
351         } while (UR.UpdatedC);
352       } while (!CWorklist.empty());
353 
354       // We only need to keep internal inlined edge information within
355       // a RefSCC, clear it to save on space and let the next time we visit
356       // any of these functions have a fresh start.
357       InlinedInternalEdges.clear();
358     } while (!RCWorklist.empty());
359   }
360 
361   // By definition we preserve the call garph, all SCC analyses, and the
362   // analysis proxies by handling them above and in any nested pass managers.
363   PA.preserveSet<AllAnalysesOn<LazyCallGraph::SCC>>();
364   PA.preserve<LazyCallGraphAnalysis>();
365   PA.preserve<CGSCCAnalysisManagerModuleProxy>();
366   PA.preserve<FunctionAnalysisManagerModuleProxy>();
367   return PA;
368 }
369 
370 PreservedAnalyses DevirtSCCRepeatedPass::run(LazyCallGraph::SCC &InitialC,
371                                              CGSCCAnalysisManager &AM,
372                                              LazyCallGraph &CG,
373                                              CGSCCUpdateResult &UR) {
374   PreservedAnalyses PA = PreservedAnalyses::all();
375   PassInstrumentation PI =
376       AM.getResult<PassInstrumentationAnalysis>(InitialC, CG);
377 
378   // The SCC may be refined while we are running passes over it, so set up
379   // a pointer that we can update.
380   LazyCallGraph::SCC *C = &InitialC;
381 
382   // Struct to track the counts of direct and indirect calls in each function
383   // of the SCC.
384   struct CallCount {
385     int Direct;
386     int Indirect;
387   };
388 
389   // Put value handles on all of the indirect calls and return the number of
390   // direct calls for each function in the SCC.
391   auto ScanSCC = [](LazyCallGraph::SCC &C,
392                     SmallMapVector<Value *, WeakTrackingVH, 16> &CallHandles) {
393     assert(CallHandles.empty() && "Must start with a clear set of handles.");
394 
395     SmallDenseMap<Function *, CallCount> CallCounts;
396     CallCount CountLocal = {0, 0};
397     for (LazyCallGraph::Node &N : C) {
398       CallCount &Count =
399           CallCounts.insert(std::make_pair(&N.getFunction(), CountLocal))
400               .first->second;
401       for (Instruction &I : instructions(N.getFunction()))
402         if (auto *CB = dyn_cast<CallBase>(&I)) {
403           if (CB->getCalledFunction()) {
404             ++Count.Direct;
405           } else {
406             ++Count.Indirect;
407             CallHandles.insert({CB, WeakTrackingVH(CB)});
408           }
409         }
410     }
411 
412     return CallCounts;
413   };
414 
415   UR.IndirectVHs.clear();
416   // Populate the initial call handles and get the initial call counts.
417   auto CallCounts = ScanSCC(*C, UR.IndirectVHs);
418 
419   for (int Iteration = 0;; ++Iteration) {
420     if (!PI.runBeforePass<LazyCallGraph::SCC>(*Pass, *C))
421       continue;
422 
423     PreservedAnalyses PassPA = Pass->run(*C, AM, CG, UR);
424 
425     if (UR.InvalidatedSCCs.count(C))
426       PI.runAfterPassInvalidated<LazyCallGraph::SCC>(*Pass, PassPA);
427     else
428       PI.runAfterPass<LazyCallGraph::SCC>(*Pass, *C, PassPA);
429 
430     // If the SCC structure has changed, bail immediately and let the outer
431     // CGSCC layer handle any iteration to reflect the refined structure.
432     if (UR.UpdatedC && UR.UpdatedC != C) {
433       PA.intersect(std::move(PassPA));
434       break;
435     }
436 
437     // Check that we didn't miss any update scenario.
438     assert(!UR.InvalidatedSCCs.count(C) && "Processing an invalid SCC!");
439     assert(C->begin() != C->end() && "Cannot have an empty SCC!");
440 
441     // Check whether any of the handles were devirtualized.
442     bool Devirt = llvm::any_of(UR.IndirectVHs, [](auto &P) -> bool {
443       if (P.second) {
444         if (CallBase *CB = dyn_cast<CallBase>(P.second)) {
445           if (CB->getCalledFunction()) {
446             LLVM_DEBUG(dbgs() << "Found devirtualized call: " << *CB << "\n");
447             return true;
448           }
449         }
450       }
451       return false;
452     });
453 
454     // Rescan to build up a new set of handles and count how many direct
455     // calls remain. If we decide to iterate, this also sets up the input to
456     // the next iteration.
457     UR.IndirectVHs.clear();
458     auto NewCallCounts = ScanSCC(*C, UR.IndirectVHs);
459 
460     // If we haven't found an explicit devirtualization already see if we
461     // have decreased the number of indirect calls and increased the number
462     // of direct calls for any function in the SCC. This can be fooled by all
463     // manner of transformations such as DCE and other things, but seems to
464     // work well in practice.
465     if (!Devirt)
466       // Iterate over the keys in NewCallCounts, if Function also exists in
467       // CallCounts, make the check below.
468       for (auto &Pair : NewCallCounts) {
469         auto &CallCountNew = Pair.second;
470         auto CountIt = CallCounts.find(Pair.first);
471         if (CountIt != CallCounts.end()) {
472           const auto &CallCountOld = CountIt->second;
473           if (CallCountOld.Indirect > CallCountNew.Indirect &&
474               CallCountOld.Direct < CallCountNew.Direct) {
475             Devirt = true;
476             break;
477           }
478         }
479       }
480 
481     if (!Devirt) {
482       PA.intersect(std::move(PassPA));
483       break;
484     }
485 
486     // Otherwise, if we've already hit our max, we're done.
487     if (Iteration >= MaxIterations) {
488       if (AbortOnMaxDevirtIterationsReached)
489         report_fatal_error("Max devirtualization iterations reached");
490       LLVM_DEBUG(
491           dbgs() << "Found another devirtualization after hitting the max "
492                     "number of repetitions ("
493                  << MaxIterations << ") on SCC: " << *C << "\n");
494       PA.intersect(std::move(PassPA));
495       break;
496     }
497 
498     LLVM_DEBUG(
499         dbgs() << "Repeating an SCC pass after finding a devirtualization in: "
500                << *C << "\n");
501 
502     // Move over the new call counts in preparation for iterating.
503     CallCounts = std::move(NewCallCounts);
504 
505     // Update the analysis manager with each run and intersect the total set
506     // of preserved analyses so we're ready to iterate.
507     AM.invalidate(*C, PassPA);
508 
509     PA.intersect(std::move(PassPA));
510   }
511 
512   // Note that we don't add any preserved entries here unlike a more normal
513   // "pass manager" because we only handle invalidation *between* iterations,
514   // not after the last iteration.
515   return PA;
516 }
517 
518 PreservedAnalyses CGSCCToFunctionPassAdaptor::run(LazyCallGraph::SCC &C,
519                                                   CGSCCAnalysisManager &AM,
520                                                   LazyCallGraph &CG,
521                                                   CGSCCUpdateResult &UR) {
522   // Setup the function analysis manager from its proxy.
523   FunctionAnalysisManager &FAM =
524       AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
525 
526   SmallVector<LazyCallGraph::Node *, 4> Nodes;
527   for (LazyCallGraph::Node &N : C)
528     Nodes.push_back(&N);
529 
530   // The SCC may get split while we are optimizing functions due to deleting
531   // edges. If this happens, the current SCC can shift, so keep track of
532   // a pointer we can overwrite.
533   LazyCallGraph::SCC *CurrentC = &C;
534 
535   LLVM_DEBUG(dbgs() << "Running function passes across an SCC: " << C << "\n");
536 
537   PreservedAnalyses PA = PreservedAnalyses::all();
538   for (LazyCallGraph::Node *N : Nodes) {
539     // Skip nodes from other SCCs. These may have been split out during
540     // processing. We'll eventually visit those SCCs and pick up the nodes
541     // there.
542     if (CG.lookupSCC(*N) != CurrentC)
543       continue;
544 
545     Function &F = N->getFunction();
546     // The expectation here is that FunctionStatusAnalysis was required at the
547     // end of the function passes pipeline managed by this adaptor. Then, if any
548     // CGSCC passes were re-run because CGSCCs changed (or devirtualization),
549     // and none changed F, then FunctionStatusAnalysis would still be cached
550     // here and we don't need to rerun the passes managed by this adaptor.
551     if (FAM.getCachedResult<FunctionStatusAnalysis>(F))
552       continue;
553 
554     PassInstrumentation PI = FAM.getResult<PassInstrumentationAnalysis>(F);
555     if (!PI.runBeforePass<Function>(*Pass, F))
556       continue;
557 
558     PreservedAnalyses PassPA;
559     {
560       TimeTraceScope TimeScope(Pass->name());
561       PassPA = Pass->run(F, FAM);
562     }
563 
564     PI.runAfterPass<Function>(*Pass, F, PassPA);
565 
566     // We know that the function pass couldn't have invalidated any other
567     // function's analyses (that's the contract of a function pass), so
568     // directly handle the function analysis manager's invalidation here.
569     FAM.invalidate(F, PassPA);
570 
571     // Then intersect the preserved set so that invalidation of module
572     // analyses will eventually occur when the module pass completes.
573     PA.intersect(std::move(PassPA));
574 
575     // If the call graph hasn't been preserved, update it based on this
576     // function pass. This may also update the current SCC to point to
577     // a smaller, more refined SCC.
578     auto PAC = PA.getChecker<LazyCallGraphAnalysis>();
579     if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Module>>()) {
580       CurrentC = &updateCGAndAnalysisManagerForFunctionPass(CG, *CurrentC, *N,
581                                                             AM, UR, FAM);
582       assert(CG.lookupSCC(*N) == CurrentC &&
583              "Current SCC not updated to the SCC containing the current node!");
584     }
585   }
586 
587   // By definition we preserve the proxy. And we preserve all analyses on
588   // Functions. This precludes *any* invalidation of function analyses by the
589   // proxy, but that's OK because we've taken care to invalidate analyses in
590   // the function analysis manager incrementally above.
591   PA.preserveSet<AllAnalysesOn<Function>>();
592   PA.preserve<FunctionAnalysisManagerCGSCCProxy>();
593 
594   // We've also ensured that we updated the call graph along the way.
595   PA.preserve<LazyCallGraphAnalysis>();
596 
597   return PA;
598 }
599 
600 bool CGSCCAnalysisManagerModuleProxy::Result::invalidate(
601     Module &M, const PreservedAnalyses &PA,
602     ModuleAnalysisManager::Invalidator &Inv) {
603   // If literally everything is preserved, we're done.
604   if (PA.areAllPreserved())
605     return false; // This is still a valid proxy.
606 
607   // If this proxy or the call graph is going to be invalidated, we also need
608   // to clear all the keys coming from that analysis.
609   //
610   // We also directly invalidate the FAM's module proxy if necessary, and if
611   // that proxy isn't preserved we can't preserve this proxy either. We rely on
612   // it to handle module -> function analysis invalidation in the face of
613   // structural changes and so if it's unavailable we conservatively clear the
614   // entire SCC layer as well rather than trying to do invalidation ourselves.
615   auto PAC = PA.getChecker<CGSCCAnalysisManagerModuleProxy>();
616   if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Module>>()) ||
617       Inv.invalidate<LazyCallGraphAnalysis>(M, PA) ||
618       Inv.invalidate<FunctionAnalysisManagerModuleProxy>(M, PA)) {
619     InnerAM->clear();
620 
621     // And the proxy itself should be marked as invalid so that we can observe
622     // the new call graph. This isn't strictly necessary because we cheat
623     // above, but is still useful.
624     return true;
625   }
626 
627   // Directly check if the relevant set is preserved so we can short circuit
628   // invalidating SCCs below.
629   bool AreSCCAnalysesPreserved =
630       PA.allAnalysesInSetPreserved<AllAnalysesOn<LazyCallGraph::SCC>>();
631 
632   // Ok, we have a graph, so we can propagate the invalidation down into it.
633   G->buildRefSCCs();
634   for (auto &RC : G->postorder_ref_sccs())
635     for (auto &C : RC) {
636       Optional<PreservedAnalyses> InnerPA;
637 
638       // Check to see whether the preserved set needs to be adjusted based on
639       // module-level analysis invalidation triggering deferred invalidation
640       // for this SCC.
641       if (auto *OuterProxy =
642               InnerAM->getCachedResult<ModuleAnalysisManagerCGSCCProxy>(C))
643         for (const auto &OuterInvalidationPair :
644              OuterProxy->getOuterInvalidations()) {
645           AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first;
646           const auto &InnerAnalysisIDs = OuterInvalidationPair.second;
647           if (Inv.invalidate(OuterAnalysisID, M, PA)) {
648             if (!InnerPA)
649               InnerPA = PA;
650             for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs)
651               InnerPA->abandon(InnerAnalysisID);
652           }
653         }
654 
655       // Check if we needed a custom PA set. If so we'll need to run the inner
656       // invalidation.
657       if (InnerPA) {
658         InnerAM->invalidate(C, *InnerPA);
659         continue;
660       }
661 
662       // Otherwise we only need to do invalidation if the original PA set didn't
663       // preserve all SCC analyses.
664       if (!AreSCCAnalysesPreserved)
665         InnerAM->invalidate(C, PA);
666     }
667 
668   // Return false to indicate that this result is still a valid proxy.
669   return false;
670 }
671 
672 template <>
673 CGSCCAnalysisManagerModuleProxy::Result
674 CGSCCAnalysisManagerModuleProxy::run(Module &M, ModuleAnalysisManager &AM) {
675   // Force the Function analysis manager to also be available so that it can
676   // be accessed in an SCC analysis and proxied onward to function passes.
677   // FIXME: It is pretty awkward to just drop the result here and assert that
678   // we can find it again later.
679   (void)AM.getResult<FunctionAnalysisManagerModuleProxy>(M);
680 
681   return Result(*InnerAM, AM.getResult<LazyCallGraphAnalysis>(M));
682 }
683 
684 AnalysisKey FunctionAnalysisManagerCGSCCProxy::Key;
685 
686 FunctionAnalysisManagerCGSCCProxy::Result
687 FunctionAnalysisManagerCGSCCProxy::run(LazyCallGraph::SCC &C,
688                                        CGSCCAnalysisManager &AM,
689                                        LazyCallGraph &CG) {
690   // Note: unconditionally getting checking that the proxy exists may get it at
691   // this point. There are cases when this is being run unnecessarily, but
692   // it is cheap and having the assertion in place is more valuable.
693   auto &MAMProxy = AM.getResult<ModuleAnalysisManagerCGSCCProxy>(C, CG);
694   Module &M = *C.begin()->getFunction().getParent();
695   bool ProxyExists =
696       MAMProxy.cachedResultExists<FunctionAnalysisManagerModuleProxy>(M);
697   assert(ProxyExists &&
698          "The CGSCC pass manager requires that the FAM module proxy is run "
699          "on the module prior to entering the CGSCC walk");
700   (void)ProxyExists;
701 
702   // We just return an empty result. The caller will use the updateFAM interface
703   // to correctly register the relevant FunctionAnalysisManager based on the
704   // context in which this proxy is run.
705   return Result();
706 }
707 
708 bool FunctionAnalysisManagerCGSCCProxy::Result::invalidate(
709     LazyCallGraph::SCC &C, const PreservedAnalyses &PA,
710     CGSCCAnalysisManager::Invalidator &Inv) {
711   // If literally everything is preserved, we're done.
712   if (PA.areAllPreserved())
713     return false; // This is still a valid proxy.
714 
715   // All updates to preserve valid results are done below, so we don't need to
716   // invalidate this proxy.
717   //
718   // Note that in order to preserve this proxy, a module pass must ensure that
719   // the FAM has been completely updated to handle the deletion of functions.
720   // Specifically, any FAM-cached results for those functions need to have been
721   // forcibly cleared. When preserved, this proxy will only invalidate results
722   // cached on functions *still in the module* at the end of the module pass.
723   auto PAC = PA.getChecker<FunctionAnalysisManagerCGSCCProxy>();
724   if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<LazyCallGraph::SCC>>()) {
725     for (LazyCallGraph::Node &N : C)
726       FAM->invalidate(N.getFunction(), PA);
727 
728     return false;
729   }
730 
731   // Directly check if the relevant set is preserved.
732   bool AreFunctionAnalysesPreserved =
733       PA.allAnalysesInSetPreserved<AllAnalysesOn<Function>>();
734 
735   // Now walk all the functions to see if any inner analysis invalidation is
736   // necessary.
737   for (LazyCallGraph::Node &N : C) {
738     Function &F = N.getFunction();
739     Optional<PreservedAnalyses> FunctionPA;
740 
741     // Check to see whether the preserved set needs to be pruned based on
742     // SCC-level analysis invalidation that triggers deferred invalidation
743     // registered with the outer analysis manager proxy for this function.
744     if (auto *OuterProxy =
745             FAM->getCachedResult<CGSCCAnalysisManagerFunctionProxy>(F))
746       for (const auto &OuterInvalidationPair :
747            OuterProxy->getOuterInvalidations()) {
748         AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first;
749         const auto &InnerAnalysisIDs = OuterInvalidationPair.second;
750         if (Inv.invalidate(OuterAnalysisID, C, PA)) {
751           if (!FunctionPA)
752             FunctionPA = PA;
753           for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs)
754             FunctionPA->abandon(InnerAnalysisID);
755         }
756       }
757 
758     // Check if we needed a custom PA set, and if so we'll need to run the
759     // inner invalidation.
760     if (FunctionPA) {
761       FAM->invalidate(F, *FunctionPA);
762       continue;
763     }
764 
765     // Otherwise we only need to do invalidation if the original PA set didn't
766     // preserve all function analyses.
767     if (!AreFunctionAnalysesPreserved)
768       FAM->invalidate(F, PA);
769   }
770 
771   // Return false to indicate that this result is still a valid proxy.
772   return false;
773 }
774 
775 } // end namespace llvm
776 
777 /// When a new SCC is created for the graph we first update the
778 /// FunctionAnalysisManager in the Proxy's result.
779 /// As there might be function analysis results cached for the functions now in
780 /// that SCC, two forms of  updates are required.
781 ///
782 /// First, a proxy from the SCC to the FunctionAnalysisManager needs to be
783 /// created so that any subsequent invalidation events to the SCC are
784 /// propagated to the function analysis results cached for functions within it.
785 ///
786 /// Second, if any of the functions within the SCC have analysis results with
787 /// outer analysis dependencies, then those dependencies would point to the
788 /// *wrong* SCC's analysis result. We forcibly invalidate the necessary
789 /// function analyses so that they don't retain stale handles.
790 static void updateNewSCCFunctionAnalyses(LazyCallGraph::SCC &C,
791                                          LazyCallGraph &G,
792                                          CGSCCAnalysisManager &AM,
793                                          FunctionAnalysisManager &FAM) {
794   AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, G).updateFAM(FAM);
795 
796   // Now walk the functions in this SCC and invalidate any function analysis
797   // results that might have outer dependencies on an SCC analysis.
798   for (LazyCallGraph::Node &N : C) {
799     Function &F = N.getFunction();
800 
801     auto *OuterProxy =
802         FAM.getCachedResult<CGSCCAnalysisManagerFunctionProxy>(F);
803     if (!OuterProxy)
804       // No outer analyses were queried, nothing to do.
805       continue;
806 
807     // Forcibly abandon all the inner analyses with dependencies, but
808     // invalidate nothing else.
809     auto PA = PreservedAnalyses::all();
810     for (const auto &OuterInvalidationPair :
811          OuterProxy->getOuterInvalidations()) {
812       const auto &InnerAnalysisIDs = OuterInvalidationPair.second;
813       for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs)
814         PA.abandon(InnerAnalysisID);
815     }
816 
817     // Now invalidate anything we found.
818     FAM.invalidate(F, PA);
819   }
820 }
821 
822 /// Helper function to update both the \c CGSCCAnalysisManager \p AM and the \c
823 /// CGSCCPassManager's \c CGSCCUpdateResult \p UR based on a range of newly
824 /// added SCCs.
825 ///
826 /// The range of new SCCs must be in postorder already. The SCC they were split
827 /// out of must be provided as \p C. The current node being mutated and
828 /// triggering updates must be passed as \p N.
829 ///
830 /// This function returns the SCC containing \p N. This will be either \p C if
831 /// no new SCCs have been split out, or it will be the new SCC containing \p N.
832 template <typename SCCRangeT>
833 static LazyCallGraph::SCC *
834 incorporateNewSCCRange(const SCCRangeT &NewSCCRange, LazyCallGraph &G,
835                        LazyCallGraph::Node &N, LazyCallGraph::SCC *C,
836                        CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR) {
837   using SCC = LazyCallGraph::SCC;
838 
839   if (NewSCCRange.empty())
840     return C;
841 
842   // Add the current SCC to the worklist as its shape has changed.
843   UR.CWorklist.insert(C);
844   LLVM_DEBUG(dbgs() << "Enqueuing the existing SCC in the worklist:" << *C
845                     << "\n");
846 
847   SCC *OldC = C;
848 
849   // Update the current SCC. Note that if we have new SCCs, this must actually
850   // change the SCC.
851   assert(C != &*NewSCCRange.begin() &&
852          "Cannot insert new SCCs without changing current SCC!");
853   C = &*NewSCCRange.begin();
854   assert(G.lookupSCC(N) == C && "Failed to update current SCC!");
855 
856   // If we had a cached FAM proxy originally, we will want to create more of
857   // them for each SCC that was split off.
858   FunctionAnalysisManager *FAM = nullptr;
859   if (auto *FAMProxy =
860           AM.getCachedResult<FunctionAnalysisManagerCGSCCProxy>(*OldC))
861     FAM = &FAMProxy->getManager();
862 
863   // We need to propagate an invalidation call to all but the newly current SCC
864   // because the outer pass manager won't do that for us after splitting them.
865   // FIXME: We should accept a PreservedAnalysis from the CG updater so that if
866   // there are preserved analysis we can avoid invalidating them here for
867   // split-off SCCs.
868   // We know however that this will preserve any FAM proxy so go ahead and mark
869   // that.
870   auto PA = PreservedAnalyses::allInSet<AllAnalysesOn<Function>>();
871   PA.preserve<FunctionAnalysisManagerCGSCCProxy>();
872   AM.invalidate(*OldC, PA);
873 
874   // Ensure the now-current SCC's function analyses are updated.
875   if (FAM)
876     updateNewSCCFunctionAnalyses(*C, G, AM, *FAM);
877 
878   for (SCC &NewC : llvm::reverse(llvm::drop_begin(NewSCCRange))) {
879     assert(C != &NewC && "No need to re-visit the current SCC!");
880     assert(OldC != &NewC && "Already handled the original SCC!");
881     UR.CWorklist.insert(&NewC);
882     LLVM_DEBUG(dbgs() << "Enqueuing a newly formed SCC:" << NewC << "\n");
883 
884     // Ensure new SCCs' function analyses are updated.
885     if (FAM)
886       updateNewSCCFunctionAnalyses(NewC, G, AM, *FAM);
887 
888     // Also propagate a normal invalidation to the new SCC as only the current
889     // will get one from the pass manager infrastructure.
890     AM.invalidate(NewC, PA);
891   }
892   return C;
893 }
894 
895 static LazyCallGraph::SCC &updateCGAndAnalysisManagerForPass(
896     LazyCallGraph &G, LazyCallGraph::SCC &InitialC, LazyCallGraph::Node &N,
897     CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR,
898     FunctionAnalysisManager &FAM, bool FunctionPass) {
899   using Node = LazyCallGraph::Node;
900   using Edge = LazyCallGraph::Edge;
901   using SCC = LazyCallGraph::SCC;
902   using RefSCC = LazyCallGraph::RefSCC;
903 
904   RefSCC &InitialRC = InitialC.getOuterRefSCC();
905   SCC *C = &InitialC;
906   RefSCC *RC = &InitialRC;
907   Function &F = N.getFunction();
908 
909   // Walk the function body and build up the set of retained, promoted, and
910   // demoted edges.
911   SmallVector<Constant *, 16> Worklist;
912   SmallPtrSet<Constant *, 16> Visited;
913   SmallPtrSet<Node *, 16> RetainedEdges;
914   SmallSetVector<Node *, 4> PromotedRefTargets;
915   SmallSetVector<Node *, 4> DemotedCallTargets;
916   SmallSetVector<Node *, 4> NewCallEdges;
917   SmallSetVector<Node *, 4> NewRefEdges;
918 
919   // First walk the function and handle all called functions. We do this first
920   // because if there is a single call edge, whether there are ref edges is
921   // irrelevant.
922   for (Instruction &I : instructions(F)) {
923     if (auto *CB = dyn_cast<CallBase>(&I)) {
924       if (Function *Callee = CB->getCalledFunction()) {
925         if (Visited.insert(Callee).second && !Callee->isDeclaration()) {
926           Node *CalleeN = G.lookup(*Callee);
927           assert(CalleeN &&
928                  "Visited function should already have an associated node");
929           Edge *E = N->lookup(*CalleeN);
930           assert((E || !FunctionPass) &&
931                  "No function transformations should introduce *new* "
932                  "call edges! Any new calls should be modeled as "
933                  "promoted existing ref edges!");
934           bool Inserted = RetainedEdges.insert(CalleeN).second;
935           (void)Inserted;
936           assert(Inserted && "We should never visit a function twice.");
937           if (!E)
938             NewCallEdges.insert(CalleeN);
939           else if (!E->isCall())
940             PromotedRefTargets.insert(CalleeN);
941         }
942       } else {
943         // We can miss devirtualization if an indirect call is created then
944         // promoted before updateCGAndAnalysisManagerForPass runs.
945         auto *Entry = UR.IndirectVHs.find(CB);
946         if (Entry == UR.IndirectVHs.end())
947           UR.IndirectVHs.insert({CB, WeakTrackingVH(CB)});
948         else if (!Entry->second)
949           Entry->second = WeakTrackingVH(CB);
950       }
951     }
952   }
953 
954   // Now walk all references.
955   for (Instruction &I : instructions(F))
956     for (Value *Op : I.operand_values())
957       if (auto *OpC = dyn_cast<Constant>(Op))
958         if (Visited.insert(OpC).second)
959           Worklist.push_back(OpC);
960 
961   auto VisitRef = [&](Function &Referee) {
962     Node *RefereeN = G.lookup(Referee);
963     assert(RefereeN &&
964            "Visited function should already have an associated node");
965     Edge *E = N->lookup(*RefereeN);
966     assert((E || !FunctionPass) &&
967            "No function transformations should introduce *new* ref "
968            "edges! Any new ref edges would require IPO which "
969            "function passes aren't allowed to do!");
970     bool Inserted = RetainedEdges.insert(RefereeN).second;
971     (void)Inserted;
972     assert(Inserted && "We should never visit a function twice.");
973     if (!E)
974       NewRefEdges.insert(RefereeN);
975     else if (E->isCall())
976       DemotedCallTargets.insert(RefereeN);
977   };
978   LazyCallGraph::visitReferences(Worklist, Visited, VisitRef);
979 
980   // Handle new ref edges.
981   for (Node *RefTarget : NewRefEdges) {
982     SCC &TargetC = *G.lookupSCC(*RefTarget);
983     RefSCC &TargetRC = TargetC.getOuterRefSCC();
984     (void)TargetRC;
985     // TODO: This only allows trivial edges to be added for now.
986 #ifdef EXPENSIVE_CHECKS
987     assert((RC == &TargetRC ||
988            RC->isAncestorOf(TargetRC)) && "New ref edge is not trivial!");
989 #endif
990     RC->insertTrivialRefEdge(N, *RefTarget);
991   }
992 
993   // Handle new call edges.
994   for (Node *CallTarget : NewCallEdges) {
995     SCC &TargetC = *G.lookupSCC(*CallTarget);
996     RefSCC &TargetRC = TargetC.getOuterRefSCC();
997     (void)TargetRC;
998     // TODO: This only allows trivial edges to be added for now.
999 #ifdef EXPENSIVE_CHECKS
1000     assert((RC == &TargetRC ||
1001            RC->isAncestorOf(TargetRC)) && "New call edge is not trivial!");
1002 #endif
1003     // Add a trivial ref edge to be promoted later on alongside
1004     // PromotedRefTargets.
1005     RC->insertTrivialRefEdge(N, *CallTarget);
1006   }
1007 
1008   // Include synthetic reference edges to known, defined lib functions.
1009   for (auto *LibFn : G.getLibFunctions())
1010     // While the list of lib functions doesn't have repeats, don't re-visit
1011     // anything handled above.
1012     if (!Visited.count(LibFn))
1013       VisitRef(*LibFn);
1014 
1015   // First remove all of the edges that are no longer present in this function.
1016   // The first step makes these edges uniformly ref edges and accumulates them
1017   // into a separate data structure so removal doesn't invalidate anything.
1018   SmallVector<Node *, 4> DeadTargets;
1019   for (Edge &E : *N) {
1020     if (RetainedEdges.count(&E.getNode()))
1021       continue;
1022 
1023     SCC &TargetC = *G.lookupSCC(E.getNode());
1024     RefSCC &TargetRC = TargetC.getOuterRefSCC();
1025     if (&TargetRC == RC && E.isCall()) {
1026       if (C != &TargetC) {
1027         // For separate SCCs this is trivial.
1028         RC->switchTrivialInternalEdgeToRef(N, E.getNode());
1029       } else {
1030         // Now update the call graph.
1031         C = incorporateNewSCCRange(RC->switchInternalEdgeToRef(N, E.getNode()),
1032                                    G, N, C, AM, UR);
1033       }
1034     }
1035 
1036     // Now that this is ready for actual removal, put it into our list.
1037     DeadTargets.push_back(&E.getNode());
1038   }
1039   // Remove the easy cases quickly and actually pull them out of our list.
1040   llvm::erase_if(DeadTargets, [&](Node *TargetN) {
1041     SCC &TargetC = *G.lookupSCC(*TargetN);
1042     RefSCC &TargetRC = TargetC.getOuterRefSCC();
1043 
1044     // We can't trivially remove internal targets, so skip
1045     // those.
1046     if (&TargetRC == RC)
1047       return false;
1048 
1049     LLVM_DEBUG(dbgs() << "Deleting outgoing edge from '" << N << "' to '"
1050                       << *TargetN << "'\n");
1051     RC->removeOutgoingEdge(N, *TargetN);
1052     return true;
1053   });
1054 
1055   // Now do a batch removal of the internal ref edges left.
1056   auto NewRefSCCs = RC->removeInternalRefEdge(N, DeadTargets);
1057   if (!NewRefSCCs.empty()) {
1058     // The old RefSCC is dead, mark it as such.
1059     UR.InvalidatedRefSCCs.insert(RC);
1060 
1061     // Note that we don't bother to invalidate analyses as ref-edge
1062     // connectivity is not really observable in any way and is intended
1063     // exclusively to be used for ordering of transforms rather than for
1064     // analysis conclusions.
1065 
1066     // Update RC to the "bottom".
1067     assert(G.lookupSCC(N) == C && "Changed the SCC when splitting RefSCCs!");
1068     RC = &C->getOuterRefSCC();
1069     assert(G.lookupRefSCC(N) == RC && "Failed to update current RefSCC!");
1070 
1071     // The RC worklist is in reverse postorder, so we enqueue the new ones in
1072     // RPO except for the one which contains the source node as that is the
1073     // "bottom" we will continue processing in the bottom-up walk.
1074     assert(NewRefSCCs.front() == RC &&
1075            "New current RefSCC not first in the returned list!");
1076     for (RefSCC *NewRC : llvm::reverse(llvm::drop_begin(NewRefSCCs))) {
1077       assert(NewRC != RC && "Should not encounter the current RefSCC further "
1078                             "in the postorder list of new RefSCCs.");
1079       UR.RCWorklist.insert(NewRC);
1080       LLVM_DEBUG(dbgs() << "Enqueuing a new RefSCC in the update worklist: "
1081                         << *NewRC << "\n");
1082     }
1083   }
1084 
1085   // Next demote all the call edges that are now ref edges. This helps make
1086   // the SCCs small which should minimize the work below as we don't want to
1087   // form cycles that this would break.
1088   for (Node *RefTarget : DemotedCallTargets) {
1089     SCC &TargetC = *G.lookupSCC(*RefTarget);
1090     RefSCC &TargetRC = TargetC.getOuterRefSCC();
1091 
1092     // The easy case is when the target RefSCC is not this RefSCC. This is
1093     // only supported when the target RefSCC is a child of this RefSCC.
1094     if (&TargetRC != RC) {
1095 #ifdef EXPENSIVE_CHECKS
1096       assert(RC->isAncestorOf(TargetRC) &&
1097              "Cannot potentially form RefSCC cycles here!");
1098 #endif
1099       RC->switchOutgoingEdgeToRef(N, *RefTarget);
1100       LLVM_DEBUG(dbgs() << "Switch outgoing call edge to a ref edge from '" << N
1101                         << "' to '" << *RefTarget << "'\n");
1102       continue;
1103     }
1104 
1105     // We are switching an internal call edge to a ref edge. This may split up
1106     // some SCCs.
1107     if (C != &TargetC) {
1108       // For separate SCCs this is trivial.
1109       RC->switchTrivialInternalEdgeToRef(N, *RefTarget);
1110       continue;
1111     }
1112 
1113     // Now update the call graph.
1114     C = incorporateNewSCCRange(RC->switchInternalEdgeToRef(N, *RefTarget), G, N,
1115                                C, AM, UR);
1116   }
1117 
1118   // We added a ref edge earlier for new call edges, promote those to call edges
1119   // alongside PromotedRefTargets.
1120   for (Node *E : NewCallEdges)
1121     PromotedRefTargets.insert(E);
1122 
1123   // Now promote ref edges into call edges.
1124   for (Node *CallTarget : PromotedRefTargets) {
1125     SCC &TargetC = *G.lookupSCC(*CallTarget);
1126     RefSCC &TargetRC = TargetC.getOuterRefSCC();
1127 
1128     // The easy case is when the target RefSCC is not this RefSCC. This is
1129     // only supported when the target RefSCC is a child of this RefSCC.
1130     if (&TargetRC != RC) {
1131 #ifdef EXPENSIVE_CHECKS
1132       assert(RC->isAncestorOf(TargetRC) &&
1133              "Cannot potentially form RefSCC cycles here!");
1134 #endif
1135       RC->switchOutgoingEdgeToCall(N, *CallTarget);
1136       LLVM_DEBUG(dbgs() << "Switch outgoing ref edge to a call edge from '" << N
1137                         << "' to '" << *CallTarget << "'\n");
1138       continue;
1139     }
1140     LLVM_DEBUG(dbgs() << "Switch an internal ref edge to a call edge from '"
1141                       << N << "' to '" << *CallTarget << "'\n");
1142 
1143     // Otherwise we are switching an internal ref edge to a call edge. This
1144     // may merge away some SCCs, and we add those to the UpdateResult. We also
1145     // need to make sure to update the worklist in the event SCCs have moved
1146     // before the current one in the post-order sequence
1147     bool HasFunctionAnalysisProxy = false;
1148     auto InitialSCCIndex = RC->find(*C) - RC->begin();
1149     bool FormedCycle = RC->switchInternalEdgeToCall(
1150         N, *CallTarget, [&](ArrayRef<SCC *> MergedSCCs) {
1151           for (SCC *MergedC : MergedSCCs) {
1152             assert(MergedC != &TargetC && "Cannot merge away the target SCC!");
1153 
1154             HasFunctionAnalysisProxy |=
1155                 AM.getCachedResult<FunctionAnalysisManagerCGSCCProxy>(
1156                     *MergedC) != nullptr;
1157 
1158             // Mark that this SCC will no longer be valid.
1159             UR.InvalidatedSCCs.insert(MergedC);
1160 
1161             // FIXME: We should really do a 'clear' here to forcibly release
1162             // memory, but we don't have a good way of doing that and
1163             // preserving the function analyses.
1164             auto PA = PreservedAnalyses::allInSet<AllAnalysesOn<Function>>();
1165             PA.preserve<FunctionAnalysisManagerCGSCCProxy>();
1166             AM.invalidate(*MergedC, PA);
1167           }
1168         });
1169 
1170     // If we formed a cycle by creating this call, we need to update more data
1171     // structures.
1172     if (FormedCycle) {
1173       C = &TargetC;
1174       assert(G.lookupSCC(N) == C && "Failed to update current SCC!");
1175 
1176       // If one of the invalidated SCCs had a cached proxy to a function
1177       // analysis manager, we need to create a proxy in the new current SCC as
1178       // the invalidated SCCs had their functions moved.
1179       if (HasFunctionAnalysisProxy)
1180         AM.getResult<FunctionAnalysisManagerCGSCCProxy>(*C, G).updateFAM(FAM);
1181 
1182       // Any analyses cached for this SCC are no longer precise as the shape
1183       // has changed by introducing this cycle. However, we have taken care to
1184       // update the proxies so it remains valide.
1185       auto PA = PreservedAnalyses::allInSet<AllAnalysesOn<Function>>();
1186       PA.preserve<FunctionAnalysisManagerCGSCCProxy>();
1187       AM.invalidate(*C, PA);
1188     }
1189     auto NewSCCIndex = RC->find(*C) - RC->begin();
1190     // If we have actually moved an SCC to be topologically "below" the current
1191     // one due to merging, we will need to revisit the current SCC after
1192     // visiting those moved SCCs.
1193     //
1194     // It is critical that we *do not* revisit the current SCC unless we
1195     // actually move SCCs in the process of merging because otherwise we may
1196     // form a cycle where an SCC is split apart, merged, split, merged and so
1197     // on infinitely.
1198     if (InitialSCCIndex < NewSCCIndex) {
1199       // Put our current SCC back onto the worklist as we'll visit other SCCs
1200       // that are now definitively ordered prior to the current one in the
1201       // post-order sequence, and may end up observing more precise context to
1202       // optimize the current SCC.
1203       UR.CWorklist.insert(C);
1204       LLVM_DEBUG(dbgs() << "Enqueuing the existing SCC in the worklist: " << *C
1205                         << "\n");
1206       // Enqueue in reverse order as we pop off the back of the worklist.
1207       for (SCC &MovedC : llvm::reverse(make_range(RC->begin() + InitialSCCIndex,
1208                                                   RC->begin() + NewSCCIndex))) {
1209         UR.CWorklist.insert(&MovedC);
1210         LLVM_DEBUG(dbgs() << "Enqueuing a newly earlier in post-order SCC: "
1211                           << MovedC << "\n");
1212       }
1213     }
1214   }
1215 
1216   assert(!UR.InvalidatedSCCs.count(C) && "Invalidated the current SCC!");
1217   assert(!UR.InvalidatedRefSCCs.count(RC) && "Invalidated the current RefSCC!");
1218   assert(&C->getOuterRefSCC() == RC && "Current SCC not in current RefSCC!");
1219 
1220   // Record the current RefSCC and SCC for higher layers of the CGSCC pass
1221   // manager now that all the updates have been applied.
1222   if (RC != &InitialRC)
1223     UR.UpdatedRC = RC;
1224   if (C != &InitialC)
1225     UR.UpdatedC = C;
1226 
1227   return *C;
1228 }
1229 
1230 LazyCallGraph::SCC &llvm::updateCGAndAnalysisManagerForFunctionPass(
1231     LazyCallGraph &G, LazyCallGraph::SCC &InitialC, LazyCallGraph::Node &N,
1232     CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR,
1233     FunctionAnalysisManager &FAM) {
1234   return updateCGAndAnalysisManagerForPass(G, InitialC, N, AM, UR, FAM,
1235                                            /* FunctionPass */ true);
1236 }
1237 LazyCallGraph::SCC &llvm::updateCGAndAnalysisManagerForCGSCCPass(
1238     LazyCallGraph &G, LazyCallGraph::SCC &InitialC, LazyCallGraph::Node &N,
1239     CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR,
1240     FunctionAnalysisManager &FAM) {
1241   return updateCGAndAnalysisManagerForPass(G, InitialC, N, AM, UR, FAM,
1242                                            /* FunctionPass */ false);
1243 }
1244