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