Initial support for writing bitcode files. This currently only writes types,

the type symtab, and global/function protos, and is missing the important
size optimization, but it is a place to start.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36331 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2007-04-22 06:24:45 +00:00
parent caee0dccff
commit fd57cecd2c
4 changed files with 652 additions and 0 deletions

View File

@ -0,0 +1,317 @@
//===--- Bitcode/Writer/Writer.cpp - Bitcode Writer -----------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by Chris Lattner and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Bitcode writer implementation.
//
//===----------------------------------------------------------------------===//
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/Bitcode/BitstreamWriter.h"
#include "../LLVMBitCodes.h"
#include "ValueEnumerator.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Module.h"
#include "llvm/TypeSymbolTable.h"
#include "llvm/Support/MathExtras.h"
using namespace llvm;
static const unsigned CurVersion = 0;
static void WriteStringRecord(unsigned Code, const std::string &Str,
unsigned AbbrevToUse, BitstreamWriter &Stream) {
SmallVector<unsigned, 64> Vals;
// Code: [strlen, strchar x N]
Vals.push_back(Str.size());
for (unsigned i = 0, e = Str.size(); i != e; ++i)
Vals.push_back(Str[i]);
// Emit the finished record.
Stream.EmitRecord(Code, Vals, AbbrevToUse);
}
/// WriteTypeTable - Write out the type table for a module.
static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) {
const ValueEnumerator::TypeList &TypeList = VE.getTypes();
Stream.EnterSubblock(bitc::TYPE_BLOCK_ID, 4 /*count from # abbrevs */);
SmallVector<uint64_t, 64> TypeVals;
// FIXME: Set up abbrevs now that we know the width of the type fields, etc.
// Emit an entry count so the reader can reserve space.
TypeVals.push_back(TypeList.size());
Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
TypeVals.clear();
// Loop over all of the types, emitting each in turn.
for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
const Type *T = TypeList[i].first;
int AbbrevToUse = 0;
unsigned Code = 0;
switch (T->getTypeID()) {
case Type::PackedStructTyID: // FIXME: Delete Type::PackedStructTyID.
default: assert(0 && "Unknown type!");
case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
case Type::OpaqueTyID: Code = bitc::TYPE_CODE_OPAQUE; break;
case Type::IntegerTyID:
// INTEGER: [width]
Code = bitc::TYPE_CODE_INTEGER;
TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
break;
case Type::PointerTyID:
// POINTER: [pointee type]
Code = bitc::TYPE_CODE_POINTER;
TypeVals.push_back(VE.getTypeID(cast<PointerType>(T)->getElementType()));
break;
case Type::FunctionTyID: {
const FunctionType *FT = cast<FunctionType>(T);
// FUNCTION: [isvararg, #pararms, paramty x N]
Code = bitc::TYPE_CODE_FUNCTION;
TypeVals.push_back(FT->isVarArg());
TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
// FIXME: PARAM ATTR ID!
TypeVals.push_back(FT->getNumParams());
for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
break;
}
case Type::StructTyID: {
const StructType *ST = cast<StructType>(T);
// STRUCT: [ispacked, #elts, eltty x N]
Code = bitc::TYPE_CODE_STRUCT;
TypeVals.push_back(ST->isPacked());
TypeVals.push_back(ST->getNumElements());
// Output all of the element types...
for (StructType::element_iterator I = ST->element_begin(),
E = ST->element_end(); I != E; ++I)
TypeVals.push_back(VE.getTypeID(*I));
break;
}
case Type::ArrayTyID: {
const ArrayType *AT = cast<ArrayType>(T);
// ARRAY: [numelts, eltty]
Code = bitc::TYPE_CODE_ARRAY;
TypeVals.push_back(AT->getNumElements());
TypeVals.push_back(VE.getTypeID(AT->getElementType()));
break;
}
case Type::VectorTyID: {
const VectorType *VT = cast<VectorType>(T);
// VECTOR [numelts, eltty]
Code = bitc::TYPE_CODE_VECTOR;
TypeVals.push_back(VT->getNumElements());
TypeVals.push_back(VE.getTypeID(VT->getElementType()));
break;
}
}
// Emit the finished record.
Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
TypeVals.clear();
}
Stream.ExitBlock();
}
/// WriteTypeSymbolTable - Emit a block for the specified type symtab.
static void WriteTypeSymbolTable(const TypeSymbolTable &TST,
const ValueEnumerator &VE,
BitstreamWriter &Stream) {
if (TST.empty()) return;
Stream.EnterSubblock(bitc::TYPE_SYMTAB_BLOCK_ID, 3);
// FIXME: Set up the abbrev, we know how many types there are!
// FIXME: We know if the type names can use 7-bit ascii.
SmallVector<unsigned, 64> NameVals;
for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
TI != TE; ++TI) {
unsigned AbbrevToUse = 0;
// TST_ENTRY: [typeid, namelen, namechar x N]
NameVals.push_back(VE.getTypeID(TI->second));
const std::string &Str = TI->first;
NameVals.push_back(Str.size());
for (unsigned i = 0, e = Str.size(); i != e; ++i)
NameVals.push_back(Str[i]);
// Emit the finished record.
Stream.EmitRecord(bitc::TST_ENTRY_CODE, NameVals, AbbrevToUse);
NameVals.clear();
}
Stream.ExitBlock();
}
static unsigned getEncodedLinkage(const GlobalValue *GV) {
switch (GV->getLinkage()) {
default: assert(0 && "Invalid linkage!");
case GlobalValue::ExternalLinkage: return 0;
case GlobalValue::WeakLinkage: return 1;
case GlobalValue::AppendingLinkage: return 2;
case GlobalValue::InternalLinkage: return 3;
case GlobalValue::LinkOnceLinkage: return 4;
case GlobalValue::DLLImportLinkage: return 5;
case GlobalValue::DLLExportLinkage: return 6;
case GlobalValue::ExternalWeakLinkage: return 7;
}
}
static unsigned getEncodedVisibility(const GlobalValue *GV) {
switch (GV->getVisibility()) {
default: assert(0 && "Invalid visibility!");
case GlobalValue::DefaultVisibility: return 0;
case GlobalValue::HiddenVisibility: return 1;
}
}
// Emit top-level description of module, including target triple, inline asm,
// descriptors for global variables, and function prototype info.
static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
BitstreamWriter &Stream) {
// Emit the list of dependent libraries for the Module.
for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
WriteStringRecord(bitc::MODULE_CODE_DEPLIB, *I, 0/*TODO*/, Stream);
// Emit various pieces of data attached to a module.
if (!M->getTargetTriple().empty())
WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
0/*TODO*/, Stream);
if (!M->getDataLayout().empty())
WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(),
0/*TODO*/, Stream);
if (!M->getModuleInlineAsm().empty())
WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
0/*TODO*/, Stream);
// Emit information about sections.
std::map<std::string, unsigned> SectionMap;
for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
GV != E; ++GV) {
if (!GV->hasSection()) continue;
// Give section names unique ID's.
unsigned &Entry = SectionMap[GV->getSection()];
if (Entry != 0) continue;
WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV->getSection(),
0/*TODO*/, Stream);
Entry = SectionMap.size();
}
for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
if (!F->hasSection()) continue;
// Give section names unique ID's.
unsigned &Entry = SectionMap[F->getSection()];
if (Entry != 0) continue;
WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F->getSection(),
0/*TODO*/, Stream);
Entry = SectionMap.size();
}
// TODO: Emit abbrev, now that we know # sections.
// Emit the global variable information.
SmallVector<unsigned, 64> Vals;
for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
GV != E; ++GV) {
// GLOBALVAR: [type, isconst, initid,
// linkage, alignment, section, visibility, threadlocal]
Vals.push_back(VE.getTypeID(GV->getType()));
Vals.push_back(GV->isConstant());
Vals.push_back(GV->isDeclaration() ? 0 :
(VE.getValueID(GV->getInitializer()) + 1));
Vals.push_back(getEncodedLinkage(GV));
Vals.push_back(Log2_32(GV->getAlignment())+1);
Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0);
Vals.push_back(getEncodedVisibility(GV));
Vals.push_back(GV->isThreadLocal());
unsigned AbbrevToUse = 0;
Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
Vals.clear();
}
// Emit the function proto information.
for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
// FUNCTION: [type, callingconv, isproto, linkage, alignment, section,
// visibility]
Vals.push_back(VE.getTypeID(F->getType()));
Vals.push_back(F->getCallingConv());
Vals.push_back(F->isDeclaration());
Vals.push_back(getEncodedLinkage(F));
Vals.push_back(Log2_32(F->getAlignment())+1);
Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0);
Vals.push_back(getEncodedVisibility(F));
unsigned AbbrevToUse = 0;
Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
Vals.clear();
}
}
/// WriteModule - Emit the specified module to the bitstream.
static void WriteModule(const Module *M, BitstreamWriter &Stream) {
Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 2);
// Emit the version number if it is non-zero.
if (CurVersion) {
SmallVector<unsigned, 1> VersionVals;
VersionVals.push_back(CurVersion);
Stream.EmitRecord(bitc::MODULE_CODE_VERSION, VersionVals);
}
// Analyze the module, enumerating globals, functions, etc.
ValueEnumerator VE(M);
// Emit information describing all of the types in the module.
WriteTypeTable(VE, Stream);
// FIXME: Emit constants.
// Emit top-level description of module, including target triple, inline asm,
// descriptors for global variables, and function prototype info.
WriteModuleInfo(M, VE, Stream);
// Emit the type symbol table information.
WriteTypeSymbolTable(M->getTypeSymbolTable(), VE, Stream);
Stream.ExitBlock();
}
/// WriteBitcodeToFile - Write the specified module to the specified output
/// stream.
void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) {
std::vector<unsigned char> Buffer;
BitstreamWriter Stream(Buffer);
Buffer.reserve(256*1024);
// Emit the file header.
Stream.Emit((unsigned)'B', 8);
Stream.Emit((unsigned)'C', 8);
Stream.Emit(0x0, 4);
Stream.Emit(0xC, 4);
Stream.Emit(0xE, 4);
Stream.Emit(0xD, 4);
// Emit the module.
WriteModule(M, Stream);
// Write the generated bitstream to "Out".
Out.write((char*)&Buffer.front(), Buffer.size());
}

