#include "llvm/Analysis/SlotCalculator.h"
#include "llvm/Analysis/ConstantsScanner.h"
#include "llvm/Method.h"
+#include "llvm/GlobalVariable.h"
#include "llvm/Module.h"
#include "llvm/BasicBlock.h"
-#include "llvm/ConstPoolVals.h"
+#include "llvm/ConstantVals.h"
#include "llvm/iOther.h"
#include "llvm/DerivedTypes.h"
#include "llvm/SymbolTable.h"
-#include "llvm/Support/STLExtras.h"
-#include "llvm/CFG.h"
+#include "Support/DepthFirstIterator.h"
+#include "Support/STLExtras.h"
#include <algorithm>
#if 0
//
void SlotCalculator::processModule() {
SC_DEBUG("begin processModule!\n");
- // Currently, the only module level declarations are methods and method
- // prototypes. We simply scavenge the types out of the methods, then add the
- // methods themselves to the value table...
+
+ // 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(),
+ bind_obj(this, &SlotCalculator::insertValue));
+
+ // Scavenge the types out of the methods, then add the methods themselves to
+ // the value table...
//
for_each(TheModule->begin(), TheModule->end(), // Insert methods...
bind_obj(this, &SlotCalculator::insertValue));
+ // Insert constants that are named at module level into the slot pool so that
+ // the module symbol table can refer to them...
+ //
if (TheModule->hasSymbolTable() && !IgnoreNamedNodes) {
SC_DEBUG("Inserting SymbolTable values:\n");
processSymbolTable(TheModule->getSymbolTable());
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<Constant>(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");
- map<const Value *, unsigned>::iterator NI = NodeMap.find(CurPlane.back());
+ //SC_DEBUG(" Removing [" << i << "] Value=" << CurPlane.back() << "\n");
+ std::map<const Value *, unsigned>::iterator NI =
+ NodeMap.find(CurPlane.back());
assert(NI != NodeMap.end() && "Node not in nodemap?");
NodeMap.erase(NI); // Erase from nodemap
CurPlane.pop_back(); // Shrink plane
}
int SlotCalculator::getValSlot(const Value *D) const {
- map<const Value*, unsigned>::const_iterator I = NodeMap.find(D);
+ std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(D);
if (I == NodeMap.end()) return -1;
return (int)I->second;
int SlotCalculator::insertValue(const Value *D) {
- if (const ConstPoolVal *CPV = D->castConstant()) {
+ if (isa<Constant>(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.
+ // of const ints), that they are inserted also. Same for global variable
+ // initializers.
//
- for_each(CPV->op_begin(), CPV->op_end(),
- bind_obj(this, &SlotCalculator::insertValue));
+ for(User::const_op_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<Constant>(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 = Constant, T = Type, M = Method, o = other
+ SC_DEBUG((isa<GlobalVariable>(D) ? "G" : (isa<Constant>(D) ? "C" :
+ (isa<Type>(D) ? "T" : (isa<Method>(D) ? "M" : "o")))));
+ SC_DEBUG("]\n");
return (int)DestSlot;
}