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