blob: 4704215800bd534c16fd68b5c120db77f0f4a382 [file] [log] [blame]
Teresa Johnson26ab5772016-03-15 00:04:37 +00001//===-- ModuleSummaryIndex.cpp - Module Summary Index ---------------------===//
Teresa Johnsoncec0cae2016-03-14 21:18:10 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Teresa Johnsoncec0cae2016-03-14 21:18:10 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the module index and summary classes for the
10// IR library.
11//
12//===----------------------------------------------------------------------===//
13
Teresa Johnson26ab5772016-03-15 00:04:37 +000014#include "llvm/IR/ModuleSummaryIndex.h"
Charles Saternosb040fcc2018-02-19 15:14:50 +000015#include "llvm/ADT/SCCIterator.h"
Teresa Johnson8c1915c2018-11-17 20:03:22 +000016#include "llvm/ADT/Statistic.h"
Teresa Johnsoncec0cae2016-03-14 21:18:10 +000017#include "llvm/ADT/StringMap.h"
Eugene Leviant28d8a492018-01-22 13:35:40 +000018#include "llvm/Support/Path.h"
Charles Saternosb040fcc2018-02-19 15:14:50 +000019#include "llvm/Support/raw_ostream.h"
Teresa Johnsoncec0cae2016-03-14 21:18:10 +000020using namespace llvm;
21
Teresa Johnson8c1915c2018-11-17 20:03:22 +000022#define DEBUG_TYPE "module-summary-index"
23
24STATISTIC(ReadOnlyLiveGVars,
25 "Number of live global variables marked read only");
26
Charles Saternosb040fcc2018-02-19 15:14:50 +000027FunctionSummary FunctionSummary::ExternalNode =
28 FunctionSummary::makeDummyFunctionSummary({});
Peter Collingbourneb4edfb92018-02-05 17:17:51 +000029bool ValueInfo::isDSOLocal() const {
30 // Need to check all summaries are local in case of hash collisions.
31 return getSummaryList().size() &&
32 llvm::all_of(getSummaryList(),
33 [](const std::unique_ptr<GlobalValueSummary> &Summary) {
34 return Summary->isDSOLocal();
35 });
36}
37
Eugene Leviantbf46e742018-11-16 07:08:00 +000038// Gets the number of immutable refs in RefEdgeList
39unsigned FunctionSummary::immutableRefCount() const {
40 // Here we take advantage of having all readonly references
41 // located in the end of the RefEdgeList.
42 auto Refs = refs();
43 unsigned ImmutableRefCnt = 0;
44 for (int I = Refs.size() - 1; I >= 0 && Refs[I].isReadOnly(); --I)
45 ImmutableRefCnt++;
46 return ImmutableRefCnt;
47}
48
Teresa Johnsonc86af332016-04-12 21:13:11 +000049// Collect for the given module the list of function it defines
50// (GUID -> Summary).
51void ModuleSummaryIndex::collectDefinedFunctionsForModule(
Teresa Johnsonc851d212016-04-25 21:09:51 +000052 StringRef ModulePath, GVSummaryMapTy &GVSummaryMap) const {
Teresa Johnsonc86af332016-04-12 21:13:11 +000053 for (auto &GlobalList : *this) {
54 auto GUID = GlobalList.first;
Peter Collingbourne9667b912017-05-04 18:03:25 +000055 for (auto &GlobSummary : GlobalList.second.SummaryList) {
Teresa Johnson28e457b2016-04-24 14:57:11 +000056 auto *Summary = dyn_cast_or_null<FunctionSummary>(GlobSummary.get());
Teresa Johnsonc86af332016-04-12 21:13:11 +000057 if (!Summary)
58 // Ignore global variable, focus on functions
59 continue;
60 // Ignore summaries from other modules.
61 if (Summary->modulePath() != ModulePath)
62 continue;
Teresa Johnson28e457b2016-04-24 14:57:11 +000063 GVSummaryMap[GUID] = Summary;
Teresa Johnsonc86af332016-04-12 21:13:11 +000064 }
65 }
66}
67
Teresa Johnson28e457b2016-04-24 14:57:11 +000068GlobalValueSummary *
69ModuleSummaryIndex::getGlobalValueSummary(uint64_t ValueGUID,
70 bool PerModuleIndex) const {
Peter Collingbourne9667b912017-05-04 18:03:25 +000071 auto VI = getValueInfo(ValueGUID);
72 assert(VI && "GlobalValue not found in index");
73 assert((!PerModuleIndex || VI.getSummaryList().size() == 1) &&
Haojian Wu591ae462016-04-05 09:07:47 +000074 "Expected a single entry per global value in per-module index");
Peter Collingbourne9667b912017-05-04 18:03:25 +000075 auto &Summary = VI.getSummaryList()[0];
Teresa Johnson28e457b2016-04-24 14:57:11 +000076 return Summary.get();
Teresa Johnsonfb7c7642016-04-05 00:40:16 +000077}
Peter Collingbournedbd2fed2017-06-15 17:26:13 +000078
79bool ModuleSummaryIndex::isGUIDLive(GlobalValue::GUID GUID) const {
80 auto VI = getValueInfo(GUID);
81 if (!VI)
Evgeniy Stepanov4d4ee932017-06-16 00:18:29 +000082 return true;
83 const auto &SummaryList = VI.getSummaryList();
84 if (SummaryList.empty())
85 return true;
86 for (auto &I : SummaryList)
Peter Collingbournedbd2fed2017-06-15 17:26:13 +000087 if (isGlobalValueLive(I.get()))
88 return true;
89 return false;
90}
Eugene Leviant28d8a492018-01-22 13:35:40 +000091
Eugene Leviantbf46e742018-11-16 07:08:00 +000092static void propagateConstantsToRefs(GlobalValueSummary *S) {
93 // If reference is not readonly then referenced summary is not
94 // readonly either. Note that:
95 // - All references from GlobalVarSummary are conservatively considered as
96 // not readonly. Tracking them properly requires more complex analysis
97 // then we have now.
98 //
99 // - AliasSummary objects have no refs at all so this function is a no-op
100 // for them.
101 for (auto &VI : S->refs()) {
102 if (VI.isReadOnly()) {
103 // We only mark refs as readonly when computing function summaries on
104 // analysis phase.
105 assert(isa<FunctionSummary>(S));
106 continue;
107 }
108 for (auto &Ref : VI.getSummaryList())
109 // If references to alias is not readonly then aliasee is not readonly
110 if (auto *GVS = dyn_cast<GlobalVarSummary>(Ref->getBaseObject()))
111 GVS->setReadOnly(false);
112 }
113}
114
115// Do the constant propagation in combined index.
116// The goal of constant propagation is internalization of readonly
117// variables. To determine which variables are readonly and which
118// are not we take following steps:
119// - During analysis we speculatively assign readonly attribute to
120// all variables which can be internalized. When computing function
121// summary we also assign readonly attribute to a reference if
122// function doesn't modify referenced variable.
123//
124// - After computing dead symbols in combined index we do the constant
125// propagation. During this step we clear readonly attribute from
126// all variables which:
Eugene Leviant0c7460a2018-11-19 14:19:37 +0000127// a. are preserved or can't be imported
Eugene Leviantbf46e742018-11-16 07:08:00 +0000128// b. referenced by any global variable initializer
129// c. referenced by a function and reference is not readonly
130//
131// Internalization itself happens in the backend after import is finished
132// See internalizeImmutableGVs.
133void ModuleSummaryIndex::propagateConstants(
134 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
135 for (auto &P : *this)
136 for (auto &S : P.second.SummaryList) {
137 if (!isGlobalValueLive(S.get()))
138 // We don't examine references from dead objects
139 continue;
140
141 // Global variable can't be marked read only if it is not eligible
142 // to import since we need to ensure that all external references
143 // get a local (imported) copy. It also can't be marked read only
144 // if it or any alias (since alias points to the same memory) are
145 // preserved or notEligibleToImport, since either of those means
146 // there could be writes that are not visible (because preserved
147 // means it could have external to DSO writes, and notEligibleToImport
148 // means it could have writes via inline assembly leading it to be
149 // in the @llvm.*used).
150 if (auto *GVS = dyn_cast<GlobalVarSummary>(S->getBaseObject()))
151 // Here we intentionally pass S.get() not GVS, because S could be
152 // an alias.
153 if (!canImportGlobalVar(S.get()) || GUIDPreservedSymbols.count(P.first))
154 GVS->setReadOnly(false);
155 propagateConstantsToRefs(S.get());
156 }
Teresa Johnson5b9bb252018-11-17 20:41:45 +0000157 if (llvm::AreStatisticsEnabled())
158 for (auto &P : *this)
159 if (P.second.SummaryList.size())
160 if (auto *GVS = dyn_cast<GlobalVarSummary>(
161 P.second.SummaryList[0]->getBaseObject()))
162 if (isGlobalValueLive(GVS) && GVS->isReadOnly())
163 ReadOnlyLiveGVars++;
Eugene Leviantbf46e742018-11-16 07:08:00 +0000164}
165
Charles Saternosb040fcc2018-02-19 15:14:50 +0000166// TODO: write a graphviz dumper for SCCs (see ModuleSummaryIndex::exportToDot)
167// then delete this function and update its tests
168LLVM_DUMP_METHOD
169void ModuleSummaryIndex::dumpSCCs(raw_ostream &O) {
170 for (scc_iterator<ModuleSummaryIndex *> I =
171 scc_begin<ModuleSummaryIndex *>(this);
172 !I.isAtEnd(); ++I) {
173 O << "SCC (" << utostr(I->size()) << " node" << (I->size() == 1 ? "" : "s")
174 << ") {\n";
175 for (const ValueInfo V : *I) {
176 FunctionSummary *F = nullptr;
177 if (V.getSummaryList().size())
178 F = cast<FunctionSummary>(V.getSummaryList().front().get());
179 O << " " << (F == nullptr ? "External" : "") << " " << utostr(V.getGUID())
180 << (I.hasLoop() ? " (has loop)" : "") << "\n";
181 }
182 O << "}\n";
183 }
184}
185
Eugene Leviant28d8a492018-01-22 13:35:40 +0000186namespace {
187struct Attributes {
188 void add(const Twine &Name, const Twine &Value,
189 const Twine &Comment = Twine());
Eugene Leviantbf46e742018-11-16 07:08:00 +0000190 void addComment(const Twine &Comment);
Eugene Leviant28d8a492018-01-22 13:35:40 +0000191 std::string getAsString() const;
192
193 std::vector<std::string> Attrs;
194 std::string Comments;
195};
196
197struct Edge {
198 uint64_t SrcMod;
199 int Hotness;
200 GlobalValue::GUID Src;
201 GlobalValue::GUID Dst;
202};
203}
204
205void Attributes::add(const Twine &Name, const Twine &Value,
206 const Twine &Comment) {
207 std::string A = Name.str();
208 A += "=\"";
209 A += Value.str();
210 A += "\"";
211 Attrs.push_back(A);
Eugene Leviantbf46e742018-11-16 07:08:00 +0000212 addComment(Comment);
213}
214
215void Attributes::addComment(const Twine &Comment) {
Eugene Leviant28d8a492018-01-22 13:35:40 +0000216 if (!Comment.isTriviallyEmpty()) {
217 if (Comments.empty())
218 Comments = " // ";
219 else
220 Comments += ", ";
221 Comments += Comment.str();
222 }
223}
224
225std::string Attributes::getAsString() const {
226 if (Attrs.empty())
227 return "";
228
229 std::string Ret = "[";
230 for (auto &A : Attrs)
231 Ret += A + ",";
232 Ret.pop_back();
233 Ret += "];";
234 Ret += Comments;
235 return Ret;
236}
237
238static std::string linkageToString(GlobalValue::LinkageTypes LT) {
239 switch (LT) {
240 case GlobalValue::ExternalLinkage:
241 return "extern";
242 case GlobalValue::AvailableExternallyLinkage:
243 return "av_ext";
244 case GlobalValue::LinkOnceAnyLinkage:
245 return "linkonce";
246 case GlobalValue::LinkOnceODRLinkage:
247 return "linkonce_odr";
248 case GlobalValue::WeakAnyLinkage:
249 return "weak";
250 case GlobalValue::WeakODRLinkage:
251 return "weak_odr";
252 case GlobalValue::AppendingLinkage:
253 return "appending";
254 case GlobalValue::InternalLinkage:
255 return "internal";
256 case GlobalValue::PrivateLinkage:
257 return "private";
258 case GlobalValue::ExternalWeakLinkage:
259 return "extern_weak";
260 case GlobalValue::CommonLinkage:
261 return "common";
262 }
263
264 return "<unknown>";
265}
266
267static std::string fflagsToString(FunctionSummary::FFlags F) {
268 auto FlagValue = [](unsigned V) { return V ? '1' : '0'; };
Teresa Johnsoncb397462018-11-06 19:41:35 +0000269 char FlagRep[] = {FlagValue(F.ReadNone), FlagValue(F.ReadOnly),
270 FlagValue(F.NoRecurse), FlagValue(F.ReturnDoesNotAlias),
271 FlagValue(F.NoInline), 0};
Eugene Leviant28d8a492018-01-22 13:35:40 +0000272
273 return FlagRep;
274}
275
276// Get string representation of function instruction count and flags.
277static std::string getSummaryAttributes(GlobalValueSummary* GVS) {
278 auto *FS = dyn_cast_or_null<FunctionSummary>(GVS);
279 if (!FS)
280 return "";
281
282 return std::string("inst: ") + std::to_string(FS->instCount()) +
283 ", ffl: " + fflagsToString(FS->fflags());
284}
285
Teresa Johnson7a92bc32018-11-02 23:49:21 +0000286static std::string getNodeVisualName(GlobalValue::GUID Id) {
287 return std::string("@") + std::to_string(Id);
288}
289
Eugene Leviant28d8a492018-01-22 13:35:40 +0000290static std::string getNodeVisualName(const ValueInfo &VI) {
Teresa Johnson7a92bc32018-11-02 23:49:21 +0000291 return VI.name().empty() ? getNodeVisualName(VI.getGUID()) : VI.name().str();
Eugene Leviant28d8a492018-01-22 13:35:40 +0000292}
293
294static std::string getNodeLabel(const ValueInfo &VI, GlobalValueSummary *GVS) {
295 if (isa<AliasSummary>(GVS))
296 return getNodeVisualName(VI);
297
298 std::string Attrs = getSummaryAttributes(GVS);
299 std::string Label =
300 getNodeVisualName(VI) + "|" + linkageToString(GVS->linkage());
301 if (!Attrs.empty())
302 Label += std::string(" (") + Attrs + ")";
303 Label += "}";
304
305 return Label;
306}
307
308// Write definition of external node, which doesn't have any
309// specific module associated with it. Typically this is function
310// or variable defined in native object or library.
311static void defineExternalNode(raw_ostream &OS, const char *Pfx,
Teresa Johnson7a92bc32018-11-02 23:49:21 +0000312 const ValueInfo &VI, GlobalValue::GUID Id) {
313 auto StrId = std::to_string(Id);
314 OS << " " << StrId << " [label=\"";
315
316 if (VI) {
317 OS << getNodeVisualName(VI);
318 } else {
319 OS << getNodeVisualName(Id);
320 }
321 OS << "\"]; // defined externally\n";
Eugene Leviant28d8a492018-01-22 13:35:40 +0000322}
323
Eugene Leviantbf46e742018-11-16 07:08:00 +0000324static bool hasReadOnlyFlag(const GlobalValueSummary *S) {
325 if (auto *GVS = dyn_cast<GlobalVarSummary>(S))
326 return GVS->isReadOnly();
327 return false;
328}
329
Teresa Johnson7a92bc32018-11-02 23:49:21 +0000330void ModuleSummaryIndex::exportToDot(raw_ostream &OS) const {
Eugene Leviant28d8a492018-01-22 13:35:40 +0000331 std::vector<Edge> CrossModuleEdges;
332 DenseMap<GlobalValue::GUID, std::vector<uint64_t>> NodeMap;
Eugene Leviant24b3d252019-02-26 07:38:21 +0000333 using GVSOrderedMapTy = std::map<GlobalValue::GUID, GlobalValueSummary *>;
334 std::map<StringRef, GVSOrderedMapTy> ModuleToDefinedGVS;
Eugene Leviant28d8a492018-01-22 13:35:40 +0000335 collectDefinedGVSummariesPerModule(ModuleToDefinedGVS);
336
337 // Get node identifier in form MXXX_<GUID>. The MXXX prefix is required,
338 // because we may have multiple linkonce functions summaries.
339 auto NodeId = [](uint64_t ModId, GlobalValue::GUID Id) {
340 return ModId == (uint64_t)-1 ? std::to_string(Id)
341 : std::string("M") + std::to_string(ModId) +
342 "_" + std::to_string(Id);
343 };
344
Eugene Leviant1f545002018-10-24 07:48:32 +0000345 auto DrawEdge = [&](const char *Pfx, uint64_t SrcMod, GlobalValue::GUID SrcId,
Eugene Leviantbf46e742018-11-16 07:08:00 +0000346 uint64_t DstMod, GlobalValue::GUID DstId,
347 int TypeOrHotness) {
348 // 0 - alias
349 // 1 - reference
350 // 2 - constant reference
351 // Other value: (hotness - 3).
352 TypeOrHotness += 3;
Eugene Leviant28d8a492018-01-22 13:35:40 +0000353 static const char *EdgeAttrs[] = {
354 " [style=dotted]; // alias",
355 " [style=dashed]; // ref",
Eugene Leviantbf46e742018-11-16 07:08:00 +0000356 " [style=dashed,color=forestgreen]; // const-ref",
Eugene Leviant28d8a492018-01-22 13:35:40 +0000357 " // call (hotness : Unknown)",
358 " [color=blue]; // call (hotness : Cold)",
359 " // call (hotness : None)",
360 " [color=brown]; // call (hotness : Hot)",
361 " [style=bold,color=red]; // call (hotness : Critical)"};
362
363 assert(static_cast<size_t>(TypeOrHotness) <
364 sizeof(EdgeAttrs) / sizeof(EdgeAttrs[0]));
365 OS << Pfx << NodeId(SrcMod, SrcId) << " -> " << NodeId(DstMod, DstId)
366 << EdgeAttrs[TypeOrHotness] << "\n";
367 };
368
369 OS << "digraph Summary {\n";
370 for (auto &ModIt : ModuleToDefinedGVS) {
Eugene Leviant24b3d252019-02-26 07:38:21 +0000371 auto ModId = getModuleId(ModIt.first);
372 OS << " // Module: " << ModIt.first << "\n";
Eugene Leviant28d8a492018-01-22 13:35:40 +0000373 OS << " subgraph cluster_" << std::to_string(ModId) << " {\n";
374 OS << " style = filled;\n";
375 OS << " color = lightgrey;\n";
Eugene Leviant24b3d252019-02-26 07:38:21 +0000376 OS << " label = \"" << sys::path::filename(ModIt.first) << "\";\n";
Eugene Leviant28d8a492018-01-22 13:35:40 +0000377 OS << " node [style=filled,fillcolor=lightblue];\n";
378
379 auto &GVSMap = ModIt.second;
380 auto Draw = [&](GlobalValue::GUID IdFrom, GlobalValue::GUID IdTo, int Hotness) {
381 if (!GVSMap.count(IdTo)) {
382 CrossModuleEdges.push_back({ModId, Hotness, IdFrom, IdTo});
383 return;
384 }
385 DrawEdge(" ", ModId, IdFrom, ModId, IdTo, Hotness);
386 };
387
388 for (auto &SummaryIt : GVSMap) {
389 NodeMap[SummaryIt.first].push_back(ModId);
390 auto Flags = SummaryIt.second->flags();
391 Attributes A;
392 if (isa<FunctionSummary>(SummaryIt.second)) {
393 A.add("shape", "record", "function");
394 } else if (isa<AliasSummary>(SummaryIt.second)) {
395 A.add("style", "dotted,filled", "alias");
396 A.add("shape", "box");
397 } else {
398 A.add("shape", "Mrecord", "variable");
Eugene Leviantbf46e742018-11-16 07:08:00 +0000399 if (Flags.Live && hasReadOnlyFlag(SummaryIt.second))
400 A.addComment("immutable");
Eugene Leviant28d8a492018-01-22 13:35:40 +0000401 }
402
403 auto VI = getValueInfo(SummaryIt.first);
404 A.add("label", getNodeLabel(VI, SummaryIt.second));
405 if (!Flags.Live)
406 A.add("fillcolor", "red", "dead");
407 else if (Flags.NotEligibleToImport)
408 A.add("fillcolor", "yellow", "not eligible to import");
409
410 OS << " " << NodeId(ModId, SummaryIt.first) << " " << A.getAsString()
411 << "\n";
412 }
413 OS << " // Edges:\n";
414
415 for (auto &SummaryIt : GVSMap) {
416 auto *GVS = SummaryIt.second;
417 for (auto &R : GVS->refs())
Eugene Leviantbf46e742018-11-16 07:08:00 +0000418 Draw(SummaryIt.first, R.getGUID(), R.isReadOnly() ? -1 : -2);
Eugene Leviant28d8a492018-01-22 13:35:40 +0000419
420 if (auto *AS = dyn_cast_or_null<AliasSummary>(SummaryIt.second)) {
Teresa Johnson70ec64c2019-03-15 15:11:38 +0000421 Draw(SummaryIt.first, AS->getAliaseeGUID(), -3);
Eugene Leviant28d8a492018-01-22 13:35:40 +0000422 continue;
423 }
424
425 if (auto *FS = dyn_cast_or_null<FunctionSummary>(SummaryIt.second))
426 for (auto &CGEdge : FS->calls())
427 Draw(SummaryIt.first, CGEdge.first.getGUID(),
428 static_cast<int>(CGEdge.second.Hotness));
429 }
430 OS << " }\n";
431 }
432
433 OS << " // Cross-module edges:\n";
434 for (auto &E : CrossModuleEdges) {
435 auto &ModList = NodeMap[E.Dst];
436 if (ModList.empty()) {
Teresa Johnson7a92bc32018-11-02 23:49:21 +0000437 defineExternalNode(OS, " ", getValueInfo(E.Dst), E.Dst);
Eugene Leviant28d8a492018-01-22 13:35:40 +0000438 // Add fake module to the list to draw an edge to an external node
439 // in the loop below.
440 ModList.push_back(-1);
441 }
442 for (auto DstMod : ModList)
443 // The edge representing call or ref is drawn to every module where target
444 // symbol is defined. When target is a linkonce symbol there can be
445 // multiple edges representing a single call or ref, both intra-module and
446 // cross-module. As we've already drawn all intra-module edges before we
447 // skip it here.
448 if (DstMod != E.SrcMod)
449 DrawEdge(" ", E.SrcMod, E.Src, DstMod, E.Dst, E.Hotness);
450 }
451
452 OS << "}";
453}