blob: 6b83a116e5202c86bbe871fb479b2a8f661f7cb3 [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
24#define SC_DEBUG(X) cerr << X
25#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 Lattnerf1fef652001-10-13 06:35:09 +000070 // Add all of the constants that the global variables might refer to first.
71 //
72 for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
73 I != E; ++I) {
Chris Lattner7e708292002-06-25 16:13:24 +000074 if (I->hasInitializer())
75 insertValue(I->getInitializer());
Chris Lattnerf1fef652001-10-13 06:35:09 +000076 }
77
Chris Lattner70cc3392001-09-10 07:58:01 +000078 // Add all of the global variables to the value table...
79 //
Chris Lattner7e708292002-06-25 16:13:24 +000080 for(Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
81 I != E; ++I)
82 insertValue(I);
Chris Lattner70cc3392001-09-10 07:58:01 +000083
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000084 // Scavenge the types out of the functions, then add the functions themselves
85 // to the value table...
Chris Lattner9a297902001-09-07 16:31:52 +000086 //
Chris Lattner7e708292002-06-25 16:13:24 +000087 for(Module::const_iterator I = TheModule->begin(), E = TheModule->end();
88 I != E; ++I)
89 insertValue(I);
Chris Lattner9a297902001-09-07 16:31:52 +000090
Chris Lattner70cc3392001-09-10 07:58:01 +000091 // Insert constants that are named at module level into the slot pool so that
92 // the module symbol table can refer to them...
93 //
Chris Lattner9a297902001-09-07 16:31:52 +000094 if (TheModule->hasSymbolTable() && !IgnoreNamedNodes) {
95 SC_DEBUG("Inserting SymbolTable values:\n");
96 processSymbolTable(TheModule->getSymbolTable());
97 }
98
99 SC_DEBUG("end processModule!\n");
100}
101
102// processSymbolTable - Insert all of the values in the specified symbol table
103// into the values table...
104//
105void SlotCalculator::processSymbolTable(const SymbolTable *ST) {
106 for (SymbolTable::const_iterator I = ST->begin(), E = ST->end(); I != E; ++I)
107 for (SymbolTable::type_const_iterator TI = I->second.begin(),
108 TE = I->second.end(); TI != TE; ++TI)
109 insertValue(TI->second);
110}
111
112void SlotCalculator::processSymbolTableConstants(const SymbolTable *ST) {
113 for (SymbolTable::const_iterator I = ST->begin(), E = ST->end(); I != E; ++I)
114 for (SymbolTable::type_const_iterator TI = I->second.begin(),
115 TE = I->second.end(); TI != TE; ++TI)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000116 if (isa<Constant>(TI->second))
Chris Lattner9a297902001-09-07 16:31:52 +0000117 insertValue(TI->second);
118}
119
120
Chris Lattnerb5794002002-04-07 22:49:37 +0000121void SlotCalculator::incorporateFunction(const Function *M) {
Chris Lattner00950542001-06-06 20:29:01 +0000122 assert(ModuleLevel.size() == 0 && "Module already incorporated!");
123
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000124 SC_DEBUG("begin processFunction!\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000125
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000126 // Save the Table state before we process the function...
Chris Lattner9a297902001-09-07 16:31:52 +0000127 for (unsigned i = 0; i < Table.size(); ++i)
Chris Lattner00950542001-06-06 20:29:01 +0000128 ModuleLevel.push_back(Table[i].size());
Chris Lattner9a297902001-09-07 16:31:52 +0000129
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000130 SC_DEBUG("Inserting function arguments\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000131
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000132 // Iterate over function arguments, adding them to the value table...
Chris Lattner7e708292002-06-25 16:13:24 +0000133 for(Function::const_aiterator I = M->abegin(), E = M->aend(); I != E; ++I)
134 insertValue(I);
Chris Lattner9a297902001-09-07 16:31:52 +0000135
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000136 // Iterate over all of the instructions in the function, looking for constant
Chris Lattner9a297902001-09-07 16:31:52 +0000137 // values that are referenced. Add these to the value pools before any
138 // nonconstant values. This will be turned into the constant pool for the
139 // bytecode writer.
140 //
141 if (!IgnoreNamedNodes) { // Assembly writer does not need this!
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000142 SC_DEBUG("Inserting function constants:\n";
Chris Lattner9a297902001-09-07 16:31:52 +0000143 for (constant_iterator I = constant_begin(M), E = constant_end(M);
144 I != E; ++I) {
Chris Lattnerf759c4e2002-04-16 21:36:59 +0000145 cerr << " " << *I->getType()
146 << " " << *I << "\n";
Chris Lattner9a297902001-09-07 16:31:52 +0000147 });
148
149 // Emit all of the constants that are being used by the instructions in the
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000150 // function...
Chris Lattner9a297902001-09-07 16:31:52 +0000151 for_each(constant_begin(M), constant_end(M),
152 bind_obj(this, &SlotCalculator::insertValue));
153
154 // If there is a symbol table, it is possible that the user has names for
155 // constants that are not being used. In this case, we will have problems
156 // if we don't emit the constants now, because otherwise we will get
157 // symboltable references to constants not in the output. Scan for these
158 // constants now.
159 //
160 if (M->hasSymbolTable())
161 processSymbolTableConstants(M->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +0000162 }
163
Chris Lattner9a297902001-09-07 16:31:52 +0000164 SC_DEBUG("Inserting Labels:\n");
165
166 // Iterate over basic blocks, adding them to the value table...
Chris Lattner7e708292002-06-25 16:13:24 +0000167 for (Function::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
168 insertValue(I);
169 /* for_each(M->begin(), M->end(),
170 bind_obj(this, &SlotCalculator::insertValue));*/
Chris Lattner9a297902001-09-07 16:31:52 +0000171
172 SC_DEBUG("Inserting Instructions:\n");
173
174 // Add all of the instructions to the type planes...
Chris Lattner221d6882002-02-12 21:07:25 +0000175 for_each(inst_begin(M), inst_end(M),
Chris Lattner9a297902001-09-07 16:31:52 +0000176 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 Lattner2fbfdcf2002-04-07 20:49:59 +0000183 SC_DEBUG("end processFunction!\n");
Chris Lattner00950542001-06-06 20:29:01 +0000184}
185
Chris Lattnerb5794002002-04-07 22:49:37 +0000186void SlotCalculator::purgeFunction() {
Chris Lattner00950542001-06-06 20:29:01 +0000187 assert(ModuleLevel.size() != 0 && "Module not incorporated!");
188 unsigned NumModuleTypes = ModuleLevel.size();
189
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000190 SC_DEBUG("begin purgeFunction!\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000191
Chris Lattner00950542001-06-06 20:29:01 +0000192 // First, remove values from existing type planes
193 for (unsigned i = 0; i < NumModuleTypes; ++i) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000194 unsigned ModuleSize = ModuleLevel[i]; // Size of plane before function came
Chris Lattner9a297902001-09-07 16:31:52 +0000195 TypePlane &CurPlane = Table[i];
Chris Lattnerf1fef652001-10-13 06:35:09 +0000196 //SC_DEBUG("Processing Plane " <<i<< " of size " << CurPlane.size() <<endl);
Chris Lattner9a297902001-09-07 16:31:52 +0000197
198 while (CurPlane.size() != ModuleSize) {
Chris Lattnerf1fef652001-10-13 06:35:09 +0000199 //SC_DEBUG(" Removing [" << i << "] Value=" << CurPlane.back() << "\n");
Chris Lattner697954c2002-01-20 22:54:45 +0000200 std::map<const Value *, unsigned>::iterator NI =
201 NodeMap.find(CurPlane.back());
Chris Lattner9a297902001-09-07 16:31:52 +0000202 assert(NI != NodeMap.end() && "Node not in nodemap?");
203 NodeMap.erase(NI); // Erase from nodemap
204 CurPlane.pop_back(); // Shrink plane
Chris Lattner00950542001-06-06 20:29:01 +0000205 }
206 }
207
208 // We don't need this state anymore, free it up.
209 ModuleLevel.clear();
210
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000211 // Next, remove any type planes defined by the function...
Chris Lattner00950542001-06-06 20:29:01 +0000212 while (NumModuleTypes != Table.size()) {
213 TypePlane &Plane = Table.back();
Chris Lattner9a297902001-09-07 16:31:52 +0000214 SC_DEBUG("Removing Plane " << (Table.size()-1) << " of size "
215 << Plane.size() << endl);
Chris Lattner00950542001-06-06 20:29:01 +0000216 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 Lattner00950542001-06-06 20:29:01 +0000223
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000224 SC_DEBUG("end purgeFunction!\n");
Chris Lattner00950542001-06-06 20:29:01 +0000225}
226
227int SlotCalculator::getValSlot(const Value *D) const {
Chris Lattner697954c2002-01-20 22:54:45 +0000228 std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(D);
Chris Lattner00950542001-06-06 20:29:01 +0000229 if (I == NodeMap.end()) return -1;
230
231 return (int)I->second;
232}
233
Chris Lattner9a297902001-09-07 16:31:52 +0000234
235int SlotCalculator::insertValue(const Value *D) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000236 if (isa<Constant>(D) || isa<GlobalVariable>(D)) {
Chris Lattnerf1fef652001-10-13 06:35:09 +0000237 const User *U = cast<const User>(D);
Chris Lattner9a297902001-09-07 16:31:52 +0000238 // This makes sure that if a constant has uses (for example an array
Chris Lattnerd70684f2001-09-18 04:01:05 +0000239 // of const ints), that they are inserted also. Same for global variable
240 // initializers.
Chris Lattner9a297902001-09-07 16:31:52 +0000241 //
Chris Lattner7a176752001-12-04 00:03:30 +0000242 for(User::const_op_iterator I = U->op_begin(), E = U->op_end(); I != E; ++I)
Chris Lattnerf1fef652001-10-13 06:35:09 +0000243 if (!isa<GlobalValue>(*I)) // Don't chain insert global values
244 insertValue(*I);
Chris Lattner9a297902001-09-07 16:31:52 +0000245 }
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
253int 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 Lattner00950542001-06-06 20:29:01 +0000256
257 // If this node does not contribute to a plane, or if the node has a
Chris Lattner9a297902001-09-07 16:31:52 +0000258 // 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 Lattner00950542001-06-06 20:29:01 +0000260 //
Chris Lattner9b50c152001-07-26 16:28:37 +0000261 if (!dontIgnore) // Don't ignore nonignorables!
262 if (D->getType() == Type::VoidTy || // Ignore void type nodes
Chris Lattner9a297902001-09-07 16:31:52 +0000263 (IgnoreNamedNodes && // Ignore named and constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000264 (D->hasName() || isa<Constant>(D)) && !isa<Type>(D))) {
Chris Lattner9a297902001-09-07 16:31:52 +0000265 SC_DEBUG("ignored value " << D << endl);
266 return -1; // We do need types unconditionally though
267 }
Chris Lattner00950542001-06-06 20:29:01 +0000268
Chris Lattner9a297902001-09-07 16:31:52 +0000269 // If it's a type, make sure that all subtypes of the type are included...
Chris Lattnercfe26c92001-10-01 18:26:53 +0000270 if (const Type *TheTy = dyn_cast<const Type>(D)) {
Chris Lattnerf1fef652001-10-13 06:35:09 +0000271
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 Lattner9a297902001-09-07 16:31:52 +0000284
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 Lattner3ff43872001-09-28 22:56:31 +0000290 for (df_iterator<const Type*> I = df_begin(TheTy, true),
291 E = df_end(TheTy); I != E; ++I)
Chris Lattner9a297902001-09-07 16:31:52 +0000292 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 Lattnerf1fef652001-10-13 06:35:09 +0000297 int Slot = doInsertVal(SubTy);
298 SC_DEBUG(" Inserted subtype: " << SubTy->getDescription() <<
299 " slot=" << Slot << endl);
Chris Lattner9a297902001-09-07 16:31:52 +0000300 }
301 }
Chris Lattnerf1fef652001-10-13 06:35:09 +0000302 return ResultSlot;
Chris Lattner9a297902001-09-07 16:31:52 +0000303 }
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//
312int SlotCalculator::doInsertVal(const Value *D) {
Chris Lattner00950542001-06-06 20:29:01 +0000313 const Type *Typ = D->getType();
Chris Lattner9b50c152001-07-26 16:28:37 +0000314 unsigned Ty;
315
316 // Used for debugging DefSlot=-1 assertion...
317 //if (Typ == Type::TypeTy)
Chris Lattnercfe26c92001-10-01 18:26:53 +0000318 // cerr << "Inserting type '" << cast<Type>(D)->getDescription() << "'!\n";
Chris Lattner9b50c152001-07-26 16:28:37 +0000319
Chris Lattner00950542001-06-06 20:29:01 +0000320 if (Typ->isDerivedType()) {
321 int DefSlot = getValSlot(Typ);
322 if (DefSlot == -1) { // Have we already entered this type?
Chris Lattner9a297902001-09-07 16:31:52 +0000323 // 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 Lattner00950542001-06-06 20:29:01 +0000326 }
327 Ty = (unsigned)DefSlot;
Chris Lattner9b50c152001-07-26 16:28:37 +0000328 } else {
329 Ty = Typ->getPrimitiveID();
Chris Lattner00950542001-06-06 20:29:01 +0000330 }
331
332 if (Table.size() <= Ty) // Make sure we have the type plane allocated...
333 Table.resize(Ty+1, TypePlane());
334
Chris Lattner9a297902001-09-07 16:31:52 +0000335 // Insert node into table and NodeMap...
336 unsigned DestSlot = NodeMap[D] = Table[Ty].size();
Chris Lattner00950542001-06-06 20:29:01 +0000337 Table[Ty].push_back(D);
Chris Lattner9a297902001-09-07 16:31:52 +0000338
Chris Lattnerf1fef652001-10-13 06:35:09 +0000339 SC_DEBUG(" Inserting value [" << Ty << "] = " << D << " slot=" <<
340 DestSlot << " [");
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000341 // G = Global, C = Constant, T = Type, F = Function, o = other
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000342 SC_DEBUG((isa<GlobalVariable>(D) ? "G" : (isa<Constant>(D) ? "C" :
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000343 (isa<Type>(D) ? "T" : (isa<Function>(D) ? "F" : "o")))));
Chris Lattnerf1fef652001-10-13 06:35:09 +0000344 SC_DEBUG("]\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000345 return (int)DestSlot;
Chris Lattner00950542001-06-06 20:29:01 +0000346}