blob: 37515eb6f285457d4f64f6c1515ca45ad672853f [file] [log] [blame]
Chris Lattner1314b992007-04-22 06:23:29 +00001//===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner1314b992007-04-22 06:23:29 +00007//
8//===----------------------------------------------------------------------===//
Chris Lattner1314b992007-04-22 06:23:29 +00009
Chris Lattner6694f602007-04-29 07:54:31 +000010#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattner1314b992007-04-22 06:23:29 +000011#include "BitcodeReader.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000012#include "llvm/ADT/SmallString.h"
13#include "llvm/ADT/SmallVector.h"
14#include "llvm/AutoUpgrade.h"
Tobias Grosser0a8e12f2013-07-26 04:16:55 +000015#include "llvm/Bitcode/LLVMBitCodes.h"
Chandler Carruth9fb823b2013-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 Ren209b17c2013-09-28 00:22:27 +000020#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Module.h"
22#include "llvm/IR/OperandTraits.h"
23#include "llvm/IR/Operator.h"
Derek Schuff8b2dcad2012-02-06 22:30:29 +000024#include "llvm/Support/DataStream.h"
Chris Lattner08feb1e2007-04-24 04:04:35 +000025#include "llvm/Support/MathExtras.h"
Chris Lattner6694f602007-04-29 07:54:31 +000026#include "llvm/Support/MemoryBuffer.h"
Tobias Grosser0a8e12f2013-07-26 04:16:55 +000027#include "llvm/Support/raw_ostream.h"
Chris Lattner1314b992007-04-22 06:23:29 +000028using namespace llvm;
29
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +000030enum {
31 SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex
32};
33
Rafael Espindolab7993462012-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 Lattner9eeada92007-05-18 04:02:46 +000041void BitcodeReader::FreeState() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000042 if (BufferOwned)
43 delete Buffer;
Chris Lattner9eeada92007-05-18 04:02:46 +000044 Buffer = 0;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000045 std::vector<Type*>().swap(TypeList);
Chris Lattner9eeada92007-05-18 04:02:46 +000046 ValueList.clear();
Devang Patel05eb6172009-08-04 06:00:18 +000047 MDValueList.clear();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000048
Bill Wendlinge94d8432012-12-07 23:16:57 +000049 std::vector<AttributeSet>().swap(MAttributes);
Chris Lattner9eeada92007-05-18 04:02:46 +000050 std::vector<BasicBlock*>().swap(FunctionBBs);
51 std::vector<Function*>().swap(FunctionsWithBodies);
52 DeferredFunctionInfo.clear();
Dan Gohman43aa8f02010-07-20 21:42:28 +000053 MDKindMap.clear();
Benjamin Kramer736a4fc2012-09-21 14:34:31 +000054
55 assert(BlockAddrFwdRefs.empty() && "Unresolved blockaddress fwd references");
Chris Lattner6694f602007-04-29 07:54:31 +000056}
57
Chris Lattnerfee5a372007-05-04 03:30:17 +000058//===----------------------------------------------------------------------===//
59// Helper functions to implement forward reference resolution, etc.
60//===----------------------------------------------------------------------===//
Chris Lattner6694f602007-04-29 07:54:31 +000061
Chris Lattner1314b992007-04-22 06:23:29 +000062/// ConvertToString - Convert a string from a record into an std::string, return
63/// true on failure.
Chris Lattnerccaa4482007-04-23 21:26:05 +000064template<typename StrTy>
Benjamin Kramer9704ed02012-05-28 14:10:31 +000065static bool ConvertToString(ArrayRef<uint64_t> Record, unsigned Idx,
Chris Lattnerccaa4482007-04-23 21:26:05 +000066 StrTy &Result) {
Chris Lattnere14cb882007-05-04 19:11:41 +000067 if (Idx > Record.size())
Chris Lattner1314b992007-04-22 06:23:29 +000068 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000069
Chris Lattnere14cb882007-05-04 19:11:41 +000070 for (unsigned i = Idx, e = Record.size(); i != e; ++i)
71 Result += (char)Record[i];
Chris Lattner1314b992007-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 Wendlinga3c6f6b2009-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 Sands12da8ce2009-03-07 15:45:40 +000088 case 10: return GlobalValue::WeakODRLinkage;
89 case 11: return GlobalValue::LinkOnceODRLinkage;
Chris Lattner184f1be2009-04-13 05:44:34 +000090 case 12: return GlobalValue::AvailableExternallyLinkage;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +000091 case 13: return GlobalValue::LinkerPrivateLinkage;
Bill Wendling03bcd6e2010-07-01 21:55:59 +000092 case 14: return GlobalValue::LinkerPrivateWeakLinkage;
Chris Lattner1314b992007-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 Korobeynikov31fc4f92007-04-29 20:56:48 +0000101 case 2: return GlobalValue::ProtectedVisibility;
Chris Lattner1314b992007-04-22 06:23:29 +0000102 }
103}
104
Hans Wennborgcbe34b42012-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 Lattner1e16bcf72007-04-24 07:07:11 +0000116static int GetDecodedCastOpcode(unsigned Val) {
117 switch (Val) {
118 default: return -1;
119 case bitc::CAST_TRUNC : return Instruction::Trunc;
120 case bitc::CAST_ZEXT : return Instruction::ZExt;
121 case bitc::CAST_SEXT : return Instruction::SExt;
122 case bitc::CAST_FPTOUI : return Instruction::FPToUI;
123 case bitc::CAST_FPTOSI : return Instruction::FPToSI;
124 case bitc::CAST_UITOFP : return Instruction::UIToFP;
125 case bitc::CAST_SITOFP : return Instruction::SIToFP;
126 case bitc::CAST_FPTRUNC : return Instruction::FPTrunc;
127 case bitc::CAST_FPEXT : return Instruction::FPExt;
128 case bitc::CAST_PTRTOINT: return Instruction::PtrToInt;
129 case bitc::CAST_INTTOPTR: return Instruction::IntToPtr;
130 case bitc::CAST_BITCAST : return Instruction::BitCast;
Matt Arsenault3aa9b032013-11-18 02:51:33 +0000131 case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000132 }
133}
Chris Lattner229907c2011-07-18 04:54:35 +0000134static int GetDecodedBinaryOpcode(unsigned Val, Type *Ty) {
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000135 switch (Val) {
136 default: return -1;
Dan Gohmana5b96452009-06-04 22:49:04 +0000137 case bitc::BINOP_ADD:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000138 return Ty->isFPOrFPVectorTy() ? Instruction::FAdd : Instruction::Add;
Dan Gohmana5b96452009-06-04 22:49:04 +0000139 case bitc::BINOP_SUB:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000140 return Ty->isFPOrFPVectorTy() ? Instruction::FSub : Instruction::Sub;
Dan Gohmana5b96452009-06-04 22:49:04 +0000141 case bitc::BINOP_MUL:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000142 return Ty->isFPOrFPVectorTy() ? Instruction::FMul : Instruction::Mul;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000143 case bitc::BINOP_UDIV: return Instruction::UDiv;
144 case bitc::BINOP_SDIV:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000145 return Ty->isFPOrFPVectorTy() ? Instruction::FDiv : Instruction::SDiv;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000146 case bitc::BINOP_UREM: return Instruction::URem;
147 case bitc::BINOP_SREM:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000148 return Ty->isFPOrFPVectorTy() ? Instruction::FRem : Instruction::SRem;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000149 case bitc::BINOP_SHL: return Instruction::Shl;
150 case bitc::BINOP_LSHR: return Instruction::LShr;
151 case bitc::BINOP_ASHR: return Instruction::AShr;
152 case bitc::BINOP_AND: return Instruction::And;
153 case bitc::BINOP_OR: return Instruction::Or;
154 case bitc::BINOP_XOR: return Instruction::Xor;
155 }
156}
157
Eli Friedmanc9a551e2011-07-28 21:48:00 +0000158static AtomicRMWInst::BinOp GetDecodedRMWOperation(unsigned Val) {
159 switch (Val) {
160 default: return AtomicRMWInst::BAD_BINOP;
161 case bitc::RMW_XCHG: return AtomicRMWInst::Xchg;
162 case bitc::RMW_ADD: return AtomicRMWInst::Add;
163 case bitc::RMW_SUB: return AtomicRMWInst::Sub;
164 case bitc::RMW_AND: return AtomicRMWInst::And;
165 case bitc::RMW_NAND: return AtomicRMWInst::Nand;
166 case bitc::RMW_OR: return AtomicRMWInst::Or;
167 case bitc::RMW_XOR: return AtomicRMWInst::Xor;
168 case bitc::RMW_MAX: return AtomicRMWInst::Max;
169 case bitc::RMW_MIN: return AtomicRMWInst::Min;
170 case bitc::RMW_UMAX: return AtomicRMWInst::UMax;
171 case bitc::RMW_UMIN: return AtomicRMWInst::UMin;
172 }
173}
174
Eli Friedmanfee02c62011-07-25 23:16:38 +0000175static AtomicOrdering GetDecodedOrdering(unsigned Val) {
176 switch (Val) {
177 case bitc::ORDERING_NOTATOMIC: return NotAtomic;
178 case bitc::ORDERING_UNORDERED: return Unordered;
179 case bitc::ORDERING_MONOTONIC: return Monotonic;
180 case bitc::ORDERING_ACQUIRE: return Acquire;
181 case bitc::ORDERING_RELEASE: return Release;
182 case bitc::ORDERING_ACQREL: return AcquireRelease;
183 default: // Map unknown orderings to sequentially-consistent.
184 case bitc::ORDERING_SEQCST: return SequentiallyConsistent;
185 }
186}
187
188static SynchronizationScope GetDecodedSynchScope(unsigned Val) {
189 switch (Val) {
190 case bitc::SYNCHSCOPE_SINGLETHREAD: return SingleThread;
191 default: // Map unknown scopes to cross-thread.
192 case bitc::SYNCHSCOPE_CROSSTHREAD: return CrossThread;
193 }
194}
195
Gabor Greiff6caff662008-05-10 08:32:32 +0000196namespace llvm {
Chris Lattner1663cca2007-04-24 05:48:56 +0000197namespace {
198 /// @brief A class for maintaining the slot number definition
199 /// as a placeholder for the actual definition for forward constants defs.
200 class ConstantPlaceHolder : public ConstantExpr {
Craig Toppera60c0f12012-09-15 17:09:36 +0000201 void operator=(const ConstantPlaceHolder &) LLVM_DELETED_FUNCTION;
Gabor Greife9ecc682008-04-06 20:25:17 +0000202 public:
203 // allocate space for exactly one operand
204 void *operator new(size_t s) {
205 return User::operator new(s, 1);
206 }
Chris Lattner229907c2011-07-18 04:54:35 +0000207 explicit ConstantPlaceHolder(Type *Ty, LLVMContext& Context)
Gabor Greiff6caff662008-05-10 08:32:32 +0000208 : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000209 Op<0>() = UndefValue::get(Type::getInt32Ty(Context));
Chris Lattner1663cca2007-04-24 05:48:56 +0000210 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000211
Chris Lattner74429932008-08-21 02:34:16 +0000212 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
Chris Lattner74429932008-08-21 02:34:16 +0000213 static bool classof(const Value *V) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000214 return isa<ConstantExpr>(V) &&
Chris Lattner74429932008-08-21 02:34:16 +0000215 cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1;
216 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000217
218
Gabor Greiff6caff662008-05-10 08:32:32 +0000219 /// Provide fast operand accessors
Chris Lattner2d8cd802009-03-31 22:55:09 +0000220 //DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Chris Lattner1663cca2007-04-24 05:48:56 +0000221 };
222}
223
Chris Lattner2d8cd802009-03-31 22:55:09 +0000224// FIXME: can we inherit this from ConstantExpr?
Gabor Greiff6caff662008-05-10 08:32:32 +0000225template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000226struct OperandTraits<ConstantPlaceHolder> :
227 public FixedNumOperandTraits<ConstantPlaceHolder, 1> {
Gabor Greiff6caff662008-05-10 08:32:32 +0000228};
Gabor Greiff6caff662008-05-10 08:32:32 +0000229}
230
Chris Lattner2d8cd802009-03-31 22:55:09 +0000231
232void BitcodeReaderValueList::AssignValue(Value *V, unsigned Idx) {
233 if (Idx == size()) {
234 push_back(V);
235 return;
236 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000237
Chris Lattner2d8cd802009-03-31 22:55:09 +0000238 if (Idx >= size())
239 resize(Idx+1);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000240
Chris Lattner2d8cd802009-03-31 22:55:09 +0000241 WeakVH &OldV = ValuePtrs[Idx];
242 if (OldV == 0) {
243 OldV = V;
244 return;
245 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000246
Chris Lattner2d8cd802009-03-31 22:55:09 +0000247 // Handle constants and non-constants (e.g. instrs) differently for
248 // efficiency.
249 if (Constant *PHC = dyn_cast<Constant>(&*OldV)) {
250 ResolveConstants.push_back(std::make_pair(PHC, Idx));
251 OldV = V;
252 } else {
253 // If there was a forward reference to this value, replace it.
254 Value *PrevVal = OldV;
255 OldV->replaceAllUsesWith(V);
256 delete PrevVal;
Gabor Greiff6caff662008-05-10 08:32:32 +0000257 }
258}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000259
Gabor Greiff6caff662008-05-10 08:32:32 +0000260
Chris Lattner1663cca2007-04-24 05:48:56 +0000261Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx,
Chris Lattner229907c2011-07-18 04:54:35 +0000262 Type *Ty) {
Chris Lattner2d8cd802009-03-31 22:55:09 +0000263 if (Idx >= size())
Gabor Greiff6caff662008-05-10 08:32:32 +0000264 resize(Idx + 1);
Chris Lattner1663cca2007-04-24 05:48:56 +0000265
Chris Lattner2d8cd802009-03-31 22:55:09 +0000266 if (Value *V = ValuePtrs[Idx]) {
Chris Lattner83930552007-05-01 07:01:57 +0000267 assert(Ty == V->getType() && "Type mismatch in constant table!");
268 return cast<Constant>(V);
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000269 }
Chris Lattner1663cca2007-04-24 05:48:56 +0000270
271 // Create and return a placeholder, which will later be RAUW'd.
Owen Andersone9f98042009-07-07 20:18:58 +0000272 Constant *C = new ConstantPlaceHolder(Ty, Context);
Chris Lattner2d8cd802009-03-31 22:55:09 +0000273 ValuePtrs[Idx] = C;
Chris Lattner1663cca2007-04-24 05:48:56 +0000274 return C;
275}
276
Chris Lattner229907c2011-07-18 04:54:35 +0000277Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, Type *Ty) {
Chris Lattner2d8cd802009-03-31 22:55:09 +0000278 if (Idx >= size())
Gabor Greiff6caff662008-05-10 08:32:32 +0000279 resize(Idx + 1);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000280
Chris Lattner2d8cd802009-03-31 22:55:09 +0000281 if (Value *V = ValuePtrs[Idx]) {
Chris Lattner83930552007-05-01 07:01:57 +0000282 assert((Ty == 0 || Ty == V->getType()) && "Type mismatch in value table!");
283 return V;
284 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000285
Chris Lattner1fc27f02007-05-02 05:16:49 +0000286 // No type specified, must be invalid reference.
287 if (Ty == 0) return 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000288
Chris Lattner83930552007-05-01 07:01:57 +0000289 // Create and return a placeholder, which will later be RAUW'd.
290 Value *V = new Argument(Ty);
Chris Lattner2d8cd802009-03-31 22:55:09 +0000291 ValuePtrs[Idx] = V;
Chris Lattner83930552007-05-01 07:01:57 +0000292 return V;
293}
294
Chris Lattner74429932008-08-21 02:34:16 +0000295/// ResolveConstantForwardRefs - Once all constants are read, this method bulk
296/// resolves any forward references. The idea behind this is that we sometimes
297/// get constants (such as large arrays) which reference *many* forward ref
298/// constants. Replacing each of these causes a lot of thrashing when
299/// building/reuniquing the constant. Instead of doing this, we look at all the
300/// uses and rewrite all the place holders at once for any constant that uses
301/// a placeholder.
302void BitcodeReaderValueList::ResolveConstantForwardRefs() {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000303 // Sort the values by-pointer so that they are efficient to look up with a
Chris Lattner74429932008-08-21 02:34:16 +0000304 // binary search.
305 std::sort(ResolveConstants.begin(), ResolveConstants.end());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000306
Chris Lattner74429932008-08-21 02:34:16 +0000307 SmallVector<Constant*, 64> NewOps;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000308
Chris Lattner74429932008-08-21 02:34:16 +0000309 while (!ResolveConstants.empty()) {
Chris Lattner2d8cd802009-03-31 22:55:09 +0000310 Value *RealVal = operator[](ResolveConstants.back().second);
Chris Lattner74429932008-08-21 02:34:16 +0000311 Constant *Placeholder = ResolveConstants.back().first;
312 ResolveConstants.pop_back();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000313
Chris Lattner74429932008-08-21 02:34:16 +0000314 // Loop over all users of the placeholder, updating them to reference the
315 // new value. If they reference more than one placeholder, update them all
316 // at once.
317 while (!Placeholder->use_empty()) {
Chris Lattner479c5d92008-08-21 17:31:45 +0000318 Value::use_iterator UI = Placeholder->use_begin();
Gabor Greif2c0ab482010-07-09 16:01:21 +0000319 User *U = *UI;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000320
Chris Lattner74429932008-08-21 02:34:16 +0000321 // If the using object isn't uniqued, just update the operands. This
322 // handles instructions and initializers for global variables.
Gabor Greif2c0ab482010-07-09 16:01:21 +0000323 if (!isa<Constant>(U) || isa<GlobalValue>(U)) {
Chris Lattner479c5d92008-08-21 17:31:45 +0000324 UI.getUse().set(RealVal);
Chris Lattner74429932008-08-21 02:34:16 +0000325 continue;
326 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000327
Chris Lattner74429932008-08-21 02:34:16 +0000328 // Otherwise, we have a constant that uses the placeholder. Replace that
329 // constant with a new constant that has *all* placeholder uses updated.
Gabor Greif2c0ab482010-07-09 16:01:21 +0000330 Constant *UserC = cast<Constant>(U);
Chris Lattner74429932008-08-21 02:34:16 +0000331 for (User::op_iterator I = UserC->op_begin(), E = UserC->op_end();
332 I != E; ++I) {
333 Value *NewOp;
334 if (!isa<ConstantPlaceHolder>(*I)) {
335 // Not a placeholder reference.
336 NewOp = *I;
337 } else if (*I == Placeholder) {
338 // Common case is that it just references this one placeholder.
339 NewOp = RealVal;
340 } else {
341 // Otherwise, look up the placeholder in ResolveConstants.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000342 ResolveConstantsTy::iterator It =
343 std::lower_bound(ResolveConstants.begin(), ResolveConstants.end(),
Chris Lattner74429932008-08-21 02:34:16 +0000344 std::pair<Constant*, unsigned>(cast<Constant>(*I),
345 0));
346 assert(It != ResolveConstants.end() && It->first == *I);
Chris Lattner2d8cd802009-03-31 22:55:09 +0000347 NewOp = operator[](It->second);
Chris Lattner74429932008-08-21 02:34:16 +0000348 }
349
350 NewOps.push_back(cast<Constant>(NewOp));
351 }
352
353 // Make the new constant.
354 Constant *NewC;
355 if (ConstantArray *UserCA = dyn_cast<ConstantArray>(UserC)) {
Jay Foad83be3612011-06-22 09:24:39 +0000356 NewC = ConstantArray::get(UserCA->getType(), NewOps);
Chris Lattner74429932008-08-21 02:34:16 +0000357 } else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) {
Chris Lattnercc19efa2011-06-20 04:01:31 +0000358 NewC = ConstantStruct::get(UserCS->getType(), NewOps);
Chris Lattner74429932008-08-21 02:34:16 +0000359 } else if (isa<ConstantVector>(UserC)) {
Chris Lattner69229312011-02-15 00:14:00 +0000360 NewC = ConstantVector::get(NewOps);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000361 } else {
362 assert(isa<ConstantExpr>(UserC) && "Must be a ConstantExpr.");
Jay Foad5c984e562011-04-13 13:46:01 +0000363 NewC = cast<ConstantExpr>(UserC)->getWithOperands(NewOps);
Chris Lattner74429932008-08-21 02:34:16 +0000364 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000365
Chris Lattner74429932008-08-21 02:34:16 +0000366 UserC->replaceAllUsesWith(NewC);
367 UserC->destroyConstant();
368 NewOps.clear();
369 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000370
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000371 // Update all ValueHandles, they should be the only users at this point.
372 Placeholder->replaceAllUsesWith(RealVal);
Chris Lattner74429932008-08-21 02:34:16 +0000373 delete Placeholder;
374 }
375}
376
Devang Patel05eb6172009-08-04 06:00:18 +0000377void BitcodeReaderMDValueList::AssignValue(Value *V, unsigned Idx) {
378 if (Idx == size()) {
379 push_back(V);
380 return;
381 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000382
Devang Patel05eb6172009-08-04 06:00:18 +0000383 if (Idx >= size())
384 resize(Idx+1);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000385
Devang Patel05eb6172009-08-04 06:00:18 +0000386 WeakVH &OldV = MDValuePtrs[Idx];
387 if (OldV == 0) {
388 OldV = V;
389 return;
390 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000391
Devang Patel05eb6172009-08-04 06:00:18 +0000392 // If there was a forward reference to this value, replace it.
Dan Gohman16a5d982010-08-20 22:02:26 +0000393 MDNode *PrevVal = cast<MDNode>(OldV);
Devang Patel05eb6172009-08-04 06:00:18 +0000394 OldV->replaceAllUsesWith(V);
Dan Gohman16a5d982010-08-20 22:02:26 +0000395 MDNode::deleteTemporary(PrevVal);
Devang Patel116b4a02009-09-03 01:38:02 +0000396 // Deleting PrevVal sets Idx value in MDValuePtrs to null. Set new
397 // value for Idx.
398 MDValuePtrs[Idx] = V;
Devang Patel05eb6172009-08-04 06:00:18 +0000399}
400
401Value *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) {
402 if (Idx >= size())
403 resize(Idx + 1);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000404
Devang Patel05eb6172009-08-04 06:00:18 +0000405 if (Value *V = MDValuePtrs[Idx]) {
Chris Lattnerfdd87902009-10-05 05:54:46 +0000406 assert(V->getType()->isMetadataTy() && "Type mismatch in value table!");
Devang Patel05eb6172009-08-04 06:00:18 +0000407 return V;
408 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000409
Devang Patel05eb6172009-08-04 06:00:18 +0000410 // Create and return a placeholder, which will later be RAUW'd.
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000411 Value *V = MDNode::getTemporary(Context, None);
Devang Patel05eb6172009-08-04 06:00:18 +0000412 MDValuePtrs[Idx] = V;
413 return V;
414}
Chris Lattner1314b992007-04-22 06:23:29 +0000415
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000416Type *BitcodeReader::getTypeByID(unsigned ID) {
417 // The type table size is always specified correctly.
418 if (ID >= TypeList.size())
419 return 0;
Derek Schuff206dddd2012-02-06 19:03:04 +0000420
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000421 if (Type *Ty = TypeList[ID])
422 return Ty;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000423
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000424 // If we have a forward reference, the only possible case is when it is to a
425 // named struct. Just create a placeholder for now.
Chris Lattner335d3992011-08-12 18:06:37 +0000426 return TypeList[ID] = StructType::create(Context);
Chris Lattner1314b992007-04-22 06:23:29 +0000427}
428
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000429
Chris Lattnerfee5a372007-05-04 03:30:17 +0000430//===----------------------------------------------------------------------===//
431// Functions for parsing blocks from the bitcode file
432//===----------------------------------------------------------------------===//
433
Bill Wendling56aeccc2013-02-04 23:32:23 +0000434
435/// \brief This fills an AttrBuilder object with the LLVM attributes that have
436/// been decoded from the given integer. This function must stay in sync with
437/// 'encodeLLVMAttributesForBitcode'.
438static void decodeLLVMAttributesForBitcode(AttrBuilder &B,
439 uint64_t EncodedAttrs) {
440 // FIXME: Remove in 4.0.
441
442 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
443 // the bits above 31 down by 11 bits.
444 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
445 assert((!Alignment || isPowerOf2_32(Alignment)) &&
446 "Alignment must be a power of two.");
447
448 if (Alignment)
449 B.addAlignmentAttr(Alignment);
Kostya Serebryanyd688bab2013-02-11 08:13:54 +0000450 B.addRawValue(((EncodedAttrs & (0xfffffULL << 32)) >> 11) |
Bill Wendling56aeccc2013-02-04 23:32:23 +0000451 (EncodedAttrs & 0xffff));
452}
453
Rafael Espindola48da4f42013-11-04 16:16:24 +0000454error_code BitcodeReader::ParseAttributeBlock() {
Chris Lattner982ec1e2007-05-05 00:17:00 +0000455 if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +0000456 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000457
Devang Patela05633e2008-09-26 22:53:05 +0000458 if (!MAttributes.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +0000459 return Error(InvalidMultipleBlocks);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000460
Chris Lattnerfee5a372007-05-04 03:30:17 +0000461 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000462
Bill Wendling71173cb2013-01-27 00:36:48 +0000463 SmallVector<AttributeSet, 8> Attrs;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000464
Chris Lattnerfee5a372007-05-04 03:30:17 +0000465 // Read all the records.
466 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +0000467 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +0000468
Chris Lattner27d38752013-01-20 02:13:19 +0000469 switch (Entry.Kind) {
470 case BitstreamEntry::SubBlock: // Handled for us already.
471 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +0000472 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +0000473 case BitstreamEntry::EndBlock:
Rafael Espindola48da4f42013-11-04 16:16:24 +0000474 return error_code::success();
Chris Lattner27d38752013-01-20 02:13:19 +0000475 case BitstreamEntry::Record:
476 // The interesting case.
477 break;
Chris Lattnerfee5a372007-05-04 03:30:17 +0000478 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000479
Chris Lattnerfee5a372007-05-04 03:30:17 +0000480 // Read a record.
481 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +0000482 switch (Stream.readRecord(Entry.ID, Record)) {
Chris Lattnerfee5a372007-05-04 03:30:17 +0000483 default: // Default behavior: ignore.
484 break;
Bill Wendling56aeccc2013-02-04 23:32:23 +0000485 case bitc::PARAMATTR_CODE_ENTRY_OLD: { // ENTRY: [paramidx0, attr0, ...]
486 // FIXME: Remove in 4.0.
Chris Lattnerfee5a372007-05-04 03:30:17 +0000487 if (Record.size() & 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000488 return Error(InvalidRecord);
Chris Lattnerfee5a372007-05-04 03:30:17 +0000489
Chris Lattnerfee5a372007-05-04 03:30:17 +0000490 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
Bill Wendling60011b82013-01-29 01:43:29 +0000491 AttrBuilder B;
Bill Wendling56aeccc2013-02-04 23:32:23 +0000492 decodeLLVMAttributesForBitcode(B, Record[i+1]);
Bill Wendling60011b82013-01-29 01:43:29 +0000493 Attrs.push_back(AttributeSet::get(Context, Record[i], B));
Devang Patela05633e2008-09-26 22:53:05 +0000494 }
Devang Patela05633e2008-09-26 22:53:05 +0000495
Bill Wendlinge94d8432012-12-07 23:16:57 +0000496 MAttributes.push_back(AttributeSet::get(Context, Attrs));
Chris Lattnerfee5a372007-05-04 03:30:17 +0000497 Attrs.clear();
498 break;
499 }
Bill Wendling0dc08912013-02-12 08:13:50 +0000500 case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [attrgrp0, attrgrp1, ...]
501 for (unsigned i = 0, e = Record.size(); i != e; ++i)
502 Attrs.push_back(MAttributeGroups[Record[i]]);
503
504 MAttributes.push_back(AttributeSet::get(Context, Attrs));
505 Attrs.clear();
506 break;
507 }
Duncan Sands04eb67e2007-11-20 14:09:29 +0000508 }
Chris Lattnerfee5a372007-05-04 03:30:17 +0000509 }
510}
511
Reid Klecknere9f36af2013-11-12 01:31:00 +0000512// Returns Attribute::None on unrecognized codes.
513static Attribute::AttrKind GetAttrFromCode(uint64_t Code) {
514 switch (Code) {
515 default:
516 return Attribute::None;
517 case bitc::ATTR_KIND_ALIGNMENT:
518 return Attribute::Alignment;
519 case bitc::ATTR_KIND_ALWAYS_INLINE:
520 return Attribute::AlwaysInline;
521 case bitc::ATTR_KIND_BUILTIN:
522 return Attribute::Builtin;
523 case bitc::ATTR_KIND_BY_VAL:
524 return Attribute::ByVal;
Reid Klecknera534a382013-12-19 02:14:12 +0000525 case bitc::ATTR_KIND_IN_ALLOCA:
526 return Attribute::InAlloca;
Reid Klecknere9f36af2013-11-12 01:31:00 +0000527 case bitc::ATTR_KIND_COLD:
528 return Attribute::Cold;
529 case bitc::ATTR_KIND_INLINE_HINT:
530 return Attribute::InlineHint;
531 case bitc::ATTR_KIND_IN_REG:
532 return Attribute::InReg;
533 case bitc::ATTR_KIND_MIN_SIZE:
534 return Attribute::MinSize;
535 case bitc::ATTR_KIND_NAKED:
536 return Attribute::Naked;
537 case bitc::ATTR_KIND_NEST:
538 return Attribute::Nest;
539 case bitc::ATTR_KIND_NO_ALIAS:
540 return Attribute::NoAlias;
541 case bitc::ATTR_KIND_NO_BUILTIN:
542 return Attribute::NoBuiltin;
543 case bitc::ATTR_KIND_NO_CAPTURE:
544 return Attribute::NoCapture;
545 case bitc::ATTR_KIND_NO_DUPLICATE:
546 return Attribute::NoDuplicate;
547 case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT:
548 return Attribute::NoImplicitFloat;
549 case bitc::ATTR_KIND_NO_INLINE:
550 return Attribute::NoInline;
551 case bitc::ATTR_KIND_NON_LAZY_BIND:
552 return Attribute::NonLazyBind;
553 case bitc::ATTR_KIND_NO_RED_ZONE:
554 return Attribute::NoRedZone;
555 case bitc::ATTR_KIND_NO_RETURN:
556 return Attribute::NoReturn;
557 case bitc::ATTR_KIND_NO_UNWIND:
558 return Attribute::NoUnwind;
559 case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE:
560 return Attribute::OptimizeForSize;
561 case bitc::ATTR_KIND_OPTIMIZE_NONE:
562 return Attribute::OptimizeNone;
563 case bitc::ATTR_KIND_READ_NONE:
564 return Attribute::ReadNone;
565 case bitc::ATTR_KIND_READ_ONLY:
566 return Attribute::ReadOnly;
567 case bitc::ATTR_KIND_RETURNED:
568 return Attribute::Returned;
569 case bitc::ATTR_KIND_RETURNS_TWICE:
570 return Attribute::ReturnsTwice;
571 case bitc::ATTR_KIND_S_EXT:
572 return Attribute::SExt;
573 case bitc::ATTR_KIND_STACK_ALIGNMENT:
574 return Attribute::StackAlignment;
575 case bitc::ATTR_KIND_STACK_PROTECT:
576 return Attribute::StackProtect;
577 case bitc::ATTR_KIND_STACK_PROTECT_REQ:
578 return Attribute::StackProtectReq;
579 case bitc::ATTR_KIND_STACK_PROTECT_STRONG:
580 return Attribute::StackProtectStrong;
581 case bitc::ATTR_KIND_STRUCT_RET:
582 return Attribute::StructRet;
583 case bitc::ATTR_KIND_SANITIZE_ADDRESS:
584 return Attribute::SanitizeAddress;
585 case bitc::ATTR_KIND_SANITIZE_THREAD:
586 return Attribute::SanitizeThread;
587 case bitc::ATTR_KIND_SANITIZE_MEMORY:
588 return Attribute::SanitizeMemory;
589 case bitc::ATTR_KIND_UW_TABLE:
590 return Attribute::UWTable;
591 case bitc::ATTR_KIND_Z_EXT:
592 return Attribute::ZExt;
593 }
594}
595
Rafael Espindola48da4f42013-11-04 16:16:24 +0000596error_code BitcodeReader::ParseAttrKind(uint64_t Code,
597 Attribute::AttrKind *Kind) {
Reid Klecknere9f36af2013-11-12 01:31:00 +0000598 *Kind = GetAttrFromCode(Code);
599 if (*Kind == Attribute::None)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000600 return Error(InvalidValue);
Reid Klecknere9f36af2013-11-12 01:31:00 +0000601 return error_code::success();
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000602}
603
Rafael Espindola48da4f42013-11-04 16:16:24 +0000604error_code BitcodeReader::ParseAttributeGroupBlock() {
Bill Wendlingba629332013-02-10 23:24:25 +0000605 if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +0000606 return Error(InvalidRecord);
Bill Wendlingba629332013-02-10 23:24:25 +0000607
608 if (!MAttributeGroups.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +0000609 return Error(InvalidMultipleBlocks);
Bill Wendlingba629332013-02-10 23:24:25 +0000610
611 SmallVector<uint64_t, 64> Record;
612
613 // Read all the records.
614 while (1) {
615 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
616
617 switch (Entry.Kind) {
618 case BitstreamEntry::SubBlock: // Handled for us already.
619 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +0000620 return Error(MalformedBlock);
Bill Wendlingba629332013-02-10 23:24:25 +0000621 case BitstreamEntry::EndBlock:
Rafael Espindola48da4f42013-11-04 16:16:24 +0000622 return error_code::success();
Bill Wendlingba629332013-02-10 23:24:25 +0000623 case BitstreamEntry::Record:
624 // The interesting case.
625 break;
626 }
627
628 // Read a record.
629 Record.clear();
630 switch (Stream.readRecord(Entry.ID, Record)) {
631 default: // Default behavior: ignore.
632 break;
633 case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...]
634 if (Record.size() < 3)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000635 return Error(InvalidRecord);
Bill Wendlingba629332013-02-10 23:24:25 +0000636
Bill Wendlinge46707e2013-02-11 22:32:29 +0000637 uint64_t GrpID = Record[0];
Bill Wendlingba629332013-02-10 23:24:25 +0000638 uint64_t Idx = Record[1]; // Index of the object this attribute refers to.
639
640 AttrBuilder B;
641 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
642 if (Record[i] == 0) { // Enum attribute
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000643 Attribute::AttrKind Kind;
Rafael Espindola48da4f42013-11-04 16:16:24 +0000644 if (error_code EC = ParseAttrKind(Record[++i], &Kind))
645 return EC;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000646
647 B.addAttribute(Kind);
Bill Wendlingba629332013-02-10 23:24:25 +0000648 } else if (Record[i] == 1) { // Align attribute
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000649 Attribute::AttrKind Kind;
Rafael Espindola48da4f42013-11-04 16:16:24 +0000650 if (error_code EC = ParseAttrKind(Record[++i], &Kind))
651 return EC;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000652 if (Kind == Attribute::Alignment)
Bill Wendlingba629332013-02-10 23:24:25 +0000653 B.addAlignmentAttr(Record[++i]);
654 else
655 B.addStackAlignmentAttr(Record[++i]);
656 } else { // String attribute
Bill Wendlinge46707e2013-02-11 22:32:29 +0000657 assert((Record[i] == 3 || Record[i] == 4) &&
658 "Invalid attribute group entry");
Bill Wendlingba629332013-02-10 23:24:25 +0000659 bool HasValue = (Record[i++] == 4);
660 SmallString<64> KindStr;
661 SmallString<64> ValStr;
662
663 while (Record[i] != 0 && i != e)
664 KindStr += Record[i++];
Bill Wendlinge46707e2013-02-11 22:32:29 +0000665 assert(Record[i] == 0 && "Kind string not null terminated");
Bill Wendlingba629332013-02-10 23:24:25 +0000666
667 if (HasValue) {
668 // Has a value associated with it.
Bill Wendlinge46707e2013-02-11 22:32:29 +0000669 ++i; // Skip the '0' that terminates the "kind" string.
Bill Wendlingba629332013-02-10 23:24:25 +0000670 while (Record[i] != 0 && i != e)
671 ValStr += Record[i++];
Bill Wendlinge46707e2013-02-11 22:32:29 +0000672 assert(Record[i] == 0 && "Value string not null terminated");
Bill Wendlingba629332013-02-10 23:24:25 +0000673 }
674
675 B.addAttribute(KindStr.str(), ValStr.str());
676 }
677 }
678
Bill Wendlinge46707e2013-02-11 22:32:29 +0000679 MAttributeGroups[GrpID] = AttributeSet::get(Context, Idx, B);
Bill Wendlingba629332013-02-10 23:24:25 +0000680 break;
681 }
682 }
683 }
684}
685
Rafael Espindola48da4f42013-11-04 16:16:24 +0000686error_code BitcodeReader::ParseTypeTable() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000687 if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW))
Rafael Espindola48da4f42013-11-04 16:16:24 +0000688 return Error(InvalidRecord);
Derek Schuff206dddd2012-02-06 19:03:04 +0000689
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000690 return ParseTypeTableBody();
691}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000692
Rafael Espindola48da4f42013-11-04 16:16:24 +0000693error_code BitcodeReader::ParseTypeTableBody() {
Chris Lattner1314b992007-04-22 06:23:29 +0000694 if (!TypeList.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +0000695 return Error(InvalidMultipleBlocks);
Chris Lattner1314b992007-04-22 06:23:29 +0000696
697 SmallVector<uint64_t, 64> Record;
698 unsigned NumRecords = 0;
699
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000700 SmallString<64> TypeName;
Derek Schuff206dddd2012-02-06 19:03:04 +0000701
Chris Lattner1314b992007-04-22 06:23:29 +0000702 // Read all the records for this type table.
703 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +0000704 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +0000705
Chris Lattner27d38752013-01-20 02:13:19 +0000706 switch (Entry.Kind) {
707 case BitstreamEntry::SubBlock: // Handled for us already.
708 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +0000709 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +0000710 case BitstreamEntry::EndBlock:
Chris Lattner1314b992007-04-22 06:23:29 +0000711 if (NumRecords != TypeList.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +0000712 return Error(MalformedBlock);
713 return error_code::success();
Chris Lattner27d38752013-01-20 02:13:19 +0000714 case BitstreamEntry::Record:
715 // The interesting case.
716 break;
Chris Lattner1314b992007-04-22 06:23:29 +0000717 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000718
Chris Lattner1314b992007-04-22 06:23:29 +0000719 // Read a record.
720 Record.clear();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000721 Type *ResultTy = 0;
Chris Lattner27d38752013-01-20 02:13:19 +0000722 switch (Stream.readRecord(Entry.ID, Record)) {
Rafael Espindola48da4f42013-11-04 16:16:24 +0000723 default:
724 return Error(InvalidValue);
Chris Lattner1314b992007-04-22 06:23:29 +0000725 case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
726 // TYPE_CODE_NUMENTRY contains a count of the number of types in the
727 // type list. This allows us to reserve space.
728 if (Record.size() < 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000729 return Error(InvalidRecord);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000730 TypeList.resize(Record[0]);
Chris Lattner1314b992007-04-22 06:23:29 +0000731 continue;
Chris Lattner1314b992007-04-22 06:23:29 +0000732 case bitc::TYPE_CODE_VOID: // VOID
Owen Anderson55f1c092009-08-13 21:58:54 +0000733 ResultTy = Type::getVoidTy(Context);
Chris Lattner1314b992007-04-22 06:23:29 +0000734 break;
Dan Gohman518cda42011-12-17 00:04:22 +0000735 case bitc::TYPE_CODE_HALF: // HALF
736 ResultTy = Type::getHalfTy(Context);
737 break;
Chris Lattner1314b992007-04-22 06:23:29 +0000738 case bitc::TYPE_CODE_FLOAT: // FLOAT
Owen Anderson55f1c092009-08-13 21:58:54 +0000739 ResultTy = Type::getFloatTy(Context);
Chris Lattner1314b992007-04-22 06:23:29 +0000740 break;
741 case bitc::TYPE_CODE_DOUBLE: // DOUBLE
Owen Anderson55f1c092009-08-13 21:58:54 +0000742 ResultTy = Type::getDoubleTy(Context);
Chris Lattner1314b992007-04-22 06:23:29 +0000743 break;
Dale Johannesenff4c3be2007-08-03 01:03:46 +0000744 case bitc::TYPE_CODE_X86_FP80: // X86_FP80
Owen Anderson55f1c092009-08-13 21:58:54 +0000745 ResultTy = Type::getX86_FP80Ty(Context);
Dale Johannesenff4c3be2007-08-03 01:03:46 +0000746 break;
747 case bitc::TYPE_CODE_FP128: // FP128
Owen Anderson55f1c092009-08-13 21:58:54 +0000748 ResultTy = Type::getFP128Ty(Context);
Dale Johannesenff4c3be2007-08-03 01:03:46 +0000749 break;
750 case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128
Owen Anderson55f1c092009-08-13 21:58:54 +0000751 ResultTy = Type::getPPC_FP128Ty(Context);
Dale Johannesenff4c3be2007-08-03 01:03:46 +0000752 break;
Chris Lattner1314b992007-04-22 06:23:29 +0000753 case bitc::TYPE_CODE_LABEL: // LABEL
Owen Anderson55f1c092009-08-13 21:58:54 +0000754 ResultTy = Type::getLabelTy(Context);
Chris Lattner1314b992007-04-22 06:23:29 +0000755 break;
Nick Lewyckyadbc2842009-05-30 05:06:04 +0000756 case bitc::TYPE_CODE_METADATA: // METADATA
Owen Anderson55f1c092009-08-13 21:58:54 +0000757 ResultTy = Type::getMetadataTy(Context);
Nick Lewyckyadbc2842009-05-30 05:06:04 +0000758 break;
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000759 case bitc::TYPE_CODE_X86_MMX: // X86_MMX
760 ResultTy = Type::getX86_MMXTy(Context);
761 break;
Chris Lattner1314b992007-04-22 06:23:29 +0000762 case bitc::TYPE_CODE_INTEGER: // INTEGER: [width]
763 if (Record.size() < 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000764 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000765
Owen Anderson55f1c092009-08-13 21:58:54 +0000766 ResultTy = IntegerType::get(Context, Record[0]);
Chris Lattner1314b992007-04-22 06:23:29 +0000767 break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000768 case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000769 // [pointee type, address space]
Chris Lattner1314b992007-04-22 06:23:29 +0000770 if (Record.size() < 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000771 return Error(InvalidRecord);
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000772 unsigned AddressSpace = 0;
773 if (Record.size() == 2)
774 AddressSpace = Record[1];
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000775 ResultTy = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +0000776 if (ResultTy == 0)
777 return Error(InvalidType);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000778 ResultTy = PointerType::get(ResultTy, AddressSpace);
Chris Lattner1314b992007-04-22 06:23:29 +0000779 break;
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000780 }
Nuno Lopes561dae02012-05-23 15:19:39 +0000781 case bitc::TYPE_CODE_FUNCTION_OLD: {
782 // FIXME: attrid is dead, remove it in LLVM 4.0
783 // FUNCTION: [vararg, attrid, retty, paramty x N]
784 if (Record.size() < 3)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000785 return Error(InvalidRecord);
Nuno Lopes561dae02012-05-23 15:19:39 +0000786 SmallVector<Type*, 8> ArgTys;
787 for (unsigned i = 3, e = Record.size(); i != e; ++i) {
788 if (Type *T = getTypeByID(Record[i]))
789 ArgTys.push_back(T);
790 else
791 break;
792 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000793
Nuno Lopes561dae02012-05-23 15:19:39 +0000794 ResultTy = getTypeByID(Record[2]);
795 if (ResultTy == 0 || ArgTys.size() < Record.size()-3)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000796 return Error(InvalidType);
Nuno Lopes561dae02012-05-23 15:19:39 +0000797
798 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
799 break;
800 }
Chad Rosier95898722011-11-03 00:14:01 +0000801 case bitc::TYPE_CODE_FUNCTION: {
802 // FUNCTION: [vararg, retty, paramty x N]
803 if (Record.size() < 2)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000804 return Error(InvalidRecord);
Chris Lattnercc3aaf12012-01-27 03:15:49 +0000805 SmallVector<Type*, 8> ArgTys;
Chad Rosier95898722011-11-03 00:14:01 +0000806 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
807 if (Type *T = getTypeByID(Record[i]))
808 ArgTys.push_back(T);
809 else
810 break;
811 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000812
Chad Rosier95898722011-11-03 00:14:01 +0000813 ResultTy = getTypeByID(Record[1]);
814 if (ResultTy == 0 || ArgTys.size() < Record.size()-2)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000815 return Error(InvalidType);
Chad Rosier95898722011-11-03 00:14:01 +0000816
817 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
818 break;
819 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000820 case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N]
Chris Lattner3c5616e2007-05-06 08:21:50 +0000821 if (Record.size() < 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000822 return Error(InvalidRecord);
Chris Lattnercc3aaf12012-01-27 03:15:49 +0000823 SmallVector<Type*, 8> EltTys;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000824 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
825 if (Type *T = getTypeByID(Record[i]))
826 EltTys.push_back(T);
827 else
828 break;
829 }
830 if (EltTys.size() != Record.size()-1)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000831 return Error(InvalidType);
Owen Anderson03cb69f2009-08-05 23:16:16 +0000832 ResultTy = StructType::get(Context, EltTys, Record[0]);
Chris Lattner1314b992007-04-22 06:23:29 +0000833 break;
834 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000835 case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N]
836 if (ConvertToString(Record, 0, TypeName))
Rafael Espindola48da4f42013-11-04 16:16:24 +0000837 return Error(InvalidRecord);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000838 continue;
839
840 case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N]
841 if (Record.size() < 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000842 return Error(InvalidRecord);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000843
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000844 if (NumRecords >= TypeList.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +0000845 return Error(InvalidTYPETable);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000846
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000847 // Check to see if this was forward referenced, if so fill in the temp.
848 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
849 if (Res) {
850 Res->setName(TypeName);
851 TypeList[NumRecords] = 0;
852 } else // Otherwise, create a new struct.
Chris Lattner335d3992011-08-12 18:06:37 +0000853 Res = StructType::create(Context, TypeName);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000854 TypeName.clear();
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000855
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000856 SmallVector<Type*, 8> EltTys;
857 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
858 if (Type *T = getTypeByID(Record[i]))
859 EltTys.push_back(T);
860 else
861 break;
862 }
863 if (EltTys.size() != Record.size()-1)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000864 return Error(InvalidRecord);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000865 Res->setBody(EltTys, Record[0]);
866 ResultTy = Res;
867 break;
868 }
869 case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: []
870 if (Record.size() != 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000871 return Error(InvalidRecord);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000872
873 if (NumRecords >= TypeList.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +0000874 return Error(InvalidTYPETable);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000875
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000876 // Check to see if this was forward referenced, if so fill in the temp.
877 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
878 if (Res) {
879 Res->setName(TypeName);
880 TypeList[NumRecords] = 0;
881 } else // Otherwise, create a new struct with no body.
Chris Lattner335d3992011-08-12 18:06:37 +0000882 Res = StructType::create(Context, TypeName);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000883 TypeName.clear();
884 ResultTy = Res;
885 break;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000886 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000887 case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty]
888 if (Record.size() < 2)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000889 return Error(InvalidRecord);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000890 if ((ResultTy = getTypeByID(Record[1])))
891 ResultTy = ArrayType::get(ResultTy, Record[0]);
892 else
Rafael Espindola48da4f42013-11-04 16:16:24 +0000893 return Error(InvalidType);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000894 break;
895 case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty]
896 if (Record.size() < 2)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000897 return Error(InvalidRecord);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000898 if ((ResultTy = getTypeByID(Record[1])))
899 ResultTy = VectorType::get(ResultTy, Record[0]);
900 else
Rafael Espindola48da4f42013-11-04 16:16:24 +0000901 return Error(InvalidType);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000902 break;
903 }
904
905 if (NumRecords >= TypeList.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +0000906 return Error(InvalidTYPETable);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000907 assert(ResultTy && "Didn't read a type?");
908 assert(TypeList[NumRecords] == 0 && "Already read type?");
909 TypeList[NumRecords++] = ResultTy;
910 }
911}
912
Rafael Espindola48da4f42013-11-04 16:16:24 +0000913error_code BitcodeReader::ParseValueSymbolTable() {
Chris Lattner982ec1e2007-05-05 00:17:00 +0000914 if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +0000915 return Error(InvalidRecord);
Chris Lattnerccaa4482007-04-23 21:26:05 +0000916
917 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000918
Chris Lattnerccaa4482007-04-23 21:26:05 +0000919 // Read all the records for this value table.
920 SmallString<128> ValueName;
921 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +0000922 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +0000923
Chris Lattner27d38752013-01-20 02:13:19 +0000924 switch (Entry.Kind) {
925 case BitstreamEntry::SubBlock: // Handled for us already.
926 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +0000927 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +0000928 case BitstreamEntry::EndBlock:
Rafael Espindola48da4f42013-11-04 16:16:24 +0000929 return error_code::success();
Chris Lattner27d38752013-01-20 02:13:19 +0000930 case BitstreamEntry::Record:
931 // The interesting case.
932 break;
Chris Lattnerccaa4482007-04-23 21:26:05 +0000933 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000934
Chris Lattnerccaa4482007-04-23 21:26:05 +0000935 // Read a record.
936 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +0000937 switch (Stream.readRecord(Entry.ID, Record)) {
Chris Lattnerccaa4482007-04-23 21:26:05 +0000938 default: // Default behavior: unknown type.
939 break;
Chris Lattnere14cb882007-05-04 19:11:41 +0000940 case bitc::VST_CODE_ENTRY: { // VST_ENTRY: [valueid, namechar x N]
Chris Lattnerccaa4482007-04-23 21:26:05 +0000941 if (ConvertToString(Record, 1, ValueName))
Rafael Espindola48da4f42013-11-04 16:16:24 +0000942 return Error(InvalidRecord);
Chris Lattnerccaa4482007-04-23 21:26:05 +0000943 unsigned ValueID = Record[0];
944 if (ValueID >= ValueList.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +0000945 return Error(InvalidRecord);
Chris Lattnerccaa4482007-04-23 21:26:05 +0000946 Value *V = ValueList[ValueID];
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000947
Daniel Dunbard786b512009-07-26 00:34:27 +0000948 V->setName(StringRef(ValueName.data(), ValueName.size()));
Chris Lattnerccaa4482007-04-23 21:26:05 +0000949 ValueName.clear();
950 break;
Reid Spencerdea02bd2007-05-04 01:43:33 +0000951 }
Bill Wendling35a9c3c2011-04-10 23:18:04 +0000952 case bitc::VST_CODE_BBENTRY: {
Chris Lattner6be58c62007-05-03 22:18:21 +0000953 if (ConvertToString(Record, 1, ValueName))
Rafael Espindola48da4f42013-11-04 16:16:24 +0000954 return Error(InvalidRecord);
Chris Lattner6be58c62007-05-03 22:18:21 +0000955 BasicBlock *BB = getBasicBlock(Record[0]);
956 if (BB == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000957 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000958
Daniel Dunbard786b512009-07-26 00:34:27 +0000959 BB->setName(StringRef(ValueName.data(), ValueName.size()));
Chris Lattner6be58c62007-05-03 22:18:21 +0000960 ValueName.clear();
961 break;
Chris Lattnerccaa4482007-04-23 21:26:05 +0000962 }
Reid Spencerdea02bd2007-05-04 01:43:33 +0000963 }
Chris Lattnerccaa4482007-04-23 21:26:05 +0000964 }
965}
966
Rafael Espindola48da4f42013-11-04 16:16:24 +0000967error_code BitcodeReader::ParseMetadata() {
Devang Patel89923232010-01-11 18:52:33 +0000968 unsigned NextMDValueNo = MDValueList.size();
Devang Patel7428d8a2009-07-22 17:43:22 +0000969
970 if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +0000971 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000972
Devang Patel7428d8a2009-07-22 17:43:22 +0000973 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000974
Devang Patel7428d8a2009-07-22 17:43:22 +0000975 // Read all the records.
976 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +0000977 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +0000978
Chris Lattner27d38752013-01-20 02:13:19 +0000979 switch (Entry.Kind) {
980 case BitstreamEntry::SubBlock: // Handled for us already.
981 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +0000982 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +0000983 case BitstreamEntry::EndBlock:
Rafael Espindola48da4f42013-11-04 16:16:24 +0000984 return error_code::success();
Chris Lattner27d38752013-01-20 02:13:19 +0000985 case BitstreamEntry::Record:
986 // The interesting case.
987 break;
Devang Patel7428d8a2009-07-22 17:43:22 +0000988 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000989
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000990 bool IsFunctionLocal = false;
Devang Patel7428d8a2009-07-22 17:43:22 +0000991 // Read a record.
992 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +0000993 unsigned Code = Stream.readRecord(Entry.ID, Record);
Dan Gohmanbbcd04d2010-09-13 18:00:48 +0000994 switch (Code) {
Devang Patel7428d8a2009-07-22 17:43:22 +0000995 default: // Default behavior: ignore.
996 break;
Devang Patel27c87ff2009-07-29 22:34:41 +0000997 case bitc::METADATA_NAME: {
Chris Lattner8d140532013-01-20 02:54:05 +0000998 // Read name of the named metadata.
Benjamin Kramer9704ed02012-05-28 14:10:31 +0000999 SmallString<8> Name(Record.begin(), Record.end());
Devang Patel27c87ff2009-07-29 22:34:41 +00001000 Record.clear();
1001 Code = Stream.ReadCode();
1002
Chris Lattnerb8778552011-06-17 17:50:30 +00001003 // METADATA_NAME is always followed by METADATA_NAMED_NODE.
Chris Lattner27d38752013-01-20 02:13:19 +00001004 unsigned NextBitCode = Stream.readRecord(Code, Record);
Chris Lattnerb8778552011-06-17 17:50:30 +00001005 assert(NextBitCode == bitc::METADATA_NAMED_NODE); (void)NextBitCode;
Devang Patel27c87ff2009-07-29 22:34:41 +00001006
1007 // Read named metadata elements.
1008 unsigned Size = Record.size();
Dan Gohman2637cc12010-07-21 23:38:33 +00001009 NamedMDNode *NMD = TheModule->getOrInsertNamedMetadata(Name);
Devang Patel27c87ff2009-07-29 22:34:41 +00001010 for (unsigned i = 0; i != Size; ++i) {
Chris Lattner003143c2010-01-09 02:02:37 +00001011 MDNode *MD = dyn_cast<MDNode>(MDValueList.getValueFwdRef(Record[i]));
1012 if (MD == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001013 return Error(InvalidRecord);
Dan Gohman2637cc12010-07-21 23:38:33 +00001014 NMD->addOperand(MD);
Devang Patel27c87ff2009-07-29 22:34:41 +00001015 }
Devang Patel27c87ff2009-07-29 22:34:41 +00001016 break;
1017 }
Chris Lattnerb8778552011-06-17 17:50:30 +00001018 case bitc::METADATA_FN_NODE:
Victor Hernandezb8fd1522010-01-10 07:14:18 +00001019 IsFunctionLocal = true;
1020 // fall-through
Chris Lattnerb8778552011-06-17 17:50:30 +00001021 case bitc::METADATA_NODE: {
Dan Gohman1e0213a2010-07-13 19:33:27 +00001022 if (Record.size() % 2 == 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001023 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001024
Devang Patele059ba6e2009-07-23 01:07:34 +00001025 unsigned Size = Record.size();
1026 SmallVector<Value*, 8> Elts;
1027 for (unsigned i = 0; i != Size; i += 2) {
Chris Lattner229907c2011-07-18 04:54:35 +00001028 Type *Ty = getTypeByID(Record[i]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001029 if (!Ty)
1030 return Error(InvalidRecord);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001031 if (Ty->isMetadataTy())
Devang Patel05eb6172009-08-04 06:00:18 +00001032 Elts.push_back(MDValueList.getValueFwdRef(Record[i+1]));
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001033 else if (!Ty->isVoidTy())
Devang Patele059ba6e2009-07-23 01:07:34 +00001034 Elts.push_back(ValueList.getValueFwdRef(Record[i+1], Ty));
1035 else
1036 Elts.push_back(NULL);
1037 }
Jay Foad5514afe2011-04-21 19:59:31 +00001038 Value *V = MDNode::getWhenValsUnresolved(Context, Elts, IsFunctionLocal);
Victor Hernandezb8fd1522010-01-10 07:14:18 +00001039 IsFunctionLocal = false;
Devang Patel89923232010-01-11 18:52:33 +00001040 MDValueList.AssignValue(V, NextMDValueNo++);
Devang Patele059ba6e2009-07-23 01:07:34 +00001041 break;
1042 }
Devang Patel7428d8a2009-07-22 17:43:22 +00001043 case bitc::METADATA_STRING: {
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001044 SmallString<8> String(Record.begin(), Record.end());
1045 Value *V = MDString::get(Context, String);
Devang Patel89923232010-01-11 18:52:33 +00001046 MDValueList.AssignValue(V, NextMDValueNo++);
Devang Patel7428d8a2009-07-22 17:43:22 +00001047 break;
1048 }
Devang Patelaf206b82009-09-18 19:26:43 +00001049 case bitc::METADATA_KIND: {
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001050 if (Record.size() < 2)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001051 return Error(InvalidRecord);
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001052
Devang Patelb1a44772009-09-28 21:14:55 +00001053 unsigned Kind = Record[0];
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001054 SmallString<8> Name(Record.begin()+1, Record.end());
1055
Chris Lattnera0566972009-12-29 09:01:33 +00001056 unsigned NewKind = TheModule->getMDKindID(Name.str());
Dan Gohman43aa8f02010-07-20 21:42:28 +00001057 if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001058 return Error(ConflictingMETADATA_KINDRecords);
Devang Patelaf206b82009-09-18 19:26:43 +00001059 break;
1060 }
Devang Patel7428d8a2009-07-22 17:43:22 +00001061 }
1062 }
1063}
1064
Jan Wen Voungafaced02012-10-11 20:20:40 +00001065/// decodeSignRotatedValue - Decode a signed value stored with the sign bit in
Chris Lattner08feb1e2007-04-24 04:04:35 +00001066/// the LSB for dense VBR encoding.
Jan Wen Voungafaced02012-10-11 20:20:40 +00001067uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) {
Chris Lattner08feb1e2007-04-24 04:04:35 +00001068 if ((V & 1) == 0)
1069 return V >> 1;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001070 if (V != 1)
Chris Lattner08feb1e2007-04-24 04:04:35 +00001071 return -(V >> 1);
1072 // There is no such thing as -0 with integers. "-0" really means MININT.
1073 return 1ULL << 63;
1074}
1075
Chris Lattner44c17072007-04-26 02:46:40 +00001076/// ResolveGlobalAndAliasInits - Resolve all of the initializers for global
1077/// values and aliases that we can.
Rafael Espindola48da4f42013-11-04 16:16:24 +00001078error_code BitcodeReader::ResolveGlobalAndAliasInits() {
Chris Lattner44c17072007-04-26 02:46:40 +00001079 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist;
1080 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist;
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001081 std::vector<std::pair<Function*, unsigned> > FunctionPrefixWorklist;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001082
Chris Lattner44c17072007-04-26 02:46:40 +00001083 GlobalInitWorklist.swap(GlobalInits);
1084 AliasInitWorklist.swap(AliasInits);
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001085 FunctionPrefixWorklist.swap(FunctionPrefixes);
Chris Lattner44c17072007-04-26 02:46:40 +00001086
1087 while (!GlobalInitWorklist.empty()) {
Chris Lattner831d4202007-04-26 03:27:58 +00001088 unsigned ValID = GlobalInitWorklist.back().second;
Chris Lattner44c17072007-04-26 02:46:40 +00001089 if (ValID >= ValueList.size()) {
1090 // Not ready to resolve this yet, it requires something later in the file.
Chris Lattner831d4202007-04-26 03:27:58 +00001091 GlobalInits.push_back(GlobalInitWorklist.back());
Chris Lattner44c17072007-04-26 02:46:40 +00001092 } else {
1093 if (Constant *C = dyn_cast<Constant>(ValueList[ValID]))
1094 GlobalInitWorklist.back().first->setInitializer(C);
1095 else
Rafael Espindola48da4f42013-11-04 16:16:24 +00001096 return Error(ExpectedConstant);
Chris Lattner44c17072007-04-26 02:46:40 +00001097 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001098 GlobalInitWorklist.pop_back();
Chris Lattner44c17072007-04-26 02:46:40 +00001099 }
1100
1101 while (!AliasInitWorklist.empty()) {
1102 unsigned ValID = AliasInitWorklist.back().second;
1103 if (ValID >= ValueList.size()) {
1104 AliasInits.push_back(AliasInitWorklist.back());
1105 } else {
1106 if (Constant *C = dyn_cast<Constant>(ValueList[ValID]))
Anton Korobeynikovdaf358b2007-04-28 14:57:59 +00001107 AliasInitWorklist.back().first->setAliasee(C);
Chris Lattner44c17072007-04-26 02:46:40 +00001108 else
Rafael Espindola48da4f42013-11-04 16:16:24 +00001109 return Error(ExpectedConstant);
Chris Lattner44c17072007-04-26 02:46:40 +00001110 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001111 AliasInitWorklist.pop_back();
Chris Lattner44c17072007-04-26 02:46:40 +00001112 }
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001113
1114 while (!FunctionPrefixWorklist.empty()) {
1115 unsigned ValID = FunctionPrefixWorklist.back().second;
1116 if (ValID >= ValueList.size()) {
1117 FunctionPrefixes.push_back(FunctionPrefixWorklist.back());
1118 } else {
1119 if (Constant *C = dyn_cast<Constant>(ValueList[ValID]))
1120 FunctionPrefixWorklist.back().first->setPrefixData(C);
1121 else
Rafael Espindola48da4f42013-11-04 16:16:24 +00001122 return Error(ExpectedConstant);
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001123 }
1124 FunctionPrefixWorklist.pop_back();
1125 }
1126
Rafael Espindola48da4f42013-11-04 16:16:24 +00001127 return error_code::success();
Chris Lattner44c17072007-04-26 02:46:40 +00001128}
1129
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001130static APInt ReadWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) {
1131 SmallVector<uint64_t, 8> Words(Vals.size());
1132 std::transform(Vals.begin(), Vals.end(), Words.begin(),
Jan Wen Voungafaced02012-10-11 20:20:40 +00001133 BitcodeReader::decodeSignRotatedValue);
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001134
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00001135 return APInt(TypeBits, Words);
1136}
1137
Rafael Espindola48da4f42013-11-04 16:16:24 +00001138error_code BitcodeReader::ParseConstants() {
Chris Lattner982ec1e2007-05-05 00:17:00 +00001139 if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001140 return Error(InvalidRecord);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001141
1142 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001143
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001144 // Read all the records for this value table.
Chris Lattner229907c2011-07-18 04:54:35 +00001145 Type *CurTy = Type::getInt32Ty(Context);
Chris Lattner1663cca2007-04-24 05:48:56 +00001146 unsigned NextCstNo = ValueList.size();
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001147 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00001148 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00001149
Chris Lattner27d38752013-01-20 02:13:19 +00001150 switch (Entry.Kind) {
1151 case BitstreamEntry::SubBlock: // Handled for us already.
1152 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001153 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00001154 case BitstreamEntry::EndBlock:
1155 if (NextCstNo != ValueList.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001156 return Error(InvalidConstantReference);
Joe Abbey97b7a172013-02-06 22:14:06 +00001157
Chris Lattner27d38752013-01-20 02:13:19 +00001158 // Once all the constants have been read, go through and resolve forward
1159 // references.
1160 ValueList.ResolveConstantForwardRefs();
Rafael Espindola48da4f42013-11-04 16:16:24 +00001161 return error_code::success();
Chris Lattner27d38752013-01-20 02:13:19 +00001162 case BitstreamEntry::Record:
1163 // The interesting case.
Chris Lattner74429932008-08-21 02:34:16 +00001164 break;
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001165 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001166
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001167 // Read a record.
1168 Record.clear();
1169 Value *V = 0;
Chris Lattner27d38752013-01-20 02:13:19 +00001170 unsigned BitCode = Stream.readRecord(Entry.ID, Record);
Dan Gohman0ebd6962009-07-20 21:19:07 +00001171 switch (BitCode) {
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001172 default: // Default behavior: unknown constant
1173 case bitc::CST_CODE_UNDEF: // UNDEF
Owen Andersonb292b8c2009-07-30 23:03:37 +00001174 V = UndefValue::get(CurTy);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001175 break;
1176 case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid]
1177 if (Record.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001178 return Error(InvalidRecord);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001179 if (Record[0] >= TypeList.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001180 return Error(InvalidRecord);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001181 CurTy = TypeList[Record[0]];
Chris Lattner08feb1e2007-04-24 04:04:35 +00001182 continue; // Skip the ValueList manipulation.
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001183 case bitc::CST_CODE_NULL: // NULL
Owen Anderson5a1acd92009-07-31 20:28:14 +00001184 V = Constant::getNullValue(CurTy);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001185 break;
1186 case bitc::CST_CODE_INTEGER: // INTEGER: [intval]
Duncan Sands19d0b472010-02-16 11:11:14 +00001187 if (!CurTy->isIntegerTy() || Record.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001188 return Error(InvalidRecord);
Jan Wen Voungafaced02012-10-11 20:20:40 +00001189 V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0]));
Chris Lattner08feb1e2007-04-24 04:04:35 +00001190 break;
Chris Lattnere14cb882007-05-04 19:11:41 +00001191 case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
Duncan Sands19d0b472010-02-16 11:11:14 +00001192 if (!CurTy->isIntegerTy() || Record.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001193 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001194
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001195 APInt VInt = ReadWideAPInt(Record,
1196 cast<IntegerType>(CurTy)->getBitWidth());
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00001197 V = ConstantInt::get(Context, VInt);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001198
Chris Lattner08feb1e2007-04-24 04:04:35 +00001199 break;
1200 }
Dale Johannesen245dceb2007-09-11 18:32:33 +00001201 case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval]
Chris Lattner08feb1e2007-04-24 04:04:35 +00001202 if (Record.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001203 return Error(InvalidRecord);
Dan Gohman518cda42011-12-17 00:04:22 +00001204 if (CurTy->isHalfTy())
Tim Northover29178a32013-01-22 09:46:31 +00001205 V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf,
1206 APInt(16, (uint16_t)Record[0])));
Dan Gohman518cda42011-12-17 00:04:22 +00001207 else if (CurTy->isFloatTy())
Tim Northover29178a32013-01-22 09:46:31 +00001208 V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle,
1209 APInt(32, (uint32_t)Record[0])));
Chris Lattnerfdd87902009-10-05 05:54:46 +00001210 else if (CurTy->isDoubleTy())
Tim Northover29178a32013-01-22 09:46:31 +00001211 V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble,
1212 APInt(64, Record[0])));
Chris Lattnerfdd87902009-10-05 05:54:46 +00001213 else if (CurTy->isX86_FP80Ty()) {
Dale Johannesen93eefa02009-03-23 21:16:53 +00001214 // Bits are not stored the same way as a normal i80 APInt, compensate.
1215 uint64_t Rearrange[2];
1216 Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);
1217 Rearrange[1] = Record[0] >> 48;
Tim Northover29178a32013-01-22 09:46:31 +00001218 V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended,
1219 APInt(80, Rearrange)));
Chris Lattnerfdd87902009-10-05 05:54:46 +00001220 } else if (CurTy->isFP128Ty())
Tim Northover29178a32013-01-22 09:46:31 +00001221 V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad,
1222 APInt(128, Record)));
Chris Lattnerfdd87902009-10-05 05:54:46 +00001223 else if (CurTy->isPPC_FP128Ty())
Tim Northover29178a32013-01-22 09:46:31 +00001224 V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble,
1225 APInt(128, Record)));
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001226 else
Owen Andersonb292b8c2009-07-30 23:03:37 +00001227 V = UndefValue::get(CurTy);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001228 break;
Dale Johannesen245dceb2007-09-11 18:32:33 +00001229 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001230
Chris Lattnere14cb882007-05-04 19:11:41 +00001231 case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]
1232 if (Record.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001233 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001234
Chris Lattnere14cb882007-05-04 19:11:41 +00001235 unsigned Size = Record.size();
Chris Lattnercc3aaf12012-01-27 03:15:49 +00001236 SmallVector<Constant*, 16> Elts;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001237
Chris Lattner229907c2011-07-18 04:54:35 +00001238 if (StructType *STy = dyn_cast<StructType>(CurTy)) {
Chris Lattner1663cca2007-04-24 05:48:56 +00001239 for (unsigned i = 0; i != Size; ++i)
Chris Lattnere14cb882007-05-04 19:11:41 +00001240 Elts.push_back(ValueList.getConstantFwdRef(Record[i],
Chris Lattner1663cca2007-04-24 05:48:56 +00001241 STy->getElementType(i)));
Owen Anderson45308b52009-07-27 22:29:26 +00001242 V = ConstantStruct::get(STy, Elts);
Chris Lattner229907c2011-07-18 04:54:35 +00001243 } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) {
1244 Type *EltTy = ATy->getElementType();
Chris Lattner1663cca2007-04-24 05:48:56 +00001245 for (unsigned i = 0; i != Size; ++i)
Chris Lattnere14cb882007-05-04 19:11:41 +00001246 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
Owen Andersonc2c79322009-07-28 18:32:17 +00001247 V = ConstantArray::get(ATy, Elts);
Chris Lattner229907c2011-07-18 04:54:35 +00001248 } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) {
1249 Type *EltTy = VTy->getElementType();
Chris Lattner1663cca2007-04-24 05:48:56 +00001250 for (unsigned i = 0; i != Size; ++i)
Chris Lattnere14cb882007-05-04 19:11:41 +00001251 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
Owen Anderson4aa32952009-07-28 21:19:26 +00001252 V = ConstantVector::get(Elts);
Chris Lattner1663cca2007-04-24 05:48:56 +00001253 } else {
Owen Andersonb292b8c2009-07-30 23:03:37 +00001254 V = UndefValue::get(CurTy);
Chris Lattner1663cca2007-04-24 05:48:56 +00001255 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001256 break;
1257 }
Chris Lattnerbb8278a2012-02-05 02:41:35 +00001258 case bitc::CST_CODE_STRING: // STRING: [values]
Chris Lattnerf25f7102007-05-06 00:53:07 +00001259 case bitc::CST_CODE_CSTRING: { // CSTRING: [values]
1260 if (Record.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001261 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001262
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001263 SmallString<16> Elts(Record.begin(), Record.end());
Chris Lattnerbb8278a2012-02-05 02:41:35 +00001264 V = ConstantDataArray::getString(Context, Elts,
1265 BitCode == bitc::CST_CODE_CSTRING);
Chris Lattnerf25f7102007-05-06 00:53:07 +00001266 break;
1267 }
Chris Lattner372dd1e2012-01-30 00:51:16 +00001268 case bitc::CST_CODE_DATA: {// DATA: [n x value]
1269 if (Record.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001270 return Error(InvalidRecord);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001271
Chris Lattner372dd1e2012-01-30 00:51:16 +00001272 Type *EltTy = cast<SequentialType>(CurTy)->getElementType();
1273 unsigned Size = Record.size();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001274
Chris Lattner372dd1e2012-01-30 00:51:16 +00001275 if (EltTy->isIntegerTy(8)) {
1276 SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end());
1277 if (isa<VectorType>(CurTy))
1278 V = ConstantDataVector::get(Context, Elts);
1279 else
1280 V = ConstantDataArray::get(Context, Elts);
1281 } else if (EltTy->isIntegerTy(16)) {
1282 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
1283 if (isa<VectorType>(CurTy))
1284 V = ConstantDataVector::get(Context, Elts);
1285 else
1286 V = ConstantDataArray::get(Context, Elts);
1287 } else if (EltTy->isIntegerTy(32)) {
1288 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
1289 if (isa<VectorType>(CurTy))
1290 V = ConstantDataVector::get(Context, Elts);
1291 else
1292 V = ConstantDataArray::get(Context, Elts);
1293 } else if (EltTy->isIntegerTy(64)) {
1294 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
1295 if (isa<VectorType>(CurTy))
1296 V = ConstantDataVector::get(Context, Elts);
1297 else
1298 V = ConstantDataArray::get(Context, Elts);
1299 } else if (EltTy->isFloatTy()) {
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001300 SmallVector<float, 16> Elts(Size);
1301 std::transform(Record.begin(), Record.end(), Elts.begin(), BitsToFloat);
Chris Lattner372dd1e2012-01-30 00:51:16 +00001302 if (isa<VectorType>(CurTy))
1303 V = ConstantDataVector::get(Context, Elts);
1304 else
1305 V = ConstantDataArray::get(Context, Elts);
1306 } else if (EltTy->isDoubleTy()) {
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001307 SmallVector<double, 16> Elts(Size);
1308 std::transform(Record.begin(), Record.end(), Elts.begin(),
1309 BitsToDouble);
Chris Lattner372dd1e2012-01-30 00:51:16 +00001310 if (isa<VectorType>(CurTy))
1311 V = ConstantDataVector::get(Context, Elts);
1312 else
1313 V = ConstantDataArray::get(Context, Elts);
1314 } else {
Rafael Espindola48da4f42013-11-04 16:16:24 +00001315 return Error(InvalidTypeForValue);
Chris Lattner372dd1e2012-01-30 00:51:16 +00001316 }
1317 break;
1318 }
1319
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001320 case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval]
Rafael Espindola48da4f42013-11-04 16:16:24 +00001321 if (Record.size() < 3)
1322 return Error(InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001323 int Opc = GetDecodedBinaryOpcode(Record[0], CurTy);
Chris Lattner890683d2007-04-24 18:15:21 +00001324 if (Opc < 0) {
Owen Andersonb292b8c2009-07-30 23:03:37 +00001325 V = UndefValue::get(CurTy); // Unknown binop.
Chris Lattner890683d2007-04-24 18:15:21 +00001326 } else {
1327 Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy);
1328 Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy);
Dan Gohman1b849082009-09-07 23:54:19 +00001329 unsigned Flags = 0;
1330 if (Record.size() >= 4) {
1331 if (Opc == Instruction::Add ||
1332 Opc == Instruction::Sub ||
Chris Lattnera676c0f2011-02-07 16:40:21 +00001333 Opc == Instruction::Mul ||
1334 Opc == Instruction::Shl) {
Dan Gohman1b849082009-09-07 23:54:19 +00001335 if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP))
1336 Flags |= OverflowingBinaryOperator::NoSignedWrap;
1337 if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
1338 Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
Chris Lattner35315d02011-02-06 21:44:57 +00001339 } else if (Opc == Instruction::SDiv ||
Chris Lattnera676c0f2011-02-07 16:40:21 +00001340 Opc == Instruction::UDiv ||
1341 Opc == Instruction::LShr ||
1342 Opc == Instruction::AShr) {
Chris Lattner35315d02011-02-06 21:44:57 +00001343 if (Record[3] & (1 << bitc::PEO_EXACT))
Dan Gohman1b849082009-09-07 23:54:19 +00001344 Flags |= SDivOperator::IsExact;
1345 }
1346 }
1347 V = ConstantExpr::get(Opc, LHS, RHS, Flags);
Chris Lattner890683d2007-04-24 18:15:21 +00001348 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001349 break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001350 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001351 case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval]
Rafael Espindola48da4f42013-11-04 16:16:24 +00001352 if (Record.size() < 3)
1353 return Error(InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001354 int Opc = GetDecodedCastOpcode(Record[0]);
Chris Lattner890683d2007-04-24 18:15:21 +00001355 if (Opc < 0) {
Owen Andersonb292b8c2009-07-30 23:03:37 +00001356 V = UndefValue::get(CurTy); // Unknown cast.
Chris Lattner890683d2007-04-24 18:15:21 +00001357 } else {
Chris Lattner229907c2011-07-18 04:54:35 +00001358 Type *OpTy = getTypeByID(Record[1]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001359 if (!OpTy)
1360 return Error(InvalidRecord);
Chris Lattner890683d2007-04-24 18:15:21 +00001361 Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001362 V = UpgradeBitCastExpr(Opc, Op, CurTy);
1363 if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy);
Chris Lattner890683d2007-04-24 18:15:21 +00001364 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001365 break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001366 }
Dan Gohman1639c392009-07-27 21:53:46 +00001367 case bitc::CST_CODE_CE_INBOUNDS_GEP:
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001368 case bitc::CST_CODE_CE_GEP: { // CE_GEP: [n x operands]
Rafael Espindola48da4f42013-11-04 16:16:24 +00001369 if (Record.size() & 1)
1370 return Error(InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001371 SmallVector<Constant*, 16> Elts;
Chris Lattnere14cb882007-05-04 19:11:41 +00001372 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
Chris Lattner229907c2011-07-18 04:54:35 +00001373 Type *ElTy = getTypeByID(Record[i]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001374 if (!ElTy)
1375 return Error(InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001376 Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], ElTy));
1377 }
Jay Foaded8db7d2011-07-21 14:31:17 +00001378 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
Jay Foad2f5fc8c2011-07-21 15:15:37 +00001379 V = ConstantExpr::getGetElementPtr(Elts[0], Indices,
1380 BitCode ==
1381 bitc::CST_CODE_CE_INBOUNDS_GEP);
Chris Lattner890683d2007-04-24 18:15:21 +00001382 break;
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001383 }
Joe Abbey1a6e7702013-09-12 22:02:31 +00001384 case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#]
Rafael Espindola48da4f42013-11-04 16:16:24 +00001385 if (Record.size() < 3)
1386 return Error(InvalidRecord);
Joe Abbey1a6e7702013-09-12 22:02:31 +00001387
1388 Type *SelectorTy = Type::getInt1Ty(Context);
1389
1390 // If CurTy is a vector of length n, then Record[0] must be a <n x i1>
1391 // vector. Otherwise, it must be a single bit.
1392 if (VectorType *VTy = dyn_cast<VectorType>(CurTy))
1393 SelectorTy = VectorType::get(Type::getInt1Ty(Context),
1394 VTy->getNumElements());
1395
1396 V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0],
1397 SelectorTy),
1398 ValueList.getConstantFwdRef(Record[1],CurTy),
1399 ValueList.getConstantFwdRef(Record[2],CurTy));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001400 break;
Joe Abbey1a6e7702013-09-12 22:02:31 +00001401 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001402 case bitc::CST_CODE_CE_EXTRACTELT: { // CE_EXTRACTELT: [opty, opval, opval]
Rafael Espindola48da4f42013-11-04 16:16:24 +00001403 if (Record.size() < 3)
1404 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00001405 VectorType *OpTy =
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001406 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
Rafael Espindola48da4f42013-11-04 16:16:24 +00001407 if (OpTy == 0)
1408 return Error(InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001409 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
Joe Abbey2ad8df22012-11-25 15:23:39 +00001410 Constant *Op1 = ValueList.getConstantFwdRef(Record[2],
Joe Abbey5beb3f62012-11-19 19:22:55 +00001411 Type::getInt32Ty(Context));
Owen Anderson487375e2009-07-29 18:55:55 +00001412 V = ConstantExpr::getExtractElement(Op0, Op1);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001413 break;
1414 }
1415 case bitc::CST_CODE_CE_INSERTELT: { // CE_INSERTELT: [opval, opval, opval]
Chris Lattner229907c2011-07-18 04:54:35 +00001416 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001417 if (Record.size() < 3 || OpTy == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001418 return Error(InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001419 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
1420 Constant *Op1 = ValueList.getConstantFwdRef(Record[1],
1421 OpTy->getElementType());
Joe Abbey2ad8df22012-11-25 15:23:39 +00001422 Constant *Op2 = ValueList.getConstantFwdRef(Record[2],
Joe Abbey5beb3f62012-11-19 19:22:55 +00001423 Type::getInt32Ty(Context));
Owen Anderson487375e2009-07-29 18:55:55 +00001424 V = ConstantExpr::getInsertElement(Op0, Op1, Op2);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001425 break;
1426 }
1427 case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval]
Chris Lattner229907c2011-07-18 04:54:35 +00001428 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001429 if (Record.size() < 3 || OpTy == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001430 return Error(InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001431 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
1432 Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy);
Chris Lattner229907c2011-07-18 04:54:35 +00001433 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
Owen Andersone9f98042009-07-07 20:18:58 +00001434 OpTy->getNumElements());
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001435 Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy);
Owen Anderson487375e2009-07-29 18:55:55 +00001436 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001437 break;
1438 }
Nate Begeman94aa38d2009-02-12 21:28:33 +00001439 case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval]
Chris Lattner229907c2011-07-18 04:54:35 +00001440 VectorType *RTy = dyn_cast<VectorType>(CurTy);
1441 VectorType *OpTy =
Duncan Sands89d412a2010-10-28 15:47:26 +00001442 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
Nate Begeman94aa38d2009-02-12 21:28:33 +00001443 if (Record.size() < 4 || RTy == 0 || OpTy == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001444 return Error(InvalidRecord);
Nate Begeman94aa38d2009-02-12 21:28:33 +00001445 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
1446 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
Chris Lattner229907c2011-07-18 04:54:35 +00001447 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
Owen Andersone9f98042009-07-07 20:18:58 +00001448 RTy->getNumElements());
Nate Begeman94aa38d2009-02-12 21:28:33 +00001449 Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy);
Owen Anderson487375e2009-07-29 18:55:55 +00001450 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Nate Begeman94aa38d2009-02-12 21:28:33 +00001451 break;
1452 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001453 case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred]
Rafael Espindola48da4f42013-11-04 16:16:24 +00001454 if (Record.size() < 4)
1455 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00001456 Type *OpTy = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001457 if (OpTy == 0)
1458 return Error(InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001459 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
1460 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
1461
Duncan Sands9dff9be2010-02-15 16:12:20 +00001462 if (OpTy->isFPOrFPVectorTy())
Owen Anderson487375e2009-07-29 18:55:55 +00001463 V = ConstantExpr::getFCmp(Record[3], Op0, Op1);
Nate Begemand2195702008-05-12 19:01:56 +00001464 else
Owen Anderson487375e2009-07-29 18:55:55 +00001465 V = ConstantExpr::getICmp(Record[3], Op0, Op1);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001466 break;
Chris Lattner1663cca2007-04-24 05:48:56 +00001467 }
Chad Rosierd8c76102012-09-05 19:00:49 +00001468 // This maintains backward compatibility, pre-asm dialect keywords.
Chad Rosier5895eda2012-09-05 06:28:52 +00001469 // FIXME: Remove with the 4.0 release.
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001470 case bitc::CST_CODE_INLINEASM_OLD: {
Rafael Espindola48da4f42013-11-04 16:16:24 +00001471 if (Record.size() < 2)
1472 return Error(InvalidRecord);
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001473 std::string AsmStr, ConstrStr;
Dale Johannesenfd04c742009-10-13 20:46:56 +00001474 bool HasSideEffects = Record[0] & 1;
Dale Johannesen1cfb9582009-10-21 23:28:00 +00001475 bool IsAlignStack = Record[0] >> 1;
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001476 unsigned AsmStrSize = Record[1];
1477 if (2+AsmStrSize >= Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001478 return Error(InvalidRecord);
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001479 unsigned ConstStrSize = Record[2+AsmStrSize];
1480 if (3+AsmStrSize+ConstStrSize > Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001481 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001482
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001483 for (unsigned i = 0; i != AsmStrSize; ++i)
1484 AsmStr += (char)Record[2+i];
1485 for (unsigned i = 0; i != ConstStrSize; ++i)
1486 ConstrStr += (char)Record[3+AsmStrSize+i];
Chris Lattner229907c2011-07-18 04:54:35 +00001487 PointerType *PTy = cast<PointerType>(CurTy);
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001488 V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
Dale Johannesen1cfb9582009-10-21 23:28:00 +00001489 AsmStr, ConstrStr, HasSideEffects, IsAlignStack);
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001490 break;
1491 }
Chad Rosierd8c76102012-09-05 19:00:49 +00001492 // This version adds support for the asm dialect keywords (e.g.,
1493 // inteldialect).
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001494 case bitc::CST_CODE_INLINEASM: {
Rafael Espindola48da4f42013-11-04 16:16:24 +00001495 if (Record.size() < 2)
1496 return Error(InvalidRecord);
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001497 std::string AsmStr, ConstrStr;
1498 bool HasSideEffects = Record[0] & 1;
1499 bool IsAlignStack = (Record[0] >> 1) & 1;
1500 unsigned AsmDialect = Record[0] >> 2;
1501 unsigned AsmStrSize = Record[1];
1502 if (2+AsmStrSize >= Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001503 return Error(InvalidRecord);
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001504 unsigned ConstStrSize = Record[2+AsmStrSize];
1505 if (3+AsmStrSize+ConstStrSize > Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001506 return Error(InvalidRecord);
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001507
1508 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];
1512 PointerType *PTy = cast<PointerType>(CurTy);
1513 V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
1514 AsmStr, ConstrStr, HasSideEffects, IsAlignStack,
Chad Rosierd8c76102012-09-05 19:00:49 +00001515 InlineAsm::AsmDialect(AsmDialect));
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001516 break;
1517 }
Chris Lattner5956dc82009-10-28 05:53:48 +00001518 case bitc::CST_CODE_BLOCKADDRESS:{
Rafael Espindola48da4f42013-11-04 16:16:24 +00001519 if (Record.size() < 3)
1520 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00001521 Type *FnTy = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001522 if (FnTy == 0)
1523 return Error(InvalidRecord);
Chris Lattner5956dc82009-10-28 05:53:48 +00001524 Function *Fn =
1525 dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy));
Rafael Espindola48da4f42013-11-04 16:16:24 +00001526 if (Fn == 0)
1527 return Error(InvalidRecord);
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00001528
1529 // If the function is already parsed we can insert the block address right
1530 // away.
1531 if (!Fn->empty()) {
1532 Function::iterator BBI = Fn->begin(), BBE = Fn->end();
1533 for (size_t I = 0, E = Record[2]; I != E; ++I) {
1534 if (BBI == BBE)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001535 return Error(InvalidID);
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00001536 ++BBI;
1537 }
1538 V = BlockAddress::get(Fn, BBI);
1539 } else {
1540 // Otherwise insert a placeholder and remember it so it can be inserted
1541 // when the function is parsed.
1542 GlobalVariable *FwdRef = new GlobalVariable(*Fn->getParent(),
1543 Type::getInt8Ty(Context),
Chris Lattner5956dc82009-10-28 05:53:48 +00001544 false, GlobalValue::InternalLinkage,
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00001545 0, "");
1546 BlockAddrFwdRefs[Fn].push_back(std::make_pair(Record[2], FwdRef));
1547 V = FwdRef;
1548 }
Chris Lattner5956dc82009-10-28 05:53:48 +00001549 break;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001550 }
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001551 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001552
Chris Lattner83930552007-05-01 07:01:57 +00001553 ValueList.AssignValue(V, NextCstNo);
Chris Lattner1663cca2007-04-24 05:48:56 +00001554 ++NextCstNo;
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001555 }
1556}
Chris Lattner1314b992007-04-22 06:23:29 +00001557
Rafael Espindola48da4f42013-11-04 16:16:24 +00001558error_code BitcodeReader::ParseUseLists() {
Chad Rosierca2567b2011-12-07 21:44:12 +00001559 if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001560 return Error(InvalidRecord);
Chad Rosierca2567b2011-12-07 21:44:12 +00001561
1562 SmallVector<uint64_t, 64> Record;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001563
Chad Rosierca2567b2011-12-07 21:44:12 +00001564 // Read all the records.
1565 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00001566 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00001567
Chris Lattner27d38752013-01-20 02:13:19 +00001568 switch (Entry.Kind) {
1569 case BitstreamEntry::SubBlock: // Handled for us already.
1570 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001571 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00001572 case BitstreamEntry::EndBlock:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001573 return error_code::success();
Chris Lattner27d38752013-01-20 02:13:19 +00001574 case BitstreamEntry::Record:
1575 // The interesting case.
1576 break;
Chad Rosierca2567b2011-12-07 21:44:12 +00001577 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001578
Chad Rosierca2567b2011-12-07 21:44:12 +00001579 // Read a use list record.
1580 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +00001581 switch (Stream.readRecord(Entry.ID, Record)) {
Chad Rosierca2567b2011-12-07 21:44:12 +00001582 default: // Default behavior: unknown type.
1583 break;
1584 case bitc::USELIST_CODE_ENTRY: { // USELIST_CODE_ENTRY: TBD.
1585 unsigned RecordLength = Record.size();
1586 if (RecordLength < 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001587 return Error(InvalidRecord);
Chad Rosierca2567b2011-12-07 21:44:12 +00001588 UseListRecords.push_back(Record);
1589 break;
1590 }
1591 }
1592 }
1593}
1594
Chris Lattner85b7b402007-05-01 05:52:21 +00001595/// RememberAndSkipFunctionBody - When we see the block for a function body,
1596/// remember where it is and then skip it. This lets us lazily deserialize the
1597/// functions.
Rafael Espindola48da4f42013-11-04 16:16:24 +00001598error_code BitcodeReader::RememberAndSkipFunctionBody() {
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001599 // Get the function we are talking about.
1600 if (FunctionsWithBodies.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001601 return Error(InsufficientFunctionProtos);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001602
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001603 Function *Fn = FunctionsWithBodies.back();
1604 FunctionsWithBodies.pop_back();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001605
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001606 // Save the current stream state.
1607 uint64_t CurBit = Stream.GetCurrentBitNo();
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001608 DeferredFunctionInfo[Fn] = CurBit;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001609
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001610 // Skip over the function block for now.
1611 if (Stream.SkipBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001612 return Error(InvalidRecord);
1613 return error_code::success();
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001614}
1615
Rafael Espindola48da4f42013-11-04 16:16:24 +00001616error_code BitcodeReader::GlobalCleanup() {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001617 // Patch the initializers for globals and aliases up.
1618 ResolveGlobalAndAliasInits();
1619 if (!GlobalInits.empty() || !AliasInits.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001620 return Error(MalformedGlobalInitializerSet);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001621
1622 // Look for intrinsic functions which need to be upgraded at some point
1623 for (Module::iterator FI = TheModule->begin(), FE = TheModule->end();
1624 FI != FE; ++FI) {
1625 Function *NewFn;
1626 if (UpgradeIntrinsicFunction(FI, NewFn))
1627 UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn));
1628 }
1629
1630 // Look for global variables which need to be renamed.
1631 for (Module::global_iterator
1632 GI = TheModule->global_begin(), GE = TheModule->global_end();
1633 GI != GE; ++GI)
1634 UpgradeGlobalVariable(GI);
1635 // Force deallocation of memory for these vectors to favor the client that
1636 // want lazy deserialization.
1637 std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits);
1638 std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001639 return error_code::success();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001640}
1641
Rafael Espindola48da4f42013-11-04 16:16:24 +00001642error_code BitcodeReader::ParseModule(bool Resume) {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001643 if (Resume)
1644 Stream.JumpToBit(NextUnreadBit);
1645 else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001646 return Error(InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001647
Chris Lattner1314b992007-04-22 06:23:29 +00001648 SmallVector<uint64_t, 64> Record;
1649 std::vector<std::string> SectionTable;
Gordon Henriksend930f912008-08-17 18:44:35 +00001650 std::vector<std::string> GCTable;
Chris Lattner1314b992007-04-22 06:23:29 +00001651
1652 // Read all the records for this module.
Chris Lattner27d38752013-01-20 02:13:19 +00001653 while (1) {
1654 BitstreamEntry Entry = Stream.advance();
Joe Abbey97b7a172013-02-06 22:14:06 +00001655
Chris Lattner27d38752013-01-20 02:13:19 +00001656 switch (Entry.Kind) {
1657 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001658 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00001659 case BitstreamEntry::EndBlock:
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001660 return GlobalCleanup();
Joe Abbey97b7a172013-02-06 22:14:06 +00001661
Chris Lattner27d38752013-01-20 02:13:19 +00001662 case BitstreamEntry::SubBlock:
1663 switch (Entry.ID) {
Chris Lattner1314b992007-04-22 06:23:29 +00001664 default: // Skip unknown content.
1665 if (Stream.SkipBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001666 return Error(InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001667 break;
Chris Lattner6eeea5d2007-05-05 18:57:30 +00001668 case bitc::BLOCKINFO_BLOCK_ID:
1669 if (Stream.ReadBlockInfoBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001670 return Error(MalformedBlock);
Chris Lattner6eeea5d2007-05-05 18:57:30 +00001671 break;
Chris Lattnerfee5a372007-05-04 03:30:17 +00001672 case bitc::PARAMATTR_BLOCK_ID:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001673 if (error_code EC = ParseAttributeBlock())
1674 return EC;
Chris Lattnerfee5a372007-05-04 03:30:17 +00001675 break;
Bill Wendlingba629332013-02-10 23:24:25 +00001676 case bitc::PARAMATTR_GROUP_BLOCK_ID:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001677 if (error_code EC = ParseAttributeGroupBlock())
1678 return EC;
Bill Wendlingba629332013-02-10 23:24:25 +00001679 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001680 case bitc::TYPE_BLOCK_ID_NEW:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001681 if (error_code EC = ParseTypeTable())
1682 return EC;
Chris Lattner1314b992007-04-22 06:23:29 +00001683 break;
Chris Lattnerccaa4482007-04-23 21:26:05 +00001684 case bitc::VALUE_SYMTAB_BLOCK_ID:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001685 if (error_code EC = ParseValueSymbolTable())
1686 return EC;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001687 SeenValueSymbolTable = true;
Chris Lattnerccaa4482007-04-23 21:26:05 +00001688 break;
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001689 case bitc::CONSTANTS_BLOCK_ID:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001690 if (error_code EC = ParseConstants())
1691 return EC;
1692 if (error_code EC = ResolveGlobalAndAliasInits())
1693 return EC;
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001694 break;
Devang Patel7428d8a2009-07-22 17:43:22 +00001695 case bitc::METADATA_BLOCK_ID:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001696 if (error_code EC = ParseMetadata())
1697 return EC;
Devang Patel7428d8a2009-07-22 17:43:22 +00001698 break;
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001699 case bitc::FUNCTION_BLOCK_ID:
1700 // If this is the first function body we've seen, reverse the
1701 // FunctionsWithBodies list.
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001702 if (!SeenFirstFunctionBody) {
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001703 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
Rafael Espindola48da4f42013-11-04 16:16:24 +00001704 if (error_code EC = GlobalCleanup())
1705 return EC;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001706 SeenFirstFunctionBody = true;
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001707 }
Joe Abbey97b7a172013-02-06 22:14:06 +00001708
Rafael Espindola48da4f42013-11-04 16:16:24 +00001709 if (error_code EC = RememberAndSkipFunctionBody())
1710 return EC;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001711 // For streaming bitcode, suspend parsing when we reach the function
1712 // bodies. Subsequent materialization calls will resume it when
1713 // necessary. For streaming, the function bodies must be at the end of
1714 // the bitcode. If the bitcode file is old, the symbol table will be
1715 // at the end instead and will not have been seen yet. In this case,
1716 // just finish the parse now.
1717 if (LazyStreamer && SeenValueSymbolTable) {
1718 NextUnreadBit = Stream.GetCurrentBitNo();
Rafael Espindola48da4f42013-11-04 16:16:24 +00001719 return error_code::success();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001720 }
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001721 break;
Chad Rosierca2567b2011-12-07 21:44:12 +00001722 case bitc::USELIST_BLOCK_ID:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001723 if (error_code EC = ParseUseLists())
1724 return EC;
Chad Rosierca2567b2011-12-07 21:44:12 +00001725 break;
Chris Lattner1314b992007-04-22 06:23:29 +00001726 }
1727 continue;
Joe Abbey97b7a172013-02-06 22:14:06 +00001728
Chris Lattner27d38752013-01-20 02:13:19 +00001729 case BitstreamEntry::Record:
1730 // The interesting case.
1731 break;
Chris Lattner1314b992007-04-22 06:23:29 +00001732 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001733
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001734
Chris Lattner1314b992007-04-22 06:23:29 +00001735 // Read a record.
Chris Lattner27d38752013-01-20 02:13:19 +00001736 switch (Stream.readRecord(Entry.ID, Record)) {
Chris Lattner1314b992007-04-22 06:23:29 +00001737 default: break; // Default behavior, ignore unknown content.
Jan Wen Voungafaced02012-10-11 20:20:40 +00001738 case bitc::MODULE_CODE_VERSION: { // VERSION: [version#]
Chris Lattner1314b992007-04-22 06:23:29 +00001739 if (Record.size() < 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001740 return Error(InvalidRecord);
Jan Wen Voungafaced02012-10-11 20:20:40 +00001741 // Only version #0 and #1 are supported so far.
1742 unsigned module_version = Record[0];
1743 switch (module_version) {
Rafael Espindola48da4f42013-11-04 16:16:24 +00001744 default:
1745 return Error(InvalidValue);
Jan Wen Voungafaced02012-10-11 20:20:40 +00001746 case 0:
1747 UseRelativeIDs = false;
1748 break;
1749 case 1:
1750 UseRelativeIDs = true;
1751 break;
1752 }
Chris Lattner1314b992007-04-22 06:23:29 +00001753 break;
Jan Wen Voungafaced02012-10-11 20:20:40 +00001754 }
Chris Lattnere14cb882007-05-04 19:11:41 +00001755 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00001756 std::string S;
1757 if (ConvertToString(Record, 0, S))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001758 return Error(InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001759 TheModule->setTargetTriple(S);
1760 break;
1761 }
Chris Lattnere14cb882007-05-04 19:11:41 +00001762 case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00001763 std::string S;
1764 if (ConvertToString(Record, 0, S))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001765 return Error(InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001766 TheModule->setDataLayout(S);
1767 break;
1768 }
Chris Lattnere14cb882007-05-04 19:11:41 +00001769 case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00001770 std::string S;
1771 if (ConvertToString(Record, 0, S))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001772 return Error(InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001773 TheModule->setModuleInlineAsm(S);
1774 break;
1775 }
Bill Wendling706d3d62012-11-28 08:41:48 +00001776 case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N]
1777 // FIXME: Remove in 4.0.
1778 std::string S;
1779 if (ConvertToString(Record, 0, S))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001780 return Error(InvalidRecord);
Bill Wendling706d3d62012-11-28 08:41:48 +00001781 // Ignore value.
1782 break;
1783 }
Chris Lattnere14cb882007-05-04 19:11:41 +00001784 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00001785 std::string S;
1786 if (ConvertToString(Record, 0, S))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001787 return Error(InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001788 SectionTable.push_back(S);
1789 break;
1790 }
Gordon Henriksend930f912008-08-17 18:44:35 +00001791 case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N]
Gordon Henriksen71183b62007-12-10 03:18:06 +00001792 std::string S;
1793 if (ConvertToString(Record, 0, S))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001794 return Error(InvalidRecord);
Gordon Henriksend930f912008-08-17 18:44:35 +00001795 GCTable.push_back(S);
Gordon Henriksen71183b62007-12-10 03:18:06 +00001796 break;
1797 }
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001798 // GLOBALVAR: [pointer type, isconst, initid,
Rafael Espindola45e6c192011-01-08 16:42:36 +00001799 // linkage, alignment, section, visibility, threadlocal,
1800 // unnamed_addr]
Chris Lattner1314b992007-04-22 06:23:29 +00001801 case bitc::MODULE_CODE_GLOBALVAR: {
Chris Lattner4b00d922007-04-23 16:04:05 +00001802 if (Record.size() < 6)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001803 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00001804 Type *Ty = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001805 if (!Ty)
1806 return Error(InvalidRecord);
Duncan Sands19d0b472010-02-16 11:11:14 +00001807 if (!Ty->isPointerTy())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001808 return Error(InvalidTypeForValue);
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001809 unsigned AddressSpace = cast<PointerType>(Ty)->getAddressSpace();
Chris Lattner1314b992007-04-22 06:23:29 +00001810 Ty = cast<PointerType>(Ty)->getElementType();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001811
Chris Lattner1314b992007-04-22 06:23:29 +00001812 bool isConstant = Record[1];
1813 GlobalValue::LinkageTypes Linkage = GetDecodedLinkage(Record[3]);
1814 unsigned Alignment = (1 << Record[4]) >> 1;
1815 std::string Section;
1816 if (Record[5]) {
1817 if (Record[5]-1 >= SectionTable.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001818 return Error(InvalidID);
Chris Lattner1314b992007-04-22 06:23:29 +00001819 Section = SectionTable[Record[5]-1];
1820 }
Chris Lattner4b00d922007-04-23 16:04:05 +00001821 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
Chris Lattner53862f72007-05-06 19:27:46 +00001822 if (Record.size() > 6)
1823 Visibility = GetDecodedVisibility(Record[6]);
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001824
1825 GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal;
Chris Lattner53862f72007-05-06 19:27:46 +00001826 if (Record.size() > 7)
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001827 TLM = GetDecodedThreadLocalMode(Record[7]);
Chris Lattner1314b992007-04-22 06:23:29 +00001828
Rafael Espindola45e6c192011-01-08 16:42:36 +00001829 bool UnnamedAddr = false;
1830 if (Record.size() > 8)
1831 UnnamedAddr = Record[8];
1832
Michael Gottesman27e7ef32013-02-05 05:57:38 +00001833 bool ExternallyInitialized = false;
1834 if (Record.size() > 9)
1835 ExternallyInitialized = Record[9];
1836
Chris Lattner1314b992007-04-22 06:23:29 +00001837 GlobalVariable *NewGV =
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001838 new GlobalVariable(*TheModule, Ty, isConstant, Linkage, 0, "", 0,
Michael Gottesman27e7ef32013-02-05 05:57:38 +00001839 TLM, AddressSpace, ExternallyInitialized);
Chris Lattner1314b992007-04-22 06:23:29 +00001840 NewGV->setAlignment(Alignment);
1841 if (!Section.empty())
1842 NewGV->setSection(Section);
1843 NewGV->setVisibility(Visibility);
Rafael Espindola45e6c192011-01-08 16:42:36 +00001844 NewGV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001845
Chris Lattnerccaa4482007-04-23 21:26:05 +00001846 ValueList.push_back(NewGV);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001847
Chris Lattner47d131b2007-04-24 00:18:21 +00001848 // Remember which value to use for the global initializer.
1849 if (unsigned InitID = Record[2])
1850 GlobalInits.push_back(std::make_pair(NewGV, InitID-1));
Chris Lattner1314b992007-04-22 06:23:29 +00001851 break;
1852 }
Chris Lattner4c0a6d62007-05-08 05:38:01 +00001853 // FUNCTION: [type, callingconv, isproto, linkage, paramattr,
Rafael Espindola45e6c192011-01-08 16:42:36 +00001854 // alignment, section, visibility, gc, unnamed_addr]
Chris Lattner1314b992007-04-22 06:23:29 +00001855 case bitc::MODULE_CODE_FUNCTION: {
Chris Lattner4c0a6d62007-05-08 05:38:01 +00001856 if (Record.size() < 8)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001857 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00001858 Type *Ty = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001859 if (!Ty)
1860 return Error(InvalidRecord);
Duncan Sands19d0b472010-02-16 11:11:14 +00001861 if (!Ty->isPointerTy())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001862 return Error(InvalidTypeForValue);
Chris Lattner229907c2011-07-18 04:54:35 +00001863 FunctionType *FTy =
Chris Lattner1314b992007-04-22 06:23:29 +00001864 dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType());
1865 if (!FTy)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001866 return Error(InvalidTypeForValue);
Chris Lattner1314b992007-04-22 06:23:29 +00001867
Gabor Greife9ecc682008-04-06 20:25:17 +00001868 Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage,
1869 "", TheModule);
Chris Lattner1314b992007-04-22 06:23:29 +00001870
Sandeep Patel68c5f472009-09-02 08:44:58 +00001871 Func->setCallingConv(static_cast<CallingConv::ID>(Record[1]));
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001872 bool isProto = Record[2];
Chris Lattner1314b992007-04-22 06:23:29 +00001873 Func->setLinkage(GetDecodedLinkage(Record[3]));
Devang Patel4c758ea2008-09-25 21:00:45 +00001874 Func->setAttributes(getAttributes(Record[4]));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001875
Chris Lattner4c0a6d62007-05-08 05:38:01 +00001876 Func->setAlignment((1 << Record[5]) >> 1);
1877 if (Record[6]) {
1878 if (Record[6]-1 >= SectionTable.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001879 return Error(InvalidID);
Chris Lattner4c0a6d62007-05-08 05:38:01 +00001880 Func->setSection(SectionTable[Record[6]-1]);
Chris Lattner1314b992007-04-22 06:23:29 +00001881 }
Chris Lattner4c0a6d62007-05-08 05:38:01 +00001882 Func->setVisibility(GetDecodedVisibility(Record[7]));
Gordon Henriksen71183b62007-12-10 03:18:06 +00001883 if (Record.size() > 8 && Record[8]) {
Gordon Henriksend930f912008-08-17 18:44:35 +00001884 if (Record[8]-1 > GCTable.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001885 return Error(InvalidID);
Gordon Henriksend930f912008-08-17 18:44:35 +00001886 Func->setGC(GCTable[Record[8]-1].c_str());
Gordon Henriksen71183b62007-12-10 03:18:06 +00001887 }
Rafael Espindola45e6c192011-01-08 16:42:36 +00001888 bool UnnamedAddr = false;
1889 if (Record.size() > 9)
1890 UnnamedAddr = Record[9];
1891 Func->setUnnamedAddr(UnnamedAddr);
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001892 if (Record.size() > 10 && Record[10] != 0)
1893 FunctionPrefixes.push_back(std::make_pair(Func, Record[10]-1));
Chris Lattnerccaa4482007-04-23 21:26:05 +00001894 ValueList.push_back(Func);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001895
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001896 // If this is a function with a body, remember the prototype we are
1897 // creating now, so that we can match up the body with them later.
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001898 if (!isProto) {
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001899 FunctionsWithBodies.push_back(Func);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001900 if (LazyStreamer) DeferredFunctionInfo[Func] = 0;
1901 }
Chris Lattner1314b992007-04-22 06:23:29 +00001902 break;
1903 }
Anton Korobeynikov2f22e3f2008-03-12 00:49:19 +00001904 // ALIAS: [alias type, aliasee val#, linkage]
Anton Korobeynikovdb691cb2008-03-11 21:40:17 +00001905 // ALIAS: [alias type, aliasee val#, linkage, visibility]
Chris Lattner831d4202007-04-26 03:27:58 +00001906 case bitc::MODULE_CODE_ALIAS: {
Chris Lattner44c17072007-04-26 02:46:40 +00001907 if (Record.size() < 3)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001908 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00001909 Type *Ty = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001910 if (!Ty)
1911 return Error(InvalidRecord);
Duncan Sands19d0b472010-02-16 11:11:14 +00001912 if (!Ty->isPointerTy())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001913 return Error(InvalidTypeForValue);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001914
Chris Lattner44c17072007-04-26 02:46:40 +00001915 GlobalAlias *NewGA = new GlobalAlias(Ty, GetDecodedLinkage(Record[2]),
1916 "", 0, TheModule);
Anton Korobeynikov2f22e3f2008-03-12 00:49:19 +00001917 // Old bitcode files didn't have visibility field.
1918 if (Record.size() > 3)
1919 NewGA->setVisibility(GetDecodedVisibility(Record[3]));
Chris Lattner44c17072007-04-26 02:46:40 +00001920 ValueList.push_back(NewGA);
1921 AliasInits.push_back(std::make_pair(NewGA, Record[1]));
1922 break;
Chris Lattner1314b992007-04-22 06:23:29 +00001923 }
Chris Lattner831d4202007-04-26 03:27:58 +00001924 /// MODULE_CODE_PURGEVALS: [numvals]
1925 case bitc::MODULE_CODE_PURGEVALS:
1926 // Trim down the value list to the specified size.
1927 if (Record.size() < 1 || Record[0] > ValueList.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001928 return Error(InvalidRecord);
Chris Lattner831d4202007-04-26 03:27:58 +00001929 ValueList.shrinkTo(Record[0]);
1930 break;
1931 }
Chris Lattner1314b992007-04-22 06:23:29 +00001932 Record.clear();
1933 }
Chris Lattner1314b992007-04-22 06:23:29 +00001934}
1935
Rafael Espindola48da4f42013-11-04 16:16:24 +00001936error_code BitcodeReader::ParseBitcodeInto(Module *M) {
Chris Lattner1314b992007-04-22 06:23:29 +00001937 TheModule = 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001938
Rafael Espindola48da4f42013-11-04 16:16:24 +00001939 if (error_code EC = InitStream())
1940 return EC;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001941
Chris Lattner1314b992007-04-22 06:23:29 +00001942 // Sniff for the signature.
1943 if (Stream.Read(8) != 'B' ||
1944 Stream.Read(8) != 'C' ||
1945 Stream.Read(4) != 0x0 ||
1946 Stream.Read(4) != 0xC ||
1947 Stream.Read(4) != 0xE ||
1948 Stream.Read(4) != 0xD)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001949 return Error(InvalidBitcodeSignature);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001950
Chris Lattner1314b992007-04-22 06:23:29 +00001951 // We expect a number of well-defined blocks, though we don't necessarily
1952 // need to understand them all.
Chris Lattner27d38752013-01-20 02:13:19 +00001953 while (1) {
1954 if (Stream.AtEndOfStream())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001955 return error_code::success();
Joe Abbey97b7a172013-02-06 22:14:06 +00001956
Chris Lattner27d38752013-01-20 02:13:19 +00001957 BitstreamEntry Entry =
1958 Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
Joe Abbey97b7a172013-02-06 22:14:06 +00001959
Chris Lattner27d38752013-01-20 02:13:19 +00001960 switch (Entry.Kind) {
1961 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001962 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00001963 case BitstreamEntry::EndBlock:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001964 return error_code::success();
Joe Abbey97b7a172013-02-06 22:14:06 +00001965
Chris Lattner27d38752013-01-20 02:13:19 +00001966 case BitstreamEntry::SubBlock:
1967 switch (Entry.ID) {
1968 case bitc::BLOCKINFO_BLOCK_ID:
1969 if (Stream.ReadBlockInfoBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001970 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00001971 break;
1972 case bitc::MODULE_BLOCK_ID:
1973 // Reject multiple MODULE_BLOCK's in a single bitstream.
1974 if (TheModule)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001975 return Error(InvalidMultipleBlocks);
Chris Lattner27d38752013-01-20 02:13:19 +00001976 TheModule = M;
Rafael Espindola48da4f42013-11-04 16:16:24 +00001977 if (error_code EC = ParseModule(false))
1978 return EC;
1979 if (LazyStreamer)
1980 return error_code::success();
Chris Lattner27d38752013-01-20 02:13:19 +00001981 break;
1982 default:
1983 if (Stream.SkipBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001984 return Error(InvalidRecord);
Chris Lattner27d38752013-01-20 02:13:19 +00001985 break;
1986 }
1987 continue;
1988 case BitstreamEntry::Record:
1989 // There should be no records in the top-level of blocks.
Joe Abbey97b7a172013-02-06 22:14:06 +00001990
Chris Lattner27d38752013-01-20 02:13:19 +00001991 // The ranlib in Xcode 4 will align archive members by appending newlines
Chad Rosiera15e3aa2011-08-09 22:23:40 +00001992 // to the end of them. If this file size is a multiple of 4 but not 8, we
1993 // have to read and ignore these final 4 bytes :-(
Chris Lattner27d38752013-01-20 02:13:19 +00001994 if (Stream.getAbbrevIDWidth() == 2 && Entry.ID == 2 &&
Rafael Espindolaa97b2382011-05-26 18:59:54 +00001995 Stream.Read(6) == 2 && Stream.Read(24) == 0xa0a0a &&
Bill Wendling318f03f2012-07-19 00:15:11 +00001996 Stream.AtEndOfStream())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001997 return error_code::success();
Joe Abbey97b7a172013-02-06 22:14:06 +00001998
Rafael Espindola48da4f42013-11-04 16:16:24 +00001999 return Error(InvalidRecord);
Rafael Espindolaa97b2382011-05-26 18:59:54 +00002000 }
Chris Lattner1314b992007-04-22 06:23:29 +00002001 }
Chris Lattner1314b992007-04-22 06:23:29 +00002002}
Chris Lattner6694f602007-04-29 07:54:31 +00002003
Rafael Espindola48da4f42013-11-04 16:16:24 +00002004error_code BitcodeReader::ParseModuleTriple(std::string &Triple) {
Bill Wendling0198ce02010-10-06 01:22:42 +00002005 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002006 return Error(InvalidRecord);
Bill Wendling0198ce02010-10-06 01:22:42 +00002007
2008 SmallVector<uint64_t, 64> Record;
2009
2010 // Read all the records for this module.
Chris Lattner27d38752013-01-20 02:13:19 +00002011 while (1) {
2012 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00002013
Chris Lattner27d38752013-01-20 02:13:19 +00002014 switch (Entry.Kind) {
2015 case BitstreamEntry::SubBlock: // Handled for us already.
2016 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +00002017 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002018 case BitstreamEntry::EndBlock:
Rafael Espindola48da4f42013-11-04 16:16:24 +00002019 return error_code::success();
Chris Lattner27d38752013-01-20 02:13:19 +00002020 case BitstreamEntry::Record:
2021 // The interesting case.
2022 break;
Bill Wendling0198ce02010-10-06 01:22:42 +00002023 }
2024
2025 // Read a record.
Chris Lattner27d38752013-01-20 02:13:19 +00002026 switch (Stream.readRecord(Entry.ID, Record)) {
Bill Wendling0198ce02010-10-06 01:22:42 +00002027 default: break; // Default behavior, ignore unknown content.
Bill Wendling0198ce02010-10-06 01:22:42 +00002028 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
2029 std::string S;
2030 if (ConvertToString(Record, 0, S))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002031 return Error(InvalidRecord);
Bill Wendling0198ce02010-10-06 01:22:42 +00002032 Triple = S;
2033 break;
2034 }
2035 }
2036 Record.clear();
2037 }
Bill Wendling0198ce02010-10-06 01:22:42 +00002038}
2039
Rafael Espindola48da4f42013-11-04 16:16:24 +00002040error_code BitcodeReader::ParseTriple(std::string &Triple) {
2041 if (error_code EC = InitStream())
2042 return EC;
Bill Wendling0198ce02010-10-06 01:22:42 +00002043
2044 // Sniff for the signature.
2045 if (Stream.Read(8) != 'B' ||
2046 Stream.Read(8) != 'C' ||
2047 Stream.Read(4) != 0x0 ||
2048 Stream.Read(4) != 0xC ||
2049 Stream.Read(4) != 0xE ||
2050 Stream.Read(4) != 0xD)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002051 return Error(InvalidBitcodeSignature);
Bill Wendling0198ce02010-10-06 01:22:42 +00002052
2053 // We expect a number of well-defined blocks, though we don't necessarily
2054 // need to understand them all.
Chris Lattner27d38752013-01-20 02:13:19 +00002055 while (1) {
2056 BitstreamEntry Entry = Stream.advance();
Joe Abbey97b7a172013-02-06 22:14:06 +00002057
Chris Lattner27d38752013-01-20 02:13:19 +00002058 switch (Entry.Kind) {
2059 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +00002060 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002061 case BitstreamEntry::EndBlock:
Rafael Espindola48da4f42013-11-04 16:16:24 +00002062 return error_code::success();
Joe Abbey97b7a172013-02-06 22:14:06 +00002063
Chris Lattner27d38752013-01-20 02:13:19 +00002064 case BitstreamEntry::SubBlock:
2065 if (Entry.ID == bitc::MODULE_BLOCK_ID)
2066 return ParseModuleTriple(Triple);
Joe Abbey97b7a172013-02-06 22:14:06 +00002067
Chris Lattner27d38752013-01-20 02:13:19 +00002068 // Ignore other sub-blocks.
Rafael Espindola48da4f42013-11-04 16:16:24 +00002069 if (Stream.SkipBlock())
2070 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002071 continue;
Joe Abbey97b7a172013-02-06 22:14:06 +00002072
Chris Lattner27d38752013-01-20 02:13:19 +00002073 case BitstreamEntry::Record:
2074 Stream.skipRecord(Entry.ID);
2075 continue;
Bill Wendling0198ce02010-10-06 01:22:42 +00002076 }
2077 }
Bill Wendling0198ce02010-10-06 01:22:42 +00002078}
2079
Devang Patelaf206b82009-09-18 19:26:43 +00002080/// ParseMetadataAttachment - Parse metadata attachments.
Rafael Espindola48da4f42013-11-04 16:16:24 +00002081error_code BitcodeReader::ParseMetadataAttachment() {
Devang Patelaf206b82009-09-18 19:26:43 +00002082 if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002083 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002084
Devang Patelaf206b82009-09-18 19:26:43 +00002085 SmallVector<uint64_t, 64> Record;
Chris Lattner27d38752013-01-20 02:13:19 +00002086 while (1) {
2087 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00002088
Chris Lattner27d38752013-01-20 02:13:19 +00002089 switch (Entry.Kind) {
2090 case BitstreamEntry::SubBlock: // Handled for us already.
2091 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +00002092 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002093 case BitstreamEntry::EndBlock:
Rafael Espindola48da4f42013-11-04 16:16:24 +00002094 return error_code::success();
Chris Lattner27d38752013-01-20 02:13:19 +00002095 case BitstreamEntry::Record:
2096 // The interesting case.
Devang Patelaf206b82009-09-18 19:26:43 +00002097 break;
2098 }
Chris Lattner27d38752013-01-20 02:13:19 +00002099
Devang Patelaf206b82009-09-18 19:26:43 +00002100 // Read a metadata attachment record.
2101 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +00002102 switch (Stream.readRecord(Entry.ID, Record)) {
Devang Patelaf206b82009-09-18 19:26:43 +00002103 default: // Default behavior: ignore.
2104 break;
Chris Lattnerb8778552011-06-17 17:50:30 +00002105 case bitc::METADATA_ATTACHMENT: {
Devang Patelaf206b82009-09-18 19:26:43 +00002106 unsigned RecordLength = Record.size();
2107 if (Record.empty() || (RecordLength - 1) % 2 == 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002108 return Error(InvalidRecord);
Devang Patelaf206b82009-09-18 19:26:43 +00002109 Instruction *Inst = InstructionList[Record[0]];
2110 for (unsigned i = 1; i != RecordLength; i = i+2) {
Devang Patelb1a44772009-09-28 21:14:55 +00002111 unsigned Kind = Record[i];
Dan Gohman43aa8f02010-07-20 21:42:28 +00002112 DenseMap<unsigned, unsigned>::iterator I =
2113 MDKindMap.find(Kind);
2114 if (I == MDKindMap.end())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002115 return Error(InvalidID);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002116 Value *Node = MDValueList.getValueFwdRef(Record[i+1]);
Dan Gohman43aa8f02010-07-20 21:42:28 +00002117 Inst->setMetadata(I->second, cast<MDNode>(Node));
Manman Ren209b17c2013-09-28 00:22:27 +00002118 if (I->second == LLVMContext::MD_tbaa)
2119 InstsWithTBAATag.push_back(Inst);
Devang Patelaf206b82009-09-18 19:26:43 +00002120 }
2121 break;
2122 }
2123 }
2124 }
Devang Patelaf206b82009-09-18 19:26:43 +00002125}
Chris Lattner51ffe7c2007-05-01 04:59:48 +00002126
Chris Lattner85b7b402007-05-01 05:52:21 +00002127/// ParseFunctionBody - Lazily parse the specified function body block.
Rafael Espindola48da4f42013-11-04 16:16:24 +00002128error_code BitcodeReader::ParseFunctionBody(Function *F) {
Chris Lattner982ec1e2007-05-05 00:17:00 +00002129 if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002130 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002131
Nick Lewyckya72e1af2010-02-25 08:30:17 +00002132 InstructionList.clear();
Chris Lattner85b7b402007-05-01 05:52:21 +00002133 unsigned ModuleValueListSize = ValueList.size();
Dan Gohman26d837d2010-08-25 20:22:53 +00002134 unsigned ModuleMDValueListSize = MDValueList.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002135
Chris Lattner85b7b402007-05-01 05:52:21 +00002136 // Add all the function arguments to the value table.
2137 for(Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
2138 ValueList.push_back(I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002139
Chris Lattner83930552007-05-01 07:01:57 +00002140 unsigned NextValueNo = ValueList.size();
Chris Lattnere53603e2007-05-02 04:27:25 +00002141 BasicBlock *CurBB = 0;
2142 unsigned CurBBNo = 0;
2143
Chris Lattner07d09ed2010-04-03 02:17:50 +00002144 DebugLoc LastLoc;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002145
Chris Lattner85b7b402007-05-01 05:52:21 +00002146 // Read all the records.
2147 SmallVector<uint64_t, 64> Record;
2148 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00002149 BitstreamEntry Entry = Stream.advance();
Joe Abbey97b7a172013-02-06 22:14:06 +00002150
Chris Lattner27d38752013-01-20 02:13:19 +00002151 switch (Entry.Kind) {
2152 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +00002153 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002154 case BitstreamEntry::EndBlock:
2155 goto OutOfRecordLoop;
Joe Abbey97b7a172013-02-06 22:14:06 +00002156
Chris Lattner27d38752013-01-20 02:13:19 +00002157 case BitstreamEntry::SubBlock:
2158 switch (Entry.ID) {
Chris Lattner85b7b402007-05-01 05:52:21 +00002159 default: // Skip unknown content.
2160 if (Stream.SkipBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002161 return Error(InvalidRecord);
Chris Lattner85b7b402007-05-01 05:52:21 +00002162 break;
2163 case bitc::CONSTANTS_BLOCK_ID:
Rafael Espindola48da4f42013-11-04 16:16:24 +00002164 if (error_code EC = ParseConstants())
2165 return EC;
Chris Lattner83930552007-05-01 07:01:57 +00002166 NextValueNo = ValueList.size();
Chris Lattner85b7b402007-05-01 05:52:21 +00002167 break;
2168 case bitc::VALUE_SYMTAB_BLOCK_ID:
Rafael Espindola48da4f42013-11-04 16:16:24 +00002169 if (error_code EC = ParseValueSymbolTable())
2170 return EC;
Chris Lattner85b7b402007-05-01 05:52:21 +00002171 break;
Devang Patelaf206b82009-09-18 19:26:43 +00002172 case bitc::METADATA_ATTACHMENT_ID:
Rafael Espindola48da4f42013-11-04 16:16:24 +00002173 if (error_code EC = ParseMetadataAttachment())
2174 return EC;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002175 break;
Victor Hernandez108d3ac2010-01-13 19:34:08 +00002176 case bitc::METADATA_BLOCK_ID:
Rafael Espindola48da4f42013-11-04 16:16:24 +00002177 if (error_code EC = ParseMetadata())
2178 return EC;
Victor Hernandez108d3ac2010-01-13 19:34:08 +00002179 break;
Chris Lattner85b7b402007-05-01 05:52:21 +00002180 }
2181 continue;
Joe Abbey97b7a172013-02-06 22:14:06 +00002182
Chris Lattner27d38752013-01-20 02:13:19 +00002183 case BitstreamEntry::Record:
2184 // The interesting case.
2185 break;
Chris Lattner85b7b402007-05-01 05:52:21 +00002186 }
Joe Abbey97b7a172013-02-06 22:14:06 +00002187
Chris Lattner85b7b402007-05-01 05:52:21 +00002188 // Read a record.
2189 Record.clear();
Chris Lattner83930552007-05-01 07:01:57 +00002190 Instruction *I = 0;
Chris Lattner27d38752013-01-20 02:13:19 +00002191 unsigned BitCode = Stream.readRecord(Entry.ID, Record);
Dan Gohman0ebd6962009-07-20 21:19:07 +00002192 switch (BitCode) {
Chris Lattner83930552007-05-01 07:01:57 +00002193 default: // Default behavior: reject
Rafael Espindola48da4f42013-11-04 16:16:24 +00002194 return Error(InvalidValue);
Chris Lattner85b7b402007-05-01 05:52:21 +00002195 case bitc::FUNC_CODE_DECLAREBLOCKS: // DECLAREBLOCKS: [nblocks]
Chris Lattner83930552007-05-01 07:01:57 +00002196 if (Record.size() < 1 || Record[0] == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002197 return Error(InvalidRecord);
Chris Lattner85b7b402007-05-01 05:52:21 +00002198 // Create all the basic blocks for the function.
Chris Lattner6ce15cb2007-05-03 22:09:51 +00002199 FunctionBBs.resize(Record[0]);
Chris Lattner85b7b402007-05-01 05:52:21 +00002200 for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i)
Owen Anderson55f1c092009-08-13 21:58:54 +00002201 FunctionBBs[i] = BasicBlock::Create(Context, "", F);
Chris Lattner83930552007-05-01 07:01:57 +00002202 CurBB = FunctionBBs[0];
2203 continue;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002204
Chris Lattner07d09ed2010-04-03 02:17:50 +00002205 case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN
2206 // This record indicates that the last instruction is at the same
2207 // location as the previous instruction with a location.
2208 I = 0;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002209
Chris Lattner07d09ed2010-04-03 02:17:50 +00002210 // Get the last instruction emitted.
2211 if (CurBB && !CurBB->empty())
2212 I = &CurBB->back();
2213 else if (CurBBNo && FunctionBBs[CurBBNo-1] &&
2214 !FunctionBBs[CurBBNo-1]->empty())
2215 I = &FunctionBBs[CurBBNo-1]->back();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002216
Rafael Espindola48da4f42013-11-04 16:16:24 +00002217 if (I == 0)
2218 return Error(InvalidRecord);
Chris Lattner07d09ed2010-04-03 02:17:50 +00002219 I->setDebugLoc(LastLoc);
2220 I = 0;
2221 continue;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002222
Chris Lattnerc44070802011-06-17 18:17:37 +00002223 case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia]
Chris Lattner07d09ed2010-04-03 02:17:50 +00002224 I = 0; // Get the last instruction emitted.
2225 if (CurBB && !CurBB->empty())
2226 I = &CurBB->back();
2227 else if (CurBBNo && FunctionBBs[CurBBNo-1] &&
2228 !FunctionBBs[CurBBNo-1]->empty())
2229 I = &FunctionBBs[CurBBNo-1]->back();
2230 if (I == 0 || Record.size() < 4)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002231 return Error(InvalidRecord);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002232
Chris Lattner07d09ed2010-04-03 02:17:50 +00002233 unsigned Line = Record[0], Col = Record[1];
2234 unsigned ScopeID = Record[2], IAID = Record[3];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002235
Chris Lattner07d09ed2010-04-03 02:17:50 +00002236 MDNode *Scope = 0, *IA = 0;
2237 if (ScopeID) Scope = cast<MDNode>(MDValueList.getValueFwdRef(ScopeID-1));
2238 if (IAID) IA = cast<MDNode>(MDValueList.getValueFwdRef(IAID-1));
2239 LastLoc = DebugLoc::get(Line, Col, Scope, IA);
2240 I->setDebugLoc(LastLoc);
2241 I = 0;
2242 continue;
2243 }
2244
Chris Lattnere9759c22007-05-06 00:21:25 +00002245 case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode]
2246 unsigned OpNum = 0;
2247 Value *LHS, *RHS;
2248 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002249 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
Dan Gohman0ebd6962009-07-20 21:19:07 +00002250 OpNum+1 > Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002251 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002252
Dan Gohman0ebd6962009-07-20 21:19:07 +00002253 int Opc = GetDecodedBinaryOpcode(Record[OpNum++], LHS->getType());
Rafael Espindola48da4f42013-11-04 16:16:24 +00002254 if (Opc == -1)
2255 return Error(InvalidRecord);
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002256 I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
Devang Patelaf206b82009-09-18 19:26:43 +00002257 InstructionList.push_back(I);
Dan Gohman1b849082009-09-07 23:54:19 +00002258 if (OpNum < Record.size()) {
2259 if (Opc == Instruction::Add ||
2260 Opc == Instruction::Sub ||
Chris Lattnera676c0f2011-02-07 16:40:21 +00002261 Opc == Instruction::Mul ||
2262 Opc == Instruction::Shl) {
Dan Gohman00f47472010-01-25 21:55:39 +00002263 if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP))
Dan Gohman1b849082009-09-07 23:54:19 +00002264 cast<BinaryOperator>(I)->setHasNoSignedWrap(true);
Dan Gohman00f47472010-01-25 21:55:39 +00002265 if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
Dan Gohman1b849082009-09-07 23:54:19 +00002266 cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true);
Chris Lattner35315d02011-02-06 21:44:57 +00002267 } else if (Opc == Instruction::SDiv ||
Chris Lattnera676c0f2011-02-07 16:40:21 +00002268 Opc == Instruction::UDiv ||
2269 Opc == Instruction::LShr ||
2270 Opc == Instruction::AShr) {
Chris Lattner35315d02011-02-06 21:44:57 +00002271 if (Record[OpNum] & (1 << bitc::PEO_EXACT))
Dan Gohman1b849082009-09-07 23:54:19 +00002272 cast<BinaryOperator>(I)->setIsExact(true);
Michael Ilseman9978d7e2012-11-27 00:43:38 +00002273 } else if (isa<FPMathOperator>(I)) {
2274 FastMathFlags FMF;
Michael Ilseman65f14352012-12-09 21:12:04 +00002275 if (0 != (Record[OpNum] & FastMathFlags::UnsafeAlgebra))
2276 FMF.setUnsafeAlgebra();
2277 if (0 != (Record[OpNum] & FastMathFlags::NoNaNs))
2278 FMF.setNoNaNs();
2279 if (0 != (Record[OpNum] & FastMathFlags::NoInfs))
2280 FMF.setNoInfs();
2281 if (0 != (Record[OpNum] & FastMathFlags::NoSignedZeros))
2282 FMF.setNoSignedZeros();
2283 if (0 != (Record[OpNum] & FastMathFlags::AllowReciprocal))
2284 FMF.setAllowReciprocal();
Michael Ilseman9978d7e2012-11-27 00:43:38 +00002285 if (FMF.any())
2286 I->setFastMathFlags(FMF);
Dan Gohman1b849082009-09-07 23:54:19 +00002287 }
Michael Ilseman9978d7e2012-11-27 00:43:38 +00002288
Dan Gohman1b849082009-09-07 23:54:19 +00002289 }
Chris Lattner85b7b402007-05-01 05:52:21 +00002290 break;
2291 }
Chris Lattnere9759c22007-05-06 00:21:25 +00002292 case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc]
2293 unsigned OpNum = 0;
2294 Value *Op;
2295 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
2296 OpNum+2 != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002297 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002298
Chris Lattner229907c2011-07-18 04:54:35 +00002299 Type *ResTy = getTypeByID(Record[OpNum]);
Chris Lattnere9759c22007-05-06 00:21:25 +00002300 int Opc = GetDecodedCastOpcode(Record[OpNum+1]);
2301 if (Opc == -1 || ResTy == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002302 return Error(InvalidRecord);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002303 Instruction *Temp = 0;
2304 if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) {
2305 if (Temp) {
2306 InstructionList.push_back(Temp);
2307 CurBB->getInstList().push_back(Temp);
2308 }
2309 } else {
2310 I = CastInst::Create((Instruction::CastOps)Opc, Op, ResTy);
2311 }
Devang Patelaf206b82009-09-18 19:26:43 +00002312 InstructionList.push_back(I);
Chris Lattnere53603e2007-05-02 04:27:25 +00002313 break;
2314 }
Dan Gohman1639c392009-07-27 21:53:46 +00002315 case bitc::FUNC_CODE_INST_INBOUNDS_GEP:
Chris Lattnere14cb882007-05-04 19:11:41 +00002316 case bitc::FUNC_CODE_INST_GEP: { // GEP: [n x operands]
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002317 unsigned OpNum = 0;
2318 Value *BasePtr;
2319 if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002320 return Error(InvalidRecord);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002321
Chris Lattner5285b5e2007-05-02 05:46:45 +00002322 SmallVector<Value*, 16> GEPIdx;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002323 while (OpNum != Record.size()) {
2324 Value *Op;
2325 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002326 return Error(InvalidRecord);
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002327 GEPIdx.push_back(Op);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002328 }
2329
Jay Foadd1b78492011-07-25 09:48:08 +00002330 I = GetElementPtrInst::Create(BasePtr, GEPIdx);
Devang Patelaf206b82009-09-18 19:26:43 +00002331 InstructionList.push_back(I);
Dan Gohman1639c392009-07-27 21:53:46 +00002332 if (BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP)
Dan Gohman1b849082009-09-07 23:54:19 +00002333 cast<GetElementPtrInst>(I)->setIsInBounds(true);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002334 break;
2335 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002336
Dan Gohman1ecaf452008-05-31 00:58:22 +00002337 case bitc::FUNC_CODE_INST_EXTRACTVAL: {
2338 // EXTRACTVAL: [opty, opval, n x indices]
Dan Gohman30499842008-05-23 01:55:30 +00002339 unsigned OpNum = 0;
2340 Value *Agg;
2341 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002342 return Error(InvalidRecord);
Dan Gohman30499842008-05-23 01:55:30 +00002343
Dan Gohman1ecaf452008-05-31 00:58:22 +00002344 SmallVector<unsigned, 4> EXTRACTVALIdx;
2345 for (unsigned RecSize = Record.size();
2346 OpNum != RecSize; ++OpNum) {
2347 uint64_t Index = Record[OpNum];
2348 if ((unsigned)Index != Index)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002349 return Error(InvalidValue);
Dan Gohman1ecaf452008-05-31 00:58:22 +00002350 EXTRACTVALIdx.push_back((unsigned)Index);
Dan Gohman30499842008-05-23 01:55:30 +00002351 }
2352
Jay Foad57aa6362011-07-13 10:26:04 +00002353 I = ExtractValueInst::Create(Agg, EXTRACTVALIdx);
Devang Patelaf206b82009-09-18 19:26:43 +00002354 InstructionList.push_back(I);
Dan Gohman30499842008-05-23 01:55:30 +00002355 break;
2356 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002357
Dan Gohman1ecaf452008-05-31 00:58:22 +00002358 case bitc::FUNC_CODE_INST_INSERTVAL: {
2359 // INSERTVAL: [opty, opval, opty, opval, n x indices]
Dan Gohman30499842008-05-23 01:55:30 +00002360 unsigned OpNum = 0;
2361 Value *Agg;
2362 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002363 return Error(InvalidRecord);
Dan Gohman30499842008-05-23 01:55:30 +00002364 Value *Val;
2365 if (getValueTypePair(Record, OpNum, NextValueNo, Val))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002366 return Error(InvalidRecord);
Dan Gohman30499842008-05-23 01:55:30 +00002367
Dan Gohman1ecaf452008-05-31 00:58:22 +00002368 SmallVector<unsigned, 4> INSERTVALIdx;
2369 for (unsigned RecSize = Record.size();
2370 OpNum != RecSize; ++OpNum) {
2371 uint64_t Index = Record[OpNum];
2372 if ((unsigned)Index != Index)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002373 return Error(InvalidValue);
Dan Gohman1ecaf452008-05-31 00:58:22 +00002374 INSERTVALIdx.push_back((unsigned)Index);
Dan Gohman30499842008-05-23 01:55:30 +00002375 }
2376
Jay Foad57aa6362011-07-13 10:26:04 +00002377 I = InsertValueInst::Create(Agg, Val, INSERTVALIdx);
Devang Patelaf206b82009-09-18 19:26:43 +00002378 InstructionList.push_back(I);
Dan Gohman30499842008-05-23 01:55:30 +00002379 break;
2380 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002381
Chris Lattnere9759c22007-05-06 00:21:25 +00002382 case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval]
Dan Gohmanc5d28922008-09-16 01:01:33 +00002383 // obsolete form of select
2384 // handles select i1 ... in old bitcode
Chris Lattnere9759c22007-05-06 00:21:25 +00002385 unsigned OpNum = 0;
2386 Value *TrueVal, *FalseVal, *Cond;
2387 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002388 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
2389 popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002390 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002391
Dan Gohmanc5d28922008-09-16 01:01:33 +00002392 I = SelectInst::Create(Cond, TrueVal, FalseVal);
Devang Patelaf206b82009-09-18 19:26:43 +00002393 InstructionList.push_back(I);
Dan Gohmanc5d28922008-09-16 01:01:33 +00002394 break;
2395 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002396
Dan Gohmanc5d28922008-09-16 01:01:33 +00002397 case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred]
2398 // new form of select
2399 // handles select i1 or select [N x i1]
2400 unsigned OpNum = 0;
2401 Value *TrueVal, *FalseVal, *Cond;
2402 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002403 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
Dan Gohmanc5d28922008-09-16 01:01:33 +00002404 getValueTypePair(Record, OpNum, NextValueNo, Cond))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002405 return Error(InvalidRecord);
Dan Gohmanc579d972008-09-09 01:02:47 +00002406
2407 // select condition can be either i1 or [N x i1]
Chris Lattner229907c2011-07-18 04:54:35 +00002408 if (VectorType* vector_type =
2409 dyn_cast<VectorType>(Cond->getType())) {
Dan Gohmanc579d972008-09-09 01:02:47 +00002410 // expect <n x i1>
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002411 if (vector_type->getElementType() != Type::getInt1Ty(Context))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002412 return Error(InvalidTypeForValue);
Dan Gohmanc579d972008-09-09 01:02:47 +00002413 } else {
2414 // expect i1
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002415 if (Cond->getType() != Type::getInt1Ty(Context))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002416 return Error(InvalidTypeForValue);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002417 }
2418
Gabor Greife9ecc682008-04-06 20:25:17 +00002419 I = SelectInst::Create(Cond, TrueVal, FalseVal);
Devang Patelaf206b82009-09-18 19:26:43 +00002420 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002421 break;
2422 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002423
Chris Lattner1fc27f02007-05-02 05:16:49 +00002424 case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval]
Chris Lattnere9759c22007-05-06 00:21:25 +00002425 unsigned OpNum = 0;
2426 Value *Vec, *Idx;
2427 if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002428 popValue(Record, OpNum, NextValueNo, Type::getInt32Ty(Context), Idx))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002429 return Error(InvalidRecord);
Eric Christopherc9742252009-07-25 02:28:41 +00002430 I = ExtractElementInst::Create(Vec, Idx);
Devang Patelaf206b82009-09-18 19:26:43 +00002431 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002432 break;
2433 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002434
Chris Lattner1fc27f02007-05-02 05:16:49 +00002435 case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval]
Chris Lattnere9759c22007-05-06 00:21:25 +00002436 unsigned OpNum = 0;
2437 Value *Vec, *Elt, *Idx;
2438 if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002439 popValue(Record, OpNum, NextValueNo,
Chris Lattnere9759c22007-05-06 00:21:25 +00002440 cast<VectorType>(Vec->getType())->getElementType(), Elt) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002441 popValue(Record, OpNum, NextValueNo, Type::getInt32Ty(Context), Idx))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002442 return Error(InvalidRecord);
Gabor Greife9ecc682008-04-06 20:25:17 +00002443 I = InsertElementInst::Create(Vec, Elt, Idx);
Devang Patelaf206b82009-09-18 19:26:43 +00002444 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002445 break;
2446 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002447
Chris Lattnere9759c22007-05-06 00:21:25 +00002448 case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval]
2449 unsigned OpNum = 0;
2450 Value *Vec1, *Vec2, *Mask;
2451 if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002452 popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002453 return Error(InvalidRecord);
Chris Lattnere9759c22007-05-06 00:21:25 +00002454
Mon P Wang25f01062008-11-10 04:46:22 +00002455 if (getValueTypePair(Record, OpNum, NextValueNo, Mask))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002456 return Error(InvalidRecord);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002457 I = new ShuffleVectorInst(Vec1, Vec2, Mask);
Devang Patelaf206b82009-09-18 19:26:43 +00002458 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002459 break;
2460 }
Mon P Wang25f01062008-11-10 04:46:22 +00002461
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002462 case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred]
2463 // Old form of ICmp/FCmp returning bool
2464 // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were
2465 // both legal on vectors but had different behaviour.
2466 case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred]
2467 // FCmp/ICmp returning bool or vector of bool
2468
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002469 unsigned OpNum = 0;
2470 Value *LHS, *RHS;
2471 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002472 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002473 OpNum+1 != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002474 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002475
Duncan Sands9dff9be2010-02-15 16:12:20 +00002476 if (LHS->getType()->isFPOrFPVectorTy())
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002477 I = new FCmpInst((FCmpInst::Predicate)Record[OpNum], LHS, RHS);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002478 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002479 I = new ICmpInst((ICmpInst::Predicate)Record[OpNum], LHS, RHS);
Devang Patelaf206b82009-09-18 19:26:43 +00002480 InstructionList.push_back(I);
Dan Gohmanc579d972008-09-09 01:02:47 +00002481 break;
2482 }
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002483
Chris Lattnere53603e2007-05-02 04:27:25 +00002484 case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>]
Devang Patelbbfd8742008-02-26 01:29:32 +00002485 {
2486 unsigned Size = Record.size();
2487 if (Size == 0) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002488 I = ReturnInst::Create(Context);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002489 InstructionList.push_back(I);
Devang Patelbbfd8742008-02-26 01:29:32 +00002490 break;
Dan Gohmanfa1211f2008-07-23 00:34:11 +00002491 }
Devang Patelbbfd8742008-02-26 01:29:32 +00002492
Dan Gohmanfa1211f2008-07-23 00:34:11 +00002493 unsigned OpNum = 0;
Chris Lattnerf1c87102011-06-17 18:09:11 +00002494 Value *Op = NULL;
2495 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002496 return Error(InvalidRecord);
Chris Lattnerf1c87102011-06-17 18:09:11 +00002497 if (OpNum != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002498 return Error(InvalidRecord);
Dan Gohmanfa1211f2008-07-23 00:34:11 +00002499
Chris Lattnerf1c87102011-06-17 18:09:11 +00002500 I = ReturnInst::Create(Context, Op);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002501 InstructionList.push_back(I);
Dan Gohmanfa1211f2008-07-23 00:34:11 +00002502 break;
Chris Lattnere53603e2007-05-02 04:27:25 +00002503 }
Chris Lattner5285b5e2007-05-02 05:46:45 +00002504 case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#]
Chris Lattner6ce15cb2007-05-03 22:09:51 +00002505 if (Record.size() != 1 && Record.size() != 3)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002506 return Error(InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002507 BasicBlock *TrueDest = getBasicBlock(Record[0]);
2508 if (TrueDest == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002509 return Error(InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002510
Devang Patelaf206b82009-09-18 19:26:43 +00002511 if (Record.size() == 1) {
Gabor Greife9ecc682008-04-06 20:25:17 +00002512 I = BranchInst::Create(TrueDest);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002513 InstructionList.push_back(I);
Devang Patelaf206b82009-09-18 19:26:43 +00002514 }
Chris Lattner5285b5e2007-05-02 05:46:45 +00002515 else {
2516 BasicBlock *FalseDest = getBasicBlock(Record[1]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002517 Value *Cond = getValue(Record, 2, NextValueNo,
2518 Type::getInt1Ty(Context));
Chris Lattner5285b5e2007-05-02 05:46:45 +00002519 if (FalseDest == 0 || Cond == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002520 return Error(InvalidRecord);
Gabor Greife9ecc682008-04-06 20:25:17 +00002521 I = BranchInst::Create(TrueDest, FalseDest, Cond);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002522 InstructionList.push_back(I);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002523 }
2524 break;
2525 }
Chris Lattner3ed871f2009-10-27 19:13:16 +00002526 case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...]
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002527 // Check magic
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002528 if ((Record[0] >> 16) == SWITCH_INST_MAGIC) {
Bob Wilsone4077362013-09-09 19:14:35 +00002529 // "New" SwitchInst format with case ranges. The changes to write this
2530 // format were reverted but we still recognize bitcode that uses it.
2531 // Hopefully someday we will have support for case ranges and can use
2532 // this format again.
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002533
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002534 Type *OpTy = getTypeByID(Record[1]);
2535 unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth();
2536
Jan Wen Voungafaced02012-10-11 20:20:40 +00002537 Value *Cond = getValue(Record, 2, NextValueNo, OpTy);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002538 BasicBlock *Default = getBasicBlock(Record[3]);
2539 if (OpTy == 0 || Cond == 0 || Default == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002540 return Error(InvalidRecord);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002541
2542 unsigned NumCases = Record[4];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002543
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002544 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
2545 InstructionList.push_back(SI);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002546
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002547 unsigned CurIdx = 5;
2548 for (unsigned i = 0; i != NumCases; ++i) {
Bob Wilsone4077362013-09-09 19:14:35 +00002549 SmallVector<ConstantInt*, 1> CaseVals;
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002550 unsigned NumItems = Record[CurIdx++];
2551 for (unsigned ci = 0; ci != NumItems; ++ci) {
2552 bool isSingleNumber = Record[CurIdx++];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002553
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002554 APInt Low;
2555 unsigned ActiveWords = 1;
2556 if (ValueBitWidth > 64)
2557 ActiveWords = Record[CurIdx++];
Benjamin Kramer9704ed02012-05-28 14:10:31 +00002558 Low = ReadWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
2559 ValueBitWidth);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002560 CurIdx += ActiveWords;
Stepan Dyatkovskiye3e19cb2012-05-28 12:39:09 +00002561
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002562 if (!isSingleNumber) {
2563 ActiveWords = 1;
2564 if (ValueBitWidth > 64)
2565 ActiveWords = Record[CurIdx++];
2566 APInt High =
Benjamin Kramer9704ed02012-05-28 14:10:31 +00002567 ReadWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
2568 ValueBitWidth);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002569 CurIdx += ActiveWords;
Bob Wilsone4077362013-09-09 19:14:35 +00002570
2571 // FIXME: It is not clear whether values in the range should be
2572 // compared as signed or unsigned values. The partially
2573 // implemented changes that used this format in the past used
2574 // unsigned comparisons.
2575 for ( ; Low.ule(High); ++Low)
2576 CaseVals.push_back(ConstantInt::get(Context, Low));
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002577 } else
Bob Wilsone4077362013-09-09 19:14:35 +00002578 CaseVals.push_back(ConstantInt::get(Context, Low));
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002579 }
2580 BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]);
Bob Wilsone4077362013-09-09 19:14:35 +00002581 for (SmallVector<ConstantInt*, 1>::iterator cvi = CaseVals.begin(),
2582 cve = CaseVals.end(); cvi != cve; ++cvi)
2583 SI->addCase(*cvi, DestBB);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002584 }
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002585 I = SI;
2586 break;
2587 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002588
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002589 // Old SwitchInst format without case ranges.
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002590
Chris Lattner5285b5e2007-05-02 05:46:45 +00002591 if (Record.size() < 3 || (Record.size() & 1) == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002592 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00002593 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002594 Value *Cond = getValue(Record, 1, NextValueNo, OpTy);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002595 BasicBlock *Default = getBasicBlock(Record[2]);
2596 if (OpTy == 0 || Cond == 0 || Default == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002597 return Error(InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002598 unsigned NumCases = (Record.size()-3)/2;
Gabor Greife9ecc682008-04-06 20:25:17 +00002599 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
Devang Patelaf206b82009-09-18 19:26:43 +00002600 InstructionList.push_back(SI);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002601 for (unsigned i = 0, e = NumCases; i != e; ++i) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002602 ConstantInt *CaseVal =
Chris Lattner5285b5e2007-05-02 05:46:45 +00002603 dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy));
2604 BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]);
2605 if (CaseVal == 0 || DestBB == 0) {
2606 delete SI;
Rafael Espindola48da4f42013-11-04 16:16:24 +00002607 return Error(InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002608 }
2609 SI->addCase(CaseVal, DestBB);
2610 }
2611 I = SI;
2612 break;
2613 }
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00002614 case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...]
Chris Lattner3ed871f2009-10-27 19:13:16 +00002615 if (Record.size() < 2)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002616 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00002617 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002618 Value *Address = getValue(Record, 1, NextValueNo, OpTy);
Chris Lattner3ed871f2009-10-27 19:13:16 +00002619 if (OpTy == 0 || Address == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002620 return Error(InvalidRecord);
Chris Lattner3ed871f2009-10-27 19:13:16 +00002621 unsigned NumDests = Record.size()-2;
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00002622 IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests);
Chris Lattner3ed871f2009-10-27 19:13:16 +00002623 InstructionList.push_back(IBI);
2624 for (unsigned i = 0, e = NumDests; i != e; ++i) {
2625 if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) {
2626 IBI->addDestination(DestBB);
2627 } else {
2628 delete IBI;
Rafael Espindola48da4f42013-11-04 16:16:24 +00002629 return Error(InvalidRecord);
Chris Lattner3ed871f2009-10-27 19:13:16 +00002630 }
2631 }
2632 I = IBI;
2633 break;
2634 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002635
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00002636 case bitc::FUNC_CODE_INST_INVOKE: {
2637 // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
Rafael Espindola48da4f42013-11-04 16:16:24 +00002638 if (Record.size() < 4)
2639 return Error(InvalidRecord);
Bill Wendlinge94d8432012-12-07 23:16:57 +00002640 AttributeSet PAL = getAttributes(Record[0]);
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002641 unsigned CCInfo = Record[1];
2642 BasicBlock *NormalBB = getBasicBlock(Record[2]);
2643 BasicBlock *UnwindBB = getBasicBlock(Record[3]);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002644
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002645 unsigned OpNum = 4;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002646 Value *Callee;
2647 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002648 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002649
Chris Lattner229907c2011-07-18 04:54:35 +00002650 PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType());
2651 FunctionType *FTy = !CalleeTy ? 0 :
Chris Lattner5285b5e2007-05-02 05:46:45 +00002652 dyn_cast<FunctionType>(CalleeTy->getElementType());
2653
2654 // Check that the right number of fixed parameters are here.
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002655 if (FTy == 0 || NormalBB == 0 || UnwindBB == 0 ||
2656 Record.size() < OpNum+FTy->getNumParams())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002657 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002658
Chris Lattner5285b5e2007-05-02 05:46:45 +00002659 SmallVector<Value*, 16> Ops;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002660 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00002661 Ops.push_back(getValue(Record, OpNum, NextValueNo,
2662 FTy->getParamType(i)));
Rafael Espindola48da4f42013-11-04 16:16:24 +00002663 if (Ops.back() == 0)
2664 return Error(InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002665 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002666
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002667 if (!FTy->isVarArg()) {
2668 if (Record.size() != OpNum)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002669 return Error(InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002670 } else {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002671 // Read type/value pairs for varargs params.
2672 while (OpNum != Record.size()) {
2673 Value *Op;
2674 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002675 return Error(InvalidRecord);
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002676 Ops.push_back(Op);
2677 }
Chris Lattner5285b5e2007-05-02 05:46:45 +00002678 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002679
Jay Foad5bd375a2011-07-15 08:37:34 +00002680 I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops);
Devang Patelaf206b82009-09-18 19:26:43 +00002681 InstructionList.push_back(I);
Sandeep Patel68c5f472009-09-02 08:44:58 +00002682 cast<InvokeInst>(I)->setCallingConv(
2683 static_cast<CallingConv::ID>(CCInfo));
Devang Patel4c758ea2008-09-25 21:00:45 +00002684 cast<InvokeInst>(I)->setAttributes(PAL);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002685 break;
2686 }
Bill Wendlingf891bf82011-07-31 06:30:59 +00002687 case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval]
2688 unsigned Idx = 0;
2689 Value *Val = 0;
2690 if (getValueTypePair(Record, Idx, NextValueNo, Val))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002691 return Error(InvalidRecord);
Bill Wendlingf891bf82011-07-31 06:30:59 +00002692 I = ResumeInst::Create(Val);
Bill Wendlingb9a89992011-09-01 00:50:20 +00002693 InstructionList.push_back(I);
Bill Wendlingf891bf82011-07-31 06:30:59 +00002694 break;
2695 }
Chris Lattnere53603e2007-05-02 04:27:25 +00002696 case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE
Owen Anderson55f1c092009-08-13 21:58:54 +00002697 I = new UnreachableInst(Context);
Devang Patelaf206b82009-09-18 19:26:43 +00002698 InstructionList.push_back(I);
Chris Lattnere53603e2007-05-02 04:27:25 +00002699 break;
Chris Lattnere9759c22007-05-06 00:21:25 +00002700 case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...]
Chris Lattnere14cb882007-05-04 19:11:41 +00002701 if (Record.size() < 1 || ((Record.size()-1)&1))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002702 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00002703 Type *Ty = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00002704 if (!Ty)
2705 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002706
Jay Foad52131342011-03-30 11:28:46 +00002707 PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2);
Devang Patelaf206b82009-09-18 19:26:43 +00002708 InstructionList.push_back(PN);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002709
Chris Lattnere14cb882007-05-04 19:11:41 +00002710 for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00002711 Value *V;
2712 // With the new function encoding, it is possible that operands have
2713 // negative IDs (for forward references). Use a signed VBR
2714 // representation to keep the encoding small.
2715 if (UseRelativeIDs)
2716 V = getValueSigned(Record, 1+i, NextValueNo, Ty);
2717 else
2718 V = getValue(Record, 1+i, NextValueNo, Ty);
Chris Lattnere14cb882007-05-04 19:11:41 +00002719 BasicBlock *BB = getBasicBlock(Record[2+i]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00002720 if (!V || !BB)
2721 return Error(InvalidRecord);
Chris Lattnerc332bba2007-05-03 18:58:09 +00002722 PN->addIncoming(V, BB);
2723 }
2724 I = PN;
2725 break;
2726 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002727
Bill Wendlingfae14752011-08-12 20:24:12 +00002728 case bitc::FUNC_CODE_INST_LANDINGPAD: {
2729 // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?]
2730 unsigned Idx = 0;
2731 if (Record.size() < 4)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002732 return Error(InvalidRecord);
Bill Wendlingfae14752011-08-12 20:24:12 +00002733 Type *Ty = getTypeByID(Record[Idx++]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00002734 if (!Ty)
2735 return Error(InvalidRecord);
Bill Wendlingfae14752011-08-12 20:24:12 +00002736 Value *PersFn = 0;
2737 if (getValueTypePair(Record, Idx, NextValueNo, PersFn))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002738 return Error(InvalidRecord);
Bill Wendlingfae14752011-08-12 20:24:12 +00002739
2740 bool IsCleanup = !!Record[Idx++];
2741 unsigned NumClauses = Record[Idx++];
2742 LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, NumClauses);
2743 LP->setCleanup(IsCleanup);
2744 for (unsigned J = 0; J != NumClauses; ++J) {
2745 LandingPadInst::ClauseType CT =
2746 LandingPadInst::ClauseType(Record[Idx++]); (void)CT;
2747 Value *Val;
2748
2749 if (getValueTypePair(Record, Idx, NextValueNo, Val)) {
2750 delete LP;
Rafael Espindola48da4f42013-11-04 16:16:24 +00002751 return Error(InvalidRecord);
Bill Wendlingfae14752011-08-12 20:24:12 +00002752 }
2753
2754 assert((CT != LandingPadInst::Catch ||
2755 !isa<ArrayType>(Val->getType())) &&
2756 "Catch clause has a invalid type!");
2757 assert((CT != LandingPadInst::Filter ||
2758 isa<ArrayType>(Val->getType())) &&
2759 "Filter clause has invalid type!");
2760 LP->addClause(Val);
2761 }
2762
2763 I = LP;
Bill Wendlingb9a89992011-09-01 00:50:20 +00002764 InstructionList.push_back(I);
Bill Wendlingfae14752011-08-12 20:24:12 +00002765 break;
2766 }
2767
Chris Lattnerf1c87102011-06-17 18:09:11 +00002768 case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]
2769 if (Record.size() != 4)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002770 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00002771 PointerType *Ty =
Chris Lattnerc332bba2007-05-03 18:58:09 +00002772 dyn_cast_or_null<PointerType>(getTypeByID(Record[0]));
Chris Lattner229907c2011-07-18 04:54:35 +00002773 Type *OpTy = getTypeByID(Record[1]);
Chris Lattnerf1c87102011-06-17 18:09:11 +00002774 Value *Size = getFnValueByID(Record[2], OpTy);
2775 unsigned Align = Record[3];
Rafael Espindola48da4f42013-11-04 16:16:24 +00002776 if (!Ty || !Size)
2777 return Error(InvalidRecord);
Owen Anderson4fdeba92009-07-15 23:53:25 +00002778 I = new AllocaInst(Ty->getElementType(), Size, (1 << Align) >> 1);
Devang Patelaf206b82009-09-18 19:26:43 +00002779 InstructionList.push_back(I);
Chris Lattnerc332bba2007-05-03 18:58:09 +00002780 break;
2781 }
Chris Lattner9f600c52007-05-03 22:04:19 +00002782 case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol]
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002783 unsigned OpNum = 0;
2784 Value *Op;
2785 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
2786 OpNum+2 != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002787 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002788
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002789 I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1);
Devang Patelaf206b82009-09-18 19:26:43 +00002790 InstructionList.push_back(I);
Chris Lattner83930552007-05-01 07:01:57 +00002791 break;
Chris Lattner9f600c52007-05-03 22:04:19 +00002792 }
Eli Friedman59b66882011-08-09 23:02:53 +00002793 case bitc::FUNC_CODE_INST_LOADATOMIC: {
2794 // LOADATOMIC: [opty, op, align, vol, ordering, synchscope]
2795 unsigned OpNum = 0;
2796 Value *Op;
2797 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
2798 OpNum+4 != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002799 return Error(InvalidRecord);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002800
Eli Friedman59b66882011-08-09 23:02:53 +00002801
2802 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
2803 if (Ordering == NotAtomic || Ordering == Release ||
2804 Ordering == AcquireRelease)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002805 return Error(InvalidRecord);
Eli Friedman59b66882011-08-09 23:02:53 +00002806 if (Ordering != NotAtomic && Record[OpNum] == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002807 return Error(InvalidRecord);
Eli Friedman59b66882011-08-09 23:02:53 +00002808 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
2809
2810 I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1,
2811 Ordering, SynchScope);
2812 InstructionList.push_back(I);
2813 break;
2814 }
Chris Lattnerc44070802011-06-17 18:17:37 +00002815 case bitc::FUNC_CODE_INST_STORE: { // STORE2:[ptrty, ptr, val, align, vol]
Christopher Lamb54dd24c2007-12-11 08:59:05 +00002816 unsigned OpNum = 0;
2817 Value *Val, *Ptr;
2818 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002819 popValue(Record, OpNum, NextValueNo,
Christopher Lamb54dd24c2007-12-11 08:59:05 +00002820 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
2821 OpNum+2 != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002822 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002823
Christopher Lamb54dd24c2007-12-11 08:59:05 +00002824 I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1);
Devang Patelaf206b82009-09-18 19:26:43 +00002825 InstructionList.push_back(I);
Christopher Lamb54dd24c2007-12-11 08:59:05 +00002826 break;
2827 }
Eli Friedman59b66882011-08-09 23:02:53 +00002828 case bitc::FUNC_CODE_INST_STOREATOMIC: {
2829 // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, synchscope]
2830 unsigned OpNum = 0;
2831 Value *Val, *Ptr;
2832 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002833 popValue(Record, OpNum, NextValueNo,
Eli Friedman59b66882011-08-09 23:02:53 +00002834 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
2835 OpNum+4 != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002836 return Error(InvalidRecord);
Eli Friedman59b66882011-08-09 23:02:53 +00002837
2838 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
Eli Friedman222b5a42011-09-19 19:41:28 +00002839 if (Ordering == NotAtomic || Ordering == Acquire ||
Eli Friedman59b66882011-08-09 23:02:53 +00002840 Ordering == AcquireRelease)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002841 return Error(InvalidRecord);
Eli Friedman59b66882011-08-09 23:02:53 +00002842 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
2843 if (Ordering != NotAtomic && Record[OpNum] == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002844 return Error(InvalidRecord);
Eli Friedman59b66882011-08-09 23:02:53 +00002845
2846 I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1,
2847 Ordering, SynchScope);
2848 InstructionList.push_back(I);
2849 break;
2850 }
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002851 case bitc::FUNC_CODE_INST_CMPXCHG: {
2852 // CMPXCHG:[ptrty, ptr, cmp, new, vol, ordering, synchscope]
2853 unsigned OpNum = 0;
2854 Value *Ptr, *Cmp, *New;
2855 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002856 popValue(Record, OpNum, NextValueNo,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002857 cast<PointerType>(Ptr->getType())->getElementType(), Cmp) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002858 popValue(Record, OpNum, NextValueNo,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002859 cast<PointerType>(Ptr->getType())->getElementType(), New) ||
2860 OpNum+3 != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002861 return Error(InvalidRecord);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002862 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+1]);
Eli Friedman59b66882011-08-09 23:02:53 +00002863 if (Ordering == NotAtomic || Ordering == Unordered)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002864 return Error(InvalidRecord);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002865 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+2]);
2866 I = new AtomicCmpXchgInst(Ptr, Cmp, New, Ordering, SynchScope);
2867 cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]);
2868 InstructionList.push_back(I);
2869 break;
2870 }
2871 case bitc::FUNC_CODE_INST_ATOMICRMW: {
2872 // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, synchscope]
2873 unsigned OpNum = 0;
2874 Value *Ptr, *Val;
2875 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002876 popValue(Record, OpNum, NextValueNo,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002877 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
2878 OpNum+4 != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002879 return Error(InvalidRecord);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002880 AtomicRMWInst::BinOp Operation = GetDecodedRMWOperation(Record[OpNum]);
2881 if (Operation < AtomicRMWInst::FIRST_BINOP ||
2882 Operation > AtomicRMWInst::LAST_BINOP)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002883 return Error(InvalidRecord);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002884 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
Eli Friedman59b66882011-08-09 23:02:53 +00002885 if (Ordering == NotAtomic || Ordering == Unordered)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002886 return Error(InvalidRecord);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002887 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
2888 I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SynchScope);
2889 cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]);
2890 InstructionList.push_back(I);
2891 break;
2892 }
Eli Friedmanfee02c62011-07-25 23:16:38 +00002893 case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, synchscope]
2894 if (2 != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002895 return Error(InvalidRecord);
Eli Friedmanfee02c62011-07-25 23:16:38 +00002896 AtomicOrdering Ordering = GetDecodedOrdering(Record[0]);
2897 if (Ordering == NotAtomic || Ordering == Unordered ||
2898 Ordering == Monotonic)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002899 return Error(InvalidRecord);
Eli Friedmanfee02c62011-07-25 23:16:38 +00002900 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[1]);
2901 I = new FenceInst(Context, Ordering, SynchScope);
2902 InstructionList.push_back(I);
2903 break;
2904 }
Chris Lattnerc44070802011-06-17 18:17:37 +00002905 case bitc::FUNC_CODE_INST_CALL: {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00002906 // CALL: [paramattrs, cc, fnty, fnid, arg0, arg1...]
2907 if (Record.size() < 3)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002908 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002909
Bill Wendlinge94d8432012-12-07 23:16:57 +00002910 AttributeSet PAL = getAttributes(Record[0]);
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002911 unsigned CCInfo = Record[1];
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002912
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002913 unsigned OpNum = 2;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002914 Value *Callee;
2915 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002916 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002917
Chris Lattner229907c2011-07-18 04:54:35 +00002918 PointerType *OpTy = dyn_cast<PointerType>(Callee->getType());
2919 FunctionType *FTy = 0;
Chris Lattner9f600c52007-05-03 22:04:19 +00002920 if (OpTy) FTy = dyn_cast<FunctionType>(OpTy->getElementType());
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002921 if (!FTy || Record.size() < FTy->getNumParams()+OpNum)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002922 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002923
Chris Lattner9f600c52007-05-03 22:04:19 +00002924 SmallVector<Value*, 16> Args;
2925 // Read the fixed params.
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002926 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002927 if (FTy->getParamType(i)->isLabelTy())
Dale Johannesen4646aa32007-11-05 21:20:28 +00002928 Args.push_back(getBasicBlock(Record[OpNum]));
Dan Gohmanbbcd04d2010-09-13 18:00:48 +00002929 else
Jan Wen Voungafaced02012-10-11 20:20:40 +00002930 Args.push_back(getValue(Record, OpNum, NextValueNo,
2931 FTy->getParamType(i)));
Rafael Espindola48da4f42013-11-04 16:16:24 +00002932 if (Args.back() == 0)
2933 return Error(InvalidRecord);
Chris Lattner9f600c52007-05-03 22:04:19 +00002934 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002935
Chris Lattner9f600c52007-05-03 22:04:19 +00002936 // Read type/value pairs for varargs params.
Chris Lattner9f600c52007-05-03 22:04:19 +00002937 if (!FTy->isVarArg()) {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002938 if (OpNum != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002939 return Error(InvalidRecord);
Chris Lattner9f600c52007-05-03 22:04:19 +00002940 } else {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002941 while (OpNum != Record.size()) {
2942 Value *Op;
2943 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002944 return Error(InvalidRecord);
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002945 Args.push_back(Op);
Chris Lattner9f600c52007-05-03 22:04:19 +00002946 }
2947 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002948
Jay Foad5bd375a2011-07-15 08:37:34 +00002949 I = CallInst::Create(Callee, Args);
Devang Patelaf206b82009-09-18 19:26:43 +00002950 InstructionList.push_back(I);
Sandeep Patel68c5f472009-09-02 08:44:58 +00002951 cast<CallInst>(I)->setCallingConv(
2952 static_cast<CallingConv::ID>(CCInfo>>1));
Chris Lattner47045272007-05-03 22:34:03 +00002953 cast<CallInst>(I)->setTailCall(CCInfo & 1);
Devang Patel4c758ea2008-09-25 21:00:45 +00002954 cast<CallInst>(I)->setAttributes(PAL);
Chris Lattner9f600c52007-05-03 22:04:19 +00002955 break;
2956 }
2957 case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty]
2958 if (Record.size() < 3)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002959 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00002960 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002961 Value *Op = getValue(Record, 1, NextValueNo, OpTy);
Chris Lattner229907c2011-07-18 04:54:35 +00002962 Type *ResTy = getTypeByID(Record[2]);
Chris Lattner9f600c52007-05-03 22:04:19 +00002963 if (!OpTy || !Op || !ResTy)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002964 return Error(InvalidRecord);
Chris Lattner9f600c52007-05-03 22:04:19 +00002965 I = new VAArgInst(Op, ResTy);
Devang Patelaf206b82009-09-18 19:26:43 +00002966 InstructionList.push_back(I);
Chris Lattner9f600c52007-05-03 22:04:19 +00002967 break;
2968 }
Chris Lattner83930552007-05-01 07:01:57 +00002969 }
2970
2971 // Add instruction to end of current BB. If there is no current BB, reject
2972 // this file.
2973 if (CurBB == 0) {
2974 delete I;
Rafael Espindola48da4f42013-11-04 16:16:24 +00002975 return Error(InvalidInstructionWithNoBB);
Chris Lattner83930552007-05-01 07:01:57 +00002976 }
2977 CurBB->getInstList().push_back(I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002978
Chris Lattner83930552007-05-01 07:01:57 +00002979 // If this was a terminator instruction, move to the next block.
2980 if (isa<TerminatorInst>(I)) {
2981 ++CurBBNo;
2982 CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : 0;
2983 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002984
Chris Lattner83930552007-05-01 07:01:57 +00002985 // Non-void values get registered in the value table for future use.
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00002986 if (I && !I->getType()->isVoidTy())
Chris Lattner83930552007-05-01 07:01:57 +00002987 ValueList.AssignValue(I, NextValueNo++);
Chris Lattner85b7b402007-05-01 05:52:21 +00002988 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002989
Chris Lattner27d38752013-01-20 02:13:19 +00002990OutOfRecordLoop:
Joe Abbey97b7a172013-02-06 22:14:06 +00002991
Chris Lattner83930552007-05-01 07:01:57 +00002992 // Check the function list for unresolved values.
2993 if (Argument *A = dyn_cast<Argument>(ValueList.back())) {
2994 if (A->getParent() == 0) {
2995 // We found at least one unresolved value. Nuke them all to avoid leaks.
2996 for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){
Dan Gohman950ad652010-08-25 20:20:21 +00002997 if ((A = dyn_cast<Argument>(ValueList[i])) && A->getParent() == 0) {
Owen Andersonb292b8c2009-07-30 23:03:37 +00002998 A->replaceAllUsesWith(UndefValue::get(A->getType()));
Chris Lattner83930552007-05-01 07:01:57 +00002999 delete A;
3000 }
3001 }
Rafael Espindola48da4f42013-11-04 16:16:24 +00003002 return Error(NeverResolvedValueFoundInFunction);
Chris Lattner83930552007-05-01 07:01:57 +00003003 }
Chris Lattner83930552007-05-01 07:01:57 +00003004 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003005
Dan Gohman9b9ff462010-08-25 20:23:38 +00003006 // FIXME: Check for unresolved forward-declared metadata references
3007 // and clean up leaks.
3008
Chris Lattner5956dc82009-10-28 05:53:48 +00003009 // See if anything took the address of blocks in this function. If so,
3010 // resolve them now.
Chris Lattner5956dc82009-10-28 05:53:48 +00003011 DenseMap<Function*, std::vector<BlockAddrRefTy> >::iterator BAFRI =
3012 BlockAddrFwdRefs.find(F);
3013 if (BAFRI != BlockAddrFwdRefs.end()) {
3014 std::vector<BlockAddrRefTy> &RefList = BAFRI->second;
3015 for (unsigned i = 0, e = RefList.size(); i != e; ++i) {
3016 unsigned BlockIdx = RefList[i].first;
Chris Lattneraa99c942009-11-01 01:27:45 +00003017 if (BlockIdx >= FunctionBBs.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00003018 return Error(InvalidID);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003019
Chris Lattner5956dc82009-10-28 05:53:48 +00003020 GlobalVariable *FwdRef = RefList[i].second;
Chris Lattneraa99c942009-11-01 01:27:45 +00003021 FwdRef->replaceAllUsesWith(BlockAddress::get(F, FunctionBBs[BlockIdx]));
Chris Lattner5956dc82009-10-28 05:53:48 +00003022 FwdRef->eraseFromParent();
3023 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003024
Chris Lattner5956dc82009-10-28 05:53:48 +00003025 BlockAddrFwdRefs.erase(BAFRI);
3026 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003027
Chris Lattner85b7b402007-05-01 05:52:21 +00003028 // Trim the value list down to the size it was before we parsed this function.
3029 ValueList.shrinkTo(ModuleValueListSize);
Dan Gohman26d837d2010-08-25 20:22:53 +00003030 MDValueList.shrinkTo(ModuleMDValueListSize);
Chris Lattner85b7b402007-05-01 05:52:21 +00003031 std::vector<BasicBlock*>().swap(FunctionBBs);
Rafael Espindola48da4f42013-11-04 16:16:24 +00003032 return error_code::success();
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003033}
3034
Rafael Espindola7d712032013-11-05 17:16:08 +00003035/// Find the function body in the bitcode stream
3036error_code BitcodeReader::FindFunctionInStream(Function *F,
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003037 DenseMap<Function*, uint64_t>::iterator DeferredFunctionInfoIterator) {
3038 while (DeferredFunctionInfoIterator->second == 0) {
3039 if (Stream.AtEndOfStream())
Rafael Espindola48da4f42013-11-04 16:16:24 +00003040 return Error(CouldNotFindFunctionInStream);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003041 // ParseModule will parse the next body in the stream and set its
3042 // position in the DeferredFunctionInfo map.
Rafael Espindola7d712032013-11-05 17:16:08 +00003043 if (error_code EC = ParseModule(true))
3044 return EC;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003045 }
Rafael Espindola7d712032013-11-05 17:16:08 +00003046 return error_code::success();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003047}
3048
Chris Lattner9eeada92007-05-18 04:02:46 +00003049//===----------------------------------------------------------------------===//
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003050// GVMaterializer implementation
Chris Lattner9eeada92007-05-18 04:02:46 +00003051//===----------------------------------------------------------------------===//
3052
3053
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003054bool BitcodeReader::isMaterializable(const GlobalValue *GV) const {
3055 if (const Function *F = dyn_cast<Function>(GV)) {
3056 return F->isDeclaration() &&
3057 DeferredFunctionInfo.count(const_cast<Function*>(F));
3058 }
3059 return false;
3060}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003061
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003062error_code BitcodeReader::Materialize(GlobalValue *GV) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003063 Function *F = dyn_cast<Function>(GV);
3064 // If it's not a function or is already material, ignore the request.
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003065 if (!F || !F->isMaterializable())
3066 return error_code::success();
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003067
3068 DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F);
Chris Lattner9eeada92007-05-18 04:02:46 +00003069 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003070 // If its position is recorded as 0, its body is somewhere in the stream
3071 // but we haven't seen it yet.
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003072 if (DFII->second == 0 && LazyStreamer)
3073 if (error_code EC = FindFunctionInStream(F, DFII))
3074 return EC;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003075
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003076 // Move the bit stream to the saved position of the deferred function body.
3077 Stream.JumpToBit(DFII->second);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003078
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003079 if (error_code EC = ParseFunctionBody(F))
3080 return EC;
Chandler Carruth7132e002007-08-04 01:51:18 +00003081
3082 // Upgrade any old intrinsic calls in the function.
3083 for (UpgradedIntrinsicMap::iterator I = UpgradedIntrinsics.begin(),
3084 E = UpgradedIntrinsics.end(); I != E; ++I) {
3085 if (I->first != I->second) {
3086 for (Value::use_iterator UI = I->first->use_begin(),
3087 UE = I->first->use_end(); UI != UE; ) {
3088 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
3089 UpgradeIntrinsicCall(CI, I->second);
3090 }
3091 }
3092 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003093
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003094 return error_code::success();
Chris Lattner9eeada92007-05-18 04:02:46 +00003095}
3096
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003097bool BitcodeReader::isDematerializable(const GlobalValue *GV) const {
3098 const Function *F = dyn_cast<Function>(GV);
3099 if (!F || F->isDeclaration())
3100 return false;
3101 return DeferredFunctionInfo.count(const_cast<Function*>(F));
3102}
3103
3104void BitcodeReader::Dematerialize(GlobalValue *GV) {
3105 Function *F = dyn_cast<Function>(GV);
3106 // If this function isn't dematerializable, this is a noop.
3107 if (!F || !isDematerializable(F))
Chris Lattner9eeada92007-05-18 04:02:46 +00003108 return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003109
Chris Lattner9eeada92007-05-18 04:02:46 +00003110 assert(DeferredFunctionInfo.count(F) && "No info to read function later?");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003111
Chris Lattner9eeada92007-05-18 04:02:46 +00003112 // Just forget the function body, we can remat it later.
3113 F->deleteBody();
Chris Lattner9eeada92007-05-18 04:02:46 +00003114}
3115
3116
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003117error_code BitcodeReader::MaterializeModule(Module *M) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003118 assert(M == TheModule &&
3119 "Can only Materialize the Module this BitcodeReader is attached to.");
Chris Lattner06310bf2009-06-16 05:15:21 +00003120 // Iterate over the module, deserializing any functions that are still on
3121 // disk.
3122 for (Module::iterator F = TheModule->begin(), E = TheModule->end();
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003123 F != E; ++F) {
3124 if (F->isMaterializable()) {
3125 if (error_code EC = Materialize(F))
3126 return EC;
3127 }
3128 }
Derek Schuff92ef9752012-02-29 00:07:09 +00003129 // At this point, if there are any function bodies, the current bit is
3130 // pointing to the END_BLOCK record after them. Now make sure the rest
3131 // of the bits in the module have been read.
3132 if (NextUnreadBit)
3133 ParseModule(true);
3134
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003135 // Upgrade any intrinsic calls that slipped through (should not happen!) and
3136 // delete the old functions to clean up. We can't do this unless the entire
3137 // module is materialized because there could always be another function body
Chandler Carruth7132e002007-08-04 01:51:18 +00003138 // with calls to the old function.
3139 for (std::vector<std::pair<Function*, Function*> >::iterator I =
3140 UpgradedIntrinsics.begin(), E = UpgradedIntrinsics.end(); I != E; ++I) {
3141 if (I->first != I->second) {
3142 for (Value::use_iterator UI = I->first->use_begin(),
3143 UE = I->first->use_end(); UI != UE; ) {
3144 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
3145 UpgradeIntrinsicCall(CI, I->second);
3146 }
Chris Lattner647cffb2009-04-01 01:43:03 +00003147 if (!I->first->use_empty())
3148 I->first->replaceAllUsesWith(I->second);
Chandler Carruth7132e002007-08-04 01:51:18 +00003149 I->first->eraseFromParent();
3150 }
3151 }
3152 std::vector<std::pair<Function*, Function*> >().swap(UpgradedIntrinsics);
Devang Patel80ae3492009-08-28 23:24:31 +00003153
Manman Ren209b17c2013-09-28 00:22:27 +00003154 for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++)
3155 UpgradeInstWithTBAATag(InstsWithTBAATag[I]);
3156
Manman Ren8b4306c2013-12-02 21:29:56 +00003157 UpgradeDebugInfo(*M);
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003158 return error_code::success();
Chris Lattner9eeada92007-05-18 04:02:46 +00003159}
3160
Rafael Espindola48da4f42013-11-04 16:16:24 +00003161error_code BitcodeReader::InitStream() {
3162 if (LazyStreamer)
3163 return InitLazyStream();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003164 return InitStreamFromBuffer();
3165}
3166
Rafael Espindola48da4f42013-11-04 16:16:24 +00003167error_code BitcodeReader::InitStreamFromBuffer() {
Roman Divacky4717a8d2012-09-06 15:42:13 +00003168 const unsigned char *BufPtr = (const unsigned char*)Buffer->getBufferStart();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003169 const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
3170
3171 if (Buffer->getBufferSize() & 3) {
3172 if (!isRawBitcode(BufPtr, BufEnd) && !isBitcodeWrapper(BufPtr, BufEnd))
Rafael Espindola48da4f42013-11-04 16:16:24 +00003173 return Error(InvalidBitcodeSignature);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003174 else
Rafael Espindola48da4f42013-11-04 16:16:24 +00003175 return Error(BitcodeStreamInvalidSize);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003176 }
3177
3178 // If we have a wrapper header, parse it and ignore the non-bc file contents.
3179 // The magic number is 0x0B17C0DE stored in little endian.
3180 if (isBitcodeWrapper(BufPtr, BufEnd))
3181 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
Rafael Espindola48da4f42013-11-04 16:16:24 +00003182 return Error(InvalidBitcodeWrapperHeader);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003183
3184 StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
3185 Stream.init(*StreamFile);
3186
Rafael Espindola48da4f42013-11-04 16:16:24 +00003187 return error_code::success();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003188}
3189
Rafael Espindola48da4f42013-11-04 16:16:24 +00003190error_code BitcodeReader::InitLazyStream() {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003191 // Check and strip off the bitcode wrapper; BitstreamReader expects never to
3192 // see it.
3193 StreamingMemoryObject *Bytes = new StreamingMemoryObject(LazyStreamer);
3194 StreamFile.reset(new BitstreamReader(Bytes));
3195 Stream.init(*StreamFile);
3196
3197 unsigned char buf[16];
Benjamin Kramer534d3a42013-05-24 10:54:58 +00003198 if (Bytes->readBytes(0, 16, buf) == -1)
Rafael Espindola48da4f42013-11-04 16:16:24 +00003199 return Error(BitcodeStreamInvalidSize);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003200
3201 if (!isBitcode(buf, buf + 16))
Rafael Espindola48da4f42013-11-04 16:16:24 +00003202 return Error(InvalidBitcodeSignature);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003203
3204 if (isBitcodeWrapper(buf, buf + 4)) {
3205 const unsigned char *bitcodeStart = buf;
3206 const unsigned char *bitcodeEnd = buf + 16;
3207 SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
3208 Bytes->dropLeadingBytes(bitcodeStart - buf);
3209 Bytes->setKnownObjectSize(bitcodeEnd - bitcodeStart);
3210 }
Rafael Espindola48da4f42013-11-04 16:16:24 +00003211 return error_code::success();
3212}
3213
3214namespace {
3215class BitcodeErrorCategoryType : public _do_message {
3216 const char *name() const LLVM_OVERRIDE {
3217 return "llvm.bitcode";
3218 }
3219 std::string message(int IE) const LLVM_OVERRIDE {
3220 BitcodeReader::ErrorType E = static_cast<BitcodeReader::ErrorType>(IE);
3221 switch (E) {
3222 case BitcodeReader::BitcodeStreamInvalidSize:
3223 return "Bitcode stream length should be >= 16 bytes and a multiple of 4";
3224 case BitcodeReader::ConflictingMETADATA_KINDRecords:
3225 return "Conflicting METADATA_KIND records";
3226 case BitcodeReader::CouldNotFindFunctionInStream:
3227 return "Could not find function in stream";
3228 case BitcodeReader::ExpectedConstant:
3229 return "Expected a constant";
3230 case BitcodeReader::InsufficientFunctionProtos:
3231 return "Insufficient function protos";
3232 case BitcodeReader::InvalidBitcodeSignature:
3233 return "Invalid bitcode signature";
3234 case BitcodeReader::InvalidBitcodeWrapperHeader:
3235 return "Invalid bitcode wrapper header";
3236 case BitcodeReader::InvalidConstantReference:
3237 return "Invalid ronstant reference";
3238 case BitcodeReader::InvalidID:
3239 return "Invalid ID";
3240 case BitcodeReader::InvalidInstructionWithNoBB:
3241 return "Invalid instruction with no BB";
3242 case BitcodeReader::InvalidRecord:
3243 return "Invalid record";
3244 case BitcodeReader::InvalidTypeForValue:
3245 return "Invalid type for value";
3246 case BitcodeReader::InvalidTYPETable:
3247 return "Invalid TYPE table";
3248 case BitcodeReader::InvalidType:
3249 return "Invalid type";
3250 case BitcodeReader::MalformedBlock:
3251 return "Malformed block";
3252 case BitcodeReader::MalformedGlobalInitializerSet:
3253 return "Malformed global initializer set";
3254 case BitcodeReader::InvalidMultipleBlocks:
3255 return "Invalid multiple blocks";
3256 case BitcodeReader::NeverResolvedValueFoundInFunction:
3257 return "Never resolved value found in function";
3258 case BitcodeReader::InvalidValue:
3259 return "Invalid value";
3260 }
Benjamin Kramer77db1632013-11-05 13:45:09 +00003261 llvm_unreachable("Unknown error type!");
Rafael Espindola48da4f42013-11-04 16:16:24 +00003262 }
3263};
3264}
3265
3266const error_category &BitcodeReader::BitcodeErrorCategory() {
3267 static BitcodeErrorCategoryType O;
3268 return O;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003269}
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003270
Chris Lattner6694f602007-04-29 07:54:31 +00003271//===----------------------------------------------------------------------===//
3272// External interface
3273//===----------------------------------------------------------------------===//
3274
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003275/// getLazyBitcodeModule - lazy function-at-a-time loading from a file.
Chris Lattner6694f602007-04-29 07:54:31 +00003276///
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003277Module *llvm::getLazyBitcodeModule(MemoryBuffer *Buffer,
3278 LLVMContext& Context,
3279 std::string *ErrMsg) {
3280 Module *M = new Module(Buffer->getBufferIdentifier(), Context);
Owen Anderson6773d382009-07-01 16:58:40 +00003281 BitcodeReader *R = new BitcodeReader(Buffer, Context);
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003282 M->setMaterializer(R);
Rafael Espindola48da4f42013-11-04 16:16:24 +00003283 if (error_code EC = R->ParseBitcodeInto(M)) {
Chris Lattner6694f602007-04-29 07:54:31 +00003284 if (ErrMsg)
Rafael Espindola48da4f42013-11-04 16:16:24 +00003285 *ErrMsg = EC.message();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003286
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003287 delete M; // Also deletes R.
Chris Lattner6694f602007-04-29 07:54:31 +00003288 return 0;
3289 }
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003290 // Have the BitcodeReader dtor delete 'Buffer'.
3291 R->setBufferOwned(true);
Rafael Espindolab7993462012-01-02 07:49:53 +00003292
3293 R->materializeForwardReferencedFunctions();
3294
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003295 return M;
Chris Lattner6694f602007-04-29 07:54:31 +00003296}
3297
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003298
3299Module *llvm::getStreamedBitcodeModule(const std::string &name,
3300 DataStreamer *streamer,
3301 LLVMContext &Context,
3302 std::string *ErrMsg) {
3303 Module *M = new Module(name, Context);
3304 BitcodeReader *R = new BitcodeReader(streamer, Context);
3305 M->setMaterializer(R);
Rafael Espindola48da4f42013-11-04 16:16:24 +00003306 if (error_code EC = R->ParseBitcodeInto(M)) {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003307 if (ErrMsg)
Rafael Espindola48da4f42013-11-04 16:16:24 +00003308 *ErrMsg = EC.message();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003309 delete M; // Also deletes R.
3310 return 0;
3311 }
3312 R->setBufferOwned(false); // no buffer to delete
3313 return M;
3314}
3315
Chris Lattner6694f602007-04-29 07:54:31 +00003316/// ParseBitcodeFile - Read the specified bitcode file, returning the module.
3317/// If an error occurs, return null and fill in *ErrMsg if non-null.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003318Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
Owen Anderson6773d382009-07-01 16:58:40 +00003319 std::string *ErrMsg){
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003320 Module *M = getLazyBitcodeModule(Buffer, Context, ErrMsg);
3321 if (!M) return 0;
Chris Lattner9eeada92007-05-18 04:02:46 +00003322
3323 // Don't let the BitcodeReader dtor delete 'Buffer', regardless of whether
3324 // there was an error.
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003325 static_cast<BitcodeReader*>(M->getMaterializer())->setBufferOwned(false);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003326
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003327 // Read in the entire module, and destroy the BitcodeReader.
3328 if (M->MaterializeAllPermanently(ErrMsg)) {
3329 delete M;
Bill Wendling0198ce02010-10-06 01:22:42 +00003330 return 0;
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003331 }
Bill Wendling0198ce02010-10-06 01:22:42 +00003332
Chad Rosierca2567b2011-12-07 21:44:12 +00003333 // TODO: Restore the use-lists to the in-memory state when the bitcode was
3334 // written. We must defer until the Module has been fully materialized.
3335
Chris Lattner6694f602007-04-29 07:54:31 +00003336 return M;
3337}
Bill Wendling0198ce02010-10-06 01:22:42 +00003338
3339std::string llvm::getBitcodeTargetTriple(MemoryBuffer *Buffer,
3340 LLVMContext& Context,
3341 std::string *ErrMsg) {
3342 BitcodeReader *R = new BitcodeReader(Buffer, Context);
3343 // Don't let the BitcodeReader dtor delete 'Buffer'.
3344 R->setBufferOwned(false);
3345
3346 std::string Triple("");
Rafael Espindola48da4f42013-11-04 16:16:24 +00003347 if (error_code EC = R->ParseTriple(Triple))
Bill Wendling0198ce02010-10-06 01:22:42 +00003348 if (ErrMsg)
Rafael Espindola48da4f42013-11-04 16:16:24 +00003349 *ErrMsg = EC.message();
Bill Wendling0198ce02010-10-06 01:22:42 +00003350
3351 delete R;
3352 return Triple;
3353}