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/CollectorMetadata.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.
70 /// getName - The name of the collector, for debugging.
72 const std::string &getName() const { return Name; }
74 /// getModule - The module upon which the collector is operating.
76 const Module &getModule() const { return *M; }
78 /// True if this collector requires safe points of any kind. By default,
79 /// none are recorded.
80 bool needsSafePoints() const { return NeededSafePoints != 0; }
82 /// True if the collector requires the given kind of safe point. By default,
83 /// none are recorded.
84 bool needsSafePoint(GC::PointKind Kind) const {
85 return (NeededSafePoints & 1 << Kind) != 0;
88 /// By default, write barriers are replaced with simple store instructions.
89 /// If true, then addPassesToCustomLowerIntrinsics must instead process
91 bool customWriteBarrier() const { return CustomWriteBarriers; }
93 /// By default, read barriers are replaced with simple load instructions.
94 /// If true, then addPassesToCustomLowerIntrinsics must instead process
96 bool customReadBarrier() const { return CustomReadBarriers; }
98 /// By default, roots are left for the code generator. If Custom, then
99 /// addPassesToCustomLowerIntrinsics must add passes to delete them.
100 bool customRoots() const { return CustomRoots; }
102 /// If set, gcroot intrinsics should initialize their allocas to null. This
103 /// is necessary for most collectors.
104 bool initializeRoots() const { return InitRoots; }
107 /// beginAssembly/finishAssembly - Emit module metadata as assembly code.
108 virtual void beginAssembly(std::ostream &OS, AsmPrinter &AP,
109 const TargetAsmInfo &TAI);
110 virtual void finishAssembly(std::ostream &OS, AsmPrinter &AP,
111 const TargetAsmInfo &TAI);
113 /// begin/end - Iterators for function metadata.
115 iterator begin() { return Functions.begin(); }
116 iterator end() { return Functions.end(); }
118 /// insertFunctionMetadata - Creates metadata for a function.
120 CollectorMetadata *insertFunctionMetadata(const Function &F);
122 /// initializeCustomLowering/performCustomLowering - If any of the actions
123 /// are set to custom, performCustomLowering must be overriden to create a
124 /// transform to lower those actions to LLVM IR. initializeCustomLowering
125 /// is optional to override. These are the only Collector methods through
126 /// which the LLVM IR can be modified.
127 virtual bool initializeCustomLowering(Module &F);
128 virtual bool performCustomLowering(Function &F);