#include "llvm/ValueSymbolTable.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
-#include "llvm/Support/Streams.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/System/Program.h"
using namespace llvm;
if (const OverflowingBinaryOperator *OBO =
dyn_cast<OverflowingBinaryOperator>(V)) {
- if (OBO->hasNoSignedOverflow())
- Flags |= 1 << bitc::OBO_NO_SIGNED_OVERFLOW;
- if (OBO->hasNoUnsignedOverflow())
- Flags |= 1 << bitc::OBO_NO_UNSIGNED_OVERFLOW;
+ if (OBO->hasNoSignedWrap())
+ Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
+ if (OBO->hasNoUnsignedWrap())
+ Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
} else if (const SDivOperator *Div = dyn_cast<SDivOperator>(V)) {
if (Div->isExact())
Flags |= 1 << bitc::SDIV_EXACT;
return Flags;
}
-/// WriteValues - Write Constants and Metadata.
-/// This function could use some refactoring help.
-static void WriteValues(unsigned FirstVal, unsigned LastVal,
+static void WriteMDNode(const MDNode *N,
const ValueEnumerator &VE,
- BitstreamWriter &Stream, bool isGlobal) {
- if (FirstVal == LastVal) return;
-
- // MODULE_BLOCK_ID is 0, which is not handled here. So it is OK to use
- // 0 as the initializer to indicate that block is not set.
- enum bitc::BlockIDs LastBlockID = bitc::MODULE_BLOCK_ID;
+ BitstreamWriter &Stream,
+ SmallVector<uint64_t, 64> &Record) {
+ for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {
+ if (N->getElement(i)) {
+ Record.push_back(VE.getTypeID(N->getElement(i)->getType()));
+ Record.push_back(VE.getValueID(N->getElement(i)));
+ } else {
+ Record.push_back(VE.getTypeID(Type::getVoidTy(N->getContext())));
+ Record.push_back(0);
+ }
+ }
+ Stream.EmitRecord(bitc::METADATA_NODE, Record, 0);
+ Record.clear();
+}
- unsigned AggregateAbbrev = 0;
- unsigned String8Abbrev = 0;
- unsigned CString7Abbrev = 0;
- unsigned CString6Abbrev = 0;
+static void WriteModuleMetadata(const ValueEnumerator &VE,
+ BitstreamWriter &Stream) {
+ const ValueEnumerator::ValueList &Vals = VE.getMDValues();
+ bool StartedMetadataBlock = false;
unsigned MDSAbbrev = 0;
-
SmallVector<uint64_t, 64> Record;
-
- const ValueEnumerator::ValueList &Vals = VE.getValues();
- const Type *LastTy = 0;
- for (unsigned i = FirstVal; i != LastVal; ++i) {
- const Value *V = Vals[i].first;
- if (isa<MetadataBase>(V)) {
- if (LastBlockID != bitc::METADATA_BLOCK_ID) {
- // Exit privious block.
- if (LastBlockID != bitc::MODULE_BLOCK_ID)
- Stream.ExitBlock();
-
- LastBlockID = bitc::METADATA_BLOCK_ID;
+ for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
+
+ if (const MDNode *N = dyn_cast<MDNode>(Vals[i].first)) {
+ if (!StartedMetadataBlock) {
Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
+ StartedMetadataBlock = true;
+ }
+ WriteMDNode(N, VE, Stream, Record);
+ } else if (const MDString *MDS = dyn_cast<MDString>(Vals[i].first)) {
+ if (!StartedMetadataBlock) {
+ Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
+
// Abbrev for METADATA_STRING.
BitCodeAbbrev *Abbv = new BitCodeAbbrev();
Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING));
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
MDSAbbrev = Stream.EmitAbbrev(Abbv);
+ StartedMetadataBlock = true;
}
- }
- if (const MDString *MDS = dyn_cast<MDString>(V)) {
+
// Code: [strchar x N]
const char *StrBegin = MDS->begin();
for (unsigned i = 0, e = MDS->length(); i != e; ++i)
// Emit the finished record.
Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev);
Record.clear();
- continue;
- } else if (const MDNode *N = dyn_cast<MDNode>(V)) {
- for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {
- if (N->getElement(i)) {
- Record.push_back(VE.getTypeID(N->getElement(i)->getType()));
- Record.push_back(VE.getValueID(N->getElement(i)));
- } else {
- Record.push_back(VE.getTypeID(Type::VoidTy));
- Record.push_back(0);
- }
+ } else if (const NamedMDNode *NMD = dyn_cast<NamedMDNode>(Vals[i].first)) {
+ if (!StartedMetadataBlock) {
+ Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
+ StartedMetadataBlock = true;
}
- Stream.EmitRecord(bitc::METADATA_NODE, Record, 0);
- Record.clear();
- continue;
- } else if (const NamedMDNode *NMD = dyn_cast<NamedMDNode>(V)) {
+
// Write name.
std::string Str = NMD->getNameStr();
const char *StrBegin = Str.c_str();
Record.push_back(StrBegin[i]);
Stream.EmitRecord(bitc::METADATA_NAME, Record, 0/*TODO*/);
Record.clear();
-
+
// Write named metadata elements.
for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
if (NMD->getElement(i))
}
Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
Record.clear();
- continue;
}
+ }
+
+ if (StartedMetadataBlock)
+ Stream.ExitBlock();
+}
- // If we need to switch block, do so now.
- if (LastBlockID != bitc::CONSTANTS_BLOCK_ID) {
- // Exit privious block.
- if (LastBlockID != bitc::MODULE_BLOCK_ID)
- Stream.ExitBlock();
-
- LastBlockID = bitc::CONSTANTS_BLOCK_ID;
- Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
- // If this is a constant pool for the module, emit module-specific abbrevs.
- if (isGlobal) {
- // Abbrev for CST_CODE_AGGREGATE.
- BitCodeAbbrev *Abbv = new BitCodeAbbrev();
- Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
- Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
- Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
- AggregateAbbrev = Stream.EmitAbbrev(Abbv);
-
- // Abbrev for CST_CODE_STRING.
- Abbv = new BitCodeAbbrev();
- Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
- Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
- Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
- String8Abbrev = Stream.EmitAbbrev(Abbv);
+static void WriteConstants(unsigned FirstVal, unsigned LastVal,
+ const ValueEnumerator &VE,
+ BitstreamWriter &Stream, bool isGlobal) {
+ if (FirstVal == LastVal) return;
+
+ Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
- // Abbrev for CST_CODE_CSTRING.
- Abbv = new BitCodeAbbrev();
- Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
- Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
- Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
- CString7Abbrev = Stream.EmitAbbrev(Abbv);
+ unsigned AggregateAbbrev = 0;
+ unsigned String8Abbrev = 0;
+ unsigned CString7Abbrev = 0;
+ unsigned CString6Abbrev = 0;
+ // If this is a constant pool for the module, emit module-specific abbrevs.
+ if (isGlobal) {
+ // Abbrev for CST_CODE_AGGREGATE.
+ BitCodeAbbrev *Abbv = new BitCodeAbbrev();
+ Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
+ AggregateAbbrev = Stream.EmitAbbrev(Abbv);
- // Abbrev for CST_CODE_CSTRING.
- Abbv = new BitCodeAbbrev();
- Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
- Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
- Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
- CString6Abbrev = Stream.EmitAbbrev(Abbv);
- }
+ // Abbrev for CST_CODE_STRING.
+ Abbv = new BitCodeAbbrev();
+ Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
+ String8Abbrev = Stream.EmitAbbrev(Abbv);
+ // Abbrev for CST_CODE_CSTRING.
+ Abbv = new BitCodeAbbrev();
+ Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
+ CString7Abbrev = Stream.EmitAbbrev(Abbv);
+ // Abbrev for CST_CODE_CSTRING.
+ Abbv = new BitCodeAbbrev();
+ Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
+ CString6Abbrev = Stream.EmitAbbrev(Abbv);
+ }
+
+ SmallVector<uint64_t, 64> Record;
- }
+ const ValueEnumerator::ValueList &Vals = VE.getValues();
+ const Type *LastTy = 0;
+ for (unsigned i = FirstVal; i != LastVal; ++i) {
+ const Value *V = Vals[i].first;
// If we need to switch types, do so now.
if (V->getType() != LastTy) {
LastTy = V->getType();
} else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
Code = bitc::CST_CODE_FLOAT;
const Type *Ty = CFP->getType();
- if (Ty == Type::FloatTy || Ty == Type::DoubleTy) {
+ if (Ty == Type::getFloatTy(Ty->getContext()) ||
+ Ty == Type::getDoubleTy(Ty->getContext())) {
Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
- } else if (Ty == Type::X86_FP80Ty) {
+ } else if (Ty == Type::getX86_FP80Ty(Ty->getContext())) {
// api needed to prevent premature destruction
// bits are not in the same order as a normal i80 APInt, compensate.
APInt api = CFP->getValueAPF().bitcastToAPInt();
const uint64_t *p = api.getRawData();
Record.push_back((p[1] << 48) | (p[0] >> 16));
Record.push_back(p[0] & 0xffffLL);
- } else if (Ty == Type::FP128Ty || Ty == Type::PPC_FP128Ty) {
+ } else if (Ty == Type::getFP128Ty(Ty->getContext()) ||
+ Ty == Type::getPPC_FP128Ty(Ty->getContext())) {
APInt api = CFP->getValueAPF().bitcastToAPInt();
const uint64_t *p = api.getRawData();
Record.push_back(p[0]);
// We know globalvalues have been emitted by WriteModuleInfo.
for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
if (!isa<GlobalValue>(Vals[i].first)) {
- WriteValues(i, Vals.size(), VE, Stream, true);
+ WriteConstants(i, Vals.size(), VE, Stream, true);
return;
}
}
// VST_ENTRY: [valueid, namechar x N]
// VST_BBENTRY: [bbid, namechar x N]
unsigned Code;
- if (isa<BasicBlock>(*SI->getValue())) {
+ if (isa<BasicBlock>(SI->getValue())) {
Code = bitc::VST_CODE_BBENTRY;
if (isChar6)
AbbrevToUse = VST_BBENTRY_6_ABBREV;
// If there are function-local constants, emit them now.
unsigned CstStart, CstEnd;
VE.getFunctionConstantRange(CstStart, CstEnd);
- WriteValues(CstStart, CstEnd, VE, Stream, false);
+ WriteConstants(CstStart, CstEnd, VE, Stream, false);
// Keep a running idea of what the instruction ID is.
unsigned InstID = CstEnd;
for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
I != E; ++I) {
WriteInstruction(*I, InstID, VE, Stream, Vals);
- if (I->getType() != Type::VoidTy)
+ if (I->getType() != Type::getVoidTy(F.getContext()))
++InstID;
}
// Emit constants.
WriteModuleConstants(VE, Stream);
+ // Emit metadata.
+ WriteModuleMetadata(VE, Stream);
+
// Emit function bodies.
for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
if (!I->isDeclaration())
// Emit names for globals/functions etc.
WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
-
+
Stream.ExitBlock();
}
}
-/// WriteBitcodeToFile - Write the specified module to the specified output
-/// stream.
-void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) {
- raw_os_ostream RawOut(Out);
- // If writing to stdout, set binary mode.
- if (llvm::cout == Out)
- sys::Program::ChangeStdoutToBinary();
- WriteBitcodeToFile(M, RawOut);
-}
-
/// WriteBitcodeToFile - Write the specified module to the specified output
/// stream.
void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out) {