664e13c62b89e3ef8c3dc02fa03ceae4a93adc1d
[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 was developed by Gordon Henriksen and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // GCInfo 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 // GCSafePointPass identifies the GC safe points in the machine code. (Roots are
29 // 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
39 namespace llvm {
40   
41   class AsmPrinter;
42   class FunctionPassManager;
43   class PassManager;
44   class TargetAsmInfo;
45   
46   
47   /// Collector describes a garbage collector's code generation requirements,
48   /// and provides overridable hooks for those needs which cannot be abstractly
49   /// described.
50   class Collector {
51   protected:
52     unsigned NeededSafePoints; //< Bitmask of required safe points.
53     bool CustomReadBarriers;   //< Default is to insert loads.
54     bool CustomWriteBarriers;  //< Default is to insert stores.
55     bool CustomRoots;          //< Default is to pass through to backend.
56     bool InitRoots;            //< If set, roots are nulled during lowering.
57     
58     /// If any of the actions are set to Custom, this is expected to be
59     /// overriden to create a transform to lower those actions to LLVM IR.
60     virtual Pass *createCustomLoweringPass() const;
61     
62   public:
63     Collector();
64     
65     virtual ~Collector();
66     
67     
68     /// True if this collector requires safe points of any kind. By default,
69     /// none are recorded.
70     bool needsSafePoints() const { return NeededSafePoints != 0; }
71     
72     /// True if the collector requires the given kind of safe point. By default,
73     /// none are recorded.
74     bool needsSafePoint(GC::PointKind Kind) const {
75       return (NeededSafePoints & 1 << Kind) != 0;
76     }
77     
78     /// By default, write barriers are replaced with simple store instructions.
79     /// If true, then addPassesToCustomLowerIntrinsics must instead process
80     /// them.
81     bool customWriteBarrier() const { return CustomWriteBarriers; }
82     
83     /// By default, read barriers are replaced with simple load instructions.
84     /// If true, then addPassesToCustomLowerIntrinsics must instead process
85     /// them.
86     bool customReadBarrier() const { return CustomReadBarriers; }
87     
88     /// By default, roots are left for the code generator. If Custom, then 
89     /// addPassesToCustomLowerIntrinsics must add passes to delete them.
90     bool customRoots() const { return CustomRoots; }
91     
92     /// If set, gcroot intrinsics should initialize their allocas to null. This
93     /// is necessary for most collectors.
94     bool initializeRoots() const { return InitRoots; }
95     
96     
97     /// Adds LLVM IR transforms to handle collection intrinsics. By default,
98     /// read- and write barriers are replaced with direct memory accesses, and
99     /// roots are passed on to the code generator.
100     void addLoweringPasses(FunctionPassManager &PM) const;
101     
102     /// Same as addLoweringPasses(FunctionPassManager &), except uses a
103     /// PassManager for compatibility with unusual backends (such as MSIL or
104     /// CBackend).
105     void addLoweringPasses(PassManager &PM) const;
106     
107     /// Adds target-independent MachineFunction pass to mark safe points. This 
108     /// is added very late during code generation, just prior to output, and
109     /// importantly after all CFG transformations (like branch folding).
110     void addGenericMachineCodePass(FunctionPassManager &PM,
111                                    const TargetMachine &TM, bool Fast) const;
112     
113     /// beginAssembly/finishAssembly - Emit module metadata as assembly code.
114     virtual void beginAssembly(Module &M, std::ostream &OS, AsmPrinter &AP,
115                                const TargetAsmInfo &TAI) const;
116     virtual void finishAssembly(Module &M, CollectorModuleMetadata &CMM,
117                                 std::ostream &OS, AsmPrinter &AP,
118                                 const TargetAsmInfo &TAI) const;
119     
120   private:
121     bool NeedsDefaultLoweringPass() const;
122     bool NeedsCustomLoweringPass() const;
123     
124   };
125   
126   
127   /// If set, the code generator should generate garbage collection as specified
128   /// by the collector properties.
129   extern const Collector *TheCollector;  // FIXME: Find a better home!
130   
131 }
132
133 #endif