View File

@ -0,0 +1,15 @@
##===- lib/Bitcode/Reader/Makefile -------------------------*- Makefile -*-===##
#
# The LLVM Compiler Infrastructure
#
# This file was developed by Chris Lattner and is distributed under
# the University of Illinois Open Source License. See LICENSE.TXT for details.
#
##===----------------------------------------------------------------------===##
LEVEL = ../../..
LIBRARYNAME = LLVMBitWriter
BUILD_ARCHIVE = 1
include $(LEVEL)/Makefile.common

View File

@ -0,0 +1,235 @@
//===-- ValueEnumerator.cpp - Number values and types for bitcode writer --===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by Chris Lattner and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the ValueEnumerator class.
//
//===----------------------------------------------------------------------===//
#include "ValueEnumerator.h"
#include "llvm/Module.h"
#include "llvm/TypeSymbolTable.h"
#include "llvm/ValueSymbolTable.h"
using namespace llvm;
/// ValueEnumerator - Enumerate module-level information.
ValueEnumerator::ValueEnumerator(const Module *M) {
// Enumerate the global variables.
for (Module::const_global_iterator I = M->global_begin(),
E = M->global_end(); I != E; ++I)
EnumerateValue(I);
// Enumerate the functions.
for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
EnumerateValue(I);
// Enumerate the global variable initializers.
for (Module::const_global_iterator I = M->global_begin(),
E = M->global_end(); I != E; ++I)
if (I->hasInitializer())
EnumerateValue(I->getInitializer());
// FIXME: Implement the 'string constant' optimization.
// Enumerate types used by the type symbol table.
EnumerateTypeSymbolTable(M->getTypeSymbolTable());
// Insert constants that are named at module level into the slot pool so that
// the module symbol table can refer to them...
EnumerateValueSymbolTable(M->getValueSymbolTable());
// Enumerate types used by function bodies.
for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){
for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
OI != E; ++OI)
EnumerateType((*OI)->getType());
EnumerateType(I->getType());
}
}
// FIXME: std::partition the type and value tables so that first-class types
// come earlier than aggregates.
// FIXME: Sort type/value tables by frequency.
}
/// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol
/// table.
void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) {
for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
TI != TE; ++TI)
EnumerateType(TI->second);
}
/// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
/// table into the values table.
void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
VI != VE; ++VI)
EnumerateValue(VI->getValue());
}
void ValueEnumerator::EnumerateValue(const Value *V) {
assert(V->getType() != Type::VoidTy && "Can't insert void values!");
// Check to see if it's already in!
unsigned &ValueID = ValueMap[V];
if (ValueID) {
// Increment use count.
Values[ValueID-1].second++;
return;
}
// Add the value.
Values.push_back(std::make_pair(V, 1U));
ValueID = Values.size();
if (const Constant *C = dyn_cast<Constant>(V)) {
if (isa<GlobalValue>(C)) {
// Initializers for globals are handled explicitly elsewhere.
} else {
// 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)
EnumerateValue(*I);
}
}
EnumerateType(V->getType());
}
void ValueEnumerator::EnumerateType(const Type *Ty) {
unsigned &TypeID = TypeMap[Ty];
if (TypeID) {
// If we've already seen this type, just increase its occurrence count.
Types[TypeID-1].second++;
return;
}
// First time we saw this type, add it.
Types.push_back(std::make_pair(Ty, 1U));
TypeID = Types.size();
// Enumerate subtypes.
for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
I != E; ++I)
EnumerateType(*I);
}
#if 0
void SlotCalculator::incorporateFunction(const Function *F) {
SC_DEBUG("begin processFunction!\n");
// Iterate over function arguments, adding them to the value table...
for(Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
I != E; ++I)
CreateFunctionValueSlot(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) {
CreateFunctionValueSlot(BB);
for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
if (I->getType() != Type::VoidTy)
CreateFunctionValueSlot(I);
}
}
SC_DEBUG("end processFunction!\n");
}
void SlotCalculator::purgeFunction() {
SC_DEBUG("begin purgeFunction!\n");
// Next, remove values from existing type planes
for (DenseMap<unsigned,unsigned,
ModuleLevelDenseMapKeyInfo>::iterator I = ModuleLevel.begin(),
E = ModuleLevel.end(); I != E; ++I) {
unsigned PlaneNo = I->first;
unsigned ModuleLev = I->second;
// Pop all function-local values in this type-plane off of Table.
TypePlane &Plane = getPlane(PlaneNo);
assert(ModuleLev < Plane.size() && "module levels higher than elements?");
for (unsigned i = ModuleLev, e = Plane.size(); i != e; ++i) {
NodeMap.erase(Plane.back()); // Erase from nodemap
Plane.pop_back(); // Shrink plane
}
}
ModuleLevel.clear();
// Finally, remove any type planes defined by the function...
while (Table.size() > NumModuleTypes) {
TypePlane &Plane = Table.back();
SC_DEBUG("Removing Plane " << (Table.size()-1) << " of size "
<< Plane.size() << "\n");
for (unsigned i = 0, e = Plane.size(); i != e; ++i)
NodeMap.erase(Plane[i]); // Erase from nodemap
Table.pop_back(); // Nuke the plane, we don't like it.
}
SC_DEBUG("end purgeFunction!\n");
}
inline static bool hasImplicitNull(const Type* Ty) {
return Ty != Type::LabelTy && Ty != Type::VoidTy && !isa<OpaqueType>(Ty);
}
void SlotCalculator::CreateFunctionValueSlot(const Value *V) {
assert(!NodeMap.count(V) && "Function-local value can't be inserted!");
const Type *Ty = V->getType();
assert(Ty != Type::VoidTy && "Can't insert void values!");
assert(!isa<Constant>(V) && "Not a function-local value!");
unsigned TyPlane = getOrCreateTypeSlot(Ty);
if (Table.size() <= TyPlane) // Make sure we have the type plane allocated.
Table.resize(TyPlane+1, TypePlane());
// If this is the first value noticed of this type within this function,
// remember the module level for this type plane in ModuleLevel. This reminds
// us to remove the values in purgeFunction and tells us how many to remove.
if (TyPlane < NumModuleTypes)
ModuleLevel.insert(std::make_pair(TyPlane, Table[TyPlane].size()));
// If this is the first value to get inserted into the type plane, make sure
// to insert the implicit null value.
if (Table[TyPlane].empty()) {
// Label's and opaque types can't have a null value.
if (hasImplicitNull(Ty)) {
Value *ZeroInitializer = Constant::getNullValue(Ty);
// If we are pushing zeroinit, it will be handled below.
if (V != ZeroInitializer) {
Table[TyPlane].push_back(ZeroInitializer);
NodeMap[ZeroInitializer] = 0;
}
}
}
// Insert node into table and NodeMap...
NodeMap[V] = Table[TyPlane].size();
Table[TyPlane].push_back(V);
SC_DEBUG(" Inserting value [" << TyPlane << "] = " << *V << " slot=" <<
NodeMap[V] << "\n");
}
#endif

