blob: 4c122c7241ec6605be3b04d6d7bec629f49f7178 [file] [log] [blame]
Teresa Johnson26ab5772016-03-15 00:04:37 +00001//===-- ModuleSummaryIndex.cpp - Module Summary Index ---------------------===//
Teresa Johnsoncec0cae2016-03-14 21:18:10 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the module index and summary classes for the
11// IR library.
12//
13//===----------------------------------------------------------------------===//
14
Teresa Johnson26ab5772016-03-15 00:04:37 +000015#include "llvm/IR/ModuleSummaryIndex.h"
Teresa Johnsoncec0cae2016-03-14 21:18:10 +000016#include "llvm/ADT/StringMap.h"
17using namespace llvm;
18
19// Create the combined module index/summary from multiple
20// per-module instances.
Teresa Johnson26ab5772016-03-15 00:04:37 +000021void ModuleSummaryIndex::mergeFrom(std::unique_ptr<ModuleSummaryIndex> Other,
22 uint64_t NextModuleId) {
Teresa Johnsoncec0cae2016-03-14 21:18:10 +000023
24 StringRef ModPath;
Teresa Johnson28e457b2016-04-24 14:57:11 +000025 for (auto &OtherGlobalValSummaryLists : *Other) {
26 GlobalValue::GUID ValueGUID = OtherGlobalValSummaryLists.first;
27 GlobalValueSummaryList &List = OtherGlobalValSummaryLists.second;
Teresa Johnsoncec0cae2016-03-14 21:18:10 +000028
Teresa Johnson28e457b2016-04-24 14:57:11 +000029 // Assert that the value summary list only has one entry, since we shouldn't
Teresa Johnsoncec0cae2016-03-14 21:18:10 +000030 // have duplicate names within a single per-module index.
31 assert(List.size() == 1);
Teresa Johnson28e457b2016-04-24 14:57:11 +000032 std::unique_ptr<GlobalValueSummary> Summary = std::move(List.front());
Teresa Johnsoncec0cae2016-03-14 21:18:10 +000033
34 // Add the module path string ref for this module if we haven't already
35 // saved a reference to it.
Mehdi Aminid7ad2212016-04-01 05:33:11 +000036 if (ModPath.empty()) {
Teresa Johnson28e457b2016-04-24 14:57:11 +000037 auto Path = Summary->modulePath();
Mehdi Aminid7ad2212016-04-01 05:33:11 +000038 ModPath = addModulePath(Path, NextModuleId, Other->getModuleHash(Path))
39 ->first();
40 } else
Teresa Johnson28e457b2016-04-24 14:57:11 +000041 assert(ModPath == Summary->modulePath() &&
Teresa Johnsoncec0cae2016-03-14 21:18:10 +000042 "Each module in the combined map should have a unique ID");
43
44 // Note the module path string ref was copied above and is still owned by
45 // the original per-module index. Reset it to the new module path
46 // string reference owned by the combined index.
Teresa Johnson28e457b2016-04-24 14:57:11 +000047 Summary->setModulePath(ModPath);
Teresa Johnsoncec0cae2016-03-14 21:18:10 +000048
Teresa Johnson28e457b2016-04-24 14:57:11 +000049 // Add new value summary to existing list. There may be duplicates when
Teresa Johnsoncec0cae2016-03-14 21:18:10 +000050 // combining GlobalValueMap entries, due to COMDAT values. Any local
51 // values were given unique global IDs.
Teresa Johnson28e457b2016-04-24 14:57:11 +000052 addGlobalValueSummary(ValueGUID, std::move(Summary));
Teresa Johnsoncec0cae2016-03-14 21:18:10 +000053 }
54}
55
Teresa Johnson26ab5772016-03-15 00:04:37 +000056void ModuleSummaryIndex::removeEmptySummaryEntries() {
Teresa Johnsoncec0cae2016-03-14 21:18:10 +000057 for (auto MI = begin(), MIE = end(); MI != MIE;) {
58 // Only expect this to be called on a per-module index, which has a single
59 // entry per value entry list.
60 assert(MI->second.size() == 1);
Teresa Johnson28e457b2016-04-24 14:57:11 +000061 if (!MI->second[0])
Teresa Johnsoncec0cae2016-03-14 21:18:10 +000062 MI = GlobalValueMap.erase(MI);
63 else
64 ++MI;
65 }
66}
Teresa Johnsonfb7c7642016-04-05 00:40:16 +000067
Teresa Johnsonc86af332016-04-12 21:13:11 +000068// Collect for the given module the list of function it defines
69// (GUID -> Summary).
70void ModuleSummaryIndex::collectDefinedFunctionsForModule(
71 StringRef ModulePath,
Teresa Johnson28e457b2016-04-24 14:57:11 +000072 std::map<GlobalValue::GUID, GlobalValueSummary *> &GVSummaryMap) const {
Teresa Johnsonc86af332016-04-12 21:13:11 +000073 for (auto &GlobalList : *this) {
74 auto GUID = GlobalList.first;
Teresa Johnson28e457b2016-04-24 14:57:11 +000075 for (auto &GlobSummary : GlobalList.second) {
76 auto *Summary = dyn_cast_or_null<FunctionSummary>(GlobSummary.get());
Teresa Johnsonc86af332016-04-12 21:13:11 +000077 if (!Summary)
78 // Ignore global variable, focus on functions
79 continue;
80 // Ignore summaries from other modules.
81 if (Summary->modulePath() != ModulePath)
82 continue;
Teresa Johnson28e457b2016-04-24 14:57:11 +000083 GVSummaryMap[GUID] = Summary;
Teresa Johnsonc86af332016-04-12 21:13:11 +000084 }
85 }
86}
87
Mehdi Amini1aafabf2016-04-16 07:02:16 +000088// Collect for each module the list of function it defines (GUID -> Summary).
89void ModuleSummaryIndex::collectDefinedGVSummariesPerModule(
Teresa Johnson28e457b2016-04-24 14:57:11 +000090 StringMap<std::map<GlobalValue::GUID, GlobalValueSummary *>>
91 &ModuleToDefinedGVSummaries) const {
Mehdi Amini1aafabf2016-04-16 07:02:16 +000092 for (auto &GlobalList : *this) {
93 auto GUID = GlobalList.first;
Teresa Johnson28e457b2016-04-24 14:57:11 +000094 for (auto &Summary : GlobalList.second) {
95 ModuleToDefinedGVSummaries[Summary->modulePath()][GUID] = Summary.get();
Mehdi Amini1aafabf2016-04-16 07:02:16 +000096 }
97 }
98}
99
Teresa Johnson28e457b2016-04-24 14:57:11 +0000100GlobalValueSummary *
101ModuleSummaryIndex::getGlobalValueSummary(uint64_t ValueGUID,
102 bool PerModuleIndex) const {
103 auto SummaryList = findGlobalValueSummaryList(ValueGUID);
104 assert(SummaryList != end() && "GlobalValue not found in index");
105 assert((!PerModuleIndex || SummaryList->second.size() == 1) &&
Haojian Wu591ae462016-04-05 09:07:47 +0000106 "Expected a single entry per global value in per-module index");
Teresa Johnson28e457b2016-04-24 14:57:11 +0000107 auto &Summary = SummaryList->second[0];
108 return Summary.get();
Teresa Johnsonfb7c7642016-04-05 00:40:16 +0000109}