LLVM  15.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/StringExtras.h"
15 #include "llvm/CodeGen/Passes.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/InitializePasses.h"
18 #include "llvm/MC/MCSymbol.h"
19 #include "llvm/Pass.h"
21 #include <cassert>
22 #include <memory>
23 #include <string>
24 
25 using namespace llvm;
26 
27 namespace {
28 
29 class Printer : public FunctionPass {
30  static char ID;
31 
32  raw_ostream &OS;
33 
34 public:
35  explicit Printer(raw_ostream &OS) : FunctionPass(ID), OS(OS) {}
36 
37  StringRef getPassName() const override;
38  void getAnalysisUsage(AnalysisUsage &AU) const override;
39 
40  bool runOnFunction(Function &F) override;
41  bool doFinalization(Module &M) override;
42 };
43 
44 } // end anonymous namespace
45 
46 INITIALIZE_PASS(GCModuleInfo, "collector-metadata",
47  "Create Garbage Collector Module Metadata", false, false)
48 
49 // -----------------------------------------------------------------------------
50 
52  : F(F), S(S), FrameSize(~0LL) {}
53 
55 
56 // -----------------------------------------------------------------------------
57 
58 char GCModuleInfo::ID = 0;
59 
62 }
63 
65  assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
66  assert(F.hasGC());
67 
68  finfo_map_type::iterator I = FInfoMap.find(&F);
69  if (I != FInfoMap.end())
70  return *I->second;
71 
72  GCStrategy *S = getGCStrategy(F.getGC());
73  Functions.push_back(std::make_unique<GCFunctionInfo>(F, *S));
74  GCFunctionInfo *GFI = Functions.back().get();
75  FInfoMap[&F] = GFI;
76  return *GFI;
77 }
78 
80  Functions.clear();
81  FInfoMap.clear();
82  GCStrategyList.clear();
83 }
84 
85 // -----------------------------------------------------------------------------
86 
87 char Printer::ID = 0;
88 
90  return new Printer(OS);
91 }
92 
93 StringRef Printer::getPassName() const {
94  return "Print Garbage Collector Information";
95 }
96 
97 void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
99  AU.setPreservesAll();
101 }
102 
104  if (F.hasGC())
105  return false;
106 
107  GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F);
108 
109  OS << "GC roots for " << FD->getFunction().getName() << ":\n";
111  RE = FD->roots_end();
112  RI != RE; ++RI)
113  OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
114 
115  OS << "GC safe points for " << FD->getFunction().getName() << ":\n";
116  for (GCFunctionInfo::iterator PI = FD->begin(), PE = FD->end(); PI != PE;
117  ++PI) {
118 
119  OS << "\t" << PI->Label->getName() << ": " << "post-call"
120  << ", live = {";
121 
122  ListSeparator LS(",");
123  for (const GCRoot &R : make_range(FD->live_begin(PI), FD->live_end(PI)))
124  OS << LS << " " << R.Num;
125 
126  OS << " }\n";
127  }
128 
129  return false;
130 }
131 
132 bool Printer::doFinalization(Module &M) {
133  GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>();
134  assert(GMI && "Printer didn't require GCModuleInfo?!");
135  GMI->clear();
136  return false;
137 }
138 
140  // TODO: Arguably, just doing a linear search would be faster for small N
141  auto NMI = GCStrategyMap.find(Name);
142  if (NMI != GCStrategyMap.end())
143  return NMI->getValue();
144 
145  std::unique_ptr<GCStrategy> S = llvm::getGCStrategy(Name);
146  S->Name = std::string(Name);
147  GCStrategyMap[Name] = S.get();
148  GCStrategyList.push_back(std::move(S));
149  return GCStrategyList.back().get();
150 }
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
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:64
llvm::ImmutablePass
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition: Pass.h:279
llvm::Function
Definition: Function.h:60
Pass.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:57
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:89
llvm::initializeGCModuleInfoPass
void initializeGCModuleInfoPass(PassRegistry &)
llvm::GCFunctionInfo::~GCFunctionInfo
~GCFunctionInfo()
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::RISCVFenceField::R
@ R
Definition: RISCVBaseInfo.h:240
Printer
print alias Alias Set Printer
Definition: AliasSetTracker.cpp:733
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:139
INITIALIZE_PASS
INITIALIZE_PASS(GCModuleInfo, "collector-metadata", "Create Garbage Collector Module Metadata", false, false) GCFunctionInfo
Definition: GCMetadata.cpp:46
llvm::GCModuleInfo::clear
void clear()
clear - Resets the pass.
Definition: GCMetadata.cpp:79
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:54
llvm::GCFunctionInfo::live_end
live_iterator live_end(const iterator &p)
Definition: GCMetadata.h:145
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
Passes.h
llvm::DenseMapBase::clear
void clear()
Definition: DenseMap.h:112
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:64
I
#define I(x, y, z)
Definition: MD5.cpp:58
StringExtras.h
llvm::DenseMapBase::find
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:152
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:65
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:305
runOnFunction
static bool runOnFunction(Function &F, bool PostInlining)
Definition: EntryExitInstrumenter.cpp:69
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
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:84
llvm::AArch64CC::LS
@ LS
Definition: AArch64BaseInfo.h:264
Function.h
llvm::GCModuleInfo::GCModuleInfo
GCModuleInfo()
Definition: GCMetadata.cpp:60
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:97
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
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:38