1 //===-- Bitcode/Writer/ValueEnumerator.h - Number values --------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This class gives values and types Unique ID's. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_LIB_BITCODE_WRITER_VALUEENUMERATOR_H 15 #define LLVM_LIB_BITCODE_WRITER_VALUEENUMERATOR_H 16 17 #include "llvm/ADT/DenseMap.h" 18 #include "llvm/ADT/UniqueVector.h" 19 #include "llvm/IR/Attributes.h" 20 #include "llvm/IR/Metadata.h" 21 #include "llvm/IR/Type.h" 22 #include "llvm/IR/UseListOrder.h" 23 #include <vector> 24 25 namespace llvm { 26 27 class Type; 28 class Value; 29 class Instruction; 30 class BasicBlock; 31 class Comdat; 32 class Function; 33 class Module; 34 class Metadata; 35 class LocalAsMetadata; 36 class MDNode; 37 class NamedMDNode; 38 class AttributeSet; 39 class ValueSymbolTable; 40 class MDSymbolTable; 41 class raw_ostream; 42 43 class ValueEnumerator { 44 public: 45 typedef std::vector<Type*> TypeList; 46 47 // For each value, we remember its Value* and occurrence frequency. 48 typedef std::vector<std::pair<const Value*, unsigned> > ValueList; 49 50 UseListOrderStack UseListOrders; 51 52 private: 53 typedef DenseMap<Type*, unsigned> TypeMapType; 54 TypeMapType TypeMap; 55 TypeList Types; 56 57 typedef DenseMap<const Value*, unsigned> ValueMapType; 58 ValueMapType ValueMap; 59 ValueList Values; 60 61 typedef UniqueVector<const Comdat *> ComdatSetType; 62 ComdatSetType Comdats; 63 64 std::vector<const Metadata *> MDs; 65 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 }; 85 86 typedef DenseMap<const Metadata *, MDIndex> MetadataMapType; 87 MetadataMapType MetadataMap; 88 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 102 bool ShouldPreserveUseListOrder; 103 104 typedef DenseMap<AttributeSet, unsigned> AttributeGroupMapType; 105 AttributeGroupMapType AttributeGroupMap; 106 std::vector<AttributeSet> AttributeGroups; 107 108 typedef DenseMap<AttributeSet, unsigned> AttributeMapType; 109 AttributeMapType AttributeMap; 110 std::vector<AttributeSet> Attribute; 111 112 /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by 113 /// the "getGlobalBasicBlockID" method. 114 mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs; 115 116 typedef DenseMap<const Instruction*, unsigned> InstructionMapType; 117 InstructionMapType InstructionMap; 118 unsigned InstructionCount; 119 120 /// 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; 123 124 /// When a function is incorporated, this is the size of the Values list 125 /// before incorporation. 126 unsigned NumModuleValues; 127 128 /// When a function is incorporated, this is the size of the Metadatas list 129 /// before incorporation. 130 unsigned NumModuleMDs = 0; 131 unsigned NumMDStrings = 0; 132 133 unsigned FirstFuncConstantID; 134 unsigned FirstInstID; 135 136 ValueEnumerator(const ValueEnumerator &) = delete; 137 void operator=(const ValueEnumerator &) = delete; 138 public: 139 ValueEnumerator(const Module &M, bool ShouldPreserveUseListOrder); 140 141 void dump() const; 142 void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const; 143 void print(raw_ostream &OS, const MetadataMapType &Map, 144 const char *Name) const; 145 146 unsigned getValueID(const Value *V) const; 147 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 { 153 return MetadataMap.lookup(MD).ID; 154 } 155 unsigned numMDs() const { return MDs.size(); } 156 157 bool shouldPreserveUseListOrder() const { return ShouldPreserveUseListOrder; } 158 159 unsigned getTypeID(Type *T) const { 160 TypeMapType::const_iterator I = TypeMap.find(T); 161 assert(I != TypeMap.end() && "Type not in ValueEnumerator!"); 162 return I->second-1; 163 } 164 165 unsigned getInstructionID(const Instruction *I) const; 166 void setInstructionID(const Instruction *I); 167 168 unsigned getAttributeID(AttributeSet PAL) const { 169 if (PAL.isEmpty()) return 0; // Null maps to zero. 170 AttributeMapType::const_iterator I = AttributeMap.find(PAL); 171 assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!"); 172 return I->second; 173 } 174 175 unsigned getAttributeGroupID(AttributeSet PAL) const { 176 if (PAL.isEmpty()) return 0; // Null maps to zero. 177 AttributeGroupMapType::const_iterator I = AttributeGroupMap.find(PAL); 178 assert(I != AttributeGroupMap.end() && "Attribute not in ValueEnumerator!"); 179 return I->second; 180 } 181 182 /// 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 } 188 189 const ValueList &getValues() const { return Values; } 190 191 /// Check whether the current block has any metadata to emit. 192 bool hasMDs() const { return NumModuleMDs < MDs.size(); } 193 194 /// Get the MDString metadata for this block. 195 ArrayRef<const Metadata *> getMDStrings() const { 196 return makeArrayRef(MDs).slice(NumModuleMDs, NumMDStrings); 197 } 198 199 /// Get the non-MDString metadata for this block. 200 ArrayRef<const Metadata *> getNonMDStrings() const { 201 return makeArrayRef(MDs).slice(NumModuleMDs).slice(NumMDStrings); 202 } 203 204 const TypeList &getTypes() const { return Types; } 205 const std::vector<const BasicBlock*> &getBasicBlocks() const { 206 return BasicBlocks; 207 } 208 const std::vector<AttributeSet> &getAttributes() const { 209 return Attribute; 210 } 211 const std::vector<AttributeSet> &getAttributeGroups() const { 212 return AttributeGroups; 213 } 214 215 const ComdatSetType &getComdats() const { return Comdats; } 216 unsigned getComdatID(const Comdat *C) const; 217 218 /// 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; 222 223 /// incorporateFunction/purgeFunction - If you'd like to deal with a function, 224 /// use these two methods to get its data into the ValueEnumerator! 225 /// 226 void incorporateFunction(const Function &F); 227 void purgeFunction(); 228 uint64_t computeBitsRequiredForTypeIndicies() const; 229 230 private: 231 void OptimizeConstants(unsigned CstStart, unsigned CstEnd); 232 233 /// Reorder the reachable metadata. 234 /// 235 /// This is not just an optimization, but is mandatory for emitting MDString 236 /// correctly. 237 void organizeMetadata(); 238 239 /// Drop the function tag from the transitive operands of the given node. 240 void dropFunctionFromMetadata(MetadataMapType::value_type &FirstMD); 241 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 248 void enumerateMetadataImpl( 249 unsigned F, const Metadata *MD, 250 SmallVectorImpl<std::pair<const MDNode *, bool>> &Worklist); 251 252 unsigned getMetadataFunctionID(const Function *F) const; 253 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); 258 void EnumerateNamedMDNode(const NamedMDNode *NMD); 259 void EnumerateValue(const Value *V); 260 void EnumerateType(Type *T); 261 void EnumerateOperandType(const Value *V); 262 void EnumerateAttributes(AttributeSet PAL); 263 264 void EnumerateValueSymbolTable(const ValueSymbolTable &ST); 265 void EnumerateNamedMetadata(const Module &M); 266 }; 267 268 } // End llvm namespace 269 270 #endif 271