blob: f2039d4ed66de705fb056a74d1601ddffdf96e85 [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;
131 }
132}
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000133static int GetDecodedBinaryOpcode(unsigned Val, Type *Ty) {
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000134 switch (Val) {
135 default: return -1;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000136 case bitc::BINOP_ADD:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000137 return Ty->isFPOrFPVectorTy() ? Instruction::FAdd : Instruction::Add;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000138 case bitc::BINOP_SUB:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000139 return Ty->isFPOrFPVectorTy() ? Instruction::FSub : Instruction::Sub;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000140 case bitc::BINOP_MUL:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000141 return Ty->isFPOrFPVectorTy() ? Instruction::FMul : Instruction::Mul;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000142 case bitc::BINOP_UDIV: return Instruction::UDiv;
143 case bitc::BINOP_SDIV:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000144 return Ty->isFPOrFPVectorTy() ? Instruction::FDiv : Instruction::SDiv;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000145 case bitc::BINOP_UREM: return Instruction::URem;
146 case bitc::BINOP_SREM:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000147 return Ty->isFPOrFPVectorTy() ? Instruction::FRem : Instruction::SRem;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000148 case bitc::BINOP_SHL: return Instruction::Shl;
149 case bitc::BINOP_LSHR: return Instruction::LShr;
150 case bitc::BINOP_ASHR: return Instruction::AShr;
151 case bitc::BINOP_AND: return Instruction::And;
152 case bitc::BINOP_OR: return Instruction::Or;
153 case bitc::BINOP_XOR: return Instruction::Xor;
154 }
155}
156
Eli Friedmanff030482011-07-28 21:48:00 +0000157static AtomicRMWInst::BinOp GetDecodedRMWOperation(unsigned Val) {
158 switch (Val) {
159 default: return AtomicRMWInst::BAD_BINOP;
160 case bitc::RMW_XCHG: return AtomicRMWInst::Xchg;
161 case bitc::RMW_ADD: return AtomicRMWInst::Add;
162 case bitc::RMW_SUB: return AtomicRMWInst::Sub;
163 case bitc::RMW_AND: return AtomicRMWInst::And;
164 case bitc::RMW_NAND: return AtomicRMWInst::Nand;
165 case bitc::RMW_OR: return AtomicRMWInst::Or;
166 case bitc::RMW_XOR: return AtomicRMWInst::Xor;
167 case bitc::RMW_MAX: return AtomicRMWInst::Max;
168 case bitc::RMW_MIN: return AtomicRMWInst::Min;
169 case bitc::RMW_UMAX: return AtomicRMWInst::UMax;
170 case bitc::RMW_UMIN: return AtomicRMWInst::UMin;
171 }
172}
173
Eli Friedman47f35132011-07-25 23:16:38 +0000174static AtomicOrdering GetDecodedOrdering(unsigned Val) {
175 switch (Val) {
176 case bitc::ORDERING_NOTATOMIC: return NotAtomic;
177 case bitc::ORDERING_UNORDERED: return Unordered;
178 case bitc::ORDERING_MONOTONIC: return Monotonic;
179 case bitc::ORDERING_ACQUIRE: return Acquire;
180 case bitc::ORDERING_RELEASE: return Release;
181 case bitc::ORDERING_ACQREL: return AcquireRelease;
182 default: // Map unknown orderings to sequentially-consistent.
183 case bitc::ORDERING_SEQCST: return SequentiallyConsistent;
184 }
185}
186
187static SynchronizationScope GetDecodedSynchScope(unsigned Val) {
188 switch (Val) {
189 case bitc::SYNCHSCOPE_SINGLETHREAD: return SingleThread;
190 default: // Map unknown scopes to cross-thread.
191 case bitc::SYNCHSCOPE_CROSSTHREAD: return CrossThread;
192 }
193}
194
Gabor Greifefe65362008-05-10 08:32:32 +0000195namespace llvm {
Chris Lattner522b7b12007-04-24 05:48:56 +0000196namespace {
197 /// @brief A class for maintaining the slot number definition
198 /// as a placeholder for the actual definition for forward constants defs.
199 class ConstantPlaceHolder : public ConstantExpr {
Craig Topper86a1c322012-09-15 17:09:36 +0000200 void operator=(const ConstantPlaceHolder &) LLVM_DELETED_FUNCTION;
Gabor Greif051a9502008-04-06 20:25:17 +0000201 public:
202 // allocate space for exactly one operand
203 void *operator new(size_t s) {
204 return User::operator new(s, 1);
205 }
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000206 explicit ConstantPlaceHolder(Type *Ty, LLVMContext& Context)
Gabor Greifefe65362008-05-10 08:32:32 +0000207 : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000208 Op<0>() = UndefValue::get(Type::getInt32Ty(Context));
Chris Lattner522b7b12007-04-24 05:48:56 +0000209 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000210
Chris Lattnerea693df2008-08-21 02:34:16 +0000211 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
Chris Lattnerea693df2008-08-21 02:34:16 +0000212 static bool classof(const Value *V) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000213 return isa<ConstantExpr>(V) &&
Chris Lattnerea693df2008-08-21 02:34:16 +0000214 cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1;
215 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000216
217
Gabor Greifefe65362008-05-10 08:32:32 +0000218 /// Provide fast operand accessors
Chris Lattner46e77402009-03-31 22:55:09 +0000219 //DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Chris Lattner522b7b12007-04-24 05:48:56 +0000220 };
221}
222
Chris Lattner46e77402009-03-31 22:55:09 +0000223// FIXME: can we inherit this from ConstantExpr?
Gabor Greifefe65362008-05-10 08:32:32 +0000224template <>
Jay Foad67c619b2011-01-11 15:07:38 +0000225struct OperandTraits<ConstantPlaceHolder> :
226 public FixedNumOperandTraits<ConstantPlaceHolder, 1> {
Gabor Greifefe65362008-05-10 08:32:32 +0000227};
Gabor Greifefe65362008-05-10 08:32:32 +0000228}
229
Chris Lattner46e77402009-03-31 22:55:09 +0000230
231void BitcodeReaderValueList::AssignValue(Value *V, unsigned Idx) {
232 if (Idx == size()) {
233 push_back(V);
234 return;
235 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000236
Chris Lattner46e77402009-03-31 22:55:09 +0000237 if (Idx >= size())
238 resize(Idx+1);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000239
Chris Lattner46e77402009-03-31 22:55:09 +0000240 WeakVH &OldV = ValuePtrs[Idx];
241 if (OldV == 0) {
242 OldV = V;
243 return;
244 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000245
Chris Lattner46e77402009-03-31 22:55:09 +0000246 // Handle constants and non-constants (e.g. instrs) differently for
247 // efficiency.
248 if (Constant *PHC = dyn_cast<Constant>(&*OldV)) {
249 ResolveConstants.push_back(std::make_pair(PHC, Idx));
250 OldV = V;
251 } else {
252 // If there was a forward reference to this value, replace it.
253 Value *PrevVal = OldV;
254 OldV->replaceAllUsesWith(V);
255 delete PrevVal;
Gabor Greifefe65362008-05-10 08:32:32 +0000256 }
257}
Daniel Dunbara279bc32009-09-20 02:20:51 +0000258
Gabor Greifefe65362008-05-10 08:32:32 +0000259
Chris Lattner522b7b12007-04-24 05:48:56 +0000260Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000261 Type *Ty) {
Chris Lattner46e77402009-03-31 22:55:09 +0000262 if (Idx >= size())
Gabor Greifefe65362008-05-10 08:32:32 +0000263 resize(Idx + 1);
Chris Lattner522b7b12007-04-24 05:48:56 +0000264
Chris Lattner46e77402009-03-31 22:55:09 +0000265 if (Value *V = ValuePtrs[Idx]) {
Chris Lattnera7c49aa2007-05-01 07:01:57 +0000266 assert(Ty == V->getType() && "Type mismatch in constant table!");
267 return cast<Constant>(V);
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000268 }
Chris Lattner522b7b12007-04-24 05:48:56 +0000269
270 // Create and return a placeholder, which will later be RAUW'd.
Owen Anderson74a77812009-07-07 20:18:58 +0000271 Constant *C = new ConstantPlaceHolder(Ty, Context);
Chris Lattner46e77402009-03-31 22:55:09 +0000272 ValuePtrs[Idx] = C;
Chris Lattner522b7b12007-04-24 05:48:56 +0000273 return C;
274}
275
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000276Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, Type *Ty) {
Chris Lattner46e77402009-03-31 22:55:09 +0000277 if (Idx >= size())
Gabor Greifefe65362008-05-10 08:32:32 +0000278 resize(Idx + 1);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000279
Chris Lattner46e77402009-03-31 22:55:09 +0000280 if (Value *V = ValuePtrs[Idx]) {
Chris Lattnera7c49aa2007-05-01 07:01:57 +0000281 assert((Ty == 0 || Ty == V->getType()) && "Type mismatch in value table!");
282 return V;
283 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000284
Chris Lattner01ff65f2007-05-02 05:16:49 +0000285 // No type specified, must be invalid reference.
286 if (Ty == 0) return 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000287
Chris Lattnera7c49aa2007-05-01 07:01:57 +0000288 // Create and return a placeholder, which will later be RAUW'd.
289 Value *V = new Argument(Ty);
Chris Lattner46e77402009-03-31 22:55:09 +0000290 ValuePtrs[Idx] = V;
Chris Lattnera7c49aa2007-05-01 07:01:57 +0000291 return V;
292}
293
Chris Lattnerea693df2008-08-21 02:34:16 +0000294/// ResolveConstantForwardRefs - Once all constants are read, this method bulk
295/// resolves any forward references. The idea behind this is that we sometimes
296/// get constants (such as large arrays) which reference *many* forward ref
297/// constants. Replacing each of these causes a lot of thrashing when
298/// building/reuniquing the constant. Instead of doing this, we look at all the
299/// uses and rewrite all the place holders at once for any constant that uses
300/// a placeholder.
301void BitcodeReaderValueList::ResolveConstantForwardRefs() {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000302 // Sort the values by-pointer so that they are efficient to look up with a
Chris Lattnerea693df2008-08-21 02:34:16 +0000303 // binary search.
304 std::sort(ResolveConstants.begin(), ResolveConstants.end());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000305
Chris Lattnerea693df2008-08-21 02:34:16 +0000306 SmallVector<Constant*, 64> NewOps;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000307
Chris Lattnerea693df2008-08-21 02:34:16 +0000308 while (!ResolveConstants.empty()) {
Chris Lattner46e77402009-03-31 22:55:09 +0000309 Value *RealVal = operator[](ResolveConstants.back().second);
Chris Lattnerea693df2008-08-21 02:34:16 +0000310 Constant *Placeholder = ResolveConstants.back().first;
311 ResolveConstants.pop_back();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000312
Chris Lattnerea693df2008-08-21 02:34:16 +0000313 // Loop over all users of the placeholder, updating them to reference the
314 // new value. If they reference more than one placeholder, update them all
315 // at once.
316 while (!Placeholder->use_empty()) {
Chris Lattnerb6135a02008-08-21 17:31:45 +0000317 Value::use_iterator UI = Placeholder->use_begin();
Gabor Greifc654d1b2010-07-09 16:01:21 +0000318 User *U = *UI;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000319
Chris Lattnerea693df2008-08-21 02:34:16 +0000320 // If the using object isn't uniqued, just update the operands. This
321 // handles instructions and initializers for global variables.
Gabor Greifc654d1b2010-07-09 16:01:21 +0000322 if (!isa<Constant>(U) || isa<GlobalValue>(U)) {
Chris Lattnerb6135a02008-08-21 17:31:45 +0000323 UI.getUse().set(RealVal);
Chris Lattnerea693df2008-08-21 02:34:16 +0000324 continue;
325 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000326
Chris Lattnerea693df2008-08-21 02:34:16 +0000327 // Otherwise, we have a constant that uses the placeholder. Replace that
328 // constant with a new constant that has *all* placeholder uses updated.
Gabor Greifc654d1b2010-07-09 16:01:21 +0000329 Constant *UserC = cast<Constant>(U);
Chris Lattnerea693df2008-08-21 02:34:16 +0000330 for (User::op_iterator I = UserC->op_begin(), E = UserC->op_end();
331 I != E; ++I) {
332 Value *NewOp;
333 if (!isa<ConstantPlaceHolder>(*I)) {
334 // Not a placeholder reference.
335 NewOp = *I;
336 } else if (*I == Placeholder) {
337 // Common case is that it just references this one placeholder.
338 NewOp = RealVal;
339 } else {
340 // Otherwise, look up the placeholder in ResolveConstants.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000341 ResolveConstantsTy::iterator It =
342 std::lower_bound(ResolveConstants.begin(), ResolveConstants.end(),
Chris Lattnerea693df2008-08-21 02:34:16 +0000343 std::pair<Constant*, unsigned>(cast<Constant>(*I),
344 0));
345 assert(It != ResolveConstants.end() && It->first == *I);
Chris Lattner46e77402009-03-31 22:55:09 +0000346 NewOp = operator[](It->second);
Chris Lattnerea693df2008-08-21 02:34:16 +0000347 }
348
349 NewOps.push_back(cast<Constant>(NewOp));
350 }
351
352 // Make the new constant.
353 Constant *NewC;
354 if (ConstantArray *UserCA = dyn_cast<ConstantArray>(UserC)) {
Jay Foad26701082011-06-22 09:24:39 +0000355 NewC = ConstantArray::get(UserCA->getType(), NewOps);
Chris Lattnerea693df2008-08-21 02:34:16 +0000356 } else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) {
Chris Lattnerb065b062011-06-20 04:01:31 +0000357 NewC = ConstantStruct::get(UserCS->getType(), NewOps);
Chris Lattnerea693df2008-08-21 02:34:16 +0000358 } else if (isa<ConstantVector>(UserC)) {
Chris Lattner2ca5c862011-02-15 00:14:00 +0000359 NewC = ConstantVector::get(NewOps);
Nick Lewyckycb337992009-05-10 20:57:05 +0000360 } else {
361 assert(isa<ConstantExpr>(UserC) && "Must be a ConstantExpr.");
Jay Foadb81e4572011-04-13 13:46:01 +0000362 NewC = cast<ConstantExpr>(UserC)->getWithOperands(NewOps);
Chris Lattnerea693df2008-08-21 02:34:16 +0000363 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000364
Chris Lattnerea693df2008-08-21 02:34:16 +0000365 UserC->replaceAllUsesWith(NewC);
366 UserC->destroyConstant();
367 NewOps.clear();
368 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000369
Nick Lewyckycb337992009-05-10 20:57:05 +0000370 // Update all ValueHandles, they should be the only users at this point.
371 Placeholder->replaceAllUsesWith(RealVal);
Chris Lattnerea693df2008-08-21 02:34:16 +0000372 delete Placeholder;
373 }
374}
375
Devang Pateld5ac4042009-08-04 06:00:18 +0000376void BitcodeReaderMDValueList::AssignValue(Value *V, unsigned Idx) {
377 if (Idx == size()) {
378 push_back(V);
379 return;
380 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000381
Devang Pateld5ac4042009-08-04 06:00:18 +0000382 if (Idx >= size())
383 resize(Idx+1);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000384
Devang Pateld5ac4042009-08-04 06:00:18 +0000385 WeakVH &OldV = MDValuePtrs[Idx];
386 if (OldV == 0) {
387 OldV = V;
388 return;
389 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000390
Devang Pateld5ac4042009-08-04 06:00:18 +0000391 // If there was a forward reference to this value, replace it.
Dan Gohman489b29b2010-08-20 22:02:26 +0000392 MDNode *PrevVal = cast<MDNode>(OldV);
Devang Pateld5ac4042009-08-04 06:00:18 +0000393 OldV->replaceAllUsesWith(V);
Dan Gohman489b29b2010-08-20 22:02:26 +0000394 MDNode::deleteTemporary(PrevVal);
Devang Patelc0ff8c82009-09-03 01:38:02 +0000395 // Deleting PrevVal sets Idx value in MDValuePtrs to null. Set new
396 // value for Idx.
397 MDValuePtrs[Idx] = V;
Devang Pateld5ac4042009-08-04 06:00:18 +0000398}
399
400Value *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) {
401 if (Idx >= size())
402 resize(Idx + 1);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000403
Devang Pateld5ac4042009-08-04 06:00:18 +0000404 if (Value *V = MDValuePtrs[Idx]) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000405 assert(V->getType()->isMetadataTy() && "Type mismatch in value table!");
Devang Pateld5ac4042009-08-04 06:00:18 +0000406 return V;
407 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000408
Devang Pateld5ac4042009-08-04 06:00:18 +0000409 // Create and return a placeholder, which will later be RAUW'd.
Dmitri Gribenko5c332db2013-05-05 00:40:33 +0000410 Value *V = MDNode::getTemporary(Context, None);
Devang Pateld5ac4042009-08-04 06:00:18 +0000411 MDValuePtrs[Idx] = V;
412 return V;
413}
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000414
Chris Lattner1afcace2011-07-09 17:41:24 +0000415Type *BitcodeReader::getTypeByID(unsigned ID) {
416 // The type table size is always specified correctly.
417 if (ID >= TypeList.size())
418 return 0;
Derek Schufffccf0622012-02-06 19:03:04 +0000419
Chris Lattner1afcace2011-07-09 17:41:24 +0000420 if (Type *Ty = TypeList[ID])
421 return Ty;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000422
Chris Lattner1afcace2011-07-09 17:41:24 +0000423 // If we have a forward reference, the only possible case is when it is to a
424 // named struct. Just create a placeholder for now.
Chris Lattner3ebb6492011-08-12 18:06:37 +0000425 return TypeList[ID] = StructType::create(Context);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000426}
427
Chris Lattner1afcace2011-07-09 17:41:24 +0000428
Chris Lattner48c85b82007-05-04 03:30:17 +0000429//===----------------------------------------------------------------------===//
430// Functions for parsing blocks from the bitcode file
431//===----------------------------------------------------------------------===//
432
Bill Wendlingf9271ea2013-02-04 23:32:23 +0000433
434/// \brief This fills an AttrBuilder object with the LLVM attributes that have
435/// been decoded from the given integer. This function must stay in sync with
436/// 'encodeLLVMAttributesForBitcode'.
437static void decodeLLVMAttributesForBitcode(AttrBuilder &B,
438 uint64_t EncodedAttrs) {
439 // FIXME: Remove in 4.0.
440
441 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
442 // the bits above 31 down by 11 bits.
443 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
444 assert((!Alignment || isPowerOf2_32(Alignment)) &&
445 "Alignment must be a power of two.");
446
447 if (Alignment)
448 B.addAlignmentAttr(Alignment);
Kostya Serebryanyab39afa2013-02-11 08:13:54 +0000449 B.addRawValue(((EncodedAttrs & (0xfffffULL << 32)) >> 11) |
Bill Wendlingf9271ea2013-02-04 23:32:23 +0000450 (EncodedAttrs & 0xffff));
451}
452
Rafael Espindolae076b532013-11-04 16:16:24 +0000453error_code BitcodeReader::ParseAttributeBlock() {
Chris Lattnere17b6582007-05-05 00:17:00 +0000454 if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +0000455 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000456
Devang Patel19c87462008-09-26 22:53:05 +0000457 if (!MAttributes.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +0000458 return Error(InvalidMultipleBlocks);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000459
Chris Lattner48c85b82007-05-04 03:30:17 +0000460 SmallVector<uint64_t, 64> Record;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000461
Bill Wendling0c2f0ff2013-01-27 00:36:48 +0000462 SmallVector<AttributeSet, 8> Attrs;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000463
Chris Lattner48c85b82007-05-04 03:30:17 +0000464 // Read all the records.
465 while (1) {
Chris Lattner5a4251c2013-01-20 02:13:19 +0000466 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbeyacb61942013-02-06 22:14:06 +0000467
Chris Lattner5a4251c2013-01-20 02:13:19 +0000468 switch (Entry.Kind) {
469 case BitstreamEntry::SubBlock: // Handled for us already.
470 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +0000471 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +0000472 case BitstreamEntry::EndBlock:
Rafael Espindolae076b532013-11-04 16:16:24 +0000473 return error_code::success();
Chris Lattner5a4251c2013-01-20 02:13:19 +0000474 case BitstreamEntry::Record:
475 // The interesting case.
476 break;
Chris Lattner48c85b82007-05-04 03:30:17 +0000477 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000478
Chris Lattner48c85b82007-05-04 03:30:17 +0000479 // Read a record.
480 Record.clear();
Chris Lattner5a4251c2013-01-20 02:13:19 +0000481 switch (Stream.readRecord(Entry.ID, Record)) {
Chris Lattner48c85b82007-05-04 03:30:17 +0000482 default: // Default behavior: ignore.
483 break;
Bill Wendlingf9271ea2013-02-04 23:32:23 +0000484 case bitc::PARAMATTR_CODE_ENTRY_OLD: { // ENTRY: [paramidx0, attr0, ...]
485 // FIXME: Remove in 4.0.
Chris Lattner48c85b82007-05-04 03:30:17 +0000486 if (Record.size() & 1)
Rafael Espindolae076b532013-11-04 16:16:24 +0000487 return Error(InvalidRecord);
Chris Lattner48c85b82007-05-04 03:30:17 +0000488
Chris Lattner48c85b82007-05-04 03:30:17 +0000489 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
Bill Wendling8232ece2013-01-29 01:43:29 +0000490 AttrBuilder B;
Bill Wendlingf9271ea2013-02-04 23:32:23 +0000491 decodeLLVMAttributesForBitcode(B, Record[i+1]);
Bill Wendling8232ece2013-01-29 01:43:29 +0000492 Attrs.push_back(AttributeSet::get(Context, Record[i], B));
Devang Patel19c87462008-09-26 22:53:05 +0000493 }
Devang Patel19c87462008-09-26 22:53:05 +0000494
Bill Wendling99faa3b2012-12-07 23:16:57 +0000495 MAttributes.push_back(AttributeSet::get(Context, Attrs));
Chris Lattner48c85b82007-05-04 03:30:17 +0000496 Attrs.clear();
497 break;
498 }
Bill Wendling48fbcfe2013-02-12 08:13:50 +0000499 case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [attrgrp0, attrgrp1, ...]
500 for (unsigned i = 0, e = Record.size(); i != e; ++i)
501 Attrs.push_back(MAttributeGroups[Record[i]]);
502
503 MAttributes.push_back(AttributeSet::get(Context, Attrs));
504 Attrs.clear();
505 break;
506 }
Duncan Sands5e41f652007-11-20 14:09:29 +0000507 }
Chris Lattner48c85b82007-05-04 03:30:17 +0000508 }
509}
510
Rafael Espindolae076b532013-11-04 16:16:24 +0000511error_code BitcodeReader::ParseAttrKind(uint64_t Code,
512 Attribute::AttrKind *Kind) {
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000513 switch (Code) {
514 case bitc::ATTR_KIND_ALIGNMENT:
515 *Kind = Attribute::Alignment;
Rafael Espindolae076b532013-11-04 16:16:24 +0000516 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000517 case bitc::ATTR_KIND_ALWAYS_INLINE:
518 *Kind = Attribute::AlwaysInline;
Rafael Espindolae076b532013-11-04 16:16:24 +0000519 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000520 case bitc::ATTR_KIND_BUILTIN:
521 *Kind = Attribute::Builtin;
Rafael Espindolae076b532013-11-04 16:16:24 +0000522 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000523 case bitc::ATTR_KIND_BY_VAL:
524 *Kind = Attribute::ByVal;
Rafael Espindolae076b532013-11-04 16:16:24 +0000525 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000526 case bitc::ATTR_KIND_COLD:
527 *Kind = Attribute::Cold;
Rafael Espindolae076b532013-11-04 16:16:24 +0000528 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000529 case bitc::ATTR_KIND_INLINE_HINT:
530 *Kind = Attribute::InlineHint;
Rafael Espindolae076b532013-11-04 16:16:24 +0000531 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000532 case bitc::ATTR_KIND_IN_REG:
533 *Kind = Attribute::InReg;
Rafael Espindolae076b532013-11-04 16:16:24 +0000534 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000535 case bitc::ATTR_KIND_MIN_SIZE:
536 *Kind = Attribute::MinSize;
Rafael Espindolae076b532013-11-04 16:16:24 +0000537 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000538 case bitc::ATTR_KIND_NAKED:
539 *Kind = Attribute::Naked;
Rafael Espindolae076b532013-11-04 16:16:24 +0000540 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000541 case bitc::ATTR_KIND_NEST:
542 *Kind = Attribute::Nest;
Rafael Espindolae076b532013-11-04 16:16:24 +0000543 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000544 case bitc::ATTR_KIND_NO_ALIAS:
545 *Kind = Attribute::NoAlias;
Rafael Espindolae076b532013-11-04 16:16:24 +0000546 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000547 case bitc::ATTR_KIND_NO_BUILTIN:
548 *Kind = Attribute::NoBuiltin;
Rafael Espindolae076b532013-11-04 16:16:24 +0000549 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000550 case bitc::ATTR_KIND_NO_CAPTURE:
551 *Kind = Attribute::NoCapture;
Rafael Espindolae076b532013-11-04 16:16:24 +0000552 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000553 case bitc::ATTR_KIND_NO_DUPLICATE:
554 *Kind = Attribute::NoDuplicate;
Rafael Espindolae076b532013-11-04 16:16:24 +0000555 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000556 case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT:
557 *Kind = Attribute::NoImplicitFloat;
Rafael Espindolae076b532013-11-04 16:16:24 +0000558 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000559 case bitc::ATTR_KIND_NO_INLINE:
560 *Kind = Attribute::NoInline;
Rafael Espindolae076b532013-11-04 16:16:24 +0000561 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000562 case bitc::ATTR_KIND_NON_LAZY_BIND:
563 *Kind = Attribute::NonLazyBind;
Rafael Espindolae076b532013-11-04 16:16:24 +0000564 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000565 case bitc::ATTR_KIND_NO_RED_ZONE:
566 *Kind = Attribute::NoRedZone;
Rafael Espindolae076b532013-11-04 16:16:24 +0000567 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000568 case bitc::ATTR_KIND_NO_RETURN:
569 *Kind = Attribute::NoReturn;
Rafael Espindolae076b532013-11-04 16:16:24 +0000570 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000571 case bitc::ATTR_KIND_NO_UNWIND:
572 *Kind = Attribute::NoUnwind;
Rafael Espindolae076b532013-11-04 16:16:24 +0000573 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000574 case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE:
575 *Kind = Attribute::OptimizeForSize;
Rafael Espindolae076b532013-11-04 16:16:24 +0000576 return error_code::success();
Andrea Di Biagio5768bb82013-08-23 11:53:55 +0000577 case bitc::ATTR_KIND_OPTIMIZE_NONE:
578 *Kind = Attribute::OptimizeNone;
Rafael Espindolae076b532013-11-04 16:16:24 +0000579 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000580 case bitc::ATTR_KIND_READ_NONE:
581 *Kind = Attribute::ReadNone;
Rafael Espindolae076b532013-11-04 16:16:24 +0000582 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000583 case bitc::ATTR_KIND_READ_ONLY:
584 *Kind = Attribute::ReadOnly;
Rafael Espindolae076b532013-11-04 16:16:24 +0000585 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000586 case bitc::ATTR_KIND_RETURNED:
587 *Kind = Attribute::Returned;
Rafael Espindolae076b532013-11-04 16:16:24 +0000588 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000589 case bitc::ATTR_KIND_RETURNS_TWICE:
590 *Kind = Attribute::ReturnsTwice;
Rafael Espindolae076b532013-11-04 16:16:24 +0000591 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000592 case bitc::ATTR_KIND_S_EXT:
593 *Kind = Attribute::SExt;
Rafael Espindolae076b532013-11-04 16:16:24 +0000594 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000595 case bitc::ATTR_KIND_STACK_ALIGNMENT:
596 *Kind = Attribute::StackAlignment;
Rafael Espindolae076b532013-11-04 16:16:24 +0000597 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000598 case bitc::ATTR_KIND_STACK_PROTECT:
599 *Kind = Attribute::StackProtect;
Rafael Espindolae076b532013-11-04 16:16:24 +0000600 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000601 case bitc::ATTR_KIND_STACK_PROTECT_REQ:
602 *Kind = Attribute::StackProtectReq;
Rafael Espindolae076b532013-11-04 16:16:24 +0000603 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000604 case bitc::ATTR_KIND_STACK_PROTECT_STRONG:
605 *Kind = Attribute::StackProtectStrong;
Rafael Espindolae076b532013-11-04 16:16:24 +0000606 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000607 case bitc::ATTR_KIND_STRUCT_RET:
608 *Kind = Attribute::StructRet;
Rafael Espindolae076b532013-11-04 16:16:24 +0000609 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000610 case bitc::ATTR_KIND_SANITIZE_ADDRESS:
611 *Kind = Attribute::SanitizeAddress;
Rafael Espindolae076b532013-11-04 16:16:24 +0000612 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000613 case bitc::ATTR_KIND_SANITIZE_THREAD:
614 *Kind = Attribute::SanitizeThread;
Rafael Espindolae076b532013-11-04 16:16:24 +0000615 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000616 case bitc::ATTR_KIND_SANITIZE_MEMORY:
617 *Kind = Attribute::SanitizeMemory;
Rafael Espindolae076b532013-11-04 16:16:24 +0000618 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000619 case bitc::ATTR_KIND_UW_TABLE:
620 *Kind = Attribute::UWTable;
Rafael Espindolae076b532013-11-04 16:16:24 +0000621 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000622 case bitc::ATTR_KIND_Z_EXT:
623 *Kind = Attribute::ZExt;
Rafael Espindolae076b532013-11-04 16:16:24 +0000624 return error_code::success();
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000625 default:
Rafael Espindolae076b532013-11-04 16:16:24 +0000626 return Error(InvalidValue);
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000627 }
628}
629
Rafael Espindolae076b532013-11-04 16:16:24 +0000630error_code BitcodeReader::ParseAttributeGroupBlock() {
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000631 if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +0000632 return Error(InvalidRecord);
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000633
634 if (!MAttributeGroups.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +0000635 return Error(InvalidMultipleBlocks);
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000636
637 SmallVector<uint64_t, 64> Record;
638
639 // Read all the records.
640 while (1) {
641 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
642
643 switch (Entry.Kind) {
644 case BitstreamEntry::SubBlock: // Handled for us already.
645 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +0000646 return Error(MalformedBlock);
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000647 case BitstreamEntry::EndBlock:
Rafael Espindolae076b532013-11-04 16:16:24 +0000648 return error_code::success();
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000649 case BitstreamEntry::Record:
650 // The interesting case.
651 break;
652 }
653
654 // Read a record.
655 Record.clear();
656 switch (Stream.readRecord(Entry.ID, Record)) {
657 default: // Default behavior: ignore.
658 break;
659 case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...]
660 if (Record.size() < 3)
Rafael Espindolae076b532013-11-04 16:16:24 +0000661 return Error(InvalidRecord);
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000662
Bill Wendling04ef4be2013-02-11 22:32:29 +0000663 uint64_t GrpID = Record[0];
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000664 uint64_t Idx = Record[1]; // Index of the object this attribute refers to.
665
666 AttrBuilder B;
667 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
668 if (Record[i] == 0) { // Enum attribute
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000669 Attribute::AttrKind Kind;
Rafael Espindolae076b532013-11-04 16:16:24 +0000670 if (error_code EC = ParseAttrKind(Record[++i], &Kind))
671 return EC;
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000672
673 B.addAttribute(Kind);
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000674 } else if (Record[i] == 1) { // Align attribute
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000675 Attribute::AttrKind Kind;
Rafael Espindolae076b532013-11-04 16:16:24 +0000676 if (error_code EC = ParseAttrKind(Record[++i], &Kind))
677 return EC;
Tobias Grossere7bc5bb2013-07-26 04:16:55 +0000678 if (Kind == Attribute::Alignment)
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000679 B.addAlignmentAttr(Record[++i]);
680 else
681 B.addStackAlignmentAttr(Record[++i]);
682 } else { // String attribute
Bill Wendling04ef4be2013-02-11 22:32:29 +0000683 assert((Record[i] == 3 || Record[i] == 4) &&
684 "Invalid attribute group entry");
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000685 bool HasValue = (Record[i++] == 4);
686 SmallString<64> KindStr;
687 SmallString<64> ValStr;
688
689 while (Record[i] != 0 && i != e)
690 KindStr += Record[i++];
Bill Wendling04ef4be2013-02-11 22:32:29 +0000691 assert(Record[i] == 0 && "Kind string not null terminated");
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000692
693 if (HasValue) {
694 // Has a value associated with it.
Bill Wendling04ef4be2013-02-11 22:32:29 +0000695 ++i; // Skip the '0' that terminates the "kind" string.
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000696 while (Record[i] != 0 && i != e)
697 ValStr += Record[i++];
Bill Wendling04ef4be2013-02-11 22:32:29 +0000698 assert(Record[i] == 0 && "Value string not null terminated");
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000699 }
700
701 B.addAttribute(KindStr.str(), ValStr.str());
702 }
703 }
704
Bill Wendling04ef4be2013-02-11 22:32:29 +0000705 MAttributeGroups[GrpID] = AttributeSet::get(Context, Idx, B);
Bill Wendlingc3ba0a82013-02-10 23:24:25 +0000706 break;
707 }
708 }
709 }
710}
711
Rafael Espindolae076b532013-11-04 16:16:24 +0000712error_code BitcodeReader::ParseTypeTable() {
Chris Lattner1afcace2011-07-09 17:41:24 +0000713 if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW))
Rafael Espindolae076b532013-11-04 16:16:24 +0000714 return Error(InvalidRecord);
Derek Schufffccf0622012-02-06 19:03:04 +0000715
Chris Lattner1afcace2011-07-09 17:41:24 +0000716 return ParseTypeTableBody();
717}
Daniel Dunbara279bc32009-09-20 02:20:51 +0000718
Rafael Espindolae076b532013-11-04 16:16:24 +0000719error_code BitcodeReader::ParseTypeTableBody() {
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000720 if (!TypeList.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +0000721 return Error(InvalidMultipleBlocks);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000722
723 SmallVector<uint64_t, 64> Record;
724 unsigned NumRecords = 0;
725
Chris Lattner1afcace2011-07-09 17:41:24 +0000726 SmallString<64> TypeName;
Derek Schufffccf0622012-02-06 19:03:04 +0000727
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000728 // Read all the records for this type table.
729 while (1) {
Chris Lattner5a4251c2013-01-20 02:13:19 +0000730 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbeyacb61942013-02-06 22:14:06 +0000731
Chris Lattner5a4251c2013-01-20 02:13:19 +0000732 switch (Entry.Kind) {
733 case BitstreamEntry::SubBlock: // Handled for us already.
734 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +0000735 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +0000736 case BitstreamEntry::EndBlock:
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000737 if (NumRecords != TypeList.size())
Rafael Espindolae076b532013-11-04 16:16:24 +0000738 return Error(MalformedBlock);
739 return error_code::success();
Chris Lattner5a4251c2013-01-20 02:13:19 +0000740 case BitstreamEntry::Record:
741 // The interesting case.
742 break;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000743 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000744
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000745 // Read a record.
746 Record.clear();
Chris Lattner1afcace2011-07-09 17:41:24 +0000747 Type *ResultTy = 0;
Chris Lattner5a4251c2013-01-20 02:13:19 +0000748 switch (Stream.readRecord(Entry.ID, Record)) {
Rafael Espindolae076b532013-11-04 16:16:24 +0000749 default:
750 return Error(InvalidValue);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000751 case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
752 // TYPE_CODE_NUMENTRY contains a count of the number of types in the
753 // type list. This allows us to reserve space.
754 if (Record.size() < 1)
Rafael Espindolae076b532013-11-04 16:16:24 +0000755 return Error(InvalidRecord);
Chris Lattner1afcace2011-07-09 17:41:24 +0000756 TypeList.resize(Record[0]);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000757 continue;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000758 case bitc::TYPE_CODE_VOID: // VOID
Owen Anderson1d0be152009-08-13 21:58:54 +0000759 ResultTy = Type::getVoidTy(Context);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000760 break;
Dan Gohmance163392011-12-17 00:04:22 +0000761 case bitc::TYPE_CODE_HALF: // HALF
762 ResultTy = Type::getHalfTy(Context);
763 break;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000764 case bitc::TYPE_CODE_FLOAT: // FLOAT
Owen Anderson1d0be152009-08-13 21:58:54 +0000765 ResultTy = Type::getFloatTy(Context);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000766 break;
767 case bitc::TYPE_CODE_DOUBLE: // DOUBLE
Owen Anderson1d0be152009-08-13 21:58:54 +0000768 ResultTy = Type::getDoubleTy(Context);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000769 break;
Dale Johannesen320fc8a2007-08-03 01:03:46 +0000770 case bitc::TYPE_CODE_X86_FP80: // X86_FP80
Owen Anderson1d0be152009-08-13 21:58:54 +0000771 ResultTy = Type::getX86_FP80Ty(Context);
Dale Johannesen320fc8a2007-08-03 01:03:46 +0000772 break;
773 case bitc::TYPE_CODE_FP128: // FP128
Owen Anderson1d0be152009-08-13 21:58:54 +0000774 ResultTy = Type::getFP128Ty(Context);
Dale Johannesen320fc8a2007-08-03 01:03:46 +0000775 break;
776 case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128
Owen Anderson1d0be152009-08-13 21:58:54 +0000777 ResultTy = Type::getPPC_FP128Ty(Context);
Dale Johannesen320fc8a2007-08-03 01:03:46 +0000778 break;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000779 case bitc::TYPE_CODE_LABEL: // LABEL
Owen Anderson1d0be152009-08-13 21:58:54 +0000780 ResultTy = Type::getLabelTy(Context);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000781 break;
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000782 case bitc::TYPE_CODE_METADATA: // METADATA
Owen Anderson1d0be152009-08-13 21:58:54 +0000783 ResultTy = Type::getMetadataTy(Context);
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000784 break;
Dale Johannesenbb811a22010-09-10 20:55:01 +0000785 case bitc::TYPE_CODE_X86_MMX: // X86_MMX
786 ResultTy = Type::getX86_MMXTy(Context);
787 break;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000788 case bitc::TYPE_CODE_INTEGER: // INTEGER: [width]
789 if (Record.size() < 1)
Rafael Espindolae076b532013-11-04 16:16:24 +0000790 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000791
Owen Anderson1d0be152009-08-13 21:58:54 +0000792 ResultTy = IntegerType::get(Context, Record[0]);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000793 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000794 case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or
Christopher Lambfe63fb92007-12-11 08:59:05 +0000795 // [pointee type, address space]
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000796 if (Record.size() < 1)
Rafael Espindolae076b532013-11-04 16:16:24 +0000797 return Error(InvalidRecord);
Christopher Lambfe63fb92007-12-11 08:59:05 +0000798 unsigned AddressSpace = 0;
799 if (Record.size() == 2)
800 AddressSpace = Record[1];
Chris Lattner1afcace2011-07-09 17:41:24 +0000801 ResultTy = getTypeByID(Record[0]);
Rafael Espindolae076b532013-11-04 16:16:24 +0000802 if (ResultTy == 0)
803 return Error(InvalidType);
Chris Lattner1afcace2011-07-09 17:41:24 +0000804 ResultTy = PointerType::get(ResultTy, AddressSpace);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000805 break;
Christopher Lambfe63fb92007-12-11 08:59:05 +0000806 }
Nuno Lopesee8100d2012-05-23 15:19:39 +0000807 case bitc::TYPE_CODE_FUNCTION_OLD: {
808 // FIXME: attrid is dead, remove it in LLVM 4.0
809 // FUNCTION: [vararg, attrid, retty, paramty x N]
810 if (Record.size() < 3)
Rafael Espindolae076b532013-11-04 16:16:24 +0000811 return Error(InvalidRecord);
Nuno Lopesee8100d2012-05-23 15:19:39 +0000812 SmallVector<Type*, 8> ArgTys;
813 for (unsigned i = 3, e = Record.size(); i != e; ++i) {
814 if (Type *T = getTypeByID(Record[i]))
815 ArgTys.push_back(T);
816 else
817 break;
818 }
Michael Ilseman407a6162012-11-15 22:34:00 +0000819
Nuno Lopesee8100d2012-05-23 15:19:39 +0000820 ResultTy = getTypeByID(Record[2]);
821 if (ResultTy == 0 || ArgTys.size() < Record.size()-3)
Rafael Espindolae076b532013-11-04 16:16:24 +0000822 return Error(InvalidType);
Nuno Lopesee8100d2012-05-23 15:19:39 +0000823
824 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
825 break;
826 }
Chad Rosiercde54642011-11-03 00:14:01 +0000827 case bitc::TYPE_CODE_FUNCTION: {
828 // FUNCTION: [vararg, retty, paramty x N]
829 if (Record.size() < 2)
Rafael Espindolae076b532013-11-04 16:16:24 +0000830 return Error(InvalidRecord);
Chris Lattnerd629efa2012-01-27 03:15:49 +0000831 SmallVector<Type*, 8> ArgTys;
Chad Rosiercde54642011-11-03 00:14:01 +0000832 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
833 if (Type *T = getTypeByID(Record[i]))
834 ArgTys.push_back(T);
835 else
836 break;
837 }
Michael Ilseman407a6162012-11-15 22:34:00 +0000838
Chad Rosiercde54642011-11-03 00:14:01 +0000839 ResultTy = getTypeByID(Record[1]);
840 if (ResultTy == 0 || ArgTys.size() < Record.size()-2)
Rafael Espindolae076b532013-11-04 16:16:24 +0000841 return Error(InvalidType);
Chad Rosiercde54642011-11-03 00:14:01 +0000842
843 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
844 break;
845 }
Chris Lattner1afcace2011-07-09 17:41:24 +0000846 case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N]
Chris Lattner7108dce2007-05-06 08:21:50 +0000847 if (Record.size() < 1)
Rafael Espindolae076b532013-11-04 16:16:24 +0000848 return Error(InvalidRecord);
Chris Lattnerd629efa2012-01-27 03:15:49 +0000849 SmallVector<Type*, 8> EltTys;
Chris Lattner1afcace2011-07-09 17:41:24 +0000850 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
851 if (Type *T = getTypeByID(Record[i]))
852 EltTys.push_back(T);
853 else
854 break;
855 }
856 if (EltTys.size() != Record.size()-1)
Rafael Espindolae076b532013-11-04 16:16:24 +0000857 return Error(InvalidType);
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000858 ResultTy = StructType::get(Context, EltTys, Record[0]);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000859 break;
860 }
Chris Lattner1afcace2011-07-09 17:41:24 +0000861 case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N]
862 if (ConvertToString(Record, 0, TypeName))
Rafael Espindolae076b532013-11-04 16:16:24 +0000863 return Error(InvalidRecord);
Chris Lattner1afcace2011-07-09 17:41:24 +0000864 continue;
865
866 case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N]
867 if (Record.size() < 1)
Rafael Espindolae076b532013-11-04 16:16:24 +0000868 return Error(InvalidRecord);
Michael Ilseman407a6162012-11-15 22:34:00 +0000869
Chris Lattner1afcace2011-07-09 17:41:24 +0000870 if (NumRecords >= TypeList.size())
Rafael Espindolae076b532013-11-04 16:16:24 +0000871 return Error(InvalidTYPETable);
Michael Ilseman407a6162012-11-15 22:34:00 +0000872
Chris Lattner1afcace2011-07-09 17:41:24 +0000873 // Check to see if this was forward referenced, if so fill in the temp.
874 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
875 if (Res) {
876 Res->setName(TypeName);
877 TypeList[NumRecords] = 0;
878 } else // Otherwise, create a new struct.
Chris Lattner3ebb6492011-08-12 18:06:37 +0000879 Res = StructType::create(Context, TypeName);
Chris Lattner1afcace2011-07-09 17:41:24 +0000880 TypeName.clear();
Michael Ilseman407a6162012-11-15 22:34:00 +0000881
Chris Lattner1afcace2011-07-09 17:41:24 +0000882 SmallVector<Type*, 8> EltTys;
883 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
884 if (Type *T = getTypeByID(Record[i]))
885 EltTys.push_back(T);
886 else
887 break;
888 }
889 if (EltTys.size() != Record.size()-1)
Rafael Espindolae076b532013-11-04 16:16:24 +0000890 return Error(InvalidRecord);
Chris Lattner1afcace2011-07-09 17:41:24 +0000891 Res->setBody(EltTys, Record[0]);
892 ResultTy = Res;
893 break;
894 }
895 case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: []
896 if (Record.size() != 1)
Rafael Espindolae076b532013-11-04 16:16:24 +0000897 return Error(InvalidRecord);
Chris Lattner1afcace2011-07-09 17:41:24 +0000898
899 if (NumRecords >= TypeList.size())
Rafael Espindolae076b532013-11-04 16:16:24 +0000900 return Error(InvalidTYPETable);
Michael Ilseman407a6162012-11-15 22:34:00 +0000901
Chris Lattner1afcace2011-07-09 17:41:24 +0000902 // Check to see if this was forward referenced, if so fill in the temp.
903 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
904 if (Res) {
905 Res->setName(TypeName);
906 TypeList[NumRecords] = 0;
907 } else // Otherwise, create a new struct with no body.
Chris Lattner3ebb6492011-08-12 18:06:37 +0000908 Res = StructType::create(Context, TypeName);
Chris Lattner1afcace2011-07-09 17:41:24 +0000909 TypeName.clear();
910 ResultTy = Res;
911 break;
Michael Ilseman407a6162012-11-15 22:34:00 +0000912 }
Chris Lattner1afcace2011-07-09 17:41:24 +0000913 case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty]
914 if (Record.size() < 2)
Rafael Espindolae076b532013-11-04 16:16:24 +0000915 return Error(InvalidRecord);
Chris Lattner1afcace2011-07-09 17:41:24 +0000916 if ((ResultTy = getTypeByID(Record[1])))
917 ResultTy = ArrayType::get(ResultTy, Record[0]);
918 else
Rafael Espindolae076b532013-11-04 16:16:24 +0000919 return Error(InvalidType);
Chris Lattner1afcace2011-07-09 17:41:24 +0000920 break;
921 case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty]
922 if (Record.size() < 2)
Rafael Espindolae076b532013-11-04 16:16:24 +0000923 return Error(InvalidRecord);
Chris Lattner1afcace2011-07-09 17:41:24 +0000924 if ((ResultTy = getTypeByID(Record[1])))
925 ResultTy = VectorType::get(ResultTy, Record[0]);
926 else
Rafael Espindolae076b532013-11-04 16:16:24 +0000927 return Error(InvalidType);
Chris Lattner1afcace2011-07-09 17:41:24 +0000928 break;
929 }
930
931 if (NumRecords >= TypeList.size())
Rafael Espindolae076b532013-11-04 16:16:24 +0000932 return Error(InvalidTYPETable);
Chris Lattner1afcace2011-07-09 17:41:24 +0000933 assert(ResultTy && "Didn't read a type?");
934 assert(TypeList[NumRecords] == 0 && "Already read type?");
935 TypeList[NumRecords++] = ResultTy;
936 }
937}
938
Rafael Espindolae076b532013-11-04 16:16:24 +0000939error_code BitcodeReader::ParseValueSymbolTable() {
Chris Lattnere17b6582007-05-05 00:17:00 +0000940 if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +0000941 return Error(InvalidRecord);
Chris Lattner0b2482a2007-04-23 21:26:05 +0000942
943 SmallVector<uint64_t, 64> Record;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000944
Chris Lattner0b2482a2007-04-23 21:26:05 +0000945 // Read all the records for this value table.
946 SmallString<128> ValueName;
947 while (1) {
Chris Lattner5a4251c2013-01-20 02:13:19 +0000948 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbeyacb61942013-02-06 22:14:06 +0000949
Chris Lattner5a4251c2013-01-20 02:13:19 +0000950 switch (Entry.Kind) {
951 case BitstreamEntry::SubBlock: // Handled for us already.
952 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +0000953 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +0000954 case BitstreamEntry::EndBlock:
Rafael Espindolae076b532013-11-04 16:16:24 +0000955 return error_code::success();
Chris Lattner5a4251c2013-01-20 02:13:19 +0000956 case BitstreamEntry::Record:
957 // The interesting case.
958 break;
Chris Lattner0b2482a2007-04-23 21:26:05 +0000959 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000960
Chris Lattner0b2482a2007-04-23 21:26:05 +0000961 // Read a record.
962 Record.clear();
Chris Lattner5a4251c2013-01-20 02:13:19 +0000963 switch (Stream.readRecord(Entry.ID, Record)) {
Chris Lattner0b2482a2007-04-23 21:26:05 +0000964 default: // Default behavior: unknown type.
965 break;
Chris Lattner15e6d172007-05-04 19:11:41 +0000966 case bitc::VST_CODE_ENTRY: { // VST_ENTRY: [valueid, namechar x N]
Chris Lattner0b2482a2007-04-23 21:26:05 +0000967 if (ConvertToString(Record, 1, ValueName))
Rafael Espindolae076b532013-11-04 16:16:24 +0000968 return Error(InvalidRecord);
Chris Lattner0b2482a2007-04-23 21:26:05 +0000969 unsigned ValueID = Record[0];
970 if (ValueID >= ValueList.size())
Rafael Espindolae076b532013-11-04 16:16:24 +0000971 return Error(InvalidRecord);
Chris Lattner0b2482a2007-04-23 21:26:05 +0000972 Value *V = ValueList[ValueID];
Daniel Dunbara279bc32009-09-20 02:20:51 +0000973
Daniel Dunbar3f53fa92009-07-26 00:34:27 +0000974 V->setName(StringRef(ValueName.data(), ValueName.size()));
Chris Lattner0b2482a2007-04-23 21:26:05 +0000975 ValueName.clear();
976 break;
Reid Spencerc8f8a242007-05-04 01:43:33 +0000977 }
Bill Wendling5d7a5a42011-04-10 23:18:04 +0000978 case bitc::VST_CODE_BBENTRY: {
Chris Lattnere825ed52007-05-03 22:18:21 +0000979 if (ConvertToString(Record, 1, ValueName))
Rafael Espindolae076b532013-11-04 16:16:24 +0000980 return Error(InvalidRecord);
Chris Lattnere825ed52007-05-03 22:18:21 +0000981 BasicBlock *BB = getBasicBlock(Record[0]);
982 if (BB == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +0000983 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000984
Daniel Dunbar3f53fa92009-07-26 00:34:27 +0000985 BB->setName(StringRef(ValueName.data(), ValueName.size()));
Chris Lattnere825ed52007-05-03 22:18:21 +0000986 ValueName.clear();
987 break;
Chris Lattner0b2482a2007-04-23 21:26:05 +0000988 }
Reid Spencerc8f8a242007-05-04 01:43:33 +0000989 }
Chris Lattner0b2482a2007-04-23 21:26:05 +0000990 }
991}
992
Rafael Espindolae076b532013-11-04 16:16:24 +0000993error_code BitcodeReader::ParseMetadata() {
Devang Patel23598502010-01-11 18:52:33 +0000994 unsigned NextMDValueNo = MDValueList.size();
Devang Patele54abc92009-07-22 17:43:22 +0000995
996 if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +0000997 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000998
Devang Patele54abc92009-07-22 17:43:22 +0000999 SmallVector<uint64_t, 64> Record;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001000
Devang Patele54abc92009-07-22 17:43:22 +00001001 // Read all the records.
1002 while (1) {
Chris Lattner5a4251c2013-01-20 02:13:19 +00001003 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbeyacb61942013-02-06 22:14:06 +00001004
Chris Lattner5a4251c2013-01-20 02:13:19 +00001005 switch (Entry.Kind) {
1006 case BitstreamEntry::SubBlock: // Handled for us already.
1007 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +00001008 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00001009 case BitstreamEntry::EndBlock:
Rafael Espindolae076b532013-11-04 16:16:24 +00001010 return error_code::success();
Chris Lattner5a4251c2013-01-20 02:13:19 +00001011 case BitstreamEntry::Record:
1012 // The interesting case.
1013 break;
Devang Patele54abc92009-07-22 17:43:22 +00001014 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001015
Victor Hernandez24e64df2010-01-10 07:14:18 +00001016 bool IsFunctionLocal = false;
Devang Patele54abc92009-07-22 17:43:22 +00001017 // Read a record.
1018 Record.clear();
Chris Lattner5a4251c2013-01-20 02:13:19 +00001019 unsigned Code = Stream.readRecord(Entry.ID, Record);
Dan Gohman9b10dfb2010-09-13 18:00:48 +00001020 switch (Code) {
Devang Patele54abc92009-07-22 17:43:22 +00001021 default: // Default behavior: ignore.
1022 break;
Devang Patelaa993142009-07-29 22:34:41 +00001023 case bitc::METADATA_NAME: {
Chris Lattner1ca114a2013-01-20 02:54:05 +00001024 // Read name of the named metadata.
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001025 SmallString<8> Name(Record.begin(), Record.end());
Devang Patelaa993142009-07-29 22:34:41 +00001026 Record.clear();
1027 Code = Stream.ReadCode();
1028
Chris Lattner9d61dd92011-06-17 17:50:30 +00001029 // METADATA_NAME is always followed by METADATA_NAMED_NODE.
Chris Lattner5a4251c2013-01-20 02:13:19 +00001030 unsigned NextBitCode = Stream.readRecord(Code, Record);
Chris Lattner9d61dd92011-06-17 17:50:30 +00001031 assert(NextBitCode == bitc::METADATA_NAMED_NODE); (void)NextBitCode;
Devang Patelaa993142009-07-29 22:34:41 +00001032
1033 // Read named metadata elements.
1034 unsigned Size = Record.size();
Dan Gohman17aa92c2010-07-21 23:38:33 +00001035 NamedMDNode *NMD = TheModule->getOrInsertNamedMetadata(Name);
Devang Patelaa993142009-07-29 22:34:41 +00001036 for (unsigned i = 0; i != Size; ++i) {
Chris Lattner70644e92010-01-09 02:02:37 +00001037 MDNode *MD = dyn_cast<MDNode>(MDValueList.getValueFwdRef(Record[i]));
1038 if (MD == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00001039 return Error(InvalidRecord);
Dan Gohman17aa92c2010-07-21 23:38:33 +00001040 NMD->addOperand(MD);
Devang Patelaa993142009-07-29 22:34:41 +00001041 }
Devang Patelaa993142009-07-29 22:34:41 +00001042 break;
1043 }
Chris Lattner9d61dd92011-06-17 17:50:30 +00001044 case bitc::METADATA_FN_NODE:
Victor Hernandez24e64df2010-01-10 07:14:18 +00001045 IsFunctionLocal = true;
1046 // fall-through
Chris Lattner9d61dd92011-06-17 17:50:30 +00001047 case bitc::METADATA_NODE: {
Dan Gohmanac809752010-07-13 19:33:27 +00001048 if (Record.size() % 2 == 1)
Rafael Espindolae076b532013-11-04 16:16:24 +00001049 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001050
Devang Patel104cf9e2009-07-23 01:07:34 +00001051 unsigned Size = Record.size();
1052 SmallVector<Value*, 8> Elts;
1053 for (unsigned i = 0; i != Size; i += 2) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001054 Type *Ty = getTypeByID(Record[i]);
Rafael Espindolae076b532013-11-04 16:16:24 +00001055 if (!Ty)
1056 return Error(InvalidRecord);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001057 if (Ty->isMetadataTy())
Devang Pateld5ac4042009-08-04 06:00:18 +00001058 Elts.push_back(MDValueList.getValueFwdRef(Record[i+1]));
Benjamin Kramerf0127052010-01-05 13:12:22 +00001059 else if (!Ty->isVoidTy())
Devang Patel104cf9e2009-07-23 01:07:34 +00001060 Elts.push_back(ValueList.getValueFwdRef(Record[i+1], Ty));
1061 else
1062 Elts.push_back(NULL);
1063 }
Jay Foadec9186b2011-04-21 19:59:31 +00001064 Value *V = MDNode::getWhenValsUnresolved(Context, Elts, IsFunctionLocal);
Victor Hernandez24e64df2010-01-10 07:14:18 +00001065 IsFunctionLocal = false;
Devang Patel23598502010-01-11 18:52:33 +00001066 MDValueList.AssignValue(V, NextMDValueNo++);
Devang Patel104cf9e2009-07-23 01:07:34 +00001067 break;
1068 }
Devang Patele54abc92009-07-22 17:43:22 +00001069 case bitc::METADATA_STRING: {
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001070 SmallString<8> String(Record.begin(), Record.end());
1071 Value *V = MDString::get(Context, String);
Devang Patel23598502010-01-11 18:52:33 +00001072 MDValueList.AssignValue(V, NextMDValueNo++);
Devang Patele54abc92009-07-22 17:43:22 +00001073 break;
1074 }
Devang Patele8e02132009-09-18 19:26:43 +00001075 case bitc::METADATA_KIND: {
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001076 if (Record.size() < 2)
Rafael Espindolae076b532013-11-04 16:16:24 +00001077 return Error(InvalidRecord);
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001078
Devang Patela2148402009-09-28 21:14:55 +00001079 unsigned Kind = Record[0];
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001080 SmallString<8> Name(Record.begin()+1, Record.end());
1081
Chris Lattner08113472009-12-29 09:01:33 +00001082 unsigned NewKind = TheModule->getMDKindID(Name.str());
Dan Gohman19538d12010-07-20 21:42:28 +00001083 if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second)
Rafael Espindolae076b532013-11-04 16:16:24 +00001084 return Error(ConflictingMETADATA_KINDRecords);
Devang Patele8e02132009-09-18 19:26:43 +00001085 break;
1086 }
Devang Patele54abc92009-07-22 17:43:22 +00001087 }
1088 }
1089}
1090
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00001091/// decodeSignRotatedValue - Decode a signed value stored with the sign bit in
Chris Lattner0eef0802007-04-24 04:04:35 +00001092/// the LSB for dense VBR encoding.
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00001093uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) {
Chris Lattner0eef0802007-04-24 04:04:35 +00001094 if ((V & 1) == 0)
1095 return V >> 1;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001096 if (V != 1)
Chris Lattner0eef0802007-04-24 04:04:35 +00001097 return -(V >> 1);
1098 // There is no such thing as -0 with integers. "-0" really means MININT.
1099 return 1ULL << 63;
1100}
1101
Chris Lattner07d98b42007-04-26 02:46:40 +00001102/// ResolveGlobalAndAliasInits - Resolve all of the initializers for global
1103/// values and aliases that we can.
Rafael Espindolae076b532013-11-04 16:16:24 +00001104error_code BitcodeReader::ResolveGlobalAndAliasInits() {
Chris Lattner07d98b42007-04-26 02:46:40 +00001105 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist;
1106 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist;
Peter Collingbourne1e3037f2013-09-16 01:08:15 +00001107 std::vector<std::pair<Function*, unsigned> > FunctionPrefixWorklist;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001108
Chris Lattner07d98b42007-04-26 02:46:40 +00001109 GlobalInitWorklist.swap(GlobalInits);
1110 AliasInitWorklist.swap(AliasInits);
Peter Collingbourne1e3037f2013-09-16 01:08:15 +00001111 FunctionPrefixWorklist.swap(FunctionPrefixes);
Chris Lattner07d98b42007-04-26 02:46:40 +00001112
1113 while (!GlobalInitWorklist.empty()) {
Chris Lattner198f34a2007-04-26 03:27:58 +00001114 unsigned ValID = GlobalInitWorklist.back().second;
Chris Lattner07d98b42007-04-26 02:46:40 +00001115 if (ValID >= ValueList.size()) {
1116 // Not ready to resolve this yet, it requires something later in the file.
Chris Lattner198f34a2007-04-26 03:27:58 +00001117 GlobalInits.push_back(GlobalInitWorklist.back());
Chris Lattner07d98b42007-04-26 02:46:40 +00001118 } else {
1119 if (Constant *C = dyn_cast<Constant>(ValueList[ValID]))
1120 GlobalInitWorklist.back().first->setInitializer(C);
1121 else
Rafael Espindolae076b532013-11-04 16:16:24 +00001122 return Error(ExpectedConstant);
Chris Lattner07d98b42007-04-26 02:46:40 +00001123 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001124 GlobalInitWorklist.pop_back();
Chris Lattner07d98b42007-04-26 02:46:40 +00001125 }
1126
1127 while (!AliasInitWorklist.empty()) {
1128 unsigned ValID = AliasInitWorklist.back().second;
1129 if (ValID >= ValueList.size()) {
1130 AliasInits.push_back(AliasInitWorklist.back());
1131 } else {
1132 if (Constant *C = dyn_cast<Constant>(ValueList[ValID]))
Anton Korobeynikov7dde0ff2007-04-28 14:57:59 +00001133 AliasInitWorklist.back().first->setAliasee(C);
Chris Lattner07d98b42007-04-26 02:46:40 +00001134 else
Rafael Espindolae076b532013-11-04 16:16:24 +00001135 return Error(ExpectedConstant);
Chris Lattner07d98b42007-04-26 02:46:40 +00001136 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001137 AliasInitWorklist.pop_back();
Chris Lattner07d98b42007-04-26 02:46:40 +00001138 }
Peter Collingbourne1e3037f2013-09-16 01:08:15 +00001139
1140 while (!FunctionPrefixWorklist.empty()) {
1141 unsigned ValID = FunctionPrefixWorklist.back().second;
1142 if (ValID >= ValueList.size()) {
1143 FunctionPrefixes.push_back(FunctionPrefixWorklist.back());
1144 } else {
1145 if (Constant *C = dyn_cast<Constant>(ValueList[ValID]))
1146 FunctionPrefixWorklist.back().first->setPrefixData(C);
1147 else
Rafael Espindolae076b532013-11-04 16:16:24 +00001148 return Error(ExpectedConstant);
Peter Collingbourne1e3037f2013-09-16 01:08:15 +00001149 }
1150 FunctionPrefixWorklist.pop_back();
1151 }
1152
Rafael Espindolae076b532013-11-04 16:16:24 +00001153 return error_code::success();
Chris Lattner07d98b42007-04-26 02:46:40 +00001154}
1155
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001156static APInt ReadWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) {
1157 SmallVector<uint64_t, 8> Words(Vals.size());
1158 std::transform(Vals.begin(), Vals.end(), Words.begin(),
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00001159 BitcodeReader::decodeSignRotatedValue);
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001160
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00001161 return APInt(TypeBits, Words);
1162}
1163
Rafael Espindolae076b532013-11-04 16:16:24 +00001164error_code BitcodeReader::ParseConstants() {
Chris Lattnere17b6582007-05-05 00:17:00 +00001165 if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +00001166 return Error(InvalidRecord);
Chris Lattnere16504e2007-04-24 03:30:34 +00001167
1168 SmallVector<uint64_t, 64> Record;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001169
Chris Lattnere16504e2007-04-24 03:30:34 +00001170 // Read all the records for this value table.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001171 Type *CurTy = Type::getInt32Ty(Context);
Chris Lattner522b7b12007-04-24 05:48:56 +00001172 unsigned NextCstNo = ValueList.size();
Chris Lattnere16504e2007-04-24 03:30:34 +00001173 while (1) {
Chris Lattner5a4251c2013-01-20 02:13:19 +00001174 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbeyacb61942013-02-06 22:14:06 +00001175
Chris Lattner5a4251c2013-01-20 02:13:19 +00001176 switch (Entry.Kind) {
1177 case BitstreamEntry::SubBlock: // Handled for us already.
1178 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +00001179 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00001180 case BitstreamEntry::EndBlock:
1181 if (NextCstNo != ValueList.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001182 return Error(InvalidConstantReference);
Joe Abbeyacb61942013-02-06 22:14:06 +00001183
Chris Lattner5a4251c2013-01-20 02:13:19 +00001184 // Once all the constants have been read, go through and resolve forward
1185 // references.
1186 ValueList.ResolveConstantForwardRefs();
Rafael Espindolae076b532013-11-04 16:16:24 +00001187 return error_code::success();
Chris Lattner5a4251c2013-01-20 02:13:19 +00001188 case BitstreamEntry::Record:
1189 // The interesting case.
Chris Lattnerea693df2008-08-21 02:34:16 +00001190 break;
Chris Lattnere16504e2007-04-24 03:30:34 +00001191 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001192
Chris Lattnere16504e2007-04-24 03:30:34 +00001193 // Read a record.
1194 Record.clear();
1195 Value *V = 0;
Chris Lattner5a4251c2013-01-20 02:13:19 +00001196 unsigned BitCode = Stream.readRecord(Entry.ID, Record);
Dan Gohman1224c382009-07-20 21:19:07 +00001197 switch (BitCode) {
Chris Lattnere16504e2007-04-24 03:30:34 +00001198 default: // Default behavior: unknown constant
1199 case bitc::CST_CODE_UNDEF: // UNDEF
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001200 V = UndefValue::get(CurTy);
Chris Lattnere16504e2007-04-24 03:30:34 +00001201 break;
1202 case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid]
1203 if (Record.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +00001204 return Error(InvalidRecord);
Chris Lattnere16504e2007-04-24 03:30:34 +00001205 if (Record[0] >= TypeList.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001206 return Error(InvalidRecord);
Chris Lattnere16504e2007-04-24 03:30:34 +00001207 CurTy = TypeList[Record[0]];
Chris Lattner0eef0802007-04-24 04:04:35 +00001208 continue; // Skip the ValueList manipulation.
Chris Lattnere16504e2007-04-24 03:30:34 +00001209 case bitc::CST_CODE_NULL: // NULL
Owen Andersona7235ea2009-07-31 20:28:14 +00001210 V = Constant::getNullValue(CurTy);
Chris Lattnere16504e2007-04-24 03:30:34 +00001211 break;
1212 case bitc::CST_CODE_INTEGER: // INTEGER: [intval]
Duncan Sands1df98592010-02-16 11:11:14 +00001213 if (!CurTy->isIntegerTy() || Record.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +00001214 return Error(InvalidRecord);
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00001215 V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0]));
Chris Lattner0eef0802007-04-24 04:04:35 +00001216 break;
Chris Lattner15e6d172007-05-04 19:11:41 +00001217 case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
Duncan Sands1df98592010-02-16 11:11:14 +00001218 if (!CurTy->isIntegerTy() || Record.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +00001219 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001220
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001221 APInt VInt = ReadWideAPInt(Record,
1222 cast<IntegerType>(CurTy)->getBitWidth());
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00001223 V = ConstantInt::get(Context, VInt);
Michael Ilseman407a6162012-11-15 22:34:00 +00001224
Chris Lattner0eef0802007-04-24 04:04:35 +00001225 break;
1226 }
Dale Johannesen3f6eb742007-09-11 18:32:33 +00001227 case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval]
Chris Lattner0eef0802007-04-24 04:04:35 +00001228 if (Record.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +00001229 return Error(InvalidRecord);
Dan Gohmance163392011-12-17 00:04:22 +00001230 if (CurTy->isHalfTy())
Tim Northover0a29cb02013-01-22 09:46:31 +00001231 V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf,
1232 APInt(16, (uint16_t)Record[0])));
Dan Gohmance163392011-12-17 00:04:22 +00001233 else if (CurTy->isFloatTy())
Tim Northover0a29cb02013-01-22 09:46:31 +00001234 V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle,
1235 APInt(32, (uint32_t)Record[0])));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001236 else if (CurTy->isDoubleTy())
Tim Northover0a29cb02013-01-22 09:46:31 +00001237 V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble,
1238 APInt(64, Record[0])));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001239 else if (CurTy->isX86_FP80Ty()) {
Dale Johannesen1b25cb22009-03-23 21:16:53 +00001240 // Bits are not stored the same way as a normal i80 APInt, compensate.
1241 uint64_t Rearrange[2];
1242 Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);
1243 Rearrange[1] = Record[0] >> 48;
Tim Northover0a29cb02013-01-22 09:46:31 +00001244 V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended,
1245 APInt(80, Rearrange)));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001246 } else if (CurTy->isFP128Ty())
Tim Northover0a29cb02013-01-22 09:46:31 +00001247 V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad,
1248 APInt(128, Record)));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001249 else if (CurTy->isPPC_FP128Ty())
Tim Northover0a29cb02013-01-22 09:46:31 +00001250 V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble,
1251 APInt(128, Record)));
Chris Lattnere16504e2007-04-24 03:30:34 +00001252 else
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001253 V = UndefValue::get(CurTy);
Chris Lattnere16504e2007-04-24 03:30:34 +00001254 break;
Dale Johannesen3f6eb742007-09-11 18:32:33 +00001255 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001256
Chris Lattner15e6d172007-05-04 19:11:41 +00001257 case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]
1258 if (Record.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +00001259 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001260
Chris Lattner15e6d172007-05-04 19:11:41 +00001261 unsigned Size = Record.size();
Chris Lattnerd629efa2012-01-27 03:15:49 +00001262 SmallVector<Constant*, 16> Elts;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001263
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001264 if (StructType *STy = dyn_cast<StructType>(CurTy)) {
Chris Lattner522b7b12007-04-24 05:48:56 +00001265 for (unsigned i = 0; i != Size; ++i)
Chris Lattner15e6d172007-05-04 19:11:41 +00001266 Elts.push_back(ValueList.getConstantFwdRef(Record[i],
Chris Lattner522b7b12007-04-24 05:48:56 +00001267 STy->getElementType(i)));
Owen Anderson8fa33382009-07-27 22:29:26 +00001268 V = ConstantStruct::get(STy, Elts);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001269 } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) {
1270 Type *EltTy = ATy->getElementType();
Chris Lattner522b7b12007-04-24 05:48:56 +00001271 for (unsigned i = 0; i != Size; ++i)
Chris Lattner15e6d172007-05-04 19:11:41 +00001272 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
Owen Anderson1fd70962009-07-28 18:32:17 +00001273 V = ConstantArray::get(ATy, Elts);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001274 } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) {
1275 Type *EltTy = VTy->getElementType();
Chris Lattner522b7b12007-04-24 05:48:56 +00001276 for (unsigned i = 0; i != Size; ++i)
Chris Lattner15e6d172007-05-04 19:11:41 +00001277 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00001278 V = ConstantVector::get(Elts);
Chris Lattner522b7b12007-04-24 05:48:56 +00001279 } else {
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001280 V = UndefValue::get(CurTy);
Chris Lattner522b7b12007-04-24 05:48:56 +00001281 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001282 break;
1283 }
Chris Lattner2237f842012-02-05 02:41:35 +00001284 case bitc::CST_CODE_STRING: // STRING: [values]
Chris Lattnercb3d91b2007-05-06 00:53:07 +00001285 case bitc::CST_CODE_CSTRING: { // CSTRING: [values]
1286 if (Record.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +00001287 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001288
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001289 SmallString<16> Elts(Record.begin(), Record.end());
Chris Lattner2237f842012-02-05 02:41:35 +00001290 V = ConstantDataArray::getString(Context, Elts,
1291 BitCode == bitc::CST_CODE_CSTRING);
Chris Lattnercb3d91b2007-05-06 00:53:07 +00001292 break;
1293 }
Chris Lattnerd408f062012-01-30 00:51:16 +00001294 case bitc::CST_CODE_DATA: {// DATA: [n x value]
1295 if (Record.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +00001296 return Error(InvalidRecord);
Michael Ilseman407a6162012-11-15 22:34:00 +00001297
Chris Lattnerd408f062012-01-30 00:51:16 +00001298 Type *EltTy = cast<SequentialType>(CurTy)->getElementType();
1299 unsigned Size = Record.size();
Michael Ilseman407a6162012-11-15 22:34:00 +00001300
Chris Lattnerd408f062012-01-30 00:51:16 +00001301 if (EltTy->isIntegerTy(8)) {
1302 SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end());
1303 if (isa<VectorType>(CurTy))
1304 V = ConstantDataVector::get(Context, Elts);
1305 else
1306 V = ConstantDataArray::get(Context, Elts);
1307 } else if (EltTy->isIntegerTy(16)) {
1308 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
1309 if (isa<VectorType>(CurTy))
1310 V = ConstantDataVector::get(Context, Elts);
1311 else
1312 V = ConstantDataArray::get(Context, Elts);
1313 } else if (EltTy->isIntegerTy(32)) {
1314 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
1315 if (isa<VectorType>(CurTy))
1316 V = ConstantDataVector::get(Context, Elts);
1317 else
1318 V = ConstantDataArray::get(Context, Elts);
1319 } else if (EltTy->isIntegerTy(64)) {
1320 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
1321 if (isa<VectorType>(CurTy))
1322 V = ConstantDataVector::get(Context, Elts);
1323 else
1324 V = ConstantDataArray::get(Context, Elts);
1325 } else if (EltTy->isFloatTy()) {
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001326 SmallVector<float, 16> Elts(Size);
1327 std::transform(Record.begin(), Record.end(), Elts.begin(), BitsToFloat);
Chris Lattnerd408f062012-01-30 00:51:16 +00001328 if (isa<VectorType>(CurTy))
1329 V = ConstantDataVector::get(Context, Elts);
1330 else
1331 V = ConstantDataArray::get(Context, Elts);
1332 } else if (EltTy->isDoubleTy()) {
Benjamin Kramerf52aea82012-05-28 14:10:31 +00001333 SmallVector<double, 16> Elts(Size);
1334 std::transform(Record.begin(), Record.end(), Elts.begin(),
1335 BitsToDouble);
Chris Lattnerd408f062012-01-30 00:51:16 +00001336 if (isa<VectorType>(CurTy))
1337 V = ConstantDataVector::get(Context, Elts);
1338 else
1339 V = ConstantDataArray::get(Context, Elts);
1340 } else {
Rafael Espindolae076b532013-11-04 16:16:24 +00001341 return Error(InvalidTypeForValue);
Chris Lattnerd408f062012-01-30 00:51:16 +00001342 }
1343 break;
1344 }
1345
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001346 case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval]
Rafael Espindolae076b532013-11-04 16:16:24 +00001347 if (Record.size() < 3)
1348 return Error(InvalidRecord);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001349 int Opc = GetDecodedBinaryOpcode(Record[0], CurTy);
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001350 if (Opc < 0) {
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001351 V = UndefValue::get(CurTy); // Unknown binop.
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001352 } else {
1353 Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy);
1354 Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy);
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001355 unsigned Flags = 0;
1356 if (Record.size() >= 4) {
1357 if (Opc == Instruction::Add ||
1358 Opc == Instruction::Sub ||
Chris Lattnerf067d582011-02-07 16:40:21 +00001359 Opc == Instruction::Mul ||
1360 Opc == Instruction::Shl) {
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001361 if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP))
1362 Flags |= OverflowingBinaryOperator::NoSignedWrap;
1363 if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
1364 Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
Chris Lattner35bda892011-02-06 21:44:57 +00001365 } else if (Opc == Instruction::SDiv ||
Chris Lattnerf067d582011-02-07 16:40:21 +00001366 Opc == Instruction::UDiv ||
1367 Opc == Instruction::LShr ||
1368 Opc == Instruction::AShr) {
Chris Lattner35bda892011-02-06 21:44:57 +00001369 if (Record[3] & (1 << bitc::PEO_EXACT))
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001370 Flags |= SDivOperator::IsExact;
1371 }
1372 }
1373 V = ConstantExpr::get(Opc, LHS, RHS, Flags);
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001374 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001375 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001376 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001377 case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval]
Rafael Espindolae076b532013-11-04 16:16:24 +00001378 if (Record.size() < 3)
1379 return Error(InvalidRecord);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001380 int Opc = GetDecodedCastOpcode(Record[0]);
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001381 if (Opc < 0) {
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001382 V = UndefValue::get(CurTy); // Unknown cast.
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001383 } else {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001384 Type *OpTy = getTypeByID(Record[1]);
Rafael Espindolae076b532013-11-04 16:16:24 +00001385 if (!OpTy)
1386 return Error(InvalidRecord);
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001387 Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy);
Owen Andersonbaf3c402009-07-29 18:55:55 +00001388 V = ConstantExpr::getCast(Opc, Op, CurTy);
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001389 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001390 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001391 }
Dan Gohmandd8004d2009-07-27 21:53:46 +00001392 case bitc::CST_CODE_CE_INBOUNDS_GEP:
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001393 case bitc::CST_CODE_CE_GEP: { // CE_GEP: [n x operands]
Rafael Espindolae076b532013-11-04 16:16:24 +00001394 if (Record.size() & 1)
1395 return Error(InvalidRecord);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001396 SmallVector<Constant*, 16> Elts;
Chris Lattner15e6d172007-05-04 19:11:41 +00001397 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001398 Type *ElTy = getTypeByID(Record[i]);
Rafael Espindolae076b532013-11-04 16:16:24 +00001399 if (!ElTy)
1400 return Error(InvalidRecord);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001401 Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], ElTy));
1402 }
Jay Foaddab3d292011-07-21 14:31:17 +00001403 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
Jay Foad4b5e2072011-07-21 15:15:37 +00001404 V = ConstantExpr::getGetElementPtr(Elts[0], Indices,
1405 BitCode ==
1406 bitc::CST_CODE_CE_INBOUNDS_GEP);
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001407 break;
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001408 }
Joe Abbey405b6502013-09-12 22:02:31 +00001409 case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#]
Rafael Espindolae076b532013-11-04 16:16:24 +00001410 if (Record.size() < 3)
1411 return Error(InvalidRecord);
Joe Abbey405b6502013-09-12 22:02:31 +00001412
1413 Type *SelectorTy = Type::getInt1Ty(Context);
1414
1415 // If CurTy is a vector of length n, then Record[0] must be a <n x i1>
1416 // vector. Otherwise, it must be a single bit.
1417 if (VectorType *VTy = dyn_cast<VectorType>(CurTy))
1418 SelectorTy = VectorType::get(Type::getInt1Ty(Context),
1419 VTy->getNumElements());
1420
1421 V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0],
1422 SelectorTy),
1423 ValueList.getConstantFwdRef(Record[1],CurTy),
1424 ValueList.getConstantFwdRef(Record[2],CurTy));
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001425 break;
Joe Abbey405b6502013-09-12 22:02:31 +00001426 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001427 case bitc::CST_CODE_CE_EXTRACTELT: { // CE_EXTRACTELT: [opty, opval, opval]
Rafael Espindolae076b532013-11-04 16:16:24 +00001428 if (Record.size() < 3)
1429 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001430 VectorType *OpTy =
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001431 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
Rafael Espindolae076b532013-11-04 16:16:24 +00001432 if (OpTy == 0)
1433 return Error(InvalidRecord);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001434 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
Joe Abbey170a15e2012-11-25 15:23:39 +00001435 Constant *Op1 = ValueList.getConstantFwdRef(Record[2],
Joe Abbeye46b14a2012-11-19 19:22:55 +00001436 Type::getInt32Ty(Context));
Owen Andersonbaf3c402009-07-29 18:55:55 +00001437 V = ConstantExpr::getExtractElement(Op0, Op1);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001438 break;
1439 }
1440 case bitc::CST_CODE_CE_INSERTELT: { // CE_INSERTELT: [opval, opval, opval]
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001441 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001442 if (Record.size() < 3 || OpTy == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00001443 return Error(InvalidRecord);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001444 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
1445 Constant *Op1 = ValueList.getConstantFwdRef(Record[1],
1446 OpTy->getElementType());
Joe Abbey170a15e2012-11-25 15:23:39 +00001447 Constant *Op2 = ValueList.getConstantFwdRef(Record[2],
Joe Abbeye46b14a2012-11-19 19:22:55 +00001448 Type::getInt32Ty(Context));
Owen Andersonbaf3c402009-07-29 18:55:55 +00001449 V = ConstantExpr::getInsertElement(Op0, Op1, Op2);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001450 break;
1451 }
1452 case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval]
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001453 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001454 if (Record.size() < 3 || OpTy == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00001455 return Error(InvalidRecord);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001456 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
1457 Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001458 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
Owen Anderson74a77812009-07-07 20:18:58 +00001459 OpTy->getNumElements());
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001460 Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy);
Owen Andersonbaf3c402009-07-29 18:55:55 +00001461 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001462 break;
1463 }
Nate Begeman0f123cf2009-02-12 21:28:33 +00001464 case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval]
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001465 VectorType *RTy = dyn_cast<VectorType>(CurTy);
1466 VectorType *OpTy =
Duncan Sandsf22b7462010-10-28 15:47:26 +00001467 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
Nate Begeman0f123cf2009-02-12 21:28:33 +00001468 if (Record.size() < 4 || RTy == 0 || OpTy == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00001469 return Error(InvalidRecord);
Nate Begeman0f123cf2009-02-12 21:28:33 +00001470 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
1471 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001472 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
Owen Anderson74a77812009-07-07 20:18:58 +00001473 RTy->getNumElements());
Nate Begeman0f123cf2009-02-12 21:28:33 +00001474 Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy);
Owen Andersonbaf3c402009-07-29 18:55:55 +00001475 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Nate Begeman0f123cf2009-02-12 21:28:33 +00001476 break;
1477 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001478 case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred]
Rafael Espindolae076b532013-11-04 16:16:24 +00001479 if (Record.size() < 4)
1480 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001481 Type *OpTy = getTypeByID(Record[0]);
Rafael Espindolae076b532013-11-04 16:16:24 +00001482 if (OpTy == 0)
1483 return Error(InvalidRecord);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001484 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
1485 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
1486
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001487 if (OpTy->isFPOrFPVectorTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +00001488 V = ConstantExpr::getFCmp(Record[3], Op0, Op1);
Nate Begemanac80ade2008-05-12 19:01:56 +00001489 else
Owen Andersonbaf3c402009-07-29 18:55:55 +00001490 V = ConstantExpr::getICmp(Record[3], Op0, Op1);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001491 break;
Chris Lattner522b7b12007-04-24 05:48:56 +00001492 }
Chad Rosier581600b2012-09-05 19:00:49 +00001493 // This maintains backward compatibility, pre-asm dialect keywords.
Chad Rosier27b25c22012-09-05 06:28:52 +00001494 // FIXME: Remove with the 4.0 release.
Chad Rosierf16ae582012-09-05 00:56:20 +00001495 case bitc::CST_CODE_INLINEASM_OLD: {
Rafael Espindolae076b532013-11-04 16:16:24 +00001496 if (Record.size() < 2)
1497 return Error(InvalidRecord);
Chris Lattner2bce93a2007-05-06 01:58:20 +00001498 std::string AsmStr, ConstrStr;
Dale Johannesen43602982009-10-13 20:46:56 +00001499 bool HasSideEffects = Record[0] & 1;
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +00001500 bool IsAlignStack = Record[0] >> 1;
Chris Lattner2bce93a2007-05-06 01:58:20 +00001501 unsigned AsmStrSize = Record[1];
1502 if (2+AsmStrSize >= Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001503 return Error(InvalidRecord);
Chris Lattner2bce93a2007-05-06 01:58:20 +00001504 unsigned ConstStrSize = Record[2+AsmStrSize];
1505 if (3+AsmStrSize+ConstStrSize > Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001506 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001507
Chris Lattner2bce93a2007-05-06 01:58:20 +00001508 for (unsigned i = 0; i != AsmStrSize; ++i)
1509 AsmStr += (char)Record[2+i];
1510 for (unsigned i = 0; i != ConstStrSize; ++i)
1511 ConstrStr += (char)Record[3+AsmStrSize+i];
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001512 PointerType *PTy = cast<PointerType>(CurTy);
Chris Lattner2bce93a2007-05-06 01:58:20 +00001513 V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +00001514 AsmStr, ConstrStr, HasSideEffects, IsAlignStack);
Chris Lattner2bce93a2007-05-06 01:58:20 +00001515 break;
1516 }
Chad Rosier581600b2012-09-05 19:00:49 +00001517 // This version adds support for the asm dialect keywords (e.g.,
1518 // inteldialect).
Chad Rosierf16ae582012-09-05 00:56:20 +00001519 case bitc::CST_CODE_INLINEASM: {
Rafael Espindolae076b532013-11-04 16:16:24 +00001520 if (Record.size() < 2)
1521 return Error(InvalidRecord);
Chad Rosierf16ae582012-09-05 00:56:20 +00001522 std::string AsmStr, ConstrStr;
1523 bool HasSideEffects = Record[0] & 1;
1524 bool IsAlignStack = (Record[0] >> 1) & 1;
1525 unsigned AsmDialect = Record[0] >> 2;
1526 unsigned AsmStrSize = Record[1];
1527 if (2+AsmStrSize >= Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001528 return Error(InvalidRecord);
Chad Rosierf16ae582012-09-05 00:56:20 +00001529 unsigned ConstStrSize = Record[2+AsmStrSize];
1530 if (3+AsmStrSize+ConstStrSize > Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001531 return Error(InvalidRecord);
Chad Rosierf16ae582012-09-05 00:56:20 +00001532
1533 for (unsigned i = 0; i != AsmStrSize; ++i)
1534 AsmStr += (char)Record[2+i];
1535 for (unsigned i = 0; i != ConstStrSize; ++i)
1536 ConstrStr += (char)Record[3+AsmStrSize+i];
1537 PointerType *PTy = cast<PointerType>(CurTy);
1538 V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
1539 AsmStr, ConstrStr, HasSideEffects, IsAlignStack,
Chad Rosier581600b2012-09-05 19:00:49 +00001540 InlineAsm::AsmDialect(AsmDialect));
Chad Rosierf16ae582012-09-05 00:56:20 +00001541 break;
1542 }
Chris Lattner50b136d2009-10-28 05:53:48 +00001543 case bitc::CST_CODE_BLOCKADDRESS:{
Rafael Espindolae076b532013-11-04 16:16:24 +00001544 if (Record.size() < 3)
1545 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001546 Type *FnTy = getTypeByID(Record[0]);
Rafael Espindolae076b532013-11-04 16:16:24 +00001547 if (FnTy == 0)
1548 return Error(InvalidRecord);
Chris Lattner50b136d2009-10-28 05:53:48 +00001549 Function *Fn =
1550 dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy));
Rafael Espindolae076b532013-11-04 16:16:24 +00001551 if (Fn == 0)
1552 return Error(InvalidRecord);
Benjamin Kramer122f5e52012-09-21 14:34:31 +00001553
1554 // If the function is already parsed we can insert the block address right
1555 // away.
1556 if (!Fn->empty()) {
1557 Function::iterator BBI = Fn->begin(), BBE = Fn->end();
1558 for (size_t I = 0, E = Record[2]; I != E; ++I) {
1559 if (BBI == BBE)
Rafael Espindolae076b532013-11-04 16:16:24 +00001560 return Error(InvalidID);
Benjamin Kramer122f5e52012-09-21 14:34:31 +00001561 ++BBI;
1562 }
1563 V = BlockAddress::get(Fn, BBI);
1564 } else {
1565 // Otherwise insert a placeholder and remember it so it can be inserted
1566 // when the function is parsed.
1567 GlobalVariable *FwdRef = new GlobalVariable(*Fn->getParent(),
1568 Type::getInt8Ty(Context),
Chris Lattner50b136d2009-10-28 05:53:48 +00001569 false, GlobalValue::InternalLinkage,
Benjamin Kramer122f5e52012-09-21 14:34:31 +00001570 0, "");
1571 BlockAddrFwdRefs[Fn].push_back(std::make_pair(Record[2], FwdRef));
1572 V = FwdRef;
1573 }
Chris Lattner50b136d2009-10-28 05:53:48 +00001574 break;
Michael Ilseman407a6162012-11-15 22:34:00 +00001575 }
Chris Lattnere16504e2007-04-24 03:30:34 +00001576 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001577
Chris Lattnera7c49aa2007-05-01 07:01:57 +00001578 ValueList.AssignValue(V, NextCstNo);
Chris Lattner522b7b12007-04-24 05:48:56 +00001579 ++NextCstNo;
Chris Lattnere16504e2007-04-24 03:30:34 +00001580 }
1581}
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001582
Rafael Espindolae076b532013-11-04 16:16:24 +00001583error_code BitcodeReader::ParseUseLists() {
Chad Rosiercbbb0962011-12-07 21:44:12 +00001584 if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +00001585 return Error(InvalidRecord);
Chad Rosiercbbb0962011-12-07 21:44:12 +00001586
1587 SmallVector<uint64_t, 64> Record;
Michael Ilseman407a6162012-11-15 22:34:00 +00001588
Chad Rosiercbbb0962011-12-07 21:44:12 +00001589 // Read all the records.
1590 while (1) {
Chris Lattner5a4251c2013-01-20 02:13:19 +00001591 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbeyacb61942013-02-06 22:14:06 +00001592
Chris Lattner5a4251c2013-01-20 02:13:19 +00001593 switch (Entry.Kind) {
1594 case BitstreamEntry::SubBlock: // Handled for us already.
1595 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +00001596 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00001597 case BitstreamEntry::EndBlock:
Rafael Espindolae076b532013-11-04 16:16:24 +00001598 return error_code::success();
Chris Lattner5a4251c2013-01-20 02:13:19 +00001599 case BitstreamEntry::Record:
1600 // The interesting case.
1601 break;
Chad Rosiercbbb0962011-12-07 21:44:12 +00001602 }
Michael Ilseman407a6162012-11-15 22:34:00 +00001603
Chad Rosiercbbb0962011-12-07 21:44:12 +00001604 // Read a use list record.
1605 Record.clear();
Chris Lattner5a4251c2013-01-20 02:13:19 +00001606 switch (Stream.readRecord(Entry.ID, Record)) {
Chad Rosiercbbb0962011-12-07 21:44:12 +00001607 default: // Default behavior: unknown type.
1608 break;
1609 case bitc::USELIST_CODE_ENTRY: { // USELIST_CODE_ENTRY: TBD.
1610 unsigned RecordLength = Record.size();
1611 if (RecordLength < 1)
Rafael Espindolae076b532013-11-04 16:16:24 +00001612 return Error(InvalidRecord);
Chad Rosiercbbb0962011-12-07 21:44:12 +00001613 UseListRecords.push_back(Record);
1614 break;
1615 }
1616 }
1617 }
1618}
1619
Chris Lattner980e5aa2007-05-01 05:52:21 +00001620/// RememberAndSkipFunctionBody - When we see the block for a function body,
1621/// remember where it is and then skip it. This lets us lazily deserialize the
1622/// functions.
Rafael Espindolae076b532013-11-04 16:16:24 +00001623error_code BitcodeReader::RememberAndSkipFunctionBody() {
Chris Lattner48f84872007-05-01 04:59:48 +00001624 // Get the function we are talking about.
1625 if (FunctionsWithBodies.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +00001626 return Error(InsufficientFunctionProtos);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001627
Chris Lattner48f84872007-05-01 04:59:48 +00001628 Function *Fn = FunctionsWithBodies.back();
1629 FunctionsWithBodies.pop_back();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001630
Chris Lattner48f84872007-05-01 04:59:48 +00001631 // Save the current stream state.
1632 uint64_t CurBit = Stream.GetCurrentBitNo();
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00001633 DeferredFunctionInfo[Fn] = CurBit;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001634
Chris Lattner48f84872007-05-01 04:59:48 +00001635 // Skip over the function block for now.
1636 if (Stream.SkipBlock())
Rafael Espindolae076b532013-11-04 16:16:24 +00001637 return Error(InvalidRecord);
1638 return error_code::success();
Chris Lattner48f84872007-05-01 04:59:48 +00001639}
1640
Rafael Espindolae076b532013-11-04 16:16:24 +00001641error_code BitcodeReader::GlobalCleanup() {
Derek Schuff2ea93872012-02-06 22:30:29 +00001642 // Patch the initializers for globals and aliases up.
1643 ResolveGlobalAndAliasInits();
1644 if (!GlobalInits.empty() || !AliasInits.empty())
Rafael Espindolae076b532013-11-04 16:16:24 +00001645 return Error(MalformedGlobalInitializerSet);
Derek Schuff2ea93872012-02-06 22:30:29 +00001646
1647 // Look for intrinsic functions which need to be upgraded at some point
1648 for (Module::iterator FI = TheModule->begin(), FE = TheModule->end();
1649 FI != FE; ++FI) {
1650 Function *NewFn;
1651 if (UpgradeIntrinsicFunction(FI, NewFn))
1652 UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn));
1653 }
1654
1655 // Look for global variables which need to be renamed.
1656 for (Module::global_iterator
1657 GI = TheModule->global_begin(), GE = TheModule->global_end();
1658 GI != GE; ++GI)
1659 UpgradeGlobalVariable(GI);
1660 // Force deallocation of memory for these vectors to favor the client that
1661 // want lazy deserialization.
1662 std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits);
1663 std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits);
Rafael Espindolae076b532013-11-04 16:16:24 +00001664 return error_code::success();
Derek Schuff2ea93872012-02-06 22:30:29 +00001665}
1666
Rafael Espindolae076b532013-11-04 16:16:24 +00001667error_code BitcodeReader::ParseModule(bool Resume) {
Derek Schuff2ea93872012-02-06 22:30:29 +00001668 if (Resume)
1669 Stream.JumpToBit(NextUnreadBit);
1670 else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +00001671 return Error(InvalidRecord);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001672
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001673 SmallVector<uint64_t, 64> Record;
1674 std::vector<std::string> SectionTable;
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001675 std::vector<std::string> GCTable;
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001676
1677 // Read all the records for this module.
Chris Lattner5a4251c2013-01-20 02:13:19 +00001678 while (1) {
1679 BitstreamEntry Entry = Stream.advance();
Joe Abbeyacb61942013-02-06 22:14:06 +00001680
Chris Lattner5a4251c2013-01-20 02:13:19 +00001681 switch (Entry.Kind) {
1682 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +00001683 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00001684 case BitstreamEntry::EndBlock:
Derek Schuff2ea93872012-02-06 22:30:29 +00001685 return GlobalCleanup();
Joe Abbeyacb61942013-02-06 22:14:06 +00001686
Chris Lattner5a4251c2013-01-20 02:13:19 +00001687 case BitstreamEntry::SubBlock:
1688 switch (Entry.ID) {
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001689 default: // Skip unknown content.
1690 if (Stream.SkipBlock())
Rafael Espindolae076b532013-11-04 16:16:24 +00001691 return Error(InvalidRecord);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001692 break;
Chris Lattner3f799802007-05-05 18:57:30 +00001693 case bitc::BLOCKINFO_BLOCK_ID:
1694 if (Stream.ReadBlockInfoBlock())
Rafael Espindolae076b532013-11-04 16:16:24 +00001695 return Error(MalformedBlock);
Chris Lattner3f799802007-05-05 18:57:30 +00001696 break;
Chris Lattner48c85b82007-05-04 03:30:17 +00001697 case bitc::PARAMATTR_BLOCK_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00001698 if (error_code EC = ParseAttributeBlock())
1699 return EC;
Chris Lattner48c85b82007-05-04 03:30:17 +00001700 break;
Bill Wendlingc3ba0a82013-02-10 23:24:25 +00001701 case bitc::PARAMATTR_GROUP_BLOCK_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00001702 if (error_code EC = ParseAttributeGroupBlock())
1703 return EC;
Bill Wendlingc3ba0a82013-02-10 23:24:25 +00001704 break;
Chris Lattner1afcace2011-07-09 17:41:24 +00001705 case bitc::TYPE_BLOCK_ID_NEW:
Rafael Espindolae076b532013-11-04 16:16:24 +00001706 if (error_code EC = ParseTypeTable())
1707 return EC;
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001708 break;
Chris Lattner0b2482a2007-04-23 21:26:05 +00001709 case bitc::VALUE_SYMTAB_BLOCK_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00001710 if (error_code EC = ParseValueSymbolTable())
1711 return EC;
Derek Schuff2ea93872012-02-06 22:30:29 +00001712 SeenValueSymbolTable = true;
Chris Lattner0b2482a2007-04-23 21:26:05 +00001713 break;
Chris Lattnere16504e2007-04-24 03:30:34 +00001714 case bitc::CONSTANTS_BLOCK_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00001715 if (error_code EC = ParseConstants())
1716 return EC;
1717 if (error_code EC = ResolveGlobalAndAliasInits())
1718 return EC;
Chris Lattnere16504e2007-04-24 03:30:34 +00001719 break;
Devang Patele54abc92009-07-22 17:43:22 +00001720 case bitc::METADATA_BLOCK_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00001721 if (error_code EC = ParseMetadata())
1722 return EC;
Devang Patele54abc92009-07-22 17:43:22 +00001723 break;
Chris Lattner48f84872007-05-01 04:59:48 +00001724 case bitc::FUNCTION_BLOCK_ID:
1725 // If this is the first function body we've seen, reverse the
1726 // FunctionsWithBodies list.
Derek Schuff2ea93872012-02-06 22:30:29 +00001727 if (!SeenFirstFunctionBody) {
Chris Lattner48f84872007-05-01 04:59:48 +00001728 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
Rafael Espindolae076b532013-11-04 16:16:24 +00001729 if (error_code EC = GlobalCleanup())
1730 return EC;
Derek Schuff2ea93872012-02-06 22:30:29 +00001731 SeenFirstFunctionBody = true;
Chris Lattner48f84872007-05-01 04:59:48 +00001732 }
Joe Abbeyacb61942013-02-06 22:14:06 +00001733
Rafael Espindolae076b532013-11-04 16:16:24 +00001734 if (error_code EC = RememberAndSkipFunctionBody())
1735 return EC;
Derek Schuff2ea93872012-02-06 22:30:29 +00001736 // For streaming bitcode, suspend parsing when we reach the function
1737 // bodies. Subsequent materialization calls will resume it when
1738 // necessary. For streaming, the function bodies must be at the end of
1739 // the bitcode. If the bitcode file is old, the symbol table will be
1740 // at the end instead and will not have been seen yet. In this case,
1741 // just finish the parse now.
1742 if (LazyStreamer && SeenValueSymbolTable) {
1743 NextUnreadBit = Stream.GetCurrentBitNo();
Rafael Espindolae076b532013-11-04 16:16:24 +00001744 return error_code::success();
Derek Schuff2ea93872012-02-06 22:30:29 +00001745 }
Chris Lattner48f84872007-05-01 04:59:48 +00001746 break;
Chad Rosiercbbb0962011-12-07 21:44:12 +00001747 case bitc::USELIST_BLOCK_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00001748 if (error_code EC = ParseUseLists())
1749 return EC;
Chad Rosiercbbb0962011-12-07 21:44:12 +00001750 break;
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001751 }
1752 continue;
Joe Abbeyacb61942013-02-06 22:14:06 +00001753
Chris Lattner5a4251c2013-01-20 02:13:19 +00001754 case BitstreamEntry::Record:
1755 // The interesting case.
1756 break;
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001757 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001758
Daniel Dunbara279bc32009-09-20 02:20:51 +00001759
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001760 // Read a record.
Chris Lattner5a4251c2013-01-20 02:13:19 +00001761 switch (Stream.readRecord(Entry.ID, Record)) {
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001762 default: break; // Default behavior, ignore unknown content.
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00001763 case bitc::MODULE_CODE_VERSION: { // VERSION: [version#]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001764 if (Record.size() < 1)
Rafael Espindolae076b532013-11-04 16:16:24 +00001765 return Error(InvalidRecord);
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00001766 // Only version #0 and #1 are supported so far.
1767 unsigned module_version = Record[0];
1768 switch (module_version) {
Rafael Espindolae076b532013-11-04 16:16:24 +00001769 default:
1770 return Error(InvalidValue);
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00001771 case 0:
1772 UseRelativeIDs = false;
1773 break;
1774 case 1:
1775 UseRelativeIDs = true;
1776 break;
1777 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001778 break;
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00001779 }
Chris Lattner15e6d172007-05-04 19:11:41 +00001780 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001781 std::string S;
1782 if (ConvertToString(Record, 0, S))
Rafael Espindolae076b532013-11-04 16:16:24 +00001783 return Error(InvalidRecord);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001784 TheModule->setTargetTriple(S);
1785 break;
1786 }
Chris Lattner15e6d172007-05-04 19:11:41 +00001787 case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001788 std::string S;
1789 if (ConvertToString(Record, 0, S))
Rafael Espindolae076b532013-11-04 16:16:24 +00001790 return Error(InvalidRecord);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001791 TheModule->setDataLayout(S);
1792 break;
1793 }
Chris Lattner15e6d172007-05-04 19:11:41 +00001794 case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001795 std::string S;
1796 if (ConvertToString(Record, 0, S))
Rafael Espindolae076b532013-11-04 16:16:24 +00001797 return Error(InvalidRecord);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001798 TheModule->setModuleInlineAsm(S);
1799 break;
1800 }
Bill Wendling3defc0b2012-11-28 08:41:48 +00001801 case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N]
1802 // FIXME: Remove in 4.0.
1803 std::string S;
1804 if (ConvertToString(Record, 0, S))
Rafael Espindolae076b532013-11-04 16:16:24 +00001805 return Error(InvalidRecord);
Bill Wendling3defc0b2012-11-28 08:41:48 +00001806 // Ignore value.
1807 break;
1808 }
Chris Lattner15e6d172007-05-04 19:11:41 +00001809 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001810 std::string S;
1811 if (ConvertToString(Record, 0, S))
Rafael Espindolae076b532013-11-04 16:16:24 +00001812 return Error(InvalidRecord);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001813 SectionTable.push_back(S);
1814 break;
1815 }
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001816 case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N]
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001817 std::string S;
1818 if (ConvertToString(Record, 0, S))
Rafael Espindolae076b532013-11-04 16:16:24 +00001819 return Error(InvalidRecord);
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001820 GCTable.push_back(S);
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001821 break;
1822 }
Christopher Lambfe63fb92007-12-11 08:59:05 +00001823 // GLOBALVAR: [pointer type, isconst, initid,
Rafael Espindolabea46262011-01-08 16:42:36 +00001824 // linkage, alignment, section, visibility, threadlocal,
1825 // unnamed_addr]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001826 case bitc::MODULE_CODE_GLOBALVAR: {
Chris Lattner36d5e7d2007-04-23 16:04:05 +00001827 if (Record.size() < 6)
Rafael Espindolae076b532013-11-04 16:16:24 +00001828 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001829 Type *Ty = getTypeByID(Record[0]);
Rafael Espindolae076b532013-11-04 16:16:24 +00001830 if (!Ty)
1831 return Error(InvalidRecord);
Duncan Sands1df98592010-02-16 11:11:14 +00001832 if (!Ty->isPointerTy())
Rafael Espindolae076b532013-11-04 16:16:24 +00001833 return Error(InvalidTypeForValue);
Christopher Lambfe63fb92007-12-11 08:59:05 +00001834 unsigned AddressSpace = cast<PointerType>(Ty)->getAddressSpace();
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001835 Ty = cast<PointerType>(Ty)->getElementType();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001836
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001837 bool isConstant = Record[1];
1838 GlobalValue::LinkageTypes Linkage = GetDecodedLinkage(Record[3]);
1839 unsigned Alignment = (1 << Record[4]) >> 1;
1840 std::string Section;
1841 if (Record[5]) {
1842 if (Record[5]-1 >= SectionTable.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001843 return Error(InvalidID);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001844 Section = SectionTable[Record[5]-1];
1845 }
Chris Lattner36d5e7d2007-04-23 16:04:05 +00001846 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
Chris Lattner5f32c012007-05-06 19:27:46 +00001847 if (Record.size() > 6)
1848 Visibility = GetDecodedVisibility(Record[6]);
Hans Wennborgce718ff2012-06-23 11:37:03 +00001849
1850 GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal;
Chris Lattner5f32c012007-05-06 19:27:46 +00001851 if (Record.size() > 7)
Hans Wennborgce718ff2012-06-23 11:37:03 +00001852 TLM = GetDecodedThreadLocalMode(Record[7]);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001853
Rafael Espindolabea46262011-01-08 16:42:36 +00001854 bool UnnamedAddr = false;
1855 if (Record.size() > 8)
1856 UnnamedAddr = Record[8];
1857
Michael Gottesmana2de37c2013-02-05 05:57:38 +00001858 bool ExternallyInitialized = false;
1859 if (Record.size() > 9)
1860 ExternallyInitialized = Record[9];
1861
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001862 GlobalVariable *NewGV =
Daniel Dunbara279bc32009-09-20 02:20:51 +00001863 new GlobalVariable(*TheModule, Ty, isConstant, Linkage, 0, "", 0,
Michael Gottesmana2de37c2013-02-05 05:57:38 +00001864 TLM, AddressSpace, ExternallyInitialized);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001865 NewGV->setAlignment(Alignment);
1866 if (!Section.empty())
1867 NewGV->setSection(Section);
1868 NewGV->setVisibility(Visibility);
Rafael Espindolabea46262011-01-08 16:42:36 +00001869 NewGV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001870
Chris Lattner0b2482a2007-04-23 21:26:05 +00001871 ValueList.push_back(NewGV);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001872
Chris Lattner6dbfd7b2007-04-24 00:18:21 +00001873 // Remember which value to use for the global initializer.
1874 if (unsigned InitID = Record[2])
1875 GlobalInits.push_back(std::make_pair(NewGV, InitID-1));
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001876 break;
1877 }
Chris Lattnera9bb7132007-05-08 05:38:01 +00001878 // FUNCTION: [type, callingconv, isproto, linkage, paramattr,
Rafael Espindolabea46262011-01-08 16:42:36 +00001879 // alignment, section, visibility, gc, unnamed_addr]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001880 case bitc::MODULE_CODE_FUNCTION: {
Chris Lattnera9bb7132007-05-08 05:38:01 +00001881 if (Record.size() < 8)
Rafael Espindolae076b532013-11-04 16:16:24 +00001882 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001883 Type *Ty = getTypeByID(Record[0]);
Rafael Espindolae076b532013-11-04 16:16:24 +00001884 if (!Ty)
1885 return Error(InvalidRecord);
Duncan Sands1df98592010-02-16 11:11:14 +00001886 if (!Ty->isPointerTy())
Rafael Espindolae076b532013-11-04 16:16:24 +00001887 return Error(InvalidTypeForValue);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001888 FunctionType *FTy =
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001889 dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType());
1890 if (!FTy)
Rafael Espindolae076b532013-11-04 16:16:24 +00001891 return Error(InvalidTypeForValue);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001892
Gabor Greif051a9502008-04-06 20:25:17 +00001893 Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage,
1894 "", TheModule);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001895
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001896 Func->setCallingConv(static_cast<CallingConv::ID>(Record[1]));
Chris Lattner48f84872007-05-01 04:59:48 +00001897 bool isProto = Record[2];
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001898 Func->setLinkage(GetDecodedLinkage(Record[3]));
Devang Patel05988662008-09-25 21:00:45 +00001899 Func->setAttributes(getAttributes(Record[4]));
Daniel Dunbara279bc32009-09-20 02:20:51 +00001900
Chris Lattnera9bb7132007-05-08 05:38:01 +00001901 Func->setAlignment((1 << Record[5]) >> 1);
1902 if (Record[6]) {
1903 if (Record[6]-1 >= SectionTable.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001904 return Error(InvalidID);
Chris Lattnera9bb7132007-05-08 05:38:01 +00001905 Func->setSection(SectionTable[Record[6]-1]);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001906 }
Chris Lattnera9bb7132007-05-08 05:38:01 +00001907 Func->setVisibility(GetDecodedVisibility(Record[7]));
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001908 if (Record.size() > 8 && Record[8]) {
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001909 if (Record[8]-1 > GCTable.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001910 return Error(InvalidID);
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001911 Func->setGC(GCTable[Record[8]-1].c_str());
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001912 }
Rafael Espindolabea46262011-01-08 16:42:36 +00001913 bool UnnamedAddr = false;
1914 if (Record.size() > 9)
1915 UnnamedAddr = Record[9];
1916 Func->setUnnamedAddr(UnnamedAddr);
Peter Collingbourne1e3037f2013-09-16 01:08:15 +00001917 if (Record.size() > 10 && Record[10] != 0)
1918 FunctionPrefixes.push_back(std::make_pair(Func, Record[10]-1));
Chris Lattner0b2482a2007-04-23 21:26:05 +00001919 ValueList.push_back(Func);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001920
Chris Lattner48f84872007-05-01 04:59:48 +00001921 // If this is a function with a body, remember the prototype we are
1922 // creating now, so that we can match up the body with them later.
Derek Schuff2ea93872012-02-06 22:30:29 +00001923 if (!isProto) {
Chris Lattner48f84872007-05-01 04:59:48 +00001924 FunctionsWithBodies.push_back(Func);
Derek Schuff2ea93872012-02-06 22:30:29 +00001925 if (LazyStreamer) DeferredFunctionInfo[Func] = 0;
1926 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001927 break;
1928 }
Anton Korobeynikov91342d82008-03-12 00:49:19 +00001929 // ALIAS: [alias type, aliasee val#, linkage]
Anton Korobeynikovf8342b92008-03-11 21:40:17 +00001930 // ALIAS: [alias type, aliasee val#, linkage, visibility]
Chris Lattner198f34a2007-04-26 03:27:58 +00001931 case bitc::MODULE_CODE_ALIAS: {
Chris Lattner07d98b42007-04-26 02:46:40 +00001932 if (Record.size() < 3)
Rafael Espindolae076b532013-11-04 16:16:24 +00001933 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001934 Type *Ty = getTypeByID(Record[0]);
Rafael Espindolae076b532013-11-04 16:16:24 +00001935 if (!Ty)
1936 return Error(InvalidRecord);
Duncan Sands1df98592010-02-16 11:11:14 +00001937 if (!Ty->isPointerTy())
Rafael Espindolae076b532013-11-04 16:16:24 +00001938 return Error(InvalidTypeForValue);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001939
Chris Lattner07d98b42007-04-26 02:46:40 +00001940 GlobalAlias *NewGA = new GlobalAlias(Ty, GetDecodedLinkage(Record[2]),
1941 "", 0, TheModule);
Anton Korobeynikov91342d82008-03-12 00:49:19 +00001942 // Old bitcode files didn't have visibility field.
1943 if (Record.size() > 3)
1944 NewGA->setVisibility(GetDecodedVisibility(Record[3]));
Chris Lattner07d98b42007-04-26 02:46:40 +00001945 ValueList.push_back(NewGA);
1946 AliasInits.push_back(std::make_pair(NewGA, Record[1]));
1947 break;
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001948 }
Chris Lattner198f34a2007-04-26 03:27:58 +00001949 /// MODULE_CODE_PURGEVALS: [numvals]
1950 case bitc::MODULE_CODE_PURGEVALS:
1951 // Trim down the value list to the specified size.
1952 if (Record.size() < 1 || Record[0] > ValueList.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00001953 return Error(InvalidRecord);
Chris Lattner198f34a2007-04-26 03:27:58 +00001954 ValueList.shrinkTo(Record[0]);
1955 break;
1956 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001957 Record.clear();
1958 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001959}
1960
Rafael Espindolae076b532013-11-04 16:16:24 +00001961error_code BitcodeReader::ParseBitcodeInto(Module *M) {
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001962 TheModule = 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001963
Rafael Espindolae076b532013-11-04 16:16:24 +00001964 if (error_code EC = InitStream())
1965 return EC;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001966
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001967 // Sniff for the signature.
1968 if (Stream.Read(8) != 'B' ||
1969 Stream.Read(8) != 'C' ||
1970 Stream.Read(4) != 0x0 ||
1971 Stream.Read(4) != 0xC ||
1972 Stream.Read(4) != 0xE ||
1973 Stream.Read(4) != 0xD)
Rafael Espindolae076b532013-11-04 16:16:24 +00001974 return Error(InvalidBitcodeSignature);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001975
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001976 // We expect a number of well-defined blocks, though we don't necessarily
1977 // need to understand them all.
Chris Lattner5a4251c2013-01-20 02:13:19 +00001978 while (1) {
1979 if (Stream.AtEndOfStream())
Rafael Espindolae076b532013-11-04 16:16:24 +00001980 return error_code::success();
Joe Abbeyacb61942013-02-06 22:14:06 +00001981
Chris Lattner5a4251c2013-01-20 02:13:19 +00001982 BitstreamEntry Entry =
1983 Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
Joe Abbeyacb61942013-02-06 22:14:06 +00001984
Chris Lattner5a4251c2013-01-20 02:13:19 +00001985 switch (Entry.Kind) {
1986 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +00001987 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00001988 case BitstreamEntry::EndBlock:
Rafael Espindolae076b532013-11-04 16:16:24 +00001989 return error_code::success();
Joe Abbeyacb61942013-02-06 22:14:06 +00001990
Chris Lattner5a4251c2013-01-20 02:13:19 +00001991 case BitstreamEntry::SubBlock:
1992 switch (Entry.ID) {
1993 case bitc::BLOCKINFO_BLOCK_ID:
1994 if (Stream.ReadBlockInfoBlock())
Rafael Espindolae076b532013-11-04 16:16:24 +00001995 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00001996 break;
1997 case bitc::MODULE_BLOCK_ID:
1998 // Reject multiple MODULE_BLOCK's in a single bitstream.
1999 if (TheModule)
Rafael Espindolae076b532013-11-04 16:16:24 +00002000 return Error(InvalidMultipleBlocks);
Chris Lattner5a4251c2013-01-20 02:13:19 +00002001 TheModule = M;
Rafael Espindolae076b532013-11-04 16:16:24 +00002002 if (error_code EC = ParseModule(false))
2003 return EC;
2004 if (LazyStreamer)
2005 return error_code::success();
Chris Lattner5a4251c2013-01-20 02:13:19 +00002006 break;
2007 default:
2008 if (Stream.SkipBlock())
Rafael Espindolae076b532013-11-04 16:16:24 +00002009 return Error(InvalidRecord);
Chris Lattner5a4251c2013-01-20 02:13:19 +00002010 break;
2011 }
2012 continue;
2013 case BitstreamEntry::Record:
2014 // There should be no records in the top-level of blocks.
Joe Abbeyacb61942013-02-06 22:14:06 +00002015
Chris Lattner5a4251c2013-01-20 02:13:19 +00002016 // The ranlib in Xcode 4 will align archive members by appending newlines
Chad Rosier6ff9aa22011-08-09 22:23:40 +00002017 // to the end of them. If this file size is a multiple of 4 but not 8, we
2018 // have to read and ignore these final 4 bytes :-(
Chris Lattner5a4251c2013-01-20 02:13:19 +00002019 if (Stream.getAbbrevIDWidth() == 2 && Entry.ID == 2 &&
Rafael Espindolac9687b32011-05-26 18:59:54 +00002020 Stream.Read(6) == 2 && Stream.Read(24) == 0xa0a0a &&
Bill Wendling2127c9b2012-07-19 00:15:11 +00002021 Stream.AtEndOfStream())
Rafael Espindolae076b532013-11-04 16:16:24 +00002022 return error_code::success();
Joe Abbeyacb61942013-02-06 22:14:06 +00002023
Rafael Espindolae076b532013-11-04 16:16:24 +00002024 return Error(InvalidRecord);
Rafael Espindolac9687b32011-05-26 18:59:54 +00002025 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +00002026 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +00002027}
Chris Lattnerc453f762007-04-29 07:54:31 +00002028
Rafael Espindolae076b532013-11-04 16:16:24 +00002029error_code BitcodeReader::ParseModuleTriple(std::string &Triple) {
Bill Wendling34711742010-10-06 01:22:42 +00002030 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +00002031 return Error(InvalidRecord);
Bill Wendling34711742010-10-06 01:22:42 +00002032
2033 SmallVector<uint64_t, 64> Record;
2034
2035 // Read all the records for this module.
Chris Lattner5a4251c2013-01-20 02:13:19 +00002036 while (1) {
2037 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbeyacb61942013-02-06 22:14:06 +00002038
Chris Lattner5a4251c2013-01-20 02:13:19 +00002039 switch (Entry.Kind) {
2040 case BitstreamEntry::SubBlock: // Handled for us already.
2041 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +00002042 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00002043 case BitstreamEntry::EndBlock:
Rafael Espindolae076b532013-11-04 16:16:24 +00002044 return error_code::success();
Chris Lattner5a4251c2013-01-20 02:13:19 +00002045 case BitstreamEntry::Record:
2046 // The interesting case.
2047 break;
Bill Wendling34711742010-10-06 01:22:42 +00002048 }
2049
2050 // Read a record.
Chris Lattner5a4251c2013-01-20 02:13:19 +00002051 switch (Stream.readRecord(Entry.ID, Record)) {
Bill Wendling34711742010-10-06 01:22:42 +00002052 default: break; // Default behavior, ignore unknown content.
Bill Wendling34711742010-10-06 01:22:42 +00002053 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
2054 std::string S;
2055 if (ConvertToString(Record, 0, S))
Rafael Espindolae076b532013-11-04 16:16:24 +00002056 return Error(InvalidRecord);
Bill Wendling34711742010-10-06 01:22:42 +00002057 Triple = S;
2058 break;
2059 }
2060 }
2061 Record.clear();
2062 }
Bill Wendling34711742010-10-06 01:22:42 +00002063}
2064
Rafael Espindolae076b532013-11-04 16:16:24 +00002065error_code BitcodeReader::ParseTriple(std::string &Triple) {
2066 if (error_code EC = InitStream())
2067 return EC;
Bill Wendling34711742010-10-06 01:22:42 +00002068
2069 // Sniff for the signature.
2070 if (Stream.Read(8) != 'B' ||
2071 Stream.Read(8) != 'C' ||
2072 Stream.Read(4) != 0x0 ||
2073 Stream.Read(4) != 0xC ||
2074 Stream.Read(4) != 0xE ||
2075 Stream.Read(4) != 0xD)
Rafael Espindolae076b532013-11-04 16:16:24 +00002076 return Error(InvalidBitcodeSignature);
Bill Wendling34711742010-10-06 01:22:42 +00002077
2078 // We expect a number of well-defined blocks, though we don't necessarily
2079 // need to understand them all.
Chris Lattner5a4251c2013-01-20 02:13:19 +00002080 while (1) {
2081 BitstreamEntry Entry = Stream.advance();
Joe Abbeyacb61942013-02-06 22:14:06 +00002082
Chris Lattner5a4251c2013-01-20 02:13:19 +00002083 switch (Entry.Kind) {
2084 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +00002085 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00002086 case BitstreamEntry::EndBlock:
Rafael Espindolae076b532013-11-04 16:16:24 +00002087 return error_code::success();
Joe Abbeyacb61942013-02-06 22:14:06 +00002088
Chris Lattner5a4251c2013-01-20 02:13:19 +00002089 case BitstreamEntry::SubBlock:
2090 if (Entry.ID == bitc::MODULE_BLOCK_ID)
2091 return ParseModuleTriple(Triple);
Joe Abbeyacb61942013-02-06 22:14:06 +00002092
Chris Lattner5a4251c2013-01-20 02:13:19 +00002093 // Ignore other sub-blocks.
Rafael Espindolae076b532013-11-04 16:16:24 +00002094 if (Stream.SkipBlock())
2095 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00002096 continue;
Joe Abbeyacb61942013-02-06 22:14:06 +00002097
Chris Lattner5a4251c2013-01-20 02:13:19 +00002098 case BitstreamEntry::Record:
2099 Stream.skipRecord(Entry.ID);
2100 continue;
Bill Wendling34711742010-10-06 01:22:42 +00002101 }
2102 }
Bill Wendling34711742010-10-06 01:22:42 +00002103}
2104
Devang Patele8e02132009-09-18 19:26:43 +00002105/// ParseMetadataAttachment - Parse metadata attachments.
Rafael Espindolae076b532013-11-04 16:16:24 +00002106error_code BitcodeReader::ParseMetadataAttachment() {
Devang Patele8e02132009-09-18 19:26:43 +00002107 if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +00002108 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002109
Devang Patele8e02132009-09-18 19:26:43 +00002110 SmallVector<uint64_t, 64> Record;
Chris Lattner5a4251c2013-01-20 02:13:19 +00002111 while (1) {
2112 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbeyacb61942013-02-06 22:14:06 +00002113
Chris Lattner5a4251c2013-01-20 02:13:19 +00002114 switch (Entry.Kind) {
2115 case BitstreamEntry::SubBlock: // Handled for us already.
2116 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +00002117 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00002118 case BitstreamEntry::EndBlock:
Rafael Espindolae076b532013-11-04 16:16:24 +00002119 return error_code::success();
Chris Lattner5a4251c2013-01-20 02:13:19 +00002120 case BitstreamEntry::Record:
2121 // The interesting case.
Devang Patele8e02132009-09-18 19:26:43 +00002122 break;
2123 }
Chris Lattner5a4251c2013-01-20 02:13:19 +00002124
Devang Patele8e02132009-09-18 19:26:43 +00002125 // Read a metadata attachment record.
2126 Record.clear();
Chris Lattner5a4251c2013-01-20 02:13:19 +00002127 switch (Stream.readRecord(Entry.ID, Record)) {
Devang Patele8e02132009-09-18 19:26:43 +00002128 default: // Default behavior: ignore.
2129 break;
Chris Lattner9d61dd92011-06-17 17:50:30 +00002130 case bitc::METADATA_ATTACHMENT: {
Devang Patele8e02132009-09-18 19:26:43 +00002131 unsigned RecordLength = Record.size();
2132 if (Record.empty() || (RecordLength - 1) % 2 == 1)
Rafael Espindolae076b532013-11-04 16:16:24 +00002133 return Error(InvalidRecord);
Devang Patele8e02132009-09-18 19:26:43 +00002134 Instruction *Inst = InstructionList[Record[0]];
2135 for (unsigned i = 1; i != RecordLength; i = i+2) {
Devang Patela2148402009-09-28 21:14:55 +00002136 unsigned Kind = Record[i];
Dan Gohman19538d12010-07-20 21:42:28 +00002137 DenseMap<unsigned, unsigned>::iterator I =
2138 MDKindMap.find(Kind);
2139 if (I == MDKindMap.end())
Rafael Espindolae076b532013-11-04 16:16:24 +00002140 return Error(InvalidID);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002141 Value *Node = MDValueList.getValueFwdRef(Record[i+1]);
Dan Gohman19538d12010-07-20 21:42:28 +00002142 Inst->setMetadata(I->second, cast<MDNode>(Node));
Manman Ren804f0342013-09-28 00:22:27 +00002143 if (I->second == LLVMContext::MD_tbaa)
2144 InstsWithTBAATag.push_back(Inst);
Devang Patele8e02132009-09-18 19:26:43 +00002145 }
2146 break;
2147 }
2148 }
2149 }
Devang Patele8e02132009-09-18 19:26:43 +00002150}
Chris Lattner48f84872007-05-01 04:59:48 +00002151
Chris Lattner980e5aa2007-05-01 05:52:21 +00002152/// ParseFunctionBody - Lazily parse the specified function body block.
Rafael Espindolae076b532013-11-04 16:16:24 +00002153error_code BitcodeReader::ParseFunctionBody(Function *F) {
Chris Lattnere17b6582007-05-05 00:17:00 +00002154 if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
Rafael Espindolae076b532013-11-04 16:16:24 +00002155 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002156
Nick Lewycky9a49f152010-02-25 08:30:17 +00002157 InstructionList.clear();
Chris Lattner980e5aa2007-05-01 05:52:21 +00002158 unsigned ModuleValueListSize = ValueList.size();
Dan Gohman69813832010-08-25 20:22:53 +00002159 unsigned ModuleMDValueListSize = MDValueList.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +00002160
Chris Lattner980e5aa2007-05-01 05:52:21 +00002161 // Add all the function arguments to the value table.
2162 for(Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
2163 ValueList.push_back(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002164
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002165 unsigned NextValueNo = ValueList.size();
Chris Lattner231cbcb2007-05-02 04:27:25 +00002166 BasicBlock *CurBB = 0;
2167 unsigned CurBBNo = 0;
2168
Chris Lattnera6245242010-04-03 02:17:50 +00002169 DebugLoc LastLoc;
Michael Ilseman407a6162012-11-15 22:34:00 +00002170
Chris Lattner980e5aa2007-05-01 05:52:21 +00002171 // Read all the records.
2172 SmallVector<uint64_t, 64> Record;
2173 while (1) {
Chris Lattner5a4251c2013-01-20 02:13:19 +00002174 BitstreamEntry Entry = Stream.advance();
Joe Abbeyacb61942013-02-06 22:14:06 +00002175
Chris Lattner5a4251c2013-01-20 02:13:19 +00002176 switch (Entry.Kind) {
2177 case BitstreamEntry::Error:
Rafael Espindolae076b532013-11-04 16:16:24 +00002178 return Error(MalformedBlock);
Chris Lattner5a4251c2013-01-20 02:13:19 +00002179 case BitstreamEntry::EndBlock:
2180 goto OutOfRecordLoop;
Joe Abbeyacb61942013-02-06 22:14:06 +00002181
Chris Lattner5a4251c2013-01-20 02:13:19 +00002182 case BitstreamEntry::SubBlock:
2183 switch (Entry.ID) {
Chris Lattner980e5aa2007-05-01 05:52:21 +00002184 default: // Skip unknown content.
2185 if (Stream.SkipBlock())
Rafael Espindolae076b532013-11-04 16:16:24 +00002186 return Error(InvalidRecord);
Chris Lattner980e5aa2007-05-01 05:52:21 +00002187 break;
2188 case bitc::CONSTANTS_BLOCK_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00002189 if (error_code EC = ParseConstants())
2190 return EC;
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002191 NextValueNo = ValueList.size();
Chris Lattner980e5aa2007-05-01 05:52:21 +00002192 break;
2193 case bitc::VALUE_SYMTAB_BLOCK_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00002194 if (error_code EC = ParseValueSymbolTable())
2195 return EC;
Chris Lattner980e5aa2007-05-01 05:52:21 +00002196 break;
Devang Patele8e02132009-09-18 19:26:43 +00002197 case bitc::METADATA_ATTACHMENT_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00002198 if (error_code EC = ParseMetadataAttachment())
2199 return EC;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002200 break;
Victor Hernandezfab9e99c2010-01-13 19:34:08 +00002201 case bitc::METADATA_BLOCK_ID:
Rafael Espindolae076b532013-11-04 16:16:24 +00002202 if (error_code EC = ParseMetadata())
2203 return EC;
Victor Hernandezfab9e99c2010-01-13 19:34:08 +00002204 break;
Chris Lattner980e5aa2007-05-01 05:52:21 +00002205 }
2206 continue;
Joe Abbeyacb61942013-02-06 22:14:06 +00002207
Chris Lattner5a4251c2013-01-20 02:13:19 +00002208 case BitstreamEntry::Record:
2209 // The interesting case.
2210 break;
Chris Lattner980e5aa2007-05-01 05:52:21 +00002211 }
Joe Abbeyacb61942013-02-06 22:14:06 +00002212
Chris Lattner980e5aa2007-05-01 05:52:21 +00002213 // Read a record.
2214 Record.clear();
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002215 Instruction *I = 0;
Chris Lattner5a4251c2013-01-20 02:13:19 +00002216 unsigned BitCode = Stream.readRecord(Entry.ID, Record);
Dan Gohman1224c382009-07-20 21:19:07 +00002217 switch (BitCode) {
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002218 default: // Default behavior: reject
Rafael Espindolae076b532013-11-04 16:16:24 +00002219 return Error(InvalidValue);
Chris Lattner980e5aa2007-05-01 05:52:21 +00002220 case bitc::FUNC_CODE_DECLAREBLOCKS: // DECLAREBLOCKS: [nblocks]
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002221 if (Record.size() < 1 || Record[0] == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002222 return Error(InvalidRecord);
Chris Lattner980e5aa2007-05-01 05:52:21 +00002223 // Create all the basic blocks for the function.
Chris Lattnerf61e6452007-05-03 22:09:51 +00002224 FunctionBBs.resize(Record[0]);
Chris Lattner980e5aa2007-05-01 05:52:21 +00002225 for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i)
Owen Anderson1d0be152009-08-13 21:58:54 +00002226 FunctionBBs[i] = BasicBlock::Create(Context, "", F);
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002227 CurBB = FunctionBBs[0];
2228 continue;
Michael Ilseman407a6162012-11-15 22:34:00 +00002229
Chris Lattnera6245242010-04-03 02:17:50 +00002230 case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN
2231 // This record indicates that the last instruction is at the same
2232 // location as the previous instruction with a location.
2233 I = 0;
Michael Ilseman407a6162012-11-15 22:34:00 +00002234
Chris Lattnera6245242010-04-03 02:17:50 +00002235 // Get the last instruction emitted.
2236 if (CurBB && !CurBB->empty())
2237 I = &CurBB->back();
2238 else if (CurBBNo && FunctionBBs[CurBBNo-1] &&
2239 !FunctionBBs[CurBBNo-1]->empty())
2240 I = &FunctionBBs[CurBBNo-1]->back();
Michael Ilseman407a6162012-11-15 22:34:00 +00002241
Rafael Espindolae076b532013-11-04 16:16:24 +00002242 if (I == 0)
2243 return Error(InvalidRecord);
Chris Lattnera6245242010-04-03 02:17:50 +00002244 I->setDebugLoc(LastLoc);
2245 I = 0;
2246 continue;
Michael Ilseman407a6162012-11-15 22:34:00 +00002247
Chris Lattner4f6bab92011-06-17 18:17:37 +00002248 case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia]
Chris Lattnera6245242010-04-03 02:17:50 +00002249 I = 0; // Get the last instruction emitted.
2250 if (CurBB && !CurBB->empty())
2251 I = &CurBB->back();
2252 else if (CurBBNo && FunctionBBs[CurBBNo-1] &&
2253 !FunctionBBs[CurBBNo-1]->empty())
2254 I = &FunctionBBs[CurBBNo-1]->back();
2255 if (I == 0 || Record.size() < 4)
Rafael Espindolae076b532013-11-04 16:16:24 +00002256 return Error(InvalidRecord);
Michael Ilseman407a6162012-11-15 22:34:00 +00002257
Chris Lattnera6245242010-04-03 02:17:50 +00002258 unsigned Line = Record[0], Col = Record[1];
2259 unsigned ScopeID = Record[2], IAID = Record[3];
Michael Ilseman407a6162012-11-15 22:34:00 +00002260
Chris Lattnera6245242010-04-03 02:17:50 +00002261 MDNode *Scope = 0, *IA = 0;
2262 if (ScopeID) Scope = cast<MDNode>(MDValueList.getValueFwdRef(ScopeID-1));
2263 if (IAID) IA = cast<MDNode>(MDValueList.getValueFwdRef(IAID-1));
2264 LastLoc = DebugLoc::get(Line, Col, Scope, IA);
2265 I->setDebugLoc(LastLoc);
2266 I = 0;
2267 continue;
2268 }
2269
Chris Lattnerabfbf852007-05-06 00:21:25 +00002270 case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode]
2271 unsigned OpNum = 0;
2272 Value *LHS, *RHS;
2273 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002274 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
Dan Gohman1224c382009-07-20 21:19:07 +00002275 OpNum+1 > Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002276 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002277
Dan Gohman1224c382009-07-20 21:19:07 +00002278 int Opc = GetDecodedBinaryOpcode(Record[OpNum++], LHS->getType());
Rafael Espindolae076b532013-11-04 16:16:24 +00002279 if (Opc == -1)
2280 return Error(InvalidRecord);
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002281 I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
Devang Patele8e02132009-09-18 19:26:43 +00002282 InstructionList.push_back(I);
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002283 if (OpNum < Record.size()) {
2284 if (Opc == Instruction::Add ||
2285 Opc == Instruction::Sub ||
Chris Lattnerf067d582011-02-07 16:40:21 +00002286 Opc == Instruction::Mul ||
2287 Opc == Instruction::Shl) {
Dan Gohman26793ed2010-01-25 21:55:39 +00002288 if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP))
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002289 cast<BinaryOperator>(I)->setHasNoSignedWrap(true);
Dan Gohman26793ed2010-01-25 21:55:39 +00002290 if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002291 cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true);
Chris Lattner35bda892011-02-06 21:44:57 +00002292 } else if (Opc == Instruction::SDiv ||
Chris Lattnerf067d582011-02-07 16:40:21 +00002293 Opc == Instruction::UDiv ||
2294 Opc == Instruction::LShr ||
2295 Opc == Instruction::AShr) {
Chris Lattner35bda892011-02-06 21:44:57 +00002296 if (Record[OpNum] & (1 << bitc::PEO_EXACT))
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002297 cast<BinaryOperator>(I)->setIsExact(true);
Michael Ilseman495d10a2012-11-27 00:43:38 +00002298 } else if (isa<FPMathOperator>(I)) {
2299 FastMathFlags FMF;
Michael Ilseman1638b832012-12-09 21:12:04 +00002300 if (0 != (Record[OpNum] & FastMathFlags::UnsafeAlgebra))
2301 FMF.setUnsafeAlgebra();
2302 if (0 != (Record[OpNum] & FastMathFlags::NoNaNs))
2303 FMF.setNoNaNs();
2304 if (0 != (Record[OpNum] & FastMathFlags::NoInfs))
2305 FMF.setNoInfs();
2306 if (0 != (Record[OpNum] & FastMathFlags::NoSignedZeros))
2307 FMF.setNoSignedZeros();
2308 if (0 != (Record[OpNum] & FastMathFlags::AllowReciprocal))
2309 FMF.setAllowReciprocal();
Michael Ilseman495d10a2012-11-27 00:43:38 +00002310 if (FMF.any())
2311 I->setFastMathFlags(FMF);
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002312 }
Michael Ilseman495d10a2012-11-27 00:43:38 +00002313
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002314 }
Chris Lattner980e5aa2007-05-01 05:52:21 +00002315 break;
2316 }
Chris Lattnerabfbf852007-05-06 00:21:25 +00002317 case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc]
2318 unsigned OpNum = 0;
2319 Value *Op;
2320 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
2321 OpNum+2 != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002322 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002323
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002324 Type *ResTy = getTypeByID(Record[OpNum]);
Chris Lattnerabfbf852007-05-06 00:21:25 +00002325 int Opc = GetDecodedCastOpcode(Record[OpNum+1]);
2326 if (Opc == -1 || ResTy == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002327 return Error(InvalidRecord);
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002328 I = CastInst::Create((Instruction::CastOps)Opc, Op, ResTy);
Devang Patele8e02132009-09-18 19:26:43 +00002329 InstructionList.push_back(I);
Chris Lattner231cbcb2007-05-02 04:27:25 +00002330 break;
2331 }
Dan Gohmandd8004d2009-07-27 21:53:46 +00002332 case bitc::FUNC_CODE_INST_INBOUNDS_GEP:
Chris Lattner15e6d172007-05-04 19:11:41 +00002333 case bitc::FUNC_CODE_INST_GEP: { // GEP: [n x operands]
Chris Lattner7337ab92007-05-06 00:00:00 +00002334 unsigned OpNum = 0;
2335 Value *BasePtr;
2336 if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr))
Rafael Espindolae076b532013-11-04 16:16:24 +00002337 return Error(InvalidRecord);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002338
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002339 SmallVector<Value*, 16> GEPIdx;
Chris Lattner7337ab92007-05-06 00:00:00 +00002340 while (OpNum != Record.size()) {
2341 Value *Op;
2342 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolae076b532013-11-04 16:16:24 +00002343 return Error(InvalidRecord);
Chris Lattner7337ab92007-05-06 00:00:00 +00002344 GEPIdx.push_back(Op);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002345 }
2346
Jay Foada9203102011-07-25 09:48:08 +00002347 I = GetElementPtrInst::Create(BasePtr, GEPIdx);
Devang Patele8e02132009-09-18 19:26:43 +00002348 InstructionList.push_back(I);
Dan Gohmandd8004d2009-07-27 21:53:46 +00002349 if (BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP)
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002350 cast<GetElementPtrInst>(I)->setIsInBounds(true);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002351 break;
2352 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002353
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002354 case bitc::FUNC_CODE_INST_EXTRACTVAL: {
2355 // EXTRACTVAL: [opty, opval, n x indices]
Dan Gohmane4977cf2008-05-23 01:55:30 +00002356 unsigned OpNum = 0;
2357 Value *Agg;
2358 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
Rafael Espindolae076b532013-11-04 16:16:24 +00002359 return Error(InvalidRecord);
Dan Gohmane4977cf2008-05-23 01:55:30 +00002360
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002361 SmallVector<unsigned, 4> EXTRACTVALIdx;
2362 for (unsigned RecSize = Record.size();
2363 OpNum != RecSize; ++OpNum) {
2364 uint64_t Index = Record[OpNum];
2365 if ((unsigned)Index != Index)
Rafael Espindolae076b532013-11-04 16:16:24 +00002366 return Error(InvalidValue);
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002367 EXTRACTVALIdx.push_back((unsigned)Index);
Dan Gohmane4977cf2008-05-23 01:55:30 +00002368 }
2369
Jay Foadfc6d3a42011-07-13 10:26:04 +00002370 I = ExtractValueInst::Create(Agg, EXTRACTVALIdx);
Devang Patele8e02132009-09-18 19:26:43 +00002371 InstructionList.push_back(I);
Dan Gohmane4977cf2008-05-23 01:55:30 +00002372 break;
2373 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002374
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002375 case bitc::FUNC_CODE_INST_INSERTVAL: {
2376 // INSERTVAL: [opty, opval, opty, opval, n x indices]
Dan Gohmane4977cf2008-05-23 01:55:30 +00002377 unsigned OpNum = 0;
2378 Value *Agg;
2379 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
Rafael Espindolae076b532013-11-04 16:16:24 +00002380 return Error(InvalidRecord);
Dan Gohmane4977cf2008-05-23 01:55:30 +00002381 Value *Val;
2382 if (getValueTypePair(Record, OpNum, NextValueNo, Val))
Rafael Espindolae076b532013-11-04 16:16:24 +00002383 return Error(InvalidRecord);
Dan Gohmane4977cf2008-05-23 01:55:30 +00002384
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002385 SmallVector<unsigned, 4> INSERTVALIdx;
2386 for (unsigned RecSize = Record.size();
2387 OpNum != RecSize; ++OpNum) {
2388 uint64_t Index = Record[OpNum];
2389 if ((unsigned)Index != Index)
Rafael Espindolae076b532013-11-04 16:16:24 +00002390 return Error(InvalidValue);
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002391 INSERTVALIdx.push_back((unsigned)Index);
Dan Gohmane4977cf2008-05-23 01:55:30 +00002392 }
2393
Jay Foadfc6d3a42011-07-13 10:26:04 +00002394 I = InsertValueInst::Create(Agg, Val, INSERTVALIdx);
Devang Patele8e02132009-09-18 19:26:43 +00002395 InstructionList.push_back(I);
Dan Gohmane4977cf2008-05-23 01:55:30 +00002396 break;
2397 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002398
Chris Lattnerabfbf852007-05-06 00:21:25 +00002399 case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval]
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +00002400 // obsolete form of select
2401 // handles select i1 ... in old bitcode
Chris Lattnerabfbf852007-05-06 00:21:25 +00002402 unsigned OpNum = 0;
2403 Value *TrueVal, *FalseVal, *Cond;
2404 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002405 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
2406 popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond))
Rafael Espindolae076b532013-11-04 16:16:24 +00002407 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002408
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +00002409 I = SelectInst::Create(Cond, TrueVal, FalseVal);
Devang Patele8e02132009-09-18 19:26:43 +00002410 InstructionList.push_back(I);
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +00002411 break;
2412 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002413
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +00002414 case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred]
2415 // new form of select
2416 // handles select i1 or select [N x i1]
2417 unsigned OpNum = 0;
2418 Value *TrueVal, *FalseVal, *Cond;
2419 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002420 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +00002421 getValueTypePair(Record, OpNum, NextValueNo, Cond))
Rafael Espindolae076b532013-11-04 16:16:24 +00002422 return Error(InvalidRecord);
Dan Gohmanf72fb672008-09-09 01:02:47 +00002423
2424 // select condition can be either i1 or [N x i1]
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002425 if (VectorType* vector_type =
2426 dyn_cast<VectorType>(Cond->getType())) {
Dan Gohmanf72fb672008-09-09 01:02:47 +00002427 // expect <n x i1>
Daniel Dunbara279bc32009-09-20 02:20:51 +00002428 if (vector_type->getElementType() != Type::getInt1Ty(Context))
Rafael Espindolae076b532013-11-04 16:16:24 +00002429 return Error(InvalidTypeForValue);
Dan Gohmanf72fb672008-09-09 01:02:47 +00002430 } else {
2431 // expect i1
Daniel Dunbara279bc32009-09-20 02:20:51 +00002432 if (Cond->getType() != Type::getInt1Ty(Context))
Rafael Espindolae076b532013-11-04 16:16:24 +00002433 return Error(InvalidTypeForValue);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002434 }
2435
Gabor Greif051a9502008-04-06 20:25:17 +00002436 I = SelectInst::Create(Cond, TrueVal, FalseVal);
Devang Patele8e02132009-09-18 19:26:43 +00002437 InstructionList.push_back(I);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002438 break;
2439 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002440
Chris Lattner01ff65f2007-05-02 05:16:49 +00002441 case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval]
Chris Lattnerabfbf852007-05-06 00:21:25 +00002442 unsigned OpNum = 0;
2443 Value *Vec, *Idx;
2444 if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002445 popValue(Record, OpNum, NextValueNo, Type::getInt32Ty(Context), Idx))
Rafael Espindolae076b532013-11-04 16:16:24 +00002446 return Error(InvalidRecord);
Eric Christophera3500da2009-07-25 02:28:41 +00002447 I = ExtractElementInst::Create(Vec, Idx);
Devang Patele8e02132009-09-18 19:26:43 +00002448 InstructionList.push_back(I);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002449 break;
2450 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002451
Chris Lattner01ff65f2007-05-02 05:16:49 +00002452 case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval]
Chris Lattnerabfbf852007-05-06 00:21:25 +00002453 unsigned OpNum = 0;
2454 Value *Vec, *Elt, *Idx;
2455 if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002456 popValue(Record, OpNum, NextValueNo,
Chris Lattnerabfbf852007-05-06 00:21:25 +00002457 cast<VectorType>(Vec->getType())->getElementType(), Elt) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002458 popValue(Record, OpNum, NextValueNo, Type::getInt32Ty(Context), Idx))
Rafael Espindolae076b532013-11-04 16:16:24 +00002459 return Error(InvalidRecord);
Gabor Greif051a9502008-04-06 20:25:17 +00002460 I = InsertElementInst::Create(Vec, Elt, Idx);
Devang Patele8e02132009-09-18 19:26:43 +00002461 InstructionList.push_back(I);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002462 break;
2463 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002464
Chris Lattnerabfbf852007-05-06 00:21:25 +00002465 case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval]
2466 unsigned OpNum = 0;
2467 Value *Vec1, *Vec2, *Mask;
2468 if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002469 popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2))
Rafael Espindolae076b532013-11-04 16:16:24 +00002470 return Error(InvalidRecord);
Chris Lattnerabfbf852007-05-06 00:21:25 +00002471
Mon P Wangaeb06d22008-11-10 04:46:22 +00002472 if (getValueTypePair(Record, OpNum, NextValueNo, Mask))
Rafael Espindolae076b532013-11-04 16:16:24 +00002473 return Error(InvalidRecord);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002474 I = new ShuffleVectorInst(Vec1, Vec2, Mask);
Devang Patele8e02132009-09-18 19:26:43 +00002475 InstructionList.push_back(I);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002476 break;
2477 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002478
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002479 case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred]
2480 // Old form of ICmp/FCmp returning bool
2481 // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were
2482 // both legal on vectors but had different behaviour.
2483 case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred]
2484 // FCmp/ICmp returning bool or vector of bool
2485
Chris Lattner7337ab92007-05-06 00:00:00 +00002486 unsigned OpNum = 0;
2487 Value *LHS, *RHS;
2488 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002489 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
Chris Lattner7337ab92007-05-06 00:00:00 +00002490 OpNum+1 != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002491 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002492
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002493 if (LHS->getType()->isFPOrFPVectorTy())
Dan Gohman1c8a23c2009-08-25 23:17:54 +00002494 I = new FCmpInst((FCmpInst::Predicate)Record[OpNum], LHS, RHS);
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002495 else
Dan Gohman1c8a23c2009-08-25 23:17:54 +00002496 I = new ICmpInst((ICmpInst::Predicate)Record[OpNum], LHS, RHS);
Devang Patele8e02132009-09-18 19:26:43 +00002497 InstructionList.push_back(I);
Dan Gohmanf72fb672008-09-09 01:02:47 +00002498 break;
2499 }
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002500
Chris Lattner231cbcb2007-05-02 04:27:25 +00002501 case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>]
Devang Pateld9d99ff2008-02-26 01:29:32 +00002502 {
2503 unsigned Size = Record.size();
2504 if (Size == 0) {
Owen Anderson1d0be152009-08-13 21:58:54 +00002505 I = ReturnInst::Create(Context);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002506 InstructionList.push_back(I);
Devang Pateld9d99ff2008-02-26 01:29:32 +00002507 break;
Dan Gohmanfc74abf2008-07-23 00:34:11 +00002508 }
Devang Pateld9d99ff2008-02-26 01:29:32 +00002509
Dan Gohmanfc74abf2008-07-23 00:34:11 +00002510 unsigned OpNum = 0;
Chris Lattner96a74c52011-06-17 18:09:11 +00002511 Value *Op = NULL;
2512 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolae076b532013-11-04 16:16:24 +00002513 return Error(InvalidRecord);
Chris Lattner96a74c52011-06-17 18:09:11 +00002514 if (OpNum != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002515 return Error(InvalidRecord);
Dan Gohmanfc74abf2008-07-23 00:34:11 +00002516
Chris Lattner96a74c52011-06-17 18:09:11 +00002517 I = ReturnInst::Create(Context, Op);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002518 InstructionList.push_back(I);
Dan Gohmanfc74abf2008-07-23 00:34:11 +00002519 break;
Chris Lattner231cbcb2007-05-02 04:27:25 +00002520 }
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002521 case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#]
Chris Lattnerf61e6452007-05-03 22:09:51 +00002522 if (Record.size() != 1 && Record.size() != 3)
Rafael Espindolae076b532013-11-04 16:16:24 +00002523 return Error(InvalidRecord);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002524 BasicBlock *TrueDest = getBasicBlock(Record[0]);
2525 if (TrueDest == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002526 return Error(InvalidRecord);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002527
Devang Patele8e02132009-09-18 19:26:43 +00002528 if (Record.size() == 1) {
Gabor Greif051a9502008-04-06 20:25:17 +00002529 I = BranchInst::Create(TrueDest);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002530 InstructionList.push_back(I);
Devang Patele8e02132009-09-18 19:26:43 +00002531 }
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002532 else {
2533 BasicBlock *FalseDest = getBasicBlock(Record[1]);
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002534 Value *Cond = getValue(Record, 2, NextValueNo,
2535 Type::getInt1Ty(Context));
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002536 if (FalseDest == 0 || Cond == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002537 return Error(InvalidRecord);
Gabor Greif051a9502008-04-06 20:25:17 +00002538 I = BranchInst::Create(TrueDest, FalseDest, Cond);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002539 InstructionList.push_back(I);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002540 }
2541 break;
2542 }
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002543 case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...]
Michael Ilseman407a6162012-11-15 22:34:00 +00002544 // Check magic
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002545 if ((Record[0] >> 16) == SWITCH_INST_MAGIC) {
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002546 // "New" SwitchInst format with case ranges. The changes to write this
2547 // format were reverted but we still recognize bitcode that uses it.
2548 // Hopefully someday we will have support for case ranges and can use
2549 // this format again.
Michael Ilseman407a6162012-11-15 22:34:00 +00002550
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002551 Type *OpTy = getTypeByID(Record[1]);
2552 unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth();
2553
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002554 Value *Cond = getValue(Record, 2, NextValueNo, OpTy);
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002555 BasicBlock *Default = getBasicBlock(Record[3]);
2556 if (OpTy == 0 || Cond == 0 || Default == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002557 return Error(InvalidRecord);
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002558
2559 unsigned NumCases = Record[4];
Michael Ilseman407a6162012-11-15 22:34:00 +00002560
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002561 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
2562 InstructionList.push_back(SI);
Michael Ilseman407a6162012-11-15 22:34:00 +00002563
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002564 unsigned CurIdx = 5;
2565 for (unsigned i = 0; i != NumCases; ++i) {
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002566 SmallVector<ConstantInt*, 1> CaseVals;
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002567 unsigned NumItems = Record[CurIdx++];
2568 for (unsigned ci = 0; ci != NumItems; ++ci) {
2569 bool isSingleNumber = Record[CurIdx++];
Michael Ilseman407a6162012-11-15 22:34:00 +00002570
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002571 APInt Low;
2572 unsigned ActiveWords = 1;
2573 if (ValueBitWidth > 64)
2574 ActiveWords = Record[CurIdx++];
Benjamin Kramerf52aea82012-05-28 14:10:31 +00002575 Low = ReadWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
2576 ValueBitWidth);
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002577 CurIdx += ActiveWords;
Stepan Dyatkovskiy484fc932012-05-28 12:39:09 +00002578
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002579 if (!isSingleNumber) {
2580 ActiveWords = 1;
2581 if (ValueBitWidth > 64)
2582 ActiveWords = Record[CurIdx++];
2583 APInt High =
Benjamin Kramerf52aea82012-05-28 14:10:31 +00002584 ReadWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
2585 ValueBitWidth);
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002586 CurIdx += ActiveWords;
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002587
2588 // FIXME: It is not clear whether values in the range should be
2589 // compared as signed or unsigned values. The partially
2590 // implemented changes that used this format in the past used
2591 // unsigned comparisons.
2592 for ( ; Low.ule(High); ++Low)
2593 CaseVals.push_back(ConstantInt::get(Context, Low));
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002594 } else
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002595 CaseVals.push_back(ConstantInt::get(Context, Low));
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002596 }
2597 BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]);
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002598 for (SmallVector<ConstantInt*, 1>::iterator cvi = CaseVals.begin(),
2599 cve = CaseVals.end(); cvi != cve; ++cvi)
2600 SI->addCase(*cvi, DestBB);
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002601 }
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002602 I = SI;
2603 break;
2604 }
Michael Ilseman407a6162012-11-15 22:34:00 +00002605
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002606 // Old SwitchInst format without case ranges.
Michael Ilseman407a6162012-11-15 22:34:00 +00002607
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002608 if (Record.size() < 3 || (Record.size() & 1) == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002609 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002610 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002611 Value *Cond = getValue(Record, 1, NextValueNo, OpTy);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002612 BasicBlock *Default = getBasicBlock(Record[2]);
2613 if (OpTy == 0 || Cond == 0 || Default == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002614 return Error(InvalidRecord);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002615 unsigned NumCases = (Record.size()-3)/2;
Gabor Greif051a9502008-04-06 20:25:17 +00002616 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
Devang Patele8e02132009-09-18 19:26:43 +00002617 InstructionList.push_back(SI);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002618 for (unsigned i = 0, e = NumCases; i != e; ++i) {
Daniel Dunbara279bc32009-09-20 02:20:51 +00002619 ConstantInt *CaseVal =
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002620 dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy));
2621 BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]);
2622 if (CaseVal == 0 || DestBB == 0) {
2623 delete SI;
Rafael Espindolae076b532013-11-04 16:16:24 +00002624 return Error(InvalidRecord);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002625 }
2626 SI->addCase(CaseVal, DestBB);
2627 }
2628 I = SI;
2629 break;
2630 }
Chris Lattnerab21db72009-10-28 00:19:10 +00002631 case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...]
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002632 if (Record.size() < 2)
Rafael Espindolae076b532013-11-04 16:16:24 +00002633 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002634 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002635 Value *Address = getValue(Record, 1, NextValueNo, OpTy);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002636 if (OpTy == 0 || Address == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002637 return Error(InvalidRecord);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002638 unsigned NumDests = Record.size()-2;
Chris Lattnerab21db72009-10-28 00:19:10 +00002639 IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002640 InstructionList.push_back(IBI);
2641 for (unsigned i = 0, e = NumDests; i != e; ++i) {
2642 if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) {
2643 IBI->addDestination(DestBB);
2644 } else {
2645 delete IBI;
Rafael Espindolae076b532013-11-04 16:16:24 +00002646 return Error(InvalidRecord);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002647 }
2648 }
2649 I = IBI;
2650 break;
2651 }
Michael Ilseman407a6162012-11-15 22:34:00 +00002652
Duncan Sandsdc024672007-11-27 13:23:08 +00002653 case bitc::FUNC_CODE_INST_INVOKE: {
2654 // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
Rafael Espindolae076b532013-11-04 16:16:24 +00002655 if (Record.size() < 4)
2656 return Error(InvalidRecord);
Bill Wendling99faa3b2012-12-07 23:16:57 +00002657 AttributeSet PAL = getAttributes(Record[0]);
Chris Lattnera9bb7132007-05-08 05:38:01 +00002658 unsigned CCInfo = Record[1];
2659 BasicBlock *NormalBB = getBasicBlock(Record[2]);
2660 BasicBlock *UnwindBB = getBasicBlock(Record[3]);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002661
Chris Lattnera9bb7132007-05-08 05:38:01 +00002662 unsigned OpNum = 4;
Chris Lattner7337ab92007-05-06 00:00:00 +00002663 Value *Callee;
2664 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
Rafael Espindolae076b532013-11-04 16:16:24 +00002665 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002666
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002667 PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType());
2668 FunctionType *FTy = !CalleeTy ? 0 :
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002669 dyn_cast<FunctionType>(CalleeTy->getElementType());
2670
2671 // Check that the right number of fixed parameters are here.
Chris Lattner7337ab92007-05-06 00:00:00 +00002672 if (FTy == 0 || NormalBB == 0 || UnwindBB == 0 ||
2673 Record.size() < OpNum+FTy->getNumParams())
Rafael Espindolae076b532013-11-04 16:16:24 +00002674 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002675
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002676 SmallVector<Value*, 16> Ops;
Chris Lattner7337ab92007-05-06 00:00:00 +00002677 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002678 Ops.push_back(getValue(Record, OpNum, NextValueNo,
2679 FTy->getParamType(i)));
Rafael Espindolae076b532013-11-04 16:16:24 +00002680 if (Ops.back() == 0)
2681 return Error(InvalidRecord);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002682 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002683
Chris Lattner7337ab92007-05-06 00:00:00 +00002684 if (!FTy->isVarArg()) {
2685 if (Record.size() != OpNum)
Rafael Espindolae076b532013-11-04 16:16:24 +00002686 return Error(InvalidRecord);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002687 } else {
Chris Lattner7337ab92007-05-06 00:00:00 +00002688 // Read type/value pairs for varargs params.
2689 while (OpNum != Record.size()) {
2690 Value *Op;
2691 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolae076b532013-11-04 16:16:24 +00002692 return Error(InvalidRecord);
Chris Lattner7337ab92007-05-06 00:00:00 +00002693 Ops.push_back(Op);
2694 }
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002695 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002696
Jay Foada3efbb12011-07-15 08:37:34 +00002697 I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops);
Devang Patele8e02132009-09-18 19:26:43 +00002698 InstructionList.push_back(I);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002699 cast<InvokeInst>(I)->setCallingConv(
2700 static_cast<CallingConv::ID>(CCInfo));
Devang Patel05988662008-09-25 21:00:45 +00002701 cast<InvokeInst>(I)->setAttributes(PAL);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002702 break;
2703 }
Bill Wendlingdccc03b2011-07-31 06:30:59 +00002704 case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval]
2705 unsigned Idx = 0;
2706 Value *Val = 0;
2707 if (getValueTypePair(Record, Idx, NextValueNo, Val))
Rafael Espindolae076b532013-11-04 16:16:24 +00002708 return Error(InvalidRecord);
Bill Wendlingdccc03b2011-07-31 06:30:59 +00002709 I = ResumeInst::Create(Val);
Bill Wendling35726bf2011-09-01 00:50:20 +00002710 InstructionList.push_back(I);
Bill Wendlingdccc03b2011-07-31 06:30:59 +00002711 break;
2712 }
Chris Lattner231cbcb2007-05-02 04:27:25 +00002713 case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE
Owen Anderson1d0be152009-08-13 21:58:54 +00002714 I = new UnreachableInst(Context);
Devang Patele8e02132009-09-18 19:26:43 +00002715 InstructionList.push_back(I);
Chris Lattner231cbcb2007-05-02 04:27:25 +00002716 break;
Chris Lattnerabfbf852007-05-06 00:21:25 +00002717 case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...]
Chris Lattner15e6d172007-05-04 19:11:41 +00002718 if (Record.size() < 1 || ((Record.size()-1)&1))
Rafael Espindolae076b532013-11-04 16:16:24 +00002719 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002720 Type *Ty = getTypeByID(Record[0]);
Rafael Espindolae076b532013-11-04 16:16:24 +00002721 if (!Ty)
2722 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002723
Jay Foad3ecfc862011-03-30 11:28:46 +00002724 PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2);
Devang Patele8e02132009-09-18 19:26:43 +00002725 InstructionList.push_back(PN);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002726
Chris Lattner15e6d172007-05-04 19:11:41 +00002727 for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) {
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002728 Value *V;
2729 // With the new function encoding, it is possible that operands have
2730 // negative IDs (for forward references). Use a signed VBR
2731 // representation to keep the encoding small.
2732 if (UseRelativeIDs)
2733 V = getValueSigned(Record, 1+i, NextValueNo, Ty);
2734 else
2735 V = getValue(Record, 1+i, NextValueNo, Ty);
Chris Lattner15e6d172007-05-04 19:11:41 +00002736 BasicBlock *BB = getBasicBlock(Record[2+i]);
Rafael Espindolae076b532013-11-04 16:16:24 +00002737 if (!V || !BB)
2738 return Error(InvalidRecord);
Chris Lattner2a98cca2007-05-03 18:58:09 +00002739 PN->addIncoming(V, BB);
2740 }
2741 I = PN;
2742 break;
2743 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002744
Bill Wendlinge6e88262011-08-12 20:24:12 +00002745 case bitc::FUNC_CODE_INST_LANDINGPAD: {
2746 // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?]
2747 unsigned Idx = 0;
2748 if (Record.size() < 4)
Rafael Espindolae076b532013-11-04 16:16:24 +00002749 return Error(InvalidRecord);
Bill Wendlinge6e88262011-08-12 20:24:12 +00002750 Type *Ty = getTypeByID(Record[Idx++]);
Rafael Espindolae076b532013-11-04 16:16:24 +00002751 if (!Ty)
2752 return Error(InvalidRecord);
Bill Wendlinge6e88262011-08-12 20:24:12 +00002753 Value *PersFn = 0;
2754 if (getValueTypePair(Record, Idx, NextValueNo, PersFn))
Rafael Espindolae076b532013-11-04 16:16:24 +00002755 return Error(InvalidRecord);
Bill Wendlinge6e88262011-08-12 20:24:12 +00002756
2757 bool IsCleanup = !!Record[Idx++];
2758 unsigned NumClauses = Record[Idx++];
2759 LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, NumClauses);
2760 LP->setCleanup(IsCleanup);
2761 for (unsigned J = 0; J != NumClauses; ++J) {
2762 LandingPadInst::ClauseType CT =
2763 LandingPadInst::ClauseType(Record[Idx++]); (void)CT;
2764 Value *Val;
2765
2766 if (getValueTypePair(Record, Idx, NextValueNo, Val)) {
2767 delete LP;
Rafael Espindolae076b532013-11-04 16:16:24 +00002768 return Error(InvalidRecord);
Bill Wendlinge6e88262011-08-12 20:24:12 +00002769 }
2770
2771 assert((CT != LandingPadInst::Catch ||
2772 !isa<ArrayType>(Val->getType())) &&
2773 "Catch clause has a invalid type!");
2774 assert((CT != LandingPadInst::Filter ||
2775 isa<ArrayType>(Val->getType())) &&
2776 "Filter clause has invalid type!");
2777 LP->addClause(Val);
2778 }
2779
2780 I = LP;
Bill Wendling35726bf2011-09-01 00:50:20 +00002781 InstructionList.push_back(I);
Bill Wendlinge6e88262011-08-12 20:24:12 +00002782 break;
2783 }
2784
Chris Lattner96a74c52011-06-17 18:09:11 +00002785 case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]
2786 if (Record.size() != 4)
Rafael Espindolae076b532013-11-04 16:16:24 +00002787 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002788 PointerType *Ty =
Chris Lattner2a98cca2007-05-03 18:58:09 +00002789 dyn_cast_or_null<PointerType>(getTypeByID(Record[0]));
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002790 Type *OpTy = getTypeByID(Record[1]);
Chris Lattner96a74c52011-06-17 18:09:11 +00002791 Value *Size = getFnValueByID(Record[2], OpTy);
2792 unsigned Align = Record[3];
Rafael Espindolae076b532013-11-04 16:16:24 +00002793 if (!Ty || !Size)
2794 return Error(InvalidRecord);
Owen Anderson50dead02009-07-15 23:53:25 +00002795 I = new AllocaInst(Ty->getElementType(), Size, (1 << Align) >> 1);
Devang Patele8e02132009-09-18 19:26:43 +00002796 InstructionList.push_back(I);
Chris Lattner2a98cca2007-05-03 18:58:09 +00002797 break;
2798 }
Chris Lattner0579f7f2007-05-03 22:04:19 +00002799 case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol]
Chris Lattner7337ab92007-05-06 00:00:00 +00002800 unsigned OpNum = 0;
2801 Value *Op;
2802 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
2803 OpNum+2 != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002804 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002805
Chris Lattner7337ab92007-05-06 00:00:00 +00002806 I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1);
Devang Patele8e02132009-09-18 19:26:43 +00002807 InstructionList.push_back(I);
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002808 break;
Chris Lattner0579f7f2007-05-03 22:04:19 +00002809 }
Eli Friedman21006d42011-08-09 23:02:53 +00002810 case bitc::FUNC_CODE_INST_LOADATOMIC: {
2811 // LOADATOMIC: [opty, op, align, vol, ordering, synchscope]
2812 unsigned OpNum = 0;
2813 Value *Op;
2814 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
2815 OpNum+4 != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002816 return Error(InvalidRecord);
Michael Ilseman407a6162012-11-15 22:34:00 +00002817
Eli Friedman21006d42011-08-09 23:02:53 +00002818
2819 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
2820 if (Ordering == NotAtomic || Ordering == Release ||
2821 Ordering == AcquireRelease)
Rafael Espindolae076b532013-11-04 16:16:24 +00002822 return Error(InvalidRecord);
Eli Friedman21006d42011-08-09 23:02:53 +00002823 if (Ordering != NotAtomic && Record[OpNum] == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002824 return Error(InvalidRecord);
Eli Friedman21006d42011-08-09 23:02:53 +00002825 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
2826
2827 I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1,
2828 Ordering, SynchScope);
2829 InstructionList.push_back(I);
2830 break;
2831 }
Chris Lattner4f6bab92011-06-17 18:17:37 +00002832 case bitc::FUNC_CODE_INST_STORE: { // STORE2:[ptrty, ptr, val, align, vol]
Christopher Lambfe63fb92007-12-11 08:59:05 +00002833 unsigned OpNum = 0;
2834 Value *Val, *Ptr;
2835 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002836 popValue(Record, OpNum, NextValueNo,
Christopher Lambfe63fb92007-12-11 08:59:05 +00002837 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
2838 OpNum+2 != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002839 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002840
Christopher Lambfe63fb92007-12-11 08:59:05 +00002841 I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1);
Devang Patele8e02132009-09-18 19:26:43 +00002842 InstructionList.push_back(I);
Christopher Lambfe63fb92007-12-11 08:59:05 +00002843 break;
2844 }
Eli Friedman21006d42011-08-09 23:02:53 +00002845 case bitc::FUNC_CODE_INST_STOREATOMIC: {
2846 // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, synchscope]
2847 unsigned OpNum = 0;
2848 Value *Val, *Ptr;
2849 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002850 popValue(Record, OpNum, NextValueNo,
Eli Friedman21006d42011-08-09 23:02:53 +00002851 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
2852 OpNum+4 != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002853 return Error(InvalidRecord);
Eli Friedman21006d42011-08-09 23:02:53 +00002854
2855 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
Eli Friedmanc3d35982011-09-19 19:41:28 +00002856 if (Ordering == NotAtomic || Ordering == Acquire ||
Eli Friedman21006d42011-08-09 23:02:53 +00002857 Ordering == AcquireRelease)
Rafael Espindolae076b532013-11-04 16:16:24 +00002858 return Error(InvalidRecord);
Eli Friedman21006d42011-08-09 23:02:53 +00002859 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
2860 if (Ordering != NotAtomic && Record[OpNum] == 0)
Rafael Espindolae076b532013-11-04 16:16:24 +00002861 return Error(InvalidRecord);
Eli Friedman21006d42011-08-09 23:02:53 +00002862
2863 I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1,
2864 Ordering, SynchScope);
2865 InstructionList.push_back(I);
2866 break;
2867 }
Eli Friedmanff030482011-07-28 21:48:00 +00002868 case bitc::FUNC_CODE_INST_CMPXCHG: {
2869 // CMPXCHG:[ptrty, ptr, cmp, new, vol, ordering, synchscope]
2870 unsigned OpNum = 0;
2871 Value *Ptr, *Cmp, *New;
2872 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002873 popValue(Record, OpNum, NextValueNo,
Eli Friedmanff030482011-07-28 21:48:00 +00002874 cast<PointerType>(Ptr->getType())->getElementType(), Cmp) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002875 popValue(Record, OpNum, NextValueNo,
Eli Friedmanff030482011-07-28 21:48:00 +00002876 cast<PointerType>(Ptr->getType())->getElementType(), New) ||
2877 OpNum+3 != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002878 return Error(InvalidRecord);
Eli Friedmanff030482011-07-28 21:48:00 +00002879 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+1]);
Eli Friedman21006d42011-08-09 23:02:53 +00002880 if (Ordering == NotAtomic || Ordering == Unordered)
Rafael Espindolae076b532013-11-04 16:16:24 +00002881 return Error(InvalidRecord);
Eli Friedmanff030482011-07-28 21:48:00 +00002882 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+2]);
2883 I = new AtomicCmpXchgInst(Ptr, Cmp, New, Ordering, SynchScope);
2884 cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]);
2885 InstructionList.push_back(I);
2886 break;
2887 }
2888 case bitc::FUNC_CODE_INST_ATOMICRMW: {
2889 // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, synchscope]
2890 unsigned OpNum = 0;
2891 Value *Ptr, *Val;
2892 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002893 popValue(Record, OpNum, NextValueNo,
Eli Friedmanff030482011-07-28 21:48:00 +00002894 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
2895 OpNum+4 != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002896 return Error(InvalidRecord);
Eli Friedmanff030482011-07-28 21:48:00 +00002897 AtomicRMWInst::BinOp Operation = GetDecodedRMWOperation(Record[OpNum]);
2898 if (Operation < AtomicRMWInst::FIRST_BINOP ||
2899 Operation > AtomicRMWInst::LAST_BINOP)
Rafael Espindolae076b532013-11-04 16:16:24 +00002900 return Error(InvalidRecord);
Eli Friedmanff030482011-07-28 21:48:00 +00002901 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
Eli Friedman21006d42011-08-09 23:02:53 +00002902 if (Ordering == NotAtomic || Ordering == Unordered)
Rafael Espindolae076b532013-11-04 16:16:24 +00002903 return Error(InvalidRecord);
Eli Friedmanff030482011-07-28 21:48:00 +00002904 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
2905 I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SynchScope);
2906 cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]);
2907 InstructionList.push_back(I);
2908 break;
2909 }
Eli Friedman47f35132011-07-25 23:16:38 +00002910 case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, synchscope]
2911 if (2 != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002912 return Error(InvalidRecord);
Eli Friedman47f35132011-07-25 23:16:38 +00002913 AtomicOrdering Ordering = GetDecodedOrdering(Record[0]);
2914 if (Ordering == NotAtomic || Ordering == Unordered ||
2915 Ordering == Monotonic)
Rafael Espindolae076b532013-11-04 16:16:24 +00002916 return Error(InvalidRecord);
Eli Friedman47f35132011-07-25 23:16:38 +00002917 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[1]);
2918 I = new FenceInst(Context, Ordering, SynchScope);
2919 InstructionList.push_back(I);
2920 break;
2921 }
Chris Lattner4f6bab92011-06-17 18:17:37 +00002922 case bitc::FUNC_CODE_INST_CALL: {
Duncan Sandsdc024672007-11-27 13:23:08 +00002923 // CALL: [paramattrs, cc, fnty, fnid, arg0, arg1...]
2924 if (Record.size() < 3)
Rafael Espindolae076b532013-11-04 16:16:24 +00002925 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002926
Bill Wendling99faa3b2012-12-07 23:16:57 +00002927 AttributeSet PAL = getAttributes(Record[0]);
Chris Lattnera9bb7132007-05-08 05:38:01 +00002928 unsigned CCInfo = Record[1];
Daniel Dunbara279bc32009-09-20 02:20:51 +00002929
Chris Lattnera9bb7132007-05-08 05:38:01 +00002930 unsigned OpNum = 2;
Chris Lattner7337ab92007-05-06 00:00:00 +00002931 Value *Callee;
2932 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
Rafael Espindolae076b532013-11-04 16:16:24 +00002933 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002934
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002935 PointerType *OpTy = dyn_cast<PointerType>(Callee->getType());
2936 FunctionType *FTy = 0;
Chris Lattner0579f7f2007-05-03 22:04:19 +00002937 if (OpTy) FTy = dyn_cast<FunctionType>(OpTy->getElementType());
Chris Lattner7337ab92007-05-06 00:00:00 +00002938 if (!FTy || Record.size() < FTy->getNumParams()+OpNum)
Rafael Espindolae076b532013-11-04 16:16:24 +00002939 return Error(InvalidRecord);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002940
Chris Lattner0579f7f2007-05-03 22:04:19 +00002941 SmallVector<Value*, 16> Args;
2942 // Read the fixed params.
Chris Lattner7337ab92007-05-06 00:00:00 +00002943 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
Chris Lattner1afcace2011-07-09 17:41:24 +00002944 if (FTy->getParamType(i)->isLabelTy())
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002945 Args.push_back(getBasicBlock(Record[OpNum]));
Dan Gohman9b10dfb2010-09-13 18:00:48 +00002946 else
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002947 Args.push_back(getValue(Record, OpNum, NextValueNo,
2948 FTy->getParamType(i)));
Rafael Espindolae076b532013-11-04 16:16:24 +00002949 if (Args.back() == 0)
2950 return Error(InvalidRecord);
Chris Lattner0579f7f2007-05-03 22:04:19 +00002951 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002952
Chris Lattner0579f7f2007-05-03 22:04:19 +00002953 // Read type/value pairs for varargs params.
Chris Lattner0579f7f2007-05-03 22:04:19 +00002954 if (!FTy->isVarArg()) {
Chris Lattner7337ab92007-05-06 00:00:00 +00002955 if (OpNum != Record.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00002956 return Error(InvalidRecord);
Chris Lattner0579f7f2007-05-03 22:04:19 +00002957 } else {
Chris Lattner7337ab92007-05-06 00:00:00 +00002958 while (OpNum != Record.size()) {
2959 Value *Op;
2960 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolae076b532013-11-04 16:16:24 +00002961 return Error(InvalidRecord);
Chris Lattner7337ab92007-05-06 00:00:00 +00002962 Args.push_back(Op);
Chris Lattner0579f7f2007-05-03 22:04:19 +00002963 }
2964 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002965
Jay Foada3efbb12011-07-15 08:37:34 +00002966 I = CallInst::Create(Callee, Args);
Devang Patele8e02132009-09-18 19:26:43 +00002967 InstructionList.push_back(I);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002968 cast<CallInst>(I)->setCallingConv(
2969 static_cast<CallingConv::ID>(CCInfo>>1));
Chris Lattner76520192007-05-03 22:34:03 +00002970 cast<CallInst>(I)->setTailCall(CCInfo & 1);
Devang Patel05988662008-09-25 21:00:45 +00002971 cast<CallInst>(I)->setAttributes(PAL);
Chris Lattner0579f7f2007-05-03 22:04:19 +00002972 break;
2973 }
2974 case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty]
2975 if (Record.size() < 3)
Rafael Espindolae076b532013-11-04 16:16:24 +00002976 return Error(InvalidRecord);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002977 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungd9a3bad2012-10-11 20:20:40 +00002978 Value *Op = getValue(Record, 1, NextValueNo, OpTy);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002979 Type *ResTy = getTypeByID(Record[2]);
Chris Lattner0579f7f2007-05-03 22:04:19 +00002980 if (!OpTy || !Op || !ResTy)
Rafael Espindolae076b532013-11-04 16:16:24 +00002981 return Error(InvalidRecord);
Chris Lattner0579f7f2007-05-03 22:04:19 +00002982 I = new VAArgInst(Op, ResTy);
Devang Patele8e02132009-09-18 19:26:43 +00002983 InstructionList.push_back(I);
Chris Lattner0579f7f2007-05-03 22:04:19 +00002984 break;
2985 }
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002986 }
2987
2988 // Add instruction to end of current BB. If there is no current BB, reject
2989 // this file.
2990 if (CurBB == 0) {
2991 delete I;
Rafael Espindolae076b532013-11-04 16:16:24 +00002992 return Error(InvalidInstructionWithNoBB);
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002993 }
2994 CurBB->getInstList().push_back(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002995
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002996 // If this was a terminator instruction, move to the next block.
2997 if (isa<TerminatorInst>(I)) {
2998 ++CurBBNo;
2999 CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : 0;
3000 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003001
Chris Lattnera7c49aa2007-05-01 07:01:57 +00003002 // Non-void values get registered in the value table for future use.
Benjamin Kramerf0127052010-01-05 13:12:22 +00003003 if (I && !I->getType()->isVoidTy())
Chris Lattnera7c49aa2007-05-01 07:01:57 +00003004 ValueList.AssignValue(I, NextValueNo++);
Chris Lattner980e5aa2007-05-01 05:52:21 +00003005 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003006
Chris Lattner5a4251c2013-01-20 02:13:19 +00003007OutOfRecordLoop:
Joe Abbeyacb61942013-02-06 22:14:06 +00003008
Chris Lattnera7c49aa2007-05-01 07:01:57 +00003009 // Check the function list for unresolved values.
3010 if (Argument *A = dyn_cast<Argument>(ValueList.back())) {
3011 if (A->getParent() == 0) {
3012 // We found at least one unresolved value. Nuke them all to avoid leaks.
3013 for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){
Dan Gohman56e2a572010-08-25 20:20:21 +00003014 if ((A = dyn_cast<Argument>(ValueList[i])) && A->getParent() == 0) {
Owen Anderson9e9a0d52009-07-30 23:03:37 +00003015 A->replaceAllUsesWith(UndefValue::get(A->getType()));
Chris Lattnera7c49aa2007-05-01 07:01:57 +00003016 delete A;
3017 }
3018 }
Rafael Espindolae076b532013-11-04 16:16:24 +00003019 return Error(NeverResolvedValueFoundInFunction);
Chris Lattnera7c49aa2007-05-01 07:01:57 +00003020 }
Chris Lattnera7c49aa2007-05-01 07:01:57 +00003021 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003022
Dan Gohman064ff3e2010-08-25 20:23:38 +00003023 // FIXME: Check for unresolved forward-declared metadata references
3024 // and clean up leaks.
3025
Chris Lattner50b136d2009-10-28 05:53:48 +00003026 // See if anything took the address of blocks in this function. If so,
3027 // resolve them now.
Chris Lattner50b136d2009-10-28 05:53:48 +00003028 DenseMap<Function*, std::vector<BlockAddrRefTy> >::iterator BAFRI =
3029 BlockAddrFwdRefs.find(F);
3030 if (BAFRI != BlockAddrFwdRefs.end()) {
3031 std::vector<BlockAddrRefTy> &RefList = BAFRI->second;
3032 for (unsigned i = 0, e = RefList.size(); i != e; ++i) {
3033 unsigned BlockIdx = RefList[i].first;
Chris Lattnercdfc9402009-11-01 01:27:45 +00003034 if (BlockIdx >= FunctionBBs.size())
Rafael Espindolae076b532013-11-04 16:16:24 +00003035 return Error(InvalidID);
Michael Ilseman407a6162012-11-15 22:34:00 +00003036
Chris Lattner50b136d2009-10-28 05:53:48 +00003037 GlobalVariable *FwdRef = RefList[i].second;
Chris Lattnercdfc9402009-11-01 01:27:45 +00003038 FwdRef->replaceAllUsesWith(BlockAddress::get(F, FunctionBBs[BlockIdx]));
Chris Lattner50b136d2009-10-28 05:53:48 +00003039 FwdRef->eraseFromParent();
3040 }
Michael Ilseman407a6162012-11-15 22:34:00 +00003041
Chris Lattner50b136d2009-10-28 05:53:48 +00003042 BlockAddrFwdRefs.erase(BAFRI);
3043 }
Michael Ilseman407a6162012-11-15 22:34:00 +00003044
Chris Lattner980e5aa2007-05-01 05:52:21 +00003045 // Trim the value list down to the size it was before we parsed this function.
3046 ValueList.shrinkTo(ModuleValueListSize);
Dan Gohman69813832010-08-25 20:22:53 +00003047 MDValueList.shrinkTo(ModuleMDValueListSize);
Chris Lattner980e5aa2007-05-01 05:52:21 +00003048 std::vector<BasicBlock*>().swap(FunctionBBs);
Rafael Espindolae076b532013-11-04 16:16:24 +00003049 return error_code::success();
Chris Lattner48f84872007-05-01 04:59:48 +00003050}
3051
Derek Schuff2ea93872012-02-06 22:30:29 +00003052/// FindFunctionInStream - Find the function body in the bitcode stream
3053bool BitcodeReader::FindFunctionInStream(Function *F,
3054 DenseMap<Function*, uint64_t>::iterator DeferredFunctionInfoIterator) {
3055 while (DeferredFunctionInfoIterator->second == 0) {
3056 if (Stream.AtEndOfStream())
Rafael Espindolae076b532013-11-04 16:16:24 +00003057 return Error(CouldNotFindFunctionInStream);
Derek Schuff2ea93872012-02-06 22:30:29 +00003058 // ParseModule will parse the next body in the stream and set its
3059 // position in the DeferredFunctionInfo map.
3060 if (ParseModule(true)) return true;
3061 }
3062 return false;
3063}
3064
Chris Lattnerb348bb82007-05-18 04:02:46 +00003065//===----------------------------------------------------------------------===//
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003066// GVMaterializer implementation
Chris Lattnerb348bb82007-05-18 04:02:46 +00003067//===----------------------------------------------------------------------===//
3068
3069
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003070bool BitcodeReader::isMaterializable(const GlobalValue *GV) const {
3071 if (const Function *F = dyn_cast<Function>(GV)) {
3072 return F->isDeclaration() &&
3073 DeferredFunctionInfo.count(const_cast<Function*>(F));
3074 }
3075 return false;
3076}
Daniel Dunbara279bc32009-09-20 02:20:51 +00003077
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003078bool BitcodeReader::Materialize(GlobalValue *GV, std::string *ErrInfo) {
3079 Function *F = dyn_cast<Function>(GV);
3080 // If it's not a function or is already material, ignore the request.
3081 if (!F || !F->isMaterializable()) return false;
3082
3083 DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F);
Chris Lattnerb348bb82007-05-18 04:02:46 +00003084 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
Derek Schuff2ea93872012-02-06 22:30:29 +00003085 // If its position is recorded as 0, its body is somewhere in the stream
3086 // but we haven't seen it yet.
3087 if (DFII->second == 0)
3088 if (LazyStreamer && FindFunctionInStream(F, DFII)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003089
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003090 // Move the bit stream to the saved position of the deferred function body.
3091 Stream.JumpToBit(DFII->second);
Daniel Dunbara279bc32009-09-20 02:20:51 +00003092
Rafael Espindolae076b532013-11-04 16:16:24 +00003093 if (error_code EC = ParseFunctionBody(F)) {
3094 if (ErrInfo)
3095 *ErrInfo = EC.message();
Chris Lattnerb348bb82007-05-18 04:02:46 +00003096 return true;
3097 }
Chandler Carruth69940402007-08-04 01:51:18 +00003098
3099 // Upgrade any old intrinsic calls in the function.
3100 for (UpgradedIntrinsicMap::iterator I = UpgradedIntrinsics.begin(),
3101 E = UpgradedIntrinsics.end(); I != E; ++I) {
3102 if (I->first != I->second) {
3103 for (Value::use_iterator UI = I->first->use_begin(),
3104 UE = I->first->use_end(); UI != UE; ) {
3105 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
3106 UpgradeIntrinsicCall(CI, I->second);
3107 }
3108 }
3109 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003110
Chris Lattnerb348bb82007-05-18 04:02:46 +00003111 return false;
3112}
3113
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003114bool BitcodeReader::isDematerializable(const GlobalValue *GV) const {
3115 const Function *F = dyn_cast<Function>(GV);
3116 if (!F || F->isDeclaration())
3117 return false;
3118 return DeferredFunctionInfo.count(const_cast<Function*>(F));
3119}
3120
3121void BitcodeReader::Dematerialize(GlobalValue *GV) {
3122 Function *F = dyn_cast<Function>(GV);
3123 // If this function isn't dematerializable, this is a noop.
3124 if (!F || !isDematerializable(F))
Chris Lattnerb348bb82007-05-18 04:02:46 +00003125 return;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003126
Chris Lattnerb348bb82007-05-18 04:02:46 +00003127 assert(DeferredFunctionInfo.count(F) && "No info to read function later?");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003128
Chris Lattnerb348bb82007-05-18 04:02:46 +00003129 // Just forget the function body, we can remat it later.
3130 F->deleteBody();
Chris Lattnerb348bb82007-05-18 04:02:46 +00003131}
3132
3133
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003134bool BitcodeReader::MaterializeModule(Module *M, std::string *ErrInfo) {
3135 assert(M == TheModule &&
3136 "Can only Materialize the Module this BitcodeReader is attached to.");
Chris Lattner714fa952009-06-16 05:15:21 +00003137 // Iterate over the module, deserializing any functions that are still on
3138 // disk.
3139 for (Module::iterator F = TheModule->begin(), E = TheModule->end();
3140 F != E; ++F)
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003141 if (F->isMaterializable() &&
3142 Materialize(F, ErrInfo))
3143 return true;
Chandler Carruth69940402007-08-04 01:51:18 +00003144
Derek Schuff0ffe6982012-02-29 00:07:09 +00003145 // At this point, if there are any function bodies, the current bit is
3146 // pointing to the END_BLOCK record after them. Now make sure the rest
3147 // of the bits in the module have been read.
3148 if (NextUnreadBit)
3149 ParseModule(true);
3150
Daniel Dunbara279bc32009-09-20 02:20:51 +00003151 // Upgrade any intrinsic calls that slipped through (should not happen!) and
3152 // delete the old functions to clean up. We can't do this unless the entire
3153 // module is materialized because there could always be another function body
Chandler Carruth69940402007-08-04 01:51:18 +00003154 // with calls to the old function.
3155 for (std::vector<std::pair<Function*, Function*> >::iterator I =
3156 UpgradedIntrinsics.begin(), E = UpgradedIntrinsics.end(); I != E; ++I) {
3157 if (I->first != I->second) {
3158 for (Value::use_iterator UI = I->first->use_begin(),
3159 UE = I->first->use_end(); UI != UE; ) {
3160 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
3161 UpgradeIntrinsicCall(CI, I->second);
3162 }
Chris Lattner7d9eb582009-04-01 01:43:03 +00003163 if (!I->first->use_empty())
3164 I->first->replaceAllUsesWith(I->second);
Chandler Carruth69940402007-08-04 01:51:18 +00003165 I->first->eraseFromParent();
3166 }
3167 }
3168 std::vector<std::pair<Function*, Function*> >().swap(UpgradedIntrinsics);
Devang Patele4b27562009-08-28 23:24:31 +00003169
Manman Ren804f0342013-09-28 00:22:27 +00003170 for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++)
3171 UpgradeInstWithTBAATag(InstsWithTBAATag[I]);
3172
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003173 return false;
Chris Lattnerb348bb82007-05-18 04:02:46 +00003174}
3175
Rafael Espindolae076b532013-11-04 16:16:24 +00003176error_code BitcodeReader::InitStream() {
3177 if (LazyStreamer)
3178 return InitLazyStream();
Derek Schuff2ea93872012-02-06 22:30:29 +00003179 return InitStreamFromBuffer();
3180}
3181
Rafael Espindolae076b532013-11-04 16:16:24 +00003182error_code BitcodeReader::InitStreamFromBuffer() {
Roman Divacky5177b3a2012-09-06 15:42:13 +00003183 const unsigned char *BufPtr = (const unsigned char*)Buffer->getBufferStart();
Derek Schuff2ea93872012-02-06 22:30:29 +00003184 const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
3185
3186 if (Buffer->getBufferSize() & 3) {
3187 if (!isRawBitcode(BufPtr, BufEnd) && !isBitcodeWrapper(BufPtr, BufEnd))
Rafael Espindolae076b532013-11-04 16:16:24 +00003188 return Error(InvalidBitcodeSignature);
Derek Schuff2ea93872012-02-06 22:30:29 +00003189 else
Rafael Espindolae076b532013-11-04 16:16:24 +00003190 return Error(BitcodeStreamInvalidSize);
Derek Schuff2ea93872012-02-06 22:30:29 +00003191 }
3192
3193 // If we have a wrapper header, parse it and ignore the non-bc file contents.
3194 // The magic number is 0x0B17C0DE stored in little endian.
3195 if (isBitcodeWrapper(BufPtr, BufEnd))
3196 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
Rafael Espindolae076b532013-11-04 16:16:24 +00003197 return Error(InvalidBitcodeWrapperHeader);
Derek Schuff2ea93872012-02-06 22:30:29 +00003198
3199 StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
3200 Stream.init(*StreamFile);
3201
Rafael Espindolae076b532013-11-04 16:16:24 +00003202 return error_code::success();
Derek Schuff2ea93872012-02-06 22:30:29 +00003203}
3204
Rafael Espindolae076b532013-11-04 16:16:24 +00003205error_code BitcodeReader::InitLazyStream() {
Derek Schuff2ea93872012-02-06 22:30:29 +00003206 // Check and strip off the bitcode wrapper; BitstreamReader expects never to
3207 // see it.
3208 StreamingMemoryObject *Bytes = new StreamingMemoryObject(LazyStreamer);
3209 StreamFile.reset(new BitstreamReader(Bytes));
3210 Stream.init(*StreamFile);
3211
3212 unsigned char buf[16];
Benjamin Kramer49a6a8d2013-05-24 10:54:58 +00003213 if (Bytes->readBytes(0, 16, buf) == -1)
Rafael Espindolae076b532013-11-04 16:16:24 +00003214 return Error(BitcodeStreamInvalidSize);
Derek Schuff2ea93872012-02-06 22:30:29 +00003215
3216 if (!isBitcode(buf, buf + 16))
Rafael Espindolae076b532013-11-04 16:16:24 +00003217 return Error(InvalidBitcodeSignature);
Derek Schuff2ea93872012-02-06 22:30:29 +00003218
3219 if (isBitcodeWrapper(buf, buf + 4)) {
3220 const unsigned char *bitcodeStart = buf;
3221 const unsigned char *bitcodeEnd = buf + 16;
3222 SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
3223 Bytes->dropLeadingBytes(bitcodeStart - buf);
3224 Bytes->setKnownObjectSize(bitcodeEnd - bitcodeStart);
3225 }
Rafael Espindolae076b532013-11-04 16:16:24 +00003226 return error_code::success();
3227}
3228
3229namespace {
3230class BitcodeErrorCategoryType : public _do_message {
3231 const char *name() const LLVM_OVERRIDE {
3232 return "llvm.bitcode";
3233 }
3234 std::string message(int IE) const LLVM_OVERRIDE {
3235 BitcodeReader::ErrorType E = static_cast<BitcodeReader::ErrorType>(IE);
3236 switch (E) {
3237 case BitcodeReader::BitcodeStreamInvalidSize:
3238 return "Bitcode stream length should be >= 16 bytes and a multiple of 4";
3239 case BitcodeReader::ConflictingMETADATA_KINDRecords:
3240 return "Conflicting METADATA_KIND records";
3241 case BitcodeReader::CouldNotFindFunctionInStream:
3242 return "Could not find function in stream";
3243 case BitcodeReader::ExpectedConstant:
3244 return "Expected a constant";
3245 case BitcodeReader::InsufficientFunctionProtos:
3246 return "Insufficient function protos";
3247 case BitcodeReader::InvalidBitcodeSignature:
3248 return "Invalid bitcode signature";
3249 case BitcodeReader::InvalidBitcodeWrapperHeader:
3250 return "Invalid bitcode wrapper header";
3251 case BitcodeReader::InvalidConstantReference:
3252 return "Invalid ronstant reference";
3253 case BitcodeReader::InvalidID:
3254 return "Invalid ID";
3255 case BitcodeReader::InvalidInstructionWithNoBB:
3256 return "Invalid instruction with no BB";
3257 case BitcodeReader::InvalidRecord:
3258 return "Invalid record";
3259 case BitcodeReader::InvalidTypeForValue:
3260 return "Invalid type for value";
3261 case BitcodeReader::InvalidTYPETable:
3262 return "Invalid TYPE table";
3263 case BitcodeReader::InvalidType:
3264 return "Invalid type";
3265 case BitcodeReader::MalformedBlock:
3266 return "Malformed block";
3267 case BitcodeReader::MalformedGlobalInitializerSet:
3268 return "Malformed global initializer set";
3269 case BitcodeReader::InvalidMultipleBlocks:
3270 return "Invalid multiple blocks";
3271 case BitcodeReader::NeverResolvedValueFoundInFunction:
3272 return "Never resolved value found in function";
3273 case BitcodeReader::InvalidValue:
3274 return "Invalid value";
3275 }
Benjamin Kramera83342b2013-11-05 13:45:09 +00003276 llvm_unreachable("Unknown error type!");
Rafael Espindolae076b532013-11-04 16:16:24 +00003277 }
3278};
3279}
3280
3281const error_category &BitcodeReader::BitcodeErrorCategory() {
3282 static BitcodeErrorCategoryType O;
3283 return O;
Derek Schuff2ea93872012-02-06 22:30:29 +00003284}
Chris Lattner48f84872007-05-01 04:59:48 +00003285
Chris Lattnerc453f762007-04-29 07:54:31 +00003286//===----------------------------------------------------------------------===//
3287// External interface
3288//===----------------------------------------------------------------------===//
3289
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003290/// getLazyBitcodeModule - lazy function-at-a-time loading from a file.
Chris Lattnerc453f762007-04-29 07:54:31 +00003291///
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003292Module *llvm::getLazyBitcodeModule(MemoryBuffer *Buffer,
3293 LLVMContext& Context,
3294 std::string *ErrMsg) {
3295 Module *M = new Module(Buffer->getBufferIdentifier(), Context);
Owen Anderson8b477ed2009-07-01 16:58:40 +00003296 BitcodeReader *R = new BitcodeReader(Buffer, Context);
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003297 M->setMaterializer(R);
Rafael Espindolae076b532013-11-04 16:16:24 +00003298 if (error_code EC = R->ParseBitcodeInto(M)) {
Chris Lattnerc453f762007-04-29 07:54:31 +00003299 if (ErrMsg)
Rafael Espindolae076b532013-11-04 16:16:24 +00003300 *ErrMsg = EC.message();
Daniel Dunbara279bc32009-09-20 02:20:51 +00003301
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003302 delete M; // Also deletes R.
Chris Lattnerc453f762007-04-29 07:54:31 +00003303 return 0;
3304 }
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003305 // Have the BitcodeReader dtor delete 'Buffer'.
3306 R->setBufferOwned(true);
Rafael Espindola47f79bb2012-01-02 07:49:53 +00003307
3308 R->materializeForwardReferencedFunctions();
3309
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003310 return M;
Chris Lattnerc453f762007-04-29 07:54:31 +00003311}
3312
Derek Schuff2ea93872012-02-06 22:30:29 +00003313
3314Module *llvm::getStreamedBitcodeModule(const std::string &name,
3315 DataStreamer *streamer,
3316 LLVMContext &Context,
3317 std::string *ErrMsg) {
3318 Module *M = new Module(name, Context);
3319 BitcodeReader *R = new BitcodeReader(streamer, Context);
3320 M->setMaterializer(R);
Rafael Espindolae076b532013-11-04 16:16:24 +00003321 if (error_code EC = R->ParseBitcodeInto(M)) {
Derek Schuff2ea93872012-02-06 22:30:29 +00003322 if (ErrMsg)
Rafael Espindolae076b532013-11-04 16:16:24 +00003323 *ErrMsg = EC.message();
Derek Schuff2ea93872012-02-06 22:30:29 +00003324 delete M; // Also deletes R.
3325 return 0;
3326 }
3327 R->setBufferOwned(false); // no buffer to delete
3328 return M;
3329}
3330
Chris Lattnerc453f762007-04-29 07:54:31 +00003331/// ParseBitcodeFile - Read the specified bitcode file, returning the module.
3332/// If an error occurs, return null and fill in *ErrMsg if non-null.
Daniel Dunbara279bc32009-09-20 02:20:51 +00003333Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
Owen Anderson8b477ed2009-07-01 16:58:40 +00003334 std::string *ErrMsg){
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003335 Module *M = getLazyBitcodeModule(Buffer, Context, ErrMsg);
3336 if (!M) return 0;
Chris Lattnerb348bb82007-05-18 04:02:46 +00003337
3338 // Don't let the BitcodeReader dtor delete 'Buffer', regardless of whether
3339 // there was an error.
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003340 static_cast<BitcodeReader*>(M->getMaterializer())->setBufferOwned(false);
Daniel Dunbara279bc32009-09-20 02:20:51 +00003341
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003342 // Read in the entire module, and destroy the BitcodeReader.
3343 if (M->MaterializeAllPermanently(ErrMsg)) {
3344 delete M;
Bill Wendling34711742010-10-06 01:22:42 +00003345 return 0;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00003346 }
Bill Wendling34711742010-10-06 01:22:42 +00003347
Chad Rosiercbbb0962011-12-07 21:44:12 +00003348 // TODO: Restore the use-lists to the in-memory state when the bitcode was
3349 // written. We must defer until the Module has been fully materialized.
3350
Chris Lattnerc453f762007-04-29 07:54:31 +00003351 return M;
3352}
Bill Wendling34711742010-10-06 01:22:42 +00003353
3354std::string llvm::getBitcodeTargetTriple(MemoryBuffer *Buffer,
3355 LLVMContext& Context,
3356 std::string *ErrMsg) {
3357 BitcodeReader *R = new BitcodeReader(Buffer, Context);
3358 // Don't let the BitcodeReader dtor delete 'Buffer'.
3359 R->setBufferOwned(false);
3360
3361 std::string Triple("");
Rafael Espindolae076b532013-11-04 16:16:24 +00003362 if (error_code EC = R->ParseTriple(Triple))
Bill Wendling34711742010-10-06 01:22:42 +00003363 if (ErrMsg)
Rafael Espindolae076b532013-11-04 16:16:24 +00003364 *ErrMsg = EC.message();
Bill Wendling34711742010-10-06 01:22:42 +00003365
3366 delete R;
3367 return Triple;
3368}