xref: /freebsd/contrib/llvm-project/llvm/lib/Bitcode/Writer/ValueEnumerator.h (revision 6be3386466ab79a84b48429ae66244f21526d3df)
1 //===- Bitcode/Writer/ValueEnumerator.h - Number values ---------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This class gives values and types Unique ID's.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_LIB_BITCODE_WRITER_VALUEENUMERATOR_H
14 #define LLVM_LIB_BITCODE_WRITER_VALUEENUMERATOR_H
15 
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/UniqueVector.h"
19 #include "llvm/IR/Attributes.h"
20 #include "llvm/IR/UseListOrder.h"
21 #include <cassert>
22 #include <cstdint>
23 #include <utility>
24 #include <vector>
25 
26 namespace llvm {
27 
28 class BasicBlock;
29 class Comdat;
30 class Function;
31 class Instruction;
32 class LocalAsMetadata;
33 class MDNode;
34 class Metadata;
35 class Module;
36 class NamedMDNode;
37 class raw_ostream;
38 class Type;
39 class Value;
40 class ValueSymbolTable;
41 
42 class ValueEnumerator {
43 public:
44   using TypeList = std::vector<Type *>;
45 
46   // For each value, we remember its Value* and occurrence frequency.
47   using ValueList = std::vector<std::pair<const Value *, unsigned>>;
48 
49   /// Attribute groups as encoded in bitcode are almost AttributeSets, but they
50   /// include the AttributeList index, so we have to track that in our map.
51   using IndexAndAttrSet = std::pair<unsigned, AttributeSet>;
52 
53   UseListOrderStack UseListOrders;
54 
55 private:
56   using TypeMapType = DenseMap<Type *, unsigned>;
57   TypeMapType TypeMap;
58   TypeList Types;
59 
60   using ValueMapType = DenseMap<const Value *, unsigned>;
61   ValueMapType ValueMap;
62   ValueList Values;
63 
64   using ComdatSetType = UniqueVector<const Comdat *>;
65   ComdatSetType Comdats;
66 
67   std::vector<const Metadata *> MDs;
68   std::vector<const Metadata *> FunctionMDs;
69 
70   /// Index of information about a piece of metadata.
71   struct MDIndex {
72     unsigned F = 0;  ///< The ID of the function for this metadata, if any.
73     unsigned ID = 0; ///< The implicit ID of this metadata in bitcode.
74 
75     MDIndex() = default;
76     explicit MDIndex(unsigned F) : F(F) {}
77 
78     /// Check if this has a function tag, and it's different from NewF.
79     bool hasDifferentFunction(unsigned NewF) const { return F && F != NewF; }
80 
81     /// Fetch the MD this references out of the given metadata array.
82     const Metadata *get(ArrayRef<const Metadata *> MDs) const {
83       assert(ID && "Expected non-zero ID");
84       assert(ID <= MDs.size() && "Expected valid ID");
85       return MDs[ID - 1];
86     }
87   };
88 
89   using MetadataMapType = DenseMap<const Metadata *, MDIndex>;
90   MetadataMapType MetadataMap;
91 
92   /// Range of metadata IDs, as a half-open range.
93   struct MDRange {
94     unsigned First = 0;
95     unsigned Last = 0;
96 
97     /// Number of strings in the prefix of the metadata range.
98     unsigned NumStrings = 0;
99 
100     MDRange() = default;
101     explicit MDRange(unsigned First) : First(First) {}
102   };
103   SmallDenseMap<unsigned, MDRange, 1> FunctionMDInfo;
104 
105   bool ShouldPreserveUseListOrder;
106 
107   using AttributeGroupMapType = DenseMap<IndexAndAttrSet, unsigned>;
108   AttributeGroupMapType AttributeGroupMap;
109   std::vector<IndexAndAttrSet> AttributeGroups;
110 
111   using AttributeListMapType = DenseMap<AttributeList, unsigned>;
112   AttributeListMapType AttributeListMap;
113   std::vector<AttributeList> AttributeLists;
114 
115   /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
116   /// the "getGlobalBasicBlockID" method.
117   mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs;
118 
119   using InstructionMapType = DenseMap<const Instruction *, unsigned>;
120   InstructionMapType InstructionMap;
121   unsigned InstructionCount;
122 
123   /// BasicBlocks - This contains all the basic blocks for the currently
124   /// incorporated function.  Their reverse mapping is stored in ValueMap.
125   std::vector<const BasicBlock*> BasicBlocks;
126 
127   /// When a function is incorporated, this is the size of the Values list
128   /// before incorporation.
129   unsigned NumModuleValues;
130 
131   /// When a function is incorporated, this is the size of the Metadatas list
132   /// before incorporation.
133   unsigned NumModuleMDs = 0;
134   unsigned NumMDStrings = 0;
135 
136   unsigned FirstFuncConstantID;
137   unsigned FirstInstID;
138 
139 public:
140   ValueEnumerator(const Module &M, bool ShouldPreserveUseListOrder);
141   ValueEnumerator(const ValueEnumerator &) = delete;
142   ValueEnumerator &operator=(const ValueEnumerator &) = delete;
143 
144   void dump() const;
145   void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const;
146   void print(raw_ostream &OS, const MetadataMapType &Map,
147              const char *Name) const;
148 
149   unsigned getValueID(const Value *V) const;
150 
151   unsigned getMetadataID(const Metadata *MD) const {
152     auto ID = getMetadataOrNullID(MD);
153     assert(ID != 0 && "Metadata not in slotcalculator!");
154     return ID - 1;
155   }
156 
157   unsigned getMetadataOrNullID(const Metadata *MD) const {
158     return MetadataMap.lookup(MD).ID;
159   }
160 
161   unsigned numMDs() const { return MDs.size(); }
162 
163   bool shouldPreserveUseListOrder() const { return ShouldPreserveUseListOrder; }
164 
165   unsigned getTypeID(Type *T) const {
166     TypeMapType::const_iterator I = TypeMap.find(T);
167     assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
168     return I->second-1;
169   }
170 
171   unsigned getInstructionID(const Instruction *I) const;
172   void setInstructionID(const Instruction *I);
173 
174   unsigned getAttributeListID(AttributeList PAL) const {
175     if (PAL.isEmpty()) return 0;  // Null maps to zero.
176     AttributeListMapType::const_iterator I = AttributeListMap.find(PAL);
177     assert(I != AttributeListMap.end() && "Attribute not in ValueEnumerator!");
178     return I->second;
179   }
180 
181   unsigned getAttributeGroupID(IndexAndAttrSet Group) const {
182     if (!Group.second.hasAttributes())
183       return 0; // Null maps to zero.
184     AttributeGroupMapType::const_iterator I = AttributeGroupMap.find(Group);
185     assert(I != AttributeGroupMap.end() && "Attribute not in ValueEnumerator!");
186     return I->second;
187   }
188 
189   /// getFunctionConstantRange - Return the range of values that corresponds to
190   /// function-local constants.
191   void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
192     Start = FirstFuncConstantID;
193     End = FirstInstID;
194   }
195 
196   const ValueList &getValues() const { return Values; }
197 
198   /// Check whether the current block has any metadata to emit.
199   bool hasMDs() const { return NumModuleMDs < MDs.size(); }
200 
201   /// Get the MDString metadata for this block.
202   ArrayRef<const Metadata *> getMDStrings() const {
203     return makeArrayRef(MDs).slice(NumModuleMDs, NumMDStrings);
204   }
205 
206   /// Get the non-MDString metadata for this block.
207   ArrayRef<const Metadata *> getNonMDStrings() const {
208     return makeArrayRef(MDs).slice(NumModuleMDs).slice(NumMDStrings);
209   }
210 
211   const TypeList &getTypes() const { return Types; }
212 
213   const std::vector<const BasicBlock*> &getBasicBlocks() const {
214     return BasicBlocks;
215   }
216 
217   const std::vector<AttributeList> &getAttributeLists() const { return AttributeLists; }
218 
219   const std::vector<IndexAndAttrSet> &getAttributeGroups() const {
220     return AttributeGroups;
221   }
222 
223   const ComdatSetType &getComdats() const { return Comdats; }
224   unsigned getComdatID(const Comdat *C) const;
225 
226   /// getGlobalBasicBlockID - This returns the function-specific ID for the
227   /// specified basic block.  This is relatively expensive information, so it
228   /// should only be used by rare constructs such as address-of-label.
229   unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;
230 
231   /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
232   /// use these two methods to get its data into the ValueEnumerator!
233   void incorporateFunction(const Function &F);
234 
235   void purgeFunction();
236   uint64_t computeBitsRequiredForTypeIndicies() const;
237 
238 private:
239   void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
240 
241   /// Reorder the reachable metadata.
242   ///
243   /// This is not just an optimization, but is mandatory for emitting MDString
244   /// correctly.
245   void organizeMetadata();
246 
247   /// Drop the function tag from the transitive operands of the given node.
248   void dropFunctionFromMetadata(MetadataMapType::value_type &FirstMD);
249 
250   /// Incorporate the function metadata.
251   ///
252   /// This should be called before enumerating LocalAsMetadata for the
253   /// function.
254   void incorporateFunctionMetadata(const Function &F);
255 
256   /// Enumerate a single instance of metadata with the given function tag.
257   ///
258   /// If \c MD has already been enumerated, check that \c F matches its
259   /// function tag.  If not, call \a dropFunctionFromMetadata().
260   ///
261   /// Otherwise, mark \c MD as visited.  Assign it an ID, or just return it if
262   /// it's an \a MDNode.
263   const MDNode *enumerateMetadataImpl(unsigned F, const Metadata *MD);
264 
265   unsigned getMetadataFunctionID(const Function *F) const;
266 
267   /// Enumerate reachable metadata in (almost) post-order.
268   ///
269   /// Enumerate all the metadata reachable from MD.  We want to minimize the
270   /// cost of reading bitcode records, and so the primary consideration is that
271   /// operands of uniqued nodes are resolved before the nodes are read.  This
272   /// avoids re-uniquing them on the context and factors away RAUW support.
273   ///
274   /// This algorithm guarantees that subgraphs of uniqued nodes are in
275   /// post-order.  Distinct subgraphs reachable only from a single uniqued node
276   /// will be in post-order.
277   ///
278   /// \note The relative order of a distinct and uniqued node is irrelevant.
279   /// \a organizeMetadata() will later partition distinct nodes ahead of
280   /// uniqued ones.
281   ///{
282   void EnumerateMetadata(const Function *F, const Metadata *MD);
283   void EnumerateMetadata(unsigned F, const Metadata *MD);
284   ///}
285 
286   void EnumerateFunctionLocalMetadata(const Function &F,
287                                       const LocalAsMetadata *Local);
288   void EnumerateFunctionLocalMetadata(unsigned F, const LocalAsMetadata *Local);
289   void EnumerateNamedMDNode(const NamedMDNode *NMD);
290   void EnumerateValue(const Value *V);
291   void EnumerateType(Type *T);
292   void EnumerateOperandType(const Value *V);
293   void EnumerateAttributes(AttributeList PAL);
294 
295   void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
296   void EnumerateNamedMetadata(const Module &M);
297 };
298 
299 } // end namespace llvm
300 
301 #endif // LLVM_LIB_BITCODE_WRITER_VALUEENUMERATOR_H
302