blob: 112f0b4a1dc465c04ad43f14a62902c46a854324 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattnerc1d10d62007-04-22 06:24:45 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This class gives values and types Unique ID's.
10//
11//===----------------------------------------------------------------------===//
12
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000013#ifndef LLVM_LIB_BITCODE_WRITER_VALUEENUMERATOR_H
14#define LLVM_LIB_BITCODE_WRITER_VALUEENUMERATOR_H
Chris Lattnerc1d10d62007-04-22 06:24:45 +000015
Eugene Zelenko975293f2017-09-07 23:28:24 +000016#include "llvm/ADT/ArrayRef.h"
Chris Lattnerc1d10d62007-04-22 06:24:45 +000017#include "llvm/ADT/DenseMap.h"
David Majnemerdad0a642014-06-27 18:19:56 +000018#include "llvm/ADT/UniqueVector.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Attributes.h"
David Blaikie526f30b2017-11-03 20:24:19 +000020#include "llvm/IR/Metadata.h"
21#include "llvm/IR/Type.h"
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +000022#include "llvm/IR/UseListOrder.h"
Eugene Zelenko975293f2017-09-07 23:28:24 +000023#include <cassert>
24#include <cstdint>
25#include <utility>
Chris Lattnerc1d10d62007-04-22 06:24:45 +000026#include <vector>
27
28namespace llvm {
29
Chris Lattner7c37b012007-04-26 04:42:16 +000030class BasicBlock;
David Majnemerdad0a642014-06-27 18:19:56 +000031class Comdat;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000032class Function;
Eugene Zelenko975293f2017-09-07 23:28:24 +000033class Instruction;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000034class LocalAsMetadata;
Devang Pateldf84e8b2010-06-02 23:05:04 +000035class MDNode;
Eugene Zelenko975293f2017-09-07 23:28:24 +000036class Metadata;
37class Module;
Devang Patel99ff5a82010-01-09 00:30:14 +000038class NamedMDNode;
Chad Rosier78037a92011-12-07 20:44:46 +000039class raw_ostream;
Eugene Zelenko975293f2017-09-07 23:28:24 +000040class Type;
41class Value;
42class ValueSymbolTable;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000043
Benjamin Kramer079b96e2013-09-11 18:05:11 +000044class ValueEnumerator {
Chris Lattnerc1d10d62007-04-22 06:24:45 +000045public:
Eugene Zelenko975293f2017-09-07 23:28:24 +000046 using TypeList = std::vector<Type *>;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000047
48 // For each value, we remember its Value* and occurrence frequency.
Eugene Zelenko975293f2017-09-07 23:28:24 +000049 using ValueList = std::vector<std::pair<const Value *, unsigned>>;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +000050
Reid Klecknerb4a2d182017-04-24 20:38:30 +000051 /// 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.
Eugene Zelenko975293f2017-09-07 23:28:24 +000053 using IndexAndAttrSet = std::pair<unsigned, AttributeSet>;
Reid Klecknerb4a2d182017-04-24 20:38:30 +000054
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +000055 UseListOrderStack UseListOrders;
56
Chris Lattnerc1d10d62007-04-22 06:24:45 +000057private:
Eugene Zelenko975293f2017-09-07 23:28:24 +000058 using TypeMapType = DenseMap<Type *, unsigned>;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000059 TypeMapType TypeMap;
Chris Lattner52523562007-04-24 00:16:04 +000060 TypeList Types;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000061
Eugene Zelenko975293f2017-09-07 23:28:24 +000062 using ValueMapType = DenseMap<const Value *, unsigned>;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000063 ValueMapType ValueMap;
Chris Lattner52523562007-04-24 00:16:04 +000064 ValueList Values;
David Majnemerdad0a642014-06-27 18:19:56 +000065
Eugene Zelenko975293f2017-09-07 23:28:24 +000066 using ComdatSetType = UniqueVector<const Comdat *>;
David Majnemerdad0a642014-06-27 18:19:56 +000067 ComdatSetType Comdats;
68
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000069 std::vector<const Metadata *> MDs;
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +000070 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 };
Duncan P. N. Exon Smith9695eb32016-04-19 03:46:51 +000090
Eugene Zelenko975293f2017-09-07 23:28:24 +000091 using MetadataMapType = DenseMap<const Metadata *, MDIndex>;
Teresa Johnson61b406e2015-12-29 23:00:22 +000092 MetadataMapType MetadataMap;
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +000093
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
Eugene Zelenko975293f2017-09-07 23:28:24 +0000102 MDRange() = default;
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000103 explicit MDRange(unsigned First) : First(First) {}
104 };
105 SmallDenseMap<unsigned, MDRange, 1> FunctionMDInfo;
106
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +0000107 bool ShouldPreserveUseListOrder;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000108
Eugene Zelenko975293f2017-09-07 23:28:24 +0000109 using AttributeGroupMapType = DenseMap<IndexAndAttrSet, unsigned>;
Bill Wendling92ed7002013-02-11 22:33:26 +0000110 AttributeGroupMapType AttributeGroupMap;
Reid Klecknerb4a2d182017-04-24 20:38:30 +0000111 std::vector<IndexAndAttrSet> AttributeGroups;
Bill Wendling51f612e2013-02-10 23:06:02 +0000112
Eugene Zelenko975293f2017-09-07 23:28:24 +0000113 using AttributeListMapType = DenseMap<AttributeList, unsigned>;
Reid Klecknerb4a2d182017-04-24 20:38:30 +0000114 AttributeListMapType AttributeListMap;
115 std::vector<AttributeList> AttributeLists;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000116
Chris Lattnerf540d742009-10-28 05:24:40 +0000117 /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
118 /// the "getGlobalBasicBlockID" method.
119 mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000120
Eugene Zelenko975293f2017-09-07 23:28:24 +0000121 using InstructionMapType = DenseMap<const Instruction *, unsigned>;
Devang Patelaf206b82009-09-18 19:26:43 +0000122 InstructionMapType InstructionMap;
123 unsigned InstructionCount;
124
Chris Lattner7c37b012007-04-26 04:42:16 +0000125 /// 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;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000128
Chris Lattner5f640b92007-04-26 03:50:57 +0000129 /// When a function is incorporated, this is the size of the Values list
130 /// before incorporation.
Chris Lattnere6e364c2007-04-26 05:53:54 +0000131 unsigned NumModuleValues;
Dan Gohmanc828c542010-08-24 02:24:03 +0000132
Teresa Johnson61b406e2015-12-29 23:00:22 +0000133 /// When a function is incorporated, this is the size of the Metadatas list
Dan Gohmanc828c542010-08-24 02:24:03 +0000134 /// before incorporation.
Duncan P. N. Exon Smith93429112016-04-02 15:09:42 +0000135 unsigned NumModuleMDs = 0;
136 unsigned NumMDStrings = 0;
Dan Gohmanc828c542010-08-24 02:24:03 +0000137
Chris Lattnere6e364c2007-04-26 05:53:54 +0000138 unsigned FirstFuncConstantID;
139 unsigned FirstInstID;
Craig Toppera60c0f12012-09-15 17:09:36 +0000140
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000141public:
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +0000142 ValueEnumerator(const Module &M, bool ShouldPreserveUseListOrder);
Eugene Zelenko975293f2017-09-07 23:28:24 +0000143 ValueEnumerator(const ValueEnumerator &) = delete;
144 ValueEnumerator &operator=(const ValueEnumerator &) = delete;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000145
Chad Rosier78037a92011-12-07 20:44:46 +0000146 void dump() const;
147 void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000148 void print(raw_ostream &OS, const MetadataMapType &Map,
149 const char *Name) const;
Chad Rosier78037a92011-12-07 20:44:46 +0000150
Devang Patel05eb6172009-08-04 06:00:18 +0000151 unsigned getValueID(const Value *V) const;
Eugene Zelenko975293f2017-09-07 23:28:24 +0000152
Duncan P. N. Exon Smith9a6f64e2015-01-20 01:00:23 +0000153 unsigned getMetadataID(const Metadata *MD) const {
154 auto ID = getMetadataOrNullID(MD);
155 assert(ID != 0 && "Metadata not in slotcalculator!");
156 return ID - 1;
157 }
Eugene Zelenko975293f2017-09-07 23:28:24 +0000158
Duncan P. N. Exon Smith9a6f64e2015-01-20 01:00:23 +0000159 unsigned getMetadataOrNullID(const Metadata *MD) const {
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000160 return MetadataMap.lookup(MD).ID;
Duncan P. N. Exon Smith9a6f64e2015-01-20 01:00:23 +0000161 }
Eugene Zelenko975293f2017-09-07 23:28:24 +0000162
Teresa Johnsond4d3dfd2015-11-20 14:51:27 +0000163 unsigned numMDs() const { return MDs.size(); }
Devang Patel05eb6172009-08-04 06:00:18 +0000164
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +0000165 bool shouldPreserveUseListOrder() const { return ShouldPreserveUseListOrder; }
166
Chris Lattner229907c2011-07-18 04:54:35 +0000167 unsigned getTypeID(Type *T) const {
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000168 TypeMapType::const_iterator I = TypeMap.find(T);
169 assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
170 return I->second-1;
171 }
Devang Patelaf206b82009-09-18 19:26:43 +0000172
173 unsigned getInstructionID(const Instruction *I) const;
174 void setInstructionID(const Instruction *I);
175
Reid Klecknerb4a2d182017-04-24 20:38:30 +0000176 unsigned getAttributeListID(AttributeList PAL) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000177 if (PAL.isEmpty()) return 0; // Null maps to zero.
Reid Klecknerb4a2d182017-04-24 20:38:30 +0000178 AttributeListMapType::const_iterator I = AttributeListMap.find(PAL);
179 assert(I != AttributeListMap.end() && "Attribute not in ValueEnumerator!");
Chris Lattnere4bbad62007-05-03 22:46:43 +0000180 return I->second;
181 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000182
Reid Klecknerb4a2d182017-04-24 20:38:30 +0000183 unsigned getAttributeGroupID(IndexAndAttrSet Group) const {
184 if (!Group.second.hasAttributes())
185 return 0; // Null maps to zero.
186 AttributeGroupMapType::const_iterator I = AttributeGroupMap.find(Group);
Bill Wendling92ed7002013-02-11 22:33:26 +0000187 assert(I != AttributeGroupMap.end() && "Attribute not in ValueEnumerator!");
Bill Wendling51f612e2013-02-10 23:06:02 +0000188 return I->second;
189 }
190
Chris Lattnere6e364c2007-04-26 05:53:54 +0000191 /// getFunctionConstantRange - Return the range of values that corresponds to
192 /// function-local constants.
193 void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
194 Start = FirstFuncConstantID;
195 End = FirstInstID;
196 }
Joe Abbey2ad8df22012-11-25 15:23:39 +0000197
Chris Lattner52523562007-04-24 00:16:04 +0000198 const ValueList &getValues() const { return Values; }
Duncan P. N. Exon Smith93429112016-04-02 15:09:42 +0000199
200 /// Check whether the current block has any metadata to emit.
201 bool hasMDs() const { return NumModuleMDs < MDs.size(); }
202
Duncan P. N. Exon Smith0b76b722016-04-02 15:16:56 +0000203 /// Get the MDString metadata for this block.
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000204 ArrayRef<const Metadata *> getMDStrings() const {
Duncan P. N. Exon Smith93429112016-04-02 15:09:42 +0000205 return makeArrayRef(MDs).slice(NumModuleMDs, NumMDStrings);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000206 }
Duncan P. N. Exon Smith93429112016-04-02 15:09:42 +0000207
Duncan P. N. Exon Smith0b76b722016-04-02 15:16:56 +0000208 /// Get the non-MDString metadata for this block.
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000209 ArrayRef<const Metadata *> getNonMDStrings() const {
Duncan P. N. Exon Smith93429112016-04-02 15:09:42 +0000210 return makeArrayRef(MDs).slice(NumModuleMDs).slice(NumMDStrings);
Devang Pateldf84e8b2010-06-02 23:05:04 +0000211 }
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000212
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000213 const TypeList &getTypes() const { return Types; }
Eugene Zelenko975293f2017-09-07 23:28:24 +0000214
Chris Lattner7c37b012007-04-26 04:42:16 +0000215 const std::vector<const BasicBlock*> &getBasicBlocks() const {
Joe Abbey2ad8df22012-11-25 15:23:39 +0000216 return BasicBlocks;
Chris Lattner7c37b012007-04-26 04:42:16 +0000217 }
Eugene Zelenko975293f2017-09-07 23:28:24 +0000218
Reid Klecknerb4a2d182017-04-24 20:38:30 +0000219 const std::vector<AttributeList> &getAttributeLists() const { return AttributeLists; }
Eugene Zelenko975293f2017-09-07 23:28:24 +0000220
Reid Klecknerb4a2d182017-04-24 20:38:30 +0000221 const std::vector<IndexAndAttrSet> &getAttributeGroups() const {
Bill Wendling92ed7002013-02-11 22:33:26 +0000222 return AttributeGroups;
Bill Wendling51f612e2013-02-10 23:06:02 +0000223 }
Joe Abbey2ad8df22012-11-25 15:23:39 +0000224
David Majnemerdad0a642014-06-27 18:19:56 +0000225 const ComdatSetType &getComdats() const { return Comdats; }
226 unsigned getComdatID(const Comdat *C) const;
227
Chris Lattnerf540d742009-10-28 05:24:40 +0000228 /// getGlobalBasicBlockID - This returns the function-specific ID for the
229 /// specified basic block. This is relatively expensive information, so it
230 /// should only be used by rare constructs such as address-of-label.
231 unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000232
Chad Rosier6a11b642011-06-03 17:02:19 +0000233 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000234 /// use these two methods to get its data into the ValueEnumerator!
Chad Rosier6a11b642011-06-03 17:02:19 +0000235 void incorporateFunction(const Function &F);
Eugene Zelenko975293f2017-09-07 23:28:24 +0000236
Chad Rosier6a11b642011-06-03 17:02:19 +0000237 void purgeFunction();
David Blaikie7b028102015-02-25 00:51:52 +0000238 uint64_t computeBitsRequiredForTypeIndicies() const;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000239
240private:
Chris Lattner430e80d2007-05-04 05:21:47 +0000241 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
Joe Abbey2ad8df22012-11-25 15:23:39 +0000242
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000243 /// Reorder the reachable metadata.
244 ///
245 /// This is not just an optimization, but is mandatory for emitting MDString
246 /// correctly.
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000247 void organizeMetadata();
248
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000249 /// Drop the function tag from the transitive operands of the given node.
Duncan P. N. Exon Smith9695eb32016-04-19 03:46:51 +0000250 void dropFunctionFromMetadata(MetadataMapType::value_type &FirstMD);
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000251
252 /// Incorporate the function metadata.
253 ///
254 /// This should be called before enumerating LocalAsMetadata for the
255 /// function.
256 void incorporateFunctionMetadata(const Function &F);
257
Duncan P. N. Exon Smith71480bd2016-04-22 02:33:06 +0000258 /// Enumerate a single instance of metadata with the given function tag.
259 ///
260 /// If \c MD has already been enumerated, check that \c F matches its
261 /// function tag. If not, call \a dropFunctionFromMetadata().
262 ///
263 /// Otherwise, mark \c MD as visited. Assign it an ID, or just return it if
264 /// it's an \a MDNode.
265 const MDNode *enumerateMetadataImpl(unsigned F, const Metadata *MD);
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000266
Peter Collingbourne21521892016-06-21 23:42:48 +0000267 unsigned getMetadataFunctionID(const Function *F) const;
Duncan P. N. Exon Smith30805b22016-04-23 04:59:22 +0000268
269 /// Enumerate reachable metadata in (almost) post-order.
270 ///
271 /// Enumerate all the metadata reachable from MD. We want to minimize the
272 /// cost of reading bitcode records, and so the primary consideration is that
273 /// operands of uniqued nodes are resolved before the nodes are read. This
274 /// avoids re-uniquing them on the context and factors away RAUW support.
275 ///
276 /// This algorithm guarantees that subgraphs of uniqued nodes are in
277 /// post-order. Distinct subgraphs reachable only from a single uniqued node
278 /// will be in post-order.
279 ///
280 /// \note The relative order of a distinct and uniqued node is irrelevant.
281 /// \a organizeMetadata() will later partition distinct nodes ahead of
282 /// uniqued ones.
283 ///{
Peter Collingbourne21521892016-06-21 23:42:48 +0000284 void EnumerateMetadata(const Function *F, const Metadata *MD);
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000285 void EnumerateMetadata(unsigned F, const Metadata *MD);
Duncan P. N. Exon Smith30805b22016-04-23 04:59:22 +0000286 ///}
287
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000288 void EnumerateFunctionLocalMetadata(const Function &F,
289 const LocalAsMetadata *Local);
290 void EnumerateFunctionLocalMetadata(unsigned F, const LocalAsMetadata *Local);
Devang Patel99ff5a82010-01-09 00:30:14 +0000291 void EnumerateNamedMDNode(const NamedMDNode *NMD);
Victor Hernandez572218b2010-01-14 19:54:11 +0000292 void EnumerateValue(const Value *V);
Chris Lattner229907c2011-07-18 04:54:35 +0000293 void EnumerateType(Type *T);
Victor Hernandez572218b2010-01-14 19:54:11 +0000294 void EnumerateOperandType(const Value *V);
Reid Klecknerb5180542017-03-21 16:57:19 +0000295 void EnumerateAttributes(AttributeList PAL);
Joe Abbey2ad8df22012-11-25 15:23:39 +0000296
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000297 void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000298 void EnumerateNamedMetadata(const Module &M);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000299};
300
Eugene Zelenko975293f2017-09-07 23:28:24 +0000301} // end namespace llvm
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000302
Eugene Zelenko975293f2017-09-07 23:28:24 +0000303#endif // LLVM_LIB_BITCODE_WRITER_VALUEENUMERATOR_H