LLVM API Documentation

GCMetadata.h

Go to the documentation of this file.
00001 //===-- GCMetadata.h - Garbage collector metadata -------------------------===//
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/Pass.h"
00037 #include "llvm/ADT/DenseMap.h"
00038 #include "llvm/ADT/StringMap.h"
00039 
00040 namespace llvm {
00041   
00042   class AsmPrinter;
00043   class GCStrategy;
00044   class Constant;
00045   class TargetAsmInfo;
00046   
00047   
00048   namespace GC {
00049     /// PointKind - The type of a collector-safe point.
00050     /// 
00051     enum PointKind {
00052       Loop,    //< Instr is a loop (backwards branch).
00053       Return,  //< Instr is a return instruction.
00054       PreCall, //< Instr is a call instruction.
00055       PostCall //< Instr is the return address of a call.
00056     };
00057   }
00058   
00059   /// GCPoint - Metadata for a collector-safe point in machine code.
00060   /// 
00061   struct GCPoint {
00062     GC::PointKind Kind; //< The kind of the safe point.
00063     unsigned Num;       //< Usually a label.
00064     
00065     GCPoint(GC::PointKind K, unsigned N) : Kind(K), Num(N) {}
00066   };
00067   
00068   /// GCRoot - Metadata for a pointer to an object managed by the garbage
00069   /// collector.
00070   struct GCRoot {
00071     int Num;            //< Usually a frame index.
00072     int StackOffset;    //< Offset from the stack pointer.
00073     Constant *Metadata; //< Metadata straight from the call to llvm.gcroot.
00074     
00075     GCRoot(int N, Constant *MD) : Num(N), StackOffset(-1), Metadata(MD) {}
00076   };
00077   
00078   
00079   /// GCFunctionInfo - Garbage collection metadata for a single function.
00080   /// 
00081   class GCFunctionInfo {
00082   public:
00083     typedef std::vector<GCPoint>::iterator iterator;
00084     typedef std::vector<GCRoot>::iterator roots_iterator;
00085     typedef std::vector<GCRoot>::const_iterator live_iterator;
00086     
00087   private:
00088     const Function &F;
00089     GCStrategy &S;
00090     uint64_t FrameSize;
00091     std::vector<GCRoot> Roots;
00092     std::vector<GCPoint> SafePoints;
00093     
00094     // FIXME: Liveness. A 2D BitVector, perhaps?
00095     // 
00096     //   BitVector Liveness;
00097     //   
00098     //   bool islive(int point, int root) =
00099     //     Liveness[point * SafePoints.size() + root]
00100     // 
00101     // The bit vector is the more compact representation where >3.2% of roots
00102     // are live per safe point (1.5% on 64-bit hosts).
00103     
00104   public:
00105     GCFunctionInfo(const Function &F, GCStrategy &S);
00106     ~GCFunctionInfo();
00107     
00108     /// getFunction - Return the function to which this metadata applies.
00109     /// 
00110     const Function &getFunction() const { return F; }
00111     
00112     /// getStrategy - Return the GC strategy for the function.
00113     /// 
00114     GCStrategy &getStrategy() { return S; }
00115     
00116     /// addStackRoot - Registers a root that lives on the stack. Num is the
00117     ///                stack object ID for the alloca (if the code generator is
00118     //                 using  MachineFrameInfo).
00119     void addStackRoot(int Num, Constant *Metadata) {
00120       Roots.push_back(GCRoot(Num, Metadata));
00121     }
00122     
00123     /// addSafePoint - Notes the existence of a safe point. Num is the ID of the
00124     /// label just prior to the safe point (if the code generator is using 
00125     /// MachineModuleInfo).
00126     void addSafePoint(GC::PointKind Kind, unsigned Num) {
00127       SafePoints.push_back(GCPoint(Kind, Num));
00128     }
00129     
00130     /// getFrameSize/setFrameSize - Records the function's frame size.
00131     /// 
00132     uint64_t getFrameSize() const { return FrameSize; }
00133     void setFrameSize(uint64_t S) { FrameSize = S; }
00134     
00135     /// begin/end - Iterators for safe points.
00136     /// 
00137     iterator begin() { return SafePoints.begin(); }
00138     iterator end()   { return SafePoints.end();   }
00139     size_t size() const { return SafePoints.size(); }
00140     
00141     /// roots_begin/roots_end - Iterators for all roots in the function.
00142     /// 
00143     roots_iterator roots_begin() { return Roots.begin(); }
00144     roots_iterator roots_end  () { return Roots.end();   }
00145     size_t roots_size() const { return Roots.size(); }
00146     
00147     /// live_begin/live_end - Iterators for live roots at a given safe point.
00148     /// 
00149     live_iterator live_begin(const iterator &p) { return roots_begin(); }
00150     live_iterator live_end  (const iterator &p) { return roots_end();   }
00151     size_t live_size(const iterator &p) const { return roots_size(); }
00152   };
00153   
00154   
00155   /// GCModuleInfo - Garbage collection metadata for a whole module.
00156   /// 
00157   class GCModuleInfo : public ImmutablePass {
00158     typedef StringMap<GCStrategy*> strategy_map_type;
00159     typedef std::vector<GCStrategy*> list_type;
00160     typedef DenseMap<const Function*,GCFunctionInfo*> finfo_map_type;
00161     
00162     strategy_map_type StrategyMap;
00163     list_type StrategyList;
00164     finfo_map_type FInfoMap;
00165     
00166     GCStrategy *getOrCreateStrategy(const Module *M, const std::string &Name);
00167     
00168   public:
00169     typedef list_type::const_iterator iterator;
00170     
00171     static char ID;
00172     
00173     GCModuleInfo();
00174     ~GCModuleInfo();
00175     
00176     /// clear - Resets the pass. The metadata deleter pass calls this.
00177     /// 
00178     void clear();
00179     
00180     /// begin/end - Iterators for used strategies.
00181     /// 
00182     iterator begin() const { return StrategyList.begin(); }
00183     iterator end()   const { return StrategyList.end();   }
00184     
00185     /// get - Look up function metadata.
00186     /// 
00187     GCFunctionInfo &getFunctionInfo(const Function &F);
00188   };
00189   
00190 }
00191 
00192 #endif



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.