optimize constant layout. This fixes encoding of 181.mcf (by ensuring
[oota-llvm.git] / lib / Bitcode / Writer / ValueEnumerator.h
1 //===-- Bitcode/Writer/ValueEnumerator.h - Number values --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This class gives values and types Unique ID's.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef VALUE_ENUMERATOR_H
15 #define VALUE_ENUMERATOR_H
16
17 #include "llvm/ADT/DenseMap.h"
18 #include <vector>
19
20 namespace llvm {
21
22 class Type;
23 class Value;
24 class BasicBlock;
25 class Function;
26 class Module;
27 class ParamAttrsList;
28 class TypeSymbolTable;
29 class ValueSymbolTable;
30
31 class ValueEnumerator {
32 public:
33   // For each type, we remember its Type* and occurrence frequency.
34   typedef std::vector<std::pair<const Type*, unsigned> > TypeList;
35
36   // For each value, we remember its Value* and occurrence frequency.
37   typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
38 private:
39   typedef DenseMap<const Type*, unsigned> TypeMapType;
40   TypeMapType TypeMap;
41   TypeList Types;
42
43   typedef DenseMap<const Value*, unsigned> ValueMapType;
44   ValueMapType ValueMap;
45   ValueList Values;
46   
47   typedef DenseMap<const ParamAttrsList*, unsigned> ParamAttrMapType;
48   ParamAttrMapType ParamAttrMap;
49   std::vector<const ParamAttrsList*> ParamAttrs;
50   
51   /// BasicBlocks - This contains all the basic blocks for the currently
52   /// incorporated function.  Their reverse mapping is stored in ValueMap.
53   std::vector<const BasicBlock*> BasicBlocks;
54   
55   /// When a function is incorporated, this is the size of the Values list
56   /// before incorporation.
57   unsigned NumModuleValues;
58   unsigned FirstFuncConstantID;
59   unsigned FirstInstID;
60   
61   ValueEnumerator(const ValueEnumerator &);  // DO NOT IMPLEMENT
62   void operator=(const ValueEnumerator &);   // DO NOT IMPLEMENT
63 public:
64   ValueEnumerator(const Module *M);
65
66   unsigned getValueID(const Value *V) const {
67     ValueMapType::const_iterator I = ValueMap.find(V);
68     assert(I != ValueMap.end() && "Value not in slotcalculator!");
69     return I->second-1;
70   }
71   
72   unsigned getTypeID(const Type *T) const {
73     TypeMapType::const_iterator I = TypeMap.find(T);
74     assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
75     return I->second-1;
76   }
77   
78   unsigned getParamAttrID(const ParamAttrsList *PAL) const {
79     if (PAL == 0) return 0;  // Null maps to zero.
80     ParamAttrMapType::const_iterator I = ParamAttrMap.find(PAL);
81     assert(I != ParamAttrMap.end() && "ParamAttr not in ValueEnumerator!");
82     return I->second;
83   }
84
85   /// getFunctionConstantRange - Return the range of values that corresponds to
86   /// function-local constants.
87   void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
88     Start = FirstFuncConstantID;
89     End = FirstInstID;
90   }
91   
92   const ValueList &getValues() const { return Values; }
93   const TypeList &getTypes() const { return Types; }
94   const std::vector<const BasicBlock*> &getBasicBlocks() const {
95     return BasicBlocks; 
96   }
97   const std::vector<const ParamAttrsList*> &getParamAttrs() const {
98     return ParamAttrs;
99   }
100
101   /// PurgeAggregateValues - If there are any aggregate values at the end of the
102   /// value list, remove them and return the count of the remaining values.  If
103   /// there are none, return -1.
104   int PurgeAggregateValues();
105   
106   /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
107   /// use these two methods to get its data into the ValueEnumerator!
108   ///
109   void incorporateFunction(const Function &F);
110   void purgeFunction();
111
112 private:
113   void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
114     
115   void EnumerateValue(const Value *V);
116   void EnumerateType(const Type *T);
117   void EnumerateParamAttrs(const ParamAttrsList *PAL);
118   
119   void EnumerateTypeSymbolTable(const TypeSymbolTable &ST);
120   void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
121 };
122
123 } // End llvm namespace
124
125 #endif