#include "llvm/DerivedTypes.h"
#include "llvm/SymbolTable.h"
#include "llvm/Support/STLExtras.h"
-#include "llvm/CFG.h"
+#include "llvm/Support/DepthFirstIterator.h"
#include <algorithm>
#if 0
void SlotCalculator::processModule() {
SC_DEBUG("begin processModule!\n");
+ // 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());
+ }
+
// Add all of the global variables to the value table...
//
for_each(TheModule->gbegin(), TheModule->gend(),
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)
- if (TI->second->isConstant())
+ if (isa<ConstPoolVal>(TI->second))
insertValue(TI->second);
}
for (unsigned i = 0; i < NumModuleTypes; ++i) {
unsigned ModuleSize = ModuleLevel[i]; // Size of plane before method came
TypePlane &CurPlane = Table[i];
- SC_DEBUG("Processing Plane " << i << " of size " << CurPlane.size() <<endl);
+ //SC_DEBUG("Processing Plane " <<i<< " of size " << CurPlane.size() <<endl);
while (CurPlane.size() != ModuleSize) {
- SC_DEBUG(" Removing [" << i << "] Value=" << CurPlane.back() << "\n");
+ //SC_DEBUG(" Removing [" << i << "] Value=" << CurPlane.back() << "\n");
map<const Value *, unsigned>::iterator NI = NodeMap.find(CurPlane.back());
assert(NI != NodeMap.end() && "Node not in nodemap?");
NodeMap.erase(NI); // Erase from nodemap
int SlotCalculator::insertValue(const Value *D) {
- if (D->isConstant() || D->isGlobal()) {
- const User *U = (const User *)D;
+ if (isa<ConstPoolVal>(D) || isa<GlobalVariable>(D)) {
+ const User *U = cast<const User>(D);
// This makes sure that if a constant has uses (for example an array
// of const ints), that they are inserted also. Same for global variable
// initializers.
//
- for_each(U->op_begin(), U->op_end(),
- bind_obj(this, &SlotCalculator::insertValue));
+ for(User::op_const_iterator I = U->op_begin(), E = U->op_end(); I != E; ++I)
+ if (!isa<GlobalValue>(*I)) // Don't chain insert global values
+ insertValue(*I);
}
int SlotNo = getValSlot(D); // Check to see if it's already in!
if (!dontIgnore) // Don't ignore nonignorables!
if (D->getType() == Type::VoidTy || // Ignore void type nodes
(IgnoreNamedNodes && // Ignore named and constants
- (D->hasName() || D->isConstant()) && !D->isType())) {
+ (D->hasName() || isa<ConstPoolVal>(D)) && !isa<Type>(D))) {
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...
- if (const Type *TheTy = D->castType()) {
- SC_DEBUG(" Inserted type: " << TheTy->getDescription() << endl);
+ if (const Type *TheTy = dyn_cast<const Type>(D)) {
+
+ // 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);
+ }
// 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...
//
- for (cfg::tdf_iterator I = cfg::tdf_begin(TheTy, true),
- E = cfg::tdf_end(TheTy); I != E; ++I)
+ for (df_iterator<const Type*> I = df_begin(TheTy, true),
+ E = df_end(TheTy); I != E; ++I)
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);
- doInsertVal(SubTy);
+ int Slot = doInsertVal(SubTy);
+ SC_DEBUG(" Inserted subtype: " << SubTy->getDescription() <<
+ " slot=" << Slot << endl);
}
}
+ return ResultSlot;
}
// Okay, everything is happy, actually insert the silly value now...
// Used for debugging DefSlot=-1 assertion...
//if (Typ == Type::TypeTy)
- // cerr << "Inserting type '" << D->castTypeAsserting()->getDescription() << "'!\n";
+ // cerr << "Inserting type '" << cast<Type>(D)->getDescription() << "'!\n";
if (Typ->isDerivedType()) {
int DefSlot = getValSlot(Typ);
if (Table.size() <= Ty) // Make sure we have the type plane allocated...
Table.resize(Ty+1, TypePlane());
- SC_DEBUG(" Inserting value [" << Ty << "] = " << D << endl);
-
// Insert node into table and NodeMap...
unsigned DestSlot = NodeMap[D] = Table[Ty].size();
Table[Ty].push_back(D);
+ SC_DEBUG(" Inserting value [" << Ty << "] = " << D << " slot=" <<
+ DestSlot << " [");
+ // G = Global, C = ConstPoolVal, T = Type, M = Method, o = other
+ SC_DEBUG((isa<GlobalVariable>(D) ? "G" : (isa<ConstPoolVal>(D) ? "C" :
+ (isa<Type>(D) ? "T" : (isa<Method>(D) ? "M" : "o")))));
+ SC_DEBUG("]\n");
return (int)DestSlot;
}