blob: f712d9db641b98e8ef60bb0c8dd68eacdfbc08e8 [file] [log] [blame]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001//===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnercaee0dc2007-04-22 06:23:29 +00007//
8//===----------------------------------------------------------------------===//
Chris Lattnercaee0dc2007-04-22 06:23:29 +00009
Chris Lattnerc453f762007-04-29 07:54:31 +000010#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattnercaee0dc2007-04-22 06:23:29 +000011#include "BitcodeReader.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000012#include "llvm/ADT/SmallString.h"
13#include "llvm/ADT/SmallVector.h"
Tobias Grossere7bc5bb2013-07-26 04:16:55 +000014#include "llvm/Bitcode/LLVMBitCodes.h"
Stephen Hines36b56882014-04-23 16:57:46 -070015#include "llvm/IR/AutoUpgrade.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000016#include "llvm/IR/Constants.h"
17#include "llvm/IR/DerivedTypes.h"
18#include "llvm/IR/InlineAsm.h"
19#include "llvm/IR/IntrinsicInst.h"
Manman Ren804f0342013-09-28 00:22:27 +000020#include "llvm/IR/LLVMContext.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000021#include "llvm/IR/Module.h"
22#include "llvm/IR/OperandTraits.h"
23#include "llvm/IR/Operator.h"
Derek Schuff2ea93872012-02-06 22:30:29 +000024#include "llvm/Support/DataStream.h"
Chris Lattner0eef0802007-04-24 04:04:35 +000025#include "llvm/Support/MathExtras.h"
Chris Lattnerc453f762007-04-29 07:54:31 +000026#include "llvm/Support/MemoryBuffer.h"
Tobias Grossere7bc5bb2013-07-26 04:16:55 +000027#include "llvm/Support/raw_ostream.h"
Chris Lattnercaee0dc2007-04-22 06:23:29 +000028using namespace llvm;
29
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +000030enum {
31 SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex
32};
33
Rafael Espindola47f79bb2012-01-02 07:49:53 +000034void BitcodeReader::materializeForwardReferencedFunctions() {
35 while (!BlockAddrFwdRefs.empty()) {
36 Function *F = BlockAddrFwdRefs.begin()->first;
37 F->Materialize();
38 }
39}
40
Chris Lattnerb348bb82007-05-18 04:02:46 +000041void BitcodeReader::FreeState() {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000042 if (BufferOwned)
43 delete Buffer;
Chris Lattnerb348bb82007-05-18 04:02:46 +000044 Buffer = 0;
Chris Lattner1afcace2011-07-09 17:41:24 +000045 std::vector<Type*>().swap(TypeList);
Chris Lattnerb348bb82007-05-18 04:02:46 +000046 ValueList.clear();
Devang Pateld5ac4042009-08-04 06:00:18 +000047 MDValueList.clear();
Daniel Dunbara279bc32009-09-20 02:20:51 +000048
Bill Wendling99faa3b2012-12-07 23:16:57 +000049 std::vector<AttributeSet>().swap(MAttributes);
Chris Lattnerb348bb82007-05-18 04:02:46 +000050 std::vector<BasicBlock*>().swap(FunctionBBs);
51 std::vector<Function*>().swap(FunctionsWithBodies);
52 DeferredFunctionInfo.clear();
Dan Gohman19538d12010-07-20 21:42:28 +000053 MDKindMap.clear();
Benjamin Kramer122f5e52012-09-21 14:34:31 +000054
55 assert(BlockAddrFwdRefs.empty() && "Unresolved blockaddress fwd references");
Chris Lattnerc453f762007-04-29 07:54:31 +000056}
57
Chris Lattner48c85b82007-05-04 03:30:17 +000058//===----------------------------------------------------------------------===//
59// Helper functions to implement forward reference resolution, etc.
60//===----------------------------------------------------------------------===//
Chris Lattnerc453f762007-04-29 07:54:31 +000061
Chris Lattnercaee0dc2007-04-22 06:23:29 +000062/// ConvertToString - Convert a string from a record into an std::string, return
63/// true on failure.
Chris Lattner0b2482a2007-04-23 21:26:05 +000064template<typename StrTy>
Benjamin Kramerf52aea82012-05-28 14:10:31 +000065static bool ConvertToString(ArrayRef<uint64_t> Record, unsigned Idx,
Chris Lattner0b2482a2007-04-23 21:26:05 +000066 StrTy &Result) {
Chris Lattner15e6d172007-05-04 19:11:41 +000067 if (Idx > Record.size())
Chris Lattnercaee0dc2007-04-22 06:23:29 +000068 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +000069
Chris Lattner15e6d172007-05-04 19:11:41 +000070 for (unsigned i = Idx, e = Record.size(); i != e; ++i)
71 Result += (char)Record[i];
Chris Lattnercaee0dc2007-04-22 06:23:29 +000072 return false;
73}
74
75static GlobalValue::LinkageTypes GetDecodedLinkage(unsigned Val) {
76 switch (Val) {
77 default: // Map unknown/new linkages to external
Bill Wendling3d10a5a2009-07-20 01:03:30 +000078 case 0: return GlobalValue::ExternalLinkage;
79 case 1: return GlobalValue::WeakAnyLinkage;
80 case 2: return GlobalValue::AppendingLinkage;
81 case 3: return GlobalValue::InternalLinkage;
82 case 4: return GlobalValue::LinkOnceAnyLinkage;
Stephen Hines36b56882014-04-23 16:57:46 -070083 case 5: return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage
84 case 6: return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage
Bill Wendling3d10a5a2009-07-20 01:03:30 +000085 case 7: return GlobalValue::ExternalWeakLinkage;
86 case 8: return GlobalValue::CommonLinkage;
87 case 9: return GlobalValue::PrivateLinkage;
Duncan Sands667d4b82009-03-07 15:45:40 +000088 case 10: return GlobalValue::WeakODRLinkage;
89 case 11: return GlobalValue::LinkOnceODRLinkage;
Chris Lattner266c7bb2009-04-13 05:44:34 +000090 case 12: return GlobalValue::AvailableExternallyLinkage;
Stephen Hines36b56882014-04-23 16:57:46 -070091 case 13:
92 return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage
93 case 14:
94 return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage
Chris Lattnercaee0dc2007-04-22 06:23:29 +000095 }
96}
97
98static GlobalValue::VisibilityTypes GetDecodedVisibility(unsigned Val) {
99 switch (Val) {
100 default: // Map unknown visibilities to default.
101 case 0: return GlobalValue::DefaultVisibility;
102 case 1: return GlobalValue::HiddenVisibility;
Anton Korobeynikov9cd3ccf2007-04-29 20:56:48 +0000103 case 2: return GlobalValue::ProtectedVisibility;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000104 }
105}
106
Stephen Hines36b56882014-04-23 16:57:46 -0700107static GlobalValue::DLLStorageClassTypes
108GetDecodedDLLStorageClass(unsigned Val) {
109 switch (Val) {
110 default: // Map unknown values to default.
111 case 0: return GlobalValue::DefaultStorageClass;
112 case 1: return GlobalValue::DLLImportStorageClass;
113 case 2: return GlobalValue::DLLExportStorageClass;
114 }
115}
116
Hans Wennborgce718ff2012-06-23 11:37:03 +0000117static GlobalVariable::ThreadLocalMode GetDecodedThreadLocalMode(unsigned Val) {
118 switch (Val) {
119 case 0: return GlobalVariable::NotThreadLocal;
120 default: // Map unknown non-zero value to general dynamic.
121 case 1: return GlobalVariable::GeneralDynamicTLSModel;
122 case 2: return GlobalVariable::LocalDynamicTLSModel;
123 case 3: return GlobalVariable::InitialExecTLSModel;
124 case 4: return GlobalVariable::LocalExecTLSModel;
125 }
126}
127
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000128static int GetDecodedCastOpcode(unsigned Val) {
129 switch (Val) {
130 default: return -1;
131 case bitc::CAST_TRUNC : return Instruction::Trunc;
132 case bitc::CAST_ZEXT : return Instruction::ZExt;
133 case bitc::CAST_SEXT : return Instruction::SExt;
134 case bitc::CAST_FPTOUI : return Instruction::FPToUI;
135 case bitc::CAST_FPTOSI : return Instruction::FPToSI;
136 case bitc::CAST_UITOFP : return Instruction::UIToFP;
137 case bitc::CAST_SITOFP : return Instruction::SIToFP;
138 case bitc::CAST_FPTRUNC : return Instruction::FPTrunc;
139 case bitc::CAST_FPEXT : return Instruction::FPExt;
140 case bitc::CAST_PTRTOINT: return Instruction::PtrToInt;
141 case bitc::CAST_INTTOPTR: return Instruction::IntToPtr;
142 case bitc::CAST_BITCAST : return Instruction::BitCast;
Matt Arsenault1572ba72013-11-18 02:51:33 +0000143 case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000144 }
145}
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000146static int GetDecodedBinaryOpcode(unsigned Val, Type *Ty) {
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000147 switch (Val) {
148 default: return -1;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000149 case bitc::BINOP_ADD:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000150 return Ty->isFPOrFPVectorTy() ? Instruction::FAdd : Instruction::Add;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000151 case bitc::BINOP_SUB:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000152 return Ty->isFPOrFPVectorTy() ? Instruction::FSub : Instruction::Sub;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000153 case bitc::BINOP_MUL:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000154 return Ty->isFPOrFPVectorTy() ? Instruction::FMul : Instruction::Mul;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000155 case bitc::BINOP_UDIV: return Instruction::UDiv;
156 case bitc::BINOP_SDIV:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000157 return Ty->isFPOrFPVectorTy() ? Instruction::FDiv : Instruction::SDiv;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000158 case bitc::BINOP_UREM: return Instruction::URem;
159 case bitc::BINOP_SREM:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000160 return Ty->isFPOrFPVectorTy() ? Instruction::FRem : Instruction::SRem;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000161 case bitc::BINOP_SHL: return Instruction::Shl;
162 case bitc::BINOP_LSHR: return Instruction::LShr;
163 case bitc::BINOP_ASHR: return Instruction::AShr;
164 case bitc::BINOP_AND: return Instruction::And;
165 case bitc::BINOP_OR: return Instruction::Or;
166 case bitc::BINOP_XOR: return Instruction::Xor;
167 }
168}
169
Eli Friedmanff030482011-07-28 21:48:00 +0000170static AtomicRMWInst::BinOp GetDecodedRMWOperation(unsigned Val) {
171 switch (Val) {
172 default: return AtomicRMWInst::BAD_BINOP;
173 case bitc::RMW_XCHG: return AtomicRMWInst::Xchg;
174 case bitc::RMW_ADD: return AtomicRMWInst::Add;
175 case bitc::RMW_SUB: return AtomicRMWInst::Sub;
176 case bitc::RMW_AND: return AtomicRMWInst::And;
177 case bitc::RMW_NAND: return AtomicRMWInst::Nand;
178 case bitc::RMW_OR: return AtomicRMWInst::Or;
179 case bitc::RMW_XOR: return AtomicRMWInst::Xor;
180 case bitc::RMW_MAX: return AtomicRMWInst::Max;
181 case bitc::RMW_MIN: return AtomicRMWInst::Min;
182 case bitc::RMW_UMAX: return AtomicRMWInst::UMax;
183 case bitc::RMW_UMIN: return AtomicRMWInst::UMin;
184 }
185}
186
Eli Friedman47f35132011-07-25 23:16:38 +0000187static AtomicOrdering GetDecodedOrdering(unsigned Val) {
188 switch (Val) {
189 case bitc::ORDERING_NOTATOMIC: return NotAtomic;
190 case bitc::ORDERING_UNORDERED: return Unordered;
191 case bitc::ORDERING_MONOTONIC: return Monotonic;
192 case bitc::ORDERING_ACQUIRE: return Acquire;
193 case bitc::ORDERING_RELEASE: return Release;
194 case bitc::ORDERING_ACQREL: return AcquireRelease;
195 default: // Map unknown orderings to sequentially-consistent.
196 case bitc::ORDERING_SEQCST: return SequentiallyConsistent;
197 }
198}
199
200static SynchronizationScope GetDecodedSynchScope(unsigned Val) {
201 switch (Val) {
202 case bitc::SYNCHSCOPE_SINGLETHREAD: return SingleThread;
203 default: // Map unknown scopes to cross-thread.
204 case bitc::SYNCHSCOPE_CROSSTHREAD: return CrossThread;
205 }
206}
207
Stephen Hines36b56882014-04-23 16:57:46 -0700208static void UpgradeDLLImportExportLinkage(llvm::GlobalValue *GV, unsigned Val) {
209 switch (Val) {
210 case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break;
211 case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break;
212 }
213}
214
Gabor Greifefe65362008-05-10 08:32:32 +0000215namespace llvm {
Chris Lattner522b7b12007-04-24 05:48:56 +0000216namespace {
217 /// @brief A class for maintaining the slot number definition
218 /// as a placeholder for the actual definition for forward constants defs.
219 class ConstantPlaceHolder : public ConstantExpr {
Craig Topper86a1c322012-09-15 17:09:36 +0000220 void operator=(const ConstantPlaceHolder &) LLVM_DELETED_FUNCTION;
Gabor Greif051a9502008-04-06 20:25:17 +0000221 public:
222 // allocate space for exactly one operand
223 void *operator new(size_t s) {
224 return User::operator new(s, 1);
225 }
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000226 explicit ConstantPlaceHolder(Type *Ty, LLVMContext& Context)
Gabor Greifefe65362008-05-10 08:32:32 +0000227 : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000228 Op<0>() = UndefValue::get(Type::getInt32Ty(Context));
Chris Lattner522b7b12007-04-24 05:48:56 +0000229 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000230
Chris Lattnerea693df2008-08-21 02:34:16 +0000231 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
Chris Lattnerea693df2008-08-21 02:34:16 +0000232 static bool classof(const Value *V) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000233 return isa<ConstantExpr>(V) &&
Chris Lattnerea693df2008-08-21 02:34:16 +0000234 cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1;
235 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000236
237
Gabor Greifefe65362008-05-10 08:32:32 +0000238 /// Provide fast operand accessors
Chris Lattner46e77402009-03-31 22:55:09 +0000239 //DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Chris Lattner522b7b12007-04-24 05:48:56 +0000240 };
241}
242
Chris Lattner46e77402009-03-31 22:55:09 +0000243// FIXME: can we inherit this from ConstantExpr?
Gabor Greifefe65362008-05-10 08:32:32 +0000244template <>
Jay Foad67c619b2011-01-11 15:07:38 +0000245struct OperandTraits<ConstantPlaceHolder> :
246 public FixedNumOperandTraits<ConstantPlaceHolder, 1> {
Gabor Greifefe65362008-05-10 08:32:32 +0000247};
Gabor Greifefe65362008-05-10 08:32:32 +0000248}
249
Chris Lattner46e77402009-03-31 22:55:09 +0000250
251void BitcodeReaderValueList::AssignValue(Value *V, unsigned Idx) {
252 if (Idx == size()) {
253 push_back(V);
254 return;
255 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000256
Chris Lattner46e77402009-03-31 22:55:09 +0000257 if (Idx >= size())
258 resize(Idx+1);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000259
Chris Lattner46e77402009-03-31 22:55:09 +0000260 WeakVH &OldV = ValuePtrs[Idx];
261 if (OldV == 0) {
262 OldV = V;
263 return;
264 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000265
Chris Lattner46e77402009-03-31 22:55:09 +0000266 // Handle constants and non-constants (e.g. instrs) differently for
267 // efficiency.
268 if (Constant *PHC = dyn_cast<Constant>(&*OldV)) {
269 ResolveConstants.push_back(std::make_pair(PHC, Idx));
270 OldV = V;
271 } else {
272 // If there was a forward reference to this value, replace it.
273 Value *PrevVal = OldV;
274 OldV->replaceAllUsesWith(V);
275 delete PrevVal;
Gabor Greifefe65362008-05-10 08:32:32 +0000276 }
277}
Daniel Dunbara279bc32009-09-20 02:20:51 +0000278
Gabor Greifefe65362008-05-10 08:32:32 +0000279
Chris Lattner522b7b12007-04-24 05:48:56 +0000280Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000281 Type *Ty) {
Chris Lattner46e77402009-03-31 22:55:09 +0000282 if (Idx >= size())
Gabor Greifefe65362008-05-10 08:32:32 +0000283 resize(Idx + 1);
Chris Lattner522b7b12007-04-24 05:48:56 +0000284
Chris Lattner46e77402009-03-31 22:55:09 +0000285 if (Value *V = ValuePtrs[Idx]) {
Chris Lattnera7c49aa2007-05-01 07:01:57 +0000286 assert(Ty == V->getType() && "Type mismatch in constant table!");
287 return cast<Constant>(V);
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000288 }
Chris Lattner522b7b12007-04-24 05:48:56 +0000289
290 // Create and return a placeholder, which will later be RAUW'd.
Owen Anderson74a77812009-07-07 20:18:58 +0000291 Constant *C = new ConstantPlaceHolder(Ty, Context);
Chris Lattner46e77402009-03-31 22:55:09 +0000292 ValuePtrs[Idx] = C;
Chris Lattner522b7b12007-04-24 05:48:56 +0000293 return C;
294}
295
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000296Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, Type *Ty) {
Chris Lattner46e77402009-03-31 22:55:09 +0000297 if (Idx >= size())
Gabor Greifefe65362008-05-10 08:32:32 +0000298 resize(Idx + 1);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000299
Chris Lattner46e77402009-03-31 22:55:09 +0000300 if (Value *V = ValuePtrs[Idx]) {
Chris Lattnera7c49aa2007-05-01 07:01:57 +0000301 assert((Ty == 0 || Ty == V->getType()) && "Type mismatch in value table!");
302 return V;
303 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000304
Chris Lattner01ff65f2007-05-02 05:16:49 +0000305 // No type specified, must be invalid reference.
306 if (Ty == 0) return 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000307
Chris Lattnera7c49aa2007-05-01 07:01:57 +0000308 // Create and return a placeholder, which will later be RAUW'd.
309 Value *V = new Argument(Ty);
Chris Lattner46e77402009-03-31 22:55:09 +0000310 ValuePtrs[Idx] = V;
Chris Lattnera7c49aa2007-05-01 07:01:57 +0000311 return V;
312}
313
Chris Lattnerea693df2008-08-21 02:34:16 +0000314/// ResolveConstantForwardRefs - Once all constants are read, this method bulk
315/// resolves any forward references. The idea behind this is that we sometimes
316/// get constants (such as large arrays) which reference *many* forward ref
317/// constants. Replacing each of these causes a lot of thrashing when
318/// building/reuniquing the constant. Instead of doing this, we look at all the
319/// uses and rewrite all the place holders at once for any constant that uses
320/// a placeholder.
321void BitcodeReaderValueList::ResolveConstantForwardRefs() {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000322 // Sort the values by-pointer so that they are efficient to look up with a
Chris Lattnerea693df2008-08-21 02:34:16 +0000323 // binary search.
324 std::sort(ResolveConstants.begin(), ResolveConstants.end());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000325
Chris Lattnerea693df2008-08-21 02:34:16 +0000326 SmallVector<Constant*, 64> NewOps;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000327
Chris Lattnerea693df2008-08-21 02:34:16 +0000328 while (!ResolveConstants.empty()) {
Chris Lattner46e77402009-03-31 22:55:09 +0000329 Value *RealVal = operator[](ResolveConstants.back().second);
Chris Lattnerea693df2008-08-21 02:34:16 +0000330 Constant *Placeholder = ResolveConstants.back().first;
331 ResolveConstants.pop_back();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000332
Chris Lattnerea693df2008-08-21 02:34:16 +0000333 // Loop over all users of the placeholder, updating them to reference the
334 // new value. If they reference more than one placeholder, update them all
335 // at once.
336 while (!Placeholder->use_empty()) {
Stephen Hines36b56882014-04-23 16:57:46 -0700337 auto UI = Placeholder->user_begin();
Gabor Greifc654d1b2010-07-09 16:01:21 +0000338 User *U = *UI;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000339
Chris Lattnerea693df2008-08-21 02:34:16 +0000340 // If the using object isn't uniqued, just update the operands. This
341 // handles instructions and initializers for global variables.
Gabor Greifc654d1b2010-07-09 16:01:21 +0000342 if (!isa<Constant>(U) || isa<GlobalValue>(U)) {
Chris Lattnerb6135a02008-08-21 17:31:45 +0000343 UI.getUse().set(RealVal);
Chris Lattnerea693df2008-08-21 02:34:16 +0000344 continue;
345 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000346
Chris Lattnerea693df2008-08-21 02:34:16 +0000347 // Otherwise, we have a constant that uses the placeholder. Replace that
348 // constant with a new constant that has *all* placeholder uses updated.
Gabor Greifc654d1b2010-07-09 16:01:21 +0000349 Constant *UserC = cast<Constant>(U);
Chris Lattnerea693df2008-08-21 02:34:16 +0000350 for (User::op_iterator I = UserC->op_begin(), E = UserC->op_end();
351 I != E; ++I) {
352 Value *NewOp;
353 if (!isa<ConstantPlaceHolder>(*I)) {
354 // Not a placeholder reference.
355 NewOp = *I;
356 } else if (*I == Placeholder) {
357 // Common case is that it just references this one placeholder.
358 NewOp = RealVal;
359 } else {
360 // Otherwise, look up the placeholder in ResolveConstants.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000361 ResolveConstantsTy::iterator It =
362 std::lower_bound(ResolveConstants.begin(), ResolveConstants.end(),
Chris Lattnerea693df2008-08-21 02:34:16 +0000363 std::pair<Constant*, unsigned>(cast<Constant>(*I),
364 0));
365 assert(It != ResolveConstants.end() && It->first == *I);
Chris Lattner46e77402009-03-31 22:55:09 +0000366 NewOp = operator[](It->second);
Chris Lattnerea693df2008-08-21 02:34:16 +0000367 }
368
369 NewOps.push_back(cast<Constant>(NewOp));
370 }
371
372 // Make the new constant.
373 Constant *NewC;
374 if (ConstantArray *UserCA = dyn_cast<ConstantArray>(UserC)) {
Jay Foad26701082011-06-22 09:24:39 +0000375 NewC = ConstantArray::get(UserCA->getType(), NewOps);
Chris Lattnerea693df2008-08-21 02:34:16 +0000376 } else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) {
Chris Lattnerb065b062011-06-20 04:01:31 +0000377 NewC = ConstantStruct::get(UserCS->getType(), NewOps);
Chris Lattnerea693df2008-08-21 02:34:16 +0000378 } else if (isa<ConstantVector>(UserC)) {
Chris Lattner2ca5c862011-02-15 00:14:00 +0000379 NewC = ConstantVector::get(NewOps);
Nick Lewyckycb337992009-05-10 20:57:05 +0000380 } else {
381 assert(isa<ConstantExpr>(UserC) && "Must be a ConstantExpr.");
Jay Foadb81e4572011-04-13 13:46:01 +0000382 NewC = cast<ConstantExpr>(UserC)->getWithOperands(NewOps);
Chris Lattnerea693df2008-08-21 02:34:16 +0000383 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000384
Chris Lattnerea693df2008-08-21 02:34:16 +0000385 UserC->replaceAllUsesWith(NewC);
386 UserC->destroyConstant();
387 NewOps.clear();
388 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000389
Nick Lewyckycb337992009-05-10 20:57:05 +0000390 // Update all ValueHandles, they should be the only users at this point.
391 Placeholder->replaceAllUsesWith(RealVal);
Chris Lattnerea693df2008-08-21 02:34:16 +0000392 delete Placeholder;
393 }
394}
395
Devang Pateld5ac4042009-08-04 06:00:18 +0000396void BitcodeReaderMDValueList::AssignValue(Value *V, unsigned Idx) {
397 if (Idx == size()) {
398 push_back(V);
399 return;
400 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000401
Devang Pateld5ac4042009-08-04 06:00:18 +0000402 if (Idx >= size())
403 resize(Idx+1);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000404
Devang Pateld5ac4042009-08-04 06:00:18 +0000405 WeakVH &OldV = MDValuePtrs[Idx];
406 if (OldV == 0) {
407 OldV = V;
408 return;
409 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000410
Devang Pateld5ac4042009-08-04 06:00:18 +0000411 // If there was a forward reference to this value, replace it.
Dan Gohman489b29b2010-08-20 22:02:26 +0000412 MDNode *PrevVal = cast<MDNode>(OldV);
Devang Pateld5ac4042009-08-04 06:00:18 +0000413 OldV->replaceAllUsesWith(V);
Dan Gohman489b29b2010-08-20 22:02:26 +0000414 MDNode::deleteTemporary(PrevVal);
Devang Patelc0ff8c82009-09-03 01:38:02 +0000415 // Deleting PrevVal sets Idx value in MDValuePtrs to null. Set new
416 // value for Idx.
417 MDValuePtrs[Idx] = V;
Devang Pateld5ac4042009-08-04 06:00:18 +0000418}
419
420Value *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) {
421 if (Idx >= size())
422 resize(Idx + 1);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000423
Devang Pateld5ac4042009-08-04 06:00:18 +0000424 if (Value *V = MDValuePtrs[Idx]) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000425 assert(V->getType()->isMetadataTy() && "Type mismatch in value table!");
Devang Pateld5ac4042009-08-04 06:00:18 +0000426 return V;
427 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000428
Devang Pateld5ac4042009-08-04 06:00:18 +0000429 // Create and return a placeholder, which will later be RAUW'd.
Dmitri Gribenko5c332db2013-05-05 00:40:33 +0000430 Value *V = MDNode::getTemporary(Context, None);
Devang Pateld5ac4042009-08-04 06:00:18 +0000431 MDValuePtrs[Idx] = V;
432 return V;
433}
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000434
Chris Lattner1afcace2011-07-09 17:41:24 +0000435Type *BitcodeReader::getTypeByID(unsigned ID) {
436 // The type table size is always specified correctly.
437 if (ID >= TypeList.size())
438 return 0;
Derek Schufffccf0622012-02-06 19:03:04 +0000439
Chris Lattner1afcace2011-07-09 17:41:24 +0000440 if (Type *Ty = TypeList[ID])
441 return Ty;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000442
Chris Lattner1afcace2011-07-09 17:41:24 +0000443 // If we have a forward reference, the only possible case is when it is to a
444 // named struct. Just create a placeholder for now.
Chris Lattner3ebb6492011-08-12 18:06:37 +0000445 return TypeList[ID] = StructType::create(Context);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000446}
447
Chris Lattner1afcace2011-07-09 17:41:24 +0000448
Chris Lattner48c85b82007-05-04 03:30:17 +0000449//===----------------------------------------------------------------------===//
450// Functions for parsing blocks from the bitcode file
451//===----------------------------------------------------------------------===//
452
Bill Wendlingf9271ea2013-02-04 23:32:23 +0000453
454/// \brief This fills an AttrBuilder object with the LLVM attributes that have
455/// been decoded from the given integer. This function must stay in sync with
456/// 'encodeLLVMAttributesForBitcode'.
457static void decodeLLVMAttributesForBitcode(AttrBuilder &B,
458 uint64_t EncodedAttrs) {
459 // FIXME: Remove in 4.0.
460
461 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
462 // the bits above 31 down by 11 bits.
463 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
464 assert((!Alignment || isPowerOf2_32(Alignment)) &&
465 "Alignment must be a power of two.");
466
467 if (Alignment)
468 B.addAlignmentAttr(Alignment);
Kostya Serebryanyab39afa2013-02-11 08:13:54 +0000469 B.addRawValue(((EncodedAttrs & (0xfffffULL << 32)) >> 11) |
Bill Wendlingf9271ea2013-02-04 23:32:23 +0000470 (EncodedAttrs & 0xffff));
471}
472
Rafael Espindolae076b532013-11-04 16:16:24 +0000473error_code BitcodeReader::ParseAttributeBlock() {
Chris Lattnere17b6582007-05-05 00:17:00 +0000474 if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +0000475 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000476
Devang Patel19c87462008-09-26 22:53:05 +0000477 if (!MAttributes.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +0000478 return Error(InvalidMultipleBlocks);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000479
Chris Lattner48c85b82007-05-04 03:30:17 +0000480 SmallVector<uint64_t, 64> Record;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000481
Bill Wendling0c2f0ff2013-01-27 00:36:48 +0000482 SmallVector<AttributeSet, 8> Attrs;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000483
Chris Lattner48c85b82007-05-04 03:30:17 +0000484 // Read all the records.
485 while (1) {
Chris Lattner5a4251c2013-01-20 02:13:19 +0000486 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbeyacb61942013-02-06 22:14:06 +0000487
Chris Lattner5a4251c2013-01-20 02:13:19 +0000488 switch (Entry.Kind) {
489 case BitstreamEntry::SubBlock: // Handled for us already.
490 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +0000491 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +0000492 case BitstreamEntry::EndBlock:
Rafael Espindolae076b532013-11-04 16:16:24 +0000493 return error_code::success();
Chris Lattner5a4251c2013-01-20 02:13:19 +0000494 case BitstreamEntry::Record:
495 // The interesting case.
496 break;
Chris Lattner48c85b82007-05-04 03:30:17 +0000497 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000498
Chris Lattner48c85b82007-05-04 03:30:17 +0000499 // Read a record.
500 Record.clear();
Chris Lattner5a4251c2013-01-20 02:13:19 +0000501 switch (Stream.readRecord(Entry.ID, Record)) {
Chris Lattner48c85b82007-05-04 03:30:17 +0000502 default: // Default behavior: ignore.
503 break;
Bill Wendlingf9271ea2013-02-04 23:32:23 +0000504 case bitc::PARAMATTR_CODE_ENTRY_OLD: { // ENTRY: [paramidx0, attr0, ...]
505 // FIXME: Remove in 4.0.
Chris Lattner48c85b82007-05-04 03:30:17 +0000506 if (Record.size() & 1)
Rafael Espindolae076b532013-11-04 16:16:24 +0000507 return Error(InvalidRecord);
Chris Lattner48c85b82007-05-04 03:30:17 +0000508
Chris Lattner48c85b82007-05-04 03:30:17 +0000509 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
Bill Wendling8232ece2013-01-29 01:43:29 +0000510 AttrBuilder B;
Bill Wendlingf9271ea2013-02-04 23:32:23 +0000511 decodeLLVMAttributesForBitcode(B, Record[i+1]);
Bill Wendling8232ece2013-01-29 01:43:29 +0000512 Attrs.push_back(AttributeSet::get(Context, Record[i], B));
Devang Patel19c87462008-09-26 22:53:05 +0000513 }
Devang Patel19c87462008-09-26 22:53:05 +0000514
Bill Wendling99faa3b2012-12-07 23:16:57 +0000515 MAttributes.push_back(AttributeSet::get(Context, Attrs));
Chris Lattner48c85b82007-05-04 03:30:17 +0000516 Attrs.clear();
517 break;
518 }
Bill Wendling48fbcfe2013-02-12 08:13:50 +0000519 case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [attrgrp0, attrgrp1, ...]
520 for (unsigned i = 0, e = Record.size(); i != e; ++i)
521 Attrs.push_back(MAttributeGroups[Record[i]]);
522
523 MAttributes.push_back(AttributeSet::get(Context, Attrs));
524 Attrs.clear();
525 break;
526 }
Duncan Sands5e41f652007-11-20 14:09:29 +0000527 }
Chris Lattner48c85b82007-05-04 03:30:17 +0000528 }
529}
530
Reid Kleckner161dd532013-11-12 01:31:00 +0000531// Returns Attribute::None on unrecognized codes.
532static Attribute::AttrKind GetAttrFromCode(uint64_t Code) {
533 switch (Code) {
534 default:
535 return Attribute::None;
536 case bitc::ATTR_KIND_ALIGNMENT:
537 return Attribute::Alignment;
538 case bitc::ATTR_KIND_ALWAYS_INLINE:
539 return Attribute::AlwaysInline;
540 case bitc::ATTR_KIND_BUILTIN:
541 return Attribute::Builtin;
542 case bitc::ATTR_KIND_BY_VAL:
543 return Attribute::ByVal;
Stephen Hines36b56882014-04-23 16:57:46 -0700544 case bitc::ATTR_KIND_IN_ALLOCA:
545 return Attribute::InAlloca;
Reid Kleckner161dd532013-11-12 01:31:00 +0000546 case bitc::ATTR_KIND_COLD:
547 return Attribute::Cold;
548 case bitc::ATTR_KIND_INLINE_HINT:
549 return Attribute::InlineHint;
550 case bitc::ATTR_KIND_IN_REG:
551 return Attribute::InReg;
552 case bitc::ATTR_KIND_MIN_SIZE:
553 return Attribute::MinSize;
554 case bitc::ATTR_KIND_NAKED:
555 return Attribute::Naked;
556 case bitc::ATTR_KIND_NEST:
557 return Attribute::Nest;
558 case bitc::ATTR_KIND_NO_ALIAS:
559 return Attribute::NoAlias;
560 case bitc::ATTR_KIND_NO_BUILTIN:
561 return Attribute::NoBuiltin;
562 case bitc::ATTR_KIND_NO_CAPTURE:
563 return Attribute::NoCapture;
564 case bitc::ATTR_KIND_NO_DUPLICATE:
565 return Attribute::NoDuplicate;
566 case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT:
567 return Attribute::NoImplicitFloat;
568 case bitc::ATTR_KIND_NO_INLINE:
569 return Attribute::NoInline;
570 case bitc::ATTR_KIND_NON_LAZY_BIND:
571 return Attribute::NonLazyBind;
572 case bitc::ATTR_KIND_NO_RED_ZONE:
573 return Attribute::NoRedZone;
574 case bitc::ATTR_KIND_NO_RETURN:
575 return Attribute::NoReturn;
576 case bitc::ATTR_KIND_NO_UNWIND:
577 return Attribute::NoUnwind;
578 case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE:
579 return Attribute::OptimizeForSize;
580 case bitc::ATTR_KIND_OPTIMIZE_NONE:
581 return Attribute::OptimizeNone;
582 case bitc::ATTR_KIND_READ_NONE:
583 return Attribute::ReadNone;
584 case bitc::ATTR_KIND_READ_ONLY:
585 return Attribute::ReadOnly;
586 case bitc::ATTR_KIND_RETURNED:
587 return Attribute::Returned;
588 case bitc::ATTR_KIND_RETURNS_TWICE:
589 return Attribute::ReturnsTwice;
590 case bitc::ATTR_KIND_S_EXT:
591 return Attribute::SExt;
592 case bitc::ATTR_KIND_STACK_ALIGNMENT:
593 return Attribute::StackAlignment;
594 case bitc::ATTR_KIND_STACK_PROTECT:
595 return Attribute::StackProtect;
596 case bitc::ATTR_KIND_STACK_PROTECT_REQ:
597 return Attribute::StackProtectReq;
598 case bitc::ATTR_KIND_STACK_PROTECT_STRONG:
599 return Attribute::StackProtectStrong;
600 case bitc::ATTR_KIND_STRUCT_RET:
601 return Attribute::StructRet;
602 case bitc::ATTR_KIND_SANITIZE_ADDRESS:
603 return Attribute::SanitizeAddress;
604 case bitc::ATTR_KIND_SANITIZE_THREAD:
605 return Attribute::SanitizeThread;
606 case bitc::ATTR_KIND_SANITIZE_MEMORY:
607 return Attribute::SanitizeMemory;
608 case bitc::ATTR_KIND_UW_TABLE:
609 return Attribute::UWTable;
610 case bitc::ATTR_KIND_Z_EXT:
611 return Attribute::ZExt;
612 }
613}
614
Rafael Espindolae076b532013-11-04 16:16:24 +0000615error_code BitcodeReader::ParseAttrKind(uint64_t Code,
616 Attribute::AttrKind *Kind) {
Reid Kleckner161dd532013-11-12 01:31:00 +0000617 *Kind = GetAttrFromCode(Code);
618 if (*Kind == Attribute::None)
Rafael Espindolae076b532013-11-04 16:16:24 +0000619 return Error(InvalidValue);
Reid Kleckner161dd532013-11-12 01:31:00 +0000620 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000621}
622
Rafael Espindolae076b532013-11-04 16:16:24 +0000623error_code BitcodeReader::ParseAttributeGroupBlock() {
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000624 if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +0000625 return Error(InvalidRecord);
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000626
627 if (!MAttributeGroups.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +0000628 return Error(InvalidMultipleBlocks);
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000629
630 SmallVector<uint64_t, 64> Record;
631
632 // Read all the records.
633 while (1) {
634 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
635
636 switch (Entry.Kind) {
637 case BitstreamEntry::SubBlock: // Handled for us already.
638 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +0000639 return Error(MalformedBlock);
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000640 case BitstreamEntry::EndBlock:
Rafael Espindolae076b532013-11-04 16:16:24 +0000641 return error_code::success();
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000642 case BitstreamEntry::Record:
643 // The interesting case.
644 break;
645 }
646
647 // Read a record.
648 Record.clear();
649 switch (Stream.readRecord(Entry.ID, Record)) {
650 default: // Default behavior: ignore.
651 break;
652 case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...]
653 if (Record.size() < 3)
Rafael Espindolae076b532013-11-04 16:16:24 +0000654 return Error(InvalidRecord);
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000655
Bill Wendling04ef4be2013-02-11 22:32:29 +0000656 uint64_t GrpID = Record[0];
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000657 uint64_t Idx = Record[1]; // Index of the object this attribute refers to.
658
659 AttrBuilder B;
660 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
661 if (Record[i] == 0) { // Enum attribute
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000662 Attribute::AttrKind Kind;
Rafael Espindolae076b532013-11-04 16:16:24 +0000663 if (error_code EC = ParseAttrKind(Record[++i], &Kind))
664 return EC;
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000665
666 B.addAttribute(Kind);
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000667 } else if (Record[i] == 1) { // Align attribute
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000668 Attribute::AttrKind Kind;
Rafael Espindolae076b532013-11-04 16:16:24 +0000669 if (error_code EC = ParseAttrKind(Record[++i], &Kind))
670 return EC;
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000671 if (Kind == Attribute::Alignment)
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000672 B.addAlignmentAttr(Record[++i]);
673 else
674 B.addStackAlignmentAttr(Record[++i]);
675 } else { // String attribute
Bill Wendling04ef4be2013-02-11 22:32:29 +0000676 assert((Record[i] == 3 || Record[i] == 4) &&
677 "Invalid attribute group entry");
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000678 bool HasValue = (Record[i++] == 4);
679 SmallString<64> KindStr;
680 SmallString<64> ValStr;
681
682 while (Record[i] != 0 && i != e)
683 KindStr += Record[i++];
Bill Wendling04ef4be2013-02-11 22:32:29 +0000684 assert(Record[i] == 0 && "Kind string not null terminated");
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000685
686 if (HasValue) {
687 // Has a value associated with it.
Bill Wendling04ef4be2013-02-11 22:32:29 +0000688 ++i; // Skip the '0' that terminates the "kind" string.
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000689 while (Record[i] != 0 && i != e)
690 ValStr += Record[i++];
Bill Wendling04ef4be2013-02-11 22:32:29 +0000691 assert(Record[i] == 0 && "Value string not null terminated");
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000692 }
693
694 B.addAttribute(KindStr.str(), ValStr.str());
695 }
696 }
697
Bill Wendling04ef4be2013-02-11 22:32:29 +0000698 MAttributeGroups[GrpID] = AttributeSet::get(Context, Idx, B);
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000699 break;
700 }
701 }
702 }
703}
704
Rafael Espindolae076b532013-11-04 16:16:24 +0000705error_code BitcodeReader::ParseTypeTable() {
Chris Lattner1afcace2011-07-09 17:41:24 +0000706 if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW))
Rafael Espindolae076b532013-11-04 16:16:24 +0000707 return Error(InvalidRecord);
Derek Schufffccf0622012-02-06 19:03:04 +0000708
Chris Lattner1afcace2011-07-09 17:41:24 +0000709 return ParseTypeTableBody();
710}
Daniel Dunbara279bc32009-09-20 02:20:51 +0000711
Rafael Espindolae076b532013-11-04 16:16:24 +0000712error_code BitcodeReader::ParseTypeTableBody() {
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000713 if (!TypeList.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +0000714 return Error(InvalidMultipleBlocks);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000715
716 SmallVector<uint64_t, 64> Record;
717 unsigned NumRecords = 0;
718
Chris Lattner1afcace2011-07-09 17:41:24 +0000719 SmallString<64> TypeName;
Derek Schufffccf0622012-02-06 19:03:04 +0000720
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000721 // Read all the records for this type table.
722 while (1) {
Chris Lattner5a4251c2013-01-20 02:13:19 +0000723 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbeyacb61942013-02-06 22:14:06 +0000724
Chris Lattner5a4251c2013-01-20 02:13:19 +0000725 switch (Entry.Kind) {
726 case BitstreamEntry::SubBlock: // Handled for us already.
727 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +0000728 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +0000729 case BitstreamEntry::EndBlock:
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000730 if (NumRecords != TypeList.size())
Rafael Espindolae076b532013-11-04 16:16:24 +0000731 return Error(MalformedBlock);
732 return error_code::success();
Chris Lattner5a4251c2013-01-20 02:13:19 +0000733 case BitstreamEntry::Record:
734 // The interesting case.
735 break;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000736 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000737
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000738 // Read a record.
739 Record.clear();
Chris Lattner1afcace2011-07-09 17:41:24 +0000740 Type *ResultTy = 0;
Chris Lattner5a4251c2013-01-20 02:13:19 +0000741 switch (Stream.readRecord(Entry.ID, Record)) {
Rafael Espindolae076b532013-11-04 16:16:24 +0000742 default:
743 return Error(InvalidValue);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000744 case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
745 // TYPE_CODE_NUMENTRY contains a count of the number of types in the
746 // type list. This allows us to reserve space.
747 if (Record.size() < 1)
Rafael Espindolae076b532013-11-04 16:16:24 +0000748 return Error(InvalidRecord);
Chris Lattner1afcace2011-07-09 17:41:24 +0000749 TypeList.resize(Record[0]);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000750 continue;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000751 case bitc::TYPE_CODE_VOID: // VOID
Owen Anderson1d0be152009-08-13 21:58:54 +0000752 ResultTy = Type::getVoidTy(Context);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000753 break;
Dan Gohmance163392011-12-17 00:04:22 +0000754 case bitc::TYPE_CODE_HALF: // HALF
755 ResultTy = Type::getHalfTy(Context);
756 break;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000757 case bitc::TYPE_CODE_FLOAT: // FLOAT
Owen Anderson1d0be152009-08-13 21:58:54 +0000758 ResultTy = Type::getFloatTy(Context);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000759 break;
760 case bitc::TYPE_CODE_DOUBLE: // DOUBLE
Owen Anderson1d0be152009-08-13 21:58:54 +0000761 ResultTy = Type::getDoubleTy(Context);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000762 break;
Dale Johannesen320fc8a2007-08-03 01:03:46 +0000763 case bitc::TYPE_CODE_X86_FP80: // X86_FP80
Owen Anderson1d0be152009-08-13 21:58:54 +0000764 ResultTy = Type::getX86_FP80Ty(Context);
Dale Johannesen320fc8a2007-08-03 01:03:46 +0000765 break;
766 case bitc::TYPE_CODE_FP128: // FP128
Owen Anderson1d0be152009-08-13 21:58:54 +0000767 ResultTy = Type::getFP128Ty(Context);
Dale Johannesen320fc8a2007-08-03 01:03:46 +0000768 break;
769 case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128
Owen Anderson1d0be152009-08-13 21:58:54 +0000770 ResultTy = Type::getPPC_FP128Ty(Context);
Dale Johannesen320fc8a2007-08-03 01:03:46 +0000771 break;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000772 case bitc::TYPE_CODE_LABEL: // LABEL
Owen Anderson1d0be152009-08-13 21:58:54 +0000773 ResultTy = Type::getLabelTy(Context);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000774 break;
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000775 case bitc::TYPE_CODE_METADATA: // METADATA
Owen Anderson1d0be152009-08-13 21:58:54 +0000776 ResultTy = Type::getMetadataTy(Context);
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000777 break;
Dale Johannesenbb811a22010-09-10 20:55:01 +0000778 case bitc::TYPE_CODE_X86_MMX: // X86_MMX
779 ResultTy = Type::getX86_MMXTy(Context);
780 break;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000781 case bitc::TYPE_CODE_INTEGER: // INTEGER: [width]
782 if (Record.size() < 1)
Rafael Espindolae076b532013-11-04 16:16:24 +0000783 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000784
Owen Anderson1d0be152009-08-13 21:58:54 +0000785 ResultTy = IntegerType::get(Context, Record[0]);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000786 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000787 case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or
Christopher Lambfe63fb92007-12-11 08:59:05 +0000788 // [pointee type, address space]
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000789 if (Record.size() < 1)
Rafael Espindolae076b532013-11-04 16:16:24 +0000790 return Error(InvalidRecord);
Christopher Lambfe63fb92007-12-11 08:59:05 +0000791 unsigned AddressSpace = 0;
792 if (Record.size() == 2)
793 AddressSpace = Record[1];
Chris Lattner1afcace2011-07-09 17:41:24 +0000794 ResultTy = getTypeByID(Record[0]);
Rafael Espindolae076b532013-11-04 16:16:24 +0000795 if (ResultTy == 0)
796 return Error(InvalidType);
Chris Lattner1afcace2011-07-09 17:41:24 +0000797 ResultTy = PointerType::get(ResultTy, AddressSpace);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000798 break;
Christopher Lambfe63fb92007-12-11 08:59:05 +0000799 }
Nuno Lopesee8100d2012-05-23 15:19:39 +0000800 case bitc::TYPE_CODE_FUNCTION_OLD: {
801 // FIXME: attrid is dead, remove it in LLVM 4.0
802 // FUNCTION: [vararg, attrid, retty, paramty x N]
803 if (Record.size() < 3)
Rafael Espindolae076b532013-11-04 16:16:24 +0000804 return Error(InvalidRecord);
Nuno Lopesee8100d2012-05-23 15:19:39 +0000805 SmallVector<Type*, 8> ArgTys;
806 for (unsigned i = 3, e = Record.size(); i != e; ++i) {
807 if (Type *T = getTypeByID(Record[i]))
808 ArgTys.push_back(T);
809 else
810 break;
811 }
Michael Ilseman407a6162012-11-15 22:34:00 +0000812
Nuno Lopesee8100d2012-05-23 15:19:39 +0000813 ResultTy = getTypeByID(Record[2]);
814 if (ResultTy == 0 || ArgTys.size() < Record.size()-3)
Rafael Espindolae076b532013-11-04 16:16:24 +0000815 return Error(InvalidType);
Nuno Lopesee8100d2012-05-23 15:19:39 +0000816
817 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
818 break;
819 }
Chad Rosiercde54642011-11-03 00:14:01 +0000820 case bitc::TYPE_CODE_FUNCTION: {
821 // FUNCTION: [vararg, retty, paramty x N]
822 if (Record.size() < 2)
Rafael Espindolae076b532013-11-04 16:16:24 +0000823 return Error(InvalidRecord);
Chris Lattnerd629efa2012-01-27 03:15:49 +0000824 SmallVector<Type*, 8> ArgTys;
Chad Rosiercde54642011-11-03 00:14:01 +0000825 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
826 if (Type *T = getTypeByID(Record[i]))
827 ArgTys.push_back(T);
828 else
829 break;
830 }
Michael Ilseman407a6162012-11-15 22:34:00 +0000831
Chad Rosiercde54642011-11-03 00:14:01 +0000832 ResultTy = getTypeByID(Record[1]);
833 if (ResultTy == 0 || ArgTys.size() < Record.size()-2)
Rafael Espindolae076b532013-11-04 16:16:24 +0000834 return Error(InvalidType);
Chad Rosiercde54642011-11-03 00:14:01 +0000835
836 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
837 break;
838 }
Chris Lattner1afcace2011-07-09 17:41:24 +0000839 case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N]
Chris Lattner7108dce2007-05-06 08:21:50 +0000840 if (Record.size() < 1)
Rafael Espindolae076b532013-11-04 16:16:24 +0000841 return Error(InvalidRecord);
Chris Lattnerd629efa2012-01-27 03:15:49 +0000842 SmallVector<Type*, 8> EltTys;
Chris Lattner1afcace2011-07-09 17:41:24 +0000843 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
844 if (Type *T = getTypeByID(Record[i]))
845 EltTys.push_back(T);
846 else
847 break;
848 }
849 if (EltTys.size() != Record.size()-1)
Rafael Espindolae076b532013-11-04 16:16:24 +0000850 return Error(InvalidType);
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000851 ResultTy = StructType::get(Context, EltTys, Record[0]);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000852 break;
853 }
Chris Lattner1afcace2011-07-09 17:41:24 +0000854 case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N]
855 if (ConvertToString(Record, 0, TypeName))
Rafael Espindolae076b532013-11-04 16:16:24 +0000856 return Error(InvalidRecord);
Chris Lattner1afcace2011-07-09 17:41:24 +0000857 continue;
858
859 case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N]
860 if (Record.size() < 1)
Rafael Espindolae076b532013-11-04 16:16:24 +0000861 return Error(InvalidRecord);
Michael Ilseman407a6162012-11-15 22:34:00 +0000862
Chris Lattner1afcace2011-07-09 17:41:24 +0000863 if (NumRecords >= TypeList.size())
Rafael Espindolae076b532013-11-04 16:16:24 +0000864 return Error(InvalidTYPETable);
Michael Ilseman407a6162012-11-15 22:34:00 +0000865
Chris Lattner1afcace2011-07-09 17:41:24 +0000866 // Check to see if this was forward referenced, if so fill in the temp.
867 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
868 if (Res) {
869 Res->setName(TypeName);
870 TypeList[NumRecords] = 0;
871 } else // Otherwise, create a new struct.
Chris Lattner3ebb6492011-08-12 18:06:37 +0000872 Res = StructType::create(Context, TypeName);
Chris Lattner1afcace2011-07-09 17:41:24 +0000873 TypeName.clear();
Michael Ilseman407a6162012-11-15 22:34:00 +0000874
Chris Lattner1afcace2011-07-09 17:41:24 +0000875 SmallVector<Type*, 8> EltTys;
876 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
877 if (Type *T = getTypeByID(Record[i]))
878 EltTys.push_back(T);
879 else
880 break;
881 }
882 if (EltTys.size() != Record.size()-1)
Rafael Espindolae076b532013-11-04 16:16:24 +0000883 return Error(InvalidRecord);
Chris Lattner1afcace2011-07-09 17:41:24 +0000884 Res->setBody(EltTys, Record[0]);
885 ResultTy = Res;
886 break;
887 }
888 case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: []
889 if (Record.size() != 1)
Rafael Espindolae076b532013-11-04 16:16:24 +0000890 return Error(InvalidRecord);
Chris Lattner1afcace2011-07-09 17:41:24 +0000891
892 if (NumRecords >= TypeList.size())
Rafael Espindolae076b532013-11-04 16:16:24 +0000893 return Error(InvalidTYPETable);
Michael Ilseman407a6162012-11-15 22:34:00 +0000894
Chris Lattner1afcace2011-07-09 17:41:24 +0000895 // Check to see if this was forward referenced, if so fill in the temp.
896 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
897 if (Res) {
898 Res->setName(TypeName);
899 TypeList[NumRecords] = 0;
900 } else // Otherwise, create a new struct with no body.
Chris Lattner3ebb6492011-08-12 18:06:37 +0000901 Res = StructType::create(Context, TypeName);
Chris Lattner1afcace2011-07-09 17:41:24 +0000902 TypeName.clear();
903 ResultTy = Res;
904 break;
Michael Ilseman407a6162012-11-15 22:34:00 +0000905 }
Chris Lattner1afcace2011-07-09 17:41:24 +0000906 case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty]
907 if (Record.size() < 2)
Rafael Espindolae076b532013-11-04 16:16:24 +0000908 return Error(InvalidRecord);
Chris Lattner1afcace2011-07-09 17:41:24 +0000909 if ((ResultTy = getTypeByID(Record[1])))
910 ResultTy = ArrayType::get(ResultTy, Record[0]);
911 else
Rafael Espindolae076b532013-11-04 16:16:24 +0000912 return Error(InvalidType);
Chris Lattner1afcace2011-07-09 17:41:24 +0000913 break;
914 case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty]
915 if (Record.size() < 2)
Rafael Espindolae076b532013-11-04 16:16:24 +0000916 return Error(InvalidRecord);
Chris Lattner1afcace2011-07-09 17:41:24 +0000917 if ((ResultTy = getTypeByID(Record[1])))
918 ResultTy = VectorType::get(ResultTy, Record[0]);
919 else
Rafael Espindolae076b532013-11-04 16:16:24 +0000920 return Error(InvalidType);
Chris Lattner1afcace2011-07-09 17:41:24 +0000921 break;
922 }
923
924 if (NumRecords >= TypeList.size())
Rafael Espindolae076b532013-11-04 16:16:24 +0000925 return Error(InvalidTYPETable);
Chris Lattner1afcace2011-07-09 17:41:24 +0000926 assert(ResultTy && "Didn't read a type?");
927 assert(TypeList[NumRecords] == 0 && "Already read type?");
928 TypeList[NumRecords++] = ResultTy;
929 }
930}
931
Rafael Espindolae076b532013-11-04 16:16:24 +0000932error_code BitcodeReader::ParseValueSymbolTable() {
Chris Lattnere17b6582007-05-05 00:17:00 +0000933 if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +0000934 return Error(InvalidRecord);
Chris Lattner0b2482a2007-04-23 21:26:05 +0000935
936 SmallVector<uint64_t, 64> Record;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000937
Chris Lattner0b2482a2007-04-23 21:26:05 +0000938 // Read all the records for this value table.
939 SmallString<128> ValueName;
940 while (1) {
Chris Lattner5a4251c2013-01-20 02:13:19 +0000941 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbeyacb61942013-02-06 22:14:06 +0000942
Chris Lattner5a4251c2013-01-20 02:13:19 +0000943 switch (Entry.Kind) {
944 case BitstreamEntry::SubBlock: // Handled for us already.
945 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +0000946 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +0000947 case BitstreamEntry::EndBlock:
Rafael Espindolae076b532013-11-04 16:16:24 +0000948 return error_code::success();
Chris Lattner5a4251c2013-01-20 02:13:19 +0000949 case BitstreamEntry::Record:
950 // The interesting case.
951 break;
Chris Lattner0b2482a2007-04-23 21:26:05 +0000952 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000953
Chris Lattner0b2482a2007-04-23 21:26:05 +0000954 // Read a record.
955 Record.clear();
Chris Lattner5a4251c2013-01-20 02:13:19 +0000956 switch (Stream.readRecord(Entry.ID, Record)) {
Chris Lattner0b2482a2007-04-23 21:26:05 +0000957 default: // Default behavior: unknown type.
958 break;
Chris Lattner15e6d172007-05-04 19:11:41 +0000959 case bitc::VST_CODE_ENTRY: { // VST_ENTRY: [valueid, namechar x N]
Chris Lattner0b2482a2007-04-23 21:26:05 +0000960 if (ConvertToString(Record, 1, ValueName))
Rafael Espindolae076b532013-11-04 16:16:24 +0000961 return Error(InvalidRecord);
Chris Lattner0b2482a2007-04-23 21:26:05 +0000962 unsigned ValueID = Record[0];
Stephen Hines36b56882014-04-23 16:57:46 -0700963 if (ValueID >= ValueList.size() || !ValueList[ValueID])
Rafael Espindolae076b532013-11-04 16:16:24 +0000964 return Error(InvalidRecord);
Chris Lattner0b2482a2007-04-23 21:26:05 +0000965 Value *V = ValueList[ValueID];
Daniel Dunbara279bc32009-09-20 02:20:51 +0000966
Daniel Dunbar3f53fa92009-07-26 00:34:27 +0000967 V->setName(StringRef(ValueName.data(), ValueName.size()));
Chris Lattner0b2482a2007-04-23 21:26:05 +0000968 ValueName.clear();
969 break;
Reid Spencerc8f8a242007-05-04 01:43:33 +0000970 }
Bill Wendling5d7a5a42011-04-10 23:18:04 +0000971 case bitc::VST_CODE_BBENTRY: {
Chris Lattnere825ed52007-05-03 22:18:21 +0000972 if (ConvertToString(Record, 1, ValueName))
Rafael Espindolae076b532013-11-04 16:16:24 +0000973 return Error(InvalidRecord);
Chris Lattnere825ed52007-05-03 22:18:21 +0000974 BasicBlock *BB = getBasicBlock(Record[0]);
975 if (BB == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +0000976 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000977
Daniel Dunbar3f53fa92009-07-26 00:34:27 +0000978 BB->setName(StringRef(ValueName.data(), ValueName.size()));
Chris Lattnere825ed52007-05-03 22:18:21 +0000979 ValueName.clear();
980 break;
Chris Lattner0b2482a2007-04-23 21:26:05 +0000981 }
Reid Spencerc8f8a242007-05-04 01:43:33 +0000982 }
Chris Lattner0b2482a2007-04-23 21:26:05 +0000983 }
984}
985
Rafael Espindolae076b532013-11-04 16:16:24 +0000986error_code BitcodeReader::ParseMetadata() {
Devang Patel23598502010-01-11 18:52:33 +0000987 unsigned NextMDValueNo = MDValueList.size();
Devang Patele54abc92009-07-22 17:43:22 +0000988
989 if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +0000990 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000991
Devang Patele54abc92009-07-22 17:43:22 +0000992 SmallVector<uint64_t, 64> Record;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000993
Devang Patele54abc92009-07-22 17:43:22 +0000994 // Read all the records.
995 while (1) {
Chris Lattner5a4251c2013-01-20 02:13:19 +0000996 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbeyacb61942013-02-06 22:14:06 +0000997
Chris Lattner5a4251c2013-01-20 02:13:19 +0000998 switch (Entry.Kind) {
999 case BitstreamEntry::SubBlock: // Handled for us already.
1000 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +00001001 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00001002 case BitstreamEntry::EndBlock:
Rafael Espindolae076b532013-11-04 16:16:24 +00001003 return error_code::success();
Chris Lattner5a4251c2013-01-20 02:13:19 +00001004 case BitstreamEntry::Record:
1005 // The interesting case.
1006 break;
Devang Patele54abc92009-07-22 17:43:22 +00001007 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001008
Victor Hernandez24e64df2010-01-10 07:14:18 +00001009 bool IsFunctionLocal = false;
Devang Patele54abc92009-07-22 17:43:22 +00001010 // Read a record.
1011 Record.clear();
Chris Lattner5a4251c2013-01-20 02:13:19 +00001012 unsigned Code = Stream.readRecord(Entry.ID, Record);
Dan Gohman9b10dfb2010-09-13 18:00:48 +00001013 switch (Code) {
Devang Patele54abc92009-07-22 17:43:22 +00001014 default: // Default behavior: ignore.
1015 break;
Devang Patelaa993142009-07-29 22:34:41 +00001016 case bitc::METADATA_NAME: {
Chris Lattner1ca114a2013-01-20 02:54:05 +00001017 // Read name of the named metadata.
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001018 SmallString<8> Name(Record.begin(), Record.end());
Devang Patelaa993142009-07-29 22:34:41 +00001019 Record.clear();
1020 Code = Stream.ReadCode();
1021
Chris Lattner9d61dd92011-06-17 17:50:30 +00001022 // METADATA_NAME is always followed by METADATA_NAMED_NODE.
Chris Lattner5a4251c2013-01-20 02:13:19 +00001023 unsigned NextBitCode = Stream.readRecord(Code, Record);
Chris Lattner9d61dd92011-06-17 17:50:30 +00001024 assert(NextBitCode == bitc::METADATA_NAMED_NODE); (void)NextBitCode;
Devang Patelaa993142009-07-29 22:34:41 +00001025
1026 // Read named metadata elements.
1027 unsigned Size = Record.size();
Dan Gohman17aa92c2010-07-21 23:38:33 +00001028 NamedMDNode *NMD = TheModule->getOrInsertNamedMetadata(Name);
Devang Patelaa993142009-07-29 22:34:41 +00001029 for (unsigned i = 0; i != Size; ++i) {
Stephen Hines36b56882014-04-23 16:57:46 -07001030 MDNode *MD = dyn_cast_or_null<MDNode>(MDValueList.getValueFwdRef(Record[i]));
Chris Lattner70644e92010-01-09 02:02:37 +00001031 if (MD == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00001032 return Error(InvalidRecord);
Dan Gohman17aa92c2010-07-21 23:38:33 +00001033 NMD->addOperand(MD);
Devang Patelaa993142009-07-29 22:34:41 +00001034 }
Devang Patelaa993142009-07-29 22:34:41 +00001035 break;
1036 }
Chris Lattner9d61dd92011-06-17 17:50:30 +00001037 case bitc::METADATA_FN_NODE:
Victor Hernandez24e64df2010-01-10 07:14:18 +00001038 IsFunctionLocal = true;
1039 // fall-through
Chris Lattner9d61dd92011-06-17 17:50:30 +00001040 case bitc::METADATA_NODE: {
Dan Gohmanac809752010-07-13 19:33:27 +00001041 if (Record.size() % 2 == 1)
Rafael Espindolae076b532013-11-04 16:16:24 +00001042 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001043
Devang Patel104cf9e2009-07-23 01:07:34 +00001044 unsigned Size = Record.size();
1045 SmallVector<Value*, 8> Elts;
1046 for (unsigned i = 0; i != Size; i += 2) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001047 Type *Ty = getTypeByID(Record[i]);
Rafael Espindolae076b532013-11-04 16:16:24 +00001048 if (!Ty)
1049 return Error(InvalidRecord);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001050 if (Ty->isMetadataTy())
Devang Pateld5ac4042009-08-04 06:00:18 +00001051 Elts.push_back(MDValueList.getValueFwdRef(Record[i+1]));
Benjamin Kramerf0127052010-01-05 13:12:22 +00001052 else if (!Ty->isVoidTy())
Devang Patel104cf9e2009-07-23 01:07:34 +00001053 Elts.push_back(ValueList.getValueFwdRef(Record[i+1], Ty));
1054 else
1055 Elts.push_back(NULL);
1056 }
Jay Foadec9186b2011-04-21 19:59:31 +00001057 Value *V = MDNode::getWhenValsUnresolved(Context, Elts, IsFunctionLocal);
Victor Hernandez24e64df2010-01-10 07:14:18 +00001058 IsFunctionLocal = false;
Devang Patel23598502010-01-11 18:52:33 +00001059 MDValueList.AssignValue(V, NextMDValueNo++);
Devang Patel104cf9e2009-07-23 01:07:34 +00001060 break;
1061 }
Devang Patele54abc92009-07-22 17:43:22 +00001062 case bitc::METADATA_STRING: {
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001063 SmallString<8> String(Record.begin(), Record.end());
1064 Value *V = MDString::get(Context, String);
Devang Patel23598502010-01-11 18:52:33 +00001065 MDValueList.AssignValue(V, NextMDValueNo++);
Devang Patele54abc92009-07-22 17:43:22 +00001066 break;
1067 }
Devang Patele8e02132009-09-18 19:26:43 +00001068 case bitc::METADATA_KIND: {
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001069 if (Record.size() < 2)
Rafael Espindolae076b532013-11-04 16:16:24 +00001070 return Error(InvalidRecord);
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001071
Devang Patela2148402009-09-28 21:14:55 +00001072 unsigned Kind = Record[0];
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001073 SmallString<8> Name(Record.begin()+1, Record.end());
1074
Chris Lattner08113472009-12-29 09:01:33 +00001075 unsigned NewKind = TheModule->getMDKindID(Name.str());
Dan Gohman19538d12010-07-20 21:42:28 +00001076 if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second)
Rafael Espindolae076b532013-11-04 16:16:24 +00001077 return Error(ConflictingMETADATA_KINDRecords);
Devang Patele8e02132009-09-18 19:26:43 +00001078 break;
1079 }
Devang Patele54abc92009-07-22 17:43:22 +00001080 }
1081 }
1082}
1083
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00001084/// decodeSignRotatedValue - Decode a signed value stored with the sign bit in
Chris Lattner0eef0802007-04-24 04:04:35 +00001085/// the LSB for dense VBR encoding.
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00001086uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) {
Chris Lattner0eef0802007-04-24 04:04:35 +00001087 if ((V & 1) == 0)
1088 return V >> 1;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001089 if (V != 1)
Chris Lattner0eef0802007-04-24 04:04:35 +00001090 return -(V >> 1);
1091 // There is no such thing as -0 with integers. "-0" really means MININT.
1092 return 1ULL << 63;
1093}
1094
Chris Lattner07d98b42007-04-26 02:46:40 +00001095/// ResolveGlobalAndAliasInits - Resolve all of the initializers for global
1096/// values and aliases that we can.
Rafael Espindolae076b532013-11-04 16:16:24 +00001097error_code BitcodeReader::ResolveGlobalAndAliasInits() {
Chris Lattner07d98b42007-04-26 02:46:40 +00001098 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist;
1099 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist;
Peter Collingbourne1e3037f2013-09-16 01:08:15 +00001100 std::vector<std::pair<Function*, unsigned> > FunctionPrefixWorklist;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001101
Chris Lattner07d98b42007-04-26 02:46:40 +00001102 GlobalInitWorklist.swap(GlobalInits);
1103 AliasInitWorklist.swap(AliasInits);
Peter Collingbourne1e3037f2013-09-16 01:08:15 +00001104 FunctionPrefixWorklist.swap(FunctionPrefixes);
Chris Lattner07d98b42007-04-26 02:46:40 +00001105
1106 while (!GlobalInitWorklist.empty()) {
Chris Lattner198f34a2007-04-26 03:27:58 +00001107 unsigned ValID = GlobalInitWorklist.back().second;
Chris Lattner07d98b42007-04-26 02:46:40 +00001108 if (ValID >= ValueList.size()) {
1109 // Not ready to resolve this yet, it requires something later in the file.
Chris Lattner198f34a2007-04-26 03:27:58 +00001110 GlobalInits.push_back(GlobalInitWorklist.back());
Chris Lattner07d98b42007-04-26 02:46:40 +00001111 } else {
Stephen Hines36b56882014-04-23 16:57:46 -07001112 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
Chris Lattner07d98b42007-04-26 02:46:40 +00001113 GlobalInitWorklist.back().first->setInitializer(C);
1114 else
Rafael Espindolae076b532013-11-04 16:16:24 +00001115 return Error(ExpectedConstant);
Chris Lattner07d98b42007-04-26 02:46:40 +00001116 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001117 GlobalInitWorklist.pop_back();
Chris Lattner07d98b42007-04-26 02:46:40 +00001118 }
1119
1120 while (!AliasInitWorklist.empty()) {
1121 unsigned ValID = AliasInitWorklist.back().second;
1122 if (ValID >= ValueList.size()) {
1123 AliasInits.push_back(AliasInitWorklist.back());
1124 } else {
Stephen Hines36b56882014-04-23 16:57:46 -07001125 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
Anton Korobeynikov7dde0ff2007-04-28 14:57:59 +00001126 AliasInitWorklist.back().first->setAliasee(C);
Chris Lattner07d98b42007-04-26 02:46:40 +00001127 else
Rafael Espindolae076b532013-11-04 16:16:24 +00001128 return Error(ExpectedConstant);
Chris Lattner07d98b42007-04-26 02:46:40 +00001129 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001130 AliasInitWorklist.pop_back();
Chris Lattner07d98b42007-04-26 02:46:40 +00001131 }
Peter Collingbourne1e3037f2013-09-16 01:08:15 +00001132
1133 while (!FunctionPrefixWorklist.empty()) {
1134 unsigned ValID = FunctionPrefixWorklist.back().second;
1135 if (ValID >= ValueList.size()) {
1136 FunctionPrefixes.push_back(FunctionPrefixWorklist.back());
1137 } else {
Stephen Hines36b56882014-04-23 16:57:46 -07001138 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
Peter Collingbourne1e3037f2013-09-16 01:08:15 +00001139 FunctionPrefixWorklist.back().first->setPrefixData(C);
1140 else
Rafael Espindolae076b532013-11-04 16:16:24 +00001141 return Error(ExpectedConstant);
Peter Collingbourne1e3037f2013-09-16 01:08:15 +00001142 }
1143 FunctionPrefixWorklist.pop_back();
1144 }
1145
Rafael Espindolae076b532013-11-04 16:16:24 +00001146 return error_code::success();
Chris Lattner07d98b42007-04-26 02:46:40 +00001147}
1148
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001149static APInt ReadWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) {
1150 SmallVector<uint64_t, 8> Words(Vals.size());
1151 std::transform(Vals.begin(), Vals.end(), Words.begin(),
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00001152 BitcodeReader::decodeSignRotatedValue);
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001153
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00001154 return APInt(TypeBits, Words);
1155}
1156
Rafael Espindolae076b532013-11-04 16:16:24 +00001157error_code BitcodeReader::ParseConstants() {
Chris Lattnere17b6582007-05-05 00:17:00 +00001158 if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +00001159 return Error(InvalidRecord);
Chris Lattnere16504e2007-04-24 03:30:34 +00001160
1161 SmallVector<uint64_t, 64> Record;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001162
Chris Lattnere16504e2007-04-24 03:30:34 +00001163 // Read all the records for this value table.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001164 Type *CurTy = Type::getInt32Ty(Context);
Chris Lattner522b7b12007-04-24 05:48:56 +00001165 unsigned NextCstNo = ValueList.size();
Chris Lattnere16504e2007-04-24 03:30:34 +00001166 while (1) {
Chris Lattner5a4251c2013-01-20 02:13:19 +00001167 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbeyacb61942013-02-06 22:14:06 +00001168
Chris Lattner5a4251c2013-01-20 02:13:19 +00001169 switch (Entry.Kind) {
1170 case BitstreamEntry::SubBlock: // Handled for us already.
1171 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +00001172 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00001173 case BitstreamEntry::EndBlock:
1174 if (NextCstNo != ValueList.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001175 return Error(InvalidConstantReference);
Joe Abbeyacb61942013-02-06 22:14:06 +00001176
Chris Lattner5a4251c2013-01-20 02:13:19 +00001177 // Once all the constants have been read, go through and resolve forward
1178 // references.
1179 ValueList.ResolveConstantForwardRefs();
Rafael Espindolae076b532013-11-04 16:16:24 +00001180 return error_code::success();
Chris Lattner5a4251c2013-01-20 02:13:19 +00001181 case BitstreamEntry::Record:
1182 // The interesting case.
Chris Lattnerea693df2008-08-21 02:34:16 +00001183 break;
Chris Lattnere16504e2007-04-24 03:30:34 +00001184 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001185
Chris Lattnere16504e2007-04-24 03:30:34 +00001186 // Read a record.
1187 Record.clear();
1188 Value *V = 0;
Chris Lattner5a4251c2013-01-20 02:13:19 +00001189 unsigned BitCode = Stream.readRecord(Entry.ID, Record);
Dan Gohman1224c382009-07-20 21:19:07 +00001190 switch (BitCode) {
Chris Lattnere16504e2007-04-24 03:30:34 +00001191 default: // Default behavior: unknown constant
1192 case bitc::CST_CODE_UNDEF: // UNDEF
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001193 V = UndefValue::get(CurTy);
Chris Lattnere16504e2007-04-24 03:30:34 +00001194 break;
1195 case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid]
1196 if (Record.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +00001197 return Error(InvalidRecord);
Stephen Hines36b56882014-04-23 16:57:46 -07001198 if (Record[0] >= TypeList.size() || !TypeList[Record[0]])
Rafael Espindolae076b532013-11-04 16:16:24 +00001199 return Error(InvalidRecord);
Chris Lattnere16504e2007-04-24 03:30:34 +00001200 CurTy = TypeList[Record[0]];
Chris Lattner0eef0802007-04-24 04:04:35 +00001201 continue; // Skip the ValueList manipulation.
Chris Lattnere16504e2007-04-24 03:30:34 +00001202 case bitc::CST_CODE_NULL: // NULL
Owen Andersona7235ea2009-07-31 20:28:14 +00001203 V = Constant::getNullValue(CurTy);
Chris Lattnere16504e2007-04-24 03:30:34 +00001204 break;
1205 case bitc::CST_CODE_INTEGER: // INTEGER: [intval]
Duncan Sands1df98592010-02-16 11:11:14 +00001206 if (!CurTy->isIntegerTy() || Record.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +00001207 return Error(InvalidRecord);
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00001208 V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0]));
Chris Lattner0eef0802007-04-24 04:04:35 +00001209 break;
Chris Lattner15e6d172007-05-04 19:11:41 +00001210 case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
Duncan Sands1df98592010-02-16 11:11:14 +00001211 if (!CurTy->isIntegerTy() || Record.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +00001212 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001213
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001214 APInt VInt = ReadWideAPInt(Record,
1215 cast<IntegerType>(CurTy)->getBitWidth());
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00001216 V = ConstantInt::get(Context, VInt);
Michael Ilseman407a6162012-11-15 22:34:00 +00001217
Chris Lattner0eef0802007-04-24 04:04:35 +00001218 break;
1219 }
Dale Johannesen3f6eb742007-09-11 18:32:33 +00001220 case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval]
Chris Lattner0eef0802007-04-24 04:04:35 +00001221 if (Record.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +00001222 return Error(InvalidRecord);
Dan Gohmance163392011-12-17 00:04:22 +00001223 if (CurTy->isHalfTy())
Tim Northover0a29cb02013-01-22 09:46:31 +00001224 V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf,
1225 APInt(16, (uint16_t)Record[0])));
Dan Gohmance163392011-12-17 00:04:22 +00001226 else if (CurTy->isFloatTy())
Tim Northover0a29cb02013-01-22 09:46:31 +00001227 V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle,
1228 APInt(32, (uint32_t)Record[0])));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001229 else if (CurTy->isDoubleTy())
Tim Northover0a29cb02013-01-22 09:46:31 +00001230 V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble,
1231 APInt(64, Record[0])));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001232 else if (CurTy->isX86_FP80Ty()) {
Dale Johannesen1b25cb22009-03-23 21:16:53 +00001233 // Bits are not stored the same way as a normal i80 APInt, compensate.
1234 uint64_t Rearrange[2];
1235 Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);
1236 Rearrange[1] = Record[0] >> 48;
Tim Northover0a29cb02013-01-22 09:46:31 +00001237 V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended,
1238 APInt(80, Rearrange)));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001239 } else if (CurTy->isFP128Ty())
Tim Northover0a29cb02013-01-22 09:46:31 +00001240 V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad,
1241 APInt(128, Record)));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001242 else if (CurTy->isPPC_FP128Ty())
Tim Northover0a29cb02013-01-22 09:46:31 +00001243 V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble,
1244 APInt(128, Record)));
Chris Lattnere16504e2007-04-24 03:30:34 +00001245 else
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001246 V = UndefValue::get(CurTy);
Chris Lattnere16504e2007-04-24 03:30:34 +00001247 break;
Dale Johannesen3f6eb742007-09-11 18:32:33 +00001248 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001249
Chris Lattner15e6d172007-05-04 19:11:41 +00001250 case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]
1251 if (Record.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +00001252 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001253
Chris Lattner15e6d172007-05-04 19:11:41 +00001254 unsigned Size = Record.size();
Chris Lattnerd629efa2012-01-27 03:15:49 +00001255 SmallVector<Constant*, 16> Elts;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001256
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001257 if (StructType *STy = dyn_cast<StructType>(CurTy)) {
Chris Lattner522b7b12007-04-24 05:48:56 +00001258 for (unsigned i = 0; i != Size; ++i)
Chris Lattner15e6d172007-05-04 19:11:41 +00001259 Elts.push_back(ValueList.getConstantFwdRef(Record[i],
Chris Lattner522b7b12007-04-24 05:48:56 +00001260 STy->getElementType(i)));
Owen Anderson8fa33382009-07-27 22:29:26 +00001261 V = ConstantStruct::get(STy, Elts);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001262 } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) {
1263 Type *EltTy = ATy->getElementType();
Chris Lattner522b7b12007-04-24 05:48:56 +00001264 for (unsigned i = 0; i != Size; ++i)
Chris Lattner15e6d172007-05-04 19:11:41 +00001265 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
Owen Anderson1fd70962009-07-28 18:32:17 +00001266 V = ConstantArray::get(ATy, Elts);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001267 } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) {
1268 Type *EltTy = VTy->getElementType();
Chris Lattner522b7b12007-04-24 05:48:56 +00001269 for (unsigned i = 0; i != Size; ++i)
Chris Lattner15e6d172007-05-04 19:11:41 +00001270 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00001271 V = ConstantVector::get(Elts);
Chris Lattner522b7b12007-04-24 05:48:56 +00001272 } else {
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001273 V = UndefValue::get(CurTy);
Chris Lattner522b7b12007-04-24 05:48:56 +00001274 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001275 break;
1276 }
Chris Lattner2237f842012-02-05 02:41:35 +00001277 case bitc::CST_CODE_STRING: // STRING: [values]
Chris Lattnercb3d91b2007-05-06 00:53:07 +00001278 case bitc::CST_CODE_CSTRING: { // CSTRING: [values]
1279 if (Record.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +00001280 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001281
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001282 SmallString<16> Elts(Record.begin(), Record.end());
Chris Lattner2237f842012-02-05 02:41:35 +00001283 V = ConstantDataArray::getString(Context, Elts,
1284 BitCode == bitc::CST_CODE_CSTRING);
Chris Lattnercb3d91b2007-05-06 00:53:07 +00001285 break;
1286 }
Chris Lattnerd408f062012-01-30 00:51:16 +00001287 case bitc::CST_CODE_DATA: {// DATA: [n x value]
1288 if (Record.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +00001289 return Error(InvalidRecord);
Michael Ilseman407a6162012-11-15 22:34:00 +00001290
Chris Lattnerd408f062012-01-30 00:51:16 +00001291 Type *EltTy = cast<SequentialType>(CurTy)->getElementType();
1292 unsigned Size = Record.size();
Michael Ilseman407a6162012-11-15 22:34:00 +00001293
Chris Lattnerd408f062012-01-30 00:51:16 +00001294 if (EltTy->isIntegerTy(8)) {
1295 SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end());
1296 if (isa<VectorType>(CurTy))
1297 V = ConstantDataVector::get(Context, Elts);
1298 else
1299 V = ConstantDataArray::get(Context, Elts);
1300 } else if (EltTy->isIntegerTy(16)) {
1301 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
1302 if (isa<VectorType>(CurTy))
1303 V = ConstantDataVector::get(Context, Elts);
1304 else
1305 V = ConstantDataArray::get(Context, Elts);
1306 } else if (EltTy->isIntegerTy(32)) {
1307 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
1308 if (isa<VectorType>(CurTy))
1309 V = ConstantDataVector::get(Context, Elts);
1310 else
1311 V = ConstantDataArray::get(Context, Elts);
1312 } else if (EltTy->isIntegerTy(64)) {
1313 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
1314 if (isa<VectorType>(CurTy))
1315 V = ConstantDataVector::get(Context, Elts);
1316 else
1317 V = ConstantDataArray::get(Context, Elts);
1318 } else if (EltTy->isFloatTy()) {
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001319 SmallVector<float, 16> Elts(Size);
1320 std::transform(Record.begin(), Record.end(), Elts.begin(), BitsToFloat);
Chris Lattnerd408f062012-01-30 00:51:16 +00001321 if (isa<VectorType>(CurTy))
1322 V = ConstantDataVector::get(Context, Elts);
1323 else
1324 V = ConstantDataArray::get(Context, Elts);
1325 } else if (EltTy->isDoubleTy()) {
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001326 SmallVector<double, 16> Elts(Size);
1327 std::transform(Record.begin(), Record.end(), Elts.begin(),
1328 BitsToDouble);
Chris Lattnerd408f062012-01-30 00:51:16 +00001329 if (isa<VectorType>(CurTy))
1330 V = ConstantDataVector::get(Context, Elts);
1331 else
1332 V = ConstantDataArray::get(Context, Elts);
1333 } else {
Rafael Espindolae076b532013-11-04 16:16:24 +00001334 return Error(InvalidTypeForValue);
Chris Lattnerd408f062012-01-30 00:51:16 +00001335 }
1336 break;
1337 }
1338
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001339 case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval]
Rafael Espindolae076b532013-11-04 16:16:24 +00001340 if (Record.size() < 3)
1341 return Error(InvalidRecord);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001342 int Opc = GetDecodedBinaryOpcode(Record[0], CurTy);
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001343 if (Opc < 0) {
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001344 V = UndefValue::get(CurTy); // Unknown binop.
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001345 } else {
1346 Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy);
1347 Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy);
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001348 unsigned Flags = 0;
1349 if (Record.size() >= 4) {
1350 if (Opc == Instruction::Add ||
1351 Opc == Instruction::Sub ||
Chris Lattnerf067d582011-02-07 16:40:21 +00001352 Opc == Instruction::Mul ||
1353 Opc == Instruction::Shl) {
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001354 if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP))
1355 Flags |= OverflowingBinaryOperator::NoSignedWrap;
1356 if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
1357 Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
Chris Lattner35bda892011-02-06 21:44:57 +00001358 } else if (Opc == Instruction::SDiv ||
Chris Lattnerf067d582011-02-07 16:40:21 +00001359 Opc == Instruction::UDiv ||
1360 Opc == Instruction::LShr ||
1361 Opc == Instruction::AShr) {
Chris Lattner35bda892011-02-06 21:44:57 +00001362 if (Record[3] & (1 << bitc::PEO_EXACT))
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001363 Flags |= SDivOperator::IsExact;
1364 }
1365 }
1366 V = ConstantExpr::get(Opc, LHS, RHS, Flags);
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001367 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001368 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001369 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001370 case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval]
Rafael Espindolae076b532013-11-04 16:16:24 +00001371 if (Record.size() < 3)
1372 return Error(InvalidRecord);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001373 int Opc = GetDecodedCastOpcode(Record[0]);
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001374 if (Opc < 0) {
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001375 V = UndefValue::get(CurTy); // Unknown cast.
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001376 } else {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001377 Type *OpTy = getTypeByID(Record[1]);
Rafael Espindolae076b532013-11-04 16:16:24 +00001378 if (!OpTy)
1379 return Error(InvalidRecord);
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001380 Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy);
Matt Arsenault59d3ae62013-11-15 01:34:59 +00001381 V = UpgradeBitCastExpr(Opc, Op, CurTy);
1382 if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy);
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001383 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001384 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001385 }
Dan Gohmandd8004d2009-07-27 21:53:46 +00001386 case bitc::CST_CODE_CE_INBOUNDS_GEP:
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001387 case bitc::CST_CODE_CE_GEP: { // CE_GEP: [n x operands]
Rafael Espindolae076b532013-11-04 16:16:24 +00001388 if (Record.size() & 1)
1389 return Error(InvalidRecord);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001390 SmallVector<Constant*, 16> Elts;
Chris Lattner15e6d172007-05-04 19:11:41 +00001391 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001392 Type *ElTy = getTypeByID(Record[i]);
Rafael Espindolae076b532013-11-04 16:16:24 +00001393 if (!ElTy)
1394 return Error(InvalidRecord);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001395 Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], ElTy));
1396 }
Jay Foaddab3d292011-07-21 14:31:17 +00001397 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
Jay Foad4b5e2072011-07-21 15:15:37 +00001398 V = ConstantExpr::getGetElementPtr(Elts[0], Indices,
1399 BitCode ==
1400 bitc::CST_CODE_CE_INBOUNDS_GEP);
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001401 break;
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001402 }
Joe Abbey405b6502013-09-12 22:02:31 +00001403 case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#]
Rafael Espindolae076b532013-11-04 16:16:24 +00001404 if (Record.size() < 3)
1405 return Error(InvalidRecord);
Joe Abbey405b6502013-09-12 22:02:31 +00001406
1407 Type *SelectorTy = Type::getInt1Ty(Context);
1408
1409 // If CurTy is a vector of length n, then Record[0] must be a <n x i1>
1410 // vector. Otherwise, it must be a single bit.
1411 if (VectorType *VTy = dyn_cast<VectorType>(CurTy))
1412 SelectorTy = VectorType::get(Type::getInt1Ty(Context),
1413 VTy->getNumElements());
1414
1415 V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0],
1416 SelectorTy),
1417 ValueList.getConstantFwdRef(Record[1],CurTy),
1418 ValueList.getConstantFwdRef(Record[2],CurTy));
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001419 break;
Joe Abbey405b6502013-09-12 22:02:31 +00001420 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001421 case bitc::CST_CODE_CE_EXTRACTELT: { // CE_EXTRACTELT: [opty, opval, opval]
Rafael Espindolae076b532013-11-04 16:16:24 +00001422 if (Record.size() < 3)
1423 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001424 VectorType *OpTy =
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001425 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
Rafael Espindolae076b532013-11-04 16:16:24 +00001426 if (OpTy == 0)
1427 return Error(InvalidRecord);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001428 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
Joe Abbey170a15e2012-11-25 15:23:39 +00001429 Constant *Op1 = ValueList.getConstantFwdRef(Record[2],
Joe Abbeye46b14a2012-11-19 19:22:55 +00001430 Type::getInt32Ty(Context));
Owen Andersonbaf3c402009-07-29 18:55:55 +00001431 V = ConstantExpr::getExtractElement(Op0, Op1);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001432 break;
1433 }
1434 case bitc::CST_CODE_CE_INSERTELT: { // CE_INSERTELT: [opval, opval, opval]
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001435 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001436 if (Record.size() < 3 || OpTy == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00001437 return Error(InvalidRecord);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001438 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
1439 Constant *Op1 = ValueList.getConstantFwdRef(Record[1],
1440 OpTy->getElementType());
Joe Abbey170a15e2012-11-25 15:23:39 +00001441 Constant *Op2 = ValueList.getConstantFwdRef(Record[2],
Joe Abbeye46b14a2012-11-19 19:22:55 +00001442 Type::getInt32Ty(Context));
Owen Andersonbaf3c402009-07-29 18:55:55 +00001443 V = ConstantExpr::getInsertElement(Op0, Op1, Op2);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001444 break;
1445 }
1446 case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval]
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001447 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001448 if (Record.size() < 3 || OpTy == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00001449 return Error(InvalidRecord);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001450 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
1451 Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001452 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
Owen Anderson74a77812009-07-07 20:18:58 +00001453 OpTy->getNumElements());
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001454 Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy);
Owen Andersonbaf3c402009-07-29 18:55:55 +00001455 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001456 break;
1457 }
Nate Begeman0f123cf2009-02-12 21:28:33 +00001458 case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval]
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001459 VectorType *RTy = dyn_cast<VectorType>(CurTy);
1460 VectorType *OpTy =
Duncan Sandsf22b7462010-10-28 15:47:26 +00001461 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
Nate Begeman0f123cf2009-02-12 21:28:33 +00001462 if (Record.size() < 4 || RTy == 0 || OpTy == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00001463 return Error(InvalidRecord);
Nate Begeman0f123cf2009-02-12 21:28:33 +00001464 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
1465 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001466 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
Owen Anderson74a77812009-07-07 20:18:58 +00001467 RTy->getNumElements());
Nate Begeman0f123cf2009-02-12 21:28:33 +00001468 Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy);
Owen Andersonbaf3c402009-07-29 18:55:55 +00001469 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Nate Begeman0f123cf2009-02-12 21:28:33 +00001470 break;
1471 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001472 case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred]
Rafael Espindolae076b532013-11-04 16:16:24 +00001473 if (Record.size() < 4)
1474 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001475 Type *OpTy = getTypeByID(Record[0]);
Rafael Espindolae076b532013-11-04 16:16:24 +00001476 if (OpTy == 0)
1477 return Error(InvalidRecord);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001478 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
1479 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
1480
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001481 if (OpTy->isFPOrFPVectorTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +00001482 V = ConstantExpr::getFCmp(Record[3], Op0, Op1);
Nate Begemanac80ade2008-05-12 19:01:56 +00001483 else
Owen Andersonbaf3c402009-07-29 18:55:55 +00001484 V = ConstantExpr::getICmp(Record[3], Op0, Op1);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001485 break;
Chris Lattner522b7b12007-04-24 05:48:56 +00001486 }
Chad Rosier581600b2012-09-05 19:00:49 +00001487 // This maintains backward compatibility, pre-asm dialect keywords.
Chad Rosier27b25c22012-09-05 06:28:52 +00001488 // FIXME: Remove with the 4.0 release.
Chad Rosierf16ae582012-09-05 00:56:20 +00001489 case bitc::CST_CODE_INLINEASM_OLD: {
Rafael Espindolae076b532013-11-04 16:16:24 +00001490 if (Record.size() < 2)
1491 return Error(InvalidRecord);
Chris Lattner2bce93a2007-05-06 01:58:20 +00001492 std::string AsmStr, ConstrStr;
Dale Johannesen43602982009-10-13 20:46:56 +00001493 bool HasSideEffects = Record[0] & 1;
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +00001494 bool IsAlignStack = Record[0] >> 1;
Chris Lattner2bce93a2007-05-06 01:58:20 +00001495 unsigned AsmStrSize = Record[1];
1496 if (2+AsmStrSize >= Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001497 return Error(InvalidRecord);
Chris Lattner2bce93a2007-05-06 01:58:20 +00001498 unsigned ConstStrSize = Record[2+AsmStrSize];
1499 if (3+AsmStrSize+ConstStrSize > Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001500 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001501
Chris Lattner2bce93a2007-05-06 01:58:20 +00001502 for (unsigned i = 0; i != AsmStrSize; ++i)
1503 AsmStr += (char)Record[2+i];
1504 for (unsigned i = 0; i != ConstStrSize; ++i)
1505 ConstrStr += (char)Record[3+AsmStrSize+i];
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001506 PointerType *PTy = cast<PointerType>(CurTy);
Chris Lattner2bce93a2007-05-06 01:58:20 +00001507 V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +00001508 AsmStr, ConstrStr, HasSideEffects, IsAlignStack);
Chris Lattner2bce93a2007-05-06 01:58:20 +00001509 break;
1510 }
Chad Rosier581600b2012-09-05 19:00:49 +00001511 // This version adds support for the asm dialect keywords (e.g.,
1512 // inteldialect).
Chad Rosierf16ae582012-09-05 00:56:20 +00001513 case bitc::CST_CODE_INLINEASM: {
Rafael Espindolae076b532013-11-04 16:16:24 +00001514 if (Record.size() < 2)
1515 return Error(InvalidRecord);
Chad Rosierf16ae582012-09-05 00:56:20 +00001516 std::string AsmStr, ConstrStr;
1517 bool HasSideEffects = Record[0] & 1;
1518 bool IsAlignStack = (Record[0] >> 1) & 1;
1519 unsigned AsmDialect = Record[0] >> 2;
1520 unsigned AsmStrSize = Record[1];
1521 if (2+AsmStrSize >= Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001522 return Error(InvalidRecord);
Chad Rosierf16ae582012-09-05 00:56:20 +00001523 unsigned ConstStrSize = Record[2+AsmStrSize];
1524 if (3+AsmStrSize+ConstStrSize > Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001525 return Error(InvalidRecord);
Chad Rosierf16ae582012-09-05 00:56:20 +00001526
1527 for (unsigned i = 0; i != AsmStrSize; ++i)
1528 AsmStr += (char)Record[2+i];
1529 for (unsigned i = 0; i != ConstStrSize; ++i)
1530 ConstrStr += (char)Record[3+AsmStrSize+i];
1531 PointerType *PTy = cast<PointerType>(CurTy);
1532 V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
1533 AsmStr, ConstrStr, HasSideEffects, IsAlignStack,
Chad Rosier581600b2012-09-05 19:00:49 +00001534 InlineAsm::AsmDialect(AsmDialect));
Chad Rosierf16ae582012-09-05 00:56:20 +00001535 break;
1536 }
Chris Lattner50b136d2009-10-28 05:53:48 +00001537 case bitc::CST_CODE_BLOCKADDRESS:{
Rafael Espindolae076b532013-11-04 16:16:24 +00001538 if (Record.size() < 3)
1539 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001540 Type *FnTy = getTypeByID(Record[0]);
Rafael Espindolae076b532013-11-04 16:16:24 +00001541 if (FnTy == 0)
1542 return Error(InvalidRecord);
Chris Lattner50b136d2009-10-28 05:53:48 +00001543 Function *Fn =
1544 dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy));
Rafael Espindolae076b532013-11-04 16:16:24 +00001545 if (Fn == 0)
1546 return Error(InvalidRecord);
Benjamin Kramer122f5e52012-09-21 14:34:31 +00001547
1548 // If the function is already parsed we can insert the block address right
1549 // away.
1550 if (!Fn->empty()) {
1551 Function::iterator BBI = Fn->begin(), BBE = Fn->end();
1552 for (size_t I = 0, E = Record[2]; I != E; ++I) {
1553 if (BBI == BBE)
Rafael Espindolae076b532013-11-04 16:16:24 +00001554 return Error(InvalidID);
Benjamin Kramer122f5e52012-09-21 14:34:31 +00001555 ++BBI;
1556 }
1557 V = BlockAddress::get(Fn, BBI);
1558 } else {
1559 // Otherwise insert a placeholder and remember it so it can be inserted
1560 // when the function is parsed.
1561 GlobalVariable *FwdRef = new GlobalVariable(*Fn->getParent(),
1562 Type::getInt8Ty(Context),
Chris Lattner50b136d2009-10-28 05:53:48 +00001563 false, GlobalValue::InternalLinkage,
Benjamin Kramer122f5e52012-09-21 14:34:31 +00001564 0, "");
1565 BlockAddrFwdRefs[Fn].push_back(std::make_pair(Record[2], FwdRef));
1566 V = FwdRef;
1567 }
Chris Lattner50b136d2009-10-28 05:53:48 +00001568 break;
Michael Ilseman407a6162012-11-15 22:34:00 +00001569 }
Chris Lattnere16504e2007-04-24 03:30:34 +00001570 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001571
Chris Lattnera7c49aa2007-05-01 07:01:57 +00001572 ValueList.AssignValue(V, NextCstNo);
Chris Lattner522b7b12007-04-24 05:48:56 +00001573 ++NextCstNo;
Chris Lattnere16504e2007-04-24 03:30:34 +00001574 }
1575}
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001576
Rafael Espindolae076b532013-11-04 16:16:24 +00001577error_code BitcodeReader::ParseUseLists() {
Chad Rosiercbbb0962011-12-07 21:44:12 +00001578 if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +00001579 return Error(InvalidRecord);
Chad Rosiercbbb0962011-12-07 21:44:12 +00001580
1581 SmallVector<uint64_t, 64> Record;
Michael Ilseman407a6162012-11-15 22:34:00 +00001582
Chad Rosiercbbb0962011-12-07 21:44:12 +00001583 // Read all the records.
1584 while (1) {
Chris Lattner5a4251c2013-01-20 02:13:19 +00001585 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbeyacb61942013-02-06 22:14:06 +00001586
Chris Lattner5a4251c2013-01-20 02:13:19 +00001587 switch (Entry.Kind) {
1588 case BitstreamEntry::SubBlock: // Handled for us already.
1589 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +00001590 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00001591 case BitstreamEntry::EndBlock:
Rafael Espindolae076b532013-11-04 16:16:24 +00001592 return error_code::success();
Chris Lattner5a4251c2013-01-20 02:13:19 +00001593 case BitstreamEntry::Record:
1594 // The interesting case.
1595 break;
Chad Rosiercbbb0962011-12-07 21:44:12 +00001596 }
Michael Ilseman407a6162012-11-15 22:34:00 +00001597
Chad Rosiercbbb0962011-12-07 21:44:12 +00001598 // Read a use list record.
1599 Record.clear();
Chris Lattner5a4251c2013-01-20 02:13:19 +00001600 switch (Stream.readRecord(Entry.ID, Record)) {
Chad Rosiercbbb0962011-12-07 21:44:12 +00001601 default: // Default behavior: unknown type.
1602 break;
1603 case bitc::USELIST_CODE_ENTRY: { // USELIST_CODE_ENTRY: TBD.
1604 unsigned RecordLength = Record.size();
1605 if (RecordLength < 1)
Rafael Espindolae076b532013-11-04 16:16:24 +00001606 return Error(InvalidRecord);
Chad Rosiercbbb0962011-12-07 21:44:12 +00001607 UseListRecords.push_back(Record);
1608 break;
1609 }
1610 }
1611 }
1612}
1613
Chris Lattner980e5aa2007-05-01 05:52:21 +00001614/// RememberAndSkipFunctionBody - When we see the block for a function body,
1615/// remember where it is and then skip it. This lets us lazily deserialize the
1616/// functions.
Rafael Espindolae076b532013-11-04 16:16:24 +00001617error_code BitcodeReader::RememberAndSkipFunctionBody() {
Chris Lattner48f84872007-05-01 04:59:48 +00001618 // Get the function we are talking about.
1619 if (FunctionsWithBodies.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +00001620 return Error(InsufficientFunctionProtos);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001621
Chris Lattner48f84872007-05-01 04:59:48 +00001622 Function *Fn = FunctionsWithBodies.back();
1623 FunctionsWithBodies.pop_back();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001624
Chris Lattner48f84872007-05-01 04:59:48 +00001625 // Save the current stream state.
1626 uint64_t CurBit = Stream.GetCurrentBitNo();
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00001627 DeferredFunctionInfo[Fn] = CurBit;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001628
Chris Lattner48f84872007-05-01 04:59:48 +00001629 // Skip over the function block for now.
1630 if (Stream.SkipBlock())
Rafael Espindolae076b532013-11-04 16:16:24 +00001631 return Error(InvalidRecord);
1632 return error_code::success();
Chris Lattner48f84872007-05-01 04:59:48 +00001633}
1634
Rafael Espindolae076b532013-11-04 16:16:24 +00001635error_code BitcodeReader::GlobalCleanup() {
Derek Schuff2ea93872012-02-06 22:30:29 +00001636 // Patch the initializers for globals and aliases up.
1637 ResolveGlobalAndAliasInits();
1638 if (!GlobalInits.empty() || !AliasInits.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +00001639 return Error(MalformedGlobalInitializerSet);
Derek Schuff2ea93872012-02-06 22:30:29 +00001640
1641 // Look for intrinsic functions which need to be upgraded at some point
1642 for (Module::iterator FI = TheModule->begin(), FE = TheModule->end();
1643 FI != FE; ++FI) {
1644 Function *NewFn;
1645 if (UpgradeIntrinsicFunction(FI, NewFn))
1646 UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn));
1647 }
1648
1649 // Look for global variables which need to be renamed.
1650 for (Module::global_iterator
1651 GI = TheModule->global_begin(), GE = TheModule->global_end();
1652 GI != GE; ++GI)
1653 UpgradeGlobalVariable(GI);
1654 // Force deallocation of memory for these vectors to favor the client that
1655 // want lazy deserialization.
1656 std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits);
1657 std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits);
Rafael Espindolae076b532013-11-04 16:16:24 +00001658 return error_code::success();
Derek Schuff2ea93872012-02-06 22:30:29 +00001659}
1660
Rafael Espindolae076b532013-11-04 16:16:24 +00001661error_code BitcodeReader::ParseModule(bool Resume) {
Derek Schuff2ea93872012-02-06 22:30:29 +00001662 if (Resume)
1663 Stream.JumpToBit(NextUnreadBit);
1664 else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +00001665 return Error(InvalidRecord);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001666
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001667 SmallVector<uint64_t, 64> Record;
1668 std::vector<std::string> SectionTable;
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001669 std::vector<std::string> GCTable;
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001670
1671 // Read all the records for this module.
Chris Lattner5a4251c2013-01-20 02:13:19 +00001672 while (1) {
1673 BitstreamEntry Entry = Stream.advance();
Joe Abbeyacb61942013-02-06 22:14:06 +00001674
Chris Lattner5a4251c2013-01-20 02:13:19 +00001675 switch (Entry.Kind) {
1676 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +00001677 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00001678 case BitstreamEntry::EndBlock:
Derek Schuff2ea93872012-02-06 22:30:29 +00001679 return GlobalCleanup();
Joe Abbeyacb61942013-02-06 22:14:06 +00001680
Chris Lattner5a4251c2013-01-20 02:13:19 +00001681 case BitstreamEntry::SubBlock:
1682 switch (Entry.ID) {
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001683 default: // Skip unknown content.
1684 if (Stream.SkipBlock())
Rafael Espindolae076b532013-11-04 16:16:24 +00001685 return Error(InvalidRecord);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001686 break;
Chris Lattner3f799802007-05-05 18:57:30 +00001687 case bitc::BLOCKINFO_BLOCK_ID:
1688 if (Stream.ReadBlockInfoBlock())
Rafael Espindolae076b532013-11-04 16:16:24 +00001689 return Error(MalformedBlock);
Chris Lattner3f799802007-05-05 18:57:30 +00001690 break;
Chris Lattner48c85b82007-05-04 03:30:17 +00001691 case bitc::PARAMATTR_BLOCK_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00001692 if (error_code EC = ParseAttributeBlock())
1693 return EC;
Chris Lattner48c85b82007-05-04 03:30:17 +00001694 break;
Bill Wendlingc3ba0a82013-02-10 23:24:25 +00001695 case bitc::PARAMATTR_GROUP_BLOCK_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00001696 if (error_code EC = ParseAttributeGroupBlock())
1697 return EC;
Bill Wendlingc3ba0a82013-02-10 23:24:25 +00001698 break;
Chris Lattner1afcace2011-07-09 17:41:24 +00001699 case bitc::TYPE_BLOCK_ID_NEW:
Rafael Espindolae076b532013-11-04 16:16:24 +00001700 if (error_code EC = ParseTypeTable())
1701 return EC;
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001702 break;
Chris Lattner0b2482a2007-04-23 21:26:05 +00001703 case bitc::VALUE_SYMTAB_BLOCK_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00001704 if (error_code EC = ParseValueSymbolTable())
1705 return EC;
Derek Schuff2ea93872012-02-06 22:30:29 +00001706 SeenValueSymbolTable = true;
Chris Lattner0b2482a2007-04-23 21:26:05 +00001707 break;
Chris Lattnere16504e2007-04-24 03:30:34 +00001708 case bitc::CONSTANTS_BLOCK_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00001709 if (error_code EC = ParseConstants())
1710 return EC;
1711 if (error_code EC = ResolveGlobalAndAliasInits())
1712 return EC;
Chris Lattnere16504e2007-04-24 03:30:34 +00001713 break;
Devang Patele54abc92009-07-22 17:43:22 +00001714 case bitc::METADATA_BLOCK_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00001715 if (error_code EC = ParseMetadata())
1716 return EC;
Devang Patele54abc92009-07-22 17:43:22 +00001717 break;
Chris Lattner48f84872007-05-01 04:59:48 +00001718 case bitc::FUNCTION_BLOCK_ID:
1719 // If this is the first function body we've seen, reverse the
1720 // FunctionsWithBodies list.
Derek Schuff2ea93872012-02-06 22:30:29 +00001721 if (!SeenFirstFunctionBody) {
Chris Lattner48f84872007-05-01 04:59:48 +00001722 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
Rafael Espindolae076b532013-11-04 16:16:24 +00001723 if (error_code EC = GlobalCleanup())
1724 return EC;
Derek Schuff2ea93872012-02-06 22:30:29 +00001725 SeenFirstFunctionBody = true;
Chris Lattner48f84872007-05-01 04:59:48 +00001726 }
Joe Abbeyacb61942013-02-06 22:14:06 +00001727
Rafael Espindolae076b532013-11-04 16:16:24 +00001728 if (error_code EC = RememberAndSkipFunctionBody())
1729 return EC;
Derek Schuff2ea93872012-02-06 22:30:29 +00001730 // For streaming bitcode, suspend parsing when we reach the function
1731 // bodies. Subsequent materialization calls will resume it when
1732 // necessary. For streaming, the function bodies must be at the end of
1733 // the bitcode. If the bitcode file is old, the symbol table will be
1734 // at the end instead and will not have been seen yet. In this case,
1735 // just finish the parse now.
1736 if (LazyStreamer && SeenValueSymbolTable) {
1737 NextUnreadBit = Stream.GetCurrentBitNo();
Rafael Espindolae076b532013-11-04 16:16:24 +00001738 return error_code::success();
Derek Schuff2ea93872012-02-06 22:30:29 +00001739 }
Chris Lattner48f84872007-05-01 04:59:48 +00001740 break;
Chad Rosiercbbb0962011-12-07 21:44:12 +00001741 case bitc::USELIST_BLOCK_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00001742 if (error_code EC = ParseUseLists())
1743 return EC;
Chad Rosiercbbb0962011-12-07 21:44:12 +00001744 break;
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001745 }
1746 continue;
Joe Abbeyacb61942013-02-06 22:14:06 +00001747
Chris Lattner5a4251c2013-01-20 02:13:19 +00001748 case BitstreamEntry::Record:
1749 // The interesting case.
1750 break;
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001751 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001752
Daniel Dunbara279bc32009-09-20 02:20:51 +00001753
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001754 // Read a record.
Chris Lattner5a4251c2013-01-20 02:13:19 +00001755 switch (Stream.readRecord(Entry.ID, Record)) {
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001756 default: break; // Default behavior, ignore unknown content.
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00001757 case bitc::MODULE_CODE_VERSION: { // VERSION: [version#]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001758 if (Record.size() < 1)
Rafael Espindolae076b532013-11-04 16:16:24 +00001759 return Error(InvalidRecord);
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00001760 // Only version #0 and #1 are supported so far.
1761 unsigned module_version = Record[0];
1762 switch (module_version) {
Rafael Espindolae076b532013-11-04 16:16:24 +00001763 default:
1764 return Error(InvalidValue);
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00001765 case 0:
1766 UseRelativeIDs = false;
1767 break;
1768 case 1:
1769 UseRelativeIDs = true;
1770 break;
1771 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001772 break;
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00001773 }
Chris Lattner15e6d172007-05-04 19:11:41 +00001774 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001775 std::string S;
1776 if (ConvertToString(Record, 0, S))
Rafael Espindolae076b532013-11-04 16:16:24 +00001777 return Error(InvalidRecord);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001778 TheModule->setTargetTriple(S);
1779 break;
1780 }
Chris Lattner15e6d172007-05-04 19:11:41 +00001781 case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001782 std::string S;
1783 if (ConvertToString(Record, 0, S))
Rafael Espindolae076b532013-11-04 16:16:24 +00001784 return Error(InvalidRecord);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001785 TheModule->setDataLayout(S);
1786 break;
1787 }
Chris Lattner15e6d172007-05-04 19:11:41 +00001788 case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001789 std::string S;
1790 if (ConvertToString(Record, 0, S))
Rafael Espindolae076b532013-11-04 16:16:24 +00001791 return Error(InvalidRecord);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001792 TheModule->setModuleInlineAsm(S);
1793 break;
1794 }
Bill Wendling3defc0b2012-11-28 08:41:48 +00001795 case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N]
1796 // FIXME: Remove in 4.0.
1797 std::string S;
1798 if (ConvertToString(Record, 0, S))
Rafael Espindolae076b532013-11-04 16:16:24 +00001799 return Error(InvalidRecord);
Bill Wendling3defc0b2012-11-28 08:41:48 +00001800 // Ignore value.
1801 break;
1802 }
Chris Lattner15e6d172007-05-04 19:11:41 +00001803 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001804 std::string S;
1805 if (ConvertToString(Record, 0, S))
Rafael Espindolae076b532013-11-04 16:16:24 +00001806 return Error(InvalidRecord);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001807 SectionTable.push_back(S);
1808 break;
1809 }
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001810 case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N]
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001811 std::string S;
1812 if (ConvertToString(Record, 0, S))
Rafael Espindolae076b532013-11-04 16:16:24 +00001813 return Error(InvalidRecord);
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001814 GCTable.push_back(S);
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001815 break;
1816 }
Christopher Lambfe63fb92007-12-11 08:59:05 +00001817 // GLOBALVAR: [pointer type, isconst, initid,
Rafael Espindolabea46262011-01-08 16:42:36 +00001818 // linkage, alignment, section, visibility, threadlocal,
Stephen Hines36b56882014-04-23 16:57:46 -07001819 // unnamed_addr, dllstorageclass]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001820 case bitc::MODULE_CODE_GLOBALVAR: {
Chris Lattner36d5e7d2007-04-23 16:04:05 +00001821 if (Record.size() < 6)
Rafael Espindolae076b532013-11-04 16:16:24 +00001822 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001823 Type *Ty = getTypeByID(Record[0]);
Rafael Espindolae076b532013-11-04 16:16:24 +00001824 if (!Ty)
1825 return Error(InvalidRecord);
Duncan Sands1df98592010-02-16 11:11:14 +00001826 if (!Ty->isPointerTy())
Rafael Espindolae076b532013-11-04 16:16:24 +00001827 return Error(InvalidTypeForValue);
Christopher Lambfe63fb92007-12-11 08:59:05 +00001828 unsigned AddressSpace = cast<PointerType>(Ty)->getAddressSpace();
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001829 Ty = cast<PointerType>(Ty)->getElementType();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001830
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001831 bool isConstant = Record[1];
1832 GlobalValue::LinkageTypes Linkage = GetDecodedLinkage(Record[3]);
1833 unsigned Alignment = (1 << Record[4]) >> 1;
1834 std::string Section;
1835 if (Record[5]) {
1836 if (Record[5]-1 >= SectionTable.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001837 return Error(InvalidID);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001838 Section = SectionTable[Record[5]-1];
1839 }
Chris Lattner36d5e7d2007-04-23 16:04:05 +00001840 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
Chris Lattner5f32c012007-05-06 19:27:46 +00001841 if (Record.size() > 6)
1842 Visibility = GetDecodedVisibility(Record[6]);
Hans Wennborgce718ff2012-06-23 11:37:03 +00001843
1844 GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal;
Chris Lattner5f32c012007-05-06 19:27:46 +00001845 if (Record.size() > 7)
Hans Wennborgce718ff2012-06-23 11:37:03 +00001846 TLM = GetDecodedThreadLocalMode(Record[7]);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001847
Rafael Espindolabea46262011-01-08 16:42:36 +00001848 bool UnnamedAddr = false;
1849 if (Record.size() > 8)
1850 UnnamedAddr = Record[8];
1851
Michael Gottesmana2de37c2013-02-05 05:57:38 +00001852 bool ExternallyInitialized = false;
1853 if (Record.size() > 9)
1854 ExternallyInitialized = Record[9];
1855
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001856 GlobalVariable *NewGV =
Daniel Dunbara279bc32009-09-20 02:20:51 +00001857 new GlobalVariable(*TheModule, Ty, isConstant, Linkage, 0, "", 0,
Michael Gottesmana2de37c2013-02-05 05:57:38 +00001858 TLM, AddressSpace, ExternallyInitialized);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001859 NewGV->setAlignment(Alignment);
1860 if (!Section.empty())
1861 NewGV->setSection(Section);
1862 NewGV->setVisibility(Visibility);
Rafael Espindolabea46262011-01-08 16:42:36 +00001863 NewGV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001864
Stephen Hines36b56882014-04-23 16:57:46 -07001865 if (Record.size() > 10)
1866 NewGV->setDLLStorageClass(GetDecodedDLLStorageClass(Record[10]));
1867 else
1868 UpgradeDLLImportExportLinkage(NewGV, Record[3]);
1869
Chris Lattner0b2482a2007-04-23 21:26:05 +00001870 ValueList.push_back(NewGV);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001871
Chris Lattner6dbfd7b2007-04-24 00:18:21 +00001872 // Remember which value to use for the global initializer.
1873 if (unsigned InitID = Record[2])
1874 GlobalInits.push_back(std::make_pair(NewGV, InitID-1));
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001875 break;
1876 }
Chris Lattnera9bb7132007-05-08 05:38:01 +00001877 // FUNCTION: [type, callingconv, isproto, linkage, paramattr,
Stephen Hines36b56882014-04-23 16:57:46 -07001878 // alignment, section, visibility, gc, unnamed_addr,
1879 // dllstorageclass]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001880 case bitc::MODULE_CODE_FUNCTION: {
Chris Lattnera9bb7132007-05-08 05:38:01 +00001881 if (Record.size() < 8)
Rafael Espindolae076b532013-11-04 16:16:24 +00001882 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001883 Type *Ty = getTypeByID(Record[0]);
Rafael Espindolae076b532013-11-04 16:16:24 +00001884 if (!Ty)
1885 return Error(InvalidRecord);
Duncan Sands1df98592010-02-16 11:11:14 +00001886 if (!Ty->isPointerTy())
Rafael Espindolae076b532013-11-04 16:16:24 +00001887 return Error(InvalidTypeForValue);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001888 FunctionType *FTy =
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001889 dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType());
1890 if (!FTy)
Rafael Espindolae076b532013-11-04 16:16:24 +00001891 return Error(InvalidTypeForValue);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001892
Gabor Greif051a9502008-04-06 20:25:17 +00001893 Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage,
1894 "", TheModule);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001895
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001896 Func->setCallingConv(static_cast<CallingConv::ID>(Record[1]));
Chris Lattner48f84872007-05-01 04:59:48 +00001897 bool isProto = Record[2];
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001898 Func->setLinkage(GetDecodedLinkage(Record[3]));
Devang Patel05988662008-09-25 21:00:45 +00001899 Func->setAttributes(getAttributes(Record[4]));
Daniel Dunbara279bc32009-09-20 02:20:51 +00001900
Chris Lattnera9bb7132007-05-08 05:38:01 +00001901 Func->setAlignment((1 << Record[5]) >> 1);
1902 if (Record[6]) {
1903 if (Record[6]-1 >= SectionTable.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001904 return Error(InvalidID);
Chris Lattnera9bb7132007-05-08 05:38:01 +00001905 Func->setSection(SectionTable[Record[6]-1]);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001906 }
Chris Lattnera9bb7132007-05-08 05:38:01 +00001907 Func->setVisibility(GetDecodedVisibility(Record[7]));
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001908 if (Record.size() > 8 && Record[8]) {
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001909 if (Record[8]-1 > GCTable.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001910 return Error(InvalidID);
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001911 Func->setGC(GCTable[Record[8]-1].c_str());
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001912 }
Rafael Espindolabea46262011-01-08 16:42:36 +00001913 bool UnnamedAddr = false;
1914 if (Record.size() > 9)
1915 UnnamedAddr = Record[9];
1916 Func->setUnnamedAddr(UnnamedAddr);
Peter Collingbourne1e3037f2013-09-16 01:08:15 +00001917 if (Record.size() > 10 && Record[10] != 0)
1918 FunctionPrefixes.push_back(std::make_pair(Func, Record[10]-1));
Stephen Hines36b56882014-04-23 16:57:46 -07001919
1920 if (Record.size() > 11)
1921 Func->setDLLStorageClass(GetDecodedDLLStorageClass(Record[11]));
1922 else
1923 UpgradeDLLImportExportLinkage(Func, Record[3]);
1924
Chris Lattner0b2482a2007-04-23 21:26:05 +00001925 ValueList.push_back(Func);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001926
Chris Lattner48f84872007-05-01 04:59:48 +00001927 // If this is a function with a body, remember the prototype we are
1928 // creating now, so that we can match up the body with them later.
Derek Schuff2ea93872012-02-06 22:30:29 +00001929 if (!isProto) {
Chris Lattner48f84872007-05-01 04:59:48 +00001930 FunctionsWithBodies.push_back(Func);
Derek Schuff2ea93872012-02-06 22:30:29 +00001931 if (LazyStreamer) DeferredFunctionInfo[Func] = 0;
1932 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001933 break;
1934 }
Anton Korobeynikov91342d82008-03-12 00:49:19 +00001935 // ALIAS: [alias type, aliasee val#, linkage]
Stephen Hines36b56882014-04-23 16:57:46 -07001936 // ALIAS: [alias type, aliasee val#, linkage, visibility, dllstorageclass]
Chris Lattner198f34a2007-04-26 03:27:58 +00001937 case bitc::MODULE_CODE_ALIAS: {
Chris Lattner07d98b42007-04-26 02:46:40 +00001938 if (Record.size() < 3)
Rafael Espindolae076b532013-11-04 16:16:24 +00001939 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001940 Type *Ty = getTypeByID(Record[0]);
Rafael Espindolae076b532013-11-04 16:16:24 +00001941 if (!Ty)
1942 return Error(InvalidRecord);
Duncan Sands1df98592010-02-16 11:11:14 +00001943 if (!Ty->isPointerTy())
Rafael Espindolae076b532013-11-04 16:16:24 +00001944 return Error(InvalidTypeForValue);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001945
Chris Lattner07d98b42007-04-26 02:46:40 +00001946 GlobalAlias *NewGA = new GlobalAlias(Ty, GetDecodedLinkage(Record[2]),
1947 "", 0, TheModule);
Anton Korobeynikov91342d82008-03-12 00:49:19 +00001948 // Old bitcode files didn't have visibility field.
1949 if (Record.size() > 3)
1950 NewGA->setVisibility(GetDecodedVisibility(Record[3]));
Stephen Hines36b56882014-04-23 16:57:46 -07001951 if (Record.size() > 4)
1952 NewGA->setDLLStorageClass(GetDecodedDLLStorageClass(Record[4]));
1953 else
1954 UpgradeDLLImportExportLinkage(NewGA, Record[2]);
Chris Lattner07d98b42007-04-26 02:46:40 +00001955 ValueList.push_back(NewGA);
1956 AliasInits.push_back(std::make_pair(NewGA, Record[1]));
1957 break;
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001958 }
Chris Lattner198f34a2007-04-26 03:27:58 +00001959 /// MODULE_CODE_PURGEVALS: [numvals]
1960 case bitc::MODULE_CODE_PURGEVALS:
1961 // Trim down the value list to the specified size.
1962 if (Record.size() < 1 || Record[0] > ValueList.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001963 return Error(InvalidRecord);
Chris Lattner198f34a2007-04-26 03:27:58 +00001964 ValueList.shrinkTo(Record[0]);
1965 break;
1966 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001967 Record.clear();
1968 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001969}
1970
Rafael Espindolae076b532013-11-04 16:16:24 +00001971error_code BitcodeReader::ParseBitcodeInto(Module *M) {
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001972 TheModule = 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001973
Rafael Espindolae076b532013-11-04 16:16:24 +00001974 if (error_code EC = InitStream())
1975 return EC;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001976
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001977 // Sniff for the signature.
1978 if (Stream.Read(8) != 'B' ||
1979 Stream.Read(8) != 'C' ||
1980 Stream.Read(4) != 0x0 ||
1981 Stream.Read(4) != 0xC ||
1982 Stream.Read(4) != 0xE ||
1983 Stream.Read(4) != 0xD)
Rafael Espindolae076b532013-11-04 16:16:24 +00001984 return Error(InvalidBitcodeSignature);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001985
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001986 // We expect a number of well-defined blocks, though we don't necessarily
1987 // need to understand them all.
Chris Lattner5a4251c2013-01-20 02:13:19 +00001988 while (1) {
1989 if (Stream.AtEndOfStream())
Rafael Espindolae076b532013-11-04 16:16:24 +00001990 return error_code::success();
Joe Abbeyacb61942013-02-06 22:14:06 +00001991
Chris Lattner5a4251c2013-01-20 02:13:19 +00001992 BitstreamEntry Entry =
1993 Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
Joe Abbeyacb61942013-02-06 22:14:06 +00001994
Chris Lattner5a4251c2013-01-20 02:13:19 +00001995 switch (Entry.Kind) {
1996 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +00001997 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00001998 case BitstreamEntry::EndBlock:
Rafael Espindolae076b532013-11-04 16:16:24 +00001999 return error_code::success();
Joe Abbeyacb61942013-02-06 22:14:06 +00002000
Chris Lattner5a4251c2013-01-20 02:13:19 +00002001 case BitstreamEntry::SubBlock:
2002 switch (Entry.ID) {
2003 case bitc::BLOCKINFO_BLOCK_ID:
2004 if (Stream.ReadBlockInfoBlock())
Rafael Espindolae076b532013-11-04 16:16:24 +00002005 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00002006 break;
2007 case bitc::MODULE_BLOCK_ID:
2008 // Reject multiple MODULE_BLOCK's in a single bitstream.
2009 if (TheModule)
Rafael Espindolae076b532013-11-04 16:16:24 +00002010 return Error(InvalidMultipleBlocks);
Chris Lattner5a4251c2013-01-20 02:13:19 +00002011 TheModule = M;
Rafael Espindolae076b532013-11-04 16:16:24 +00002012 if (error_code EC = ParseModule(false))
2013 return EC;
2014 if (LazyStreamer)
2015 return error_code::success();
Chris Lattner5a4251c2013-01-20 02:13:19 +00002016 break;
2017 default:
2018 if (Stream.SkipBlock())
Rafael Espindolae076b532013-11-04 16:16:24 +00002019 return Error(InvalidRecord);
Chris Lattner5a4251c2013-01-20 02:13:19 +00002020 break;
2021 }
2022 continue;
2023 case BitstreamEntry::Record:
2024 // There should be no records in the top-level of blocks.
Joe Abbeyacb61942013-02-06 22:14:06 +00002025
Chris Lattner5a4251c2013-01-20 02:13:19 +00002026 // The ranlib in Xcode 4 will align archive members by appending newlines
Chad Rosier6ff9aa22011-08-09 22:23:40 +00002027 // to the end of them. If this file size is a multiple of 4 but not 8, we
2028 // have to read and ignore these final 4 bytes :-(
Chris Lattner5a4251c2013-01-20 02:13:19 +00002029 if (Stream.getAbbrevIDWidth() == 2 && Entry.ID == 2 &&
Rafael Espindolac9687b32011-05-26 18:59:54 +00002030 Stream.Read(6) == 2 && Stream.Read(24) == 0xa0a0a &&
Bill Wendling2127c9b2012-07-19 00:15:11 +00002031 Stream.AtEndOfStream())
Rafael Espindolae076b532013-11-04 16:16:24 +00002032 return error_code::success();
Joe Abbeyacb61942013-02-06 22:14:06 +00002033
Rafael Espindolae076b532013-11-04 16:16:24 +00002034 return Error(InvalidRecord);
Rafael Espindolac9687b32011-05-26 18:59:54 +00002035 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +00002036 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +00002037}
Chris Lattnerc453f762007-04-29 07:54:31 +00002038
Rafael Espindolae076b532013-11-04 16:16:24 +00002039error_code BitcodeReader::ParseModuleTriple(std::string &Triple) {
Bill Wendling34711742010-10-06 01:22:42 +00002040 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +00002041 return Error(InvalidRecord);
Bill Wendling34711742010-10-06 01:22:42 +00002042
2043 SmallVector<uint64_t, 64> Record;
2044
2045 // Read all the records for this module.
Chris Lattner5a4251c2013-01-20 02:13:19 +00002046 while (1) {
2047 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbeyacb61942013-02-06 22:14:06 +00002048
Chris Lattner5a4251c2013-01-20 02:13:19 +00002049 switch (Entry.Kind) {
2050 case BitstreamEntry::SubBlock: // Handled for us already.
2051 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +00002052 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00002053 case BitstreamEntry::EndBlock:
Rafael Espindolae076b532013-11-04 16:16:24 +00002054 return error_code::success();
Chris Lattner5a4251c2013-01-20 02:13:19 +00002055 case BitstreamEntry::Record:
2056 // The interesting case.
2057 break;
Bill Wendling34711742010-10-06 01:22:42 +00002058 }
2059
2060 // Read a record.
Chris Lattner5a4251c2013-01-20 02:13:19 +00002061 switch (Stream.readRecord(Entry.ID, Record)) {
Bill Wendling34711742010-10-06 01:22:42 +00002062 default: break; // Default behavior, ignore unknown content.
Bill Wendling34711742010-10-06 01:22:42 +00002063 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
2064 std::string S;
2065 if (ConvertToString(Record, 0, S))
Rafael Espindolae076b532013-11-04 16:16:24 +00002066 return Error(InvalidRecord);
Bill Wendling34711742010-10-06 01:22:42 +00002067 Triple = S;
2068 break;
2069 }
2070 }
2071 Record.clear();
2072 }
Bill Wendling34711742010-10-06 01:22:42 +00002073}
2074
Rafael Espindolae076b532013-11-04 16:16:24 +00002075error_code BitcodeReader::ParseTriple(std::string &Triple) {
2076 if (error_code EC = InitStream())
2077 return EC;
Bill Wendling34711742010-10-06 01:22:42 +00002078
2079 // Sniff for the signature.
2080 if (Stream.Read(8) != 'B' ||
2081 Stream.Read(8) != 'C' ||
2082 Stream.Read(4) != 0x0 ||
2083 Stream.Read(4) != 0xC ||
2084 Stream.Read(4) != 0xE ||
2085 Stream.Read(4) != 0xD)
Rafael Espindolae076b532013-11-04 16:16:24 +00002086 return Error(InvalidBitcodeSignature);
Bill Wendling34711742010-10-06 01:22:42 +00002087
2088 // We expect a number of well-defined blocks, though we don't necessarily
2089 // need to understand them all.
Chris Lattner5a4251c2013-01-20 02:13:19 +00002090 while (1) {
2091 BitstreamEntry Entry = Stream.advance();
Joe Abbeyacb61942013-02-06 22:14:06 +00002092
Chris Lattner5a4251c2013-01-20 02:13:19 +00002093 switch (Entry.Kind) {
2094 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +00002095 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00002096 case BitstreamEntry::EndBlock:
Rafael Espindolae076b532013-11-04 16:16:24 +00002097 return error_code::success();
Joe Abbeyacb61942013-02-06 22:14:06 +00002098
Chris Lattner5a4251c2013-01-20 02:13:19 +00002099 case BitstreamEntry::SubBlock:
2100 if (Entry.ID == bitc::MODULE_BLOCK_ID)
2101 return ParseModuleTriple(Triple);
Joe Abbeyacb61942013-02-06 22:14:06 +00002102
Chris Lattner5a4251c2013-01-20 02:13:19 +00002103 // Ignore other sub-blocks.
Rafael Espindolae076b532013-11-04 16:16:24 +00002104 if (Stream.SkipBlock())
2105 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00002106 continue;
Joe Abbeyacb61942013-02-06 22:14:06 +00002107
Chris Lattner5a4251c2013-01-20 02:13:19 +00002108 case BitstreamEntry::Record:
2109 Stream.skipRecord(Entry.ID);
2110 continue;
Bill Wendling34711742010-10-06 01:22:42 +00002111 }
2112 }
Bill Wendling34711742010-10-06 01:22:42 +00002113}
2114
Devang Patele8e02132009-09-18 19:26:43 +00002115/// ParseMetadataAttachment - Parse metadata attachments.
Rafael Espindolae076b532013-11-04 16:16:24 +00002116error_code BitcodeReader::ParseMetadataAttachment() {
Devang Patele8e02132009-09-18 19:26:43 +00002117 if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +00002118 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002119
Devang Patele8e02132009-09-18 19:26:43 +00002120 SmallVector<uint64_t, 64> Record;
Chris Lattner5a4251c2013-01-20 02:13:19 +00002121 while (1) {
2122 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbeyacb61942013-02-06 22:14:06 +00002123
Chris Lattner5a4251c2013-01-20 02:13:19 +00002124 switch (Entry.Kind) {
2125 case BitstreamEntry::SubBlock: // Handled for us already.
2126 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +00002127 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00002128 case BitstreamEntry::EndBlock:
Rafael Espindolae076b532013-11-04 16:16:24 +00002129 return error_code::success();
Chris Lattner5a4251c2013-01-20 02:13:19 +00002130 case BitstreamEntry::Record:
2131 // The interesting case.
Devang Patele8e02132009-09-18 19:26:43 +00002132 break;
2133 }
Chris Lattner5a4251c2013-01-20 02:13:19 +00002134
Devang Patele8e02132009-09-18 19:26:43 +00002135 // Read a metadata attachment record.
2136 Record.clear();
Chris Lattner5a4251c2013-01-20 02:13:19 +00002137 switch (Stream.readRecord(Entry.ID, Record)) {
Devang Patele8e02132009-09-18 19:26:43 +00002138 default: // Default behavior: ignore.
2139 break;
Chris Lattner9d61dd92011-06-17 17:50:30 +00002140 case bitc::METADATA_ATTACHMENT: {
Devang Patele8e02132009-09-18 19:26:43 +00002141 unsigned RecordLength = Record.size();
2142 if (Record.empty() || (RecordLength - 1) % 2 == 1)
Rafael Espindolae076b532013-11-04 16:16:24 +00002143 return Error(InvalidRecord);
Devang Patele8e02132009-09-18 19:26:43 +00002144 Instruction *Inst = InstructionList[Record[0]];
2145 for (unsigned i = 1; i != RecordLength; i = i+2) {
Devang Patela2148402009-09-28 21:14:55 +00002146 unsigned Kind = Record[i];
Dan Gohman19538d12010-07-20 21:42:28 +00002147 DenseMap<unsigned, unsigned>::iterator I =
2148 MDKindMap.find(Kind);
2149 if (I == MDKindMap.end())
Rafael Espindolae076b532013-11-04 16:16:24 +00002150 return Error(InvalidID);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002151 Value *Node = MDValueList.getValueFwdRef(Record[i+1]);
Dan Gohman19538d12010-07-20 21:42:28 +00002152 Inst->setMetadata(I->second, cast<MDNode>(Node));
Manman Ren804f0342013-09-28 00:22:27 +00002153 if (I->second == LLVMContext::MD_tbaa)
2154 InstsWithTBAATag.push_back(Inst);
Devang Patele8e02132009-09-18 19:26:43 +00002155 }
2156 break;
2157 }
2158 }
2159 }
Devang Patele8e02132009-09-18 19:26:43 +00002160}
Chris Lattner48f84872007-05-01 04:59:48 +00002161
Chris Lattner980e5aa2007-05-01 05:52:21 +00002162/// ParseFunctionBody - Lazily parse the specified function body block.
Rafael Espindolae076b532013-11-04 16:16:24 +00002163error_code BitcodeReader::ParseFunctionBody(Function *F) {
Chris Lattnere17b6582007-05-05 00:17:00 +00002164 if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +00002165 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002166
Nick Lewycky9a49f152010-02-25 08:30:17 +00002167 InstructionList.clear();
Chris Lattner980e5aa2007-05-01 05:52:21 +00002168 unsigned ModuleValueListSize = ValueList.size();
Dan Gohman69813832010-08-25 20:22:53 +00002169 unsigned ModuleMDValueListSize = MDValueList.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +00002170
Chris Lattner980e5aa2007-05-01 05:52:21 +00002171 // Add all the function arguments to the value table.
2172 for(Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
2173 ValueList.push_back(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002174
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002175 unsigned NextValueNo = ValueList.size();
Chris Lattner231cbcb2007-05-02 04:27:25 +00002176 BasicBlock *CurBB = 0;
2177 unsigned CurBBNo = 0;
2178
Chris Lattnera6245242010-04-03 02:17:50 +00002179 DebugLoc LastLoc;
Michael Ilseman407a6162012-11-15 22:34:00 +00002180
Chris Lattner980e5aa2007-05-01 05:52:21 +00002181 // Read all the records.
2182 SmallVector<uint64_t, 64> Record;
2183 while (1) {
Chris Lattner5a4251c2013-01-20 02:13:19 +00002184 BitstreamEntry Entry = Stream.advance();
Joe Abbeyacb61942013-02-06 22:14:06 +00002185
Chris Lattner5a4251c2013-01-20 02:13:19 +00002186 switch (Entry.Kind) {
2187 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +00002188 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00002189 case BitstreamEntry::EndBlock:
2190 goto OutOfRecordLoop;
Joe Abbeyacb61942013-02-06 22:14:06 +00002191
Chris Lattner5a4251c2013-01-20 02:13:19 +00002192 case BitstreamEntry::SubBlock:
2193 switch (Entry.ID) {
Chris Lattner980e5aa2007-05-01 05:52:21 +00002194 default: // Skip unknown content.
2195 if (Stream.SkipBlock())
Rafael Espindolae076b532013-11-04 16:16:24 +00002196 return Error(InvalidRecord);
Chris Lattner980e5aa2007-05-01 05:52:21 +00002197 break;
2198 case bitc::CONSTANTS_BLOCK_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00002199 if (error_code EC = ParseConstants())
2200 return EC;
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002201 NextValueNo = ValueList.size();
Chris Lattner980e5aa2007-05-01 05:52:21 +00002202 break;
2203 case bitc::VALUE_SYMTAB_BLOCK_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00002204 if (error_code EC = ParseValueSymbolTable())
2205 return EC;
Chris Lattner980e5aa2007-05-01 05:52:21 +00002206 break;
Devang Patele8e02132009-09-18 19:26:43 +00002207 case bitc::METADATA_ATTACHMENT_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00002208 if (error_code EC = ParseMetadataAttachment())
2209 return EC;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002210 break;
Victor Hernandezfab9e99c2010-01-13 19:34:08 +00002211 case bitc::METADATA_BLOCK_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00002212 if (error_code EC = ParseMetadata())
2213 return EC;
Victor Hernandezfab9e99c2010-01-13 19:34:08 +00002214 break;
Chris Lattner980e5aa2007-05-01 05:52:21 +00002215 }
2216 continue;
Joe Abbeyacb61942013-02-06 22:14:06 +00002217
Chris Lattner5a4251c2013-01-20 02:13:19 +00002218 case BitstreamEntry::Record:
2219 // The interesting case.
2220 break;
Chris Lattner980e5aa2007-05-01 05:52:21 +00002221 }
Joe Abbeyacb61942013-02-06 22:14:06 +00002222
Chris Lattner980e5aa2007-05-01 05:52:21 +00002223 // Read a record.
2224 Record.clear();
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002225 Instruction *I = 0;
Chris Lattner5a4251c2013-01-20 02:13:19 +00002226 unsigned BitCode = Stream.readRecord(Entry.ID, Record);
Dan Gohman1224c382009-07-20 21:19:07 +00002227 switch (BitCode) {
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002228 default: // Default behavior: reject
Rafael Espindolae076b532013-11-04 16:16:24 +00002229 return Error(InvalidValue);
Chris Lattner980e5aa2007-05-01 05:52:21 +00002230 case bitc::FUNC_CODE_DECLAREBLOCKS: // DECLAREBLOCKS: [nblocks]
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002231 if (Record.size() < 1 || Record[0] == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002232 return Error(InvalidRecord);
Chris Lattner980e5aa2007-05-01 05:52:21 +00002233 // Create all the basic blocks for the function.
Chris Lattnerf61e6452007-05-03 22:09:51 +00002234 FunctionBBs.resize(Record[0]);
Chris Lattner980e5aa2007-05-01 05:52:21 +00002235 for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i)
Owen Anderson1d0be152009-08-13 21:58:54 +00002236 FunctionBBs[i] = BasicBlock::Create(Context, "", F);
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002237 CurBB = FunctionBBs[0];
2238 continue;
Michael Ilseman407a6162012-11-15 22:34:00 +00002239
Chris Lattnera6245242010-04-03 02:17:50 +00002240 case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN
2241 // This record indicates that the last instruction is at the same
2242 // location as the previous instruction with a location.
2243 I = 0;
Michael Ilseman407a6162012-11-15 22:34:00 +00002244
Chris Lattnera6245242010-04-03 02:17:50 +00002245 // Get the last instruction emitted.
2246 if (CurBB && !CurBB->empty())
2247 I = &CurBB->back();
2248 else if (CurBBNo && FunctionBBs[CurBBNo-1] &&
2249 !FunctionBBs[CurBBNo-1]->empty())
2250 I = &FunctionBBs[CurBBNo-1]->back();
Michael Ilseman407a6162012-11-15 22:34:00 +00002251
Rafael Espindolae076b532013-11-04 16:16:24 +00002252 if (I == 0)
2253 return Error(InvalidRecord);
Chris Lattnera6245242010-04-03 02:17:50 +00002254 I->setDebugLoc(LastLoc);
2255 I = 0;
2256 continue;
Michael Ilseman407a6162012-11-15 22:34:00 +00002257
Chris Lattner4f6bab92011-06-17 18:17:37 +00002258 case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia]
Chris Lattnera6245242010-04-03 02:17:50 +00002259 I = 0; // Get the last instruction emitted.
2260 if (CurBB && !CurBB->empty())
2261 I = &CurBB->back();
2262 else if (CurBBNo && FunctionBBs[CurBBNo-1] &&
2263 !FunctionBBs[CurBBNo-1]->empty())
2264 I = &FunctionBBs[CurBBNo-1]->back();
2265 if (I == 0 || Record.size() < 4)
Rafael Espindolae076b532013-11-04 16:16:24 +00002266 return Error(InvalidRecord);
Michael Ilseman407a6162012-11-15 22:34:00 +00002267
Chris Lattnera6245242010-04-03 02:17:50 +00002268 unsigned Line = Record[0], Col = Record[1];
2269 unsigned ScopeID = Record[2], IAID = Record[3];
Michael Ilseman407a6162012-11-15 22:34:00 +00002270
Chris Lattnera6245242010-04-03 02:17:50 +00002271 MDNode *Scope = 0, *IA = 0;
2272 if (ScopeID) Scope = cast<MDNode>(MDValueList.getValueFwdRef(ScopeID-1));
2273 if (IAID) IA = cast<MDNode>(MDValueList.getValueFwdRef(IAID-1));
2274 LastLoc = DebugLoc::get(Line, Col, Scope, IA);
2275 I->setDebugLoc(LastLoc);
2276 I = 0;
2277 continue;
2278 }
2279
Chris Lattnerabfbf852007-05-06 00:21:25 +00002280 case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode]
2281 unsigned OpNum = 0;
2282 Value *LHS, *RHS;
2283 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002284 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
Dan Gohman1224c382009-07-20 21:19:07 +00002285 OpNum+1 > Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002286 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002287
Dan Gohman1224c382009-07-20 21:19:07 +00002288 int Opc = GetDecodedBinaryOpcode(Record[OpNum++], LHS->getType());
Rafael Espindolae076b532013-11-04 16:16:24 +00002289 if (Opc == -1)
2290 return Error(InvalidRecord);
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002291 I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
Devang Patele8e02132009-09-18 19:26:43 +00002292 InstructionList.push_back(I);
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002293 if (OpNum < Record.size()) {
2294 if (Opc == Instruction::Add ||
2295 Opc == Instruction::Sub ||
Chris Lattnerf067d582011-02-07 16:40:21 +00002296 Opc == Instruction::Mul ||
2297 Opc == Instruction::Shl) {
Dan Gohman26793ed2010-01-25 21:55:39 +00002298 if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP))
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002299 cast<BinaryOperator>(I)->setHasNoSignedWrap(true);
Dan Gohman26793ed2010-01-25 21:55:39 +00002300 if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002301 cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true);
Chris Lattner35bda892011-02-06 21:44:57 +00002302 } else if (Opc == Instruction::SDiv ||
Chris Lattnerf067d582011-02-07 16:40:21 +00002303 Opc == Instruction::UDiv ||
2304 Opc == Instruction::LShr ||
2305 Opc == Instruction::AShr) {
Chris Lattner35bda892011-02-06 21:44:57 +00002306 if (Record[OpNum] & (1 << bitc::PEO_EXACT))
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002307 cast<BinaryOperator>(I)->setIsExact(true);
Michael Ilseman495d10a2012-11-27 00:43:38 +00002308 } else if (isa<FPMathOperator>(I)) {
2309 FastMathFlags FMF;
Michael Ilseman1638b832012-12-09 21:12:04 +00002310 if (0 != (Record[OpNum] & FastMathFlags::UnsafeAlgebra))
2311 FMF.setUnsafeAlgebra();
2312 if (0 != (Record[OpNum] & FastMathFlags::NoNaNs))
2313 FMF.setNoNaNs();
2314 if (0 != (Record[OpNum] & FastMathFlags::NoInfs))
2315 FMF.setNoInfs();
2316 if (0 != (Record[OpNum] & FastMathFlags::NoSignedZeros))
2317 FMF.setNoSignedZeros();
2318 if (0 != (Record[OpNum] & FastMathFlags::AllowReciprocal))
2319 FMF.setAllowReciprocal();
Michael Ilseman495d10a2012-11-27 00:43:38 +00002320 if (FMF.any())
2321 I->setFastMathFlags(FMF);
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002322 }
Michael Ilseman495d10a2012-11-27 00:43:38 +00002323
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002324 }
Chris Lattner980e5aa2007-05-01 05:52:21 +00002325 break;
2326 }
Chris Lattnerabfbf852007-05-06 00:21:25 +00002327 case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc]
2328 unsigned OpNum = 0;
2329 Value *Op;
2330 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
2331 OpNum+2 != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002332 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002333
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002334 Type *ResTy = getTypeByID(Record[OpNum]);
Chris Lattnerabfbf852007-05-06 00:21:25 +00002335 int Opc = GetDecodedCastOpcode(Record[OpNum+1]);
2336 if (Opc == -1 || ResTy == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002337 return Error(InvalidRecord);
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002338 Instruction *Temp = 0;
2339 if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) {
2340 if (Temp) {
2341 InstructionList.push_back(Temp);
2342 CurBB->getInstList().push_back(Temp);
2343 }
2344 } else {
2345 I = CastInst::Create((Instruction::CastOps)Opc, Op, ResTy);
2346 }
Devang Patele8e02132009-09-18 19:26:43 +00002347 InstructionList.push_back(I);
Chris Lattner231cbcb2007-05-02 04:27:25 +00002348 break;
2349 }
Dan Gohmandd8004d2009-07-27 21:53:46 +00002350 case bitc::FUNC_CODE_INST_INBOUNDS_GEP:
Chris Lattner15e6d172007-05-04 19:11:41 +00002351 case bitc::FUNC_CODE_INST_GEP: { // GEP: [n x operands]
Chris Lattner7337ab92007-05-06 00:00:00 +00002352 unsigned OpNum = 0;
2353 Value *BasePtr;
2354 if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr))
Rafael Espindolae076b532013-11-04 16:16:24 +00002355 return Error(InvalidRecord);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002356
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002357 SmallVector<Value*, 16> GEPIdx;
Chris Lattner7337ab92007-05-06 00:00:00 +00002358 while (OpNum != Record.size()) {
2359 Value *Op;
2360 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolae076b532013-11-04 16:16:24 +00002361 return Error(InvalidRecord);
Chris Lattner7337ab92007-05-06 00:00:00 +00002362 GEPIdx.push_back(Op);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002363 }
2364
Jay Foada9203102011-07-25 09:48:08 +00002365 I = GetElementPtrInst::Create(BasePtr, GEPIdx);
Devang Patele8e02132009-09-18 19:26:43 +00002366 InstructionList.push_back(I);
Dan Gohmandd8004d2009-07-27 21:53:46 +00002367 if (BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP)
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002368 cast<GetElementPtrInst>(I)->setIsInBounds(true);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002369 break;
2370 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002371
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002372 case bitc::FUNC_CODE_INST_EXTRACTVAL: {
2373 // EXTRACTVAL: [opty, opval, n x indices]
Dan Gohmane4977cf2008-05-23 01:55:30 +00002374 unsigned OpNum = 0;
2375 Value *Agg;
2376 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
Rafael Espindolae076b532013-11-04 16:16:24 +00002377 return Error(InvalidRecord);
Dan Gohmane4977cf2008-05-23 01:55:30 +00002378
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002379 SmallVector<unsigned, 4> EXTRACTVALIdx;
2380 for (unsigned RecSize = Record.size();
2381 OpNum != RecSize; ++OpNum) {
2382 uint64_t Index = Record[OpNum];
2383 if ((unsigned)Index != Index)
Rafael Espindolae076b532013-11-04 16:16:24 +00002384 return Error(InvalidValue);
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002385 EXTRACTVALIdx.push_back((unsigned)Index);
Dan Gohmane4977cf2008-05-23 01:55:30 +00002386 }
2387
Jay Foadfc6d3a42011-07-13 10:26:04 +00002388 I = ExtractValueInst::Create(Agg, EXTRACTVALIdx);
Devang Patele8e02132009-09-18 19:26:43 +00002389 InstructionList.push_back(I);
Dan Gohmane4977cf2008-05-23 01:55:30 +00002390 break;
2391 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002392
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002393 case bitc::FUNC_CODE_INST_INSERTVAL: {
2394 // INSERTVAL: [opty, opval, opty, opval, n x indices]
Dan Gohmane4977cf2008-05-23 01:55:30 +00002395 unsigned OpNum = 0;
2396 Value *Agg;
2397 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
Rafael Espindolae076b532013-11-04 16:16:24 +00002398 return Error(InvalidRecord);
Dan Gohmane4977cf2008-05-23 01:55:30 +00002399 Value *Val;
2400 if (getValueTypePair(Record, OpNum, NextValueNo, Val))
Rafael Espindolae076b532013-11-04 16:16:24 +00002401 return Error(InvalidRecord);
Dan Gohmane4977cf2008-05-23 01:55:30 +00002402
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002403 SmallVector<unsigned, 4> INSERTVALIdx;
2404 for (unsigned RecSize = Record.size();
2405 OpNum != RecSize; ++OpNum) {
2406 uint64_t Index = Record[OpNum];
2407 if ((unsigned)Index != Index)
Rafael Espindolae076b532013-11-04 16:16:24 +00002408 return Error(InvalidValue);
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002409 INSERTVALIdx.push_back((unsigned)Index);
Dan Gohmane4977cf2008-05-23 01:55:30 +00002410 }
2411
Jay Foadfc6d3a42011-07-13 10:26:04 +00002412 I = InsertValueInst::Create(Agg, Val, INSERTVALIdx);
Devang Patele8e02132009-09-18 19:26:43 +00002413 InstructionList.push_back(I);
Dan Gohmane4977cf2008-05-23 01:55:30 +00002414 break;
2415 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002416
Chris Lattnerabfbf852007-05-06 00:21:25 +00002417 case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval]
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +00002418 // obsolete form of select
2419 // handles select i1 ... in old bitcode
Chris Lattnerabfbf852007-05-06 00:21:25 +00002420 unsigned OpNum = 0;
2421 Value *TrueVal, *FalseVal, *Cond;
2422 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002423 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
2424 popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond))
Rafael Espindolae076b532013-11-04 16:16:24 +00002425 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002426
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +00002427 I = SelectInst::Create(Cond, TrueVal, FalseVal);
Devang Patele8e02132009-09-18 19:26:43 +00002428 InstructionList.push_back(I);
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +00002429 break;
2430 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002431
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +00002432 case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred]
2433 // new form of select
2434 // handles select i1 or select [N x i1]
2435 unsigned OpNum = 0;
2436 Value *TrueVal, *FalseVal, *Cond;
2437 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002438 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +00002439 getValueTypePair(Record, OpNum, NextValueNo, Cond))
Rafael Espindolae076b532013-11-04 16:16:24 +00002440 return Error(InvalidRecord);
Dan Gohmanf72fb672008-09-09 01:02:47 +00002441
2442 // select condition can be either i1 or [N x i1]
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002443 if (VectorType* vector_type =
2444 dyn_cast<VectorType>(Cond->getType())) {
Dan Gohmanf72fb672008-09-09 01:02:47 +00002445 // expect <n x i1>
Daniel Dunbara279bc32009-09-20 02:20:51 +00002446 if (vector_type->getElementType() != Type::getInt1Ty(Context))
Rafael Espindolae076b532013-11-04 16:16:24 +00002447 return Error(InvalidTypeForValue);
Dan Gohmanf72fb672008-09-09 01:02:47 +00002448 } else {
2449 // expect i1
Daniel Dunbara279bc32009-09-20 02:20:51 +00002450 if (Cond->getType() != Type::getInt1Ty(Context))
Rafael Espindolae076b532013-11-04 16:16:24 +00002451 return Error(InvalidTypeForValue);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002452 }
2453
Gabor Greif051a9502008-04-06 20:25:17 +00002454 I = SelectInst::Create(Cond, TrueVal, FalseVal);
Devang Patele8e02132009-09-18 19:26:43 +00002455 InstructionList.push_back(I);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002456 break;
2457 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002458
Chris Lattner01ff65f2007-05-02 05:16:49 +00002459 case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval]
Chris Lattnerabfbf852007-05-06 00:21:25 +00002460 unsigned OpNum = 0;
2461 Value *Vec, *Idx;
2462 if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002463 popValue(Record, OpNum, NextValueNo, Type::getInt32Ty(Context), Idx))
Rafael Espindolae076b532013-11-04 16:16:24 +00002464 return Error(InvalidRecord);
Eric Christophera3500da2009-07-25 02:28:41 +00002465 I = ExtractElementInst::Create(Vec, Idx);
Devang Patele8e02132009-09-18 19:26:43 +00002466 InstructionList.push_back(I);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002467 break;
2468 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002469
Chris Lattner01ff65f2007-05-02 05:16:49 +00002470 case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval]
Chris Lattnerabfbf852007-05-06 00:21:25 +00002471 unsigned OpNum = 0;
2472 Value *Vec, *Elt, *Idx;
2473 if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002474 popValue(Record, OpNum, NextValueNo,
Chris Lattnerabfbf852007-05-06 00:21:25 +00002475 cast<VectorType>(Vec->getType())->getElementType(), Elt) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002476 popValue(Record, OpNum, NextValueNo, Type::getInt32Ty(Context), Idx))
Rafael Espindolae076b532013-11-04 16:16:24 +00002477 return Error(InvalidRecord);
Gabor Greif051a9502008-04-06 20:25:17 +00002478 I = InsertElementInst::Create(Vec, Elt, Idx);
Devang Patele8e02132009-09-18 19:26:43 +00002479 InstructionList.push_back(I);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002480 break;
2481 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002482
Chris Lattnerabfbf852007-05-06 00:21:25 +00002483 case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval]
2484 unsigned OpNum = 0;
2485 Value *Vec1, *Vec2, *Mask;
2486 if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002487 popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2))
Rafael Espindolae076b532013-11-04 16:16:24 +00002488 return Error(InvalidRecord);
Chris Lattnerabfbf852007-05-06 00:21:25 +00002489
Mon P Wangaeb06d22008-11-10 04:46:22 +00002490 if (getValueTypePair(Record, OpNum, NextValueNo, Mask))
Rafael Espindolae076b532013-11-04 16:16:24 +00002491 return Error(InvalidRecord);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002492 I = new ShuffleVectorInst(Vec1, Vec2, Mask);
Devang Patele8e02132009-09-18 19:26:43 +00002493 InstructionList.push_back(I);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002494 break;
2495 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002496
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002497 case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred]
2498 // Old form of ICmp/FCmp returning bool
2499 // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were
2500 // both legal on vectors but had different behaviour.
2501 case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred]
2502 // FCmp/ICmp returning bool or vector of bool
2503
Chris Lattner7337ab92007-05-06 00:00:00 +00002504 unsigned OpNum = 0;
2505 Value *LHS, *RHS;
2506 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002507 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
Chris Lattner7337ab92007-05-06 00:00:00 +00002508 OpNum+1 != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002509 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002510
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002511 if (LHS->getType()->isFPOrFPVectorTy())
Dan Gohman1c8a23c2009-08-25 23:17:54 +00002512 I = new FCmpInst((FCmpInst::Predicate)Record[OpNum], LHS, RHS);
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002513 else
Dan Gohman1c8a23c2009-08-25 23:17:54 +00002514 I = new ICmpInst((ICmpInst::Predicate)Record[OpNum], LHS, RHS);
Devang Patele8e02132009-09-18 19:26:43 +00002515 InstructionList.push_back(I);
Dan Gohmanf72fb672008-09-09 01:02:47 +00002516 break;
2517 }
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002518
Chris Lattner231cbcb2007-05-02 04:27:25 +00002519 case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>]
Devang Pateld9d99ff2008-02-26 01:29:32 +00002520 {
2521 unsigned Size = Record.size();
2522 if (Size == 0) {
Owen Anderson1d0be152009-08-13 21:58:54 +00002523 I = ReturnInst::Create(Context);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002524 InstructionList.push_back(I);
Devang Pateld9d99ff2008-02-26 01:29:32 +00002525 break;
Dan Gohmanfc74abf2008-07-23 00:34:11 +00002526 }
Devang Pateld9d99ff2008-02-26 01:29:32 +00002527
Dan Gohmanfc74abf2008-07-23 00:34:11 +00002528 unsigned OpNum = 0;
Chris Lattner96a74c52011-06-17 18:09:11 +00002529 Value *Op = NULL;
2530 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolae076b532013-11-04 16:16:24 +00002531 return Error(InvalidRecord);
Chris Lattner96a74c52011-06-17 18:09:11 +00002532 if (OpNum != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002533 return Error(InvalidRecord);
Dan Gohmanfc74abf2008-07-23 00:34:11 +00002534
Chris Lattner96a74c52011-06-17 18:09:11 +00002535 I = ReturnInst::Create(Context, Op);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002536 InstructionList.push_back(I);
Dan Gohmanfc74abf2008-07-23 00:34:11 +00002537 break;
Chris Lattner231cbcb2007-05-02 04:27:25 +00002538 }
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002539 case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#]
Chris Lattnerf61e6452007-05-03 22:09:51 +00002540 if (Record.size() != 1 && Record.size() != 3)
Rafael Espindolae076b532013-11-04 16:16:24 +00002541 return Error(InvalidRecord);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002542 BasicBlock *TrueDest = getBasicBlock(Record[0]);
2543 if (TrueDest == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002544 return Error(InvalidRecord);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002545
Devang Patele8e02132009-09-18 19:26:43 +00002546 if (Record.size() == 1) {
Gabor Greif051a9502008-04-06 20:25:17 +00002547 I = BranchInst::Create(TrueDest);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002548 InstructionList.push_back(I);
Devang Patele8e02132009-09-18 19:26:43 +00002549 }
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002550 else {
2551 BasicBlock *FalseDest = getBasicBlock(Record[1]);
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002552 Value *Cond = getValue(Record, 2, NextValueNo,
2553 Type::getInt1Ty(Context));
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002554 if (FalseDest == 0 || Cond == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002555 return Error(InvalidRecord);
Gabor Greif051a9502008-04-06 20:25:17 +00002556 I = BranchInst::Create(TrueDest, FalseDest, Cond);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002557 InstructionList.push_back(I);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002558 }
2559 break;
2560 }
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002561 case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...]
Michael Ilseman407a6162012-11-15 22:34:00 +00002562 // Check magic
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002563 if ((Record[0] >> 16) == SWITCH_INST_MAGIC) {
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002564 // "New" SwitchInst format with case ranges. The changes to write this
2565 // format were reverted but we still recognize bitcode that uses it.
2566 // Hopefully someday we will have support for case ranges and can use
2567 // this format again.
Michael Ilseman407a6162012-11-15 22:34:00 +00002568
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002569 Type *OpTy = getTypeByID(Record[1]);
2570 unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth();
2571
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002572 Value *Cond = getValue(Record, 2, NextValueNo, OpTy);
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002573 BasicBlock *Default = getBasicBlock(Record[3]);
2574 if (OpTy == 0 || Cond == 0 || Default == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002575 return Error(InvalidRecord);
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002576
2577 unsigned NumCases = Record[4];
Michael Ilseman407a6162012-11-15 22:34:00 +00002578
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002579 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
2580 InstructionList.push_back(SI);
Michael Ilseman407a6162012-11-15 22:34:00 +00002581
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002582 unsigned CurIdx = 5;
2583 for (unsigned i = 0; i != NumCases; ++i) {
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002584 SmallVector<ConstantInt*, 1> CaseVals;
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002585 unsigned NumItems = Record[CurIdx++];
2586 for (unsigned ci = 0; ci != NumItems; ++ci) {
2587 bool isSingleNumber = Record[CurIdx++];
Michael Ilseman407a6162012-11-15 22:34:00 +00002588
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002589 APInt Low;
2590 unsigned ActiveWords = 1;
2591 if (ValueBitWidth > 64)
2592 ActiveWords = Record[CurIdx++];
Benjamin Kramerf52aea82012-05-28 14:10:31 +00002593 Low = ReadWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
2594 ValueBitWidth);
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002595 CurIdx += ActiveWords;
Stepan Dyatkovskiy484fc932012-05-28 12:39:09 +00002596
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002597 if (!isSingleNumber) {
2598 ActiveWords = 1;
2599 if (ValueBitWidth > 64)
2600 ActiveWords = Record[CurIdx++];
2601 APInt High =
Benjamin Kramerf52aea82012-05-28 14:10:31 +00002602 ReadWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
2603 ValueBitWidth);
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002604 CurIdx += ActiveWords;
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002605
2606 // FIXME: It is not clear whether values in the range should be
2607 // compared as signed or unsigned values. The partially
2608 // implemented changes that used this format in the past used
2609 // unsigned comparisons.
2610 for ( ; Low.ule(High); ++Low)
2611 CaseVals.push_back(ConstantInt::get(Context, Low));
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002612 } else
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002613 CaseVals.push_back(ConstantInt::get(Context, Low));
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002614 }
2615 BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]);
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002616 for (SmallVector<ConstantInt*, 1>::iterator cvi = CaseVals.begin(),
2617 cve = CaseVals.end(); cvi != cve; ++cvi)
2618 SI->addCase(*cvi, DestBB);
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002619 }
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002620 I = SI;
2621 break;
2622 }
Michael Ilseman407a6162012-11-15 22:34:00 +00002623
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002624 // Old SwitchInst format without case ranges.
Michael Ilseman407a6162012-11-15 22:34:00 +00002625
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002626 if (Record.size() < 3 || (Record.size() & 1) == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002627 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002628 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002629 Value *Cond = getValue(Record, 1, NextValueNo, OpTy);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002630 BasicBlock *Default = getBasicBlock(Record[2]);
2631 if (OpTy == 0 || Cond == 0 || Default == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002632 return Error(InvalidRecord);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002633 unsigned NumCases = (Record.size()-3)/2;
Gabor Greif051a9502008-04-06 20:25:17 +00002634 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
Devang Patele8e02132009-09-18 19:26:43 +00002635 InstructionList.push_back(SI);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002636 for (unsigned i = 0, e = NumCases; i != e; ++i) {
Daniel Dunbara279bc32009-09-20 02:20:51 +00002637 ConstantInt *CaseVal =
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002638 dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy));
2639 BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]);
2640 if (CaseVal == 0 || DestBB == 0) {
2641 delete SI;
Rafael Espindolae076b532013-11-04 16:16:24 +00002642 return Error(InvalidRecord);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002643 }
2644 SI->addCase(CaseVal, DestBB);
2645 }
2646 I = SI;
2647 break;
2648 }
Chris Lattnerab21db72009-10-28 00:19:10 +00002649 case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...]
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002650 if (Record.size() < 2)
Rafael Espindolae076b532013-11-04 16:16:24 +00002651 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002652 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002653 Value *Address = getValue(Record, 1, NextValueNo, OpTy);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002654 if (OpTy == 0 || Address == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002655 return Error(InvalidRecord);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002656 unsigned NumDests = Record.size()-2;
Chris Lattnerab21db72009-10-28 00:19:10 +00002657 IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002658 InstructionList.push_back(IBI);
2659 for (unsigned i = 0, e = NumDests; i != e; ++i) {
2660 if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) {
2661 IBI->addDestination(DestBB);
2662 } else {
2663 delete IBI;
Rafael Espindolae076b532013-11-04 16:16:24 +00002664 return Error(InvalidRecord);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002665 }
2666 }
2667 I = IBI;
2668 break;
2669 }
Michael Ilseman407a6162012-11-15 22:34:00 +00002670
Duncan Sandsdc024672007-11-27 13:23:08 +00002671 case bitc::FUNC_CODE_INST_INVOKE: {
2672 // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
Rafael Espindolae076b532013-11-04 16:16:24 +00002673 if (Record.size() < 4)
2674 return Error(InvalidRecord);
Bill Wendling99faa3b2012-12-07 23:16:57 +00002675 AttributeSet PAL = getAttributes(Record[0]);
Chris Lattnera9bb7132007-05-08 05:38:01 +00002676 unsigned CCInfo = Record[1];
2677 BasicBlock *NormalBB = getBasicBlock(Record[2]);
2678 BasicBlock *UnwindBB = getBasicBlock(Record[3]);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002679
Chris Lattnera9bb7132007-05-08 05:38:01 +00002680 unsigned OpNum = 4;
Chris Lattner7337ab92007-05-06 00:00:00 +00002681 Value *Callee;
2682 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
Rafael Espindolae076b532013-11-04 16:16:24 +00002683 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002684
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002685 PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType());
2686 FunctionType *FTy = !CalleeTy ? 0 :
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002687 dyn_cast<FunctionType>(CalleeTy->getElementType());
2688
2689 // Check that the right number of fixed parameters are here.
Chris Lattner7337ab92007-05-06 00:00:00 +00002690 if (FTy == 0 || NormalBB == 0 || UnwindBB == 0 ||
2691 Record.size() < OpNum+FTy->getNumParams())
Rafael Espindolae076b532013-11-04 16:16:24 +00002692 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002693
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002694 SmallVector<Value*, 16> Ops;
Chris Lattner7337ab92007-05-06 00:00:00 +00002695 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002696 Ops.push_back(getValue(Record, OpNum, NextValueNo,
2697 FTy->getParamType(i)));
Rafael Espindolae076b532013-11-04 16:16:24 +00002698 if (Ops.back() == 0)
2699 return Error(InvalidRecord);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002700 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002701
Chris Lattner7337ab92007-05-06 00:00:00 +00002702 if (!FTy->isVarArg()) {
2703 if (Record.size() != OpNum)
Rafael Espindolae076b532013-11-04 16:16:24 +00002704 return Error(InvalidRecord);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002705 } else {
Chris Lattner7337ab92007-05-06 00:00:00 +00002706 // Read type/value pairs for varargs params.
2707 while (OpNum != Record.size()) {
2708 Value *Op;
2709 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolae076b532013-11-04 16:16:24 +00002710 return Error(InvalidRecord);
Chris Lattner7337ab92007-05-06 00:00:00 +00002711 Ops.push_back(Op);
2712 }
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002713 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002714
Jay Foada3efbb12011-07-15 08:37:34 +00002715 I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops);
Devang Patele8e02132009-09-18 19:26:43 +00002716 InstructionList.push_back(I);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002717 cast<InvokeInst>(I)->setCallingConv(
2718 static_cast<CallingConv::ID>(CCInfo));
Devang Patel05988662008-09-25 21:00:45 +00002719 cast<InvokeInst>(I)->setAttributes(PAL);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002720 break;
2721 }
Bill Wendlingdccc03b2011-07-31 06:30:59 +00002722 case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval]
2723 unsigned Idx = 0;
2724 Value *Val = 0;
2725 if (getValueTypePair(Record, Idx, NextValueNo, Val))
Rafael Espindolae076b532013-11-04 16:16:24 +00002726 return Error(InvalidRecord);
Bill Wendlingdccc03b2011-07-31 06:30:59 +00002727 I = ResumeInst::Create(Val);
Bill Wendling35726bf2011-09-01 00:50:20 +00002728 InstructionList.push_back(I);
Bill Wendlingdccc03b2011-07-31 06:30:59 +00002729 break;
2730 }
Chris Lattner231cbcb2007-05-02 04:27:25 +00002731 case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE
Owen Anderson1d0be152009-08-13 21:58:54 +00002732 I = new UnreachableInst(Context);
Devang Patele8e02132009-09-18 19:26:43 +00002733 InstructionList.push_back(I);
Chris Lattner231cbcb2007-05-02 04:27:25 +00002734 break;
Chris Lattnerabfbf852007-05-06 00:21:25 +00002735 case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...]
Chris Lattner15e6d172007-05-04 19:11:41 +00002736 if (Record.size() < 1 || ((Record.size()-1)&1))
Rafael Espindolae076b532013-11-04 16:16:24 +00002737 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002738 Type *Ty = getTypeByID(Record[0]);
Rafael Espindolae076b532013-11-04 16:16:24 +00002739 if (!Ty)
2740 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002741
Jay Foad3ecfc862011-03-30 11:28:46 +00002742 PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2);
Devang Patele8e02132009-09-18 19:26:43 +00002743 InstructionList.push_back(PN);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002744
Chris Lattner15e6d172007-05-04 19:11:41 +00002745 for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) {
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002746 Value *V;
2747 // With the new function encoding, it is possible that operands have
2748 // negative IDs (for forward references). Use a signed VBR
2749 // representation to keep the encoding small.
2750 if (UseRelativeIDs)
2751 V = getValueSigned(Record, 1+i, NextValueNo, Ty);
2752 else
2753 V = getValue(Record, 1+i, NextValueNo, Ty);
Chris Lattner15e6d172007-05-04 19:11:41 +00002754 BasicBlock *BB = getBasicBlock(Record[2+i]);
Rafael Espindolae076b532013-11-04 16:16:24 +00002755 if (!V || !BB)
2756 return Error(InvalidRecord);
Chris Lattner2a98cca2007-05-03 18:58:09 +00002757 PN->addIncoming(V, BB);
2758 }
2759 I = PN;
2760 break;
2761 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002762
Bill Wendlinge6e88262011-08-12 20:24:12 +00002763 case bitc::FUNC_CODE_INST_LANDINGPAD: {
2764 // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?]
2765 unsigned Idx = 0;
2766 if (Record.size() < 4)
Rafael Espindolae076b532013-11-04 16:16:24 +00002767 return Error(InvalidRecord);
Bill Wendlinge6e88262011-08-12 20:24:12 +00002768 Type *Ty = getTypeByID(Record[Idx++]);
Rafael Espindolae076b532013-11-04 16:16:24 +00002769 if (!Ty)
2770 return Error(InvalidRecord);
Bill Wendlinge6e88262011-08-12 20:24:12 +00002771 Value *PersFn = 0;
2772 if (getValueTypePair(Record, Idx, NextValueNo, PersFn))
Rafael Espindolae076b532013-11-04 16:16:24 +00002773 return Error(InvalidRecord);
Bill Wendlinge6e88262011-08-12 20:24:12 +00002774
2775 bool IsCleanup = !!Record[Idx++];
2776 unsigned NumClauses = Record[Idx++];
2777 LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, NumClauses);
2778 LP->setCleanup(IsCleanup);
2779 for (unsigned J = 0; J != NumClauses; ++J) {
2780 LandingPadInst::ClauseType CT =
2781 LandingPadInst::ClauseType(Record[Idx++]); (void)CT;
2782 Value *Val;
2783
2784 if (getValueTypePair(Record, Idx, NextValueNo, Val)) {
2785 delete LP;
Rafael Espindolae076b532013-11-04 16:16:24 +00002786 return Error(InvalidRecord);
Bill Wendlinge6e88262011-08-12 20:24:12 +00002787 }
2788
2789 assert((CT != LandingPadInst::Catch ||
2790 !isa<ArrayType>(Val->getType())) &&
2791 "Catch clause has a invalid type!");
2792 assert((CT != LandingPadInst::Filter ||
2793 isa<ArrayType>(Val->getType())) &&
2794 "Filter clause has invalid type!");
2795 LP->addClause(Val);
2796 }
2797
2798 I = LP;
Bill Wendling35726bf2011-09-01 00:50:20 +00002799 InstructionList.push_back(I);
Bill Wendlinge6e88262011-08-12 20:24:12 +00002800 break;
2801 }
2802
Chris Lattner96a74c52011-06-17 18:09:11 +00002803 case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]
2804 if (Record.size() != 4)
Rafael Espindolae076b532013-11-04 16:16:24 +00002805 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002806 PointerType *Ty =
Chris Lattner2a98cca2007-05-03 18:58:09 +00002807 dyn_cast_or_null<PointerType>(getTypeByID(Record[0]));
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002808 Type *OpTy = getTypeByID(Record[1]);
Chris Lattner96a74c52011-06-17 18:09:11 +00002809 Value *Size = getFnValueByID(Record[2], OpTy);
2810 unsigned Align = Record[3];
Rafael Espindolae076b532013-11-04 16:16:24 +00002811 if (!Ty || !Size)
2812 return Error(InvalidRecord);
Owen Anderson50dead02009-07-15 23:53:25 +00002813 I = new AllocaInst(Ty->getElementType(), Size, (1 << Align) >> 1);
Devang Patele8e02132009-09-18 19:26:43 +00002814 InstructionList.push_back(I);
Chris Lattner2a98cca2007-05-03 18:58:09 +00002815 break;
2816 }
Chris Lattner0579f7f2007-05-03 22:04:19 +00002817 case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol]
Chris Lattner7337ab92007-05-06 00:00:00 +00002818 unsigned OpNum = 0;
2819 Value *Op;
2820 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
2821 OpNum+2 != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002822 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002823
Chris Lattner7337ab92007-05-06 00:00:00 +00002824 I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1);
Devang Patele8e02132009-09-18 19:26:43 +00002825 InstructionList.push_back(I);
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002826 break;
Chris Lattner0579f7f2007-05-03 22:04:19 +00002827 }
Eli Friedman21006d42011-08-09 23:02:53 +00002828 case bitc::FUNC_CODE_INST_LOADATOMIC: {
2829 // LOADATOMIC: [opty, op, align, vol, ordering, synchscope]
2830 unsigned OpNum = 0;
2831 Value *Op;
2832 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
2833 OpNum+4 != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002834 return Error(InvalidRecord);
Michael Ilseman407a6162012-11-15 22:34:00 +00002835
Eli Friedman21006d42011-08-09 23:02:53 +00002836
2837 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
2838 if (Ordering == NotAtomic || Ordering == Release ||
2839 Ordering == AcquireRelease)
Rafael Espindolae076b532013-11-04 16:16:24 +00002840 return Error(InvalidRecord);
Eli Friedman21006d42011-08-09 23:02:53 +00002841 if (Ordering != NotAtomic && Record[OpNum] == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002842 return Error(InvalidRecord);
Eli Friedman21006d42011-08-09 23:02:53 +00002843 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
2844
2845 I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1,
2846 Ordering, SynchScope);
2847 InstructionList.push_back(I);
2848 break;
2849 }
Chris Lattner4f6bab92011-06-17 18:17:37 +00002850 case bitc::FUNC_CODE_INST_STORE: { // STORE2:[ptrty, ptr, val, align, vol]
Christopher Lambfe63fb92007-12-11 08:59:05 +00002851 unsigned OpNum = 0;
2852 Value *Val, *Ptr;
2853 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002854 popValue(Record, OpNum, NextValueNo,
Christopher Lambfe63fb92007-12-11 08:59:05 +00002855 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
2856 OpNum+2 != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002857 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002858
Christopher Lambfe63fb92007-12-11 08:59:05 +00002859 I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1);
Devang Patele8e02132009-09-18 19:26:43 +00002860 InstructionList.push_back(I);
Christopher Lambfe63fb92007-12-11 08:59:05 +00002861 break;
2862 }
Eli Friedman21006d42011-08-09 23:02:53 +00002863 case bitc::FUNC_CODE_INST_STOREATOMIC: {
2864 // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, synchscope]
2865 unsigned OpNum = 0;
2866 Value *Val, *Ptr;
2867 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002868 popValue(Record, OpNum, NextValueNo,
Eli Friedman21006d42011-08-09 23:02:53 +00002869 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
2870 OpNum+4 != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002871 return Error(InvalidRecord);
Eli Friedman21006d42011-08-09 23:02:53 +00002872
2873 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
Eli Friedmanc3d35982011-09-19 19:41:28 +00002874 if (Ordering == NotAtomic || Ordering == Acquire ||
Eli Friedman21006d42011-08-09 23:02:53 +00002875 Ordering == AcquireRelease)
Rafael Espindolae076b532013-11-04 16:16:24 +00002876 return Error(InvalidRecord);
Eli Friedman21006d42011-08-09 23:02:53 +00002877 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
2878 if (Ordering != NotAtomic && Record[OpNum] == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002879 return Error(InvalidRecord);
Eli Friedman21006d42011-08-09 23:02:53 +00002880
2881 I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1,
2882 Ordering, SynchScope);
2883 InstructionList.push_back(I);
2884 break;
2885 }
Eli Friedmanff030482011-07-28 21:48:00 +00002886 case bitc::FUNC_CODE_INST_CMPXCHG: {
Stephen Hines36b56882014-04-23 16:57:46 -07002887 // CMPXCHG:[ptrty, ptr, cmp, new, vol, successordering, synchscope,
2888 // failureordering]
Eli Friedmanff030482011-07-28 21:48:00 +00002889 unsigned OpNum = 0;
2890 Value *Ptr, *Cmp, *New;
2891 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002892 popValue(Record, OpNum, NextValueNo,
Eli Friedmanff030482011-07-28 21:48:00 +00002893 cast<PointerType>(Ptr->getType())->getElementType(), Cmp) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002894 popValue(Record, OpNum, NextValueNo,
Eli Friedmanff030482011-07-28 21:48:00 +00002895 cast<PointerType>(Ptr->getType())->getElementType(), New) ||
Stephen Hines36b56882014-04-23 16:57:46 -07002896 (OpNum + 3 != Record.size() && OpNum + 4 != Record.size()))
Rafael Espindolae076b532013-11-04 16:16:24 +00002897 return Error(InvalidRecord);
Stephen Hines36b56882014-04-23 16:57:46 -07002898 AtomicOrdering SuccessOrdering = GetDecodedOrdering(Record[OpNum+1]);
2899 if (SuccessOrdering == NotAtomic || SuccessOrdering == Unordered)
Rafael Espindolae076b532013-11-04 16:16:24 +00002900 return Error(InvalidRecord);
Eli Friedmanff030482011-07-28 21:48:00 +00002901 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+2]);
Stephen Hines36b56882014-04-23 16:57:46 -07002902
2903 AtomicOrdering FailureOrdering;
2904 if (Record.size() < 7)
2905 FailureOrdering =
2906 AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering);
2907 else
2908 FailureOrdering = GetDecodedOrdering(Record[OpNum+3]);
2909
2910 I = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering, FailureOrdering,
2911 SynchScope);
Eli Friedmanff030482011-07-28 21:48:00 +00002912 cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]);
2913 InstructionList.push_back(I);
2914 break;
2915 }
2916 case bitc::FUNC_CODE_INST_ATOMICRMW: {
2917 // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, synchscope]
2918 unsigned OpNum = 0;
2919 Value *Ptr, *Val;
2920 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002921 popValue(Record, OpNum, NextValueNo,
Eli Friedmanff030482011-07-28 21:48:00 +00002922 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
2923 OpNum+4 != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002924 return Error(InvalidRecord);
Eli Friedmanff030482011-07-28 21:48:00 +00002925 AtomicRMWInst::BinOp Operation = GetDecodedRMWOperation(Record[OpNum]);
2926 if (Operation < AtomicRMWInst::FIRST_BINOP ||
2927 Operation > AtomicRMWInst::LAST_BINOP)
Rafael Espindolae076b532013-11-04 16:16:24 +00002928 return Error(InvalidRecord);
Eli Friedmanff030482011-07-28 21:48:00 +00002929 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
Eli Friedman21006d42011-08-09 23:02:53 +00002930 if (Ordering == NotAtomic || Ordering == Unordered)
Rafael Espindolae076b532013-11-04 16:16:24 +00002931 return Error(InvalidRecord);
Eli Friedmanff030482011-07-28 21:48:00 +00002932 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
2933 I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SynchScope);
2934 cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]);
2935 InstructionList.push_back(I);
2936 break;
2937 }
Eli Friedman47f35132011-07-25 23:16:38 +00002938 case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, synchscope]
2939 if (2 != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002940 return Error(InvalidRecord);
Eli Friedman47f35132011-07-25 23:16:38 +00002941 AtomicOrdering Ordering = GetDecodedOrdering(Record[0]);
2942 if (Ordering == NotAtomic || Ordering == Unordered ||
2943 Ordering == Monotonic)
Rafael Espindolae076b532013-11-04 16:16:24 +00002944 return Error(InvalidRecord);
Eli Friedman47f35132011-07-25 23:16:38 +00002945 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[1]);
2946 I = new FenceInst(Context, Ordering, SynchScope);
2947 InstructionList.push_back(I);
2948 break;
2949 }
Chris Lattner4f6bab92011-06-17 18:17:37 +00002950 case bitc::FUNC_CODE_INST_CALL: {
Duncan Sandsdc024672007-11-27 13:23:08 +00002951 // CALL: [paramattrs, cc, fnty, fnid, arg0, arg1...]
2952 if (Record.size() < 3)
Rafael Espindolae076b532013-11-04 16:16:24 +00002953 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002954
Bill Wendling99faa3b2012-12-07 23:16:57 +00002955 AttributeSet PAL = getAttributes(Record[0]);
Chris Lattnera9bb7132007-05-08 05:38:01 +00002956 unsigned CCInfo = Record[1];
Daniel Dunbara279bc32009-09-20 02:20:51 +00002957
Chris Lattnera9bb7132007-05-08 05:38:01 +00002958 unsigned OpNum = 2;
Chris Lattner7337ab92007-05-06 00:00:00 +00002959 Value *Callee;
2960 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
Rafael Espindolae076b532013-11-04 16:16:24 +00002961 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002962
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002963 PointerType *OpTy = dyn_cast<PointerType>(Callee->getType());
2964 FunctionType *FTy = 0;
Chris Lattner0579f7f2007-05-03 22:04:19 +00002965 if (OpTy) FTy = dyn_cast<FunctionType>(OpTy->getElementType());
Chris Lattner7337ab92007-05-06 00:00:00 +00002966 if (!FTy || Record.size() < FTy->getNumParams()+OpNum)
Rafael Espindolae076b532013-11-04 16:16:24 +00002967 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002968
Chris Lattner0579f7f2007-05-03 22:04:19 +00002969 SmallVector<Value*, 16> Args;
2970 // Read the fixed params.
Chris Lattner7337ab92007-05-06 00:00:00 +00002971 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
Chris Lattner1afcace2011-07-09 17:41:24 +00002972 if (FTy->getParamType(i)->isLabelTy())
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002973 Args.push_back(getBasicBlock(Record[OpNum]));
Dan Gohman9b10dfb2010-09-13 18:00:48 +00002974 else
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002975 Args.push_back(getValue(Record, OpNum, NextValueNo,
2976 FTy->getParamType(i)));
Rafael Espindolae076b532013-11-04 16:16:24 +00002977 if (Args.back() == 0)
2978 return Error(InvalidRecord);
Chris Lattner0579f7f2007-05-03 22:04:19 +00002979 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002980
Chris Lattner0579f7f2007-05-03 22:04:19 +00002981 // Read type/value pairs for varargs params.
Chris Lattner0579f7f2007-05-03 22:04:19 +00002982 if (!FTy->isVarArg()) {
Chris Lattner7337ab92007-05-06 00:00:00 +00002983 if (OpNum != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002984 return Error(InvalidRecord);
Chris Lattner0579f7f2007-05-03 22:04:19 +00002985 } else {
Chris Lattner7337ab92007-05-06 00:00:00 +00002986 while (OpNum != Record.size()) {
2987 Value *Op;
2988 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolae076b532013-11-04 16:16:24 +00002989 return Error(InvalidRecord);
Chris Lattner7337ab92007-05-06 00:00:00 +00002990 Args.push_back(Op);
Chris Lattner0579f7f2007-05-03 22:04:19 +00002991 }
2992 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002993
Jay Foada3efbb12011-07-15 08:37:34 +00002994 I = CallInst::Create(Callee, Args);
Devang Patele8e02132009-09-18 19:26:43 +00002995 InstructionList.push_back(I);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002996 cast<CallInst>(I)->setCallingConv(
2997 static_cast<CallingConv::ID>(CCInfo>>1));
Chris Lattner76520192007-05-03 22:34:03 +00002998 cast<CallInst>(I)->setTailCall(CCInfo & 1);
Devang Patel05988662008-09-25 21:00:45 +00002999 cast<CallInst>(I)->setAttributes(PAL);
Chris Lattner0579f7f2007-05-03 22:04:19 +00003000 break;
3001 }
3002 case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty]
3003 if (Record.size() < 3)
Rafael Espindolae076b532013-11-04 16:16:24 +00003004 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003005 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00003006 Value *Op = getValue(Record, 1, NextValueNo, OpTy);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003007 Type *ResTy = getTypeByID(Record[2]);
Chris Lattner0579f7f2007-05-03 22:04:19 +00003008 if (!OpTy || !Op || !ResTy)
Rafael Espindolae076b532013-11-04 16:16:24 +00003009 return Error(InvalidRecord);
Chris Lattner0579f7f2007-05-03 22:04:19 +00003010 I = new VAArgInst(Op, ResTy);
Devang Patele8e02132009-09-18 19:26:43 +00003011 InstructionList.push_back(I);
Chris Lattner0579f7f2007-05-03 22:04:19 +00003012 break;
3013 }
Chris Lattnera7c49aa2007-05-01 07:01:57 +00003014 }
3015
3016 // Add instruction to end of current BB. If there is no current BB, reject
3017 // this file.
3018 if (CurBB == 0) {
3019 delete I;
Rafael Espindolae076b532013-11-04 16:16:24 +00003020 return Error(InvalidInstructionWithNoBB);
Chris Lattnera7c49aa2007-05-01 07:01:57 +00003021 }
3022 CurBB->getInstList().push_back(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +00003023
Chris Lattnera7c49aa2007-05-01 07:01:57 +00003024 // If this was a terminator instruction, move to the next block.
3025 if (isa<TerminatorInst>(I)) {
3026 ++CurBBNo;
3027 CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : 0;
3028 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003029
Chris Lattnera7c49aa2007-05-01 07:01:57 +00003030 // Non-void values get registered in the value table for future use.
Benjamin Kramerf0127052010-01-05 13:12:22 +00003031 if (I && !I->getType()->isVoidTy())
Chris Lattnera7c49aa2007-05-01 07:01:57 +00003032 ValueList.AssignValue(I, NextValueNo++);
Chris Lattner980e5aa2007-05-01 05:52:21 +00003033 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003034
Chris Lattner5a4251c2013-01-20 02:13:19 +00003035OutOfRecordLoop:
Joe Abbeyacb61942013-02-06 22:14:06 +00003036
Chris Lattnera7c49aa2007-05-01 07:01:57 +00003037 // Check the function list for unresolved values.
3038 if (Argument *A = dyn_cast<Argument>(ValueList.back())) {
3039 if (A->getParent() == 0) {
3040 // We found at least one unresolved value. Nuke them all to avoid leaks.
3041 for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){
Stephen Hines36b56882014-04-23 16:57:46 -07003042 if ((A = dyn_cast_or_null<Argument>(ValueList[i])) && A->getParent() == 0) {
Owen Anderson9e9a0d52009-07-30 23:03:37 +00003043 A->replaceAllUsesWith(UndefValue::get(A->getType()));
Chris Lattnera7c49aa2007-05-01 07:01:57 +00003044 delete A;
3045 }
3046 }
Rafael Espindolae076b532013-11-04 16:16:24 +00003047 return Error(NeverResolvedValueFoundInFunction);
Chris Lattnera7c49aa2007-05-01 07:01:57 +00003048 }
Chris Lattnera7c49aa2007-05-01 07:01:57 +00003049 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003050
Dan Gohman064ff3e2010-08-25 20:23:38 +00003051 // FIXME: Check for unresolved forward-declared metadata references
3052 // and clean up leaks.
3053
Chris Lattner50b136d2009-10-28 05:53:48 +00003054 // See if anything took the address of blocks in this function. If so,
3055 // resolve them now.
Chris Lattner50b136d2009-10-28 05:53:48 +00003056 DenseMap<Function*, std::vector<BlockAddrRefTy> >::iterator BAFRI =
3057 BlockAddrFwdRefs.find(F);
3058 if (BAFRI != BlockAddrFwdRefs.end()) {
3059 std::vector<BlockAddrRefTy> &RefList = BAFRI->second;
3060 for (unsigned i = 0, e = RefList.size(); i != e; ++i) {
3061 unsigned BlockIdx = RefList[i].first;
Chris Lattnercdfc9402009-11-01 01:27:45 +00003062 if (BlockIdx >= FunctionBBs.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00003063 return Error(InvalidID);
Michael Ilseman407a6162012-11-15 22:34:00 +00003064
Chris Lattner50b136d2009-10-28 05:53:48 +00003065 GlobalVariable *FwdRef = RefList[i].second;
Chris Lattnercdfc9402009-11-01 01:27:45 +00003066 FwdRef->replaceAllUsesWith(BlockAddress::get(F, FunctionBBs[BlockIdx]));
Chris Lattner50b136d2009-10-28 05:53:48 +00003067 FwdRef->eraseFromParent();
3068 }
Michael Ilseman407a6162012-11-15 22:34:00 +00003069
Chris Lattner50b136d2009-10-28 05:53:48 +00003070 BlockAddrFwdRefs.erase(BAFRI);
3071 }
Michael Ilseman407a6162012-11-15 22:34:00 +00003072
Chris Lattner980e5aa2007-05-01 05:52:21 +00003073 // Trim the value list down to the size it was before we parsed this function.
3074 ValueList.shrinkTo(ModuleValueListSize);
Dan Gohman69813832010-08-25 20:22:53 +00003075 MDValueList.shrinkTo(ModuleMDValueListSize);
Chris Lattner980e5aa2007-05-01 05:52:21 +00003076 std::vector<BasicBlock*>().swap(FunctionBBs);
Rafael Espindolae076b532013-11-04 16:16:24 +00003077 return error_code::success();
Chris Lattner48f84872007-05-01 04:59:48 +00003078}
3079
Rafael Espindolae05744b2013-11-05 17:16:08 +00003080/// Find the function body in the bitcode stream
3081error_code BitcodeReader::FindFunctionInStream(Function *F,
Derek Schuff2ea93872012-02-06 22:30:29 +00003082 DenseMap<Function*, uint64_t>::iterator DeferredFunctionInfoIterator) {
3083 while (DeferredFunctionInfoIterator->second == 0) {
3084 if (Stream.AtEndOfStream())
Rafael Espindolae076b532013-11-04 16:16:24 +00003085 return Error(CouldNotFindFunctionInStream);
Derek Schuff2ea93872012-02-06 22:30:29 +00003086 // ParseModule will parse the next body in the stream and set its
3087 // position in the DeferredFunctionInfo map.
Rafael Espindolae05744b2013-11-05 17:16:08 +00003088 if (error_code EC = ParseModule(true))
3089 return EC;
Derek Schuff2ea93872012-02-06 22:30:29 +00003090 }
Rafael Espindolae05744b2013-11-05 17:16:08 +00003091 return error_code::success();
Derek Schuff2ea93872012-02-06 22:30:29 +00003092}
3093
Chris Lattnerb348bb82007-05-18 04:02:46 +00003094//===----------------------------------------------------------------------===//
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003095// GVMaterializer implementation
Chris Lattnerb348bb82007-05-18 04:02:46 +00003096//===----------------------------------------------------------------------===//
3097
3098
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003099bool BitcodeReader::isMaterializable(const GlobalValue *GV) const {
3100 if (const Function *F = dyn_cast<Function>(GV)) {
3101 return F->isDeclaration() &&
3102 DeferredFunctionInfo.count(const_cast<Function*>(F));
3103 }
3104 return false;
3105}
Daniel Dunbara279bc32009-09-20 02:20:51 +00003106
Rafael Espindolaaf9e8e62013-11-05 19:36:34 +00003107error_code BitcodeReader::Materialize(GlobalValue *GV) {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003108 Function *F = dyn_cast<Function>(GV);
3109 // If it's not a function or is already material, ignore the request.
Rafael Espindolaaf9e8e62013-11-05 19:36:34 +00003110 if (!F || !F->isMaterializable())
3111 return error_code::success();
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003112
3113 DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F);
Chris Lattnerb348bb82007-05-18 04:02:46 +00003114 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
Derek Schuff2ea93872012-02-06 22:30:29 +00003115 // If its position is recorded as 0, its body is somewhere in the stream
3116 // but we haven't seen it yet.
Rafael Espindolaaf9e8e62013-11-05 19:36:34 +00003117 if (DFII->second == 0 && LazyStreamer)
3118 if (error_code EC = FindFunctionInStream(F, DFII))
3119 return EC;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003120
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003121 // Move the bit stream to the saved position of the deferred function body.
3122 Stream.JumpToBit(DFII->second);
Daniel Dunbara279bc32009-09-20 02:20:51 +00003123
Rafael Espindolaaf9e8e62013-11-05 19:36:34 +00003124 if (error_code EC = ParseFunctionBody(F))
3125 return EC;
Chandler Carruth69940402007-08-04 01:51:18 +00003126
3127 // Upgrade any old intrinsic calls in the function.
3128 for (UpgradedIntrinsicMap::iterator I = UpgradedIntrinsics.begin(),
3129 E = UpgradedIntrinsics.end(); I != E; ++I) {
3130 if (I->first != I->second) {
Stephen Hines36b56882014-04-23 16:57:46 -07003131 for (auto UI = I->first->user_begin(), UE = I->first->user_end();
3132 UI != UE;) {
Chandler Carruth69940402007-08-04 01:51:18 +00003133 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
3134 UpgradeIntrinsicCall(CI, I->second);
3135 }
3136 }
3137 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003138
Rafael Espindolaaf9e8e62013-11-05 19:36:34 +00003139 return error_code::success();
Chris Lattnerb348bb82007-05-18 04:02:46 +00003140}
3141
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003142bool BitcodeReader::isDematerializable(const GlobalValue *GV) const {
3143 const Function *F = dyn_cast<Function>(GV);
3144 if (!F || F->isDeclaration())
3145 return false;
3146 return DeferredFunctionInfo.count(const_cast<Function*>(F));
3147}
3148
3149void BitcodeReader::Dematerialize(GlobalValue *GV) {
3150 Function *F = dyn_cast<Function>(GV);
3151 // If this function isn't dematerializable, this is a noop.
3152 if (!F || !isDematerializable(F))
Chris Lattnerb348bb82007-05-18 04:02:46 +00003153 return;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003154
Chris Lattnerb348bb82007-05-18 04:02:46 +00003155 assert(DeferredFunctionInfo.count(F) && "No info to read function later?");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003156
Chris Lattnerb348bb82007-05-18 04:02:46 +00003157 // Just forget the function body, we can remat it later.
3158 F->deleteBody();
Chris Lattnerb348bb82007-05-18 04:02:46 +00003159}
3160
3161
Rafael Espindolaaf9e8e62013-11-05 19:36:34 +00003162error_code BitcodeReader::MaterializeModule(Module *M) {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003163 assert(M == TheModule &&
3164 "Can only Materialize the Module this BitcodeReader is attached to.");
Chris Lattner714fa952009-06-16 05:15:21 +00003165 // Iterate over the module, deserializing any functions that are still on
3166 // disk.
3167 for (Module::iterator F = TheModule->begin(), E = TheModule->end();
Rafael Espindolaaf9e8e62013-11-05 19:36:34 +00003168 F != E; ++F) {
3169 if (F->isMaterializable()) {
3170 if (error_code EC = Materialize(F))
3171 return EC;
3172 }
3173 }
Derek Schuff0ffe6982012-02-29 00:07:09 +00003174 // At this point, if there are any function bodies, the current bit is
3175 // pointing to the END_BLOCK record after them. Now make sure the rest
3176 // of the bits in the module have been read.
3177 if (NextUnreadBit)
3178 ParseModule(true);
3179
Daniel Dunbara279bc32009-09-20 02:20:51 +00003180 // Upgrade any intrinsic calls that slipped through (should not happen!) and
3181 // delete the old functions to clean up. We can't do this unless the entire
3182 // module is materialized because there could always be another function body
Chandler Carruth69940402007-08-04 01:51:18 +00003183 // with calls to the old function.
3184 for (std::vector<std::pair<Function*, Function*> >::iterator I =
3185 UpgradedIntrinsics.begin(), E = UpgradedIntrinsics.end(); I != E; ++I) {
3186 if (I->first != I->second) {
Stephen Hines36b56882014-04-23 16:57:46 -07003187 for (auto UI = I->first->user_begin(), UE = I->first->user_end();
3188 UI != UE;) {
Chandler Carruth69940402007-08-04 01:51:18 +00003189 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
3190 UpgradeIntrinsicCall(CI, I->second);
3191 }
Chris Lattner7d9eb582009-04-01 01:43:03 +00003192 if (!I->first->use_empty())
3193 I->first->replaceAllUsesWith(I->second);
Chandler Carruth69940402007-08-04 01:51:18 +00003194 I->first->eraseFromParent();
3195 }
3196 }
3197 std::vector<std::pair<Function*, Function*> >().swap(UpgradedIntrinsics);
Devang Patele4b27562009-08-28 23:24:31 +00003198
Manman Ren804f0342013-09-28 00:22:27 +00003199 for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++)
3200 UpgradeInstWithTBAATag(InstsWithTBAATag[I]);
3201
Manman Ren27457ac2013-12-09 21:06:30 +00003202 UpgradeDebugInfo(*M);
Rafael Espindolaaf9e8e62013-11-05 19:36:34 +00003203 return error_code::success();
Chris Lattnerb348bb82007-05-18 04:02:46 +00003204}
3205
Rafael Espindolae076b532013-11-04 16:16:24 +00003206error_code BitcodeReader::InitStream() {
3207 if (LazyStreamer)
3208 return InitLazyStream();
Derek Schuff2ea93872012-02-06 22:30:29 +00003209 return InitStreamFromBuffer();
3210}
3211
Rafael Espindolae076b532013-11-04 16:16:24 +00003212error_code BitcodeReader::InitStreamFromBuffer() {
Roman Divacky5177b3a2012-09-06 15:42:13 +00003213 const unsigned char *BufPtr = (const unsigned char*)Buffer->getBufferStart();
Derek Schuff2ea93872012-02-06 22:30:29 +00003214 const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
3215
3216 if (Buffer->getBufferSize() & 3) {
3217 if (!isRawBitcode(BufPtr, BufEnd) && !isBitcodeWrapper(BufPtr, BufEnd))
Rafael Espindolae076b532013-11-04 16:16:24 +00003218 return Error(InvalidBitcodeSignature);
Derek Schuff2ea93872012-02-06 22:30:29 +00003219 else
Rafael Espindolae076b532013-11-04 16:16:24 +00003220 return Error(BitcodeStreamInvalidSize);
Derek Schuff2ea93872012-02-06 22:30:29 +00003221 }
3222
3223 // If we have a wrapper header, parse it and ignore the non-bc file contents.
3224 // The magic number is 0x0B17C0DE stored in little endian.
3225 if (isBitcodeWrapper(BufPtr, BufEnd))
3226 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
Rafael Espindolae076b532013-11-04 16:16:24 +00003227 return Error(InvalidBitcodeWrapperHeader);
Derek Schuff2ea93872012-02-06 22:30:29 +00003228
3229 StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
3230 Stream.init(*StreamFile);
3231
Rafael Espindolae076b532013-11-04 16:16:24 +00003232 return error_code::success();
Derek Schuff2ea93872012-02-06 22:30:29 +00003233}
3234
Rafael Espindolae076b532013-11-04 16:16:24 +00003235error_code BitcodeReader::InitLazyStream() {
Derek Schuff2ea93872012-02-06 22:30:29 +00003236 // Check and strip off the bitcode wrapper; BitstreamReader expects never to
3237 // see it.
3238 StreamingMemoryObject *Bytes = new StreamingMemoryObject(LazyStreamer);
3239 StreamFile.reset(new BitstreamReader(Bytes));
3240 Stream.init(*StreamFile);
3241
3242 unsigned char buf[16];
Benjamin Kramer49a6a8d2013-05-24 10:54:58 +00003243 if (Bytes->readBytes(0, 16, buf) == -1)
Rafael Espindolae076b532013-11-04 16:16:24 +00003244 return Error(BitcodeStreamInvalidSize);
Derek Schuff2ea93872012-02-06 22:30:29 +00003245
3246 if (!isBitcode(buf, buf + 16))
Rafael Espindolae076b532013-11-04 16:16:24 +00003247 return Error(InvalidBitcodeSignature);
Derek Schuff2ea93872012-02-06 22:30:29 +00003248
3249 if (isBitcodeWrapper(buf, buf + 4)) {
3250 const unsigned char *bitcodeStart = buf;
3251 const unsigned char *bitcodeEnd = buf + 16;
3252 SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
3253 Bytes->dropLeadingBytes(bitcodeStart - buf);
3254 Bytes->setKnownObjectSize(bitcodeEnd - bitcodeStart);
3255 }
Rafael Espindolae076b532013-11-04 16:16:24 +00003256 return error_code::success();
3257}
3258
3259namespace {
Stephen Hines36b56882014-04-23 16:57:46 -07003260class BitcodeErrorCategoryType : public error_category {
3261 const char *name() const override {
Rafael Espindolae076b532013-11-04 16:16:24 +00003262 return "llvm.bitcode";
3263 }
Stephen Hines36b56882014-04-23 16:57:46 -07003264 std::string message(int IE) const override {
Rafael Espindolae076b532013-11-04 16:16:24 +00003265 BitcodeReader::ErrorType E = static_cast<BitcodeReader::ErrorType>(IE);
3266 switch (E) {
3267 case BitcodeReader::BitcodeStreamInvalidSize:
3268 return "Bitcode stream length should be >= 16 bytes and a multiple of 4";
3269 case BitcodeReader::ConflictingMETADATA_KINDRecords:
3270 return "Conflicting METADATA_KIND records";
3271 case BitcodeReader::CouldNotFindFunctionInStream:
3272 return "Could not find function in stream";
3273 case BitcodeReader::ExpectedConstant:
3274 return "Expected a constant";
3275 case BitcodeReader::InsufficientFunctionProtos:
3276 return "Insufficient function protos";
3277 case BitcodeReader::InvalidBitcodeSignature:
3278 return "Invalid bitcode signature";
3279 case BitcodeReader::InvalidBitcodeWrapperHeader:
3280 return "Invalid bitcode wrapper header";
3281 case BitcodeReader::InvalidConstantReference:
3282 return "Invalid ronstant reference";
3283 case BitcodeReader::InvalidID:
3284 return "Invalid ID";
3285 case BitcodeReader::InvalidInstructionWithNoBB:
3286 return "Invalid instruction with no BB";
3287 case BitcodeReader::InvalidRecord:
3288 return "Invalid record";
3289 case BitcodeReader::InvalidTypeForValue:
3290 return "Invalid type for value";
3291 case BitcodeReader::InvalidTYPETable:
3292 return "Invalid TYPE table";
3293 case BitcodeReader::InvalidType:
3294 return "Invalid type";
3295 case BitcodeReader::MalformedBlock:
3296 return "Malformed block";
3297 case BitcodeReader::MalformedGlobalInitializerSet:
3298 return "Malformed global initializer set";
3299 case BitcodeReader::InvalidMultipleBlocks:
3300 return "Invalid multiple blocks";
3301 case BitcodeReader::NeverResolvedValueFoundInFunction:
3302 return "Never resolved value found in function";
3303 case BitcodeReader::InvalidValue:
3304 return "Invalid value";
3305 }
Benjamin Kramera83342b2013-11-05 13:45:09 +00003306 llvm_unreachable("Unknown error type!");
Rafael Espindolae076b532013-11-04 16:16:24 +00003307 }
3308};
3309}
3310
3311const error_category &BitcodeReader::BitcodeErrorCategory() {
3312 static BitcodeErrorCategoryType O;
3313 return O;
Derek Schuff2ea93872012-02-06 22:30:29 +00003314}
Chris Lattner48f84872007-05-01 04:59:48 +00003315
Chris Lattnerc453f762007-04-29 07:54:31 +00003316//===----------------------------------------------------------------------===//
3317// External interface
3318//===----------------------------------------------------------------------===//
3319
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003320/// getLazyBitcodeModule - lazy function-at-a-time loading from a file.
Chris Lattnerc453f762007-04-29 07:54:31 +00003321///
Stephen Hines36b56882014-04-23 16:57:46 -07003322ErrorOr<Module *> llvm::getLazyBitcodeModule(MemoryBuffer *Buffer,
3323 LLVMContext &Context) {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003324 Module *M = new Module(Buffer->getBufferIdentifier(), Context);
Owen Anderson8b477ed2009-07-01 16:58:40 +00003325 BitcodeReader *R = new BitcodeReader(Buffer, Context);
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003326 M->setMaterializer(R);
Rafael Espindolae076b532013-11-04 16:16:24 +00003327 if (error_code EC = R->ParseBitcodeInto(M)) {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003328 delete M; // Also deletes R.
Stephen Hines36b56882014-04-23 16:57:46 -07003329 return EC;
Chris Lattnerc453f762007-04-29 07:54:31 +00003330 }
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003331 // Have the BitcodeReader dtor delete 'Buffer'.
3332 R->setBufferOwned(true);
Rafael Espindola47f79bb2012-01-02 07:49:53 +00003333
3334 R->materializeForwardReferencedFunctions();
3335
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003336 return M;
Chris Lattnerc453f762007-04-29 07:54:31 +00003337}
3338
Derek Schuff2ea93872012-02-06 22:30:29 +00003339
3340Module *llvm::getStreamedBitcodeModule(const std::string &name,
3341 DataStreamer *streamer,
3342 LLVMContext &Context,
3343 std::string *ErrMsg) {
3344 Module *M = new Module(name, Context);
3345 BitcodeReader *R = new BitcodeReader(streamer, Context);
3346 M->setMaterializer(R);
Rafael Espindolae076b532013-11-04 16:16:24 +00003347 if (error_code EC = R->ParseBitcodeInto(M)) {
Derek Schuff2ea93872012-02-06 22:30:29 +00003348 if (ErrMsg)
Rafael Espindolae076b532013-11-04 16:16:24 +00003349 *ErrMsg = EC.message();
Derek Schuff2ea93872012-02-06 22:30:29 +00003350 delete M; // Also deletes R.
3351 return 0;
3352 }
3353 R->setBufferOwned(false); // no buffer to delete
3354 return M;
3355}
3356
Stephen Hines36b56882014-04-23 16:57:46 -07003357ErrorOr<Module *> llvm::parseBitcodeFile(MemoryBuffer *Buffer,
3358 LLVMContext &Context) {
3359 ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
3360 if (!ModuleOrErr)
3361 return ModuleOrErr;
3362 Module *M = ModuleOrErr.get();
Chris Lattnerb348bb82007-05-18 04:02:46 +00003363
3364 // Don't let the BitcodeReader dtor delete 'Buffer', regardless of whether
3365 // there was an error.
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003366 static_cast<BitcodeReader*>(M->getMaterializer())->setBufferOwned(false);
Daniel Dunbara279bc32009-09-20 02:20:51 +00003367
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003368 // Read in the entire module, and destroy the BitcodeReader.
Stephen Hines36b56882014-04-23 16:57:46 -07003369 if (error_code EC = M->materializeAllPermanently()) {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003370 delete M;
Stephen Hines36b56882014-04-23 16:57:46 -07003371 return EC;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003372 }
Bill Wendling34711742010-10-06 01:22:42 +00003373
Chad Rosiercbbb0962011-12-07 21:44:12 +00003374 // TODO: Restore the use-lists to the in-memory state when the bitcode was
3375 // written. We must defer until the Module has been fully materialized.
3376
Chris Lattnerc453f762007-04-29 07:54:31 +00003377 return M;
3378}
Bill Wendling34711742010-10-06 01:22:42 +00003379
3380std::string llvm::getBitcodeTargetTriple(MemoryBuffer *Buffer,
3381 LLVMContext& Context,
3382 std::string *ErrMsg) {
3383 BitcodeReader *R = new BitcodeReader(Buffer, Context);
3384 // Don't let the BitcodeReader dtor delete 'Buffer'.
3385 R->setBufferOwned(false);
3386
3387 std::string Triple("");
Rafael Espindolae076b532013-11-04 16:16:24 +00003388 if (error_code EC = R->ParseTriple(Triple))
Bill Wendling34711742010-10-06 01:22:42 +00003389 if (ErrMsg)
Rafael Espindolae076b532013-11-04 16:16:24 +00003390 *ErrMsg = EC.message();
Bill Wendling34711742010-10-06 01:22:42 +00003391
3392 delete R;
3393 return Triple;
3394}