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 SmallVector<const LocalAsMetadata *, 8> FunctionLocalMDs; 67 typedef DenseMap<const Metadata *, unsigned> MetadataMapType; 68 MetadataMapType MetadataMap; 69 bool HasMDString; 70 bool HasDILocation; 71 bool HasGenericDINode; 72 bool ShouldPreserveUseListOrder; 73 74 typedef DenseMap<AttributeSet, unsigned> AttributeGroupMapType; 75 AttributeGroupMapType AttributeGroupMap; 76 std::vector<AttributeSet> AttributeGroups; 77 78 typedef DenseMap<AttributeSet, unsigned> AttributeMapType; 79 AttributeMapType AttributeMap; 80 std::vector<AttributeSet> Attribute; 81 82 /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by 83 /// the "getGlobalBasicBlockID" method. 84 mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs; 85 86 typedef DenseMap<const Instruction*, unsigned> InstructionMapType; 87 InstructionMapType InstructionMap; 88 unsigned InstructionCount; 89 90 /// BasicBlocks - This contains all the basic blocks for the currently 91 /// incorporated function. Their reverse mapping is stored in ValueMap. 92 std::vector<const BasicBlock*> BasicBlocks; 93 94 /// When a function is incorporated, this is the size of the Values list 95 /// before incorporation. 96 unsigned NumModuleValues; 97 98 /// When a function is incorporated, this is the size of the Metadatas list 99 /// before incorporation. 100 unsigned NumModuleMDs; 101 102 unsigned FirstFuncConstantID; 103 unsigned FirstInstID; 104 105 ValueEnumerator(const ValueEnumerator &) = delete; 106 void operator=(const ValueEnumerator &) = delete; 107 public: 108 ValueEnumerator(const Module &M, bool ShouldPreserveUseListOrder); 109 110 void dump() const; 111 void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const; 112 void print(raw_ostream &OS, const MetadataMapType &Map, 113 const char *Name) const; 114 115 unsigned getValueID(const Value *V) const; 116 unsigned getMetadataID(const Metadata *MD) const { 117 auto ID = getMetadataOrNullID(MD); 118 assert(ID != 0 && "Metadata not in slotcalculator!"); 119 return ID - 1; 120 } 121 unsigned getMetadataOrNullID(const Metadata *MD) const { 122 return MetadataMap.lookup(MD); 123 } 124 unsigned numMDs() const { return MDs.size(); } 125 126 bool hasMDString() const { return HasMDString; } 127 bool hasDILocation() const { return HasDILocation; } 128 bool hasGenericDINode() const { return HasGenericDINode; } 129 130 bool shouldPreserveUseListOrder() const { return ShouldPreserveUseListOrder; } 131 132 unsigned getTypeID(Type *T) const { 133 TypeMapType::const_iterator I = TypeMap.find(T); 134 assert(I != TypeMap.end() && "Type not in ValueEnumerator!"); 135 return I->second-1; 136 } 137 138 unsigned getInstructionID(const Instruction *I) const; 139 void setInstructionID(const Instruction *I); 140 141 unsigned getAttributeID(AttributeSet PAL) const { 142 if (PAL.isEmpty()) return 0; // Null maps to zero. 143 AttributeMapType::const_iterator I = AttributeMap.find(PAL); 144 assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!"); 145 return I->second; 146 } 147 148 unsigned getAttributeGroupID(AttributeSet PAL) const { 149 if (PAL.isEmpty()) return 0; // Null maps to zero. 150 AttributeGroupMapType::const_iterator I = AttributeGroupMap.find(PAL); 151 assert(I != AttributeGroupMap.end() && "Attribute not in ValueEnumerator!"); 152 return I->second; 153 } 154 155 /// getFunctionConstantRange - Return the range of values that corresponds to 156 /// function-local constants. 157 void getFunctionConstantRange(unsigned &Start, unsigned &End) const { 158 Start = FirstFuncConstantID; 159 End = FirstInstID; 160 } 161 162 const ValueList &getValues() const { return Values; } 163 const std::vector<const Metadata *> &getMDs() const { return MDs; } 164 const SmallVectorImpl<const LocalAsMetadata *> &getFunctionLocalMDs() const { 165 return FunctionLocalMDs; 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 void EnumerateMDNodeOperands(const MDNode *N); 197 void EnumerateMetadata(const Metadata *MD); 198 void EnumerateFunctionLocalMetadata(const LocalAsMetadata *Local); 199 void EnumerateNamedMDNode(const NamedMDNode *NMD); 200 void EnumerateValue(const Value *V); 201 void EnumerateType(Type *T); 202 void EnumerateOperandType(const Value *V); 203 void EnumerateAttributes(AttributeSet PAL); 204 205 void EnumerateValueSymbolTable(const ValueSymbolTable &ST); 206 void EnumerateNamedMetadata(const Module &M); 207 }; 208 209 } // End llvm namespace 210 211 #endif 212