Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 1 | //===- ModuleSummaryAnalysis.cpp - Module summary index builder -----------===// |
| 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 | // This pass builds a ModuleSummaryIndex object for the module, to be written |
| 11 | // to bitcode or LLVM assembly. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Analysis/ModuleSummaryAnalysis.h" |
| 16 | #include "llvm/Analysis/BlockFrequencyInfo.h" |
| 17 | #include "llvm/Analysis/BlockFrequencyInfoImpl.h" |
| 18 | #include "llvm/Analysis/BranchProbabilityInfo.h" |
Teresa Johnson | cd21a64 | 2016-07-17 14:47:01 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/IndirectCallPromotionAnalysis.h" |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/LoopInfo.h" |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/ProfileSummaryInfo.h" |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 22 | #include "llvm/IR/CallSite.h" |
| 23 | #include "llvm/IR/Dominators.h" |
Teresa Johnson | df5ef87 | 2016-04-27 14:19:38 +0000 | [diff] [blame] | 24 | #include "llvm/IR/InstIterator.h" |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 25 | #include "llvm/IR/IntrinsicInst.h" |
| 26 | #include "llvm/IR/ValueSymbolTable.h" |
| 27 | #include "llvm/Pass.h" |
| 28 | using namespace llvm; |
| 29 | |
| 30 | #define DEBUG_TYPE "module-summary-analysis" |
| 31 | |
| 32 | // Walk through the operands of a given User via worklist iteration and populate |
| 33 | // the set of GlobalValue references encountered. Invoked either on an |
| 34 | // Instruction or a GlobalVariable (which walks its initializer). |
| 35 | static void findRefEdges(const User *CurUser, DenseSet<const Value *> &RefEdges, |
| 36 | SmallPtrSet<const User *, 8> &Visited) { |
| 37 | SmallVector<const User *, 32> Worklist; |
| 38 | Worklist.push_back(CurUser); |
| 39 | |
| 40 | while (!Worklist.empty()) { |
| 41 | const User *U = Worklist.pop_back_val(); |
| 42 | |
| 43 | if (!Visited.insert(U).second) |
| 44 | continue; |
| 45 | |
| 46 | ImmutableCallSite CS(U); |
| 47 | |
| 48 | for (const auto &OI : U->operands()) { |
| 49 | const User *Operand = dyn_cast<User>(OI); |
| 50 | if (!Operand) |
| 51 | continue; |
| 52 | if (isa<BlockAddress>(Operand)) |
| 53 | continue; |
| 54 | if (isa<GlobalValue>(Operand)) { |
| 55 | // We have a reference to a global value. This should be added to |
| 56 | // the reference set unless it is a callee. Callees are handled |
| 57 | // specially by WriteFunction and are added to a separate list. |
| 58 | if (!(CS && CS.isCallee(&OI))) |
| 59 | RefEdges.insert(Operand); |
| 60 | continue; |
| 61 | } |
| 62 | Worklist.push_back(Operand); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 67 | static CalleeInfo::HotnessType getHotness(uint64_t ProfileCount, |
| 68 | ProfileSummaryInfo *PSI) { |
| 69 | if (!PSI) |
| 70 | return CalleeInfo::HotnessType::Unknown; |
| 71 | if (PSI->isHotCount(ProfileCount)) |
| 72 | return CalleeInfo::HotnessType::Hot; |
| 73 | if (PSI->isColdCount(ProfileCount)) |
| 74 | return CalleeInfo::HotnessType::Cold; |
| 75 | return CalleeInfo::HotnessType::None; |
| 76 | } |
| 77 | |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 78 | static void computeFunctionSummary(ModuleSummaryIndex &Index, const Module &M, |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 79 | const Function &F, BlockFrequencyInfo *BFI, |
Teresa Johnson | bf28c8f | 2016-10-30 05:40:44 +0000 | [diff] [blame] | 80 | ProfileSummaryInfo *PSI, |
Teresa Johnson | d5033a4 | 2016-11-14 16:40:19 +0000 | [diff] [blame] | 81 | bool HasLocalsInUsed) { |
Teresa Johnson | 02563cd | 2016-10-28 02:39:38 +0000 | [diff] [blame] | 82 | // Summary not currently supported for anonymous functions, they should |
| 83 | // have been named. |
| 84 | assert(F.hasName()); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 85 | |
| 86 | unsigned NumInsts = 0; |
| 87 | // Map from callee ValueId to profile count. Used to accumulate profile |
| 88 | // counts for all static calls to a given callee. |
| 89 | DenseMap<const Value *, CalleeInfo> CallGraphEdges; |
Teresa Johnson | cd21a64 | 2016-07-17 14:47:01 +0000 | [diff] [blame] | 90 | DenseMap<GlobalValue::GUID, CalleeInfo> IndirectCallEdges; |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 91 | DenseSet<const Value *> RefEdges; |
Teresa Johnson | cd21a64 | 2016-07-17 14:47:01 +0000 | [diff] [blame] | 92 | ICallPromotionAnalysis ICallAnalysis; |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 93 | |
Teresa Johnson | d5033a4 | 2016-11-14 16:40:19 +0000 | [diff] [blame] | 94 | bool HasInlineAsmMaybeReferencingInternal = false; |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 95 | SmallPtrSet<const User *, 8> Visited; |
Benjamin Kramer | aa20915 | 2016-06-26 17:27:42 +0000 | [diff] [blame] | 96 | for (const BasicBlock &BB : F) |
| 97 | for (const Instruction &I : BB) { |
Piotr Padlewski | 442d38c | 2016-08-30 00:46:26 +0000 | [diff] [blame] | 98 | if (isa<DbgInfoIntrinsic>(I)) |
| 99 | continue; |
| 100 | ++NumInsts; |
Benjamin Kramer | aa20915 | 2016-06-26 17:27:42 +0000 | [diff] [blame] | 101 | findRefEdges(&I, RefEdges, Visited); |
Piotr Padlewski | 442d38c | 2016-08-30 00:46:26 +0000 | [diff] [blame] | 102 | auto CS = ImmutableCallSite(&I); |
| 103 | if (!CS) |
| 104 | continue; |
Teresa Johnson | bf28c8f | 2016-10-30 05:40:44 +0000 | [diff] [blame] | 105 | |
| 106 | const auto *CI = dyn_cast<CallInst>(&I); |
| 107 | // Since we don't know exactly which local values are referenced in inline |
Teresa Johnson | d5033a4 | 2016-11-14 16:40:19 +0000 | [diff] [blame] | 108 | // assembly, conservatively mark the function as possibly referencing |
| 109 | // a local value from inline assembly to ensure we don't export a |
| 110 | // reference (which would require renaming and promotion of the |
| 111 | // referenced value). |
Teresa Johnson | bf28c8f | 2016-10-30 05:40:44 +0000 | [diff] [blame] | 112 | if (HasLocalsInUsed && CI && CI->isInlineAsm()) |
Teresa Johnson | d5033a4 | 2016-11-14 16:40:19 +0000 | [diff] [blame] | 113 | HasInlineAsmMaybeReferencingInternal = true; |
Teresa Johnson | bf28c8f | 2016-10-30 05:40:44 +0000 | [diff] [blame] | 114 | |
Teresa Johnson | 897bab9 | 2016-10-08 16:11:42 +0000 | [diff] [blame] | 115 | auto *CalledValue = CS.getCalledValue(); |
Piotr Padlewski | 442d38c | 2016-08-30 00:46:26 +0000 | [diff] [blame] | 116 | auto *CalledFunction = CS.getCalledFunction(); |
Teresa Johnson | 897bab9 | 2016-10-08 16:11:42 +0000 | [diff] [blame] | 117 | // Check if this is an alias to a function. If so, get the |
| 118 | // called aliasee for the checks below. |
| 119 | if (auto *GA = dyn_cast<GlobalAlias>(CalledValue)) { |
| 120 | assert(!CalledFunction && "Expected null called function in callsite for alias"); |
| 121 | CalledFunction = dyn_cast<Function>(GA->getBaseObject()); |
| 122 | } |
Piotr Padlewski | 442d38c | 2016-08-30 00:46:26 +0000 | [diff] [blame] | 123 | // Check if this is a direct call to a known function. |
| 124 | if (CalledFunction) { |
Teresa Johnson | 02563cd | 2016-10-28 02:39:38 +0000 | [diff] [blame] | 125 | // Skip intrinsics. |
| 126 | if (CalledFunction->isIntrinsic()) |
Piotr Padlewski | 442d38c | 2016-08-30 00:46:26 +0000 | [diff] [blame] | 127 | continue; |
Teresa Johnson | 02563cd | 2016-10-28 02:39:38 +0000 | [diff] [blame] | 128 | // We should have named any anonymous globals |
| 129 | assert(CalledFunction->hasName()); |
Piotr Padlewski | 442d38c | 2016-08-30 00:46:26 +0000 | [diff] [blame] | 130 | auto ScaledCount = BFI ? BFI->getBlockProfileCount(&BB) : None; |
Teresa Johnson | 897bab9 | 2016-10-08 16:11:42 +0000 | [diff] [blame] | 131 | // Use the original CalledValue, in case it was an alias. We want |
| 132 | // to record the call edge to the alias in that case. Eventually |
| 133 | // an alias summary will be created to associate the alias and |
| 134 | // aliasee. |
Piotr Padlewski | 442d38c | 2016-08-30 00:46:26 +0000 | [diff] [blame] | 135 | auto *CalleeId = |
Teresa Johnson | 897bab9 | 2016-10-08 16:11:42 +0000 | [diff] [blame] | 136 | M.getValueSymbolTable().lookup(CalledValue->getName()); |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 137 | |
| 138 | auto Hotness = ScaledCount ? getHotness(ScaledCount.getValue(), PSI) |
| 139 | : CalleeInfo::HotnessType::Unknown; |
| 140 | CallGraphEdges[CalleeId].updateHotness(Hotness); |
Piotr Padlewski | 442d38c | 2016-08-30 00:46:26 +0000 | [diff] [blame] | 141 | } else { |
Piotr Padlewski | 442d38c | 2016-08-30 00:46:26 +0000 | [diff] [blame] | 142 | // Skip inline assembly calls. |
| 143 | if (CI && CI->isInlineAsm()) |
| 144 | continue; |
| 145 | // Skip direct calls. |
| 146 | if (!CS.getCalledValue() || isa<Constant>(CS.getCalledValue())) |
| 147 | continue; |
| 148 | |
| 149 | uint32_t NumVals, NumCandidates; |
| 150 | uint64_t TotalCount; |
| 151 | auto CandidateProfileData = |
| 152 | ICallAnalysis.getPromotionCandidatesForInstruction( |
| 153 | &I, NumVals, TotalCount, NumCandidates); |
| 154 | for (auto &Candidate : CandidateProfileData) |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 155 | IndirectCallEdges[Candidate.Value].updateHotness( |
| 156 | getHotness(Candidate.Count, PSI)); |
Piotr Padlewski | 442d38c | 2016-08-30 00:46:26 +0000 | [diff] [blame] | 157 | } |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Mehdi Amini | c3ed48c | 2016-04-24 03:18:18 +0000 | [diff] [blame] | 160 | GlobalValueSummary::GVFlags Flags(F); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 161 | std::unique_ptr<FunctionSummary> FuncSummary = |
Mehdi Amini | c3ed48c | 2016-04-24 03:18:18 +0000 | [diff] [blame] | 162 | llvm::make_unique<FunctionSummary>(Flags, NumInsts); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 163 | FuncSummary->addCallGraphEdges(CallGraphEdges); |
Teresa Johnson | cd21a64 | 2016-07-17 14:47:01 +0000 | [diff] [blame] | 164 | FuncSummary->addCallGraphEdges(IndirectCallEdges); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 165 | FuncSummary->addRefEdges(RefEdges); |
Teresa Johnson | d5033a4 | 2016-11-14 16:40:19 +0000 | [diff] [blame] | 166 | if (HasInlineAsmMaybeReferencingInternal) |
| 167 | FuncSummary->setHasInlineAsmMaybeReferencingInternal(); |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 168 | Index.addGlobalValueSummary(F.getName(), std::move(FuncSummary)); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 171 | static void computeVariableSummary(ModuleSummaryIndex &Index, |
| 172 | const GlobalVariable &V) { |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 173 | DenseSet<const Value *> RefEdges; |
| 174 | SmallPtrSet<const User *, 8> Visited; |
| 175 | findRefEdges(&V, RefEdges, Visited); |
Mehdi Amini | c3ed48c | 2016-04-24 03:18:18 +0000 | [diff] [blame] | 176 | GlobalValueSummary::GVFlags Flags(V); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 177 | std::unique_ptr<GlobalVarSummary> GVarSummary = |
Mehdi Amini | c3ed48c | 2016-04-24 03:18:18 +0000 | [diff] [blame] | 178 | llvm::make_unique<GlobalVarSummary>(Flags); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 179 | GVarSummary->addRefEdges(RefEdges); |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 180 | Index.addGlobalValueSummary(V.getName(), std::move(GVarSummary)); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Teresa Johnson | 02563cd | 2016-10-28 02:39:38 +0000 | [diff] [blame] | 183 | static void computeAliasSummary(ModuleSummaryIndex &Index, |
| 184 | const GlobalAlias &A) { |
| 185 | GlobalValueSummary::GVFlags Flags(A); |
| 186 | std::unique_ptr<AliasSummary> AS = llvm::make_unique<AliasSummary>(Flags); |
| 187 | auto *Aliasee = A.getBaseObject(); |
| 188 | auto *AliaseeSummary = Index.getGlobalValueSummary(*Aliasee); |
| 189 | assert(AliaseeSummary && "Alias expects aliasee summary to be parsed"); |
| 190 | AS->setAliasee(AliaseeSummary); |
| 191 | Index.addGlobalValueSummary(A.getName(), std::move(AS)); |
| 192 | } |
| 193 | |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 194 | ModuleSummaryIndex llvm::buildModuleSummaryIndex( |
| 195 | const Module &M, |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 196 | std::function<BlockFrequencyInfo *(const Function &F)> GetBFICallback, |
| 197 | ProfileSummaryInfo *PSI) { |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 198 | ModuleSummaryIndex Index; |
Teresa Johnson | bf28c8f | 2016-10-30 05:40:44 +0000 | [diff] [blame] | 199 | |
Teresa Johnson | a081145 | 2016-11-10 16:57:32 +0000 | [diff] [blame] | 200 | // Identify the local values in the llvm.used and llvm.compiler.used sets, |
| 201 | // which should not be exported as they would then require renaming and |
| 202 | // promotion, but we may have opaque uses e.g. in inline asm. We collect them |
| 203 | // here because we use this information to mark functions containing inline |
| 204 | // assembly calls as not importable. |
Mehdi Amini | b6a11a7 | 2016-11-09 01:45:13 +0000 | [diff] [blame] | 205 | SmallPtrSet<GlobalValue *, 8> LocalsUsed; |
Teresa Johnson | a081145 | 2016-11-10 16:57:32 +0000 | [diff] [blame] | 206 | SmallPtrSet<GlobalValue *, 8> Used; |
| 207 | // First collect those in the llvm.used set. |
| 208 | collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false); |
| 209 | for (auto *V : Used) { |
| 210 | if (V->hasLocalLinkage()) |
| 211 | LocalsUsed.insert(V); |
| 212 | } |
| 213 | Used.clear(); |
| 214 | // Next collect those in the llvm.compiler.used set. |
| 215 | collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ true); |
Teresa Johnson | bf28c8f | 2016-10-30 05:40:44 +0000 | [diff] [blame] | 216 | for (auto *V : Used) { |
| 217 | if (V->hasLocalLinkage()) |
| 218 | LocalsUsed.insert(V); |
| 219 | } |
Teresa Johnson | b35cc69 | 2016-04-20 14:39:45 +0000 | [diff] [blame] | 220 | |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 221 | // Compute summaries for all functions defined in module, and save in the |
| 222 | // index. |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 223 | for (auto &F : M) { |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 224 | if (F.isDeclaration()) |
| 225 | continue; |
| 226 | |
| 227 | BlockFrequencyInfo *BFI = nullptr; |
| 228 | std::unique_ptr<BlockFrequencyInfo> BFIPtr; |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 229 | if (GetBFICallback) |
| 230 | BFI = GetBFICallback(F); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 231 | else if (F.getEntryCount().hasValue()) { |
| 232 | LoopInfo LI{DominatorTree(const_cast<Function &>(F))}; |
| 233 | BranchProbabilityInfo BPI{F, LI}; |
| 234 | BFIPtr = llvm::make_unique<BlockFrequencyInfo>(F, BPI, LI); |
| 235 | BFI = BFIPtr.get(); |
| 236 | } |
| 237 | |
Teresa Johnson | d5033a4 | 2016-11-14 16:40:19 +0000 | [diff] [blame] | 238 | computeFunctionSummary(Index, M, F, BFI, PSI, !LocalsUsed.empty()); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // Compute summaries for all variables defined in module, and save in the |
| 242 | // index. |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 243 | for (const GlobalVariable &G : M.globals()) { |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 244 | if (G.isDeclaration()) |
| 245 | continue; |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 246 | computeVariableSummary(Index, G); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 247 | } |
Teresa Johnson | 02563cd | 2016-10-28 02:39:38 +0000 | [diff] [blame] | 248 | |
| 249 | // Compute summaries for all aliases defined in module, and save in the |
| 250 | // index. |
| 251 | for (const GlobalAlias &A : M.aliases()) |
| 252 | computeAliasSummary(Index, A); |
| 253 | |
Teresa Johnson | bf28c8f | 2016-10-30 05:40:44 +0000 | [diff] [blame] | 254 | for (auto *V : LocalsUsed) { |
| 255 | auto *Summary = Index.getGlobalValueSummary(*V); |
| 256 | assert(Summary && "Missing summary for global value"); |
| 257 | Summary->setNoRename(); |
| 258 | } |
| 259 | |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 260 | return Index; |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Teresa Johnson | f93b246 | 2016-08-12 13:53:02 +0000 | [diff] [blame] | 263 | char ModuleSummaryIndexAnalysis::PassID; |
| 264 | |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 265 | ModuleSummaryIndex |
Teresa Johnson | f93b246 | 2016-08-12 13:53:02 +0000 | [diff] [blame] | 266 | ModuleSummaryIndexAnalysis::run(Module &M, ModuleAnalysisManager &AM) { |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 267 | ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M); |
Teresa Johnson | f93b246 | 2016-08-12 13:53:02 +0000 | [diff] [blame] | 268 | auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 269 | return buildModuleSummaryIndex( |
| 270 | M, |
| 271 | [&FAM](const Function &F) { |
| 272 | return &FAM.getResult<BlockFrequencyAnalysis>( |
| 273 | *const_cast<Function *>(&F)); |
| 274 | }, |
| 275 | &PSI); |
Teresa Johnson | f93b246 | 2016-08-12 13:53:02 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 278 | char ModuleSummaryIndexWrapperPass::ID = 0; |
| 279 | INITIALIZE_PASS_BEGIN(ModuleSummaryIndexWrapperPass, "module-summary-analysis", |
| 280 | "Module Summary Analysis", false, true) |
| 281 | INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass) |
| 282 | INITIALIZE_PASS_END(ModuleSummaryIndexWrapperPass, "module-summary-analysis", |
| 283 | "Module Summary Analysis", false, true) |
| 284 | |
| 285 | ModulePass *llvm::createModuleSummaryIndexWrapperPass() { |
| 286 | return new ModuleSummaryIndexWrapperPass(); |
| 287 | } |
| 288 | |
| 289 | ModuleSummaryIndexWrapperPass::ModuleSummaryIndexWrapperPass() |
| 290 | : ModulePass(ID) { |
| 291 | initializeModuleSummaryIndexWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 292 | } |
| 293 | |
| 294 | bool ModuleSummaryIndexWrapperPass::runOnModule(Module &M) { |
Dehao Chen | 5461d8b | 2016-09-28 21:00:58 +0000 | [diff] [blame] | 295 | auto &PSI = *getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 296 | Index = buildModuleSummaryIndex( |
| 297 | M, |
| 298 | [this](const Function &F) { |
| 299 | return &(this->getAnalysis<BlockFrequencyInfoWrapperPass>( |
| 300 | *const_cast<Function *>(&F)) |
| 301 | .getBFI()); |
| 302 | }, |
| 303 | &PSI); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 304 | return false; |
| 305 | } |
| 306 | |
| 307 | bool ModuleSummaryIndexWrapperPass::doFinalization(Module &M) { |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 308 | Index.reset(); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 309 | return false; |
| 310 | } |
| 311 | |
| 312 | void ModuleSummaryIndexWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 313 | AU.setPreservesAll(); |
| 314 | AU.addRequired<BlockFrequencyInfoWrapperPass>(); |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 315 | AU.addRequired<ProfileSummaryInfoWrapperPass>(); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 316 | } |