blob: d59f12898e974cc5dd1d33ff670e1dde6b88a480 [file] [log] [blame]
Chris Lattner1314b992007-04-22 06:23:29 +00001//===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Lattner1314b992007-04-22 06:23:29 +00007//
8//===----------------------------------------------------------------------===//
Chris Lattner1314b992007-04-22 06:23:29 +00009
Chris Lattner6694f602007-04-29 07:54:31 +000010#include "llvm/Bitcode/ReaderWriter.h"
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000011#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000012#include "llvm/ADT/SmallString.h"
13#include "llvm/ADT/SmallVector.h"
David Majnemer3087b222015-01-20 05:58:07 +000014#include "llvm/ADT/Triple.h"
Benjamin Kramercced8be2015-03-17 20:40:24 +000015#include "llvm/Bitcode/BitstreamReader.h"
Tobias Grosser0a8e12f2013-07-26 04:16:55 +000016#include "llvm/Bitcode/LLVMBitCodes.h"
Chandler Carruth91065212014-03-05 10:34:14 +000017#include "llvm/IR/AutoUpgrade.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Constants.h"
Rafael Espindola0d68b4c2015-03-30 21:36:43 +000019#include "llvm/IR/DebugInfo.h"
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000020#include "llvm/IR/DebugInfoMetadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/DerivedTypes.h"
Rafael Espindolad0b23be2015-01-10 00:07:30 +000022#include "llvm/IR/DiagnosticPrinter.h"
Benjamin Kramercced8be2015-03-17 20:40:24 +000023#include "llvm/IR/GVMaterializer.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/InlineAsm.h"
25#include "llvm/IR/IntrinsicInst.h"
Manman Ren209b17c2013-09-28 00:22:27 +000026#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Module.h"
28#include "llvm/IR/OperandTraits.h"
29#include "llvm/IR/Operator.h"
Teresa Johnson403a7872015-10-04 14:33:43 +000030#include "llvm/IR/FunctionInfo.h"
Benjamin Kramercced8be2015-03-17 20:40:24 +000031#include "llvm/IR/ValueHandle.h"
Derek Schuff8b2dcad2012-02-06 22:30:29 +000032#include "llvm/Support/DataStream.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000033#include "llvm/Support/ManagedStatic.h"
Chris Lattner08feb1e2007-04-24 04:04:35 +000034#include "llvm/Support/MathExtras.h"
Chris Lattner6694f602007-04-29 07:54:31 +000035#include "llvm/Support/MemoryBuffer.h"
Tobias Grosser0a8e12f2013-07-26 04:16:55 +000036#include "llvm/Support/raw_ostream.h"
Benjamin Kramercced8be2015-03-17 20:40:24 +000037#include <deque>
Chris Lattner1314b992007-04-22 06:23:29 +000038using namespace llvm;
39
Benjamin Kramercced8be2015-03-17 20:40:24 +000040namespace {
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +000041enum {
42 SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex
43};
44
Benjamin Kramercced8be2015-03-17 20:40:24 +000045class BitcodeReaderValueList {
46 std::vector<WeakVH> ValuePtrs;
47
Rafael Espindolacbdcb502015-06-15 20:55:37 +000048 /// As we resolve forward-referenced constants, we add information about them
49 /// to this vector. This allows us to resolve them in bulk instead of
50 /// resolving each reference at a time. See the code in
Benjamin Kramercced8be2015-03-17 20:40:24 +000051 /// ResolveConstantForwardRefs for more information about this.
52 ///
53 /// The key of this vector is the placeholder constant, the value is the slot
54 /// number that holds the resolved value.
55 typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy;
56 ResolveConstantsTy ResolveConstants;
57 LLVMContext &Context;
58public:
59 BitcodeReaderValueList(LLVMContext &C) : Context(C) {}
60 ~BitcodeReaderValueList() {
61 assert(ResolveConstants.empty() && "Constants not resolved?");
62 }
63
64 // vector compatibility methods
65 unsigned size() const { return ValuePtrs.size(); }
66 void resize(unsigned N) { ValuePtrs.resize(N); }
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +000067 void push_back(Value *V) { ValuePtrs.emplace_back(V); }
Benjamin Kramercced8be2015-03-17 20:40:24 +000068
69 void clear() {
70 assert(ResolveConstants.empty() && "Constants not resolved?");
71 ValuePtrs.clear();
72 }
73
74 Value *operator[](unsigned i) const {
75 assert(i < ValuePtrs.size());
76 return ValuePtrs[i];
77 }
78
79 Value *back() const { return ValuePtrs.back(); }
80 void pop_back() { ValuePtrs.pop_back(); }
81 bool empty() const { return ValuePtrs.empty(); }
82 void shrinkTo(unsigned N) {
83 assert(N <= size() && "Invalid shrinkTo request!");
84 ValuePtrs.resize(N);
85 }
86
87 Constant *getConstantFwdRef(unsigned Idx, Type *Ty);
David Majnemer8a1c45d2015-12-12 05:38:55 +000088 Value *getValueFwdRef(unsigned Idx, Type *Ty);
Benjamin Kramercced8be2015-03-17 20:40:24 +000089
David Majnemer8a1c45d2015-12-12 05:38:55 +000090 void assignValue(Value *V, unsigned Idx);
Benjamin Kramercced8be2015-03-17 20:40:24 +000091
Rafael Espindolacbdcb502015-06-15 20:55:37 +000092 /// Once all constants are read, this method bulk resolves any forward
93 /// references.
94 void resolveConstantForwardRefs();
Benjamin Kramercced8be2015-03-17 20:40:24 +000095};
96
97class BitcodeReaderMDValueList {
98 unsigned NumFwdRefs;
99 bool AnyFwdRefs;
100 unsigned MinFwdRef;
101 unsigned MaxFwdRef;
102 std::vector<TrackingMDRef> MDValuePtrs;
103
104 LLVMContext &Context;
105public:
106 BitcodeReaderMDValueList(LLVMContext &C)
107 : NumFwdRefs(0), AnyFwdRefs(false), Context(C) {}
108
109 // vector compatibility methods
110 unsigned size() const { return MDValuePtrs.size(); }
111 void resize(unsigned N) { MDValuePtrs.resize(N); }
112 void push_back(Metadata *MD) { MDValuePtrs.emplace_back(MD); }
113 void clear() { MDValuePtrs.clear(); }
114 Metadata *back() const { return MDValuePtrs.back(); }
115 void pop_back() { MDValuePtrs.pop_back(); }
116 bool empty() const { return MDValuePtrs.empty(); }
117
118 Metadata *operator[](unsigned i) const {
119 assert(i < MDValuePtrs.size());
120 return MDValuePtrs[i];
121 }
122
123 void shrinkTo(unsigned N) {
124 assert(N <= size() && "Invalid shrinkTo request!");
125 MDValuePtrs.resize(N);
126 }
127
128 Metadata *getValueFwdRef(unsigned Idx);
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000129 void assignValue(Metadata *MD, unsigned Idx);
Benjamin Kramercced8be2015-03-17 20:40:24 +0000130 void tryToResolveCycles();
131};
132
133class BitcodeReader : public GVMaterializer {
134 LLVMContext &Context;
135 DiagnosticHandlerFunction DiagnosticHandler;
Rafael Espindola4223a1f2015-06-15 20:08:17 +0000136 Module *TheModule = nullptr;
Benjamin Kramercced8be2015-03-17 20:40:24 +0000137 std::unique_ptr<MemoryBuffer> Buffer;
138 std::unique_ptr<BitstreamReader> StreamFile;
139 BitstreamCursor Stream;
Teresa Johnson1493ad92015-10-10 14:18:36 +0000140 // Next offset to start scanning for lazy parsing of function bodies.
Rafael Espindola4223a1f2015-06-15 20:08:17 +0000141 uint64_t NextUnreadBit = 0;
Teresa Johnson1493ad92015-10-10 14:18:36 +0000142 // Last function offset found in the VST.
143 uint64_t LastFunctionBlockBit = 0;
Rafael Espindola4223a1f2015-06-15 20:08:17 +0000144 bool SeenValueSymbolTable = false;
Peter Collingbourne128a9762015-10-27 23:01:25 +0000145 uint64_t VSTOffset = 0;
Mehdi Amini5d303282015-10-26 18:37:00 +0000146 // Contains an arbitrary and optional string identifying the bitcode producer
147 std::string ProducerIdentification;
Teresa Johnsond4d3dfd2015-11-20 14:51:27 +0000148 // Number of module level metadata records specified by the
149 // MODULE_CODE_METADATA_VALUES record.
150 unsigned NumModuleMDs = 0;
151 // Support older bitcode without the MODULE_CODE_METADATA_VALUES record.
152 bool SeenModuleValuesRecord = false;
Benjamin Kramercced8be2015-03-17 20:40:24 +0000153
154 std::vector<Type*> TypeList;
155 BitcodeReaderValueList ValueList;
156 BitcodeReaderMDValueList MDValueList;
157 std::vector<Comdat *> ComdatList;
158 SmallVector<Instruction *, 64> InstructionList;
159
160 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
161 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
162 std::vector<std::pair<Function*, unsigned> > FunctionPrefixes;
163 std::vector<std::pair<Function*, unsigned> > FunctionPrologues;
David Majnemer7fddecc2015-06-17 20:52:32 +0000164 std::vector<std::pair<Function*, unsigned> > FunctionPersonalityFns;
Benjamin Kramercced8be2015-03-17 20:40:24 +0000165
166 SmallVector<Instruction*, 64> InstsWithTBAATag;
167
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000168 /// The set of attributes by index. Index zero in the file is for null, and
169 /// is thus not represented here. As such all indices are off by one.
Benjamin Kramercced8be2015-03-17 20:40:24 +0000170 std::vector<AttributeSet> MAttributes;
171
Karl Schimpf36440082015-08-31 16:43:55 +0000172 /// The set of attribute groups.
Benjamin Kramercced8be2015-03-17 20:40:24 +0000173 std::map<unsigned, AttributeSet> MAttributeGroups;
174
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000175 /// While parsing a function body, this is a list of the basic blocks for the
176 /// function.
Benjamin Kramercced8be2015-03-17 20:40:24 +0000177 std::vector<BasicBlock*> FunctionBBs;
178
179 // When reading the module header, this list is populated with functions that
180 // have bodies later in the file.
181 std::vector<Function*> FunctionsWithBodies;
182
183 // When intrinsic functions are encountered which require upgrading they are
184 // stored here with their replacement function.
Rafael Espindola4e721212015-07-02 16:22:40 +0000185 typedef DenseMap<Function*, Function*> UpgradedIntrinsicMap;
Benjamin Kramercced8be2015-03-17 20:40:24 +0000186 UpgradedIntrinsicMap UpgradedIntrinsics;
187
188 // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
189 DenseMap<unsigned, unsigned> MDKindMap;
190
191 // Several operations happen after the module header has been read, but
192 // before function bodies are processed. This keeps track of whether
193 // we've done this yet.
Rafael Espindola4223a1f2015-06-15 20:08:17 +0000194 bool SeenFirstFunctionBody = false;
Benjamin Kramercced8be2015-03-17 20:40:24 +0000195
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000196 /// When function bodies are initially scanned, this map contains info about
197 /// where to find deferred function body in the stream.
Benjamin Kramercced8be2015-03-17 20:40:24 +0000198 DenseMap<Function*, uint64_t> DeferredFunctionInfo;
199
200 /// When Metadata block is initially scanned when parsing the module, we may
201 /// choose to defer parsing of the metadata. This vector contains info about
202 /// which Metadata blocks are deferred.
203 std::vector<uint64_t> DeferredMetadataInfo;
204
205 /// These are basic blocks forward-referenced by block addresses. They are
206 /// inserted lazily into functions when they're loaded. The basic block ID is
207 /// its index into the vector.
208 DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs;
209 std::deque<Function *> BasicBlockFwdRefQueue;
210
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000211 /// Indicates that we are using a new encoding for instruction operands where
212 /// most operands in the current FUNCTION_BLOCK are encoded relative to the
213 /// instruction number, for a more compact encoding. Some instruction
214 /// operands are not relative to the instruction ID: basic block numbers, and
215 /// types. Once the old style function blocks have been phased out, we would
Benjamin Kramercced8be2015-03-17 20:40:24 +0000216 /// not need this flag.
Rafael Espindola4223a1f2015-06-15 20:08:17 +0000217 bool UseRelativeIDs = false;
Benjamin Kramercced8be2015-03-17 20:40:24 +0000218
219 /// True if all functions will be materialized, negating the need to process
220 /// (e.g.) blockaddress forward references.
Rafael Espindola4223a1f2015-06-15 20:08:17 +0000221 bool WillMaterializeAllForwardRefs = false;
Benjamin Kramercced8be2015-03-17 20:40:24 +0000222
223 /// Functions that have block addresses taken. This is usually empty.
224 SmallPtrSet<const Function *, 4> BlockAddressesTaken;
225
226 /// True if any Metadata block has been materialized.
Rafael Espindola4223a1f2015-06-15 20:08:17 +0000227 bool IsMetadataMaterialized = false;
Benjamin Kramercced8be2015-03-17 20:40:24 +0000228
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000229 bool StripDebugInfo = false;
230
Peter Collingbourned4bff302015-11-05 22:03:56 +0000231 /// Functions that need to be matched with subprograms when upgrading old
232 /// metadata.
233 SmallDenseMap<Function *, DISubprogram *, 16> FunctionsWithSPs;
234
Sanjoy Dasb513a9f2015-09-24 23:34:52 +0000235 std::vector<std::string> BundleTags;
236
Benjamin Kramercced8be2015-03-17 20:40:24 +0000237public:
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000238 std::error_code error(BitcodeError E, const Twine &Message);
239 std::error_code error(BitcodeError E);
240 std::error_code error(const Twine &Message);
Benjamin Kramercced8be2015-03-17 20:40:24 +0000241
Rafael Espindola4223a1f2015-06-15 20:08:17 +0000242 BitcodeReader(MemoryBuffer *Buffer, LLVMContext &Context,
243 DiagnosticHandlerFunction DiagnosticHandler);
Rafael Espindola1aabf982015-06-16 23:29:49 +0000244 BitcodeReader(LLVMContext &Context,
Rafael Espindola4223a1f2015-06-15 20:08:17 +0000245 DiagnosticHandlerFunction DiagnosticHandler);
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000246 ~BitcodeReader() override { freeState(); }
Benjamin Kramercced8be2015-03-17 20:40:24 +0000247
248 std::error_code materializeForwardReferencedFunctions();
249
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000250 void freeState();
Benjamin Kramercced8be2015-03-17 20:40:24 +0000251
252 void releaseBuffer();
253
254 bool isDematerializable(const GlobalValue *GV) const override;
255 std::error_code materialize(GlobalValue *GV) override;
Eric Christopher97cb5652015-05-15 18:20:14 +0000256 std::error_code materializeModule(Module *M) override;
Benjamin Kramercced8be2015-03-17 20:40:24 +0000257 std::vector<StructType *> getIdentifiedStructTypes() const override;
Eric Christopher97cb5652015-05-15 18:20:14 +0000258 void dematerialize(GlobalValue *GV) override;
Benjamin Kramercced8be2015-03-17 20:40:24 +0000259
Rafael Espindola6ace6852015-06-15 21:02:49 +0000260 /// \brief Main interface to parsing a bitcode buffer.
261 /// \returns true if an error occurred.
Rafael Espindola1aabf982015-06-16 23:29:49 +0000262 std::error_code parseBitcodeInto(std::unique_ptr<DataStreamer> Streamer,
263 Module *M,
Benjamin Kramercced8be2015-03-17 20:40:24 +0000264 bool ShouldLazyLoadMetadata = false);
265
Rafael Espindola6ace6852015-06-15 21:02:49 +0000266 /// \brief Cheap mechanism to just extract module triple
267 /// \returns true if an error occurred.
Benjamin Kramercced8be2015-03-17 20:40:24 +0000268 ErrorOr<std::string> parseTriple();
269
Mehdi Amini3383ccc2015-11-09 02:46:41 +0000270 /// Cheap mechanism to just extract the identification block out of bitcode.
271 ErrorOr<std::string> parseIdentificationBlock();
272
Benjamin Kramercced8be2015-03-17 20:40:24 +0000273 static uint64_t decodeSignRotatedValue(uint64_t V);
274
275 /// Materialize any deferred Metadata block.
276 std::error_code materializeMetadata() override;
277
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000278 void setStripDebugInfo() override;
279
Benjamin Kramercced8be2015-03-17 20:40:24 +0000280private:
Mehdi Amini5d303282015-10-26 18:37:00 +0000281 /// Parse the "IDENTIFICATION_BLOCK_ID" block, populate the
282 // ProducerIdentification data member, and do some basic enforcement on the
283 // "epoch" encoded in the bitcode.
284 std::error_code parseBitcodeVersion();
285
Benjamin Kramercced8be2015-03-17 20:40:24 +0000286 std::vector<StructType *> IdentifiedStructTypes;
287 StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name);
288 StructType *createIdentifiedStructType(LLVMContext &Context);
289
290 Type *getTypeByID(unsigned ID);
David Majnemer8a1c45d2015-12-12 05:38:55 +0000291 Value *getFnValueByID(unsigned ID, Type *Ty) {
Benjamin Kramercced8be2015-03-17 20:40:24 +0000292 if (Ty && Ty->isMetadataTy())
293 return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID));
David Majnemer8a1c45d2015-12-12 05:38:55 +0000294 return ValueList.getValueFwdRef(ID, Ty);
Benjamin Kramercced8be2015-03-17 20:40:24 +0000295 }
296 Metadata *getFnMetadataByID(unsigned ID) {
297 return MDValueList.getValueFwdRef(ID);
298 }
299 BasicBlock *getBasicBlock(unsigned ID) const {
300 if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID
301 return FunctionBBs[ID];
302 }
303 AttributeSet getAttributes(unsigned i) const {
304 if (i-1 < MAttributes.size())
305 return MAttributes[i-1];
306 return AttributeSet();
307 }
308
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000309 /// Read a value/type pair out of the specified record from slot 'Slot'.
310 /// Increment Slot past the number of slots used in the record. Return true on
311 /// failure.
Benjamin Kramercced8be2015-03-17 20:40:24 +0000312 bool getValueTypePair(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
313 unsigned InstNum, Value *&ResVal) {
314 if (Slot == Record.size()) return true;
315 unsigned ValNo = (unsigned)Record[Slot++];
316 // Adjust the ValNo, if it was encoded relative to the InstNum.
317 if (UseRelativeIDs)
318 ValNo = InstNum - ValNo;
319 if (ValNo < InstNum) {
320 // If this is not a forward reference, just return the value we already
321 // have.
322 ResVal = getFnValueByID(ValNo, nullptr);
323 return ResVal == nullptr;
Benjamin Kramercced8be2015-03-17 20:40:24 +0000324 }
David Blaikiedbe6e0f2015-04-17 06:40:14 +0000325 if (Slot == Record.size())
326 return true;
Benjamin Kramercced8be2015-03-17 20:40:24 +0000327
328 unsigned TypeNo = (unsigned)Record[Slot++];
329 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
330 return ResVal == nullptr;
331 }
332
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000333 /// Read a value out of the specified record from slot 'Slot'. Increment Slot
334 /// past the number of slots used by the value in the record. Return true if
335 /// there is an error.
Benjamin Kramercced8be2015-03-17 20:40:24 +0000336 bool popValue(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
David Majnemer8a1c45d2015-12-12 05:38:55 +0000337 unsigned InstNum, Type *Ty, Value *&ResVal) {
338 if (getValue(Record, Slot, InstNum, Ty, ResVal))
Benjamin Kramercced8be2015-03-17 20:40:24 +0000339 return true;
340 // All values currently take a single record slot.
341 ++Slot;
342 return false;
343 }
344
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000345 /// Like popValue, but does not increment the Slot number.
Benjamin Kramercced8be2015-03-17 20:40:24 +0000346 bool getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
David Majnemer8a1c45d2015-12-12 05:38:55 +0000347 unsigned InstNum, Type *Ty, Value *&ResVal) {
348 ResVal = getValue(Record, Slot, InstNum, Ty);
Benjamin Kramercced8be2015-03-17 20:40:24 +0000349 return ResVal == nullptr;
350 }
351
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000352 /// Version of getValue that returns ResVal directly, or 0 if there is an
353 /// error.
Benjamin Kramercced8be2015-03-17 20:40:24 +0000354 Value *getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
David Majnemer8a1c45d2015-12-12 05:38:55 +0000355 unsigned InstNum, Type *Ty) {
Benjamin Kramercced8be2015-03-17 20:40:24 +0000356 if (Slot == Record.size()) return nullptr;
357 unsigned ValNo = (unsigned)Record[Slot];
358 // Adjust the ValNo, if it was encoded relative to the InstNum.
359 if (UseRelativeIDs)
360 ValNo = InstNum - ValNo;
David Majnemer8a1c45d2015-12-12 05:38:55 +0000361 return getFnValueByID(ValNo, Ty);
Benjamin Kramercced8be2015-03-17 20:40:24 +0000362 }
363
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000364 /// Like getValue, but decodes signed VBRs.
Benjamin Kramercced8be2015-03-17 20:40:24 +0000365 Value *getValueSigned(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
David Majnemer8a1c45d2015-12-12 05:38:55 +0000366 unsigned InstNum, Type *Ty) {
Benjamin Kramercced8be2015-03-17 20:40:24 +0000367 if (Slot == Record.size()) return nullptr;
368 unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]);
369 // Adjust the ValNo, if it was encoded relative to the InstNum.
370 if (UseRelativeIDs)
371 ValNo = InstNum - ValNo;
David Majnemer8a1c45d2015-12-12 05:38:55 +0000372 return getFnValueByID(ValNo, Ty);
Benjamin Kramercced8be2015-03-17 20:40:24 +0000373 }
374
375 /// Converts alignment exponent (i.e. power of two (or zero)) to the
376 /// corresponding alignment to use. If alignment is too large, returns
377 /// a corresponding error code.
378 std::error_code parseAlignmentValue(uint64_t Exponent, unsigned &Alignment);
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000379 std::error_code parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind);
Teresa Johnson1493ad92015-10-10 14:18:36 +0000380 std::error_code parseModule(uint64_t ResumeBit,
381 bool ShouldLazyLoadMetadata = false);
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000382 std::error_code parseAttributeBlock();
383 std::error_code parseAttributeGroupBlock();
384 std::error_code parseTypeTable();
385 std::error_code parseTypeTableBody();
Sanjoy Dasb513a9f2015-09-24 23:34:52 +0000386 std::error_code parseOperandBundleTags();
Benjamin Kramercced8be2015-03-17 20:40:24 +0000387
Teresa Johnsonff642b92015-09-17 20:12:00 +0000388 ErrorOr<Value *> recordValue(SmallVectorImpl<uint64_t> &Record,
389 unsigned NameIndex, Triple &TT);
Peter Collingbourne128a9762015-10-27 23:01:25 +0000390 std::error_code parseValueSymbolTable(uint64_t Offset = 0);
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000391 std::error_code parseConstants();
Teresa Johnson1493ad92015-10-10 14:18:36 +0000392 std::error_code rememberAndSkipFunctionBodies();
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000393 std::error_code rememberAndSkipFunctionBody();
Benjamin Kramercced8be2015-03-17 20:40:24 +0000394 /// Save the positions of the Metadata blocks and skip parsing the blocks.
395 std::error_code rememberAndSkipMetadata();
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000396 std::error_code parseFunctionBody(Function *F);
397 std::error_code globalCleanup();
398 std::error_code resolveGlobalAndAliasInits();
Teresa Johnsond4d3dfd2015-11-20 14:51:27 +0000399 std::error_code parseMetadata(bool ModuleLevel = false);
Teresa Johnson12545072015-11-15 02:00:09 +0000400 std::error_code parseMetadataKinds();
401 std::error_code parseMetadataKindRecord(SmallVectorImpl<uint64_t> &Record);
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000402 std::error_code parseMetadataAttachment(Function &F);
Benjamin Kramercced8be2015-03-17 20:40:24 +0000403 ErrorOr<std::string> parseModuleTriple();
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000404 std::error_code parseUseLists();
Rafael Espindola1aabf982015-06-16 23:29:49 +0000405 std::error_code initStream(std::unique_ptr<DataStreamer> Streamer);
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000406 std::error_code initStreamFromBuffer();
Rafael Espindola1aabf982015-06-16 23:29:49 +0000407 std::error_code initLazyStream(std::unique_ptr<DataStreamer> Streamer);
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000408 std::error_code findFunctionInStream(
Benjamin Kramercced8be2015-03-17 20:40:24 +0000409 Function *F,
410 DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator);
411};
Teresa Johnson403a7872015-10-04 14:33:43 +0000412
413/// Class to manage reading and parsing function summary index bitcode
414/// files/sections.
415class FunctionIndexBitcodeReader {
Teresa Johnson403a7872015-10-04 14:33:43 +0000416 DiagnosticHandlerFunction DiagnosticHandler;
417
418 /// Eventually points to the function index built during parsing.
419 FunctionInfoIndex *TheIndex = nullptr;
420
421 std::unique_ptr<MemoryBuffer> Buffer;
422 std::unique_ptr<BitstreamReader> StreamFile;
423 BitstreamCursor Stream;
424
425 /// \brief Used to indicate whether we are doing lazy parsing of summary data.
426 ///
427 /// If false, the summary section is fully parsed into the index during
428 /// the initial parse. Otherwise, if true, the caller is expected to
429 /// invoke \a readFunctionSummary for each summary needed, and the summary
430 /// section is thus parsed lazily.
431 bool IsLazy = false;
432
433 /// Used to indicate whether caller only wants to check for the presence
434 /// of the function summary bitcode section. All blocks are skipped,
435 /// but the SeenFuncSummary boolean is set.
436 bool CheckFuncSummaryPresenceOnly = false;
437
438 /// Indicates whether we have encountered a function summary section
439 /// yet during parsing, used when checking if file contains function
440 /// summary section.
441 bool SeenFuncSummary = false;
442
443 /// \brief Map populated during function summary section parsing, and
444 /// consumed during ValueSymbolTable parsing.
445 ///
446 /// Used to correlate summary records with VST entries. For the per-module
447 /// index this maps the ValueID to the parsed function summary, and
448 /// for the combined index this maps the summary record's bitcode
449 /// offset to the function summary (since in the combined index the
450 /// VST records do not hold value IDs but rather hold the function
451 /// summary record offset).
452 DenseMap<uint64_t, std::unique_ptr<FunctionSummary>> SummaryMap;
453
454 /// Map populated during module path string table parsing, from the
455 /// module ID to a string reference owned by the index's module
456 /// path string table, used to correlate with combined index function
457 /// summary records.
458 DenseMap<uint64_t, StringRef> ModuleIdMap;
459
Teresa Johnsonf72278f2015-11-02 18:02:11 +0000460public:
Teresa Johnson403a7872015-10-04 14:33:43 +0000461 std::error_code error(BitcodeError E, const Twine &Message);
462 std::error_code error(BitcodeError E);
463 std::error_code error(const Twine &Message);
464
Mehdi Amini354f5202015-11-19 05:52:29 +0000465 FunctionIndexBitcodeReader(MemoryBuffer *Buffer,
Teresa Johnson403a7872015-10-04 14:33:43 +0000466 DiagnosticHandlerFunction DiagnosticHandler,
467 bool IsLazy = false,
468 bool CheckFuncSummaryPresenceOnly = false);
Mehdi Amini354f5202015-11-19 05:52:29 +0000469 FunctionIndexBitcodeReader(DiagnosticHandlerFunction DiagnosticHandler,
Teresa Johnson403a7872015-10-04 14:33:43 +0000470 bool IsLazy = false,
471 bool CheckFuncSummaryPresenceOnly = false);
472 ~FunctionIndexBitcodeReader() { freeState(); }
473
474 void freeState();
475
476 void releaseBuffer();
477
478 /// Check if the parser has encountered a function summary section.
479 bool foundFuncSummary() { return SeenFuncSummary; }
480
481 /// \brief Main interface to parsing a bitcode buffer.
482 /// \returns true if an error occurred.
483 std::error_code parseSummaryIndexInto(std::unique_ptr<DataStreamer> Streamer,
484 FunctionInfoIndex *I);
485
486 /// \brief Interface for parsing a function summary lazily.
487 std::error_code parseFunctionSummary(std::unique_ptr<DataStreamer> Streamer,
488 FunctionInfoIndex *I,
489 size_t FunctionSummaryOffset);
490
Teresa Johnsonf72278f2015-11-02 18:02:11 +0000491private:
Teresa Johnson403a7872015-10-04 14:33:43 +0000492 std::error_code parseModule();
493 std::error_code parseValueSymbolTable();
494 std::error_code parseEntireSummary();
495 std::error_code parseModuleStringTable();
496 std::error_code initStream(std::unique_ptr<DataStreamer> Streamer);
497 std::error_code initStreamFromBuffer();
498 std::error_code initLazyStream(std::unique_ptr<DataStreamer> Streamer);
499};
Benjamin Kramercced8be2015-03-17 20:40:24 +0000500} // namespace
501
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000502BitcodeDiagnosticInfo::BitcodeDiagnosticInfo(std::error_code EC,
503 DiagnosticSeverity Severity,
504 const Twine &Msg)
505 : DiagnosticInfo(DK_Bitcode, Severity), Msg(Msg), EC(EC) {}
506
507void BitcodeDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; }
508
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000509static std::error_code error(DiagnosticHandlerFunction DiagnosticHandler,
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000510 std::error_code EC, const Twine &Message) {
511 BitcodeDiagnosticInfo DI(EC, DS_Error, Message);
512 DiagnosticHandler(DI);
513 return EC;
514}
515
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000516static std::error_code error(DiagnosticHandlerFunction DiagnosticHandler,
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000517 std::error_code EC) {
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000518 return error(DiagnosticHandler, EC, EC.message());
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000519}
520
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000521static std::error_code error(DiagnosticHandlerFunction DiagnosticHandler,
Filipe Cabecinhas11bb8492015-05-18 21:48:55 +0000522 const Twine &Message) {
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000523 return error(DiagnosticHandler,
Filipe Cabecinhas11bb8492015-05-18 21:48:55 +0000524 make_error_code(BitcodeError::CorruptedBitcode), Message);
525}
526
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000527std::error_code BitcodeReader::error(BitcodeError E, const Twine &Message) {
Mehdi Amini5d303282015-10-26 18:37:00 +0000528 if (!ProducerIdentification.empty()) {
Filipe Cabecinhasf3e167a2015-11-03 13:48:21 +0000529 return ::error(DiagnosticHandler, make_error_code(E),
530 Message + " (Producer: '" + ProducerIdentification +
531 "' Reader: 'LLVM " + LLVM_VERSION_STRING "')");
Mehdi Amini5d303282015-10-26 18:37:00 +0000532 }
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000533 return ::error(DiagnosticHandler, make_error_code(E), Message);
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000534}
535
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000536std::error_code BitcodeReader::error(const Twine &Message) {
Mehdi Amini5d303282015-10-26 18:37:00 +0000537 if (!ProducerIdentification.empty()) {
Mehdi Amini5d303282015-10-26 18:37:00 +0000538 return ::error(DiagnosticHandler,
Filipe Cabecinhasf3e167a2015-11-03 13:48:21 +0000539 make_error_code(BitcodeError::CorruptedBitcode),
540 Message + " (Producer: '" + ProducerIdentification +
541 "' Reader: 'LLVM " + LLVM_VERSION_STRING "')");
Mehdi Amini5d303282015-10-26 18:37:00 +0000542 }
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000543 return ::error(DiagnosticHandler,
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000544 make_error_code(BitcodeError::CorruptedBitcode), Message);
545}
546
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000547std::error_code BitcodeReader::error(BitcodeError E) {
548 return ::error(DiagnosticHandler, make_error_code(E));
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000549}
550
551static DiagnosticHandlerFunction getDiagHandler(DiagnosticHandlerFunction F,
552 LLVMContext &C) {
553 if (F)
554 return F;
555 return [&C](const DiagnosticInfo &DI) { C.diagnose(DI); };
556}
557
Rafael Espindola4223a1f2015-06-15 20:08:17 +0000558BitcodeReader::BitcodeReader(MemoryBuffer *Buffer, LLVMContext &Context,
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000559 DiagnosticHandlerFunction DiagnosticHandler)
Rafael Espindola4223a1f2015-06-15 20:08:17 +0000560 : Context(Context),
561 DiagnosticHandler(getDiagHandler(DiagnosticHandler, Context)),
Rafael Espindola1c863ca2015-06-22 18:06:15 +0000562 Buffer(Buffer), ValueList(Context), MDValueList(Context) {}
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000563
Rafael Espindola1aabf982015-06-16 23:29:49 +0000564BitcodeReader::BitcodeReader(LLVMContext &Context,
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000565 DiagnosticHandlerFunction DiagnosticHandler)
Rafael Espindola4223a1f2015-06-15 20:08:17 +0000566 : Context(Context),
567 DiagnosticHandler(getDiagHandler(DiagnosticHandler, Context)),
Rafael Espindola1c863ca2015-06-22 18:06:15 +0000568 Buffer(nullptr), ValueList(Context), MDValueList(Context) {}
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000569
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +0000570std::error_code BitcodeReader::materializeForwardReferencedFunctions() {
571 if (WillMaterializeAllForwardRefs)
572 return std::error_code();
573
574 // Prevent recursion.
575 WillMaterializeAllForwardRefs = true;
576
Duncan P. N. Exon Smith5a511b52014-08-05 17:49:48 +0000577 while (!BasicBlockFwdRefQueue.empty()) {
578 Function *F = BasicBlockFwdRefQueue.front();
579 BasicBlockFwdRefQueue.pop_front();
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +0000580 assert(F && "Expected valid function");
Duncan P. N. Exon Smith5a511b52014-08-05 17:49:48 +0000581 if (!BasicBlockFwdRefs.count(F))
582 // Already materialized.
583 continue;
584
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +0000585 // Check for a function that isn't materializable to prevent an infinite
586 // loop. When parsing a blockaddress stored in a global variable, there
587 // isn't a trivial way to check if a function will have a body without a
588 // linear search through FunctionsWithBodies, so just check it here.
589 if (!F->isMaterializable())
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000590 return error("Never resolved function from blockaddress");
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +0000591
592 // Try to materialize F.
Rafael Espindola5a52e6d2014-10-24 22:50:48 +0000593 if (std::error_code EC = materialize(F))
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +0000594 return EC;
Rafael Espindolab7993462012-01-02 07:49:53 +0000595 }
Duncan P. N. Exon Smith5a511b52014-08-05 17:49:48 +0000596 assert(BasicBlockFwdRefs.empty() && "Function missing from queue");
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +0000597
598 // Reset state.
599 WillMaterializeAllForwardRefs = false;
600 return std::error_code();
Rafael Espindolab7993462012-01-02 07:49:53 +0000601}
602
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000603void BitcodeReader::freeState() {
Craig Topper2617dcc2014-04-15 06:32:26 +0000604 Buffer = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000605 std::vector<Type*>().swap(TypeList);
Chris Lattner9eeada92007-05-18 04:02:46 +0000606 ValueList.clear();
Devang Patel05eb6172009-08-04 06:00:18 +0000607 MDValueList.clear();
David Majnemerdad0a642014-06-27 18:19:56 +0000608 std::vector<Comdat *>().swap(ComdatList);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000609
Bill Wendlinge94d8432012-12-07 23:16:57 +0000610 std::vector<AttributeSet>().swap(MAttributes);
Chris Lattner9eeada92007-05-18 04:02:46 +0000611 std::vector<BasicBlock*>().swap(FunctionBBs);
612 std::vector<Function*>().swap(FunctionsWithBodies);
613 DeferredFunctionInfo.clear();
Manman Ren4a9b0eb2015-03-13 19:24:30 +0000614 DeferredMetadataInfo.clear();
Dan Gohman43aa8f02010-07-20 21:42:28 +0000615 MDKindMap.clear();
Benjamin Kramer736a4fc2012-09-21 14:34:31 +0000616
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +0000617 assert(BasicBlockFwdRefs.empty() && "Unresolved blockaddress fwd references");
Duncan P. N. Exon Smith5a511b52014-08-05 17:49:48 +0000618 BasicBlockFwdRefQueue.clear();
Chris Lattner6694f602007-04-29 07:54:31 +0000619}
620
Chris Lattnerfee5a372007-05-04 03:30:17 +0000621//===----------------------------------------------------------------------===//
622// Helper functions to implement forward reference resolution, etc.
623//===----------------------------------------------------------------------===//
Chris Lattner6694f602007-04-29 07:54:31 +0000624
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000625/// Convert a string from a record into an std::string, return true on failure.
626template <typename StrTy>
627static bool convertToString(ArrayRef<uint64_t> Record, unsigned Idx,
Chris Lattnerccaa4482007-04-23 21:26:05 +0000628 StrTy &Result) {
Chris Lattnere14cb882007-05-04 19:11:41 +0000629 if (Idx > Record.size())
Chris Lattner1314b992007-04-22 06:23:29 +0000630 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000631
Chris Lattnere14cb882007-05-04 19:11:41 +0000632 for (unsigned i = Idx, e = Record.size(); i != e; ++i)
633 Result += (char)Record[i];
Chris Lattner1314b992007-04-22 06:23:29 +0000634 return false;
635}
636
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000637static bool hasImplicitComdat(size_t Val) {
638 switch (Val) {
639 default:
640 return false;
641 case 1: // Old WeakAnyLinkage
642 case 4: // Old LinkOnceAnyLinkage
643 case 10: // Old WeakODRLinkage
644 case 11: // Old LinkOnceODRLinkage
645 return true;
646 }
647}
648
Rafael Espindola7b4b2dc2015-01-08 15:36:32 +0000649static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) {
Chris Lattner1314b992007-04-22 06:23:29 +0000650 switch (Val) {
651 default: // Map unknown/new linkages to external
Rafael Espindola7b4b2dc2015-01-08 15:36:32 +0000652 case 0:
653 return GlobalValue::ExternalLinkage;
Rafael Espindola7b4b2dc2015-01-08 15:36:32 +0000654 case 2:
655 return GlobalValue::AppendingLinkage;
656 case 3:
657 return GlobalValue::InternalLinkage;
Rafael Espindola7b4b2dc2015-01-08 15:36:32 +0000658 case 5:
659 return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage
660 case 6:
661 return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage
662 case 7:
663 return GlobalValue::ExternalWeakLinkage;
664 case 8:
665 return GlobalValue::CommonLinkage;
666 case 9:
667 return GlobalValue::PrivateLinkage;
Rafael Espindola7b4b2dc2015-01-08 15:36:32 +0000668 case 12:
669 return GlobalValue::AvailableExternallyLinkage;
Rafael Espindola2fb5bc32014-03-13 23:18:37 +0000670 case 13:
671 return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage
672 case 14:
673 return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage
Rafael Espindolabec6af62015-01-08 15:39:50 +0000674 case 15:
675 return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000676 case 1: // Old value with implicit comdat.
677 case 16:
678 return GlobalValue::WeakAnyLinkage;
679 case 10: // Old value with implicit comdat.
680 case 17:
681 return GlobalValue::WeakODRLinkage;
682 case 4: // Old value with implicit comdat.
683 case 18:
684 return GlobalValue::LinkOnceAnyLinkage;
685 case 11: // Old value with implicit comdat.
686 case 19:
687 return GlobalValue::LinkOnceODRLinkage;
Chris Lattner1314b992007-04-22 06:23:29 +0000688 }
689}
690
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000691static GlobalValue::VisibilityTypes getDecodedVisibility(unsigned Val) {
Chris Lattner1314b992007-04-22 06:23:29 +0000692 switch (Val) {
693 default: // Map unknown visibilities to default.
694 case 0: return GlobalValue::DefaultVisibility;
695 case 1: return GlobalValue::HiddenVisibility;
Anton Korobeynikov31fc4f92007-04-29 20:56:48 +0000696 case 2: return GlobalValue::ProtectedVisibility;
Chris Lattner1314b992007-04-22 06:23:29 +0000697 }
698}
699
Nico Rieck7157bb72014-01-14 15:22:47 +0000700static GlobalValue::DLLStorageClassTypes
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000701getDecodedDLLStorageClass(unsigned Val) {
Nico Rieck7157bb72014-01-14 15:22:47 +0000702 switch (Val) {
703 default: // Map unknown values to default.
704 case 0: return GlobalValue::DefaultStorageClass;
705 case 1: return GlobalValue::DLLImportStorageClass;
706 case 2: return GlobalValue::DLLExportStorageClass;
707 }
708}
709
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000710static GlobalVariable::ThreadLocalMode getDecodedThreadLocalMode(unsigned Val) {
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000711 switch (Val) {
712 case 0: return GlobalVariable::NotThreadLocal;
713 default: // Map unknown non-zero value to general dynamic.
714 case 1: return GlobalVariable::GeneralDynamicTLSModel;
715 case 2: return GlobalVariable::LocalDynamicTLSModel;
716 case 3: return GlobalVariable::InitialExecTLSModel;
717 case 4: return GlobalVariable::LocalExecTLSModel;
718 }
719}
720
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000721static int getDecodedCastOpcode(unsigned Val) {
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000722 switch (Val) {
723 default: return -1;
724 case bitc::CAST_TRUNC : return Instruction::Trunc;
725 case bitc::CAST_ZEXT : return Instruction::ZExt;
726 case bitc::CAST_SEXT : return Instruction::SExt;
727 case bitc::CAST_FPTOUI : return Instruction::FPToUI;
728 case bitc::CAST_FPTOSI : return Instruction::FPToSI;
729 case bitc::CAST_UITOFP : return Instruction::UIToFP;
730 case bitc::CAST_SITOFP : return Instruction::SIToFP;
731 case bitc::CAST_FPTRUNC : return Instruction::FPTrunc;
732 case bitc::CAST_FPEXT : return Instruction::FPExt;
733 case bitc::CAST_PTRTOINT: return Instruction::PtrToInt;
734 case bitc::CAST_INTTOPTR: return Instruction::IntToPtr;
735 case bitc::CAST_BITCAST : return Instruction::BitCast;
Matt Arsenault3aa9b032013-11-18 02:51:33 +0000736 case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000737 }
738}
Filipe Cabecinhasea79c5b2015-04-22 09:06:21 +0000739
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000740static int getDecodedBinaryOpcode(unsigned Val, Type *Ty) {
Filipe Cabecinhasea79c5b2015-04-22 09:06:21 +0000741 bool IsFP = Ty->isFPOrFPVectorTy();
742 // BinOps are only valid for int/fp or vector of int/fp types
743 if (!IsFP && !Ty->isIntOrIntVectorTy())
744 return -1;
745
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000746 switch (Val) {
Filipe Cabecinhasea79c5b2015-04-22 09:06:21 +0000747 default:
748 return -1;
Dan Gohmana5b96452009-06-04 22:49:04 +0000749 case bitc::BINOP_ADD:
Filipe Cabecinhasea79c5b2015-04-22 09:06:21 +0000750 return IsFP ? Instruction::FAdd : Instruction::Add;
Dan Gohmana5b96452009-06-04 22:49:04 +0000751 case bitc::BINOP_SUB:
Filipe Cabecinhasea79c5b2015-04-22 09:06:21 +0000752 return IsFP ? Instruction::FSub : Instruction::Sub;
Dan Gohmana5b96452009-06-04 22:49:04 +0000753 case bitc::BINOP_MUL:
Filipe Cabecinhasea79c5b2015-04-22 09:06:21 +0000754 return IsFP ? Instruction::FMul : Instruction::Mul;
755 case bitc::BINOP_UDIV:
756 return IsFP ? -1 : Instruction::UDiv;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000757 case bitc::BINOP_SDIV:
Filipe Cabecinhasea79c5b2015-04-22 09:06:21 +0000758 return IsFP ? Instruction::FDiv : Instruction::SDiv;
759 case bitc::BINOP_UREM:
760 return IsFP ? -1 : Instruction::URem;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000761 case bitc::BINOP_SREM:
Filipe Cabecinhasea79c5b2015-04-22 09:06:21 +0000762 return IsFP ? Instruction::FRem : Instruction::SRem;
763 case bitc::BINOP_SHL:
764 return IsFP ? -1 : Instruction::Shl;
765 case bitc::BINOP_LSHR:
766 return IsFP ? -1 : Instruction::LShr;
767 case bitc::BINOP_ASHR:
768 return IsFP ? -1 : Instruction::AShr;
769 case bitc::BINOP_AND:
770 return IsFP ? -1 : Instruction::And;
771 case bitc::BINOP_OR:
772 return IsFP ? -1 : Instruction::Or;
773 case bitc::BINOP_XOR:
774 return IsFP ? -1 : Instruction::Xor;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000775 }
776}
777
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000778static AtomicRMWInst::BinOp getDecodedRMWOperation(unsigned Val) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +0000779 switch (Val) {
780 default: return AtomicRMWInst::BAD_BINOP;
781 case bitc::RMW_XCHG: return AtomicRMWInst::Xchg;
782 case bitc::RMW_ADD: return AtomicRMWInst::Add;
783 case bitc::RMW_SUB: return AtomicRMWInst::Sub;
784 case bitc::RMW_AND: return AtomicRMWInst::And;
785 case bitc::RMW_NAND: return AtomicRMWInst::Nand;
786 case bitc::RMW_OR: return AtomicRMWInst::Or;
787 case bitc::RMW_XOR: return AtomicRMWInst::Xor;
788 case bitc::RMW_MAX: return AtomicRMWInst::Max;
789 case bitc::RMW_MIN: return AtomicRMWInst::Min;
790 case bitc::RMW_UMAX: return AtomicRMWInst::UMax;
791 case bitc::RMW_UMIN: return AtomicRMWInst::UMin;
792 }
793}
794
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000795static AtomicOrdering getDecodedOrdering(unsigned Val) {
Eli Friedmanfee02c62011-07-25 23:16:38 +0000796 switch (Val) {
797 case bitc::ORDERING_NOTATOMIC: return NotAtomic;
798 case bitc::ORDERING_UNORDERED: return Unordered;
799 case bitc::ORDERING_MONOTONIC: return Monotonic;
800 case bitc::ORDERING_ACQUIRE: return Acquire;
801 case bitc::ORDERING_RELEASE: return Release;
802 case bitc::ORDERING_ACQREL: return AcquireRelease;
803 default: // Map unknown orderings to sequentially-consistent.
804 case bitc::ORDERING_SEQCST: return SequentiallyConsistent;
805 }
806}
807
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000808static SynchronizationScope getDecodedSynchScope(unsigned Val) {
Eli Friedmanfee02c62011-07-25 23:16:38 +0000809 switch (Val) {
810 case bitc::SYNCHSCOPE_SINGLETHREAD: return SingleThread;
811 default: // Map unknown scopes to cross-thread.
812 case bitc::SYNCHSCOPE_CROSSTHREAD: return CrossThread;
813 }
814}
815
David Majnemerdad0a642014-06-27 18:19:56 +0000816static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) {
817 switch (Val) {
818 default: // Map unknown selection kinds to any.
819 case bitc::COMDAT_SELECTION_KIND_ANY:
820 return Comdat::Any;
821 case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH:
822 return Comdat::ExactMatch;
823 case bitc::COMDAT_SELECTION_KIND_LARGEST:
824 return Comdat::Largest;
825 case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES:
826 return Comdat::NoDuplicates;
827 case bitc::COMDAT_SELECTION_KIND_SAME_SIZE:
828 return Comdat::SameSize;
829 }
830}
831
James Molloy88eb5352015-07-10 12:52:00 +0000832static FastMathFlags getDecodedFastMathFlags(unsigned Val) {
833 FastMathFlags FMF;
834 if (0 != (Val & FastMathFlags::UnsafeAlgebra))
835 FMF.setUnsafeAlgebra();
836 if (0 != (Val & FastMathFlags::NoNaNs))
837 FMF.setNoNaNs();
838 if (0 != (Val & FastMathFlags::NoInfs))
839 FMF.setNoInfs();
840 if (0 != (Val & FastMathFlags::NoSignedZeros))
841 FMF.setNoSignedZeros();
842 if (0 != (Val & FastMathFlags::AllowReciprocal))
843 FMF.setAllowReciprocal();
844 return FMF;
845}
846
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000847static void upgradeDLLImportExportLinkage(llvm::GlobalValue *GV, unsigned Val) {
Nico Rieck7157bb72014-01-14 15:22:47 +0000848 switch (Val) {
849 case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break;
850 case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break;
851 }
852}
853
Gabor Greiff6caff662008-05-10 08:32:32 +0000854namespace llvm {
Chris Lattner1663cca2007-04-24 05:48:56 +0000855namespace {
Rafael Espindola64a27fb2015-06-15 21:04:27 +0000856/// \brief A class for maintaining the slot number definition
857/// as a placeholder for the actual definition for forward constants defs.
858class ConstantPlaceHolder : public ConstantExpr {
859 void operator=(const ConstantPlaceHolder &) = delete;
860
861public:
862 // allocate space for exactly one operand
863 void *operator new(size_t s) { return User::operator new(s, 1); }
864 explicit ConstantPlaceHolder(Type *Ty, LLVMContext &Context)
Gabor Greiff6caff662008-05-10 08:32:32 +0000865 : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) {
Rafael Espindola64a27fb2015-06-15 21:04:27 +0000866 Op<0>() = UndefValue::get(Type::getInt32Ty(Context));
867 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000868
Rafael Espindola64a27fb2015-06-15 21:04:27 +0000869 /// \brief Methods to support type inquiry through isa, cast, and dyn_cast.
870 static bool classof(const Value *V) {
871 return isa<ConstantExpr>(V) &&
872 cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1;
873 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000874
Rafael Espindola64a27fb2015-06-15 21:04:27 +0000875 /// Provide fast operand accessors
876 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
877};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000878}
Chris Lattner1663cca2007-04-24 05:48:56 +0000879
Chris Lattner2d8cd802009-03-31 22:55:09 +0000880// FIXME: can we inherit this from ConstantExpr?
Gabor Greiff6caff662008-05-10 08:32:32 +0000881template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000882struct OperandTraits<ConstantPlaceHolder> :
883 public FixedNumOperandTraits<ConstantPlaceHolder, 1> {
Gabor Greiff6caff662008-05-10 08:32:32 +0000884};
Richard Trieue3d126c2014-11-21 02:42:08 +0000885DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantPlaceHolder, Value)
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000886}
Gabor Greiff6caff662008-05-10 08:32:32 +0000887
David Majnemer8a1c45d2015-12-12 05:38:55 +0000888void BitcodeReaderValueList::assignValue(Value *V, unsigned Idx) {
Chris Lattner2d8cd802009-03-31 22:55:09 +0000889 if (Idx == size()) {
890 push_back(V);
David Majnemer8a1c45d2015-12-12 05:38:55 +0000891 return;
Chris Lattner2d8cd802009-03-31 22:55:09 +0000892 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000893
Chris Lattner2d8cd802009-03-31 22:55:09 +0000894 if (Idx >= size())
895 resize(Idx+1);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000896
Chris Lattner2d8cd802009-03-31 22:55:09 +0000897 WeakVH &OldV = ValuePtrs[Idx];
Craig Topper2617dcc2014-04-15 06:32:26 +0000898 if (!OldV) {
Chris Lattner2d8cd802009-03-31 22:55:09 +0000899 OldV = V;
David Majnemer8a1c45d2015-12-12 05:38:55 +0000900 return;
Chris Lattner2d8cd802009-03-31 22:55:09 +0000901 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000902
Chris Lattner2d8cd802009-03-31 22:55:09 +0000903 // Handle constants and non-constants (e.g. instrs) differently for
904 // efficiency.
905 if (Constant *PHC = dyn_cast<Constant>(&*OldV)) {
906 ResolveConstants.push_back(std::make_pair(PHC, Idx));
907 OldV = V;
908 } else {
909 // If there was a forward reference to this value, replace it.
910 Value *PrevVal = OldV;
911 OldV->replaceAllUsesWith(V);
912 delete PrevVal;
Gabor Greiff6caff662008-05-10 08:32:32 +0000913 }
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000914
David Majnemer8a1c45d2015-12-12 05:38:55 +0000915 return;
Gabor Greiff6caff662008-05-10 08:32:32 +0000916}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000917
Gabor Greiff6caff662008-05-10 08:32:32 +0000918
Chris Lattner1663cca2007-04-24 05:48:56 +0000919Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx,
Chris Lattner229907c2011-07-18 04:54:35 +0000920 Type *Ty) {
Chris Lattner2d8cd802009-03-31 22:55:09 +0000921 if (Idx >= size())
Gabor Greiff6caff662008-05-10 08:32:32 +0000922 resize(Idx + 1);
Chris Lattner1663cca2007-04-24 05:48:56 +0000923
Chris Lattner2d8cd802009-03-31 22:55:09 +0000924 if (Value *V = ValuePtrs[Idx]) {
Filipe Cabecinhas6a92a3f2015-05-27 01:05:40 +0000925 if (Ty != V->getType())
926 report_fatal_error("Type mismatch in constant table!");
Chris Lattner83930552007-05-01 07:01:57 +0000927 return cast<Constant>(V);
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000928 }
Chris Lattner1663cca2007-04-24 05:48:56 +0000929
930 // Create and return a placeholder, which will later be RAUW'd.
Owen Andersone9f98042009-07-07 20:18:58 +0000931 Constant *C = new ConstantPlaceHolder(Ty, Context);
Chris Lattner2d8cd802009-03-31 22:55:09 +0000932 ValuePtrs[Idx] = C;
Chris Lattner1663cca2007-04-24 05:48:56 +0000933 return C;
934}
935
David Majnemer8a1c45d2015-12-12 05:38:55 +0000936Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, Type *Ty) {
Filipe Cabecinhasbad07792015-04-30 00:52:42 +0000937 // Bail out for a clearly invalid value. This would make us call resize(0)
938 if (Idx == UINT_MAX)
939 return nullptr;
940
Chris Lattner2d8cd802009-03-31 22:55:09 +0000941 if (Idx >= size())
Gabor Greiff6caff662008-05-10 08:32:32 +0000942 resize(Idx + 1);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000943
Chris Lattner2d8cd802009-03-31 22:55:09 +0000944 if (Value *V = ValuePtrs[Idx]) {
Filipe Cabecinhasb435d0f2015-04-28 20:18:47 +0000945 // If the types don't match, it's invalid.
946 if (Ty && Ty != V->getType())
947 return nullptr;
David Majnemer8a1c45d2015-12-12 05:38:55 +0000948 return V;
Chris Lattner83930552007-05-01 07:01:57 +0000949 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000950
Chris Lattner1fc27f02007-05-02 05:16:49 +0000951 // No type specified, must be invalid reference.
Craig Topper2617dcc2014-04-15 06:32:26 +0000952 if (!Ty) return nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000953
Chris Lattner83930552007-05-01 07:01:57 +0000954 // Create and return a placeholder, which will later be RAUW'd.
David Majnemer8a1c45d2015-12-12 05:38:55 +0000955 Value *V = new Argument(Ty);
Chris Lattner2d8cd802009-03-31 22:55:09 +0000956 ValuePtrs[Idx] = V;
Chris Lattner83930552007-05-01 07:01:57 +0000957 return V;
958}
959
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000960/// Once all constants are read, this method bulk resolves any forward
961/// references. The idea behind this is that we sometimes get constants (such
962/// as large arrays) which reference *many* forward ref constants. Replacing
963/// each of these causes a lot of thrashing when building/reuniquing the
964/// constant. Instead of doing this, we look at all the uses and rewrite all
965/// the place holders at once for any constant that uses a placeholder.
966void BitcodeReaderValueList::resolveConstantForwardRefs() {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000967 // Sort the values by-pointer so that they are efficient to look up with a
Chris Lattner74429932008-08-21 02:34:16 +0000968 // binary search.
969 std::sort(ResolveConstants.begin(), ResolveConstants.end());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000970
Chris Lattner74429932008-08-21 02:34:16 +0000971 SmallVector<Constant*, 64> NewOps;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000972
Chris Lattner74429932008-08-21 02:34:16 +0000973 while (!ResolveConstants.empty()) {
Chris Lattner2d8cd802009-03-31 22:55:09 +0000974 Value *RealVal = operator[](ResolveConstants.back().second);
Chris Lattner74429932008-08-21 02:34:16 +0000975 Constant *Placeholder = ResolveConstants.back().first;
976 ResolveConstants.pop_back();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000977
Chris Lattner74429932008-08-21 02:34:16 +0000978 // Loop over all users of the placeholder, updating them to reference the
979 // new value. If they reference more than one placeholder, update them all
980 // at once.
981 while (!Placeholder->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000982 auto UI = Placeholder->user_begin();
Gabor Greif2c0ab482010-07-09 16:01:21 +0000983 User *U = *UI;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000984
Chris Lattner74429932008-08-21 02:34:16 +0000985 // If the using object isn't uniqued, just update the operands. This
986 // handles instructions and initializers for global variables.
Gabor Greif2c0ab482010-07-09 16:01:21 +0000987 if (!isa<Constant>(U) || isa<GlobalValue>(U)) {
Chris Lattner479c5d92008-08-21 17:31:45 +0000988 UI.getUse().set(RealVal);
Chris Lattner74429932008-08-21 02:34:16 +0000989 continue;
990 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000991
Chris Lattner74429932008-08-21 02:34:16 +0000992 // Otherwise, we have a constant that uses the placeholder. Replace that
993 // constant with a new constant that has *all* placeholder uses updated.
Gabor Greif2c0ab482010-07-09 16:01:21 +0000994 Constant *UserC = cast<Constant>(U);
Chris Lattner74429932008-08-21 02:34:16 +0000995 for (User::op_iterator I = UserC->op_begin(), E = UserC->op_end();
996 I != E; ++I) {
997 Value *NewOp;
998 if (!isa<ConstantPlaceHolder>(*I)) {
999 // Not a placeholder reference.
1000 NewOp = *I;
1001 } else if (*I == Placeholder) {
1002 // Common case is that it just references this one placeholder.
1003 NewOp = RealVal;
1004 } else {
1005 // Otherwise, look up the placeholder in ResolveConstants.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001006 ResolveConstantsTy::iterator It =
1007 std::lower_bound(ResolveConstants.begin(), ResolveConstants.end(),
Chris Lattner74429932008-08-21 02:34:16 +00001008 std::pair<Constant*, unsigned>(cast<Constant>(*I),
1009 0));
1010 assert(It != ResolveConstants.end() && It->first == *I);
Chris Lattner2d8cd802009-03-31 22:55:09 +00001011 NewOp = operator[](It->second);
Chris Lattner74429932008-08-21 02:34:16 +00001012 }
1013
1014 NewOps.push_back(cast<Constant>(NewOp));
1015 }
1016
1017 // Make the new constant.
1018 Constant *NewC;
1019 if (ConstantArray *UserCA = dyn_cast<ConstantArray>(UserC)) {
Jay Foad83be3612011-06-22 09:24:39 +00001020 NewC = ConstantArray::get(UserCA->getType(), NewOps);
Chris Lattner74429932008-08-21 02:34:16 +00001021 } else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) {
Chris Lattnercc19efa2011-06-20 04:01:31 +00001022 NewC = ConstantStruct::get(UserCS->getType(), NewOps);
Chris Lattner74429932008-08-21 02:34:16 +00001023 } else if (isa<ConstantVector>(UserC)) {
Chris Lattner69229312011-02-15 00:14:00 +00001024 NewC = ConstantVector::get(NewOps);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001025 } else {
1026 assert(isa<ConstantExpr>(UserC) && "Must be a ConstantExpr.");
Jay Foad5c984e562011-04-13 13:46:01 +00001027 NewC = cast<ConstantExpr>(UserC)->getWithOperands(NewOps);
Chris Lattner74429932008-08-21 02:34:16 +00001028 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001029
Chris Lattner74429932008-08-21 02:34:16 +00001030 UserC->replaceAllUsesWith(NewC);
1031 UserC->destroyConstant();
1032 NewOps.clear();
1033 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001034
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001035 // Update all ValueHandles, they should be the only users at this point.
1036 Placeholder->replaceAllUsesWith(RealVal);
Chris Lattner74429932008-08-21 02:34:16 +00001037 delete Placeholder;
1038 }
1039}
1040
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001041void BitcodeReaderMDValueList::assignValue(Metadata *MD, unsigned Idx) {
Devang Patel05eb6172009-08-04 06:00:18 +00001042 if (Idx == size()) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001043 push_back(MD);
Devang Patel05eb6172009-08-04 06:00:18 +00001044 return;
1045 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001046
Devang Patel05eb6172009-08-04 06:00:18 +00001047 if (Idx >= size())
1048 resize(Idx+1);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001049
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001050 TrackingMDRef &OldMD = MDValuePtrs[Idx];
1051 if (!OldMD) {
1052 OldMD.reset(MD);
Devang Patel05eb6172009-08-04 06:00:18 +00001053 return;
1054 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001055
Devang Patel05eb6172009-08-04 06:00:18 +00001056 // If there was a forward reference to this value, replace it.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00001057 TempMDTuple PrevMD(cast<MDTuple>(OldMD.get()));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001058 PrevMD->replaceAllUsesWith(MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001059 --NumFwdRefs;
Devang Patel05eb6172009-08-04 06:00:18 +00001060}
1061
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001062Metadata *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) {
Devang Patel05eb6172009-08-04 06:00:18 +00001063 if (Idx >= size())
1064 resize(Idx + 1);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001065
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001066 if (Metadata *MD = MDValuePtrs[Idx])
1067 return MD;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001068
Duncan P. N. Exon Smith060ee622015-02-16 19:18:01 +00001069 // Track forward refs to be resolved later.
1070 if (AnyFwdRefs) {
1071 MinFwdRef = std::min(MinFwdRef, Idx);
1072 MaxFwdRef = std::max(MaxFwdRef, Idx);
1073 } else {
1074 AnyFwdRefs = true;
1075 MinFwdRef = MaxFwdRef = Idx;
1076 }
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001077 ++NumFwdRefs;
Duncan P. N. Exon Smith060ee622015-02-16 19:18:01 +00001078
1079 // Create and return a placeholder, which will later be RAUW'd.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00001080 Metadata *MD = MDNode::getTemporary(Context, None).release();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001081 MDValuePtrs[Idx].reset(MD);
1082 return MD;
1083}
1084
1085void BitcodeReaderMDValueList::tryToResolveCycles() {
1086 if (!AnyFwdRefs)
1087 // Nothing to do.
1088 return;
1089
1090 if (NumFwdRefs)
1091 // Still forward references... can't resolve cycles.
1092 return;
1093
1094 // Resolve any cycles.
Duncan P. N. Exon Smith060ee622015-02-16 19:18:01 +00001095 for (unsigned I = MinFwdRef, E = MaxFwdRef + 1; I != E; ++I) {
1096 auto &MD = MDValuePtrs[I];
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +00001097 auto *N = dyn_cast_or_null<MDNode>(MD);
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +00001098 if (!N)
1099 continue;
1100
1101 assert(!N->isTemporary() && "Unexpected forward reference");
1102 N->resolveCycles();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001103 }
Duncan P. N. Exon Smith060ee622015-02-16 19:18:01 +00001104
1105 // Make sure we return early again until there's another forward ref.
1106 AnyFwdRefs = false;
Devang Patel05eb6172009-08-04 06:00:18 +00001107}
Chris Lattner1314b992007-04-22 06:23:29 +00001108
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001109Type *BitcodeReader::getTypeByID(unsigned ID) {
1110 // The type table size is always specified correctly.
1111 if (ID >= TypeList.size())
Craig Topper2617dcc2014-04-15 06:32:26 +00001112 return nullptr;
Derek Schuff206dddd2012-02-06 19:03:04 +00001113
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001114 if (Type *Ty = TypeList[ID])
1115 return Ty;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001116
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001117 // If we have a forward reference, the only possible case is when it is to a
1118 // named struct. Just create a placeholder for now.
Rafael Espindola2fa1e432014-12-03 07:18:23 +00001119 return TypeList[ID] = createIdentifiedStructType(Context);
1120}
1121
1122StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context,
1123 StringRef Name) {
1124 auto *Ret = StructType::create(Context, Name);
1125 IdentifiedStructTypes.push_back(Ret);
1126 return Ret;
1127}
1128
1129StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) {
1130 auto *Ret = StructType::create(Context);
1131 IdentifiedStructTypes.push_back(Ret);
1132 return Ret;
Chris Lattner1314b992007-04-22 06:23:29 +00001133}
1134
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001135
Chris Lattnerfee5a372007-05-04 03:30:17 +00001136//===----------------------------------------------------------------------===//
1137// Functions for parsing blocks from the bitcode file
1138//===----------------------------------------------------------------------===//
1139
Bill Wendling56aeccc2013-02-04 23:32:23 +00001140
1141/// \brief This fills an AttrBuilder object with the LLVM attributes that have
1142/// been decoded from the given integer. This function must stay in sync with
1143/// 'encodeLLVMAttributesForBitcode'.
1144static void decodeLLVMAttributesForBitcode(AttrBuilder &B,
1145 uint64_t EncodedAttrs) {
1146 // FIXME: Remove in 4.0.
1147
1148 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
1149 // the bits above 31 down by 11 bits.
1150 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
1151 assert((!Alignment || isPowerOf2_32(Alignment)) &&
1152 "Alignment must be a power of two.");
1153
1154 if (Alignment)
1155 B.addAlignmentAttr(Alignment);
Kostya Serebryanyd688bab2013-02-11 08:13:54 +00001156 B.addRawValue(((EncodedAttrs & (0xfffffULL << 32)) >> 11) |
Bill Wendling56aeccc2013-02-04 23:32:23 +00001157 (EncodedAttrs & 0xffff));
1158}
1159
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001160std::error_code BitcodeReader::parseAttributeBlock() {
Chris Lattner982ec1e2007-05-05 00:17:00 +00001161 if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001162 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001163
Devang Patela05633e2008-09-26 22:53:05 +00001164 if (!MAttributes.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001165 return error("Invalid multiple blocks");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001166
Chris Lattnerfee5a372007-05-04 03:30:17 +00001167 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001168
Bill Wendling71173cb2013-01-27 00:36:48 +00001169 SmallVector<AttributeSet, 8> Attrs;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001170
Chris Lattnerfee5a372007-05-04 03:30:17 +00001171 // Read all the records.
1172 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00001173 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00001174
Chris Lattner27d38752013-01-20 02:13:19 +00001175 switch (Entry.Kind) {
1176 case BitstreamEntry::SubBlock: // Handled for us already.
1177 case BitstreamEntry::Error:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001178 return error("Malformed block");
Chris Lattner27d38752013-01-20 02:13:19 +00001179 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001180 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00001181 case BitstreamEntry::Record:
1182 // The interesting case.
1183 break;
Chris Lattnerfee5a372007-05-04 03:30:17 +00001184 }
Joe Abbey97b7a172013-02-06 22:14:06 +00001185
Chris Lattnerfee5a372007-05-04 03:30:17 +00001186 // Read a record.
1187 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +00001188 switch (Stream.readRecord(Entry.ID, Record)) {
Chris Lattnerfee5a372007-05-04 03:30:17 +00001189 default: // Default behavior: ignore.
1190 break;
Bill Wendling56aeccc2013-02-04 23:32:23 +00001191 case bitc::PARAMATTR_CODE_ENTRY_OLD: { // ENTRY: [paramidx0, attr0, ...]
1192 // FIXME: Remove in 4.0.
Chris Lattnerfee5a372007-05-04 03:30:17 +00001193 if (Record.size() & 1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001194 return error("Invalid record");
Chris Lattnerfee5a372007-05-04 03:30:17 +00001195
Chris Lattnerfee5a372007-05-04 03:30:17 +00001196 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
Bill Wendling60011b82013-01-29 01:43:29 +00001197 AttrBuilder B;
Bill Wendling56aeccc2013-02-04 23:32:23 +00001198 decodeLLVMAttributesForBitcode(B, Record[i+1]);
Bill Wendling60011b82013-01-29 01:43:29 +00001199 Attrs.push_back(AttributeSet::get(Context, Record[i], B));
Devang Patela05633e2008-09-26 22:53:05 +00001200 }
Devang Patela05633e2008-09-26 22:53:05 +00001201
Bill Wendlinge94d8432012-12-07 23:16:57 +00001202 MAttributes.push_back(AttributeSet::get(Context, Attrs));
Chris Lattnerfee5a372007-05-04 03:30:17 +00001203 Attrs.clear();
1204 break;
1205 }
Bill Wendling0dc08912013-02-12 08:13:50 +00001206 case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [attrgrp0, attrgrp1, ...]
1207 for (unsigned i = 0, e = Record.size(); i != e; ++i)
1208 Attrs.push_back(MAttributeGroups[Record[i]]);
1209
1210 MAttributes.push_back(AttributeSet::get(Context, Attrs));
1211 Attrs.clear();
1212 break;
1213 }
Duncan Sands04eb67e2007-11-20 14:09:29 +00001214 }
Chris Lattnerfee5a372007-05-04 03:30:17 +00001215 }
1216}
1217
Reid Klecknere9f36af2013-11-12 01:31:00 +00001218// Returns Attribute::None on unrecognized codes.
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001219static Attribute::AttrKind getAttrFromCode(uint64_t Code) {
Reid Klecknere9f36af2013-11-12 01:31:00 +00001220 switch (Code) {
1221 default:
1222 return Attribute::None;
1223 case bitc::ATTR_KIND_ALIGNMENT:
1224 return Attribute::Alignment;
1225 case bitc::ATTR_KIND_ALWAYS_INLINE:
1226 return Attribute::AlwaysInline;
Igor Laevsky39d662f2015-07-11 10:30:36 +00001227 case bitc::ATTR_KIND_ARGMEMONLY:
1228 return Attribute::ArgMemOnly;
Reid Klecknere9f36af2013-11-12 01:31:00 +00001229 case bitc::ATTR_KIND_BUILTIN:
1230 return Attribute::Builtin;
1231 case bitc::ATTR_KIND_BY_VAL:
1232 return Attribute::ByVal;
Reid Klecknera534a382013-12-19 02:14:12 +00001233 case bitc::ATTR_KIND_IN_ALLOCA:
1234 return Attribute::InAlloca;
Reid Klecknere9f36af2013-11-12 01:31:00 +00001235 case bitc::ATTR_KIND_COLD:
1236 return Attribute::Cold;
Owen Anderson85fa7d52015-05-26 23:48:40 +00001237 case bitc::ATTR_KIND_CONVERGENT:
1238 return Attribute::Convergent;
Reid Klecknere9f36af2013-11-12 01:31:00 +00001239 case bitc::ATTR_KIND_INLINE_HINT:
1240 return Attribute::InlineHint;
1241 case bitc::ATTR_KIND_IN_REG:
1242 return Attribute::InReg;
Tom Roeder44cb65f2014-06-05 19:29:43 +00001243 case bitc::ATTR_KIND_JUMP_TABLE:
1244 return Attribute::JumpTable;
Reid Klecknere9f36af2013-11-12 01:31:00 +00001245 case bitc::ATTR_KIND_MIN_SIZE:
1246 return Attribute::MinSize;
1247 case bitc::ATTR_KIND_NAKED:
1248 return Attribute::Naked;
1249 case bitc::ATTR_KIND_NEST:
1250 return Attribute::Nest;
1251 case bitc::ATTR_KIND_NO_ALIAS:
1252 return Attribute::NoAlias;
1253 case bitc::ATTR_KIND_NO_BUILTIN:
1254 return Attribute::NoBuiltin;
1255 case bitc::ATTR_KIND_NO_CAPTURE:
1256 return Attribute::NoCapture;
1257 case bitc::ATTR_KIND_NO_DUPLICATE:
1258 return Attribute::NoDuplicate;
1259 case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT:
1260 return Attribute::NoImplicitFloat;
1261 case bitc::ATTR_KIND_NO_INLINE:
1262 return Attribute::NoInline;
James Molloye6f87ca2015-11-06 10:32:53 +00001263 case bitc::ATTR_KIND_NO_RECURSE:
1264 return Attribute::NoRecurse;
Reid Klecknere9f36af2013-11-12 01:31:00 +00001265 case bitc::ATTR_KIND_NON_LAZY_BIND:
1266 return Attribute::NonLazyBind;
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001267 case bitc::ATTR_KIND_NON_NULL:
1268 return Attribute::NonNull;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001269 case bitc::ATTR_KIND_DEREFERENCEABLE:
1270 return Attribute::Dereferenceable;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001271 case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL:
1272 return Attribute::DereferenceableOrNull;
Reid Klecknere9f36af2013-11-12 01:31:00 +00001273 case bitc::ATTR_KIND_NO_RED_ZONE:
1274 return Attribute::NoRedZone;
1275 case bitc::ATTR_KIND_NO_RETURN:
1276 return Attribute::NoReturn;
1277 case bitc::ATTR_KIND_NO_UNWIND:
1278 return Attribute::NoUnwind;
1279 case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE:
1280 return Attribute::OptimizeForSize;
1281 case bitc::ATTR_KIND_OPTIMIZE_NONE:
1282 return Attribute::OptimizeNone;
1283 case bitc::ATTR_KIND_READ_NONE:
1284 return Attribute::ReadNone;
1285 case bitc::ATTR_KIND_READ_ONLY:
1286 return Attribute::ReadOnly;
1287 case bitc::ATTR_KIND_RETURNED:
1288 return Attribute::Returned;
1289 case bitc::ATTR_KIND_RETURNS_TWICE:
1290 return Attribute::ReturnsTwice;
1291 case bitc::ATTR_KIND_S_EXT:
1292 return Attribute::SExt;
1293 case bitc::ATTR_KIND_STACK_ALIGNMENT:
1294 return Attribute::StackAlignment;
1295 case bitc::ATTR_KIND_STACK_PROTECT:
1296 return Attribute::StackProtect;
1297 case bitc::ATTR_KIND_STACK_PROTECT_REQ:
1298 return Attribute::StackProtectReq;
1299 case bitc::ATTR_KIND_STACK_PROTECT_STRONG:
1300 return Attribute::StackProtectStrong;
Peter Collingbourne82437bf2015-06-15 21:07:11 +00001301 case bitc::ATTR_KIND_SAFESTACK:
1302 return Attribute::SafeStack;
Reid Klecknere9f36af2013-11-12 01:31:00 +00001303 case bitc::ATTR_KIND_STRUCT_RET:
1304 return Attribute::StructRet;
1305 case bitc::ATTR_KIND_SANITIZE_ADDRESS:
1306 return Attribute::SanitizeAddress;
1307 case bitc::ATTR_KIND_SANITIZE_THREAD:
1308 return Attribute::SanitizeThread;
1309 case bitc::ATTR_KIND_SANITIZE_MEMORY:
1310 return Attribute::SanitizeMemory;
1311 case bitc::ATTR_KIND_UW_TABLE:
1312 return Attribute::UWTable;
1313 case bitc::ATTR_KIND_Z_EXT:
1314 return Attribute::ZExt;
1315 }
1316}
1317
JF Bastien30bf96b2015-02-22 19:32:03 +00001318std::error_code BitcodeReader::parseAlignmentValue(uint64_t Exponent,
1319 unsigned &Alignment) {
1320 // Note: Alignment in bitcode files is incremented by 1, so that zero
1321 // can be used for default alignment.
1322 if (Exponent > Value::MaxAlignmentExponent + 1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001323 return error("Invalid alignment value");
JF Bastien30bf96b2015-02-22 19:32:03 +00001324 Alignment = (1 << static_cast<unsigned>(Exponent)) >> 1;
1325 return std::error_code();
1326}
1327
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001328std::error_code BitcodeReader::parseAttrKind(uint64_t Code,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001329 Attribute::AttrKind *Kind) {
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001330 *Kind = getAttrFromCode(Code);
Reid Klecknere9f36af2013-11-12 01:31:00 +00001331 if (*Kind == Attribute::None)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001332 return error(BitcodeError::CorruptedBitcode,
Rafael Espindolad0b23be2015-01-10 00:07:30 +00001333 "Unknown attribute kind (" + Twine(Code) + ")");
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001334 return std::error_code();
Tobias Grosser0a8e12f2013-07-26 04:16:55 +00001335}
1336
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001337std::error_code BitcodeReader::parseAttributeGroupBlock() {
Bill Wendlingba629332013-02-10 23:24:25 +00001338 if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001339 return error("Invalid record");
Bill Wendlingba629332013-02-10 23:24:25 +00001340
1341 if (!MAttributeGroups.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001342 return error("Invalid multiple blocks");
Bill Wendlingba629332013-02-10 23:24:25 +00001343
1344 SmallVector<uint64_t, 64> Record;
1345
1346 // Read all the records.
1347 while (1) {
1348 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1349
1350 switch (Entry.Kind) {
1351 case BitstreamEntry::SubBlock: // Handled for us already.
1352 case BitstreamEntry::Error:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001353 return error("Malformed block");
Bill Wendlingba629332013-02-10 23:24:25 +00001354 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001355 return std::error_code();
Bill Wendlingba629332013-02-10 23:24:25 +00001356 case BitstreamEntry::Record:
1357 // The interesting case.
1358 break;
1359 }
1360
1361 // Read a record.
1362 Record.clear();
1363 switch (Stream.readRecord(Entry.ID, Record)) {
1364 default: // Default behavior: ignore.
1365 break;
1366 case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...]
1367 if (Record.size() < 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001368 return error("Invalid record");
Bill Wendlingba629332013-02-10 23:24:25 +00001369
Bill Wendlinge46707e2013-02-11 22:32:29 +00001370 uint64_t GrpID = Record[0];
Bill Wendlingba629332013-02-10 23:24:25 +00001371 uint64_t Idx = Record[1]; // Index of the object this attribute refers to.
1372
1373 AttrBuilder B;
1374 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
1375 if (Record[i] == 0) { // Enum attribute
Tobias Grosser0a8e12f2013-07-26 04:16:55 +00001376 Attribute::AttrKind Kind;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001377 if (std::error_code EC = parseAttrKind(Record[++i], &Kind))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001378 return EC;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +00001379
1380 B.addAttribute(Kind);
Hal Finkele15442c2014-07-18 06:51:55 +00001381 } else if (Record[i] == 1) { // Integer attribute
Tobias Grosser0a8e12f2013-07-26 04:16:55 +00001382 Attribute::AttrKind Kind;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001383 if (std::error_code EC = parseAttrKind(Record[++i], &Kind))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001384 return EC;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +00001385 if (Kind == Attribute::Alignment)
Bill Wendlingba629332013-02-10 23:24:25 +00001386 B.addAlignmentAttr(Record[++i]);
Hal Finkelb0407ba2014-07-18 15:51:28 +00001387 else if (Kind == Attribute::StackAlignment)
Bill Wendlingba629332013-02-10 23:24:25 +00001388 B.addStackAlignmentAttr(Record[++i]);
Hal Finkelb0407ba2014-07-18 15:51:28 +00001389 else if (Kind == Attribute::Dereferenceable)
1390 B.addDereferenceableAttr(Record[++i]);
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001391 else if (Kind == Attribute::DereferenceableOrNull)
1392 B.addDereferenceableOrNullAttr(Record[++i]);
Bill Wendlingba629332013-02-10 23:24:25 +00001393 } else { // String attribute
Bill Wendlinge46707e2013-02-11 22:32:29 +00001394 assert((Record[i] == 3 || Record[i] == 4) &&
1395 "Invalid attribute group entry");
Bill Wendlingba629332013-02-10 23:24:25 +00001396 bool HasValue = (Record[i++] == 4);
1397 SmallString<64> KindStr;
1398 SmallString<64> ValStr;
1399
1400 while (Record[i] != 0 && i != e)
1401 KindStr += Record[i++];
Bill Wendlinge46707e2013-02-11 22:32:29 +00001402 assert(Record[i] == 0 && "Kind string not null terminated");
Bill Wendlingba629332013-02-10 23:24:25 +00001403
1404 if (HasValue) {
1405 // Has a value associated with it.
Bill Wendlinge46707e2013-02-11 22:32:29 +00001406 ++i; // Skip the '0' that terminates the "kind" string.
Bill Wendlingba629332013-02-10 23:24:25 +00001407 while (Record[i] != 0 && i != e)
1408 ValStr += Record[i++];
Bill Wendlinge46707e2013-02-11 22:32:29 +00001409 assert(Record[i] == 0 && "Value string not null terminated");
Bill Wendlingba629332013-02-10 23:24:25 +00001410 }
1411
1412 B.addAttribute(KindStr.str(), ValStr.str());
1413 }
1414 }
1415
Bill Wendlinge46707e2013-02-11 22:32:29 +00001416 MAttributeGroups[GrpID] = AttributeSet::get(Context, Idx, B);
Bill Wendlingba629332013-02-10 23:24:25 +00001417 break;
1418 }
1419 }
1420 }
1421}
1422
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001423std::error_code BitcodeReader::parseTypeTable() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001424 if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001425 return error("Invalid record");
Derek Schuff206dddd2012-02-06 19:03:04 +00001426
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001427 return parseTypeTableBody();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001428}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001429
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001430std::error_code BitcodeReader::parseTypeTableBody() {
Chris Lattner1314b992007-04-22 06:23:29 +00001431 if (!TypeList.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001432 return error("Invalid multiple blocks");
Chris Lattner1314b992007-04-22 06:23:29 +00001433
1434 SmallVector<uint64_t, 64> Record;
1435 unsigned NumRecords = 0;
1436
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001437 SmallString<64> TypeName;
Derek Schuff206dddd2012-02-06 19:03:04 +00001438
Chris Lattner1314b992007-04-22 06:23:29 +00001439 // Read all the records for this type table.
1440 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00001441 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00001442
Chris Lattner27d38752013-01-20 02:13:19 +00001443 switch (Entry.Kind) {
1444 case BitstreamEntry::SubBlock: // Handled for us already.
1445 case BitstreamEntry::Error:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001446 return error("Malformed block");
Chris Lattner27d38752013-01-20 02:13:19 +00001447 case BitstreamEntry::EndBlock:
Chris Lattner1314b992007-04-22 06:23:29 +00001448 if (NumRecords != TypeList.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001449 return error("Malformed block");
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001450 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00001451 case BitstreamEntry::Record:
1452 // The interesting case.
1453 break;
Chris Lattner1314b992007-04-22 06:23:29 +00001454 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001455
Chris Lattner1314b992007-04-22 06:23:29 +00001456 // Read a record.
1457 Record.clear();
Craig Topper2617dcc2014-04-15 06:32:26 +00001458 Type *ResultTy = nullptr;
Chris Lattner27d38752013-01-20 02:13:19 +00001459 switch (Stream.readRecord(Entry.ID, Record)) {
Rafael Espindola48da4f42013-11-04 16:16:24 +00001460 default:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001461 return error("Invalid value");
Chris Lattner1314b992007-04-22 06:23:29 +00001462 case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
1463 // TYPE_CODE_NUMENTRY contains a count of the number of types in the
1464 // type list. This allows us to reserve space.
1465 if (Record.size() < 1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001466 return error("Invalid record");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001467 TypeList.resize(Record[0]);
Chris Lattner1314b992007-04-22 06:23:29 +00001468 continue;
Chris Lattner1314b992007-04-22 06:23:29 +00001469 case bitc::TYPE_CODE_VOID: // VOID
Owen Anderson55f1c092009-08-13 21:58:54 +00001470 ResultTy = Type::getVoidTy(Context);
Chris Lattner1314b992007-04-22 06:23:29 +00001471 break;
Dan Gohman518cda42011-12-17 00:04:22 +00001472 case bitc::TYPE_CODE_HALF: // HALF
1473 ResultTy = Type::getHalfTy(Context);
1474 break;
Chris Lattner1314b992007-04-22 06:23:29 +00001475 case bitc::TYPE_CODE_FLOAT: // FLOAT
Owen Anderson55f1c092009-08-13 21:58:54 +00001476 ResultTy = Type::getFloatTy(Context);
Chris Lattner1314b992007-04-22 06:23:29 +00001477 break;
1478 case bitc::TYPE_CODE_DOUBLE: // DOUBLE
Owen Anderson55f1c092009-08-13 21:58:54 +00001479 ResultTy = Type::getDoubleTy(Context);
Chris Lattner1314b992007-04-22 06:23:29 +00001480 break;
Dale Johannesenff4c3be2007-08-03 01:03:46 +00001481 case bitc::TYPE_CODE_X86_FP80: // X86_FP80
Owen Anderson55f1c092009-08-13 21:58:54 +00001482 ResultTy = Type::getX86_FP80Ty(Context);
Dale Johannesenff4c3be2007-08-03 01:03:46 +00001483 break;
1484 case bitc::TYPE_CODE_FP128: // FP128
Owen Anderson55f1c092009-08-13 21:58:54 +00001485 ResultTy = Type::getFP128Ty(Context);
Dale Johannesenff4c3be2007-08-03 01:03:46 +00001486 break;
1487 case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128
Owen Anderson55f1c092009-08-13 21:58:54 +00001488 ResultTy = Type::getPPC_FP128Ty(Context);
Dale Johannesenff4c3be2007-08-03 01:03:46 +00001489 break;
Chris Lattner1314b992007-04-22 06:23:29 +00001490 case bitc::TYPE_CODE_LABEL: // LABEL
Owen Anderson55f1c092009-08-13 21:58:54 +00001491 ResultTy = Type::getLabelTy(Context);
Chris Lattner1314b992007-04-22 06:23:29 +00001492 break;
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001493 case bitc::TYPE_CODE_METADATA: // METADATA
Owen Anderson55f1c092009-08-13 21:58:54 +00001494 ResultTy = Type::getMetadataTy(Context);
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001495 break;
Dale Johannesenbaa5d042010-09-10 20:55:01 +00001496 case bitc::TYPE_CODE_X86_MMX: // X86_MMX
1497 ResultTy = Type::getX86_MMXTy(Context);
1498 break;
David Majnemerb611e3f2015-08-14 05:09:07 +00001499 case bitc::TYPE_CODE_TOKEN: // TOKEN
1500 ResultTy = Type::getTokenTy(Context);
1501 break;
Filipe Cabecinhasfcd044b2015-01-30 18:13:50 +00001502 case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width]
Chris Lattner1314b992007-04-22 06:23:29 +00001503 if (Record.size() < 1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001504 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001505
Filipe Cabecinhasfcd044b2015-01-30 18:13:50 +00001506 uint64_t NumBits = Record[0];
1507 if (NumBits < IntegerType::MIN_INT_BITS ||
1508 NumBits > IntegerType::MAX_INT_BITS)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001509 return error("Bitwidth for integer type out of range");
Filipe Cabecinhasfcd044b2015-01-30 18:13:50 +00001510 ResultTy = IntegerType::get(Context, NumBits);
Chris Lattner1314b992007-04-22 06:23:29 +00001511 break;
Filipe Cabecinhasfcd044b2015-01-30 18:13:50 +00001512 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001513 case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001514 // [pointee type, address space]
Chris Lattner1314b992007-04-22 06:23:29 +00001515 if (Record.size() < 1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001516 return error("Invalid record");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001517 unsigned AddressSpace = 0;
1518 if (Record.size() == 2)
1519 AddressSpace = Record[1];
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001520 ResultTy = getTypeByID(Record[0]);
Filipe Cabecinhasd8a1bcd2015-04-29 02:27:28 +00001521 if (!ResultTy ||
1522 !PointerType::isValidElementType(ResultTy))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001523 return error("Invalid type");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001524 ResultTy = PointerType::get(ResultTy, AddressSpace);
Chris Lattner1314b992007-04-22 06:23:29 +00001525 break;
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001526 }
Nuno Lopes561dae02012-05-23 15:19:39 +00001527 case bitc::TYPE_CODE_FUNCTION_OLD: {
1528 // FIXME: attrid is dead, remove it in LLVM 4.0
1529 // FUNCTION: [vararg, attrid, retty, paramty x N]
1530 if (Record.size() < 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001531 return error("Invalid record");
Nuno Lopes561dae02012-05-23 15:19:39 +00001532 SmallVector<Type*, 8> ArgTys;
1533 for (unsigned i = 3, e = Record.size(); i != e; ++i) {
1534 if (Type *T = getTypeByID(Record[i]))
1535 ArgTys.push_back(T);
1536 else
1537 break;
1538 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001539
Nuno Lopes561dae02012-05-23 15:19:39 +00001540 ResultTy = getTypeByID(Record[2]);
Craig Topper2617dcc2014-04-15 06:32:26 +00001541 if (!ResultTy || ArgTys.size() < Record.size()-3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001542 return error("Invalid type");
Nuno Lopes561dae02012-05-23 15:19:39 +00001543
1544 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
1545 break;
1546 }
Chad Rosier95898722011-11-03 00:14:01 +00001547 case bitc::TYPE_CODE_FUNCTION: {
1548 // FUNCTION: [vararg, retty, paramty x N]
1549 if (Record.size() < 2)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001550 return error("Invalid record");
Chris Lattnercc3aaf12012-01-27 03:15:49 +00001551 SmallVector<Type*, 8> ArgTys;
Chad Rosier95898722011-11-03 00:14:01 +00001552 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
Filipe Cabecinhas32af5422015-05-19 01:21:06 +00001553 if (Type *T = getTypeByID(Record[i])) {
1554 if (!FunctionType::isValidArgumentType(T))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001555 return error("Invalid function argument type");
Chad Rosier95898722011-11-03 00:14:01 +00001556 ArgTys.push_back(T);
Filipe Cabecinhas32af5422015-05-19 01:21:06 +00001557 }
Chad Rosier95898722011-11-03 00:14:01 +00001558 else
1559 break;
1560 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001561
Chad Rosier95898722011-11-03 00:14:01 +00001562 ResultTy = getTypeByID(Record[1]);
Craig Topper2617dcc2014-04-15 06:32:26 +00001563 if (!ResultTy || ArgTys.size() < Record.size()-2)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001564 return error("Invalid type");
Chad Rosier95898722011-11-03 00:14:01 +00001565
1566 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
1567 break;
1568 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001569 case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N]
Chris Lattner3c5616e2007-05-06 08:21:50 +00001570 if (Record.size() < 1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001571 return error("Invalid record");
Chris Lattnercc3aaf12012-01-27 03:15:49 +00001572 SmallVector<Type*, 8> EltTys;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001573 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
1574 if (Type *T = getTypeByID(Record[i]))
1575 EltTys.push_back(T);
1576 else
1577 break;
1578 }
1579 if (EltTys.size() != Record.size()-1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001580 return error("Invalid type");
Owen Anderson03cb69f2009-08-05 23:16:16 +00001581 ResultTy = StructType::get(Context, EltTys, Record[0]);
Chris Lattner1314b992007-04-22 06:23:29 +00001582 break;
1583 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001584 case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N]
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001585 if (convertToString(Record, 0, TypeName))
1586 return error("Invalid record");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001587 continue;
1588
1589 case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N]
1590 if (Record.size() < 1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001591 return error("Invalid record");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001592
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001593 if (NumRecords >= TypeList.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001594 return error("Invalid TYPE table");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001595
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001596 // Check to see if this was forward referenced, if so fill in the temp.
1597 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
1598 if (Res) {
1599 Res->setName(TypeName);
Craig Topper2617dcc2014-04-15 06:32:26 +00001600 TypeList[NumRecords] = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001601 } else // Otherwise, create a new struct.
Rafael Espindola2fa1e432014-12-03 07:18:23 +00001602 Res = createIdentifiedStructType(Context, TypeName);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001603 TypeName.clear();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001604
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001605 SmallVector<Type*, 8> EltTys;
1606 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
1607 if (Type *T = getTypeByID(Record[i]))
1608 EltTys.push_back(T);
1609 else
1610 break;
1611 }
1612 if (EltTys.size() != Record.size()-1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001613 return error("Invalid record");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001614 Res->setBody(EltTys, Record[0]);
1615 ResultTy = Res;
1616 break;
1617 }
1618 case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: []
1619 if (Record.size() != 1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001620 return error("Invalid record");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001621
1622 if (NumRecords >= TypeList.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001623 return error("Invalid TYPE table");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001624
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001625 // Check to see if this was forward referenced, if so fill in the temp.
1626 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
1627 if (Res) {
1628 Res->setName(TypeName);
Craig Topper2617dcc2014-04-15 06:32:26 +00001629 TypeList[NumRecords] = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001630 } else // Otherwise, create a new struct with no body.
Rafael Espindola2fa1e432014-12-03 07:18:23 +00001631 Res = createIdentifiedStructType(Context, TypeName);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001632 TypeName.clear();
1633 ResultTy = Res;
1634 break;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001635 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001636 case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty]
1637 if (Record.size() < 2)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001638 return error("Invalid record");
Filipe Cabecinhas6fe8aab2015-04-29 02:36:08 +00001639 ResultTy = getTypeByID(Record[1]);
1640 if (!ResultTy || !ArrayType::isValidElementType(ResultTy))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001641 return error("Invalid type");
Filipe Cabecinhas6fe8aab2015-04-29 02:36:08 +00001642 ResultTy = ArrayType::get(ResultTy, Record[0]);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001643 break;
1644 case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty]
1645 if (Record.size() < 2)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001646 return error("Invalid record");
Filipe Cabecinhas8e421902015-06-03 00:05:30 +00001647 if (Record[0] == 0)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001648 return error("Invalid vector length");
Filipe Cabecinhas6fe8aab2015-04-29 02:36:08 +00001649 ResultTy = getTypeByID(Record[1]);
1650 if (!ResultTy || !StructType::isValidElementType(ResultTy))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001651 return error("Invalid type");
Filipe Cabecinhas6fe8aab2015-04-29 02:36:08 +00001652 ResultTy = VectorType::get(ResultTy, Record[0]);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001653 break;
1654 }
1655
1656 if (NumRecords >= TypeList.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001657 return error("Invalid TYPE table");
Filipe Cabecinhasd0858e12015-01-30 10:57:58 +00001658 if (TypeList[NumRecords])
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001659 return error(
Filipe Cabecinhasd0858e12015-01-30 10:57:58 +00001660 "Invalid TYPE table: Only named structs can be forward referenced");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001661 assert(ResultTy && "Didn't read a type?");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001662 TypeList[NumRecords++] = ResultTy;
1663 }
1664}
1665
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00001666std::error_code BitcodeReader::parseOperandBundleTags() {
1667 if (Stream.EnterSubBlock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID))
1668 return error("Invalid record");
1669
1670 if (!BundleTags.empty())
1671 return error("Invalid multiple blocks");
1672
1673 SmallVector<uint64_t, 64> Record;
1674
1675 while (1) {
1676 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1677
1678 switch (Entry.Kind) {
1679 case BitstreamEntry::SubBlock: // Handled for us already.
1680 case BitstreamEntry::Error:
1681 return error("Malformed block");
1682 case BitstreamEntry::EndBlock:
1683 return std::error_code();
1684 case BitstreamEntry::Record:
1685 // The interesting case.
1686 break;
1687 }
1688
1689 // Tags are implicitly mapped to integers by their order.
1690
1691 if (Stream.readRecord(Entry.ID, Record) != bitc::OPERAND_BUNDLE_TAG)
1692 return error("Invalid record");
1693
1694 // OPERAND_BUNDLE_TAG: [strchr x N]
1695 BundleTags.emplace_back();
1696 if (convertToString(Record, 0, BundleTags.back()))
1697 return error("Invalid record");
1698 Record.clear();
1699 }
1700}
1701
Teresa Johnsonff642b92015-09-17 20:12:00 +00001702/// Associate a value with its name from the given index in the provided record.
1703ErrorOr<Value *> BitcodeReader::recordValue(SmallVectorImpl<uint64_t> &Record,
1704 unsigned NameIndex, Triple &TT) {
1705 SmallString<128> ValueName;
1706 if (convertToString(Record, NameIndex, ValueName))
1707 return error("Invalid record");
1708 unsigned ValueID = Record[0];
1709 if (ValueID >= ValueList.size() || !ValueList[ValueID])
1710 return error("Invalid record");
1711 Value *V = ValueList[ValueID];
1712
Filipe Cabecinhasa2b0ac42015-11-04 14:53:36 +00001713 StringRef NameStr(ValueName.data(), ValueName.size());
1714 if (NameStr.find_first_of(0) != StringRef::npos)
1715 return error("Invalid value name");
1716 V->setName(NameStr);
Teresa Johnsonff642b92015-09-17 20:12:00 +00001717 auto *GO = dyn_cast<GlobalObject>(V);
1718 if (GO) {
1719 if (GO->getComdat() == reinterpret_cast<Comdat *>(1)) {
1720 if (TT.isOSBinFormatMachO())
1721 GO->setComdat(nullptr);
1722 else
1723 GO->setComdat(TheModule->getOrInsertComdat(V->getName()));
1724 }
1725 }
1726 return V;
1727}
1728
1729/// Parse the value symbol table at either the current parsing location or
1730/// at the given bit offset if provided.
Peter Collingbourne128a9762015-10-27 23:01:25 +00001731std::error_code BitcodeReader::parseValueSymbolTable(uint64_t Offset) {
Teresa Johnsonff642b92015-09-17 20:12:00 +00001732 uint64_t CurrentBit;
1733 // Pass in the Offset to distinguish between calling for the module-level
1734 // VST (where we want to jump to the VST offset) and the function-level
1735 // VST (where we don't).
1736 if (Offset > 0) {
1737 // Save the current parsing location so we can jump back at the end
1738 // of the VST read.
1739 CurrentBit = Stream.GetCurrentBitNo();
1740 Stream.JumpToBit(Offset * 32);
1741#ifndef NDEBUG
1742 // Do some checking if we are in debug mode.
1743 BitstreamEntry Entry = Stream.advance();
1744 assert(Entry.Kind == BitstreamEntry::SubBlock);
1745 assert(Entry.ID == bitc::VALUE_SYMTAB_BLOCK_ID);
1746#else
1747 // In NDEBUG mode ignore the output so we don't get an unused variable
1748 // warning.
1749 Stream.advance();
1750#endif
1751 }
1752
1753 // Compute the delta between the bitcode indices in the VST (the word offset
1754 // to the word-aligned ENTER_SUBBLOCK for the function block, and that
1755 // expected by the lazy reader. The reader's EnterSubBlock expects to have
1756 // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID
1757 // (size BlockIDWidth). Note that we access the stream's AbbrevID width here
1758 // just before entering the VST subblock because: 1) the EnterSubBlock
1759 // changes the AbbrevID width; 2) the VST block is nested within the same
1760 // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same
1761 // AbbrevID width before calling EnterSubBlock; and 3) when we want to
1762 // jump to the FUNCTION_BLOCK using this offset later, we don't want
1763 // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK.
1764 unsigned FuncBitcodeOffsetDelta =
1765 Stream.getAbbrevIDWidth() + bitc::BlockIDWidth;
1766
Chris Lattner982ec1e2007-05-05 00:17:00 +00001767 if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001768 return error("Invalid record");
Chris Lattnerccaa4482007-04-23 21:26:05 +00001769
1770 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001771
David Majnemer3087b222015-01-20 05:58:07 +00001772 Triple TT(TheModule->getTargetTriple());
1773
Chris Lattnerccaa4482007-04-23 21:26:05 +00001774 // Read all the records for this value table.
1775 SmallString<128> ValueName;
1776 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00001777 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00001778
Chris Lattner27d38752013-01-20 02:13:19 +00001779 switch (Entry.Kind) {
1780 case BitstreamEntry::SubBlock: // Handled for us already.
1781 case BitstreamEntry::Error:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001782 return error("Malformed block");
Chris Lattner27d38752013-01-20 02:13:19 +00001783 case BitstreamEntry::EndBlock:
Teresa Johnsonff642b92015-09-17 20:12:00 +00001784 if (Offset > 0)
1785 Stream.JumpToBit(CurrentBit);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001786 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00001787 case BitstreamEntry::Record:
1788 // The interesting case.
1789 break;
Chris Lattnerccaa4482007-04-23 21:26:05 +00001790 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001791
Chris Lattnerccaa4482007-04-23 21:26:05 +00001792 // Read a record.
1793 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +00001794 switch (Stream.readRecord(Entry.ID, Record)) {
Chris Lattnerccaa4482007-04-23 21:26:05 +00001795 default: // Default behavior: unknown type.
1796 break;
Chris Lattnere14cb882007-05-04 19:11:41 +00001797 case bitc::VST_CODE_ENTRY: { // VST_ENTRY: [valueid, namechar x N]
Teresa Johnsonff642b92015-09-17 20:12:00 +00001798 ErrorOr<Value *> ValOrErr = recordValue(Record, 1, TT);
1799 if (std::error_code EC = ValOrErr.getError())
1800 return EC;
1801 ValOrErr.get();
1802 break;
1803 }
1804 case bitc::VST_CODE_FNENTRY: {
1805 // VST_FNENTRY: [valueid, offset, namechar x N]
1806 ErrorOr<Value *> ValOrErr = recordValue(Record, 2, TT);
1807 if (std::error_code EC = ValOrErr.getError())
1808 return EC;
1809 Value *V = ValOrErr.get();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001810
Teresa Johnsonff642b92015-09-17 20:12:00 +00001811 auto *GO = dyn_cast<GlobalObject>(V);
1812 if (!GO) {
1813 // If this is an alias, need to get the actual Function object
1814 // it aliases, in order to set up the DeferredFunctionInfo entry below.
1815 auto *GA = dyn_cast<GlobalAlias>(V);
1816 if (GA)
1817 GO = GA->getBaseObject();
1818 assert(GO);
Rafael Espindola12ca34f2015-01-19 15:16:06 +00001819 }
Teresa Johnsonff642b92015-09-17 20:12:00 +00001820
1821 uint64_t FuncWordOffset = Record[1];
1822 Function *F = dyn_cast<Function>(GO);
1823 assert(F);
1824 uint64_t FuncBitOffset = FuncWordOffset * 32;
1825 DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta;
Teresa Johnson1493ad92015-10-10 14:18:36 +00001826 // Set the LastFunctionBlockBit to point to the last function block.
Teresa Johnsonff642b92015-09-17 20:12:00 +00001827 // Later when parsing is resumed after function materialization,
1828 // we can simply skip that last function block.
Teresa Johnson1493ad92015-10-10 14:18:36 +00001829 if (FuncBitOffset > LastFunctionBlockBit)
1830 LastFunctionBlockBit = FuncBitOffset;
Chris Lattnerccaa4482007-04-23 21:26:05 +00001831 break;
Reid Spencerdea02bd2007-05-04 01:43:33 +00001832 }
Bill Wendling35a9c3c2011-04-10 23:18:04 +00001833 case bitc::VST_CODE_BBENTRY: {
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001834 if (convertToString(Record, 1, ValueName))
1835 return error("Invalid record");
Chris Lattner6be58c62007-05-03 22:18:21 +00001836 BasicBlock *BB = getBasicBlock(Record[0]);
Craig Topper2617dcc2014-04-15 06:32:26 +00001837 if (!BB)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001838 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001839
Daniel Dunbard786b512009-07-26 00:34:27 +00001840 BB->setName(StringRef(ValueName.data(), ValueName.size()));
Chris Lattner6be58c62007-05-03 22:18:21 +00001841 ValueName.clear();
1842 break;
Chris Lattnerccaa4482007-04-23 21:26:05 +00001843 }
Reid Spencerdea02bd2007-05-04 01:43:33 +00001844 }
Chris Lattnerccaa4482007-04-23 21:26:05 +00001845 }
1846}
1847
Teresa Johnson12545072015-11-15 02:00:09 +00001848/// Parse a single METADATA_KIND record, inserting result in MDKindMap.
1849std::error_code
1850BitcodeReader::parseMetadataKindRecord(SmallVectorImpl<uint64_t> &Record) {
1851 if (Record.size() < 2)
1852 return error("Invalid record");
1853
1854 unsigned Kind = Record[0];
1855 SmallString<8> Name(Record.begin() + 1, Record.end());
1856
1857 unsigned NewKind = TheModule->getMDKindID(Name.str());
1858 if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second)
1859 return error("Conflicting METADATA_KIND records");
1860 return std::error_code();
1861}
1862
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001863static int64_t unrotateSign(uint64_t U) { return U & 1 ? ~(U >> 1) : U >> 1; }
1864
Teresa Johnsond4d3dfd2015-11-20 14:51:27 +00001865/// Parse a METADATA_BLOCK. If ModuleLevel is true then we are parsing
1866/// module level metadata.
1867std::error_code BitcodeReader::parseMetadata(bool ModuleLevel) {
Manman Ren4a9b0eb2015-03-13 19:24:30 +00001868 IsMetadataMaterialized = true;
Devang Patel89923232010-01-11 18:52:33 +00001869 unsigned NextMDValueNo = MDValueList.size();
Teresa Johnsond4d3dfd2015-11-20 14:51:27 +00001870 if (ModuleLevel && SeenModuleValuesRecord) {
1871 // Now that we are parsing the module level metadata, we want to restart
1872 // the numbering of the MD values, and replace temp MD created earlier
1873 // with their real values. If we saw a METADATA_VALUE record then we
1874 // would have set the MDValueList size to the number specified in that
1875 // record, to support parsing function-level metadata first, and we need
1876 // to reset back to 0 to fill the MDValueList in with the parsed module
1877 // The function-level metadata parsing should have reset the MDValueList
1878 // size back to the value reported by the METADATA_VALUE record, saved in
1879 // NumModuleMDs.
1880 assert(NumModuleMDs == MDValueList.size() &&
1881 "Expected MDValueList to only contain module level values");
1882 NextMDValueNo = 0;
1883 }
Devang Patel7428d8a2009-07-22 17:43:22 +00001884
1885 if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001886 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001887
Devang Patel7428d8a2009-07-22 17:43:22 +00001888 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001889
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001890 auto getMD =
1891 [&](unsigned ID) -> Metadata *{ return MDValueList.getValueFwdRef(ID); };
1892 auto getMDOrNull = [&](unsigned ID) -> Metadata *{
1893 if (ID)
1894 return getMD(ID - 1);
1895 return nullptr;
1896 };
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001897 auto getMDString = [&](unsigned ID) -> MDString *{
1898 // This requires that the ID is not really a forward reference. In
1899 // particular, the MDString must already have been resolved.
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001900 return cast_or_null<MDString>(getMDOrNull(ID));
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001901 };
1902
1903#define GET_OR_DISTINCT(CLASS, DISTINCT, ARGS) \
1904 (DISTINCT ? CLASS::getDistinct ARGS : CLASS::get ARGS)
1905
Devang Patel7428d8a2009-07-22 17:43:22 +00001906 // Read all the records.
1907 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00001908 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00001909
Chris Lattner27d38752013-01-20 02:13:19 +00001910 switch (Entry.Kind) {
1911 case BitstreamEntry::SubBlock: // Handled for us already.
1912 case BitstreamEntry::Error:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001913 return error("Malformed block");
Chris Lattner27d38752013-01-20 02:13:19 +00001914 case BitstreamEntry::EndBlock:
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001915 MDValueList.tryToResolveCycles();
Teresa Johnson16e2a9e2015-11-21 03:51:23 +00001916 assert((!(ModuleLevel && SeenModuleValuesRecord) ||
1917 NumModuleMDs == MDValueList.size()) &&
1918 "Inconsistent bitcode: METADATA_VALUES mismatch");
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001919 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00001920 case BitstreamEntry::Record:
1921 // The interesting case.
1922 break;
Devang Patel7428d8a2009-07-22 17:43:22 +00001923 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001924
Devang Patel7428d8a2009-07-22 17:43:22 +00001925 // Read a record.
1926 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +00001927 unsigned Code = Stream.readRecord(Entry.ID, Record);
Duncan P. N. Exon Smith090a19b2015-01-08 22:38:29 +00001928 bool IsDistinct = false;
Dan Gohmanbbcd04d2010-09-13 18:00:48 +00001929 switch (Code) {
Devang Patel7428d8a2009-07-22 17:43:22 +00001930 default: // Default behavior: ignore.
1931 break;
Devang Patel27c87ff2009-07-29 22:34:41 +00001932 case bitc::METADATA_NAME: {
Chris Lattner8d140532013-01-20 02:54:05 +00001933 // Read name of the named metadata.
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001934 SmallString<8> Name(Record.begin(), Record.end());
Devang Patel27c87ff2009-07-29 22:34:41 +00001935 Record.clear();
1936 Code = Stream.ReadCode();
1937
Chris Lattner27d38752013-01-20 02:13:19 +00001938 unsigned NextBitCode = Stream.readRecord(Code, Record);
Filipe Cabecinhas14e68672015-05-30 00:17:20 +00001939 if (NextBitCode != bitc::METADATA_NAMED_NODE)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001940 return error("METADATA_NAME not followed by METADATA_NAMED_NODE");
Devang Patel27c87ff2009-07-29 22:34:41 +00001941
1942 // Read named metadata elements.
1943 unsigned Size = Record.size();
Dan Gohman2637cc12010-07-21 23:38:33 +00001944 NamedMDNode *NMD = TheModule->getOrInsertNamedMetadata(Name);
Devang Patel27c87ff2009-07-29 22:34:41 +00001945 for (unsigned i = 0; i != Size; ++i) {
Karthik Bhat82540e92014-03-27 12:08:23 +00001946 MDNode *MD = dyn_cast_or_null<MDNode>(MDValueList.getValueFwdRef(Record[i]));
Craig Topper2617dcc2014-04-15 06:32:26 +00001947 if (!MD)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001948 return error("Invalid record");
Dan Gohman2637cc12010-07-21 23:38:33 +00001949 NMD->addOperand(MD);
Devang Patel27c87ff2009-07-29 22:34:41 +00001950 }
Devang Patel27c87ff2009-07-29 22:34:41 +00001951 break;
1952 }
Duncan P. N. Exon Smith005f9f42014-12-11 22:30:48 +00001953 case bitc::METADATA_OLD_FN_NODE: {
Duncan P. N. Exon Smith5bd34e52014-12-12 02:11:31 +00001954 // FIXME: Remove in 4.0.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001955 // This is a LocalAsMetadata record, the only type of function-local
1956 // metadata.
Duncan P. N. Exon Smithda41af92014-12-06 01:26:49 +00001957 if (Record.size() % 2 == 1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001958 return error("Invalid record");
Duncan P. N. Exon Smithda41af92014-12-06 01:26:49 +00001959
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001960 // If this isn't a LocalAsMetadata record, we're dropping it. This used
1961 // to be legal, but there's no upgrade path.
Duncan P. N. Exon Smithda41af92014-12-06 01:26:49 +00001962 auto dropRecord = [&] {
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001963 MDValueList.assignValue(MDNode::get(Context, None), NextMDValueNo++);
Duncan P. N. Exon Smithda41af92014-12-06 01:26:49 +00001964 };
1965 if (Record.size() != 2) {
1966 dropRecord();
1967 break;
1968 }
1969
1970 Type *Ty = getTypeByID(Record[0]);
1971 if (Ty->isMetadataTy() || Ty->isVoidTy()) {
1972 dropRecord();
1973 break;
1974 }
1975
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001976 MDValueList.assignValue(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001977 LocalAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
1978 NextMDValueNo++);
Duncan P. N. Exon Smithda41af92014-12-06 01:26:49 +00001979 break;
1980 }
Duncan P. N. Exon Smith005f9f42014-12-11 22:30:48 +00001981 case bitc::METADATA_OLD_NODE: {
Duncan P. N. Exon Smith5bd34e52014-12-12 02:11:31 +00001982 // FIXME: Remove in 4.0.
Dan Gohman1e0213a2010-07-13 19:33:27 +00001983 if (Record.size() % 2 == 1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001984 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001985
Devang Patele059ba6e2009-07-23 01:07:34 +00001986 unsigned Size = Record.size();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001987 SmallVector<Metadata *, 8> Elts;
Devang Patele059ba6e2009-07-23 01:07:34 +00001988 for (unsigned i = 0; i != Size; i += 2) {
Chris Lattner229907c2011-07-18 04:54:35 +00001989 Type *Ty = getTypeByID(Record[i]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001990 if (!Ty)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001991 return error("Invalid record");
Chris Lattnerfdd87902009-10-05 05:54:46 +00001992 if (Ty->isMetadataTy())
Devang Patel05eb6172009-08-04 06:00:18 +00001993 Elts.push_back(MDValueList.getValueFwdRef(Record[i+1]));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001994 else if (!Ty->isVoidTy()) {
1995 auto *MD =
1996 ValueAsMetadata::get(ValueList.getValueFwdRef(Record[i + 1], Ty));
1997 assert(isa<ConstantAsMetadata>(MD) &&
1998 "Expected non-function-local metadata");
1999 Elts.push_back(MD);
2000 } else
Craig Topper2617dcc2014-04-15 06:32:26 +00002001 Elts.push_back(nullptr);
Devang Patele059ba6e2009-07-23 01:07:34 +00002002 }
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002003 MDValueList.assignValue(MDNode::get(Context, Elts), NextMDValueNo++);
Devang Patele059ba6e2009-07-23 01:07:34 +00002004 break;
2005 }
Duncan P. N. Exon Smith5c7006e2014-12-11 23:02:24 +00002006 case bitc::METADATA_VALUE: {
2007 if (Record.size() != 2)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002008 return error("Invalid record");
Duncan P. N. Exon Smith5c7006e2014-12-11 23:02:24 +00002009
2010 Type *Ty = getTypeByID(Record[0]);
2011 if (Ty->isMetadataTy() || Ty->isVoidTy())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002012 return error("Invalid record");
Duncan P. N. Exon Smith5c7006e2014-12-11 23:02:24 +00002013
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002014 MDValueList.assignValue(
Duncan P. N. Exon Smith5c7006e2014-12-11 23:02:24 +00002015 ValueAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
2016 NextMDValueNo++);
2017 break;
2018 }
Duncan P. N. Exon Smith090a19b2015-01-08 22:38:29 +00002019 case bitc::METADATA_DISTINCT_NODE:
2020 IsDistinct = true;
2021 // fallthrough...
Duncan P. N. Exon Smith5c7006e2014-12-11 23:02:24 +00002022 case bitc::METADATA_NODE: {
2023 SmallVector<Metadata *, 8> Elts;
2024 Elts.reserve(Record.size());
2025 for (unsigned ID : Record)
2026 Elts.push_back(ID ? MDValueList.getValueFwdRef(ID - 1) : nullptr);
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002027 MDValueList.assignValue(IsDistinct ? MDNode::getDistinct(Context, Elts)
Duncan P. N. Exon Smith090a19b2015-01-08 22:38:29 +00002028 : MDNode::get(Context, Elts),
2029 NextMDValueNo++);
Duncan P. N. Exon Smith5c7006e2014-12-11 23:02:24 +00002030 break;
2031 }
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00002032 case bitc::METADATA_LOCATION: {
2033 if (Record.size() != 5)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002034 return error("Invalid record");
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00002035
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00002036 unsigned Line = Record[1];
2037 unsigned Column = Record[2];
2038 MDNode *Scope = cast<MDNode>(MDValueList.getValueFwdRef(Record[3]));
2039 Metadata *InlinedAt =
2040 Record[4] ? MDValueList.getValueFwdRef(Record[4] - 1) : nullptr;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002041 MDValueList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002042 GET_OR_DISTINCT(DILocation, Record[0],
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +00002043 (Context, Line, Column, Scope, InlinedAt)),
2044 NextMDValueNo++);
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00002045 break;
2046 }
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00002047 case bitc::METADATA_GENERIC_DEBUG: {
2048 if (Record.size() < 4)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002049 return error("Invalid record");
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00002050
2051 unsigned Tag = Record[1];
2052 unsigned Version = Record[2];
2053
2054 if (Tag >= 1u << 16 || Version != 0)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002055 return error("Invalid record");
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00002056
2057 auto *Header = getMDString(Record[3]);
2058 SmallVector<Metadata *, 8> DwarfOps;
2059 for (unsigned I = 4, E = Record.size(); I != E; ++I)
2060 DwarfOps.push_back(Record[I] ? MDValueList.getValueFwdRef(Record[I] - 1)
2061 : nullptr);
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002062 MDValueList.assignValue(GET_OR_DISTINCT(GenericDINode, Record[0],
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00002063 (Context, Tag, Header, DwarfOps)),
2064 NextMDValueNo++);
2065 break;
2066 }
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00002067 case bitc::METADATA_SUBRANGE: {
2068 if (Record.size() != 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002069 return error("Invalid record");
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00002070
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002071 MDValueList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002072 GET_OR_DISTINCT(DISubrange, Record[0],
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00002073 (Context, Record[1], unrotateSign(Record[2]))),
2074 NextMDValueNo++);
2075 break;
2076 }
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00002077 case bitc::METADATA_ENUMERATOR: {
2078 if (Record.size() != 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002079 return error("Invalid record");
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00002080
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002081 MDValueList.assignValue(GET_OR_DISTINCT(DIEnumerator, Record[0],
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00002082 (Context, unrotateSign(Record[1]),
2083 getMDString(Record[2]))),
2084 NextMDValueNo++);
2085 break;
2086 }
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00002087 case bitc::METADATA_BASIC_TYPE: {
2088 if (Record.size() != 6)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002089 return error("Invalid record");
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00002090
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002091 MDValueList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002092 GET_OR_DISTINCT(DIBasicType, Record[0],
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00002093 (Context, Record[1], getMDString(Record[2]),
2094 Record[3], Record[4], Record[5])),
2095 NextMDValueNo++);
2096 break;
2097 }
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00002098 case bitc::METADATA_DERIVED_TYPE: {
2099 if (Record.size() != 12)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002100 return error("Invalid record");
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00002101
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002102 MDValueList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002103 GET_OR_DISTINCT(DIDerivedType, Record[0],
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00002104 (Context, Record[1], getMDString(Record[2]),
2105 getMDOrNull(Record[3]), Record[4],
Duncan P. N. Exon Smithad6eb1272015-02-20 03:17:58 +00002106 getMDOrNull(Record[5]), getMDOrNull(Record[6]),
2107 Record[7], Record[8], Record[9], Record[10],
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00002108 getMDOrNull(Record[11]))),
2109 NextMDValueNo++);
2110 break;
2111 }
2112 case bitc::METADATA_COMPOSITE_TYPE: {
2113 if (Record.size() != 16)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002114 return error("Invalid record");
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00002115
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002116 MDValueList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002117 GET_OR_DISTINCT(DICompositeType, Record[0],
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00002118 (Context, Record[1], getMDString(Record[2]),
2119 getMDOrNull(Record[3]), Record[4],
2120 getMDOrNull(Record[5]), getMDOrNull(Record[6]),
2121 Record[7], Record[8], Record[9], Record[10],
2122 getMDOrNull(Record[11]), Record[12],
2123 getMDOrNull(Record[13]), getMDOrNull(Record[14]),
2124 getMDString(Record[15]))),
2125 NextMDValueNo++);
2126 break;
2127 }
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00002128 case bitc::METADATA_SUBROUTINE_TYPE: {
2129 if (Record.size() != 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002130 return error("Invalid record");
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00002131
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002132 MDValueList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002133 GET_OR_DISTINCT(DISubroutineType, Record[0],
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00002134 (Context, Record[1], getMDOrNull(Record[2]))),
2135 NextMDValueNo++);
2136 break;
2137 }
Adrian Prantlab1243f2015-06-29 23:03:47 +00002138
2139 case bitc::METADATA_MODULE: {
2140 if (Record.size() != 6)
2141 return error("Invalid record");
2142
2143 MDValueList.assignValue(
2144 GET_OR_DISTINCT(DIModule, Record[0],
2145 (Context, getMDOrNull(Record[1]),
2146 getMDString(Record[2]), getMDString(Record[3]),
2147 getMDString(Record[4]), getMDString(Record[5]))),
2148 NextMDValueNo++);
2149 break;
2150 }
2151
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00002152 case bitc::METADATA_FILE: {
2153 if (Record.size() != 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002154 return error("Invalid record");
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00002155
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002156 MDValueList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002157 GET_OR_DISTINCT(DIFile, Record[0], (Context, getMDString(Record[1]),
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00002158 getMDString(Record[2]))),
2159 NextMDValueNo++);
2160 break;
2161 }
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00002162 case bitc::METADATA_COMPILE_UNIT: {
Amjad Abouda9bcf162015-12-10 12:56:35 +00002163 if (Record.size() < 14 || Record.size() > 16)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002164 return error("Invalid record");
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00002165
Amjad Abouda9bcf162015-12-10 12:56:35 +00002166 // Ignore Record[0], which indicates whether this compile unit is
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00002167 // distinct. It's always distinct.
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002168 MDValueList.assignValue(
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00002169 DICompileUnit::getDistinct(
2170 Context, Record[1], getMDOrNull(Record[2]),
2171 getMDString(Record[3]), Record[4], getMDString(Record[5]),
2172 Record[6], getMDString(Record[7]), Record[8],
2173 getMDOrNull(Record[9]), getMDOrNull(Record[10]),
2174 getMDOrNull(Record[11]), getMDOrNull(Record[12]),
Amjad Abouda9bcf162015-12-10 12:56:35 +00002175 getMDOrNull(Record[13]),
2176 Record.size() <= 15 ? 0 : getMDOrNull(Record[15]),
2177 Record.size() <= 14 ? 0 : Record[14]),
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00002178 NextMDValueNo++);
2179 break;
2180 }
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00002181 case bitc::METADATA_SUBPROGRAM: {
Peter Collingbourned4bff302015-11-05 22:03:56 +00002182 if (Record.size() != 18 && Record.size() != 19)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002183 return error("Invalid record");
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00002184
Peter Collingbourned4bff302015-11-05 22:03:56 +00002185 bool HasFn = Record.size() == 19;
2186 DISubprogram *SP = GET_OR_DISTINCT(
2187 DISubprogram,
2188 Record[0] || Record[8], // All definitions should be distinct.
2189 (Context, getMDOrNull(Record[1]), getMDString(Record[2]),
2190 getMDString(Record[3]), getMDOrNull(Record[4]), Record[5],
2191 getMDOrNull(Record[6]), Record[7], Record[8], Record[9],
2192 getMDOrNull(Record[10]), Record[11], Record[12], Record[13],
2193 Record[14], getMDOrNull(Record[15 + HasFn]),
2194 getMDOrNull(Record[16 + HasFn]), getMDOrNull(Record[17 + HasFn])));
2195 MDValueList.assignValue(SP, NextMDValueNo++);
2196
2197 // Upgrade sp->function mapping to function->sp mapping.
2198 if (HasFn && Record[15]) {
2199 if (auto *CMD = dyn_cast<ConstantAsMetadata>(getMDOrNull(Record[15])))
2200 if (auto *F = dyn_cast<Function>(CMD->getValue())) {
2201 if (F->isMaterializable())
2202 // Defer until materialized; unmaterialized functions may not have
2203 // metadata.
2204 FunctionsWithSPs[F] = SP;
2205 else if (!F->empty())
2206 F->setSubprogram(SP);
2207 }
2208 }
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00002209 break;
2210 }
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00002211 case bitc::METADATA_LEXICAL_BLOCK: {
2212 if (Record.size() != 5)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002213 return error("Invalid record");
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00002214
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002215 MDValueList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002216 GET_OR_DISTINCT(DILexicalBlock, Record[0],
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00002217 (Context, getMDOrNull(Record[1]),
2218 getMDOrNull(Record[2]), Record[3], Record[4])),
2219 NextMDValueNo++);
2220 break;
2221 }
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00002222 case bitc::METADATA_LEXICAL_BLOCK_FILE: {
2223 if (Record.size() != 4)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002224 return error("Invalid record");
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00002225
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002226 MDValueList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002227 GET_OR_DISTINCT(DILexicalBlockFile, Record[0],
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00002228 (Context, getMDOrNull(Record[1]),
2229 getMDOrNull(Record[2]), Record[3])),
2230 NextMDValueNo++);
2231 break;
2232 }
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00002233 case bitc::METADATA_NAMESPACE: {
2234 if (Record.size() != 5)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002235 return error("Invalid record");
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00002236
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002237 MDValueList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002238 GET_OR_DISTINCT(DINamespace, Record[0],
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00002239 (Context, getMDOrNull(Record[1]),
2240 getMDOrNull(Record[2]), getMDString(Record[3]),
2241 Record[4])),
2242 NextMDValueNo++);
2243 break;
2244 }
Amjad Abouda9bcf162015-12-10 12:56:35 +00002245 case bitc::METADATA_MACRO: {
2246 if (Record.size() != 5)
2247 return error("Invalid record");
2248
2249 MDValueList.assignValue(
2250 GET_OR_DISTINCT(DIMacro, Record[0],
2251 (Context, Record[1], Record[2],
2252 getMDString(Record[3]), getMDString(Record[4]))),
2253 NextMDValueNo++);
2254 break;
2255 }
2256 case bitc::METADATA_MACRO_FILE: {
2257 if (Record.size() != 5)
2258 return error("Invalid record");
2259
2260 MDValueList.assignValue(
2261 GET_OR_DISTINCT(DIMacroFile, Record[0],
2262 (Context, Record[1], Record[2],
2263 getMDOrNull(Record[3]), getMDOrNull(Record[4]))),
2264 NextMDValueNo++);
2265 break;
2266 }
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002267 case bitc::METADATA_TEMPLATE_TYPE: {
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00002268 if (Record.size() != 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002269 return error("Invalid record");
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002270
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002271 MDValueList.assignValue(GET_OR_DISTINCT(DITemplateTypeParameter,
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00002272 Record[0],
2273 (Context, getMDString(Record[1]),
2274 getMDOrNull(Record[2]))),
2275 NextMDValueNo++);
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002276 break;
2277 }
2278 case bitc::METADATA_TEMPLATE_VALUE: {
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00002279 if (Record.size() != 5)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002280 return error("Invalid record");
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002281
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002282 MDValueList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002283 GET_OR_DISTINCT(DITemplateValueParameter, Record[0],
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00002284 (Context, Record[1], getMDString(Record[2]),
2285 getMDOrNull(Record[3]), getMDOrNull(Record[4]))),
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002286 NextMDValueNo++);
2287 break;
2288 }
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00002289 case bitc::METADATA_GLOBAL_VAR: {
2290 if (Record.size() != 11)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002291 return error("Invalid record");
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00002292
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002293 MDValueList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002294 GET_OR_DISTINCT(DIGlobalVariable, Record[0],
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00002295 (Context, getMDOrNull(Record[1]),
2296 getMDString(Record[2]), getMDString(Record[3]),
2297 getMDOrNull(Record[4]), Record[5],
2298 getMDOrNull(Record[6]), Record[7], Record[8],
2299 getMDOrNull(Record[9]), getMDOrNull(Record[10]))),
2300 NextMDValueNo++);
2301 break;
2302 }
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00002303 case bitc::METADATA_LOCAL_VAR: {
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00002304 // 10th field is for the obseleted 'inlinedAt:' field.
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002305 if (Record.size() < 8 || Record.size() > 10)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002306 return error("Invalid record");
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00002307
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002308 // 2nd field used to be an artificial tag, either DW_TAG_auto_variable or
2309 // DW_TAG_arg_variable.
2310 bool HasTag = Record.size() > 8;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002311 MDValueList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002312 GET_OR_DISTINCT(DILocalVariable, Record[0],
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002313 (Context, getMDOrNull(Record[1 + HasTag]),
2314 getMDString(Record[2 + HasTag]),
2315 getMDOrNull(Record[3 + HasTag]), Record[4 + HasTag],
2316 getMDOrNull(Record[5 + HasTag]), Record[6 + HasTag],
2317 Record[7 + HasTag])),
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00002318 NextMDValueNo++);
2319 break;
2320 }
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00002321 case bitc::METADATA_EXPRESSION: {
2322 if (Record.size() < 1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002323 return error("Invalid record");
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00002324
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002325 MDValueList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002326 GET_OR_DISTINCT(DIExpression, Record[0],
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00002327 (Context, makeArrayRef(Record).slice(1))),
2328 NextMDValueNo++);
2329 break;
2330 }
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00002331 case bitc::METADATA_OBJC_PROPERTY: {
2332 if (Record.size() != 8)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002333 return error("Invalid record");
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00002334
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002335 MDValueList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002336 GET_OR_DISTINCT(DIObjCProperty, Record[0],
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00002337 (Context, getMDString(Record[1]),
2338 getMDOrNull(Record[2]), Record[3],
2339 getMDString(Record[4]), getMDString(Record[5]),
2340 Record[6], getMDOrNull(Record[7]))),
2341 NextMDValueNo++);
2342 break;
2343 }
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00002344 case bitc::METADATA_IMPORTED_ENTITY: {
2345 if (Record.size() != 6)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002346 return error("Invalid record");
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00002347
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002348 MDValueList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002349 GET_OR_DISTINCT(DIImportedEntity, Record[0],
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00002350 (Context, Record[1], getMDOrNull(Record[2]),
2351 getMDOrNull(Record[3]), Record[4],
2352 getMDString(Record[5]))),
2353 NextMDValueNo++);
2354 break;
2355 }
Devang Patel7428d8a2009-07-22 17:43:22 +00002356 case bitc::METADATA_STRING: {
Eli Bendersky5d5e18d2014-06-25 15:41:00 +00002357 std::string String(Record.begin(), Record.end());
2358 llvm::UpgradeMDStringConstant(String);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002359 Metadata *MD = MDString::get(Context, String);
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002360 MDValueList.assignValue(MD, NextMDValueNo++);
Devang Patel7428d8a2009-07-22 17:43:22 +00002361 break;
2362 }
Devang Patelaf206b82009-09-18 19:26:43 +00002363 case bitc::METADATA_KIND: {
Teresa Johnson12545072015-11-15 02:00:09 +00002364 // Support older bitcode files that had METADATA_KIND records in a
2365 // block with METADATA_BLOCK_ID.
2366 if (std::error_code EC = parseMetadataKindRecord(Record))
2367 return EC;
Devang Patelaf206b82009-09-18 19:26:43 +00002368 break;
2369 }
Devang Patel7428d8a2009-07-22 17:43:22 +00002370 }
2371 }
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00002372#undef GET_OR_DISTINCT
Devang Patel7428d8a2009-07-22 17:43:22 +00002373}
2374
Teresa Johnson12545072015-11-15 02:00:09 +00002375/// Parse the metadata kinds out of the METADATA_KIND_BLOCK.
2376std::error_code BitcodeReader::parseMetadataKinds() {
2377 if (Stream.EnterSubBlock(bitc::METADATA_KIND_BLOCK_ID))
2378 return error("Invalid record");
2379
2380 SmallVector<uint64_t, 64> Record;
2381
2382 // Read all the records.
2383 while (1) {
2384 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
2385
2386 switch (Entry.Kind) {
2387 case BitstreamEntry::SubBlock: // Handled for us already.
2388 case BitstreamEntry::Error:
2389 return error("Malformed block");
2390 case BitstreamEntry::EndBlock:
2391 return std::error_code();
2392 case BitstreamEntry::Record:
2393 // The interesting case.
2394 break;
2395 }
2396
2397 // Read a record.
2398 Record.clear();
2399 unsigned Code = Stream.readRecord(Entry.ID, Record);
2400 switch (Code) {
2401 default: // Default behavior: ignore.
2402 break;
2403 case bitc::METADATA_KIND: {
2404 if (std::error_code EC = parseMetadataKindRecord(Record))
2405 return EC;
2406 break;
2407 }
2408 }
2409 }
2410}
2411
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002412/// Decode a signed value stored with the sign bit in the LSB for dense VBR
2413/// encoding.
Jan Wen Voungafaced02012-10-11 20:20:40 +00002414uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) {
Chris Lattner08feb1e2007-04-24 04:04:35 +00002415 if ((V & 1) == 0)
2416 return V >> 1;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002417 if (V != 1)
Chris Lattner08feb1e2007-04-24 04:04:35 +00002418 return -(V >> 1);
2419 // There is no such thing as -0 with integers. "-0" really means MININT.
2420 return 1ULL << 63;
2421}
2422
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002423/// Resolve all of the initializers for global values and aliases that we can.
2424std::error_code BitcodeReader::resolveGlobalAndAliasInits() {
Chris Lattner44c17072007-04-26 02:46:40 +00002425 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist;
2426 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist;
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00002427 std::vector<std::pair<Function*, unsigned> > FunctionPrefixWorklist;
Peter Collingbourne51d2de72014-12-03 02:08:38 +00002428 std::vector<std::pair<Function*, unsigned> > FunctionPrologueWorklist;
David Majnemer7fddecc2015-06-17 20:52:32 +00002429 std::vector<std::pair<Function*, unsigned> > FunctionPersonalityFnWorklist;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002430
Chris Lattner44c17072007-04-26 02:46:40 +00002431 GlobalInitWorklist.swap(GlobalInits);
2432 AliasInitWorklist.swap(AliasInits);
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00002433 FunctionPrefixWorklist.swap(FunctionPrefixes);
Peter Collingbourne51d2de72014-12-03 02:08:38 +00002434 FunctionPrologueWorklist.swap(FunctionPrologues);
David Majnemer7fddecc2015-06-17 20:52:32 +00002435 FunctionPersonalityFnWorklist.swap(FunctionPersonalityFns);
Chris Lattner44c17072007-04-26 02:46:40 +00002436
2437 while (!GlobalInitWorklist.empty()) {
Chris Lattner831d4202007-04-26 03:27:58 +00002438 unsigned ValID = GlobalInitWorklist.back().second;
Chris Lattner44c17072007-04-26 02:46:40 +00002439 if (ValID >= ValueList.size()) {
2440 // Not ready to resolve this yet, it requires something later in the file.
Chris Lattner831d4202007-04-26 03:27:58 +00002441 GlobalInits.push_back(GlobalInitWorklist.back());
Chris Lattner44c17072007-04-26 02:46:40 +00002442 } else {
Karthik Bhat82540e92014-03-27 12:08:23 +00002443 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
Chris Lattner44c17072007-04-26 02:46:40 +00002444 GlobalInitWorklist.back().first->setInitializer(C);
2445 else
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002446 return error("Expected a constant");
Chris Lattner44c17072007-04-26 02:46:40 +00002447 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002448 GlobalInitWorklist.pop_back();
Chris Lattner44c17072007-04-26 02:46:40 +00002449 }
2450
2451 while (!AliasInitWorklist.empty()) {
2452 unsigned ValID = AliasInitWorklist.back().second;
2453 if (ValID >= ValueList.size()) {
2454 AliasInits.push_back(AliasInitWorklist.back());
2455 } else {
Filipe Cabecinhasa911af02015-06-06 20:44:53 +00002456 Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]);
2457 if (!C)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002458 return error("Expected a constant");
Filipe Cabecinhasa911af02015-06-06 20:44:53 +00002459 GlobalAlias *Alias = AliasInitWorklist.back().first;
2460 if (C->getType() != Alias->getType())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002461 return error("Alias and aliasee types don't match");
Filipe Cabecinhasa911af02015-06-06 20:44:53 +00002462 Alias->setAliasee(C);
Chris Lattner44c17072007-04-26 02:46:40 +00002463 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002464 AliasInitWorklist.pop_back();
Chris Lattner44c17072007-04-26 02:46:40 +00002465 }
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00002466
2467 while (!FunctionPrefixWorklist.empty()) {
2468 unsigned ValID = FunctionPrefixWorklist.back().second;
2469 if (ValID >= ValueList.size()) {
2470 FunctionPrefixes.push_back(FunctionPrefixWorklist.back());
2471 } else {
Karthik Bhat82540e92014-03-27 12:08:23 +00002472 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00002473 FunctionPrefixWorklist.back().first->setPrefixData(C);
2474 else
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002475 return error("Expected a constant");
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00002476 }
2477 FunctionPrefixWorklist.pop_back();
2478 }
2479
Peter Collingbourne51d2de72014-12-03 02:08:38 +00002480 while (!FunctionPrologueWorklist.empty()) {
2481 unsigned ValID = FunctionPrologueWorklist.back().second;
2482 if (ValID >= ValueList.size()) {
2483 FunctionPrologues.push_back(FunctionPrologueWorklist.back());
2484 } else {
2485 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2486 FunctionPrologueWorklist.back().first->setPrologueData(C);
2487 else
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002488 return error("Expected a constant");
Peter Collingbourne51d2de72014-12-03 02:08:38 +00002489 }
2490 FunctionPrologueWorklist.pop_back();
2491 }
2492
David Majnemer7fddecc2015-06-17 20:52:32 +00002493 while (!FunctionPersonalityFnWorklist.empty()) {
2494 unsigned ValID = FunctionPersonalityFnWorklist.back().second;
2495 if (ValID >= ValueList.size()) {
2496 FunctionPersonalityFns.push_back(FunctionPersonalityFnWorklist.back());
2497 } else {
2498 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2499 FunctionPersonalityFnWorklist.back().first->setPersonalityFn(C);
2500 else
2501 return error("Expected a constant");
2502 }
2503 FunctionPersonalityFnWorklist.pop_back();
2504 }
2505
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002506 return std::error_code();
Chris Lattner44c17072007-04-26 02:46:40 +00002507}
2508
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002509static APInt readWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) {
Benjamin Kramer9704ed02012-05-28 14:10:31 +00002510 SmallVector<uint64_t, 8> Words(Vals.size());
2511 std::transform(Vals.begin(), Vals.end(), Words.begin(),
Jan Wen Voungafaced02012-10-11 20:20:40 +00002512 BitcodeReader::decodeSignRotatedValue);
Benjamin Kramer9704ed02012-05-28 14:10:31 +00002513
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002514 return APInt(TypeBits, Words);
2515}
2516
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002517std::error_code BitcodeReader::parseConstants() {
Chris Lattner982ec1e2007-05-05 00:17:00 +00002518 if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002519 return error("Invalid record");
Chris Lattnerfbc1d332007-04-24 03:30:34 +00002520
2521 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002522
Chris Lattnerfbc1d332007-04-24 03:30:34 +00002523 // Read all the records for this value table.
Chris Lattner229907c2011-07-18 04:54:35 +00002524 Type *CurTy = Type::getInt32Ty(Context);
Chris Lattner1663cca2007-04-24 05:48:56 +00002525 unsigned NextCstNo = ValueList.size();
Chris Lattnerfbc1d332007-04-24 03:30:34 +00002526 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00002527 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00002528
Chris Lattner27d38752013-01-20 02:13:19 +00002529 switch (Entry.Kind) {
2530 case BitstreamEntry::SubBlock: // Handled for us already.
2531 case BitstreamEntry::Error:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002532 return error("Malformed block");
Chris Lattner27d38752013-01-20 02:13:19 +00002533 case BitstreamEntry::EndBlock:
2534 if (NextCstNo != ValueList.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002535 return error("Invalid ronstant reference");
Joe Abbey97b7a172013-02-06 22:14:06 +00002536
Chris Lattner27d38752013-01-20 02:13:19 +00002537 // Once all the constants have been read, go through and resolve forward
2538 // references.
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002539 ValueList.resolveConstantForwardRefs();
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002540 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00002541 case BitstreamEntry::Record:
2542 // The interesting case.
Chris Lattner74429932008-08-21 02:34:16 +00002543 break;
Chris Lattnerfbc1d332007-04-24 03:30:34 +00002544 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002545
Chris Lattnerfbc1d332007-04-24 03:30:34 +00002546 // Read a record.
2547 Record.clear();
Craig Topper2617dcc2014-04-15 06:32:26 +00002548 Value *V = nullptr;
Chris Lattner27d38752013-01-20 02:13:19 +00002549 unsigned BitCode = Stream.readRecord(Entry.ID, Record);
Dan Gohman0ebd6962009-07-20 21:19:07 +00002550 switch (BitCode) {
Chris Lattnerfbc1d332007-04-24 03:30:34 +00002551 default: // Default behavior: unknown constant
2552 case bitc::CST_CODE_UNDEF: // UNDEF
Owen Andersonb292b8c2009-07-30 23:03:37 +00002553 V = UndefValue::get(CurTy);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00002554 break;
2555 case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid]
2556 if (Record.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002557 return error("Invalid record");
Karthik Bhat82540e92014-03-27 12:08:23 +00002558 if (Record[0] >= TypeList.size() || !TypeList[Record[0]])
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002559 return error("Invalid record");
Chris Lattnerfbc1d332007-04-24 03:30:34 +00002560 CurTy = TypeList[Record[0]];
Chris Lattner08feb1e2007-04-24 04:04:35 +00002561 continue; // Skip the ValueList manipulation.
Chris Lattnerfbc1d332007-04-24 03:30:34 +00002562 case bitc::CST_CODE_NULL: // NULL
Owen Anderson5a1acd92009-07-31 20:28:14 +00002563 V = Constant::getNullValue(CurTy);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00002564 break;
2565 case bitc::CST_CODE_INTEGER: // INTEGER: [intval]
Duncan Sands19d0b472010-02-16 11:11:14 +00002566 if (!CurTy->isIntegerTy() || Record.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002567 return error("Invalid record");
Jan Wen Voungafaced02012-10-11 20:20:40 +00002568 V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0]));
Chris Lattner08feb1e2007-04-24 04:04:35 +00002569 break;
Chris Lattnere14cb882007-05-04 19:11:41 +00002570 case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
Duncan Sands19d0b472010-02-16 11:11:14 +00002571 if (!CurTy->isIntegerTy() || Record.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002572 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002573
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002574 APInt VInt =
2575 readWideAPInt(Record, cast<IntegerType>(CurTy)->getBitWidth());
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002576 V = ConstantInt::get(Context, VInt);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002577
Chris Lattner08feb1e2007-04-24 04:04:35 +00002578 break;
2579 }
Dale Johannesen245dceb2007-09-11 18:32:33 +00002580 case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval]
Chris Lattner08feb1e2007-04-24 04:04:35 +00002581 if (Record.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002582 return error("Invalid record");
Dan Gohman518cda42011-12-17 00:04:22 +00002583 if (CurTy->isHalfTy())
Tim Northover29178a32013-01-22 09:46:31 +00002584 V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf,
2585 APInt(16, (uint16_t)Record[0])));
Dan Gohman518cda42011-12-17 00:04:22 +00002586 else if (CurTy->isFloatTy())
Tim Northover29178a32013-01-22 09:46:31 +00002587 V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle,
2588 APInt(32, (uint32_t)Record[0])));
Chris Lattnerfdd87902009-10-05 05:54:46 +00002589 else if (CurTy->isDoubleTy())
Tim Northover29178a32013-01-22 09:46:31 +00002590 V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble,
2591 APInt(64, Record[0])));
Chris Lattnerfdd87902009-10-05 05:54:46 +00002592 else if (CurTy->isX86_FP80Ty()) {
Dale Johannesen93eefa02009-03-23 21:16:53 +00002593 // Bits are not stored the same way as a normal i80 APInt, compensate.
2594 uint64_t Rearrange[2];
2595 Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);
2596 Rearrange[1] = Record[0] >> 48;
Tim Northover29178a32013-01-22 09:46:31 +00002597 V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended,
2598 APInt(80, Rearrange)));
Chris Lattnerfdd87902009-10-05 05:54:46 +00002599 } else if (CurTy->isFP128Ty())
Tim Northover29178a32013-01-22 09:46:31 +00002600 V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad,
2601 APInt(128, Record)));
Chris Lattnerfdd87902009-10-05 05:54:46 +00002602 else if (CurTy->isPPC_FP128Ty())
Tim Northover29178a32013-01-22 09:46:31 +00002603 V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble,
2604 APInt(128, Record)));
Chris Lattnerfbc1d332007-04-24 03:30:34 +00002605 else
Owen Andersonb292b8c2009-07-30 23:03:37 +00002606 V = UndefValue::get(CurTy);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00002607 break;
Dale Johannesen245dceb2007-09-11 18:32:33 +00002608 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002609
Chris Lattnere14cb882007-05-04 19:11:41 +00002610 case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]
2611 if (Record.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002612 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002613
Chris Lattnere14cb882007-05-04 19:11:41 +00002614 unsigned Size = Record.size();
Chris Lattnercc3aaf12012-01-27 03:15:49 +00002615 SmallVector<Constant*, 16> Elts;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002616
Chris Lattner229907c2011-07-18 04:54:35 +00002617 if (StructType *STy = dyn_cast<StructType>(CurTy)) {
Chris Lattner1663cca2007-04-24 05:48:56 +00002618 for (unsigned i = 0; i != Size; ++i)
Chris Lattnere14cb882007-05-04 19:11:41 +00002619 Elts.push_back(ValueList.getConstantFwdRef(Record[i],
Chris Lattner1663cca2007-04-24 05:48:56 +00002620 STy->getElementType(i)));
Owen Anderson45308b52009-07-27 22:29:26 +00002621 V = ConstantStruct::get(STy, Elts);
Chris Lattner229907c2011-07-18 04:54:35 +00002622 } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) {
2623 Type *EltTy = ATy->getElementType();
Chris Lattner1663cca2007-04-24 05:48:56 +00002624 for (unsigned i = 0; i != Size; ++i)
Chris Lattnere14cb882007-05-04 19:11:41 +00002625 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
Owen Andersonc2c79322009-07-28 18:32:17 +00002626 V = ConstantArray::get(ATy, Elts);
Chris Lattner229907c2011-07-18 04:54:35 +00002627 } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) {
2628 Type *EltTy = VTy->getElementType();
Chris Lattner1663cca2007-04-24 05:48:56 +00002629 for (unsigned i = 0; i != Size; ++i)
Chris Lattnere14cb882007-05-04 19:11:41 +00002630 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
Owen Anderson4aa32952009-07-28 21:19:26 +00002631 V = ConstantVector::get(Elts);
Chris Lattner1663cca2007-04-24 05:48:56 +00002632 } else {
Owen Andersonb292b8c2009-07-30 23:03:37 +00002633 V = UndefValue::get(CurTy);
Chris Lattner1663cca2007-04-24 05:48:56 +00002634 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002635 break;
2636 }
Chris Lattnerbb8278a2012-02-05 02:41:35 +00002637 case bitc::CST_CODE_STRING: // STRING: [values]
Chris Lattnerf25f7102007-05-06 00:53:07 +00002638 case bitc::CST_CODE_CSTRING: { // CSTRING: [values]
2639 if (Record.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002640 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002641
Benjamin Kramer9704ed02012-05-28 14:10:31 +00002642 SmallString<16> Elts(Record.begin(), Record.end());
Chris Lattnerbb8278a2012-02-05 02:41:35 +00002643 V = ConstantDataArray::getString(Context, Elts,
2644 BitCode == bitc::CST_CODE_CSTRING);
Chris Lattnerf25f7102007-05-06 00:53:07 +00002645 break;
2646 }
Chris Lattner372dd1e2012-01-30 00:51:16 +00002647 case bitc::CST_CODE_DATA: {// DATA: [n x value]
2648 if (Record.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002649 return error("Invalid record");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002650
Chris Lattner372dd1e2012-01-30 00:51:16 +00002651 Type *EltTy = cast<SequentialType>(CurTy)->getElementType();
2652 unsigned Size = Record.size();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002653
Chris Lattner372dd1e2012-01-30 00:51:16 +00002654 if (EltTy->isIntegerTy(8)) {
2655 SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end());
2656 if (isa<VectorType>(CurTy))
2657 V = ConstantDataVector::get(Context, Elts);
2658 else
2659 V = ConstantDataArray::get(Context, Elts);
2660 } else if (EltTy->isIntegerTy(16)) {
2661 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
2662 if (isa<VectorType>(CurTy))
2663 V = ConstantDataVector::get(Context, Elts);
2664 else
2665 V = ConstantDataArray::get(Context, Elts);
2666 } else if (EltTy->isIntegerTy(32)) {
2667 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
2668 if (isa<VectorType>(CurTy))
2669 V = ConstantDataVector::get(Context, Elts);
2670 else
2671 V = ConstantDataArray::get(Context, Elts);
2672 } else if (EltTy->isIntegerTy(64)) {
2673 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
2674 if (isa<VectorType>(CurTy))
2675 V = ConstantDataVector::get(Context, Elts);
2676 else
2677 V = ConstantDataArray::get(Context, Elts);
2678 } else if (EltTy->isFloatTy()) {
Benjamin Kramer9704ed02012-05-28 14:10:31 +00002679 SmallVector<float, 16> Elts(Size);
2680 std::transform(Record.begin(), Record.end(), Elts.begin(), BitsToFloat);
Chris Lattner372dd1e2012-01-30 00:51:16 +00002681 if (isa<VectorType>(CurTy))
2682 V = ConstantDataVector::get(Context, Elts);
2683 else
2684 V = ConstantDataArray::get(Context, Elts);
2685 } else if (EltTy->isDoubleTy()) {
Benjamin Kramer9704ed02012-05-28 14:10:31 +00002686 SmallVector<double, 16> Elts(Size);
2687 std::transform(Record.begin(), Record.end(), Elts.begin(),
2688 BitsToDouble);
Chris Lattner372dd1e2012-01-30 00:51:16 +00002689 if (isa<VectorType>(CurTy))
2690 V = ConstantDataVector::get(Context, Elts);
2691 else
2692 V = ConstantDataArray::get(Context, Elts);
2693 } else {
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002694 return error("Invalid type for value");
Chris Lattner372dd1e2012-01-30 00:51:16 +00002695 }
2696 break;
2697 }
2698
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002699 case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval]
Rafael Espindola48da4f42013-11-04 16:16:24 +00002700 if (Record.size() < 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002701 return error("Invalid record");
2702 int Opc = getDecodedBinaryOpcode(Record[0], CurTy);
Chris Lattner890683d2007-04-24 18:15:21 +00002703 if (Opc < 0) {
Owen Andersonb292b8c2009-07-30 23:03:37 +00002704 V = UndefValue::get(CurTy); // Unknown binop.
Chris Lattner890683d2007-04-24 18:15:21 +00002705 } else {
2706 Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy);
2707 Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy);
Dan Gohman1b849082009-09-07 23:54:19 +00002708 unsigned Flags = 0;
2709 if (Record.size() >= 4) {
2710 if (Opc == Instruction::Add ||
2711 Opc == Instruction::Sub ||
Chris Lattnera676c0f2011-02-07 16:40:21 +00002712 Opc == Instruction::Mul ||
2713 Opc == Instruction::Shl) {
Dan Gohman1b849082009-09-07 23:54:19 +00002714 if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP))
2715 Flags |= OverflowingBinaryOperator::NoSignedWrap;
2716 if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
2717 Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
Chris Lattner35315d02011-02-06 21:44:57 +00002718 } else if (Opc == Instruction::SDiv ||
Chris Lattnera676c0f2011-02-07 16:40:21 +00002719 Opc == Instruction::UDiv ||
2720 Opc == Instruction::LShr ||
2721 Opc == Instruction::AShr) {
Chris Lattner35315d02011-02-06 21:44:57 +00002722 if (Record[3] & (1 << bitc::PEO_EXACT))
Dan Gohman1b849082009-09-07 23:54:19 +00002723 Flags |= SDivOperator::IsExact;
2724 }
2725 }
2726 V = ConstantExpr::get(Opc, LHS, RHS, Flags);
Chris Lattner890683d2007-04-24 18:15:21 +00002727 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002728 break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002729 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002730 case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval]
Rafael Espindola48da4f42013-11-04 16:16:24 +00002731 if (Record.size() < 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002732 return error("Invalid record");
2733 int Opc = getDecodedCastOpcode(Record[0]);
Chris Lattner890683d2007-04-24 18:15:21 +00002734 if (Opc < 0) {
Owen Andersonb292b8c2009-07-30 23:03:37 +00002735 V = UndefValue::get(CurTy); // Unknown cast.
Chris Lattner890683d2007-04-24 18:15:21 +00002736 } else {
Chris Lattner229907c2011-07-18 04:54:35 +00002737 Type *OpTy = getTypeByID(Record[1]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00002738 if (!OpTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002739 return error("Invalid record");
Chris Lattner890683d2007-04-24 18:15:21 +00002740 Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002741 V = UpgradeBitCastExpr(Opc, Op, CurTy);
2742 if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy);
Chris Lattner890683d2007-04-24 18:15:21 +00002743 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002744 break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002745 }
Dan Gohman1639c392009-07-27 21:53:46 +00002746 case bitc::CST_CODE_CE_INBOUNDS_GEP:
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002747 case bitc::CST_CODE_CE_GEP: { // CE_GEP: [n x operands]
David Blaikieb9263572015-03-13 21:03:36 +00002748 unsigned OpNum = 0;
2749 Type *PointeeType = nullptr;
2750 if (Record.size() % 2)
2751 PointeeType = getTypeByID(Record[OpNum++]);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002752 SmallVector<Constant*, 16> Elts;
David Blaikieb9263572015-03-13 21:03:36 +00002753 while (OpNum != Record.size()) {
2754 Type *ElTy = getTypeByID(Record[OpNum++]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00002755 if (!ElTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002756 return error("Invalid record");
David Blaikieb9263572015-03-13 21:03:36 +00002757 Elts.push_back(ValueList.getConstantFwdRef(Record[OpNum++], ElTy));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002758 }
David Blaikieb9263572015-03-13 21:03:36 +00002759
David Blaikieb9263572015-03-13 21:03:36 +00002760 if (PointeeType &&
David Blaikie4a2e73b2015-04-02 18:55:32 +00002761 PointeeType !=
2762 cast<SequentialType>(Elts[0]->getType()->getScalarType())
2763 ->getElementType())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002764 return error("Explicit gep operator type does not match pointee type "
David Blaikie12cf5d702015-03-16 22:03:50 +00002765 "of pointer operand");
David Blaikie4a2e73b2015-04-02 18:55:32 +00002766
2767 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
2768 V = ConstantExpr::getGetElementPtr(PointeeType, Elts[0], Indices,
2769 BitCode ==
2770 bitc::CST_CODE_CE_INBOUNDS_GEP);
Chris Lattner890683d2007-04-24 18:15:21 +00002771 break;
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002772 }
Joe Abbey1a6e7702013-09-12 22:02:31 +00002773 case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#]
Rafael Espindola48da4f42013-11-04 16:16:24 +00002774 if (Record.size() < 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002775 return error("Invalid record");
Joe Abbey1a6e7702013-09-12 22:02:31 +00002776
2777 Type *SelectorTy = Type::getInt1Ty(Context);
2778
Filipe Cabecinhas984fefd2015-08-31 18:00:30 +00002779 // The selector might be an i1 or an <n x i1>
2780 // Get the type from the ValueList before getting a forward ref.
Joe Abbey1a6e7702013-09-12 22:02:31 +00002781 if (VectorType *VTy = dyn_cast<VectorType>(CurTy))
Filipe Cabecinhas984fefd2015-08-31 18:00:30 +00002782 if (Value *V = ValueList[Record[0]])
2783 if (SelectorTy != V->getType())
2784 SelectorTy = VectorType::get(SelectorTy, VTy->getNumElements());
Joe Abbey1a6e7702013-09-12 22:02:31 +00002785
2786 V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0],
2787 SelectorTy),
2788 ValueList.getConstantFwdRef(Record[1],CurTy),
2789 ValueList.getConstantFwdRef(Record[2],CurTy));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002790 break;
Joe Abbey1a6e7702013-09-12 22:02:31 +00002791 }
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002792 case bitc::CST_CODE_CE_EXTRACTELT
2793 : { // CE_EXTRACTELT: [opty, opval, opty, opval]
Rafael Espindola48da4f42013-11-04 16:16:24 +00002794 if (Record.size() < 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002795 return error("Invalid record");
Chris Lattner229907c2011-07-18 04:54:35 +00002796 VectorType *OpTy =
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002797 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
Craig Topper2617dcc2014-04-15 06:32:26 +00002798 if (!OpTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002799 return error("Invalid record");
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002800 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002801 Constant *Op1 = nullptr;
2802 if (Record.size() == 4) {
2803 Type *IdxTy = getTypeByID(Record[2]);
2804 if (!IdxTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002805 return error("Invalid record");
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002806 Op1 = ValueList.getConstantFwdRef(Record[3], IdxTy);
2807 } else // TODO: Remove with llvm 4.0
2808 Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
2809 if (!Op1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002810 return error("Invalid record");
Owen Anderson487375e2009-07-29 18:55:55 +00002811 V = ConstantExpr::getExtractElement(Op0, Op1);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002812 break;
2813 }
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002814 case bitc::CST_CODE_CE_INSERTELT
2815 : { // CE_INSERTELT: [opval, opval, opty, opval]
Chris Lattner229907c2011-07-18 04:54:35 +00002816 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
Craig Topper2617dcc2014-04-15 06:32:26 +00002817 if (Record.size() < 3 || !OpTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002818 return error("Invalid record");
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002819 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
2820 Constant *Op1 = ValueList.getConstantFwdRef(Record[1],
2821 OpTy->getElementType());
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002822 Constant *Op2 = nullptr;
2823 if (Record.size() == 4) {
2824 Type *IdxTy = getTypeByID(Record[2]);
2825 if (!IdxTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002826 return error("Invalid record");
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002827 Op2 = ValueList.getConstantFwdRef(Record[3], IdxTy);
2828 } else // TODO: Remove with llvm 4.0
2829 Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
2830 if (!Op2)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002831 return error("Invalid record");
Owen Anderson487375e2009-07-29 18:55:55 +00002832 V = ConstantExpr::getInsertElement(Op0, Op1, Op2);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002833 break;
2834 }
2835 case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval]
Chris Lattner229907c2011-07-18 04:54:35 +00002836 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
Craig Topper2617dcc2014-04-15 06:32:26 +00002837 if (Record.size() < 3 || !OpTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002838 return error("Invalid record");
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002839 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
2840 Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy);
Chris Lattner229907c2011-07-18 04:54:35 +00002841 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
Owen Andersone9f98042009-07-07 20:18:58 +00002842 OpTy->getNumElements());
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002843 Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy);
Owen Anderson487375e2009-07-29 18:55:55 +00002844 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002845 break;
2846 }
Nate Begeman94aa38d2009-02-12 21:28:33 +00002847 case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval]
Chris Lattner229907c2011-07-18 04:54:35 +00002848 VectorType *RTy = dyn_cast<VectorType>(CurTy);
2849 VectorType *OpTy =
Duncan Sands89d412a2010-10-28 15:47:26 +00002850 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
Craig Topper2617dcc2014-04-15 06:32:26 +00002851 if (Record.size() < 4 || !RTy || !OpTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002852 return error("Invalid record");
Nate Begeman94aa38d2009-02-12 21:28:33 +00002853 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
2854 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
Chris Lattner229907c2011-07-18 04:54:35 +00002855 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
Owen Andersone9f98042009-07-07 20:18:58 +00002856 RTy->getNumElements());
Nate Begeman94aa38d2009-02-12 21:28:33 +00002857 Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy);
Owen Anderson487375e2009-07-29 18:55:55 +00002858 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Nate Begeman94aa38d2009-02-12 21:28:33 +00002859 break;
2860 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002861 case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred]
Rafael Espindola48da4f42013-11-04 16:16:24 +00002862 if (Record.size() < 4)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002863 return error("Invalid record");
Chris Lattner229907c2011-07-18 04:54:35 +00002864 Type *OpTy = getTypeByID(Record[0]);
Craig Topper2617dcc2014-04-15 06:32:26 +00002865 if (!OpTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002866 return error("Invalid record");
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002867 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
2868 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
2869
Duncan Sands9dff9be2010-02-15 16:12:20 +00002870 if (OpTy->isFPOrFPVectorTy())
Owen Anderson487375e2009-07-29 18:55:55 +00002871 V = ConstantExpr::getFCmp(Record[3], Op0, Op1);
Nate Begemand2195702008-05-12 19:01:56 +00002872 else
Owen Anderson487375e2009-07-29 18:55:55 +00002873 V = ConstantExpr::getICmp(Record[3], Op0, Op1);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002874 break;
Chris Lattner1663cca2007-04-24 05:48:56 +00002875 }
Chad Rosierd8c76102012-09-05 19:00:49 +00002876 // This maintains backward compatibility, pre-asm dialect keywords.
Chad Rosier5895eda2012-09-05 06:28:52 +00002877 // FIXME: Remove with the 4.0 release.
Chad Rosier18fcdcf2012-09-05 00:56:20 +00002878 case bitc::CST_CODE_INLINEASM_OLD: {
Rafael Espindola48da4f42013-11-04 16:16:24 +00002879 if (Record.size() < 2)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002880 return error("Invalid record");
Chris Lattneraf8fffc2007-05-06 01:58:20 +00002881 std::string AsmStr, ConstrStr;
Dale Johannesenfd04c742009-10-13 20:46:56 +00002882 bool HasSideEffects = Record[0] & 1;
Dale Johannesen1cfb9582009-10-21 23:28:00 +00002883 bool IsAlignStack = Record[0] >> 1;
Chris Lattneraf8fffc2007-05-06 01:58:20 +00002884 unsigned AsmStrSize = Record[1];
2885 if (2+AsmStrSize >= Record.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002886 return error("Invalid record");
Chris Lattneraf8fffc2007-05-06 01:58:20 +00002887 unsigned ConstStrSize = Record[2+AsmStrSize];
2888 if (3+AsmStrSize+ConstStrSize > Record.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002889 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002890
Chris Lattneraf8fffc2007-05-06 01:58:20 +00002891 for (unsigned i = 0; i != AsmStrSize; ++i)
2892 AsmStr += (char)Record[2+i];
2893 for (unsigned i = 0; i != ConstStrSize; ++i)
2894 ConstrStr += (char)Record[3+AsmStrSize+i];
Chris Lattner229907c2011-07-18 04:54:35 +00002895 PointerType *PTy = cast<PointerType>(CurTy);
Chris Lattneraf8fffc2007-05-06 01:58:20 +00002896 V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
Dale Johannesen1cfb9582009-10-21 23:28:00 +00002897 AsmStr, ConstrStr, HasSideEffects, IsAlignStack);
Chris Lattneraf8fffc2007-05-06 01:58:20 +00002898 break;
2899 }
Chad Rosierd8c76102012-09-05 19:00:49 +00002900 // This version adds support for the asm dialect keywords (e.g.,
2901 // inteldialect).
Chad Rosier18fcdcf2012-09-05 00:56:20 +00002902 case bitc::CST_CODE_INLINEASM: {
Rafael Espindola48da4f42013-11-04 16:16:24 +00002903 if (Record.size() < 2)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002904 return error("Invalid record");
Chad Rosier18fcdcf2012-09-05 00:56:20 +00002905 std::string AsmStr, ConstrStr;
2906 bool HasSideEffects = Record[0] & 1;
2907 bool IsAlignStack = (Record[0] >> 1) & 1;
2908 unsigned AsmDialect = Record[0] >> 2;
2909 unsigned AsmStrSize = Record[1];
2910 if (2+AsmStrSize >= Record.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002911 return error("Invalid record");
Chad Rosier18fcdcf2012-09-05 00:56:20 +00002912 unsigned ConstStrSize = Record[2+AsmStrSize];
2913 if (3+AsmStrSize+ConstStrSize > Record.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002914 return error("Invalid record");
Chad Rosier18fcdcf2012-09-05 00:56:20 +00002915
2916 for (unsigned i = 0; i != AsmStrSize; ++i)
2917 AsmStr += (char)Record[2+i];
2918 for (unsigned i = 0; i != ConstStrSize; ++i)
2919 ConstrStr += (char)Record[3+AsmStrSize+i];
2920 PointerType *PTy = cast<PointerType>(CurTy);
2921 V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
2922 AsmStr, ConstrStr, HasSideEffects, IsAlignStack,
Chad Rosierd8c76102012-09-05 19:00:49 +00002923 InlineAsm::AsmDialect(AsmDialect));
Chad Rosier18fcdcf2012-09-05 00:56:20 +00002924 break;
2925 }
Chris Lattner5956dc82009-10-28 05:53:48 +00002926 case bitc::CST_CODE_BLOCKADDRESS:{
Rafael Espindola48da4f42013-11-04 16:16:24 +00002927 if (Record.size() < 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002928 return error("Invalid record");
Chris Lattner229907c2011-07-18 04:54:35 +00002929 Type *FnTy = getTypeByID(Record[0]);
Craig Topper2617dcc2014-04-15 06:32:26 +00002930 if (!FnTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002931 return error("Invalid record");
Chris Lattner5956dc82009-10-28 05:53:48 +00002932 Function *Fn =
2933 dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy));
Craig Topper2617dcc2014-04-15 06:32:26 +00002934 if (!Fn)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002935 return error("Invalid record");
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00002936
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00002937 // Don't let Fn get dematerialized.
2938 BlockAddressesTaken.insert(Fn);
2939
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00002940 // If the function is already parsed we can insert the block address right
2941 // away.
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00002942 BasicBlock *BB;
2943 unsigned BBID = Record[2];
2944 if (!BBID)
2945 // Invalid reference to entry block.
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002946 return error("Invalid ID");
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00002947 if (!Fn->empty()) {
2948 Function::iterator BBI = Fn->begin(), BBE = Fn->end();
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00002949 for (size_t I = 0, E = BBID; I != E; ++I) {
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00002950 if (BBI == BBE)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002951 return error("Invalid ID");
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00002952 ++BBI;
2953 }
Duncan P. N. Exon Smithfb1743a32015-10-13 16:48:55 +00002954 BB = &*BBI;
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00002955 } else {
2956 // Otherwise insert a placeholder and remember it so it can be inserted
2957 // when the function is parsed.
Duncan P. N. Exon Smith5a511b52014-08-05 17:49:48 +00002958 auto &FwdBBs = BasicBlockFwdRefs[Fn];
2959 if (FwdBBs.empty())
2960 BasicBlockFwdRefQueue.push_back(Fn);
Duncan P. N. Exon Smith5a5fd7b2014-08-16 01:54:37 +00002961 if (FwdBBs.size() < BBID + 1)
2962 FwdBBs.resize(BBID + 1);
2963 if (!FwdBBs[BBID])
2964 FwdBBs[BBID] = BasicBlock::Create(Context);
2965 BB = FwdBBs[BBID];
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00002966 }
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00002967 V = BlockAddress::get(Fn, BB);
Chris Lattner5956dc82009-10-28 05:53:48 +00002968 break;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002969 }
Chris Lattnerfbc1d332007-04-24 03:30:34 +00002970 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002971
David Majnemer8a1c45d2015-12-12 05:38:55 +00002972 ValueList.assignValue(V, NextCstNo);
Chris Lattner1663cca2007-04-24 05:48:56 +00002973 ++NextCstNo;
Chris Lattnerfbc1d332007-04-24 03:30:34 +00002974 }
2975}
Chris Lattner1314b992007-04-22 06:23:29 +00002976
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002977std::error_code BitcodeReader::parseUseLists() {
Chad Rosierca2567b2011-12-07 21:44:12 +00002978 if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002979 return error("Invalid record");
Chad Rosierca2567b2011-12-07 21:44:12 +00002980
Chad Rosierca2567b2011-12-07 21:44:12 +00002981 // Read all the records.
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002982 SmallVector<uint64_t, 64> Record;
Chad Rosierca2567b2011-12-07 21:44:12 +00002983 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00002984 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00002985
Chris Lattner27d38752013-01-20 02:13:19 +00002986 switch (Entry.Kind) {
2987 case BitstreamEntry::SubBlock: // Handled for us already.
2988 case BitstreamEntry::Error:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002989 return error("Malformed block");
Chris Lattner27d38752013-01-20 02:13:19 +00002990 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002991 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00002992 case BitstreamEntry::Record:
2993 // The interesting case.
2994 break;
Chad Rosierca2567b2011-12-07 21:44:12 +00002995 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002996
Chad Rosierca2567b2011-12-07 21:44:12 +00002997 // Read a use list record.
2998 Record.clear();
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002999 bool IsBB = false;
Chris Lattner27d38752013-01-20 02:13:19 +00003000 switch (Stream.readRecord(Entry.ID, Record)) {
Chad Rosierca2567b2011-12-07 21:44:12 +00003001 default: // Default behavior: unknown type.
3002 break;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00003003 case bitc::USELIST_CODE_BB:
3004 IsBB = true;
3005 // fallthrough
3006 case bitc::USELIST_CODE_DEFAULT: {
Chad Rosierca2567b2011-12-07 21:44:12 +00003007 unsigned RecordLength = Record.size();
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00003008 if (RecordLength < 3)
3009 // Records should have at least an ID and two indexes.
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003010 return error("Invalid record");
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00003011 unsigned ID = Record.back();
3012 Record.pop_back();
3013
3014 Value *V;
3015 if (IsBB) {
3016 assert(ID < FunctionBBs.size() && "Basic block not found");
3017 V = FunctionBBs[ID];
3018 } else
3019 V = ValueList[ID];
3020 unsigned NumUses = 0;
3021 SmallDenseMap<const Use *, unsigned, 16> Order;
3022 for (const Use &U : V->uses()) {
Duncan P. N. Exon Smith13183642014-08-16 01:54:34 +00003023 if (++NumUses > Record.size())
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00003024 break;
Duncan P. N. Exon Smith13183642014-08-16 01:54:34 +00003025 Order[&U] = Record[NumUses - 1];
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00003026 }
3027 if (Order.size() != Record.size() || NumUses > Record.size())
3028 // Mismatches can happen if the functions are being materialized lazily
3029 // (out-of-order), or a value has been upgraded.
3030 break;
3031
3032 V->sortUseList([&](const Use &L, const Use &R) {
3033 return Order.lookup(&L) < Order.lookup(&R);
3034 });
Chad Rosierca2567b2011-12-07 21:44:12 +00003035 break;
3036 }
3037 }
3038 }
3039}
3040
Manman Ren4a9b0eb2015-03-13 19:24:30 +00003041/// When we see the block for metadata, remember where it is and then skip it.
3042/// This lets us lazily deserialize the metadata.
3043std::error_code BitcodeReader::rememberAndSkipMetadata() {
3044 // Save the current stream state.
3045 uint64_t CurBit = Stream.GetCurrentBitNo();
3046 DeferredMetadataInfo.push_back(CurBit);
3047
3048 // Skip over the block for now.
3049 if (Stream.SkipBlock())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003050 return error("Invalid record");
Manman Ren4a9b0eb2015-03-13 19:24:30 +00003051 return std::error_code();
3052}
3053
3054std::error_code BitcodeReader::materializeMetadata() {
3055 for (uint64_t BitPos : DeferredMetadataInfo) {
3056 // Move the bit stream to the saved position.
3057 Stream.JumpToBit(BitPos);
Teresa Johnsond4d3dfd2015-11-20 14:51:27 +00003058 if (std::error_code EC = parseMetadata(true))
Manman Ren4a9b0eb2015-03-13 19:24:30 +00003059 return EC;
3060 }
3061 DeferredMetadataInfo.clear();
3062 return std::error_code();
3063}
3064
Rafael Espindola468b8682015-04-01 14:44:59 +00003065void BitcodeReader::setStripDebugInfo() { StripDebugInfo = true; }
Rafael Espindola0d68b4c2015-03-30 21:36:43 +00003066
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003067/// When we see the block for a function body, remember where it is and then
3068/// skip it. This lets us lazily deserialize the functions.
3069std::error_code BitcodeReader::rememberAndSkipFunctionBody() {
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003070 // Get the function we are talking about.
3071 if (FunctionsWithBodies.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003072 return error("Insufficient function protos");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003073
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003074 Function *Fn = FunctionsWithBodies.back();
3075 FunctionsWithBodies.pop_back();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003076
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003077 // Save the current stream state.
3078 uint64_t CurBit = Stream.GetCurrentBitNo();
Teresa Johnson1493ad92015-10-10 14:18:36 +00003079 assert(
3080 (DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) &&
3081 "Mismatch between VST and scanned function offsets");
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003082 DeferredFunctionInfo[Fn] = CurBit;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003083
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003084 // Skip over the function block for now.
3085 if (Stream.SkipBlock())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003086 return error("Invalid record");
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003087 return std::error_code();
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003088}
3089
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003090std::error_code BitcodeReader::globalCleanup() {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003091 // Patch the initializers for globals and aliases up.
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003092 resolveGlobalAndAliasInits();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003093 if (!GlobalInits.empty() || !AliasInits.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003094 return error("Malformed global initializer set");
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003095
3096 // Look for intrinsic functions which need to be upgraded at some point
Yaron Kerenef5e7ad2015-06-12 18:13:20 +00003097 for (Function &F : *TheModule) {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003098 Function *NewFn;
Yaron Kerenef5e7ad2015-06-12 18:13:20 +00003099 if (UpgradeIntrinsicFunction(&F, NewFn))
Rafael Espindola4e721212015-07-02 16:22:40 +00003100 UpgradedIntrinsics[&F] = NewFn;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003101 }
3102
3103 // Look for global variables which need to be renamed.
Yaron Kerenef5e7ad2015-06-12 18:13:20 +00003104 for (GlobalVariable &GV : TheModule->globals())
3105 UpgradeGlobalVariable(&GV);
Reid Klecknerfceb76f2014-05-16 20:39:27 +00003106
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003107 // Force deallocation of memory for these vectors to favor the client that
3108 // want lazy deserialization.
3109 std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits);
3110 std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003111 return std::error_code();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003112}
3113
Teresa Johnson1493ad92015-10-10 14:18:36 +00003114/// Support for lazy parsing of function bodies. This is required if we
3115/// either have an old bitcode file without a VST forward declaration record,
3116/// or if we have an anonymous function being materialized, since anonymous
3117/// functions do not have a name and are therefore not in the VST.
3118std::error_code BitcodeReader::rememberAndSkipFunctionBodies() {
3119 Stream.JumpToBit(NextUnreadBit);
3120
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003121 if (Stream.AtEndOfStream())
3122 return error("Could not find function in stream");
Teresa Johnson1493ad92015-10-10 14:18:36 +00003123
Filipe Cabecinhas7aae2f22015-11-03 13:48:26 +00003124 if (!SeenFirstFunctionBody)
3125 return error("Trying to materialize functions before seeing function blocks");
3126
Teresa Johnson1493ad92015-10-10 14:18:36 +00003127 // An old bitcode file with the symbol table at the end would have
3128 // finished the parse greedily.
3129 assert(SeenValueSymbolTable);
3130
3131 SmallVector<uint64_t, 64> Record;
3132
3133 while (1) {
3134 BitstreamEntry Entry = Stream.advance();
3135 switch (Entry.Kind) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003136 default:
3137 return error("Expect SubBlock");
3138 case BitstreamEntry::SubBlock:
3139 switch (Entry.ID) {
Teresa Johnson1493ad92015-10-10 14:18:36 +00003140 default:
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003141 return error("Expect function block");
3142 case bitc::FUNCTION_BLOCK_ID:
3143 if (std::error_code EC = rememberAndSkipFunctionBody())
3144 return EC;
3145 NextUnreadBit = Stream.GetCurrentBitNo();
3146 return std::error_code();
3147 }
Teresa Johnson1493ad92015-10-10 14:18:36 +00003148 }
3149 }
3150}
3151
Mehdi Amini5d303282015-10-26 18:37:00 +00003152std::error_code BitcodeReader::parseBitcodeVersion() {
3153 if (Stream.EnterSubBlock(bitc::IDENTIFICATION_BLOCK_ID))
3154 return error("Invalid record");
3155
3156 // Read all the records.
3157 SmallVector<uint64_t, 64> Record;
3158 while (1) {
3159 BitstreamEntry Entry = Stream.advance();
3160
3161 switch (Entry.Kind) {
3162 default:
3163 case BitstreamEntry::Error:
3164 return error("Malformed block");
3165 case BitstreamEntry::EndBlock:
3166 return std::error_code();
3167 case BitstreamEntry::Record:
3168 // The interesting case.
3169 break;
3170 }
3171
3172 // Read a record.
3173 Record.clear();
3174 unsigned BitCode = Stream.readRecord(Entry.ID, Record);
3175 switch (BitCode) {
3176 default: // Default behavior: reject
3177 return error("Invalid value");
3178 case bitc::IDENTIFICATION_CODE_STRING: { // IDENTIFICATION: [strchr x
3179 // N]
3180 convertToString(Record, 0, ProducerIdentification);
3181 break;
3182 }
3183 case bitc::IDENTIFICATION_CODE_EPOCH: { // EPOCH: [epoch#]
3184 unsigned epoch = (unsigned)Record[0];
3185 if (epoch != bitc::BITCODE_CURRENT_EPOCH) {
Oleksiy Vyalov6c2403f2015-10-26 22:37:36 +00003186 return error(
3187 Twine("Incompatible epoch: Bitcode '") + Twine(epoch) +
3188 "' vs current: '" + Twine(bitc::BITCODE_CURRENT_EPOCH) + "'");
Mehdi Amini5d303282015-10-26 18:37:00 +00003189 }
3190 }
3191 }
3192 }
3193}
3194
Teresa Johnson1493ad92015-10-10 14:18:36 +00003195std::error_code BitcodeReader::parseModule(uint64_t ResumeBit,
Manman Ren4a9b0eb2015-03-13 19:24:30 +00003196 bool ShouldLazyLoadMetadata) {
Teresa Johnson1493ad92015-10-10 14:18:36 +00003197 if (ResumeBit)
3198 Stream.JumpToBit(ResumeBit);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003199 else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003200 return error("Invalid record");
Chris Lattner1314b992007-04-22 06:23:29 +00003201
Chris Lattner1314b992007-04-22 06:23:29 +00003202 SmallVector<uint64_t, 64> Record;
3203 std::vector<std::string> SectionTable;
Gordon Henriksend930f912008-08-17 18:44:35 +00003204 std::vector<std::string> GCTable;
Chris Lattner1314b992007-04-22 06:23:29 +00003205
3206 // Read all the records for this module.
Chris Lattner27d38752013-01-20 02:13:19 +00003207 while (1) {
3208 BitstreamEntry Entry = Stream.advance();
Joe Abbey97b7a172013-02-06 22:14:06 +00003209
Chris Lattner27d38752013-01-20 02:13:19 +00003210 switch (Entry.Kind) {
3211 case BitstreamEntry::Error:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003212 return error("Malformed block");
Chris Lattner27d38752013-01-20 02:13:19 +00003213 case BitstreamEntry::EndBlock:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003214 return globalCleanup();
Joe Abbey97b7a172013-02-06 22:14:06 +00003215
Chris Lattner27d38752013-01-20 02:13:19 +00003216 case BitstreamEntry::SubBlock:
3217 switch (Entry.ID) {
Chris Lattner1314b992007-04-22 06:23:29 +00003218 default: // Skip unknown content.
3219 if (Stream.SkipBlock())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003220 return error("Invalid record");
Chris Lattner1314b992007-04-22 06:23:29 +00003221 break;
Chris Lattner6eeea5d2007-05-05 18:57:30 +00003222 case bitc::BLOCKINFO_BLOCK_ID:
3223 if (Stream.ReadBlockInfoBlock())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003224 return error("Malformed block");
Chris Lattner6eeea5d2007-05-05 18:57:30 +00003225 break;
Chris Lattnerfee5a372007-05-04 03:30:17 +00003226 case bitc::PARAMATTR_BLOCK_ID:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003227 if (std::error_code EC = parseAttributeBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00003228 return EC;
Chris Lattnerfee5a372007-05-04 03:30:17 +00003229 break;
Bill Wendlingba629332013-02-10 23:24:25 +00003230 case bitc::PARAMATTR_GROUP_BLOCK_ID:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003231 if (std::error_code EC = parseAttributeGroupBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00003232 return EC;
Bill Wendlingba629332013-02-10 23:24:25 +00003233 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003234 case bitc::TYPE_BLOCK_ID_NEW:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003235 if (std::error_code EC = parseTypeTable())
Rafael Espindola48da4f42013-11-04 16:16:24 +00003236 return EC;
Chris Lattner1314b992007-04-22 06:23:29 +00003237 break;
Chris Lattnerccaa4482007-04-23 21:26:05 +00003238 case bitc::VALUE_SYMTAB_BLOCK_ID:
Teresa Johnsonff642b92015-09-17 20:12:00 +00003239 if (!SeenValueSymbolTable) {
3240 // Either this is an old form VST without function index and an
3241 // associated VST forward declaration record (which would have caused
3242 // the VST to be jumped to and parsed before it was encountered
3243 // normally in the stream), or there were no function blocks to
3244 // trigger an earlier parsing of the VST.
3245 assert(VSTOffset == 0 || FunctionsWithBodies.empty());
3246 if (std::error_code EC = parseValueSymbolTable())
3247 return EC;
3248 SeenValueSymbolTable = true;
3249 } else {
3250 // We must have had a VST forward declaration record, which caused
3251 // the parser to jump to and parse the VST earlier.
3252 assert(VSTOffset > 0);
3253 if (Stream.SkipBlock())
3254 return error("Invalid record");
3255 }
Chris Lattnerccaa4482007-04-23 21:26:05 +00003256 break;
Chris Lattnerfbc1d332007-04-24 03:30:34 +00003257 case bitc::CONSTANTS_BLOCK_ID:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003258 if (std::error_code EC = parseConstants())
Rafael Espindola48da4f42013-11-04 16:16:24 +00003259 return EC;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003260 if (std::error_code EC = resolveGlobalAndAliasInits())
Rafael Espindola48da4f42013-11-04 16:16:24 +00003261 return EC;
Chris Lattnerfbc1d332007-04-24 03:30:34 +00003262 break;
Devang Patel7428d8a2009-07-22 17:43:22 +00003263 case bitc::METADATA_BLOCK_ID:
Manman Ren4a9b0eb2015-03-13 19:24:30 +00003264 if (ShouldLazyLoadMetadata && !IsMetadataMaterialized) {
3265 if (std::error_code EC = rememberAndSkipMetadata())
3266 return EC;
3267 break;
3268 }
3269 assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata");
Teresa Johnsond4d3dfd2015-11-20 14:51:27 +00003270 if (std::error_code EC = parseMetadata(true))
Rafael Espindola48da4f42013-11-04 16:16:24 +00003271 return EC;
Devang Patel7428d8a2009-07-22 17:43:22 +00003272 break;
Teresa Johnson12545072015-11-15 02:00:09 +00003273 case bitc::METADATA_KIND_BLOCK_ID:
3274 if (std::error_code EC = parseMetadataKinds())
3275 return EC;
3276 break;
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003277 case bitc::FUNCTION_BLOCK_ID:
3278 // If this is the first function body we've seen, reverse the
3279 // FunctionsWithBodies list.
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003280 if (!SeenFirstFunctionBody) {
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003281 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003282 if (std::error_code EC = globalCleanup())
Rafael Espindola48da4f42013-11-04 16:16:24 +00003283 return EC;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003284 SeenFirstFunctionBody = true;
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003285 }
Joe Abbey97b7a172013-02-06 22:14:06 +00003286
Teresa Johnsonff642b92015-09-17 20:12:00 +00003287 if (VSTOffset > 0) {
3288 // If we have a VST forward declaration record, make sure we
3289 // parse the VST now if we haven't already. It is needed to
3290 // set up the DeferredFunctionInfo vector for lazy reading.
3291 if (!SeenValueSymbolTable) {
3292 if (std::error_code EC =
3293 BitcodeReader::parseValueSymbolTable(VSTOffset))
3294 return EC;
3295 SeenValueSymbolTable = true;
Teresa Johnson1493ad92015-10-10 14:18:36 +00003296 // Fall through so that we record the NextUnreadBit below.
3297 // This is necessary in case we have an anonymous function that
3298 // is later materialized. Since it will not have a VST entry we
3299 // need to fall back to the lazy parse to find its offset.
Teresa Johnsonff642b92015-09-17 20:12:00 +00003300 } else {
3301 // If we have a VST forward declaration record, but have already
3302 // parsed the VST (just above, when the first function body was
3303 // encountered here), then we are resuming the parse after
Teresa Johnson1493ad92015-10-10 14:18:36 +00003304 // materializing functions. The ResumeBit points to the
3305 // start of the last function block recorded in the
3306 // DeferredFunctionInfo map. Skip it.
Teresa Johnsonff642b92015-09-17 20:12:00 +00003307 if (Stream.SkipBlock())
3308 return error("Invalid record");
3309 continue;
3310 }
3311 }
3312
3313 // Support older bitcode files that did not have the function
Teresa Johnson1493ad92015-10-10 14:18:36 +00003314 // index in the VST, nor a VST forward declaration record, as
3315 // well as anonymous functions that do not have VST entries.
Teresa Johnsonff642b92015-09-17 20:12:00 +00003316 // Build the DeferredFunctionInfo vector on the fly.
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003317 if (std::error_code EC = rememberAndSkipFunctionBody())
Rafael Espindola48da4f42013-11-04 16:16:24 +00003318 return EC;
Teresa Johnson1493ad92015-10-10 14:18:36 +00003319
Rafael Espindola1c863ca2015-06-22 18:06:15 +00003320 // Suspend parsing when we reach the function bodies. Subsequent
3321 // materialization calls will resume it when necessary. If the bitcode
3322 // file is old, the symbol table will be at the end instead and will not
3323 // have been seen yet. In this case, just finish the parse now.
3324 if (SeenValueSymbolTable) {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003325 NextUnreadBit = Stream.GetCurrentBitNo();
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003326 return std::error_code();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003327 }
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003328 break;
Chad Rosierca2567b2011-12-07 21:44:12 +00003329 case bitc::USELIST_BLOCK_ID:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003330 if (std::error_code EC = parseUseLists())
Rafael Espindola48da4f42013-11-04 16:16:24 +00003331 return EC;
Chad Rosierca2567b2011-12-07 21:44:12 +00003332 break;
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00003333 case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID:
3334 if (std::error_code EC = parseOperandBundleTags())
3335 return EC;
3336 break;
Chris Lattner1314b992007-04-22 06:23:29 +00003337 }
3338 continue;
Joe Abbey97b7a172013-02-06 22:14:06 +00003339
Chris Lattner27d38752013-01-20 02:13:19 +00003340 case BitstreamEntry::Record:
3341 // The interesting case.
3342 break;
Chris Lattner1314b992007-04-22 06:23:29 +00003343 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003344
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003345
Chris Lattner1314b992007-04-22 06:23:29 +00003346 // Read a record.
David Blaikie6a51dbd2015-09-17 22:18:59 +00003347 auto BitCode = Stream.readRecord(Entry.ID, Record);
3348 switch (BitCode) {
Chris Lattner1314b992007-04-22 06:23:29 +00003349 default: break; // Default behavior, ignore unknown content.
Jan Wen Voungafaced02012-10-11 20:20:40 +00003350 case bitc::MODULE_CODE_VERSION: { // VERSION: [version#]
Chris Lattner1314b992007-04-22 06:23:29 +00003351 if (Record.size() < 1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003352 return error("Invalid record");
Jan Wen Voungafaced02012-10-11 20:20:40 +00003353 // Only version #0 and #1 are supported so far.
3354 unsigned module_version = Record[0];
3355 switch (module_version) {
Rafael Espindola48da4f42013-11-04 16:16:24 +00003356 default:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003357 return error("Invalid value");
Jan Wen Voungafaced02012-10-11 20:20:40 +00003358 case 0:
3359 UseRelativeIDs = false;
3360 break;
3361 case 1:
3362 UseRelativeIDs = true;
3363 break;
3364 }
Chris Lattner1314b992007-04-22 06:23:29 +00003365 break;
Jan Wen Voungafaced02012-10-11 20:20:40 +00003366 }
Chris Lattnere14cb882007-05-04 19:11:41 +00003367 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00003368 std::string S;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003369 if (convertToString(Record, 0, S))
3370 return error("Invalid record");
Chris Lattner1314b992007-04-22 06:23:29 +00003371 TheModule->setTargetTriple(S);
3372 break;
3373 }
Chris Lattnere14cb882007-05-04 19:11:41 +00003374 case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00003375 std::string S;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003376 if (convertToString(Record, 0, S))
3377 return error("Invalid record");
Chris Lattner1314b992007-04-22 06:23:29 +00003378 TheModule->setDataLayout(S);
3379 break;
3380 }
Chris Lattnere14cb882007-05-04 19:11:41 +00003381 case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00003382 std::string S;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003383 if (convertToString(Record, 0, S))
3384 return error("Invalid record");
Chris Lattner1314b992007-04-22 06:23:29 +00003385 TheModule->setModuleInlineAsm(S);
3386 break;
3387 }
Bill Wendling706d3d62012-11-28 08:41:48 +00003388 case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N]
3389 // FIXME: Remove in 4.0.
3390 std::string S;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003391 if (convertToString(Record, 0, S))
3392 return error("Invalid record");
Bill Wendling706d3d62012-11-28 08:41:48 +00003393 // Ignore value.
3394 break;
3395 }
Chris Lattnere14cb882007-05-04 19:11:41 +00003396 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00003397 std::string S;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003398 if (convertToString(Record, 0, S))
3399 return error("Invalid record");
Chris Lattner1314b992007-04-22 06:23:29 +00003400 SectionTable.push_back(S);
3401 break;
3402 }
Gordon Henriksend930f912008-08-17 18:44:35 +00003403 case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N]
Gordon Henriksen71183b62007-12-10 03:18:06 +00003404 std::string S;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003405 if (convertToString(Record, 0, S))
3406 return error("Invalid record");
Gordon Henriksend930f912008-08-17 18:44:35 +00003407 GCTable.push_back(S);
Gordon Henriksen71183b62007-12-10 03:18:06 +00003408 break;
3409 }
David Majnemerdad0a642014-06-27 18:19:56 +00003410 case bitc::MODULE_CODE_COMDAT: { // COMDAT: [selection_kind, name]
3411 if (Record.size() < 2)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003412 return error("Invalid record");
David Majnemerdad0a642014-06-27 18:19:56 +00003413 Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]);
3414 unsigned ComdatNameSize = Record[1];
3415 std::string ComdatName;
3416 ComdatName.reserve(ComdatNameSize);
3417 for (unsigned i = 0; i != ComdatNameSize; ++i)
3418 ComdatName += (char)Record[2 + i];
3419 Comdat *C = TheModule->getOrInsertComdat(ComdatName);
3420 C->setSelectionKind(SK);
3421 ComdatList.push_back(C);
3422 break;
3423 }
Christopher Lamb54dd24c2007-12-11 08:59:05 +00003424 // GLOBALVAR: [pointer type, isconst, initid,
Rafael Espindola45e6c192011-01-08 16:42:36 +00003425 // linkage, alignment, section, visibility, threadlocal,
Peter Collingbourne69ba0162015-02-04 00:42:45 +00003426 // unnamed_addr, externally_initialized, dllstorageclass,
3427 // comdat]
Chris Lattner1314b992007-04-22 06:23:29 +00003428 case bitc::MODULE_CODE_GLOBALVAR: {
Chris Lattner4b00d922007-04-23 16:04:05 +00003429 if (Record.size() < 6)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003430 return error("Invalid record");
Chris Lattner229907c2011-07-18 04:54:35 +00003431 Type *Ty = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00003432 if (!Ty)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003433 return error("Invalid record");
David Blaikie1a848da2015-04-27 19:58:56 +00003434 bool isConstant = Record[1] & 1;
3435 bool explicitType = Record[1] & 2;
3436 unsigned AddressSpace;
3437 if (explicitType) {
3438 AddressSpace = Record[1] >> 2;
3439 } else {
3440 if (!Ty->isPointerTy())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003441 return error("Invalid type for value");
David Blaikie1a848da2015-04-27 19:58:56 +00003442 AddressSpace = cast<PointerType>(Ty)->getAddressSpace();
3443 Ty = cast<PointerType>(Ty)->getElementType();
3444 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003445
Rafael Espindola12ca34f2015-01-19 15:16:06 +00003446 uint64_t RawLinkage = Record[3];
3447 GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage);
JF Bastien30bf96b2015-02-22 19:32:03 +00003448 unsigned Alignment;
3449 if (std::error_code EC = parseAlignmentValue(Record[4], Alignment))
3450 return EC;
Chris Lattner1314b992007-04-22 06:23:29 +00003451 std::string Section;
3452 if (Record[5]) {
3453 if (Record[5]-1 >= SectionTable.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003454 return error("Invalid ID");
Chris Lattner1314b992007-04-22 06:23:29 +00003455 Section = SectionTable[Record[5]-1];
3456 }
Chris Lattner4b00d922007-04-23 16:04:05 +00003457 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00003458 // Local linkage must have default visibility.
3459 if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage))
3460 // FIXME: Change to an error if non-default in 4.0.
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003461 Visibility = getDecodedVisibility(Record[6]);
Hans Wennborgcbe34b42012-06-23 11:37:03 +00003462
3463 GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal;
Chris Lattner53862f72007-05-06 19:27:46 +00003464 if (Record.size() > 7)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003465 TLM = getDecodedThreadLocalMode(Record[7]);
Chris Lattner1314b992007-04-22 06:23:29 +00003466
Rafael Espindola45e6c192011-01-08 16:42:36 +00003467 bool UnnamedAddr = false;
3468 if (Record.size() > 8)
3469 UnnamedAddr = Record[8];
3470
Michael Gottesman27e7ef32013-02-05 05:57:38 +00003471 bool ExternallyInitialized = false;
3472 if (Record.size() > 9)
3473 ExternallyInitialized = Record[9];
3474
Chris Lattner1314b992007-04-22 06:23:29 +00003475 GlobalVariable *NewGV =
Craig Topper2617dcc2014-04-15 06:32:26 +00003476 new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, "", nullptr,
Michael Gottesman27e7ef32013-02-05 05:57:38 +00003477 TLM, AddressSpace, ExternallyInitialized);
Chris Lattner1314b992007-04-22 06:23:29 +00003478 NewGV->setAlignment(Alignment);
3479 if (!Section.empty())
3480 NewGV->setSection(Section);
3481 NewGV->setVisibility(Visibility);
Rafael Espindola45e6c192011-01-08 16:42:36 +00003482 NewGV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003483
Nico Rieck7157bb72014-01-14 15:22:47 +00003484 if (Record.size() > 10)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003485 NewGV->setDLLStorageClass(getDecodedDLLStorageClass(Record[10]));
Nico Rieck7157bb72014-01-14 15:22:47 +00003486 else
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003487 upgradeDLLImportExportLinkage(NewGV, RawLinkage);
Nico Rieck7157bb72014-01-14 15:22:47 +00003488
Chris Lattnerccaa4482007-04-23 21:26:05 +00003489 ValueList.push_back(NewGV);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003490
Chris Lattner47d131b2007-04-24 00:18:21 +00003491 // Remember which value to use for the global initializer.
3492 if (unsigned InitID = Record[2])
3493 GlobalInits.push_back(std::make_pair(NewGV, InitID-1));
David Majnemerdad0a642014-06-27 18:19:56 +00003494
Rafael Espindola12ca34f2015-01-19 15:16:06 +00003495 if (Record.size() > 11) {
David Majnemerdad0a642014-06-27 18:19:56 +00003496 if (unsigned ComdatID = Record[11]) {
Filipe Cabecinhas0eb8a592015-05-26 23:00:56 +00003497 if (ComdatID > ComdatList.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003498 return error("Invalid global variable comdat ID");
David Majnemerdad0a642014-06-27 18:19:56 +00003499 NewGV->setComdat(ComdatList[ComdatID - 1]);
3500 }
Rafael Espindola12ca34f2015-01-19 15:16:06 +00003501 } else if (hasImplicitComdat(RawLinkage)) {
3502 NewGV->setComdat(reinterpret_cast<Comdat *>(1));
3503 }
Chris Lattner1314b992007-04-22 06:23:29 +00003504 break;
3505 }
Chris Lattner4c0a6d62007-05-08 05:38:01 +00003506 // FUNCTION: [type, callingconv, isproto, linkage, paramattr,
Nico Rieck7157bb72014-01-14 15:22:47 +00003507 // alignment, section, visibility, gc, unnamed_addr,
Peter Collingbourne51d2de72014-12-03 02:08:38 +00003508 // prologuedata, dllstorageclass, comdat, prefixdata]
Chris Lattner1314b992007-04-22 06:23:29 +00003509 case bitc::MODULE_CODE_FUNCTION: {
Chris Lattner4c0a6d62007-05-08 05:38:01 +00003510 if (Record.size() < 8)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003511 return error("Invalid record");
Chris Lattner229907c2011-07-18 04:54:35 +00003512 Type *Ty = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00003513 if (!Ty)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003514 return error("Invalid record");
David Blaikie561a1572015-04-17 16:28:26 +00003515 if (auto *PTy = dyn_cast<PointerType>(Ty))
3516 Ty = PTy->getElementType();
3517 auto *FTy = dyn_cast<FunctionType>(Ty);
Chris Lattner1314b992007-04-22 06:23:29 +00003518 if (!FTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003519 return error("Invalid type for value");
Vedant Kumarad6d6e72015-10-27 21:17:06 +00003520 auto CC = static_cast<CallingConv::ID>(Record[1]);
3521 if (CC & ~CallingConv::MaxID)
3522 return error("Invalid calling convention ID");
Chris Lattner1314b992007-04-22 06:23:29 +00003523
Gabor Greife9ecc682008-04-06 20:25:17 +00003524 Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage,
3525 "", TheModule);
Chris Lattner1314b992007-04-22 06:23:29 +00003526
Vedant Kumarad6d6e72015-10-27 21:17:06 +00003527 Func->setCallingConv(CC);
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003528 bool isProto = Record[2];
Rafael Espindola12ca34f2015-01-19 15:16:06 +00003529 uint64_t RawLinkage = Record[3];
3530 Func->setLinkage(getDecodedLinkage(RawLinkage));
Devang Patel4c758ea2008-09-25 21:00:45 +00003531 Func->setAttributes(getAttributes(Record[4]));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003532
JF Bastien30bf96b2015-02-22 19:32:03 +00003533 unsigned Alignment;
3534 if (std::error_code EC = parseAlignmentValue(Record[5], Alignment))
3535 return EC;
3536 Func->setAlignment(Alignment);
Chris Lattner4c0a6d62007-05-08 05:38:01 +00003537 if (Record[6]) {
3538 if (Record[6]-1 >= SectionTable.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003539 return error("Invalid ID");
Chris Lattner4c0a6d62007-05-08 05:38:01 +00003540 Func->setSection(SectionTable[Record[6]-1]);
Chris Lattner1314b992007-04-22 06:23:29 +00003541 }
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00003542 // Local linkage must have default visibility.
3543 if (!Func->hasLocalLinkage())
3544 // FIXME: Change to an error if non-default in 4.0.
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003545 Func->setVisibility(getDecodedVisibility(Record[7]));
Gordon Henriksen71183b62007-12-10 03:18:06 +00003546 if (Record.size() > 8 && Record[8]) {
Filipe Cabecinhasf8a16a92015-04-30 04:09:41 +00003547 if (Record[8]-1 >= GCTable.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003548 return error("Invalid ID");
Gordon Henriksend930f912008-08-17 18:44:35 +00003549 Func->setGC(GCTable[Record[8]-1].c_str());
Gordon Henriksen71183b62007-12-10 03:18:06 +00003550 }
Rafael Espindola45e6c192011-01-08 16:42:36 +00003551 bool UnnamedAddr = false;
3552 if (Record.size() > 9)
3553 UnnamedAddr = Record[9];
3554 Func->setUnnamedAddr(UnnamedAddr);
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003555 if (Record.size() > 10 && Record[10] != 0)
Peter Collingbourne51d2de72014-12-03 02:08:38 +00003556 FunctionPrologues.push_back(std::make_pair(Func, Record[10]-1));
Nico Rieck7157bb72014-01-14 15:22:47 +00003557
3558 if (Record.size() > 11)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003559 Func->setDLLStorageClass(getDecodedDLLStorageClass(Record[11]));
Nico Rieck7157bb72014-01-14 15:22:47 +00003560 else
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003561 upgradeDLLImportExportLinkage(Func, RawLinkage);
Nico Rieck7157bb72014-01-14 15:22:47 +00003562
Rafael Espindola12ca34f2015-01-19 15:16:06 +00003563 if (Record.size() > 12) {
David Majnemerdad0a642014-06-27 18:19:56 +00003564 if (unsigned ComdatID = Record[12]) {
Filipe Cabecinhas0eb8a592015-05-26 23:00:56 +00003565 if (ComdatID > ComdatList.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003566 return error("Invalid function comdat ID");
David Majnemerdad0a642014-06-27 18:19:56 +00003567 Func->setComdat(ComdatList[ComdatID - 1]);
3568 }
Rafael Espindola12ca34f2015-01-19 15:16:06 +00003569 } else if (hasImplicitComdat(RawLinkage)) {
3570 Func->setComdat(reinterpret_cast<Comdat *>(1));
3571 }
David Majnemerdad0a642014-06-27 18:19:56 +00003572
Peter Collingbourne51d2de72014-12-03 02:08:38 +00003573 if (Record.size() > 13 && Record[13] != 0)
3574 FunctionPrefixes.push_back(std::make_pair(Func, Record[13]-1));
3575
David Majnemer7fddecc2015-06-17 20:52:32 +00003576 if (Record.size() > 14 && Record[14] != 0)
3577 FunctionPersonalityFns.push_back(std::make_pair(Func, Record[14] - 1));
3578
Chris Lattnerccaa4482007-04-23 21:26:05 +00003579 ValueList.push_back(Func);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003580
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003581 // If this is a function with a body, remember the prototype we are
3582 // creating now, so that we can match up the body with them later.
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003583 if (!isProto) {
Rafael Espindolad4bcefc2014-10-24 18:13:04 +00003584 Func->setIsMaterializable(true);
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003585 FunctionsWithBodies.push_back(Func);
Rafael Espindola1c863ca2015-06-22 18:06:15 +00003586 DeferredFunctionInfo[Func] = 0;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003587 }
Chris Lattner1314b992007-04-22 06:23:29 +00003588 break;
3589 }
David Blaikie6a51dbd2015-09-17 22:18:59 +00003590 // ALIAS: [alias type, addrspace, aliasee val#, linkage]
3591 // ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility, dllstorageclass]
3592 case bitc::MODULE_CODE_ALIAS:
3593 case bitc::MODULE_CODE_ALIAS_OLD: {
3594 bool NewRecord = BitCode == bitc::MODULE_CODE_ALIAS;
Aaron Ballman2d0f38c2015-09-18 13:31:42 +00003595 if (Record.size() < (3 + (unsigned)NewRecord))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003596 return error("Invalid record");
David Blaikie6a51dbd2015-09-17 22:18:59 +00003597 unsigned OpNum = 0;
3598 Type *Ty = getTypeByID(Record[OpNum++]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00003599 if (!Ty)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003600 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003601
David Blaikie6a51dbd2015-09-17 22:18:59 +00003602 unsigned AddrSpace;
3603 if (!NewRecord) {
3604 auto *PTy = dyn_cast<PointerType>(Ty);
3605 if (!PTy)
3606 return error("Invalid type for value");
3607 Ty = PTy->getElementType();
3608 AddrSpace = PTy->getAddressSpace();
3609 } else {
3610 AddrSpace = Record[OpNum++];
3611 }
3612
3613 auto Val = Record[OpNum++];
3614 auto Linkage = Record[OpNum++];
3615 auto *NewGA = GlobalAlias::create(
3616 Ty, AddrSpace, getDecodedLinkage(Linkage), "", TheModule);
Anton Korobeynikov2f22e3f2008-03-12 00:49:19 +00003617 // Old bitcode files didn't have visibility field.
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00003618 // Local linkage must have default visibility.
David Blaikie6a51dbd2015-09-17 22:18:59 +00003619 if (OpNum != Record.size()) {
3620 auto VisInd = OpNum++;
3621 if (!NewGA->hasLocalLinkage())
3622 // FIXME: Change to an error if non-default in 4.0.
3623 NewGA->setVisibility(getDecodedVisibility(Record[VisInd]));
3624 }
3625 if (OpNum != Record.size())
3626 NewGA->setDLLStorageClass(getDecodedDLLStorageClass(Record[OpNum++]));
Nico Rieck7157bb72014-01-14 15:22:47 +00003627 else
David Blaikie6a51dbd2015-09-17 22:18:59 +00003628 upgradeDLLImportExportLinkage(NewGA, Linkage);
3629 if (OpNum != Record.size())
3630 NewGA->setThreadLocalMode(getDecodedThreadLocalMode(Record[OpNum++]));
3631 if (OpNum != Record.size())
3632 NewGA->setUnnamedAddr(Record[OpNum++]);
Chris Lattner44c17072007-04-26 02:46:40 +00003633 ValueList.push_back(NewGA);
David Blaikie6a51dbd2015-09-17 22:18:59 +00003634 AliasInits.push_back(std::make_pair(NewGA, Val));
Chris Lattner44c17072007-04-26 02:46:40 +00003635 break;
Chris Lattner1314b992007-04-22 06:23:29 +00003636 }
Chris Lattner831d4202007-04-26 03:27:58 +00003637 /// MODULE_CODE_PURGEVALS: [numvals]
3638 case bitc::MODULE_CODE_PURGEVALS:
3639 // Trim down the value list to the specified size.
3640 if (Record.size() < 1 || Record[0] > ValueList.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003641 return error("Invalid record");
Chris Lattner831d4202007-04-26 03:27:58 +00003642 ValueList.shrinkTo(Record[0]);
3643 break;
Teresa Johnsonff642b92015-09-17 20:12:00 +00003644 /// MODULE_CODE_VSTOFFSET: [offset]
3645 case bitc::MODULE_CODE_VSTOFFSET:
3646 if (Record.size() < 1)
3647 return error("Invalid record");
3648 VSTOffset = Record[0];
3649 break;
Teresa Johnsond4d3dfd2015-11-20 14:51:27 +00003650 /// MODULE_CODE_METADATA_VALUES: [numvals]
3651 case bitc::MODULE_CODE_METADATA_VALUES:
3652 if (Record.size() < 1)
3653 return error("Invalid record");
3654 assert(!IsMetadataMaterialized);
3655 // This record contains the number of metadata values in the module-level
3656 // METADATA_BLOCK. It is used to support lazy parsing of metadata as
3657 // a postpass, where we will parse function-level metadata first.
3658 // This is needed because the ids of metadata are assigned implicitly
3659 // based on their ordering in the bitcode, with the function-level
3660 // metadata ids starting after the module-level metadata ids. Otherwise,
3661 // we would have to parse the module-level metadata block to prime the
3662 // MDValueList when we are lazy loading metadata during function
3663 // importing. Initialize the MDValueList size here based on the
3664 // record value, regardless of whether we are doing lazy metadata
3665 // loading, so that we have consistent handling and assertion
3666 // checking in parseMetadata for module-level metadata.
3667 NumModuleMDs = Record[0];
3668 SeenModuleValuesRecord = true;
3669 assert(MDValueList.size() == 0);
3670 MDValueList.resize(NumModuleMDs);
3671 break;
Chris Lattner831d4202007-04-26 03:27:58 +00003672 }
Chris Lattner1314b992007-04-22 06:23:29 +00003673 Record.clear();
3674 }
Chris Lattner1314b992007-04-22 06:23:29 +00003675}
3676
Teresa Johnson403a7872015-10-04 14:33:43 +00003677/// Helper to read the header common to all bitcode files.
3678static bool hasValidBitcodeHeader(BitstreamCursor &Stream) {
3679 // Sniff for the signature.
3680 if (Stream.Read(8) != 'B' ||
3681 Stream.Read(8) != 'C' ||
3682 Stream.Read(4) != 0x0 ||
3683 Stream.Read(4) != 0xC ||
3684 Stream.Read(4) != 0xE ||
3685 Stream.Read(4) != 0xD)
3686 return false;
3687 return true;
3688}
3689
Rafael Espindola1aabf982015-06-16 23:29:49 +00003690std::error_code
3691BitcodeReader::parseBitcodeInto(std::unique_ptr<DataStreamer> Streamer,
3692 Module *M, bool ShouldLazyLoadMetadata) {
Rafael Espindolac6afe0d2015-06-16 20:03:39 +00003693 TheModule = M;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003694
Rafael Espindola1aabf982015-06-16 23:29:49 +00003695 if (std::error_code EC = initStream(std::move(Streamer)))
Rafael Espindola48da4f42013-11-04 16:16:24 +00003696 return EC;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003697
Chris Lattner1314b992007-04-22 06:23:29 +00003698 // Sniff for the signature.
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003699 if (!hasValidBitcodeHeader(Stream))
3700 return error("Invalid bitcode signature");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003701
Chris Lattner1314b992007-04-22 06:23:29 +00003702 // We expect a number of well-defined blocks, though we don't necessarily
3703 // need to understand them all.
Chris Lattner27d38752013-01-20 02:13:19 +00003704 while (1) {
Filipe Cabecinhas22554272015-04-14 14:07:15 +00003705 if (Stream.AtEndOfStream()) {
Filipe Cabecinhas22554272015-04-14 14:07:15 +00003706 // We didn't really read a proper Module.
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003707 return error("Malformed IR file");
Filipe Cabecinhas22554272015-04-14 14:07:15 +00003708 }
Joe Abbey97b7a172013-02-06 22:14:06 +00003709
Chris Lattner27d38752013-01-20 02:13:19 +00003710 BitstreamEntry Entry =
3711 Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
Joe Abbey97b7a172013-02-06 22:14:06 +00003712
Rafael Espindolac6afe0d2015-06-16 20:03:39 +00003713 if (Entry.Kind != BitstreamEntry::SubBlock)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003714 return error("Malformed block");
Joe Abbey97b7a172013-02-06 22:14:06 +00003715
Mehdi Amini5d303282015-10-26 18:37:00 +00003716 if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) {
3717 parseBitcodeVersion();
3718 continue;
3719 }
3720
Rafael Espindolac6afe0d2015-06-16 20:03:39 +00003721 if (Entry.ID == bitc::MODULE_BLOCK_ID)
Teresa Johnson1493ad92015-10-10 14:18:36 +00003722 return parseModule(0, ShouldLazyLoadMetadata);
Joe Abbey97b7a172013-02-06 22:14:06 +00003723
Rafael Espindolac6afe0d2015-06-16 20:03:39 +00003724 if (Stream.SkipBlock())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003725 return error("Invalid record");
Chris Lattner1314b992007-04-22 06:23:29 +00003726 }
Chris Lattner1314b992007-04-22 06:23:29 +00003727}
Chris Lattner6694f602007-04-29 07:54:31 +00003728
Rafael Espindolac75c4fa2014-07-04 20:02:42 +00003729ErrorOr<std::string> BitcodeReader::parseModuleTriple() {
Bill Wendling0198ce02010-10-06 01:22:42 +00003730 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003731 return error("Invalid record");
Bill Wendling0198ce02010-10-06 01:22:42 +00003732
3733 SmallVector<uint64_t, 64> Record;
3734
Rafael Espindolac75c4fa2014-07-04 20:02:42 +00003735 std::string Triple;
Bill Wendling0198ce02010-10-06 01:22:42 +00003736 // Read all the records for this module.
Chris Lattner27d38752013-01-20 02:13:19 +00003737 while (1) {
3738 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00003739
Chris Lattner27d38752013-01-20 02:13:19 +00003740 switch (Entry.Kind) {
3741 case BitstreamEntry::SubBlock: // Handled for us already.
3742 case BitstreamEntry::Error:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003743 return error("Malformed block");
Chris Lattner27d38752013-01-20 02:13:19 +00003744 case BitstreamEntry::EndBlock:
Rafael Espindolae6107792014-07-04 20:05:56 +00003745 return Triple;
Chris Lattner27d38752013-01-20 02:13:19 +00003746 case BitstreamEntry::Record:
3747 // The interesting case.
3748 break;
Bill Wendling0198ce02010-10-06 01:22:42 +00003749 }
3750
3751 // Read a record.
Chris Lattner27d38752013-01-20 02:13:19 +00003752 switch (Stream.readRecord(Entry.ID, Record)) {
Bill Wendling0198ce02010-10-06 01:22:42 +00003753 default: break; // Default behavior, ignore unknown content.
Bill Wendling0198ce02010-10-06 01:22:42 +00003754 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
Rafael Espindolac75c4fa2014-07-04 20:02:42 +00003755 std::string S;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003756 if (convertToString(Record, 0, S))
3757 return error("Invalid record");
Rafael Espindolac75c4fa2014-07-04 20:02:42 +00003758 Triple = S;
Bill Wendling0198ce02010-10-06 01:22:42 +00003759 break;
3760 }
3761 }
3762 Record.clear();
3763 }
Rafael Espindolae6107792014-07-04 20:05:56 +00003764 llvm_unreachable("Exit infinite loop");
Bill Wendling0198ce02010-10-06 01:22:42 +00003765}
3766
Rafael Espindolac75c4fa2014-07-04 20:02:42 +00003767ErrorOr<std::string> BitcodeReader::parseTriple() {
Rafael Espindola1aabf982015-06-16 23:29:49 +00003768 if (std::error_code EC = initStream(nullptr))
Rafael Espindola48da4f42013-11-04 16:16:24 +00003769 return EC;
Bill Wendling0198ce02010-10-06 01:22:42 +00003770
3771 // Sniff for the signature.
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003772 if (!hasValidBitcodeHeader(Stream))
3773 return error("Invalid bitcode signature");
Bill Wendling0198ce02010-10-06 01:22:42 +00003774
3775 // We expect a number of well-defined blocks, though we don't necessarily
3776 // need to understand them all.
Chris Lattner27d38752013-01-20 02:13:19 +00003777 while (1) {
3778 BitstreamEntry Entry = Stream.advance();
Joe Abbey97b7a172013-02-06 22:14:06 +00003779
Chris Lattner27d38752013-01-20 02:13:19 +00003780 switch (Entry.Kind) {
3781 case BitstreamEntry::Error:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003782 return error("Malformed block");
Chris Lattner27d38752013-01-20 02:13:19 +00003783 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003784 return std::error_code();
Joe Abbey97b7a172013-02-06 22:14:06 +00003785
Chris Lattner27d38752013-01-20 02:13:19 +00003786 case BitstreamEntry::SubBlock:
3787 if (Entry.ID == bitc::MODULE_BLOCK_ID)
Rafael Espindolad346cc82014-07-04 13:52:01 +00003788 return parseModuleTriple();
Joe Abbey97b7a172013-02-06 22:14:06 +00003789
Chris Lattner27d38752013-01-20 02:13:19 +00003790 // Ignore other sub-blocks.
Rafael Espindola48da4f42013-11-04 16:16:24 +00003791 if (Stream.SkipBlock())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003792 return error("Malformed block");
Chris Lattner27d38752013-01-20 02:13:19 +00003793 continue;
Joe Abbey97b7a172013-02-06 22:14:06 +00003794
Chris Lattner27d38752013-01-20 02:13:19 +00003795 case BitstreamEntry::Record:
3796 Stream.skipRecord(Entry.ID);
3797 continue;
Bill Wendling0198ce02010-10-06 01:22:42 +00003798 }
3799 }
Bill Wendling0198ce02010-10-06 01:22:42 +00003800}
3801
Mehdi Amini3383ccc2015-11-09 02:46:41 +00003802ErrorOr<std::string> BitcodeReader::parseIdentificationBlock() {
3803 if (std::error_code EC = initStream(nullptr))
3804 return EC;
3805
3806 // Sniff for the signature.
3807 if (!hasValidBitcodeHeader(Stream))
3808 return error("Invalid bitcode signature");
3809
3810 // We expect a number of well-defined blocks, though we don't necessarily
3811 // need to understand them all.
3812 while (1) {
3813 BitstreamEntry Entry = Stream.advance();
3814 switch (Entry.Kind) {
3815 case BitstreamEntry::Error:
3816 return error("Malformed block");
3817 case BitstreamEntry::EndBlock:
3818 return std::error_code();
3819
3820 case BitstreamEntry::SubBlock:
3821 if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) {
3822 if (std::error_code EC = parseBitcodeVersion())
3823 return EC;
3824 return ProducerIdentification;
3825 }
3826 // Ignore other sub-blocks.
3827 if (Stream.SkipBlock())
3828 return error("Malformed block");
3829 continue;
3830 case BitstreamEntry::Record:
3831 Stream.skipRecord(Entry.ID);
3832 continue;
3833 }
3834 }
3835}
3836
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003837/// Parse metadata attachments.
3838std::error_code BitcodeReader::parseMetadataAttachment(Function &F) {
Devang Patelaf206b82009-09-18 19:26:43 +00003839 if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003840 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003841
Devang Patelaf206b82009-09-18 19:26:43 +00003842 SmallVector<uint64_t, 64> Record;
Chris Lattner27d38752013-01-20 02:13:19 +00003843 while (1) {
3844 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00003845
Chris Lattner27d38752013-01-20 02:13:19 +00003846 switch (Entry.Kind) {
3847 case BitstreamEntry::SubBlock: // Handled for us already.
3848 case BitstreamEntry::Error:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003849 return error("Malformed block");
Chris Lattner27d38752013-01-20 02:13:19 +00003850 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003851 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00003852 case BitstreamEntry::Record:
3853 // The interesting case.
Devang Patelaf206b82009-09-18 19:26:43 +00003854 break;
3855 }
Chris Lattner27d38752013-01-20 02:13:19 +00003856
Devang Patelaf206b82009-09-18 19:26:43 +00003857 // Read a metadata attachment record.
3858 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +00003859 switch (Stream.readRecord(Entry.ID, Record)) {
Devang Patelaf206b82009-09-18 19:26:43 +00003860 default: // Default behavior: ignore.
3861 break;
Chris Lattnerb8778552011-06-17 17:50:30 +00003862 case bitc::METADATA_ATTACHMENT: {
Devang Patelaf206b82009-09-18 19:26:43 +00003863 unsigned RecordLength = Record.size();
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00003864 if (Record.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003865 return error("Invalid record");
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00003866 if (RecordLength % 2 == 0) {
3867 // A function attachment.
3868 for (unsigned I = 0; I != RecordLength; I += 2) {
3869 auto K = MDKindMap.find(Record[I]);
3870 if (K == MDKindMap.end())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003871 return error("Invalid ID");
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00003872 Metadata *MD = MDValueList.getValueFwdRef(Record[I + 1]);
3873 F.setMetadata(K->second, cast<MDNode>(MD));
3874 }
3875 continue;
3876 }
3877
3878 // An instruction attachment.
Devang Patelaf206b82009-09-18 19:26:43 +00003879 Instruction *Inst = InstructionList[Record[0]];
3880 for (unsigned i = 1; i != RecordLength; i = i+2) {
Devang Patelb1a44772009-09-28 21:14:55 +00003881 unsigned Kind = Record[i];
Dan Gohman43aa8f02010-07-20 21:42:28 +00003882 DenseMap<unsigned, unsigned>::iterator I =
3883 MDKindMap.find(Kind);
3884 if (I == MDKindMap.end())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003885 return error("Invalid ID");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00003886 Metadata *Node = MDValueList.getValueFwdRef(Record[i + 1]);
3887 if (isa<LocalAsMetadata>(Node))
Duncan P. N. Exon Smith35303fd2014-12-06 02:29:44 +00003888 // Drop the attachment. This used to be legal, but there's no
3889 // upgrade path.
3890 break;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00003891 Inst->setMetadata(I->second, cast<MDNode>(Node));
Manman Ren209b17c2013-09-28 00:22:27 +00003892 if (I->second == LLVMContext::MD_tbaa)
3893 InstsWithTBAATag.push_back(Inst);
Devang Patelaf206b82009-09-18 19:26:43 +00003894 }
3895 break;
3896 }
3897 }
3898 }
Devang Patelaf206b82009-09-18 19:26:43 +00003899}
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003900
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003901static std::error_code typeCheckLoadStoreInst(DiagnosticHandlerFunction DH,
Filipe Cabecinhas11bb8492015-05-18 21:48:55 +00003902 Type *ValType, Type *PtrType) {
3903 if (!isa<PointerType>(PtrType))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003904 return error(DH, "Load/Store operand is not a pointer type");
Filipe Cabecinhas11bb8492015-05-18 21:48:55 +00003905 Type *ElemType = cast<PointerType>(PtrType)->getElementType();
3906
3907 if (ValType && ValType != ElemType)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003908 return error(DH, "Explicit load/store type does not match pointee type of "
Filipe Cabecinhas11bb8492015-05-18 21:48:55 +00003909 "pointer operand");
3910 if (!PointerType::isLoadableOrStorableType(ElemType))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003911 return error(DH, "Cannot load/store from pointer");
Filipe Cabecinhas11bb8492015-05-18 21:48:55 +00003912 return std::error_code();
3913}
3914
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003915/// Lazily parse the specified function body block.
3916std::error_code BitcodeReader::parseFunctionBody(Function *F) {
Chris Lattner982ec1e2007-05-05 00:17:00 +00003917 if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003918 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003919
Nick Lewyckya72e1af2010-02-25 08:30:17 +00003920 InstructionList.clear();
Chris Lattner85b7b402007-05-01 05:52:21 +00003921 unsigned ModuleValueListSize = ValueList.size();
Dan Gohman26d837d2010-08-25 20:22:53 +00003922 unsigned ModuleMDValueListSize = MDValueList.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003923
Chris Lattner85b7b402007-05-01 05:52:21 +00003924 // Add all the function arguments to the value table.
Duncan P. N. Exon Smithfb1743a32015-10-13 16:48:55 +00003925 for (Argument &I : F->args())
3926 ValueList.push_back(&I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003927
Chris Lattner83930552007-05-01 07:01:57 +00003928 unsigned NextValueNo = ValueList.size();
Craig Topper2617dcc2014-04-15 06:32:26 +00003929 BasicBlock *CurBB = nullptr;
Chris Lattnere53603e2007-05-02 04:27:25 +00003930 unsigned CurBBNo = 0;
3931
Chris Lattner07d09ed2010-04-03 02:17:50 +00003932 DebugLoc LastLoc;
Duncan P. N. Exon Smith52d0f162015-01-09 02:51:45 +00003933 auto getLastInstruction = [&]() -> Instruction * {
3934 if (CurBB && !CurBB->empty())
3935 return &CurBB->back();
3936 else if (CurBBNo && FunctionBBs[CurBBNo - 1] &&
3937 !FunctionBBs[CurBBNo - 1]->empty())
3938 return &FunctionBBs[CurBBNo - 1]->back();
3939 return nullptr;
3940 };
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003941
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00003942 std::vector<OperandBundleDef> OperandBundles;
3943
Chris Lattner85b7b402007-05-01 05:52:21 +00003944 // Read all the records.
3945 SmallVector<uint64_t, 64> Record;
3946 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00003947 BitstreamEntry Entry = Stream.advance();
Joe Abbey97b7a172013-02-06 22:14:06 +00003948
Chris Lattner27d38752013-01-20 02:13:19 +00003949 switch (Entry.Kind) {
3950 case BitstreamEntry::Error:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003951 return error("Malformed block");
Chris Lattner27d38752013-01-20 02:13:19 +00003952 case BitstreamEntry::EndBlock:
3953 goto OutOfRecordLoop;
Joe Abbey97b7a172013-02-06 22:14:06 +00003954
Chris Lattner27d38752013-01-20 02:13:19 +00003955 case BitstreamEntry::SubBlock:
3956 switch (Entry.ID) {
Chris Lattner85b7b402007-05-01 05:52:21 +00003957 default: // Skip unknown content.
3958 if (Stream.SkipBlock())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003959 return error("Invalid record");
Chris Lattner85b7b402007-05-01 05:52:21 +00003960 break;
3961 case bitc::CONSTANTS_BLOCK_ID:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003962 if (std::error_code EC = parseConstants())
Rafael Espindola48da4f42013-11-04 16:16:24 +00003963 return EC;
Chris Lattner83930552007-05-01 07:01:57 +00003964 NextValueNo = ValueList.size();
Chris Lattner85b7b402007-05-01 05:52:21 +00003965 break;
3966 case bitc::VALUE_SYMTAB_BLOCK_ID:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003967 if (std::error_code EC = parseValueSymbolTable())
Rafael Espindola48da4f42013-11-04 16:16:24 +00003968 return EC;
Chris Lattner85b7b402007-05-01 05:52:21 +00003969 break;
Devang Patelaf206b82009-09-18 19:26:43 +00003970 case bitc::METADATA_ATTACHMENT_ID:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003971 if (std::error_code EC = parseMetadataAttachment(*F))
Rafael Espindola48da4f42013-11-04 16:16:24 +00003972 return EC;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003973 break;
Victor Hernandez108d3ac2010-01-13 19:34:08 +00003974 case bitc::METADATA_BLOCK_ID:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003975 if (std::error_code EC = parseMetadata())
Rafael Espindola48da4f42013-11-04 16:16:24 +00003976 return EC;
Victor Hernandez108d3ac2010-01-13 19:34:08 +00003977 break;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00003978 case bitc::USELIST_BLOCK_ID:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003979 if (std::error_code EC = parseUseLists())
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00003980 return EC;
3981 break;
Chris Lattner85b7b402007-05-01 05:52:21 +00003982 }
3983 continue;
Joe Abbey97b7a172013-02-06 22:14:06 +00003984
Chris Lattner27d38752013-01-20 02:13:19 +00003985 case BitstreamEntry::Record:
3986 // The interesting case.
3987 break;
Chris Lattner85b7b402007-05-01 05:52:21 +00003988 }
Joe Abbey97b7a172013-02-06 22:14:06 +00003989
Chris Lattner85b7b402007-05-01 05:52:21 +00003990 // Read a record.
3991 Record.clear();
Craig Topper2617dcc2014-04-15 06:32:26 +00003992 Instruction *I = nullptr;
Chris Lattner27d38752013-01-20 02:13:19 +00003993 unsigned BitCode = Stream.readRecord(Entry.ID, Record);
Dan Gohman0ebd6962009-07-20 21:19:07 +00003994 switch (BitCode) {
Chris Lattner83930552007-05-01 07:01:57 +00003995 default: // Default behavior: reject
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003996 return error("Invalid value");
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00003997 case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks]
Chris Lattner83930552007-05-01 07:01:57 +00003998 if (Record.size() < 1 || Record[0] == 0)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003999 return error("Invalid record");
Chris Lattner85b7b402007-05-01 05:52:21 +00004000 // Create all the basic blocks for the function.
Chris Lattner6ce15cb2007-05-03 22:09:51 +00004001 FunctionBBs.resize(Record[0]);
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00004002
4003 // See if anything took the address of blocks in this function.
4004 auto BBFRI = BasicBlockFwdRefs.find(F);
4005 if (BBFRI == BasicBlockFwdRefs.end()) {
4006 for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i)
4007 FunctionBBs[i] = BasicBlock::Create(Context, "", F);
4008 } else {
4009 auto &BBRefs = BBFRI->second;
Duncan P. N. Exon Smith5a5fd7b2014-08-16 01:54:37 +00004010 // Check for invalid basic block references.
4011 if (BBRefs.size() > FunctionBBs.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004012 return error("Invalid ID");
Duncan P. N. Exon Smith5a5fd7b2014-08-16 01:54:37 +00004013 assert(!BBRefs.empty() && "Unexpected empty array");
4014 assert(!BBRefs.front() && "Invalid reference to entry block");
4015 for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E;
4016 ++I)
4017 if (I < RE && BBRefs[I]) {
4018 BBRefs[I]->insertInto(F);
4019 FunctionBBs[I] = BBRefs[I];
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00004020 } else {
4021 FunctionBBs[I] = BasicBlock::Create(Context, "", F);
4022 }
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00004023
4024 // Erase from the table.
4025 BasicBlockFwdRefs.erase(BBFRI);
4026 }
4027
Chris Lattner83930552007-05-01 07:01:57 +00004028 CurBB = FunctionBBs[0];
4029 continue;
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00004030 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004031
Chris Lattner07d09ed2010-04-03 02:17:50 +00004032 case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN
4033 // This record indicates that the last instruction is at the same
4034 // location as the previous instruction with a location.
Duncan P. N. Exon Smith52d0f162015-01-09 02:51:45 +00004035 I = getLastInstruction();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004036
Craig Topper2617dcc2014-04-15 06:32:26 +00004037 if (!I)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004038 return error("Invalid record");
Chris Lattner07d09ed2010-04-03 02:17:50 +00004039 I->setDebugLoc(LastLoc);
Craig Topper2617dcc2014-04-15 06:32:26 +00004040 I = nullptr;
Chris Lattner07d09ed2010-04-03 02:17:50 +00004041 continue;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004042
Duncan P. N. Exon Smith9ed19662015-01-09 17:53:27 +00004043 case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia]
Duncan P. N. Exon Smith52d0f162015-01-09 02:51:45 +00004044 I = getLastInstruction();
Craig Topper2617dcc2014-04-15 06:32:26 +00004045 if (!I || Record.size() < 4)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004046 return error("Invalid record");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004047
Chris Lattner07d09ed2010-04-03 02:17:50 +00004048 unsigned Line = Record[0], Col = Record[1];
4049 unsigned ScopeID = Record[2], IAID = Record[3];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004050
Craig Topper2617dcc2014-04-15 06:32:26 +00004051 MDNode *Scope = nullptr, *IA = nullptr;
Chris Lattner07d09ed2010-04-03 02:17:50 +00004052 if (ScopeID) Scope = cast<MDNode>(MDValueList.getValueFwdRef(ScopeID-1));
4053 if (IAID) IA = cast<MDNode>(MDValueList.getValueFwdRef(IAID-1));
4054 LastLoc = DebugLoc::get(Line, Col, Scope, IA);
4055 I->setDebugLoc(LastLoc);
Craig Topper2617dcc2014-04-15 06:32:26 +00004056 I = nullptr;
Chris Lattner07d09ed2010-04-03 02:17:50 +00004057 continue;
4058 }
4059
Chris Lattnere9759c22007-05-06 00:21:25 +00004060 case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode]
4061 unsigned OpNum = 0;
4062 Value *LHS, *RHS;
4063 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00004064 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
Dan Gohman0ebd6962009-07-20 21:19:07 +00004065 OpNum+1 > Record.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004066 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004067
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004068 int Opc = getDecodedBinaryOpcode(Record[OpNum++], LHS->getType());
Rafael Espindola48da4f42013-11-04 16:16:24 +00004069 if (Opc == -1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004070 return error("Invalid record");
Gabor Greife1f6e4b2008-05-16 19:29:10 +00004071 I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
Devang Patelaf206b82009-09-18 19:26:43 +00004072 InstructionList.push_back(I);
Dan Gohman1b849082009-09-07 23:54:19 +00004073 if (OpNum < Record.size()) {
4074 if (Opc == Instruction::Add ||
4075 Opc == Instruction::Sub ||
Chris Lattnera676c0f2011-02-07 16:40:21 +00004076 Opc == Instruction::Mul ||
4077 Opc == Instruction::Shl) {
Dan Gohman00f47472010-01-25 21:55:39 +00004078 if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP))
Dan Gohman1b849082009-09-07 23:54:19 +00004079 cast<BinaryOperator>(I)->setHasNoSignedWrap(true);
Dan Gohman00f47472010-01-25 21:55:39 +00004080 if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
Dan Gohman1b849082009-09-07 23:54:19 +00004081 cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true);
Chris Lattner35315d02011-02-06 21:44:57 +00004082 } else if (Opc == Instruction::SDiv ||
Chris Lattnera676c0f2011-02-07 16:40:21 +00004083 Opc == Instruction::UDiv ||
4084 Opc == Instruction::LShr ||
4085 Opc == Instruction::AShr) {
Chris Lattner35315d02011-02-06 21:44:57 +00004086 if (Record[OpNum] & (1 << bitc::PEO_EXACT))
Dan Gohman1b849082009-09-07 23:54:19 +00004087 cast<BinaryOperator>(I)->setIsExact(true);
Michael Ilseman9978d7e2012-11-27 00:43:38 +00004088 } else if (isa<FPMathOperator>(I)) {
James Molloy88eb5352015-07-10 12:52:00 +00004089 FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]);
Michael Ilseman9978d7e2012-11-27 00:43:38 +00004090 if (FMF.any())
4091 I->setFastMathFlags(FMF);
Dan Gohman1b849082009-09-07 23:54:19 +00004092 }
Michael Ilseman9978d7e2012-11-27 00:43:38 +00004093
Dan Gohman1b849082009-09-07 23:54:19 +00004094 }
Chris Lattner85b7b402007-05-01 05:52:21 +00004095 break;
4096 }
Chris Lattnere9759c22007-05-06 00:21:25 +00004097 case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc]
4098 unsigned OpNum = 0;
4099 Value *Op;
4100 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
4101 OpNum+2 != Record.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004102 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004103
Chris Lattner229907c2011-07-18 04:54:35 +00004104 Type *ResTy = getTypeByID(Record[OpNum]);
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004105 int Opc = getDecodedCastOpcode(Record[OpNum + 1]);
Craig Topper2617dcc2014-04-15 06:32:26 +00004106 if (Opc == -1 || !ResTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004107 return error("Invalid record");
Craig Topper2617dcc2014-04-15 06:32:26 +00004108 Instruction *Temp = nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00004109 if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) {
4110 if (Temp) {
4111 InstructionList.push_back(Temp);
4112 CurBB->getInstList().push_back(Temp);
4113 }
4114 } else {
Filipe Cabecinhasb70fd872015-10-06 12:37:54 +00004115 auto CastOp = (Instruction::CastOps)Opc;
4116 if (!CastInst::castIsValid(CastOp, Op, ResTy))
4117 return error("Invalid cast");
4118 I = CastInst::Create(CastOp, Op, ResTy);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00004119 }
Devang Patelaf206b82009-09-18 19:26:43 +00004120 InstructionList.push_back(I);
Chris Lattnere53603e2007-05-02 04:27:25 +00004121 break;
4122 }
David Blaikieb5b5efd2015-02-25 01:08:52 +00004123 case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD:
4124 case bitc::FUNC_CODE_INST_GEP_OLD:
4125 case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands]
Chris Lattnerdf1233d2007-05-06 00:00:00 +00004126 unsigned OpNum = 0;
David Blaikieb5b5efd2015-02-25 01:08:52 +00004127
4128 Type *Ty;
4129 bool InBounds;
4130
4131 if (BitCode == bitc::FUNC_CODE_INST_GEP) {
4132 InBounds = Record[OpNum++];
4133 Ty = getTypeByID(Record[OpNum++]);
4134 } else {
4135 InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD;
4136 Ty = nullptr;
4137 }
4138
Chris Lattnerdf1233d2007-05-06 00:00:00 +00004139 Value *BasePtr;
4140 if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004141 return error("Invalid record");
Chris Lattner1fc27f02007-05-02 05:16:49 +00004142
David Blaikie60310f22015-05-08 00:42:26 +00004143 if (!Ty)
4144 Ty = cast<SequentialType>(BasePtr->getType()->getScalarType())
4145 ->getElementType();
4146 else if (Ty !=
4147 cast<SequentialType>(BasePtr->getType()->getScalarType())
4148 ->getElementType())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004149 return error(
David Blaikie675e8cb2015-03-16 21:35:48 +00004150 "Explicit gep type does not match pointee type of pointer operand");
4151
Chris Lattner5285b5e2007-05-02 05:46:45 +00004152 SmallVector<Value*, 16> GEPIdx;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00004153 while (OpNum != Record.size()) {
4154 Value *Op;
4155 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004156 return error("Invalid record");
Chris Lattnerdf1233d2007-05-06 00:00:00 +00004157 GEPIdx.push_back(Op);
Chris Lattner1fc27f02007-05-02 05:16:49 +00004158 }
4159
David Blaikie096b1da2015-03-14 19:53:33 +00004160 I = GetElementPtrInst::Create(Ty, BasePtr, GEPIdx);
David Blaikie675e8cb2015-03-16 21:35:48 +00004161
Devang Patelaf206b82009-09-18 19:26:43 +00004162 InstructionList.push_back(I);
David Blaikieb5b5efd2015-02-25 01:08:52 +00004163 if (InBounds)
Dan Gohman1b849082009-09-07 23:54:19 +00004164 cast<GetElementPtrInst>(I)->setIsInBounds(true);
Chris Lattner1fc27f02007-05-02 05:16:49 +00004165 break;
4166 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004167
Dan Gohman1ecaf452008-05-31 00:58:22 +00004168 case bitc::FUNC_CODE_INST_EXTRACTVAL: {
4169 // EXTRACTVAL: [opty, opval, n x indices]
Dan Gohman30499842008-05-23 01:55:30 +00004170 unsigned OpNum = 0;
4171 Value *Agg;
4172 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004173 return error("Invalid record");
Dan Gohman30499842008-05-23 01:55:30 +00004174
Filipe Cabecinhas1c299d02015-05-16 00:33:12 +00004175 unsigned RecSize = Record.size();
4176 if (OpNum == RecSize)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004177 return error("EXTRACTVAL: Invalid instruction with 0 indices");
Filipe Cabecinhas1c299d02015-05-16 00:33:12 +00004178
Dan Gohman1ecaf452008-05-31 00:58:22 +00004179 SmallVector<unsigned, 4> EXTRACTVALIdx;
Filipe Cabecinhasecf8f7f2015-02-16 00:03:11 +00004180 Type *CurTy = Agg->getType();
Filipe Cabecinhas1c299d02015-05-16 00:33:12 +00004181 for (; OpNum != RecSize; ++OpNum) {
Filipe Cabecinhasecf8f7f2015-02-16 00:03:11 +00004182 bool IsArray = CurTy->isArrayTy();
4183 bool IsStruct = CurTy->isStructTy();
Dan Gohman1ecaf452008-05-31 00:58:22 +00004184 uint64_t Index = Record[OpNum];
Filipe Cabecinhasecf8f7f2015-02-16 00:03:11 +00004185
4186 if (!IsStruct && !IsArray)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004187 return error("EXTRACTVAL: Invalid type");
Dan Gohman1ecaf452008-05-31 00:58:22 +00004188 if ((unsigned)Index != Index)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004189 return error("Invalid value");
Filipe Cabecinhasecf8f7f2015-02-16 00:03:11 +00004190 if (IsStruct && Index >= CurTy->subtypes().size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004191 return error("EXTRACTVAL: Invalid struct index");
Filipe Cabecinhasecf8f7f2015-02-16 00:03:11 +00004192 if (IsArray && Index >= CurTy->getArrayNumElements())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004193 return error("EXTRACTVAL: Invalid array index");
Dan Gohman1ecaf452008-05-31 00:58:22 +00004194 EXTRACTVALIdx.push_back((unsigned)Index);
Filipe Cabecinhasecf8f7f2015-02-16 00:03:11 +00004195
4196 if (IsStruct)
4197 CurTy = CurTy->subtypes()[Index];
4198 else
4199 CurTy = CurTy->subtypes()[0];
Dan Gohman30499842008-05-23 01:55:30 +00004200 }
4201
Jay Foad57aa6362011-07-13 10:26:04 +00004202 I = ExtractValueInst::Create(Agg, EXTRACTVALIdx);
Devang Patelaf206b82009-09-18 19:26:43 +00004203 InstructionList.push_back(I);
Dan Gohman30499842008-05-23 01:55:30 +00004204 break;
4205 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004206
Dan Gohman1ecaf452008-05-31 00:58:22 +00004207 case bitc::FUNC_CODE_INST_INSERTVAL: {
4208 // INSERTVAL: [opty, opval, opty, opval, n x indices]
Dan Gohman30499842008-05-23 01:55:30 +00004209 unsigned OpNum = 0;
4210 Value *Agg;
4211 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004212 return error("Invalid record");
Dan Gohman30499842008-05-23 01:55:30 +00004213 Value *Val;
4214 if (getValueTypePair(Record, OpNum, NextValueNo, Val))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004215 return error("Invalid record");
Dan Gohman30499842008-05-23 01:55:30 +00004216
Filipe Cabecinhas1c299d02015-05-16 00:33:12 +00004217 unsigned RecSize = Record.size();
4218 if (OpNum == RecSize)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004219 return error("INSERTVAL: Invalid instruction with 0 indices");
Filipe Cabecinhas1c299d02015-05-16 00:33:12 +00004220
Dan Gohman1ecaf452008-05-31 00:58:22 +00004221 SmallVector<unsigned, 4> INSERTVALIdx;
Filipe Cabecinhasecf8f7f2015-02-16 00:03:11 +00004222 Type *CurTy = Agg->getType();
Filipe Cabecinhas1c299d02015-05-16 00:33:12 +00004223 for (; OpNum != RecSize; ++OpNum) {
Filipe Cabecinhasecf8f7f2015-02-16 00:03:11 +00004224 bool IsArray = CurTy->isArrayTy();
4225 bool IsStruct = CurTy->isStructTy();
Dan Gohman1ecaf452008-05-31 00:58:22 +00004226 uint64_t Index = Record[OpNum];
Filipe Cabecinhasecf8f7f2015-02-16 00:03:11 +00004227
4228 if (!IsStruct && !IsArray)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004229 return error("INSERTVAL: Invalid type");
Dan Gohman1ecaf452008-05-31 00:58:22 +00004230 if ((unsigned)Index != Index)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004231 return error("Invalid value");
Filipe Cabecinhasecf8f7f2015-02-16 00:03:11 +00004232 if (IsStruct && Index >= CurTy->subtypes().size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004233 return error("INSERTVAL: Invalid struct index");
Filipe Cabecinhasecf8f7f2015-02-16 00:03:11 +00004234 if (IsArray && Index >= CurTy->getArrayNumElements())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004235 return error("INSERTVAL: Invalid array index");
Filipe Cabecinhasecf8f7f2015-02-16 00:03:11 +00004236
Dan Gohman1ecaf452008-05-31 00:58:22 +00004237 INSERTVALIdx.push_back((unsigned)Index);
Filipe Cabecinhasecf8f7f2015-02-16 00:03:11 +00004238 if (IsStruct)
4239 CurTy = CurTy->subtypes()[Index];
4240 else
4241 CurTy = CurTy->subtypes()[0];
Dan Gohman30499842008-05-23 01:55:30 +00004242 }
4243
Filipe Cabecinhas4708a022015-05-18 22:27:11 +00004244 if (CurTy != Val->getType())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004245 return error("Inserted value type doesn't match aggregate type");
Filipe Cabecinhas4708a022015-05-18 22:27:11 +00004246
Jay Foad57aa6362011-07-13 10:26:04 +00004247 I = InsertValueInst::Create(Agg, Val, INSERTVALIdx);
Devang Patelaf206b82009-09-18 19:26:43 +00004248 InstructionList.push_back(I);
Dan Gohman30499842008-05-23 01:55:30 +00004249 break;
4250 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004251
Chris Lattnere9759c22007-05-06 00:21:25 +00004252 case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval]
Dan Gohmanc5d28922008-09-16 01:01:33 +00004253 // obsolete form of select
4254 // handles select i1 ... in old bitcode
Chris Lattnere9759c22007-05-06 00:21:25 +00004255 unsigned OpNum = 0;
4256 Value *TrueVal, *FalseVal, *Cond;
4257 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00004258 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
4259 popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004260 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004261
Dan Gohmanc5d28922008-09-16 01:01:33 +00004262 I = SelectInst::Create(Cond, TrueVal, FalseVal);
Devang Patelaf206b82009-09-18 19:26:43 +00004263 InstructionList.push_back(I);
Dan Gohmanc5d28922008-09-16 01:01:33 +00004264 break;
4265 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004266
Dan Gohmanc5d28922008-09-16 01:01:33 +00004267 case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred]
4268 // new form of select
4269 // handles select i1 or select [N x i1]
4270 unsigned OpNum = 0;
4271 Value *TrueVal, *FalseVal, *Cond;
4272 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00004273 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
Dan Gohmanc5d28922008-09-16 01:01:33 +00004274 getValueTypePair(Record, OpNum, NextValueNo, Cond))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004275 return error("Invalid record");
Dan Gohmanc579d972008-09-09 01:02:47 +00004276
4277 // select condition can be either i1 or [N x i1]
Chris Lattner229907c2011-07-18 04:54:35 +00004278 if (VectorType* vector_type =
4279 dyn_cast<VectorType>(Cond->getType())) {
Dan Gohmanc579d972008-09-09 01:02:47 +00004280 // expect <n x i1>
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004281 if (vector_type->getElementType() != Type::getInt1Ty(Context))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004282 return error("Invalid type for value");
Dan Gohmanc579d972008-09-09 01:02:47 +00004283 } else {
4284 // expect i1
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004285 if (Cond->getType() != Type::getInt1Ty(Context))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004286 return error("Invalid type for value");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004287 }
4288
Gabor Greife9ecc682008-04-06 20:25:17 +00004289 I = SelectInst::Create(Cond, TrueVal, FalseVal);
Devang Patelaf206b82009-09-18 19:26:43 +00004290 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00004291 break;
4292 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004293
Chris Lattner1fc27f02007-05-02 05:16:49 +00004294 case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval]
Chris Lattnere9759c22007-05-06 00:21:25 +00004295 unsigned OpNum = 0;
4296 Value *Vec, *Idx;
4297 if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00004298 getValueTypePair(Record, OpNum, NextValueNo, Idx))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004299 return error("Invalid record");
Filipe Cabecinhasff1e2342015-04-24 11:30:15 +00004300 if (!Vec->getType()->isVectorTy())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004301 return error("Invalid type for value");
Eric Christopherc9742252009-07-25 02:28:41 +00004302 I = ExtractElementInst::Create(Vec, Idx);
Devang Patelaf206b82009-09-18 19:26:43 +00004303 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00004304 break;
4305 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004306
Chris Lattner1fc27f02007-05-02 05:16:49 +00004307 case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval]
Chris Lattnere9759c22007-05-06 00:21:25 +00004308 unsigned OpNum = 0;
4309 Value *Vec, *Elt, *Idx;
Filipe Cabecinhasff1e2342015-04-24 11:30:15 +00004310 if (getValueTypePair(Record, OpNum, NextValueNo, Vec))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004311 return error("Invalid record");
Filipe Cabecinhasff1e2342015-04-24 11:30:15 +00004312 if (!Vec->getType()->isVectorTy())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004313 return error("Invalid type for value");
Filipe Cabecinhasff1e2342015-04-24 11:30:15 +00004314 if (popValue(Record, OpNum, NextValueNo,
Chris Lattnere9759c22007-05-06 00:21:25 +00004315 cast<VectorType>(Vec->getType())->getElementType(), Elt) ||
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00004316 getValueTypePair(Record, OpNum, NextValueNo, Idx))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004317 return error("Invalid record");
Gabor Greife9ecc682008-04-06 20:25:17 +00004318 I = InsertElementInst::Create(Vec, Elt, Idx);
Devang Patelaf206b82009-09-18 19:26:43 +00004319 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00004320 break;
4321 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004322
Chris Lattnere9759c22007-05-06 00:21:25 +00004323 case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval]
4324 unsigned OpNum = 0;
4325 Value *Vec1, *Vec2, *Mask;
4326 if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00004327 popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004328 return error("Invalid record");
Chris Lattnere9759c22007-05-06 00:21:25 +00004329
Mon P Wang25f01062008-11-10 04:46:22 +00004330 if (getValueTypePair(Record, OpNum, NextValueNo, Mask))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004331 return error("Invalid record");
Filipe Cabecinhasff1e2342015-04-24 11:30:15 +00004332 if (!Vec1->getType()->isVectorTy() || !Vec2->getType()->isVectorTy())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004333 return error("Invalid type for value");
Chris Lattner1fc27f02007-05-02 05:16:49 +00004334 I = new ShuffleVectorInst(Vec1, Vec2, Mask);
Devang Patelaf206b82009-09-18 19:26:43 +00004335 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00004336 break;
4337 }
Mon P Wang25f01062008-11-10 04:46:22 +00004338
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004339 case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred]
4340 // Old form of ICmp/FCmp returning bool
4341 // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were
4342 // both legal on vectors but had different behaviour.
4343 case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred]
4344 // FCmp/ICmp returning bool or vector of bool
4345
Chris Lattnerdf1233d2007-05-06 00:00:00 +00004346 unsigned OpNum = 0;
4347 Value *LHS, *RHS;
4348 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
James Molloy88eb5352015-07-10 12:52:00 +00004349 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS))
4350 return error("Invalid record");
4351
4352 unsigned PredVal = Record[OpNum];
4353 bool IsFP = LHS->getType()->isFPOrFPVectorTy();
4354 FastMathFlags FMF;
4355 if (IsFP && Record.size() > OpNum+1)
4356 FMF = getDecodedFastMathFlags(Record[++OpNum]);
4357
4358 if (OpNum+1 != Record.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004359 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004360
Duncan Sands9dff9be2010-02-15 16:12:20 +00004361 if (LHS->getType()->isFPOrFPVectorTy())
James Molloy88eb5352015-07-10 12:52:00 +00004362 I = new FCmpInst((FCmpInst::Predicate)PredVal, LHS, RHS);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004363 else
James Molloy88eb5352015-07-10 12:52:00 +00004364 I = new ICmpInst((ICmpInst::Predicate)PredVal, LHS, RHS);
4365
4366 if (FMF.any())
4367 I->setFastMathFlags(FMF);
Devang Patelaf206b82009-09-18 19:26:43 +00004368 InstructionList.push_back(I);
Dan Gohmanc579d972008-09-09 01:02:47 +00004369 break;
4370 }
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004371
Chris Lattnere53603e2007-05-02 04:27:25 +00004372 case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>]
Devang Patelbbfd8742008-02-26 01:29:32 +00004373 {
4374 unsigned Size = Record.size();
4375 if (Size == 0) {
Owen Anderson55f1c092009-08-13 21:58:54 +00004376 I = ReturnInst::Create(Context);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004377 InstructionList.push_back(I);
Devang Patelbbfd8742008-02-26 01:29:32 +00004378 break;
Dan Gohmanfa1211f2008-07-23 00:34:11 +00004379 }
Devang Patelbbfd8742008-02-26 01:29:32 +00004380
Dan Gohmanfa1211f2008-07-23 00:34:11 +00004381 unsigned OpNum = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +00004382 Value *Op = nullptr;
Chris Lattnerf1c87102011-06-17 18:09:11 +00004383 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004384 return error("Invalid record");
Chris Lattnerf1c87102011-06-17 18:09:11 +00004385 if (OpNum != Record.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004386 return error("Invalid record");
Dan Gohmanfa1211f2008-07-23 00:34:11 +00004387
Chris Lattnerf1c87102011-06-17 18:09:11 +00004388 I = ReturnInst::Create(Context, Op);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004389 InstructionList.push_back(I);
Dan Gohmanfa1211f2008-07-23 00:34:11 +00004390 break;
Chris Lattnere53603e2007-05-02 04:27:25 +00004391 }
Chris Lattner5285b5e2007-05-02 05:46:45 +00004392 case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#]
Chris Lattner6ce15cb2007-05-03 22:09:51 +00004393 if (Record.size() != 1 && Record.size() != 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004394 return error("Invalid record");
Chris Lattner5285b5e2007-05-02 05:46:45 +00004395 BasicBlock *TrueDest = getBasicBlock(Record[0]);
Craig Topper2617dcc2014-04-15 06:32:26 +00004396 if (!TrueDest)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004397 return error("Invalid record");
Chris Lattner5285b5e2007-05-02 05:46:45 +00004398
Devang Patelaf206b82009-09-18 19:26:43 +00004399 if (Record.size() == 1) {
Gabor Greife9ecc682008-04-06 20:25:17 +00004400 I = BranchInst::Create(TrueDest);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004401 InstructionList.push_back(I);
Devang Patelaf206b82009-09-18 19:26:43 +00004402 }
Chris Lattner5285b5e2007-05-02 05:46:45 +00004403 else {
4404 BasicBlock *FalseDest = getBasicBlock(Record[1]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00004405 Value *Cond = getValue(Record, 2, NextValueNo,
4406 Type::getInt1Ty(Context));
Craig Topper2617dcc2014-04-15 06:32:26 +00004407 if (!FalseDest || !Cond)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004408 return error("Invalid record");
Gabor Greife9ecc682008-04-06 20:25:17 +00004409 I = BranchInst::Create(TrueDest, FalseDest, Cond);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004410 InstructionList.push_back(I);
Chris Lattner5285b5e2007-05-02 05:46:45 +00004411 }
4412 break;
4413 }
David Majnemerb01aa9f2015-08-23 19:22:31 +00004414 case bitc::FUNC_CODE_INST_CLEANUPRET: { // CLEANUPRET: [val] or [val,bb#]
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00004415 if (Record.size() != 1 && Record.size() != 2)
David Majnemer654e1302015-07-31 17:58:14 +00004416 return error("Invalid record");
4417 unsigned Idx = 0;
David Majnemer8a1c45d2015-12-12 05:38:55 +00004418 Value *CleanupPad =
4419 getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00004420 if (!CleanupPad)
David Majnemer654e1302015-07-31 17:58:14 +00004421 return error("Invalid record");
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00004422 BasicBlock *UnwindDest = nullptr;
4423 if (Record.size() == 2) {
David Majnemer654e1302015-07-31 17:58:14 +00004424 UnwindDest = getBasicBlock(Record[Idx++]);
4425 if (!UnwindDest)
4426 return error("Invalid record");
4427 }
4428
David Majnemer8a1c45d2015-12-12 05:38:55 +00004429 I = CleanupReturnInst::Create(CleanupPad, UnwindDest);
David Majnemer654e1302015-07-31 17:58:14 +00004430 InstructionList.push_back(I);
4431 break;
4432 }
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00004433 case bitc::FUNC_CODE_INST_CATCHRET: { // CATCHRET: [val,bb#]
4434 if (Record.size() != 2)
David Majnemer654e1302015-07-31 17:58:14 +00004435 return error("Invalid record");
David Majnemer0bc0eef2015-08-15 02:46:08 +00004436 unsigned Idx = 0;
David Majnemer8a1c45d2015-12-12 05:38:55 +00004437 Value *CatchPad =
4438 getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00004439 if (!CatchPad)
4440 return error("Invalid record");
David Majnemer0bc0eef2015-08-15 02:46:08 +00004441 BasicBlock *BB = getBasicBlock(Record[Idx++]);
David Majnemer654e1302015-07-31 17:58:14 +00004442 if (!BB)
4443 return error("Invalid record");
David Majnemer0bc0eef2015-08-15 02:46:08 +00004444
David Majnemer8a1c45d2015-12-12 05:38:55 +00004445 I = CatchReturnInst::Create(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +00004446 InstructionList.push_back(I);
4447 break;
4448 }
David Majnemer8a1c45d2015-12-12 05:38:55 +00004449 case bitc::FUNC_CODE_INST_CATCHSWITCH: { // CATCHSWITCH: [tok,num,(bb)*,bb?]
4450 // We must have, at minimum, the outer scope and the number of arguments.
4451 if (Record.size() < 2)
David Majnemer654e1302015-07-31 17:58:14 +00004452 return error("Invalid record");
4453
David Majnemer654e1302015-07-31 17:58:14 +00004454 unsigned Idx = 0;
David Majnemer8a1c45d2015-12-12 05:38:55 +00004455
4456 Value *ParentPad =
4457 getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
4458
4459 unsigned NumHandlers = Record[Idx++];
4460
4461 SmallVector<BasicBlock *, 2> Handlers;
4462 for (unsigned Op = 0; Op != NumHandlers; ++Op) {
4463 BasicBlock *BB = getBasicBlock(Record[Idx++]);
4464 if (!BB)
David Majnemer654e1302015-07-31 17:58:14 +00004465 return error("Invalid record");
David Majnemer8a1c45d2015-12-12 05:38:55 +00004466 Handlers.push_back(BB);
4467 }
4468
4469 BasicBlock *UnwindDest = nullptr;
4470 if (Idx + 1 == Record.size()) {
David Majnemer654e1302015-07-31 17:58:14 +00004471 UnwindDest = getBasicBlock(Record[Idx++]);
4472 if (!UnwindDest)
4473 return error("Invalid record");
4474 }
David Majnemer8a1c45d2015-12-12 05:38:55 +00004475
4476 if (Record.size() != Idx)
4477 return error("Invalid record");
4478
4479 auto *CatchSwitch =
4480 CatchSwitchInst::Create(ParentPad, UnwindDest, NumHandlers);
4481 for (BasicBlock *Handler : Handlers)
4482 CatchSwitch->addHandler(Handler);
4483 I = CatchSwitch;
4484 InstructionList.push_back(I);
4485 break;
4486 }
David Majnemer8a1c45d2015-12-12 05:38:55 +00004487 case bitc::FUNC_CODE_INST_CATCHPAD:
4488 case bitc::FUNC_CODE_INST_CLEANUPPAD: { // [tok,num,(ty,val)*]
4489 // We must have, at minimum, the outer scope and the number of arguments.
4490 if (Record.size() < 2)
David Majnemer654e1302015-07-31 17:58:14 +00004491 return error("Invalid record");
David Majnemer8a1c45d2015-12-12 05:38:55 +00004492
David Majnemer654e1302015-07-31 17:58:14 +00004493 unsigned Idx = 0;
David Majnemer8a1c45d2015-12-12 05:38:55 +00004494
4495 Value *ParentPad =
4496 getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
4497
David Majnemer654e1302015-07-31 17:58:14 +00004498 unsigned NumArgOperands = Record[Idx++];
David Majnemer8a1c45d2015-12-12 05:38:55 +00004499
David Majnemer654e1302015-07-31 17:58:14 +00004500 SmallVector<Value *, 2> Args;
4501 for (unsigned Op = 0; Op != NumArgOperands; ++Op) {
4502 Value *Val;
4503 if (getValueTypePair(Record, Idx, NextValueNo, Val))
4504 return error("Invalid record");
4505 Args.push_back(Val);
4506 }
David Majnemer8a1c45d2015-12-12 05:38:55 +00004507
David Majnemer654e1302015-07-31 17:58:14 +00004508 if (Record.size() != Idx)
4509 return error("Invalid record");
4510
David Majnemer8a1c45d2015-12-12 05:38:55 +00004511 if (BitCode == bitc::FUNC_CODE_INST_CLEANUPPAD)
4512 I = CleanupPadInst::Create(ParentPad, Args);
4513 else
4514 I = CatchPadInst::Create(ParentPad, Args);
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +00004515 InstructionList.push_back(I);
4516 break;
4517 }
Chris Lattner3ed871f2009-10-27 19:13:16 +00004518 case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...]
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004519 // Check magic
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004520 if ((Record[0] >> 16) == SWITCH_INST_MAGIC) {
Bob Wilsone4077362013-09-09 19:14:35 +00004521 // "New" SwitchInst format with case ranges. The changes to write this
4522 // format were reverted but we still recognize bitcode that uses it.
4523 // Hopefully someday we will have support for case ranges and can use
4524 // this format again.
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004525
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004526 Type *OpTy = getTypeByID(Record[1]);
4527 unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth();
4528
Jan Wen Voungafaced02012-10-11 20:20:40 +00004529 Value *Cond = getValue(Record, 2, NextValueNo, OpTy);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004530 BasicBlock *Default = getBasicBlock(Record[3]);
Craig Topper2617dcc2014-04-15 06:32:26 +00004531 if (!OpTy || !Cond || !Default)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004532 return error("Invalid record");
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004533
4534 unsigned NumCases = Record[4];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004535
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004536 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
4537 InstructionList.push_back(SI);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004538
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004539 unsigned CurIdx = 5;
4540 for (unsigned i = 0; i != NumCases; ++i) {
Bob Wilsone4077362013-09-09 19:14:35 +00004541 SmallVector<ConstantInt*, 1> CaseVals;
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004542 unsigned NumItems = Record[CurIdx++];
4543 for (unsigned ci = 0; ci != NumItems; ++ci) {
4544 bool isSingleNumber = Record[CurIdx++];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004545
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004546 APInt Low;
4547 unsigned ActiveWords = 1;
4548 if (ValueBitWidth > 64)
4549 ActiveWords = Record[CurIdx++];
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004550 Low = readWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
Benjamin Kramer9704ed02012-05-28 14:10:31 +00004551 ValueBitWidth);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004552 CurIdx += ActiveWords;
Stepan Dyatkovskiye3e19cb2012-05-28 12:39:09 +00004553
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004554 if (!isSingleNumber) {
4555 ActiveWords = 1;
4556 if (ValueBitWidth > 64)
4557 ActiveWords = Record[CurIdx++];
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004558 APInt High = readWideAPInt(
4559 makeArrayRef(&Record[CurIdx], ActiveWords), ValueBitWidth);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004560 CurIdx += ActiveWords;
Bob Wilsone4077362013-09-09 19:14:35 +00004561
4562 // FIXME: It is not clear whether values in the range should be
4563 // compared as signed or unsigned values. The partially
4564 // implemented changes that used this format in the past used
4565 // unsigned comparisons.
4566 for ( ; Low.ule(High); ++Low)
4567 CaseVals.push_back(ConstantInt::get(Context, Low));
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004568 } else
Bob Wilsone4077362013-09-09 19:14:35 +00004569 CaseVals.push_back(ConstantInt::get(Context, Low));
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004570 }
4571 BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]);
Bob Wilsone4077362013-09-09 19:14:35 +00004572 for (SmallVector<ConstantInt*, 1>::iterator cvi = CaseVals.begin(),
4573 cve = CaseVals.end(); cvi != cve; ++cvi)
4574 SI->addCase(*cvi, DestBB);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004575 }
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004576 I = SI;
4577 break;
4578 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004579
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004580 // Old SwitchInst format without case ranges.
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004581
Chris Lattner5285b5e2007-05-02 05:46:45 +00004582 if (Record.size() < 3 || (Record.size() & 1) == 0)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004583 return error("Invalid record");
Chris Lattner229907c2011-07-18 04:54:35 +00004584 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00004585 Value *Cond = getValue(Record, 1, NextValueNo, OpTy);
Chris Lattner5285b5e2007-05-02 05:46:45 +00004586 BasicBlock *Default = getBasicBlock(Record[2]);
Craig Topper2617dcc2014-04-15 06:32:26 +00004587 if (!OpTy || !Cond || !Default)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004588 return error("Invalid record");
Chris Lattner5285b5e2007-05-02 05:46:45 +00004589 unsigned NumCases = (Record.size()-3)/2;
Gabor Greife9ecc682008-04-06 20:25:17 +00004590 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
Devang Patelaf206b82009-09-18 19:26:43 +00004591 InstructionList.push_back(SI);
Chris Lattner5285b5e2007-05-02 05:46:45 +00004592 for (unsigned i = 0, e = NumCases; i != e; ++i) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004593 ConstantInt *CaseVal =
Chris Lattner5285b5e2007-05-02 05:46:45 +00004594 dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy));
4595 BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]);
Craig Topper2617dcc2014-04-15 06:32:26 +00004596 if (!CaseVal || !DestBB) {
Chris Lattner5285b5e2007-05-02 05:46:45 +00004597 delete SI;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004598 return error("Invalid record");
Chris Lattner5285b5e2007-05-02 05:46:45 +00004599 }
4600 SI->addCase(CaseVal, DestBB);
4601 }
4602 I = SI;
4603 break;
4604 }
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004605 case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...]
Chris Lattner3ed871f2009-10-27 19:13:16 +00004606 if (Record.size() < 2)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004607 return error("Invalid record");
Chris Lattner229907c2011-07-18 04:54:35 +00004608 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00004609 Value *Address = getValue(Record, 1, NextValueNo, OpTy);
Craig Topper2617dcc2014-04-15 06:32:26 +00004610 if (!OpTy || !Address)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004611 return error("Invalid record");
Chris Lattner3ed871f2009-10-27 19:13:16 +00004612 unsigned NumDests = Record.size()-2;
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004613 IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests);
Chris Lattner3ed871f2009-10-27 19:13:16 +00004614 InstructionList.push_back(IBI);
4615 for (unsigned i = 0, e = NumDests; i != e; ++i) {
4616 if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) {
4617 IBI->addDestination(DestBB);
4618 } else {
4619 delete IBI;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004620 return error("Invalid record");
Chris Lattner3ed871f2009-10-27 19:13:16 +00004621 }
4622 }
4623 I = IBI;
4624 break;
4625 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004626
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00004627 case bitc::FUNC_CODE_INST_INVOKE: {
4628 // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
Rafael Espindola48da4f42013-11-04 16:16:24 +00004629 if (Record.size() < 4)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004630 return error("Invalid record");
David Blaikie5ea1f7b2015-04-24 18:06:06 +00004631 unsigned OpNum = 0;
4632 AttributeSet PAL = getAttributes(Record[OpNum++]);
4633 unsigned CCInfo = Record[OpNum++];
4634 BasicBlock *NormalBB = getBasicBlock(Record[OpNum++]);
4635 BasicBlock *UnwindBB = getBasicBlock(Record[OpNum++]);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004636
David Blaikie5ea1f7b2015-04-24 18:06:06 +00004637 FunctionType *FTy = nullptr;
4638 if (CCInfo >> 13 & 1 &&
4639 !(FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++]))))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004640 return error("Explicit invoke type is not a function type");
David Blaikie5ea1f7b2015-04-24 18:06:06 +00004641
Chris Lattnerdf1233d2007-05-06 00:00:00 +00004642 Value *Callee;
4643 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004644 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004645
Chris Lattner229907c2011-07-18 04:54:35 +00004646 PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType());
David Blaikie5ea1f7b2015-04-24 18:06:06 +00004647 if (!CalleeTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004648 return error("Callee is not a pointer");
David Blaikie5ea1f7b2015-04-24 18:06:06 +00004649 if (!FTy) {
4650 FTy = dyn_cast<FunctionType>(CalleeTy->getElementType());
4651 if (!FTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004652 return error("Callee is not of pointer to function type");
David Blaikie5ea1f7b2015-04-24 18:06:06 +00004653 } else if (CalleeTy->getElementType() != FTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004654 return error("Explicit invoke type does not match pointee type of "
David Blaikie5ea1f7b2015-04-24 18:06:06 +00004655 "callee operand");
4656 if (Record.size() < FTy->getNumParams() + OpNum)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004657 return error("Insufficient operands to call");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004658
Chris Lattner5285b5e2007-05-02 05:46:45 +00004659 SmallVector<Value*, 16> Ops;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00004660 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00004661 Ops.push_back(getValue(Record, OpNum, NextValueNo,
4662 FTy->getParamType(i)));
Craig Topper2617dcc2014-04-15 06:32:26 +00004663 if (!Ops.back())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004664 return error("Invalid record");
Chris Lattner5285b5e2007-05-02 05:46:45 +00004665 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004666
Chris Lattnerdf1233d2007-05-06 00:00:00 +00004667 if (!FTy->isVarArg()) {
4668 if (Record.size() != OpNum)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004669 return error("Invalid record");
Chris Lattner5285b5e2007-05-02 05:46:45 +00004670 } else {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00004671 // Read type/value pairs for varargs params.
4672 while (OpNum != Record.size()) {
4673 Value *Op;
4674 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004675 return error("Invalid record");
Chris Lattnerdf1233d2007-05-06 00:00:00 +00004676 Ops.push_back(Op);
4677 }
Chris Lattner5285b5e2007-05-02 05:46:45 +00004678 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004679
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00004680 I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops, OperandBundles);
4681 OperandBundles.clear();
Devang Patelaf206b82009-09-18 19:26:43 +00004682 InstructionList.push_back(I);
Vedant Kumarad6d6e72015-10-27 21:17:06 +00004683 cast<InvokeInst>(I)->setCallingConv(
4684 static_cast<CallingConv::ID>(CallingConv::MaxID & CCInfo));
Devang Patel4c758ea2008-09-25 21:00:45 +00004685 cast<InvokeInst>(I)->setAttributes(PAL);
Chris Lattner5285b5e2007-05-02 05:46:45 +00004686 break;
4687 }
Bill Wendlingf891bf82011-07-31 06:30:59 +00004688 case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval]
4689 unsigned Idx = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +00004690 Value *Val = nullptr;
Bill Wendlingf891bf82011-07-31 06:30:59 +00004691 if (getValueTypePair(Record, Idx, NextValueNo, Val))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004692 return error("Invalid record");
Bill Wendlingf891bf82011-07-31 06:30:59 +00004693 I = ResumeInst::Create(Val);
Bill Wendlingb9a89992011-09-01 00:50:20 +00004694 InstructionList.push_back(I);
Bill Wendlingf891bf82011-07-31 06:30:59 +00004695 break;
4696 }
Chris Lattnere53603e2007-05-02 04:27:25 +00004697 case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE
Owen Anderson55f1c092009-08-13 21:58:54 +00004698 I = new UnreachableInst(Context);
Devang Patelaf206b82009-09-18 19:26:43 +00004699 InstructionList.push_back(I);
Chris Lattnere53603e2007-05-02 04:27:25 +00004700 break;
Chris Lattnere9759c22007-05-06 00:21:25 +00004701 case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...]
Chris Lattnere14cb882007-05-04 19:11:41 +00004702 if (Record.size() < 1 || ((Record.size()-1)&1))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004703 return error("Invalid record");
Chris Lattner229907c2011-07-18 04:54:35 +00004704 Type *Ty = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00004705 if (!Ty)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004706 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004707
Jay Foad52131342011-03-30 11:28:46 +00004708 PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2);
Devang Patelaf206b82009-09-18 19:26:43 +00004709 InstructionList.push_back(PN);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004710
Chris Lattnere14cb882007-05-04 19:11:41 +00004711 for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00004712 Value *V;
4713 // With the new function encoding, it is possible that operands have
4714 // negative IDs (for forward references). Use a signed VBR
4715 // representation to keep the encoding small.
4716 if (UseRelativeIDs)
4717 V = getValueSigned(Record, 1+i, NextValueNo, Ty);
4718 else
4719 V = getValue(Record, 1+i, NextValueNo, Ty);
Chris Lattnere14cb882007-05-04 19:11:41 +00004720 BasicBlock *BB = getBasicBlock(Record[2+i]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00004721 if (!V || !BB)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004722 return error("Invalid record");
Chris Lattnerc332bba2007-05-03 18:58:09 +00004723 PN->addIncoming(V, BB);
4724 }
4725 I = PN;
4726 break;
4727 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004728
David Majnemer7fddecc2015-06-17 20:52:32 +00004729 case bitc::FUNC_CODE_INST_LANDINGPAD:
4730 case bitc::FUNC_CODE_INST_LANDINGPAD_OLD: {
Bill Wendlingfae14752011-08-12 20:24:12 +00004731 // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?]
4732 unsigned Idx = 0;
David Majnemer7fddecc2015-06-17 20:52:32 +00004733 if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD) {
4734 if (Record.size() < 3)
4735 return error("Invalid record");
4736 } else {
4737 assert(BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD);
4738 if (Record.size() < 4)
4739 return error("Invalid record");
4740 }
Bill Wendlingfae14752011-08-12 20:24:12 +00004741 Type *Ty = getTypeByID(Record[Idx++]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00004742 if (!Ty)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004743 return error("Invalid record");
David Majnemer7fddecc2015-06-17 20:52:32 +00004744 if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD) {
4745 Value *PersFn = nullptr;
4746 if (getValueTypePair(Record, Idx, NextValueNo, PersFn))
4747 return error("Invalid record");
4748
4749 if (!F->hasPersonalityFn())
4750 F->setPersonalityFn(cast<Constant>(PersFn));
4751 else if (F->getPersonalityFn() != cast<Constant>(PersFn))
4752 return error("Personality function mismatch");
4753 }
Bill Wendlingfae14752011-08-12 20:24:12 +00004754
4755 bool IsCleanup = !!Record[Idx++];
4756 unsigned NumClauses = Record[Idx++];
David Majnemer7fddecc2015-06-17 20:52:32 +00004757 LandingPadInst *LP = LandingPadInst::Create(Ty, NumClauses);
Bill Wendlingfae14752011-08-12 20:24:12 +00004758 LP->setCleanup(IsCleanup);
4759 for (unsigned J = 0; J != NumClauses; ++J) {
4760 LandingPadInst::ClauseType CT =
4761 LandingPadInst::ClauseType(Record[Idx++]); (void)CT;
4762 Value *Val;
4763
4764 if (getValueTypePair(Record, Idx, NextValueNo, Val)) {
4765 delete LP;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004766 return error("Invalid record");
Bill Wendlingfae14752011-08-12 20:24:12 +00004767 }
4768
4769 assert((CT != LandingPadInst::Catch ||
4770 !isa<ArrayType>(Val->getType())) &&
4771 "Catch clause has a invalid type!");
4772 assert((CT != LandingPadInst::Filter ||
4773 isa<ArrayType>(Val->getType())) &&
4774 "Filter clause has invalid type!");
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +00004775 LP->addClause(cast<Constant>(Val));
Bill Wendlingfae14752011-08-12 20:24:12 +00004776 }
4777
4778 I = LP;
Bill Wendlingb9a89992011-09-01 00:50:20 +00004779 InstructionList.push_back(I);
Bill Wendlingfae14752011-08-12 20:24:12 +00004780 break;
4781 }
4782
Chris Lattnerf1c87102011-06-17 18:09:11 +00004783 case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]
4784 if (Record.size() != 4)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004785 return error("Invalid record");
JF Bastien30bf96b2015-02-22 19:32:03 +00004786 uint64_t AlignRecord = Record[3];
4787 const uint64_t InAllocaMask = uint64_t(1) << 5;
David Blaikiebdb49102015-04-28 16:51:01 +00004788 const uint64_t ExplicitTypeMask = uint64_t(1) << 6;
Bob Wilson043ee652015-07-28 04:05:45 +00004789 // Reserve bit 7 for SwiftError flag.
4790 // const uint64_t SwiftErrorMask = uint64_t(1) << 7;
David Blaikiebdb49102015-04-28 16:51:01 +00004791 const uint64_t FlagMask = InAllocaMask | ExplicitTypeMask;
JF Bastien30bf96b2015-02-22 19:32:03 +00004792 bool InAlloca = AlignRecord & InAllocaMask;
David Blaikiebdb49102015-04-28 16:51:01 +00004793 Type *Ty = getTypeByID(Record[0]);
4794 if ((AlignRecord & ExplicitTypeMask) == 0) {
4795 auto *PTy = dyn_cast_or_null<PointerType>(Ty);
4796 if (!PTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004797 return error("Old-style alloca with a non-pointer type");
David Blaikiebdb49102015-04-28 16:51:01 +00004798 Ty = PTy->getElementType();
4799 }
4800 Type *OpTy = getTypeByID(Record[1]);
4801 Value *Size = getFnValueByID(Record[2], OpTy);
JF Bastien30bf96b2015-02-22 19:32:03 +00004802 unsigned Align;
4803 if (std::error_code EC =
David Blaikiebdb49102015-04-28 16:51:01 +00004804 parseAlignmentValue(AlignRecord & ~FlagMask, Align)) {
JF Bastien30bf96b2015-02-22 19:32:03 +00004805 return EC;
4806 }
Rafael Espindola48da4f42013-11-04 16:16:24 +00004807 if (!Ty || !Size)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004808 return error("Invalid record");
David Blaikiebdb49102015-04-28 16:51:01 +00004809 AllocaInst *AI = new AllocaInst(Ty, Size, Align);
Reid Kleckner56b56ea2014-07-16 01:34:27 +00004810 AI->setUsedWithInAlloca(InAlloca);
4811 I = AI;
Devang Patelaf206b82009-09-18 19:26:43 +00004812 InstructionList.push_back(I);
Chris Lattnerc332bba2007-05-03 18:58:09 +00004813 break;
4814 }
Chris Lattner9f600c52007-05-03 22:04:19 +00004815 case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol]
Chris Lattnerdf1233d2007-05-06 00:00:00 +00004816 unsigned OpNum = 0;
4817 Value *Op;
4818 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
David Blaikie85035652015-02-25 01:07:20 +00004819 (OpNum + 2 != Record.size() && OpNum + 3 != Record.size()))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004820 return error("Invalid record");
David Blaikie85035652015-02-25 01:07:20 +00004821
4822 Type *Ty = nullptr;
4823 if (OpNum + 3 == Record.size())
4824 Ty = getTypeByID(Record[OpNum++]);
Filipe Cabecinhas11bb8492015-05-18 21:48:55 +00004825 if (std::error_code EC =
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004826 typeCheckLoadStoreInst(DiagnosticHandler, Ty, Op->getType()))
Filipe Cabecinhas11bb8492015-05-18 21:48:55 +00004827 return EC;
David Blaikieb7a029872015-04-17 19:56:21 +00004828 if (!Ty)
4829 Ty = cast<PointerType>(Op->getType())->getElementType();
David Blaikie85035652015-02-25 01:07:20 +00004830
JF Bastien30bf96b2015-02-22 19:32:03 +00004831 unsigned Align;
4832 if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
4833 return EC;
David Blaikieb7a029872015-04-17 19:56:21 +00004834 I = new LoadInst(Ty, Op, "", Record[OpNum + 1], Align);
David Blaikie85035652015-02-25 01:07:20 +00004835
Devang Patelaf206b82009-09-18 19:26:43 +00004836 InstructionList.push_back(I);
Chris Lattner83930552007-05-01 07:01:57 +00004837 break;
Chris Lattner9f600c52007-05-03 22:04:19 +00004838 }
Eli Friedman59b66882011-08-09 23:02:53 +00004839 case bitc::FUNC_CODE_INST_LOADATOMIC: {
4840 // LOADATOMIC: [opty, op, align, vol, ordering, synchscope]
4841 unsigned OpNum = 0;
4842 Value *Op;
4843 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
David Blaikie85035652015-02-25 01:07:20 +00004844 (OpNum + 4 != Record.size() && OpNum + 5 != Record.size()))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004845 return error("Invalid record");
Eli Friedman59b66882011-08-09 23:02:53 +00004846
David Blaikie85035652015-02-25 01:07:20 +00004847 Type *Ty = nullptr;
4848 if (OpNum + 5 == Record.size())
4849 Ty = getTypeByID(Record[OpNum++]);
Filipe Cabecinhas11bb8492015-05-18 21:48:55 +00004850 if (std::error_code EC =
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004851 typeCheckLoadStoreInst(DiagnosticHandler, Ty, Op->getType()))
Filipe Cabecinhas11bb8492015-05-18 21:48:55 +00004852 return EC;
4853 if (!Ty)
4854 Ty = cast<PointerType>(Op->getType())->getElementType();
David Blaikie85035652015-02-25 01:07:20 +00004855
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004856 AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]);
Eli Friedman59b66882011-08-09 23:02:53 +00004857 if (Ordering == NotAtomic || Ordering == Release ||
4858 Ordering == AcquireRelease)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004859 return error("Invalid record");
Eli Friedman59b66882011-08-09 23:02:53 +00004860 if (Ordering != NotAtomic && Record[OpNum] == 0)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004861 return error("Invalid record");
4862 SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]);
Eli Friedman59b66882011-08-09 23:02:53 +00004863
JF Bastien30bf96b2015-02-22 19:32:03 +00004864 unsigned Align;
4865 if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
4866 return EC;
4867 I = new LoadInst(Op, "", Record[OpNum+1], Align, Ordering, SynchScope);
David Blaikie85035652015-02-25 01:07:20 +00004868
Eli Friedman59b66882011-08-09 23:02:53 +00004869 InstructionList.push_back(I);
4870 break;
4871 }
David Blaikie612ddbf2015-04-22 04:14:42 +00004872 case bitc::FUNC_CODE_INST_STORE:
4873 case bitc::FUNC_CODE_INST_STORE_OLD: { // STORE2:[ptrty, ptr, val, align, vol]
Christopher Lamb54dd24c2007-12-11 08:59:05 +00004874 unsigned OpNum = 0;
4875 Value *Val, *Ptr;
4876 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
David Blaikie612ddbf2015-04-22 04:14:42 +00004877 (BitCode == bitc::FUNC_CODE_INST_STORE
4878 ? getValueTypePair(Record, OpNum, NextValueNo, Val)
4879 : popValue(Record, OpNum, NextValueNo,
4880 cast<PointerType>(Ptr->getType())->getElementType(),
4881 Val)) ||
4882 OpNum + 2 != Record.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004883 return error("Invalid record");
Filipe Cabecinhas11bb8492015-05-18 21:48:55 +00004884
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004885 if (std::error_code EC = typeCheckLoadStoreInst(
Filipe Cabecinhas11bb8492015-05-18 21:48:55 +00004886 DiagnosticHandler, Val->getType(), Ptr->getType()))
4887 return EC;
JF Bastien30bf96b2015-02-22 19:32:03 +00004888 unsigned Align;
4889 if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
4890 return EC;
4891 I = new StoreInst(Val, Ptr, Record[OpNum+1], Align);
Devang Patelaf206b82009-09-18 19:26:43 +00004892 InstructionList.push_back(I);
Christopher Lamb54dd24c2007-12-11 08:59:05 +00004893 break;
4894 }
David Blaikie50a06152015-04-22 04:14:46 +00004895 case bitc::FUNC_CODE_INST_STOREATOMIC:
4896 case bitc::FUNC_CODE_INST_STOREATOMIC_OLD: {
Eli Friedman59b66882011-08-09 23:02:53 +00004897 // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, synchscope]
4898 unsigned OpNum = 0;
4899 Value *Val, *Ptr;
4900 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
David Blaikie50a06152015-04-22 04:14:46 +00004901 (BitCode == bitc::FUNC_CODE_INST_STOREATOMIC
4902 ? getValueTypePair(Record, OpNum, NextValueNo, Val)
4903 : popValue(Record, OpNum, NextValueNo,
4904 cast<PointerType>(Ptr->getType())->getElementType(),
4905 Val)) ||
4906 OpNum + 4 != Record.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004907 return error("Invalid record");
Eli Friedman59b66882011-08-09 23:02:53 +00004908
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004909 if (std::error_code EC = typeCheckLoadStoreInst(
Filipe Cabecinhas11bb8492015-05-18 21:48:55 +00004910 DiagnosticHandler, Val->getType(), Ptr->getType()))
4911 return EC;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004912 AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]);
Eli Friedman222b5a42011-09-19 19:41:28 +00004913 if (Ordering == NotAtomic || Ordering == Acquire ||
Eli Friedman59b66882011-08-09 23:02:53 +00004914 Ordering == AcquireRelease)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004915 return error("Invalid record");
4916 SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]);
Eli Friedman59b66882011-08-09 23:02:53 +00004917 if (Ordering != NotAtomic && Record[OpNum] == 0)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004918 return error("Invalid record");
Eli Friedman59b66882011-08-09 23:02:53 +00004919
JF Bastien30bf96b2015-02-22 19:32:03 +00004920 unsigned Align;
4921 if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
4922 return EC;
4923 I = new StoreInst(Val, Ptr, Record[OpNum+1], Align, Ordering, SynchScope);
Eli Friedman59b66882011-08-09 23:02:53 +00004924 InstructionList.push_back(I);
4925 break;
4926 }
David Blaikie2a661cd2015-04-28 04:30:29 +00004927 case bitc::FUNC_CODE_INST_CMPXCHG_OLD:
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004928 case bitc::FUNC_CODE_INST_CMPXCHG: {
Tim Northovere94a5182014-03-11 10:48:52 +00004929 // CMPXCHG:[ptrty, ptr, cmp, new, vol, successordering, synchscope,
Tim Northover420a2162014-06-13 14:24:07 +00004930 // failureordering?, isweak?]
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004931 unsigned OpNum = 0;
4932 Value *Ptr, *Cmp, *New;
4933 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
David Blaikie2a661cd2015-04-28 04:30:29 +00004934 (BitCode == bitc::FUNC_CODE_INST_CMPXCHG
4935 ? getValueTypePair(Record, OpNum, NextValueNo, Cmp)
4936 : popValue(Record, OpNum, NextValueNo,
4937 cast<PointerType>(Ptr->getType())->getElementType(),
4938 Cmp)) ||
4939 popValue(Record, OpNum, NextValueNo, Cmp->getType(), New) ||
4940 Record.size() < OpNum + 3 || Record.size() > OpNum + 5)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004941 return error("Invalid record");
4942 AtomicOrdering SuccessOrdering = getDecodedOrdering(Record[OpNum + 1]);
Tim Northovere94a5182014-03-11 10:48:52 +00004943 if (SuccessOrdering == NotAtomic || SuccessOrdering == Unordered)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004944 return error("Invalid record");
4945 SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 2]);
Tim Northovere94a5182014-03-11 10:48:52 +00004946
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004947 if (std::error_code EC = typeCheckLoadStoreInst(
Filipe Cabecinhas11bb8492015-05-18 21:48:55 +00004948 DiagnosticHandler, Cmp->getType(), Ptr->getType()))
4949 return EC;
Tim Northovere94a5182014-03-11 10:48:52 +00004950 AtomicOrdering FailureOrdering;
4951 if (Record.size() < 7)
4952 FailureOrdering =
4953 AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering);
4954 else
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004955 FailureOrdering = getDecodedOrdering(Record[OpNum + 3]);
Tim Northovere94a5182014-03-11 10:48:52 +00004956
4957 I = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering, FailureOrdering,
4958 SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004959 cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]);
Tim Northover420a2162014-06-13 14:24:07 +00004960
4961 if (Record.size() < 8) {
4962 // Before weak cmpxchgs existed, the instruction simply returned the
4963 // value loaded from memory, so bitcode files from that era will be
4964 // expecting the first component of a modern cmpxchg.
4965 CurBB->getInstList().push_back(I);
4966 I = ExtractValueInst::Create(I, 0);
4967 } else {
4968 cast<AtomicCmpXchgInst>(I)->setWeak(Record[OpNum+4]);
4969 }
4970
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004971 InstructionList.push_back(I);
4972 break;
4973 }
4974 case bitc::FUNC_CODE_INST_ATOMICRMW: {
4975 // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, synchscope]
4976 unsigned OpNum = 0;
4977 Value *Ptr, *Val;
4978 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00004979 popValue(Record, OpNum, NextValueNo,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004980 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
4981 OpNum+4 != Record.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004982 return error("Invalid record");
4983 AtomicRMWInst::BinOp Operation = getDecodedRMWOperation(Record[OpNum]);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004984 if (Operation < AtomicRMWInst::FIRST_BINOP ||
4985 Operation > AtomicRMWInst::LAST_BINOP)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004986 return error("Invalid record");
4987 AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]);
Eli Friedman59b66882011-08-09 23:02:53 +00004988 if (Ordering == NotAtomic || Ordering == Unordered)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004989 return error("Invalid record");
4990 SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004991 I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SynchScope);
4992 cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]);
4993 InstructionList.push_back(I);
4994 break;
4995 }
Eli Friedmanfee02c62011-07-25 23:16:38 +00004996 case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, synchscope]
4997 if (2 != Record.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004998 return error("Invalid record");
4999 AtomicOrdering Ordering = getDecodedOrdering(Record[0]);
Eli Friedmanfee02c62011-07-25 23:16:38 +00005000 if (Ordering == NotAtomic || Ordering == Unordered ||
5001 Ordering == Monotonic)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005002 return error("Invalid record");
5003 SynchronizationScope SynchScope = getDecodedSynchScope(Record[1]);
Eli Friedmanfee02c62011-07-25 23:16:38 +00005004 I = new FenceInst(Context, Ordering, SynchScope);
5005 InstructionList.push_back(I);
5006 break;
5007 }
Chris Lattnerc44070802011-06-17 18:17:37 +00005008 case bitc::FUNC_CODE_INST_CALL: {
Sanjay Patelfa54ace2015-12-14 21:59:03 +00005009 // CALL: [paramattrs, cc, fmf, fnty, fnid, arg0, arg1...]
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00005010 if (Record.size() < 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005011 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005012
David Blaikiedbe6e0f2015-04-17 06:40:14 +00005013 unsigned OpNum = 0;
5014 AttributeSet PAL = getAttributes(Record[OpNum++]);
5015 unsigned CCInfo = Record[OpNum++];
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005016
Sanjay Patelfa54ace2015-12-14 21:59:03 +00005017 FastMathFlags FMF;
5018 if ((CCInfo >> bitc::CALL_FMF) & 1) {
5019 FMF = getDecodedFastMathFlags(Record[OpNum++]);
5020 if (!FMF.any())
5021 return error("Fast math flags indicator set for call with no FMF");
5022 }
5023
David Blaikiedbe6e0f2015-04-17 06:40:14 +00005024 FunctionType *FTy = nullptr;
Akira Hatanaka97cb3972015-11-07 02:48:49 +00005025 if (CCInfo >> bitc::CALL_EXPLICIT_TYPE & 1 &&
David Blaikiedbe6e0f2015-04-17 06:40:14 +00005026 !(FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++]))))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005027 return error("Explicit call type is not a function type");
David Blaikiedbe6e0f2015-04-17 06:40:14 +00005028
Chris Lattnerdf1233d2007-05-06 00:00:00 +00005029 Value *Callee;
5030 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005031 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005032
Chris Lattner229907c2011-07-18 04:54:35 +00005033 PointerType *OpTy = dyn_cast<PointerType>(Callee->getType());
David Blaikiedbe6e0f2015-04-17 06:40:14 +00005034 if (!OpTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005035 return error("Callee is not a pointer type");
David Blaikie348de692015-04-23 21:36:23 +00005036 if (!FTy) {
5037 FTy = dyn_cast<FunctionType>(OpTy->getElementType());
5038 if (!FTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005039 return error("Callee is not of pointer to function type");
David Blaikie348de692015-04-23 21:36:23 +00005040 } else if (OpTy->getElementType() != FTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005041 return error("Explicit call type does not match pointee type of "
David Blaikiedbe6e0f2015-04-17 06:40:14 +00005042 "callee operand");
5043 if (Record.size() < FTy->getNumParams() + OpNum)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005044 return error("Insufficient operands to call");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005045
Chris Lattner9f600c52007-05-03 22:04:19 +00005046 SmallVector<Value*, 16> Args;
5047 // Read the fixed params.
Chris Lattnerdf1233d2007-05-06 00:00:00 +00005048 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00005049 if (FTy->getParamType(i)->isLabelTy())
Dale Johannesen4646aa32007-11-05 21:20:28 +00005050 Args.push_back(getBasicBlock(Record[OpNum]));
Dan Gohmanbbcd04d2010-09-13 18:00:48 +00005051 else
Jan Wen Voungafaced02012-10-11 20:20:40 +00005052 Args.push_back(getValue(Record, OpNum, NextValueNo,
5053 FTy->getParamType(i)));
Craig Topper2617dcc2014-04-15 06:32:26 +00005054 if (!Args.back())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005055 return error("Invalid record");
Chris Lattner9f600c52007-05-03 22:04:19 +00005056 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005057
Chris Lattner9f600c52007-05-03 22:04:19 +00005058 // Read type/value pairs for varargs params.
Chris Lattner9f600c52007-05-03 22:04:19 +00005059 if (!FTy->isVarArg()) {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00005060 if (OpNum != Record.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005061 return error("Invalid record");
Chris Lattner9f600c52007-05-03 22:04:19 +00005062 } else {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00005063 while (OpNum != Record.size()) {
5064 Value *Op;
5065 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005066 return error("Invalid record");
Chris Lattnerdf1233d2007-05-06 00:00:00 +00005067 Args.push_back(Op);
Chris Lattner9f600c52007-05-03 22:04:19 +00005068 }
5069 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005070
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00005071 I = CallInst::Create(FTy, Callee, Args, OperandBundles);
5072 OperandBundles.clear();
Devang Patelaf206b82009-09-18 19:26:43 +00005073 InstructionList.push_back(I);
Sandeep Patel68c5f472009-09-02 08:44:58 +00005074 cast<CallInst>(I)->setCallingConv(
Akira Hatanaka97cb3972015-11-07 02:48:49 +00005075 static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV));
Reid Kleckner5772b772014-04-24 20:14:34 +00005076 CallInst::TailCallKind TCK = CallInst::TCK_None;
Akira Hatanaka97cb3972015-11-07 02:48:49 +00005077 if (CCInfo & 1 << bitc::CALL_TAIL)
Reid Kleckner5772b772014-04-24 20:14:34 +00005078 TCK = CallInst::TCK_Tail;
Akira Hatanaka97cb3972015-11-07 02:48:49 +00005079 if (CCInfo & (1 << bitc::CALL_MUSTTAIL))
Reid Kleckner5772b772014-04-24 20:14:34 +00005080 TCK = CallInst::TCK_MustTail;
Akira Hatanaka97cb3972015-11-07 02:48:49 +00005081 if (CCInfo & (1 << bitc::CALL_NOTAIL))
Akira Hatanaka5cfcce122015-11-06 23:55:38 +00005082 TCK = CallInst::TCK_NoTail;
Reid Kleckner5772b772014-04-24 20:14:34 +00005083 cast<CallInst>(I)->setTailCallKind(TCK);
Devang Patel4c758ea2008-09-25 21:00:45 +00005084 cast<CallInst>(I)->setAttributes(PAL);
Sanjay Patelfa54ace2015-12-14 21:59:03 +00005085 if (FMF.any()) {
5086 if (!isa<FPMathOperator>(I))
5087 return error("Fast-math-flags specified for call without "
5088 "floating-point scalar or vector return type");
5089 I->setFastMathFlags(FMF);
5090 }
Chris Lattner9f600c52007-05-03 22:04:19 +00005091 break;
5092 }
5093 case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty]
5094 if (Record.size() < 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005095 return error("Invalid record");
Chris Lattner229907c2011-07-18 04:54:35 +00005096 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00005097 Value *Op = getValue(Record, 1, NextValueNo, OpTy);
Chris Lattner229907c2011-07-18 04:54:35 +00005098 Type *ResTy = getTypeByID(Record[2]);
Chris Lattner9f600c52007-05-03 22:04:19 +00005099 if (!OpTy || !Op || !ResTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005100 return error("Invalid record");
Chris Lattner9f600c52007-05-03 22:04:19 +00005101 I = new VAArgInst(Op, ResTy);
Devang Patelaf206b82009-09-18 19:26:43 +00005102 InstructionList.push_back(I);
Chris Lattner9f600c52007-05-03 22:04:19 +00005103 break;
5104 }
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00005105
5106 case bitc::FUNC_CODE_OPERAND_BUNDLE: {
5107 // A call or an invoke can be optionally prefixed with some variable
5108 // number of operand bundle blocks. These blocks are read into
5109 // OperandBundles and consumed at the next call or invoke instruction.
5110
5111 if (Record.size() < 1 || Record[0] >= BundleTags.size())
5112 return error("Invalid record");
5113
Sanjoy Dasf79d3442015-11-18 08:30:07 +00005114 std::vector<Value *> Inputs;
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00005115
5116 unsigned OpNum = 1;
5117 while (OpNum != Record.size()) {
5118 Value *Op;
5119 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
5120 return error("Invalid record");
5121 Inputs.push_back(Op);
5122 }
5123
Sanjoy Dasf79d3442015-11-18 08:30:07 +00005124 OperandBundles.emplace_back(BundleTags[Record[0]], std::move(Inputs));
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00005125 continue;
5126 }
Chris Lattner83930552007-05-01 07:01:57 +00005127 }
5128
5129 // Add instruction to end of current BB. If there is no current BB, reject
5130 // this file.
Craig Topper2617dcc2014-04-15 06:32:26 +00005131 if (!CurBB) {
Chris Lattner83930552007-05-01 07:01:57 +00005132 delete I;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005133 return error("Invalid instruction with no BB");
Chris Lattner83930552007-05-01 07:01:57 +00005134 }
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00005135 if (!OperandBundles.empty()) {
5136 delete I;
5137 return error("Operand bundles found with no consumer");
5138 }
Chris Lattner83930552007-05-01 07:01:57 +00005139 CurBB->getInstList().push_back(I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005140
Chris Lattner83930552007-05-01 07:01:57 +00005141 // If this was a terminator instruction, move to the next block.
5142 if (isa<TerminatorInst>(I)) {
5143 ++CurBBNo;
Craig Topper2617dcc2014-04-15 06:32:26 +00005144 CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr;
Chris Lattner83930552007-05-01 07:01:57 +00005145 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005146
Chris Lattner83930552007-05-01 07:01:57 +00005147 // Non-void values get registered in the value table for future use.
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00005148 if (I && !I->getType()->isVoidTy())
David Majnemer8a1c45d2015-12-12 05:38:55 +00005149 ValueList.assignValue(I, NextValueNo++);
Chris Lattner85b7b402007-05-01 05:52:21 +00005150 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005151
Chris Lattner27d38752013-01-20 02:13:19 +00005152OutOfRecordLoop:
Joe Abbey97b7a172013-02-06 22:14:06 +00005153
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00005154 if (!OperandBundles.empty())
5155 return error("Operand bundles found with no consumer");
5156
Chris Lattner83930552007-05-01 07:01:57 +00005157 // Check the function list for unresolved values.
5158 if (Argument *A = dyn_cast<Argument>(ValueList.back())) {
Craig Topper2617dcc2014-04-15 06:32:26 +00005159 if (!A->getParent()) {
Chris Lattner83930552007-05-01 07:01:57 +00005160 // We found at least one unresolved value. Nuke them all to avoid leaks.
5161 for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){
Craig Topper2617dcc2014-04-15 06:32:26 +00005162 if ((A = dyn_cast_or_null<Argument>(ValueList[i])) && !A->getParent()) {
Owen Andersonb292b8c2009-07-30 23:03:37 +00005163 A->replaceAllUsesWith(UndefValue::get(A->getType()));
Chris Lattner83930552007-05-01 07:01:57 +00005164 delete A;
5165 }
5166 }
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005167 return error("Never resolved value found in function");
Chris Lattner83930552007-05-01 07:01:57 +00005168 }
Chris Lattner83930552007-05-01 07:01:57 +00005169 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005170
Dan Gohman9b9ff462010-08-25 20:23:38 +00005171 // FIXME: Check for unresolved forward-declared metadata references
5172 // and clean up leaks.
5173
Chris Lattner85b7b402007-05-01 05:52:21 +00005174 // Trim the value list down to the size it was before we parsed this function.
5175 ValueList.shrinkTo(ModuleValueListSize);
Dan Gohman26d837d2010-08-25 20:22:53 +00005176 MDValueList.shrinkTo(ModuleMDValueListSize);
Chris Lattner85b7b402007-05-01 05:52:21 +00005177 std::vector<BasicBlock*>().swap(FunctionBBs);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00005178 return std::error_code();
Chris Lattner51ffe7c2007-05-01 04:59:48 +00005179}
5180
Rafael Espindola7d712032013-11-05 17:16:08 +00005181/// Find the function body in the bitcode stream
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005182std::error_code BitcodeReader::findFunctionInStream(
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00005183 Function *F,
5184 DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005185 while (DeferredFunctionInfoIterator->second == 0) {
Teresa Johnsonff642b92015-09-17 20:12:00 +00005186 // This is the fallback handling for the old format bitcode that
Teresa Johnson1493ad92015-10-10 14:18:36 +00005187 // didn't contain the function index in the VST, or when we have
5188 // an anonymous function which would not have a VST entry.
5189 // Assert that we have one of those two cases.
5190 assert(VSTOffset == 0 || !F->hasName());
5191 // Parse the next body in the stream and set its position in the
5192 // DeferredFunctionInfo map.
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005193 if (std::error_code EC = rememberAndSkipFunctionBodies())
5194 return EC;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005195 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00005196 return std::error_code();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005197}
5198
Chris Lattner9eeada92007-05-18 04:02:46 +00005199//===----------------------------------------------------------------------===//
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00005200// GVMaterializer implementation
Chris Lattner9eeada92007-05-18 04:02:46 +00005201//===----------------------------------------------------------------------===//
5202
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +00005203void BitcodeReader::releaseBuffer() { Buffer.release(); }
Chris Lattner9eeada92007-05-18 04:02:46 +00005204
Rafael Espindola5a52e6d2014-10-24 22:50:48 +00005205std::error_code BitcodeReader::materialize(GlobalValue *GV) {
Teresa Johnsond4d3dfd2015-11-20 14:51:27 +00005206 // In older bitcode we must materialize the metadata before parsing
5207 // any functions, in order to set up the MDValueList properly.
5208 if (!SeenModuleValuesRecord) {
5209 if (std::error_code EC = materializeMetadata())
5210 return EC;
5211 }
Manman Ren4a9b0eb2015-03-13 19:24:30 +00005212
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00005213 Function *F = dyn_cast<Function>(GV);
5214 // If it's not a function or is already material, ignore the request.
Rafael Espindola2b11ad42013-11-05 19:36:34 +00005215 if (!F || !F->isMaterializable())
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00005216 return std::error_code();
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00005217
5218 DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F);
Chris Lattner9eeada92007-05-18 04:02:46 +00005219 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005220 // If its position is recorded as 0, its body is somewhere in the stream
5221 // but we haven't seen it yet.
Rafael Espindola1c863ca2015-06-22 18:06:15 +00005222 if (DFII->second == 0)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005223 if (std::error_code EC = findFunctionInStream(F, DFII))
Rafael Espindola2b11ad42013-11-05 19:36:34 +00005224 return EC;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005225
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00005226 // Move the bit stream to the saved position of the deferred function body.
5227 Stream.JumpToBit(DFII->second);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005228
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005229 if (std::error_code EC = parseFunctionBody(F))
Rafael Espindola2b11ad42013-11-05 19:36:34 +00005230 return EC;
Rafael Espindolad4bcefc2014-10-24 18:13:04 +00005231 F->setIsMaterializable(false);
Chandler Carruth7132e002007-08-04 01:51:18 +00005232
Rafael Espindola0d68b4c2015-03-30 21:36:43 +00005233 if (StripDebugInfo)
5234 stripDebugInfo(*F);
5235
Chandler Carruth7132e002007-08-04 01:51:18 +00005236 // Upgrade any old intrinsic calls in the function.
Rafael Espindola86e33402015-07-02 15:55:09 +00005237 for (auto &I : UpgradedIntrinsics) {
Filipe Cabecinhas0011c582015-07-03 20:12:01 +00005238 for (auto UI = I.first->user_begin(), UE = I.first->user_end(); UI != UE;) {
5239 User *U = *UI;
5240 ++UI;
5241 if (CallInst *CI = dyn_cast<CallInst>(U))
5242 UpgradeIntrinsicCall(CI, I.second);
Chandler Carruth7132e002007-08-04 01:51:18 +00005243 }
5244 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005245
Peter Collingbourned4bff302015-11-05 22:03:56 +00005246 // Finish fn->subprogram upgrade for materialized functions.
5247 if (DISubprogram *SP = FunctionsWithSPs.lookup(F))
5248 F->setSubprogram(SP);
5249
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00005250 // Bring in any functions that this function forward-referenced via
5251 // blockaddresses.
5252 return materializeForwardReferencedFunctions();
Chris Lattner9eeada92007-05-18 04:02:46 +00005253}
5254
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00005255bool BitcodeReader::isDematerializable(const GlobalValue *GV) const {
5256 const Function *F = dyn_cast<Function>(GV);
5257 if (!F || F->isDeclaration())
5258 return false;
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00005259
5260 // Dematerializing F would leave dangling references that wouldn't be
5261 // reconnected on re-materialization.
5262 if (BlockAddressesTaken.count(F))
5263 return false;
5264
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00005265 return DeferredFunctionInfo.count(const_cast<Function*>(F));
5266}
5267
Eric Christopher97cb5652015-05-15 18:20:14 +00005268void BitcodeReader::dematerialize(GlobalValue *GV) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00005269 Function *F = dyn_cast<Function>(GV);
5270 // If this function isn't dematerializable, this is a noop.
5271 if (!F || !isDematerializable(F))
Chris Lattner9eeada92007-05-18 04:02:46 +00005272 return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005273
Chris Lattner9eeada92007-05-18 04:02:46 +00005274 assert(DeferredFunctionInfo.count(F) && "No info to read function later?");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005275
Chris Lattner9eeada92007-05-18 04:02:46 +00005276 // Just forget the function body, we can remat it later.
Petar Jovanovic7480e4d2014-09-23 12:54:19 +00005277 F->dropAllReferences();
Rafael Espindolad4bcefc2014-10-24 18:13:04 +00005278 F->setIsMaterializable(true);
Chris Lattner9eeada92007-05-18 04:02:46 +00005279}
5280
Eric Christopher97cb5652015-05-15 18:20:14 +00005281std::error_code BitcodeReader::materializeModule(Module *M) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00005282 assert(M == TheModule &&
5283 "Can only Materialize the Module this BitcodeReader is attached to.");
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00005284
Manman Ren4a9b0eb2015-03-13 19:24:30 +00005285 if (std::error_code EC = materializeMetadata())
5286 return EC;
5287
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00005288 // Promise to materialize all forward references.
5289 WillMaterializeAllForwardRefs = true;
5290
Chris Lattner06310bf2009-06-16 05:15:21 +00005291 // Iterate over the module, deserializing any functions that are still on
5292 // disk.
Duncan P. N. Exon Smithfb1743a32015-10-13 16:48:55 +00005293 for (Function &F : *TheModule) {
5294 if (std::error_code EC = materialize(&F))
Rafael Espindola246c4fb2014-11-01 16:46:18 +00005295 return EC;
Rafael Espindola2b11ad42013-11-05 19:36:34 +00005296 }
Teresa Johnson1493ad92015-10-10 14:18:36 +00005297 // At this point, if there are any function bodies, parse the rest of
5298 // the bits in the module past the last function block we have recorded
5299 // through either lazy scanning or the VST.
5300 if (LastFunctionBlockBit || NextUnreadBit)
5301 parseModule(LastFunctionBlockBit > NextUnreadBit ? LastFunctionBlockBit
5302 : NextUnreadBit);
Derek Schuff92ef9752012-02-29 00:07:09 +00005303
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00005304 // Check that all block address forward references got resolved (as we
5305 // promised above).
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00005306 if (!BasicBlockFwdRefs.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005307 return error("Never resolved function from blockaddress");
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00005308
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005309 // Upgrade any intrinsic calls that slipped through (should not happen!) and
5310 // delete the old functions to clean up. We can't do this unless the entire
5311 // module is materialized because there could always be another function body
Chandler Carruth7132e002007-08-04 01:51:18 +00005312 // with calls to the old function.
Rafael Espindola86e33402015-07-02 15:55:09 +00005313 for (auto &I : UpgradedIntrinsics) {
Filipe Cabecinhas0011c582015-07-03 20:12:01 +00005314 for (auto *U : I.first->users()) {
5315 if (CallInst *CI = dyn_cast<CallInst>(U))
5316 UpgradeIntrinsicCall(CI, I.second);
Chandler Carruth7132e002007-08-04 01:51:18 +00005317 }
Filipe Cabecinhas0011c582015-07-03 20:12:01 +00005318 if (!I.first->use_empty())
5319 I.first->replaceAllUsesWith(I.second);
5320 I.first->eraseFromParent();
Chandler Carruth7132e002007-08-04 01:51:18 +00005321 }
Rafael Espindola4e721212015-07-02 16:22:40 +00005322 UpgradedIntrinsics.clear();
Devang Patel80ae3492009-08-28 23:24:31 +00005323
Manman Ren209b17c2013-09-28 00:22:27 +00005324 for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++)
5325 UpgradeInstWithTBAATag(InstsWithTBAATag[I]);
5326
Manman Ren8b4306c2013-12-02 21:29:56 +00005327 UpgradeDebugInfo(*M);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00005328 return std::error_code();
Chris Lattner9eeada92007-05-18 04:02:46 +00005329}
5330
Rafael Espindola2fa1e432014-12-03 07:18:23 +00005331std::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const {
5332 return IdentifiedStructTypes;
5333}
5334
Rafael Espindola1aabf982015-06-16 23:29:49 +00005335std::error_code
5336BitcodeReader::initStream(std::unique_ptr<DataStreamer> Streamer) {
Rafael Espindola4223a1f2015-06-15 20:08:17 +00005337 if (Streamer)
Rafael Espindola1aabf982015-06-16 23:29:49 +00005338 return initLazyStream(std::move(Streamer));
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005339 return initStreamFromBuffer();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005340}
5341
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005342std::error_code BitcodeReader::initStreamFromBuffer() {
Roman Divacky4717a8d2012-09-06 15:42:13 +00005343 const unsigned char *BufPtr = (const unsigned char*)Buffer->getBufferStart();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005344 const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
5345
Rafael Espindola27435252014-07-29 21:01:24 +00005346 if (Buffer->getBufferSize() & 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005347 return error("Invalid bitcode signature");
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005348
5349 // If we have a wrapper header, parse it and ignore the non-bc file contents.
5350 // The magic number is 0x0B17C0DE stored in little endian.
5351 if (isBitcodeWrapper(BufPtr, BufEnd))
5352 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005353 return error("Invalid bitcode wrapper header");
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005354
5355 StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
Rafael Espindolade1e5b82014-11-12 14:48:38 +00005356 Stream.init(&*StreamFile);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005357
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00005358 return std::error_code();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005359}
5360
Rafael Espindola1aabf982015-06-16 23:29:49 +00005361std::error_code
5362BitcodeReader::initLazyStream(std::unique_ptr<DataStreamer> Streamer) {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005363 // Check and strip off the bitcode wrapper; BitstreamReader expects never to
5364 // see it.
Rafael Espindola1aabf982015-06-16 23:29:49 +00005365 auto OwnedBytes =
5366 llvm::make_unique<StreamingMemoryObject>(std::move(Streamer));
Rafael Espindola7d727b52014-12-18 05:08:43 +00005367 StreamingMemoryObject &Bytes = *OwnedBytes;
Yaron Keren06d69302014-12-18 10:03:35 +00005368 StreamFile = llvm::make_unique<BitstreamReader>(std::move(OwnedBytes));
Rafael Espindolade1e5b82014-11-12 14:48:38 +00005369 Stream.init(&*StreamFile);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005370
5371 unsigned char buf[16];
Rafael Espindola7d727b52014-12-18 05:08:43 +00005372 if (Bytes.readBytes(buf, 16, 0) != 16)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005373 return error("Invalid bitcode signature");
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005374
5375 if (!isBitcode(buf, buf + 16))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005376 return error("Invalid bitcode signature");
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005377
5378 if (isBitcodeWrapper(buf, buf + 4)) {
5379 const unsigned char *bitcodeStart = buf;
5380 const unsigned char *bitcodeEnd = buf + 16;
5381 SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
Rafael Espindola7d727b52014-12-18 05:08:43 +00005382 Bytes.dropLeadingBytes(bitcodeStart - buf);
5383 Bytes.setKnownObjectSize(bitcodeEnd - bitcodeStart);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005384 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00005385 return std::error_code();
Rafael Espindola48da4f42013-11-04 16:16:24 +00005386}
5387
Teresa Johnson403a7872015-10-04 14:33:43 +00005388std::error_code FunctionIndexBitcodeReader::error(BitcodeError E,
5389 const Twine &Message) {
5390 return ::error(DiagnosticHandler, make_error_code(E), Message);
5391}
5392
5393std::error_code FunctionIndexBitcodeReader::error(const Twine &Message) {
5394 return ::error(DiagnosticHandler,
5395 make_error_code(BitcodeError::CorruptedBitcode), Message);
5396}
5397
5398std::error_code FunctionIndexBitcodeReader::error(BitcodeError E) {
5399 return ::error(DiagnosticHandler, make_error_code(E));
5400}
5401
5402FunctionIndexBitcodeReader::FunctionIndexBitcodeReader(
Mehdi Amini354f5202015-11-19 05:52:29 +00005403 MemoryBuffer *Buffer, DiagnosticHandlerFunction DiagnosticHandler,
5404 bool IsLazy, bool CheckFuncSummaryPresenceOnly)
5405 : DiagnosticHandler(DiagnosticHandler), Buffer(Buffer), IsLazy(IsLazy),
Teresa Johnson403a7872015-10-04 14:33:43 +00005406 CheckFuncSummaryPresenceOnly(CheckFuncSummaryPresenceOnly) {}
5407
5408FunctionIndexBitcodeReader::FunctionIndexBitcodeReader(
Mehdi Amini354f5202015-11-19 05:52:29 +00005409 DiagnosticHandlerFunction DiagnosticHandler, bool IsLazy,
5410 bool CheckFuncSummaryPresenceOnly)
5411 : DiagnosticHandler(DiagnosticHandler), Buffer(nullptr), IsLazy(IsLazy),
Teresa Johnson403a7872015-10-04 14:33:43 +00005412 CheckFuncSummaryPresenceOnly(CheckFuncSummaryPresenceOnly) {}
5413
5414void FunctionIndexBitcodeReader::freeState() { Buffer = nullptr; }
5415
5416void FunctionIndexBitcodeReader::releaseBuffer() { Buffer.release(); }
5417
5418// Specialized value symbol table parser used when reading function index
5419// blocks where we don't actually create global values.
5420// At the end of this routine the function index is populated with a map
5421// from function name to FunctionInfo. The function info contains
5422// the function block's bitcode offset as well as the offset into the
5423// function summary section.
5424std::error_code FunctionIndexBitcodeReader::parseValueSymbolTable() {
5425 if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
5426 return error("Invalid record");
5427
5428 SmallVector<uint64_t, 64> Record;
5429
5430 // Read all the records for this value table.
5431 SmallString<128> ValueName;
5432 while (1) {
5433 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
5434
5435 switch (Entry.Kind) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005436 case BitstreamEntry::SubBlock: // Handled for us already.
5437 case BitstreamEntry::Error:
5438 return error("Malformed block");
5439 case BitstreamEntry::EndBlock:
5440 return std::error_code();
5441 case BitstreamEntry::Record:
5442 // The interesting case.
5443 break;
Teresa Johnson403a7872015-10-04 14:33:43 +00005444 }
5445
5446 // Read a record.
5447 Record.clear();
5448 switch (Stream.readRecord(Entry.ID, Record)) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005449 default: // Default behavior: ignore (e.g. VST_CODE_BBENTRY records).
5450 break;
5451 case bitc::VST_CODE_FNENTRY: {
5452 // VST_FNENTRY: [valueid, offset, namechar x N]
5453 if (convertToString(Record, 2, ValueName))
5454 return error("Invalid record");
5455 unsigned ValueID = Record[0];
5456 uint64_t FuncOffset = Record[1];
5457 std::unique_ptr<FunctionInfo> FuncInfo =
5458 llvm::make_unique<FunctionInfo>(FuncOffset);
5459 if (foundFuncSummary() && !IsLazy) {
5460 DenseMap<uint64_t, std::unique_ptr<FunctionSummary>>::iterator SMI =
5461 SummaryMap.find(ValueID);
5462 assert(SMI != SummaryMap.end() && "Summary info not found");
5463 FuncInfo->setFunctionSummary(std::move(SMI->second));
Teresa Johnson403a7872015-10-04 14:33:43 +00005464 }
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005465 TheIndex->addFunctionInfo(ValueName, std::move(FuncInfo));
Teresa Johnson403a7872015-10-04 14:33:43 +00005466
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005467 ValueName.clear();
5468 break;
5469 }
5470 case bitc::VST_CODE_COMBINED_FNENTRY: {
5471 // VST_FNENTRY: [offset, namechar x N]
5472 if (convertToString(Record, 1, ValueName))
5473 return error("Invalid record");
5474 uint64_t FuncSummaryOffset = Record[0];
5475 std::unique_ptr<FunctionInfo> FuncInfo =
5476 llvm::make_unique<FunctionInfo>(FuncSummaryOffset);
5477 if (foundFuncSummary() && !IsLazy) {
5478 DenseMap<uint64_t, std::unique_ptr<FunctionSummary>>::iterator SMI =
5479 SummaryMap.find(FuncSummaryOffset);
5480 assert(SMI != SummaryMap.end() && "Summary info not found");
5481 FuncInfo->setFunctionSummary(std::move(SMI->second));
Teresa Johnson403a7872015-10-04 14:33:43 +00005482 }
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005483 TheIndex->addFunctionInfo(ValueName, std::move(FuncInfo));
5484
5485 ValueName.clear();
5486 break;
5487 }
Teresa Johnson403a7872015-10-04 14:33:43 +00005488 }
5489 }
5490}
5491
5492// Parse just the blocks needed for function index building out of the module.
5493// At the end of this routine the function Index is populated with a map
5494// from function name to FunctionInfo. The function info contains
5495// either the parsed function summary information (when parsing summaries
5496// eagerly), or just to the function summary record's offset
5497// if parsing lazily (IsLazy).
5498std::error_code FunctionIndexBitcodeReader::parseModule() {
5499 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
5500 return error("Invalid record");
5501
5502 // Read the function index for this module.
5503 while (1) {
5504 BitstreamEntry Entry = Stream.advance();
5505
5506 switch (Entry.Kind) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005507 case BitstreamEntry::Error:
5508 return error("Malformed block");
5509 case BitstreamEntry::EndBlock:
5510 return std::error_code();
5511
5512 case BitstreamEntry::SubBlock:
5513 if (CheckFuncSummaryPresenceOnly) {
Teresa Johnson6290dbc2015-11-21 21:55:48 +00005514 if (Entry.ID == bitc::FUNCTION_SUMMARY_BLOCK_ID) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005515 SeenFuncSummary = true;
Teresa Johnson6290dbc2015-11-21 21:55:48 +00005516 // No need to parse the rest since we found the summary.
5517 return std::error_code();
5518 }
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005519 if (Stream.SkipBlock())
5520 return error("Invalid record");
Teresa Johnson6290dbc2015-11-21 21:55:48 +00005521 continue;
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005522 }
5523 switch (Entry.ID) {
5524 default: // Skip unknown content.
5525 if (Stream.SkipBlock())
5526 return error("Invalid record");
5527 break;
5528 case bitc::BLOCKINFO_BLOCK_ID:
5529 // Need to parse these to get abbrev ids (e.g. for VST)
5530 if (Stream.ReadBlockInfoBlock())
5531 return error("Malformed block");
5532 break;
5533 case bitc::VALUE_SYMTAB_BLOCK_ID:
5534 if (std::error_code EC = parseValueSymbolTable())
5535 return EC;
5536 break;
5537 case bitc::FUNCTION_SUMMARY_BLOCK_ID:
5538 SeenFuncSummary = true;
5539 if (IsLazy) {
5540 // Lazy parsing of summary info, skip it.
5541 if (Stream.SkipBlock())
5542 return error("Invalid record");
5543 } else if (std::error_code EC = parseEntireSummary())
5544 return EC;
5545 break;
5546 case bitc::MODULE_STRTAB_BLOCK_ID:
5547 if (std::error_code EC = parseModuleStringTable())
5548 return EC;
5549 break;
5550 }
5551 continue;
Teresa Johnson403a7872015-10-04 14:33:43 +00005552
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005553 case BitstreamEntry::Record:
5554 Stream.skipRecord(Entry.ID);
5555 continue;
Teresa Johnson403a7872015-10-04 14:33:43 +00005556 }
5557 }
5558}
5559
5560// Eagerly parse the entire function summary block (i.e. for all functions
5561// in the index). This populates the FunctionSummary objects in
5562// the index.
5563std::error_code FunctionIndexBitcodeReader::parseEntireSummary() {
5564 if (Stream.EnterSubBlock(bitc::FUNCTION_SUMMARY_BLOCK_ID))
5565 return error("Invalid record");
5566
5567 SmallVector<uint64_t, 64> Record;
5568
5569 while (1) {
5570 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
5571
5572 switch (Entry.Kind) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005573 case BitstreamEntry::SubBlock: // Handled for us already.
5574 case BitstreamEntry::Error:
5575 return error("Malformed block");
5576 case BitstreamEntry::EndBlock:
5577 return std::error_code();
5578 case BitstreamEntry::Record:
5579 // The interesting case.
5580 break;
Teresa Johnson403a7872015-10-04 14:33:43 +00005581 }
5582
5583 // Read a record. The record format depends on whether this
5584 // is a per-module index or a combined index file. In the per-module
5585 // case the records contain the associated value's ID for correlation
5586 // with VST entries. In the combined index the correlation is done
5587 // via the bitcode offset of the summary records (which were saved
5588 // in the combined index VST entries). The records also contain
5589 // information used for ThinLTO renaming and importing.
5590 Record.clear();
5591 uint64_t CurRecordBit = Stream.GetCurrentBitNo();
5592 switch (Stream.readRecord(Entry.ID, Record)) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005593 default: // Default behavior: ignore.
5594 break;
5595 // FS_PERMODULE_ENTRY: [valueid, islocal, instcount]
5596 case bitc::FS_CODE_PERMODULE_ENTRY: {
5597 unsigned ValueID = Record[0];
5598 bool IsLocal = Record[1];
5599 unsigned InstCount = Record[2];
5600 std::unique_ptr<FunctionSummary> FS =
5601 llvm::make_unique<FunctionSummary>(InstCount);
5602 FS->setLocalFunction(IsLocal);
5603 // The module path string ref set in the summary must be owned by the
5604 // index's module string table. Since we don't have a module path
5605 // string table section in the per-module index, we create a single
5606 // module path string table entry with an empty (0) ID to take
5607 // ownership.
5608 FS->setModulePath(
5609 TheIndex->addModulePath(Buffer->getBufferIdentifier(), 0));
5610 SummaryMap[ValueID] = std::move(FS);
5611 }
5612 // FS_COMBINED_ENTRY: [modid, instcount]
5613 case bitc::FS_CODE_COMBINED_ENTRY: {
5614 uint64_t ModuleId = Record[0];
5615 unsigned InstCount = Record[1];
5616 std::unique_ptr<FunctionSummary> FS =
5617 llvm::make_unique<FunctionSummary>(InstCount);
5618 FS->setModulePath(ModuleIdMap[ModuleId]);
5619 SummaryMap[CurRecordBit] = std::move(FS);
5620 }
Teresa Johnson403a7872015-10-04 14:33:43 +00005621 }
5622 }
5623 llvm_unreachable("Exit infinite loop");
5624}
5625
5626// Parse the module string table block into the Index.
5627// This populates the ModulePathStringTable map in the index.
5628std::error_code FunctionIndexBitcodeReader::parseModuleStringTable() {
5629 if (Stream.EnterSubBlock(bitc::MODULE_STRTAB_BLOCK_ID))
5630 return error("Invalid record");
5631
5632 SmallVector<uint64_t, 64> Record;
5633
5634 SmallString<128> ModulePath;
5635 while (1) {
5636 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
5637
5638 switch (Entry.Kind) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005639 case BitstreamEntry::SubBlock: // Handled for us already.
5640 case BitstreamEntry::Error:
5641 return error("Malformed block");
5642 case BitstreamEntry::EndBlock:
5643 return std::error_code();
5644 case BitstreamEntry::Record:
5645 // The interesting case.
5646 break;
Teresa Johnson403a7872015-10-04 14:33:43 +00005647 }
5648
5649 Record.clear();
5650 switch (Stream.readRecord(Entry.ID, Record)) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005651 default: // Default behavior: ignore.
5652 break;
5653 case bitc::MST_CODE_ENTRY: {
5654 // MST_ENTRY: [modid, namechar x N]
5655 if (convertToString(Record, 1, ModulePath))
5656 return error("Invalid record");
5657 uint64_t ModuleId = Record[0];
5658 StringRef ModulePathInMap = TheIndex->addModulePath(ModulePath, ModuleId);
5659 ModuleIdMap[ModuleId] = ModulePathInMap;
5660 ModulePath.clear();
5661 break;
5662 }
Teresa Johnson403a7872015-10-04 14:33:43 +00005663 }
5664 }
5665 llvm_unreachable("Exit infinite loop");
5666}
5667
5668// Parse the function info index from the bitcode streamer into the given index.
5669std::error_code FunctionIndexBitcodeReader::parseSummaryIndexInto(
5670 std::unique_ptr<DataStreamer> Streamer, FunctionInfoIndex *I) {
5671 TheIndex = I;
5672
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005673 if (std::error_code EC = initStream(std::move(Streamer)))
5674 return EC;
Teresa Johnson403a7872015-10-04 14:33:43 +00005675
5676 // Sniff for the signature.
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005677 if (!hasValidBitcodeHeader(Stream))
5678 return error("Invalid bitcode signature");
Teresa Johnson403a7872015-10-04 14:33:43 +00005679
5680 // We expect a number of well-defined blocks, though we don't necessarily
5681 // need to understand them all.
5682 while (1) {
5683 if (Stream.AtEndOfStream()) {
5684 // We didn't really read a proper Module block.
5685 return error("Malformed block");
5686 }
5687
5688 BitstreamEntry Entry =
5689 Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
5690
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005691 if (Entry.Kind != BitstreamEntry::SubBlock)
5692 return error("Malformed block");
Teresa Johnson403a7872015-10-04 14:33:43 +00005693
5694 // If we see a MODULE_BLOCK, parse it to find the blocks needed for
5695 // building the function summary index.
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005696 if (Entry.ID == bitc::MODULE_BLOCK_ID)
5697 return parseModule();
Teresa Johnson403a7872015-10-04 14:33:43 +00005698
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005699 if (Stream.SkipBlock())
5700 return error("Invalid record");
Teresa Johnson403a7872015-10-04 14:33:43 +00005701 }
5702}
5703
5704// Parse the function information at the given offset in the buffer into
5705// the index. Used to support lazy parsing of function summaries from the
5706// combined index during importing.
5707// TODO: This function is not yet complete as it won't have a consumer
5708// until ThinLTO function importing is added.
5709std::error_code FunctionIndexBitcodeReader::parseFunctionSummary(
5710 std::unique_ptr<DataStreamer> Streamer, FunctionInfoIndex *I,
5711 size_t FunctionSummaryOffset) {
5712 TheIndex = I;
5713
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005714 if (std::error_code EC = initStream(std::move(Streamer)))
5715 return EC;
Teresa Johnson403a7872015-10-04 14:33:43 +00005716
5717 // Sniff for the signature.
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005718 if (!hasValidBitcodeHeader(Stream))
5719 return error("Invalid bitcode signature");
Teresa Johnson403a7872015-10-04 14:33:43 +00005720
5721 Stream.JumpToBit(FunctionSummaryOffset);
5722
5723 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
5724
5725 switch (Entry.Kind) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005726 default:
5727 return error("Malformed block");
5728 case BitstreamEntry::Record:
5729 // The expected case.
5730 break;
Teresa Johnson403a7872015-10-04 14:33:43 +00005731 }
5732
5733 // TODO: Read a record. This interface will be completed when ThinLTO
5734 // importing is added so that it can be tested.
5735 SmallVector<uint64_t, 64> Record;
5736 switch (Stream.readRecord(Entry.ID, Record)) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005737 case bitc::FS_CODE_COMBINED_ENTRY:
5738 default:
5739 return error("Invalid record");
Teresa Johnson403a7872015-10-04 14:33:43 +00005740 }
5741
5742 return std::error_code();
5743}
5744
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005745std::error_code
5746FunctionIndexBitcodeReader::initStream(std::unique_ptr<DataStreamer> Streamer) {
5747 if (Streamer)
5748 return initLazyStream(std::move(Streamer));
Teresa Johnson403a7872015-10-04 14:33:43 +00005749 return initStreamFromBuffer();
5750}
5751
5752std::error_code FunctionIndexBitcodeReader::initStreamFromBuffer() {
5753 const unsigned char *BufPtr = (const unsigned char *)Buffer->getBufferStart();
5754 const unsigned char *BufEnd = BufPtr + Buffer->getBufferSize();
5755
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005756 if (Buffer->getBufferSize() & 3)
5757 return error("Invalid bitcode signature");
Teresa Johnson403a7872015-10-04 14:33:43 +00005758
5759 // If we have a wrapper header, parse it and ignore the non-bc file contents.
5760 // The magic number is 0x0B17C0DE stored in little endian.
5761 if (isBitcodeWrapper(BufPtr, BufEnd))
5762 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
5763 return error("Invalid bitcode wrapper header");
5764
5765 StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
5766 Stream.init(&*StreamFile);
5767
5768 return std::error_code();
5769}
5770
5771std::error_code FunctionIndexBitcodeReader::initLazyStream(
5772 std::unique_ptr<DataStreamer> Streamer) {
5773 // Check and strip off the bitcode wrapper; BitstreamReader expects never to
5774 // see it.
5775 auto OwnedBytes =
5776 llvm::make_unique<StreamingMemoryObject>(std::move(Streamer));
5777 StreamingMemoryObject &Bytes = *OwnedBytes;
5778 StreamFile = llvm::make_unique<BitstreamReader>(std::move(OwnedBytes));
5779 Stream.init(&*StreamFile);
5780
5781 unsigned char buf[16];
5782 if (Bytes.readBytes(buf, 16, 0) != 16)
5783 return error("Invalid bitcode signature");
5784
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005785 if (!isBitcode(buf, buf + 16))
5786 return error("Invalid bitcode signature");
Teresa Johnson403a7872015-10-04 14:33:43 +00005787
5788 if (isBitcodeWrapper(buf, buf + 4)) {
5789 const unsigned char *bitcodeStart = buf;
5790 const unsigned char *bitcodeEnd = buf + 16;
5791 SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
5792 Bytes.dropLeadingBytes(bitcodeStart - buf);
5793 Bytes.setKnownObjectSize(bitcodeEnd - bitcodeStart);
5794 }
5795 return std::error_code();
5796}
5797
Rafael Espindola48da4f42013-11-04 16:16:24 +00005798namespace {
Rafael Espindola25188c92014-06-12 01:45:43 +00005799class BitcodeErrorCategoryType : public std::error_category {
Rafael Espindolaf5d07fa2014-06-10 21:26:47 +00005800 const char *name() const LLVM_NOEXCEPT override {
Rafael Espindola48da4f42013-11-04 16:16:24 +00005801 return "llvm.bitcode";
5802 }
Craig Topper73156022014-03-02 09:09:27 +00005803 std::string message(int IE) const override {
Rafael Espindolac3f2e732014-07-29 20:22:46 +00005804 BitcodeError E = static_cast<BitcodeError>(IE);
Rafael Espindola48da4f42013-11-04 16:16:24 +00005805 switch (E) {
Rafael Espindolac3f2e732014-07-29 20:22:46 +00005806 case BitcodeError::InvalidBitcodeSignature:
Rafael Espindola48da4f42013-11-04 16:16:24 +00005807 return "Invalid bitcode signature";
Rafael Espindolad0b23be2015-01-10 00:07:30 +00005808 case BitcodeError::CorruptedBitcode:
5809 return "Corrupted bitcode";
Rafael Espindola48da4f42013-11-04 16:16:24 +00005810 }
Benjamin Kramer77db1632013-11-05 13:45:09 +00005811 llvm_unreachable("Unknown error type!");
Rafael Espindola48da4f42013-11-04 16:16:24 +00005812 }
5813};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00005814}
Rafael Espindola48da4f42013-11-04 16:16:24 +00005815
Chris Bieneman770163e2014-09-19 20:29:02 +00005816static ManagedStatic<BitcodeErrorCategoryType> ErrorCategory;
5817
Rafael Espindolac3f2e732014-07-29 20:22:46 +00005818const std::error_category &llvm::BitcodeErrorCategory() {
Chris Bieneman770163e2014-09-19 20:29:02 +00005819 return *ErrorCategory;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005820}
Chris Lattner51ffe7c2007-05-01 04:59:48 +00005821
Chris Lattner6694f602007-04-29 07:54:31 +00005822//===----------------------------------------------------------------------===//
5823// External interface
5824//===----------------------------------------------------------------------===//
5825
Rafael Espindola456baad2015-06-17 01:15:47 +00005826static ErrorOr<std::unique_ptr<Module>>
5827getBitcodeModuleImpl(std::unique_ptr<DataStreamer> Streamer, StringRef Name,
5828 BitcodeReader *R, LLVMContext &Context,
5829 bool MaterializeAll, bool ShouldLazyLoadMetadata) {
5830 std::unique_ptr<Module> M = make_unique<Module>(Name, Context);
5831 M->setMaterializer(R);
5832
5833 auto cleanupOnError = [&](std::error_code EC) {
5834 R->releaseBuffer(); // Never take ownership on error.
5835 return EC;
5836 };
5837
5838 // Delay parsing Metadata if ShouldLazyLoadMetadata is true.
5839 if (std::error_code EC = R->parseBitcodeInto(std::move(Streamer), M.get(),
5840 ShouldLazyLoadMetadata))
5841 return cleanupOnError(EC);
5842
5843 if (MaterializeAll) {
5844 // Read in the entire module, and destroy the BitcodeReader.
5845 if (std::error_code EC = M->materializeAllPermanently())
5846 return cleanupOnError(EC);
5847 } else {
5848 // Resolve forward references from blockaddresses.
5849 if (std::error_code EC = R->materializeForwardReferencedFunctions())
5850 return cleanupOnError(EC);
5851 }
5852 return std::move(M);
5853}
5854
Duncan P. N. Exon Smith6e1009b2014-08-01 22:27:19 +00005855/// \brief Get a lazy one-at-time loading module from bitcode.
Chris Lattner6694f602007-04-29 07:54:31 +00005856///
Duncan P. N. Exon Smith6e1009b2014-08-01 22:27:19 +00005857/// This isn't always used in a lazy context. In particular, it's also used by
5858/// \a parseBitcodeFile(). If this is truly lazy, then we need to eagerly pull
5859/// in forward-referenced functions from block address references.
5860///
Rafael Espindola728074b2015-06-17 00:40:56 +00005861/// \param[in] MaterializeAll Set to \c true if we should materialize
5862/// everything.
Rafael Espindoladcd1dca2015-06-16 22:27:55 +00005863static ErrorOr<std::unique_ptr<Module>>
Rafael Espindola68812152014-09-03 17:31:46 +00005864getLazyBitcodeModuleImpl(std::unique_ptr<MemoryBuffer> &&Buffer,
Rafael Espindola728074b2015-06-17 00:40:56 +00005865 LLVMContext &Context, bool MaterializeAll,
Manman Ren4a9b0eb2015-03-13 19:24:30 +00005866 DiagnosticHandlerFunction DiagnosticHandler,
5867 bool ShouldLazyLoadMetadata = false) {
Rafael Espindolad0b23be2015-01-10 00:07:30 +00005868 BitcodeReader *R =
5869 new BitcodeReader(Buffer.get(), Context, DiagnosticHandler);
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00005870
Rafael Espindola456baad2015-06-17 01:15:47 +00005871 ErrorOr<std::unique_ptr<Module>> Ret =
5872 getBitcodeModuleImpl(nullptr, Buffer->getBufferIdentifier(), R, Context,
5873 MaterializeAll, ShouldLazyLoadMetadata);
5874 if (!Ret)
5875 return Ret;
Rafael Espindolab7993462012-01-02 07:49:53 +00005876
Rafael Espindolae2c1d772014-08-26 22:00:09 +00005877 Buffer.release(); // The BitcodeReader owns it now.
Rafael Espindola456baad2015-06-17 01:15:47 +00005878 return Ret;
Chris Lattner6694f602007-04-29 07:54:31 +00005879}
5880
Rafael Espindoladcd1dca2015-06-16 22:27:55 +00005881ErrorOr<std::unique_ptr<Module>> llvm::getLazyBitcodeModule(
5882 std::unique_ptr<MemoryBuffer> &&Buffer, LLVMContext &Context,
5883 DiagnosticHandlerFunction DiagnosticHandler, bool ShouldLazyLoadMetadata) {
Rafael Espindolad0b23be2015-01-10 00:07:30 +00005884 return getLazyBitcodeModuleImpl(std::move(Buffer), Context, false,
Manman Ren4a9b0eb2015-03-13 19:24:30 +00005885 DiagnosticHandler, ShouldLazyLoadMetadata);
Duncan P. N. Exon Smith6e1009b2014-08-01 22:27:19 +00005886}
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005887
Rafael Espindola1aabf982015-06-16 23:29:49 +00005888ErrorOr<std::unique_ptr<Module>> llvm::getStreamedBitcodeModule(
5889 StringRef Name, std::unique_ptr<DataStreamer> Streamer,
5890 LLVMContext &Context, DiagnosticHandlerFunction DiagnosticHandler) {
Rafael Espindola7d727b52014-12-18 05:08:43 +00005891 std::unique_ptr<Module> M = make_unique<Module>(Name, Context);
Rafael Espindola1aabf982015-06-16 23:29:49 +00005892 BitcodeReader *R = new BitcodeReader(Context, DiagnosticHandler);
Rafael Espindola456baad2015-06-17 01:15:47 +00005893
5894 return getBitcodeModuleImpl(std::move(Streamer), Name, R, Context, false,
5895 false);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005896}
5897
Rafael Espindoladcd1dca2015-06-16 22:27:55 +00005898ErrorOr<std::unique_ptr<Module>>
Rafael Espindolad0b23be2015-01-10 00:07:30 +00005899llvm::parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context,
5900 DiagnosticHandlerFunction DiagnosticHandler) {
Rafael Espindolad96d5532014-08-26 21:49:01 +00005901 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
Rafael Espindola728074b2015-06-17 00:40:56 +00005902 return getLazyBitcodeModuleImpl(std::move(Buf), Context, true,
5903 DiagnosticHandler);
Chad Rosierca2567b2011-12-07 21:44:12 +00005904 // TODO: Restore the use-lists to the in-memory state when the bitcode was
5905 // written. We must defer until the Module has been fully materialized.
Chris Lattner6694f602007-04-29 07:54:31 +00005906}
Bill Wendling0198ce02010-10-06 01:22:42 +00005907
Rafael Espindolad0b23be2015-01-10 00:07:30 +00005908std::string
5909llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer, LLVMContext &Context,
5910 DiagnosticHandlerFunction DiagnosticHandler) {
Rafael Espindolad96d5532014-08-26 21:49:01 +00005911 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
Rafael Espindolad0b23be2015-01-10 00:07:30 +00005912 auto R = llvm::make_unique<BitcodeReader>(Buf.release(), Context,
5913 DiagnosticHandler);
Rafael Espindolac75c4fa2014-07-04 20:02:42 +00005914 ErrorOr<std::string> Triple = R->parseTriple();
Rafael Espindolad346cc82014-07-04 13:52:01 +00005915 if (Triple.getError())
5916 return "";
5917 return Triple.get();
Bill Wendling0198ce02010-10-06 01:22:42 +00005918}
Teresa Johnson403a7872015-10-04 14:33:43 +00005919
Mehdi Amini3383ccc2015-11-09 02:46:41 +00005920std::string
5921llvm::getBitcodeProducerString(MemoryBufferRef Buffer, LLVMContext &Context,
5922 DiagnosticHandlerFunction DiagnosticHandler) {
5923 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
5924 BitcodeReader R(Buf.release(), Context, DiagnosticHandler);
5925 ErrorOr<std::string> ProducerString = R.parseIdentificationBlock();
5926 if (ProducerString.getError())
5927 return "";
5928 return ProducerString.get();
5929}
5930
Teresa Johnson403a7872015-10-04 14:33:43 +00005931// Parse the specified bitcode buffer, returning the function info index.
5932// If IsLazy is false, parse the entire function summary into
5933// the index. Otherwise skip the function summary section, and only create
5934// an index object with a map from function name to function summary offset.
5935// The index is used to perform lazy function summary reading later.
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005936ErrorOr<std::unique_ptr<FunctionInfoIndex>>
Mehdi Amini354f5202015-11-19 05:52:29 +00005937llvm::getFunctionInfoIndex(MemoryBufferRef Buffer,
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005938 DiagnosticHandlerFunction DiagnosticHandler,
Mehdi Amini9abe1082015-12-03 02:37:23 +00005939 bool IsLazy) {
Teresa Johnson403a7872015-10-04 14:33:43 +00005940 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
Mehdi Amini354f5202015-11-19 05:52:29 +00005941 FunctionIndexBitcodeReader R(Buf.get(), DiagnosticHandler, IsLazy);
Teresa Johnson403a7872015-10-04 14:33:43 +00005942
Mehdi Amini9abe1082015-12-03 02:37:23 +00005943 auto Index = llvm::make_unique<FunctionInfoIndex>();
Teresa Johnson403a7872015-10-04 14:33:43 +00005944
5945 auto cleanupOnError = [&](std::error_code EC) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005946 R.releaseBuffer(); // Never take ownership on error.
Teresa Johnson403a7872015-10-04 14:33:43 +00005947 return EC;
5948 };
5949
5950 if (std::error_code EC = R.parseSummaryIndexInto(nullptr, Index.get()))
5951 return cleanupOnError(EC);
5952
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005953 Buf.release(); // The FunctionIndexBitcodeReader owns it now.
Teresa Johnson403a7872015-10-04 14:33:43 +00005954 return std::move(Index);
5955}
5956
5957// Check if the given bitcode buffer contains a function summary block.
Mehdi Amini354f5202015-11-19 05:52:29 +00005958bool llvm::hasFunctionSummary(MemoryBufferRef Buffer,
Teresa Johnson403a7872015-10-04 14:33:43 +00005959 DiagnosticHandlerFunction DiagnosticHandler) {
5960 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
Mehdi Amini354f5202015-11-19 05:52:29 +00005961 FunctionIndexBitcodeReader R(Buf.get(), DiagnosticHandler, false, true);
Teresa Johnson403a7872015-10-04 14:33:43 +00005962
5963 auto cleanupOnError = [&](std::error_code EC) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005964 R.releaseBuffer(); // Never take ownership on error.
Teresa Johnson403a7872015-10-04 14:33:43 +00005965 return false;
5966 };
5967
5968 if (std::error_code EC = R.parseSummaryIndexInto(nullptr, nullptr))
5969 return cleanupOnError(EC);
5970
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005971 Buf.release(); // The FunctionIndexBitcodeReader owns it now.
Teresa Johnson403a7872015-10-04 14:33:43 +00005972 return R.foundFuncSummary();
5973}
5974
5975// This method supports lazy reading of function summary data from the combined
5976// index during ThinLTO function importing. When reading the combined index
5977// file, getFunctionInfoIndex is first invoked with IsLazy=true.
5978// Then this method is called for each function considered for importing,
5979// to parse the summary information for the given function name into
5980// the index.
Mehdi Amini354f5202015-11-19 05:52:29 +00005981std::error_code llvm::readFunctionSummary(
5982 MemoryBufferRef Buffer, DiagnosticHandlerFunction DiagnosticHandler,
5983 StringRef FunctionName, std::unique_ptr<FunctionInfoIndex> Index) {
Teresa Johnson403a7872015-10-04 14:33:43 +00005984 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
Mehdi Amini354f5202015-11-19 05:52:29 +00005985 FunctionIndexBitcodeReader R(Buf.get(), DiagnosticHandler);
Teresa Johnson403a7872015-10-04 14:33:43 +00005986
5987 auto cleanupOnError = [&](std::error_code EC) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005988 R.releaseBuffer(); // Never take ownership on error.
Teresa Johnson403a7872015-10-04 14:33:43 +00005989 return EC;
5990 };
5991
5992 // Lookup the given function name in the FunctionMap, which may
5993 // contain a list of function infos in the case of a COMDAT. Walk through
5994 // and parse each function summary info at the function summary offset
5995 // recorded when parsing the value symbol table.
5996 for (const auto &FI : Index->getFunctionInfoList(FunctionName)) {
5997 size_t FunctionSummaryOffset = FI->bitcodeIndex();
5998 if (std::error_code EC =
5999 R.parseFunctionSummary(nullptr, Index.get(), FunctionSummaryOffset))
6000 return cleanupOnError(EC);
6001 }
6002
Teresa Johnsonf72278f2015-11-02 18:02:11 +00006003 Buf.release(); // The FunctionIndexBitcodeReader owns it now.
Teresa Johnson403a7872015-10-04 14:33:43 +00006004 return std::error_code();
6005}