xref: /freebsd/contrib/llvm-project/llvm/lib/IR/ModuleSummaryIndex.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===-- ModuleSummaryIndex.cpp - Module Summary Index ---------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements the module index and summary classes for the
100b57cec5SDimitry Andric // IR library.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "llvm/IR/ModuleSummaryIndex.h"
150b57cec5SDimitry Andric #include "llvm/ADT/SCCIterator.h"
160b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
17480093f4SDimitry Andric #include "llvm/Support/CommandLine.h"
180b57cec5SDimitry Andric #include "llvm/Support/Path.h"
190b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
200b57cec5SDimitry Andric using namespace llvm;
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric #define DEBUG_TYPE "module-summary-index"
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric STATISTIC(ReadOnlyLiveGVars,
250b57cec5SDimitry Andric           "Number of live global variables marked read only");
260b57cec5SDimitry Andric STATISTIC(WriteOnlyLiveGVars,
270b57cec5SDimitry Andric           "Number of live global variables marked write only");
280b57cec5SDimitry Andric 
29480093f4SDimitry Andric static cl::opt<bool> PropagateAttrs("propagate-attrs", cl::init(true),
30480093f4SDimitry Andric                                     cl::Hidden,
31480093f4SDimitry Andric                                     cl::desc("Propagate attributes in index"));
32480093f4SDimitry Andric 
335ffd83dbSDimitry Andric static cl::opt<bool> ImportConstantsWithRefs(
345ffd83dbSDimitry Andric     "import-constants-with-refs", cl::init(true), cl::Hidden,
355ffd83dbSDimitry Andric     cl::desc("Import constant global variables with references"));
365ffd83dbSDimitry Andric 
375ffd83dbSDimitry Andric constexpr uint32_t FunctionSummary::ParamAccess::RangeWidth;
385ffd83dbSDimitry Andric 
390b57cec5SDimitry Andric FunctionSummary FunctionSummary::ExternalNode =
400b57cec5SDimitry Andric     FunctionSummary::makeDummyFunctionSummary({});
410b57cec5SDimitry Andric 
getELFVisibility() const42fe6060f1SDimitry Andric GlobalValue::VisibilityTypes ValueInfo::getELFVisibility() const {
43fe6060f1SDimitry Andric   bool HasProtected = false;
44fe6060f1SDimitry Andric   for (const auto &S : make_pointee_range(getSummaryList())) {
45fe6060f1SDimitry Andric     if (S.getVisibility() == GlobalValue::HiddenVisibility)
46fe6060f1SDimitry Andric       return GlobalValue::HiddenVisibility;
47fe6060f1SDimitry Andric     if (S.getVisibility() == GlobalValue::ProtectedVisibility)
48fe6060f1SDimitry Andric       HasProtected = true;
49fe6060f1SDimitry Andric   }
50fe6060f1SDimitry Andric   return HasProtected ? GlobalValue::ProtectedVisibility
51fe6060f1SDimitry Andric                       : GlobalValue::DefaultVisibility;
52fe6060f1SDimitry Andric }
53fe6060f1SDimitry Andric 
isDSOLocal(bool WithDSOLocalPropagation) const54fe6060f1SDimitry Andric bool ValueInfo::isDSOLocal(bool WithDSOLocalPropagation) const {
55fe6060f1SDimitry Andric   // With DSOLocal propagation done, the flag in evey summary is the same.
56fe6060f1SDimitry Andric   // Check the first one is enough.
57fe6060f1SDimitry Andric   return WithDSOLocalPropagation
58fe6060f1SDimitry Andric              ? getSummaryList().size() && getSummaryList()[0]->isDSOLocal()
59fe6060f1SDimitry Andric              : getSummaryList().size() &&
60fe6060f1SDimitry Andric                    llvm::all_of(
61fe6060f1SDimitry Andric                        getSummaryList(),
620b57cec5SDimitry Andric                        [](const std::unique_ptr<GlobalValueSummary> &Summary) {
630b57cec5SDimitry Andric                          return Summary->isDSOLocal();
640b57cec5SDimitry Andric                        });
650b57cec5SDimitry Andric }
660b57cec5SDimitry Andric 
canAutoHide() const670b57cec5SDimitry Andric bool ValueInfo::canAutoHide() const {
680b57cec5SDimitry Andric   // Can only auto hide if all copies are eligible to auto hide.
690b57cec5SDimitry Andric   return getSummaryList().size() &&
700b57cec5SDimitry Andric          llvm::all_of(getSummaryList(),
710b57cec5SDimitry Andric                       [](const std::unique_ptr<GlobalValueSummary> &Summary) {
720b57cec5SDimitry Andric                         return Summary->canAutoHide();
730b57cec5SDimitry Andric                       });
740b57cec5SDimitry Andric }
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric // Gets the number of readonly and writeonly refs in RefEdgeList
specialRefCounts() const770b57cec5SDimitry Andric std::pair<unsigned, unsigned> FunctionSummary::specialRefCounts() const {
780b57cec5SDimitry Andric   // Here we take advantage of having all readonly and writeonly references
790b57cec5SDimitry Andric   // located in the end of the RefEdgeList.
800b57cec5SDimitry Andric   auto Refs = refs();
810b57cec5SDimitry Andric   unsigned RORefCnt = 0, WORefCnt = 0;
820b57cec5SDimitry Andric   int I;
830b57cec5SDimitry Andric   for (I = Refs.size() - 1; I >= 0 && Refs[I].isWriteOnly(); --I)
840b57cec5SDimitry Andric     WORefCnt++;
850b57cec5SDimitry Andric   for (; I >= 0 && Refs[I].isReadOnly(); --I)
860b57cec5SDimitry Andric     RORefCnt++;
870b57cec5SDimitry Andric   return {RORefCnt, WORefCnt};
880b57cec5SDimitry Andric }
890b57cec5SDimitry Andric 
90480093f4SDimitry Andric constexpr uint64_t ModuleSummaryIndex::BitcodeSummaryVersion;
91480093f4SDimitry Andric 
getFlags() const925ffd83dbSDimitry Andric uint64_t ModuleSummaryIndex::getFlags() const {
935ffd83dbSDimitry Andric   uint64_t Flags = 0;
945ffd83dbSDimitry Andric   if (withGlobalValueDeadStripping())
955ffd83dbSDimitry Andric     Flags |= 0x1;
965ffd83dbSDimitry Andric   if (skipModuleByDistributedBackend())
975ffd83dbSDimitry Andric     Flags |= 0x2;
985ffd83dbSDimitry Andric   if (hasSyntheticEntryCounts())
995ffd83dbSDimitry Andric     Flags |= 0x4;
1005ffd83dbSDimitry Andric   if (enableSplitLTOUnit())
1015ffd83dbSDimitry Andric     Flags |= 0x8;
1025ffd83dbSDimitry Andric   if (partiallySplitLTOUnits())
1035ffd83dbSDimitry Andric     Flags |= 0x10;
1045ffd83dbSDimitry Andric   if (withAttributePropagation())
1055ffd83dbSDimitry Andric     Flags |= 0x20;
106fe6060f1SDimitry Andric   if (withDSOLocalPropagation())
107fe6060f1SDimitry Andric     Flags |= 0x40;
108972a253aSDimitry Andric   if (withWholeProgramVisibility())
109972a253aSDimitry Andric     Flags |= 0x80;
11006c3fb27SDimitry Andric   if (withSupportsHotColdNew())
11106c3fb27SDimitry Andric     Flags |= 0x100;
11206c3fb27SDimitry Andric   if (hasUnifiedLTO())
11306c3fb27SDimitry Andric     Flags |= 0x200;
1145ffd83dbSDimitry Andric   return Flags;
1155ffd83dbSDimitry Andric }
1165ffd83dbSDimitry Andric 
setFlags(uint64_t Flags)1175ffd83dbSDimitry Andric void ModuleSummaryIndex::setFlags(uint64_t Flags) {
11806c3fb27SDimitry Andric   assert(Flags <= 0x2ff && "Unexpected bits in flag");
1195ffd83dbSDimitry Andric   // 1 bit: WithGlobalValueDeadStripping flag.
1205ffd83dbSDimitry Andric   // Set on combined index only.
1215ffd83dbSDimitry Andric   if (Flags & 0x1)
1225ffd83dbSDimitry Andric     setWithGlobalValueDeadStripping();
1235ffd83dbSDimitry Andric   // 1 bit: SkipModuleByDistributedBackend flag.
1245ffd83dbSDimitry Andric   // Set on combined index only.
1255ffd83dbSDimitry Andric   if (Flags & 0x2)
1265ffd83dbSDimitry Andric     setSkipModuleByDistributedBackend();
1275ffd83dbSDimitry Andric   // 1 bit: HasSyntheticEntryCounts flag.
1285ffd83dbSDimitry Andric   // Set on combined index only.
1295ffd83dbSDimitry Andric   if (Flags & 0x4)
1305ffd83dbSDimitry Andric     setHasSyntheticEntryCounts();
1315ffd83dbSDimitry Andric   // 1 bit: DisableSplitLTOUnit flag.
1325ffd83dbSDimitry Andric   // Set on per module indexes. It is up to the client to validate
1335ffd83dbSDimitry Andric   // the consistency of this flag across modules being linked.
1345ffd83dbSDimitry Andric   if (Flags & 0x8)
1355ffd83dbSDimitry Andric     setEnableSplitLTOUnit();
1365ffd83dbSDimitry Andric   // 1 bit: PartiallySplitLTOUnits flag.
1375ffd83dbSDimitry Andric   // Set on combined index only.
1385ffd83dbSDimitry Andric   if (Flags & 0x10)
1395ffd83dbSDimitry Andric     setPartiallySplitLTOUnits();
1405ffd83dbSDimitry Andric   // 1 bit: WithAttributePropagation flag.
1415ffd83dbSDimitry Andric   // Set on combined index only.
1425ffd83dbSDimitry Andric   if (Flags & 0x20)
1435ffd83dbSDimitry Andric     setWithAttributePropagation();
144fe6060f1SDimitry Andric   // 1 bit: WithDSOLocalPropagation flag.
145fe6060f1SDimitry Andric   // Set on combined index only.
146fe6060f1SDimitry Andric   if (Flags & 0x40)
147fe6060f1SDimitry Andric     setWithDSOLocalPropagation();
148972a253aSDimitry Andric   // 1 bit: WithWholeProgramVisibility flag.
149972a253aSDimitry Andric   // Set on combined index only.
150972a253aSDimitry Andric   if (Flags & 0x80)
151972a253aSDimitry Andric     setWithWholeProgramVisibility();
15206c3fb27SDimitry Andric   // 1 bit: WithSupportsHotColdNew flag.
15306c3fb27SDimitry Andric   // Set on combined index only.
15406c3fb27SDimitry Andric   if (Flags & 0x100)
15506c3fb27SDimitry Andric     setWithSupportsHotColdNew();
15606c3fb27SDimitry Andric   // 1 bit: WithUnifiedLTO flag.
15706c3fb27SDimitry Andric   // Set on combined index only.
15806c3fb27SDimitry Andric   if (Flags & 0x200)
15906c3fb27SDimitry Andric     setUnifiedLTO();
1605ffd83dbSDimitry Andric }
1615ffd83dbSDimitry Andric 
1620b57cec5SDimitry Andric // Collect for the given module the list of function it defines
1630b57cec5SDimitry Andric // (GUID -> Summary).
collectDefinedFunctionsForModule(StringRef ModulePath,GVSummaryMapTy & GVSummaryMap) const1640b57cec5SDimitry Andric void ModuleSummaryIndex::collectDefinedFunctionsForModule(
1650b57cec5SDimitry Andric     StringRef ModulePath, GVSummaryMapTy &GVSummaryMap) const {
1660b57cec5SDimitry Andric   for (auto &GlobalList : *this) {
1670b57cec5SDimitry Andric     auto GUID = GlobalList.first;
1680b57cec5SDimitry Andric     for (auto &GlobSummary : GlobalList.second.SummaryList) {
1690b57cec5SDimitry Andric       auto *Summary = dyn_cast_or_null<FunctionSummary>(GlobSummary.get());
1700b57cec5SDimitry Andric       if (!Summary)
1710b57cec5SDimitry Andric         // Ignore global variable, focus on functions
1720b57cec5SDimitry Andric         continue;
1730b57cec5SDimitry Andric       // Ignore summaries from other modules.
1740b57cec5SDimitry Andric       if (Summary->modulePath() != ModulePath)
1750b57cec5SDimitry Andric         continue;
1760b57cec5SDimitry Andric       GVSummaryMap[GUID] = Summary;
1770b57cec5SDimitry Andric     }
1780b57cec5SDimitry Andric   }
1790b57cec5SDimitry Andric }
1800b57cec5SDimitry Andric 
1810b57cec5SDimitry Andric GlobalValueSummary *
getGlobalValueSummary(uint64_t ValueGUID,bool PerModuleIndex) const1820b57cec5SDimitry Andric ModuleSummaryIndex::getGlobalValueSummary(uint64_t ValueGUID,
1830b57cec5SDimitry Andric                                           bool PerModuleIndex) const {
1840b57cec5SDimitry Andric   auto VI = getValueInfo(ValueGUID);
1850b57cec5SDimitry Andric   assert(VI && "GlobalValue not found in index");
1860b57cec5SDimitry Andric   assert((!PerModuleIndex || VI.getSummaryList().size() == 1) &&
1870b57cec5SDimitry Andric          "Expected a single entry per global value in per-module index");
1880b57cec5SDimitry Andric   auto &Summary = VI.getSummaryList()[0];
1890b57cec5SDimitry Andric   return Summary.get();
1900b57cec5SDimitry Andric }
1910b57cec5SDimitry Andric 
isGUIDLive(GlobalValue::GUID GUID) const1920b57cec5SDimitry Andric bool ModuleSummaryIndex::isGUIDLive(GlobalValue::GUID GUID) const {
1930b57cec5SDimitry Andric   auto VI = getValueInfo(GUID);
1940b57cec5SDimitry Andric   if (!VI)
1950b57cec5SDimitry Andric     return true;
1960b57cec5SDimitry Andric   const auto &SummaryList = VI.getSummaryList();
1970b57cec5SDimitry Andric   if (SummaryList.empty())
1980b57cec5SDimitry Andric     return true;
1990b57cec5SDimitry Andric   for (auto &I : SummaryList)
2000b57cec5SDimitry Andric     if (isGlobalValueLive(I.get()))
2010b57cec5SDimitry Andric       return true;
2020b57cec5SDimitry Andric   return false;
2030b57cec5SDimitry Andric }
2040b57cec5SDimitry Andric 
205e8d8bef9SDimitry Andric static void
propagateAttributesToRefs(GlobalValueSummary * S,DenseSet<ValueInfo> & MarkedNonReadWriteOnly)206e8d8bef9SDimitry Andric propagateAttributesToRefs(GlobalValueSummary *S,
207e8d8bef9SDimitry Andric                           DenseSet<ValueInfo> &MarkedNonReadWriteOnly) {
2080b57cec5SDimitry Andric   // If reference is not readonly or writeonly then referenced summary is not
2090b57cec5SDimitry Andric   // read/writeonly either. Note that:
2100b57cec5SDimitry Andric   // - All references from GlobalVarSummary are conservatively considered as
2110b57cec5SDimitry Andric   //   not readonly or writeonly. Tracking them properly requires more complex
2120b57cec5SDimitry Andric   //   analysis then we have now.
2130b57cec5SDimitry Andric   //
2140b57cec5SDimitry Andric   // - AliasSummary objects have no refs at all so this function is a no-op
2150b57cec5SDimitry Andric   //   for them.
2160b57cec5SDimitry Andric   for (auto &VI : S->refs()) {
2170b57cec5SDimitry Andric     assert(VI.getAccessSpecifier() == 0 || isa<FunctionSummary>(S));
218e8d8bef9SDimitry Andric     if (!VI.getAccessSpecifier()) {
219e8d8bef9SDimitry Andric       if (!MarkedNonReadWriteOnly.insert(VI).second)
220e8d8bef9SDimitry Andric         continue;
221e8d8bef9SDimitry Andric     } else if (MarkedNonReadWriteOnly.contains(VI))
222e8d8bef9SDimitry Andric       continue;
2230b57cec5SDimitry Andric     for (auto &Ref : VI.getSummaryList())
2240b57cec5SDimitry Andric       // If references to alias is not read/writeonly then aliasee
2250b57cec5SDimitry Andric       // is not read/writeonly
2260b57cec5SDimitry Andric       if (auto *GVS = dyn_cast<GlobalVarSummary>(Ref->getBaseObject())) {
2270b57cec5SDimitry Andric         if (!VI.isReadOnly())
2280b57cec5SDimitry Andric           GVS->setReadOnly(false);
2290b57cec5SDimitry Andric         if (!VI.isWriteOnly())
2300b57cec5SDimitry Andric           GVS->setWriteOnly(false);
2310b57cec5SDimitry Andric       }
2320b57cec5SDimitry Andric   }
2330b57cec5SDimitry Andric }
2340b57cec5SDimitry Andric 
235fe6060f1SDimitry Andric // Do the access attribute and DSOLocal propagation in combined index.
2360b57cec5SDimitry Andric // The goal of attribute propagation is internalization of readonly (RO)
2370b57cec5SDimitry Andric // or writeonly (WO) variables. To determine which variables are RO or WO
2380b57cec5SDimitry Andric // and which are not we take following steps:
2390b57cec5SDimitry Andric // - During analysis we speculatively assign readonly and writeonly
2400b57cec5SDimitry Andric //   attribute to all variables which can be internalized. When computing
2410b57cec5SDimitry Andric //   function summary we also assign readonly or writeonly attribute to a
2420b57cec5SDimitry Andric //   reference if function doesn't modify referenced variable (readonly)
2430b57cec5SDimitry Andric //   or doesn't read it (writeonly).
2440b57cec5SDimitry Andric //
2450b57cec5SDimitry Andric // - After computing dead symbols in combined index we do the attribute
246fe6060f1SDimitry Andric //   and DSOLocal propagation. During this step we:
2470b57cec5SDimitry Andric //   a. clear RO and WO attributes from variables which are preserved or
2480b57cec5SDimitry Andric //      can't be imported
2490b57cec5SDimitry Andric //   b. clear RO and WO attributes from variables referenced by any global
2500b57cec5SDimitry Andric //      variable initializer
2510b57cec5SDimitry Andric //   c. clear RO attribute from variable referenced by a function when
2520b57cec5SDimitry Andric //      reference is not readonly
2530b57cec5SDimitry Andric //   d. clear WO attribute from variable referenced by a function when
2540b57cec5SDimitry Andric //      reference is not writeonly
255fe6060f1SDimitry Andric //   e. clear IsDSOLocal flag in every summary if any of them is false.
2560b57cec5SDimitry Andric //
2570b57cec5SDimitry Andric //   Because of (c, d) we don't internalize variables read by function A
2580b57cec5SDimitry Andric //   and modified by function B.
2590b57cec5SDimitry Andric //
2600b57cec5SDimitry Andric // Internalization itself happens in the backend after import is finished
2610b57cec5SDimitry Andric // See internalizeGVsAfterImport.
propagateAttributes(const DenseSet<GlobalValue::GUID> & GUIDPreservedSymbols)2620b57cec5SDimitry Andric void ModuleSummaryIndex::propagateAttributes(
2630b57cec5SDimitry Andric     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
264480093f4SDimitry Andric   if (!PropagateAttrs)
265480093f4SDimitry Andric     return;
266e8d8bef9SDimitry Andric   DenseSet<ValueInfo> MarkedNonReadWriteOnly;
267fe6060f1SDimitry Andric   for (auto &P : *this) {
268fe6060f1SDimitry Andric     bool IsDSOLocal = true;
2690b57cec5SDimitry Andric     for (auto &S : P.second.SummaryList) {
270e8d8bef9SDimitry Andric       if (!isGlobalValueLive(S.get())) {
271349cc55cSDimitry Andric         // computeDeadSymbolsAndUpdateIndirectCalls should have marked all
272349cc55cSDimitry Andric         // copies live. Note that it is possible that there is a GUID collision
273349cc55cSDimitry Andric         // between internal symbols with the same name in different files of the
274349cc55cSDimitry Andric         // same name but not enough distinguishing path. Because
275349cc55cSDimitry Andric         // computeDeadSymbolsAndUpdateIndirectCalls should conservatively mark
276349cc55cSDimitry Andric         // all copies live we can assert here that all are dead if any copy is
277349cc55cSDimitry Andric         // dead.
278e8d8bef9SDimitry Andric         assert(llvm::none_of(
279e8d8bef9SDimitry Andric             P.second.SummaryList,
280e8d8bef9SDimitry Andric             [&](const std::unique_ptr<GlobalValueSummary> &Summary) {
281e8d8bef9SDimitry Andric               return isGlobalValueLive(Summary.get());
282e8d8bef9SDimitry Andric             }));
2830b57cec5SDimitry Andric         // We don't examine references from dead objects
284e8d8bef9SDimitry Andric         break;
285e8d8bef9SDimitry Andric       }
2860b57cec5SDimitry Andric 
2870b57cec5SDimitry Andric       // Global variable can't be marked read/writeonly if it is not eligible
2880b57cec5SDimitry Andric       // to import since we need to ensure that all external references get
2890b57cec5SDimitry Andric       // a local (imported) copy. It also can't be marked read/writeonly if
2900b57cec5SDimitry Andric       // it or any alias (since alias points to the same memory) are preserved
2910b57cec5SDimitry Andric       // or notEligibleToImport, since either of those means there could be
2920b57cec5SDimitry Andric       // writes (or reads in case of writeonly) that are not visible (because
2930b57cec5SDimitry Andric       // preserved means it could have external to DSO writes or reads, and
2940b57cec5SDimitry Andric       // notEligibleToImport means it could have writes or reads via inline
2950b57cec5SDimitry Andric       // assembly leading it to be in the @llvm.*used).
2960b57cec5SDimitry Andric       if (auto *GVS = dyn_cast<GlobalVarSummary>(S->getBaseObject()))
2970b57cec5SDimitry Andric         // Here we intentionally pass S.get() not GVS, because S could be
298480093f4SDimitry Andric         // an alias. We don't analyze references here, because we have to
299480093f4SDimitry Andric         // know exactly if GV is readonly to do so.
300480093f4SDimitry Andric         if (!canImportGlobalVar(S.get(), /* AnalyzeRefs */ false) ||
3010b57cec5SDimitry Andric             GUIDPreservedSymbols.count(P.first)) {
3020b57cec5SDimitry Andric           GVS->setReadOnly(false);
3030b57cec5SDimitry Andric           GVS->setWriteOnly(false);
3040b57cec5SDimitry Andric         }
305e8d8bef9SDimitry Andric       propagateAttributesToRefs(S.get(), MarkedNonReadWriteOnly);
306fe6060f1SDimitry Andric 
307fe6060f1SDimitry Andric       // If the flag from any summary is false, the GV is not DSOLocal.
308fe6060f1SDimitry Andric       IsDSOLocal &= S->isDSOLocal();
309fe6060f1SDimitry Andric     }
310fe6060f1SDimitry Andric     if (!IsDSOLocal)
311fe6060f1SDimitry Andric       // Mark the flag in all summaries false so that we can do quick check
312fe6060f1SDimitry Andric       // without going through the whole list.
313fe6060f1SDimitry Andric       for (const std::unique_ptr<GlobalValueSummary> &Summary :
314fe6060f1SDimitry Andric            P.second.SummaryList)
315fe6060f1SDimitry Andric         Summary->setDSOLocal(false);
3160b57cec5SDimitry Andric   }
317480093f4SDimitry Andric   setWithAttributePropagation();
318fe6060f1SDimitry Andric   setWithDSOLocalPropagation();
3190b57cec5SDimitry Andric   if (llvm::AreStatisticsEnabled())
3200b57cec5SDimitry Andric     for (auto &P : *this)
3210b57cec5SDimitry Andric       if (P.second.SummaryList.size())
3220b57cec5SDimitry Andric         if (auto *GVS = dyn_cast<GlobalVarSummary>(
3230b57cec5SDimitry Andric                 P.second.SummaryList[0]->getBaseObject()))
3240b57cec5SDimitry Andric           if (isGlobalValueLive(GVS)) {
3250b57cec5SDimitry Andric             if (GVS->maybeReadOnly())
3260b57cec5SDimitry Andric               ReadOnlyLiveGVars++;
3270b57cec5SDimitry Andric             if (GVS->maybeWriteOnly())
3280b57cec5SDimitry Andric               WriteOnlyLiveGVars++;
3290b57cec5SDimitry Andric           }
3300b57cec5SDimitry Andric }
3310b57cec5SDimitry Andric 
canImportGlobalVar(const GlobalValueSummary * S,bool AnalyzeRefs) const33206c3fb27SDimitry Andric bool ModuleSummaryIndex::canImportGlobalVar(const GlobalValueSummary *S,
333480093f4SDimitry Andric                                             bool AnalyzeRefs) const {
334480093f4SDimitry Andric   auto HasRefsPreventingImport = [this](const GlobalVarSummary *GVS) {
335480093f4SDimitry Andric     // We don't analyze GV references during attribute propagation, so
336480093f4SDimitry Andric     // GV with non-trivial initializer can be marked either read or
337480093f4SDimitry Andric     // write-only.
338480093f4SDimitry Andric     // Importing definiton of readonly GV with non-trivial initializer
339480093f4SDimitry Andric     // allows us doing some extra optimizations (like converting indirect
340480093f4SDimitry Andric     // calls to direct).
341480093f4SDimitry Andric     // Definition of writeonly GV with non-trivial initializer should also
342480093f4SDimitry Andric     // be imported. Not doing so will result in:
343480093f4SDimitry Andric     // a) GV internalization in source module (because it's writeonly)
344480093f4SDimitry Andric     // b) Importing of GV declaration to destination module as a result
345480093f4SDimitry Andric     //    of promotion.
346480093f4SDimitry Andric     // c) Link error (external declaration with internal definition).
347480093f4SDimitry Andric     // However we do not promote objects referenced by writeonly GV
348480093f4SDimitry Andric     // initializer by means of converting it to 'zeroinitializer'
3495ffd83dbSDimitry Andric     return !(ImportConstantsWithRefs && GVS->isConstant()) &&
3505ffd83dbSDimitry Andric            !isReadOnly(GVS) && !isWriteOnly(GVS) && GVS->refs().size();
351480093f4SDimitry Andric   };
352480093f4SDimitry Andric   auto *GVS = cast<GlobalVarSummary>(S->getBaseObject());
353480093f4SDimitry Andric 
354480093f4SDimitry Andric   // Global variable with non-trivial initializer can be imported
355480093f4SDimitry Andric   // if it's readonly. This gives us extra opportunities for constant
356480093f4SDimitry Andric   // folding and converting indirect calls to direct calls. We don't
357480093f4SDimitry Andric   // analyze GV references during attribute propagation, because we
358480093f4SDimitry Andric   // don't know yet if it is readonly or not.
359480093f4SDimitry Andric   return !GlobalValue::isInterposableLinkage(S->linkage()) &&
360480093f4SDimitry Andric          !S->notEligibleToImport() &&
361480093f4SDimitry Andric          (!AnalyzeRefs || !HasRefsPreventingImport(GVS));
362480093f4SDimitry Andric }
363480093f4SDimitry Andric 
3640b57cec5SDimitry Andric // TODO: write a graphviz dumper for SCCs (see ModuleSummaryIndex::exportToDot)
3650b57cec5SDimitry Andric // then delete this function and update its tests
3660b57cec5SDimitry Andric LLVM_DUMP_METHOD
dumpSCCs(raw_ostream & O)3670b57cec5SDimitry Andric void ModuleSummaryIndex::dumpSCCs(raw_ostream &O) {
3680b57cec5SDimitry Andric   for (scc_iterator<ModuleSummaryIndex *> I =
3690b57cec5SDimitry Andric            scc_begin<ModuleSummaryIndex *>(this);
3700b57cec5SDimitry Andric        !I.isAtEnd(); ++I) {
3710b57cec5SDimitry Andric     O << "SCC (" << utostr(I->size()) << " node" << (I->size() == 1 ? "" : "s")
3720b57cec5SDimitry Andric       << ") {\n";
373480093f4SDimitry Andric     for (const ValueInfo &V : *I) {
3740b57cec5SDimitry Andric       FunctionSummary *F = nullptr;
3750b57cec5SDimitry Andric       if (V.getSummaryList().size())
3760b57cec5SDimitry Andric         F = cast<FunctionSummary>(V.getSummaryList().front().get());
3770b57cec5SDimitry Andric       O << " " << (F == nullptr ? "External" : "") << " " << utostr(V.getGUID())
3785ffd83dbSDimitry Andric         << (I.hasCycle() ? " (has cycle)" : "") << "\n";
3790b57cec5SDimitry Andric     }
3800b57cec5SDimitry Andric     O << "}\n";
3810b57cec5SDimitry Andric   }
3820b57cec5SDimitry Andric }
3830b57cec5SDimitry Andric 
3840b57cec5SDimitry Andric namespace {
3850b57cec5SDimitry Andric struct Attributes {
3860b57cec5SDimitry Andric   void add(const Twine &Name, const Twine &Value,
3870b57cec5SDimitry Andric            const Twine &Comment = Twine());
3880b57cec5SDimitry Andric   void addComment(const Twine &Comment);
3890b57cec5SDimitry Andric   std::string getAsString() const;
3900b57cec5SDimitry Andric 
3910b57cec5SDimitry Andric   std::vector<std::string> Attrs;
3920b57cec5SDimitry Andric   std::string Comments;
3930b57cec5SDimitry Andric };
3940b57cec5SDimitry Andric 
3950b57cec5SDimitry Andric struct Edge {
3960b57cec5SDimitry Andric   uint64_t SrcMod;
3970b57cec5SDimitry Andric   int Hotness;
3980b57cec5SDimitry Andric   GlobalValue::GUID Src;
3990b57cec5SDimitry Andric   GlobalValue::GUID Dst;
4000b57cec5SDimitry Andric };
4010b57cec5SDimitry Andric }
4020b57cec5SDimitry Andric 
add(const Twine & Name,const Twine & Value,const Twine & Comment)4030b57cec5SDimitry Andric void Attributes::add(const Twine &Name, const Twine &Value,
4040b57cec5SDimitry Andric                      const Twine &Comment) {
4050b57cec5SDimitry Andric   std::string A = Name.str();
4060b57cec5SDimitry Andric   A += "=\"";
4070b57cec5SDimitry Andric   A += Value.str();
4080b57cec5SDimitry Andric   A += "\"";
4090b57cec5SDimitry Andric   Attrs.push_back(A);
4100b57cec5SDimitry Andric   addComment(Comment);
4110b57cec5SDimitry Andric }
4120b57cec5SDimitry Andric 
addComment(const Twine & Comment)4130b57cec5SDimitry Andric void Attributes::addComment(const Twine &Comment) {
4140b57cec5SDimitry Andric   if (!Comment.isTriviallyEmpty()) {
4150b57cec5SDimitry Andric     if (Comments.empty())
4160b57cec5SDimitry Andric       Comments = " // ";
4170b57cec5SDimitry Andric     else
4180b57cec5SDimitry Andric       Comments += ", ";
4190b57cec5SDimitry Andric     Comments += Comment.str();
4200b57cec5SDimitry Andric   }
4210b57cec5SDimitry Andric }
4220b57cec5SDimitry Andric 
getAsString() const4230b57cec5SDimitry Andric std::string Attributes::getAsString() const {
4240b57cec5SDimitry Andric   if (Attrs.empty())
4250b57cec5SDimitry Andric     return "";
4260b57cec5SDimitry Andric 
4270b57cec5SDimitry Andric   std::string Ret = "[";
4280b57cec5SDimitry Andric   for (auto &A : Attrs)
4290b57cec5SDimitry Andric     Ret += A + ",";
4300b57cec5SDimitry Andric   Ret.pop_back();
4310b57cec5SDimitry Andric   Ret += "];";
4320b57cec5SDimitry Andric   Ret += Comments;
4330b57cec5SDimitry Andric   return Ret;
4340b57cec5SDimitry Andric }
4350b57cec5SDimitry Andric 
linkageToString(GlobalValue::LinkageTypes LT)4360b57cec5SDimitry Andric static std::string linkageToString(GlobalValue::LinkageTypes LT) {
4370b57cec5SDimitry Andric   switch (LT) {
4380b57cec5SDimitry Andric   case GlobalValue::ExternalLinkage:
4390b57cec5SDimitry Andric     return "extern";
4400b57cec5SDimitry Andric   case GlobalValue::AvailableExternallyLinkage:
4410b57cec5SDimitry Andric     return "av_ext";
4420b57cec5SDimitry Andric   case GlobalValue::LinkOnceAnyLinkage:
4430b57cec5SDimitry Andric     return "linkonce";
4440b57cec5SDimitry Andric   case GlobalValue::LinkOnceODRLinkage:
4450b57cec5SDimitry Andric     return "linkonce_odr";
4460b57cec5SDimitry Andric   case GlobalValue::WeakAnyLinkage:
4470b57cec5SDimitry Andric     return "weak";
4480b57cec5SDimitry Andric   case GlobalValue::WeakODRLinkage:
4490b57cec5SDimitry Andric     return "weak_odr";
4500b57cec5SDimitry Andric   case GlobalValue::AppendingLinkage:
4510b57cec5SDimitry Andric     return "appending";
4520b57cec5SDimitry Andric   case GlobalValue::InternalLinkage:
4530b57cec5SDimitry Andric     return "internal";
4540b57cec5SDimitry Andric   case GlobalValue::PrivateLinkage:
4550b57cec5SDimitry Andric     return "private";
4560b57cec5SDimitry Andric   case GlobalValue::ExternalWeakLinkage:
4570b57cec5SDimitry Andric     return "extern_weak";
4580b57cec5SDimitry Andric   case GlobalValue::CommonLinkage:
4590b57cec5SDimitry Andric     return "common";
4600b57cec5SDimitry Andric   }
4610b57cec5SDimitry Andric 
4620b57cec5SDimitry Andric   return "<unknown>";
4630b57cec5SDimitry Andric }
4640b57cec5SDimitry Andric 
fflagsToString(FunctionSummary::FFlags F)4650b57cec5SDimitry Andric static std::string fflagsToString(FunctionSummary::FFlags F) {
4660b57cec5SDimitry Andric   auto FlagValue = [](unsigned V) { return V ? '1' : '0'; };
4670eae32dcSDimitry Andric   char FlagRep[] = {FlagValue(F.ReadNone),
4680eae32dcSDimitry Andric                     FlagValue(F.ReadOnly),
4690eae32dcSDimitry Andric                     FlagValue(F.NoRecurse),
4700eae32dcSDimitry Andric                     FlagValue(F.ReturnDoesNotAlias),
4710eae32dcSDimitry Andric                     FlagValue(F.NoInline),
4720eae32dcSDimitry Andric                     FlagValue(F.AlwaysInline),
4730eae32dcSDimitry Andric                     FlagValue(F.NoUnwind),
4740eae32dcSDimitry Andric                     FlagValue(F.MayThrow),
4750eae32dcSDimitry Andric                     FlagValue(F.HasUnknownCall),
4760eae32dcSDimitry Andric                     FlagValue(F.MustBeUnreachable),
4770eae32dcSDimitry Andric                     0};
4780b57cec5SDimitry Andric 
4790b57cec5SDimitry Andric   return FlagRep;
4800b57cec5SDimitry Andric }
4810b57cec5SDimitry Andric 
4820b57cec5SDimitry Andric // Get string representation of function instruction count and flags.
getSummaryAttributes(GlobalValueSummary * GVS)4830b57cec5SDimitry Andric static std::string getSummaryAttributes(GlobalValueSummary* GVS) {
4840b57cec5SDimitry Andric   auto *FS = dyn_cast_or_null<FunctionSummary>(GVS);
4850b57cec5SDimitry Andric   if (!FS)
4860b57cec5SDimitry Andric     return "";
4870b57cec5SDimitry Andric 
4880b57cec5SDimitry Andric   return std::string("inst: ") + std::to_string(FS->instCount()) +
4890b57cec5SDimitry Andric          ", ffl: " + fflagsToString(FS->fflags());
4900b57cec5SDimitry Andric }
4910b57cec5SDimitry Andric 
getNodeVisualName(GlobalValue::GUID Id)4920b57cec5SDimitry Andric static std::string getNodeVisualName(GlobalValue::GUID Id) {
4930b57cec5SDimitry Andric   return std::string("@") + std::to_string(Id);
4940b57cec5SDimitry Andric }
4950b57cec5SDimitry Andric 
getNodeVisualName(const ValueInfo & VI)4960b57cec5SDimitry Andric static std::string getNodeVisualName(const ValueInfo &VI) {
4970b57cec5SDimitry Andric   return VI.name().empty() ? getNodeVisualName(VI.getGUID()) : VI.name().str();
4980b57cec5SDimitry Andric }
4990b57cec5SDimitry Andric 
getNodeLabel(const ValueInfo & VI,GlobalValueSummary * GVS)5000b57cec5SDimitry Andric static std::string getNodeLabel(const ValueInfo &VI, GlobalValueSummary *GVS) {
5010b57cec5SDimitry Andric   if (isa<AliasSummary>(GVS))
5020b57cec5SDimitry Andric     return getNodeVisualName(VI);
5030b57cec5SDimitry Andric 
5040b57cec5SDimitry Andric   std::string Attrs = getSummaryAttributes(GVS);
5050b57cec5SDimitry Andric   std::string Label =
5060b57cec5SDimitry Andric       getNodeVisualName(VI) + "|" + linkageToString(GVS->linkage());
5070b57cec5SDimitry Andric   if (!Attrs.empty())
5080b57cec5SDimitry Andric     Label += std::string(" (") + Attrs + ")";
5090b57cec5SDimitry Andric   Label += "}";
5100b57cec5SDimitry Andric 
5110b57cec5SDimitry Andric   return Label;
5120b57cec5SDimitry Andric }
5130b57cec5SDimitry Andric 
5140b57cec5SDimitry Andric // Write definition of external node, which doesn't have any
5150b57cec5SDimitry Andric // specific module associated with it. Typically this is function
5160b57cec5SDimitry Andric // or variable defined in native object or library.
defineExternalNode(raw_ostream & OS,const char * Pfx,const ValueInfo & VI,GlobalValue::GUID Id)5170b57cec5SDimitry Andric static void defineExternalNode(raw_ostream &OS, const char *Pfx,
5180b57cec5SDimitry Andric                                const ValueInfo &VI, GlobalValue::GUID Id) {
5190b57cec5SDimitry Andric   auto StrId = std::to_string(Id);
5200b57cec5SDimitry Andric   OS << "  " << StrId << " [label=\"";
5210b57cec5SDimitry Andric 
5220b57cec5SDimitry Andric   if (VI) {
5230b57cec5SDimitry Andric     OS << getNodeVisualName(VI);
5240b57cec5SDimitry Andric   } else {
5250b57cec5SDimitry Andric     OS << getNodeVisualName(Id);
5260b57cec5SDimitry Andric   }
5270b57cec5SDimitry Andric   OS << "\"]; // defined externally\n";
5280b57cec5SDimitry Andric }
5290b57cec5SDimitry Andric 
hasReadOnlyFlag(const GlobalValueSummary * S)5300b57cec5SDimitry Andric static bool hasReadOnlyFlag(const GlobalValueSummary *S) {
5310b57cec5SDimitry Andric   if (auto *GVS = dyn_cast<GlobalVarSummary>(S))
5320b57cec5SDimitry Andric     return GVS->maybeReadOnly();
5330b57cec5SDimitry Andric   return false;
5340b57cec5SDimitry Andric }
5350b57cec5SDimitry Andric 
hasWriteOnlyFlag(const GlobalValueSummary * S)5360b57cec5SDimitry Andric static bool hasWriteOnlyFlag(const GlobalValueSummary *S) {
5370b57cec5SDimitry Andric   if (auto *GVS = dyn_cast<GlobalVarSummary>(S))
5380b57cec5SDimitry Andric     return GVS->maybeWriteOnly();
5390b57cec5SDimitry Andric   return false;
5400b57cec5SDimitry Andric }
5410b57cec5SDimitry Andric 
hasConstantFlag(const GlobalValueSummary * S)5425ffd83dbSDimitry Andric static bool hasConstantFlag(const GlobalValueSummary *S) {
5435ffd83dbSDimitry Andric   if (auto *GVS = dyn_cast<GlobalVarSummary>(S))
5445ffd83dbSDimitry Andric     return GVS->isConstant();
5455ffd83dbSDimitry Andric   return false;
5465ffd83dbSDimitry Andric }
5475ffd83dbSDimitry Andric 
exportToDot(raw_ostream & OS,const DenseSet<GlobalValue::GUID> & GUIDPreservedSymbols) const548480093f4SDimitry Andric void ModuleSummaryIndex::exportToDot(
549480093f4SDimitry Andric     raw_ostream &OS,
550480093f4SDimitry Andric     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) const {
5510b57cec5SDimitry Andric   std::vector<Edge> CrossModuleEdges;
5520b57cec5SDimitry Andric   DenseMap<GlobalValue::GUID, std::vector<uint64_t>> NodeMap;
5530b57cec5SDimitry Andric   using GVSOrderedMapTy = std::map<GlobalValue::GUID, GlobalValueSummary *>;
5540b57cec5SDimitry Andric   std::map<StringRef, GVSOrderedMapTy> ModuleToDefinedGVS;
5550b57cec5SDimitry Andric   collectDefinedGVSummariesPerModule(ModuleToDefinedGVS);
5560b57cec5SDimitry Andric 
5575f757f3fSDimitry Andric   // Assign an id to each module path for use in graph labels. Since the
5585f757f3fSDimitry Andric   // StringMap iteration order isn't guaranteed, order by path string before
5595f757f3fSDimitry Andric   // assigning ids.
5605f757f3fSDimitry Andric   std::vector<StringRef> ModulePaths;
5615f757f3fSDimitry Andric   for (auto &[ModPath, _] : modulePaths())
5625f757f3fSDimitry Andric     ModulePaths.push_back(ModPath);
5635f757f3fSDimitry Andric   llvm::sort(ModulePaths);
5645f757f3fSDimitry Andric   DenseMap<StringRef, uint64_t> ModuleIdMap;
5655f757f3fSDimitry Andric   for (auto &ModPath : ModulePaths)
5665f757f3fSDimitry Andric     ModuleIdMap.try_emplace(ModPath, ModuleIdMap.size());
5675f757f3fSDimitry Andric 
5680b57cec5SDimitry Andric   // Get node identifier in form MXXX_<GUID>. The MXXX prefix is required,
5690b57cec5SDimitry Andric   // because we may have multiple linkonce functions summaries.
5700b57cec5SDimitry Andric   auto NodeId = [](uint64_t ModId, GlobalValue::GUID Id) {
5710b57cec5SDimitry Andric     return ModId == (uint64_t)-1 ? std::to_string(Id)
5720b57cec5SDimitry Andric                                  : std::string("M") + std::to_string(ModId) +
5730b57cec5SDimitry Andric                                        "_" + std::to_string(Id);
5740b57cec5SDimitry Andric   };
5750b57cec5SDimitry Andric 
5760b57cec5SDimitry Andric   auto DrawEdge = [&](const char *Pfx, uint64_t SrcMod, GlobalValue::GUID SrcId,
5770b57cec5SDimitry Andric                       uint64_t DstMod, GlobalValue::GUID DstId,
5780b57cec5SDimitry Andric                       int TypeOrHotness) {
5790b57cec5SDimitry Andric     // 0 - alias
5800b57cec5SDimitry Andric     // 1 - reference
5810b57cec5SDimitry Andric     // 2 - constant reference
5820b57cec5SDimitry Andric     // 3 - writeonly reference
5830b57cec5SDimitry Andric     // Other value: (hotness - 4).
5840b57cec5SDimitry Andric     TypeOrHotness += 4;
5850b57cec5SDimitry Andric     static const char *EdgeAttrs[] = {
5860b57cec5SDimitry Andric         " [style=dotted]; // alias",
5870b57cec5SDimitry Andric         " [style=dashed]; // ref",
5880b57cec5SDimitry Andric         " [style=dashed,color=forestgreen]; // const-ref",
5890b57cec5SDimitry Andric         " [style=dashed,color=violetred]; // writeOnly-ref",
5900b57cec5SDimitry Andric         " // call (hotness : Unknown)",
5910b57cec5SDimitry Andric         " [color=blue]; // call (hotness : Cold)",
5920b57cec5SDimitry Andric         " // call (hotness : None)",
5930b57cec5SDimitry Andric         " [color=brown]; // call (hotness : Hot)",
5940b57cec5SDimitry Andric         " [style=bold,color=red]; // call (hotness : Critical)"};
5950b57cec5SDimitry Andric 
596bdd1243dSDimitry Andric     assert(static_cast<size_t>(TypeOrHotness) < std::size(EdgeAttrs));
5970b57cec5SDimitry Andric     OS << Pfx << NodeId(SrcMod, SrcId) << " -> " << NodeId(DstMod, DstId)
5980b57cec5SDimitry Andric        << EdgeAttrs[TypeOrHotness] << "\n";
5990b57cec5SDimitry Andric   };
6000b57cec5SDimitry Andric 
6010b57cec5SDimitry Andric   OS << "digraph Summary {\n";
6020b57cec5SDimitry Andric   for (auto &ModIt : ModuleToDefinedGVS) {
6035f757f3fSDimitry Andric     // Will be empty for a just built per-module index, which doesn't setup a
6045f757f3fSDimitry Andric     // module paths table. In that case use 0 as the module id.
6055f757f3fSDimitry Andric     assert(ModuleIdMap.count(ModIt.first) || ModuleIdMap.empty());
6065f757f3fSDimitry Andric     auto ModId = ModuleIdMap.empty() ? 0 : ModuleIdMap[ModIt.first];
6070b57cec5SDimitry Andric     OS << "  // Module: " << ModIt.first << "\n";
6080b57cec5SDimitry Andric     OS << "  subgraph cluster_" << std::to_string(ModId) << " {\n";
6090b57cec5SDimitry Andric     OS << "    style = filled;\n";
6100b57cec5SDimitry Andric     OS << "    color = lightgrey;\n";
6110b57cec5SDimitry Andric     OS << "    label = \"" << sys::path::filename(ModIt.first) << "\";\n";
6120b57cec5SDimitry Andric     OS << "    node [style=filled,fillcolor=lightblue];\n";
6130b57cec5SDimitry Andric 
6140b57cec5SDimitry Andric     auto &GVSMap = ModIt.second;
6150b57cec5SDimitry Andric     auto Draw = [&](GlobalValue::GUID IdFrom, GlobalValue::GUID IdTo, int Hotness) {
6160b57cec5SDimitry Andric       if (!GVSMap.count(IdTo)) {
6170b57cec5SDimitry Andric         CrossModuleEdges.push_back({ModId, Hotness, IdFrom, IdTo});
6180b57cec5SDimitry Andric         return;
6190b57cec5SDimitry Andric       }
6200b57cec5SDimitry Andric       DrawEdge("    ", ModId, IdFrom, ModId, IdTo, Hotness);
6210b57cec5SDimitry Andric     };
6220b57cec5SDimitry Andric 
6230b57cec5SDimitry Andric     for (auto &SummaryIt : GVSMap) {
6240b57cec5SDimitry Andric       NodeMap[SummaryIt.first].push_back(ModId);
6250b57cec5SDimitry Andric       auto Flags = SummaryIt.second->flags();
6260b57cec5SDimitry Andric       Attributes A;
6270b57cec5SDimitry Andric       if (isa<FunctionSummary>(SummaryIt.second)) {
6280b57cec5SDimitry Andric         A.add("shape", "record", "function");
6290b57cec5SDimitry Andric       } else if (isa<AliasSummary>(SummaryIt.second)) {
6300b57cec5SDimitry Andric         A.add("style", "dotted,filled", "alias");
6310b57cec5SDimitry Andric         A.add("shape", "box");
6320b57cec5SDimitry Andric       } else {
6330b57cec5SDimitry Andric         A.add("shape", "Mrecord", "variable");
6340b57cec5SDimitry Andric         if (Flags.Live && hasReadOnlyFlag(SummaryIt.second))
6350b57cec5SDimitry Andric           A.addComment("immutable");
6360b57cec5SDimitry Andric         if (Flags.Live && hasWriteOnlyFlag(SummaryIt.second))
6370b57cec5SDimitry Andric           A.addComment("writeOnly");
6385ffd83dbSDimitry Andric         if (Flags.Live && hasConstantFlag(SummaryIt.second))
6395ffd83dbSDimitry Andric           A.addComment("constant");
6400b57cec5SDimitry Andric       }
641fe6060f1SDimitry Andric       if (Flags.Visibility)
642fe6060f1SDimitry Andric         A.addComment("visibility");
6430b57cec5SDimitry Andric       if (Flags.DSOLocal)
6440b57cec5SDimitry Andric         A.addComment("dsoLocal");
6450b57cec5SDimitry Andric       if (Flags.CanAutoHide)
6460b57cec5SDimitry Andric         A.addComment("canAutoHide");
647*0fca6ea1SDimitry Andric       if (Flags.ImportType == GlobalValueSummary::ImportKind::Definition)
648*0fca6ea1SDimitry Andric         A.addComment("definition");
649*0fca6ea1SDimitry Andric       else if (Flags.ImportType == GlobalValueSummary::ImportKind::Declaration)
650*0fca6ea1SDimitry Andric         A.addComment("declaration");
651480093f4SDimitry Andric       if (GUIDPreservedSymbols.count(SummaryIt.first))
652480093f4SDimitry Andric         A.addComment("preserved");
6530b57cec5SDimitry Andric 
6540b57cec5SDimitry Andric       auto VI = getValueInfo(SummaryIt.first);
6550b57cec5SDimitry Andric       A.add("label", getNodeLabel(VI, SummaryIt.second));
6560b57cec5SDimitry Andric       if (!Flags.Live)
6570b57cec5SDimitry Andric         A.add("fillcolor", "red", "dead");
6580b57cec5SDimitry Andric       else if (Flags.NotEligibleToImport)
6590b57cec5SDimitry Andric         A.add("fillcolor", "yellow", "not eligible to import");
6600b57cec5SDimitry Andric 
6610b57cec5SDimitry Andric       OS << "    " << NodeId(ModId, SummaryIt.first) << " " << A.getAsString()
6620b57cec5SDimitry Andric          << "\n";
6630b57cec5SDimitry Andric     }
6640b57cec5SDimitry Andric     OS << "    // Edges:\n";
6650b57cec5SDimitry Andric 
6660b57cec5SDimitry Andric     for (auto &SummaryIt : GVSMap) {
6670b57cec5SDimitry Andric       auto *GVS = SummaryIt.second;
6680b57cec5SDimitry Andric       for (auto &R : GVS->refs())
6690b57cec5SDimitry Andric         Draw(SummaryIt.first, R.getGUID(),
6700b57cec5SDimitry Andric              R.isWriteOnly() ? -1 : (R.isReadOnly() ? -2 : -3));
6710b57cec5SDimitry Andric 
6720b57cec5SDimitry Andric       if (auto *AS = dyn_cast_or_null<AliasSummary>(SummaryIt.second)) {
6730b57cec5SDimitry Andric         Draw(SummaryIt.first, AS->getAliaseeGUID(), -4);
6740b57cec5SDimitry Andric         continue;
6750b57cec5SDimitry Andric       }
6760b57cec5SDimitry Andric 
6770b57cec5SDimitry Andric       if (auto *FS = dyn_cast_or_null<FunctionSummary>(SummaryIt.second))
6780b57cec5SDimitry Andric         for (auto &CGEdge : FS->calls())
6790b57cec5SDimitry Andric           Draw(SummaryIt.first, CGEdge.first.getGUID(),
6800b57cec5SDimitry Andric                static_cast<int>(CGEdge.second.Hotness));
6810b57cec5SDimitry Andric     }
6820b57cec5SDimitry Andric     OS << "  }\n";
6830b57cec5SDimitry Andric   }
6840b57cec5SDimitry Andric 
6850b57cec5SDimitry Andric   OS << "  // Cross-module edges:\n";
6860b57cec5SDimitry Andric   for (auto &E : CrossModuleEdges) {
6870b57cec5SDimitry Andric     auto &ModList = NodeMap[E.Dst];
6880b57cec5SDimitry Andric     if (ModList.empty()) {
6890b57cec5SDimitry Andric       defineExternalNode(OS, "  ", getValueInfo(E.Dst), E.Dst);
6900b57cec5SDimitry Andric       // Add fake module to the list to draw an edge to an external node
6910b57cec5SDimitry Andric       // in the loop below.
6920b57cec5SDimitry Andric       ModList.push_back(-1);
6930b57cec5SDimitry Andric     }
6940b57cec5SDimitry Andric     for (auto DstMod : ModList)
6950b57cec5SDimitry Andric       // The edge representing call or ref is drawn to every module where target
6960b57cec5SDimitry Andric       // symbol is defined. When target is a linkonce symbol there can be
6970b57cec5SDimitry Andric       // multiple edges representing a single call or ref, both intra-module and
6980b57cec5SDimitry Andric       // cross-module. As we've already drawn all intra-module edges before we
6990b57cec5SDimitry Andric       // skip it here.
7000b57cec5SDimitry Andric       if (DstMod != E.SrcMod)
7010b57cec5SDimitry Andric         DrawEdge("  ", E.SrcMod, E.Src, DstMod, E.Dst, E.Hotness);
7020b57cec5SDimitry Andric   }
7030b57cec5SDimitry Andric 
7040b57cec5SDimitry Andric   OS << "}";
7050b57cec5SDimitry Andric }
706