Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 1 | //===-- ModuleSummaryIndex.cpp - Module Summary Index ---------------------===// |
Teresa Johnson | cec0cae | 2016-03-14 21:18:10 +0000 | [diff] [blame] | 2 | // |
| 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 Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 15 | #include "llvm/IR/ModuleSummaryIndex.h" |
Teresa Johnson | cec0cae | 2016-03-14 21:18:10 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringMap.h" |
| 17 | using namespace llvm; |
| 18 | |
| 19 | // Create the combined module index/summary from multiple |
| 20 | // per-module instances. |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 21 | void ModuleSummaryIndex::mergeFrom(std::unique_ptr<ModuleSummaryIndex> Other, |
| 22 | uint64_t NextModuleId) { |
Teresa Johnson | cec0cae | 2016-03-14 21:18:10 +0000 | [diff] [blame] | 23 | |
| 24 | StringRef ModPath; |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 25 | for (auto &OtherGlobalValSummaryLists : *Other) { |
| 26 | GlobalValue::GUID ValueGUID = OtherGlobalValSummaryLists.first; |
| 27 | GlobalValueSummaryList &List = OtherGlobalValSummaryLists.second; |
Teresa Johnson | cec0cae | 2016-03-14 21:18:10 +0000 | [diff] [blame] | 28 | |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 29 | // Assert that the value summary list only has one entry, since we shouldn't |
Teresa Johnson | cec0cae | 2016-03-14 21:18:10 +0000 | [diff] [blame] | 30 | // have duplicate names within a single per-module index. |
| 31 | assert(List.size() == 1); |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 32 | std::unique_ptr<GlobalValueSummary> Summary = std::move(List.front()); |
Teresa Johnson | cec0cae | 2016-03-14 21:18:10 +0000 | [diff] [blame] | 33 | |
| 34 | // Add the module path string ref for this module if we haven't already |
| 35 | // saved a reference to it. |
Mehdi Amini | d7ad221 | 2016-04-01 05:33:11 +0000 | [diff] [blame] | 36 | if (ModPath.empty()) { |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 37 | auto Path = Summary->modulePath(); |
Mehdi Amini | d7ad221 | 2016-04-01 05:33:11 +0000 | [diff] [blame] | 38 | ModPath = addModulePath(Path, NextModuleId, Other->getModuleHash(Path)) |
| 39 | ->first(); |
| 40 | } else |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 41 | assert(ModPath == Summary->modulePath() && |
Teresa Johnson | cec0cae | 2016-03-14 21:18:10 +0000 | [diff] [blame] | 42 | "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 Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 47 | Summary->setModulePath(ModPath); |
Teresa Johnson | cec0cae | 2016-03-14 21:18:10 +0000 | [diff] [blame] | 48 | |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 49 | // Add new value summary to existing list. There may be duplicates when |
Teresa Johnson | cec0cae | 2016-03-14 21:18:10 +0000 | [diff] [blame] | 50 | // combining GlobalValueMap entries, due to COMDAT values. Any local |
| 51 | // values were given unique global IDs. |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 52 | addGlobalValueSummary(ValueGUID, std::move(Summary)); |
Teresa Johnson | cec0cae | 2016-03-14 21:18:10 +0000 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 56 | void ModuleSummaryIndex::removeEmptySummaryEntries() { |
Teresa Johnson | cec0cae | 2016-03-14 21:18:10 +0000 | [diff] [blame] | 57 | 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 Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 61 | if (!MI->second[0]) |
Teresa Johnson | cec0cae | 2016-03-14 21:18:10 +0000 | [diff] [blame] | 62 | MI = GlobalValueMap.erase(MI); |
| 63 | else |
| 64 | ++MI; |
| 65 | } |
| 66 | } |
Teresa Johnson | fb7c764 | 2016-04-05 00:40:16 +0000 | [diff] [blame] | 67 | |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 68 | // Collect for the given module the list of function it defines |
| 69 | // (GUID -> Summary). |
| 70 | void ModuleSummaryIndex::collectDefinedFunctionsForModule( |
| 71 | StringRef ModulePath, |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 72 | std::map<GlobalValue::GUID, GlobalValueSummary *> &GVSummaryMap) const { |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 73 | for (auto &GlobalList : *this) { |
| 74 | auto GUID = GlobalList.first; |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 75 | for (auto &GlobSummary : GlobalList.second) { |
| 76 | auto *Summary = dyn_cast_or_null<FunctionSummary>(GlobSummary.get()); |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 77 | 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 Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 83 | GVSummaryMap[GUID] = Summary; |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 88 | // Collect for each module the list of function it defines (GUID -> Summary). |
| 89 | void ModuleSummaryIndex::collectDefinedGVSummariesPerModule( |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 90 | StringMap<std::map<GlobalValue::GUID, GlobalValueSummary *>> |
| 91 | &ModuleToDefinedGVSummaries) const { |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 92 | for (auto &GlobalList : *this) { |
| 93 | auto GUID = GlobalList.first; |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 94 | for (auto &Summary : GlobalList.second) { |
| 95 | ModuleToDefinedGVSummaries[Summary->modulePath()][GUID] = Summary.get(); |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 100 | GlobalValueSummary * |
| 101 | ModuleSummaryIndex::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 Wu | 591ae46 | 2016-04-05 09:07:47 +0000 | [diff] [blame] | 106 | "Expected a single entry per global value in per-module index"); |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 107 | auto &Summary = SummaryList->second[0]; |
| 108 | return Summary.get(); |
Teresa Johnson | fb7c764 | 2016-04-05 00:40:16 +0000 | [diff] [blame] | 109 | } |