14 #ifndef LLVM_BITSTREAM_BITSTREAMWRITER_H 15 #define LLVM_BITSTREAM_BITSTREAMWRITER_H 42 unsigned BlockInfoCurBID;
45 std::vector<std::shared_ptr<BitCodeAbbrev>> CurAbbrevs;
48 unsigned PrevCodeSize;
50 std::vector<std::shared_ptr<BitCodeAbbrev>> PrevAbbrevs;
51 Block(
unsigned PCS,
size_t SSW) : PrevCodeSize(PCS), StartSizeWord(SSW) {}
55 std::vector<Block> BlockScope;
61 std::vector<std::shared_ptr<BitCodeAbbrev>> Abbrevs;
63 std::vector<BlockInfo> BlockInfoRecords;
65 void WriteByte(
unsigned char Value) {
69 void WriteWord(
unsigned Value) {
70 Value = support::endian::byte_swap<uint32_t, support::little>(
Value);
71 Out.
append(reinterpret_cast<const char *>(&Value),
72 reinterpret_cast<const char *>(&Value + 1));
75 size_t GetBufferOffset()
const {
return Out.
size(); }
77 size_t GetWordIndex()
const {
78 size_t Offset = GetBufferOffset();
79 assert((Offset & 3) == 0 &&
"Not 32-bit aligned");
85 : Out(O), CurBit(0), CurValue(0), CurCodeSize(2) {}
88 assert(CurBit == 0 &&
"Unflushed data remaining");
89 assert(BlockScope.empty() && CurAbbrevs.empty() &&
"Block imbalance");
106 unsigned ByteNo = BitNo / 8;
107 assert((!endian::readAtBitAlignment<uint32_t, little, unaligned>(
108 &Out[ByteNo], BitNo & 7)) &&
109 "Expected to be patching over 0-value placeholders");
110 endian::writeAtBitAlignment<uint32_t, little, unaligned>(
111 &Out[ByteNo], NewWord, BitNo & 7);
120 assert(NumBits && NumBits <= 32 &&
"Invalid value size!");
121 assert((Val & ~(~0U >> (32-NumBits))) == 0 &&
"High bits set!");
122 CurValue |= Val << CurBit;
123 if (CurBit + NumBits < 32) {
132 CurValue = Val >> (32-CurBit);
135 CurBit = (CurBit+NumBits) & 31;
147 assert(NumBits <= 32 &&
"Too many bits to emit!");
151 while (Val >= Threshold) {
152 Emit((Val & ((1 << (NumBits-1))-1)) | (1 << (NumBits-1)), NumBits);
160 assert(NumBits <= 32 &&
"Too many bits to emit!");
167 while (Val >= Threshold) {
169 (1 << (NumBits-1)), NumBits);
178 Emit(Val, CurCodeSize);
189 if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID)
190 return &BlockInfoRecords.back();
192 for (
unsigned i = 0,
e = static_cast<unsigned>(BlockInfoRecords.size());
194 if (BlockInfoRecords[i].BlockID == BlockID)
195 return &BlockInfoRecords[i];
207 size_t BlockSizeWordIndex = GetWordIndex();
208 unsigned OldCodeSize = CurCodeSize;
213 CurCodeSize = CodeLen;
217 BlockScope.emplace_back(OldCodeSize, BlockSizeWordIndex);
218 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
223 CurAbbrevs.insert(CurAbbrevs.end(),
Info->Abbrevs.begin(),
224 Info->Abbrevs.end());
229 assert(!BlockScope.empty() &&
"Block scope imbalance!");
230 const Block &
B = BlockScope.back();
238 size_t SizeInWords = GetWordIndex() - B.StartSizeWord - 1;
239 uint64_t BitNo = uint64_t(B.StartSizeWord) * 32;
245 CurCodeSize = B.PrevCodeSize;
246 CurAbbrevs = std::move(B.PrevAbbrevs);
247 BlockScope.pop_back();
257 template<
typename u
intty>
263 "Invalid abbrev for record!");
268 template<
typename u
intty>
270 assert(!Op.
isLiteral() &&
"Literals should use EmitAbbreviatedLiteral!");
295 template <
typename u
intty>
298 const char *BlobData = Blob.
data();
301 assert(AbbrevNo < CurAbbrevs.size() &&
"Invalid abbrev #!");
306 unsigned i = 0,
e =
static_cast<unsigned>(Abbv->getNumOperandInfos());
308 assert(
e &&
"Expected non-empty abbreviation");
312 EmitAbbreviatedLiteral(Op, Code.
getValue());
316 "Expected literal or scalar");
317 EmitAbbreviatedField(Op, Code.
getValue());
321 unsigned RecordIdx = 0;
322 for (; i !=
e; ++i) {
325 assert(RecordIdx < Vals.
size() &&
"Invalid abbrev/record");
326 EmitAbbreviatedLiteral(Op, Vals[RecordIdx]);
330 assert(i + 2 ==
e &&
"array op not second to last?");
337 "Blob data and record entries specified for array!");
339 EmitVBR(static_cast<uint32_t>(BlobLen), 6);
342 for (
unsigned i = 0; i != BlobLen; ++i)
343 EmitAbbreviatedField(EltEnc, (
unsigned char)BlobData[i]);
349 EmitVBR(static_cast<uint32_t>(Vals.
size()-RecordIdx), 6);
352 for (
unsigned e = Vals.
size(); RecordIdx !=
e; ++RecordIdx)
353 EmitAbbreviatedField(EltEnc, Vals[RecordIdx]);
361 "Blob data and record entries specified for blob operand!");
363 assert(Blob.
data() == BlobData &&
"BlobData got moved");
364 assert(Blob.
size() == BlobLen &&
"BlobLen got changed");
371 assert(RecordIdx < Vals.
size() &&
"Invalid abbrev/record");
372 EmitAbbreviatedField(Op, Vals[RecordIdx]);
376 assert(RecordIdx == Vals.
size() &&
"Not all record operands emitted!");
377 assert(BlobData ==
nullptr &&
378 "Blob data specified for record that doesn't use it!");
383 template <
class UIntTy>
393 for (
const auto &
B : Bytes) {
395 WriteByte((
unsigned char)
B);
399 while (GetBufferOffset() & 3)
409 template <
typename Container>
410 void EmitRecord(
unsigned Code,
const Container &Vals,
unsigned Abbrev = 0) {
418 for (
unsigned i = 0,
e = Count; i !=
e; ++i)
429 template <
typename Container>
439 template <
typename Container>
444 template <
typename Container>
446 const char *BlobData,
unsigned BlobLen) {
447 return EmitRecordWithAbbrevImpl(Abbrev,
makeArrayRef(Vals),
453 template <
typename Container>
458 template <
typename Container>
460 const char *ArrayData,
unsigned ArrayLen) {
461 return EmitRecordWithAbbrevImpl(Abbrev,
makeArrayRef(Vals),
492 CurAbbrevs.push_back(std::move(Abbv));
493 return static_cast<unsigned>(CurAbbrevs.size())-1 +
504 BlockInfoCurBID = ~0U;
505 BlockInfoRecords.clear();
510 void SwitchToBlockID(
unsigned BlockID) {
511 if (BlockInfoCurBID == BlockID)
return;
515 BlockInfoCurBID = BlockID;
518 BlockInfo &getOrCreateBlockInfo(
unsigned BlockID) {
523 BlockInfoRecords.emplace_back();
524 BlockInfoRecords.back().BlockID = BlockID;
525 return BlockInfoRecords.back();
533 SwitchToBlockID(BlockID);
537 BlockInfo &
Info = getOrCreateBlockInfo(BlockID);
538 Info.Abbrevs.push_back(std::move(Abbv));
Encoding getEncoding() const
BLOCKINFO_BLOCK is used to define metadata about blocks, for example, standard abbrevs that should be...
This class represents lattice values for constants.
void push_back(const T &Elt)
BitCodeAbbrev - This class represents an abbreviation record.
uint64_t GetCurrentBitNo() const
Retrieve the current position in the stream, in bits.
unsigned GetAbbrevIDWidth() const
Retrieve the number of bits currently used to encode an abbrev ID.
uint64_t getLiteralValue() const
static unsigned EncodeChar6(char C)
void EmitRecordWithArray(unsigned Abbrev, const Container &Vals, const char *ArrayData, unsigned ArrayLen)
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
void BackpatchWord(uint64_t BitNo, unsigned NewWord)
Backpatch a 32-bit word in the output at the given bit offset with the specified value.
void Emit(uint32_t Val, unsigned NumBits)
void EnterBlockInfoBlock()
EnterBlockInfoBlock - Start emitting the BLOCKINFO_BLOCK.
void EmitRecordWithAbbrev(unsigned Abbrev, const Container &Vals)
EmitRecordWithAbbrev - Emit a record with the specified abbreviation.
const T & getValue() const LLVM_LVALUE_FUNCTION
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
LLVM_NODISCARD size_t size() const
size - Get the string size.
const BitCodeAbbrevOp & getOperandInfo(unsigned N) const
void EmitRecord(unsigned Code, const Container &Vals, unsigned Abbrev=0)
EmitRecord - Emit the specified record to the stream, using an abbrev if we have one to compress the ...
void EnterSubblock(unsigned BlockID, unsigned CodeLen)
Analysis containing CSE Info
void BackpatchWord64(uint64_t BitNo, uint64_t Val)
void EmitCode(unsigned Val)
EmitCode - Emit the specified code.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
constexpr bool isUInt< 8 >(uint64_t x)
size_t size() const
size - Get the array size.
void EmitRecordWithBlob(unsigned Abbrev, const Container &Vals, const char *BlobData, unsigned BlobLen)
BitCodeAbbrevOp - This describes one or more operands in an abbreviation.
void EmitVBR(uint32_t Val, unsigned NumBits)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void emitBlob(StringRef Bytes, bool ShouldEmitSize=true)
unsigned EmitAbbrev(std::shared_ptr< BitCodeAbbrev > Abbv)
Emits the abbreviation Abbv to the stream.
BlockInfo * getBlockInfo(unsigned BlockID)
getBlockInfo - If there is block info for the specified ID, return it, otherwise return null...
void EmitRecordWithArray(unsigned Abbrev, const Container &Vals, StringRef Array)
EmitRecordWithArray - Just like EmitRecordWithBlob, works with records that end with an array...
void EmitRecordWithBlob(unsigned Abbrev, const Container &Vals, StringRef Blob)
EmitRecordWithBlob - Emit the specified record to the stream, using an abbrev that includes a blob at...
DEFINE_ABBREV - Defines an abbrev for the current block.
static cl::opt< unsigned > Threshold("loop-unswitch-threshold", cl::desc("Max loop size to unswitch"), cl::init(100), cl::Hidden)
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array...
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
unsigned getNumOperandInfos() const
uint64_t getEncodingData() const
BitstreamWriter(SmallVectorImpl< char > &O)
bool hasEncodingData() const
void EmitVBR64(uint64_t Val, unsigned NumBits)
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
StringRef - Represent a constant reference to a string, i.e.
void emitBlob(ArrayRef< UIntTy > Bytes, bool ShouldEmitSize=true)
Emit a blob, including flushing before and tail-padding.
unsigned EmitBlockInfoAbbrev(unsigned BlockID, std::shared_ptr< BitCodeAbbrev > Abbv)
EmitBlockInfoAbbrev - Emit a DEFINE_ABBREV record for the specified BlockID.