1193323Sed//===-- llvm/CodeGen/GCStrategy.h - Garbage collection ----------*- C++ -*-===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// GCStrategy coordinates code generation algorithms and implements some itself
11193323Sed// in order to generate code compatible with a target code generator as
12193323Sed// specified in a function's 'gc' attribute. Algorithms are enabled by setting
13193323Sed// flags in a subclass's constructor, and some virtual methods can be
14193323Sed// overridden.
15193323Sed//
16193323Sed// When requested, the GCStrategy will be populated with data about each
17193323Sed// function which uses it. Specifically:
18193323Sed//
19193323Sed// - Safe points
20193323Sed//   Garbage collection is generally only possible at certain points in code.
21193323Sed//   GCStrategy can request that the collector insert such points:
22193323Sed//
23193323Sed//     - At and after any call to a subroutine
24193323Sed//     - Before returning from the current function
25193323Sed//     - Before backwards branches (loops)
26193323Sed//
27193323Sed// - Roots
28193323Sed//   When a reference to a GC-allocated object exists on the stack, it must be
29193323Sed//   stored in an alloca registered with llvm.gcoot.
30193323Sed//
31193323Sed// This information can used to emit the metadata tables which are required by
32193323Sed// the target garbage collector runtime.
33193323Sed//
34193323Sed//===----------------------------------------------------------------------===//
35193323Sed
36193323Sed#ifndef LLVM_CODEGEN_GCSTRATEGY_H
37193323Sed#define LLVM_CODEGEN_GCSTRATEGY_H
38193323Sed
39193323Sed#include "llvm/CodeGen/GCMetadata.h"
40234353Sdim#include "llvm/CodeGen/MachineFunction.h"
41193323Sed#include "llvm/Support/Registry.h"
42193323Sed#include <string>
43193323Sed
44193323Sednamespace llvm {
45193323Sed
46193323Sed  class GCStrategy;
47193323Sed
48193323Sed  /// The GC strategy registry uses all the defaults from Registry.
49193323Sed  ///
50193323Sed  typedef Registry<GCStrategy> GCRegistry;
51193323Sed
52193323Sed  /// GCStrategy describes a garbage collector algorithm's code generation
53193323Sed  /// requirements, and provides overridable hooks for those needs which cannot
54193323Sed  /// be abstractly described.
55193323Sed  class GCStrategy {
56193323Sed  public:
57193323Sed    typedef std::vector<GCFunctionInfo*> list_type;
58193323Sed    typedef list_type::iterator iterator;
59193323Sed
60193323Sed  private:
61193323Sed    friend class GCModuleInfo;
62193323Sed    const Module *M;
63193323Sed    std::string Name;
64193323Sed
65193323Sed    list_type Functions;
66193323Sed
67193323Sed  protected:
68239462Sdim    unsigned NeededSafePoints; ///< Bitmask of required safe points.
69239462Sdim    bool CustomReadBarriers;   ///< Default is to insert loads.
70239462Sdim    bool CustomWriteBarriers;  ///< Default is to insert stores.
71239462Sdim    bool CustomRoots;          ///< Default is to pass through to backend.
72239462Sdim    bool CustomSafePoints;     ///< Default is to use NeededSafePoints
73239462Sdim                               ///< to find safe points.
74239462Sdim    bool InitRoots;            ///< If set, roots are nulled during lowering.
75239462Sdim    bool UsesMetadata;         ///< If set, backend must emit metadata tables.
76193323Sed
77193323Sed  public:
78193323Sed    GCStrategy();
79193323Sed
80193323Sed    virtual ~GCStrategy();
81193323Sed
82193323Sed
83193323Sed    /// getName - The name of the GC strategy, for debugging.
84193323Sed    ///
85193323Sed    const std::string &getName() const { return Name; }
86193323Sed
87193323Sed    /// getModule - The module within which the GC strategy is operating.
88193323Sed    ///
89193323Sed    const Module &getModule() const { return *M; }
90193323Sed
91193323Sed    /// needsSafePoitns - True if safe points of any kind are required. By
92193323Sed    //                    default, none are recorded.
93234353Sdim    bool needsSafePoints() const {
94234353Sdim      return CustomSafePoints || NeededSafePoints != 0;
95234353Sdim    }
96193323Sed
97193323Sed    /// needsSafePoint(Kind) - True if the given kind of safe point is
98193323Sed    //                          required. By default, none are recorded.
99193323Sed    bool needsSafePoint(GC::PointKind Kind) const {
100193323Sed      return (NeededSafePoints & 1 << Kind) != 0;
101193323Sed    }
102193323Sed
103193323Sed    /// customWriteBarrier - By default, write barriers are replaced with simple
104193323Sed    ///                      store instructions. If true, then
105193323Sed    ///                      performCustomLowering must instead lower them.
106193323Sed    bool customWriteBarrier() const { return CustomWriteBarriers; }
107193323Sed
108193323Sed    /// customReadBarrier - By default, read barriers are replaced with simple
109193323Sed    ///                     load instructions. If true, then
110193323Sed    ///                     performCustomLowering must instead lower them.
111193323Sed    bool customReadBarrier() const { return CustomReadBarriers; }
112193323Sed
113193323Sed    /// customRoots - By default, roots are left for the code generator so it
114193323Sed    ///               can generate a stack map. If true, then
115193323Sed    //                performCustomLowering must delete them.
116193323Sed    bool customRoots() const { return CustomRoots; }
117234353Sdim
118234353Sdim    /// customSafePoints - By default, the GC analysis will find safe
119234353Sdim    ///                    points according to NeededSafePoints. If true,
120234353Sdim    ///                    then findCustomSafePoints must create them.
121234353Sdim    bool customSafePoints() const { return CustomSafePoints; }
122193323Sed
123193323Sed    /// initializeRoots - If set, gcroot intrinsics should initialize their
124193323Sed    //                    allocas to null before the first use. This is
125193323Sed    //                    necessary for most GCs and is enabled by default.
126193323Sed    bool initializeRoots() const { return InitRoots; }
127193323Sed
128193323Sed    /// usesMetadata - If set, appropriate metadata tables must be emitted by
129193323Sed    ///                the back-end (assembler, JIT, or otherwise).
130193323Sed    bool usesMetadata() const { return UsesMetadata; }
131193323Sed
132193323Sed    /// begin/end - Iterators for function metadata.
133193323Sed    ///
134193323Sed    iterator begin() { return Functions.begin(); }
135193323Sed    iterator end()   { return Functions.end();   }
136193323Sed
137193323Sed    /// insertFunctionMetadata - Creates metadata for a function.
138193323Sed    ///
139193323Sed    GCFunctionInfo *insertFunctionInfo(const Function &F);
140193323Sed
141193323Sed    /// initializeCustomLowering/performCustomLowering - If any of the actions
142193323Sed    /// are set to custom, performCustomLowering must be overriden to transform
143193323Sed    /// the corresponding actions to LLVM IR. initializeCustomLowering is
144193323Sed    /// optional to override. These are the only GCStrategy methods through
145193323Sed    /// which the LLVM IR can be modified.
146193323Sed    virtual bool initializeCustomLowering(Module &F);
147193323Sed    virtual bool performCustomLowering(Function &F);
148234353Sdim    virtual bool findCustomSafePoints(GCFunctionInfo& FI, MachineFunction& MF);
149193323Sed  };
150193323Sed
151193323Sed}
152193323Sed
153193323Sed#endif
154