GCStrategy.h revision 193323
1284345Ssjg//===-- llvm/CodeGen/GCStrategy.h - Garbage collection ----------*- C++ -*-===//
2284345Ssjg//
3284345Ssjg//                     The LLVM Compiler Infrastructure
4284345Ssjg//
5284345Ssjg// This file is distributed under the University of Illinois Open Source
6284345Ssjg// License. See LICENSE.TXT for details.
7284345Ssjg//
8284345Ssjg//===----------------------------------------------------------------------===//
9284345Ssjg//
10284345Ssjg// GCStrategy coordinates code generation algorithms and implements some itself
11284345Ssjg// 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/Support/Registry.h"
41#include <string>
42
43namespace llvm {
44
45  class GCStrategy;
46
47  /// The GC strategy registry uses all the defaults from Registry.
48  ///
49  typedef Registry<GCStrategy> GCRegistry;
50
51  /// GCStrategy describes a garbage collector algorithm's code generation
52  /// requirements, and provides overridable hooks for those needs which cannot
53  /// be abstractly described.
54  class GCStrategy {
55  public:
56    typedef std::vector<GCFunctionInfo*> list_type;
57    typedef list_type::iterator iterator;
58
59  private:
60    friend class GCModuleInfo;
61    const Module *M;
62    std::string Name;
63
64    list_type Functions;
65
66  protected:
67    unsigned NeededSafePoints; //< Bitmask of required safe points.
68    bool CustomReadBarriers;   //< Default is to insert loads.
69    bool CustomWriteBarriers;  //< Default is to insert stores.
70    bool CustomRoots;          //< Default is to pass through to backend.
71    bool InitRoots;            //< If set, roots are nulled during lowering.
72    bool UsesMetadata;         //< If set, backend must emit metadata tables.
73
74  public:
75    GCStrategy();
76
77    virtual ~GCStrategy();
78
79
80    /// getName - The name of the GC strategy, for debugging.
81    ///
82    const std::string &getName() const { return Name; }
83
84    /// getModule - The module within which the GC strategy is operating.
85    ///
86    const Module &getModule() const { return *M; }
87
88    /// needsSafePoitns - True if safe points of any kind are required. By
89    //                    default, none are recorded.
90    bool needsSafePoints() const { return NeededSafePoints != 0; }
91
92    /// needsSafePoint(Kind) - True if the given kind of safe point is
93    //                          required. By default, none are recorded.
94    bool needsSafePoint(GC::PointKind Kind) const {
95      return (NeededSafePoints & 1 << Kind) != 0;
96    }
97
98    /// customWriteBarrier - By default, write barriers are replaced with simple
99    ///                      store instructions. If true, then
100    ///                      performCustomLowering must instead lower them.
101    bool customWriteBarrier() const { return CustomWriteBarriers; }
102
103    /// customReadBarrier - By default, read barriers are replaced with simple
104    ///                     load instructions. If true, then
105    ///                     performCustomLowering must instead lower them.
106    bool customReadBarrier() const { return CustomReadBarriers; }
107
108    /// customRoots - By default, roots are left for the code generator so it
109    ///               can generate a stack map. If true, then
110    //                performCustomLowering must delete them.
111    bool customRoots() const { return CustomRoots; }
112
113    /// initializeRoots - If set, gcroot intrinsics should initialize their
114    //                    allocas to null before the first use. This is
115    //                    necessary for most GCs and is enabled by default.
116    bool initializeRoots() const { return InitRoots; }
117
118    /// usesMetadata - If set, appropriate metadata tables must be emitted by
119    ///                the back-end (assembler, JIT, or otherwise).
120    bool usesMetadata() const { return UsesMetadata; }
121
122    /// begin/end - Iterators for function metadata.
123    ///
124    iterator begin() { return Functions.begin(); }
125    iterator end()   { return Functions.end();   }
126
127    /// insertFunctionMetadata - Creates metadata for a function.
128    ///
129    GCFunctionInfo *insertFunctionInfo(const Function &F);
130
131    /// initializeCustomLowering/performCustomLowering - If any of the actions
132    /// are set to custom, performCustomLowering must be overriden to transform
133    /// the corresponding actions to LLVM IR. initializeCustomLowering is
134    /// optional to override. These are the only GCStrategy methods through
135    /// which the LLVM IR can be modified.
136    virtual bool initializeCustomLowering(Module &F);
137    virtual bool performCustomLowering(Function &F);
138  };
139
140}
141
142#endif
143