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