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;
34 class ValueEnumerator {
36 // For each type, we remember its Type* and occurrence frequency.
37 typedef std::vector<std::pair<const Type*, unsigned> > TypeList;
39 // For each value, we remember its Value* and occurrence frequency.
40 typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
42 typedef DenseMap<const Type*, unsigned> TypeMapType;
46 typedef DenseMap<const Value*, unsigned> ValueMapType;
47 ValueMapType ValueMap;
50 ValueMapType MDValueMap;
52 typedef DenseMap<void*, unsigned> AttributeMapType;
53 AttributeMapType AttributeMap;
54 std::vector<AttrListPtr> Attributes;
56 /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
57 /// the "getGlobalBasicBlockID" method.
58 mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs;
60 typedef DenseMap<const Instruction*, unsigned> InstructionMapType;
61 InstructionMapType InstructionMap;
62 unsigned InstructionCount;
64 /// BasicBlocks - This contains all the basic blocks for the currently
65 /// incorporated function. Their reverse mapping is stored in ValueMap.
66 std::vector<const BasicBlock*> BasicBlocks;
68 /// When a function is incorporated, this is the size of the Values list
69 /// before incorporation.
70 unsigned NumModuleValues;
71 unsigned FirstFuncConstantID;
74 ValueEnumerator(const ValueEnumerator &); // DO NOT IMPLEMENT
75 void operator=(const ValueEnumerator &); // DO NOT IMPLEMENT
77 ValueEnumerator(const Module *M);
79 unsigned getValueID(const Value *V) const;
81 unsigned getTypeID(const Type *T) const {
82 TypeMapType::const_iterator I = TypeMap.find(T);
83 assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
87 unsigned getInstructionID(const Instruction *I) const;
88 void setInstructionID(const Instruction *I);
90 unsigned getAttributeID(const AttrListPtr &PAL) const {
91 if (PAL.isEmpty()) return 0; // Null maps to zero.
92 AttributeMapType::const_iterator I = AttributeMap.find(PAL.getRawPointer());
93 assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
97 /// getFunctionConstantRange - Return the range of values that corresponds to
98 /// function-local constants.
99 void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
100 Start = FirstFuncConstantID;
104 const ValueList &getValues() const { return Values; }
105 const ValueList &getMDValues() const { return MDValues; }
106 const TypeList &getTypes() const { return Types; }
107 const std::vector<const BasicBlock*> &getBasicBlocks() const {
110 const std::vector<AttrListPtr> &getAttributes() const {
114 /// getGlobalBasicBlockID - This returns the function-specific ID for the
115 /// specified basic block. This is relatively expensive information, so it
116 /// should only be used by rare constructs such as address-of-label.
117 unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;
119 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
120 /// use these two methods to get its data into the ValueEnumerator!
122 void incorporateFunction(const Function &F);
123 void purgeFunction();
126 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
128 void EnumerateMetadata(const MetadataBase *MD);
129 void EnumerateValue(const Value *V);
130 void EnumerateType(const Type *T);
131 void EnumerateOperandType(const Value *V);
132 void EnumerateAttributes(const AttrListPtr &PAL);
134 void EnumerateTypeSymbolTable(const TypeSymbolTable &ST);
135 void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
138 } // End llvm namespace