20b470296e1a2c7b8ea3b33cddf8cc0b6c0cf3a8
[oota-llvm.git] / include / llvm / CodeGen / GCMedataPrinter.h
1 //===-- llvm/CodeGen/Collector.h - Garbage collection -----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Collector records sufficient information about a machine function to enable
11 // accurate garbage collectors. Specifically:
12 // 
13 // - Safe points
14 //   Garbage collection is only possible at certain points in code. Code
15 //   generators should record points:
16 //
17 //     - At and after any call to a subroutine
18 //     - Before returning from the current function
19 //     - Before backwards branches (loops)
20 // 
21 // - Roots
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.
24 //
25 // This generic information should used by ABI-specific passes to emit support
26 // tables for the runtime garbage collector.
27 //
28 // MachineCodeAnalysis identifies the GC safe points in the machine code. (Roots
29 // are identified in SelectionDAGISel.)
30 //
31 //===----------------------------------------------------------------------===//
32
33 #ifndef LLVM_CODEGEN_COLLECTOR_H
34 #define LLVM_CODEGEN_COLLECTOR_H
35
36 #include "llvm/CodeGen/CollectorMetadata.h"
37 #include <iosfwd>
38 #include <string>
39
40 namespace llvm {
41   
42   /// Collector describes a garbage collector's code generation requirements,
43   /// and provides overridable hooks for those needs which cannot be abstractly
44   /// described.
45   class Collector {
46   public:
47     typedef std::vector<CollectorMetadata*> list_type;
48     typedef list_type::iterator iterator;
49     
50   private:
51     friend class CollectorModuleMetadata;
52     const Module *M;
53     std::string Name;
54     
55     list_type Functions;
56     
57   protected:
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.
64     
65   public:
66     Collector();
67     
68     virtual ~Collector();
69     
70     
71     /// getName - The name of the collector, for debugging.
72     /// 
73     const std::string &getName() const { return Name; }
74
75     /// getModule - The module upon which the collector is operating.
76     /// 
77     const Module &getModule() const { return *M; }
78
79     /// True if this collector requires safe points of any kind. By default,
80     /// none are recorded.
81     bool needsSafePoints() const { return NeededSafePoints != 0; }
82     
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;
87     }
88     
89     /// By default, write barriers are replaced with simple store instructions.
90     /// If true, then addPassesToCustomLowerIntrinsics must instead process
91     /// them.
92     bool customWriteBarrier() const { return CustomWriteBarriers; }
93     
94     /// By default, read barriers are replaced with simple load instructions.
95     /// If true, then addPassesToCustomLowerIntrinsics must instead process
96     /// them.
97     bool customReadBarrier() const { return CustomReadBarriers; }
98     
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; }
102     
103     /// If set, gcroot intrinsics should initialize their allocas to null. This
104     /// is necessary for most collectors.
105     bool initializeRoots() const { return InitRoots; }
106     
107     /// If set, appropriate metadata tables must be emitted by the back-end
108     /// (assembler, JIT, or otherwise).
109     bool usesMetadata() const { return UsesMetadata; }
110     
111     /// begin/end - Iterators for function metadata.
112     /// 
113     iterator begin() { return Functions.begin(); }
114     iterator end()   { return Functions.end();   }
115
116     /// insertFunctionMetadata - Creates metadata for a function.
117     /// 
118     CollectorMetadata *insertFunctionMetadata(const Function &F);
119
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);
127   };
128   
129   // GCMetadataPrinter - Emits GC metadata as assembly code.
130   class GCMetadataPrinter {
131   public:
132     typedef Collector::list_type list_type;
133     typedef Collector::iterator iterator;
134     
135   private:
136     Collector *Coll;
137     
138     friend class AsmPrinter;
139     
140   protected:
141     // May only be subclassed.
142     GCMetadataPrinter();
143     
144     // Do not implement.
145     GCMetadataPrinter(const GCMetadataPrinter &);
146     GCMetadataPrinter &operator=(const GCMetadataPrinter &);
147     
148   public:
149     Collector &getCollector() { return *Coll; }
150     const Module &getModule() const { return Coll->getModule(); }
151     
152     iterator begin() { return Coll->begin(); }
153     iterator end()   { return Coll->end();   }
154     
155     /// beginAssembly/finishAssembly - Emit module metadata as assembly code.
156     virtual void beginAssembly(std::ostream &OS, AsmPrinter &AP,
157                                const TargetAsmInfo &TAI);
158     
159     virtual void finishAssembly(std::ostream &OS, AsmPrinter &AP,
160                                 const TargetAsmInfo &TAI);
161     
162     virtual ~GCMetadataPrinter();
163   };
164   
165 }
166
167 #endif