1 //===- BitcodeReader.h - Internal BitcodeReader impl ------------*- 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 BitcodeReader class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_LIB_BITCODE_READER_BITCODEREADER_H
15 #define LLVM_LIB_BITCODE_READER_BITCODEREADER_H
16 
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/Bitcode/BitstreamReader.h"
19 #include "llvm/Bitcode/LLVMBitCodes.h"
20 #include "llvm/IR/Attributes.h"
21 #include "llvm/IR/GVMaterializer.h"
22 #include "llvm/IR/Metadata.h"
23 #include "llvm/IR/OperandTraits.h"
24 #include "llvm/IR/TrackingMDRef.h"
25 #include "llvm/IR/Type.h"
26 #include "llvm/IR/ValueHandle.h"
27 #include <deque>
28 #include <system_error>
29 #include <vector>
30 
31 namespace llvm {
32   class Comdat;
33   class MemoryBuffer;
34   class LLVMContext;
35 
36 //===----------------------------------------------------------------------===//
37 //                          BitcodeReaderValueList Class
38 //===----------------------------------------------------------------------===//
39 
40 class BitcodeReaderValueList {
41   std::vector<WeakVH> ValuePtrs;
42 
43   /// ResolveConstants - As we resolve forward-referenced constants, we add
44   /// information about them to this vector.  This allows us to resolve them in
45   /// bulk instead of resolving each reference at a time.  See the code in
46   /// ResolveConstantForwardRefs for more information about this.
47   ///
48   /// The key of this vector is the placeholder constant, the value is the slot
49   /// number that holds the resolved value.
50   typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy;
51   ResolveConstantsTy ResolveConstants;
52   LLVMContext &Context;
53 public:
BitcodeReaderValueList(LLVMContext & C)54   BitcodeReaderValueList(LLVMContext &C) : Context(C) {}
~BitcodeReaderValueList()55   ~BitcodeReaderValueList() {
56     assert(ResolveConstants.empty() && "Constants not resolved?");
57   }
58 
59   // vector compatibility methods
size()60   unsigned size() const { return ValuePtrs.size(); }
resize(unsigned N)61   void resize(unsigned N) { ValuePtrs.resize(N); }
push_back(Value * V)62   void push_back(Value *V) {
63     ValuePtrs.push_back(V);
64   }
65 
clear()66   void clear() {
67     assert(ResolveConstants.empty() && "Constants not resolved?");
68     ValuePtrs.clear();
69   }
70 
71   Value *operator[](unsigned i) const {
72     assert(i < ValuePtrs.size());
73     return ValuePtrs[i];
74   }
75 
back()76   Value *back() const { return ValuePtrs.back(); }
pop_back()77     void pop_back() { ValuePtrs.pop_back(); }
empty()78   bool empty() const { return ValuePtrs.empty(); }
shrinkTo(unsigned N)79   void shrinkTo(unsigned N) {
80     assert(N <= size() && "Invalid shrinkTo request!");
81     ValuePtrs.resize(N);
82   }
83 
84   Constant *getConstantFwdRef(unsigned Idx, Type *Ty);
85   Value *getValueFwdRef(unsigned Idx, Type *Ty);
86 
87   void AssignValue(Value *V, unsigned Idx);
88 
89   /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
90   /// resolves any forward references.
91   void ResolveConstantForwardRefs();
92 };
93 
94 
95 //===----------------------------------------------------------------------===//
96 //                          BitcodeReaderMDValueList Class
97 //===----------------------------------------------------------------------===//
98 
99 class BitcodeReaderMDValueList {
100   unsigned NumFwdRefs;
101   bool AnyFwdRefs;
102   unsigned MinFwdRef;
103   unsigned MaxFwdRef;
104   std::vector<TrackingMDRef> MDValuePtrs;
105 
106   LLVMContext &Context;
107 public:
BitcodeReaderMDValueList(LLVMContext & C)108   BitcodeReaderMDValueList(LLVMContext &C)
109       : NumFwdRefs(0), AnyFwdRefs(false), Context(C) {}
110 
111   // vector compatibility methods
size()112   unsigned size() const       { return MDValuePtrs.size(); }
resize(unsigned N)113   void resize(unsigned N)     { MDValuePtrs.resize(N); }
push_back(Metadata * MD)114   void push_back(Metadata *MD) { MDValuePtrs.emplace_back(MD); }
clear()115   void clear()                { MDValuePtrs.clear();  }
back()116   Metadata *back() const      { return MDValuePtrs.back(); }
pop_back()117   void pop_back()             { MDValuePtrs.pop_back(); }
empty()118   bool empty() const          { return MDValuePtrs.empty(); }
119 
120   Metadata *operator[](unsigned i) const {
121     assert(i < MDValuePtrs.size());
122     return MDValuePtrs[i];
123   }
124 
shrinkTo(unsigned N)125   void shrinkTo(unsigned N) {
126     assert(N <= size() && "Invalid shrinkTo request!");
127     MDValuePtrs.resize(N);
128   }
129 
130   Metadata *getValueFwdRef(unsigned Idx);
131   void AssignValue(Metadata *MD, unsigned Idx);
132   void tryToResolveCycles();
133 };
134 
135 class BitcodeReader : public GVMaterializer {
136   LLVMContext &Context;
137   DiagnosticHandlerFunction DiagnosticHandler;
138   Module *TheModule;
139   std::unique_ptr<MemoryBuffer> Buffer;
140   std::unique_ptr<BitstreamReader> StreamFile;
141   BitstreamCursor Stream;
142   DataStreamer *LazyStreamer;
143   uint64_t NextUnreadBit;
144   bool SeenValueSymbolTable;
145 
146   std::vector<Type*> TypeList;
147   BitcodeReaderValueList ValueList;
148   BitcodeReaderMDValueList MDValueList;
149   std::vector<Comdat *> ComdatList;
150   SmallVector<Instruction *, 64> InstructionList;
151 
152   std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
153   std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
154   std::vector<std::pair<Function*, unsigned> > FunctionPrefixes;
155   std::vector<std::pair<Function*, unsigned> > FunctionPrologues;
156 
157   SmallVector<Instruction*, 64> InstsWithTBAATag;
158 
159   /// MAttributes - The set of attributes by index.  Index zero in the
160   /// file is for null, and is thus not represented here.  As such all indices
161   /// are off by one.
162   std::vector<AttributeSet> MAttributes;
163 
164   /// \brief The set of attribute groups.
165   std::map<unsigned, AttributeSet> MAttributeGroups;
166 
167   /// FunctionBBs - While parsing a function body, this is a list of the basic
168   /// blocks for the function.
169   std::vector<BasicBlock*> FunctionBBs;
170 
171   // When reading the module header, this list is populated with functions that
172   // have bodies later in the file.
173   std::vector<Function*> FunctionsWithBodies;
174 
175   // When intrinsic functions are encountered which require upgrading they are
176   // stored here with their replacement function.
177   typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
178   UpgradedIntrinsicMap UpgradedIntrinsics;
179 
180   // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
181   DenseMap<unsigned, unsigned> MDKindMap;
182 
183   // Several operations happen after the module header has been read, but
184   // before function bodies are processed. This keeps track of whether
185   // we've done this yet.
186   bool SeenFirstFunctionBody;
187 
188   /// DeferredFunctionInfo - When function bodies are initially scanned, this
189   /// map contains info about where to find deferred function body in the
190   /// stream.
191   DenseMap<Function*, uint64_t> DeferredFunctionInfo;
192 
193   /// These are basic blocks forward-referenced by block addresses.  They are
194   /// inserted lazily into functions when they're loaded.  The basic block ID is
195   /// its index into the vector.
196   DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs;
197   std::deque<Function *> BasicBlockFwdRefQueue;
198 
199   /// UseRelativeIDs - Indicates that we are using a new encoding for
200   /// instruction operands where most operands in the current
201   /// FUNCTION_BLOCK are encoded relative to the instruction number,
202   /// for a more compact encoding.  Some instruction operands are not
203   /// relative to the instruction ID: basic block numbers, and types.
204   /// Once the old style function blocks have been phased out, we would
205   /// not need this flag.
206   bool UseRelativeIDs;
207 
208   /// True if all functions will be materialized, negating the need to process
209   /// (e.g.) blockaddress forward references.
210   bool WillMaterializeAllForwardRefs;
211 
212   /// Functions that have block addresses taken.  This is usually empty.
213   SmallPtrSet<const Function *, 4> BlockAddressesTaken;
214 
215 public:
216   std::error_code Error(BitcodeError E, const Twine &Message);
217   std::error_code Error(BitcodeError E);
218   std::error_code Error(const Twine &Message);
219 
220   explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C,
221                          DiagnosticHandlerFunction DiagnosticHandler);
222   explicit BitcodeReader(DataStreamer *streamer, LLVMContext &C,
223                          DiagnosticHandlerFunction DiagnosticHandler);
~BitcodeReader()224   ~BitcodeReader() { FreeState(); }
225 
226   std::error_code materializeForwardReferencedFunctions();
227 
228   void FreeState();
229 
230   void releaseBuffer();
231 
232   bool isDematerializable(const GlobalValue *GV) const override;
233   std::error_code materialize(GlobalValue *GV) override;
234   std::error_code MaterializeModule(Module *M) override;
235   std::vector<StructType *> getIdentifiedStructTypes() const override;
236   void Dematerialize(GlobalValue *GV) override;
237 
238   /// @brief Main interface to parsing a bitcode buffer.
239   /// @returns true if an error occurred.
240   std::error_code ParseBitcodeInto(Module *M);
241 
242   /// @brief Cheap mechanism to just extract module triple
243   /// @returns true if an error occurred.
244   ErrorOr<std::string> parseTriple();
245 
246   static uint64_t decodeSignRotatedValue(uint64_t V);
247 
248 private:
249   std::vector<StructType *> IdentifiedStructTypes;
250   StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name);
251   StructType *createIdentifiedStructType(LLVMContext &Context);
252 
253   Type *getTypeByID(unsigned ID);
getFnValueByID(unsigned ID,Type * Ty)254   Value *getFnValueByID(unsigned ID, Type *Ty) {
255     if (Ty && Ty->isMetadataTy())
256       return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID));
257     return ValueList.getValueFwdRef(ID, Ty);
258   }
getFnMetadataByID(unsigned ID)259   Metadata *getFnMetadataByID(unsigned ID) {
260     return MDValueList.getValueFwdRef(ID);
261   }
getBasicBlock(unsigned ID)262   BasicBlock *getBasicBlock(unsigned ID) const {
263     if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID
264     return FunctionBBs[ID];
265   }
getAttributes(unsigned i)266   AttributeSet getAttributes(unsigned i) const {
267     if (i-1 < MAttributes.size())
268       return MAttributes[i-1];
269     return AttributeSet();
270   }
271 
272   /// getValueTypePair - Read a value/type pair out of the specified record from
273   /// slot 'Slot'.  Increment Slot past the number of slots used in the record.
274   /// Return true on failure.
getValueTypePair(SmallVectorImpl<uint64_t> & Record,unsigned & Slot,unsigned InstNum,Value * & ResVal)275   bool getValueTypePair(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
276                         unsigned InstNum, Value *&ResVal) {
277     if (Slot == Record.size()) return true;
278     unsigned ValNo = (unsigned)Record[Slot++];
279     // Adjust the ValNo, if it was encoded relative to the InstNum.
280     if (UseRelativeIDs)
281       ValNo = InstNum - ValNo;
282     if (ValNo < InstNum) {
283       // If this is not a forward reference, just return the value we already
284       // have.
285       ResVal = getFnValueByID(ValNo, nullptr);
286       return ResVal == nullptr;
287     } else if (Slot == Record.size()) {
288       return true;
289     }
290 
291     unsigned TypeNo = (unsigned)Record[Slot++];
292     ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
293     return ResVal == nullptr;
294   }
295 
296   /// popValue - Read a value out of the specified record from slot 'Slot'.
297   /// Increment Slot past the number of slots used by the value in the record.
298   /// Return true if there is an error.
popValue(SmallVectorImpl<uint64_t> & Record,unsigned & Slot,unsigned InstNum,Type * Ty,Value * & ResVal)299   bool popValue(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
300                 unsigned InstNum, Type *Ty, Value *&ResVal) {
301     if (getValue(Record, Slot, InstNum, Ty, ResVal))
302       return true;
303     // All values currently take a single record slot.
304     ++Slot;
305     return false;
306   }
307 
308   /// getValue -- Like popValue, but does not increment the Slot number.
getValue(SmallVectorImpl<uint64_t> & Record,unsigned Slot,unsigned InstNum,Type * Ty,Value * & ResVal)309   bool getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
310                 unsigned InstNum, Type *Ty, Value *&ResVal) {
311     ResVal = getValue(Record, Slot, InstNum, Ty);
312     return ResVal == nullptr;
313   }
314 
315   /// getValue -- Version of getValue that returns ResVal directly,
316   /// or 0 if there is an error.
getValue(SmallVectorImpl<uint64_t> & Record,unsigned Slot,unsigned InstNum,Type * Ty)317   Value *getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
318                   unsigned InstNum, Type *Ty) {
319     if (Slot == Record.size()) return nullptr;
320     unsigned ValNo = (unsigned)Record[Slot];
321     // Adjust the ValNo, if it was encoded relative to the InstNum.
322     if (UseRelativeIDs)
323       ValNo = InstNum - ValNo;
324     return getFnValueByID(ValNo, Ty);
325   }
326 
327   /// getValueSigned -- Like getValue, but decodes signed VBRs.
getValueSigned(SmallVectorImpl<uint64_t> & Record,unsigned Slot,unsigned InstNum,Type * Ty)328   Value *getValueSigned(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
329                         unsigned InstNum, Type *Ty) {
330     if (Slot == Record.size()) return nullptr;
331     unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]);
332     // Adjust the ValNo, if it was encoded relative to the InstNum.
333     if (UseRelativeIDs)
334       ValNo = InstNum - ValNo;
335     return getFnValueByID(ValNo, Ty);
336   }
337 
338   std::error_code ParseAttrKind(uint64_t Code, Attribute::AttrKind *Kind);
339   std::error_code ParseModule(bool Resume);
340   std::error_code ParseAttributeBlock();
341   std::error_code ParseAttributeGroupBlock();
342   std::error_code ParseTypeTable();
343   std::error_code ParseTypeTableBody();
344 
345   std::error_code ParseValueSymbolTable();
346   std::error_code ParseConstants();
347   std::error_code RememberAndSkipFunctionBody();
348   std::error_code ParseFunctionBody(Function *F);
349   std::error_code GlobalCleanup();
350   std::error_code ResolveGlobalAndAliasInits();
351   std::error_code ParseMetadata();
352   std::error_code ParseMetadataAttachment();
353   ErrorOr<std::string> parseModuleTriple();
354   std::error_code ParseUseLists();
355   std::error_code InitStream();
356   std::error_code InitStreamFromBuffer();
357   std::error_code InitLazyStream();
358   std::error_code FindFunctionInStream(
359       Function *F,
360       DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator);
361 };
362 
363 } // End llvm namespace
364 
365 #endif
366