View File

@ -0,0 +1,85 @@
//===-- Bitcode/Writer/ValueEnumerator.h - Number values --------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by Chris Lattner and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This class gives values and types Unique ID's.
//
//===----------------------------------------------------------------------===//
#ifndef VALUE_ENUMERATOR_H
#define VALUE_ENUMERATOR_H
#include "llvm/ADT/DenseMap.h"
#include <vector>
namespace llvm {
class Value;
class Type;
class Module;
class Function;
class TypeSymbolTable;
class ValueSymbolTable;
class ConstantArray;
class ValueEnumerator {
public:
// For each type, we remember its Type* and occurrence frequency.
typedef std::vector<std::pair<const Type*, unsigned> > TypeList;
// For each value, we remember its Value* and occurrence frequency.
typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
private:
TypeList Types;
typedef DenseMap<const Type*, unsigned> TypeMapType;
TypeMapType TypeMap;
ValueList Values;
typedef DenseMap<const Value*, unsigned> ValueMapType;
ValueMapType ValueMap;
ValueEnumerator(const ValueEnumerator &); // DO NOT IMPLEMENT
void operator=(const ValueEnumerator &); // DO NOT IMPLEMENT
public:
ValueEnumerator(const Module *M);
unsigned getValueID(const Value *V) const {
ValueMapType::const_iterator I = ValueMap.find(V);
assert(I != ValueMap.end() && "Value not in slotcalculator!");
return I->second;
}
unsigned getTypeID(const Type *T) const {
TypeMapType::const_iterator I = TypeMap.find(T);
assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
return I->second-1;
}
const TypeList &getTypes() const { return Types; }
/// incorporateFunction/purgeFunction - If you'd like to deal with a function,
/// use these two methods to get its data into the ValueEnumerator!
///
void incorporateFunction(const Function *F);
void purgeFunction();
private:
void EnumerateValue(const Value *V);
void EnumerateType(const Type *T);
void EnumerateTypeSymbolTable(const TypeSymbolTable &ST);
void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
};
} // End llvm namespace
#endif