61ed2d2095bf34a507f26b8abebed2680785a826
[oota-llvm.git] / include / llvm / CodeGen / GCStrategy.h
1 //===-- llvm/CodeGen/GCStrategy.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 // GCStrategy coordinates code generation algorithms and implements some itself
11 // in order to generate code compatible with a target code generator as
12 // specified in a function's 'gc' attribute. Algorithms are enabled by setting
13 // flags in a subclass's constructor, and some virtual methods can be
14 // overridden.
15 // 
16 // When requested, the GCStrategy will be populated with data about each
17 // function which uses it. Specifically:
18 // 
19 // - Safe points
20 //   Garbage collection is generally only possible at certain points in code.
21 //   GCStrategy can request that the collector insert such points:
22 //
23 //     - At and after any call to a subroutine
24 //     - Before returning from the current function
25 //     - Before backwards branches (loops)
26 // 
27 // - Roots
28 //   When a reference to a GC-allocated object exists on the stack, it must be
29 //   stored in an alloca registered with llvm.gcoot.
30 //
31 // This information can used to emit the metadata tables which are required by
32 // the target garbage collector runtime.
33 //
34 //===----------------------------------------------------------------------===//
35
36 #ifndef LLVM_CODEGEN_GCSTRATEGY_H
37 #define LLVM_CODEGEN_GCSTRATEGY_H
38
39 #include "llvm/CodeGen/GCMetadata.h"
40 #include "llvm/CodeGen/MachineFunction.h"
41 #include "llvm/Support/Registry.h"
42 #include <string>
43
44 namespace llvm {
45   
46   class GCStrategy;
47   
48   /// The GC strategy registry uses all the defaults from Registry.
49   /// 
50   typedef Registry<GCStrategy> GCRegistry;
51   
52   /// GCStrategy describes a garbage collector algorithm's code generation
53   /// requirements, and provides overridable hooks for those needs which cannot
54   /// be abstractly described.  GCStrategy objects currently must be looked up
55   /// through the GCModuleInfo analysis pass.  They are owned by the analysis
56   /// pass and recreated every time that pass is invalidated.
57   class GCStrategy {
58   private:
59     friend class GCModuleInfo;
60     std::string Name;
61     
62   protected:
63     unsigned NeededSafePoints; ///< Bitmask of required safe points.
64     bool CustomReadBarriers;   ///< Default is to insert loads.
65     bool CustomWriteBarriers;  ///< Default is to insert stores.
66     bool CustomRoots;          ///< Default is to pass through to backend.
67     bool CustomSafePoints;     ///< Default is to use NeededSafePoints
68                                ///< to find safe points.
69     bool InitRoots;            ///< If set, roots are nulled during lowering.
70     bool UsesMetadata;         ///< If set, backend must emit metadata tables.
71     
72   public:
73     GCStrategy();
74     
75     virtual ~GCStrategy() {}
76     
77     
78     /// getName - The name of the GC strategy, for debugging.
79     /// 
80     const std::string &getName() const { return Name; }
81
82     /// needsSafePoitns - True if safe points of any kind are required. By
83     //                    default, none are recorded.
84     bool needsSafePoints() const {
85       return CustomSafePoints || NeededSafePoints != 0;
86     }
87     
88     /// needsSafePoint(Kind) - True if the given kind of safe point is
89     //                          required. By default, none are recorded.
90     bool needsSafePoint(GC::PointKind Kind) const {
91       return (NeededSafePoints & 1 << Kind) != 0;
92     }
93     
94     /// customWriteBarrier - By default, write barriers are replaced with simple
95     ///                      store instructions. If true, then
96     ///                      performCustomLowering must instead lower them.
97     bool customWriteBarrier() const { return CustomWriteBarriers; }
98     
99     /// customReadBarrier - By default, read barriers are replaced with simple
100     ///                     load instructions. If true, then
101     ///                     performCustomLowering must instead lower them.
102     bool customReadBarrier() const { return CustomReadBarriers; }
103     
104     /// customRoots - By default, roots are left for the code generator so it
105     ///               can generate a stack map. If true, then
106     //                performCustomLowering must delete them.
107     bool customRoots() const { return CustomRoots; }
108
109     /// customSafePoints - By default, the GC analysis will find safe
110     ///                    points according to NeededSafePoints. If true,
111     ///                    then findCustomSafePoints must create them.
112     bool customSafePoints() const { return CustomSafePoints; }
113     
114     /// initializeRoots - If set, gcroot intrinsics should initialize their
115     //                    allocas to null before the first use. This is
116     //                    necessary for most GCs and is enabled by default.
117     bool initializeRoots() const { return InitRoots; }
118     
119     /// usesMetadata - If set, appropriate metadata tables must be emitted by
120     ///                the back-end (assembler, JIT, or otherwise).
121     bool usesMetadata() const { return UsesMetadata; }
122     
123     /// initializeCustomLowering/performCustomLowering - If any of the actions
124     /// are set to custom, performCustomLowering must be overriden to transform
125     /// the corresponding actions to LLVM IR. initializeCustomLowering is
126     /// optional to override. These are the only GCStrategy methods through
127     /// which the LLVM IR can be modified.
128     virtual bool initializeCustomLowering(Module &F);
129     virtual bool performCustomLowering(Function &F);
130     virtual bool findCustomSafePoints(GCFunctionInfo& FI, MachineFunction& MF);
131   };
132   
133 }
134
135 #endif