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/Support/CommandLine.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 static cl::opt<bool> PropagateAttrs("propagate-attrs", cl::init(true), 30 cl::Hidden, 31 cl::desc("Propagate attributes in index")); 32 33 static cl::opt<bool> ImportConstantsWithRefs( 34 "import-constants-with-refs", cl::init(true), cl::Hidden, 35 cl::desc("Import constant global variables with references")); 36 37 constexpr uint32_t FunctionSummary::ParamAccess::RangeWidth; 38 39 FunctionSummary FunctionSummary::ExternalNode = 40 FunctionSummary::makeDummyFunctionSummary({}); 41 42 GlobalValue::VisibilityTypes ValueInfo::getELFVisibility() const { 43 bool HasProtected = false; 44 for (const auto &S : make_pointee_range(getSummaryList())) { 45 if (S.getVisibility() == GlobalValue::HiddenVisibility) 46 return GlobalValue::HiddenVisibility; 47 if (S.getVisibility() == GlobalValue::ProtectedVisibility) 48 HasProtected = true; 49 } 50 return HasProtected ? GlobalValue::ProtectedVisibility 51 : GlobalValue::DefaultVisibility; 52 } 53 54 bool ValueInfo::isDSOLocal(bool WithDSOLocalPropagation) const { 55 // With DSOLocal propagation done, the flag in evey summary is the same. 56 // Check the first one is enough. 57 return WithDSOLocalPropagation 58 ? getSummaryList().size() && getSummaryList()[0]->isDSOLocal() 59 : getSummaryList().size() && 60 llvm::all_of( 61 getSummaryList(), 62 [](const std::unique_ptr<GlobalValueSummary> &Summary) { 63 return Summary->isDSOLocal(); 64 }); 65 } 66 67 bool ValueInfo::canAutoHide() const { 68 // Can only auto hide if all copies are eligible to auto hide. 69 return getSummaryList().size() && 70 llvm::all_of(getSummaryList(), 71 [](const std::unique_ptr<GlobalValueSummary> &Summary) { 72 return Summary->canAutoHide(); 73 }); 74 } 75 76 // Gets the number of readonly and writeonly refs in RefEdgeList 77 std::pair<unsigned, unsigned> FunctionSummary::specialRefCounts() const { 78 // Here we take advantage of having all readonly and writeonly references 79 // located in the end of the RefEdgeList. 80 auto Refs = refs(); 81 unsigned RORefCnt = 0, WORefCnt = 0; 82 int I; 83 for (I = Refs.size() - 1; I >= 0 && Refs[I].isWriteOnly(); --I) 84 WORefCnt++; 85 for (; I >= 0 && Refs[I].isReadOnly(); --I) 86 RORefCnt++; 87 return {RORefCnt, WORefCnt}; 88 } 89 90 constexpr uint64_t ModuleSummaryIndex::BitcodeSummaryVersion; 91 92 uint64_t ModuleSummaryIndex::getFlags() const { 93 uint64_t Flags = 0; 94 if (withGlobalValueDeadStripping()) 95 Flags |= 0x1; 96 if (skipModuleByDistributedBackend()) 97 Flags |= 0x2; 98 if (hasSyntheticEntryCounts()) 99 Flags |= 0x4; 100 if (enableSplitLTOUnit()) 101 Flags |= 0x8; 102 if (partiallySplitLTOUnits()) 103 Flags |= 0x10; 104 if (withAttributePropagation()) 105 Flags |= 0x20; 106 if (withDSOLocalPropagation()) 107 Flags |= 0x40; 108 if (withWholeProgramVisibility()) 109 Flags |= 0x80; 110 return Flags; 111 } 112 113 void ModuleSummaryIndex::setFlags(uint64_t Flags) { 114 assert(Flags <= 0xff && "Unexpected bits in flag"); 115 // 1 bit: WithGlobalValueDeadStripping flag. 116 // Set on combined index only. 117 if (Flags & 0x1) 118 setWithGlobalValueDeadStripping(); 119 // 1 bit: SkipModuleByDistributedBackend flag. 120 // Set on combined index only. 121 if (Flags & 0x2) 122 setSkipModuleByDistributedBackend(); 123 // 1 bit: HasSyntheticEntryCounts flag. 124 // Set on combined index only. 125 if (Flags & 0x4) 126 setHasSyntheticEntryCounts(); 127 // 1 bit: DisableSplitLTOUnit flag. 128 // Set on per module indexes. It is up to the client to validate 129 // the consistency of this flag across modules being linked. 130 if (Flags & 0x8) 131 setEnableSplitLTOUnit(); 132 // 1 bit: PartiallySplitLTOUnits flag. 133 // Set on combined index only. 134 if (Flags & 0x10) 135 setPartiallySplitLTOUnits(); 136 // 1 bit: WithAttributePropagation flag. 137 // Set on combined index only. 138 if (Flags & 0x20) 139 setWithAttributePropagation(); 140 // 1 bit: WithDSOLocalPropagation flag. 141 // Set on combined index only. 142 if (Flags & 0x40) 143 setWithDSOLocalPropagation(); 144 // 1 bit: WithWholeProgramVisibility flag. 145 // Set on combined index only. 146 if (Flags & 0x80) 147 setWithWholeProgramVisibility(); 148 } 149 150 // Collect for the given module the list of function it defines 151 // (GUID -> Summary). 152 void ModuleSummaryIndex::collectDefinedFunctionsForModule( 153 StringRef ModulePath, GVSummaryMapTy &GVSummaryMap) const { 154 for (auto &GlobalList : *this) { 155 auto GUID = GlobalList.first; 156 for (auto &GlobSummary : GlobalList.second.SummaryList) { 157 auto *Summary = dyn_cast_or_null<FunctionSummary>(GlobSummary.get()); 158 if (!Summary) 159 // Ignore global variable, focus on functions 160 continue; 161 // Ignore summaries from other modules. 162 if (Summary->modulePath() != ModulePath) 163 continue; 164 GVSummaryMap[GUID] = Summary; 165 } 166 } 167 } 168 169 GlobalValueSummary * 170 ModuleSummaryIndex::getGlobalValueSummary(uint64_t ValueGUID, 171 bool PerModuleIndex) const { 172 auto VI = getValueInfo(ValueGUID); 173 assert(VI && "GlobalValue not found in index"); 174 assert((!PerModuleIndex || VI.getSummaryList().size() == 1) && 175 "Expected a single entry per global value in per-module index"); 176 auto &Summary = VI.getSummaryList()[0]; 177 return Summary.get(); 178 } 179 180 bool ModuleSummaryIndex::isGUIDLive(GlobalValue::GUID GUID) const { 181 auto VI = getValueInfo(GUID); 182 if (!VI) 183 return true; 184 const auto &SummaryList = VI.getSummaryList(); 185 if (SummaryList.empty()) 186 return true; 187 for (auto &I : SummaryList) 188 if (isGlobalValueLive(I.get())) 189 return true; 190 return false; 191 } 192 193 static void 194 propagateAttributesToRefs(GlobalValueSummary *S, 195 DenseSet<ValueInfo> &MarkedNonReadWriteOnly) { 196 // If reference is not readonly or writeonly then referenced summary is not 197 // read/writeonly either. Note that: 198 // - All references from GlobalVarSummary are conservatively considered as 199 // not readonly or writeonly. Tracking them properly requires more complex 200 // analysis then we have now. 201 // 202 // - AliasSummary objects have no refs at all so this function is a no-op 203 // for them. 204 for (auto &VI : S->refs()) { 205 assert(VI.getAccessSpecifier() == 0 || isa<FunctionSummary>(S)); 206 if (!VI.getAccessSpecifier()) { 207 if (!MarkedNonReadWriteOnly.insert(VI).second) 208 continue; 209 } else if (MarkedNonReadWriteOnly.contains(VI)) 210 continue; 211 for (auto &Ref : VI.getSummaryList()) 212 // If references to alias is not read/writeonly then aliasee 213 // is not read/writeonly 214 if (auto *GVS = dyn_cast<GlobalVarSummary>(Ref->getBaseObject())) { 215 if (!VI.isReadOnly()) 216 GVS->setReadOnly(false); 217 if (!VI.isWriteOnly()) 218 GVS->setWriteOnly(false); 219 } 220 } 221 } 222 223 // Do the access attribute and DSOLocal propagation in combined index. 224 // The goal of attribute propagation is internalization of readonly (RO) 225 // or writeonly (WO) variables. To determine which variables are RO or WO 226 // and which are not we take following steps: 227 // - During analysis we speculatively assign readonly and writeonly 228 // attribute to all variables which can be internalized. When computing 229 // function summary we also assign readonly or writeonly attribute to a 230 // reference if function doesn't modify referenced variable (readonly) 231 // or doesn't read it (writeonly). 232 // 233 // - After computing dead symbols in combined index we do the attribute 234 // and DSOLocal propagation. During this step we: 235 // a. clear RO and WO attributes from variables which are preserved or 236 // can't be imported 237 // b. clear RO and WO attributes from variables referenced by any global 238 // variable initializer 239 // c. clear RO attribute from variable referenced by a function when 240 // reference is not readonly 241 // d. clear WO attribute from variable referenced by a function when 242 // reference is not writeonly 243 // e. clear IsDSOLocal flag in every summary if any of them is false. 244 // 245 // Because of (c, d) we don't internalize variables read by function A 246 // and modified by function B. 247 // 248 // Internalization itself happens in the backend after import is finished 249 // See internalizeGVsAfterImport. 250 void ModuleSummaryIndex::propagateAttributes( 251 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) { 252 if (!PropagateAttrs) 253 return; 254 DenseSet<ValueInfo> MarkedNonReadWriteOnly; 255 for (auto &P : *this) { 256 bool IsDSOLocal = true; 257 for (auto &S : P.second.SummaryList) { 258 if (!isGlobalValueLive(S.get())) { 259 // computeDeadSymbolsAndUpdateIndirectCalls should have marked all 260 // copies live. Note that it is possible that there is a GUID collision 261 // between internal symbols with the same name in different files of the 262 // same name but not enough distinguishing path. Because 263 // computeDeadSymbolsAndUpdateIndirectCalls should conservatively mark 264 // all copies live we can assert here that all are dead if any copy is 265 // dead. 266 assert(llvm::none_of( 267 P.second.SummaryList, 268 [&](const std::unique_ptr<GlobalValueSummary> &Summary) { 269 return isGlobalValueLive(Summary.get()); 270 })); 271 // We don't examine references from dead objects 272 break; 273 } 274 275 // Global variable can't be marked read/writeonly if it is not eligible 276 // to import since we need to ensure that all external references get 277 // a local (imported) copy. It also can't be marked read/writeonly if 278 // it or any alias (since alias points to the same memory) are preserved 279 // or notEligibleToImport, since either of those means there could be 280 // writes (or reads in case of writeonly) that are not visible (because 281 // preserved means it could have external to DSO writes or reads, and 282 // notEligibleToImport means it could have writes or reads via inline 283 // assembly leading it to be in the @llvm.*used). 284 if (auto *GVS = dyn_cast<GlobalVarSummary>(S->getBaseObject())) 285 // Here we intentionally pass S.get() not GVS, because S could be 286 // an alias. We don't analyze references here, because we have to 287 // know exactly if GV is readonly to do so. 288 if (!canImportGlobalVar(S.get(), /* AnalyzeRefs */ false) || 289 GUIDPreservedSymbols.count(P.first)) { 290 GVS->setReadOnly(false); 291 GVS->setWriteOnly(false); 292 } 293 propagateAttributesToRefs(S.get(), MarkedNonReadWriteOnly); 294 295 // If the flag from any summary is false, the GV is not DSOLocal. 296 IsDSOLocal &= S->isDSOLocal(); 297 } 298 if (!IsDSOLocal) 299 // Mark the flag in all summaries false so that we can do quick check 300 // without going through the whole list. 301 for (const std::unique_ptr<GlobalValueSummary> &Summary : 302 P.second.SummaryList) 303 Summary->setDSOLocal(false); 304 } 305 setWithAttributePropagation(); 306 setWithDSOLocalPropagation(); 307 if (llvm::AreStatisticsEnabled()) 308 for (auto &P : *this) 309 if (P.second.SummaryList.size()) 310 if (auto *GVS = dyn_cast<GlobalVarSummary>( 311 P.second.SummaryList[0]->getBaseObject())) 312 if (isGlobalValueLive(GVS)) { 313 if (GVS->maybeReadOnly()) 314 ReadOnlyLiveGVars++; 315 if (GVS->maybeWriteOnly()) 316 WriteOnlyLiveGVars++; 317 } 318 } 319 320 bool ModuleSummaryIndex::canImportGlobalVar(GlobalValueSummary *S, 321 bool AnalyzeRefs) const { 322 auto HasRefsPreventingImport = [this](const GlobalVarSummary *GVS) { 323 // We don't analyze GV references during attribute propagation, so 324 // GV with non-trivial initializer can be marked either read or 325 // write-only. 326 // Importing definiton of readonly GV with non-trivial initializer 327 // allows us doing some extra optimizations (like converting indirect 328 // calls to direct). 329 // Definition of writeonly GV with non-trivial initializer should also 330 // be imported. Not doing so will result in: 331 // a) GV internalization in source module (because it's writeonly) 332 // b) Importing of GV declaration to destination module as a result 333 // of promotion. 334 // c) Link error (external declaration with internal definition). 335 // However we do not promote objects referenced by writeonly GV 336 // initializer by means of converting it to 'zeroinitializer' 337 return !(ImportConstantsWithRefs && GVS->isConstant()) && 338 !isReadOnly(GVS) && !isWriteOnly(GVS) && GVS->refs().size(); 339 }; 340 auto *GVS = cast<GlobalVarSummary>(S->getBaseObject()); 341 342 // Global variable with non-trivial initializer can be imported 343 // if it's readonly. This gives us extra opportunities for constant 344 // folding and converting indirect calls to direct calls. We don't 345 // analyze GV references during attribute propagation, because we 346 // don't know yet if it is readonly or not. 347 return !GlobalValue::isInterposableLinkage(S->linkage()) && 348 !S->notEligibleToImport() && 349 (!AnalyzeRefs || !HasRefsPreventingImport(GVS)); 350 } 351 352 // TODO: write a graphviz dumper for SCCs (see ModuleSummaryIndex::exportToDot) 353 // then delete this function and update its tests 354 LLVM_DUMP_METHOD 355 void ModuleSummaryIndex::dumpSCCs(raw_ostream &O) { 356 for (scc_iterator<ModuleSummaryIndex *> I = 357 scc_begin<ModuleSummaryIndex *>(this); 358 !I.isAtEnd(); ++I) { 359 O << "SCC (" << utostr(I->size()) << " node" << (I->size() == 1 ? "" : "s") 360 << ") {\n"; 361 for (const ValueInfo &V : *I) { 362 FunctionSummary *F = nullptr; 363 if (V.getSummaryList().size()) 364 F = cast<FunctionSummary>(V.getSummaryList().front().get()); 365 O << " " << (F == nullptr ? "External" : "") << " " << utostr(V.getGUID()) 366 << (I.hasCycle() ? " (has cycle)" : "") << "\n"; 367 } 368 O << "}\n"; 369 } 370 } 371 372 namespace { 373 struct Attributes { 374 void add(const Twine &Name, const Twine &Value, 375 const Twine &Comment = Twine()); 376 void addComment(const Twine &Comment); 377 std::string getAsString() const; 378 379 std::vector<std::string> Attrs; 380 std::string Comments; 381 }; 382 383 struct Edge { 384 uint64_t SrcMod; 385 int Hotness; 386 GlobalValue::GUID Src; 387 GlobalValue::GUID Dst; 388 }; 389 } 390 391 void Attributes::add(const Twine &Name, const Twine &Value, 392 const Twine &Comment) { 393 std::string A = Name.str(); 394 A += "=\""; 395 A += Value.str(); 396 A += "\""; 397 Attrs.push_back(A); 398 addComment(Comment); 399 } 400 401 void Attributes::addComment(const Twine &Comment) { 402 if (!Comment.isTriviallyEmpty()) { 403 if (Comments.empty()) 404 Comments = " // "; 405 else 406 Comments += ", "; 407 Comments += Comment.str(); 408 } 409 } 410 411 std::string Attributes::getAsString() const { 412 if (Attrs.empty()) 413 return ""; 414 415 std::string Ret = "["; 416 for (auto &A : Attrs) 417 Ret += A + ","; 418 Ret.pop_back(); 419 Ret += "];"; 420 Ret += Comments; 421 return Ret; 422 } 423 424 static std::string linkageToString(GlobalValue::LinkageTypes LT) { 425 switch (LT) { 426 case GlobalValue::ExternalLinkage: 427 return "extern"; 428 case GlobalValue::AvailableExternallyLinkage: 429 return "av_ext"; 430 case GlobalValue::LinkOnceAnyLinkage: 431 return "linkonce"; 432 case GlobalValue::LinkOnceODRLinkage: 433 return "linkonce_odr"; 434 case GlobalValue::WeakAnyLinkage: 435 return "weak"; 436 case GlobalValue::WeakODRLinkage: 437 return "weak_odr"; 438 case GlobalValue::AppendingLinkage: 439 return "appending"; 440 case GlobalValue::InternalLinkage: 441 return "internal"; 442 case GlobalValue::PrivateLinkage: 443 return "private"; 444 case GlobalValue::ExternalWeakLinkage: 445 return "extern_weak"; 446 case GlobalValue::CommonLinkage: 447 return "common"; 448 } 449 450 return "<unknown>"; 451 } 452 453 static std::string fflagsToString(FunctionSummary::FFlags F) { 454 auto FlagValue = [](unsigned V) { return V ? '1' : '0'; }; 455 char FlagRep[] = {FlagValue(F.ReadNone), 456 FlagValue(F.ReadOnly), 457 FlagValue(F.NoRecurse), 458 FlagValue(F.ReturnDoesNotAlias), 459 FlagValue(F.NoInline), 460 FlagValue(F.AlwaysInline), 461 FlagValue(F.NoUnwind), 462 FlagValue(F.MayThrow), 463 FlagValue(F.HasUnknownCall), 464 FlagValue(F.MustBeUnreachable), 465 0}; 466 467 return FlagRep; 468 } 469 470 // Get string representation of function instruction count and flags. 471 static std::string getSummaryAttributes(GlobalValueSummary* GVS) { 472 auto *FS = dyn_cast_or_null<FunctionSummary>(GVS); 473 if (!FS) 474 return ""; 475 476 return std::string("inst: ") + std::to_string(FS->instCount()) + 477 ", ffl: " + fflagsToString(FS->fflags()); 478 } 479 480 static std::string getNodeVisualName(GlobalValue::GUID Id) { 481 return std::string("@") + std::to_string(Id); 482 } 483 484 static std::string getNodeVisualName(const ValueInfo &VI) { 485 return VI.name().empty() ? getNodeVisualName(VI.getGUID()) : VI.name().str(); 486 } 487 488 static std::string getNodeLabel(const ValueInfo &VI, GlobalValueSummary *GVS) { 489 if (isa<AliasSummary>(GVS)) 490 return getNodeVisualName(VI); 491 492 std::string Attrs = getSummaryAttributes(GVS); 493 std::string Label = 494 getNodeVisualName(VI) + "|" + linkageToString(GVS->linkage()); 495 if (!Attrs.empty()) 496 Label += std::string(" (") + Attrs + ")"; 497 Label += "}"; 498 499 return Label; 500 } 501 502 // Write definition of external node, which doesn't have any 503 // specific module associated with it. Typically this is function 504 // or variable defined in native object or library. 505 static void defineExternalNode(raw_ostream &OS, const char *Pfx, 506 const ValueInfo &VI, GlobalValue::GUID Id) { 507 auto StrId = std::to_string(Id); 508 OS << " " << StrId << " [label=\""; 509 510 if (VI) { 511 OS << getNodeVisualName(VI); 512 } else { 513 OS << getNodeVisualName(Id); 514 } 515 OS << "\"]; // defined externally\n"; 516 } 517 518 static bool hasReadOnlyFlag(const GlobalValueSummary *S) { 519 if (auto *GVS = dyn_cast<GlobalVarSummary>(S)) 520 return GVS->maybeReadOnly(); 521 return false; 522 } 523 524 static bool hasWriteOnlyFlag(const GlobalValueSummary *S) { 525 if (auto *GVS = dyn_cast<GlobalVarSummary>(S)) 526 return GVS->maybeWriteOnly(); 527 return false; 528 } 529 530 static bool hasConstantFlag(const GlobalValueSummary *S) { 531 if (auto *GVS = dyn_cast<GlobalVarSummary>(S)) 532 return GVS->isConstant(); 533 return false; 534 } 535 536 void ModuleSummaryIndex::exportToDot( 537 raw_ostream &OS, 538 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) const { 539 std::vector<Edge> CrossModuleEdges; 540 DenseMap<GlobalValue::GUID, std::vector<uint64_t>> NodeMap; 541 using GVSOrderedMapTy = std::map<GlobalValue::GUID, GlobalValueSummary *>; 542 std::map<StringRef, GVSOrderedMapTy> ModuleToDefinedGVS; 543 collectDefinedGVSummariesPerModule(ModuleToDefinedGVS); 544 545 // Get node identifier in form MXXX_<GUID>. The MXXX prefix is required, 546 // because we may have multiple linkonce functions summaries. 547 auto NodeId = [](uint64_t ModId, GlobalValue::GUID Id) { 548 return ModId == (uint64_t)-1 ? std::to_string(Id) 549 : std::string("M") + std::to_string(ModId) + 550 "_" + std::to_string(Id); 551 }; 552 553 auto DrawEdge = [&](const char *Pfx, uint64_t SrcMod, GlobalValue::GUID SrcId, 554 uint64_t DstMod, GlobalValue::GUID DstId, 555 int TypeOrHotness) { 556 // 0 - alias 557 // 1 - reference 558 // 2 - constant reference 559 // 3 - writeonly reference 560 // Other value: (hotness - 4). 561 TypeOrHotness += 4; 562 static const char *EdgeAttrs[] = { 563 " [style=dotted]; // alias", 564 " [style=dashed]; // ref", 565 " [style=dashed,color=forestgreen]; // const-ref", 566 " [style=dashed,color=violetred]; // writeOnly-ref", 567 " // call (hotness : Unknown)", 568 " [color=blue]; // call (hotness : Cold)", 569 " // call (hotness : None)", 570 " [color=brown]; // call (hotness : Hot)", 571 " [style=bold,color=red]; // call (hotness : Critical)"}; 572 573 assert(static_cast<size_t>(TypeOrHotness) < std::size(EdgeAttrs)); 574 OS << Pfx << NodeId(SrcMod, SrcId) << " -> " << NodeId(DstMod, DstId) 575 << EdgeAttrs[TypeOrHotness] << "\n"; 576 }; 577 578 OS << "digraph Summary {\n"; 579 for (auto &ModIt : ModuleToDefinedGVS) { 580 auto ModId = getModuleId(ModIt.first); 581 OS << " // Module: " << ModIt.first << "\n"; 582 OS << " subgraph cluster_" << std::to_string(ModId) << " {\n"; 583 OS << " style = filled;\n"; 584 OS << " color = lightgrey;\n"; 585 OS << " label = \"" << sys::path::filename(ModIt.first) << "\";\n"; 586 OS << " node [style=filled,fillcolor=lightblue];\n"; 587 588 auto &GVSMap = ModIt.second; 589 auto Draw = [&](GlobalValue::GUID IdFrom, GlobalValue::GUID IdTo, int Hotness) { 590 if (!GVSMap.count(IdTo)) { 591 CrossModuleEdges.push_back({ModId, Hotness, IdFrom, IdTo}); 592 return; 593 } 594 DrawEdge(" ", ModId, IdFrom, ModId, IdTo, Hotness); 595 }; 596 597 for (auto &SummaryIt : GVSMap) { 598 NodeMap[SummaryIt.first].push_back(ModId); 599 auto Flags = SummaryIt.second->flags(); 600 Attributes A; 601 if (isa<FunctionSummary>(SummaryIt.second)) { 602 A.add("shape", "record", "function"); 603 } else if (isa<AliasSummary>(SummaryIt.second)) { 604 A.add("style", "dotted,filled", "alias"); 605 A.add("shape", "box"); 606 } else { 607 A.add("shape", "Mrecord", "variable"); 608 if (Flags.Live && hasReadOnlyFlag(SummaryIt.second)) 609 A.addComment("immutable"); 610 if (Flags.Live && hasWriteOnlyFlag(SummaryIt.second)) 611 A.addComment("writeOnly"); 612 if (Flags.Live && hasConstantFlag(SummaryIt.second)) 613 A.addComment("constant"); 614 } 615 if (Flags.Visibility) 616 A.addComment("visibility"); 617 if (Flags.DSOLocal) 618 A.addComment("dsoLocal"); 619 if (Flags.CanAutoHide) 620 A.addComment("canAutoHide"); 621 if (GUIDPreservedSymbols.count(SummaryIt.first)) 622 A.addComment("preserved"); 623 624 auto VI = getValueInfo(SummaryIt.first); 625 A.add("label", getNodeLabel(VI, SummaryIt.second)); 626 if (!Flags.Live) 627 A.add("fillcolor", "red", "dead"); 628 else if (Flags.NotEligibleToImport) 629 A.add("fillcolor", "yellow", "not eligible to import"); 630 631 OS << " " << NodeId(ModId, SummaryIt.first) << " " << A.getAsString() 632 << "\n"; 633 } 634 OS << " // Edges:\n"; 635 636 for (auto &SummaryIt : GVSMap) { 637 auto *GVS = SummaryIt.second; 638 for (auto &R : GVS->refs()) 639 Draw(SummaryIt.first, R.getGUID(), 640 R.isWriteOnly() ? -1 : (R.isReadOnly() ? -2 : -3)); 641 642 if (auto *AS = dyn_cast_or_null<AliasSummary>(SummaryIt.second)) { 643 Draw(SummaryIt.first, AS->getAliaseeGUID(), -4); 644 continue; 645 } 646 647 if (auto *FS = dyn_cast_or_null<FunctionSummary>(SummaryIt.second)) 648 for (auto &CGEdge : FS->calls()) 649 Draw(SummaryIt.first, CGEdge.first.getGUID(), 650 static_cast<int>(CGEdge.second.Hotness)); 651 } 652 OS << " }\n"; 653 } 654 655 OS << " // Cross-module edges:\n"; 656 for (auto &E : CrossModuleEdges) { 657 auto &ModList = NodeMap[E.Dst]; 658 if (ModList.empty()) { 659 defineExternalNode(OS, " ", getValueInfo(E.Dst), E.Dst); 660 // Add fake module to the list to draw an edge to an external node 661 // in the loop below. 662 ModList.push_back(-1); 663 } 664 for (auto DstMod : ModList) 665 // The edge representing call or ref is drawn to every module where target 666 // symbol is defined. When target is a linkonce symbol there can be 667 // multiple edges representing a single call or ref, both intra-module and 668 // cross-module. As we've already drawn all intra-module edges before we 669 // skip it here. 670 if (DstMod != E.SrcMod) 671 DrawEdge(" ", E.SrcMod, E.Src, DstMod, E.Dst, E.Hotness); 672 } 673 674 OS << "}"; 675 } 676