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