blob: 730187087dc524539749761dbcd757b4a83b1e38 [file] [log] [blame]
Eugene Zelenko975293f2017-09-07 23:28:24 +00001//===- Bitcode/Writer/ValueEnumerator.h - Number values ---------*- C++ -*-===//
Chris Lattnerc1d10d62007-04-22 06:24:45 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc1d10d62007-04-22 06:24:45 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This class gives values and types Unique ID's.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000014#ifndef LLVM_LIB_BITCODE_WRITER_VALUEENUMERATOR_H
15#define LLVM_LIB_BITCODE_WRITER_VALUEENUMERATOR_H
Chris Lattnerc1d10d62007-04-22 06:24:45 +000016
Eugene Zelenko975293f2017-09-07 23:28:24 +000017#include "llvm/ADT/ArrayRef.h"
Chris Lattnerc1d10d62007-04-22 06:24:45 +000018#include "llvm/ADT/DenseMap.h"
David Majnemerdad0a642014-06-27 18:19:56 +000019#include "llvm/ADT/UniqueVector.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Attributes.h"
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +000021#include "llvm/IR/UseListOrder.h"
Eugene Zelenko975293f2017-09-07 23:28:24 +000022#include <cassert>
23#include <cstdint>
24#include <utility>
Chris Lattnerc1d10d62007-04-22 06:24:45 +000025#include <vector>
26
27namespace llvm {
28
Chris Lattner7c37b012007-04-26 04:42:16 +000029class BasicBlock;
David Majnemerdad0a642014-06-27 18:19:56 +000030class Comdat;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000031class Function;
Eugene Zelenko975293f2017-09-07 23:28:24 +000032class Instruction;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000033class LocalAsMetadata;
Devang Pateldf84e8b2010-06-02 23:05:04 +000034class MDNode;
Eugene Zelenko975293f2017-09-07 23:28:24 +000035class Metadata;
36class Module;
Devang Patel99ff5a82010-01-09 00:30:14 +000037class NamedMDNode;
Chad Rosier78037a92011-12-07 20:44:46 +000038class raw_ostream;
Eugene Zelenko975293f2017-09-07 23:28:24 +000039class Type;
40class Value;
41class ValueSymbolTable;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000042
Benjamin Kramer079b96e2013-09-11 18:05:11 +000043class ValueEnumerator {
Chris Lattnerc1d10d62007-04-22 06:24:45 +000044public:
Eugene Zelenko975293f2017-09-07 23:28:24 +000045 using TypeList = std::vector<Type *>;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000046
47 // For each value, we remember its Value* and occurrence frequency.
Eugene Zelenko975293f2017-09-07 23:28:24 +000048 using ValueList = std::vector<std::pair<const Value *, unsigned>>;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +000049
Reid Klecknerb4a2d182017-04-24 20:38:30 +000050 /// Attribute groups as encoded in bitcode are almost AttributeSets, but they
51 /// include the AttributeList index, so we have to track that in our map.
Eugene Zelenko975293f2017-09-07 23:28:24 +000052 using IndexAndAttrSet = std::pair<unsigned, AttributeSet>;
Reid Klecknerb4a2d182017-04-24 20:38:30 +000053
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +000054 UseListOrderStack UseListOrders;
55
Chris Lattnerc1d10d62007-04-22 06:24:45 +000056private:
Eugene Zelenko975293f2017-09-07 23:28:24 +000057 using TypeMapType = DenseMap<Type *, unsigned>;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000058 TypeMapType TypeMap;
Chris Lattner52523562007-04-24 00:16:04 +000059 TypeList Types;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000060
Eugene Zelenko975293f2017-09-07 23:28:24 +000061 using ValueMapType = DenseMap<const Value *, unsigned>;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000062 ValueMapType ValueMap;
Chris Lattner52523562007-04-24 00:16:04 +000063 ValueList Values;
David Majnemerdad0a642014-06-27 18:19:56 +000064
Eugene Zelenko975293f2017-09-07 23:28:24 +000065 using ComdatSetType = UniqueVector<const Comdat *>;
David Majnemerdad0a642014-06-27 18:19:56 +000066 ComdatSetType Comdats;
67
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000068 std::vector<const Metadata *> MDs;
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +000069 std::vector<const Metadata *> FunctionMDs;
70
71 /// Index of information about a piece of metadata.
72 struct MDIndex {
73 unsigned F = 0; ///< The ID of the function for this metadata, if any.
74 unsigned ID = 0; ///< The implicit ID of this metadata in bitcode.
75
76 MDIndex() = default;
77 explicit MDIndex(unsigned F) : F(F) {}
78
79 /// Check if this has a function tag, and it's different from NewF.
80 bool hasDifferentFunction(unsigned NewF) const { return F && F != NewF; }
81
82 /// Fetch the MD this references out of the given metadata array.
83 const Metadata *get(ArrayRef<const Metadata *> MDs) const {
84 assert(ID && "Expected non-zero ID");
85 assert(ID <= MDs.size() && "Expected valid ID");
86 return MDs[ID - 1];
87 }
88 };
Duncan P. N. Exon Smith9695eb32016-04-19 03:46:51 +000089
Eugene Zelenko975293f2017-09-07 23:28:24 +000090 using MetadataMapType = DenseMap<const Metadata *, MDIndex>;
Teresa Johnson61b406e2015-12-29 23:00:22 +000091 MetadataMapType MetadataMap;
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +000092
93 /// Range of metadata IDs, as a half-open range.
94 struct MDRange {
95 unsigned First = 0;
96 unsigned Last = 0;
97
98 /// Number of strings in the prefix of the metadata range.
99 unsigned NumStrings = 0;
100
Eugene Zelenko975293f2017-09-07 23:28:24 +0000101 MDRange() = default;
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000102 explicit MDRange(unsigned First) : First(First) {}
103 };
104 SmallDenseMap<unsigned, MDRange, 1> FunctionMDInfo;
105
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +0000106 bool ShouldPreserveUseListOrder;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000107
Eugene Zelenko975293f2017-09-07 23:28:24 +0000108 using AttributeGroupMapType = DenseMap<IndexAndAttrSet, unsigned>;
Bill Wendling92ed7002013-02-11 22:33:26 +0000109 AttributeGroupMapType AttributeGroupMap;
Reid Klecknerb4a2d182017-04-24 20:38:30 +0000110 std::vector<IndexAndAttrSet> AttributeGroups;
Bill Wendling51f612e2013-02-10 23:06:02 +0000111
Eugene Zelenko975293f2017-09-07 23:28:24 +0000112 using AttributeListMapType = DenseMap<AttributeList, unsigned>;
Reid Klecknerb4a2d182017-04-24 20:38:30 +0000113 AttributeListMapType AttributeListMap;
114 std::vector<AttributeList> AttributeLists;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000115
Chris Lattnerf540d742009-10-28 05:24:40 +0000116 /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
117 /// the "getGlobalBasicBlockID" method.
118 mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000119
Eugene Zelenko975293f2017-09-07 23:28:24 +0000120 using InstructionMapType = DenseMap<const Instruction *, unsigned>;
Devang Patelaf206b82009-09-18 19:26:43 +0000121 InstructionMapType InstructionMap;
122 unsigned InstructionCount;
123
Chris Lattner7c37b012007-04-26 04:42:16 +0000124 /// BasicBlocks - This contains all the basic blocks for the currently
125 /// incorporated function. Their reverse mapping is stored in ValueMap.
126 std::vector<const BasicBlock*> BasicBlocks;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000127
Chris Lattner5f640b92007-04-26 03:50:57 +0000128 /// When a function is incorporated, this is the size of the Values list
129 /// before incorporation.
Chris Lattnere6e364c2007-04-26 05:53:54 +0000130 unsigned NumModuleValues;
Dan Gohmanc828c542010-08-24 02:24:03 +0000131
Teresa Johnson61b406e2015-12-29 23:00:22 +0000132 /// When a function is incorporated, this is the size of the Metadatas list
Dan Gohmanc828c542010-08-24 02:24:03 +0000133 /// before incorporation.
Duncan P. N. Exon Smith93429112016-04-02 15:09:42 +0000134 unsigned NumModuleMDs = 0;
135 unsigned NumMDStrings = 0;
Dan Gohmanc828c542010-08-24 02:24:03 +0000136
Chris Lattnere6e364c2007-04-26 05:53:54 +0000137 unsigned FirstFuncConstantID;
138 unsigned FirstInstID;
Craig Toppera60c0f12012-09-15 17:09:36 +0000139
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000140public:
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +0000141 ValueEnumerator(const Module &M, bool ShouldPreserveUseListOrder);
Eugene Zelenko975293f2017-09-07 23:28:24 +0000142 ValueEnumerator(const ValueEnumerator &) = delete;
143 ValueEnumerator &operator=(const ValueEnumerator &) = delete;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000144
Chad Rosier78037a92011-12-07 20:44:46 +0000145 void dump() const;
146 void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000147 void print(raw_ostream &OS, const MetadataMapType &Map,
148 const char *Name) const;
Chad Rosier78037a92011-12-07 20:44:46 +0000149
Devang Patel05eb6172009-08-04 06:00:18 +0000150 unsigned getValueID(const Value *V) const;
Eugene Zelenko975293f2017-09-07 23:28:24 +0000151
Duncan P. N. Exon Smith9a6f64e2015-01-20 01:00:23 +0000152 unsigned getMetadataID(const Metadata *MD) const {
153 auto ID = getMetadataOrNullID(MD);
154 assert(ID != 0 && "Metadata not in slotcalculator!");
155 return ID - 1;
156 }
Eugene Zelenko975293f2017-09-07 23:28:24 +0000157
Duncan P. N. Exon Smith9a6f64e2015-01-20 01:00:23 +0000158 unsigned getMetadataOrNullID(const Metadata *MD) const {
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000159 return MetadataMap.lookup(MD).ID;
Duncan P. N. Exon Smith9a6f64e2015-01-20 01:00:23 +0000160 }
Eugene Zelenko975293f2017-09-07 23:28:24 +0000161
Teresa Johnsond4d3dfd2015-11-20 14:51:27 +0000162 unsigned numMDs() const { return MDs.size(); }
Devang Patel05eb6172009-08-04 06:00:18 +0000163
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +0000164 bool shouldPreserveUseListOrder() const { return ShouldPreserveUseListOrder; }
165
Chris Lattner229907c2011-07-18 04:54:35 +0000166 unsigned getTypeID(Type *T) const {
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000167 TypeMapType::const_iterator I = TypeMap.find(T);
168 assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
169 return I->second-1;
170 }
Devang Patelaf206b82009-09-18 19:26:43 +0000171
172 unsigned getInstructionID(const Instruction *I) const;
173 void setInstructionID(const Instruction *I);
174
Reid Klecknerb4a2d182017-04-24 20:38:30 +0000175 unsigned getAttributeListID(AttributeList PAL) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000176 if (PAL.isEmpty()) return 0; // Null maps to zero.
Reid Klecknerb4a2d182017-04-24 20:38:30 +0000177 AttributeListMapType::const_iterator I = AttributeListMap.find(PAL);
178 assert(I != AttributeListMap.end() && "Attribute not in ValueEnumerator!");
Chris Lattnere4bbad62007-05-03 22:46:43 +0000179 return I->second;
180 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000181
Reid Klecknerb4a2d182017-04-24 20:38:30 +0000182 unsigned getAttributeGroupID(IndexAndAttrSet Group) const {
183 if (!Group.second.hasAttributes())
184 return 0; // Null maps to zero.
185 AttributeGroupMapType::const_iterator I = AttributeGroupMap.find(Group);
Bill Wendling92ed7002013-02-11 22:33:26 +0000186 assert(I != AttributeGroupMap.end() && "Attribute not in ValueEnumerator!");
Bill Wendling51f612e2013-02-10 23:06:02 +0000187 return I->second;
188 }
189
Chris Lattnere6e364c2007-04-26 05:53:54 +0000190 /// getFunctionConstantRange - Return the range of values that corresponds to
191 /// function-local constants.
192 void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
193 Start = FirstFuncConstantID;
194 End = FirstInstID;
195 }
Joe Abbey2ad8df22012-11-25 15:23:39 +0000196
Chris Lattner52523562007-04-24 00:16:04 +0000197 const ValueList &getValues() const { return Values; }
Duncan P. N. Exon Smith93429112016-04-02 15:09:42 +0000198
199 /// Check whether the current block has any metadata to emit.
200 bool hasMDs() const { return NumModuleMDs < MDs.size(); }
201
Duncan P. N. Exon Smith0b76b722016-04-02 15:16:56 +0000202 /// Get the MDString metadata for this block.
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000203 ArrayRef<const Metadata *> getMDStrings() const {
Duncan P. N. Exon Smith93429112016-04-02 15:09:42 +0000204 return makeArrayRef(MDs).slice(NumModuleMDs, NumMDStrings);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000205 }
Duncan P. N. Exon Smith93429112016-04-02 15:09:42 +0000206
Duncan P. N. Exon Smith0b76b722016-04-02 15:16:56 +0000207 /// Get the non-MDString metadata for this block.
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000208 ArrayRef<const Metadata *> getNonMDStrings() const {
Duncan P. N. Exon Smith93429112016-04-02 15:09:42 +0000209 return makeArrayRef(MDs).slice(NumModuleMDs).slice(NumMDStrings);
Devang Pateldf84e8b2010-06-02 23:05:04 +0000210 }
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000211
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000212 const TypeList &getTypes() const { return Types; }
Eugene Zelenko975293f2017-09-07 23:28:24 +0000213
Chris Lattner7c37b012007-04-26 04:42:16 +0000214 const std::vector<const BasicBlock*> &getBasicBlocks() const {
Joe Abbey2ad8df22012-11-25 15:23:39 +0000215 return BasicBlocks;
Chris Lattner7c37b012007-04-26 04:42:16 +0000216 }
Eugene Zelenko975293f2017-09-07 23:28:24 +0000217
Reid Klecknerb4a2d182017-04-24 20:38:30 +0000218 const std::vector<AttributeList> &getAttributeLists() const { return AttributeLists; }
Eugene Zelenko975293f2017-09-07 23:28:24 +0000219
Reid Klecknerb4a2d182017-04-24 20:38:30 +0000220 const std::vector<IndexAndAttrSet> &getAttributeGroups() const {
Bill Wendling92ed7002013-02-11 22:33:26 +0000221 return AttributeGroups;
Bill Wendling51f612e2013-02-10 23:06:02 +0000222 }
Joe Abbey2ad8df22012-11-25 15:23:39 +0000223
David Majnemerdad0a642014-06-27 18:19:56 +0000224 const ComdatSetType &getComdats() const { return Comdats; }
225 unsigned getComdatID(const Comdat *C) const;
226
Chris Lattnerf540d742009-10-28 05:24:40 +0000227 /// getGlobalBasicBlockID - This returns the function-specific ID for the
228 /// specified basic block. This is relatively expensive information, so it
229 /// should only be used by rare constructs such as address-of-label.
230 unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000231
Chad Rosier6a11b642011-06-03 17:02:19 +0000232 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000233 /// use these two methods to get its data into the ValueEnumerator!
Chad Rosier6a11b642011-06-03 17:02:19 +0000234 void incorporateFunction(const Function &F);
Eugene Zelenko975293f2017-09-07 23:28:24 +0000235
Chad Rosier6a11b642011-06-03 17:02:19 +0000236 void purgeFunction();
David Blaikie7b028102015-02-25 00:51:52 +0000237 uint64_t computeBitsRequiredForTypeIndicies() const;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000238
239private:
Chris Lattner430e80d2007-05-04 05:21:47 +0000240 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
Joe Abbey2ad8df22012-11-25 15:23:39 +0000241
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000242 /// Reorder the reachable metadata.
243 ///
244 /// This is not just an optimization, but is mandatory for emitting MDString
245 /// correctly.
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000246 void organizeMetadata();
247
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000248 /// Drop the function tag from the transitive operands of the given node.
Duncan P. N. Exon Smith9695eb32016-04-19 03:46:51 +0000249 void dropFunctionFromMetadata(MetadataMapType::value_type &FirstMD);
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000250
251 /// Incorporate the function metadata.
252 ///
253 /// This should be called before enumerating LocalAsMetadata for the
254 /// function.
255 void incorporateFunctionMetadata(const Function &F);
256
Duncan P. N. Exon Smith71480bd2016-04-22 02:33:06 +0000257 /// Enumerate a single instance of metadata with the given function tag.
258 ///
259 /// If \c MD has already been enumerated, check that \c F matches its
260 /// function tag. If not, call \a dropFunctionFromMetadata().
261 ///
262 /// Otherwise, mark \c MD as visited. Assign it an ID, or just return it if
263 /// it's an \a MDNode.
264 const MDNode *enumerateMetadataImpl(unsigned F, const Metadata *MD);
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000265
Peter Collingbourne21521892016-06-21 23:42:48 +0000266 unsigned getMetadataFunctionID(const Function *F) const;
Duncan P. N. Exon Smith30805b22016-04-23 04:59:22 +0000267
268 /// Enumerate reachable metadata in (almost) post-order.
269 ///
270 /// Enumerate all the metadata reachable from MD. We want to minimize the
271 /// cost of reading bitcode records, and so the primary consideration is that
272 /// operands of uniqued nodes are resolved before the nodes are read. This
273 /// avoids re-uniquing them on the context and factors away RAUW support.
274 ///
275 /// This algorithm guarantees that subgraphs of uniqued nodes are in
276 /// post-order. Distinct subgraphs reachable only from a single uniqued node
277 /// will be in post-order.
278 ///
279 /// \note The relative order of a distinct and uniqued node is irrelevant.
280 /// \a organizeMetadata() will later partition distinct nodes ahead of
281 /// uniqued ones.
282 ///{
Peter Collingbourne21521892016-06-21 23:42:48 +0000283 void EnumerateMetadata(const Function *F, const Metadata *MD);
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000284 void EnumerateMetadata(unsigned F, const Metadata *MD);
Duncan P. N. Exon Smith30805b22016-04-23 04:59:22 +0000285 ///}
286
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000287 void EnumerateFunctionLocalMetadata(const Function &F,
288 const LocalAsMetadata *Local);
289 void EnumerateFunctionLocalMetadata(unsigned F, const LocalAsMetadata *Local);
Devang Patel99ff5a82010-01-09 00:30:14 +0000290 void EnumerateNamedMDNode(const NamedMDNode *NMD);
Victor Hernandez572218b2010-01-14 19:54:11 +0000291 void EnumerateValue(const Value *V);
Chris Lattner229907c2011-07-18 04:54:35 +0000292 void EnumerateType(Type *T);
Victor Hernandez572218b2010-01-14 19:54:11 +0000293 void EnumerateOperandType(const Value *V);
Reid Klecknerb5180542017-03-21 16:57:19 +0000294 void EnumerateAttributes(AttributeList PAL);
Joe Abbey2ad8df22012-11-25 15:23:39 +0000295
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000296 void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000297 void EnumerateNamedMetadata(const Module &M);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000298};
299
Eugene Zelenko975293f2017-09-07 23:28:24 +0000300} // end namespace llvm
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000301
Eugene Zelenko975293f2017-09-07 23:28:24 +0000302#endif // LLVM_LIB_BITCODE_WRITER_VALUEENUMERATOR_H