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