//===-- SlotCalculator.cpp - Calculate what slots values land in ----------===//
-//
+//
// The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
-//
+//
//===----------------------------------------------------------------------===//
//
// This file implements a useful analysis step to figure out what numbered slots
TypePlane &Plane = Table[plane];
unsigned FirstNonStringID = 0;
for (unsigned i = 0, e = Plane.size(); i != e; ++i)
- if (isa<ConstantAggregateZero>(Plane[i]) ||
+ if (isa<ConstantAggregateZero>(Plane[i]) ||
(isa<ConstantArray>(Plane[i]) &&
cast<ConstantArray>(Plane[i])->isString())) {
// Check to see if we have to shuffle this string around. If not,
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;
}
}
}
-
- // 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
+
+ // 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.
//
- // This functionality tends to produce smaller bytecode files. This should
- // not be used in the future by clients that want to, for example, build and
- // emit functions on the fly. For now, however, it is unconditionally
+ // This functionality tends to produce smaller bytecode files. This should
+ // not be used in the future by clients that want to, for example, build and
+ // emit functions on the fly. For now, however, it is unconditionally
// enabled.
ModuleContainsAllFunctionConstants = true;
F != E; ++F) {
for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I){
for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
- if (isa<Constant>(I->getOperand(op)) &&
+ if (isa<Constant>(I->getOperand(op)) &&
!isa<GlobalValue>(I->getOperand(op)))
getOrCreateSlot(I->getOperand(op));
getOrCreateSlot(I->getType());
getOrCreateSlot(TI->second);
// Now do the values.
- for (SymbolTable::plane_const_iterator PI = ST->plane_begin(),
+ for (SymbolTable::plane_const_iterator PI = ST->plane_begin(),
PE = ST->plane_end(); PI != PE; ++PI)
for (SymbolTable::value_const_iterator VI = PI->second.begin(),
VE = PI->second.end(); VI != VE; ++VI)
getOrCreateSlot(TI->second);
// Now do the constant values in all planes
- for (SymbolTable::plane_const_iterator PI = ST->plane_begin(),
+ for (SymbolTable::plane_const_iterator PI = ST->plane_begin(),
PE = ST->plane_end(); PI != PE; ++PI)
for (SymbolTable::value_const_iterator VI = PI->second.begin(),
VE = PI->second.end(); VI != VE; ++VI)
// before any nonconstant values. This will be turned into the constant
// pool for the bytecode writer.
//
-
+
// Emit all of the constants that are being used by the instructions in
// the function...
constant_iterator CI = constant_begin(F);
this->getOrCreateSlot(*CI);
++CI;
}
-
+
// 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
+ // if we don't emit the constants now, because otherwise we will get
// symbol table references to constants not in the output. Scan for these
// constants now.
//
NodeMap.erase(Plane.back()); // Erase from nodemap
Plane.pop_back(); // Shrink plane
}
-
+
Table.pop_back(); // Nuke the plane, we don't like it.
}
}
getOrCreateCompactionTableSlot(TI->second);
// Now do the constants and global values
- for (SymbolTable::plane_const_iterator PI = ST.plane_begin(),
+ for (SymbolTable::plane_const_iterator PI = ST.plane_begin(),
PE = ST.plane_end(); PI != PE; ++PI)
for (SymbolTable::value_const_iterator VI = PI->second.begin(),
VE = PI->second.end(); VI != VE; ++VI)
assert(Ty->getTypeID() != Type::LabelTyID);
getOrCreateCompactionTableSlot(Constant::getNullValue(Ty));
}
-
+
// Okay, now at this point, we have a legal compaction table. Since we want
// to emit the smallest possible binaries, do not compactify the type plane if
// it will not save us anything. Because we have not yet incorporated the
// function body itself yet, we don't know whether or not it's a good idea to
// compactify other planes. We will defer this decision until later.
TypeList &GlobalTypes = Types;
-
+
// All of the values types will be scrunched to the start of the types plane
// of the global table. Figure out just how many there are.
assert(!GlobalTypes.empty() && "No global types???");
std::swap(CompactionTable, TmpCompactionTable);
TypeList TmpTypes;
std::swap(TmpTypes, CompactionTypes);
-
+
// Move each plane back over to the uncompactified plane
while (!TmpTypes.empty()) {
const Type *Ty = TmpTypes.back();
// Find the global slot number for this type.
int TySlot = getSlot(Ty);
assert(TySlot != -1 && "Type doesn't exist in global table?");
-
+
// Now we know where to put the compaction table plane.
if (CompactionTable.size() <= unsigned(TySlot))
CompactionTable.resize(TySlot+1);
if (GlobalSlot >= Table.size())
Table.resize(GlobalSlot+1);
TypePlane &GPlane = Table[GlobalSlot];
-
+
unsigned ModLevel = getModuleLevel(ctp);
unsigned NumFunctionObjs = CPlane.size()-ModLevel;
}
/// Determine if the compaction table is actually empty. Because the
-/// compaction table always includes the primitive type planes, we
+/// compaction table always includes the primitive type planes, we
/// can't just check getCompactionTable().size() because it will never
/// be zero. Furthermore, the ModuleLevel factors into whether a given
/// plane is empty or not. This function does the necessary computation
// If the module level is non-zero then at least the
// first element of the plane is valid and therefore not empty.
unsigned End = getModuleLevel(i);
- if (End != 0)
+ if (End != 0)
return false;
}
}
assert(CompactionNodeMap.empty() &&
"All needed constants should be in the compaction map already!");
- // Do not index the characters that make up constant strings. We emit
- // constant strings as special entities that don't require their
+ // 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 (!isa<ConstantArray>(C) || !cast<ConstantArray>(C)->isString()) {
// This makes sure that if a constant has uses (for example an array of
return getOrCreateCompactionTableSlot(D);
}
- // If this node does not contribute to a plane, or if the node has a
+ // If this node does not contribute to a plane, or if the node has a
// 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.
//
} else {
Ty = Typ->getTypeID();
}
-
+
if (Table.size() <= Ty) // Make sure we have the type plane allocated...
Table.resize(Ty+1, TypePlane());
unsigned DestSlot = NodeMap[D] = Table[Ty].size();
Table[Ty].push_back(D);
- SC_DEBUG(" Inserting value [" << Ty << "] = " << D << " slot=" <<
+ SC_DEBUG(" Inserting value [" << Ty << "] = " << D << " slot=" <<
DestSlot << " [");
// G = Global, C = Constant, T = Type, F = Function, o = other
- SC_DEBUG((isa<GlobalVariable>(D) ? "G" : (isa<Constant>(D) ? "C" :
+ SC_DEBUG((isa<GlobalVariable>(D) ? "G" : (isa<Constant>(D) ? "C" :
(isa<Function>(D) ? "F" : "o"))));
SC_DEBUG("]\n");
return (int)DestSlot;
//===-- Writer.cpp - Library for writing LLVM bytecode files --------------===//
-//
+//
// The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
-//
+//
//===----------------------------------------------------------------------===//
//
// This library implements the functionality defined in llvm/Bytecode/Writer.h
static RegisterPass<WriteBytecodePass> X("emitbytecode", "Bytecode Writer");
-static Statistic<>
+static Statistic<>
BytesWritten("bytecodewriter", "Number of bytecode bytes written");
//===----------------------------------------------------------------------===//
//===----------------------------------------------------------------------===//
// output - If a position is specified, it must be in the valid portion of the
-// string... note that this should be inlined always so only the relevant IF
+// string... note that this should be inlined always so only the relevant IF
// body should be included.
inline void BytecodeWriter::output(unsigned i, int pos) {
if (pos == -1) { // Be endian clean, little endian is our friend
- Out.push_back((unsigned char)i);
+ Out.push_back((unsigned char)i);
Out.push_back((unsigned char)(i >> 8));
Out.push_back((unsigned char)(i >> 16));
Out.push_back((unsigned char)(i >> 24));
/// output_vbr - Output an unsigned value, by using the least number of bytes
/// possible. This is useful because many of our "infinite" values are really
/// very small most of the time; but can be large a few times.
-/// Data format used: If you read a byte with the high bit set, use the low
-/// seven bits as data and then read another byte.
+/// Data format used: If you read a byte with the high bit set, use the low
+/// seven bits as data and then read another byte.
inline void BytecodeWriter::output_vbr(uint64_t i) {
while (1) {
if (i < 0x80) { // done?
Out.push_back((unsigned char)i); // We know the high bit is clear...
return;
}
-
+
// Nope, we are bigger than a character, output the next 7 bits and set the
// high bit to say that there is more coming...
Out.push_back(0x80 | ((unsigned char)i & 0x7F));
Out.push_back((unsigned char)i); // We know the high bit is clear...
return;
}
-
+
// Nope, we are bigger than a character, output the next 7 bits and set the
// high bit to say that there is more coming...
Out.push_back(0x80 | ((unsigned char)i & 0x7F));
}
inline void BytecodeWriter::output_vbr(int64_t i) {
- if (i < 0)
+ if (i < 0)
output_vbr(((uint64_t)(-i) << 1) | 1); // Set low order sign bit...
else
output_vbr((uint64_t)i << 1); // Low order bit is clear.
inline void BytecodeWriter::output_vbr(int i) {
- if (i < 0)
+ if (i < 0)
output_vbr(((unsigned)(-i) << 1) | 1); // Set low order sign bit...
else
output_vbr((unsigned)i << 1); // Low order bit is clear.
}
inline BytecodeBlock::BytecodeBlock(unsigned ID, BytecodeWriter& w,
- bool elideIfEmpty, bool hasLongFormat )
+ bool elideIfEmpty, bool hasLongFormat )
: Id(ID), Writer(w), ElideIfEmpty(elideIfEmpty), HasLongFormat(hasLongFormat){
if (HasLongFormat) {
}
inline BytecodeBlock::~BytecodeBlock() { // Do backpatch when block goes out
- // of scope...
+ // of scope...
if (Loc == Writer.size() && ElideIfEmpty) {
// If the block is empty, and we are allowed to, do not emit the block at
// all!
void BytecodeWriter::outputType(const Type *T) {
output_vbr((unsigned)T->getTypeID());
-
+
// That's all there is to handling primitive types...
if (T->isPrimitiveType()) {
return; // We might do this if we alias a prim type: %x = type int
// We must check for a ConstantExpr before switching by type because
// a ConstantExpr can be of any type, and has no explicit value.
- //
+ //
if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
// FIXME: Encoding of constant exprs could be much more compact!
assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
assert(CE->getNumOperands() != 1 || CE->getOpcode() == Instruction::Cast);
output_vbr(1+CE->getNumOperands()); // flags as an expr
output_vbr(CE->getOpcode()); // flags as an expr
-
+
for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){
int Slot = Table.getSlot(*OI);
assert(Slot != -1 && "Unknown constant used in ConstantExpr!!");
} else {
output_vbr(0U); // flag as not a ConstantExpr
}
-
+
switch (CPV->getType()->getTypeID()) {
case Type::BoolTyID: // Boolean Types
if (cast<ConstantBool>(CPV)->getValue())
break;
}
- case Type::VoidTyID:
+ case Type::VoidTyID:
case Type::LabelTyID:
default:
std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
// the 'void' type plane.
output_vbr(unsigned(E-I));
output_typeid(Type::VoidTyID);
-
+
// Emit all of the strings.
for (I = Table.string_begin(); I != E; ++I) {
const ConstantArray *Str = *I;
int Slot = Table.getSlot(Str->getType());
assert(Slot != -1 && "Constant string of unknown type?");
output_typeid((unsigned)Slot);
-
+
// Now that we emitted the type (which indicates the size of the string),
// emit all of the characters.
std::string Val = Str->getAsString();
if (!isa<GetElementPtrInst>(&I)) {
for (unsigned i = 0; i < NumArgs; ++i) {
int Slot = Table.getSlot(I->getOperand(i));
- assert(Slot >= 0 && "No slot number for value!?!?");
+ assert(Slot >= 0 && "No slot number for value!?!?");
output_vbr((unsigned)Slot);
}
} else {
int Slot = Table.getSlot(I->getOperand(0));
- assert(Slot >= 0 && "No slot number for value!?!?");
+ assert(Slot >= 0 && "No slot number for value!?!?");
output_vbr(unsigned(Slot));
// We need to encode the type of sequential type indices into their slot #
for (gep_type_iterator TI = gep_type_begin(I), E = gep_type_end(I);
Idx != NumArgs; ++TI, ++Idx) {
Slot = Table.getSlot(I->getOperand(Idx));
- assert(Slot >= 0 && "No slot number for value!?!?");
-
+ assert(Slot >= 0 && "No slot number for value!?!?");
+
if (isa<SequentialType>(*TI)) {
unsigned IdxId;
switch (I->getOperand(Idx)->getType()->getTypeID()) {
//
// Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
//
-void BytecodeWriter::outputInstrVarArgsCall(const Instruction *I,
- unsigned Opcode,
- const SlotCalculator &Table,
- unsigned Type) {
+void BytecodeWriter::outputInstrVarArgsCall(const Instruction *I,
+ unsigned Opcode,
+ const SlotCalculator &Table,
+ unsigned Type) {
assert(isa<CallInst>(I) || isa<InvokeInst>(I));
// Opcode must have top two bits clear...
output_vbr(Opcode << 2); // Instruction Opcode ID
// instruction. Just emit the slot # now.
for (unsigned i = 0; i != NumFixedOperands; ++i) {
int Slot = Table.getSlot(I->getOperand(i));
- assert(Slot >= 0 && "No slot number for value!?!?");
+ assert(Slot >= 0 && "No slot number for value!?!?");
output_vbr((unsigned)Slot);
}
for (unsigned i = NumFixedOperands, e = I->getNumOperands(); i != e; ++i) {
// Output Arg Type ID
int Slot = Table.getSlot(I->getOperand(i)->getType());
- assert(Slot >= 0 && "No slot number for value!?!?");
+ assert(Slot >= 0 && "No slot number for value!?!?");
output_typeid((unsigned)Slot);
-
+
// Output arg ID itself
Slot = Table.getSlot(I->getOperand(i));
- assert(Slot >= 0 && "No slot number for value!?!?");
+ assert(Slot >= 0 && "No slot number for value!?!?");
output_vbr((unsigned)Slot);
}
}
// outputInstructionFormat1 - Output one operand instructions, knowing that no
// operand index is >= 2^12.
//
-inline void BytecodeWriter::outputInstructionFormat1(const Instruction *I,
- unsigned Opcode,
- unsigned *Slots,
- unsigned Type) {
+inline void BytecodeWriter::outputInstructionFormat1(const Instruction *I,
+ unsigned Opcode,
+ unsigned *Slots,
+ unsigned Type) {
// bits Instruction format:
// --------------------------
// 01-00: Opcode type, fixed to 1.
// outputInstructionFormat2 - Output two operand instructions, knowing that no
// operand index is >= 2^8.
//
-inline void BytecodeWriter::outputInstructionFormat2(const Instruction *I,
- unsigned Opcode,
- unsigned *Slots,
- unsigned Type) {
+inline void BytecodeWriter::outputInstructionFormat2(const Instruction *I,
+ unsigned Opcode,
+ unsigned *Slots,
+ unsigned Type) {
// bits Instruction format:
// --------------------------
// 01-00: Opcode type, fixed to 2.
// 07-02: Opcode
// 15-08: Resulting type plane
// 23-16: Operand #1
- // 31-24: Operand #2
+ // 31-24: Operand #2
//
output(2 | (Opcode << 2) | (Type << 8) | (Slots[0] << 16) | (Slots[1] << 24));
}
// outputInstructionFormat3 - Output three operand instructions, knowing that no
// operand index is >= 2^6.
//
-inline void BytecodeWriter::outputInstructionFormat3(const Instruction *I,
+inline void BytecodeWriter::outputInstructionFormat3(const Instruction *I,
unsigned Opcode,
- unsigned *Slots,
- unsigned Type) {
+ unsigned *Slots,
+ unsigned Type) {
// bits Instruction format:
// --------------------------
// 01-00: Opcode type, fixed to 3.
// the type of the first parameter, as opposed to the type of the instruction
// (for example, with setcc, we always know it returns bool, but the type of
// the first param is actually interesting). But if we have no arguments
- // we take the type of the instruction itself.
+ // we take the type of the instruction itself.
//
const Type *Ty;
switch (I.getOpcode()) {
//
unsigned MaxOpSlot = Type;
unsigned Slots[3]; Slots[0] = (1 << 12)-1; // Marker to signify 0 operands
-
+
for (unsigned i = 0; i != NumOperands; ++i) {
int slot = Table.getSlot(I.getOperand(i));
assert(slot != -1 && "Broken bytecode!");
//=== Block Output ===//
//===----------------------------------------------------------------------===//
-BytecodeWriter::BytecodeWriter(std::vector<unsigned char> &o, const Module *M)
+BytecodeWriter::BytecodeWriter(std::vector<unsigned char> &o, const Module *M)
: Out(o), Table(M) {
// Emit the signature...
bool hasNoPointerSize = M->getPointerSize() == Module::AnyPointerSize;
// Output the version identifier and other information.
- unsigned Version = (BCVersionNum << 4) |
+ unsigned Version = (BCVersionNum << 4) |
(unsigned)isBigEndian | (hasLongPointers << 1) |
- (hasNoEndianness << 2) |
+ (hasNoEndianness << 2) |
(hasNoPointerSize << 3);
output_vbr(Version);
assert(TypeNum <= Types.size() && "Invalid TypeNo index");
unsigned NumEntries = Types.size() - TypeNum;
-
+
// Output type header: [num entries]
output_vbr(NumEntries);
// Helper function for outputConstants().
// Writes out all the constants in the plane Plane starting at entry StartNo.
-//
+//
void BytecodeWriter::outputConstantsInPlane(const std::vector<const Value*>
&Plane, unsigned StartNo) {
unsigned ValNo = StartNo;
-
+
// Scan through and ignore function arguments, global values, and constant
// strings.
for (; ValNo < Plane.size() &&
unsigned ValNo = 0;
if (isFunction) // Don't re-emit module constants
ValNo += Table.getModuleLevel(pno);
-
+
if (hasNullValue(pno)) {
// Skip zero initializer
if (ValNo == 0)
ValNo = 1;
}
-
+
// Write out constants in the plane
outputConstantsInPlane(Plane, ValNo);
}
void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfoBlockID, *this);
-
+
// Output the types for the global variables in the module...
for (Module::const_global_iterator I = M->global_begin(), End = M->global_end(); I != End;++I) {
int Slot = Table.getSlot(I->getType());
// Otherwise, emit the compaction table.
outputCompactionTable();
}
-
+
// Output all of the instructions in the body of the function
outputInstructions(F);
-
+
// If needed, output the symbol table for the function...
outputSymbolTable(F->getSymbolTable());
-
+
Table.purgeFunction();
}
// Avoid writing the compaction table at all if there is no content.
if (Table.getCompactionTypes().size() >= Type::FirstDerivedTyID ||
(!Table.CompactionTableIsEmpty())) {
- BytecodeBlock CTB(BytecodeFormat::CompactionTableBlockID, *this,
+ BytecodeBlock CTB(BytecodeFormat::CompactionTableBlockID, *this,
true/*ElideIfEmpty*/);
const std::vector<std::vector<const Value*> > &CT =
Table.getCompactionTable();
-
+
// First things first, emit the type compaction table if there is one.
outputCompactionTypes(Type::FirstDerivedTyID);
BytecodeBlock SymTabBlock(BytecodeFormat::SymbolTableBlockID, *this,
true/*ElideIfEmpty*/);
- // Write the number of types
+ // Write the number of types
output_vbr(MST.num_types());
// Write each of the types
TE = MST.type_end(); TI != TE; ++TI ) {
// Symtab entry:[def slot #][name]
output_typeid((unsigned)Table.getSlot(TI->second));
- output(TI->first);
+ output(TI->first);
}
// Now do each of the type planes in order.
- for (SymbolTable::plane_const_iterator PI = MST.plane_begin(),
+ for (SymbolTable::plane_const_iterator PI = MST.plane_begin(),
PE = MST.plane_end(); PI != PE; ++PI) {
SymbolTable::value_const_iterator I = MST.value_begin(PI->first);
SymbolTable::value_const_iterator End = MST.value_end(PI->first);
int Slot;
-
+
if (I == End) continue; // Don't mess with an absent type...
// Write the number of values in this plane
// We signal compression by using an alternate magic number for the
// file. The compressed bytecode file's magic number is "llvc" instead
- // of "llvm".
+ // of "llvm".
char compressed_magic[4];
compressed_magic[0] = 'l';
compressed_magic[1] = 'l';