LLVM API Documentation
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/Support/Registry.h" 00041 #include <string> 00042 00043 namespace llvm { 00044 00045 class GCStrategy; 00046 00047 /// The GC strategy registry uses all the defaults from Registry. 00048 /// 00049 typedef Registry<GCStrategy> GCRegistry; 00050 00051 /// GCStrategy describes a garbage collector algorithm's code generation 00052 /// requirements, and provides overridable hooks for those needs which cannot 00053 /// be abstractly described. 00054 class GCStrategy { 00055 public: 00056 typedef std::vector<GCFunctionInfo*> list_type; 00057 typedef list_type::iterator iterator; 00058 00059 private: 00060 friend class GCModuleInfo; 00061 const Module *M; 00062 std::string Name; 00063 00064 list_type Functions; 00065 00066 protected: 00067 unsigned NeededSafePoints; //< Bitmask of required safe points. 00068 bool CustomReadBarriers; //< Default is to insert loads. 00069 bool CustomWriteBarriers; //< Default is to insert stores. 00070 bool CustomRoots; //< Default is to pass through to backend. 00071 bool InitRoots; //< If set, roots are nulled during lowering. 00072 bool UsesMetadata; //< If set, backend must emit metadata tables. 00073 00074 public: 00075 GCStrategy(); 00076 00077 virtual ~GCStrategy(); 00078 00079 00080 /// getName - The name of the GC strategy, for debugging. 00081 /// 00082 const std::string &getName() const { return Name; } 00083 00084 /// getModule - The module within which the GC strategy is operating. 00085 /// 00086 const Module &getModule() const { return *M; } 00087 00088 /// needsSafePoitns - True if safe points of any kind are required. By 00089 // default, none are recorded. 00090 bool needsSafePoints() const { return NeededSafePoints != 0; } 00091 00092 /// needsSafePoint(Kind) - True if the given kind of safe point is 00093 // required. By default, none are recorded. 00094 bool needsSafePoint(GC::PointKind Kind) const { 00095 return (NeededSafePoints & 1 << Kind) != 0; 00096 } 00097 00098 /// customWriteBarrier - By default, write barriers are replaced with simple 00099 /// store instructions. If true, then 00100 /// performCustomLowering must instead lower them. 00101 bool customWriteBarrier() const { return CustomWriteBarriers; } 00102 00103 /// customReadBarrier - By default, read barriers are replaced with simple 00104 /// load instructions. If true, then 00105 /// performCustomLowering must instead lower them. 00106 bool customReadBarrier() const { return CustomReadBarriers; } 00107 00108 /// customRoots - By default, roots are left for the code generator so it 00109 /// can generate a stack map. If true, then 00110 // performCustomLowering must delete them. 00111 bool customRoots() const { return CustomRoots; } 00112 00113 /// initializeRoots - If set, gcroot intrinsics should initialize their 00114 // allocas to null before the first use. This is 00115 // necessary for most GCs and is enabled by default. 00116 bool initializeRoots() const { return InitRoots; } 00117 00118 /// usesMetadata - If set, appropriate metadata tables must be emitted by 00119 /// the back-end (assembler, JIT, or otherwise). 00120 bool usesMetadata() const { return UsesMetadata; } 00121 00122 /// begin/end - Iterators for function metadata. 00123 /// 00124 iterator begin() { return Functions.begin(); } 00125 iterator end() { return Functions.end(); } 00126 00127 /// insertFunctionMetadata - Creates metadata for a function. 00128 /// 00129 GCFunctionInfo *insertFunctionInfo(const Function &F); 00130 00131 /// initializeCustomLowering/performCustomLowering - If any of the actions 00132 /// are set to custom, performCustomLowering must be overriden to transform 00133 /// the corresponding actions to LLVM IR. initializeCustomLowering is 00134 /// optional to override. These are the only GCStrategy methods through 00135 /// which the LLVM IR can be modified. 00136 virtual bool initializeCustomLowering(Module &F); 00137 virtual bool performCustomLowering(Function &F); 00138 }; 00139 00140 } 00141 00142 #endif