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