LLVM  8.0.1
BitstreamReader.cpp
Go to the documentation of this file.
1 //===- BitstreamReader.cpp - BitstreamReader implementation ---------------===//
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 
11 #include "llvm/ADT/StringRef.h"
12 #include <cassert>
13 #include <string>
14 
15 using namespace llvm;
16 
17 //===----------------------------------------------------------------------===//
18 // BitstreamCursor implementation
19 //===----------------------------------------------------------------------===//
20 
21 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter
22 /// the block, and return true if the block has an error.
23 bool BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
24  // Save the current block's state on BlockScope.
25  BlockScope.push_back(Block(CurCodeSize));
26  BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
27 
28  // Add the abbrevs specific to this block to the CurAbbrevs list.
29  if (BlockInfo) {
31  BlockInfo->getBlockInfo(BlockID)) {
32  CurAbbrevs.insert(CurAbbrevs.end(), Info->Abbrevs.begin(),
33  Info->Abbrevs.end());
34  }
35  }
36 
37  // Get the codesize of this block.
38  CurCodeSize = ReadVBR(bitc::CodeLenWidth);
39  // We can't read more than MaxChunkSize at a time
40  if (CurCodeSize > MaxChunkSize)
41  return true;
42 
44  unsigned NumWords = Read(bitc::BlockSizeWidth);
45  if (NumWordsP) *NumWordsP = NumWords;
46 
47  // Validate that this block is sane.
48  return CurCodeSize == 0 || AtEndOfStream();
49 }
50 
51 static uint64_t readAbbreviatedField(BitstreamCursor &Cursor,
52  const BitCodeAbbrevOp &Op) {
53  assert(!Op.isLiteral() && "Not to be used with literals!");
54 
55  // Decode the value as we are commanded.
56  switch (Op.getEncoding()) {
59  llvm_unreachable("Should not reach here");
61  assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
62  return Cursor.Read((unsigned)Op.getEncodingData());
64  assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
65  return Cursor.ReadVBR64((unsigned)Op.getEncodingData());
67  return BitCodeAbbrevOp::DecodeChar6(Cursor.Read(6));
68  }
69  llvm_unreachable("invalid abbreviation encoding");
70 }
71 
73  const BitCodeAbbrevOp &Op) {
74  assert(!Op.isLiteral() && "Not to be used with literals!");
75 
76  // Decode the value as we are commanded.
77  switch (Op.getEncoding()) {
80  llvm_unreachable("Should not reach here");
82  assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
83  Cursor.Read((unsigned)Op.getEncodingData());
84  break;
86  assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
87  Cursor.ReadVBR64((unsigned)Op.getEncodingData());
88  break;
90  Cursor.Read(6);
91  break;
92  }
93 }
94 
95 /// skipRecord - Read the current record and discard it.
96 unsigned BitstreamCursor::skipRecord(unsigned AbbrevID) {
97  // Skip unabbreviated records by reading past their entries.
98  if (AbbrevID == bitc::UNABBREV_RECORD) {
99  unsigned Code = ReadVBR(6);
100  unsigned NumElts = ReadVBR(6);
101  for (unsigned i = 0; i != NumElts; ++i)
102  (void)ReadVBR64(6);
103  return Code;
104  }
105 
106  const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
107  const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
108  unsigned Code;
109  if (CodeOp.isLiteral())
110  Code = CodeOp.getLiteralValue();
111  else {
112  if (CodeOp.getEncoding() == BitCodeAbbrevOp::Array ||
114  report_fatal_error("Abbreviation starts with an Array or a Blob");
115  Code = readAbbreviatedField(*this, CodeOp);
116  }
117 
118  for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i < e; ++i) {
119  const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
120  if (Op.isLiteral())
121  continue;
122 
123  if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
125  skipAbbreviatedField(*this, Op);
126  continue;
127  }
128 
129  if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
130  // Array case. Read the number of elements as a vbr6.
131  unsigned NumElts = ReadVBR(6);
132 
133  // Get the element encoding.
134  assert(i+2 == e && "array op not second to last?");
135  const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
136 
137  // Read all the elements.
138  // Decode the value as we are commanded.
139  switch (EltEnc.getEncoding()) {
140  default:
141  report_fatal_error("Array element type can't be an Array or a Blob");
143  assert((unsigned)EltEnc.getEncodingData() <= MaxChunkSize);
144  JumpToBit(GetCurrentBitNo() + NumElts * EltEnc.getEncodingData());
145  break;
147  assert((unsigned)EltEnc.getEncodingData() <= MaxChunkSize);
148  for (; NumElts; --NumElts)
149  ReadVBR64((unsigned)EltEnc.getEncodingData());
150  break;
152  JumpToBit(GetCurrentBitNo() + NumElts * 6);
153  break;
154  }
155  continue;
156  }
157 
159  // Blob case. Read the number of bytes as a vbr6.
160  unsigned NumElts = ReadVBR(6);
161  SkipToFourByteBoundary(); // 32-bit alignment
162 
163  // Figure out where the end of this blob will be including tail padding.
164  size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8;
165 
166  // If this would read off the end of the bitcode file, just set the
167  // record to empty and return.
168  if (!canSkipToPos(NewEnd/8)) {
169  skipToEnd();
170  break;
171  }
172 
173  // Skip over the blob.
174  JumpToBit(NewEnd);
175  }
176  return Code;
177 }
178 
179 unsigned BitstreamCursor::readRecord(unsigned AbbrevID,
181  StringRef *Blob) {
182  if (AbbrevID == bitc::UNABBREV_RECORD) {
183  unsigned Code = ReadVBR(6);
184  unsigned NumElts = ReadVBR(6);
185  for (unsigned i = 0; i != NumElts; ++i)
186  Vals.push_back(ReadVBR64(6));
187  return Code;
188  }
189 
190  const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
191 
192  // Read the record code first.
193  assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?");
194  const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
195  unsigned Code;
196  if (CodeOp.isLiteral())
197  Code = CodeOp.getLiteralValue();
198  else {
199  if (CodeOp.getEncoding() == BitCodeAbbrevOp::Array ||
200  CodeOp.getEncoding() == BitCodeAbbrevOp::Blob)
201  report_fatal_error("Abbreviation starts with an Array or a Blob");
202  Code = readAbbreviatedField(*this, CodeOp);
203  }
204 
205  for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
206  const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
207  if (Op.isLiteral()) {
208  Vals.push_back(Op.getLiteralValue());
209  continue;
210  }
211 
212  if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
214  Vals.push_back(readAbbreviatedField(*this, Op));
215  continue;
216  }
217 
218  if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
219  // Array case. Read the number of elements as a vbr6.
220  unsigned NumElts = ReadVBR(6);
221 
222  // Get the element encoding.
223  if (i + 2 != e)
224  report_fatal_error("Array op not second to last");
225  const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
226  if (!EltEnc.isEncoding())
228  "Array element type has to be an encoding of a type");
229 
230  // Read all the elements.
231  switch (EltEnc.getEncoding()) {
232  default:
233  report_fatal_error("Array element type can't be an Array or a Blob");
235  for (; NumElts; --NumElts)
236  Vals.push_back(Read((unsigned)EltEnc.getEncodingData()));
237  break;
239  for (; NumElts; --NumElts)
240  Vals.push_back(ReadVBR64((unsigned)EltEnc.getEncodingData()));
241  break;
243  for (; NumElts; --NumElts)
245  }
246  continue;
247  }
248 
250  // Blob case. Read the number of bytes as a vbr6.
251  unsigned NumElts = ReadVBR(6);
252  SkipToFourByteBoundary(); // 32-bit alignment
253 
254  // Figure out where the end of this blob will be including tail padding.
255  size_t CurBitPos = GetCurrentBitNo();
256  size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8;
257 
258  // If this would read off the end of the bitcode file, just set the
259  // record to empty and return.
260  if (!canSkipToPos(NewEnd/8)) {
261  Vals.append(NumElts, 0);
262  skipToEnd();
263  break;
264  }
265 
266  // Otherwise, inform the streamer that we need these bytes in memory. Skip
267  // over tail padding first, in case jumping to NewEnd invalidates the Blob
268  // pointer.
269  JumpToBit(NewEnd);
270  const char *Ptr = (const char *)getPointerToBit(CurBitPos, NumElts);
271 
272  // If we can return a reference to the data, do so to avoid copying it.
273  if (Blob) {
274  *Blob = StringRef(Ptr, NumElts);
275  } else {
276  // Otherwise, unpack into Vals with zero extension.
277  for (; NumElts; --NumElts)
278  Vals.push_back((unsigned char)*Ptr++);
279  }
280  }
281 
282  return Code;
283 }
284 
286  auto Abbv = std::make_shared<BitCodeAbbrev>();
287  unsigned NumOpInfo = ReadVBR(5);
288  for (unsigned i = 0; i != NumOpInfo; ++i) {
289  bool IsLiteral = Read(1);
290  if (IsLiteral) {
291  Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
292  continue;
293  }
294 
297  uint64_t Data = ReadVBR64(5);
298 
299  // As a special case, handle fixed(0) (i.e., a fixed field with zero bits)
300  // and vbr(0) as a literal zero. This is decoded the same way, and avoids
301  // a slow path in Read() to have to handle reading zero bits.
302  if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
303  Data == 0) {
304  Abbv->Add(BitCodeAbbrevOp(0));
305  continue;
306  }
307 
308  if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
309  Data > MaxChunkSize)
311  "Fixed or VBR abbrev record with size > MaxChunkData");
312 
313  Abbv->Add(BitCodeAbbrevOp(E, Data));
314  } else
315  Abbv->Add(BitCodeAbbrevOp(E));
316  }
317 
318  if (Abbv->getNumOperandInfos() == 0)
319  report_fatal_error("Abbrev record with no operands");
320  CurAbbrevs.push_back(std::move(Abbv));
321 }
322 
324 BitstreamCursor::ReadBlockInfoBlock(bool ReadBlockInfoNames) {
326 
327  BitstreamBlockInfo NewBlockInfo;
328 
330  BitstreamBlockInfo::BlockInfo *CurBlockInfo = nullptr;
331 
332  // Read all the records for this module.
333  while (true) {
335 
336  switch (Entry.Kind) {
337  case llvm::BitstreamEntry::SubBlock: // Handled for us already.
339  return None;
341  return std::move(NewBlockInfo);
343  // The interesting case.
344  break;
345  }
346 
347  // Read abbrev records, associate them with CurBID.
348  if (Entry.ID == bitc::DEFINE_ABBREV) {
349  if (!CurBlockInfo) return None;
351 
352  // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the
353  // appropriate BlockInfo.
354  CurBlockInfo->Abbrevs.push_back(std::move(CurAbbrevs.back()));
355  CurAbbrevs.pop_back();
356  continue;
357  }
358 
359  // Read a record.
360  Record.clear();
361  switch (readRecord(Entry.ID, Record)) {
362  default: break; // Default behavior, ignore unknown content.
364  if (Record.size() < 1) return None;
365  CurBlockInfo = &NewBlockInfo.getOrCreateBlockInfo((unsigned)Record[0]);
366  break;
368  if (!CurBlockInfo) return None;
369  if (!ReadBlockInfoNames)
370  break; // Ignore name.
371  std::string Name;
372  for (unsigned i = 0, e = Record.size(); i != e; ++i)
373  Name += (char)Record[i];
374  CurBlockInfo->Name = Name;
375  break;
376  }
378  if (!CurBlockInfo) return None;
379  if (!ReadBlockInfoNames)
380  break; // Ignore name.
381  std::string Name;
382  for (unsigned i = 1, e = Record.size(); i != e; ++i)
383  Name += (char)Record[i];
384  CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],
385  Name));
386  break;
387  }
388  }
389  }
390 }
bool isEncoding() const
Definition: BitCodes.h:112
Encoding getEncoding() const
Definition: BitCodes.h:118
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:69
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:140
This class represents lattice values for constants.
Definition: AllocatorList.h:24
uint64_t GetCurrentBitNo() const
Return the bit # of the bit we are reading.
void push_back(const T &Elt)
Definition: SmallVector.h:218
BitCodeAbbrev - This class represents an abbreviation record.
Definition: BitCodes.h:168
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:115
bool canSkipToPos(size_t pos) const
amdgpu Simplify well known AMD library false Value Value const Twine & Name
uint64_t ReadVBR64(unsigned NumBits)
void skipToEnd()
Skip to the end of the file.
uint32_t ReadVBR(unsigned NumBits)
const BitCodeAbbrevOp & getOperandInfo(unsigned N) const
Definition: BitCodes.h:175
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:21
enum llvm::BitstreamEntry::@149 Kind
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:94
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:53
#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:847
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:53
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.
If this flag is used, abbrev entries are returned just like normal records.
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:394
unsigned getNumOperandInfos() const
Definition: BitCodes.h:172
uint64_t getEncodingData() const
Definition: BitCodes.h:119
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:111
static char DecodeChar6(unsigned V)
Definition: BitCodes.h:155
bool hasEncodingData() const
Definition: BitCodes.h:124
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49