LLVM  14.0.0git
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 
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/CodeGen/Passes.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/InitializePasses.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(std::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  ListSeparator LS(",");
126  for (const GCRoot &R : make_range(FD->live_begin(PI), FD->live_end(PI)))
127  OS << LS << " " << R.Num;
128 
129  OS << " }\n";
130  }
131 
132  return false;
133 }
134 
135 bool Printer::doFinalization(Module &M) {
136  GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>();
137  assert(GMI && "Printer didn't require GCModuleInfo?!");
138  GMI->clear();
139  return false;
140 }
141 
143  // TODO: Arguably, just doing a linear search would be faster for small N
144  auto NMI = GCStrategyMap.find(Name);
145  if (NMI != GCStrategyMap.end())
146  return NMI->getValue();
147 
148  std::unique_ptr<GCStrategy> S = llvm::getGCStrategy(Name);
149  S->Name = std::string(Name);
150  GCStrategyMap[Name] = S.get();
151  GCStrategyList.push_back(std::move(S));
152  return GCStrategyList.back().get();
153 }
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
GCMetadata.h
llvm::make_range
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Definition: iterator_range.h:53
llvm::GCFunctionInfo::live_begin
live_iterator live_begin(const iterator &p)
live_begin/live_end - Iterators for live roots at a given safe point.
Definition: GCMetadata.h:144
llvm::GCStrategy
GCStrategy describes a garbage collector algorithm's code generation requirements,...
Definition: GCStrategy.h:66
llvm::ImmutablePass
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition: Pass.h:269
llvm::Function
Definition: Function.h:62
Pass.h
ErrorHandling.h
llvm::GCRoot
GCRoot - Metadata for a pointer to an object managed by the garbage collector.
Definition: GCMetadata.h:66
llvm::GCFunctionInfo
Garbage collection metadata for a single function.
Definition: GCMetadata.h:77
llvm::GCFunctionInfo::iterator
std::vector< GCPoint >::iterator iterator
Definition: GCMetadata.h:79
llvm::DenseMapIterator
Definition: DenseMap.h:56
llvm::GCFunctionInfo::roots_begin
roots_iterator roots_begin()
roots_begin/roots_end - Iterators for all roots in the function.
Definition: GCMetadata.h:139
llvm::createGCInfoPrinter
FunctionPass * createGCInfoPrinter(raw_ostream &OS)
Creates a pass to print GC metadata.
Definition: GCMetadata.cpp:92
STLExtras.h
llvm::initializeGCModuleInfoPass
void initializeGCModuleInfoPass(PassRegistry &)
llvm::GCFunctionInfo::~GCFunctionInfo
~GCFunctionInfo()
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::RISCVFenceField::R
@ R
Definition: RISCVBaseInfo.h:198
Printer
print alias Alias Set Printer
Definition: AliasSetTracker.cpp:762
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
llvm::GCFunctionInfo::begin
iterator begin()
begin/end - Iterators for safe points.
Definition: GCMetadata.h:134
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
MCSymbol.h
llvm::GCModuleInfo::getGCStrategy
GCStrategy * getGCStrategy(const StringRef Name)
Lookup the GCStrategy object associated with the given gc name.
Definition: GCMetadata.cpp:142
INITIALIZE_PASS
INITIALIZE_PASS(GCModuleInfo, "collector-metadata", "Create Garbage Collector Module Metadata", false, false) GCFunctionInfo
Definition: GCMetadata.cpp:49
llvm::GCModuleInfo::clear
void clear()
clear - Resets the pass.
Definition: GCMetadata.cpp:82
llvm::GCModuleInfo::ID
static char ID
Definition: GCMetadata.h:183
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::GCFunctionInfo::live_end
live_iterator live_end(const iterator &p)
Definition: GCMetadata.h:145
Passes.h
llvm::DenseMapBase::clear
void clear()
Definition: DenseMap.h:111
const
aarch64 promote const
Definition: AArch64PromoteConstant.cpp:232
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::GCModuleInfo::getFunctionInfo
GCFunctionInfo & getFunctionInfo(const Function &F)
get - Look up function metadata.
Definition: GCMetadata.cpp:67
I
#define I(x, y, z)
Definition: MD5.cpp:59
StringExtras.h
llvm::DenseMapBase::find
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:150
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::GCFunctionInfo::end
iterator end()
Definition: GCMetadata.h:135
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::Value::getName
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
runOnFunction
static bool runOnFunction(Function &F, bool PostInlining)
Definition: EntryExitInstrumenter.cpp:69
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::AnalysisUsage::setPreservesAll
void setPreservesAll()
Set by analyses that do not transform their input at all.
Definition: PassAnalysisSupport.h:130
llvm::DenseMapBase::end
iterator end()
Definition: DenseMap.h:83
llvm::AArch64CC::LS
@ LS
Definition: AArch64BaseInfo.h:264
Function.h
llvm::GCModuleInfo::GCModuleInfo
GCModuleInfo()
Definition: GCMetadata.cpp:63
llvm::getGCStrategy
std::unique_ptr< GCStrategy > getGCStrategy(const StringRef Name)
Lookup the GCStrategy object associated with the given gc name.
Definition: GCStrategy.cpp:22
llvm::Pass::getAnalysisUsage
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:93
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
llvm::GCFunctionInfo::roots_iterator
std::vector< GCRoot >::iterator roots_iterator
Definition: GCMetadata.h:80
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition: PassAnalysisSupport.h:75
raw_ostream.h
llvm::GCFunctionInfo::roots_end
roots_iterator roots_end()
Definition: GCMetadata.h:140
llvm::GCModuleInfo
An analysis pass which caches information about the entire Module.
Definition: GCMetadata.h:152
InitializePasses.h
llvm::GCFunctionInfo::getFunction
const Function & getFunction() const
getFunction - Return the function to which this metadata applies.
Definition: GCMetadata.h:105
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37