blob: e052e6e5bae0e85c6faa2f7a56fe4b0f02b6ca1f [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"
14#include "llvm/AutoUpgrade.h"
Tobias Grossere7bc5bb2013-07-26 04:16:55 +000015#include "llvm/Bitcode/LLVMBitCodes.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;
83 case 5: return GlobalValue::DLLImportLinkage;
84 case 6: return GlobalValue::DLLExportLinkage;
85 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;
Bill Wendling3d10a5a2009-07-20 01:03:30 +000091 case 13: return GlobalValue::LinkerPrivateLinkage;
Bill Wendling5e721d72010-07-01 21:55:59 +000092 case 14: return GlobalValue::LinkerPrivateWeakLinkage;
Chris Lattnercaee0dc2007-04-22 06:23:29 +000093 }
94}
95
96static GlobalValue::VisibilityTypes GetDecodedVisibility(unsigned Val) {
97 switch (Val) {
98 default: // Map unknown visibilities to default.
99 case 0: return GlobalValue::DefaultVisibility;
100 case 1: return GlobalValue::HiddenVisibility;
Anton Korobeynikov9cd3ccf2007-04-29 20:56:48 +0000101 case 2: return GlobalValue::ProtectedVisibility;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000102 }
103}
104
Hans Wennborgce718ff2012-06-23 11:37:03 +0000105static GlobalVariable::ThreadLocalMode GetDecodedThreadLocalMode(unsigned Val) {
106 switch (Val) {
107 case 0: return GlobalVariable::NotThreadLocal;
108 default: // Map unknown non-zero value to general dynamic.
109 case 1: return GlobalVariable::GeneralDynamicTLSModel;
110 case 2: return GlobalVariable::LocalDynamicTLSModel;
111 case 3: return GlobalVariable::InitialExecTLSModel;
112 case 4: return GlobalVariable::LocalExecTLSModel;
113 }
114}
115
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000116static int GetDecodedCastOpcode(unsigned Val) {
117 switch (Val) {
118 default: return -1;
119 case bitc::CAST_TRUNC : return Instruction::Trunc;
120 case bitc::CAST_ZEXT : return Instruction::ZExt;
121 case bitc::CAST_SEXT : return Instruction::SExt;
122 case bitc::CAST_FPTOUI : return Instruction::FPToUI;
123 case bitc::CAST_FPTOSI : return Instruction::FPToSI;
124 case bitc::CAST_UITOFP : return Instruction::UIToFP;
125 case bitc::CAST_SITOFP : return Instruction::SIToFP;
126 case bitc::CAST_FPTRUNC : return Instruction::FPTrunc;
127 case bitc::CAST_FPEXT : return Instruction::FPExt;
128 case bitc::CAST_PTRTOINT: return Instruction::PtrToInt;
129 case bitc::CAST_INTTOPTR: return Instruction::IntToPtr;
130 case bitc::CAST_BITCAST : return Instruction::BitCast;
Matt Arsenault59d3ae62013-11-15 01:34:59 +0000131 case bitc::CAST_ADDRSPACECAST : return Instruction::AddrSpaceCast;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000132 }
133}
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000134static int GetDecodedBinaryOpcode(unsigned Val, Type *Ty) {
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000135 switch (Val) {
136 default: return -1;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000137 case bitc::BINOP_ADD:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000138 return Ty->isFPOrFPVectorTy() ? Instruction::FAdd : Instruction::Add;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000139 case bitc::BINOP_SUB:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000140 return Ty->isFPOrFPVectorTy() ? Instruction::FSub : Instruction::Sub;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000141 case bitc::BINOP_MUL:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000142 return Ty->isFPOrFPVectorTy() ? Instruction::FMul : Instruction::Mul;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000143 case bitc::BINOP_UDIV: return Instruction::UDiv;
144 case bitc::BINOP_SDIV:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000145 return Ty->isFPOrFPVectorTy() ? Instruction::FDiv : Instruction::SDiv;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000146 case bitc::BINOP_UREM: return Instruction::URem;
147 case bitc::BINOP_SREM:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000148 return Ty->isFPOrFPVectorTy() ? Instruction::FRem : Instruction::SRem;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000149 case bitc::BINOP_SHL: return Instruction::Shl;
150 case bitc::BINOP_LSHR: return Instruction::LShr;
151 case bitc::BINOP_ASHR: return Instruction::AShr;
152 case bitc::BINOP_AND: return Instruction::And;
153 case bitc::BINOP_OR: return Instruction::Or;
154 case bitc::BINOP_XOR: return Instruction::Xor;
155 }
156}
157
Eli Friedmanff030482011-07-28 21:48:00 +0000158static AtomicRMWInst::BinOp GetDecodedRMWOperation(unsigned Val) {
159 switch (Val) {
160 default: return AtomicRMWInst::BAD_BINOP;
161 case bitc::RMW_XCHG: return AtomicRMWInst::Xchg;
162 case bitc::RMW_ADD: return AtomicRMWInst::Add;
163 case bitc::RMW_SUB: return AtomicRMWInst::Sub;
164 case bitc::RMW_AND: return AtomicRMWInst::And;
165 case bitc::RMW_NAND: return AtomicRMWInst::Nand;
166 case bitc::RMW_OR: return AtomicRMWInst::Or;
167 case bitc::RMW_XOR: return AtomicRMWInst::Xor;
168 case bitc::RMW_MAX: return AtomicRMWInst::Max;
169 case bitc::RMW_MIN: return AtomicRMWInst::Min;
170 case bitc::RMW_UMAX: return AtomicRMWInst::UMax;
171 case bitc::RMW_UMIN: return AtomicRMWInst::UMin;
172 }
173}
174
Eli Friedman47f35132011-07-25 23:16:38 +0000175static AtomicOrdering GetDecodedOrdering(unsigned Val) {
176 switch (Val) {
177 case bitc::ORDERING_NOTATOMIC: return NotAtomic;
178 case bitc::ORDERING_UNORDERED: return Unordered;
179 case bitc::ORDERING_MONOTONIC: return Monotonic;
180 case bitc::ORDERING_ACQUIRE: return Acquire;
181 case bitc::ORDERING_RELEASE: return Release;
182 case bitc::ORDERING_ACQREL: return AcquireRelease;
183 default: // Map unknown orderings to sequentially-consistent.
184 case bitc::ORDERING_SEQCST: return SequentiallyConsistent;
185 }
186}
187
188static SynchronizationScope GetDecodedSynchScope(unsigned Val) {
189 switch (Val) {
190 case bitc::SYNCHSCOPE_SINGLETHREAD: return SingleThread;
191 default: // Map unknown scopes to cross-thread.
192 case bitc::SYNCHSCOPE_CROSSTHREAD: return CrossThread;
193 }
194}
195
Gabor Greifefe65362008-05-10 08:32:32 +0000196namespace llvm {
Chris Lattner522b7b12007-04-24 05:48:56 +0000197namespace {
198 /// @brief A class for maintaining the slot number definition
199 /// as a placeholder for the actual definition for forward constants defs.
200 class ConstantPlaceHolder : public ConstantExpr {
Craig Topper86a1c322012-09-15 17:09:36 +0000201 void operator=(const ConstantPlaceHolder &) LLVM_DELETED_FUNCTION;
Gabor Greif051a9502008-04-06 20:25:17 +0000202 public:
203 // allocate space for exactly one operand
204 void *operator new(size_t s) {
205 return User::operator new(s, 1);
206 }
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000207 explicit ConstantPlaceHolder(Type *Ty, LLVMContext& Context)
Gabor Greifefe65362008-05-10 08:32:32 +0000208 : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000209 Op<0>() = UndefValue::get(Type::getInt32Ty(Context));
Chris Lattner522b7b12007-04-24 05:48:56 +0000210 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000211
Chris Lattnerea693df2008-08-21 02:34:16 +0000212 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
Chris Lattnerea693df2008-08-21 02:34:16 +0000213 static bool classof(const Value *V) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000214 return isa<ConstantExpr>(V) &&
Chris Lattnerea693df2008-08-21 02:34:16 +0000215 cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1;
216 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000217
218
Gabor Greifefe65362008-05-10 08:32:32 +0000219 /// Provide fast operand accessors
Chris Lattner46e77402009-03-31 22:55:09 +0000220 //DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Chris Lattner522b7b12007-04-24 05:48:56 +0000221 };
222}
223
Chris Lattner46e77402009-03-31 22:55:09 +0000224// FIXME: can we inherit this from ConstantExpr?
Gabor Greifefe65362008-05-10 08:32:32 +0000225template <>
Jay Foad67c619b2011-01-11 15:07:38 +0000226struct OperandTraits<ConstantPlaceHolder> :
227 public FixedNumOperandTraits<ConstantPlaceHolder, 1> {
Gabor Greifefe65362008-05-10 08:32:32 +0000228};
Gabor Greifefe65362008-05-10 08:32:32 +0000229}
230
Chris Lattner46e77402009-03-31 22:55:09 +0000231
232void BitcodeReaderValueList::AssignValue(Value *V, unsigned Idx) {
233 if (Idx == size()) {
234 push_back(V);
235 return;
236 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000237
Chris Lattner46e77402009-03-31 22:55:09 +0000238 if (Idx >= size())
239 resize(Idx+1);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000240
Chris Lattner46e77402009-03-31 22:55:09 +0000241 WeakVH &OldV = ValuePtrs[Idx];
242 if (OldV == 0) {
243 OldV = V;
244 return;
245 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000246
Chris Lattner46e77402009-03-31 22:55:09 +0000247 // Handle constants and non-constants (e.g. instrs) differently for
248 // efficiency.
249 if (Constant *PHC = dyn_cast<Constant>(&*OldV)) {
250 ResolveConstants.push_back(std::make_pair(PHC, Idx));
251 OldV = V;
252 } else {
253 // If there was a forward reference to this value, replace it.
254 Value *PrevVal = OldV;
255 OldV->replaceAllUsesWith(V);
256 delete PrevVal;
Gabor Greifefe65362008-05-10 08:32:32 +0000257 }
258}
Daniel Dunbara279bc32009-09-20 02:20:51 +0000259
Gabor Greifefe65362008-05-10 08:32:32 +0000260
Chris Lattner522b7b12007-04-24 05:48:56 +0000261Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000262 Type *Ty) {
Chris Lattner46e77402009-03-31 22:55:09 +0000263 if (Idx >= size())
Gabor Greifefe65362008-05-10 08:32:32 +0000264 resize(Idx + 1);
Chris Lattner522b7b12007-04-24 05:48:56 +0000265
Chris Lattner46e77402009-03-31 22:55:09 +0000266 if (Value *V = ValuePtrs[Idx]) {
Chris Lattnera7c49aa2007-05-01 07:01:57 +0000267 assert(Ty == V->getType() && "Type mismatch in constant table!");
268 return cast<Constant>(V);
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000269 }
Chris Lattner522b7b12007-04-24 05:48:56 +0000270
271 // Create and return a placeholder, which will later be RAUW'd.
Owen Anderson74a77812009-07-07 20:18:58 +0000272 Constant *C = new ConstantPlaceHolder(Ty, Context);
Chris Lattner46e77402009-03-31 22:55:09 +0000273 ValuePtrs[Idx] = C;
Chris Lattner522b7b12007-04-24 05:48:56 +0000274 return C;
275}
276
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000277Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, Type *Ty) {
Chris Lattner46e77402009-03-31 22:55:09 +0000278 if (Idx >= size())
Gabor Greifefe65362008-05-10 08:32:32 +0000279 resize(Idx + 1);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000280
Chris Lattner46e77402009-03-31 22:55:09 +0000281 if (Value *V = ValuePtrs[Idx]) {
Chris Lattnera7c49aa2007-05-01 07:01:57 +0000282 assert((Ty == 0 || Ty == V->getType()) && "Type mismatch in value table!");
283 return V;
284 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000285
Chris Lattner01ff65f2007-05-02 05:16:49 +0000286 // No type specified, must be invalid reference.
287 if (Ty == 0) return 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000288
Chris Lattnera7c49aa2007-05-01 07:01:57 +0000289 // Create and return a placeholder, which will later be RAUW'd.
290 Value *V = new Argument(Ty);
Chris Lattner46e77402009-03-31 22:55:09 +0000291 ValuePtrs[Idx] = V;
Chris Lattnera7c49aa2007-05-01 07:01:57 +0000292 return V;
293}
294
Chris Lattnerea693df2008-08-21 02:34:16 +0000295/// ResolveConstantForwardRefs - Once all constants are read, this method bulk
296/// resolves any forward references. The idea behind this is that we sometimes
297/// get constants (such as large arrays) which reference *many* forward ref
298/// constants. Replacing each of these causes a lot of thrashing when
299/// building/reuniquing the constant. Instead of doing this, we look at all the
300/// uses and rewrite all the place holders at once for any constant that uses
301/// a placeholder.
302void BitcodeReaderValueList::ResolveConstantForwardRefs() {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000303 // Sort the values by-pointer so that they are efficient to look up with a
Chris Lattnerea693df2008-08-21 02:34:16 +0000304 // binary search.
305 std::sort(ResolveConstants.begin(), ResolveConstants.end());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000306
Chris Lattnerea693df2008-08-21 02:34:16 +0000307 SmallVector<Constant*, 64> NewOps;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000308
Chris Lattnerea693df2008-08-21 02:34:16 +0000309 while (!ResolveConstants.empty()) {
Chris Lattner46e77402009-03-31 22:55:09 +0000310 Value *RealVal = operator[](ResolveConstants.back().second);
Chris Lattnerea693df2008-08-21 02:34:16 +0000311 Constant *Placeholder = ResolveConstants.back().first;
312 ResolveConstants.pop_back();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000313
Chris Lattnerea693df2008-08-21 02:34:16 +0000314 // Loop over all users of the placeholder, updating them to reference the
315 // new value. If they reference more than one placeholder, update them all
316 // at once.
317 while (!Placeholder->use_empty()) {
Chris Lattnerb6135a02008-08-21 17:31:45 +0000318 Value::use_iterator UI = Placeholder->use_begin();
Gabor Greifc654d1b2010-07-09 16:01:21 +0000319 User *U = *UI;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000320
Chris Lattnerea693df2008-08-21 02:34:16 +0000321 // If the using object isn't uniqued, just update the operands. This
322 // handles instructions and initializers for global variables.
Gabor Greifc654d1b2010-07-09 16:01:21 +0000323 if (!isa<Constant>(U) || isa<GlobalValue>(U)) {
Chris Lattnerb6135a02008-08-21 17:31:45 +0000324 UI.getUse().set(RealVal);
Chris Lattnerea693df2008-08-21 02:34:16 +0000325 continue;
326 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000327
Chris Lattnerea693df2008-08-21 02:34:16 +0000328 // Otherwise, we have a constant that uses the placeholder. Replace that
329 // constant with a new constant that has *all* placeholder uses updated.
Gabor Greifc654d1b2010-07-09 16:01:21 +0000330 Constant *UserC = cast<Constant>(U);
Chris Lattnerea693df2008-08-21 02:34:16 +0000331 for (User::op_iterator I = UserC->op_begin(), E = UserC->op_end();
332 I != E; ++I) {
333 Value *NewOp;
334 if (!isa<ConstantPlaceHolder>(*I)) {
335 // Not a placeholder reference.
336 NewOp = *I;
337 } else if (*I == Placeholder) {
338 // Common case is that it just references this one placeholder.
339 NewOp = RealVal;
340 } else {
341 // Otherwise, look up the placeholder in ResolveConstants.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000342 ResolveConstantsTy::iterator It =
343 std::lower_bound(ResolveConstants.begin(), ResolveConstants.end(),
Chris Lattnerea693df2008-08-21 02:34:16 +0000344 std::pair<Constant*, unsigned>(cast<Constant>(*I),
345 0));
346 assert(It != ResolveConstants.end() && It->first == *I);
Chris Lattner46e77402009-03-31 22:55:09 +0000347 NewOp = operator[](It->second);
Chris Lattnerea693df2008-08-21 02:34:16 +0000348 }
349
350 NewOps.push_back(cast<Constant>(NewOp));
351 }
352
353 // Make the new constant.
354 Constant *NewC;
355 if (ConstantArray *UserCA = dyn_cast<ConstantArray>(UserC)) {
Jay Foad26701082011-06-22 09:24:39 +0000356 NewC = ConstantArray::get(UserCA->getType(), NewOps);
Chris Lattnerea693df2008-08-21 02:34:16 +0000357 } else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) {
Chris Lattnerb065b062011-06-20 04:01:31 +0000358 NewC = ConstantStruct::get(UserCS->getType(), NewOps);
Chris Lattnerea693df2008-08-21 02:34:16 +0000359 } else if (isa<ConstantVector>(UserC)) {
Chris Lattner2ca5c862011-02-15 00:14:00 +0000360 NewC = ConstantVector::get(NewOps);
Nick Lewyckycb337992009-05-10 20:57:05 +0000361 } else {
362 assert(isa<ConstantExpr>(UserC) && "Must be a ConstantExpr.");
Jay Foadb81e4572011-04-13 13:46:01 +0000363 NewC = cast<ConstantExpr>(UserC)->getWithOperands(NewOps);
Chris Lattnerea693df2008-08-21 02:34:16 +0000364 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000365
Chris Lattnerea693df2008-08-21 02:34:16 +0000366 UserC->replaceAllUsesWith(NewC);
367 UserC->destroyConstant();
368 NewOps.clear();
369 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000370
Nick Lewyckycb337992009-05-10 20:57:05 +0000371 // Update all ValueHandles, they should be the only users at this point.
372 Placeholder->replaceAllUsesWith(RealVal);
Chris Lattnerea693df2008-08-21 02:34:16 +0000373 delete Placeholder;
374 }
375}
376
Devang Pateld5ac4042009-08-04 06:00:18 +0000377void BitcodeReaderMDValueList::AssignValue(Value *V, unsigned Idx) {
378 if (Idx == size()) {
379 push_back(V);
380 return;
381 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000382
Devang Pateld5ac4042009-08-04 06:00:18 +0000383 if (Idx >= size())
384 resize(Idx+1);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000385
Devang Pateld5ac4042009-08-04 06:00:18 +0000386 WeakVH &OldV = MDValuePtrs[Idx];
387 if (OldV == 0) {
388 OldV = V;
389 return;
390 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000391
Devang Pateld5ac4042009-08-04 06:00:18 +0000392 // If there was a forward reference to this value, replace it.
Dan Gohman489b29b2010-08-20 22:02:26 +0000393 MDNode *PrevVal = cast<MDNode>(OldV);
Devang Pateld5ac4042009-08-04 06:00:18 +0000394 OldV->replaceAllUsesWith(V);
Dan Gohman489b29b2010-08-20 22:02:26 +0000395 MDNode::deleteTemporary(PrevVal);
Devang Patelc0ff8c82009-09-03 01:38:02 +0000396 // Deleting PrevVal sets Idx value in MDValuePtrs to null. Set new
397 // value for Idx.
398 MDValuePtrs[Idx] = V;
Devang Pateld5ac4042009-08-04 06:00:18 +0000399}
400
401Value *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) {
402 if (Idx >= size())
403 resize(Idx + 1);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000404
Devang Pateld5ac4042009-08-04 06:00:18 +0000405 if (Value *V = MDValuePtrs[Idx]) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000406 assert(V->getType()->isMetadataTy() && "Type mismatch in value table!");
Devang Pateld5ac4042009-08-04 06:00:18 +0000407 return V;
408 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000409
Devang Pateld5ac4042009-08-04 06:00:18 +0000410 // Create and return a placeholder, which will later be RAUW'd.
Dmitri Gribenko5c332db2013-05-05 00:40:33 +0000411 Value *V = MDNode::getTemporary(Context, None);
Devang Pateld5ac4042009-08-04 06:00:18 +0000412 MDValuePtrs[Idx] = V;
413 return V;
414}
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000415
Chris Lattner1afcace2011-07-09 17:41:24 +0000416Type *BitcodeReader::getTypeByID(unsigned ID) {
417 // The type table size is always specified correctly.
418 if (ID >= TypeList.size())
419 return 0;
Derek Schufffccf0622012-02-06 19:03:04 +0000420
Chris Lattner1afcace2011-07-09 17:41:24 +0000421 if (Type *Ty = TypeList[ID])
422 return Ty;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000423
Chris Lattner1afcace2011-07-09 17:41:24 +0000424 // If we have a forward reference, the only possible case is when it is to a
425 // named struct. Just create a placeholder for now.
Chris Lattner3ebb6492011-08-12 18:06:37 +0000426 return TypeList[ID] = StructType::create(Context);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000427}
428
Chris Lattner1afcace2011-07-09 17:41:24 +0000429
Chris Lattner48c85b82007-05-04 03:30:17 +0000430//===----------------------------------------------------------------------===//
431// Functions for parsing blocks from the bitcode file
432//===----------------------------------------------------------------------===//
433
Bill Wendlingf9271ea2013-02-04 23:32:23 +0000434
435/// \brief This fills an AttrBuilder object with the LLVM attributes that have
436/// been decoded from the given integer. This function must stay in sync with
437/// 'encodeLLVMAttributesForBitcode'.
438static void decodeLLVMAttributesForBitcode(AttrBuilder &B,
439 uint64_t EncodedAttrs) {
440 // FIXME: Remove in 4.0.
441
442 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
443 // the bits above 31 down by 11 bits.
444 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
445 assert((!Alignment || isPowerOf2_32(Alignment)) &&
446 "Alignment must be a power of two.");
447
448 if (Alignment)
449 B.addAlignmentAttr(Alignment);
Kostya Serebryanyab39afa2013-02-11 08:13:54 +0000450 B.addRawValue(((EncodedAttrs & (0xfffffULL << 32)) >> 11) |
Bill Wendlingf9271ea2013-02-04 23:32:23 +0000451 (EncodedAttrs & 0xffff));
452}
453
Rafael Espindolae076b532013-11-04 16:16:24 +0000454error_code BitcodeReader::ParseAttributeBlock() {
Chris Lattnere17b6582007-05-05 00:17:00 +0000455 if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +0000456 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000457
Devang Patel19c87462008-09-26 22:53:05 +0000458 if (!MAttributes.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +0000459 return Error(InvalidMultipleBlocks);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000460
Chris Lattner48c85b82007-05-04 03:30:17 +0000461 SmallVector<uint64_t, 64> Record;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000462
Bill Wendling0c2f0ff2013-01-27 00:36:48 +0000463 SmallVector<AttributeSet, 8> Attrs;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000464
Chris Lattner48c85b82007-05-04 03:30:17 +0000465 // Read all the records.
466 while (1) {
Chris Lattner5a4251c2013-01-20 02:13:19 +0000467 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbeyacb61942013-02-06 22:14:06 +0000468
Chris Lattner5a4251c2013-01-20 02:13:19 +0000469 switch (Entry.Kind) {
470 case BitstreamEntry::SubBlock: // Handled for us already.
471 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +0000472 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +0000473 case BitstreamEntry::EndBlock:
Rafael Espindolae076b532013-11-04 16:16:24 +0000474 return error_code::success();
Chris Lattner5a4251c2013-01-20 02:13:19 +0000475 case BitstreamEntry::Record:
476 // The interesting case.
477 break;
Chris Lattner48c85b82007-05-04 03:30:17 +0000478 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000479
Chris Lattner48c85b82007-05-04 03:30:17 +0000480 // Read a record.
481 Record.clear();
Chris Lattner5a4251c2013-01-20 02:13:19 +0000482 switch (Stream.readRecord(Entry.ID, Record)) {
Chris Lattner48c85b82007-05-04 03:30:17 +0000483 default: // Default behavior: ignore.
484 break;
Bill Wendlingf9271ea2013-02-04 23:32:23 +0000485 case bitc::PARAMATTR_CODE_ENTRY_OLD: { // ENTRY: [paramidx0, attr0, ...]
486 // FIXME: Remove in 4.0.
Chris Lattner48c85b82007-05-04 03:30:17 +0000487 if (Record.size() & 1)
Rafael Espindolae076b532013-11-04 16:16:24 +0000488 return Error(InvalidRecord);
Chris Lattner48c85b82007-05-04 03:30:17 +0000489
Chris Lattner48c85b82007-05-04 03:30:17 +0000490 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
Bill Wendling8232ece2013-01-29 01:43:29 +0000491 AttrBuilder B;
Bill Wendlingf9271ea2013-02-04 23:32:23 +0000492 decodeLLVMAttributesForBitcode(B, Record[i+1]);
Bill Wendling8232ece2013-01-29 01:43:29 +0000493 Attrs.push_back(AttributeSet::get(Context, Record[i], B));
Devang Patel19c87462008-09-26 22:53:05 +0000494 }
Devang Patel19c87462008-09-26 22:53:05 +0000495
Bill Wendling99faa3b2012-12-07 23:16:57 +0000496 MAttributes.push_back(AttributeSet::get(Context, Attrs));
Chris Lattner48c85b82007-05-04 03:30:17 +0000497 Attrs.clear();
498 break;
499 }
Bill Wendling48fbcfe2013-02-12 08:13:50 +0000500 case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [attrgrp0, attrgrp1, ...]
501 for (unsigned i = 0, e = Record.size(); i != e; ++i)
502 Attrs.push_back(MAttributeGroups[Record[i]]);
503
504 MAttributes.push_back(AttributeSet::get(Context, Attrs));
505 Attrs.clear();
506 break;
507 }
Duncan Sands5e41f652007-11-20 14:09:29 +0000508 }
Chris Lattner48c85b82007-05-04 03:30:17 +0000509 }
510}
511
Reid Kleckner161dd532013-11-12 01:31:00 +0000512// Returns Attribute::None on unrecognized codes.
513static Attribute::AttrKind GetAttrFromCode(uint64_t Code) {
514 switch (Code) {
515 default:
516 return Attribute::None;
517 case bitc::ATTR_KIND_ALIGNMENT:
518 return Attribute::Alignment;
519 case bitc::ATTR_KIND_ALWAYS_INLINE:
520 return Attribute::AlwaysInline;
521 case bitc::ATTR_KIND_BUILTIN:
522 return Attribute::Builtin;
523 case bitc::ATTR_KIND_BY_VAL:
524 return Attribute::ByVal;
525 case bitc::ATTR_KIND_COLD:
526 return Attribute::Cold;
527 case bitc::ATTR_KIND_INLINE_HINT:
528 return Attribute::InlineHint;
529 case bitc::ATTR_KIND_IN_REG:
530 return Attribute::InReg;
531 case bitc::ATTR_KIND_MIN_SIZE:
532 return Attribute::MinSize;
533 case bitc::ATTR_KIND_NAKED:
534 return Attribute::Naked;
535 case bitc::ATTR_KIND_NEST:
536 return Attribute::Nest;
537 case bitc::ATTR_KIND_NO_ALIAS:
538 return Attribute::NoAlias;
539 case bitc::ATTR_KIND_NO_BUILTIN:
540 return Attribute::NoBuiltin;
541 case bitc::ATTR_KIND_NO_CAPTURE:
542 return Attribute::NoCapture;
543 case bitc::ATTR_KIND_NO_DUPLICATE:
544 return Attribute::NoDuplicate;
545 case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT:
546 return Attribute::NoImplicitFloat;
547 case bitc::ATTR_KIND_NO_INLINE:
548 return Attribute::NoInline;
549 case bitc::ATTR_KIND_NON_LAZY_BIND:
550 return Attribute::NonLazyBind;
551 case bitc::ATTR_KIND_NO_RED_ZONE:
552 return Attribute::NoRedZone;
553 case bitc::ATTR_KIND_NO_RETURN:
554 return Attribute::NoReturn;
555 case bitc::ATTR_KIND_NO_UNWIND:
556 return Attribute::NoUnwind;
557 case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE:
558 return Attribute::OptimizeForSize;
559 case bitc::ATTR_KIND_OPTIMIZE_NONE:
560 return Attribute::OptimizeNone;
561 case bitc::ATTR_KIND_READ_NONE:
562 return Attribute::ReadNone;
563 case bitc::ATTR_KIND_READ_ONLY:
564 return Attribute::ReadOnly;
565 case bitc::ATTR_KIND_RETURNED:
566 return Attribute::Returned;
567 case bitc::ATTR_KIND_RETURNS_TWICE:
568 return Attribute::ReturnsTwice;
569 case bitc::ATTR_KIND_S_EXT:
570 return Attribute::SExt;
571 case bitc::ATTR_KIND_STACK_ALIGNMENT:
572 return Attribute::StackAlignment;
573 case bitc::ATTR_KIND_STACK_PROTECT:
574 return Attribute::StackProtect;
575 case bitc::ATTR_KIND_STACK_PROTECT_REQ:
576 return Attribute::StackProtectReq;
577 case bitc::ATTR_KIND_STACK_PROTECT_STRONG:
578 return Attribute::StackProtectStrong;
579 case bitc::ATTR_KIND_STRUCT_RET:
580 return Attribute::StructRet;
581 case bitc::ATTR_KIND_SANITIZE_ADDRESS:
582 return Attribute::SanitizeAddress;
583 case bitc::ATTR_KIND_SANITIZE_THREAD:
584 return Attribute::SanitizeThread;
585 case bitc::ATTR_KIND_SANITIZE_MEMORY:
586 return Attribute::SanitizeMemory;
587 case bitc::ATTR_KIND_UW_TABLE:
588 return Attribute::UWTable;
589 case bitc::ATTR_KIND_Z_EXT:
590 return Attribute::ZExt;
591 }
592}
593
Rafael Espindolae076b532013-11-04 16:16:24 +0000594error_code BitcodeReader::ParseAttrKind(uint64_t Code,
595 Attribute::AttrKind *Kind) {
Reid Kleckner161dd532013-11-12 01:31:00 +0000596 *Kind = GetAttrFromCode(Code);
597 if (*Kind == Attribute::None)
Rafael Espindolae076b532013-11-04 16:16:24 +0000598 return Error(InvalidValue);
Reid Kleckner161dd532013-11-12 01:31:00 +0000599 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000600}
601
Rafael Espindolae076b532013-11-04 16:16:24 +0000602error_code BitcodeReader::ParseAttributeGroupBlock() {
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000603 if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +0000604 return Error(InvalidRecord);
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000605
606 if (!MAttributeGroups.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +0000607 return Error(InvalidMultipleBlocks);
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000608
609 SmallVector<uint64_t, 64> Record;
610
611 // Read all the records.
612 while (1) {
613 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
614
615 switch (Entry.Kind) {
616 case BitstreamEntry::SubBlock: // Handled for us already.
617 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +0000618 return Error(MalformedBlock);
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000619 case BitstreamEntry::EndBlock:
Rafael Espindolae076b532013-11-04 16:16:24 +0000620 return error_code::success();
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000621 case BitstreamEntry::Record:
622 // The interesting case.
623 break;
624 }
625
626 // Read a record.
627 Record.clear();
628 switch (Stream.readRecord(Entry.ID, Record)) {
629 default: // Default behavior: ignore.
630 break;
631 case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...]
632 if (Record.size() < 3)
Rafael Espindolae076b532013-11-04 16:16:24 +0000633 return Error(InvalidRecord);
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000634
Bill Wendling04ef4be2013-02-11 22:32:29 +0000635 uint64_t GrpID = Record[0];
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000636 uint64_t Idx = Record[1]; // Index of the object this attribute refers to.
637
638 AttrBuilder B;
639 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
640 if (Record[i] == 0) { // Enum attribute
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000641 Attribute::AttrKind Kind;
Rafael Espindolae076b532013-11-04 16:16:24 +0000642 if (error_code EC = ParseAttrKind(Record[++i], &Kind))
643 return EC;
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000644
645 B.addAttribute(Kind);
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000646 } else if (Record[i] == 1) { // Align attribute
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000647 Attribute::AttrKind Kind;
Rafael Espindolae076b532013-11-04 16:16:24 +0000648 if (error_code EC = ParseAttrKind(Record[++i], &Kind))
649 return EC;
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000650 if (Kind == Attribute::Alignment)
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000651 B.addAlignmentAttr(Record[++i]);
652 else
653 B.addStackAlignmentAttr(Record[++i]);
654 } else { // String attribute
Bill Wendling04ef4be2013-02-11 22:32:29 +0000655 assert((Record[i] == 3 || Record[i] == 4) &&
656 "Invalid attribute group entry");
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000657 bool HasValue = (Record[i++] == 4);
658 SmallString<64> KindStr;
659 SmallString<64> ValStr;
660
661 while (Record[i] != 0 && i != e)
662 KindStr += Record[i++];
Bill Wendling04ef4be2013-02-11 22:32:29 +0000663 assert(Record[i] == 0 && "Kind string not null terminated");
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000664
665 if (HasValue) {
666 // Has a value associated with it.
Bill Wendling04ef4be2013-02-11 22:32:29 +0000667 ++i; // Skip the '0' that terminates the "kind" string.
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000668 while (Record[i] != 0 && i != e)
669 ValStr += Record[i++];
Bill Wendling04ef4be2013-02-11 22:32:29 +0000670 assert(Record[i] == 0 && "Value string not null terminated");
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000671 }
672
673 B.addAttribute(KindStr.str(), ValStr.str());
674 }
675 }
676
Bill Wendling04ef4be2013-02-11 22:32:29 +0000677 MAttributeGroups[GrpID] = AttributeSet::get(Context, Idx, B);
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000678 break;
679 }
680 }
681 }
682}
683
Rafael Espindolae076b532013-11-04 16:16:24 +0000684error_code BitcodeReader::ParseTypeTable() {
Chris Lattner1afcace2011-07-09 17:41:24 +0000685 if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW))
Rafael Espindolae076b532013-11-04 16:16:24 +0000686 return Error(InvalidRecord);
Derek Schufffccf0622012-02-06 19:03:04 +0000687
Chris Lattner1afcace2011-07-09 17:41:24 +0000688 return ParseTypeTableBody();
689}
Daniel Dunbara279bc32009-09-20 02:20:51 +0000690
Rafael Espindolae076b532013-11-04 16:16:24 +0000691error_code BitcodeReader::ParseTypeTableBody() {
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000692 if (!TypeList.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +0000693 return Error(InvalidMultipleBlocks);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000694
695 SmallVector<uint64_t, 64> Record;
696 unsigned NumRecords = 0;
697
Chris Lattner1afcace2011-07-09 17:41:24 +0000698 SmallString<64> TypeName;
Derek Schufffccf0622012-02-06 19:03:04 +0000699
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000700 // Read all the records for this type table.
701 while (1) {
Chris Lattner5a4251c2013-01-20 02:13:19 +0000702 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbeyacb61942013-02-06 22:14:06 +0000703
Chris Lattner5a4251c2013-01-20 02:13:19 +0000704 switch (Entry.Kind) {
705 case BitstreamEntry::SubBlock: // Handled for us already.
706 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +0000707 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +0000708 case BitstreamEntry::EndBlock:
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000709 if (NumRecords != TypeList.size())
Rafael Espindolae076b532013-11-04 16:16:24 +0000710 return Error(MalformedBlock);
711 return error_code::success();
Chris Lattner5a4251c2013-01-20 02:13:19 +0000712 case BitstreamEntry::Record:
713 // The interesting case.
714 break;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000715 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000716
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000717 // Read a record.
718 Record.clear();
Chris Lattner1afcace2011-07-09 17:41:24 +0000719 Type *ResultTy = 0;
Chris Lattner5a4251c2013-01-20 02:13:19 +0000720 switch (Stream.readRecord(Entry.ID, Record)) {
Rafael Espindolae076b532013-11-04 16:16:24 +0000721 default:
722 return Error(InvalidValue);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000723 case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
724 // TYPE_CODE_NUMENTRY contains a count of the number of types in the
725 // type list. This allows us to reserve space.
726 if (Record.size() < 1)
Rafael Espindolae076b532013-11-04 16:16:24 +0000727 return Error(InvalidRecord);
Chris Lattner1afcace2011-07-09 17:41:24 +0000728 TypeList.resize(Record[0]);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000729 continue;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000730 case bitc::TYPE_CODE_VOID: // VOID
Owen Anderson1d0be152009-08-13 21:58:54 +0000731 ResultTy = Type::getVoidTy(Context);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000732 break;
Dan Gohmance163392011-12-17 00:04:22 +0000733 case bitc::TYPE_CODE_HALF: // HALF
734 ResultTy = Type::getHalfTy(Context);
735 break;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000736 case bitc::TYPE_CODE_FLOAT: // FLOAT
Owen Anderson1d0be152009-08-13 21:58:54 +0000737 ResultTy = Type::getFloatTy(Context);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000738 break;
739 case bitc::TYPE_CODE_DOUBLE: // DOUBLE
Owen Anderson1d0be152009-08-13 21:58:54 +0000740 ResultTy = Type::getDoubleTy(Context);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000741 break;
Dale Johannesen320fc8a2007-08-03 01:03:46 +0000742 case bitc::TYPE_CODE_X86_FP80: // X86_FP80
Owen Anderson1d0be152009-08-13 21:58:54 +0000743 ResultTy = Type::getX86_FP80Ty(Context);
Dale Johannesen320fc8a2007-08-03 01:03:46 +0000744 break;
745 case bitc::TYPE_CODE_FP128: // FP128
Owen Anderson1d0be152009-08-13 21:58:54 +0000746 ResultTy = Type::getFP128Ty(Context);
Dale Johannesen320fc8a2007-08-03 01:03:46 +0000747 break;
748 case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128
Owen Anderson1d0be152009-08-13 21:58:54 +0000749 ResultTy = Type::getPPC_FP128Ty(Context);
Dale Johannesen320fc8a2007-08-03 01:03:46 +0000750 break;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000751 case bitc::TYPE_CODE_LABEL: // LABEL
Owen Anderson1d0be152009-08-13 21:58:54 +0000752 ResultTy = Type::getLabelTy(Context);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000753 break;
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000754 case bitc::TYPE_CODE_METADATA: // METADATA
Owen Anderson1d0be152009-08-13 21:58:54 +0000755 ResultTy = Type::getMetadataTy(Context);
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000756 break;
Dale Johannesenbb811a22010-09-10 20:55:01 +0000757 case bitc::TYPE_CODE_X86_MMX: // X86_MMX
758 ResultTy = Type::getX86_MMXTy(Context);
759 break;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000760 case bitc::TYPE_CODE_INTEGER: // INTEGER: [width]
761 if (Record.size() < 1)
Rafael Espindolae076b532013-11-04 16:16:24 +0000762 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000763
Owen Anderson1d0be152009-08-13 21:58:54 +0000764 ResultTy = IntegerType::get(Context, Record[0]);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000765 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000766 case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or
Christopher Lambfe63fb92007-12-11 08:59:05 +0000767 // [pointee type, address space]
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000768 if (Record.size() < 1)
Rafael Espindolae076b532013-11-04 16:16:24 +0000769 return Error(InvalidRecord);
Christopher Lambfe63fb92007-12-11 08:59:05 +0000770 unsigned AddressSpace = 0;
771 if (Record.size() == 2)
772 AddressSpace = Record[1];
Chris Lattner1afcace2011-07-09 17:41:24 +0000773 ResultTy = getTypeByID(Record[0]);
Rafael Espindolae076b532013-11-04 16:16:24 +0000774 if (ResultTy == 0)
775 return Error(InvalidType);
Chris Lattner1afcace2011-07-09 17:41:24 +0000776 ResultTy = PointerType::get(ResultTy, AddressSpace);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000777 break;
Christopher Lambfe63fb92007-12-11 08:59:05 +0000778 }
Nuno Lopesee8100d2012-05-23 15:19:39 +0000779 case bitc::TYPE_CODE_FUNCTION_OLD: {
780 // FIXME: attrid is dead, remove it in LLVM 4.0
781 // FUNCTION: [vararg, attrid, retty, paramty x N]
782 if (Record.size() < 3)
Rafael Espindolae076b532013-11-04 16:16:24 +0000783 return Error(InvalidRecord);
Nuno Lopesee8100d2012-05-23 15:19:39 +0000784 SmallVector<Type*, 8> ArgTys;
785 for (unsigned i = 3, e = Record.size(); i != e; ++i) {
786 if (Type *T = getTypeByID(Record[i]))
787 ArgTys.push_back(T);
788 else
789 break;
790 }
Michael Ilseman407a6162012-11-15 22:34:00 +0000791
Nuno Lopesee8100d2012-05-23 15:19:39 +0000792 ResultTy = getTypeByID(Record[2]);
793 if (ResultTy == 0 || ArgTys.size() < Record.size()-3)
Rafael Espindolae076b532013-11-04 16:16:24 +0000794 return Error(InvalidType);
Nuno Lopesee8100d2012-05-23 15:19:39 +0000795
796 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
797 break;
798 }
Chad Rosiercde54642011-11-03 00:14:01 +0000799 case bitc::TYPE_CODE_FUNCTION: {
800 // FUNCTION: [vararg, retty, paramty x N]
801 if (Record.size() < 2)
Rafael Espindolae076b532013-11-04 16:16:24 +0000802 return Error(InvalidRecord);
Chris Lattnerd629efa2012-01-27 03:15:49 +0000803 SmallVector<Type*, 8> ArgTys;
Chad Rosiercde54642011-11-03 00:14:01 +0000804 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
805 if (Type *T = getTypeByID(Record[i]))
806 ArgTys.push_back(T);
807 else
808 break;
809 }
Michael Ilseman407a6162012-11-15 22:34:00 +0000810
Chad Rosiercde54642011-11-03 00:14:01 +0000811 ResultTy = getTypeByID(Record[1]);
812 if (ResultTy == 0 || ArgTys.size() < Record.size()-2)
Rafael Espindolae076b532013-11-04 16:16:24 +0000813 return Error(InvalidType);
Chad Rosiercde54642011-11-03 00:14:01 +0000814
815 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
816 break;
817 }
Chris Lattner1afcace2011-07-09 17:41:24 +0000818 case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N]
Chris Lattner7108dce2007-05-06 08:21:50 +0000819 if (Record.size() < 1)
Rafael Espindolae076b532013-11-04 16:16:24 +0000820 return Error(InvalidRecord);
Chris Lattnerd629efa2012-01-27 03:15:49 +0000821 SmallVector<Type*, 8> EltTys;
Chris Lattner1afcace2011-07-09 17:41:24 +0000822 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
823 if (Type *T = getTypeByID(Record[i]))
824 EltTys.push_back(T);
825 else
826 break;
827 }
828 if (EltTys.size() != Record.size()-1)
Rafael Espindolae076b532013-11-04 16:16:24 +0000829 return Error(InvalidType);
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000830 ResultTy = StructType::get(Context, EltTys, Record[0]);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000831 break;
832 }
Chris Lattner1afcace2011-07-09 17:41:24 +0000833 case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N]
834 if (ConvertToString(Record, 0, TypeName))
Rafael Espindolae076b532013-11-04 16:16:24 +0000835 return Error(InvalidRecord);
Chris Lattner1afcace2011-07-09 17:41:24 +0000836 continue;
837
838 case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N]
839 if (Record.size() < 1)
Rafael Espindolae076b532013-11-04 16:16:24 +0000840 return Error(InvalidRecord);
Michael Ilseman407a6162012-11-15 22:34:00 +0000841
Chris Lattner1afcace2011-07-09 17:41:24 +0000842 if (NumRecords >= TypeList.size())
Rafael Espindolae076b532013-11-04 16:16:24 +0000843 return Error(InvalidTYPETable);
Michael Ilseman407a6162012-11-15 22:34:00 +0000844
Chris Lattner1afcace2011-07-09 17:41:24 +0000845 // Check to see if this was forward referenced, if so fill in the temp.
846 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
847 if (Res) {
848 Res->setName(TypeName);
849 TypeList[NumRecords] = 0;
850 } else // Otherwise, create a new struct.
Chris Lattner3ebb6492011-08-12 18:06:37 +0000851 Res = StructType::create(Context, TypeName);
Chris Lattner1afcace2011-07-09 17:41:24 +0000852 TypeName.clear();
Michael Ilseman407a6162012-11-15 22:34:00 +0000853
Chris Lattner1afcace2011-07-09 17:41:24 +0000854 SmallVector<Type*, 8> EltTys;
855 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
856 if (Type *T = getTypeByID(Record[i]))
857 EltTys.push_back(T);
858 else
859 break;
860 }
861 if (EltTys.size() != Record.size()-1)
Rafael Espindolae076b532013-11-04 16:16:24 +0000862 return Error(InvalidRecord);
Chris Lattner1afcace2011-07-09 17:41:24 +0000863 Res->setBody(EltTys, Record[0]);
864 ResultTy = Res;
865 break;
866 }
867 case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: []
868 if (Record.size() != 1)
Rafael Espindolae076b532013-11-04 16:16:24 +0000869 return Error(InvalidRecord);
Chris Lattner1afcace2011-07-09 17:41:24 +0000870
871 if (NumRecords >= TypeList.size())
Rafael Espindolae076b532013-11-04 16:16:24 +0000872 return Error(InvalidTYPETable);
Michael Ilseman407a6162012-11-15 22:34:00 +0000873
Chris Lattner1afcace2011-07-09 17:41:24 +0000874 // Check to see if this was forward referenced, if so fill in the temp.
875 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
876 if (Res) {
877 Res->setName(TypeName);
878 TypeList[NumRecords] = 0;
879 } else // Otherwise, create a new struct with no body.
Chris Lattner3ebb6492011-08-12 18:06:37 +0000880 Res = StructType::create(Context, TypeName);
Chris Lattner1afcace2011-07-09 17:41:24 +0000881 TypeName.clear();
882 ResultTy = Res;
883 break;
Michael Ilseman407a6162012-11-15 22:34:00 +0000884 }
Chris Lattner1afcace2011-07-09 17:41:24 +0000885 case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty]
886 if (Record.size() < 2)
Rafael Espindolae076b532013-11-04 16:16:24 +0000887 return Error(InvalidRecord);
Chris Lattner1afcace2011-07-09 17:41:24 +0000888 if ((ResultTy = getTypeByID(Record[1])))
889 ResultTy = ArrayType::get(ResultTy, Record[0]);
890 else
Rafael Espindolae076b532013-11-04 16:16:24 +0000891 return Error(InvalidType);
Chris Lattner1afcace2011-07-09 17:41:24 +0000892 break;
893 case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty]
894 if (Record.size() < 2)
Rafael Espindolae076b532013-11-04 16:16:24 +0000895 return Error(InvalidRecord);
Chris Lattner1afcace2011-07-09 17:41:24 +0000896 if ((ResultTy = getTypeByID(Record[1])))
897 ResultTy = VectorType::get(ResultTy, Record[0]);
898 else
Rafael Espindolae076b532013-11-04 16:16:24 +0000899 return Error(InvalidType);
Chris Lattner1afcace2011-07-09 17:41:24 +0000900 break;
901 }
902
903 if (NumRecords >= TypeList.size())
Rafael Espindolae076b532013-11-04 16:16:24 +0000904 return Error(InvalidTYPETable);
Chris Lattner1afcace2011-07-09 17:41:24 +0000905 assert(ResultTy && "Didn't read a type?");
906 assert(TypeList[NumRecords] == 0 && "Already read type?");
907 TypeList[NumRecords++] = ResultTy;
908 }
909}
910
Rafael Espindolae076b532013-11-04 16:16:24 +0000911error_code BitcodeReader::ParseValueSymbolTable() {
Chris Lattnere17b6582007-05-05 00:17:00 +0000912 if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +0000913 return Error(InvalidRecord);
Chris Lattner0b2482a2007-04-23 21:26:05 +0000914
915 SmallVector<uint64_t, 64> Record;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000916
Chris Lattner0b2482a2007-04-23 21:26:05 +0000917 // Read all the records for this value table.
918 SmallString<128> ValueName;
919 while (1) {
Chris Lattner5a4251c2013-01-20 02:13:19 +0000920 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbeyacb61942013-02-06 22:14:06 +0000921
Chris Lattner5a4251c2013-01-20 02:13:19 +0000922 switch (Entry.Kind) {
923 case BitstreamEntry::SubBlock: // Handled for us already.
924 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +0000925 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +0000926 case BitstreamEntry::EndBlock:
Rafael Espindolae076b532013-11-04 16:16:24 +0000927 return error_code::success();
Chris Lattner5a4251c2013-01-20 02:13:19 +0000928 case BitstreamEntry::Record:
929 // The interesting case.
930 break;
Chris Lattner0b2482a2007-04-23 21:26:05 +0000931 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000932
Chris Lattner0b2482a2007-04-23 21:26:05 +0000933 // Read a record.
934 Record.clear();
Chris Lattner5a4251c2013-01-20 02:13:19 +0000935 switch (Stream.readRecord(Entry.ID, Record)) {
Chris Lattner0b2482a2007-04-23 21:26:05 +0000936 default: // Default behavior: unknown type.
937 break;
Chris Lattner15e6d172007-05-04 19:11:41 +0000938 case bitc::VST_CODE_ENTRY: { // VST_ENTRY: [valueid, namechar x N]
Chris Lattner0b2482a2007-04-23 21:26:05 +0000939 if (ConvertToString(Record, 1, ValueName))
Rafael Espindolae076b532013-11-04 16:16:24 +0000940 return Error(InvalidRecord);
Chris Lattner0b2482a2007-04-23 21:26:05 +0000941 unsigned ValueID = Record[0];
942 if (ValueID >= ValueList.size())
Rafael Espindolae076b532013-11-04 16:16:24 +0000943 return Error(InvalidRecord);
Chris Lattner0b2482a2007-04-23 21:26:05 +0000944 Value *V = ValueList[ValueID];
Daniel Dunbara279bc32009-09-20 02:20:51 +0000945
Daniel Dunbar3f53fa92009-07-26 00:34:27 +0000946 V->setName(StringRef(ValueName.data(), ValueName.size()));
Chris Lattner0b2482a2007-04-23 21:26:05 +0000947 ValueName.clear();
948 break;
Reid Spencerc8f8a242007-05-04 01:43:33 +0000949 }
Bill Wendling5d7a5a42011-04-10 23:18:04 +0000950 case bitc::VST_CODE_BBENTRY: {
Chris Lattnere825ed52007-05-03 22:18:21 +0000951 if (ConvertToString(Record, 1, ValueName))
Rafael Espindolae076b532013-11-04 16:16:24 +0000952 return Error(InvalidRecord);
Chris Lattnere825ed52007-05-03 22:18:21 +0000953 BasicBlock *BB = getBasicBlock(Record[0]);
954 if (BB == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +0000955 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000956
Daniel Dunbar3f53fa92009-07-26 00:34:27 +0000957 BB->setName(StringRef(ValueName.data(), ValueName.size()));
Chris Lattnere825ed52007-05-03 22:18:21 +0000958 ValueName.clear();
959 break;
Chris Lattner0b2482a2007-04-23 21:26:05 +0000960 }
Reid Spencerc8f8a242007-05-04 01:43:33 +0000961 }
Chris Lattner0b2482a2007-04-23 21:26:05 +0000962 }
963}
964
Rafael Espindolae076b532013-11-04 16:16:24 +0000965error_code BitcodeReader::ParseMetadata() {
Devang Patel23598502010-01-11 18:52:33 +0000966 unsigned NextMDValueNo = MDValueList.size();
Devang Patele54abc92009-07-22 17:43:22 +0000967
968 if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +0000969 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000970
Devang Patele54abc92009-07-22 17:43:22 +0000971 SmallVector<uint64_t, 64> Record;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000972
Devang Patele54abc92009-07-22 17:43:22 +0000973 // Read all the records.
974 while (1) {
Chris Lattner5a4251c2013-01-20 02:13:19 +0000975 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbeyacb61942013-02-06 22:14:06 +0000976
Chris Lattner5a4251c2013-01-20 02:13:19 +0000977 switch (Entry.Kind) {
978 case BitstreamEntry::SubBlock: // Handled for us already.
979 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +0000980 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +0000981 case BitstreamEntry::EndBlock:
Rafael Espindolae076b532013-11-04 16:16:24 +0000982 return error_code::success();
Chris Lattner5a4251c2013-01-20 02:13:19 +0000983 case BitstreamEntry::Record:
984 // The interesting case.
985 break;
Devang Patele54abc92009-07-22 17:43:22 +0000986 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000987
Victor Hernandez24e64df2010-01-10 07:14:18 +0000988 bool IsFunctionLocal = false;
Devang Patele54abc92009-07-22 17:43:22 +0000989 // Read a record.
990 Record.clear();
Chris Lattner5a4251c2013-01-20 02:13:19 +0000991 unsigned Code = Stream.readRecord(Entry.ID, Record);
Dan Gohman9b10dfb2010-09-13 18:00:48 +0000992 switch (Code) {
Devang Patele54abc92009-07-22 17:43:22 +0000993 default: // Default behavior: ignore.
994 break;
Devang Patelaa993142009-07-29 22:34:41 +0000995 case bitc::METADATA_NAME: {
Chris Lattner1ca114a2013-01-20 02:54:05 +0000996 // Read name of the named metadata.
Benjamin Kramerf52aea82012-05-28 14:10:31 +0000997 SmallString<8> Name(Record.begin(), Record.end());
Devang Patelaa993142009-07-29 22:34:41 +0000998 Record.clear();
999 Code = Stream.ReadCode();
1000
Chris Lattner9d61dd92011-06-17 17:50:30 +00001001 // METADATA_NAME is always followed by METADATA_NAMED_NODE.
Chris Lattner5a4251c2013-01-20 02:13:19 +00001002 unsigned NextBitCode = Stream.readRecord(Code, Record);
Chris Lattner9d61dd92011-06-17 17:50:30 +00001003 assert(NextBitCode == bitc::METADATA_NAMED_NODE); (void)NextBitCode;
Devang Patelaa993142009-07-29 22:34:41 +00001004
1005 // Read named metadata elements.
1006 unsigned Size = Record.size();
Dan Gohman17aa92c2010-07-21 23:38:33 +00001007 NamedMDNode *NMD = TheModule->getOrInsertNamedMetadata(Name);
Devang Patelaa993142009-07-29 22:34:41 +00001008 for (unsigned i = 0; i != Size; ++i) {
Chris Lattner70644e92010-01-09 02:02:37 +00001009 MDNode *MD = dyn_cast<MDNode>(MDValueList.getValueFwdRef(Record[i]));
1010 if (MD == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00001011 return Error(InvalidRecord);
Dan Gohman17aa92c2010-07-21 23:38:33 +00001012 NMD->addOperand(MD);
Devang Patelaa993142009-07-29 22:34:41 +00001013 }
Devang Patelaa993142009-07-29 22:34:41 +00001014 break;
1015 }
Chris Lattner9d61dd92011-06-17 17:50:30 +00001016 case bitc::METADATA_FN_NODE:
Victor Hernandez24e64df2010-01-10 07:14:18 +00001017 IsFunctionLocal = true;
1018 // fall-through
Chris Lattner9d61dd92011-06-17 17:50:30 +00001019 case bitc::METADATA_NODE: {
Dan Gohmanac809752010-07-13 19:33:27 +00001020 if (Record.size() % 2 == 1)
Rafael Espindolae076b532013-11-04 16:16:24 +00001021 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001022
Devang Patel104cf9e2009-07-23 01:07:34 +00001023 unsigned Size = Record.size();
1024 SmallVector<Value*, 8> Elts;
1025 for (unsigned i = 0; i != Size; i += 2) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001026 Type *Ty = getTypeByID(Record[i]);
Rafael Espindolae076b532013-11-04 16:16:24 +00001027 if (!Ty)
1028 return Error(InvalidRecord);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001029 if (Ty->isMetadataTy())
Devang Pateld5ac4042009-08-04 06:00:18 +00001030 Elts.push_back(MDValueList.getValueFwdRef(Record[i+1]));
Benjamin Kramerf0127052010-01-05 13:12:22 +00001031 else if (!Ty->isVoidTy())
Devang Patel104cf9e2009-07-23 01:07:34 +00001032 Elts.push_back(ValueList.getValueFwdRef(Record[i+1], Ty));
1033 else
1034 Elts.push_back(NULL);
1035 }
Jay Foadec9186b2011-04-21 19:59:31 +00001036 Value *V = MDNode::getWhenValsUnresolved(Context, Elts, IsFunctionLocal);
Victor Hernandez24e64df2010-01-10 07:14:18 +00001037 IsFunctionLocal = false;
Devang Patel23598502010-01-11 18:52:33 +00001038 MDValueList.AssignValue(V, NextMDValueNo++);
Devang Patel104cf9e2009-07-23 01:07:34 +00001039 break;
1040 }
Devang Patele54abc92009-07-22 17:43:22 +00001041 case bitc::METADATA_STRING: {
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001042 SmallString<8> String(Record.begin(), Record.end());
1043 Value *V = MDString::get(Context, String);
Devang Patel23598502010-01-11 18:52:33 +00001044 MDValueList.AssignValue(V, NextMDValueNo++);
Devang Patele54abc92009-07-22 17:43:22 +00001045 break;
1046 }
Devang Patele8e02132009-09-18 19:26:43 +00001047 case bitc::METADATA_KIND: {
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001048 if (Record.size() < 2)
Rafael Espindolae076b532013-11-04 16:16:24 +00001049 return Error(InvalidRecord);
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001050
Devang Patela2148402009-09-28 21:14:55 +00001051 unsigned Kind = Record[0];
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001052 SmallString<8> Name(Record.begin()+1, Record.end());
1053
Chris Lattner08113472009-12-29 09:01:33 +00001054 unsigned NewKind = TheModule->getMDKindID(Name.str());
Dan Gohman19538d12010-07-20 21:42:28 +00001055 if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second)
Rafael Espindolae076b532013-11-04 16:16:24 +00001056 return Error(ConflictingMETADATA_KINDRecords);
Devang Patele8e02132009-09-18 19:26:43 +00001057 break;
1058 }
Devang Patele54abc92009-07-22 17:43:22 +00001059 }
1060 }
1061}
1062
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00001063/// decodeSignRotatedValue - Decode a signed value stored with the sign bit in
Chris Lattner0eef0802007-04-24 04:04:35 +00001064/// the LSB for dense VBR encoding.
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00001065uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) {
Chris Lattner0eef0802007-04-24 04:04:35 +00001066 if ((V & 1) == 0)
1067 return V >> 1;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001068 if (V != 1)
Chris Lattner0eef0802007-04-24 04:04:35 +00001069 return -(V >> 1);
1070 // There is no such thing as -0 with integers. "-0" really means MININT.
1071 return 1ULL << 63;
1072}
1073
Chris Lattner07d98b42007-04-26 02:46:40 +00001074/// ResolveGlobalAndAliasInits - Resolve all of the initializers for global
1075/// values and aliases that we can.
Rafael Espindolae076b532013-11-04 16:16:24 +00001076error_code BitcodeReader::ResolveGlobalAndAliasInits() {
Chris Lattner07d98b42007-04-26 02:46:40 +00001077 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist;
1078 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist;
Peter Collingbourne1e3037f2013-09-16 01:08:15 +00001079 std::vector<std::pair<Function*, unsigned> > FunctionPrefixWorklist;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001080
Chris Lattner07d98b42007-04-26 02:46:40 +00001081 GlobalInitWorklist.swap(GlobalInits);
1082 AliasInitWorklist.swap(AliasInits);
Peter Collingbourne1e3037f2013-09-16 01:08:15 +00001083 FunctionPrefixWorklist.swap(FunctionPrefixes);
Chris Lattner07d98b42007-04-26 02:46:40 +00001084
1085 while (!GlobalInitWorklist.empty()) {
Chris Lattner198f34a2007-04-26 03:27:58 +00001086 unsigned ValID = GlobalInitWorklist.back().second;
Chris Lattner07d98b42007-04-26 02:46:40 +00001087 if (ValID >= ValueList.size()) {
1088 // Not ready to resolve this yet, it requires something later in the file.
Chris Lattner198f34a2007-04-26 03:27:58 +00001089 GlobalInits.push_back(GlobalInitWorklist.back());
Chris Lattner07d98b42007-04-26 02:46:40 +00001090 } else {
1091 if (Constant *C = dyn_cast<Constant>(ValueList[ValID]))
1092 GlobalInitWorklist.back().first->setInitializer(C);
1093 else
Rafael Espindolae076b532013-11-04 16:16:24 +00001094 return Error(ExpectedConstant);
Chris Lattner07d98b42007-04-26 02:46:40 +00001095 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001096 GlobalInitWorklist.pop_back();
Chris Lattner07d98b42007-04-26 02:46:40 +00001097 }
1098
1099 while (!AliasInitWorklist.empty()) {
1100 unsigned ValID = AliasInitWorklist.back().second;
1101 if (ValID >= ValueList.size()) {
1102 AliasInits.push_back(AliasInitWorklist.back());
1103 } else {
1104 if (Constant *C = dyn_cast<Constant>(ValueList[ValID]))
Anton Korobeynikov7dde0ff2007-04-28 14:57:59 +00001105 AliasInitWorklist.back().first->setAliasee(C);
Chris Lattner07d98b42007-04-26 02:46:40 +00001106 else
Rafael Espindolae076b532013-11-04 16:16:24 +00001107 return Error(ExpectedConstant);
Chris Lattner07d98b42007-04-26 02:46:40 +00001108 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001109 AliasInitWorklist.pop_back();
Chris Lattner07d98b42007-04-26 02:46:40 +00001110 }
Peter Collingbourne1e3037f2013-09-16 01:08:15 +00001111
1112 while (!FunctionPrefixWorklist.empty()) {
1113 unsigned ValID = FunctionPrefixWorklist.back().second;
1114 if (ValID >= ValueList.size()) {
1115 FunctionPrefixes.push_back(FunctionPrefixWorklist.back());
1116 } else {
1117 if (Constant *C = dyn_cast<Constant>(ValueList[ValID]))
1118 FunctionPrefixWorklist.back().first->setPrefixData(C);
1119 else
Rafael Espindolae076b532013-11-04 16:16:24 +00001120 return Error(ExpectedConstant);
Peter Collingbourne1e3037f2013-09-16 01:08:15 +00001121 }
1122 FunctionPrefixWorklist.pop_back();
1123 }
1124
Rafael Espindolae076b532013-11-04 16:16:24 +00001125 return error_code::success();
Chris Lattner07d98b42007-04-26 02:46:40 +00001126}
1127
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001128static APInt ReadWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) {
1129 SmallVector<uint64_t, 8> Words(Vals.size());
1130 std::transform(Vals.begin(), Vals.end(), Words.begin(),
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00001131 BitcodeReader::decodeSignRotatedValue);
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001132
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00001133 return APInt(TypeBits, Words);
1134}
1135
Rafael Espindolae076b532013-11-04 16:16:24 +00001136error_code BitcodeReader::ParseConstants() {
Chris Lattnere17b6582007-05-05 00:17:00 +00001137 if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +00001138 return Error(InvalidRecord);
Chris Lattnere16504e2007-04-24 03:30:34 +00001139
1140 SmallVector<uint64_t, 64> Record;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001141
Chris Lattnere16504e2007-04-24 03:30:34 +00001142 // Read all the records for this value table.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001143 Type *CurTy = Type::getInt32Ty(Context);
Chris Lattner522b7b12007-04-24 05:48:56 +00001144 unsigned NextCstNo = ValueList.size();
Chris Lattnere16504e2007-04-24 03:30:34 +00001145 while (1) {
Chris Lattner5a4251c2013-01-20 02:13:19 +00001146 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbeyacb61942013-02-06 22:14:06 +00001147
Chris Lattner5a4251c2013-01-20 02:13:19 +00001148 switch (Entry.Kind) {
1149 case BitstreamEntry::SubBlock: // Handled for us already.
1150 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +00001151 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00001152 case BitstreamEntry::EndBlock:
1153 if (NextCstNo != ValueList.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001154 return Error(InvalidConstantReference);
Joe Abbeyacb61942013-02-06 22:14:06 +00001155
Chris Lattner5a4251c2013-01-20 02:13:19 +00001156 // Once all the constants have been read, go through and resolve forward
1157 // references.
1158 ValueList.ResolveConstantForwardRefs();
Rafael Espindolae076b532013-11-04 16:16:24 +00001159 return error_code::success();
Chris Lattner5a4251c2013-01-20 02:13:19 +00001160 case BitstreamEntry::Record:
1161 // The interesting case.
Chris Lattnerea693df2008-08-21 02:34:16 +00001162 break;
Chris Lattnere16504e2007-04-24 03:30:34 +00001163 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001164
Chris Lattnere16504e2007-04-24 03:30:34 +00001165 // Read a record.
1166 Record.clear();
1167 Value *V = 0;
Chris Lattner5a4251c2013-01-20 02:13:19 +00001168 unsigned BitCode = Stream.readRecord(Entry.ID, Record);
Dan Gohman1224c382009-07-20 21:19:07 +00001169 switch (BitCode) {
Chris Lattnere16504e2007-04-24 03:30:34 +00001170 default: // Default behavior: unknown constant
1171 case bitc::CST_CODE_UNDEF: // UNDEF
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001172 V = UndefValue::get(CurTy);
Chris Lattnere16504e2007-04-24 03:30:34 +00001173 break;
1174 case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid]
1175 if (Record.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +00001176 return Error(InvalidRecord);
Chris Lattnere16504e2007-04-24 03:30:34 +00001177 if (Record[0] >= TypeList.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001178 return Error(InvalidRecord);
Chris Lattnere16504e2007-04-24 03:30:34 +00001179 CurTy = TypeList[Record[0]];
Chris Lattner0eef0802007-04-24 04:04:35 +00001180 continue; // Skip the ValueList manipulation.
Chris Lattnere16504e2007-04-24 03:30:34 +00001181 case bitc::CST_CODE_NULL: // NULL
Owen Andersona7235ea2009-07-31 20:28:14 +00001182 V = Constant::getNullValue(CurTy);
Chris Lattnere16504e2007-04-24 03:30:34 +00001183 break;
1184 case bitc::CST_CODE_INTEGER: // INTEGER: [intval]
Duncan Sands1df98592010-02-16 11:11:14 +00001185 if (!CurTy->isIntegerTy() || Record.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +00001186 return Error(InvalidRecord);
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00001187 V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0]));
Chris Lattner0eef0802007-04-24 04:04:35 +00001188 break;
Chris Lattner15e6d172007-05-04 19:11:41 +00001189 case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
Duncan Sands1df98592010-02-16 11:11:14 +00001190 if (!CurTy->isIntegerTy() || Record.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +00001191 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001192
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001193 APInt VInt = ReadWideAPInt(Record,
1194 cast<IntegerType>(CurTy)->getBitWidth());
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00001195 V = ConstantInt::get(Context, VInt);
Michael Ilseman407a6162012-11-15 22:34:00 +00001196
Chris Lattner0eef0802007-04-24 04:04:35 +00001197 break;
1198 }
Dale Johannesen3f6eb742007-09-11 18:32:33 +00001199 case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval]
Chris Lattner0eef0802007-04-24 04:04:35 +00001200 if (Record.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +00001201 return Error(InvalidRecord);
Dan Gohmance163392011-12-17 00:04:22 +00001202 if (CurTy->isHalfTy())
Tim Northover0a29cb02013-01-22 09:46:31 +00001203 V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf,
1204 APInt(16, (uint16_t)Record[0])));
Dan Gohmance163392011-12-17 00:04:22 +00001205 else if (CurTy->isFloatTy())
Tim Northover0a29cb02013-01-22 09:46:31 +00001206 V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle,
1207 APInt(32, (uint32_t)Record[0])));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001208 else if (CurTy->isDoubleTy())
Tim Northover0a29cb02013-01-22 09:46:31 +00001209 V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble,
1210 APInt(64, Record[0])));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001211 else if (CurTy->isX86_FP80Ty()) {
Dale Johannesen1b25cb22009-03-23 21:16:53 +00001212 // Bits are not stored the same way as a normal i80 APInt, compensate.
1213 uint64_t Rearrange[2];
1214 Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);
1215 Rearrange[1] = Record[0] >> 48;
Tim Northover0a29cb02013-01-22 09:46:31 +00001216 V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended,
1217 APInt(80, Rearrange)));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001218 } else if (CurTy->isFP128Ty())
Tim Northover0a29cb02013-01-22 09:46:31 +00001219 V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad,
1220 APInt(128, Record)));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001221 else if (CurTy->isPPC_FP128Ty())
Tim Northover0a29cb02013-01-22 09:46:31 +00001222 V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble,
1223 APInt(128, Record)));
Chris Lattnere16504e2007-04-24 03:30:34 +00001224 else
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001225 V = UndefValue::get(CurTy);
Chris Lattnere16504e2007-04-24 03:30:34 +00001226 break;
Dale Johannesen3f6eb742007-09-11 18:32:33 +00001227 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001228
Chris Lattner15e6d172007-05-04 19:11:41 +00001229 case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]
1230 if (Record.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +00001231 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001232
Chris Lattner15e6d172007-05-04 19:11:41 +00001233 unsigned Size = Record.size();
Chris Lattnerd629efa2012-01-27 03:15:49 +00001234 SmallVector<Constant*, 16> Elts;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001235
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001236 if (StructType *STy = dyn_cast<StructType>(CurTy)) {
Chris Lattner522b7b12007-04-24 05:48:56 +00001237 for (unsigned i = 0; i != Size; ++i)
Chris Lattner15e6d172007-05-04 19:11:41 +00001238 Elts.push_back(ValueList.getConstantFwdRef(Record[i],
Chris Lattner522b7b12007-04-24 05:48:56 +00001239 STy->getElementType(i)));
Owen Anderson8fa33382009-07-27 22:29:26 +00001240 V = ConstantStruct::get(STy, Elts);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001241 } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) {
1242 Type *EltTy = ATy->getElementType();
Chris Lattner522b7b12007-04-24 05:48:56 +00001243 for (unsigned i = 0; i != Size; ++i)
Chris Lattner15e6d172007-05-04 19:11:41 +00001244 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
Owen Anderson1fd70962009-07-28 18:32:17 +00001245 V = ConstantArray::get(ATy, Elts);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001246 } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) {
1247 Type *EltTy = VTy->getElementType();
Chris Lattner522b7b12007-04-24 05:48:56 +00001248 for (unsigned i = 0; i != Size; ++i)
Chris Lattner15e6d172007-05-04 19:11:41 +00001249 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00001250 V = ConstantVector::get(Elts);
Chris Lattner522b7b12007-04-24 05:48:56 +00001251 } else {
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001252 V = UndefValue::get(CurTy);
Chris Lattner522b7b12007-04-24 05:48:56 +00001253 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001254 break;
1255 }
Chris Lattner2237f842012-02-05 02:41:35 +00001256 case bitc::CST_CODE_STRING: // STRING: [values]
Chris Lattnercb3d91b2007-05-06 00:53:07 +00001257 case bitc::CST_CODE_CSTRING: { // CSTRING: [values]
1258 if (Record.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +00001259 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001260
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001261 SmallString<16> Elts(Record.begin(), Record.end());
Chris Lattner2237f842012-02-05 02:41:35 +00001262 V = ConstantDataArray::getString(Context, Elts,
1263 BitCode == bitc::CST_CODE_CSTRING);
Chris Lattnercb3d91b2007-05-06 00:53:07 +00001264 break;
1265 }
Chris Lattnerd408f062012-01-30 00:51:16 +00001266 case bitc::CST_CODE_DATA: {// DATA: [n x value]
1267 if (Record.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +00001268 return Error(InvalidRecord);
Michael Ilseman407a6162012-11-15 22:34:00 +00001269
Chris Lattnerd408f062012-01-30 00:51:16 +00001270 Type *EltTy = cast<SequentialType>(CurTy)->getElementType();
1271 unsigned Size = Record.size();
Michael Ilseman407a6162012-11-15 22:34:00 +00001272
Chris Lattnerd408f062012-01-30 00:51:16 +00001273 if (EltTy->isIntegerTy(8)) {
1274 SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end());
1275 if (isa<VectorType>(CurTy))
1276 V = ConstantDataVector::get(Context, Elts);
1277 else
1278 V = ConstantDataArray::get(Context, Elts);
1279 } else if (EltTy->isIntegerTy(16)) {
1280 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
1281 if (isa<VectorType>(CurTy))
1282 V = ConstantDataVector::get(Context, Elts);
1283 else
1284 V = ConstantDataArray::get(Context, Elts);
1285 } else if (EltTy->isIntegerTy(32)) {
1286 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
1287 if (isa<VectorType>(CurTy))
1288 V = ConstantDataVector::get(Context, Elts);
1289 else
1290 V = ConstantDataArray::get(Context, Elts);
1291 } else if (EltTy->isIntegerTy(64)) {
1292 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
1293 if (isa<VectorType>(CurTy))
1294 V = ConstantDataVector::get(Context, Elts);
1295 else
1296 V = ConstantDataArray::get(Context, Elts);
1297 } else if (EltTy->isFloatTy()) {
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001298 SmallVector<float, 16> Elts(Size);
1299 std::transform(Record.begin(), Record.end(), Elts.begin(), BitsToFloat);
Chris Lattnerd408f062012-01-30 00:51:16 +00001300 if (isa<VectorType>(CurTy))
1301 V = ConstantDataVector::get(Context, Elts);
1302 else
1303 V = ConstantDataArray::get(Context, Elts);
1304 } else if (EltTy->isDoubleTy()) {
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001305 SmallVector<double, 16> Elts(Size);
1306 std::transform(Record.begin(), Record.end(), Elts.begin(),
1307 BitsToDouble);
Chris Lattnerd408f062012-01-30 00:51:16 +00001308 if (isa<VectorType>(CurTy))
1309 V = ConstantDataVector::get(Context, Elts);
1310 else
1311 V = ConstantDataArray::get(Context, Elts);
1312 } else {
Rafael Espindolae076b532013-11-04 16:16:24 +00001313 return Error(InvalidTypeForValue);
Chris Lattnerd408f062012-01-30 00:51:16 +00001314 }
1315 break;
1316 }
1317
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001318 case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval]
Rafael Espindolae076b532013-11-04 16:16:24 +00001319 if (Record.size() < 3)
1320 return Error(InvalidRecord);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001321 int Opc = GetDecodedBinaryOpcode(Record[0], CurTy);
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001322 if (Opc < 0) {
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001323 V = UndefValue::get(CurTy); // Unknown binop.
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001324 } else {
1325 Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy);
1326 Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy);
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001327 unsigned Flags = 0;
1328 if (Record.size() >= 4) {
1329 if (Opc == Instruction::Add ||
1330 Opc == Instruction::Sub ||
Chris Lattnerf067d582011-02-07 16:40:21 +00001331 Opc == Instruction::Mul ||
1332 Opc == Instruction::Shl) {
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001333 if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP))
1334 Flags |= OverflowingBinaryOperator::NoSignedWrap;
1335 if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
1336 Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
Chris Lattner35bda892011-02-06 21:44:57 +00001337 } else if (Opc == Instruction::SDiv ||
Chris Lattnerf067d582011-02-07 16:40:21 +00001338 Opc == Instruction::UDiv ||
1339 Opc == Instruction::LShr ||
1340 Opc == Instruction::AShr) {
Chris Lattner35bda892011-02-06 21:44:57 +00001341 if (Record[3] & (1 << bitc::PEO_EXACT))
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001342 Flags |= SDivOperator::IsExact;
1343 }
1344 }
1345 V = ConstantExpr::get(Opc, LHS, RHS, Flags);
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001346 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001347 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001348 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001349 case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval]
Rafael Espindolae076b532013-11-04 16:16:24 +00001350 if (Record.size() < 3)
1351 return Error(InvalidRecord);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001352 int Opc = GetDecodedCastOpcode(Record[0]);
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001353 if (Opc < 0) {
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001354 V = UndefValue::get(CurTy); // Unknown cast.
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001355 } else {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001356 Type *OpTy = getTypeByID(Record[1]);
Rafael Espindolae076b532013-11-04 16:16:24 +00001357 if (!OpTy)
1358 return Error(InvalidRecord);
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001359 Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy);
Matt Arsenault59d3ae62013-11-15 01:34:59 +00001360 V = UpgradeBitCastExpr(Opc, Op, CurTy);
1361 if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy);
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001362 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001363 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001364 }
Dan Gohmandd8004d2009-07-27 21:53:46 +00001365 case bitc::CST_CODE_CE_INBOUNDS_GEP:
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001366 case bitc::CST_CODE_CE_GEP: { // CE_GEP: [n x operands]
Rafael Espindolae076b532013-11-04 16:16:24 +00001367 if (Record.size() & 1)
1368 return Error(InvalidRecord);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001369 SmallVector<Constant*, 16> Elts;
Chris Lattner15e6d172007-05-04 19:11:41 +00001370 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001371 Type *ElTy = getTypeByID(Record[i]);
Rafael Espindolae076b532013-11-04 16:16:24 +00001372 if (!ElTy)
1373 return Error(InvalidRecord);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001374 Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], ElTy));
1375 }
Jay Foaddab3d292011-07-21 14:31:17 +00001376 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
Jay Foad4b5e2072011-07-21 15:15:37 +00001377 V = ConstantExpr::getGetElementPtr(Elts[0], Indices,
1378 BitCode ==
1379 bitc::CST_CODE_CE_INBOUNDS_GEP);
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001380 break;
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001381 }
Joe Abbey405b6502013-09-12 22:02:31 +00001382 case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#]
Rafael Espindolae076b532013-11-04 16:16:24 +00001383 if (Record.size() < 3)
1384 return Error(InvalidRecord);
Joe Abbey405b6502013-09-12 22:02:31 +00001385
1386 Type *SelectorTy = Type::getInt1Ty(Context);
1387
1388 // If CurTy is a vector of length n, then Record[0] must be a <n x i1>
1389 // vector. Otherwise, it must be a single bit.
1390 if (VectorType *VTy = dyn_cast<VectorType>(CurTy))
1391 SelectorTy = VectorType::get(Type::getInt1Ty(Context),
1392 VTy->getNumElements());
1393
1394 V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0],
1395 SelectorTy),
1396 ValueList.getConstantFwdRef(Record[1],CurTy),
1397 ValueList.getConstantFwdRef(Record[2],CurTy));
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001398 break;
Joe Abbey405b6502013-09-12 22:02:31 +00001399 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001400 case bitc::CST_CODE_CE_EXTRACTELT: { // CE_EXTRACTELT: [opty, opval, opval]
Rafael Espindolae076b532013-11-04 16:16:24 +00001401 if (Record.size() < 3)
1402 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001403 VectorType *OpTy =
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001404 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
Rafael Espindolae076b532013-11-04 16:16:24 +00001405 if (OpTy == 0)
1406 return Error(InvalidRecord);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001407 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
Joe Abbey170a15e2012-11-25 15:23:39 +00001408 Constant *Op1 = ValueList.getConstantFwdRef(Record[2],
Joe Abbeye46b14a2012-11-19 19:22:55 +00001409 Type::getInt32Ty(Context));
Owen Andersonbaf3c402009-07-29 18:55:55 +00001410 V = ConstantExpr::getExtractElement(Op0, Op1);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001411 break;
1412 }
1413 case bitc::CST_CODE_CE_INSERTELT: { // CE_INSERTELT: [opval, opval, opval]
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001414 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001415 if (Record.size() < 3 || OpTy == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00001416 return Error(InvalidRecord);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001417 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
1418 Constant *Op1 = ValueList.getConstantFwdRef(Record[1],
1419 OpTy->getElementType());
Joe Abbey170a15e2012-11-25 15:23:39 +00001420 Constant *Op2 = ValueList.getConstantFwdRef(Record[2],
Joe Abbeye46b14a2012-11-19 19:22:55 +00001421 Type::getInt32Ty(Context));
Owen Andersonbaf3c402009-07-29 18:55:55 +00001422 V = ConstantExpr::getInsertElement(Op0, Op1, Op2);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001423 break;
1424 }
1425 case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval]
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001426 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001427 if (Record.size() < 3 || OpTy == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00001428 return Error(InvalidRecord);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001429 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
1430 Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001431 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
Owen Anderson74a77812009-07-07 20:18:58 +00001432 OpTy->getNumElements());
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001433 Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy);
Owen Andersonbaf3c402009-07-29 18:55:55 +00001434 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001435 break;
1436 }
Nate Begeman0f123cf2009-02-12 21:28:33 +00001437 case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval]
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001438 VectorType *RTy = dyn_cast<VectorType>(CurTy);
1439 VectorType *OpTy =
Duncan Sandsf22b7462010-10-28 15:47:26 +00001440 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
Nate Begeman0f123cf2009-02-12 21:28:33 +00001441 if (Record.size() < 4 || RTy == 0 || OpTy == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00001442 return Error(InvalidRecord);
Nate Begeman0f123cf2009-02-12 21:28:33 +00001443 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
1444 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001445 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
Owen Anderson74a77812009-07-07 20:18:58 +00001446 RTy->getNumElements());
Nate Begeman0f123cf2009-02-12 21:28:33 +00001447 Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy);
Owen Andersonbaf3c402009-07-29 18:55:55 +00001448 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Nate Begeman0f123cf2009-02-12 21:28:33 +00001449 break;
1450 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001451 case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred]
Rafael Espindolae076b532013-11-04 16:16:24 +00001452 if (Record.size() < 4)
1453 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001454 Type *OpTy = getTypeByID(Record[0]);
Rafael Espindolae076b532013-11-04 16:16:24 +00001455 if (OpTy == 0)
1456 return Error(InvalidRecord);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001457 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
1458 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
1459
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001460 if (OpTy->isFPOrFPVectorTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +00001461 V = ConstantExpr::getFCmp(Record[3], Op0, Op1);
Nate Begemanac80ade2008-05-12 19:01:56 +00001462 else
Owen Andersonbaf3c402009-07-29 18:55:55 +00001463 V = ConstantExpr::getICmp(Record[3], Op0, Op1);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001464 break;
Chris Lattner522b7b12007-04-24 05:48:56 +00001465 }
Chad Rosier581600b2012-09-05 19:00:49 +00001466 // This maintains backward compatibility, pre-asm dialect keywords.
Chad Rosier27b25c22012-09-05 06:28:52 +00001467 // FIXME: Remove with the 4.0 release.
Chad Rosierf16ae582012-09-05 00:56:20 +00001468 case bitc::CST_CODE_INLINEASM_OLD: {
Rafael Espindolae076b532013-11-04 16:16:24 +00001469 if (Record.size() < 2)
1470 return Error(InvalidRecord);
Chris Lattner2bce93a2007-05-06 01:58:20 +00001471 std::string AsmStr, ConstrStr;
Dale Johannesen43602982009-10-13 20:46:56 +00001472 bool HasSideEffects = Record[0] & 1;
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +00001473 bool IsAlignStack = Record[0] >> 1;
Chris Lattner2bce93a2007-05-06 01:58:20 +00001474 unsigned AsmStrSize = Record[1];
1475 if (2+AsmStrSize >= Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001476 return Error(InvalidRecord);
Chris Lattner2bce93a2007-05-06 01:58:20 +00001477 unsigned ConstStrSize = Record[2+AsmStrSize];
1478 if (3+AsmStrSize+ConstStrSize > Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001479 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001480
Chris Lattner2bce93a2007-05-06 01:58:20 +00001481 for (unsigned i = 0; i != AsmStrSize; ++i)
1482 AsmStr += (char)Record[2+i];
1483 for (unsigned i = 0; i != ConstStrSize; ++i)
1484 ConstrStr += (char)Record[3+AsmStrSize+i];
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001485 PointerType *PTy = cast<PointerType>(CurTy);
Chris Lattner2bce93a2007-05-06 01:58:20 +00001486 V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +00001487 AsmStr, ConstrStr, HasSideEffects, IsAlignStack);
Chris Lattner2bce93a2007-05-06 01:58:20 +00001488 break;
1489 }
Chad Rosier581600b2012-09-05 19:00:49 +00001490 // This version adds support for the asm dialect keywords (e.g.,
1491 // inteldialect).
Chad Rosierf16ae582012-09-05 00:56:20 +00001492 case bitc::CST_CODE_INLINEASM: {
Rafael Espindolae076b532013-11-04 16:16:24 +00001493 if (Record.size() < 2)
1494 return Error(InvalidRecord);
Chad Rosierf16ae582012-09-05 00:56:20 +00001495 std::string AsmStr, ConstrStr;
1496 bool HasSideEffects = Record[0] & 1;
1497 bool IsAlignStack = (Record[0] >> 1) & 1;
1498 unsigned AsmDialect = Record[0] >> 2;
1499 unsigned AsmStrSize = Record[1];
1500 if (2+AsmStrSize >= Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001501 return Error(InvalidRecord);
Chad Rosierf16ae582012-09-05 00:56:20 +00001502 unsigned ConstStrSize = Record[2+AsmStrSize];
1503 if (3+AsmStrSize+ConstStrSize > Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001504 return Error(InvalidRecord);
Chad Rosierf16ae582012-09-05 00:56:20 +00001505
1506 for (unsigned i = 0; i != AsmStrSize; ++i)
1507 AsmStr += (char)Record[2+i];
1508 for (unsigned i = 0; i != ConstStrSize; ++i)
1509 ConstrStr += (char)Record[3+AsmStrSize+i];
1510 PointerType *PTy = cast<PointerType>(CurTy);
1511 V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
1512 AsmStr, ConstrStr, HasSideEffects, IsAlignStack,
Chad Rosier581600b2012-09-05 19:00:49 +00001513 InlineAsm::AsmDialect(AsmDialect));
Chad Rosierf16ae582012-09-05 00:56:20 +00001514 break;
1515 }
Chris Lattner50b136d2009-10-28 05:53:48 +00001516 case bitc::CST_CODE_BLOCKADDRESS:{
Rafael Espindolae076b532013-11-04 16:16:24 +00001517 if (Record.size() < 3)
1518 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001519 Type *FnTy = getTypeByID(Record[0]);
Rafael Espindolae076b532013-11-04 16:16:24 +00001520 if (FnTy == 0)
1521 return Error(InvalidRecord);
Chris Lattner50b136d2009-10-28 05:53:48 +00001522 Function *Fn =
1523 dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy));
Rafael Espindolae076b532013-11-04 16:16:24 +00001524 if (Fn == 0)
1525 return Error(InvalidRecord);
Benjamin Kramer122f5e52012-09-21 14:34:31 +00001526
1527 // If the function is already parsed we can insert the block address right
1528 // away.
1529 if (!Fn->empty()) {
1530 Function::iterator BBI = Fn->begin(), BBE = Fn->end();
1531 for (size_t I = 0, E = Record[2]; I != E; ++I) {
1532 if (BBI == BBE)
Rafael Espindolae076b532013-11-04 16:16:24 +00001533 return Error(InvalidID);
Benjamin Kramer122f5e52012-09-21 14:34:31 +00001534 ++BBI;
1535 }
1536 V = BlockAddress::get(Fn, BBI);
1537 } else {
1538 // Otherwise insert a placeholder and remember it so it can be inserted
1539 // when the function is parsed.
1540 GlobalVariable *FwdRef = new GlobalVariable(*Fn->getParent(),
1541 Type::getInt8Ty(Context),
Chris Lattner50b136d2009-10-28 05:53:48 +00001542 false, GlobalValue::InternalLinkage,
Benjamin Kramer122f5e52012-09-21 14:34:31 +00001543 0, "");
1544 BlockAddrFwdRefs[Fn].push_back(std::make_pair(Record[2], FwdRef));
1545 V = FwdRef;
1546 }
Chris Lattner50b136d2009-10-28 05:53:48 +00001547 break;
Michael Ilseman407a6162012-11-15 22:34:00 +00001548 }
Chris Lattnere16504e2007-04-24 03:30:34 +00001549 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001550
Chris Lattnera7c49aa2007-05-01 07:01:57 +00001551 ValueList.AssignValue(V, NextCstNo);
Chris Lattner522b7b12007-04-24 05:48:56 +00001552 ++NextCstNo;
Chris Lattnere16504e2007-04-24 03:30:34 +00001553 }
1554}
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001555
Rafael Espindolae076b532013-11-04 16:16:24 +00001556error_code BitcodeReader::ParseUseLists() {
Chad Rosiercbbb0962011-12-07 21:44:12 +00001557 if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +00001558 return Error(InvalidRecord);
Chad Rosiercbbb0962011-12-07 21:44:12 +00001559
1560 SmallVector<uint64_t, 64> Record;
Michael Ilseman407a6162012-11-15 22:34:00 +00001561
Chad Rosiercbbb0962011-12-07 21:44:12 +00001562 // Read all the records.
1563 while (1) {
Chris Lattner5a4251c2013-01-20 02:13:19 +00001564 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbeyacb61942013-02-06 22:14:06 +00001565
Chris Lattner5a4251c2013-01-20 02:13:19 +00001566 switch (Entry.Kind) {
1567 case BitstreamEntry::SubBlock: // Handled for us already.
1568 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +00001569 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00001570 case BitstreamEntry::EndBlock:
Rafael Espindolae076b532013-11-04 16:16:24 +00001571 return error_code::success();
Chris Lattner5a4251c2013-01-20 02:13:19 +00001572 case BitstreamEntry::Record:
1573 // The interesting case.
1574 break;
Chad Rosiercbbb0962011-12-07 21:44:12 +00001575 }
Michael Ilseman407a6162012-11-15 22:34:00 +00001576
Chad Rosiercbbb0962011-12-07 21:44:12 +00001577 // Read a use list record.
1578 Record.clear();
Chris Lattner5a4251c2013-01-20 02:13:19 +00001579 switch (Stream.readRecord(Entry.ID, Record)) {
Chad Rosiercbbb0962011-12-07 21:44:12 +00001580 default: // Default behavior: unknown type.
1581 break;
1582 case bitc::USELIST_CODE_ENTRY: { // USELIST_CODE_ENTRY: TBD.
1583 unsigned RecordLength = Record.size();
1584 if (RecordLength < 1)
Rafael Espindolae076b532013-11-04 16:16:24 +00001585 return Error(InvalidRecord);
Chad Rosiercbbb0962011-12-07 21:44:12 +00001586 UseListRecords.push_back(Record);
1587 break;
1588 }
1589 }
1590 }
1591}
1592
Chris Lattner980e5aa2007-05-01 05:52:21 +00001593/// RememberAndSkipFunctionBody - When we see the block for a function body,
1594/// remember where it is and then skip it. This lets us lazily deserialize the
1595/// functions.
Rafael Espindolae076b532013-11-04 16:16:24 +00001596error_code BitcodeReader::RememberAndSkipFunctionBody() {
Chris Lattner48f84872007-05-01 04:59:48 +00001597 // Get the function we are talking about.
1598 if (FunctionsWithBodies.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +00001599 return Error(InsufficientFunctionProtos);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001600
Chris Lattner48f84872007-05-01 04:59:48 +00001601 Function *Fn = FunctionsWithBodies.back();
1602 FunctionsWithBodies.pop_back();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001603
Chris Lattner48f84872007-05-01 04:59:48 +00001604 // Save the current stream state.
1605 uint64_t CurBit = Stream.GetCurrentBitNo();
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00001606 DeferredFunctionInfo[Fn] = CurBit;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001607
Chris Lattner48f84872007-05-01 04:59:48 +00001608 // Skip over the function block for now.
1609 if (Stream.SkipBlock())
Rafael Espindolae076b532013-11-04 16:16:24 +00001610 return Error(InvalidRecord);
1611 return error_code::success();
Chris Lattner48f84872007-05-01 04:59:48 +00001612}
1613
Rafael Espindolae076b532013-11-04 16:16:24 +00001614error_code BitcodeReader::GlobalCleanup() {
Derek Schuff2ea93872012-02-06 22:30:29 +00001615 // Patch the initializers for globals and aliases up.
1616 ResolveGlobalAndAliasInits();
1617 if (!GlobalInits.empty() || !AliasInits.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +00001618 return Error(MalformedGlobalInitializerSet);
Derek Schuff2ea93872012-02-06 22:30:29 +00001619
1620 // Look for intrinsic functions which need to be upgraded at some point
1621 for (Module::iterator FI = TheModule->begin(), FE = TheModule->end();
1622 FI != FE; ++FI) {
1623 Function *NewFn;
1624 if (UpgradeIntrinsicFunction(FI, NewFn))
1625 UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn));
1626 }
1627
1628 // Look for global variables which need to be renamed.
1629 for (Module::global_iterator
1630 GI = TheModule->global_begin(), GE = TheModule->global_end();
1631 GI != GE; ++GI)
1632 UpgradeGlobalVariable(GI);
1633 // Force deallocation of memory for these vectors to favor the client that
1634 // want lazy deserialization.
1635 std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits);
1636 std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits);
Rafael Espindolae076b532013-11-04 16:16:24 +00001637 return error_code::success();
Derek Schuff2ea93872012-02-06 22:30:29 +00001638}
1639
Rafael Espindolae076b532013-11-04 16:16:24 +00001640error_code BitcodeReader::ParseModule(bool Resume) {
Derek Schuff2ea93872012-02-06 22:30:29 +00001641 if (Resume)
1642 Stream.JumpToBit(NextUnreadBit);
1643 else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +00001644 return Error(InvalidRecord);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001645
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001646 SmallVector<uint64_t, 64> Record;
1647 std::vector<std::string> SectionTable;
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001648 std::vector<std::string> GCTable;
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001649
1650 // Read all the records for this module.
Chris Lattner5a4251c2013-01-20 02:13:19 +00001651 while (1) {
1652 BitstreamEntry Entry = Stream.advance();
Joe Abbeyacb61942013-02-06 22:14:06 +00001653
Chris Lattner5a4251c2013-01-20 02:13:19 +00001654 switch (Entry.Kind) {
1655 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +00001656 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00001657 case BitstreamEntry::EndBlock:
Derek Schuff2ea93872012-02-06 22:30:29 +00001658 return GlobalCleanup();
Joe Abbeyacb61942013-02-06 22:14:06 +00001659
Chris Lattner5a4251c2013-01-20 02:13:19 +00001660 case BitstreamEntry::SubBlock:
1661 switch (Entry.ID) {
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001662 default: // Skip unknown content.
1663 if (Stream.SkipBlock())
Rafael Espindolae076b532013-11-04 16:16:24 +00001664 return Error(InvalidRecord);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001665 break;
Chris Lattner3f799802007-05-05 18:57:30 +00001666 case bitc::BLOCKINFO_BLOCK_ID:
1667 if (Stream.ReadBlockInfoBlock())
Rafael Espindolae076b532013-11-04 16:16:24 +00001668 return Error(MalformedBlock);
Chris Lattner3f799802007-05-05 18:57:30 +00001669 break;
Chris Lattner48c85b82007-05-04 03:30:17 +00001670 case bitc::PARAMATTR_BLOCK_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00001671 if (error_code EC = ParseAttributeBlock())
1672 return EC;
Chris Lattner48c85b82007-05-04 03:30:17 +00001673 break;
Bill Wendlingc3ba0a82013-02-10 23:24:25 +00001674 case bitc::PARAMATTR_GROUP_BLOCK_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00001675 if (error_code EC = ParseAttributeGroupBlock())
1676 return EC;
Bill Wendlingc3ba0a82013-02-10 23:24:25 +00001677 break;
Chris Lattner1afcace2011-07-09 17:41:24 +00001678 case bitc::TYPE_BLOCK_ID_NEW:
Rafael Espindolae076b532013-11-04 16:16:24 +00001679 if (error_code EC = ParseTypeTable())
1680 return EC;
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001681 break;
Chris Lattner0b2482a2007-04-23 21:26:05 +00001682 case bitc::VALUE_SYMTAB_BLOCK_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00001683 if (error_code EC = ParseValueSymbolTable())
1684 return EC;
Derek Schuff2ea93872012-02-06 22:30:29 +00001685 SeenValueSymbolTable = true;
Chris Lattner0b2482a2007-04-23 21:26:05 +00001686 break;
Chris Lattnere16504e2007-04-24 03:30:34 +00001687 case bitc::CONSTANTS_BLOCK_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00001688 if (error_code EC = ParseConstants())
1689 return EC;
1690 if (error_code EC = ResolveGlobalAndAliasInits())
1691 return EC;
Chris Lattnere16504e2007-04-24 03:30:34 +00001692 break;
Devang Patele54abc92009-07-22 17:43:22 +00001693 case bitc::METADATA_BLOCK_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00001694 if (error_code EC = ParseMetadata())
1695 return EC;
Devang Patele54abc92009-07-22 17:43:22 +00001696 break;
Chris Lattner48f84872007-05-01 04:59:48 +00001697 case bitc::FUNCTION_BLOCK_ID:
1698 // If this is the first function body we've seen, reverse the
1699 // FunctionsWithBodies list.
Derek Schuff2ea93872012-02-06 22:30:29 +00001700 if (!SeenFirstFunctionBody) {
Chris Lattner48f84872007-05-01 04:59:48 +00001701 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
Rafael Espindolae076b532013-11-04 16:16:24 +00001702 if (error_code EC = GlobalCleanup())
1703 return EC;
Derek Schuff2ea93872012-02-06 22:30:29 +00001704 SeenFirstFunctionBody = true;
Chris Lattner48f84872007-05-01 04:59:48 +00001705 }
Joe Abbeyacb61942013-02-06 22:14:06 +00001706
Rafael Espindolae076b532013-11-04 16:16:24 +00001707 if (error_code EC = RememberAndSkipFunctionBody())
1708 return EC;
Derek Schuff2ea93872012-02-06 22:30:29 +00001709 // For streaming bitcode, suspend parsing when we reach the function
1710 // bodies. Subsequent materialization calls will resume it when
1711 // necessary. For streaming, the function bodies must be at the end of
1712 // the bitcode. If the bitcode file is old, the symbol table will be
1713 // at the end instead and will not have been seen yet. In this case,
1714 // just finish the parse now.
1715 if (LazyStreamer && SeenValueSymbolTable) {
1716 NextUnreadBit = Stream.GetCurrentBitNo();
Rafael Espindolae076b532013-11-04 16:16:24 +00001717 return error_code::success();
Derek Schuff2ea93872012-02-06 22:30:29 +00001718 }
Chris Lattner48f84872007-05-01 04:59:48 +00001719 break;
Chad Rosiercbbb0962011-12-07 21:44:12 +00001720 case bitc::USELIST_BLOCK_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00001721 if (error_code EC = ParseUseLists())
1722 return EC;
Chad Rosiercbbb0962011-12-07 21:44:12 +00001723 break;
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001724 }
1725 continue;
Joe Abbeyacb61942013-02-06 22:14:06 +00001726
Chris Lattner5a4251c2013-01-20 02:13:19 +00001727 case BitstreamEntry::Record:
1728 // The interesting case.
1729 break;
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001730 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001731
Daniel Dunbara279bc32009-09-20 02:20:51 +00001732
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001733 // Read a record.
Chris Lattner5a4251c2013-01-20 02:13:19 +00001734 switch (Stream.readRecord(Entry.ID, Record)) {
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001735 default: break; // Default behavior, ignore unknown content.
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00001736 case bitc::MODULE_CODE_VERSION: { // VERSION: [version#]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001737 if (Record.size() < 1)
Rafael Espindolae076b532013-11-04 16:16:24 +00001738 return Error(InvalidRecord);
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00001739 // Only version #0 and #1 are supported so far.
1740 unsigned module_version = Record[0];
1741 switch (module_version) {
Rafael Espindolae076b532013-11-04 16:16:24 +00001742 default:
1743 return Error(InvalidValue);
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00001744 case 0:
1745 UseRelativeIDs = false;
1746 break;
1747 case 1:
1748 UseRelativeIDs = true;
1749 break;
1750 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001751 break;
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00001752 }
Chris Lattner15e6d172007-05-04 19:11:41 +00001753 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001754 std::string S;
1755 if (ConvertToString(Record, 0, S))
Rafael Espindolae076b532013-11-04 16:16:24 +00001756 return Error(InvalidRecord);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001757 TheModule->setTargetTriple(S);
1758 break;
1759 }
Chris Lattner15e6d172007-05-04 19:11:41 +00001760 case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001761 std::string S;
1762 if (ConvertToString(Record, 0, S))
Rafael Espindolae076b532013-11-04 16:16:24 +00001763 return Error(InvalidRecord);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001764 TheModule->setDataLayout(S);
1765 break;
1766 }
Chris Lattner15e6d172007-05-04 19:11:41 +00001767 case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001768 std::string S;
1769 if (ConvertToString(Record, 0, S))
Rafael Espindolae076b532013-11-04 16:16:24 +00001770 return Error(InvalidRecord);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001771 TheModule->setModuleInlineAsm(S);
1772 break;
1773 }
Bill Wendling3defc0b2012-11-28 08:41:48 +00001774 case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N]
1775 // FIXME: Remove in 4.0.
1776 std::string S;
1777 if (ConvertToString(Record, 0, S))
Rafael Espindolae076b532013-11-04 16:16:24 +00001778 return Error(InvalidRecord);
Bill Wendling3defc0b2012-11-28 08:41:48 +00001779 // Ignore value.
1780 break;
1781 }
Chris Lattner15e6d172007-05-04 19:11:41 +00001782 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001783 std::string S;
1784 if (ConvertToString(Record, 0, S))
Rafael Espindolae076b532013-11-04 16:16:24 +00001785 return Error(InvalidRecord);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001786 SectionTable.push_back(S);
1787 break;
1788 }
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001789 case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N]
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001790 std::string S;
1791 if (ConvertToString(Record, 0, S))
Rafael Espindolae076b532013-11-04 16:16:24 +00001792 return Error(InvalidRecord);
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001793 GCTable.push_back(S);
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001794 break;
1795 }
Christopher Lambfe63fb92007-12-11 08:59:05 +00001796 // GLOBALVAR: [pointer type, isconst, initid,
Rafael Espindolabea46262011-01-08 16:42:36 +00001797 // linkage, alignment, section, visibility, threadlocal,
1798 // unnamed_addr]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001799 case bitc::MODULE_CODE_GLOBALVAR: {
Chris Lattner36d5e7d2007-04-23 16:04:05 +00001800 if (Record.size() < 6)
Rafael Espindolae076b532013-11-04 16:16:24 +00001801 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001802 Type *Ty = getTypeByID(Record[0]);
Rafael Espindolae076b532013-11-04 16:16:24 +00001803 if (!Ty)
1804 return Error(InvalidRecord);
Duncan Sands1df98592010-02-16 11:11:14 +00001805 if (!Ty->isPointerTy())
Rafael Espindolae076b532013-11-04 16:16:24 +00001806 return Error(InvalidTypeForValue);
Christopher Lambfe63fb92007-12-11 08:59:05 +00001807 unsigned AddressSpace = cast<PointerType>(Ty)->getAddressSpace();
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001808 Ty = cast<PointerType>(Ty)->getElementType();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001809
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001810 bool isConstant = Record[1];
1811 GlobalValue::LinkageTypes Linkage = GetDecodedLinkage(Record[3]);
1812 unsigned Alignment = (1 << Record[4]) >> 1;
1813 std::string Section;
1814 if (Record[5]) {
1815 if (Record[5]-1 >= SectionTable.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001816 return Error(InvalidID);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001817 Section = SectionTable[Record[5]-1];
1818 }
Chris Lattner36d5e7d2007-04-23 16:04:05 +00001819 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
Chris Lattner5f32c012007-05-06 19:27:46 +00001820 if (Record.size() > 6)
1821 Visibility = GetDecodedVisibility(Record[6]);
Hans Wennborgce718ff2012-06-23 11:37:03 +00001822
1823 GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal;
Chris Lattner5f32c012007-05-06 19:27:46 +00001824 if (Record.size() > 7)
Hans Wennborgce718ff2012-06-23 11:37:03 +00001825 TLM = GetDecodedThreadLocalMode(Record[7]);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001826
Rafael Espindolabea46262011-01-08 16:42:36 +00001827 bool UnnamedAddr = false;
1828 if (Record.size() > 8)
1829 UnnamedAddr = Record[8];
1830
Michael Gottesmana2de37c2013-02-05 05:57:38 +00001831 bool ExternallyInitialized = false;
1832 if (Record.size() > 9)
1833 ExternallyInitialized = Record[9];
1834
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001835 GlobalVariable *NewGV =
Daniel Dunbara279bc32009-09-20 02:20:51 +00001836 new GlobalVariable(*TheModule, Ty, isConstant, Linkage, 0, "", 0,
Michael Gottesmana2de37c2013-02-05 05:57:38 +00001837 TLM, AddressSpace, ExternallyInitialized);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001838 NewGV->setAlignment(Alignment);
1839 if (!Section.empty())
1840 NewGV->setSection(Section);
1841 NewGV->setVisibility(Visibility);
Rafael Espindolabea46262011-01-08 16:42:36 +00001842 NewGV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001843
Chris Lattner0b2482a2007-04-23 21:26:05 +00001844 ValueList.push_back(NewGV);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001845
Chris Lattner6dbfd7b2007-04-24 00:18:21 +00001846 // Remember which value to use for the global initializer.
1847 if (unsigned InitID = Record[2])
1848 GlobalInits.push_back(std::make_pair(NewGV, InitID-1));
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001849 break;
1850 }
Chris Lattnera9bb7132007-05-08 05:38:01 +00001851 // FUNCTION: [type, callingconv, isproto, linkage, paramattr,
Rafael Espindolabea46262011-01-08 16:42:36 +00001852 // alignment, section, visibility, gc, unnamed_addr]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001853 case bitc::MODULE_CODE_FUNCTION: {
Chris Lattnera9bb7132007-05-08 05:38:01 +00001854 if (Record.size() < 8)
Rafael Espindolae076b532013-11-04 16:16:24 +00001855 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001856 Type *Ty = getTypeByID(Record[0]);
Rafael Espindolae076b532013-11-04 16:16:24 +00001857 if (!Ty)
1858 return Error(InvalidRecord);
Duncan Sands1df98592010-02-16 11:11:14 +00001859 if (!Ty->isPointerTy())
Rafael Espindolae076b532013-11-04 16:16:24 +00001860 return Error(InvalidTypeForValue);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001861 FunctionType *FTy =
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001862 dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType());
1863 if (!FTy)
Rafael Espindolae076b532013-11-04 16:16:24 +00001864 return Error(InvalidTypeForValue);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001865
Gabor Greif051a9502008-04-06 20:25:17 +00001866 Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage,
1867 "", TheModule);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001868
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001869 Func->setCallingConv(static_cast<CallingConv::ID>(Record[1]));
Chris Lattner48f84872007-05-01 04:59:48 +00001870 bool isProto = Record[2];
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001871 Func->setLinkage(GetDecodedLinkage(Record[3]));
Devang Patel05988662008-09-25 21:00:45 +00001872 Func->setAttributes(getAttributes(Record[4]));
Daniel Dunbara279bc32009-09-20 02:20:51 +00001873
Chris Lattnera9bb7132007-05-08 05:38:01 +00001874 Func->setAlignment((1 << Record[5]) >> 1);
1875 if (Record[6]) {
1876 if (Record[6]-1 >= SectionTable.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001877 return Error(InvalidID);
Chris Lattnera9bb7132007-05-08 05:38:01 +00001878 Func->setSection(SectionTable[Record[6]-1]);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001879 }
Chris Lattnera9bb7132007-05-08 05:38:01 +00001880 Func->setVisibility(GetDecodedVisibility(Record[7]));
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001881 if (Record.size() > 8 && Record[8]) {
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001882 if (Record[8]-1 > GCTable.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001883 return Error(InvalidID);
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001884 Func->setGC(GCTable[Record[8]-1].c_str());
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001885 }
Rafael Espindolabea46262011-01-08 16:42:36 +00001886 bool UnnamedAddr = false;
1887 if (Record.size() > 9)
1888 UnnamedAddr = Record[9];
1889 Func->setUnnamedAddr(UnnamedAddr);
Peter Collingbourne1e3037f2013-09-16 01:08:15 +00001890 if (Record.size() > 10 && Record[10] != 0)
1891 FunctionPrefixes.push_back(std::make_pair(Func, Record[10]-1));
Chris Lattner0b2482a2007-04-23 21:26:05 +00001892 ValueList.push_back(Func);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001893
Chris Lattner48f84872007-05-01 04:59:48 +00001894 // If this is a function with a body, remember the prototype we are
1895 // creating now, so that we can match up the body with them later.
Derek Schuff2ea93872012-02-06 22:30:29 +00001896 if (!isProto) {
Chris Lattner48f84872007-05-01 04:59:48 +00001897 FunctionsWithBodies.push_back(Func);
Derek Schuff2ea93872012-02-06 22:30:29 +00001898 if (LazyStreamer) DeferredFunctionInfo[Func] = 0;
1899 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001900 break;
1901 }
Anton Korobeynikov91342d82008-03-12 00:49:19 +00001902 // ALIAS: [alias type, aliasee val#, linkage]
Anton Korobeynikovf8342b92008-03-11 21:40:17 +00001903 // ALIAS: [alias type, aliasee val#, linkage, visibility]
Chris Lattner198f34a2007-04-26 03:27:58 +00001904 case bitc::MODULE_CODE_ALIAS: {
Chris Lattner07d98b42007-04-26 02:46:40 +00001905 if (Record.size() < 3)
Rafael Espindolae076b532013-11-04 16:16:24 +00001906 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001907 Type *Ty = getTypeByID(Record[0]);
Rafael Espindolae076b532013-11-04 16:16:24 +00001908 if (!Ty)
1909 return Error(InvalidRecord);
Duncan Sands1df98592010-02-16 11:11:14 +00001910 if (!Ty->isPointerTy())
Rafael Espindolae076b532013-11-04 16:16:24 +00001911 return Error(InvalidTypeForValue);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001912
Chris Lattner07d98b42007-04-26 02:46:40 +00001913 GlobalAlias *NewGA = new GlobalAlias(Ty, GetDecodedLinkage(Record[2]),
1914 "", 0, TheModule);
Anton Korobeynikov91342d82008-03-12 00:49:19 +00001915 // Old bitcode files didn't have visibility field.
1916 if (Record.size() > 3)
1917 NewGA->setVisibility(GetDecodedVisibility(Record[3]));
Chris Lattner07d98b42007-04-26 02:46:40 +00001918 ValueList.push_back(NewGA);
1919 AliasInits.push_back(std::make_pair(NewGA, Record[1]));
1920 break;
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001921 }
Chris Lattner198f34a2007-04-26 03:27:58 +00001922 /// MODULE_CODE_PURGEVALS: [numvals]
1923 case bitc::MODULE_CODE_PURGEVALS:
1924 // Trim down the value list to the specified size.
1925 if (Record.size() < 1 || Record[0] > ValueList.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001926 return Error(InvalidRecord);
Chris Lattner198f34a2007-04-26 03:27:58 +00001927 ValueList.shrinkTo(Record[0]);
1928 break;
1929 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001930 Record.clear();
1931 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001932}
1933
Rafael Espindolae076b532013-11-04 16:16:24 +00001934error_code BitcodeReader::ParseBitcodeInto(Module *M) {
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001935 TheModule = 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001936
Rafael Espindolae076b532013-11-04 16:16:24 +00001937 if (error_code EC = InitStream())
1938 return EC;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001939
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001940 // Sniff for the signature.
1941 if (Stream.Read(8) != 'B' ||
1942 Stream.Read(8) != 'C' ||
1943 Stream.Read(4) != 0x0 ||
1944 Stream.Read(4) != 0xC ||
1945 Stream.Read(4) != 0xE ||
1946 Stream.Read(4) != 0xD)
Rafael Espindolae076b532013-11-04 16:16:24 +00001947 return Error(InvalidBitcodeSignature);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001948
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001949 // We expect a number of well-defined blocks, though we don't necessarily
1950 // need to understand them all.
Chris Lattner5a4251c2013-01-20 02:13:19 +00001951 while (1) {
1952 if (Stream.AtEndOfStream())
Rafael Espindolae076b532013-11-04 16:16:24 +00001953 return error_code::success();
Joe Abbeyacb61942013-02-06 22:14:06 +00001954
Chris Lattner5a4251c2013-01-20 02:13:19 +00001955 BitstreamEntry Entry =
1956 Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
Joe Abbeyacb61942013-02-06 22:14:06 +00001957
Chris Lattner5a4251c2013-01-20 02:13:19 +00001958 switch (Entry.Kind) {
1959 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +00001960 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00001961 case BitstreamEntry::EndBlock:
Rafael Espindolae076b532013-11-04 16:16:24 +00001962 return error_code::success();
Joe Abbeyacb61942013-02-06 22:14:06 +00001963
Chris Lattner5a4251c2013-01-20 02:13:19 +00001964 case BitstreamEntry::SubBlock:
1965 switch (Entry.ID) {
1966 case bitc::BLOCKINFO_BLOCK_ID:
1967 if (Stream.ReadBlockInfoBlock())
Rafael Espindolae076b532013-11-04 16:16:24 +00001968 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00001969 break;
1970 case bitc::MODULE_BLOCK_ID:
1971 // Reject multiple MODULE_BLOCK's in a single bitstream.
1972 if (TheModule)
Rafael Espindolae076b532013-11-04 16:16:24 +00001973 return Error(InvalidMultipleBlocks);
Chris Lattner5a4251c2013-01-20 02:13:19 +00001974 TheModule = M;
Rafael Espindolae076b532013-11-04 16:16:24 +00001975 if (error_code EC = ParseModule(false))
1976 return EC;
1977 if (LazyStreamer)
1978 return error_code::success();
Chris Lattner5a4251c2013-01-20 02:13:19 +00001979 break;
1980 default:
1981 if (Stream.SkipBlock())
Rafael Espindolae076b532013-11-04 16:16:24 +00001982 return Error(InvalidRecord);
Chris Lattner5a4251c2013-01-20 02:13:19 +00001983 break;
1984 }
1985 continue;
1986 case BitstreamEntry::Record:
1987 // There should be no records in the top-level of blocks.
Joe Abbeyacb61942013-02-06 22:14:06 +00001988
Chris Lattner5a4251c2013-01-20 02:13:19 +00001989 // The ranlib in Xcode 4 will align archive members by appending newlines
Chad Rosier6ff9aa22011-08-09 22:23:40 +00001990 // to the end of them. If this file size is a multiple of 4 but not 8, we
1991 // have to read and ignore these final 4 bytes :-(
Chris Lattner5a4251c2013-01-20 02:13:19 +00001992 if (Stream.getAbbrevIDWidth() == 2 && Entry.ID == 2 &&
Rafael Espindolac9687b32011-05-26 18:59:54 +00001993 Stream.Read(6) == 2 && Stream.Read(24) == 0xa0a0a &&
Bill Wendling2127c9b2012-07-19 00:15:11 +00001994 Stream.AtEndOfStream())
Rafael Espindolae076b532013-11-04 16:16:24 +00001995 return error_code::success();
Joe Abbeyacb61942013-02-06 22:14:06 +00001996
Rafael Espindolae076b532013-11-04 16:16:24 +00001997 return Error(InvalidRecord);
Rafael Espindolac9687b32011-05-26 18:59:54 +00001998 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001999 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +00002000}
Chris Lattnerc453f762007-04-29 07:54:31 +00002001
Rafael Espindolae076b532013-11-04 16:16:24 +00002002error_code BitcodeReader::ParseModuleTriple(std::string &Triple) {
Bill Wendling34711742010-10-06 01:22:42 +00002003 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +00002004 return Error(InvalidRecord);
Bill Wendling34711742010-10-06 01:22:42 +00002005
2006 SmallVector<uint64_t, 64> Record;
2007
2008 // Read all the records for this module.
Chris Lattner5a4251c2013-01-20 02:13:19 +00002009 while (1) {
2010 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbeyacb61942013-02-06 22:14:06 +00002011
Chris Lattner5a4251c2013-01-20 02:13:19 +00002012 switch (Entry.Kind) {
2013 case BitstreamEntry::SubBlock: // Handled for us already.
2014 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +00002015 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00002016 case BitstreamEntry::EndBlock:
Rafael Espindolae076b532013-11-04 16:16:24 +00002017 return error_code::success();
Chris Lattner5a4251c2013-01-20 02:13:19 +00002018 case BitstreamEntry::Record:
2019 // The interesting case.
2020 break;
Bill Wendling34711742010-10-06 01:22:42 +00002021 }
2022
2023 // Read a record.
Chris Lattner5a4251c2013-01-20 02:13:19 +00002024 switch (Stream.readRecord(Entry.ID, Record)) {
Bill Wendling34711742010-10-06 01:22:42 +00002025 default: break; // Default behavior, ignore unknown content.
Bill Wendling34711742010-10-06 01:22:42 +00002026 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
2027 std::string S;
2028 if (ConvertToString(Record, 0, S))
Rafael Espindolae076b532013-11-04 16:16:24 +00002029 return Error(InvalidRecord);
Bill Wendling34711742010-10-06 01:22:42 +00002030 Triple = S;
2031 break;
2032 }
2033 }
2034 Record.clear();
2035 }
Bill Wendling34711742010-10-06 01:22:42 +00002036}
2037
Rafael Espindolae076b532013-11-04 16:16:24 +00002038error_code BitcodeReader::ParseTriple(std::string &Triple) {
2039 if (error_code EC = InitStream())
2040 return EC;
Bill Wendling34711742010-10-06 01:22:42 +00002041
2042 // Sniff for the signature.
2043 if (Stream.Read(8) != 'B' ||
2044 Stream.Read(8) != 'C' ||
2045 Stream.Read(4) != 0x0 ||
2046 Stream.Read(4) != 0xC ||
2047 Stream.Read(4) != 0xE ||
2048 Stream.Read(4) != 0xD)
Rafael Espindolae076b532013-11-04 16:16:24 +00002049 return Error(InvalidBitcodeSignature);
Bill Wendling34711742010-10-06 01:22:42 +00002050
2051 // We expect a number of well-defined blocks, though we don't necessarily
2052 // need to understand them all.
Chris Lattner5a4251c2013-01-20 02:13:19 +00002053 while (1) {
2054 BitstreamEntry Entry = Stream.advance();
Joe Abbeyacb61942013-02-06 22:14:06 +00002055
Chris Lattner5a4251c2013-01-20 02:13:19 +00002056 switch (Entry.Kind) {
2057 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +00002058 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00002059 case BitstreamEntry::EndBlock:
Rafael Espindolae076b532013-11-04 16:16:24 +00002060 return error_code::success();
Joe Abbeyacb61942013-02-06 22:14:06 +00002061
Chris Lattner5a4251c2013-01-20 02:13:19 +00002062 case BitstreamEntry::SubBlock:
2063 if (Entry.ID == bitc::MODULE_BLOCK_ID)
2064 return ParseModuleTriple(Triple);
Joe Abbeyacb61942013-02-06 22:14:06 +00002065
Chris Lattner5a4251c2013-01-20 02:13:19 +00002066 // Ignore other sub-blocks.
Rafael Espindolae076b532013-11-04 16:16:24 +00002067 if (Stream.SkipBlock())
2068 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00002069 continue;
Joe Abbeyacb61942013-02-06 22:14:06 +00002070
Chris Lattner5a4251c2013-01-20 02:13:19 +00002071 case BitstreamEntry::Record:
2072 Stream.skipRecord(Entry.ID);
2073 continue;
Bill Wendling34711742010-10-06 01:22:42 +00002074 }
2075 }
Bill Wendling34711742010-10-06 01:22:42 +00002076}
2077
Devang Patele8e02132009-09-18 19:26:43 +00002078/// ParseMetadataAttachment - Parse metadata attachments.
Rafael Espindolae076b532013-11-04 16:16:24 +00002079error_code BitcodeReader::ParseMetadataAttachment() {
Devang Patele8e02132009-09-18 19:26:43 +00002080 if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +00002081 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002082
Devang Patele8e02132009-09-18 19:26:43 +00002083 SmallVector<uint64_t, 64> Record;
Chris Lattner5a4251c2013-01-20 02:13:19 +00002084 while (1) {
2085 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbeyacb61942013-02-06 22:14:06 +00002086
Chris Lattner5a4251c2013-01-20 02:13:19 +00002087 switch (Entry.Kind) {
2088 case BitstreamEntry::SubBlock: // Handled for us already.
2089 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +00002090 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00002091 case BitstreamEntry::EndBlock:
Rafael Espindolae076b532013-11-04 16:16:24 +00002092 return error_code::success();
Chris Lattner5a4251c2013-01-20 02:13:19 +00002093 case BitstreamEntry::Record:
2094 // The interesting case.
Devang Patele8e02132009-09-18 19:26:43 +00002095 break;
2096 }
Chris Lattner5a4251c2013-01-20 02:13:19 +00002097
Devang Patele8e02132009-09-18 19:26:43 +00002098 // Read a metadata attachment record.
2099 Record.clear();
Chris Lattner5a4251c2013-01-20 02:13:19 +00002100 switch (Stream.readRecord(Entry.ID, Record)) {
Devang Patele8e02132009-09-18 19:26:43 +00002101 default: // Default behavior: ignore.
2102 break;
Chris Lattner9d61dd92011-06-17 17:50:30 +00002103 case bitc::METADATA_ATTACHMENT: {
Devang Patele8e02132009-09-18 19:26:43 +00002104 unsigned RecordLength = Record.size();
2105 if (Record.empty() || (RecordLength - 1) % 2 == 1)
Rafael Espindolae076b532013-11-04 16:16:24 +00002106 return Error(InvalidRecord);
Devang Patele8e02132009-09-18 19:26:43 +00002107 Instruction *Inst = InstructionList[Record[0]];
2108 for (unsigned i = 1; i != RecordLength; i = i+2) {
Devang Patela2148402009-09-28 21:14:55 +00002109 unsigned Kind = Record[i];
Dan Gohman19538d12010-07-20 21:42:28 +00002110 DenseMap<unsigned, unsigned>::iterator I =
2111 MDKindMap.find(Kind);
2112 if (I == MDKindMap.end())
Rafael Espindolae076b532013-11-04 16:16:24 +00002113 return Error(InvalidID);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002114 Value *Node = MDValueList.getValueFwdRef(Record[i+1]);
Dan Gohman19538d12010-07-20 21:42:28 +00002115 Inst->setMetadata(I->second, cast<MDNode>(Node));
Manman Ren804f0342013-09-28 00:22:27 +00002116 if (I->second == LLVMContext::MD_tbaa)
2117 InstsWithTBAATag.push_back(Inst);
Devang Patele8e02132009-09-18 19:26:43 +00002118 }
2119 break;
2120 }
2121 }
2122 }
Devang Patele8e02132009-09-18 19:26:43 +00002123}
Chris Lattner48f84872007-05-01 04:59:48 +00002124
Chris Lattner980e5aa2007-05-01 05:52:21 +00002125/// ParseFunctionBody - Lazily parse the specified function body block.
Rafael Espindolae076b532013-11-04 16:16:24 +00002126error_code BitcodeReader::ParseFunctionBody(Function *F) {
Chris Lattnere17b6582007-05-05 00:17:00 +00002127 if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +00002128 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002129
Nick Lewycky9a49f152010-02-25 08:30:17 +00002130 InstructionList.clear();
Chris Lattner980e5aa2007-05-01 05:52:21 +00002131 unsigned ModuleValueListSize = ValueList.size();
Dan Gohman69813832010-08-25 20:22:53 +00002132 unsigned ModuleMDValueListSize = MDValueList.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +00002133
Chris Lattner980e5aa2007-05-01 05:52:21 +00002134 // Add all the function arguments to the value table.
2135 for(Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
2136 ValueList.push_back(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002137
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002138 unsigned NextValueNo = ValueList.size();
Chris Lattner231cbcb2007-05-02 04:27:25 +00002139 BasicBlock *CurBB = 0;
2140 unsigned CurBBNo = 0;
2141
Chris Lattnera6245242010-04-03 02:17:50 +00002142 DebugLoc LastLoc;
Michael Ilseman407a6162012-11-15 22:34:00 +00002143
Chris Lattner980e5aa2007-05-01 05:52:21 +00002144 // Read all the records.
2145 SmallVector<uint64_t, 64> Record;
2146 while (1) {
Chris Lattner5a4251c2013-01-20 02:13:19 +00002147 BitstreamEntry Entry = Stream.advance();
Joe Abbeyacb61942013-02-06 22:14:06 +00002148
Chris Lattner5a4251c2013-01-20 02:13:19 +00002149 switch (Entry.Kind) {
2150 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +00002151 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00002152 case BitstreamEntry::EndBlock:
2153 goto OutOfRecordLoop;
Joe Abbeyacb61942013-02-06 22:14:06 +00002154
Chris Lattner5a4251c2013-01-20 02:13:19 +00002155 case BitstreamEntry::SubBlock:
2156 switch (Entry.ID) {
Chris Lattner980e5aa2007-05-01 05:52:21 +00002157 default: // Skip unknown content.
2158 if (Stream.SkipBlock())
Rafael Espindolae076b532013-11-04 16:16:24 +00002159 return Error(InvalidRecord);
Chris Lattner980e5aa2007-05-01 05:52:21 +00002160 break;
2161 case bitc::CONSTANTS_BLOCK_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00002162 if (error_code EC = ParseConstants())
2163 return EC;
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002164 NextValueNo = ValueList.size();
Chris Lattner980e5aa2007-05-01 05:52:21 +00002165 break;
2166 case bitc::VALUE_SYMTAB_BLOCK_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00002167 if (error_code EC = ParseValueSymbolTable())
2168 return EC;
Chris Lattner980e5aa2007-05-01 05:52:21 +00002169 break;
Devang Patele8e02132009-09-18 19:26:43 +00002170 case bitc::METADATA_ATTACHMENT_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00002171 if (error_code EC = ParseMetadataAttachment())
2172 return EC;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002173 break;
Victor Hernandezfab9e99c2010-01-13 19:34:08 +00002174 case bitc::METADATA_BLOCK_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00002175 if (error_code EC = ParseMetadata())
2176 return EC;
Victor Hernandezfab9e99c2010-01-13 19:34:08 +00002177 break;
Chris Lattner980e5aa2007-05-01 05:52:21 +00002178 }
2179 continue;
Joe Abbeyacb61942013-02-06 22:14:06 +00002180
Chris Lattner5a4251c2013-01-20 02:13:19 +00002181 case BitstreamEntry::Record:
2182 // The interesting case.
2183 break;
Chris Lattner980e5aa2007-05-01 05:52:21 +00002184 }
Joe Abbeyacb61942013-02-06 22:14:06 +00002185
Chris Lattner980e5aa2007-05-01 05:52:21 +00002186 // Read a record.
2187 Record.clear();
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002188 Instruction *I = 0;
Chris Lattner5a4251c2013-01-20 02:13:19 +00002189 unsigned BitCode = Stream.readRecord(Entry.ID, Record);
Dan Gohman1224c382009-07-20 21:19:07 +00002190 switch (BitCode) {
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002191 default: // Default behavior: reject
Rafael Espindolae076b532013-11-04 16:16:24 +00002192 return Error(InvalidValue);
Chris Lattner980e5aa2007-05-01 05:52:21 +00002193 case bitc::FUNC_CODE_DECLAREBLOCKS: // DECLAREBLOCKS: [nblocks]
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002194 if (Record.size() < 1 || Record[0] == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002195 return Error(InvalidRecord);
Chris Lattner980e5aa2007-05-01 05:52:21 +00002196 // Create all the basic blocks for the function.
Chris Lattnerf61e6452007-05-03 22:09:51 +00002197 FunctionBBs.resize(Record[0]);
Chris Lattner980e5aa2007-05-01 05:52:21 +00002198 for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i)
Owen Anderson1d0be152009-08-13 21:58:54 +00002199 FunctionBBs[i] = BasicBlock::Create(Context, "", F);
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002200 CurBB = FunctionBBs[0];
2201 continue;
Michael Ilseman407a6162012-11-15 22:34:00 +00002202
Chris Lattnera6245242010-04-03 02:17:50 +00002203 case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN
2204 // This record indicates that the last instruction is at the same
2205 // location as the previous instruction with a location.
2206 I = 0;
Michael Ilseman407a6162012-11-15 22:34:00 +00002207
Chris Lattnera6245242010-04-03 02:17:50 +00002208 // Get the last instruction emitted.
2209 if (CurBB && !CurBB->empty())
2210 I = &CurBB->back();
2211 else if (CurBBNo && FunctionBBs[CurBBNo-1] &&
2212 !FunctionBBs[CurBBNo-1]->empty())
2213 I = &FunctionBBs[CurBBNo-1]->back();
Michael Ilseman407a6162012-11-15 22:34:00 +00002214
Rafael Espindolae076b532013-11-04 16:16:24 +00002215 if (I == 0)
2216 return Error(InvalidRecord);
Chris Lattnera6245242010-04-03 02:17:50 +00002217 I->setDebugLoc(LastLoc);
2218 I = 0;
2219 continue;
Michael Ilseman407a6162012-11-15 22:34:00 +00002220
Chris Lattner4f6bab92011-06-17 18:17:37 +00002221 case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia]
Chris Lattnera6245242010-04-03 02:17:50 +00002222 I = 0; // Get the last instruction emitted.
2223 if (CurBB && !CurBB->empty())
2224 I = &CurBB->back();
2225 else if (CurBBNo && FunctionBBs[CurBBNo-1] &&
2226 !FunctionBBs[CurBBNo-1]->empty())
2227 I = &FunctionBBs[CurBBNo-1]->back();
2228 if (I == 0 || Record.size() < 4)
Rafael Espindolae076b532013-11-04 16:16:24 +00002229 return Error(InvalidRecord);
Michael Ilseman407a6162012-11-15 22:34:00 +00002230
Chris Lattnera6245242010-04-03 02:17:50 +00002231 unsigned Line = Record[0], Col = Record[1];
2232 unsigned ScopeID = Record[2], IAID = Record[3];
Michael Ilseman407a6162012-11-15 22:34:00 +00002233
Chris Lattnera6245242010-04-03 02:17:50 +00002234 MDNode *Scope = 0, *IA = 0;
2235 if (ScopeID) Scope = cast<MDNode>(MDValueList.getValueFwdRef(ScopeID-1));
2236 if (IAID) IA = cast<MDNode>(MDValueList.getValueFwdRef(IAID-1));
2237 LastLoc = DebugLoc::get(Line, Col, Scope, IA);
2238 I->setDebugLoc(LastLoc);
2239 I = 0;
2240 continue;
2241 }
2242
Chris Lattnerabfbf852007-05-06 00:21:25 +00002243 case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode]
2244 unsigned OpNum = 0;
2245 Value *LHS, *RHS;
2246 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002247 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
Dan Gohman1224c382009-07-20 21:19:07 +00002248 OpNum+1 > Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002249 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002250
Dan Gohman1224c382009-07-20 21:19:07 +00002251 int Opc = GetDecodedBinaryOpcode(Record[OpNum++], LHS->getType());
Rafael Espindolae076b532013-11-04 16:16:24 +00002252 if (Opc == -1)
2253 return Error(InvalidRecord);
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002254 I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
Devang Patele8e02132009-09-18 19:26:43 +00002255 InstructionList.push_back(I);
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002256 if (OpNum < Record.size()) {
2257 if (Opc == Instruction::Add ||
2258 Opc == Instruction::Sub ||
Chris Lattnerf067d582011-02-07 16:40:21 +00002259 Opc == Instruction::Mul ||
2260 Opc == Instruction::Shl) {
Dan Gohman26793ed2010-01-25 21:55:39 +00002261 if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP))
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002262 cast<BinaryOperator>(I)->setHasNoSignedWrap(true);
Dan Gohman26793ed2010-01-25 21:55:39 +00002263 if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002264 cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true);
Chris Lattner35bda892011-02-06 21:44:57 +00002265 } else if (Opc == Instruction::SDiv ||
Chris Lattnerf067d582011-02-07 16:40:21 +00002266 Opc == Instruction::UDiv ||
2267 Opc == Instruction::LShr ||
2268 Opc == Instruction::AShr) {
Chris Lattner35bda892011-02-06 21:44:57 +00002269 if (Record[OpNum] & (1 << bitc::PEO_EXACT))
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002270 cast<BinaryOperator>(I)->setIsExact(true);
Michael Ilseman495d10a2012-11-27 00:43:38 +00002271 } else if (isa<FPMathOperator>(I)) {
2272 FastMathFlags FMF;
Michael Ilseman1638b832012-12-09 21:12:04 +00002273 if (0 != (Record[OpNum] & FastMathFlags::UnsafeAlgebra))
2274 FMF.setUnsafeAlgebra();
2275 if (0 != (Record[OpNum] & FastMathFlags::NoNaNs))
2276 FMF.setNoNaNs();
2277 if (0 != (Record[OpNum] & FastMathFlags::NoInfs))
2278 FMF.setNoInfs();
2279 if (0 != (Record[OpNum] & FastMathFlags::NoSignedZeros))
2280 FMF.setNoSignedZeros();
2281 if (0 != (Record[OpNum] & FastMathFlags::AllowReciprocal))
2282 FMF.setAllowReciprocal();
Michael Ilseman495d10a2012-11-27 00:43:38 +00002283 if (FMF.any())
2284 I->setFastMathFlags(FMF);
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002285 }
Michael Ilseman495d10a2012-11-27 00:43:38 +00002286
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002287 }
Chris Lattner980e5aa2007-05-01 05:52:21 +00002288 break;
2289 }
Chris Lattnerabfbf852007-05-06 00:21:25 +00002290 case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc]
2291 unsigned OpNum = 0;
2292 Value *Op;
2293 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
2294 OpNum+2 != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002295 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002296
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002297 Type *ResTy = getTypeByID(Record[OpNum]);
Chris Lattnerabfbf852007-05-06 00:21:25 +00002298 int Opc = GetDecodedCastOpcode(Record[OpNum+1]);
2299 if (Opc == -1 || ResTy == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002300 return Error(InvalidRecord);
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002301 Instruction *Temp = 0;
2302 if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) {
2303 if (Temp) {
2304 InstructionList.push_back(Temp);
2305 CurBB->getInstList().push_back(Temp);
2306 }
2307 } else {
2308 I = CastInst::Create((Instruction::CastOps)Opc, Op, ResTy);
2309 }
Devang Patele8e02132009-09-18 19:26:43 +00002310 InstructionList.push_back(I);
Chris Lattner231cbcb2007-05-02 04:27:25 +00002311 break;
2312 }
Dan Gohmandd8004d2009-07-27 21:53:46 +00002313 case bitc::FUNC_CODE_INST_INBOUNDS_GEP:
Chris Lattner15e6d172007-05-04 19:11:41 +00002314 case bitc::FUNC_CODE_INST_GEP: { // GEP: [n x operands]
Chris Lattner7337ab92007-05-06 00:00:00 +00002315 unsigned OpNum = 0;
2316 Value *BasePtr;
2317 if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr))
Rafael Espindolae076b532013-11-04 16:16:24 +00002318 return Error(InvalidRecord);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002319
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002320 SmallVector<Value*, 16> GEPIdx;
Chris Lattner7337ab92007-05-06 00:00:00 +00002321 while (OpNum != Record.size()) {
2322 Value *Op;
2323 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolae076b532013-11-04 16:16:24 +00002324 return Error(InvalidRecord);
Chris Lattner7337ab92007-05-06 00:00:00 +00002325 GEPIdx.push_back(Op);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002326 }
2327
Jay Foada9203102011-07-25 09:48:08 +00002328 I = GetElementPtrInst::Create(BasePtr, GEPIdx);
Devang Patele8e02132009-09-18 19:26:43 +00002329 InstructionList.push_back(I);
Dan Gohmandd8004d2009-07-27 21:53:46 +00002330 if (BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP)
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002331 cast<GetElementPtrInst>(I)->setIsInBounds(true);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002332 break;
2333 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002334
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002335 case bitc::FUNC_CODE_INST_EXTRACTVAL: {
2336 // EXTRACTVAL: [opty, opval, n x indices]
Dan Gohmane4977cf2008-05-23 01:55:30 +00002337 unsigned OpNum = 0;
2338 Value *Agg;
2339 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
Rafael Espindolae076b532013-11-04 16:16:24 +00002340 return Error(InvalidRecord);
Dan Gohmane4977cf2008-05-23 01:55:30 +00002341
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002342 SmallVector<unsigned, 4> EXTRACTVALIdx;
2343 for (unsigned RecSize = Record.size();
2344 OpNum != RecSize; ++OpNum) {
2345 uint64_t Index = Record[OpNum];
2346 if ((unsigned)Index != Index)
Rafael Espindolae076b532013-11-04 16:16:24 +00002347 return Error(InvalidValue);
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002348 EXTRACTVALIdx.push_back((unsigned)Index);
Dan Gohmane4977cf2008-05-23 01:55:30 +00002349 }
2350
Jay Foadfc6d3a42011-07-13 10:26:04 +00002351 I = ExtractValueInst::Create(Agg, EXTRACTVALIdx);
Devang Patele8e02132009-09-18 19:26:43 +00002352 InstructionList.push_back(I);
Dan Gohmane4977cf2008-05-23 01:55:30 +00002353 break;
2354 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002355
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002356 case bitc::FUNC_CODE_INST_INSERTVAL: {
2357 // INSERTVAL: [opty, opval, opty, opval, n x indices]
Dan Gohmane4977cf2008-05-23 01:55:30 +00002358 unsigned OpNum = 0;
2359 Value *Agg;
2360 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
Rafael Espindolae076b532013-11-04 16:16:24 +00002361 return Error(InvalidRecord);
Dan Gohmane4977cf2008-05-23 01:55:30 +00002362 Value *Val;
2363 if (getValueTypePair(Record, OpNum, NextValueNo, Val))
Rafael Espindolae076b532013-11-04 16:16:24 +00002364 return Error(InvalidRecord);
Dan Gohmane4977cf2008-05-23 01:55:30 +00002365
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002366 SmallVector<unsigned, 4> INSERTVALIdx;
2367 for (unsigned RecSize = Record.size();
2368 OpNum != RecSize; ++OpNum) {
2369 uint64_t Index = Record[OpNum];
2370 if ((unsigned)Index != Index)
Rafael Espindolae076b532013-11-04 16:16:24 +00002371 return Error(InvalidValue);
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002372 INSERTVALIdx.push_back((unsigned)Index);
Dan Gohmane4977cf2008-05-23 01:55:30 +00002373 }
2374
Jay Foadfc6d3a42011-07-13 10:26:04 +00002375 I = InsertValueInst::Create(Agg, Val, INSERTVALIdx);
Devang Patele8e02132009-09-18 19:26:43 +00002376 InstructionList.push_back(I);
Dan Gohmane4977cf2008-05-23 01:55:30 +00002377 break;
2378 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002379
Chris Lattnerabfbf852007-05-06 00:21:25 +00002380 case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval]
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +00002381 // obsolete form of select
2382 // handles select i1 ... in old bitcode
Chris Lattnerabfbf852007-05-06 00:21:25 +00002383 unsigned OpNum = 0;
2384 Value *TrueVal, *FalseVal, *Cond;
2385 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002386 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
2387 popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond))
Rafael Espindolae076b532013-11-04 16:16:24 +00002388 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002389
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +00002390 I = SelectInst::Create(Cond, TrueVal, FalseVal);
Devang Patele8e02132009-09-18 19:26:43 +00002391 InstructionList.push_back(I);
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +00002392 break;
2393 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002394
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +00002395 case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred]
2396 // new form of select
2397 // handles select i1 or select [N x i1]
2398 unsigned OpNum = 0;
2399 Value *TrueVal, *FalseVal, *Cond;
2400 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002401 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +00002402 getValueTypePair(Record, OpNum, NextValueNo, Cond))
Rafael Espindolae076b532013-11-04 16:16:24 +00002403 return Error(InvalidRecord);
Dan Gohmanf72fb672008-09-09 01:02:47 +00002404
2405 // select condition can be either i1 or [N x i1]
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002406 if (VectorType* vector_type =
2407 dyn_cast<VectorType>(Cond->getType())) {
Dan Gohmanf72fb672008-09-09 01:02:47 +00002408 // expect <n x i1>
Daniel Dunbara279bc32009-09-20 02:20:51 +00002409 if (vector_type->getElementType() != Type::getInt1Ty(Context))
Rafael Espindolae076b532013-11-04 16:16:24 +00002410 return Error(InvalidTypeForValue);
Dan Gohmanf72fb672008-09-09 01:02:47 +00002411 } else {
2412 // expect i1
Daniel Dunbara279bc32009-09-20 02:20:51 +00002413 if (Cond->getType() != Type::getInt1Ty(Context))
Rafael Espindolae076b532013-11-04 16:16:24 +00002414 return Error(InvalidTypeForValue);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002415 }
2416
Gabor Greif051a9502008-04-06 20:25:17 +00002417 I = SelectInst::Create(Cond, TrueVal, FalseVal);
Devang Patele8e02132009-09-18 19:26:43 +00002418 InstructionList.push_back(I);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002419 break;
2420 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002421
Chris Lattner01ff65f2007-05-02 05:16:49 +00002422 case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval]
Chris Lattnerabfbf852007-05-06 00:21:25 +00002423 unsigned OpNum = 0;
2424 Value *Vec, *Idx;
2425 if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002426 popValue(Record, OpNum, NextValueNo, Type::getInt32Ty(Context), Idx))
Rafael Espindolae076b532013-11-04 16:16:24 +00002427 return Error(InvalidRecord);
Eric Christophera3500da2009-07-25 02:28:41 +00002428 I = ExtractElementInst::Create(Vec, Idx);
Devang Patele8e02132009-09-18 19:26:43 +00002429 InstructionList.push_back(I);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002430 break;
2431 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002432
Chris Lattner01ff65f2007-05-02 05:16:49 +00002433 case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval]
Chris Lattnerabfbf852007-05-06 00:21:25 +00002434 unsigned OpNum = 0;
2435 Value *Vec, *Elt, *Idx;
2436 if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002437 popValue(Record, OpNum, NextValueNo,
Chris Lattnerabfbf852007-05-06 00:21:25 +00002438 cast<VectorType>(Vec->getType())->getElementType(), Elt) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002439 popValue(Record, OpNum, NextValueNo, Type::getInt32Ty(Context), Idx))
Rafael Espindolae076b532013-11-04 16:16:24 +00002440 return Error(InvalidRecord);
Gabor Greif051a9502008-04-06 20:25:17 +00002441 I = InsertElementInst::Create(Vec, Elt, Idx);
Devang Patele8e02132009-09-18 19:26:43 +00002442 InstructionList.push_back(I);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002443 break;
2444 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002445
Chris Lattnerabfbf852007-05-06 00:21:25 +00002446 case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval]
2447 unsigned OpNum = 0;
2448 Value *Vec1, *Vec2, *Mask;
2449 if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002450 popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2))
Rafael Espindolae076b532013-11-04 16:16:24 +00002451 return Error(InvalidRecord);
Chris Lattnerabfbf852007-05-06 00:21:25 +00002452
Mon P Wangaeb06d22008-11-10 04:46:22 +00002453 if (getValueTypePair(Record, OpNum, NextValueNo, Mask))
Rafael Espindolae076b532013-11-04 16:16:24 +00002454 return Error(InvalidRecord);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002455 I = new ShuffleVectorInst(Vec1, Vec2, Mask);
Devang Patele8e02132009-09-18 19:26:43 +00002456 InstructionList.push_back(I);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002457 break;
2458 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002459
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002460 case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred]
2461 // Old form of ICmp/FCmp returning bool
2462 // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were
2463 // both legal on vectors but had different behaviour.
2464 case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred]
2465 // FCmp/ICmp returning bool or vector of bool
2466
Chris Lattner7337ab92007-05-06 00:00:00 +00002467 unsigned OpNum = 0;
2468 Value *LHS, *RHS;
2469 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002470 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
Chris Lattner7337ab92007-05-06 00:00:00 +00002471 OpNum+1 != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002472 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002473
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002474 if (LHS->getType()->isFPOrFPVectorTy())
Dan Gohman1c8a23c2009-08-25 23:17:54 +00002475 I = new FCmpInst((FCmpInst::Predicate)Record[OpNum], LHS, RHS);
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002476 else
Dan Gohman1c8a23c2009-08-25 23:17:54 +00002477 I = new ICmpInst((ICmpInst::Predicate)Record[OpNum], LHS, RHS);
Devang Patele8e02132009-09-18 19:26:43 +00002478 InstructionList.push_back(I);
Dan Gohmanf72fb672008-09-09 01:02:47 +00002479 break;
2480 }
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002481
Chris Lattner231cbcb2007-05-02 04:27:25 +00002482 case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>]
Devang Pateld9d99ff2008-02-26 01:29:32 +00002483 {
2484 unsigned Size = Record.size();
2485 if (Size == 0) {
Owen Anderson1d0be152009-08-13 21:58:54 +00002486 I = ReturnInst::Create(Context);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002487 InstructionList.push_back(I);
Devang Pateld9d99ff2008-02-26 01:29:32 +00002488 break;
Dan Gohmanfc74abf2008-07-23 00:34:11 +00002489 }
Devang Pateld9d99ff2008-02-26 01:29:32 +00002490
Dan Gohmanfc74abf2008-07-23 00:34:11 +00002491 unsigned OpNum = 0;
Chris Lattner96a74c52011-06-17 18:09:11 +00002492 Value *Op = NULL;
2493 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolae076b532013-11-04 16:16:24 +00002494 return Error(InvalidRecord);
Chris Lattner96a74c52011-06-17 18:09:11 +00002495 if (OpNum != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002496 return Error(InvalidRecord);
Dan Gohmanfc74abf2008-07-23 00:34:11 +00002497
Chris Lattner96a74c52011-06-17 18:09:11 +00002498 I = ReturnInst::Create(Context, Op);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002499 InstructionList.push_back(I);
Dan Gohmanfc74abf2008-07-23 00:34:11 +00002500 break;
Chris Lattner231cbcb2007-05-02 04:27:25 +00002501 }
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002502 case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#]
Chris Lattnerf61e6452007-05-03 22:09:51 +00002503 if (Record.size() != 1 && Record.size() != 3)
Rafael Espindolae076b532013-11-04 16:16:24 +00002504 return Error(InvalidRecord);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002505 BasicBlock *TrueDest = getBasicBlock(Record[0]);
2506 if (TrueDest == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002507 return Error(InvalidRecord);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002508
Devang Patele8e02132009-09-18 19:26:43 +00002509 if (Record.size() == 1) {
Gabor Greif051a9502008-04-06 20:25:17 +00002510 I = BranchInst::Create(TrueDest);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002511 InstructionList.push_back(I);
Devang Patele8e02132009-09-18 19:26:43 +00002512 }
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002513 else {
2514 BasicBlock *FalseDest = getBasicBlock(Record[1]);
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002515 Value *Cond = getValue(Record, 2, NextValueNo,
2516 Type::getInt1Ty(Context));
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002517 if (FalseDest == 0 || Cond == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002518 return Error(InvalidRecord);
Gabor Greif051a9502008-04-06 20:25:17 +00002519 I = BranchInst::Create(TrueDest, FalseDest, Cond);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002520 InstructionList.push_back(I);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002521 }
2522 break;
2523 }
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002524 case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...]
Michael Ilseman407a6162012-11-15 22:34:00 +00002525 // Check magic
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002526 if ((Record[0] >> 16) == SWITCH_INST_MAGIC) {
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002527 // "New" SwitchInst format with case ranges. The changes to write this
2528 // format were reverted but we still recognize bitcode that uses it.
2529 // Hopefully someday we will have support for case ranges and can use
2530 // this format again.
Michael Ilseman407a6162012-11-15 22:34:00 +00002531
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002532 Type *OpTy = getTypeByID(Record[1]);
2533 unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth();
2534
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002535 Value *Cond = getValue(Record, 2, NextValueNo, OpTy);
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002536 BasicBlock *Default = getBasicBlock(Record[3]);
2537 if (OpTy == 0 || Cond == 0 || Default == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002538 return Error(InvalidRecord);
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002539
2540 unsigned NumCases = Record[4];
Michael Ilseman407a6162012-11-15 22:34:00 +00002541
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002542 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
2543 InstructionList.push_back(SI);
Michael Ilseman407a6162012-11-15 22:34:00 +00002544
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002545 unsigned CurIdx = 5;
2546 for (unsigned i = 0; i != NumCases; ++i) {
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002547 SmallVector<ConstantInt*, 1> CaseVals;
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002548 unsigned NumItems = Record[CurIdx++];
2549 for (unsigned ci = 0; ci != NumItems; ++ci) {
2550 bool isSingleNumber = Record[CurIdx++];
Michael Ilseman407a6162012-11-15 22:34:00 +00002551
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002552 APInt Low;
2553 unsigned ActiveWords = 1;
2554 if (ValueBitWidth > 64)
2555 ActiveWords = Record[CurIdx++];
Benjamin Kramerf52aea82012-05-28 14:10:31 +00002556 Low = ReadWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
2557 ValueBitWidth);
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002558 CurIdx += ActiveWords;
Stepan Dyatkovskiy484fc932012-05-28 12:39:09 +00002559
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002560 if (!isSingleNumber) {
2561 ActiveWords = 1;
2562 if (ValueBitWidth > 64)
2563 ActiveWords = Record[CurIdx++];
2564 APInt High =
Benjamin Kramerf52aea82012-05-28 14:10:31 +00002565 ReadWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
2566 ValueBitWidth);
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002567 CurIdx += ActiveWords;
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002568
2569 // FIXME: It is not clear whether values in the range should be
2570 // compared as signed or unsigned values. The partially
2571 // implemented changes that used this format in the past used
2572 // unsigned comparisons.
2573 for ( ; Low.ule(High); ++Low)
2574 CaseVals.push_back(ConstantInt::get(Context, Low));
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002575 } else
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002576 CaseVals.push_back(ConstantInt::get(Context, Low));
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002577 }
2578 BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]);
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002579 for (SmallVector<ConstantInt*, 1>::iterator cvi = CaseVals.begin(),
2580 cve = CaseVals.end(); cvi != cve; ++cvi)
2581 SI->addCase(*cvi, DestBB);
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002582 }
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002583 I = SI;
2584 break;
2585 }
Michael Ilseman407a6162012-11-15 22:34:00 +00002586
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002587 // Old SwitchInst format without case ranges.
Michael Ilseman407a6162012-11-15 22:34:00 +00002588
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002589 if (Record.size() < 3 || (Record.size() & 1) == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002590 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002591 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002592 Value *Cond = getValue(Record, 1, NextValueNo, OpTy);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002593 BasicBlock *Default = getBasicBlock(Record[2]);
2594 if (OpTy == 0 || Cond == 0 || Default == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002595 return Error(InvalidRecord);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002596 unsigned NumCases = (Record.size()-3)/2;
Gabor Greif051a9502008-04-06 20:25:17 +00002597 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
Devang Patele8e02132009-09-18 19:26:43 +00002598 InstructionList.push_back(SI);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002599 for (unsigned i = 0, e = NumCases; i != e; ++i) {
Daniel Dunbara279bc32009-09-20 02:20:51 +00002600 ConstantInt *CaseVal =
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002601 dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy));
2602 BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]);
2603 if (CaseVal == 0 || DestBB == 0) {
2604 delete SI;
Rafael Espindolae076b532013-11-04 16:16:24 +00002605 return Error(InvalidRecord);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002606 }
2607 SI->addCase(CaseVal, DestBB);
2608 }
2609 I = SI;
2610 break;
2611 }
Chris Lattnerab21db72009-10-28 00:19:10 +00002612 case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...]
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002613 if (Record.size() < 2)
Rafael Espindolae076b532013-11-04 16:16:24 +00002614 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002615 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002616 Value *Address = getValue(Record, 1, NextValueNo, OpTy);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002617 if (OpTy == 0 || Address == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002618 return Error(InvalidRecord);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002619 unsigned NumDests = Record.size()-2;
Chris Lattnerab21db72009-10-28 00:19:10 +00002620 IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002621 InstructionList.push_back(IBI);
2622 for (unsigned i = 0, e = NumDests; i != e; ++i) {
2623 if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) {
2624 IBI->addDestination(DestBB);
2625 } else {
2626 delete IBI;
Rafael Espindolae076b532013-11-04 16:16:24 +00002627 return Error(InvalidRecord);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002628 }
2629 }
2630 I = IBI;
2631 break;
2632 }
Michael Ilseman407a6162012-11-15 22:34:00 +00002633
Duncan Sandsdc024672007-11-27 13:23:08 +00002634 case bitc::FUNC_CODE_INST_INVOKE: {
2635 // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
Rafael Espindolae076b532013-11-04 16:16:24 +00002636 if (Record.size() < 4)
2637 return Error(InvalidRecord);
Bill Wendling99faa3b2012-12-07 23:16:57 +00002638 AttributeSet PAL = getAttributes(Record[0]);
Chris Lattnera9bb7132007-05-08 05:38:01 +00002639 unsigned CCInfo = Record[1];
2640 BasicBlock *NormalBB = getBasicBlock(Record[2]);
2641 BasicBlock *UnwindBB = getBasicBlock(Record[3]);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002642
Chris Lattnera9bb7132007-05-08 05:38:01 +00002643 unsigned OpNum = 4;
Chris Lattner7337ab92007-05-06 00:00:00 +00002644 Value *Callee;
2645 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
Rafael Espindolae076b532013-11-04 16:16:24 +00002646 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002647
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002648 PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType());
2649 FunctionType *FTy = !CalleeTy ? 0 :
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002650 dyn_cast<FunctionType>(CalleeTy->getElementType());
2651
2652 // Check that the right number of fixed parameters are here.
Chris Lattner7337ab92007-05-06 00:00:00 +00002653 if (FTy == 0 || NormalBB == 0 || UnwindBB == 0 ||
2654 Record.size() < OpNum+FTy->getNumParams())
Rafael Espindolae076b532013-11-04 16:16:24 +00002655 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002656
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002657 SmallVector<Value*, 16> Ops;
Chris Lattner7337ab92007-05-06 00:00:00 +00002658 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002659 Ops.push_back(getValue(Record, OpNum, NextValueNo,
2660 FTy->getParamType(i)));
Rafael Espindolae076b532013-11-04 16:16:24 +00002661 if (Ops.back() == 0)
2662 return Error(InvalidRecord);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002663 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002664
Chris Lattner7337ab92007-05-06 00:00:00 +00002665 if (!FTy->isVarArg()) {
2666 if (Record.size() != OpNum)
Rafael Espindolae076b532013-11-04 16:16:24 +00002667 return Error(InvalidRecord);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002668 } else {
Chris Lattner7337ab92007-05-06 00:00:00 +00002669 // Read type/value pairs for varargs params.
2670 while (OpNum != Record.size()) {
2671 Value *Op;
2672 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolae076b532013-11-04 16:16:24 +00002673 return Error(InvalidRecord);
Chris Lattner7337ab92007-05-06 00:00:00 +00002674 Ops.push_back(Op);
2675 }
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002676 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002677
Jay Foada3efbb12011-07-15 08:37:34 +00002678 I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops);
Devang Patele8e02132009-09-18 19:26:43 +00002679 InstructionList.push_back(I);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002680 cast<InvokeInst>(I)->setCallingConv(
2681 static_cast<CallingConv::ID>(CCInfo));
Devang Patel05988662008-09-25 21:00:45 +00002682 cast<InvokeInst>(I)->setAttributes(PAL);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002683 break;
2684 }
Bill Wendlingdccc03b2011-07-31 06:30:59 +00002685 case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval]
2686 unsigned Idx = 0;
2687 Value *Val = 0;
2688 if (getValueTypePair(Record, Idx, NextValueNo, Val))
Rafael Espindolae076b532013-11-04 16:16:24 +00002689 return Error(InvalidRecord);
Bill Wendlingdccc03b2011-07-31 06:30:59 +00002690 I = ResumeInst::Create(Val);
Bill Wendling35726bf2011-09-01 00:50:20 +00002691 InstructionList.push_back(I);
Bill Wendlingdccc03b2011-07-31 06:30:59 +00002692 break;
2693 }
Chris Lattner231cbcb2007-05-02 04:27:25 +00002694 case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE
Owen Anderson1d0be152009-08-13 21:58:54 +00002695 I = new UnreachableInst(Context);
Devang Patele8e02132009-09-18 19:26:43 +00002696 InstructionList.push_back(I);
Chris Lattner231cbcb2007-05-02 04:27:25 +00002697 break;
Chris Lattnerabfbf852007-05-06 00:21:25 +00002698 case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...]
Chris Lattner15e6d172007-05-04 19:11:41 +00002699 if (Record.size() < 1 || ((Record.size()-1)&1))
Rafael Espindolae076b532013-11-04 16:16:24 +00002700 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002701 Type *Ty = getTypeByID(Record[0]);
Rafael Espindolae076b532013-11-04 16:16:24 +00002702 if (!Ty)
2703 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002704
Jay Foad3ecfc862011-03-30 11:28:46 +00002705 PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2);
Devang Patele8e02132009-09-18 19:26:43 +00002706 InstructionList.push_back(PN);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002707
Chris Lattner15e6d172007-05-04 19:11:41 +00002708 for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) {
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002709 Value *V;
2710 // With the new function encoding, it is possible that operands have
2711 // negative IDs (for forward references). Use a signed VBR
2712 // representation to keep the encoding small.
2713 if (UseRelativeIDs)
2714 V = getValueSigned(Record, 1+i, NextValueNo, Ty);
2715 else
2716 V = getValue(Record, 1+i, NextValueNo, Ty);
Chris Lattner15e6d172007-05-04 19:11:41 +00002717 BasicBlock *BB = getBasicBlock(Record[2+i]);
Rafael Espindolae076b532013-11-04 16:16:24 +00002718 if (!V || !BB)
2719 return Error(InvalidRecord);
Chris Lattner2a98cca2007-05-03 18:58:09 +00002720 PN->addIncoming(V, BB);
2721 }
2722 I = PN;
2723 break;
2724 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002725
Bill Wendlinge6e88262011-08-12 20:24:12 +00002726 case bitc::FUNC_CODE_INST_LANDINGPAD: {
2727 // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?]
2728 unsigned Idx = 0;
2729 if (Record.size() < 4)
Rafael Espindolae076b532013-11-04 16:16:24 +00002730 return Error(InvalidRecord);
Bill Wendlinge6e88262011-08-12 20:24:12 +00002731 Type *Ty = getTypeByID(Record[Idx++]);
Rafael Espindolae076b532013-11-04 16:16:24 +00002732 if (!Ty)
2733 return Error(InvalidRecord);
Bill Wendlinge6e88262011-08-12 20:24:12 +00002734 Value *PersFn = 0;
2735 if (getValueTypePair(Record, Idx, NextValueNo, PersFn))
Rafael Espindolae076b532013-11-04 16:16:24 +00002736 return Error(InvalidRecord);
Bill Wendlinge6e88262011-08-12 20:24:12 +00002737
2738 bool IsCleanup = !!Record[Idx++];
2739 unsigned NumClauses = Record[Idx++];
2740 LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, NumClauses);
2741 LP->setCleanup(IsCleanup);
2742 for (unsigned J = 0; J != NumClauses; ++J) {
2743 LandingPadInst::ClauseType CT =
2744 LandingPadInst::ClauseType(Record[Idx++]); (void)CT;
2745 Value *Val;
2746
2747 if (getValueTypePair(Record, Idx, NextValueNo, Val)) {
2748 delete LP;
Rafael Espindolae076b532013-11-04 16:16:24 +00002749 return Error(InvalidRecord);
Bill Wendlinge6e88262011-08-12 20:24:12 +00002750 }
2751
2752 assert((CT != LandingPadInst::Catch ||
2753 !isa<ArrayType>(Val->getType())) &&
2754 "Catch clause has a invalid type!");
2755 assert((CT != LandingPadInst::Filter ||
2756 isa<ArrayType>(Val->getType())) &&
2757 "Filter clause has invalid type!");
2758 LP->addClause(Val);
2759 }
2760
2761 I = LP;
Bill Wendling35726bf2011-09-01 00:50:20 +00002762 InstructionList.push_back(I);
Bill Wendlinge6e88262011-08-12 20:24:12 +00002763 break;
2764 }
2765
Chris Lattner96a74c52011-06-17 18:09:11 +00002766 case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]
2767 if (Record.size() != 4)
Rafael Espindolae076b532013-11-04 16:16:24 +00002768 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002769 PointerType *Ty =
Chris Lattner2a98cca2007-05-03 18:58:09 +00002770 dyn_cast_or_null<PointerType>(getTypeByID(Record[0]));
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002771 Type *OpTy = getTypeByID(Record[1]);
Chris Lattner96a74c52011-06-17 18:09:11 +00002772 Value *Size = getFnValueByID(Record[2], OpTy);
2773 unsigned Align = Record[3];
Rafael Espindolae076b532013-11-04 16:16:24 +00002774 if (!Ty || !Size)
2775 return Error(InvalidRecord);
Owen Anderson50dead02009-07-15 23:53:25 +00002776 I = new AllocaInst(Ty->getElementType(), Size, (1 << Align) >> 1);
Devang Patele8e02132009-09-18 19:26:43 +00002777 InstructionList.push_back(I);
Chris Lattner2a98cca2007-05-03 18:58:09 +00002778 break;
2779 }
Chris Lattner0579f7f2007-05-03 22:04:19 +00002780 case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol]
Chris Lattner7337ab92007-05-06 00:00:00 +00002781 unsigned OpNum = 0;
2782 Value *Op;
2783 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
2784 OpNum+2 != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002785 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002786
Chris Lattner7337ab92007-05-06 00:00:00 +00002787 I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1);
Devang Patele8e02132009-09-18 19:26:43 +00002788 InstructionList.push_back(I);
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002789 break;
Chris Lattner0579f7f2007-05-03 22:04:19 +00002790 }
Eli Friedman21006d42011-08-09 23:02:53 +00002791 case bitc::FUNC_CODE_INST_LOADATOMIC: {
2792 // LOADATOMIC: [opty, op, align, vol, ordering, synchscope]
2793 unsigned OpNum = 0;
2794 Value *Op;
2795 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
2796 OpNum+4 != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002797 return Error(InvalidRecord);
Michael Ilseman407a6162012-11-15 22:34:00 +00002798
Eli Friedman21006d42011-08-09 23:02:53 +00002799
2800 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
2801 if (Ordering == NotAtomic || Ordering == Release ||
2802 Ordering == AcquireRelease)
Rafael Espindolae076b532013-11-04 16:16:24 +00002803 return Error(InvalidRecord);
Eli Friedman21006d42011-08-09 23:02:53 +00002804 if (Ordering != NotAtomic && Record[OpNum] == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002805 return Error(InvalidRecord);
Eli Friedman21006d42011-08-09 23:02:53 +00002806 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
2807
2808 I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1,
2809 Ordering, SynchScope);
2810 InstructionList.push_back(I);
2811 break;
2812 }
Chris Lattner4f6bab92011-06-17 18:17:37 +00002813 case bitc::FUNC_CODE_INST_STORE: { // STORE2:[ptrty, ptr, val, align, vol]
Christopher Lambfe63fb92007-12-11 08:59:05 +00002814 unsigned OpNum = 0;
2815 Value *Val, *Ptr;
2816 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002817 popValue(Record, OpNum, NextValueNo,
Christopher Lambfe63fb92007-12-11 08:59:05 +00002818 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
2819 OpNum+2 != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002820 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002821
Christopher Lambfe63fb92007-12-11 08:59:05 +00002822 I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1);
Devang Patele8e02132009-09-18 19:26:43 +00002823 InstructionList.push_back(I);
Christopher Lambfe63fb92007-12-11 08:59:05 +00002824 break;
2825 }
Eli Friedman21006d42011-08-09 23:02:53 +00002826 case bitc::FUNC_CODE_INST_STOREATOMIC: {
2827 // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, synchscope]
2828 unsigned OpNum = 0;
2829 Value *Val, *Ptr;
2830 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002831 popValue(Record, OpNum, NextValueNo,
Eli Friedman21006d42011-08-09 23:02:53 +00002832 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
2833 OpNum+4 != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002834 return Error(InvalidRecord);
Eli Friedman21006d42011-08-09 23:02:53 +00002835
2836 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
Eli Friedmanc3d35982011-09-19 19:41:28 +00002837 if (Ordering == NotAtomic || Ordering == Acquire ||
Eli Friedman21006d42011-08-09 23:02:53 +00002838 Ordering == AcquireRelease)
Rafael Espindolae076b532013-11-04 16:16:24 +00002839 return Error(InvalidRecord);
Eli Friedman21006d42011-08-09 23:02:53 +00002840 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
2841 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
2844 I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1,
2845 Ordering, SynchScope);
2846 InstructionList.push_back(I);
2847 break;
2848 }
Eli Friedmanff030482011-07-28 21:48:00 +00002849 case bitc::FUNC_CODE_INST_CMPXCHG: {
2850 // CMPXCHG:[ptrty, ptr, cmp, new, vol, ordering, synchscope]
2851 unsigned OpNum = 0;
2852 Value *Ptr, *Cmp, *New;
2853 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002854 popValue(Record, OpNum, NextValueNo,
Eli Friedmanff030482011-07-28 21:48:00 +00002855 cast<PointerType>(Ptr->getType())->getElementType(), Cmp) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002856 popValue(Record, OpNum, NextValueNo,
Eli Friedmanff030482011-07-28 21:48:00 +00002857 cast<PointerType>(Ptr->getType())->getElementType(), New) ||
2858 OpNum+3 != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002859 return Error(InvalidRecord);
Eli Friedmanff030482011-07-28 21:48:00 +00002860 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+1]);
Eli Friedman21006d42011-08-09 23:02:53 +00002861 if (Ordering == NotAtomic || Ordering == Unordered)
Rafael Espindolae076b532013-11-04 16:16:24 +00002862 return Error(InvalidRecord);
Eli Friedmanff030482011-07-28 21:48:00 +00002863 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+2]);
2864 I = new AtomicCmpXchgInst(Ptr, Cmp, New, Ordering, SynchScope);
2865 cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]);
2866 InstructionList.push_back(I);
2867 break;
2868 }
2869 case bitc::FUNC_CODE_INST_ATOMICRMW: {
2870 // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, synchscope]
2871 unsigned OpNum = 0;
2872 Value *Ptr, *Val;
2873 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002874 popValue(Record, OpNum, NextValueNo,
Eli Friedmanff030482011-07-28 21:48:00 +00002875 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
2876 OpNum+4 != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002877 return Error(InvalidRecord);
Eli Friedmanff030482011-07-28 21:48:00 +00002878 AtomicRMWInst::BinOp Operation = GetDecodedRMWOperation(Record[OpNum]);
2879 if (Operation < AtomicRMWInst::FIRST_BINOP ||
2880 Operation > AtomicRMWInst::LAST_BINOP)
Rafael Espindolae076b532013-11-04 16:16:24 +00002881 return Error(InvalidRecord);
Eli Friedmanff030482011-07-28 21:48:00 +00002882 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
Eli Friedman21006d42011-08-09 23:02:53 +00002883 if (Ordering == NotAtomic || Ordering == Unordered)
Rafael Espindolae076b532013-11-04 16:16:24 +00002884 return Error(InvalidRecord);
Eli Friedmanff030482011-07-28 21:48:00 +00002885 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
2886 I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SynchScope);
2887 cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]);
2888 InstructionList.push_back(I);
2889 break;
2890 }
Eli Friedman47f35132011-07-25 23:16:38 +00002891 case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, synchscope]
2892 if (2 != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002893 return Error(InvalidRecord);
Eli Friedman47f35132011-07-25 23:16:38 +00002894 AtomicOrdering Ordering = GetDecodedOrdering(Record[0]);
2895 if (Ordering == NotAtomic || Ordering == Unordered ||
2896 Ordering == Monotonic)
Rafael Espindolae076b532013-11-04 16:16:24 +00002897 return Error(InvalidRecord);
Eli Friedman47f35132011-07-25 23:16:38 +00002898 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[1]);
2899 I = new FenceInst(Context, Ordering, SynchScope);
2900 InstructionList.push_back(I);
2901 break;
2902 }
Chris Lattner4f6bab92011-06-17 18:17:37 +00002903 case bitc::FUNC_CODE_INST_CALL: {
Duncan Sandsdc024672007-11-27 13:23:08 +00002904 // CALL: [paramattrs, cc, fnty, fnid, arg0, arg1...]
2905 if (Record.size() < 3)
Rafael Espindolae076b532013-11-04 16:16:24 +00002906 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002907
Bill Wendling99faa3b2012-12-07 23:16:57 +00002908 AttributeSet PAL = getAttributes(Record[0]);
Chris Lattnera9bb7132007-05-08 05:38:01 +00002909 unsigned CCInfo = Record[1];
Daniel Dunbara279bc32009-09-20 02:20:51 +00002910
Chris Lattnera9bb7132007-05-08 05:38:01 +00002911 unsigned OpNum = 2;
Chris Lattner7337ab92007-05-06 00:00:00 +00002912 Value *Callee;
2913 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
Rafael Espindolae076b532013-11-04 16:16:24 +00002914 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002915
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002916 PointerType *OpTy = dyn_cast<PointerType>(Callee->getType());
2917 FunctionType *FTy = 0;
Chris Lattner0579f7f2007-05-03 22:04:19 +00002918 if (OpTy) FTy = dyn_cast<FunctionType>(OpTy->getElementType());
Chris Lattner7337ab92007-05-06 00:00:00 +00002919 if (!FTy || Record.size() < FTy->getNumParams()+OpNum)
Rafael Espindolae076b532013-11-04 16:16:24 +00002920 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002921
Chris Lattner0579f7f2007-05-03 22:04:19 +00002922 SmallVector<Value*, 16> Args;
2923 // Read the fixed params.
Chris Lattner7337ab92007-05-06 00:00:00 +00002924 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
Chris Lattner1afcace2011-07-09 17:41:24 +00002925 if (FTy->getParamType(i)->isLabelTy())
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002926 Args.push_back(getBasicBlock(Record[OpNum]));
Dan Gohman9b10dfb2010-09-13 18:00:48 +00002927 else
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002928 Args.push_back(getValue(Record, OpNum, NextValueNo,
2929 FTy->getParamType(i)));
Rafael Espindolae076b532013-11-04 16:16:24 +00002930 if (Args.back() == 0)
2931 return Error(InvalidRecord);
Chris Lattner0579f7f2007-05-03 22:04:19 +00002932 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002933
Chris Lattner0579f7f2007-05-03 22:04:19 +00002934 // Read type/value pairs for varargs params.
Chris Lattner0579f7f2007-05-03 22:04:19 +00002935 if (!FTy->isVarArg()) {
Chris Lattner7337ab92007-05-06 00:00:00 +00002936 if (OpNum != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002937 return Error(InvalidRecord);
Chris Lattner0579f7f2007-05-03 22:04:19 +00002938 } else {
Chris Lattner7337ab92007-05-06 00:00:00 +00002939 while (OpNum != Record.size()) {
2940 Value *Op;
2941 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolae076b532013-11-04 16:16:24 +00002942 return Error(InvalidRecord);
Chris Lattner7337ab92007-05-06 00:00:00 +00002943 Args.push_back(Op);
Chris Lattner0579f7f2007-05-03 22:04:19 +00002944 }
2945 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002946
Jay Foada3efbb12011-07-15 08:37:34 +00002947 I = CallInst::Create(Callee, Args);
Devang Patele8e02132009-09-18 19:26:43 +00002948 InstructionList.push_back(I);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002949 cast<CallInst>(I)->setCallingConv(
2950 static_cast<CallingConv::ID>(CCInfo>>1));
Chris Lattner76520192007-05-03 22:34:03 +00002951 cast<CallInst>(I)->setTailCall(CCInfo & 1);
Devang Patel05988662008-09-25 21:00:45 +00002952 cast<CallInst>(I)->setAttributes(PAL);
Chris Lattner0579f7f2007-05-03 22:04:19 +00002953 break;
2954 }
2955 case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty]
2956 if (Record.size() < 3)
Rafael Espindolae076b532013-11-04 16:16:24 +00002957 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002958 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002959 Value *Op = getValue(Record, 1, NextValueNo, OpTy);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002960 Type *ResTy = getTypeByID(Record[2]);
Chris Lattner0579f7f2007-05-03 22:04:19 +00002961 if (!OpTy || !Op || !ResTy)
Rafael Espindolae076b532013-11-04 16:16:24 +00002962 return Error(InvalidRecord);
Chris Lattner0579f7f2007-05-03 22:04:19 +00002963 I = new VAArgInst(Op, ResTy);
Devang Patele8e02132009-09-18 19:26:43 +00002964 InstructionList.push_back(I);
Chris Lattner0579f7f2007-05-03 22:04:19 +00002965 break;
2966 }
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002967 }
2968
2969 // Add instruction to end of current BB. If there is no current BB, reject
2970 // this file.
2971 if (CurBB == 0) {
2972 delete I;
Rafael Espindolae076b532013-11-04 16:16:24 +00002973 return Error(InvalidInstructionWithNoBB);
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002974 }
2975 CurBB->getInstList().push_back(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002976
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002977 // If this was a terminator instruction, move to the next block.
2978 if (isa<TerminatorInst>(I)) {
2979 ++CurBBNo;
2980 CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : 0;
2981 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002982
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002983 // Non-void values get registered in the value table for future use.
Benjamin Kramerf0127052010-01-05 13:12:22 +00002984 if (I && !I->getType()->isVoidTy())
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002985 ValueList.AssignValue(I, NextValueNo++);
Chris Lattner980e5aa2007-05-01 05:52:21 +00002986 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002987
Chris Lattner5a4251c2013-01-20 02:13:19 +00002988OutOfRecordLoop:
Joe Abbeyacb61942013-02-06 22:14:06 +00002989
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002990 // Check the function list for unresolved values.
2991 if (Argument *A = dyn_cast<Argument>(ValueList.back())) {
2992 if (A->getParent() == 0) {
2993 // We found at least one unresolved value. Nuke them all to avoid leaks.
2994 for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){
Dan Gohman56e2a572010-08-25 20:20:21 +00002995 if ((A = dyn_cast<Argument>(ValueList[i])) && A->getParent() == 0) {
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002996 A->replaceAllUsesWith(UndefValue::get(A->getType()));
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002997 delete A;
2998 }
2999 }
Rafael Espindolae076b532013-11-04 16:16:24 +00003000 return Error(NeverResolvedValueFoundInFunction);
Chris Lattnera7c49aa2007-05-01 07:01:57 +00003001 }
Chris Lattnera7c49aa2007-05-01 07:01:57 +00003002 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003003
Dan Gohman064ff3e2010-08-25 20:23:38 +00003004 // FIXME: Check for unresolved forward-declared metadata references
3005 // and clean up leaks.
3006
Chris Lattner50b136d2009-10-28 05:53:48 +00003007 // See if anything took the address of blocks in this function. If so,
3008 // resolve them now.
Chris Lattner50b136d2009-10-28 05:53:48 +00003009 DenseMap<Function*, std::vector<BlockAddrRefTy> >::iterator BAFRI =
3010 BlockAddrFwdRefs.find(F);
3011 if (BAFRI != BlockAddrFwdRefs.end()) {
3012 std::vector<BlockAddrRefTy> &RefList = BAFRI->second;
3013 for (unsigned i = 0, e = RefList.size(); i != e; ++i) {
3014 unsigned BlockIdx = RefList[i].first;
Chris Lattnercdfc9402009-11-01 01:27:45 +00003015 if (BlockIdx >= FunctionBBs.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00003016 return Error(InvalidID);
Michael Ilseman407a6162012-11-15 22:34:00 +00003017
Chris Lattner50b136d2009-10-28 05:53:48 +00003018 GlobalVariable *FwdRef = RefList[i].second;
Chris Lattnercdfc9402009-11-01 01:27:45 +00003019 FwdRef->replaceAllUsesWith(BlockAddress::get(F, FunctionBBs[BlockIdx]));
Chris Lattner50b136d2009-10-28 05:53:48 +00003020 FwdRef->eraseFromParent();
3021 }
Michael Ilseman407a6162012-11-15 22:34:00 +00003022
Chris Lattner50b136d2009-10-28 05:53:48 +00003023 BlockAddrFwdRefs.erase(BAFRI);
3024 }
Michael Ilseman407a6162012-11-15 22:34:00 +00003025
Chris Lattner980e5aa2007-05-01 05:52:21 +00003026 // Trim the value list down to the size it was before we parsed this function.
3027 ValueList.shrinkTo(ModuleValueListSize);
Dan Gohman69813832010-08-25 20:22:53 +00003028 MDValueList.shrinkTo(ModuleMDValueListSize);
Chris Lattner980e5aa2007-05-01 05:52:21 +00003029 std::vector<BasicBlock*>().swap(FunctionBBs);
Rafael Espindolae076b532013-11-04 16:16:24 +00003030 return error_code::success();
Chris Lattner48f84872007-05-01 04:59:48 +00003031}
3032
Rafael Espindolae05744b2013-11-05 17:16:08 +00003033/// Find the function body in the bitcode stream
3034error_code BitcodeReader::FindFunctionInStream(Function *F,
Derek Schuff2ea93872012-02-06 22:30:29 +00003035 DenseMap<Function*, uint64_t>::iterator DeferredFunctionInfoIterator) {
3036 while (DeferredFunctionInfoIterator->second == 0) {
3037 if (Stream.AtEndOfStream())
Rafael Espindolae076b532013-11-04 16:16:24 +00003038 return Error(CouldNotFindFunctionInStream);
Derek Schuff2ea93872012-02-06 22:30:29 +00003039 // ParseModule will parse the next body in the stream and set its
3040 // position in the DeferredFunctionInfo map.
Rafael Espindolae05744b2013-11-05 17:16:08 +00003041 if (error_code EC = ParseModule(true))
3042 return EC;
Derek Schuff2ea93872012-02-06 22:30:29 +00003043 }
Rafael Espindolae05744b2013-11-05 17:16:08 +00003044 return error_code::success();
Derek Schuff2ea93872012-02-06 22:30:29 +00003045}
3046
Chris Lattnerb348bb82007-05-18 04:02:46 +00003047//===----------------------------------------------------------------------===//
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003048// GVMaterializer implementation
Chris Lattnerb348bb82007-05-18 04:02:46 +00003049//===----------------------------------------------------------------------===//
3050
3051
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003052bool BitcodeReader::isMaterializable(const GlobalValue *GV) const {
3053 if (const Function *F = dyn_cast<Function>(GV)) {
3054 return F->isDeclaration() &&
3055 DeferredFunctionInfo.count(const_cast<Function*>(F));
3056 }
3057 return false;
3058}
Daniel Dunbara279bc32009-09-20 02:20:51 +00003059
Rafael Espindolaaf9e8e62013-11-05 19:36:34 +00003060error_code BitcodeReader::Materialize(GlobalValue *GV) {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003061 Function *F = dyn_cast<Function>(GV);
3062 // If it's not a function or is already material, ignore the request.
Rafael Espindolaaf9e8e62013-11-05 19:36:34 +00003063 if (!F || !F->isMaterializable())
3064 return error_code::success();
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003065
3066 DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F);
Chris Lattnerb348bb82007-05-18 04:02:46 +00003067 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
Derek Schuff2ea93872012-02-06 22:30:29 +00003068 // If its position is recorded as 0, its body is somewhere in the stream
3069 // but we haven't seen it yet.
Rafael Espindolaaf9e8e62013-11-05 19:36:34 +00003070 if (DFII->second == 0 && LazyStreamer)
3071 if (error_code EC = FindFunctionInStream(F, DFII))
3072 return EC;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003073
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003074 // Move the bit stream to the saved position of the deferred function body.
3075 Stream.JumpToBit(DFII->second);
Daniel Dunbara279bc32009-09-20 02:20:51 +00003076
Rafael Espindolaaf9e8e62013-11-05 19:36:34 +00003077 if (error_code EC = ParseFunctionBody(F))
3078 return EC;
Chandler Carruth69940402007-08-04 01:51:18 +00003079
3080 // Upgrade any old intrinsic calls in the function.
3081 for (UpgradedIntrinsicMap::iterator I = UpgradedIntrinsics.begin(),
3082 E = UpgradedIntrinsics.end(); I != E; ++I) {
3083 if (I->first != I->second) {
3084 for (Value::use_iterator UI = I->first->use_begin(),
3085 UE = I->first->use_end(); UI != UE; ) {
3086 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
3087 UpgradeIntrinsicCall(CI, I->second);
3088 }
3089 }
3090 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003091
Rafael Espindolaaf9e8e62013-11-05 19:36:34 +00003092 return error_code::success();
Chris Lattnerb348bb82007-05-18 04:02:46 +00003093}
3094
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003095bool BitcodeReader::isDematerializable(const GlobalValue *GV) const {
3096 const Function *F = dyn_cast<Function>(GV);
3097 if (!F || F->isDeclaration())
3098 return false;
3099 return DeferredFunctionInfo.count(const_cast<Function*>(F));
3100}
3101
3102void BitcodeReader::Dematerialize(GlobalValue *GV) {
3103 Function *F = dyn_cast<Function>(GV);
3104 // If this function isn't dematerializable, this is a noop.
3105 if (!F || !isDematerializable(F))
Chris Lattnerb348bb82007-05-18 04:02:46 +00003106 return;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003107
Chris Lattnerb348bb82007-05-18 04:02:46 +00003108 assert(DeferredFunctionInfo.count(F) && "No info to read function later?");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003109
Chris Lattnerb348bb82007-05-18 04:02:46 +00003110 // Just forget the function body, we can remat it later.
3111 F->deleteBody();
Chris Lattnerb348bb82007-05-18 04:02:46 +00003112}
3113
3114
Rafael Espindolaaf9e8e62013-11-05 19:36:34 +00003115error_code BitcodeReader::MaterializeModule(Module *M) {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003116 assert(M == TheModule &&
3117 "Can only Materialize the Module this BitcodeReader is attached to.");
Chris Lattner714fa952009-06-16 05:15:21 +00003118 // Iterate over the module, deserializing any functions that are still on
3119 // disk.
3120 for (Module::iterator F = TheModule->begin(), E = TheModule->end();
Rafael Espindolaaf9e8e62013-11-05 19:36:34 +00003121 F != E; ++F) {
3122 if (F->isMaterializable()) {
3123 if (error_code EC = Materialize(F))
3124 return EC;
3125 }
3126 }
Derek Schuff0ffe6982012-02-29 00:07:09 +00003127 // At this point, if there are any function bodies, the current bit is
3128 // pointing to the END_BLOCK record after them. Now make sure the rest
3129 // of the bits in the module have been read.
3130 if (NextUnreadBit)
3131 ParseModule(true);
3132
Daniel Dunbara279bc32009-09-20 02:20:51 +00003133 // Upgrade any intrinsic calls that slipped through (should not happen!) and
3134 // delete the old functions to clean up. We can't do this unless the entire
3135 // module is materialized because there could always be another function body
Chandler Carruth69940402007-08-04 01:51:18 +00003136 // with calls to the old function.
3137 for (std::vector<std::pair<Function*, Function*> >::iterator I =
3138 UpgradedIntrinsics.begin(), E = UpgradedIntrinsics.end(); I != E; ++I) {
3139 if (I->first != I->second) {
3140 for (Value::use_iterator UI = I->first->use_begin(),
3141 UE = I->first->use_end(); UI != UE; ) {
3142 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
3143 UpgradeIntrinsicCall(CI, I->second);
3144 }
Chris Lattner7d9eb582009-04-01 01:43:03 +00003145 if (!I->first->use_empty())
3146 I->first->replaceAllUsesWith(I->second);
Chandler Carruth69940402007-08-04 01:51:18 +00003147 I->first->eraseFromParent();
3148 }
3149 }
3150 std::vector<std::pair<Function*, Function*> >().swap(UpgradedIntrinsics);
Devang Patele4b27562009-08-28 23:24:31 +00003151
Manman Ren804f0342013-09-28 00:22:27 +00003152 for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++)
3153 UpgradeInstWithTBAATag(InstsWithTBAATag[I]);
3154
Rafael Espindolaaf9e8e62013-11-05 19:36:34 +00003155 return error_code::success();
Chris Lattnerb348bb82007-05-18 04:02:46 +00003156}
3157
Rafael Espindolae076b532013-11-04 16:16:24 +00003158error_code BitcodeReader::InitStream() {
3159 if (LazyStreamer)
3160 return InitLazyStream();
Derek Schuff2ea93872012-02-06 22:30:29 +00003161 return InitStreamFromBuffer();
3162}
3163
Rafael Espindolae076b532013-11-04 16:16:24 +00003164error_code BitcodeReader::InitStreamFromBuffer() {
Roman Divacky5177b3a2012-09-06 15:42:13 +00003165 const unsigned char *BufPtr = (const unsigned char*)Buffer->getBufferStart();
Derek Schuff2ea93872012-02-06 22:30:29 +00003166 const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
3167
3168 if (Buffer->getBufferSize() & 3) {
3169 if (!isRawBitcode(BufPtr, BufEnd) && !isBitcodeWrapper(BufPtr, BufEnd))
Rafael Espindolae076b532013-11-04 16:16:24 +00003170 return Error(InvalidBitcodeSignature);
Derek Schuff2ea93872012-02-06 22:30:29 +00003171 else
Rafael Espindolae076b532013-11-04 16:16:24 +00003172 return Error(BitcodeStreamInvalidSize);
Derek Schuff2ea93872012-02-06 22:30:29 +00003173 }
3174
3175 // If we have a wrapper header, parse it and ignore the non-bc file contents.
3176 // The magic number is 0x0B17C0DE stored in little endian.
3177 if (isBitcodeWrapper(BufPtr, BufEnd))
3178 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
Rafael Espindolae076b532013-11-04 16:16:24 +00003179 return Error(InvalidBitcodeWrapperHeader);
Derek Schuff2ea93872012-02-06 22:30:29 +00003180
3181 StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
3182 Stream.init(*StreamFile);
3183
Rafael Espindolae076b532013-11-04 16:16:24 +00003184 return error_code::success();
Derek Schuff2ea93872012-02-06 22:30:29 +00003185}
3186
Rafael Espindolae076b532013-11-04 16:16:24 +00003187error_code BitcodeReader::InitLazyStream() {
Derek Schuff2ea93872012-02-06 22:30:29 +00003188 // Check and strip off the bitcode wrapper; BitstreamReader expects never to
3189 // see it.
3190 StreamingMemoryObject *Bytes = new StreamingMemoryObject(LazyStreamer);
3191 StreamFile.reset(new BitstreamReader(Bytes));
3192 Stream.init(*StreamFile);
3193
3194 unsigned char buf[16];
Benjamin Kramer49a6a8d2013-05-24 10:54:58 +00003195 if (Bytes->readBytes(0, 16, buf) == -1)
Rafael Espindolae076b532013-11-04 16:16:24 +00003196 return Error(BitcodeStreamInvalidSize);
Derek Schuff2ea93872012-02-06 22:30:29 +00003197
3198 if (!isBitcode(buf, buf + 16))
Rafael Espindolae076b532013-11-04 16:16:24 +00003199 return Error(InvalidBitcodeSignature);
Derek Schuff2ea93872012-02-06 22:30:29 +00003200
3201 if (isBitcodeWrapper(buf, buf + 4)) {
3202 const unsigned char *bitcodeStart = buf;
3203 const unsigned char *bitcodeEnd = buf + 16;
3204 SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
3205 Bytes->dropLeadingBytes(bitcodeStart - buf);
3206 Bytes->setKnownObjectSize(bitcodeEnd - bitcodeStart);
3207 }
Rafael Espindolae076b532013-11-04 16:16:24 +00003208 return error_code::success();
3209}
3210
3211namespace {
3212class BitcodeErrorCategoryType : public _do_message {
3213 const char *name() const LLVM_OVERRIDE {
3214 return "llvm.bitcode";
3215 }
3216 std::string message(int IE) const LLVM_OVERRIDE {
3217 BitcodeReader::ErrorType E = static_cast<BitcodeReader::ErrorType>(IE);
3218 switch (E) {
3219 case BitcodeReader::BitcodeStreamInvalidSize:
3220 return "Bitcode stream length should be >= 16 bytes and a multiple of 4";
3221 case BitcodeReader::ConflictingMETADATA_KINDRecords:
3222 return "Conflicting METADATA_KIND records";
3223 case BitcodeReader::CouldNotFindFunctionInStream:
3224 return "Could not find function in stream";
3225 case BitcodeReader::ExpectedConstant:
3226 return "Expected a constant";
3227 case BitcodeReader::InsufficientFunctionProtos:
3228 return "Insufficient function protos";
3229 case BitcodeReader::InvalidBitcodeSignature:
3230 return "Invalid bitcode signature";
3231 case BitcodeReader::InvalidBitcodeWrapperHeader:
3232 return "Invalid bitcode wrapper header";
3233 case BitcodeReader::InvalidConstantReference:
3234 return "Invalid ronstant reference";
3235 case BitcodeReader::InvalidID:
3236 return "Invalid ID";
3237 case BitcodeReader::InvalidInstructionWithNoBB:
3238 return "Invalid instruction with no BB";
3239 case BitcodeReader::InvalidRecord:
3240 return "Invalid record";
3241 case BitcodeReader::InvalidTypeForValue:
3242 return "Invalid type for value";
3243 case BitcodeReader::InvalidTYPETable:
3244 return "Invalid TYPE table";
3245 case BitcodeReader::InvalidType:
3246 return "Invalid type";
3247 case BitcodeReader::MalformedBlock:
3248 return "Malformed block";
3249 case BitcodeReader::MalformedGlobalInitializerSet:
3250 return "Malformed global initializer set";
3251 case BitcodeReader::InvalidMultipleBlocks:
3252 return "Invalid multiple blocks";
3253 case BitcodeReader::NeverResolvedValueFoundInFunction:
3254 return "Never resolved value found in function";
3255 case BitcodeReader::InvalidValue:
3256 return "Invalid value";
3257 }
Benjamin Kramera83342b2013-11-05 13:45:09 +00003258 llvm_unreachable("Unknown error type!");
Rafael Espindolae076b532013-11-04 16:16:24 +00003259 }
3260};
3261}
3262
3263const error_category &BitcodeReader::BitcodeErrorCategory() {
3264 static BitcodeErrorCategoryType O;
3265 return O;
Derek Schuff2ea93872012-02-06 22:30:29 +00003266}
Chris Lattner48f84872007-05-01 04:59:48 +00003267
Chris Lattnerc453f762007-04-29 07:54:31 +00003268//===----------------------------------------------------------------------===//
3269// External interface
3270//===----------------------------------------------------------------------===//
3271
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003272/// getLazyBitcodeModule - lazy function-at-a-time loading from a file.
Chris Lattnerc453f762007-04-29 07:54:31 +00003273///
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003274Module *llvm::getLazyBitcodeModule(MemoryBuffer *Buffer,
3275 LLVMContext& Context,
3276 std::string *ErrMsg) {
3277 Module *M = new Module(Buffer->getBufferIdentifier(), Context);
Owen Anderson8b477ed2009-07-01 16:58:40 +00003278 BitcodeReader *R = new BitcodeReader(Buffer, Context);
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003279 M->setMaterializer(R);
Rafael Espindolae076b532013-11-04 16:16:24 +00003280 if (error_code EC = R->ParseBitcodeInto(M)) {
Chris Lattnerc453f762007-04-29 07:54:31 +00003281 if (ErrMsg)
Rafael Espindolae076b532013-11-04 16:16:24 +00003282 *ErrMsg = EC.message();
Daniel Dunbara279bc32009-09-20 02:20:51 +00003283
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003284 delete M; // Also deletes R.
Chris Lattnerc453f762007-04-29 07:54:31 +00003285 return 0;
3286 }
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003287 // Have the BitcodeReader dtor delete 'Buffer'.
3288 R->setBufferOwned(true);
Rafael Espindola47f79bb2012-01-02 07:49:53 +00003289
3290 R->materializeForwardReferencedFunctions();
3291
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003292 return M;
Chris Lattnerc453f762007-04-29 07:54:31 +00003293}
3294
Derek Schuff2ea93872012-02-06 22:30:29 +00003295
3296Module *llvm::getStreamedBitcodeModule(const std::string &name,
3297 DataStreamer *streamer,
3298 LLVMContext &Context,
3299 std::string *ErrMsg) {
3300 Module *M = new Module(name, Context);
3301 BitcodeReader *R = new BitcodeReader(streamer, Context);
3302 M->setMaterializer(R);
Rafael Espindolae076b532013-11-04 16:16:24 +00003303 if (error_code EC = R->ParseBitcodeInto(M)) {
Derek Schuff2ea93872012-02-06 22:30:29 +00003304 if (ErrMsg)
Rafael Espindolae076b532013-11-04 16:16:24 +00003305 *ErrMsg = EC.message();
Derek Schuff2ea93872012-02-06 22:30:29 +00003306 delete M; // Also deletes R.
3307 return 0;
3308 }
3309 R->setBufferOwned(false); // no buffer to delete
3310 return M;
3311}
3312
Chris Lattnerc453f762007-04-29 07:54:31 +00003313/// ParseBitcodeFile - Read the specified bitcode file, returning the module.
3314/// If an error occurs, return null and fill in *ErrMsg if non-null.
Daniel Dunbara279bc32009-09-20 02:20:51 +00003315Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
Owen Anderson8b477ed2009-07-01 16:58:40 +00003316 std::string *ErrMsg){
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003317 Module *M = getLazyBitcodeModule(Buffer, Context, ErrMsg);
3318 if (!M) return 0;
Chris Lattnerb348bb82007-05-18 04:02:46 +00003319
3320 // Don't let the BitcodeReader dtor delete 'Buffer', regardless of whether
3321 // there was an error.
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003322 static_cast<BitcodeReader*>(M->getMaterializer())->setBufferOwned(false);
Daniel Dunbara279bc32009-09-20 02:20:51 +00003323
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003324 // Read in the entire module, and destroy the BitcodeReader.
3325 if (M->MaterializeAllPermanently(ErrMsg)) {
3326 delete M;
Bill Wendling34711742010-10-06 01:22:42 +00003327 return 0;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003328 }
Bill Wendling34711742010-10-06 01:22:42 +00003329
Chad Rosiercbbb0962011-12-07 21:44:12 +00003330 // TODO: Restore the use-lists to the in-memory state when the bitcode was
3331 // written. We must defer until the Module has been fully materialized.
3332
Chris Lattnerc453f762007-04-29 07:54:31 +00003333 return M;
3334}
Bill Wendling34711742010-10-06 01:22:42 +00003335
3336std::string llvm::getBitcodeTargetTriple(MemoryBuffer *Buffer,
3337 LLVMContext& Context,
3338 std::string *ErrMsg) {
3339 BitcodeReader *R = new BitcodeReader(Buffer, Context);
3340 // Don't let the BitcodeReader dtor delete 'Buffer'.
3341 R->setBufferOwned(false);
3342
3343 std::string Triple("");
Rafael Espindolae076b532013-11-04 16:16:24 +00003344 if (error_code EC = R->ParseTriple(Triple))
Bill Wendling34711742010-10-06 01:22:42 +00003345 if (ErrMsg)
Rafael Espindolae076b532013-11-04 16:16:24 +00003346 *ErrMsg = EC.message();
Bill Wendling34711742010-10-06 01:22:42 +00003347
3348 delete R;
3349 return Triple;
3350}