#include "llvm/SlotCalculator.h"
#include "llvm/Analysis/ConstantsScanner.h"
-#include "llvm/Module.h"
-#include "llvm/iOther.h"
-#include "llvm/Constant.h"
+#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
+#include "llvm/iOther.h"
+#include "llvm/Module.h"
#include "llvm/SymbolTable.h"
#include "Support/PostOrderIterator.h"
#include "Support/STLExtras.h"
#include <algorithm>
-
-namespace llvm {
+using namespace llvm;
#if 0
#define SC_DEBUG(X) std::cerr << X
#define SC_DEBUG(X)
#endif
-SlotCalculator::SlotCalculator(const Module *M, bool IgnoreNamed) {
- IgnoreNamedNodes = IgnoreNamed;
+SlotCalculator::SlotCalculator(const Module *M, bool buildBytecodeInfo) {
+ BuildBytecodeInfo = buildBytecodeInfo;
TheModule = M;
// Preload table... Make sure that all of the primitive types are in the table
processModule();
}
-SlotCalculator::SlotCalculator(const Function *M, bool IgnoreNamed) {
- IgnoreNamedNodes = IgnoreNamed;
+SlotCalculator::SlotCalculator(const Function *M, bool buildBytecodeInfo) {
+ BuildBytecodeInfo = buildBytecodeInfo;
TheModule = M ? M->getParent() : 0;
// Preload table... Make sure that all of the primitive types are in the table
if (I->hasInitializer())
getOrCreateSlot(I->getInitializer());
+ // Now that all global constants have been added, rearrange constant planes
+ // that contain constant strings so that the strings occur at the start of the
+ // plane, not somewhere in the middle.
+ //
+ if (BuildBytecodeInfo) {
+ TypePlane &Types = Table[Type::TypeTyID];
+ for (unsigned plane = 0, e = Table.size(); plane != e; ++plane) {
+ if (const ArrayType *AT = dyn_cast<ArrayType>(Types[plane]))
+ if (AT->getElementType() == Type::SByteTy ||
+ AT->getElementType() == Type::UByteTy) {
+ TypePlane &Plane = Table[plane];
+ unsigned FirstNonStringID = 0;
+ for (unsigned i = 0, e = Plane.size(); i != e; ++i)
+ if (cast<ConstantArray>(Plane[i])->isString()) {
+ // Check to see if we have to shuffle this string around. If not,
+ // don't do anything.
+ if (i != FirstNonStringID) {
+ // Swap the plane entries....
+ std::swap(Plane[i], Plane[FirstNonStringID]);
+
+ // Keep the NodeMap up to date.
+ NodeMap[Plane[i]] = i;
+ NodeMap[Plane[FirstNonStringID]] = FirstNonStringID;
+ }
+ ++FirstNonStringID;
+ }
+ }
+ }
+ }
+
+#if 0
+ // FIXME: Empirically, this causes the bytecode files to get BIGGER, because
+ // it explodes the operand size numbers to be bigger than can be handled
+ // compactly, which offsets the ~40% savings in constant sizes. Whoops.
+
+ // If we are emitting a bytecode file, scan all of the functions for their
+ // constants, which allows us to emit more compact modules. This is optional,
+ // and is just used to compactify the constants used by different functions
+ // together.
+ if (BuildBytecodeInfo) {
+ SC_DEBUG("Inserting function constants:\n");
+ for (Module::const_iterator F = TheModule->begin(), E = TheModule->end();
+ F != E; ++F)
+ for_each(constant_begin(F), constant_end(F),
+ bind_obj(this, &SlotCalculator::getOrCreateSlot));
+ }
+#endif
+
// Insert constants that are named at module level into the slot pool so that
// the module symbol table can refer to them...
//
- if (!IgnoreNamedNodes) {
+ if (BuildBytecodeInfo) {
SC_DEBUG("Inserting SymbolTable values:\n");
processSymbolTable(&TheModule->getSymbolTable());
}
+ // Now that we have collected together all of the information relevant to the
+ // module, compactify the type table if it is particularly big and outputting
+ // a bytecode file. The basic problem we run into is that some programs have
+ // a large number of types, which causes the type field to overflow its size,
+ // which causes instructions to explode in size (particularly call
+ // instructions). To avoid this behavior, we "sort" the type table so that
+ // all non-value types are pushed to the end of the type table, giving nice
+ // low numbers to the types that can be used by instructions, thus reducing
+ // the amount of explodage we suffer.
+ if (BuildBytecodeInfo && Table[Type::TypeTyID].size() >= 64) {
+ // Scan through the type table moving value types to the start of the table.
+ TypePlane *Types = &Table[Type::TypeTyID];
+ unsigned FirstNonValueTypeID = 0;
+ for (unsigned i = 0, e = Types->size(); i != e; ++i)
+ if (cast<Type>((*Types)[i])->isFirstClassType() ||
+ cast<Type>((*Types)[i])->isPrimitiveType()) {
+ // Check to see if we have to shuffle this type around. If not, don't
+ // do anything.
+ if (i != FirstNonValueTypeID) {
+ assert(i != Type::TypeTyID && FirstNonValueTypeID != Type::TypeTyID &&
+ "Cannot move around the type plane!");
+
+ // Swap the type ID's.
+ std::swap((*Types)[i], (*Types)[FirstNonValueTypeID]);
+
+ // Keep the NodeMap up to date.
+ NodeMap[(*Types)[i]] = i;
+ NodeMap[(*Types)[FirstNonValueTypeID]] = FirstNonValueTypeID;
+
+ // When we move a type, make sure to move its value plane as needed.
+ if (Table.size() > FirstNonValueTypeID) {
+ if (Table.size() <= i) Table.resize(i+1);
+ std::swap(Table[i], Table[FirstNonValueTypeID]);
+ Types = &Table[Type::TypeTyID];
+ }
+ }
+ ++FirstNonValueTypeID;
+ }
+ }
+
SC_DEBUG("end processModule!\n");
}
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 (isa<Constant>(TI->second))
+ if (isa<Constant>(TI->second) || isa<Type>(TI->second))
getOrCreateSlot(TI->second);
}
// 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 function constants:\n";
- for (constant_iterator I = constant_begin(F), E = constant_end(F);
- I != E; ++I) {
- std::cerr << " " << *I->getType() << " " << *I << "\n";
- });
-
+ if (BuildBytecodeInfo) { // Assembly writer does not need this!
// Emit all of the constants that are being used by the instructions in the
// function...
for_each(constant_begin(F), constant_end(F),
// 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
+ // symbol table references to constants not in the output. Scan for these
// constants now.
//
processSymbolTableConstants(&F->getSymbolTable());
}
- SC_DEBUG("Inserting Labels:\n");
-
- // Iterate over basic blocks, adding them to the value table...
- for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
- getOrCreateSlot(I);
-
SC_DEBUG("Inserting Instructions:\n");
// Add all of the instructions to the type planes...
- for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
+ for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
+ getOrCreateSlot(BB);
for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
getOrCreateSlot(I);
if (const VANextInst *VAN = dyn_cast<VANextInst>(I))
getOrCreateSlot(VAN->getArgType());
}
-
- if (!IgnoreNamedNodes) {
- SC_DEBUG("Inserting SymbolTable values:\n");
- processSymbolTable(&F->getSymbolTable());
}
SC_DEBUG("end processFunction!\n");
SC_DEBUG("end purgeFunction!\n");
}
-int SlotCalculator::getSlot(const Value *D) const {
- std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(D);
- if (I == NodeMap.end()) return -1;
-
- return (int)I->second;
+int SlotCalculator::getSlot(const Value *V) const {
+ std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(V);
+ if (I != NodeMap.end())
+ return (int)I->second;
+
+ // Do not number ConstantPointerRef's at all. They are an abomination.
+ if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V))
+ return getSlot(CPR->getValue());
+
+ return -1;
}
int SlotNo = getSlot(V); // Check to see if it's already in!
if (SlotNo != -1) return SlotNo;
+ // Do not number ConstantPointerRef's at all. They are an abomination.
+ if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V))
+ return getOrCreateSlot(CPR->getValue());
+
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)
- getOrCreateSlot(*I);
+ // If we are emitting a bytecode file, do not index the characters that
+ // make up constant strings. We emit constant strings as special
+ // entities that don't require their individual characters to be emitted.
+ if (!BuildBytecodeInfo || !isa<ConstantArray>(C) ||
+ !cast<ConstantArray>(C)->isString()) {
+ // 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)
+ getOrCreateSlot(*I);
+ } else {
+ assert(ModuleLevel.empty() &&
+ "How can a constant string be directly accessed in a function?");
+ // Otherwise, if we are emitting a bytecode file and this IS a string,
+ // remember it.
+ if (!C->isNullValue())
+ ConstantStrings.push_back(cast<ConstantArray>(C));
+ }
}
return insertValue(V);
//
if (!dontIgnore) // Don't ignore nonignorables!
if (D->getType() == Type::VoidTy || // Ignore void type nodes
- (IgnoreNamedNodes && // Ignore named and constants
+ (!BuildBytecodeInfo && // Ignore named and constants
(D->hasName() || isa<Constant>(D)) && !isa<Type>(D))) {
SC_DEBUG("ignored value " << *D << "\n");
return -1; // We do need types unconditionally though
// 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) {
+ if (Table[Ty].empty() && Ty >= Type::FirstDerivedTyID && BuildBytecodeInfo) {
Value *ZeroInitializer = Constant::getNullValue(Typ);
// If we are pushing zeroinit, it will be handled below.
SC_DEBUG("]\n");
return (int)DestSlot;
}
-
-} // End llvm namespace