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; |
Chris Lattner | 4c4007b | 2001-09-07 16:27:05 +0000 | [diff] [blame] | 29 | class Module; |
Chris Lattner | e7506a3 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 30 | class Function; |
Chris Lattner | 184b2fa | 2002-04-09 18:35:38 +0000 | [diff] [blame] | 31 | class SymbolTable; |
Chris Lattner | 7851e1b | 2004-01-14 23:37:43 +0000 | [diff] [blame] | 32 | class ConstantArray; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 4c4007b | 2001-09-07 16:27:05 +0000 | [diff] [blame] | 34 | class SlotCalculator { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 35 | const Module *TheModule; |
Chris Lattner | 8ce7501 | 2004-01-14 02:49:34 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 37 | typedef std::vector<const Value*> TypePlane; |
| 38 | std::vector<TypePlane> Table; |
Chris Lattner | 7851e1b | 2004-01-14 23:37:43 +0000 | [diff] [blame] | 39 | std::map<const Value*, unsigned> NodeMap; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 7851e1b | 2004-01-14 23:37:43 +0000 | [diff] [blame] | 41 | /// ConstantStrings - If we are indexing for a bytecode file, this keeps track |
| 42 | /// of all of the constants strings that need to be emitted. |
| 43 | std::vector<const ConstantArray*> ConstantStrings; |
| 44 | |
| 45 | /// ModuleLevel - Used to keep track of which values belong to the module, |
| 46 | /// and which values belong to the currently incorporated function. |
| 47 | /// |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 48 | std::vector<unsigned> ModuleLevel; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 49 | |
Chris Lattner | af894e9 | 2004-01-18 21:03:49 +0000 | [diff] [blame] | 50 | /// ModuleContainsAllFunctionConstants - This flag is set to true if all |
| 51 | /// function constants are incorporated into the module constant table. This |
| 52 | /// is only possible if building information for a bytecode file. |
| 53 | bool ModuleContainsAllFunctionConstants; |
| 54 | |
| 55 | /// CompactionTable/NodeMap - When function compaction has been performed, |
| 56 | /// these entries provide a compacted view of the namespace needed to emit |
| 57 | /// instructions in a function body. The 'getSlot()' method automatically |
| 58 | /// returns these entries if applicable, or the global entries if not. |
| 59 | std::vector<TypePlane> CompactionTable; |
| 60 | std::map<const Value*, unsigned> CompactionNodeMap; |
| 61 | |
| 62 | SlotCalculator(const SlotCalculator &); // DO NOT IMPLEMENT |
| 63 | void operator=(const SlotCalculator &); // DO NOT IMPLEMENT |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 64 | public: |
Reid Spencer | 798ff64 | 2004-05-26 07:37:11 +0000 | [diff] [blame^] | 65 | SlotCalculator(const Module *M ); |
Chris Lattner | e7506a3 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 66 | // Start out in incorp state |
Reid Spencer | 798ff64 | 2004-05-26 07:37:11 +0000 | [diff] [blame^] | 67 | SlotCalculator(const Function *F ); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 68 | |
Chris Lattner | af894e9 | 2004-01-18 21:03:49 +0000 | [diff] [blame] | 69 | /// getSlot - Return the slot number of the specified value in it's type |
| 70 | /// plane. This returns < 0 on error! |
Chris Lattner | 7851e1b | 2004-01-14 23:37:43 +0000 | [diff] [blame] | 71 | /// |
Chris Lattner | af894e9 | 2004-01-18 21:03:49 +0000 | [diff] [blame] | 72 | int getSlot(const Value *V) const; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 73 | |
Chris Lattner | af894e9 | 2004-01-18 21:03:49 +0000 | [diff] [blame] | 74 | /// getGlobalSlot - Return a slot number from the global table. This can only |
| 75 | /// be used when a compaction table is active. |
| 76 | unsigned getGlobalSlot(const Value *V) const; |
| 77 | |
| 78 | inline unsigned getNumPlanes() const { |
| 79 | if (CompactionTable.empty()) |
| 80 | return Table.size(); |
| 81 | else |
| 82 | return CompactionTable.size(); |
| 83 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 84 | inline unsigned getModuleLevel(unsigned Plane) const { |
| 85 | return Plane < ModuleLevel.size() ? ModuleLevel[Plane] : 0; |
| 86 | } |
| 87 | |
Chris Lattner | a2b4f93 | 2004-01-20 00:54:47 +0000 | [diff] [blame] | 88 | TypePlane &getPlane(unsigned Plane); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 7851e1b | 2004-01-14 23:37:43 +0000 | [diff] [blame] | 90 | /// incorporateFunction/purgeFunction - If you'd like to deal with a function, |
| 91 | /// use these two methods to get its data into the SlotCalculator! |
| 92 | /// |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 93 | void incorporateFunction(const Function *F); |
| 94 | void purgeFunction(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 7851e1b | 2004-01-14 23:37:43 +0000 | [diff] [blame] | 96 | /// string_iterator/string_begin/end - Access the list of module-level |
| 97 | /// constant strings that have been incorporated. This is only applicable to |
| 98 | /// bytecode files. |
| 99 | typedef std::vector<const ConstantArray*>::const_iterator string_iterator; |
| 100 | string_iterator string_begin() const { return ConstantStrings.begin(); } |
| 101 | string_iterator string_end() const { return ConstantStrings.end(); } |
| 102 | |
Chris Lattner | af894e9 | 2004-01-18 21:03:49 +0000 | [diff] [blame] | 103 | const std::vector<TypePlane> &getCompactionTable() const { |
| 104 | return CompactionTable; |
| 105 | } |
Chris Lattner | 7851e1b | 2004-01-14 23:37:43 +0000 | [diff] [blame] | 106 | |
Chris Lattner | af894e9 | 2004-01-18 21:03:49 +0000 | [diff] [blame] | 107 | private: |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 108 | // getOrCreateSlot - Values can be crammed into here at will... if |
| 109 | // they haven't been inserted already, they get inserted, otherwise |
| 110 | // they are ignored. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 111 | // |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 112 | int getOrCreateSlot(const Value *D); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 113 | |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 114 | // insertValue - Insert a value into the value table... Return the |
| 115 | // slot that it occupies, or -1 if the declaration is to be ignored |
| 116 | // because of the IgnoreNamedNodes flag. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 117 | // |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 118 | int insertValue(const Value *D, bool dontIgnore = false); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 119 | |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 120 | // doInsertValue - Small helper function to be called only be insertVal. |
| 121 | int doInsertValue(const Value *D); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 122 | |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 123 | // processModule - Process all of the module level function declarations and |
Chris Lattner | 4c4007b | 2001-09-07 16:27:05 +0000 | [diff] [blame] | 124 | // types that are available. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 125 | // |
Chris Lattner | 4c4007b | 2001-09-07 16:27:05 +0000 | [diff] [blame] | 126 | void processModule(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 127 | |
Chris Lattner | 4c4007b | 2001-09-07 16:27:05 +0000 | [diff] [blame] | 128 | // processSymbolTable - Insert all of the values in the specified symbol table |
| 129 | // into the values table... |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 130 | // |
Chris Lattner | 4c4007b | 2001-09-07 16:27:05 +0000 | [diff] [blame] | 131 | void processSymbolTable(const SymbolTable *ST); |
| 132 | void processSymbolTableConstants(const SymbolTable *ST); |
Chris Lattner | af894e9 | 2004-01-18 21:03:49 +0000 | [diff] [blame] | 133 | |
| 134 | void buildCompactionTable(const Function *F); |
| 135 | unsigned getOrCreateCompactionTableSlot(const Value *V); |
Chris Lattner | a2b4f93 | 2004-01-20 00:54:47 +0000 | [diff] [blame] | 136 | void pruneCompactionTable(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 139 | } // End llvm namespace |
| 140 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 141 | #endif |