Line data Source code
1 : //===- BitstreamWriter.h - Low-level bitstream writer interface -*- C++ -*-===//
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // License. See LICENSE.TXT for details.
7 : //
8 : //===----------------------------------------------------------------------===//
9 : //
10 : // This header defines the BitstreamWriter class. This class can be used to
11 : // write an arbitrary bitstream, regardless of its contents.
12 : //
13 : //===----------------------------------------------------------------------===//
14 :
15 : #ifndef LLVM_BITCODE_BITSTREAMWRITER_H
16 : #define LLVM_BITCODE_BITSTREAMWRITER_H
17 :
18 : #include "llvm/ADT/ArrayRef.h"
19 : #include "llvm/ADT/Optional.h"
20 : #include "llvm/ADT/SmallVector.h"
21 : #include "llvm/ADT/StringRef.h"
22 : #include "llvm/Bitcode/BitCodes.h"
23 : #include "llvm/Support/Endian.h"
24 : #include <vector>
25 :
26 : namespace llvm {
27 :
28 : class BitstreamWriter {
29 : SmallVectorImpl<char> &Out;
30 :
31 : /// CurBit - Always between 0 and 31 inclusive, specifies the next bit to use.
32 : unsigned CurBit;
33 :
34 : /// CurValue - The current value. Only bits < CurBit are valid.
35 : uint32_t CurValue;
36 :
37 : /// CurCodeSize - This is the declared size of code values used for the
38 : /// current block, in bits.
39 : unsigned CurCodeSize;
40 :
41 : /// BlockInfoCurBID - When emitting a BLOCKINFO_BLOCK, this is the currently
42 : /// selected BLOCK ID.
43 : unsigned BlockInfoCurBID;
44 :
45 : /// CurAbbrevs - Abbrevs installed at in this block.
46 : std::vector<std::shared_ptr<BitCodeAbbrev>> CurAbbrevs;
47 :
48 149921 : struct Block {
49 : unsigned PrevCodeSize;
50 : size_t StartSizeWord;
51 : std::vector<std::shared_ptr<BitCodeAbbrev>> PrevAbbrevs;
52 121399 : Block(unsigned PCS, size_t SSW) : PrevCodeSize(PCS), StartSizeWord(SSW) {}
53 : };
54 :
55 : /// BlockScope - This tracks the current blocks that we have entered.
56 : std::vector<Block> BlockScope;
57 :
58 : /// BlockInfo - This contains information emitted to BLOCKINFO_BLOCK blocks.
59 : /// These describe abbreviations that all blocks of the specified ID inherit.
60 42727 : struct BlockInfo {
61 : unsigned BlockID;
62 : std::vector<std::shared_ptr<BitCodeAbbrev>> Abbrevs;
63 : };
64 : std::vector<BlockInfo> BlockInfoRecords;
65 :
66 : void WriteByte(unsigned char Value) {
67 62641 : Out.push_back(Value);
68 : }
69 :
70 0 : void WriteWord(unsigned Value) {
71 : Value = support::endian::byte_swap<uint32_t, support::little>(Value);
72 0 : Out.append(reinterpret_cast<const char *>(&Value),
73 : reinterpret_cast<const char *>(&Value + 1));
74 0 : }
75 :
76 6180424 : size_t GetBufferOffset() const { return Out.size(); }
77 :
78 : size_t GetWordIndex() const {
79 242798 : size_t Offset = GetBufferOffset();
80 : assert((Offset & 3) == 0 && "Not 32-bit aligned");
81 242798 : return Offset / 4;
82 : }
83 :
84 : public:
85 : explicit BitstreamWriter(SmallVectorImpl<char> &O)
86 10955 : : Out(O), CurBit(0), CurValue(0), CurCodeSize(2) {}
87 :
88 10944 : ~BitstreamWriter() {
89 : assert(CurBit == 0 && "Unflushed data remaining");
90 : assert(BlockScope.empty() && CurAbbrevs.empty() && "Block imbalance");
91 10944 : }
92 :
93 : /// Retrieve the current position in the stream, in bits.
94 9147742 : uint64_t GetCurrentBitNo() const { return GetBufferOffset() * 8 + CurBit; }
95 :
96 : /// Retrieve the number of bits currently used to encode an abbrev ID.
97 : unsigned GetAbbrevIDWidth() const { return CurCodeSize; }
98 :
99 : //===--------------------------------------------------------------------===//
100 : // Basic Primitives for emitting bits to the stream.
101 : //===--------------------------------------------------------------------===//
102 :
103 : /// Backpatch a 32-bit word in the output at the given bit offset
104 : /// with the specified value.
105 0 : void BackpatchWord(uint64_t BitNo, unsigned NewWord) {
106 : using namespace llvm::support;
107 126375 : unsigned ByteNo = BitNo / 8;
108 : assert((!endian::readAtBitAlignment<uint32_t, little, unaligned>(
109 : &Out[ByteNo], BitNo & 7)) &&
110 : "Expected to be patching over 0-value placeholders");
111 130949 : endian::writeAtBitAlignment<uint32_t, little, unaligned>(
112 0 : &Out[ByteNo], NewWord, BitNo & 7);
113 0 : }
114 :
115 134 : void BackpatchWord64(uint64_t BitNo, uint64_t Val) {
116 134 : BackpatchWord(BitNo, (uint32_t)Val);
117 134 : BackpatchWord(BitNo + 32, (uint32_t)(Val >> 32));
118 134 : }
119 :
120 177057074 : void Emit(uint32_t Val, unsigned NumBits) {
121 : assert(NumBits && NumBits <= 32 && "Invalid value size!");
122 : assert((Val & ~(~0U >> (32-NumBits))) == 0 && "High bits set!");
123 177057074 : CurValue |= Val << CurBit;
124 177057074 : if (CurBit + NumBits < 32) {
125 145511379 : CurBit += NumBits;
126 145511379 : return;
127 : }
128 :
129 : // Add the current word.
130 31545695 : WriteWord(CurValue);
131 :
132 31545700 : if (CurBit)
133 31423828 : CurValue = Val >> (32-CurBit);
134 : else
135 121872 : CurValue = 0;
136 31545700 : CurBit = (CurBit+NumBits) & 31;
137 : }
138 :
139 433089 : void FlushToWord() {
140 433089 : if (CurBit) {
141 419458 : WriteWord(CurValue);
142 419458 : CurBit = 0;
143 419458 : CurValue = 0;
144 : }
145 433089 : }
146 :
147 91871108 : void EmitVBR(uint32_t Val, unsigned NumBits) {
148 : assert(NumBits <= 32 && "Too many bits to emit!");
149 91871108 : uint32_t Threshold = 1U << (NumBits-1);
150 :
151 : // Emit the bits with VBR encoding, NumBits-1 bits at a time.
152 158021907 : while (Val >= Threshold) {
153 66150804 : Emit((Val & ((1 << (NumBits-1))-1)) | (1 << (NumBits-1)), NumBits);
154 66150799 : Val >>= NumBits-1;
155 : }
156 :
157 91871103 : Emit(Val, NumBits);
158 91871097 : }
159 :
160 78534959 : void EmitVBR64(uint64_t Val, unsigned NumBits) {
161 : assert(NumBits <= 32 && "Too many bits to emit!");
162 78534959 : if ((uint32_t)Val == Val)
163 78532589 : return EmitVBR((uint32_t)Val, NumBits);
164 :
165 2370 : uint32_t Threshold = 1U << (NumBits-1);
166 :
167 : // Emit the bits with VBR encoding, NumBits-1 bits at a time.
168 30287 : while (Val >= Threshold) {
169 55834 : Emit(((uint32_t)Val & ((1 << (NumBits-1))-1)) |
170 27917 : (1 << (NumBits-1)), NumBits);
171 27917 : Val >>= NumBits-1;
172 : }
173 :
174 2370 : Emit((uint32_t)Val, NumBits);
175 : }
176 :
177 : /// EmitCode - Emit the specified code.
178 : void EmitCode(unsigned Val) {
179 6115010 : Emit(Val, CurCodeSize);
180 : }
181 :
182 : //===--------------------------------------------------------------------===//
183 : // Block Manipulation
184 : //===--------------------------------------------------------------------===//
185 :
186 : /// getBlockInfo - If there is block info for the specified ID, return it,
187 : /// otherwise return null.
188 : BlockInfo *getBlockInfo(unsigned BlockID) {
189 : // Common case, the most recent entry matches BlockID.
190 197369 : if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID)
191 : return &BlockInfoRecords.back();
192 :
193 247979 : for (unsigned i = 0, e = static_cast<unsigned>(BlockInfoRecords.size());
194 247979 : i != e; ++i)
195 293734 : if (BlockInfoRecords[i].BlockID == BlockID)
196 : return &BlockInfoRecords[i];
197 : return nullptr;
198 : }
199 :
200 121398 : void EnterSubblock(unsigned BlockID, unsigned CodeLen) {
201 : // Block header:
202 : // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen]
203 : EmitCode(bitc::ENTER_SUBBLOCK);
204 121399 : EmitVBR(BlockID, bitc::BlockIDWidth);
205 121399 : EmitVBR(CodeLen, bitc::CodeLenWidth);
206 121399 : FlushToWord();
207 :
208 121399 : size_t BlockSizeWordIndex = GetWordIndex();
209 121399 : unsigned OldCodeSize = CurCodeSize;
210 :
211 : // Emit a placeholder, which will be replaced when the block is popped.
212 121399 : Emit(0, bitc::BlockSizeWidth);
213 :
214 121399 : CurCodeSize = CodeLen;
215 :
216 : // Push the outer block's abbrev set onto the stack, start out with an
217 : // empty abbrev set.
218 121399 : BlockScope.emplace_back(OldCodeSize, BlockSizeWordIndex);
219 121399 : BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
220 :
221 : // If there is a blockinfo for this BlockID, add all the predefined abbrevs
222 : // to the abbrev list.
223 121399 : if (BlockInfo *Info = getBlockInfo(BlockID)) {
224 : CurAbbrevs.insert(CurAbbrevs.end(), Info->Abbrevs.begin(),
225 34574 : Info->Abbrevs.end());
226 : }
227 121399 : }
228 :
229 121399 : void ExitBlock() {
230 : assert(!BlockScope.empty() && "Block scope imbalance!");
231 : const Block &B = BlockScope.back();
232 :
233 : // Block tail:
234 : // [END_BLOCK, <align4bytes>]
235 : EmitCode(bitc::END_BLOCK);
236 121399 : FlushToWord();
237 :
238 : // Compute the size of the block, in words, not counting the size field.
239 121399 : size_t SizeInWords = GetWordIndex() - B.StartSizeWord - 1;
240 121399 : uint64_t BitNo = uint64_t(B.StartSizeWord) * 32;
241 :
242 : // Update the block size field in the header of this sub-block.
243 121399 : BackpatchWord(BitNo, SizeInWords);
244 :
245 : // Restore the inner block's code size and abbrev table.
246 121399 : CurCodeSize = B.PrevCodeSize;
247 121399 : CurAbbrevs = std::move(B.PrevAbbrevs);
248 : BlockScope.pop_back();
249 121399 : }
250 :
251 : //===--------------------------------------------------------------------===//
252 : // Record Emission
253 : //===--------------------------------------------------------------------===//
254 :
255 : private:
256 : /// EmitAbbreviatedLiteral - Emit a literal value according to its abbrev
257 : /// record. This is a no-op, since the abbrev specifies the literal to use.
258 : template<typename uintty>
259 0 : void EmitAbbreviatedLiteral(const BitCodeAbbrevOp &Op, uintty V) {
260 : assert(Op.isLiteral() && "Not a literal");
261 : // If the abbrev specifies the literal value to use, don't emit
262 : // anything.
263 : assert(V == Op.getLiteralValue() &&
264 : "Invalid abbrev for record!");
265 0 : }
266 0 :
267 : /// EmitAbbreviatedField - Emit a single scalar field value with the specified
268 : /// encoding.
269 : template<typename uintty>
270 417 : void EmitAbbreviatedField(const BitCodeAbbrevOp &Op, uintty V) {
271 : assert(!Op.isLiteral() && "Literals should use EmitAbbreviatedLiteral!");
272 0 :
273 0 : // Encode the value as we are commanded.
274 417 : switch (Op.getEncoding()) {
275 0 : default: llvm_unreachable("Unknown encoding!");
276 417 : case BitCodeAbbrevOp::Fixed:
277 417 : if (Op.getEncodingData())
278 417 : Emit((unsigned)V, (unsigned)Op.getEncodingData());
279 0 : break;
280 0 : case BitCodeAbbrevOp::VBR:
281 0 : if (Op.getEncodingData())
282 0 : EmitVBR64(V, (unsigned)Op.getEncodingData());
283 : break;
284 11423219 : case BitCodeAbbrevOp::Char6:
285 0 : Emit(BitCodeAbbrevOp::EncodeChar6((char)V), 6);
286 0 : break;
287 : }
288 11423636 : }
289 0 :
290 6319735 : /// EmitRecordWithAbbrevImpl - This is the core implementation of the record
291 6319735 : /// emission code. If BlobData is non-null, then it specifies an array of
292 6319323 : /// data that should be emitted as part of the Blob or Array operand that is
293 0 : /// known to exist at the end of the record. If Code is specified, then
294 4179258 : /// it is the record code to emit before the Vals, which must not contain
295 4179258 : /// the code.
296 4179258 : template <typename uintty>
297 0 : void EmitRecordWithAbbrevImpl(unsigned Abbrev, ArrayRef<uintty> Vals,
298 924226 : StringRef Blob, Optional<unsigned> Code) {
299 1848452 : const char *BlobData = Blob.data();
300 924226 : unsigned BlobLen = (unsigned) Blob.size();
301 0 : unsigned AbbrevNo = Abbrev-bitc::FIRST_APPLICATION_ABBREV;
302 11423219 : assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!");
303 10759847 : const BitCodeAbbrev *Abbv = CurAbbrevs[AbbrevNo].get();
304 0 :
305 0 : EmitCode(Abbrev);
306 :
307 10759847 : unsigned i = 0, e = static_cast<unsigned>(Abbv->getNumOperandInfos());
308 417 : if (Code) {
309 5899252 : assert(e && "Expected non-empty abbreviation");
310 5899252 : const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i++);
311 5899252 :
312 417 : if (Op.isLiteral())
313 4026465 : EmitAbbreviatedLiteral(Op, Code.getValue());
314 4026882 : else {
315 4026882 : assert(Op.getEncoding() != BitCodeAbbrevOp::Array &&
316 417 : Op.getEncoding() != BitCodeAbbrevOp::Blob &&
317 834130 : "Expected literal or scalar");
318 1668260 : EmitAbbreviatedField(Op, Code.getValue());
319 834130 : }
320 0 : }
321 10759847 :
322 0 : unsigned RecordIdx = 0;
323 0 : for (; i != e; ++i) {
324 0 : const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
325 : if (Op.isLiteral()) {
326 417 : assert(RecordIdx < Vals.size() && "Invalid abbrev/record");
327 0 : EmitAbbreviatedLiteral(Op, Vals[RecordIdx]);
328 0 : ++RecordIdx;
329 0 : } else if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
330 0 : // Array case.
331 : assert(i + 2 == e && "array op not second to last?");
332 0 : const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
333 0 :
334 0 : // If this record has blob data, emit it, otherwise we must have record
335 228 : // entries to encode this way.
336 0 : if (BlobData) {
337 0 : assert(RecordIdx == Vals.size() &&
338 228 : "Blob data and record entries specified for array!");
339 228 : // Emit a vbr6 to indicate the number of elements present.
340 0 : EmitVBR(static_cast<uint32_t>(BlobLen), 6);
341 663828 :
342 : // Emit each field.
343 : for (unsigned i = 0; i != BlobLen; ++i)
344 : EmitAbbreviatedField(EltEnc, (unsigned char)BlobData[i]);
345 663372 :
346 228 : // Know that blob data is consumed for assertion below.
347 420483 : BlobData = nullptr;
348 420483 : } else {
349 420071 : // Emit a vbr6 to indicate the number of elements present.
350 0 : EmitVBR(static_cast<uint32_t>(Vals.size()-RecordIdx), 6);
351 152793 :
352 152793 : // Emit each field.
353 152793 : for (unsigned e = Vals.size(); RecordIdx != e; ++RecordIdx)
354 : EmitAbbreviatedField(EltEnc, Vals[RecordIdx]);
355 90096 : }
356 180192 : } else if (Op.getEncoding() == BitCodeAbbrevOp::Blob) {
357 90096 : // If this record has blob data, emit it, otherwise we must have record
358 : // entries to encode this way.
359 663372 :
360 : if (BlobData) {
361 817 : assert(RecordIdx == Vals.size() &&
362 : "Blob data and record entries specified for blob operand!");
363 589 :
364 : assert(Blob.data() == BlobData && "BlobData got moved");
365 : assert(Blob.size() == BlobLen && "BlobLen got changed");
366 228 : emitBlob(Blob);
367 361 : BlobData = nullptr;
368 1420313 : } else {
369 : emitBlob(Vals.slice(RecordIdx));
370 11 : }
371 1420313 : } else { // Single scalar field.
372 1420313 : assert(RecordIdx < Vals.size() && "Invalid abbrev/record");
373 : EmitAbbreviatedField(Op, Vals[RecordIdx]);
374 2840637 : ++RecordIdx;
375 : }
376 : }
377 : assert(RecordIdx == Vals.size() && "Not all record operands emitted!");
378 0 : assert(BlobData == nullptr &&
379 1420313 : "Blob data specified for record that doesn't use it!");
380 : }
381 0 :
382 0 : public:
383 980076 : /// Emit a blob, including flushing before and tail-padding.
384 : template <class UIntTy>
385 4 : void emitBlob(ArrayRef<UIntTy> Bytes, bool ShouldEmitSize = true) {
386 : // Emit a vbr6 to indicate the number of elements present.
387 4 : if (ShouldEmitSize)
388 12 : EmitVBR(static_cast<uint32_t>(Bytes.size()), 6);
389 31 :
390 : // Flush to a 32-bit alignment boundary.
391 235 : FlushToWord();
392 440 :
393 : // Emit literal bytes.
394 15219785 : for (const auto &B : Bytes) {
395 : assert(isUInt<8>(B) && "Value too large to emit as byte");
396 13799118 : WriteByte((unsigned char)B);
397 : }
398 153 :
399 3629266 : // Align end to 32-bits.
400 10169848 : while (GetBufferOffset() & 3)
401 : WriteByte(0);
402 4 : }
403 266435 : void emitBlob(StringRef Bytes, bool ShouldEmitSize = true) {
404 4 : emitBlob(makeArrayRef((const uint8_t *)Bytes.data(), Bytes.size()),
405 : ShouldEmitSize);
406 : }
407 266435 :
408 : /// EmitRecord - Emit the specified record to the stream, using an abbrev if
409 : /// we have one to compress the output.
410 : template <typename Container>
411 394 : void EmitRecord(unsigned Code, const Container &Vals, unsigned Abbrev = 0) {
412 197 : if (!Abbrev) {
413 : // If we don't have an abbrev to use, emit this in its fully unabbreviated
414 0 : // form.
415 0 : auto Count = static_cast<uint32_t>(makeArrayRef(Vals).size());
416 : EmitCode(bitc::UNABBREV_RECORD);
417 : EmitVBR(Code, 6);
418 228 : EmitVBR(Count, 6);
419 0 : for (unsigned i = 0, e = Count; i != e; ++i)
420 : EmitVBR64(Vals[i], 6);
421 266435 : return;
422 0 : }
423 45 :
424 1971038 : EmitRecordWithAbbrevImpl(Abbrev, makeArrayRef(Vals), StringRef(), Code);
425 3409251 : }
426 45 :
427 9903407 : /// EmitRecordWithAbbrev - Emit a record with the specified abbreviation.
428 : /// Unlike EmitRecord, the code for the record should be included in Vals as
429 45 : /// the first entry.
430 0 : template <typename Container>
431 184822 : void EmitRecordWithAbbrev(unsigned Abbrev, const Container &Vals) {
432 15225 : EmitRecordWithAbbrevImpl(Abbrev, makeArrayRef(Vals), StringRef(), None);
433 : }
434 15180 :
435 : /// EmitRecordWithBlob - Emit the specified record to the stream, using an
436 : /// abbrev that includes a blob at the end. The blob data to emit is
437 : /// specified by the pointer and length specified at the end. In contrast to
438 113 : /// EmitRecord, this routine expects that the first entry in Vals is the code
439 : /// of the record.
440 3097 : template <typename Container>
441 0 : void EmitRecordWithBlob(unsigned Abbrev, const Container &Vals,
442 : StringRef Blob) {
443 0 : EmitRecordWithAbbrevImpl(Abbrev, makeArrayRef(Vals), Blob, None);
444 19437170 : }
445 9718585 : template <typename Container>
446 : void EmitRecordWithBlob(unsigned Abbrev, const Container &Vals,
447 0 : const char *BlobData, unsigned BlobLen) {
448 : return EmitRecordWithAbbrevImpl(Abbrev, makeArrayRef(Vals),
449 : StringRef(BlobData, BlobLen), None);
450 0 : }
451 1420313 :
452 1323615 : /// EmitRecordWithArray - Just like EmitRecordWithBlob, works with records
453 : /// that end with an array.
454 0 : template <typename Container>
455 1323615 : void EmitRecordWithArray(unsigned Abbrev, const Container &Vals,
456 1323615 : StringRef Array) {
457 : EmitRecordWithAbbrevImpl(Abbrev, makeArrayRef(Vals), Array, None);
458 2647230 : }
459 45 : template <typename Container>
460 : void EmitRecordWithArray(unsigned Abbrev, const Container &Vals,
461 45 : const char *ArrayData, unsigned ArrayLen) {
462 45 : return EmitRecordWithAbbrevImpl(Abbrev, makeArrayRef(Vals),
463 1323615 : StringRef(ArrayData, ArrayLen), None);
464 : }
465 45 :
466 0 : //===--------------------------------------------------------------------===//
467 883378 : // Abbrev Emission
468 15225 : //===--------------------------------------------------------------------===//
469 :
470 15180 : private:
471 0 : // Emit the abbreviation as a DEFINE_ABBREV record.
472 0 : void EncodeAbbrev(const BitCodeAbbrev &Abbv) {
473 31 : EmitCode(bitc::DEFINE_ABBREV);
474 113 : EmitVBR(Abbv.getNumOperandInfos(), 5);
475 0 : for (unsigned i = 0, e = static_cast<unsigned>(Abbv.getNumOperandInfos());
476 45 : i != e; ++i) {
477 0 : const BitCodeAbbrevOp &Op = Abbv.getOperandInfo(i);
478 14834532 : Emit(Op.isLiteral(), 1);
479 : if (Op.isLiteral()) {
480 13510872 : EmitVBR64(Op.getLiteralValue(), 8);
481 : } else {
482 0 : Emit(Op.getEncoding(), 3);
483 3625574 : if (Op.hasEncodingData())
484 9885298 : EmitVBR64(Op.getEncodingData(), 5);
485 0 : }
486 : }
487 243193 : }
488 : public:
489 0 :
490 : /// EmitAbbrev - This emits an abbreviation to the stream. Note that this
491 243193 : /// method takes ownership of the specified abbrev.
492 0 : unsigned EmitAbbrev(std::shared_ptr<BitCodeAbbrev> Abbv) {
493 : // Emit the abbreviation as a record.
494 0 : EncodeAbbrev(*Abbv);
495 0 : CurAbbrevs.push_back(std::move(Abbv));
496 0 : return static_cast<unsigned>(CurAbbrevs.size())-1 +
497 : bitc::FIRST_APPLICATION_ABBREV;
498 0 : }
499 0 :
500 : //===--------------------------------------------------------------------===//
501 0 : // BlockInfo Block Emission
502 0 : //===--------------------------------------------------------------------===//
503 183 :
504 : /// EnterBlockInfoBlock - Start emitting the BLOCKINFO_BLOCK.
505 243193 : void EnterBlockInfoBlock() {
506 183 : EnterSubblock(bitc::BLOCKINFO_BLOCK_ID, 2);
507 183 : BlockInfoCurBID = ~0U;
508 1545757 : BlockInfoRecords.clear();
509 2605494 : }
510 : private:
511 9642105 : /// SwitchToBlockID - If we aren't already talking about the specified block
512 : /// ID, emit a BLOCKINFO_CODE_SETBID record.
513 : void SwitchToBlockID(unsigned BlockID) {
514 183 : if (BlockInfoCurBID == BlockID) return;
515 184822 : SmallVector<unsigned, 2> V;
516 : V.push_back(BlockID);
517 45 : EmitRecord(bitc::BLOCKINFO_CODE_SETBID, V);
518 4964683 : BlockInfoCurBID = BlockID;
519 4964683 : }
520 :
521 : BlockInfo &getOrCreateBlockInfo(unsigned BlockID) {
522 : if (BlockInfo *BI = getBlockInfo(BlockID))
523 : return *BI;
524 4834500 :
525 4831448 : // Otherwise, add a new record.
526 68502713 : BlockInfoRecords.emplace_back();
527 127322133 : BlockInfoRecords.back().BlockID = BlockID;
528 18914566 : return BlockInfoRecords.back();
529 9457965 : }
530 :
531 266969 : public:
532 :
533 5317 : /// EmitBlockInfoAbbrev - Emit a DEFINE_ABBREV record for the specified
534 5500 : /// BlockID.
535 1323931 : unsigned EmitBlockInfoAbbrev(unsigned BlockID, std::shared_ptr<BitCodeAbbrev> Abbv) {
536 96698 : SwitchToBlockID(BlockID);
537 : EncodeAbbrev(*Abbv);
538 11 :
539 102015 : // Add the abbrev to the specified block record.
540 102015 : BlockInfo &Info = getOrCreateBlockInfo(BlockID);
541 205363 : Info.Abbrevs.push_back(std::move(Abbv));
542 579435 :
543 : return Info.Abbrevs.size()-1+bitc::FIRST_APPLICATION_ABBREV;
544 : }
545 : };
546 45 :
547 96698 :
548 4849 : } // End llvm namespace
549 4894 :
550 147 : #endif
|