2007-04-22 06:23:29 +00:00
|
|
|
//===- BitcodeReader.h - Internal BitcodeReader impl ------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-04-22 06:23:29 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This header defines the BitcodeReader class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef BITCODE_READER_H
|
|
|
|
#define BITCODE_READER_H
|
|
|
|
|
2010-01-27 20:34:15 +00:00
|
|
|
#include "llvm/GVMaterializer.h"
|
2008-09-23 23:03:40 +00:00
|
|
|
#include "llvm/Attributes.h"
|
2007-04-24 05:48:56 +00:00
|
|
|
#include "llvm/Type.h"
|
2008-05-10 08:32:32 +00:00
|
|
|
#include "llvm/OperandTraits.h"
|
2007-05-01 04:59:48 +00:00
|
|
|
#include "llvm/Bitcode/BitstreamReader.h"
|
2007-04-23 01:01:37 +00:00
|
|
|
#include "llvm/Bitcode/LLVMBitCodes.h"
|
2009-03-31 22:55:09 +00:00
|
|
|
#include "llvm/Support/ValueHandle.h"
|
2007-05-01 04:59:48 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2007-04-22 06:23:29 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace llvm {
|
2007-04-29 07:54:31 +00:00
|
|
|
class MemoryBuffer;
|
2009-08-11 17:45:13 +00:00
|
|
|
class LLVMContext;
|
2007-04-24 05:48:56 +00:00
|
|
|
|
2008-05-10 08:32:32 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BitcodeReaderValueList Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-03-31 22:55:09 +00:00
|
|
|
class BitcodeReaderValueList {
|
|
|
|
std::vector<WeakVH> ValuePtrs;
|
2008-08-21 02:34:16 +00:00
|
|
|
|
|
|
|
/// ResolveConstants - As we resolve forward-referenced constants, we add
|
|
|
|
/// information about them to this vector. This allows us to resolve them in
|
|
|
|
/// bulk instead of resolving each reference at a time. See the code in
|
|
|
|
/// ResolveConstantForwardRefs for more information about this.
|
|
|
|
///
|
|
|
|
/// The key of this vector is the placeholder constant, the value is the slot
|
|
|
|
/// number that holds the resolved value.
|
|
|
|
typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy;
|
|
|
|
ResolveConstantsTy ResolveConstants;
|
2011-07-07 05:12:37 +00:00
|
|
|
LLVMContext &Context;
|
2007-04-24 05:48:56 +00:00
|
|
|
public:
|
2011-07-07 05:12:37 +00:00
|
|
|
BitcodeReaderValueList(LLVMContext &C) : Context(C) {}
|
2008-08-21 02:34:16 +00:00
|
|
|
~BitcodeReaderValueList() {
|
|
|
|
assert(ResolveConstants.empty() && "Constants not resolved?");
|
|
|
|
}
|
2008-05-10 08:32:32 +00:00
|
|
|
|
2007-04-24 05:48:56 +00:00
|
|
|
// vector compatibility methods
|
2009-03-31 22:55:09 +00:00
|
|
|
unsigned size() const { return ValuePtrs.size(); }
|
|
|
|
void resize(unsigned N) { ValuePtrs.resize(N); }
|
2007-04-24 05:48:56 +00:00
|
|
|
void push_back(Value *V) {
|
2009-03-31 22:55:09 +00:00
|
|
|
ValuePtrs.push_back(V);
|
2007-04-24 05:48:56 +00:00
|
|
|
}
|
|
|
|
|
2007-05-18 04:02:46 +00:00
|
|
|
void clear() {
|
2008-08-21 02:34:16 +00:00
|
|
|
assert(ResolveConstants.empty() && "Constants not resolved?");
|
2009-03-31 22:55:09 +00:00
|
|
|
ValuePtrs.clear();
|
2007-05-18 04:02:46 +00:00
|
|
|
}
|
|
|
|
|
2009-03-31 22:55:09 +00:00
|
|
|
Value *operator[](unsigned i) const {
|
|
|
|
assert(i < ValuePtrs.size());
|
|
|
|
return ValuePtrs[i];
|
|
|
|
}
|
2007-04-24 05:48:56 +00:00
|
|
|
|
2009-03-31 22:55:09 +00:00
|
|
|
Value *back() const { return ValuePtrs.back(); }
|
|
|
|
void pop_back() { ValuePtrs.pop_back(); }
|
|
|
|
bool empty() const { return ValuePtrs.empty(); }
|
2007-04-26 03:27:58 +00:00
|
|
|
void shrinkTo(unsigned N) {
|
2009-03-31 22:55:09 +00:00
|
|
|
assert(N <= size() && "Invalid shrinkTo request!");
|
|
|
|
ValuePtrs.resize(N);
|
2007-04-26 03:27:58 +00:00
|
|
|
}
|
2007-04-24 05:48:56 +00:00
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
Constant *getConstantFwdRef(unsigned Idx, Type *Ty);
|
|
|
|
Value *getValueFwdRef(unsigned Idx, Type *Ty);
|
2007-05-01 07:01:57 +00:00
|
|
|
|
2009-03-31 22:55:09 +00:00
|
|
|
void AssignValue(Value *V, unsigned Idx);
|
2007-05-01 07:01:57 +00:00
|
|
|
|
2008-08-21 02:34:16 +00:00
|
|
|
/// ResolveConstantForwardRefs - Once all constants are read, this method bulk
|
|
|
|
/// resolves any forward references.
|
|
|
|
void ResolveConstantForwardRefs();
|
2008-05-10 08:32:32 +00:00
|
|
|
};
|
|
|
|
|
2009-08-04 06:00:18 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BitcodeReaderMDValueList Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
class BitcodeReaderMDValueList {
|
|
|
|
std::vector<WeakVH> MDValuePtrs;
|
|
|
|
|
2009-10-28 05:53:48 +00:00
|
|
|
LLVMContext &Context;
|
2009-08-04 06:00:18 +00:00
|
|
|
public:
|
|
|
|
BitcodeReaderMDValueList(LLVMContext& C) : Context(C) {}
|
|
|
|
|
|
|
|
// vector compatibility methods
|
|
|
|
unsigned size() const { return MDValuePtrs.size(); }
|
|
|
|
void resize(unsigned N) { MDValuePtrs.resize(N); }
|
|
|
|
void push_back(Value *V) { MDValuePtrs.push_back(V); }
|
|
|
|
void clear() { MDValuePtrs.clear(); }
|
|
|
|
Value *back() const { return MDValuePtrs.back(); }
|
|
|
|
void pop_back() { MDValuePtrs.pop_back(); }
|
|
|
|
bool empty() const { return MDValuePtrs.empty(); }
|
|
|
|
|
|
|
|
Value *operator[](unsigned i) const {
|
|
|
|
assert(i < MDValuePtrs.size());
|
|
|
|
return MDValuePtrs[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
void shrinkTo(unsigned N) {
|
|
|
|
assert(N <= size() && "Invalid shrinkTo request!");
|
|
|
|
MDValuePtrs.resize(N);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *getValueFwdRef(unsigned Idx);
|
|
|
|
void AssignValue(Value *V, unsigned Idx);
|
|
|
|
};
|
|
|
|
|
2010-01-27 20:34:15 +00:00
|
|
|
class BitcodeReader : public GVMaterializer {
|
2009-10-28 05:53:48 +00:00
|
|
|
LLVMContext &Context;
|
2010-01-27 20:34:15 +00:00
|
|
|
Module *TheModule;
|
2007-04-29 07:54:31 +00:00
|
|
|
MemoryBuffer *Buffer;
|
2010-01-27 20:34:15 +00:00
|
|
|
bool BufferOwned;
|
2009-04-26 20:59:02 +00:00
|
|
|
BitstreamReader StreamFile;
|
|
|
|
BitstreamCursor Stream;
|
2007-05-01 04:59:48 +00:00
|
|
|
|
2007-04-22 06:23:29 +00:00
|
|
|
const char *ErrorString;
|
|
|
|
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@134829 91177308-0d34-0410-b5e6-96231b3b80d8
2011-07-09 17:41:24 +00:00
|
|
|
std::vector<Type*> TypeList;
|
2007-04-24 05:48:56 +00:00
|
|
|
BitcodeReaderValueList ValueList;
|
2009-08-04 06:00:18 +00:00
|
|
|
BitcodeReaderMDValueList MDValueList;
|
2009-09-18 19:26:43 +00:00
|
|
|
SmallVector<Instruction *, 64> InstructionList;
|
|
|
|
|
2007-04-24 03:30:34 +00:00
|
|
|
std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
|
2007-04-26 02:46:40 +00:00
|
|
|
std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
|
2007-05-01 04:59:48 +00:00
|
|
|
|
2008-09-26 22:53:05 +00:00
|
|
|
/// MAttributes - The set of attributes by index. Index zero in the
|
2007-05-04 03:30:17 +00:00
|
|
|
/// file is for null, and is thus not represented here. As such all indices
|
|
|
|
/// are off by one.
|
2008-09-26 22:53:05 +00:00
|
|
|
std::vector<AttrListPtr> MAttributes;
|
2007-05-04 03:30:17 +00:00
|
|
|
|
2007-05-01 05:52:21 +00:00
|
|
|
/// FunctionBBs - While parsing a function body, this is a list of the basic
|
|
|
|
/// blocks for the function.
|
|
|
|
std::vector<BasicBlock*> FunctionBBs;
|
|
|
|
|
2007-05-01 04:59:48 +00:00
|
|
|
// When reading the module header, this list is populated with functions that
|
|
|
|
// have bodies later in the file.
|
|
|
|
std::vector<Function*> FunctionsWithBodies;
|
2007-08-04 01:51:18 +00:00
|
|
|
|
|
|
|
// When intrinsic functions are encountered which require upgrading they are
|
|
|
|
// stored here with their replacement function.
|
|
|
|
typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
|
|
|
|
UpgradedIntrinsicMap UpgradedIntrinsics;
|
2010-07-20 21:42:28 +00:00
|
|
|
|
|
|
|
// Map the bitcode's custom MDKind ID to the Module's MDKind ID.
|
|
|
|
DenseMap<unsigned, unsigned> MDKindMap;
|
2007-05-01 04:59:48 +00:00
|
|
|
|
|
|
|
// After the module header has been read, the FunctionsWithBodies list is
|
|
|
|
// reversed. This keeps track of whether we've done this yet.
|
|
|
|
bool HasReversedFunctionsWithBodies;
|
|
|
|
|
|
|
|
/// DeferredFunctionInfo - When function bodies are initially scanned, this
|
2010-01-27 20:34:15 +00:00
|
|
|
/// map contains info about where to find deferred function body in the
|
|
|
|
/// stream.
|
|
|
|
DenseMap<Function*, uint64_t> DeferredFunctionInfo;
|
2009-10-28 05:53:48 +00:00
|
|
|
|
|
|
|
/// BlockAddrFwdRefs - These are blockaddr references to basic blocks. These
|
|
|
|
/// are resolved lazily when functions are loaded.
|
|
|
|
typedef std::pair<unsigned, GlobalVariable*> BlockAddrRefTy;
|
|
|
|
DenseMap<Function*, std::vector<BlockAddrRefTy> > BlockAddrFwdRefs;
|
2010-09-13 18:00:48 +00:00
|
|
|
|
2007-04-22 06:23:29 +00:00
|
|
|
public:
|
2009-12-29 09:01:33 +00:00
|
|
|
explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C)
|
2010-01-27 20:34:15 +00:00
|
|
|
: Context(C), TheModule(0), Buffer(buffer), BufferOwned(false),
|
2011-06-17 17:48:53 +00:00
|
|
|
ErrorString(0), ValueList(C), MDValueList(C) {
|
2007-05-01 04:59:48 +00:00
|
|
|
HasReversedFunctionsWithBodies = false;
|
|
|
|
}
|
2007-05-18 04:02:46 +00:00
|
|
|
~BitcodeReader() {
|
|
|
|
FreeState();
|
|
|
|
}
|
2007-04-22 06:23:29 +00:00
|
|
|
|
2007-05-18 04:02:46 +00:00
|
|
|
void FreeState();
|
2007-04-29 07:54:31 +00:00
|
|
|
|
2010-01-27 20:34:15 +00:00
|
|
|
/// setBufferOwned - If this is true, the reader will destroy the MemoryBuffer
|
|
|
|
/// when the reader is destroyed.
|
|
|
|
void setBufferOwned(bool Owned) { BufferOwned = Owned; }
|
2007-04-22 06:23:29 +00:00
|
|
|
|
2010-01-27 20:34:15 +00:00
|
|
|
virtual bool isMaterializable(const GlobalValue *GV) const;
|
|
|
|
virtual bool isDematerializable(const GlobalValue *GV) const;
|
|
|
|
virtual bool Materialize(GlobalValue *GV, std::string *ErrInfo = 0);
|
|
|
|
virtual bool MaterializeModule(Module *M, std::string *ErrInfo = 0);
|
|
|
|
virtual void Dematerialize(GlobalValue *GV);
|
2007-05-15 06:29:44 +00:00
|
|
|
|
2007-04-22 06:23:29 +00:00
|
|
|
bool Error(const char *Str) {
|
|
|
|
ErrorString = Str;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const char *getErrorString() const { return ErrorString; }
|
|
|
|
|
|
|
|
/// @brief Main interface to parsing a bitcode buffer.
|
|
|
|
/// @returns true if an error occurred.
|
2010-01-27 20:34:15 +00:00
|
|
|
bool ParseBitcodeInto(Module *M);
|
2010-10-06 01:22:42 +00:00
|
|
|
|
|
|
|
/// @brief Cheap mechanism to just extract module triple
|
|
|
|
/// @returns true if an error occurred.
|
|
|
|
bool ParseTriple(std::string &Triple);
|
2007-04-22 06:23:29 +00:00
|
|
|
private:
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@134829 91177308-0d34-0410-b5e6-96231b3b80d8
2011-07-09 17:41:24 +00:00
|
|
|
Type *getTypeByID(unsigned ID);
|
|
|
|
Type *getTypeByIDOrNull(unsigned ID);
|
2011-07-18 04:54:35 +00:00
|
|
|
Value *getFnValueByID(unsigned ID, Type *Ty) {
|
2011-07-07 05:29:18 +00:00
|
|
|
if (Ty && Ty->isMetadataTy())
|
2009-08-04 06:00:18 +00:00
|
|
|
return MDValueList.getValueFwdRef(ID);
|
2011-07-07 05:12:37 +00:00
|
|
|
return ValueList.getValueFwdRef(ID, Ty);
|
2007-05-01 07:01:57 +00:00
|
|
|
}
|
2007-05-02 05:46:45 +00:00
|
|
|
BasicBlock *getBasicBlock(unsigned ID) const {
|
|
|
|
if (ID >= FunctionBBs.size()) return 0; // Invalid ID
|
|
|
|
return FunctionBBs[ID];
|
|
|
|
}
|
2008-09-25 21:00:45 +00:00
|
|
|
AttrListPtr getAttributes(unsigned i) const {
|
2008-09-26 22:53:05 +00:00
|
|
|
if (i-1 < MAttributes.size())
|
|
|
|
return MAttributes[i-1];
|
2008-09-25 21:00:45 +00:00
|
|
|
return AttrListPtr();
|
2007-05-04 03:30:17 +00:00
|
|
|
}
|
2007-05-06 00:00:00 +00:00
|
|
|
|
|
|
|
/// getValueTypePair - Read a value/type pair out of the specified record from
|
|
|
|
/// slot 'Slot'. Increment Slot past the number of slots used in the record.
|
|
|
|
/// Return true on failure.
|
|
|
|
bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
|
|
|
|
unsigned InstNum, Value *&ResVal) {
|
|
|
|
if (Slot == Record.size()) return true;
|
2007-05-06 03:23:14 +00:00
|
|
|
unsigned ValNo = (unsigned)Record[Slot++];
|
2007-05-06 00:00:00 +00:00
|
|
|
if (ValNo < InstNum) {
|
|
|
|
// If this is not a forward reference, just return the value we already
|
|
|
|
// have.
|
|
|
|
ResVal = getFnValueByID(ValNo, 0);
|
|
|
|
return ResVal == 0;
|
|
|
|
} else if (Slot == Record.size()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-05-06 03:23:14 +00:00
|
|
|
unsigned TypeNo = (unsigned)Record[Slot++];
|
2007-05-06 00:00:00 +00:00
|
|
|
ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
|
|
|
|
return ResVal == 0;
|
|
|
|
}
|
|
|
|
bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *Ty, Value *&ResVal) {
|
2007-05-06 00:00:00 +00:00
|
|
|
if (Slot == Record.size()) return true;
|
2007-05-06 03:23:14 +00:00
|
|
|
unsigned ValNo = (unsigned)Record[Slot++];
|
2007-05-06 00:00:00 +00:00
|
|
|
ResVal = getFnValueByID(ValNo, Ty);
|
|
|
|
return ResVal == 0;
|
|
|
|
}
|
2007-05-04 03:30:17 +00:00
|
|
|
|
2007-04-22 06:23:29 +00:00
|
|
|
|
2010-01-27 20:34:15 +00:00
|
|
|
bool ParseModule();
|
2008-09-25 21:00:45 +00:00
|
|
|
bool ParseAttributeBlock();
|
2007-05-01 05:01:34 +00:00
|
|
|
bool ParseTypeTable();
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@134829 91177308-0d34-0410-b5e6-96231b3b80d8
2011-07-09 17:41:24 +00:00
|
|
|
bool ParseOldTypeTable(); // FIXME: Remove in LLVM 3.1
|
|
|
|
bool ParseTypeTableBody();
|
|
|
|
|
|
|
|
bool ParseOldTypeSymbolTable(); // FIXME: Remove in LLVM 3.1
|
2007-05-01 05:01:34 +00:00
|
|
|
bool ParseValueSymbolTable();
|
|
|
|
bool ParseConstants();
|
2007-05-01 05:52:21 +00:00
|
|
|
bool RememberAndSkipFunctionBody();
|
|
|
|
bool ParseFunctionBody(Function *F);
|
2007-04-26 02:46:40 +00:00
|
|
|
bool ResolveGlobalAndAliasInits();
|
2009-07-22 17:43:22 +00:00
|
|
|
bool ParseMetadata();
|
2009-09-18 19:26:43 +00:00
|
|
|
bool ParseMetadataAttachment();
|
2010-10-06 01:22:42 +00:00
|
|
|
bool ParseModuleTriple(std::string &Triple);
|
2007-04-22 06:23:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // End llvm namespace
|
|
|
|
|
|
|
|
#endif
|