Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 1 | //===- ModuleSummaryAnalysis.cpp - Module summary index builder -----------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This pass builds a ModuleSummaryIndex object for the module, to be written |
| 10 | // to bitcode or LLVM assembly. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Analysis/ModuleSummaryAnalysis.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/ArrayRef.h" |
| 16 | #include "llvm/ADT/DenseSet.h" |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/MapVector.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SetVector.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallPtrSet.h" |
| 21 | #include "llvm/ADT/SmallVector.h" |
| 22 | #include "llvm/ADT/StringRef.h" |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/BlockFrequencyInfo.h" |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/BranchProbabilityInfo.h" |
Teresa Johnson | cd21a64 | 2016-07-17 14:47:01 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/IndirectCallPromotionAnalysis.h" |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/LoopInfo.h" |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/ProfileSummaryInfo.h" |
Peter Collingbourne | 1b4137a7 | 2016-12-21 23:03:45 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/TypeMetadataUtils.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Attributes.h" |
| 30 | #include "llvm/IR/BasicBlock.h" |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 31 | #include "llvm/IR/CallSite.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Constant.h" |
| 33 | #include "llvm/IR/Constants.h" |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 34 | #include "llvm/IR/Dominators.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Function.h" |
| 36 | #include "llvm/IR/GlobalAlias.h" |
| 37 | #include "llvm/IR/GlobalValue.h" |
| 38 | #include "llvm/IR/GlobalVariable.h" |
| 39 | #include "llvm/IR/Instructions.h" |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 40 | #include "llvm/IR/IntrinsicInst.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 41 | #include "llvm/IR/Intrinsics.h" |
| 42 | #include "llvm/IR/Metadata.h" |
| 43 | #include "llvm/IR/Module.h" |
| 44 | #include "llvm/IR/ModuleSummaryIndex.h" |
| 45 | #include "llvm/IR/Use.h" |
| 46 | #include "llvm/IR/User.h" |
Peter Collingbourne | 61781ac | 2017-03-31 00:08:24 +0000 | [diff] [blame] | 47 | #include "llvm/Object/ModuleSymbolTable.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 48 | #include "llvm/Object/SymbolicFile.h" |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 49 | #include "llvm/Pass.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 50 | #include "llvm/Support/Casting.h" |
Teresa Johnson | db83ace | 2018-03-31 00:18:08 +0000 | [diff] [blame] | 51 | #include "llvm/Support/CommandLine.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 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 | |
Teresa Johnson | db83ace | 2018-03-31 00:18:08 +0000 | [diff] [blame] | 61 | // Option to force edges cold which will block importing when the |
| 62 | // -import-cold-multiplier is set to 0. Useful for debugging. |
| 63 | FunctionSummary::ForceSummaryHotnessType ForceSummaryEdgesCold = |
| 64 | FunctionSummary::FSHT_None; |
| 65 | cl::opt<FunctionSummary::ForceSummaryHotnessType, true> FSEC( |
| 66 | "force-summary-edges-cold", cl::Hidden, cl::location(ForceSummaryEdgesCold), |
| 67 | cl::desc("Force all edges in the function summary to cold"), |
| 68 | cl::values(clEnumValN(FunctionSummary::FSHT_None, "none", "None."), |
| 69 | clEnumValN(FunctionSummary::FSHT_AllNonCritical, |
| 70 | "all-non-critical", "All non-critical edges."), |
| 71 | clEnumValN(FunctionSummary::FSHT_All, "all", "All edges."))); |
| 72 | |
Teresa Johnson | 2f616e4 | 2019-01-28 23:43:26 +0000 | [diff] [blame] | 73 | cl::opt<std::string> ModuleSummaryDotFile( |
| 74 | "module-summary-dot-file", cl::init(""), cl::Hidden, |
| 75 | cl::value_desc("filename"), |
| 76 | cl::desc("File to emit dot graph of new summary into.")); |
| 77 | |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 78 | // Walk through the operands of a given User via worklist iteration and populate |
| 79 | // the set of GlobalValue references encountered. Invoked either on an |
| 80 | // Instruction or a GlobalVariable (which walks its initializer). |
Eugene Leviant | eddf6b5 | 2018-10-12 07:24:02 +0000 | [diff] [blame] | 81 | // Return true if any of the operands contains blockaddress. This is important |
| 82 | // to know when computing summary for global var, because if global variable |
| 83 | // references basic block address we can't import it separately from function |
| 84 | // containing that basic block. For simplicity we currently don't import such |
| 85 | // global vars at all. When importing function we aren't interested if any |
| 86 | // instruction in it takes an address of any basic block, because instruction |
| 87 | // can only take an address of basic block located in the same function. |
| 88 | static bool findRefEdges(ModuleSummaryIndex &Index, const User *CurUser, |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 89 | SetVector<ValueInfo> &RefEdges, |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 90 | SmallPtrSet<const User *, 8> &Visited) { |
Eugene Leviant | eddf6b5 | 2018-10-12 07:24:02 +0000 | [diff] [blame] | 91 | bool HasBlockAddress = false; |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 92 | SmallVector<const User *, 32> Worklist; |
| 93 | Worklist.push_back(CurUser); |
| 94 | |
| 95 | while (!Worklist.empty()) { |
| 96 | const User *U = Worklist.pop_back_val(); |
| 97 | |
| 98 | if (!Visited.insert(U).second) |
| 99 | continue; |
| 100 | |
| 101 | ImmutableCallSite CS(U); |
| 102 | |
| 103 | for (const auto &OI : U->operands()) { |
| 104 | const User *Operand = dyn_cast<User>(OI); |
| 105 | if (!Operand) |
| 106 | continue; |
Eugene Leviant | eddf6b5 | 2018-10-12 07:24:02 +0000 | [diff] [blame] | 107 | if (isa<BlockAddress>(Operand)) { |
| 108 | HasBlockAddress = true; |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 109 | continue; |
Eugene Leviant | eddf6b5 | 2018-10-12 07:24:02 +0000 | [diff] [blame] | 110 | } |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 111 | if (auto *GV = dyn_cast<GlobalValue>(Operand)) { |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 112 | // We have a reference to a global value. This should be added to |
| 113 | // the reference set unless it is a callee. Callees are handled |
| 114 | // specially by WriteFunction and are added to a separate list. |
| 115 | if (!(CS && CS.isCallee(&OI))) |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 116 | RefEdges.insert(Index.getOrInsertValueInfo(GV)); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 117 | continue; |
| 118 | } |
| 119 | Worklist.push_back(Operand); |
| 120 | } |
| 121 | } |
Eugene Leviant | eddf6b5 | 2018-10-12 07:24:02 +0000 | [diff] [blame] | 122 | return HasBlockAddress; |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 125 | static CalleeInfo::HotnessType getHotness(uint64_t ProfileCount, |
| 126 | ProfileSummaryInfo *PSI) { |
| 127 | if (!PSI) |
| 128 | return CalleeInfo::HotnessType::Unknown; |
| 129 | if (PSI->isHotCount(ProfileCount)) |
| 130 | return CalleeInfo::HotnessType::Hot; |
| 131 | if (PSI->isColdCount(ProfileCount)) |
| 132 | return CalleeInfo::HotnessType::Cold; |
| 133 | return CalleeInfo::HotnessType::None; |
| 134 | } |
| 135 | |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 136 | static bool isNonRenamableLocal(const GlobalValue &GV) { |
| 137 | return GV.hasSection() && GV.hasLocalLinkage(); |
| 138 | } |
| 139 | |
Peter Collingbourne | be9ffaa | 2017-02-10 22:29:38 +0000 | [diff] [blame] | 140 | /// Determine whether this call has all constant integer arguments (excluding |
| 141 | /// "this") and summarize it to VCalls or ConstVCalls as appropriate. |
| 142 | static void addVCallToSet(DevirtCallSite Call, GlobalValue::GUID Guid, |
| 143 | SetVector<FunctionSummary::VFuncId> &VCalls, |
| 144 | SetVector<FunctionSummary::ConstVCall> &ConstVCalls) { |
| 145 | std::vector<uint64_t> Args; |
| 146 | // Start from the second argument to skip the "this" pointer. |
| 147 | for (auto &Arg : make_range(Call.CS.arg_begin() + 1, Call.CS.arg_end())) { |
| 148 | auto *CI = dyn_cast<ConstantInt>(Arg); |
| 149 | if (!CI || CI->getBitWidth() > 64) { |
| 150 | VCalls.insert({Guid, Call.Offset}); |
| 151 | return; |
| 152 | } |
| 153 | Args.push_back(CI->getZExtValue()); |
| 154 | } |
| 155 | ConstVCalls.insert({{Guid, Call.Offset}, std::move(Args)}); |
| 156 | } |
| 157 | |
| 158 | /// If this intrinsic call requires that we add information to the function |
| 159 | /// summary, do so via the non-constant reference arguments. |
| 160 | static void addIntrinsicToSummary( |
| 161 | const CallInst *CI, SetVector<GlobalValue::GUID> &TypeTests, |
| 162 | SetVector<FunctionSummary::VFuncId> &TypeTestAssumeVCalls, |
| 163 | SetVector<FunctionSummary::VFuncId> &TypeCheckedLoadVCalls, |
| 164 | SetVector<FunctionSummary::ConstVCall> &TypeTestAssumeConstVCalls, |
Teresa Johnson | f24136f | 2018-09-27 14:55:32 +0000 | [diff] [blame] | 165 | SetVector<FunctionSummary::ConstVCall> &TypeCheckedLoadConstVCalls, |
| 166 | DominatorTree &DT) { |
Peter Collingbourne | be9ffaa | 2017-02-10 22:29:38 +0000 | [diff] [blame] | 167 | switch (CI->getCalledFunction()->getIntrinsicID()) { |
| 168 | case Intrinsic::type_test: { |
| 169 | auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(1)); |
| 170 | auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata()); |
| 171 | if (!TypeId) |
| 172 | break; |
| 173 | GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString()); |
| 174 | |
| 175 | // Produce a summary from type.test intrinsics. We only summarize type.test |
| 176 | // intrinsics that are used other than by an llvm.assume intrinsic. |
| 177 | // Intrinsics that are assumed are relevant only to the devirtualization |
| 178 | // pass, not the type test lowering pass. |
| 179 | bool HasNonAssumeUses = llvm::any_of(CI->uses(), [](const Use &CIU) { |
| 180 | auto *AssumeCI = dyn_cast<CallInst>(CIU.getUser()); |
| 181 | if (!AssumeCI) |
| 182 | return true; |
| 183 | Function *F = AssumeCI->getCalledFunction(); |
| 184 | return !F || F->getIntrinsicID() != Intrinsic::assume; |
| 185 | }); |
| 186 | if (HasNonAssumeUses) |
| 187 | TypeTests.insert(Guid); |
| 188 | |
| 189 | SmallVector<DevirtCallSite, 4> DevirtCalls; |
| 190 | SmallVector<CallInst *, 4> Assumes; |
Teresa Johnson | f24136f | 2018-09-27 14:55:32 +0000 | [diff] [blame] | 191 | findDevirtualizableCallsForTypeTest(DevirtCalls, Assumes, CI, DT); |
Peter Collingbourne | be9ffaa | 2017-02-10 22:29:38 +0000 | [diff] [blame] | 192 | for (auto &Call : DevirtCalls) |
| 193 | addVCallToSet(Call, Guid, TypeTestAssumeVCalls, |
| 194 | TypeTestAssumeConstVCalls); |
| 195 | |
| 196 | break; |
| 197 | } |
| 198 | |
| 199 | case Intrinsic::type_checked_load: { |
| 200 | auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(2)); |
| 201 | auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata()); |
| 202 | if (!TypeId) |
| 203 | break; |
| 204 | GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString()); |
| 205 | |
| 206 | SmallVector<DevirtCallSite, 4> DevirtCalls; |
| 207 | SmallVector<Instruction *, 4> LoadedPtrs; |
| 208 | SmallVector<Instruction *, 4> Preds; |
| 209 | bool HasNonCallUses = false; |
| 210 | findDevirtualizableCallsForTypeCheckedLoad(DevirtCalls, LoadedPtrs, Preds, |
Teresa Johnson | f24136f | 2018-09-27 14:55:32 +0000 | [diff] [blame] | 211 | HasNonCallUses, CI, DT); |
Peter Collingbourne | be9ffaa | 2017-02-10 22:29:38 +0000 | [diff] [blame] | 212 | // Any non-call uses of the result of llvm.type.checked.load will |
| 213 | // prevent us from optimizing away the llvm.type.test. |
| 214 | if (HasNonCallUses) |
| 215 | TypeTests.insert(Guid); |
| 216 | for (auto &Call : DevirtCalls) |
| 217 | addVCallToSet(Call, Guid, TypeCheckedLoadVCalls, |
| 218 | TypeCheckedLoadConstVCalls); |
| 219 | |
| 220 | break; |
| 221 | } |
| 222 | default: |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | |
Eugene Leviant | bf46e74 | 2018-11-16 07:08:00 +0000 | [diff] [blame] | 227 | static bool isNonVolatileLoad(const Instruction *I) { |
| 228 | if (const auto *LI = dyn_cast<LoadInst>(I)) |
| 229 | return !LI->isVolatile(); |
| 230 | |
| 231 | return false; |
| 232 | } |
| 233 | |
Eugene Leviant | 3aef352 | 2019-07-05 15:25:05 +0000 | [diff] [blame] | 234 | static bool isNonVolatileStore(const Instruction *I) { |
| 235 | if (const auto *SI = dyn_cast<StoreInst>(I)) |
| 236 | return !SI->isVolatile(); |
| 237 | |
| 238 | return false; |
| 239 | } |
| 240 | |
Eugene Leviant | bf46e74 | 2018-11-16 07:08:00 +0000 | [diff] [blame] | 241 | static void computeFunctionSummary(ModuleSummaryIndex &Index, const Module &M, |
| 242 | const Function &F, BlockFrequencyInfo *BFI, |
| 243 | ProfileSummaryInfo *PSI, DominatorTree &DT, |
| 244 | bool HasLocalsInUsedOrAsm, |
| 245 | DenseSet<GlobalValue::GUID> &CantBePromoted, |
| 246 | bool IsThinLTO) { |
Teresa Johnson | 02563cd | 2016-10-28 02:39:38 +0000 | [diff] [blame] | 247 | // Summary not currently supported for anonymous functions, they should |
| 248 | // have been named. |
| 249 | assert(F.hasName()); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 250 | |
| 251 | unsigned NumInsts = 0; |
| 252 | // Map from callee ValueId to profile count. Used to accumulate profile |
| 253 | // counts for all static calls to a given callee. |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 254 | MapVector<ValueInfo, CalleeInfo> CallGraphEdges; |
Eugene Leviant | 3aef352 | 2019-07-05 15:25:05 +0000 | [diff] [blame] | 255 | SetVector<ValueInfo> RefEdges, LoadRefEdges, StoreRefEdges; |
Peter Collingbourne | 1b4137a7 | 2016-12-21 23:03:45 +0000 | [diff] [blame] | 256 | SetVector<GlobalValue::GUID> TypeTests; |
Peter Collingbourne | be9ffaa | 2017-02-10 22:29:38 +0000 | [diff] [blame] | 257 | SetVector<FunctionSummary::VFuncId> TypeTestAssumeVCalls, |
| 258 | TypeCheckedLoadVCalls; |
| 259 | SetVector<FunctionSummary::ConstVCall> TypeTestAssumeConstVCalls, |
| 260 | TypeCheckedLoadConstVCalls; |
Teresa Johnson | cd21a64 | 2016-07-17 14:47:01 +0000 | [diff] [blame] | 261 | ICallPromotionAnalysis ICallAnalysis; |
Peter Collingbourne | 681fbb6 | 2017-09-07 05:35:35 +0000 | [diff] [blame] | 262 | SmallPtrSet<const User *, 8> Visited; |
| 263 | |
| 264 | // Add personality function, prefix data and prologue data to function's ref |
| 265 | // list. |
| 266 | findRefEdges(Index, &F, RefEdges, Visited); |
Eugene Leviant | bf46e74 | 2018-11-16 07:08:00 +0000 | [diff] [blame] | 267 | std::vector<const Instruction *> NonVolatileLoads; |
Eugene Leviant | 3aef352 | 2019-07-05 15:25:05 +0000 | [diff] [blame] | 268 | std::vector<const Instruction *> NonVolatileStores; |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 269 | |
Teresa Johnson | d5033a4 | 2016-11-14 16:40:19 +0000 | [diff] [blame] | 270 | bool HasInlineAsmMaybeReferencingInternal = false; |
Benjamin Kramer | aa20915 | 2016-06-26 17:27:42 +0000 | [diff] [blame] | 271 | for (const BasicBlock &BB : F) |
| 272 | for (const Instruction &I : BB) { |
Piotr Padlewski | 442d38c | 2016-08-30 00:46:26 +0000 | [diff] [blame] | 273 | if (isa<DbgInfoIntrinsic>(I)) |
| 274 | continue; |
| 275 | ++NumInsts; |
Eugene Leviant | 3aef352 | 2019-07-05 15:25:05 +0000 | [diff] [blame] | 276 | // Regular LTO module doesn't participate in ThinLTO import, |
| 277 | // so no reference from it can be read/writeonly, since this |
| 278 | // would require importing variable as local copy |
| 279 | if (IsThinLTO) { |
| 280 | if (isNonVolatileLoad(&I)) { |
| 281 | // Postpone processing of non-volatile load instructions |
| 282 | // See comments below |
| 283 | Visited.insert(&I); |
| 284 | NonVolatileLoads.push_back(&I); |
| 285 | continue; |
| 286 | } else if (isNonVolatileStore(&I)) { |
| 287 | Visited.insert(&I); |
| 288 | NonVolatileStores.push_back(&I); |
| 289 | // All references from second operand of store (destination address) |
| 290 | // can be considered write-only if they're not referenced by any |
| 291 | // non-store instruction. References from first operand of store |
| 292 | // (stored value) can't be treated either as read- or as write-only |
| 293 | // so we add them to RefEdges as we do with all other instructions |
| 294 | // except non-volatile load. |
| 295 | Value *Stored = I.getOperand(0); |
| 296 | if (auto *GV = dyn_cast<GlobalValue>(Stored)) |
| 297 | // findRefEdges will try to examine GV operands, so instead |
| 298 | // of calling it we should add GV to RefEdges directly. |
| 299 | RefEdges.insert(Index.getOrInsertValueInfo(GV)); |
| 300 | else if (auto *U = dyn_cast<User>(Stored)) |
| 301 | findRefEdges(Index, U, RefEdges, Visited); |
| 302 | continue; |
| 303 | } |
Eugene Leviant | bf46e74 | 2018-11-16 07:08:00 +0000 | [diff] [blame] | 304 | } |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 305 | findRefEdges(Index, &I, RefEdges, Visited); |
Piotr Padlewski | 442d38c | 2016-08-30 00:46:26 +0000 | [diff] [blame] | 306 | auto CS = ImmutableCallSite(&I); |
| 307 | if (!CS) |
| 308 | continue; |
Teresa Johnson | bf28c8f | 2016-10-30 05:40:44 +0000 | [diff] [blame] | 309 | |
| 310 | const auto *CI = dyn_cast<CallInst>(&I); |
| 311 | // 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] | 312 | // assembly, conservatively mark the function as possibly referencing |
| 313 | // a local value from inline assembly to ensure we don't export a |
| 314 | // reference (which would require renaming and promotion of the |
| 315 | // referenced value). |
Peter Collingbourne | 5e8b94c1 | 2017-09-01 16:24:02 +0000 | [diff] [blame] | 316 | if (HasLocalsInUsedOrAsm && CI && CI->isInlineAsm()) |
Teresa Johnson | d5033a4 | 2016-11-14 16:40:19 +0000 | [diff] [blame] | 317 | HasInlineAsmMaybeReferencingInternal = true; |
Teresa Johnson | bf28c8f | 2016-10-30 05:40:44 +0000 | [diff] [blame] | 318 | |
Teresa Johnson | 897bab9 | 2016-10-08 16:11:42 +0000 | [diff] [blame] | 319 | auto *CalledValue = CS.getCalledValue(); |
Piotr Padlewski | 442d38c | 2016-08-30 00:46:26 +0000 | [diff] [blame] | 320 | auto *CalledFunction = CS.getCalledFunction(); |
Volodymyr Sapsai | a739602 | 2017-11-10 00:47:47 +0000 | [diff] [blame] | 321 | if (CalledValue && !CalledFunction) { |
Peter Collingbourne | 2452d70 | 2019-08-22 19:56:14 +0000 | [diff] [blame] | 322 | CalledValue = CalledValue->stripPointerCasts(); |
Volodymyr Sapsai | a739602 | 2017-11-10 00:47:47 +0000 | [diff] [blame] | 323 | // Stripping pointer casts can reveal a called function. |
| 324 | CalledFunction = dyn_cast<Function>(CalledValue); |
| 325 | } |
Teresa Johnson | 897bab9 | 2016-10-08 16:11:42 +0000 | [diff] [blame] | 326 | // Check if this is an alias to a function. If so, get the |
| 327 | // called aliasee for the checks below. |
| 328 | if (auto *GA = dyn_cast<GlobalAlias>(CalledValue)) { |
| 329 | assert(!CalledFunction && "Expected null called function in callsite for alias"); |
| 330 | CalledFunction = dyn_cast<Function>(GA->getBaseObject()); |
| 331 | } |
Peter Collingbourne | 1b4137a7 | 2016-12-21 23:03:45 +0000 | [diff] [blame] | 332 | // Check if this is a direct call to a known function or a known |
| 333 | // intrinsic, or an indirect call with profile data. |
Piotr Padlewski | 442d38c | 2016-08-30 00:46:26 +0000 | [diff] [blame] | 334 | if (CalledFunction) { |
Peter Collingbourne | be9ffaa | 2017-02-10 22:29:38 +0000 | [diff] [blame] | 335 | if (CI && CalledFunction->isIntrinsic()) { |
| 336 | addIntrinsicToSummary( |
| 337 | CI, TypeTests, TypeTestAssumeVCalls, TypeCheckedLoadVCalls, |
Teresa Johnson | f24136f | 2018-09-27 14:55:32 +0000 | [diff] [blame] | 338 | TypeTestAssumeConstVCalls, TypeCheckedLoadConstVCalls, DT); |
Peter Collingbourne | be9ffaa | 2017-02-10 22:29:38 +0000 | [diff] [blame] | 339 | continue; |
Peter Collingbourne | 1b4137a7 | 2016-12-21 23:03:45 +0000 | [diff] [blame] | 340 | } |
Teresa Johnson | 02563cd | 2016-10-28 02:39:38 +0000 | [diff] [blame] | 341 | // We should have named any anonymous globals |
| 342 | assert(CalledFunction->hasName()); |
Easwaran Raman | f5f9160 | 2017-05-09 23:21:10 +0000 | [diff] [blame] | 343 | auto ScaledCount = PSI->getProfileCount(&I, BFI); |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 344 | auto Hotness = ScaledCount ? getHotness(ScaledCount.getValue(), PSI) |
| 345 | : CalleeInfo::HotnessType::Unknown; |
Teresa Johnson | db83ace | 2018-03-31 00:18:08 +0000 | [diff] [blame] | 346 | if (ForceSummaryEdgesCold != FunctionSummary::FSHT_None) |
| 347 | Hotness = CalleeInfo::HotnessType::Cold; |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 348 | |
Teresa Johnson | 897bab9 | 2016-10-08 16:11:42 +0000 | [diff] [blame] | 349 | // Use the original CalledValue, in case it was an alias. We want |
| 350 | // to record the call edge to the alias in that case. Eventually |
| 351 | // an alias summary will be created to associate the alias and |
| 352 | // aliasee. |
Easwaran Raman | c73cec8 | 2018-01-25 19:27:17 +0000 | [diff] [blame] | 353 | auto &ValueInfo = CallGraphEdges[Index.getOrInsertValueInfo( |
| 354 | cast<GlobalValue>(CalledValue))]; |
| 355 | ValueInfo.updateHotness(Hotness); |
| 356 | // Add the relative block frequency to CalleeInfo if there is no profile |
| 357 | // information. |
| 358 | if (BFI != nullptr && Hotness == CalleeInfo::HotnessType::Unknown) { |
Easwaran Raman | 385d8ea | 2018-02-22 19:44:08 +0000 | [diff] [blame] | 359 | uint64_t BBFreq = BFI->getBlockFreq(&BB).getFrequency(); |
| 360 | uint64_t EntryFreq = BFI->getEntryFreq(); |
| 361 | ValueInfo.updateRelBlockFreq(BBFreq, EntryFreq); |
Easwaran Raman | c73cec8 | 2018-01-25 19:27:17 +0000 | [diff] [blame] | 362 | } |
Piotr Padlewski | 442d38c | 2016-08-30 00:46:26 +0000 | [diff] [blame] | 363 | } else { |
Piotr Padlewski | 442d38c | 2016-08-30 00:46:26 +0000 | [diff] [blame] | 364 | // Skip inline assembly calls. |
| 365 | if (CI && CI->isInlineAsm()) |
| 366 | continue; |
Volodymyr Sapsai | 8b46ff1 | 2017-11-17 18:28:05 +0000 | [diff] [blame] | 367 | // Skip direct calls. |
| 368 | if (!CalledValue || isa<Constant>(CalledValue)) |
| 369 | continue; |
Piotr Padlewski | 442d38c | 2016-08-30 00:46:26 +0000 | [diff] [blame] | 370 | |
Taewook Oh | 7646e77 | 2018-03-13 04:26:58 +0000 | [diff] [blame] | 371 | // Check if the instruction has a callees metadata. If so, add callees |
| 372 | // to CallGraphEdges to reflect the references from the metadata, and |
| 373 | // to enable importing for subsequent indirect call promotion and |
| 374 | // inlining. |
| 375 | if (auto *MD = I.getMetadata(LLVMContext::MD_callees)) { |
| 376 | for (auto &Op : MD->operands()) { |
| 377 | Function *Callee = mdconst::extract_or_null<Function>(Op); |
| 378 | if (Callee) |
| 379 | CallGraphEdges[Index.getOrInsertValueInfo(Callee)]; |
| 380 | } |
| 381 | } |
| 382 | |
Piotr Padlewski | 442d38c | 2016-08-30 00:46:26 +0000 | [diff] [blame] | 383 | uint32_t NumVals, NumCandidates; |
| 384 | uint64_t TotalCount; |
| 385 | auto CandidateProfileData = |
| 386 | ICallAnalysis.getPromotionCandidatesForInstruction( |
| 387 | &I, NumVals, TotalCount, NumCandidates); |
| 388 | for (auto &Candidate : CandidateProfileData) |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 389 | CallGraphEdges[Index.getOrInsertValueInfo(Candidate.Value)] |
| 390 | .updateHotness(getHotness(Candidate.Count, PSI)); |
Piotr Padlewski | 442d38c | 2016-08-30 00:46:26 +0000 | [diff] [blame] | 391 | } |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Eugene Leviant | 3aef352 | 2019-07-05 15:25:05 +0000 | [diff] [blame] | 394 | std::vector<ValueInfo> Refs; |
| 395 | if (IsThinLTO) { |
| 396 | auto AddRefEdges = [&](const std::vector<const Instruction *> &Instrs, |
| 397 | SetVector<ValueInfo> &Edges, |
| 398 | SmallPtrSet<const User *, 8> &Cache) { |
| 399 | for (const auto *I : Instrs) { |
| 400 | Cache.erase(I); |
| 401 | findRefEdges(Index, I, Edges, Cache); |
| 402 | } |
| 403 | }; |
| 404 | |
| 405 | // By now we processed all instructions in a function, except |
| 406 | // non-volatile loads and non-volatile value stores. Let's find |
| 407 | // ref edges for both of instruction sets |
| 408 | AddRefEdges(NonVolatileLoads, LoadRefEdges, Visited); |
| 409 | // We can add some values to the Visited set when processing load |
| 410 | // instructions which are also used by stores in NonVolatileStores. |
| 411 | // For example this can happen if we have following code: |
| 412 | // |
| 413 | // store %Derived* @foo, %Derived** bitcast (%Base** @bar to %Derived**) |
| 414 | // %42 = load %Derived*, %Derived** bitcast (%Base** @bar to %Derived**) |
| 415 | // |
| 416 | // After processing loads we'll add bitcast to the Visited set, and if |
| 417 | // we use the same set while processing stores, we'll never see store |
| 418 | // to @bar and @bar will be mistakenly treated as readonly. |
| 419 | SmallPtrSet<const llvm::User *, 8> StoreCache; |
| 420 | AddRefEdges(NonVolatileStores, StoreRefEdges, StoreCache); |
| 421 | |
| 422 | // If both load and store instruction reference the same variable |
| 423 | // we won't be able to optimize it. Add all such reference edges |
| 424 | // to RefEdges set. |
| 425 | for (auto &VI : StoreRefEdges) |
| 426 | if (LoadRefEdges.remove(VI)) |
| 427 | RefEdges.insert(VI); |
| 428 | |
| 429 | unsigned RefCnt = RefEdges.size(); |
| 430 | // All new reference edges inserted in two loops below are either |
| 431 | // read or write only. They will be grouped in the end of RefEdges |
| 432 | // vector, so we can use a single integer value to identify them. |
| 433 | for (auto &VI : LoadRefEdges) |
| 434 | RefEdges.insert(VI); |
| 435 | |
| 436 | unsigned FirstWORef = RefEdges.size(); |
| 437 | for (auto &VI : StoreRefEdges) |
| 438 | RefEdges.insert(VI); |
| 439 | |
| 440 | Refs = RefEdges.takeVector(); |
| 441 | for (; RefCnt < FirstWORef; ++RefCnt) |
Eugene Leviant | bf46e74 | 2018-11-16 07:08:00 +0000 | [diff] [blame] | 442 | Refs[RefCnt].setReadOnly(); |
| 443 | |
Eugene Leviant | 3aef352 | 2019-07-05 15:25:05 +0000 | [diff] [blame] | 444 | for (; RefCnt < Refs.size(); ++RefCnt) |
| 445 | Refs[RefCnt].setWriteOnly(); |
| 446 | } else { |
| 447 | Refs = RefEdges.takeVector(); |
| 448 | } |
Dehao Chen | a60cdd3 | 2017-02-28 18:09:44 +0000 | [diff] [blame] | 449 | // Explicit add hot edges to enforce importing for designated GUIDs for |
| 450 | // sample PGO, to enable the same inlines as the profiled optimized binary. |
| 451 | for (auto &I : F.getImportGUIDs()) |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 452 | CallGraphEdges[Index.getOrInsertValueInfo(I)].updateHotness( |
Teresa Johnson | db83ace | 2018-03-31 00:18:08 +0000 | [diff] [blame] | 453 | ForceSummaryEdgesCold == FunctionSummary::FSHT_All |
| 454 | ? CalleeInfo::HotnessType::Cold |
| 455 | : CalleeInfo::HotnessType::Critical); |
Dehao Chen | a60cdd3 | 2017-02-28 18:09:44 +0000 | [diff] [blame] | 456 | |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 457 | bool NonRenamableLocal = isNonRenamableLocal(F); |
| 458 | bool NotEligibleForImport = |
Teresa Johnson | cb39746 | 2018-11-06 19:41:35 +0000 | [diff] [blame] | 459 | NonRenamableLocal || HasInlineAsmMaybeReferencingInternal; |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 460 | GlobalValueSummary::GVFlags Flags(F.getLinkage(), NotEligibleForImport, |
Teresa Johnson | 37b8012 | 2019-05-10 20:08:24 +0000 | [diff] [blame] | 461 | /* Live = */ false, F.isDSOLocal(), |
| 462 | F.hasLinkOnceODRLinkage() && F.hasGlobalUnnamedAddr()); |
Charles Saternos | 75da10d | 2017-08-04 16:00:58 +0000 | [diff] [blame] | 463 | FunctionSummary::FFlags FunFlags{ |
| 464 | F.hasFnAttribute(Attribute::ReadNone), |
| 465 | F.hasFnAttribute(Attribute::ReadOnly), |
Teresa Johnson | cb39746 | 2018-11-06 19:41:35 +0000 | [diff] [blame] | 466 | F.hasFnAttribute(Attribute::NoRecurse), F.returnDoesNotAlias(), |
Teresa Johnson | cb39746 | 2018-11-06 19:41:35 +0000 | [diff] [blame] | 467 | // FIXME: refactor this to use the same code that inliner is using. |
Teresa Johnson | 5b8ff37 | 2018-12-01 05:11:46 +0000 | [diff] [blame] | 468 | // Don't try to import functions with noinline attribute. |
Teresa Johnson | b11391b | 2019-11-08 15:50:55 -0800 | [diff] [blame] | 469 | F.getAttributes().hasFnAttribute(Attribute::NoInline), |
| 470 | F.hasFnAttribute(Attribute::AlwaysInline)}; |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 471 | auto FuncSummary = std::make_unique<FunctionSummary>( |
Easwaran Raman | 5a7056f | 2018-12-13 19:54:27 +0000 | [diff] [blame] | 472 | Flags, NumInsts, FunFlags, /*EntryCount=*/0, std::move(Refs), |
| 473 | CallGraphEdges.takeVector(), TypeTests.takeVector(), |
| 474 | TypeTestAssumeVCalls.takeVector(), TypeCheckedLoadVCalls.takeVector(), |
Peter Collingbourne | be9ffaa | 2017-02-10 22:29:38 +0000 | [diff] [blame] | 475 | TypeTestAssumeConstVCalls.takeVector(), |
| 476 | TypeCheckedLoadConstVCalls.takeVector()); |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 477 | if (NonRenamableLocal) |
| 478 | CantBePromoted.insert(F.getGUID()); |
Teresa Johnson | 7bea1aa | 2018-06-26 00:20:49 +0000 | [diff] [blame] | 479 | Index.addGlobalValueSummary(F, std::move(FuncSummary)); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Teresa Johnson | a700436 | 2019-07-02 19:38:02 +0000 | [diff] [blame] | 482 | /// Find function pointers referenced within the given vtable initializer |
| 483 | /// (or subset of an initializer) \p I. The starting offset of \p I within |
| 484 | /// the vtable initializer is \p StartingOffset. Any discovered function |
| 485 | /// pointers are added to \p VTableFuncs along with their cumulative offset |
| 486 | /// within the initializer. |
| 487 | static void findFuncPointers(const Constant *I, uint64_t StartingOffset, |
| 488 | const Module &M, ModuleSummaryIndex &Index, |
| 489 | VTableFuncList &VTableFuncs) { |
| 490 | // First check if this is a function pointer. |
| 491 | if (I->getType()->isPointerTy()) { |
| 492 | auto Fn = dyn_cast<Function>(I->stripPointerCasts()); |
| 493 | // We can disregard __cxa_pure_virtual as a possible call target, as |
| 494 | // calls to pure virtuals are UB. |
| 495 | if (Fn && Fn->getName() != "__cxa_pure_virtual") |
| 496 | VTableFuncs.push_back({Index.getOrInsertValueInfo(Fn), StartingOffset}); |
| 497 | return; |
| 498 | } |
| 499 | |
| 500 | // Walk through the elements in the constant struct or array and recursively |
| 501 | // look for virtual function pointers. |
| 502 | const DataLayout &DL = M.getDataLayout(); |
| 503 | if (auto *C = dyn_cast<ConstantStruct>(I)) { |
| 504 | StructType *STy = dyn_cast<StructType>(C->getType()); |
| 505 | assert(STy); |
| 506 | const StructLayout *SL = DL.getStructLayout(C->getType()); |
| 507 | |
| 508 | for (StructType::element_iterator EB = STy->element_begin(), EI = EB, |
| 509 | EE = STy->element_end(); |
| 510 | EI != EE; ++EI) { |
| 511 | auto Offset = SL->getElementOffset(EI - EB); |
| 512 | unsigned Op = SL->getElementContainingOffset(Offset); |
| 513 | findFuncPointers(cast<Constant>(I->getOperand(Op)), |
| 514 | StartingOffset + Offset, M, Index, VTableFuncs); |
| 515 | } |
| 516 | } else if (auto *C = dyn_cast<ConstantArray>(I)) { |
| 517 | ArrayType *ATy = C->getType(); |
| 518 | Type *EltTy = ATy->getElementType(); |
| 519 | uint64_t EltSize = DL.getTypeAllocSize(EltTy); |
| 520 | for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) { |
| 521 | findFuncPointers(cast<Constant>(I->getOperand(i)), |
| 522 | StartingOffset + i * EltSize, M, Index, VTableFuncs); |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | // Identify the function pointers referenced by vtable definition \p V. |
| 528 | static void computeVTableFuncs(ModuleSummaryIndex &Index, |
| 529 | const GlobalVariable &V, const Module &M, |
| 530 | VTableFuncList &VTableFuncs) { |
| 531 | if (!V.isConstant()) |
| 532 | return; |
| 533 | |
| 534 | findFuncPointers(V.getInitializer(), /*StartingOffset=*/0, M, Index, |
| 535 | VTableFuncs); |
| 536 | |
| 537 | #ifndef NDEBUG |
| 538 | // Validate that the VTableFuncs list is ordered by offset. |
| 539 | uint64_t PrevOffset = 0; |
| 540 | for (auto &P : VTableFuncs) { |
| 541 | // The findVFuncPointers traversal should have encountered the |
| 542 | // functions in offset order. We need to use ">=" since PrevOffset |
| 543 | // starts at 0. |
| 544 | assert(P.VTableOffset >= PrevOffset); |
| 545 | PrevOffset = P.VTableOffset; |
| 546 | } |
| 547 | #endif |
| 548 | } |
| 549 | |
| 550 | /// Record vtable definition \p V for each type metadata it references. |
Teresa Johnson | 8d86f1b | 2019-01-17 16:05:04 +0000 | [diff] [blame] | 551 | static void |
Teresa Johnson | a700436 | 2019-07-02 19:38:02 +0000 | [diff] [blame] | 552 | recordTypeIdCompatibleVtableReferences(ModuleSummaryIndex &Index, |
| 553 | const GlobalVariable &V, |
| 554 | SmallVectorImpl<MDNode *> &Types) { |
| 555 | for (MDNode *Type : Types) { |
| 556 | auto TypeID = Type->getOperand(1).get(); |
| 557 | |
| 558 | uint64_t Offset = |
| 559 | cast<ConstantInt>( |
| 560 | cast<ConstantAsMetadata>(Type->getOperand(0))->getValue()) |
| 561 | ->getZExtValue(); |
| 562 | |
| 563 | if (auto *TypeId = dyn_cast<MDString>(TypeID)) |
| 564 | Index.getOrInsertTypeIdCompatibleVtableSummary(TypeId->getString()) |
| 565 | .push_back({Offset, Index.getOrInsertValueInfo(&V)}); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | static void computeVariableSummary(ModuleSummaryIndex &Index, |
| 570 | const GlobalVariable &V, |
| 571 | DenseSet<GlobalValue::GUID> &CantBePromoted, |
| 572 | const Module &M, |
| 573 | SmallVectorImpl<MDNode *> &Types) { |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 574 | SetVector<ValueInfo> RefEdges; |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 575 | SmallPtrSet<const User *, 8> Visited; |
Eugene Leviant | eddf6b5 | 2018-10-12 07:24:02 +0000 | [diff] [blame] | 576 | bool HasBlockAddress = findRefEdges(Index, &V, RefEdges, Visited); |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 577 | bool NonRenamableLocal = isNonRenamableLocal(V); |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 578 | GlobalValueSummary::GVFlags Flags(V.getLinkage(), NonRenamableLocal, |
Teresa Johnson | 37b8012 | 2019-05-10 20:08:24 +0000 | [diff] [blame] | 579 | /* Live = */ false, V.isDSOLocal(), |
| 580 | V.hasLinkOnceODRLinkage() && V.hasGlobalUnnamedAddr()); |
Eugene Leviant | bf46e74 | 2018-11-16 07:08:00 +0000 | [diff] [blame] | 581 | |
Teresa Johnson | a700436 | 2019-07-02 19:38:02 +0000 | [diff] [blame] | 582 | VTableFuncList VTableFuncs; |
| 583 | // If splitting is not enabled, then we compute the summary information |
| 584 | // necessary for index-based whole program devirtualization. |
| 585 | if (!Index.enableSplitLTOUnit()) { |
| 586 | Types.clear(); |
| 587 | V.getMetadata(LLVMContext::MD_type, Types); |
| 588 | if (!Types.empty()) { |
| 589 | // Identify the function pointers referenced by this vtable definition. |
| 590 | computeVTableFuncs(Index, V, M, VTableFuncs); |
| 591 | |
| 592 | // Record this vtable definition for each type metadata it references. |
| 593 | recordTypeIdCompatibleVtableReferences(Index, V, Types); |
| 594 | } |
| 595 | } |
| 596 | |
Eugene Leviant | 3aef352 | 2019-07-05 15:25:05 +0000 | [diff] [blame] | 597 | // Don't mark variables we won't be able to internalize as read/write-only. |
| 598 | bool CanBeInternalized = |
Eugene Leviant | bf46e74 | 2018-11-16 07:08:00 +0000 | [diff] [blame] | 599 | !V.hasComdat() && !V.hasAppendingLinkage() && !V.isInterposable() && |
Eugene Leviant | 3aef352 | 2019-07-05 15:25:05 +0000 | [diff] [blame] | 600 | !V.hasAvailableExternallyLinkage() && !V.hasDLLExportStorageClass(); |
| 601 | GlobalVarSummary::GVarFlags VarFlags(CanBeInternalized, CanBeInternalized); |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 602 | auto GVarSummary = std::make_unique<GlobalVarSummary>(Flags, VarFlags, |
Eugene Leviant | bf46e74 | 2018-11-16 07:08:00 +0000 | [diff] [blame] | 603 | RefEdges.takeVector()); |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 604 | if (NonRenamableLocal) |
| 605 | CantBePromoted.insert(V.getGUID()); |
Eugene Leviant | eddf6b5 | 2018-10-12 07:24:02 +0000 | [diff] [blame] | 606 | if (HasBlockAddress) |
| 607 | GVarSummary->setNotEligibleToImport(); |
Teresa Johnson | a700436 | 2019-07-02 19:38:02 +0000 | [diff] [blame] | 608 | if (!VTableFuncs.empty()) |
| 609 | GVarSummary->setVTableFuncs(VTableFuncs); |
Teresa Johnson | 7bea1aa | 2018-06-26 00:20:49 +0000 | [diff] [blame] | 610 | Index.addGlobalValueSummary(V, std::move(GVarSummary)); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 613 | static void |
| 614 | computeAliasSummary(ModuleSummaryIndex &Index, const GlobalAlias &A, |
Teresa Johnson | e27b058 | 2017-01-05 14:59:56 +0000 | [diff] [blame] | 615 | DenseSet<GlobalValue::GUID> &CantBePromoted) { |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 616 | bool NonRenamableLocal = isNonRenamableLocal(A); |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 617 | GlobalValueSummary::GVFlags Flags(A.getLinkage(), NonRenamableLocal, |
Teresa Johnson | 37b8012 | 2019-05-10 20:08:24 +0000 | [diff] [blame] | 618 | /* Live = */ false, A.isDSOLocal(), |
| 619 | A.hasLinkOnceODRLinkage() && A.hasGlobalUnnamedAddr()); |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 620 | auto AS = std::make_unique<AliasSummary>(Flags); |
Teresa Johnson | 02563cd | 2016-10-28 02:39:38 +0000 | [diff] [blame] | 621 | auto *Aliasee = A.getBaseObject(); |
Teresa Johnson | 70ec64c | 2019-03-15 15:11:38 +0000 | [diff] [blame] | 622 | auto AliaseeVI = Index.getValueInfo(Aliasee->getGUID()); |
| 623 | assert(AliaseeVI && "Alias expects aliasee summary to be available"); |
| 624 | assert(AliaseeVI.getSummaryList().size() == 1 && |
| 625 | "Expected a single entry per aliasee in per-module index"); |
| 626 | AS->setAliasee(AliaseeVI, AliaseeVI.getSummaryList()[0].get()); |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 627 | if (NonRenamableLocal) |
| 628 | CantBePromoted.insert(A.getGUID()); |
Teresa Johnson | 7bea1aa | 2018-06-26 00:20:49 +0000 | [diff] [blame] | 629 | Index.addGlobalValueSummary(A, std::move(AS)); |
Teresa Johnson | 02563cd | 2016-10-28 02:39:38 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 632 | // Set LiveRoot flag on entries matching the given value name. |
| 633 | static void setLiveRoot(ModuleSummaryIndex &Index, StringRef Name) { |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 634 | if (ValueInfo VI = Index.getValueInfo(GlobalValue::getGUID(Name))) |
| 635 | for (auto &Summary : VI.getSummaryList()) |
Evgeniy Stepanov | 56584bb | 2017-06-01 20:30:06 +0000 | [diff] [blame] | 636 | Summary->setLive(true); |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 639 | ModuleSummaryIndex llvm::buildModuleSummaryIndex( |
| 640 | const Module &M, |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 641 | std::function<BlockFrequencyInfo *(const Function &F)> GetBFICallback, |
| 642 | ProfileSummaryInfo *PSI) { |
Teresa Johnson | 94624ac | 2017-05-10 18:52:16 +0000 | [diff] [blame] | 643 | assert(PSI); |
Teresa Johnson | 290a839 | 2019-01-11 18:31:57 +0000 | [diff] [blame] | 644 | bool EnableSplitLTOUnit = false; |
| 645 | if (auto *MD = mdconst::extract_or_null<ConstantInt>( |
| 646 | M.getModuleFlag("EnableSplitLTOUnit"))) |
| 647 | EnableSplitLTOUnit = MD->getZExtValue(); |
| 648 | ModuleSummaryIndex Index(/*HaveGVs=*/true, EnableSplitLTOUnit); |
Teresa Johnson | bf28c8f | 2016-10-30 05:40:44 +0000 | [diff] [blame] | 649 | |
Teresa Johnson | a081145 | 2016-11-10 16:57:32 +0000 | [diff] [blame] | 650 | // Identify the local values in the llvm.used and llvm.compiler.used sets, |
| 651 | // which should not be exported as they would then require renaming and |
| 652 | // promotion, but we may have opaque uses e.g. in inline asm. We collect them |
| 653 | // here because we use this information to mark functions containing inline |
| 654 | // assembly calls as not importable. |
Mehdi Amini | b6a11a7 | 2016-11-09 01:45:13 +0000 | [diff] [blame] | 655 | SmallPtrSet<GlobalValue *, 8> LocalsUsed; |
Teresa Johnson | a081145 | 2016-11-10 16:57:32 +0000 | [diff] [blame] | 656 | SmallPtrSet<GlobalValue *, 8> Used; |
| 657 | // First collect those in the llvm.used set. |
| 658 | collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false); |
Teresa Johnson | a081145 | 2016-11-10 16:57:32 +0000 | [diff] [blame] | 659 | // Next collect those in the llvm.compiler.used set. |
| 660 | collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ true); |
Teresa Johnson | e27b058 | 2017-01-05 14:59:56 +0000 | [diff] [blame] | 661 | DenseSet<GlobalValue::GUID> CantBePromoted; |
Teresa Johnson | bf28c8f | 2016-10-30 05:40:44 +0000 | [diff] [blame] | 662 | for (auto *V : Used) { |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 663 | if (V->hasLocalLinkage()) { |
Teresa Johnson | bf28c8f | 2016-10-30 05:40:44 +0000 | [diff] [blame] | 664 | LocalsUsed.insert(V); |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 665 | CantBePromoted.insert(V->getGUID()); |
| 666 | } |
Teresa Johnson | bf28c8f | 2016-10-30 05:40:44 +0000 | [diff] [blame] | 667 | } |
Teresa Johnson | b35cc69 | 2016-04-20 14:39:45 +0000 | [diff] [blame] | 668 | |
Peter Collingbourne | 5e8b94c1 | 2017-09-01 16:24:02 +0000 | [diff] [blame] | 669 | bool HasLocalInlineAsmSymbol = false; |
| 670 | if (!M.getModuleInlineAsm().empty()) { |
| 671 | // Collect the local values defined by module level asm, and set up |
| 672 | // summaries for these symbols so that they can be marked as NoRename, |
| 673 | // to prevent export of any use of them in regular IR that would require |
| 674 | // renaming within the module level asm. Note we don't need to create a |
| 675 | // summary for weak or global defs, as they don't need to be flagged as |
| 676 | // NoRename, and defs in module level asm can't be imported anyway. |
| 677 | // Also, any values used but not defined within module level asm should |
| 678 | // be listed on the llvm.used or llvm.compiler.used global and marked as |
| 679 | // referenced from there. |
| 680 | ModuleSymbolTable::CollectAsmSymbols( |
| 681 | M, [&](StringRef Name, object::BasicSymbolRef::Flags Flags) { |
| 682 | // Symbols not marked as Weak or Global are local definitions. |
| 683 | if (Flags & (object::BasicSymbolRef::SF_Weak | |
| 684 | object::BasicSymbolRef::SF_Global)) |
| 685 | return; |
| 686 | HasLocalInlineAsmSymbol = true; |
| 687 | GlobalValue *GV = M.getNamedValue(Name); |
| 688 | if (!GV) |
| 689 | return; |
| 690 | assert(GV->isDeclaration() && "Def in module asm already has definition"); |
| 691 | GlobalValueSummary::GVFlags GVFlags(GlobalValue::InternalLinkage, |
| 692 | /* NotEligibleToImport = */ true, |
Sean Fertile | 4595a91 | 2017-11-04 17:04:39 +0000 | [diff] [blame] | 693 | /* Live = */ true, |
Teresa Johnson | 37b8012 | 2019-05-10 20:08:24 +0000 | [diff] [blame] | 694 | /* Local */ GV->isDSOLocal(), |
| 695 | GV->hasLinkOnceODRLinkage() && GV->hasGlobalUnnamedAddr()); |
Teresa Johnson | 7bea1aa | 2018-06-26 00:20:49 +0000 | [diff] [blame] | 696 | CantBePromoted.insert(GV->getGUID()); |
Peter Collingbourne | 5e8b94c1 | 2017-09-01 16:24:02 +0000 | [diff] [blame] | 697 | // Create the appropriate summary type. |
| 698 | if (Function *F = dyn_cast<Function>(GV)) { |
| 699 | std::unique_ptr<FunctionSummary> Summary = |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 700 | std::make_unique<FunctionSummary>( |
Easwaran Raman | 5a7056f | 2018-12-13 19:54:27 +0000 | [diff] [blame] | 701 | GVFlags, /*InstCount=*/0, |
Peter Collingbourne | 5e8b94c1 | 2017-09-01 16:24:02 +0000 | [diff] [blame] | 702 | FunctionSummary::FFlags{ |
| 703 | F->hasFnAttribute(Attribute::ReadNone), |
| 704 | F->hasFnAttribute(Attribute::ReadOnly), |
| 705 | F->hasFnAttribute(Attribute::NoRecurse), |
Teresa Johnson | cb39746 | 2018-11-06 19:41:35 +0000 | [diff] [blame] | 706 | F->returnDoesNotAlias(), |
Teresa Johnson | b11391b | 2019-11-08 15:50:55 -0800 | [diff] [blame] | 707 | /* NoInline = */ false, |
| 708 | F->hasFnAttribute(Attribute::AlwaysInline)}, |
Easwaran Raman | 5a7056f | 2018-12-13 19:54:27 +0000 | [diff] [blame] | 709 | /*EntryCount=*/0, ArrayRef<ValueInfo>{}, |
| 710 | ArrayRef<FunctionSummary::EdgeTy>{}, |
Peter Collingbourne | 5e8b94c1 | 2017-09-01 16:24:02 +0000 | [diff] [blame] | 711 | ArrayRef<GlobalValue::GUID>{}, |
| 712 | ArrayRef<FunctionSummary::VFuncId>{}, |
| 713 | ArrayRef<FunctionSummary::VFuncId>{}, |
| 714 | ArrayRef<FunctionSummary::ConstVCall>{}, |
| 715 | ArrayRef<FunctionSummary::ConstVCall>{}); |
Teresa Johnson | 7bea1aa | 2018-06-26 00:20:49 +0000 | [diff] [blame] | 716 | Index.addGlobalValueSummary(*GV, std::move(Summary)); |
Peter Collingbourne | 5e8b94c1 | 2017-09-01 16:24:02 +0000 | [diff] [blame] | 717 | } else { |
| 718 | std::unique_ptr<GlobalVarSummary> Summary = |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 719 | std::make_unique<GlobalVarSummary>( |
Eugene Leviant | 3aef352 | 2019-07-05 15:25:05 +0000 | [diff] [blame] | 720 | GVFlags, GlobalVarSummary::GVarFlags(false, false), |
Eugene Leviant | bf46e74 | 2018-11-16 07:08:00 +0000 | [diff] [blame] | 721 | ArrayRef<ValueInfo>{}); |
Teresa Johnson | 7bea1aa | 2018-06-26 00:20:49 +0000 | [diff] [blame] | 722 | Index.addGlobalValueSummary(*GV, std::move(Summary)); |
Peter Collingbourne | 5e8b94c1 | 2017-09-01 16:24:02 +0000 | [diff] [blame] | 723 | } |
| 724 | }); |
| 725 | } |
| 726 | |
Eugene Leviant | bf46e74 | 2018-11-16 07:08:00 +0000 | [diff] [blame] | 727 | bool IsThinLTO = true; |
| 728 | if (auto *MD = |
| 729 | mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("ThinLTO"))) |
| 730 | IsThinLTO = MD->getZExtValue(); |
| 731 | |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 732 | // Compute summaries for all functions defined in module, and save in the |
| 733 | // index. |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 734 | for (auto &F : M) { |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 735 | if (F.isDeclaration()) |
| 736 | continue; |
| 737 | |
Teresa Johnson | f24136f | 2018-09-27 14:55:32 +0000 | [diff] [blame] | 738 | DominatorTree DT(const_cast<Function &>(F)); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 739 | BlockFrequencyInfo *BFI = nullptr; |
| 740 | std::unique_ptr<BlockFrequencyInfo> BFIPtr; |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 741 | if (GetBFICallback) |
| 742 | BFI = GetBFICallback(F); |
Easwaran Raman | a17f220 | 2017-12-22 01:33:52 +0000 | [diff] [blame] | 743 | else if (F.hasProfileData()) { |
Teresa Johnson | f24136f | 2018-09-27 14:55:32 +0000 | [diff] [blame] | 744 | LoopInfo LI{DT}; |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 745 | BranchProbabilityInfo BPI{F, LI}; |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 746 | BFIPtr = std::make_unique<BlockFrequencyInfo>(F, BPI, LI); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 747 | BFI = BFIPtr.get(); |
| 748 | } |
| 749 | |
Teresa Johnson | f24136f | 2018-09-27 14:55:32 +0000 | [diff] [blame] | 750 | computeFunctionSummary(Index, M, F, BFI, PSI, DT, |
Peter Collingbourne | 5e8b94c1 | 2017-09-01 16:24:02 +0000 | [diff] [blame] | 751 | !LocalsUsed.empty() || HasLocalInlineAsmSymbol, |
Eugene Leviant | bf46e74 | 2018-11-16 07:08:00 +0000 | [diff] [blame] | 752 | CantBePromoted, IsThinLTO); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | // Compute summaries for all variables defined in module, and save in the |
| 756 | // index. |
Teresa Johnson | a700436 | 2019-07-02 19:38:02 +0000 | [diff] [blame] | 757 | SmallVector<MDNode *, 2> Types; |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 758 | for (const GlobalVariable &G : M.globals()) { |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 759 | if (G.isDeclaration()) |
| 760 | continue; |
Teresa Johnson | a700436 | 2019-07-02 19:38:02 +0000 | [diff] [blame] | 761 | computeVariableSummary(Index, G, CantBePromoted, M, Types); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 762 | } |
Teresa Johnson | 02563cd | 2016-10-28 02:39:38 +0000 | [diff] [blame] | 763 | |
| 764 | // Compute summaries for all aliases defined in module, and save in the |
| 765 | // index. |
| 766 | for (const GlobalAlias &A : M.aliases()) |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 767 | computeAliasSummary(Index, A, CantBePromoted); |
Teresa Johnson | 02563cd | 2016-10-28 02:39:38 +0000 | [diff] [blame] | 768 | |
Teresa Johnson | bf28c8f | 2016-10-30 05:40:44 +0000 | [diff] [blame] | 769 | for (auto *V : LocalsUsed) { |
| 770 | auto *Summary = Index.getGlobalValueSummary(*V); |
| 771 | assert(Summary && "Missing summary for global value"); |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 772 | Summary->setNotEligibleToImport(); |
Teresa Johnson | bf28c8f | 2016-10-30 05:40:44 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 775 | // The linker doesn't know about these LLVM produced values, so we need |
| 776 | // to flag them as live in the index to ensure index-based dead value |
| 777 | // analysis treats them as live roots of the analysis. |
| 778 | setLiveRoot(Index, "llvm.used"); |
| 779 | setLiveRoot(Index, "llvm.compiler.used"); |
| 780 | setLiveRoot(Index, "llvm.global_ctors"); |
| 781 | setLiveRoot(Index, "llvm.global_dtors"); |
| 782 | setLiveRoot(Index, "llvm.global.annotations"); |
| 783 | |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 784 | for (auto &GlobalList : Index) { |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 785 | // Ignore entries for references that are undefined in the current module. |
| 786 | if (GlobalList.second.SummaryList.empty()) |
| 787 | continue; |
| 788 | |
| 789 | assert(GlobalList.second.SummaryList.size() == 1 && |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 790 | "Expected module's index to have one summary per GUID"); |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 791 | auto &Summary = GlobalList.second.SummaryList[0]; |
Peter Collingbourne | e357fbd | 2017-06-08 23:01:49 +0000 | [diff] [blame] | 792 | if (!IsThinLTO) { |
| 793 | Summary->setNotEligibleToImport(); |
| 794 | continue; |
| 795 | } |
| 796 | |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 797 | bool AllRefsCanBeExternallyReferenced = |
| 798 | llvm::all_of(Summary->refs(), [&](const ValueInfo &VI) { |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 799 | return !CantBePromoted.count(VI.getGUID()); |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 800 | }); |
| 801 | if (!AllRefsCanBeExternallyReferenced) { |
| 802 | Summary->setNotEligibleToImport(); |
| 803 | continue; |
| 804 | } |
| 805 | |
| 806 | if (auto *FuncSummary = dyn_cast<FunctionSummary>(Summary.get())) { |
| 807 | bool AllCallsCanBeExternallyReferenced = llvm::all_of( |
| 808 | FuncSummary->calls(), [&](const FunctionSummary::EdgeTy &Edge) { |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 809 | return !CantBePromoted.count(Edge.first.getGUID()); |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 810 | }); |
| 811 | if (!AllCallsCanBeExternallyReferenced) |
| 812 | Summary->setNotEligibleToImport(); |
| 813 | } |
| 814 | } |
| 815 | |
Teresa Johnson | 2f616e4 | 2019-01-28 23:43:26 +0000 | [diff] [blame] | 816 | if (!ModuleSummaryDotFile.empty()) { |
| 817 | std::error_code EC; |
Fangrui Song | d9b948b | 2019-08-05 05:43:48 +0000 | [diff] [blame] | 818 | raw_fd_ostream OSDot(ModuleSummaryDotFile, EC, sys::fs::OpenFlags::OF_None); |
Teresa Johnson | 2f616e4 | 2019-01-28 23:43:26 +0000 | [diff] [blame] | 819 | if (EC) |
| 820 | report_fatal_error(Twine("Failed to open dot file ") + |
| 821 | ModuleSummaryDotFile + ": " + EC.message() + "\n"); |
| 822 | Index.exportToDot(OSDot); |
| 823 | } |
| 824 | |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 825 | return Index; |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 828 | AnalysisKey ModuleSummaryIndexAnalysis::Key; |
Teresa Johnson | f93b246 | 2016-08-12 13:53:02 +0000 | [diff] [blame] | 829 | |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 830 | ModuleSummaryIndex |
Teresa Johnson | f93b246 | 2016-08-12 13:53:02 +0000 | [diff] [blame] | 831 | ModuleSummaryIndexAnalysis::run(Module &M, ModuleAnalysisManager &AM) { |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 832 | ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M); |
Teresa Johnson | f93b246 | 2016-08-12 13:53:02 +0000 | [diff] [blame] | 833 | auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 834 | return buildModuleSummaryIndex( |
| 835 | M, |
| 836 | [&FAM](const Function &F) { |
| 837 | return &FAM.getResult<BlockFrequencyAnalysis>( |
| 838 | *const_cast<Function *>(&F)); |
| 839 | }, |
| 840 | &PSI); |
Teresa Johnson | f93b246 | 2016-08-12 13:53:02 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 843 | char ModuleSummaryIndexWrapperPass::ID = 0; |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 844 | |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 845 | INITIALIZE_PASS_BEGIN(ModuleSummaryIndexWrapperPass, "module-summary-analysis", |
| 846 | "Module Summary Analysis", false, true) |
| 847 | INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass) |
Mehdi Amini | 8902948 | 2017-01-21 06:01:22 +0000 | [diff] [blame] | 848 | INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass) |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 849 | INITIALIZE_PASS_END(ModuleSummaryIndexWrapperPass, "module-summary-analysis", |
| 850 | "Module Summary Analysis", false, true) |
| 851 | |
| 852 | ModulePass *llvm::createModuleSummaryIndexWrapperPass() { |
| 853 | return new ModuleSummaryIndexWrapperPass(); |
| 854 | } |
| 855 | |
| 856 | ModuleSummaryIndexWrapperPass::ModuleSummaryIndexWrapperPass() |
| 857 | : ModulePass(ID) { |
| 858 | initializeModuleSummaryIndexWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 859 | } |
| 860 | |
| 861 | bool ModuleSummaryIndexWrapperPass::runOnModule(Module &M) { |
Vedant Kumar | e7b789b | 2018-11-19 05:23:16 +0000 | [diff] [blame] | 862 | auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); |
Teresa Johnson | 5190553 | 2018-06-26 02:29:08 +0000 | [diff] [blame] | 863 | Index.emplace(buildModuleSummaryIndex( |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 864 | M, |
| 865 | [this](const Function &F) { |
| 866 | return &(this->getAnalysis<BlockFrequencyInfoWrapperPass>( |
| 867 | *const_cast<Function *>(&F)) |
| 868 | .getBFI()); |
| 869 | }, |
Vedant Kumar | e7b789b | 2018-11-19 05:23:16 +0000 | [diff] [blame] | 870 | PSI)); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 871 | return false; |
| 872 | } |
| 873 | |
| 874 | bool ModuleSummaryIndexWrapperPass::doFinalization(Module &M) { |
Chandler Carruth | b7be5b6 | 2016-08-19 07:49:19 +0000 | [diff] [blame] | 875 | Index.reset(); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 876 | return false; |
| 877 | } |
| 878 | |
| 879 | void ModuleSummaryIndexWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 880 | AU.setPreservesAll(); |
| 881 | AU.addRequired<BlockFrequencyInfoWrapperPass>(); |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 882 | AU.addRequired<ProfileSummaryInfoWrapperPass>(); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 883 | } |