1210299Sed//===-- GCMetadata.h - Garbage collector metadata ---------------*- C++ -*-===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file declares the GCFunctionInfo and GCModuleInfo classes, which are 11193323Sed// used as a communication channel from the target code generator to the target 12193323Sed// garbage collectors. This interface allows code generators and garbage 13193323Sed// collectors to be developed independently. 14193323Sed// 15193323Sed// The GCFunctionInfo class logs the data necessary to build a type accurate 16193323Sed// stack map. The code generator outputs: 17210299Sed// 18193323Sed// - Safe points as specified by the GCStrategy's NeededSafePoints. 19193323Sed// - Stack offsets for GC roots, as specified by calls to llvm.gcroot 20193323Sed// 21193323Sed// As a refinement, liveness analysis calculates the set of live roots at each 22193323Sed// safe point. Liveness analysis is not presently performed by the code 23193323Sed// generator, so all roots are assumed live. 24193323Sed// 25193323Sed// GCModuleInfo simply collects GCFunctionInfo instances for each Function as 26193323Sed// they are compiled. This accretion is necessary for collectors which must emit 27193323Sed// a stack map for the compilation unit as a whole. Therefore, GCFunctionInfo 28193323Sed// outlives the MachineFunction from which it is derived and must not refer to 29193323Sed// any code generator data structures. 30193323Sed// 31193323Sed//===----------------------------------------------------------------------===// 32193323Sed 33193323Sed#ifndef LLVM_CODEGEN_GCMETADATA_H 34193323Sed#define LLVM_CODEGEN_GCMETADATA_H 35193323Sed 36193323Sed#include "llvm/ADT/DenseMap.h" 37193323Sed#include "llvm/ADT/StringMap.h" 38252723Sdim#include "llvm/Pass.h" 39218893Sdim#include "llvm/Support/DebugLoc.h" 40193323Sed 41193323Sednamespace llvm { 42193323Sed class AsmPrinter; 43193323Sed class GCStrategy; 44193323Sed class Constant; 45205218Srdivacky class MCSymbol; 46210299Sed 47193323Sed namespace GC { 48193323Sed /// PointKind - The type of a collector-safe point. 49210299Sed /// 50193323Sed enum PointKind { 51245431Sdim Loop, ///< Instr is a loop (backwards branch). 52245431Sdim Return, ///< Instr is a return instruction. 53245431Sdim PreCall, ///< Instr is a call instruction. 54245431Sdim PostCall ///< Instr is the return address of a call. 55193323Sed }; 56193323Sed } 57210299Sed 58193323Sed /// GCPoint - Metadata for a collector-safe point in machine code. 59210299Sed /// 60193323Sed struct GCPoint { 61245431Sdim GC::PointKind Kind; ///< The kind of the safe point. 62245431Sdim MCSymbol *Label; ///< A label. 63218893Sdim DebugLoc Loc; 64210299Sed 65218893Sdim GCPoint(GC::PointKind K, MCSymbol *L, DebugLoc DL) 66218893Sdim : Kind(K), Label(L), Loc(DL) {} 67193323Sed }; 68210299Sed 69193323Sed /// GCRoot - Metadata for a pointer to an object managed by the garbage 70193323Sed /// collector. 71193323Sed struct GCRoot { 72245431Sdim int Num; ///< Usually a frame index. 73245431Sdim int StackOffset; ///< Offset from the stack pointer. 74245431Sdim const Constant *Metadata; ///< Metadata straight from the call 75245431Sdim ///< to llvm.gcroot. 76210299Sed 77207618Srdivacky GCRoot(int N, const Constant *MD) : Num(N), StackOffset(-1), Metadata(MD) {} 78193323Sed }; 79210299Sed 80210299Sed 81193323Sed /// GCFunctionInfo - Garbage collection metadata for a single function. 82210299Sed /// 83193323Sed class GCFunctionInfo { 84193323Sed public: 85193323Sed typedef std::vector<GCPoint>::iterator iterator; 86193323Sed typedef std::vector<GCRoot>::iterator roots_iterator; 87193323Sed typedef std::vector<GCRoot>::const_iterator live_iterator; 88210299Sed 89193323Sed private: 90193323Sed const Function &F; 91193323Sed GCStrategy &S; 92193323Sed uint64_t FrameSize; 93193323Sed std::vector<GCRoot> Roots; 94193323Sed std::vector<GCPoint> SafePoints; 95210299Sed 96193323Sed // FIXME: Liveness. A 2D BitVector, perhaps? 97210299Sed // 98193323Sed // BitVector Liveness; 99210299Sed // 100193323Sed // bool islive(int point, int root) = 101193323Sed // Liveness[point * SafePoints.size() + root] 102210299Sed // 103193323Sed // The bit vector is the more compact representation where >3.2% of roots 104193323Sed // are live per safe point (1.5% on 64-bit hosts). 105210299Sed 106193323Sed public: 107193323Sed GCFunctionInfo(const Function &F, GCStrategy &S); 108193323Sed ~GCFunctionInfo(); 109210299Sed 110193323Sed /// getFunction - Return the function to which this metadata applies. 111210299Sed /// 112193323Sed const Function &getFunction() const { return F; } 113210299Sed 114193323Sed /// getStrategy - Return the GC strategy for the function. 115210299Sed /// 116193323Sed GCStrategy &getStrategy() { return S; } 117210299Sed 118193323Sed /// addStackRoot - Registers a root that lives on the stack. Num is the 119193323Sed /// stack object ID for the alloca (if the code generator is 120193323Sed // using MachineFrameInfo). 121207618Srdivacky void addStackRoot(int Num, const Constant *Metadata) { 122193323Sed Roots.push_back(GCRoot(Num, Metadata)); 123193323Sed } 124210299Sed 125245431Sdim /// removeStackRoot - Removes a root. 126245431Sdim roots_iterator removeStackRoot(roots_iterator position) { 127245431Sdim return Roots.erase(position); 128245431Sdim } 129245431Sdim 130193323Sed /// addSafePoint - Notes the existence of a safe point. Num is the ID of the 131210299Sed /// label just prior to the safe point (if the code generator is using 132193323Sed /// MachineModuleInfo). 133218893Sdim void addSafePoint(GC::PointKind Kind, MCSymbol *Label, DebugLoc DL) { 134218893Sdim SafePoints.push_back(GCPoint(Kind, Label, DL)); 135193323Sed } 136210299Sed 137193323Sed /// getFrameSize/setFrameSize - Records the function's frame size. 138210299Sed /// 139193323Sed uint64_t getFrameSize() const { return FrameSize; } 140193323Sed void setFrameSize(uint64_t S) { FrameSize = S; } 141210299Sed 142193323Sed /// begin/end - Iterators for safe points. 143210299Sed /// 144193323Sed iterator begin() { return SafePoints.begin(); } 145193323Sed iterator end() { return SafePoints.end(); } 146193323Sed size_t size() const { return SafePoints.size(); } 147210299Sed 148193323Sed /// roots_begin/roots_end - Iterators for all roots in the function. 149210299Sed /// 150193323Sed roots_iterator roots_begin() { return Roots.begin(); } 151193323Sed roots_iterator roots_end () { return Roots.end(); } 152193323Sed size_t roots_size() const { return Roots.size(); } 153210299Sed 154193323Sed /// live_begin/live_end - Iterators for live roots at a given safe point. 155210299Sed /// 156193323Sed live_iterator live_begin(const iterator &p) { return roots_begin(); } 157193323Sed live_iterator live_end (const iterator &p) { return roots_end(); } 158193323Sed size_t live_size(const iterator &p) const { return roots_size(); } 159193323Sed }; 160210299Sed 161210299Sed 162193323Sed /// GCModuleInfo - Garbage collection metadata for a whole module. 163210299Sed /// 164193323Sed class GCModuleInfo : public ImmutablePass { 165193323Sed typedef StringMap<GCStrategy*> strategy_map_type; 166193323Sed typedef std::vector<GCStrategy*> list_type; 167193323Sed typedef DenseMap<const Function*,GCFunctionInfo*> finfo_map_type; 168210299Sed 169193323Sed strategy_map_type StrategyMap; 170193323Sed list_type StrategyList; 171193323Sed finfo_map_type FInfoMap; 172210299Sed 173193323Sed GCStrategy *getOrCreateStrategy(const Module *M, const std::string &Name); 174210299Sed 175193323Sed public: 176193323Sed typedef list_type::const_iterator iterator; 177210299Sed 178193323Sed static char ID; 179210299Sed 180193323Sed GCModuleInfo(); 181193323Sed ~GCModuleInfo(); 182210299Sed 183252723Sdim /// clear - Resets the pass. Any pass, which uses GCModuleInfo, should 184252723Sdim /// call it in doFinalization(). 185210299Sed /// 186193323Sed void clear(); 187210299Sed 188193323Sed /// begin/end - Iterators for used strategies. 189210299Sed /// 190193323Sed iterator begin() const { return StrategyList.begin(); } 191193323Sed iterator end() const { return StrategyList.end(); } 192210299Sed 193193323Sed /// get - Look up function metadata. 194210299Sed /// 195193323Sed GCFunctionInfo &getFunctionInfo(const Function &F); 196193323Sed }; 197210299Sed 198193323Sed} 199193323Sed 200193323Sed#endif 201