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/ArrayRef.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/UniqueVector.h"
20 #include "llvm/IR/Attributes.h"
21 #include "llvm/IR/Metadata.h"
22 #include "llvm/IR/Type.h"
23 #include "llvm/IR/UseListOrder.h"
35 class LocalAsMetadata;
43 class ValueSymbolTable;
45 class ValueEnumerator {
47 using TypeList = std::vector<Type *>;
49 // For each value, we remember its Value* and occurrence frequency.
50 using ValueList = std::vector<std::pair<const Value *, unsigned>>;
52 /// Attribute groups as encoded in bitcode are almost AttributeSets, but they
53 /// include the AttributeList index, so we have to track that in our map.
54 using IndexAndAttrSet = std::pair<unsigned, AttributeSet>;
56 UseListOrderStack UseListOrders;
59 using TypeMapType = DenseMap<Type *, unsigned>;
63 using ValueMapType = DenseMap<const Value *, unsigned>;
64 ValueMapType ValueMap;
67 using ComdatSetType = UniqueVector<const Comdat *>;
68 ComdatSetType Comdats;
70 std::vector<const Metadata *> MDs;
71 std::vector<const Metadata *> FunctionMDs;
73 /// Index of information about a piece of metadata.
75 unsigned F = 0; ///< The ID of the function for this metadata, if any.
76 unsigned ID = 0; ///< The implicit ID of this metadata in bitcode.
79 explicit MDIndex(unsigned F) : F(F) {}
81 /// Check if this has a function tag, and it's different from NewF.
82 bool hasDifferentFunction(unsigned NewF) const { return F && F != NewF; }
84 /// Fetch the MD this references out of the given metadata array.
85 const Metadata *get(ArrayRef<const Metadata *> MDs) const {
86 assert(ID && "Expected non-zero ID");
87 assert(ID <= MDs.size() && "Expected valid ID");
92 using MetadataMapType = DenseMap<const Metadata *, MDIndex>;
93 MetadataMapType MetadataMap;
95 /// Range of metadata IDs, as a half-open range.
100 /// Number of strings in the prefix of the metadata range.
101 unsigned NumStrings = 0;
104 explicit MDRange(unsigned First) : First(First) {}
106 SmallDenseMap<unsigned, MDRange, 1> FunctionMDInfo;
108 bool ShouldPreserveUseListOrder;
110 using AttributeGroupMapType = DenseMap<IndexAndAttrSet, unsigned>;
111 AttributeGroupMapType AttributeGroupMap;
112 std::vector<IndexAndAttrSet> AttributeGroups;
114 using AttributeListMapType = DenseMap<AttributeList, unsigned>;
115 AttributeListMapType AttributeListMap;
116 std::vector<AttributeList> AttributeLists;
118 /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
119 /// the "getGlobalBasicBlockID" method.
120 mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs;
122 using InstructionMapType = DenseMap<const Instruction *, unsigned>;
123 InstructionMapType InstructionMap;
124 unsigned InstructionCount;
126 /// BasicBlocks - This contains all the basic blocks for the currently
127 /// incorporated function. Their reverse mapping is stored in ValueMap.
128 std::vector<const BasicBlock*> BasicBlocks;
130 /// When a function is incorporated, this is the size of the Values list
131 /// before incorporation.
132 unsigned NumModuleValues;
134 /// When a function is incorporated, this is the size of the Metadatas list
135 /// before incorporation.
136 unsigned NumModuleMDs = 0;
137 unsigned NumMDStrings = 0;
139 unsigned FirstFuncConstantID;
140 unsigned FirstInstID;
143 ValueEnumerator(const Module &M, bool ShouldPreserveUseListOrder);
144 ValueEnumerator(const ValueEnumerator &) = delete;
145 ValueEnumerator &operator=(const ValueEnumerator &) = delete;
148 void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const;
149 void print(raw_ostream &OS, const MetadataMapType &Map,
150 const char *Name) const;
152 unsigned getValueID(const Value *V) const;
154 unsigned getMetadataID(const Metadata *MD) const {
155 auto ID = getMetadataOrNullID(MD);
156 assert(ID != 0 && "Metadata not in slotcalculator!");
160 unsigned getMetadataOrNullID(const Metadata *MD) const {
161 return MetadataMap.lookup(MD).ID;
164 unsigned numMDs() const { return MDs.size(); }
166 bool shouldPreserveUseListOrder() const { return ShouldPreserveUseListOrder; }
168 unsigned getTypeID(Type *T) const {
169 TypeMapType::const_iterator I = TypeMap.find(T);
170 assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
174 unsigned getInstructionID(const Instruction *I) const;
175 void setInstructionID(const Instruction *I);
177 unsigned getAttributeListID(AttributeList PAL) const {
178 if (PAL.isEmpty()) return 0; // Null maps to zero.
179 AttributeListMapType::const_iterator I = AttributeListMap.find(PAL);
180 assert(I != AttributeListMap.end() && "Attribute not in ValueEnumerator!");
184 unsigned getAttributeGroupID(IndexAndAttrSet Group) const {
185 if (!Group.second.hasAttributes())
186 return 0; // Null maps to zero.
187 AttributeGroupMapType::const_iterator I = AttributeGroupMap.find(Group);
188 assert(I != AttributeGroupMap.end() && "Attribute not in ValueEnumerator!");
192 /// getFunctionConstantRange - Return the range of values that corresponds to
193 /// function-local constants.
194 void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
195 Start = FirstFuncConstantID;
199 const ValueList &getValues() const { return Values; }
201 /// Check whether the current block has any metadata to emit.
202 bool hasMDs() const { return NumModuleMDs < MDs.size(); }
204 /// Get the MDString metadata for this block.
205 ArrayRef<const Metadata *> getMDStrings() const {
206 return makeArrayRef(MDs).slice(NumModuleMDs, NumMDStrings);
209 /// Get the non-MDString metadata for this block.
210 ArrayRef<const Metadata *> getNonMDStrings() const {
211 return makeArrayRef(MDs).slice(NumModuleMDs).slice(NumMDStrings);
214 const TypeList &getTypes() const { return Types; }
216 const std::vector<const BasicBlock*> &getBasicBlocks() const {
220 const std::vector<AttributeList> &getAttributeLists() const { return AttributeLists; }
222 const std::vector<IndexAndAttrSet> &getAttributeGroups() const {
223 return AttributeGroups;
226 const ComdatSetType &getComdats() const { return Comdats; }
227 unsigned getComdatID(const Comdat *C) const;
229 /// getGlobalBasicBlockID - This returns the function-specific ID for the
230 /// specified basic block. This is relatively expensive information, so it
231 /// should only be used by rare constructs such as address-of-label.
232 unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;
234 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
235 /// use these two methods to get its data into the ValueEnumerator!
236 void incorporateFunction(const Function &F);
238 void purgeFunction();
239 uint64_t computeBitsRequiredForTypeIndicies() const;
242 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
244 /// Reorder the reachable metadata.
246 /// This is not just an optimization, but is mandatory for emitting MDString
248 void organizeMetadata();
250 /// Drop the function tag from the transitive operands of the given node.
251 void dropFunctionFromMetadata(MetadataMapType::value_type &FirstMD);
253 /// Incorporate the function metadata.
255 /// This should be called before enumerating LocalAsMetadata for the
257 void incorporateFunctionMetadata(const Function &F);
259 /// Enumerate a single instance of metadata with the given function tag.
261 /// If \c MD has already been enumerated, check that \c F matches its
262 /// function tag. If not, call \a dropFunctionFromMetadata().
264 /// Otherwise, mark \c MD as visited. Assign it an ID, or just return it if
265 /// it's an \a MDNode.
266 const MDNode *enumerateMetadataImpl(unsigned F, const Metadata *MD);
268 unsigned getMetadataFunctionID(const Function *F) const;
270 /// Enumerate reachable metadata in (almost) post-order.
272 /// Enumerate all the metadata reachable from MD. We want to minimize the
273 /// cost of reading bitcode records, and so the primary consideration is that
274 /// operands of uniqued nodes are resolved before the nodes are read. This
275 /// avoids re-uniquing them on the context and factors away RAUW support.
277 /// This algorithm guarantees that subgraphs of uniqued nodes are in
278 /// post-order. Distinct subgraphs reachable only from a single uniqued node
279 /// will be in post-order.
281 /// \note The relative order of a distinct and uniqued node is irrelevant.
282 /// \a organizeMetadata() will later partition distinct nodes ahead of
285 void EnumerateMetadata(const Function *F, const Metadata *MD);
286 void EnumerateMetadata(unsigned F, const Metadata *MD);
289 void EnumerateFunctionLocalMetadata(const Function &F,
290 const LocalAsMetadata *Local);
291 void EnumerateFunctionLocalMetadata(unsigned F, const LocalAsMetadata *Local);
292 void EnumerateNamedMDNode(const NamedMDNode *NMD);
293 void EnumerateValue(const Value *V);
294 void EnumerateType(Type *T);
295 void EnumerateOperandType(const Value *V);
296 void EnumerateAttributes(AttributeList PAL);
298 void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
299 void EnumerateNamedMetadata(const Module &M);
302 } // end namespace llvm
304 #endif // LLVM_LIB_BITCODE_WRITER_VALUEENUMERATOR_H