LLVM  9.0.0svn
GCMetadata.cpp
Go to the documentation of this file.
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"
16 #include "llvm/CodeGen/Passes.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/MC/MCSymbol.h"
19 #include "llvm/Pass.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  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 
54  : F(F), S(S), FrameSize(~0LL) {}
55 
57 
58 // -----------------------------------------------------------------------------
59 
60 char GCModuleInfo::ID = 0;
61 
64 }
65 
67  assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
68  assert(F.hasGC());
69 
70  finfo_map_type::iterator I = FInfoMap.find(&F);
71  if (I != FInfoMap.end())
72  return *I->second;
73 
74  GCStrategy *S = getGCStrategy(F.getGC());
75  Functions.push_back(llvm::make_unique<GCFunctionInfo>(F, *S));
76  GCFunctionInfo *GFI = Functions.back().get();
77  FInfoMap[&F] = GFI;
78  return *GFI;
79 }
80 
82  Functions.clear();
83  FInfoMap.clear();
84  GCStrategyList.clear();
85 }
86 
87 // -----------------------------------------------------------------------------
88 
89 char Printer::ID = 0;
90 
92  return new Printer(OS);
93 }
94 
95 StringRef Printer::getPassName() const {
96  return "Print Garbage Collector Information";
97 }
98 
99 void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
101  AU.setPreservesAll();
103 }
104 
106  if (F.hasGC())
107  return false;
108 
109  GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F);
110 
111  OS << "GC roots for " << FD->getFunction().getName() << ":\n";
113  RE = FD->roots_end();
114  RI != RE; ++RI)
115  OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
116 
117  OS << "GC safe points for " << FD->getFunction().getName() << ":\n";
118  for (GCFunctionInfo::iterator PI = FD->begin(), PE = FD->end(); PI != PE;
119  ++PI) {
120 
121  OS << "\t" << PI->Label->getName() << ": " << "post-call"
122  << ", live = {";
123 
124  for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI),
125  RE = FD->live_end(PI);
126  ;) {
127  OS << " " << RI->Num;
128  if (++RI == RE)
129  break;
130  OS << ",";
131  }
132 
133  OS << " }\n";
134  }
135 
136  return false;
137 }
138 
139 bool Printer::doFinalization(Module &M) {
140  GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>();
141  assert(GMI && "Printer didn't require GCModuleInfo?!");
142  GMI->clear();
143  return false;
144 }
145 
147  // TODO: Arguably, just doing a linear search would be faster for small N
148  auto NMI = GCStrategyMap.find(Name);
149  if (NMI != GCStrategyMap.end())
150  return NMI->getValue();
151 
152  for (auto& Entry : GCRegistry::entries()) {
153  if (Name == Entry.getName()) {
154  std::unique_ptr<GCStrategy> S = Entry.instantiate();
155  S->Name = Name;
156  GCStrategyMap[Name] = S.get();
157  GCStrategyList.push_back(std::move(S));
158  return GCStrategyList.back().get();
159  }
160  }
161 
162  if (GCRegistry::begin() == GCRegistry::end()) {
163  // In normal operation, the registry should not be empty. There should
164  // be the builtin GCs if nothing else. The most likely scenario here is
165  // that we got here without running the initializers used by the Registry
166  // itself and it's registration mechanism.
167  const std::string error = ("unsupported GC: " + Name).str() +
168  " (did you remember to link and initialize the CodeGen library?)";
169  report_fatal_error(error);
170  } else
171  report_fatal_error(std::string("unsupported GC: ") + Name);
172 }
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:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
INITIALIZE_PASS(GCModuleInfo, "collector-metadata", "Create Garbage Collector Module Metadata", false, false) GCFunctionInfo
Definition: GCMetadata.cpp:48
#define error(X)
std::string Name
F(f)
static iterator_range< iterator > entries()
Definition: Registry.h:101
print alias Alias Set Printer
AnalysisUsage & addRequired()
GCFunctionInfo & getFunctionInfo(const Function &F)
get - Look up function metadata.
Definition: GCMetadata.cpp:66
An analysis pass which caches information about the entire Module.
Definition: GCMetadata.h:152
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:96
const std::string & getGC() const
Definition: Function.cpp:477
FunctionPass * createGCInfoPrinter(raw_ostream &OS)
Creates a pass to print GC metadata.
Definition: GCMetadata.cpp:91
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:140
const Function & getFunction() const
getFunction - Return the function to which this metadata applies.
Definition: GCMetadata.h:105
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
roots_iterator roots_begin()
roots_begin/roots_end - Iterators for all roots in the function.
Definition: GCMetadata.h:139
std::vector< GCRoot >::iterator roots_iterator
Definition: GCMetadata.h:80
static iterator begin()
ImmutablePass class - This class is used to provide information that does not need to be run...
Definition: Pass.h:255
live_iterator live_begin(const iterator &p)
live_begin/live_end - Iterators for live roots at a given safe point.
Definition: GCMetadata.h:144
GCStrategy * getGCStrategy(const StringRef Name)
Lookup the GCStrategy object associated with the given gc name.
Definition: GCMetadata.cpp:146
void setPreservesAll()
Set by analyses that do not transform their input at all.
iterator begin()
begin/end - Iterators for safe points.
Definition: GCMetadata.h:134
static char ID
Definition: GCMetadata.h:183
std::vector< GCPoint >::iterator iterator
Definition: GCMetadata.h:79
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:66
bool hasGC() const
hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm to use during code generatio...
Definition: Function.h:351
void clear()
clear - Resets the pass.
Definition: GCMetadata.cpp:81
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:108
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:227
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
aarch64 promote const
static iterator end()
Definition: Registry.h:99
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
std::vector< GCRoot >::const_iterator live_iterator
Definition: GCMetadata.h:81
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
live_iterator live_end(const iterator &p)
Definition: GCMetadata.h:145
Garbage collection metadata for a single function.
Definition: GCMetadata.h:77