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 41 #include <memory> 42 43 namespace llvm { 44 class AsmPrinter; 45 class GCStrategy; 46 class Constant; 47 class MCSymbol; 48 49 namespace GC { 50 /// PointKind - The type of a collector-safe point. 51 /// 52 enum PointKind { 53 Loop, ///< Instr is a loop (backwards branch). 54 Return, ///< Instr is a return instruction. 55 PreCall, ///< Instr is a call instruction. 56 PostCall ///< Instr is the return address of a call. 57 }; 58 } 59 60 /// GCPoint - Metadata for a collector-safe point in machine code. 61 /// 62 struct GCPoint { 63 GC::PointKind Kind; ///< The kind of the safe point. 64 MCSymbol *Label; ///< A label. 65 DebugLoc Loc; 66 GCPointGCPoint67 GCPoint(GC::PointKind K, MCSymbol *L, DebugLoc DL) 68 : Kind(K), Label(L), Loc(DL) {} 69 }; 70 71 /// GCRoot - Metadata for a pointer to an object managed by the garbage 72 /// collector. 73 struct GCRoot { 74 int Num; ///< Usually a frame index. 75 int StackOffset; ///< Offset from the stack pointer. 76 const Constant *Metadata; ///< Metadata straight from the call 77 ///< to llvm.gcroot. 78 GCRootGCRoot79 GCRoot(int N, const Constant *MD) : Num(N), StackOffset(-1), Metadata(MD) {} 80 }; 81 82 83 /// GCFunctionInfo - Garbage collection metadata for a single function. 84 /// 85 class GCFunctionInfo { 86 public: 87 typedef std::vector<GCPoint>::iterator iterator; 88 typedef std::vector<GCRoot>::iterator roots_iterator; 89 typedef std::vector<GCRoot>::const_iterator live_iterator; 90 91 private: 92 const Function &F; 93 GCStrategy &S; 94 uint64_t FrameSize; 95 std::vector<GCRoot> Roots; 96 std::vector<GCPoint> SafePoints; 97 98 // FIXME: Liveness. A 2D BitVector, perhaps? 99 // 100 // BitVector Liveness; 101 // 102 // bool islive(int point, int root) = 103 // Liveness[point * SafePoints.size() + root] 104 // 105 // The bit vector is the more compact representation where >3.2% of roots 106 // are live per safe point (1.5% on 64-bit hosts). 107 108 public: 109 GCFunctionInfo(const Function &F, GCStrategy &S); 110 ~GCFunctionInfo(); 111 112 /// getFunction - Return the function to which this metadata applies. 113 /// getFunction()114 const Function &getFunction() const { return F; } 115 116 /// getStrategy - Return the GC strategy for the function. 117 /// getStrategy()118 GCStrategy &getStrategy() { return S; } 119 120 /// addStackRoot - Registers a root that lives on the stack. Num is the 121 /// stack object ID for the alloca (if the code generator is 122 // using MachineFrameInfo). addStackRoot(int Num,const Constant * Metadata)123 void addStackRoot(int Num, const Constant *Metadata) { 124 Roots.push_back(GCRoot(Num, Metadata)); 125 } 126 127 /// removeStackRoot - Removes a root. removeStackRoot(roots_iterator position)128 roots_iterator removeStackRoot(roots_iterator position) { 129 return Roots.erase(position); 130 } 131 132 /// addSafePoint - Notes the existence of a safe point. Num is the ID of the 133 /// label just prior to the safe point (if the code generator is using 134 /// MachineModuleInfo). addSafePoint(GC::PointKind Kind,MCSymbol * Label,DebugLoc DL)135 void addSafePoint(GC::PointKind Kind, MCSymbol *Label, DebugLoc DL) { 136 SafePoints.push_back(GCPoint(Kind, Label, DL)); 137 } 138 139 /// getFrameSize/setFrameSize - Records the function's frame size. 140 /// getFrameSize()141 uint64_t getFrameSize() const { return FrameSize; } setFrameSize(uint64_t S)142 void setFrameSize(uint64_t S) { FrameSize = S; } 143 144 /// begin/end - Iterators for safe points. 145 /// begin()146 iterator begin() { return SafePoints.begin(); } end()147 iterator end() { return SafePoints.end(); } size()148 size_t size() const { return SafePoints.size(); } 149 150 /// roots_begin/roots_end - Iterators for all roots in the function. 151 /// roots_begin()152 roots_iterator roots_begin() { return Roots.begin(); } roots_end()153 roots_iterator roots_end () { return Roots.end(); } roots_size()154 size_t roots_size() const { return Roots.size(); } 155 156 /// live_begin/live_end - Iterators for live roots at a given safe point. 157 /// live_begin(const iterator & p)158 live_iterator live_begin(const iterator &p) { return roots_begin(); } live_end(const iterator & p)159 live_iterator live_end (const iterator &p) { return roots_end(); } live_size(const iterator & p)160 size_t live_size(const iterator &p) const { return roots_size(); } 161 }; 162 163 164 /// GCModuleInfo - Garbage collection metadata for a whole module. 165 /// 166 class GCModuleInfo : public ImmutablePass { 167 typedef StringMap<GCStrategy*> strategy_map_type; 168 typedef std::vector<std::unique_ptr<GCStrategy>> list_type; 169 typedef DenseMap<const Function*,GCFunctionInfo*> finfo_map_type; 170 171 strategy_map_type StrategyMap; 172 list_type StrategyList; 173 finfo_map_type FInfoMap; 174 175 GCStrategy *getOrCreateStrategy(const Module *M, const std::string &Name); 176 177 public: 178 typedef list_type::const_iterator iterator; 179 180 static char ID; 181 182 GCModuleInfo(); 183 184 /// clear - Resets the pass. Any pass, which uses GCModuleInfo, should 185 /// call it in doFinalization(). 186 /// 187 void clear(); 188 189 /// begin/end - Iterators for used strategies. 190 /// begin()191 iterator begin() const { return StrategyList.begin(); } end()192 iterator end() const { return StrategyList.end(); } 193 194 /// get - Look up function metadata. 195 /// 196 GCFunctionInfo &getFunctionInfo(const Function &F); 197 }; 198 199 } 200 201 #endif 202