LLVM API Documentation

GCMetadata.h
Go to the documentation of this file.
00001 //===-- GCMetadata.h - Garbage collector metadata ---------------*- 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 // This file declares the GCFunctionInfo and GCModuleInfo classes, which are
00011 // used as a communication channel from the target code generator to the target
00012 // garbage collectors. This interface allows code generators and garbage
00013 // collectors to be developed independently.
00014 //
00015 // The GCFunctionInfo class logs the data necessary to build a type accurate
00016 // stack map. The code generator outputs:
00017 //
00018 //   - Safe points as specified by the GCStrategy's NeededSafePoints.
00019 //   - Stack offsets for GC roots, as specified by calls to llvm.gcroot
00020 //
00021 // As a refinement, liveness analysis calculates the set of live roots at each
00022 // safe point. Liveness analysis is not presently performed by the code
00023 // generator, so all roots are assumed live.
00024 //
00025 // GCModuleInfo simply collects GCFunctionInfo instances for each Function as
00026 // they are compiled. This accretion is necessary for collectors which must emit
00027 // a stack map for the compilation unit as a whole. Therefore, GCFunctionInfo
00028 // outlives the MachineFunction from which it is derived and must not refer to
00029 // any code generator data structures.
00030 //
00031 //===----------------------------------------------------------------------===//
00032 
00033 #ifndef LLVM_CODEGEN_GCMETADATA_H
00034 #define LLVM_CODEGEN_GCMETADATA_H
00035 
00036 #include "llvm/ADT/DenseMap.h"
00037 #include "llvm/ADT/StringMap.h"
00038 #include "llvm/IR/DebugLoc.h"
00039 #include "llvm/Pass.h"
00040 
00041 #include <memory>
00042 
00043 namespace llvm {
00044   class AsmPrinter;
00045   class GCStrategy;
00046   class Constant;
00047   class MCSymbol;
00048 
00049   namespace GC {
00050     /// PointKind - The type of a collector-safe point.
00051     ///
00052     enum PointKind {
00053       Loop,    ///< Instr is a loop (backwards branch).
00054       Return,  ///< Instr is a return instruction.
00055       PreCall, ///< Instr is a call instruction.
00056       PostCall ///< Instr is the return address of a call.
00057     };
00058   }
00059 
00060   /// GCPoint - Metadata for a collector-safe point in machine code.
00061   ///
00062   struct GCPoint {
00063     GC::PointKind Kind; ///< The kind of the safe point.
00064     MCSymbol *Label;    ///< A label.
00065     DebugLoc Loc;
00066 
00067     GCPoint(GC::PointKind K, MCSymbol *L, DebugLoc DL)
00068         : Kind(K), Label(L), Loc(DL) {}
00069   };
00070 
00071   /// GCRoot - Metadata for a pointer to an object managed by the garbage
00072   /// collector.
00073   struct GCRoot {
00074     int Num;            ///< Usually a frame index.
00075     int StackOffset;    ///< Offset from the stack pointer.
00076     const Constant *Metadata; ///< Metadata straight from the call
00077                               ///< to llvm.gcroot.
00078 
00079     GCRoot(int N, const Constant *MD) : Num(N), StackOffset(-1), Metadata(MD) {}
00080   };
00081 
00082 
00083   /// GCFunctionInfo - Garbage collection metadata for a single function.
00084   ///
00085   class GCFunctionInfo {
00086   public:
00087     typedef std::vector<GCPoint>::iterator iterator;
00088     typedef std::vector<GCRoot>::iterator roots_iterator;
00089     typedef std::vector<GCRoot>::const_iterator live_iterator;
00090 
00091   private:
00092     const Function &F;
00093     GCStrategy &S;
00094     uint64_t FrameSize;
00095     std::vector<GCRoot> Roots;
00096     std::vector<GCPoint> SafePoints;
00097 
00098     // FIXME: Liveness. A 2D BitVector, perhaps?
00099     //
00100     //   BitVector Liveness;
00101     //
00102     //   bool islive(int point, int root) =
00103     //     Liveness[point * SafePoints.size() + root]
00104     //
00105     // The bit vector is the more compact representation where >3.2% of roots
00106     // are live per safe point (1.5% on 64-bit hosts).
00107 
00108   public:
00109     GCFunctionInfo(const Function &F, GCStrategy &S);
00110     ~GCFunctionInfo();
00111 
00112     /// getFunction - Return the function to which this metadata applies.
00113     ///
00114     const Function &getFunction() const { return F; }
00115 
00116     /// getStrategy - Return the GC strategy for the function.
00117     ///
00118     GCStrategy &getStrategy() { return S; }
00119 
00120     /// addStackRoot - Registers a root that lives on the stack. Num is the
00121     ///                stack object ID for the alloca (if the code generator is
00122     //                 using  MachineFrameInfo).
00123     void addStackRoot(int Num, const Constant *Metadata) {
00124       Roots.push_back(GCRoot(Num, Metadata));
00125     }
00126 
00127     /// removeStackRoot - Removes a root.
00128     roots_iterator removeStackRoot(roots_iterator position) {
00129       return Roots.erase(position);
00130     }
00131 
00132     /// addSafePoint - Notes the existence of a safe point. Num is the ID of the
00133     /// label just prior to the safe point (if the code generator is using
00134     /// MachineModuleInfo).
00135     void addSafePoint(GC::PointKind Kind, MCSymbol *Label, DebugLoc DL) {
00136       SafePoints.push_back(GCPoint(Kind, Label, DL));
00137     }
00138 
00139     /// getFrameSize/setFrameSize - Records the function's frame size.
00140     ///
00141     uint64_t getFrameSize() const { return FrameSize; }
00142     void setFrameSize(uint64_t S) { FrameSize = S; }
00143 
00144     /// begin/end - Iterators for safe points.
00145     ///
00146     iterator begin() { return SafePoints.begin(); }
00147     iterator end()   { return SafePoints.end();   }
00148     size_t size() const { return SafePoints.size(); }
00149 
00150     /// roots_begin/roots_end - Iterators for all roots in the function.
00151     ///
00152     roots_iterator roots_begin() { return Roots.begin(); }
00153     roots_iterator roots_end  () { return Roots.end();   }
00154     size_t roots_size() const { return Roots.size(); }
00155 
00156     /// live_begin/live_end - Iterators for live roots at a given safe point.
00157     ///
00158     live_iterator live_begin(const iterator &p) { return roots_begin(); }
00159     live_iterator live_end  (const iterator &p) { return roots_end();   }
00160     size_t live_size(const iterator &p) const { return roots_size(); }
00161   };
00162 
00163 
00164   /// GCModuleInfo - Garbage collection metadata for a whole module.
00165   ///
00166   class GCModuleInfo : public ImmutablePass {
00167     typedef StringMap<GCStrategy*> strategy_map_type;
00168     typedef std::vector<std::unique_ptr<GCStrategy>> list_type;
00169     typedef DenseMap<const Function*,GCFunctionInfo*> finfo_map_type;
00170 
00171     strategy_map_type StrategyMap;
00172     list_type StrategyList;
00173     finfo_map_type FInfoMap;
00174 
00175     GCStrategy *getOrCreateStrategy(const Module *M, const std::string &Name);
00176 
00177   public:
00178     typedef list_type::const_iterator iterator;
00179 
00180     static char ID;
00181 
00182     GCModuleInfo();
00183 
00184     /// clear - Resets the pass. Any pass, which uses GCModuleInfo, should
00185     /// call it in doFinalization().
00186     ///
00187     void clear();
00188 
00189     /// begin/end - Iterators for used strategies.
00190     ///
00191     iterator begin() const { return StrategyList.begin(); }
00192     iterator end()   const { return StrategyList.end();   }
00193 
00194     /// get - Look up function metadata.
00195     ///
00196     GCFunctionInfo &getFunctionInfo(const Function &F);
00197   };
00198 
00199 }
00200 
00201 #endif