1 //===-- SlotCalculator.cpp - Calculate what slots values land in ----------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
14 // This is used primarily for when writing a file to disk, either in bytecode
17 //===----------------------------------------------------------------------===//
19 #include "llvm/SlotCalculator.h"
20 #include "llvm/Analysis/ConstantsScanner.h"
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/iOther.h"
24 #include "llvm/Module.h"
25 #include "llvm/SymbolTable.h"
26 #include "Support/PostOrderIterator.h"
27 #include "Support/STLExtras.h"
32 #define SC_DEBUG(X) std::cerr << X
37 SlotCalculator::SlotCalculator(const Module *M, bool buildBytecodeInfo) {
38 BuildBytecodeInfo = buildBytecodeInfo;
39 ModuleContainsAllFunctionConstants = false;
42 // Preload table... Make sure that all of the primitive types are in the table
43 // and that their Primitive ID is equal to their slot #
45 SC_DEBUG("Inserting primitive types:\n");
46 for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) {
47 assert(Type::getPrimitiveType((Type::PrimitiveID)i));
48 insertValue(Type::getPrimitiveType((Type::PrimitiveID)i), true);
51 if (M == 0) return; // Empty table...
55 SlotCalculator::SlotCalculator(const Function *M, bool buildBytecodeInfo) {
56 BuildBytecodeInfo = buildBytecodeInfo;
57 ModuleContainsAllFunctionConstants = false;
58 TheModule = M ? M->getParent() : 0;
60 // Preload table... Make sure that all of the primitive types are in the table
61 // and that their Primitive ID is equal to their slot #
63 SC_DEBUG("Inserting primitive types:\n");
64 for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) {
65 assert(Type::getPrimitiveType((Type::PrimitiveID)i));
66 insertValue(Type::getPrimitiveType((Type::PrimitiveID)i), true);
69 if (TheModule == 0) return; // Empty table...
71 processModule(); // Process module level stuff
72 incorporateFunction(M); // Start out in incorporated state
75 unsigned SlotCalculator::getGlobalSlot(const Value *V) const {
76 assert(!CompactionTable.empty() &&
77 "This method can only be used when compaction is enabled!");
78 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V))
80 std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(V);
81 assert(I != NodeMap.end() && "Didn't find entry!");
87 // processModule - Process all of the module level function declarations and
88 // types that are available.
90 void SlotCalculator::processModule() {
91 SC_DEBUG("begin processModule!\n");
93 // Add all of the global variables to the value table...
95 for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
99 // Scavenge the types out of the functions, then add the functions themselves
100 // to the value table...
102 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
106 // Add all of the module level constants used as initializers
108 for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
110 if (I->hasInitializer())
111 getOrCreateSlot(I->getInitializer());
113 // Now that all global constants have been added, rearrange constant planes
114 // that contain constant strings so that the strings occur at the start of the
115 // plane, not somewhere in the middle.
117 if (BuildBytecodeInfo) {
118 TypePlane &Types = Table[Type::TypeTyID];
119 for (unsigned plane = 0, e = Table.size(); plane != e; ++plane) {
120 if (const ArrayType *AT = dyn_cast<ArrayType>(Types[plane]))
121 if (AT->getElementType() == Type::SByteTy ||
122 AT->getElementType() == Type::UByteTy) {
123 TypePlane &Plane = Table[plane];
124 unsigned FirstNonStringID = 0;
125 for (unsigned i = 0, e = Plane.size(); i != e; ++i)
126 if (cast<ConstantArray>(Plane[i])->isString()) {
127 // Check to see if we have to shuffle this string around. If not,
128 // don't do anything.
129 if (i != FirstNonStringID) {
130 // Swap the plane entries....
131 std::swap(Plane[i], Plane[FirstNonStringID]);
133 // Keep the NodeMap up to date.
134 NodeMap[Plane[i]] = i;
135 NodeMap[Plane[FirstNonStringID]] = FirstNonStringID;
143 // If we are emitting a bytecode file, scan all of the functions for their
144 // constants, which allows us to emit more compact modules. This is optional,
145 // and is just used to compactify the constants used by different functions
148 // This functionality is completely optional for the bytecode writer, but
149 // tends to produce smaller bytecode files. This should not be used in the
150 // future by clients that want to, for example, build and emit functions on
151 // the fly. For now, however, it is unconditionally enabled when building
152 // bytecode information.
154 if (BuildBytecodeInfo) {
155 ModuleContainsAllFunctionConstants = true;
157 SC_DEBUG("Inserting function constants:\n");
158 for (Module::const_iterator F = TheModule->begin(), E = TheModule->end();
160 for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I){
161 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
162 if (isa<Constant>(I->getOperand(op)))
163 getOrCreateSlot(I->getOperand(op));
164 getOrCreateSlot(I->getType());
165 if (const VANextInst *VAN = dyn_cast<VANextInst>(*I))
166 getOrCreateSlot(VAN->getArgType());
168 processSymbolTableConstants(&F->getSymbolTable());
174 // Insert constants that are named at module level into the slot pool so that
175 // the module symbol table can refer to them...
177 if (BuildBytecodeInfo) {
178 SC_DEBUG("Inserting SymbolTable values:\n");
179 processSymbolTable(&TheModule->getSymbolTable());
182 // Now that we have collected together all of the information relevant to the
183 // module, compactify the type table if it is particularly big and outputting
184 // a bytecode file. The basic problem we run into is that some programs have
185 // a large number of types, which causes the type field to overflow its size,
186 // which causes instructions to explode in size (particularly call
187 // instructions). To avoid this behavior, we "sort" the type table so that
188 // all non-value types are pushed to the end of the type table, giving nice
189 // low numbers to the types that can be used by instructions, thus reducing
190 // the amount of explodage we suffer.
191 if (BuildBytecodeInfo && Table[Type::TypeTyID].size() >= 64) {
192 // Scan through the type table moving value types to the start of the table.
193 TypePlane *Types = &Table[Type::TypeTyID];
194 unsigned FirstNonValueTypeID = 0;
195 for (unsigned i = 0, e = Types->size(); i != e; ++i)
196 if (cast<Type>((*Types)[i])->isFirstClassType() ||
197 cast<Type>((*Types)[i])->isPrimitiveType()) {
198 // Check to see if we have to shuffle this type around. If not, don't
200 if (i != FirstNonValueTypeID) {
201 assert(i != Type::TypeTyID && FirstNonValueTypeID != Type::TypeTyID &&
202 "Cannot move around the type plane!");
204 // Swap the type ID's.
205 std::swap((*Types)[i], (*Types)[FirstNonValueTypeID]);
207 // Keep the NodeMap up to date.
208 NodeMap[(*Types)[i]] = i;
209 NodeMap[(*Types)[FirstNonValueTypeID]] = FirstNonValueTypeID;
211 // When we move a type, make sure to move its value plane as needed.
212 if (Table.size() > FirstNonValueTypeID) {
213 if (Table.size() <= i) Table.resize(i+1);
214 std::swap(Table[i], Table[FirstNonValueTypeID]);
215 Types = &Table[Type::TypeTyID];
218 ++FirstNonValueTypeID;
222 SC_DEBUG("end processModule!\n");
225 // processSymbolTable - Insert all of the values in the specified symbol table
226 // into the values table...
228 void SlotCalculator::processSymbolTable(const SymbolTable *ST) {
229 for (SymbolTable::const_iterator I = ST->begin(), E = ST->end(); I != E; ++I)
230 for (SymbolTable::type_const_iterator TI = I->second.begin(),
231 TE = I->second.end(); TI != TE; ++TI)
232 getOrCreateSlot(TI->second);
235 void SlotCalculator::processSymbolTableConstants(const SymbolTable *ST) {
236 for (SymbolTable::const_iterator I = ST->begin(), E = ST->end(); I != E; ++I)
237 for (SymbolTable::type_const_iterator TI = I->second.begin(),
238 TE = I->second.end(); TI != TE; ++TI)
239 if (isa<Constant>(TI->second) || isa<Type>(TI->second))
240 getOrCreateSlot(TI->second);
244 void SlotCalculator::incorporateFunction(const Function *F) {
245 assert(ModuleLevel.size() == 0 && "Module already incorporated!");
247 SC_DEBUG("begin processFunction!\n");
249 // If we emitted all of the function constants, build a compaction table.
250 if (BuildBytecodeInfo && ModuleContainsAllFunctionConstants)
251 buildCompactionTable(F);
253 // Save the Table state before we process the function...
254 for (unsigned i = 0, e = Table.size(); i != e; ++i)
255 ModuleLevel.push_back(Table[i].size());
258 // Iterate over function arguments, adding them to the value table...
259 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
262 if (BuildBytecodeInfo && // Assembly writer does not need this!
263 !ModuleContainsAllFunctionConstants) {
264 // Iterate over all of the instructions in the function, looking for
265 // constant values that are referenced. Add these to the value pools
266 // before any nonconstant values. This will be turned into the constant
267 // pool for the bytecode writer.
270 // Emit all of the constants that are being used by the instructions in
272 for_each(constant_begin(F), constant_end(F),
273 bind_obj(this, &SlotCalculator::getOrCreateSlot));
275 // If there is a symbol table, it is possible that the user has names for
276 // constants that are not being used. In this case, we will have problems
277 // if we don't emit the constants now, because otherwise we will get
278 // symbol table references to constants not in the output. Scan for these
281 processSymbolTableConstants(&F->getSymbolTable());
284 SC_DEBUG("Inserting Instructions:\n");
286 // Add all of the instructions to the type planes...
287 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
289 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
291 if (const VANextInst *VAN = dyn_cast<VANextInst>(I))
292 getOrCreateSlot(VAN->getArgType());
296 SC_DEBUG("end processFunction!\n");
299 void SlotCalculator::purgeFunction() {
300 assert(ModuleLevel.size() != 0 && "Module not incorporated!");
301 unsigned NumModuleTypes = ModuleLevel.size();
303 SC_DEBUG("begin purgeFunction!\n");
305 // First, free the compaction map if used.
306 CompactionNodeMap.clear();
308 // Next, remove values from existing type planes
309 for (unsigned i = 0; i != NumModuleTypes; ++i)
310 if (i >= CompactionTable.size() || CompactionTable[i].empty()) {
311 unsigned ModuleSize = ModuleLevel[i];// Size of plane before function came
312 TypePlane &CurPlane = Table[i];
314 while (CurPlane.size() != ModuleSize) {
315 std::map<const Value *, unsigned>::iterator NI =
316 NodeMap.find(CurPlane.back());
317 assert(NI != NodeMap.end() && "Node not in nodemap?");
318 NodeMap.erase(NI); // Erase from nodemap
319 CurPlane.pop_back(); // Shrink plane
323 // We don't need this state anymore, free it up.
326 if (!CompactionTable.empty()) {
327 CompactionTable.clear();
329 // FIXME: this will require adjustment when we don't compact everything.
331 // Finally, remove any type planes defined by the function...
332 while (NumModuleTypes != Table.size()) {
333 TypePlane &Plane = Table.back();
334 SC_DEBUG("Removing Plane " << (Table.size()-1) << " of size "
335 << Plane.size() << "\n");
336 while (Plane.size()) {
337 NodeMap.erase(NodeMap.find(Plane.back())); // Erase from nodemap
338 Plane.pop_back(); // Shrink plane
341 Table.pop_back(); // Nuke the plane, we don't like it.
344 SC_DEBUG("end purgeFunction!\n");
347 static inline bool hasNullValue(unsigned TyID) {
348 return TyID != Type::LabelTyID && TyID != Type::TypeTyID &&
349 TyID != Type::VoidTyID;
352 /// getOrCreateCompactionTableSlot - This method is used to build up the initial
353 /// approximation of the compaction table.
354 unsigned SlotCalculator::getOrCreateCompactionTableSlot(const Value *V) {
355 std::map<const Value*, unsigned>::iterator I =
356 CompactionNodeMap.lower_bound(V);
357 if (I != CompactionNodeMap.end() && I->first == V)
358 return I->second; // Already exists?
360 // Make sure the type is in the table.
361 unsigned Ty = getOrCreateCompactionTableSlot(V->getType());
362 if (CompactionTable.size() <= Ty)
363 CompactionTable.resize(Ty+1);
365 assert(!isa<Type>(V) || ModuleLevel.empty());
367 TypePlane &TyPlane = CompactionTable[Ty];
369 // Make sure to insert the null entry if the thing we are inserting is not a
371 if (TyPlane.empty() && hasNullValue(V->getType()->getPrimitiveID())) {
372 Value *ZeroInitializer = Constant::getNullValue(V->getType());
373 if (V != ZeroInitializer) {
374 TyPlane.push_back(ZeroInitializer);
375 CompactionNodeMap[ZeroInitializer] = 0;
379 unsigned SlotNo = TyPlane.size();
380 TyPlane.push_back(V);
381 CompactionNodeMap.insert(std::make_pair(V, SlotNo));
386 /// buildCompactionTable - Since all of the function constants and types are
387 /// stored in the module-level constant table, we don't need to emit a function
388 /// constant table. Also due to this, the indices for various constants and
389 /// types might be very large in large programs. In order to avoid blowing up
390 /// the size of instructions in the bytecode encoding, we build a compaction
391 /// table, which defines a mapping from function-local identifiers to global
393 void SlotCalculator::buildCompactionTable(const Function *F) {
394 assert(CompactionNodeMap.empty() && "Compaction table already built!");
395 // First step, insert the primitive types.
396 CompactionTable.resize(Type::TypeTyID+1);
397 for (unsigned i = 0; i != Type::FirstDerivedTyID; ++i) {
398 const Type *PrimTy = Type::getPrimitiveType((Type::PrimitiveID)i);
399 CompactionTable[Type::TypeTyID].push_back(PrimTy);
400 CompactionNodeMap[PrimTy] = i;
403 // Next, include any types used by function arguments.
404 for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
405 getOrCreateCompactionTableSlot(I->getType());
407 // Next, find all of the types and values that are referred to by the
408 // instructions in the program.
409 for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
410 getOrCreateCompactionTableSlot(I->getType());
411 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
412 if (isa<Constant>(I->getOperand(op)) ||
413 isa<GlobalValue>(I->getOperand(op)))
414 getOrCreateCompactionTableSlot(I->getOperand(op));
415 if (const VANextInst *VAN = dyn_cast<VANextInst>(*I))
416 getOrCreateCompactionTableSlot(VAN->getArgType());
419 const SymbolTable &ST = F->getSymbolTable();
420 for (SymbolTable::const_iterator I = ST.begin(), E = ST.end(); I != E; ++I)
421 for (SymbolTable::type_const_iterator TI = I->second.begin(),
422 TE = I->second.end(); TI != TE; ++TI)
423 if (isa<Constant>(TI->second) || isa<Type>(TI->second) ||
424 isa<GlobalValue>(TI->second))
425 getOrCreateCompactionTableSlot(TI->second);
427 // Now that we have all of the values in the table, and know what types are
428 // referenced, make sure that there is at least the zero initializer in any
429 // used type plane. Since the type was used, we will be emitting instructions
430 // to the plane even if there are no constants in it.
431 CompactionTable.resize(CompactionTable[Type::TypeTyID].size());
432 for (unsigned i = 0, e = CompactionTable.size(); i != e; ++i)
433 if (CompactionTable[i].empty() && i != Type::VoidTyID &&
434 i != Type::LabelTyID) {
435 const Type *Ty = cast<Type>(CompactionTable[Type::TypeTyID][i]);
436 getOrCreateCompactionTableSlot(Constant::getNullValue(Ty));
439 // Okay, now at this point, we have a legal compaction table. Since we want
440 // to emit the smallest possible binaries, we delete planes that do not NEED
441 // to be compacted, starting with the type plane.
444 // If decided not to compact anything, do not modify ModuleLevels.
445 if (CompactionTable.empty())
446 // FIXME: must update ModuleLevel.
449 // Finally, for any planes that we have decided to compact, update the
450 // ModuleLevel entries to be accurate.
452 // FIXME: This does not yet work for partially compacted tables.
453 ModuleLevel.resize(CompactionTable.size());
454 for (unsigned i = 0, e = CompactionTable.size(); i != e; ++i)
455 ModuleLevel[i] = CompactionTable[i].size();
458 int SlotCalculator::getSlot(const Value *V) const {
459 // If there is a CompactionTable active...
460 if (!CompactionNodeMap.empty()) {
461 std::map<const Value*, unsigned>::const_iterator I =
462 CompactionNodeMap.find(V);
463 if (I != CompactionNodeMap.end())
464 return (int)I->second;
468 std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(V);
469 if (I != NodeMap.end())
470 return (int)I->second;
472 // Do not number ConstantPointerRef's at all. They are an abomination.
473 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V))
474 return getSlot(CPR->getValue());
480 int SlotCalculator::getOrCreateSlot(const Value *V) {
481 int SlotNo = getSlot(V); // Check to see if it's already in!
482 if (SlotNo != -1) return SlotNo;
484 // Do not number ConstantPointerRef's at all. They are an abomination.
485 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V))
486 return getOrCreateSlot(CPR->getValue());
488 if (!isa<GlobalValue>(V)) // Initializers for globals are handled explicitly
489 if (const Constant *C = dyn_cast<Constant>(V)) {
490 assert(CompactionNodeMap.empty() &&
491 "All needed constants should be in the compaction map already!");
493 // If we are emitting a bytecode file, do not index the characters that
494 // make up constant strings. We emit constant strings as special
495 // entities that don't require their individual characters to be emitted.
496 if (!BuildBytecodeInfo || !isa<ConstantArray>(C) ||
497 !cast<ConstantArray>(C)->isString()) {
498 // This makes sure that if a constant has uses (for example an array of
499 // const ints), that they are inserted also.
501 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
505 assert(ModuleLevel.empty() &&
506 "How can a constant string be directly accessed in a function?");
507 // Otherwise, if we are emitting a bytecode file and this IS a string,
509 if (!C->isNullValue())
510 ConstantStrings.push_back(cast<ConstantArray>(C));
514 return insertValue(V);
518 int SlotCalculator::insertValue(const Value *D, bool dontIgnore) {
519 assert(D && "Can't insert a null value!");
520 assert(getSlot(D) == -1 && "Value is already in the table!");
522 // If we are building a compaction map, and if this plane is being compacted,
523 // insert the value into the compaction map, not into the global map.
524 if (!CompactionNodeMap.empty()) {
525 if (D->getType() == Type::VoidTy) return -1; // Do not insert void values
526 assert(!isa<Type>(D) && !isa<Constant>(D) && !isa<GlobalValue>(D) &&
527 "Types, constants, and globals should be in global SymTab!");
529 // FIXME: this does not yet handle partially compacted tables yet!
530 return getOrCreateCompactionTableSlot(D);
533 // If this node does not contribute to a plane, or if the node has a
534 // name and we don't want names, then ignore the silly node... Note that types
535 // do need slot numbers so that we can keep track of where other values land.
537 if (!dontIgnore) // Don't ignore nonignorables!
538 if (D->getType() == Type::VoidTy || // Ignore void type nodes
539 (!BuildBytecodeInfo && // Ignore named and constants
540 (D->hasName() || isa<Constant>(D)) && !isa<Type>(D))) {
541 SC_DEBUG("ignored value " << *D << "\n");
542 return -1; // We do need types unconditionally though
545 // If it's a type, make sure that all subtypes of the type are included...
546 if (const Type *TheTy = dyn_cast<Type>(D)) {
548 // Insert the current type before any subtypes. This is important because
549 // recursive types elements are inserted in a bottom up order. Changing
550 // this here can break things. For example:
552 // global { \2 * } { { \2 }* null }
554 int ResultSlot = doInsertValue(TheTy);
555 SC_DEBUG(" Inserted type: " << TheTy->getDescription() << " slot=" <<
558 // Loop over any contained types in the definition... in post
561 for (po_iterator<const Type*> I = po_begin(TheTy), E = po_end(TheTy);
564 const Type *SubTy = *I;
565 // If we haven't seen this sub type before, add it to our type table!
566 if (getSlot(SubTy) == -1) {
567 SC_DEBUG(" Inserting subtype: " << SubTy->getDescription() << "\n");
568 int Slot = doInsertValue(SubTy);
569 SC_DEBUG(" Inserted subtype: " << SubTy->getDescription() <<
570 " slot=" << Slot << "\n");
577 // Okay, everything is happy, actually insert the silly value now...
578 return doInsertValue(D);
581 // doInsertValue - This is a small helper function to be called only
584 int SlotCalculator::doInsertValue(const Value *D) {
585 const Type *Typ = D->getType();
588 // Used for debugging DefSlot=-1 assertion...
589 //if (Typ == Type::TypeTy)
590 // cerr << "Inserting type '" << cast<Type>(D)->getDescription() << "'!\n";
592 if (Typ->isDerivedType()) {
593 int ValSlot = getSlot(Typ);
594 if (ValSlot == -1) { // Have we already entered this type?
595 // Nope, this is the first we have seen the type, process it.
596 ValSlot = insertValue(Typ, true);
597 assert(ValSlot != -1 && "ProcessType returned -1 for a type?");
599 Ty = (unsigned)ValSlot;
601 Ty = Typ->getPrimitiveID();
604 if (Table.size() <= Ty) // Make sure we have the type plane allocated...
605 Table.resize(Ty+1, TypePlane());
607 // If this is the first value to get inserted into the type plane, make sure
608 // to insert the implicit null value...
609 if (Table[Ty].empty() && BuildBytecodeInfo && hasNullValue(Ty)) {
610 Value *ZeroInitializer = Constant::getNullValue(Typ);
612 // If we are pushing zeroinit, it will be handled below.
613 if (D != ZeroInitializer) {
614 Table[Ty].push_back(ZeroInitializer);
615 NodeMap[ZeroInitializer] = 0;
619 // Insert node into table and NodeMap...
620 unsigned DestSlot = NodeMap[D] = Table[Ty].size();
621 Table[Ty].push_back(D);
623 SC_DEBUG(" Inserting value [" << Ty << "] = " << D << " slot=" <<
625 // G = Global, C = Constant, T = Type, F = Function, o = other
626 SC_DEBUG((isa<GlobalVariable>(D) ? "G" : (isa<Constant>(D) ? "C" :
627 (isa<Type>(D) ? "T" : (isa<Function>(D) ? "F" : "o")))));
629 return (int)DestSlot;