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