Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 1 | //===- CGSCCPassManager.cpp - Managing & running CGSCC passes -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/Analysis/CGSCCPassManager.h" |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 11 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 8977223 | 2016-12-06 10:06:06 +0000 | [diff] [blame] | 12 | #include "llvm/IR/InstIterator.h" |
Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 13 | |
| 14 | using namespace llvm; |
| 15 | |
Chandler Carruth | 6b98164 | 2016-12-10 06:34:44 +0000 | [diff] [blame] | 16 | // Explicit template instantiations and specialization defininitions for core |
| 17 | // template typedefs. |
Chandler Carruth | 2a54094 | 2016-02-27 10:38:10 +0000 | [diff] [blame] | 18 | namespace llvm { |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 19 | |
| 20 | // Explicit instantiations for the core proxy templates. |
Chandler Carruth | 3ab2a5a | 2016-11-28 22:04:31 +0000 | [diff] [blame] | 21 | template class AllAnalysesOn<LazyCallGraph::SCC>; |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 22 | template class AnalysisManager<LazyCallGraph::SCC, LazyCallGraph &>; |
| 23 | template class PassManager<LazyCallGraph::SCC, CGSCCAnalysisManager, |
| 24 | LazyCallGraph &, CGSCCUpdateResult &>; |
Chandler Carruth | 2a54094 | 2016-02-27 10:38:10 +0000 | [diff] [blame] | 25 | template class InnerAnalysisManagerProxy<CGSCCAnalysisManager, Module>; |
| 26 | template class OuterAnalysisManagerProxy<ModuleAnalysisManager, |
Chandler Carruth | 346542b | 2017-02-07 01:50:48 +0000 | [diff] [blame] | 27 | LazyCallGraph::SCC, LazyCallGraph &>; |
Chandler Carruth | 2a54094 | 2016-02-27 10:38:10 +0000 | [diff] [blame] | 28 | template class OuterAnalysisManagerProxy<CGSCCAnalysisManager, Function>; |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 29 | |
| 30 | /// Explicitly specialize the pass manager run method to handle call graph |
| 31 | /// updates. |
| 32 | template <> |
| 33 | PreservedAnalyses |
| 34 | PassManager<LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, |
| 35 | CGSCCUpdateResult &>::run(LazyCallGraph::SCC &InitialC, |
| 36 | CGSCCAnalysisManager &AM, |
| 37 | LazyCallGraph &G, CGSCCUpdateResult &UR) { |
| 38 | PreservedAnalyses PA = PreservedAnalyses::all(); |
| 39 | |
| 40 | if (DebugLogging) |
| 41 | dbgs() << "Starting CGSCC pass manager run.\n"; |
| 42 | |
| 43 | // The SCC may be refined while we are running passes over it, so set up |
| 44 | // a pointer that we can update. |
| 45 | LazyCallGraph::SCC *C = &InitialC; |
| 46 | |
| 47 | for (auto &Pass : Passes) { |
| 48 | if (DebugLogging) |
| 49 | dbgs() << "Running pass: " << Pass->name() << " on " << *C << "\n"; |
| 50 | |
| 51 | PreservedAnalyses PassPA = Pass->run(*C, AM, G, UR); |
| 52 | |
| 53 | // Update the SCC if necessary. |
| 54 | C = UR.UpdatedC ? UR.UpdatedC : C; |
| 55 | |
| 56 | // Check that we didn't miss any update scenario. |
| 57 | assert(!UR.InvalidatedSCCs.count(C) && "Processing an invalid SCC!"); |
| 58 | assert(C->begin() != C->end() && "Cannot have an empty SCC!"); |
| 59 | |
| 60 | // Update the analysis manager as each pass runs and potentially |
Chandler Carruth | 0c6efff1 | 2016-11-28 10:42:21 +0000 | [diff] [blame] | 61 | // invalidates analyses. |
| 62 | AM.invalidate(*C, PassPA); |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 63 | |
| 64 | // Finally, we intersect the final preserved analyses to compute the |
| 65 | // aggregate preserved set for this pass manager. |
| 66 | PA.intersect(std::move(PassPA)); |
| 67 | |
| 68 | // FIXME: Historically, the pass managers all called the LLVM context's |
| 69 | // yield function here. We don't have a generic way to acquire the |
| 70 | // context and it isn't yet clear what the right pattern is for yielding |
| 71 | // in the new pass manager so it is currently omitted. |
| 72 | // ...getContext().yield(); |
| 73 | } |
| 74 | |
Chandler Carruth | 0c6efff1 | 2016-11-28 10:42:21 +0000 | [diff] [blame] | 75 | // Invaliadtion was handled after each pass in the above loop for the current |
| 76 | // SCC. Therefore, the remaining analysis results in the AnalysisManager are |
| 77 | // preserved. We mark this with a set so that we don't need to inspect each |
| 78 | // one individually. |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 79 | PA.preserveSet<AllAnalysesOn<LazyCallGraph::SCC>>(); |
Chandler Carruth | 0c6efff1 | 2016-11-28 10:42:21 +0000 | [diff] [blame] | 80 | |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 81 | if (DebugLogging) |
| 82 | dbgs() << "Finished CGSCC pass manager run.\n"; |
| 83 | |
| 84 | return PA; |
| 85 | } |
| 86 | |
Chandler Carruth | 6b98164 | 2016-12-10 06:34:44 +0000 | [diff] [blame] | 87 | bool CGSCCAnalysisManagerModuleProxy::Result::invalidate( |
| 88 | Module &M, const PreservedAnalyses &PA, |
| 89 | ModuleAnalysisManager::Invalidator &Inv) { |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 90 | // If literally everything is preserved, we're done. |
| 91 | if (PA.areAllPreserved()) |
| 92 | return false; // This is still a valid proxy. |
| 93 | |
Chandler Carruth | 6b98164 | 2016-12-10 06:34:44 +0000 | [diff] [blame] | 94 | // If this proxy or the call graph is going to be invalidated, we also need |
| 95 | // to clear all the keys coming from that analysis. |
| 96 | // |
| 97 | // We also directly invalidate the FAM's module proxy if necessary, and if |
| 98 | // that proxy isn't preserved we can't preserve this proxy either. We rely on |
| 99 | // it to handle module -> function analysis invalidation in the face of |
| 100 | // structural changes and so if it's unavailable we conservatively clear the |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 101 | // entire SCC layer as well rather than trying to do invalidation ourselves. |
| 102 | auto PAC = PA.getChecker<CGSCCAnalysisManagerModuleProxy>(); |
| 103 | if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Module>>()) || |
Chandler Carruth | 6b98164 | 2016-12-10 06:34:44 +0000 | [diff] [blame] | 104 | Inv.invalidate<LazyCallGraphAnalysis>(M, PA) || |
| 105 | Inv.invalidate<FunctionAnalysisManagerModuleProxy>(M, PA)) { |
| 106 | InnerAM->clear(); |
| 107 | |
| 108 | // And the proxy itself should be marked as invalid so that we can observe |
| 109 | // the new call graph. This isn't strictly necessary because we cheat |
| 110 | // above, but is still useful. |
| 111 | return true; |
| 112 | } |
| 113 | |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 114 | // Directly check if the relevant set is preserved so we can short circuit |
| 115 | // invalidating SCCs below. |
| 116 | bool AreSCCAnalysesPreserved = |
| 117 | PA.allAnalysesInSetPreserved<AllAnalysesOn<LazyCallGraph::SCC>>(); |
| 118 | |
Chandler Carruth | 6b98164 | 2016-12-10 06:34:44 +0000 | [diff] [blame] | 119 | // Ok, we have a graph, so we can propagate the invalidation down into it. |
Chandler Carruth | 2e0fe3e | 2017-02-06 19:38:06 +0000 | [diff] [blame] | 120 | G->buildRefSCCs(); |
Chandler Carruth | 6b98164 | 2016-12-10 06:34:44 +0000 | [diff] [blame] | 121 | for (auto &RC : G->postorder_ref_sccs()) |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 122 | for (auto &C : RC) { |
| 123 | Optional<PreservedAnalyses> InnerPA; |
| 124 | |
| 125 | // Check to see whether the preserved set needs to be adjusted based on |
| 126 | // module-level analysis invalidation triggering deferred invalidation |
| 127 | // for this SCC. |
| 128 | if (auto *OuterProxy = |
| 129 | InnerAM->getCachedResult<ModuleAnalysisManagerCGSCCProxy>(C)) |
| 130 | for (const auto &OuterInvalidationPair : |
| 131 | OuterProxy->getOuterInvalidations()) { |
| 132 | AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first; |
| 133 | const auto &InnerAnalysisIDs = OuterInvalidationPair.second; |
| 134 | if (Inv.invalidate(OuterAnalysisID, M, PA)) { |
| 135 | if (!InnerPA) |
| 136 | InnerPA = PA; |
| 137 | for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs) |
| 138 | InnerPA->abandon(InnerAnalysisID); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // Check if we needed a custom PA set. If so we'll need to run the inner |
| 143 | // invalidation. |
| 144 | if (InnerPA) { |
| 145 | InnerAM->invalidate(C, *InnerPA); |
| 146 | continue; |
| 147 | } |
| 148 | |
| 149 | // Otherwise we only need to do invalidation if the original PA set didn't |
| 150 | // preserve all SCC analyses. |
| 151 | if (!AreSCCAnalysesPreserved) |
| 152 | InnerAM->invalidate(C, PA); |
| 153 | } |
Chandler Carruth | 6b98164 | 2016-12-10 06:34:44 +0000 | [diff] [blame] | 154 | |
| 155 | // Return false to indicate that this result is still a valid proxy. |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | template <> |
| 160 | CGSCCAnalysisManagerModuleProxy::Result |
| 161 | CGSCCAnalysisManagerModuleProxy::run(Module &M, ModuleAnalysisManager &AM) { |
| 162 | // Force the Function analysis manager to also be available so that it can |
| 163 | // be accessed in an SCC analysis and proxied onward to function passes. |
| 164 | // FIXME: It is pretty awkward to just drop the result here and assert that |
| 165 | // we can find it again later. |
| 166 | (void)AM.getResult<FunctionAnalysisManagerModuleProxy>(M); |
| 167 | |
| 168 | return Result(*InnerAM, AM.getResult<LazyCallGraphAnalysis>(M)); |
| 169 | } |
| 170 | |
| 171 | AnalysisKey FunctionAnalysisManagerCGSCCProxy::Key; |
| 172 | |
| 173 | FunctionAnalysisManagerCGSCCProxy::Result |
| 174 | FunctionAnalysisManagerCGSCCProxy::run(LazyCallGraph::SCC &C, |
| 175 | CGSCCAnalysisManager &AM, |
| 176 | LazyCallGraph &CG) { |
| 177 | // Collect the FunctionAnalysisManager from the Module layer and use that to |
| 178 | // build the proxy result. |
| 179 | // |
| 180 | // This allows us to rely on the FunctionAnalysisMangaerModuleProxy to |
| 181 | // invalidate the function analyses. |
| 182 | auto &MAM = AM.getResult<ModuleAnalysisManagerCGSCCProxy>(C, CG).getManager(); |
| 183 | Module &M = *C.begin()->getFunction().getParent(); |
| 184 | auto *FAMProxy = MAM.getCachedResult<FunctionAnalysisManagerModuleProxy>(M); |
| 185 | assert(FAMProxy && "The CGSCC pass manager requires that the FAM module " |
| 186 | "proxy is run on the module prior to entering the CGSCC " |
| 187 | "walk."); |
| 188 | |
| 189 | // Note that we special-case invalidation handling of this proxy in the CGSCC |
| 190 | // analysis manager's Module proxy. This avoids the need to do anything |
| 191 | // special here to recompute all of this if ever the FAM's module proxy goes |
| 192 | // away. |
| 193 | return Result(FAMProxy->getManager()); |
| 194 | } |
| 195 | |
| 196 | bool FunctionAnalysisManagerCGSCCProxy::Result::invalidate( |
| 197 | LazyCallGraph::SCC &C, const PreservedAnalyses &PA, |
| 198 | CGSCCAnalysisManager::Invalidator &Inv) { |
Chandler Carruth | bd9c290 | 2017-07-09 03:59:31 +0000 | [diff] [blame] | 199 | // If literally everything is preserved, we're done. |
| 200 | if (PA.areAllPreserved()) |
| 201 | return false; // This is still a valid proxy. |
Chandler Carruth | 6b98164 | 2016-12-10 06:34:44 +0000 | [diff] [blame] | 202 | |
Chandler Carruth | bd9c290 | 2017-07-09 03:59:31 +0000 | [diff] [blame] | 203 | // If this proxy isn't marked as preserved, then even if the result remains |
| 204 | // valid, the key itself may no longer be valid, so we clear everything. |
| 205 | // |
| 206 | // Note that in order to preserve this proxy, a module pass must ensure that |
| 207 | // the FAM has been completely updated to handle the deletion of functions. |
| 208 | // Specifically, any FAM-cached results for those functions need to have been |
| 209 | // forcibly cleared. When preserved, this proxy will only invalidate results |
| 210 | // cached on functions *still in the module* at the end of the module pass. |
| 211 | auto PAC = PA.getChecker<FunctionAnalysisManagerCGSCCProxy>(); |
| 212 | if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<LazyCallGraph::SCC>>()) { |
| 213 | for (LazyCallGraph::Node &N : C) |
| 214 | FAM->clear(N.getFunction()); |
| 215 | |
| 216 | return true; |
| 217 | } |
| 218 | |
| 219 | // Directly check if the relevant set is preserved. |
| 220 | bool AreFunctionAnalysesPreserved = |
| 221 | PA.allAnalysesInSetPreserved<AllAnalysesOn<Function>>(); |
| 222 | |
| 223 | // Now walk all the functions to see if any inner analysis invalidation is |
| 224 | // necessary. |
| 225 | for (LazyCallGraph::Node &N : C) { |
| 226 | Function &F = N.getFunction(); |
| 227 | Optional<PreservedAnalyses> FunctionPA; |
| 228 | |
| 229 | // Check to see whether the preserved set needs to be pruned based on |
| 230 | // SCC-level analysis invalidation that triggers deferred invalidation |
| 231 | // registered with the outer analysis manager proxy for this function. |
| 232 | if (auto *OuterProxy = |
| 233 | FAM->getCachedResult<CGSCCAnalysisManagerFunctionProxy>(F)) |
| 234 | for (const auto &OuterInvalidationPair : |
| 235 | OuterProxy->getOuterInvalidations()) { |
| 236 | AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first; |
| 237 | const auto &InnerAnalysisIDs = OuterInvalidationPair.second; |
| 238 | if (Inv.invalidate(OuterAnalysisID, C, PA)) { |
| 239 | if (!FunctionPA) |
| 240 | FunctionPA = PA; |
| 241 | for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs) |
| 242 | FunctionPA->abandon(InnerAnalysisID); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | // Check if we needed a custom PA set, and if so we'll need to run the |
| 247 | // inner invalidation. |
| 248 | if (FunctionPA) { |
| 249 | FAM->invalidate(F, *FunctionPA); |
| 250 | continue; |
| 251 | } |
| 252 | |
| 253 | // Otherwise we only need to do invalidation if the original PA set didn't |
| 254 | // preserve all function analyses. |
| 255 | if (!AreFunctionAnalysesPreserved) |
| 256 | FAM->invalidate(F, PA); |
| 257 | } |
| 258 | |
| 259 | // Return false to indicate that this result is still a valid proxy. |
Chandler Carruth | 6b98164 | 2016-12-10 06:34:44 +0000 | [diff] [blame] | 260 | return false; |
| 261 | } |
| 262 | |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 263 | } // End llvm namespace |
| 264 | |
Chandler Carruth | 7c8964d | 2017-07-09 13:16:55 +0000 | [diff] [blame] | 265 | /// When a new SCC is created for the graph and there might be function |
| 266 | /// analysis results cached for the functions now in that SCC two forms of |
| 267 | /// updates are required. |
| 268 | /// |
| 269 | /// First, a proxy from the SCC to the FunctionAnalysisManager needs to be |
| 270 | /// created so that any subsequent invalidation events to the SCC are |
| 271 | /// propagated to the function analysis results cached for functions within it. |
| 272 | /// |
| 273 | /// Second, if any of the functions within the SCC have analysis results with |
| 274 | /// outer analysis dependencies, then those dependencies would point to the |
| 275 | /// *wrong* SCC's analysis result. We forcibly invalidate the necessary |
| 276 | /// function analyses so that they don't retain stale handles. |
| 277 | static void updateNewSCCFunctionAnalyses(LazyCallGraph::SCC &C, |
| 278 | LazyCallGraph &G, |
| 279 | CGSCCAnalysisManager &AM) { |
| 280 | // Get the relevant function analysis manager. |
| 281 | auto &FAM = |
| 282 | AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, G).getManager(); |
| 283 | |
| 284 | // Now walk the functions in this SCC and invalidate any function analysis |
| 285 | // results that might have outer dependencies on an SCC analysis. |
| 286 | for (LazyCallGraph::Node &N : C) { |
| 287 | Function &F = N.getFunction(); |
| 288 | |
| 289 | auto *OuterProxy = |
| 290 | FAM.getCachedResult<CGSCCAnalysisManagerFunctionProxy>(F); |
| 291 | if (!OuterProxy) |
| 292 | // No outer analyses were queried, nothing to do. |
| 293 | continue; |
| 294 | |
| 295 | // Forcibly abandon all the inner analyses with dependencies, but |
| 296 | // invalidate nothing else. |
| 297 | auto PA = PreservedAnalyses::all(); |
| 298 | for (const auto &OuterInvalidationPair : |
| 299 | OuterProxy->getOuterInvalidations()) { |
| 300 | const auto &InnerAnalysisIDs = OuterInvalidationPair.second; |
| 301 | for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs) |
| 302 | PA.abandon(InnerAnalysisID); |
| 303 | } |
| 304 | |
| 305 | // Now invalidate anything we found. |
| 306 | FAM.invalidate(F, PA); |
| 307 | } |
| 308 | } |
| 309 | |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 310 | namespace { |
| 311 | /// Helper function to update both the \c CGSCCAnalysisManager \p AM and the \c |
| 312 | /// CGSCCPassManager's \c CGSCCUpdateResult \p UR based on a range of newly |
| 313 | /// added SCCs. |
| 314 | /// |
| 315 | /// The range of new SCCs must be in postorder already. The SCC they were split |
| 316 | /// out of must be provided as \p C. The current node being mutated and |
| 317 | /// triggering updates must be passed as \p N. |
| 318 | /// |
| 319 | /// This function returns the SCC containing \p N. This will be either \p C if |
| 320 | /// no new SCCs have been split out, or it will be the new SCC containing \p N. |
| 321 | template <typename SCCRangeT> |
| 322 | LazyCallGraph::SCC * |
| 323 | incorporateNewSCCRange(const SCCRangeT &NewSCCRange, LazyCallGraph &G, |
| 324 | LazyCallGraph::Node &N, LazyCallGraph::SCC *C, |
| 325 | CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR, |
| 326 | bool DebugLogging = false) { |
| 327 | typedef LazyCallGraph::SCC SCC; |
| 328 | |
| 329 | if (NewSCCRange.begin() == NewSCCRange.end()) |
| 330 | return C; |
| 331 | |
Chandler Carruth | 443e57e | 2016-12-28 10:34:50 +0000 | [diff] [blame] | 332 | // Add the current SCC to the worklist as its shape has changed. |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 333 | UR.CWorklist.insert(C); |
| 334 | if (DebugLogging) |
| 335 | dbgs() << "Enqueuing the existing SCC in the worklist:" << *C << "\n"; |
| 336 | |
| 337 | SCC *OldC = C; |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 338 | |
| 339 | // Update the current SCC. Note that if we have new SCCs, this must actually |
| 340 | // change the SCC. |
| 341 | assert(C != &*NewSCCRange.begin() && |
| 342 | "Cannot insert new SCCs without changing current SCC!"); |
| 343 | C = &*NewSCCRange.begin(); |
| 344 | assert(G.lookupSCC(N) == C && "Failed to update current SCC!"); |
| 345 | |
Chandler Carruth | bd9c290 | 2017-07-09 03:59:31 +0000 | [diff] [blame] | 346 | // If we had a cached FAM proxy originally, we will want to create more of |
| 347 | // them for each SCC that was split off. |
| 348 | bool NeedFAMProxy = |
| 349 | AM.getCachedResult<FunctionAnalysisManagerCGSCCProxy>(*OldC) != nullptr; |
| 350 | |
| 351 | // We need to propagate an invalidation call to all but the newly current SCC |
| 352 | // because the outer pass manager won't do that for us after splitting them. |
| 353 | // FIXME: We should accept a PreservedAnalysis from the CG updater so that if |
| 354 | // there are preserved ananalyses we can avoid invalidating them here for |
| 355 | // split-off SCCs. |
| 356 | // We know however that this will preserve any FAM proxy so go ahead and mark |
| 357 | // that. |
| 358 | PreservedAnalyses PA; |
| 359 | PA.preserve<FunctionAnalysisManagerCGSCCProxy>(); |
| 360 | AM.invalidate(*OldC, PA); |
| 361 | |
Chandler Carruth | 7c8964d | 2017-07-09 13:16:55 +0000 | [diff] [blame] | 362 | // Ensure the now-current SCC's function analyses are updated. |
Chandler Carruth | bd9c290 | 2017-07-09 03:59:31 +0000 | [diff] [blame] | 363 | if (NeedFAMProxy) |
Chandler Carruth | 7c8964d | 2017-07-09 13:16:55 +0000 | [diff] [blame] | 364 | updateNewSCCFunctionAnalyses(*C, G, AM); |
Chandler Carruth | bd9c290 | 2017-07-09 03:59:31 +0000 | [diff] [blame] | 365 | |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 366 | for (SCC &NewC : |
| 367 | reverse(make_range(std::next(NewSCCRange.begin()), NewSCCRange.end()))) { |
| 368 | assert(C != &NewC && "No need to re-visit the current SCC!"); |
| 369 | assert(OldC != &NewC && "Already handled the original SCC!"); |
| 370 | UR.CWorklist.insert(&NewC); |
| 371 | if (DebugLogging) |
| 372 | dbgs() << "Enqueuing a newly formed SCC:" << NewC << "\n"; |
Chandler Carruth | bd9c290 | 2017-07-09 03:59:31 +0000 | [diff] [blame] | 373 | |
Chandler Carruth | 7c8964d | 2017-07-09 13:16:55 +0000 | [diff] [blame] | 374 | // Ensure new SCCs' function analyses are updated. |
Chandler Carruth | bd9c290 | 2017-07-09 03:59:31 +0000 | [diff] [blame] | 375 | if (NeedFAMProxy) |
Chandler Carruth | 7c8964d | 2017-07-09 13:16:55 +0000 | [diff] [blame] | 376 | updateNewSCCFunctionAnalyses(*C, G, AM); |
Chandler Carruth | bd9c290 | 2017-07-09 03:59:31 +0000 | [diff] [blame] | 377 | |
Chandler Carruth | 7c8964d | 2017-07-09 13:16:55 +0000 | [diff] [blame] | 378 | // Also propagate a normal invalidation to the new SCC as only the current |
| 379 | // will get one from the pass manager infrastructure. |
Chandler Carruth | bd9c290 | 2017-07-09 03:59:31 +0000 | [diff] [blame] | 380 | AM.invalidate(NewC, PA); |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 381 | } |
| 382 | return C; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | LazyCallGraph::SCC &llvm::updateCGAndAnalysisManagerForFunctionPass( |
| 387 | LazyCallGraph &G, LazyCallGraph::SCC &InitialC, LazyCallGraph::Node &N, |
| 388 | CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR, bool DebugLogging) { |
| 389 | typedef LazyCallGraph::Node Node; |
| 390 | typedef LazyCallGraph::Edge Edge; |
| 391 | typedef LazyCallGraph::SCC SCC; |
| 392 | typedef LazyCallGraph::RefSCC RefSCC; |
| 393 | |
| 394 | RefSCC &InitialRC = InitialC.getOuterRefSCC(); |
| 395 | SCC *C = &InitialC; |
| 396 | RefSCC *RC = &InitialRC; |
| 397 | Function &F = N.getFunction(); |
| 398 | |
| 399 | // Walk the function body and build up the set of retained, promoted, and |
| 400 | // demoted edges. |
| 401 | SmallVector<Constant *, 16> Worklist; |
| 402 | SmallPtrSet<Constant *, 16> Visited; |
Chandler Carruth | aaad9f8 | 2017-02-09 23:24:13 +0000 | [diff] [blame] | 403 | SmallPtrSet<Node *, 16> RetainedEdges; |
| 404 | SmallSetVector<Node *, 4> PromotedRefTargets; |
| 405 | SmallSetVector<Node *, 4> DemotedCallTargets; |
Chandler Carruth | 8977223 | 2016-12-06 10:06:06 +0000 | [diff] [blame] | 406 | |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 407 | // First walk the function and handle all called functions. We do this first |
| 408 | // because if there is a single call edge, whether there are ref edges is |
| 409 | // irrelevant. |
Chandler Carruth | 8977223 | 2016-12-06 10:06:06 +0000 | [diff] [blame] | 410 | for (Instruction &I : instructions(F)) |
| 411 | if (auto CS = CallSite(&I)) |
| 412 | if (Function *Callee = CS.getCalledFunction()) |
| 413 | if (Visited.insert(Callee).second && !Callee->isDeclaration()) { |
Chandler Carruth | aaad9f8 | 2017-02-09 23:24:13 +0000 | [diff] [blame] | 414 | Node &CalleeN = *G.lookup(*Callee); |
| 415 | Edge *E = N->lookup(CalleeN); |
Chandler Carruth | 8977223 | 2016-12-06 10:06:06 +0000 | [diff] [blame] | 416 | // FIXME: We should really handle adding new calls. While it will |
| 417 | // make downstream usage more complex, there is no fundamental |
| 418 | // limitation and it will allow passes within the CGSCC to be a bit |
| 419 | // more flexible in what transforms they can do. Until then, we |
| 420 | // verify that new calls haven't been introduced. |
| 421 | assert(E && "No function transformations should introduce *new* " |
| 422 | "call edges! Any new calls should be modeled as " |
| 423 | "promoted existing ref edges!"); |
Chandler Carruth | aaad9f8 | 2017-02-09 23:24:13 +0000 | [diff] [blame] | 424 | RetainedEdges.insert(&CalleeN); |
Chandler Carruth | 8977223 | 2016-12-06 10:06:06 +0000 | [diff] [blame] | 425 | if (!E->isCall()) |
Chandler Carruth | aaad9f8 | 2017-02-09 23:24:13 +0000 | [diff] [blame] | 426 | PromotedRefTargets.insert(&CalleeN); |
Chandler Carruth | 8977223 | 2016-12-06 10:06:06 +0000 | [diff] [blame] | 427 | } |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 428 | |
| 429 | // Now walk all references. |
Chandler Carruth | 8977223 | 2016-12-06 10:06:06 +0000 | [diff] [blame] | 430 | for (Instruction &I : instructions(F)) |
| 431 | for (Value *Op : I.operand_values()) |
| 432 | if (Constant *C = dyn_cast<Constant>(Op)) |
| 433 | if (Visited.insert(C).second) |
| 434 | Worklist.push_back(C); |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 435 | |
Chandler Carruth | 8977223 | 2016-12-06 10:06:06 +0000 | [diff] [blame] | 436 | LazyCallGraph::visitReferences(Worklist, Visited, [&](Function &Referee) { |
Chandler Carruth | aaad9f8 | 2017-02-09 23:24:13 +0000 | [diff] [blame] | 437 | Node &RefereeN = *G.lookup(Referee); |
| 438 | Edge *E = N->lookup(RefereeN); |
Chandler Carruth | 8977223 | 2016-12-06 10:06:06 +0000 | [diff] [blame] | 439 | // FIXME: Similarly to new calls, we also currently preclude |
| 440 | // introducing new references. See above for details. |
| 441 | assert(E && "No function transformations should introduce *new* ref " |
| 442 | "edges! Any new ref edges would require IPO which " |
| 443 | "function passes aren't allowed to do!"); |
Chandler Carruth | aaad9f8 | 2017-02-09 23:24:13 +0000 | [diff] [blame] | 444 | RetainedEdges.insert(&RefereeN); |
Chandler Carruth | 8977223 | 2016-12-06 10:06:06 +0000 | [diff] [blame] | 445 | if (E->isCall()) |
Chandler Carruth | aaad9f8 | 2017-02-09 23:24:13 +0000 | [diff] [blame] | 446 | DemotedCallTargets.insert(&RefereeN); |
Chandler Carruth | 8977223 | 2016-12-06 10:06:06 +0000 | [diff] [blame] | 447 | }); |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 448 | |
| 449 | // First remove all of the edges that are no longer present in this function. |
| 450 | // We have to build a list of dead targets first and then remove them as the |
| 451 | // data structures will all be invalidated by removing them. |
| 452 | SmallVector<PointerIntPair<Node *, 1, Edge::Kind>, 4> DeadTargets; |
Chandler Carruth | aaad9f8 | 2017-02-09 23:24:13 +0000 | [diff] [blame] | 453 | for (Edge &E : *N) |
| 454 | if (!RetainedEdges.count(&E.getNode())) |
| 455 | DeadTargets.push_back({&E.getNode(), E.getKind()}); |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 456 | for (auto DeadTarget : DeadTargets) { |
| 457 | Node &TargetN = *DeadTarget.getPointer(); |
| 458 | bool IsCall = DeadTarget.getInt() == Edge::Call; |
| 459 | SCC &TargetC = *G.lookupSCC(TargetN); |
| 460 | RefSCC &TargetRC = TargetC.getOuterRefSCC(); |
| 461 | |
| 462 | if (&TargetRC != RC) { |
| 463 | RC->removeOutgoingEdge(N, TargetN); |
| 464 | if (DebugLogging) |
| 465 | dbgs() << "Deleting outgoing edge from '" << N << "' to '" << TargetN |
| 466 | << "'\n"; |
| 467 | continue; |
| 468 | } |
| 469 | if (DebugLogging) |
| 470 | dbgs() << "Deleting internal " << (IsCall ? "call" : "ref") |
| 471 | << " edge from '" << N << "' to '" << TargetN << "'\n"; |
| 472 | |
Chandler Carruth | 443e57e | 2016-12-28 10:34:50 +0000 | [diff] [blame] | 473 | if (IsCall) { |
| 474 | if (C != &TargetC) { |
| 475 | // For separate SCCs this is trivial. |
| 476 | RC->switchTrivialInternalEdgeToRef(N, TargetN); |
| 477 | } else { |
Chandler Carruth | 443e57e | 2016-12-28 10:34:50 +0000 | [diff] [blame] | 478 | // Now update the call graph. |
| 479 | C = incorporateNewSCCRange(RC->switchInternalEdgeToRef(N, TargetN), G, |
| 480 | N, C, AM, UR, DebugLogging); |
| 481 | } |
| 482 | } |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 483 | |
| 484 | auto NewRefSCCs = RC->removeInternalRefEdge(N, TargetN); |
| 485 | if (!NewRefSCCs.empty()) { |
| 486 | // Note that we don't bother to invalidate analyses as ref-edge |
| 487 | // connectivity is not really observable in any way and is intended |
| 488 | // exclusively to be used for ordering of transforms rather than for |
| 489 | // analysis conclusions. |
| 490 | |
| 491 | // The RC worklist is in reverse postorder, so we first enqueue the |
| 492 | // current RefSCC as it will remain the parent of all split RefSCCs, then |
| 493 | // we enqueue the new ones in RPO except for the one which contains the |
| 494 | // source node as that is the "bottom" we will continue processing in the |
| 495 | // bottom-up walk. |
| 496 | UR.RCWorklist.insert(RC); |
| 497 | if (DebugLogging) |
| 498 | dbgs() << "Enqueuing the existing RefSCC in the update worklist: " |
| 499 | << *RC << "\n"; |
| 500 | // Update the RC to the "bottom". |
| 501 | assert(G.lookupSCC(N) == C && "Changed the SCC when splitting RefSCCs!"); |
| 502 | RC = &C->getOuterRefSCC(); |
| 503 | assert(G.lookupRefSCC(N) == RC && "Failed to update current RefSCC!"); |
Chandler Carruth | 66a9568 | 2016-12-20 03:32:17 +0000 | [diff] [blame] | 504 | assert(NewRefSCCs.front() == RC && |
| 505 | "New current RefSCC not first in the returned list!"); |
| 506 | for (RefSCC *NewRC : reverse( |
| 507 | make_range(std::next(NewRefSCCs.begin()), NewRefSCCs.end()))) { |
| 508 | assert(NewRC != RC && "Should not encounter the current RefSCC further " |
| 509 | "in the postorder list of new RefSCCs."); |
| 510 | UR.RCWorklist.insert(NewRC); |
| 511 | if (DebugLogging) |
| 512 | dbgs() << "Enqueuing a new RefSCC in the update worklist: " << *NewRC |
| 513 | << "\n"; |
| 514 | } |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | |
| 518 | // Next demote all the call edges that are now ref edges. This helps make |
| 519 | // the SCCs small which should minimize the work below as we don't want to |
| 520 | // form cycles that this would break. |
Chandler Carruth | aaad9f8 | 2017-02-09 23:24:13 +0000 | [diff] [blame] | 521 | for (Node *RefTarget : DemotedCallTargets) { |
| 522 | SCC &TargetC = *G.lookupSCC(*RefTarget); |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 523 | RefSCC &TargetRC = TargetC.getOuterRefSCC(); |
| 524 | |
| 525 | // The easy case is when the target RefSCC is not this RefSCC. This is |
| 526 | // only supported when the target RefSCC is a child of this RefSCC. |
| 527 | if (&TargetRC != RC) { |
| 528 | assert(RC->isAncestorOf(TargetRC) && |
| 529 | "Cannot potentially form RefSCC cycles here!"); |
Chandler Carruth | aaad9f8 | 2017-02-09 23:24:13 +0000 | [diff] [blame] | 530 | RC->switchOutgoingEdgeToRef(N, *RefTarget); |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 531 | if (DebugLogging) |
| 532 | dbgs() << "Switch outgoing call edge to a ref edge from '" << N |
Chandler Carruth | aaad9f8 | 2017-02-09 23:24:13 +0000 | [diff] [blame] | 533 | << "' to '" << *RefTarget << "'\n"; |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 534 | continue; |
| 535 | } |
| 536 | |
Chandler Carruth | 443e57e | 2016-12-28 10:34:50 +0000 | [diff] [blame] | 537 | // We are switching an internal call edge to a ref edge. This may split up |
| 538 | // some SCCs. |
| 539 | if (C != &TargetC) { |
| 540 | // For separate SCCs this is trivial. |
Chandler Carruth | aaad9f8 | 2017-02-09 23:24:13 +0000 | [diff] [blame] | 541 | RC->switchTrivialInternalEdgeToRef(N, *RefTarget); |
Chandler Carruth | 443e57e | 2016-12-28 10:34:50 +0000 | [diff] [blame] | 542 | continue; |
| 543 | } |
| 544 | |
Chandler Carruth | 443e57e | 2016-12-28 10:34:50 +0000 | [diff] [blame] | 545 | // Now update the call graph. |
Chandler Carruth | aaad9f8 | 2017-02-09 23:24:13 +0000 | [diff] [blame] | 546 | C = incorporateNewSCCRange(RC->switchInternalEdgeToRef(N, *RefTarget), G, N, |
| 547 | C, AM, UR, DebugLogging); |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | // Now promote ref edges into call edges. |
Chandler Carruth | aaad9f8 | 2017-02-09 23:24:13 +0000 | [diff] [blame] | 551 | for (Node *CallTarget : PromotedRefTargets) { |
| 552 | SCC &TargetC = *G.lookupSCC(*CallTarget); |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 553 | RefSCC &TargetRC = TargetC.getOuterRefSCC(); |
| 554 | |
| 555 | // The easy case is when the target RefSCC is not this RefSCC. This is |
| 556 | // only supported when the target RefSCC is a child of this RefSCC. |
| 557 | if (&TargetRC != RC) { |
| 558 | assert(RC->isAncestorOf(TargetRC) && |
| 559 | "Cannot potentially form RefSCC cycles here!"); |
Chandler Carruth | aaad9f8 | 2017-02-09 23:24:13 +0000 | [diff] [blame] | 560 | RC->switchOutgoingEdgeToCall(N, *CallTarget); |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 561 | if (DebugLogging) |
| 562 | dbgs() << "Switch outgoing ref edge to a call edge from '" << N |
Chandler Carruth | aaad9f8 | 2017-02-09 23:24:13 +0000 | [diff] [blame] | 563 | << "' to '" << *CallTarget << "'\n"; |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 564 | continue; |
| 565 | } |
| 566 | if (DebugLogging) |
| 567 | dbgs() << "Switch an internal ref edge to a call edge from '" << N |
Chandler Carruth | aaad9f8 | 2017-02-09 23:24:13 +0000 | [diff] [blame] | 568 | << "' to '" << *CallTarget << "'\n"; |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 569 | |
| 570 | // Otherwise we are switching an internal ref edge to a call edge. This |
| 571 | // may merge away some SCCs, and we add those to the UpdateResult. We also |
| 572 | // need to make sure to update the worklist in the event SCCs have moved |
Chandler Carruth | c213c67 | 2017-07-09 13:45:11 +0000 | [diff] [blame] | 573 | // before the current one in the post-order sequence |
| 574 | bool HasFunctionAnalysisProxy = false; |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 575 | auto InitialSCCIndex = RC->find(*C) - RC->begin(); |
Chandler Carruth | c213c67 | 2017-07-09 13:45:11 +0000 | [diff] [blame] | 576 | bool FormedCycle = RC->switchInternalEdgeToCall( |
| 577 | N, *CallTarget, [&](ArrayRef<SCC *> MergedSCCs) { |
| 578 | for (SCC *MergedC : MergedSCCs) { |
| 579 | assert(MergedC != &TargetC && "Cannot merge away the target SCC!"); |
| 580 | |
| 581 | HasFunctionAnalysisProxy |= |
| 582 | AM.getCachedResult<FunctionAnalysisManagerCGSCCProxy>( |
| 583 | *MergedC) != nullptr; |
| 584 | |
| 585 | // Mark that this SCC will no longer be valid. |
| 586 | UR.InvalidatedSCCs.insert(MergedC); |
| 587 | |
| 588 | // FIXME: We should really do a 'clear' here to forcibly release |
| 589 | // memory, but we don't have a good way of doing that and |
| 590 | // preserving the function analyses. |
| 591 | auto PA = PreservedAnalyses::allInSet<AllAnalysesOn<Function>>(); |
| 592 | PA.preserve<FunctionAnalysisManagerCGSCCProxy>(); |
| 593 | AM.invalidate(*MergedC, PA); |
| 594 | } |
| 595 | }); |
| 596 | |
| 597 | // If we formed a cycle by creating this call, we need to update more data |
| 598 | // structures. |
| 599 | if (FormedCycle) { |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 600 | C = &TargetC; |
| 601 | assert(G.lookupSCC(N) == C && "Failed to update current SCC!"); |
| 602 | |
Chandler Carruth | c213c67 | 2017-07-09 13:45:11 +0000 | [diff] [blame] | 603 | // If one of the invalidated SCCs had a cached proxy to a function |
| 604 | // analysis manager, we need to create a proxy in the new current SCC as |
| 605 | // the invaliadted SCCs had their functions moved. |
| 606 | if (HasFunctionAnalysisProxy) |
| 607 | AM.getResult<FunctionAnalysisManagerCGSCCProxy>(*C, G); |
| 608 | |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 609 | // Any analyses cached for this SCC are no longer precise as the shape |
Chandler Carruth | c213c67 | 2017-07-09 13:45:11 +0000 | [diff] [blame] | 610 | // has changed by introducing this cycle. However, we have taken care to |
| 611 | // update the proxies so it remains valide. |
| 612 | auto PA = PreservedAnalyses::allInSet<AllAnalysesOn<Function>>(); |
| 613 | PA.preserve<FunctionAnalysisManagerCGSCCProxy>(); |
| 614 | AM.invalidate(*C, PA); |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 615 | } |
| 616 | auto NewSCCIndex = RC->find(*C) - RC->begin(); |
| 617 | if (InitialSCCIndex < NewSCCIndex) { |
| 618 | // Put our current SCC back onto the worklist as we'll visit other SCCs |
| 619 | // that are now definitively ordered prior to the current one in the |
| 620 | // post-order sequence, and may end up observing more precise context to |
| 621 | // optimize the current SCC. |
| 622 | UR.CWorklist.insert(C); |
| 623 | if (DebugLogging) |
| 624 | dbgs() << "Enqueuing the existing SCC in the worklist: " << *C << "\n"; |
| 625 | // Enqueue in reverse order as we pop off the back of the worklist. |
| 626 | for (SCC &MovedC : reverse(make_range(RC->begin() + InitialSCCIndex, |
| 627 | RC->begin() + NewSCCIndex))) { |
| 628 | UR.CWorklist.insert(&MovedC); |
| 629 | if (DebugLogging) |
| 630 | dbgs() << "Enqueuing a newly earlier in post-order SCC: " << MovedC |
| 631 | << "\n"; |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | assert(!UR.InvalidatedSCCs.count(C) && "Invalidated the current SCC!"); |
| 637 | assert(!UR.InvalidatedRefSCCs.count(RC) && "Invalidated the current RefSCC!"); |
| 638 | assert(&C->getOuterRefSCC() == RC && "Current SCC not in current RefSCC!"); |
| 639 | |
| 640 | // Record the current RefSCC and SCC for higher layers of the CGSCC pass |
| 641 | // manager now that all the updates have been applied. |
| 642 | if (RC != &InitialRC) |
| 643 | UR.UpdatedRC = RC; |
| 644 | if (C != &InitialC) |
| 645 | UR.UpdatedC = C; |
| 646 | |
| 647 | return *C; |
Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 648 | } |