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"
34 class LocalAsMetadata;
38 class ValueSymbolTable;
42 class ValueEnumerator {
44 typedef std::vector<Type*> TypeList;
46 // For each value, we remember its Value* and occurrence frequency.
47 typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
49 UseListOrderStack UseListOrders;
52 typedef DenseMap<Type*, unsigned> TypeMapType;
56 typedef DenseMap<const Value*, unsigned> ValueMapType;
57 ValueMapType ValueMap;
60 typedef UniqueVector<const Comdat *> ComdatSetType;
61 ComdatSetType Comdats;
63 std::vector<const Metadata *> MDs;
64 SmallVector<const LocalAsMetadata *, 8> FunctionLocalMDs;
65 typedef DenseMap<const Metadata *, unsigned> MetadataMapType;
66 MetadataMapType MDValueMap;
69 typedef DenseMap<AttributeSet, unsigned> AttributeGroupMapType;
70 AttributeGroupMapType AttributeGroupMap;
71 std::vector<AttributeSet> AttributeGroups;
73 typedef DenseMap<AttributeSet, unsigned> AttributeMapType;
74 AttributeMapType AttributeMap;
75 std::vector<AttributeSet> Attribute;
77 /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
78 /// the "getGlobalBasicBlockID" method.
79 mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs;
81 typedef DenseMap<const Instruction*, unsigned> InstructionMapType;
82 InstructionMapType InstructionMap;
83 unsigned InstructionCount;
85 /// BasicBlocks - This contains all the basic blocks for the currently
86 /// incorporated function. Their reverse mapping is stored in ValueMap.
87 std::vector<const BasicBlock*> BasicBlocks;
89 /// When a function is incorporated, this is the size of the Values list
90 /// before incorporation.
91 unsigned NumModuleValues;
93 /// When a function is incorporated, this is the size of the MDValues list
94 /// before incorporation.
95 unsigned NumModuleMDs;
97 unsigned FirstFuncConstantID;
100 ValueEnumerator(const ValueEnumerator &) LLVM_DELETED_FUNCTION;
101 void operator=(const ValueEnumerator &) LLVM_DELETED_FUNCTION;
103 ValueEnumerator(const Module &M);
106 void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const;
107 void print(raw_ostream &OS, const MetadataMapType &Map,
108 const char *Name) const;
110 unsigned getValueID(const Value *V) const;
111 unsigned getMetadataID(const Metadata *V) const;
113 bool hasMDString() const { return HasMDString; }
115 unsigned getTypeID(Type *T) const {
116 TypeMapType::const_iterator I = TypeMap.find(T);
117 assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
121 unsigned getInstructionID(const Instruction *I) const;
122 void setInstructionID(const Instruction *I);
124 unsigned getAttributeID(AttributeSet PAL) const {
125 if (PAL.isEmpty()) return 0; // Null maps to zero.
126 AttributeMapType::const_iterator I = AttributeMap.find(PAL);
127 assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
131 unsigned getAttributeGroupID(AttributeSet PAL) const {
132 if (PAL.isEmpty()) return 0; // Null maps to zero.
133 AttributeGroupMapType::const_iterator I = AttributeGroupMap.find(PAL);
134 assert(I != AttributeGroupMap.end() && "Attribute not in ValueEnumerator!");
138 /// getFunctionConstantRange - Return the range of values that corresponds to
139 /// function-local constants.
140 void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
141 Start = FirstFuncConstantID;
145 const ValueList &getValues() const { return Values; }
146 const std::vector<const Metadata *> &getMDs() const { return MDs; }
147 const SmallVectorImpl<const LocalAsMetadata *> &getFunctionLocalMDs() const {
148 return FunctionLocalMDs;
150 const TypeList &getTypes() const { return Types; }
151 const std::vector<const BasicBlock*> &getBasicBlocks() const {
154 const std::vector<AttributeSet> &getAttributes() const {
157 const std::vector<AttributeSet> &getAttributeGroups() const {
158 return AttributeGroups;
161 const ComdatSetType &getComdats() const { return Comdats; }
162 unsigned getComdatID(const Comdat *C) const;
164 /// getGlobalBasicBlockID - This returns the function-specific ID for the
165 /// specified basic block. This is relatively expensive information, so it
166 /// should only be used by rare constructs such as address-of-label.
167 unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;
169 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
170 /// use these two methods to get its data into the ValueEnumerator!
172 void incorporateFunction(const Function &F);
173 void purgeFunction();
176 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
178 void EnumerateMDNodeOperands(const MDNode *N);
179 void EnumerateMetadata(const Metadata *MD);
180 void EnumerateFunctionLocalMetadata(const LocalAsMetadata *Local);
181 void EnumerateNamedMDNode(const NamedMDNode *NMD);
182 void EnumerateValue(const Value *V);
183 void EnumerateType(Type *T);
184 void EnumerateOperandType(const Value *V);
185 void EnumerateAttributes(AttributeSet PAL);
187 void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
188 void EnumerateNamedMetadata(const Module &M);
191 } // End llvm namespace