Chris Lattner | db9b998 | 2004-01-20 19:50:12 +0000 | [diff] [blame] | 1 | //===-- Analysis/SlotCalculator.h - Calculate value slots -------*- C++ -*-===// |
John Criswell | 6fbcc26 | 2003-10-20 20:19:47 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 10 | // This class calculates the slots that values will land in. This is useful for |
| 11 | // when writing bytecode or assembly out, because you have to know these things. |
| 12 | // |
| 13 | // Specifically, this class calculates the "type plane numbering" that you see |
| 14 | // for a function if you strip out all of the symbols in it. For assembly |
| 15 | // writing, this is used when a symbol does not have a name. For bytecode |
| 16 | // writing, this is always used, and the symbol table is added on later. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Chris Lattner | db9b998 | 2004-01-20 19:50:12 +0000 | [diff] [blame] | 20 | #ifndef LLVM_ANALYSIS_SLOTCALCULATOR_H |
| 21 | #define LLVM_ANALYSIS_SLOTCALCULATOR_H |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 22 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 23 | #include <vector> |
| 24 | #include <map> |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | |
| 26 | namespace llvm { |
| 27 | |
Chris Lattner | 644dc17 | 2001-07-14 06:08:51 +0000 | [diff] [blame] | 28 | class Value; |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 29 | class Type; |
Chris Lattner | 4c4007b | 2001-09-07 16:27:05 +0000 | [diff] [blame] | 30 | class Module; |
Chris Lattner | e7506a3 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 31 | class Function; |
Chris Lattner | 184b2fa | 2002-04-09 18:35:38 +0000 | [diff] [blame] | 32 | class SymbolTable; |
Chris Lattner | 7851e1b | 2004-01-14 23:37:43 +0000 | [diff] [blame] | 33 | class ConstantArray; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 4c4007b | 2001-09-07 16:27:05 +0000 | [diff] [blame] | 35 | class SlotCalculator { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 36 | const Module *TheModule; |
Chris Lattner | 8ce7501 | 2004-01-14 02:49:34 +0000 | [diff] [blame] | 37 | |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 38 | typedef std::vector<const Type*> TypeList; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 39 | typedef std::vector<const Value*> TypePlane; |
| 40 | std::vector<TypePlane> Table; |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 41 | TypeList Types; |
| 42 | typedef std::map<const Value*, unsigned> NodeMapType; |
| 43 | NodeMapType NodeMap; |
| 44 | |
| 45 | typedef std::map<const Type*, unsigned> TypeMapType; |
| 46 | TypeMapType TypeMap; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 7851e1b | 2004-01-14 23:37:43 +0000 | [diff] [blame] | 48 | /// ConstantStrings - If we are indexing for a bytecode file, this keeps track |
| 49 | /// of all of the constants strings that need to be emitted. |
| 50 | std::vector<const ConstantArray*> ConstantStrings; |
| 51 | |
| 52 | /// ModuleLevel - Used to keep track of which values belong to the module, |
| 53 | /// and which values belong to the currently incorporated function. |
| 54 | /// |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 55 | std::vector<unsigned> ModuleLevel; |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 56 | unsigned ModuleTypeLevel; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 57 | |
Chris Lattner | af894e9 | 2004-01-18 21:03:49 +0000 | [diff] [blame] | 58 | /// ModuleContainsAllFunctionConstants - This flag is set to true if all |
| 59 | /// function constants are incorporated into the module constant table. This |
| 60 | /// is only possible if building information for a bytecode file. |
| 61 | bool ModuleContainsAllFunctionConstants; |
| 62 | |
| 63 | /// CompactionTable/NodeMap - When function compaction has been performed, |
| 64 | /// these entries provide a compacted view of the namespace needed to emit |
| 65 | /// instructions in a function body. The 'getSlot()' method automatically |
| 66 | /// returns these entries if applicable, or the global entries if not. |
| 67 | std::vector<TypePlane> CompactionTable; |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 68 | TypeList CompactionTypes; |
| 69 | typedef std::map<const Value*, unsigned> CompactionNodeMapType; |
| 70 | CompactionNodeMapType CompactionNodeMap; |
| 71 | typedef std::map<const Type*, unsigned> CompactionTypeMapType; |
| 72 | CompactionTypeMapType CompactionTypeMap; |
Chris Lattner | af894e9 | 2004-01-18 21:03:49 +0000 | [diff] [blame] | 73 | |
| 74 | SlotCalculator(const SlotCalculator &); // DO NOT IMPLEMENT |
| 75 | void operator=(const SlotCalculator &); // DO NOT IMPLEMENT |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 76 | public: |
Reid Spencer | 798ff64 | 2004-05-26 07:37:11 +0000 | [diff] [blame] | 77 | SlotCalculator(const Module *M ); |
Chris Lattner | e7506a3 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 78 | // Start out in incorp state |
Reid Spencer | 798ff64 | 2004-05-26 07:37:11 +0000 | [diff] [blame] | 79 | SlotCalculator(const Function *F ); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 80 | |
Chris Lattner | af894e9 | 2004-01-18 21:03:49 +0000 | [diff] [blame] | 81 | /// getSlot - Return the slot number of the specified value in it's type |
| 82 | /// plane. This returns < 0 on error! |
Chris Lattner | 7851e1b | 2004-01-14 23:37:43 +0000 | [diff] [blame] | 83 | /// |
Chris Lattner | af894e9 | 2004-01-18 21:03:49 +0000 | [diff] [blame] | 84 | int getSlot(const Value *V) const; |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 85 | int getSlot(const Type* T) const; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 86 | |
Chris Lattner | af894e9 | 2004-01-18 21:03:49 +0000 | [diff] [blame] | 87 | /// getGlobalSlot - Return a slot number from the global table. This can only |
| 88 | /// be used when a compaction table is active. |
| 89 | unsigned getGlobalSlot(const Value *V) const; |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 90 | unsigned getGlobalSlot(const Type *V) const; |
Chris Lattner | af894e9 | 2004-01-18 21:03:49 +0000 | [diff] [blame] | 91 | |
| 92 | inline unsigned getNumPlanes() const { |
| 93 | if (CompactionTable.empty()) |
| 94 | return Table.size(); |
| 95 | else |
| 96 | return CompactionTable.size(); |
| 97 | } |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 98 | |
| 99 | inline unsigned getNumTypes() const { |
| 100 | if (CompactionTypes.empty()) |
| 101 | return Types.size(); |
| 102 | else |
| 103 | return CompactionTypes.size(); |
| 104 | } |
| 105 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 106 | inline unsigned getModuleLevel(unsigned Plane) const { |
| 107 | return Plane < ModuleLevel.size() ? ModuleLevel[Plane] : 0; |
| 108 | } |
| 109 | |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 110 | /// Returns the number of types in the type list that are at module level |
| 111 | inline unsigned getModuleTypeLevel() const { |
| 112 | return ModuleTypeLevel; |
| 113 | } |
| 114 | |
Chris Lattner | a2b4f93 | 2004-01-20 00:54:47 +0000 | [diff] [blame] | 115 | TypePlane &getPlane(unsigned Plane); |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 116 | TypeList& getTypes() { |
| 117 | if (!CompactionTypes.empty()) |
| 118 | return CompactionTypes; |
| 119 | return Types; |
| 120 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 7851e1b | 2004-01-14 23:37:43 +0000 | [diff] [blame] | 122 | /// incorporateFunction/purgeFunction - If you'd like to deal with a function, |
| 123 | /// use these two methods to get its data into the SlotCalculator! |
| 124 | /// |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 125 | void incorporateFunction(const Function *F); |
| 126 | void purgeFunction(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 127 | |
Chris Lattner | 7851e1b | 2004-01-14 23:37:43 +0000 | [diff] [blame] | 128 | /// string_iterator/string_begin/end - Access the list of module-level |
| 129 | /// constant strings that have been incorporated. This is only applicable to |
| 130 | /// bytecode files. |
| 131 | typedef std::vector<const ConstantArray*>::const_iterator string_iterator; |
| 132 | string_iterator string_begin() const { return ConstantStrings.begin(); } |
| 133 | string_iterator string_end() const { return ConstantStrings.end(); } |
| 134 | |
Chris Lattner | af894e9 | 2004-01-18 21:03:49 +0000 | [diff] [blame] | 135 | const std::vector<TypePlane> &getCompactionTable() const { |
| 136 | return CompactionTable; |
| 137 | } |
Chris Lattner | 7851e1b | 2004-01-14 23:37:43 +0000 | [diff] [blame] | 138 | |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 139 | const TypeList& getCompactionTypes() const { return CompactionTypes; } |
| 140 | |
Reid Spencer | 07ea191 | 2004-08-26 22:32:00 +0000 | [diff] [blame] | 141 | /// @brief Determine if the compaction table (not types) is empty |
| 142 | bool CompactionTableIsEmpty() const; |
| 143 | |
Chris Lattner | af894e9 | 2004-01-18 21:03:49 +0000 | [diff] [blame] | 144 | private: |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 145 | // getOrCreateSlot - Values can be crammed into here at will... if |
| 146 | // they haven't been inserted already, they get inserted, otherwise |
| 147 | // they are ignored. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 148 | // |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 149 | int getOrCreateSlot(const Value *D); |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 150 | int getOrCreateSlot(const Type* T); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 151 | |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 152 | // insertValue - Insert a value into the value table... Return the |
| 153 | // slot that it occupies, or -1 if the declaration is to be ignored |
| 154 | // because of the IgnoreNamedNodes flag. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 155 | // |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 156 | int insertValue(const Value *D, bool dontIgnore = false); |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 157 | int insertType(const Type* T, bool dontIgnore = false ); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 158 | |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 159 | // doInsertValue - Small helper function to be called only be insertVal. |
| 160 | int doInsertValue(const Value *D); |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 161 | int doInsertType(const Type*T); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 162 | |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 163 | // processModule - Process all of the module level function declarations and |
Chris Lattner | 4c4007b | 2001-09-07 16:27:05 +0000 | [diff] [blame] | 164 | // types that are available. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 165 | // |
Chris Lattner | 4c4007b | 2001-09-07 16:27:05 +0000 | [diff] [blame] | 166 | void processModule(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 4c4007b | 2001-09-07 16:27:05 +0000 | [diff] [blame] | 168 | // processSymbolTable - Insert all of the values in the specified symbol table |
| 169 | // into the values table... |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 170 | // |
Chris Lattner | 4c4007b | 2001-09-07 16:27:05 +0000 | [diff] [blame] | 171 | void processSymbolTable(const SymbolTable *ST); |
| 172 | void processSymbolTableConstants(const SymbolTable *ST); |
Chris Lattner | af894e9 | 2004-01-18 21:03:49 +0000 | [diff] [blame] | 173 | |
| 174 | void buildCompactionTable(const Function *F); |
| 175 | unsigned getOrCreateCompactionTableSlot(const Value *V); |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 176 | unsigned getOrCreateCompactionTableSlot(const Type *V); |
Chris Lattner | a2b4f93 | 2004-01-20 00:54:47 +0000 | [diff] [blame] | 177 | void pruneCompactionTable(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 180 | } // End llvm namespace |
| 181 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 182 | #endif |