Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- SlotCalculator.cpp - Calculate what slots values land in ------------=// |
| 2 | // |
| 3 | // This file implements a useful analysis step to figure out what numbered |
| 4 | // slots values in a program will land in (keeping track of per plane |
| 5 | // information as required. |
| 6 | // |
| 7 | // This is used primarily for when writing a file to disk, either in bytecode |
| 8 | // or source format. |
| 9 | // |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 12 | #include "llvm/SlotCalculator.h" |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 13 | #include "llvm/Analysis/ConstantsScanner.h" |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 14 | #include "llvm/Function.h" |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 15 | #include "llvm/GlobalVariable.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 16 | #include "llvm/Module.h" |
| 17 | #include "llvm/BasicBlock.h" |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 18 | #include "llvm/ConstantVals.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 19 | #include "llvm/iOther.h" |
| 20 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 21 | #include "llvm/SymbolTable.h" |
Chris Lattner | 73e2142 | 2002-04-09 19:48:49 +0000 | [diff] [blame^] | 22 | #include "llvm/Argument.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 23 | #include "Support/DepthFirstIterator.h" |
| 24 | #include "Support/STLExtras.h" |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 25 | #include <algorithm> |
| 26 | |
| 27 | #if 0 |
| 28 | #define SC_DEBUG(X) cerr << X |
| 29 | #else |
| 30 | #define SC_DEBUG(X) |
| 31 | #endif |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 32 | |
| 33 | SlotCalculator::SlotCalculator(const Module *M, bool IgnoreNamed) { |
| 34 | IgnoreNamedNodes = IgnoreNamed; |
| 35 | TheModule = M; |
| 36 | |
| 37 | // Preload table... Make sure that all of the primitive types are in the table |
| 38 | // and that their Primitive ID is equal to their slot # |
| 39 | // |
| 40 | for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) { |
| 41 | assert(Type::getPrimitiveType((Type::PrimitiveID)i)); |
Chris Lattner | 9b50c15 | 2001-07-26 16:28:37 +0000 | [diff] [blame] | 42 | insertVal(Type::getPrimitiveType((Type::PrimitiveID)i), true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | if (M == 0) return; // Empty table... |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 46 | processModule(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 49 | SlotCalculator::SlotCalculator(const Function *M, bool IgnoreNamed) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 50 | IgnoreNamedNodes = IgnoreNamed; |
| 51 | TheModule = M ? M->getParent() : 0; |
| 52 | |
| 53 | // Preload table... Make sure that all of the primitive types are in the table |
| 54 | // and that their Primitive ID is equal to their slot # |
| 55 | // |
| 56 | for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) { |
| 57 | assert(Type::getPrimitiveType((Type::PrimitiveID)i)); |
Chris Lattner | 9b50c15 | 2001-07-26 16:28:37 +0000 | [diff] [blame] | 58 | insertVal(Type::getPrimitiveType((Type::PrimitiveID)i), true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | if (TheModule == 0) return; // Empty table... |
| 62 | |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 63 | processModule(); // Process module level stuff |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 64 | incorporateFunction(M); // Start out in incorporated state |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 67 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 68 | // processModule - Process all of the module level function declarations and |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 69 | // types that are available. |
| 70 | // |
| 71 | void SlotCalculator::processModule() { |
| 72 | SC_DEBUG("begin processModule!\n"); |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 73 | |
Chris Lattner | f1fef65 | 2001-10-13 06:35:09 +0000 | [diff] [blame] | 74 | // Add all of the constants that the global variables might refer to first. |
| 75 | // |
| 76 | for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend(); |
| 77 | I != E; ++I) { |
| 78 | if ((*I)->hasInitializer()) |
| 79 | insertValue((*I)->getInitializer()); |
| 80 | } |
| 81 | |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 82 | // Add all of the global variables to the value table... |
| 83 | // |
| 84 | for_each(TheModule->gbegin(), TheModule->gend(), |
| 85 | bind_obj(this, &SlotCalculator::insertValue)); |
| 86 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 87 | // Scavenge the types out of the functions, then add the functions themselves |
| 88 | // to the value table... |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 89 | // |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 90 | for_each(TheModule->begin(), TheModule->end(), // Insert functions... |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 91 | bind_obj(this, &SlotCalculator::insertValue)); |
| 92 | |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 93 | // Insert constants that are named at module level into the slot pool so that |
| 94 | // the module symbol table can refer to them... |
| 95 | // |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 96 | if (TheModule->hasSymbolTable() && !IgnoreNamedNodes) { |
| 97 | SC_DEBUG("Inserting SymbolTable values:\n"); |
| 98 | processSymbolTable(TheModule->getSymbolTable()); |
| 99 | } |
| 100 | |
| 101 | SC_DEBUG("end processModule!\n"); |
| 102 | } |
| 103 | |
| 104 | // processSymbolTable - Insert all of the values in the specified symbol table |
| 105 | // into the values table... |
| 106 | // |
| 107 | void SlotCalculator::processSymbolTable(const SymbolTable *ST) { |
| 108 | for (SymbolTable::const_iterator I = ST->begin(), E = ST->end(); I != E; ++I) |
| 109 | for (SymbolTable::type_const_iterator TI = I->second.begin(), |
| 110 | TE = I->second.end(); TI != TE; ++TI) |
| 111 | insertValue(TI->second); |
| 112 | } |
| 113 | |
| 114 | void SlotCalculator::processSymbolTableConstants(const SymbolTable *ST) { |
| 115 | for (SymbolTable::const_iterator I = ST->begin(), E = ST->end(); I != E; ++I) |
| 116 | for (SymbolTable::type_const_iterator TI = I->second.begin(), |
| 117 | TE = I->second.end(); TI != TE; ++TI) |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 118 | if (isa<Constant>(TI->second)) |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 119 | insertValue(TI->second); |
| 120 | } |
| 121 | |
| 122 | |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 123 | void SlotCalculator::incorporateFunction(const Function *M) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 124 | assert(ModuleLevel.size() == 0 && "Module already incorporated!"); |
| 125 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 126 | SC_DEBUG("begin processFunction!\n"); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 127 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 128 | // Save the Table state before we process the function... |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 129 | for (unsigned i = 0; i < Table.size(); ++i) |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 130 | ModuleLevel.push_back(Table[i].size()); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 131 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 132 | SC_DEBUG("Inserting function arguments\n"); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 134 | // Iterate over function arguments, adding them to the value table... |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 135 | for_each(M->getArgumentList().begin(), M->getArgumentList().end(), |
| 136 | bind_obj(this, &SlotCalculator::insertValue)); |
| 137 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 138 | // Iterate over all of the instructions in the function, looking for constant |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 139 | // values that are referenced. Add these to the value pools before any |
| 140 | // nonconstant values. This will be turned into the constant pool for the |
| 141 | // bytecode writer. |
| 142 | // |
| 143 | if (!IgnoreNamedNodes) { // Assembly writer does not need this! |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 144 | SC_DEBUG("Inserting function constants:\n"; |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 145 | for (constant_iterator I = constant_begin(M), E = constant_end(M); |
| 146 | I != E; ++I) { |
| 147 | cerr << " " << I->getType()->getDescription() |
| 148 | << " " << I->getStrValue() << endl; |
| 149 | }); |
| 150 | |
| 151 | // Emit all of the constants that are being used by the instructions in the |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 152 | // function... |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 153 | for_each(constant_begin(M), constant_end(M), |
| 154 | bind_obj(this, &SlotCalculator::insertValue)); |
| 155 | |
| 156 | // If there is a symbol table, it is possible that the user has names for |
| 157 | // constants that are not being used. In this case, we will have problems |
| 158 | // if we don't emit the constants now, because otherwise we will get |
| 159 | // symboltable references to constants not in the output. Scan for these |
| 160 | // constants now. |
| 161 | // |
| 162 | if (M->hasSymbolTable()) |
| 163 | processSymbolTableConstants(M->getSymbolTable()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 166 | SC_DEBUG("Inserting Labels:\n"); |
| 167 | |
| 168 | // Iterate over basic blocks, adding them to the value table... |
| 169 | for_each(M->begin(), M->end(), |
| 170 | bind_obj(this, &SlotCalculator::insertValue)); |
| 171 | |
| 172 | SC_DEBUG("Inserting Instructions:\n"); |
| 173 | |
| 174 | // Add all of the instructions to the type planes... |
Chris Lattner | 221d688 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 175 | for_each(inst_begin(M), inst_end(M), |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 176 | bind_obj(this, &SlotCalculator::insertValue)); |
| 177 | |
| 178 | if (M->hasSymbolTable() && !IgnoreNamedNodes) { |
| 179 | SC_DEBUG("Inserting SymbolTable values:\n"); |
| 180 | processSymbolTable(M->getSymbolTable()); |
| 181 | } |
| 182 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 183 | SC_DEBUG("end processFunction!\n"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 186 | void SlotCalculator::purgeFunction() { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 187 | assert(ModuleLevel.size() != 0 && "Module not incorporated!"); |
| 188 | unsigned NumModuleTypes = ModuleLevel.size(); |
| 189 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 190 | SC_DEBUG("begin purgeFunction!\n"); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 191 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 192 | // First, remove values from existing type planes |
| 193 | for (unsigned i = 0; i < NumModuleTypes; ++i) { |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 194 | unsigned ModuleSize = ModuleLevel[i]; // Size of plane before function came |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 195 | TypePlane &CurPlane = Table[i]; |
Chris Lattner | f1fef65 | 2001-10-13 06:35:09 +0000 | [diff] [blame] | 196 | //SC_DEBUG("Processing Plane " <<i<< " of size " << CurPlane.size() <<endl); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 197 | |
| 198 | while (CurPlane.size() != ModuleSize) { |
Chris Lattner | f1fef65 | 2001-10-13 06:35:09 +0000 | [diff] [blame] | 199 | //SC_DEBUG(" Removing [" << i << "] Value=" << CurPlane.back() << "\n"); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 200 | std::map<const Value *, unsigned>::iterator NI = |
| 201 | NodeMap.find(CurPlane.back()); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 202 | assert(NI != NodeMap.end() && "Node not in nodemap?"); |
| 203 | NodeMap.erase(NI); // Erase from nodemap |
| 204 | CurPlane.pop_back(); // Shrink plane |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
| 208 | // We don't need this state anymore, free it up. |
| 209 | ModuleLevel.clear(); |
| 210 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 211 | // Next, remove any type planes defined by the function... |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 212 | while (NumModuleTypes != Table.size()) { |
| 213 | TypePlane &Plane = Table.back(); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 214 | SC_DEBUG("Removing Plane " << (Table.size()-1) << " of size " |
| 215 | << Plane.size() << endl); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 216 | while (Plane.size()) { |
| 217 | NodeMap.erase(NodeMap.find(Plane.back())); // Erase from nodemap |
| 218 | Plane.pop_back(); // Shrink plane |
| 219 | } |
| 220 | |
| 221 | Table.pop_back(); // Nuke the plane, we don't like it. |
| 222 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 223 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 224 | SC_DEBUG("end purgeFunction!\n"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | int SlotCalculator::getValSlot(const Value *D) const { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 228 | std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(D); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 229 | if (I == NodeMap.end()) return -1; |
| 230 | |
| 231 | return (int)I->second; |
| 232 | } |
| 233 | |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 234 | |
| 235 | int SlotCalculator::insertValue(const Value *D) { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 236 | if (isa<Constant>(D) || isa<GlobalVariable>(D)) { |
Chris Lattner | f1fef65 | 2001-10-13 06:35:09 +0000 | [diff] [blame] | 237 | const User *U = cast<const User>(D); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 238 | // This makes sure that if a constant has uses (for example an array |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 239 | // of const ints), that they are inserted also. Same for global variable |
| 240 | // initializers. |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 241 | // |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 242 | for(User::const_op_iterator I = U->op_begin(), E = U->op_end(); I != E; ++I) |
Chris Lattner | f1fef65 | 2001-10-13 06:35:09 +0000 | [diff] [blame] | 243 | if (!isa<GlobalValue>(*I)) // Don't chain insert global values |
| 244 | insertValue(*I); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | int SlotNo = getValSlot(D); // Check to see if it's already in! |
| 248 | if (SlotNo != -1) return SlotNo; |
| 249 | return insertVal(D); |
| 250 | } |
| 251 | |
| 252 | |
| 253 | int SlotCalculator::insertVal(const Value *D, bool dontIgnore = false) { |
| 254 | assert(D && "Can't insert a null value!"); |
| 255 | assert(getValSlot(D) == -1 && "Value is already in the table!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 256 | |
| 257 | // 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] | 258 | // name and we don't want names, then ignore the silly node... Note that types |
| 259 | // 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] | 260 | // |
Chris Lattner | 9b50c15 | 2001-07-26 16:28:37 +0000 | [diff] [blame] | 261 | if (!dontIgnore) // Don't ignore nonignorables! |
| 262 | if (D->getType() == Type::VoidTy || // Ignore void type nodes |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 263 | (IgnoreNamedNodes && // Ignore named and constants |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 264 | (D->hasName() || isa<Constant>(D)) && !isa<Type>(D))) { |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 265 | SC_DEBUG("ignored value " << D << endl); |
| 266 | return -1; // We do need types unconditionally though |
| 267 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 268 | |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 269 | // If it's a type, make sure that all subtypes of the type are included... |
Chris Lattner | cfe26c9 | 2001-10-01 18:26:53 +0000 | [diff] [blame] | 270 | if (const Type *TheTy = dyn_cast<const Type>(D)) { |
Chris Lattner | f1fef65 | 2001-10-13 06:35:09 +0000 | [diff] [blame] | 271 | |
| 272 | // Insert the current type before any subtypes. This is important because |
| 273 | // recursive types elements are inserted in a bottom up order. Changing |
| 274 | // this here can break things. For example: |
| 275 | // |
| 276 | // global { \2 * } { { \2 }* null } |
| 277 | // |
| 278 | int ResultSlot; |
| 279 | if ((ResultSlot = getValSlot(TheTy)) == -1) { |
| 280 | ResultSlot = doInsertVal(TheTy); |
| 281 | SC_DEBUG(" Inserted type: " << TheTy->getDescription() << " slot=" << |
| 282 | ResultSlot << endl); |
| 283 | } |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 284 | |
| 285 | // Loop over any contained types in the definition... in reverse depth first |
| 286 | // order. This assures that all of the leafs of a type are output before |
| 287 | // the type itself is. This also assures us that we will not hit infinite |
| 288 | // recursion on recursive types... |
| 289 | // |
Chris Lattner | 3ff4387 | 2001-09-28 22:56:31 +0000 | [diff] [blame] | 290 | for (df_iterator<const Type*> I = df_begin(TheTy, true), |
| 291 | E = df_end(TheTy); I != E; ++I) |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 292 | if (*I != TheTy) { |
| 293 | // If we haven't seen this sub type before, add it to our type table! |
| 294 | const Type *SubTy = *I; |
| 295 | if (getValSlot(SubTy) == -1) { |
| 296 | SC_DEBUG(" Inserting subtype: " << SubTy->getDescription() << endl); |
Chris Lattner | f1fef65 | 2001-10-13 06:35:09 +0000 | [diff] [blame] | 297 | int Slot = doInsertVal(SubTy); |
| 298 | SC_DEBUG(" Inserted subtype: " << SubTy->getDescription() << |
| 299 | " slot=" << Slot << endl); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 300 | } |
| 301 | } |
Chris Lattner | f1fef65 | 2001-10-13 06:35:09 +0000 | [diff] [blame] | 302 | return ResultSlot; |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | // Okay, everything is happy, actually insert the silly value now... |
| 306 | return doInsertVal(D); |
| 307 | } |
| 308 | |
| 309 | |
| 310 | // doInsertVal - This is a small helper function to be called only be insertVal. |
| 311 | // |
| 312 | int SlotCalculator::doInsertVal(const Value *D) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 313 | const Type *Typ = D->getType(); |
Chris Lattner | 9b50c15 | 2001-07-26 16:28:37 +0000 | [diff] [blame] | 314 | unsigned Ty; |
| 315 | |
| 316 | // Used for debugging DefSlot=-1 assertion... |
| 317 | //if (Typ == Type::TypeTy) |
Chris Lattner | cfe26c9 | 2001-10-01 18:26:53 +0000 | [diff] [blame] | 318 | // cerr << "Inserting type '" << cast<Type>(D)->getDescription() << "'!\n"; |
Chris Lattner | 9b50c15 | 2001-07-26 16:28:37 +0000 | [diff] [blame] | 319 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 320 | if (Typ->isDerivedType()) { |
| 321 | int DefSlot = getValSlot(Typ); |
| 322 | if (DefSlot == -1) { // Have we already entered this type? |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 323 | // Nope, this is the first we have seen the type, process it. |
| 324 | DefSlot = insertVal(Typ, true); |
| 325 | assert(DefSlot != -1 && "ProcessType returned -1 for a type?"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 326 | } |
| 327 | Ty = (unsigned)DefSlot; |
Chris Lattner | 9b50c15 | 2001-07-26 16:28:37 +0000 | [diff] [blame] | 328 | } else { |
| 329 | Ty = Typ->getPrimitiveID(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | if (Table.size() <= Ty) // Make sure we have the type plane allocated... |
| 333 | Table.resize(Ty+1, TypePlane()); |
| 334 | |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 335 | // Insert node into table and NodeMap... |
| 336 | unsigned DestSlot = NodeMap[D] = Table[Ty].size(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 337 | Table[Ty].push_back(D); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 338 | |
Chris Lattner | f1fef65 | 2001-10-13 06:35:09 +0000 | [diff] [blame] | 339 | SC_DEBUG(" Inserting value [" << Ty << "] = " << D << " slot=" << |
| 340 | DestSlot << " ["); |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 341 | // G = Global, C = Constant, T = Type, F = Function, o = other |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 342 | SC_DEBUG((isa<GlobalVariable>(D) ? "G" : (isa<Constant>(D) ? "C" : |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 343 | (isa<Type>(D) ? "T" : (isa<Function>(D) ? "F" : "o"))))); |
Chris Lattner | f1fef65 | 2001-10-13 06:35:09 +0000 | [diff] [blame] | 344 | SC_DEBUG("]\n"); |
Chris Lattner | 9a29790 | 2001-09-07 16:31:52 +0000 | [diff] [blame] | 345 | return (int)DestSlot; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 346 | } |