23 #define DEBUG_TYPE "module-summary-index" 26 "Number of live global variables marked read only");
28 "Number of live global variables marked write only");
32 cl::desc(
"Propagate attributes in index"));
41 [](
const std::unique_ptr<GlobalValueSummary> &Summary) {
42 return Summary->isDSOLocal();
50 [](
const std::unique_ptr<GlobalValueSummary> &Summary) {
51 return Summary->canAutoHide();
60 unsigned RORefCnt = 0, WORefCnt = 0;
62 for (I = Refs.size() - 1; I >= 0 && Refs[
I].isWriteOnly(); --
I)
64 for (; I >= 0 && Refs[
I].isReadOnly(); --
I)
66 return {RORefCnt, WORefCnt};
75 for (
auto &GlobalList : *
this) {
76 auto GUID = GlobalList.first;
77 for (
auto &GlobSummary : GlobalList.second.SummaryList) {
78 auto *Summary = dyn_cast_or_null<FunctionSummary>(GlobSummary.get());
83 if (Summary->modulePath() != ModulePath)
85 GVSummaryMap[GUID] = Summary;
92 bool PerModuleIndex)
const {
93 auto VI = getValueInfo(ValueGUID);
94 assert(
VI &&
"GlobalValue not found in index");
95 assert((!PerModuleIndex ||
VI.getSummaryList().size() == 1) &&
96 "Expected a single entry per global value in per-module index");
97 auto &Summary =
VI.getSummaryList()[0];
102 auto VI = getValueInfo(GUID);
105 const auto &SummaryList =
VI.getSummaryList();
106 if (SummaryList.empty())
108 for (
auto &
I : SummaryList)
109 if (isGlobalValueLive(
I.get()))
123 for (
auto &
VI : S->
refs()) {
124 assert(
VI.getAccessSpecifier() == 0 || isa<FunctionSummary>(S));
125 for (
auto &
Ref :
VI.getSummaryList())
128 if (
auto *GVS = dyn_cast<GlobalVarSummary>(
Ref->getBaseObject())) {
129 if (!
VI.isReadOnly())
130 GVS->setReadOnly(
false);
131 if (!
VI.isWriteOnly())
132 GVS->setWriteOnly(
false);
167 for (
auto &
P : *
this)
168 for (
auto &S :
P.second.SummaryList) {
169 if (!isGlobalValueLive(S.get()))
182 if (
auto *GVS = dyn_cast<GlobalVarSummary>(S->getBaseObject()))
186 if (!canImportGlobalVar(S.get(),
false) ||
187 GUIDPreservedSymbols.
count(
P.first)) {
188 GVS->setReadOnly(
false);
189 GVS->setWriteOnly(
false);
193 setWithAttributePropagation();
195 for (
auto &
P : *
this)
196 if (
P.second.SummaryList.size())
197 if (
auto *GVS = dyn_cast<GlobalVarSummary>(
198 P.second.SummaryList[0]->getBaseObject()))
199 if (isGlobalValueLive(GVS)) {
200 if (GVS->maybeReadOnly())
202 if (GVS->maybeWriteOnly())
203 WriteOnlyLiveGVars++;
208 bool AnalyzeRefs)
const {
235 (!AnalyzeRefs || !HasRefsPreventingImport(GVS));
243 scc_begin<ModuleSummaryIndex *>(
this);
245 O <<
"SCC (" <<
utostr(
I->size()) <<
" node" << (
I->size() == 1 ?
"" :
"s")
249 if (V.getSummaryList().size())
250 F = cast<FunctionSummary>(V.getSummaryList().front().get());
251 O <<
" " << (F ==
nullptr ?
"External" :
"") <<
" " <<
utostr(V.getGUID())
252 << (I.hasLoop() ?
" (has loop)" :
"") <<
"\n";
262 void addComment(
const Twine &Comment);
263 std::string getAsString()
const;
265 std::vector<std::string>
Attrs;
266 std::string Comments;
278 const Twine &Comment) {
279 std::string A = Name.
str();
287 void Attributes::addComment(
const Twine &Comment) {
289 if (Comments.empty())
293 Comments += Comment.
str();
297 std::string Attributes::getAsString()
const {
301 std::string
Ret =
"[";
302 for (
auto &A :
Attrs)
319 return "linkonce_odr";
331 return "extern_weak";
340 auto FlagValue = [](
unsigned V) {
return V ?
'1' :
'0'; };
350 auto *
FS = dyn_cast_or_null<FunctionSummary>(GVS);
367 if (isa<AliasSummary>(GVS))
374 Label += std::string(
" (") + Attrs +
")";
386 OS <<
" " << StrId <<
" [label=\"";
393 OS <<
"\"]; // defined externally\n";
397 if (
auto *GVS = dyn_cast<GlobalVarSummary>(S))
398 return GVS->maybeReadOnly();
403 if (
auto *GVS = dyn_cast<GlobalVarSummary>(S))
404 return GVS->maybeWriteOnly();
411 std::vector<Edge> CrossModuleEdges;
413 using GVSOrderedMapTy = std::map<GlobalValue::GUID, GlobalValueSummary *>;
414 std::map<StringRef, GVSOrderedMapTy> ModuleToDefinedGVS;
415 collectDefinedGVSummariesPerModule(ModuleToDefinedGVS);
434 static const char *EdgeAttrs[] = {
435 " [style=dotted]; // alias",
436 " [style=dashed]; // ref",
437 " [style=dashed,color=forestgreen]; // const-ref",
438 " [style=dashed,color=violetred]; // writeOnly-ref",
439 " // call (hotness : Unknown)",
440 " [color=blue]; // call (hotness : Cold)",
441 " // call (hotness : None)",
442 " [color=brown]; // call (hotness : Hot)",
443 " [style=bold,color=red]; // call (hotness : Critical)"};
445 assert(static_cast<size_t>(TypeOrHotness) <
446 sizeof(EdgeAttrs) /
sizeof(EdgeAttrs[0]));
447 OS << Pfx <<
NodeId(SrcMod, SrcId) <<
" -> " <<
NodeId(DstMod, DstId)
448 << EdgeAttrs[TypeOrHotness] <<
"\n";
451 OS <<
"digraph Summary {\n";
452 for (
auto &ModIt : ModuleToDefinedGVS) {
453 auto ModId = getModuleId(ModIt.first);
454 OS <<
" // Module: " << ModIt.first <<
"\n";
456 OS <<
" style = filled;\n";
457 OS <<
" color = lightgrey;\n";
459 OS <<
" node [style=filled,fillcolor=lightblue];\n";
461 auto &GVSMap = ModIt.second;
463 if (!GVSMap.count(IdTo)) {
464 CrossModuleEdges.push_back({ModId, Hotness, IdFrom, IdTo});
467 DrawEdge(
" ", ModId, IdFrom, ModId, IdTo, Hotness);
470 for (
auto &SummaryIt : GVSMap) {
471 NodeMap[SummaryIt.first].push_back(ModId);
472 auto Flags = SummaryIt.second->flags();
474 if (isa<FunctionSummary>(SummaryIt.second)) {
475 A.add(
"shape",
"record",
"function");
476 }
else if (isa<AliasSummary>(SummaryIt.second)) {
477 A.add(
"style",
"dotted,filled",
"alias");
478 A.add(
"shape",
"box");
480 A.add(
"shape",
"Mrecord",
"variable");
482 A.addComment(
"immutable");
484 A.addComment(
"writeOnly");
487 A.addComment(
"dsoLocal");
488 if (
Flags.CanAutoHide)
489 A.addComment(
"canAutoHide");
490 if (GUIDPreservedSymbols.
count(SummaryIt.first))
491 A.addComment(
"preserved");
493 auto VI = getValueInfo(SummaryIt.first);
496 A.add(
"fillcolor",
"red",
"dead");
497 else if (
Flags.NotEligibleToImport)
498 A.add(
"fillcolor",
"yellow",
"not eligible to import");
500 OS <<
" " <<
NodeId(ModId, SummaryIt.first) <<
" " << A.getAsString()
503 OS <<
" // Edges:\n";
505 for (
auto &SummaryIt : GVSMap) {
506 auto *GVS = SummaryIt.second;
507 for (
auto &R : GVS->refs())
508 Draw(SummaryIt.first, R.getGUID(),
509 R.isWriteOnly() ? -1 : (R.isReadOnly() ? -2 : -3));
511 if (
auto *AS = dyn_cast_or_null<AliasSummary>(SummaryIt.second)) {
512 Draw(SummaryIt.first, AS->getAliaseeGUID(), -4);
516 if (
auto *
FS = dyn_cast_or_null<FunctionSummary>(SummaryIt.second))
517 for (
auto &CGEdge :
FS->calls())
518 Draw(SummaryIt.first, CGEdge.first.getGUID(),
519 static_cast<int>(CGEdge.second.Hotness));
524 OS <<
" // Cross-module edges:\n";
525 for (
auto &
E : CrossModuleEdges) {
526 auto &ModList = NodeMap[
E.Dst];
527 if (ModList.empty()) {
531 ModList.push_back(-1);
533 for (
auto DstMod : ModList)
539 if (DstMod !=
E.SrcMod)
540 DrawEdge(
" ",
E.SrcMod,
E.Src, DstMod,
E.Dst,
E.Hotness);
static bool isInterposableLinkage(LinkageTypes Linkage)
Whether the definition of this global may be replaced by something non-equivalent at link time...
This builds on the llvm/ADT/GraphTraits.h file to find the strongly connected components (SCCs) of a ...
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.
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
static std::string linkageToString(GlobalValue::LinkageTypes LT)
Same, but only replaced by something equivalent.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
GlobalValueSummary * getGlobalValueSummary(const GlobalValue &GV, bool PerModuleIndex=true) const
Returns the first GlobalValueSummary for GV, asserting that there is only one if PerModuleIndex.
Global variable summary information to aid decisions and implementation of importing.
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
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
The access may reference the value stored in memory.
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
static std::string getSummaryAttributes(GlobalValueSummary *GVS)
bool canAutoHide() const
Checks if all copies are eligible for auto-hiding (have flag set).
static constexpr uint64_t BitcodeSummaryVersion
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.
initializer< Ty > init(const Ty &Val)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
bool isGUIDLive(GlobalValue::GUID GUID) const
static void propagateAttributesToRefs(GlobalValueSummary *S)
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.
void exportToDot(raw_ostream &OS, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols) const
Export summary to dot file for GraphViz.
static std::string fflagsToString(FunctionSummary::FFlags F)
GlobalValueSummary * getBaseObject()
If this is an alias summary, returns the summary of the aliased object (a global variable or function...
static bool hasWriteOnlyFlag(const GlobalValueSummary *S)
static uint64_t add(uint64_t LeftOp, uint64_t RightOp)
Keep one copy of function when linking (inline)
std::string utostr(uint64_t X, bool isNeg=false)
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.
void propagateAttributes(const DenseSet< GlobalValue::GUID > &PreservedSymbols)
Analyze index and detect unmodified globals.
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
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)
bool notEligibleToImport() const
Return true if this global value can't be imported.
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.
bool canImportGlobalVar(GlobalValueSummary *S, bool AnalyzeRefs) const
Checks if we can import global variable from another module.
This class implements an extremely fast bulk output stream that can only output to a stream...
static cl::opt< bool > PropagateAttrs("propagate-attrs", cl::init(true), cl::Hidden, cl::desc("Propagate attributes in index"))
StringRef - Represent a constant reference to a string, i.e.
unsigned ReturnDoesNotAlias
std::pair< unsigned, unsigned > specialRefCounts() const
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.