LLVM  9.0.0svn
BitstreamReader.cpp
Go to the documentation of this file.
1 //===- BitstreamReader.cpp - BitstreamReader implementation ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "llvm/ADT/StringRef.h"
11 #include <cassert>
12 #include <string>
13 
14 using namespace llvm;
15 
16 //===----------------------------------------------------------------------===//
17 // BitstreamCursor implementation
18 //===----------------------------------------------------------------------===//
19 
20 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter
21 /// the block, and return true if the block has an error.
22 bool BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
23  // Save the current block's state on BlockScope.
24  BlockScope.push_back(Block(CurCodeSize));
25  BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
26 
27  // Add the abbrevs specific to this block to the CurAbbrevs list.
28  if (BlockInfo) {
30  BlockInfo->getBlockInfo(BlockID)) {
31  CurAbbrevs.insert(CurAbbrevs.end(), Info->Abbrevs.begin(),
32  Info->Abbrevs.end());
33  }
34  }
35 
36  // Get the codesize of this block.
37  CurCodeSize = ReadVBR(bitc::CodeLenWidth);
38  // We can't read more than MaxChunkSize at a time
39  if (CurCodeSize > MaxChunkSize)
40  return true;
41 
43  unsigned NumWords = Read(bitc::BlockSizeWidth);
44  if (NumWordsP) *NumWordsP = NumWords;
45 
46  // Validate that this block is sane.
47  return CurCodeSize == 0 || AtEndOfStream();
48 }
49 
50 static uint64_t readAbbreviatedField(BitstreamCursor &Cursor,
51  const BitCodeAbbrevOp &Op) {
52  assert(!Op.isLiteral() && "Not to be used with literals!");
53 
54  // Decode the value as we are commanded.
55  switch (Op.getEncoding()) {
58  llvm_unreachable("Should not reach here");
60  assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
61  return Cursor.Read((unsigned)Op.getEncodingData());
63  assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
64  return Cursor.ReadVBR64((unsigned)Op.getEncodingData());
66  return BitCodeAbbrevOp::DecodeChar6(Cursor.Read(6));
67  }
68  llvm_unreachable("invalid abbreviation encoding");
69 }
70 
72  const BitCodeAbbrevOp &Op) {
73  assert(!Op.isLiteral() && "Not to be used with literals!");
74 
75  // Decode the value as we are commanded.
76  switch (Op.getEncoding()) {
79  llvm_unreachable("Should not reach here");
81  assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
82  Cursor.Read((unsigned)Op.getEncodingData());
83  break;
85  assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
86  Cursor.ReadVBR64((unsigned)Op.getEncodingData());
87  break;
89  Cursor.Read(6);
90  break;
91  }
92 }
93 
94 /// skipRecord - Read the current record and discard it.
95 unsigned BitstreamCursor::skipRecord(unsigned AbbrevID) {
96  // Skip unabbreviated records by reading past their entries.
97  if (AbbrevID == bitc::UNABBREV_RECORD) {
98  unsigned Code = ReadVBR(6);
99  unsigned NumElts = ReadVBR(6);
100  for (unsigned i = 0; i != NumElts; ++i)
101  (void)ReadVBR64(6);
102  return Code;
103  }
104 
105  const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
106  const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
107  unsigned Code;
108  if (CodeOp.isLiteral())
109  Code = CodeOp.getLiteralValue();
110  else {
111  if (CodeOp.getEncoding() == BitCodeAbbrevOp::Array ||
113  report_fatal_error("Abbreviation starts with an Array or a Blob");
114  Code = readAbbreviatedField(*this, CodeOp);
115  }
116 
117  for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i < e; ++i) {
118  const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
119  if (Op.isLiteral())
120  continue;
121 
122  if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
124  skipAbbreviatedField(*this, Op);
125  continue;
126  }
127 
128  if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
129  // Array case. Read the number of elements as a vbr6.
130  unsigned NumElts = ReadVBR(6);
131 
132  // Get the element encoding.
133  assert(i+2 == e && "array op not second to last?");
134  const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
135 
136  // Read all the elements.
137  // Decode the value as we are commanded.
138  switch (EltEnc.getEncoding()) {
139  default:
140  report_fatal_error("Array element type can't be an Array or a Blob");
142  assert((unsigned)EltEnc.getEncodingData() <= MaxChunkSize);
143  JumpToBit(GetCurrentBitNo() + NumElts * EltEnc.getEncodingData());
144  break;
146  assert((unsigned)EltEnc.getEncodingData() <= MaxChunkSize);
147  for (; NumElts; --NumElts)
148  ReadVBR64((unsigned)EltEnc.getEncodingData());
149  break;
151  JumpToBit(GetCurrentBitNo() + NumElts * 6);
152  break;
153  }
154  continue;
155  }
156 
158  // Blob case. Read the number of bytes as a vbr6.
159  unsigned NumElts = ReadVBR(6);
160  SkipToFourByteBoundary(); // 32-bit alignment
161 
162  // Figure out where the end of this blob will be including tail padding.
163  size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8;
164 
165  // If this would read off the end of the bitcode file, just set the
166  // record to empty and return.
167  if (!canSkipToPos(NewEnd/8)) {
168  skipToEnd();
169  break;
170  }
171 
172  // Skip over the blob.
173  JumpToBit(NewEnd);
174  }
175  return Code;
176 }
177 
178 unsigned BitstreamCursor::readRecord(unsigned AbbrevID,
180  StringRef *Blob) {
181  if (AbbrevID == bitc::UNABBREV_RECORD) {
182  unsigned Code = ReadVBR(6);
183  unsigned NumElts = ReadVBR(6);
184  for (unsigned i = 0; i != NumElts; ++i)
185  Vals.push_back(ReadVBR64(6));
186  return Code;
187  }
188 
189  const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
190 
191  // Read the record code first.
192  assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?");
193  const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
194  unsigned Code;
195  if (CodeOp.isLiteral())
196  Code = CodeOp.getLiteralValue();
197  else {
198  if (CodeOp.getEncoding() == BitCodeAbbrevOp::Array ||
199  CodeOp.getEncoding() == BitCodeAbbrevOp::Blob)
200  report_fatal_error("Abbreviation starts with an Array or a Blob");
201  Code = readAbbreviatedField(*this, CodeOp);
202  }
203 
204  for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
205  const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
206  if (Op.isLiteral()) {
207  Vals.push_back(Op.getLiteralValue());
208  continue;
209  }
210 
211  if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
213  Vals.push_back(readAbbreviatedField(*this, Op));
214  continue;
215  }
216 
217  if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
218  // Array case. Read the number of elements as a vbr6.
219  unsigned NumElts = ReadVBR(6);
220 
221  // Get the element encoding.
222  if (i + 2 != e)
223  report_fatal_error("Array op not second to last");
224  const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
225  if (!EltEnc.isEncoding())
227  "Array element type has to be an encoding of a type");
228 
229  // Read all the elements.
230  switch (EltEnc.getEncoding()) {
231  default:
232  report_fatal_error("Array element type can't be an Array or a Blob");
234  for (; NumElts; --NumElts)
235  Vals.push_back(Read((unsigned)EltEnc.getEncodingData()));
236  break;
238  for (; NumElts; --NumElts)
239  Vals.push_back(ReadVBR64((unsigned)EltEnc.getEncodingData()));
240  break;
242  for (; NumElts; --NumElts)
244  }
245  continue;
246  }
247 
249  // Blob case. Read the number of bytes as a vbr6.
250  unsigned NumElts = ReadVBR(6);
251  SkipToFourByteBoundary(); // 32-bit alignment
252 
253  // Figure out where the end of this blob will be including tail padding.
254  size_t CurBitPos = GetCurrentBitNo();
255  size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8;
256 
257  // If this would read off the end of the bitcode file, just set the
258  // record to empty and return.
259  if (!canSkipToPos(NewEnd/8)) {
260  Vals.append(NumElts, 0);
261  skipToEnd();
262  break;
263  }
264 
265  // Otherwise, inform the streamer that we need these bytes in memory. Skip
266  // over tail padding first, in case jumping to NewEnd invalidates the Blob
267  // pointer.
268  JumpToBit(NewEnd);
269  const char *Ptr = (const char *)getPointerToBit(CurBitPos, NumElts);
270 
271  // If we can return a reference to the data, do so to avoid copying it.
272  if (Blob) {
273  *Blob = StringRef(Ptr, NumElts);
274  } else {
275  // Otherwise, unpack into Vals with zero extension.
276  for (; NumElts; --NumElts)
277  Vals.push_back((unsigned char)*Ptr++);
278  }
279  }
280 
281  return Code;
282 }
283 
285  auto Abbv = std::make_shared<BitCodeAbbrev>();
286  unsigned NumOpInfo = ReadVBR(5);
287  for (unsigned i = 0; i != NumOpInfo; ++i) {
288  bool IsLiteral = Read(1);
289  if (IsLiteral) {
290  Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
291  continue;
292  }
293 
296  uint64_t Data = ReadVBR64(5);
297 
298  // As a special case, handle fixed(0) (i.e., a fixed field with zero bits)
299  // and vbr(0) as a literal zero. This is decoded the same way, and avoids
300  // a slow path in Read() to have to handle reading zero bits.
301  if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
302  Data == 0) {
303  Abbv->Add(BitCodeAbbrevOp(0));
304  continue;
305  }
306 
307  if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
308  Data > MaxChunkSize)
310  "Fixed or VBR abbrev record with size > MaxChunkData");
311 
312  Abbv->Add(BitCodeAbbrevOp(E, Data));
313  } else
314  Abbv->Add(BitCodeAbbrevOp(E));
315  }
316 
317  if (Abbv->getNumOperandInfos() == 0)
318  report_fatal_error("Abbrev record with no operands");
319  CurAbbrevs.push_back(std::move(Abbv));
320 }
321 
323 BitstreamCursor::ReadBlockInfoBlock(bool ReadBlockInfoNames) {
325 
326  BitstreamBlockInfo NewBlockInfo;
327 
329  BitstreamBlockInfo::BlockInfo *CurBlockInfo = nullptr;
330 
331  // Read all the records for this module.
332  while (true) {
334 
335  switch (Entry.Kind) {
336  case llvm::BitstreamEntry::SubBlock: // Handled for us already.
338  return None;
340  return std::move(NewBlockInfo);
342  // The interesting case.
343  break;
344  }
345 
346  // Read abbrev records, associate them with CurBID.
347  if (Entry.ID == bitc::DEFINE_ABBREV) {
348  if (!CurBlockInfo) return None;
350 
351  // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the
352  // appropriate BlockInfo.
353  CurBlockInfo->Abbrevs.push_back(std::move(CurAbbrevs.back()));
354  CurAbbrevs.pop_back();
355  continue;
356  }
357 
358  // Read a record.
359  Record.clear();
360  switch (readRecord(Entry.ID, Record)) {
361  default: break; // Default behavior, ignore unknown content.
363  if (Record.size() < 1) return None;
364  CurBlockInfo = &NewBlockInfo.getOrCreateBlockInfo((unsigned)Record[0]);
365  break;
367  if (!CurBlockInfo) return None;
368  if (!ReadBlockInfoNames)
369  break; // Ignore name.
370  std::string Name;
371  for (unsigned i = 0, e = Record.size(); i != e; ++i)
372  Name += (char)Record[i];
373  CurBlockInfo->Name = Name;
374  break;
375  }
377  if (!CurBlockInfo) return None;
378  if (!ReadBlockInfoNames)
379  break; // Ignore name.
380  std::string Name;
381  for (unsigned i = 1, e = Record.size(); i != e; ++i)
382  Name += (char)Record[i];
383  CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],
384  Name));
385  break;
386  }
387  }
388  }
389 }
bool isEncoding() const
Definition: BitCodes.h:111
Encoding getEncoding() const
Definition: BitCodes.h:117
This contains information emitted to BLOCKINFO_BLOCK blocks.
const BitCodeAbbrev * getAbbrev(unsigned AbbrevID)
Return the abbreviation for the specified AbbrevId.
BLOCKINFO_BLOCK is used to define metadata about blocks, for example, standard abbrevs that should be...
Definition: BitCodes.h:68
BlockInfo & getOrCreateBlockInfo(unsigned BlockID)
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
uint64_t GetCurrentBitNo() const
Return the bit # of the bit we are reading.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
void push_back(const T &Elt)
Definition: SmallVector.h:211
BitCodeAbbrev - This class represents an abbreviation record.
Definition: BitCodes.h:165
static const size_t MaxChunkSize
const uint8_t * getPointerToBit(uint64_t BitNo, uint64_t NumBytes)
Get a pointer into the bitstream at the specified bit offset.
std::vector< std::pair< unsigned, std::string > > RecordNames
word_t Read(unsigned NumBits)
uint64_t getLiteralValue() const
Definition: BitCodes.h:114
bool canSkipToPos(size_t pos) const
uint64_t ReadVBR64(unsigned NumBits)
void skipToEnd()
Skip to the end of the file.
uint32_t ReadVBR(unsigned NumBits)
enum llvm::BitstreamEntry::@154 Kind
const BitCodeAbbrevOp & getOperandInfo(unsigned N) const
Definition: BitCodes.h:172
BitstreamEntry advanceSkippingSubblocks(unsigned Flags=0)
This is a convenience function for clients that don&#39;t expect any subblocks.
Analysis containing CSE Info
Definition: CSEInfo.cpp:20
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This represents a position within a bitcode file, implemented on top of a SimpleBitstreamCursor.
BitCodeAbbrevOp - This describes one or more operands in an abbreviation.
Definition: BitCodes.h:93
static void skipAbbreviatedField(BitstreamCursor &Cursor, const BitCodeAbbrevOp &Op)
const BlockInfo * getBlockInfo(unsigned BlockID) const
If there is block info for the specified ID, return it, otherwise return null.
void JumpToBit(uint64_t BitNo)
Reset the stream to the specified bit number.
size_t size() const
Definition: SmallVector.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::vector< std::shared_ptr< BitCodeAbbrev > > Abbrevs
When advancing through a bitstream cursor, each advance can discover a few different kinds of entries...
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:839
unsigned readRecord(unsigned AbbrevID, SmallVectorImpl< uint64_t > &Vals, StringRef *Blob=nullptr)
This class maintains the abbreviations read from a block info block.
DEFINE_ABBREV - Defines an abbrev for the current block.
Definition: BitCodes.h:52
static uint64_t readAbbreviatedField(BitstreamCursor &Cursor, const BitCodeAbbrevOp &Op)
unsigned skipRecord(unsigned AbbrevID)
Read the current record and discard it, returning the code for the record.
Optional< BitstreamBlockInfo > ReadBlockInfoBlock(bool ReadBlockInfoNames=false)
Read and return a block info block from the bitstream.
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:386
unsigned getNumOperandInfos() const
Definition: BitCodes.h:169
uint64_t getEncodingData() const
Definition: BitCodes.h:118
bool EnterSubBlock(unsigned BlockID, unsigned *NumWordsP=nullptr)
Having read the ENTER_SUBBLOCK abbrevid, enter the block, and return true if the block has an error...
bool isLiteral() const
Definition: BitCodes.h:110
static char DecodeChar6(unsigned V)
Definition: BitCodes.h:154
bool hasEncodingData() const
Definition: BitCodes.h:123
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
If this flag is used, abbrev entries are returned just like normal records.