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/UseListOrder.h" 22 #include <vector> 23 24 namespace llvm { 25 26 class Type; 27 class Value; 28 class Instruction; 29 class BasicBlock; 30 class Comdat; 31 class Function; 32 class Module; 33 class Metadata; 34 class LocalAsMetadata; 35 class MDNode; 36 class NamedMDNode; 37 class AttributeSet; 38 class ValueSymbolTable; 39 class MDSymbolTable; 40 class raw_ostream; 41 42 class ValueEnumerator { 43 public: 44 typedef std::vector<Type*> TypeList; 45 46 // For each value, we remember its Value* and occurrence frequency. 47 typedef std::vector<std::pair<const Value*, unsigned> > ValueList; 48 49 UseListOrderStack UseListOrders; 50 51 private: 52 typedef DenseMap<Type*, unsigned> TypeMapType; 53 TypeMapType TypeMap; 54 TypeList Types; 55 56 typedef DenseMap<const Value*, unsigned> ValueMapType; 57 ValueMapType ValueMap; 58 ValueList Values; 59 60 typedef UniqueVector<const Comdat *> ComdatSetType; 61 ComdatSetType Comdats; 62 63 std::vector<const Metadata *> MDs; 64 SmallVector<const LocalAsMetadata *, 8> FunctionLocalMDs; 65 typedef DenseMap<const Metadata *, unsigned> MetadataMapType; 66 MetadataMapType MDValueMap; 67 bool HasMDString; 68 bool HasMDLocation; 69 70 typedef DenseMap<AttributeSet, unsigned> AttributeGroupMapType; 71 AttributeGroupMapType AttributeGroupMap; 72 std::vector<AttributeSet> AttributeGroups; 73 74 typedef DenseMap<AttributeSet, unsigned> AttributeMapType; 75 AttributeMapType AttributeMap; 76 std::vector<AttributeSet> Attribute; 77 78 /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by 79 /// the "getGlobalBasicBlockID" method. 80 mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs; 81 82 typedef DenseMap<const Instruction*, unsigned> InstructionMapType; 83 InstructionMapType InstructionMap; 84 unsigned InstructionCount; 85 86 /// BasicBlocks - This contains all the basic blocks for the currently 87 /// incorporated function. Their reverse mapping is stored in ValueMap. 88 std::vector<const BasicBlock*> BasicBlocks; 89 90 /// When a function is incorporated, this is the size of the Values list 91 /// before incorporation. 92 unsigned NumModuleValues; 93 94 /// When a function is incorporated, this is the size of the MDValues list 95 /// before incorporation. 96 unsigned NumModuleMDs; 97 98 unsigned FirstFuncConstantID; 99 unsigned FirstInstID; 100 101 ValueEnumerator(const ValueEnumerator &) LLVM_DELETED_FUNCTION; 102 void operator=(const ValueEnumerator &) LLVM_DELETED_FUNCTION; 103 public: 104 ValueEnumerator(const Module &M); 105 106 void dump() const; 107 void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const; 108 void print(raw_ostream &OS, const MetadataMapType &Map, 109 const char *Name) const; 110 111 unsigned getValueID(const Value *V) const; 112 unsigned getMetadataID(const Metadata *V) const; 113 hasMDString()114 bool hasMDString() const { return HasMDString; } hasMDLocation()115 bool hasMDLocation() const { return HasMDLocation; } 116 getTypeID(Type * T)117 unsigned getTypeID(Type *T) const { 118 TypeMapType::const_iterator I = TypeMap.find(T); 119 assert(I != TypeMap.end() && "Type not in ValueEnumerator!"); 120 return I->second-1; 121 } 122 123 unsigned getInstructionID(const Instruction *I) const; 124 void setInstructionID(const Instruction *I); 125 getAttributeID(AttributeSet PAL)126 unsigned getAttributeID(AttributeSet PAL) const { 127 if (PAL.isEmpty()) return 0; // Null maps to zero. 128 AttributeMapType::const_iterator I = AttributeMap.find(PAL); 129 assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!"); 130 return I->second; 131 } 132 getAttributeGroupID(AttributeSet PAL)133 unsigned getAttributeGroupID(AttributeSet PAL) const { 134 if (PAL.isEmpty()) return 0; // Null maps to zero. 135 AttributeGroupMapType::const_iterator I = AttributeGroupMap.find(PAL); 136 assert(I != AttributeGroupMap.end() && "Attribute not in ValueEnumerator!"); 137 return I->second; 138 } 139 140 /// getFunctionConstantRange - Return the range of values that corresponds to 141 /// function-local constants. getFunctionConstantRange(unsigned & Start,unsigned & End)142 void getFunctionConstantRange(unsigned &Start, unsigned &End) const { 143 Start = FirstFuncConstantID; 144 End = FirstInstID; 145 } 146 getValues()147 const ValueList &getValues() const { return Values; } getMDs()148 const std::vector<const Metadata *> &getMDs() const { return MDs; } getFunctionLocalMDs()149 const SmallVectorImpl<const LocalAsMetadata *> &getFunctionLocalMDs() const { 150 return FunctionLocalMDs; 151 } getTypes()152 const TypeList &getTypes() const { return Types; } getBasicBlocks()153 const std::vector<const BasicBlock*> &getBasicBlocks() const { 154 return BasicBlocks; 155 } getAttributes()156 const std::vector<AttributeSet> &getAttributes() const { 157 return Attribute; 158 } getAttributeGroups()159 const std::vector<AttributeSet> &getAttributeGroups() const { 160 return AttributeGroups; 161 } 162 getComdats()163 const ComdatSetType &getComdats() const { return Comdats; } 164 unsigned getComdatID(const Comdat *C) const; 165 166 /// getGlobalBasicBlockID - This returns the function-specific ID for the 167 /// specified basic block. This is relatively expensive information, so it 168 /// should only be used by rare constructs such as address-of-label. 169 unsigned getGlobalBasicBlockID(const BasicBlock *BB) const; 170 171 /// incorporateFunction/purgeFunction - If you'd like to deal with a function, 172 /// use these two methods to get its data into the ValueEnumerator! 173 /// 174 void incorporateFunction(const Function &F); 175 void purgeFunction(); 176 177 private: 178 void OptimizeConstants(unsigned CstStart, unsigned CstEnd); 179 180 void EnumerateMDNodeOperands(const MDNode *N); 181 void EnumerateMetadata(const Metadata *MD); 182 void EnumerateFunctionLocalMetadata(const LocalAsMetadata *Local); 183 void EnumerateNamedMDNode(const NamedMDNode *NMD); 184 void EnumerateValue(const Value *V); 185 void EnumerateType(Type *T); 186 void EnumerateOperandType(const Value *V); 187 void EnumerateAttributes(AttributeSet PAL); 188 189 void EnumerateValueSymbolTable(const ValueSymbolTable &ST); 190 void EnumerateNamedMetadata(const Module &M); 191 }; 192 193 } // End llvm namespace 194 195 #endif 196