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 LLVM_LIB_BITCODE_WRITER_VALUEENUMERATOR_H
15 #define LLVM_LIB_BITCODE_WRITER_VALUEENUMERATOR_H
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/UniqueVector.h"
20 #include "llvm/IR/Attributes.h"
21 #include "llvm/IR/UseListOrder.h"
36 class ValueSymbolTable;
40 class ValueEnumerator {
42 typedef std::vector<Type*> TypeList;
44 // For each value, we remember its Value* and occurrence frequency.
45 typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
47 UseListOrderStack UseListOrders;
50 typedef DenseMap<Type*, unsigned> TypeMapType;
54 typedef DenseMap<const Value*, unsigned> ValueMapType;
55 ValueMapType ValueMap;
58 typedef UniqueVector<const Comdat *> ComdatSetType;
59 ComdatSetType Comdats;
62 SmallVector<const MDNode *, 8> FunctionLocalMDs;
63 ValueMapType MDValueMap;
65 typedef DenseMap<AttributeSet, unsigned> AttributeGroupMapType;
66 AttributeGroupMapType AttributeGroupMap;
67 std::vector<AttributeSet> AttributeGroups;
69 typedef DenseMap<AttributeSet, unsigned> AttributeMapType;
70 AttributeMapType AttributeMap;
71 std::vector<AttributeSet> Attribute;
73 /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
74 /// the "getGlobalBasicBlockID" method.
75 mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs;
77 typedef DenseMap<const Instruction*, unsigned> InstructionMapType;
78 InstructionMapType InstructionMap;
79 unsigned InstructionCount;
81 /// BasicBlocks - This contains all the basic blocks for the currently
82 /// incorporated function. Their reverse mapping is stored in ValueMap.
83 std::vector<const BasicBlock*> BasicBlocks;
85 /// When a function is incorporated, this is the size of the Values list
86 /// before incorporation.
87 unsigned NumModuleValues;
89 /// When a function is incorporated, this is the size of the MDValues list
90 /// before incorporation.
91 unsigned NumModuleMDValues;
93 unsigned FirstFuncConstantID;
96 ValueEnumerator(const ValueEnumerator &) LLVM_DELETED_FUNCTION;
97 void operator=(const ValueEnumerator &) LLVM_DELETED_FUNCTION;
99 ValueEnumerator(const Module *M);
102 void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const;
104 unsigned getValueID(const Value *V) const;
106 unsigned getTypeID(Type *T) const {
107 TypeMapType::const_iterator I = TypeMap.find(T);
108 assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
112 unsigned getInstructionID(const Instruction *I) const;
113 void setInstructionID(const Instruction *I);
115 unsigned getAttributeID(AttributeSet PAL) const {
116 if (PAL.isEmpty()) return 0; // Null maps to zero.
117 AttributeMapType::const_iterator I = AttributeMap.find(PAL);
118 assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
122 unsigned getAttributeGroupID(AttributeSet PAL) const {
123 if (PAL.isEmpty()) return 0; // Null maps to zero.
124 AttributeGroupMapType::const_iterator I = AttributeGroupMap.find(PAL);
125 assert(I != AttributeGroupMap.end() && "Attribute not in ValueEnumerator!");
129 /// getFunctionConstantRange - Return the range of values that corresponds to
130 /// function-local constants.
131 void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
132 Start = FirstFuncConstantID;
136 const ValueList &getValues() const { return Values; }
137 const ValueList &getMDValues() const { return MDValues; }
138 const SmallVectorImpl<const MDNode *> &getFunctionLocalMDValues() const {
139 return FunctionLocalMDs;
141 const TypeList &getTypes() const { return Types; }
142 const std::vector<const BasicBlock*> &getBasicBlocks() const {
145 const std::vector<AttributeSet> &getAttributes() const {
148 const std::vector<AttributeSet> &getAttributeGroups() const {
149 return AttributeGroups;
152 const ComdatSetType &getComdats() const { return Comdats; }
153 unsigned getComdatID(const Comdat *C) const;
155 /// getGlobalBasicBlockID - This returns the function-specific ID for the
156 /// specified basic block. This is relatively expensive information, so it
157 /// should only be used by rare constructs such as address-of-label.
158 unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;
160 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
161 /// use these two methods to get its data into the ValueEnumerator!
163 void incorporateFunction(const Function &F);
164 void purgeFunction();
167 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
169 void EnumerateMDNodeOperands(const MDNode *N);
170 void EnumerateMetadata(const Value *MD);
171 void EnumerateFunctionLocalMetadata(const MDNode *N);
172 void EnumerateNamedMDNode(const NamedMDNode *NMD);
173 void EnumerateValue(const Value *V);
174 void EnumerateType(Type *T);
175 void EnumerateOperandType(const Value *V);
176 void EnumerateAttributes(AttributeSet PAL);
178 void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
179 void EnumerateNamedMetadata(const Module *M);
182 } // End llvm namespace