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