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