LLVM 18.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
15#include "llvm/CodeGen/Passes.h"
16#include "llvm/IR/Function.h"
18#include "llvm/MC/MCSymbol.h"
19#include "llvm/Pass.h"
21#include <cassert>
22#include <memory>
23#include <string>
24
25using namespace llvm;
26
27namespace {
28
29class Printer : public FunctionPass {
30 static char ID;
31
33
34public:
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
46INITIALIZE_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
58char 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
87char Printer::ID = 0;
88
90 return new Printer(OS);
91}
92
93StringRef Printer::getPassName() const {
94 return "Print Garbage Collector Information";
95}
96
97void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
99 AU.setPreservesAll();
101}
102
103bool Printer::runOnFunction(Function &F) {
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
132bool 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}
aarch64 promote const
dxil pretty DXIL Metadata Pretty Printer
std::string Name
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file contains some functions that are useful when dealing with strings.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:155
iterator end()
Definition: DenseMap.h:84
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
Garbage collection metadata for a single function.
Definition: GCMetadata.h:77
std::vector< GCRoot >::iterator roots_iterator
Definition: GCMetadata.h:80
iterator begin()
begin/end - Iterators for safe points.
Definition: GCMetadata.h:134
live_iterator live_begin(const iterator &p)
live_begin/live_end - Iterators for live roots at a given safe point.
Definition: GCMetadata.h:144
live_iterator live_end(const iterator &p)
Definition: GCMetadata.h:145
roots_iterator roots_end()
Definition: GCMetadata.h:140
std::vector< GCPoint >::iterator iterator
Definition: GCMetadata.h:79
const Function & getFunction() const
getFunction - Return the function to which this metadata applies.
Definition: GCMetadata.h:105
roots_iterator roots_begin()
roots_begin/roots_end - Iterators for all roots in the function.
Definition: GCMetadata.h:139
An analysis pass which caches information about the entire Module.
Definition: GCMetadata.h:152
GCFunctionInfo & getFunctionInfo(const Function &F)
get - Look up function metadata.
Definition: GCMetadata.cpp:64
static char ID
Definition: GCMetadata.h:183
void clear()
clear - Resets the pass.
Definition: GCMetadata.cpp:79
GCStrategy * getGCStrategy(const StringRef Name)
Lookup the GCStrategy object associated with the given gc name.
Definition: GCMetadata.cpp:139
GCStrategy describes a garbage collector algorithm's code generation requirements,...
Definition: GCStrategy.h:63
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition: Pass.h:282
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
virtual bool doFinalization(Module &)
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
Definition: Pass.h:123
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void initializeGCModuleInfoPass(PassRegistry &)
FunctionPass * createGCInfoPrinter(raw_ostream &OS)
Creates a pass to print GC metadata.
Definition: GCMetadata.cpp:89
std::unique_ptr< GCStrategy > getGCStrategy(const StringRef Name)
Lookup the GCStrategy object associated with the given gc name.
Definition: GCStrategy.cpp:24
GCRoot - Metadata for a pointer to an object managed by the garbage collector.
Definition: GCMetadata.h:66