GCStrategy.h revision 276479
128257Smsmith//===-- llvm/CodeGen/GCStrategy.h - Garbage collection ----------*- C++ -*-===//
255939Snsouch//
328257Smsmith//                     The LLVM Compiler Infrastructure
428257Smsmith//
528257Smsmith// This file is distributed under the University of Illinois Open Source
628257Smsmith// License. See LICENSE.TXT for details.
728257Smsmith//
828257Smsmith//===----------------------------------------------------------------------===//
928257Smsmith//
1028257Smsmith// GCStrategy coordinates code generation algorithms and implements some itself
1128257Smsmith// in order to generate code compatible with a target code generator as
1228257Smsmith// specified in a function's 'gc' attribute. Algorithms are enabled by setting
1328257Smsmith// flags in a subclass's constructor, and some virtual methods can be
1428257Smsmith// overridden.
1528257Smsmith//
1628257Smsmith// When requested, the GCStrategy will be populated with data about each
1728257Smsmith// function which uses it. Specifically:
1828257Smsmith//
1928257Smsmith// - Safe points
2028257Smsmith//   Garbage collection is generally only possible at certain points in code.
2128257Smsmith//   GCStrategy can request that the collector insert such points:
2228257Smsmith//
2328257Smsmith//     - At and after any call to a subroutine
2428257Smsmith//     - Before returning from the current function
2528257Smsmith//     - Before backwards branches (loops)
26119418Sobrien//
27119418Sobrien// - Roots
28119418Sobrien//   When a reference to a GC-allocated object exists on the stack, it must be
29119418Sobrien//   stored in an alloca registered with llvm.gcoot.
3028257Smsmith//
31187576Sjhb// This information can used to emit the metadata tables which are required by
3228257Smsmith// the target garbage collector runtime.
3355939Snsouch//
34187576Sjhb//===----------------------------------------------------------------------===//
35187576Sjhb
3655939Snsouch#ifndef LLVM_CODEGEN_GCSTRATEGY_H
3755939Snsouch#define LLVM_CODEGEN_GCSTRATEGY_H
3828257Smsmith
39185003Sjhb#include "llvm/CodeGen/GCMetadata.h"
4055939Snsouch#include "llvm/CodeGen/MachineFunction.h"
4128257Smsmith#include "llvm/Support/Registry.h"
4255939Snsouch#include <string>
43185003Sjhb
44119284Simpnamespace llvm {
45119284Simp
4655939Snsouch  class GCStrategy;
47185003Sjhb
4828257Smsmith  /// The GC strategy registry uses all the defaults from Registry.
4955939Snsouch  ///
5028257Smsmith  typedef Registry<GCStrategy> GCRegistry;
5155939Snsouch
5228257Smsmith  /// GCStrategy describes a garbage collector algorithm's code generation
5328257Smsmith  /// requirements, and provides overridable hooks for those needs which cannot
5428257Smsmith  /// be abstractly described.
5528257Smsmith  class GCStrategy {
5655939Snsouch  public:
5755939Snsouch    typedef std::vector<std::unique_ptr<GCFunctionInfo>> list_type;
5828257Smsmith    typedef list_type::iterator iterator;
59187576Sjhb
6042475Snsouch  private:
6142475Snsouch    friend class GCModuleInfo;
6228257Smsmith    const Module *M;
63230800Sattilio    std::string Name;
64187576Sjhb
6542475Snsouch    list_type Functions;
6642475Snsouch
6742475Snsouch  protected:
6855939Snsouch    unsigned NeededSafePoints; ///< Bitmask of required safe points.
6942475Snsouch    bool CustomReadBarriers;   ///< Default is to insert loads.
7042475Snsouch    bool CustomWriteBarriers;  ///< Default is to insert stores.
7142475Snsouch    bool CustomRoots;          ///< Default is to pass through to backend.
7242475Snsouch    bool CustomSafePoints;     ///< Default is to use NeededSafePoints
7342475Snsouch                               ///< to find safe points.
7442475Snsouch    bool InitRoots;            ///< If set, roots are nulled during lowering.
7542475Snsouch    bool UsesMetadata;         ///< If set, backend must emit metadata tables.
7642475Snsouch
7755939Snsouch  public:
7828257Smsmith    GCStrategy();
7928257Smsmith
80187576Sjhb    virtual ~GCStrategy() {}
81187576Sjhb
82187576Sjhb
83187576Sjhb    /// getName - The name of the GC strategy, for debugging.
84187576Sjhb    ///
8542475Snsouch    const std::string &getName() const { return Name; }
8628257Smsmith
8728257Smsmith    /// getModule - The module within which the GC strategy is operating.
8828257Smsmith    ///
8928257Smsmith    const Module &getModule() const { return *M; }
9028257Smsmith
9128257Smsmith    /// needsSafePoitns - True if safe points of any kind are required. By
9255939Snsouch    //                    default, none are recorded.
9338061Smsmith    bool needsSafePoints() const {
9455939Snsouch      return CustomSafePoints || NeededSafePoints != 0;
9538061Smsmith    }
9638061Smsmith
9755939Snsouch    /// needsSafePoint(Kind) - True if the given kind of safe point is
9838061Smsmith    //                          required. By default, none are recorded.
9955939Snsouch    bool needsSafePoint(GC::PointKind Kind) const {
10038061Smsmith      return (NeededSafePoints & 1 << Kind) != 0;
101230800Sattilio    }
10255939Snsouch
10338061Smsmith    /// customWriteBarrier - By default, write barriers are replaced with simple
10455939Snsouch    ///                      store instructions. If true, then
10555939Snsouch    ///                      performCustomLowering must instead lower them.
10655939Snsouch    bool customWriteBarrier() const { return CustomWriteBarriers; }
10755939Snsouch
10855939Snsouch    /// customReadBarrier - By default, read barriers are replaced with simple
10955939Snsouch    ///                     load instructions. If true, then
11055939Snsouch    ///                     performCustomLowering must instead lower them.
11155939Snsouch    bool customReadBarrier() const { return CustomReadBarriers; }
11255939Snsouch
11355939Snsouch    /// customRoots - By default, roots are left for the code generator so it
11455939Snsouch    ///               can generate a stack map. If true, then
11555939Snsouch    //                performCustomLowering must delete them.
11638061Smsmith    bool customRoots() const { return CustomRoots; }
117230800Sattilio
11855939Snsouch    /// customSafePoints - By default, the GC analysis will find safe
11955939Snsouch    ///                    points according to NeededSafePoints. If true,
12038061Smsmith    ///                    then findCustomSafePoints must create them.
12155939Snsouch    bool customSafePoints() const { return CustomSafePoints; }
12255939Snsouch
12355939Snsouch    /// initializeRoots - If set, gcroot intrinsics should initialize their
12455939Snsouch    //                    allocas to null before the first use. This is
12555939Snsouch    //                    necessary for most GCs and is enabled by default.
12655939Snsouch    bool initializeRoots() const { return InitRoots; }
12755939Snsouch
12855939Snsouch    /// usesMetadata - If set, appropriate metadata tables must be emitted by
12955939Snsouch    ///                the back-end (assembler, JIT, or otherwise).
13055939Snsouch    bool usesMetadata() const { return UsesMetadata; }
13155939Snsouch
132230800Sattilio    /// begin/end - Iterators for function metadata.
13363458Sn_hibma    ///
134185003Sjhb    iterator begin() { return Functions.begin(); }
13555939Snsouch    iterator end()   { return Functions.end();   }
13663458Sn_hibma
13763458Sn_hibma    /// insertFunctionMetadata - Creates metadata for a function.
13863458Sn_hibma    ///
13938061Smsmith    GCFunctionInfo *insertFunctionInfo(const Function &F);
14038061Smsmith
14138061Smsmith    /// initializeCustomLowering/performCustomLowering - If any of the actions
14238061Smsmith    /// are set to custom, performCustomLowering must be overriden to transform
14342475Snsouch    /// the corresponding actions to LLVM IR. initializeCustomLowering is
14442475Snsouch    /// optional to override. These are the only GCStrategy methods through
14542475Snsouch    /// which the LLVM IR can be modified.
14642475Snsouch    virtual bool initializeCustomLowering(Module &F);
14742475Snsouch    virtual bool performCustomLowering(Function &F);
14855939Snsouch    virtual bool findCustomSafePoints(GCFunctionInfo& FI, MachineFunction& MF);
14942475Snsouch  };
150187576Sjhb
151230800Sattilio}
15255939Snsouch
15342475Snsouch#endif
15442475Snsouch