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