Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 1 | //===-- Bitcode/Writer/ValueEnumerator.h - Number values --------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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 | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 22 | class Type; |
Chris Lattner | 7c37b01 | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 23 | class Value; |
| 24 | class BasicBlock; |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 25 | class Function; |
Chris Lattner | 7c37b01 | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 26 | class Module; |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 27 | class TypeSymbolTable; |
| 28 | class ValueSymbolTable; |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 29 | |
| 30 | class ValueEnumerator { |
| 31 | public: |
| 32 | // For each type, we remember its Type* and occurrence frequency. |
| 33 | typedef std::vector<std::pair<const Type*, unsigned> > TypeList; |
| 34 | |
| 35 | // For each value, we remember its Value* and occurrence frequency. |
| 36 | typedef std::vector<std::pair<const Value*, unsigned> > ValueList; |
| 37 | private: |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 38 | typedef DenseMap<const Type*, unsigned> TypeMapType; |
| 39 | TypeMapType TypeMap; |
Chris Lattner | 5252356 | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 40 | TypeList Types; |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 41 | |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 42 | typedef DenseMap<const Value*, unsigned> ValueMapType; |
| 43 | ValueMapType ValueMap; |
Chris Lattner | 5252356 | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 44 | ValueList Values; |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 45 | |
Chris Lattner | 7c37b01 | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 46 | /// BasicBlocks - This contains all the basic blocks for the currently |
| 47 | /// incorporated function. Their reverse mapping is stored in ValueMap. |
| 48 | std::vector<const BasicBlock*> BasicBlocks; |
| 49 | |
Chris Lattner | 5f640b9 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 50 | /// When a function is incorporated, this is the size of the Values list |
| 51 | /// before incorporation. |
| 52 | unsigned ModuleLevel; |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 53 | |
| 54 | ValueEnumerator(const ValueEnumerator &); // DO NOT IMPLEMENT |
| 55 | void operator=(const ValueEnumerator &); // DO NOT IMPLEMENT |
| 56 | public: |
| 57 | ValueEnumerator(const Module *M); |
| 58 | |
| 59 | unsigned getValueID(const Value *V) const { |
| 60 | ValueMapType::const_iterator I = ValueMap.find(V); |
| 61 | assert(I != ValueMap.end() && "Value not in slotcalculator!"); |
Chris Lattner | 1e93f5b | 2007-04-23 21:23:41 +0000 | [diff] [blame] | 62 | return I->second-1; |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | unsigned getTypeID(const Type *T) const { |
| 66 | TypeMapType::const_iterator I = TypeMap.find(T); |
| 67 | assert(I != TypeMap.end() && "Type not in ValueEnumerator!"); |
| 68 | return I->second-1; |
| 69 | } |
| 70 | |
Chris Lattner | 5252356 | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 71 | const ValueList &getValues() const { return Values; } |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 72 | const TypeList &getTypes() const { return Types; } |
Chris Lattner | 7c37b01 | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 73 | const std::vector<const BasicBlock*> &getBasicBlocks() const { |
| 74 | return BasicBlocks; |
| 75 | } |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 831d420 | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 77 | /// PurgeAggregateValues - If there are any aggregate values at the end of the |
| 78 | /// value list, remove them and return the count of the remaining values. If |
| 79 | /// there are none, return -1. |
| 80 | int PurgeAggregateValues(); |
| 81 | |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 82 | /// incorporateFunction/purgeFunction - If you'd like to deal with a function, |
| 83 | /// use these two methods to get its data into the ValueEnumerator! |
| 84 | /// |
Chris Lattner | 5f640b9 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 85 | void incorporateFunction(const Function &F); |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 86 | void purgeFunction(); |
| 87 | |
| 88 | private: |
| 89 | void EnumerateValue(const Value *V); |
| 90 | void EnumerateType(const Type *T); |
| 91 | |
| 92 | void EnumerateTypeSymbolTable(const TypeSymbolTable &ST); |
| 93 | void EnumerateValueSymbolTable(const ValueSymbolTable &ST); |
| 94 | }; |
| 95 | |
| 96 | } // End llvm namespace |
| 97 | |
| 98 | #endif |