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