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