LCOV - code coverage report
Current view: top level - include/llvm/Bitcode - BitstreamWriter.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 504 585 86.2 %
Date: 2018-09-23 13:06:45 Functions: 41 49 83.7 %
Legend: Lines: hit not hit

          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      152308 :   struct Block {
      49             :     unsigned PrevCodeSize;
      50             :     size_t StartSizeWord;
      51             :     std::vector<std::shared_ptr<BitCodeAbbrev>> PrevAbbrevs;
      52      122785 :     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       51661 :   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       62421 :     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     6158568 :   size_t GetBufferOffset() const { return Out.size(); }
      77             : 
      78             :   size_t GetWordIndex() const {
      79      245570 :     size_t Offset = GetBufferOffset();
      80             :     assert((Offset & 3) == 0 && "Not 32-bit aligned");
      81      245570 :     return Offset / 4;
      82             :   }
      83             : 
      84             : public:
      85             :   explicit BitstreamWriter(SmallVectorImpl<char> &O)
      86       11135 :     : Out(O), CurBit(0), CurValue(0), CurCodeSize(2) {}
      87             : 
      88       11124 :   ~BitstreamWriter() {
      89             :     assert(CurBit == 0 && "Unflushed data remaining");
      90             :     assert(BlockScope.empty() && CurAbbrevs.empty() && "Block imbalance");
      91       11124 :   }
      92             : 
      93             :   /// Retrieve the current position in the stream, in bits.
      94     9113680 :   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      127720 :     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      132262 :     endian::writeAtBitAlignment<uint32_t, little, unaligned>(
     112           0 :         &Out[ByteNo], NewWord, BitNo & 7);
     113           0 :   }
     114             : 
     115         131 :   void BackpatchWord64(uint64_t BitNo, uint64_t Val) {
     116         131 :     BackpatchWord(BitNo, (uint32_t)Val);
     117         131 :     BackpatchWord(BitNo + 32, (uint32_t)(Val >> 32));
     118         131 :   }
     119             : 
     120   176781049 :   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   176781049 :     CurValue |= Val << CurBit;
     124   176781049 :     if (CurBit + NumBits < 32) {
     125   145290447 :       CurBit += NumBits;
     126   145290447 :       return;
     127             :     }
     128             : 
     129             :     // Add the current word.
     130    31490602 :     WriteWord(CurValue);
     131             : 
     132    31490604 :     if (CurBit)
     133    31367327 :       CurValue = Val >> (32-CurBit);
     134             :     else
     135      123277 :       CurValue = 0;
     136    31490604 :     CurBit = (CurBit+NumBits) & 31;
     137             :   }
     138             : 
     139      436590 :   void FlushToWord() {
     140      436590 :     if (CurBit) {
     141      423230 :       WriteWord(CurValue);
     142      423230 :       CurBit = 0;
     143      423230 :       CurValue = 0;
     144             :     }
     145      436590 :   }
     146             : 
     147    91658932 :   void EmitVBR(uint32_t Val, unsigned NumBits) {
     148             :     assert(NumBits <= 32 && "Too many bits to emit!");
     149    91658932 :     uint32_t Threshold = 1U << (NumBits-1);
     150             : 
     151             :     // Emit the bits with VBR encoding, NumBits-1 bits at a time.
     152   157719993 :     while (Val >= Threshold) {
     153    66060985 :       Emit((Val & ((1 << (NumBits-1))-1)) | (1 << (NumBits-1)), NumBits);
     154    66061061 :       Val >>= NumBits-1;
     155             :     }
     156             : 
     157    91659008 :     Emit(Val, NumBits);
     158    91659029 :   }
     159             : 
     160    78313377 :   void EmitVBR64(uint64_t Val, unsigned NumBits) {
     161             :     assert(NumBits <= 32 && "Too many bits to emit!");
     162    78313377 :     if ((uint32_t)Val == Val)
     163    78311026 :       return EmitVBR((uint32_t)Val, NumBits);
     164             : 
     165        2351 :     uint32_t Threshold = 1U << (NumBits-1);
     166             : 
     167             :     // Emit the bits with VBR encoding, NumBits-1 bits at a time.
     168       30039 :     while (Val >= Threshold) {
     169       55376 :       Emit(((uint32_t)Val & ((1 << (NumBits-1))-1)) |
     170       27688 :            (1 << (NumBits-1)), NumBits);
     171       27688 :       Val >>= NumBits-1;
     172             :     }
     173             : 
     174        2351 :     Emit((uint32_t)Val, NumBits);
     175             :   }
     176             : 
     177             :   /// EmitCode - Emit the specified code.
     178             :   void EmitCode(unsigned Val) {
     179     6114682 :     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      207201 :     if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID)
     191             :       return &BlockInfoRecords.back();
     192             : 
     193      263049 :     for (unsigned i = 0, e = static_cast<unsigned>(BlockInfoRecords.size());
     194      263049 :          i != e; ++i)
     195      319778 :       if (BlockInfoRecords[i].BlockID == BlockID)
     196             :         return &BlockInfoRecords[i];
     197             :     return nullptr;
     198             :   }
     199             : 
     200      122785 :   void EnterSubblock(unsigned BlockID, unsigned CodeLen) {
     201             :     // Block header:
     202             :     //    [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen]
     203             :     EmitCode(bitc::ENTER_SUBBLOCK);
     204      122785 :     EmitVBR(BlockID, bitc::BlockIDWidth);
     205      122784 :     EmitVBR(CodeLen, bitc::CodeLenWidth);
     206      122785 :     FlushToWord();
     207             : 
     208      122785 :     size_t BlockSizeWordIndex = GetWordIndex();
     209      122785 :     unsigned OldCodeSize = CurCodeSize;
     210             : 
     211             :     // Emit a placeholder, which will be replaced when the block is popped.
     212      122785 :     Emit(0, bitc::BlockSizeWidth);
     213             : 
     214      122785 :     CurCodeSize = CodeLen;
     215             : 
     216             :     // Push the outer block's abbrev set onto the stack, start out with an
     217             :     // empty abbrev set.
     218      122785 :     BlockScope.emplace_back(OldCodeSize, BlockSizeWordIndex);
     219      122785 :     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      122785 :     if (BlockInfo *Info = getBlockInfo(BlockID)) {
     224             :       CurAbbrevs.insert(CurAbbrevs.end(), Info->Abbrevs.begin(),
     225       35940 :                         Info->Abbrevs.end());
     226             :     }
     227      122785 :   }
     228             : 
     229      122783 :   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      122785 :     FlushToWord();
     237             : 
     238             :     // Compute the size of the block, in words, not counting the size field.
     239      122785 :     size_t SizeInWords = GetWordIndex() - B.StartSizeWord - 1;
     240      122785 :     uint64_t BitNo = uint64_t(B.StartSizeWord) * 32;
     241             : 
     242             :     // Update the block size field in the header of this sub-block.
     243      122785 :     BackpatchWord(BitNo, SizeInWords);
     244             : 
     245             :     // Restore the inner block's code size and abbrev table.
     246      122785 :     CurCodeSize = B.PrevCodeSize;
     247      122785 :     CurAbbrevs = std::move(B.PrevAbbrevs);
     248             :     BlockScope.pop_back();
     249      122786 :   }
     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       16396 :   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       16396 :     switch (Op.getEncoding()) {
     275           0 :     default: llvm_unreachable("Unknown encoding!");
     276       16396 :     case BitCodeAbbrevOp::Fixed:
     277       16396 :       if (Op.getEncodingData())
     278       16396 :         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    11378805 :     case BitCodeAbbrevOp::Char6:
     285           0 :       Emit(BitCodeAbbrevOp::EncodeChar6((char)V), 6);
     286           0 :       break;
     287             :     }
     288    11395201 :   }
     289           0 : 
     290     6288408 :   /// EmitRecordWithAbbrevImpl - This is the core implementation of the record
     291     6288408 :   /// emission code.  If BlobData is non-null, then it specifies an array of
     292     6287998 :   /// 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     4168674 :   /// it is the record code to emit before the Vals, which must not contain
     295     4168674 :   /// the code.
     296     4168674 :   template <typename uintty>
     297           0 :   void EmitRecordWithAbbrevImpl(unsigned Abbrev, ArrayRef<uintty> Vals,
     298      921723 :                                 StringRef Blob, Optional<unsigned> Code) {
     299     1843446 :     const char *BlobData = Blob.data();
     300      921721 :     unsigned BlobLen = (unsigned) Blob.size();
     301           0 :     unsigned AbbrevNo = Abbrev-bitc::FIRST_APPLICATION_ABBREV;
     302    11378803 :     assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!");
     303    10718727 :     const BitCodeAbbrev *Abbv = CurAbbrevs[AbbrevNo].get();
     304           0 : 
     305           0 :     EmitCode(Abbrev);
     306             : 
     307    10718727 :     unsigned i = 0, e = static_cast<unsigned>(Abbv->getNumOperandInfos());
     308       16396 :     if (Code) {
     309     5870397 :       assert(e && "Expected non-empty abbreviation");
     310     5870397 :       const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i++);
     311     5870397 : 
     312       16396 :       if (Op.isLiteral())
     313     4016197 :         EmitAbbreviatedLiteral(Op, Code.getValue());
     314     4032593 :       else {
     315     4032593 :         assert(Op.getEncoding() != BitCodeAbbrevOp::Array &&
     316       16396 :                Op.getEncoding() != BitCodeAbbrevOp::Blob &&
     317      832133 :                "Expected literal or scalar");
     318     1664266 :         EmitAbbreviatedField(Op, Code.getValue());
     319      832133 :       }
     320           0 :     }
     321    10718727 : 
     322           0 :     unsigned RecordIdx = 0;
     323           0 :     for (; i != e; ++i) {
     324           0 :       const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
     325             :       if (Op.isLiteral()) {
     326       16396 :         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        3547 :         // entries to encode this way.
     336           0 :         if (BlobData) {
     337           0 :           assert(RecordIdx == Vals.size() &&
     338        3547 :                  "Blob data and record entries specified for array!");
     339        3547 :           // Emit a vbr6 to indicate the number of elements present.
     340           0 :           EmitVBR(static_cast<uint32_t>(BlobLen), 6);
     341      667172 : 
     342             :           // Emit each field.
     343             :           for (unsigned i = 0; i != BlobLen; ++i)
     344             :             EmitAbbreviatedField(EltEnc, (unsigned char)BlobData[i]);
     345      660078 : 
     346        3547 :           // Know that blob data is consumed for assertion below.
     347      418011 :           BlobData = nullptr;
     348      418011 :         } else {
     349      417601 :           // Emit a vbr6 to indicate the number of elements present.
     350           0 :           EmitVBR(static_cast<uint32_t>(Vals.size()-RecordIdx), 6);
     351      152477 : 
     352      152477 :           // Emit each field.
     353      152477 :           for (unsigned e = Vals.size(); RecordIdx != e; ++RecordIdx)
     354             :             EmitAbbreviatedField(EltEnc, Vals[RecordIdx]);
     355       89590 :         }
     356      179180 :       } else if (Op.getEncoding() == BitCodeAbbrevOp::Blob) {
     357       89588 :         // If this record has blob data, emit it, otherwise we must have record
     358             :         // entries to encode this way.
     359      660076 : 
     360             :         if (BlobData) {
     361       13145 :           assert(RecordIdx == Vals.size() &&
     362             :                  "Blob data and record entries specified for blob operand!");
     363        9598 : 
     364             :           assert(Blob.data() == BlobData && "BlobData got moved");
     365             :           assert(Blob.size() == BlobLen && "BlobLen got changed");
     366        3547 :           emitBlob(Blob);
     367        6051 :           BlobData = nullptr;
     368     1414751 :         } else {
     369             :           emitBlob(Vals.slice(RecordIdx));
     370         632 :         }
     371     1414751 :       } else {  // Single scalar field.
     372     1414751 :         assert(RecordIdx < Vals.size() && "Invalid abbrev/record");
     373             :         EmitAbbreviatedField(Op, Vals[RecordIdx]);
     374     2830134 :         ++RecordIdx;
     375             :       }
     376             :     }
     377             :     assert(RecordIdx == Vals.size() && "Not all record operands emitted!");
     378           0 :     assert(BlobData == nullptr &&
     379     1414751 :            "Blob data specified for record that doesn't use it!");
     380             :   }
     381           0 : 
     382           0 : public:
     383      975427 :   /// 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         633 :       EmitVBR(static_cast<uint32_t>(Bytes.size()), 6);
     389          31 : 
     390             :     // Flush to a 32-bit alignment boundary.
     391       13276 :     FlushToWord();
     392       25280 : 
     393             :     // Emit literal bytes.
     394    15171867 :     for (const auto &B : Bytes) {
     395             :       assert(isUInt<8>(B) && "Value too large to emit as byte");
     396    13751693 :       WriteByte((unsigned char)B);
     397             :     }
     398        1663 : 
     399     3620751 :     // Align end to 32-bits.
     400    10130938 :     while (GetBufferOffset() & 3)
     401             :       WriteByte(0);
     402           4 :   }
     403      265600 :   void emitBlob(StringRef Bytes, bool ShouldEmitSize = true) {
     404           4 :     emitBlob(makeArrayRef((const uint8_t *)Bytes.data(), Bytes.size()),
     405             :              ShouldEmitSize);
     406             :   }
     407      265600 : 
     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        7512 :   void EmitRecord(unsigned Code, const Container &Vals, unsigned Abbrev = 0) {
     412        3756 :     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        3547 :       EmitVBR(Count, 6);
     419           0 :       for (unsigned i = 0, e = Count; i != e; ++i)
     420             :         EmitVBR64(Vals[i], 6);
     421      265600 :       return;
     422           0 :     }
     423          45 : 
     424     1963102 :     EmitRecordWithAbbrevImpl(Abbrev, makeArrayRef(Vals), StringRef(), Code);
     425     3395047 :   }
     426          45 : 
     427     9865332 :   /// 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      184063 :   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        3101 :   template <typename Container>
     441           0 :   void EmitRecordWithBlob(unsigned Abbrev, const Container &Vals,
     442             :                           StringRef Blob) {
     443           0 :     EmitRecordWithAbbrevImpl(Abbrev, makeArrayRef(Vals), Blob, None);
     444    19362538 :   }
     445     9681269 :   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     1414751 : 
     452     1318372 :   /// EmitRecordWithArray - Just like EmitRecordWithBlob, works with records
     453             :   /// that end with an array.
     454           0 :   template <typename Container>
     455     1318372 :   void EmitRecordWithArray(unsigned Abbrev, const Container &Vals,
     456     1318384 :                            StringRef Array) {
     457             :     EmitRecordWithAbbrevImpl(Abbrev, makeArrayRef(Vals), Array, None);
     458     2636756 :   }
     459          57 :   template <typename Container>
     460             :   void EmitRecordWithArray(unsigned Abbrev, const Container &Vals,
     461          45 :                            const char *ArrayData, unsigned ArrayLen) {
     462          57 :     return EmitRecordWithAbbrevImpl(Abbrev, makeArrayRef(Vals),
     463     1318372 :                                     StringRef(ArrayData, ArrayLen), None);
     464             :   }
     465         381 : 
     466           0 :   //===--------------------------------------------------------------------===//
     467      879384 :   // Abbrev Emission
     468       15225 :   //===--------------------------------------------------------------------===//
     469             : 
     470       15180 : private:
     471          24 :   // Emit the abbreviation as a DEFINE_ABBREV record.
     472           0 :   void EncodeAbbrev(const BitCodeAbbrev &Abbv) {
     473          43 :     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    14782515 :       Emit(Op.isLiteral(), 1);
     479             :       if (Op.isLiteral()) {
     480    13464098 :         EmitVBR64(Op.getLiteralValue(), 8);
     481             :       } else {
     482           0 :         Emit(Op.getEncoding(), 3);
     483     3617048 :         if (Op.hasEncodingData())
     484     9847050 :           EmitVBR64(Op.getEncodingData(), 5);
     485           0 :       }
     486             :     }
     487      242477 :   }
     488             : public:
     489           0 : 
     490             :   /// EmitAbbrev - This emits an abbreviation to the stream.  Note that this
     491      242477 :   /// method takes ownership of the specified abbrev.
     492          12 :   unsigned EmitAbbrev(std::shared_ptr<BitCodeAbbrev> Abbv) {
     493             :     // Emit the abbreviation as a record.
     494          12 :     EncodeAbbrev(*Abbv);
     495          12 :     CurAbbrevs.push_back(std::move(Abbv));
     496           0 :     return static_cast<unsigned>(CurAbbrevs.size())-1 +
     497             :       bitc::FIRST_APPLICATION_ABBREV;
     498          12 :   }
     499           0 : 
     500             :   //===--------------------------------------------------------------------===//
     501         336 :   // BlockInfo Block Emission
     502           0 :   //===--------------------------------------------------------------------===//
     503        3826 : 
     504             :   /// EnterBlockInfoBlock - Start emitting the BLOCKINFO_BLOCK.
     505      242477 :   void EnterBlockInfoBlock() {
     506        3502 :     EnterSubblock(bitc::BLOCKINFO_BLOCK_ID, 2);
     507        3526 :     BlockInfoCurBID = ~0U;
     508     1540670 :     BlockInfoRecords.clear();
     509     2603402 :   }
     510             : private:
     511     9604585 :   /// 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        3502 :     if (BlockInfoCurBID == BlockID) return;
     515      184051 :     SmallVector<unsigned, 2> V;
     516             :     V.push_back(BlockID);
     517          45 :     EmitRecord(bitc::BLOCKINFO_CODE_SETBID, V);
     518     4956873 :     BlockInfoCurBID = BlockID;
     519     4956873 :   }
     520             : 
     521             :   BlockInfo &getOrCreateBlockInfo(unsigned BlockID) {
     522             :     if (BlockInfo *BI = getBlockInfo(BlockID))
     523             :       return *BI;
     524     4826935 : 
     525     4823879 :     // Otherwise, add a new record.
     526    68249562 :     BlockInfoRecords.emplace_back();
     527   126830969 :     BlockInfoRecords.back().BlockID = BlockID;
     528    18841044 :     return BlockInfoRecords.back();
     529     9433532 :   }
     530             : 
     531      275496 : public:
     532             : 
     533        5321 :   /// EmitBlockInfoAbbrev - Emit a DEFINE_ABBREV record for the specified
     534        8823 :   /// BlockID.
     535     1324378 :   unsigned EmitBlockInfoAbbrev(unsigned BlockID, std::shared_ptr<BitCodeAbbrev> Abbv) {
     536       96367 :     SwitchToBlockID(BlockID);
     537             :     EncodeAbbrev(*Abbv);
     538         632 : 
     539      101688 :     // Add the abbrev to the specified block record.
     540      101688 :     BlockInfo &Info = getOrCreateBlockInfo(BlockID);
     541      205994 :     Info.Abbrevs.push_back(std::move(Abbv));
     542      580648 : 
     543             :     return Info.Abbrevs.size()-1+bitc::FIRST_APPLICATION_ABBREV;
     544             :   }
     545             : };
     546          45 : 
     547       96367 : 
     548        4853 : } // End llvm namespace
     549        4898 : 
     550         147 : #endif

Generated by: LCOV version 1.13