#include "llvm/DerivedTypes.h"
#include "llvm/InlineAsm.h"
#include "llvm/Instructions.h"
+#include "llvm/LLVMContext.h"
+#include "llvm/MDNode.h"
#include "llvm/Module.h"
+#include "llvm/Operator.h"
#include "llvm/AutoUpgrade.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
static GlobalValue::LinkageTypes GetDecodedLinkage(unsigned Val) {
switch (Val) {
default: // Map unknown/new linkages to external
- case 0: return GlobalValue::ExternalLinkage;
- case 1: return GlobalValue::WeakAnyLinkage;
- case 2: return GlobalValue::AppendingLinkage;
- case 3: return GlobalValue::InternalLinkage;
- case 4: return GlobalValue::LinkOnceAnyLinkage;
- case 5: return GlobalValue::DLLImportLinkage;
- case 6: return GlobalValue::DLLExportLinkage;
- case 7: return GlobalValue::ExternalWeakLinkage;
- case 8: return GlobalValue::CommonLinkage;
- case 9: return GlobalValue::PrivateLinkage;
+ case 0: return GlobalValue::ExternalLinkage;
+ case 1: return GlobalValue::WeakAnyLinkage;
+ case 2: return GlobalValue::AppendingLinkage;
+ case 3: return GlobalValue::InternalLinkage;
+ case 4: return GlobalValue::LinkOnceAnyLinkage;
+ case 5: return GlobalValue::DLLImportLinkage;
+ case 6: return GlobalValue::DLLExportLinkage;
+ case 7: return GlobalValue::ExternalWeakLinkage;
+ case 8: return GlobalValue::CommonLinkage;
+ case 9: return GlobalValue::PrivateLinkage;
case 10: return GlobalValue::WeakODRLinkage;
case 11: return GlobalValue::LinkOnceODRLinkage;
case 12: return GlobalValue::AvailableExternallyLinkage;
+ case 13: return GlobalValue::LinkerPrivateLinkage;
}
}
static int GetDecodedBinaryOpcode(unsigned Val, const Type *Ty) {
switch (Val) {
default: return -1;
- case bitc::BINOP_ADD: return Instruction::Add;
- case bitc::BINOP_SUB: return Instruction::Sub;
- case bitc::BINOP_MUL: return Instruction::Mul;
+ case bitc::BINOP_ADD:
+ return Ty->isFPOrFPVector() ? Instruction::FAdd : Instruction::Add;
+ case bitc::BINOP_SUB:
+ return Ty->isFPOrFPVector() ? Instruction::FSub : Instruction::Sub;
+ case bitc::BINOP_MUL:
+ return Ty->isFPOrFPVector() ? Instruction::FMul : Instruction::Mul;
case bitc::BINOP_UDIV: return Instruction::UDiv;
case bitc::BINOP_SDIV:
return Ty->isFPOrFPVector() ? Instruction::FDiv : Instruction::SDiv;
void *operator new(size_t s) {
return User::operator new(s, 1);
}
- explicit ConstantPlaceHolder(const Type *Ty)
+ explicit ConstantPlaceHolder(const Type *Ty, LLVMContext& Context)
: ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) {
- Op<0>() = UndefValue::get(Type::Int32Ty);
+ Op<0>() = Context.getUndef(Type::Int32Ty);
}
/// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
}
// Create and return a placeholder, which will later be RAUW'd.
- Constant *C = new ConstantPlaceHolder(Ty);
+ Constant *C = new ConstantPlaceHolder(Ty, Context);
ValuePtrs[Idx] = C;
return C;
}
// Make the new constant.
Constant *NewC;
if (ConstantArray *UserCA = dyn_cast<ConstantArray>(UserC)) {
- NewC = ConstantArray::get(UserCA->getType(), &NewOps[0], NewOps.size());
+ NewC = Context.getConstantArray(UserCA->getType(), &NewOps[0],
+ NewOps.size());
} else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) {
- NewC = ConstantStruct::get(&NewOps[0], NewOps.size(),
- UserCS->getType()->isPacked());
+ NewC = Context.getConstantStruct(&NewOps[0], NewOps.size(),
+ UserCS->getType()->isPacked());
} else if (isa<ConstantVector>(UserC)) {
- NewC = ConstantVector::get(&NewOps[0], NewOps.size());
- } else if (isa<ConstantExpr>(UserC)) {
+ NewC = Context.getConstantVector(&NewOps[0], NewOps.size());
+ } else {
+ assert(isa<ConstantExpr>(UserC) && "Must be a ConstantExpr.");
NewC = cast<ConstantExpr>(UserC)->getWithOperands(&NewOps[0],
NewOps.size());
- } else {
- assert(isa<MDNode>(UserC) && "Must be a metadata node.");
- NewC = MDNode::get(&NewOps[0], NewOps.size());
}
UserC->replaceAllUsesWith(NewC);
NewOps.clear();
}
+ // Update all ValueHandles, they should be the only users at this point.
+ Placeholder->replaceAllUsesWith(RealVal);
delete Placeholder;
}
}
// The type table allows forward references. Push as many Opaque types as
// needed to get up to ID.
while (TypeList.size() <= ID)
- TypeList.push_back(OpaqueType::get());
+ TypeList.push_back(Context.getOpaqueType());
return TypeList.back().get();
}
case bitc::TYPE_CODE_OPAQUE: // OPAQUE
ResultTy = 0;
break;
+ case bitc::TYPE_CODE_METADATA: // METADATA
+ ResultTy = Type::MetadataTy;
+ break;
case bitc::TYPE_CODE_INTEGER: // INTEGER: [width]
if (Record.size() < 1)
return Error("Invalid Integer type record");
- ResultTy = IntegerType::get(Record[0]);
+ ResultTy = Context.getIntegerType(Record[0]);
break;
case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or
// [pointee type, address space]
unsigned AddressSpace = 0;
if (Record.size() == 2)
AddressSpace = Record[1];
- ResultTy = PointerType::get(getTypeByID(Record[0], true), AddressSpace);
+ ResultTy = Context.getPointerType(getTypeByID(Record[0], true),
+ AddressSpace);
break;
}
case bitc::TYPE_CODE_FUNCTION: {
for (unsigned i = 3, e = Record.size(); i != e; ++i)
ArgTys.push_back(getTypeByID(Record[i], true));
- ResultTy = FunctionType::get(getTypeByID(Record[2], true), ArgTys,
+ ResultTy = Context.getFunctionType(getTypeByID(Record[2], true), ArgTys,
Record[0]);
break;
}
std::vector<const Type*> EltTys;
for (unsigned i = 1, e = Record.size(); i != e; ++i)
EltTys.push_back(getTypeByID(Record[i], true));
- ResultTy = StructType::get(EltTys, Record[0]);
+ ResultTy = Context.getStructType(EltTys, Record[0]);
break;
}
case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty]
if (Record.size() < 2)
return Error("Invalid ARRAY type record");
- ResultTy = ArrayType::get(getTypeByID(Record[1], true), Record[0]);
+ ResultTy = Context.getArrayType(getTypeByID(Record[1], true), Record[0]);
break;
case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty]
if (Record.size() < 2)
return Error("Invalid VECTOR type record");
- ResultTy = VectorType::get(getTypeByID(Record[1], true), Record[0]);
+ ResultTy = Context.getVectorType(getTypeByID(Record[1], true), Record[0]);
break;
}
if (NumRecords == TypeList.size()) {
// If this is a new type slot, just append it.
- TypeList.push_back(ResultTy ? ResultTy : OpaqueType::get());
+ TypeList.push_back(ResultTy ? ResultTy : Context.getOpaqueType());
++NumRecords;
} else if (ResultTy == 0) {
// Otherwise, this was forward referenced, so an opaque type was created,
break;
case bitc::VST_CODE_ENTRY: { // VST_ENTRY: [valueid, namechar x N]
if (ConvertToString(Record, 1, ValueName))
- return Error("Invalid TST_ENTRY record");
+ return Error("Invalid VST_ENTRY record");
unsigned ValueID = Record[0];
if (ValueID >= ValueList.size())
return Error("Invalid Value ID in VST_ENTRY record");
}
}
+bool BitcodeReader::ParseMetadata() {
+ unsigned NextValueNo = ValueList.size();
+
+ if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
+ return Error("Malformed block record");
+
+ SmallVector<uint64_t, 64> Record;
+
+ // Read all the records.
+ while (1) {
+ unsigned Code = Stream.ReadCode();
+ if (Code == bitc::END_BLOCK) {
+ if (Stream.ReadBlockEnd())
+ return Error("Error at end of PARAMATTR block");
+ return false;
+ }
+
+ if (Code == bitc::ENTER_SUBBLOCK) {
+ // No known subblocks, always skip them.
+ Stream.ReadSubBlockID();
+ if (Stream.SkipBlock())
+ return Error("Malformed block record");
+ continue;
+ }
+
+ if (Code == bitc::DEFINE_ABBREV) {
+ Stream.ReadAbbrevRecord();
+ continue;
+ }
+
+ // Read a record.
+ Record.clear();
+ switch (Stream.ReadRecord(Code, Record)) {
+ default: // Default behavior: ignore.
+ break;
+ case bitc::METADATA_NODE: {
+ if (Record.empty() || Record.size() % 2 == 1)
+ return Error("Invalid METADATA_NODE record");
+
+ unsigned Size = Record.size();
+ SmallVector<Value*, 8> Elts;
+ for (unsigned i = 0; i != Size; i += 2) {
+ const Type *Ty = getTypeByID(Record[i], false);
+ if (Ty != Type::VoidTy)
+ Elts.push_back(ValueList.getValueFwdRef(Record[i+1], Ty));
+ else
+ Elts.push_back(NULL);
+ }
+ Value *V = Context.getMDNode(&Elts[0], Elts.size());
+ ValueList.AssignValue(V, NextValueNo++);
+ break;
+ }
+ case bitc::METADATA_STRING: {
+ unsigned MDStringLength = Record.size();
+ SmallString<8> String;
+ String.resize(MDStringLength);
+ for (unsigned i = 0; i != MDStringLength; ++i)
+ String[i] = Record[i];
+ Value *V =
+ Context.getMDString(String.c_str(), String.c_str() + MDStringLength);
+ ValueList.AssignValue(V, NextValueNo++);
+ break;
+ }
+ }
+ }
+}
+
/// DecodeSignRotatedValue - Decode a signed value stored with the sign bit in
/// the LSB for dense VBR encoding.
static uint64_t DecodeSignRotatedValue(uint64_t V) {
return false;
}
+static void SetOptimizationFlags(Value *V, uint64_t Flags) {
+ if (OverflowingBinaryOperator *OBO =
+ dyn_cast<OverflowingBinaryOperator>(V)) {
+ if (Flags & (1 << bitc::OBO_NO_SIGNED_OVERFLOW))
+ OBO->setHasNoSignedOverflow(true);
+ if (Flags & (1 << bitc::OBO_NO_UNSIGNED_OVERFLOW))
+ OBO->setHasNoUnsignedOverflow(true);
+ } else if (SDivOperator *Div = dyn_cast<SDivOperator>(V)) {
+ if (Flags & (1 << bitc::SDIV_EXACT))
+ Div->setIsExact(true);
+ }
+}
bool BitcodeReader::ParseConstants() {
if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
// Read a record.
Record.clear();
Value *V = 0;
- switch (Stream.ReadRecord(Code, Record)) {
+ unsigned BitCode = Stream.ReadRecord(Code, Record);
+ switch (BitCode) {
default: // Default behavior: unknown constant
case bitc::CST_CODE_UNDEF: // UNDEF
- V = UndefValue::get(CurTy);
+ V = Context.getUndef(CurTy);
break;
case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid]
if (Record.empty())
CurTy = TypeList[Record[0]];
continue; // Skip the ValueList manipulation.
case bitc::CST_CODE_NULL: // NULL
- V = Constant::getNullValue(CurTy);
+ V = Context.getNullValue(CurTy);
break;
case bitc::CST_CODE_INTEGER: // INTEGER: [intval]
if (!isa<IntegerType>(CurTy) || Record.empty())
return Error("Invalid CST_INTEGER record");
- V = ConstantInt::get(CurTy, DecodeSignRotatedValue(Record[0]));
+ V = Context.getConstantInt(CurTy, DecodeSignRotatedValue(Record[0]));
break;
case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
if (!isa<IntegerType>(CurTy) || Record.empty())
Words.resize(NumWords);
for (unsigned i = 0; i != NumWords; ++i)
Words[i] = DecodeSignRotatedValue(Record[i]);
- V = ConstantInt::get(APInt(cast<IntegerType>(CurTy)->getBitWidth(),
+ V = Context.getConstantInt(APInt(cast<IntegerType>(CurTy)->getBitWidth(),
NumWords, &Words[0]));
break;
}
if (Record.empty())
return Error("Invalid FLOAT record");
if (CurTy == Type::FloatTy)
- V = ConstantFP::get(APFloat(APInt(32, (uint32_t)Record[0])));
+ V = Context.getConstantFP(APFloat(APInt(32, (uint32_t)Record[0])));
else if (CurTy == Type::DoubleTy)
- V = ConstantFP::get(APFloat(APInt(64, Record[0])));
+ V = Context.getConstantFP(APFloat(APInt(64, Record[0])));
else if (CurTy == Type::X86_FP80Ty) {
// Bits are not stored the same way as a normal i80 APInt, compensate.
uint64_t Rearrange[2];
Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);
Rearrange[1] = Record[0] >> 48;
- V = ConstantFP::get(APFloat(APInt(80, 2, Rearrange)));
+ V = Context.getConstantFP(APFloat(APInt(80, 2, Rearrange)));
} else if (CurTy == Type::FP128Ty)
- V = ConstantFP::get(APFloat(APInt(128, 2, &Record[0]), true));
+ V = Context.getConstantFP(APFloat(APInt(128, 2, &Record[0]), true));
else if (CurTy == Type::PPC_FP128Ty)
- V = ConstantFP::get(APFloat(APInt(128, 2, &Record[0])));
+ V = Context.getConstantFP(APFloat(APInt(128, 2, &Record[0])));
else
- V = UndefValue::get(CurTy);
+ V = Context.getUndef(CurTy);
break;
}
for (unsigned i = 0; i != Size; ++i)
Elts.push_back(ValueList.getConstantFwdRef(Record[i],
STy->getElementType(i)));
- V = ConstantStruct::get(STy, Elts);
+ V = Context.getConstantStruct(STy, Elts);
} else if (const ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) {
const Type *EltTy = ATy->getElementType();
for (unsigned i = 0; i != Size; ++i)
Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
- V = ConstantArray::get(ATy, Elts);
+ V = Context.getConstantArray(ATy, Elts);
} else if (const VectorType *VTy = dyn_cast<VectorType>(CurTy)) {
const Type *EltTy = VTy->getElementType();
for (unsigned i = 0; i != Size; ++i)
Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
- V = ConstantVector::get(Elts);
+ V = Context.getConstantVector(Elts);
} else {
- V = UndefValue::get(CurTy);
+ V = Context.getUndef(CurTy);
}
break;
}
unsigned Size = Record.size();
std::vector<Constant*> Elts;
for (unsigned i = 0; i != Size; ++i)
- Elts.push_back(ConstantInt::get(EltTy, Record[i]));
- V = ConstantArray::get(ATy, Elts);
+ Elts.push_back(Context.getConstantInt(EltTy, Record[i]));
+ V = Context.getConstantArray(ATy, Elts);
break;
}
case bitc::CST_CODE_CSTRING: { // CSTRING: [values]
unsigned Size = Record.size();
std::vector<Constant*> Elts;
for (unsigned i = 0; i != Size; ++i)
- Elts.push_back(ConstantInt::get(EltTy, Record[i]));
- Elts.push_back(Constant::getNullValue(EltTy));
- V = ConstantArray::get(ATy, Elts);
+ Elts.push_back(Context.getConstantInt(EltTy, Record[i]));
+ Elts.push_back(Context.getNullValue(EltTy));
+ V = Context.getConstantArray(ATy, Elts);
break;
}
case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval]
if (Record.size() < 3) return Error("Invalid CE_BINOP record");
int Opc = GetDecodedBinaryOpcode(Record[0], CurTy);
if (Opc < 0) {
- V = UndefValue::get(CurTy); // Unknown binop.
+ V = Context.getUndef(CurTy); // Unknown binop.
} else {
Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy);
Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy);
- V = ConstantExpr::get(Opc, LHS, RHS);
+ V = Context.getConstantExpr(Opc, LHS, RHS);
}
+ if (Record.size() >= 4)
+ SetOptimizationFlags(V, Record[3]);
break;
}
case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval]
if (Record.size() < 3) return Error("Invalid CE_CAST record");
int Opc = GetDecodedCastOpcode(Record[0]);
if (Opc < 0) {
- V = UndefValue::get(CurTy); // Unknown cast.
+ V = Context.getUndef(CurTy); // Unknown cast.
} else {
const Type *OpTy = getTypeByID(Record[1]);
if (!OpTy) return Error("Invalid CE_CAST record");
Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy);
- V = ConstantExpr::getCast(Opc, Op, CurTy);
+ V = Context.getConstantExprCast(Opc, Op, CurTy);
}
break;
}
if (!ElTy) return Error("Invalid CE_GEP record");
Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], ElTy));
}
- V = ConstantExpr::getGetElementPtr(Elts[0], &Elts[1], Elts.size()-1);
+ V = Context.getConstantExprGetElementPtr(Elts[0], &Elts[1],
+ Elts.size()-1);
break;
}
case bitc::CST_CODE_CE_SELECT: // CE_SELECT: [opval#, opval#, opval#]
if (Record.size() < 3) return Error("Invalid CE_SELECT record");
- V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0],
+ V = Context.getConstantExprSelect(ValueList.getConstantFwdRef(Record[0],
Type::Int1Ty),
ValueList.getConstantFwdRef(Record[1],CurTy),
ValueList.getConstantFwdRef(Record[2],CurTy));
if (OpTy == 0) return Error("Invalid CE_EXTRACTELT record");
Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
Constant *Op1 = ValueList.getConstantFwdRef(Record[2], Type::Int32Ty);
- V = ConstantExpr::getExtractElement(Op0, Op1);
+ V = Context.getConstantExprExtractElement(Op0, Op1);
break;
}
case bitc::CST_CODE_CE_INSERTELT: { // CE_INSERTELT: [opval, opval, opval]
Constant *Op1 = ValueList.getConstantFwdRef(Record[1],
OpTy->getElementType());
Constant *Op2 = ValueList.getConstantFwdRef(Record[2], Type::Int32Ty);
- V = ConstantExpr::getInsertElement(Op0, Op1, Op2);
+ V = Context.getConstantExprInsertElement(Op0, Op1, Op2);
break;
}
case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval]
return Error("Invalid CE_SHUFFLEVEC record");
Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy);
- const Type *ShufTy=VectorType::get(Type::Int32Ty, OpTy->getNumElements());
+ const Type *ShufTy = Context.getVectorType(Type::Int32Ty,
+ OpTy->getNumElements());
Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy);
- V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
+ V = Context.getConstantExprShuffleVector(Op0, Op1, Op2);
break;
}
case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval]
return Error("Invalid CE_SHUFVEC_EX record");
Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
- const Type *ShufTy=VectorType::get(Type::Int32Ty, RTy->getNumElements());
+ const Type *ShufTy = Context.getVectorType(Type::Int32Ty,
+ RTy->getNumElements());
Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy);
- V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
+ V = Context.getConstantExprShuffleVector(Op0, Op1, Op2);
break;
}
case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred]
Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
if (OpTy->isFloatingPoint())
- V = ConstantExpr::getFCmp(Record[3], Op0, Op1);
- else if (!isa<VectorType>(OpTy))
- V = ConstantExpr::getICmp(Record[3], Op0, Op1);
- else if (OpTy->isFPOrFPVector())
- V = ConstantExpr::getVFCmp(Record[3], Op0, Op1);
+ V = Context.getConstantExprFCmp(Record[3], Op0, Op1);
else
- V = ConstantExpr::getVICmp(Record[3], Op0, Op1);
+ V = Context.getConstantExprICmp(Record[3], Op0, Op1);
break;
}
case bitc::CST_CODE_INLINEASM: {
AsmStr, ConstrStr, HasSideEffects);
break;
}
- case bitc::CST_CODE_MDSTRING: {
- if (Record.size() < 2) return Error("Invalid MDSTRING record");
- unsigned MDStringLength = Record.size();
- SmallString<8> String;
- String.resize(MDStringLength);
- for (unsigned i = 0; i != MDStringLength; ++i)
- String[i] = Record[i];
- V = MDString::get(String.c_str(), String.c_str() + MDStringLength);
- break;
- }
- case bitc::CST_CODE_MDNODE: {
- if (Record.empty() || Record.size() % 2 == 1)
- return Error("Invalid CST_MDNODE record");
-
- unsigned Size = Record.size();
- SmallVector<Constant*, 8> Elts;
- for (unsigned i = 0; i != Size; i += 2) {
- const Type *Ty = getTypeByID(Record[i], false);
- Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], Ty));
- }
- V = MDNode::get(&Elts[0], Elts.size());
- break;
- }
}
ValueList.AssignValue(V, NextCstNo);
return Error("Malformed block record");
// Otherwise, create the module.
- TheModule = new Module(ModuleID);
+ TheModule = new Module(ModuleID, Context);
SmallVector<uint64_t, 64> Record;
std::vector<std::string> SectionTable;
if (ParseConstants() || ResolveGlobalAndAliasInits())
return true;
break;
+ case bitc::METADATA_BLOCK_ID:
+ if (ParseMetadata())
+ return true;
+ break;
case bitc::FUNCTION_BLOCK_ID:
// If this is the first function body we've seen, reverse the
// FunctionsWithBodies list.
isThreadLocal = Record[7];
GlobalVariable *NewGV =
- new GlobalVariable(Ty, isConstant, Linkage, 0, "", TheModule,
+ new GlobalVariable(*TheModule, Ty, isConstant, Linkage, 0, "", 0,
isThreadLocal, AddressSpace);
NewGV->setAlignment(Alignment);
if (!Section.empty())
if (SkipBitcodeWrapperHeader(BufPtr, BufEnd))
return Error("Invalid bitcode wrapper header");
- Stream.init(BufPtr, BufEnd);
+ StreamFile.init(BufPtr, BufEnd);
+ Stream.init(StreamFile);
// Sniff for the signature.
if (Stream.Read(8) != 'B' ||
// Read a record.
Record.clear();
Instruction *I = 0;
- switch (Stream.ReadRecord(Code, Record)) {
+ unsigned BitCode = Stream.ReadRecord(Code, Record);
+ switch (BitCode) {
default: // Default behavior: reject
return Error("Unknown instruction");
case bitc::FUNC_CODE_DECLAREBLOCKS: // DECLAREBLOCKS: [nblocks]
Value *LHS, *RHS;
if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
getValue(Record, OpNum, LHS->getType(), RHS) ||
- OpNum+1 != Record.size())
+ OpNum+1 > Record.size())
return Error("Invalid BINOP record");
- int Opc = GetDecodedBinaryOpcode(Record[OpNum], LHS->getType());
+ int Opc = GetDecodedBinaryOpcode(Record[OpNum++], LHS->getType());
if (Opc == -1) return Error("Invalid BINOP record");
I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
+ if (OpNum < Record.size())
+ SetOptimizationFlags(I, Record[3]);
break;
}
case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc]
break;
}
- case bitc::FUNC_CODE_INST_CMP: { // CMP: [opty, opval, opval, pred]
- // VFCmp/VICmp
- // or old form of ICmp/FCmp returning bool
- unsigned OpNum = 0;
- Value *LHS, *RHS;
- if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
- getValue(Record, OpNum, LHS->getType(), RHS) ||
- OpNum+1 != Record.size())
- return Error("Invalid CMP record");
-
- if (LHS->getType()->isFloatingPoint())
- I = new FCmpInst((FCmpInst::Predicate)Record[OpNum], LHS, RHS);
- else if (!isa<VectorType>(LHS->getType()))
- I = new ICmpInst((ICmpInst::Predicate)Record[OpNum], LHS, RHS);
- else if (LHS->getType()->isFPOrFPVector())
- I = new VFCmpInst((FCmpInst::Predicate)Record[OpNum], LHS, RHS);
- else
- I = new VICmpInst((ICmpInst::Predicate)Record[OpNum], LHS, RHS);
- break;
- }
+ case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred]
+ // Old form of ICmp/FCmp returning bool
+ // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were
+ // both legal on vectors but had different behaviour.
case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred]
- // Fcmp/ICmp returning bool or vector of bool
+ // FCmp/ICmp returning bool or vector of bool
+
unsigned OpNum = 0;
Value *LHS, *RHS;
if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
getValue(Record, OpNum, LHS->getType(), RHS) ||
OpNum+1 != Record.size())
- return Error("Invalid CMP2 record");
+ return Error("Invalid CMP record");
if (LHS->getType()->isFPOrFPVector())
- I = new FCmpInst((FCmpInst::Predicate)Record[OpNum], LHS, RHS);
- else
- I = new ICmpInst((ICmpInst::Predicate)Record[OpNum], LHS, RHS);
+ I = new FCmpInst(Context, (FCmpInst::Predicate)Record[OpNum], LHS, RHS);
+ else
+ I = new ICmpInst(Context, (ICmpInst::Predicate)Record[OpNum], LHS, RHS);
break;
}
+
case bitc::FUNC_CODE_INST_GETRESULT: { // GETRESULT: [ty, val, n]
if (Record.size() != 2)
return Error("Invalid GETRESULT record");
if (Vs.size() > 1 ||
(isa<StructType>(ReturnType) &&
(Vs.empty() || Vs[0]->getType() != ReturnType))) {
- Value *RV = UndefValue::get(ReturnType);
+ Value *RV = Context.getUndef(ReturnType);
for (unsigned i = 0, e = Vs.size(); i != e; ++i) {
I = InsertValueInst::Create(RV, Vs[i], i, "mrv");
CurBB->getInstList().push_back(I);
unsigned OpNum = 0;
Value *Val, *Ptr;
if (getValueTypePair(Record, OpNum, NextValueNo, Val) ||
- getValue(Record, OpNum, PointerType::getUnqual(Val->getType()), Ptr)||
+ getValue(Record, OpNum,
+ Context.getPointerTypeUnqual(Val->getType()), Ptr)||
OpNum+2 != Record.size())
return Error("Invalid STORE record");
// We found at least one unresolved value. Nuke them all to avoid leaks.
for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){
if ((A = dyn_cast<Argument>(ValueList.back())) && A->getParent() == 0) {
- A->replaceAllUsesWith(UndefValue::get(A->getType()));
+ A->replaceAllUsesWith(Context.getUndef(A->getType()));
delete A;
}
}
Module *BitcodeReader::materializeModule(std::string *ErrInfo) {
- for (DenseMap<Function*, std::pair<uint64_t, unsigned> >::iterator I =
- DeferredFunctionInfo.begin(), E = DeferredFunctionInfo.end(); I != E;
- ++I) {
- Function *F = I->first;
+ // Iterate over the module, deserializing any functions that are still on
+ // disk.
+ for (Module::iterator F = TheModule->begin(), E = TheModule->end();
+ F != E; ++F)
if (F->hasNotBeenReadFromBitcode() &&
materializeFunction(F, ErrInfo))
return 0;
- }
// Upgrade any intrinsic calls that slipped through (should not happen!) and
// delete the old functions to clean up. We can't do this unless the entire
/// getBitcodeModuleProvider - lazy function-at-a-time loading from a file.
///
ModuleProvider *llvm::getBitcodeModuleProvider(MemoryBuffer *Buffer,
+ LLVMContext& Context,
std::string *ErrMsg) {
- BitcodeReader *R = new BitcodeReader(Buffer);
+ BitcodeReader *R = new BitcodeReader(Buffer, Context);
if (R->ParseBitcode()) {
if (ErrMsg)
*ErrMsg = R->getErrorString();
/// ParseBitcodeFile - Read the specified bitcode file, returning the module.
/// If an error occurs, return null and fill in *ErrMsg if non-null.
-Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, std::string *ErrMsg){
+Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
+ std::string *ErrMsg){
BitcodeReader *R;
- R = static_cast<BitcodeReader*>(getBitcodeModuleProvider(Buffer, ErrMsg));
+ R = static_cast<BitcodeReader*>(getBitcodeModuleProvider(Buffer, Context,
+ ErrMsg));
if (!R) return 0;
// Read in the entire module.
// is run.
if (M)
M = R->releaseModule(ErrMsg);
-
+
delete R;
return M;
}