LLVM API Documentation

GCStrategy.h
Go to the documentation of this file.
00001 //===-- llvm/CodeGen/GCStrategy.h - Garbage collection ----------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // GCStrategy coordinates code generation algorithms and implements some itself
00011 // in order to generate code compatible with a target code generator as
00012 // specified in a function's 'gc' attribute. Algorithms are enabled by setting
00013 // flags in a subclass's constructor, and some virtual methods can be
00014 // overridden.
00015 // 
00016 // When requested, the GCStrategy will be populated with data about each
00017 // function which uses it. Specifically:
00018 // 
00019 // - Safe points
00020 //   Garbage collection is generally only possible at certain points in code.
00021 //   GCStrategy can request that the collector insert such points:
00022 //
00023 //     - At and after any call to a subroutine
00024 //     - Before returning from the current function
00025 //     - Before backwards branches (loops)
00026 // 
00027 // - Roots
00028 //   When a reference to a GC-allocated object exists on the stack, it must be
00029 //   stored in an alloca registered with llvm.gcoot.
00030 //
00031 // This information can used to emit the metadata tables which are required by
00032 // the target garbage collector runtime.
00033 //
00034 //===----------------------------------------------------------------------===//
00035 
00036 #ifndef LLVM_CODEGEN_GCSTRATEGY_H
00037 #define LLVM_CODEGEN_GCSTRATEGY_H
00038 
00039 #include "llvm/CodeGen/GCMetadata.h"
00040 #include "llvm/CodeGen/MachineFunction.h"
00041 #include "llvm/Support/Registry.h"
00042 #include <string>
00043 
00044 namespace llvm {
00045   
00046   class GCStrategy;
00047   
00048   /// The GC strategy registry uses all the defaults from Registry.
00049   /// 
00050   typedef Registry<GCStrategy> GCRegistry;
00051   
00052   /// GCStrategy describes a garbage collector algorithm's code generation
00053   /// requirements, and provides overridable hooks for those needs which cannot
00054   /// be abstractly described.
00055   class GCStrategy {
00056   public:
00057     typedef std::vector<std::unique_ptr<GCFunctionInfo>> list_type;
00058     typedef list_type::iterator iterator;
00059     
00060   private:
00061     friend class GCModuleInfo;
00062     const Module *M;
00063     std::string Name;
00064     
00065     list_type Functions;
00066     
00067   protected:
00068     unsigned NeededSafePoints; ///< Bitmask of required safe points.
00069     bool CustomReadBarriers;   ///< Default is to insert loads.
00070     bool CustomWriteBarriers;  ///< Default is to insert stores.
00071     bool CustomRoots;          ///< Default is to pass through to backend.
00072     bool CustomSafePoints;     ///< Default is to use NeededSafePoints
00073                                ///< to find safe points.
00074     bool InitRoots;            ///< If set, roots are nulled during lowering.
00075     bool UsesMetadata;         ///< If set, backend must emit metadata tables.
00076     
00077   public:
00078     GCStrategy();
00079     
00080     virtual ~GCStrategy() {}
00081     
00082     
00083     /// getName - The name of the GC strategy, for debugging.
00084     /// 
00085     const std::string &getName() const { return Name; }
00086 
00087     /// getModule - The module within which the GC strategy is operating.
00088     /// 
00089     const Module &getModule() const { return *M; }
00090 
00091     /// needsSafePoitns - True if safe points of any kind are required. By
00092     //                    default, none are recorded.
00093     bool needsSafePoints() const {
00094       return CustomSafePoints || NeededSafePoints != 0;
00095     }
00096     
00097     /// needsSafePoint(Kind) - True if the given kind of safe point is
00098     //                          required. By default, none are recorded.
00099     bool needsSafePoint(GC::PointKind Kind) const {
00100       return (NeededSafePoints & 1 << Kind) != 0;
00101     }
00102     
00103     /// customWriteBarrier - By default, write barriers are replaced with simple
00104     ///                      store instructions. If true, then
00105     ///                      performCustomLowering must instead lower them.
00106     bool customWriteBarrier() const { return CustomWriteBarriers; }
00107     
00108     /// customReadBarrier - By default, read barriers are replaced with simple
00109     ///                     load instructions. If true, then
00110     ///                     performCustomLowering must instead lower them.
00111     bool customReadBarrier() const { return CustomReadBarriers; }
00112     
00113     /// customRoots - By default, roots are left for the code generator so it
00114     ///               can generate a stack map. If true, then
00115     //                performCustomLowering must delete them.
00116     bool customRoots() const { return CustomRoots; }
00117 
00118     /// customSafePoints - By default, the GC analysis will find safe
00119     ///                    points according to NeededSafePoints. If true,
00120     ///                    then findCustomSafePoints must create them.
00121     bool customSafePoints() const { return CustomSafePoints; }
00122     
00123     /// initializeRoots - If set, gcroot intrinsics should initialize their
00124     //                    allocas to null before the first use. This is
00125     //                    necessary for most GCs and is enabled by default.
00126     bool initializeRoots() const { return InitRoots; }
00127     
00128     /// usesMetadata - If set, appropriate metadata tables must be emitted by
00129     ///                the back-end (assembler, JIT, or otherwise).
00130     bool usesMetadata() const { return UsesMetadata; }
00131     
00132     /// begin/end - Iterators for function metadata.
00133     /// 
00134     iterator begin() { return Functions.begin(); }
00135     iterator end()   { return Functions.end();   }
00136 
00137     /// insertFunctionMetadata - Creates metadata for a function.
00138     /// 
00139     GCFunctionInfo *insertFunctionInfo(const Function &F);
00140 
00141     /// initializeCustomLowering/performCustomLowering - If any of the actions
00142     /// are set to custom, performCustomLowering must be overriden to transform
00143     /// the corresponding actions to LLVM IR. initializeCustomLowering is
00144     /// optional to override. These are the only GCStrategy methods through
00145     /// which the LLVM IR can be modified.
00146     virtual bool initializeCustomLowering(Module &F);
00147     virtual bool performCustomLowering(Function &F);
00148     virtual bool findCustomSafePoints(GCFunctionInfo& FI, MachineFunction& MF);
00149   };
00150   
00151 }
00152 
00153 #endif