blob: eb716660eb4bd8f3dc0982512df1219001edfdf0 [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"
Tobias Grosser0a8e12f2013-07-26 04:16:55 +000014#include "llvm/Bitcode/LLVMBitCodes.h"
Chandler Carruth91065212014-03-05 10:34:14 +000015#include "llvm/IR/AutoUpgrade.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;
Nico Rieck7157bb72014-01-14 15:22:47 +000083 case 5: return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage
84 case 6: return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +000085 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
Nico Rieck7157bb72014-01-14 15:22:47 +0000105static GlobalValue::DLLStorageClassTypes
106GetDecodedDLLStorageClass(unsigned Val) {
107 switch (Val) {
108 default: // Map unknown values to default.
109 case 0: return GlobalValue::DefaultStorageClass;
110 case 1: return GlobalValue::DLLImportStorageClass;
111 case 2: return GlobalValue::DLLExportStorageClass;
112 }
113}
114
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000115static GlobalVariable::ThreadLocalMode GetDecodedThreadLocalMode(unsigned Val) {
116 switch (Val) {
117 case 0: return GlobalVariable::NotThreadLocal;
118 default: // Map unknown non-zero value to general dynamic.
119 case 1: return GlobalVariable::GeneralDynamicTLSModel;
120 case 2: return GlobalVariable::LocalDynamicTLSModel;
121 case 3: return GlobalVariable::InitialExecTLSModel;
122 case 4: return GlobalVariable::LocalExecTLSModel;
123 }
124}
125
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000126static int GetDecodedCastOpcode(unsigned Val) {
127 switch (Val) {
128 default: return -1;
129 case bitc::CAST_TRUNC : return Instruction::Trunc;
130 case bitc::CAST_ZEXT : return Instruction::ZExt;
131 case bitc::CAST_SEXT : return Instruction::SExt;
132 case bitc::CAST_FPTOUI : return Instruction::FPToUI;
133 case bitc::CAST_FPTOSI : return Instruction::FPToSI;
134 case bitc::CAST_UITOFP : return Instruction::UIToFP;
135 case bitc::CAST_SITOFP : return Instruction::SIToFP;
136 case bitc::CAST_FPTRUNC : return Instruction::FPTrunc;
137 case bitc::CAST_FPEXT : return Instruction::FPExt;
138 case bitc::CAST_PTRTOINT: return Instruction::PtrToInt;
139 case bitc::CAST_INTTOPTR: return Instruction::IntToPtr;
140 case bitc::CAST_BITCAST : return Instruction::BitCast;
Matt Arsenault3aa9b032013-11-18 02:51:33 +0000141 case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000142 }
143}
Chris Lattner229907c2011-07-18 04:54:35 +0000144static int GetDecodedBinaryOpcode(unsigned Val, Type *Ty) {
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000145 switch (Val) {
146 default: return -1;
Dan Gohmana5b96452009-06-04 22:49:04 +0000147 case bitc::BINOP_ADD:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000148 return Ty->isFPOrFPVectorTy() ? Instruction::FAdd : Instruction::Add;
Dan Gohmana5b96452009-06-04 22:49:04 +0000149 case bitc::BINOP_SUB:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000150 return Ty->isFPOrFPVectorTy() ? Instruction::FSub : Instruction::Sub;
Dan Gohmana5b96452009-06-04 22:49:04 +0000151 case bitc::BINOP_MUL:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000152 return Ty->isFPOrFPVectorTy() ? Instruction::FMul : Instruction::Mul;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000153 case bitc::BINOP_UDIV: return Instruction::UDiv;
154 case bitc::BINOP_SDIV:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000155 return Ty->isFPOrFPVectorTy() ? Instruction::FDiv : Instruction::SDiv;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000156 case bitc::BINOP_UREM: return Instruction::URem;
157 case bitc::BINOP_SREM:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000158 return Ty->isFPOrFPVectorTy() ? Instruction::FRem : Instruction::SRem;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000159 case bitc::BINOP_SHL: return Instruction::Shl;
160 case bitc::BINOP_LSHR: return Instruction::LShr;
161 case bitc::BINOP_ASHR: return Instruction::AShr;
162 case bitc::BINOP_AND: return Instruction::And;
163 case bitc::BINOP_OR: return Instruction::Or;
164 case bitc::BINOP_XOR: return Instruction::Xor;
165 }
166}
167
Eli Friedmanc9a551e2011-07-28 21:48:00 +0000168static AtomicRMWInst::BinOp GetDecodedRMWOperation(unsigned Val) {
169 switch (Val) {
170 default: return AtomicRMWInst::BAD_BINOP;
171 case bitc::RMW_XCHG: return AtomicRMWInst::Xchg;
172 case bitc::RMW_ADD: return AtomicRMWInst::Add;
173 case bitc::RMW_SUB: return AtomicRMWInst::Sub;
174 case bitc::RMW_AND: return AtomicRMWInst::And;
175 case bitc::RMW_NAND: return AtomicRMWInst::Nand;
176 case bitc::RMW_OR: return AtomicRMWInst::Or;
177 case bitc::RMW_XOR: return AtomicRMWInst::Xor;
178 case bitc::RMW_MAX: return AtomicRMWInst::Max;
179 case bitc::RMW_MIN: return AtomicRMWInst::Min;
180 case bitc::RMW_UMAX: return AtomicRMWInst::UMax;
181 case bitc::RMW_UMIN: return AtomicRMWInst::UMin;
182 }
183}
184
Eli Friedmanfee02c62011-07-25 23:16:38 +0000185static AtomicOrdering GetDecodedOrdering(unsigned Val) {
186 switch (Val) {
187 case bitc::ORDERING_NOTATOMIC: return NotAtomic;
188 case bitc::ORDERING_UNORDERED: return Unordered;
189 case bitc::ORDERING_MONOTONIC: return Monotonic;
190 case bitc::ORDERING_ACQUIRE: return Acquire;
191 case bitc::ORDERING_RELEASE: return Release;
192 case bitc::ORDERING_ACQREL: return AcquireRelease;
193 default: // Map unknown orderings to sequentially-consistent.
194 case bitc::ORDERING_SEQCST: return SequentiallyConsistent;
195 }
196}
197
198static SynchronizationScope GetDecodedSynchScope(unsigned Val) {
199 switch (Val) {
200 case bitc::SYNCHSCOPE_SINGLETHREAD: return SingleThread;
201 default: // Map unknown scopes to cross-thread.
202 case bitc::SYNCHSCOPE_CROSSTHREAD: return CrossThread;
203 }
204}
205
Nico Rieck7157bb72014-01-14 15:22:47 +0000206static void UpgradeDLLImportExportLinkage(llvm::GlobalValue *GV, unsigned Val) {
207 switch (Val) {
208 case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break;
209 case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break;
210 }
211}
212
Gabor Greiff6caff662008-05-10 08:32:32 +0000213namespace llvm {
Chris Lattner1663cca2007-04-24 05:48:56 +0000214namespace {
215 /// @brief A class for maintaining the slot number definition
216 /// as a placeholder for the actual definition for forward constants defs.
217 class ConstantPlaceHolder : public ConstantExpr {
Craig Toppera60c0f12012-09-15 17:09:36 +0000218 void operator=(const ConstantPlaceHolder &) LLVM_DELETED_FUNCTION;
Gabor Greife9ecc682008-04-06 20:25:17 +0000219 public:
220 // allocate space for exactly one operand
221 void *operator new(size_t s) {
222 return User::operator new(s, 1);
223 }
Chris Lattner229907c2011-07-18 04:54:35 +0000224 explicit ConstantPlaceHolder(Type *Ty, LLVMContext& Context)
Gabor Greiff6caff662008-05-10 08:32:32 +0000225 : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000226 Op<0>() = UndefValue::get(Type::getInt32Ty(Context));
Chris Lattner1663cca2007-04-24 05:48:56 +0000227 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000228
Chris Lattner74429932008-08-21 02:34:16 +0000229 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
Chris Lattner74429932008-08-21 02:34:16 +0000230 static bool classof(const Value *V) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000231 return isa<ConstantExpr>(V) &&
Chris Lattner74429932008-08-21 02:34:16 +0000232 cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1;
233 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000234
235
Gabor Greiff6caff662008-05-10 08:32:32 +0000236 /// Provide fast operand accessors
Chris Lattner2d8cd802009-03-31 22:55:09 +0000237 //DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Chris Lattner1663cca2007-04-24 05:48:56 +0000238 };
239}
240
Chris Lattner2d8cd802009-03-31 22:55:09 +0000241// FIXME: can we inherit this from ConstantExpr?
Gabor Greiff6caff662008-05-10 08:32:32 +0000242template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000243struct OperandTraits<ConstantPlaceHolder> :
244 public FixedNumOperandTraits<ConstantPlaceHolder, 1> {
Gabor Greiff6caff662008-05-10 08:32:32 +0000245};
Gabor Greiff6caff662008-05-10 08:32:32 +0000246}
247
Chris Lattner2d8cd802009-03-31 22:55:09 +0000248
249void BitcodeReaderValueList::AssignValue(Value *V, unsigned Idx) {
250 if (Idx == size()) {
251 push_back(V);
252 return;
253 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000254
Chris Lattner2d8cd802009-03-31 22:55:09 +0000255 if (Idx >= size())
256 resize(Idx+1);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000257
Chris Lattner2d8cd802009-03-31 22:55:09 +0000258 WeakVH &OldV = ValuePtrs[Idx];
259 if (OldV == 0) {
260 OldV = V;
261 return;
262 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000263
Chris Lattner2d8cd802009-03-31 22:55:09 +0000264 // Handle constants and non-constants (e.g. instrs) differently for
265 // efficiency.
266 if (Constant *PHC = dyn_cast<Constant>(&*OldV)) {
267 ResolveConstants.push_back(std::make_pair(PHC, Idx));
268 OldV = V;
269 } else {
270 // If there was a forward reference to this value, replace it.
271 Value *PrevVal = OldV;
272 OldV->replaceAllUsesWith(V);
273 delete PrevVal;
Gabor Greiff6caff662008-05-10 08:32:32 +0000274 }
275}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000276
Gabor Greiff6caff662008-05-10 08:32:32 +0000277
Chris Lattner1663cca2007-04-24 05:48:56 +0000278Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx,
Chris Lattner229907c2011-07-18 04:54:35 +0000279 Type *Ty) {
Chris Lattner2d8cd802009-03-31 22:55:09 +0000280 if (Idx >= size())
Gabor Greiff6caff662008-05-10 08:32:32 +0000281 resize(Idx + 1);
Chris Lattner1663cca2007-04-24 05:48:56 +0000282
Chris Lattner2d8cd802009-03-31 22:55:09 +0000283 if (Value *V = ValuePtrs[Idx]) {
Chris Lattner83930552007-05-01 07:01:57 +0000284 assert(Ty == V->getType() && "Type mismatch in constant table!");
285 return cast<Constant>(V);
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000286 }
Chris Lattner1663cca2007-04-24 05:48:56 +0000287
288 // Create and return a placeholder, which will later be RAUW'd.
Owen Andersone9f98042009-07-07 20:18:58 +0000289 Constant *C = new ConstantPlaceHolder(Ty, Context);
Chris Lattner2d8cd802009-03-31 22:55:09 +0000290 ValuePtrs[Idx] = C;
Chris Lattner1663cca2007-04-24 05:48:56 +0000291 return C;
292}
293
Chris Lattner229907c2011-07-18 04:54:35 +0000294Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, Type *Ty) {
Chris Lattner2d8cd802009-03-31 22:55:09 +0000295 if (Idx >= size())
Gabor Greiff6caff662008-05-10 08:32:32 +0000296 resize(Idx + 1);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000297
Chris Lattner2d8cd802009-03-31 22:55:09 +0000298 if (Value *V = ValuePtrs[Idx]) {
Chris Lattner83930552007-05-01 07:01:57 +0000299 assert((Ty == 0 || Ty == V->getType()) && "Type mismatch in value table!");
300 return V;
301 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000302
Chris Lattner1fc27f02007-05-02 05:16:49 +0000303 // No type specified, must be invalid reference.
304 if (Ty == 0) return 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000305
Chris Lattner83930552007-05-01 07:01:57 +0000306 // Create and return a placeholder, which will later be RAUW'd.
307 Value *V = new Argument(Ty);
Chris Lattner2d8cd802009-03-31 22:55:09 +0000308 ValuePtrs[Idx] = V;
Chris Lattner83930552007-05-01 07:01:57 +0000309 return V;
310}
311
Chris Lattner74429932008-08-21 02:34:16 +0000312/// ResolveConstantForwardRefs - Once all constants are read, this method bulk
313/// resolves any forward references. The idea behind this is that we sometimes
314/// get constants (such as large arrays) which reference *many* forward ref
315/// constants. Replacing each of these causes a lot of thrashing when
316/// building/reuniquing the constant. Instead of doing this, we look at all the
317/// uses and rewrite all the place holders at once for any constant that uses
318/// a placeholder.
319void BitcodeReaderValueList::ResolveConstantForwardRefs() {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000320 // Sort the values by-pointer so that they are efficient to look up with a
Chris Lattner74429932008-08-21 02:34:16 +0000321 // binary search.
322 std::sort(ResolveConstants.begin(), ResolveConstants.end());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000323
Chris Lattner74429932008-08-21 02:34:16 +0000324 SmallVector<Constant*, 64> NewOps;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000325
Chris Lattner74429932008-08-21 02:34:16 +0000326 while (!ResolveConstants.empty()) {
Chris Lattner2d8cd802009-03-31 22:55:09 +0000327 Value *RealVal = operator[](ResolveConstants.back().second);
Chris Lattner74429932008-08-21 02:34:16 +0000328 Constant *Placeholder = ResolveConstants.back().first;
329 ResolveConstants.pop_back();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000330
Chris Lattner74429932008-08-21 02:34:16 +0000331 // Loop over all users of the placeholder, updating them to reference the
332 // new value. If they reference more than one placeholder, update them all
333 // at once.
334 while (!Placeholder->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000335 auto UI = Placeholder->user_begin();
Gabor Greif2c0ab482010-07-09 16:01:21 +0000336 User *U = *UI;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000337
Chris Lattner74429932008-08-21 02:34:16 +0000338 // If the using object isn't uniqued, just update the operands. This
339 // handles instructions and initializers for global variables.
Gabor Greif2c0ab482010-07-09 16:01:21 +0000340 if (!isa<Constant>(U) || isa<GlobalValue>(U)) {
Chris Lattner479c5d92008-08-21 17:31:45 +0000341 UI.getUse().set(RealVal);
Chris Lattner74429932008-08-21 02:34:16 +0000342 continue;
343 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000344
Chris Lattner74429932008-08-21 02:34:16 +0000345 // Otherwise, we have a constant that uses the placeholder. Replace that
346 // constant with a new constant that has *all* placeholder uses updated.
Gabor Greif2c0ab482010-07-09 16:01:21 +0000347 Constant *UserC = cast<Constant>(U);
Chris Lattner74429932008-08-21 02:34:16 +0000348 for (User::op_iterator I = UserC->op_begin(), E = UserC->op_end();
349 I != E; ++I) {
350 Value *NewOp;
351 if (!isa<ConstantPlaceHolder>(*I)) {
352 // Not a placeholder reference.
353 NewOp = *I;
354 } else if (*I == Placeholder) {
355 // Common case is that it just references this one placeholder.
356 NewOp = RealVal;
357 } else {
358 // Otherwise, look up the placeholder in ResolveConstants.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000359 ResolveConstantsTy::iterator It =
360 std::lower_bound(ResolveConstants.begin(), ResolveConstants.end(),
Chris Lattner74429932008-08-21 02:34:16 +0000361 std::pair<Constant*, unsigned>(cast<Constant>(*I),
362 0));
363 assert(It != ResolveConstants.end() && It->first == *I);
Chris Lattner2d8cd802009-03-31 22:55:09 +0000364 NewOp = operator[](It->second);
Chris Lattner74429932008-08-21 02:34:16 +0000365 }
366
367 NewOps.push_back(cast<Constant>(NewOp));
368 }
369
370 // Make the new constant.
371 Constant *NewC;
372 if (ConstantArray *UserCA = dyn_cast<ConstantArray>(UserC)) {
Jay Foad83be3612011-06-22 09:24:39 +0000373 NewC = ConstantArray::get(UserCA->getType(), NewOps);
Chris Lattner74429932008-08-21 02:34:16 +0000374 } else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) {
Chris Lattnercc19efa2011-06-20 04:01:31 +0000375 NewC = ConstantStruct::get(UserCS->getType(), NewOps);
Chris Lattner74429932008-08-21 02:34:16 +0000376 } else if (isa<ConstantVector>(UserC)) {
Chris Lattner69229312011-02-15 00:14:00 +0000377 NewC = ConstantVector::get(NewOps);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000378 } else {
379 assert(isa<ConstantExpr>(UserC) && "Must be a ConstantExpr.");
Jay Foad5c984e562011-04-13 13:46:01 +0000380 NewC = cast<ConstantExpr>(UserC)->getWithOperands(NewOps);
Chris Lattner74429932008-08-21 02:34:16 +0000381 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000382
Chris Lattner74429932008-08-21 02:34:16 +0000383 UserC->replaceAllUsesWith(NewC);
384 UserC->destroyConstant();
385 NewOps.clear();
386 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000387
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000388 // Update all ValueHandles, they should be the only users at this point.
389 Placeholder->replaceAllUsesWith(RealVal);
Chris Lattner74429932008-08-21 02:34:16 +0000390 delete Placeholder;
391 }
392}
393
Devang Patel05eb6172009-08-04 06:00:18 +0000394void BitcodeReaderMDValueList::AssignValue(Value *V, unsigned Idx) {
395 if (Idx == size()) {
396 push_back(V);
397 return;
398 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000399
Devang Patel05eb6172009-08-04 06:00:18 +0000400 if (Idx >= size())
401 resize(Idx+1);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000402
Devang Patel05eb6172009-08-04 06:00:18 +0000403 WeakVH &OldV = MDValuePtrs[Idx];
404 if (OldV == 0) {
405 OldV = V;
406 return;
407 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000408
Devang Patel05eb6172009-08-04 06:00:18 +0000409 // If there was a forward reference to this value, replace it.
Dan Gohman16a5d982010-08-20 22:02:26 +0000410 MDNode *PrevVal = cast<MDNode>(OldV);
Devang Patel05eb6172009-08-04 06:00:18 +0000411 OldV->replaceAllUsesWith(V);
Dan Gohman16a5d982010-08-20 22:02:26 +0000412 MDNode::deleteTemporary(PrevVal);
Devang Patel116b4a02009-09-03 01:38:02 +0000413 // Deleting PrevVal sets Idx value in MDValuePtrs to null. Set new
414 // value for Idx.
415 MDValuePtrs[Idx] = V;
Devang Patel05eb6172009-08-04 06:00:18 +0000416}
417
418Value *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) {
419 if (Idx >= size())
420 resize(Idx + 1);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000421
Devang Patel05eb6172009-08-04 06:00:18 +0000422 if (Value *V = MDValuePtrs[Idx]) {
Chris Lattnerfdd87902009-10-05 05:54:46 +0000423 assert(V->getType()->isMetadataTy() && "Type mismatch in value table!");
Devang Patel05eb6172009-08-04 06:00:18 +0000424 return V;
425 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000426
Devang Patel05eb6172009-08-04 06:00:18 +0000427 // Create and return a placeholder, which will later be RAUW'd.
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000428 Value *V = MDNode::getTemporary(Context, None);
Devang Patel05eb6172009-08-04 06:00:18 +0000429 MDValuePtrs[Idx] = V;
430 return V;
431}
Chris Lattner1314b992007-04-22 06:23:29 +0000432
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000433Type *BitcodeReader::getTypeByID(unsigned ID) {
434 // The type table size is always specified correctly.
435 if (ID >= TypeList.size())
436 return 0;
Derek Schuff206dddd2012-02-06 19:03:04 +0000437
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000438 if (Type *Ty = TypeList[ID])
439 return Ty;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000440
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000441 // If we have a forward reference, the only possible case is when it is to a
442 // named struct. Just create a placeholder for now.
Chris Lattner335d3992011-08-12 18:06:37 +0000443 return TypeList[ID] = StructType::create(Context);
Chris Lattner1314b992007-04-22 06:23:29 +0000444}
445
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000446
Chris Lattnerfee5a372007-05-04 03:30:17 +0000447//===----------------------------------------------------------------------===//
448// Functions for parsing blocks from the bitcode file
449//===----------------------------------------------------------------------===//
450
Bill Wendling56aeccc2013-02-04 23:32:23 +0000451
452/// \brief This fills an AttrBuilder object with the LLVM attributes that have
453/// been decoded from the given integer. This function must stay in sync with
454/// 'encodeLLVMAttributesForBitcode'.
455static void decodeLLVMAttributesForBitcode(AttrBuilder &B,
456 uint64_t EncodedAttrs) {
457 // FIXME: Remove in 4.0.
458
459 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
460 // the bits above 31 down by 11 bits.
461 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
462 assert((!Alignment || isPowerOf2_32(Alignment)) &&
463 "Alignment must be a power of two.");
464
465 if (Alignment)
466 B.addAlignmentAttr(Alignment);
Kostya Serebryanyd688bab2013-02-11 08:13:54 +0000467 B.addRawValue(((EncodedAttrs & (0xfffffULL << 32)) >> 11) |
Bill Wendling56aeccc2013-02-04 23:32:23 +0000468 (EncodedAttrs & 0xffff));
469}
470
Rafael Espindola48da4f42013-11-04 16:16:24 +0000471error_code BitcodeReader::ParseAttributeBlock() {
Chris Lattner982ec1e2007-05-05 00:17:00 +0000472 if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +0000473 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000474
Devang Patela05633e2008-09-26 22:53:05 +0000475 if (!MAttributes.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +0000476 return Error(InvalidMultipleBlocks);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000477
Chris Lattnerfee5a372007-05-04 03:30:17 +0000478 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000479
Bill Wendling71173cb2013-01-27 00:36:48 +0000480 SmallVector<AttributeSet, 8> Attrs;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000481
Chris Lattnerfee5a372007-05-04 03:30:17 +0000482 // Read all the records.
483 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +0000484 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +0000485
Chris Lattner27d38752013-01-20 02:13:19 +0000486 switch (Entry.Kind) {
487 case BitstreamEntry::SubBlock: // Handled for us already.
488 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +0000489 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +0000490 case BitstreamEntry::EndBlock:
Rafael Espindola48da4f42013-11-04 16:16:24 +0000491 return error_code::success();
Chris Lattner27d38752013-01-20 02:13:19 +0000492 case BitstreamEntry::Record:
493 // The interesting case.
494 break;
Chris Lattnerfee5a372007-05-04 03:30:17 +0000495 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000496
Chris Lattnerfee5a372007-05-04 03:30:17 +0000497 // Read a record.
498 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +0000499 switch (Stream.readRecord(Entry.ID, Record)) {
Chris Lattnerfee5a372007-05-04 03:30:17 +0000500 default: // Default behavior: ignore.
501 break;
Bill Wendling56aeccc2013-02-04 23:32:23 +0000502 case bitc::PARAMATTR_CODE_ENTRY_OLD: { // ENTRY: [paramidx0, attr0, ...]
503 // FIXME: Remove in 4.0.
Chris Lattnerfee5a372007-05-04 03:30:17 +0000504 if (Record.size() & 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000505 return Error(InvalidRecord);
Chris Lattnerfee5a372007-05-04 03:30:17 +0000506
Chris Lattnerfee5a372007-05-04 03:30:17 +0000507 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
Bill Wendling60011b82013-01-29 01:43:29 +0000508 AttrBuilder B;
Bill Wendling56aeccc2013-02-04 23:32:23 +0000509 decodeLLVMAttributesForBitcode(B, Record[i+1]);
Bill Wendling60011b82013-01-29 01:43:29 +0000510 Attrs.push_back(AttributeSet::get(Context, Record[i], B));
Devang Patela05633e2008-09-26 22:53:05 +0000511 }
Devang Patela05633e2008-09-26 22:53:05 +0000512
Bill Wendlinge94d8432012-12-07 23:16:57 +0000513 MAttributes.push_back(AttributeSet::get(Context, Attrs));
Chris Lattnerfee5a372007-05-04 03:30:17 +0000514 Attrs.clear();
515 break;
516 }
Bill Wendling0dc08912013-02-12 08:13:50 +0000517 case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [attrgrp0, attrgrp1, ...]
518 for (unsigned i = 0, e = Record.size(); i != e; ++i)
519 Attrs.push_back(MAttributeGroups[Record[i]]);
520
521 MAttributes.push_back(AttributeSet::get(Context, Attrs));
522 Attrs.clear();
523 break;
524 }
Duncan Sands04eb67e2007-11-20 14:09:29 +0000525 }
Chris Lattnerfee5a372007-05-04 03:30:17 +0000526 }
527}
528
Reid Klecknere9f36af2013-11-12 01:31:00 +0000529// Returns Attribute::None on unrecognized codes.
530static Attribute::AttrKind GetAttrFromCode(uint64_t Code) {
531 switch (Code) {
532 default:
533 return Attribute::None;
534 case bitc::ATTR_KIND_ALIGNMENT:
535 return Attribute::Alignment;
536 case bitc::ATTR_KIND_ALWAYS_INLINE:
537 return Attribute::AlwaysInline;
538 case bitc::ATTR_KIND_BUILTIN:
539 return Attribute::Builtin;
540 case bitc::ATTR_KIND_BY_VAL:
541 return Attribute::ByVal;
Reid Klecknera534a382013-12-19 02:14:12 +0000542 case bitc::ATTR_KIND_IN_ALLOCA:
543 return Attribute::InAlloca;
Reid Klecknere9f36af2013-11-12 01:31:00 +0000544 case bitc::ATTR_KIND_COLD:
545 return Attribute::Cold;
546 case bitc::ATTR_KIND_INLINE_HINT:
547 return Attribute::InlineHint;
548 case bitc::ATTR_KIND_IN_REG:
549 return Attribute::InReg;
550 case bitc::ATTR_KIND_MIN_SIZE:
551 return Attribute::MinSize;
552 case bitc::ATTR_KIND_NAKED:
553 return Attribute::Naked;
554 case bitc::ATTR_KIND_NEST:
555 return Attribute::Nest;
556 case bitc::ATTR_KIND_NO_ALIAS:
557 return Attribute::NoAlias;
558 case bitc::ATTR_KIND_NO_BUILTIN:
559 return Attribute::NoBuiltin;
560 case bitc::ATTR_KIND_NO_CAPTURE:
561 return Attribute::NoCapture;
562 case bitc::ATTR_KIND_NO_DUPLICATE:
563 return Attribute::NoDuplicate;
564 case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT:
565 return Attribute::NoImplicitFloat;
566 case bitc::ATTR_KIND_NO_INLINE:
567 return Attribute::NoInline;
568 case bitc::ATTR_KIND_NON_LAZY_BIND:
569 return Attribute::NonLazyBind;
570 case bitc::ATTR_KIND_NO_RED_ZONE:
571 return Attribute::NoRedZone;
572 case bitc::ATTR_KIND_NO_RETURN:
573 return Attribute::NoReturn;
574 case bitc::ATTR_KIND_NO_UNWIND:
575 return Attribute::NoUnwind;
576 case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE:
577 return Attribute::OptimizeForSize;
578 case bitc::ATTR_KIND_OPTIMIZE_NONE:
579 return Attribute::OptimizeNone;
580 case bitc::ATTR_KIND_READ_NONE:
581 return Attribute::ReadNone;
582 case bitc::ATTR_KIND_READ_ONLY:
583 return Attribute::ReadOnly;
584 case bitc::ATTR_KIND_RETURNED:
585 return Attribute::Returned;
586 case bitc::ATTR_KIND_RETURNS_TWICE:
587 return Attribute::ReturnsTwice;
588 case bitc::ATTR_KIND_S_EXT:
589 return Attribute::SExt;
590 case bitc::ATTR_KIND_STACK_ALIGNMENT:
591 return Attribute::StackAlignment;
592 case bitc::ATTR_KIND_STACK_PROTECT:
593 return Attribute::StackProtect;
594 case bitc::ATTR_KIND_STACK_PROTECT_REQ:
595 return Attribute::StackProtectReq;
596 case bitc::ATTR_KIND_STACK_PROTECT_STRONG:
597 return Attribute::StackProtectStrong;
598 case bitc::ATTR_KIND_STRUCT_RET:
599 return Attribute::StructRet;
600 case bitc::ATTR_KIND_SANITIZE_ADDRESS:
601 return Attribute::SanitizeAddress;
602 case bitc::ATTR_KIND_SANITIZE_THREAD:
603 return Attribute::SanitizeThread;
604 case bitc::ATTR_KIND_SANITIZE_MEMORY:
605 return Attribute::SanitizeMemory;
606 case bitc::ATTR_KIND_UW_TABLE:
607 return Attribute::UWTable;
608 case bitc::ATTR_KIND_Z_EXT:
609 return Attribute::ZExt;
610 }
611}
612
Rafael Espindola48da4f42013-11-04 16:16:24 +0000613error_code BitcodeReader::ParseAttrKind(uint64_t Code,
614 Attribute::AttrKind *Kind) {
Reid Klecknere9f36af2013-11-12 01:31:00 +0000615 *Kind = GetAttrFromCode(Code);
616 if (*Kind == Attribute::None)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000617 return Error(InvalidValue);
Reid Klecknere9f36af2013-11-12 01:31:00 +0000618 return error_code::success();
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000619}
620
Rafael Espindola48da4f42013-11-04 16:16:24 +0000621error_code BitcodeReader::ParseAttributeGroupBlock() {
Bill Wendlingba629332013-02-10 23:24:25 +0000622 if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +0000623 return Error(InvalidRecord);
Bill Wendlingba629332013-02-10 23:24:25 +0000624
625 if (!MAttributeGroups.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +0000626 return Error(InvalidMultipleBlocks);
Bill Wendlingba629332013-02-10 23:24:25 +0000627
628 SmallVector<uint64_t, 64> Record;
629
630 // Read all the records.
631 while (1) {
632 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
633
634 switch (Entry.Kind) {
635 case BitstreamEntry::SubBlock: // Handled for us already.
636 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +0000637 return Error(MalformedBlock);
Bill Wendlingba629332013-02-10 23:24:25 +0000638 case BitstreamEntry::EndBlock:
Rafael Espindola48da4f42013-11-04 16:16:24 +0000639 return error_code::success();
Bill Wendlingba629332013-02-10 23:24:25 +0000640 case BitstreamEntry::Record:
641 // The interesting case.
642 break;
643 }
644
645 // Read a record.
646 Record.clear();
647 switch (Stream.readRecord(Entry.ID, Record)) {
648 default: // Default behavior: ignore.
649 break;
650 case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...]
651 if (Record.size() < 3)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000652 return Error(InvalidRecord);
Bill Wendlingba629332013-02-10 23:24:25 +0000653
Bill Wendlinge46707e2013-02-11 22:32:29 +0000654 uint64_t GrpID = Record[0];
Bill Wendlingba629332013-02-10 23:24:25 +0000655 uint64_t Idx = Record[1]; // Index of the object this attribute refers to.
656
657 AttrBuilder B;
658 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
659 if (Record[i] == 0) { // Enum attribute
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000660 Attribute::AttrKind Kind;
Rafael Espindola48da4f42013-11-04 16:16:24 +0000661 if (error_code EC = ParseAttrKind(Record[++i], &Kind))
662 return EC;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000663
664 B.addAttribute(Kind);
Bill Wendlingba629332013-02-10 23:24:25 +0000665 } else if (Record[i] == 1) { // Align attribute
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000666 Attribute::AttrKind Kind;
Rafael Espindola48da4f42013-11-04 16:16:24 +0000667 if (error_code EC = ParseAttrKind(Record[++i], &Kind))
668 return EC;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000669 if (Kind == Attribute::Alignment)
Bill Wendlingba629332013-02-10 23:24:25 +0000670 B.addAlignmentAttr(Record[++i]);
671 else
672 B.addStackAlignmentAttr(Record[++i]);
673 } else { // String attribute
Bill Wendlinge46707e2013-02-11 22:32:29 +0000674 assert((Record[i] == 3 || Record[i] == 4) &&
675 "Invalid attribute group entry");
Bill Wendlingba629332013-02-10 23:24:25 +0000676 bool HasValue = (Record[i++] == 4);
677 SmallString<64> KindStr;
678 SmallString<64> ValStr;
679
680 while (Record[i] != 0 && i != e)
681 KindStr += Record[i++];
Bill Wendlinge46707e2013-02-11 22:32:29 +0000682 assert(Record[i] == 0 && "Kind string not null terminated");
Bill Wendlingba629332013-02-10 23:24:25 +0000683
684 if (HasValue) {
685 // Has a value associated with it.
Bill Wendlinge46707e2013-02-11 22:32:29 +0000686 ++i; // Skip the '0' that terminates the "kind" string.
Bill Wendlingba629332013-02-10 23:24:25 +0000687 while (Record[i] != 0 && i != e)
688 ValStr += Record[i++];
Bill Wendlinge46707e2013-02-11 22:32:29 +0000689 assert(Record[i] == 0 && "Value string not null terminated");
Bill Wendlingba629332013-02-10 23:24:25 +0000690 }
691
692 B.addAttribute(KindStr.str(), ValStr.str());
693 }
694 }
695
Bill Wendlinge46707e2013-02-11 22:32:29 +0000696 MAttributeGroups[GrpID] = AttributeSet::get(Context, Idx, B);
Bill Wendlingba629332013-02-10 23:24:25 +0000697 break;
698 }
699 }
700 }
701}
702
Rafael Espindola48da4f42013-11-04 16:16:24 +0000703error_code BitcodeReader::ParseTypeTable() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000704 if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW))
Rafael Espindola48da4f42013-11-04 16:16:24 +0000705 return Error(InvalidRecord);
Derek Schuff206dddd2012-02-06 19:03:04 +0000706
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000707 return ParseTypeTableBody();
708}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000709
Rafael Espindola48da4f42013-11-04 16:16:24 +0000710error_code BitcodeReader::ParseTypeTableBody() {
Chris Lattner1314b992007-04-22 06:23:29 +0000711 if (!TypeList.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +0000712 return Error(InvalidMultipleBlocks);
Chris Lattner1314b992007-04-22 06:23:29 +0000713
714 SmallVector<uint64_t, 64> Record;
715 unsigned NumRecords = 0;
716
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000717 SmallString<64> TypeName;
Derek Schuff206dddd2012-02-06 19:03:04 +0000718
Chris Lattner1314b992007-04-22 06:23:29 +0000719 // Read all the records for this type table.
720 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +0000721 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +0000722
Chris Lattner27d38752013-01-20 02:13:19 +0000723 switch (Entry.Kind) {
724 case BitstreamEntry::SubBlock: // Handled for us already.
725 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +0000726 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +0000727 case BitstreamEntry::EndBlock:
Chris Lattner1314b992007-04-22 06:23:29 +0000728 if (NumRecords != TypeList.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +0000729 return Error(MalformedBlock);
730 return error_code::success();
Chris Lattner27d38752013-01-20 02:13:19 +0000731 case BitstreamEntry::Record:
732 // The interesting case.
733 break;
Chris Lattner1314b992007-04-22 06:23:29 +0000734 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000735
Chris Lattner1314b992007-04-22 06:23:29 +0000736 // Read a record.
737 Record.clear();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000738 Type *ResultTy = 0;
Chris Lattner27d38752013-01-20 02:13:19 +0000739 switch (Stream.readRecord(Entry.ID, Record)) {
Rafael Espindola48da4f42013-11-04 16:16:24 +0000740 default:
741 return Error(InvalidValue);
Chris Lattner1314b992007-04-22 06:23:29 +0000742 case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
743 // TYPE_CODE_NUMENTRY contains a count of the number of types in the
744 // type list. This allows us to reserve space.
745 if (Record.size() < 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000746 return Error(InvalidRecord);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000747 TypeList.resize(Record[0]);
Chris Lattner1314b992007-04-22 06:23:29 +0000748 continue;
Chris Lattner1314b992007-04-22 06:23:29 +0000749 case bitc::TYPE_CODE_VOID: // VOID
Owen Anderson55f1c092009-08-13 21:58:54 +0000750 ResultTy = Type::getVoidTy(Context);
Chris Lattner1314b992007-04-22 06:23:29 +0000751 break;
Dan Gohman518cda42011-12-17 00:04:22 +0000752 case bitc::TYPE_CODE_HALF: // HALF
753 ResultTy = Type::getHalfTy(Context);
754 break;
Chris Lattner1314b992007-04-22 06:23:29 +0000755 case bitc::TYPE_CODE_FLOAT: // FLOAT
Owen Anderson55f1c092009-08-13 21:58:54 +0000756 ResultTy = Type::getFloatTy(Context);
Chris Lattner1314b992007-04-22 06:23:29 +0000757 break;
758 case bitc::TYPE_CODE_DOUBLE: // DOUBLE
Owen Anderson55f1c092009-08-13 21:58:54 +0000759 ResultTy = Type::getDoubleTy(Context);
Chris Lattner1314b992007-04-22 06:23:29 +0000760 break;
Dale Johannesenff4c3be2007-08-03 01:03:46 +0000761 case bitc::TYPE_CODE_X86_FP80: // X86_FP80
Owen Anderson55f1c092009-08-13 21:58:54 +0000762 ResultTy = Type::getX86_FP80Ty(Context);
Dale Johannesenff4c3be2007-08-03 01:03:46 +0000763 break;
764 case bitc::TYPE_CODE_FP128: // FP128
Owen Anderson55f1c092009-08-13 21:58:54 +0000765 ResultTy = Type::getFP128Ty(Context);
Dale Johannesenff4c3be2007-08-03 01:03:46 +0000766 break;
767 case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128
Owen Anderson55f1c092009-08-13 21:58:54 +0000768 ResultTy = Type::getPPC_FP128Ty(Context);
Dale Johannesenff4c3be2007-08-03 01:03:46 +0000769 break;
Chris Lattner1314b992007-04-22 06:23:29 +0000770 case bitc::TYPE_CODE_LABEL: // LABEL
Owen Anderson55f1c092009-08-13 21:58:54 +0000771 ResultTy = Type::getLabelTy(Context);
Chris Lattner1314b992007-04-22 06:23:29 +0000772 break;
Nick Lewyckyadbc2842009-05-30 05:06:04 +0000773 case bitc::TYPE_CODE_METADATA: // METADATA
Owen Anderson55f1c092009-08-13 21:58:54 +0000774 ResultTy = Type::getMetadataTy(Context);
Nick Lewyckyadbc2842009-05-30 05:06:04 +0000775 break;
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000776 case bitc::TYPE_CODE_X86_MMX: // X86_MMX
777 ResultTy = Type::getX86_MMXTy(Context);
778 break;
Chris Lattner1314b992007-04-22 06:23:29 +0000779 case bitc::TYPE_CODE_INTEGER: // INTEGER: [width]
780 if (Record.size() < 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000781 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000782
Owen Anderson55f1c092009-08-13 21:58:54 +0000783 ResultTy = IntegerType::get(Context, Record[0]);
Chris Lattner1314b992007-04-22 06:23:29 +0000784 break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000785 case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000786 // [pointee type, address space]
Chris Lattner1314b992007-04-22 06:23:29 +0000787 if (Record.size() < 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000788 return Error(InvalidRecord);
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000789 unsigned AddressSpace = 0;
790 if (Record.size() == 2)
791 AddressSpace = Record[1];
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000792 ResultTy = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +0000793 if (ResultTy == 0)
794 return Error(InvalidType);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000795 ResultTy = PointerType::get(ResultTy, AddressSpace);
Chris Lattner1314b992007-04-22 06:23:29 +0000796 break;
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000797 }
Nuno Lopes561dae02012-05-23 15:19:39 +0000798 case bitc::TYPE_CODE_FUNCTION_OLD: {
799 // FIXME: attrid is dead, remove it in LLVM 4.0
800 // FUNCTION: [vararg, attrid, retty, paramty x N]
801 if (Record.size() < 3)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000802 return Error(InvalidRecord);
Nuno Lopes561dae02012-05-23 15:19:39 +0000803 SmallVector<Type*, 8> ArgTys;
804 for (unsigned i = 3, e = Record.size(); i != e; ++i) {
805 if (Type *T = getTypeByID(Record[i]))
806 ArgTys.push_back(T);
807 else
808 break;
809 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000810
Nuno Lopes561dae02012-05-23 15:19:39 +0000811 ResultTy = getTypeByID(Record[2]);
812 if (ResultTy == 0 || ArgTys.size() < Record.size()-3)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000813 return Error(InvalidType);
Nuno Lopes561dae02012-05-23 15:19:39 +0000814
815 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
816 break;
817 }
Chad Rosier95898722011-11-03 00:14:01 +0000818 case bitc::TYPE_CODE_FUNCTION: {
819 // FUNCTION: [vararg, retty, paramty x N]
820 if (Record.size() < 2)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000821 return Error(InvalidRecord);
Chris Lattnercc3aaf12012-01-27 03:15:49 +0000822 SmallVector<Type*, 8> ArgTys;
Chad Rosier95898722011-11-03 00:14:01 +0000823 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
824 if (Type *T = getTypeByID(Record[i]))
825 ArgTys.push_back(T);
826 else
827 break;
828 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000829
Chad Rosier95898722011-11-03 00:14:01 +0000830 ResultTy = getTypeByID(Record[1]);
831 if (ResultTy == 0 || ArgTys.size() < Record.size()-2)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000832 return Error(InvalidType);
Chad Rosier95898722011-11-03 00:14:01 +0000833
834 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
835 break;
836 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000837 case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N]
Chris Lattner3c5616e2007-05-06 08:21:50 +0000838 if (Record.size() < 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000839 return Error(InvalidRecord);
Chris Lattnercc3aaf12012-01-27 03:15:49 +0000840 SmallVector<Type*, 8> EltTys;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000841 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
842 if (Type *T = getTypeByID(Record[i]))
843 EltTys.push_back(T);
844 else
845 break;
846 }
847 if (EltTys.size() != Record.size()-1)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000848 return Error(InvalidType);
Owen Anderson03cb69f2009-08-05 23:16:16 +0000849 ResultTy = StructType::get(Context, EltTys, Record[0]);
Chris Lattner1314b992007-04-22 06:23:29 +0000850 break;
851 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000852 case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N]
853 if (ConvertToString(Record, 0, TypeName))
Rafael Espindola48da4f42013-11-04 16:16:24 +0000854 return Error(InvalidRecord);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000855 continue;
856
857 case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N]
858 if (Record.size() < 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000859 return Error(InvalidRecord);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000860
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000861 if (NumRecords >= TypeList.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +0000862 return Error(InvalidTYPETable);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000863
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000864 // Check to see if this was forward referenced, if so fill in the temp.
865 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
866 if (Res) {
867 Res->setName(TypeName);
868 TypeList[NumRecords] = 0;
869 } else // Otherwise, create a new struct.
Chris Lattner335d3992011-08-12 18:06:37 +0000870 Res = StructType::create(Context, TypeName);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000871 TypeName.clear();
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000872
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000873 SmallVector<Type*, 8> EltTys;
874 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
875 if (Type *T = getTypeByID(Record[i]))
876 EltTys.push_back(T);
877 else
878 break;
879 }
880 if (EltTys.size() != Record.size()-1)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000881 return Error(InvalidRecord);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000882 Res->setBody(EltTys, Record[0]);
883 ResultTy = Res;
884 break;
885 }
886 case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: []
887 if (Record.size() != 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000888 return Error(InvalidRecord);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000889
890 if (NumRecords >= TypeList.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +0000891 return Error(InvalidTYPETable);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000892
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000893 // Check to see if this was forward referenced, if so fill in the temp.
894 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
895 if (Res) {
896 Res->setName(TypeName);
897 TypeList[NumRecords] = 0;
898 } else // Otherwise, create a new struct with no body.
Chris Lattner335d3992011-08-12 18:06:37 +0000899 Res = StructType::create(Context, TypeName);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000900 TypeName.clear();
901 ResultTy = Res;
902 break;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000903 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000904 case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty]
905 if (Record.size() < 2)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000906 return Error(InvalidRecord);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000907 if ((ResultTy = getTypeByID(Record[1])))
908 ResultTy = ArrayType::get(ResultTy, Record[0]);
909 else
Rafael Espindola48da4f42013-11-04 16:16:24 +0000910 return Error(InvalidType);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000911 break;
912 case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty]
913 if (Record.size() < 2)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000914 return Error(InvalidRecord);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000915 if ((ResultTy = getTypeByID(Record[1])))
916 ResultTy = VectorType::get(ResultTy, Record[0]);
917 else
Rafael Espindola48da4f42013-11-04 16:16:24 +0000918 return Error(InvalidType);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000919 break;
920 }
921
922 if (NumRecords >= TypeList.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +0000923 return Error(InvalidTYPETable);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000924 assert(ResultTy && "Didn't read a type?");
925 assert(TypeList[NumRecords] == 0 && "Already read type?");
926 TypeList[NumRecords++] = ResultTy;
927 }
928}
929
Rafael Espindola48da4f42013-11-04 16:16:24 +0000930error_code BitcodeReader::ParseValueSymbolTable() {
Chris Lattner982ec1e2007-05-05 00:17:00 +0000931 if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +0000932 return Error(InvalidRecord);
Chris Lattnerccaa4482007-04-23 21:26:05 +0000933
934 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000935
Chris Lattnerccaa4482007-04-23 21:26:05 +0000936 // Read all the records for this value table.
937 SmallString<128> ValueName;
938 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +0000939 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +0000940
Chris Lattner27d38752013-01-20 02:13:19 +0000941 switch (Entry.Kind) {
942 case BitstreamEntry::SubBlock: // Handled for us already.
943 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +0000944 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +0000945 case BitstreamEntry::EndBlock:
Rafael Espindola48da4f42013-11-04 16:16:24 +0000946 return error_code::success();
Chris Lattner27d38752013-01-20 02:13:19 +0000947 case BitstreamEntry::Record:
948 // The interesting case.
949 break;
Chris Lattnerccaa4482007-04-23 21:26:05 +0000950 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000951
Chris Lattnerccaa4482007-04-23 21:26:05 +0000952 // Read a record.
953 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +0000954 switch (Stream.readRecord(Entry.ID, Record)) {
Chris Lattnerccaa4482007-04-23 21:26:05 +0000955 default: // Default behavior: unknown type.
956 break;
Chris Lattnere14cb882007-05-04 19:11:41 +0000957 case bitc::VST_CODE_ENTRY: { // VST_ENTRY: [valueid, namechar x N]
Chris Lattnerccaa4482007-04-23 21:26:05 +0000958 if (ConvertToString(Record, 1, ValueName))
Rafael Espindola48da4f42013-11-04 16:16:24 +0000959 return Error(InvalidRecord);
Chris Lattnerccaa4482007-04-23 21:26:05 +0000960 unsigned ValueID = Record[0];
961 if (ValueID >= ValueList.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +0000962 return Error(InvalidRecord);
Chris Lattnerccaa4482007-04-23 21:26:05 +0000963 Value *V = ValueList[ValueID];
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000964
Daniel Dunbard786b512009-07-26 00:34:27 +0000965 V->setName(StringRef(ValueName.data(), ValueName.size()));
Chris Lattnerccaa4482007-04-23 21:26:05 +0000966 ValueName.clear();
967 break;
Reid Spencerdea02bd2007-05-04 01:43:33 +0000968 }
Bill Wendling35a9c3c2011-04-10 23:18:04 +0000969 case bitc::VST_CODE_BBENTRY: {
Chris Lattner6be58c62007-05-03 22:18:21 +0000970 if (ConvertToString(Record, 1, ValueName))
Rafael Espindola48da4f42013-11-04 16:16:24 +0000971 return Error(InvalidRecord);
Chris Lattner6be58c62007-05-03 22:18:21 +0000972 BasicBlock *BB = getBasicBlock(Record[0]);
973 if (BB == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000974 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000975
Daniel Dunbard786b512009-07-26 00:34:27 +0000976 BB->setName(StringRef(ValueName.data(), ValueName.size()));
Chris Lattner6be58c62007-05-03 22:18:21 +0000977 ValueName.clear();
978 break;
Chris Lattnerccaa4482007-04-23 21:26:05 +0000979 }
Reid Spencerdea02bd2007-05-04 01:43:33 +0000980 }
Chris Lattnerccaa4482007-04-23 21:26:05 +0000981 }
982}
983
Rafael Espindola48da4f42013-11-04 16:16:24 +0000984error_code BitcodeReader::ParseMetadata() {
Devang Patel89923232010-01-11 18:52:33 +0000985 unsigned NextMDValueNo = MDValueList.size();
Devang Patel7428d8a2009-07-22 17:43:22 +0000986
987 if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +0000988 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000989
Devang Patel7428d8a2009-07-22 17:43:22 +0000990 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000991
Devang Patel7428d8a2009-07-22 17:43:22 +0000992 // Read all the records.
993 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +0000994 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +0000995
Chris Lattner27d38752013-01-20 02:13:19 +0000996 switch (Entry.Kind) {
997 case BitstreamEntry::SubBlock: // Handled for us already.
998 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +0000999 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00001000 case BitstreamEntry::EndBlock:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001001 return error_code::success();
Chris Lattner27d38752013-01-20 02:13:19 +00001002 case BitstreamEntry::Record:
1003 // The interesting case.
1004 break;
Devang Patel7428d8a2009-07-22 17:43:22 +00001005 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001006
Victor Hernandezb8fd1522010-01-10 07:14:18 +00001007 bool IsFunctionLocal = false;
Devang Patel7428d8a2009-07-22 17:43:22 +00001008 // Read a record.
1009 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +00001010 unsigned Code = Stream.readRecord(Entry.ID, Record);
Dan Gohmanbbcd04d2010-09-13 18:00:48 +00001011 switch (Code) {
Devang Patel7428d8a2009-07-22 17:43:22 +00001012 default: // Default behavior: ignore.
1013 break;
Devang Patel27c87ff2009-07-29 22:34:41 +00001014 case bitc::METADATA_NAME: {
Chris Lattner8d140532013-01-20 02:54:05 +00001015 // Read name of the named metadata.
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001016 SmallString<8> Name(Record.begin(), Record.end());
Devang Patel27c87ff2009-07-29 22:34:41 +00001017 Record.clear();
1018 Code = Stream.ReadCode();
1019
Chris Lattnerb8778552011-06-17 17:50:30 +00001020 // METADATA_NAME is always followed by METADATA_NAMED_NODE.
Chris Lattner27d38752013-01-20 02:13:19 +00001021 unsigned NextBitCode = Stream.readRecord(Code, Record);
Chris Lattnerb8778552011-06-17 17:50:30 +00001022 assert(NextBitCode == bitc::METADATA_NAMED_NODE); (void)NextBitCode;
Devang Patel27c87ff2009-07-29 22:34:41 +00001023
1024 // Read named metadata elements.
1025 unsigned Size = Record.size();
Dan Gohman2637cc12010-07-21 23:38:33 +00001026 NamedMDNode *NMD = TheModule->getOrInsertNamedMetadata(Name);
Devang Patel27c87ff2009-07-29 22:34:41 +00001027 for (unsigned i = 0; i != Size; ++i) {
Chris Lattner003143c2010-01-09 02:02:37 +00001028 MDNode *MD = dyn_cast<MDNode>(MDValueList.getValueFwdRef(Record[i]));
1029 if (MD == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001030 return Error(InvalidRecord);
Dan Gohman2637cc12010-07-21 23:38:33 +00001031 NMD->addOperand(MD);
Devang Patel27c87ff2009-07-29 22:34:41 +00001032 }
Devang Patel27c87ff2009-07-29 22:34:41 +00001033 break;
1034 }
Chris Lattnerb8778552011-06-17 17:50:30 +00001035 case bitc::METADATA_FN_NODE:
Victor Hernandezb8fd1522010-01-10 07:14:18 +00001036 IsFunctionLocal = true;
1037 // fall-through
Chris Lattnerb8778552011-06-17 17:50:30 +00001038 case bitc::METADATA_NODE: {
Dan Gohman1e0213a2010-07-13 19:33:27 +00001039 if (Record.size() % 2 == 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001040 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001041
Devang Patele059ba6e2009-07-23 01:07:34 +00001042 unsigned Size = Record.size();
1043 SmallVector<Value*, 8> Elts;
1044 for (unsigned i = 0; i != Size; i += 2) {
Chris Lattner229907c2011-07-18 04:54:35 +00001045 Type *Ty = getTypeByID(Record[i]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001046 if (!Ty)
1047 return Error(InvalidRecord);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001048 if (Ty->isMetadataTy())
Devang Patel05eb6172009-08-04 06:00:18 +00001049 Elts.push_back(MDValueList.getValueFwdRef(Record[i+1]));
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001050 else if (!Ty->isVoidTy())
Devang Patele059ba6e2009-07-23 01:07:34 +00001051 Elts.push_back(ValueList.getValueFwdRef(Record[i+1], Ty));
1052 else
1053 Elts.push_back(NULL);
1054 }
Jay Foad5514afe2011-04-21 19:59:31 +00001055 Value *V = MDNode::getWhenValsUnresolved(Context, Elts, IsFunctionLocal);
Victor Hernandezb8fd1522010-01-10 07:14:18 +00001056 IsFunctionLocal = false;
Devang Patel89923232010-01-11 18:52:33 +00001057 MDValueList.AssignValue(V, NextMDValueNo++);
Devang Patele059ba6e2009-07-23 01:07:34 +00001058 break;
1059 }
Devang Patel7428d8a2009-07-22 17:43:22 +00001060 case bitc::METADATA_STRING: {
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001061 SmallString<8> String(Record.begin(), Record.end());
1062 Value *V = MDString::get(Context, String);
Devang Patel89923232010-01-11 18:52:33 +00001063 MDValueList.AssignValue(V, NextMDValueNo++);
Devang Patel7428d8a2009-07-22 17:43:22 +00001064 break;
1065 }
Devang Patelaf206b82009-09-18 19:26:43 +00001066 case bitc::METADATA_KIND: {
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001067 if (Record.size() < 2)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001068 return Error(InvalidRecord);
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001069
Devang Patelb1a44772009-09-28 21:14:55 +00001070 unsigned Kind = Record[0];
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001071 SmallString<8> Name(Record.begin()+1, Record.end());
1072
Chris Lattnera0566972009-12-29 09:01:33 +00001073 unsigned NewKind = TheModule->getMDKindID(Name.str());
Dan Gohman43aa8f02010-07-20 21:42:28 +00001074 if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001075 return Error(ConflictingMETADATA_KINDRecords);
Devang Patelaf206b82009-09-18 19:26:43 +00001076 break;
1077 }
Devang Patel7428d8a2009-07-22 17:43:22 +00001078 }
1079 }
1080}
1081
Jan Wen Voungafaced02012-10-11 20:20:40 +00001082/// decodeSignRotatedValue - Decode a signed value stored with the sign bit in
Chris Lattner08feb1e2007-04-24 04:04:35 +00001083/// the LSB for dense VBR encoding.
Jan Wen Voungafaced02012-10-11 20:20:40 +00001084uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) {
Chris Lattner08feb1e2007-04-24 04:04:35 +00001085 if ((V & 1) == 0)
1086 return V >> 1;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001087 if (V != 1)
Chris Lattner08feb1e2007-04-24 04:04:35 +00001088 return -(V >> 1);
1089 // There is no such thing as -0 with integers. "-0" really means MININT.
1090 return 1ULL << 63;
1091}
1092
Chris Lattner44c17072007-04-26 02:46:40 +00001093/// ResolveGlobalAndAliasInits - Resolve all of the initializers for global
1094/// values and aliases that we can.
Rafael Espindola48da4f42013-11-04 16:16:24 +00001095error_code BitcodeReader::ResolveGlobalAndAliasInits() {
Chris Lattner44c17072007-04-26 02:46:40 +00001096 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist;
1097 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist;
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001098 std::vector<std::pair<Function*, unsigned> > FunctionPrefixWorklist;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001099
Chris Lattner44c17072007-04-26 02:46:40 +00001100 GlobalInitWorklist.swap(GlobalInits);
1101 AliasInitWorklist.swap(AliasInits);
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001102 FunctionPrefixWorklist.swap(FunctionPrefixes);
Chris Lattner44c17072007-04-26 02:46:40 +00001103
1104 while (!GlobalInitWorklist.empty()) {
Chris Lattner831d4202007-04-26 03:27:58 +00001105 unsigned ValID = GlobalInitWorklist.back().second;
Chris Lattner44c17072007-04-26 02:46:40 +00001106 if (ValID >= ValueList.size()) {
1107 // Not ready to resolve this yet, it requires something later in the file.
Chris Lattner831d4202007-04-26 03:27:58 +00001108 GlobalInits.push_back(GlobalInitWorklist.back());
Chris Lattner44c17072007-04-26 02:46:40 +00001109 } else {
1110 if (Constant *C = dyn_cast<Constant>(ValueList[ValID]))
1111 GlobalInitWorklist.back().first->setInitializer(C);
1112 else
Rafael Espindola48da4f42013-11-04 16:16:24 +00001113 return Error(ExpectedConstant);
Chris Lattner44c17072007-04-26 02:46:40 +00001114 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001115 GlobalInitWorklist.pop_back();
Chris Lattner44c17072007-04-26 02:46:40 +00001116 }
1117
1118 while (!AliasInitWorklist.empty()) {
1119 unsigned ValID = AliasInitWorklist.back().second;
1120 if (ValID >= ValueList.size()) {
1121 AliasInits.push_back(AliasInitWorklist.back());
1122 } else {
1123 if (Constant *C = dyn_cast<Constant>(ValueList[ValID]))
Anton Korobeynikovdaf358b2007-04-28 14:57:59 +00001124 AliasInitWorklist.back().first->setAliasee(C);
Chris Lattner44c17072007-04-26 02:46:40 +00001125 else
Rafael Espindola48da4f42013-11-04 16:16:24 +00001126 return Error(ExpectedConstant);
Chris Lattner44c17072007-04-26 02:46:40 +00001127 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001128 AliasInitWorklist.pop_back();
Chris Lattner44c17072007-04-26 02:46:40 +00001129 }
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001130
1131 while (!FunctionPrefixWorklist.empty()) {
1132 unsigned ValID = FunctionPrefixWorklist.back().second;
1133 if (ValID >= ValueList.size()) {
1134 FunctionPrefixes.push_back(FunctionPrefixWorklist.back());
1135 } else {
1136 if (Constant *C = dyn_cast<Constant>(ValueList[ValID]))
1137 FunctionPrefixWorklist.back().first->setPrefixData(C);
1138 else
Rafael Espindola48da4f42013-11-04 16:16:24 +00001139 return Error(ExpectedConstant);
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001140 }
1141 FunctionPrefixWorklist.pop_back();
1142 }
1143
Rafael Espindola48da4f42013-11-04 16:16:24 +00001144 return error_code::success();
Chris Lattner44c17072007-04-26 02:46:40 +00001145}
1146
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001147static APInt ReadWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) {
1148 SmallVector<uint64_t, 8> Words(Vals.size());
1149 std::transform(Vals.begin(), Vals.end(), Words.begin(),
Jan Wen Voungafaced02012-10-11 20:20:40 +00001150 BitcodeReader::decodeSignRotatedValue);
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001151
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00001152 return APInt(TypeBits, Words);
1153}
1154
Rafael Espindola48da4f42013-11-04 16:16:24 +00001155error_code BitcodeReader::ParseConstants() {
Chris Lattner982ec1e2007-05-05 00:17:00 +00001156 if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001157 return Error(InvalidRecord);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001158
1159 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001160
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001161 // Read all the records for this value table.
Chris Lattner229907c2011-07-18 04:54:35 +00001162 Type *CurTy = Type::getInt32Ty(Context);
Chris Lattner1663cca2007-04-24 05:48:56 +00001163 unsigned NextCstNo = ValueList.size();
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001164 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00001165 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00001166
Chris Lattner27d38752013-01-20 02:13:19 +00001167 switch (Entry.Kind) {
1168 case BitstreamEntry::SubBlock: // Handled for us already.
1169 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001170 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00001171 case BitstreamEntry::EndBlock:
1172 if (NextCstNo != ValueList.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001173 return Error(InvalidConstantReference);
Joe Abbey97b7a172013-02-06 22:14:06 +00001174
Chris Lattner27d38752013-01-20 02:13:19 +00001175 // Once all the constants have been read, go through and resolve forward
1176 // references.
1177 ValueList.ResolveConstantForwardRefs();
Rafael Espindola48da4f42013-11-04 16:16:24 +00001178 return error_code::success();
Chris Lattner27d38752013-01-20 02:13:19 +00001179 case BitstreamEntry::Record:
1180 // The interesting case.
Chris Lattner74429932008-08-21 02:34:16 +00001181 break;
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001182 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001183
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001184 // Read a record.
1185 Record.clear();
1186 Value *V = 0;
Chris Lattner27d38752013-01-20 02:13:19 +00001187 unsigned BitCode = Stream.readRecord(Entry.ID, Record);
Dan Gohman0ebd6962009-07-20 21:19:07 +00001188 switch (BitCode) {
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001189 default: // Default behavior: unknown constant
1190 case bitc::CST_CODE_UNDEF: // UNDEF
Owen Andersonb292b8c2009-07-30 23:03:37 +00001191 V = UndefValue::get(CurTy);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001192 break;
1193 case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid]
1194 if (Record.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001195 return Error(InvalidRecord);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001196 if (Record[0] >= TypeList.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001197 return Error(InvalidRecord);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001198 CurTy = TypeList[Record[0]];
Chris Lattner08feb1e2007-04-24 04:04:35 +00001199 continue; // Skip the ValueList manipulation.
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001200 case bitc::CST_CODE_NULL: // NULL
Owen Anderson5a1acd92009-07-31 20:28:14 +00001201 V = Constant::getNullValue(CurTy);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001202 break;
1203 case bitc::CST_CODE_INTEGER: // INTEGER: [intval]
Duncan Sands19d0b472010-02-16 11:11:14 +00001204 if (!CurTy->isIntegerTy() || Record.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001205 return Error(InvalidRecord);
Jan Wen Voungafaced02012-10-11 20:20:40 +00001206 V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0]));
Chris Lattner08feb1e2007-04-24 04:04:35 +00001207 break;
Chris Lattnere14cb882007-05-04 19:11:41 +00001208 case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
Duncan Sands19d0b472010-02-16 11:11:14 +00001209 if (!CurTy->isIntegerTy() || Record.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001210 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001211
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001212 APInt VInt = ReadWideAPInt(Record,
1213 cast<IntegerType>(CurTy)->getBitWidth());
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00001214 V = ConstantInt::get(Context, VInt);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001215
Chris Lattner08feb1e2007-04-24 04:04:35 +00001216 break;
1217 }
Dale Johannesen245dceb2007-09-11 18:32:33 +00001218 case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval]
Chris Lattner08feb1e2007-04-24 04:04:35 +00001219 if (Record.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001220 return Error(InvalidRecord);
Dan Gohman518cda42011-12-17 00:04:22 +00001221 if (CurTy->isHalfTy())
Tim Northover29178a32013-01-22 09:46:31 +00001222 V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf,
1223 APInt(16, (uint16_t)Record[0])));
Dan Gohman518cda42011-12-17 00:04:22 +00001224 else if (CurTy->isFloatTy())
Tim Northover29178a32013-01-22 09:46:31 +00001225 V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle,
1226 APInt(32, (uint32_t)Record[0])));
Chris Lattnerfdd87902009-10-05 05:54:46 +00001227 else if (CurTy->isDoubleTy())
Tim Northover29178a32013-01-22 09:46:31 +00001228 V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble,
1229 APInt(64, Record[0])));
Chris Lattnerfdd87902009-10-05 05:54:46 +00001230 else if (CurTy->isX86_FP80Ty()) {
Dale Johannesen93eefa02009-03-23 21:16:53 +00001231 // Bits are not stored the same way as a normal i80 APInt, compensate.
1232 uint64_t Rearrange[2];
1233 Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);
1234 Rearrange[1] = Record[0] >> 48;
Tim Northover29178a32013-01-22 09:46:31 +00001235 V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended,
1236 APInt(80, Rearrange)));
Chris Lattnerfdd87902009-10-05 05:54:46 +00001237 } else if (CurTy->isFP128Ty())
Tim Northover29178a32013-01-22 09:46:31 +00001238 V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad,
1239 APInt(128, Record)));
Chris Lattnerfdd87902009-10-05 05:54:46 +00001240 else if (CurTy->isPPC_FP128Ty())
Tim Northover29178a32013-01-22 09:46:31 +00001241 V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble,
1242 APInt(128, Record)));
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001243 else
Owen Andersonb292b8c2009-07-30 23:03:37 +00001244 V = UndefValue::get(CurTy);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001245 break;
Dale Johannesen245dceb2007-09-11 18:32:33 +00001246 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001247
Chris Lattnere14cb882007-05-04 19:11:41 +00001248 case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]
1249 if (Record.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001250 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001251
Chris Lattnere14cb882007-05-04 19:11:41 +00001252 unsigned Size = Record.size();
Chris Lattnercc3aaf12012-01-27 03:15:49 +00001253 SmallVector<Constant*, 16> Elts;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001254
Chris Lattner229907c2011-07-18 04:54:35 +00001255 if (StructType *STy = dyn_cast<StructType>(CurTy)) {
Chris Lattner1663cca2007-04-24 05:48:56 +00001256 for (unsigned i = 0; i != Size; ++i)
Chris Lattnere14cb882007-05-04 19:11:41 +00001257 Elts.push_back(ValueList.getConstantFwdRef(Record[i],
Chris Lattner1663cca2007-04-24 05:48:56 +00001258 STy->getElementType(i)));
Owen Anderson45308b52009-07-27 22:29:26 +00001259 V = ConstantStruct::get(STy, Elts);
Chris Lattner229907c2011-07-18 04:54:35 +00001260 } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) {
1261 Type *EltTy = ATy->getElementType();
Chris Lattner1663cca2007-04-24 05:48:56 +00001262 for (unsigned i = 0; i != Size; ++i)
Chris Lattnere14cb882007-05-04 19:11:41 +00001263 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
Owen Andersonc2c79322009-07-28 18:32:17 +00001264 V = ConstantArray::get(ATy, Elts);
Chris Lattner229907c2011-07-18 04:54:35 +00001265 } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) {
1266 Type *EltTy = VTy->getElementType();
Chris Lattner1663cca2007-04-24 05:48:56 +00001267 for (unsigned i = 0; i != Size; ++i)
Chris Lattnere14cb882007-05-04 19:11:41 +00001268 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
Owen Anderson4aa32952009-07-28 21:19:26 +00001269 V = ConstantVector::get(Elts);
Chris Lattner1663cca2007-04-24 05:48:56 +00001270 } else {
Owen Andersonb292b8c2009-07-30 23:03:37 +00001271 V = UndefValue::get(CurTy);
Chris Lattner1663cca2007-04-24 05:48:56 +00001272 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001273 break;
1274 }
Chris Lattnerbb8278a2012-02-05 02:41:35 +00001275 case bitc::CST_CODE_STRING: // STRING: [values]
Chris Lattnerf25f7102007-05-06 00:53:07 +00001276 case bitc::CST_CODE_CSTRING: { // CSTRING: [values]
1277 if (Record.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001278 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001279
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001280 SmallString<16> Elts(Record.begin(), Record.end());
Chris Lattnerbb8278a2012-02-05 02:41:35 +00001281 V = ConstantDataArray::getString(Context, Elts,
1282 BitCode == bitc::CST_CODE_CSTRING);
Chris Lattnerf25f7102007-05-06 00:53:07 +00001283 break;
1284 }
Chris Lattner372dd1e2012-01-30 00:51:16 +00001285 case bitc::CST_CODE_DATA: {// DATA: [n x value]
1286 if (Record.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001287 return Error(InvalidRecord);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001288
Chris Lattner372dd1e2012-01-30 00:51:16 +00001289 Type *EltTy = cast<SequentialType>(CurTy)->getElementType();
1290 unsigned Size = Record.size();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001291
Chris Lattner372dd1e2012-01-30 00:51:16 +00001292 if (EltTy->isIntegerTy(8)) {
1293 SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end());
1294 if (isa<VectorType>(CurTy))
1295 V = ConstantDataVector::get(Context, Elts);
1296 else
1297 V = ConstantDataArray::get(Context, Elts);
1298 } else if (EltTy->isIntegerTy(16)) {
1299 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
1300 if (isa<VectorType>(CurTy))
1301 V = ConstantDataVector::get(Context, Elts);
1302 else
1303 V = ConstantDataArray::get(Context, Elts);
1304 } else if (EltTy->isIntegerTy(32)) {
1305 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
1306 if (isa<VectorType>(CurTy))
1307 V = ConstantDataVector::get(Context, Elts);
1308 else
1309 V = ConstantDataArray::get(Context, Elts);
1310 } else if (EltTy->isIntegerTy(64)) {
1311 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
1312 if (isa<VectorType>(CurTy))
1313 V = ConstantDataVector::get(Context, Elts);
1314 else
1315 V = ConstantDataArray::get(Context, Elts);
1316 } else if (EltTy->isFloatTy()) {
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001317 SmallVector<float, 16> Elts(Size);
1318 std::transform(Record.begin(), Record.end(), Elts.begin(), BitsToFloat);
Chris Lattner372dd1e2012-01-30 00:51:16 +00001319 if (isa<VectorType>(CurTy))
1320 V = ConstantDataVector::get(Context, Elts);
1321 else
1322 V = ConstantDataArray::get(Context, Elts);
1323 } else if (EltTy->isDoubleTy()) {
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001324 SmallVector<double, 16> Elts(Size);
1325 std::transform(Record.begin(), Record.end(), Elts.begin(),
1326 BitsToDouble);
Chris Lattner372dd1e2012-01-30 00:51:16 +00001327 if (isa<VectorType>(CurTy))
1328 V = ConstantDataVector::get(Context, Elts);
1329 else
1330 V = ConstantDataArray::get(Context, Elts);
1331 } else {
Rafael Espindola48da4f42013-11-04 16:16:24 +00001332 return Error(InvalidTypeForValue);
Chris Lattner372dd1e2012-01-30 00:51:16 +00001333 }
1334 break;
1335 }
1336
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001337 case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval]
Rafael Espindola48da4f42013-11-04 16:16:24 +00001338 if (Record.size() < 3)
1339 return Error(InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001340 int Opc = GetDecodedBinaryOpcode(Record[0], CurTy);
Chris Lattner890683d2007-04-24 18:15:21 +00001341 if (Opc < 0) {
Owen Andersonb292b8c2009-07-30 23:03:37 +00001342 V = UndefValue::get(CurTy); // Unknown binop.
Chris Lattner890683d2007-04-24 18:15:21 +00001343 } else {
1344 Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy);
1345 Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy);
Dan Gohman1b849082009-09-07 23:54:19 +00001346 unsigned Flags = 0;
1347 if (Record.size() >= 4) {
1348 if (Opc == Instruction::Add ||
1349 Opc == Instruction::Sub ||
Chris Lattnera676c0f2011-02-07 16:40:21 +00001350 Opc == Instruction::Mul ||
1351 Opc == Instruction::Shl) {
Dan Gohman1b849082009-09-07 23:54:19 +00001352 if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP))
1353 Flags |= OverflowingBinaryOperator::NoSignedWrap;
1354 if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
1355 Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
Chris Lattner35315d02011-02-06 21:44:57 +00001356 } else if (Opc == Instruction::SDiv ||
Chris Lattnera676c0f2011-02-07 16:40:21 +00001357 Opc == Instruction::UDiv ||
1358 Opc == Instruction::LShr ||
1359 Opc == Instruction::AShr) {
Chris Lattner35315d02011-02-06 21:44:57 +00001360 if (Record[3] & (1 << bitc::PEO_EXACT))
Dan Gohman1b849082009-09-07 23:54:19 +00001361 Flags |= SDivOperator::IsExact;
1362 }
1363 }
1364 V = ConstantExpr::get(Opc, LHS, RHS, Flags);
Chris Lattner890683d2007-04-24 18:15:21 +00001365 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001366 break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001367 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001368 case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval]
Rafael Espindola48da4f42013-11-04 16:16:24 +00001369 if (Record.size() < 3)
1370 return Error(InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001371 int Opc = GetDecodedCastOpcode(Record[0]);
Chris Lattner890683d2007-04-24 18:15:21 +00001372 if (Opc < 0) {
Owen Andersonb292b8c2009-07-30 23:03:37 +00001373 V = UndefValue::get(CurTy); // Unknown cast.
Chris Lattner890683d2007-04-24 18:15:21 +00001374 } else {
Chris Lattner229907c2011-07-18 04:54:35 +00001375 Type *OpTy = getTypeByID(Record[1]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001376 if (!OpTy)
1377 return Error(InvalidRecord);
Chris Lattner890683d2007-04-24 18:15:21 +00001378 Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001379 V = UpgradeBitCastExpr(Opc, Op, CurTy);
1380 if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy);
Chris Lattner890683d2007-04-24 18:15:21 +00001381 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001382 break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001383 }
Dan Gohman1639c392009-07-27 21:53:46 +00001384 case bitc::CST_CODE_CE_INBOUNDS_GEP:
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001385 case bitc::CST_CODE_CE_GEP: { // CE_GEP: [n x operands]
Rafael Espindola48da4f42013-11-04 16:16:24 +00001386 if (Record.size() & 1)
1387 return Error(InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001388 SmallVector<Constant*, 16> Elts;
Chris Lattnere14cb882007-05-04 19:11:41 +00001389 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
Chris Lattner229907c2011-07-18 04:54:35 +00001390 Type *ElTy = getTypeByID(Record[i]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001391 if (!ElTy)
1392 return Error(InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001393 Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], ElTy));
1394 }
Jay Foaded8db7d2011-07-21 14:31:17 +00001395 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
Jay Foad2f5fc8c2011-07-21 15:15:37 +00001396 V = ConstantExpr::getGetElementPtr(Elts[0], Indices,
1397 BitCode ==
1398 bitc::CST_CODE_CE_INBOUNDS_GEP);
Chris Lattner890683d2007-04-24 18:15:21 +00001399 break;
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001400 }
Joe Abbey1a6e7702013-09-12 22:02:31 +00001401 case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#]
Rafael Espindola48da4f42013-11-04 16:16:24 +00001402 if (Record.size() < 3)
1403 return Error(InvalidRecord);
Joe Abbey1a6e7702013-09-12 22:02:31 +00001404
1405 Type *SelectorTy = Type::getInt1Ty(Context);
1406
1407 // If CurTy is a vector of length n, then Record[0] must be a <n x i1>
1408 // vector. Otherwise, it must be a single bit.
1409 if (VectorType *VTy = dyn_cast<VectorType>(CurTy))
1410 SelectorTy = VectorType::get(Type::getInt1Ty(Context),
1411 VTy->getNumElements());
1412
1413 V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0],
1414 SelectorTy),
1415 ValueList.getConstantFwdRef(Record[1],CurTy),
1416 ValueList.getConstantFwdRef(Record[2],CurTy));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001417 break;
Joe Abbey1a6e7702013-09-12 22:02:31 +00001418 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001419 case bitc::CST_CODE_CE_EXTRACTELT: { // CE_EXTRACTELT: [opty, opval, opval]
Rafael Espindola48da4f42013-11-04 16:16:24 +00001420 if (Record.size() < 3)
1421 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00001422 VectorType *OpTy =
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001423 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
Rafael Espindola48da4f42013-11-04 16:16:24 +00001424 if (OpTy == 0)
1425 return Error(InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001426 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
Joe Abbey2ad8df22012-11-25 15:23:39 +00001427 Constant *Op1 = ValueList.getConstantFwdRef(Record[2],
Joe Abbey5beb3f62012-11-19 19:22:55 +00001428 Type::getInt32Ty(Context));
Owen Anderson487375e2009-07-29 18:55:55 +00001429 V = ConstantExpr::getExtractElement(Op0, Op1);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001430 break;
1431 }
1432 case bitc::CST_CODE_CE_INSERTELT: { // CE_INSERTELT: [opval, opval, opval]
Chris Lattner229907c2011-07-18 04:54:35 +00001433 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001434 if (Record.size() < 3 || OpTy == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001435 return Error(InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001436 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
1437 Constant *Op1 = ValueList.getConstantFwdRef(Record[1],
1438 OpTy->getElementType());
Joe Abbey2ad8df22012-11-25 15:23:39 +00001439 Constant *Op2 = ValueList.getConstantFwdRef(Record[2],
Joe Abbey5beb3f62012-11-19 19:22:55 +00001440 Type::getInt32Ty(Context));
Owen Anderson487375e2009-07-29 18:55:55 +00001441 V = ConstantExpr::getInsertElement(Op0, Op1, Op2);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001442 break;
1443 }
1444 case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval]
Chris Lattner229907c2011-07-18 04:54:35 +00001445 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001446 if (Record.size() < 3 || OpTy == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001447 return Error(InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001448 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
1449 Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy);
Chris Lattner229907c2011-07-18 04:54:35 +00001450 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
Owen Andersone9f98042009-07-07 20:18:58 +00001451 OpTy->getNumElements());
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001452 Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy);
Owen Anderson487375e2009-07-29 18:55:55 +00001453 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001454 break;
1455 }
Nate Begeman94aa38d2009-02-12 21:28:33 +00001456 case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval]
Chris Lattner229907c2011-07-18 04:54:35 +00001457 VectorType *RTy = dyn_cast<VectorType>(CurTy);
1458 VectorType *OpTy =
Duncan Sands89d412a2010-10-28 15:47:26 +00001459 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
Nate Begeman94aa38d2009-02-12 21:28:33 +00001460 if (Record.size() < 4 || RTy == 0 || OpTy == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001461 return Error(InvalidRecord);
Nate Begeman94aa38d2009-02-12 21:28:33 +00001462 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
1463 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
Chris Lattner229907c2011-07-18 04:54:35 +00001464 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
Owen Andersone9f98042009-07-07 20:18:58 +00001465 RTy->getNumElements());
Nate Begeman94aa38d2009-02-12 21:28:33 +00001466 Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy);
Owen Anderson487375e2009-07-29 18:55:55 +00001467 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Nate Begeman94aa38d2009-02-12 21:28:33 +00001468 break;
1469 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001470 case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred]
Rafael Espindola48da4f42013-11-04 16:16:24 +00001471 if (Record.size() < 4)
1472 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00001473 Type *OpTy = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001474 if (OpTy == 0)
1475 return Error(InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001476 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
1477 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
1478
Duncan Sands9dff9be2010-02-15 16:12:20 +00001479 if (OpTy->isFPOrFPVectorTy())
Owen Anderson487375e2009-07-29 18:55:55 +00001480 V = ConstantExpr::getFCmp(Record[3], Op0, Op1);
Nate Begemand2195702008-05-12 19:01:56 +00001481 else
Owen Anderson487375e2009-07-29 18:55:55 +00001482 V = ConstantExpr::getICmp(Record[3], Op0, Op1);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001483 break;
Chris Lattner1663cca2007-04-24 05:48:56 +00001484 }
Chad Rosierd8c76102012-09-05 19:00:49 +00001485 // This maintains backward compatibility, pre-asm dialect keywords.
Chad Rosier5895eda2012-09-05 06:28:52 +00001486 // FIXME: Remove with the 4.0 release.
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001487 case bitc::CST_CODE_INLINEASM_OLD: {
Rafael Espindola48da4f42013-11-04 16:16:24 +00001488 if (Record.size() < 2)
1489 return Error(InvalidRecord);
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001490 std::string AsmStr, ConstrStr;
Dale Johannesenfd04c742009-10-13 20:46:56 +00001491 bool HasSideEffects = Record[0] & 1;
Dale Johannesen1cfb9582009-10-21 23:28:00 +00001492 bool IsAlignStack = Record[0] >> 1;
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001493 unsigned AsmStrSize = Record[1];
1494 if (2+AsmStrSize >= Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001495 return Error(InvalidRecord);
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001496 unsigned ConstStrSize = Record[2+AsmStrSize];
1497 if (3+AsmStrSize+ConstStrSize > Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001498 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001499
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001500 for (unsigned i = 0; i != AsmStrSize; ++i)
1501 AsmStr += (char)Record[2+i];
1502 for (unsigned i = 0; i != ConstStrSize; ++i)
1503 ConstrStr += (char)Record[3+AsmStrSize+i];
Chris Lattner229907c2011-07-18 04:54:35 +00001504 PointerType *PTy = cast<PointerType>(CurTy);
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001505 V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
Dale Johannesen1cfb9582009-10-21 23:28:00 +00001506 AsmStr, ConstrStr, HasSideEffects, IsAlignStack);
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001507 break;
1508 }
Chad Rosierd8c76102012-09-05 19:00:49 +00001509 // This version adds support for the asm dialect keywords (e.g.,
1510 // inteldialect).
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001511 case bitc::CST_CODE_INLINEASM: {
Rafael Espindola48da4f42013-11-04 16:16:24 +00001512 if (Record.size() < 2)
1513 return Error(InvalidRecord);
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001514 std::string AsmStr, ConstrStr;
1515 bool HasSideEffects = Record[0] & 1;
1516 bool IsAlignStack = (Record[0] >> 1) & 1;
1517 unsigned AsmDialect = Record[0] >> 2;
1518 unsigned AsmStrSize = Record[1];
1519 if (2+AsmStrSize >= Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001520 return Error(InvalidRecord);
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001521 unsigned ConstStrSize = Record[2+AsmStrSize];
1522 if (3+AsmStrSize+ConstStrSize > Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001523 return Error(InvalidRecord);
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001524
1525 for (unsigned i = 0; i != AsmStrSize; ++i)
1526 AsmStr += (char)Record[2+i];
1527 for (unsigned i = 0; i != ConstStrSize; ++i)
1528 ConstrStr += (char)Record[3+AsmStrSize+i];
1529 PointerType *PTy = cast<PointerType>(CurTy);
1530 V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
1531 AsmStr, ConstrStr, HasSideEffects, IsAlignStack,
Chad Rosierd8c76102012-09-05 19:00:49 +00001532 InlineAsm::AsmDialect(AsmDialect));
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001533 break;
1534 }
Chris Lattner5956dc82009-10-28 05:53:48 +00001535 case bitc::CST_CODE_BLOCKADDRESS:{
Rafael Espindola48da4f42013-11-04 16:16:24 +00001536 if (Record.size() < 3)
1537 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00001538 Type *FnTy = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001539 if (FnTy == 0)
1540 return Error(InvalidRecord);
Chris Lattner5956dc82009-10-28 05:53:48 +00001541 Function *Fn =
1542 dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy));
Rafael Espindola48da4f42013-11-04 16:16:24 +00001543 if (Fn == 0)
1544 return Error(InvalidRecord);
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00001545
1546 // If the function is already parsed we can insert the block address right
1547 // away.
1548 if (!Fn->empty()) {
1549 Function::iterator BBI = Fn->begin(), BBE = Fn->end();
1550 for (size_t I = 0, E = Record[2]; I != E; ++I) {
1551 if (BBI == BBE)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001552 return Error(InvalidID);
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00001553 ++BBI;
1554 }
1555 V = BlockAddress::get(Fn, BBI);
1556 } else {
1557 // Otherwise insert a placeholder and remember it so it can be inserted
1558 // when the function is parsed.
1559 GlobalVariable *FwdRef = new GlobalVariable(*Fn->getParent(),
1560 Type::getInt8Ty(Context),
Chris Lattner5956dc82009-10-28 05:53:48 +00001561 false, GlobalValue::InternalLinkage,
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00001562 0, "");
1563 BlockAddrFwdRefs[Fn].push_back(std::make_pair(Record[2], FwdRef));
1564 V = FwdRef;
1565 }
Chris Lattner5956dc82009-10-28 05:53:48 +00001566 break;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001567 }
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001568 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001569
Chris Lattner83930552007-05-01 07:01:57 +00001570 ValueList.AssignValue(V, NextCstNo);
Chris Lattner1663cca2007-04-24 05:48:56 +00001571 ++NextCstNo;
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001572 }
1573}
Chris Lattner1314b992007-04-22 06:23:29 +00001574
Rafael Espindola48da4f42013-11-04 16:16:24 +00001575error_code BitcodeReader::ParseUseLists() {
Chad Rosierca2567b2011-12-07 21:44:12 +00001576 if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001577 return Error(InvalidRecord);
Chad Rosierca2567b2011-12-07 21:44:12 +00001578
1579 SmallVector<uint64_t, 64> Record;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001580
Chad Rosierca2567b2011-12-07 21:44:12 +00001581 // Read all the records.
1582 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00001583 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00001584
Chris Lattner27d38752013-01-20 02:13:19 +00001585 switch (Entry.Kind) {
1586 case BitstreamEntry::SubBlock: // Handled for us already.
1587 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001588 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00001589 case BitstreamEntry::EndBlock:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001590 return error_code::success();
Chris Lattner27d38752013-01-20 02:13:19 +00001591 case BitstreamEntry::Record:
1592 // The interesting case.
1593 break;
Chad Rosierca2567b2011-12-07 21:44:12 +00001594 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001595
Chad Rosierca2567b2011-12-07 21:44:12 +00001596 // Read a use list record.
1597 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +00001598 switch (Stream.readRecord(Entry.ID, Record)) {
Chad Rosierca2567b2011-12-07 21:44:12 +00001599 default: // Default behavior: unknown type.
1600 break;
1601 case bitc::USELIST_CODE_ENTRY: { // USELIST_CODE_ENTRY: TBD.
1602 unsigned RecordLength = Record.size();
1603 if (RecordLength < 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001604 return Error(InvalidRecord);
Chad Rosierca2567b2011-12-07 21:44:12 +00001605 UseListRecords.push_back(Record);
1606 break;
1607 }
1608 }
1609 }
1610}
1611
Chris Lattner85b7b402007-05-01 05:52:21 +00001612/// RememberAndSkipFunctionBody - When we see the block for a function body,
1613/// remember where it is and then skip it. This lets us lazily deserialize the
1614/// functions.
Rafael Espindola48da4f42013-11-04 16:16:24 +00001615error_code BitcodeReader::RememberAndSkipFunctionBody() {
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001616 // Get the function we are talking about.
1617 if (FunctionsWithBodies.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001618 return Error(InsufficientFunctionProtos);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001619
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001620 Function *Fn = FunctionsWithBodies.back();
1621 FunctionsWithBodies.pop_back();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001622
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001623 // Save the current stream state.
1624 uint64_t CurBit = Stream.GetCurrentBitNo();
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001625 DeferredFunctionInfo[Fn] = CurBit;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001626
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001627 // Skip over the function block for now.
1628 if (Stream.SkipBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001629 return Error(InvalidRecord);
1630 return error_code::success();
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001631}
1632
Rafael Espindola48da4f42013-11-04 16:16:24 +00001633error_code BitcodeReader::GlobalCleanup() {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001634 // Patch the initializers for globals and aliases up.
1635 ResolveGlobalAndAliasInits();
1636 if (!GlobalInits.empty() || !AliasInits.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001637 return Error(MalformedGlobalInitializerSet);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001638
1639 // Look for intrinsic functions which need to be upgraded at some point
1640 for (Module::iterator FI = TheModule->begin(), FE = TheModule->end();
1641 FI != FE; ++FI) {
1642 Function *NewFn;
1643 if (UpgradeIntrinsicFunction(FI, NewFn))
1644 UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn));
1645 }
1646
1647 // Look for global variables which need to be renamed.
1648 for (Module::global_iterator
1649 GI = TheModule->global_begin(), GE = TheModule->global_end();
1650 GI != GE; ++GI)
1651 UpgradeGlobalVariable(GI);
1652 // Force deallocation of memory for these vectors to favor the client that
1653 // want lazy deserialization.
1654 std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits);
1655 std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001656 return error_code::success();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001657}
1658
Rafael Espindola48da4f42013-11-04 16:16:24 +00001659error_code BitcodeReader::ParseModule(bool Resume) {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001660 if (Resume)
1661 Stream.JumpToBit(NextUnreadBit);
1662 else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001663 return Error(InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001664
Chris Lattner1314b992007-04-22 06:23:29 +00001665 SmallVector<uint64_t, 64> Record;
1666 std::vector<std::string> SectionTable;
Gordon Henriksend930f912008-08-17 18:44:35 +00001667 std::vector<std::string> GCTable;
Chris Lattner1314b992007-04-22 06:23:29 +00001668
1669 // Read all the records for this module.
Chris Lattner27d38752013-01-20 02:13:19 +00001670 while (1) {
1671 BitstreamEntry Entry = Stream.advance();
Joe Abbey97b7a172013-02-06 22:14:06 +00001672
Chris Lattner27d38752013-01-20 02:13:19 +00001673 switch (Entry.Kind) {
1674 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001675 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00001676 case BitstreamEntry::EndBlock:
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001677 return GlobalCleanup();
Joe Abbey97b7a172013-02-06 22:14:06 +00001678
Chris Lattner27d38752013-01-20 02:13:19 +00001679 case BitstreamEntry::SubBlock:
1680 switch (Entry.ID) {
Chris Lattner1314b992007-04-22 06:23:29 +00001681 default: // Skip unknown content.
1682 if (Stream.SkipBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001683 return Error(InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001684 break;
Chris Lattner6eeea5d2007-05-05 18:57:30 +00001685 case bitc::BLOCKINFO_BLOCK_ID:
1686 if (Stream.ReadBlockInfoBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001687 return Error(MalformedBlock);
Chris Lattner6eeea5d2007-05-05 18:57:30 +00001688 break;
Chris Lattnerfee5a372007-05-04 03:30:17 +00001689 case bitc::PARAMATTR_BLOCK_ID:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001690 if (error_code EC = ParseAttributeBlock())
1691 return EC;
Chris Lattnerfee5a372007-05-04 03:30:17 +00001692 break;
Bill Wendlingba629332013-02-10 23:24:25 +00001693 case bitc::PARAMATTR_GROUP_BLOCK_ID:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001694 if (error_code EC = ParseAttributeGroupBlock())
1695 return EC;
Bill Wendlingba629332013-02-10 23:24:25 +00001696 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001697 case bitc::TYPE_BLOCK_ID_NEW:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001698 if (error_code EC = ParseTypeTable())
1699 return EC;
Chris Lattner1314b992007-04-22 06:23:29 +00001700 break;
Chris Lattnerccaa4482007-04-23 21:26:05 +00001701 case bitc::VALUE_SYMTAB_BLOCK_ID:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001702 if (error_code EC = ParseValueSymbolTable())
1703 return EC;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001704 SeenValueSymbolTable = true;
Chris Lattnerccaa4482007-04-23 21:26:05 +00001705 break;
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001706 case bitc::CONSTANTS_BLOCK_ID:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001707 if (error_code EC = ParseConstants())
1708 return EC;
1709 if (error_code EC = ResolveGlobalAndAliasInits())
1710 return EC;
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001711 break;
Devang Patel7428d8a2009-07-22 17:43:22 +00001712 case bitc::METADATA_BLOCK_ID:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001713 if (error_code EC = ParseMetadata())
1714 return EC;
Devang Patel7428d8a2009-07-22 17:43:22 +00001715 break;
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001716 case bitc::FUNCTION_BLOCK_ID:
1717 // If this is the first function body we've seen, reverse the
1718 // FunctionsWithBodies list.
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001719 if (!SeenFirstFunctionBody) {
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001720 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
Rafael Espindola48da4f42013-11-04 16:16:24 +00001721 if (error_code EC = GlobalCleanup())
1722 return EC;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001723 SeenFirstFunctionBody = true;
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001724 }
Joe Abbey97b7a172013-02-06 22:14:06 +00001725
Rafael Espindola48da4f42013-11-04 16:16:24 +00001726 if (error_code EC = RememberAndSkipFunctionBody())
1727 return EC;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001728 // For streaming bitcode, suspend parsing when we reach the function
1729 // bodies. Subsequent materialization calls will resume it when
1730 // necessary. For streaming, the function bodies must be at the end of
1731 // the bitcode. If the bitcode file is old, the symbol table will be
1732 // at the end instead and will not have been seen yet. In this case,
1733 // just finish the parse now.
1734 if (LazyStreamer && SeenValueSymbolTable) {
1735 NextUnreadBit = Stream.GetCurrentBitNo();
Rafael Espindola48da4f42013-11-04 16:16:24 +00001736 return error_code::success();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001737 }
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001738 break;
Chad Rosierca2567b2011-12-07 21:44:12 +00001739 case bitc::USELIST_BLOCK_ID:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001740 if (error_code EC = ParseUseLists())
1741 return EC;
Chad Rosierca2567b2011-12-07 21:44:12 +00001742 break;
Chris Lattner1314b992007-04-22 06:23:29 +00001743 }
1744 continue;
Joe Abbey97b7a172013-02-06 22:14:06 +00001745
Chris Lattner27d38752013-01-20 02:13:19 +00001746 case BitstreamEntry::Record:
1747 // The interesting case.
1748 break;
Chris Lattner1314b992007-04-22 06:23:29 +00001749 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001750
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001751
Chris Lattner1314b992007-04-22 06:23:29 +00001752 // Read a record.
Chris Lattner27d38752013-01-20 02:13:19 +00001753 switch (Stream.readRecord(Entry.ID, Record)) {
Chris Lattner1314b992007-04-22 06:23:29 +00001754 default: break; // Default behavior, ignore unknown content.
Jan Wen Voungafaced02012-10-11 20:20:40 +00001755 case bitc::MODULE_CODE_VERSION: { // VERSION: [version#]
Chris Lattner1314b992007-04-22 06:23:29 +00001756 if (Record.size() < 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001757 return Error(InvalidRecord);
Jan Wen Voungafaced02012-10-11 20:20:40 +00001758 // Only version #0 and #1 are supported so far.
1759 unsigned module_version = Record[0];
1760 switch (module_version) {
Rafael Espindola48da4f42013-11-04 16:16:24 +00001761 default:
1762 return Error(InvalidValue);
Jan Wen Voungafaced02012-10-11 20:20:40 +00001763 case 0:
1764 UseRelativeIDs = false;
1765 break;
1766 case 1:
1767 UseRelativeIDs = true;
1768 break;
1769 }
Chris Lattner1314b992007-04-22 06:23:29 +00001770 break;
Jan Wen Voungafaced02012-10-11 20:20:40 +00001771 }
Chris Lattnere14cb882007-05-04 19:11:41 +00001772 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00001773 std::string S;
1774 if (ConvertToString(Record, 0, S))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001775 return Error(InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001776 TheModule->setTargetTriple(S);
1777 break;
1778 }
Chris Lattnere14cb882007-05-04 19:11:41 +00001779 case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00001780 std::string S;
1781 if (ConvertToString(Record, 0, S))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001782 return Error(InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001783 TheModule->setDataLayout(S);
1784 break;
1785 }
Chris Lattnere14cb882007-05-04 19:11:41 +00001786 case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00001787 std::string S;
1788 if (ConvertToString(Record, 0, S))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001789 return Error(InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001790 TheModule->setModuleInlineAsm(S);
1791 break;
1792 }
Bill Wendling706d3d62012-11-28 08:41:48 +00001793 case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N]
1794 // FIXME: Remove in 4.0.
1795 std::string S;
1796 if (ConvertToString(Record, 0, S))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001797 return Error(InvalidRecord);
Bill Wendling706d3d62012-11-28 08:41:48 +00001798 // Ignore value.
1799 break;
1800 }
Chris Lattnere14cb882007-05-04 19:11:41 +00001801 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00001802 std::string S;
1803 if (ConvertToString(Record, 0, S))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001804 return Error(InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001805 SectionTable.push_back(S);
1806 break;
1807 }
Gordon Henriksend930f912008-08-17 18:44:35 +00001808 case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N]
Gordon Henriksen71183b62007-12-10 03:18:06 +00001809 std::string S;
1810 if (ConvertToString(Record, 0, S))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001811 return Error(InvalidRecord);
Gordon Henriksend930f912008-08-17 18:44:35 +00001812 GCTable.push_back(S);
Gordon Henriksen71183b62007-12-10 03:18:06 +00001813 break;
1814 }
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001815 // GLOBALVAR: [pointer type, isconst, initid,
Rafael Espindola45e6c192011-01-08 16:42:36 +00001816 // linkage, alignment, section, visibility, threadlocal,
Nico Rieck7157bb72014-01-14 15:22:47 +00001817 // unnamed_addr, dllstorageclass]
Chris Lattner1314b992007-04-22 06:23:29 +00001818 case bitc::MODULE_CODE_GLOBALVAR: {
Chris Lattner4b00d922007-04-23 16:04:05 +00001819 if (Record.size() < 6)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001820 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00001821 Type *Ty = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001822 if (!Ty)
1823 return Error(InvalidRecord);
Duncan Sands19d0b472010-02-16 11:11:14 +00001824 if (!Ty->isPointerTy())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001825 return Error(InvalidTypeForValue);
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001826 unsigned AddressSpace = cast<PointerType>(Ty)->getAddressSpace();
Chris Lattner1314b992007-04-22 06:23:29 +00001827 Ty = cast<PointerType>(Ty)->getElementType();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001828
Chris Lattner1314b992007-04-22 06:23:29 +00001829 bool isConstant = Record[1];
1830 GlobalValue::LinkageTypes Linkage = GetDecodedLinkage(Record[3]);
1831 unsigned Alignment = (1 << Record[4]) >> 1;
1832 std::string Section;
1833 if (Record[5]) {
1834 if (Record[5]-1 >= SectionTable.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001835 return Error(InvalidID);
Chris Lattner1314b992007-04-22 06:23:29 +00001836 Section = SectionTable[Record[5]-1];
1837 }
Chris Lattner4b00d922007-04-23 16:04:05 +00001838 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
Chris Lattner53862f72007-05-06 19:27:46 +00001839 if (Record.size() > 6)
1840 Visibility = GetDecodedVisibility(Record[6]);
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001841
1842 GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal;
Chris Lattner53862f72007-05-06 19:27:46 +00001843 if (Record.size() > 7)
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001844 TLM = GetDecodedThreadLocalMode(Record[7]);
Chris Lattner1314b992007-04-22 06:23:29 +00001845
Rafael Espindola45e6c192011-01-08 16:42:36 +00001846 bool UnnamedAddr = false;
1847 if (Record.size() > 8)
1848 UnnamedAddr = Record[8];
1849
Michael Gottesman27e7ef32013-02-05 05:57:38 +00001850 bool ExternallyInitialized = false;
1851 if (Record.size() > 9)
1852 ExternallyInitialized = Record[9];
1853
Chris Lattner1314b992007-04-22 06:23:29 +00001854 GlobalVariable *NewGV =
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001855 new GlobalVariable(*TheModule, Ty, isConstant, Linkage, 0, "", 0,
Michael Gottesman27e7ef32013-02-05 05:57:38 +00001856 TLM, AddressSpace, ExternallyInitialized);
Chris Lattner1314b992007-04-22 06:23:29 +00001857 NewGV->setAlignment(Alignment);
1858 if (!Section.empty())
1859 NewGV->setSection(Section);
1860 NewGV->setVisibility(Visibility);
Rafael Espindola45e6c192011-01-08 16:42:36 +00001861 NewGV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001862
Nico Rieck7157bb72014-01-14 15:22:47 +00001863 if (Record.size() > 10)
1864 NewGV->setDLLStorageClass(GetDecodedDLLStorageClass(Record[10]));
1865 else
1866 UpgradeDLLImportExportLinkage(NewGV, Record[3]);
1867
Chris Lattnerccaa4482007-04-23 21:26:05 +00001868 ValueList.push_back(NewGV);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001869
Chris Lattner47d131b2007-04-24 00:18:21 +00001870 // Remember which value to use for the global initializer.
1871 if (unsigned InitID = Record[2])
1872 GlobalInits.push_back(std::make_pair(NewGV, InitID-1));
Chris Lattner1314b992007-04-22 06:23:29 +00001873 break;
1874 }
Chris Lattner4c0a6d62007-05-08 05:38:01 +00001875 // FUNCTION: [type, callingconv, isproto, linkage, paramattr,
Nico Rieck7157bb72014-01-14 15:22:47 +00001876 // alignment, section, visibility, gc, unnamed_addr,
1877 // dllstorageclass]
Chris Lattner1314b992007-04-22 06:23:29 +00001878 case bitc::MODULE_CODE_FUNCTION: {
Chris Lattner4c0a6d62007-05-08 05:38:01 +00001879 if (Record.size() < 8)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001880 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00001881 Type *Ty = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001882 if (!Ty)
1883 return Error(InvalidRecord);
Duncan Sands19d0b472010-02-16 11:11:14 +00001884 if (!Ty->isPointerTy())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001885 return Error(InvalidTypeForValue);
Chris Lattner229907c2011-07-18 04:54:35 +00001886 FunctionType *FTy =
Chris Lattner1314b992007-04-22 06:23:29 +00001887 dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType());
1888 if (!FTy)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001889 return Error(InvalidTypeForValue);
Chris Lattner1314b992007-04-22 06:23:29 +00001890
Gabor Greife9ecc682008-04-06 20:25:17 +00001891 Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage,
1892 "", TheModule);
Chris Lattner1314b992007-04-22 06:23:29 +00001893
Sandeep Patel68c5f472009-09-02 08:44:58 +00001894 Func->setCallingConv(static_cast<CallingConv::ID>(Record[1]));
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001895 bool isProto = Record[2];
Chris Lattner1314b992007-04-22 06:23:29 +00001896 Func->setLinkage(GetDecodedLinkage(Record[3]));
Devang Patel4c758ea2008-09-25 21:00:45 +00001897 Func->setAttributes(getAttributes(Record[4]));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001898
Chris Lattner4c0a6d62007-05-08 05:38:01 +00001899 Func->setAlignment((1 << Record[5]) >> 1);
1900 if (Record[6]) {
1901 if (Record[6]-1 >= SectionTable.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001902 return Error(InvalidID);
Chris Lattner4c0a6d62007-05-08 05:38:01 +00001903 Func->setSection(SectionTable[Record[6]-1]);
Chris Lattner1314b992007-04-22 06:23:29 +00001904 }
Chris Lattner4c0a6d62007-05-08 05:38:01 +00001905 Func->setVisibility(GetDecodedVisibility(Record[7]));
Gordon Henriksen71183b62007-12-10 03:18:06 +00001906 if (Record.size() > 8 && Record[8]) {
Gordon Henriksend930f912008-08-17 18:44:35 +00001907 if (Record[8]-1 > GCTable.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001908 return Error(InvalidID);
Gordon Henriksend930f912008-08-17 18:44:35 +00001909 Func->setGC(GCTable[Record[8]-1].c_str());
Gordon Henriksen71183b62007-12-10 03:18:06 +00001910 }
Rafael Espindola45e6c192011-01-08 16:42:36 +00001911 bool UnnamedAddr = false;
1912 if (Record.size() > 9)
1913 UnnamedAddr = Record[9];
1914 Func->setUnnamedAddr(UnnamedAddr);
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001915 if (Record.size() > 10 && Record[10] != 0)
1916 FunctionPrefixes.push_back(std::make_pair(Func, Record[10]-1));
Nico Rieck7157bb72014-01-14 15:22:47 +00001917
1918 if (Record.size() > 11)
1919 Func->setDLLStorageClass(GetDecodedDLLStorageClass(Record[11]));
1920 else
1921 UpgradeDLLImportExportLinkage(Func, Record[3]);
1922
Chris Lattnerccaa4482007-04-23 21:26:05 +00001923 ValueList.push_back(Func);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001924
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001925 // If this is a function with a body, remember the prototype we are
1926 // creating now, so that we can match up the body with them later.
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001927 if (!isProto) {
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001928 FunctionsWithBodies.push_back(Func);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001929 if (LazyStreamer) DeferredFunctionInfo[Func] = 0;
1930 }
Chris Lattner1314b992007-04-22 06:23:29 +00001931 break;
1932 }
Anton Korobeynikov2f22e3f2008-03-12 00:49:19 +00001933 // ALIAS: [alias type, aliasee val#, linkage]
Nico Rieck7157bb72014-01-14 15:22:47 +00001934 // ALIAS: [alias type, aliasee val#, linkage, visibility, dllstorageclass]
Chris Lattner831d4202007-04-26 03:27:58 +00001935 case bitc::MODULE_CODE_ALIAS: {
Chris Lattner44c17072007-04-26 02:46:40 +00001936 if (Record.size() < 3)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001937 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00001938 Type *Ty = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001939 if (!Ty)
1940 return Error(InvalidRecord);
Duncan Sands19d0b472010-02-16 11:11:14 +00001941 if (!Ty->isPointerTy())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001942 return Error(InvalidTypeForValue);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001943
Chris Lattner44c17072007-04-26 02:46:40 +00001944 GlobalAlias *NewGA = new GlobalAlias(Ty, GetDecodedLinkage(Record[2]),
1945 "", 0, TheModule);
Anton Korobeynikov2f22e3f2008-03-12 00:49:19 +00001946 // Old bitcode files didn't have visibility field.
1947 if (Record.size() > 3)
1948 NewGA->setVisibility(GetDecodedVisibility(Record[3]));
Nico Rieck7157bb72014-01-14 15:22:47 +00001949 if (Record.size() > 4)
1950 NewGA->setDLLStorageClass(GetDecodedDLLStorageClass(Record[4]));
1951 else
1952 UpgradeDLLImportExportLinkage(NewGA, Record[2]);
Chris Lattner44c17072007-04-26 02:46:40 +00001953 ValueList.push_back(NewGA);
1954 AliasInits.push_back(std::make_pair(NewGA, Record[1]));
1955 break;
Chris Lattner1314b992007-04-22 06:23:29 +00001956 }
Chris Lattner831d4202007-04-26 03:27:58 +00001957 /// MODULE_CODE_PURGEVALS: [numvals]
1958 case bitc::MODULE_CODE_PURGEVALS:
1959 // Trim down the value list to the specified size.
1960 if (Record.size() < 1 || Record[0] > ValueList.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001961 return Error(InvalidRecord);
Chris Lattner831d4202007-04-26 03:27:58 +00001962 ValueList.shrinkTo(Record[0]);
1963 break;
1964 }
Chris Lattner1314b992007-04-22 06:23:29 +00001965 Record.clear();
1966 }
Chris Lattner1314b992007-04-22 06:23:29 +00001967}
1968
Rafael Espindola48da4f42013-11-04 16:16:24 +00001969error_code BitcodeReader::ParseBitcodeInto(Module *M) {
Chris Lattner1314b992007-04-22 06:23:29 +00001970 TheModule = 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001971
Rafael Espindola48da4f42013-11-04 16:16:24 +00001972 if (error_code EC = InitStream())
1973 return EC;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001974
Chris Lattner1314b992007-04-22 06:23:29 +00001975 // Sniff for the signature.
1976 if (Stream.Read(8) != 'B' ||
1977 Stream.Read(8) != 'C' ||
1978 Stream.Read(4) != 0x0 ||
1979 Stream.Read(4) != 0xC ||
1980 Stream.Read(4) != 0xE ||
1981 Stream.Read(4) != 0xD)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001982 return Error(InvalidBitcodeSignature);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001983
Chris Lattner1314b992007-04-22 06:23:29 +00001984 // We expect a number of well-defined blocks, though we don't necessarily
1985 // need to understand them all.
Chris Lattner27d38752013-01-20 02:13:19 +00001986 while (1) {
1987 if (Stream.AtEndOfStream())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001988 return error_code::success();
Joe Abbey97b7a172013-02-06 22:14:06 +00001989
Chris Lattner27d38752013-01-20 02:13:19 +00001990 BitstreamEntry Entry =
1991 Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
Joe Abbey97b7a172013-02-06 22:14:06 +00001992
Chris Lattner27d38752013-01-20 02:13:19 +00001993 switch (Entry.Kind) {
1994 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001995 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00001996 case BitstreamEntry::EndBlock:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001997 return error_code::success();
Joe Abbey97b7a172013-02-06 22:14:06 +00001998
Chris Lattner27d38752013-01-20 02:13:19 +00001999 case BitstreamEntry::SubBlock:
2000 switch (Entry.ID) {
2001 case bitc::BLOCKINFO_BLOCK_ID:
2002 if (Stream.ReadBlockInfoBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002003 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002004 break;
2005 case bitc::MODULE_BLOCK_ID:
2006 // Reject multiple MODULE_BLOCK's in a single bitstream.
2007 if (TheModule)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002008 return Error(InvalidMultipleBlocks);
Chris Lattner27d38752013-01-20 02:13:19 +00002009 TheModule = M;
Rafael Espindola48da4f42013-11-04 16:16:24 +00002010 if (error_code EC = ParseModule(false))
2011 return EC;
2012 if (LazyStreamer)
2013 return error_code::success();
Chris Lattner27d38752013-01-20 02:13:19 +00002014 break;
2015 default:
2016 if (Stream.SkipBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002017 return Error(InvalidRecord);
Chris Lattner27d38752013-01-20 02:13:19 +00002018 break;
2019 }
2020 continue;
2021 case BitstreamEntry::Record:
2022 // There should be no records in the top-level of blocks.
Joe Abbey97b7a172013-02-06 22:14:06 +00002023
Chris Lattner27d38752013-01-20 02:13:19 +00002024 // The ranlib in Xcode 4 will align archive members by appending newlines
Chad Rosiera15e3aa2011-08-09 22:23:40 +00002025 // to the end of them. If this file size is a multiple of 4 but not 8, we
2026 // have to read and ignore these final 4 bytes :-(
Chris Lattner27d38752013-01-20 02:13:19 +00002027 if (Stream.getAbbrevIDWidth() == 2 && Entry.ID == 2 &&
Rafael Espindolaa97b2382011-05-26 18:59:54 +00002028 Stream.Read(6) == 2 && Stream.Read(24) == 0xa0a0a &&
Bill Wendling318f03f2012-07-19 00:15:11 +00002029 Stream.AtEndOfStream())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002030 return error_code::success();
Joe Abbey97b7a172013-02-06 22:14:06 +00002031
Rafael Espindola48da4f42013-11-04 16:16:24 +00002032 return Error(InvalidRecord);
Rafael Espindolaa97b2382011-05-26 18:59:54 +00002033 }
Chris Lattner1314b992007-04-22 06:23:29 +00002034 }
Chris Lattner1314b992007-04-22 06:23:29 +00002035}
Chris Lattner6694f602007-04-29 07:54:31 +00002036
Rafael Espindola48da4f42013-11-04 16:16:24 +00002037error_code BitcodeReader::ParseModuleTriple(std::string &Triple) {
Bill Wendling0198ce02010-10-06 01:22:42 +00002038 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002039 return Error(InvalidRecord);
Bill Wendling0198ce02010-10-06 01:22:42 +00002040
2041 SmallVector<uint64_t, 64> Record;
2042
2043 // Read all the records for this module.
Chris Lattner27d38752013-01-20 02:13:19 +00002044 while (1) {
2045 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00002046
Chris Lattner27d38752013-01-20 02:13:19 +00002047 switch (Entry.Kind) {
2048 case BitstreamEntry::SubBlock: // Handled for us already.
2049 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +00002050 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002051 case BitstreamEntry::EndBlock:
Rafael Espindola48da4f42013-11-04 16:16:24 +00002052 return error_code::success();
Chris Lattner27d38752013-01-20 02:13:19 +00002053 case BitstreamEntry::Record:
2054 // The interesting case.
2055 break;
Bill Wendling0198ce02010-10-06 01:22:42 +00002056 }
2057
2058 // Read a record.
Chris Lattner27d38752013-01-20 02:13:19 +00002059 switch (Stream.readRecord(Entry.ID, Record)) {
Bill Wendling0198ce02010-10-06 01:22:42 +00002060 default: break; // Default behavior, ignore unknown content.
Bill Wendling0198ce02010-10-06 01:22:42 +00002061 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
2062 std::string S;
2063 if (ConvertToString(Record, 0, S))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002064 return Error(InvalidRecord);
Bill Wendling0198ce02010-10-06 01:22:42 +00002065 Triple = S;
2066 break;
2067 }
2068 }
2069 Record.clear();
2070 }
Bill Wendling0198ce02010-10-06 01:22:42 +00002071}
2072
Rafael Espindola48da4f42013-11-04 16:16:24 +00002073error_code BitcodeReader::ParseTriple(std::string &Triple) {
2074 if (error_code EC = InitStream())
2075 return EC;
Bill Wendling0198ce02010-10-06 01:22:42 +00002076
2077 // Sniff for the signature.
2078 if (Stream.Read(8) != 'B' ||
2079 Stream.Read(8) != 'C' ||
2080 Stream.Read(4) != 0x0 ||
2081 Stream.Read(4) != 0xC ||
2082 Stream.Read(4) != 0xE ||
2083 Stream.Read(4) != 0xD)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002084 return Error(InvalidBitcodeSignature);
Bill Wendling0198ce02010-10-06 01:22:42 +00002085
2086 // We expect a number of well-defined blocks, though we don't necessarily
2087 // need to understand them all.
Chris Lattner27d38752013-01-20 02:13:19 +00002088 while (1) {
2089 BitstreamEntry Entry = Stream.advance();
Joe Abbey97b7a172013-02-06 22:14:06 +00002090
Chris Lattner27d38752013-01-20 02:13:19 +00002091 switch (Entry.Kind) {
2092 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +00002093 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002094 case BitstreamEntry::EndBlock:
Rafael Espindola48da4f42013-11-04 16:16:24 +00002095 return error_code::success();
Joe Abbey97b7a172013-02-06 22:14:06 +00002096
Chris Lattner27d38752013-01-20 02:13:19 +00002097 case BitstreamEntry::SubBlock:
2098 if (Entry.ID == bitc::MODULE_BLOCK_ID)
2099 return ParseModuleTriple(Triple);
Joe Abbey97b7a172013-02-06 22:14:06 +00002100
Chris Lattner27d38752013-01-20 02:13:19 +00002101 // Ignore other sub-blocks.
Rafael Espindola48da4f42013-11-04 16:16:24 +00002102 if (Stream.SkipBlock())
2103 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002104 continue;
Joe Abbey97b7a172013-02-06 22:14:06 +00002105
Chris Lattner27d38752013-01-20 02:13:19 +00002106 case BitstreamEntry::Record:
2107 Stream.skipRecord(Entry.ID);
2108 continue;
Bill Wendling0198ce02010-10-06 01:22:42 +00002109 }
2110 }
Bill Wendling0198ce02010-10-06 01:22:42 +00002111}
2112
Devang Patelaf206b82009-09-18 19:26:43 +00002113/// ParseMetadataAttachment - Parse metadata attachments.
Rafael Espindola48da4f42013-11-04 16:16:24 +00002114error_code BitcodeReader::ParseMetadataAttachment() {
Devang Patelaf206b82009-09-18 19:26:43 +00002115 if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002116 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002117
Devang Patelaf206b82009-09-18 19:26:43 +00002118 SmallVector<uint64_t, 64> Record;
Chris Lattner27d38752013-01-20 02:13:19 +00002119 while (1) {
2120 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00002121
Chris Lattner27d38752013-01-20 02:13:19 +00002122 switch (Entry.Kind) {
2123 case BitstreamEntry::SubBlock: // Handled for us already.
2124 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +00002125 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002126 case BitstreamEntry::EndBlock:
Rafael Espindola48da4f42013-11-04 16:16:24 +00002127 return error_code::success();
Chris Lattner27d38752013-01-20 02:13:19 +00002128 case BitstreamEntry::Record:
2129 // The interesting case.
Devang Patelaf206b82009-09-18 19:26:43 +00002130 break;
2131 }
Chris Lattner27d38752013-01-20 02:13:19 +00002132
Devang Patelaf206b82009-09-18 19:26:43 +00002133 // Read a metadata attachment record.
2134 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +00002135 switch (Stream.readRecord(Entry.ID, Record)) {
Devang Patelaf206b82009-09-18 19:26:43 +00002136 default: // Default behavior: ignore.
2137 break;
Chris Lattnerb8778552011-06-17 17:50:30 +00002138 case bitc::METADATA_ATTACHMENT: {
Devang Patelaf206b82009-09-18 19:26:43 +00002139 unsigned RecordLength = Record.size();
2140 if (Record.empty() || (RecordLength - 1) % 2 == 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002141 return Error(InvalidRecord);
Devang Patelaf206b82009-09-18 19:26:43 +00002142 Instruction *Inst = InstructionList[Record[0]];
2143 for (unsigned i = 1; i != RecordLength; i = i+2) {
Devang Patelb1a44772009-09-28 21:14:55 +00002144 unsigned Kind = Record[i];
Dan Gohman43aa8f02010-07-20 21:42:28 +00002145 DenseMap<unsigned, unsigned>::iterator I =
2146 MDKindMap.find(Kind);
2147 if (I == MDKindMap.end())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002148 return Error(InvalidID);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002149 Value *Node = MDValueList.getValueFwdRef(Record[i+1]);
Dan Gohman43aa8f02010-07-20 21:42:28 +00002150 Inst->setMetadata(I->second, cast<MDNode>(Node));
Manman Ren209b17c2013-09-28 00:22:27 +00002151 if (I->second == LLVMContext::MD_tbaa)
2152 InstsWithTBAATag.push_back(Inst);
Devang Patelaf206b82009-09-18 19:26:43 +00002153 }
2154 break;
2155 }
2156 }
2157 }
Devang Patelaf206b82009-09-18 19:26:43 +00002158}
Chris Lattner51ffe7c2007-05-01 04:59:48 +00002159
Chris Lattner85b7b402007-05-01 05:52:21 +00002160/// ParseFunctionBody - Lazily parse the specified function body block.
Rafael Espindola48da4f42013-11-04 16:16:24 +00002161error_code BitcodeReader::ParseFunctionBody(Function *F) {
Chris Lattner982ec1e2007-05-05 00:17:00 +00002162 if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002163 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002164
Nick Lewyckya72e1af2010-02-25 08:30:17 +00002165 InstructionList.clear();
Chris Lattner85b7b402007-05-01 05:52:21 +00002166 unsigned ModuleValueListSize = ValueList.size();
Dan Gohman26d837d2010-08-25 20:22:53 +00002167 unsigned ModuleMDValueListSize = MDValueList.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002168
Chris Lattner85b7b402007-05-01 05:52:21 +00002169 // Add all the function arguments to the value table.
2170 for(Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
2171 ValueList.push_back(I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002172
Chris Lattner83930552007-05-01 07:01:57 +00002173 unsigned NextValueNo = ValueList.size();
Chris Lattnere53603e2007-05-02 04:27:25 +00002174 BasicBlock *CurBB = 0;
2175 unsigned CurBBNo = 0;
2176
Chris Lattner07d09ed2010-04-03 02:17:50 +00002177 DebugLoc LastLoc;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002178
Chris Lattner85b7b402007-05-01 05:52:21 +00002179 // Read all the records.
2180 SmallVector<uint64_t, 64> Record;
2181 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00002182 BitstreamEntry Entry = Stream.advance();
Joe Abbey97b7a172013-02-06 22:14:06 +00002183
Chris Lattner27d38752013-01-20 02:13:19 +00002184 switch (Entry.Kind) {
2185 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +00002186 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002187 case BitstreamEntry::EndBlock:
2188 goto OutOfRecordLoop;
Joe Abbey97b7a172013-02-06 22:14:06 +00002189
Chris Lattner27d38752013-01-20 02:13:19 +00002190 case BitstreamEntry::SubBlock:
2191 switch (Entry.ID) {
Chris Lattner85b7b402007-05-01 05:52:21 +00002192 default: // Skip unknown content.
2193 if (Stream.SkipBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002194 return Error(InvalidRecord);
Chris Lattner85b7b402007-05-01 05:52:21 +00002195 break;
2196 case bitc::CONSTANTS_BLOCK_ID:
Rafael Espindola48da4f42013-11-04 16:16:24 +00002197 if (error_code EC = ParseConstants())
2198 return EC;
Chris Lattner83930552007-05-01 07:01:57 +00002199 NextValueNo = ValueList.size();
Chris Lattner85b7b402007-05-01 05:52:21 +00002200 break;
2201 case bitc::VALUE_SYMTAB_BLOCK_ID:
Rafael Espindola48da4f42013-11-04 16:16:24 +00002202 if (error_code EC = ParseValueSymbolTable())
2203 return EC;
Chris Lattner85b7b402007-05-01 05:52:21 +00002204 break;
Devang Patelaf206b82009-09-18 19:26:43 +00002205 case bitc::METADATA_ATTACHMENT_ID:
Rafael Espindola48da4f42013-11-04 16:16:24 +00002206 if (error_code EC = ParseMetadataAttachment())
2207 return EC;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002208 break;
Victor Hernandez108d3ac2010-01-13 19:34:08 +00002209 case bitc::METADATA_BLOCK_ID:
Rafael Espindola48da4f42013-11-04 16:16:24 +00002210 if (error_code EC = ParseMetadata())
2211 return EC;
Victor Hernandez108d3ac2010-01-13 19:34:08 +00002212 break;
Chris Lattner85b7b402007-05-01 05:52:21 +00002213 }
2214 continue;
Joe Abbey97b7a172013-02-06 22:14:06 +00002215
Chris Lattner27d38752013-01-20 02:13:19 +00002216 case BitstreamEntry::Record:
2217 // The interesting case.
2218 break;
Chris Lattner85b7b402007-05-01 05:52:21 +00002219 }
Joe Abbey97b7a172013-02-06 22:14:06 +00002220
Chris Lattner85b7b402007-05-01 05:52:21 +00002221 // Read a record.
2222 Record.clear();
Chris Lattner83930552007-05-01 07:01:57 +00002223 Instruction *I = 0;
Chris Lattner27d38752013-01-20 02:13:19 +00002224 unsigned BitCode = Stream.readRecord(Entry.ID, Record);
Dan Gohman0ebd6962009-07-20 21:19:07 +00002225 switch (BitCode) {
Chris Lattner83930552007-05-01 07:01:57 +00002226 default: // Default behavior: reject
Rafael Espindola48da4f42013-11-04 16:16:24 +00002227 return Error(InvalidValue);
Chris Lattner85b7b402007-05-01 05:52:21 +00002228 case bitc::FUNC_CODE_DECLAREBLOCKS: // DECLAREBLOCKS: [nblocks]
Chris Lattner83930552007-05-01 07:01:57 +00002229 if (Record.size() < 1 || Record[0] == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002230 return Error(InvalidRecord);
Chris Lattner85b7b402007-05-01 05:52:21 +00002231 // Create all the basic blocks for the function.
Chris Lattner6ce15cb2007-05-03 22:09:51 +00002232 FunctionBBs.resize(Record[0]);
Chris Lattner85b7b402007-05-01 05:52:21 +00002233 for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i)
Owen Anderson55f1c092009-08-13 21:58:54 +00002234 FunctionBBs[i] = BasicBlock::Create(Context, "", F);
Chris Lattner83930552007-05-01 07:01:57 +00002235 CurBB = FunctionBBs[0];
2236 continue;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002237
Chris Lattner07d09ed2010-04-03 02:17:50 +00002238 case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN
2239 // This record indicates that the last instruction is at the same
2240 // location as the previous instruction with a location.
2241 I = 0;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002242
Chris Lattner07d09ed2010-04-03 02:17:50 +00002243 // Get the last instruction emitted.
2244 if (CurBB && !CurBB->empty())
2245 I = &CurBB->back();
2246 else if (CurBBNo && FunctionBBs[CurBBNo-1] &&
2247 !FunctionBBs[CurBBNo-1]->empty())
2248 I = &FunctionBBs[CurBBNo-1]->back();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002249
Rafael Espindola48da4f42013-11-04 16:16:24 +00002250 if (I == 0)
2251 return Error(InvalidRecord);
Chris Lattner07d09ed2010-04-03 02:17:50 +00002252 I->setDebugLoc(LastLoc);
2253 I = 0;
2254 continue;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002255
Chris Lattnerc44070802011-06-17 18:17:37 +00002256 case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia]
Chris Lattner07d09ed2010-04-03 02:17:50 +00002257 I = 0; // Get the last instruction emitted.
2258 if (CurBB && !CurBB->empty())
2259 I = &CurBB->back();
2260 else if (CurBBNo && FunctionBBs[CurBBNo-1] &&
2261 !FunctionBBs[CurBBNo-1]->empty())
2262 I = &FunctionBBs[CurBBNo-1]->back();
2263 if (I == 0 || Record.size() < 4)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002264 return Error(InvalidRecord);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002265
Chris Lattner07d09ed2010-04-03 02:17:50 +00002266 unsigned Line = Record[0], Col = Record[1];
2267 unsigned ScopeID = Record[2], IAID = Record[3];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002268
Chris Lattner07d09ed2010-04-03 02:17:50 +00002269 MDNode *Scope = 0, *IA = 0;
2270 if (ScopeID) Scope = cast<MDNode>(MDValueList.getValueFwdRef(ScopeID-1));
2271 if (IAID) IA = cast<MDNode>(MDValueList.getValueFwdRef(IAID-1));
2272 LastLoc = DebugLoc::get(Line, Col, Scope, IA);
2273 I->setDebugLoc(LastLoc);
2274 I = 0;
2275 continue;
2276 }
2277
Chris Lattnere9759c22007-05-06 00:21:25 +00002278 case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode]
2279 unsigned OpNum = 0;
2280 Value *LHS, *RHS;
2281 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002282 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
Dan Gohman0ebd6962009-07-20 21:19:07 +00002283 OpNum+1 > Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002284 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002285
Dan Gohman0ebd6962009-07-20 21:19:07 +00002286 int Opc = GetDecodedBinaryOpcode(Record[OpNum++], LHS->getType());
Rafael Espindola48da4f42013-11-04 16:16:24 +00002287 if (Opc == -1)
2288 return Error(InvalidRecord);
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002289 I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
Devang Patelaf206b82009-09-18 19:26:43 +00002290 InstructionList.push_back(I);
Dan Gohman1b849082009-09-07 23:54:19 +00002291 if (OpNum < Record.size()) {
2292 if (Opc == Instruction::Add ||
2293 Opc == Instruction::Sub ||
Chris Lattnera676c0f2011-02-07 16:40:21 +00002294 Opc == Instruction::Mul ||
2295 Opc == Instruction::Shl) {
Dan Gohman00f47472010-01-25 21:55:39 +00002296 if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP))
Dan Gohman1b849082009-09-07 23:54:19 +00002297 cast<BinaryOperator>(I)->setHasNoSignedWrap(true);
Dan Gohman00f47472010-01-25 21:55:39 +00002298 if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
Dan Gohman1b849082009-09-07 23:54:19 +00002299 cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true);
Chris Lattner35315d02011-02-06 21:44:57 +00002300 } else if (Opc == Instruction::SDiv ||
Chris Lattnera676c0f2011-02-07 16:40:21 +00002301 Opc == Instruction::UDiv ||
2302 Opc == Instruction::LShr ||
2303 Opc == Instruction::AShr) {
Chris Lattner35315d02011-02-06 21:44:57 +00002304 if (Record[OpNum] & (1 << bitc::PEO_EXACT))
Dan Gohman1b849082009-09-07 23:54:19 +00002305 cast<BinaryOperator>(I)->setIsExact(true);
Michael Ilseman9978d7e2012-11-27 00:43:38 +00002306 } else if (isa<FPMathOperator>(I)) {
2307 FastMathFlags FMF;
Michael Ilseman65f14352012-12-09 21:12:04 +00002308 if (0 != (Record[OpNum] & FastMathFlags::UnsafeAlgebra))
2309 FMF.setUnsafeAlgebra();
2310 if (0 != (Record[OpNum] & FastMathFlags::NoNaNs))
2311 FMF.setNoNaNs();
2312 if (0 != (Record[OpNum] & FastMathFlags::NoInfs))
2313 FMF.setNoInfs();
2314 if (0 != (Record[OpNum] & FastMathFlags::NoSignedZeros))
2315 FMF.setNoSignedZeros();
2316 if (0 != (Record[OpNum] & FastMathFlags::AllowReciprocal))
2317 FMF.setAllowReciprocal();
Michael Ilseman9978d7e2012-11-27 00:43:38 +00002318 if (FMF.any())
2319 I->setFastMathFlags(FMF);
Dan Gohman1b849082009-09-07 23:54:19 +00002320 }
Michael Ilseman9978d7e2012-11-27 00:43:38 +00002321
Dan Gohman1b849082009-09-07 23:54:19 +00002322 }
Chris Lattner85b7b402007-05-01 05:52:21 +00002323 break;
2324 }
Chris Lattnere9759c22007-05-06 00:21:25 +00002325 case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc]
2326 unsigned OpNum = 0;
2327 Value *Op;
2328 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
2329 OpNum+2 != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002330 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002331
Chris Lattner229907c2011-07-18 04:54:35 +00002332 Type *ResTy = getTypeByID(Record[OpNum]);
Chris Lattnere9759c22007-05-06 00:21:25 +00002333 int Opc = GetDecodedCastOpcode(Record[OpNum+1]);
2334 if (Opc == -1 || ResTy == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002335 return Error(InvalidRecord);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002336 Instruction *Temp = 0;
2337 if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) {
2338 if (Temp) {
2339 InstructionList.push_back(Temp);
2340 CurBB->getInstList().push_back(Temp);
2341 }
2342 } else {
2343 I = CastInst::Create((Instruction::CastOps)Opc, Op, ResTy);
2344 }
Devang Patelaf206b82009-09-18 19:26:43 +00002345 InstructionList.push_back(I);
Chris Lattnere53603e2007-05-02 04:27:25 +00002346 break;
2347 }
Dan Gohman1639c392009-07-27 21:53:46 +00002348 case bitc::FUNC_CODE_INST_INBOUNDS_GEP:
Chris Lattnere14cb882007-05-04 19:11:41 +00002349 case bitc::FUNC_CODE_INST_GEP: { // GEP: [n x operands]
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002350 unsigned OpNum = 0;
2351 Value *BasePtr;
2352 if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002353 return Error(InvalidRecord);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002354
Chris Lattner5285b5e2007-05-02 05:46:45 +00002355 SmallVector<Value*, 16> GEPIdx;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002356 while (OpNum != Record.size()) {
2357 Value *Op;
2358 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002359 return Error(InvalidRecord);
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002360 GEPIdx.push_back(Op);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002361 }
2362
Jay Foadd1b78492011-07-25 09:48:08 +00002363 I = GetElementPtrInst::Create(BasePtr, GEPIdx);
Devang Patelaf206b82009-09-18 19:26:43 +00002364 InstructionList.push_back(I);
Dan Gohman1639c392009-07-27 21:53:46 +00002365 if (BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP)
Dan Gohman1b849082009-09-07 23:54:19 +00002366 cast<GetElementPtrInst>(I)->setIsInBounds(true);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002367 break;
2368 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002369
Dan Gohman1ecaf452008-05-31 00:58:22 +00002370 case bitc::FUNC_CODE_INST_EXTRACTVAL: {
2371 // EXTRACTVAL: [opty, opval, n x indices]
Dan Gohman30499842008-05-23 01:55:30 +00002372 unsigned OpNum = 0;
2373 Value *Agg;
2374 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002375 return Error(InvalidRecord);
Dan Gohman30499842008-05-23 01:55:30 +00002376
Dan Gohman1ecaf452008-05-31 00:58:22 +00002377 SmallVector<unsigned, 4> EXTRACTVALIdx;
2378 for (unsigned RecSize = Record.size();
2379 OpNum != RecSize; ++OpNum) {
2380 uint64_t Index = Record[OpNum];
2381 if ((unsigned)Index != Index)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002382 return Error(InvalidValue);
Dan Gohman1ecaf452008-05-31 00:58:22 +00002383 EXTRACTVALIdx.push_back((unsigned)Index);
Dan Gohman30499842008-05-23 01:55:30 +00002384 }
2385
Jay Foad57aa6362011-07-13 10:26:04 +00002386 I = ExtractValueInst::Create(Agg, EXTRACTVALIdx);
Devang Patelaf206b82009-09-18 19:26:43 +00002387 InstructionList.push_back(I);
Dan Gohman30499842008-05-23 01:55:30 +00002388 break;
2389 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002390
Dan Gohman1ecaf452008-05-31 00:58:22 +00002391 case bitc::FUNC_CODE_INST_INSERTVAL: {
2392 // INSERTVAL: [opty, opval, opty, opval, n x indices]
Dan Gohman30499842008-05-23 01:55:30 +00002393 unsigned OpNum = 0;
2394 Value *Agg;
2395 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002396 return Error(InvalidRecord);
Dan Gohman30499842008-05-23 01:55:30 +00002397 Value *Val;
2398 if (getValueTypePair(Record, OpNum, NextValueNo, Val))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002399 return Error(InvalidRecord);
Dan Gohman30499842008-05-23 01:55:30 +00002400
Dan Gohman1ecaf452008-05-31 00:58:22 +00002401 SmallVector<unsigned, 4> INSERTVALIdx;
2402 for (unsigned RecSize = Record.size();
2403 OpNum != RecSize; ++OpNum) {
2404 uint64_t Index = Record[OpNum];
2405 if ((unsigned)Index != Index)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002406 return Error(InvalidValue);
Dan Gohman1ecaf452008-05-31 00:58:22 +00002407 INSERTVALIdx.push_back((unsigned)Index);
Dan Gohman30499842008-05-23 01:55:30 +00002408 }
2409
Jay Foad57aa6362011-07-13 10:26:04 +00002410 I = InsertValueInst::Create(Agg, Val, INSERTVALIdx);
Devang Patelaf206b82009-09-18 19:26:43 +00002411 InstructionList.push_back(I);
Dan Gohman30499842008-05-23 01:55:30 +00002412 break;
2413 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002414
Chris Lattnere9759c22007-05-06 00:21:25 +00002415 case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval]
Dan Gohmanc5d28922008-09-16 01:01:33 +00002416 // obsolete form of select
2417 // handles select i1 ... in old bitcode
Chris Lattnere9759c22007-05-06 00:21:25 +00002418 unsigned OpNum = 0;
2419 Value *TrueVal, *FalseVal, *Cond;
2420 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002421 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
2422 popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002423 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002424
Dan Gohmanc5d28922008-09-16 01:01:33 +00002425 I = SelectInst::Create(Cond, TrueVal, FalseVal);
Devang Patelaf206b82009-09-18 19:26:43 +00002426 InstructionList.push_back(I);
Dan Gohmanc5d28922008-09-16 01:01:33 +00002427 break;
2428 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002429
Dan Gohmanc5d28922008-09-16 01:01:33 +00002430 case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred]
2431 // new form of select
2432 // handles select i1 or select [N x i1]
2433 unsigned OpNum = 0;
2434 Value *TrueVal, *FalseVal, *Cond;
2435 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002436 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
Dan Gohmanc5d28922008-09-16 01:01:33 +00002437 getValueTypePair(Record, OpNum, NextValueNo, Cond))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002438 return Error(InvalidRecord);
Dan Gohmanc579d972008-09-09 01:02:47 +00002439
2440 // select condition can be either i1 or [N x i1]
Chris Lattner229907c2011-07-18 04:54:35 +00002441 if (VectorType* vector_type =
2442 dyn_cast<VectorType>(Cond->getType())) {
Dan Gohmanc579d972008-09-09 01:02:47 +00002443 // expect <n x i1>
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002444 if (vector_type->getElementType() != Type::getInt1Ty(Context))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002445 return Error(InvalidTypeForValue);
Dan Gohmanc579d972008-09-09 01:02:47 +00002446 } else {
2447 // expect i1
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002448 if (Cond->getType() != Type::getInt1Ty(Context))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002449 return Error(InvalidTypeForValue);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002450 }
2451
Gabor Greife9ecc682008-04-06 20:25:17 +00002452 I = SelectInst::Create(Cond, TrueVal, FalseVal);
Devang Patelaf206b82009-09-18 19:26:43 +00002453 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002454 break;
2455 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002456
Chris Lattner1fc27f02007-05-02 05:16:49 +00002457 case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval]
Chris Lattnere9759c22007-05-06 00:21:25 +00002458 unsigned OpNum = 0;
2459 Value *Vec, *Idx;
2460 if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002461 popValue(Record, OpNum, NextValueNo, Type::getInt32Ty(Context), Idx))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002462 return Error(InvalidRecord);
Eric Christopherc9742252009-07-25 02:28:41 +00002463 I = ExtractElementInst::Create(Vec, Idx);
Devang Patelaf206b82009-09-18 19:26:43 +00002464 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002465 break;
2466 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002467
Chris Lattner1fc27f02007-05-02 05:16:49 +00002468 case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval]
Chris Lattnere9759c22007-05-06 00:21:25 +00002469 unsigned OpNum = 0;
2470 Value *Vec, *Elt, *Idx;
2471 if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002472 popValue(Record, OpNum, NextValueNo,
Chris Lattnere9759c22007-05-06 00:21:25 +00002473 cast<VectorType>(Vec->getType())->getElementType(), Elt) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002474 popValue(Record, OpNum, NextValueNo, Type::getInt32Ty(Context), Idx))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002475 return Error(InvalidRecord);
Gabor Greife9ecc682008-04-06 20:25:17 +00002476 I = InsertElementInst::Create(Vec, Elt, Idx);
Devang Patelaf206b82009-09-18 19:26:43 +00002477 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002478 break;
2479 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002480
Chris Lattnere9759c22007-05-06 00:21:25 +00002481 case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval]
2482 unsigned OpNum = 0;
2483 Value *Vec1, *Vec2, *Mask;
2484 if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002485 popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002486 return Error(InvalidRecord);
Chris Lattnere9759c22007-05-06 00:21:25 +00002487
Mon P Wang25f01062008-11-10 04:46:22 +00002488 if (getValueTypePair(Record, OpNum, NextValueNo, Mask))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002489 return Error(InvalidRecord);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002490 I = new ShuffleVectorInst(Vec1, Vec2, Mask);
Devang Patelaf206b82009-09-18 19:26:43 +00002491 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002492 break;
2493 }
Mon P Wang25f01062008-11-10 04:46:22 +00002494
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002495 case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred]
2496 // Old form of ICmp/FCmp returning bool
2497 // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were
2498 // both legal on vectors but had different behaviour.
2499 case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred]
2500 // FCmp/ICmp returning bool or vector of bool
2501
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002502 unsigned OpNum = 0;
2503 Value *LHS, *RHS;
2504 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002505 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002506 OpNum+1 != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002507 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002508
Duncan Sands9dff9be2010-02-15 16:12:20 +00002509 if (LHS->getType()->isFPOrFPVectorTy())
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002510 I = new FCmpInst((FCmpInst::Predicate)Record[OpNum], LHS, RHS);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002511 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002512 I = new ICmpInst((ICmpInst::Predicate)Record[OpNum], LHS, RHS);
Devang Patelaf206b82009-09-18 19:26:43 +00002513 InstructionList.push_back(I);
Dan Gohmanc579d972008-09-09 01:02:47 +00002514 break;
2515 }
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002516
Chris Lattnere53603e2007-05-02 04:27:25 +00002517 case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>]
Devang Patelbbfd8742008-02-26 01:29:32 +00002518 {
2519 unsigned Size = Record.size();
2520 if (Size == 0) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002521 I = ReturnInst::Create(Context);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002522 InstructionList.push_back(I);
Devang Patelbbfd8742008-02-26 01:29:32 +00002523 break;
Dan Gohmanfa1211f2008-07-23 00:34:11 +00002524 }
Devang Patelbbfd8742008-02-26 01:29:32 +00002525
Dan Gohmanfa1211f2008-07-23 00:34:11 +00002526 unsigned OpNum = 0;
Chris Lattnerf1c87102011-06-17 18:09:11 +00002527 Value *Op = NULL;
2528 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002529 return Error(InvalidRecord);
Chris Lattnerf1c87102011-06-17 18:09:11 +00002530 if (OpNum != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002531 return Error(InvalidRecord);
Dan Gohmanfa1211f2008-07-23 00:34:11 +00002532
Chris Lattnerf1c87102011-06-17 18:09:11 +00002533 I = ReturnInst::Create(Context, Op);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002534 InstructionList.push_back(I);
Dan Gohmanfa1211f2008-07-23 00:34:11 +00002535 break;
Chris Lattnere53603e2007-05-02 04:27:25 +00002536 }
Chris Lattner5285b5e2007-05-02 05:46:45 +00002537 case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#]
Chris Lattner6ce15cb2007-05-03 22:09:51 +00002538 if (Record.size() != 1 && Record.size() != 3)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002539 return Error(InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002540 BasicBlock *TrueDest = getBasicBlock(Record[0]);
2541 if (TrueDest == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002542 return Error(InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002543
Devang Patelaf206b82009-09-18 19:26:43 +00002544 if (Record.size() == 1) {
Gabor Greife9ecc682008-04-06 20:25:17 +00002545 I = BranchInst::Create(TrueDest);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002546 InstructionList.push_back(I);
Devang Patelaf206b82009-09-18 19:26:43 +00002547 }
Chris Lattner5285b5e2007-05-02 05:46:45 +00002548 else {
2549 BasicBlock *FalseDest = getBasicBlock(Record[1]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002550 Value *Cond = getValue(Record, 2, NextValueNo,
2551 Type::getInt1Ty(Context));
Chris Lattner5285b5e2007-05-02 05:46:45 +00002552 if (FalseDest == 0 || Cond == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002553 return Error(InvalidRecord);
Gabor Greife9ecc682008-04-06 20:25:17 +00002554 I = BranchInst::Create(TrueDest, FalseDest, Cond);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002555 InstructionList.push_back(I);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002556 }
2557 break;
2558 }
Chris Lattner3ed871f2009-10-27 19:13:16 +00002559 case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...]
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002560 // Check magic
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002561 if ((Record[0] >> 16) == SWITCH_INST_MAGIC) {
Bob Wilsone4077362013-09-09 19:14:35 +00002562 // "New" SwitchInst format with case ranges. The changes to write this
2563 // format were reverted but we still recognize bitcode that uses it.
2564 // Hopefully someday we will have support for case ranges and can use
2565 // this format again.
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002566
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002567 Type *OpTy = getTypeByID(Record[1]);
2568 unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth();
2569
Jan Wen Voungafaced02012-10-11 20:20:40 +00002570 Value *Cond = getValue(Record, 2, NextValueNo, OpTy);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002571 BasicBlock *Default = getBasicBlock(Record[3]);
2572 if (OpTy == 0 || Cond == 0 || Default == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002573 return Error(InvalidRecord);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002574
2575 unsigned NumCases = Record[4];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002576
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002577 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
2578 InstructionList.push_back(SI);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002579
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002580 unsigned CurIdx = 5;
2581 for (unsigned i = 0; i != NumCases; ++i) {
Bob Wilsone4077362013-09-09 19:14:35 +00002582 SmallVector<ConstantInt*, 1> CaseVals;
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002583 unsigned NumItems = Record[CurIdx++];
2584 for (unsigned ci = 0; ci != NumItems; ++ci) {
2585 bool isSingleNumber = Record[CurIdx++];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002586
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002587 APInt Low;
2588 unsigned ActiveWords = 1;
2589 if (ValueBitWidth > 64)
2590 ActiveWords = Record[CurIdx++];
Benjamin Kramer9704ed02012-05-28 14:10:31 +00002591 Low = ReadWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
2592 ValueBitWidth);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002593 CurIdx += ActiveWords;
Stepan Dyatkovskiye3e19cb2012-05-28 12:39:09 +00002594
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002595 if (!isSingleNumber) {
2596 ActiveWords = 1;
2597 if (ValueBitWidth > 64)
2598 ActiveWords = Record[CurIdx++];
2599 APInt High =
Benjamin Kramer9704ed02012-05-28 14:10:31 +00002600 ReadWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
2601 ValueBitWidth);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002602 CurIdx += ActiveWords;
Bob Wilsone4077362013-09-09 19:14:35 +00002603
2604 // FIXME: It is not clear whether values in the range should be
2605 // compared as signed or unsigned values. The partially
2606 // implemented changes that used this format in the past used
2607 // unsigned comparisons.
2608 for ( ; Low.ule(High); ++Low)
2609 CaseVals.push_back(ConstantInt::get(Context, Low));
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002610 } else
Bob Wilsone4077362013-09-09 19:14:35 +00002611 CaseVals.push_back(ConstantInt::get(Context, Low));
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002612 }
2613 BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]);
Bob Wilsone4077362013-09-09 19:14:35 +00002614 for (SmallVector<ConstantInt*, 1>::iterator cvi = CaseVals.begin(),
2615 cve = CaseVals.end(); cvi != cve; ++cvi)
2616 SI->addCase(*cvi, DestBB);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002617 }
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002618 I = SI;
2619 break;
2620 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002621
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002622 // Old SwitchInst format without case ranges.
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002623
Chris Lattner5285b5e2007-05-02 05:46:45 +00002624 if (Record.size() < 3 || (Record.size() & 1) == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002625 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00002626 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002627 Value *Cond = getValue(Record, 1, NextValueNo, OpTy);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002628 BasicBlock *Default = getBasicBlock(Record[2]);
2629 if (OpTy == 0 || Cond == 0 || Default == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002630 return Error(InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002631 unsigned NumCases = (Record.size()-3)/2;
Gabor Greife9ecc682008-04-06 20:25:17 +00002632 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
Devang Patelaf206b82009-09-18 19:26:43 +00002633 InstructionList.push_back(SI);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002634 for (unsigned i = 0, e = NumCases; i != e; ++i) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002635 ConstantInt *CaseVal =
Chris Lattner5285b5e2007-05-02 05:46:45 +00002636 dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy));
2637 BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]);
2638 if (CaseVal == 0 || DestBB == 0) {
2639 delete SI;
Rafael Espindola48da4f42013-11-04 16:16:24 +00002640 return Error(InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002641 }
2642 SI->addCase(CaseVal, DestBB);
2643 }
2644 I = SI;
2645 break;
2646 }
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00002647 case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...]
Chris Lattner3ed871f2009-10-27 19:13:16 +00002648 if (Record.size() < 2)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002649 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00002650 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002651 Value *Address = getValue(Record, 1, NextValueNo, OpTy);
Chris Lattner3ed871f2009-10-27 19:13:16 +00002652 if (OpTy == 0 || Address == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002653 return Error(InvalidRecord);
Chris Lattner3ed871f2009-10-27 19:13:16 +00002654 unsigned NumDests = Record.size()-2;
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00002655 IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests);
Chris Lattner3ed871f2009-10-27 19:13:16 +00002656 InstructionList.push_back(IBI);
2657 for (unsigned i = 0, e = NumDests; i != e; ++i) {
2658 if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) {
2659 IBI->addDestination(DestBB);
2660 } else {
2661 delete IBI;
Rafael Espindola48da4f42013-11-04 16:16:24 +00002662 return Error(InvalidRecord);
Chris Lattner3ed871f2009-10-27 19:13:16 +00002663 }
2664 }
2665 I = IBI;
2666 break;
2667 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002668
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00002669 case bitc::FUNC_CODE_INST_INVOKE: {
2670 // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
Rafael Espindola48da4f42013-11-04 16:16:24 +00002671 if (Record.size() < 4)
2672 return Error(InvalidRecord);
Bill Wendlinge94d8432012-12-07 23:16:57 +00002673 AttributeSet PAL = getAttributes(Record[0]);
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002674 unsigned CCInfo = Record[1];
2675 BasicBlock *NormalBB = getBasicBlock(Record[2]);
2676 BasicBlock *UnwindBB = getBasicBlock(Record[3]);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002677
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002678 unsigned OpNum = 4;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002679 Value *Callee;
2680 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002681 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002682
Chris Lattner229907c2011-07-18 04:54:35 +00002683 PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType());
2684 FunctionType *FTy = !CalleeTy ? 0 :
Chris Lattner5285b5e2007-05-02 05:46:45 +00002685 dyn_cast<FunctionType>(CalleeTy->getElementType());
2686
2687 // Check that the right number of fixed parameters are here.
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002688 if (FTy == 0 || NormalBB == 0 || UnwindBB == 0 ||
2689 Record.size() < OpNum+FTy->getNumParams())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002690 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002691
Chris Lattner5285b5e2007-05-02 05:46:45 +00002692 SmallVector<Value*, 16> Ops;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002693 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00002694 Ops.push_back(getValue(Record, OpNum, NextValueNo,
2695 FTy->getParamType(i)));
Rafael Espindola48da4f42013-11-04 16:16:24 +00002696 if (Ops.back() == 0)
2697 return Error(InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002698 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002699
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002700 if (!FTy->isVarArg()) {
2701 if (Record.size() != OpNum)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002702 return Error(InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002703 } else {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002704 // Read type/value pairs for varargs params.
2705 while (OpNum != Record.size()) {
2706 Value *Op;
2707 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002708 return Error(InvalidRecord);
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002709 Ops.push_back(Op);
2710 }
Chris Lattner5285b5e2007-05-02 05:46:45 +00002711 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002712
Jay Foad5bd375a2011-07-15 08:37:34 +00002713 I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops);
Devang Patelaf206b82009-09-18 19:26:43 +00002714 InstructionList.push_back(I);
Sandeep Patel68c5f472009-09-02 08:44:58 +00002715 cast<InvokeInst>(I)->setCallingConv(
2716 static_cast<CallingConv::ID>(CCInfo));
Devang Patel4c758ea2008-09-25 21:00:45 +00002717 cast<InvokeInst>(I)->setAttributes(PAL);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002718 break;
2719 }
Bill Wendlingf891bf82011-07-31 06:30:59 +00002720 case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval]
2721 unsigned Idx = 0;
2722 Value *Val = 0;
2723 if (getValueTypePair(Record, Idx, NextValueNo, Val))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002724 return Error(InvalidRecord);
Bill Wendlingf891bf82011-07-31 06:30:59 +00002725 I = ResumeInst::Create(Val);
Bill Wendlingb9a89992011-09-01 00:50:20 +00002726 InstructionList.push_back(I);
Bill Wendlingf891bf82011-07-31 06:30:59 +00002727 break;
2728 }
Chris Lattnere53603e2007-05-02 04:27:25 +00002729 case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE
Owen Anderson55f1c092009-08-13 21:58:54 +00002730 I = new UnreachableInst(Context);
Devang Patelaf206b82009-09-18 19:26:43 +00002731 InstructionList.push_back(I);
Chris Lattnere53603e2007-05-02 04:27:25 +00002732 break;
Chris Lattnere9759c22007-05-06 00:21:25 +00002733 case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...]
Chris Lattnere14cb882007-05-04 19:11:41 +00002734 if (Record.size() < 1 || ((Record.size()-1)&1))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002735 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00002736 Type *Ty = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00002737 if (!Ty)
2738 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002739
Jay Foad52131342011-03-30 11:28:46 +00002740 PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2);
Devang Patelaf206b82009-09-18 19:26:43 +00002741 InstructionList.push_back(PN);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002742
Chris Lattnere14cb882007-05-04 19:11:41 +00002743 for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00002744 Value *V;
2745 // With the new function encoding, it is possible that operands have
2746 // negative IDs (for forward references). Use a signed VBR
2747 // representation to keep the encoding small.
2748 if (UseRelativeIDs)
2749 V = getValueSigned(Record, 1+i, NextValueNo, Ty);
2750 else
2751 V = getValue(Record, 1+i, NextValueNo, Ty);
Chris Lattnere14cb882007-05-04 19:11:41 +00002752 BasicBlock *BB = getBasicBlock(Record[2+i]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00002753 if (!V || !BB)
2754 return Error(InvalidRecord);
Chris Lattnerc332bba2007-05-03 18:58:09 +00002755 PN->addIncoming(V, BB);
2756 }
2757 I = PN;
2758 break;
2759 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002760
Bill Wendlingfae14752011-08-12 20:24:12 +00002761 case bitc::FUNC_CODE_INST_LANDINGPAD: {
2762 // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?]
2763 unsigned Idx = 0;
2764 if (Record.size() < 4)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002765 return Error(InvalidRecord);
Bill Wendlingfae14752011-08-12 20:24:12 +00002766 Type *Ty = getTypeByID(Record[Idx++]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00002767 if (!Ty)
2768 return Error(InvalidRecord);
Bill Wendlingfae14752011-08-12 20:24:12 +00002769 Value *PersFn = 0;
2770 if (getValueTypePair(Record, Idx, NextValueNo, PersFn))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002771 return Error(InvalidRecord);
Bill Wendlingfae14752011-08-12 20:24:12 +00002772
2773 bool IsCleanup = !!Record[Idx++];
2774 unsigned NumClauses = Record[Idx++];
2775 LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, NumClauses);
2776 LP->setCleanup(IsCleanup);
2777 for (unsigned J = 0; J != NumClauses; ++J) {
2778 LandingPadInst::ClauseType CT =
2779 LandingPadInst::ClauseType(Record[Idx++]); (void)CT;
2780 Value *Val;
2781
2782 if (getValueTypePair(Record, Idx, NextValueNo, Val)) {
2783 delete LP;
Rafael Espindola48da4f42013-11-04 16:16:24 +00002784 return Error(InvalidRecord);
Bill Wendlingfae14752011-08-12 20:24:12 +00002785 }
2786
2787 assert((CT != LandingPadInst::Catch ||
2788 !isa<ArrayType>(Val->getType())) &&
2789 "Catch clause has a invalid type!");
2790 assert((CT != LandingPadInst::Filter ||
2791 isa<ArrayType>(Val->getType())) &&
2792 "Filter clause has invalid type!");
2793 LP->addClause(Val);
2794 }
2795
2796 I = LP;
Bill Wendlingb9a89992011-09-01 00:50:20 +00002797 InstructionList.push_back(I);
Bill Wendlingfae14752011-08-12 20:24:12 +00002798 break;
2799 }
2800
Chris Lattnerf1c87102011-06-17 18:09:11 +00002801 case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]
2802 if (Record.size() != 4)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002803 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00002804 PointerType *Ty =
Chris Lattnerc332bba2007-05-03 18:58:09 +00002805 dyn_cast_or_null<PointerType>(getTypeByID(Record[0]));
Chris Lattner229907c2011-07-18 04:54:35 +00002806 Type *OpTy = getTypeByID(Record[1]);
Chris Lattnerf1c87102011-06-17 18:09:11 +00002807 Value *Size = getFnValueByID(Record[2], OpTy);
2808 unsigned Align = Record[3];
Rafael Espindola48da4f42013-11-04 16:16:24 +00002809 if (!Ty || !Size)
2810 return Error(InvalidRecord);
Owen Anderson4fdeba92009-07-15 23:53:25 +00002811 I = new AllocaInst(Ty->getElementType(), Size, (1 << Align) >> 1);
Devang Patelaf206b82009-09-18 19:26:43 +00002812 InstructionList.push_back(I);
Chris Lattnerc332bba2007-05-03 18:58:09 +00002813 break;
2814 }
Chris Lattner9f600c52007-05-03 22:04:19 +00002815 case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol]
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002816 unsigned OpNum = 0;
2817 Value *Op;
2818 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
2819 OpNum+2 != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002820 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002821
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002822 I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1);
Devang Patelaf206b82009-09-18 19:26:43 +00002823 InstructionList.push_back(I);
Chris Lattner83930552007-05-01 07:01:57 +00002824 break;
Chris Lattner9f600c52007-05-03 22:04:19 +00002825 }
Eli Friedman59b66882011-08-09 23:02:53 +00002826 case bitc::FUNC_CODE_INST_LOADATOMIC: {
2827 // LOADATOMIC: [opty, op, align, vol, ordering, synchscope]
2828 unsigned OpNum = 0;
2829 Value *Op;
2830 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
2831 OpNum+4 != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002832 return Error(InvalidRecord);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002833
Eli Friedman59b66882011-08-09 23:02:53 +00002834
2835 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
2836 if (Ordering == NotAtomic || Ordering == Release ||
2837 Ordering == AcquireRelease)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002838 return Error(InvalidRecord);
Eli Friedman59b66882011-08-09 23:02:53 +00002839 if (Ordering != NotAtomic && Record[OpNum] == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002840 return Error(InvalidRecord);
Eli Friedman59b66882011-08-09 23:02:53 +00002841 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
2842
2843 I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1,
2844 Ordering, SynchScope);
2845 InstructionList.push_back(I);
2846 break;
2847 }
Chris Lattnerc44070802011-06-17 18:17:37 +00002848 case bitc::FUNC_CODE_INST_STORE: { // STORE2:[ptrty, ptr, val, align, vol]
Christopher Lamb54dd24c2007-12-11 08:59:05 +00002849 unsigned OpNum = 0;
2850 Value *Val, *Ptr;
2851 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002852 popValue(Record, OpNum, NextValueNo,
Christopher Lamb54dd24c2007-12-11 08:59:05 +00002853 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
2854 OpNum+2 != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002855 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002856
Christopher Lamb54dd24c2007-12-11 08:59:05 +00002857 I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1);
Devang Patelaf206b82009-09-18 19:26:43 +00002858 InstructionList.push_back(I);
Christopher Lamb54dd24c2007-12-11 08:59:05 +00002859 break;
2860 }
Eli Friedman59b66882011-08-09 23:02:53 +00002861 case bitc::FUNC_CODE_INST_STOREATOMIC: {
2862 // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, synchscope]
2863 unsigned OpNum = 0;
2864 Value *Val, *Ptr;
2865 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002866 popValue(Record, OpNum, NextValueNo,
Eli Friedman59b66882011-08-09 23:02:53 +00002867 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
2868 OpNum+4 != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002869 return Error(InvalidRecord);
Eli Friedman59b66882011-08-09 23:02:53 +00002870
2871 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
Eli Friedman222b5a42011-09-19 19:41:28 +00002872 if (Ordering == NotAtomic || Ordering == Acquire ||
Eli Friedman59b66882011-08-09 23:02:53 +00002873 Ordering == AcquireRelease)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002874 return Error(InvalidRecord);
Eli Friedman59b66882011-08-09 23:02:53 +00002875 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
2876 if (Ordering != NotAtomic && Record[OpNum] == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002877 return Error(InvalidRecord);
Eli Friedman59b66882011-08-09 23:02:53 +00002878
2879 I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1,
2880 Ordering, SynchScope);
2881 InstructionList.push_back(I);
2882 break;
2883 }
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002884 case bitc::FUNC_CODE_INST_CMPXCHG: {
Tim Northovere94a5182014-03-11 10:48:52 +00002885 // CMPXCHG:[ptrty, ptr, cmp, new, vol, successordering, synchscope,
2886 // failureordering]
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002887 unsigned OpNum = 0;
2888 Value *Ptr, *Cmp, *New;
2889 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002890 popValue(Record, OpNum, NextValueNo,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002891 cast<PointerType>(Ptr->getType())->getElementType(), Cmp) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002892 popValue(Record, OpNum, NextValueNo,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002893 cast<PointerType>(Ptr->getType())->getElementType(), New) ||
Tim Northovere94a5182014-03-11 10:48:52 +00002894 (OpNum + 3 != Record.size() && OpNum + 4 != Record.size()))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002895 return Error(InvalidRecord);
Tim Northovere94a5182014-03-11 10:48:52 +00002896 AtomicOrdering SuccessOrdering = GetDecodedOrdering(Record[OpNum+1]);
2897 if (SuccessOrdering == NotAtomic || SuccessOrdering == Unordered)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002898 return Error(InvalidRecord);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002899 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+2]);
Tim Northovere94a5182014-03-11 10:48:52 +00002900
2901 AtomicOrdering FailureOrdering;
2902 if (Record.size() < 7)
2903 FailureOrdering =
2904 AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering);
2905 else
2906 FailureOrdering = GetDecodedOrdering(Record[OpNum+3]);
2907
2908 I = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering, FailureOrdering,
2909 SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002910 cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]);
2911 InstructionList.push_back(I);
2912 break;
2913 }
2914 case bitc::FUNC_CODE_INST_ATOMICRMW: {
2915 // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, synchscope]
2916 unsigned OpNum = 0;
2917 Value *Ptr, *Val;
2918 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002919 popValue(Record, OpNum, NextValueNo,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002920 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
2921 OpNum+4 != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002922 return Error(InvalidRecord);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002923 AtomicRMWInst::BinOp Operation = GetDecodedRMWOperation(Record[OpNum]);
2924 if (Operation < AtomicRMWInst::FIRST_BINOP ||
2925 Operation > AtomicRMWInst::LAST_BINOP)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002926 return Error(InvalidRecord);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002927 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
Eli Friedman59b66882011-08-09 23:02:53 +00002928 if (Ordering == NotAtomic || Ordering == Unordered)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002929 return Error(InvalidRecord);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002930 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
2931 I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SynchScope);
2932 cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]);
2933 InstructionList.push_back(I);
2934 break;
2935 }
Eli Friedmanfee02c62011-07-25 23:16:38 +00002936 case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, synchscope]
2937 if (2 != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002938 return Error(InvalidRecord);
Eli Friedmanfee02c62011-07-25 23:16:38 +00002939 AtomicOrdering Ordering = GetDecodedOrdering(Record[0]);
2940 if (Ordering == NotAtomic || Ordering == Unordered ||
2941 Ordering == Monotonic)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002942 return Error(InvalidRecord);
Eli Friedmanfee02c62011-07-25 23:16:38 +00002943 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[1]);
2944 I = new FenceInst(Context, Ordering, SynchScope);
2945 InstructionList.push_back(I);
2946 break;
2947 }
Chris Lattnerc44070802011-06-17 18:17:37 +00002948 case bitc::FUNC_CODE_INST_CALL: {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00002949 // CALL: [paramattrs, cc, fnty, fnid, arg0, arg1...]
2950 if (Record.size() < 3)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002951 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002952
Bill Wendlinge94d8432012-12-07 23:16:57 +00002953 AttributeSet PAL = getAttributes(Record[0]);
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002954 unsigned CCInfo = Record[1];
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002955
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002956 unsigned OpNum = 2;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002957 Value *Callee;
2958 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002959 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002960
Chris Lattner229907c2011-07-18 04:54:35 +00002961 PointerType *OpTy = dyn_cast<PointerType>(Callee->getType());
2962 FunctionType *FTy = 0;
Chris Lattner9f600c52007-05-03 22:04:19 +00002963 if (OpTy) FTy = dyn_cast<FunctionType>(OpTy->getElementType());
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002964 if (!FTy || Record.size() < FTy->getNumParams()+OpNum)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002965 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002966
Chris Lattner9f600c52007-05-03 22:04:19 +00002967 SmallVector<Value*, 16> Args;
2968 // Read the fixed params.
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002969 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002970 if (FTy->getParamType(i)->isLabelTy())
Dale Johannesen4646aa32007-11-05 21:20:28 +00002971 Args.push_back(getBasicBlock(Record[OpNum]));
Dan Gohmanbbcd04d2010-09-13 18:00:48 +00002972 else
Jan Wen Voungafaced02012-10-11 20:20:40 +00002973 Args.push_back(getValue(Record, OpNum, NextValueNo,
2974 FTy->getParamType(i)));
Rafael Espindola48da4f42013-11-04 16:16:24 +00002975 if (Args.back() == 0)
2976 return Error(InvalidRecord);
Chris Lattner9f600c52007-05-03 22:04:19 +00002977 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002978
Chris Lattner9f600c52007-05-03 22:04:19 +00002979 // Read type/value pairs for varargs params.
Chris Lattner9f600c52007-05-03 22:04:19 +00002980 if (!FTy->isVarArg()) {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002981 if (OpNum != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002982 return Error(InvalidRecord);
Chris Lattner9f600c52007-05-03 22:04:19 +00002983 } else {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002984 while (OpNum != Record.size()) {
2985 Value *Op;
2986 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002987 return Error(InvalidRecord);
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002988 Args.push_back(Op);
Chris Lattner9f600c52007-05-03 22:04:19 +00002989 }
2990 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002991
Jay Foad5bd375a2011-07-15 08:37:34 +00002992 I = CallInst::Create(Callee, Args);
Devang Patelaf206b82009-09-18 19:26:43 +00002993 InstructionList.push_back(I);
Sandeep Patel68c5f472009-09-02 08:44:58 +00002994 cast<CallInst>(I)->setCallingConv(
2995 static_cast<CallingConv::ID>(CCInfo>>1));
Chris Lattner47045272007-05-03 22:34:03 +00002996 cast<CallInst>(I)->setTailCall(CCInfo & 1);
Devang Patel4c758ea2008-09-25 21:00:45 +00002997 cast<CallInst>(I)->setAttributes(PAL);
Chris Lattner9f600c52007-05-03 22:04:19 +00002998 break;
2999 }
3000 case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty]
3001 if (Record.size() < 3)
Rafael Espindola48da4f42013-11-04 16:16:24 +00003002 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00003003 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00003004 Value *Op = getValue(Record, 1, NextValueNo, OpTy);
Chris Lattner229907c2011-07-18 04:54:35 +00003005 Type *ResTy = getTypeByID(Record[2]);
Chris Lattner9f600c52007-05-03 22:04:19 +00003006 if (!OpTy || !Op || !ResTy)
Rafael Espindola48da4f42013-11-04 16:16:24 +00003007 return Error(InvalidRecord);
Chris Lattner9f600c52007-05-03 22:04:19 +00003008 I = new VAArgInst(Op, ResTy);
Devang Patelaf206b82009-09-18 19:26:43 +00003009 InstructionList.push_back(I);
Chris Lattner9f600c52007-05-03 22:04:19 +00003010 break;
3011 }
Chris Lattner83930552007-05-01 07:01:57 +00003012 }
3013
3014 // Add instruction to end of current BB. If there is no current BB, reject
3015 // this file.
3016 if (CurBB == 0) {
3017 delete I;
Rafael Espindola48da4f42013-11-04 16:16:24 +00003018 return Error(InvalidInstructionWithNoBB);
Chris Lattner83930552007-05-01 07:01:57 +00003019 }
3020 CurBB->getInstList().push_back(I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003021
Chris Lattner83930552007-05-01 07:01:57 +00003022 // If this was a terminator instruction, move to the next block.
3023 if (isa<TerminatorInst>(I)) {
3024 ++CurBBNo;
3025 CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : 0;
3026 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003027
Chris Lattner83930552007-05-01 07:01:57 +00003028 // Non-void values get registered in the value table for future use.
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00003029 if (I && !I->getType()->isVoidTy())
Chris Lattner83930552007-05-01 07:01:57 +00003030 ValueList.AssignValue(I, NextValueNo++);
Chris Lattner85b7b402007-05-01 05:52:21 +00003031 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003032
Chris Lattner27d38752013-01-20 02:13:19 +00003033OutOfRecordLoop:
Joe Abbey97b7a172013-02-06 22:14:06 +00003034
Chris Lattner83930552007-05-01 07:01:57 +00003035 // Check the function list for unresolved values.
3036 if (Argument *A = dyn_cast<Argument>(ValueList.back())) {
3037 if (A->getParent() == 0) {
3038 // We found at least one unresolved value. Nuke them all to avoid leaks.
3039 for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){
Dan Gohman950ad652010-08-25 20:20:21 +00003040 if ((A = dyn_cast<Argument>(ValueList[i])) && A->getParent() == 0) {
Owen Andersonb292b8c2009-07-30 23:03:37 +00003041 A->replaceAllUsesWith(UndefValue::get(A->getType()));
Chris Lattner83930552007-05-01 07:01:57 +00003042 delete A;
3043 }
3044 }
Rafael Espindola48da4f42013-11-04 16:16:24 +00003045 return Error(NeverResolvedValueFoundInFunction);
Chris Lattner83930552007-05-01 07:01:57 +00003046 }
Chris Lattner83930552007-05-01 07:01:57 +00003047 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003048
Dan Gohman9b9ff462010-08-25 20:23:38 +00003049 // FIXME: Check for unresolved forward-declared metadata references
3050 // and clean up leaks.
3051
Chris Lattner5956dc82009-10-28 05:53:48 +00003052 // See if anything took the address of blocks in this function. If so,
3053 // resolve them now.
Chris Lattner5956dc82009-10-28 05:53:48 +00003054 DenseMap<Function*, std::vector<BlockAddrRefTy> >::iterator BAFRI =
3055 BlockAddrFwdRefs.find(F);
3056 if (BAFRI != BlockAddrFwdRefs.end()) {
3057 std::vector<BlockAddrRefTy> &RefList = BAFRI->second;
3058 for (unsigned i = 0, e = RefList.size(); i != e; ++i) {
3059 unsigned BlockIdx = RefList[i].first;
Chris Lattneraa99c942009-11-01 01:27:45 +00003060 if (BlockIdx >= FunctionBBs.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00003061 return Error(InvalidID);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003062
Chris Lattner5956dc82009-10-28 05:53:48 +00003063 GlobalVariable *FwdRef = RefList[i].second;
Chris Lattneraa99c942009-11-01 01:27:45 +00003064 FwdRef->replaceAllUsesWith(BlockAddress::get(F, FunctionBBs[BlockIdx]));
Chris Lattner5956dc82009-10-28 05:53:48 +00003065 FwdRef->eraseFromParent();
3066 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003067
Chris Lattner5956dc82009-10-28 05:53:48 +00003068 BlockAddrFwdRefs.erase(BAFRI);
3069 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003070
Chris Lattner85b7b402007-05-01 05:52:21 +00003071 // Trim the value list down to the size it was before we parsed this function.
3072 ValueList.shrinkTo(ModuleValueListSize);
Dan Gohman26d837d2010-08-25 20:22:53 +00003073 MDValueList.shrinkTo(ModuleMDValueListSize);
Chris Lattner85b7b402007-05-01 05:52:21 +00003074 std::vector<BasicBlock*>().swap(FunctionBBs);
Rafael Espindola48da4f42013-11-04 16:16:24 +00003075 return error_code::success();
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003076}
3077
Rafael Espindola7d712032013-11-05 17:16:08 +00003078/// Find the function body in the bitcode stream
3079error_code BitcodeReader::FindFunctionInStream(Function *F,
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003080 DenseMap<Function*, uint64_t>::iterator DeferredFunctionInfoIterator) {
3081 while (DeferredFunctionInfoIterator->second == 0) {
3082 if (Stream.AtEndOfStream())
Rafael Espindola48da4f42013-11-04 16:16:24 +00003083 return Error(CouldNotFindFunctionInStream);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003084 // ParseModule will parse the next body in the stream and set its
3085 // position in the DeferredFunctionInfo map.
Rafael Espindola7d712032013-11-05 17:16:08 +00003086 if (error_code EC = ParseModule(true))
3087 return EC;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003088 }
Rafael Espindola7d712032013-11-05 17:16:08 +00003089 return error_code::success();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003090}
3091
Chris Lattner9eeada92007-05-18 04:02:46 +00003092//===----------------------------------------------------------------------===//
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003093// GVMaterializer implementation
Chris Lattner9eeada92007-05-18 04:02:46 +00003094//===----------------------------------------------------------------------===//
3095
3096
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003097bool BitcodeReader::isMaterializable(const GlobalValue *GV) const {
3098 if (const Function *F = dyn_cast<Function>(GV)) {
3099 return F->isDeclaration() &&
3100 DeferredFunctionInfo.count(const_cast<Function*>(F));
3101 }
3102 return false;
3103}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003104
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003105error_code BitcodeReader::Materialize(GlobalValue *GV) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003106 Function *F = dyn_cast<Function>(GV);
3107 // If it's not a function or is already material, ignore the request.
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003108 if (!F || !F->isMaterializable())
3109 return error_code::success();
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003110
3111 DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F);
Chris Lattner9eeada92007-05-18 04:02:46 +00003112 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003113 // If its position is recorded as 0, its body is somewhere in the stream
3114 // but we haven't seen it yet.
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003115 if (DFII->second == 0 && LazyStreamer)
3116 if (error_code EC = FindFunctionInStream(F, DFII))
3117 return EC;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003118
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003119 // Move the bit stream to the saved position of the deferred function body.
3120 Stream.JumpToBit(DFII->second);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003121
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003122 if (error_code EC = ParseFunctionBody(F))
3123 return EC;
Chandler Carruth7132e002007-08-04 01:51:18 +00003124
3125 // Upgrade any old intrinsic calls in the function.
3126 for (UpgradedIntrinsicMap::iterator I = UpgradedIntrinsics.begin(),
3127 E = UpgradedIntrinsics.end(); I != E; ++I) {
3128 if (I->first != I->second) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00003129 for (auto UI = I->first->user_begin(), UE = I->first->user_end();
3130 UI != UE;) {
Chandler Carruth7132e002007-08-04 01:51:18 +00003131 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
3132 UpgradeIntrinsicCall(CI, I->second);
3133 }
3134 }
3135 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003136
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003137 return error_code::success();
Chris Lattner9eeada92007-05-18 04:02:46 +00003138}
3139
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003140bool BitcodeReader::isDematerializable(const GlobalValue *GV) const {
3141 const Function *F = dyn_cast<Function>(GV);
3142 if (!F || F->isDeclaration())
3143 return false;
3144 return DeferredFunctionInfo.count(const_cast<Function*>(F));
3145}
3146
3147void BitcodeReader::Dematerialize(GlobalValue *GV) {
3148 Function *F = dyn_cast<Function>(GV);
3149 // If this function isn't dematerializable, this is a noop.
3150 if (!F || !isDematerializable(F))
Chris Lattner9eeada92007-05-18 04:02:46 +00003151 return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003152
Chris Lattner9eeada92007-05-18 04:02:46 +00003153 assert(DeferredFunctionInfo.count(F) && "No info to read function later?");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003154
Chris Lattner9eeada92007-05-18 04:02:46 +00003155 // Just forget the function body, we can remat it later.
3156 F->deleteBody();
Chris Lattner9eeada92007-05-18 04:02:46 +00003157}
3158
3159
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003160error_code BitcodeReader::MaterializeModule(Module *M) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003161 assert(M == TheModule &&
3162 "Can only Materialize the Module this BitcodeReader is attached to.");
Chris Lattner06310bf2009-06-16 05:15:21 +00003163 // Iterate over the module, deserializing any functions that are still on
3164 // disk.
3165 for (Module::iterator F = TheModule->begin(), E = TheModule->end();
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003166 F != E; ++F) {
3167 if (F->isMaterializable()) {
3168 if (error_code EC = Materialize(F))
3169 return EC;
3170 }
3171 }
Derek Schuff92ef9752012-02-29 00:07:09 +00003172 // At this point, if there are any function bodies, the current bit is
3173 // pointing to the END_BLOCK record after them. Now make sure the rest
3174 // of the bits in the module have been read.
3175 if (NextUnreadBit)
3176 ParseModule(true);
3177
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003178 // Upgrade any intrinsic calls that slipped through (should not happen!) and
3179 // delete the old functions to clean up. We can't do this unless the entire
3180 // module is materialized because there could always be another function body
Chandler Carruth7132e002007-08-04 01:51:18 +00003181 // with calls to the old function.
3182 for (std::vector<std::pair<Function*, Function*> >::iterator I =
3183 UpgradedIntrinsics.begin(), E = UpgradedIntrinsics.end(); I != E; ++I) {
3184 if (I->first != I->second) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00003185 for (auto UI = I->first->user_begin(), UE = I->first->user_end();
3186 UI != UE;) {
Chandler Carruth7132e002007-08-04 01:51:18 +00003187 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
3188 UpgradeIntrinsicCall(CI, I->second);
3189 }
Chris Lattner647cffb2009-04-01 01:43:03 +00003190 if (!I->first->use_empty())
3191 I->first->replaceAllUsesWith(I->second);
Chandler Carruth7132e002007-08-04 01:51:18 +00003192 I->first->eraseFromParent();
3193 }
3194 }
3195 std::vector<std::pair<Function*, Function*> >().swap(UpgradedIntrinsics);
Devang Patel80ae3492009-08-28 23:24:31 +00003196
Manman Ren209b17c2013-09-28 00:22:27 +00003197 for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++)
3198 UpgradeInstWithTBAATag(InstsWithTBAATag[I]);
3199
Manman Ren8b4306c2013-12-02 21:29:56 +00003200 UpgradeDebugInfo(*M);
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003201 return error_code::success();
Chris Lattner9eeada92007-05-18 04:02:46 +00003202}
3203
Rafael Espindola48da4f42013-11-04 16:16:24 +00003204error_code BitcodeReader::InitStream() {
3205 if (LazyStreamer)
3206 return InitLazyStream();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003207 return InitStreamFromBuffer();
3208}
3209
Rafael Espindola48da4f42013-11-04 16:16:24 +00003210error_code BitcodeReader::InitStreamFromBuffer() {
Roman Divacky4717a8d2012-09-06 15:42:13 +00003211 const unsigned char *BufPtr = (const unsigned char*)Buffer->getBufferStart();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003212 const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
3213
3214 if (Buffer->getBufferSize() & 3) {
3215 if (!isRawBitcode(BufPtr, BufEnd) && !isBitcodeWrapper(BufPtr, BufEnd))
Rafael Espindola48da4f42013-11-04 16:16:24 +00003216 return Error(InvalidBitcodeSignature);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003217 else
Rafael Espindola48da4f42013-11-04 16:16:24 +00003218 return Error(BitcodeStreamInvalidSize);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003219 }
3220
3221 // If we have a wrapper header, parse it and ignore the non-bc file contents.
3222 // The magic number is 0x0B17C0DE stored in little endian.
3223 if (isBitcodeWrapper(BufPtr, BufEnd))
3224 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
Rafael Espindola48da4f42013-11-04 16:16:24 +00003225 return Error(InvalidBitcodeWrapperHeader);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003226
3227 StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
3228 Stream.init(*StreamFile);
3229
Rafael Espindola48da4f42013-11-04 16:16:24 +00003230 return error_code::success();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003231}
3232
Rafael Espindola48da4f42013-11-04 16:16:24 +00003233error_code BitcodeReader::InitLazyStream() {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003234 // Check and strip off the bitcode wrapper; BitstreamReader expects never to
3235 // see it.
3236 StreamingMemoryObject *Bytes = new StreamingMemoryObject(LazyStreamer);
3237 StreamFile.reset(new BitstreamReader(Bytes));
3238 Stream.init(*StreamFile);
3239
3240 unsigned char buf[16];
Benjamin Kramer534d3a42013-05-24 10:54:58 +00003241 if (Bytes->readBytes(0, 16, buf) == -1)
Rafael Espindola48da4f42013-11-04 16:16:24 +00003242 return Error(BitcodeStreamInvalidSize);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003243
3244 if (!isBitcode(buf, buf + 16))
Rafael Espindola48da4f42013-11-04 16:16:24 +00003245 return Error(InvalidBitcodeSignature);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003246
3247 if (isBitcodeWrapper(buf, buf + 4)) {
3248 const unsigned char *bitcodeStart = buf;
3249 const unsigned char *bitcodeEnd = buf + 16;
3250 SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
3251 Bytes->dropLeadingBytes(bitcodeStart - buf);
3252 Bytes->setKnownObjectSize(bitcodeEnd - bitcodeStart);
3253 }
Rafael Espindola48da4f42013-11-04 16:16:24 +00003254 return error_code::success();
3255}
3256
3257namespace {
3258class BitcodeErrorCategoryType : public _do_message {
Craig Topper73156022014-03-02 09:09:27 +00003259 const char *name() const override {
Rafael Espindola48da4f42013-11-04 16:16:24 +00003260 return "llvm.bitcode";
3261 }
Craig Topper73156022014-03-02 09:09:27 +00003262 std::string message(int IE) const override {
Rafael Espindola48da4f42013-11-04 16:16:24 +00003263 BitcodeReader::ErrorType E = static_cast<BitcodeReader::ErrorType>(IE);
3264 switch (E) {
3265 case BitcodeReader::BitcodeStreamInvalidSize:
3266 return "Bitcode stream length should be >= 16 bytes and a multiple of 4";
3267 case BitcodeReader::ConflictingMETADATA_KINDRecords:
3268 return "Conflicting METADATA_KIND records";
3269 case BitcodeReader::CouldNotFindFunctionInStream:
3270 return "Could not find function in stream";
3271 case BitcodeReader::ExpectedConstant:
3272 return "Expected a constant";
3273 case BitcodeReader::InsufficientFunctionProtos:
3274 return "Insufficient function protos";
3275 case BitcodeReader::InvalidBitcodeSignature:
3276 return "Invalid bitcode signature";
3277 case BitcodeReader::InvalidBitcodeWrapperHeader:
3278 return "Invalid bitcode wrapper header";
3279 case BitcodeReader::InvalidConstantReference:
3280 return "Invalid ronstant reference";
3281 case BitcodeReader::InvalidID:
3282 return "Invalid ID";
3283 case BitcodeReader::InvalidInstructionWithNoBB:
3284 return "Invalid instruction with no BB";
3285 case BitcodeReader::InvalidRecord:
3286 return "Invalid record";
3287 case BitcodeReader::InvalidTypeForValue:
3288 return "Invalid type for value";
3289 case BitcodeReader::InvalidTYPETable:
3290 return "Invalid TYPE table";
3291 case BitcodeReader::InvalidType:
3292 return "Invalid type";
3293 case BitcodeReader::MalformedBlock:
3294 return "Malformed block";
3295 case BitcodeReader::MalformedGlobalInitializerSet:
3296 return "Malformed global initializer set";
3297 case BitcodeReader::InvalidMultipleBlocks:
3298 return "Invalid multiple blocks";
3299 case BitcodeReader::NeverResolvedValueFoundInFunction:
3300 return "Never resolved value found in function";
3301 case BitcodeReader::InvalidValue:
3302 return "Invalid value";
3303 }
Benjamin Kramer77db1632013-11-05 13:45:09 +00003304 llvm_unreachable("Unknown error type!");
Rafael Espindola48da4f42013-11-04 16:16:24 +00003305 }
3306};
3307}
3308
3309const error_category &BitcodeReader::BitcodeErrorCategory() {
3310 static BitcodeErrorCategoryType O;
3311 return O;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003312}
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003313
Chris Lattner6694f602007-04-29 07:54:31 +00003314//===----------------------------------------------------------------------===//
3315// External interface
3316//===----------------------------------------------------------------------===//
3317
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003318/// getLazyBitcodeModule - lazy function-at-a-time loading from a file.
Chris Lattner6694f602007-04-29 07:54:31 +00003319///
Rafael Espindola5b6c1e82014-01-13 18:31:04 +00003320ErrorOr<Module *> llvm::getLazyBitcodeModule(MemoryBuffer *Buffer,
3321 LLVMContext &Context) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003322 Module *M = new Module(Buffer->getBufferIdentifier(), Context);
Owen Anderson6773d382009-07-01 16:58:40 +00003323 BitcodeReader *R = new BitcodeReader(Buffer, Context);
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003324 M->setMaterializer(R);
Rafael Espindola48da4f42013-11-04 16:16:24 +00003325 if (error_code EC = R->ParseBitcodeInto(M)) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003326 delete M; // Also deletes R.
Rafael Espindola5b6c1e82014-01-13 18:31:04 +00003327 return EC;
Chris Lattner6694f602007-04-29 07:54:31 +00003328 }
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003329 // Have the BitcodeReader dtor delete 'Buffer'.
3330 R->setBufferOwned(true);
Rafael Espindolab7993462012-01-02 07:49:53 +00003331
3332 R->materializeForwardReferencedFunctions();
3333
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003334 return M;
Chris Lattner6694f602007-04-29 07:54:31 +00003335}
3336
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003337
3338Module *llvm::getStreamedBitcodeModule(const std::string &name,
3339 DataStreamer *streamer,
3340 LLVMContext &Context,
3341 std::string *ErrMsg) {
3342 Module *M = new Module(name, Context);
3343 BitcodeReader *R = new BitcodeReader(streamer, Context);
3344 M->setMaterializer(R);
Rafael Espindola48da4f42013-11-04 16:16:24 +00003345 if (error_code EC = R->ParseBitcodeInto(M)) {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003346 if (ErrMsg)
Rafael Espindola48da4f42013-11-04 16:16:24 +00003347 *ErrMsg = EC.message();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003348 delete M; // Also deletes R.
3349 return 0;
3350 }
3351 R->setBufferOwned(false); // no buffer to delete
3352 return M;
3353}
3354
Rafael Espindola8f31e212014-01-15 01:08:23 +00003355ErrorOr<Module *> llvm::parseBitcodeFile(MemoryBuffer *Buffer,
3356 LLVMContext &Context) {
Rafael Espindola5b6c1e82014-01-13 18:31:04 +00003357 ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
Rafael Espindola8f31e212014-01-15 01:08:23 +00003358 if (!ModuleOrErr)
3359 return ModuleOrErr;
Rafael Espindola5b6c1e82014-01-13 18:31:04 +00003360 Module *M = ModuleOrErr.get();
Chris Lattner9eeada92007-05-18 04:02:46 +00003361
3362 // Don't let the BitcodeReader dtor delete 'Buffer', regardless of whether
3363 // there was an error.
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003364 static_cast<BitcodeReader*>(M->getMaterializer())->setBufferOwned(false);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003365
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003366 // Read in the entire module, and destroy the BitcodeReader.
Rafael Espindolae9fab9b2014-01-14 23:51:27 +00003367 if (error_code EC = M->materializeAllPermanently()) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003368 delete M;
Rafael Espindola8f31e212014-01-15 01:08:23 +00003369 return EC;
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003370 }
Bill Wendling0198ce02010-10-06 01:22:42 +00003371
Chad Rosierca2567b2011-12-07 21:44:12 +00003372 // TODO: Restore the use-lists to the in-memory state when the bitcode was
3373 // written. We must defer until the Module has been fully materialized.
3374
Chris Lattner6694f602007-04-29 07:54:31 +00003375 return M;
3376}
Bill Wendling0198ce02010-10-06 01:22:42 +00003377
3378std::string llvm::getBitcodeTargetTriple(MemoryBuffer *Buffer,
3379 LLVMContext& Context,
3380 std::string *ErrMsg) {
3381 BitcodeReader *R = new BitcodeReader(Buffer, Context);
3382 // Don't let the BitcodeReader dtor delete 'Buffer'.
3383 R->setBufferOwned(false);
3384
3385 std::string Triple("");
Rafael Espindola48da4f42013-11-04 16:16:24 +00003386 if (error_code EC = R->ParseTriple(Triple))
Bill Wendling0198ce02010-10-06 01:22:42 +00003387 if (ErrMsg)
Rafael Espindola48da4f42013-11-04 16:16:24 +00003388 *ErrMsg = EC.message();
Bill Wendling0198ce02010-10-06 01:22:42 +00003389
3390 delete R;
3391 return Triple;
3392}