1 //===- PassManager.cpp - Infrastructure for managing & running IR 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/IR/PassManager.h" 10 #include "llvm/ADT/STLExtras.h" 11 #include "llvm/IR/LLVMContext.h" 12 #include "llvm/IR/PassManagerImpl.h" 13 #include "llvm/Support/CommandLine.h" 14 15 using namespace llvm; 16 17 namespace llvm { 18 // Experimental option to eagerly invalidate more analyses. This has the 19 // potential to decrease max memory usage in exchange for more compile time. 20 // This may affect codegen due to either passes using analyses only when 21 // cached, or invalidating and recalculating an analysis that was 22 // stale/imprecise but still valid. Currently this invalidates all function 23 // analyses after a module->function or cgscc->function adaptor. 24 // TODO: make this a PipelineTuningOption. 25 cl::opt<bool> EagerlyInvalidateAnalyses( 26 "eagerly-invalidate-analyses", cl::init(false), cl::Hidden, 27 cl::desc("Eagerly invalidate more analyses in default pipelines")); 28 29 // Explicit template instantiations and specialization defininitions for core 30 // template typedefs. 31 template class AllAnalysesOn<Module>; 32 template class AllAnalysesOn<Function>; 33 template class PassManager<Module>; 34 template class PassManager<Function>; 35 template class AnalysisManager<Module>; 36 template class AnalysisManager<Function>; 37 template class InnerAnalysisManagerProxy<FunctionAnalysisManager, Module>; 38 template class OuterAnalysisManagerProxy<ModuleAnalysisManager, Function>; 39 40 template <> 41 bool FunctionAnalysisManagerModuleProxy::Result::invalidate( 42 Module &M, const PreservedAnalyses &PA, 43 ModuleAnalysisManager::Invalidator &Inv) { 44 // If literally everything is preserved, we're done. 45 if (PA.areAllPreserved()) 46 return false; // This is still a valid proxy. 47 48 // If this proxy isn't marked as preserved, then even if the result remains 49 // valid, the key itself may no longer be valid, so we clear everything. 50 // 51 // Note that in order to preserve this proxy, a module pass must ensure that 52 // the FAM has been completely updated to handle the deletion of functions. 53 // Specifically, any FAM-cached results for those functions need to have been 54 // forcibly cleared. When preserved, this proxy will only invalidate results 55 // cached on functions *still in the module* at the end of the module pass. 56 auto PAC = PA.getChecker<FunctionAnalysisManagerModuleProxy>(); 57 if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Module>>()) { 58 InnerAM->clear(); 59 return true; 60 } 61 62 // Directly check if the relevant set is preserved. 63 bool AreFunctionAnalysesPreserved = 64 PA.allAnalysesInSetPreserved<AllAnalysesOn<Function>>(); 65 66 // Now walk all the functions to see if any inner analysis invalidation is 67 // necessary. 68 for (Function &F : M) { 69 Optional<PreservedAnalyses> FunctionPA; 70 71 // Check to see whether the preserved set needs to be pruned based on 72 // module-level analysis invalidation that triggers deferred invalidation 73 // registered with the outer analysis manager proxy for this function. 74 if (auto *OuterProxy = 75 InnerAM->getCachedResult<ModuleAnalysisManagerFunctionProxy>(F)) 76 for (const auto &OuterInvalidationPair : 77 OuterProxy->getOuterInvalidations()) { 78 AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first; 79 const auto &InnerAnalysisIDs = OuterInvalidationPair.second; 80 if (Inv.invalidate(OuterAnalysisID, M, PA)) { 81 if (!FunctionPA) 82 FunctionPA = PA; 83 for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs) 84 FunctionPA->abandon(InnerAnalysisID); 85 } 86 } 87 88 // Check if we needed a custom PA set, and if so we'll need to run the 89 // inner invalidation. 90 if (FunctionPA) { 91 InnerAM->invalidate(F, *FunctionPA); 92 continue; 93 } 94 95 // Otherwise we only need to do invalidation if the original PA set didn't 96 // preserve all function analyses. 97 if (!AreFunctionAnalysesPreserved) 98 InnerAM->invalidate(F, PA); 99 } 100 101 // Return false to indicate that this result is still a valid proxy. 102 return false; 103 } 104 } // namespace llvm 105 106 void ModuleToFunctionPassAdaptor::printPipeline( 107 raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) { 108 OS << "function("; 109 Pass->printPipeline(OS, MapClassName2PassName); 110 OS << ")"; 111 } 112 113 PreservedAnalyses ModuleToFunctionPassAdaptor::run(Module &M, 114 ModuleAnalysisManager &AM) { 115 FunctionAnalysisManager &FAM = 116 AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); 117 118 // Request PassInstrumentation from analysis manager, will use it to run 119 // instrumenting callbacks for the passes later. 120 PassInstrumentation PI = AM.getResult<PassInstrumentationAnalysis>(M); 121 122 PreservedAnalyses PA = PreservedAnalyses::all(); 123 for (Function &F : M) { 124 if (F.isDeclaration()) 125 continue; 126 127 // Check the PassInstrumentation's BeforePass callbacks before running the 128 // pass, skip its execution completely if asked to (callback returns 129 // false). 130 if (!PI.runBeforePass<Function>(*Pass, F)) 131 continue; 132 133 PreservedAnalyses PassPA; 134 { 135 TimeTraceScope TimeScope(Pass->name(), F.getName()); 136 PassPA = Pass->run(F, FAM); 137 } 138 139 PI.runAfterPass(*Pass, F, PassPA); 140 141 // We know that the function pass couldn't have invalidated any other 142 // function's analyses (that's the contract of a function pass), so 143 // directly handle the function analysis manager's invalidation here. 144 FAM.invalidate(F, EagerlyInvalidateAnalyses ? PreservedAnalyses::none() 145 : PassPA); 146 147 // Then intersect the preserved set so that invalidation of module 148 // analyses will eventually occur when the module pass completes. 149 PA.intersect(std::move(PassPA)); 150 } 151 152 // The FunctionAnalysisManagerModuleProxy is preserved because (we assume) 153 // the function passes we ran didn't add or remove any functions. 154 // 155 // We also preserve all analyses on Functions, because we did all the 156 // invalidation we needed to do above. 157 PA.preserveSet<AllAnalysesOn<Function>>(); 158 PA.preserve<FunctionAnalysisManagerModuleProxy>(); 159 return PA; 160 } 161 162 AnalysisSetKey CFGAnalyses::SetKey; 163 164 AnalysisSetKey PreservedAnalyses::AllAnalysesKey; 165