LLVM  4.0.0
GCMetadata.cpp
Go to the documentation of this file.
1 //===-- GCMetadata.cpp - Garbage collector metadata -----------------------===//
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 GCFunctionInfo class and GCModuleInfo pass.
11 //
12 //===----------------------------------------------------------------------===//
13 
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/Pass.h"
21 #include "llvm/Support/Debug.h"
24 using namespace llvm;
25 
26 namespace {
27 
28 class Printer : public FunctionPass {
29  static char ID;
30  raw_ostream &OS;
31 
32 public:
33  explicit Printer(raw_ostream &OS) : FunctionPass(ID), OS(OS) {}
34 
35  StringRef getPassName() const override;
36  void getAnalysisUsage(AnalysisUsage &AU) const override;
37 
38  bool runOnFunction(Function &F) override;
39  bool doFinalization(Module &M) override;
40 };
41 }
42 
43 INITIALIZE_PASS(GCModuleInfo, "collector-metadata",
44  "Create Garbage Collector Module Metadata", false, false)
45 
46 // -----------------------------------------------------------------------------
47 
49  : F(F), S(S), FrameSize(~0LL) {}
50 
52 
53 // -----------------------------------------------------------------------------
54 
55 char GCModuleInfo::ID = 0;
56 
59 }
60 
62  assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
63  assert(F.hasGC());
64 
65  finfo_map_type::iterator I = FInfoMap.find(&F);
66  if (I != FInfoMap.end())
67  return *I->second;
68 
69  GCStrategy *S = getGCStrategy(F.getGC());
70  Functions.push_back(make_unique<GCFunctionInfo>(F, *S));
71  GCFunctionInfo *GFI = Functions.back().get();
72  FInfoMap[&F] = GFI;
73  return *GFI;
74 }
75 
77  Functions.clear();
78  FInfoMap.clear();
79  GCStrategyList.clear();
80 }
81 
82 // -----------------------------------------------------------------------------
83 
84 char Printer::ID = 0;
85 
87  return new Printer(OS);
88 }
89 
90 StringRef Printer::getPassName() const {
91  return "Print Garbage Collector Information";
92 }
93 
94 void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
96  AU.setPreservesAll();
98 }
99 
100 static const char *DescKind(GC::PointKind Kind) {
101  switch (Kind) {
102  case GC::PreCall:
103  return "pre-call";
104  case GC::PostCall:
105  return "post-call";
106  }
107  llvm_unreachable("Invalid point kind");
108 }
109 
110 bool Printer::runOnFunction(Function &F) {
111  if (F.hasGC())
112  return false;
113 
114  GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F);
115 
116  OS << "GC roots for " << FD->getFunction().getName() << ":\n";
118  RE = FD->roots_end();
119  RI != RE; ++RI)
120  OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
121 
122  OS << "GC safe points for " << FD->getFunction().getName() << ":\n";
123  for (GCFunctionInfo::iterator PI = FD->begin(), PE = FD->end(); PI != PE;
124  ++PI) {
125 
126  OS << "\t" << PI->Label->getName() << ": " << DescKind(PI->Kind)
127  << ", live = {";
128 
129  for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI),
130  RE = FD->live_end(PI);
131  ;) {
132  OS << " " << RI->Num;
133  if (++RI == RE)
134  break;
135  OS << ",";
136  }
137 
138  OS << " }\n";
139  }
140 
141  return false;
142 }
143 
144 bool Printer::doFinalization(Module &M) {
145  GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>();
146  assert(GMI && "Printer didn't require GCModuleInfo?!");
147  GMI->clear();
148  return false;
149 }
150 
152  // TODO: Arguably, just doing a linear search would be faster for small N
153  auto NMI = GCStrategyMap.find(Name);
154  if (NMI != GCStrategyMap.end())
155  return NMI->getValue();
156 
157  for (auto& Entry : GCRegistry::entries()) {
158  if (Name == Entry.getName()) {
159  std::unique_ptr<GCStrategy> S = Entry.instantiate();
160  S->Name = Name;
161  GCStrategyMap[Name] = S.get();
162  GCStrategyList.push_back(std::move(S));
163  return GCStrategyList.back().get();
164  }
165  }
166 
167  if (GCRegistry::begin() == GCRegistry::end()) {
168  // In normal operation, the registry should not be empty. There should
169  // be the builtin GCs if nothing else. The most likely scenario here is
170  // that we got here without running the initializers used by the Registry
171  // itself and it's registration mechanism.
172  const std::string error = ("unsupported GC: " + Name).str() +
173  " (did you remember to link and initialize the CodeGen library?)";
174  report_fatal_error(error);
175  } else
176  report_fatal_error(std::string("unsupported GC: ") + Name);
177 }
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:84
static iterator_range< iterator > entries()
Definition: Registry.h:102
PointKind
PointKind - Used to indicate whether the address of the call instruction or the address after the cal...
Definition: GCStrategy.h:67
print alias Alias Set Printer
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:191
std::vector< GCPoint >::iterator iterator
Definition: GCMetadata.h:76
AnalysisUsage & addRequired()
bool hasGC() const
hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm to use during code generatio...
Definition: Function.h:250
GCFunctionInfo & getFunctionInfo(const Function &F)
get - Look up function metadata.
Definition: GCMetadata.cpp:61
An analysis pass which caches information about the entire Module.
Definition: GCMetadata.h:155
#define F(x, y, z)
Definition: MD5.cpp:51
FunctionPass * createGCInfoPrinter(raw_ostream &OS)
Creates a pass to print GC metadata.
Definition: GCMetadata.cpp:86
roots_iterator roots_end()
Definition: GCMetadata.h:142
Instr is the return address of a call.
Definition: GCStrategy.h:69
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
roots_iterator roots_begin()
roots_begin/roots_end - Iterators for all roots in the function.
Definition: GCMetadata.h:141
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static iterator begin()
ImmutablePass class - This class is used to provide information that does not need to be run...
Definition: Pass.h:266
live_iterator live_begin(const iterator &p)
live_begin/live_end - Iterators for live roots at a given safe point.
Definition: GCMetadata.h:147
GCStrategy * getGCStrategy(const StringRef Name)
Lookup the GCStrategy object associated with the given gc name.
Definition: GCMetadata.cpp:151
void setPreservesAll()
Set by analyses that do not transform their input at all.
iterator begin()
begin/end - Iterators for safe points.
Definition: GCMetadata.h:135
static char ID
Definition: GCMetadata.h:186
INITIALIZE_PASS(GCModuleInfo,"collector-metadata","Create Garbage Collector Module Metadata", false, false) GCFunctionInfo
Definition: GCMetadata.cpp:43
GCStrategy describes a garbage collector algorithm's code generation requirements, and provides overridable hooks for those needs which cannot be abstractly described.
Definition: GCStrategy.h:78
void clear()
clear - Resets the pass.
Definition: GCMetadata.cpp:76
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:188
const std::string & getGC() const
Definition: Function.cpp:412
void initializeGCModuleInfoPass(PassRegistry &)
#define I(x, y, z)
Definition: MD5.cpp:54
iterator end()
Definition: DenseMap.h:69
iterator find(const KeyT &Val)
Definition: DenseMap.h:127
Instr is a call instruction.
Definition: GCStrategy.h:68
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
aarch64 promote const
const Function & getFunction() const
getFunction - Return the function to which this metadata applies.
Definition: GCMetadata.h:103
Error error(const Twine &Message)
static iterator end()
Definition: Registry.h:100
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
live_iterator live_end(const iterator &p)
Definition: GCMetadata.h:148
Garbage collection metadata for a single function.
Definition: GCMetadata.h:74
static const char * DescKind(GC::PointKind Kind)
Definition: GCMetadata.cpp:100
std::vector< GCRoot >::iterator roots_iterator
Definition: GCMetadata.h:77
std::vector< GCRoot >::const_iterator live_iterator
Definition: GCMetadata.h:78