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