Transform BU pass to not use the horrible DSCallSiteIterator class.
[oota-llvm.git] / lib / Bytecode / Writer / SlotCalculator.h
1 //===-- Analysis/SlotCalculator.h - Calculate value slots -------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This class calculates the slots that values will land in.  This is useful for
11 // when writing bytecode or assembly out, because you have to know these things.
12 //
13 // Specifically, this class calculates the "type plane numbering" that you see
14 // for a function if you strip out all of the symbols in it.  For assembly
15 // writing, this is used when a symbol does not have a name.  For bytecode
16 // writing, this is always used, and the symbol table is added on later.
17 //
18 //===----------------------------------------------------------------------===//
19
20 #ifndef LLVM_ANALYSIS_SLOTCALCULATOR_H
21 #define LLVM_ANALYSIS_SLOTCALCULATOR_H
22
23 #include <vector>
24 #include <map>
25
26 namespace llvm {
27
28 class Value;
29 class Type;
30 class Module;
31 class Function;
32 class SymbolTable;
33 class ConstantArray;
34
35 class SlotCalculator {
36   const Module *TheModule;
37
38   typedef std::vector<const Type*> TypeList;
39   typedef std::vector<const Value*> TypePlane;
40   std::vector<TypePlane> Table;
41   TypeList Types;
42   typedef std::map<const Value*, unsigned> NodeMapType;
43   NodeMapType NodeMap;
44
45   typedef std::map<const Type*, unsigned> TypeMapType;
46   TypeMapType TypeMap;
47
48   /// ConstantStrings - If we are indexing for a bytecode file, this keeps track
49   /// of all of the constants strings that need to be emitted.
50   std::vector<const ConstantArray*> ConstantStrings;
51
52   /// ModuleLevel - Used to keep track of which values belong to the module,
53   /// and which values belong to the currently incorporated function.
54   ///
55   std::vector<unsigned> ModuleLevel;
56   unsigned ModuleTypeLevel;
57
58   /// ModuleContainsAllFunctionConstants - This flag is set to true if all
59   /// function constants are incorporated into the module constant table.  This
60   /// is only possible if building information for a bytecode file.
61   bool ModuleContainsAllFunctionConstants;
62
63   /// CompactionTable/NodeMap - When function compaction has been performed,
64   /// these entries provide a compacted view of the namespace needed to emit
65   /// instructions in a function body.  The 'getSlot()' method automatically
66   /// returns these entries if applicable, or the global entries if not.
67   std::vector<TypePlane> CompactionTable;
68   TypeList CompactionTypes;
69   typedef std::map<const Value*, unsigned> CompactionNodeMapType;
70   CompactionNodeMapType CompactionNodeMap;
71   typedef std::map<const Type*, unsigned> CompactionTypeMapType;
72   CompactionTypeMapType CompactionTypeMap;
73
74   SlotCalculator(const SlotCalculator &);  // DO NOT IMPLEMENT
75   void operator=(const SlotCalculator &);  // DO NOT IMPLEMENT
76 public:
77   SlotCalculator(const Module *M );
78   // Start out in incorp state
79   SlotCalculator(const Function *F );
80   
81   /// getSlot - Return the slot number of the specified value in it's type
82   /// plane.  This returns < 0 on error!
83   ///
84   int getSlot(const Value *V) const;
85   int getSlot(const Type* T) const;
86
87   /// getGlobalSlot - Return a slot number from the global table.  This can only
88   /// be used when a compaction table is active.
89   unsigned getGlobalSlot(const Value *V) const;
90   unsigned getGlobalSlot(const Type *V) const;
91
92   inline unsigned getNumPlanes() const {
93     if (CompactionTable.empty())
94       return Table.size();
95     else
96       return CompactionTable.size();
97   }
98
99   inline unsigned getNumTypes() const {
100     if (CompactionTypes.empty())
101       return Types.size();
102     else
103       return CompactionTypes.size();
104   }
105
106   inline unsigned getModuleLevel(unsigned Plane) const { 
107     return Plane < ModuleLevel.size() ? ModuleLevel[Plane] : 0; 
108   }
109
110   /// Returns the number of types in the type list that are at module level
111   inline unsigned getModuleTypeLevel() const {
112     return ModuleTypeLevel;
113   }
114
115   TypePlane &getPlane(unsigned Plane);
116   TypeList& getTypes() { 
117     if (!CompactionTypes.empty())
118       return CompactionTypes;
119     return Types;
120   }
121
122   /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
123   /// use these two methods to get its data into the SlotCalculator!
124   ///
125   void incorporateFunction(const Function *F);
126   void purgeFunction();
127
128   /// string_iterator/string_begin/end - Access the list of module-level
129   /// constant strings that have been incorporated.  This is only applicable to
130   /// bytecode files.
131   typedef std::vector<const ConstantArray*>::const_iterator string_iterator;
132   string_iterator string_begin() const { return ConstantStrings.begin(); }
133   string_iterator string_end() const   { return ConstantStrings.end(); }
134
135   const std::vector<TypePlane> &getCompactionTable() const {
136     return CompactionTable;
137   }
138
139   const TypeList& getCompactionTypes() const { return CompactionTypes; }
140
141   /// @brief Determine if the compaction table (not types) is empty
142   bool CompactionTableIsEmpty() const;
143
144 private:
145   // getOrCreateSlot - Values can be crammed into here at will... if
146   // they haven't been inserted already, they get inserted, otherwise
147   // they are ignored.
148   //
149   int getOrCreateSlot(const Value *D);
150   int getOrCreateSlot(const Type* T);
151
152   // insertValue - Insert a value into the value table... Return the
153   // slot that it occupies, or -1 if the declaration is to be ignored
154   // because of the IgnoreNamedNodes flag.
155   //
156   int insertValue(const Value *D, bool dontIgnore = false);
157   int insertType(const Type* T, bool dontIgnore = false );
158
159   // doInsertValue - Small helper function to be called only be insertVal.
160   int doInsertValue(const Value *D);
161   int doInsertType(const Type*T);
162
163   // processModule - Process all of the module level function declarations and
164   // types that are available.
165   //
166   void processModule();
167
168   // processSymbolTable - Insert all of the values in the specified symbol table
169   // into the values table...
170   //
171   void processSymbolTable(const SymbolTable *ST);
172   void processSymbolTableConstants(const SymbolTable *ST);
173
174   void buildCompactionTable(const Function *F);
175   unsigned getOrCreateCompactionTableSlot(const Value *V);
176   unsigned getOrCreateCompactionTableSlot(const Type *V);
177   void pruneCompactionTable();
178 };
179
180 } // End llvm namespace
181
182 #endif