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