Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- Bitcode/Writer/ValueEnumerator.h - Number values --------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 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. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +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 | e480dfa | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 18 | #include "llvm/Attributes.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
| 21 | namespace llvm { |
| 22 | |
| 23 | class Type; |
| 24 | class Value; |
| 25 | class BasicBlock; |
| 26 | class Function; |
| 27 | class Module; |
Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 28 | class AttrListPtr; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 29 | class TypeSymbolTable; |
| 30 | class ValueSymbolTable; |
| 31 | |
| 32 | class ValueEnumerator { |
| 33 | public: |
| 34 | // For each type, we remember its Type* and occurrence frequency. |
| 35 | typedef std::vector<std::pair<const Type*, unsigned> > TypeList; |
| 36 | |
| 37 | // For each value, we remember its Value* and occurrence frequency. |
| 38 | typedef std::vector<std::pair<const Value*, unsigned> > ValueList; |
| 39 | private: |
| 40 | typedef DenseMap<const Type*, unsigned> TypeMapType; |
| 41 | TypeMapType TypeMap; |
| 42 | TypeList Types; |
| 43 | |
| 44 | typedef DenseMap<const Value*, unsigned> ValueMapType; |
| 45 | ValueMapType ValueMap; |
| 46 | ValueList Values; |
| 47 | |
Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 48 | typedef DenseMap<void*, unsigned> AttributeMapType; |
| 49 | AttributeMapType AttributeMap; |
| 50 | std::vector<AttrListPtr> Attributes; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 51 | |
| 52 | /// BasicBlocks - This contains all the basic blocks for the currently |
| 53 | /// incorporated function. Their reverse mapping is stored in ValueMap. |
| 54 | std::vector<const BasicBlock*> BasicBlocks; |
| 55 | |
| 56 | /// When a function is incorporated, this is the size of the Values list |
| 57 | /// before incorporation. |
| 58 | unsigned NumModuleValues; |
| 59 | unsigned FirstFuncConstantID; |
| 60 | unsigned FirstInstID; |
| 61 | |
| 62 | ValueEnumerator(const ValueEnumerator &); // DO NOT IMPLEMENT |
| 63 | void operator=(const ValueEnumerator &); // DO NOT IMPLEMENT |
| 64 | public: |
| 65 | ValueEnumerator(const Module *M); |
| 66 | |
| 67 | unsigned getValueID(const Value *V) const { |
| 68 | ValueMapType::const_iterator I = ValueMap.find(V); |
| 69 | assert(I != ValueMap.end() && "Value not in slotcalculator!"); |
| 70 | return I->second-1; |
| 71 | } |
| 72 | |
| 73 | unsigned getTypeID(const Type *T) const { |
| 74 | TypeMapType::const_iterator I = TypeMap.find(T); |
| 75 | assert(I != TypeMap.end() && "Type not in ValueEnumerator!"); |
| 76 | return I->second-1; |
| 77 | } |
| 78 | |
Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 79 | unsigned getAttributeID(const AttrListPtr &PAL) const { |
Chris Lattner | 1c8733e | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 80 | if (PAL.isEmpty()) return 0; // Null maps to zero. |
Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 81 | AttributeMapType::const_iterator I = AttributeMap.find(PAL.getRawPointer()); |
| 82 | assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 83 | return I->second; |
| 84 | } |
| 85 | |
| 86 | /// getFunctionConstantRange - Return the range of values that corresponds to |
| 87 | /// function-local constants. |
| 88 | void getFunctionConstantRange(unsigned &Start, unsigned &End) const { |
| 89 | Start = FirstFuncConstantID; |
| 90 | End = FirstInstID; |
| 91 | } |
| 92 | |
| 93 | const ValueList &getValues() const { return Values; } |
| 94 | const TypeList &getTypes() const { return Types; } |
| 95 | const std::vector<const BasicBlock*> &getBasicBlocks() const { |
| 96 | return BasicBlocks; |
| 97 | } |
Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 98 | const std::vector<AttrListPtr> &getAttributes() const { |
| 99 | return Attributes; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /// PurgeAggregateValues - If there are any aggregate values at the end of the |
| 103 | /// value list, remove them and return the count of the remaining values. If |
| 104 | /// there are none, return -1. |
| 105 | int PurgeAggregateValues(); |
| 106 | |
| 107 | /// incorporateFunction/purgeFunction - If you'd like to deal with a function, |
| 108 | /// use these two methods to get its data into the ValueEnumerator! |
| 109 | /// |
| 110 | void incorporateFunction(const Function &F); |
| 111 | void purgeFunction(); |
| 112 | |
| 113 | private: |
| 114 | void OptimizeConstants(unsigned CstStart, unsigned CstEnd); |
| 115 | |
| 116 | void EnumerateValue(const Value *V); |
| 117 | void EnumerateType(const Type *T); |
| 118 | void EnumerateOperandType(const Value *V); |
Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 119 | void EnumerateAttributes(const AttrListPtr &PAL); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 120 | |
| 121 | void EnumerateTypeSymbolTable(const TypeSymbolTable &ST); |
| 122 | void EnumerateValueSymbolTable(const ValueSymbolTable &ST); |
| 123 | }; |
| 124 | |
| 125 | } // End llvm namespace |
| 126 | |
| 127 | #endif |