blob: 72ac217bef2a3bcb025543b2bba06ecc83ae7555 [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
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000010#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000011#include "llvm/ADT/SmallString.h"
12#include "llvm/ADT/SmallVector.h"
David Majnemer3087b222015-01-20 05:58:07 +000013#include "llvm/ADT/Triple.h"
Benjamin Kramercced8be2015-03-17 20:40:24 +000014#include "llvm/Bitcode/BitstreamReader.h"
Tobias Grosser0a8e12f2013-07-26 04:16:55 +000015#include "llvm/Bitcode/LLVMBitCodes.h"
Teresa Johnson26ab5772016-03-15 00:04:37 +000016#include "llvm/Bitcode/ReaderWriter.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"
Teresa Johnson26ab5772016-03-15 00:04:37 +000028#include "llvm/IR/ModuleSummaryIndex.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000029#include "llvm/IR/OperandTraits.h"
30#include "llvm/IR/Operator.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>
Eugene Zelenko6ac3f732016-01-26 18:48:36 +000038
Chris Lattner1314b992007-04-22 06:23:29 +000039using namespace llvm;
40
Benjamin Kramercced8be2015-03-17 20:40:24 +000041namespace {
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +000042enum {
43 SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex
44};
45
Benjamin Kramercced8be2015-03-17 20:40:24 +000046class BitcodeReaderValueList {
47 std::vector<WeakVH> ValuePtrs;
48
Rafael Espindolacbdcb502015-06-15 20:55:37 +000049 /// As we resolve forward-referenced constants, we add information about them
50 /// to this vector. This allows us to resolve them in bulk instead of
51 /// resolving each reference at a time. See the code in
Benjamin Kramercced8be2015-03-17 20:40:24 +000052 /// ResolveConstantForwardRefs for more information about this.
53 ///
54 /// The key of this vector is the placeholder constant, the value is the slot
55 /// number that holds the resolved value.
56 typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy;
57 ResolveConstantsTy ResolveConstants;
58 LLVMContext &Context;
59public:
60 BitcodeReaderValueList(LLVMContext &C) : Context(C) {}
61 ~BitcodeReaderValueList() {
62 assert(ResolveConstants.empty() && "Constants not resolved?");
63 }
64
65 // vector compatibility methods
66 unsigned size() const { return ValuePtrs.size(); }
67 void resize(unsigned N) { ValuePtrs.resize(N); }
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +000068 void push_back(Value *V) { ValuePtrs.emplace_back(V); }
Benjamin Kramercced8be2015-03-17 20:40:24 +000069
70 void clear() {
71 assert(ResolveConstants.empty() && "Constants not resolved?");
72 ValuePtrs.clear();
73 }
74
75 Value *operator[](unsigned i) const {
76 assert(i < ValuePtrs.size());
77 return ValuePtrs[i];
78 }
79
80 Value *back() const { return ValuePtrs.back(); }
81 void pop_back() { ValuePtrs.pop_back(); }
82 bool empty() const { return ValuePtrs.empty(); }
83 void shrinkTo(unsigned N) {
84 assert(N <= size() && "Invalid shrinkTo request!");
85 ValuePtrs.resize(N);
86 }
87
88 Constant *getConstantFwdRef(unsigned Idx, Type *Ty);
David Majnemer8a1c45d2015-12-12 05:38:55 +000089 Value *getValueFwdRef(unsigned Idx, Type *Ty);
Benjamin Kramercced8be2015-03-17 20:40:24 +000090
David Majnemer8a1c45d2015-12-12 05:38:55 +000091 void assignValue(Value *V, unsigned Idx);
Benjamin Kramercced8be2015-03-17 20:40:24 +000092
Rafael Espindolacbdcb502015-06-15 20:55:37 +000093 /// Once all constants are read, this method bulk resolves any forward
94 /// references.
95 void resolveConstantForwardRefs();
Benjamin Kramercced8be2015-03-17 20:40:24 +000096};
97
Teresa Johnson61b406e2015-12-29 23:00:22 +000098class BitcodeReaderMetadataList {
Benjamin Kramercced8be2015-03-17 20:40:24 +000099 unsigned NumFwdRefs;
100 bool AnyFwdRefs;
101 unsigned MinFwdRef;
102 unsigned MaxFwdRef;
Teresa Johnson61b406e2015-12-29 23:00:22 +0000103 std::vector<TrackingMDRef> MetadataPtrs;
Benjamin Kramercced8be2015-03-17 20:40:24 +0000104
105 LLVMContext &Context;
106public:
Teresa Johnson61b406e2015-12-29 23:00:22 +0000107 BitcodeReaderMetadataList(LLVMContext &C)
Teresa Johnson34702952015-12-21 15:38:13 +0000108 : NumFwdRefs(0), AnyFwdRefs(false), Context(C) {}
Benjamin Kramercced8be2015-03-17 20:40:24 +0000109
110 // vector compatibility methods
Teresa Johnson61b406e2015-12-29 23:00:22 +0000111 unsigned size() const { return MetadataPtrs.size(); }
112 void resize(unsigned N) { MetadataPtrs.resize(N); }
113 void push_back(Metadata *MD) { MetadataPtrs.emplace_back(MD); }
114 void clear() { MetadataPtrs.clear(); }
115 Metadata *back() const { return MetadataPtrs.back(); }
116 void pop_back() { MetadataPtrs.pop_back(); }
117 bool empty() const { return MetadataPtrs.empty(); }
Benjamin Kramercced8be2015-03-17 20:40:24 +0000118
119 Metadata *operator[](unsigned i) const {
Teresa Johnson61b406e2015-12-29 23:00:22 +0000120 assert(i < MetadataPtrs.size());
121 return MetadataPtrs[i];
Benjamin Kramercced8be2015-03-17 20:40:24 +0000122 }
123
124 void shrinkTo(unsigned N) {
125 assert(N <= size() && "Invalid shrinkTo request!");
Teresa Johnson61b406e2015-12-29 23:00:22 +0000126 MetadataPtrs.resize(N);
Benjamin Kramercced8be2015-03-17 20:40:24 +0000127 }
128
Justin Bognerae341c62016-03-17 20:12:06 +0000129 Metadata *getMetadataFwdRef(unsigned Idx);
130 MDNode *getMDNodeFwdRefOrNull(unsigned Idx);
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000131 void assignValue(Metadata *MD, unsigned Idx);
Benjamin Kramercced8be2015-03-17 20:40:24 +0000132 void tryToResolveCycles();
133};
134
135class BitcodeReader : public GVMaterializer {
136 LLVMContext &Context;
Rafael Espindola4223a1f2015-06-15 20:08:17 +0000137 Module *TheModule = nullptr;
Benjamin Kramercced8be2015-03-17 20:40:24 +0000138 std::unique_ptr<MemoryBuffer> Buffer;
139 std::unique_ptr<BitstreamReader> StreamFile;
140 BitstreamCursor Stream;
Teresa Johnson1493ad92015-10-10 14:18:36 +0000141 // Next offset to start scanning for lazy parsing of function bodies.
Rafael Espindola4223a1f2015-06-15 20:08:17 +0000142 uint64_t NextUnreadBit = 0;
Teresa Johnson1493ad92015-10-10 14:18:36 +0000143 // Last function offset found in the VST.
144 uint64_t LastFunctionBlockBit = 0;
Rafael Espindola4223a1f2015-06-15 20:08:17 +0000145 bool SeenValueSymbolTable = false;
Peter Collingbourne128a9762015-10-27 23:01:25 +0000146 uint64_t VSTOffset = 0;
Mehdi Amini5d303282015-10-26 18:37:00 +0000147 // Contains an arbitrary and optional string identifying the bitcode producer
148 std::string ProducerIdentification;
Benjamin Kramercced8be2015-03-17 20:40:24 +0000149
150 std::vector<Type*> TypeList;
151 BitcodeReaderValueList ValueList;
Teresa Johnson61b406e2015-12-29 23:00:22 +0000152 BitcodeReaderMetadataList MetadataList;
Benjamin Kramercced8be2015-03-17 20:40:24 +0000153 std::vector<Comdat *> ComdatList;
154 SmallVector<Instruction *, 64> InstructionList;
155
156 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
157 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
158 std::vector<std::pair<Function*, unsigned> > FunctionPrefixes;
159 std::vector<std::pair<Function*, unsigned> > FunctionPrologues;
David Majnemer7fddecc2015-06-17 20:52:32 +0000160 std::vector<std::pair<Function*, unsigned> > FunctionPersonalityFns;
Benjamin Kramercced8be2015-03-17 20:40:24 +0000161
162 SmallVector<Instruction*, 64> InstsWithTBAATag;
163
Duncan P. N. Exon Smithefe16c82016-03-25 00:56:13 +0000164 bool HasSeenOldLoopTags = false;
165
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000166 /// The set of attributes by index. Index zero in the file is for null, and
167 /// is thus not represented here. As such all indices are off by one.
Benjamin Kramercced8be2015-03-17 20:40:24 +0000168 std::vector<AttributeSet> MAttributes;
169
Karl Schimpf36440082015-08-31 16:43:55 +0000170 /// The set of attribute groups.
Benjamin Kramercced8be2015-03-17 20:40:24 +0000171 std::map<unsigned, AttributeSet> MAttributeGroups;
172
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000173 /// While parsing a function body, this is a list of the basic blocks for the
174 /// function.
Benjamin Kramercced8be2015-03-17 20:40:24 +0000175 std::vector<BasicBlock*> FunctionBBs;
176
177 // When reading the module header, this list is populated with functions that
178 // have bodies later in the file.
179 std::vector<Function*> FunctionsWithBodies;
180
181 // When intrinsic functions are encountered which require upgrading they are
182 // stored here with their replacement function.
Rafael Espindola4e721212015-07-02 16:22:40 +0000183 typedef DenseMap<Function*, Function*> UpgradedIntrinsicMap;
Benjamin Kramercced8be2015-03-17 20:40:24 +0000184 UpgradedIntrinsicMap UpgradedIntrinsics;
185
186 // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
187 DenseMap<unsigned, unsigned> MDKindMap;
188
189 // Several operations happen after the module header has been read, but
190 // before function bodies are processed. This keeps track of whether
191 // we've done this yet.
Rafael Espindola4223a1f2015-06-15 20:08:17 +0000192 bool SeenFirstFunctionBody = false;
Benjamin Kramercced8be2015-03-17 20:40:24 +0000193
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000194 /// When function bodies are initially scanned, this map contains info about
195 /// where to find deferred function body in the stream.
Benjamin Kramercced8be2015-03-17 20:40:24 +0000196 DenseMap<Function*, uint64_t> DeferredFunctionInfo;
197
198 /// When Metadata block is initially scanned when parsing the module, we may
199 /// choose to defer parsing of the metadata. This vector contains info about
200 /// which Metadata blocks are deferred.
201 std::vector<uint64_t> DeferredMetadataInfo;
202
203 /// These are basic blocks forward-referenced by block addresses. They are
204 /// inserted lazily into functions when they're loaded. The basic block ID is
205 /// its index into the vector.
206 DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs;
207 std::deque<Function *> BasicBlockFwdRefQueue;
208
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000209 /// Indicates that we are using a new encoding for instruction operands where
210 /// most operands in the current FUNCTION_BLOCK are encoded relative to the
211 /// instruction number, for a more compact encoding. Some instruction
212 /// operands are not relative to the instruction ID: basic block numbers, and
213 /// types. Once the old style function blocks have been phased out, we would
Benjamin Kramercced8be2015-03-17 20:40:24 +0000214 /// not need this flag.
Rafael Espindola4223a1f2015-06-15 20:08:17 +0000215 bool UseRelativeIDs = false;
Benjamin Kramercced8be2015-03-17 20:40:24 +0000216
217 /// True if all functions will be materialized, negating the need to process
218 /// (e.g.) blockaddress forward references.
Rafael Espindola4223a1f2015-06-15 20:08:17 +0000219 bool WillMaterializeAllForwardRefs = false;
Benjamin Kramercced8be2015-03-17 20:40:24 +0000220
Benjamin Kramercced8be2015-03-17 20:40:24 +0000221 /// True if any Metadata block has been materialized.
Rafael Espindola4223a1f2015-06-15 20:08:17 +0000222 bool IsMetadataMaterialized = false;
Benjamin Kramercced8be2015-03-17 20:40:24 +0000223
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000224 bool StripDebugInfo = false;
225
Peter Collingbourned4bff302015-11-05 22:03:56 +0000226 /// Functions that need to be matched with subprograms when upgrading old
227 /// metadata.
228 SmallDenseMap<Function *, DISubprogram *, 16> FunctionsWithSPs;
229
Sanjoy Dasb513a9f2015-09-24 23:34:52 +0000230 std::vector<std::string> BundleTags;
231
Benjamin Kramercced8be2015-03-17 20:40:24 +0000232public:
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000233 std::error_code error(BitcodeError E, const Twine &Message);
234 std::error_code error(BitcodeError E);
235 std::error_code error(const Twine &Message);
Benjamin Kramercced8be2015-03-17 20:40:24 +0000236
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000237 BitcodeReader(MemoryBuffer *Buffer, LLVMContext &Context);
238 BitcodeReader(LLVMContext &Context);
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000239 ~BitcodeReader() override { freeState(); }
Benjamin Kramercced8be2015-03-17 20:40:24 +0000240
241 std::error_code materializeForwardReferencedFunctions();
242
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000243 void freeState();
Benjamin Kramercced8be2015-03-17 20:40:24 +0000244
245 void releaseBuffer();
246
Benjamin Kramercced8be2015-03-17 20:40:24 +0000247 std::error_code materialize(GlobalValue *GV) override;
Rafael Espindola79753a02015-12-18 21:18:57 +0000248 std::error_code materializeModule() override;
Benjamin Kramercced8be2015-03-17 20:40:24 +0000249 std::vector<StructType *> getIdentifiedStructTypes() const override;
Benjamin Kramercced8be2015-03-17 20:40:24 +0000250
Rafael Espindola6ace6852015-06-15 21:02:49 +0000251 /// \brief Main interface to parsing a bitcode buffer.
252 /// \returns true if an error occurred.
Rafael Espindola1aabf982015-06-16 23:29:49 +0000253 std::error_code parseBitcodeInto(std::unique_ptr<DataStreamer> Streamer,
254 Module *M,
Benjamin Kramercced8be2015-03-17 20:40:24 +0000255 bool ShouldLazyLoadMetadata = false);
256
Rafael Espindola6ace6852015-06-15 21:02:49 +0000257 /// \brief Cheap mechanism to just extract module triple
258 /// \returns true if an error occurred.
Benjamin Kramercced8be2015-03-17 20:40:24 +0000259 ErrorOr<std::string> parseTriple();
260
Mehdi Amini3383ccc2015-11-09 02:46:41 +0000261 /// Cheap mechanism to just extract the identification block out of bitcode.
262 ErrorOr<std::string> parseIdentificationBlock();
263
Benjamin Kramercced8be2015-03-17 20:40:24 +0000264 static uint64_t decodeSignRotatedValue(uint64_t V);
265
266 /// Materialize any deferred Metadata block.
267 std::error_code materializeMetadata() override;
268
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000269 void setStripDebugInfo() override;
270
Teresa Johnsone5a61912015-12-17 17:14:09 +0000271 /// Save the mapping between the metadata values and the corresponding
Teresa Johnson61b406e2015-12-29 23:00:22 +0000272 /// value id that were recorded in the MetadataList during parsing. If
Teresa Johnsone5a61912015-12-17 17:14:09 +0000273 /// OnlyTempMD is true, then only record those entries that are still
274 /// temporary metadata. This interface is used when metadata linking is
275 /// performed as a postpass, such as during function importing.
Teresa Johnson61b406e2015-12-29 23:00:22 +0000276 void saveMetadataList(DenseMap<const Metadata *, unsigned> &MetadataToIDs,
277 bool OnlyTempMD) override;
Teresa Johnsone5a61912015-12-17 17:14:09 +0000278
Benjamin Kramercced8be2015-03-17 20:40:24 +0000279private:
Mehdi Amini5d303282015-10-26 18:37:00 +0000280 /// Parse the "IDENTIFICATION_BLOCK_ID" block, populate the
281 // ProducerIdentification data member, and do some basic enforcement on the
282 // "epoch" encoded in the bitcode.
283 std::error_code parseBitcodeVersion();
284
Benjamin Kramercced8be2015-03-17 20:40:24 +0000285 std::vector<StructType *> IdentifiedStructTypes;
286 StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name);
287 StructType *createIdentifiedStructType(LLVMContext &Context);
288
289 Type *getTypeByID(unsigned ID);
David Majnemer8a1c45d2015-12-12 05:38:55 +0000290 Value *getFnValueByID(unsigned ID, Type *Ty) {
Benjamin Kramercced8be2015-03-17 20:40:24 +0000291 if (Ty && Ty->isMetadataTy())
292 return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID));
David Majnemer8a1c45d2015-12-12 05:38:55 +0000293 return ValueList.getValueFwdRef(ID, Ty);
Benjamin Kramercced8be2015-03-17 20:40:24 +0000294 }
295 Metadata *getFnMetadataByID(unsigned ID) {
Justin Bognerae341c62016-03-17 20:12:06 +0000296 return MetadataList.getMetadataFwdRef(ID);
Benjamin Kramercced8be2015-03-17 20:40:24 +0000297 }
298 BasicBlock *getBasicBlock(unsigned ID) const {
299 if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID
300 return FunctionBBs[ID];
301 }
302 AttributeSet getAttributes(unsigned i) const {
303 if (i-1 < MAttributes.size())
304 return MAttributes[i-1];
305 return AttributeSet();
306 }
307
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000308 /// Read a value/type pair out of the specified record from slot 'Slot'.
309 /// Increment Slot past the number of slots used in the record. Return true on
310 /// failure.
Benjamin Kramercced8be2015-03-17 20:40:24 +0000311 bool getValueTypePair(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
312 unsigned InstNum, Value *&ResVal) {
313 if (Slot == Record.size()) return true;
314 unsigned ValNo = (unsigned)Record[Slot++];
315 // Adjust the ValNo, if it was encoded relative to the InstNum.
316 if (UseRelativeIDs)
317 ValNo = InstNum - ValNo;
318 if (ValNo < InstNum) {
319 // If this is not a forward reference, just return the value we already
320 // have.
321 ResVal = getFnValueByID(ValNo, nullptr);
322 return ResVal == nullptr;
Benjamin Kramercced8be2015-03-17 20:40:24 +0000323 }
David Blaikiedbe6e0f2015-04-17 06:40:14 +0000324 if (Slot == Record.size())
325 return true;
Benjamin Kramercced8be2015-03-17 20:40:24 +0000326
327 unsigned TypeNo = (unsigned)Record[Slot++];
328 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
329 return ResVal == nullptr;
330 }
331
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000332 /// Read a value out of the specified record from slot 'Slot'. Increment Slot
333 /// past the number of slots used by the value in the record. Return true if
334 /// there is an error.
Benjamin Kramercced8be2015-03-17 20:40:24 +0000335 bool popValue(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
David Majnemer8a1c45d2015-12-12 05:38:55 +0000336 unsigned InstNum, Type *Ty, Value *&ResVal) {
337 if (getValue(Record, Slot, InstNum, Ty, ResVal))
Benjamin Kramercced8be2015-03-17 20:40:24 +0000338 return true;
339 // All values currently take a single record slot.
340 ++Slot;
341 return false;
342 }
343
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000344 /// Like popValue, but does not increment the Slot number.
Benjamin Kramercced8be2015-03-17 20:40:24 +0000345 bool getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
David Majnemer8a1c45d2015-12-12 05:38:55 +0000346 unsigned InstNum, Type *Ty, Value *&ResVal) {
347 ResVal = getValue(Record, Slot, InstNum, Ty);
Benjamin Kramercced8be2015-03-17 20:40:24 +0000348 return ResVal == nullptr;
349 }
350
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000351 /// Version of getValue that returns ResVal directly, or 0 if there is an
352 /// error.
Benjamin Kramercced8be2015-03-17 20:40:24 +0000353 Value *getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
David Majnemer8a1c45d2015-12-12 05:38:55 +0000354 unsigned InstNum, Type *Ty) {
Benjamin Kramercced8be2015-03-17 20:40:24 +0000355 if (Slot == Record.size()) return nullptr;
356 unsigned ValNo = (unsigned)Record[Slot];
357 // Adjust the ValNo, if it was encoded relative to the InstNum.
358 if (UseRelativeIDs)
359 ValNo = InstNum - ValNo;
David Majnemer8a1c45d2015-12-12 05:38:55 +0000360 return getFnValueByID(ValNo, Ty);
Benjamin Kramercced8be2015-03-17 20:40:24 +0000361 }
362
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000363 /// Like getValue, but decodes signed VBRs.
Benjamin Kramercced8be2015-03-17 20:40:24 +0000364 Value *getValueSigned(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
David Majnemer8a1c45d2015-12-12 05:38:55 +0000365 unsigned InstNum, Type *Ty) {
Benjamin Kramercced8be2015-03-17 20:40:24 +0000366 if (Slot == Record.size()) return nullptr;
367 unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]);
368 // Adjust the ValNo, if it was encoded relative to the InstNum.
369 if (UseRelativeIDs)
370 ValNo = InstNum - ValNo;
David Majnemer8a1c45d2015-12-12 05:38:55 +0000371 return getFnValueByID(ValNo, Ty);
Benjamin Kramercced8be2015-03-17 20:40:24 +0000372 }
373
374 /// Converts alignment exponent (i.e. power of two (or zero)) to the
375 /// corresponding alignment to use. If alignment is too large, returns
376 /// a corresponding error code.
377 std::error_code parseAlignmentValue(uint64_t Exponent, unsigned &Alignment);
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000378 std::error_code parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind);
Teresa Johnson1493ad92015-10-10 14:18:36 +0000379 std::error_code parseModule(uint64_t ResumeBit,
380 bool ShouldLazyLoadMetadata = false);
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000381 std::error_code parseAttributeBlock();
382 std::error_code parseAttributeGroupBlock();
383 std::error_code parseTypeTable();
384 std::error_code parseTypeTableBody();
Sanjoy Dasb513a9f2015-09-24 23:34:52 +0000385 std::error_code parseOperandBundleTags();
Benjamin Kramercced8be2015-03-17 20:40:24 +0000386
Teresa Johnsonff642b92015-09-17 20:12:00 +0000387 ErrorOr<Value *> recordValue(SmallVectorImpl<uint64_t> &Record,
388 unsigned NameIndex, Triple &TT);
Peter Collingbourne128a9762015-10-27 23:01:25 +0000389 std::error_code parseValueSymbolTable(uint64_t Offset = 0);
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000390 std::error_code parseConstants();
Teresa Johnson1493ad92015-10-10 14:18:36 +0000391 std::error_code rememberAndSkipFunctionBodies();
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000392 std::error_code rememberAndSkipFunctionBody();
Benjamin Kramercced8be2015-03-17 20:40:24 +0000393 /// Save the positions of the Metadata blocks and skip parsing the blocks.
394 std::error_code rememberAndSkipMetadata();
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000395 std::error_code parseFunctionBody(Function *F);
396 std::error_code globalCleanup();
397 std::error_code resolveGlobalAndAliasInits();
Teresa Johnsond4d3dfd2015-11-20 14:51:27 +0000398 std::error_code parseMetadata(bool ModuleLevel = false);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000399 std::error_code parseMetadataStrings(ArrayRef<uint64_t> Record,
400 StringRef Blob,
401 unsigned &NextMetadataNo);
Teresa Johnson12545072015-11-15 02:00:09 +0000402 std::error_code parseMetadataKinds();
403 std::error_code parseMetadataKindRecord(SmallVectorImpl<uint64_t> &Record);
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000404 std::error_code parseMetadataAttachment(Function &F);
Benjamin Kramercced8be2015-03-17 20:40:24 +0000405 ErrorOr<std::string> parseModuleTriple();
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000406 std::error_code parseUseLists();
Rafael Espindola1aabf982015-06-16 23:29:49 +0000407 std::error_code initStream(std::unique_ptr<DataStreamer> Streamer);
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000408 std::error_code initStreamFromBuffer();
Rafael Espindola1aabf982015-06-16 23:29:49 +0000409 std::error_code initLazyStream(std::unique_ptr<DataStreamer> Streamer);
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000410 std::error_code findFunctionInStream(
Benjamin Kramercced8be2015-03-17 20:40:24 +0000411 Function *F,
412 DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator);
413};
Teresa Johnson403a7872015-10-04 14:33:43 +0000414
415/// Class to manage reading and parsing function summary index bitcode
416/// files/sections.
Teresa Johnson26ab5772016-03-15 00:04:37 +0000417class ModuleSummaryIndexBitcodeReader {
Teresa Johnson403a7872015-10-04 14:33:43 +0000418 DiagnosticHandlerFunction DiagnosticHandler;
419
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000420 /// Eventually points to the module index built during parsing.
Teresa Johnson26ab5772016-03-15 00:04:37 +0000421 ModuleSummaryIndex *TheIndex = nullptr;
Teresa Johnson403a7872015-10-04 14:33:43 +0000422
423 std::unique_ptr<MemoryBuffer> Buffer;
424 std::unique_ptr<BitstreamReader> StreamFile;
425 BitstreamCursor Stream;
426
427 /// \brief Used to indicate whether we are doing lazy parsing of summary data.
428 ///
429 /// If false, the summary section is fully parsed into the index during
430 /// the initial parse. Otherwise, if true, the caller is expected to
Teresa Johnson26ab5772016-03-15 00:04:37 +0000431 /// invoke \a readGlobalValueSummary for each summary needed, and the summary
Teresa Johnson403a7872015-10-04 14:33:43 +0000432 /// section is thus parsed lazily.
433 bool IsLazy = false;
434
435 /// Used to indicate whether caller only wants to check for the presence
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000436 /// of the global value summary bitcode section. All blocks are skipped,
437 /// but the SeenGlobalValSummary boolean is set.
438 bool CheckGlobalValSummaryPresenceOnly = false;
Teresa Johnson403a7872015-10-04 14:33:43 +0000439
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000440 /// Indicates whether we have encountered a global value summary section
441 /// yet during parsing, used when checking if file contains global value
Teresa Johnson403a7872015-10-04 14:33:43 +0000442 /// summary section.
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000443 bool SeenGlobalValSummary = false;
Teresa Johnson403a7872015-10-04 14:33:43 +0000444
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000445 /// Indicates whether we have already parsed the VST, used for error checking.
446 bool SeenValueSymbolTable = false;
447
448 /// Set to the offset of the VST recorded in the MODULE_CODE_VSTOFFSET record.
449 /// Used to enable on-demand parsing of the VST.
450 uint64_t VSTOffset = 0;
451
452 // Map to save ValueId to GUID association that was recorded in the
453 // ValueSymbolTable. It is used after the VST is parsed to convert
454 // call graph edges read from the function summary from referencing
455 // callees by their ValueId to using the GUID instead, which is how
Teresa Johnson26ab5772016-03-15 00:04:37 +0000456 // they are recorded in the summary index being built.
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000457 DenseMap<unsigned, uint64_t> ValueIdToCallGraphGUIDMap;
458
459 /// Map to save the association between summary offset in the VST to the
460 /// GlobalValueInfo object created when parsing it. Used to access the
461 /// info object when parsing the summary section.
462 DenseMap<uint64_t, GlobalValueInfo *> SummaryOffsetToInfoMap;
Teresa Johnson403a7872015-10-04 14:33:43 +0000463
464 /// Map populated during module path string table parsing, from the
465 /// module ID to a string reference owned by the index's module
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000466 /// path string table, used to correlate with combined index
Teresa Johnson403a7872015-10-04 14:33:43 +0000467 /// summary records.
468 DenseMap<uint64_t, StringRef> ModuleIdMap;
469
Teresa Johnsone1164de2016-02-10 21:55:02 +0000470 /// Original source file name recorded in a bitcode record.
471 std::string SourceFileName;
472
Teresa Johnsonf72278f2015-11-02 18:02:11 +0000473public:
Teresa Johnson403a7872015-10-04 14:33:43 +0000474 std::error_code error(BitcodeError E, const Twine &Message);
475 std::error_code error(BitcodeError E);
476 std::error_code error(const Twine &Message);
477
Teresa Johnson26ab5772016-03-15 00:04:37 +0000478 ModuleSummaryIndexBitcodeReader(
479 MemoryBuffer *Buffer, DiagnosticHandlerFunction DiagnosticHandler,
480 bool IsLazy = false, bool CheckGlobalValSummaryPresenceOnly = false);
481 ModuleSummaryIndexBitcodeReader(
482 DiagnosticHandlerFunction DiagnosticHandler, bool IsLazy = false,
483 bool CheckGlobalValSummaryPresenceOnly = false);
484 ~ModuleSummaryIndexBitcodeReader() { freeState(); }
Teresa Johnson403a7872015-10-04 14:33:43 +0000485
486 void freeState();
487
488 void releaseBuffer();
489
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000490 /// Check if the parser has encountered a summary section.
491 bool foundGlobalValSummary() { return SeenGlobalValSummary; }
Teresa Johnson403a7872015-10-04 14:33:43 +0000492
493 /// \brief Main interface to parsing a bitcode buffer.
494 /// \returns true if an error occurred.
495 std::error_code parseSummaryIndexInto(std::unique_ptr<DataStreamer> Streamer,
Teresa Johnson26ab5772016-03-15 00:04:37 +0000496 ModuleSummaryIndex *I);
Teresa Johnson403a7872015-10-04 14:33:43 +0000497
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000498 /// \brief Interface for parsing a summary lazily.
Teresa Johnson26ab5772016-03-15 00:04:37 +0000499 std::error_code
500 parseGlobalValueSummary(std::unique_ptr<DataStreamer> Streamer,
501 ModuleSummaryIndex *I, size_t SummaryOffset);
Teresa Johnson403a7872015-10-04 14:33:43 +0000502
Teresa Johnsonf72278f2015-11-02 18:02:11 +0000503private:
Teresa Johnson403a7872015-10-04 14:33:43 +0000504 std::error_code parseModule();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000505 std::error_code parseValueSymbolTable(
506 uint64_t Offset,
507 DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap);
Teresa Johnson403a7872015-10-04 14:33:43 +0000508 std::error_code parseEntireSummary();
509 std::error_code parseModuleStringTable();
510 std::error_code initStream(std::unique_ptr<DataStreamer> Streamer);
511 std::error_code initStreamFromBuffer();
512 std::error_code initLazyStream(std::unique_ptr<DataStreamer> Streamer);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000513 uint64_t getGUIDFromValueId(unsigned ValueId);
514 GlobalValueInfo *getInfoFromSummaryOffset(uint64_t Offset);
Teresa Johnson403a7872015-10-04 14:33:43 +0000515};
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000516} // end anonymous namespace
Benjamin Kramercced8be2015-03-17 20:40:24 +0000517
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000518BitcodeDiagnosticInfo::BitcodeDiagnosticInfo(std::error_code EC,
519 DiagnosticSeverity Severity,
520 const Twine &Msg)
521 : DiagnosticInfo(DK_Bitcode, Severity), Msg(Msg), EC(EC) {}
522
523void BitcodeDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; }
524
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000525static std::error_code error(DiagnosticHandlerFunction DiagnosticHandler,
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000526 std::error_code EC, const Twine &Message) {
527 BitcodeDiagnosticInfo DI(EC, DS_Error, Message);
528 DiagnosticHandler(DI);
529 return EC;
530}
531
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000532static std::error_code error(DiagnosticHandlerFunction DiagnosticHandler,
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000533 std::error_code EC) {
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000534 return error(DiagnosticHandler, EC, EC.message());
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000535}
536
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000537static std::error_code error(LLVMContext &Context, std::error_code EC,
Filipe Cabecinhas11bb8492015-05-18 21:48:55 +0000538 const Twine &Message) {
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000539 return error([&](const DiagnosticInfo &DI) { Context.diagnose(DI); }, EC,
540 Message);
541}
542
543static std::error_code error(LLVMContext &Context, std::error_code EC) {
544 return error(Context, EC, EC.message());
545}
546
547static std::error_code error(LLVMContext &Context, const Twine &Message) {
548 return error(Context, make_error_code(BitcodeError::CorruptedBitcode),
549 Message);
Filipe Cabecinhas11bb8492015-05-18 21:48:55 +0000550}
551
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000552std::error_code BitcodeReader::error(BitcodeError E, const Twine &Message) {
Mehdi Amini5d303282015-10-26 18:37:00 +0000553 if (!ProducerIdentification.empty()) {
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000554 return ::error(Context, make_error_code(E),
Filipe Cabecinhasf3e167a2015-11-03 13:48:21 +0000555 Message + " (Producer: '" + ProducerIdentification +
556 "' Reader: 'LLVM " + LLVM_VERSION_STRING "')");
Mehdi Amini5d303282015-10-26 18:37:00 +0000557 }
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000558 return ::error(Context, make_error_code(E), Message);
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000559}
560
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000561std::error_code BitcodeReader::error(const Twine &Message) {
Mehdi Amini5d303282015-10-26 18:37:00 +0000562 if (!ProducerIdentification.empty()) {
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000563 return ::error(Context, make_error_code(BitcodeError::CorruptedBitcode),
Filipe Cabecinhasf3e167a2015-11-03 13:48:21 +0000564 Message + " (Producer: '" + ProducerIdentification +
565 "' Reader: 'LLVM " + LLVM_VERSION_STRING "')");
Mehdi Amini5d303282015-10-26 18:37:00 +0000566 }
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000567 return ::error(Context, make_error_code(BitcodeError::CorruptedBitcode),
568 Message);
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000569}
570
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000571std::error_code BitcodeReader::error(BitcodeError E) {
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000572 return ::error(Context, make_error_code(E));
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000573}
574
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000575BitcodeReader::BitcodeReader(MemoryBuffer *Buffer, LLVMContext &Context)
576 : Context(Context), Buffer(Buffer), ValueList(Context),
Teresa Johnson61b406e2015-12-29 23:00:22 +0000577 MetadataList(Context) {}
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000578
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000579BitcodeReader::BitcodeReader(LLVMContext &Context)
580 : Context(Context), Buffer(nullptr), ValueList(Context),
Teresa Johnson61b406e2015-12-29 23:00:22 +0000581 MetadataList(Context) {}
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000582
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +0000583std::error_code BitcodeReader::materializeForwardReferencedFunctions() {
584 if (WillMaterializeAllForwardRefs)
585 return std::error_code();
586
587 // Prevent recursion.
588 WillMaterializeAllForwardRefs = true;
589
Duncan P. N. Exon Smith5a511b52014-08-05 17:49:48 +0000590 while (!BasicBlockFwdRefQueue.empty()) {
591 Function *F = BasicBlockFwdRefQueue.front();
592 BasicBlockFwdRefQueue.pop_front();
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +0000593 assert(F && "Expected valid function");
Duncan P. N. Exon Smith5a511b52014-08-05 17:49:48 +0000594 if (!BasicBlockFwdRefs.count(F))
595 // Already materialized.
596 continue;
597
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +0000598 // Check for a function that isn't materializable to prevent an infinite
599 // loop. When parsing a blockaddress stored in a global variable, there
600 // isn't a trivial way to check if a function will have a body without a
601 // linear search through FunctionsWithBodies, so just check it here.
602 if (!F->isMaterializable())
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000603 return error("Never resolved function from blockaddress");
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +0000604
605 // Try to materialize F.
Rafael Espindola5a52e6d2014-10-24 22:50:48 +0000606 if (std::error_code EC = materialize(F))
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +0000607 return EC;
Rafael Espindolab7993462012-01-02 07:49:53 +0000608 }
Duncan P. N. Exon Smith5a511b52014-08-05 17:49:48 +0000609 assert(BasicBlockFwdRefs.empty() && "Function missing from queue");
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +0000610
611 // Reset state.
612 WillMaterializeAllForwardRefs = false;
613 return std::error_code();
Rafael Espindolab7993462012-01-02 07:49:53 +0000614}
615
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000616void BitcodeReader::freeState() {
Craig Topper2617dcc2014-04-15 06:32:26 +0000617 Buffer = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000618 std::vector<Type*>().swap(TypeList);
Chris Lattner9eeada92007-05-18 04:02:46 +0000619 ValueList.clear();
Teresa Johnson61b406e2015-12-29 23:00:22 +0000620 MetadataList.clear();
David Majnemerdad0a642014-06-27 18:19:56 +0000621 std::vector<Comdat *>().swap(ComdatList);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000622
Bill Wendlinge94d8432012-12-07 23:16:57 +0000623 std::vector<AttributeSet>().swap(MAttributes);
Chris Lattner9eeada92007-05-18 04:02:46 +0000624 std::vector<BasicBlock*>().swap(FunctionBBs);
625 std::vector<Function*>().swap(FunctionsWithBodies);
626 DeferredFunctionInfo.clear();
Manman Ren4a9b0eb2015-03-13 19:24:30 +0000627 DeferredMetadataInfo.clear();
Dan Gohman43aa8f02010-07-20 21:42:28 +0000628 MDKindMap.clear();
Benjamin Kramer736a4fc2012-09-21 14:34:31 +0000629
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +0000630 assert(BasicBlockFwdRefs.empty() && "Unresolved blockaddress fwd references");
Duncan P. N. Exon Smith5a511b52014-08-05 17:49:48 +0000631 BasicBlockFwdRefQueue.clear();
Chris Lattner6694f602007-04-29 07:54:31 +0000632}
633
Chris Lattnerfee5a372007-05-04 03:30:17 +0000634//===----------------------------------------------------------------------===//
635// Helper functions to implement forward reference resolution, etc.
636//===----------------------------------------------------------------------===//
Chris Lattner6694f602007-04-29 07:54:31 +0000637
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000638/// Convert a string from a record into an std::string, return true on failure.
639template <typename StrTy>
640static bool convertToString(ArrayRef<uint64_t> Record, unsigned Idx,
Chris Lattnerccaa4482007-04-23 21:26:05 +0000641 StrTy &Result) {
Chris Lattnere14cb882007-05-04 19:11:41 +0000642 if (Idx > Record.size())
Chris Lattner1314b992007-04-22 06:23:29 +0000643 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000644
Chris Lattnere14cb882007-05-04 19:11:41 +0000645 for (unsigned i = Idx, e = Record.size(); i != e; ++i)
646 Result += (char)Record[i];
Chris Lattner1314b992007-04-22 06:23:29 +0000647 return false;
648}
649
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000650static bool hasImplicitComdat(size_t Val) {
651 switch (Val) {
652 default:
653 return false;
654 case 1: // Old WeakAnyLinkage
655 case 4: // Old LinkOnceAnyLinkage
656 case 10: // Old WeakODRLinkage
657 case 11: // Old LinkOnceODRLinkage
658 return true;
659 }
660}
661
Rafael Espindola7b4b2dc2015-01-08 15:36:32 +0000662static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) {
Chris Lattner1314b992007-04-22 06:23:29 +0000663 switch (Val) {
664 default: // Map unknown/new linkages to external
Rafael Espindola7b4b2dc2015-01-08 15:36:32 +0000665 case 0:
666 return GlobalValue::ExternalLinkage;
Rafael Espindola7b4b2dc2015-01-08 15:36:32 +0000667 case 2:
668 return GlobalValue::AppendingLinkage;
669 case 3:
670 return GlobalValue::InternalLinkage;
Rafael Espindola7b4b2dc2015-01-08 15:36:32 +0000671 case 5:
672 return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage
673 case 6:
674 return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage
675 case 7:
676 return GlobalValue::ExternalWeakLinkage;
677 case 8:
678 return GlobalValue::CommonLinkage;
679 case 9:
680 return GlobalValue::PrivateLinkage;
Rafael Espindola7b4b2dc2015-01-08 15:36:32 +0000681 case 12:
682 return GlobalValue::AvailableExternallyLinkage;
Rafael Espindola2fb5bc32014-03-13 23:18:37 +0000683 case 13:
684 return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage
685 case 14:
686 return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage
Rafael Espindolabec6af62015-01-08 15:39:50 +0000687 case 15:
688 return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000689 case 1: // Old value with implicit comdat.
690 case 16:
691 return GlobalValue::WeakAnyLinkage;
692 case 10: // Old value with implicit comdat.
693 case 17:
694 return GlobalValue::WeakODRLinkage;
695 case 4: // Old value with implicit comdat.
696 case 18:
697 return GlobalValue::LinkOnceAnyLinkage;
698 case 11: // Old value with implicit comdat.
699 case 19:
700 return GlobalValue::LinkOnceODRLinkage;
Chris Lattner1314b992007-04-22 06:23:29 +0000701 }
702}
703
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000704static GlobalValue::VisibilityTypes getDecodedVisibility(unsigned Val) {
Chris Lattner1314b992007-04-22 06:23:29 +0000705 switch (Val) {
706 default: // Map unknown visibilities to default.
707 case 0: return GlobalValue::DefaultVisibility;
708 case 1: return GlobalValue::HiddenVisibility;
Anton Korobeynikov31fc4f92007-04-29 20:56:48 +0000709 case 2: return GlobalValue::ProtectedVisibility;
Chris Lattner1314b992007-04-22 06:23:29 +0000710 }
711}
712
Nico Rieck7157bb72014-01-14 15:22:47 +0000713static GlobalValue::DLLStorageClassTypes
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000714getDecodedDLLStorageClass(unsigned Val) {
Nico Rieck7157bb72014-01-14 15:22:47 +0000715 switch (Val) {
716 default: // Map unknown values to default.
717 case 0: return GlobalValue::DefaultStorageClass;
718 case 1: return GlobalValue::DLLImportStorageClass;
719 case 2: return GlobalValue::DLLExportStorageClass;
720 }
721}
722
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000723static GlobalVariable::ThreadLocalMode getDecodedThreadLocalMode(unsigned Val) {
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000724 switch (Val) {
725 case 0: return GlobalVariable::NotThreadLocal;
726 default: // Map unknown non-zero value to general dynamic.
727 case 1: return GlobalVariable::GeneralDynamicTLSModel;
728 case 2: return GlobalVariable::LocalDynamicTLSModel;
729 case 3: return GlobalVariable::InitialExecTLSModel;
730 case 4: return GlobalVariable::LocalExecTLSModel;
731 }
732}
733
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000734static int getDecodedCastOpcode(unsigned Val) {
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000735 switch (Val) {
736 default: return -1;
737 case bitc::CAST_TRUNC : return Instruction::Trunc;
738 case bitc::CAST_ZEXT : return Instruction::ZExt;
739 case bitc::CAST_SEXT : return Instruction::SExt;
740 case bitc::CAST_FPTOUI : return Instruction::FPToUI;
741 case bitc::CAST_FPTOSI : return Instruction::FPToSI;
742 case bitc::CAST_UITOFP : return Instruction::UIToFP;
743 case bitc::CAST_SITOFP : return Instruction::SIToFP;
744 case bitc::CAST_FPTRUNC : return Instruction::FPTrunc;
745 case bitc::CAST_FPEXT : return Instruction::FPExt;
746 case bitc::CAST_PTRTOINT: return Instruction::PtrToInt;
747 case bitc::CAST_INTTOPTR: return Instruction::IntToPtr;
748 case bitc::CAST_BITCAST : return Instruction::BitCast;
Matt Arsenault3aa9b032013-11-18 02:51:33 +0000749 case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000750 }
751}
Filipe Cabecinhasea79c5b2015-04-22 09:06:21 +0000752
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000753static int getDecodedBinaryOpcode(unsigned Val, Type *Ty) {
Filipe Cabecinhasea79c5b2015-04-22 09:06:21 +0000754 bool IsFP = Ty->isFPOrFPVectorTy();
755 // BinOps are only valid for int/fp or vector of int/fp types
756 if (!IsFP && !Ty->isIntOrIntVectorTy())
757 return -1;
758
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000759 switch (Val) {
Filipe Cabecinhasea79c5b2015-04-22 09:06:21 +0000760 default:
761 return -1;
Dan Gohmana5b96452009-06-04 22:49:04 +0000762 case bitc::BINOP_ADD:
Filipe Cabecinhasea79c5b2015-04-22 09:06:21 +0000763 return IsFP ? Instruction::FAdd : Instruction::Add;
Dan Gohmana5b96452009-06-04 22:49:04 +0000764 case bitc::BINOP_SUB:
Filipe Cabecinhasea79c5b2015-04-22 09:06:21 +0000765 return IsFP ? Instruction::FSub : Instruction::Sub;
Dan Gohmana5b96452009-06-04 22:49:04 +0000766 case bitc::BINOP_MUL:
Filipe Cabecinhasea79c5b2015-04-22 09:06:21 +0000767 return IsFP ? Instruction::FMul : Instruction::Mul;
768 case bitc::BINOP_UDIV:
769 return IsFP ? -1 : Instruction::UDiv;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000770 case bitc::BINOP_SDIV:
Filipe Cabecinhasea79c5b2015-04-22 09:06:21 +0000771 return IsFP ? Instruction::FDiv : Instruction::SDiv;
772 case bitc::BINOP_UREM:
773 return IsFP ? -1 : Instruction::URem;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000774 case bitc::BINOP_SREM:
Filipe Cabecinhasea79c5b2015-04-22 09:06:21 +0000775 return IsFP ? Instruction::FRem : Instruction::SRem;
776 case bitc::BINOP_SHL:
777 return IsFP ? -1 : Instruction::Shl;
778 case bitc::BINOP_LSHR:
779 return IsFP ? -1 : Instruction::LShr;
780 case bitc::BINOP_ASHR:
781 return IsFP ? -1 : Instruction::AShr;
782 case bitc::BINOP_AND:
783 return IsFP ? -1 : Instruction::And;
784 case bitc::BINOP_OR:
785 return IsFP ? -1 : Instruction::Or;
786 case bitc::BINOP_XOR:
787 return IsFP ? -1 : Instruction::Xor;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000788 }
789}
790
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000791static AtomicRMWInst::BinOp getDecodedRMWOperation(unsigned Val) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +0000792 switch (Val) {
793 default: return AtomicRMWInst::BAD_BINOP;
794 case bitc::RMW_XCHG: return AtomicRMWInst::Xchg;
795 case bitc::RMW_ADD: return AtomicRMWInst::Add;
796 case bitc::RMW_SUB: return AtomicRMWInst::Sub;
797 case bitc::RMW_AND: return AtomicRMWInst::And;
798 case bitc::RMW_NAND: return AtomicRMWInst::Nand;
799 case bitc::RMW_OR: return AtomicRMWInst::Or;
800 case bitc::RMW_XOR: return AtomicRMWInst::Xor;
801 case bitc::RMW_MAX: return AtomicRMWInst::Max;
802 case bitc::RMW_MIN: return AtomicRMWInst::Min;
803 case bitc::RMW_UMAX: return AtomicRMWInst::UMax;
804 case bitc::RMW_UMIN: return AtomicRMWInst::UMin;
805 }
806}
807
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000808static AtomicOrdering getDecodedOrdering(unsigned Val) {
Eli Friedmanfee02c62011-07-25 23:16:38 +0000809 switch (Val) {
810 case bitc::ORDERING_NOTATOMIC: return NotAtomic;
811 case bitc::ORDERING_UNORDERED: return Unordered;
812 case bitc::ORDERING_MONOTONIC: return Monotonic;
813 case bitc::ORDERING_ACQUIRE: return Acquire;
814 case bitc::ORDERING_RELEASE: return Release;
815 case bitc::ORDERING_ACQREL: return AcquireRelease;
816 default: // Map unknown orderings to sequentially-consistent.
817 case bitc::ORDERING_SEQCST: return SequentiallyConsistent;
818 }
819}
820
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000821static SynchronizationScope getDecodedSynchScope(unsigned Val) {
Eli Friedmanfee02c62011-07-25 23:16:38 +0000822 switch (Val) {
823 case bitc::SYNCHSCOPE_SINGLETHREAD: return SingleThread;
824 default: // Map unknown scopes to cross-thread.
825 case bitc::SYNCHSCOPE_CROSSTHREAD: return CrossThread;
826 }
827}
828
David Majnemerdad0a642014-06-27 18:19:56 +0000829static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) {
830 switch (Val) {
831 default: // Map unknown selection kinds to any.
832 case bitc::COMDAT_SELECTION_KIND_ANY:
833 return Comdat::Any;
834 case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH:
835 return Comdat::ExactMatch;
836 case bitc::COMDAT_SELECTION_KIND_LARGEST:
837 return Comdat::Largest;
838 case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES:
839 return Comdat::NoDuplicates;
840 case bitc::COMDAT_SELECTION_KIND_SAME_SIZE:
841 return Comdat::SameSize;
842 }
843}
844
James Molloy88eb5352015-07-10 12:52:00 +0000845static FastMathFlags getDecodedFastMathFlags(unsigned Val) {
846 FastMathFlags FMF;
847 if (0 != (Val & FastMathFlags::UnsafeAlgebra))
848 FMF.setUnsafeAlgebra();
849 if (0 != (Val & FastMathFlags::NoNaNs))
850 FMF.setNoNaNs();
851 if (0 != (Val & FastMathFlags::NoInfs))
852 FMF.setNoInfs();
853 if (0 != (Val & FastMathFlags::NoSignedZeros))
854 FMF.setNoSignedZeros();
855 if (0 != (Val & FastMathFlags::AllowReciprocal))
856 FMF.setAllowReciprocal();
857 return FMF;
858}
859
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000860static void upgradeDLLImportExportLinkage(llvm::GlobalValue *GV, unsigned Val) {
Nico Rieck7157bb72014-01-14 15:22:47 +0000861 switch (Val) {
862 case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break;
863 case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break;
864 }
865}
866
Gabor Greiff6caff662008-05-10 08:32:32 +0000867namespace llvm {
Chris Lattner1663cca2007-04-24 05:48:56 +0000868namespace {
Rafael Espindola64a27fb2015-06-15 21:04:27 +0000869/// \brief A class for maintaining the slot number definition
870/// as a placeholder for the actual definition for forward constants defs.
871class ConstantPlaceHolder : public ConstantExpr {
872 void operator=(const ConstantPlaceHolder &) = delete;
873
874public:
875 // allocate space for exactly one operand
876 void *operator new(size_t s) { return User::operator new(s, 1); }
877 explicit ConstantPlaceHolder(Type *Ty, LLVMContext &Context)
Gabor Greiff6caff662008-05-10 08:32:32 +0000878 : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) {
Rafael Espindola64a27fb2015-06-15 21:04:27 +0000879 Op<0>() = UndefValue::get(Type::getInt32Ty(Context));
880 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000881
Rafael Espindola64a27fb2015-06-15 21:04:27 +0000882 /// \brief Methods to support type inquiry through isa, cast, and dyn_cast.
883 static bool classof(const Value *V) {
884 return isa<ConstantExpr>(V) &&
885 cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1;
886 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000887
Rafael Espindola64a27fb2015-06-15 21:04:27 +0000888 /// Provide fast operand accessors
889 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
890};
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000891} // end anonymous namespace
Chris Lattner1663cca2007-04-24 05:48:56 +0000892
Chris Lattner2d8cd802009-03-31 22:55:09 +0000893// FIXME: can we inherit this from ConstantExpr?
Gabor Greiff6caff662008-05-10 08:32:32 +0000894template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000895struct OperandTraits<ConstantPlaceHolder> :
896 public FixedNumOperandTraits<ConstantPlaceHolder, 1> {
Gabor Greiff6caff662008-05-10 08:32:32 +0000897};
Richard Trieue3d126c2014-11-21 02:42:08 +0000898DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantPlaceHolder, Value)
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000899} // end namespace llvm
Gabor Greiff6caff662008-05-10 08:32:32 +0000900
David Majnemer8a1c45d2015-12-12 05:38:55 +0000901void BitcodeReaderValueList::assignValue(Value *V, unsigned Idx) {
Chris Lattner2d8cd802009-03-31 22:55:09 +0000902 if (Idx == size()) {
903 push_back(V);
David Majnemer8a1c45d2015-12-12 05:38:55 +0000904 return;
Chris Lattner2d8cd802009-03-31 22:55:09 +0000905 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000906
Chris Lattner2d8cd802009-03-31 22:55:09 +0000907 if (Idx >= size())
908 resize(Idx+1);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000909
Chris Lattner2d8cd802009-03-31 22:55:09 +0000910 WeakVH &OldV = ValuePtrs[Idx];
Craig Topper2617dcc2014-04-15 06:32:26 +0000911 if (!OldV) {
Chris Lattner2d8cd802009-03-31 22:55:09 +0000912 OldV = V;
David Majnemer8a1c45d2015-12-12 05:38:55 +0000913 return;
Chris Lattner2d8cd802009-03-31 22:55:09 +0000914 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000915
Chris Lattner2d8cd802009-03-31 22:55:09 +0000916 // Handle constants and non-constants (e.g. instrs) differently for
917 // efficiency.
918 if (Constant *PHC = dyn_cast<Constant>(&*OldV)) {
919 ResolveConstants.push_back(std::make_pair(PHC, Idx));
920 OldV = V;
921 } else {
922 // If there was a forward reference to this value, replace it.
923 Value *PrevVal = OldV;
924 OldV->replaceAllUsesWith(V);
925 delete PrevVal;
Gabor Greiff6caff662008-05-10 08:32:32 +0000926 }
927}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000928
Chris Lattner1663cca2007-04-24 05:48:56 +0000929Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx,
Chris Lattner229907c2011-07-18 04:54:35 +0000930 Type *Ty) {
Chris Lattner2d8cd802009-03-31 22:55:09 +0000931 if (Idx >= size())
Gabor Greiff6caff662008-05-10 08:32:32 +0000932 resize(Idx + 1);
Chris Lattner1663cca2007-04-24 05:48:56 +0000933
Chris Lattner2d8cd802009-03-31 22:55:09 +0000934 if (Value *V = ValuePtrs[Idx]) {
Filipe Cabecinhas6a92a3f2015-05-27 01:05:40 +0000935 if (Ty != V->getType())
936 report_fatal_error("Type mismatch in constant table!");
Chris Lattner83930552007-05-01 07:01:57 +0000937 return cast<Constant>(V);
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000938 }
Chris Lattner1663cca2007-04-24 05:48:56 +0000939
940 // Create and return a placeholder, which will later be RAUW'd.
Owen Andersone9f98042009-07-07 20:18:58 +0000941 Constant *C = new ConstantPlaceHolder(Ty, Context);
Chris Lattner2d8cd802009-03-31 22:55:09 +0000942 ValuePtrs[Idx] = C;
Chris Lattner1663cca2007-04-24 05:48:56 +0000943 return C;
944}
945
David Majnemer8a1c45d2015-12-12 05:38:55 +0000946Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, Type *Ty) {
Filipe Cabecinhasbad07792015-04-30 00:52:42 +0000947 // Bail out for a clearly invalid value. This would make us call resize(0)
948 if (Idx == UINT_MAX)
949 return nullptr;
950
Chris Lattner2d8cd802009-03-31 22:55:09 +0000951 if (Idx >= size())
Gabor Greiff6caff662008-05-10 08:32:32 +0000952 resize(Idx + 1);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000953
Chris Lattner2d8cd802009-03-31 22:55:09 +0000954 if (Value *V = ValuePtrs[Idx]) {
Filipe Cabecinhasb435d0f2015-04-28 20:18:47 +0000955 // If the types don't match, it's invalid.
956 if (Ty && Ty != V->getType())
957 return nullptr;
David Majnemer8a1c45d2015-12-12 05:38:55 +0000958 return V;
Chris Lattner83930552007-05-01 07:01:57 +0000959 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000960
Chris Lattner1fc27f02007-05-02 05:16:49 +0000961 // No type specified, must be invalid reference.
Craig Topper2617dcc2014-04-15 06:32:26 +0000962 if (!Ty) return nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000963
Chris Lattner83930552007-05-01 07:01:57 +0000964 // Create and return a placeholder, which will later be RAUW'd.
David Majnemer8a1c45d2015-12-12 05:38:55 +0000965 Value *V = new Argument(Ty);
Chris Lattner2d8cd802009-03-31 22:55:09 +0000966 ValuePtrs[Idx] = V;
Chris Lattner83930552007-05-01 07:01:57 +0000967 return V;
968}
969
Rafael Espindolacbdcb502015-06-15 20:55:37 +0000970/// Once all constants are read, this method bulk resolves any forward
971/// references. The idea behind this is that we sometimes get constants (such
972/// as large arrays) which reference *many* forward ref constants. Replacing
973/// each of these causes a lot of thrashing when building/reuniquing the
974/// constant. Instead of doing this, we look at all the uses and rewrite all
975/// the place holders at once for any constant that uses a placeholder.
976void BitcodeReaderValueList::resolveConstantForwardRefs() {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000977 // Sort the values by-pointer so that they are efficient to look up with a
Chris Lattner74429932008-08-21 02:34:16 +0000978 // binary search.
979 std::sort(ResolveConstants.begin(), ResolveConstants.end());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000980
Chris Lattner74429932008-08-21 02:34:16 +0000981 SmallVector<Constant*, 64> NewOps;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000982
Chris Lattner74429932008-08-21 02:34:16 +0000983 while (!ResolveConstants.empty()) {
Chris Lattner2d8cd802009-03-31 22:55:09 +0000984 Value *RealVal = operator[](ResolveConstants.back().second);
Chris Lattner74429932008-08-21 02:34:16 +0000985 Constant *Placeholder = ResolveConstants.back().first;
986 ResolveConstants.pop_back();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000987
Chris Lattner74429932008-08-21 02:34:16 +0000988 // Loop over all users of the placeholder, updating them to reference the
989 // new value. If they reference more than one placeholder, update them all
990 // at once.
991 while (!Placeholder->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000992 auto UI = Placeholder->user_begin();
Gabor Greif2c0ab482010-07-09 16:01:21 +0000993 User *U = *UI;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000994
Chris Lattner74429932008-08-21 02:34:16 +0000995 // If the using object isn't uniqued, just update the operands. This
996 // handles instructions and initializers for global variables.
Gabor Greif2c0ab482010-07-09 16:01:21 +0000997 if (!isa<Constant>(U) || isa<GlobalValue>(U)) {
Chris Lattner479c5d92008-08-21 17:31:45 +0000998 UI.getUse().set(RealVal);
Chris Lattner74429932008-08-21 02:34:16 +0000999 continue;
1000 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001001
Chris Lattner74429932008-08-21 02:34:16 +00001002 // Otherwise, we have a constant that uses the placeholder. Replace that
1003 // constant with a new constant that has *all* placeholder uses updated.
Gabor Greif2c0ab482010-07-09 16:01:21 +00001004 Constant *UserC = cast<Constant>(U);
Chris Lattner74429932008-08-21 02:34:16 +00001005 for (User::op_iterator I = UserC->op_begin(), E = UserC->op_end();
1006 I != E; ++I) {
1007 Value *NewOp;
1008 if (!isa<ConstantPlaceHolder>(*I)) {
1009 // Not a placeholder reference.
1010 NewOp = *I;
1011 } else if (*I == Placeholder) {
1012 // Common case is that it just references this one placeholder.
1013 NewOp = RealVal;
1014 } else {
1015 // Otherwise, look up the placeholder in ResolveConstants.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001016 ResolveConstantsTy::iterator It =
1017 std::lower_bound(ResolveConstants.begin(), ResolveConstants.end(),
Chris Lattner74429932008-08-21 02:34:16 +00001018 std::pair<Constant*, unsigned>(cast<Constant>(*I),
1019 0));
1020 assert(It != ResolveConstants.end() && It->first == *I);
Chris Lattner2d8cd802009-03-31 22:55:09 +00001021 NewOp = operator[](It->second);
Chris Lattner74429932008-08-21 02:34:16 +00001022 }
1023
1024 NewOps.push_back(cast<Constant>(NewOp));
1025 }
1026
1027 // Make the new constant.
1028 Constant *NewC;
1029 if (ConstantArray *UserCA = dyn_cast<ConstantArray>(UserC)) {
Jay Foad83be3612011-06-22 09:24:39 +00001030 NewC = ConstantArray::get(UserCA->getType(), NewOps);
Chris Lattner74429932008-08-21 02:34:16 +00001031 } else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) {
Chris Lattnercc19efa2011-06-20 04:01:31 +00001032 NewC = ConstantStruct::get(UserCS->getType(), NewOps);
Chris Lattner74429932008-08-21 02:34:16 +00001033 } else if (isa<ConstantVector>(UserC)) {
Chris Lattner69229312011-02-15 00:14:00 +00001034 NewC = ConstantVector::get(NewOps);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001035 } else {
1036 assert(isa<ConstantExpr>(UserC) && "Must be a ConstantExpr.");
Jay Foad5c984e562011-04-13 13:46:01 +00001037 NewC = cast<ConstantExpr>(UserC)->getWithOperands(NewOps);
Chris Lattner74429932008-08-21 02:34:16 +00001038 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001039
Chris Lattner74429932008-08-21 02:34:16 +00001040 UserC->replaceAllUsesWith(NewC);
1041 UserC->destroyConstant();
1042 NewOps.clear();
1043 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001044
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001045 // Update all ValueHandles, they should be the only users at this point.
1046 Placeholder->replaceAllUsesWith(RealVal);
Chris Lattner74429932008-08-21 02:34:16 +00001047 delete Placeholder;
1048 }
1049}
1050
Teresa Johnson61b406e2015-12-29 23:00:22 +00001051void BitcodeReaderMetadataList::assignValue(Metadata *MD, unsigned Idx) {
Devang Patel05eb6172009-08-04 06:00:18 +00001052 if (Idx == size()) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001053 push_back(MD);
Devang Patel05eb6172009-08-04 06:00:18 +00001054 return;
1055 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001056
Devang Patel05eb6172009-08-04 06:00:18 +00001057 if (Idx >= size())
1058 resize(Idx+1);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001059
Teresa Johnson61b406e2015-12-29 23:00:22 +00001060 TrackingMDRef &OldMD = MetadataPtrs[Idx];
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001061 if (!OldMD) {
1062 OldMD.reset(MD);
Devang Patel05eb6172009-08-04 06:00:18 +00001063 return;
1064 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001065
Devang Patel05eb6172009-08-04 06:00:18 +00001066 // If there was a forward reference to this value, replace it.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00001067 TempMDTuple PrevMD(cast<MDTuple>(OldMD.get()));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001068 PrevMD->replaceAllUsesWith(MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001069 --NumFwdRefs;
Devang Patel05eb6172009-08-04 06:00:18 +00001070}
1071
Justin Bognerae341c62016-03-17 20:12:06 +00001072Metadata *BitcodeReaderMetadataList::getMetadataFwdRef(unsigned Idx) {
Devang Patel05eb6172009-08-04 06:00:18 +00001073 if (Idx >= size())
1074 resize(Idx + 1);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001075
Teresa Johnson61b406e2015-12-29 23:00:22 +00001076 if (Metadata *MD = MetadataPtrs[Idx])
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001077 return MD;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001078
Duncan P. N. Exon Smith060ee622015-02-16 19:18:01 +00001079 // Track forward refs to be resolved later.
1080 if (AnyFwdRefs) {
1081 MinFwdRef = std::min(MinFwdRef, Idx);
1082 MaxFwdRef = std::max(MaxFwdRef, Idx);
1083 } else {
1084 AnyFwdRefs = true;
1085 MinFwdRef = MaxFwdRef = Idx;
1086 }
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001087 ++NumFwdRefs;
Duncan P. N. Exon Smith060ee622015-02-16 19:18:01 +00001088
1089 // Create and return a placeholder, which will later be RAUW'd.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00001090 Metadata *MD = MDNode::getTemporary(Context, None).release();
Teresa Johnson61b406e2015-12-29 23:00:22 +00001091 MetadataPtrs[Idx].reset(MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001092 return MD;
1093}
1094
Justin Bognerae341c62016-03-17 20:12:06 +00001095MDNode *BitcodeReaderMetadataList::getMDNodeFwdRefOrNull(unsigned Idx) {
1096 return dyn_cast_or_null<MDNode>(getMetadataFwdRef(Idx));
1097}
1098
Teresa Johnson61b406e2015-12-29 23:00:22 +00001099void BitcodeReaderMetadataList::tryToResolveCycles() {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001100 if (!AnyFwdRefs)
1101 // Nothing to do.
1102 return;
1103
1104 if (NumFwdRefs)
1105 // Still forward references... can't resolve cycles.
1106 return;
1107
1108 // Resolve any cycles.
Duncan P. N. Exon Smith060ee622015-02-16 19:18:01 +00001109 for (unsigned I = MinFwdRef, E = MaxFwdRef + 1; I != E; ++I) {
Teresa Johnson61b406e2015-12-29 23:00:22 +00001110 auto &MD = MetadataPtrs[I];
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +00001111 auto *N = dyn_cast_or_null<MDNode>(MD);
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +00001112 if (!N)
1113 continue;
1114
1115 assert(!N->isTemporary() && "Unexpected forward reference");
1116 N->resolveCycles();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001117 }
Duncan P. N. Exon Smith060ee622015-02-16 19:18:01 +00001118
1119 // Make sure we return early again until there's another forward ref.
1120 AnyFwdRefs = false;
Devang Patel05eb6172009-08-04 06:00:18 +00001121}
Chris Lattner1314b992007-04-22 06:23:29 +00001122
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001123Type *BitcodeReader::getTypeByID(unsigned ID) {
1124 // The type table size is always specified correctly.
1125 if (ID >= TypeList.size())
Craig Topper2617dcc2014-04-15 06:32:26 +00001126 return nullptr;
Derek Schuff206dddd2012-02-06 19:03:04 +00001127
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001128 if (Type *Ty = TypeList[ID])
1129 return Ty;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001130
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001131 // If we have a forward reference, the only possible case is when it is to a
1132 // named struct. Just create a placeholder for now.
Rafael Espindola2fa1e432014-12-03 07:18:23 +00001133 return TypeList[ID] = createIdentifiedStructType(Context);
1134}
1135
1136StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context,
1137 StringRef Name) {
1138 auto *Ret = StructType::create(Context, Name);
1139 IdentifiedStructTypes.push_back(Ret);
1140 return Ret;
1141}
1142
1143StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) {
1144 auto *Ret = StructType::create(Context);
1145 IdentifiedStructTypes.push_back(Ret);
1146 return Ret;
Chris Lattner1314b992007-04-22 06:23:29 +00001147}
1148
Chris Lattnerfee5a372007-05-04 03:30:17 +00001149//===----------------------------------------------------------------------===//
1150// Functions for parsing blocks from the bitcode file
1151//===----------------------------------------------------------------------===//
1152
Bill Wendling56aeccc2013-02-04 23:32:23 +00001153
1154/// \brief This fills an AttrBuilder object with the LLVM attributes that have
1155/// been decoded from the given integer. This function must stay in sync with
1156/// 'encodeLLVMAttributesForBitcode'.
1157static void decodeLLVMAttributesForBitcode(AttrBuilder &B,
1158 uint64_t EncodedAttrs) {
1159 // FIXME: Remove in 4.0.
1160
1161 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
1162 // the bits above 31 down by 11 bits.
1163 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
1164 assert((!Alignment || isPowerOf2_32(Alignment)) &&
1165 "Alignment must be a power of two.");
1166
1167 if (Alignment)
1168 B.addAlignmentAttr(Alignment);
Kostya Serebryanyd688bab2013-02-11 08:13:54 +00001169 B.addRawValue(((EncodedAttrs & (0xfffffULL << 32)) >> 11) |
Bill Wendling56aeccc2013-02-04 23:32:23 +00001170 (EncodedAttrs & 0xffff));
1171}
1172
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001173std::error_code BitcodeReader::parseAttributeBlock() {
Chris Lattner982ec1e2007-05-05 00:17:00 +00001174 if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001175 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001176
Devang Patela05633e2008-09-26 22:53:05 +00001177 if (!MAttributes.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001178 return error("Invalid multiple blocks");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001179
Chris Lattnerfee5a372007-05-04 03:30:17 +00001180 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001181
Bill Wendling71173cb2013-01-27 00:36:48 +00001182 SmallVector<AttributeSet, 8> Attrs;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001183
Chris Lattnerfee5a372007-05-04 03:30:17 +00001184 // Read all the records.
1185 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00001186 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00001187
Chris Lattner27d38752013-01-20 02:13:19 +00001188 switch (Entry.Kind) {
1189 case BitstreamEntry::SubBlock: // Handled for us already.
1190 case BitstreamEntry::Error:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001191 return error("Malformed block");
Chris Lattner27d38752013-01-20 02:13:19 +00001192 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001193 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00001194 case BitstreamEntry::Record:
1195 // The interesting case.
1196 break;
Chris Lattnerfee5a372007-05-04 03:30:17 +00001197 }
Joe Abbey97b7a172013-02-06 22:14:06 +00001198
Chris Lattnerfee5a372007-05-04 03:30:17 +00001199 // Read a record.
1200 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +00001201 switch (Stream.readRecord(Entry.ID, Record)) {
Chris Lattnerfee5a372007-05-04 03:30:17 +00001202 default: // Default behavior: ignore.
1203 break;
Bill Wendling56aeccc2013-02-04 23:32:23 +00001204 case bitc::PARAMATTR_CODE_ENTRY_OLD: { // ENTRY: [paramidx0, attr0, ...]
1205 // FIXME: Remove in 4.0.
Chris Lattnerfee5a372007-05-04 03:30:17 +00001206 if (Record.size() & 1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001207 return error("Invalid record");
Chris Lattnerfee5a372007-05-04 03:30:17 +00001208
Chris Lattnerfee5a372007-05-04 03:30:17 +00001209 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
Bill Wendling60011b82013-01-29 01:43:29 +00001210 AttrBuilder B;
Bill Wendling56aeccc2013-02-04 23:32:23 +00001211 decodeLLVMAttributesForBitcode(B, Record[i+1]);
Bill Wendling60011b82013-01-29 01:43:29 +00001212 Attrs.push_back(AttributeSet::get(Context, Record[i], B));
Devang Patela05633e2008-09-26 22:53:05 +00001213 }
Devang Patela05633e2008-09-26 22:53:05 +00001214
Bill Wendlinge94d8432012-12-07 23:16:57 +00001215 MAttributes.push_back(AttributeSet::get(Context, Attrs));
Chris Lattnerfee5a372007-05-04 03:30:17 +00001216 Attrs.clear();
1217 break;
1218 }
Bill Wendling0dc08912013-02-12 08:13:50 +00001219 case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [attrgrp0, attrgrp1, ...]
1220 for (unsigned i = 0, e = Record.size(); i != e; ++i)
1221 Attrs.push_back(MAttributeGroups[Record[i]]);
1222
1223 MAttributes.push_back(AttributeSet::get(Context, Attrs));
1224 Attrs.clear();
1225 break;
1226 }
Duncan Sands04eb67e2007-11-20 14:09:29 +00001227 }
Chris Lattnerfee5a372007-05-04 03:30:17 +00001228 }
1229}
1230
Reid Klecknere9f36af2013-11-12 01:31:00 +00001231// Returns Attribute::None on unrecognized codes.
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001232static Attribute::AttrKind getAttrFromCode(uint64_t Code) {
Reid Klecknere9f36af2013-11-12 01:31:00 +00001233 switch (Code) {
1234 default:
1235 return Attribute::None;
1236 case bitc::ATTR_KIND_ALIGNMENT:
1237 return Attribute::Alignment;
1238 case bitc::ATTR_KIND_ALWAYS_INLINE:
1239 return Attribute::AlwaysInline;
Igor Laevsky39d662f2015-07-11 10:30:36 +00001240 case bitc::ATTR_KIND_ARGMEMONLY:
1241 return Attribute::ArgMemOnly;
Reid Klecknere9f36af2013-11-12 01:31:00 +00001242 case bitc::ATTR_KIND_BUILTIN:
1243 return Attribute::Builtin;
1244 case bitc::ATTR_KIND_BY_VAL:
1245 return Attribute::ByVal;
Reid Klecknera534a382013-12-19 02:14:12 +00001246 case bitc::ATTR_KIND_IN_ALLOCA:
1247 return Attribute::InAlloca;
Reid Klecknere9f36af2013-11-12 01:31:00 +00001248 case bitc::ATTR_KIND_COLD:
1249 return Attribute::Cold;
Owen Anderson85fa7d52015-05-26 23:48:40 +00001250 case bitc::ATTR_KIND_CONVERGENT:
1251 return Attribute::Convergent;
Vaivaswatha Nagarajfb3f4902015-12-16 16:16:19 +00001252 case bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY:
1253 return Attribute::InaccessibleMemOnly;
1254 case bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY:
1255 return Attribute::InaccessibleMemOrArgMemOnly;
Reid Klecknere9f36af2013-11-12 01:31:00 +00001256 case bitc::ATTR_KIND_INLINE_HINT:
1257 return Attribute::InlineHint;
1258 case bitc::ATTR_KIND_IN_REG:
1259 return Attribute::InReg;
Tom Roeder44cb65f2014-06-05 19:29:43 +00001260 case bitc::ATTR_KIND_JUMP_TABLE:
1261 return Attribute::JumpTable;
Reid Klecknere9f36af2013-11-12 01:31:00 +00001262 case bitc::ATTR_KIND_MIN_SIZE:
1263 return Attribute::MinSize;
1264 case bitc::ATTR_KIND_NAKED:
1265 return Attribute::Naked;
1266 case bitc::ATTR_KIND_NEST:
1267 return Attribute::Nest;
1268 case bitc::ATTR_KIND_NO_ALIAS:
1269 return Attribute::NoAlias;
1270 case bitc::ATTR_KIND_NO_BUILTIN:
1271 return Attribute::NoBuiltin;
1272 case bitc::ATTR_KIND_NO_CAPTURE:
1273 return Attribute::NoCapture;
1274 case bitc::ATTR_KIND_NO_DUPLICATE:
1275 return Attribute::NoDuplicate;
1276 case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT:
1277 return Attribute::NoImplicitFloat;
1278 case bitc::ATTR_KIND_NO_INLINE:
1279 return Attribute::NoInline;
James Molloye6f87ca2015-11-06 10:32:53 +00001280 case bitc::ATTR_KIND_NO_RECURSE:
1281 return Attribute::NoRecurse;
Reid Klecknere9f36af2013-11-12 01:31:00 +00001282 case bitc::ATTR_KIND_NON_LAZY_BIND:
1283 return Attribute::NonLazyBind;
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001284 case bitc::ATTR_KIND_NON_NULL:
1285 return Attribute::NonNull;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001286 case bitc::ATTR_KIND_DEREFERENCEABLE:
1287 return Attribute::Dereferenceable;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001288 case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL:
1289 return Attribute::DereferenceableOrNull;
Reid Klecknere9f36af2013-11-12 01:31:00 +00001290 case bitc::ATTR_KIND_NO_RED_ZONE:
1291 return Attribute::NoRedZone;
1292 case bitc::ATTR_KIND_NO_RETURN:
1293 return Attribute::NoReturn;
1294 case bitc::ATTR_KIND_NO_UNWIND:
1295 return Attribute::NoUnwind;
1296 case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE:
1297 return Attribute::OptimizeForSize;
1298 case bitc::ATTR_KIND_OPTIMIZE_NONE:
1299 return Attribute::OptimizeNone;
1300 case bitc::ATTR_KIND_READ_NONE:
1301 return Attribute::ReadNone;
1302 case bitc::ATTR_KIND_READ_ONLY:
1303 return Attribute::ReadOnly;
1304 case bitc::ATTR_KIND_RETURNED:
1305 return Attribute::Returned;
1306 case bitc::ATTR_KIND_RETURNS_TWICE:
1307 return Attribute::ReturnsTwice;
1308 case bitc::ATTR_KIND_S_EXT:
1309 return Attribute::SExt;
1310 case bitc::ATTR_KIND_STACK_ALIGNMENT:
1311 return Attribute::StackAlignment;
1312 case bitc::ATTR_KIND_STACK_PROTECT:
1313 return Attribute::StackProtect;
1314 case bitc::ATTR_KIND_STACK_PROTECT_REQ:
1315 return Attribute::StackProtectReq;
1316 case bitc::ATTR_KIND_STACK_PROTECT_STRONG:
1317 return Attribute::StackProtectStrong;
Peter Collingbourne82437bf2015-06-15 21:07:11 +00001318 case bitc::ATTR_KIND_SAFESTACK:
1319 return Attribute::SafeStack;
Reid Klecknere9f36af2013-11-12 01:31:00 +00001320 case bitc::ATTR_KIND_STRUCT_RET:
1321 return Attribute::StructRet;
1322 case bitc::ATTR_KIND_SANITIZE_ADDRESS:
1323 return Attribute::SanitizeAddress;
1324 case bitc::ATTR_KIND_SANITIZE_THREAD:
1325 return Attribute::SanitizeThread;
1326 case bitc::ATTR_KIND_SANITIZE_MEMORY:
1327 return Attribute::SanitizeMemory;
1328 case bitc::ATTR_KIND_UW_TABLE:
1329 return Attribute::UWTable;
1330 case bitc::ATTR_KIND_Z_EXT:
1331 return Attribute::ZExt;
1332 }
1333}
1334
JF Bastien30bf96b2015-02-22 19:32:03 +00001335std::error_code BitcodeReader::parseAlignmentValue(uint64_t Exponent,
1336 unsigned &Alignment) {
1337 // Note: Alignment in bitcode files is incremented by 1, so that zero
1338 // can be used for default alignment.
1339 if (Exponent > Value::MaxAlignmentExponent + 1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001340 return error("Invalid alignment value");
JF Bastien30bf96b2015-02-22 19:32:03 +00001341 Alignment = (1 << static_cast<unsigned>(Exponent)) >> 1;
1342 return std::error_code();
1343}
1344
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001345std::error_code BitcodeReader::parseAttrKind(uint64_t Code,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001346 Attribute::AttrKind *Kind) {
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001347 *Kind = getAttrFromCode(Code);
Reid Klecknere9f36af2013-11-12 01:31:00 +00001348 if (*Kind == Attribute::None)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001349 return error(BitcodeError::CorruptedBitcode,
Rafael Espindolad0b23be2015-01-10 00:07:30 +00001350 "Unknown attribute kind (" + Twine(Code) + ")");
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001351 return std::error_code();
Tobias Grosser0a8e12f2013-07-26 04:16:55 +00001352}
1353
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001354std::error_code BitcodeReader::parseAttributeGroupBlock() {
Bill Wendlingba629332013-02-10 23:24:25 +00001355 if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001356 return error("Invalid record");
Bill Wendlingba629332013-02-10 23:24:25 +00001357
1358 if (!MAttributeGroups.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001359 return error("Invalid multiple blocks");
Bill Wendlingba629332013-02-10 23:24:25 +00001360
1361 SmallVector<uint64_t, 64> Record;
1362
1363 // Read all the records.
1364 while (1) {
1365 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1366
1367 switch (Entry.Kind) {
1368 case BitstreamEntry::SubBlock: // Handled for us already.
1369 case BitstreamEntry::Error:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001370 return error("Malformed block");
Bill Wendlingba629332013-02-10 23:24:25 +00001371 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001372 return std::error_code();
Bill Wendlingba629332013-02-10 23:24:25 +00001373 case BitstreamEntry::Record:
1374 // The interesting case.
1375 break;
1376 }
1377
1378 // Read a record.
1379 Record.clear();
1380 switch (Stream.readRecord(Entry.ID, Record)) {
1381 default: // Default behavior: ignore.
1382 break;
1383 case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...]
1384 if (Record.size() < 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001385 return error("Invalid record");
Bill Wendlingba629332013-02-10 23:24:25 +00001386
Bill Wendlinge46707e2013-02-11 22:32:29 +00001387 uint64_t GrpID = Record[0];
Bill Wendlingba629332013-02-10 23:24:25 +00001388 uint64_t Idx = Record[1]; // Index of the object this attribute refers to.
1389
1390 AttrBuilder B;
1391 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
1392 if (Record[i] == 0) { // Enum attribute
Tobias Grosser0a8e12f2013-07-26 04:16:55 +00001393 Attribute::AttrKind Kind;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001394 if (std::error_code EC = parseAttrKind(Record[++i], &Kind))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001395 return EC;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +00001396
1397 B.addAttribute(Kind);
Hal Finkele15442c2014-07-18 06:51:55 +00001398 } else if (Record[i] == 1) { // Integer attribute
Tobias Grosser0a8e12f2013-07-26 04:16:55 +00001399 Attribute::AttrKind Kind;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001400 if (std::error_code EC = parseAttrKind(Record[++i], &Kind))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001401 return EC;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +00001402 if (Kind == Attribute::Alignment)
Bill Wendlingba629332013-02-10 23:24:25 +00001403 B.addAlignmentAttr(Record[++i]);
Hal Finkelb0407ba2014-07-18 15:51:28 +00001404 else if (Kind == Attribute::StackAlignment)
Bill Wendlingba629332013-02-10 23:24:25 +00001405 B.addStackAlignmentAttr(Record[++i]);
Hal Finkelb0407ba2014-07-18 15:51:28 +00001406 else if (Kind == Attribute::Dereferenceable)
1407 B.addDereferenceableAttr(Record[++i]);
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001408 else if (Kind == Attribute::DereferenceableOrNull)
1409 B.addDereferenceableOrNullAttr(Record[++i]);
Bill Wendlingba629332013-02-10 23:24:25 +00001410 } else { // String attribute
Bill Wendlinge46707e2013-02-11 22:32:29 +00001411 assert((Record[i] == 3 || Record[i] == 4) &&
1412 "Invalid attribute group entry");
Bill Wendlingba629332013-02-10 23:24:25 +00001413 bool HasValue = (Record[i++] == 4);
1414 SmallString<64> KindStr;
1415 SmallString<64> ValStr;
1416
1417 while (Record[i] != 0 && i != e)
1418 KindStr += Record[i++];
Bill Wendlinge46707e2013-02-11 22:32:29 +00001419 assert(Record[i] == 0 && "Kind string not null terminated");
Bill Wendlingba629332013-02-10 23:24:25 +00001420
1421 if (HasValue) {
1422 // Has a value associated with it.
Bill Wendlinge46707e2013-02-11 22:32:29 +00001423 ++i; // Skip the '0' that terminates the "kind" string.
Bill Wendlingba629332013-02-10 23:24:25 +00001424 while (Record[i] != 0 && i != e)
1425 ValStr += Record[i++];
Bill Wendlinge46707e2013-02-11 22:32:29 +00001426 assert(Record[i] == 0 && "Value string not null terminated");
Bill Wendlingba629332013-02-10 23:24:25 +00001427 }
1428
1429 B.addAttribute(KindStr.str(), ValStr.str());
1430 }
1431 }
1432
Bill Wendlinge46707e2013-02-11 22:32:29 +00001433 MAttributeGroups[GrpID] = AttributeSet::get(Context, Idx, B);
Bill Wendlingba629332013-02-10 23:24:25 +00001434 break;
1435 }
1436 }
1437 }
1438}
1439
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001440std::error_code BitcodeReader::parseTypeTable() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001441 if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001442 return error("Invalid record");
Derek Schuff206dddd2012-02-06 19:03:04 +00001443
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001444 return parseTypeTableBody();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001445}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001446
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001447std::error_code BitcodeReader::parseTypeTableBody() {
Chris Lattner1314b992007-04-22 06:23:29 +00001448 if (!TypeList.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001449 return error("Invalid multiple blocks");
Chris Lattner1314b992007-04-22 06:23:29 +00001450
1451 SmallVector<uint64_t, 64> Record;
1452 unsigned NumRecords = 0;
1453
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001454 SmallString<64> TypeName;
Derek Schuff206dddd2012-02-06 19:03:04 +00001455
Chris Lattner1314b992007-04-22 06:23:29 +00001456 // Read all the records for this type table.
1457 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00001458 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00001459
Chris Lattner27d38752013-01-20 02:13:19 +00001460 switch (Entry.Kind) {
1461 case BitstreamEntry::SubBlock: // Handled for us already.
1462 case BitstreamEntry::Error:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001463 return error("Malformed block");
Chris Lattner27d38752013-01-20 02:13:19 +00001464 case BitstreamEntry::EndBlock:
Chris Lattner1314b992007-04-22 06:23:29 +00001465 if (NumRecords != TypeList.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001466 return error("Malformed block");
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001467 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00001468 case BitstreamEntry::Record:
1469 // The interesting case.
1470 break;
Chris Lattner1314b992007-04-22 06:23:29 +00001471 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001472
Chris Lattner1314b992007-04-22 06:23:29 +00001473 // Read a record.
1474 Record.clear();
Craig Topper2617dcc2014-04-15 06:32:26 +00001475 Type *ResultTy = nullptr;
Chris Lattner27d38752013-01-20 02:13:19 +00001476 switch (Stream.readRecord(Entry.ID, Record)) {
Rafael Espindola48da4f42013-11-04 16:16:24 +00001477 default:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001478 return error("Invalid value");
Chris Lattner1314b992007-04-22 06:23:29 +00001479 case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
1480 // TYPE_CODE_NUMENTRY contains a count of the number of types in the
1481 // type list. This allows us to reserve space.
1482 if (Record.size() < 1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001483 return error("Invalid record");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001484 TypeList.resize(Record[0]);
Chris Lattner1314b992007-04-22 06:23:29 +00001485 continue;
Chris Lattner1314b992007-04-22 06:23:29 +00001486 case bitc::TYPE_CODE_VOID: // VOID
Owen Anderson55f1c092009-08-13 21:58:54 +00001487 ResultTy = Type::getVoidTy(Context);
Chris Lattner1314b992007-04-22 06:23:29 +00001488 break;
Dan Gohman518cda42011-12-17 00:04:22 +00001489 case bitc::TYPE_CODE_HALF: // HALF
1490 ResultTy = Type::getHalfTy(Context);
1491 break;
Chris Lattner1314b992007-04-22 06:23:29 +00001492 case bitc::TYPE_CODE_FLOAT: // FLOAT
Owen Anderson55f1c092009-08-13 21:58:54 +00001493 ResultTy = Type::getFloatTy(Context);
Chris Lattner1314b992007-04-22 06:23:29 +00001494 break;
1495 case bitc::TYPE_CODE_DOUBLE: // DOUBLE
Owen Anderson55f1c092009-08-13 21:58:54 +00001496 ResultTy = Type::getDoubleTy(Context);
Chris Lattner1314b992007-04-22 06:23:29 +00001497 break;
Dale Johannesenff4c3be2007-08-03 01:03:46 +00001498 case bitc::TYPE_CODE_X86_FP80: // X86_FP80
Owen Anderson55f1c092009-08-13 21:58:54 +00001499 ResultTy = Type::getX86_FP80Ty(Context);
Dale Johannesenff4c3be2007-08-03 01:03:46 +00001500 break;
1501 case bitc::TYPE_CODE_FP128: // FP128
Owen Anderson55f1c092009-08-13 21:58:54 +00001502 ResultTy = Type::getFP128Ty(Context);
Dale Johannesenff4c3be2007-08-03 01:03:46 +00001503 break;
1504 case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128
Owen Anderson55f1c092009-08-13 21:58:54 +00001505 ResultTy = Type::getPPC_FP128Ty(Context);
Dale Johannesenff4c3be2007-08-03 01:03:46 +00001506 break;
Chris Lattner1314b992007-04-22 06:23:29 +00001507 case bitc::TYPE_CODE_LABEL: // LABEL
Owen Anderson55f1c092009-08-13 21:58:54 +00001508 ResultTy = Type::getLabelTy(Context);
Chris Lattner1314b992007-04-22 06:23:29 +00001509 break;
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001510 case bitc::TYPE_CODE_METADATA: // METADATA
Owen Anderson55f1c092009-08-13 21:58:54 +00001511 ResultTy = Type::getMetadataTy(Context);
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001512 break;
Dale Johannesenbaa5d042010-09-10 20:55:01 +00001513 case bitc::TYPE_CODE_X86_MMX: // X86_MMX
1514 ResultTy = Type::getX86_MMXTy(Context);
1515 break;
David Majnemerb611e3f2015-08-14 05:09:07 +00001516 case bitc::TYPE_CODE_TOKEN: // TOKEN
1517 ResultTy = Type::getTokenTy(Context);
1518 break;
Filipe Cabecinhasfcd044b2015-01-30 18:13:50 +00001519 case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width]
Chris Lattner1314b992007-04-22 06:23:29 +00001520 if (Record.size() < 1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001521 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001522
Filipe Cabecinhasfcd044b2015-01-30 18:13:50 +00001523 uint64_t NumBits = Record[0];
1524 if (NumBits < IntegerType::MIN_INT_BITS ||
1525 NumBits > IntegerType::MAX_INT_BITS)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001526 return error("Bitwidth for integer type out of range");
Filipe Cabecinhasfcd044b2015-01-30 18:13:50 +00001527 ResultTy = IntegerType::get(Context, NumBits);
Chris Lattner1314b992007-04-22 06:23:29 +00001528 break;
Filipe Cabecinhasfcd044b2015-01-30 18:13:50 +00001529 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001530 case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001531 // [pointee type, address space]
Chris Lattner1314b992007-04-22 06:23:29 +00001532 if (Record.size() < 1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001533 return error("Invalid record");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001534 unsigned AddressSpace = 0;
1535 if (Record.size() == 2)
1536 AddressSpace = Record[1];
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001537 ResultTy = getTypeByID(Record[0]);
Filipe Cabecinhasd8a1bcd2015-04-29 02:27:28 +00001538 if (!ResultTy ||
1539 !PointerType::isValidElementType(ResultTy))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001540 return error("Invalid type");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001541 ResultTy = PointerType::get(ResultTy, AddressSpace);
Chris Lattner1314b992007-04-22 06:23:29 +00001542 break;
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001543 }
Nuno Lopes561dae02012-05-23 15:19:39 +00001544 case bitc::TYPE_CODE_FUNCTION_OLD: {
1545 // FIXME: attrid is dead, remove it in LLVM 4.0
1546 // FUNCTION: [vararg, attrid, retty, paramty x N]
1547 if (Record.size() < 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001548 return error("Invalid record");
Nuno Lopes561dae02012-05-23 15:19:39 +00001549 SmallVector<Type*, 8> ArgTys;
1550 for (unsigned i = 3, e = Record.size(); i != e; ++i) {
1551 if (Type *T = getTypeByID(Record[i]))
1552 ArgTys.push_back(T);
1553 else
1554 break;
1555 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001556
Nuno Lopes561dae02012-05-23 15:19:39 +00001557 ResultTy = getTypeByID(Record[2]);
Craig Topper2617dcc2014-04-15 06:32:26 +00001558 if (!ResultTy || ArgTys.size() < Record.size()-3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001559 return error("Invalid type");
Nuno Lopes561dae02012-05-23 15:19:39 +00001560
1561 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
1562 break;
1563 }
Chad Rosier95898722011-11-03 00:14:01 +00001564 case bitc::TYPE_CODE_FUNCTION: {
1565 // FUNCTION: [vararg, retty, paramty x N]
1566 if (Record.size() < 2)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001567 return error("Invalid record");
Chris Lattnercc3aaf12012-01-27 03:15:49 +00001568 SmallVector<Type*, 8> ArgTys;
Chad Rosier95898722011-11-03 00:14:01 +00001569 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
Filipe Cabecinhas32af5422015-05-19 01:21:06 +00001570 if (Type *T = getTypeByID(Record[i])) {
1571 if (!FunctionType::isValidArgumentType(T))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001572 return error("Invalid function argument type");
Chad Rosier95898722011-11-03 00:14:01 +00001573 ArgTys.push_back(T);
Filipe Cabecinhas32af5422015-05-19 01:21:06 +00001574 }
Chad Rosier95898722011-11-03 00:14:01 +00001575 else
1576 break;
1577 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001578
Chad Rosier95898722011-11-03 00:14:01 +00001579 ResultTy = getTypeByID(Record[1]);
Craig Topper2617dcc2014-04-15 06:32:26 +00001580 if (!ResultTy || ArgTys.size() < Record.size()-2)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001581 return error("Invalid type");
Chad Rosier95898722011-11-03 00:14:01 +00001582
1583 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
1584 break;
1585 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001586 case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N]
Chris Lattner3c5616e2007-05-06 08:21:50 +00001587 if (Record.size() < 1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001588 return error("Invalid record");
Chris Lattnercc3aaf12012-01-27 03:15:49 +00001589 SmallVector<Type*, 8> EltTys;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001590 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
1591 if (Type *T = getTypeByID(Record[i]))
1592 EltTys.push_back(T);
1593 else
1594 break;
1595 }
1596 if (EltTys.size() != Record.size()-1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001597 return error("Invalid type");
Owen Anderson03cb69f2009-08-05 23:16:16 +00001598 ResultTy = StructType::get(Context, EltTys, Record[0]);
Chris Lattner1314b992007-04-22 06:23:29 +00001599 break;
1600 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001601 case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N]
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001602 if (convertToString(Record, 0, TypeName))
1603 return error("Invalid record");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001604 continue;
1605
1606 case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N]
1607 if (Record.size() < 1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001608 return error("Invalid record");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001609
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001610 if (NumRecords >= TypeList.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001611 return error("Invalid TYPE table");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001612
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001613 // Check to see if this was forward referenced, if so fill in the temp.
1614 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
1615 if (Res) {
1616 Res->setName(TypeName);
Craig Topper2617dcc2014-04-15 06:32:26 +00001617 TypeList[NumRecords] = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001618 } else // Otherwise, create a new struct.
Rafael Espindola2fa1e432014-12-03 07:18:23 +00001619 Res = createIdentifiedStructType(Context, TypeName);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001620 TypeName.clear();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001621
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001622 SmallVector<Type*, 8> EltTys;
1623 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
1624 if (Type *T = getTypeByID(Record[i]))
1625 EltTys.push_back(T);
1626 else
1627 break;
1628 }
1629 if (EltTys.size() != Record.size()-1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001630 return error("Invalid record");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001631 Res->setBody(EltTys, Record[0]);
1632 ResultTy = Res;
1633 break;
1634 }
1635 case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: []
1636 if (Record.size() != 1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001637 return error("Invalid record");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001638
1639 if (NumRecords >= TypeList.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001640 return error("Invalid TYPE table");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001641
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001642 // Check to see if this was forward referenced, if so fill in the temp.
1643 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
1644 if (Res) {
1645 Res->setName(TypeName);
Craig Topper2617dcc2014-04-15 06:32:26 +00001646 TypeList[NumRecords] = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001647 } else // Otherwise, create a new struct with no body.
Rafael Espindola2fa1e432014-12-03 07:18:23 +00001648 Res = createIdentifiedStructType(Context, TypeName);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001649 TypeName.clear();
1650 ResultTy = Res;
1651 break;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001652 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001653 case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty]
1654 if (Record.size() < 2)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001655 return error("Invalid record");
Filipe Cabecinhas6fe8aab2015-04-29 02:36:08 +00001656 ResultTy = getTypeByID(Record[1]);
1657 if (!ResultTy || !ArrayType::isValidElementType(ResultTy))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001658 return error("Invalid type");
Filipe Cabecinhas6fe8aab2015-04-29 02:36:08 +00001659 ResultTy = ArrayType::get(ResultTy, Record[0]);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001660 break;
1661 case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty]
1662 if (Record.size() < 2)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001663 return error("Invalid record");
Filipe Cabecinhas8e421902015-06-03 00:05:30 +00001664 if (Record[0] == 0)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001665 return error("Invalid vector length");
Filipe Cabecinhas6fe8aab2015-04-29 02:36:08 +00001666 ResultTy = getTypeByID(Record[1]);
1667 if (!ResultTy || !StructType::isValidElementType(ResultTy))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001668 return error("Invalid type");
Filipe Cabecinhas6fe8aab2015-04-29 02:36:08 +00001669 ResultTy = VectorType::get(ResultTy, Record[0]);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001670 break;
1671 }
1672
1673 if (NumRecords >= TypeList.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001674 return error("Invalid TYPE table");
Filipe Cabecinhasd0858e12015-01-30 10:57:58 +00001675 if (TypeList[NumRecords])
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001676 return error(
Filipe Cabecinhasd0858e12015-01-30 10:57:58 +00001677 "Invalid TYPE table: Only named structs can be forward referenced");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001678 assert(ResultTy && "Didn't read a type?");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001679 TypeList[NumRecords++] = ResultTy;
1680 }
1681}
1682
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00001683std::error_code BitcodeReader::parseOperandBundleTags() {
1684 if (Stream.EnterSubBlock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID))
1685 return error("Invalid record");
1686
1687 if (!BundleTags.empty())
1688 return error("Invalid multiple blocks");
1689
1690 SmallVector<uint64_t, 64> Record;
1691
1692 while (1) {
1693 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1694
1695 switch (Entry.Kind) {
1696 case BitstreamEntry::SubBlock: // Handled for us already.
1697 case BitstreamEntry::Error:
1698 return error("Malformed block");
1699 case BitstreamEntry::EndBlock:
1700 return std::error_code();
1701 case BitstreamEntry::Record:
1702 // The interesting case.
1703 break;
1704 }
1705
1706 // Tags are implicitly mapped to integers by their order.
1707
1708 if (Stream.readRecord(Entry.ID, Record) != bitc::OPERAND_BUNDLE_TAG)
1709 return error("Invalid record");
1710
1711 // OPERAND_BUNDLE_TAG: [strchr x N]
1712 BundleTags.emplace_back();
1713 if (convertToString(Record, 0, BundleTags.back()))
1714 return error("Invalid record");
1715 Record.clear();
1716 }
1717}
1718
Teresa Johnsonff642b92015-09-17 20:12:00 +00001719/// Associate a value with its name from the given index in the provided record.
1720ErrorOr<Value *> BitcodeReader::recordValue(SmallVectorImpl<uint64_t> &Record,
1721 unsigned NameIndex, Triple &TT) {
1722 SmallString<128> ValueName;
1723 if (convertToString(Record, NameIndex, ValueName))
1724 return error("Invalid record");
1725 unsigned ValueID = Record[0];
1726 if (ValueID >= ValueList.size() || !ValueList[ValueID])
1727 return error("Invalid record");
1728 Value *V = ValueList[ValueID];
1729
Filipe Cabecinhasa2b0ac42015-11-04 14:53:36 +00001730 StringRef NameStr(ValueName.data(), ValueName.size());
1731 if (NameStr.find_first_of(0) != StringRef::npos)
1732 return error("Invalid value name");
1733 V->setName(NameStr);
Teresa Johnsonff642b92015-09-17 20:12:00 +00001734 auto *GO = dyn_cast<GlobalObject>(V);
1735 if (GO) {
1736 if (GO->getComdat() == reinterpret_cast<Comdat *>(1)) {
1737 if (TT.isOSBinFormatMachO())
1738 GO->setComdat(nullptr);
1739 else
1740 GO->setComdat(TheModule->getOrInsertComdat(V->getName()));
1741 }
1742 }
1743 return V;
1744}
1745
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00001746/// Helper to note and return the current location, and jump to the given
1747/// offset.
1748static uint64_t jumpToValueSymbolTable(uint64_t Offset,
1749 BitstreamCursor &Stream) {
1750 // Save the current parsing location so we can jump back at the end
1751 // of the VST read.
1752 uint64_t CurrentBit = Stream.GetCurrentBitNo();
1753 Stream.JumpToBit(Offset * 32);
1754#ifndef NDEBUG
1755 // Do some checking if we are in debug mode.
1756 BitstreamEntry Entry = Stream.advance();
1757 assert(Entry.Kind == BitstreamEntry::SubBlock);
1758 assert(Entry.ID == bitc::VALUE_SYMTAB_BLOCK_ID);
1759#else
1760 // In NDEBUG mode ignore the output so we don't get an unused variable
1761 // warning.
1762 Stream.advance();
1763#endif
1764 return CurrentBit;
1765}
1766
Teresa Johnsonff642b92015-09-17 20:12:00 +00001767/// Parse the value symbol table at either the current parsing location or
1768/// at the given bit offset if provided.
Peter Collingbourne128a9762015-10-27 23:01:25 +00001769std::error_code BitcodeReader::parseValueSymbolTable(uint64_t Offset) {
Teresa Johnsonff642b92015-09-17 20:12:00 +00001770 uint64_t CurrentBit;
1771 // Pass in the Offset to distinguish between calling for the module-level
1772 // VST (where we want to jump to the VST offset) and the function-level
1773 // VST (where we don't).
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00001774 if (Offset > 0)
1775 CurrentBit = jumpToValueSymbolTable(Offset, Stream);
Teresa Johnsonff642b92015-09-17 20:12:00 +00001776
1777 // Compute the delta between the bitcode indices in the VST (the word offset
1778 // to the word-aligned ENTER_SUBBLOCK for the function block, and that
1779 // expected by the lazy reader. The reader's EnterSubBlock expects to have
1780 // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID
1781 // (size BlockIDWidth). Note that we access the stream's AbbrevID width here
1782 // just before entering the VST subblock because: 1) the EnterSubBlock
1783 // changes the AbbrevID width; 2) the VST block is nested within the same
1784 // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same
1785 // AbbrevID width before calling EnterSubBlock; and 3) when we want to
1786 // jump to the FUNCTION_BLOCK using this offset later, we don't want
1787 // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK.
1788 unsigned FuncBitcodeOffsetDelta =
1789 Stream.getAbbrevIDWidth() + bitc::BlockIDWidth;
1790
Chris Lattner982ec1e2007-05-05 00:17:00 +00001791 if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001792 return error("Invalid record");
Chris Lattnerccaa4482007-04-23 21:26:05 +00001793
1794 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001795
David Majnemer3087b222015-01-20 05:58:07 +00001796 Triple TT(TheModule->getTargetTriple());
1797
Chris Lattnerccaa4482007-04-23 21:26:05 +00001798 // Read all the records for this value table.
1799 SmallString<128> ValueName;
1800 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00001801 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00001802
Chris Lattner27d38752013-01-20 02:13:19 +00001803 switch (Entry.Kind) {
1804 case BitstreamEntry::SubBlock: // Handled for us already.
1805 case BitstreamEntry::Error:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001806 return error("Malformed block");
Chris Lattner27d38752013-01-20 02:13:19 +00001807 case BitstreamEntry::EndBlock:
Teresa Johnsonff642b92015-09-17 20:12:00 +00001808 if (Offset > 0)
1809 Stream.JumpToBit(CurrentBit);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001810 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00001811 case BitstreamEntry::Record:
1812 // The interesting case.
1813 break;
Chris Lattnerccaa4482007-04-23 21:26:05 +00001814 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001815
Chris Lattnerccaa4482007-04-23 21:26:05 +00001816 // Read a record.
1817 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +00001818 switch (Stream.readRecord(Entry.ID, Record)) {
Chris Lattnerccaa4482007-04-23 21:26:05 +00001819 default: // Default behavior: unknown type.
1820 break;
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00001821 case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N]
Teresa Johnsonff642b92015-09-17 20:12:00 +00001822 ErrorOr<Value *> ValOrErr = recordValue(Record, 1, TT);
1823 if (std::error_code EC = ValOrErr.getError())
1824 return EC;
1825 ValOrErr.get();
1826 break;
1827 }
1828 case bitc::VST_CODE_FNENTRY: {
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00001829 // VST_CODE_FNENTRY: [valueid, offset, namechar x N]
Teresa Johnsonff642b92015-09-17 20:12:00 +00001830 ErrorOr<Value *> ValOrErr = recordValue(Record, 2, TT);
1831 if (std::error_code EC = ValOrErr.getError())
1832 return EC;
1833 Value *V = ValOrErr.get();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001834
Teresa Johnsonff642b92015-09-17 20:12:00 +00001835 auto *GO = dyn_cast<GlobalObject>(V);
1836 if (!GO) {
1837 // If this is an alias, need to get the actual Function object
1838 // it aliases, in order to set up the DeferredFunctionInfo entry below.
1839 auto *GA = dyn_cast<GlobalAlias>(V);
1840 if (GA)
1841 GO = GA->getBaseObject();
1842 assert(GO);
Rafael Espindola12ca34f2015-01-19 15:16:06 +00001843 }
Teresa Johnsonff642b92015-09-17 20:12:00 +00001844
1845 uint64_t FuncWordOffset = Record[1];
1846 Function *F = dyn_cast<Function>(GO);
1847 assert(F);
1848 uint64_t FuncBitOffset = FuncWordOffset * 32;
1849 DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta;
Teresa Johnson1493ad92015-10-10 14:18:36 +00001850 // Set the LastFunctionBlockBit to point to the last function block.
Teresa Johnsonff642b92015-09-17 20:12:00 +00001851 // Later when parsing is resumed after function materialization,
1852 // we can simply skip that last function block.
Teresa Johnson1493ad92015-10-10 14:18:36 +00001853 if (FuncBitOffset > LastFunctionBlockBit)
1854 LastFunctionBlockBit = FuncBitOffset;
Chris Lattnerccaa4482007-04-23 21:26:05 +00001855 break;
Reid Spencerdea02bd2007-05-04 01:43:33 +00001856 }
Bill Wendling35a9c3c2011-04-10 23:18:04 +00001857 case bitc::VST_CODE_BBENTRY: {
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001858 if (convertToString(Record, 1, ValueName))
1859 return error("Invalid record");
Chris Lattner6be58c62007-05-03 22:18:21 +00001860 BasicBlock *BB = getBasicBlock(Record[0]);
Craig Topper2617dcc2014-04-15 06:32:26 +00001861 if (!BB)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001862 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001863
Daniel Dunbard786b512009-07-26 00:34:27 +00001864 BB->setName(StringRef(ValueName.data(), ValueName.size()));
Chris Lattner6be58c62007-05-03 22:18:21 +00001865 ValueName.clear();
1866 break;
Chris Lattnerccaa4482007-04-23 21:26:05 +00001867 }
Reid Spencerdea02bd2007-05-04 01:43:33 +00001868 }
Chris Lattnerccaa4482007-04-23 21:26:05 +00001869 }
1870}
1871
Teresa Johnson12545072015-11-15 02:00:09 +00001872/// Parse a single METADATA_KIND record, inserting result in MDKindMap.
1873std::error_code
1874BitcodeReader::parseMetadataKindRecord(SmallVectorImpl<uint64_t> &Record) {
1875 if (Record.size() < 2)
1876 return error("Invalid record");
1877
1878 unsigned Kind = Record[0];
1879 SmallString<8> Name(Record.begin() + 1, Record.end());
1880
1881 unsigned NewKind = TheModule->getMDKindID(Name.str());
1882 if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second)
1883 return error("Conflicting METADATA_KIND records");
1884 return std::error_code();
1885}
1886
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001887static int64_t unrotateSign(uint64_t U) { return U & 1 ? ~(U >> 1) : U >> 1; }
1888
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001889std::error_code BitcodeReader::parseMetadataStrings(ArrayRef<uint64_t> Record,
1890 StringRef Blob,
1891 unsigned &NextMetadataNo) {
1892 // All the MDStrings in the block are emitted together in a single
1893 // record. The strings are concatenated and stored in a blob along with
1894 // their sizes.
1895 if (Record.size() != 2)
1896 return error("Invalid record: metadata strings layout");
1897
1898 unsigned NumStrings = Record[0];
1899 unsigned StringsOffset = Record[1];
1900 if (!NumStrings)
1901 return error("Invalid record: metadata strings with no strings");
Duncan P. N. Exon Smithbb7ce3b2016-03-29 05:25:17 +00001902 if (StringsOffset > Blob.size())
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001903 return error("Invalid record: metadata strings corrupt offset");
1904
1905 StringRef Lengths = Blob.slice(0, StringsOffset);
1906 SimpleBitstreamCursor R(*StreamFile);
1907 R.jumpToPointer(Lengths.begin());
1908
1909 // Ensure that Blob doesn't get invalidated, even if this is reading from
1910 // a StreamingMemoryObject with corrupt data.
1911 R.setArtificialByteLimit(R.getCurrentByteNo() + StringsOffset);
1912
1913 StringRef Strings = Blob.drop_front(StringsOffset);
1914 do {
1915 if (R.AtEndOfStream())
1916 return error("Invalid record: metadata strings bad length");
1917
1918 unsigned Size = R.ReadVBR(6);
1919 if (Strings.size() < Size)
1920 return error("Invalid record: metadata strings truncated chars");
1921
1922 MetadataList.assignValue(MDString::get(Context, Strings.slice(0, Size)),
1923 NextMetadataNo++);
1924 Strings = Strings.drop_front(Size);
1925 } while (--NumStrings);
1926
1927 return std::error_code();
1928}
1929
Teresa Johnsond4d3dfd2015-11-20 14:51:27 +00001930/// Parse a METADATA_BLOCK. If ModuleLevel is true then we are parsing
1931/// module level metadata.
1932std::error_code BitcodeReader::parseMetadata(bool ModuleLevel) {
Manman Ren4a9b0eb2015-03-13 19:24:30 +00001933 IsMetadataMaterialized = true;
Teresa Johnson61b406e2015-12-29 23:00:22 +00001934 unsigned NextMetadataNo = MetadataList.size();
Devang Patel7428d8a2009-07-22 17:43:22 +00001935
1936 if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001937 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001938
Devang Patel7428d8a2009-07-22 17:43:22 +00001939 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001940
Teresa Johnson61b406e2015-12-29 23:00:22 +00001941 auto getMD = [&](unsigned ID) -> Metadata * {
Justin Bognerae341c62016-03-17 20:12:06 +00001942 return MetadataList.getMetadataFwdRef(ID);
Teresa Johnson61b406e2015-12-29 23:00:22 +00001943 };
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001944 auto getMDOrNull = [&](unsigned ID) -> Metadata *{
1945 if (ID)
1946 return getMD(ID - 1);
1947 return nullptr;
1948 };
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001949 auto getMDString = [&](unsigned ID) -> MDString *{
1950 // This requires that the ID is not really a forward reference. In
1951 // particular, the MDString must already have been resolved.
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001952 return cast_or_null<MDString>(getMDOrNull(ID));
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001953 };
1954
1955#define GET_OR_DISTINCT(CLASS, DISTINCT, ARGS) \
1956 (DISTINCT ? CLASS::getDistinct ARGS : CLASS::get ARGS)
1957
Devang Patel7428d8a2009-07-22 17:43:22 +00001958 // Read all the records.
1959 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00001960 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00001961
Chris Lattner27d38752013-01-20 02:13:19 +00001962 switch (Entry.Kind) {
1963 case BitstreamEntry::SubBlock: // Handled for us already.
1964 case BitstreamEntry::Error:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001965 return error("Malformed block");
Chris Lattner27d38752013-01-20 02:13:19 +00001966 case BitstreamEntry::EndBlock:
Teresa Johnson61b406e2015-12-29 23:00:22 +00001967 MetadataList.tryToResolveCycles();
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001968 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00001969 case BitstreamEntry::Record:
1970 // The interesting case.
1971 break;
Devang Patel7428d8a2009-07-22 17:43:22 +00001972 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001973
Devang Patel7428d8a2009-07-22 17:43:22 +00001974 // Read a record.
1975 Record.clear();
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00001976 StringRef Blob;
1977 unsigned Code = Stream.readRecord(Entry.ID, Record, &Blob);
Duncan P. N. Exon Smith090a19b2015-01-08 22:38:29 +00001978 bool IsDistinct = false;
Dan Gohmanbbcd04d2010-09-13 18:00:48 +00001979 switch (Code) {
Devang Patel7428d8a2009-07-22 17:43:22 +00001980 default: // Default behavior: ignore.
1981 break;
Devang Patel27c87ff2009-07-29 22:34:41 +00001982 case bitc::METADATA_NAME: {
Chris Lattner8d140532013-01-20 02:54:05 +00001983 // Read name of the named metadata.
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001984 SmallString<8> Name(Record.begin(), Record.end());
Devang Patel27c87ff2009-07-29 22:34:41 +00001985 Record.clear();
1986 Code = Stream.ReadCode();
1987
Chris Lattner27d38752013-01-20 02:13:19 +00001988 unsigned NextBitCode = Stream.readRecord(Code, Record);
Filipe Cabecinhas14e68672015-05-30 00:17:20 +00001989 if (NextBitCode != bitc::METADATA_NAMED_NODE)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001990 return error("METADATA_NAME not followed by METADATA_NAMED_NODE");
Devang Patel27c87ff2009-07-29 22:34:41 +00001991
1992 // Read named metadata elements.
1993 unsigned Size = Record.size();
Dan Gohman2637cc12010-07-21 23:38:33 +00001994 NamedMDNode *NMD = TheModule->getOrInsertNamedMetadata(Name);
Devang Patel27c87ff2009-07-29 22:34:41 +00001995 for (unsigned i = 0; i != Size; ++i) {
Justin Bognerae341c62016-03-17 20:12:06 +00001996 MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[i]);
Craig Topper2617dcc2014-04-15 06:32:26 +00001997 if (!MD)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00001998 return error("Invalid record");
Dan Gohman2637cc12010-07-21 23:38:33 +00001999 NMD->addOperand(MD);
Devang Patel27c87ff2009-07-29 22:34:41 +00002000 }
Devang Patel27c87ff2009-07-29 22:34:41 +00002001 break;
2002 }
Duncan P. N. Exon Smith005f9f42014-12-11 22:30:48 +00002003 case bitc::METADATA_OLD_FN_NODE: {
Duncan P. N. Exon Smith5bd34e52014-12-12 02:11:31 +00002004 // FIXME: Remove in 4.0.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002005 // This is a LocalAsMetadata record, the only type of function-local
2006 // metadata.
Duncan P. N. Exon Smithda41af92014-12-06 01:26:49 +00002007 if (Record.size() % 2 == 1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002008 return error("Invalid record");
Duncan P. N. Exon Smithda41af92014-12-06 01:26:49 +00002009
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002010 // If this isn't a LocalAsMetadata record, we're dropping it. This used
2011 // to be legal, but there's no upgrade path.
Duncan P. N. Exon Smithda41af92014-12-06 01:26:49 +00002012 auto dropRecord = [&] {
Teresa Johnson61b406e2015-12-29 23:00:22 +00002013 MetadataList.assignValue(MDNode::get(Context, None), NextMetadataNo++);
Duncan P. N. Exon Smithda41af92014-12-06 01:26:49 +00002014 };
2015 if (Record.size() != 2) {
2016 dropRecord();
2017 break;
2018 }
2019
2020 Type *Ty = getTypeByID(Record[0]);
2021 if (Ty->isMetadataTy() || Ty->isVoidTy()) {
2022 dropRecord();
2023 break;
2024 }
2025
Teresa Johnson61b406e2015-12-29 23:00:22 +00002026 MetadataList.assignValue(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002027 LocalAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
Teresa Johnson61b406e2015-12-29 23:00:22 +00002028 NextMetadataNo++);
Duncan P. N. Exon Smithda41af92014-12-06 01:26:49 +00002029 break;
2030 }
Duncan P. N. Exon Smith005f9f42014-12-11 22:30:48 +00002031 case bitc::METADATA_OLD_NODE: {
Duncan P. N. Exon Smith5bd34e52014-12-12 02:11:31 +00002032 // FIXME: Remove in 4.0.
Dan Gohman1e0213a2010-07-13 19:33:27 +00002033 if (Record.size() % 2 == 1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002034 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002035
Devang Patele059ba6e2009-07-23 01:07:34 +00002036 unsigned Size = Record.size();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002037 SmallVector<Metadata *, 8> Elts;
Devang Patele059ba6e2009-07-23 01:07:34 +00002038 for (unsigned i = 0; i != Size; i += 2) {
Chris Lattner229907c2011-07-18 04:54:35 +00002039 Type *Ty = getTypeByID(Record[i]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00002040 if (!Ty)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002041 return error("Invalid record");
Chris Lattnerfdd87902009-10-05 05:54:46 +00002042 if (Ty->isMetadataTy())
Justin Bognerae341c62016-03-17 20:12:06 +00002043 Elts.push_back(MetadataList.getMetadataFwdRef(Record[i + 1]));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002044 else if (!Ty->isVoidTy()) {
2045 auto *MD =
2046 ValueAsMetadata::get(ValueList.getValueFwdRef(Record[i + 1], Ty));
2047 assert(isa<ConstantAsMetadata>(MD) &&
2048 "Expected non-function-local metadata");
2049 Elts.push_back(MD);
2050 } else
Craig Topper2617dcc2014-04-15 06:32:26 +00002051 Elts.push_back(nullptr);
Devang Patele059ba6e2009-07-23 01:07:34 +00002052 }
Teresa Johnson61b406e2015-12-29 23:00:22 +00002053 MetadataList.assignValue(MDNode::get(Context, Elts), NextMetadataNo++);
Devang Patele059ba6e2009-07-23 01:07:34 +00002054 break;
2055 }
Duncan P. N. Exon Smith5c7006e2014-12-11 23:02:24 +00002056 case bitc::METADATA_VALUE: {
2057 if (Record.size() != 2)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002058 return error("Invalid record");
Duncan P. N. Exon Smith5c7006e2014-12-11 23:02:24 +00002059
2060 Type *Ty = getTypeByID(Record[0]);
2061 if (Ty->isMetadataTy() || Ty->isVoidTy())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002062 return error("Invalid record");
Duncan P. N. Exon Smith5c7006e2014-12-11 23:02:24 +00002063
Teresa Johnson61b406e2015-12-29 23:00:22 +00002064 MetadataList.assignValue(
Duncan P. N. Exon Smith5c7006e2014-12-11 23:02:24 +00002065 ValueAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
Teresa Johnson61b406e2015-12-29 23:00:22 +00002066 NextMetadataNo++);
Duncan P. N. Exon Smith5c7006e2014-12-11 23:02:24 +00002067 break;
2068 }
Duncan P. N. Exon Smith090a19b2015-01-08 22:38:29 +00002069 case bitc::METADATA_DISTINCT_NODE:
2070 IsDistinct = true;
2071 // fallthrough...
Duncan P. N. Exon Smith5c7006e2014-12-11 23:02:24 +00002072 case bitc::METADATA_NODE: {
2073 SmallVector<Metadata *, 8> Elts;
2074 Elts.reserve(Record.size());
2075 for (unsigned ID : Record)
Justin Bognerae341c62016-03-17 20:12:06 +00002076 Elts.push_back(ID ? MetadataList.getMetadataFwdRef(ID - 1) : nullptr);
Teresa Johnson61b406e2015-12-29 23:00:22 +00002077 MetadataList.assignValue(IsDistinct ? MDNode::getDistinct(Context, Elts)
2078 : MDNode::get(Context, Elts),
2079 NextMetadataNo++);
Duncan P. N. Exon Smith5c7006e2014-12-11 23:02:24 +00002080 break;
2081 }
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00002082 case bitc::METADATA_LOCATION: {
2083 if (Record.size() != 5)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002084 return error("Invalid record");
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00002085
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00002086 unsigned Line = Record[1];
2087 unsigned Column = Record[2];
Justin Bognerae341c62016-03-17 20:12:06 +00002088 MDNode *Scope = MetadataList.getMDNodeFwdRefOrNull(Record[3]);
2089 if (!Scope)
2090 return error("Invalid record");
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00002091 Metadata *InlinedAt =
Justin Bognerae341c62016-03-17 20:12:06 +00002092 Record[4] ? MetadataList.getMetadataFwdRef(Record[4] - 1) : nullptr;
Teresa Johnson61b406e2015-12-29 23:00:22 +00002093 MetadataList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002094 GET_OR_DISTINCT(DILocation, Record[0],
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +00002095 (Context, Line, Column, Scope, InlinedAt)),
Teresa Johnson61b406e2015-12-29 23:00:22 +00002096 NextMetadataNo++);
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00002097 break;
2098 }
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00002099 case bitc::METADATA_GENERIC_DEBUG: {
2100 if (Record.size() < 4)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002101 return error("Invalid record");
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00002102
2103 unsigned Tag = Record[1];
2104 unsigned Version = Record[2];
2105
2106 if (Tag >= 1u << 16 || Version != 0)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002107 return error("Invalid record");
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00002108
2109 auto *Header = getMDString(Record[3]);
2110 SmallVector<Metadata *, 8> DwarfOps;
2111 for (unsigned I = 4, E = Record.size(); I != E; ++I)
Justin Bognerae341c62016-03-17 20:12:06 +00002112 DwarfOps.push_back(Record[I]
2113 ? MetadataList.getMetadataFwdRef(Record[I] - 1)
2114 : nullptr);
Teresa Johnson61b406e2015-12-29 23:00:22 +00002115 MetadataList.assignValue(
2116 GET_OR_DISTINCT(GenericDINode, Record[0],
2117 (Context, Tag, Header, DwarfOps)),
2118 NextMetadataNo++);
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00002119 break;
2120 }
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00002121 case bitc::METADATA_SUBRANGE: {
2122 if (Record.size() != 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002123 return error("Invalid record");
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00002124
Teresa Johnson61b406e2015-12-29 23:00:22 +00002125 MetadataList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002126 GET_OR_DISTINCT(DISubrange, Record[0],
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00002127 (Context, Record[1], unrotateSign(Record[2]))),
Teresa Johnson61b406e2015-12-29 23:00:22 +00002128 NextMetadataNo++);
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00002129 break;
2130 }
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00002131 case bitc::METADATA_ENUMERATOR: {
2132 if (Record.size() != 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002133 return error("Invalid record");
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00002134
Teresa Johnson61b406e2015-12-29 23:00:22 +00002135 MetadataList.assignValue(
2136 GET_OR_DISTINCT(
2137 DIEnumerator, Record[0],
2138 (Context, unrotateSign(Record[1]), getMDString(Record[2]))),
2139 NextMetadataNo++);
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00002140 break;
2141 }
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00002142 case bitc::METADATA_BASIC_TYPE: {
2143 if (Record.size() != 6)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002144 return error("Invalid record");
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00002145
Teresa Johnson61b406e2015-12-29 23:00:22 +00002146 MetadataList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002147 GET_OR_DISTINCT(DIBasicType, Record[0],
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00002148 (Context, Record[1], getMDString(Record[2]),
2149 Record[3], Record[4], Record[5])),
Teresa Johnson61b406e2015-12-29 23:00:22 +00002150 NextMetadataNo++);
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00002151 break;
2152 }
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00002153 case bitc::METADATA_DERIVED_TYPE: {
2154 if (Record.size() != 12)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002155 return error("Invalid record");
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00002156
Teresa Johnson61b406e2015-12-29 23:00:22 +00002157 MetadataList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002158 GET_OR_DISTINCT(DIDerivedType, Record[0],
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00002159 (Context, Record[1], getMDString(Record[2]),
2160 getMDOrNull(Record[3]), Record[4],
Duncan P. N. Exon Smithad6eb1272015-02-20 03:17:58 +00002161 getMDOrNull(Record[5]), getMDOrNull(Record[6]),
2162 Record[7], Record[8], Record[9], Record[10],
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00002163 getMDOrNull(Record[11]))),
Teresa Johnson61b406e2015-12-29 23:00:22 +00002164 NextMetadataNo++);
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00002165 break;
2166 }
2167 case bitc::METADATA_COMPOSITE_TYPE: {
2168 if (Record.size() != 16)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002169 return error("Invalid record");
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00002170
Teresa Johnson61b406e2015-12-29 23:00:22 +00002171 MetadataList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002172 GET_OR_DISTINCT(DICompositeType, Record[0],
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00002173 (Context, Record[1], getMDString(Record[2]),
2174 getMDOrNull(Record[3]), Record[4],
2175 getMDOrNull(Record[5]), getMDOrNull(Record[6]),
2176 Record[7], Record[8], Record[9], Record[10],
2177 getMDOrNull(Record[11]), Record[12],
2178 getMDOrNull(Record[13]), getMDOrNull(Record[14]),
2179 getMDString(Record[15]))),
Teresa Johnson61b406e2015-12-29 23:00:22 +00002180 NextMetadataNo++);
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00002181 break;
2182 }
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00002183 case bitc::METADATA_SUBROUTINE_TYPE: {
2184 if (Record.size() != 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002185 return error("Invalid record");
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00002186
Teresa Johnson61b406e2015-12-29 23:00:22 +00002187 MetadataList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002188 GET_OR_DISTINCT(DISubroutineType, Record[0],
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00002189 (Context, Record[1], getMDOrNull(Record[2]))),
Teresa Johnson61b406e2015-12-29 23:00:22 +00002190 NextMetadataNo++);
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00002191 break;
2192 }
Adrian Prantlab1243f2015-06-29 23:03:47 +00002193
2194 case bitc::METADATA_MODULE: {
2195 if (Record.size() != 6)
2196 return error("Invalid record");
2197
Teresa Johnson61b406e2015-12-29 23:00:22 +00002198 MetadataList.assignValue(
Adrian Prantlab1243f2015-06-29 23:03:47 +00002199 GET_OR_DISTINCT(DIModule, Record[0],
2200 (Context, getMDOrNull(Record[1]),
Teresa Johnson61b406e2015-12-29 23:00:22 +00002201 getMDString(Record[2]), getMDString(Record[3]),
2202 getMDString(Record[4]), getMDString(Record[5]))),
2203 NextMetadataNo++);
Adrian Prantlab1243f2015-06-29 23:03:47 +00002204 break;
2205 }
2206
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00002207 case bitc::METADATA_FILE: {
2208 if (Record.size() != 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002209 return error("Invalid record");
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00002210
Teresa Johnson61b406e2015-12-29 23:00:22 +00002211 MetadataList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002212 GET_OR_DISTINCT(DIFile, Record[0], (Context, getMDString(Record[1]),
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00002213 getMDString(Record[2]))),
Teresa Johnson61b406e2015-12-29 23:00:22 +00002214 NextMetadataNo++);
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00002215 break;
2216 }
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00002217 case bitc::METADATA_COMPILE_UNIT: {
Amjad Abouda9bcf162015-12-10 12:56:35 +00002218 if (Record.size() < 14 || Record.size() > 16)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002219 return error("Invalid record");
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00002220
Amjad Abouda9bcf162015-12-10 12:56:35 +00002221 // Ignore Record[0], which indicates whether this compile unit is
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00002222 // distinct. It's always distinct.
Teresa Johnson61b406e2015-12-29 23:00:22 +00002223 MetadataList.assignValue(
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00002224 DICompileUnit::getDistinct(
2225 Context, Record[1], getMDOrNull(Record[2]),
2226 getMDString(Record[3]), Record[4], getMDString(Record[5]),
2227 Record[6], getMDString(Record[7]), Record[8],
2228 getMDOrNull(Record[9]), getMDOrNull(Record[10]),
2229 getMDOrNull(Record[11]), getMDOrNull(Record[12]),
Amjad Abouda9bcf162015-12-10 12:56:35 +00002230 getMDOrNull(Record[13]),
Eugene Zelenko6ac3f732016-01-26 18:48:36 +00002231 Record.size() <= 15 ? nullptr : getMDOrNull(Record[15]),
Amjad Abouda9bcf162015-12-10 12:56:35 +00002232 Record.size() <= 14 ? 0 : Record[14]),
Teresa Johnson61b406e2015-12-29 23:00:22 +00002233 NextMetadataNo++);
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00002234 break;
2235 }
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00002236 case bitc::METADATA_SUBPROGRAM: {
Peter Collingbourned4bff302015-11-05 22:03:56 +00002237 if (Record.size() != 18 && Record.size() != 19)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002238 return error("Invalid record");
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00002239
Peter Collingbourned4bff302015-11-05 22:03:56 +00002240 bool HasFn = Record.size() == 19;
2241 DISubprogram *SP = GET_OR_DISTINCT(
2242 DISubprogram,
2243 Record[0] || Record[8], // All definitions should be distinct.
2244 (Context, getMDOrNull(Record[1]), getMDString(Record[2]),
2245 getMDString(Record[3]), getMDOrNull(Record[4]), Record[5],
2246 getMDOrNull(Record[6]), Record[7], Record[8], Record[9],
2247 getMDOrNull(Record[10]), Record[11], Record[12], Record[13],
2248 Record[14], getMDOrNull(Record[15 + HasFn]),
2249 getMDOrNull(Record[16 + HasFn]), getMDOrNull(Record[17 + HasFn])));
Teresa Johnson61b406e2015-12-29 23:00:22 +00002250 MetadataList.assignValue(SP, NextMetadataNo++);
Peter Collingbourned4bff302015-11-05 22:03:56 +00002251
2252 // Upgrade sp->function mapping to function->sp mapping.
2253 if (HasFn && Record[15]) {
2254 if (auto *CMD = dyn_cast<ConstantAsMetadata>(getMDOrNull(Record[15])))
2255 if (auto *F = dyn_cast<Function>(CMD->getValue())) {
2256 if (F->isMaterializable())
2257 // Defer until materialized; unmaterialized functions may not have
2258 // metadata.
2259 FunctionsWithSPs[F] = SP;
2260 else if (!F->empty())
2261 F->setSubprogram(SP);
2262 }
2263 }
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00002264 break;
2265 }
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00002266 case bitc::METADATA_LEXICAL_BLOCK: {
2267 if (Record.size() != 5)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002268 return error("Invalid record");
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00002269
Teresa Johnson61b406e2015-12-29 23:00:22 +00002270 MetadataList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002271 GET_OR_DISTINCT(DILexicalBlock, Record[0],
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00002272 (Context, getMDOrNull(Record[1]),
2273 getMDOrNull(Record[2]), Record[3], Record[4])),
Teresa Johnson61b406e2015-12-29 23:00:22 +00002274 NextMetadataNo++);
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00002275 break;
2276 }
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00002277 case bitc::METADATA_LEXICAL_BLOCK_FILE: {
2278 if (Record.size() != 4)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002279 return error("Invalid record");
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00002280
Teresa Johnson61b406e2015-12-29 23:00:22 +00002281 MetadataList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002282 GET_OR_DISTINCT(DILexicalBlockFile, Record[0],
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00002283 (Context, getMDOrNull(Record[1]),
2284 getMDOrNull(Record[2]), Record[3])),
Teresa Johnson61b406e2015-12-29 23:00:22 +00002285 NextMetadataNo++);
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00002286 break;
2287 }
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00002288 case bitc::METADATA_NAMESPACE: {
2289 if (Record.size() != 5)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002290 return error("Invalid record");
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00002291
Teresa Johnson61b406e2015-12-29 23:00:22 +00002292 MetadataList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002293 GET_OR_DISTINCT(DINamespace, Record[0],
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00002294 (Context, getMDOrNull(Record[1]),
2295 getMDOrNull(Record[2]), getMDString(Record[3]),
2296 Record[4])),
Teresa Johnson61b406e2015-12-29 23:00:22 +00002297 NextMetadataNo++);
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00002298 break;
2299 }
Amjad Abouda9bcf162015-12-10 12:56:35 +00002300 case bitc::METADATA_MACRO: {
2301 if (Record.size() != 5)
2302 return error("Invalid record");
2303
Teresa Johnson61b406e2015-12-29 23:00:22 +00002304 MetadataList.assignValue(
Amjad Abouda9bcf162015-12-10 12:56:35 +00002305 GET_OR_DISTINCT(DIMacro, Record[0],
2306 (Context, Record[1], Record[2],
2307 getMDString(Record[3]), getMDString(Record[4]))),
Teresa Johnson61b406e2015-12-29 23:00:22 +00002308 NextMetadataNo++);
Amjad Abouda9bcf162015-12-10 12:56:35 +00002309 break;
2310 }
2311 case bitc::METADATA_MACRO_FILE: {
2312 if (Record.size() != 5)
2313 return error("Invalid record");
2314
Teresa Johnson61b406e2015-12-29 23:00:22 +00002315 MetadataList.assignValue(
Amjad Abouda9bcf162015-12-10 12:56:35 +00002316 GET_OR_DISTINCT(DIMacroFile, Record[0],
2317 (Context, Record[1], Record[2],
2318 getMDOrNull(Record[3]), getMDOrNull(Record[4]))),
Teresa Johnson61b406e2015-12-29 23:00:22 +00002319 NextMetadataNo++);
Amjad Abouda9bcf162015-12-10 12:56:35 +00002320 break;
2321 }
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002322 case bitc::METADATA_TEMPLATE_TYPE: {
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00002323 if (Record.size() != 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002324 return error("Invalid record");
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002325
Teresa Johnson61b406e2015-12-29 23:00:22 +00002326 MetadataList.assignValue(GET_OR_DISTINCT(DITemplateTypeParameter,
2327 Record[0],
2328 (Context, getMDString(Record[1]),
2329 getMDOrNull(Record[2]))),
2330 NextMetadataNo++);
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002331 break;
2332 }
2333 case bitc::METADATA_TEMPLATE_VALUE: {
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00002334 if (Record.size() != 5)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002335 return error("Invalid record");
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002336
Teresa Johnson61b406e2015-12-29 23:00:22 +00002337 MetadataList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002338 GET_OR_DISTINCT(DITemplateValueParameter, Record[0],
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00002339 (Context, Record[1], getMDString(Record[2]),
2340 getMDOrNull(Record[3]), getMDOrNull(Record[4]))),
Teresa Johnson61b406e2015-12-29 23:00:22 +00002341 NextMetadataNo++);
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002342 break;
2343 }
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00002344 case bitc::METADATA_GLOBAL_VAR: {
2345 if (Record.size() != 11)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002346 return error("Invalid record");
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00002347
Teresa Johnson61b406e2015-12-29 23:00:22 +00002348 MetadataList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002349 GET_OR_DISTINCT(DIGlobalVariable, Record[0],
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00002350 (Context, getMDOrNull(Record[1]),
2351 getMDString(Record[2]), getMDString(Record[3]),
2352 getMDOrNull(Record[4]), Record[5],
2353 getMDOrNull(Record[6]), Record[7], Record[8],
2354 getMDOrNull(Record[9]), getMDOrNull(Record[10]))),
Teresa Johnson61b406e2015-12-29 23:00:22 +00002355 NextMetadataNo++);
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00002356 break;
2357 }
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00002358 case bitc::METADATA_LOCAL_VAR: {
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00002359 // 10th field is for the obseleted 'inlinedAt:' field.
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002360 if (Record.size() < 8 || Record.size() > 10)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002361 return error("Invalid record");
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00002362
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002363 // 2nd field used to be an artificial tag, either DW_TAG_auto_variable or
2364 // DW_TAG_arg_variable.
2365 bool HasTag = Record.size() > 8;
Teresa Johnson61b406e2015-12-29 23:00:22 +00002366 MetadataList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002367 GET_OR_DISTINCT(DILocalVariable, Record[0],
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002368 (Context, getMDOrNull(Record[1 + HasTag]),
2369 getMDString(Record[2 + HasTag]),
2370 getMDOrNull(Record[3 + HasTag]), Record[4 + HasTag],
2371 getMDOrNull(Record[5 + HasTag]), Record[6 + HasTag],
2372 Record[7 + HasTag])),
Teresa Johnson61b406e2015-12-29 23:00:22 +00002373 NextMetadataNo++);
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00002374 break;
2375 }
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00002376 case bitc::METADATA_EXPRESSION: {
2377 if (Record.size() < 1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002378 return error("Invalid record");
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00002379
Teresa Johnson61b406e2015-12-29 23:00:22 +00002380 MetadataList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002381 GET_OR_DISTINCT(DIExpression, Record[0],
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00002382 (Context, makeArrayRef(Record).slice(1))),
Teresa Johnson61b406e2015-12-29 23:00:22 +00002383 NextMetadataNo++);
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00002384 break;
2385 }
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00002386 case bitc::METADATA_OBJC_PROPERTY: {
2387 if (Record.size() != 8)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002388 return error("Invalid record");
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00002389
Teresa Johnson61b406e2015-12-29 23:00:22 +00002390 MetadataList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002391 GET_OR_DISTINCT(DIObjCProperty, Record[0],
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00002392 (Context, getMDString(Record[1]),
2393 getMDOrNull(Record[2]), Record[3],
2394 getMDString(Record[4]), getMDString(Record[5]),
2395 Record[6], getMDOrNull(Record[7]))),
Teresa Johnson61b406e2015-12-29 23:00:22 +00002396 NextMetadataNo++);
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00002397 break;
2398 }
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00002399 case bitc::METADATA_IMPORTED_ENTITY: {
2400 if (Record.size() != 6)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002401 return error("Invalid record");
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00002402
Teresa Johnson61b406e2015-12-29 23:00:22 +00002403 MetadataList.assignValue(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002404 GET_OR_DISTINCT(DIImportedEntity, Record[0],
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00002405 (Context, Record[1], getMDOrNull(Record[2]),
2406 getMDOrNull(Record[3]), Record[4],
2407 getMDString(Record[5]))),
Teresa Johnson61b406e2015-12-29 23:00:22 +00002408 NextMetadataNo++);
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00002409 break;
2410 }
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00002411 case bitc::METADATA_STRING_OLD: {
Eli Bendersky5d5e18d2014-06-25 15:41:00 +00002412 std::string String(Record.begin(), Record.end());
Duncan P. N. Exon Smithefe16c82016-03-25 00:56:13 +00002413
2414 // Test for upgrading !llvm.loop.
2415 HasSeenOldLoopTags |= mayBeOldLoopAttachmentTag(String);
2416
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002417 Metadata *MD = MDString::get(Context, String);
Teresa Johnson61b406e2015-12-29 23:00:22 +00002418 MetadataList.assignValue(MD, NextMetadataNo++);
Devang Patel7428d8a2009-07-22 17:43:22 +00002419 break;
2420 }
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +00002421 case bitc::METADATA_STRINGS:
2422 if (std::error_code EC =
2423 parseMetadataStrings(Record, Blob, NextMetadataNo))
2424 return EC;
2425 break;
Devang Patelaf206b82009-09-18 19:26:43 +00002426 case bitc::METADATA_KIND: {
Teresa Johnson12545072015-11-15 02:00:09 +00002427 // Support older bitcode files that had METADATA_KIND records in a
2428 // block with METADATA_BLOCK_ID.
2429 if (std::error_code EC = parseMetadataKindRecord(Record))
2430 return EC;
Devang Patelaf206b82009-09-18 19:26:43 +00002431 break;
2432 }
Devang Patel7428d8a2009-07-22 17:43:22 +00002433 }
2434 }
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00002435#undef GET_OR_DISTINCT
Devang Patel7428d8a2009-07-22 17:43:22 +00002436}
2437
Teresa Johnson12545072015-11-15 02:00:09 +00002438/// Parse the metadata kinds out of the METADATA_KIND_BLOCK.
2439std::error_code BitcodeReader::parseMetadataKinds() {
2440 if (Stream.EnterSubBlock(bitc::METADATA_KIND_BLOCK_ID))
2441 return error("Invalid record");
2442
2443 SmallVector<uint64_t, 64> Record;
2444
2445 // Read all the records.
2446 while (1) {
2447 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
2448
2449 switch (Entry.Kind) {
2450 case BitstreamEntry::SubBlock: // Handled for us already.
2451 case BitstreamEntry::Error:
2452 return error("Malformed block");
2453 case BitstreamEntry::EndBlock:
2454 return std::error_code();
2455 case BitstreamEntry::Record:
2456 // The interesting case.
2457 break;
2458 }
2459
2460 // Read a record.
2461 Record.clear();
2462 unsigned Code = Stream.readRecord(Entry.ID, Record);
2463 switch (Code) {
2464 default: // Default behavior: ignore.
2465 break;
2466 case bitc::METADATA_KIND: {
2467 if (std::error_code EC = parseMetadataKindRecord(Record))
2468 return EC;
2469 break;
2470 }
2471 }
2472 }
2473}
2474
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002475/// Decode a signed value stored with the sign bit in the LSB for dense VBR
2476/// encoding.
Jan Wen Voungafaced02012-10-11 20:20:40 +00002477uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) {
Chris Lattner08feb1e2007-04-24 04:04:35 +00002478 if ((V & 1) == 0)
2479 return V >> 1;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002480 if (V != 1)
Chris Lattner08feb1e2007-04-24 04:04:35 +00002481 return -(V >> 1);
2482 // There is no such thing as -0 with integers. "-0" really means MININT.
2483 return 1ULL << 63;
2484}
2485
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002486/// Resolve all of the initializers for global values and aliases that we can.
2487std::error_code BitcodeReader::resolveGlobalAndAliasInits() {
Chris Lattner44c17072007-04-26 02:46:40 +00002488 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist;
2489 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist;
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00002490 std::vector<std::pair<Function*, unsigned> > FunctionPrefixWorklist;
Peter Collingbourne51d2de72014-12-03 02:08:38 +00002491 std::vector<std::pair<Function*, unsigned> > FunctionPrologueWorklist;
David Majnemer7fddecc2015-06-17 20:52:32 +00002492 std::vector<std::pair<Function*, unsigned> > FunctionPersonalityFnWorklist;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002493
Chris Lattner44c17072007-04-26 02:46:40 +00002494 GlobalInitWorklist.swap(GlobalInits);
2495 AliasInitWorklist.swap(AliasInits);
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00002496 FunctionPrefixWorklist.swap(FunctionPrefixes);
Peter Collingbourne51d2de72014-12-03 02:08:38 +00002497 FunctionPrologueWorklist.swap(FunctionPrologues);
David Majnemer7fddecc2015-06-17 20:52:32 +00002498 FunctionPersonalityFnWorklist.swap(FunctionPersonalityFns);
Chris Lattner44c17072007-04-26 02:46:40 +00002499
2500 while (!GlobalInitWorklist.empty()) {
Chris Lattner831d4202007-04-26 03:27:58 +00002501 unsigned ValID = GlobalInitWorklist.back().second;
Chris Lattner44c17072007-04-26 02:46:40 +00002502 if (ValID >= ValueList.size()) {
2503 // Not ready to resolve this yet, it requires something later in the file.
Chris Lattner831d4202007-04-26 03:27:58 +00002504 GlobalInits.push_back(GlobalInitWorklist.back());
Chris Lattner44c17072007-04-26 02:46:40 +00002505 } else {
Karthik Bhat82540e92014-03-27 12:08:23 +00002506 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
Chris Lattner44c17072007-04-26 02:46:40 +00002507 GlobalInitWorklist.back().first->setInitializer(C);
2508 else
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002509 return error("Expected a constant");
Chris Lattner44c17072007-04-26 02:46:40 +00002510 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002511 GlobalInitWorklist.pop_back();
Chris Lattner44c17072007-04-26 02:46:40 +00002512 }
2513
2514 while (!AliasInitWorklist.empty()) {
2515 unsigned ValID = AliasInitWorklist.back().second;
2516 if (ValID >= ValueList.size()) {
2517 AliasInits.push_back(AliasInitWorklist.back());
2518 } else {
Filipe Cabecinhasa911af02015-06-06 20:44:53 +00002519 Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]);
2520 if (!C)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002521 return error("Expected a constant");
Filipe Cabecinhasa911af02015-06-06 20:44:53 +00002522 GlobalAlias *Alias = AliasInitWorklist.back().first;
2523 if (C->getType() != Alias->getType())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002524 return error("Alias and aliasee types don't match");
Filipe Cabecinhasa911af02015-06-06 20:44:53 +00002525 Alias->setAliasee(C);
Chris Lattner44c17072007-04-26 02:46:40 +00002526 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002527 AliasInitWorklist.pop_back();
Chris Lattner44c17072007-04-26 02:46:40 +00002528 }
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00002529
2530 while (!FunctionPrefixWorklist.empty()) {
2531 unsigned ValID = FunctionPrefixWorklist.back().second;
2532 if (ValID >= ValueList.size()) {
2533 FunctionPrefixes.push_back(FunctionPrefixWorklist.back());
2534 } else {
Karthik Bhat82540e92014-03-27 12:08:23 +00002535 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00002536 FunctionPrefixWorklist.back().first->setPrefixData(C);
2537 else
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002538 return error("Expected a constant");
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00002539 }
2540 FunctionPrefixWorklist.pop_back();
2541 }
2542
Peter Collingbourne51d2de72014-12-03 02:08:38 +00002543 while (!FunctionPrologueWorklist.empty()) {
2544 unsigned ValID = FunctionPrologueWorklist.back().second;
2545 if (ValID >= ValueList.size()) {
2546 FunctionPrologues.push_back(FunctionPrologueWorklist.back());
2547 } else {
2548 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2549 FunctionPrologueWorklist.back().first->setPrologueData(C);
2550 else
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002551 return error("Expected a constant");
Peter Collingbourne51d2de72014-12-03 02:08:38 +00002552 }
2553 FunctionPrologueWorklist.pop_back();
2554 }
2555
David Majnemer7fddecc2015-06-17 20:52:32 +00002556 while (!FunctionPersonalityFnWorklist.empty()) {
2557 unsigned ValID = FunctionPersonalityFnWorklist.back().second;
2558 if (ValID >= ValueList.size()) {
2559 FunctionPersonalityFns.push_back(FunctionPersonalityFnWorklist.back());
2560 } else {
2561 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2562 FunctionPersonalityFnWorklist.back().first->setPersonalityFn(C);
2563 else
2564 return error("Expected a constant");
2565 }
2566 FunctionPersonalityFnWorklist.pop_back();
2567 }
2568
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002569 return std::error_code();
Chris Lattner44c17072007-04-26 02:46:40 +00002570}
2571
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002572static APInt readWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) {
Benjamin Kramer9704ed02012-05-28 14:10:31 +00002573 SmallVector<uint64_t, 8> Words(Vals.size());
2574 std::transform(Vals.begin(), Vals.end(), Words.begin(),
Jan Wen Voungafaced02012-10-11 20:20:40 +00002575 BitcodeReader::decodeSignRotatedValue);
Benjamin Kramer9704ed02012-05-28 14:10:31 +00002576
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002577 return APInt(TypeBits, Words);
2578}
2579
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002580std::error_code BitcodeReader::parseConstants() {
Chris Lattner982ec1e2007-05-05 00:17:00 +00002581 if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002582 return error("Invalid record");
Chris Lattnerfbc1d332007-04-24 03:30:34 +00002583
2584 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002585
Chris Lattnerfbc1d332007-04-24 03:30:34 +00002586 // Read all the records for this value table.
Chris Lattner229907c2011-07-18 04:54:35 +00002587 Type *CurTy = Type::getInt32Ty(Context);
Chris Lattner1663cca2007-04-24 05:48:56 +00002588 unsigned NextCstNo = ValueList.size();
Chris Lattnerfbc1d332007-04-24 03:30:34 +00002589 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00002590 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00002591
Chris Lattner27d38752013-01-20 02:13:19 +00002592 switch (Entry.Kind) {
2593 case BitstreamEntry::SubBlock: // Handled for us already.
2594 case BitstreamEntry::Error:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002595 return error("Malformed block");
Chris Lattner27d38752013-01-20 02:13:19 +00002596 case BitstreamEntry::EndBlock:
2597 if (NextCstNo != ValueList.size())
George Burgess IV1030d682016-01-20 22:15:23 +00002598 return error("Invalid constant reference");
Joe Abbey97b7a172013-02-06 22:14:06 +00002599
Chris Lattner27d38752013-01-20 02:13:19 +00002600 // Once all the constants have been read, go through and resolve forward
2601 // references.
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002602 ValueList.resolveConstantForwardRefs();
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002603 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00002604 case BitstreamEntry::Record:
2605 // The interesting case.
Chris Lattner74429932008-08-21 02:34:16 +00002606 break;
Chris Lattnerfbc1d332007-04-24 03:30:34 +00002607 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002608
Chris Lattnerfbc1d332007-04-24 03:30:34 +00002609 // Read a record.
2610 Record.clear();
Craig Topper2617dcc2014-04-15 06:32:26 +00002611 Value *V = nullptr;
Chris Lattner27d38752013-01-20 02:13:19 +00002612 unsigned BitCode = Stream.readRecord(Entry.ID, Record);
Dan Gohman0ebd6962009-07-20 21:19:07 +00002613 switch (BitCode) {
Chris Lattnerfbc1d332007-04-24 03:30:34 +00002614 default: // Default behavior: unknown constant
2615 case bitc::CST_CODE_UNDEF: // UNDEF
Owen Andersonb292b8c2009-07-30 23:03:37 +00002616 V = UndefValue::get(CurTy);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00002617 break;
2618 case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid]
2619 if (Record.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002620 return error("Invalid record");
Karthik Bhat82540e92014-03-27 12:08:23 +00002621 if (Record[0] >= TypeList.size() || !TypeList[Record[0]])
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002622 return error("Invalid record");
Chris Lattnerfbc1d332007-04-24 03:30:34 +00002623 CurTy = TypeList[Record[0]];
Chris Lattner08feb1e2007-04-24 04:04:35 +00002624 continue; // Skip the ValueList manipulation.
Chris Lattnerfbc1d332007-04-24 03:30:34 +00002625 case bitc::CST_CODE_NULL: // NULL
Owen Anderson5a1acd92009-07-31 20:28:14 +00002626 V = Constant::getNullValue(CurTy);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00002627 break;
2628 case bitc::CST_CODE_INTEGER: // INTEGER: [intval]
Duncan Sands19d0b472010-02-16 11:11:14 +00002629 if (!CurTy->isIntegerTy() || Record.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002630 return error("Invalid record");
Jan Wen Voungafaced02012-10-11 20:20:40 +00002631 V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0]));
Chris Lattner08feb1e2007-04-24 04:04:35 +00002632 break;
Chris Lattnere14cb882007-05-04 19:11:41 +00002633 case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
Duncan Sands19d0b472010-02-16 11:11:14 +00002634 if (!CurTy->isIntegerTy() || Record.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002635 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002636
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002637 APInt VInt =
2638 readWideAPInt(Record, cast<IntegerType>(CurTy)->getBitWidth());
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002639 V = ConstantInt::get(Context, VInt);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002640
Chris Lattner08feb1e2007-04-24 04:04:35 +00002641 break;
2642 }
Dale Johannesen245dceb2007-09-11 18:32:33 +00002643 case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval]
Chris Lattner08feb1e2007-04-24 04:04:35 +00002644 if (Record.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002645 return error("Invalid record");
Dan Gohman518cda42011-12-17 00:04:22 +00002646 if (CurTy->isHalfTy())
Tim Northover29178a32013-01-22 09:46:31 +00002647 V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf,
2648 APInt(16, (uint16_t)Record[0])));
Dan Gohman518cda42011-12-17 00:04:22 +00002649 else if (CurTy->isFloatTy())
Tim Northover29178a32013-01-22 09:46:31 +00002650 V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle,
2651 APInt(32, (uint32_t)Record[0])));
Chris Lattnerfdd87902009-10-05 05:54:46 +00002652 else if (CurTy->isDoubleTy())
Tim Northover29178a32013-01-22 09:46:31 +00002653 V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble,
2654 APInt(64, Record[0])));
Chris Lattnerfdd87902009-10-05 05:54:46 +00002655 else if (CurTy->isX86_FP80Ty()) {
Dale Johannesen93eefa02009-03-23 21:16:53 +00002656 // Bits are not stored the same way as a normal i80 APInt, compensate.
2657 uint64_t Rearrange[2];
2658 Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);
2659 Rearrange[1] = Record[0] >> 48;
Tim Northover29178a32013-01-22 09:46:31 +00002660 V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended,
2661 APInt(80, Rearrange)));
Chris Lattnerfdd87902009-10-05 05:54:46 +00002662 } else if (CurTy->isFP128Ty())
Tim Northover29178a32013-01-22 09:46:31 +00002663 V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad,
2664 APInt(128, Record)));
Chris Lattnerfdd87902009-10-05 05:54:46 +00002665 else if (CurTy->isPPC_FP128Ty())
Tim Northover29178a32013-01-22 09:46:31 +00002666 V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble,
2667 APInt(128, Record)));
Chris Lattnerfbc1d332007-04-24 03:30:34 +00002668 else
Owen Andersonb292b8c2009-07-30 23:03:37 +00002669 V = UndefValue::get(CurTy);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00002670 break;
Dale Johannesen245dceb2007-09-11 18:32:33 +00002671 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002672
Chris Lattnere14cb882007-05-04 19:11:41 +00002673 case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]
2674 if (Record.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002675 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002676
Chris Lattnere14cb882007-05-04 19:11:41 +00002677 unsigned Size = Record.size();
Chris Lattnercc3aaf12012-01-27 03:15:49 +00002678 SmallVector<Constant*, 16> Elts;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002679
Chris Lattner229907c2011-07-18 04:54:35 +00002680 if (StructType *STy = dyn_cast<StructType>(CurTy)) {
Chris Lattner1663cca2007-04-24 05:48:56 +00002681 for (unsigned i = 0; i != Size; ++i)
Chris Lattnere14cb882007-05-04 19:11:41 +00002682 Elts.push_back(ValueList.getConstantFwdRef(Record[i],
Chris Lattner1663cca2007-04-24 05:48:56 +00002683 STy->getElementType(i)));
Owen Anderson45308b52009-07-27 22:29:26 +00002684 V = ConstantStruct::get(STy, Elts);
Chris Lattner229907c2011-07-18 04:54:35 +00002685 } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) {
2686 Type *EltTy = ATy->getElementType();
Chris Lattner1663cca2007-04-24 05:48:56 +00002687 for (unsigned i = 0; i != Size; ++i)
Chris Lattnere14cb882007-05-04 19:11:41 +00002688 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
Owen Andersonc2c79322009-07-28 18:32:17 +00002689 V = ConstantArray::get(ATy, Elts);
Chris Lattner229907c2011-07-18 04:54:35 +00002690 } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) {
2691 Type *EltTy = VTy->getElementType();
Chris Lattner1663cca2007-04-24 05:48:56 +00002692 for (unsigned i = 0; i != Size; ++i)
Chris Lattnere14cb882007-05-04 19:11:41 +00002693 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
Owen Anderson4aa32952009-07-28 21:19:26 +00002694 V = ConstantVector::get(Elts);
Chris Lattner1663cca2007-04-24 05:48:56 +00002695 } else {
Owen Andersonb292b8c2009-07-30 23:03:37 +00002696 V = UndefValue::get(CurTy);
Chris Lattner1663cca2007-04-24 05:48:56 +00002697 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002698 break;
2699 }
Chris Lattnerbb8278a2012-02-05 02:41:35 +00002700 case bitc::CST_CODE_STRING: // STRING: [values]
Chris Lattnerf25f7102007-05-06 00:53:07 +00002701 case bitc::CST_CODE_CSTRING: { // CSTRING: [values]
2702 if (Record.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002703 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002704
Benjamin Kramer9704ed02012-05-28 14:10:31 +00002705 SmallString<16> Elts(Record.begin(), Record.end());
Chris Lattnerbb8278a2012-02-05 02:41:35 +00002706 V = ConstantDataArray::getString(Context, Elts,
2707 BitCode == bitc::CST_CODE_CSTRING);
Chris Lattnerf25f7102007-05-06 00:53:07 +00002708 break;
2709 }
Chris Lattner372dd1e2012-01-30 00:51:16 +00002710 case bitc::CST_CODE_DATA: {// DATA: [n x value]
2711 if (Record.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002712 return error("Invalid record");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002713
Chris Lattner372dd1e2012-01-30 00:51:16 +00002714 Type *EltTy = cast<SequentialType>(CurTy)->getElementType();
Chris Lattner372dd1e2012-01-30 00:51:16 +00002715 if (EltTy->isIntegerTy(8)) {
2716 SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end());
2717 if (isa<VectorType>(CurTy))
2718 V = ConstantDataVector::get(Context, Elts);
2719 else
2720 V = ConstantDataArray::get(Context, Elts);
2721 } else if (EltTy->isIntegerTy(16)) {
2722 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
2723 if (isa<VectorType>(CurTy))
2724 V = ConstantDataVector::get(Context, Elts);
2725 else
2726 V = ConstantDataArray::get(Context, Elts);
2727 } else if (EltTy->isIntegerTy(32)) {
2728 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
2729 if (isa<VectorType>(CurTy))
2730 V = ConstantDataVector::get(Context, Elts);
2731 else
2732 V = ConstantDataArray::get(Context, Elts);
2733 } else if (EltTy->isIntegerTy(64)) {
2734 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
2735 if (isa<VectorType>(CurTy))
2736 V = ConstantDataVector::get(Context, Elts);
2737 else
2738 V = ConstantDataArray::get(Context, Elts);
Justin Bognera43eacb2016-01-06 22:31:32 +00002739 } else if (EltTy->isHalfTy()) {
2740 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
2741 if (isa<VectorType>(CurTy))
2742 V = ConstantDataVector::getFP(Context, Elts);
2743 else
2744 V = ConstantDataArray::getFP(Context, Elts);
Chris Lattner372dd1e2012-01-30 00:51:16 +00002745 } else if (EltTy->isFloatTy()) {
Justin Bognera43eacb2016-01-06 22:31:32 +00002746 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
Chris Lattner372dd1e2012-01-30 00:51:16 +00002747 if (isa<VectorType>(CurTy))
Justin Bognera43eacb2016-01-06 22:31:32 +00002748 V = ConstantDataVector::getFP(Context, Elts);
Chris Lattner372dd1e2012-01-30 00:51:16 +00002749 else
Justin Bognera43eacb2016-01-06 22:31:32 +00002750 V = ConstantDataArray::getFP(Context, Elts);
Chris Lattner372dd1e2012-01-30 00:51:16 +00002751 } else if (EltTy->isDoubleTy()) {
Justin Bognera43eacb2016-01-06 22:31:32 +00002752 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
Chris Lattner372dd1e2012-01-30 00:51:16 +00002753 if (isa<VectorType>(CurTy))
Justin Bognera43eacb2016-01-06 22:31:32 +00002754 V = ConstantDataVector::getFP(Context, Elts);
Chris Lattner372dd1e2012-01-30 00:51:16 +00002755 else
Justin Bognera43eacb2016-01-06 22:31:32 +00002756 V = ConstantDataArray::getFP(Context, Elts);
Chris Lattner372dd1e2012-01-30 00:51:16 +00002757 } else {
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002758 return error("Invalid type for value");
Chris Lattner372dd1e2012-01-30 00:51:16 +00002759 }
2760 break;
2761 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002762 case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval]
Rafael Espindola48da4f42013-11-04 16:16:24 +00002763 if (Record.size() < 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002764 return error("Invalid record");
2765 int Opc = getDecodedBinaryOpcode(Record[0], CurTy);
Chris Lattner890683d2007-04-24 18:15:21 +00002766 if (Opc < 0) {
Owen Andersonb292b8c2009-07-30 23:03:37 +00002767 V = UndefValue::get(CurTy); // Unknown binop.
Chris Lattner890683d2007-04-24 18:15:21 +00002768 } else {
2769 Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy);
2770 Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy);
Dan Gohman1b849082009-09-07 23:54:19 +00002771 unsigned Flags = 0;
2772 if (Record.size() >= 4) {
2773 if (Opc == Instruction::Add ||
2774 Opc == Instruction::Sub ||
Chris Lattnera676c0f2011-02-07 16:40:21 +00002775 Opc == Instruction::Mul ||
2776 Opc == Instruction::Shl) {
Dan Gohman1b849082009-09-07 23:54:19 +00002777 if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP))
2778 Flags |= OverflowingBinaryOperator::NoSignedWrap;
2779 if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
2780 Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
Chris Lattner35315d02011-02-06 21:44:57 +00002781 } else if (Opc == Instruction::SDiv ||
Chris Lattnera676c0f2011-02-07 16:40:21 +00002782 Opc == Instruction::UDiv ||
2783 Opc == Instruction::LShr ||
2784 Opc == Instruction::AShr) {
Chris Lattner35315d02011-02-06 21:44:57 +00002785 if (Record[3] & (1 << bitc::PEO_EXACT))
Dan Gohman1b849082009-09-07 23:54:19 +00002786 Flags |= SDivOperator::IsExact;
2787 }
2788 }
2789 V = ConstantExpr::get(Opc, LHS, RHS, Flags);
Chris Lattner890683d2007-04-24 18:15:21 +00002790 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002791 break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002792 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002793 case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, 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");
2796 int Opc = getDecodedCastOpcode(Record[0]);
Chris Lattner890683d2007-04-24 18:15:21 +00002797 if (Opc < 0) {
Owen Andersonb292b8c2009-07-30 23:03:37 +00002798 V = UndefValue::get(CurTy); // Unknown cast.
Chris Lattner890683d2007-04-24 18:15:21 +00002799 } else {
Chris Lattner229907c2011-07-18 04:54:35 +00002800 Type *OpTy = getTypeByID(Record[1]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00002801 if (!OpTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002802 return error("Invalid record");
Chris Lattner890683d2007-04-24 18:15:21 +00002803 Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002804 V = UpgradeBitCastExpr(Opc, Op, CurTy);
2805 if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy);
Chris Lattner890683d2007-04-24 18:15:21 +00002806 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002807 break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002808 }
Dan Gohman1639c392009-07-27 21:53:46 +00002809 case bitc::CST_CODE_CE_INBOUNDS_GEP:
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002810 case bitc::CST_CODE_CE_GEP: { // CE_GEP: [n x operands]
David Blaikieb9263572015-03-13 21:03:36 +00002811 unsigned OpNum = 0;
2812 Type *PointeeType = nullptr;
2813 if (Record.size() % 2)
2814 PointeeType = getTypeByID(Record[OpNum++]);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002815 SmallVector<Constant*, 16> Elts;
David Blaikieb9263572015-03-13 21:03:36 +00002816 while (OpNum != Record.size()) {
2817 Type *ElTy = getTypeByID(Record[OpNum++]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00002818 if (!ElTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002819 return error("Invalid record");
David Blaikieb9263572015-03-13 21:03:36 +00002820 Elts.push_back(ValueList.getConstantFwdRef(Record[OpNum++], ElTy));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002821 }
David Blaikieb9263572015-03-13 21:03:36 +00002822
David Blaikieb9263572015-03-13 21:03:36 +00002823 if (PointeeType &&
David Blaikie4a2e73b2015-04-02 18:55:32 +00002824 PointeeType !=
2825 cast<SequentialType>(Elts[0]->getType()->getScalarType())
2826 ->getElementType())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002827 return error("Explicit gep operator type does not match pointee type "
David Blaikie12cf5d702015-03-16 22:03:50 +00002828 "of pointer operand");
David Blaikie4a2e73b2015-04-02 18:55:32 +00002829
2830 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
2831 V = ConstantExpr::getGetElementPtr(PointeeType, Elts[0], Indices,
2832 BitCode ==
2833 bitc::CST_CODE_CE_INBOUNDS_GEP);
Chris Lattner890683d2007-04-24 18:15:21 +00002834 break;
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002835 }
Joe Abbey1a6e7702013-09-12 22:02:31 +00002836 case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#]
Rafael Espindola48da4f42013-11-04 16:16:24 +00002837 if (Record.size() < 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002838 return error("Invalid record");
Joe Abbey1a6e7702013-09-12 22:02:31 +00002839
2840 Type *SelectorTy = Type::getInt1Ty(Context);
2841
Filipe Cabecinhas984fefd2015-08-31 18:00:30 +00002842 // The selector might be an i1 or an <n x i1>
2843 // Get the type from the ValueList before getting a forward ref.
Joe Abbey1a6e7702013-09-12 22:02:31 +00002844 if (VectorType *VTy = dyn_cast<VectorType>(CurTy))
Filipe Cabecinhas984fefd2015-08-31 18:00:30 +00002845 if (Value *V = ValueList[Record[0]])
2846 if (SelectorTy != V->getType())
2847 SelectorTy = VectorType::get(SelectorTy, VTy->getNumElements());
Joe Abbey1a6e7702013-09-12 22:02:31 +00002848
2849 V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0],
2850 SelectorTy),
2851 ValueList.getConstantFwdRef(Record[1],CurTy),
2852 ValueList.getConstantFwdRef(Record[2],CurTy));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002853 break;
Joe Abbey1a6e7702013-09-12 22:02:31 +00002854 }
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002855 case bitc::CST_CODE_CE_EXTRACTELT
2856 : { // CE_EXTRACTELT: [opty, opval, opty, opval]
Rafael Espindola48da4f42013-11-04 16:16:24 +00002857 if (Record.size() < 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002858 return error("Invalid record");
Chris Lattner229907c2011-07-18 04:54:35 +00002859 VectorType *OpTy =
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002860 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
Craig Topper2617dcc2014-04-15 06:32:26 +00002861 if (!OpTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002862 return error("Invalid record");
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002863 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002864 Constant *Op1 = nullptr;
2865 if (Record.size() == 4) {
2866 Type *IdxTy = getTypeByID(Record[2]);
2867 if (!IdxTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002868 return error("Invalid record");
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002869 Op1 = ValueList.getConstantFwdRef(Record[3], IdxTy);
2870 } else // TODO: Remove with llvm 4.0
2871 Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
2872 if (!Op1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002873 return error("Invalid record");
Owen Anderson487375e2009-07-29 18:55:55 +00002874 V = ConstantExpr::getExtractElement(Op0, Op1);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002875 break;
2876 }
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002877 case bitc::CST_CODE_CE_INSERTELT
2878 : { // CE_INSERTELT: [opval, opval, opty, opval]
Chris Lattner229907c2011-07-18 04:54:35 +00002879 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
Craig Topper2617dcc2014-04-15 06:32:26 +00002880 if (Record.size() < 3 || !OpTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002881 return error("Invalid record");
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002882 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
2883 Constant *Op1 = ValueList.getConstantFwdRef(Record[1],
2884 OpTy->getElementType());
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002885 Constant *Op2 = nullptr;
2886 if (Record.size() == 4) {
2887 Type *IdxTy = getTypeByID(Record[2]);
2888 if (!IdxTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002889 return error("Invalid record");
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002890 Op2 = ValueList.getConstantFwdRef(Record[3], IdxTy);
2891 } else // TODO: Remove with llvm 4.0
2892 Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
2893 if (!Op2)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002894 return error("Invalid record");
Owen Anderson487375e2009-07-29 18:55:55 +00002895 V = ConstantExpr::getInsertElement(Op0, Op1, Op2);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002896 break;
2897 }
2898 case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval]
Chris Lattner229907c2011-07-18 04:54:35 +00002899 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
Craig Topper2617dcc2014-04-15 06:32:26 +00002900 if (Record.size() < 3 || !OpTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002901 return error("Invalid record");
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002902 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
2903 Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy);
Chris Lattner229907c2011-07-18 04:54:35 +00002904 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
Owen Andersone9f98042009-07-07 20:18:58 +00002905 OpTy->getNumElements());
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002906 Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy);
Owen Anderson487375e2009-07-29 18:55:55 +00002907 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002908 break;
2909 }
Nate Begeman94aa38d2009-02-12 21:28:33 +00002910 case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval]
Chris Lattner229907c2011-07-18 04:54:35 +00002911 VectorType *RTy = dyn_cast<VectorType>(CurTy);
2912 VectorType *OpTy =
Duncan Sands89d412a2010-10-28 15:47:26 +00002913 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
Craig Topper2617dcc2014-04-15 06:32:26 +00002914 if (Record.size() < 4 || !RTy || !OpTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002915 return error("Invalid record");
Nate Begeman94aa38d2009-02-12 21:28:33 +00002916 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
2917 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
Chris Lattner229907c2011-07-18 04:54:35 +00002918 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
Owen Andersone9f98042009-07-07 20:18:58 +00002919 RTy->getNumElements());
Nate Begeman94aa38d2009-02-12 21:28:33 +00002920 Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy);
Owen Anderson487375e2009-07-29 18:55:55 +00002921 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Nate Begeman94aa38d2009-02-12 21:28:33 +00002922 break;
2923 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002924 case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred]
Rafael Espindola48da4f42013-11-04 16:16:24 +00002925 if (Record.size() < 4)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002926 return error("Invalid record");
Chris Lattner229907c2011-07-18 04:54:35 +00002927 Type *OpTy = getTypeByID(Record[0]);
Craig Topper2617dcc2014-04-15 06:32:26 +00002928 if (!OpTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002929 return error("Invalid record");
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002930 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
2931 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
2932
Duncan Sands9dff9be2010-02-15 16:12:20 +00002933 if (OpTy->isFPOrFPVectorTy())
Owen Anderson487375e2009-07-29 18:55:55 +00002934 V = ConstantExpr::getFCmp(Record[3], Op0, Op1);
Nate Begemand2195702008-05-12 19:01:56 +00002935 else
Owen Anderson487375e2009-07-29 18:55:55 +00002936 V = ConstantExpr::getICmp(Record[3], Op0, Op1);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00002937 break;
Chris Lattner1663cca2007-04-24 05:48:56 +00002938 }
Chad Rosierd8c76102012-09-05 19:00:49 +00002939 // This maintains backward compatibility, pre-asm dialect keywords.
Chad Rosier5895eda2012-09-05 06:28:52 +00002940 // FIXME: Remove with the 4.0 release.
Chad Rosier18fcdcf2012-09-05 00:56:20 +00002941 case bitc::CST_CODE_INLINEASM_OLD: {
Rafael Espindola48da4f42013-11-04 16:16:24 +00002942 if (Record.size() < 2)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002943 return error("Invalid record");
Chris Lattneraf8fffc2007-05-06 01:58:20 +00002944 std::string AsmStr, ConstrStr;
Dale Johannesenfd04c742009-10-13 20:46:56 +00002945 bool HasSideEffects = Record[0] & 1;
Dale Johannesen1cfb9582009-10-21 23:28:00 +00002946 bool IsAlignStack = Record[0] >> 1;
Chris Lattneraf8fffc2007-05-06 01:58:20 +00002947 unsigned AsmStrSize = Record[1];
2948 if (2+AsmStrSize >= Record.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002949 return error("Invalid record");
Chris Lattneraf8fffc2007-05-06 01:58:20 +00002950 unsigned ConstStrSize = Record[2+AsmStrSize];
2951 if (3+AsmStrSize+ConstStrSize > Record.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002952 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002953
Chris Lattneraf8fffc2007-05-06 01:58:20 +00002954 for (unsigned i = 0; i != AsmStrSize; ++i)
2955 AsmStr += (char)Record[2+i];
2956 for (unsigned i = 0; i != ConstStrSize; ++i)
2957 ConstrStr += (char)Record[3+AsmStrSize+i];
Chris Lattner229907c2011-07-18 04:54:35 +00002958 PointerType *PTy = cast<PointerType>(CurTy);
Chris Lattneraf8fffc2007-05-06 01:58:20 +00002959 V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
Dale Johannesen1cfb9582009-10-21 23:28:00 +00002960 AsmStr, ConstrStr, HasSideEffects, IsAlignStack);
Chris Lattneraf8fffc2007-05-06 01:58:20 +00002961 break;
2962 }
Chad Rosierd8c76102012-09-05 19:00:49 +00002963 // This version adds support for the asm dialect keywords (e.g.,
2964 // inteldialect).
Chad Rosier18fcdcf2012-09-05 00:56:20 +00002965 case bitc::CST_CODE_INLINEASM: {
Rafael Espindola48da4f42013-11-04 16:16:24 +00002966 if (Record.size() < 2)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002967 return error("Invalid record");
Chad Rosier18fcdcf2012-09-05 00:56:20 +00002968 std::string AsmStr, ConstrStr;
2969 bool HasSideEffects = Record[0] & 1;
2970 bool IsAlignStack = (Record[0] >> 1) & 1;
2971 unsigned AsmDialect = Record[0] >> 2;
2972 unsigned AsmStrSize = Record[1];
2973 if (2+AsmStrSize >= Record.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002974 return error("Invalid record");
Chad Rosier18fcdcf2012-09-05 00:56:20 +00002975 unsigned ConstStrSize = Record[2+AsmStrSize];
2976 if (3+AsmStrSize+ConstStrSize > Record.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002977 return error("Invalid record");
Chad Rosier18fcdcf2012-09-05 00:56:20 +00002978
2979 for (unsigned i = 0; i != AsmStrSize; ++i)
2980 AsmStr += (char)Record[2+i];
2981 for (unsigned i = 0; i != ConstStrSize; ++i)
2982 ConstrStr += (char)Record[3+AsmStrSize+i];
2983 PointerType *PTy = cast<PointerType>(CurTy);
2984 V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
2985 AsmStr, ConstrStr, HasSideEffects, IsAlignStack,
Chad Rosierd8c76102012-09-05 19:00:49 +00002986 InlineAsm::AsmDialect(AsmDialect));
Chad Rosier18fcdcf2012-09-05 00:56:20 +00002987 break;
2988 }
Chris Lattner5956dc82009-10-28 05:53:48 +00002989 case bitc::CST_CODE_BLOCKADDRESS:{
Rafael Espindola48da4f42013-11-04 16:16:24 +00002990 if (Record.size() < 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002991 return error("Invalid record");
Chris Lattner229907c2011-07-18 04:54:35 +00002992 Type *FnTy = getTypeByID(Record[0]);
Craig Topper2617dcc2014-04-15 06:32:26 +00002993 if (!FnTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002994 return error("Invalid record");
Chris Lattner5956dc82009-10-28 05:53:48 +00002995 Function *Fn =
2996 dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy));
Craig Topper2617dcc2014-04-15 06:32:26 +00002997 if (!Fn)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00002998 return error("Invalid record");
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00002999
3000 // If the function is already parsed we can insert the block address right
3001 // away.
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00003002 BasicBlock *BB;
3003 unsigned BBID = Record[2];
3004 if (!BBID)
3005 // Invalid reference to entry block.
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003006 return error("Invalid ID");
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00003007 if (!Fn->empty()) {
3008 Function::iterator BBI = Fn->begin(), BBE = Fn->end();
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00003009 for (size_t I = 0, E = BBID; I != E; ++I) {
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00003010 if (BBI == BBE)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003011 return error("Invalid ID");
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00003012 ++BBI;
3013 }
Duncan P. N. Exon Smithfb1743a32015-10-13 16:48:55 +00003014 BB = &*BBI;
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00003015 } else {
3016 // Otherwise insert a placeholder and remember it so it can be inserted
3017 // when the function is parsed.
Duncan P. N. Exon Smith5a511b52014-08-05 17:49:48 +00003018 auto &FwdBBs = BasicBlockFwdRefs[Fn];
3019 if (FwdBBs.empty())
3020 BasicBlockFwdRefQueue.push_back(Fn);
Duncan P. N. Exon Smith5a5fd7b2014-08-16 01:54:37 +00003021 if (FwdBBs.size() < BBID + 1)
3022 FwdBBs.resize(BBID + 1);
3023 if (!FwdBBs[BBID])
3024 FwdBBs[BBID] = BasicBlock::Create(Context);
3025 BB = FwdBBs[BBID];
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00003026 }
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00003027 V = BlockAddress::get(Fn, BB);
Chris Lattner5956dc82009-10-28 05:53:48 +00003028 break;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003029 }
Chris Lattnerfbc1d332007-04-24 03:30:34 +00003030 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003031
David Majnemer8a1c45d2015-12-12 05:38:55 +00003032 ValueList.assignValue(V, NextCstNo);
Chris Lattner1663cca2007-04-24 05:48:56 +00003033 ++NextCstNo;
Chris Lattnerfbc1d332007-04-24 03:30:34 +00003034 }
3035}
Chris Lattner1314b992007-04-22 06:23:29 +00003036
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003037std::error_code BitcodeReader::parseUseLists() {
Chad Rosierca2567b2011-12-07 21:44:12 +00003038 if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003039 return error("Invalid record");
Chad Rosierca2567b2011-12-07 21:44:12 +00003040
Chad Rosierca2567b2011-12-07 21:44:12 +00003041 // Read all the records.
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00003042 SmallVector<uint64_t, 64> Record;
Chad Rosierca2567b2011-12-07 21:44:12 +00003043 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00003044 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00003045
Chris Lattner27d38752013-01-20 02:13:19 +00003046 switch (Entry.Kind) {
3047 case BitstreamEntry::SubBlock: // Handled for us already.
3048 case BitstreamEntry::Error:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003049 return error("Malformed block");
Chris Lattner27d38752013-01-20 02:13:19 +00003050 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003051 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00003052 case BitstreamEntry::Record:
3053 // The interesting case.
3054 break;
Chad Rosierca2567b2011-12-07 21:44:12 +00003055 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003056
Chad Rosierca2567b2011-12-07 21:44:12 +00003057 // Read a use list record.
3058 Record.clear();
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00003059 bool IsBB = false;
Chris Lattner27d38752013-01-20 02:13:19 +00003060 switch (Stream.readRecord(Entry.ID, Record)) {
Chad Rosierca2567b2011-12-07 21:44:12 +00003061 default: // Default behavior: unknown type.
3062 break;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00003063 case bitc::USELIST_CODE_BB:
3064 IsBB = true;
3065 // fallthrough
3066 case bitc::USELIST_CODE_DEFAULT: {
Chad Rosierca2567b2011-12-07 21:44:12 +00003067 unsigned RecordLength = Record.size();
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00003068 if (RecordLength < 3)
3069 // Records should have at least an ID and two indexes.
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003070 return error("Invalid record");
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00003071 unsigned ID = Record.back();
3072 Record.pop_back();
3073
3074 Value *V;
3075 if (IsBB) {
3076 assert(ID < FunctionBBs.size() && "Basic block not found");
3077 V = FunctionBBs[ID];
3078 } else
3079 V = ValueList[ID];
3080 unsigned NumUses = 0;
3081 SmallDenseMap<const Use *, unsigned, 16> Order;
Rafael Espindola257a3532016-01-15 19:00:20 +00003082 for (const Use &U : V->materialized_uses()) {
Duncan P. N. Exon Smith13183642014-08-16 01:54:34 +00003083 if (++NumUses > Record.size())
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00003084 break;
Duncan P. N. Exon Smith13183642014-08-16 01:54:34 +00003085 Order[&U] = Record[NumUses - 1];
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00003086 }
3087 if (Order.size() != Record.size() || NumUses > Record.size())
3088 // Mismatches can happen if the functions are being materialized lazily
3089 // (out-of-order), or a value has been upgraded.
3090 break;
3091
3092 V->sortUseList([&](const Use &L, const Use &R) {
3093 return Order.lookup(&L) < Order.lookup(&R);
3094 });
Chad Rosierca2567b2011-12-07 21:44:12 +00003095 break;
3096 }
3097 }
3098 }
3099}
3100
Manman Ren4a9b0eb2015-03-13 19:24:30 +00003101/// When we see the block for metadata, remember where it is and then skip it.
3102/// This lets us lazily deserialize the metadata.
3103std::error_code BitcodeReader::rememberAndSkipMetadata() {
3104 // Save the current stream state.
3105 uint64_t CurBit = Stream.GetCurrentBitNo();
3106 DeferredMetadataInfo.push_back(CurBit);
3107
3108 // Skip over the block for now.
3109 if (Stream.SkipBlock())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003110 return error("Invalid record");
Manman Ren4a9b0eb2015-03-13 19:24:30 +00003111 return std::error_code();
3112}
3113
3114std::error_code BitcodeReader::materializeMetadata() {
3115 for (uint64_t BitPos : DeferredMetadataInfo) {
3116 // Move the bit stream to the saved position.
3117 Stream.JumpToBit(BitPos);
Teresa Johnsond4d3dfd2015-11-20 14:51:27 +00003118 if (std::error_code EC = parseMetadata(true))
Manman Ren4a9b0eb2015-03-13 19:24:30 +00003119 return EC;
3120 }
3121 DeferredMetadataInfo.clear();
3122 return std::error_code();
3123}
3124
Rafael Espindola468b8682015-04-01 14:44:59 +00003125void BitcodeReader::setStripDebugInfo() { StripDebugInfo = true; }
Rafael Espindola0d68b4c2015-03-30 21:36:43 +00003126
Teresa Johnson61b406e2015-12-29 23:00:22 +00003127void BitcodeReader::saveMetadataList(
3128 DenseMap<const Metadata *, unsigned> &MetadataToIDs, bool OnlyTempMD) {
3129 for (unsigned ID = 0; ID < MetadataList.size(); ++ID) {
3130 Metadata *MD = MetadataList[ID];
Teresa Johnsone5a61912015-12-17 17:14:09 +00003131 auto *N = dyn_cast_or_null<MDNode>(MD);
Teresa Johnson26aa9352015-12-30 19:13:57 +00003132 assert((!N || (N->isResolved() || N->isTemporary())) &&
3133 "Found non-resolved non-temp MDNode while saving metadata");
Teresa Johnsone5a61912015-12-17 17:14:09 +00003134 // Save all values if !OnlyTempMD, otherwise just the temporary metadata.
Teresa Johnson26aa9352015-12-30 19:13:57 +00003135 // Note that in the !OnlyTempMD case we need to save all Metadata, not
3136 // just MDNode, as we may have references to other types of module-level
3137 // metadata (e.g. ValueAsMetadata) from instructions.
Teresa Johnsone5a61912015-12-17 17:14:09 +00003138 if (!OnlyTempMD || (N && N->isTemporary())) {
3139 // Will call this after materializing each function, in order to
3140 // handle remapping of the function's instructions/metadata.
Teresa Johnson6f508af2016-01-21 16:46:40 +00003141 auto IterBool = MetadataToIDs.insert(std::make_pair(MD, ID));
Teresa Johnsone5a61912015-12-17 17:14:09 +00003142 // See if we already have an entry in that case.
Teresa Johnson6f508af2016-01-21 16:46:40 +00003143 if (OnlyTempMD && !IterBool.second) {
3144 assert(IterBool.first->second == ID &&
3145 "Inconsistent metadata value id");
Teresa Johnsone5a61912015-12-17 17:14:09 +00003146 continue;
3147 }
Teresa Johnsoncc428572015-12-30 19:32:24 +00003148 if (N && N->isTemporary())
3149 // Ensure that we assert if someone tries to RAUW this temporary
3150 // metadata while it is the key of a map. The flag will be set back
3151 // to true when the saved metadata list is destroyed.
3152 N->setCanReplace(false);
Teresa Johnsone5a61912015-12-17 17:14:09 +00003153 }
3154 }
3155}
3156
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003157/// When we see the block for a function body, remember where it is and then
3158/// skip it. This lets us lazily deserialize the functions.
3159std::error_code BitcodeReader::rememberAndSkipFunctionBody() {
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003160 // Get the function we are talking about.
3161 if (FunctionsWithBodies.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003162 return error("Insufficient function protos");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003163
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003164 Function *Fn = FunctionsWithBodies.back();
3165 FunctionsWithBodies.pop_back();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003166
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003167 // Save the current stream state.
3168 uint64_t CurBit = Stream.GetCurrentBitNo();
Teresa Johnson1493ad92015-10-10 14:18:36 +00003169 assert(
3170 (DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) &&
3171 "Mismatch between VST and scanned function offsets");
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003172 DeferredFunctionInfo[Fn] = CurBit;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003173
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003174 // Skip over the function block for now.
3175 if (Stream.SkipBlock())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003176 return error("Invalid record");
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003177 return std::error_code();
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003178}
3179
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003180std::error_code BitcodeReader::globalCleanup() {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003181 // Patch the initializers for globals and aliases up.
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003182 resolveGlobalAndAliasInits();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003183 if (!GlobalInits.empty() || !AliasInits.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003184 return error("Malformed global initializer set");
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003185
3186 // Look for intrinsic functions which need to be upgraded at some point
Yaron Kerenef5e7ad2015-06-12 18:13:20 +00003187 for (Function &F : *TheModule) {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003188 Function *NewFn;
Yaron Kerenef5e7ad2015-06-12 18:13:20 +00003189 if (UpgradeIntrinsicFunction(&F, NewFn))
Rafael Espindola4e721212015-07-02 16:22:40 +00003190 UpgradedIntrinsics[&F] = NewFn;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003191 }
3192
3193 // Look for global variables which need to be renamed.
Yaron Kerenef5e7ad2015-06-12 18:13:20 +00003194 for (GlobalVariable &GV : TheModule->globals())
3195 UpgradeGlobalVariable(&GV);
Reid Klecknerfceb76f2014-05-16 20:39:27 +00003196
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003197 // Force deallocation of memory for these vectors to favor the client that
3198 // want lazy deserialization.
3199 std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits);
3200 std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003201 return std::error_code();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003202}
3203
Teresa Johnson1493ad92015-10-10 14:18:36 +00003204/// Support for lazy parsing of function bodies. This is required if we
3205/// either have an old bitcode file without a VST forward declaration record,
3206/// or if we have an anonymous function being materialized, since anonymous
3207/// functions do not have a name and are therefore not in the VST.
3208std::error_code BitcodeReader::rememberAndSkipFunctionBodies() {
3209 Stream.JumpToBit(NextUnreadBit);
3210
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003211 if (Stream.AtEndOfStream())
3212 return error("Could not find function in stream");
Teresa Johnson1493ad92015-10-10 14:18:36 +00003213
Filipe Cabecinhas7aae2f22015-11-03 13:48:26 +00003214 if (!SeenFirstFunctionBody)
3215 return error("Trying to materialize functions before seeing function blocks");
3216
Teresa Johnson1493ad92015-10-10 14:18:36 +00003217 // An old bitcode file with the symbol table at the end would have
3218 // finished the parse greedily.
3219 assert(SeenValueSymbolTable);
3220
3221 SmallVector<uint64_t, 64> Record;
3222
3223 while (1) {
3224 BitstreamEntry Entry = Stream.advance();
3225 switch (Entry.Kind) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003226 default:
3227 return error("Expect SubBlock");
3228 case BitstreamEntry::SubBlock:
3229 switch (Entry.ID) {
Teresa Johnson1493ad92015-10-10 14:18:36 +00003230 default:
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003231 return error("Expect function block");
3232 case bitc::FUNCTION_BLOCK_ID:
3233 if (std::error_code EC = rememberAndSkipFunctionBody())
3234 return EC;
3235 NextUnreadBit = Stream.GetCurrentBitNo();
3236 return std::error_code();
3237 }
Teresa Johnson1493ad92015-10-10 14:18:36 +00003238 }
3239 }
3240}
3241
Mehdi Amini5d303282015-10-26 18:37:00 +00003242std::error_code BitcodeReader::parseBitcodeVersion() {
3243 if (Stream.EnterSubBlock(bitc::IDENTIFICATION_BLOCK_ID))
3244 return error("Invalid record");
3245
3246 // Read all the records.
3247 SmallVector<uint64_t, 64> Record;
3248 while (1) {
3249 BitstreamEntry Entry = Stream.advance();
3250
3251 switch (Entry.Kind) {
3252 default:
3253 case BitstreamEntry::Error:
3254 return error("Malformed block");
3255 case BitstreamEntry::EndBlock:
3256 return std::error_code();
3257 case BitstreamEntry::Record:
3258 // The interesting case.
3259 break;
3260 }
3261
3262 // Read a record.
3263 Record.clear();
3264 unsigned BitCode = Stream.readRecord(Entry.ID, Record);
3265 switch (BitCode) {
3266 default: // Default behavior: reject
3267 return error("Invalid value");
3268 case bitc::IDENTIFICATION_CODE_STRING: { // IDENTIFICATION: [strchr x
3269 // N]
3270 convertToString(Record, 0, ProducerIdentification);
3271 break;
3272 }
3273 case bitc::IDENTIFICATION_CODE_EPOCH: { // EPOCH: [epoch#]
3274 unsigned epoch = (unsigned)Record[0];
3275 if (epoch != bitc::BITCODE_CURRENT_EPOCH) {
Oleksiy Vyalov6c2403f2015-10-26 22:37:36 +00003276 return error(
3277 Twine("Incompatible epoch: Bitcode '") + Twine(epoch) +
3278 "' vs current: '" + Twine(bitc::BITCODE_CURRENT_EPOCH) + "'");
Mehdi Amini5d303282015-10-26 18:37:00 +00003279 }
3280 }
3281 }
3282 }
3283}
3284
Teresa Johnson1493ad92015-10-10 14:18:36 +00003285std::error_code BitcodeReader::parseModule(uint64_t ResumeBit,
Manman Ren4a9b0eb2015-03-13 19:24:30 +00003286 bool ShouldLazyLoadMetadata) {
Teresa Johnson1493ad92015-10-10 14:18:36 +00003287 if (ResumeBit)
3288 Stream.JumpToBit(ResumeBit);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003289 else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003290 return error("Invalid record");
Chris Lattner1314b992007-04-22 06:23:29 +00003291
Chris Lattner1314b992007-04-22 06:23:29 +00003292 SmallVector<uint64_t, 64> Record;
3293 std::vector<std::string> SectionTable;
Gordon Henriksend930f912008-08-17 18:44:35 +00003294 std::vector<std::string> GCTable;
Chris Lattner1314b992007-04-22 06:23:29 +00003295
3296 // Read all the records for this module.
Chris Lattner27d38752013-01-20 02:13:19 +00003297 while (1) {
3298 BitstreamEntry Entry = Stream.advance();
Joe Abbey97b7a172013-02-06 22:14:06 +00003299
Chris Lattner27d38752013-01-20 02:13:19 +00003300 switch (Entry.Kind) {
3301 case BitstreamEntry::Error:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003302 return error("Malformed block");
Chris Lattner27d38752013-01-20 02:13:19 +00003303 case BitstreamEntry::EndBlock:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003304 return globalCleanup();
Joe Abbey97b7a172013-02-06 22:14:06 +00003305
Chris Lattner27d38752013-01-20 02:13:19 +00003306 case BitstreamEntry::SubBlock:
3307 switch (Entry.ID) {
Chris Lattner1314b992007-04-22 06:23:29 +00003308 default: // Skip unknown content.
3309 if (Stream.SkipBlock())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003310 return error("Invalid record");
Chris Lattner1314b992007-04-22 06:23:29 +00003311 break;
Chris Lattner6eeea5d2007-05-05 18:57:30 +00003312 case bitc::BLOCKINFO_BLOCK_ID:
3313 if (Stream.ReadBlockInfoBlock())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003314 return error("Malformed block");
Chris Lattner6eeea5d2007-05-05 18:57:30 +00003315 break;
Chris Lattnerfee5a372007-05-04 03:30:17 +00003316 case bitc::PARAMATTR_BLOCK_ID:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003317 if (std::error_code EC = parseAttributeBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00003318 return EC;
Chris Lattnerfee5a372007-05-04 03:30:17 +00003319 break;
Bill Wendlingba629332013-02-10 23:24:25 +00003320 case bitc::PARAMATTR_GROUP_BLOCK_ID:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003321 if (std::error_code EC = parseAttributeGroupBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00003322 return EC;
Bill Wendlingba629332013-02-10 23:24:25 +00003323 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003324 case bitc::TYPE_BLOCK_ID_NEW:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003325 if (std::error_code EC = parseTypeTable())
Rafael Espindola48da4f42013-11-04 16:16:24 +00003326 return EC;
Chris Lattner1314b992007-04-22 06:23:29 +00003327 break;
Chris Lattnerccaa4482007-04-23 21:26:05 +00003328 case bitc::VALUE_SYMTAB_BLOCK_ID:
Teresa Johnsonff642b92015-09-17 20:12:00 +00003329 if (!SeenValueSymbolTable) {
3330 // Either this is an old form VST without function index and an
3331 // associated VST forward declaration record (which would have caused
3332 // the VST to be jumped to and parsed before it was encountered
3333 // normally in the stream), or there were no function blocks to
3334 // trigger an earlier parsing of the VST.
3335 assert(VSTOffset == 0 || FunctionsWithBodies.empty());
3336 if (std::error_code EC = parseValueSymbolTable())
3337 return EC;
3338 SeenValueSymbolTable = true;
3339 } else {
3340 // We must have had a VST forward declaration record, which caused
3341 // the parser to jump to and parse the VST earlier.
3342 assert(VSTOffset > 0);
3343 if (Stream.SkipBlock())
3344 return error("Invalid record");
3345 }
Chris Lattnerccaa4482007-04-23 21:26:05 +00003346 break;
Chris Lattnerfbc1d332007-04-24 03:30:34 +00003347 case bitc::CONSTANTS_BLOCK_ID:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003348 if (std::error_code EC = parseConstants())
Rafael Espindola48da4f42013-11-04 16:16:24 +00003349 return EC;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003350 if (std::error_code EC = resolveGlobalAndAliasInits())
Rafael Espindola48da4f42013-11-04 16:16:24 +00003351 return EC;
Chris Lattnerfbc1d332007-04-24 03:30:34 +00003352 break;
Devang Patel7428d8a2009-07-22 17:43:22 +00003353 case bitc::METADATA_BLOCK_ID:
Manman Ren4a9b0eb2015-03-13 19:24:30 +00003354 if (ShouldLazyLoadMetadata && !IsMetadataMaterialized) {
3355 if (std::error_code EC = rememberAndSkipMetadata())
3356 return EC;
3357 break;
3358 }
3359 assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata");
Teresa Johnsond4d3dfd2015-11-20 14:51:27 +00003360 if (std::error_code EC = parseMetadata(true))
Rafael Espindola48da4f42013-11-04 16:16:24 +00003361 return EC;
Devang Patel7428d8a2009-07-22 17:43:22 +00003362 break;
Teresa Johnson12545072015-11-15 02:00:09 +00003363 case bitc::METADATA_KIND_BLOCK_ID:
3364 if (std::error_code EC = parseMetadataKinds())
3365 return EC;
3366 break;
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003367 case bitc::FUNCTION_BLOCK_ID:
3368 // If this is the first function body we've seen, reverse the
3369 // FunctionsWithBodies list.
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003370 if (!SeenFirstFunctionBody) {
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003371 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003372 if (std::error_code EC = globalCleanup())
Rafael Espindola48da4f42013-11-04 16:16:24 +00003373 return EC;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003374 SeenFirstFunctionBody = true;
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003375 }
Joe Abbey97b7a172013-02-06 22:14:06 +00003376
Teresa Johnsonff642b92015-09-17 20:12:00 +00003377 if (VSTOffset > 0) {
3378 // If we have a VST forward declaration record, make sure we
3379 // parse the VST now if we haven't already. It is needed to
3380 // set up the DeferredFunctionInfo vector for lazy reading.
3381 if (!SeenValueSymbolTable) {
3382 if (std::error_code EC =
3383 BitcodeReader::parseValueSymbolTable(VSTOffset))
3384 return EC;
3385 SeenValueSymbolTable = true;
Teresa Johnson1493ad92015-10-10 14:18:36 +00003386 // Fall through so that we record the NextUnreadBit below.
3387 // This is necessary in case we have an anonymous function that
3388 // is later materialized. Since it will not have a VST entry we
3389 // need to fall back to the lazy parse to find its offset.
Teresa Johnsonff642b92015-09-17 20:12:00 +00003390 } else {
3391 // If we have a VST forward declaration record, but have already
3392 // parsed the VST (just above, when the first function body was
3393 // encountered here), then we are resuming the parse after
Teresa Johnson1493ad92015-10-10 14:18:36 +00003394 // materializing functions. The ResumeBit points to the
3395 // start of the last function block recorded in the
3396 // DeferredFunctionInfo map. Skip it.
Teresa Johnsonff642b92015-09-17 20:12:00 +00003397 if (Stream.SkipBlock())
3398 return error("Invalid record");
3399 continue;
3400 }
3401 }
3402
3403 // Support older bitcode files that did not have the function
Teresa Johnson1493ad92015-10-10 14:18:36 +00003404 // index in the VST, nor a VST forward declaration record, as
3405 // well as anonymous functions that do not have VST entries.
Teresa Johnsonff642b92015-09-17 20:12:00 +00003406 // Build the DeferredFunctionInfo vector on the fly.
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003407 if (std::error_code EC = rememberAndSkipFunctionBody())
Rafael Espindola48da4f42013-11-04 16:16:24 +00003408 return EC;
Teresa Johnson1493ad92015-10-10 14:18:36 +00003409
Rafael Espindola1c863ca2015-06-22 18:06:15 +00003410 // Suspend parsing when we reach the function bodies. Subsequent
3411 // materialization calls will resume it when necessary. If the bitcode
3412 // file is old, the symbol table will be at the end instead and will not
3413 // have been seen yet. In this case, just finish the parse now.
3414 if (SeenValueSymbolTable) {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003415 NextUnreadBit = Stream.GetCurrentBitNo();
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003416 return std::error_code();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003417 }
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003418 break;
Chad Rosierca2567b2011-12-07 21:44:12 +00003419 case bitc::USELIST_BLOCK_ID:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003420 if (std::error_code EC = parseUseLists())
Rafael Espindola48da4f42013-11-04 16:16:24 +00003421 return EC;
Chad Rosierca2567b2011-12-07 21:44:12 +00003422 break;
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00003423 case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID:
3424 if (std::error_code EC = parseOperandBundleTags())
3425 return EC;
3426 break;
Chris Lattner1314b992007-04-22 06:23:29 +00003427 }
3428 continue;
Joe Abbey97b7a172013-02-06 22:14:06 +00003429
Chris Lattner27d38752013-01-20 02:13:19 +00003430 case BitstreamEntry::Record:
3431 // The interesting case.
3432 break;
Chris Lattner1314b992007-04-22 06:23:29 +00003433 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003434
Chris Lattner1314b992007-04-22 06:23:29 +00003435 // Read a record.
David Blaikie6a51dbd2015-09-17 22:18:59 +00003436 auto BitCode = Stream.readRecord(Entry.ID, Record);
3437 switch (BitCode) {
Chris Lattner1314b992007-04-22 06:23:29 +00003438 default: break; // Default behavior, ignore unknown content.
Jan Wen Voungafaced02012-10-11 20:20:40 +00003439 case bitc::MODULE_CODE_VERSION: { // VERSION: [version#]
Chris Lattner1314b992007-04-22 06:23:29 +00003440 if (Record.size() < 1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003441 return error("Invalid record");
Jan Wen Voungafaced02012-10-11 20:20:40 +00003442 // Only version #0 and #1 are supported so far.
3443 unsigned module_version = Record[0];
3444 switch (module_version) {
Rafael Espindola48da4f42013-11-04 16:16:24 +00003445 default:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003446 return error("Invalid value");
Jan Wen Voungafaced02012-10-11 20:20:40 +00003447 case 0:
3448 UseRelativeIDs = false;
3449 break;
3450 case 1:
3451 UseRelativeIDs = true;
3452 break;
3453 }
Chris Lattner1314b992007-04-22 06:23:29 +00003454 break;
Jan Wen Voungafaced02012-10-11 20:20:40 +00003455 }
Chris Lattnere14cb882007-05-04 19:11:41 +00003456 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00003457 std::string S;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003458 if (convertToString(Record, 0, S))
3459 return error("Invalid record");
Chris Lattner1314b992007-04-22 06:23:29 +00003460 TheModule->setTargetTriple(S);
3461 break;
3462 }
Chris Lattnere14cb882007-05-04 19:11:41 +00003463 case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00003464 std::string S;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003465 if (convertToString(Record, 0, S))
3466 return error("Invalid record");
Chris Lattner1314b992007-04-22 06:23:29 +00003467 TheModule->setDataLayout(S);
3468 break;
3469 }
Chris Lattnere14cb882007-05-04 19:11:41 +00003470 case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00003471 std::string S;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003472 if (convertToString(Record, 0, S))
3473 return error("Invalid record");
Chris Lattner1314b992007-04-22 06:23:29 +00003474 TheModule->setModuleInlineAsm(S);
3475 break;
3476 }
Bill Wendling706d3d62012-11-28 08:41:48 +00003477 case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N]
3478 // FIXME: Remove in 4.0.
3479 std::string S;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003480 if (convertToString(Record, 0, S))
3481 return error("Invalid record");
Bill Wendling706d3d62012-11-28 08:41:48 +00003482 // Ignore value.
3483 break;
3484 }
Chris Lattnere14cb882007-05-04 19:11:41 +00003485 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00003486 std::string S;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003487 if (convertToString(Record, 0, S))
3488 return error("Invalid record");
Chris Lattner1314b992007-04-22 06:23:29 +00003489 SectionTable.push_back(S);
3490 break;
3491 }
Gordon Henriksend930f912008-08-17 18:44:35 +00003492 case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N]
Gordon Henriksen71183b62007-12-10 03:18:06 +00003493 std::string S;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003494 if (convertToString(Record, 0, S))
3495 return error("Invalid record");
Gordon Henriksend930f912008-08-17 18:44:35 +00003496 GCTable.push_back(S);
Gordon Henriksen71183b62007-12-10 03:18:06 +00003497 break;
3498 }
David Majnemerdad0a642014-06-27 18:19:56 +00003499 case bitc::MODULE_CODE_COMDAT: { // COMDAT: [selection_kind, name]
3500 if (Record.size() < 2)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003501 return error("Invalid record");
David Majnemerdad0a642014-06-27 18:19:56 +00003502 Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]);
3503 unsigned ComdatNameSize = Record[1];
3504 std::string ComdatName;
3505 ComdatName.reserve(ComdatNameSize);
3506 for (unsigned i = 0; i != ComdatNameSize; ++i)
3507 ComdatName += (char)Record[2 + i];
3508 Comdat *C = TheModule->getOrInsertComdat(ComdatName);
3509 C->setSelectionKind(SK);
3510 ComdatList.push_back(C);
3511 break;
3512 }
Christopher Lamb54dd24c2007-12-11 08:59:05 +00003513 // GLOBALVAR: [pointer type, isconst, initid,
Rafael Espindola45e6c192011-01-08 16:42:36 +00003514 // linkage, alignment, section, visibility, threadlocal,
Peter Collingbourne69ba0162015-02-04 00:42:45 +00003515 // unnamed_addr, externally_initialized, dllstorageclass,
3516 // comdat]
Chris Lattner1314b992007-04-22 06:23:29 +00003517 case bitc::MODULE_CODE_GLOBALVAR: {
Chris Lattner4b00d922007-04-23 16:04:05 +00003518 if (Record.size() < 6)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003519 return error("Invalid record");
Chris Lattner229907c2011-07-18 04:54:35 +00003520 Type *Ty = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00003521 if (!Ty)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003522 return error("Invalid record");
David Blaikie1a848da2015-04-27 19:58:56 +00003523 bool isConstant = Record[1] & 1;
3524 bool explicitType = Record[1] & 2;
3525 unsigned AddressSpace;
3526 if (explicitType) {
3527 AddressSpace = Record[1] >> 2;
3528 } else {
3529 if (!Ty->isPointerTy())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003530 return error("Invalid type for value");
David Blaikie1a848da2015-04-27 19:58:56 +00003531 AddressSpace = cast<PointerType>(Ty)->getAddressSpace();
3532 Ty = cast<PointerType>(Ty)->getElementType();
3533 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003534
Rafael Espindola12ca34f2015-01-19 15:16:06 +00003535 uint64_t RawLinkage = Record[3];
3536 GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage);
JF Bastien30bf96b2015-02-22 19:32:03 +00003537 unsigned Alignment;
3538 if (std::error_code EC = parseAlignmentValue(Record[4], Alignment))
3539 return EC;
Chris Lattner1314b992007-04-22 06:23:29 +00003540 std::string Section;
3541 if (Record[5]) {
3542 if (Record[5]-1 >= SectionTable.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003543 return error("Invalid ID");
Chris Lattner1314b992007-04-22 06:23:29 +00003544 Section = SectionTable[Record[5]-1];
3545 }
Chris Lattner4b00d922007-04-23 16:04:05 +00003546 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00003547 // Local linkage must have default visibility.
3548 if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage))
3549 // FIXME: Change to an error if non-default in 4.0.
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003550 Visibility = getDecodedVisibility(Record[6]);
Hans Wennborgcbe34b42012-06-23 11:37:03 +00003551
3552 GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal;
Chris Lattner53862f72007-05-06 19:27:46 +00003553 if (Record.size() > 7)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003554 TLM = getDecodedThreadLocalMode(Record[7]);
Chris Lattner1314b992007-04-22 06:23:29 +00003555
Rafael Espindola45e6c192011-01-08 16:42:36 +00003556 bool UnnamedAddr = false;
3557 if (Record.size() > 8)
3558 UnnamedAddr = Record[8];
3559
Michael Gottesman27e7ef32013-02-05 05:57:38 +00003560 bool ExternallyInitialized = false;
3561 if (Record.size() > 9)
3562 ExternallyInitialized = Record[9];
3563
Chris Lattner1314b992007-04-22 06:23:29 +00003564 GlobalVariable *NewGV =
Craig Topper2617dcc2014-04-15 06:32:26 +00003565 new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, "", nullptr,
Michael Gottesman27e7ef32013-02-05 05:57:38 +00003566 TLM, AddressSpace, ExternallyInitialized);
Chris Lattner1314b992007-04-22 06:23:29 +00003567 NewGV->setAlignment(Alignment);
3568 if (!Section.empty())
3569 NewGV->setSection(Section);
3570 NewGV->setVisibility(Visibility);
Rafael Espindola45e6c192011-01-08 16:42:36 +00003571 NewGV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003572
Nico Rieck7157bb72014-01-14 15:22:47 +00003573 if (Record.size() > 10)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003574 NewGV->setDLLStorageClass(getDecodedDLLStorageClass(Record[10]));
Nico Rieck7157bb72014-01-14 15:22:47 +00003575 else
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003576 upgradeDLLImportExportLinkage(NewGV, RawLinkage);
Nico Rieck7157bb72014-01-14 15:22:47 +00003577
Chris Lattnerccaa4482007-04-23 21:26:05 +00003578 ValueList.push_back(NewGV);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003579
Chris Lattner47d131b2007-04-24 00:18:21 +00003580 // Remember which value to use for the global initializer.
3581 if (unsigned InitID = Record[2])
3582 GlobalInits.push_back(std::make_pair(NewGV, InitID-1));
David Majnemerdad0a642014-06-27 18:19:56 +00003583
Rafael Espindola12ca34f2015-01-19 15:16:06 +00003584 if (Record.size() > 11) {
David Majnemerdad0a642014-06-27 18:19:56 +00003585 if (unsigned ComdatID = Record[11]) {
Filipe Cabecinhas0eb8a592015-05-26 23:00:56 +00003586 if (ComdatID > ComdatList.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003587 return error("Invalid global variable comdat ID");
David Majnemerdad0a642014-06-27 18:19:56 +00003588 NewGV->setComdat(ComdatList[ComdatID - 1]);
3589 }
Rafael Espindola12ca34f2015-01-19 15:16:06 +00003590 } else if (hasImplicitComdat(RawLinkage)) {
3591 NewGV->setComdat(reinterpret_cast<Comdat *>(1));
3592 }
Chris Lattner1314b992007-04-22 06:23:29 +00003593 break;
3594 }
Chris Lattner4c0a6d62007-05-08 05:38:01 +00003595 // FUNCTION: [type, callingconv, isproto, linkage, paramattr,
Nico Rieck7157bb72014-01-14 15:22:47 +00003596 // alignment, section, visibility, gc, unnamed_addr,
Peter Collingbourne51d2de72014-12-03 02:08:38 +00003597 // prologuedata, dllstorageclass, comdat, prefixdata]
Chris Lattner1314b992007-04-22 06:23:29 +00003598 case bitc::MODULE_CODE_FUNCTION: {
Chris Lattner4c0a6d62007-05-08 05:38:01 +00003599 if (Record.size() < 8)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003600 return error("Invalid record");
Chris Lattner229907c2011-07-18 04:54:35 +00003601 Type *Ty = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00003602 if (!Ty)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003603 return error("Invalid record");
David Blaikie561a1572015-04-17 16:28:26 +00003604 if (auto *PTy = dyn_cast<PointerType>(Ty))
3605 Ty = PTy->getElementType();
3606 auto *FTy = dyn_cast<FunctionType>(Ty);
Chris Lattner1314b992007-04-22 06:23:29 +00003607 if (!FTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003608 return error("Invalid type for value");
Vedant Kumarad6d6e72015-10-27 21:17:06 +00003609 auto CC = static_cast<CallingConv::ID>(Record[1]);
3610 if (CC & ~CallingConv::MaxID)
3611 return error("Invalid calling convention ID");
Chris Lattner1314b992007-04-22 06:23:29 +00003612
Gabor Greife9ecc682008-04-06 20:25:17 +00003613 Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage,
3614 "", TheModule);
Chris Lattner1314b992007-04-22 06:23:29 +00003615
Vedant Kumarad6d6e72015-10-27 21:17:06 +00003616 Func->setCallingConv(CC);
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003617 bool isProto = Record[2];
Rafael Espindola12ca34f2015-01-19 15:16:06 +00003618 uint64_t RawLinkage = Record[3];
3619 Func->setLinkage(getDecodedLinkage(RawLinkage));
Devang Patel4c758ea2008-09-25 21:00:45 +00003620 Func->setAttributes(getAttributes(Record[4]));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003621
JF Bastien30bf96b2015-02-22 19:32:03 +00003622 unsigned Alignment;
3623 if (std::error_code EC = parseAlignmentValue(Record[5], Alignment))
3624 return EC;
3625 Func->setAlignment(Alignment);
Chris Lattner4c0a6d62007-05-08 05:38:01 +00003626 if (Record[6]) {
3627 if (Record[6]-1 >= SectionTable.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003628 return error("Invalid ID");
Chris Lattner4c0a6d62007-05-08 05:38:01 +00003629 Func->setSection(SectionTable[Record[6]-1]);
Chris Lattner1314b992007-04-22 06:23:29 +00003630 }
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00003631 // Local linkage must have default visibility.
3632 if (!Func->hasLocalLinkage())
3633 // FIXME: Change to an error if non-default in 4.0.
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003634 Func->setVisibility(getDecodedVisibility(Record[7]));
Gordon Henriksen71183b62007-12-10 03:18:06 +00003635 if (Record.size() > 8 && Record[8]) {
Filipe Cabecinhasf8a16a92015-04-30 04:09:41 +00003636 if (Record[8]-1 >= GCTable.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003637 return error("Invalid ID");
Gordon Henriksend930f912008-08-17 18:44:35 +00003638 Func->setGC(GCTable[Record[8]-1].c_str());
Gordon Henriksen71183b62007-12-10 03:18:06 +00003639 }
Rafael Espindola45e6c192011-01-08 16:42:36 +00003640 bool UnnamedAddr = false;
3641 if (Record.size() > 9)
3642 UnnamedAddr = Record[9];
3643 Func->setUnnamedAddr(UnnamedAddr);
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003644 if (Record.size() > 10 && Record[10] != 0)
Peter Collingbourne51d2de72014-12-03 02:08:38 +00003645 FunctionPrologues.push_back(std::make_pair(Func, Record[10]-1));
Nico Rieck7157bb72014-01-14 15:22:47 +00003646
3647 if (Record.size() > 11)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003648 Func->setDLLStorageClass(getDecodedDLLStorageClass(Record[11]));
Nico Rieck7157bb72014-01-14 15:22:47 +00003649 else
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003650 upgradeDLLImportExportLinkage(Func, RawLinkage);
Nico Rieck7157bb72014-01-14 15:22:47 +00003651
Rafael Espindola12ca34f2015-01-19 15:16:06 +00003652 if (Record.size() > 12) {
David Majnemerdad0a642014-06-27 18:19:56 +00003653 if (unsigned ComdatID = Record[12]) {
Filipe Cabecinhas0eb8a592015-05-26 23:00:56 +00003654 if (ComdatID > ComdatList.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003655 return error("Invalid function comdat ID");
David Majnemerdad0a642014-06-27 18:19:56 +00003656 Func->setComdat(ComdatList[ComdatID - 1]);
3657 }
Rafael Espindola12ca34f2015-01-19 15:16:06 +00003658 } else if (hasImplicitComdat(RawLinkage)) {
3659 Func->setComdat(reinterpret_cast<Comdat *>(1));
3660 }
David Majnemerdad0a642014-06-27 18:19:56 +00003661
Peter Collingbourne51d2de72014-12-03 02:08:38 +00003662 if (Record.size() > 13 && Record[13] != 0)
3663 FunctionPrefixes.push_back(std::make_pair(Func, Record[13]-1));
3664
David Majnemer7fddecc2015-06-17 20:52:32 +00003665 if (Record.size() > 14 && Record[14] != 0)
3666 FunctionPersonalityFns.push_back(std::make_pair(Func, Record[14] - 1));
3667
Chris Lattnerccaa4482007-04-23 21:26:05 +00003668 ValueList.push_back(Func);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003669
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003670 // If this is a function with a body, remember the prototype we are
3671 // creating now, so that we can match up the body with them later.
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003672 if (!isProto) {
Rafael Espindolad4bcefc2014-10-24 18:13:04 +00003673 Func->setIsMaterializable(true);
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003674 FunctionsWithBodies.push_back(Func);
Rafael Espindola1c863ca2015-06-22 18:06:15 +00003675 DeferredFunctionInfo[Func] = 0;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003676 }
Chris Lattner1314b992007-04-22 06:23:29 +00003677 break;
3678 }
David Blaikie6a51dbd2015-09-17 22:18:59 +00003679 // ALIAS: [alias type, addrspace, aliasee val#, linkage]
3680 // ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility, dllstorageclass]
3681 case bitc::MODULE_CODE_ALIAS:
3682 case bitc::MODULE_CODE_ALIAS_OLD: {
3683 bool NewRecord = BitCode == bitc::MODULE_CODE_ALIAS;
Aaron Ballman2d0f38c2015-09-18 13:31:42 +00003684 if (Record.size() < (3 + (unsigned)NewRecord))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003685 return error("Invalid record");
David Blaikie6a51dbd2015-09-17 22:18:59 +00003686 unsigned OpNum = 0;
3687 Type *Ty = getTypeByID(Record[OpNum++]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00003688 if (!Ty)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003689 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003690
David Blaikie6a51dbd2015-09-17 22:18:59 +00003691 unsigned AddrSpace;
3692 if (!NewRecord) {
3693 auto *PTy = dyn_cast<PointerType>(Ty);
3694 if (!PTy)
3695 return error("Invalid type for value");
3696 Ty = PTy->getElementType();
3697 AddrSpace = PTy->getAddressSpace();
3698 } else {
3699 AddrSpace = Record[OpNum++];
3700 }
3701
3702 auto Val = Record[OpNum++];
3703 auto Linkage = Record[OpNum++];
3704 auto *NewGA = GlobalAlias::create(
3705 Ty, AddrSpace, getDecodedLinkage(Linkage), "", TheModule);
Anton Korobeynikov2f22e3f2008-03-12 00:49:19 +00003706 // Old bitcode files didn't have visibility field.
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00003707 // Local linkage must have default visibility.
David Blaikie6a51dbd2015-09-17 22:18:59 +00003708 if (OpNum != Record.size()) {
3709 auto VisInd = OpNum++;
3710 if (!NewGA->hasLocalLinkage())
3711 // FIXME: Change to an error if non-default in 4.0.
3712 NewGA->setVisibility(getDecodedVisibility(Record[VisInd]));
3713 }
3714 if (OpNum != Record.size())
3715 NewGA->setDLLStorageClass(getDecodedDLLStorageClass(Record[OpNum++]));
Nico Rieck7157bb72014-01-14 15:22:47 +00003716 else
David Blaikie6a51dbd2015-09-17 22:18:59 +00003717 upgradeDLLImportExportLinkage(NewGA, Linkage);
3718 if (OpNum != Record.size())
3719 NewGA->setThreadLocalMode(getDecodedThreadLocalMode(Record[OpNum++]));
3720 if (OpNum != Record.size())
3721 NewGA->setUnnamedAddr(Record[OpNum++]);
Chris Lattner44c17072007-04-26 02:46:40 +00003722 ValueList.push_back(NewGA);
David Blaikie6a51dbd2015-09-17 22:18:59 +00003723 AliasInits.push_back(std::make_pair(NewGA, Val));
Chris Lattner44c17072007-04-26 02:46:40 +00003724 break;
Chris Lattner1314b992007-04-22 06:23:29 +00003725 }
Chris Lattner831d4202007-04-26 03:27:58 +00003726 /// MODULE_CODE_PURGEVALS: [numvals]
3727 case bitc::MODULE_CODE_PURGEVALS:
3728 // Trim down the value list to the specified size.
3729 if (Record.size() < 1 || Record[0] > ValueList.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003730 return error("Invalid record");
Chris Lattner831d4202007-04-26 03:27:58 +00003731 ValueList.shrinkTo(Record[0]);
3732 break;
Teresa Johnsonff642b92015-09-17 20:12:00 +00003733 /// MODULE_CODE_VSTOFFSET: [offset]
3734 case bitc::MODULE_CODE_VSTOFFSET:
3735 if (Record.size() < 1)
3736 return error("Invalid record");
3737 VSTOffset = Record[0];
3738 break;
Teresa Johnsone1164de2016-02-10 21:55:02 +00003739 /// MODULE_CODE_SOURCE_FILENAME: [namechar x N]
3740 case bitc::MODULE_CODE_SOURCE_FILENAME:
3741 SmallString<128> ValueName;
3742 if (convertToString(Record, 0, ValueName))
3743 return error("Invalid record");
3744 TheModule->setSourceFileName(ValueName);
3745 break;
Chris Lattner831d4202007-04-26 03:27:58 +00003746 }
Chris Lattner1314b992007-04-22 06:23:29 +00003747 Record.clear();
3748 }
Chris Lattner1314b992007-04-22 06:23:29 +00003749}
3750
Teresa Johnson403a7872015-10-04 14:33:43 +00003751/// Helper to read the header common to all bitcode files.
3752static bool hasValidBitcodeHeader(BitstreamCursor &Stream) {
3753 // Sniff for the signature.
3754 if (Stream.Read(8) != 'B' ||
3755 Stream.Read(8) != 'C' ||
3756 Stream.Read(4) != 0x0 ||
3757 Stream.Read(4) != 0xC ||
3758 Stream.Read(4) != 0xE ||
3759 Stream.Read(4) != 0xD)
3760 return false;
3761 return true;
3762}
3763
Rafael Espindola1aabf982015-06-16 23:29:49 +00003764std::error_code
3765BitcodeReader::parseBitcodeInto(std::unique_ptr<DataStreamer> Streamer,
3766 Module *M, bool ShouldLazyLoadMetadata) {
Rafael Espindolac6afe0d2015-06-16 20:03:39 +00003767 TheModule = M;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003768
Rafael Espindola1aabf982015-06-16 23:29:49 +00003769 if (std::error_code EC = initStream(std::move(Streamer)))
Rafael Espindola48da4f42013-11-04 16:16:24 +00003770 return EC;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003771
Chris Lattner1314b992007-04-22 06:23:29 +00003772 // Sniff for the signature.
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003773 if (!hasValidBitcodeHeader(Stream))
3774 return error("Invalid bitcode signature");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003775
Chris Lattner1314b992007-04-22 06:23:29 +00003776 // We expect a number of well-defined blocks, though we don't necessarily
3777 // need to understand them all.
Chris Lattner27d38752013-01-20 02:13:19 +00003778 while (1) {
Filipe Cabecinhas22554272015-04-14 14:07:15 +00003779 if (Stream.AtEndOfStream()) {
Filipe Cabecinhas22554272015-04-14 14:07:15 +00003780 // We didn't really read a proper Module.
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003781 return error("Malformed IR file");
Filipe Cabecinhas22554272015-04-14 14:07:15 +00003782 }
Joe Abbey97b7a172013-02-06 22:14:06 +00003783
Chris Lattner27d38752013-01-20 02:13:19 +00003784 BitstreamEntry Entry =
3785 Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
Joe Abbey97b7a172013-02-06 22:14:06 +00003786
Rafael Espindolac6afe0d2015-06-16 20:03:39 +00003787 if (Entry.Kind != BitstreamEntry::SubBlock)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003788 return error("Malformed block");
Joe Abbey97b7a172013-02-06 22:14:06 +00003789
Mehdi Amini5d303282015-10-26 18:37:00 +00003790 if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) {
3791 parseBitcodeVersion();
3792 continue;
3793 }
3794
Rafael Espindolac6afe0d2015-06-16 20:03:39 +00003795 if (Entry.ID == bitc::MODULE_BLOCK_ID)
Teresa Johnson1493ad92015-10-10 14:18:36 +00003796 return parseModule(0, ShouldLazyLoadMetadata);
Joe Abbey97b7a172013-02-06 22:14:06 +00003797
Rafael Espindolac6afe0d2015-06-16 20:03:39 +00003798 if (Stream.SkipBlock())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003799 return error("Invalid record");
Chris Lattner1314b992007-04-22 06:23:29 +00003800 }
Chris Lattner1314b992007-04-22 06:23:29 +00003801}
Chris Lattner6694f602007-04-29 07:54:31 +00003802
Rafael Espindolac75c4fa2014-07-04 20:02:42 +00003803ErrorOr<std::string> BitcodeReader::parseModuleTriple() {
Bill Wendling0198ce02010-10-06 01:22:42 +00003804 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003805 return error("Invalid record");
Bill Wendling0198ce02010-10-06 01:22:42 +00003806
3807 SmallVector<uint64_t, 64> Record;
3808
Rafael Espindolac75c4fa2014-07-04 20:02:42 +00003809 std::string Triple;
Bill Wendling0198ce02010-10-06 01:22:42 +00003810 // Read all the records for this module.
Chris Lattner27d38752013-01-20 02:13:19 +00003811 while (1) {
3812 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00003813
Chris Lattner27d38752013-01-20 02:13:19 +00003814 switch (Entry.Kind) {
3815 case BitstreamEntry::SubBlock: // Handled for us already.
3816 case BitstreamEntry::Error:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003817 return error("Malformed block");
Chris Lattner27d38752013-01-20 02:13:19 +00003818 case BitstreamEntry::EndBlock:
Rafael Espindolae6107792014-07-04 20:05:56 +00003819 return Triple;
Chris Lattner27d38752013-01-20 02:13:19 +00003820 case BitstreamEntry::Record:
3821 // The interesting case.
3822 break;
Bill Wendling0198ce02010-10-06 01:22:42 +00003823 }
3824
3825 // Read a record.
Chris Lattner27d38752013-01-20 02:13:19 +00003826 switch (Stream.readRecord(Entry.ID, Record)) {
Bill Wendling0198ce02010-10-06 01:22:42 +00003827 default: break; // Default behavior, ignore unknown content.
Bill Wendling0198ce02010-10-06 01:22:42 +00003828 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
Rafael Espindolac75c4fa2014-07-04 20:02:42 +00003829 std::string S;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003830 if (convertToString(Record, 0, S))
3831 return error("Invalid record");
Rafael Espindolac75c4fa2014-07-04 20:02:42 +00003832 Triple = S;
Bill Wendling0198ce02010-10-06 01:22:42 +00003833 break;
3834 }
3835 }
3836 Record.clear();
3837 }
Rafael Espindolae6107792014-07-04 20:05:56 +00003838 llvm_unreachable("Exit infinite loop");
Bill Wendling0198ce02010-10-06 01:22:42 +00003839}
3840
Rafael Espindolac75c4fa2014-07-04 20:02:42 +00003841ErrorOr<std::string> BitcodeReader::parseTriple() {
Rafael Espindola1aabf982015-06-16 23:29:49 +00003842 if (std::error_code EC = initStream(nullptr))
Rafael Espindola48da4f42013-11-04 16:16:24 +00003843 return EC;
Bill Wendling0198ce02010-10-06 01:22:42 +00003844
3845 // Sniff for the signature.
Teresa Johnsonf72278f2015-11-02 18:02:11 +00003846 if (!hasValidBitcodeHeader(Stream))
3847 return error("Invalid bitcode signature");
Bill Wendling0198ce02010-10-06 01:22:42 +00003848
3849 // We expect a number of well-defined blocks, though we don't necessarily
3850 // need to understand them all.
Chris Lattner27d38752013-01-20 02:13:19 +00003851 while (1) {
3852 BitstreamEntry Entry = Stream.advance();
Joe Abbey97b7a172013-02-06 22:14:06 +00003853
Chris Lattner27d38752013-01-20 02:13:19 +00003854 switch (Entry.Kind) {
3855 case BitstreamEntry::Error:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003856 return error("Malformed block");
Chris Lattner27d38752013-01-20 02:13:19 +00003857 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003858 return std::error_code();
Joe Abbey97b7a172013-02-06 22:14:06 +00003859
Chris Lattner27d38752013-01-20 02:13:19 +00003860 case BitstreamEntry::SubBlock:
3861 if (Entry.ID == bitc::MODULE_BLOCK_ID)
Rafael Espindolad346cc82014-07-04 13:52:01 +00003862 return parseModuleTriple();
Joe Abbey97b7a172013-02-06 22:14:06 +00003863
Chris Lattner27d38752013-01-20 02:13:19 +00003864 // Ignore other sub-blocks.
Rafael Espindola48da4f42013-11-04 16:16:24 +00003865 if (Stream.SkipBlock())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003866 return error("Malformed block");
Chris Lattner27d38752013-01-20 02:13:19 +00003867 continue;
Joe Abbey97b7a172013-02-06 22:14:06 +00003868
Chris Lattner27d38752013-01-20 02:13:19 +00003869 case BitstreamEntry::Record:
3870 Stream.skipRecord(Entry.ID);
3871 continue;
Bill Wendling0198ce02010-10-06 01:22:42 +00003872 }
3873 }
Bill Wendling0198ce02010-10-06 01:22:42 +00003874}
3875
Mehdi Amini3383ccc2015-11-09 02:46:41 +00003876ErrorOr<std::string> BitcodeReader::parseIdentificationBlock() {
3877 if (std::error_code EC = initStream(nullptr))
3878 return EC;
3879
3880 // Sniff for the signature.
3881 if (!hasValidBitcodeHeader(Stream))
3882 return error("Invalid bitcode signature");
3883
3884 // We expect a number of well-defined blocks, though we don't necessarily
3885 // need to understand them all.
3886 while (1) {
3887 BitstreamEntry Entry = Stream.advance();
3888 switch (Entry.Kind) {
3889 case BitstreamEntry::Error:
3890 return error("Malformed block");
3891 case BitstreamEntry::EndBlock:
3892 return std::error_code();
3893
3894 case BitstreamEntry::SubBlock:
3895 if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) {
3896 if (std::error_code EC = parseBitcodeVersion())
3897 return EC;
3898 return ProducerIdentification;
3899 }
3900 // Ignore other sub-blocks.
3901 if (Stream.SkipBlock())
3902 return error("Malformed block");
3903 continue;
3904 case BitstreamEntry::Record:
3905 Stream.skipRecord(Entry.ID);
3906 continue;
3907 }
3908 }
3909}
3910
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003911/// Parse metadata attachments.
3912std::error_code BitcodeReader::parseMetadataAttachment(Function &F) {
Devang Patelaf206b82009-09-18 19:26:43 +00003913 if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003914 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003915
Devang Patelaf206b82009-09-18 19:26:43 +00003916 SmallVector<uint64_t, 64> Record;
Chris Lattner27d38752013-01-20 02:13:19 +00003917 while (1) {
3918 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00003919
Chris Lattner27d38752013-01-20 02:13:19 +00003920 switch (Entry.Kind) {
3921 case BitstreamEntry::SubBlock: // Handled for us already.
3922 case BitstreamEntry::Error:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003923 return error("Malformed block");
Chris Lattner27d38752013-01-20 02:13:19 +00003924 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003925 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00003926 case BitstreamEntry::Record:
3927 // The interesting case.
Devang Patelaf206b82009-09-18 19:26:43 +00003928 break;
3929 }
Chris Lattner27d38752013-01-20 02:13:19 +00003930
Devang Patelaf206b82009-09-18 19:26:43 +00003931 // Read a metadata attachment record.
3932 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +00003933 switch (Stream.readRecord(Entry.ID, Record)) {
Devang Patelaf206b82009-09-18 19:26:43 +00003934 default: // Default behavior: ignore.
3935 break;
Chris Lattnerb8778552011-06-17 17:50:30 +00003936 case bitc::METADATA_ATTACHMENT: {
Devang Patelaf206b82009-09-18 19:26:43 +00003937 unsigned RecordLength = Record.size();
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00003938 if (Record.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003939 return error("Invalid record");
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00003940 if (RecordLength % 2 == 0) {
3941 // A function attachment.
3942 for (unsigned I = 0; I != RecordLength; I += 2) {
3943 auto K = MDKindMap.find(Record[I]);
3944 if (K == MDKindMap.end())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003945 return error("Invalid ID");
Justin Bognerae341c62016-03-17 20:12:06 +00003946 MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[I + 1]);
3947 if (!MD)
3948 return error("Invalid metadata attachment");
3949 F.setMetadata(K->second, MD);
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00003950 }
3951 continue;
3952 }
3953
3954 // An instruction attachment.
Devang Patelaf206b82009-09-18 19:26:43 +00003955 Instruction *Inst = InstructionList[Record[0]];
3956 for (unsigned i = 1; i != RecordLength; i = i+2) {
Devang Patelb1a44772009-09-28 21:14:55 +00003957 unsigned Kind = Record[i];
Dan Gohman43aa8f02010-07-20 21:42:28 +00003958 DenseMap<unsigned, unsigned>::iterator I =
3959 MDKindMap.find(Kind);
3960 if (I == MDKindMap.end())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00003961 return error("Invalid ID");
Justin Bognerae341c62016-03-17 20:12:06 +00003962 Metadata *Node = MetadataList.getMetadataFwdRef(Record[i + 1]);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00003963 if (isa<LocalAsMetadata>(Node))
Duncan P. N. Exon Smith35303fd2014-12-06 02:29:44 +00003964 // Drop the attachment. This used to be legal, but there's no
3965 // upgrade path.
3966 break;
Justin Bognerae341c62016-03-17 20:12:06 +00003967 MDNode *MD = dyn_cast_or_null<MDNode>(Node);
3968 if (!MD)
3969 return error("Invalid metadata attachment");
Duncan P. N. Exon Smithefe16c82016-03-25 00:56:13 +00003970
3971 if (HasSeenOldLoopTags && I->second == LLVMContext::MD_loop)
3972 MD = upgradeInstructionLoopAttachment(*MD);
3973
Justin Bognerae341c62016-03-17 20:12:06 +00003974 Inst->setMetadata(I->second, MD);
Duncan P. N. Exon Smithefe16c82016-03-25 00:56:13 +00003975 if (I->second == LLVMContext::MD_tbaa) {
Manman Ren209b17c2013-09-28 00:22:27 +00003976 InstsWithTBAATag.push_back(Inst);
Duncan P. N. Exon Smithefe16c82016-03-25 00:56:13 +00003977 continue;
3978 }
Devang Patelaf206b82009-09-18 19:26:43 +00003979 }
3980 break;
3981 }
3982 }
3983 }
Devang Patelaf206b82009-09-18 19:26:43 +00003984}
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003985
Rafael Espindola9d2bfc42015-12-14 23:17:03 +00003986static std::error_code typeCheckLoadStoreInst(Type *ValType, Type *PtrType) {
3987 LLVMContext &Context = PtrType->getContext();
Filipe Cabecinhas11bb8492015-05-18 21:48:55 +00003988 if (!isa<PointerType>(PtrType))
Rafael Espindola9d2bfc42015-12-14 23:17:03 +00003989 return error(Context, "Load/Store operand is not a pointer type");
Filipe Cabecinhas11bb8492015-05-18 21:48:55 +00003990 Type *ElemType = cast<PointerType>(PtrType)->getElementType();
3991
3992 if (ValType && ValType != ElemType)
Rafael Espindola9d2bfc42015-12-14 23:17:03 +00003993 return error(Context, "Explicit load/store type does not match pointee "
3994 "type of pointer operand");
Filipe Cabecinhas11bb8492015-05-18 21:48:55 +00003995 if (!PointerType::isLoadableOrStorableType(ElemType))
Rafael Espindola9d2bfc42015-12-14 23:17:03 +00003996 return error(Context, "Cannot load/store from pointer");
Filipe Cabecinhas11bb8492015-05-18 21:48:55 +00003997 return std::error_code();
3998}
3999
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004000/// Lazily parse the specified function body block.
4001std::error_code BitcodeReader::parseFunctionBody(Function *F) {
Chris Lattner982ec1e2007-05-05 00:17:00 +00004002 if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004003 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004004
Nick Lewyckya72e1af2010-02-25 08:30:17 +00004005 InstructionList.clear();
Chris Lattner85b7b402007-05-01 05:52:21 +00004006 unsigned ModuleValueListSize = ValueList.size();
Teresa Johnson61b406e2015-12-29 23:00:22 +00004007 unsigned ModuleMetadataListSize = MetadataList.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004008
Chris Lattner85b7b402007-05-01 05:52:21 +00004009 // Add all the function arguments to the value table.
Duncan P. N. Exon Smithfb1743a32015-10-13 16:48:55 +00004010 for (Argument &I : F->args())
4011 ValueList.push_back(&I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004012
Chris Lattner83930552007-05-01 07:01:57 +00004013 unsigned NextValueNo = ValueList.size();
Craig Topper2617dcc2014-04-15 06:32:26 +00004014 BasicBlock *CurBB = nullptr;
Chris Lattnere53603e2007-05-02 04:27:25 +00004015 unsigned CurBBNo = 0;
4016
Chris Lattner07d09ed2010-04-03 02:17:50 +00004017 DebugLoc LastLoc;
Duncan P. N. Exon Smith52d0f162015-01-09 02:51:45 +00004018 auto getLastInstruction = [&]() -> Instruction * {
4019 if (CurBB && !CurBB->empty())
4020 return &CurBB->back();
4021 else if (CurBBNo && FunctionBBs[CurBBNo - 1] &&
4022 !FunctionBBs[CurBBNo - 1]->empty())
4023 return &FunctionBBs[CurBBNo - 1]->back();
4024 return nullptr;
4025 };
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004026
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00004027 std::vector<OperandBundleDef> OperandBundles;
4028
Chris Lattner85b7b402007-05-01 05:52:21 +00004029 // Read all the records.
4030 SmallVector<uint64_t, 64> Record;
4031 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00004032 BitstreamEntry Entry = Stream.advance();
Joe Abbey97b7a172013-02-06 22:14:06 +00004033
Chris Lattner27d38752013-01-20 02:13:19 +00004034 switch (Entry.Kind) {
4035 case BitstreamEntry::Error:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004036 return error("Malformed block");
Chris Lattner27d38752013-01-20 02:13:19 +00004037 case BitstreamEntry::EndBlock:
4038 goto OutOfRecordLoop;
Joe Abbey97b7a172013-02-06 22:14:06 +00004039
Chris Lattner27d38752013-01-20 02:13:19 +00004040 case BitstreamEntry::SubBlock:
4041 switch (Entry.ID) {
Chris Lattner85b7b402007-05-01 05:52:21 +00004042 default: // Skip unknown content.
4043 if (Stream.SkipBlock())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004044 return error("Invalid record");
Chris Lattner85b7b402007-05-01 05:52:21 +00004045 break;
4046 case bitc::CONSTANTS_BLOCK_ID:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004047 if (std::error_code EC = parseConstants())
Rafael Espindola48da4f42013-11-04 16:16:24 +00004048 return EC;
Chris Lattner83930552007-05-01 07:01:57 +00004049 NextValueNo = ValueList.size();
Chris Lattner85b7b402007-05-01 05:52:21 +00004050 break;
4051 case bitc::VALUE_SYMTAB_BLOCK_ID:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004052 if (std::error_code EC = parseValueSymbolTable())
Rafael Espindola48da4f42013-11-04 16:16:24 +00004053 return EC;
Chris Lattner85b7b402007-05-01 05:52:21 +00004054 break;
Devang Patelaf206b82009-09-18 19:26:43 +00004055 case bitc::METADATA_ATTACHMENT_ID:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004056 if (std::error_code EC = parseMetadataAttachment(*F))
Rafael Espindola48da4f42013-11-04 16:16:24 +00004057 return EC;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004058 break;
Victor Hernandez108d3ac2010-01-13 19:34:08 +00004059 case bitc::METADATA_BLOCK_ID:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004060 if (std::error_code EC = parseMetadata())
Rafael Espindola48da4f42013-11-04 16:16:24 +00004061 return EC;
Victor Hernandez108d3ac2010-01-13 19:34:08 +00004062 break;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00004063 case bitc::USELIST_BLOCK_ID:
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004064 if (std::error_code EC = parseUseLists())
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00004065 return EC;
4066 break;
Chris Lattner85b7b402007-05-01 05:52:21 +00004067 }
4068 continue;
Joe Abbey97b7a172013-02-06 22:14:06 +00004069
Chris Lattner27d38752013-01-20 02:13:19 +00004070 case BitstreamEntry::Record:
4071 // The interesting case.
4072 break;
Chris Lattner85b7b402007-05-01 05:52:21 +00004073 }
Joe Abbey97b7a172013-02-06 22:14:06 +00004074
Chris Lattner85b7b402007-05-01 05:52:21 +00004075 // Read a record.
4076 Record.clear();
Craig Topper2617dcc2014-04-15 06:32:26 +00004077 Instruction *I = nullptr;
Chris Lattner27d38752013-01-20 02:13:19 +00004078 unsigned BitCode = Stream.readRecord(Entry.ID, Record);
Dan Gohman0ebd6962009-07-20 21:19:07 +00004079 switch (BitCode) {
Chris Lattner83930552007-05-01 07:01:57 +00004080 default: // Default behavior: reject
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004081 return error("Invalid value");
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00004082 case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks]
Chris Lattner83930552007-05-01 07:01:57 +00004083 if (Record.size() < 1 || Record[0] == 0)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004084 return error("Invalid record");
Chris Lattner85b7b402007-05-01 05:52:21 +00004085 // Create all the basic blocks for the function.
Chris Lattner6ce15cb2007-05-03 22:09:51 +00004086 FunctionBBs.resize(Record[0]);
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00004087
4088 // See if anything took the address of blocks in this function.
4089 auto BBFRI = BasicBlockFwdRefs.find(F);
4090 if (BBFRI == BasicBlockFwdRefs.end()) {
4091 for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i)
4092 FunctionBBs[i] = BasicBlock::Create(Context, "", F);
4093 } else {
4094 auto &BBRefs = BBFRI->second;
Duncan P. N. Exon Smith5a5fd7b2014-08-16 01:54:37 +00004095 // Check for invalid basic block references.
4096 if (BBRefs.size() > FunctionBBs.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004097 return error("Invalid ID");
Duncan P. N. Exon Smith5a5fd7b2014-08-16 01:54:37 +00004098 assert(!BBRefs.empty() && "Unexpected empty array");
4099 assert(!BBRefs.front() && "Invalid reference to entry block");
4100 for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E;
4101 ++I)
4102 if (I < RE && BBRefs[I]) {
4103 BBRefs[I]->insertInto(F);
4104 FunctionBBs[I] = BBRefs[I];
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00004105 } else {
4106 FunctionBBs[I] = BasicBlock::Create(Context, "", F);
4107 }
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00004108
4109 // Erase from the table.
4110 BasicBlockFwdRefs.erase(BBFRI);
4111 }
4112
Chris Lattner83930552007-05-01 07:01:57 +00004113 CurBB = FunctionBBs[0];
4114 continue;
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00004115 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004116
Chris Lattner07d09ed2010-04-03 02:17:50 +00004117 case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN
4118 // This record indicates that the last instruction is at the same
4119 // location as the previous instruction with a location.
Duncan P. N. Exon Smith52d0f162015-01-09 02:51:45 +00004120 I = getLastInstruction();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004121
Craig Topper2617dcc2014-04-15 06:32:26 +00004122 if (!I)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004123 return error("Invalid record");
Chris Lattner07d09ed2010-04-03 02:17:50 +00004124 I->setDebugLoc(LastLoc);
Craig Topper2617dcc2014-04-15 06:32:26 +00004125 I = nullptr;
Chris Lattner07d09ed2010-04-03 02:17:50 +00004126 continue;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004127
Duncan P. N. Exon Smith9ed19662015-01-09 17:53:27 +00004128 case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia]
Duncan P. N. Exon Smith52d0f162015-01-09 02:51:45 +00004129 I = getLastInstruction();
Craig Topper2617dcc2014-04-15 06:32:26 +00004130 if (!I || Record.size() < 4)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004131 return error("Invalid record");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004132
Chris Lattner07d09ed2010-04-03 02:17:50 +00004133 unsigned Line = Record[0], Col = Record[1];
4134 unsigned ScopeID = Record[2], IAID = Record[3];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004135
Craig Topper2617dcc2014-04-15 06:32:26 +00004136 MDNode *Scope = nullptr, *IA = nullptr;
Justin Bognerae341c62016-03-17 20:12:06 +00004137 if (ScopeID) {
4138 Scope = MetadataList.getMDNodeFwdRefOrNull(ScopeID - 1);
4139 if (!Scope)
4140 return error("Invalid record");
4141 }
4142 if (IAID) {
4143 IA = MetadataList.getMDNodeFwdRefOrNull(IAID - 1);
4144 if (!IA)
4145 return error("Invalid record");
4146 }
Chris Lattner07d09ed2010-04-03 02:17:50 +00004147 LastLoc = DebugLoc::get(Line, Col, Scope, IA);
4148 I->setDebugLoc(LastLoc);
Craig Topper2617dcc2014-04-15 06:32:26 +00004149 I = nullptr;
Chris Lattner07d09ed2010-04-03 02:17:50 +00004150 continue;
4151 }
4152
Chris Lattnere9759c22007-05-06 00:21:25 +00004153 case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode]
4154 unsigned OpNum = 0;
4155 Value *LHS, *RHS;
4156 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00004157 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
Dan Gohman0ebd6962009-07-20 21:19:07 +00004158 OpNum+1 > Record.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004159 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004160
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004161 int Opc = getDecodedBinaryOpcode(Record[OpNum++], LHS->getType());
Rafael Espindola48da4f42013-11-04 16:16:24 +00004162 if (Opc == -1)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004163 return error("Invalid record");
Gabor Greife1f6e4b2008-05-16 19:29:10 +00004164 I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
Devang Patelaf206b82009-09-18 19:26:43 +00004165 InstructionList.push_back(I);
Dan Gohman1b849082009-09-07 23:54:19 +00004166 if (OpNum < Record.size()) {
4167 if (Opc == Instruction::Add ||
4168 Opc == Instruction::Sub ||
Chris Lattnera676c0f2011-02-07 16:40:21 +00004169 Opc == Instruction::Mul ||
4170 Opc == Instruction::Shl) {
Dan Gohman00f47472010-01-25 21:55:39 +00004171 if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP))
Dan Gohman1b849082009-09-07 23:54:19 +00004172 cast<BinaryOperator>(I)->setHasNoSignedWrap(true);
Dan Gohman00f47472010-01-25 21:55:39 +00004173 if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
Dan Gohman1b849082009-09-07 23:54:19 +00004174 cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true);
Chris Lattner35315d02011-02-06 21:44:57 +00004175 } else if (Opc == Instruction::SDiv ||
Chris Lattnera676c0f2011-02-07 16:40:21 +00004176 Opc == Instruction::UDiv ||
4177 Opc == Instruction::LShr ||
4178 Opc == Instruction::AShr) {
Chris Lattner35315d02011-02-06 21:44:57 +00004179 if (Record[OpNum] & (1 << bitc::PEO_EXACT))
Dan Gohman1b849082009-09-07 23:54:19 +00004180 cast<BinaryOperator>(I)->setIsExact(true);
Michael Ilseman9978d7e2012-11-27 00:43:38 +00004181 } else if (isa<FPMathOperator>(I)) {
James Molloy88eb5352015-07-10 12:52:00 +00004182 FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]);
Michael Ilseman9978d7e2012-11-27 00:43:38 +00004183 if (FMF.any())
4184 I->setFastMathFlags(FMF);
Dan Gohman1b849082009-09-07 23:54:19 +00004185 }
Michael Ilseman9978d7e2012-11-27 00:43:38 +00004186
Dan Gohman1b849082009-09-07 23:54:19 +00004187 }
Chris Lattner85b7b402007-05-01 05:52:21 +00004188 break;
4189 }
Chris Lattnere9759c22007-05-06 00:21:25 +00004190 case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc]
4191 unsigned OpNum = 0;
4192 Value *Op;
4193 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
4194 OpNum+2 != Record.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004195 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004196
Chris Lattner229907c2011-07-18 04:54:35 +00004197 Type *ResTy = getTypeByID(Record[OpNum]);
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004198 int Opc = getDecodedCastOpcode(Record[OpNum + 1]);
Craig Topper2617dcc2014-04-15 06:32:26 +00004199 if (Opc == -1 || !ResTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004200 return error("Invalid record");
Craig Topper2617dcc2014-04-15 06:32:26 +00004201 Instruction *Temp = nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00004202 if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) {
4203 if (Temp) {
4204 InstructionList.push_back(Temp);
4205 CurBB->getInstList().push_back(Temp);
4206 }
4207 } else {
Filipe Cabecinhasb70fd872015-10-06 12:37:54 +00004208 auto CastOp = (Instruction::CastOps)Opc;
4209 if (!CastInst::castIsValid(CastOp, Op, ResTy))
4210 return error("Invalid cast");
4211 I = CastInst::Create(CastOp, Op, ResTy);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00004212 }
Devang Patelaf206b82009-09-18 19:26:43 +00004213 InstructionList.push_back(I);
Chris Lattnere53603e2007-05-02 04:27:25 +00004214 break;
4215 }
David Blaikieb5b5efd2015-02-25 01:08:52 +00004216 case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD:
4217 case bitc::FUNC_CODE_INST_GEP_OLD:
4218 case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands]
Chris Lattnerdf1233d2007-05-06 00:00:00 +00004219 unsigned OpNum = 0;
David Blaikieb5b5efd2015-02-25 01:08:52 +00004220
4221 Type *Ty;
4222 bool InBounds;
4223
4224 if (BitCode == bitc::FUNC_CODE_INST_GEP) {
4225 InBounds = Record[OpNum++];
4226 Ty = getTypeByID(Record[OpNum++]);
4227 } else {
4228 InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD;
4229 Ty = nullptr;
4230 }
4231
Chris Lattnerdf1233d2007-05-06 00:00:00 +00004232 Value *BasePtr;
4233 if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004234 return error("Invalid record");
Chris Lattner1fc27f02007-05-02 05:16:49 +00004235
David Blaikie60310f22015-05-08 00:42:26 +00004236 if (!Ty)
4237 Ty = cast<SequentialType>(BasePtr->getType()->getScalarType())
4238 ->getElementType();
4239 else if (Ty !=
4240 cast<SequentialType>(BasePtr->getType()->getScalarType())
4241 ->getElementType())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004242 return error(
David Blaikie675e8cb2015-03-16 21:35:48 +00004243 "Explicit gep type does not match pointee type of pointer operand");
4244
Chris Lattner5285b5e2007-05-02 05:46:45 +00004245 SmallVector<Value*, 16> GEPIdx;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00004246 while (OpNum != Record.size()) {
4247 Value *Op;
4248 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004249 return error("Invalid record");
Chris Lattnerdf1233d2007-05-06 00:00:00 +00004250 GEPIdx.push_back(Op);
Chris Lattner1fc27f02007-05-02 05:16:49 +00004251 }
4252
David Blaikie096b1da2015-03-14 19:53:33 +00004253 I = GetElementPtrInst::Create(Ty, BasePtr, GEPIdx);
David Blaikie675e8cb2015-03-16 21:35:48 +00004254
Devang Patelaf206b82009-09-18 19:26:43 +00004255 InstructionList.push_back(I);
David Blaikieb5b5efd2015-02-25 01:08:52 +00004256 if (InBounds)
Dan Gohman1b849082009-09-07 23:54:19 +00004257 cast<GetElementPtrInst>(I)->setIsInBounds(true);
Chris Lattner1fc27f02007-05-02 05:16:49 +00004258 break;
4259 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004260
Dan Gohman1ecaf452008-05-31 00:58:22 +00004261 case bitc::FUNC_CODE_INST_EXTRACTVAL: {
4262 // EXTRACTVAL: [opty, opval, n x indices]
Dan Gohman30499842008-05-23 01:55:30 +00004263 unsigned OpNum = 0;
4264 Value *Agg;
4265 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004266 return error("Invalid record");
Dan Gohman30499842008-05-23 01:55:30 +00004267
Filipe Cabecinhas1c299d02015-05-16 00:33:12 +00004268 unsigned RecSize = Record.size();
4269 if (OpNum == RecSize)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004270 return error("EXTRACTVAL: Invalid instruction with 0 indices");
Filipe Cabecinhas1c299d02015-05-16 00:33:12 +00004271
Dan Gohman1ecaf452008-05-31 00:58:22 +00004272 SmallVector<unsigned, 4> EXTRACTVALIdx;
Filipe Cabecinhasecf8f7f2015-02-16 00:03:11 +00004273 Type *CurTy = Agg->getType();
Filipe Cabecinhas1c299d02015-05-16 00:33:12 +00004274 for (; OpNum != RecSize; ++OpNum) {
Filipe Cabecinhasecf8f7f2015-02-16 00:03:11 +00004275 bool IsArray = CurTy->isArrayTy();
4276 bool IsStruct = CurTy->isStructTy();
Dan Gohman1ecaf452008-05-31 00:58:22 +00004277 uint64_t Index = Record[OpNum];
Filipe Cabecinhasecf8f7f2015-02-16 00:03:11 +00004278
4279 if (!IsStruct && !IsArray)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004280 return error("EXTRACTVAL: Invalid type");
Dan Gohman1ecaf452008-05-31 00:58:22 +00004281 if ((unsigned)Index != Index)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004282 return error("Invalid value");
Filipe Cabecinhasecf8f7f2015-02-16 00:03:11 +00004283 if (IsStruct && Index >= CurTy->subtypes().size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004284 return error("EXTRACTVAL: Invalid struct index");
Filipe Cabecinhasecf8f7f2015-02-16 00:03:11 +00004285 if (IsArray && Index >= CurTy->getArrayNumElements())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004286 return error("EXTRACTVAL: Invalid array index");
Dan Gohman1ecaf452008-05-31 00:58:22 +00004287 EXTRACTVALIdx.push_back((unsigned)Index);
Filipe Cabecinhasecf8f7f2015-02-16 00:03:11 +00004288
4289 if (IsStruct)
4290 CurTy = CurTy->subtypes()[Index];
4291 else
4292 CurTy = CurTy->subtypes()[0];
Dan Gohman30499842008-05-23 01:55:30 +00004293 }
4294
Jay Foad57aa6362011-07-13 10:26:04 +00004295 I = ExtractValueInst::Create(Agg, EXTRACTVALIdx);
Devang Patelaf206b82009-09-18 19:26:43 +00004296 InstructionList.push_back(I);
Dan Gohman30499842008-05-23 01:55:30 +00004297 break;
4298 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004299
Dan Gohman1ecaf452008-05-31 00:58:22 +00004300 case bitc::FUNC_CODE_INST_INSERTVAL: {
4301 // INSERTVAL: [opty, opval, opty, opval, n x indices]
Dan Gohman30499842008-05-23 01:55:30 +00004302 unsigned OpNum = 0;
4303 Value *Agg;
4304 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004305 return error("Invalid record");
Dan Gohman30499842008-05-23 01:55:30 +00004306 Value *Val;
4307 if (getValueTypePair(Record, OpNum, NextValueNo, Val))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004308 return error("Invalid record");
Dan Gohman30499842008-05-23 01:55:30 +00004309
Filipe Cabecinhas1c299d02015-05-16 00:33:12 +00004310 unsigned RecSize = Record.size();
4311 if (OpNum == RecSize)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004312 return error("INSERTVAL: Invalid instruction with 0 indices");
Filipe Cabecinhas1c299d02015-05-16 00:33:12 +00004313
Dan Gohman1ecaf452008-05-31 00:58:22 +00004314 SmallVector<unsigned, 4> INSERTVALIdx;
Filipe Cabecinhasecf8f7f2015-02-16 00:03:11 +00004315 Type *CurTy = Agg->getType();
Filipe Cabecinhas1c299d02015-05-16 00:33:12 +00004316 for (; OpNum != RecSize; ++OpNum) {
Filipe Cabecinhasecf8f7f2015-02-16 00:03:11 +00004317 bool IsArray = CurTy->isArrayTy();
4318 bool IsStruct = CurTy->isStructTy();
Dan Gohman1ecaf452008-05-31 00:58:22 +00004319 uint64_t Index = Record[OpNum];
Filipe Cabecinhasecf8f7f2015-02-16 00:03:11 +00004320
4321 if (!IsStruct && !IsArray)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004322 return error("INSERTVAL: Invalid type");
Dan Gohman1ecaf452008-05-31 00:58:22 +00004323 if ((unsigned)Index != Index)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004324 return error("Invalid value");
Filipe Cabecinhasecf8f7f2015-02-16 00:03:11 +00004325 if (IsStruct && Index >= CurTy->subtypes().size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004326 return error("INSERTVAL: Invalid struct index");
Filipe Cabecinhasecf8f7f2015-02-16 00:03:11 +00004327 if (IsArray && Index >= CurTy->getArrayNumElements())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004328 return error("INSERTVAL: Invalid array index");
Filipe Cabecinhasecf8f7f2015-02-16 00:03:11 +00004329
Dan Gohman1ecaf452008-05-31 00:58:22 +00004330 INSERTVALIdx.push_back((unsigned)Index);
Filipe Cabecinhasecf8f7f2015-02-16 00:03:11 +00004331 if (IsStruct)
4332 CurTy = CurTy->subtypes()[Index];
4333 else
4334 CurTy = CurTy->subtypes()[0];
Dan Gohman30499842008-05-23 01:55:30 +00004335 }
4336
Filipe Cabecinhas4708a022015-05-18 22:27:11 +00004337 if (CurTy != Val->getType())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004338 return error("Inserted value type doesn't match aggregate type");
Filipe Cabecinhas4708a022015-05-18 22:27:11 +00004339
Jay Foad57aa6362011-07-13 10:26:04 +00004340 I = InsertValueInst::Create(Agg, Val, INSERTVALIdx);
Devang Patelaf206b82009-09-18 19:26:43 +00004341 InstructionList.push_back(I);
Dan Gohman30499842008-05-23 01:55:30 +00004342 break;
4343 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004344
Chris Lattnere9759c22007-05-06 00:21:25 +00004345 case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval]
Dan Gohmanc5d28922008-09-16 01:01:33 +00004346 // obsolete form of select
4347 // handles select i1 ... in old bitcode
Chris Lattnere9759c22007-05-06 00:21:25 +00004348 unsigned OpNum = 0;
4349 Value *TrueVal, *FalseVal, *Cond;
4350 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00004351 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
4352 popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004353 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004354
Dan Gohmanc5d28922008-09-16 01:01:33 +00004355 I = SelectInst::Create(Cond, TrueVal, FalseVal);
Devang Patelaf206b82009-09-18 19:26:43 +00004356 InstructionList.push_back(I);
Dan Gohmanc5d28922008-09-16 01:01:33 +00004357 break;
4358 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004359
Dan Gohmanc5d28922008-09-16 01:01:33 +00004360 case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred]
4361 // new form of select
4362 // handles select i1 or select [N x i1]
4363 unsigned OpNum = 0;
4364 Value *TrueVal, *FalseVal, *Cond;
4365 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00004366 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
Dan Gohmanc5d28922008-09-16 01:01:33 +00004367 getValueTypePair(Record, OpNum, NextValueNo, Cond))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004368 return error("Invalid record");
Dan Gohmanc579d972008-09-09 01:02:47 +00004369
4370 // select condition can be either i1 or [N x i1]
Chris Lattner229907c2011-07-18 04:54:35 +00004371 if (VectorType* vector_type =
4372 dyn_cast<VectorType>(Cond->getType())) {
Dan Gohmanc579d972008-09-09 01:02:47 +00004373 // expect <n x i1>
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004374 if (vector_type->getElementType() != Type::getInt1Ty(Context))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004375 return error("Invalid type for value");
Dan Gohmanc579d972008-09-09 01:02:47 +00004376 } else {
4377 // expect i1
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004378 if (Cond->getType() != Type::getInt1Ty(Context))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004379 return error("Invalid type for value");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004380 }
4381
Gabor Greife9ecc682008-04-06 20:25:17 +00004382 I = SelectInst::Create(Cond, TrueVal, FalseVal);
Devang Patelaf206b82009-09-18 19:26:43 +00004383 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00004384 break;
4385 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004386
Chris Lattner1fc27f02007-05-02 05:16:49 +00004387 case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval]
Chris Lattnere9759c22007-05-06 00:21:25 +00004388 unsigned OpNum = 0;
4389 Value *Vec, *Idx;
4390 if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00004391 getValueTypePair(Record, OpNum, NextValueNo, Idx))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004392 return error("Invalid record");
Filipe Cabecinhasff1e2342015-04-24 11:30:15 +00004393 if (!Vec->getType()->isVectorTy())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004394 return error("Invalid type for value");
Eric Christopherc9742252009-07-25 02:28:41 +00004395 I = ExtractElementInst::Create(Vec, Idx);
Devang Patelaf206b82009-09-18 19:26:43 +00004396 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00004397 break;
4398 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004399
Chris Lattner1fc27f02007-05-02 05:16:49 +00004400 case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval]
Chris Lattnere9759c22007-05-06 00:21:25 +00004401 unsigned OpNum = 0;
4402 Value *Vec, *Elt, *Idx;
Filipe Cabecinhasff1e2342015-04-24 11:30:15 +00004403 if (getValueTypePair(Record, OpNum, NextValueNo, Vec))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004404 return error("Invalid record");
Filipe Cabecinhasff1e2342015-04-24 11:30:15 +00004405 if (!Vec->getType()->isVectorTy())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004406 return error("Invalid type for value");
Filipe Cabecinhasff1e2342015-04-24 11:30:15 +00004407 if (popValue(Record, OpNum, NextValueNo,
Chris Lattnere9759c22007-05-06 00:21:25 +00004408 cast<VectorType>(Vec->getType())->getElementType(), Elt) ||
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00004409 getValueTypePair(Record, OpNum, NextValueNo, Idx))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004410 return error("Invalid record");
Gabor Greife9ecc682008-04-06 20:25:17 +00004411 I = InsertElementInst::Create(Vec, Elt, Idx);
Devang Patelaf206b82009-09-18 19:26:43 +00004412 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00004413 break;
4414 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004415
Chris Lattnere9759c22007-05-06 00:21:25 +00004416 case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval]
4417 unsigned OpNum = 0;
4418 Value *Vec1, *Vec2, *Mask;
4419 if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00004420 popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004421 return error("Invalid record");
Chris Lattnere9759c22007-05-06 00:21:25 +00004422
Mon P Wang25f01062008-11-10 04:46:22 +00004423 if (getValueTypePair(Record, OpNum, NextValueNo, Mask))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004424 return error("Invalid record");
Filipe Cabecinhasff1e2342015-04-24 11:30:15 +00004425 if (!Vec1->getType()->isVectorTy() || !Vec2->getType()->isVectorTy())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004426 return error("Invalid type for value");
Chris Lattner1fc27f02007-05-02 05:16:49 +00004427 I = new ShuffleVectorInst(Vec1, Vec2, Mask);
Devang Patelaf206b82009-09-18 19:26:43 +00004428 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00004429 break;
4430 }
Mon P Wang25f01062008-11-10 04:46:22 +00004431
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004432 case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred]
4433 // Old form of ICmp/FCmp returning bool
4434 // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were
4435 // both legal on vectors but had different behaviour.
4436 case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred]
4437 // FCmp/ICmp returning bool or vector of bool
4438
Chris Lattnerdf1233d2007-05-06 00:00:00 +00004439 unsigned OpNum = 0;
4440 Value *LHS, *RHS;
4441 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
James Molloy88eb5352015-07-10 12:52:00 +00004442 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS))
4443 return error("Invalid record");
4444
4445 unsigned PredVal = Record[OpNum];
4446 bool IsFP = LHS->getType()->isFPOrFPVectorTy();
4447 FastMathFlags FMF;
4448 if (IsFP && Record.size() > OpNum+1)
4449 FMF = getDecodedFastMathFlags(Record[++OpNum]);
4450
4451 if (OpNum+1 != Record.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004452 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004453
Duncan Sands9dff9be2010-02-15 16:12:20 +00004454 if (LHS->getType()->isFPOrFPVectorTy())
James Molloy88eb5352015-07-10 12:52:00 +00004455 I = new FCmpInst((FCmpInst::Predicate)PredVal, LHS, RHS);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004456 else
James Molloy88eb5352015-07-10 12:52:00 +00004457 I = new ICmpInst((ICmpInst::Predicate)PredVal, LHS, RHS);
4458
4459 if (FMF.any())
4460 I->setFastMathFlags(FMF);
Devang Patelaf206b82009-09-18 19:26:43 +00004461 InstructionList.push_back(I);
Dan Gohmanc579d972008-09-09 01:02:47 +00004462 break;
4463 }
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004464
Chris Lattnere53603e2007-05-02 04:27:25 +00004465 case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>]
Devang Patelbbfd8742008-02-26 01:29:32 +00004466 {
4467 unsigned Size = Record.size();
4468 if (Size == 0) {
Owen Anderson55f1c092009-08-13 21:58:54 +00004469 I = ReturnInst::Create(Context);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004470 InstructionList.push_back(I);
Devang Patelbbfd8742008-02-26 01:29:32 +00004471 break;
Dan Gohmanfa1211f2008-07-23 00:34:11 +00004472 }
Devang Patelbbfd8742008-02-26 01:29:32 +00004473
Dan Gohmanfa1211f2008-07-23 00:34:11 +00004474 unsigned OpNum = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +00004475 Value *Op = nullptr;
Chris Lattnerf1c87102011-06-17 18:09:11 +00004476 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004477 return error("Invalid record");
Chris Lattnerf1c87102011-06-17 18:09:11 +00004478 if (OpNum != Record.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004479 return error("Invalid record");
Dan Gohmanfa1211f2008-07-23 00:34:11 +00004480
Chris Lattnerf1c87102011-06-17 18:09:11 +00004481 I = ReturnInst::Create(Context, Op);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004482 InstructionList.push_back(I);
Dan Gohmanfa1211f2008-07-23 00:34:11 +00004483 break;
Chris Lattnere53603e2007-05-02 04:27:25 +00004484 }
Chris Lattner5285b5e2007-05-02 05:46:45 +00004485 case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#]
Chris Lattner6ce15cb2007-05-03 22:09:51 +00004486 if (Record.size() != 1 && Record.size() != 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004487 return error("Invalid record");
Chris Lattner5285b5e2007-05-02 05:46:45 +00004488 BasicBlock *TrueDest = getBasicBlock(Record[0]);
Craig Topper2617dcc2014-04-15 06:32:26 +00004489 if (!TrueDest)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004490 return error("Invalid record");
Chris Lattner5285b5e2007-05-02 05:46:45 +00004491
Devang Patelaf206b82009-09-18 19:26:43 +00004492 if (Record.size() == 1) {
Gabor Greife9ecc682008-04-06 20:25:17 +00004493 I = BranchInst::Create(TrueDest);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004494 InstructionList.push_back(I);
Devang Patelaf206b82009-09-18 19:26:43 +00004495 }
Chris Lattner5285b5e2007-05-02 05:46:45 +00004496 else {
4497 BasicBlock *FalseDest = getBasicBlock(Record[1]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00004498 Value *Cond = getValue(Record, 2, NextValueNo,
4499 Type::getInt1Ty(Context));
Craig Topper2617dcc2014-04-15 06:32:26 +00004500 if (!FalseDest || !Cond)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004501 return error("Invalid record");
Gabor Greife9ecc682008-04-06 20:25:17 +00004502 I = BranchInst::Create(TrueDest, FalseDest, Cond);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004503 InstructionList.push_back(I);
Chris Lattner5285b5e2007-05-02 05:46:45 +00004504 }
4505 break;
4506 }
David Majnemerb01aa9f2015-08-23 19:22:31 +00004507 case bitc::FUNC_CODE_INST_CLEANUPRET: { // CLEANUPRET: [val] or [val,bb#]
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00004508 if (Record.size() != 1 && Record.size() != 2)
David Majnemer654e1302015-07-31 17:58:14 +00004509 return error("Invalid record");
4510 unsigned Idx = 0;
David Majnemer8a1c45d2015-12-12 05:38:55 +00004511 Value *CleanupPad =
4512 getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00004513 if (!CleanupPad)
David Majnemer654e1302015-07-31 17:58:14 +00004514 return error("Invalid record");
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00004515 BasicBlock *UnwindDest = nullptr;
4516 if (Record.size() == 2) {
David Majnemer654e1302015-07-31 17:58:14 +00004517 UnwindDest = getBasicBlock(Record[Idx++]);
4518 if (!UnwindDest)
4519 return error("Invalid record");
4520 }
4521
David Majnemer8a1c45d2015-12-12 05:38:55 +00004522 I = CleanupReturnInst::Create(CleanupPad, UnwindDest);
David Majnemer654e1302015-07-31 17:58:14 +00004523 InstructionList.push_back(I);
4524 break;
4525 }
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00004526 case bitc::FUNC_CODE_INST_CATCHRET: { // CATCHRET: [val,bb#]
4527 if (Record.size() != 2)
David Majnemer654e1302015-07-31 17:58:14 +00004528 return error("Invalid record");
David Majnemer0bc0eef2015-08-15 02:46:08 +00004529 unsigned Idx = 0;
David Majnemer8a1c45d2015-12-12 05:38:55 +00004530 Value *CatchPad =
4531 getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00004532 if (!CatchPad)
4533 return error("Invalid record");
David Majnemer0bc0eef2015-08-15 02:46:08 +00004534 BasicBlock *BB = getBasicBlock(Record[Idx++]);
David Majnemer654e1302015-07-31 17:58:14 +00004535 if (!BB)
4536 return error("Invalid record");
David Majnemer0bc0eef2015-08-15 02:46:08 +00004537
David Majnemer8a1c45d2015-12-12 05:38:55 +00004538 I = CatchReturnInst::Create(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +00004539 InstructionList.push_back(I);
4540 break;
4541 }
David Majnemer8a1c45d2015-12-12 05:38:55 +00004542 case bitc::FUNC_CODE_INST_CATCHSWITCH: { // CATCHSWITCH: [tok,num,(bb)*,bb?]
4543 // We must have, at minimum, the outer scope and the number of arguments.
4544 if (Record.size() < 2)
David Majnemer654e1302015-07-31 17:58:14 +00004545 return error("Invalid record");
4546
David Majnemer654e1302015-07-31 17:58:14 +00004547 unsigned Idx = 0;
David Majnemer8a1c45d2015-12-12 05:38:55 +00004548
4549 Value *ParentPad =
4550 getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
4551
4552 unsigned NumHandlers = Record[Idx++];
4553
4554 SmallVector<BasicBlock *, 2> Handlers;
4555 for (unsigned Op = 0; Op != NumHandlers; ++Op) {
4556 BasicBlock *BB = getBasicBlock(Record[Idx++]);
4557 if (!BB)
David Majnemer654e1302015-07-31 17:58:14 +00004558 return error("Invalid record");
David Majnemer8a1c45d2015-12-12 05:38:55 +00004559 Handlers.push_back(BB);
4560 }
4561
4562 BasicBlock *UnwindDest = nullptr;
4563 if (Idx + 1 == Record.size()) {
David Majnemer654e1302015-07-31 17:58:14 +00004564 UnwindDest = getBasicBlock(Record[Idx++]);
4565 if (!UnwindDest)
4566 return error("Invalid record");
4567 }
David Majnemer8a1c45d2015-12-12 05:38:55 +00004568
4569 if (Record.size() != Idx)
4570 return error("Invalid record");
4571
4572 auto *CatchSwitch =
4573 CatchSwitchInst::Create(ParentPad, UnwindDest, NumHandlers);
4574 for (BasicBlock *Handler : Handlers)
4575 CatchSwitch->addHandler(Handler);
4576 I = CatchSwitch;
4577 InstructionList.push_back(I);
4578 break;
4579 }
David Majnemer8a1c45d2015-12-12 05:38:55 +00004580 case bitc::FUNC_CODE_INST_CATCHPAD:
4581 case bitc::FUNC_CODE_INST_CLEANUPPAD: { // [tok,num,(ty,val)*]
4582 // We must have, at minimum, the outer scope and the number of arguments.
4583 if (Record.size() < 2)
David Majnemer654e1302015-07-31 17:58:14 +00004584 return error("Invalid record");
David Majnemer8a1c45d2015-12-12 05:38:55 +00004585
David Majnemer654e1302015-07-31 17:58:14 +00004586 unsigned Idx = 0;
David Majnemer8a1c45d2015-12-12 05:38:55 +00004587
4588 Value *ParentPad =
4589 getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
4590
David Majnemer654e1302015-07-31 17:58:14 +00004591 unsigned NumArgOperands = Record[Idx++];
David Majnemer8a1c45d2015-12-12 05:38:55 +00004592
David Majnemer654e1302015-07-31 17:58:14 +00004593 SmallVector<Value *, 2> Args;
4594 for (unsigned Op = 0; Op != NumArgOperands; ++Op) {
4595 Value *Val;
4596 if (getValueTypePair(Record, Idx, NextValueNo, Val))
4597 return error("Invalid record");
4598 Args.push_back(Val);
4599 }
David Majnemer8a1c45d2015-12-12 05:38:55 +00004600
David Majnemer654e1302015-07-31 17:58:14 +00004601 if (Record.size() != Idx)
4602 return error("Invalid record");
4603
David Majnemer8a1c45d2015-12-12 05:38:55 +00004604 if (BitCode == bitc::FUNC_CODE_INST_CLEANUPPAD)
4605 I = CleanupPadInst::Create(ParentPad, Args);
4606 else
4607 I = CatchPadInst::Create(ParentPad, Args);
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +00004608 InstructionList.push_back(I);
4609 break;
4610 }
Chris Lattner3ed871f2009-10-27 19:13:16 +00004611 case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...]
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004612 // Check magic
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004613 if ((Record[0] >> 16) == SWITCH_INST_MAGIC) {
Bob Wilsone4077362013-09-09 19:14:35 +00004614 // "New" SwitchInst format with case ranges. The changes to write this
4615 // format were reverted but we still recognize bitcode that uses it.
4616 // Hopefully someday we will have support for case ranges and can use
4617 // this format again.
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004618
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004619 Type *OpTy = getTypeByID(Record[1]);
4620 unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth();
4621
Jan Wen Voungafaced02012-10-11 20:20:40 +00004622 Value *Cond = getValue(Record, 2, NextValueNo, OpTy);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004623 BasicBlock *Default = getBasicBlock(Record[3]);
Craig Topper2617dcc2014-04-15 06:32:26 +00004624 if (!OpTy || !Cond || !Default)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004625 return error("Invalid record");
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004626
4627 unsigned NumCases = Record[4];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004628
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004629 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
4630 InstructionList.push_back(SI);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004631
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004632 unsigned CurIdx = 5;
4633 for (unsigned i = 0; i != NumCases; ++i) {
Bob Wilsone4077362013-09-09 19:14:35 +00004634 SmallVector<ConstantInt*, 1> CaseVals;
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004635 unsigned NumItems = Record[CurIdx++];
4636 for (unsigned ci = 0; ci != NumItems; ++ci) {
4637 bool isSingleNumber = Record[CurIdx++];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004638
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004639 APInt Low;
4640 unsigned ActiveWords = 1;
4641 if (ValueBitWidth > 64)
4642 ActiveWords = Record[CurIdx++];
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004643 Low = readWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
Benjamin Kramer9704ed02012-05-28 14:10:31 +00004644 ValueBitWidth);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004645 CurIdx += ActiveWords;
Stepan Dyatkovskiye3e19cb2012-05-28 12:39:09 +00004646
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004647 if (!isSingleNumber) {
4648 ActiveWords = 1;
4649 if (ValueBitWidth > 64)
4650 ActiveWords = Record[CurIdx++];
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004651 APInt High = readWideAPInt(
4652 makeArrayRef(&Record[CurIdx], ActiveWords), ValueBitWidth);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004653 CurIdx += ActiveWords;
Bob Wilsone4077362013-09-09 19:14:35 +00004654
4655 // FIXME: It is not clear whether values in the range should be
4656 // compared as signed or unsigned values. The partially
4657 // implemented changes that used this format in the past used
4658 // unsigned comparisons.
4659 for ( ; Low.ule(High); ++Low)
4660 CaseVals.push_back(ConstantInt::get(Context, Low));
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004661 } else
Bob Wilsone4077362013-09-09 19:14:35 +00004662 CaseVals.push_back(ConstantInt::get(Context, Low));
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004663 }
4664 BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]);
Bob Wilsone4077362013-09-09 19:14:35 +00004665 for (SmallVector<ConstantInt*, 1>::iterator cvi = CaseVals.begin(),
4666 cve = CaseVals.end(); cvi != cve; ++cvi)
4667 SI->addCase(*cvi, DestBB);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004668 }
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004669 I = SI;
4670 break;
4671 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004672
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00004673 // Old SwitchInst format without case ranges.
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004674
Chris Lattner5285b5e2007-05-02 05:46:45 +00004675 if (Record.size() < 3 || (Record.size() & 1) == 0)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004676 return error("Invalid record");
Chris Lattner229907c2011-07-18 04:54:35 +00004677 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00004678 Value *Cond = getValue(Record, 1, NextValueNo, OpTy);
Chris Lattner5285b5e2007-05-02 05:46:45 +00004679 BasicBlock *Default = getBasicBlock(Record[2]);
Craig Topper2617dcc2014-04-15 06:32:26 +00004680 if (!OpTy || !Cond || !Default)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004681 return error("Invalid record");
Chris Lattner5285b5e2007-05-02 05:46:45 +00004682 unsigned NumCases = (Record.size()-3)/2;
Gabor Greife9ecc682008-04-06 20:25:17 +00004683 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
Devang Patelaf206b82009-09-18 19:26:43 +00004684 InstructionList.push_back(SI);
Chris Lattner5285b5e2007-05-02 05:46:45 +00004685 for (unsigned i = 0, e = NumCases; i != e; ++i) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004686 ConstantInt *CaseVal =
Chris Lattner5285b5e2007-05-02 05:46:45 +00004687 dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy));
4688 BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]);
Craig Topper2617dcc2014-04-15 06:32:26 +00004689 if (!CaseVal || !DestBB) {
Chris Lattner5285b5e2007-05-02 05:46:45 +00004690 delete SI;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004691 return error("Invalid record");
Chris Lattner5285b5e2007-05-02 05:46:45 +00004692 }
4693 SI->addCase(CaseVal, DestBB);
4694 }
4695 I = SI;
4696 break;
4697 }
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004698 case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...]
Chris Lattner3ed871f2009-10-27 19:13:16 +00004699 if (Record.size() < 2)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004700 return error("Invalid record");
Chris Lattner229907c2011-07-18 04:54:35 +00004701 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00004702 Value *Address = getValue(Record, 1, NextValueNo, OpTy);
Craig Topper2617dcc2014-04-15 06:32:26 +00004703 if (!OpTy || !Address)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004704 return error("Invalid record");
Chris Lattner3ed871f2009-10-27 19:13:16 +00004705 unsigned NumDests = Record.size()-2;
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004706 IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests);
Chris Lattner3ed871f2009-10-27 19:13:16 +00004707 InstructionList.push_back(IBI);
4708 for (unsigned i = 0, e = NumDests; i != e; ++i) {
4709 if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) {
4710 IBI->addDestination(DestBB);
4711 } else {
4712 delete IBI;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004713 return error("Invalid record");
Chris Lattner3ed871f2009-10-27 19:13:16 +00004714 }
4715 }
4716 I = IBI;
4717 break;
4718 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004719
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00004720 case bitc::FUNC_CODE_INST_INVOKE: {
4721 // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
Rafael Espindola48da4f42013-11-04 16:16:24 +00004722 if (Record.size() < 4)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004723 return error("Invalid record");
David Blaikie5ea1f7b2015-04-24 18:06:06 +00004724 unsigned OpNum = 0;
4725 AttributeSet PAL = getAttributes(Record[OpNum++]);
4726 unsigned CCInfo = Record[OpNum++];
4727 BasicBlock *NormalBB = getBasicBlock(Record[OpNum++]);
4728 BasicBlock *UnwindBB = getBasicBlock(Record[OpNum++]);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004729
David Blaikie5ea1f7b2015-04-24 18:06:06 +00004730 FunctionType *FTy = nullptr;
4731 if (CCInfo >> 13 & 1 &&
4732 !(FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++]))))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004733 return error("Explicit invoke type is not a function type");
David Blaikie5ea1f7b2015-04-24 18:06:06 +00004734
Chris Lattnerdf1233d2007-05-06 00:00:00 +00004735 Value *Callee;
4736 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004737 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004738
Chris Lattner229907c2011-07-18 04:54:35 +00004739 PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType());
David Blaikie5ea1f7b2015-04-24 18:06:06 +00004740 if (!CalleeTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004741 return error("Callee is not a pointer");
David Blaikie5ea1f7b2015-04-24 18:06:06 +00004742 if (!FTy) {
4743 FTy = dyn_cast<FunctionType>(CalleeTy->getElementType());
4744 if (!FTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004745 return error("Callee is not of pointer to function type");
David Blaikie5ea1f7b2015-04-24 18:06:06 +00004746 } else if (CalleeTy->getElementType() != FTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004747 return error("Explicit invoke type does not match pointee type of "
David Blaikie5ea1f7b2015-04-24 18:06:06 +00004748 "callee operand");
4749 if (Record.size() < FTy->getNumParams() + OpNum)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004750 return error("Insufficient operands to call");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004751
Chris Lattner5285b5e2007-05-02 05:46:45 +00004752 SmallVector<Value*, 16> Ops;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00004753 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00004754 Ops.push_back(getValue(Record, OpNum, NextValueNo,
4755 FTy->getParamType(i)));
Craig Topper2617dcc2014-04-15 06:32:26 +00004756 if (!Ops.back())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004757 return error("Invalid record");
Chris Lattner5285b5e2007-05-02 05:46:45 +00004758 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004759
Chris Lattnerdf1233d2007-05-06 00:00:00 +00004760 if (!FTy->isVarArg()) {
4761 if (Record.size() != OpNum)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004762 return error("Invalid record");
Chris Lattner5285b5e2007-05-02 05:46:45 +00004763 } else {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00004764 // Read type/value pairs for varargs params.
4765 while (OpNum != Record.size()) {
4766 Value *Op;
4767 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004768 return error("Invalid record");
Chris Lattnerdf1233d2007-05-06 00:00:00 +00004769 Ops.push_back(Op);
4770 }
Chris Lattner5285b5e2007-05-02 05:46:45 +00004771 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004772
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00004773 I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops, OperandBundles);
4774 OperandBundles.clear();
Devang Patelaf206b82009-09-18 19:26:43 +00004775 InstructionList.push_back(I);
Vedant Kumarad6d6e72015-10-27 21:17:06 +00004776 cast<InvokeInst>(I)->setCallingConv(
4777 static_cast<CallingConv::ID>(CallingConv::MaxID & CCInfo));
Devang Patel4c758ea2008-09-25 21:00:45 +00004778 cast<InvokeInst>(I)->setAttributes(PAL);
Chris Lattner5285b5e2007-05-02 05:46:45 +00004779 break;
4780 }
Bill Wendlingf891bf82011-07-31 06:30:59 +00004781 case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval]
4782 unsigned Idx = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +00004783 Value *Val = nullptr;
Bill Wendlingf891bf82011-07-31 06:30:59 +00004784 if (getValueTypePair(Record, Idx, NextValueNo, Val))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004785 return error("Invalid record");
Bill Wendlingf891bf82011-07-31 06:30:59 +00004786 I = ResumeInst::Create(Val);
Bill Wendlingb9a89992011-09-01 00:50:20 +00004787 InstructionList.push_back(I);
Bill Wendlingf891bf82011-07-31 06:30:59 +00004788 break;
4789 }
Chris Lattnere53603e2007-05-02 04:27:25 +00004790 case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE
Owen Anderson55f1c092009-08-13 21:58:54 +00004791 I = new UnreachableInst(Context);
Devang Patelaf206b82009-09-18 19:26:43 +00004792 InstructionList.push_back(I);
Chris Lattnere53603e2007-05-02 04:27:25 +00004793 break;
Chris Lattnere9759c22007-05-06 00:21:25 +00004794 case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...]
Chris Lattnere14cb882007-05-04 19:11:41 +00004795 if (Record.size() < 1 || ((Record.size()-1)&1))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004796 return error("Invalid record");
Chris Lattner229907c2011-07-18 04:54:35 +00004797 Type *Ty = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00004798 if (!Ty)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004799 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004800
Jay Foad52131342011-03-30 11:28:46 +00004801 PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2);
Devang Patelaf206b82009-09-18 19:26:43 +00004802 InstructionList.push_back(PN);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004803
Chris Lattnere14cb882007-05-04 19:11:41 +00004804 for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00004805 Value *V;
4806 // With the new function encoding, it is possible that operands have
4807 // negative IDs (for forward references). Use a signed VBR
4808 // representation to keep the encoding small.
4809 if (UseRelativeIDs)
4810 V = getValueSigned(Record, 1+i, NextValueNo, Ty);
4811 else
4812 V = getValue(Record, 1+i, NextValueNo, Ty);
Chris Lattnere14cb882007-05-04 19:11:41 +00004813 BasicBlock *BB = getBasicBlock(Record[2+i]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00004814 if (!V || !BB)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004815 return error("Invalid record");
Chris Lattnerc332bba2007-05-03 18:58:09 +00004816 PN->addIncoming(V, BB);
4817 }
4818 I = PN;
4819 break;
4820 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004821
David Majnemer7fddecc2015-06-17 20:52:32 +00004822 case bitc::FUNC_CODE_INST_LANDINGPAD:
4823 case bitc::FUNC_CODE_INST_LANDINGPAD_OLD: {
Bill Wendlingfae14752011-08-12 20:24:12 +00004824 // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?]
4825 unsigned Idx = 0;
David Majnemer7fddecc2015-06-17 20:52:32 +00004826 if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD) {
4827 if (Record.size() < 3)
4828 return error("Invalid record");
4829 } else {
4830 assert(BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD);
4831 if (Record.size() < 4)
4832 return error("Invalid record");
4833 }
Bill Wendlingfae14752011-08-12 20:24:12 +00004834 Type *Ty = getTypeByID(Record[Idx++]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00004835 if (!Ty)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004836 return error("Invalid record");
David Majnemer7fddecc2015-06-17 20:52:32 +00004837 if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD) {
4838 Value *PersFn = nullptr;
4839 if (getValueTypePair(Record, Idx, NextValueNo, PersFn))
4840 return error("Invalid record");
4841
4842 if (!F->hasPersonalityFn())
4843 F->setPersonalityFn(cast<Constant>(PersFn));
4844 else if (F->getPersonalityFn() != cast<Constant>(PersFn))
4845 return error("Personality function mismatch");
4846 }
Bill Wendlingfae14752011-08-12 20:24:12 +00004847
4848 bool IsCleanup = !!Record[Idx++];
4849 unsigned NumClauses = Record[Idx++];
David Majnemer7fddecc2015-06-17 20:52:32 +00004850 LandingPadInst *LP = LandingPadInst::Create(Ty, NumClauses);
Bill Wendlingfae14752011-08-12 20:24:12 +00004851 LP->setCleanup(IsCleanup);
4852 for (unsigned J = 0; J != NumClauses; ++J) {
4853 LandingPadInst::ClauseType CT =
4854 LandingPadInst::ClauseType(Record[Idx++]); (void)CT;
4855 Value *Val;
4856
4857 if (getValueTypePair(Record, Idx, NextValueNo, Val)) {
4858 delete LP;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004859 return error("Invalid record");
Bill Wendlingfae14752011-08-12 20:24:12 +00004860 }
4861
4862 assert((CT != LandingPadInst::Catch ||
4863 !isa<ArrayType>(Val->getType())) &&
4864 "Catch clause has a invalid type!");
4865 assert((CT != LandingPadInst::Filter ||
4866 isa<ArrayType>(Val->getType())) &&
4867 "Filter clause has invalid type!");
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +00004868 LP->addClause(cast<Constant>(Val));
Bill Wendlingfae14752011-08-12 20:24:12 +00004869 }
4870
4871 I = LP;
Bill Wendlingb9a89992011-09-01 00:50:20 +00004872 InstructionList.push_back(I);
Bill Wendlingfae14752011-08-12 20:24:12 +00004873 break;
4874 }
4875
Chris Lattnerf1c87102011-06-17 18:09:11 +00004876 case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]
4877 if (Record.size() != 4)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004878 return error("Invalid record");
JF Bastien30bf96b2015-02-22 19:32:03 +00004879 uint64_t AlignRecord = Record[3];
4880 const uint64_t InAllocaMask = uint64_t(1) << 5;
David Blaikiebdb49102015-04-28 16:51:01 +00004881 const uint64_t ExplicitTypeMask = uint64_t(1) << 6;
Bob Wilson043ee652015-07-28 04:05:45 +00004882 // Reserve bit 7 for SwiftError flag.
4883 // const uint64_t SwiftErrorMask = uint64_t(1) << 7;
David Blaikiebdb49102015-04-28 16:51:01 +00004884 const uint64_t FlagMask = InAllocaMask | ExplicitTypeMask;
JF Bastien30bf96b2015-02-22 19:32:03 +00004885 bool InAlloca = AlignRecord & InAllocaMask;
David Blaikiebdb49102015-04-28 16:51:01 +00004886 Type *Ty = getTypeByID(Record[0]);
4887 if ((AlignRecord & ExplicitTypeMask) == 0) {
4888 auto *PTy = dyn_cast_or_null<PointerType>(Ty);
4889 if (!PTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004890 return error("Old-style alloca with a non-pointer type");
David Blaikiebdb49102015-04-28 16:51:01 +00004891 Ty = PTy->getElementType();
4892 }
4893 Type *OpTy = getTypeByID(Record[1]);
4894 Value *Size = getFnValueByID(Record[2], OpTy);
JF Bastien30bf96b2015-02-22 19:32:03 +00004895 unsigned Align;
4896 if (std::error_code EC =
David Blaikiebdb49102015-04-28 16:51:01 +00004897 parseAlignmentValue(AlignRecord & ~FlagMask, Align)) {
JF Bastien30bf96b2015-02-22 19:32:03 +00004898 return EC;
4899 }
Rafael Espindola48da4f42013-11-04 16:16:24 +00004900 if (!Ty || !Size)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004901 return error("Invalid record");
David Blaikiebdb49102015-04-28 16:51:01 +00004902 AllocaInst *AI = new AllocaInst(Ty, Size, Align);
Reid Kleckner56b56ea2014-07-16 01:34:27 +00004903 AI->setUsedWithInAlloca(InAlloca);
4904 I = AI;
Devang Patelaf206b82009-09-18 19:26:43 +00004905 InstructionList.push_back(I);
Chris Lattnerc332bba2007-05-03 18:58:09 +00004906 break;
4907 }
Chris Lattner9f600c52007-05-03 22:04:19 +00004908 case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol]
Chris Lattnerdf1233d2007-05-06 00:00:00 +00004909 unsigned OpNum = 0;
4910 Value *Op;
4911 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
David Blaikie85035652015-02-25 01:07:20 +00004912 (OpNum + 2 != Record.size() && OpNum + 3 != Record.size()))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004913 return error("Invalid record");
David Blaikie85035652015-02-25 01:07:20 +00004914
4915 Type *Ty = nullptr;
4916 if (OpNum + 3 == Record.size())
4917 Ty = getTypeByID(Record[OpNum++]);
Rafael Espindola9d2bfc42015-12-14 23:17:03 +00004918 if (std::error_code EC = typeCheckLoadStoreInst(Ty, Op->getType()))
Filipe Cabecinhas11bb8492015-05-18 21:48:55 +00004919 return EC;
David Blaikieb7a029872015-04-17 19:56:21 +00004920 if (!Ty)
4921 Ty = cast<PointerType>(Op->getType())->getElementType();
David Blaikie85035652015-02-25 01:07:20 +00004922
JF Bastien30bf96b2015-02-22 19:32:03 +00004923 unsigned Align;
4924 if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
4925 return EC;
David Blaikieb7a029872015-04-17 19:56:21 +00004926 I = new LoadInst(Ty, Op, "", Record[OpNum + 1], Align);
David Blaikie85035652015-02-25 01:07:20 +00004927
Devang Patelaf206b82009-09-18 19:26:43 +00004928 InstructionList.push_back(I);
Chris Lattner83930552007-05-01 07:01:57 +00004929 break;
Chris Lattner9f600c52007-05-03 22:04:19 +00004930 }
Eli Friedman59b66882011-08-09 23:02:53 +00004931 case bitc::FUNC_CODE_INST_LOADATOMIC: {
4932 // LOADATOMIC: [opty, op, align, vol, ordering, synchscope]
4933 unsigned OpNum = 0;
4934 Value *Op;
4935 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
David Blaikie85035652015-02-25 01:07:20 +00004936 (OpNum + 4 != Record.size() && OpNum + 5 != Record.size()))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004937 return error("Invalid record");
Eli Friedman59b66882011-08-09 23:02:53 +00004938
David Blaikie85035652015-02-25 01:07:20 +00004939 Type *Ty = nullptr;
4940 if (OpNum + 5 == Record.size())
4941 Ty = getTypeByID(Record[OpNum++]);
Rafael Espindola9d2bfc42015-12-14 23:17:03 +00004942 if (std::error_code EC = typeCheckLoadStoreInst(Ty, Op->getType()))
Filipe Cabecinhas11bb8492015-05-18 21:48:55 +00004943 return EC;
4944 if (!Ty)
4945 Ty = cast<PointerType>(Op->getType())->getElementType();
David Blaikie85035652015-02-25 01:07:20 +00004946
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004947 AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]);
Eli Friedman59b66882011-08-09 23:02:53 +00004948 if (Ordering == NotAtomic || Ordering == Release ||
4949 Ordering == AcquireRelease)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004950 return error("Invalid record");
Eli Friedman59b66882011-08-09 23:02:53 +00004951 if (Ordering != NotAtomic && Record[OpNum] == 0)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004952 return error("Invalid record");
4953 SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]);
Eli Friedman59b66882011-08-09 23:02:53 +00004954
JF Bastien30bf96b2015-02-22 19:32:03 +00004955 unsigned Align;
4956 if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
4957 return EC;
4958 I = new LoadInst(Op, "", Record[OpNum+1], Align, Ordering, SynchScope);
David Blaikie85035652015-02-25 01:07:20 +00004959
Eli Friedman59b66882011-08-09 23:02:53 +00004960 InstructionList.push_back(I);
4961 break;
4962 }
David Blaikie612ddbf2015-04-22 04:14:42 +00004963 case bitc::FUNC_CODE_INST_STORE:
4964 case bitc::FUNC_CODE_INST_STORE_OLD: { // STORE2:[ptrty, ptr, val, align, vol]
Christopher Lamb54dd24c2007-12-11 08:59:05 +00004965 unsigned OpNum = 0;
4966 Value *Val, *Ptr;
4967 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
David Blaikie612ddbf2015-04-22 04:14:42 +00004968 (BitCode == bitc::FUNC_CODE_INST_STORE
4969 ? getValueTypePair(Record, OpNum, NextValueNo, Val)
4970 : popValue(Record, OpNum, NextValueNo,
4971 cast<PointerType>(Ptr->getType())->getElementType(),
4972 Val)) ||
4973 OpNum + 2 != Record.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004974 return error("Invalid record");
Filipe Cabecinhas11bb8492015-05-18 21:48:55 +00004975
Rafael Espindola9d2bfc42015-12-14 23:17:03 +00004976 if (std::error_code EC =
4977 typeCheckLoadStoreInst(Val->getType(), Ptr->getType()))
Filipe Cabecinhas11bb8492015-05-18 21:48:55 +00004978 return EC;
JF Bastien30bf96b2015-02-22 19:32:03 +00004979 unsigned Align;
4980 if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
4981 return EC;
4982 I = new StoreInst(Val, Ptr, Record[OpNum+1], Align);
Devang Patelaf206b82009-09-18 19:26:43 +00004983 InstructionList.push_back(I);
Christopher Lamb54dd24c2007-12-11 08:59:05 +00004984 break;
4985 }
David Blaikie50a06152015-04-22 04:14:46 +00004986 case bitc::FUNC_CODE_INST_STOREATOMIC:
4987 case bitc::FUNC_CODE_INST_STOREATOMIC_OLD: {
Eli Friedman59b66882011-08-09 23:02:53 +00004988 // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, synchscope]
4989 unsigned OpNum = 0;
4990 Value *Val, *Ptr;
4991 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
David Blaikie50a06152015-04-22 04:14:46 +00004992 (BitCode == bitc::FUNC_CODE_INST_STOREATOMIC
4993 ? getValueTypePair(Record, OpNum, NextValueNo, Val)
4994 : popValue(Record, OpNum, NextValueNo,
4995 cast<PointerType>(Ptr->getType())->getElementType(),
4996 Val)) ||
4997 OpNum + 4 != Record.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00004998 return error("Invalid record");
Eli Friedman59b66882011-08-09 23:02:53 +00004999
Rafael Espindola9d2bfc42015-12-14 23:17:03 +00005000 if (std::error_code EC =
5001 typeCheckLoadStoreInst(Val->getType(), Ptr->getType()))
Filipe Cabecinhas11bb8492015-05-18 21:48:55 +00005002 return EC;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005003 AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]);
Eli Friedman222b5a42011-09-19 19:41:28 +00005004 if (Ordering == NotAtomic || Ordering == Acquire ||
Eli Friedman59b66882011-08-09 23:02:53 +00005005 Ordering == AcquireRelease)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005006 return error("Invalid record");
5007 SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]);
Eli Friedman59b66882011-08-09 23:02:53 +00005008 if (Ordering != NotAtomic && Record[OpNum] == 0)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005009 return error("Invalid record");
Eli Friedman59b66882011-08-09 23:02:53 +00005010
JF Bastien30bf96b2015-02-22 19:32:03 +00005011 unsigned Align;
5012 if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
5013 return EC;
5014 I = new StoreInst(Val, Ptr, Record[OpNum+1], Align, Ordering, SynchScope);
Eli Friedman59b66882011-08-09 23:02:53 +00005015 InstructionList.push_back(I);
5016 break;
5017 }
David Blaikie2a661cd2015-04-28 04:30:29 +00005018 case bitc::FUNC_CODE_INST_CMPXCHG_OLD:
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005019 case bitc::FUNC_CODE_INST_CMPXCHG: {
Tim Northovere94a5182014-03-11 10:48:52 +00005020 // CMPXCHG:[ptrty, ptr, cmp, new, vol, successordering, synchscope,
Tim Northover420a2162014-06-13 14:24:07 +00005021 // failureordering?, isweak?]
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005022 unsigned OpNum = 0;
5023 Value *Ptr, *Cmp, *New;
5024 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
David Blaikie2a661cd2015-04-28 04:30:29 +00005025 (BitCode == bitc::FUNC_CODE_INST_CMPXCHG
5026 ? getValueTypePair(Record, OpNum, NextValueNo, Cmp)
5027 : popValue(Record, OpNum, NextValueNo,
5028 cast<PointerType>(Ptr->getType())->getElementType(),
5029 Cmp)) ||
5030 popValue(Record, OpNum, NextValueNo, Cmp->getType(), New) ||
5031 Record.size() < OpNum + 3 || Record.size() > OpNum + 5)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005032 return error("Invalid record");
5033 AtomicOrdering SuccessOrdering = getDecodedOrdering(Record[OpNum + 1]);
Tim Northovere94a5182014-03-11 10:48:52 +00005034 if (SuccessOrdering == NotAtomic || SuccessOrdering == Unordered)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005035 return error("Invalid record");
5036 SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 2]);
Tim Northovere94a5182014-03-11 10:48:52 +00005037
Rafael Espindola9d2bfc42015-12-14 23:17:03 +00005038 if (std::error_code EC =
5039 typeCheckLoadStoreInst(Cmp->getType(), Ptr->getType()))
Filipe Cabecinhas11bb8492015-05-18 21:48:55 +00005040 return EC;
Tim Northovere94a5182014-03-11 10:48:52 +00005041 AtomicOrdering FailureOrdering;
5042 if (Record.size() < 7)
5043 FailureOrdering =
5044 AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering);
5045 else
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005046 FailureOrdering = getDecodedOrdering(Record[OpNum + 3]);
Tim Northovere94a5182014-03-11 10:48:52 +00005047
5048 I = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering, FailureOrdering,
5049 SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005050 cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]);
Tim Northover420a2162014-06-13 14:24:07 +00005051
5052 if (Record.size() < 8) {
5053 // Before weak cmpxchgs existed, the instruction simply returned the
5054 // value loaded from memory, so bitcode files from that era will be
5055 // expecting the first component of a modern cmpxchg.
5056 CurBB->getInstList().push_back(I);
5057 I = ExtractValueInst::Create(I, 0);
5058 } else {
5059 cast<AtomicCmpXchgInst>(I)->setWeak(Record[OpNum+4]);
5060 }
5061
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005062 InstructionList.push_back(I);
5063 break;
5064 }
5065 case bitc::FUNC_CODE_INST_ATOMICRMW: {
5066 // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, synchscope]
5067 unsigned OpNum = 0;
5068 Value *Ptr, *Val;
5069 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00005070 popValue(Record, OpNum, NextValueNo,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005071 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
5072 OpNum+4 != Record.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005073 return error("Invalid record");
5074 AtomicRMWInst::BinOp Operation = getDecodedRMWOperation(Record[OpNum]);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005075 if (Operation < AtomicRMWInst::FIRST_BINOP ||
5076 Operation > AtomicRMWInst::LAST_BINOP)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005077 return error("Invalid record");
5078 AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]);
Eli Friedman59b66882011-08-09 23:02:53 +00005079 if (Ordering == NotAtomic || Ordering == Unordered)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005080 return error("Invalid record");
5081 SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005082 I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SynchScope);
5083 cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]);
5084 InstructionList.push_back(I);
5085 break;
5086 }
Eli Friedmanfee02c62011-07-25 23:16:38 +00005087 case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, synchscope]
5088 if (2 != Record.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005089 return error("Invalid record");
5090 AtomicOrdering Ordering = getDecodedOrdering(Record[0]);
Eli Friedmanfee02c62011-07-25 23:16:38 +00005091 if (Ordering == NotAtomic || Ordering == Unordered ||
5092 Ordering == Monotonic)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005093 return error("Invalid record");
5094 SynchronizationScope SynchScope = getDecodedSynchScope(Record[1]);
Eli Friedmanfee02c62011-07-25 23:16:38 +00005095 I = new FenceInst(Context, Ordering, SynchScope);
5096 InstructionList.push_back(I);
5097 break;
5098 }
Chris Lattnerc44070802011-06-17 18:17:37 +00005099 case bitc::FUNC_CODE_INST_CALL: {
Sanjay Patelfa54ace2015-12-14 21:59:03 +00005100 // CALL: [paramattrs, cc, fmf, fnty, fnid, arg0, arg1...]
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00005101 if (Record.size() < 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005102 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005103
David Blaikiedbe6e0f2015-04-17 06:40:14 +00005104 unsigned OpNum = 0;
5105 AttributeSet PAL = getAttributes(Record[OpNum++]);
5106 unsigned CCInfo = Record[OpNum++];
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005107
Sanjay Patelfa54ace2015-12-14 21:59:03 +00005108 FastMathFlags FMF;
5109 if ((CCInfo >> bitc::CALL_FMF) & 1) {
5110 FMF = getDecodedFastMathFlags(Record[OpNum++]);
5111 if (!FMF.any())
5112 return error("Fast math flags indicator set for call with no FMF");
5113 }
5114
David Blaikiedbe6e0f2015-04-17 06:40:14 +00005115 FunctionType *FTy = nullptr;
Akira Hatanaka97cb3972015-11-07 02:48:49 +00005116 if (CCInfo >> bitc::CALL_EXPLICIT_TYPE & 1 &&
David Blaikiedbe6e0f2015-04-17 06:40:14 +00005117 !(FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++]))))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005118 return error("Explicit call type is not a function type");
David Blaikiedbe6e0f2015-04-17 06:40:14 +00005119
Chris Lattnerdf1233d2007-05-06 00:00:00 +00005120 Value *Callee;
5121 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005122 return error("Invalid record");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005123
Chris Lattner229907c2011-07-18 04:54:35 +00005124 PointerType *OpTy = dyn_cast<PointerType>(Callee->getType());
David Blaikiedbe6e0f2015-04-17 06:40:14 +00005125 if (!OpTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005126 return error("Callee is not a pointer type");
David Blaikie348de692015-04-23 21:36:23 +00005127 if (!FTy) {
5128 FTy = dyn_cast<FunctionType>(OpTy->getElementType());
5129 if (!FTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005130 return error("Callee is not of pointer to function type");
David Blaikie348de692015-04-23 21:36:23 +00005131 } else if (OpTy->getElementType() != FTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005132 return error("Explicit call type does not match pointee type of "
David Blaikiedbe6e0f2015-04-17 06:40:14 +00005133 "callee operand");
5134 if (Record.size() < FTy->getNumParams() + OpNum)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005135 return error("Insufficient operands to call");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005136
Chris Lattner9f600c52007-05-03 22:04:19 +00005137 SmallVector<Value*, 16> Args;
5138 // Read the fixed params.
Chris Lattnerdf1233d2007-05-06 00:00:00 +00005139 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00005140 if (FTy->getParamType(i)->isLabelTy())
Dale Johannesen4646aa32007-11-05 21:20:28 +00005141 Args.push_back(getBasicBlock(Record[OpNum]));
Dan Gohmanbbcd04d2010-09-13 18:00:48 +00005142 else
Jan Wen Voungafaced02012-10-11 20:20:40 +00005143 Args.push_back(getValue(Record, OpNum, NextValueNo,
5144 FTy->getParamType(i)));
Craig Topper2617dcc2014-04-15 06:32:26 +00005145 if (!Args.back())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005146 return error("Invalid record");
Chris Lattner9f600c52007-05-03 22:04:19 +00005147 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005148
Chris Lattner9f600c52007-05-03 22:04:19 +00005149 // Read type/value pairs for varargs params.
Chris Lattner9f600c52007-05-03 22:04:19 +00005150 if (!FTy->isVarArg()) {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00005151 if (OpNum != Record.size())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005152 return error("Invalid record");
Chris Lattner9f600c52007-05-03 22:04:19 +00005153 } else {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00005154 while (OpNum != Record.size()) {
5155 Value *Op;
5156 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005157 return error("Invalid record");
Chris Lattnerdf1233d2007-05-06 00:00:00 +00005158 Args.push_back(Op);
Chris Lattner9f600c52007-05-03 22:04:19 +00005159 }
5160 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005161
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00005162 I = CallInst::Create(FTy, Callee, Args, OperandBundles);
5163 OperandBundles.clear();
Devang Patelaf206b82009-09-18 19:26:43 +00005164 InstructionList.push_back(I);
Sandeep Patel68c5f472009-09-02 08:44:58 +00005165 cast<CallInst>(I)->setCallingConv(
Akira Hatanaka97cb3972015-11-07 02:48:49 +00005166 static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV));
Reid Kleckner5772b772014-04-24 20:14:34 +00005167 CallInst::TailCallKind TCK = CallInst::TCK_None;
Akira Hatanaka97cb3972015-11-07 02:48:49 +00005168 if (CCInfo & 1 << bitc::CALL_TAIL)
Reid Kleckner5772b772014-04-24 20:14:34 +00005169 TCK = CallInst::TCK_Tail;
Akira Hatanaka97cb3972015-11-07 02:48:49 +00005170 if (CCInfo & (1 << bitc::CALL_MUSTTAIL))
Reid Kleckner5772b772014-04-24 20:14:34 +00005171 TCK = CallInst::TCK_MustTail;
Akira Hatanaka97cb3972015-11-07 02:48:49 +00005172 if (CCInfo & (1 << bitc::CALL_NOTAIL))
Akira Hatanaka5cfcce122015-11-06 23:55:38 +00005173 TCK = CallInst::TCK_NoTail;
Reid Kleckner5772b772014-04-24 20:14:34 +00005174 cast<CallInst>(I)->setTailCallKind(TCK);
Devang Patel4c758ea2008-09-25 21:00:45 +00005175 cast<CallInst>(I)->setAttributes(PAL);
Sanjay Patelfa54ace2015-12-14 21:59:03 +00005176 if (FMF.any()) {
5177 if (!isa<FPMathOperator>(I))
5178 return error("Fast-math-flags specified for call without "
5179 "floating-point scalar or vector return type");
5180 I->setFastMathFlags(FMF);
5181 }
Chris Lattner9f600c52007-05-03 22:04:19 +00005182 break;
5183 }
5184 case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty]
5185 if (Record.size() < 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005186 return error("Invalid record");
Chris Lattner229907c2011-07-18 04:54:35 +00005187 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00005188 Value *Op = getValue(Record, 1, NextValueNo, OpTy);
Chris Lattner229907c2011-07-18 04:54:35 +00005189 Type *ResTy = getTypeByID(Record[2]);
Chris Lattner9f600c52007-05-03 22:04:19 +00005190 if (!OpTy || !Op || !ResTy)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005191 return error("Invalid record");
Chris Lattner9f600c52007-05-03 22:04:19 +00005192 I = new VAArgInst(Op, ResTy);
Devang Patelaf206b82009-09-18 19:26:43 +00005193 InstructionList.push_back(I);
Chris Lattner9f600c52007-05-03 22:04:19 +00005194 break;
5195 }
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00005196
5197 case bitc::FUNC_CODE_OPERAND_BUNDLE: {
5198 // A call or an invoke can be optionally prefixed with some variable
5199 // number of operand bundle blocks. These blocks are read into
5200 // OperandBundles and consumed at the next call or invoke instruction.
5201
5202 if (Record.size() < 1 || Record[0] >= BundleTags.size())
5203 return error("Invalid record");
5204
Sanjoy Dasf79d3442015-11-18 08:30:07 +00005205 std::vector<Value *> Inputs;
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00005206
5207 unsigned OpNum = 1;
5208 while (OpNum != Record.size()) {
5209 Value *Op;
5210 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
5211 return error("Invalid record");
5212 Inputs.push_back(Op);
5213 }
5214
Sanjoy Dasf79d3442015-11-18 08:30:07 +00005215 OperandBundles.emplace_back(BundleTags[Record[0]], std::move(Inputs));
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00005216 continue;
5217 }
Chris Lattner83930552007-05-01 07:01:57 +00005218 }
5219
5220 // Add instruction to end of current BB. If there is no current BB, reject
5221 // this file.
Craig Topper2617dcc2014-04-15 06:32:26 +00005222 if (!CurBB) {
Chris Lattner83930552007-05-01 07:01:57 +00005223 delete I;
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005224 return error("Invalid instruction with no BB");
Chris Lattner83930552007-05-01 07:01:57 +00005225 }
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00005226 if (!OperandBundles.empty()) {
5227 delete I;
5228 return error("Operand bundles found with no consumer");
5229 }
Chris Lattner83930552007-05-01 07:01:57 +00005230 CurBB->getInstList().push_back(I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005231
Chris Lattner83930552007-05-01 07:01:57 +00005232 // If this was a terminator instruction, move to the next block.
5233 if (isa<TerminatorInst>(I)) {
5234 ++CurBBNo;
Craig Topper2617dcc2014-04-15 06:32:26 +00005235 CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr;
Chris Lattner83930552007-05-01 07:01:57 +00005236 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005237
Chris Lattner83930552007-05-01 07:01:57 +00005238 // Non-void values get registered in the value table for future use.
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00005239 if (I && !I->getType()->isVoidTy())
David Majnemer8a1c45d2015-12-12 05:38:55 +00005240 ValueList.assignValue(I, NextValueNo++);
Chris Lattner85b7b402007-05-01 05:52:21 +00005241 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005242
Chris Lattner27d38752013-01-20 02:13:19 +00005243OutOfRecordLoop:
Joe Abbey97b7a172013-02-06 22:14:06 +00005244
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00005245 if (!OperandBundles.empty())
5246 return error("Operand bundles found with no consumer");
5247
Chris Lattner83930552007-05-01 07:01:57 +00005248 // Check the function list for unresolved values.
5249 if (Argument *A = dyn_cast<Argument>(ValueList.back())) {
Craig Topper2617dcc2014-04-15 06:32:26 +00005250 if (!A->getParent()) {
Chris Lattner83930552007-05-01 07:01:57 +00005251 // We found at least one unresolved value. Nuke them all to avoid leaks.
5252 for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){
Craig Topper2617dcc2014-04-15 06:32:26 +00005253 if ((A = dyn_cast_or_null<Argument>(ValueList[i])) && !A->getParent()) {
Owen Andersonb292b8c2009-07-30 23:03:37 +00005254 A->replaceAllUsesWith(UndefValue::get(A->getType()));
Chris Lattner83930552007-05-01 07:01:57 +00005255 delete A;
5256 }
5257 }
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005258 return error("Never resolved value found in function");
Chris Lattner83930552007-05-01 07:01:57 +00005259 }
Chris Lattner83930552007-05-01 07:01:57 +00005260 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005261
Dan Gohman9b9ff462010-08-25 20:23:38 +00005262 // FIXME: Check for unresolved forward-declared metadata references
5263 // and clean up leaks.
5264
Chris Lattner85b7b402007-05-01 05:52:21 +00005265 // Trim the value list down to the size it was before we parsed this function.
5266 ValueList.shrinkTo(ModuleValueListSize);
Teresa Johnson61b406e2015-12-29 23:00:22 +00005267 MetadataList.shrinkTo(ModuleMetadataListSize);
Chris Lattner85b7b402007-05-01 05:52:21 +00005268 std::vector<BasicBlock*>().swap(FunctionBBs);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00005269 return std::error_code();
Chris Lattner51ffe7c2007-05-01 04:59:48 +00005270}
5271
Rafael Espindola7d712032013-11-05 17:16:08 +00005272/// Find the function body in the bitcode stream
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005273std::error_code BitcodeReader::findFunctionInStream(
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00005274 Function *F,
5275 DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005276 while (DeferredFunctionInfoIterator->second == 0) {
Teresa Johnsonff642b92015-09-17 20:12:00 +00005277 // This is the fallback handling for the old format bitcode that
Teresa Johnson1493ad92015-10-10 14:18:36 +00005278 // didn't contain the function index in the VST, or when we have
5279 // an anonymous function which would not have a VST entry.
5280 // Assert that we have one of those two cases.
5281 assert(VSTOffset == 0 || !F->hasName());
5282 // Parse the next body in the stream and set its position in the
5283 // DeferredFunctionInfo map.
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005284 if (std::error_code EC = rememberAndSkipFunctionBodies())
5285 return EC;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005286 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00005287 return std::error_code();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005288}
5289
Chris Lattner9eeada92007-05-18 04:02:46 +00005290//===----------------------------------------------------------------------===//
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00005291// GVMaterializer implementation
Chris Lattner9eeada92007-05-18 04:02:46 +00005292//===----------------------------------------------------------------------===//
5293
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +00005294void BitcodeReader::releaseBuffer() { Buffer.release(); }
Chris Lattner9eeada92007-05-18 04:02:46 +00005295
Rafael Espindola5a52e6d2014-10-24 22:50:48 +00005296std::error_code BitcodeReader::materialize(GlobalValue *GV) {
Duncan P. N. Exon Smith68f56242016-03-25 01:29:50 +00005297 if (std::error_code EC = materializeMetadata())
5298 return EC;
Manman Ren4a9b0eb2015-03-13 19:24:30 +00005299
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00005300 Function *F = dyn_cast<Function>(GV);
5301 // If it's not a function or is already material, ignore the request.
Rafael Espindola2b11ad42013-11-05 19:36:34 +00005302 if (!F || !F->isMaterializable())
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00005303 return std::error_code();
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00005304
5305 DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F);
Chris Lattner9eeada92007-05-18 04:02:46 +00005306 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005307 // If its position is recorded as 0, its body is somewhere in the stream
5308 // but we haven't seen it yet.
Rafael Espindola1c863ca2015-06-22 18:06:15 +00005309 if (DFII->second == 0)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005310 if (std::error_code EC = findFunctionInStream(F, DFII))
Rafael Espindola2b11ad42013-11-05 19:36:34 +00005311 return EC;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005312
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00005313 // Move the bit stream to the saved position of the deferred function body.
5314 Stream.JumpToBit(DFII->second);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005315
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005316 if (std::error_code EC = parseFunctionBody(F))
Rafael Espindola2b11ad42013-11-05 19:36:34 +00005317 return EC;
Rafael Espindolad4bcefc2014-10-24 18:13:04 +00005318 F->setIsMaterializable(false);
Chandler Carruth7132e002007-08-04 01:51:18 +00005319
Rafael Espindola0d68b4c2015-03-30 21:36:43 +00005320 if (StripDebugInfo)
5321 stripDebugInfo(*F);
5322
Chandler Carruth7132e002007-08-04 01:51:18 +00005323 // Upgrade any old intrinsic calls in the function.
Rafael Espindola86e33402015-07-02 15:55:09 +00005324 for (auto &I : UpgradedIntrinsics) {
Rafael Espindola257a3532016-01-15 19:00:20 +00005325 for (auto UI = I.first->materialized_user_begin(), UE = I.first->user_end();
5326 UI != UE;) {
Filipe Cabecinhas0011c582015-07-03 20:12:01 +00005327 User *U = *UI;
5328 ++UI;
5329 if (CallInst *CI = dyn_cast<CallInst>(U))
5330 UpgradeIntrinsicCall(CI, I.second);
Chandler Carruth7132e002007-08-04 01:51:18 +00005331 }
5332 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005333
Peter Collingbourned4bff302015-11-05 22:03:56 +00005334 // Finish fn->subprogram upgrade for materialized functions.
5335 if (DISubprogram *SP = FunctionsWithSPs.lookup(F))
5336 F->setSubprogram(SP);
5337
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00005338 // Bring in any functions that this function forward-referenced via
5339 // blockaddresses.
5340 return materializeForwardReferencedFunctions();
Chris Lattner9eeada92007-05-18 04:02:46 +00005341}
5342
Rafael Espindola79753a02015-12-18 21:18:57 +00005343std::error_code BitcodeReader::materializeModule() {
Manman Ren4a9b0eb2015-03-13 19:24:30 +00005344 if (std::error_code EC = materializeMetadata())
5345 return EC;
5346
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00005347 // Promise to materialize all forward references.
5348 WillMaterializeAllForwardRefs = true;
5349
Chris Lattner06310bf2009-06-16 05:15:21 +00005350 // Iterate over the module, deserializing any functions that are still on
5351 // disk.
Duncan P. N. Exon Smithfb1743a32015-10-13 16:48:55 +00005352 for (Function &F : *TheModule) {
5353 if (std::error_code EC = materialize(&F))
Rafael Espindola246c4fb2014-11-01 16:46:18 +00005354 return EC;
Rafael Espindola2b11ad42013-11-05 19:36:34 +00005355 }
Teresa Johnson1493ad92015-10-10 14:18:36 +00005356 // At this point, if there are any function bodies, parse the rest of
5357 // the bits in the module past the last function block we have recorded
5358 // through either lazy scanning or the VST.
5359 if (LastFunctionBlockBit || NextUnreadBit)
5360 parseModule(LastFunctionBlockBit > NextUnreadBit ? LastFunctionBlockBit
5361 : NextUnreadBit);
Derek Schuff92ef9752012-02-29 00:07:09 +00005362
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00005363 // Check that all block address forward references got resolved (as we
5364 // promised above).
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00005365 if (!BasicBlockFwdRefs.empty())
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005366 return error("Never resolved function from blockaddress");
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00005367
Chris Bieneman671d0dd2016-03-16 23:17:54 +00005368 // Upgrading intrinsic calls before TBAA can cause TBAA metadata to be lost,
5369 // to prevent this instructions with TBAA tags should be upgraded first.
5370 for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++)
5371 UpgradeInstWithTBAATag(InstsWithTBAATag[I]);
5372
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005373 // Upgrade any intrinsic calls that slipped through (should not happen!) and
5374 // delete the old functions to clean up. We can't do this unless the entire
5375 // module is materialized because there could always be another function body
Chandler Carruth7132e002007-08-04 01:51:18 +00005376 // with calls to the old function.
Rafael Espindola86e33402015-07-02 15:55:09 +00005377 for (auto &I : UpgradedIntrinsics) {
Filipe Cabecinhas0011c582015-07-03 20:12:01 +00005378 for (auto *U : I.first->users()) {
5379 if (CallInst *CI = dyn_cast<CallInst>(U))
5380 UpgradeIntrinsicCall(CI, I.second);
Chandler Carruth7132e002007-08-04 01:51:18 +00005381 }
Filipe Cabecinhas0011c582015-07-03 20:12:01 +00005382 if (!I.first->use_empty())
5383 I.first->replaceAllUsesWith(I.second);
5384 I.first->eraseFromParent();
Chandler Carruth7132e002007-08-04 01:51:18 +00005385 }
Rafael Espindola4e721212015-07-02 16:22:40 +00005386 UpgradedIntrinsics.clear();
Devang Patel80ae3492009-08-28 23:24:31 +00005387
Rafael Espindola79753a02015-12-18 21:18:57 +00005388 UpgradeDebugInfo(*TheModule);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00005389 return std::error_code();
Chris Lattner9eeada92007-05-18 04:02:46 +00005390}
5391
Rafael Espindola2fa1e432014-12-03 07:18:23 +00005392std::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const {
5393 return IdentifiedStructTypes;
5394}
5395
Rafael Espindola1aabf982015-06-16 23:29:49 +00005396std::error_code
5397BitcodeReader::initStream(std::unique_ptr<DataStreamer> Streamer) {
Rafael Espindola4223a1f2015-06-15 20:08:17 +00005398 if (Streamer)
Rafael Espindola1aabf982015-06-16 23:29:49 +00005399 return initLazyStream(std::move(Streamer));
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005400 return initStreamFromBuffer();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005401}
5402
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005403std::error_code BitcodeReader::initStreamFromBuffer() {
Roman Divacky4717a8d2012-09-06 15:42:13 +00005404 const unsigned char *BufPtr = (const unsigned char*)Buffer->getBufferStart();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005405 const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
5406
Rafael Espindola27435252014-07-29 21:01:24 +00005407 if (Buffer->getBufferSize() & 3)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005408 return error("Invalid bitcode signature");
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005409
5410 // If we have a wrapper header, parse it and ignore the non-bc file contents.
5411 // The magic number is 0x0B17C0DE stored in little endian.
5412 if (isBitcodeWrapper(BufPtr, BufEnd))
5413 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005414 return error("Invalid bitcode wrapper header");
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005415
5416 StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
Rafael Espindolade1e5b82014-11-12 14:48:38 +00005417 Stream.init(&*StreamFile);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005418
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00005419 return std::error_code();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005420}
5421
Rafael Espindola1aabf982015-06-16 23:29:49 +00005422std::error_code
5423BitcodeReader::initLazyStream(std::unique_ptr<DataStreamer> Streamer) {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005424 // Check and strip off the bitcode wrapper; BitstreamReader expects never to
5425 // see it.
Rafael Espindola1aabf982015-06-16 23:29:49 +00005426 auto OwnedBytes =
5427 llvm::make_unique<StreamingMemoryObject>(std::move(Streamer));
Rafael Espindola7d727b52014-12-18 05:08:43 +00005428 StreamingMemoryObject &Bytes = *OwnedBytes;
Yaron Keren06d69302014-12-18 10:03:35 +00005429 StreamFile = llvm::make_unique<BitstreamReader>(std::move(OwnedBytes));
Rafael Espindolade1e5b82014-11-12 14:48:38 +00005430 Stream.init(&*StreamFile);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005431
5432 unsigned char buf[16];
Rafael Espindola7d727b52014-12-18 05:08:43 +00005433 if (Bytes.readBytes(buf, 16, 0) != 16)
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005434 return error("Invalid bitcode signature");
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005435
5436 if (!isBitcode(buf, buf + 16))
Rafael Espindolacbdcb502015-06-15 20:55:37 +00005437 return error("Invalid bitcode signature");
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005438
5439 if (isBitcodeWrapper(buf, buf + 4)) {
5440 const unsigned char *bitcodeStart = buf;
5441 const unsigned char *bitcodeEnd = buf + 16;
5442 SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
Rafael Espindola7d727b52014-12-18 05:08:43 +00005443 Bytes.dropLeadingBytes(bitcodeStart - buf);
5444 Bytes.setKnownObjectSize(bitcodeEnd - bitcodeStart);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00005445 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00005446 return std::error_code();
Rafael Espindola48da4f42013-11-04 16:16:24 +00005447}
5448
Teresa Johnson26ab5772016-03-15 00:04:37 +00005449std::error_code ModuleSummaryIndexBitcodeReader::error(BitcodeError E,
5450 const Twine &Message) {
Teresa Johnson403a7872015-10-04 14:33:43 +00005451 return ::error(DiagnosticHandler, make_error_code(E), Message);
5452}
5453
Teresa Johnson26ab5772016-03-15 00:04:37 +00005454std::error_code ModuleSummaryIndexBitcodeReader::error(const Twine &Message) {
Teresa Johnson403a7872015-10-04 14:33:43 +00005455 return ::error(DiagnosticHandler,
5456 make_error_code(BitcodeError::CorruptedBitcode), Message);
5457}
5458
Teresa Johnson26ab5772016-03-15 00:04:37 +00005459std::error_code ModuleSummaryIndexBitcodeReader::error(BitcodeError E) {
Teresa Johnson403a7872015-10-04 14:33:43 +00005460 return ::error(DiagnosticHandler, make_error_code(E));
5461}
5462
Teresa Johnson26ab5772016-03-15 00:04:37 +00005463ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader(
Mehdi Amini354f5202015-11-19 05:52:29 +00005464 MemoryBuffer *Buffer, DiagnosticHandlerFunction DiagnosticHandler,
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005465 bool IsLazy, bool CheckGlobalValSummaryPresenceOnly)
Mehdi Amini354f5202015-11-19 05:52:29 +00005466 : DiagnosticHandler(DiagnosticHandler), Buffer(Buffer), IsLazy(IsLazy),
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005467 CheckGlobalValSummaryPresenceOnly(CheckGlobalValSummaryPresenceOnly) {}
Teresa Johnson403a7872015-10-04 14:33:43 +00005468
Teresa Johnson26ab5772016-03-15 00:04:37 +00005469ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader(
Mehdi Amini354f5202015-11-19 05:52:29 +00005470 DiagnosticHandlerFunction DiagnosticHandler, bool IsLazy,
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005471 bool CheckGlobalValSummaryPresenceOnly)
Mehdi Amini354f5202015-11-19 05:52:29 +00005472 : DiagnosticHandler(DiagnosticHandler), Buffer(nullptr), IsLazy(IsLazy),
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005473 CheckGlobalValSummaryPresenceOnly(CheckGlobalValSummaryPresenceOnly) {}
Teresa Johnson403a7872015-10-04 14:33:43 +00005474
Teresa Johnson26ab5772016-03-15 00:04:37 +00005475void ModuleSummaryIndexBitcodeReader::freeState() { Buffer = nullptr; }
Teresa Johnson403a7872015-10-04 14:33:43 +00005476
Teresa Johnson26ab5772016-03-15 00:04:37 +00005477void ModuleSummaryIndexBitcodeReader::releaseBuffer() { Buffer.release(); }
Teresa Johnson403a7872015-10-04 14:33:43 +00005478
Teresa Johnson26ab5772016-03-15 00:04:37 +00005479uint64_t ModuleSummaryIndexBitcodeReader::getGUIDFromValueId(unsigned ValueId) {
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005480 auto VGI = ValueIdToCallGraphGUIDMap.find(ValueId);
5481 assert(VGI != ValueIdToCallGraphGUIDMap.end());
5482 return VGI->second;
5483}
5484
5485GlobalValueInfo *
Teresa Johnson26ab5772016-03-15 00:04:37 +00005486ModuleSummaryIndexBitcodeReader::getInfoFromSummaryOffset(uint64_t Offset) {
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005487 auto I = SummaryOffsetToInfoMap.find(Offset);
5488 assert(I != SummaryOffsetToInfoMap.end());
5489 return I->second;
5490}
5491
5492// Specialized value symbol table parser used when reading module index
Teresa Johnson403a7872015-10-04 14:33:43 +00005493// blocks where we don't actually create global values.
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005494// At the end of this routine the module index is populated with a map
5495// from global value name to GlobalValueInfo. The global value info contains
5496// the function block's bitcode offset (if applicable), or the offset into the
5497// summary section for the combined index.
Teresa Johnson26ab5772016-03-15 00:04:37 +00005498std::error_code ModuleSummaryIndexBitcodeReader::parseValueSymbolTable(
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005499 uint64_t Offset,
5500 DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap) {
5501 assert(Offset > 0 && "Expected non-zero VST offset");
5502 uint64_t CurrentBit = jumpToValueSymbolTable(Offset, Stream);
5503
Teresa Johnson403a7872015-10-04 14:33:43 +00005504 if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
5505 return error("Invalid record");
5506
5507 SmallVector<uint64_t, 64> Record;
5508
5509 // Read all the records for this value table.
5510 SmallString<128> ValueName;
5511 while (1) {
5512 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
5513
5514 switch (Entry.Kind) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005515 case BitstreamEntry::SubBlock: // Handled for us already.
5516 case BitstreamEntry::Error:
5517 return error("Malformed block");
5518 case BitstreamEntry::EndBlock:
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005519 // Done parsing VST, jump back to wherever we came from.
5520 Stream.JumpToBit(CurrentBit);
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005521 return std::error_code();
5522 case BitstreamEntry::Record:
5523 // The interesting case.
5524 break;
Teresa Johnson403a7872015-10-04 14:33:43 +00005525 }
5526
5527 // Read a record.
5528 Record.clear();
5529 switch (Stream.readRecord(Entry.ID, Record)) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005530 default: // Default behavior: ignore (e.g. VST_CODE_BBENTRY records).
5531 break;
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005532 case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N]
5533 if (convertToString(Record, 1, ValueName))
5534 return error("Invalid record");
5535 unsigned ValueID = Record[0];
5536 std::unique_ptr<GlobalValueInfo> GlobalValInfo =
5537 llvm::make_unique<GlobalValueInfo>();
5538 assert(!SourceFileName.empty());
5539 auto VLI = ValueIdToLinkageMap.find(ValueID);
5540 assert(VLI != ValueIdToLinkageMap.end() &&
5541 "No linkage found for VST entry?");
Teresa Johnsonb43027d2016-03-15 02:13:19 +00005542 std::string GlobalId = GlobalValue::getGlobalIdentifier(
5543 ValueName, VLI->second, SourceFileName);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005544 TheIndex->addGlobalValueInfo(GlobalId, std::move(GlobalValInfo));
Teresa Johnsonb43027d2016-03-15 02:13:19 +00005545 ValueIdToCallGraphGUIDMap[ValueID] = GlobalValue::getGUID(GlobalId);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005546 ValueName.clear();
5547 break;
5548 }
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005549 case bitc::VST_CODE_FNENTRY: {
Teresa Johnson79d4e2f2016-02-10 15:02:51 +00005550 // VST_CODE_FNENTRY: [valueid, offset, namechar x N]
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005551 if (convertToString(Record, 2, ValueName))
5552 return error("Invalid record");
5553 unsigned ValueID = Record[0];
5554 uint64_t FuncOffset = Record[1];
Teresa Johnsone1164de2016-02-10 21:55:02 +00005555 assert(!IsLazy && "Lazy summary read only supported for combined index");
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005556 std::unique_ptr<GlobalValueInfo> FuncInfo =
5557 llvm::make_unique<GlobalValueInfo>(FuncOffset);
5558 assert(!SourceFileName.empty());
5559 auto VLI = ValueIdToLinkageMap.find(ValueID);
5560 assert(VLI != ValueIdToLinkageMap.end() &&
5561 "No linkage found for VST entry?");
Teresa Johnsonb43027d2016-03-15 02:13:19 +00005562 std::string FunctionGlobalId = GlobalValue::getGlobalIdentifier(
5563 ValueName, VLI->second, SourceFileName);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005564 TheIndex->addGlobalValueInfo(FunctionGlobalId, std::move(FuncInfo));
Teresa Johnsonb43027d2016-03-15 02:13:19 +00005565 ValueIdToCallGraphGUIDMap[ValueID] =
5566 GlobalValue::getGUID(FunctionGlobalId);
Teresa Johnson403a7872015-10-04 14:33:43 +00005567
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005568 ValueName.clear();
5569 break;
5570 }
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005571 case bitc::VST_CODE_COMBINED_GVDEFENTRY: {
5572 // VST_CODE_COMBINED_GVDEFENTRY: [valueid, offset, guid]
5573 unsigned ValueID = Record[0];
5574 uint64_t GlobalValSummaryOffset = Record[1];
5575 uint64_t GlobalValGUID = Record[2];
5576 std::unique_ptr<GlobalValueInfo> GlobalValInfo =
5577 llvm::make_unique<GlobalValueInfo>(GlobalValSummaryOffset);
5578 SummaryOffsetToInfoMap[GlobalValSummaryOffset] = GlobalValInfo.get();
5579 TheIndex->addGlobalValueInfo(GlobalValGUID, std::move(GlobalValInfo));
5580 ValueIdToCallGraphGUIDMap[ValueID] = GlobalValGUID;
5581 break;
5582 }
5583 case bitc::VST_CODE_COMBINED_ENTRY: {
5584 // VST_CODE_COMBINED_ENTRY: [valueid, refguid]
5585 unsigned ValueID = Record[0];
5586 uint64_t RefGUID = Record[1];
5587 ValueIdToCallGraphGUIDMap[ValueID] = RefGUID;
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005588 break;
5589 }
Teresa Johnson403a7872015-10-04 14:33:43 +00005590 }
5591 }
5592}
5593
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005594// Parse just the blocks needed for building the index out of the module.
5595// At the end of this routine the module Index is populated with a map
5596// from global value name to GlobalValueInfo. The global value info contains
5597// either the parsed summary information (when parsing summaries
5598// eagerly), or just to the summary record's offset
Teresa Johnson403a7872015-10-04 14:33:43 +00005599// if parsing lazily (IsLazy).
Teresa Johnson26ab5772016-03-15 00:04:37 +00005600std::error_code ModuleSummaryIndexBitcodeReader::parseModule() {
Teresa Johnson403a7872015-10-04 14:33:43 +00005601 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
5602 return error("Invalid record");
5603
Teresa Johnsone1164de2016-02-10 21:55:02 +00005604 SmallVector<uint64_t, 64> Record;
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005605 DenseMap<unsigned, GlobalValue::LinkageTypes> ValueIdToLinkageMap;
5606 unsigned ValueId = 0;
Teresa Johnsone1164de2016-02-10 21:55:02 +00005607
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005608 // Read the index for this module.
Teresa Johnson403a7872015-10-04 14:33:43 +00005609 while (1) {
5610 BitstreamEntry Entry = Stream.advance();
5611
5612 switch (Entry.Kind) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005613 case BitstreamEntry::Error:
5614 return error("Malformed block");
5615 case BitstreamEntry::EndBlock:
5616 return std::error_code();
5617
5618 case BitstreamEntry::SubBlock:
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005619 if (CheckGlobalValSummaryPresenceOnly) {
5620 if (Entry.ID == bitc::GLOBALVAL_SUMMARY_BLOCK_ID) {
5621 SeenGlobalValSummary = true;
Teresa Johnson6290dbc2015-11-21 21:55:48 +00005622 // No need to parse the rest since we found the summary.
5623 return std::error_code();
5624 }
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005625 if (Stream.SkipBlock())
5626 return error("Invalid record");
Teresa Johnson6290dbc2015-11-21 21:55:48 +00005627 continue;
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005628 }
5629 switch (Entry.ID) {
5630 default: // Skip unknown content.
5631 if (Stream.SkipBlock())
5632 return error("Invalid record");
5633 break;
5634 case bitc::BLOCKINFO_BLOCK_ID:
5635 // Need to parse these to get abbrev ids (e.g. for VST)
5636 if (Stream.ReadBlockInfoBlock())
5637 return error("Malformed block");
5638 break;
5639 case bitc::VALUE_SYMTAB_BLOCK_ID:
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005640 // Should have been parsed earlier via VSTOffset, unless there
5641 // is no summary section.
5642 assert(((SeenValueSymbolTable && VSTOffset > 0) ||
5643 !SeenGlobalValSummary) &&
5644 "Expected early VST parse via VSTOffset record");
5645 if (Stream.SkipBlock())
5646 return error("Invalid record");
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005647 break;
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005648 case bitc::GLOBALVAL_SUMMARY_BLOCK_ID:
5649 assert(VSTOffset > 0 && "Expected non-zero VST offset");
5650 assert(!SeenValueSymbolTable &&
5651 "Already read VST when parsing summary block?");
5652 if (std::error_code EC =
5653 parseValueSymbolTable(VSTOffset, ValueIdToLinkageMap))
5654 return EC;
5655 SeenValueSymbolTable = true;
5656 SeenGlobalValSummary = true;
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005657 if (IsLazy) {
5658 // Lazy parsing of summary info, skip it.
5659 if (Stream.SkipBlock())
5660 return error("Invalid record");
5661 } else if (std::error_code EC = parseEntireSummary())
5662 return EC;
5663 break;
5664 case bitc::MODULE_STRTAB_BLOCK_ID:
5665 if (std::error_code EC = parseModuleStringTable())
5666 return EC;
5667 break;
5668 }
5669 continue;
Teresa Johnson403a7872015-10-04 14:33:43 +00005670
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005671 case BitstreamEntry::Record:
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005672 // Once we find the last record of interest, skip the rest.
5673 if (VSTOffset > 0)
Teresa Johnsone1164de2016-02-10 21:55:02 +00005674 Stream.skipRecord(Entry.ID);
5675 else {
5676 Record.clear();
5677 auto BitCode = Stream.readRecord(Entry.ID, Record);
5678 switch (BitCode) {
5679 default:
5680 break; // Default behavior, ignore unknown content.
5681 /// MODULE_CODE_SOURCE_FILENAME: [namechar x N]
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005682 case bitc::MODULE_CODE_SOURCE_FILENAME: {
Teresa Johnsone1164de2016-02-10 21:55:02 +00005683 SmallString<128> ValueName;
5684 if (convertToString(Record, 0, ValueName))
5685 return error("Invalid record");
5686 SourceFileName = ValueName.c_str();
5687 break;
5688 }
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005689 /// MODULE_CODE_VSTOFFSET: [offset]
5690 case bitc::MODULE_CODE_VSTOFFSET:
5691 if (Record.size() < 1)
5692 return error("Invalid record");
5693 VSTOffset = Record[0];
5694 break;
5695 // GLOBALVAR: [pointer type, isconst, initid,
5696 // linkage, alignment, section, visibility, threadlocal,
5697 // unnamed_addr, externally_initialized, dllstorageclass,
5698 // comdat]
5699 case bitc::MODULE_CODE_GLOBALVAR: {
5700 if (Record.size() < 6)
5701 return error("Invalid record");
5702 uint64_t RawLinkage = Record[3];
5703 GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage);
5704 ValueIdToLinkageMap[ValueId++] = Linkage;
5705 break;
5706 }
5707 // FUNCTION: [type, callingconv, isproto, linkage, paramattr,
5708 // alignment, section, visibility, gc, unnamed_addr,
5709 // prologuedata, dllstorageclass, comdat, prefixdata]
5710 case bitc::MODULE_CODE_FUNCTION: {
5711 if (Record.size() < 8)
5712 return error("Invalid record");
5713 uint64_t RawLinkage = Record[3];
5714 GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage);
5715 ValueIdToLinkageMap[ValueId++] = Linkage;
5716 break;
5717 }
5718 // ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility,
5719 // dllstorageclass]
5720 case bitc::MODULE_CODE_ALIAS: {
5721 if (Record.size() < 6)
5722 return error("Invalid record");
5723 uint64_t RawLinkage = Record[3];
5724 GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage);
5725 ValueIdToLinkageMap[ValueId++] = Linkage;
5726 break;
5727 }
5728 }
Teresa Johnsone1164de2016-02-10 21:55:02 +00005729 }
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005730 continue;
Teresa Johnson403a7872015-10-04 14:33:43 +00005731 }
5732 }
5733}
5734
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005735// Eagerly parse the entire summary block. This populates the GlobalValueSummary
5736// objects in the index.
Teresa Johnson26ab5772016-03-15 00:04:37 +00005737std::error_code ModuleSummaryIndexBitcodeReader::parseEntireSummary() {
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005738 if (Stream.EnterSubBlock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID))
Teresa Johnson403a7872015-10-04 14:33:43 +00005739 return error("Invalid record");
5740
5741 SmallVector<uint64_t, 64> Record;
5742
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005743 bool Combined = false;
Teresa Johnson403a7872015-10-04 14:33:43 +00005744 while (1) {
5745 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
5746
5747 switch (Entry.Kind) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005748 case BitstreamEntry::SubBlock: // Handled for us already.
5749 case BitstreamEntry::Error:
5750 return error("Malformed block");
5751 case BitstreamEntry::EndBlock:
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005752 // For a per-module index, remove any entries that still have empty
5753 // summaries. The VST parsing creates entries eagerly for all symbols,
5754 // but not all have associated summaries (e.g. it doesn't know how to
5755 // distinguish between VST_CODE_ENTRY for function declarations vs global
5756 // variables with initializers that end up with a summary). Remove those
5757 // entries now so that we don't need to rely on the combined index merger
5758 // to clean them up (especially since that may not run for the first
5759 // module's index if we merge into that).
5760 if (!Combined)
5761 TheIndex->removeEmptySummaryEntries();
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005762 return std::error_code();
5763 case BitstreamEntry::Record:
5764 // The interesting case.
5765 break;
Teresa Johnson403a7872015-10-04 14:33:43 +00005766 }
5767
5768 // Read a record. The record format depends on whether this
5769 // is a per-module index or a combined index file. In the per-module
5770 // case the records contain the associated value's ID for correlation
5771 // with VST entries. In the combined index the correlation is done
5772 // via the bitcode offset of the summary records (which were saved
5773 // in the combined index VST entries). The records also contain
5774 // information used for ThinLTO renaming and importing.
5775 Record.clear();
5776 uint64_t CurRecordBit = Stream.GetCurrentBitNo();
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005777 auto BitCode = Stream.readRecord(Entry.ID, Record);
5778 switch (BitCode) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005779 default: // Default behavior: ignore.
5780 break;
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005781 // FS_PERMODULE: [valueid, linkage, instcount, numrefs, numrefs x valueid,
5782 // n x (valueid, callsitecount)]
5783 // FS_PERMODULE_PROFILE: [valueid, linkage, instcount, numrefs,
5784 // numrefs x valueid,
5785 // n x (valueid, callsitecount, profilecount)]
5786 case bitc::FS_PERMODULE:
5787 case bitc::FS_PERMODULE_PROFILE: {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005788 unsigned ValueID = Record[0];
Teresa Johnson5e22e442016-02-06 16:07:35 +00005789 uint64_t RawLinkage = Record[1];
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005790 unsigned InstCount = Record[2];
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005791 unsigned NumRefs = Record[3];
5792 std::unique_ptr<FunctionSummary> FS = llvm::make_unique<FunctionSummary>(
5793 getDecodedLinkage(RawLinkage), InstCount);
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005794 // The module path string ref set in the summary must be owned by the
5795 // index's module string table. Since we don't have a module path
5796 // string table section in the per-module index, we create a single
5797 // module path string table entry with an empty (0) ID to take
5798 // ownership.
5799 FS->setModulePath(
5800 TheIndex->addModulePath(Buffer->getBufferIdentifier(), 0));
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005801 static int RefListStartIndex = 4;
5802 int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs;
5803 assert(Record.size() >= RefListStartIndex + NumRefs &&
5804 "Record size inconsistent with number of references");
5805 for (unsigned I = 4, E = CallGraphEdgeStartIndex; I != E; ++I) {
5806 unsigned RefValueId = Record[I];
5807 uint64_t RefGUID = getGUIDFromValueId(RefValueId);
5808 FS->addRefEdge(RefGUID);
5809 }
5810 bool HasProfile = (BitCode == bitc::FS_PERMODULE_PROFILE);
5811 for (unsigned I = CallGraphEdgeStartIndex, E = Record.size(); I != E;
5812 ++I) {
5813 unsigned CalleeValueId = Record[I];
5814 unsigned CallsiteCount = Record[++I];
5815 uint64_t ProfileCount = HasProfile ? Record[++I] : 0;
5816 uint64_t CalleeGUID = getGUIDFromValueId(CalleeValueId);
5817 FS->addCallGraphEdge(CalleeGUID,
5818 CalleeInfo(CallsiteCount, ProfileCount));
5819 }
5820 uint64_t GUID = getGUIDFromValueId(ValueID);
5821 auto InfoList = TheIndex->findGlobalValueInfoList(GUID);
5822 assert(InfoList != TheIndex->end() &&
5823 "Expected VST parse to create GlobalValueInfo entry");
5824 assert(InfoList->second.size() == 1 &&
5825 "Expected a single GlobalValueInfo per GUID in module");
5826 auto &Info = InfoList->second[0];
5827 assert(!Info->summary() && "Expected a single summary per VST entry");
5828 Info->setSummary(std::move(FS));
Teresa Johnsonbbe05452016-02-24 17:57:28 +00005829 break;
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005830 }
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005831 // FS_PERMODULE_GLOBALVAR_INIT_REFS: [valueid, linkage, n x valueid]
5832 case bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS: {
5833 unsigned ValueID = Record[0];
5834 uint64_t RawLinkage = Record[1];
5835 std::unique_ptr<GlobalVarSummary> FS =
5836 llvm::make_unique<GlobalVarSummary>(getDecodedLinkage(RawLinkage));
5837 FS->setModulePath(
5838 TheIndex->addModulePath(Buffer->getBufferIdentifier(), 0));
5839 for (unsigned I = 2, E = Record.size(); I != E; ++I) {
5840 unsigned RefValueId = Record[I];
5841 uint64_t RefGUID = getGUIDFromValueId(RefValueId);
5842 FS->addRefEdge(RefGUID);
5843 }
5844 uint64_t GUID = getGUIDFromValueId(ValueID);
5845 auto InfoList = TheIndex->findGlobalValueInfoList(GUID);
5846 assert(InfoList != TheIndex->end() &&
5847 "Expected VST parse to create GlobalValueInfo entry");
5848 assert(InfoList->second.size() == 1 &&
5849 "Expected a single GlobalValueInfo per GUID in module");
5850 auto &Info = InfoList->second[0];
5851 assert(!Info->summary() && "Expected a single summary per VST entry");
5852 Info->setSummary(std::move(FS));
5853 break;
5854 }
5855 // FS_COMBINED: [modid, linkage, instcount, numrefs, numrefs x valueid,
5856 // n x (valueid, callsitecount)]
5857 // FS_COMBINED_PROFILE: [modid, linkage, instcount, numrefs,
5858 // numrefs x valueid,
5859 // n x (valueid, callsitecount, profilecount)]
5860 case bitc::FS_COMBINED:
5861 case bitc::FS_COMBINED_PROFILE: {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005862 uint64_t ModuleId = Record[0];
Teresa Johnson5e22e442016-02-06 16:07:35 +00005863 uint64_t RawLinkage = Record[1];
5864 unsigned InstCount = Record[2];
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005865 unsigned NumRefs = Record[3];
5866 std::unique_ptr<FunctionSummary> FS = llvm::make_unique<FunctionSummary>(
5867 getDecodedLinkage(RawLinkage), InstCount);
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005868 FS->setModulePath(ModuleIdMap[ModuleId]);
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00005869 static int RefListStartIndex = 4;
5870 int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs;
5871 assert(Record.size() >= RefListStartIndex + NumRefs &&
5872 "Record size inconsistent with number of references");
5873 for (unsigned I = 4, E = CallGraphEdgeStartIndex; I != E; ++I) {
5874 unsigned RefValueId = Record[I];
5875 uint64_t RefGUID = getGUIDFromValueId(RefValueId);
5876 FS->addRefEdge(RefGUID);
5877 }
5878 bool HasProfile = (BitCode == bitc::FS_COMBINED_PROFILE);
5879 for (unsigned I = CallGraphEdgeStartIndex, E = Record.size(); I != E;
5880 ++I) {
5881 unsigned CalleeValueId = Record[I];
5882 unsigned CallsiteCount = Record[++I];
5883 uint64_t ProfileCount = HasProfile ? Record[++I] : 0;
5884 uint64_t CalleeGUID = getGUIDFromValueId(CalleeValueId);
5885 FS->addCallGraphEdge(CalleeGUID,
5886 CalleeInfo(CallsiteCount, ProfileCount));
5887 }
5888 auto *Info = getInfoFromSummaryOffset(CurRecordBit);
5889 assert(!Info->summary() && "Expected a single summary per VST entry");
5890 Info->setSummary(std::move(FS));
5891 Combined = true;
5892 break;
5893 }
5894 // FS_COMBINED_GLOBALVAR_INIT_REFS: [modid, linkage, n x valueid]
5895 case bitc::FS_COMBINED_GLOBALVAR_INIT_REFS: {
5896 uint64_t ModuleId = Record[0];
5897 uint64_t RawLinkage = Record[1];
5898 std::unique_ptr<GlobalVarSummary> FS =
5899 llvm::make_unique<GlobalVarSummary>(getDecodedLinkage(RawLinkage));
5900 FS->setModulePath(ModuleIdMap[ModuleId]);
5901 for (unsigned I = 2, E = Record.size(); I != E; ++I) {
5902 unsigned RefValueId = Record[I];
5903 uint64_t RefGUID = getGUIDFromValueId(RefValueId);
5904 FS->addRefEdge(RefGUID);
5905 }
5906 auto *Info = getInfoFromSummaryOffset(CurRecordBit);
5907 assert(!Info->summary() && "Expected a single summary per VST entry");
5908 Info->setSummary(std::move(FS));
5909 Combined = true;
Teresa Johnsonbbe05452016-02-24 17:57:28 +00005910 break;
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005911 }
Teresa Johnson403a7872015-10-04 14:33:43 +00005912 }
5913 }
5914 llvm_unreachable("Exit infinite loop");
5915}
5916
5917// Parse the module string table block into the Index.
5918// This populates the ModulePathStringTable map in the index.
Teresa Johnson26ab5772016-03-15 00:04:37 +00005919std::error_code ModuleSummaryIndexBitcodeReader::parseModuleStringTable() {
Teresa Johnson403a7872015-10-04 14:33:43 +00005920 if (Stream.EnterSubBlock(bitc::MODULE_STRTAB_BLOCK_ID))
5921 return error("Invalid record");
5922
5923 SmallVector<uint64_t, 64> Record;
5924
5925 SmallString<128> ModulePath;
5926 while (1) {
5927 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
5928
5929 switch (Entry.Kind) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005930 case BitstreamEntry::SubBlock: // Handled for us already.
5931 case BitstreamEntry::Error:
5932 return error("Malformed block");
5933 case BitstreamEntry::EndBlock:
5934 return std::error_code();
5935 case BitstreamEntry::Record:
5936 // The interesting case.
5937 break;
Teresa Johnson403a7872015-10-04 14:33:43 +00005938 }
5939
5940 Record.clear();
5941 switch (Stream.readRecord(Entry.ID, Record)) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005942 default: // Default behavior: ignore.
5943 break;
5944 case bitc::MST_CODE_ENTRY: {
5945 // MST_ENTRY: [modid, namechar x N]
5946 if (convertToString(Record, 1, ModulePath))
5947 return error("Invalid record");
5948 uint64_t ModuleId = Record[0];
5949 StringRef ModulePathInMap = TheIndex->addModulePath(ModulePath, ModuleId);
5950 ModuleIdMap[ModuleId] = ModulePathInMap;
5951 ModulePath.clear();
5952 break;
5953 }
Teresa Johnson403a7872015-10-04 14:33:43 +00005954 }
5955 }
5956 llvm_unreachable("Exit infinite loop");
5957}
5958
5959// Parse the function info index from the bitcode streamer into the given index.
Teresa Johnson26ab5772016-03-15 00:04:37 +00005960std::error_code ModuleSummaryIndexBitcodeReader::parseSummaryIndexInto(
5961 std::unique_ptr<DataStreamer> Streamer, ModuleSummaryIndex *I) {
Teresa Johnson403a7872015-10-04 14:33:43 +00005962 TheIndex = I;
5963
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005964 if (std::error_code EC = initStream(std::move(Streamer)))
5965 return EC;
Teresa Johnson403a7872015-10-04 14:33:43 +00005966
5967 // Sniff for the signature.
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005968 if (!hasValidBitcodeHeader(Stream))
5969 return error("Invalid bitcode signature");
Teresa Johnson403a7872015-10-04 14:33:43 +00005970
5971 // We expect a number of well-defined blocks, though we don't necessarily
5972 // need to understand them all.
5973 while (1) {
5974 if (Stream.AtEndOfStream()) {
5975 // We didn't really read a proper Module block.
5976 return error("Malformed block");
5977 }
5978
5979 BitstreamEntry Entry =
5980 Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
5981
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005982 if (Entry.Kind != BitstreamEntry::SubBlock)
5983 return error("Malformed block");
Teresa Johnson403a7872015-10-04 14:33:43 +00005984
5985 // If we see a MODULE_BLOCK, parse it to find the blocks needed for
5986 // building the function summary index.
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005987 if (Entry.ID == bitc::MODULE_BLOCK_ID)
5988 return parseModule();
Teresa Johnson403a7872015-10-04 14:33:43 +00005989
Teresa Johnsonf72278f2015-11-02 18:02:11 +00005990 if (Stream.SkipBlock())
5991 return error("Invalid record");
Teresa Johnson403a7872015-10-04 14:33:43 +00005992 }
5993}
5994
Teresa Johnson26ab5772016-03-15 00:04:37 +00005995// Parse the summary information at the given offset in the buffer into
5996// the index. Used to support lazy parsing of summaries from the
Teresa Johnson403a7872015-10-04 14:33:43 +00005997// combined index during importing.
5998// TODO: This function is not yet complete as it won't have a consumer
5999// until ThinLTO function importing is added.
Teresa Johnson26ab5772016-03-15 00:04:37 +00006000std::error_code ModuleSummaryIndexBitcodeReader::parseGlobalValueSummary(
6001 std::unique_ptr<DataStreamer> Streamer, ModuleSummaryIndex *I,
6002 size_t SummaryOffset) {
Teresa Johnson403a7872015-10-04 14:33:43 +00006003 TheIndex = I;
6004
Teresa Johnsonf72278f2015-11-02 18:02:11 +00006005 if (std::error_code EC = initStream(std::move(Streamer)))
6006 return EC;
Teresa Johnson403a7872015-10-04 14:33:43 +00006007
6008 // Sniff for the signature.
Teresa Johnsonf72278f2015-11-02 18:02:11 +00006009 if (!hasValidBitcodeHeader(Stream))
6010 return error("Invalid bitcode signature");
Teresa Johnson403a7872015-10-04 14:33:43 +00006011
Teresa Johnson26ab5772016-03-15 00:04:37 +00006012 Stream.JumpToBit(SummaryOffset);
Teresa Johnson403a7872015-10-04 14:33:43 +00006013
6014 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
6015
6016 switch (Entry.Kind) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00006017 default:
6018 return error("Malformed block");
6019 case BitstreamEntry::Record:
6020 // The expected case.
6021 break;
Teresa Johnson403a7872015-10-04 14:33:43 +00006022 }
6023
6024 // TODO: Read a record. This interface will be completed when ThinLTO
6025 // importing is added so that it can be tested.
6026 SmallVector<uint64_t, 64> Record;
6027 switch (Stream.readRecord(Entry.ID, Record)) {
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00006028 case bitc::FS_COMBINED:
6029 case bitc::FS_COMBINED_PROFILE:
6030 case bitc::FS_COMBINED_GLOBALVAR_INIT_REFS:
Teresa Johnsonf72278f2015-11-02 18:02:11 +00006031 default:
6032 return error("Invalid record");
Teresa Johnson403a7872015-10-04 14:33:43 +00006033 }
6034
6035 return std::error_code();
6036}
6037
Teresa Johnson26ab5772016-03-15 00:04:37 +00006038std::error_code ModuleSummaryIndexBitcodeReader::initStream(
6039 std::unique_ptr<DataStreamer> Streamer) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00006040 if (Streamer)
6041 return initLazyStream(std::move(Streamer));
Teresa Johnson403a7872015-10-04 14:33:43 +00006042 return initStreamFromBuffer();
6043}
6044
Teresa Johnson26ab5772016-03-15 00:04:37 +00006045std::error_code ModuleSummaryIndexBitcodeReader::initStreamFromBuffer() {
Teresa Johnson403a7872015-10-04 14:33:43 +00006046 const unsigned char *BufPtr = (const unsigned char *)Buffer->getBufferStart();
6047 const unsigned char *BufEnd = BufPtr + Buffer->getBufferSize();
6048
Teresa Johnsonf72278f2015-11-02 18:02:11 +00006049 if (Buffer->getBufferSize() & 3)
6050 return error("Invalid bitcode signature");
Teresa Johnson403a7872015-10-04 14:33:43 +00006051
6052 // If we have a wrapper header, parse it and ignore the non-bc file contents.
6053 // The magic number is 0x0B17C0DE stored in little endian.
6054 if (isBitcodeWrapper(BufPtr, BufEnd))
6055 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
6056 return error("Invalid bitcode wrapper header");
6057
6058 StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
6059 Stream.init(&*StreamFile);
6060
6061 return std::error_code();
6062}
6063
Teresa Johnson26ab5772016-03-15 00:04:37 +00006064std::error_code ModuleSummaryIndexBitcodeReader::initLazyStream(
Teresa Johnson403a7872015-10-04 14:33:43 +00006065 std::unique_ptr<DataStreamer> Streamer) {
6066 // Check and strip off the bitcode wrapper; BitstreamReader expects never to
6067 // see it.
6068 auto OwnedBytes =
6069 llvm::make_unique<StreamingMemoryObject>(std::move(Streamer));
6070 StreamingMemoryObject &Bytes = *OwnedBytes;
6071 StreamFile = llvm::make_unique<BitstreamReader>(std::move(OwnedBytes));
6072 Stream.init(&*StreamFile);
6073
6074 unsigned char buf[16];
6075 if (Bytes.readBytes(buf, 16, 0) != 16)
6076 return error("Invalid bitcode signature");
6077
Teresa Johnsonf72278f2015-11-02 18:02:11 +00006078 if (!isBitcode(buf, buf + 16))
6079 return error("Invalid bitcode signature");
Teresa Johnson403a7872015-10-04 14:33:43 +00006080
6081 if (isBitcodeWrapper(buf, buf + 4)) {
6082 const unsigned char *bitcodeStart = buf;
6083 const unsigned char *bitcodeEnd = buf + 16;
6084 SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
6085 Bytes.dropLeadingBytes(bitcodeStart - buf);
6086 Bytes.setKnownObjectSize(bitcodeEnd - bitcodeStart);
6087 }
6088 return std::error_code();
6089}
6090
Rafael Espindola48da4f42013-11-04 16:16:24 +00006091namespace {
Rafael Espindola25188c92014-06-12 01:45:43 +00006092class BitcodeErrorCategoryType : public std::error_category {
Rafael Espindolaf5d07fa2014-06-10 21:26:47 +00006093 const char *name() const LLVM_NOEXCEPT override {
Rafael Espindola48da4f42013-11-04 16:16:24 +00006094 return "llvm.bitcode";
6095 }
Craig Topper73156022014-03-02 09:09:27 +00006096 std::string message(int IE) const override {
Rafael Espindolac3f2e732014-07-29 20:22:46 +00006097 BitcodeError E = static_cast<BitcodeError>(IE);
Rafael Espindola48da4f42013-11-04 16:16:24 +00006098 switch (E) {
Rafael Espindolac3f2e732014-07-29 20:22:46 +00006099 case BitcodeError::InvalidBitcodeSignature:
Rafael Espindola48da4f42013-11-04 16:16:24 +00006100 return "Invalid bitcode signature";
Rafael Espindolad0b23be2015-01-10 00:07:30 +00006101 case BitcodeError::CorruptedBitcode:
6102 return "Corrupted bitcode";
Rafael Espindola48da4f42013-11-04 16:16:24 +00006103 }
Benjamin Kramer77db1632013-11-05 13:45:09 +00006104 llvm_unreachable("Unknown error type!");
Rafael Espindola48da4f42013-11-04 16:16:24 +00006105 }
6106};
Eugene Zelenko6ac3f732016-01-26 18:48:36 +00006107} // end anonymous namespace
Rafael Espindola48da4f42013-11-04 16:16:24 +00006108
Chris Bieneman770163e2014-09-19 20:29:02 +00006109static ManagedStatic<BitcodeErrorCategoryType> ErrorCategory;
6110
Rafael Espindolac3f2e732014-07-29 20:22:46 +00006111const std::error_category &llvm::BitcodeErrorCategory() {
Chris Bieneman770163e2014-09-19 20:29:02 +00006112 return *ErrorCategory;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00006113}
Chris Lattner51ffe7c2007-05-01 04:59:48 +00006114
Chris Lattner6694f602007-04-29 07:54:31 +00006115//===----------------------------------------------------------------------===//
6116// External interface
6117//===----------------------------------------------------------------------===//
6118
Rafael Espindola456baad2015-06-17 01:15:47 +00006119static ErrorOr<std::unique_ptr<Module>>
6120getBitcodeModuleImpl(std::unique_ptr<DataStreamer> Streamer, StringRef Name,
6121 BitcodeReader *R, LLVMContext &Context,
6122 bool MaterializeAll, bool ShouldLazyLoadMetadata) {
6123 std::unique_ptr<Module> M = make_unique<Module>(Name, Context);
6124 M->setMaterializer(R);
6125
6126 auto cleanupOnError = [&](std::error_code EC) {
6127 R->releaseBuffer(); // Never take ownership on error.
6128 return EC;
6129 };
6130
6131 // Delay parsing Metadata if ShouldLazyLoadMetadata is true.
6132 if (std::error_code EC = R->parseBitcodeInto(std::move(Streamer), M.get(),
6133 ShouldLazyLoadMetadata))
6134 return cleanupOnError(EC);
6135
6136 if (MaterializeAll) {
6137 // Read in the entire module, and destroy the BitcodeReader.
Rafael Espindolac4a03482015-12-18 20:13:39 +00006138 if (std::error_code EC = M->materializeAll())
Rafael Espindola456baad2015-06-17 01:15:47 +00006139 return cleanupOnError(EC);
6140 } else {
6141 // Resolve forward references from blockaddresses.
6142 if (std::error_code EC = R->materializeForwardReferencedFunctions())
6143 return cleanupOnError(EC);
6144 }
6145 return std::move(M);
6146}
6147
Duncan P. N. Exon Smith6e1009b2014-08-01 22:27:19 +00006148/// \brief Get a lazy one-at-time loading module from bitcode.
Chris Lattner6694f602007-04-29 07:54:31 +00006149///
Duncan P. N. Exon Smith6e1009b2014-08-01 22:27:19 +00006150/// This isn't always used in a lazy context. In particular, it's also used by
6151/// \a parseBitcodeFile(). If this is truly lazy, then we need to eagerly pull
6152/// in forward-referenced functions from block address references.
6153///
Rafael Espindola728074b2015-06-17 00:40:56 +00006154/// \param[in] MaterializeAll Set to \c true if we should materialize
6155/// everything.
Rafael Espindoladcd1dca2015-06-16 22:27:55 +00006156static ErrorOr<std::unique_ptr<Module>>
Rafael Espindola68812152014-09-03 17:31:46 +00006157getLazyBitcodeModuleImpl(std::unique_ptr<MemoryBuffer> &&Buffer,
Rafael Espindola728074b2015-06-17 00:40:56 +00006158 LLVMContext &Context, bool MaterializeAll,
Manman Ren4a9b0eb2015-03-13 19:24:30 +00006159 bool ShouldLazyLoadMetadata = false) {
Rafael Espindola9d2bfc42015-12-14 23:17:03 +00006160 BitcodeReader *R = new BitcodeReader(Buffer.get(), Context);
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00006161
Rafael Espindola456baad2015-06-17 01:15:47 +00006162 ErrorOr<std::unique_ptr<Module>> Ret =
6163 getBitcodeModuleImpl(nullptr, Buffer->getBufferIdentifier(), R, Context,
6164 MaterializeAll, ShouldLazyLoadMetadata);
6165 if (!Ret)
6166 return Ret;
Rafael Espindolab7993462012-01-02 07:49:53 +00006167
Rafael Espindolae2c1d772014-08-26 22:00:09 +00006168 Buffer.release(); // The BitcodeReader owns it now.
Rafael Espindola456baad2015-06-17 01:15:47 +00006169 return Ret;
Chris Lattner6694f602007-04-29 07:54:31 +00006170}
6171
Rafael Espindola9d2bfc42015-12-14 23:17:03 +00006172ErrorOr<std::unique_ptr<Module>>
6173llvm::getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer,
6174 LLVMContext &Context, bool ShouldLazyLoadMetadata) {
Rafael Espindolad0b23be2015-01-10 00:07:30 +00006175 return getLazyBitcodeModuleImpl(std::move(Buffer), Context, false,
Rafael Espindola9d2bfc42015-12-14 23:17:03 +00006176 ShouldLazyLoadMetadata);
Duncan P. N. Exon Smith6e1009b2014-08-01 22:27:19 +00006177}
Derek Schuff8b2dcad2012-02-06 22:30:29 +00006178
Rafael Espindola9d2bfc42015-12-14 23:17:03 +00006179ErrorOr<std::unique_ptr<Module>>
6180llvm::getStreamedBitcodeModule(StringRef Name,
6181 std::unique_ptr<DataStreamer> Streamer,
6182 LLVMContext &Context) {
Rafael Espindola7d727b52014-12-18 05:08:43 +00006183 std::unique_ptr<Module> M = make_unique<Module>(Name, Context);
Rafael Espindola9d2bfc42015-12-14 23:17:03 +00006184 BitcodeReader *R = new BitcodeReader(Context);
Rafael Espindola456baad2015-06-17 01:15:47 +00006185
6186 return getBitcodeModuleImpl(std::move(Streamer), Name, R, Context, false,
6187 false);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00006188}
6189
Rafael Espindola9d2bfc42015-12-14 23:17:03 +00006190ErrorOr<std::unique_ptr<Module>> llvm::parseBitcodeFile(MemoryBufferRef Buffer,
6191 LLVMContext &Context) {
Rafael Espindolad96d5532014-08-26 21:49:01 +00006192 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
Rafael Espindola9d2bfc42015-12-14 23:17:03 +00006193 return getLazyBitcodeModuleImpl(std::move(Buf), Context, true);
Chad Rosierca2567b2011-12-07 21:44:12 +00006194 // TODO: Restore the use-lists to the in-memory state when the bitcode was
6195 // written. We must defer until the Module has been fully materialized.
Chris Lattner6694f602007-04-29 07:54:31 +00006196}
Bill Wendling0198ce02010-10-06 01:22:42 +00006197
Rafael Espindola9d2bfc42015-12-14 23:17:03 +00006198std::string llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer,
6199 LLVMContext &Context) {
Rafael Espindolad96d5532014-08-26 21:49:01 +00006200 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
Rafael Espindola9d2bfc42015-12-14 23:17:03 +00006201 auto R = llvm::make_unique<BitcodeReader>(Buf.release(), Context);
Rafael Espindolac75c4fa2014-07-04 20:02:42 +00006202 ErrorOr<std::string> Triple = R->parseTriple();
Rafael Espindolad346cc82014-07-04 13:52:01 +00006203 if (Triple.getError())
6204 return "";
6205 return Triple.get();
Bill Wendling0198ce02010-10-06 01:22:42 +00006206}
Teresa Johnson403a7872015-10-04 14:33:43 +00006207
Rafael Espindola9d2bfc42015-12-14 23:17:03 +00006208std::string llvm::getBitcodeProducerString(MemoryBufferRef Buffer,
6209 LLVMContext &Context) {
Mehdi Amini3383ccc2015-11-09 02:46:41 +00006210 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
Rafael Espindola9d2bfc42015-12-14 23:17:03 +00006211 BitcodeReader R(Buf.release(), Context);
Mehdi Amini3383ccc2015-11-09 02:46:41 +00006212 ErrorOr<std::string> ProducerString = R.parseIdentificationBlock();
6213 if (ProducerString.getError())
6214 return "";
6215 return ProducerString.get();
6216}
6217
Teresa Johnson403a7872015-10-04 14:33:43 +00006218// Parse the specified bitcode buffer, returning the function info index.
6219// If IsLazy is false, parse the entire function summary into
6220// the index. Otherwise skip the function summary section, and only create
6221// an index object with a map from function name to function summary offset.
6222// The index is used to perform lazy function summary reading later.
Teresa Johnson26ab5772016-03-15 00:04:37 +00006223ErrorOr<std::unique_ptr<ModuleSummaryIndex>>
6224llvm::getModuleSummaryIndex(MemoryBufferRef Buffer,
6225 DiagnosticHandlerFunction DiagnosticHandler,
6226 bool IsLazy) {
Teresa Johnson403a7872015-10-04 14:33:43 +00006227 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
Teresa Johnson26ab5772016-03-15 00:04:37 +00006228 ModuleSummaryIndexBitcodeReader R(Buf.get(), DiagnosticHandler, IsLazy);
Teresa Johnson403a7872015-10-04 14:33:43 +00006229
Teresa Johnson26ab5772016-03-15 00:04:37 +00006230 auto Index = llvm::make_unique<ModuleSummaryIndex>();
Teresa Johnson403a7872015-10-04 14:33:43 +00006231
6232 auto cleanupOnError = [&](std::error_code EC) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00006233 R.releaseBuffer(); // Never take ownership on error.
Teresa Johnson403a7872015-10-04 14:33:43 +00006234 return EC;
6235 };
6236
6237 if (std::error_code EC = R.parseSummaryIndexInto(nullptr, Index.get()))
6238 return cleanupOnError(EC);
6239
Teresa Johnson26ab5772016-03-15 00:04:37 +00006240 Buf.release(); // The ModuleSummaryIndexBitcodeReader owns it now.
Teresa Johnson403a7872015-10-04 14:33:43 +00006241 return std::move(Index);
6242}
6243
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00006244// Check if the given bitcode buffer contains a global value summary block.
6245bool llvm::hasGlobalValueSummary(MemoryBufferRef Buffer,
6246 DiagnosticHandlerFunction DiagnosticHandler) {
Teresa Johnson403a7872015-10-04 14:33:43 +00006247 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
Teresa Johnson26ab5772016-03-15 00:04:37 +00006248 ModuleSummaryIndexBitcodeReader R(Buf.get(), DiagnosticHandler, false, true);
Teresa Johnson403a7872015-10-04 14:33:43 +00006249
6250 auto cleanupOnError = [&](std::error_code EC) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00006251 R.releaseBuffer(); // Never take ownership on error.
Teresa Johnson403a7872015-10-04 14:33:43 +00006252 return false;
6253 };
6254
6255 if (std::error_code EC = R.parseSummaryIndexInto(nullptr, nullptr))
6256 return cleanupOnError(EC);
6257
Teresa Johnson26ab5772016-03-15 00:04:37 +00006258 Buf.release(); // The ModuleSummaryIndexBitcodeReader owns it now.
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00006259 return R.foundGlobalValSummary();
Teresa Johnson403a7872015-10-04 14:33:43 +00006260}
6261
Teresa Johnson26ab5772016-03-15 00:04:37 +00006262// This method supports lazy reading of summary data from the combined
Teresa Johnson403a7872015-10-04 14:33:43 +00006263// index during ThinLTO function importing. When reading the combined index
Teresa Johnson26ab5772016-03-15 00:04:37 +00006264// file, getModuleSummaryIndex is first invoked with IsLazy=true.
6265// Then this method is called for each value considered for importing,
6266// to parse the summary information for the given value name into
Teresa Johnson403a7872015-10-04 14:33:43 +00006267// the index.
Teresa Johnson26ab5772016-03-15 00:04:37 +00006268std::error_code llvm::readGlobalValueSummary(
Mehdi Amini354f5202015-11-19 05:52:29 +00006269 MemoryBufferRef Buffer, DiagnosticHandlerFunction DiagnosticHandler,
Teresa Johnson26ab5772016-03-15 00:04:37 +00006270 StringRef ValueName, std::unique_ptr<ModuleSummaryIndex> Index) {
Teresa Johnson403a7872015-10-04 14:33:43 +00006271 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
Teresa Johnson26ab5772016-03-15 00:04:37 +00006272 ModuleSummaryIndexBitcodeReader R(Buf.get(), DiagnosticHandler);
Teresa Johnson403a7872015-10-04 14:33:43 +00006273
6274 auto cleanupOnError = [&](std::error_code EC) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +00006275 R.releaseBuffer(); // Never take ownership on error.
Teresa Johnson403a7872015-10-04 14:33:43 +00006276 return EC;
6277 };
6278
Teresa Johnson26ab5772016-03-15 00:04:37 +00006279 // Lookup the given value name in the GlobalValueMap, which may
6280 // contain a list of global value infos in the case of a COMDAT. Walk through
6281 // and parse each summary info at the summary offset
Teresa Johnson403a7872015-10-04 14:33:43 +00006282 // recorded when parsing the value symbol table.
Teresa Johnson26ab5772016-03-15 00:04:37 +00006283 for (const auto &FI : Index->getGlobalValueInfoList(ValueName)) {
6284 size_t SummaryOffset = FI->bitcodeIndex();
Teresa Johnson403a7872015-10-04 14:33:43 +00006285 if (std::error_code EC =
Teresa Johnson26ab5772016-03-15 00:04:37 +00006286 R.parseGlobalValueSummary(nullptr, Index.get(), SummaryOffset))
Teresa Johnson403a7872015-10-04 14:33:43 +00006287 return cleanupOnError(EC);
6288 }
6289
Teresa Johnson26ab5772016-03-15 00:04:37 +00006290 Buf.release(); // The ModuleSummaryIndexBitcodeReader owns it now.
Teresa Johnson403a7872015-10-04 14:33:43 +00006291 return std::error_code();
6292}