1 //===-- ModuleSummaryIndex.cpp - Module Summary Index ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the module index and summary classes for the
13 //===----------------------------------------------------------------------===//
15 #include "llvm/IR/ModuleSummaryIndex.h"
16 #include "llvm/ADT/StringMap.h"
19 // Create the combined module index/summary from multiple
20 // per-module instances.
21 void ModuleSummaryIndex::mergeFrom(std::unique_ptr<ModuleSummaryIndex> Other,
22 uint64_t NextModuleId) {
23 if (Other->modulePaths().empty())
26 assert(Other->modulePaths().size() == 1 &&
27 "Can only merge from an single-module index at that time");
29 StringRef OtherModPath = Other->modulePaths().begin()->first();
30 StringRef ModPath = addModulePath(OtherModPath, NextModuleId,
31 Other->getModuleHash(OtherModPath))
34 for (auto &OtherGlobalValSummaryLists : *Other) {
35 GlobalValue::GUID ValueGUID = OtherGlobalValSummaryLists.first;
36 GlobalValueSummaryList &List = OtherGlobalValSummaryLists.second;
38 // Assert that the value summary list only has one entry, since we shouldn't
39 // have duplicate names within a single per-module index.
40 assert(List.size() == 1);
41 std::unique_ptr<GlobalValueSummary> Summary = std::move(List.front());
43 // Note the module path string ref was copied above and is still owned by
44 // the original per-module index. Reset it to the new module path
45 // string reference owned by the combined index.
46 Summary->setModulePath(ModPath);
48 // Add new value summary to existing list. There may be duplicates when
49 // combining GlobalValueMap entries, due to COMDAT values. Any local
50 // values were given unique global IDs.
51 addGlobalValueSummary(ValueGUID, std::move(Summary));
55 void ModuleSummaryIndex::removeEmptySummaryEntries() {
56 for (auto MI = begin(), MIE = end(); MI != MIE;) {
57 // Only expect this to be called on a per-module index, which has a single
58 // entry per value entry list.
59 assert(MI->second.size() == 1);
61 MI = GlobalValueMap.erase(MI);
67 // Collect for the given module the list of function it defines
69 void ModuleSummaryIndex::collectDefinedFunctionsForModule(
70 StringRef ModulePath, GVSummaryMapTy &GVSummaryMap) const {
71 for (auto &GlobalList : *this) {
72 auto GUID = GlobalList.first;
73 for (auto &GlobSummary : GlobalList.second) {
74 auto *Summary = dyn_cast_or_null<FunctionSummary>(GlobSummary.get());
76 // Ignore global variable, focus on functions
78 // Ignore summaries from other modules.
79 if (Summary->modulePath() != ModulePath)
81 GVSummaryMap[GUID] = Summary;
86 // Collect for each module the list of function it defines (GUID -> Summary).
87 void ModuleSummaryIndex::collectDefinedGVSummariesPerModule(
88 StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries) const {
89 for (auto &GlobalList : *this) {
90 auto GUID = GlobalList.first;
91 for (auto &Summary : GlobalList.second) {
92 ModuleToDefinedGVSummaries[Summary->modulePath()][GUID] = Summary.get();
98 ModuleSummaryIndex::getGlobalValueSummary(uint64_t ValueGUID,
99 bool PerModuleIndex) const {
100 auto SummaryList = findGlobalValueSummaryList(ValueGUID);
101 assert(SummaryList != end() && "GlobalValue not found in index");
102 assert((!PerModuleIndex || SummaryList->second.size() == 1) &&
103 "Expected a single entry per global value in per-module index");
104 auto &Summary = SummaryList->second[0];
105 return Summary.get();