Home | History | Annotate | Download | only in CodeGen
      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.
     55   class GCStrategy {
     56   public:
     57     typedef std::vector<GCFunctionInfo*> list_type;
     58     typedef list_type::iterator iterator;
     59 
     60   private:
     61     friend class GCModuleInfo;
     62     const Module *M;
     63     std::string Name;
     64 
     65     list_type Functions;
     66 
     67   protected:
     68     unsigned NeededSafePoints; ///< Bitmask of required safe points.
     69     bool CustomReadBarriers;   ///< Default is to insert loads.
     70     bool CustomWriteBarriers;  ///< Default is to insert stores.
     71     bool CustomRoots;          ///< Default is to pass through to backend.
     72     bool CustomSafePoints;     ///< Default is to use NeededSafePoints
     73                                ///< to find safe points.
     74     bool InitRoots;            ///< If set, roots are nulled during lowering.
     75     bool UsesMetadata;         ///< If set, backend must emit metadata tables.
     76 
     77   public:
     78     GCStrategy();
     79 
     80     virtual ~GCStrategy();
     81 
     82 
     83     /// getName - The name of the GC strategy, for debugging.
     84     ///
     85     const std::string &getName() const { return Name; }
     86 
     87     /// getModule - The module within which the GC strategy is operating.
     88     ///
     89     const Module &getModule() const { return *M; }
     90 
     91     /// needsSafePoitns - True if safe points of any kind are required. By
     92     //                    default, none are recorded.
     93     bool needsSafePoints() const {
     94       return CustomSafePoints || NeededSafePoints != 0;
     95     }
     96 
     97     /// needsSafePoint(Kind) - True if the given kind of safe point is
     98     //                          required. By default, none are recorded.
     99     bool needsSafePoint(GC::PointKind Kind) const {
    100       return (NeededSafePoints & 1 << Kind) != 0;
    101     }
    102 
    103     /// customWriteBarrier - By default, write barriers are replaced with simple
    104     ///                      store instructions. If true, then
    105     ///                      performCustomLowering must instead lower them.
    106     bool customWriteBarrier() const { return CustomWriteBarriers; }
    107 
    108     /// customReadBarrier - By default, read barriers are replaced with simple
    109     ///                     load instructions. If true, then
    110     ///                     performCustomLowering must instead lower them.
    111     bool customReadBarrier() const { return CustomReadBarriers; }
    112 
    113     /// customRoots - By default, roots are left for the code generator so it
    114     ///               can generate a stack map. If true, then
    115     //                performCustomLowering must delete them.
    116     bool customRoots() const { return CustomRoots; }
    117 
    118     /// customSafePoints - By default, the GC analysis will find safe
    119     ///                    points according to NeededSafePoints. If true,
    120     ///                    then findCustomSafePoints must create them.
    121     bool customSafePoints() const { return CustomSafePoints; }
    122 
    123     /// initializeRoots - If set, gcroot intrinsics should initialize their
    124     //                    allocas to null before the first use. This is
    125     //                    necessary for most GCs and is enabled by default.
    126     bool initializeRoots() const { return InitRoots; }
    127 
    128     /// usesMetadata - If set, appropriate metadata tables must be emitted by
    129     ///                the back-end (assembler, JIT, or otherwise).
    130     bool usesMetadata() const { return UsesMetadata; }
    131 
    132     /// begin/end - Iterators for function metadata.
    133     ///
    134     iterator begin() { return Functions.begin(); }
    135     iterator end()   { return Functions.end();   }
    136 
    137     /// insertFunctionMetadata - Creates metadata for a function.
    138     ///
    139     GCFunctionInfo *insertFunctionInfo(const Function &F);
    140 
    141     /// initializeCustomLowering/performCustomLowering - If any of the actions
    142     /// are set to custom, performCustomLowering must be overriden to transform
    143     /// the corresponding actions to LLVM IR. initializeCustomLowering is
    144     /// optional to override. These are the only GCStrategy methods through
    145     /// which the LLVM IR can be modified.
    146     virtual bool initializeCustomLowering(Module &F);
    147     virtual bool performCustomLowering(Function &F);
    148     virtual bool findCustomSafePoints(GCFunctionInfo& FI, MachineFunction& MF);
    149   };
    150 
    151 }
    152 
    153 #endif
    154