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