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