1 //===-- Bitcode/Writer/ValueEnumerator.h - Number values --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class gives values and types Unique ID's.
12 //===----------------------------------------------------------------------===//
14 #ifndef VALUE_ENUMERATOR_H
15 #define VALUE_ENUMERATOR_H
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/UniqueVector.h"
20 #include "llvm/IR/Attributes.h"
35 class ValueSymbolTable;
39 class ValueEnumerator {
41 typedef std::vector<Type*> TypeList;
43 // For each value, we remember its Value* and occurrence frequency.
44 typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
46 typedef DenseMap<Type*, unsigned> TypeMapType;
50 typedef DenseMap<const Value*, unsigned> ValueMapType;
51 ValueMapType ValueMap;
54 typedef UniqueVector<const Comdat *> ComdatSetType;
55 ComdatSetType Comdats;
58 SmallVector<const MDNode *, 8> FunctionLocalMDs;
59 ValueMapType MDValueMap;
61 typedef DenseMap<AttributeSet, unsigned> AttributeGroupMapType;
62 AttributeGroupMapType AttributeGroupMap;
63 std::vector<AttributeSet> AttributeGroups;
65 typedef DenseMap<AttributeSet, unsigned> AttributeMapType;
66 AttributeMapType AttributeMap;
67 std::vector<AttributeSet> Attribute;
69 /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
70 /// the "getGlobalBasicBlockID" method.
71 mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs;
73 typedef DenseMap<const Instruction*, unsigned> InstructionMapType;
74 InstructionMapType InstructionMap;
75 unsigned InstructionCount;
77 /// BasicBlocks - This contains all the basic blocks for the currently
78 /// incorporated function. Their reverse mapping is stored in ValueMap.
79 std::vector<const BasicBlock*> BasicBlocks;
81 /// When a function is incorporated, this is the size of the Values list
82 /// before incorporation.
83 unsigned NumModuleValues;
85 /// When a function is incorporated, this is the size of the MDValues list
86 /// before incorporation.
87 unsigned NumModuleMDValues;
89 unsigned FirstFuncConstantID;
92 ValueEnumerator(const ValueEnumerator &) LLVM_DELETED_FUNCTION;
93 void operator=(const ValueEnumerator &) LLVM_DELETED_FUNCTION;
95 ValueEnumerator(const Module *M);
98 void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const;
100 unsigned getValueID(const Value *V) const;
102 unsigned getTypeID(Type *T) const {
103 TypeMapType::const_iterator I = TypeMap.find(T);
104 assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
108 unsigned getInstructionID(const Instruction *I) const;
109 void setInstructionID(const Instruction *I);
111 unsigned getAttributeID(AttributeSet PAL) const {
112 if (PAL.isEmpty()) return 0; // Null maps to zero.
113 AttributeMapType::const_iterator I = AttributeMap.find(PAL);
114 assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
118 unsigned getAttributeGroupID(AttributeSet PAL) const {
119 if (PAL.isEmpty()) return 0; // Null maps to zero.
120 AttributeGroupMapType::const_iterator I = AttributeGroupMap.find(PAL);
121 assert(I != AttributeGroupMap.end() && "Attribute not in ValueEnumerator!");
125 /// getFunctionConstantRange - Return the range of values that corresponds to
126 /// function-local constants.
127 void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
128 Start = FirstFuncConstantID;
132 const ValueList &getValues() const { return Values; }
133 const ValueList &getMDValues() const { return MDValues; }
134 const SmallVectorImpl<const MDNode *> &getFunctionLocalMDValues() const {
135 return FunctionLocalMDs;
137 const TypeList &getTypes() const { return Types; }
138 const std::vector<const BasicBlock*> &getBasicBlocks() const {
141 const std::vector<AttributeSet> &getAttributes() const {
144 const std::vector<AttributeSet> &getAttributeGroups() const {
145 return AttributeGroups;
148 const ComdatSetType &getComdats() const { return Comdats; }
149 unsigned getComdatID(const Comdat *C) const;
151 /// getGlobalBasicBlockID - This returns the function-specific ID for the
152 /// specified basic block. This is relatively expensive information, so it
153 /// should only be used by rare constructs such as address-of-label.
154 unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;
156 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
157 /// use these two methods to get its data into the ValueEnumerator!
159 void incorporateFunction(const Function &F);
160 void purgeFunction();
163 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
165 void EnumerateMDNodeOperands(const MDNode *N);
166 void EnumerateMetadata(const Value *MD);
167 void EnumerateFunctionLocalMetadata(const MDNode *N);
168 void EnumerateNamedMDNode(const NamedMDNode *NMD);
169 void EnumerateValue(const Value *V);
170 void EnumerateType(Type *T);
171 void EnumerateOperandType(const Value *V);
172 void EnumerateAttributes(AttributeSet PAL);
174 void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
175 void EnumerateNamedMetadata(const Module *M);
178 } // End llvm namespace