Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 1 | //===-- Bitcode/Writer/ValueEnumerator.h - Number values --------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 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" |
Devang Patel | 6209869 | 2010-06-02 23:05:04 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Attributes.h" |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
| 22 | namespace llvm { |
| 23 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 24 | class Type; |
Chris Lattner | c59c0af | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 25 | class Value; |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 26 | class Instruction; |
Chris Lattner | c59c0af | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 27 | class BasicBlock; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 28 | class Function; |
Chris Lattner | c59c0af | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 29 | class Module; |
Devang Patel | 6209869 | 2010-06-02 23:05:04 +0000 | [diff] [blame] | 30 | class MDNode; |
Devang Patel | 8fba578 | 2010-01-09 00:30:14 +0000 | [diff] [blame] | 31 | class NamedMDNode; |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 32 | class AttributeSet; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 33 | class ValueSymbolTable; |
Devang Patel | 0386f01 | 2010-01-07 19:39:36 +0000 | [diff] [blame] | 34 | class MDSymbolTable; |
Chad Rosier | 4e6c03f | 2011-12-07 20:44:46 +0000 | [diff] [blame] | 35 | class raw_ostream; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 36 | |
Benjamin Kramer | 55c06ae | 2013-09-11 18:05:11 +0000 | [diff] [blame^] | 37 | class ValueEnumerator { |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 38 | public: |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 39 | typedef std::vector<Type*> TypeList; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 40 | |
| 41 | // For each value, we remember its Value* and occurrence frequency. |
| 42 | typedef std::vector<std::pair<const Value*, unsigned> > ValueList; |
| 43 | private: |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 44 | typedef DenseMap<Type*, unsigned> TypeMapType; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 45 | TypeMapType TypeMap; |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 46 | TypeList Types; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 47 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 48 | typedef DenseMap<const Value*, unsigned> ValueMapType; |
| 49 | ValueMapType ValueMap; |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 50 | ValueList Values; |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 51 | ValueList MDValues; |
Devang Patel | 6209869 | 2010-06-02 23:05:04 +0000 | [diff] [blame] | 52 | SmallVector<const MDNode *, 8> FunctionLocalMDs; |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 53 | ValueMapType MDValueMap; |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 54 | |
Bill Wendling | e9229a6 | 2013-02-11 22:33:26 +0000 | [diff] [blame] | 55 | typedef DenseMap<AttributeSet, unsigned> AttributeGroupMapType; |
| 56 | AttributeGroupMapType AttributeGroupMap; |
| 57 | std::vector<AttributeSet> AttributeGroups; |
Bill Wendling | 8c2e77f | 2013-02-10 23:06:02 +0000 | [diff] [blame] | 58 | |
Bill Wendling | 105ea3d | 2013-02-12 08:01:22 +0000 | [diff] [blame] | 59 | typedef DenseMap<AttributeSet, unsigned> AttributeMapType; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 60 | AttributeMapType AttributeMap; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 61 | std::vector<AttributeSet> Attribute; |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 837e04a | 2009-10-28 05:24:40 +0000 | [diff] [blame] | 63 | /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by |
| 64 | /// the "getGlobalBasicBlockID" method. |
| 65 | mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs; |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 66 | |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 67 | typedef DenseMap<const Instruction*, unsigned> InstructionMapType; |
| 68 | InstructionMapType InstructionMap; |
| 69 | unsigned InstructionCount; |
| 70 | |
Chris Lattner | c59c0af | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 71 | /// BasicBlocks - This contains all the basic blocks for the currently |
| 72 | /// incorporated function. Their reverse mapping is stored in ValueMap. |
| 73 | std::vector<const BasicBlock*> BasicBlocks; |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 75 | /// When a function is incorporated, this is the size of the Values list |
| 76 | /// before incorporation. |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 77 | unsigned NumModuleValues; |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 78 | |
| 79 | /// When a function is incorporated, this is the size of the MDValues list |
| 80 | /// before incorporation. |
| 81 | unsigned NumModuleMDValues; |
| 82 | |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 83 | unsigned FirstFuncConstantID; |
| 84 | unsigned FirstInstID; |
Craig Topper | 86a1c32 | 2012-09-15 17:09:36 +0000 | [diff] [blame] | 85 | |
| 86 | ValueEnumerator(const ValueEnumerator &) LLVM_DELETED_FUNCTION; |
| 87 | void operator=(const ValueEnumerator &) LLVM_DELETED_FUNCTION; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 88 | public: |
| 89 | ValueEnumerator(const Module *M); |
| 90 | |
Chad Rosier | 4e6c03f | 2011-12-07 20:44:46 +0000 | [diff] [blame] | 91 | void dump() const; |
| 92 | void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const; |
| 93 | |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 94 | unsigned getValueID(const Value *V) const; |
| 95 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 96 | unsigned getTypeID(Type *T) const { |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [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 | } |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 101 | |
| 102 | unsigned getInstructionID(const Instruction *I) const; |
| 103 | void setInstructionID(const Instruction *I); |
| 104 | |
Bill Wendling | e9229a6 | 2013-02-11 22:33:26 +0000 | [diff] [blame] | 105 | unsigned getAttributeID(AttributeSet PAL) const { |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 106 | if (PAL.isEmpty()) return 0; // Null maps to zero. |
Bill Wendling | 105ea3d | 2013-02-12 08:01:22 +0000 | [diff] [blame] | 107 | AttributeMapType::const_iterator I = AttributeMap.find(PAL); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 108 | assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!"); |
Chris Lattner | 50954f5 | 2007-05-03 22:46:43 +0000 | [diff] [blame] | 109 | return I->second; |
| 110 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 111 | |
Bill Wendling | e9229a6 | 2013-02-11 22:33:26 +0000 | [diff] [blame] | 112 | unsigned getAttributeGroupID(AttributeSet PAL) const { |
Bill Wendling | 8c2e77f | 2013-02-10 23:06:02 +0000 | [diff] [blame] | 113 | if (PAL.isEmpty()) return 0; // Null maps to zero. |
Bill Wendling | e9229a6 | 2013-02-11 22:33:26 +0000 | [diff] [blame] | 114 | AttributeGroupMapType::const_iterator I = AttributeGroupMap.find(PAL); |
| 115 | assert(I != AttributeGroupMap.end() && "Attribute not in ValueEnumerator!"); |
Bill Wendling | 8c2e77f | 2013-02-10 23:06:02 +0000 | [diff] [blame] | 116 | return I->second; |
| 117 | } |
| 118 | |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 119 | /// getFunctionConstantRange - Return the range of values that corresponds to |
| 120 | /// function-local constants. |
| 121 | void getFunctionConstantRange(unsigned &Start, unsigned &End) const { |
| 122 | Start = FirstFuncConstantID; |
| 123 | End = FirstInstID; |
| 124 | } |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 125 | |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 126 | const ValueList &getValues() const { return Values; } |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 127 | const ValueList &getMDValues() const { return MDValues; } |
Craig Topper | 9e639e8 | 2013-07-11 16:22:38 +0000 | [diff] [blame] | 128 | const SmallVectorImpl<const MDNode *> &getFunctionLocalMDValues() const { |
Devang Patel | 6209869 | 2010-06-02 23:05:04 +0000 | [diff] [blame] | 129 | return FunctionLocalMDs; |
| 130 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 131 | const TypeList &getTypes() const { return Types; } |
Chris Lattner | c59c0af | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 132 | const std::vector<const BasicBlock*> &getBasicBlocks() const { |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 133 | return BasicBlocks; |
Chris Lattner | c59c0af | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 134 | } |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 135 | const std::vector<AttributeSet> &getAttributes() const { |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 136 | return Attribute; |
Chris Lattner | 50954f5 | 2007-05-03 22:46:43 +0000 | [diff] [blame] | 137 | } |
Bill Wendling | e9229a6 | 2013-02-11 22:33:26 +0000 | [diff] [blame] | 138 | const std::vector<AttributeSet> &getAttributeGroups() const { |
| 139 | return AttributeGroups; |
Bill Wendling | 8c2e77f | 2013-02-10 23:06:02 +0000 | [diff] [blame] | 140 | } |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 141 | |
Chris Lattner | 837e04a | 2009-10-28 05:24:40 +0000 | [diff] [blame] | 142 | /// getGlobalBasicBlockID - This returns the function-specific ID for the |
| 143 | /// specified basic block. This is relatively expensive information, so it |
| 144 | /// should only be used by rare constructs such as address-of-label. |
| 145 | unsigned getGlobalBasicBlockID(const BasicBlock *BB) const; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 146 | |
Chad Rosier | 6a0c04d | 2011-06-03 17:02:19 +0000 | [diff] [blame] | 147 | /// incorporateFunction/purgeFunction - If you'd like to deal with a function, |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 148 | /// use these two methods to get its data into the ValueEnumerator! |
| 149 | /// |
Chad Rosier | 6a0c04d | 2011-06-03 17:02:19 +0000 | [diff] [blame] | 150 | void incorporateFunction(const Function &F); |
| 151 | void purgeFunction(); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 152 | |
| 153 | private: |
Chris Lattner | 6da91d3 | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 154 | void OptimizeConstants(unsigned CstStart, unsigned CstEnd); |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 155 | |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 156 | void EnumerateMDNodeOperands(const MDNode *N); |
Devang Patel | bc5201f | 2010-01-22 22:52:10 +0000 | [diff] [blame] | 157 | void EnumerateMetadata(const Value *MD); |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 158 | void EnumerateFunctionLocalMetadata(const MDNode *N); |
Devang Patel | 8fba578 | 2010-01-09 00:30:14 +0000 | [diff] [blame] | 159 | void EnumerateNamedMDNode(const NamedMDNode *NMD); |
Victor Hernandez | d7e6457 | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 160 | void EnumerateValue(const Value *V); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 161 | void EnumerateType(Type *T); |
Victor Hernandez | d7e6457 | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 162 | void EnumerateOperandType(const Value *V); |
Bill Wendling | 105ea3d | 2013-02-12 08:01:22 +0000 | [diff] [blame] | 163 | void EnumerateAttributes(AttributeSet PAL); |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 164 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 165 | void EnumerateValueSymbolTable(const ValueSymbolTable &ST); |
Dan Gohman | 17aa92c | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 166 | void EnumerateNamedMetadata(const Module *M); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | } // End llvm namespace |
| 170 | |
| 171 | #endif |