blob: 9c5d97a5881bcfaffd03b70580ed5329bb36128f [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
12#include "llvm/Analysis/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/Method.h"
Chris Lattner70cc3392001-09-10 07:58:01 +000015#include "llvm/GlobalVariable.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/Module.h"
17#include "llvm/BasicBlock.h"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000018#include "llvm/ConstantVals.h"
Chris Lattner00950542001-06-06 20:29:01 +000019#include "llvm/iOther.h"
20#include "llvm/DerivedTypes.h"
Chris Lattner9a297902001-09-07 16:31:52 +000021#include "llvm/SymbolTable.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000022#include "Support/DepthFirstIterator.h"
23#include "Support/STLExtras.h"
Chris Lattner9a297902001-09-07 16:31:52 +000024#include <algorithm>
25
26#if 0
27#define SC_DEBUG(X) cerr << X
28#else
29#define SC_DEBUG(X)
30#endif
Chris Lattner00950542001-06-06 20:29:01 +000031
32SlotCalculator::SlotCalculator(const Module *M, bool IgnoreNamed) {
33 IgnoreNamedNodes = IgnoreNamed;
34 TheModule = M;
35
36 // Preload table... Make sure that all of the primitive types are in the table
37 // and that their Primitive ID is equal to their slot #
38 //
39 for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) {
40 assert(Type::getPrimitiveType((Type::PrimitiveID)i));
Chris Lattner9b50c152001-07-26 16:28:37 +000041 insertVal(Type::getPrimitiveType((Type::PrimitiveID)i), true);
Chris Lattner00950542001-06-06 20:29:01 +000042 }
43
44 if (M == 0) return; // Empty table...
Chris Lattner9a297902001-09-07 16:31:52 +000045 processModule();
Chris Lattner00950542001-06-06 20:29:01 +000046}
47
48SlotCalculator::SlotCalculator(const Method *M, bool IgnoreNamed) {
49 IgnoreNamedNodes = IgnoreNamed;
50 TheModule = M ? M->getParent() : 0;
51
52 // Preload table... Make sure that all of the primitive types are in the table
53 // and that their Primitive ID is equal to their slot #
54 //
55 for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) {
56 assert(Type::getPrimitiveType((Type::PrimitiveID)i));
Chris Lattner9b50c152001-07-26 16:28:37 +000057 insertVal(Type::getPrimitiveType((Type::PrimitiveID)i), true);
Chris Lattner00950542001-06-06 20:29:01 +000058 }
59
60 if (TheModule == 0) return; // Empty table...
61
Chris Lattner9a297902001-09-07 16:31:52 +000062 processModule(); // Process module level stuff
63 incorporateMethod(M); // Start out in incorporated state
Chris Lattner00950542001-06-06 20:29:01 +000064}
65
Chris Lattner9a297902001-09-07 16:31:52 +000066
67// processModule - Process all of the module level method declarations and
68// types that are available.
69//
70void SlotCalculator::processModule() {
71 SC_DEBUG("begin processModule!\n");
Chris Lattner70cc3392001-09-10 07:58:01 +000072
Chris Lattnerf1fef652001-10-13 06:35:09 +000073 // Add all of the constants that the global variables might refer to first.
74 //
75 for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
76 I != E; ++I) {
77 if ((*I)->hasInitializer())
78 insertValue((*I)->getInitializer());
79 }
80
Chris Lattner70cc3392001-09-10 07:58:01 +000081 // Add all of the global variables to the value table...
82 //
83 for_each(TheModule->gbegin(), TheModule->gend(),
84 bind_obj(this, &SlotCalculator::insertValue));
85
86 // Scavenge the types out of the methods, then add the methods themselves to
87 // the value table...
Chris Lattner9a297902001-09-07 16:31:52 +000088 //
89 for_each(TheModule->begin(), TheModule->end(), // Insert methods...
90 bind_obj(this, &SlotCalculator::insertValue));
91
Chris Lattner70cc3392001-09-10 07:58:01 +000092 // Insert constants that are named at module level into the slot pool so that
93 // the module symbol table can refer to them...
94 //
Chris Lattner9a297902001-09-07 16:31:52 +000095 if (TheModule->hasSymbolTable() && !IgnoreNamedNodes) {
96 SC_DEBUG("Inserting SymbolTable values:\n");
97 processSymbolTable(TheModule->getSymbolTable());
98 }
99
100 SC_DEBUG("end processModule!\n");
101}
102
103// processSymbolTable - Insert all of the values in the specified symbol table
104// into the values table...
105//
106void SlotCalculator::processSymbolTable(const SymbolTable *ST) {
107 for (SymbolTable::const_iterator I = ST->begin(), E = ST->end(); I != E; ++I)
108 for (SymbolTable::type_const_iterator TI = I->second.begin(),
109 TE = I->second.end(); TI != TE; ++TI)
110 insertValue(TI->second);
111}
112
113void SlotCalculator::processSymbolTableConstants(const SymbolTable *ST) {
114 for (SymbolTable::const_iterator I = ST->begin(), E = ST->end(); I != E; ++I)
115 for (SymbolTable::type_const_iterator TI = I->second.begin(),
116 TE = I->second.end(); TI != TE; ++TI)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000117 if (isa<Constant>(TI->second))
Chris Lattner9a297902001-09-07 16:31:52 +0000118 insertValue(TI->second);
119}
120
121
Chris Lattner00950542001-06-06 20:29:01 +0000122void SlotCalculator::incorporateMethod(const Method *M) {
123 assert(ModuleLevel.size() == 0 && "Module already incorporated!");
124
Chris Lattner9a297902001-09-07 16:31:52 +0000125 SC_DEBUG("begin processMethod!\n");
126
Chris Lattner00950542001-06-06 20:29:01 +0000127 // Save the Table state before we process the method...
Chris Lattner9a297902001-09-07 16:31:52 +0000128 for (unsigned i = 0; i < Table.size(); ++i)
Chris Lattner00950542001-06-06 20:29:01 +0000129 ModuleLevel.push_back(Table[i].size());
Chris Lattner9a297902001-09-07 16:31:52 +0000130
131 SC_DEBUG("Inserting method arguments\n");
132
133 // Iterate over method arguments, adding them to the value table...
134 for_each(M->getArgumentList().begin(), M->getArgumentList().end(),
135 bind_obj(this, &SlotCalculator::insertValue));
136
137 // Iterate over all of the instructions in the method, looking for constant
138 // values that are referenced. Add these to the value pools before any
139 // nonconstant values. This will be turned into the constant pool for the
140 // bytecode writer.
141 //
142 if (!IgnoreNamedNodes) { // Assembly writer does not need this!
143 SC_DEBUG("Inserting method constants:\n";
144 for (constant_iterator I = constant_begin(M), E = constant_end(M);
145 I != E; ++I) {
146 cerr << " " << I->getType()->getDescription()
147 << " " << I->getStrValue() << endl;
148 });
149
150 // Emit all of the constants that are being used by the instructions in the
151 // method...
152 for_each(constant_begin(M), constant_end(M),
153 bind_obj(this, &SlotCalculator::insertValue));
154
155 // If there is a symbol table, it is possible that the user has names for
156 // constants that are not being used. In this case, we will have problems
157 // if we don't emit the constants now, because otherwise we will get
158 // symboltable references to constants not in the output. Scan for these
159 // constants now.
160 //
161 if (M->hasSymbolTable())
162 processSymbolTableConstants(M->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +0000163 }
164
Chris Lattner9a297902001-09-07 16:31:52 +0000165 SC_DEBUG("Inserting Labels:\n");
166
167 // Iterate over basic blocks, adding them to the value table...
168 for_each(M->begin(), M->end(),
169 bind_obj(this, &SlotCalculator::insertValue));
170
171 SC_DEBUG("Inserting Instructions:\n");
172
173 // Add all of the instructions to the type planes...
174 for_each(M->inst_begin(), M->inst_end(),
175 bind_obj(this, &SlotCalculator::insertValue));
176
177 if (M->hasSymbolTable() && !IgnoreNamedNodes) {
178 SC_DEBUG("Inserting SymbolTable values:\n");
179 processSymbolTable(M->getSymbolTable());
180 }
181
182 SC_DEBUG("end processMethod!\n");
Chris Lattner00950542001-06-06 20:29:01 +0000183}
184
185void SlotCalculator::purgeMethod() {
186 assert(ModuleLevel.size() != 0 && "Module not incorporated!");
187 unsigned NumModuleTypes = ModuleLevel.size();
188
Chris Lattner9a297902001-09-07 16:31:52 +0000189 SC_DEBUG("begin purgeMethod!\n");
190
Chris Lattner00950542001-06-06 20:29:01 +0000191 // First, remove values from existing type planes
192 for (unsigned i = 0; i < NumModuleTypes; ++i) {
193 unsigned ModuleSize = ModuleLevel[i]; // Size of plane before method came
Chris Lattner9a297902001-09-07 16:31:52 +0000194 TypePlane &CurPlane = Table[i];
Chris Lattnerf1fef652001-10-13 06:35:09 +0000195 //SC_DEBUG("Processing Plane " <<i<< " of size " << CurPlane.size() <<endl);
Chris Lattner9a297902001-09-07 16:31:52 +0000196
197 while (CurPlane.size() != ModuleSize) {
Chris Lattnerf1fef652001-10-13 06:35:09 +0000198 //SC_DEBUG(" Removing [" << i << "] Value=" << CurPlane.back() << "\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000199 map<const Value *, unsigned>::iterator NI = NodeMap.find(CurPlane.back());
200 assert(NI != NodeMap.end() && "Node not in nodemap?");
201 NodeMap.erase(NI); // Erase from nodemap
202 CurPlane.pop_back(); // Shrink plane
Chris Lattner00950542001-06-06 20:29:01 +0000203 }
204 }
205
206 // We don't need this state anymore, free it up.
207 ModuleLevel.clear();
208
209 // Next, remove any type planes defined by the method...
210 while (NumModuleTypes != Table.size()) {
211 TypePlane &Plane = Table.back();
Chris Lattner9a297902001-09-07 16:31:52 +0000212 SC_DEBUG("Removing Plane " << (Table.size()-1) << " of size "
213 << Plane.size() << endl);
Chris Lattner00950542001-06-06 20:29:01 +0000214 while (Plane.size()) {
215 NodeMap.erase(NodeMap.find(Plane.back())); // Erase from nodemap
216 Plane.pop_back(); // Shrink plane
217 }
218
219 Table.pop_back(); // Nuke the plane, we don't like it.
220 }
Chris Lattner00950542001-06-06 20:29:01 +0000221
Chris Lattner9a297902001-09-07 16:31:52 +0000222 SC_DEBUG("end purgeMethod!\n");
Chris Lattner00950542001-06-06 20:29:01 +0000223}
224
225int SlotCalculator::getValSlot(const Value *D) const {
226 map<const Value*, unsigned>::const_iterator I = NodeMap.find(D);
227 if (I == NodeMap.end()) return -1;
228
229 return (int)I->second;
230}
231
Chris Lattner9a297902001-09-07 16:31:52 +0000232
233int SlotCalculator::insertValue(const Value *D) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000234 if (isa<Constant>(D) || isa<GlobalVariable>(D)) {
Chris Lattnerf1fef652001-10-13 06:35:09 +0000235 const User *U = cast<const User>(D);
Chris Lattner9a297902001-09-07 16:31:52 +0000236 // This makes sure that if a constant has uses (for example an array
Chris Lattnerd70684f2001-09-18 04:01:05 +0000237 // of const ints), that they are inserted also. Same for global variable
238 // initializers.
Chris Lattner9a297902001-09-07 16:31:52 +0000239 //
Chris Lattnerf1fef652001-10-13 06:35:09 +0000240 for(User::op_const_iterator I = U->op_begin(), E = U->op_end(); I != E; ++I)
241 if (!isa<GlobalValue>(*I)) // Don't chain insert global values
242 insertValue(*I);
Chris Lattner9a297902001-09-07 16:31:52 +0000243 }
244
245 int SlotNo = getValSlot(D); // Check to see if it's already in!
246 if (SlotNo != -1) return SlotNo;
247 return insertVal(D);
248}
249
250
251int SlotCalculator::insertVal(const Value *D, bool dontIgnore = false) {
252 assert(D && "Can't insert a null value!");
253 assert(getValSlot(D) == -1 && "Value is already in the table!");
Chris Lattner00950542001-06-06 20:29:01 +0000254
255 // If this node does not contribute to a plane, or if the node has a
Chris Lattner9a297902001-09-07 16:31:52 +0000256 // name and we don't want names, then ignore the silly node... Note that types
257 // do need slot numbers so that we can keep track of where other values land.
Chris Lattner00950542001-06-06 20:29:01 +0000258 //
Chris Lattner9b50c152001-07-26 16:28:37 +0000259 if (!dontIgnore) // Don't ignore nonignorables!
260 if (D->getType() == Type::VoidTy || // Ignore void type nodes
Chris Lattner9a297902001-09-07 16:31:52 +0000261 (IgnoreNamedNodes && // Ignore named and constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000262 (D->hasName() || isa<Constant>(D)) && !isa<Type>(D))) {
Chris Lattner9a297902001-09-07 16:31:52 +0000263 SC_DEBUG("ignored value " << D << endl);
264 return -1; // We do need types unconditionally though
265 }
Chris Lattner00950542001-06-06 20:29:01 +0000266
Chris Lattner9a297902001-09-07 16:31:52 +0000267 // If it's a type, make sure that all subtypes of the type are included...
Chris Lattnercfe26c92001-10-01 18:26:53 +0000268 if (const Type *TheTy = dyn_cast<const Type>(D)) {
Chris Lattnerf1fef652001-10-13 06:35:09 +0000269
270 // Insert the current type before any subtypes. This is important because
271 // recursive types elements are inserted in a bottom up order. Changing
272 // this here can break things. For example:
273 //
274 // global { \2 * } { { \2 }* null }
275 //
276 int ResultSlot;
277 if ((ResultSlot = getValSlot(TheTy)) == -1) {
278 ResultSlot = doInsertVal(TheTy);
279 SC_DEBUG(" Inserted type: " << TheTy->getDescription() << " slot=" <<
280 ResultSlot << endl);
281 }
Chris Lattner9a297902001-09-07 16:31:52 +0000282
283 // Loop over any contained types in the definition... in reverse depth first
284 // order. This assures that all of the leafs of a type are output before
285 // the type itself is. This also assures us that we will not hit infinite
286 // recursion on recursive types...
287 //
Chris Lattner3ff43872001-09-28 22:56:31 +0000288 for (df_iterator<const Type*> I = df_begin(TheTy, true),
289 E = df_end(TheTy); I != E; ++I)
Chris Lattner9a297902001-09-07 16:31:52 +0000290 if (*I != TheTy) {
291 // If we haven't seen this sub type before, add it to our type table!
292 const Type *SubTy = *I;
293 if (getValSlot(SubTy) == -1) {
294 SC_DEBUG(" Inserting subtype: " << SubTy->getDescription() << endl);
Chris Lattnerf1fef652001-10-13 06:35:09 +0000295 int Slot = doInsertVal(SubTy);
296 SC_DEBUG(" Inserted subtype: " << SubTy->getDescription() <<
297 " slot=" << Slot << endl);
Chris Lattner9a297902001-09-07 16:31:52 +0000298 }
299 }
Chris Lattnerf1fef652001-10-13 06:35:09 +0000300 return ResultSlot;
Chris Lattner9a297902001-09-07 16:31:52 +0000301 }
302
303 // Okay, everything is happy, actually insert the silly value now...
304 return doInsertVal(D);
305}
306
307
308// doInsertVal - This is a small helper function to be called only be insertVal.
309//
310int SlotCalculator::doInsertVal(const Value *D) {
Chris Lattner00950542001-06-06 20:29:01 +0000311 const Type *Typ = D->getType();
Chris Lattner9b50c152001-07-26 16:28:37 +0000312 unsigned Ty;
313
314 // Used for debugging DefSlot=-1 assertion...
315 //if (Typ == Type::TypeTy)
Chris Lattnercfe26c92001-10-01 18:26:53 +0000316 // cerr << "Inserting type '" << cast<Type>(D)->getDescription() << "'!\n";
Chris Lattner9b50c152001-07-26 16:28:37 +0000317
Chris Lattner00950542001-06-06 20:29:01 +0000318 if (Typ->isDerivedType()) {
319 int DefSlot = getValSlot(Typ);
320 if (DefSlot == -1) { // Have we already entered this type?
Chris Lattner9a297902001-09-07 16:31:52 +0000321 // Nope, this is the first we have seen the type, process it.
322 DefSlot = insertVal(Typ, true);
323 assert(DefSlot != -1 && "ProcessType returned -1 for a type?");
Chris Lattner00950542001-06-06 20:29:01 +0000324 }
325 Ty = (unsigned)DefSlot;
Chris Lattner9b50c152001-07-26 16:28:37 +0000326 } else {
327 Ty = Typ->getPrimitiveID();
Chris Lattner00950542001-06-06 20:29:01 +0000328 }
329
330 if (Table.size() <= Ty) // Make sure we have the type plane allocated...
331 Table.resize(Ty+1, TypePlane());
332
Chris Lattner9a297902001-09-07 16:31:52 +0000333 // Insert node into table and NodeMap...
334 unsigned DestSlot = NodeMap[D] = Table[Ty].size();
Chris Lattner00950542001-06-06 20:29:01 +0000335 Table[Ty].push_back(D);
Chris Lattner9a297902001-09-07 16:31:52 +0000336
Chris Lattnerf1fef652001-10-13 06:35:09 +0000337 SC_DEBUG(" Inserting value [" << Ty << "] = " << D << " slot=" <<
338 DestSlot << " [");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000339 // G = Global, C = Constant, T = Type, M = Method, o = other
340 SC_DEBUG((isa<GlobalVariable>(D) ? "G" : (isa<Constant>(D) ? "C" :
Chris Lattnerf1fef652001-10-13 06:35:09 +0000341 (isa<Type>(D) ? "T" : (isa<Method>(D) ? "M" : "o")))));
342 SC_DEBUG("]\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000343 return (int)DestSlot;
Chris Lattner00950542001-06-06 20:29:01 +0000344}