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