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" |
Devang Patel | eaf42ab | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 19 | #include "llvm/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; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 32 | class AttrListPtr; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 33 | class TypeSymbolTable; |
| 34 | class ValueSymbolTable; |
Devang Patel | 0386f01 | 2010-01-07 19:39:36 +0000 | [diff] [blame] | 35 | class MDSymbolTable; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 36 | |
| 37 | class ValueEnumerator { |
| 38 | public: |
Rafael Espindola | f5a9056 | 2011-04-06 16:49:37 +0000 | [diff] [blame] | 39 | typedef std::vector<const 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 | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 44 | typedef DenseMap<const Type*, unsigned> TypeMapType; |
| 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; |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 54 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 55 | typedef DenseMap<void*, unsigned> AttributeMapType; |
| 56 | AttributeMapType AttributeMap; |
| 57 | std::vector<AttrListPtr> Attributes; |
Chris Lattner | 50954f5 | 2007-05-03 22:46:43 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 837e04a | 2009-10-28 05:24:40 +0000 | [diff] [blame] | 59 | /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by |
| 60 | /// the "getGlobalBasicBlockID" method. |
| 61 | mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs; |
| 62 | |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 63 | typedef DenseMap<const Instruction*, unsigned> InstructionMapType; |
| 64 | InstructionMapType InstructionMap; |
| 65 | unsigned InstructionCount; |
| 66 | |
Chris Lattner | c59c0af | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 67 | /// BasicBlocks - This contains all the basic blocks for the currently |
| 68 | /// incorporated function. Their reverse mapping is stored in ValueMap. |
| 69 | std::vector<const BasicBlock*> BasicBlocks; |
| 70 | |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 71 | /// When a function is incorporated, this is the size of the Values list |
| 72 | /// before incorporation. |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 73 | unsigned NumModuleValues; |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 74 | |
| 75 | /// When a function is incorporated, this is the size of the MDValues list |
| 76 | /// before incorporation. |
| 77 | unsigned NumModuleMDValues; |
| 78 | |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 79 | unsigned FirstFuncConstantID; |
| 80 | unsigned FirstInstID; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 81 | |
| 82 | ValueEnumerator(const ValueEnumerator &); // DO NOT IMPLEMENT |
| 83 | void operator=(const ValueEnumerator &); // DO NOT IMPLEMENT |
| 84 | public: |
| 85 | ValueEnumerator(const Module *M); |
| 86 | |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 87 | unsigned getValueID(const Value *V) const; |
| 88 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 89 | unsigned getTypeID(const Type *T) const { |
| 90 | TypeMapType::const_iterator I = TypeMap.find(T); |
| 91 | assert(I != TypeMap.end() && "Type not in ValueEnumerator!"); |
| 92 | return I->second-1; |
| 93 | } |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 94 | |
| 95 | unsigned getInstructionID(const Instruction *I) const; |
| 96 | void setInstructionID(const Instruction *I); |
| 97 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 98 | unsigned getAttributeID(const AttrListPtr &PAL) const { |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 99 | if (PAL.isEmpty()) return 0; // Null maps to zero. |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 100 | AttributeMapType::const_iterator I = AttributeMap.find(PAL.getRawPointer()); |
| 101 | assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!"); |
Chris Lattner | 50954f5 | 2007-05-03 22:46:43 +0000 | [diff] [blame] | 102 | return I->second; |
| 103 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 104 | |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 105 | /// getFunctionConstantRange - Return the range of values that corresponds to |
| 106 | /// function-local constants. |
| 107 | void getFunctionConstantRange(unsigned &Start, unsigned &End) const { |
| 108 | Start = FirstFuncConstantID; |
| 109 | End = FirstInstID; |
| 110 | } |
| 111 | |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 112 | const ValueList &getValues() const { return Values; } |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 113 | const ValueList &getMDValues() const { return MDValues; } |
Devang Patel | 6209869 | 2010-06-02 23:05:04 +0000 | [diff] [blame] | 114 | const SmallVector<const MDNode *, 8> &getFunctionLocalMDValues() const { |
| 115 | return FunctionLocalMDs; |
| 116 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 117 | const TypeList &getTypes() const { return Types; } |
Chris Lattner | c59c0af | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 118 | const std::vector<const BasicBlock*> &getBasicBlocks() const { |
| 119 | return BasicBlocks; |
| 120 | } |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 121 | const std::vector<AttrListPtr> &getAttributes() const { |
| 122 | return Attributes; |
Chris Lattner | 50954f5 | 2007-05-03 22:46:43 +0000 | [diff] [blame] | 123 | } |
Chris Lattner | 837e04a | 2009-10-28 05:24:40 +0000 | [diff] [blame] | 124 | |
| 125 | /// getGlobalBasicBlockID - This returns the function-specific ID for the |
| 126 | /// specified basic block. This is relatively expensive information, so it |
| 127 | /// should only be used by rare constructs such as address-of-label. |
| 128 | unsigned getGlobalBasicBlockID(const BasicBlock *BB) const; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 129 | |
Chad Rosier | 6a0c04d | 2011-06-03 17:02:19 +0000 | [diff] [blame] | 130 | /// incorporateFunction/purgeFunction - If you'd like to deal with a function, |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 131 | /// use these two methods to get its data into the ValueEnumerator! |
| 132 | /// |
Chad Rosier | 6a0c04d | 2011-06-03 17:02:19 +0000 | [diff] [blame] | 133 | void incorporateFunction(const Function &F); |
| 134 | void purgeFunction(); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 135 | |
| 136 | private: |
Chris Lattner | 6da91d3 | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 137 | void OptimizeConstants(unsigned CstStart, unsigned CstEnd); |
Rafael Espindola | f5a9056 | 2011-04-06 16:49:37 +0000 | [diff] [blame] | 138 | void OptimizeTypes(); |
Chris Lattner | 6da91d3 | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 139 | |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 140 | void EnumerateMDNodeOperands(const MDNode *N); |
Devang Patel | bc5201f | 2010-01-22 22:52:10 +0000 | [diff] [blame] | 141 | void EnumerateMetadata(const Value *MD); |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 142 | void EnumerateFunctionLocalMetadata(const MDNode *N); |
Devang Patel | 8fba578 | 2010-01-09 00:30:14 +0000 | [diff] [blame] | 143 | void EnumerateNamedMDNode(const NamedMDNode *NMD); |
Victor Hernandez | d7e6457 | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 144 | void EnumerateValue(const Value *V); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 145 | void EnumerateType(const Type *T); |
Victor Hernandez | d7e6457 | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 146 | void EnumerateOperandType(const Value *V); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 147 | void EnumerateAttributes(const AttrListPtr &PAL); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 148 | |
| 149 | void EnumerateTypeSymbolTable(const TypeSymbolTable &ST); |
| 150 | void EnumerateValueSymbolTable(const ValueSymbolTable &ST); |
Dan Gohman | 17aa92c | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 151 | void EnumerateNamedMetadata(const Module *M); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | } // End llvm namespace |
| 155 | |
| 156 | #endif |