* Make contained ostream not public.
[oota-llvm.git] / include / llvm / Analysis / 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 Module;
30 class Function;
31 class SymbolTable;
32 class ConstantArray;
33
34 class SlotCalculator {
35   const Module *TheModule;
36
37   /// BuildBytecodeInfo - If true, this is the creating information for the
38   /// bytecode writer, if false, we are building information for the assembly
39   /// emitter.  The assembly emitter doesn't need named objects numbered, among
40   /// other differences.
41   bool BuildBytecodeInfo;
42
43   typedef std::vector<const Value*> TypePlane;
44   std::vector<TypePlane> Table;
45   std::map<const Value*, unsigned> NodeMap;
46
47   /// ConstantStrings - If we are indexing for a bytecode file, this keeps track
48   /// of all of the constants strings that need to be emitted.
49   std::vector<const ConstantArray*> ConstantStrings;
50
51   /// ModuleLevel - Used to keep track of which values belong to the module,
52   /// and which values belong to the currently incorporated function.
53   ///
54   std::vector<unsigned> ModuleLevel;
55
56   /// ModuleContainsAllFunctionConstants - This flag is set to true if all
57   /// function constants are incorporated into the module constant table.  This
58   /// is only possible if building information for a bytecode file.
59   bool ModuleContainsAllFunctionConstants;
60
61   /// CompactionTable/NodeMap - When function compaction has been performed,
62   /// these entries provide a compacted view of the namespace needed to emit
63   /// instructions in a function body.  The 'getSlot()' method automatically
64   /// returns these entries if applicable, or the global entries if not.
65   std::vector<TypePlane> CompactionTable;
66   std::map<const Value*, unsigned> CompactionNodeMap;
67
68   SlotCalculator(const SlotCalculator &);  // DO NOT IMPLEMENT
69   void operator=(const SlotCalculator &);  // DO NOT IMPLEMENT
70 public:
71   SlotCalculator(const Module *M, bool BuildBytecodeInfo);
72   // Start out in incorp state
73   SlotCalculator(const Function *F, bool BuildBytecodeInfo);
74   
75   /// getSlot - Return the slot number of the specified value in it's type
76   /// plane.  This returns < 0 on error!
77   ///
78   int getSlot(const Value *V) const;
79
80   /// getGlobalSlot - Return a slot number from the global table.  This can only
81   /// be used when a compaction table is active.
82   unsigned getGlobalSlot(const Value *V) const;
83
84   inline unsigned getNumPlanes() const {
85     if (CompactionTable.empty())
86       return Table.size();
87     else
88       return CompactionTable.size();
89   }
90   inline unsigned getModuleLevel(unsigned Plane) const { 
91     return Plane < ModuleLevel.size() ? ModuleLevel[Plane] : 0; 
92   }
93
94   TypePlane &getPlane(unsigned Plane);
95
96   /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
97   /// use these two methods to get its data into the SlotCalculator!
98   ///
99   void incorporateFunction(const Function *F);
100   void purgeFunction();
101
102   /// string_iterator/string_begin/end - Access the list of module-level
103   /// constant strings that have been incorporated.  This is only applicable to
104   /// bytecode files.
105   typedef std::vector<const ConstantArray*>::const_iterator string_iterator;
106   string_iterator string_begin() const { return ConstantStrings.begin(); }
107   string_iterator string_end() const   { return ConstantStrings.end(); }
108
109   const std::vector<TypePlane> &getCompactionTable() const {
110     return CompactionTable;
111   }
112
113 private:
114   // getOrCreateSlot - Values can be crammed into here at will... if
115   // they haven't been inserted already, they get inserted, otherwise
116   // they are ignored.
117   //
118   int getOrCreateSlot(const Value *D);
119
120   // insertValue - Insert a value into the value table... Return the
121   // slot that it occupies, or -1 if the declaration is to be ignored
122   // because of the IgnoreNamedNodes flag.
123   //
124   int insertValue(const Value *D, bool dontIgnore = false);
125
126   // doInsertValue - Small helper function to be called only be insertVal.
127   int doInsertValue(const Value *D);
128
129   // processModule - Process all of the module level function declarations and
130   // types that are available.
131   //
132   void processModule();
133
134   // processSymbolTable - Insert all of the values in the specified symbol table
135   // into the values table...
136   //
137   void processSymbolTable(const SymbolTable *ST);
138   void processSymbolTableConstants(const SymbolTable *ST);
139
140   void buildCompactionTable(const Function *F);
141   unsigned getOrCreateCompactionTableSlot(const Value *V);
142   void pruneCompactionTable();
143 };
144
145 } // End llvm namespace
146
147 #endif