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