2001-06-06 20:29:01 +00:00
|
|
|
//===-- SlotCalculator.cpp - Calculate what slots values land in ------------=//
|
|
|
|
//
|
|
|
|
// This file implements a useful analysis step to figure out what numbered
|
|
|
|
// slots values in a program will land in (keeping track of per plane
|
|
|
|
// information as required.
|
|
|
|
//
|
|
|
|
// This is used primarily for when writing a file to disk, either in bytecode
|
|
|
|
// or source format.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/SlotCalculator.h"
|
2001-09-07 16:31:52 +00:00
|
|
|
#include "llvm/Analysis/ConstantsScanner.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include "llvm/Method.h"
|
2001-09-10 07:58:01 +00:00
|
|
|
#include "llvm/GlobalVariable.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/BasicBlock.h"
|
2001-12-03 22:26:30 +00:00
|
|
|
#include "llvm/ConstantVals.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include "llvm/iOther.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
2001-09-07 16:31:52 +00:00
|
|
|
#include "llvm/SymbolTable.h"
|
2001-11-27 00:03:19 +00:00
|
|
|
#include "Support/DepthFirstIterator.h"
|
|
|
|
#include "Support/STLExtras.h"
|
2001-09-07 16:31:52 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
#define SC_DEBUG(X) cerr << X
|
|
|
|
#else
|
|
|
|
#define SC_DEBUG(X)
|
|
|
|
#endif
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
SlotCalculator::SlotCalculator(const Module *M, bool IgnoreNamed) {
|
|
|
|
IgnoreNamedNodes = IgnoreNamed;
|
|
|
|
TheModule = M;
|
|
|
|
|
|
|
|
// Preload table... Make sure that all of the primitive types are in the table
|
|
|
|
// and that their Primitive ID is equal to their slot #
|
|
|
|
//
|
|
|
|
for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) {
|
|
|
|
assert(Type::getPrimitiveType((Type::PrimitiveID)i));
|
2001-07-26 16:28:37 +00:00
|
|
|
insertVal(Type::getPrimitiveType((Type::PrimitiveID)i), true);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (M == 0) return; // Empty table...
|
2001-09-07 16:31:52 +00:00
|
|
|
processModule();
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SlotCalculator::SlotCalculator(const Method *M, bool IgnoreNamed) {
|
|
|
|
IgnoreNamedNodes = IgnoreNamed;
|
|
|
|
TheModule = M ? M->getParent() : 0;
|
|
|
|
|
|
|
|
// Preload table... Make sure that all of the primitive types are in the table
|
|
|
|
// and that their Primitive ID is equal to their slot #
|
|
|
|
//
|
|
|
|
for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) {
|
|
|
|
assert(Type::getPrimitiveType((Type::PrimitiveID)i));
|
2001-07-26 16:28:37 +00:00
|
|
|
insertVal(Type::getPrimitiveType((Type::PrimitiveID)i), true);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (TheModule == 0) return; // Empty table...
|
|
|
|
|
2001-09-07 16:31:52 +00:00
|
|
|
processModule(); // Process module level stuff
|
|
|
|
incorporateMethod(M); // Start out in incorporated state
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// processModule - Process all of the module level method declarations and
|
|
|
|
// types that are available.
|
|
|
|
//
|
|
|
|
void SlotCalculator::processModule() {
|
|
|
|
SC_DEBUG("begin processModule!\n");
|
2001-09-10 07:58:01 +00:00
|
|
|
|
2001-10-13 06:35:09 +00:00
|
|
|
// Add all of the constants that the global variables might refer to first.
|
|
|
|
//
|
|
|
|
for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
|
|
|
|
I != E; ++I) {
|
|
|
|
if ((*I)->hasInitializer())
|
|
|
|
insertValue((*I)->getInitializer());
|
|
|
|
}
|
|
|
|
|
2001-09-10 07:58:01 +00:00
|
|
|
// Add all of the global variables to the value table...
|
|
|
|
//
|
|
|
|
for_each(TheModule->gbegin(), TheModule->gend(),
|
|
|
|
bind_obj(this, &SlotCalculator::insertValue));
|
|
|
|
|
|
|
|
// Scavenge the types out of the methods, then add the methods themselves to
|
|
|
|
// the value table...
|
2001-09-07 16:31:52 +00:00
|
|
|
//
|
|
|
|
for_each(TheModule->begin(), TheModule->end(), // Insert methods...
|
|
|
|
bind_obj(this, &SlotCalculator::insertValue));
|
|
|
|
|
2001-09-10 07:58:01 +00:00
|
|
|
// Insert constants that are named at module level into the slot pool so that
|
|
|
|
// the module symbol table can refer to them...
|
|
|
|
//
|
2001-09-07 16:31:52 +00:00
|
|
|
if (TheModule->hasSymbolTable() && !IgnoreNamedNodes) {
|
|
|
|
SC_DEBUG("Inserting SymbolTable values:\n");
|
|
|
|
processSymbolTable(TheModule->getSymbolTable());
|
|
|
|
}
|
|
|
|
|
|
|
|
SC_DEBUG("end processModule!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// processSymbolTable - Insert all of the values in the specified symbol table
|
|
|
|
// into the values table...
|
|
|
|
//
|
|
|
|
void SlotCalculator::processSymbolTable(const SymbolTable *ST) {
|
|
|
|
for (SymbolTable::const_iterator I = ST->begin(), E = ST->end(); I != E; ++I)
|
|
|
|
for (SymbolTable::type_const_iterator TI = I->second.begin(),
|
|
|
|
TE = I->second.end(); TI != TE; ++TI)
|
|
|
|
insertValue(TI->second);
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-09-07 16:31:52 +00:00
|
|
|
void SlotCalculator::processSymbolTableConstants(const SymbolTable *ST) {
|
|
|
|
for (SymbolTable::const_iterator I = ST->begin(), E = ST->end(); I != E; ++I)
|
|
|
|
for (SymbolTable::type_const_iterator TI = I->second.begin(),
|
|
|
|
TE = I->second.end(); TI != TE; ++TI)
|
2001-12-03 22:26:30 +00:00
|
|
|
if (isa<Constant>(TI->second))
|
2001-09-07 16:31:52 +00:00
|
|
|
insertValue(TI->second);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2001-09-07 16:31:52 +00:00
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
void SlotCalculator::incorporateMethod(const Method *M) {
|
|
|
|
assert(ModuleLevel.size() == 0 && "Module already incorporated!");
|
|
|
|
|
2001-09-07 16:31:52 +00:00
|
|
|
SC_DEBUG("begin processMethod!\n");
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
// Save the Table state before we process the method...
|
2001-09-07 16:31:52 +00:00
|
|
|
for (unsigned i = 0; i < Table.size(); ++i)
|
2001-06-06 20:29:01 +00:00
|
|
|
ModuleLevel.push_back(Table[i].size());
|
2001-09-07 16:31:52 +00:00
|
|
|
|
|
|
|
SC_DEBUG("Inserting method arguments\n");
|
|
|
|
|
|
|
|
// Iterate over method arguments, adding them to the value table...
|
|
|
|
for_each(M->getArgumentList().begin(), M->getArgumentList().end(),
|
|
|
|
bind_obj(this, &SlotCalculator::insertValue));
|
|
|
|
|
|
|
|
// Iterate over all of the instructions in the method, looking for constant
|
|
|
|
// values that are referenced. Add these to the value pools before any
|
|
|
|
// nonconstant values. This will be turned into the constant pool for the
|
|
|
|
// bytecode writer.
|
|
|
|
//
|
|
|
|
if (!IgnoreNamedNodes) { // Assembly writer does not need this!
|
|
|
|
SC_DEBUG("Inserting method constants:\n";
|
|
|
|
for (constant_iterator I = constant_begin(M), E = constant_end(M);
|
|
|
|
I != E; ++I) {
|
|
|
|
cerr << " " << I->getType()->getDescription()
|
|
|
|
<< " " << I->getStrValue() << endl;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Emit all of the constants that are being used by the instructions in the
|
|
|
|
// method...
|
|
|
|
for_each(constant_begin(M), constant_end(M),
|
|
|
|
bind_obj(this, &SlotCalculator::insertValue));
|
|
|
|
|
|
|
|
// If there is a symbol table, it is possible that the user has names for
|
|
|
|
// constants that are not being used. In this case, we will have problems
|
|
|
|
// if we don't emit the constants now, because otherwise we will get
|
|
|
|
// symboltable references to constants not in the output. Scan for these
|
|
|
|
// constants now.
|
|
|
|
//
|
|
|
|
if (M->hasSymbolTable())
|
|
|
|
processSymbolTableConstants(M->getSymbolTable());
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2001-09-07 16:31:52 +00:00
|
|
|
SC_DEBUG("Inserting Labels:\n");
|
|
|
|
|
|
|
|
// Iterate over basic blocks, adding them to the value table...
|
|
|
|
for_each(M->begin(), M->end(),
|
|
|
|
bind_obj(this, &SlotCalculator::insertValue));
|
|
|
|
|
|
|
|
SC_DEBUG("Inserting Instructions:\n");
|
|
|
|
|
|
|
|
// Add all of the instructions to the type planes...
|
|
|
|
for_each(M->inst_begin(), M->inst_end(),
|
|
|
|
bind_obj(this, &SlotCalculator::insertValue));
|
|
|
|
|
|
|
|
if (M->hasSymbolTable() && !IgnoreNamedNodes) {
|
|
|
|
SC_DEBUG("Inserting SymbolTable values:\n");
|
|
|
|
processSymbolTable(M->getSymbolTable());
|
|
|
|
}
|
|
|
|
|
|
|
|
SC_DEBUG("end processMethod!\n");
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SlotCalculator::purgeMethod() {
|
|
|
|
assert(ModuleLevel.size() != 0 && "Module not incorporated!");
|
|
|
|
unsigned NumModuleTypes = ModuleLevel.size();
|
|
|
|
|
2001-09-07 16:31:52 +00:00
|
|
|
SC_DEBUG("begin purgeMethod!\n");
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
// First, remove values from existing type planes
|
|
|
|
for (unsigned i = 0; i < NumModuleTypes; ++i) {
|
|
|
|
unsigned ModuleSize = ModuleLevel[i]; // Size of plane before method came
|
2001-09-07 16:31:52 +00:00
|
|
|
TypePlane &CurPlane = Table[i];
|
2001-10-13 06:35:09 +00:00
|
|
|
//SC_DEBUG("Processing Plane " <<i<< " of size " << CurPlane.size() <<endl);
|
2001-09-07 16:31:52 +00:00
|
|
|
|
|
|
|
while (CurPlane.size() != ModuleSize) {
|
2001-10-13 06:35:09 +00:00
|
|
|
//SC_DEBUG(" Removing [" << i << "] Value=" << CurPlane.back() << "\n");
|
2002-01-20 22:54:45 +00:00
|
|
|
std::map<const Value *, unsigned>::iterator NI =
|
|
|
|
NodeMap.find(CurPlane.back());
|
2001-09-07 16:31:52 +00:00
|
|
|
assert(NI != NodeMap.end() && "Node not in nodemap?");
|
|
|
|
NodeMap.erase(NI); // Erase from nodemap
|
|
|
|
CurPlane.pop_back(); // Shrink plane
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't need this state anymore, free it up.
|
|
|
|
ModuleLevel.clear();
|
|
|
|
|
|
|
|
// Next, remove any type planes defined by the method...
|
|
|
|
while (NumModuleTypes != Table.size()) {
|
|
|
|
TypePlane &Plane = Table.back();
|
2001-09-07 16:31:52 +00:00
|
|
|
SC_DEBUG("Removing Plane " << (Table.size()-1) << " of size "
|
|
|
|
<< Plane.size() << endl);
|
2001-06-06 20:29:01 +00:00
|
|
|
while (Plane.size()) {
|
|
|
|
NodeMap.erase(NodeMap.find(Plane.back())); // Erase from nodemap
|
|
|
|
Plane.pop_back(); // Shrink plane
|
|
|
|
}
|
|
|
|
|
|
|
|
Table.pop_back(); // Nuke the plane, we don't like it.
|
|
|
|
}
|
|
|
|
|
2001-09-07 16:31:52 +00:00
|
|
|
SC_DEBUG("end purgeMethod!\n");
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int SlotCalculator::getValSlot(const Value *D) const {
|
2002-01-20 22:54:45 +00:00
|
|
|
std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(D);
|
2001-06-06 20:29:01 +00:00
|
|
|
if (I == NodeMap.end()) return -1;
|
|
|
|
|
|
|
|
return (int)I->second;
|
|
|
|
}
|
|
|
|
|
2001-09-07 16:31:52 +00:00
|
|
|
|
|
|
|
int SlotCalculator::insertValue(const Value *D) {
|
2001-12-03 22:26:30 +00:00
|
|
|
if (isa<Constant>(D) || isa<GlobalVariable>(D)) {
|
2001-10-13 06:35:09 +00:00
|
|
|
const User *U = cast<const User>(D);
|
2001-09-07 16:31:52 +00:00
|
|
|
// This makes sure that if a constant has uses (for example an array
|
2001-09-18 04:01:05 +00:00
|
|
|
// of const ints), that they are inserted also. Same for global variable
|
|
|
|
// initializers.
|
2001-09-07 16:31:52 +00:00
|
|
|
//
|
2001-12-04 00:03:30 +00:00
|
|
|
for(User::const_op_iterator I = U->op_begin(), E = U->op_end(); I != E; ++I)
|
2001-10-13 06:35:09 +00:00
|
|
|
if (!isa<GlobalValue>(*I)) // Don't chain insert global values
|
|
|
|
insertValue(*I);
|
2001-09-07 16:31:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int SlotNo = getValSlot(D); // Check to see if it's already in!
|
|
|
|
if (SlotNo != -1) return SlotNo;
|
|
|
|
return insertVal(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int SlotCalculator::insertVal(const Value *D, bool dontIgnore = false) {
|
|
|
|
assert(D && "Can't insert a null value!");
|
|
|
|
assert(getValSlot(D) == -1 && "Value is already in the table!");
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
// If this node does not contribute to a plane, or if the node has a
|
2001-09-07 16:31:52 +00:00
|
|
|
// name and we don't want names, then ignore the silly node... Note that types
|
|
|
|
// do need slot numbers so that we can keep track of where other values land.
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
2001-07-26 16:28:37 +00:00
|
|
|
if (!dontIgnore) // Don't ignore nonignorables!
|
|
|
|
if (D->getType() == Type::VoidTy || // Ignore void type nodes
|
2001-09-07 16:31:52 +00:00
|
|
|
(IgnoreNamedNodes && // Ignore named and constants
|
2001-12-03 22:26:30 +00:00
|
|
|
(D->hasName() || isa<Constant>(D)) && !isa<Type>(D))) {
|
2001-09-07 16:31:52 +00:00
|
|
|
SC_DEBUG("ignored value " << D << endl);
|
|
|
|
return -1; // We do need types unconditionally though
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's a type, make sure that all subtypes of the type are included...
|
2001-10-01 18:26:53 +00:00
|
|
|
if (const Type *TheTy = dyn_cast<const Type>(D)) {
|
2001-10-13 06:35:09 +00:00
|
|
|
|
|
|
|
// Insert the current type before any subtypes. This is important because
|
|
|
|
// recursive types elements are inserted in a bottom up order. Changing
|
|
|
|
// this here can break things. For example:
|
|
|
|
//
|
|
|
|
// global { \2 * } { { \2 }* null }
|
|
|
|
//
|
|
|
|
int ResultSlot;
|
|
|
|
if ((ResultSlot = getValSlot(TheTy)) == -1) {
|
|
|
|
ResultSlot = doInsertVal(TheTy);
|
|
|
|
SC_DEBUG(" Inserted type: " << TheTy->getDescription() << " slot=" <<
|
|
|
|
ResultSlot << endl);
|
|
|
|
}
|
2001-09-07 16:31:52 +00:00
|
|
|
|
|
|
|
// Loop over any contained types in the definition... in reverse depth first
|
|
|
|
// order. This assures that all of the leafs of a type are output before
|
|
|
|
// the type itself is. This also assures us that we will not hit infinite
|
|
|
|
// recursion on recursive types...
|
|
|
|
//
|
2001-09-28 22:56:31 +00:00
|
|
|
for (df_iterator<const Type*> I = df_begin(TheTy, true),
|
|
|
|
E = df_end(TheTy); I != E; ++I)
|
2001-09-07 16:31:52 +00:00
|
|
|
if (*I != TheTy) {
|
|
|
|
// If we haven't seen this sub type before, add it to our type table!
|
|
|
|
const Type *SubTy = *I;
|
|
|
|
if (getValSlot(SubTy) == -1) {
|
|
|
|
SC_DEBUG(" Inserting subtype: " << SubTy->getDescription() << endl);
|
2001-10-13 06:35:09 +00:00
|
|
|
int Slot = doInsertVal(SubTy);
|
|
|
|
SC_DEBUG(" Inserted subtype: " << SubTy->getDescription() <<
|
|
|
|
" slot=" << Slot << endl);
|
2001-09-07 16:31:52 +00:00
|
|
|
}
|
|
|
|
}
|
2001-10-13 06:35:09 +00:00
|
|
|
return ResultSlot;
|
2001-09-07 16:31:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Okay, everything is happy, actually insert the silly value now...
|
|
|
|
return doInsertVal(D);
|
|
|
|
}
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-09-07 16:31:52 +00:00
|
|
|
// doInsertVal - This is a small helper function to be called only be insertVal.
|
|
|
|
//
|
|
|
|
int SlotCalculator::doInsertVal(const Value *D) {
|
2001-06-06 20:29:01 +00:00
|
|
|
const Type *Typ = D->getType();
|
2001-07-26 16:28:37 +00:00
|
|
|
unsigned Ty;
|
|
|
|
|
|
|
|
// Used for debugging DefSlot=-1 assertion...
|
|
|
|
//if (Typ == Type::TypeTy)
|
2001-10-01 18:26:53 +00:00
|
|
|
// cerr << "Inserting type '" << cast<Type>(D)->getDescription() << "'!\n";
|
2001-07-26 16:28:37 +00:00
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
if (Typ->isDerivedType()) {
|
|
|
|
int DefSlot = getValSlot(Typ);
|
|
|
|
if (DefSlot == -1) { // Have we already entered this type?
|
2001-09-07 16:31:52 +00:00
|
|
|
// Nope, this is the first we have seen the type, process it.
|
|
|
|
DefSlot = insertVal(Typ, true);
|
|
|
|
assert(DefSlot != -1 && "ProcessType returned -1 for a type?");
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
Ty = (unsigned)DefSlot;
|
2001-07-26 16:28:37 +00:00
|
|
|
} else {
|
|
|
|
Ty = Typ->getPrimitiveID();
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Table.size() <= Ty) // Make sure we have the type plane allocated...
|
|
|
|
Table.resize(Ty+1, TypePlane());
|
|
|
|
|
2001-09-07 16:31:52 +00:00
|
|
|
// Insert node into table and NodeMap...
|
|
|
|
unsigned DestSlot = NodeMap[D] = Table[Ty].size();
|
2001-06-06 20:29:01 +00:00
|
|
|
Table[Ty].push_back(D);
|
2001-09-07 16:31:52 +00:00
|
|
|
|
2001-10-13 06:35:09 +00:00
|
|
|
SC_DEBUG(" Inserting value [" << Ty << "] = " << D << " slot=" <<
|
|
|
|
DestSlot << " [");
|
2001-12-03 22:26:30 +00:00
|
|
|
// G = Global, C = Constant, T = Type, M = Method, o = other
|
|
|
|
SC_DEBUG((isa<GlobalVariable>(D) ? "G" : (isa<Constant>(D) ? "C" :
|
2001-10-13 06:35:09 +00:00
|
|
|
(isa<Type>(D) ? "T" : (isa<Method>(D) ? "M" : "o")))));
|
|
|
|
SC_DEBUG("]\n");
|
2001-09-07 16:31:52 +00:00
|
|
|
return (int)DestSlot;
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|