blob: 98e2df1562917a76bf15ef52ab9b2f31d1af0847 [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;
Devang Patel99ff5a82010-01-09 00:30:14 +000037class NamedMDNode;
Bill Wendlinge94d8432012-12-07 23:16:57 +000038class AttributeSet;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000039class ValueSymbolTable;
Devang Patelfcfee0f2010-01-07 19:39:36 +000040class MDSymbolTable;
Chad Rosier78037a92011-12-07 20:44:46 +000041class raw_ostream;
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:
Chris Lattner229907c2011-07-18 04:54:35 +000045 typedef std::vector<Type*> TypeList;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000046
47 // For each value, we remember its Value* and occurrence frequency.
48 typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +000049
50 UseListOrderStack UseListOrders;
51
Chris Lattnerc1d10d62007-04-22 06:24:45 +000052private:
Chris Lattner229907c2011-07-18 04:54:35 +000053 typedef DenseMap<Type*, unsigned> TypeMapType;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000054 TypeMapType TypeMap;
Chris Lattner52523562007-04-24 00:16:04 +000055 TypeList Types;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000056
Chris Lattnerc1d10d62007-04-22 06:24:45 +000057 typedef DenseMap<const Value*, unsigned> ValueMapType;
58 ValueMapType ValueMap;
Chris Lattner52523562007-04-24 00:16:04 +000059 ValueList Values;
David Majnemerdad0a642014-06-27 18:19:56 +000060
61 typedef UniqueVector<const Comdat *> ComdatSetType;
62 ComdatSetType Comdats;
63
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000064 std::vector<const Metadata *> MDs;
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +000065 std::vector<const Metadata *> FunctionMDs;
66
67 /// Index of information about a piece of metadata.
68 struct MDIndex {
69 unsigned F = 0; ///< The ID of the function for this metadata, if any.
70 unsigned ID = 0; ///< The implicit ID of this metadata in bitcode.
71
72 MDIndex() = default;
73 explicit MDIndex(unsigned F) : F(F) {}
74
75 /// Check if this has a function tag, and it's different from NewF.
76 bool hasDifferentFunction(unsigned NewF) const { return F && F != NewF; }
77
78 /// Fetch the MD this references out of the given metadata array.
79 const Metadata *get(ArrayRef<const Metadata *> MDs) const {
80 assert(ID && "Expected non-zero ID");
81 assert(ID <= MDs.size() && "Expected valid ID");
82 return MDs[ID - 1];
83 }
84 };
Duncan P. N. Exon Smith9695eb32016-04-19 03:46:51 +000085
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +000086 typedef DenseMap<const Metadata *, MDIndex> MetadataMapType;
Teresa Johnson61b406e2015-12-29 23:00:22 +000087 MetadataMapType MetadataMap;
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +000088
89 /// Range of metadata IDs, as a half-open range.
90 struct MDRange {
91 unsigned First = 0;
92 unsigned Last = 0;
93
94 /// Number of strings in the prefix of the metadata range.
95 unsigned NumStrings = 0;
96
97 MDRange() = default;
98 explicit MDRange(unsigned First) : First(First) {}
99 };
100 SmallDenseMap<unsigned, MDRange, 1> FunctionMDInfo;
101
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +0000102 bool ShouldPreserveUseListOrder;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000103
Bill Wendling92ed7002013-02-11 22:33:26 +0000104 typedef DenseMap<AttributeSet, unsigned> AttributeGroupMapType;
105 AttributeGroupMapType AttributeGroupMap;
106 std::vector<AttributeSet> AttributeGroups;
Bill Wendling51f612e2013-02-10 23:06:02 +0000107
Bill Wendling7b5f4f32013-02-12 08:01:22 +0000108 typedef DenseMap<AttributeSet, unsigned> AttributeMapType;
Devang Patel4c758ea2008-09-25 21:00:45 +0000109 AttributeMapType AttributeMap;
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000110 std::vector<AttributeSet> Attribute;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000111
Chris Lattnerf540d742009-10-28 05:24:40 +0000112 /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
113 /// the "getGlobalBasicBlockID" method.
114 mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000115
Devang Patelaf206b82009-09-18 19:26:43 +0000116 typedef DenseMap<const Instruction*, unsigned> InstructionMapType;
117 InstructionMapType InstructionMap;
118 unsigned InstructionCount;
119
Chris Lattner7c37b012007-04-26 04:42:16 +0000120 /// BasicBlocks - This contains all the basic blocks for the currently
121 /// incorporated function. Their reverse mapping is stored in ValueMap.
122 std::vector<const BasicBlock*> BasicBlocks;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000123
Chris Lattner5f640b92007-04-26 03:50:57 +0000124 /// When a function is incorporated, this is the size of the Values list
125 /// before incorporation.
Chris Lattnere6e364c2007-04-26 05:53:54 +0000126 unsigned NumModuleValues;
Dan Gohmanc828c542010-08-24 02:24:03 +0000127
Teresa Johnson61b406e2015-12-29 23:00:22 +0000128 /// When a function is incorporated, this is the size of the Metadatas list
Dan Gohmanc828c542010-08-24 02:24:03 +0000129 /// before incorporation.
Duncan P. N. Exon Smith93429112016-04-02 15:09:42 +0000130 unsigned NumModuleMDs = 0;
131 unsigned NumMDStrings = 0;
Dan Gohmanc828c542010-08-24 02:24:03 +0000132
Chris Lattnere6e364c2007-04-26 05:53:54 +0000133 unsigned FirstFuncConstantID;
134 unsigned FirstInstID;
Craig Toppera60c0f12012-09-15 17:09:36 +0000135
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000136 ValueEnumerator(const ValueEnumerator &) = delete;
137 void operator=(const ValueEnumerator &) = delete;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000138public:
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +0000139 ValueEnumerator(const Module &M, bool ShouldPreserveUseListOrder);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000140
Chad Rosier78037a92011-12-07 20:44:46 +0000141 void dump() const;
142 void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000143 void print(raw_ostream &OS, const MetadataMapType &Map,
144 const char *Name) const;
Chad Rosier78037a92011-12-07 20:44:46 +0000145
Devang Patel05eb6172009-08-04 06:00:18 +0000146 unsigned getValueID(const Value *V) const;
Duncan P. N. Exon Smith9a6f64e2015-01-20 01:00:23 +0000147 unsigned getMetadataID(const Metadata *MD) const {
148 auto ID = getMetadataOrNullID(MD);
149 assert(ID != 0 && "Metadata not in slotcalculator!");
150 return ID - 1;
151 }
152 unsigned getMetadataOrNullID(const Metadata *MD) const {
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000153 return MetadataMap.lookup(MD).ID;
Duncan P. N. Exon Smith9a6f64e2015-01-20 01:00:23 +0000154 }
Teresa Johnsond4d3dfd2015-11-20 14:51:27 +0000155 unsigned numMDs() const { return MDs.size(); }
Devang Patel05eb6172009-08-04 06:00:18 +0000156
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +0000157 bool shouldPreserveUseListOrder() const { return ShouldPreserveUseListOrder; }
158
Chris Lattner229907c2011-07-18 04:54:35 +0000159 unsigned getTypeID(Type *T) const {
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000160 TypeMapType::const_iterator I = TypeMap.find(T);
161 assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
162 return I->second-1;
163 }
Devang Patelaf206b82009-09-18 19:26:43 +0000164
165 unsigned getInstructionID(const Instruction *I) const;
166 void setInstructionID(const Instruction *I);
167
Bill Wendling92ed7002013-02-11 22:33:26 +0000168 unsigned getAttributeID(AttributeSet PAL) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000169 if (PAL.isEmpty()) return 0; // Null maps to zero.
Bill Wendling7b5f4f32013-02-12 08:01:22 +0000170 AttributeMapType::const_iterator I = AttributeMap.find(PAL);
Devang Patel4c758ea2008-09-25 21:00:45 +0000171 assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
Chris Lattnere4bbad62007-05-03 22:46:43 +0000172 return I->second;
173 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000174
Bill Wendling92ed7002013-02-11 22:33:26 +0000175 unsigned getAttributeGroupID(AttributeSet PAL) const {
Bill Wendling51f612e2013-02-10 23:06:02 +0000176 if (PAL.isEmpty()) return 0; // Null maps to zero.
Bill Wendling92ed7002013-02-11 22:33:26 +0000177 AttributeGroupMapType::const_iterator I = AttributeGroupMap.find(PAL);
178 assert(I != AttributeGroupMap.end() && "Attribute not in ValueEnumerator!");
Bill Wendling51f612e2013-02-10 23:06:02 +0000179 return I->second;
180 }
181
Chris Lattnere6e364c2007-04-26 05:53:54 +0000182 /// getFunctionConstantRange - Return the range of values that corresponds to
183 /// function-local constants.
184 void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
185 Start = FirstFuncConstantID;
186 End = FirstInstID;
187 }
Joe Abbey2ad8df22012-11-25 15:23:39 +0000188
Chris Lattner52523562007-04-24 00:16:04 +0000189 const ValueList &getValues() const { return Values; }
Duncan P. N. Exon Smith93429112016-04-02 15:09:42 +0000190
191 /// Check whether the current block has any metadata to emit.
192 bool hasMDs() const { return NumModuleMDs < MDs.size(); }
193
Duncan P. N. Exon Smith0b76b722016-04-02 15:16:56 +0000194 /// Get the MDString metadata for this block.
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000195 ArrayRef<const Metadata *> getMDStrings() const {
Duncan P. N. Exon Smith93429112016-04-02 15:09:42 +0000196 return makeArrayRef(MDs).slice(NumModuleMDs, NumMDStrings);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000197 }
Duncan P. N. Exon Smith93429112016-04-02 15:09:42 +0000198
Duncan P. N. Exon Smith0b76b722016-04-02 15:16:56 +0000199 /// Get the non-MDString metadata for this block.
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000200 ArrayRef<const Metadata *> getNonMDStrings() const {
Duncan P. N. Exon Smith93429112016-04-02 15:09:42 +0000201 return makeArrayRef(MDs).slice(NumModuleMDs).slice(NumMDStrings);
Devang Pateldf84e8b2010-06-02 23:05:04 +0000202 }
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000203
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000204 const TypeList &getTypes() const { return Types; }
Chris Lattner7c37b012007-04-26 04:42:16 +0000205 const std::vector<const BasicBlock*> &getBasicBlocks() const {
Joe Abbey2ad8df22012-11-25 15:23:39 +0000206 return BasicBlocks;
Chris Lattner7c37b012007-04-26 04:42:16 +0000207 }
Bill Wendlinge94d8432012-12-07 23:16:57 +0000208 const std::vector<AttributeSet> &getAttributes() const {
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000209 return Attribute;
Chris Lattnere4bbad62007-05-03 22:46:43 +0000210 }
Bill Wendling92ed7002013-02-11 22:33:26 +0000211 const std::vector<AttributeSet> &getAttributeGroups() const {
212 return AttributeGroups;
Bill Wendling51f612e2013-02-10 23:06:02 +0000213 }
Joe Abbey2ad8df22012-11-25 15:23:39 +0000214
David Majnemerdad0a642014-06-27 18:19:56 +0000215 const ComdatSetType &getComdats() const { return Comdats; }
216 unsigned getComdatID(const Comdat *C) const;
217
Chris Lattnerf540d742009-10-28 05:24:40 +0000218 /// getGlobalBasicBlockID - This returns the function-specific ID for the
219 /// specified basic block. This is relatively expensive information, so it
220 /// should only be used by rare constructs such as address-of-label.
221 unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000222
Chad Rosier6a11b642011-06-03 17:02:19 +0000223 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000224 /// use these two methods to get its data into the ValueEnumerator!
225 ///
Chad Rosier6a11b642011-06-03 17:02:19 +0000226 void incorporateFunction(const Function &F);
227 void purgeFunction();
David Blaikie7b028102015-02-25 00:51:52 +0000228 uint64_t computeBitsRequiredForTypeIndicies() const;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000229
230private:
Chris Lattner430e80d2007-05-04 05:21:47 +0000231 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
Joe Abbey2ad8df22012-11-25 15:23:39 +0000232
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000233 /// Reorder the reachable metadata.
234 ///
235 /// This is not just an optimization, but is mandatory for emitting MDString
236 /// correctly.
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000237 void organizeMetadata();
238
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000239 /// Drop the function tag from the transitive operands of the given node.
Duncan P. N. Exon Smith9695eb32016-04-19 03:46:51 +0000240 void dropFunctionFromMetadata(MetadataMapType::value_type &FirstMD);
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000241
242 /// Incorporate the function metadata.
243 ///
244 /// This should be called before enumerating LocalAsMetadata for the
245 /// function.
246 void incorporateFunctionMetadata(const Function &F);
247
Duncan P. N. Exon Smith9695eb32016-04-19 03:46:51 +0000248 void enumerateMetadataImpl(
249 unsigned F, const Metadata *MD,
250 SmallVectorImpl<std::pair<const MDNode *, bool>> &Worklist);
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000251
252 unsigned getMetadataFunctionID(const Function *F) const;
Duncan P. N. Exon Smith520f8542016-04-02 15:22:57 +0000253 void EnumerateMetadata(const Function *F, const Metadata *MD);
254 void EnumerateMetadata(unsigned F, const Metadata *MD);
255 void EnumerateFunctionLocalMetadata(const Function &F,
256 const LocalAsMetadata *Local);
257 void EnumerateFunctionLocalMetadata(unsigned F, const LocalAsMetadata *Local);
Devang Patel99ff5a82010-01-09 00:30:14 +0000258 void EnumerateNamedMDNode(const NamedMDNode *NMD);
Victor Hernandez572218b2010-01-14 19:54:11 +0000259 void EnumerateValue(const Value *V);
Chris Lattner229907c2011-07-18 04:54:35 +0000260 void EnumerateType(Type *T);
Victor Hernandez572218b2010-01-14 19:54:11 +0000261 void EnumerateOperandType(const Value *V);
Bill Wendling7b5f4f32013-02-12 08:01:22 +0000262 void EnumerateAttributes(AttributeSet PAL);
Joe Abbey2ad8df22012-11-25 15:23:39 +0000263
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000264 void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000265 void EnumerateNamedMetadata(const Module &M);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000266};
267
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000268} // End llvm namespace
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000269
270#endif