summaryrefslogtreecommitdiffstats
path: root/contrib/llvm/lib/IR/ModuleSummaryIndex.cpp
blob: 9072f4bc7b121bb93e8bad9655679ce4b38f3ba2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//===-- ModuleSummaryIndex.cpp - Module Summary Index ---------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the module index and summary classes for the
// IR library.
//
//===----------------------------------------------------------------------===//

#include "llvm/IR/ModuleSummaryIndex.h"
#include "llvm/ADT/StringMap.h"
using namespace llvm;

// Create the combined module index/summary from multiple
// per-module instances.
void ModuleSummaryIndex::mergeFrom(std::unique_ptr<ModuleSummaryIndex> Other,
                                   uint64_t NextModuleId) {
  if (Other->modulePaths().empty())
    return;

  assert(Other->modulePaths().size() == 1 &&
         "Can only merge from an single-module index at that time");

  StringRef OtherModPath = Other->modulePaths().begin()->first();
  StringRef ModPath = addModulePath(OtherModPath, NextModuleId,
                                    Other->getModuleHash(OtherModPath))
                          ->first();

  for (auto &OtherGlobalValSummaryLists : *Other) {
    GlobalValue::GUID ValueGUID = OtherGlobalValSummaryLists.first;
    GlobalValueSummaryList &List = OtherGlobalValSummaryLists.second;

    // Assert that the value summary list only has one entry, since we shouldn't
    // have duplicate names within a single per-module index.
    assert(List.size() == 1);
    std::unique_ptr<GlobalValueSummary> Summary = std::move(List.front());

    // Note the module path string ref was copied above and is still owned by
    // the original per-module index. Reset it to the new module path
    // string reference owned by the combined index.
    Summary->setModulePath(ModPath);

    // Add new value summary to existing list. There may be duplicates when
    // combining GlobalValueMap entries, due to COMDAT values. Any local
    // values were given unique global IDs.
    addGlobalValueSummary(ValueGUID, std::move(Summary));
  }
}

void ModuleSummaryIndex::removeEmptySummaryEntries() {
  for (auto MI = begin(), MIE = end(); MI != MIE;) {
    // Only expect this to be called on a per-module index, which has a single
    // entry per value entry list.
    assert(MI->second.size() == 1);
    if (!MI->second[0])
      MI = GlobalValueMap.erase(MI);
    else
      ++MI;
  }
}

// Collect for the given module the list of function it defines
// (GUID -> Summary).
void ModuleSummaryIndex::collectDefinedFunctionsForModule(
    StringRef ModulePath, GVSummaryMapTy &GVSummaryMap) const {
  for (auto &GlobalList : *this) {
    auto GUID = GlobalList.first;
    for (auto &GlobSummary : GlobalList.second) {
      auto *Summary = dyn_cast_or_null<FunctionSummary>(GlobSummary.get());
      if (!Summary)
        // Ignore global variable, focus on functions
        continue;
      // Ignore summaries from other modules.
      if (Summary->modulePath() != ModulePath)
        continue;
      GVSummaryMap[GUID] = Summary;
    }
  }
}

// Collect for each module the list of function it defines (GUID -> Summary).
void ModuleSummaryIndex::collectDefinedGVSummariesPerModule(
    StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries) const {
  for (auto &GlobalList : *this) {
    auto GUID = GlobalList.first;
    for (auto &Summary : GlobalList.second) {
      ModuleToDefinedGVSummaries[Summary->modulePath()][GUID] = Summary.get();
    }
  }
}

GlobalValueSummary *
ModuleSummaryIndex::getGlobalValueSummary(uint64_t ValueGUID,
                                          bool PerModuleIndex) const {
  auto SummaryList = findGlobalValueSummaryList(ValueGUID);
  assert(SummaryList != end() && "GlobalValue not found in index");
  assert((!PerModuleIndex || SummaryList->second.size() == 1) &&
         "Expected a single entry per global value in per-module index");
  auto &Summary = SummaryList->second[0];
  return Summary.get();
}
OpenPOWER on IntegriCloud