blob: 952d645a4c3eea9c19302845fbd4dc084668bc43 [file] [log] [blame]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001//===- BitcodeReader.h - Internal BitcodeReader impl ------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnercaee0dc2007-04-22 06:23:29 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This header defines the BitcodeReader class.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef BITCODE_READER_H
15#define BITCODE_READER_H
16
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000017#include "llvm/GVMaterializer.h"
Devang Pateleaf42ab2008-09-23 23:03:40 +000018#include "llvm/Attributes.h"
Chris Lattner522b7b12007-04-24 05:48:56 +000019#include "llvm/Type.h"
Gabor Greifefe65362008-05-10 08:32:32 +000020#include "llvm/OperandTraits.h"
Chris Lattner48f84872007-05-01 04:59:48 +000021#include "llvm/Bitcode/BitstreamReader.h"
Chris Lattner47f96bf2007-04-23 01:01:37 +000022#include "llvm/Bitcode/LLVMBitCodes.h"
Chris Lattner46e77402009-03-31 22:55:09 +000023#include "llvm/Support/ValueHandle.h"
Chris Lattner48f84872007-05-01 04:59:48 +000024#include "llvm/ADT/DenseMap.h"
Chris Lattnercaee0dc2007-04-22 06:23:29 +000025#include <vector>
26
27namespace llvm {
Chris Lattnerc453f762007-04-29 07:54:31 +000028 class MemoryBuffer;
Benjamin Kramer12ddd402009-08-11 17:45:13 +000029 class LLVMContext;
Chris Lattner522b7b12007-04-24 05:48:56 +000030
Gabor Greifefe65362008-05-10 08:32:32 +000031//===----------------------------------------------------------------------===//
32// BitcodeReaderValueList Class
33//===----------------------------------------------------------------------===//
34
Chris Lattner46e77402009-03-31 22:55:09 +000035class BitcodeReaderValueList {
36 std::vector<WeakVH> ValuePtrs;
Chris Lattnerea693df2008-08-21 02:34:16 +000037
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;
Chris Lattner7af453a2011-07-07 05:12:37 +000047 LLVMContext &Context;
Chris Lattner522b7b12007-04-24 05:48:56 +000048public:
Chris Lattner7af453a2011-07-07 05:12:37 +000049 BitcodeReaderValueList(LLVMContext &C) : Context(C) {}
Chris Lattnerea693df2008-08-21 02:34:16 +000050 ~BitcodeReaderValueList() {
51 assert(ResolveConstants.empty() && "Constants not resolved?");
52 }
Gabor Greifefe65362008-05-10 08:32:32 +000053
Chris Lattner522b7b12007-04-24 05:48:56 +000054 // vector compatibility methods
Chris Lattner46e77402009-03-31 22:55:09 +000055 unsigned size() const { return ValuePtrs.size(); }
56 void resize(unsigned N) { ValuePtrs.resize(N); }
Chris Lattner522b7b12007-04-24 05:48:56 +000057 void push_back(Value *V) {
Chris Lattner46e77402009-03-31 22:55:09 +000058 ValuePtrs.push_back(V);
Chris Lattner522b7b12007-04-24 05:48:56 +000059 }
60
Chris Lattnerb348bb82007-05-18 04:02:46 +000061 void clear() {
Chris Lattnerea693df2008-08-21 02:34:16 +000062 assert(ResolveConstants.empty() && "Constants not resolved?");
Chris Lattner46e77402009-03-31 22:55:09 +000063 ValuePtrs.clear();
Chris Lattnerb348bb82007-05-18 04:02:46 +000064 }
65
Chris Lattner46e77402009-03-31 22:55:09 +000066 Value *operator[](unsigned i) const {
67 assert(i < ValuePtrs.size());
68 return ValuePtrs[i];
69 }
Chris Lattner522b7b12007-04-24 05:48:56 +000070
Chris Lattner46e77402009-03-31 22:55:09 +000071 Value *back() const { return ValuePtrs.back(); }
72 void pop_back() { ValuePtrs.pop_back(); }
73 bool empty() const { return ValuePtrs.empty(); }
Chris Lattner198f34a2007-04-26 03:27:58 +000074 void shrinkTo(unsigned N) {
Chris Lattner46e77402009-03-31 22:55:09 +000075 assert(N <= size() && "Invalid shrinkTo request!");
76 ValuePtrs.resize(N);
Chris Lattner198f34a2007-04-26 03:27:58 +000077 }
Chris Lattner522b7b12007-04-24 05:48:56 +000078
Chris Lattnerdb125cf2011-07-18 04:54:35 +000079 Constant *getConstantFwdRef(unsigned Idx, Type *Ty);
80 Value *getValueFwdRef(unsigned Idx, Type *Ty);
Chris Lattnera7c49aa2007-05-01 07:01:57 +000081
Chris Lattner46e77402009-03-31 22:55:09 +000082 void AssignValue(Value *V, unsigned Idx);
Chris Lattnera7c49aa2007-05-01 07:01:57 +000083
Chris Lattnerea693df2008-08-21 02:34:16 +000084 /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
85 /// resolves any forward references.
86 void ResolveConstantForwardRefs();
Chris Lattner522b7b12007-04-24 05:48:56 +000087};
Gabor Greifefe65362008-05-10 08:32:32 +000088
Devang Pateld5ac4042009-08-04 06:00:18 +000089
90//===----------------------------------------------------------------------===//
91// BitcodeReaderMDValueList Class
92//===----------------------------------------------------------------------===//
93
94class BitcodeReaderMDValueList {
95 std::vector<WeakVH> MDValuePtrs;
96
Chris Lattner50b136d2009-10-28 05:53:48 +000097 LLVMContext &Context;
Devang Pateld5ac4042009-08-04 06:00:18 +000098public:
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
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000124class BitcodeReader : public GVMaterializer {
Chris Lattner50b136d2009-10-28 05:53:48 +0000125 LLVMContext &Context;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000126 Module *TheModule;
Chris Lattnerc453f762007-04-29 07:54:31 +0000127 MemoryBuffer *Buffer;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000128 bool BufferOwned;
Chris Lattner962dde32009-04-26 20:59:02 +0000129 BitstreamReader StreamFile;
130 BitstreamCursor Stream;
Chris Lattner48f84872007-05-01 04:59:48 +0000131
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000132 const char *ErrorString;
133
Chris Lattner1afcace2011-07-09 17:41:24 +0000134 std::vector<Type*> TypeList;
Chris Lattner522b7b12007-04-24 05:48:56 +0000135 BitcodeReaderValueList ValueList;
Devang Pateld5ac4042009-08-04 06:00:18 +0000136 BitcodeReaderMDValueList MDValueList;
Devang Patele8e02132009-09-18 19:26:43 +0000137 SmallVector<Instruction *, 64> InstructionList;
Chad Rosiercbbb0962011-12-07 21:44:12 +0000138 SmallVector<SmallVector<uint64_t, 64>, 64> UseListRecords;
Devang Patele8e02132009-09-18 19:26:43 +0000139
Chris Lattnere16504e2007-04-24 03:30:34 +0000140 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
Chris Lattner07d98b42007-04-26 02:46:40 +0000141 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
Chris Lattner48f84872007-05-01 04:59:48 +0000142
Devang Patel19c87462008-09-26 22:53:05 +0000143 /// MAttributes - The set of attributes by index. Index zero in the
Chris Lattner48c85b82007-05-04 03:30:17 +0000144 /// file is for null, and is thus not represented here. As such all indices
145 /// are off by one.
Devang Patel19c87462008-09-26 22:53:05 +0000146 std::vector<AttrListPtr> MAttributes;
Chris Lattner48c85b82007-05-04 03:30:17 +0000147
Chris Lattner980e5aa2007-05-01 05:52:21 +0000148 /// FunctionBBs - While parsing a function body, this is a list of the basic
149 /// blocks for the function.
150 std::vector<BasicBlock*> FunctionBBs;
151
Chris Lattner48f84872007-05-01 04:59:48 +0000152 // When reading the module header, this list is populated with functions that
153 // have bodies later in the file.
154 std::vector<Function*> FunctionsWithBodies;
Chandler Carruth69940402007-08-04 01:51:18 +0000155
156 // When intrinsic functions are encountered which require upgrading they are
157 // stored here with their replacement function.
158 typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
159 UpgradedIntrinsicMap UpgradedIntrinsics;
Dan Gohman19538d12010-07-20 21:42:28 +0000160
161 // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
162 DenseMap<unsigned, unsigned> MDKindMap;
Chris Lattner48f84872007-05-01 04:59:48 +0000163
164 // After the module header has been read, the FunctionsWithBodies list is
165 // reversed. This keeps track of whether we've done this yet.
166 bool HasReversedFunctionsWithBodies;
167
168 /// DeferredFunctionInfo - When function bodies are initially scanned, this
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000169 /// map contains info about where to find deferred function body in the
170 /// stream.
171 DenseMap<Function*, uint64_t> DeferredFunctionInfo;
Chris Lattner50b136d2009-10-28 05:53:48 +0000172
173 /// BlockAddrFwdRefs - These are blockaddr references to basic blocks. These
174 /// are resolved lazily when functions are loaded.
175 typedef std::pair<unsigned, GlobalVariable*> BlockAddrRefTy;
176 DenseMap<Function*, std::vector<BlockAddrRefTy> > BlockAddrFwdRefs;
Dan Gohman9b10dfb2010-09-13 18:00:48 +0000177
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000178public:
Chris Lattner08113472009-12-29 09:01:33 +0000179 explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C)
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000180 : Context(C), TheModule(0), Buffer(buffer), BufferOwned(false),
Chris Lattner020a5a42011-06-17 17:48:53 +0000181 ErrorString(0), ValueList(C), MDValueList(C) {
Chris Lattner48f84872007-05-01 04:59:48 +0000182 HasReversedFunctionsWithBodies = false;
183 }
Chris Lattnerb348bb82007-05-18 04:02:46 +0000184 ~BitcodeReader() {
185 FreeState();
186 }
Rafael Espindola47f79bb2012-01-02 07:49:53 +0000187
188 void materializeForwardReferencedFunctions();
189
Chris Lattnerb348bb82007-05-18 04:02:46 +0000190 void FreeState();
Chris Lattnerc453f762007-04-29 07:54:31 +0000191
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000192 /// setBufferOwned - If this is true, the reader will destroy the MemoryBuffer
193 /// when the reader is destroyed.
194 void setBufferOwned(bool Owned) { BufferOwned = Owned; }
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000195
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000196 virtual bool isMaterializable(const GlobalValue *GV) const;
197 virtual bool isDematerializable(const GlobalValue *GV) const;
198 virtual bool Materialize(GlobalValue *GV, std::string *ErrInfo = 0);
199 virtual bool MaterializeModule(Module *M, std::string *ErrInfo = 0);
200 virtual void Dematerialize(GlobalValue *GV);
Chris Lattnerd67c6322007-05-15 06:29:44 +0000201
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000202 bool Error(const char *Str) {
203 ErrorString = Str;
204 return true;
205 }
206 const char *getErrorString() const { return ErrorString; }
207
208 /// @brief Main interface to parsing a bitcode buffer.
209 /// @returns true if an error occurred.
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000210 bool ParseBitcodeInto(Module *M);
Bill Wendling34711742010-10-06 01:22:42 +0000211
212 /// @brief Cheap mechanism to just extract module triple
213 /// @returns true if an error occurred.
214 bool ParseTriple(std::string &Triple);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000215private:
Chris Lattner1afcace2011-07-09 17:41:24 +0000216 Type *getTypeByID(unsigned ID);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000217 Value *getFnValueByID(unsigned ID, Type *Ty) {
Chris Lattnercbd40f82011-07-07 05:29:18 +0000218 if (Ty && Ty->isMetadataTy())
Devang Pateld5ac4042009-08-04 06:00:18 +0000219 return MDValueList.getValueFwdRef(ID);
Chris Lattner7af453a2011-07-07 05:12:37 +0000220 return ValueList.getValueFwdRef(ID, Ty);
Chris Lattnera7c49aa2007-05-01 07:01:57 +0000221 }
Chris Lattnerf4c8e522007-05-02 05:46:45 +0000222 BasicBlock *getBasicBlock(unsigned ID) const {
223 if (ID >= FunctionBBs.size()) return 0; // Invalid ID
224 return FunctionBBs[ID];
225 }
Devang Patel05988662008-09-25 21:00:45 +0000226 AttrListPtr getAttributes(unsigned i) const {
Devang Patel19c87462008-09-26 22:53:05 +0000227 if (i-1 < MAttributes.size())
228 return MAttributes[i-1];
Devang Patel05988662008-09-25 21:00:45 +0000229 return AttrListPtr();
Chris Lattner48c85b82007-05-04 03:30:17 +0000230 }
Chris Lattner7337ab92007-05-06 00:00:00 +0000231
232 /// getValueTypePair - Read a value/type pair out of the specified record from
233 /// slot 'Slot'. Increment Slot past the number of slots used in the record.
234 /// Return true on failure.
235 bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
236 unsigned InstNum, Value *&ResVal) {
237 if (Slot == Record.size()) return true;
Jeff Cohen650c9382007-05-06 03:23:14 +0000238 unsigned ValNo = (unsigned)Record[Slot++];
Chris Lattner7337ab92007-05-06 00:00:00 +0000239 if (ValNo < InstNum) {
240 // If this is not a forward reference, just return the value we already
241 // have.
242 ResVal = getFnValueByID(ValNo, 0);
243 return ResVal == 0;
244 } else if (Slot == Record.size()) {
245 return true;
246 }
247
Jeff Cohen650c9382007-05-06 03:23:14 +0000248 unsigned TypeNo = (unsigned)Record[Slot++];
Chris Lattner7337ab92007-05-06 00:00:00 +0000249 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
250 return ResVal == 0;
251 }
252 bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000253 Type *Ty, Value *&ResVal) {
Chris Lattner7337ab92007-05-06 00:00:00 +0000254 if (Slot == Record.size()) return true;
Jeff Cohen650c9382007-05-06 03:23:14 +0000255 unsigned ValNo = (unsigned)Record[Slot++];
Chris Lattner7337ab92007-05-06 00:00:00 +0000256 ResVal = getFnValueByID(ValNo, Ty);
257 return ResVal == 0;
258 }
Chris Lattner48c85b82007-05-04 03:30:17 +0000259
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000260
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000261 bool ParseModule();
Devang Patel05988662008-09-25 21:00:45 +0000262 bool ParseAttributeBlock();
Chris Lattner86697142007-05-01 05:01:34 +0000263 bool ParseTypeTable();
Chris Lattner1afcace2011-07-09 17:41:24 +0000264 bool ParseTypeTableBody();
265
Chris Lattner86697142007-05-01 05:01:34 +0000266 bool ParseValueSymbolTable();
267 bool ParseConstants();
Chris Lattner980e5aa2007-05-01 05:52:21 +0000268 bool RememberAndSkipFunctionBody();
269 bool ParseFunctionBody(Function *F);
Chris Lattner07d98b42007-04-26 02:46:40 +0000270 bool ResolveGlobalAndAliasInits();
Devang Patele54abc92009-07-22 17:43:22 +0000271 bool ParseMetadata();
Devang Patele8e02132009-09-18 19:26:43 +0000272 bool ParseMetadataAttachment();
Bill Wendling34711742010-10-06 01:22:42 +0000273 bool ParseModuleTriple(std::string &Triple);
Chad Rosiercbbb0962011-12-07 21:44:12 +0000274 bool ParseUseLists();
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000275};
276
277} // End llvm namespace
278
279#endif