Bug Summary

File:llvm/lib/Analysis/CGSCCPassManager.cpp
Warning:line 229, column 11
Forming reference to null pointer

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name CGSCCPassManager.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/build-llvm -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I lib/Analysis -I /build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/llvm/lib/Analysis -I include -I /build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/llvm/include -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-command-line-argument -Wno-unknown-warning-option -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/build-llvm -ferror-limit 19 -fvisibility-inlines-hidden -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2021-10-17-004846-21170-1 -x c++ /build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/llvm/lib/Analysis/CGSCCPassManager.cpp

/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/llvm/lib/Analysis/CGSCCPassManager.cpp

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

/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/llvm/include/llvm/ADT/PriorityWorklist.h

1//===- PriorityWorklist.h - Worklist with insertion priority ----*- C++ -*-===//
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/// \file
10///
11/// This file provides a priority worklist. See the class comments for details.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_ADT_PRIORITYWORKLIST_H
16#define LLVM_ADT_PRIORITYWORKLIST_H
17
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/Support/Compiler.h"
22#include <algorithm>
23#include <cassert>
24#include <cstddef>
25#include <iterator>
26#include <type_traits>
27#include <vector>
28
29namespace llvm {
30
31/// A FILO worklist that prioritizes on re-insertion without duplication.
32///
33/// This is very similar to a \c SetVector with the primary difference that
34/// while re-insertion does not create a duplicate, it does adjust the
35/// visitation order to respect the last insertion point. This can be useful
36/// when the visit order needs to be prioritized based on insertion point
37/// without actually having duplicate visits.
38///
39/// Note that this doesn't prevent re-insertion of elements which have been
40/// visited -- if you need to break cycles, a set will still be necessary.
41///
42/// The type \c T must be default constructable to a null value that will be
43/// ignored. It is an error to insert such a value, and popping elements will
44/// never produce such a value. It is expected to be used with common nullable
45/// types like pointers or optionals.
46///
47/// Internally this uses a vector to store the worklist and a map to identify
48/// existing elements in the worklist. Both of these may be customized, but the
49/// map must support the basic DenseMap API for mapping from a T to an integer
50/// index into the vector.
51///
52/// A partial specialization is provided to automatically select a SmallVector
53/// and a SmallDenseMap if custom data structures are not provided.
54template <typename T, typename VectorT = std::vector<T>,
55 typename MapT = DenseMap<T, ptrdiff_t>>
56class PriorityWorklist {
57public:
58 using value_type = T;
59 using key_type = T;
60 using reference = T&;
61 using const_reference = const T&;
62 using size_type = typename MapT::size_type;
63
64 /// Construct an empty PriorityWorklist
65 PriorityWorklist() = default;
66
67 /// Determine if the PriorityWorklist is empty or not.
68 bool empty() const {
69 return V.empty();
70 }
71
72 /// Returns the number of elements in the worklist.
73 size_type size() const {
74 return M.size();
75 }
76
77 /// Count the number of elements of a given key in the PriorityWorklist.
78 /// \returns 0 if the element is not in the PriorityWorklist, 1 if it is.
79 size_type count(const key_type &key) const {
80 return M.count(key);
81 }
82
83 /// Return the last element of the PriorityWorklist.
84 const T &back() const {
85 assert(!empty() && "Cannot call back() on empty PriorityWorklist!")(static_cast <bool> (!empty() && "Cannot call back() on empty PriorityWorklist!"
) ? void (0) : __assert_fail ("!empty() && \"Cannot call back() on empty PriorityWorklist!\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/llvm/include/llvm/ADT/PriorityWorklist.h"
, 85, __extension__ __PRETTY_FUNCTION__))
;
10
'?' condition is true
86 return V.back();
11
Returning pointer
87 }
88
89 /// Insert a new element into the PriorityWorklist.
90 /// \returns true if the element was inserted into the PriorityWorklist.
91 bool insert(const T &X) {
92 assert(X != T() && "Cannot insert a null (default constructed) value!")(static_cast <bool> (X != T() && "Cannot insert a null (default constructed) value!"
) ? void (0) : __assert_fail ("X != T() && \"Cannot insert a null (default constructed) value!\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/llvm/include/llvm/ADT/PriorityWorklist.h"
, 92, __extension__ __PRETTY_FUNCTION__))
;
93 auto InsertResult = M.insert({X, V.size()});
94 if (InsertResult.second) {
95 // Fresh value, just append it to the vector.
96 V.push_back(X);
97 return true;
98 }
99
100 auto &Index = InsertResult.first->second;
101 assert(V[Index] == X && "Value not actually at index in map!")(static_cast <bool> (V[Index] == X && "Value not actually at index in map!"
) ? void (0) : __assert_fail ("V[Index] == X && \"Value not actually at index in map!\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/llvm/include/llvm/ADT/PriorityWorklist.h"
, 101, __extension__ __PRETTY_FUNCTION__))
;
102 if (Index != (ptrdiff_t)(V.size() - 1)) {
103 // If the element isn't at the back, null it out and append a fresh one.
104 V[Index] = T();
105 Index = (ptrdiff_t)V.size();
106 V.push_back(X);
107 }
108 return false;
109 }
110
111 /// Insert a sequence of new elements into the PriorityWorklist.
112 template <typename SequenceT>
113 std::enable_if_t<!std::is_convertible<SequenceT, T>::value>
114 insert(SequenceT &&Input) {
115 if (std::begin(Input) == std::end(Input))
116 // Nothing to do for an empty input sequence.
117 return;
118
119 // First pull the input sequence into the vector as a bulk append
120 // operation.
121 ptrdiff_t StartIndex = V.size();
122 V.insert(V.end(), std::begin(Input), std::end(Input));
123 // Now walk backwards fixing up the index map and deleting any duplicates.
124 for (ptrdiff_t i = V.size() - 1; i >= StartIndex; --i) {
125 auto InsertResult = M.insert({V[i], i});
126 if (InsertResult.second)
127 continue;
128
129 // If the existing index is before this insert's start, nuke that one and
130 // move it up.
131 ptrdiff_t &Index = InsertResult.first->second;
132 if (Index < StartIndex) {
133 V[Index] = T();
134 Index = i;
135 continue;
136 }
137
138 // Otherwise the existing one comes first so just clear out the value in
139 // this slot.
140 V[i] = T();
141 }
142 }
143
144 /// Remove the last element of the PriorityWorklist.
145 void pop_back() {
146 assert(!empty() && "Cannot remove an element when empty!")(static_cast <bool> (!empty() && "Cannot remove an element when empty!"
) ? void (0) : __assert_fail ("!empty() && \"Cannot remove an element when empty!\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/llvm/include/llvm/ADT/PriorityWorklist.h"
, 146, __extension__ __PRETTY_FUNCTION__))
;
147 assert(back() != T() && "Cannot have a null element at the back!")(static_cast <bool> (back() != T() && "Cannot have a null element at the back!"
) ? void (0) : __assert_fail ("back() != T() && \"Cannot have a null element at the back!\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/llvm/include/llvm/ADT/PriorityWorklist.h"
, 147, __extension__ __PRETTY_FUNCTION__))
;
148 M.erase(back());
149 do {
150 V.pop_back();
151 } while (!V.empty() && V.back() == T());
152 }
153
154 LLVM_NODISCARD[[clang::warn_unused_result]] T pop_back_val() {
155 T Ret = back();
9
Calling 'PriorityWorklist::back'
12
Returning from 'PriorityWorklist::back'
13
'Ret' initialized here
156 pop_back();
157 return Ret;
14
Returning pointer (loaded from 'Ret')
158 }
159
160 /// Erase an item from the worklist.
161 ///
162 /// Note that this is constant time due to the nature of the worklist implementation.
163 bool erase(const T& X) {
164 auto I = M.find(X);
165 if (I == M.end())
166 return false;
167
168 assert(V[I->second] == X && "Value not actually at index in map!")(static_cast <bool> (V[I->second] == X && "Value not actually at index in map!"
) ? void (0) : __assert_fail ("V[I->second] == X && \"Value not actually at index in map!\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/llvm/include/llvm/ADT/PriorityWorklist.h"
, 168, __extension__ __PRETTY_FUNCTION__))
;
169 if (I->second == (ptrdiff_t)(V.size() - 1)) {
170 do {
171 V.pop_back();
172 } while (!V.empty() && V.back() == T());
173 } else {
174 V[I->second] = T();
175 }
176 M.erase(I);
177 return true;
178 }
179
180 /// Erase items from the set vector based on a predicate function.
181 ///
182 /// This is intended to be equivalent to the following code, if we could
183 /// write it:
184 ///
185 /// \code
186 /// V.erase(remove_if(V, P), V.end());
187 /// \endcode
188 ///
189 /// However, PriorityWorklist doesn't expose non-const iterators, making any
190 /// algorithm like remove_if impossible to use.
191 ///
192 /// \returns true if any element is removed.
193 template <typename UnaryPredicate>
194 bool erase_if(UnaryPredicate P) {
195 typename VectorT::iterator E =
196 remove_if(V, TestAndEraseFromMap<UnaryPredicate>(P, M));
197 if (E == V.end())
198 return false;
199 for (auto I = V.begin(); I != E; ++I)
200 if (*I != T())
201 M[*I] = I - V.begin();
202 V.erase(E, V.end());
203 return true;
204 }
205
206 /// Reverse the items in the PriorityWorklist.
207 ///
208 /// This does an in-place reversal. Other kinds of reverse aren't easy to
209 /// support in the face of the worklist semantics.
210
211 /// Completely clear the PriorityWorklist
212 void clear() {
213 M.clear();
214 V.clear();
215 }
216
217private:
218 /// A wrapper predicate designed for use with std::remove_if.
219 ///
220 /// This predicate wraps a predicate suitable for use with std::remove_if to
221 /// call M.erase(x) on each element which is slated for removal. This just
222 /// allows the predicate to be move only which we can't do with lambdas
223 /// today.
224 template <typename UnaryPredicateT>
225 class TestAndEraseFromMap {
226 UnaryPredicateT P;
227 MapT &M;
228
229 public:
230 TestAndEraseFromMap(UnaryPredicateT P, MapT &M)
231 : P(std::move(P)), M(M) {}
232
233 bool operator()(const T &Arg) {
234 if (Arg == T())
235 // Skip null values in the PriorityWorklist.
236 return false;
237
238 if (P(Arg)) {
239 M.erase(Arg);
240 return true;
241 }
242 return false;
243 }
244 };
245
246 /// The map from value to index in the vector.
247 MapT M;
248
249 /// The vector of elements in insertion order.
250 VectorT V;
251};
252
253/// A version of \c PriorityWorklist that selects small size optimized data
254/// structures for the vector and map.
255template <typename T, unsigned N>
256class SmallPriorityWorklist
257 : public PriorityWorklist<T, SmallVector<T, N>,
258 SmallDenseMap<T, ptrdiff_t>> {
259public:
260 SmallPriorityWorklist() = default;
261};
262
263} // end namespace llvm
264
265#endif // LLVM_ADT_PRIORITYWORKLIST_H