1 //===-- ModuleSummaryIndex.cpp - Module Summary Index ---------------------===//
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 file implements the module index and summary classes for the
11 // IR library.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/IR/ModuleSummaryIndex.h"
16 #include "llvm/ADT/SCCIterator.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/Support/Path.h"
19 #include "llvm/Support/raw_ostream.h"
20 using namespace llvm;
21 
22 FunctionSummary FunctionSummary::ExternalNode =
23     FunctionSummary::makeDummyFunctionSummary({});
24 bool ValueInfo::isDSOLocal() const {
25   // Need to check all summaries are local in case of hash collisions.
26   return getSummaryList().size() &&
27          llvm::all_of(getSummaryList(),
28                       [](const std::unique_ptr<GlobalValueSummary> &Summary) {
29                         return Summary->isDSOLocal();
30                       });
31 }
32 
33 // Gets the number of immutable refs in RefEdgeList
34 unsigned FunctionSummary::immutableRefCount() const {
35   // Here we take advantage of having all readonly references
36   // located in the end of the RefEdgeList.
37   auto Refs = refs();
38   unsigned ImmutableRefCnt = 0;
39   for (int I = Refs.size() - 1; I >= 0 && Refs[I].isReadOnly(); --I)
40     ImmutableRefCnt++;
41   return ImmutableRefCnt;
42 }
43 
44 // Collect for the given module the list of function it defines
45 // (GUID -> Summary).
46 void ModuleSummaryIndex::collectDefinedFunctionsForModule(
47     StringRef ModulePath, GVSummaryMapTy &GVSummaryMap) const {
48   for (auto &GlobalList : *this) {
49     auto GUID = GlobalList.first;
50     for (auto &GlobSummary : GlobalList.second.SummaryList) {
51       auto *Summary = dyn_cast_or_null<FunctionSummary>(GlobSummary.get());
52       if (!Summary)
53         // Ignore global variable, focus on functions
54         continue;
55       // Ignore summaries from other modules.
56       if (Summary->modulePath() != ModulePath)
57         continue;
58       GVSummaryMap[GUID] = Summary;
59     }
60   }
61 }
62 
63 // Collect for each module the list of function it defines (GUID -> Summary).
64 void ModuleSummaryIndex::collectDefinedGVSummariesPerModule(
65     StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries) const {
66   for (auto &GlobalList : *this) {
67     auto GUID = GlobalList.first;
68     for (auto &Summary : GlobalList.second.SummaryList) {
69       ModuleToDefinedGVSummaries[Summary->modulePath()][GUID] = Summary.get();
70     }
71   }
72 }
73 
74 GlobalValueSummary *
75 ModuleSummaryIndex::getGlobalValueSummary(uint64_t ValueGUID,
76                                           bool PerModuleIndex) const {
77   auto VI = getValueInfo(ValueGUID);
78   assert(VI && "GlobalValue not found in index");
79   assert((!PerModuleIndex || VI.getSummaryList().size() == 1) &&
80          "Expected a single entry per global value in per-module index");
81   auto &Summary = VI.getSummaryList()[0];
82   return Summary.get();
83 }
84 
85 bool ModuleSummaryIndex::isGUIDLive(GlobalValue::GUID GUID) const {
86   auto VI = getValueInfo(GUID);
87   if (!VI)
88     return true;
89   const auto &SummaryList = VI.getSummaryList();
90   if (SummaryList.empty())
91     return true;
92   for (auto &I : SummaryList)
93     if (isGlobalValueLive(I.get()))
94       return true;
95   return false;
96 }
97 
98 static void propagateConstantsToRefs(GlobalValueSummary *S) {
99   // If reference is not readonly then referenced summary is not
100   // readonly either. Note that:
101   // - All references from GlobalVarSummary are conservatively considered as
102   //   not readonly. Tracking them properly requires more complex analysis
103   //   then we have now.
104   //
105   // - AliasSummary objects have no refs at all so this function is a no-op
106   //   for them.
107   for (auto &VI : S->refs()) {
108     if (VI.isReadOnly()) {
109       // We only mark refs as readonly when computing function summaries on
110       // analysis phase.
111       assert(isa<FunctionSummary>(S));
112       continue;
113     }
114     for (auto &Ref : VI.getSummaryList())
115       // If references to alias is not readonly then aliasee is not readonly
116       if (auto *GVS = dyn_cast<GlobalVarSummary>(Ref->getBaseObject()))
117         GVS->setReadOnly(false);
118   }
119 }
120 
121 // Do the constant propagation in combined index.
122 // The goal of constant propagation is internalization of readonly
123 // variables. To determine which variables are readonly and which
124 // are not we take following steps:
125 // - During analysis we speculatively assign readonly attribute to
126 //   all variables which can be internalized. When computing function
127 //   summary we also assign readonly attribute to a reference if
128 //   function doesn't modify referenced variable.
129 //
130 // - After computing dead symbols in combined index we do the constant
131 //   propagation. During this step we clear readonly attribute from
132 //   all variables which:
133 //   a. are dead, preserved or can't be imported
134 //   b. referenced by any global variable initializer
135 //   c. referenced by a function and reference is not readonly
136 //
137 // Internalization itself happens in the backend after import is finished
138 // See internalizeImmutableGVs.
139 void ModuleSummaryIndex::propagateConstants(
140     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
141   for (auto &P : *this)
142     for (auto &S : P.second.SummaryList) {
143       if (!isGlobalValueLive(S.get()))
144         // We don't examine references from dead objects
145         continue;
146 
147       // Global variable can't be marked read only if it is not eligible
148       // to import since we need to ensure that all external references
149       // get a local (imported) copy. It also can't be marked read only
150       // if it or any alias (since alias points to the same memory) are
151       // preserved or notEligibleToImport, since either of those means
152       // there could be writes that are not visible (because preserved
153       // means it could have external to DSO writes, and notEligibleToImport
154       // means it could have writes via inline assembly leading it to be
155       // in the @llvm.*used).
156       if (auto *GVS = dyn_cast<GlobalVarSummary>(S->getBaseObject()))
157         // Here we intentionally pass S.get() not GVS, because S could be
158         // an alias.
159         if (!canImportGlobalVar(S.get()) || GUIDPreservedSymbols.count(P.first))
160           GVS->setReadOnly(false);
161       propagateConstantsToRefs(S.get());
162     }
163 }
164 
165 // TODO: write a graphviz dumper for SCCs (see ModuleSummaryIndex::exportToDot)
166 // then delete this function and update its tests
167 LLVM_DUMP_METHOD
168 void ModuleSummaryIndex::dumpSCCs(raw_ostream &O) {
169   for (scc_iterator<ModuleSummaryIndex *> I =
170            scc_begin<ModuleSummaryIndex *>(this);
171        !I.isAtEnd(); ++I) {
172     O << "SCC (" << utostr(I->size()) << " node" << (I->size() == 1 ? "" : "s")
173       << ") {\n";
174     for (const ValueInfo V : *I) {
175       FunctionSummary *F = nullptr;
176       if (V.getSummaryList().size())
177         F = cast<FunctionSummary>(V.getSummaryList().front().get());
178       O << " " << (F == nullptr ? "External" : "") << " " << utostr(V.getGUID())
179         << (I.hasLoop() ? " (has loop)" : "") << "\n";
180     }
181     O << "}\n";
182   }
183 }
184 
185 namespace {
186 struct Attributes {
187   void add(const Twine &Name, const Twine &Value,
188            const Twine &Comment = Twine());
189   void addComment(const Twine &Comment);
190   std::string getAsString() const;
191 
192   std::vector<std::string> Attrs;
193   std::string Comments;
194 };
195 
196 struct Edge {
197   uint64_t SrcMod;
198   int Hotness;
199   GlobalValue::GUID Src;
200   GlobalValue::GUID Dst;
201 };
202 }
203 
204 void Attributes::add(const Twine &Name, const Twine &Value,
205                      const Twine &Comment) {
206   std::string A = Name.str();
207   A += "=\"";
208   A += Value.str();
209   A += "\"";
210   Attrs.push_back(A);
211   addComment(Comment);
212 }
213 
214 void Attributes::addComment(const Twine &Comment) {
215   if (!Comment.isTriviallyEmpty()) {
216     if (Comments.empty())
217       Comments = " // ";
218     else
219       Comments += ", ";
220     Comments += Comment.str();
221   }
222 }
223 
224 std::string Attributes::getAsString() const {
225   if (Attrs.empty())
226     return "";
227 
228   std::string Ret = "[";
229   for (auto &A : Attrs)
230     Ret += A + ",";
231   Ret.pop_back();
232   Ret += "];";
233   Ret += Comments;
234   return Ret;
235 }
236 
237 static std::string linkageToString(GlobalValue::LinkageTypes LT) {
238   switch (LT) {
239   case GlobalValue::ExternalLinkage:
240     return "extern";
241   case GlobalValue::AvailableExternallyLinkage:
242     return "av_ext";
243   case GlobalValue::LinkOnceAnyLinkage:
244     return "linkonce";
245   case GlobalValue::LinkOnceODRLinkage:
246     return "linkonce_odr";
247   case GlobalValue::WeakAnyLinkage:
248     return "weak";
249   case GlobalValue::WeakODRLinkage:
250     return "weak_odr";
251   case GlobalValue::AppendingLinkage:
252     return "appending";
253   case GlobalValue::InternalLinkage:
254     return "internal";
255   case GlobalValue::PrivateLinkage:
256     return "private";
257   case GlobalValue::ExternalWeakLinkage:
258     return "extern_weak";
259   case GlobalValue::CommonLinkage:
260     return "common";
261   }
262 
263   return "<unknown>";
264 }
265 
266 static std::string fflagsToString(FunctionSummary::FFlags F) {
267   auto FlagValue = [](unsigned V) { return V ? '1' : '0'; };
268   char FlagRep[] = {FlagValue(F.ReadNone),     FlagValue(F.ReadOnly),
269                     FlagValue(F.NoRecurse),    FlagValue(F.ReturnDoesNotAlias),
270                     FlagValue(F.NoInline), 0};
271 
272   return FlagRep;
273 }
274 
275 // Get string representation of function instruction count and flags.
276 static std::string getSummaryAttributes(GlobalValueSummary* GVS) {
277   auto *FS = dyn_cast_or_null<FunctionSummary>(GVS);
278   if (!FS)
279     return "";
280 
281   return std::string("inst: ") + std::to_string(FS->instCount()) +
282          ", ffl: " + fflagsToString(FS->fflags());
283 }
284 
285 static std::string getNodeVisualName(GlobalValue::GUID Id) {
286   return std::string("@") + std::to_string(Id);
287 }
288 
289 static std::string getNodeVisualName(const ValueInfo &VI) {
290   return VI.name().empty() ? getNodeVisualName(VI.getGUID()) : VI.name().str();
291 }
292 
293 static std::string getNodeLabel(const ValueInfo &VI, GlobalValueSummary *GVS) {
294   if (isa<AliasSummary>(GVS))
295     return getNodeVisualName(VI);
296 
297   std::string Attrs = getSummaryAttributes(GVS);
298   std::string Label =
299       getNodeVisualName(VI) + "|" + linkageToString(GVS->linkage());
300   if (!Attrs.empty())
301     Label += std::string(" (") + Attrs + ")";
302   Label += "}";
303 
304   return Label;
305 }
306 
307 // Write definition of external node, which doesn't have any
308 // specific module associated with it. Typically this is function
309 // or variable defined in native object or library.
310 static void defineExternalNode(raw_ostream &OS, const char *Pfx,
311                                const ValueInfo &VI, GlobalValue::GUID Id) {
312   auto StrId = std::to_string(Id);
313   OS << "  " << StrId << " [label=\"";
314 
315   if (VI) {
316     OS << getNodeVisualName(VI);
317   } else {
318     OS << getNodeVisualName(Id);
319   }
320   OS << "\"]; // defined externally\n";
321 }
322 
323 static bool hasReadOnlyFlag(const GlobalValueSummary *S) {
324   if (auto *GVS = dyn_cast<GlobalVarSummary>(S))
325     return GVS->isReadOnly();
326   return false;
327 }
328 
329 void ModuleSummaryIndex::exportToDot(raw_ostream &OS) const {
330   std::vector<Edge> CrossModuleEdges;
331   DenseMap<GlobalValue::GUID, std::vector<uint64_t>> NodeMap;
332   StringMap<GVSummaryMapTy> ModuleToDefinedGVS;
333   collectDefinedGVSummariesPerModule(ModuleToDefinedGVS);
334 
335   // Get node identifier in form MXXX_<GUID>. The MXXX prefix is required,
336   // because we may have multiple linkonce functions summaries.
337   auto NodeId = [](uint64_t ModId, GlobalValue::GUID Id) {
338     return ModId == (uint64_t)-1 ? std::to_string(Id)
339                                  : std::string("M") + std::to_string(ModId) +
340                                        "_" + std::to_string(Id);
341   };
342 
343   auto DrawEdge = [&](const char *Pfx, uint64_t SrcMod, GlobalValue::GUID SrcId,
344                       uint64_t DstMod, GlobalValue::GUID DstId,
345                       int TypeOrHotness) {
346     // 0 - alias
347     // 1 - reference
348     // 2 - constant reference
349     // Other value: (hotness - 3).
350     TypeOrHotness += 3;
351     static const char *EdgeAttrs[] = {
352         " [style=dotted]; // alias",
353         " [style=dashed]; // ref",
354         " [style=dashed,color=forestgreen]; // const-ref",
355         " // call (hotness : Unknown)",
356         " [color=blue]; // call (hotness : Cold)",
357         " // call (hotness : None)",
358         " [color=brown]; // call (hotness : Hot)",
359         " [style=bold,color=red]; // call (hotness : Critical)"};
360 
361     assert(static_cast<size_t>(TypeOrHotness) <
362            sizeof(EdgeAttrs) / sizeof(EdgeAttrs[0]));
363     OS << Pfx << NodeId(SrcMod, SrcId) << " -> " << NodeId(DstMod, DstId)
364        << EdgeAttrs[TypeOrHotness] << "\n";
365   };
366 
367   OS << "digraph Summary {\n";
368   for (auto &ModIt : ModuleToDefinedGVS) {
369     auto ModId = getModuleId(ModIt.first());
370     OS << "  // Module: " << ModIt.first() << "\n";
371     OS << "  subgraph cluster_" << std::to_string(ModId) << " {\n";
372     OS << "    style = filled;\n";
373     OS << "    color = lightgrey;\n";
374     OS << "    label = \"" << sys::path::filename(ModIt.first()) << "\";\n";
375     OS << "    node [style=filled,fillcolor=lightblue];\n";
376 
377     auto &GVSMap = ModIt.second;
378     auto Draw = [&](GlobalValue::GUID IdFrom, GlobalValue::GUID IdTo, int Hotness) {
379       if (!GVSMap.count(IdTo)) {
380         CrossModuleEdges.push_back({ModId, Hotness, IdFrom, IdTo});
381         return;
382       }
383       DrawEdge("    ", ModId, IdFrom, ModId, IdTo, Hotness);
384     };
385 
386     for (auto &SummaryIt : GVSMap) {
387       NodeMap[SummaryIt.first].push_back(ModId);
388       auto Flags = SummaryIt.second->flags();
389       Attributes A;
390       if (isa<FunctionSummary>(SummaryIt.second)) {
391         A.add("shape", "record", "function");
392       } else if (isa<AliasSummary>(SummaryIt.second)) {
393         A.add("style", "dotted,filled", "alias");
394         A.add("shape", "box");
395       } else {
396         A.add("shape", "Mrecord", "variable");
397         if (Flags.Live && hasReadOnlyFlag(SummaryIt.second))
398           A.addComment("immutable");
399       }
400 
401       auto VI = getValueInfo(SummaryIt.first);
402       A.add("label", getNodeLabel(VI, SummaryIt.second));
403       if (!Flags.Live)
404         A.add("fillcolor", "red", "dead");
405       else if (Flags.NotEligibleToImport)
406         A.add("fillcolor", "yellow", "not eligible to import");
407 
408       OS << "    " << NodeId(ModId, SummaryIt.first) << " " << A.getAsString()
409          << "\n";
410     }
411     OS << "    // Edges:\n";
412 
413     for (auto &SummaryIt : GVSMap) {
414       auto *GVS = SummaryIt.second;
415       for (auto &R : GVS->refs())
416         Draw(SummaryIt.first, R.getGUID(), R.isReadOnly() ? -1 : -2);
417 
418       if (auto *AS = dyn_cast_or_null<AliasSummary>(SummaryIt.second)) {
419         GlobalValue::GUID AliaseeId;
420         if (AS->hasAliaseeGUID())
421           AliaseeId = AS->getAliaseeGUID();
422         else {
423           auto AliaseeOrigId = AS->getAliasee().getOriginalName();
424           AliaseeId = getGUIDFromOriginalID(AliaseeOrigId);
425           if (!AliaseeId)
426             AliaseeId = AliaseeOrigId;
427         }
428 
429         Draw(SummaryIt.first, AliaseeId, -3);
430         continue;
431       }
432 
433       if (auto *FS = dyn_cast_or_null<FunctionSummary>(SummaryIt.second))
434         for (auto &CGEdge : FS->calls())
435           Draw(SummaryIt.first, CGEdge.first.getGUID(),
436                static_cast<int>(CGEdge.second.Hotness));
437     }
438     OS << "  }\n";
439   }
440 
441   OS << "  // Cross-module edges:\n";
442   for (auto &E : CrossModuleEdges) {
443     auto &ModList = NodeMap[E.Dst];
444     if (ModList.empty()) {
445       defineExternalNode(OS, "  ", getValueInfo(E.Dst), E.Dst);
446       // Add fake module to the list to draw an edge to an external node
447       // in the loop below.
448       ModList.push_back(-1);
449     }
450     for (auto DstMod : ModList)
451       // The edge representing call or ref is drawn to every module where target
452       // symbol is defined. When target is a linkonce symbol there can be
453       // multiple edges representing a single call or ref, both intra-module and
454       // cross-module. As we've already drawn all intra-module edges before we
455       // skip it here.
456       if (DstMod != E.SrcMod)
457         DrawEdge("  ", E.SrcMod, E.Src, DstMod, E.Dst, E.Hotness);
458   }
459 
460   OS << "}";
461 }
462