Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/CodeGen/GCMetadata.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- GCMetadata.cpp - Garbage collector metadata -----------------------===//
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 GCFunctionInfo class and GCModuleInfo pass.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/ADT/STLExtras.h"
14
#include "llvm/CodeGen/GCMetadata.h"
15
#include "llvm/CodeGen/GCStrategy.h"
16
#include "llvm/CodeGen/Passes.h"
17
#include "llvm/IR/Function.h"
18
#include "llvm/MC/MCSymbol.h"
19
#include "llvm/Pass.h"
20
#include "llvm/Support/ErrorHandling.h"
21
#include "llvm/Support/raw_ostream.h"
22
#include <algorithm>
23
#include <cassert>
24
#include <memory>
25
#include <string>
26
27
using namespace llvm;
28
29
namespace {
30
31
class Printer : public FunctionPass {
32
  static char ID;
33
34
  raw_ostream &OS;
35
36
public:
37
0
  explicit Printer(raw_ostream &OS) : FunctionPass(ID), OS(OS) {}
38
39
  StringRef getPassName() const override;
40
  void getAnalysisUsage(AnalysisUsage &AU) const override;
41
42
  bool runOnFunction(Function &F) override;
43
  bool doFinalization(Module &M) override;
44
};
45
46
} // end anonymous namespace
47
48
INITIALIZE_PASS(GCModuleInfo, "collector-metadata",
49
                "Create Garbage Collector Module Metadata", false, false)
50
51
// -----------------------------------------------------------------------------
52
53
GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S)
54
81
    : F(F), S(S), FrameSize(~0LL) {}
55
56
81
GCFunctionInfo::~GCFunctionInfo() = default;
57
58
// -----------------------------------------------------------------------------
59
60
char GCModuleInfo::ID = 0;
61
62
36.2k
GCModuleInfo::GCModuleInfo() : ImmutablePass(ID) {
63
36.2k
  initializeGCModuleInfoPass(*PassRegistry::getPassRegistry());
64
36.2k
}
65
66
324
GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) {
67
324
  assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
68
324
  assert(F.hasGC());
69
324
70
324
  finfo_map_type::iterator I = FInfoMap.find(&F);
71
324
  if (I != FInfoMap.end())
72
243
    return *I->second;
73
81
74
81
  GCStrategy *S = getGCStrategy(F.getGC());
75
81
  Functions.push_back(llvm::make_unique<GCFunctionInfo>(F, *S));
76
81
  GCFunctionInfo *GFI = Functions.back().get();
77
81
  FInfoMap[&F] = GFI;
78
81
  return *GFI;
79
81
}
80
81
0
void GCModuleInfo::clear() {
82
0
  Functions.clear();
83
0
  FInfoMap.clear();
84
0
  GCStrategyList.clear();
85
0
}
86
87
// -----------------------------------------------------------------------------
88
89
char Printer::ID = 0;
90
91
0
FunctionPass *llvm::createGCInfoPrinter(raw_ostream &OS) {
92
0
  return new Printer(OS);
93
0
}
94
95
0
StringRef Printer::getPassName() const {
96
0
  return "Print Garbage Collector Information";
97
0
}
98
99
0
void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
100
0
  FunctionPass::getAnalysisUsage(AU);
101
0
  AU.setPreservesAll();
102
0
  AU.addRequired<GCModuleInfo>();
103
0
}
104
105
0
bool Printer::runOnFunction(Function &F) {
106
0
  if (F.hasGC())
107
0
    return false;
108
0
109
0
  GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F);
110
0
111
0
  OS << "GC roots for " << FD->getFunction().getName() << ":\n";
112
0
  for (GCFunctionInfo::roots_iterator RI = FD->roots_begin(),
113
0
                                      RE = FD->roots_end();
114
0
       RI != RE; ++RI)
115
0
    OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
116
0
117
0
  OS << "GC safe points for " << FD->getFunction().getName() << ":\n";
118
0
  for (GCFunctionInfo::iterator PI = FD->begin(), PE = FD->end(); PI != PE;
119
0
       ++PI) {
120
0
121
0
    OS << "\t" << PI->Label->getName() << ": " << "post-call"
122
0
       << ", live = {";
123
0
124
0
    for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI),
125
0
                                       RE = FD->live_end(PI);
126
0
         ;) {
127
0
      OS << " " << RI->Num;
128
0
      if (++RI == RE)
129
0
        break;
130
0
      OS << ",";
131
0
    }
132
0
133
0
    OS << " }\n";
134
0
  }
135
0
136
0
  return false;
137
0
}
138
139
0
bool Printer::doFinalization(Module &M) {
140
0
  GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>();
141
0
  assert(GMI && "Printer didn't require GCModuleInfo?!");
142
0
  GMI->clear();
143
0
  return false;
144
0
}
145
146
81
GCStrategy *GCModuleInfo::getGCStrategy(const StringRef Name) {
147
81
  // TODO: Arguably, just doing a linear search would be faster for small N
148
81
  auto NMI = GCStrategyMap.find(Name);
149
81
  if (NMI != GCStrategyMap.end())
150
55
    return NMI->getValue();
151
26
152
89
  
for (auto& Entry : GCRegistry::entries())26
{
153
89
    if (Name == Entry.getName()) {
154
26
      std::unique_ptr<GCStrategy> S = Entry.instantiate();
155
26
      S->Name = Name;
156
26
      GCStrategyMap[Name] = S.get();
157
26
      GCStrategyList.push_back(std::move(S));
158
26
      return GCStrategyList.back().get();
159
26
    }
160
89
  }
161
26
162
26
  
if (0
GCRegistry::begin() == GCRegistry::end()0
) {
163
0
    // In normal operation, the registry should not be empty.  There should
164
0
    // be the builtin GCs if nothing else.  The most likely scenario here is
165
0
    // that we got here without running the initializers used by the Registry
166
0
    // itself and it's registration mechanism.
167
0
    const std::string error = ("unsupported GC: " + Name).str() +
168
0
      " (did you remember to link and initialize the CodeGen library?)";
169
0
    report_fatal_error(error);
170
0
  } else
171
0
    report_fatal_error(std::string("unsupported GC: ") + Name);
172
0
}