#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/ManagedStatic.h"
+
using namespace llvm;
enum {
SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex
};
-void BitcodeReader::materializeForwardReferencedFunctions() {
- while (!BlockAddrFwdRefs.empty()) {
- Function *F = BlockAddrFwdRefs.begin()->first;
- F->Materialize();
+std::error_code BitcodeReader::materializeForwardReferencedFunctions() {
+ if (WillMaterializeAllForwardRefs)
+ return std::error_code();
+
+ // Prevent recursion.
+ WillMaterializeAllForwardRefs = true;
+
+ while (!BasicBlockFwdRefQueue.empty()) {
+ Function *F = BasicBlockFwdRefQueue.front();
+ BasicBlockFwdRefQueue.pop_front();
+ assert(F && "Expected valid function");
+ if (!BasicBlockFwdRefs.count(F))
+ // Already materialized.
+ continue;
+
+ // Check for a function that isn't materializable to prevent an infinite
+ // loop. When parsing a blockaddress stored in a global variable, there
+ // isn't a trivial way to check if a function will have a body without a
+ // linear search through FunctionsWithBodies, so just check it here.
+ if (!F->isMaterializable())
+ return Error(BitcodeError::NeverResolvedFunctionFromBlockAddress);
+
+ // Try to materialize F.
+ if (std::error_code EC = materialize(F))
+ return EC;
}
+ assert(BasicBlockFwdRefs.empty() && "Function missing from queue");
+
+ // Reset state.
+ WillMaterializeAllForwardRefs = false;
+ return std::error_code();
}
void BitcodeReader::FreeState() {
DeferredFunctionInfo.clear();
MDKindMap.clear();
- assert(BlockAddrFwdRefs.empty() && "Unresolved blockaddress fwd references");
+ assert(BasicBlockFwdRefs.empty() && "Unresolved blockaddress fwd references");
+ BasicBlockFwdRefQueue.clear();
}
//===----------------------------------------------------------------------===//
/// Provide fast operand accessors
- //DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
+ DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
};
}
struct OperandTraits<ConstantPlaceHolder> :
public FixedNumOperandTraits<ConstantPlaceHolder, 1> {
};
+DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantPlaceHolder, Value)
}
}
}
-void BitcodeReaderMDValueList::AssignValue(Value *V, unsigned Idx) {
+void BitcodeReaderMDValueList::AssignValue(Metadata *MD, unsigned Idx) {
if (Idx == size()) {
- push_back(V);
+ push_back(MD);
return;
}
if (Idx >= size())
resize(Idx+1);
- WeakVH &OldV = MDValuePtrs[Idx];
- if (!OldV) {
- OldV = V;
+ TrackingMDRef &OldMD = MDValuePtrs[Idx];
+ if (!OldMD) {
+ OldMD.reset(MD);
return;
}
// If there was a forward reference to this value, replace it.
- MDNode *PrevVal = cast<MDNode>(OldV);
- OldV->replaceAllUsesWith(V);
- MDNode::deleteTemporary(PrevVal);
- // Deleting PrevVal sets Idx value in MDValuePtrs to null. Set new
- // value for Idx.
- MDValuePtrs[Idx] = V;
+ MDNodeFwdDecl *PrevMD = cast<MDNodeFwdDecl>(OldMD.get());
+ PrevMD->replaceAllUsesWith(MD);
+ MDNode::deleteTemporary(PrevMD);
+ --NumFwdRefs;
}
-Value *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) {
+Metadata *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) {
if (Idx >= size())
resize(Idx + 1);
- if (Value *V = MDValuePtrs[Idx]) {
- assert(V->getType()->isMetadataTy() && "Type mismatch in value table!");
- return V;
- }
+ if (Metadata *MD = MDValuePtrs[Idx])
+ return MD;
// Create and return a placeholder, which will later be RAUW'd.
- Value *V = MDNode::getTemporary(Context, None);
- MDValuePtrs[Idx] = V;
- return V;
+ AnyFwdRefs = true;
+ ++NumFwdRefs;
+ Metadata *MD = MDNode::getTemporary(Context, None);
+ MDValuePtrs[Idx].reset(MD);
+ return MD;
+}
+
+void BitcodeReaderMDValueList::tryToResolveCycles() {
+ if (!AnyFwdRefs)
+ // Nothing to do.
+ return;
+
+ if (NumFwdRefs)
+ // Still forward references... can't resolve cycles.
+ return;
+
+ // Resolve any cycles.
+ for (auto &MD : MDValuePtrs) {
+ assert(!(MD && isa<MDNodeFwdDecl>(MD)) && "Unexpected forward reference");
+ if (auto *G = dyn_cast_or_null<GenericMDNode>(MD))
+ G->resolveCycles();
+ }
}
Type *BitcodeReader::getTypeByID(unsigned ID) {
// If we have a forward reference, the only possible case is when it is to a
// named struct. Just create a placeholder for now.
- return TypeList[ID] = StructType::create(Context);
+ return TypeList[ID] = createIdentifiedStructType(Context);
+}
+
+StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context,
+ StringRef Name) {
+ auto *Ret = StructType::create(Context, Name);
+ IdentifiedStructTypes.push_back(Ret);
+ return Ret;
+}
+
+StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) {
+ auto *Ret = StructType::create(Context);
+ IdentifiedStructTypes.push_back(Ret);
+ return Ret;
}
Res->setName(TypeName);
TypeList[NumRecords] = nullptr;
} else // Otherwise, create a new struct.
- Res = StructType::create(Context, TypeName);
+ Res = createIdentifiedStructType(Context, TypeName);
TypeName.clear();
SmallVector<Type*, 8> EltTys;
Res->setName(TypeName);
TypeList[NumRecords] = nullptr;
} else // Otherwise, create a new struct with no body.
- Res = StructType::create(Context, TypeName);
+ Res = createIdentifiedStructType(Context, TypeName);
TypeName.clear();
ResultTy = Res;
break;
case BitstreamEntry::Error:
return Error(BitcodeError::MalformedBlock);
case BitstreamEntry::EndBlock:
+ MDValueList.tryToResolveCycles();
return std::error_code();
case BitstreamEntry::Record:
// The interesting case.
break;
}
- bool IsFunctionLocal = false;
// Read a record.
Record.clear();
unsigned Code = Stream.readRecord(Entry.ID, Record);
}
break;
}
- case bitc::METADATA_FN_NODE:
- IsFunctionLocal = true;
- // fall-through
- case bitc::METADATA_NODE: {
+ case bitc::METADATA_OLD_FN_NODE: {
+ // This is a LocalAsMetadata record, the only type of function-local
+ // metadata.
+ if (Record.size() % 2 == 1)
+ return Error(BitcodeError::InvalidRecord);
+
+ // If this isn't a LocalAsMetadata record, we're dropping it. This used
+ // to be legal, but there's no upgrade path.
+ auto dropRecord = [&] {
+ MDValueList.AssignValue(MDNode::get(Context, None), NextMDValueNo++);
+ };
+ if (Record.size() != 2) {
+ dropRecord();
+ break;
+ }
+
+ Type *Ty = getTypeByID(Record[0]);
+ if (Ty->isMetadataTy() || Ty->isVoidTy()) {
+ dropRecord();
+ break;
+ }
+
+ MDValueList.AssignValue(
+ LocalAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
+ NextMDValueNo++);
+ break;
+ }
+ case bitc::METADATA_OLD_NODE: {
if (Record.size() % 2 == 1)
return Error(BitcodeError::InvalidRecord);
unsigned Size = Record.size();
- SmallVector<Value*, 8> Elts;
+ SmallVector<Metadata *, 8> Elts;
for (unsigned i = 0; i != Size; i += 2) {
Type *Ty = getTypeByID(Record[i]);
if (!Ty)
return Error(BitcodeError::InvalidRecord);
if (Ty->isMetadataTy())
Elts.push_back(MDValueList.getValueFwdRef(Record[i+1]));
- else if (!Ty->isVoidTy())
- Elts.push_back(ValueList.getValueFwdRef(Record[i+1], Ty));
- else
+ else if (!Ty->isVoidTy()) {
+ auto *MD =
+ ValueAsMetadata::get(ValueList.getValueFwdRef(Record[i + 1], Ty));
+ assert(isa<ConstantAsMetadata>(MD) &&
+ "Expected non-function-local metadata");
+ Elts.push_back(MD);
+ } else
Elts.push_back(nullptr);
}
- Value *V = MDNode::getWhenValsUnresolved(Context, Elts, IsFunctionLocal);
- IsFunctionLocal = false;
- MDValueList.AssignValue(V, NextMDValueNo++);
+ MDValueList.AssignValue(MDNode::get(Context, Elts), NextMDValueNo++);
+ break;
+ }
+ case bitc::METADATA_VALUE: {
+ if (Record.size() != 2)
+ return Error(BitcodeError::InvalidRecord);
+
+ Type *Ty = getTypeByID(Record[0]);
+ if (Ty->isMetadataTy() || Ty->isVoidTy())
+ return Error(BitcodeError::InvalidRecord);
+
+ MDValueList.AssignValue(
+ ValueAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
+ NextMDValueNo++);
+ break;
+ }
+ case bitc::METADATA_NODE: {
+ SmallVector<Metadata *, 8> Elts;
+ Elts.reserve(Record.size());
+ for (unsigned ID : Record)
+ Elts.push_back(ID ? MDValueList.getValueFwdRef(ID - 1) : nullptr);
+ MDValueList.AssignValue(MDNode::get(Context, Elts), NextMDValueNo++);
break;
}
case bitc::METADATA_STRING: {
std::string String(Record.begin(), Record.end());
llvm::UpgradeMDStringConstant(String);
- Value *V = MDString::get(Context, String);
- MDValueList.AssignValue(V, NextMDValueNo++);
+ Metadata *MD = MDString::get(Context, String);
+ MDValueList.AssignValue(MD, NextMDValueNo++);
break;
}
case bitc::METADATA_KIND: {
std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist;
std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist;
std::vector<std::pair<Function*, unsigned> > FunctionPrefixWorklist;
+ std::vector<std::pair<Function*, unsigned> > FunctionPrologueWorklist;
GlobalInitWorklist.swap(GlobalInits);
AliasInitWorklist.swap(AliasInits);
FunctionPrefixWorklist.swap(FunctionPrefixes);
+ FunctionPrologueWorklist.swap(FunctionPrologues);
while (!GlobalInitWorklist.empty()) {
unsigned ValID = GlobalInitWorklist.back().second;
FunctionPrefixWorklist.pop_back();
}
+ while (!FunctionPrologueWorklist.empty()) {
+ unsigned ValID = FunctionPrologueWorklist.back().second;
+ if (ValID >= ValueList.size()) {
+ FunctionPrologues.push_back(FunctionPrologueWorklist.back());
+ } else {
+ if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
+ FunctionPrologueWorklist.back().first->setPrologueData(C);
+ else
+ return Error(BitcodeError::ExpectedConstant);
+ }
+ FunctionPrologueWorklist.pop_back();
+ }
+
return std::error_code();
}
if (!Fn)
return Error(BitcodeError::InvalidRecord);
+ // Don't let Fn get dematerialized.
+ BlockAddressesTaken.insert(Fn);
+
// If the function is already parsed we can insert the block address right
// away.
+ BasicBlock *BB;
+ unsigned BBID = Record[2];
+ if (!BBID)
+ // Invalid reference to entry block.
+ return Error(BitcodeError::InvalidID);
if (!Fn->empty()) {
Function::iterator BBI = Fn->begin(), BBE = Fn->end();
- for (size_t I = 0, E = Record[2]; I != E; ++I) {
+ for (size_t I = 0, E = BBID; I != E; ++I) {
if (BBI == BBE)
return Error(BitcodeError::InvalidID);
++BBI;
}
- V = BlockAddress::get(Fn, BBI);
+ BB = BBI;
} else {
// Otherwise insert a placeholder and remember it so it can be inserted
// when the function is parsed.
- GlobalVariable *FwdRef = new GlobalVariable(*Fn->getParent(),
- Type::getInt8Ty(Context),
- false, GlobalValue::InternalLinkage,
- nullptr, "");
- BlockAddrFwdRefs[Fn].push_back(std::make_pair(Record[2], FwdRef));
- V = FwdRef;
+ auto &FwdBBs = BasicBlockFwdRefs[Fn];
+ if (FwdBBs.empty())
+ BasicBlockFwdRefQueue.push_back(Fn);
+ if (FwdBBs.size() < BBID + 1)
+ FwdBBs.resize(BBID + 1);
+ if (!FwdBBs[BBID])
+ FwdBBs[BBID] = BasicBlock::Create(Context);
+ BB = FwdBBs[BBID];
}
+ V = BlockAddress::get(Fn, BB);
break;
}
}
unsigned NumUses = 0;
SmallDenseMap<const Use *, unsigned, 16> Order;
for (const Use &U : V->uses()) {
- if (NumUses > Record.size())
+ if (++NumUses > Record.size())
break;
- Order[&U] = Record[NumUses++];
+ Order[&U] = Record[NumUses - 1];
}
if (Order.size() != Record.size() || NumUses > Record.size())
// Mismatches can happen if the functions are being materialized lazily
}
// FUNCTION: [type, callingconv, isproto, linkage, paramattr,
// alignment, section, visibility, gc, unnamed_addr,
- // dllstorageclass]
+ // prologuedata, dllstorageclass, comdat, prefixdata]
case bitc::MODULE_CODE_FUNCTION: {
if (Record.size() < 8)
return Error(BitcodeError::InvalidRecord);
UnnamedAddr = Record[9];
Func->setUnnamedAddr(UnnamedAddr);
if (Record.size() > 10 && Record[10] != 0)
- FunctionPrefixes.push_back(std::make_pair(Func, Record[10]-1));
+ FunctionPrologues.push_back(std::make_pair(Func, Record[10]-1));
if (Record.size() > 11)
Func->setDLLStorageClass(GetDecodedDLLStorageClass(Record[11]));
Func->setComdat(ComdatList[ComdatID - 1]);
}
+ if (Record.size() > 13 && Record[13] != 0)
+ FunctionPrefixes.push_back(std::make_pair(Func, Record[13]-1));
+
ValueList.push_back(Func);
// If this is a function with a body, remember the prototype we are
// creating now, so that we can match up the body with them later.
if (!isProto) {
+ Func->setIsMaterializable(true);
FunctionsWithBodies.push_back(Func);
- if (LazyStreamer) DeferredFunctionInfo[Func] = 0;
+ if (LazyStreamer)
+ DeferredFunctionInfo[Func] = 0;
}
break;
}
else
UpgradeDLLImportExportLinkage(NewGA, Record[2]);
if (Record.size() > 5)
- NewGA->setThreadLocalMode(GetDecodedThreadLocalMode(Record[5]));
+ NewGA->setThreadLocalMode(GetDecodedThreadLocalMode(Record[5]));
if (Record.size() > 6)
- NewGA->setUnnamedAddr(Record[6]);
+ NewGA->setUnnamedAddr(Record[6]);
ValueList.push_back(NewGA);
AliasInits.push_back(std::make_pair(NewGA, Record[1]));
break;
MDKindMap.find(Kind);
if (I == MDKindMap.end())
return Error(BitcodeError::InvalidID);
- Value *Node = MDValueList.getValueFwdRef(Record[i+1]);
+ Metadata *Node = MDValueList.getValueFwdRef(Record[i + 1]);
+ if (isa<LocalAsMetadata>(Node))
+ // Drop the attachment. This used to be legal, but there's no
+ // upgrade path.
+ break;
Inst->setMetadata(I->second, cast<MDNode>(Node));
if (I->second == LLVMContext::MD_tbaa)
InstsWithTBAATag.push_back(Inst);
switch (BitCode) {
default: // Default behavior: reject
return Error(BitcodeError::InvalidValue);
- case bitc::FUNC_CODE_DECLAREBLOCKS: // DECLAREBLOCKS: [nblocks]
+ case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks]
if (Record.size() < 1 || Record[0] == 0)
return Error(BitcodeError::InvalidRecord);
// Create all the basic blocks for the function.
FunctionBBs.resize(Record[0]);
- for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i)
- FunctionBBs[i] = BasicBlock::Create(Context, "", F);
+
+ // See if anything took the address of blocks in this function.
+ auto BBFRI = BasicBlockFwdRefs.find(F);
+ if (BBFRI == BasicBlockFwdRefs.end()) {
+ for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i)
+ FunctionBBs[i] = BasicBlock::Create(Context, "", F);
+ } else {
+ auto &BBRefs = BBFRI->second;
+ // Check for invalid basic block references.
+ if (BBRefs.size() > FunctionBBs.size())
+ return Error(BitcodeError::InvalidID);
+ assert(!BBRefs.empty() && "Unexpected empty array");
+ assert(!BBRefs.front() && "Invalid reference to entry block");
+ for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E;
+ ++I)
+ if (I < RE && BBRefs[I]) {
+ BBRefs[I]->insertInto(F);
+ FunctionBBs[I] = BBRefs[I];
+ } else {
+ FunctionBBs[I] = BasicBlock::Create(Context, "", F);
+ }
+
+ // Erase from the table.
+ BasicBlockFwdRefs.erase(BBFRI);
+ }
+
CurBB = FunctionBBs[0];
continue;
+ }
case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN
// This record indicates that the last instruction is at the same
// FIXME: Check for unresolved forward-declared metadata references
// and clean up leaks.
- // See if anything took the address of blocks in this function. If so,
- // resolve them now.
- DenseMap<Function*, std::vector<BlockAddrRefTy> >::iterator BAFRI =
- BlockAddrFwdRefs.find(F);
- if (BAFRI != BlockAddrFwdRefs.end()) {
- std::vector<BlockAddrRefTy> &RefList = BAFRI->second;
- for (unsigned i = 0, e = RefList.size(); i != e; ++i) {
- unsigned BlockIdx = RefList[i].first;
- if (BlockIdx >= FunctionBBs.size())
- return Error(BitcodeError::InvalidID);
-
- GlobalVariable *FwdRef = RefList[i].second;
- FwdRef->replaceAllUsesWith(BlockAddress::get(F, FunctionBBs[BlockIdx]));
- FwdRef->eraseFromParent();
- }
-
- BlockAddrFwdRefs.erase(BAFRI);
- }
-
// Trim the value list down to the size it was before we parsed this function.
ValueList.shrinkTo(ModuleValueListSize);
MDValueList.shrinkTo(ModuleMDValueListSize);
void BitcodeReader::releaseBuffer() { Buffer.release(); }
-bool BitcodeReader::isMaterializable(const GlobalValue *GV) const {
- if (const Function *F = dyn_cast<Function>(GV)) {
- return F->isDeclaration() &&
- DeferredFunctionInfo.count(const_cast<Function*>(F));
- }
- return false;
-}
-
-std::error_code BitcodeReader::Materialize(GlobalValue *GV) {
+std::error_code BitcodeReader::materialize(GlobalValue *GV) {
Function *F = dyn_cast<Function>(GV);
// If it's not a function or is already material, ignore the request.
if (!F || !F->isMaterializable())
if (std::error_code EC = ParseFunctionBody(F))
return EC;
+ F->setIsMaterializable(false);
// Upgrade any old intrinsic calls in the function.
for (UpgradedIntrinsicMap::iterator I = UpgradedIntrinsics.begin(),
}
}
- return std::error_code();
+ // Bring in any functions that this function forward-referenced via
+ // blockaddresses.
+ return materializeForwardReferencedFunctions();
}
bool BitcodeReader::isDematerializable(const GlobalValue *GV) const {
const Function *F = dyn_cast<Function>(GV);
if (!F || F->isDeclaration())
return false;
+
+ // Dematerializing F would leave dangling references that wouldn't be
+ // reconnected on re-materialization.
+ if (BlockAddressesTaken.count(F))
+ return false;
+
return DeferredFunctionInfo.count(const_cast<Function*>(F));
}
assert(DeferredFunctionInfo.count(F) && "No info to read function later?");
// Just forget the function body, we can remat it later.
- F->deleteBody();
+ F->dropAllReferences();
+ F->setIsMaterializable(true);
}
std::error_code BitcodeReader::MaterializeModule(Module *M) {
assert(M == TheModule &&
"Can only Materialize the Module this BitcodeReader is attached to.");
+
+ // Promise to materialize all forward references.
+ WillMaterializeAllForwardRefs = true;
+
// 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->isMaterializable()) {
- if (std::error_code EC = Materialize(F))
- return EC;
- }
+ if (std::error_code EC = materialize(F))
+ return EC;
}
// At this point, if there are any function bodies, the current bit is
// pointing to the END_BLOCK record after them. Now make sure the rest
if (NextUnreadBit)
ParseModule(true);
+ // Check that all block address forward references got resolved (as we
+ // promised above).
+ if (!BasicBlockFwdRefs.empty())
+ return Error(BitcodeError::NeverResolvedFunctionFromBlockAddress);
+
// 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
// module is materialized because there could always be another function body
return std::error_code();
}
+std::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const {
+ return IdentifiedStructTypes;
+}
+
std::error_code BitcodeReader::InitStream() {
if (LazyStreamer)
return InitLazyStream();
const unsigned char *BufPtr = (const unsigned char*)Buffer->getBufferStart();
const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
- if (Buffer->getBufferSize() & 3) {
- if (!isRawBitcode(BufPtr, BufEnd) && !isBitcodeWrapper(BufPtr, BufEnd))
- return Error(BitcodeError::InvalidBitcodeSignature);
- else
- return Error(BitcodeError::BitcodeStreamInvalidSize);
- }
+ if (Buffer->getBufferSize() & 3)
+ return Error(BitcodeError::InvalidBitcodeSignature);
// If we have a wrapper header, parse it and ignore the non-bc file contents.
// The magic number is 0x0B17C0DE stored in little endian.
return Error(BitcodeError::InvalidBitcodeWrapperHeader);
StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
- Stream.init(*StreamFile);
+ Stream.init(&*StreamFile);
return std::error_code();
}
// see it.
StreamingMemoryObject *Bytes = new StreamingMemoryObject(LazyStreamer);
StreamFile.reset(new BitstreamReader(Bytes));
- Stream.init(*StreamFile);
+ Stream.init(&*StreamFile);
unsigned char buf[16];
- if (Bytes->readBytes(0, 16, buf) == -1)
- return Error(BitcodeError::BitcodeStreamInvalidSize);
+ if (Bytes->readBytes(buf, 16, 0) != 16)
+ return Error(BitcodeError::InvalidBitcodeSignature);
if (!isBitcode(buf, buf + 16))
return Error(BitcodeError::InvalidBitcodeSignature);
std::string message(int IE) const override {
BitcodeError E = static_cast<BitcodeError>(IE);
switch (E) {
- case BitcodeError::BitcodeStreamInvalidSize:
- return "Bitcode stream length should be >= 16 bytes and a multiple of 4";
case BitcodeError::ConflictingMETADATA_KINDRecords:
return "Conflicting METADATA_KIND records";
case BitcodeError::CouldNotFindFunctionInStream:
return "Invalid multiple blocks";
case BitcodeError::NeverResolvedValueFoundInFunction:
return "Never resolved value found in function";
+ case BitcodeError::NeverResolvedFunctionFromBlockAddress:
+ return "Never resolved function from blockaddress";
case BitcodeError::InvalidValue:
return "Invalid value";
}
};
}
+static ManagedStatic<BitcodeErrorCategoryType> ErrorCategory;
+
const std::error_category &llvm::BitcodeErrorCategory() {
- static BitcodeErrorCategoryType O;
- return O;
+ return *ErrorCategory;
}
//===----------------------------------------------------------------------===//
// External interface
//===----------------------------------------------------------------------===//
-/// getLazyBitcodeModule - lazy function-at-a-time loading from a file.
+/// \brief Get a lazy one-at-time loading module from bitcode.
+///
+/// This isn't always used in a lazy context. In particular, it's also used by
+/// \a parseBitcodeFile(). If this is truly lazy, then we need to eagerly pull
+/// in forward-referenced functions from block address references.
///
-ErrorOr<Module *> llvm::getLazyBitcodeModule(MemoryBuffer *Buffer,
- LLVMContext &Context) {
+/// \param[in] WillMaterializeAll Set to \c true if the caller promises to
+/// materialize everything -- in particular, if this isn't truly lazy.
+static ErrorOr<Module *>
+getLazyBitcodeModuleImpl(std::unique_ptr<MemoryBuffer> &&Buffer,
+ LLVMContext &Context, bool WillMaterializeAll) {
Module *M = new Module(Buffer->getBufferIdentifier(), Context);
- BitcodeReader *R = new BitcodeReader(Buffer, Context);
+ BitcodeReader *R = new BitcodeReader(Buffer.get(), Context);
M->setMaterializer(R);
- if (std::error_code EC = R->ParseBitcodeInto(M)) {
+
+ auto cleanupOnError = [&](std::error_code EC) {
R->releaseBuffer(); // Never take ownership on error.
delete M; // Also deletes R.
return EC;
- }
+ };
+
+ if (std::error_code EC = R->ParseBitcodeInto(M))
+ return cleanupOnError(EC);
- R->materializeForwardReferencedFunctions();
+ if (!WillMaterializeAll)
+ // Resolve forward references from blockaddresses.
+ if (std::error_code EC = R->materializeForwardReferencedFunctions())
+ return cleanupOnError(EC);
+ Buffer.release(); // The BitcodeReader owns it now.
return M;
}
+ErrorOr<Module *>
+llvm::getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer,
+ LLVMContext &Context) {
+ return getLazyBitcodeModuleImpl(std::move(Buffer), Context, false);
+}
Module *llvm::getStreamedBitcodeModule(const std::string &name,
DataStreamer *streamer,
return M;
}
-ErrorOr<Module *> llvm::parseBitcodeFile(MemoryBuffer *Buffer,
+ErrorOr<Module *> llvm::parseBitcodeFile(MemoryBufferRef Buffer,
LLVMContext &Context) {
- ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
+ std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
+ ErrorOr<Module *> ModuleOrErr =
+ getLazyBitcodeModuleImpl(std::move(Buf), Context, true);
if (!ModuleOrErr)
return ModuleOrErr;
Module *M = ModuleOrErr.get();
// Read in the entire module, and destroy the BitcodeReader.
- if (std::error_code EC = M->materializeAllPermanently(true)) {
+ if (std::error_code EC = M->materializeAllPermanently()) {
delete M;
return EC;
}
return M;
}
-std::string llvm::getBitcodeTargetTriple(MemoryBuffer *Buffer,
+std::string llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer,
LLVMContext &Context) {
- BitcodeReader *R = new BitcodeReader(Buffer, Context);
+ std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
+ auto R = llvm::make_unique<BitcodeReader>(Buf.release(), Context);
ErrorOr<std::string> Triple = R->parseTriple();
- R->releaseBuffer();
- delete R;
if (Triple.getError())
return "";
return Triple.get();