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" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/ArrayRef.h" |
| 17 | #include "llvm/ADT/DenseSet.h" |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/MapVector.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SetVector.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallPtrSet.h" |
| 22 | #include "llvm/ADT/SmallVector.h" |
| 23 | #include "llvm/ADT/StringRef.h" |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/BlockFrequencyInfo.h" |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/BranchProbabilityInfo.h" |
Teresa Johnson | cd21a64 | 2016-07-17 14:47:01 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/IndirectCallPromotionAnalysis.h" |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/LoopInfo.h" |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/ProfileSummaryInfo.h" |
Peter Collingbourne | 1b4137a7 | 2016-12-21 23:03:45 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/TypeMetadataUtils.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Attributes.h" |
| 31 | #include "llvm/IR/BasicBlock.h" |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 32 | #include "llvm/IR/CallSite.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Constant.h" |
| 34 | #include "llvm/IR/Constants.h" |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Dominators.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 36 | #include "llvm/IR/Function.h" |
| 37 | #include "llvm/IR/GlobalAlias.h" |
| 38 | #include "llvm/IR/GlobalValue.h" |
| 39 | #include "llvm/IR/GlobalVariable.h" |
| 40 | #include "llvm/IR/Instructions.h" |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 41 | #include "llvm/IR/IntrinsicInst.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 42 | #include "llvm/IR/Intrinsics.h" |
| 43 | #include "llvm/IR/Metadata.h" |
| 44 | #include "llvm/IR/Module.h" |
| 45 | #include "llvm/IR/ModuleSummaryIndex.h" |
| 46 | #include "llvm/IR/Use.h" |
| 47 | #include "llvm/IR/User.h" |
Peter Collingbourne | 61781ac | 2017-03-31 00:08:24 +0000 | [diff] [blame] | 48 | #include "llvm/Object/ModuleSymbolTable.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 49 | #include "llvm/Object/SymbolicFile.h" |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 50 | #include "llvm/Pass.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 51 | #include "llvm/Support/Casting.h" |
| 52 | #include <algorithm> |
| 53 | #include <cassert> |
| 54 | #include <cstdint> |
| 55 | #include <vector> |
| 56 | |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 57 | using namespace llvm; |
| 58 | |
| 59 | #define DEBUG_TYPE "module-summary-analysis" |
| 60 | |
| 61 | // Walk through the operands of a given User via worklist iteration and populate |
| 62 | // the set of GlobalValue references encountered. Invoked either on an |
| 63 | // Instruction or a GlobalVariable (which walks its initializer). |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 64 | static void findRefEdges(ModuleSummaryIndex &Index, const User *CurUser, |
| 65 | SetVector<ValueInfo> &RefEdges, |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 66 | SmallPtrSet<const User *, 8> &Visited) { |
| 67 | SmallVector<const User *, 32> Worklist; |
| 68 | Worklist.push_back(CurUser); |
| 69 | |
| 70 | while (!Worklist.empty()) { |
| 71 | const User *U = Worklist.pop_back_val(); |
| 72 | |
| 73 | if (!Visited.insert(U).second) |
| 74 | continue; |
| 75 | |
| 76 | ImmutableCallSite CS(U); |
| 77 | |
| 78 | for (const auto &OI : U->operands()) { |
| 79 | const User *Operand = dyn_cast<User>(OI); |
| 80 | if (!Operand) |
| 81 | continue; |
| 82 | if (isa<BlockAddress>(Operand)) |
| 83 | continue; |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 84 | if (auto *GV = dyn_cast<GlobalValue>(Operand)) { |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 85 | // We have a reference to a global value. This should be added to |
| 86 | // the reference set unless it is a callee. Callees are handled |
| 87 | // specially by WriteFunction and are added to a separate list. |
| 88 | if (!(CS && CS.isCallee(&OI))) |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 89 | RefEdges.insert(Index.getOrInsertValueInfo(GV)); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 90 | continue; |
| 91 | } |
| 92 | Worklist.push_back(Operand); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 97 | static CalleeInfo::HotnessType getHotness(uint64_t ProfileCount, |
| 98 | ProfileSummaryInfo *PSI) { |
| 99 | if (!PSI) |
| 100 | return CalleeInfo::HotnessType::Unknown; |
| 101 | if (PSI->isHotCount(ProfileCount)) |
| 102 | return CalleeInfo::HotnessType::Hot; |
| 103 | if (PSI->isColdCount(ProfileCount)) |
| 104 | return CalleeInfo::HotnessType::Cold; |
| 105 | return CalleeInfo::HotnessType::None; |
| 106 | } |
| 107 | |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 108 | static bool isNonRenamableLocal(const GlobalValue &GV) { |
| 109 | return GV.hasSection() && GV.hasLocalLinkage(); |
| 110 | } |
| 111 | |
Peter Collingbourne | be9ffaa | 2017-02-10 22:29:38 +0000 | [diff] [blame] | 112 | /// Determine whether this call has all constant integer arguments (excluding |
| 113 | /// "this") and summarize it to VCalls or ConstVCalls as appropriate. |
| 114 | static void addVCallToSet(DevirtCallSite Call, GlobalValue::GUID Guid, |
| 115 | SetVector<FunctionSummary::VFuncId> &VCalls, |
| 116 | SetVector<FunctionSummary::ConstVCall> &ConstVCalls) { |
| 117 | std::vector<uint64_t> Args; |
| 118 | // Start from the second argument to skip the "this" pointer. |
| 119 | for (auto &Arg : make_range(Call.CS.arg_begin() + 1, Call.CS.arg_end())) { |
| 120 | auto *CI = dyn_cast<ConstantInt>(Arg); |
| 121 | if (!CI || CI->getBitWidth() > 64) { |
| 122 | VCalls.insert({Guid, Call.Offset}); |
| 123 | return; |
| 124 | } |
| 125 | Args.push_back(CI->getZExtValue()); |
| 126 | } |
| 127 | ConstVCalls.insert({{Guid, Call.Offset}, std::move(Args)}); |
| 128 | } |
| 129 | |
| 130 | /// If this intrinsic call requires that we add information to the function |
| 131 | /// summary, do so via the non-constant reference arguments. |
| 132 | static void addIntrinsicToSummary( |
| 133 | const CallInst *CI, SetVector<GlobalValue::GUID> &TypeTests, |
| 134 | SetVector<FunctionSummary::VFuncId> &TypeTestAssumeVCalls, |
| 135 | SetVector<FunctionSummary::VFuncId> &TypeCheckedLoadVCalls, |
| 136 | SetVector<FunctionSummary::ConstVCall> &TypeTestAssumeConstVCalls, |
| 137 | SetVector<FunctionSummary::ConstVCall> &TypeCheckedLoadConstVCalls) { |
| 138 | switch (CI->getCalledFunction()->getIntrinsicID()) { |
| 139 | case Intrinsic::type_test: { |
| 140 | auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(1)); |
| 141 | auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata()); |
| 142 | if (!TypeId) |
| 143 | break; |
| 144 | GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString()); |
| 145 | |
| 146 | // Produce a summary from type.test intrinsics. We only summarize type.test |
| 147 | // intrinsics that are used other than by an llvm.assume intrinsic. |
| 148 | // Intrinsics that are assumed are relevant only to the devirtualization |
| 149 | // pass, not the type test lowering pass. |
| 150 | bool HasNonAssumeUses = llvm::any_of(CI->uses(), [](const Use &CIU) { |
| 151 | auto *AssumeCI = dyn_cast<CallInst>(CIU.getUser()); |
| 152 | if (!AssumeCI) |
| 153 | return true; |
| 154 | Function *F = AssumeCI->getCalledFunction(); |
| 155 | return !F || F->getIntrinsicID() != Intrinsic::assume; |
| 156 | }); |
| 157 | if (HasNonAssumeUses) |
| 158 | TypeTests.insert(Guid); |
| 159 | |
| 160 | SmallVector<DevirtCallSite, 4> DevirtCalls; |
| 161 | SmallVector<CallInst *, 4> Assumes; |
| 162 | findDevirtualizableCallsForTypeTest(DevirtCalls, Assumes, CI); |
| 163 | for (auto &Call : DevirtCalls) |
| 164 | addVCallToSet(Call, Guid, TypeTestAssumeVCalls, |
| 165 | TypeTestAssumeConstVCalls); |
| 166 | |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | case Intrinsic::type_checked_load: { |
| 171 | auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(2)); |
| 172 | auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata()); |
| 173 | if (!TypeId) |
| 174 | break; |
| 175 | GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString()); |
| 176 | |
| 177 | SmallVector<DevirtCallSite, 4> DevirtCalls; |
| 178 | SmallVector<Instruction *, 4> LoadedPtrs; |
| 179 | SmallVector<Instruction *, 4> Preds; |
| 180 | bool HasNonCallUses = false; |
| 181 | findDevirtualizableCallsForTypeCheckedLoad(DevirtCalls, LoadedPtrs, Preds, |
| 182 | HasNonCallUses, CI); |
| 183 | // Any non-call uses of the result of llvm.type.checked.load will |
| 184 | // prevent us from optimizing away the llvm.type.test. |
| 185 | if (HasNonCallUses) |
| 186 | TypeTests.insert(Guid); |
| 187 | for (auto &Call : DevirtCalls) |
| 188 | addVCallToSet(Call, Guid, TypeCheckedLoadVCalls, |
| 189 | TypeCheckedLoadConstVCalls); |
| 190 | |
| 191 | break; |
| 192 | } |
| 193 | default: |
| 194 | break; |
| 195 | } |
| 196 | } |
| 197 | |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 198 | static void |
| 199 | computeFunctionSummary(ModuleSummaryIndex &Index, const Module &M, |
| 200 | const Function &F, BlockFrequencyInfo *BFI, |
Peter Collingbourne | 5e8b94c1 | 2017-09-01 16:24:02 +0000 | [diff] [blame] | 201 | ProfileSummaryInfo *PSI, bool HasLocalsInUsedOrAsm, |
Teresa Johnson | e27b058 | 2017-01-05 14:59:56 +0000 | [diff] [blame] | 202 | DenseSet<GlobalValue::GUID> &CantBePromoted) { |
Teresa Johnson | 02563cd | 2016-10-28 02:39:38 +0000 | [diff] [blame] | 203 | // Summary not currently supported for anonymous functions, they should |
| 204 | // have been named. |
| 205 | assert(F.hasName()); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 206 | |
| 207 | unsigned NumInsts = 0; |
| 208 | // Map from callee ValueId to profile count. Used to accumulate profile |
| 209 | // counts for all static calls to a given callee. |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 210 | MapVector<ValueInfo, CalleeInfo> CallGraphEdges; |
| 211 | SetVector<ValueInfo> RefEdges; |
Peter Collingbourne | 1b4137a7 | 2016-12-21 23:03:45 +0000 | [diff] [blame] | 212 | SetVector<GlobalValue::GUID> TypeTests; |
Peter Collingbourne | be9ffaa | 2017-02-10 22:29:38 +0000 | [diff] [blame] | 213 | SetVector<FunctionSummary::VFuncId> TypeTestAssumeVCalls, |
| 214 | TypeCheckedLoadVCalls; |
| 215 | SetVector<FunctionSummary::ConstVCall> TypeTestAssumeConstVCalls, |
| 216 | TypeCheckedLoadConstVCalls; |
Teresa Johnson | cd21a64 | 2016-07-17 14:47:01 +0000 | [diff] [blame] | 217 | ICallPromotionAnalysis ICallAnalysis; |
Peter Collingbourne | 681fbb6 | 2017-09-07 05:35:35 +0000 | [diff] [blame] | 218 | SmallPtrSet<const User *, 8> Visited; |
| 219 | |
| 220 | // Add personality function, prefix data and prologue data to function's ref |
| 221 | // list. |
| 222 | findRefEdges(Index, &F, RefEdges, Visited); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 223 | |
Teresa Johnson | d5033a4 | 2016-11-14 16:40:19 +0000 | [diff] [blame] | 224 | bool HasInlineAsmMaybeReferencingInternal = false; |
Benjamin Kramer | aa20915 | 2016-06-26 17:27:42 +0000 | [diff] [blame] | 225 | for (const BasicBlock &BB : F) |
| 226 | for (const Instruction &I : BB) { |
Piotr Padlewski | 442d38c | 2016-08-30 00:46:26 +0000 | [diff] [blame] | 227 | if (isa<DbgInfoIntrinsic>(I)) |
| 228 | continue; |
| 229 | ++NumInsts; |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 230 | findRefEdges(Index, &I, RefEdges, Visited); |
Piotr Padlewski | 442d38c | 2016-08-30 00:46:26 +0000 | [diff] [blame] | 231 | auto CS = ImmutableCallSite(&I); |
| 232 | if (!CS) |
| 233 | continue; |
Teresa Johnson | bf28c8f | 2016-10-30 05:40:44 +0000 | [diff] [blame] | 234 | |
| 235 | const auto *CI = dyn_cast<CallInst>(&I); |
| 236 | // 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] | 237 | // assembly, conservatively mark the function as possibly referencing |
| 238 | // a local value from inline assembly to ensure we don't export a |
| 239 | // reference (which would require renaming and promotion of the |
| 240 | // referenced value). |
Peter Collingbourne | 5e8b94c1 | 2017-09-01 16:24:02 +0000 | [diff] [blame] | 241 | if (HasLocalsInUsedOrAsm && CI && CI->isInlineAsm()) |
Teresa Johnson | d5033a4 | 2016-11-14 16:40:19 +0000 | [diff] [blame] | 242 | HasInlineAsmMaybeReferencingInternal = true; |
Teresa Johnson | bf28c8f | 2016-10-30 05:40:44 +0000 | [diff] [blame] | 243 | |
Teresa Johnson | 897bab9 | 2016-10-08 16:11:42 +0000 | [diff] [blame] | 244 | auto *CalledValue = CS.getCalledValue(); |
Piotr Padlewski | 442d38c | 2016-08-30 00:46:26 +0000 | [diff] [blame] | 245 | auto *CalledFunction = CS.getCalledFunction(); |
Volodymyr Sapsai | a739602 | 2017-11-10 00:47:47 +0000 | [diff] [blame] | 246 | if (CalledValue && !CalledFunction) { |
| 247 | CalledValue = CalledValue->stripPointerCastsNoFollowAliases(); |
| 248 | // Stripping pointer casts can reveal a called function. |
| 249 | CalledFunction = dyn_cast<Function>(CalledValue); |
| 250 | } |
Teresa Johnson | 897bab9 | 2016-10-08 16:11:42 +0000 | [diff] [blame] | 251 | // Check if this is an alias to a function. If so, get the |
| 252 | // called aliasee for the checks below. |
| 253 | if (auto *GA = dyn_cast<GlobalAlias>(CalledValue)) { |
| 254 | assert(!CalledFunction && "Expected null called function in callsite for alias"); |
| 255 | CalledFunction = dyn_cast<Function>(GA->getBaseObject()); |
| 256 | } |
Peter Collingbourne | 1b4137a7 | 2016-12-21 23:03:45 +0000 | [diff] [blame] | 257 | // Check if this is a direct call to a known function or a known |
| 258 | // intrinsic, or an indirect call with profile data. |
Piotr Padlewski | 442d38c | 2016-08-30 00:46:26 +0000 | [diff] [blame] | 259 | if (CalledFunction) { |
Peter Collingbourne | be9ffaa | 2017-02-10 22:29:38 +0000 | [diff] [blame] | 260 | if (CI && CalledFunction->isIntrinsic()) { |
| 261 | addIntrinsicToSummary( |
| 262 | CI, TypeTests, TypeTestAssumeVCalls, TypeCheckedLoadVCalls, |
| 263 | TypeTestAssumeConstVCalls, TypeCheckedLoadConstVCalls); |
| 264 | continue; |
Peter Collingbourne | 1b4137a7 | 2016-12-21 23:03:45 +0000 | [diff] [blame] | 265 | } |
Teresa Johnson | 02563cd | 2016-10-28 02:39:38 +0000 | [diff] [blame] | 266 | // We should have named any anonymous globals |
| 267 | assert(CalledFunction->hasName()); |
Easwaran Raman | f5f9160 | 2017-05-09 23:21:10 +0000 | [diff] [blame] | 268 | auto ScaledCount = PSI->getProfileCount(&I, BFI); |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 269 | auto Hotness = ScaledCount ? getHotness(ScaledCount.getValue(), PSI) |
| 270 | : CalleeInfo::HotnessType::Unknown; |
| 271 | |
Teresa Johnson | 897bab9 | 2016-10-08 16:11:42 +0000 | [diff] [blame] | 272 | // Use the original CalledValue, in case it was an alias. We want |
| 273 | // to record the call edge to the alias in that case. Eventually |
| 274 | // an alias summary will be created to associate the alias and |
| 275 | // aliasee. |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 276 | CallGraphEdges[Index.getOrInsertValueInfo( |
| 277 | cast<GlobalValue>(CalledValue))] |
| 278 | .updateHotness(Hotness); |
Piotr Padlewski | 442d38c | 2016-08-30 00:46:26 +0000 | [diff] [blame] | 279 | } else { |
Piotr Padlewski | 442d38c | 2016-08-30 00:46:26 +0000 | [diff] [blame] | 280 | // Skip inline assembly calls. |
| 281 | if (CI && CI->isInlineAsm()) |
| 282 | continue; |
Volodymyr Sapsai | 8b46ff1 | 2017-11-17 18:28:05 +0000 | [diff] [blame] | 283 | // Skip direct calls. |
| 284 | if (!CalledValue || isa<Constant>(CalledValue)) |
| 285 | continue; |
Piotr Padlewski | 442d38c | 2016-08-30 00:46:26 +0000 | [diff] [blame] | 286 | |
| 287 | uint32_t NumVals, NumCandidates; |
| 288 | uint64_t TotalCount; |
| 289 | auto CandidateProfileData = |
| 290 | ICallAnalysis.getPromotionCandidatesForInstruction( |
| 291 | &I, NumVals, TotalCount, NumCandidates); |
| 292 | for (auto &Candidate : CandidateProfileData) |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 293 | CallGraphEdges[Index.getOrInsertValueInfo(Candidate.Value)] |
| 294 | .updateHotness(getHotness(Candidate.Count, PSI)); |
Piotr Padlewski | 442d38c | 2016-08-30 00:46:26 +0000 | [diff] [blame] | 295 | } |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Dehao Chen | a60cdd3 | 2017-02-28 18:09:44 +0000 | [diff] [blame] | 298 | // Explicit add hot edges to enforce importing for designated GUIDs for |
| 299 | // sample PGO, to enable the same inlines as the profiled optimized binary. |
| 300 | for (auto &I : F.getImportGUIDs()) |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 301 | CallGraphEdges[Index.getOrInsertValueInfo(I)].updateHotness( |
Dehao Chen | 64c4657 | 2017-07-07 21:01:00 +0000 | [diff] [blame] | 302 | CalleeInfo::HotnessType::Critical); |
Dehao Chen | a60cdd3 | 2017-02-28 18:09:44 +0000 | [diff] [blame] | 303 | |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 304 | bool NonRenamableLocal = isNonRenamableLocal(F); |
| 305 | bool NotEligibleForImport = |
| 306 | NonRenamableLocal || HasInlineAsmMaybeReferencingInternal || |
| 307 | // Inliner doesn't handle variadic functions. |
| 308 | // FIXME: refactor this to use the same code that inliner is using. |
| 309 | F.isVarArg(); |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 310 | GlobalValueSummary::GVFlags Flags(F.getLinkage(), NotEligibleForImport, |
Sean Fertile | 4595a91 | 2017-11-04 17:04:39 +0000 | [diff] [blame] | 311 | /* Live = */ false, F.isDSOLocal()); |
Charles Saternos | 75da10d | 2017-08-04 16:00:58 +0000 | [diff] [blame] | 312 | FunctionSummary::FFlags FunFlags{ |
| 313 | F.hasFnAttribute(Attribute::ReadNone), |
| 314 | F.hasFnAttribute(Attribute::ReadOnly), |
| 315 | F.hasFnAttribute(Attribute::NoRecurse), |
| 316 | F.returnDoesNotAlias(), |
| 317 | }; |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 318 | auto FuncSummary = llvm::make_unique<FunctionSummary>( |
Charles Saternos | 75da10d | 2017-08-04 16:00:58 +0000 | [diff] [blame] | 319 | Flags, NumInsts, FunFlags, RefEdges.takeVector(), |
| 320 | CallGraphEdges.takeVector(), TypeTests.takeVector(), |
| 321 | TypeTestAssumeVCalls.takeVector(), TypeCheckedLoadVCalls.takeVector(), |
Peter Collingbourne | be9ffaa | 2017-02-10 22:29:38 +0000 | [diff] [blame] | 322 | TypeTestAssumeConstVCalls.takeVector(), |
| 323 | TypeCheckedLoadConstVCalls.takeVector()); |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 324 | if (NonRenamableLocal) |
| 325 | CantBePromoted.insert(F.getGUID()); |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 326 | Index.addGlobalValueSummary(F.getName(), std::move(FuncSummary)); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 329 | static void |
| 330 | computeVariableSummary(ModuleSummaryIndex &Index, const GlobalVariable &V, |
Teresa Johnson | e27b058 | 2017-01-05 14:59:56 +0000 | [diff] [blame] | 331 | DenseSet<GlobalValue::GUID> &CantBePromoted) { |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 332 | SetVector<ValueInfo> RefEdges; |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 333 | SmallPtrSet<const User *, 8> Visited; |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 334 | findRefEdges(Index, &V, RefEdges, Visited); |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 335 | bool NonRenamableLocal = isNonRenamableLocal(V); |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 336 | GlobalValueSummary::GVFlags Flags(V.getLinkage(), NonRenamableLocal, |
Sean Fertile | 4595a91 | 2017-11-04 17:04:39 +0000 | [diff] [blame] | 337 | /* Live = */ false, V.isDSOLocal()); |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 338 | auto GVarSummary = |
| 339 | llvm::make_unique<GlobalVarSummary>(Flags, RefEdges.takeVector()); |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 340 | if (NonRenamableLocal) |
| 341 | CantBePromoted.insert(V.getGUID()); |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 342 | Index.addGlobalValueSummary(V.getName(), std::move(GVarSummary)); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 345 | static void |
| 346 | computeAliasSummary(ModuleSummaryIndex &Index, const GlobalAlias &A, |
Teresa Johnson | e27b058 | 2017-01-05 14:59:56 +0000 | [diff] [blame] | 347 | DenseSet<GlobalValue::GUID> &CantBePromoted) { |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 348 | bool NonRenamableLocal = isNonRenamableLocal(A); |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 349 | GlobalValueSummary::GVFlags Flags(A.getLinkage(), NonRenamableLocal, |
Sean Fertile | 4595a91 | 2017-11-04 17:04:39 +0000 | [diff] [blame] | 350 | /* Live = */ false, A.isDSOLocal()); |
Teresa Johnson | cbdc5ff | 2017-09-13 17:10:24 +0000 | [diff] [blame] | 351 | auto AS = llvm::make_unique<AliasSummary>(Flags); |
Teresa Johnson | 02563cd | 2016-10-28 02:39:38 +0000 | [diff] [blame] | 352 | auto *Aliasee = A.getBaseObject(); |
| 353 | auto *AliaseeSummary = Index.getGlobalValueSummary(*Aliasee); |
| 354 | assert(AliaseeSummary && "Alias expects aliasee summary to be parsed"); |
| 355 | AS->setAliasee(AliaseeSummary); |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 356 | if (NonRenamableLocal) |
| 357 | CantBePromoted.insert(A.getGUID()); |
Teresa Johnson | 02563cd | 2016-10-28 02:39:38 +0000 | [diff] [blame] | 358 | Index.addGlobalValueSummary(A.getName(), std::move(AS)); |
| 359 | } |
| 360 | |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 361 | // Set LiveRoot flag on entries matching the given value name. |
| 362 | static void setLiveRoot(ModuleSummaryIndex &Index, StringRef Name) { |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 363 | if (ValueInfo VI = Index.getValueInfo(GlobalValue::getGUID(Name))) |
| 364 | for (auto &Summary : VI.getSummaryList()) |
Evgeniy Stepanov | 56584bb | 2017-06-01 20:30:06 +0000 | [diff] [blame] | 365 | Summary->setLive(true); |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 368 | ModuleSummaryIndex llvm::buildModuleSummaryIndex( |
| 369 | const Module &M, |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 370 | std::function<BlockFrequencyInfo *(const Function &F)> GetBFICallback, |
| 371 | ProfileSummaryInfo *PSI) { |
Teresa Johnson | 94624ac | 2017-05-10 18:52:16 +0000 | [diff] [blame] | 372 | assert(PSI); |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 373 | ModuleSummaryIndex Index; |
Teresa Johnson | bf28c8f | 2016-10-30 05:40:44 +0000 | [diff] [blame] | 374 | |
Teresa Johnson | a081145 | 2016-11-10 16:57:32 +0000 | [diff] [blame] | 375 | // Identify the local values in the llvm.used and llvm.compiler.used sets, |
| 376 | // which should not be exported as they would then require renaming and |
| 377 | // promotion, but we may have opaque uses e.g. in inline asm. We collect them |
| 378 | // here because we use this information to mark functions containing inline |
| 379 | // assembly calls as not importable. |
Mehdi Amini | b6a11a7 | 2016-11-09 01:45:13 +0000 | [diff] [blame] | 380 | SmallPtrSet<GlobalValue *, 8> LocalsUsed; |
Teresa Johnson | a081145 | 2016-11-10 16:57:32 +0000 | [diff] [blame] | 381 | SmallPtrSet<GlobalValue *, 8> Used; |
| 382 | // First collect those in the llvm.used set. |
| 383 | collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false); |
Teresa Johnson | a081145 | 2016-11-10 16:57:32 +0000 | [diff] [blame] | 384 | // Next collect those in the llvm.compiler.used set. |
| 385 | collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ true); |
Teresa Johnson | e27b058 | 2017-01-05 14:59:56 +0000 | [diff] [blame] | 386 | DenseSet<GlobalValue::GUID> CantBePromoted; |
Teresa Johnson | bf28c8f | 2016-10-30 05:40:44 +0000 | [diff] [blame] | 387 | for (auto *V : Used) { |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 388 | if (V->hasLocalLinkage()) { |
Teresa Johnson | bf28c8f | 2016-10-30 05:40:44 +0000 | [diff] [blame] | 389 | LocalsUsed.insert(V); |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 390 | CantBePromoted.insert(V->getGUID()); |
| 391 | } |
Teresa Johnson | bf28c8f | 2016-10-30 05:40:44 +0000 | [diff] [blame] | 392 | } |
Teresa Johnson | b35cc69 | 2016-04-20 14:39:45 +0000 | [diff] [blame] | 393 | |
Peter Collingbourne | 5e8b94c1 | 2017-09-01 16:24:02 +0000 | [diff] [blame] | 394 | bool HasLocalInlineAsmSymbol = false; |
| 395 | if (!M.getModuleInlineAsm().empty()) { |
| 396 | // Collect the local values defined by module level asm, and set up |
| 397 | // summaries for these symbols so that they can be marked as NoRename, |
| 398 | // to prevent export of any use of them in regular IR that would require |
| 399 | // renaming within the module level asm. Note we don't need to create a |
| 400 | // summary for weak or global defs, as they don't need to be flagged as |
| 401 | // NoRename, and defs in module level asm can't be imported anyway. |
| 402 | // Also, any values used but not defined within module level asm should |
| 403 | // be listed on the llvm.used or llvm.compiler.used global and marked as |
| 404 | // referenced from there. |
| 405 | ModuleSymbolTable::CollectAsmSymbols( |
| 406 | M, [&](StringRef Name, object::BasicSymbolRef::Flags Flags) { |
| 407 | // Symbols not marked as Weak or Global are local definitions. |
| 408 | if (Flags & (object::BasicSymbolRef::SF_Weak | |
| 409 | object::BasicSymbolRef::SF_Global)) |
| 410 | return; |
| 411 | HasLocalInlineAsmSymbol = true; |
| 412 | GlobalValue *GV = M.getNamedValue(Name); |
| 413 | if (!GV) |
| 414 | return; |
| 415 | assert(GV->isDeclaration() && "Def in module asm already has definition"); |
| 416 | GlobalValueSummary::GVFlags GVFlags(GlobalValue::InternalLinkage, |
| 417 | /* NotEligibleToImport = */ true, |
Sean Fertile | 4595a91 | 2017-11-04 17:04:39 +0000 | [diff] [blame] | 418 | /* Live = */ true, |
| 419 | /* Local */ GV->isDSOLocal()); |
Peter Collingbourne | 5e8b94c1 | 2017-09-01 16:24:02 +0000 | [diff] [blame] | 420 | CantBePromoted.insert(GlobalValue::getGUID(Name)); |
| 421 | // Create the appropriate summary type. |
| 422 | if (Function *F = dyn_cast<Function>(GV)) { |
| 423 | std::unique_ptr<FunctionSummary> Summary = |
| 424 | llvm::make_unique<FunctionSummary>( |
| 425 | GVFlags, 0, |
| 426 | FunctionSummary::FFlags{ |
| 427 | F->hasFnAttribute(Attribute::ReadNone), |
| 428 | F->hasFnAttribute(Attribute::ReadOnly), |
| 429 | F->hasFnAttribute(Attribute::NoRecurse), |
| 430 | F->returnDoesNotAlias()}, |
| 431 | ArrayRef<ValueInfo>{}, ArrayRef<FunctionSummary::EdgeTy>{}, |
| 432 | ArrayRef<GlobalValue::GUID>{}, |
| 433 | ArrayRef<FunctionSummary::VFuncId>{}, |
| 434 | ArrayRef<FunctionSummary::VFuncId>{}, |
| 435 | ArrayRef<FunctionSummary::ConstVCall>{}, |
| 436 | ArrayRef<FunctionSummary::ConstVCall>{}); |
| 437 | Index.addGlobalValueSummary(Name, std::move(Summary)); |
| 438 | } else { |
| 439 | std::unique_ptr<GlobalVarSummary> Summary = |
| 440 | llvm::make_unique<GlobalVarSummary>(GVFlags, |
| 441 | ArrayRef<ValueInfo>{}); |
| 442 | Index.addGlobalValueSummary(Name, std::move(Summary)); |
| 443 | } |
| 444 | }); |
| 445 | } |
| 446 | |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 447 | // Compute summaries for all functions defined in module, and save in the |
| 448 | // index. |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 449 | for (auto &F : M) { |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 450 | if (F.isDeclaration()) |
| 451 | continue; |
| 452 | |
| 453 | BlockFrequencyInfo *BFI = nullptr; |
| 454 | std::unique_ptr<BlockFrequencyInfo> BFIPtr; |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 455 | if (GetBFICallback) |
| 456 | BFI = GetBFICallback(F); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 457 | else if (F.getEntryCount().hasValue()) { |
| 458 | LoopInfo LI{DominatorTree(const_cast<Function &>(F))}; |
| 459 | BranchProbabilityInfo BPI{F, LI}; |
| 460 | BFIPtr = llvm::make_unique<BlockFrequencyInfo>(F, BPI, LI); |
| 461 | BFI = BFIPtr.get(); |
| 462 | } |
| 463 | |
Peter Collingbourne | 5e8b94c1 | 2017-09-01 16:24:02 +0000 | [diff] [blame] | 464 | computeFunctionSummary(Index, M, F, BFI, PSI, |
| 465 | !LocalsUsed.empty() || HasLocalInlineAsmSymbol, |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 466 | CantBePromoted); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | // Compute summaries for all variables defined in module, and save in the |
| 470 | // index. |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 471 | for (const GlobalVariable &G : M.globals()) { |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 472 | if (G.isDeclaration()) |
| 473 | continue; |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 474 | computeVariableSummary(Index, G, CantBePromoted); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 475 | } |
Teresa Johnson | 02563cd | 2016-10-28 02:39:38 +0000 | [diff] [blame] | 476 | |
| 477 | // Compute summaries for all aliases defined in module, and save in the |
| 478 | // index. |
| 479 | for (const GlobalAlias &A : M.aliases()) |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 480 | computeAliasSummary(Index, A, CantBePromoted); |
Teresa Johnson | 02563cd | 2016-10-28 02:39:38 +0000 | [diff] [blame] | 481 | |
Teresa Johnson | bf28c8f | 2016-10-30 05:40:44 +0000 | [diff] [blame] | 482 | for (auto *V : LocalsUsed) { |
| 483 | auto *Summary = Index.getGlobalValueSummary(*V); |
| 484 | assert(Summary && "Missing summary for global value"); |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 485 | Summary->setNotEligibleToImport(); |
Teresa Johnson | bf28c8f | 2016-10-30 05:40:44 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 488 | // The linker doesn't know about these LLVM produced values, so we need |
| 489 | // to flag them as live in the index to ensure index-based dead value |
| 490 | // analysis treats them as live roots of the analysis. |
| 491 | setLiveRoot(Index, "llvm.used"); |
| 492 | setLiveRoot(Index, "llvm.compiler.used"); |
| 493 | setLiveRoot(Index, "llvm.global_ctors"); |
| 494 | setLiveRoot(Index, "llvm.global_dtors"); |
| 495 | setLiveRoot(Index, "llvm.global.annotations"); |
| 496 | |
Peter Collingbourne | e357fbd | 2017-06-08 23:01:49 +0000 | [diff] [blame] | 497 | bool IsThinLTO = true; |
| 498 | if (auto *MD = |
| 499 | mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("ThinLTO"))) |
| 500 | IsThinLTO = MD->getZExtValue(); |
| 501 | |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 502 | for (auto &GlobalList : Index) { |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 503 | // Ignore entries for references that are undefined in the current module. |
| 504 | if (GlobalList.second.SummaryList.empty()) |
| 505 | continue; |
| 506 | |
| 507 | assert(GlobalList.second.SummaryList.size() == 1 && |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 508 | "Expected module's index to have one summary per GUID"); |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 509 | auto &Summary = GlobalList.second.SummaryList[0]; |
Peter Collingbourne | e357fbd | 2017-06-08 23:01:49 +0000 | [diff] [blame] | 510 | if (!IsThinLTO) { |
| 511 | Summary->setNotEligibleToImport(); |
| 512 | continue; |
| 513 | } |
| 514 | |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 515 | bool AllRefsCanBeExternallyReferenced = |
| 516 | llvm::all_of(Summary->refs(), [&](const ValueInfo &VI) { |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 517 | return !CantBePromoted.count(VI.getGUID()); |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 518 | }); |
| 519 | if (!AllRefsCanBeExternallyReferenced) { |
| 520 | Summary->setNotEligibleToImport(); |
| 521 | continue; |
| 522 | } |
| 523 | |
| 524 | if (auto *FuncSummary = dyn_cast<FunctionSummary>(Summary.get())) { |
| 525 | bool AllCallsCanBeExternallyReferenced = llvm::all_of( |
| 526 | FuncSummary->calls(), [&](const FunctionSummary::EdgeTy &Edge) { |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 527 | return !CantBePromoted.count(Edge.first.getGUID()); |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 528 | }); |
| 529 | if (!AllCallsCanBeExternallyReferenced) |
| 530 | Summary->setNotEligibleToImport(); |
| 531 | } |
| 532 | } |
| 533 | |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 534 | return Index; |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 537 | AnalysisKey ModuleSummaryIndexAnalysis::Key; |
Teresa Johnson | f93b246 | 2016-08-12 13:53:02 +0000 | [diff] [blame] | 538 | |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 539 | ModuleSummaryIndex |
Teresa Johnson | f93b246 | 2016-08-12 13:53:02 +0000 | [diff] [blame] | 540 | ModuleSummaryIndexAnalysis::run(Module &M, ModuleAnalysisManager &AM) { |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 541 | ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M); |
Teresa Johnson | f93b246 | 2016-08-12 13:53:02 +0000 | [diff] [blame] | 542 | auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 543 | return buildModuleSummaryIndex( |
| 544 | M, |
| 545 | [&FAM](const Function &F) { |
| 546 | return &FAM.getResult<BlockFrequencyAnalysis>( |
| 547 | *const_cast<Function *>(&F)); |
| 548 | }, |
| 549 | &PSI); |
Teresa Johnson | f93b246 | 2016-08-12 13:53:02 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 552 | char ModuleSummaryIndexWrapperPass::ID = 0; |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 553 | |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 554 | INITIALIZE_PASS_BEGIN(ModuleSummaryIndexWrapperPass, "module-summary-analysis", |
| 555 | "Module Summary Analysis", false, true) |
| 556 | INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass) |
Mehdi Amini | 8902948 | 2017-01-21 06:01:22 +0000 | [diff] [blame] | 557 | INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass) |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 558 | INITIALIZE_PASS_END(ModuleSummaryIndexWrapperPass, "module-summary-analysis", |
| 559 | "Module Summary Analysis", false, true) |
| 560 | |
| 561 | ModulePass *llvm::createModuleSummaryIndexWrapperPass() { |
| 562 | return new ModuleSummaryIndexWrapperPass(); |
| 563 | } |
| 564 | |
| 565 | ModuleSummaryIndexWrapperPass::ModuleSummaryIndexWrapperPass() |
| 566 | : ModulePass(ID) { |
| 567 | initializeModuleSummaryIndexWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 568 | } |
| 569 | |
| 570 | bool ModuleSummaryIndexWrapperPass::runOnModule(Module &M) { |
Dehao Chen | 5461d8b | 2016-09-28 21:00:58 +0000 | [diff] [blame] | 571 | auto &PSI = *getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 572 | Index = buildModuleSummaryIndex( |
| 573 | M, |
| 574 | [this](const Function &F) { |
| 575 | return &(this->getAnalysis<BlockFrequencyInfoWrapperPass>( |
| 576 | *const_cast<Function *>(&F)) |
| 577 | .getBFI()); |
| 578 | }, |
| 579 | &PSI); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 580 | return false; |
| 581 | } |
| 582 | |
| 583 | bool ModuleSummaryIndexWrapperPass::doFinalization(Module &M) { |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 584 | Index.reset(); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 585 | return false; |
| 586 | } |
| 587 | |
| 588 | void ModuleSummaryIndexWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 589 | AU.setPreservesAll(); |
| 590 | AU.addRequired<BlockFrequencyInfoWrapperPass>(); |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 591 | AU.addRequired<ProfileSummaryInfoWrapperPass>(); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 592 | } |