fbd0976c8cbeb385de956647351445b15c6d709f
[oota-llvm.git] / include / llvm / Analysis / SlotCalculator.h
1 //===-- llvm/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_SLOTCALCULATOR_H
21 #define LLVM_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 public:
57   SlotCalculator(const Module *M, bool BuildBytecodeInfo);
58   // Start out in incorp state
59   SlotCalculator(const Function *F, bool BuildBytecodeInfo);
60   
61   /// getSlot returns < 0 on error!
62   ///
63   int getSlot(const Value *D) const;
64
65   inline unsigned getNumPlanes() const { return Table.size(); }
66   inline unsigned getModuleLevel(unsigned Plane) const { 
67     return Plane < ModuleLevel.size() ? ModuleLevel[Plane] : 0; 
68   }
69
70   inline const TypePlane &getPlane(unsigned Plane) const { 
71     return Table[Plane]; 
72   }
73
74   /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
75   /// use these two methods to get its data into the SlotCalculator!
76   ///
77   void incorporateFunction(const Function *F);
78   void purgeFunction();
79
80   /// string_iterator/string_begin/end - Access the list of module-level
81   /// constant strings that have been incorporated.  This is only applicable to
82   /// bytecode files.
83   typedef std::vector<const ConstantArray*>::const_iterator string_iterator;
84   string_iterator string_begin() const { return ConstantStrings.begin(); }
85   string_iterator string_end() const   { return ConstantStrings.end(); }
86
87
88 protected:
89   // getOrCreateSlot - Values can be crammed into here at will... if
90   // they haven't been inserted already, they get inserted, otherwise
91   // they are ignored.
92   //
93   int getOrCreateSlot(const Value *D);
94
95   // insertValue - Insert a value into the value table... Return the
96   // slot that it occupies, or -1 if the declaration is to be ignored
97   // because of the IgnoreNamedNodes flag.
98   //
99   int insertValue(const Value *D, bool dontIgnore = false);
100
101   // doInsertValue - Small helper function to be called only be insertVal.
102   int doInsertValue(const Value *D);
103
104   // processModule - Process all of the module level function declarations and
105   // types that are available.
106   //
107   void processModule();
108
109   // processSymbolTable - Insert all of the values in the specified symbol table
110   // into the values table...
111   //
112   void processSymbolTable(const SymbolTable *ST);
113   void processSymbolTableConstants(const SymbolTable *ST);
114 };
115
116 } // End llvm namespace
117
118 #endif