mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-07-25 13:24:46 +00:00
Use IntrusiveRefCntPtr to manage the lifetime of BitCodeAbbrevs.
This doesn't change the interface or gives additional safety but removes a ton of retain/release boilerplate. No functionality change. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217778 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -18,6 +18,7 @@
|
|||||||
#ifndef LLVM_BITCODE_BITCODES_H
|
#ifndef LLVM_BITCODE_BITCODES_H
|
||||||
#define LLVM_BITCODE_BITCODES_H
|
#define LLVM_BITCODE_BITCODES_H
|
||||||
|
|
||||||
|
#include "llvm/ADT/IntrusiveRefCntPtr.h"
|
||||||
#include "llvm/ADT/SmallVector.h"
|
#include "llvm/ADT/SmallVector.h"
|
||||||
#include "llvm/Support/DataTypes.h"
|
#include "llvm/Support/DataTypes.h"
|
||||||
#include "llvm/Support/ErrorHandling.h"
|
#include "llvm/Support/ErrorHandling.h"
|
||||||
@@ -161,16 +162,12 @@ template <> struct isPodLike<BitCodeAbbrevOp> { static const bool value=true; };
|
|||||||
/// BitCodeAbbrev - This class represents an abbreviation record. An
|
/// BitCodeAbbrev - This class represents an abbreviation record. An
|
||||||
/// abbreviation allows a complex record that has redundancy to be stored in a
|
/// abbreviation allows a complex record that has redundancy to be stored in a
|
||||||
/// specialized format instead of the fully-general, fully-vbr, format.
|
/// specialized format instead of the fully-general, fully-vbr, format.
|
||||||
class BitCodeAbbrev {
|
class BitCodeAbbrev : public RefCountedBase<BitCodeAbbrev> {
|
||||||
SmallVector<BitCodeAbbrevOp, 32> OperandList;
|
SmallVector<BitCodeAbbrevOp, 32> OperandList;
|
||||||
unsigned char RefCount; // Number of things using this.
|
|
||||||
~BitCodeAbbrev() {}
|
~BitCodeAbbrev() {}
|
||||||
|
friend class RefCountedBase; // Only RefCountedBase is allowed to delete.
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BitCodeAbbrev() : RefCount(1) {}
|
|
||||||
|
|
||||||
void addRef() { ++RefCount; }
|
|
||||||
void dropRef() { if (--RefCount == 0) delete this; }
|
|
||||||
|
|
||||||
unsigned getNumOperandInfos() const {
|
unsigned getNumOperandInfos() const {
|
||||||
return static_cast<unsigned>(OperandList.size());
|
return static_cast<unsigned>(OperandList.size());
|
||||||
}
|
}
|
||||||
|
@@ -37,7 +37,7 @@ public:
|
|||||||
/// These describe abbreviations that all blocks of the specified ID inherit.
|
/// These describe abbreviations that all blocks of the specified ID inherit.
|
||||||
struct BlockInfo {
|
struct BlockInfo {
|
||||||
unsigned BlockID;
|
unsigned BlockID;
|
||||||
std::vector<BitCodeAbbrev*> Abbrevs;
|
std::vector<IntrusiveRefCntPtr<BitCodeAbbrev>> Abbrevs;
|
||||||
std::string Name;
|
std::string Name;
|
||||||
|
|
||||||
std::vector<std::pair<unsigned, std::string> > RecordNames;
|
std::vector<std::pair<unsigned, std::string> > RecordNames;
|
||||||
@@ -86,18 +86,6 @@ public:
|
|||||||
|
|
||||||
StreamableMemoryObject &getBitcodeBytes() { return *BitcodeBytes; }
|
StreamableMemoryObject &getBitcodeBytes() { return *BitcodeBytes; }
|
||||||
|
|
||||||
~BitstreamReader() {
|
|
||||||
// Free the BlockInfoRecords.
|
|
||||||
while (!BlockInfoRecords.empty()) {
|
|
||||||
BlockInfo &Info = BlockInfoRecords.back();
|
|
||||||
// Free blockinfo abbrev info.
|
|
||||||
for (unsigned i = 0, e = static_cast<unsigned>(Info.Abbrevs.size());
|
|
||||||
i != e; ++i)
|
|
||||||
Info.Abbrevs[i]->dropRef();
|
|
||||||
BlockInfoRecords.pop_back();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// CollectBlockInfoNames - This is called by clients that want block/record
|
/// CollectBlockInfoNames - This is called by clients that want block/record
|
||||||
/// name information.
|
/// name information.
|
||||||
void CollectBlockInfoNames() { IgnoreBlockInfoNames = false; }
|
void CollectBlockInfoNames() { IgnoreBlockInfoNames = false; }
|
||||||
@@ -208,11 +196,11 @@ class BitstreamCursor {
|
|||||||
unsigned CurCodeSize;
|
unsigned CurCodeSize;
|
||||||
|
|
||||||
/// CurAbbrevs - Abbrevs installed at in this block.
|
/// CurAbbrevs - Abbrevs installed at in this block.
|
||||||
std::vector<BitCodeAbbrev*> CurAbbrevs;
|
std::vector<IntrusiveRefCntPtr<BitCodeAbbrev>> CurAbbrevs;
|
||||||
|
|
||||||
struct Block {
|
struct Block {
|
||||||
unsigned PrevCodeSize;
|
unsigned PrevCodeSize;
|
||||||
std::vector<BitCodeAbbrev*> PrevAbbrevs;
|
std::vector<IntrusiveRefCntPtr<BitCodeAbbrev>> PrevAbbrevs;
|
||||||
explicit Block(unsigned PCS) : PrevCodeSize(PCS) {}
|
explicit Block(unsigned PCS) : PrevCodeSize(PCS) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -222,10 +210,6 @@ class BitstreamCursor {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
BitstreamCursor() : BitStream(nullptr), NextChar(0) {}
|
BitstreamCursor() : BitStream(nullptr), NextChar(0) {}
|
||||||
BitstreamCursor(const BitstreamCursor &RHS)
|
|
||||||
: BitStream(nullptr), NextChar(0) {
|
|
||||||
operator=(RHS);
|
|
||||||
}
|
|
||||||
|
|
||||||
explicit BitstreamCursor(BitstreamReader &R) : BitStream(&R) {
|
explicit BitstreamCursor(BitstreamReader &R) : BitStream(&R) {
|
||||||
NextChar = 0;
|
NextChar = 0;
|
||||||
@@ -244,12 +228,6 @@ public:
|
|||||||
CurCodeSize = 2;
|
CurCodeSize = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
~BitstreamCursor() {
|
|
||||||
freeState();
|
|
||||||
}
|
|
||||||
|
|
||||||
void operator=(const BitstreamCursor &RHS);
|
|
||||||
|
|
||||||
void freeState();
|
void freeState();
|
||||||
|
|
||||||
bool isEndPos(size_t pos) {
|
bool isEndPos(size_t pos) {
|
||||||
@@ -529,12 +507,7 @@ private:
|
|||||||
void popBlockScope() {
|
void popBlockScope() {
|
||||||
CurCodeSize = BlockScope.back().PrevCodeSize;
|
CurCodeSize = BlockScope.back().PrevCodeSize;
|
||||||
|
|
||||||
// Delete abbrevs from popped scope.
|
CurAbbrevs = std::move(BlockScope.back().PrevAbbrevs);
|
||||||
for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size());
|
|
||||||
i != e; ++i)
|
|
||||||
CurAbbrevs[i]->dropRef();
|
|
||||||
|
|
||||||
BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
|
|
||||||
BlockScope.pop_back();
|
BlockScope.pop_back();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -555,7 +528,7 @@ public:
|
|||||||
const BitCodeAbbrev *getAbbrev(unsigned AbbrevID) {
|
const BitCodeAbbrev *getAbbrev(unsigned AbbrevID) {
|
||||||
unsigned AbbrevNo = AbbrevID-bitc::FIRST_APPLICATION_ABBREV;
|
unsigned AbbrevNo = AbbrevID-bitc::FIRST_APPLICATION_ABBREV;
|
||||||
assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!");
|
assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!");
|
||||||
return CurAbbrevs[AbbrevNo];
|
return CurAbbrevs[AbbrevNo].get();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// skipRecord - Read the current record and discard it.
|
/// skipRecord - Read the current record and discard it.
|
||||||
|
@@ -40,12 +40,12 @@ class BitstreamWriter {
|
|||||||
unsigned BlockInfoCurBID;
|
unsigned BlockInfoCurBID;
|
||||||
|
|
||||||
/// CurAbbrevs - Abbrevs installed at in this block.
|
/// CurAbbrevs - Abbrevs installed at in this block.
|
||||||
std::vector<BitCodeAbbrev*> CurAbbrevs;
|
std::vector<IntrusiveRefCntPtr<BitCodeAbbrev>> CurAbbrevs;
|
||||||
|
|
||||||
struct Block {
|
struct Block {
|
||||||
unsigned PrevCodeSize;
|
unsigned PrevCodeSize;
|
||||||
unsigned StartSizeWord;
|
unsigned StartSizeWord;
|
||||||
std::vector<BitCodeAbbrev*> PrevAbbrevs;
|
std::vector<IntrusiveRefCntPtr<BitCodeAbbrev>> PrevAbbrevs;
|
||||||
Block(unsigned PCS, unsigned SSW) : PrevCodeSize(PCS), StartSizeWord(SSW) {}
|
Block(unsigned PCS, unsigned SSW) : PrevCodeSize(PCS), StartSizeWord(SSW) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -56,7 +56,7 @@ class BitstreamWriter {
|
|||||||
/// These describe abbreviations that all blocks of the specified ID inherit.
|
/// These describe abbreviations that all blocks of the specified ID inherit.
|
||||||
struct BlockInfo {
|
struct BlockInfo {
|
||||||
unsigned BlockID;
|
unsigned BlockID;
|
||||||
std::vector<BitCodeAbbrev*> Abbrevs;
|
std::vector<IntrusiveRefCntPtr<BitCodeAbbrev>> Abbrevs;
|
||||||
};
|
};
|
||||||
std::vector<BlockInfo> BlockInfoRecords;
|
std::vector<BlockInfo> BlockInfoRecords;
|
||||||
|
|
||||||
@@ -99,16 +99,6 @@ public:
|
|||||||
~BitstreamWriter() {
|
~BitstreamWriter() {
|
||||||
assert(CurBit == 0 && "Unflushed data remaining");
|
assert(CurBit == 0 && "Unflushed data remaining");
|
||||||
assert(BlockScope.empty() && CurAbbrevs.empty() && "Block imbalance");
|
assert(BlockScope.empty() && CurAbbrevs.empty() && "Block imbalance");
|
||||||
|
|
||||||
// Free the BlockInfoRecords.
|
|
||||||
while (!BlockInfoRecords.empty()) {
|
|
||||||
BlockInfo &Info = BlockInfoRecords.back();
|
|
||||||
// Free blockinfo abbrev info.
|
|
||||||
for (unsigned i = 0, e = static_cast<unsigned>(Info.Abbrevs.size());
|
|
||||||
i != e; ++i)
|
|
||||||
Info.Abbrevs[i]->dropRef();
|
|
||||||
BlockInfoRecords.pop_back();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \brief Retrieve the current position in the stream, in bits.
|
/// \brief Retrieve the current position in the stream, in bits.
|
||||||
@@ -231,22 +221,13 @@ public:
|
|||||||
// If there is a blockinfo for this BlockID, add all the predefined abbrevs
|
// If there is a blockinfo for this BlockID, add all the predefined abbrevs
|
||||||
// to the abbrev list.
|
// to the abbrev list.
|
||||||
if (BlockInfo *Info = getBlockInfo(BlockID)) {
|
if (BlockInfo *Info = getBlockInfo(BlockID)) {
|
||||||
for (unsigned i = 0, e = static_cast<unsigned>(Info->Abbrevs.size());
|
CurAbbrevs.insert(CurAbbrevs.end(), Info->Abbrevs.begin(),
|
||||||
i != e; ++i) {
|
Info->Abbrevs.end());
|
||||||
CurAbbrevs.push_back(Info->Abbrevs[i]);
|
|
||||||
Info->Abbrevs[i]->addRef();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExitBlock() {
|
void ExitBlock() {
|
||||||
assert(!BlockScope.empty() && "Block scope imbalance!");
|
assert(!BlockScope.empty() && "Block scope imbalance!");
|
||||||
|
|
||||||
// Delete all abbrevs.
|
|
||||||
for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size());
|
|
||||||
i != e; ++i)
|
|
||||||
CurAbbrevs[i]->dropRef();
|
|
||||||
|
|
||||||
const Block &B = BlockScope.back();
|
const Block &B = BlockScope.back();
|
||||||
|
|
||||||
// Block tail:
|
// Block tail:
|
||||||
@@ -263,7 +244,7 @@ public:
|
|||||||
|
|
||||||
// Restore the inner block's code size and abbrev table.
|
// Restore the inner block's code size and abbrev table.
|
||||||
CurCodeSize = B.PrevCodeSize;
|
CurCodeSize = B.PrevCodeSize;
|
||||||
BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
|
CurAbbrevs = std::move(B.PrevAbbrevs);
|
||||||
BlockScope.pop_back();
|
BlockScope.pop_back();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -317,7 +298,7 @@ private:
|
|||||||
unsigned BlobLen = (unsigned) Blob.size();
|
unsigned BlobLen = (unsigned) Blob.size();
|
||||||
unsigned AbbrevNo = Abbrev-bitc::FIRST_APPLICATION_ABBREV;
|
unsigned AbbrevNo = Abbrev-bitc::FIRST_APPLICATION_ABBREV;
|
||||||
assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!");
|
assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!");
|
||||||
BitCodeAbbrev *Abbv = CurAbbrevs[AbbrevNo];
|
const BitCodeAbbrev *Abbv = CurAbbrevs[AbbrevNo].get();
|
||||||
|
|
||||||
EmitCode(Abbrev);
|
EmitCode(Abbrev);
|
||||||
|
|
||||||
|
@@ -15,41 +15,11 @@ using namespace llvm;
|
|||||||
// BitstreamCursor implementation
|
// BitstreamCursor implementation
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
void BitstreamCursor::operator=(const BitstreamCursor &RHS) {
|
|
||||||
freeState();
|
|
||||||
|
|
||||||
BitStream = RHS.BitStream;
|
|
||||||
NextChar = RHS.NextChar;
|
|
||||||
CurWord = RHS.CurWord;
|
|
||||||
BitsInCurWord = RHS.BitsInCurWord;
|
|
||||||
CurCodeSize = RHS.CurCodeSize;
|
|
||||||
|
|
||||||
// Copy abbreviations, and bump ref counts.
|
|
||||||
CurAbbrevs = RHS.CurAbbrevs;
|
|
||||||
for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i)
|
|
||||||
CurAbbrevs[i]->addRef();
|
|
||||||
|
|
||||||
// Copy block scope and bump ref counts.
|
|
||||||
BlockScope = RHS.BlockScope;
|
|
||||||
for (size_t S = 0, e = BlockScope.size(); S != e; ++S) {
|
|
||||||
std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
|
|
||||||
for (size_t i = 0, e = Abbrevs.size(); i != e; ++i)
|
|
||||||
Abbrevs[i]->addRef();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void BitstreamCursor::freeState() {
|
void BitstreamCursor::freeState() {
|
||||||
// Free all the Abbrevs.
|
// Free all the Abbrevs.
|
||||||
for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i)
|
|
||||||
CurAbbrevs[i]->dropRef();
|
|
||||||
CurAbbrevs.clear();
|
CurAbbrevs.clear();
|
||||||
|
|
||||||
// Free all the Abbrevs in the block scope.
|
// Free all the Abbrevs in the block scope.
|
||||||
for (size_t S = 0, e = BlockScope.size(); S != e; ++S) {
|
|
||||||
std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
|
|
||||||
for (size_t i = 0, e = Abbrevs.size(); i != e; ++i)
|
|
||||||
Abbrevs[i]->dropRef();
|
|
||||||
}
|
|
||||||
BlockScope.clear();
|
BlockScope.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,10 +33,8 @@ bool BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
|
|||||||
// Add the abbrevs specific to this block to the CurAbbrevs list.
|
// Add the abbrevs specific to this block to the CurAbbrevs list.
|
||||||
if (const BitstreamReader::BlockInfo *Info =
|
if (const BitstreamReader::BlockInfo *Info =
|
||||||
BitStream->getBlockInfo(BlockID)) {
|
BitStream->getBlockInfo(BlockID)) {
|
||||||
for (size_t i = 0, e = Info->Abbrevs.size(); i != e; ++i) {
|
CurAbbrevs.insert(CurAbbrevs.end(), Info->Abbrevs.begin(),
|
||||||
CurAbbrevs.push_back(Info->Abbrevs[i]);
|
Info->Abbrevs.end());
|
||||||
CurAbbrevs.back()->addRef();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the codesize of this block.
|
// Get the codesize of this block.
|
||||||
@@ -339,9 +307,8 @@ bool BitstreamCursor::ReadBlockInfoBlock() {
|
|||||||
|
|
||||||
// ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the
|
// ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the
|
||||||
// appropriate BlockInfo.
|
// appropriate BlockInfo.
|
||||||
BitCodeAbbrev *Abbv = CurAbbrevs.back();
|
CurBlockInfo->Abbrevs.push_back(std::move(CurAbbrevs.back()));
|
||||||
CurAbbrevs.pop_back();
|
CurAbbrevs.pop_back();
|
||||||
CurBlockInfo->Abbrevs.push_back(Abbv);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user