23 #define DEBUG_TYPE "module-summary-index" 26 "Number of live global variables marked read only");
34 [](
const std::unique_ptr<GlobalValueSummary> &Summary) {
35 return Summary->isDSOLocal();
44 unsigned ImmutableRefCnt = 0;
45 for (
int I = Refs.size() - 1;
I >= 0 && Refs[
I].isReadOnly(); --
I)
47 return ImmutableRefCnt;
54 for (
auto &GlobalList : *
this) {
55 auto GUID = GlobalList.first;
56 for (
auto &GlobSummary : GlobalList.second.SummaryList) {
57 auto *Summary = dyn_cast_or_null<FunctionSummary>(GlobSummary.get());
62 if (Summary->modulePath() != ModulePath)
64 GVSummaryMap[GUID] = Summary;
72 for (
auto &GlobalList : *
this) {
73 auto GUID = GlobalList.first;
74 for (
auto &Summary : GlobalList.second.SummaryList) {
75 ModuleToDefinedGVSummaries[Summary->modulePath()][GUID] = Summary.get();
82 bool PerModuleIndex)
const {
83 auto VI = getValueInfo(ValueGUID);
84 assert(
VI &&
"GlobalValue not found in index");
85 assert((!PerModuleIndex ||
VI.getSummaryList().size() == 1) &&
86 "Expected a single entry per global value in per-module index");
87 auto &Summary =
VI.getSummaryList()[0];
92 auto VI = getValueInfo(GUID);
95 const auto &SummaryList =
VI.getSummaryList();
96 if (SummaryList.empty())
98 for (
auto &
I : SummaryList)
99 if (isGlobalValueLive(
I.get()))
113 for (
auto &
VI : S->
refs()) {
114 if (
VI.isReadOnly()) {
117 assert(isa<FunctionSummary>(S));
120 for (
auto &
Ref :
VI.getSummaryList())
122 if (
auto *GVS = dyn_cast<GlobalVarSummary>(
Ref->getBaseObject()))
123 GVS->setReadOnly(
false);
147 for (
auto &
P : *
this)
148 for (
auto &S :
P.second.SummaryList) {
149 if (!isGlobalValueLive(S.get()))
162 if (
auto *GVS = dyn_cast<GlobalVarSummary>(S->getBaseObject()))
166 GVS->setReadOnly(
false);
170 for (
auto &
P : *
this)
171 if (
P.second.SummaryList.size())
172 if (
auto *GVS = dyn_cast<GlobalVarSummary>(
173 P.second.SummaryList[0]->getBaseObject()))
174 if (isGlobalValueLive(GVS) && GVS->isReadOnly())
183 scc_begin<ModuleSummaryIndex *>(
this);
185 O <<
"SCC (" <<
utostr(
I->size()) <<
" node" << (
I->size() == 1 ?
"" :
"s")
189 if (V.getSummaryList().size())
190 F = cast<FunctionSummary>(V.getSummaryList().front().get());
191 O <<
" " << (F ==
nullptr ?
"External" :
"") <<
" " <<
utostr(V.getGUID())
192 << (I.hasLoop() ?
" (has loop)" :
"") <<
"\n";
202 void addComment(
const Twine &Comment);
203 std::string getAsString()
const;
205 std::vector<std::string>
Attrs;
206 std::string Comments;
218 const Twine &Comment) {
219 std::string A = Name.
str();
227 void Attributes::addComment(
const Twine &Comment) {
229 if (Comments.empty())
233 Comments += Comment.
str();
237 std::string Attributes::getAsString()
const {
241 std::string
Ret =
"[";
242 for (
auto &A :
Attrs)
259 return "linkonce_odr";
271 return "extern_weak";
280 auto FlagValue = [](
unsigned V) {
return V ?
'1' :
'0'; };
290 auto *FS = dyn_cast_or_null<FunctionSummary>(GVS);
307 if (isa<AliasSummary>(GVS))
314 Label += std::string(
" (") + Attrs +
")";
326 OS <<
" " << StrId <<
" [label=\"";
333 OS <<
"\"]; // defined externally\n";
337 if (
auto *GVS = dyn_cast<GlobalVarSummary>(S))
338 return GVS->isReadOnly();
343 std::vector<Edge> CrossModuleEdges;
346 collectDefinedGVSummariesPerModule(ModuleToDefinedGVS);
364 static const char *EdgeAttrs[] = {
365 " [style=dotted]; // alias",
366 " [style=dashed]; // ref",
367 " [style=dashed,color=forestgreen]; // const-ref",
368 " // call (hotness : Unknown)",
369 " [color=blue]; // call (hotness : Cold)",
370 " // call (hotness : None)",
371 " [color=brown]; // call (hotness : Hot)",
372 " [style=bold,color=red]; // call (hotness : Critical)"};
374 assert(static_cast<size_t>(TypeOrHotness) <
375 sizeof(EdgeAttrs) /
sizeof(EdgeAttrs[0]));
376 OS << Pfx <<
NodeId(SrcMod, SrcId) <<
" -> " <<
NodeId(DstMod, DstId)
377 << EdgeAttrs[TypeOrHotness] <<
"\n";
380 OS <<
"digraph Summary {\n";
381 for (
auto &ModIt : ModuleToDefinedGVS) {
382 auto ModId = getModuleId(ModIt.first());
383 OS <<
" // Module: " << ModIt.first() <<
"\n";
385 OS <<
" style = filled;\n";
386 OS <<
" color = lightgrey;\n";
388 OS <<
" node [style=filled,fillcolor=lightblue];\n";
390 auto &GVSMap = ModIt.second;
392 if (!GVSMap.count(IdTo)) {
393 CrossModuleEdges.push_back({ModId, Hotness, IdFrom, IdTo});
396 DrawEdge(
" ", ModId, IdFrom, ModId, IdTo, Hotness);
399 for (
auto &SummaryIt : GVSMap) {
400 NodeMap[SummaryIt.first].push_back(ModId);
401 auto Flags = SummaryIt.second->flags();
403 if (isa<FunctionSummary>(SummaryIt.second)) {
404 A.add(
"shape",
"record",
"function");
405 }
else if (isa<AliasSummary>(SummaryIt.second)) {
406 A.add(
"style",
"dotted,filled",
"alias");
407 A.add(
"shape",
"box");
409 A.add(
"shape",
"Mrecord",
"variable");
411 A.addComment(
"immutable");
414 auto VI = getValueInfo(SummaryIt.first);
417 A.add(
"fillcolor",
"red",
"dead");
418 else if (Flags.NotEligibleToImport)
419 A.add(
"fillcolor",
"yellow",
"not eligible to import");
421 OS <<
" " <<
NodeId(ModId, SummaryIt.first) <<
" " << A.getAsString()
424 OS <<
" // Edges:\n";
426 for (
auto &SummaryIt : GVSMap) {
427 auto *GVS = SummaryIt.second;
428 for (
auto &R : GVS->refs())
429 Draw(SummaryIt.first, R.getGUID(), R.isReadOnly() ? -1 : -2);
431 if (
auto *AS = dyn_cast_or_null<AliasSummary>(SummaryIt.second)) {
433 if (AS->hasAliaseeGUID())
434 AliaseeId = AS->getAliaseeGUID();
436 auto AliaseeOrigId = AS->getAliasee().getOriginalName();
437 AliaseeId = getGUIDFromOriginalID(AliaseeOrigId);
439 AliaseeId = AliaseeOrigId;
442 Draw(SummaryIt.first, AliaseeId, -3);
446 if (
auto *FS = dyn_cast_or_null<FunctionSummary>(SummaryIt.second))
447 for (
auto &CGEdge : FS->calls())
448 Draw(SummaryIt.first, CGEdge.first.getGUID(),
449 static_cast<int>(CGEdge.second.Hotness));
454 OS <<
" // Cross-module edges:\n";
455 for (
auto &
E : CrossModuleEdges) {
456 auto &ModList = NodeMap[
E.Dst];
457 if (ModList.empty()) {
461 ModList.push_back(-1);
463 for (
auto DstMod : ModList)
469 if (DstMod !=
E.SrcMod)
470 DrawEdge(
" ",
E.SrcMod,
E.Src, DstMod,
E.Dst,
E.Hotness);
This builds on the llvm/ADT/GraphTraits.h file to find the strongly connected components (SCCs) of a ...
void collectDefinedGVSummariesPerModule(StringMap< GVSummaryMapTy > &ModuleToDefinedGVSummaries) const
Collect for each module the list of Summaries it defines (GUID -> Summary).
Special purpose, only applies to global arrays.
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
This class represents lattice values for constants.
static std::string linkageToString(GlobalValue::LinkageTypes LT)
Same, but only replaced by something equivalent.
GlobalValueSummary * getGlobalValueSummary(const GlobalValue &GV, bool PerModuleIndex=true) const
Returns the first GlobalValueSummary for GV, asserting that there is only one if PerModuleIndex.
Implements a dense probed hash-table based set.
Available for inspection, not emission.
ArrayRef< ValueInfo > refs() const
Return the list of values referenced by this global value definition.
bool isTriviallyEmpty() const
Check if this twine is trivially empty; a false return value does not necessarily mean the twine is e...
Like Internal, but omit from symbol table.
Externally visible function.
GlobalValue::LinkageTypes linkage() const
Return linkage type recorded for this global value.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
STATISTIC(NumFunctions, "Total number of functions")
GlobalValue::GUID getGUID() const
amdgpu Simplify well known AMD library false Value Value const Twine & Name
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
static void propagateConstantsToRefs(GlobalValueSummary *S)
The access may reference the value stored in memory.
static bool canImportGlobalVar(GlobalValueSummary *S)
static std::string getSummaryAttributes(GlobalValueSummary *GVS)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
void propagateConstants(const DenseSet< GlobalValue::GUID > &PreservedSymbols)
Analyze index and detect unmodified globals.
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
ArrayRef< std::unique_ptr< GlobalValueSummary > > getSummaryList() const
static bool hasReadOnlyFlag(const GlobalValueSummary *S)
ExternalWeak linkage description.
Same, but only replaced by something equivalent.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
unsigned immutableRefCount() const
bool isGUIDLive(GlobalValue::GUID GUID) const
Function and variable summary information to aid decisions and implementation of importing.
Struct that holds a reference to a particular GUID in a global value summary.
static std::string fflagsToString(FunctionSummary::FFlags F)
Keep one copy of function when linking (inline)
std::string utostr(uint64_t X, bool isNeg=false)
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
static FunctionSummary makeDummyFunctionSummary(std::vector< FunctionSummary::EdgeTy > Edges)
Create an empty FunctionSummary (with specified call edges).
LinkageTypes
An enumeration for the kinds of linkage for global values.
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
void exportToDot(raw_ostream &OS) const
Export summary to dot file for GraphViz.
static std::string getNodeLabel(const ValueInfo &VI, GlobalValueSummary *GVS)
void dumpSCCs(raw_ostream &OS)
Print out strongly connected components for debugging.
static void defineExternalNode(raw_ostream &OS, const char *Pfx, const ValueInfo &VI, GlobalValue::GUID Id)
Keep one copy of named function when linking (weak)
Rename collisions when linking (static functions).
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Flags specific to function summaries.
Function summary information to aid decisions and implementation of importing.
std::string str() const
Return the twine contents as a std::string.
static std::string getNodeVisualName(GlobalValue::GUID Id)
const std::string to_string(const T &Value)
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
This class implements an extremely fast bulk output stream that can only output to a stream...
StringRef - Represent a constant reference to a string, i.e.
unsigned ReturnDoesNotAlias
bool AreStatisticsEnabled()
Check if statistics are enabled.
void collectDefinedFunctionsForModule(StringRef ModulePath, GVSummaryMapTy &GVSummaryMap) const
Collect for the given module the list of functions it defines (GUID -> Summary).
Enumerate the SCCs of a directed graph in reverse topological order of the SCC DAG.
static FunctionSummary ExternalNode
A dummy node to reference external functions that aren't in the index.