Chris Lattner | cf3056d | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===-- SlotCalculator.cpp - Calculate what slots values land in ----------===// |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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. |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 10 | // This file implements a useful analysis step to figure out what numbered slots |
| 11 | // values in a program will land in (keeping track of per plane information). |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 12 | // |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 13 | // This is used when writing a file to disk, either in bytecode or assembly. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 17 | #include "SlotCalculator.h" |
Chris Lattner | dcea630 | 2004-01-14 23:34:39 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 19 | #include "llvm/DerivedTypes.h" |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 20 | #include "llvm/Function.h" |
Chris Lattner | 3bc5a60 | 2006-01-25 23:08:15 +0000 | [diff] [blame] | 21 | #include "llvm/InlineAsm.h" |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 22 | #include "llvm/Instructions.h" |
Chris Lattner | dcea630 | 2004-01-14 23:34:39 +0000 | [diff] [blame] | 23 | #include "llvm/Module.h" |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 24 | #include "llvm/SymbolTable.h" |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 25 | #include "llvm/Type.h" |
Chris Lattner | f2d577b | 2004-01-20 19:50:34 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/ConstantsScanner.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/PostOrderIterator.h" |
| 28 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 29 | #include <algorithm> |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 30 | #include <functional> |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 32 | |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 33 | #if 0 |
Bill Wendling | 68fe61d | 2006-11-29 00:19:40 +0000 | [diff] [blame^] | 34 | #include "llvm/Support/Streams.h" |
| 35 | #define SC_DEBUG(X) llvm_cerr << X |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 36 | #else |
| 37 | #define SC_DEBUG(X) |
| 38 | #endif |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 39 | |
Reid Spencer | 798ff64 | 2004-05-26 07:37:11 +0000 | [diff] [blame] | 40 | SlotCalculator::SlotCalculator(const Module *M ) { |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 41 | ModuleContainsAllFunctionConstants = false; |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 42 | ModuleTypeLevel = 0; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 43 | TheModule = M; |
| 44 | |
| 45 | // Preload table... Make sure that all of the primitive types are in the table |
| 46 | // and that their Primitive ID is equal to their slot # |
| 47 | // |
Alkis Evlogimenos | 5d1bdcd | 2003-10-29 03:12:12 +0000 | [diff] [blame] | 48 | SC_DEBUG("Inserting primitive types:\n"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 49 | for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 50 | assert(Type::getPrimitiveType((Type::TypeID)i)); |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 51 | insertType(Type::getPrimitiveType((Type::TypeID)i), true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | if (M == 0) return; // Empty table... |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 55 | processModule(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Reid Spencer | 798ff64 | 2004-05-26 07:37:11 +0000 | [diff] [blame] | 58 | SlotCalculator::SlotCalculator(const Function *M ) { |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 59 | ModuleContainsAllFunctionConstants = false; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 60 | TheModule = M ? M->getParent() : 0; |
| 61 | |
| 62 | // Preload table... Make sure that all of the primitive types are in the table |
| 63 | // and that their Primitive ID is equal to their slot # |
| 64 | // |
Alkis Evlogimenos | 5d1bdcd | 2003-10-29 03:12:12 +0000 | [diff] [blame] | 65 | SC_DEBUG("Inserting primitive types:\n"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 66 | for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 67 | assert(Type::getPrimitiveType((Type::TypeID)i)); |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 68 | insertType(Type::getPrimitiveType((Type::TypeID)i), true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | if (TheModule == 0) return; // Empty table... |
| 72 | |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 73 | processModule(); // Process module level stuff |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 74 | incorporateFunction(M); // Start out in incorporated state |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 77 | unsigned SlotCalculator::getGlobalSlot(const Value *V) const { |
| 78 | assert(!CompactionTable.empty() && |
| 79 | "This method can only be used when compaction is enabled!"); |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 80 | std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(V); |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 81 | assert(I != NodeMap.end() && "Didn't find global slot entry!"); |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 82 | return I->second; |
| 83 | } |
| 84 | |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 85 | unsigned SlotCalculator::getGlobalSlot(const Type* T) const { |
| 86 | std::map<const Type*, unsigned>::const_iterator I = TypeMap.find(T); |
| 87 | assert(I != TypeMap.end() && "Didn't find global slot entry!"); |
| 88 | return I->second; |
| 89 | } |
| 90 | |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 91 | SlotCalculator::TypePlane &SlotCalculator::getPlane(unsigned Plane) { |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 92 | if (CompactionTable.empty()) { // No compaction table active? |
| 93 | // fall out |
| 94 | } else if (!CompactionTable[Plane].empty()) { // Compaction table active. |
| 95 | assert(Plane < CompactionTable.size()); |
| 96 | return CompactionTable[Plane]; |
| 97 | } else { |
| 98 | // Final case: compaction table active, but this plane is not |
| 99 | // compactified. If the type plane is compactified, unmap back to the |
| 100 | // global type plane corresponding to "Plane". |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 101 | if (!CompactionTypes.empty()) { |
| 102 | const Type *Ty = CompactionTypes[Plane]; |
| 103 | TypeMapType::iterator It = TypeMap.find(Ty); |
| 104 | assert(It != TypeMap.end() && "Type not in global constant map?"); |
| 105 | Plane = It->second; |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | // Okay we are just returning an entry out of the main Table. Make sure the |
| 110 | // plane exists and return it. |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 111 | if (Plane >= Table.size()) |
| 112 | Table.resize(Plane+1); |
| 113 | return Table[Plane]; |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 114 | } |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 115 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 116 | // processModule - Process all of the module level function declarations and |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 117 | // types that are available. |
| 118 | // |
| 119 | void SlotCalculator::processModule() { |
| 120 | SC_DEBUG("begin processModule!\n"); |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 122 | // Add all of the global variables to the value table... |
Chris Lattner | f1fef65 | 2001-10-13 06:35:09 +0000 | [diff] [blame] | 123 | // |
Chris Lattner | 9e60d8d | 2005-05-05 22:21:19 +0000 | [diff] [blame] | 124 | for (Module::const_global_iterator I = TheModule->global_begin(), |
| 125 | E = TheModule->global_end(); I != E; ++I) |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 126 | getOrCreateSlot(I); |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 127 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 128 | // Scavenge the types out of the functions, then add the functions themselves |
| 129 | // to the value table... |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 130 | // |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 131 | for (Module::const_iterator I = TheModule->begin(), E = TheModule->end(); |
| 132 | I != E; ++I) |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 133 | getOrCreateSlot(I); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 134 | |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 135 | // Add all of the module level constants used as initializers |
| 136 | // |
Chris Lattner | 9e60d8d | 2005-05-05 22:21:19 +0000 | [diff] [blame] | 137 | for (Module::const_global_iterator I = TheModule->global_begin(), |
| 138 | E = TheModule->global_end(); I != E; ++I) |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 139 | if (I->hasInitializer()) |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 140 | getOrCreateSlot(I->getInitializer()); |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 141 | |
Chris Lattner | dcea630 | 2004-01-14 23:34:39 +0000 | [diff] [blame] | 142 | // Now that all global constants have been added, rearrange constant planes |
| 143 | // that contain constant strings so that the strings occur at the start of the |
| 144 | // plane, not somewhere in the middle. |
| 145 | // |
Reid Spencer | 798ff64 | 2004-05-26 07:37:11 +0000 | [diff] [blame] | 146 | for (unsigned plane = 0, e = Table.size(); plane != e; ++plane) { |
| 147 | if (const ArrayType *AT = dyn_cast<ArrayType>(Types[plane])) |
| 148 | if (AT->getElementType() == Type::SByteTy || |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 149 | AT->getElementType() == Type::UByteTy) { |
| 150 | TypePlane &Plane = Table[plane]; |
| 151 | unsigned FirstNonStringID = 0; |
| 152 | for (unsigned i = 0, e = Plane.size(); i != e; ++i) |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 153 | if (isa<ConstantAggregateZero>(Plane[i]) || |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 154 | (isa<ConstantArray>(Plane[i]) && |
Chris Lattner | 236ca44 | 2004-10-24 04:27:59 +0000 | [diff] [blame] | 155 | cast<ConstantArray>(Plane[i])->isString())) { |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 156 | // Check to see if we have to shuffle this string around. If not, |
| 157 | // don't do anything. |
| 158 | if (i != FirstNonStringID) { |
| 159 | // Swap the plane entries.... |
| 160 | std::swap(Plane[i], Plane[FirstNonStringID]); |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 161 | |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 162 | // Keep the NodeMap up to date. |
| 163 | NodeMap[Plane[i]] = i; |
| 164 | NodeMap[Plane[FirstNonStringID]] = FirstNonStringID; |
| 165 | } |
| 166 | ++FirstNonStringID; |
| 167 | } |
Reid Spencer | 798ff64 | 2004-05-26 07:37:11 +0000 | [diff] [blame] | 168 | } |
Chris Lattner | dcea630 | 2004-01-14 23:34:39 +0000 | [diff] [blame] | 169 | } |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 170 | |
| 171 | // Scan all of the functions for their constants, which allows us to emit |
| 172 | // more compact modules. This is optional, and is just used to compactify |
Reid Spencer | e840434 | 2004-07-18 00:18:30 +0000 | [diff] [blame] | 173 | // the constants used by different functions together. |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 174 | // |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 175 | // This functionality tends to produce smaller bytecode files. This should |
| 176 | // not be used in the future by clients that want to, for example, build and |
| 177 | // emit functions on the fly. For now, however, it is unconditionally |
Reid Spencer | e840434 | 2004-07-18 00:18:30 +0000 | [diff] [blame] | 178 | // enabled. |
Reid Spencer | 798ff64 | 2004-05-26 07:37:11 +0000 | [diff] [blame] | 179 | ModuleContainsAllFunctionConstants = true; |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 180 | |
Reid Spencer | 798ff64 | 2004-05-26 07:37:11 +0000 | [diff] [blame] | 181 | SC_DEBUG("Inserting function constants:\n"); |
| 182 | for (Module::const_iterator F = TheModule->begin(), E = TheModule->end(); |
| 183 | F != E; ++F) { |
Chris Lattner | 3bc5a60 | 2006-01-25 23:08:15 +0000 | [diff] [blame] | 184 | for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) { |
| 185 | for (User::const_op_iterator OI = I->op_begin(), E = I->op_end(); |
| 186 | OI != E; ++OI) { |
| 187 | if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) || |
| 188 | isa<InlineAsm>(*OI)) |
| 189 | getOrCreateSlot(*OI); |
| 190 | } |
Reid Spencer | 798ff64 | 2004-05-26 07:37:11 +0000 | [diff] [blame] | 191 | getOrCreateSlot(I->getType()); |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 192 | } |
Reid Spencer | 798ff64 | 2004-05-26 07:37:11 +0000 | [diff] [blame] | 193 | processSymbolTableConstants(&F->getSymbolTable()); |
Chris Lattner | a14b0d4 | 2004-01-10 23:46:13 +0000 | [diff] [blame] | 194 | } |
Chris Lattner | a14b0d4 | 2004-01-10 23:46:13 +0000 | [diff] [blame] | 195 | |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 196 | // Insert constants that are named at module level into the slot pool so that |
| 197 | // the module symbol table can refer to them... |
Reid Spencer | 798ff64 | 2004-05-26 07:37:11 +0000 | [diff] [blame] | 198 | SC_DEBUG("Inserting SymbolTable values:\n"); |
| 199 | processSymbolTable(&TheModule->getSymbolTable()); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 200 | |
Chris Lattner | a14b0d4 | 2004-01-10 23:46:13 +0000 | [diff] [blame] | 201 | // Now that we have collected together all of the information relevant to the |
| 202 | // module, compactify the type table if it is particularly big and outputting |
| 203 | // a bytecode file. The basic problem we run into is that some programs have |
| 204 | // a large number of types, which causes the type field to overflow its size, |
| 205 | // which causes instructions to explode in size (particularly call |
| 206 | // instructions). To avoid this behavior, we "sort" the type table so that |
| 207 | // all non-value types are pushed to the end of the type table, giving nice |
| 208 | // low numbers to the types that can be used by instructions, thus reducing |
| 209 | // the amount of explodage we suffer. |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 210 | if (Types.size() >= 64) { |
Chris Lattner | a14b0d4 | 2004-01-10 23:46:13 +0000 | [diff] [blame] | 211 | unsigned FirstNonValueTypeID = 0; |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 212 | for (unsigned i = 0, e = Types.size(); i != e; ++i) |
| 213 | if (Types[i]->isFirstClassType() || Types[i]->isPrimitiveType()) { |
Chris Lattner | a14b0d4 | 2004-01-10 23:46:13 +0000 | [diff] [blame] | 214 | // Check to see if we have to shuffle this type around. If not, don't |
| 215 | // do anything. |
| 216 | if (i != FirstNonValueTypeID) { |
| 217 | // Swap the type ID's. |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 218 | std::swap(Types[i], Types[FirstNonValueTypeID]); |
Chris Lattner | a14b0d4 | 2004-01-10 23:46:13 +0000 | [diff] [blame] | 219 | |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 220 | // Keep the TypeMap up to date. |
| 221 | TypeMap[Types[i]] = i; |
| 222 | TypeMap[Types[FirstNonValueTypeID]] = FirstNonValueTypeID; |
Chris Lattner | a14b0d4 | 2004-01-10 23:46:13 +0000 | [diff] [blame] | 223 | |
| 224 | // When we move a type, make sure to move its value plane as needed. |
Chris Lattner | 9380297 | 2004-01-11 23:29:26 +0000 | [diff] [blame] | 225 | if (Table.size() > FirstNonValueTypeID) { |
| 226 | if (Table.size() <= i) Table.resize(i+1); |
| 227 | std::swap(Table[i], Table[FirstNonValueTypeID]); |
Chris Lattner | 9380297 | 2004-01-11 23:29:26 +0000 | [diff] [blame] | 228 | } |
Chris Lattner | a14b0d4 | 2004-01-10 23:46:13 +0000 | [diff] [blame] | 229 | } |
| 230 | ++FirstNonValueTypeID; |
| 231 | } |
| 232 | } |
| 233 | |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 234 | SC_DEBUG("end processModule!\n"); |
| 235 | } |
| 236 | |
| 237 | // processSymbolTable - Insert all of the values in the specified symbol table |
| 238 | // into the values table... |
| 239 | // |
| 240 | void SlotCalculator::processSymbolTable(const SymbolTable *ST) { |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 241 | // Do the types first. |
| 242 | for (SymbolTable::type_const_iterator TI = ST->type_begin(), |
| 243 | TE = ST->type_end(); TI != TE; ++TI ) |
| 244 | getOrCreateSlot(TI->second); |
| 245 | |
| 246 | // Now do the values. |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 247 | for (SymbolTable::plane_const_iterator PI = ST->plane_begin(), |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 248 | PE = ST->plane_end(); PI != PE; ++PI) |
| 249 | for (SymbolTable::value_const_iterator VI = PI->second.begin(), |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 250 | VE = PI->second.end(); VI != VE; ++VI) |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 251 | getOrCreateSlot(VI->second); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | void SlotCalculator::processSymbolTableConstants(const SymbolTable *ST) { |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 255 | // Do the types first |
| 256 | for (SymbolTable::type_const_iterator TI = ST->type_begin(), |
| 257 | TE = ST->type_end(); TI != TE; ++TI ) |
| 258 | getOrCreateSlot(TI->second); |
| 259 | |
| 260 | // Now do the constant values in all planes |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 261 | for (SymbolTable::plane_const_iterator PI = ST->plane_begin(), |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 262 | PE = ST->plane_end(); PI != PE; ++PI) |
| 263 | for (SymbolTable::value_const_iterator VI = PI->second.begin(), |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 264 | VE = PI->second.end(); VI != VE; ++VI) |
Reid Spencer | e840434 | 2004-07-18 00:18:30 +0000 | [diff] [blame] | 265 | if (isa<Constant>(VI->second) && |
| 266 | !isa<GlobalValue>(VI->second)) |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 267 | getOrCreateSlot(VI->second); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | |
Chris Lattner | ce439b5 | 2003-10-20 19:10:06 +0000 | [diff] [blame] | 271 | void SlotCalculator::incorporateFunction(const Function *F) { |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 272 | assert((ModuleLevel.size() == 0 || |
| 273 | ModuleTypeLevel == 0) && "Module already incorporated!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 274 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 275 | SC_DEBUG("begin processFunction!\n"); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 276 | |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 277 | // If we emitted all of the function constants, build a compaction table. |
Chris Lattner | 101cefa | 2006-06-07 22:20:03 +0000 | [diff] [blame] | 278 | if (ModuleContainsAllFunctionConstants) |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 279 | buildCompactionTable(F); |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 280 | |
| 281 | // Update the ModuleLevel entries to be accurate. |
| 282 | ModuleLevel.resize(getNumPlanes()); |
| 283 | for (unsigned i = 0, e = getNumPlanes(); i != e; ++i) |
| 284 | ModuleLevel[i] = getPlane(i).size(); |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 285 | ModuleTypeLevel = Types.size(); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 287 | // Iterate over function arguments, adding them to the value table... |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 288 | for(Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I) |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 289 | getOrCreateSlot(I); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 290 | |
Chris Lattner | 3bc5a60 | 2006-01-25 23:08:15 +0000 | [diff] [blame] | 291 | if (!ModuleContainsAllFunctionConstants) { |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 292 | // Iterate over all of the instructions in the function, looking for |
| 293 | // constant values that are referenced. Add these to the value pools |
| 294 | // before any nonconstant values. This will be turned into the constant |
| 295 | // pool for the bytecode writer. |
| 296 | // |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 297 | |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 298 | // Emit all of the constants that are being used by the instructions in |
| 299 | // the function... |
Chris Lattner | 3bc5a60 | 2006-01-25 23:08:15 +0000 | [diff] [blame] | 300 | for (constant_iterator CI = constant_begin(F), CE = constant_end(F); |
| 301 | CI != CE; ++CI) |
| 302 | getOrCreateSlot(*CI); |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 303 | |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 304 | // If there is a symbol table, it is possible that the user has names for |
| 305 | // constants that are not being used. In this case, we will have problems |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 306 | // if we don't emit the constants now, because otherwise we will get |
Chris Lattner | a14b0d4 | 2004-01-10 23:46:13 +0000 | [diff] [blame] | 307 | // symbol table references to constants not in the output. Scan for these |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 308 | // constants now. |
| 309 | // |
Chris Lattner | ce439b5 | 2003-10-20 19:10:06 +0000 | [diff] [blame] | 310 | processSymbolTableConstants(&F->getSymbolTable()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 313 | SC_DEBUG("Inserting Instructions:\n"); |
| 314 | |
| 315 | // Add all of the instructions to the type planes... |
Chris Lattner | d5c59d5 | 2004-01-15 20:24:09 +0000 | [diff] [blame] | 316 | for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 317 | getOrCreateSlot(BB); |
Chris Lattner | 389dbfb | 2003-10-21 17:39:59 +0000 | [diff] [blame] | 318 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) { |
| 319 | getOrCreateSlot(I); |
Chris Lattner | 389dbfb | 2003-10-21 17:39:59 +0000 | [diff] [blame] | 320 | } |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 323 | // If we are building a compaction table, prune out planes that do not benefit |
| 324 | // from being compactified. |
| 325 | if (!CompactionTable.empty()) |
| 326 | pruneCompactionTable(); |
| 327 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 328 | SC_DEBUG("end processFunction!\n"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 331 | void SlotCalculator::purgeFunction() { |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 332 | assert((ModuleLevel.size() != 0 || |
| 333 | ModuleTypeLevel != 0) && "Module not incorporated!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 334 | unsigned NumModuleTypes = ModuleLevel.size(); |
| 335 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 336 | SC_DEBUG("begin purgeFunction!\n"); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 337 | |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 338 | // First, free the compaction map if used. |
| 339 | CompactionNodeMap.clear(); |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 340 | CompactionTypeMap.clear(); |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 341 | |
| 342 | // Next, remove values from existing type planes |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 343 | for (unsigned i = 0; i != NumModuleTypes; ++i) { |
| 344 | // Size of plane before function came |
| 345 | unsigned ModuleLev = getModuleLevel(i); |
| 346 | assert(int(ModuleLev) >= 0 && "BAD!"); |
| 347 | |
| 348 | TypePlane &Plane = getPlane(i); |
| 349 | |
| 350 | assert(ModuleLev <= Plane.size() && "module levels higher than elements?"); |
| 351 | while (Plane.size() != ModuleLev) { |
| 352 | assert(!isa<GlobalValue>(Plane.back()) && |
| 353 | "Functions cannot define globals!"); |
| 354 | NodeMap.erase(Plane.back()); // Erase from nodemap |
| 355 | Plane.pop_back(); // Shrink plane |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 356 | } |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 357 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 358 | |
| 359 | // We don't need this state anymore, free it up. |
| 360 | ModuleLevel.clear(); |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 361 | ModuleTypeLevel = 0; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 362 | |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 363 | // Finally, remove any type planes defined by the function... |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 364 | CompactionTypes.clear(); |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 365 | if (!CompactionTable.empty()) { |
| 366 | CompactionTable.clear(); |
| 367 | } else { |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 368 | while (Table.size() > NumModuleTypes) { |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 369 | TypePlane &Plane = Table.back(); |
| 370 | SC_DEBUG("Removing Plane " << (Table.size()-1) << " of size " |
| 371 | << Plane.size() << "\n"); |
| 372 | while (Plane.size()) { |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 373 | assert(!isa<GlobalValue>(Plane.back()) && |
| 374 | "Functions cannot define globals!"); |
| 375 | NodeMap.erase(Plane.back()); // Erase from nodemap |
| 376 | Plane.pop_back(); // Shrink plane |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 377 | } |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 378 | |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 379 | Table.pop_back(); // Nuke the plane, we don't like it. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 380 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 381 | } |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 382 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 383 | SC_DEBUG("end purgeFunction!\n"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Chris Lattner | 9e60d8d | 2005-05-05 22:21:19 +0000 | [diff] [blame] | 386 | static inline bool hasNullValue(const Type *Ty) { |
| 387 | return Ty != Type::LabelTy && Ty != Type::VoidTy && !isa<OpaqueType>(Ty); |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /// getOrCreateCompactionTableSlot - This method is used to build up the initial |
| 391 | /// approximation of the compaction table. |
| 392 | unsigned SlotCalculator::getOrCreateCompactionTableSlot(const Value *V) { |
| 393 | std::map<const Value*, unsigned>::iterator I = |
| 394 | CompactionNodeMap.lower_bound(V); |
| 395 | if (I != CompactionNodeMap.end() && I->first == V) |
| 396 | return I->second; // Already exists? |
| 397 | |
| 398 | // Make sure the type is in the table. |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 399 | unsigned Ty; |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 400 | if (!CompactionTypes.empty()) |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 401 | Ty = getOrCreateCompactionTableSlot(V->getType()); |
| 402 | else // If the type plane was decompactified, use the global plane ID |
| 403 | Ty = getSlot(V->getType()); |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 404 | if (CompactionTable.size() <= Ty) |
| 405 | CompactionTable.resize(Ty+1); |
| 406 | |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 407 | TypePlane &TyPlane = CompactionTable[Ty]; |
| 408 | |
| 409 | // Make sure to insert the null entry if the thing we are inserting is not a |
| 410 | // null constant. |
Chris Lattner | 9e60d8d | 2005-05-05 22:21:19 +0000 | [diff] [blame] | 411 | if (TyPlane.empty() && hasNullValue(V->getType())) { |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 412 | Value *ZeroInitializer = Constant::getNullValue(V->getType()); |
| 413 | if (V != ZeroInitializer) { |
| 414 | TyPlane.push_back(ZeroInitializer); |
| 415 | CompactionNodeMap[ZeroInitializer] = 0; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | unsigned SlotNo = TyPlane.size(); |
| 420 | TyPlane.push_back(V); |
| 421 | CompactionNodeMap.insert(std::make_pair(V, SlotNo)); |
| 422 | return SlotNo; |
| 423 | } |
| 424 | |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 425 | /// getOrCreateCompactionTableSlot - This method is used to build up the initial |
| 426 | /// approximation of the compaction table. |
| 427 | unsigned SlotCalculator::getOrCreateCompactionTableSlot(const Type *T) { |
| 428 | std::map<const Type*, unsigned>::iterator I = |
| 429 | CompactionTypeMap.lower_bound(T); |
| 430 | if (I != CompactionTypeMap.end() && I->first == T) |
| 431 | return I->second; // Already exists? |
| 432 | |
| 433 | unsigned SlotNo = CompactionTypes.size(); |
| 434 | SC_DEBUG("Inserting Compaction Type #" << SlotNo << ": " << T << "\n"); |
| 435 | CompactionTypes.push_back(T); |
| 436 | CompactionTypeMap.insert(std::make_pair(T, SlotNo)); |
| 437 | return SlotNo; |
| 438 | } |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 439 | |
| 440 | /// buildCompactionTable - Since all of the function constants and types are |
| 441 | /// stored in the module-level constant table, we don't need to emit a function |
| 442 | /// constant table. Also due to this, the indices for various constants and |
| 443 | /// types might be very large in large programs. In order to avoid blowing up |
| 444 | /// the size of instructions in the bytecode encoding, we build a compaction |
| 445 | /// table, which defines a mapping from function-local identifiers to global |
| 446 | /// identifiers. |
| 447 | void SlotCalculator::buildCompactionTable(const Function *F) { |
| 448 | assert(CompactionNodeMap.empty() && "Compaction table already built!"); |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 449 | assert(CompactionTypeMap.empty() && "Compaction types already built!"); |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 450 | // First step, insert the primitive types. |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 451 | CompactionTable.resize(Type::LastPrimitiveTyID+1); |
| 452 | for (unsigned i = 0; i <= Type::LastPrimitiveTyID; ++i) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 453 | const Type *PrimTy = Type::getPrimitiveType((Type::TypeID)i); |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 454 | CompactionTypes.push_back(PrimTy); |
| 455 | CompactionTypeMap[PrimTy] = i; |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | // Next, include any types used by function arguments. |
Chris Lattner | 9e60d8d | 2005-05-05 22:21:19 +0000 | [diff] [blame] | 459 | for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| 460 | I != E; ++I) |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 461 | getOrCreateCompactionTableSlot(I->getType()); |
| 462 | |
| 463 | // Next, find all of the types and values that are referred to by the |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 464 | // instructions in the function. |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 465 | for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) { |
| 466 | getOrCreateCompactionTableSlot(I->getType()); |
| 467 | for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) |
Chris Lattner | 101cefa | 2006-06-07 22:20:03 +0000 | [diff] [blame] | 468 | if (isa<Constant>(I->getOperand(op)) || isa<InlineAsm>(I->getOperand(op))) |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 469 | getOrCreateCompactionTableSlot(I->getOperand(op)); |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 472 | // Do the types in the symbol table |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 473 | const SymbolTable &ST = F->getSymbolTable(); |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 474 | for (SymbolTable::type_const_iterator TI = ST.type_begin(), |
| 475 | TE = ST.type_end(); TI != TE; ++TI) |
| 476 | getOrCreateCompactionTableSlot(TI->second); |
| 477 | |
| 478 | // Now do the constants and global values |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 479 | for (SymbolTable::plane_const_iterator PI = ST.plane_begin(), |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 480 | PE = ST.plane_end(); PI != PE; ++PI) |
| 481 | for (SymbolTable::value_const_iterator VI = PI->second.begin(), |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 482 | VE = PI->second.end(); VI != VE; ++VI) |
Reid Spencer | e840434 | 2004-07-18 00:18:30 +0000 | [diff] [blame] | 483 | if (isa<Constant>(VI->second) && !isa<GlobalValue>(VI->second)) |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 484 | getOrCreateCompactionTableSlot(VI->second); |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 485 | |
| 486 | // Now that we have all of the values in the table, and know what types are |
| 487 | // referenced, make sure that there is at least the zero initializer in any |
| 488 | // used type plane. Since the type was used, we will be emitting instructions |
| 489 | // to the plane even if there are no constants in it. |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 490 | CompactionTable.resize(CompactionTypes.size()); |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 491 | for (unsigned i = 0, e = CompactionTable.size(); i != e; ++i) |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 492 | if (CompactionTable[i].empty() && (i != Type::VoidTyID) && |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 493 | i != Type::LabelTyID) { |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 494 | const Type *Ty = CompactionTypes[i]; |
| 495 | SC_DEBUG("Getting Null Value #" << i << " for Type " << Ty << "\n"); |
| 496 | assert(Ty->getTypeID() != Type::VoidTyID); |
| 497 | assert(Ty->getTypeID() != Type::LabelTyID); |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 498 | getOrCreateCompactionTableSlot(Constant::getNullValue(Ty)); |
| 499 | } |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 500 | |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 501 | // Okay, now at this point, we have a legal compaction table. Since we want |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 502 | // to emit the smallest possible binaries, do not compactify the type plane if |
| 503 | // it will not save us anything. Because we have not yet incorporated the |
| 504 | // function body itself yet, we don't know whether or not it's a good idea to |
| 505 | // compactify other planes. We will defer this decision until later. |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 506 | TypeList &GlobalTypes = Types; |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 507 | |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 508 | // All of the values types will be scrunched to the start of the types plane |
| 509 | // of the global table. Figure out just how many there are. |
| 510 | assert(!GlobalTypes.empty() && "No global types???"); |
| 511 | unsigned NumFCTypes = GlobalTypes.size()-1; |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 512 | while (!GlobalTypes[NumFCTypes]->isFirstClassType()) |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 513 | --NumFCTypes; |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 514 | |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 515 | // If there are fewer that 64 types, no instructions will be exploded due to |
| 516 | // the size of the type operands. Thus there is no need to compactify types. |
| 517 | // Also, if the compaction table contains most of the entries in the global |
| 518 | // table, there really is no reason to compactify either. |
| 519 | if (NumFCTypes < 64) { |
| 520 | // Decompactifying types is tricky, because we have to move type planes all |
| 521 | // over the place. At least we don't need to worry about updating the |
| 522 | // CompactionNodeMap for non-types though. |
| 523 | std::vector<TypePlane> TmpCompactionTable; |
| 524 | std::swap(CompactionTable, TmpCompactionTable); |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 525 | TypeList TmpTypes; |
| 526 | std::swap(TmpTypes, CompactionTypes); |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 527 | |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 528 | // Move each plane back over to the uncompactified plane |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 529 | while (!TmpTypes.empty()) { |
| 530 | const Type *Ty = TmpTypes.back(); |
| 531 | TmpTypes.pop_back(); |
| 532 | CompactionTypeMap.erase(Ty); // Decompactify type! |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 533 | |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 534 | // Find the global slot number for this type. |
| 535 | int TySlot = getSlot(Ty); |
| 536 | assert(TySlot != -1 && "Type doesn't exist in global table?"); |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 537 | |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 538 | // Now we know where to put the compaction table plane. |
| 539 | if (CompactionTable.size() <= unsigned(TySlot)) |
| 540 | CompactionTable.resize(TySlot+1); |
| 541 | // Move the plane back into the compaction table. |
| 542 | std::swap(CompactionTable[TySlot], TmpCompactionTable[TmpTypes.size()]); |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 543 | |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 544 | // And remove the empty plane we just moved in. |
| 545 | TmpCompactionTable.pop_back(); |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 546 | } |
| 547 | } |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 550 | |
| 551 | /// pruneCompactionTable - Once the entire function being processed has been |
| 552 | /// incorporated into the current compaction table, look over the compaction |
| 553 | /// table and check to see if there are any values whose compaction will not |
| 554 | /// save us any space in the bytecode file. If compactifying these values |
| 555 | /// serves no purpose, then we might as well not even emit the compactification |
| 556 | /// information to the bytecode file, saving a bit more space. |
| 557 | /// |
| 558 | /// Note that the type plane has already been compactified if possible. |
| 559 | /// |
| 560 | void SlotCalculator::pruneCompactionTable() { |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 561 | TypeList &TyPlane = CompactionTypes; |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 562 | for (unsigned ctp = 0, e = CompactionTable.size(); ctp != e; ++ctp) |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 563 | if (!CompactionTable[ctp].empty()) { |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 564 | TypePlane &CPlane = CompactionTable[ctp]; |
| 565 | unsigned GlobalSlot = ctp; |
| 566 | if (!TyPlane.empty()) |
| 567 | GlobalSlot = getGlobalSlot(TyPlane[ctp]); |
| 568 | |
| 569 | if (GlobalSlot >= Table.size()) |
| 570 | Table.resize(GlobalSlot+1); |
| 571 | TypePlane &GPlane = Table[GlobalSlot]; |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 572 | |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 573 | unsigned ModLevel = getModuleLevel(ctp); |
| 574 | unsigned NumFunctionObjs = CPlane.size()-ModLevel; |
| 575 | |
| 576 | // If the maximum index required if all entries in this plane were merged |
| 577 | // into the global plane is less than 64, go ahead and eliminate the |
| 578 | // plane. |
| 579 | bool PrunePlane = GPlane.size() + NumFunctionObjs < 64; |
| 580 | |
| 581 | // If there are no function-local values defined, and the maximum |
| 582 | // referenced global entry is less than 64, we don't need to compactify. |
| 583 | if (!PrunePlane && NumFunctionObjs == 0) { |
| 584 | unsigned MaxIdx = 0; |
| 585 | for (unsigned i = 0; i != ModLevel; ++i) { |
| 586 | unsigned Idx = NodeMap[CPlane[i]]; |
| 587 | if (Idx > MaxIdx) MaxIdx = Idx; |
| 588 | } |
| 589 | PrunePlane = MaxIdx < 64; |
| 590 | } |
| 591 | |
| 592 | // Ok, finally, if we decided to prune this plane out of the compaction |
| 593 | // table, do so now. |
| 594 | if (PrunePlane) { |
| 595 | TypePlane OldPlane; |
| 596 | std::swap(OldPlane, CPlane); |
| 597 | |
| 598 | // Loop over the function local objects, relocating them to the global |
| 599 | // table plane. |
| 600 | for (unsigned i = ModLevel, e = OldPlane.size(); i != e; ++i) { |
| 601 | const Value *V = OldPlane[i]; |
| 602 | CompactionNodeMap.erase(V); |
| 603 | assert(NodeMap.count(V) == 0 && "Value already in table??"); |
| 604 | getOrCreateSlot(V); |
| 605 | } |
| 606 | |
| 607 | // For compactified global values, just remove them from the compaction |
| 608 | // node map. |
| 609 | for (unsigned i = 0; i != ModLevel; ++i) |
| 610 | CompactionNodeMap.erase(OldPlane[i]); |
| 611 | |
| 612 | // Update the new modulelevel for this plane. |
| 613 | assert(ctp < ModuleLevel.size() && "Cannot set modulelevel!"); |
| 614 | ModuleLevel[ctp] = GPlane.size()-NumFunctionObjs; |
| 615 | assert((int)ModuleLevel[ctp] >= 0 && "Bad computation!"); |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | |
Reid Spencer | 07ea191 | 2004-08-26 22:32:00 +0000 | [diff] [blame] | 620 | /// Determine if the compaction table is actually empty. Because the |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 621 | /// compaction table always includes the primitive type planes, we |
Reid Spencer | 07ea191 | 2004-08-26 22:32:00 +0000 | [diff] [blame] | 622 | /// can't just check getCompactionTable().size() because it will never |
| 623 | /// be zero. Furthermore, the ModuleLevel factors into whether a given |
| 624 | /// plane is empty or not. This function does the necessary computation |
| 625 | /// to determine if its actually empty. |
| 626 | bool SlotCalculator::CompactionTableIsEmpty() const { |
| 627 | // Check a degenerate case, just in case. |
| 628 | if (CompactionTable.size() == 0) return true; |
| 629 | |
| 630 | // Check each plane |
| 631 | for (unsigned i = 0, e = CompactionTable.size(); i < e; ++i) { |
| 632 | // If the plane is not empty |
| 633 | if (!CompactionTable[i].empty()) { |
| 634 | // If the module level is non-zero then at least the |
| 635 | // first element of the plane is valid and therefore not empty. |
| 636 | unsigned End = getModuleLevel(i); |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 637 | if (End != 0) |
Reid Spencer | 07ea191 | 2004-08-26 22:32:00 +0000 | [diff] [blame] | 638 | return false; |
| 639 | } |
| 640 | } |
| 641 | // All the compaction table planes are empty so the table is |
| 642 | // considered empty too. |
| 643 | return true; |
| 644 | } |
| 645 | |
Chris Lattner | 8c202cd | 2004-01-15 18:47:15 +0000 | [diff] [blame] | 646 | int SlotCalculator::getSlot(const Value *V) const { |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 647 | // If there is a CompactionTable active... |
| 648 | if (!CompactionNodeMap.empty()) { |
| 649 | std::map<const Value*, unsigned>::const_iterator I = |
| 650 | CompactionNodeMap.find(V); |
| 651 | if (I != CompactionNodeMap.end()) |
| 652 | return (int)I->second; |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 653 | // Otherwise, if it's not in the compaction table, it must be in a |
| 654 | // non-compactified plane. |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Chris Lattner | 8c202cd | 2004-01-15 18:47:15 +0000 | [diff] [blame] | 657 | std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(V); |
| 658 | if (I != NodeMap.end()) |
| 659 | return (int)I->second; |
| 660 | |
Chris Lattner | 8c202cd | 2004-01-15 18:47:15 +0000 | [diff] [blame] | 661 | return -1; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 664 | int SlotCalculator::getSlot(const Type*T) const { |
| 665 | // If there is a CompactionTable active... |
| 666 | if (!CompactionTypeMap.empty()) { |
| 667 | std::map<const Type*, unsigned>::const_iterator I = |
| 668 | CompactionTypeMap.find(T); |
| 669 | if (I != CompactionTypeMap.end()) |
| 670 | return (int)I->second; |
| 671 | // Otherwise, if it's not in the compaction table, it must be in a |
| 672 | // non-compactified plane. |
| 673 | } |
| 674 | |
| 675 | std::map<const Type*, unsigned>::const_iterator I = TypeMap.find(T); |
| 676 | if (I != TypeMap.end()) |
| 677 | return (int)I->second; |
| 678 | |
| 679 | return -1; |
| 680 | } |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 681 | |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 682 | int SlotCalculator::getOrCreateSlot(const Value *V) { |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 683 | if (V->getType() == Type::VoidTy) return -1; |
| 684 | |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 685 | int SlotNo = getSlot(V); // Check to see if it's already in! |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 686 | if (SlotNo != -1) return SlotNo; |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 687 | |
Chris Lattner | 129baf6 | 2004-12-04 21:28:47 +0000 | [diff] [blame] | 688 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) |
| 689 | assert(GV->getParent() != 0 && "Global not embedded into a module!"); |
| 690 | |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 691 | if (!isa<GlobalValue>(V)) // Initializers for globals are handled explicitly |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 692 | if (const Constant *C = dyn_cast<Constant>(V)) { |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 693 | assert(CompactionNodeMap.empty() && |
| 694 | "All needed constants should be in the compaction map already!"); |
| 695 | |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 696 | // Do not index the characters that make up constant strings. We emit |
| 697 | // constant strings as special entities that don't require their |
Reid Spencer | 798ff64 | 2004-05-26 07:37:11 +0000 | [diff] [blame] | 698 | // individual characters to be emitted. |
| 699 | if (!isa<ConstantArray>(C) || !cast<ConstantArray>(C)->isString()) { |
Chris Lattner | dcea630 | 2004-01-14 23:34:39 +0000 | [diff] [blame] | 700 | // This makes sure that if a constant has uses (for example an array of |
| 701 | // const ints), that they are inserted also. |
| 702 | // |
| 703 | for (User::const_op_iterator I = C->op_begin(), E = C->op_end(); |
| 704 | I != E; ++I) |
| 705 | getOrCreateSlot(*I); |
| 706 | } else { |
| 707 | assert(ModuleLevel.empty() && |
| 708 | "How can a constant string be directly accessed in a function?"); |
| 709 | // Otherwise, if we are emitting a bytecode file and this IS a string, |
| 710 | // remember it. |
| 711 | if (!C->isNullValue()) |
| 712 | ConstantStrings.push_back(cast<ConstantArray>(C)); |
| 713 | } |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 716 | return insertValue(V); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 719 | int SlotCalculator::getOrCreateSlot(const Type* T) { |
| 720 | int SlotNo = getSlot(T); // Check to see if it's already in! |
| 721 | if (SlotNo != -1) return SlotNo; |
| 722 | return insertType(T); |
| 723 | } |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 724 | |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 725 | int SlotCalculator::insertValue(const Value *D, bool dontIgnore) { |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 726 | assert(D && "Can't insert a null value!"); |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 727 | assert(getSlot(D) == -1 && "Value is already in the table!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 728 | |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 729 | // If we are building a compaction map, and if this plane is being compacted, |
| 730 | // insert the value into the compaction map, not into the global map. |
| 731 | if (!CompactionNodeMap.empty()) { |
| 732 | if (D->getType() == Type::VoidTy) return -1; // Do not insert void values |
Reid Spencer | e840434 | 2004-07-18 00:18:30 +0000 | [diff] [blame] | 733 | assert(!isa<Constant>(D) && |
| 734 | "Types, constants, and globals should be in global table!"); |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 735 | |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 736 | int Plane = getSlot(D->getType()); |
| 737 | assert(Plane != -1 && CompactionTable.size() > (unsigned)Plane && |
| 738 | "Didn't find value type!"); |
| 739 | if (!CompactionTable[Plane].empty()) |
| 740 | return getOrCreateCompactionTableSlot(D); |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 743 | // If this node does not contribute to a plane, or if the node has a |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 744 | // name and we don't want names, then ignore the silly node... Note that types |
| 745 | // do need slot numbers so that we can keep track of where other values land. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 746 | // |
Chris Lattner | 9b50c15 | 2001-07-26 16:28:37 +0000 | [diff] [blame] | 747 | if (!dontIgnore) // Don't ignore nonignorables! |
Reid Spencer | 798ff64 | 2004-05-26 07:37:11 +0000 | [diff] [blame] | 748 | if (D->getType() == Type::VoidTy ) { // Ignore void type nodes |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 749 | SC_DEBUG("ignored value " << *D << "\n"); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 750 | return -1; // We do need types unconditionally though |
| 751 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 752 | |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 753 | // Okay, everything is happy, actually insert the silly value now... |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 754 | return doInsertValue(D); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 757 | int SlotCalculator::insertType(const Type *Ty, bool dontIgnore) { |
| 758 | assert(Ty && "Can't insert a null type!"); |
| 759 | assert(getSlot(Ty) == -1 && "Type is already in the table!"); |
| 760 | |
| 761 | // If we are building a compaction map, and if this plane is being compacted, |
| 762 | // insert the value into the compaction map, not into the global map. |
| 763 | if (!CompactionTypeMap.empty()) { |
| 764 | getOrCreateCompactionTableSlot(Ty); |
| 765 | } |
| 766 | |
| 767 | // Insert the current type before any subtypes. This is important because |
| 768 | // recursive types elements are inserted in a bottom up order. Changing |
| 769 | // this here can break things. For example: |
| 770 | // |
| 771 | // global { \2 * } { { \2 }* null } |
| 772 | // |
| 773 | int ResultSlot = doInsertType(Ty); |
| 774 | SC_DEBUG(" Inserted type: " << Ty->getDescription() << " slot=" << |
| 775 | ResultSlot << "\n"); |
| 776 | |
| 777 | // Loop over any contained types in the definition... in post |
| 778 | // order. |
| 779 | for (po_iterator<const Type*> I = po_begin(Ty), E = po_end(Ty); |
| 780 | I != E; ++I) { |
| 781 | if (*I != Ty) { |
| 782 | const Type *SubTy = *I; |
| 783 | // If we haven't seen this sub type before, add it to our type table! |
| 784 | if (getSlot(SubTy) == -1) { |
| 785 | SC_DEBUG(" Inserting subtype: " << SubTy->getDescription() << "\n"); |
Chris Lattner | aec6dd5 | 2004-07-12 20:29:52 +0000 | [diff] [blame] | 786 | doInsertType(SubTy); |
Reid Spencer | e840434 | 2004-07-18 00:18:30 +0000 | [diff] [blame] | 787 | SC_DEBUG(" Inserted subtype: " << SubTy->getDescription() << "\n"); |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 788 | } |
| 789 | } |
| 790 | } |
| 791 | return ResultSlot; |
| 792 | } |
| 793 | |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 794 | // doInsertValue - This is a small helper function to be called only |
| 795 | // be insertValue. |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 796 | // |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 797 | int SlotCalculator::doInsertValue(const Value *D) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 798 | const Type *Typ = D->getType(); |
Chris Lattner | 9b50c15 | 2001-07-26 16:28:37 +0000 | [diff] [blame] | 799 | unsigned Ty; |
| 800 | |
| 801 | // Used for debugging DefSlot=-1 assertion... |
| 802 | //if (Typ == Type::TypeTy) |
Bill Wendling | 68fe61d | 2006-11-29 00:19:40 +0000 | [diff] [blame^] | 803 | // llvm_cerr << "Inserting type '"<<cast<Type>(D)->getDescription() <<"'!\n"; |
Chris Lattner | 9b50c15 | 2001-07-26 16:28:37 +0000 | [diff] [blame] | 804 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 805 | if (Typ->isDerivedType()) { |
Chris Lattner | 68e3dbc | 2004-01-20 00:57:32 +0000 | [diff] [blame] | 806 | int ValSlot; |
| 807 | if (CompactionTable.empty()) |
| 808 | ValSlot = getSlot(Typ); |
| 809 | else |
| 810 | ValSlot = getGlobalSlot(Typ); |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 811 | if (ValSlot == -1) { // Have we already entered this type? |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 812 | // Nope, this is the first we have seen the type, process it. |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 813 | ValSlot = insertType(Typ, true); |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 814 | assert(ValSlot != -1 && "ProcessType returned -1 for a type?"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 815 | } |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 816 | Ty = (unsigned)ValSlot; |
Chris Lattner | 9b50c15 | 2001-07-26 16:28:37 +0000 | [diff] [blame] | 817 | } else { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 818 | Ty = Typ->getTypeID(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 819 | } |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 820 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 821 | if (Table.size() <= Ty) // Make sure we have the type plane allocated... |
| 822 | Table.resize(Ty+1, TypePlane()); |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 823 | |
| 824 | // If this is the first value to get inserted into the type plane, make sure |
| 825 | // to insert the implicit null value... |
Chris Lattner | 9e60d8d | 2005-05-05 22:21:19 +0000 | [diff] [blame] | 826 | if (Table[Ty].empty() && hasNullValue(Typ)) { |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 827 | Value *ZeroInitializer = Constant::getNullValue(Typ); |
| 828 | |
| 829 | // If we are pushing zeroinit, it will be handled below. |
| 830 | if (D != ZeroInitializer) { |
| 831 | Table[Ty].push_back(ZeroInitializer); |
| 832 | NodeMap[ZeroInitializer] = 0; |
| 833 | } |
| 834 | } |
| 835 | |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 836 | // Insert node into table and NodeMap... |
| 837 | unsigned DestSlot = NodeMap[D] = Table[Ty].size(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 838 | Table[Ty].push_back(D); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 839 | |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 840 | SC_DEBUG(" Inserting value [" << Ty << "] = " << D << " slot=" << |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 841 | DestSlot << " ["); |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 842 | // G = Global, C = Constant, T = Type, F = Function, o = other |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 843 | SC_DEBUG((isa<GlobalVariable>(D) ? "G" : (isa<Constant>(D) ? "C" : |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 844 | (isa<Function>(D) ? "F" : "o")))); |
Chris Lattner | f1fef65 | 2001-10-13 06:35:09 +0000 | [diff] [blame] | 845 | SC_DEBUG("]\n"); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 846 | return (int)DestSlot; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 847 | } |
Reid Spencer | 24ab28f | 2004-07-04 11:42:49 +0000 | [diff] [blame] | 848 | |
| 849 | // doInsertType - This is a small helper function to be called only |
| 850 | // be insertType. |
| 851 | // |
| 852 | int SlotCalculator::doInsertType(const Type *Ty) { |
| 853 | |
| 854 | // Insert node into table and NodeMap... |
| 855 | unsigned DestSlot = TypeMap[Ty] = Types.size(); |
| 856 | Types.push_back(Ty); |
| 857 | |
| 858 | SC_DEBUG(" Inserting type [" << DestSlot << "] = " << Ty << "\n" ); |
| 859 | return (int)DestSlot; |
| 860 | } |
| 861 | |