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