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