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