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