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/Attributes.h"
31 class TypeSymbolTable;
32 class ValueSymbolTable;
35 class ValueEnumerator {
37 // For each type, we remember its Type* and occurrence frequency.
38 typedef std::vector<std::pair<const Type*, unsigned> > TypeList;
40 // For each value, we remember its Value* and occurrence frequency.
41 typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
43 typedef DenseMap<const Type*, unsigned> TypeMapType;
47 typedef DenseMap<const Value*, unsigned> ValueMapType;
48 ValueMapType ValueMap;
51 ValueMapType MDValueMap;
53 typedef DenseMap<void*, unsigned> AttributeMapType;
54 AttributeMapType AttributeMap;
55 std::vector<AttrListPtr> Attributes;
57 /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
58 /// the "getGlobalBasicBlockID" method.
59 mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs;
61 typedef DenseMap<const Instruction*, unsigned> InstructionMapType;
62 InstructionMapType InstructionMap;
63 unsigned InstructionCount;
65 /// BasicBlocks - This contains all the basic blocks for the currently
66 /// incorporated function. Their reverse mapping is stored in ValueMap.
67 std::vector<const BasicBlock*> BasicBlocks;
69 /// When a function is incorporated, this is the size of the Values list
70 /// before incorporation.
71 unsigned NumModuleValues;
72 unsigned FirstFuncConstantID;
75 ValueEnumerator(const ValueEnumerator &); // DO NOT IMPLEMENT
76 void operator=(const ValueEnumerator &); // DO NOT IMPLEMENT
78 ValueEnumerator(const Module *M);
80 unsigned getValueID(const Value *V) const;
82 unsigned getTypeID(const Type *T) const {
83 TypeMapType::const_iterator I = TypeMap.find(T);
84 assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
88 unsigned getInstructionID(const Instruction *I) const;
89 void setInstructionID(const Instruction *I);
91 unsigned getAttributeID(const AttrListPtr &PAL) const {
92 if (PAL.isEmpty()) return 0; // Null maps to zero.
93 AttributeMapType::const_iterator I = AttributeMap.find(PAL.getRawPointer());
94 assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
98 /// getFunctionConstantRange - Return the range of values that corresponds to
99 /// function-local constants.
100 void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
101 Start = FirstFuncConstantID;
105 const ValueList &getValues() const { return Values; }
106 const ValueList &getMDValues() const { return MDValues; }
107 const TypeList &getTypes() const { return Types; }
108 const std::vector<const BasicBlock*> &getBasicBlocks() const {
111 const std::vector<AttrListPtr> &getAttributes() const {
115 /// getGlobalBasicBlockID - This returns the function-specific ID for the
116 /// specified basic block. This is relatively expensive information, so it
117 /// should only be used by rare constructs such as address-of-label.
118 unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;
120 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
121 /// use these two methods to get its data into the ValueEnumerator!
123 void incorporateFunction(const Function &F);
124 void purgeFunction();
127 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
129 void EnumerateMetadata(const MetadataBase *MD);
130 void EnumerateValue(const Value *V);
131 void EnumerateType(const Type *T);
132 void EnumerateOperandType(const Value *V);
133 void EnumerateAttributes(const AttrListPtr &PAL);
135 void EnumerateTypeSymbolTable(const TypeSymbolTable &ST);
136 void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
137 void EnumerateMDSymbolTable(const MDSymbolTable &ST);
140 } // End llvm namespace