Rename MachineInstr::getInstrDescriptor -> getDesc(), which reflects
[oota-llvm.git] / include / llvm / CodeGen / Collector.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     
64   public:
65     Collector();
66     
67     virtual ~Collector();
68     
69     
70     /// getName - The name of the collector, for debugging.
71     /// 
72     const std::string &getName() const { return Name; }
73
74     /// getModule - The module upon which the collector is operating.
75     /// 
76     const Module &getModule() const { return *M; }
77
78     /// True if this collector requires safe points of any kind. By default,
79     /// none are recorded.
80     bool needsSafePoints() const { return NeededSafePoints != 0; }
81     
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;
86     }
87     
88     /// By default, write barriers are replaced with simple store instructions.
89     /// If true, then addPassesToCustomLowerIntrinsics must instead process
90     /// them.
91     bool customWriteBarrier() const { return CustomWriteBarriers; }
92     
93     /// By default, read barriers are replaced with simple load instructions.
94     /// If true, then addPassesToCustomLowerIntrinsics must instead process
95     /// them.
96     bool customReadBarrier() const { return CustomReadBarriers; }
97     
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; }
101     
102     /// If set, gcroot intrinsics should initialize their allocas to null. This
103     /// is necessary for most collectors.
104     bool initializeRoots() const { return InitRoots; }
105     
106     
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);
112     
113     /// begin/end - Iterators for function metadata.
114     /// 
115     iterator begin() { return Functions.begin(); }
116     iterator end()   { return Functions.end();   }
117
118     /// insertFunctionMetadata - Creates metadata for a function.
119     /// 
120     CollectorMetadata *insertFunctionMetadata(const Function &F);
121
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);
129   };
130   
131 }
132
133 #endif