blob: c9560c1d8c2fb9d7cfebd46d825fa8f0aa0e71b6 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- 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 Lattnerb5794002002-04-07 22:49:37 +000012#include "llvm/SlotCalculator.h"
Chris Lattner9a297902001-09-07 16:31:52 +000013#include "llvm/Analysis/ConstantsScanner.h"
Chris Lattner00950542001-06-06 20:29:01 +000014#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000015#include "llvm/iOther.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000016#include "llvm/Constant.h"
Chris Lattner00950542001-06-06 20:29:01 +000017#include "llvm/DerivedTypes.h"
Chris Lattner9a297902001-09-07 16:31:52 +000018#include "llvm/SymbolTable.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000019#include "Support/DepthFirstIterator.h"
20#include "Support/STLExtras.h"
Chris Lattner9a297902001-09-07 16:31:52 +000021#include <algorithm>
22
23#if 0
Chris Lattner3413d152003-03-19 20:57:22 +000024#define SC_DEBUG(X) std::cerr << X
Chris Lattner9a297902001-09-07 16:31:52 +000025#else
26#define SC_DEBUG(X)
27#endif
Chris Lattner00950542001-06-06 20:29:01 +000028
29SlotCalculator::SlotCalculator(const Module *M, bool IgnoreNamed) {
30 IgnoreNamedNodes = IgnoreNamed;
31 TheModule = M;
32
33 // Preload table... Make sure that all of the primitive types are in the table
34 // and that their Primitive ID is equal to their slot #
35 //
36 for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) {
37 assert(Type::getPrimitiveType((Type::PrimitiveID)i));
Chris Lattner9b50c152001-07-26 16:28:37 +000038 insertVal(Type::getPrimitiveType((Type::PrimitiveID)i), true);
Chris Lattner00950542001-06-06 20:29:01 +000039 }
40
41 if (M == 0) return; // Empty table...
Chris Lattner9a297902001-09-07 16:31:52 +000042 processModule();
Chris Lattner00950542001-06-06 20:29:01 +000043}
44
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000045SlotCalculator::SlotCalculator(const Function *M, bool IgnoreNamed) {
Chris Lattner00950542001-06-06 20:29:01 +000046 IgnoreNamedNodes = IgnoreNamed;
47 TheModule = M ? M->getParent() : 0;
48
49 // Preload table... Make sure that all of the primitive types are in the table
50 // and that their Primitive ID is equal to their slot #
51 //
52 for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) {
53 assert(Type::getPrimitiveType((Type::PrimitiveID)i));
Chris Lattner9b50c152001-07-26 16:28:37 +000054 insertVal(Type::getPrimitiveType((Type::PrimitiveID)i), true);
Chris Lattner00950542001-06-06 20:29:01 +000055 }
56
57 if (TheModule == 0) return; // Empty table...
58
Chris Lattner9a297902001-09-07 16:31:52 +000059 processModule(); // Process module level stuff
Chris Lattnerb5794002002-04-07 22:49:37 +000060 incorporateFunction(M); // Start out in incorporated state
Chris Lattner00950542001-06-06 20:29:01 +000061}
62
Chris Lattner9a297902001-09-07 16:31:52 +000063
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000064// processModule - Process all of the module level function declarations and
Chris Lattner9a297902001-09-07 16:31:52 +000065// types that are available.
66//
67void SlotCalculator::processModule() {
68 SC_DEBUG("begin processModule!\n");
Chris Lattner70cc3392001-09-10 07:58:01 +000069
Chris Lattner3413d152003-03-19 20:57:22 +000070 // Add all of the global variables to the value table...
Chris Lattnerf1fef652001-10-13 06:35:09 +000071 //
72 for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
Chris Lattner479b54b2002-10-14 00:48:57 +000073 I != E; ++I)
Chris Lattner7e708292002-06-25 16:13:24 +000074 insertValue(I);
Chris Lattner70cc3392001-09-10 07:58:01 +000075
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000076 // Scavenge the types out of the functions, then add the functions themselves
77 // to the value table...
Chris Lattner9a297902001-09-07 16:31:52 +000078 //
Chris Lattner3413d152003-03-19 20:57:22 +000079 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
80 I != E; ++I)
Chris Lattner7e708292002-06-25 16:13:24 +000081 insertValue(I);
Chris Lattner9a297902001-09-07 16:31:52 +000082
Chris Lattner3413d152003-03-19 20:57:22 +000083 // Add all of the module level constants used as initializers
84 //
85 for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
86 I != E; ++I)
87 if (I->hasInitializer())
88 insertValue(I->getInitializer());
89
Chris Lattner70cc3392001-09-10 07:58:01 +000090 // Insert constants that are named at module level into the slot pool so that
91 // the module symbol table can refer to them...
92 //
Chris Lattner6e6026b2002-11-20 18:36:02 +000093 if (!IgnoreNamedNodes) {
Chris Lattner9a297902001-09-07 16:31:52 +000094 SC_DEBUG("Inserting SymbolTable values:\n");
Chris Lattner6e6026b2002-11-20 18:36:02 +000095 processSymbolTable(&TheModule->getSymbolTable());
Chris Lattner9a297902001-09-07 16:31:52 +000096 }
97
98 SC_DEBUG("end processModule!\n");
99}
100
101// processSymbolTable - Insert all of the values in the specified symbol table
102// into the values table...
103//
104void SlotCalculator::processSymbolTable(const SymbolTable *ST) {
105 for (SymbolTable::const_iterator I = ST->begin(), E = ST->end(); I != E; ++I)
106 for (SymbolTable::type_const_iterator TI = I->second.begin(),
107 TE = I->second.end(); TI != TE; ++TI)
108 insertValue(TI->second);
109}
110
111void SlotCalculator::processSymbolTableConstants(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)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000115 if (isa<Constant>(TI->second))
Chris Lattner9a297902001-09-07 16:31:52 +0000116 insertValue(TI->second);
117}
118
119
Chris Lattnerb5794002002-04-07 22:49:37 +0000120void SlotCalculator::incorporateFunction(const Function *M) {
Chris Lattner00950542001-06-06 20:29:01 +0000121 assert(ModuleLevel.size() == 0 && "Module already incorporated!");
122
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000123 SC_DEBUG("begin processFunction!\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000124
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000125 // Save the Table state before we process the function...
Chris Lattner9a297902001-09-07 16:31:52 +0000126 for (unsigned i = 0; i < Table.size(); ++i)
Chris Lattner00950542001-06-06 20:29:01 +0000127 ModuleLevel.push_back(Table[i].size());
Chris Lattner9a297902001-09-07 16:31:52 +0000128
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000129 SC_DEBUG("Inserting function arguments\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000130
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000131 // Iterate over function arguments, adding them to the value table...
Chris Lattner7e708292002-06-25 16:13:24 +0000132 for(Function::const_aiterator I = M->abegin(), E = M->aend(); I != E; ++I)
133 insertValue(I);
Chris Lattner9a297902001-09-07 16:31:52 +0000134
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000135 // Iterate over all of the instructions in the function, looking for constant
Chris Lattner9a297902001-09-07 16:31:52 +0000136 // values that are referenced. Add these to the value pools before any
137 // nonconstant values. This will be turned into the constant pool for the
138 // bytecode writer.
139 //
140 if (!IgnoreNamedNodes) { // Assembly writer does not need this!
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000141 SC_DEBUG("Inserting function constants:\n";
Chris Lattner9a297902001-09-07 16:31:52 +0000142 for (constant_iterator I = constant_begin(M), E = constant_end(M);
143 I != E; ++I) {
Chris Lattner3413d152003-03-19 20:57:22 +0000144 std::cerr << " " << *I->getType() << " " << *I << "\n";
Chris Lattner9a297902001-09-07 16:31:52 +0000145 });
146
147 // Emit all of the constants that are being used by the instructions in the
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000148 // function...
Chris Lattner9a297902001-09-07 16:31:52 +0000149 for_each(constant_begin(M), constant_end(M),
150 bind_obj(this, &SlotCalculator::insertValue));
151
152 // If there is a symbol table, it is possible that the user has names for
153 // constants that are not being used. In this case, we will have problems
154 // if we don't emit the constants now, because otherwise we will get
155 // symboltable references to constants not in the output. Scan for these
156 // constants now.
157 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000158 processSymbolTableConstants(&M->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +0000159 }
160
Chris Lattner9a297902001-09-07 16:31:52 +0000161 SC_DEBUG("Inserting Labels:\n");
162
163 // Iterate over basic blocks, adding them to the value table...
Chris Lattner7e708292002-06-25 16:13:24 +0000164 for (Function::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
165 insertValue(I);
Chris Lattner9a297902001-09-07 16:31:52 +0000166
167 SC_DEBUG("Inserting Instructions:\n");
168
169 // Add all of the instructions to the type planes...
Chris Lattner221d6882002-02-12 21:07:25 +0000170 for_each(inst_begin(M), inst_end(M),
Chris Lattner9a297902001-09-07 16:31:52 +0000171 bind_obj(this, &SlotCalculator::insertValue));
172
Chris Lattner6e6026b2002-11-20 18:36:02 +0000173 if (!IgnoreNamedNodes) {
Chris Lattner9a297902001-09-07 16:31:52 +0000174 SC_DEBUG("Inserting SymbolTable values:\n");
Chris Lattner6e6026b2002-11-20 18:36:02 +0000175 processSymbolTable(&M->getSymbolTable());
Chris Lattner9a297902001-09-07 16:31:52 +0000176 }
177
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000178 SC_DEBUG("end processFunction!\n");
Chris Lattner00950542001-06-06 20:29:01 +0000179}
180
Chris Lattnerb5794002002-04-07 22:49:37 +0000181void SlotCalculator::purgeFunction() {
Chris Lattner00950542001-06-06 20:29:01 +0000182 assert(ModuleLevel.size() != 0 && "Module not incorporated!");
183 unsigned NumModuleTypes = ModuleLevel.size();
184
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000185 SC_DEBUG("begin purgeFunction!\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000186
Chris Lattner00950542001-06-06 20:29:01 +0000187 // First, remove values from existing type planes
188 for (unsigned i = 0; i < NumModuleTypes; ++i) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000189 unsigned ModuleSize = ModuleLevel[i]; // Size of plane before function came
Chris Lattner9a297902001-09-07 16:31:52 +0000190 TypePlane &CurPlane = Table[i];
Chris Lattner479b54b2002-10-14 00:48:57 +0000191 //SC_DEBUG("Processing Plane " <<i<< " of size " << CurPlane.size() <<"\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000192
193 while (CurPlane.size() != ModuleSize) {
Chris Lattnerf1fef652001-10-13 06:35:09 +0000194 //SC_DEBUG(" Removing [" << i << "] Value=" << CurPlane.back() << "\n");
Chris Lattner697954c2002-01-20 22:54:45 +0000195 std::map<const Value *, unsigned>::iterator NI =
196 NodeMap.find(CurPlane.back());
Chris Lattner9a297902001-09-07 16:31:52 +0000197 assert(NI != NodeMap.end() && "Node not in nodemap?");
198 NodeMap.erase(NI); // Erase from nodemap
199 CurPlane.pop_back(); // Shrink plane
Chris Lattner00950542001-06-06 20:29:01 +0000200 }
201 }
202
203 // We don't need this state anymore, free it up.
204 ModuleLevel.clear();
205
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000206 // Next, remove any type planes defined by the function...
Chris Lattner00950542001-06-06 20:29:01 +0000207 while (NumModuleTypes != Table.size()) {
208 TypePlane &Plane = Table.back();
Chris Lattner9a297902001-09-07 16:31:52 +0000209 SC_DEBUG("Removing Plane " << (Table.size()-1) << " of size "
Chris Lattner479b54b2002-10-14 00:48:57 +0000210 << Plane.size() << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000211 while (Plane.size()) {
212 NodeMap.erase(NodeMap.find(Plane.back())); // Erase from nodemap
213 Plane.pop_back(); // Shrink plane
214 }
215
216 Table.pop_back(); // Nuke the plane, we don't like it.
217 }
Chris Lattner00950542001-06-06 20:29:01 +0000218
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000219 SC_DEBUG("end purgeFunction!\n");
Chris Lattner00950542001-06-06 20:29:01 +0000220}
221
222int SlotCalculator::getValSlot(const Value *D) const {
Chris Lattner697954c2002-01-20 22:54:45 +0000223 std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(D);
Chris Lattner00950542001-06-06 20:29:01 +0000224 if (I == NodeMap.end()) return -1;
225
226 return (int)I->second;
227}
228
Chris Lattner9a297902001-09-07 16:31:52 +0000229
Chris Lattner3413d152003-03-19 20:57:22 +0000230int SlotCalculator::insertValue(const Value *V) {
231 int SlotNo = getValSlot(V); // Check to see if it's already in!
Chris Lattner9a297902001-09-07 16:31:52 +0000232 if (SlotNo != -1) return SlotNo;
Chris Lattner3413d152003-03-19 20:57:22 +0000233
234 if (!isa<GlobalValue>(V))
235 if (const Constant *C = dyn_cast<Constant>(V)) {
236 // This makes sure that if a constant has uses (for example an array of
237 // const ints), that they are inserted also.
238 //
239 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
240 I != E; ++I)
241 insertValue(*I);
242 }
243
244 return insertVal(V);
Chris Lattner9a297902001-09-07 16:31:52 +0000245}
246
247
Chris Lattnerfe8041a2002-07-24 22:08:53 +0000248int SlotCalculator::insertVal(const Value *D, bool dontIgnore) {
Chris Lattner9a297902001-09-07 16:31:52 +0000249 assert(D && "Can't insert a null value!");
250 assert(getValSlot(D) == -1 && "Value is already in the table!");
Chris Lattner00950542001-06-06 20:29:01 +0000251
252 // If this node does not contribute to a plane, or if the node has a
Chris Lattner9a297902001-09-07 16:31:52 +0000253 // name and we don't want names, then ignore the silly node... Note that types
254 // do need slot numbers so that we can keep track of where other values land.
Chris Lattner00950542001-06-06 20:29:01 +0000255 //
Chris Lattner9b50c152001-07-26 16:28:37 +0000256 if (!dontIgnore) // Don't ignore nonignorables!
257 if (D->getType() == Type::VoidTy || // Ignore void type nodes
Chris Lattner9a297902001-09-07 16:31:52 +0000258 (IgnoreNamedNodes && // Ignore named and constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000259 (D->hasName() || isa<Constant>(D)) && !isa<Type>(D))) {
Chris Lattner3413d152003-03-19 20:57:22 +0000260 SC_DEBUG("ignored value " << *D << "\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000261 return -1; // We do need types unconditionally though
262 }
Chris Lattner00950542001-06-06 20:29:01 +0000263
Chris Lattner9a297902001-09-07 16:31:52 +0000264 // If it's a type, make sure that all subtypes of the type are included...
Chris Lattnercfe26c92001-10-01 18:26:53 +0000265 if (const Type *TheTy = dyn_cast<const Type>(D)) {
Chris Lattnerf1fef652001-10-13 06:35:09 +0000266
267 // Insert the current type before any subtypes. This is important because
268 // recursive types elements are inserted in a bottom up order. Changing
269 // this here can break things. For example:
270 //
271 // global { \2 * } { { \2 }* null }
272 //
273 int ResultSlot;
274 if ((ResultSlot = getValSlot(TheTy)) == -1) {
275 ResultSlot = doInsertVal(TheTy);
276 SC_DEBUG(" Inserted type: " << TheTy->getDescription() << " slot=" <<
Chris Lattner479b54b2002-10-14 00:48:57 +0000277 ResultSlot << "\n");
Chris Lattnerf1fef652001-10-13 06:35:09 +0000278 }
Chris Lattner9a297902001-09-07 16:31:52 +0000279
280 // Loop over any contained types in the definition... in reverse depth first
281 // order. This assures that all of the leafs of a type are output before
282 // the type itself is. This also assures us that we will not hit infinite
283 // recursion on recursive types...
284 //
Chris Lattner3ff43872001-09-28 22:56:31 +0000285 for (df_iterator<const Type*> I = df_begin(TheTy, true),
286 E = df_end(TheTy); I != E; ++I)
Chris Lattner9a297902001-09-07 16:31:52 +0000287 if (*I != TheTy) {
288 // If we haven't seen this sub type before, add it to our type table!
289 const Type *SubTy = *I;
290 if (getValSlot(SubTy) == -1) {
Chris Lattner479b54b2002-10-14 00:48:57 +0000291 SC_DEBUG(" Inserting subtype: " << SubTy->getDescription() << "\n");
Chris Lattnerf1fef652001-10-13 06:35:09 +0000292 int Slot = doInsertVal(SubTy);
293 SC_DEBUG(" Inserted subtype: " << SubTy->getDescription() <<
Chris Lattner479b54b2002-10-14 00:48:57 +0000294 " slot=" << Slot << "\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000295 }
296 }
Chris Lattnerf1fef652001-10-13 06:35:09 +0000297 return ResultSlot;
Chris Lattner9a297902001-09-07 16:31:52 +0000298 }
299
300 // Okay, everything is happy, actually insert the silly value now...
301 return doInsertVal(D);
302}
303
304
305// doInsertVal - This is a small helper function to be called only be insertVal.
306//
307int SlotCalculator::doInsertVal(const Value *D) {
Chris Lattner00950542001-06-06 20:29:01 +0000308 const Type *Typ = D->getType();
Chris Lattner9b50c152001-07-26 16:28:37 +0000309 unsigned Ty;
310
311 // Used for debugging DefSlot=-1 assertion...
312 //if (Typ == Type::TypeTy)
Chris Lattnercfe26c92001-10-01 18:26:53 +0000313 // cerr << "Inserting type '" << cast<Type>(D)->getDescription() << "'!\n";
Chris Lattner9b50c152001-07-26 16:28:37 +0000314
Chris Lattner00950542001-06-06 20:29:01 +0000315 if (Typ->isDerivedType()) {
Chris Lattner3413d152003-03-19 20:57:22 +0000316 int ValSlot = getValSlot(Typ);
317 if (ValSlot == -1) { // Have we already entered this type?
Chris Lattner9a297902001-09-07 16:31:52 +0000318 // Nope, this is the first we have seen the type, process it.
Chris Lattner3413d152003-03-19 20:57:22 +0000319 ValSlot = insertVal(Typ, true);
320 assert(ValSlot != -1 && "ProcessType returned -1 for a type?");
Chris Lattner00950542001-06-06 20:29:01 +0000321 }
Chris Lattner3413d152003-03-19 20:57:22 +0000322 Ty = (unsigned)ValSlot;
Chris Lattner9b50c152001-07-26 16:28:37 +0000323 } else {
324 Ty = Typ->getPrimitiveID();
Chris Lattner00950542001-06-06 20:29:01 +0000325 }
326
327 if (Table.size() <= Ty) // Make sure we have the type plane allocated...
328 Table.resize(Ty+1, TypePlane());
Chris Lattner3413d152003-03-19 20:57:22 +0000329
330 // If this is the first value to get inserted into the type plane, make sure
331 // to insert the implicit null value...
332 if (Table[Ty].empty() && Ty >= Type::FirstDerivedTyID && !IgnoreNamedNodes) {
333 Value *ZeroInitializer = Constant::getNullValue(Typ);
334
335 // If we are pushing zeroinit, it will be handled below.
336 if (D != ZeroInitializer) {
337 Table[Ty].push_back(ZeroInitializer);
338 NodeMap[ZeroInitializer] = 0;
339 }
340 }
341
Chris Lattner9a297902001-09-07 16:31:52 +0000342 // Insert node into table and NodeMap...
343 unsigned DestSlot = NodeMap[D] = Table[Ty].size();
Chris Lattner00950542001-06-06 20:29:01 +0000344 Table[Ty].push_back(D);
Chris Lattner9a297902001-09-07 16:31:52 +0000345
Chris Lattnerf1fef652001-10-13 06:35:09 +0000346 SC_DEBUG(" Inserting value [" << Ty << "] = " << D << " slot=" <<
347 DestSlot << " [");
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000348 // G = Global, C = Constant, T = Type, F = Function, o = other
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000349 SC_DEBUG((isa<GlobalVariable>(D) ? "G" : (isa<Constant>(D) ? "C" :
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000350 (isa<Type>(D) ? "T" : (isa<Function>(D) ? "F" : "o")))));
Chris Lattnerf1fef652001-10-13 06:35:09 +0000351 SC_DEBUG("]\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000352 return (int)DestSlot;
Chris Lattner00950542001-06-06 20:29:01 +0000353}