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/SmallVector.h" 19 #include "llvm/ADT/UniqueVector.h" 20 #include "llvm/IR/Attributes.h" 21 #include "llvm/IR/Metadata.h" 22 #include "llvm/IR/Type.h" 23 #include "llvm/IR/UseListOrder.h" 24 #include <vector> 25 26 namespace llvm { 27 28 class Type; 29 class Value; 30 class Instruction; 31 class BasicBlock; 32 class Comdat; 33 class Function; 34 class Module; 35 class Metadata; 36 class LocalAsMetadata; 37 class MDNode; 38 class NamedMDNode; 39 class AttributeSet; 40 class ValueSymbolTable; 41 class MDSymbolTable; 42 class raw_ostream; 43 44 class ValueEnumerator { 45 public: 46 typedef std::vector<Type*> TypeList; 47 48 // For each value, we remember its Value* and occurrence frequency. 49 typedef std::vector<std::pair<const Value*, unsigned> > ValueList; 50 51 UseListOrderStack UseListOrders; 52 53 private: 54 typedef DenseMap<Type*, unsigned> TypeMapType; 55 TypeMapType TypeMap; 56 TypeList Types; 57 58 typedef DenseMap<const Value*, unsigned> ValueMapType; 59 ValueMapType ValueMap; 60 ValueList Values; 61 62 typedef UniqueVector<const Comdat *> ComdatSetType; 63 ComdatSetType Comdats; 64 65 std::vector<const Metadata *> MDs; 66 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 }; 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 dropFunctionFromOps(const MDNode &N); 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 bool insertMetadata(unsigned F, const Metadata *MD); 249 250 unsigned getMetadataFunctionID(const Function *F) const; 251 void EnumerateMDNodeOperands(const Function *F, const MDNode *N); 252 void EnumerateMDNodeOperands(unsigned F, const MDNode *N); 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