1 //===-- llvm/CodeGen/Collector.h - Garbage collection -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Collector records sufficient information about a machine function to enable
11 // accurate garbage collectors. Specifically:
14 // Garbage collection is only possible at certain points in code. Code
15 // generators should record points:
17 // - At and after any call to a subroutine
18 // - Before returning from the current function
19 // - Before backwards branches (loops)
22 // When a reference to a GC-allocated object exists on the stack, it must be
23 // stored in an alloca registered with llvm.gcoot.
25 // This generic information should used by ABI-specific passes to emit support
26 // tables for the runtime garbage collector.
28 // MachineCodeAnalysis identifies the GC safe points in the machine code. (Roots
29 // are identified in SelectionDAGISel.)
31 //===----------------------------------------------------------------------===//
33 #ifndef LLVM_CODEGEN_COLLECTOR_H
34 #define LLVM_CODEGEN_COLLECTOR_H
36 #include "llvm/CodeGen/GCMetadata.h"
42 /// Collector describes a garbage collector's code generation requirements,
43 /// and provides overridable hooks for those needs which cannot be abstractly
47 typedef std::vector<CollectorMetadata*> list_type;
48 typedef list_type::iterator iterator;
51 friend class CollectorModuleMetadata;
58 unsigned NeededSafePoints; //< Bitmask of required safe points.
59 bool CustomReadBarriers; //< Default is to insert loads.
60 bool CustomWriteBarriers; //< Default is to insert stores.
61 bool CustomRoots; //< Default is to pass through to backend.
62 bool InitRoots; //< If set, roots are nulled during lowering.
63 bool UsesMetadata; //< If set, backend must emit metadata tables.
71 /// getName - The name of the collector, for debugging.
73 const std::string &getName() const { return Name; }
75 /// getModule - The module upon which the collector is operating.
77 const Module &getModule() const { return *M; }
79 /// True if this collector requires safe points of any kind. By default,
80 /// none are recorded.
81 bool needsSafePoints() const { return NeededSafePoints != 0; }
83 /// True if the collector requires the given kind of safe point. By default,
84 /// none are recorded.
85 bool needsSafePoint(GC::PointKind Kind) const {
86 return (NeededSafePoints & 1 << Kind) != 0;
89 /// By default, write barriers are replaced with simple store instructions.
90 /// If true, then addPassesToCustomLowerIntrinsics must instead process
92 bool customWriteBarrier() const { return CustomWriteBarriers; }
94 /// By default, read barriers are replaced with simple load instructions.
95 /// If true, then addPassesToCustomLowerIntrinsics must instead process
97 bool customReadBarrier() const { return CustomReadBarriers; }
99 /// By default, roots are left for the code generator. If Custom, then
100 /// addPassesToCustomLowerIntrinsics must add passes to delete them.
101 bool customRoots() const { return CustomRoots; }
103 /// If set, gcroot intrinsics should initialize their allocas to null. This
104 /// is necessary for most collectors.
105 bool initializeRoots() const { return InitRoots; }
107 /// If set, appropriate metadata tables must be emitted by the back-end
108 /// (assembler, JIT, or otherwise).
109 bool usesMetadata() const { return UsesMetadata; }
111 /// begin/end - Iterators for function metadata.
113 iterator begin() { return Functions.begin(); }
114 iterator end() { return Functions.end(); }
116 /// insertFunctionMetadata - Creates metadata for a function.
118 CollectorMetadata *insertFunctionMetadata(const Function &F);
120 /// initializeCustomLowering/performCustomLowering - If any of the actions
121 /// are set to custom, performCustomLowering must be overriden to create a
122 /// transform to lower those actions to LLVM IR. initializeCustomLowering
123 /// is optional to override. These are the only Collector methods through
124 /// which the LLVM IR can be modified.
125 virtual bool initializeCustomLowering(Module &F);
126 virtual bool performCustomLowering(Function &F);
129 // GCMetadataPrinter - Emits GC metadata as assembly code.
130 class GCMetadataPrinter {
132 typedef Collector::list_type list_type;
133 typedef Collector::iterator iterator;
138 friend class AsmPrinter;
141 // May only be subclassed.
145 GCMetadataPrinter(const GCMetadataPrinter &);
146 GCMetadataPrinter &operator=(const GCMetadataPrinter &);
149 Collector &getCollector() { return *Coll; }
150 const Module &getModule() const { return Coll->getModule(); }
152 iterator begin() { return Coll->begin(); }
153 iterator end() { return Coll->end(); }
155 /// beginAssembly/finishAssembly - Emit module metadata as assembly code.
156 virtual void beginAssembly(std::ostream &OS, AsmPrinter &AP,
157 const TargetAsmInfo &TAI);
159 virtual void finishAssembly(std::ostream &OS, AsmPrinter &AP,
160 const TargetAsmInfo &TAI);
162 virtual ~GCMetadataPrinter();