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 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<void*, unsigned> AttributeMapType;
56 AttributeMapType AttributeMap;
57 std::vector<AttrListPtr> Attributes;
59 /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
60 /// the "getGlobalBasicBlockID" method.
61 mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs;
63 typedef DenseMap<const Instruction*, unsigned> InstructionMapType;
64 InstructionMapType InstructionMap;
65 unsigned InstructionCount;
67 /// BasicBlocks - This contains all the basic blocks for the currently
68 /// incorporated function. Their reverse mapping is stored in ValueMap.
69 std::vector<const BasicBlock*> BasicBlocks;
71 /// When a function is incorporated, this is the size of the Values list
72 /// before incorporation.
73 unsigned NumModuleValues;
75 /// When a function is incorporated, this is the size of the MDValues list
76 /// before incorporation.
77 unsigned NumModuleMDValues;
79 unsigned FirstFuncConstantID;
82 ValueEnumerator(const ValueEnumerator &) LLVM_DELETED_FUNCTION;
83 void operator=(const ValueEnumerator &) LLVM_DELETED_FUNCTION;
85 ValueEnumerator(const Module *M);
88 void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const;
90 unsigned getValueID(const Value *V) const;
92 unsigned getTypeID(Type *T) const {
93 TypeMapType::const_iterator I = TypeMap.find(T);
94 assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
98 unsigned getInstructionID(const Instruction *I) const;
99 void setInstructionID(const Instruction *I);
101 unsigned getAttributeID(const AttrListPtr &PAL) const {
102 if (PAL.isEmpty()) return 0; // Null maps to zero.
103 AttributeMapType::const_iterator I = AttributeMap.find(PAL.getRawPointer());
104 assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
108 /// getFunctionConstantRange - Return the range of values that corresponds to
109 /// function-local constants.
110 void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
111 Start = FirstFuncConstantID;
115 const ValueList &getValues() const { return Values; }
116 const ValueList &getMDValues() const { return MDValues; }
117 const SmallVector<const MDNode *, 8> &getFunctionLocalMDValues() const {
118 return FunctionLocalMDs;
120 const TypeList &getTypes() const { return Types; }
121 const std::vector<const BasicBlock*> &getBasicBlocks() const {
124 const std::vector<AttrListPtr> &getAttributes() const {
128 /// getGlobalBasicBlockID - This returns the function-specific ID for the
129 /// specified basic block. This is relatively expensive information, so it
130 /// should only be used by rare constructs such as address-of-label.
131 unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;
133 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
134 /// use these two methods to get its data into the ValueEnumerator!
136 void incorporateFunction(const Function &F);
137 void purgeFunction();
140 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
142 void EnumerateMDNodeOperands(const MDNode *N);
143 void EnumerateMetadata(const Value *MD);
144 void EnumerateFunctionLocalMetadata(const MDNode *N);
145 void EnumerateNamedMDNode(const NamedMDNode *NMD);
146 void EnumerateValue(const Value *V);
147 void EnumerateType(Type *T);
148 void EnumerateOperandType(const Value *V);
149 void EnumerateAttributes(const AttrListPtr &PAL);
151 void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
152 void EnumerateNamedMetadata(const Module *M);
155 } // End llvm namespace