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 | // |
| 10 | // This file implements a useful analysis step to figure out what numbered |
| 11 | // slots values in a program will land in (keeping track of per plane |
| 12 | // information as required. |
| 13 | // |
| 14 | // This is used primarily for when writing a file to disk, either in bytecode |
| 15 | // or source format. |
| 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 19 | #include "llvm/SlotCalculator.h" |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/ConstantsScanner.h" |
Chris Lattner | dcea630 | 2004-01-14 23:34:39 +0000 | [diff] [blame] | 21 | #include "llvm/Constants.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 22 | #include "llvm/DerivedTypes.h" |
Chris Lattner | dcea630 | 2004-01-14 23:34:39 +0000 | [diff] [blame] | 23 | #include "llvm/iOther.h" |
| 24 | #include "llvm/Module.h" |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 25 | #include "llvm/SymbolTable.h" |
Alkis Evlogimenos | 088eb45 | 2003-10-31 03:02:34 +0000 | [diff] [blame] | 26 | #include "Support/PostOrderIterator.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 27 | #include "Support/STLExtras.h" |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 28 | #include <algorithm> |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 29 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 31 | #if 0 |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 32 | #define SC_DEBUG(X) std::cerr << X |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 33 | #else |
| 34 | #define SC_DEBUG(X) |
| 35 | #endif |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 8ce7501 | 2004-01-14 02:49:34 +0000 | [diff] [blame] | 37 | SlotCalculator::SlotCalculator(const Module *M, bool buildBytecodeInfo) { |
| 38 | BuildBytecodeInfo = buildBytecodeInfo; |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 39 | ModuleContainsAllFunctionConstants = false; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 40 | TheModule = M; |
| 41 | |
| 42 | // Preload table... Make sure that all of the primitive types are in the table |
| 43 | // and that their Primitive ID is equal to their slot # |
| 44 | // |
Alkis Evlogimenos | 5d1bdcd | 2003-10-29 03:12:12 +0000 | [diff] [blame] | 45 | SC_DEBUG("Inserting primitive types:\n"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 46 | for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) { |
| 47 | assert(Type::getPrimitiveType((Type::PrimitiveID)i)); |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 48 | insertValue(Type::getPrimitiveType((Type::PrimitiveID)i), true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | if (M == 0) return; // Empty table... |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 52 | processModule(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Chris Lattner | 8ce7501 | 2004-01-14 02:49:34 +0000 | [diff] [blame] | 55 | SlotCalculator::SlotCalculator(const Function *M, bool buildBytecodeInfo) { |
| 56 | BuildBytecodeInfo = buildBytecodeInfo; |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 57 | ModuleContainsAllFunctionConstants = false; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 58 | TheModule = M ? M->getParent() : 0; |
| 59 | |
| 60 | // Preload table... Make sure that all of the primitive types are in the table |
| 61 | // and that their Primitive ID is equal to their slot # |
| 62 | // |
Alkis Evlogimenos | 5d1bdcd | 2003-10-29 03:12:12 +0000 | [diff] [blame] | 63 | SC_DEBUG("Inserting primitive types:\n"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 64 | for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) { |
| 65 | assert(Type::getPrimitiveType((Type::PrimitiveID)i)); |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 66 | insertValue(Type::getPrimitiveType((Type::PrimitiveID)i), true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | if (TheModule == 0) return; // Empty table... |
| 70 | |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 71 | processModule(); // Process module level stuff |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 72 | incorporateFunction(M); // Start out in incorporated state |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 75 | unsigned SlotCalculator::getGlobalSlot(const Value *V) const { |
| 76 | assert(!CompactionTable.empty() && |
| 77 | "This method can only be used when compaction is enabled!"); |
| 78 | if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V)) |
| 79 | V = CPR->getValue(); |
| 80 | std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(V); |
| 81 | assert(I != NodeMap.end() && "Didn't find entry!"); |
| 82 | return I->second; |
| 83 | } |
| 84 | |
| 85 | |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 87 | // processModule - Process all of the module level function declarations and |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 88 | // types that are available. |
| 89 | // |
| 90 | void SlotCalculator::processModule() { |
| 91 | SC_DEBUG("begin processModule!\n"); |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 92 | |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 93 | // Add all of the global variables to the value table... |
Chris Lattner | f1fef65 | 2001-10-13 06:35:09 +0000 | [diff] [blame] | 94 | // |
| 95 | for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend(); |
Chris Lattner | 479b54b | 2002-10-14 00:48:57 +0000 | [diff] [blame] | 96 | I != E; ++I) |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 97 | getOrCreateSlot(I); |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 99 | // Scavenge the types out of the functions, then add the functions themselves |
| 100 | // to the value table... |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 101 | // |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 102 | for (Module::const_iterator I = TheModule->begin(), E = TheModule->end(); |
| 103 | I != E; ++I) |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 104 | getOrCreateSlot(I); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 105 | |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 106 | // Add all of the module level constants used as initializers |
| 107 | // |
| 108 | for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend(); |
| 109 | I != E; ++I) |
| 110 | if (I->hasInitializer()) |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 111 | getOrCreateSlot(I->getInitializer()); |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 112 | |
Chris Lattner | dcea630 | 2004-01-14 23:34:39 +0000 | [diff] [blame] | 113 | // Now that all global constants have been added, rearrange constant planes |
| 114 | // that contain constant strings so that the strings occur at the start of the |
| 115 | // plane, not somewhere in the middle. |
| 116 | // |
| 117 | if (BuildBytecodeInfo) { |
| 118 | TypePlane &Types = Table[Type::TypeTyID]; |
| 119 | for (unsigned plane = 0, e = Table.size(); plane != e; ++plane) { |
| 120 | if (const ArrayType *AT = dyn_cast<ArrayType>(Types[plane])) |
| 121 | if (AT->getElementType() == Type::SByteTy || |
| 122 | AT->getElementType() == Type::UByteTy) { |
| 123 | TypePlane &Plane = Table[plane]; |
| 124 | unsigned FirstNonStringID = 0; |
| 125 | for (unsigned i = 0, e = Plane.size(); i != e; ++i) |
| 126 | if (cast<ConstantArray>(Plane[i])->isString()) { |
| 127 | // Check to see if we have to shuffle this string around. If not, |
| 128 | // don't do anything. |
| 129 | if (i != FirstNonStringID) { |
| 130 | // Swap the plane entries.... |
| 131 | std::swap(Plane[i], Plane[FirstNonStringID]); |
| 132 | |
| 133 | // Keep the NodeMap up to date. |
| 134 | NodeMap[Plane[i]] = i; |
| 135 | NodeMap[Plane[FirstNonStringID]] = FirstNonStringID; |
| 136 | } |
| 137 | ++FirstNonStringID; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
Chris Lattner | a14b0d4 | 2004-01-10 23:46:13 +0000 | [diff] [blame] | 143 | // If we are emitting a bytecode file, scan all of the functions for their |
| 144 | // constants, which allows us to emit more compact modules. This is optional, |
| 145 | // and is just used to compactify the constants used by different functions |
| 146 | // together. |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 147 | // |
| 148 | // This functionality is completely optional for the bytecode writer, but |
| 149 | // tends to produce smaller bytecode files. This should not be used in the |
| 150 | // future by clients that want to, for example, build and emit functions on |
| 151 | // the fly. For now, however, it is unconditionally enabled when building |
| 152 | // bytecode information. |
| 153 | // |
Chris Lattner | 8ce7501 | 2004-01-14 02:49:34 +0000 | [diff] [blame] | 154 | if (BuildBytecodeInfo) { |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 155 | ModuleContainsAllFunctionConstants = true; |
| 156 | |
Chris Lattner | a14b0d4 | 2004-01-10 23:46:13 +0000 | [diff] [blame] | 157 | SC_DEBUG("Inserting function constants:\n"); |
| 158 | for (Module::const_iterator F = TheModule->begin(), E = TheModule->end(); |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 159 | F != E; ++F) { |
| 160 | for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I){ |
| 161 | for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) |
| 162 | if (isa<Constant>(I->getOperand(op))) |
| 163 | getOrCreateSlot(I->getOperand(op)); |
| 164 | getOrCreateSlot(I->getType()); |
| 165 | if (const VANextInst *VAN = dyn_cast<VANextInst>(*I)) |
| 166 | getOrCreateSlot(VAN->getArgType()); |
| 167 | } |
| 168 | processSymbolTableConstants(&F->getSymbolTable()); |
| 169 | } |
| 170 | |
| 171 | |
Chris Lattner | a14b0d4 | 2004-01-10 23:46:13 +0000 | [diff] [blame] | 172 | } |
Chris Lattner | a14b0d4 | 2004-01-10 23:46:13 +0000 | [diff] [blame] | 173 | |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 174 | // Insert constants that are named at module level into the slot pool so that |
| 175 | // the module symbol table can refer to them... |
| 176 | // |
Chris Lattner | 8ce7501 | 2004-01-14 02:49:34 +0000 | [diff] [blame] | 177 | if (BuildBytecodeInfo) { |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 178 | SC_DEBUG("Inserting SymbolTable values:\n"); |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 179 | processSymbolTable(&TheModule->getSymbolTable()); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Chris Lattner | a14b0d4 | 2004-01-10 23:46:13 +0000 | [diff] [blame] | 182 | // Now that we have collected together all of the information relevant to the |
| 183 | // module, compactify the type table if it is particularly big and outputting |
| 184 | // a bytecode file. The basic problem we run into is that some programs have |
| 185 | // a large number of types, which causes the type field to overflow its size, |
| 186 | // which causes instructions to explode in size (particularly call |
| 187 | // instructions). To avoid this behavior, we "sort" the type table so that |
| 188 | // all non-value types are pushed to the end of the type table, giving nice |
| 189 | // low numbers to the types that can be used by instructions, thus reducing |
| 190 | // the amount of explodage we suffer. |
Chris Lattner | 8ce7501 | 2004-01-14 02:49:34 +0000 | [diff] [blame] | 191 | if (BuildBytecodeInfo && Table[Type::TypeTyID].size() >= 64) { |
Chris Lattner | a14b0d4 | 2004-01-10 23:46:13 +0000 | [diff] [blame] | 192 | // 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] | 193 | TypePlane *Types = &Table[Type::TypeTyID]; |
Chris Lattner | a14b0d4 | 2004-01-10 23:46:13 +0000 | [diff] [blame] | 194 | unsigned FirstNonValueTypeID = 0; |
Chris Lattner | 9380297 | 2004-01-11 23:29:26 +0000 | [diff] [blame] | 195 | for (unsigned i = 0, e = Types->size(); i != e; ++i) |
| 196 | if (cast<Type>((*Types)[i])->isFirstClassType() || |
| 197 | cast<Type>((*Types)[i])->isPrimitiveType()) { |
Chris Lattner | a14b0d4 | 2004-01-10 23:46:13 +0000 | [diff] [blame] | 198 | // Check to see if we have to shuffle this type around. If not, don't |
| 199 | // do anything. |
| 200 | if (i != FirstNonValueTypeID) { |
Chris Lattner | 9380297 | 2004-01-11 23:29:26 +0000 | [diff] [blame] | 201 | assert(i != Type::TypeTyID && FirstNonValueTypeID != Type::TypeTyID && |
| 202 | "Cannot move around the type plane!"); |
| 203 | |
Chris Lattner | a14b0d4 | 2004-01-10 23:46:13 +0000 | [diff] [blame] | 204 | // Swap the type ID's. |
Chris Lattner | 9380297 | 2004-01-11 23:29:26 +0000 | [diff] [blame] | 205 | std::swap((*Types)[i], (*Types)[FirstNonValueTypeID]); |
Chris Lattner | a14b0d4 | 2004-01-10 23:46:13 +0000 | [diff] [blame] | 206 | |
| 207 | // Keep the NodeMap up to date. |
Chris Lattner | 9380297 | 2004-01-11 23:29:26 +0000 | [diff] [blame] | 208 | NodeMap[(*Types)[i]] = i; |
| 209 | NodeMap[(*Types)[FirstNonValueTypeID]] = FirstNonValueTypeID; |
Chris Lattner | a14b0d4 | 2004-01-10 23:46:13 +0000 | [diff] [blame] | 210 | |
| 211 | // 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] | 212 | if (Table.size() > FirstNonValueTypeID) { |
| 213 | if (Table.size() <= i) Table.resize(i+1); |
| 214 | std::swap(Table[i], Table[FirstNonValueTypeID]); |
| 215 | Types = &Table[Type::TypeTyID]; |
| 216 | } |
Chris Lattner | a14b0d4 | 2004-01-10 23:46:13 +0000 | [diff] [blame] | 217 | } |
| 218 | ++FirstNonValueTypeID; |
| 219 | } |
| 220 | } |
| 221 | |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 222 | SC_DEBUG("end processModule!\n"); |
| 223 | } |
| 224 | |
| 225 | // processSymbolTable - Insert all of the values in the specified symbol table |
| 226 | // into the values table... |
| 227 | // |
| 228 | void SlotCalculator::processSymbolTable(const SymbolTable *ST) { |
| 229 | for (SymbolTable::const_iterator I = ST->begin(), E = ST->end(); I != E; ++I) |
| 230 | for (SymbolTable::type_const_iterator TI = I->second.begin(), |
| 231 | TE = I->second.end(); TI != TE; ++TI) |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 232 | getOrCreateSlot(TI->second); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | void SlotCalculator::processSymbolTableConstants(const SymbolTable *ST) { |
| 236 | for (SymbolTable::const_iterator I = ST->begin(), E = ST->end(); I != E; ++I) |
| 237 | for (SymbolTable::type_const_iterator TI = I->second.begin(), |
| 238 | TE = I->second.end(); TI != TE; ++TI) |
Chris Lattner | d5c59d5 | 2004-01-15 20:24:09 +0000 | [diff] [blame] | 239 | if (isa<Constant>(TI->second) || isa<Type>(TI->second)) |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 240 | getOrCreateSlot(TI->second); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | |
Chris Lattner | ce439b5 | 2003-10-20 19:10:06 +0000 | [diff] [blame] | 244 | void SlotCalculator::incorporateFunction(const Function *F) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 245 | assert(ModuleLevel.size() == 0 && "Module already incorporated!"); |
| 246 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 247 | SC_DEBUG("begin processFunction!\n"); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 248 | |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 249 | // If we emitted all of the function constants, build a compaction table. |
| 250 | if (BuildBytecodeInfo && ModuleContainsAllFunctionConstants) |
| 251 | buildCompactionTable(F); |
| 252 | else { |
| 253 | // Save the Table state before we process the function... |
| 254 | for (unsigned i = 0, e = Table.size(); i != e; ++i) |
| 255 | ModuleLevel.push_back(Table[i].size()); |
| 256 | } |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 257 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 258 | // Iterate over function arguments, adding them to the value table... |
Chris Lattner | ce439b5 | 2003-10-20 19:10:06 +0000 | [diff] [blame] | 259 | 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] | 260 | getOrCreateSlot(I); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 261 | |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 262 | if (BuildBytecodeInfo && // Assembly writer does not need this! |
| 263 | !ModuleContainsAllFunctionConstants) { |
| 264 | // Iterate over all of the instructions in the function, looking for |
| 265 | // constant values that are referenced. Add these to the value pools |
| 266 | // before any nonconstant values. This will be turned into the constant |
| 267 | // pool for the bytecode writer. |
| 268 | // |
| 269 | |
| 270 | // Emit all of the constants that are being used by the instructions in |
| 271 | // the function... |
Chris Lattner | ce439b5 | 2003-10-20 19:10:06 +0000 | [diff] [blame] | 272 | for_each(constant_begin(F), constant_end(F), |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 273 | bind_obj(this, &SlotCalculator::getOrCreateSlot)); |
| 274 | |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 275 | // If there is a symbol table, it is possible that the user has names for |
| 276 | // constants that are not being used. In this case, we will have problems |
| 277 | // 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] | 278 | // symbol table references to constants not in the output. Scan for these |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 279 | // constants now. |
| 280 | // |
Chris Lattner | ce439b5 | 2003-10-20 19:10:06 +0000 | [diff] [blame] | 281 | processSymbolTableConstants(&F->getSymbolTable()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 284 | SC_DEBUG("Inserting Instructions:\n"); |
| 285 | |
| 286 | // Add all of the instructions to the type planes... |
Chris Lattner | d5c59d5 | 2004-01-15 20:24:09 +0000 | [diff] [blame] | 287 | for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 288 | getOrCreateSlot(BB); |
Chris Lattner | 389dbfb | 2003-10-21 17:39:59 +0000 | [diff] [blame] | 289 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) { |
| 290 | getOrCreateSlot(I); |
Chris Lattner | 2765c41 | 2003-10-21 17:40:54 +0000 | [diff] [blame] | 291 | if (const VANextInst *VAN = dyn_cast<VANextInst>(I)) |
| 292 | getOrCreateSlot(VAN->getArgType()); |
Chris Lattner | 389dbfb | 2003-10-21 17:39:59 +0000 | [diff] [blame] | 293 | } |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 296 | SC_DEBUG("end processFunction!\n"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 299 | void SlotCalculator::purgeFunction() { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 300 | assert(ModuleLevel.size() != 0 && "Module not incorporated!"); |
| 301 | unsigned NumModuleTypes = ModuleLevel.size(); |
| 302 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 303 | SC_DEBUG("begin purgeFunction!\n"); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 304 | |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 305 | // First, free the compaction map if used. |
| 306 | CompactionNodeMap.clear(); |
| 307 | |
| 308 | // Next, remove values from existing type planes |
| 309 | for (unsigned i = 0; i != NumModuleTypes; ++i) |
| 310 | if (i >= CompactionTable.size() || CompactionTable[i].empty()) { |
| 311 | unsigned ModuleSize = ModuleLevel[i];// Size of plane before function came |
| 312 | TypePlane &CurPlane = Table[i]; |
| 313 | |
| 314 | while (CurPlane.size() != ModuleSize) { |
| 315 | std::map<const Value *, unsigned>::iterator NI = |
| 316 | NodeMap.find(CurPlane.back()); |
| 317 | assert(NI != NodeMap.end() && "Node not in nodemap?"); |
| 318 | NodeMap.erase(NI); // Erase from nodemap |
| 319 | CurPlane.pop_back(); // Shrink plane |
| 320 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 321 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 322 | |
| 323 | // We don't need this state anymore, free it up. |
| 324 | ModuleLevel.clear(); |
| 325 | |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 326 | if (!CompactionTable.empty()) { |
| 327 | CompactionTable.clear(); |
| 328 | } else { |
| 329 | // FIXME: this will require adjustment when we don't compact everything. |
| 330 | |
| 331 | // Finally, remove any type planes defined by the function... |
| 332 | while (NumModuleTypes != Table.size()) { |
| 333 | TypePlane &Plane = Table.back(); |
| 334 | SC_DEBUG("Removing Plane " << (Table.size()-1) << " of size " |
| 335 | << Plane.size() << "\n"); |
| 336 | while (Plane.size()) { |
| 337 | NodeMap.erase(NodeMap.find(Plane.back())); // Erase from nodemap |
| 338 | Plane.pop_back(); // Shrink plane |
| 339 | } |
| 340 | |
| 341 | Table.pop_back(); // Nuke the plane, we don't like it. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 342 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 343 | } |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 344 | SC_DEBUG("end purgeFunction!\n"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 347 | static inline bool hasNullValue(unsigned TyID) { |
| 348 | return TyID != Type::LabelTyID && TyID != Type::TypeTyID && |
| 349 | TyID != Type::VoidTyID; |
| 350 | } |
| 351 | |
| 352 | /// getOrCreateCompactionTableSlot - This method is used to build up the initial |
| 353 | /// approximation of the compaction table. |
| 354 | unsigned SlotCalculator::getOrCreateCompactionTableSlot(const Value *V) { |
| 355 | std::map<const Value*, unsigned>::iterator I = |
| 356 | CompactionNodeMap.lower_bound(V); |
| 357 | if (I != CompactionNodeMap.end() && I->first == V) |
| 358 | return I->second; // Already exists? |
| 359 | |
| 360 | // Make sure the type is in the table. |
| 361 | unsigned Ty = getOrCreateCompactionTableSlot(V->getType()); |
| 362 | if (CompactionTable.size() <= Ty) |
| 363 | CompactionTable.resize(Ty+1); |
| 364 | |
| 365 | assert(!isa<Type>(V) || ModuleLevel.empty()); |
| 366 | |
| 367 | TypePlane &TyPlane = CompactionTable[Ty]; |
| 368 | |
| 369 | // Make sure to insert the null entry if the thing we are inserting is not a |
| 370 | // null constant. |
| 371 | if (TyPlane.empty() && hasNullValue(V->getType()->getPrimitiveID())) { |
| 372 | Value *ZeroInitializer = Constant::getNullValue(V->getType()); |
| 373 | if (V != ZeroInitializer) { |
| 374 | TyPlane.push_back(ZeroInitializer); |
| 375 | CompactionNodeMap[ZeroInitializer] = 0; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | unsigned SlotNo = TyPlane.size(); |
| 380 | TyPlane.push_back(V); |
| 381 | CompactionNodeMap.insert(std::make_pair(V, SlotNo)); |
| 382 | return SlotNo; |
| 383 | } |
| 384 | |
| 385 | |
| 386 | /// buildCompactionTable - Since all of the function constants and types are |
| 387 | /// stored in the module-level constant table, we don't need to emit a function |
| 388 | /// constant table. Also due to this, the indices for various constants and |
| 389 | /// types might be very large in large programs. In order to avoid blowing up |
| 390 | /// the size of instructions in the bytecode encoding, we build a compaction |
| 391 | /// table, which defines a mapping from function-local identifiers to global |
| 392 | /// identifiers. |
| 393 | void SlotCalculator::buildCompactionTable(const Function *F) { |
| 394 | assert(CompactionNodeMap.empty() && "Compaction table already built!"); |
| 395 | // First step, insert the primitive types. |
| 396 | CompactionTable.resize(Type::TypeTyID+1); |
| 397 | for (unsigned i = 0; i != Type::FirstDerivedTyID; ++i) { |
| 398 | const Type *PrimTy = Type::getPrimitiveType((Type::PrimitiveID)i); |
| 399 | CompactionTable[Type::TypeTyID].push_back(PrimTy); |
| 400 | CompactionNodeMap[PrimTy] = i; |
| 401 | } |
| 402 | |
| 403 | // Next, include any types used by function arguments. |
| 404 | for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I) |
| 405 | getOrCreateCompactionTableSlot(I->getType()); |
| 406 | |
| 407 | // Next, find all of the types and values that are referred to by the |
| 408 | // instructions in the program. |
| 409 | for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) { |
| 410 | getOrCreateCompactionTableSlot(I->getType()); |
| 411 | for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) |
| 412 | if (isa<Constant>(I->getOperand(op)) || |
| 413 | isa<GlobalValue>(I->getOperand(op))) |
| 414 | getOrCreateCompactionTableSlot(I->getOperand(op)); |
| 415 | if (const VANextInst *VAN = dyn_cast<VANextInst>(*I)) |
| 416 | getOrCreateCompactionTableSlot(VAN->getArgType()); |
| 417 | } |
| 418 | |
| 419 | const SymbolTable &ST = F->getSymbolTable(); |
| 420 | for (SymbolTable::const_iterator I = ST.begin(), E = ST.end(); I != E; ++I) |
| 421 | for (SymbolTable::type_const_iterator TI = I->second.begin(), |
| 422 | TE = I->second.end(); TI != TE; ++TI) |
| 423 | if (isa<Constant>(TI->second) || isa<Type>(TI->second) || |
| 424 | isa<GlobalValue>(TI->second)) |
| 425 | getOrCreateCompactionTableSlot(TI->second); |
| 426 | |
| 427 | // Now that we have all of the values in the table, and know what types are |
| 428 | // referenced, make sure that there is at least the zero initializer in any |
| 429 | // used type plane. Since the type was used, we will be emitting instructions |
| 430 | // to the plane even if there are no constants in it. |
| 431 | CompactionTable.resize(CompactionTable[Type::TypeTyID].size()); |
| 432 | for (unsigned i = 0, e = CompactionTable.size(); i != e; ++i) |
| 433 | if (CompactionTable[i].empty() && i != Type::VoidTyID && |
| 434 | i != Type::LabelTyID) { |
| 435 | const Type *Ty = cast<Type>(CompactionTable[Type::TypeTyID][i]); |
| 436 | getOrCreateCompactionTableSlot(Constant::getNullValue(Ty)); |
| 437 | } |
| 438 | |
| 439 | // Okay, now at this point, we have a legal compaction table. Since we want |
| 440 | // to emit the smallest possible binaries, we delete planes that do not NEED |
| 441 | // to be compacted, starting with the type plane. |
| 442 | |
| 443 | |
| 444 | // If decided not to compact anything, do not modify ModuleLevels. |
| 445 | if (CompactionTable.empty()) |
| 446 | // FIXME: must update ModuleLevel. |
| 447 | return; |
| 448 | |
| 449 | // Finally, for any planes that we have decided to compact, update the |
| 450 | // ModuleLevel entries to be accurate. |
| 451 | |
| 452 | // FIXME: This does not yet work for partially compacted tables. |
| 453 | ModuleLevel.resize(CompactionTable.size()); |
| 454 | for (unsigned i = 0, e = CompactionTable.size(); i != e; ++i) |
| 455 | ModuleLevel[i] = CompactionTable[i].size(); |
| 456 | } |
| 457 | |
Chris Lattner | 8c202cd | 2004-01-15 18:47:15 +0000 | [diff] [blame] | 458 | int SlotCalculator::getSlot(const Value *V) const { |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 459 | // If there is a CompactionTable active... |
| 460 | if (!CompactionNodeMap.empty()) { |
| 461 | std::map<const Value*, unsigned>::const_iterator I = |
| 462 | CompactionNodeMap.find(V); |
| 463 | if (I != CompactionNodeMap.end()) |
| 464 | return (int)I->second; |
| 465 | return -1; |
| 466 | } |
| 467 | |
Chris Lattner | 8c202cd | 2004-01-15 18:47:15 +0000 | [diff] [blame] | 468 | std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(V); |
| 469 | if (I != NodeMap.end()) |
| 470 | return (int)I->second; |
| 471 | |
| 472 | // Do not number ConstantPointerRef's at all. They are an abomination. |
| 473 | if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V)) |
| 474 | return getSlot(CPR->getValue()); |
| 475 | |
| 476 | return -1; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 479 | |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 480 | int SlotCalculator::getOrCreateSlot(const Value *V) { |
| 481 | int SlotNo = getSlot(V); // Check to see if it's already in! |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 482 | if (SlotNo != -1) return SlotNo; |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 483 | |
Chris Lattner | 8c202cd | 2004-01-15 18:47:15 +0000 | [diff] [blame] | 484 | // Do not number ConstantPointerRef's at all. They are an abomination. |
| 485 | if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V)) |
| 486 | return getOrCreateSlot(CPR->getValue()); |
| 487 | |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 488 | if (!isa<GlobalValue>(V)) // Initializers for globals are handled explicitly |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 489 | if (const Constant *C = dyn_cast<Constant>(V)) { |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 490 | assert(CompactionNodeMap.empty() && |
| 491 | "All needed constants should be in the compaction map already!"); |
| 492 | |
Chris Lattner | dcea630 | 2004-01-14 23:34:39 +0000 | [diff] [blame] | 493 | // If we are emitting a bytecode file, do not index the characters that |
| 494 | // make up constant strings. We emit constant strings as special |
| 495 | // entities that don't require their individual characters to be emitted. |
| 496 | if (!BuildBytecodeInfo || !isa<ConstantArray>(C) || |
| 497 | !cast<ConstantArray>(C)->isString()) { |
| 498 | // This makes sure that if a constant has uses (for example an array of |
| 499 | // const ints), that they are inserted also. |
| 500 | // |
| 501 | for (User::const_op_iterator I = C->op_begin(), E = C->op_end(); |
| 502 | I != E; ++I) |
| 503 | getOrCreateSlot(*I); |
| 504 | } else { |
| 505 | assert(ModuleLevel.empty() && |
| 506 | "How can a constant string be directly accessed in a function?"); |
| 507 | // Otherwise, if we are emitting a bytecode file and this IS a string, |
| 508 | // remember it. |
| 509 | if (!C->isNullValue()) |
| 510 | ConstantStrings.push_back(cast<ConstantArray>(C)); |
| 511 | } |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 514 | return insertValue(V); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 518 | int SlotCalculator::insertValue(const Value *D, bool dontIgnore) { |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 519 | assert(D && "Can't insert a null value!"); |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 520 | assert(getSlot(D) == -1 && "Value is already in the table!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 521 | |
Chris Lattner | 614cdcd | 2004-01-18 21:07:07 +0000 | [diff] [blame] | 522 | // If we are building a compaction map, and if this plane is being compacted, |
| 523 | // insert the value into the compaction map, not into the global map. |
| 524 | if (!CompactionNodeMap.empty()) { |
| 525 | if (D->getType() == Type::VoidTy) return -1; // Do not insert void values |
| 526 | assert(!isa<Type>(D) && !isa<Constant>(D) && !isa<GlobalValue>(D) && |
| 527 | "Types, constants, and globals should be in global SymTab!"); |
| 528 | |
| 529 | // FIXME: this does not yet handle partially compacted tables yet! |
| 530 | return getOrCreateCompactionTableSlot(D); |
| 531 | } |
| 532 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 533 | // 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] | 534 | // name and we don't want names, then ignore the silly node... Note that types |
| 535 | // 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] | 536 | // |
Chris Lattner | 9b50c15 | 2001-07-26 16:28:37 +0000 | [diff] [blame] | 537 | if (!dontIgnore) // Don't ignore nonignorables! |
| 538 | if (D->getType() == Type::VoidTy || // Ignore void type nodes |
Chris Lattner | 8ce7501 | 2004-01-14 02:49:34 +0000 | [diff] [blame] | 539 | (!BuildBytecodeInfo && // Ignore named and constants |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 540 | (D->hasName() || isa<Constant>(D)) && !isa<Type>(D))) { |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 541 | SC_DEBUG("ignored value " << *D << "\n"); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 542 | return -1; // We do need types unconditionally though |
| 543 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 544 | |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 545 | // 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] | 546 | if (const Type *TheTy = dyn_cast<Type>(D)) { |
Chris Lattner | f1fef65 | 2001-10-13 06:35:09 +0000 | [diff] [blame] | 547 | |
| 548 | // Insert the current type before any subtypes. This is important because |
| 549 | // recursive types elements are inserted in a bottom up order. Changing |
| 550 | // this here can break things. For example: |
| 551 | // |
| 552 | // global { \2 * } { { \2 }* null } |
| 553 | // |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 554 | int ResultSlot = doInsertValue(TheTy); |
| 555 | SC_DEBUG(" Inserted type: " << TheTy->getDescription() << " slot=" << |
| 556 | ResultSlot << "\n"); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 557 | |
Alkis Evlogimenos | 088eb45 | 2003-10-31 03:02:34 +0000 | [diff] [blame] | 558 | // Loop over any contained types in the definition... in post |
| 559 | // order. |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 560 | // |
Alkis Evlogimenos | 088eb45 | 2003-10-31 03:02:34 +0000 | [diff] [blame] | 561 | 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] | 562 | I != E; ++I) { |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 563 | if (*I != TheTy) { |
Alkis Evlogimenos | 74fa84f | 2003-10-30 21:04:44 +0000 | [diff] [blame] | 564 | const Type *SubTy = *I; |
Alkis Evlogimenos | 088eb45 | 2003-10-31 03:02:34 +0000 | [diff] [blame] | 565 | // If we haven't seen this sub type before, add it to our type table! |
| 566 | if (getSlot(SubTy) == -1) { |
| 567 | SC_DEBUG(" Inserting subtype: " << SubTy->getDescription() << "\n"); |
| 568 | int Slot = doInsertValue(SubTy); |
| 569 | SC_DEBUG(" Inserted subtype: " << SubTy->getDescription() << |
| 570 | " slot=" << Slot << "\n"); |
| 571 | } |
Alkis Evlogimenos | 74fa84f | 2003-10-30 21:04:44 +0000 | [diff] [blame] | 572 | } |
| 573 | } |
Chris Lattner | f1fef65 | 2001-10-13 06:35:09 +0000 | [diff] [blame] | 574 | return ResultSlot; |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | // Okay, everything is happy, actually insert the silly value now... |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 578 | return doInsertValue(D); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 581 | // doInsertValue - This is a small helper function to be called only |
| 582 | // be insertValue. |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 583 | // |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 584 | int SlotCalculator::doInsertValue(const Value *D) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 585 | const Type *Typ = D->getType(); |
Chris Lattner | 9b50c15 | 2001-07-26 16:28:37 +0000 | [diff] [blame] | 586 | unsigned Ty; |
| 587 | |
| 588 | // Used for debugging DefSlot=-1 assertion... |
| 589 | //if (Typ == Type::TypeTy) |
Chris Lattner | cfe26c9 | 2001-10-01 18:26:53 +0000 | [diff] [blame] | 590 | // cerr << "Inserting type '" << cast<Type>(D)->getDescription() << "'!\n"; |
Chris Lattner | 9b50c15 | 2001-07-26 16:28:37 +0000 | [diff] [blame] | 591 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 592 | if (Typ->isDerivedType()) { |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 593 | int ValSlot = getSlot(Typ); |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 594 | if (ValSlot == -1) { // Have we already entered this type? |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 595 | // Nope, this is the first we have seen the type, process it. |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 596 | ValSlot = insertValue(Typ, true); |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 597 | assert(ValSlot != -1 && "ProcessType returned -1 for a type?"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 598 | } |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 599 | Ty = (unsigned)ValSlot; |
Chris Lattner | 9b50c15 | 2001-07-26 16:28:37 +0000 | [diff] [blame] | 600 | } else { |
| 601 | Ty = Typ->getPrimitiveID(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | if (Table.size() <= Ty) // Make sure we have the type plane allocated... |
| 605 | Table.resize(Ty+1, TypePlane()); |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 606 | |
| 607 | // If this is the first value to get inserted into the type plane, make sure |
| 608 | // to insert the implicit null value... |
Chris Lattner | 80b9734 | 2004-01-17 23:25:43 +0000 | [diff] [blame] | 609 | if (Table[Ty].empty() && BuildBytecodeInfo && hasNullValue(Ty)) { |
Chris Lattner | 3413d15 | 2003-03-19 20:57:22 +0000 | [diff] [blame] | 610 | Value *ZeroInitializer = Constant::getNullValue(Typ); |
| 611 | |
| 612 | // If we are pushing zeroinit, it will be handled below. |
| 613 | if (D != ZeroInitializer) { |
| 614 | Table[Ty].push_back(ZeroInitializer); |
| 615 | NodeMap[ZeroInitializer] = 0; |
| 616 | } |
| 617 | } |
| 618 | |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 619 | // Insert node into table and NodeMap... |
| 620 | unsigned DestSlot = NodeMap[D] = Table[Ty].size(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 621 | Table[Ty].push_back(D); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 622 | |
Chris Lattner | f1fef65 | 2001-10-13 06:35:09 +0000 | [diff] [blame] | 623 | SC_DEBUG(" Inserting value [" << Ty << "] = " << D << " slot=" << |
| 624 | DestSlot << " ["); |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 625 | // G = Global, C = Constant, T = Type, F = Function, o = other |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 626 | SC_DEBUG((isa<GlobalVariable>(D) ? "G" : (isa<Constant>(D) ? "C" : |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 627 | (isa<Type>(D) ? "T" : (isa<Function>(D) ? "F" : "o"))))); |
Chris Lattner | f1fef65 | 2001-10-13 06:35:09 +0000 | [diff] [blame] | 628 | SC_DEBUG("]\n"); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 629 | return (int)DestSlot; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 630 | } |