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 typedef DenseMap<const Metadata *, unsigned> MetadataMapType; 67 MetadataMapType MetadataMap; 68 unsigned NumMDStrings = 0; 69 bool ShouldPreserveUseListOrder; 70 71 typedef DenseMap<AttributeSet, unsigned> AttributeGroupMapType; 72 AttributeGroupMapType AttributeGroupMap; 73 std::vector<AttributeSet> AttributeGroups; 74 75 typedef DenseMap<AttributeSet, unsigned> AttributeMapType; 76 AttributeMapType AttributeMap; 77 std::vector<AttributeSet> Attribute; 78 79 /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by 80 /// the "getGlobalBasicBlockID" method. 81 mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs; 82 83 typedef DenseMap<const Instruction*, unsigned> InstructionMapType; 84 InstructionMapType InstructionMap; 85 unsigned InstructionCount; 86 87 /// BasicBlocks - This contains all the basic blocks for the currently 88 /// incorporated function. Their reverse mapping is stored in ValueMap. 89 std::vector<const BasicBlock*> BasicBlocks; 90 91 /// When a function is incorporated, this is the size of the Values list 92 /// before incorporation. 93 unsigned NumModuleValues; 94 95 /// When a function is incorporated, this is the size of the Metadatas list 96 /// before incorporation. 97 unsigned NumModuleMDs; 98 99 unsigned FirstFuncConstantID; 100 unsigned FirstInstID; 101 102 ValueEnumerator(const ValueEnumerator &) = delete; 103 void operator=(const ValueEnumerator &) = delete; 104 public: 105 ValueEnumerator(const Module &M, bool ShouldPreserveUseListOrder); 106 107 void dump() const; 108 void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const; 109 void print(raw_ostream &OS, const MetadataMapType &Map, 110 const char *Name) const; 111 112 unsigned getValueID(const Value *V) const; 113 unsigned getMetadataID(const Metadata *MD) const { 114 auto ID = getMetadataOrNullID(MD); 115 assert(ID != 0 && "Metadata not in slotcalculator!"); 116 return ID - 1; 117 } 118 unsigned getMetadataOrNullID(const Metadata *MD) const { 119 return MetadataMap.lookup(MD); 120 } 121 unsigned numMDs() const { return MDs.size(); } 122 123 bool shouldPreserveUseListOrder() const { return ShouldPreserveUseListOrder; } 124 125 unsigned getTypeID(Type *T) const { 126 TypeMapType::const_iterator I = TypeMap.find(T); 127 assert(I != TypeMap.end() && "Type not in ValueEnumerator!"); 128 return I->second-1; 129 } 130 131 unsigned getInstructionID(const Instruction *I) const; 132 void setInstructionID(const Instruction *I); 133 134 unsigned getAttributeID(AttributeSet PAL) const { 135 if (PAL.isEmpty()) return 0; // Null maps to zero. 136 AttributeMapType::const_iterator I = AttributeMap.find(PAL); 137 assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!"); 138 return I->second; 139 } 140 141 unsigned getAttributeGroupID(AttributeSet PAL) const { 142 if (PAL.isEmpty()) return 0; // Null maps to zero. 143 AttributeGroupMapType::const_iterator I = AttributeGroupMap.find(PAL); 144 assert(I != AttributeGroupMap.end() && "Attribute not in ValueEnumerator!"); 145 return I->second; 146 } 147 148 /// getFunctionConstantRange - Return the range of values that corresponds to 149 /// function-local constants. 150 void getFunctionConstantRange(unsigned &Start, unsigned &End) const { 151 Start = FirstFuncConstantID; 152 End = FirstInstID; 153 } 154 155 const ValueList &getValues() const { return Values; } 156 const std::vector<const Metadata *> &getMDs() const { return MDs; } 157 ArrayRef<const Metadata *> getMDStrings() const { 158 return makeArrayRef(MDs).slice(0, NumMDStrings); 159 } 160 ArrayRef<const Metadata *> getNonMDStrings() const { 161 return makeArrayRef(MDs).slice(NumMDStrings); 162 } 163 ArrayRef<const Metadata *> getFunctionMDs() const { 164 return makeArrayRef(MDs).slice(NumModuleMDs); 165 } 166 167 const TypeList &getTypes() const { return Types; } 168 const std::vector<const BasicBlock*> &getBasicBlocks() const { 169 return BasicBlocks; 170 } 171 const std::vector<AttributeSet> &getAttributes() const { 172 return Attribute; 173 } 174 const std::vector<AttributeSet> &getAttributeGroups() const { 175 return AttributeGroups; 176 } 177 178 const ComdatSetType &getComdats() const { return Comdats; } 179 unsigned getComdatID(const Comdat *C) const; 180 181 /// getGlobalBasicBlockID - This returns the function-specific ID for the 182 /// specified basic block. This is relatively expensive information, so it 183 /// should only be used by rare constructs such as address-of-label. 184 unsigned getGlobalBasicBlockID(const BasicBlock *BB) const; 185 186 /// incorporateFunction/purgeFunction - If you'd like to deal with a function, 187 /// use these two methods to get its data into the ValueEnumerator! 188 /// 189 void incorporateFunction(const Function &F); 190 void purgeFunction(); 191 uint64_t computeBitsRequiredForTypeIndicies() const; 192 193 private: 194 void OptimizeConstants(unsigned CstStart, unsigned CstEnd); 195 196 // Reorder the reachable metadata. This is not just an optimization, but is 197 // mandatory for emitting MDString correctly. 198 void organizeMetadata(); 199 200 void EnumerateMDNodeOperands(const MDNode *N); 201 void EnumerateMetadata(const Metadata *MD); 202 void EnumerateFunctionLocalMetadata(const LocalAsMetadata *Local); 203 void EnumerateNamedMDNode(const NamedMDNode *NMD); 204 void EnumerateValue(const Value *V); 205 void EnumerateType(Type *T); 206 void EnumerateOperandType(const Value *V); 207 void EnumerateAttributes(AttributeSet PAL); 208 209 void EnumerateValueSymbolTable(const ValueSymbolTable &ST); 210 void EnumerateNamedMetadata(const Module &M); 211 }; 212 213 } // End llvm namespace 214 215 #endif 216