1 //===-- Bitcode/Writer/ValueEnumerator.h - Number values --------*- 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 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 "llvm/Attributes.h"
19 #include <vector>
20 
21 namespace llvm {
22 
23 class Type;
24 class Value;
25 class BasicBlock;
26 class Function;
27 class Module;
28 class MetadataBase;
29 class AttrListPtr;
30 class TypeSymbolTable;
31 class ValueSymbolTable;
32 
33 class ValueEnumerator {
34 public:
35   // For each type, we remember its Type* and occurrence frequency.
36   typedef std::vector<std::pair<const Type*, unsigned> > TypeList;
37 
38   // For each value, we remember its Value* and occurrence frequency.
39   typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
40 private:
41   typedef DenseMap<const Type*, unsigned> TypeMapType;
42   TypeMapType TypeMap;
43   TypeList Types;
44 
45   typedef DenseMap<const Value*, unsigned> ValueMapType;
46   ValueMapType ValueMap;
47   ValueList Values;
48   ValueList MDValues;
49   ValueMapType MDValueMap;
50 
51   typedef DenseMap<void*, unsigned> AttributeMapType;
52   AttributeMapType AttributeMap;
53   std::vector<AttrListPtr> Attributes;
54 
55   /// BasicBlocks - This contains all the basic blocks for the currently
56   /// incorporated function.  Their reverse mapping is stored in ValueMap.
57   std::vector<const BasicBlock*> BasicBlocks;
58 
59   /// When a function is incorporated, this is the size of the Values list
60   /// before incorporation.
61   unsigned NumModuleValues;
62   unsigned FirstFuncConstantID;
63   unsigned FirstInstID;
64 
65   ValueEnumerator(const ValueEnumerator &);  // DO NOT IMPLEMENT
66   void operator=(const ValueEnumerator &);   // DO NOT IMPLEMENT
67 public:
68   ValueEnumerator(const Module *M);
69 
70   unsigned getValueID(const Value *V) const;
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 getAttributeID(const AttrListPtr &PAL) const {
79     if (PAL.isEmpty()) return 0;  // Null maps to zero.
80     AttributeMapType::const_iterator I = AttributeMap.find(PAL.getRawPointer());
81     assert(I != AttributeMap.end() && "Attribute 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 ValueList &getMDValues() const { return MDValues; }
94   const TypeList &getTypes() const { return Types; }
95   const std::vector<const BasicBlock*> &getBasicBlocks() const {
96     return BasicBlocks;
97   }
98   const std::vector<AttrListPtr> &getAttributes() const {
99     return Attributes;
100   }
101 
102   /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
103   /// use these two methods to get its data into the ValueEnumerator!
104   ///
105   void incorporateFunction(const Function &F);
106   void purgeFunction();
107 
108 private:
109   void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
110 
111   void EnumerateMetadata(const MetadataBase *MD);
112   void EnumerateValue(const Value *V);
113   void EnumerateType(const Type *T);
114   void EnumerateOperandType(const Value *V);
115   void EnumerateAttributes(const AttrListPtr &PAL);
116 
117   void EnumerateTypeSymbolTable(const TypeSymbolTable &ST);
118   void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
119 };
120 
121 } // End llvm namespace
122 
123 #endif
124