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