Rename AttributeSets to AttributeGroups so that it's more meaningful.
[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 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/ADT/SmallVector.h"
19 #include "llvm/IR/Attributes.h"
20 #include <vector>
21
22 namespace llvm {
23
24 class Type;
25 class Value;
26 class Instruction;
27 class BasicBlock;
28 class Function;
29 class Module;
30 class MDNode;
31 class NamedMDNode;
32 class AttributeSet;
33 class ValueSymbolTable;
34 class MDSymbolTable;
35 class raw_ostream;
36
37 class ValueEnumerator {
38 public:
39   typedef std::vector<Type*> TypeList;
40
41   // For each value, we remember its Value* and occurrence frequency.
42   typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
43 private:
44   typedef DenseMap<Type*, unsigned> TypeMapType;
45   TypeMapType TypeMap;
46   TypeList Types;
47
48   typedef DenseMap<const Value*, unsigned> ValueMapType;
49   ValueMapType ValueMap;
50   ValueList Values;
51   ValueList MDValues;
52   SmallVector<const MDNode *, 8> FunctionLocalMDs;
53   ValueMapType MDValueMap;
54
55   typedef DenseMap<AttributeSet, unsigned> AttributeGroupMapType;
56   AttributeGroupMapType AttributeGroupMap;
57   std::vector<AttributeSet> AttributeGroups;
58
59   typedef DenseMap<void*, unsigned> AttributeMapType;
60   AttributeMapType AttributeMap;
61   std::vector<AttributeSet> Attribute;
62
63   /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
64   /// the "getGlobalBasicBlockID" method.
65   mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs;
66
67   typedef DenseMap<const Instruction*, unsigned> InstructionMapType;
68   InstructionMapType InstructionMap;
69   unsigned InstructionCount;
70
71   /// BasicBlocks - This contains all the basic blocks for the currently
72   /// incorporated function.  Their reverse mapping is stored in ValueMap.
73   std::vector<const BasicBlock*> BasicBlocks;
74
75   /// When a function is incorporated, this is the size of the Values list
76   /// before incorporation.
77   unsigned NumModuleValues;
78
79   /// When a function is incorporated, this is the size of the MDValues list
80   /// before incorporation.
81   unsigned NumModuleMDValues;
82
83   unsigned FirstFuncConstantID;
84   unsigned FirstInstID;
85
86   ValueEnumerator(const ValueEnumerator &) LLVM_DELETED_FUNCTION;
87   void operator=(const ValueEnumerator &) LLVM_DELETED_FUNCTION;
88 public:
89   ValueEnumerator(const Module *M);
90
91   void dump() const;
92   void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const;
93
94   unsigned getValueID(const Value *V) const;
95
96   unsigned getTypeID(Type *T) const {
97     TypeMapType::const_iterator I = TypeMap.find(T);
98     assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
99     return I->second-1;
100   }
101
102   unsigned getInstructionID(const Instruction *I) const;
103   void setInstructionID(const Instruction *I);
104
105   unsigned getAttributeID(AttributeSet PAL) const {
106     if (PAL.isEmpty()) return 0;  // Null maps to zero.
107     AttributeMapType::const_iterator I = AttributeMap.find(PAL.getRawPointer());
108     assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
109     return I->second;
110   }
111
112   unsigned getAttributeGroupID(AttributeSet PAL) const {
113     if (PAL.isEmpty()) return 0;  // Null maps to zero.
114     AttributeGroupMapType::const_iterator I = AttributeGroupMap.find(PAL);
115     assert(I != AttributeGroupMap.end() && "Attribute not in ValueEnumerator!");
116     return I->second;
117   }
118
119   /// getFunctionConstantRange - Return the range of values that corresponds to
120   /// function-local constants.
121   void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
122     Start = FirstFuncConstantID;
123     End = FirstInstID;
124   }
125
126   const ValueList &getValues() const { return Values; }
127   const ValueList &getMDValues() const { return MDValues; }
128   const SmallVector<const MDNode *, 8> &getFunctionLocalMDValues() const {
129     return FunctionLocalMDs;
130   }
131   const TypeList &getTypes() const { return Types; }
132   const std::vector<const BasicBlock*> &getBasicBlocks() const {
133     return BasicBlocks;
134   }
135   const std::vector<AttributeSet> &getAttributes() const {
136     return Attribute;
137   }
138   const std::vector<AttributeSet> &getAttributeGroups() const {
139     return AttributeGroups;
140   }
141
142   /// getGlobalBasicBlockID - This returns the function-specific ID for the
143   /// specified basic block.  This is relatively expensive information, so it
144   /// should only be used by rare constructs such as address-of-label.
145   unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;
146
147   /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
148   /// use these two methods to get its data into the ValueEnumerator!
149   ///
150   void incorporateFunction(const Function &F);
151   void purgeFunction();
152
153 private:
154   void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
155
156   void EnumerateMDNodeOperands(const MDNode *N);
157   void EnumerateMetadata(const Value *MD);
158   void EnumerateFunctionLocalMetadata(const MDNode *N);
159   void EnumerateNamedMDNode(const NamedMDNode *NMD);
160   void EnumerateValue(const Value *V);
161   void EnumerateType(Type *T);
162   void EnumerateOperandType(const Value *V);
163   void EnumerateAttributes(const AttributeSet &PAL);
164
165   void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
166   void EnumerateNamedMetadata(const Module *M);
167 };
168
169 } // End llvm namespace
170
171 #endif