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
;
36 class ValueEnumerator
{
38 typedef std::vector
<const Type
*> 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 SmallVector
<const MDNode
*, 8> FunctionLocalMDs
;
52 ValueMapType MDValueMap
;
54 typedef DenseMap
<void*, unsigned> AttributeMapType
;
55 AttributeMapType AttributeMap
;
56 std::vector
<AttrListPtr
> Attributes
;
58 /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
59 /// the "getGlobalBasicBlockID" method.
60 mutable DenseMap
<const BasicBlock
*, unsigned> GlobalBasicBlockIDs
;
62 typedef DenseMap
<const Instruction
*, unsigned> InstructionMapType
;
63 InstructionMapType InstructionMap
;
64 unsigned InstructionCount
;
66 /// BasicBlocks - This contains all the basic blocks for the currently
67 /// incorporated function. Their reverse mapping is stored in ValueMap.
68 std::vector
<const BasicBlock
*> BasicBlocks
;
70 /// When a function is incorporated, this is the size of the Values list
71 /// before incorporation.
72 unsigned NumModuleValues
;
74 /// When a function is incorporated, this is the size of the MDValues list
75 /// before incorporation.
76 unsigned NumModuleMDValues
;
78 unsigned FirstFuncConstantID
;
81 ValueEnumerator(const ValueEnumerator
&); // DO NOT IMPLEMENT
82 void operator=(const ValueEnumerator
&); // DO NOT IMPLEMENT
84 ValueEnumerator(const Module
*M
);
86 unsigned getValueID(const Value
*V
) const;
88 unsigned getTypeID(const Type
*T
) const {
89 TypeMapType::const_iterator I
= TypeMap
.find(T
);
90 assert(I
!= TypeMap
.end() && "Type not in ValueEnumerator!");
94 unsigned getInstructionID(const Instruction
*I
) const;
95 void setInstructionID(const Instruction
*I
);
97 unsigned getAttributeID(const AttrListPtr
&PAL
) const {
98 if (PAL
.isEmpty()) return 0; // Null maps to zero.
99 AttributeMapType::const_iterator I
= AttributeMap
.find(PAL
.getRawPointer());
100 assert(I
!= AttributeMap
.end() && "Attribute not in ValueEnumerator!");
104 /// getFunctionConstantRange - Return the range of values that corresponds to
105 /// function-local constants.
106 void getFunctionConstantRange(unsigned &Start
, unsigned &End
) const {
107 Start
= FirstFuncConstantID
;
111 const ValueList
&getValues() const { return Values
; }
112 const ValueList
&getMDValues() const { return MDValues
; }
113 const SmallVector
<const MDNode
*, 8> &getFunctionLocalMDValues() const {
114 return FunctionLocalMDs
;
116 const TypeList
&getTypes() const { return Types
; }
117 const std::vector
<const BasicBlock
*> &getBasicBlocks() const {
120 const std::vector
<AttrListPtr
> &getAttributes() const {
124 /// getGlobalBasicBlockID - This returns the function-specific ID for the
125 /// specified basic block. This is relatively expensive information, so it
126 /// should only be used by rare constructs such as address-of-label.
127 unsigned getGlobalBasicBlockID(const BasicBlock
*BB
) const;
129 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
130 /// use these two methods to get its data into the ValueEnumerator!
132 void incorporateFunction(const Function
&F
);
133 void purgeFunction();
136 void OptimizeConstants(unsigned CstStart
, unsigned CstEnd
);
138 void EnumerateMDNodeOperands(const MDNode
*N
);
139 void EnumerateMetadata(const Value
*MD
);
140 void EnumerateFunctionLocalMetadata(const MDNode
*N
);
141 void EnumerateNamedMDNode(const NamedMDNode
*NMD
);
142 void EnumerateValue(const Value
*V
);
143 void EnumerateType(const Type
*T
);
144 void EnumerateOperandType(const Value
*V
);
145 void EnumerateAttributes(const AttrListPtr
&PAL
);
147 void EnumerateValueSymbolTable(const ValueSymbolTable
&ST
);
148 void EnumerateNamedMetadata(const Module
*M
);
151 } // End llvm namespace