blob: 4a84816f616c7791d3d0a83ee3ff2cb6876724cf [file] [log] [blame]
Teresa Johnson2d5487c2016-04-11 13:58:45 +00001//===- 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 Zelenkobb1b2d02017-08-16 22:07:40 +000016#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/DenseSet.h"
Peter Collingbourne0c30f082016-12-20 21:12:28 +000018#include "llvm/ADT/MapVector.h"
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000019#include "llvm/ADT/STLExtras.h"
Peter Collingbourne0c30f082016-12-20 21:12:28 +000020#include "llvm/ADT/SetVector.h"
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000021#include "llvm/ADT/SmallPtrSet.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/StringRef.h"
Teresa Johnson2d5487c2016-04-11 13:58:45 +000024#include "llvm/Analysis/BlockFrequencyInfo.h"
Teresa Johnson2d5487c2016-04-11 13:58:45 +000025#include "llvm/Analysis/BranchProbabilityInfo.h"
Teresa Johnsoncd21a642016-07-17 14:47:01 +000026#include "llvm/Analysis/IndirectCallPromotionAnalysis.h"
Teresa Johnson2d5487c2016-04-11 13:58:45 +000027#include "llvm/Analysis/LoopInfo.h"
Piotr Padlewskid9830eb2016-09-26 20:37:32 +000028#include "llvm/Analysis/ProfileSummaryInfo.h"
Peter Collingbourne1b4137a72016-12-21 23:03:45 +000029#include "llvm/Analysis/TypeMetadataUtils.h"
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000030#include "llvm/IR/Attributes.h"
31#include "llvm/IR/BasicBlock.h"
Teresa Johnson2d5487c2016-04-11 13:58:45 +000032#include "llvm/IR/CallSite.h"
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000033#include "llvm/IR/Constant.h"
34#include "llvm/IR/Constants.h"
Teresa Johnson2d5487c2016-04-11 13:58:45 +000035#include "llvm/IR/Dominators.h"
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000036#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 Johnson2d5487c2016-04-11 13:58:45 +000041#include "llvm/IR/IntrinsicInst.h"
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000042#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 Collingbourne61781ac2017-03-31 00:08:24 +000048#include "llvm/Object/ModuleSymbolTable.h"
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000049#include "llvm/Object/SymbolicFile.h"
Teresa Johnson2d5487c2016-04-11 13:58:45 +000050#include "llvm/Pass.h"
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000051#include "llvm/Support/Casting.h"
52#include <algorithm>
53#include <cassert>
54#include <cstdint>
55#include <vector>
56
Teresa Johnson2d5487c2016-04-11 13:58:45 +000057using namespace llvm;
58
59#define DEBUG_TYPE "module-summary-analysis"
60
61// Walk through the operands of a given User via worklist iteration and populate
62// the set of GlobalValue references encountered. Invoked either on an
63// Instruction or a GlobalVariable (which walks its initializer).
Peter Collingbourne9667b912017-05-04 18:03:25 +000064static void findRefEdges(ModuleSummaryIndex &Index, const User *CurUser,
65 SetVector<ValueInfo> &RefEdges,
Teresa Johnson2d5487c2016-04-11 13:58:45 +000066 SmallPtrSet<const User *, 8> &Visited) {
67 SmallVector<const User *, 32> Worklist;
68 Worklist.push_back(CurUser);
69
70 while (!Worklist.empty()) {
71 const User *U = Worklist.pop_back_val();
72
73 if (!Visited.insert(U).second)
74 continue;
75
76 ImmutableCallSite CS(U);
77
78 for (const auto &OI : U->operands()) {
79 const User *Operand = dyn_cast<User>(OI);
80 if (!Operand)
81 continue;
82 if (isa<BlockAddress>(Operand))
83 continue;
Peter Collingbourne0c30f082016-12-20 21:12:28 +000084 if (auto *GV = dyn_cast<GlobalValue>(Operand)) {
Teresa Johnson2d5487c2016-04-11 13:58:45 +000085 // We have a reference to a global value. This should be added to
86 // the reference set unless it is a callee. Callees are handled
87 // specially by WriteFunction and are added to a separate list.
88 if (!(CS && CS.isCallee(&OI)))
Peter Collingbourne9667b912017-05-04 18:03:25 +000089 RefEdges.insert(Index.getOrInsertValueInfo(GV));
Teresa Johnson2d5487c2016-04-11 13:58:45 +000090 continue;
91 }
92 Worklist.push_back(Operand);
93 }
94 }
95}
96
Piotr Padlewskid9830eb2016-09-26 20:37:32 +000097static CalleeInfo::HotnessType getHotness(uint64_t ProfileCount,
98 ProfileSummaryInfo *PSI) {
99 if (!PSI)
100 return CalleeInfo::HotnessType::Unknown;
101 if (PSI->isHotCount(ProfileCount))
102 return CalleeInfo::HotnessType::Hot;
103 if (PSI->isColdCount(ProfileCount))
104 return CalleeInfo::HotnessType::Cold;
105 return CalleeInfo::HotnessType::None;
106}
107
Teresa Johnson519465b2017-01-05 14:32:16 +0000108static bool isNonRenamableLocal(const GlobalValue &GV) {
109 return GV.hasSection() && GV.hasLocalLinkage();
110}
111
Peter Collingbournebe9ffaa2017-02-10 22:29:38 +0000112/// Determine whether this call has all constant integer arguments (excluding
113/// "this") and summarize it to VCalls or ConstVCalls as appropriate.
114static void addVCallToSet(DevirtCallSite Call, GlobalValue::GUID Guid,
115 SetVector<FunctionSummary::VFuncId> &VCalls,
116 SetVector<FunctionSummary::ConstVCall> &ConstVCalls) {
117 std::vector<uint64_t> Args;
118 // Start from the second argument to skip the "this" pointer.
119 for (auto &Arg : make_range(Call.CS.arg_begin() + 1, Call.CS.arg_end())) {
120 auto *CI = dyn_cast<ConstantInt>(Arg);
121 if (!CI || CI->getBitWidth() > 64) {
122 VCalls.insert({Guid, Call.Offset});
123 return;
124 }
125 Args.push_back(CI->getZExtValue());
126 }
127 ConstVCalls.insert({{Guid, Call.Offset}, std::move(Args)});
128}
129
130/// If this intrinsic call requires that we add information to the function
131/// summary, do so via the non-constant reference arguments.
132static void addIntrinsicToSummary(
133 const CallInst *CI, SetVector<GlobalValue::GUID> &TypeTests,
134 SetVector<FunctionSummary::VFuncId> &TypeTestAssumeVCalls,
135 SetVector<FunctionSummary::VFuncId> &TypeCheckedLoadVCalls,
136 SetVector<FunctionSummary::ConstVCall> &TypeTestAssumeConstVCalls,
137 SetVector<FunctionSummary::ConstVCall> &TypeCheckedLoadConstVCalls) {
138 switch (CI->getCalledFunction()->getIntrinsicID()) {
139 case Intrinsic::type_test: {
140 auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(1));
141 auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata());
142 if (!TypeId)
143 break;
144 GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString());
145
146 // Produce a summary from type.test intrinsics. We only summarize type.test
147 // intrinsics that are used other than by an llvm.assume intrinsic.
148 // Intrinsics that are assumed are relevant only to the devirtualization
149 // pass, not the type test lowering pass.
150 bool HasNonAssumeUses = llvm::any_of(CI->uses(), [](const Use &CIU) {
151 auto *AssumeCI = dyn_cast<CallInst>(CIU.getUser());
152 if (!AssumeCI)
153 return true;
154 Function *F = AssumeCI->getCalledFunction();
155 return !F || F->getIntrinsicID() != Intrinsic::assume;
156 });
157 if (HasNonAssumeUses)
158 TypeTests.insert(Guid);
159
160 SmallVector<DevirtCallSite, 4> DevirtCalls;
161 SmallVector<CallInst *, 4> Assumes;
162 findDevirtualizableCallsForTypeTest(DevirtCalls, Assumes, CI);
163 for (auto &Call : DevirtCalls)
164 addVCallToSet(Call, Guid, TypeTestAssumeVCalls,
165 TypeTestAssumeConstVCalls);
166
167 break;
168 }
169
170 case Intrinsic::type_checked_load: {
171 auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(2));
172 auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata());
173 if (!TypeId)
174 break;
175 GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString());
176
177 SmallVector<DevirtCallSite, 4> DevirtCalls;
178 SmallVector<Instruction *, 4> LoadedPtrs;
179 SmallVector<Instruction *, 4> Preds;
180 bool HasNonCallUses = false;
181 findDevirtualizableCallsForTypeCheckedLoad(DevirtCalls, LoadedPtrs, Preds,
182 HasNonCallUses, CI);
183 // Any non-call uses of the result of llvm.type.checked.load will
184 // prevent us from optimizing away the llvm.type.test.
185 if (HasNonCallUses)
186 TypeTests.insert(Guid);
187 for (auto &Call : DevirtCalls)
188 addVCallToSet(Call, Guid, TypeCheckedLoadVCalls,
189 TypeCheckedLoadConstVCalls);
190
191 break;
192 }
193 default:
194 break;
195 }
196}
197
Teresa Johnson519465b2017-01-05 14:32:16 +0000198static void
199computeFunctionSummary(ModuleSummaryIndex &Index, const Module &M,
200 const Function &F, BlockFrequencyInfo *BFI,
Peter Collingbourne5e8b94c12017-09-01 16:24:02 +0000201 ProfileSummaryInfo *PSI, bool HasLocalsInUsedOrAsm,
Teresa Johnsone27b0582017-01-05 14:59:56 +0000202 DenseSet<GlobalValue::GUID> &CantBePromoted) {
Teresa Johnson02563cd2016-10-28 02:39:38 +0000203 // Summary not currently supported for anonymous functions, they should
204 // have been named.
205 assert(F.hasName());
Teresa Johnson2d5487c2016-04-11 13:58:45 +0000206
207 unsigned NumInsts = 0;
208 // Map from callee ValueId to profile count. Used to accumulate profile
209 // counts for all static calls to a given callee.
Peter Collingbourne0c30f082016-12-20 21:12:28 +0000210 MapVector<ValueInfo, CalleeInfo> CallGraphEdges;
211 SetVector<ValueInfo> RefEdges;
Peter Collingbourne1b4137a72016-12-21 23:03:45 +0000212 SetVector<GlobalValue::GUID> TypeTests;
Peter Collingbournebe9ffaa2017-02-10 22:29:38 +0000213 SetVector<FunctionSummary::VFuncId> TypeTestAssumeVCalls,
214 TypeCheckedLoadVCalls;
215 SetVector<FunctionSummary::ConstVCall> TypeTestAssumeConstVCalls,
216 TypeCheckedLoadConstVCalls;
Teresa Johnsoncd21a642016-07-17 14:47:01 +0000217 ICallPromotionAnalysis ICallAnalysis;
Peter Collingbourne681fbb62017-09-07 05:35:35 +0000218 SmallPtrSet<const User *, 8> Visited;
219
220 // Add personality function, prefix data and prologue data to function's ref
221 // list.
222 findRefEdges(Index, &F, RefEdges, Visited);
Teresa Johnson2d5487c2016-04-11 13:58:45 +0000223
Teresa Johnsond5033a42016-11-14 16:40:19 +0000224 bool HasInlineAsmMaybeReferencingInternal = false;
Benjamin Krameraa209152016-06-26 17:27:42 +0000225 for (const BasicBlock &BB : F)
226 for (const Instruction &I : BB) {
Piotr Padlewski442d38c2016-08-30 00:46:26 +0000227 if (isa<DbgInfoIntrinsic>(I))
228 continue;
229 ++NumInsts;
Peter Collingbourne9667b912017-05-04 18:03:25 +0000230 findRefEdges(Index, &I, RefEdges, Visited);
Piotr Padlewski442d38c2016-08-30 00:46:26 +0000231 auto CS = ImmutableCallSite(&I);
232 if (!CS)
233 continue;
Teresa Johnsonbf28c8f2016-10-30 05:40:44 +0000234
235 const auto *CI = dyn_cast<CallInst>(&I);
236 // Since we don't know exactly which local values are referenced in inline
Teresa Johnsond5033a42016-11-14 16:40:19 +0000237 // assembly, conservatively mark the function as possibly referencing
238 // a local value from inline assembly to ensure we don't export a
239 // reference (which would require renaming and promotion of the
240 // referenced value).
Peter Collingbourne5e8b94c12017-09-01 16:24:02 +0000241 if (HasLocalsInUsedOrAsm && CI && CI->isInlineAsm())
Teresa Johnsond5033a42016-11-14 16:40:19 +0000242 HasInlineAsmMaybeReferencingInternal = true;
Teresa Johnsonbf28c8f2016-10-30 05:40:44 +0000243
Teresa Johnson897bab92016-10-08 16:11:42 +0000244 auto *CalledValue = CS.getCalledValue();
Piotr Padlewski442d38c2016-08-30 00:46:26 +0000245 auto *CalledFunction = CS.getCalledFunction();
Volodymyr Sapsaia7396022017-11-10 00:47:47 +0000246 if (CalledValue && !CalledFunction) {
247 CalledValue = CalledValue->stripPointerCastsNoFollowAliases();
248 // Stripping pointer casts can reveal a called function.
249 CalledFunction = dyn_cast<Function>(CalledValue);
250 }
Teresa Johnson897bab92016-10-08 16:11:42 +0000251 // Check if this is an alias to a function. If so, get the
252 // called aliasee for the checks below.
253 if (auto *GA = dyn_cast<GlobalAlias>(CalledValue)) {
254 assert(!CalledFunction && "Expected null called function in callsite for alias");
255 CalledFunction = dyn_cast<Function>(GA->getBaseObject());
256 }
Peter Collingbourne1b4137a72016-12-21 23:03:45 +0000257 // Check if this is a direct call to a known function or a known
258 // intrinsic, or an indirect call with profile data.
Piotr Padlewski442d38c2016-08-30 00:46:26 +0000259 if (CalledFunction) {
Peter Collingbournebe9ffaa2017-02-10 22:29:38 +0000260 if (CI && CalledFunction->isIntrinsic()) {
261 addIntrinsicToSummary(
262 CI, TypeTests, TypeTestAssumeVCalls, TypeCheckedLoadVCalls,
263 TypeTestAssumeConstVCalls, TypeCheckedLoadConstVCalls);
264 continue;
Peter Collingbourne1b4137a72016-12-21 23:03:45 +0000265 }
Teresa Johnson02563cd2016-10-28 02:39:38 +0000266 // We should have named any anonymous globals
267 assert(CalledFunction->hasName());
Easwaran Ramanf5f91602017-05-09 23:21:10 +0000268 auto ScaledCount = PSI->getProfileCount(&I, BFI);
Peter Collingbourne0c30f082016-12-20 21:12:28 +0000269 auto Hotness = ScaledCount ? getHotness(ScaledCount.getValue(), PSI)
270 : CalleeInfo::HotnessType::Unknown;
271
Teresa Johnson897bab92016-10-08 16:11:42 +0000272 // Use the original CalledValue, in case it was an alias. We want
273 // to record the call edge to the alias in that case. Eventually
274 // an alias summary will be created to associate the alias and
275 // aliasee.
Easwaran Ramanc73cec82018-01-25 19:27:17 +0000276 auto &ValueInfo = CallGraphEdges[Index.getOrInsertValueInfo(
277 cast<GlobalValue>(CalledValue))];
278 ValueInfo.updateHotness(Hotness);
279 // Add the relative block frequency to CalleeInfo if there is no profile
280 // information.
281 if (BFI != nullptr && Hotness == CalleeInfo::HotnessType::Unknown) {
Easwaran Raman385d8ea2018-02-22 19:44:08 +0000282 uint64_t BBFreq = BFI->getBlockFreq(&BB).getFrequency();
283 uint64_t EntryFreq = BFI->getEntryFreq();
284 ValueInfo.updateRelBlockFreq(BBFreq, EntryFreq);
Easwaran Ramanc73cec82018-01-25 19:27:17 +0000285 }
Piotr Padlewski442d38c2016-08-30 00:46:26 +0000286 } else {
Piotr Padlewski442d38c2016-08-30 00:46:26 +0000287 // Skip inline assembly calls.
288 if (CI && CI->isInlineAsm())
289 continue;
Volodymyr Sapsai8b46ff12017-11-17 18:28:05 +0000290 // Skip direct calls.
291 if (!CalledValue || isa<Constant>(CalledValue))
292 continue;
Piotr Padlewski442d38c2016-08-30 00:46:26 +0000293
294 uint32_t NumVals, NumCandidates;
295 uint64_t TotalCount;
296 auto CandidateProfileData =
297 ICallAnalysis.getPromotionCandidatesForInstruction(
298 &I, NumVals, TotalCount, NumCandidates);
299 for (auto &Candidate : CandidateProfileData)
Peter Collingbourne9667b912017-05-04 18:03:25 +0000300 CallGraphEdges[Index.getOrInsertValueInfo(Candidate.Value)]
301 .updateHotness(getHotness(Candidate.Count, PSI));
Piotr Padlewski442d38c2016-08-30 00:46:26 +0000302 }
Teresa Johnson2d5487c2016-04-11 13:58:45 +0000303 }
304
Dehao Chena60cdd32017-02-28 18:09:44 +0000305 // Explicit add hot edges to enforce importing for designated GUIDs for
306 // sample PGO, to enable the same inlines as the profiled optimized binary.
307 for (auto &I : F.getImportGUIDs())
Peter Collingbourne9667b912017-05-04 18:03:25 +0000308 CallGraphEdges[Index.getOrInsertValueInfo(I)].updateHotness(
Dehao Chen64c46572017-07-07 21:01:00 +0000309 CalleeInfo::HotnessType::Critical);
Dehao Chena60cdd32017-02-28 18:09:44 +0000310
Teresa Johnson519465b2017-01-05 14:32:16 +0000311 bool NonRenamableLocal = isNonRenamableLocal(F);
312 bool NotEligibleForImport =
313 NonRenamableLocal || HasInlineAsmMaybeReferencingInternal ||
314 // Inliner doesn't handle variadic functions.
315 // FIXME: refactor this to use the same code that inliner is using.
Eugene Leviant193e7012017-12-25 13:57:24 +0000316 F.isVarArg() ||
317 // Don't try to import functions with noinline attribute.
318 F.getAttributes().hasFnAttribute(Attribute::NoInline);
Teresa Johnson6c475a72017-01-05 21:34:18 +0000319 GlobalValueSummary::GVFlags Flags(F.getLinkage(), NotEligibleForImport,
Sean Fertile4595a912017-11-04 17:04:39 +0000320 /* Live = */ false, F.isDSOLocal());
Charles Saternos75da10d2017-08-04 16:00:58 +0000321 FunctionSummary::FFlags FunFlags{
322 F.hasFnAttribute(Attribute::ReadNone),
323 F.hasFnAttribute(Attribute::ReadOnly),
324 F.hasFnAttribute(Attribute::NoRecurse),
325 F.returnDoesNotAlias(),
326 };
Peter Collingbourne0c30f082016-12-20 21:12:28 +0000327 auto FuncSummary = llvm::make_unique<FunctionSummary>(
Charles Saternos75da10d2017-08-04 16:00:58 +0000328 Flags, NumInsts, FunFlags, RefEdges.takeVector(),
329 CallGraphEdges.takeVector(), TypeTests.takeVector(),
330 TypeTestAssumeVCalls.takeVector(), TypeCheckedLoadVCalls.takeVector(),
Peter Collingbournebe9ffaa2017-02-10 22:29:38 +0000331 TypeTestAssumeConstVCalls.takeVector(),
332 TypeCheckedLoadConstVCalls.takeVector());
Teresa Johnson519465b2017-01-05 14:32:16 +0000333 if (NonRenamableLocal)
334 CantBePromoted.insert(F.getGUID());
Chandler Carruthb7be5b62016-08-19 07:49:19 +0000335 Index.addGlobalValueSummary(F.getName(), std::move(FuncSummary));
Teresa Johnson2d5487c2016-04-11 13:58:45 +0000336}
337
Teresa Johnson519465b2017-01-05 14:32:16 +0000338static void
339computeVariableSummary(ModuleSummaryIndex &Index, const GlobalVariable &V,
Teresa Johnsone27b0582017-01-05 14:59:56 +0000340 DenseSet<GlobalValue::GUID> &CantBePromoted) {
Peter Collingbourne0c30f082016-12-20 21:12:28 +0000341 SetVector<ValueInfo> RefEdges;
Teresa Johnson2d5487c2016-04-11 13:58:45 +0000342 SmallPtrSet<const User *, 8> Visited;
Peter Collingbourne9667b912017-05-04 18:03:25 +0000343 findRefEdges(Index, &V, RefEdges, Visited);
Teresa Johnson519465b2017-01-05 14:32:16 +0000344 bool NonRenamableLocal = isNonRenamableLocal(V);
Teresa Johnson6c475a72017-01-05 21:34:18 +0000345 GlobalValueSummary::GVFlags Flags(V.getLinkage(), NonRenamableLocal,
Sean Fertile4595a912017-11-04 17:04:39 +0000346 /* Live = */ false, V.isDSOLocal());
Peter Collingbourne0c30f082016-12-20 21:12:28 +0000347 auto GVarSummary =
348 llvm::make_unique<GlobalVarSummary>(Flags, RefEdges.takeVector());
Teresa Johnson519465b2017-01-05 14:32:16 +0000349 if (NonRenamableLocal)
350 CantBePromoted.insert(V.getGUID());
Chandler Carruthb7be5b62016-08-19 07:49:19 +0000351 Index.addGlobalValueSummary(V.getName(), std::move(GVarSummary));
Teresa Johnson2d5487c2016-04-11 13:58:45 +0000352}
353
Teresa Johnson519465b2017-01-05 14:32:16 +0000354static void
355computeAliasSummary(ModuleSummaryIndex &Index, const GlobalAlias &A,
Teresa Johnsone27b0582017-01-05 14:59:56 +0000356 DenseSet<GlobalValue::GUID> &CantBePromoted) {
Teresa Johnson519465b2017-01-05 14:32:16 +0000357 bool NonRenamableLocal = isNonRenamableLocal(A);
Teresa Johnson6c475a72017-01-05 21:34:18 +0000358 GlobalValueSummary::GVFlags Flags(A.getLinkage(), NonRenamableLocal,
Sean Fertile4595a912017-11-04 17:04:39 +0000359 /* Live = */ false, A.isDSOLocal());
Teresa Johnsoncbdc5ff2017-09-13 17:10:24 +0000360 auto AS = llvm::make_unique<AliasSummary>(Flags);
Teresa Johnson02563cd2016-10-28 02:39:38 +0000361 auto *Aliasee = A.getBaseObject();
362 auto *AliaseeSummary = Index.getGlobalValueSummary(*Aliasee);
363 assert(AliaseeSummary && "Alias expects aliasee summary to be parsed");
364 AS->setAliasee(AliaseeSummary);
Teresa Johnson519465b2017-01-05 14:32:16 +0000365 if (NonRenamableLocal)
366 CantBePromoted.insert(A.getGUID());
Teresa Johnson02563cd2016-10-28 02:39:38 +0000367 Index.addGlobalValueSummary(A.getName(), std::move(AS));
368}
369
Teresa Johnson6c475a72017-01-05 21:34:18 +0000370// Set LiveRoot flag on entries matching the given value name.
371static void setLiveRoot(ModuleSummaryIndex &Index, StringRef Name) {
Peter Collingbourne9667b912017-05-04 18:03:25 +0000372 if (ValueInfo VI = Index.getValueInfo(GlobalValue::getGUID(Name)))
373 for (auto &Summary : VI.getSummaryList())
Evgeniy Stepanov56584bb2017-06-01 20:30:06 +0000374 Summary->setLive(true);
Teresa Johnson6c475a72017-01-05 21:34:18 +0000375}
376
Chandler Carruthb7be5b62016-08-19 07:49:19 +0000377ModuleSummaryIndex llvm::buildModuleSummaryIndex(
378 const Module &M,
Piotr Padlewskid9830eb2016-09-26 20:37:32 +0000379 std::function<BlockFrequencyInfo *(const Function &F)> GetBFICallback,
380 ProfileSummaryInfo *PSI) {
Teresa Johnson94624ac2017-05-10 18:52:16 +0000381 assert(PSI);
Eugene Leviant28d8a492018-01-22 13:35:40 +0000382 ModuleSummaryIndex Index(/*IsPerformingAnalysis=*/true);
Teresa Johnsonbf28c8f2016-10-30 05:40:44 +0000383
Teresa Johnsona0811452016-11-10 16:57:32 +0000384 // Identify the local values in the llvm.used and llvm.compiler.used sets,
385 // which should not be exported as they would then require renaming and
386 // promotion, but we may have opaque uses e.g. in inline asm. We collect them
387 // here because we use this information to mark functions containing inline
388 // assembly calls as not importable.
Mehdi Aminib6a11a72016-11-09 01:45:13 +0000389 SmallPtrSet<GlobalValue *, 8> LocalsUsed;
Teresa Johnsona0811452016-11-10 16:57:32 +0000390 SmallPtrSet<GlobalValue *, 8> Used;
391 // First collect those in the llvm.used set.
392 collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
Teresa Johnsona0811452016-11-10 16:57:32 +0000393 // Next collect those in the llvm.compiler.used set.
394 collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ true);
Teresa Johnsone27b0582017-01-05 14:59:56 +0000395 DenseSet<GlobalValue::GUID> CantBePromoted;
Teresa Johnsonbf28c8f2016-10-30 05:40:44 +0000396 for (auto *V : Used) {
Teresa Johnson519465b2017-01-05 14:32:16 +0000397 if (V->hasLocalLinkage()) {
Teresa Johnsonbf28c8f2016-10-30 05:40:44 +0000398 LocalsUsed.insert(V);
Teresa Johnson519465b2017-01-05 14:32:16 +0000399 CantBePromoted.insert(V->getGUID());
400 }
Teresa Johnsonbf28c8f2016-10-30 05:40:44 +0000401 }
Teresa Johnsonb35cc692016-04-20 14:39:45 +0000402
Peter Collingbourne5e8b94c12017-09-01 16:24:02 +0000403 bool HasLocalInlineAsmSymbol = false;
404 if (!M.getModuleInlineAsm().empty()) {
405 // Collect the local values defined by module level asm, and set up
406 // summaries for these symbols so that they can be marked as NoRename,
407 // to prevent export of any use of them in regular IR that would require
408 // renaming within the module level asm. Note we don't need to create a
409 // summary for weak or global defs, as they don't need to be flagged as
410 // NoRename, and defs in module level asm can't be imported anyway.
411 // Also, any values used but not defined within module level asm should
412 // be listed on the llvm.used or llvm.compiler.used global and marked as
413 // referenced from there.
414 ModuleSymbolTable::CollectAsmSymbols(
415 M, [&](StringRef Name, object::BasicSymbolRef::Flags Flags) {
416 // Symbols not marked as Weak or Global are local definitions.
417 if (Flags & (object::BasicSymbolRef::SF_Weak |
418 object::BasicSymbolRef::SF_Global))
419 return;
420 HasLocalInlineAsmSymbol = true;
421 GlobalValue *GV = M.getNamedValue(Name);
422 if (!GV)
423 return;
424 assert(GV->isDeclaration() && "Def in module asm already has definition");
425 GlobalValueSummary::GVFlags GVFlags(GlobalValue::InternalLinkage,
426 /* NotEligibleToImport = */ true,
Sean Fertile4595a912017-11-04 17:04:39 +0000427 /* Live = */ true,
428 /* Local */ GV->isDSOLocal());
Peter Collingbourne5e8b94c12017-09-01 16:24:02 +0000429 CantBePromoted.insert(GlobalValue::getGUID(Name));
430 // Create the appropriate summary type.
431 if (Function *F = dyn_cast<Function>(GV)) {
432 std::unique_ptr<FunctionSummary> Summary =
433 llvm::make_unique<FunctionSummary>(
434 GVFlags, 0,
435 FunctionSummary::FFlags{
436 F->hasFnAttribute(Attribute::ReadNone),
437 F->hasFnAttribute(Attribute::ReadOnly),
438 F->hasFnAttribute(Attribute::NoRecurse),
439 F->returnDoesNotAlias()},
440 ArrayRef<ValueInfo>{}, ArrayRef<FunctionSummary::EdgeTy>{},
441 ArrayRef<GlobalValue::GUID>{},
442 ArrayRef<FunctionSummary::VFuncId>{},
443 ArrayRef<FunctionSummary::VFuncId>{},
444 ArrayRef<FunctionSummary::ConstVCall>{},
445 ArrayRef<FunctionSummary::ConstVCall>{});
446 Index.addGlobalValueSummary(Name, std::move(Summary));
447 } else {
448 std::unique_ptr<GlobalVarSummary> Summary =
449 llvm::make_unique<GlobalVarSummary>(GVFlags,
450 ArrayRef<ValueInfo>{});
451 Index.addGlobalValueSummary(Name, std::move(Summary));
452 }
453 });
454 }
455
Teresa Johnson2d5487c2016-04-11 13:58:45 +0000456 // Compute summaries for all functions defined in module, and save in the
457 // index.
Chandler Carruthb7be5b62016-08-19 07:49:19 +0000458 for (auto &F : M) {
Teresa Johnson2d5487c2016-04-11 13:58:45 +0000459 if (F.isDeclaration())
460 continue;
461
462 BlockFrequencyInfo *BFI = nullptr;
463 std::unique_ptr<BlockFrequencyInfo> BFIPtr;
Chandler Carruthb7be5b62016-08-19 07:49:19 +0000464 if (GetBFICallback)
465 BFI = GetBFICallback(F);
Easwaran Ramana17f2202017-12-22 01:33:52 +0000466 else if (F.hasProfileData()) {
Teresa Johnson2d5487c2016-04-11 13:58:45 +0000467 LoopInfo LI{DominatorTree(const_cast<Function &>(F))};
468 BranchProbabilityInfo BPI{F, LI};
469 BFIPtr = llvm::make_unique<BlockFrequencyInfo>(F, BPI, LI);
470 BFI = BFIPtr.get();
471 }
472
Peter Collingbourne5e8b94c12017-09-01 16:24:02 +0000473 computeFunctionSummary(Index, M, F, BFI, PSI,
474 !LocalsUsed.empty() || HasLocalInlineAsmSymbol,
Teresa Johnson519465b2017-01-05 14:32:16 +0000475 CantBePromoted);
Teresa Johnson2d5487c2016-04-11 13:58:45 +0000476 }
477
478 // Compute summaries for all variables defined in module, and save in the
479 // index.
Chandler Carruthb7be5b62016-08-19 07:49:19 +0000480 for (const GlobalVariable &G : M.globals()) {
Teresa Johnson2d5487c2016-04-11 13:58:45 +0000481 if (G.isDeclaration())
482 continue;
Teresa Johnson519465b2017-01-05 14:32:16 +0000483 computeVariableSummary(Index, G, CantBePromoted);
Teresa Johnson2d5487c2016-04-11 13:58:45 +0000484 }
Teresa Johnson02563cd2016-10-28 02:39:38 +0000485
486 // Compute summaries for all aliases defined in module, and save in the
487 // index.
488 for (const GlobalAlias &A : M.aliases())
Teresa Johnson519465b2017-01-05 14:32:16 +0000489 computeAliasSummary(Index, A, CantBePromoted);
Teresa Johnson02563cd2016-10-28 02:39:38 +0000490
Teresa Johnsonbf28c8f2016-10-30 05:40:44 +0000491 for (auto *V : LocalsUsed) {
492 auto *Summary = Index.getGlobalValueSummary(*V);
493 assert(Summary && "Missing summary for global value");
Teresa Johnson519465b2017-01-05 14:32:16 +0000494 Summary->setNotEligibleToImport();
Teresa Johnsonbf28c8f2016-10-30 05:40:44 +0000495 }
496
Teresa Johnson6c475a72017-01-05 21:34:18 +0000497 // The linker doesn't know about these LLVM produced values, so we need
498 // to flag them as live in the index to ensure index-based dead value
499 // analysis treats them as live roots of the analysis.
500 setLiveRoot(Index, "llvm.used");
501 setLiveRoot(Index, "llvm.compiler.used");
502 setLiveRoot(Index, "llvm.global_ctors");
503 setLiveRoot(Index, "llvm.global_dtors");
504 setLiveRoot(Index, "llvm.global.annotations");
505
Peter Collingbournee357fbd2017-06-08 23:01:49 +0000506 bool IsThinLTO = true;
507 if (auto *MD =
508 mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("ThinLTO")))
509 IsThinLTO = MD->getZExtValue();
510
Teresa Johnson519465b2017-01-05 14:32:16 +0000511 for (auto &GlobalList : Index) {
Peter Collingbourne9667b912017-05-04 18:03:25 +0000512 // Ignore entries for references that are undefined in the current module.
513 if (GlobalList.second.SummaryList.empty())
514 continue;
515
516 assert(GlobalList.second.SummaryList.size() == 1 &&
Teresa Johnson519465b2017-01-05 14:32:16 +0000517 "Expected module's index to have one summary per GUID");
Peter Collingbourne9667b912017-05-04 18:03:25 +0000518 auto &Summary = GlobalList.second.SummaryList[0];
Peter Collingbournee357fbd2017-06-08 23:01:49 +0000519 if (!IsThinLTO) {
520 Summary->setNotEligibleToImport();
521 continue;
522 }
523
Teresa Johnson519465b2017-01-05 14:32:16 +0000524 bool AllRefsCanBeExternallyReferenced =
525 llvm::all_of(Summary->refs(), [&](const ValueInfo &VI) {
Peter Collingbourne9667b912017-05-04 18:03:25 +0000526 return !CantBePromoted.count(VI.getGUID());
Teresa Johnson519465b2017-01-05 14:32:16 +0000527 });
528 if (!AllRefsCanBeExternallyReferenced) {
529 Summary->setNotEligibleToImport();
530 continue;
531 }
532
533 if (auto *FuncSummary = dyn_cast<FunctionSummary>(Summary.get())) {
534 bool AllCallsCanBeExternallyReferenced = llvm::all_of(
535 FuncSummary->calls(), [&](const FunctionSummary::EdgeTy &Edge) {
Peter Collingbourne9667b912017-05-04 18:03:25 +0000536 return !CantBePromoted.count(Edge.first.getGUID());
Teresa Johnson519465b2017-01-05 14:32:16 +0000537 });
538 if (!AllCallsCanBeExternallyReferenced)
539 Summary->setNotEligibleToImport();
540 }
541 }
542
Chandler Carruthb7be5b62016-08-19 07:49:19 +0000543 return Index;
Teresa Johnson2d5487c2016-04-11 13:58:45 +0000544}
545
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000546AnalysisKey ModuleSummaryIndexAnalysis::Key;
Teresa Johnsonf93b2462016-08-12 13:53:02 +0000547
Chandler Carruthb7be5b62016-08-19 07:49:19 +0000548ModuleSummaryIndex
Teresa Johnsonf93b2462016-08-12 13:53:02 +0000549ModuleSummaryIndexAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
Piotr Padlewskid9830eb2016-09-26 20:37:32 +0000550 ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M);
Teresa Johnsonf93b2462016-08-12 13:53:02 +0000551 auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
Piotr Padlewskid9830eb2016-09-26 20:37:32 +0000552 return buildModuleSummaryIndex(
553 M,
554 [&FAM](const Function &F) {
555 return &FAM.getResult<BlockFrequencyAnalysis>(
556 *const_cast<Function *>(&F));
557 },
558 &PSI);
Teresa Johnsonf93b2462016-08-12 13:53:02 +0000559}
560
Teresa Johnson2d5487c2016-04-11 13:58:45 +0000561char ModuleSummaryIndexWrapperPass::ID = 0;
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +0000562
Teresa Johnson2d5487c2016-04-11 13:58:45 +0000563INITIALIZE_PASS_BEGIN(ModuleSummaryIndexWrapperPass, "module-summary-analysis",
564 "Module Summary Analysis", false, true)
565INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
Mehdi Amini89029482017-01-21 06:01:22 +0000566INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
Teresa Johnson2d5487c2016-04-11 13:58:45 +0000567INITIALIZE_PASS_END(ModuleSummaryIndexWrapperPass, "module-summary-analysis",
568 "Module Summary Analysis", false, true)
569
570ModulePass *llvm::createModuleSummaryIndexWrapperPass() {
571 return new ModuleSummaryIndexWrapperPass();
572}
573
574ModuleSummaryIndexWrapperPass::ModuleSummaryIndexWrapperPass()
575 : ModulePass(ID) {
576 initializeModuleSummaryIndexWrapperPassPass(*PassRegistry::getPassRegistry());
577}
578
579bool ModuleSummaryIndexWrapperPass::runOnModule(Module &M) {
Dehao Chen5461d8b2016-09-28 21:00:58 +0000580 auto &PSI = *getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
Piotr Padlewskid9830eb2016-09-26 20:37:32 +0000581 Index = buildModuleSummaryIndex(
582 M,
583 [this](const Function &F) {
584 return &(this->getAnalysis<BlockFrequencyInfoWrapperPass>(
585 *const_cast<Function *>(&F))
586 .getBFI());
587 },
588 &PSI);
Teresa Johnson2d5487c2016-04-11 13:58:45 +0000589 return false;
590}
591
592bool ModuleSummaryIndexWrapperPass::doFinalization(Module &M) {
Chandler Carruthb7be5b62016-08-19 07:49:19 +0000593 Index.reset();
Teresa Johnson2d5487c2016-04-11 13:58:45 +0000594 return false;
595}
596
597void ModuleSummaryIndexWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
598 AU.setPreservesAll();
599 AU.addRequired<BlockFrequencyInfoWrapperPass>();
Piotr Padlewskid9830eb2016-09-26 20:37:32 +0000600 AU.addRequired<ProfileSummaryInfoWrapperPass>();
Teresa Johnson2d5487c2016-04-11 13:58:45 +0000601}