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/Attributes.h"
33 class TypeSymbolTable;
34 class ValueSymbolTable;
37 class ValueEnumerator {
39 // For each type, we remember its Type* and occurrence frequency.
40 typedef std::vector<std::pair<const Type*, unsigned> > TypeList;
42 // For each value, we remember its Value* and occurrence frequency.
43 typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
45 typedef DenseMap<const Type*, unsigned> TypeMapType;
49 typedef DenseMap<const Value*, unsigned> ValueMapType;
50 ValueMapType ValueMap;
53 SmallVector<const MDNode *, 8> FunctionLocalMDs;
54 ValueMapType MDValueMap;
56 typedef DenseMap<void*, unsigned> AttributeMapType;
57 AttributeMapType AttributeMap;
58 std::vector<AttrListPtr> Attributes;
60 /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
61 /// the "getGlobalBasicBlockID" method.
62 mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs;
64 typedef DenseMap<const Instruction*, unsigned> InstructionMapType;
65 InstructionMapType InstructionMap;
66 unsigned InstructionCount;
68 /// BasicBlocks - This contains all the basic blocks for the currently
69 /// incorporated function. Their reverse mapping is stored in ValueMap.
70 std::vector<const BasicBlock*> BasicBlocks;
72 /// When a function is incorporated, this is the size of the Values list
73 /// before incorporation.
74 unsigned NumModuleValues;
75 unsigned FirstFuncConstantID;
78 ValueEnumerator(const ValueEnumerator &); // DO NOT IMPLEMENT
79 void operator=(const ValueEnumerator &); // DO NOT IMPLEMENT
81 ValueEnumerator(const Module *M);
83 unsigned getValueID(const Value *V) const;
85 unsigned getTypeID(const Type *T) const {
86 TypeMapType::const_iterator I = TypeMap.find(T);
87 assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
91 unsigned getInstructionID(const Instruction *I) const;
92 void setInstructionID(const Instruction *I);
94 unsigned getAttributeID(const AttrListPtr &PAL) const {
95 if (PAL.isEmpty()) return 0; // Null maps to zero.
96 AttributeMapType::const_iterator I = AttributeMap.find(PAL.getRawPointer());
97 assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
101 /// getFunctionConstantRange - Return the range of values that corresponds to
102 /// function-local constants.
103 void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
104 Start = FirstFuncConstantID;
108 const ValueList &getValues() const { return Values; }
109 const ValueList &getMDValues() const { return MDValues; }
110 const SmallVector<const MDNode *, 8> &getFunctionLocalMDValues() const {
111 return FunctionLocalMDs;
113 const TypeList &getTypes() const { return Types; }
114 const std::vector<const BasicBlock*> &getBasicBlocks() const {
117 const std::vector<AttrListPtr> &getAttributes() const {
121 /// getGlobalBasicBlockID - This returns the function-specific ID for the
122 /// specified basic block. This is relatively expensive information, so it
123 /// should only be used by rare constructs such as address-of-label.
124 unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;
126 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
127 /// use these two methods to get its data into the ValueEnumerator!
129 void incorporateFunction(const Function &F);
130 void purgeFunction();
133 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
135 void EnumerateMetadata(const Value *MD);
136 void EnumerateNamedMDNode(const NamedMDNode *NMD);
137 void EnumerateValue(const Value *V);
138 void EnumerateType(const Type *T);
139 void EnumerateOperandType(const Value *V);
140 void EnumerateAttributes(const AttrListPtr &PAL);
142 void EnumerateTypeSymbolTable(const TypeSymbolTable &ST);
143 void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
144 void EnumerateMDSymbolTable(const MDSymbolTable &ST);
147 } // End llvm namespace