blob: bff2de70b3ec6d6e6777651a066767a7e258922e [file] [log] [blame]
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001//===-- Bitcode/Writer/ValueEnumerator.h - Number values --------*- C++ -*-===//
2//
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
17#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"
Benjamin Kramer391be792016-01-27 19:29:56 +000020#include "llvm/IR/Metadata.h"
Benjamin Kramer45275a42016-01-27 18:03:37 +000021#include "llvm/IR/Type.h"
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +000022#include "llvm/IR/UseListOrder.h"
Chris Lattnerc1d10d62007-04-22 06:24:45 +000023#include <vector>
24
25namespace llvm {
26
Chris Lattnerc1d10d62007-04-22 06:24:45 +000027class Type;
Chris Lattner7c37b012007-04-26 04:42:16 +000028class Value;
Devang Patelaf206b82009-09-18 19:26:43 +000029class Instruction;
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;
Chris Lattner7c37b012007-04-26 04:42:16 +000033class Module;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000034class Metadata;
35class LocalAsMetadata;
Devang Pateldf84e8b2010-06-02 23:05:04 +000036class MDNode;
Duncan P. N. Exon Smithc1965312016-04-21 01:55:12 +000037class MDOperand;
Devang Patel99ff5a82010-01-09 00:30:14 +000038class NamedMDNode;
Bill Wendlinge94d8432012-12-07 23:16:57 +000039class AttributeSet;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000040class ValueSymbolTable;
Devang Patelfcfee0f2010-01-07 19:39:36 +000041class MDSymbolTable;
Chad Rosier78037a92011-12-07 20:44:46 +000042class raw_ostream;
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:
Chris Lattner229907c2011-07-18 04:54:35 +000046 typedef std::vector<Type*> TypeList;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000047
48 // For each value, we remember its Value* and occurrence frequency.
49 typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +000050
51 UseListOrderStack UseListOrders;
52
Chris Lattnerc1d10d62007-04-22 06:24:45 +000053private:
Chris Lattner229907c2011-07-18 04:54:35 +000054 typedef DenseMap<Type*, unsigned> TypeMapType;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000055 TypeMapType TypeMap;
Chris Lattner52523562007-04-24 00:16:04 +000056 TypeList Types;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000057
Chris Lattnerc1d10d62007-04-22 06:24:45 +000058 typedef DenseMap<const Value*, unsigned> ValueMapType;
59 ValueMapType ValueMap;
Chris Lattner52523562007-04-24 00:16:04 +000060 ValueList Values;
David Majnemerdad0a642014-06-27 18:19:56 +000061
62 typedef UniqueVector<const Comdat *> ComdatSetType;
63 ComdatSetType Comdats;
64
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000065 std::vector<const Metadata *> MDs;
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +000066 std::vector<const Metadata *> FunctionMDs;
67
68 /// Index of information about a piece of metadata.
69 struct MDIndex {
70 unsigned F = 0; ///< The ID of the function for this metadata, if any.
71 unsigned ID = 0; ///< The implicit ID of this metadata in bitcode.
72
73 MDIndex() = default;
74 explicit MDIndex(unsigned F) : F(F) {}
75
76 /// Check if this has a function tag, and it's different from NewF.
77 bool hasDifferentFunction(unsigned NewF) const { return F && F != NewF; }
78
79 /// Fetch the MD this references out of the given metadata array.
80 const Metadata *get(ArrayRef<const Metadata *> MDs) const {
81 assert(ID && "Expected non-zero ID");
82 assert(ID <= MDs.size() && "Expected valid ID");
83 return MDs[ID - 1];
84 }
85 };
Duncan P. N. Exon Smith9695eb32016-04-19 03:46:51 +000086
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +000087 typedef DenseMap<const Metadata *, MDIndex> MetadataMapType;
Teresa Johnson61b406e2015-12-29 23:00:22 +000088 MetadataMapType MetadataMap;
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +000089
90 /// Range of metadata IDs, as a half-open range.
91 struct MDRange {
92 unsigned First = 0;
93 unsigned Last = 0;
94
95 /// Number of strings in the prefix of the metadata range.
96 unsigned NumStrings = 0;
97
98 MDRange() = default;
99 explicit MDRange(unsigned First) : First(First) {}
100 };
101 SmallDenseMap<unsigned, MDRange, 1> FunctionMDInfo;
102
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +0000103 bool ShouldPreserveUseListOrder;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000104
Bill Wendling92ed7002013-02-11 22:33:26 +0000105 typedef DenseMap<AttributeSet, unsigned> AttributeGroupMapType;
106 AttributeGroupMapType AttributeGroupMap;
107 std::vector<AttributeSet> AttributeGroups;
Bill Wendling51f612e2013-02-10 23:06:02 +0000108
Bill Wendling7b5f4f32013-02-12 08:01:22 +0000109 typedef DenseMap<AttributeSet, unsigned> AttributeMapType;
Devang Patel4c758ea2008-09-25 21:00:45 +0000110 AttributeMapType AttributeMap;
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000111 std::vector<AttributeSet> Attribute;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000112
Chris Lattnerf540d742009-10-28 05:24:40 +0000113 /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
114 /// the "getGlobalBasicBlockID" method.
115 mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000116
Devang Patelaf206b82009-09-18 19:26:43 +0000117 typedef DenseMap<const Instruction*, unsigned> InstructionMapType;
118 InstructionMapType InstructionMap;
119 unsigned InstructionCount;
120
Chris Lattner7c37b012007-04-26 04:42:16 +0000121 /// BasicBlocks - This contains all the basic blocks for the currently
122 /// incorporated function. Their reverse mapping is stored in ValueMap.
123 std::vector<const BasicBlock*> BasicBlocks;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000124
Chris Lattner5f640b92007-04-26 03:50:57 +0000125 /// When a function is incorporated, this is the size of the Values list
126 /// before incorporation.
Chris Lattnere6e364c2007-04-26 05:53:54 +0000127 unsigned NumModuleValues;
Dan Gohmanc828c542010-08-24 02:24:03 +0000128
Teresa Johnson61b406e2015-12-29 23:00:22 +0000129 /// When a function is incorporated, this is the size of the Metadatas list
Dan Gohmanc828c542010-08-24 02:24:03 +0000130 /// before incorporation.
Duncan P. N. Exon Smith93429112016-04-02 15:09:42 +0000131 unsigned NumModuleMDs = 0;
132 unsigned NumMDStrings = 0;
Dan Gohmanc828c542010-08-24 02:24:03 +0000133
Chris Lattnere6e364c2007-04-26 05:53:54 +0000134 unsigned FirstFuncConstantID;
135 unsigned FirstInstID;
Craig Toppera60c0f12012-09-15 17:09:36 +0000136
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000137 ValueEnumerator(const ValueEnumerator &) = delete;
138 void operator=(const ValueEnumerator &) = delete;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000139public:
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +0000140 ValueEnumerator(const Module &M, bool ShouldPreserveUseListOrder);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000141
Chad Rosier78037a92011-12-07 20:44:46 +0000142 void dump() const;
143 void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000144 void print(raw_ostream &OS, const MetadataMapType &Map,
145 const char *Name) const;
Chad Rosier78037a92011-12-07 20:44:46 +0000146
Devang Patel05eb6172009-08-04 06:00:18 +0000147 unsigned getValueID(const Value *V) const;
Duncan P. N. Exon Smith9a6f64e2015-01-20 01:00:23 +0000148 unsigned getMetadataID(const Metadata *MD) const {
149 auto ID = getMetadataOrNullID(MD);
150 assert(ID != 0 && "Metadata not in slotcalculator!");
151 return ID - 1;
152 }
153 unsigned getMetadataOrNullID(const Metadata *MD) const {
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000154 return MetadataMap.lookup(MD).ID;
Duncan P. N. Exon Smith9a6f64e2015-01-20 01:00:23 +0000155 }
Teresa Johnsond4d3dfd2015-11-20 14:51:27 +0000156 unsigned numMDs() const { return MDs.size(); }
Devang Patel05eb6172009-08-04 06:00:18 +0000157
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +0000158 bool shouldPreserveUseListOrder() const { return ShouldPreserveUseListOrder; }
159
Chris Lattner229907c2011-07-18 04:54:35 +0000160 unsigned getTypeID(Type *T) const {
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000161 TypeMapType::const_iterator I = TypeMap.find(T);
162 assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
163 return I->second-1;
164 }
Devang Patelaf206b82009-09-18 19:26:43 +0000165
166 unsigned getInstructionID(const Instruction *I) const;
167 void setInstructionID(const Instruction *I);
168
Bill Wendling92ed7002013-02-11 22:33:26 +0000169 unsigned getAttributeID(AttributeSet PAL) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000170 if (PAL.isEmpty()) return 0; // Null maps to zero.
Bill Wendling7b5f4f32013-02-12 08:01:22 +0000171 AttributeMapType::const_iterator I = AttributeMap.find(PAL);
Devang Patel4c758ea2008-09-25 21:00:45 +0000172 assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
Chris Lattnere4bbad62007-05-03 22:46:43 +0000173 return I->second;
174 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000175
Bill Wendling92ed7002013-02-11 22:33:26 +0000176 unsigned getAttributeGroupID(AttributeSet PAL) const {
Bill Wendling51f612e2013-02-10 23:06:02 +0000177 if (PAL.isEmpty()) return 0; // Null maps to zero.
Bill Wendling92ed7002013-02-11 22:33:26 +0000178 AttributeGroupMapType::const_iterator I = AttributeGroupMap.find(PAL);
179 assert(I != AttributeGroupMap.end() && "Attribute not in ValueEnumerator!");
Bill Wendling51f612e2013-02-10 23:06:02 +0000180 return I->second;
181 }
182
Chris Lattnere6e364c2007-04-26 05:53:54 +0000183 /// getFunctionConstantRange - Return the range of values that corresponds to
184 /// function-local constants.
185 void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
186 Start = FirstFuncConstantID;
187 End = FirstInstID;
188 }
Joe Abbey2ad8df22012-11-25 15:23:39 +0000189
Chris Lattner52523562007-04-24 00:16:04 +0000190 const ValueList &getValues() const { return Values; }
Duncan P. N. Exon Smith93429112016-04-02 15:09:42 +0000191
192 /// Check whether the current block has any metadata to emit.
193 bool hasMDs() const { return NumModuleMDs < MDs.size(); }
194
Duncan P. N. Exon Smith0b76b722016-04-02 15:16:56 +0000195 /// Get the MDString metadata for this block.
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000196 ArrayRef<const Metadata *> getMDStrings() const {
Duncan P. N. Exon Smith93429112016-04-02 15:09:42 +0000197 return makeArrayRef(MDs).slice(NumModuleMDs, NumMDStrings);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000198 }
Duncan P. N. Exon Smith93429112016-04-02 15:09:42 +0000199
Duncan P. N. Exon Smith0b76b722016-04-02 15:16:56 +0000200 /// Get the non-MDString metadata for this block.
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000201 ArrayRef<const Metadata *> getNonMDStrings() const {
Duncan P. N. Exon Smith93429112016-04-02 15:09:42 +0000202 return makeArrayRef(MDs).slice(NumModuleMDs).slice(NumMDStrings);
Devang Pateldf84e8b2010-06-02 23:05:04 +0000203 }
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000204
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000205 const TypeList &getTypes() const { return Types; }
Chris Lattner7c37b012007-04-26 04:42:16 +0000206 const std::vector<const BasicBlock*> &getBasicBlocks() const {
Joe Abbey2ad8df22012-11-25 15:23:39 +0000207 return BasicBlocks;
Chris Lattner7c37b012007-04-26 04:42:16 +0000208 }
Bill Wendlinge94d8432012-12-07 23:16:57 +0000209 const std::vector<AttributeSet> &getAttributes() const {
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000210 return Attribute;
Chris Lattnere4bbad62007-05-03 22:46:43 +0000211 }
Bill Wendling92ed7002013-02-11 22:33:26 +0000212 const std::vector<AttributeSet> &getAttributeGroups() const {
213 return AttributeGroups;
Bill Wendling51f612e2013-02-10 23:06:02 +0000214 }
Joe Abbey2ad8df22012-11-25 15:23:39 +0000215
David Majnemerdad0a642014-06-27 18:19:56 +0000216 const ComdatSetType &getComdats() const { return Comdats; }
217 unsigned getComdatID(const Comdat *C) const;
218
Chris Lattnerf540d742009-10-28 05:24:40 +0000219 /// getGlobalBasicBlockID - This returns the function-specific ID for the
220 /// specified basic block. This is relatively expensive information, so it
221 /// should only be used by rare constructs such as address-of-label.
222 unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000223
Chad Rosier6a11b642011-06-03 17:02:19 +0000224 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000225 /// use these two methods to get its data into the ValueEnumerator!
226 ///
Chad Rosier6a11b642011-06-03 17:02:19 +0000227 void incorporateFunction(const Function &F);
228 void purgeFunction();
David Blaikie7b028102015-02-25 00:51:52 +0000229 uint64_t computeBitsRequiredForTypeIndicies() const;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000230
231private:
Chris Lattner430e80d2007-05-04 05:21:47 +0000232 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
Joe Abbey2ad8df22012-11-25 15:23:39 +0000233
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000234 /// Reorder the reachable metadata.
235 ///
236 /// This is not just an optimization, but is mandatory for emitting MDString
237 /// correctly.
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000238 void organizeMetadata();
239
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000240 /// Drop the function tag from the transitive operands of the given node.
Duncan P. N. Exon Smith9695eb32016-04-19 03:46:51 +0000241 void dropFunctionFromMetadata(MetadataMapType::value_type &FirstMD);
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000242
243 /// Incorporate the function metadata.
244 ///
245 /// This should be called before enumerating LocalAsMetadata for the
246 /// function.
247 void incorporateFunctionMetadata(const Function &F);
248
Duncan P. N. Exon Smith71480bd2016-04-22 02:33:06 +0000249 /// Enumerate a single instance of metadata with the given function tag.
250 ///
251 /// If \c MD has already been enumerated, check that \c F matches its
252 /// function tag. If not, call \a dropFunctionFromMetadata().
253 ///
254 /// Otherwise, mark \c MD as visited. Assign it an ID, or just return it if
255 /// it's an \a MDNode.
256 const MDNode *enumerateMetadataImpl(unsigned F, const Metadata *MD);
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000257
Peter Collingbourne21521892016-06-21 23:42:48 +0000258 unsigned getMetadataFunctionID(const Function *F) const;
Duncan P. N. Exon Smith30805b22016-04-23 04:59:22 +0000259
260 /// Enumerate reachable metadata in (almost) post-order.
261 ///
262 /// Enumerate all the metadata reachable from MD. We want to minimize the
263 /// cost of reading bitcode records, and so the primary consideration is that
264 /// operands of uniqued nodes are resolved before the nodes are read. This
265 /// avoids re-uniquing them on the context and factors away RAUW support.
266 ///
267 /// This algorithm guarantees that subgraphs of uniqued nodes are in
268 /// post-order. Distinct subgraphs reachable only from a single uniqued node
269 /// will be in post-order.
270 ///
271 /// \note The relative order of a distinct and uniqued node is irrelevant.
272 /// \a organizeMetadata() will later partition distinct nodes ahead of
273 /// uniqued ones.
274 ///{
Peter Collingbourne21521892016-06-21 23:42:48 +0000275 void EnumerateMetadata(const Function *F, const Metadata *MD);
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000276 void EnumerateMetadata(unsigned F, const Metadata *MD);
Duncan P. N. Exon Smith30805b22016-04-23 04:59:22 +0000277 ///}
278
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000279 void EnumerateFunctionLocalMetadata(const Function &F,
280 const LocalAsMetadata *Local);
281 void EnumerateFunctionLocalMetadata(unsigned F, const LocalAsMetadata *Local);
Devang Patel99ff5a82010-01-09 00:30:14 +0000282 void EnumerateNamedMDNode(const NamedMDNode *NMD);
Victor Hernandez572218b2010-01-14 19:54:11 +0000283 void EnumerateValue(const Value *V);
Chris Lattner229907c2011-07-18 04:54:35 +0000284 void EnumerateType(Type *T);
Victor Hernandez572218b2010-01-14 19:54:11 +0000285 void EnumerateOperandType(const Value *V);
Bill Wendling7b5f4f32013-02-12 08:01:22 +0000286 void EnumerateAttributes(AttributeSet PAL);
Joe Abbey2ad8df22012-11-25 15:23:39 +0000287
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000288 void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000289 void EnumerateNamedMetadata(const Module &M);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000290};
291
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000292} // End llvm namespace
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000293
294#endif