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