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/IR/Attributes.h"
33 class ValueSymbolTable;
37 class ValueEnumerator {
39 typedef std::vector<Type*> TypeList;
41 // For each value, we remember its Value* and occurrence frequency.
42 typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
44 typedef DenseMap<Type*, unsigned> TypeMapType;
48 typedef DenseMap<const Value*, unsigned> ValueMapType;
49 ValueMapType ValueMap;
52 SmallVector<const MDNode *, 8> FunctionLocalMDs;
53 ValueMapType MDValueMap;
55 typedef DenseMap<AttributeSet, unsigned> AttributeGroupMapType;
56 AttributeGroupMapType AttributeGroupMap;
57 std::vector<AttributeSet> AttributeGroups;
59 typedef DenseMap<AttributeSet, unsigned> AttributeMapType;
60 AttributeMapType AttributeMap;
61 std::vector<AttributeSet> Attribute;
63 /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
64 /// the "getGlobalBasicBlockID" method.
65 mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs;
67 typedef DenseMap<const Instruction*, unsigned> InstructionMapType;
68 InstructionMapType InstructionMap;
69 unsigned InstructionCount;
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;
75 /// When a function is incorporated, this is the size of the Values list
76 /// before incorporation.
77 unsigned NumModuleValues;
79 /// When a function is incorporated, this is the size of the MDValues list
80 /// before incorporation.
81 unsigned NumModuleMDValues;
83 unsigned FirstFuncConstantID;
86 ValueEnumerator(const ValueEnumerator &) LLVM_DELETED_FUNCTION;
87 void operator=(const ValueEnumerator &) LLVM_DELETED_FUNCTION;
89 ValueEnumerator(const Module *M);
92 void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const;
94 unsigned getValueID(const Value *V) const;
96 unsigned getTypeID(Type *T) const {
97 TypeMapType::const_iterator I = TypeMap.find(T);
98 assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
102 unsigned getInstructionID(const Instruction *I) const;
103 void setInstructionID(const Instruction *I);
105 unsigned getAttributeID(AttributeSet PAL) const {
106 if (PAL.isEmpty()) return 0; // Null maps to zero.
107 AttributeMapType::const_iterator I = AttributeMap.find(PAL);
108 assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
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!");
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;
126 const ValueList &getValues() const { return Values; }
127 const ValueList &getMDValues() const { return MDValues; }
128 const SmallVectorImpl<const MDNode *> &getFunctionLocalMDValues() const {
129 return FunctionLocalMDs;
131 const TypeList &getTypes() const { return Types; }
132 const std::vector<const BasicBlock*> &getBasicBlocks() const {
135 const std::vector<AttributeSet> &getAttributes() const {
138 const std::vector<AttributeSet> &getAttributeGroups() const {
139 return AttributeGroups;
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;
147 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
148 /// use these two methods to get its data into the ValueEnumerator!
150 void incorporateFunction(const Function &F);
151 void purgeFunction();
154 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
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(AttributeSet PAL);
165 void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
166 void EnumerateNamedMetadata(const Module *M);
169 } // End llvm namespace