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