1 //===-- GCMetadata.h - Garbage collector metadata ---------------*- 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 // This file declares the GCFunctionInfo and GCModuleInfo classes, which are 11 // used as a communication channel from the target code generator to the target 12 // garbage collectors. This interface allows code generators and garbage 13 // collectors to be developed independently. 14 // 15 // The GCFunctionInfo class logs the data necessary to build a type accurate 16 // stack map. The code generator outputs: 17 // 18 // - Safe points as specified by the GCStrategy's NeededSafePoints. 19 // - Stack offsets for GC roots, as specified by calls to llvm.gcroot 20 // 21 // As a refinement, liveness analysis calculates the set of live roots at each 22 // safe point. Liveness analysis is not presently performed by the code 23 // generator, so all roots are assumed live. 24 // 25 // GCModuleInfo simply collects GCFunctionInfo instances for each Function as 26 // they are compiled. This accretion is necessary for collectors which must emit 27 // a stack map for the compilation unit as a whole. Therefore, GCFunctionInfo 28 // outlives the MachineFunction from which it is derived and must not refer to 29 // any code generator data structures. 30 // 31 //===----------------------------------------------------------------------===// 32 33 #ifndef LLVM_CODEGEN_GCMETADATA_H 34 #define LLVM_CODEGEN_GCMETADATA_H 35 36 #include "llvm/ADT/DenseMap.h" 37 #include "llvm/ADT/StringMap.h" 38 #include "llvm/IR/DebugLoc.h" 39 #include "llvm/Pass.h" 40 #include <memory> 41 42 namespace llvm { 43 class AsmPrinter; 44 class GCStrategy; 45 class Constant; 46 class MCSymbol; 47 48 namespace GC { 49 /// PointKind - The type of a collector-safe point. 50 /// 51 enum PointKind { 52 Loop, ///< Instr is a loop (backwards branch). 53 Return, ///< Instr is a return instruction. 54 PreCall, ///< Instr is a call instruction. 55 PostCall ///< Instr is the return address of a call. 56 }; 57 } 58 59 /// GCPoint - Metadata for a collector-safe point in machine code. 60 /// 61 struct GCPoint { 62 GC::PointKind Kind; ///< The kind of the safe point. 63 MCSymbol *Label; ///< A label. 64 DebugLoc Loc; 65 GCPointGCPoint66 GCPoint(GC::PointKind K, MCSymbol *L, DebugLoc DL) 67 : Kind(K), Label(L), Loc(DL) {} 68 }; 69 70 /// GCRoot - Metadata for a pointer to an object managed by the garbage 71 /// collector. 72 struct GCRoot { 73 int Num; ///< Usually a frame index. 74 int StackOffset; ///< Offset from the stack pointer. 75 const Constant *Metadata; ///< Metadata straight from the call 76 ///< to llvm.gcroot. 77 GCRootGCRoot78 GCRoot(int N, const Constant *MD) : Num(N), StackOffset(-1), Metadata(MD) {} 79 }; 80 81 82 /// Garbage collection metadata for a single function. Currently, this 83 /// information only applies to GCStrategies which use GCRoot. 84 class GCFunctionInfo { 85 public: 86 typedef std::vector<GCPoint>::iterator iterator; 87 typedef std::vector<GCRoot>::iterator roots_iterator; 88 typedef std::vector<GCRoot>::const_iterator live_iterator; 89 90 private: 91 const Function &F; 92 GCStrategy &S; 93 uint64_t FrameSize; 94 std::vector<GCRoot> Roots; 95 std::vector<GCPoint> SafePoints; 96 97 // FIXME: Liveness. A 2D BitVector, perhaps? 98 // 99 // BitVector Liveness; 100 // 101 // bool islive(int point, int root) = 102 // Liveness[point * SafePoints.size() + root] 103 // 104 // The bit vector is the more compact representation where >3.2% of roots 105 // are live per safe point (1.5% on 64-bit hosts). 106 107 public: 108 GCFunctionInfo(const Function &F, GCStrategy &S); 109 ~GCFunctionInfo(); 110 111 /// getFunction - Return the function to which this metadata applies. 112 /// getFunction()113 const Function &getFunction() const { return F; } 114 115 /// getStrategy - Return the GC strategy for the function. 116 /// getStrategy()117 GCStrategy &getStrategy() { return S; } 118 119 /// addStackRoot - Registers a root that lives on the stack. Num is the 120 /// stack object ID for the alloca (if the code generator is 121 // using MachineFrameInfo). addStackRoot(int Num,const Constant * Metadata)122 void addStackRoot(int Num, const Constant *Metadata) { 123 Roots.push_back(GCRoot(Num, Metadata)); 124 } 125 126 /// removeStackRoot - Removes a root. removeStackRoot(roots_iterator position)127 roots_iterator removeStackRoot(roots_iterator position) { 128 return Roots.erase(position); 129 } 130 131 /// addSafePoint - Notes the existence of a safe point. Num is the ID of the 132 /// label just prior to the safe point (if the code generator is using 133 /// MachineModuleInfo). addSafePoint(GC::PointKind Kind,MCSymbol * Label,DebugLoc DL)134 void addSafePoint(GC::PointKind Kind, MCSymbol *Label, DebugLoc DL) { 135 SafePoints.push_back(GCPoint(Kind, Label, DL)); 136 } 137 138 /// getFrameSize/setFrameSize - Records the function's frame size. 139 /// getFrameSize()140 uint64_t getFrameSize() const { return FrameSize; } setFrameSize(uint64_t S)141 void setFrameSize(uint64_t S) { FrameSize = S; } 142 143 /// begin/end - Iterators for safe points. 144 /// begin()145 iterator begin() { return SafePoints.begin(); } end()146 iterator end() { return SafePoints.end(); } size()147 size_t size() const { return SafePoints.size(); } 148 149 /// roots_begin/roots_end - Iterators for all roots in the function. 150 /// roots_begin()151 roots_iterator roots_begin() { return Roots.begin(); } roots_end()152 roots_iterator roots_end () { return Roots.end(); } roots_size()153 size_t roots_size() const { return Roots.size(); } 154 155 /// live_begin/live_end - Iterators for live roots at a given safe point. 156 /// live_begin(const iterator & p)157 live_iterator live_begin(const iterator &p) { return roots_begin(); } live_end(const iterator & p)158 live_iterator live_end (const iterator &p) { return roots_end(); } live_size(const iterator & p)159 size_t live_size(const iterator &p) const { return roots_size(); } 160 }; 161 162 /// An analysis pass which caches information about the entire Module. 163 /// Records both the function level information used by GCRoots and a 164 /// cache of the 'active' gc strategy objects for the current Module. 165 class GCModuleInfo : public ImmutablePass { 166 typedef StringMap<GCStrategy*> strategy_map_type; 167 typedef std::vector<std::unique_ptr<GCStrategy>> list_type; 168 169 strategy_map_type StrategyMap; 170 list_type StrategyList; 171 172 GCStrategy *getOrCreateStrategy(const Module *M, const std::string &Name); 173 174 public: 175 /// List of per function info objects. In theory, Each of these 176 /// may be associated with a different GC. 177 typedef std::vector<std::unique_ptr<GCFunctionInfo>> FuncInfoVec; 178 funcinfo_begin()179 FuncInfoVec::iterator funcinfo_begin() { return Functions.begin(); } funcinfo_end()180 FuncInfoVec::iterator funcinfo_end() { return Functions.end(); } 181 182 183 private: 184 /// Owning list of all GCFunctionInfos associated with this Module 185 FuncInfoVec Functions; 186 187 /// Non-owning map to bypass linear search when finding the GCFunctionInfo 188 /// associated with a particular Function. 189 typedef DenseMap<const Function*,GCFunctionInfo*> finfo_map_type; 190 finfo_map_type FInfoMap; 191 public: 192 193 typedef list_type::const_iterator iterator; 194 195 static char ID; 196 197 GCModuleInfo(); 198 199 /// clear - Resets the pass. Any pass, which uses GCModuleInfo, should 200 /// call it in doFinalization(). 201 /// 202 void clear(); 203 204 /// begin/end - Iterators for used strategies. 205 /// begin()206 iterator begin() const { return StrategyList.begin(); } end()207 iterator end() const { return StrategyList.end(); } 208 209 /// get - Look up function metadata. This is currently assumed 210 /// have the side effect of initializing the associated GCStrategy. That 211 /// will soon change. 212 GCFunctionInfo &getFunctionInfo(const Function &F); 213 }; 214 215 } 216 217 #endif 218