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