* Change the order that globals and constants are processed in

* Add support for implicit zero initializers


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@5750 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2003-03-19 20:57:22 +00:00
parent 186a1f71e6
commit 3413d150dc
2 changed files with 90 additions and 72 deletions

View File

@ -21,7 +21,7 @@
#include <algorithm> #include <algorithm>
#if 0 #if 0
#define SC_DEBUG(X) cerr << X #define SC_DEBUG(X) std::cerr << X
#else #else
#define SC_DEBUG(X) #define SC_DEBUG(X)
#endif #endif
@ -67,26 +67,26 @@ SlotCalculator::SlotCalculator(const Function *M, bool IgnoreNamed) {
void SlotCalculator::processModule() { void SlotCalculator::processModule() {
SC_DEBUG("begin processModule!\n"); SC_DEBUG("begin processModule!\n");
// Add all of the constants that the global variables might refer to first. // Add all of the global variables to the value table...
// //
for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend(); for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
I != E; ++I) I != E; ++I)
if (I->hasInitializer())
insertValue(I->getInitializer());
// Add all of the global variables to the value table...
//
for(Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
I != E; ++I)
insertValue(I); insertValue(I);
// Scavenge the types out of the functions, then add the functions themselves // Scavenge the types out of the functions, then add the functions themselves
// to the value table... // to the value table...
// //
for(Module::const_iterator I = TheModule->begin(), E = TheModule->end(); for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
I != E; ++I) I != E; ++I)
insertValue(I); insertValue(I);
// Add all of the module level constants used as initializers
//
for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
I != E; ++I)
if (I->hasInitializer())
insertValue(I->getInitializer());
// Insert constants that are named at module level into the slot pool so that // Insert constants that are named at module level into the slot pool so that
// the module symbol table can refer to them... // the module symbol table can refer to them...
// //
@ -141,8 +141,7 @@ void SlotCalculator::incorporateFunction(const Function *M) {
SC_DEBUG("Inserting function constants:\n"; SC_DEBUG("Inserting function constants:\n";
for (constant_iterator I = constant_begin(M), E = constant_end(M); for (constant_iterator I = constant_begin(M), E = constant_end(M);
I != E; ++I) { I != E; ++I) {
cerr << " " << *I->getType() std::cerr << " " << *I->getType() << " " << *I << "\n";
<< " " << *I << "\n";
}); });
// Emit all of the constants that are being used by the instructions in the // Emit all of the constants that are being used by the instructions in the
@ -164,8 +163,6 @@ void SlotCalculator::incorporateFunction(const Function *M) {
// Iterate over basic blocks, adding them to the value table... // Iterate over basic blocks, adding them to the value table...
for (Function::const_iterator I = M->begin(), E = M->end(); I != E; ++I) for (Function::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
insertValue(I); insertValue(I);
/* for_each(M->begin(), M->end(),
bind_obj(this, &SlotCalculator::insertValue));*/
SC_DEBUG("Inserting Instructions:\n"); SC_DEBUG("Inserting Instructions:\n");
@ -230,21 +227,21 @@ int SlotCalculator::getValSlot(const Value *D) const {
} }
int SlotCalculator::insertValue(const Value *D) { int SlotCalculator::insertValue(const Value *V) {
if (isa<Constant>(D) || isa<GlobalVariable>(D)) { int SlotNo = getValSlot(V); // Check to see if it's already in!
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(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 (SlotNo != -1) return SlotNo; if (SlotNo != -1) return SlotNo;
return insertVal(D);
if (!isa<GlobalValue>(V))
if (const Constant *C = dyn_cast<Constant>(V)) {
// This makes sure that if a constant has uses (for example an array of
// const ints), that they are inserted also.
//
for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
I != E; ++I)
insertValue(*I);
}
return insertVal(V);
} }
@ -260,7 +257,7 @@ int SlotCalculator::insertVal(const Value *D, bool dontIgnore) {
if (D->getType() == Type::VoidTy || // Ignore void type nodes if (D->getType() == Type::VoidTy || // Ignore void type nodes
(IgnoreNamedNodes && // Ignore named and constants (IgnoreNamedNodes && // Ignore named and constants
(D->hasName() || isa<Constant>(D)) && !isa<Type>(D))) { (D->hasName() || isa<Constant>(D)) && !isa<Type>(D))) {
SC_DEBUG("ignored value " << D << "\n"); SC_DEBUG("ignored value " << *D << "\n");
return -1; // We do need types unconditionally though return -1; // We do need types unconditionally though
} }
@ -316,13 +313,13 @@ int SlotCalculator::doInsertVal(const Value *D) {
// cerr << "Inserting type '" << cast<Type>(D)->getDescription() << "'!\n"; // cerr << "Inserting type '" << cast<Type>(D)->getDescription() << "'!\n";
if (Typ->isDerivedType()) { if (Typ->isDerivedType()) {
int DefSlot = getValSlot(Typ); int ValSlot = getValSlot(Typ);
if (DefSlot == -1) { // Have we already entered this type? if (ValSlot == -1) { // Have we already entered this type?
// Nope, this is the first we have seen the type, process it. // Nope, this is the first we have seen the type, process it.
DefSlot = insertVal(Typ, true); ValSlot = insertVal(Typ, true);
assert(DefSlot != -1 && "ProcessType returned -1 for a type?"); assert(ValSlot != -1 && "ProcessType returned -1 for a type?");
} }
Ty = (unsigned)DefSlot; Ty = (unsigned)ValSlot;
} else { } else {
Ty = Typ->getPrimitiveID(); Ty = Typ->getPrimitiveID();
} }
@ -330,6 +327,18 @@ int SlotCalculator::doInsertVal(const Value *D) {
if (Table.size() <= Ty) // Make sure we have the type plane allocated... if (Table.size() <= Ty) // Make sure we have the type plane allocated...
Table.resize(Ty+1, TypePlane()); Table.resize(Ty+1, TypePlane());
// If this is the first value to get inserted into the type plane, make sure
// to insert the implicit null value...
if (Table[Ty].empty() && Ty >= Type::FirstDerivedTyID && !IgnoreNamedNodes) {
Value *ZeroInitializer = Constant::getNullValue(Typ);
// If we are pushing zeroinit, it will be handled below.
if (D != ZeroInitializer) {
Table[Ty].push_back(ZeroInitializer);
NodeMap[ZeroInitializer] = 0;
}
}
// Insert node into table and NodeMap... // Insert node into table and NodeMap...
unsigned DestSlot = NodeMap[D] = Table[Ty].size(); unsigned DestSlot = NodeMap[D] = Table[Ty].size();
Table[Ty].push_back(D); Table[Ty].push_back(D);

View File

@ -21,7 +21,7 @@
#include <algorithm> #include <algorithm>
#if 0 #if 0
#define SC_DEBUG(X) cerr << X #define SC_DEBUG(X) std::cerr << X
#else #else
#define SC_DEBUG(X) #define SC_DEBUG(X)
#endif #endif
@ -67,26 +67,26 @@ SlotCalculator::SlotCalculator(const Function *M, bool IgnoreNamed) {
void SlotCalculator::processModule() { void SlotCalculator::processModule() {
SC_DEBUG("begin processModule!\n"); SC_DEBUG("begin processModule!\n");
// Add all of the constants that the global variables might refer to first. // Add all of the global variables to the value table...
// //
for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend(); for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
I != E; ++I) I != E; ++I)
if (I->hasInitializer())
insertValue(I->getInitializer());
// Add all of the global variables to the value table...
//
for(Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
I != E; ++I)
insertValue(I); insertValue(I);
// Scavenge the types out of the functions, then add the functions themselves // Scavenge the types out of the functions, then add the functions themselves
// to the value table... // to the value table...
// //
for(Module::const_iterator I = TheModule->begin(), E = TheModule->end(); for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
I != E; ++I) I != E; ++I)
insertValue(I); insertValue(I);
// Add all of the module level constants used as initializers
//
for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
I != E; ++I)
if (I->hasInitializer())
insertValue(I->getInitializer());
// Insert constants that are named at module level into the slot pool so that // Insert constants that are named at module level into the slot pool so that
// the module symbol table can refer to them... // the module symbol table can refer to them...
// //
@ -141,8 +141,7 @@ void SlotCalculator::incorporateFunction(const Function *M) {
SC_DEBUG("Inserting function constants:\n"; SC_DEBUG("Inserting function constants:\n";
for (constant_iterator I = constant_begin(M), E = constant_end(M); for (constant_iterator I = constant_begin(M), E = constant_end(M);
I != E; ++I) { I != E; ++I) {
cerr << " " << *I->getType() std::cerr << " " << *I->getType() << " " << *I << "\n";
<< " " << *I << "\n";
}); });
// Emit all of the constants that are being used by the instructions in the // Emit all of the constants that are being used by the instructions in the
@ -164,8 +163,6 @@ void SlotCalculator::incorporateFunction(const Function *M) {
// Iterate over basic blocks, adding them to the value table... // Iterate over basic blocks, adding them to the value table...
for (Function::const_iterator I = M->begin(), E = M->end(); I != E; ++I) for (Function::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
insertValue(I); insertValue(I);
/* for_each(M->begin(), M->end(),
bind_obj(this, &SlotCalculator::insertValue));*/
SC_DEBUG("Inserting Instructions:\n"); SC_DEBUG("Inserting Instructions:\n");
@ -230,21 +227,21 @@ int SlotCalculator::getValSlot(const Value *D) const {
} }
int SlotCalculator::insertValue(const Value *D) { int SlotCalculator::insertValue(const Value *V) {
if (isa<Constant>(D) || isa<GlobalVariable>(D)) { int SlotNo = getValSlot(V); // Check to see if it's already in!
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(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 (SlotNo != -1) return SlotNo; if (SlotNo != -1) return SlotNo;
return insertVal(D);
if (!isa<GlobalValue>(V))
if (const Constant *C = dyn_cast<Constant>(V)) {
// This makes sure that if a constant has uses (for example an array of
// const ints), that they are inserted also.
//
for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
I != E; ++I)
insertValue(*I);
}
return insertVal(V);
} }
@ -260,7 +257,7 @@ int SlotCalculator::insertVal(const Value *D, bool dontIgnore) {
if (D->getType() == Type::VoidTy || // Ignore void type nodes if (D->getType() == Type::VoidTy || // Ignore void type nodes
(IgnoreNamedNodes && // Ignore named and constants (IgnoreNamedNodes && // Ignore named and constants
(D->hasName() || isa<Constant>(D)) && !isa<Type>(D))) { (D->hasName() || isa<Constant>(D)) && !isa<Type>(D))) {
SC_DEBUG("ignored value " << D << "\n"); SC_DEBUG("ignored value " << *D << "\n");
return -1; // We do need types unconditionally though return -1; // We do need types unconditionally though
} }
@ -316,13 +313,13 @@ int SlotCalculator::doInsertVal(const Value *D) {
// cerr << "Inserting type '" << cast<Type>(D)->getDescription() << "'!\n"; // cerr << "Inserting type '" << cast<Type>(D)->getDescription() << "'!\n";
if (Typ->isDerivedType()) { if (Typ->isDerivedType()) {
int DefSlot = getValSlot(Typ); int ValSlot = getValSlot(Typ);
if (DefSlot == -1) { // Have we already entered this type? if (ValSlot == -1) { // Have we already entered this type?
// Nope, this is the first we have seen the type, process it. // Nope, this is the first we have seen the type, process it.
DefSlot = insertVal(Typ, true); ValSlot = insertVal(Typ, true);
assert(DefSlot != -1 && "ProcessType returned -1 for a type?"); assert(ValSlot != -1 && "ProcessType returned -1 for a type?");
} }
Ty = (unsigned)DefSlot; Ty = (unsigned)ValSlot;
} else { } else {
Ty = Typ->getPrimitiveID(); Ty = Typ->getPrimitiveID();
} }
@ -330,6 +327,18 @@ int SlotCalculator::doInsertVal(const Value *D) {
if (Table.size() <= Ty) // Make sure we have the type plane allocated... if (Table.size() <= Ty) // Make sure we have the type plane allocated...
Table.resize(Ty+1, TypePlane()); Table.resize(Ty+1, TypePlane());
// If this is the first value to get inserted into the type plane, make sure
// to insert the implicit null value...
if (Table[Ty].empty() && Ty >= Type::FirstDerivedTyID && !IgnoreNamedNodes) {
Value *ZeroInitializer = Constant::getNullValue(Typ);
// If we are pushing zeroinit, it will be handled below.
if (D != ZeroInitializer) {
Table[Ty].push_back(ZeroInitializer);
NodeMap[ZeroInitializer] = 0;
}
}
// Insert node into table and NodeMap... // Insert node into table and NodeMap...
unsigned DestSlot = NodeMap[D] = Table[Ty].size(); unsigned DestSlot = NodeMap[D] = Table[Ty].size();
Table[Ty].push_back(D); Table[Ty].push_back(D);