blob: 4bbecfdb1777a5da0f1388c40d95c270f41642e3 [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() {
Craig Topper2617dcc2014-04-15 06:32:26 +000042 Buffer = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000043 std::vector<Type*>().swap(TypeList);
Chris Lattner9eeada92007-05-18 04:02:46 +000044 ValueList.clear();
Devang Patel05eb6172009-08-04 06:00:18 +000045 MDValueList.clear();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000046
Bill Wendlinge94d8432012-12-07 23:16:57 +000047 std::vector<AttributeSet>().swap(MAttributes);
Chris Lattner9eeada92007-05-18 04:02:46 +000048 std::vector<BasicBlock*>().swap(FunctionBBs);
49 std::vector<Function*>().swap(FunctionsWithBodies);
50 DeferredFunctionInfo.clear();
Dan Gohman43aa8f02010-07-20 21:42:28 +000051 MDKindMap.clear();
Benjamin Kramer736a4fc2012-09-21 14:34:31 +000052
53 assert(BlockAddrFwdRefs.empty() && "Unresolved blockaddress fwd references");
Chris Lattner6694f602007-04-29 07:54:31 +000054}
55
Chris Lattnerfee5a372007-05-04 03:30:17 +000056//===----------------------------------------------------------------------===//
57// Helper functions to implement forward reference resolution, etc.
58//===----------------------------------------------------------------------===//
Chris Lattner6694f602007-04-29 07:54:31 +000059
Chris Lattner1314b992007-04-22 06:23:29 +000060/// ConvertToString - Convert a string from a record into an std::string, return
61/// true on failure.
Chris Lattnerccaa4482007-04-23 21:26:05 +000062template<typename StrTy>
Benjamin Kramer9704ed02012-05-28 14:10:31 +000063static bool ConvertToString(ArrayRef<uint64_t> Record, unsigned Idx,
Chris Lattnerccaa4482007-04-23 21:26:05 +000064 StrTy &Result) {
Chris Lattnere14cb882007-05-04 19:11:41 +000065 if (Idx > Record.size())
Chris Lattner1314b992007-04-22 06:23:29 +000066 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000067
Chris Lattnere14cb882007-05-04 19:11:41 +000068 for (unsigned i = Idx, e = Record.size(); i != e; ++i)
69 Result += (char)Record[i];
Chris Lattner1314b992007-04-22 06:23:29 +000070 return false;
71}
72
73static GlobalValue::LinkageTypes GetDecodedLinkage(unsigned Val) {
74 switch (Val) {
75 default: // Map unknown/new linkages to external
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +000076 case 0: return GlobalValue::ExternalLinkage;
77 case 1: return GlobalValue::WeakAnyLinkage;
78 case 2: return GlobalValue::AppendingLinkage;
79 case 3: return GlobalValue::InternalLinkage;
80 case 4: return GlobalValue::LinkOnceAnyLinkage;
Nico Rieck7157bb72014-01-14 15:22:47 +000081 case 5: return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage
82 case 6: return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +000083 case 7: return GlobalValue::ExternalWeakLinkage;
84 case 8: return GlobalValue::CommonLinkage;
85 case 9: return GlobalValue::PrivateLinkage;
Duncan Sands12da8ce2009-03-07 15:45:40 +000086 case 10: return GlobalValue::WeakODRLinkage;
87 case 11: return GlobalValue::LinkOnceODRLinkage;
Chris Lattner184f1be2009-04-13 05:44:34 +000088 case 12: return GlobalValue::AvailableExternallyLinkage;
Rafael Espindola2fb5bc32014-03-13 23:18:37 +000089 case 13:
90 return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage
91 case 14:
92 return GlobalValue::PrivateLinkage; // Obsolete 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];
Craig Topper2617dcc2014-04-15 06:32:26 +0000259 if (!OldV) {
Chris Lattner2d8cd802009-03-31 22:55:09 +0000260 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]) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000299 assert((!Ty || Ty == V->getType()) && "Type mismatch in value table!");
Chris Lattner83930552007-05-01 07:01:57 +0000300 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.
Craig Topper2617dcc2014-04-15 06:32:26 +0000304 if (!Ty) return nullptr;
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];
Craig Topper2617dcc2014-04-15 06:32:26 +0000404 if (!OldV) {
Devang Patel05eb6172009-08-04 06:00:18 +0000405 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())
Craig Topper2617dcc2014-04-15 06:32:26 +0000436 return nullptr;
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 Espindoladb4ed0b2014-06-13 02:24:39 +0000471std::error_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 Espindoladb4ed0b2014-06-13 02:24:39 +0000491 return std::error_code();
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;
Tom Roeder44cb65f2014-06-05 19:29:43 +0000550 case bitc::ATTR_KIND_JUMP_TABLE:
551 return Attribute::JumpTable;
Reid Klecknere9f36af2013-11-12 01:31:00 +0000552 case bitc::ATTR_KIND_MIN_SIZE:
553 return Attribute::MinSize;
554 case bitc::ATTR_KIND_NAKED:
555 return Attribute::Naked;
556 case bitc::ATTR_KIND_NEST:
557 return Attribute::Nest;
558 case bitc::ATTR_KIND_NO_ALIAS:
559 return Attribute::NoAlias;
560 case bitc::ATTR_KIND_NO_BUILTIN:
561 return Attribute::NoBuiltin;
562 case bitc::ATTR_KIND_NO_CAPTURE:
563 return Attribute::NoCapture;
564 case bitc::ATTR_KIND_NO_DUPLICATE:
565 return Attribute::NoDuplicate;
566 case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT:
567 return Attribute::NoImplicitFloat;
568 case bitc::ATTR_KIND_NO_INLINE:
569 return Attribute::NoInline;
570 case bitc::ATTR_KIND_NON_LAZY_BIND:
571 return Attribute::NonLazyBind;
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000572 case bitc::ATTR_KIND_NON_NULL:
573 return Attribute::NonNull;
Reid Klecknere9f36af2013-11-12 01:31:00 +0000574 case bitc::ATTR_KIND_NO_RED_ZONE:
575 return Attribute::NoRedZone;
576 case bitc::ATTR_KIND_NO_RETURN:
577 return Attribute::NoReturn;
578 case bitc::ATTR_KIND_NO_UNWIND:
579 return Attribute::NoUnwind;
580 case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE:
581 return Attribute::OptimizeForSize;
582 case bitc::ATTR_KIND_OPTIMIZE_NONE:
583 return Attribute::OptimizeNone;
584 case bitc::ATTR_KIND_READ_NONE:
585 return Attribute::ReadNone;
586 case bitc::ATTR_KIND_READ_ONLY:
587 return Attribute::ReadOnly;
588 case bitc::ATTR_KIND_RETURNED:
589 return Attribute::Returned;
590 case bitc::ATTR_KIND_RETURNS_TWICE:
591 return Attribute::ReturnsTwice;
592 case bitc::ATTR_KIND_S_EXT:
593 return Attribute::SExt;
594 case bitc::ATTR_KIND_STACK_ALIGNMENT:
595 return Attribute::StackAlignment;
596 case bitc::ATTR_KIND_STACK_PROTECT:
597 return Attribute::StackProtect;
598 case bitc::ATTR_KIND_STACK_PROTECT_REQ:
599 return Attribute::StackProtectReq;
600 case bitc::ATTR_KIND_STACK_PROTECT_STRONG:
601 return Attribute::StackProtectStrong;
602 case bitc::ATTR_KIND_STRUCT_RET:
603 return Attribute::StructRet;
604 case bitc::ATTR_KIND_SANITIZE_ADDRESS:
605 return Attribute::SanitizeAddress;
606 case bitc::ATTR_KIND_SANITIZE_THREAD:
607 return Attribute::SanitizeThread;
608 case bitc::ATTR_KIND_SANITIZE_MEMORY:
609 return Attribute::SanitizeMemory;
610 case bitc::ATTR_KIND_UW_TABLE:
611 return Attribute::UWTable;
612 case bitc::ATTR_KIND_Z_EXT:
613 return Attribute::ZExt;
614 }
615}
616
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000617std::error_code BitcodeReader::ParseAttrKind(uint64_t Code,
618 Attribute::AttrKind *Kind) {
Reid Klecknere9f36af2013-11-12 01:31:00 +0000619 *Kind = GetAttrFromCode(Code);
620 if (*Kind == Attribute::None)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000621 return Error(InvalidValue);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000622 return std::error_code();
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000623}
624
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000625std::error_code BitcodeReader::ParseAttributeGroupBlock() {
Bill Wendlingba629332013-02-10 23:24:25 +0000626 if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +0000627 return Error(InvalidRecord);
Bill Wendlingba629332013-02-10 23:24:25 +0000628
629 if (!MAttributeGroups.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +0000630 return Error(InvalidMultipleBlocks);
Bill Wendlingba629332013-02-10 23:24:25 +0000631
632 SmallVector<uint64_t, 64> Record;
633
634 // Read all the records.
635 while (1) {
636 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
637
638 switch (Entry.Kind) {
639 case BitstreamEntry::SubBlock: // Handled for us already.
640 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +0000641 return Error(MalformedBlock);
Bill Wendlingba629332013-02-10 23:24:25 +0000642 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000643 return std::error_code();
Bill Wendlingba629332013-02-10 23:24:25 +0000644 case BitstreamEntry::Record:
645 // The interesting case.
646 break;
647 }
648
649 // Read a record.
650 Record.clear();
651 switch (Stream.readRecord(Entry.ID, Record)) {
652 default: // Default behavior: ignore.
653 break;
654 case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...]
655 if (Record.size() < 3)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000656 return Error(InvalidRecord);
Bill Wendlingba629332013-02-10 23:24:25 +0000657
Bill Wendlinge46707e2013-02-11 22:32:29 +0000658 uint64_t GrpID = Record[0];
Bill Wendlingba629332013-02-10 23:24:25 +0000659 uint64_t Idx = Record[1]; // Index of the object this attribute refers to.
660
661 AttrBuilder B;
662 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
663 if (Record[i] == 0) { // Enum attribute
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000664 Attribute::AttrKind Kind;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000665 if (std::error_code EC = ParseAttrKind(Record[++i], &Kind))
Rafael Espindola48da4f42013-11-04 16:16:24 +0000666 return EC;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000667
668 B.addAttribute(Kind);
Bill Wendlingba629332013-02-10 23:24:25 +0000669 } else if (Record[i] == 1) { // Align attribute
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000670 Attribute::AttrKind Kind;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000671 if (std::error_code EC = ParseAttrKind(Record[++i], &Kind))
Rafael Espindola48da4f42013-11-04 16:16:24 +0000672 return EC;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000673 if (Kind == Attribute::Alignment)
Bill Wendlingba629332013-02-10 23:24:25 +0000674 B.addAlignmentAttr(Record[++i]);
675 else
676 B.addStackAlignmentAttr(Record[++i]);
677 } else { // String attribute
Bill Wendlinge46707e2013-02-11 22:32:29 +0000678 assert((Record[i] == 3 || Record[i] == 4) &&
679 "Invalid attribute group entry");
Bill Wendlingba629332013-02-10 23:24:25 +0000680 bool HasValue = (Record[i++] == 4);
681 SmallString<64> KindStr;
682 SmallString<64> ValStr;
683
684 while (Record[i] != 0 && i != e)
685 KindStr += Record[i++];
Bill Wendlinge46707e2013-02-11 22:32:29 +0000686 assert(Record[i] == 0 && "Kind string not null terminated");
Bill Wendlingba629332013-02-10 23:24:25 +0000687
688 if (HasValue) {
689 // Has a value associated with it.
Bill Wendlinge46707e2013-02-11 22:32:29 +0000690 ++i; // Skip the '0' that terminates the "kind" string.
Bill Wendlingba629332013-02-10 23:24:25 +0000691 while (Record[i] != 0 && i != e)
692 ValStr += Record[i++];
Bill Wendlinge46707e2013-02-11 22:32:29 +0000693 assert(Record[i] == 0 && "Value string not null terminated");
Bill Wendlingba629332013-02-10 23:24:25 +0000694 }
695
696 B.addAttribute(KindStr.str(), ValStr.str());
697 }
698 }
699
Bill Wendlinge46707e2013-02-11 22:32:29 +0000700 MAttributeGroups[GrpID] = AttributeSet::get(Context, Idx, B);
Bill Wendlingba629332013-02-10 23:24:25 +0000701 break;
702 }
703 }
704 }
705}
706
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000707std::error_code BitcodeReader::ParseTypeTable() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000708 if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW))
Rafael Espindola48da4f42013-11-04 16:16:24 +0000709 return Error(InvalidRecord);
Derek Schuff206dddd2012-02-06 19:03:04 +0000710
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000711 return ParseTypeTableBody();
712}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000713
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000714std::error_code BitcodeReader::ParseTypeTableBody() {
Chris Lattner1314b992007-04-22 06:23:29 +0000715 if (!TypeList.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +0000716 return Error(InvalidMultipleBlocks);
Chris Lattner1314b992007-04-22 06:23:29 +0000717
718 SmallVector<uint64_t, 64> Record;
719 unsigned NumRecords = 0;
720
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000721 SmallString<64> TypeName;
Derek Schuff206dddd2012-02-06 19:03:04 +0000722
Chris Lattner1314b992007-04-22 06:23:29 +0000723 // Read all the records for this type table.
724 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +0000725 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +0000726
Chris Lattner27d38752013-01-20 02:13:19 +0000727 switch (Entry.Kind) {
728 case BitstreamEntry::SubBlock: // Handled for us already.
729 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +0000730 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +0000731 case BitstreamEntry::EndBlock:
Chris Lattner1314b992007-04-22 06:23:29 +0000732 if (NumRecords != TypeList.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +0000733 return Error(MalformedBlock);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000734 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +0000735 case BitstreamEntry::Record:
736 // The interesting case.
737 break;
Chris Lattner1314b992007-04-22 06:23:29 +0000738 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000739
Chris Lattner1314b992007-04-22 06:23:29 +0000740 // Read a record.
741 Record.clear();
Craig Topper2617dcc2014-04-15 06:32:26 +0000742 Type *ResultTy = nullptr;
Chris Lattner27d38752013-01-20 02:13:19 +0000743 switch (Stream.readRecord(Entry.ID, Record)) {
Rafael Espindola48da4f42013-11-04 16:16:24 +0000744 default:
745 return Error(InvalidValue);
Chris Lattner1314b992007-04-22 06:23:29 +0000746 case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
747 // TYPE_CODE_NUMENTRY contains a count of the number of types in the
748 // type list. This allows us to reserve space.
749 if (Record.size() < 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000750 return Error(InvalidRecord);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000751 TypeList.resize(Record[0]);
Chris Lattner1314b992007-04-22 06:23:29 +0000752 continue;
Chris Lattner1314b992007-04-22 06:23:29 +0000753 case bitc::TYPE_CODE_VOID: // VOID
Owen Anderson55f1c092009-08-13 21:58:54 +0000754 ResultTy = Type::getVoidTy(Context);
Chris Lattner1314b992007-04-22 06:23:29 +0000755 break;
Dan Gohman518cda42011-12-17 00:04:22 +0000756 case bitc::TYPE_CODE_HALF: // HALF
757 ResultTy = Type::getHalfTy(Context);
758 break;
Chris Lattner1314b992007-04-22 06:23:29 +0000759 case bitc::TYPE_CODE_FLOAT: // FLOAT
Owen Anderson55f1c092009-08-13 21:58:54 +0000760 ResultTy = Type::getFloatTy(Context);
Chris Lattner1314b992007-04-22 06:23:29 +0000761 break;
762 case bitc::TYPE_CODE_DOUBLE: // DOUBLE
Owen Anderson55f1c092009-08-13 21:58:54 +0000763 ResultTy = Type::getDoubleTy(Context);
Chris Lattner1314b992007-04-22 06:23:29 +0000764 break;
Dale Johannesenff4c3be2007-08-03 01:03:46 +0000765 case bitc::TYPE_CODE_X86_FP80: // X86_FP80
Owen Anderson55f1c092009-08-13 21:58:54 +0000766 ResultTy = Type::getX86_FP80Ty(Context);
Dale Johannesenff4c3be2007-08-03 01:03:46 +0000767 break;
768 case bitc::TYPE_CODE_FP128: // FP128
Owen Anderson55f1c092009-08-13 21:58:54 +0000769 ResultTy = Type::getFP128Ty(Context);
Dale Johannesenff4c3be2007-08-03 01:03:46 +0000770 break;
771 case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128
Owen Anderson55f1c092009-08-13 21:58:54 +0000772 ResultTy = Type::getPPC_FP128Ty(Context);
Dale Johannesenff4c3be2007-08-03 01:03:46 +0000773 break;
Chris Lattner1314b992007-04-22 06:23:29 +0000774 case bitc::TYPE_CODE_LABEL: // LABEL
Owen Anderson55f1c092009-08-13 21:58:54 +0000775 ResultTy = Type::getLabelTy(Context);
Chris Lattner1314b992007-04-22 06:23:29 +0000776 break;
Nick Lewyckyadbc2842009-05-30 05:06:04 +0000777 case bitc::TYPE_CODE_METADATA: // METADATA
Owen Anderson55f1c092009-08-13 21:58:54 +0000778 ResultTy = Type::getMetadataTy(Context);
Nick Lewyckyadbc2842009-05-30 05:06:04 +0000779 break;
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000780 case bitc::TYPE_CODE_X86_MMX: // X86_MMX
781 ResultTy = Type::getX86_MMXTy(Context);
782 break;
Chris Lattner1314b992007-04-22 06:23:29 +0000783 case bitc::TYPE_CODE_INTEGER: // INTEGER: [width]
784 if (Record.size() < 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000785 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000786
Owen Anderson55f1c092009-08-13 21:58:54 +0000787 ResultTy = IntegerType::get(Context, Record[0]);
Chris Lattner1314b992007-04-22 06:23:29 +0000788 break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000789 case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000790 // [pointee type, address space]
Chris Lattner1314b992007-04-22 06:23:29 +0000791 if (Record.size() < 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000792 return Error(InvalidRecord);
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000793 unsigned AddressSpace = 0;
794 if (Record.size() == 2)
795 AddressSpace = Record[1];
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000796 ResultTy = getTypeByID(Record[0]);
Craig Topper2617dcc2014-04-15 06:32:26 +0000797 if (!ResultTy)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000798 return Error(InvalidType);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000799 ResultTy = PointerType::get(ResultTy, AddressSpace);
Chris Lattner1314b992007-04-22 06:23:29 +0000800 break;
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000801 }
Nuno Lopes561dae02012-05-23 15:19:39 +0000802 case bitc::TYPE_CODE_FUNCTION_OLD: {
803 // FIXME: attrid is dead, remove it in LLVM 4.0
804 // FUNCTION: [vararg, attrid, retty, paramty x N]
805 if (Record.size() < 3)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000806 return Error(InvalidRecord);
Nuno Lopes561dae02012-05-23 15:19:39 +0000807 SmallVector<Type*, 8> ArgTys;
808 for (unsigned i = 3, e = Record.size(); i != e; ++i) {
809 if (Type *T = getTypeByID(Record[i]))
810 ArgTys.push_back(T);
811 else
812 break;
813 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000814
Nuno Lopes561dae02012-05-23 15:19:39 +0000815 ResultTy = getTypeByID(Record[2]);
Craig Topper2617dcc2014-04-15 06:32:26 +0000816 if (!ResultTy || ArgTys.size() < Record.size()-3)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000817 return Error(InvalidType);
Nuno Lopes561dae02012-05-23 15:19:39 +0000818
819 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
820 break;
821 }
Chad Rosier95898722011-11-03 00:14:01 +0000822 case bitc::TYPE_CODE_FUNCTION: {
823 // FUNCTION: [vararg, retty, paramty x N]
824 if (Record.size() < 2)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000825 return Error(InvalidRecord);
Chris Lattnercc3aaf12012-01-27 03:15:49 +0000826 SmallVector<Type*, 8> ArgTys;
Chad Rosier95898722011-11-03 00:14:01 +0000827 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
828 if (Type *T = getTypeByID(Record[i]))
829 ArgTys.push_back(T);
830 else
831 break;
832 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000833
Chad Rosier95898722011-11-03 00:14:01 +0000834 ResultTy = getTypeByID(Record[1]);
Craig Topper2617dcc2014-04-15 06:32:26 +0000835 if (!ResultTy || ArgTys.size() < Record.size()-2)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000836 return Error(InvalidType);
Chad Rosier95898722011-11-03 00:14:01 +0000837
838 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
839 break;
840 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000841 case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N]
Chris Lattner3c5616e2007-05-06 08:21:50 +0000842 if (Record.size() < 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000843 return Error(InvalidRecord);
Chris Lattnercc3aaf12012-01-27 03:15:49 +0000844 SmallVector<Type*, 8> EltTys;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000845 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
846 if (Type *T = getTypeByID(Record[i]))
847 EltTys.push_back(T);
848 else
849 break;
850 }
851 if (EltTys.size() != Record.size()-1)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000852 return Error(InvalidType);
Owen Anderson03cb69f2009-08-05 23:16:16 +0000853 ResultTy = StructType::get(Context, EltTys, Record[0]);
Chris Lattner1314b992007-04-22 06:23:29 +0000854 break;
855 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000856 case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N]
857 if (ConvertToString(Record, 0, TypeName))
Rafael Espindola48da4f42013-11-04 16:16:24 +0000858 return Error(InvalidRecord);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000859 continue;
860
861 case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N]
862 if (Record.size() < 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000863 return Error(InvalidRecord);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000864
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000865 if (NumRecords >= TypeList.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +0000866 return Error(InvalidTYPETable);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000867
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000868 // Check to see if this was forward referenced, if so fill in the temp.
869 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
870 if (Res) {
871 Res->setName(TypeName);
Craig Topper2617dcc2014-04-15 06:32:26 +0000872 TypeList[NumRecords] = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000873 } else // Otherwise, create a new struct.
Chris Lattner335d3992011-08-12 18:06:37 +0000874 Res = StructType::create(Context, TypeName);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000875 TypeName.clear();
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000876
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000877 SmallVector<Type*, 8> EltTys;
878 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
879 if (Type *T = getTypeByID(Record[i]))
880 EltTys.push_back(T);
881 else
882 break;
883 }
884 if (EltTys.size() != Record.size()-1)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000885 return Error(InvalidRecord);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000886 Res->setBody(EltTys, Record[0]);
887 ResultTy = Res;
888 break;
889 }
890 case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: []
891 if (Record.size() != 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000892 return Error(InvalidRecord);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000893
894 if (NumRecords >= TypeList.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +0000895 return Error(InvalidTYPETable);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000896
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000897 // Check to see if this was forward referenced, if so fill in the temp.
898 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
899 if (Res) {
900 Res->setName(TypeName);
Craig Topper2617dcc2014-04-15 06:32:26 +0000901 TypeList[NumRecords] = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000902 } else // Otherwise, create a new struct with no body.
Chris Lattner335d3992011-08-12 18:06:37 +0000903 Res = StructType::create(Context, TypeName);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000904 TypeName.clear();
905 ResultTy = Res;
906 break;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000907 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000908 case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty]
909 if (Record.size() < 2)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000910 return Error(InvalidRecord);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000911 if ((ResultTy = getTypeByID(Record[1])))
912 ResultTy = ArrayType::get(ResultTy, Record[0]);
913 else
Rafael Espindola48da4f42013-11-04 16:16:24 +0000914 return Error(InvalidType);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000915 break;
916 case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty]
917 if (Record.size() < 2)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000918 return Error(InvalidRecord);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000919 if ((ResultTy = getTypeByID(Record[1])))
920 ResultTy = VectorType::get(ResultTy, Record[0]);
921 else
Rafael Espindola48da4f42013-11-04 16:16:24 +0000922 return Error(InvalidType);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000923 break;
924 }
925
926 if (NumRecords >= TypeList.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +0000927 return Error(InvalidTYPETable);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000928 assert(ResultTy && "Didn't read a type?");
Craig Topper2617dcc2014-04-15 06:32:26 +0000929 assert(!TypeList[NumRecords] && "Already read type?");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000930 TypeList[NumRecords++] = ResultTy;
931 }
932}
933
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000934std::error_code BitcodeReader::ParseValueSymbolTable() {
Chris Lattner982ec1e2007-05-05 00:17:00 +0000935 if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +0000936 return Error(InvalidRecord);
Chris Lattnerccaa4482007-04-23 21:26:05 +0000937
938 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000939
Chris Lattnerccaa4482007-04-23 21:26:05 +0000940 // Read all the records for this value table.
941 SmallString<128> ValueName;
942 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +0000943 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +0000944
Chris Lattner27d38752013-01-20 02:13:19 +0000945 switch (Entry.Kind) {
946 case BitstreamEntry::SubBlock: // Handled for us already.
947 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +0000948 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +0000949 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000950 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +0000951 case BitstreamEntry::Record:
952 // The interesting case.
953 break;
Chris Lattnerccaa4482007-04-23 21:26:05 +0000954 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000955
Chris Lattnerccaa4482007-04-23 21:26:05 +0000956 // Read a record.
957 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +0000958 switch (Stream.readRecord(Entry.ID, Record)) {
Chris Lattnerccaa4482007-04-23 21:26:05 +0000959 default: // Default behavior: unknown type.
960 break;
Chris Lattnere14cb882007-05-04 19:11:41 +0000961 case bitc::VST_CODE_ENTRY: { // VST_ENTRY: [valueid, namechar x N]
Chris Lattnerccaa4482007-04-23 21:26:05 +0000962 if (ConvertToString(Record, 1, ValueName))
Rafael Espindola48da4f42013-11-04 16:16:24 +0000963 return Error(InvalidRecord);
Chris Lattnerccaa4482007-04-23 21:26:05 +0000964 unsigned ValueID = Record[0];
Karthik Bhat82540e92014-03-27 12:08:23 +0000965 if (ValueID >= ValueList.size() || !ValueList[ValueID])
Rafael Espindola48da4f42013-11-04 16:16:24 +0000966 return Error(InvalidRecord);
Chris Lattnerccaa4482007-04-23 21:26:05 +0000967 Value *V = ValueList[ValueID];
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000968
Daniel Dunbard786b512009-07-26 00:34:27 +0000969 V->setName(StringRef(ValueName.data(), ValueName.size()));
Chris Lattnerccaa4482007-04-23 21:26:05 +0000970 ValueName.clear();
971 break;
Reid Spencerdea02bd2007-05-04 01:43:33 +0000972 }
Bill Wendling35a9c3c2011-04-10 23:18:04 +0000973 case bitc::VST_CODE_BBENTRY: {
Chris Lattner6be58c62007-05-03 22:18:21 +0000974 if (ConvertToString(Record, 1, ValueName))
Rafael Espindola48da4f42013-11-04 16:16:24 +0000975 return Error(InvalidRecord);
Chris Lattner6be58c62007-05-03 22:18:21 +0000976 BasicBlock *BB = getBasicBlock(Record[0]);
Craig Topper2617dcc2014-04-15 06:32:26 +0000977 if (!BB)
Rafael Espindola48da4f42013-11-04 16:16:24 +0000978 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000979
Daniel Dunbard786b512009-07-26 00:34:27 +0000980 BB->setName(StringRef(ValueName.data(), ValueName.size()));
Chris Lattner6be58c62007-05-03 22:18:21 +0000981 ValueName.clear();
982 break;
Chris Lattnerccaa4482007-04-23 21:26:05 +0000983 }
Reid Spencerdea02bd2007-05-04 01:43:33 +0000984 }
Chris Lattnerccaa4482007-04-23 21:26:05 +0000985 }
986}
987
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000988std::error_code BitcodeReader::ParseMetadata() {
Devang Patel89923232010-01-11 18:52:33 +0000989 unsigned NextMDValueNo = MDValueList.size();
Devang Patel7428d8a2009-07-22 17:43:22 +0000990
991 if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +0000992 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000993
Devang Patel7428d8a2009-07-22 17:43:22 +0000994 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000995
Devang Patel7428d8a2009-07-22 17:43:22 +0000996 // Read all the records.
997 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +0000998 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +0000999
Chris Lattner27d38752013-01-20 02:13:19 +00001000 switch (Entry.Kind) {
1001 case BitstreamEntry::SubBlock: // Handled for us already.
1002 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001003 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00001004 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001005 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00001006 case BitstreamEntry::Record:
1007 // The interesting case.
1008 break;
Devang Patel7428d8a2009-07-22 17:43:22 +00001009 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001010
Victor Hernandezb8fd1522010-01-10 07:14:18 +00001011 bool IsFunctionLocal = false;
Devang Patel7428d8a2009-07-22 17:43:22 +00001012 // Read a record.
1013 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +00001014 unsigned Code = Stream.readRecord(Entry.ID, Record);
Dan Gohmanbbcd04d2010-09-13 18:00:48 +00001015 switch (Code) {
Devang Patel7428d8a2009-07-22 17:43:22 +00001016 default: // Default behavior: ignore.
1017 break;
Devang Patel27c87ff2009-07-29 22:34:41 +00001018 case bitc::METADATA_NAME: {
Chris Lattner8d140532013-01-20 02:54:05 +00001019 // Read name of the named metadata.
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001020 SmallString<8> Name(Record.begin(), Record.end());
Devang Patel27c87ff2009-07-29 22:34:41 +00001021 Record.clear();
1022 Code = Stream.ReadCode();
1023
Chris Lattnerb8778552011-06-17 17:50:30 +00001024 // METADATA_NAME is always followed by METADATA_NAMED_NODE.
Chris Lattner27d38752013-01-20 02:13:19 +00001025 unsigned NextBitCode = Stream.readRecord(Code, Record);
Chris Lattnerb8778552011-06-17 17:50:30 +00001026 assert(NextBitCode == bitc::METADATA_NAMED_NODE); (void)NextBitCode;
Devang Patel27c87ff2009-07-29 22:34:41 +00001027
1028 // Read named metadata elements.
1029 unsigned Size = Record.size();
Dan Gohman2637cc12010-07-21 23:38:33 +00001030 NamedMDNode *NMD = TheModule->getOrInsertNamedMetadata(Name);
Devang Patel27c87ff2009-07-29 22:34:41 +00001031 for (unsigned i = 0; i != Size; ++i) {
Karthik Bhat82540e92014-03-27 12:08:23 +00001032 MDNode *MD = dyn_cast_or_null<MDNode>(MDValueList.getValueFwdRef(Record[i]));
Craig Topper2617dcc2014-04-15 06:32:26 +00001033 if (!MD)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001034 return Error(InvalidRecord);
Dan Gohman2637cc12010-07-21 23:38:33 +00001035 NMD->addOperand(MD);
Devang Patel27c87ff2009-07-29 22:34:41 +00001036 }
Devang Patel27c87ff2009-07-29 22:34:41 +00001037 break;
1038 }
Chris Lattnerb8778552011-06-17 17:50:30 +00001039 case bitc::METADATA_FN_NODE:
Victor Hernandezb8fd1522010-01-10 07:14:18 +00001040 IsFunctionLocal = true;
1041 // fall-through
Chris Lattnerb8778552011-06-17 17:50:30 +00001042 case bitc::METADATA_NODE: {
Dan Gohman1e0213a2010-07-13 19:33:27 +00001043 if (Record.size() % 2 == 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001044 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001045
Devang Patele059ba6e2009-07-23 01:07:34 +00001046 unsigned Size = Record.size();
1047 SmallVector<Value*, 8> Elts;
1048 for (unsigned i = 0; i != Size; i += 2) {
Chris Lattner229907c2011-07-18 04:54:35 +00001049 Type *Ty = getTypeByID(Record[i]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001050 if (!Ty)
1051 return Error(InvalidRecord);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001052 if (Ty->isMetadataTy())
Devang Patel05eb6172009-08-04 06:00:18 +00001053 Elts.push_back(MDValueList.getValueFwdRef(Record[i+1]));
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001054 else if (!Ty->isVoidTy())
Devang Patele059ba6e2009-07-23 01:07:34 +00001055 Elts.push_back(ValueList.getValueFwdRef(Record[i+1], Ty));
1056 else
Craig Topper2617dcc2014-04-15 06:32:26 +00001057 Elts.push_back(nullptr);
Devang Patele059ba6e2009-07-23 01:07:34 +00001058 }
Jay Foad5514afe2011-04-21 19:59:31 +00001059 Value *V = MDNode::getWhenValsUnresolved(Context, Elts, IsFunctionLocal);
Victor Hernandezb8fd1522010-01-10 07:14:18 +00001060 IsFunctionLocal = false;
Devang Patel89923232010-01-11 18:52:33 +00001061 MDValueList.AssignValue(V, NextMDValueNo++);
Devang Patele059ba6e2009-07-23 01:07:34 +00001062 break;
1063 }
Devang Patel7428d8a2009-07-22 17:43:22 +00001064 case bitc::METADATA_STRING: {
Eli Bendersky5d5e18d2014-06-25 15:41:00 +00001065 std::string String(Record.begin(), Record.end());
1066 llvm::UpgradeMDStringConstant(String);
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001067 Value *V = MDString::get(Context, String);
Devang Patel89923232010-01-11 18:52:33 +00001068 MDValueList.AssignValue(V, NextMDValueNo++);
Devang Patel7428d8a2009-07-22 17:43:22 +00001069 break;
1070 }
Devang Patelaf206b82009-09-18 19:26:43 +00001071 case bitc::METADATA_KIND: {
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001072 if (Record.size() < 2)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001073 return Error(InvalidRecord);
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001074
Devang Patelb1a44772009-09-28 21:14:55 +00001075 unsigned Kind = Record[0];
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001076 SmallString<8> Name(Record.begin()+1, Record.end());
1077
Chris Lattnera0566972009-12-29 09:01:33 +00001078 unsigned NewKind = TheModule->getMDKindID(Name.str());
Dan Gohman43aa8f02010-07-20 21:42:28 +00001079 if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001080 return Error(ConflictingMETADATA_KINDRecords);
Devang Patelaf206b82009-09-18 19:26:43 +00001081 break;
1082 }
Devang Patel7428d8a2009-07-22 17:43:22 +00001083 }
1084 }
1085}
1086
Jan Wen Voungafaced02012-10-11 20:20:40 +00001087/// decodeSignRotatedValue - Decode a signed value stored with the sign bit in
Chris Lattner08feb1e2007-04-24 04:04:35 +00001088/// the LSB for dense VBR encoding.
Jan Wen Voungafaced02012-10-11 20:20:40 +00001089uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) {
Chris Lattner08feb1e2007-04-24 04:04:35 +00001090 if ((V & 1) == 0)
1091 return V >> 1;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001092 if (V != 1)
Chris Lattner08feb1e2007-04-24 04:04:35 +00001093 return -(V >> 1);
1094 // There is no such thing as -0 with integers. "-0" really means MININT.
1095 return 1ULL << 63;
1096}
1097
Chris Lattner44c17072007-04-26 02:46:40 +00001098/// ResolveGlobalAndAliasInits - Resolve all of the initializers for global
1099/// values and aliases that we can.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001100std::error_code BitcodeReader::ResolveGlobalAndAliasInits() {
Chris Lattner44c17072007-04-26 02:46:40 +00001101 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist;
1102 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist;
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001103 std::vector<std::pair<Function*, unsigned> > FunctionPrefixWorklist;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001104
Chris Lattner44c17072007-04-26 02:46:40 +00001105 GlobalInitWorklist.swap(GlobalInits);
1106 AliasInitWorklist.swap(AliasInits);
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001107 FunctionPrefixWorklist.swap(FunctionPrefixes);
Chris Lattner44c17072007-04-26 02:46:40 +00001108
1109 while (!GlobalInitWorklist.empty()) {
Chris Lattner831d4202007-04-26 03:27:58 +00001110 unsigned ValID = GlobalInitWorklist.back().second;
Chris Lattner44c17072007-04-26 02:46:40 +00001111 if (ValID >= ValueList.size()) {
1112 // Not ready to resolve this yet, it requires something later in the file.
Chris Lattner831d4202007-04-26 03:27:58 +00001113 GlobalInits.push_back(GlobalInitWorklist.back());
Chris Lattner44c17072007-04-26 02:46:40 +00001114 } else {
Karthik Bhat82540e92014-03-27 12:08:23 +00001115 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
Chris Lattner44c17072007-04-26 02:46:40 +00001116 GlobalInitWorklist.back().first->setInitializer(C);
1117 else
Rafael Espindola48da4f42013-11-04 16:16:24 +00001118 return Error(ExpectedConstant);
Chris Lattner44c17072007-04-26 02:46:40 +00001119 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001120 GlobalInitWorklist.pop_back();
Chris Lattner44c17072007-04-26 02:46:40 +00001121 }
1122
1123 while (!AliasInitWorklist.empty()) {
1124 unsigned ValID = AliasInitWorklist.back().second;
1125 if (ValID >= ValueList.size()) {
1126 AliasInits.push_back(AliasInitWorklist.back());
1127 } else {
Karthik Bhat82540e92014-03-27 12:08:23 +00001128 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
Rafael Espindola64c1e182014-06-03 02:41:57 +00001129 AliasInitWorklist.back().first->setAliasee(C);
Chris Lattner44c17072007-04-26 02:46:40 +00001130 else
Rafael Espindola48da4f42013-11-04 16:16:24 +00001131 return Error(ExpectedConstant);
Chris Lattner44c17072007-04-26 02:46:40 +00001132 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001133 AliasInitWorklist.pop_back();
Chris Lattner44c17072007-04-26 02:46:40 +00001134 }
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001135
1136 while (!FunctionPrefixWorklist.empty()) {
1137 unsigned ValID = FunctionPrefixWorklist.back().second;
1138 if (ValID >= ValueList.size()) {
1139 FunctionPrefixes.push_back(FunctionPrefixWorklist.back());
1140 } else {
Karthik Bhat82540e92014-03-27 12:08:23 +00001141 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001142 FunctionPrefixWorklist.back().first->setPrefixData(C);
1143 else
Rafael Espindola48da4f42013-11-04 16:16:24 +00001144 return Error(ExpectedConstant);
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001145 }
1146 FunctionPrefixWorklist.pop_back();
1147 }
1148
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001149 return std::error_code();
Chris Lattner44c17072007-04-26 02:46:40 +00001150}
1151
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001152static APInt ReadWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) {
1153 SmallVector<uint64_t, 8> Words(Vals.size());
1154 std::transform(Vals.begin(), Vals.end(), Words.begin(),
Jan Wen Voungafaced02012-10-11 20:20:40 +00001155 BitcodeReader::decodeSignRotatedValue);
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001156
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00001157 return APInt(TypeBits, Words);
1158}
1159
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001160std::error_code BitcodeReader::ParseConstants() {
Chris Lattner982ec1e2007-05-05 00:17:00 +00001161 if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001162 return Error(InvalidRecord);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001163
1164 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001165
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001166 // Read all the records for this value table.
Chris Lattner229907c2011-07-18 04:54:35 +00001167 Type *CurTy = Type::getInt32Ty(Context);
Chris Lattner1663cca2007-04-24 05:48:56 +00001168 unsigned NextCstNo = ValueList.size();
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001169 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00001170 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00001171
Chris Lattner27d38752013-01-20 02:13:19 +00001172 switch (Entry.Kind) {
1173 case BitstreamEntry::SubBlock: // Handled for us already.
1174 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001175 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00001176 case BitstreamEntry::EndBlock:
1177 if (NextCstNo != ValueList.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001178 return Error(InvalidConstantReference);
Joe Abbey97b7a172013-02-06 22:14:06 +00001179
Chris Lattner27d38752013-01-20 02:13:19 +00001180 // Once all the constants have been read, go through and resolve forward
1181 // references.
1182 ValueList.ResolveConstantForwardRefs();
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001183 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00001184 case BitstreamEntry::Record:
1185 // The interesting case.
Chris Lattner74429932008-08-21 02:34:16 +00001186 break;
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001187 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001188
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001189 // Read a record.
1190 Record.clear();
Craig Topper2617dcc2014-04-15 06:32:26 +00001191 Value *V = nullptr;
Chris Lattner27d38752013-01-20 02:13:19 +00001192 unsigned BitCode = Stream.readRecord(Entry.ID, Record);
Dan Gohman0ebd6962009-07-20 21:19:07 +00001193 switch (BitCode) {
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001194 default: // Default behavior: unknown constant
1195 case bitc::CST_CODE_UNDEF: // UNDEF
Owen Andersonb292b8c2009-07-30 23:03:37 +00001196 V = UndefValue::get(CurTy);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001197 break;
1198 case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid]
1199 if (Record.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001200 return Error(InvalidRecord);
Karthik Bhat82540e92014-03-27 12:08:23 +00001201 if (Record[0] >= TypeList.size() || !TypeList[Record[0]])
Rafael Espindola48da4f42013-11-04 16:16:24 +00001202 return Error(InvalidRecord);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001203 CurTy = TypeList[Record[0]];
Chris Lattner08feb1e2007-04-24 04:04:35 +00001204 continue; // Skip the ValueList manipulation.
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001205 case bitc::CST_CODE_NULL: // NULL
Owen Anderson5a1acd92009-07-31 20:28:14 +00001206 V = Constant::getNullValue(CurTy);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001207 break;
1208 case bitc::CST_CODE_INTEGER: // INTEGER: [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);
Jan Wen Voungafaced02012-10-11 20:20:40 +00001211 V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0]));
Chris Lattner08feb1e2007-04-24 04:04:35 +00001212 break;
Chris Lattnere14cb882007-05-04 19:11:41 +00001213 case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
Duncan Sands19d0b472010-02-16 11:11:14 +00001214 if (!CurTy->isIntegerTy() || Record.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001215 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001216
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001217 APInt VInt = ReadWideAPInt(Record,
1218 cast<IntegerType>(CurTy)->getBitWidth());
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00001219 V = ConstantInt::get(Context, VInt);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001220
Chris Lattner08feb1e2007-04-24 04:04:35 +00001221 break;
1222 }
Dale Johannesen245dceb2007-09-11 18:32:33 +00001223 case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval]
Chris Lattner08feb1e2007-04-24 04:04:35 +00001224 if (Record.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001225 return Error(InvalidRecord);
Dan Gohman518cda42011-12-17 00:04:22 +00001226 if (CurTy->isHalfTy())
Tim Northover29178a32013-01-22 09:46:31 +00001227 V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf,
1228 APInt(16, (uint16_t)Record[0])));
Dan Gohman518cda42011-12-17 00:04:22 +00001229 else if (CurTy->isFloatTy())
Tim Northover29178a32013-01-22 09:46:31 +00001230 V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle,
1231 APInt(32, (uint32_t)Record[0])));
Chris Lattnerfdd87902009-10-05 05:54:46 +00001232 else if (CurTy->isDoubleTy())
Tim Northover29178a32013-01-22 09:46:31 +00001233 V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble,
1234 APInt(64, Record[0])));
Chris Lattnerfdd87902009-10-05 05:54:46 +00001235 else if (CurTy->isX86_FP80Ty()) {
Dale Johannesen93eefa02009-03-23 21:16:53 +00001236 // Bits are not stored the same way as a normal i80 APInt, compensate.
1237 uint64_t Rearrange[2];
1238 Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);
1239 Rearrange[1] = Record[0] >> 48;
Tim Northover29178a32013-01-22 09:46:31 +00001240 V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended,
1241 APInt(80, Rearrange)));
Chris Lattnerfdd87902009-10-05 05:54:46 +00001242 } else if (CurTy->isFP128Ty())
Tim Northover29178a32013-01-22 09:46:31 +00001243 V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad,
1244 APInt(128, Record)));
Chris Lattnerfdd87902009-10-05 05:54:46 +00001245 else if (CurTy->isPPC_FP128Ty())
Tim Northover29178a32013-01-22 09:46:31 +00001246 V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble,
1247 APInt(128, Record)));
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001248 else
Owen Andersonb292b8c2009-07-30 23:03:37 +00001249 V = UndefValue::get(CurTy);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001250 break;
Dale Johannesen245dceb2007-09-11 18:32:33 +00001251 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001252
Chris Lattnere14cb882007-05-04 19:11:41 +00001253 case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]
1254 if (Record.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001255 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001256
Chris Lattnere14cb882007-05-04 19:11:41 +00001257 unsigned Size = Record.size();
Chris Lattnercc3aaf12012-01-27 03:15:49 +00001258 SmallVector<Constant*, 16> Elts;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001259
Chris Lattner229907c2011-07-18 04:54:35 +00001260 if (StructType *STy = dyn_cast<StructType>(CurTy)) {
Chris Lattner1663cca2007-04-24 05:48:56 +00001261 for (unsigned i = 0; i != Size; ++i)
Chris Lattnere14cb882007-05-04 19:11:41 +00001262 Elts.push_back(ValueList.getConstantFwdRef(Record[i],
Chris Lattner1663cca2007-04-24 05:48:56 +00001263 STy->getElementType(i)));
Owen Anderson45308b52009-07-27 22:29:26 +00001264 V = ConstantStruct::get(STy, Elts);
Chris Lattner229907c2011-07-18 04:54:35 +00001265 } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) {
1266 Type *EltTy = ATy->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 Andersonc2c79322009-07-28 18:32:17 +00001269 V = ConstantArray::get(ATy, Elts);
Chris Lattner229907c2011-07-18 04:54:35 +00001270 } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) {
1271 Type *EltTy = VTy->getElementType();
Chris Lattner1663cca2007-04-24 05:48:56 +00001272 for (unsigned i = 0; i != Size; ++i)
Chris Lattnere14cb882007-05-04 19:11:41 +00001273 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
Owen Anderson4aa32952009-07-28 21:19:26 +00001274 V = ConstantVector::get(Elts);
Chris Lattner1663cca2007-04-24 05:48:56 +00001275 } else {
Owen Andersonb292b8c2009-07-30 23:03:37 +00001276 V = UndefValue::get(CurTy);
Chris Lattner1663cca2007-04-24 05:48:56 +00001277 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001278 break;
1279 }
Chris Lattnerbb8278a2012-02-05 02:41:35 +00001280 case bitc::CST_CODE_STRING: // STRING: [values]
Chris Lattnerf25f7102007-05-06 00:53:07 +00001281 case bitc::CST_CODE_CSTRING: { // CSTRING: [values]
1282 if (Record.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001283 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001284
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001285 SmallString<16> Elts(Record.begin(), Record.end());
Chris Lattnerbb8278a2012-02-05 02:41:35 +00001286 V = ConstantDataArray::getString(Context, Elts,
1287 BitCode == bitc::CST_CODE_CSTRING);
Chris Lattnerf25f7102007-05-06 00:53:07 +00001288 break;
1289 }
Chris Lattner372dd1e2012-01-30 00:51:16 +00001290 case bitc::CST_CODE_DATA: {// DATA: [n x value]
1291 if (Record.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001292 return Error(InvalidRecord);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001293
Chris Lattner372dd1e2012-01-30 00:51:16 +00001294 Type *EltTy = cast<SequentialType>(CurTy)->getElementType();
1295 unsigned Size = Record.size();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001296
Chris Lattner372dd1e2012-01-30 00:51:16 +00001297 if (EltTy->isIntegerTy(8)) {
1298 SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end());
1299 if (isa<VectorType>(CurTy))
1300 V = ConstantDataVector::get(Context, Elts);
1301 else
1302 V = ConstantDataArray::get(Context, Elts);
1303 } else if (EltTy->isIntegerTy(16)) {
1304 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
1305 if (isa<VectorType>(CurTy))
1306 V = ConstantDataVector::get(Context, Elts);
1307 else
1308 V = ConstantDataArray::get(Context, Elts);
1309 } else if (EltTy->isIntegerTy(32)) {
1310 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
1311 if (isa<VectorType>(CurTy))
1312 V = ConstantDataVector::get(Context, Elts);
1313 else
1314 V = ConstantDataArray::get(Context, Elts);
1315 } else if (EltTy->isIntegerTy(64)) {
1316 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
1317 if (isa<VectorType>(CurTy))
1318 V = ConstantDataVector::get(Context, Elts);
1319 else
1320 V = ConstantDataArray::get(Context, Elts);
1321 } else if (EltTy->isFloatTy()) {
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001322 SmallVector<float, 16> Elts(Size);
1323 std::transform(Record.begin(), Record.end(), Elts.begin(), BitsToFloat);
Chris Lattner372dd1e2012-01-30 00:51:16 +00001324 if (isa<VectorType>(CurTy))
1325 V = ConstantDataVector::get(Context, Elts);
1326 else
1327 V = ConstantDataArray::get(Context, Elts);
1328 } else if (EltTy->isDoubleTy()) {
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001329 SmallVector<double, 16> Elts(Size);
1330 std::transform(Record.begin(), Record.end(), Elts.begin(),
1331 BitsToDouble);
Chris Lattner372dd1e2012-01-30 00:51:16 +00001332 if (isa<VectorType>(CurTy))
1333 V = ConstantDataVector::get(Context, Elts);
1334 else
1335 V = ConstantDataArray::get(Context, Elts);
1336 } else {
Rafael Espindola48da4f42013-11-04 16:16:24 +00001337 return Error(InvalidTypeForValue);
Chris Lattner372dd1e2012-01-30 00:51:16 +00001338 }
1339 break;
1340 }
1341
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001342 case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval]
Rafael Espindola48da4f42013-11-04 16:16:24 +00001343 if (Record.size() < 3)
1344 return Error(InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001345 int Opc = GetDecodedBinaryOpcode(Record[0], CurTy);
Chris Lattner890683d2007-04-24 18:15:21 +00001346 if (Opc < 0) {
Owen Andersonb292b8c2009-07-30 23:03:37 +00001347 V = UndefValue::get(CurTy); // Unknown binop.
Chris Lattner890683d2007-04-24 18:15:21 +00001348 } else {
1349 Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy);
1350 Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy);
Dan Gohman1b849082009-09-07 23:54:19 +00001351 unsigned Flags = 0;
1352 if (Record.size() >= 4) {
1353 if (Opc == Instruction::Add ||
1354 Opc == Instruction::Sub ||
Chris Lattnera676c0f2011-02-07 16:40:21 +00001355 Opc == Instruction::Mul ||
1356 Opc == Instruction::Shl) {
Dan Gohman1b849082009-09-07 23:54:19 +00001357 if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP))
1358 Flags |= OverflowingBinaryOperator::NoSignedWrap;
1359 if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
1360 Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
Chris Lattner35315d02011-02-06 21:44:57 +00001361 } else if (Opc == Instruction::SDiv ||
Chris Lattnera676c0f2011-02-07 16:40:21 +00001362 Opc == Instruction::UDiv ||
1363 Opc == Instruction::LShr ||
1364 Opc == Instruction::AShr) {
Chris Lattner35315d02011-02-06 21:44:57 +00001365 if (Record[3] & (1 << bitc::PEO_EXACT))
Dan Gohman1b849082009-09-07 23:54:19 +00001366 Flags |= SDivOperator::IsExact;
1367 }
1368 }
1369 V = ConstantExpr::get(Opc, LHS, RHS, Flags);
Chris Lattner890683d2007-04-24 18:15:21 +00001370 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001371 break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001372 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001373 case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval]
Rafael Espindola48da4f42013-11-04 16:16:24 +00001374 if (Record.size() < 3)
1375 return Error(InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001376 int Opc = GetDecodedCastOpcode(Record[0]);
Chris Lattner890683d2007-04-24 18:15:21 +00001377 if (Opc < 0) {
Owen Andersonb292b8c2009-07-30 23:03:37 +00001378 V = UndefValue::get(CurTy); // Unknown cast.
Chris Lattner890683d2007-04-24 18:15:21 +00001379 } else {
Chris Lattner229907c2011-07-18 04:54:35 +00001380 Type *OpTy = getTypeByID(Record[1]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001381 if (!OpTy)
1382 return Error(InvalidRecord);
Chris Lattner890683d2007-04-24 18:15:21 +00001383 Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001384 V = UpgradeBitCastExpr(Opc, Op, CurTy);
1385 if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy);
Chris Lattner890683d2007-04-24 18:15:21 +00001386 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001387 break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001388 }
Dan Gohman1639c392009-07-27 21:53:46 +00001389 case bitc::CST_CODE_CE_INBOUNDS_GEP:
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001390 case bitc::CST_CODE_CE_GEP: { // CE_GEP: [n x operands]
Rafael Espindola48da4f42013-11-04 16:16:24 +00001391 if (Record.size() & 1)
1392 return Error(InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001393 SmallVector<Constant*, 16> Elts;
Chris Lattnere14cb882007-05-04 19:11:41 +00001394 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
Chris Lattner229907c2011-07-18 04:54:35 +00001395 Type *ElTy = getTypeByID(Record[i]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001396 if (!ElTy)
1397 return Error(InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001398 Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], ElTy));
1399 }
Jay Foaded8db7d2011-07-21 14:31:17 +00001400 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
Jay Foad2f5fc8c2011-07-21 15:15:37 +00001401 V = ConstantExpr::getGetElementPtr(Elts[0], Indices,
1402 BitCode ==
1403 bitc::CST_CODE_CE_INBOUNDS_GEP);
Chris Lattner890683d2007-04-24 18:15:21 +00001404 break;
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001405 }
Joe Abbey1a6e7702013-09-12 22:02:31 +00001406 case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#]
Rafael Espindola48da4f42013-11-04 16:16:24 +00001407 if (Record.size() < 3)
1408 return Error(InvalidRecord);
Joe Abbey1a6e7702013-09-12 22:02:31 +00001409
1410 Type *SelectorTy = Type::getInt1Ty(Context);
1411
1412 // If CurTy is a vector of length n, then Record[0] must be a <n x i1>
1413 // vector. Otherwise, it must be a single bit.
1414 if (VectorType *VTy = dyn_cast<VectorType>(CurTy))
1415 SelectorTy = VectorType::get(Type::getInt1Ty(Context),
1416 VTy->getNumElements());
1417
1418 V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0],
1419 SelectorTy),
1420 ValueList.getConstantFwdRef(Record[1],CurTy),
1421 ValueList.getConstantFwdRef(Record[2],CurTy));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001422 break;
Joe Abbey1a6e7702013-09-12 22:02:31 +00001423 }
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001424 case bitc::CST_CODE_CE_EXTRACTELT
1425 : { // CE_EXTRACTELT: [opty, opval, opty, opval]
Rafael Espindola48da4f42013-11-04 16:16:24 +00001426 if (Record.size() < 3)
1427 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00001428 VectorType *OpTy =
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001429 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
Craig Topper2617dcc2014-04-15 06:32:26 +00001430 if (!OpTy)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001431 return Error(InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001432 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001433 Constant *Op1 = nullptr;
1434 if (Record.size() == 4) {
1435 Type *IdxTy = getTypeByID(Record[2]);
1436 if (!IdxTy)
1437 return Error(InvalidRecord);
1438 Op1 = ValueList.getConstantFwdRef(Record[3], IdxTy);
1439 } else // TODO: Remove with llvm 4.0
1440 Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
1441 if (!Op1)
1442 return Error(InvalidRecord);
Owen Anderson487375e2009-07-29 18:55:55 +00001443 V = ConstantExpr::getExtractElement(Op0, Op1);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001444 break;
1445 }
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001446 case bitc::CST_CODE_CE_INSERTELT
1447 : { // CE_INSERTELT: [opval, opval, opty, opval]
Chris Lattner229907c2011-07-18 04:54:35 +00001448 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
Craig Topper2617dcc2014-04-15 06:32:26 +00001449 if (Record.size() < 3 || !OpTy)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001450 return Error(InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001451 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
1452 Constant *Op1 = ValueList.getConstantFwdRef(Record[1],
1453 OpTy->getElementType());
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001454 Constant *Op2 = nullptr;
1455 if (Record.size() == 4) {
1456 Type *IdxTy = getTypeByID(Record[2]);
1457 if (!IdxTy)
1458 return Error(InvalidRecord);
1459 Op2 = ValueList.getConstantFwdRef(Record[3], IdxTy);
1460 } else // TODO: Remove with llvm 4.0
1461 Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
1462 if (!Op2)
1463 return Error(InvalidRecord);
Owen Anderson487375e2009-07-29 18:55:55 +00001464 V = ConstantExpr::getInsertElement(Op0, Op1, Op2);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001465 break;
1466 }
1467 case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval]
Chris Lattner229907c2011-07-18 04:54:35 +00001468 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
Craig Topper2617dcc2014-04-15 06:32:26 +00001469 if (Record.size() < 3 || !OpTy)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001470 return Error(InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001471 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
1472 Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy);
Chris Lattner229907c2011-07-18 04:54:35 +00001473 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
Owen Andersone9f98042009-07-07 20:18:58 +00001474 OpTy->getNumElements());
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001475 Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy);
Owen Anderson487375e2009-07-29 18:55:55 +00001476 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001477 break;
1478 }
Nate Begeman94aa38d2009-02-12 21:28:33 +00001479 case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval]
Chris Lattner229907c2011-07-18 04:54:35 +00001480 VectorType *RTy = dyn_cast<VectorType>(CurTy);
1481 VectorType *OpTy =
Duncan Sands89d412a2010-10-28 15:47:26 +00001482 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
Craig Topper2617dcc2014-04-15 06:32:26 +00001483 if (Record.size() < 4 || !RTy || !OpTy)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001484 return Error(InvalidRecord);
Nate Begeman94aa38d2009-02-12 21:28:33 +00001485 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
1486 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
Chris Lattner229907c2011-07-18 04:54:35 +00001487 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
Owen Andersone9f98042009-07-07 20:18:58 +00001488 RTy->getNumElements());
Nate Begeman94aa38d2009-02-12 21:28:33 +00001489 Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy);
Owen Anderson487375e2009-07-29 18:55:55 +00001490 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Nate Begeman94aa38d2009-02-12 21:28:33 +00001491 break;
1492 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001493 case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred]
Rafael Espindola48da4f42013-11-04 16:16:24 +00001494 if (Record.size() < 4)
1495 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00001496 Type *OpTy = getTypeByID(Record[0]);
Craig Topper2617dcc2014-04-15 06:32:26 +00001497 if (!OpTy)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001498 return Error(InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001499 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
1500 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
1501
Duncan Sands9dff9be2010-02-15 16:12:20 +00001502 if (OpTy->isFPOrFPVectorTy())
Owen Anderson487375e2009-07-29 18:55:55 +00001503 V = ConstantExpr::getFCmp(Record[3], Op0, Op1);
Nate Begemand2195702008-05-12 19:01:56 +00001504 else
Owen Anderson487375e2009-07-29 18:55:55 +00001505 V = ConstantExpr::getICmp(Record[3], Op0, Op1);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001506 break;
Chris Lattner1663cca2007-04-24 05:48:56 +00001507 }
Chad Rosierd8c76102012-09-05 19:00:49 +00001508 // This maintains backward compatibility, pre-asm dialect keywords.
Chad Rosier5895eda2012-09-05 06:28:52 +00001509 // FIXME: Remove with the 4.0 release.
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001510 case bitc::CST_CODE_INLINEASM_OLD: {
Rafael Espindola48da4f42013-11-04 16:16:24 +00001511 if (Record.size() < 2)
1512 return Error(InvalidRecord);
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001513 std::string AsmStr, ConstrStr;
Dale Johannesenfd04c742009-10-13 20:46:56 +00001514 bool HasSideEffects = Record[0] & 1;
Dale Johannesen1cfb9582009-10-21 23:28:00 +00001515 bool IsAlignStack = Record[0] >> 1;
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001516 unsigned AsmStrSize = Record[1];
1517 if (2+AsmStrSize >= Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001518 return Error(InvalidRecord);
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001519 unsigned ConstStrSize = Record[2+AsmStrSize];
1520 if (3+AsmStrSize+ConstStrSize > Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001521 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001522
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001523 for (unsigned i = 0; i != AsmStrSize; ++i)
1524 AsmStr += (char)Record[2+i];
1525 for (unsigned i = 0; i != ConstStrSize; ++i)
1526 ConstrStr += (char)Record[3+AsmStrSize+i];
Chris Lattner229907c2011-07-18 04:54:35 +00001527 PointerType *PTy = cast<PointerType>(CurTy);
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001528 V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
Dale Johannesen1cfb9582009-10-21 23:28:00 +00001529 AsmStr, ConstrStr, HasSideEffects, IsAlignStack);
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001530 break;
1531 }
Chad Rosierd8c76102012-09-05 19:00:49 +00001532 // This version adds support for the asm dialect keywords (e.g.,
1533 // inteldialect).
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001534 case bitc::CST_CODE_INLINEASM: {
Rafael Espindola48da4f42013-11-04 16:16:24 +00001535 if (Record.size() < 2)
1536 return Error(InvalidRecord);
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001537 std::string AsmStr, ConstrStr;
1538 bool HasSideEffects = Record[0] & 1;
1539 bool IsAlignStack = (Record[0] >> 1) & 1;
1540 unsigned AsmDialect = Record[0] >> 2;
1541 unsigned AsmStrSize = Record[1];
1542 if (2+AsmStrSize >= Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001543 return Error(InvalidRecord);
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001544 unsigned ConstStrSize = Record[2+AsmStrSize];
1545 if (3+AsmStrSize+ConstStrSize > Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001546 return Error(InvalidRecord);
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001547
1548 for (unsigned i = 0; i != AsmStrSize; ++i)
1549 AsmStr += (char)Record[2+i];
1550 for (unsigned i = 0; i != ConstStrSize; ++i)
1551 ConstrStr += (char)Record[3+AsmStrSize+i];
1552 PointerType *PTy = cast<PointerType>(CurTy);
1553 V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
1554 AsmStr, ConstrStr, HasSideEffects, IsAlignStack,
Chad Rosierd8c76102012-09-05 19:00:49 +00001555 InlineAsm::AsmDialect(AsmDialect));
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001556 break;
1557 }
Chris Lattner5956dc82009-10-28 05:53:48 +00001558 case bitc::CST_CODE_BLOCKADDRESS:{
Rafael Espindola48da4f42013-11-04 16:16:24 +00001559 if (Record.size() < 3)
1560 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00001561 Type *FnTy = getTypeByID(Record[0]);
Craig Topper2617dcc2014-04-15 06:32:26 +00001562 if (!FnTy)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001563 return Error(InvalidRecord);
Chris Lattner5956dc82009-10-28 05:53:48 +00001564 Function *Fn =
1565 dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy));
Craig Topper2617dcc2014-04-15 06:32:26 +00001566 if (!Fn)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001567 return Error(InvalidRecord);
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00001568
1569 // If the function is already parsed we can insert the block address right
1570 // away.
1571 if (!Fn->empty()) {
1572 Function::iterator BBI = Fn->begin(), BBE = Fn->end();
1573 for (size_t I = 0, E = Record[2]; I != E; ++I) {
1574 if (BBI == BBE)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001575 return Error(InvalidID);
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00001576 ++BBI;
1577 }
1578 V = BlockAddress::get(Fn, BBI);
1579 } else {
1580 // Otherwise insert a placeholder and remember it so it can be inserted
1581 // when the function is parsed.
1582 GlobalVariable *FwdRef = new GlobalVariable(*Fn->getParent(),
1583 Type::getInt8Ty(Context),
Chris Lattner5956dc82009-10-28 05:53:48 +00001584 false, GlobalValue::InternalLinkage,
Craig Topper2617dcc2014-04-15 06:32:26 +00001585 nullptr, "");
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00001586 BlockAddrFwdRefs[Fn].push_back(std::make_pair(Record[2], FwdRef));
1587 V = FwdRef;
1588 }
Chris Lattner5956dc82009-10-28 05:53:48 +00001589 break;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001590 }
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001591 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001592
Chris Lattner83930552007-05-01 07:01:57 +00001593 ValueList.AssignValue(V, NextCstNo);
Chris Lattner1663cca2007-04-24 05:48:56 +00001594 ++NextCstNo;
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001595 }
1596}
Chris Lattner1314b992007-04-22 06:23:29 +00001597
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001598std::error_code BitcodeReader::ParseUseLists() {
Chad Rosierca2567b2011-12-07 21:44:12 +00001599 if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001600 return Error(InvalidRecord);
Chad Rosierca2567b2011-12-07 21:44:12 +00001601
1602 SmallVector<uint64_t, 64> Record;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001603
Chad Rosierca2567b2011-12-07 21:44:12 +00001604 // Read all the records.
1605 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00001606 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00001607
Chris Lattner27d38752013-01-20 02:13:19 +00001608 switch (Entry.Kind) {
1609 case BitstreamEntry::SubBlock: // Handled for us already.
1610 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001611 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00001612 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001613 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00001614 case BitstreamEntry::Record:
1615 // The interesting case.
1616 break;
Chad Rosierca2567b2011-12-07 21:44:12 +00001617 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001618
Chad Rosierca2567b2011-12-07 21:44:12 +00001619 // Read a use list record.
1620 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +00001621 switch (Stream.readRecord(Entry.ID, Record)) {
Chad Rosierca2567b2011-12-07 21:44:12 +00001622 default: // Default behavior: unknown type.
1623 break;
1624 case bitc::USELIST_CODE_ENTRY: { // USELIST_CODE_ENTRY: TBD.
1625 unsigned RecordLength = Record.size();
1626 if (RecordLength < 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001627 return Error(InvalidRecord);
Chad Rosierca2567b2011-12-07 21:44:12 +00001628 UseListRecords.push_back(Record);
1629 break;
1630 }
1631 }
1632 }
1633}
1634
Chris Lattner85b7b402007-05-01 05:52:21 +00001635/// RememberAndSkipFunctionBody - When we see the block for a function body,
1636/// remember where it is and then skip it. This lets us lazily deserialize the
1637/// functions.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001638std::error_code BitcodeReader::RememberAndSkipFunctionBody() {
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001639 // Get the function we are talking about.
1640 if (FunctionsWithBodies.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001641 return Error(InsufficientFunctionProtos);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001642
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001643 Function *Fn = FunctionsWithBodies.back();
1644 FunctionsWithBodies.pop_back();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001645
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001646 // Save the current stream state.
1647 uint64_t CurBit = Stream.GetCurrentBitNo();
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001648 DeferredFunctionInfo[Fn] = CurBit;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001649
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001650 // Skip over the function block for now.
1651 if (Stream.SkipBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001652 return Error(InvalidRecord);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001653 return std::error_code();
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001654}
1655
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001656std::error_code BitcodeReader::GlobalCleanup() {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001657 // Patch the initializers for globals and aliases up.
1658 ResolveGlobalAndAliasInits();
1659 if (!GlobalInits.empty() || !AliasInits.empty())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001660 return Error(MalformedGlobalInitializerSet);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001661
1662 // Look for intrinsic functions which need to be upgraded at some point
1663 for (Module::iterator FI = TheModule->begin(), FE = TheModule->end();
1664 FI != FE; ++FI) {
1665 Function *NewFn;
1666 if (UpgradeIntrinsicFunction(FI, NewFn))
1667 UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn));
1668 }
1669
1670 // Look for global variables which need to be renamed.
1671 for (Module::global_iterator
1672 GI = TheModule->global_begin(), GE = TheModule->global_end();
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001673 GI != GE;) {
1674 GlobalVariable *GV = GI++;
1675 UpgradeGlobalVariable(GV);
1676 }
1677
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001678 // Force deallocation of memory for these vectors to favor the client that
1679 // want lazy deserialization.
1680 std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits);
1681 std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001682 return std::error_code();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001683}
1684
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001685std::error_code BitcodeReader::ParseModule(bool Resume) {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001686 if (Resume)
1687 Stream.JumpToBit(NextUnreadBit);
1688 else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001689 return Error(InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001690
Chris Lattner1314b992007-04-22 06:23:29 +00001691 SmallVector<uint64_t, 64> Record;
1692 std::vector<std::string> SectionTable;
Gordon Henriksend930f912008-08-17 18:44:35 +00001693 std::vector<std::string> GCTable;
Chris Lattner1314b992007-04-22 06:23:29 +00001694
1695 // Read all the records for this module.
Chris Lattner27d38752013-01-20 02:13:19 +00001696 while (1) {
1697 BitstreamEntry Entry = Stream.advance();
Joe Abbey97b7a172013-02-06 22:14:06 +00001698
Chris Lattner27d38752013-01-20 02:13:19 +00001699 switch (Entry.Kind) {
1700 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +00001701 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00001702 case BitstreamEntry::EndBlock:
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001703 return GlobalCleanup();
Joe Abbey97b7a172013-02-06 22:14:06 +00001704
Chris Lattner27d38752013-01-20 02:13:19 +00001705 case BitstreamEntry::SubBlock:
1706 switch (Entry.ID) {
Chris Lattner1314b992007-04-22 06:23:29 +00001707 default: // Skip unknown content.
1708 if (Stream.SkipBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001709 return Error(InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001710 break;
Chris Lattner6eeea5d2007-05-05 18:57:30 +00001711 case bitc::BLOCKINFO_BLOCK_ID:
1712 if (Stream.ReadBlockInfoBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001713 return Error(MalformedBlock);
Chris Lattner6eeea5d2007-05-05 18:57:30 +00001714 break;
Chris Lattnerfee5a372007-05-04 03:30:17 +00001715 case bitc::PARAMATTR_BLOCK_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001716 if (std::error_code EC = ParseAttributeBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001717 return EC;
Chris Lattnerfee5a372007-05-04 03:30:17 +00001718 break;
Bill Wendlingba629332013-02-10 23:24:25 +00001719 case bitc::PARAMATTR_GROUP_BLOCK_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001720 if (std::error_code EC = ParseAttributeGroupBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001721 return EC;
Bill Wendlingba629332013-02-10 23:24:25 +00001722 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001723 case bitc::TYPE_BLOCK_ID_NEW:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001724 if (std::error_code EC = ParseTypeTable())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001725 return EC;
Chris Lattner1314b992007-04-22 06:23:29 +00001726 break;
Chris Lattnerccaa4482007-04-23 21:26:05 +00001727 case bitc::VALUE_SYMTAB_BLOCK_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001728 if (std::error_code EC = ParseValueSymbolTable())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001729 return EC;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001730 SeenValueSymbolTable = true;
Chris Lattnerccaa4482007-04-23 21:26:05 +00001731 break;
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001732 case bitc::CONSTANTS_BLOCK_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001733 if (std::error_code EC = ParseConstants())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001734 return EC;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001735 if (std::error_code EC = ResolveGlobalAndAliasInits())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001736 return EC;
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001737 break;
Devang Patel7428d8a2009-07-22 17:43:22 +00001738 case bitc::METADATA_BLOCK_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001739 if (std::error_code EC = ParseMetadata())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001740 return EC;
Devang Patel7428d8a2009-07-22 17:43:22 +00001741 break;
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001742 case bitc::FUNCTION_BLOCK_ID:
1743 // If this is the first function body we've seen, reverse the
1744 // FunctionsWithBodies list.
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001745 if (!SeenFirstFunctionBody) {
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001746 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001747 if (std::error_code EC = GlobalCleanup())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001748 return EC;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001749 SeenFirstFunctionBody = true;
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001750 }
Joe Abbey97b7a172013-02-06 22:14:06 +00001751
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001752 if (std::error_code EC = RememberAndSkipFunctionBody())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001753 return EC;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001754 // For streaming bitcode, suspend parsing when we reach the function
1755 // bodies. Subsequent materialization calls will resume it when
1756 // necessary. For streaming, the function bodies must be at the end of
1757 // the bitcode. If the bitcode file is old, the symbol table will be
1758 // at the end instead and will not have been seen yet. In this case,
1759 // just finish the parse now.
1760 if (LazyStreamer && SeenValueSymbolTable) {
1761 NextUnreadBit = Stream.GetCurrentBitNo();
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001762 return std::error_code();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001763 }
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001764 break;
Chad Rosierca2567b2011-12-07 21:44:12 +00001765 case bitc::USELIST_BLOCK_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001766 if (std::error_code EC = ParseUseLists())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001767 return EC;
Chad Rosierca2567b2011-12-07 21:44:12 +00001768 break;
Chris Lattner1314b992007-04-22 06:23:29 +00001769 }
1770 continue;
Joe Abbey97b7a172013-02-06 22:14:06 +00001771
Chris Lattner27d38752013-01-20 02:13:19 +00001772 case BitstreamEntry::Record:
1773 // The interesting case.
1774 break;
Chris Lattner1314b992007-04-22 06:23:29 +00001775 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001776
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001777
Chris Lattner1314b992007-04-22 06:23:29 +00001778 // Read a record.
Chris Lattner27d38752013-01-20 02:13:19 +00001779 switch (Stream.readRecord(Entry.ID, Record)) {
Chris Lattner1314b992007-04-22 06:23:29 +00001780 default: break; // Default behavior, ignore unknown content.
Jan Wen Voungafaced02012-10-11 20:20:40 +00001781 case bitc::MODULE_CODE_VERSION: { // VERSION: [version#]
Chris Lattner1314b992007-04-22 06:23:29 +00001782 if (Record.size() < 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001783 return Error(InvalidRecord);
Jan Wen Voungafaced02012-10-11 20:20:40 +00001784 // Only version #0 and #1 are supported so far.
1785 unsigned module_version = Record[0];
1786 switch (module_version) {
Rafael Espindola48da4f42013-11-04 16:16:24 +00001787 default:
1788 return Error(InvalidValue);
Jan Wen Voungafaced02012-10-11 20:20:40 +00001789 case 0:
1790 UseRelativeIDs = false;
1791 break;
1792 case 1:
1793 UseRelativeIDs = true;
1794 break;
1795 }
Chris Lattner1314b992007-04-22 06:23:29 +00001796 break;
Jan Wen Voungafaced02012-10-11 20:20:40 +00001797 }
Chris Lattnere14cb882007-05-04 19:11:41 +00001798 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00001799 std::string S;
1800 if (ConvertToString(Record, 0, S))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001801 return Error(InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001802 TheModule->setTargetTriple(S);
1803 break;
1804 }
Chris Lattnere14cb882007-05-04 19:11:41 +00001805 case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00001806 std::string S;
1807 if (ConvertToString(Record, 0, S))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001808 return Error(InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001809 TheModule->setDataLayout(S);
1810 break;
1811 }
Chris Lattnere14cb882007-05-04 19:11:41 +00001812 case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00001813 std::string S;
1814 if (ConvertToString(Record, 0, S))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001815 return Error(InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001816 TheModule->setModuleInlineAsm(S);
1817 break;
1818 }
Bill Wendling706d3d62012-11-28 08:41:48 +00001819 case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N]
1820 // FIXME: Remove in 4.0.
1821 std::string S;
1822 if (ConvertToString(Record, 0, S))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001823 return Error(InvalidRecord);
Bill Wendling706d3d62012-11-28 08:41:48 +00001824 // Ignore value.
1825 break;
1826 }
Chris Lattnere14cb882007-05-04 19:11:41 +00001827 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00001828 std::string S;
1829 if (ConvertToString(Record, 0, S))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001830 return Error(InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001831 SectionTable.push_back(S);
1832 break;
1833 }
Gordon Henriksend930f912008-08-17 18:44:35 +00001834 case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N]
Gordon Henriksen71183b62007-12-10 03:18:06 +00001835 std::string S;
1836 if (ConvertToString(Record, 0, S))
Rafael Espindola48da4f42013-11-04 16:16:24 +00001837 return Error(InvalidRecord);
Gordon Henriksend930f912008-08-17 18:44:35 +00001838 GCTable.push_back(S);
Gordon Henriksen71183b62007-12-10 03:18:06 +00001839 break;
1840 }
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001841 // GLOBALVAR: [pointer type, isconst, initid,
Rafael Espindola45e6c192011-01-08 16:42:36 +00001842 // linkage, alignment, section, visibility, threadlocal,
Nico Rieck7157bb72014-01-14 15:22:47 +00001843 // unnamed_addr, dllstorageclass]
Chris Lattner1314b992007-04-22 06:23:29 +00001844 case bitc::MODULE_CODE_GLOBALVAR: {
Chris Lattner4b00d922007-04-23 16:04:05 +00001845 if (Record.size() < 6)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001846 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00001847 Type *Ty = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001848 if (!Ty)
1849 return Error(InvalidRecord);
Duncan Sands19d0b472010-02-16 11:11:14 +00001850 if (!Ty->isPointerTy())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001851 return Error(InvalidTypeForValue);
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001852 unsigned AddressSpace = cast<PointerType>(Ty)->getAddressSpace();
Chris Lattner1314b992007-04-22 06:23:29 +00001853 Ty = cast<PointerType>(Ty)->getElementType();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001854
Chris Lattner1314b992007-04-22 06:23:29 +00001855 bool isConstant = Record[1];
1856 GlobalValue::LinkageTypes Linkage = GetDecodedLinkage(Record[3]);
1857 unsigned Alignment = (1 << Record[4]) >> 1;
1858 std::string Section;
1859 if (Record[5]) {
1860 if (Record[5]-1 >= SectionTable.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001861 return Error(InvalidID);
Chris Lattner1314b992007-04-22 06:23:29 +00001862 Section = SectionTable[Record[5]-1];
1863 }
Chris Lattner4b00d922007-04-23 16:04:05 +00001864 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00001865 // Local linkage must have default visibility.
1866 if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage))
1867 // FIXME: Change to an error if non-default in 4.0.
Chris Lattner53862f72007-05-06 19:27:46 +00001868 Visibility = GetDecodedVisibility(Record[6]);
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001869
1870 GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal;
Chris Lattner53862f72007-05-06 19:27:46 +00001871 if (Record.size() > 7)
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001872 TLM = GetDecodedThreadLocalMode(Record[7]);
Chris Lattner1314b992007-04-22 06:23:29 +00001873
Rafael Espindola45e6c192011-01-08 16:42:36 +00001874 bool UnnamedAddr = false;
1875 if (Record.size() > 8)
1876 UnnamedAddr = Record[8];
1877
Michael Gottesman27e7ef32013-02-05 05:57:38 +00001878 bool ExternallyInitialized = false;
1879 if (Record.size() > 9)
1880 ExternallyInitialized = Record[9];
1881
Chris Lattner1314b992007-04-22 06:23:29 +00001882 GlobalVariable *NewGV =
Craig Topper2617dcc2014-04-15 06:32:26 +00001883 new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, "", nullptr,
Michael Gottesman27e7ef32013-02-05 05:57:38 +00001884 TLM, AddressSpace, ExternallyInitialized);
Chris Lattner1314b992007-04-22 06:23:29 +00001885 NewGV->setAlignment(Alignment);
1886 if (!Section.empty())
1887 NewGV->setSection(Section);
1888 NewGV->setVisibility(Visibility);
Rafael Espindola45e6c192011-01-08 16:42:36 +00001889 NewGV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001890
Nico Rieck7157bb72014-01-14 15:22:47 +00001891 if (Record.size() > 10)
1892 NewGV->setDLLStorageClass(GetDecodedDLLStorageClass(Record[10]));
1893 else
1894 UpgradeDLLImportExportLinkage(NewGV, Record[3]);
1895
Chris Lattnerccaa4482007-04-23 21:26:05 +00001896 ValueList.push_back(NewGV);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001897
Chris Lattner47d131b2007-04-24 00:18:21 +00001898 // Remember which value to use for the global initializer.
1899 if (unsigned InitID = Record[2])
1900 GlobalInits.push_back(std::make_pair(NewGV, InitID-1));
Chris Lattner1314b992007-04-22 06:23:29 +00001901 break;
1902 }
Chris Lattner4c0a6d62007-05-08 05:38:01 +00001903 // FUNCTION: [type, callingconv, isproto, linkage, paramattr,
Nico Rieck7157bb72014-01-14 15:22:47 +00001904 // alignment, section, visibility, gc, unnamed_addr,
1905 // dllstorageclass]
Chris Lattner1314b992007-04-22 06:23:29 +00001906 case bitc::MODULE_CODE_FUNCTION: {
Chris Lattner4c0a6d62007-05-08 05:38:01 +00001907 if (Record.size() < 8)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001908 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00001909 Type *Ty = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001910 if (!Ty)
1911 return Error(InvalidRecord);
Duncan Sands19d0b472010-02-16 11:11:14 +00001912 if (!Ty->isPointerTy())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001913 return Error(InvalidTypeForValue);
Chris Lattner229907c2011-07-18 04:54:35 +00001914 FunctionType *FTy =
Chris Lattner1314b992007-04-22 06:23:29 +00001915 dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType());
1916 if (!FTy)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001917 return Error(InvalidTypeForValue);
Chris Lattner1314b992007-04-22 06:23:29 +00001918
Gabor Greife9ecc682008-04-06 20:25:17 +00001919 Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage,
1920 "", TheModule);
Chris Lattner1314b992007-04-22 06:23:29 +00001921
Sandeep Patel68c5f472009-09-02 08:44:58 +00001922 Func->setCallingConv(static_cast<CallingConv::ID>(Record[1]));
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001923 bool isProto = Record[2];
Chris Lattner1314b992007-04-22 06:23:29 +00001924 Func->setLinkage(GetDecodedLinkage(Record[3]));
Devang Patel4c758ea2008-09-25 21:00:45 +00001925 Func->setAttributes(getAttributes(Record[4]));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001926
Chris Lattner4c0a6d62007-05-08 05:38:01 +00001927 Func->setAlignment((1 << Record[5]) >> 1);
1928 if (Record[6]) {
1929 if (Record[6]-1 >= SectionTable.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001930 return Error(InvalidID);
Chris Lattner4c0a6d62007-05-08 05:38:01 +00001931 Func->setSection(SectionTable[Record[6]-1]);
Chris Lattner1314b992007-04-22 06:23:29 +00001932 }
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00001933 // Local linkage must have default visibility.
1934 if (!Func->hasLocalLinkage())
1935 // FIXME: Change to an error if non-default in 4.0.
1936 Func->setVisibility(GetDecodedVisibility(Record[7]));
Gordon Henriksen71183b62007-12-10 03:18:06 +00001937 if (Record.size() > 8 && Record[8]) {
Gordon Henriksend930f912008-08-17 18:44:35 +00001938 if (Record[8]-1 > GCTable.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001939 return Error(InvalidID);
Gordon Henriksend930f912008-08-17 18:44:35 +00001940 Func->setGC(GCTable[Record[8]-1].c_str());
Gordon Henriksen71183b62007-12-10 03:18:06 +00001941 }
Rafael Espindola45e6c192011-01-08 16:42:36 +00001942 bool UnnamedAddr = false;
1943 if (Record.size() > 9)
1944 UnnamedAddr = Record[9];
1945 Func->setUnnamedAddr(UnnamedAddr);
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001946 if (Record.size() > 10 && Record[10] != 0)
1947 FunctionPrefixes.push_back(std::make_pair(Func, Record[10]-1));
Nico Rieck7157bb72014-01-14 15:22:47 +00001948
1949 if (Record.size() > 11)
1950 Func->setDLLStorageClass(GetDecodedDLLStorageClass(Record[11]));
1951 else
1952 UpgradeDLLImportExportLinkage(Func, Record[3]);
1953
Chris Lattnerccaa4482007-04-23 21:26:05 +00001954 ValueList.push_back(Func);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001955
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001956 // If this is a function with a body, remember the prototype we are
1957 // creating now, so that we can match up the body with them later.
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001958 if (!isProto) {
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001959 FunctionsWithBodies.push_back(Func);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001960 if (LazyStreamer) DeferredFunctionInfo[Func] = 0;
1961 }
Chris Lattner1314b992007-04-22 06:23:29 +00001962 break;
1963 }
Anton Korobeynikov2f22e3f2008-03-12 00:49:19 +00001964 // ALIAS: [alias type, aliasee val#, linkage]
Nico Rieck7157bb72014-01-14 15:22:47 +00001965 // ALIAS: [alias type, aliasee val#, linkage, visibility, dllstorageclass]
Chris Lattner831d4202007-04-26 03:27:58 +00001966 case bitc::MODULE_CODE_ALIAS: {
Chris Lattner44c17072007-04-26 02:46:40 +00001967 if (Record.size() < 3)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001968 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00001969 Type *Ty = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001970 if (!Ty)
1971 return Error(InvalidRecord);
Rafael Espindolaa8004452014-05-16 14:22:33 +00001972 auto *PTy = dyn_cast<PointerType>(Ty);
1973 if (!PTy)
Rafael Espindola48da4f42013-11-04 16:16:24 +00001974 return Error(InvalidTypeForValue);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001975
Rafael Espindolaa8004452014-05-16 14:22:33 +00001976 auto *NewGA =
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +00001977 GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
1978 GetDecodedLinkage(Record[2]), "", TheModule);
Anton Korobeynikov2f22e3f2008-03-12 00:49:19 +00001979 // Old bitcode files didn't have visibility field.
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00001980 // Local linkage must have default visibility.
1981 if (Record.size() > 3 && !NewGA->hasLocalLinkage())
1982 // FIXME: Change to an error if non-default in 4.0.
Anton Korobeynikov2f22e3f2008-03-12 00:49:19 +00001983 NewGA->setVisibility(GetDecodedVisibility(Record[3]));
Nico Rieck7157bb72014-01-14 15:22:47 +00001984 if (Record.size() > 4)
1985 NewGA->setDLLStorageClass(GetDecodedDLLStorageClass(Record[4]));
1986 else
1987 UpgradeDLLImportExportLinkage(NewGA, Record[2]);
Rafael Espindola59f7eba2014-05-28 18:15:43 +00001988 if (Record.size() > 5)
1989 NewGA->setThreadLocalMode(GetDecodedThreadLocalMode(Record[5]));
Rafael Espindola42a4c9f2014-06-06 01:20:28 +00001990 if (Record.size() > 6)
1991 NewGA->setUnnamedAddr(Record[6]);
Chris Lattner44c17072007-04-26 02:46:40 +00001992 ValueList.push_back(NewGA);
1993 AliasInits.push_back(std::make_pair(NewGA, Record[1]));
1994 break;
Chris Lattner1314b992007-04-22 06:23:29 +00001995 }
Chris Lattner831d4202007-04-26 03:27:58 +00001996 /// MODULE_CODE_PURGEVALS: [numvals]
1997 case bitc::MODULE_CODE_PURGEVALS:
1998 // Trim down the value list to the specified size.
1999 if (Record.size() < 1 || Record[0] > ValueList.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002000 return Error(InvalidRecord);
Chris Lattner831d4202007-04-26 03:27:58 +00002001 ValueList.shrinkTo(Record[0]);
2002 break;
2003 }
Chris Lattner1314b992007-04-22 06:23:29 +00002004 Record.clear();
2005 }
Chris Lattner1314b992007-04-22 06:23:29 +00002006}
2007
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002008std::error_code BitcodeReader::ParseBitcodeInto(Module *M) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002009 TheModule = nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002010
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002011 if (std::error_code EC = InitStream())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002012 return EC;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002013
Chris Lattner1314b992007-04-22 06:23:29 +00002014 // Sniff for the signature.
2015 if (Stream.Read(8) != 'B' ||
2016 Stream.Read(8) != 'C' ||
2017 Stream.Read(4) != 0x0 ||
2018 Stream.Read(4) != 0xC ||
2019 Stream.Read(4) != 0xE ||
2020 Stream.Read(4) != 0xD)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002021 return Error(InvalidBitcodeSignature);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002022
Chris Lattner1314b992007-04-22 06:23:29 +00002023 // We expect a number of well-defined blocks, though we don't necessarily
2024 // need to understand them all.
Chris Lattner27d38752013-01-20 02:13:19 +00002025 while (1) {
2026 if (Stream.AtEndOfStream())
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002027 return std::error_code();
Joe Abbey97b7a172013-02-06 22:14:06 +00002028
Chris Lattner27d38752013-01-20 02:13:19 +00002029 BitstreamEntry Entry =
2030 Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
Joe Abbey97b7a172013-02-06 22:14:06 +00002031
Chris Lattner27d38752013-01-20 02:13:19 +00002032 switch (Entry.Kind) {
2033 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +00002034 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002035 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002036 return std::error_code();
Joe Abbey97b7a172013-02-06 22:14:06 +00002037
Chris Lattner27d38752013-01-20 02:13:19 +00002038 case BitstreamEntry::SubBlock:
2039 switch (Entry.ID) {
2040 case bitc::BLOCKINFO_BLOCK_ID:
2041 if (Stream.ReadBlockInfoBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002042 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002043 break;
2044 case bitc::MODULE_BLOCK_ID:
2045 // Reject multiple MODULE_BLOCK's in a single bitstream.
2046 if (TheModule)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002047 return Error(InvalidMultipleBlocks);
Chris Lattner27d38752013-01-20 02:13:19 +00002048 TheModule = M;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002049 if (std::error_code EC = ParseModule(false))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002050 return EC;
2051 if (LazyStreamer)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002052 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00002053 break;
2054 default:
2055 if (Stream.SkipBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002056 return Error(InvalidRecord);
Chris Lattner27d38752013-01-20 02:13:19 +00002057 break;
2058 }
2059 continue;
2060 case BitstreamEntry::Record:
2061 // There should be no records in the top-level of blocks.
Joe Abbey97b7a172013-02-06 22:14:06 +00002062
Chris Lattner27d38752013-01-20 02:13:19 +00002063 // The ranlib in Xcode 4 will align archive members by appending newlines
Chad Rosiera15e3aa2011-08-09 22:23:40 +00002064 // to the end of them. If this file size is a multiple of 4 but not 8, we
2065 // have to read and ignore these final 4 bytes :-(
Chris Lattner27d38752013-01-20 02:13:19 +00002066 if (Stream.getAbbrevIDWidth() == 2 && Entry.ID == 2 &&
Rafael Espindolaa97b2382011-05-26 18:59:54 +00002067 Stream.Read(6) == 2 && Stream.Read(24) == 0xa0a0a &&
Bill Wendling318f03f2012-07-19 00:15:11 +00002068 Stream.AtEndOfStream())
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002069 return std::error_code();
Joe Abbey97b7a172013-02-06 22:14:06 +00002070
Rafael Espindola48da4f42013-11-04 16:16:24 +00002071 return Error(InvalidRecord);
Rafael Espindolaa97b2382011-05-26 18:59:54 +00002072 }
Chris Lattner1314b992007-04-22 06:23:29 +00002073 }
Chris Lattner1314b992007-04-22 06:23:29 +00002074}
Chris Lattner6694f602007-04-29 07:54:31 +00002075
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002076std::error_code BitcodeReader::ParseModuleTriple(std::string &Triple) {
Bill Wendling0198ce02010-10-06 01:22:42 +00002077 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002078 return Error(InvalidRecord);
Bill Wendling0198ce02010-10-06 01:22:42 +00002079
2080 SmallVector<uint64_t, 64> Record;
2081
2082 // Read all the records for this module.
Chris Lattner27d38752013-01-20 02:13:19 +00002083 while (1) {
2084 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00002085
Chris Lattner27d38752013-01-20 02:13:19 +00002086 switch (Entry.Kind) {
2087 case BitstreamEntry::SubBlock: // Handled for us already.
2088 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +00002089 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002090 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002091 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00002092 case BitstreamEntry::Record:
2093 // The interesting case.
2094 break;
Bill Wendling0198ce02010-10-06 01:22:42 +00002095 }
2096
2097 // Read a record.
Chris Lattner27d38752013-01-20 02:13:19 +00002098 switch (Stream.readRecord(Entry.ID, Record)) {
Bill Wendling0198ce02010-10-06 01:22:42 +00002099 default: break; // Default behavior, ignore unknown content.
Bill Wendling0198ce02010-10-06 01:22:42 +00002100 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
2101 std::string S;
2102 if (ConvertToString(Record, 0, S))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002103 return Error(InvalidRecord);
Bill Wendling0198ce02010-10-06 01:22:42 +00002104 Triple = S;
2105 break;
2106 }
2107 }
2108 Record.clear();
2109 }
Bill Wendling0198ce02010-10-06 01:22:42 +00002110}
2111
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002112std::error_code BitcodeReader::ParseTriple(std::string &Triple) {
2113 if (std::error_code EC = InitStream())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002114 return EC;
Bill Wendling0198ce02010-10-06 01:22:42 +00002115
2116 // Sniff for the signature.
2117 if (Stream.Read(8) != 'B' ||
2118 Stream.Read(8) != 'C' ||
2119 Stream.Read(4) != 0x0 ||
2120 Stream.Read(4) != 0xC ||
2121 Stream.Read(4) != 0xE ||
2122 Stream.Read(4) != 0xD)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002123 return Error(InvalidBitcodeSignature);
Bill Wendling0198ce02010-10-06 01:22:42 +00002124
2125 // We expect a number of well-defined blocks, though we don't necessarily
2126 // need to understand them all.
Chris Lattner27d38752013-01-20 02:13:19 +00002127 while (1) {
2128 BitstreamEntry Entry = Stream.advance();
Joe Abbey97b7a172013-02-06 22:14:06 +00002129
Chris Lattner27d38752013-01-20 02:13:19 +00002130 switch (Entry.Kind) {
2131 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +00002132 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002133 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002134 return std::error_code();
Joe Abbey97b7a172013-02-06 22:14:06 +00002135
Chris Lattner27d38752013-01-20 02:13:19 +00002136 case BitstreamEntry::SubBlock:
2137 if (Entry.ID == bitc::MODULE_BLOCK_ID)
2138 return ParseModuleTriple(Triple);
Joe Abbey97b7a172013-02-06 22:14:06 +00002139
Chris Lattner27d38752013-01-20 02:13:19 +00002140 // Ignore other sub-blocks.
Rafael Espindola48da4f42013-11-04 16:16:24 +00002141 if (Stream.SkipBlock())
2142 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002143 continue;
Joe Abbey97b7a172013-02-06 22:14:06 +00002144
Chris Lattner27d38752013-01-20 02:13:19 +00002145 case BitstreamEntry::Record:
2146 Stream.skipRecord(Entry.ID);
2147 continue;
Bill Wendling0198ce02010-10-06 01:22:42 +00002148 }
2149 }
Bill Wendling0198ce02010-10-06 01:22:42 +00002150}
2151
Devang Patelaf206b82009-09-18 19:26:43 +00002152/// ParseMetadataAttachment - Parse metadata attachments.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002153std::error_code BitcodeReader::ParseMetadataAttachment() {
Devang Patelaf206b82009-09-18 19:26:43 +00002154 if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002155 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002156
Devang Patelaf206b82009-09-18 19:26:43 +00002157 SmallVector<uint64_t, 64> Record;
Chris Lattner27d38752013-01-20 02:13:19 +00002158 while (1) {
2159 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00002160
Chris Lattner27d38752013-01-20 02:13:19 +00002161 switch (Entry.Kind) {
2162 case BitstreamEntry::SubBlock: // Handled for us already.
2163 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +00002164 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002165 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002166 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00002167 case BitstreamEntry::Record:
2168 // The interesting case.
Devang Patelaf206b82009-09-18 19:26:43 +00002169 break;
2170 }
Chris Lattner27d38752013-01-20 02:13:19 +00002171
Devang Patelaf206b82009-09-18 19:26:43 +00002172 // Read a metadata attachment record.
2173 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +00002174 switch (Stream.readRecord(Entry.ID, Record)) {
Devang Patelaf206b82009-09-18 19:26:43 +00002175 default: // Default behavior: ignore.
2176 break;
Chris Lattnerb8778552011-06-17 17:50:30 +00002177 case bitc::METADATA_ATTACHMENT: {
Devang Patelaf206b82009-09-18 19:26:43 +00002178 unsigned RecordLength = Record.size();
2179 if (Record.empty() || (RecordLength - 1) % 2 == 1)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002180 return Error(InvalidRecord);
Devang Patelaf206b82009-09-18 19:26:43 +00002181 Instruction *Inst = InstructionList[Record[0]];
2182 for (unsigned i = 1; i != RecordLength; i = i+2) {
Devang Patelb1a44772009-09-28 21:14:55 +00002183 unsigned Kind = Record[i];
Dan Gohman43aa8f02010-07-20 21:42:28 +00002184 DenseMap<unsigned, unsigned>::iterator I =
2185 MDKindMap.find(Kind);
2186 if (I == MDKindMap.end())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002187 return Error(InvalidID);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002188 Value *Node = MDValueList.getValueFwdRef(Record[i+1]);
Dan Gohman43aa8f02010-07-20 21:42:28 +00002189 Inst->setMetadata(I->second, cast<MDNode>(Node));
Manman Ren209b17c2013-09-28 00:22:27 +00002190 if (I->second == LLVMContext::MD_tbaa)
2191 InstsWithTBAATag.push_back(Inst);
Devang Patelaf206b82009-09-18 19:26:43 +00002192 }
2193 break;
2194 }
2195 }
2196 }
Devang Patelaf206b82009-09-18 19:26:43 +00002197}
Chris Lattner51ffe7c2007-05-01 04:59:48 +00002198
Chris Lattner85b7b402007-05-01 05:52:21 +00002199/// ParseFunctionBody - Lazily parse the specified function body block.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002200std::error_code BitcodeReader::ParseFunctionBody(Function *F) {
Chris Lattner982ec1e2007-05-05 00:17:00 +00002201 if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002202 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002203
Nick Lewyckya72e1af2010-02-25 08:30:17 +00002204 InstructionList.clear();
Chris Lattner85b7b402007-05-01 05:52:21 +00002205 unsigned ModuleValueListSize = ValueList.size();
Dan Gohman26d837d2010-08-25 20:22:53 +00002206 unsigned ModuleMDValueListSize = MDValueList.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002207
Chris Lattner85b7b402007-05-01 05:52:21 +00002208 // Add all the function arguments to the value table.
2209 for(Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
2210 ValueList.push_back(I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002211
Chris Lattner83930552007-05-01 07:01:57 +00002212 unsigned NextValueNo = ValueList.size();
Craig Topper2617dcc2014-04-15 06:32:26 +00002213 BasicBlock *CurBB = nullptr;
Chris Lattnere53603e2007-05-02 04:27:25 +00002214 unsigned CurBBNo = 0;
2215
Chris Lattner07d09ed2010-04-03 02:17:50 +00002216 DebugLoc LastLoc;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002217
Chris Lattner85b7b402007-05-01 05:52:21 +00002218 // Read all the records.
2219 SmallVector<uint64_t, 64> Record;
2220 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00002221 BitstreamEntry Entry = Stream.advance();
Joe Abbey97b7a172013-02-06 22:14:06 +00002222
Chris Lattner27d38752013-01-20 02:13:19 +00002223 switch (Entry.Kind) {
2224 case BitstreamEntry::Error:
Rafael Espindola48da4f42013-11-04 16:16:24 +00002225 return Error(MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002226 case BitstreamEntry::EndBlock:
2227 goto OutOfRecordLoop;
Joe Abbey97b7a172013-02-06 22:14:06 +00002228
Chris Lattner27d38752013-01-20 02:13:19 +00002229 case BitstreamEntry::SubBlock:
2230 switch (Entry.ID) {
Chris Lattner85b7b402007-05-01 05:52:21 +00002231 default: // Skip unknown content.
2232 if (Stream.SkipBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002233 return Error(InvalidRecord);
Chris Lattner85b7b402007-05-01 05:52:21 +00002234 break;
2235 case bitc::CONSTANTS_BLOCK_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002236 if (std::error_code EC = ParseConstants())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002237 return EC;
Chris Lattner83930552007-05-01 07:01:57 +00002238 NextValueNo = ValueList.size();
Chris Lattner85b7b402007-05-01 05:52:21 +00002239 break;
2240 case bitc::VALUE_SYMTAB_BLOCK_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002241 if (std::error_code EC = ParseValueSymbolTable())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002242 return EC;
Chris Lattner85b7b402007-05-01 05:52:21 +00002243 break;
Devang Patelaf206b82009-09-18 19:26:43 +00002244 case bitc::METADATA_ATTACHMENT_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002245 if (std::error_code EC = ParseMetadataAttachment())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002246 return EC;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002247 break;
Victor Hernandez108d3ac2010-01-13 19:34:08 +00002248 case bitc::METADATA_BLOCK_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002249 if (std::error_code EC = ParseMetadata())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002250 return EC;
Victor Hernandez108d3ac2010-01-13 19:34:08 +00002251 break;
Chris Lattner85b7b402007-05-01 05:52:21 +00002252 }
2253 continue;
Joe Abbey97b7a172013-02-06 22:14:06 +00002254
Chris Lattner27d38752013-01-20 02:13:19 +00002255 case BitstreamEntry::Record:
2256 // The interesting case.
2257 break;
Chris Lattner85b7b402007-05-01 05:52:21 +00002258 }
Joe Abbey97b7a172013-02-06 22:14:06 +00002259
Chris Lattner85b7b402007-05-01 05:52:21 +00002260 // Read a record.
2261 Record.clear();
Craig Topper2617dcc2014-04-15 06:32:26 +00002262 Instruction *I = nullptr;
Chris Lattner27d38752013-01-20 02:13:19 +00002263 unsigned BitCode = Stream.readRecord(Entry.ID, Record);
Dan Gohman0ebd6962009-07-20 21:19:07 +00002264 switch (BitCode) {
Chris Lattner83930552007-05-01 07:01:57 +00002265 default: // Default behavior: reject
Rafael Espindola48da4f42013-11-04 16:16:24 +00002266 return Error(InvalidValue);
Chris Lattner85b7b402007-05-01 05:52:21 +00002267 case bitc::FUNC_CODE_DECLAREBLOCKS: // DECLAREBLOCKS: [nblocks]
Chris Lattner83930552007-05-01 07:01:57 +00002268 if (Record.size() < 1 || Record[0] == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002269 return Error(InvalidRecord);
Chris Lattner85b7b402007-05-01 05:52:21 +00002270 // Create all the basic blocks for the function.
Chris Lattner6ce15cb2007-05-03 22:09:51 +00002271 FunctionBBs.resize(Record[0]);
Chris Lattner85b7b402007-05-01 05:52:21 +00002272 for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i)
Owen Anderson55f1c092009-08-13 21:58:54 +00002273 FunctionBBs[i] = BasicBlock::Create(Context, "", F);
Chris Lattner83930552007-05-01 07:01:57 +00002274 CurBB = FunctionBBs[0];
2275 continue;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002276
Chris Lattner07d09ed2010-04-03 02:17:50 +00002277 case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN
2278 // This record indicates that the last instruction is at the same
2279 // location as the previous instruction with a location.
Craig Topper2617dcc2014-04-15 06:32:26 +00002280 I = nullptr;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002281
Chris Lattner07d09ed2010-04-03 02:17:50 +00002282 // Get the last instruction emitted.
2283 if (CurBB && !CurBB->empty())
2284 I = &CurBB->back();
2285 else if (CurBBNo && FunctionBBs[CurBBNo-1] &&
2286 !FunctionBBs[CurBBNo-1]->empty())
2287 I = &FunctionBBs[CurBBNo-1]->back();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002288
Craig Topper2617dcc2014-04-15 06:32:26 +00002289 if (!I)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002290 return Error(InvalidRecord);
Chris Lattner07d09ed2010-04-03 02:17:50 +00002291 I->setDebugLoc(LastLoc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002292 I = nullptr;
Chris Lattner07d09ed2010-04-03 02:17:50 +00002293 continue;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002294
Chris Lattnerc44070802011-06-17 18:17:37 +00002295 case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia]
Craig Topper2617dcc2014-04-15 06:32:26 +00002296 I = nullptr; // Get the last instruction emitted.
Chris Lattner07d09ed2010-04-03 02:17:50 +00002297 if (CurBB && !CurBB->empty())
2298 I = &CurBB->back();
2299 else if (CurBBNo && FunctionBBs[CurBBNo-1] &&
2300 !FunctionBBs[CurBBNo-1]->empty())
2301 I = &FunctionBBs[CurBBNo-1]->back();
Craig Topper2617dcc2014-04-15 06:32:26 +00002302 if (!I || Record.size() < 4)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002303 return Error(InvalidRecord);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002304
Chris Lattner07d09ed2010-04-03 02:17:50 +00002305 unsigned Line = Record[0], Col = Record[1];
2306 unsigned ScopeID = Record[2], IAID = Record[3];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002307
Craig Topper2617dcc2014-04-15 06:32:26 +00002308 MDNode *Scope = nullptr, *IA = nullptr;
Chris Lattner07d09ed2010-04-03 02:17:50 +00002309 if (ScopeID) Scope = cast<MDNode>(MDValueList.getValueFwdRef(ScopeID-1));
2310 if (IAID) IA = cast<MDNode>(MDValueList.getValueFwdRef(IAID-1));
2311 LastLoc = DebugLoc::get(Line, Col, Scope, IA);
2312 I->setDebugLoc(LastLoc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002313 I = nullptr;
Chris Lattner07d09ed2010-04-03 02:17:50 +00002314 continue;
2315 }
2316
Chris Lattnere9759c22007-05-06 00:21:25 +00002317 case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode]
2318 unsigned OpNum = 0;
2319 Value *LHS, *RHS;
2320 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002321 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
Dan Gohman0ebd6962009-07-20 21:19:07 +00002322 OpNum+1 > Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002323 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002324
Dan Gohman0ebd6962009-07-20 21:19:07 +00002325 int Opc = GetDecodedBinaryOpcode(Record[OpNum++], LHS->getType());
Rafael Espindola48da4f42013-11-04 16:16:24 +00002326 if (Opc == -1)
2327 return Error(InvalidRecord);
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002328 I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
Devang Patelaf206b82009-09-18 19:26:43 +00002329 InstructionList.push_back(I);
Dan Gohman1b849082009-09-07 23:54:19 +00002330 if (OpNum < Record.size()) {
2331 if (Opc == Instruction::Add ||
2332 Opc == Instruction::Sub ||
Chris Lattnera676c0f2011-02-07 16:40:21 +00002333 Opc == Instruction::Mul ||
2334 Opc == Instruction::Shl) {
Dan Gohman00f47472010-01-25 21:55:39 +00002335 if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP))
Dan Gohman1b849082009-09-07 23:54:19 +00002336 cast<BinaryOperator>(I)->setHasNoSignedWrap(true);
Dan Gohman00f47472010-01-25 21:55:39 +00002337 if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
Dan Gohman1b849082009-09-07 23:54:19 +00002338 cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true);
Chris Lattner35315d02011-02-06 21:44:57 +00002339 } else if (Opc == Instruction::SDiv ||
Chris Lattnera676c0f2011-02-07 16:40:21 +00002340 Opc == Instruction::UDiv ||
2341 Opc == Instruction::LShr ||
2342 Opc == Instruction::AShr) {
Chris Lattner35315d02011-02-06 21:44:57 +00002343 if (Record[OpNum] & (1 << bitc::PEO_EXACT))
Dan Gohman1b849082009-09-07 23:54:19 +00002344 cast<BinaryOperator>(I)->setIsExact(true);
Michael Ilseman9978d7e2012-11-27 00:43:38 +00002345 } else if (isa<FPMathOperator>(I)) {
2346 FastMathFlags FMF;
Michael Ilseman65f14352012-12-09 21:12:04 +00002347 if (0 != (Record[OpNum] & FastMathFlags::UnsafeAlgebra))
2348 FMF.setUnsafeAlgebra();
2349 if (0 != (Record[OpNum] & FastMathFlags::NoNaNs))
2350 FMF.setNoNaNs();
2351 if (0 != (Record[OpNum] & FastMathFlags::NoInfs))
2352 FMF.setNoInfs();
2353 if (0 != (Record[OpNum] & FastMathFlags::NoSignedZeros))
2354 FMF.setNoSignedZeros();
2355 if (0 != (Record[OpNum] & FastMathFlags::AllowReciprocal))
2356 FMF.setAllowReciprocal();
Michael Ilseman9978d7e2012-11-27 00:43:38 +00002357 if (FMF.any())
2358 I->setFastMathFlags(FMF);
Dan Gohman1b849082009-09-07 23:54:19 +00002359 }
Michael Ilseman9978d7e2012-11-27 00:43:38 +00002360
Dan Gohman1b849082009-09-07 23:54:19 +00002361 }
Chris Lattner85b7b402007-05-01 05:52:21 +00002362 break;
2363 }
Chris Lattnere9759c22007-05-06 00:21:25 +00002364 case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc]
2365 unsigned OpNum = 0;
2366 Value *Op;
2367 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
2368 OpNum+2 != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002369 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002370
Chris Lattner229907c2011-07-18 04:54:35 +00002371 Type *ResTy = getTypeByID(Record[OpNum]);
Chris Lattnere9759c22007-05-06 00:21:25 +00002372 int Opc = GetDecodedCastOpcode(Record[OpNum+1]);
Craig Topper2617dcc2014-04-15 06:32:26 +00002373 if (Opc == -1 || !ResTy)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002374 return Error(InvalidRecord);
Craig Topper2617dcc2014-04-15 06:32:26 +00002375 Instruction *Temp = nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002376 if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) {
2377 if (Temp) {
2378 InstructionList.push_back(Temp);
2379 CurBB->getInstList().push_back(Temp);
2380 }
2381 } else {
2382 I = CastInst::Create((Instruction::CastOps)Opc, Op, ResTy);
2383 }
Devang Patelaf206b82009-09-18 19:26:43 +00002384 InstructionList.push_back(I);
Chris Lattnere53603e2007-05-02 04:27:25 +00002385 break;
2386 }
Dan Gohman1639c392009-07-27 21:53:46 +00002387 case bitc::FUNC_CODE_INST_INBOUNDS_GEP:
Chris Lattnere14cb882007-05-04 19:11:41 +00002388 case bitc::FUNC_CODE_INST_GEP: { // GEP: [n x operands]
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002389 unsigned OpNum = 0;
2390 Value *BasePtr;
2391 if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002392 return Error(InvalidRecord);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002393
Chris Lattner5285b5e2007-05-02 05:46:45 +00002394 SmallVector<Value*, 16> GEPIdx;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002395 while (OpNum != Record.size()) {
2396 Value *Op;
2397 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002398 return Error(InvalidRecord);
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002399 GEPIdx.push_back(Op);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002400 }
2401
Jay Foadd1b78492011-07-25 09:48:08 +00002402 I = GetElementPtrInst::Create(BasePtr, GEPIdx);
Devang Patelaf206b82009-09-18 19:26:43 +00002403 InstructionList.push_back(I);
Dan Gohman1639c392009-07-27 21:53:46 +00002404 if (BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP)
Dan Gohman1b849082009-09-07 23:54:19 +00002405 cast<GetElementPtrInst>(I)->setIsInBounds(true);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002406 break;
2407 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002408
Dan Gohman1ecaf452008-05-31 00:58:22 +00002409 case bitc::FUNC_CODE_INST_EXTRACTVAL: {
2410 // EXTRACTVAL: [opty, opval, n x indices]
Dan Gohman30499842008-05-23 01:55:30 +00002411 unsigned OpNum = 0;
2412 Value *Agg;
2413 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002414 return Error(InvalidRecord);
Dan Gohman30499842008-05-23 01:55:30 +00002415
Dan Gohman1ecaf452008-05-31 00:58:22 +00002416 SmallVector<unsigned, 4> EXTRACTVALIdx;
2417 for (unsigned RecSize = Record.size();
2418 OpNum != RecSize; ++OpNum) {
2419 uint64_t Index = Record[OpNum];
2420 if ((unsigned)Index != Index)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002421 return Error(InvalidValue);
Dan Gohman1ecaf452008-05-31 00:58:22 +00002422 EXTRACTVALIdx.push_back((unsigned)Index);
Dan Gohman30499842008-05-23 01:55:30 +00002423 }
2424
Jay Foad57aa6362011-07-13 10:26:04 +00002425 I = ExtractValueInst::Create(Agg, EXTRACTVALIdx);
Devang Patelaf206b82009-09-18 19:26:43 +00002426 InstructionList.push_back(I);
Dan Gohman30499842008-05-23 01:55:30 +00002427 break;
2428 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002429
Dan Gohman1ecaf452008-05-31 00:58:22 +00002430 case bitc::FUNC_CODE_INST_INSERTVAL: {
2431 // INSERTVAL: [opty, opval, opty, opval, n x indices]
Dan Gohman30499842008-05-23 01:55:30 +00002432 unsigned OpNum = 0;
2433 Value *Agg;
2434 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002435 return Error(InvalidRecord);
Dan Gohman30499842008-05-23 01:55:30 +00002436 Value *Val;
2437 if (getValueTypePair(Record, OpNum, NextValueNo, Val))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002438 return Error(InvalidRecord);
Dan Gohman30499842008-05-23 01:55:30 +00002439
Dan Gohman1ecaf452008-05-31 00:58:22 +00002440 SmallVector<unsigned, 4> INSERTVALIdx;
2441 for (unsigned RecSize = Record.size();
2442 OpNum != RecSize; ++OpNum) {
2443 uint64_t Index = Record[OpNum];
2444 if ((unsigned)Index != Index)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002445 return Error(InvalidValue);
Dan Gohman1ecaf452008-05-31 00:58:22 +00002446 INSERTVALIdx.push_back((unsigned)Index);
Dan Gohman30499842008-05-23 01:55:30 +00002447 }
2448
Jay Foad57aa6362011-07-13 10:26:04 +00002449 I = InsertValueInst::Create(Agg, Val, INSERTVALIdx);
Devang Patelaf206b82009-09-18 19:26:43 +00002450 InstructionList.push_back(I);
Dan Gohman30499842008-05-23 01:55:30 +00002451 break;
2452 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002453
Chris Lattnere9759c22007-05-06 00:21:25 +00002454 case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval]
Dan Gohmanc5d28922008-09-16 01:01:33 +00002455 // obsolete form of select
2456 // handles select i1 ... in old bitcode
Chris Lattnere9759c22007-05-06 00:21:25 +00002457 unsigned OpNum = 0;
2458 Value *TrueVal, *FalseVal, *Cond;
2459 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002460 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
2461 popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002462 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002463
Dan Gohmanc5d28922008-09-16 01:01:33 +00002464 I = SelectInst::Create(Cond, TrueVal, FalseVal);
Devang Patelaf206b82009-09-18 19:26:43 +00002465 InstructionList.push_back(I);
Dan Gohmanc5d28922008-09-16 01:01:33 +00002466 break;
2467 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002468
Dan Gohmanc5d28922008-09-16 01:01:33 +00002469 case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred]
2470 // new form of select
2471 // handles select i1 or select [N x i1]
2472 unsigned OpNum = 0;
2473 Value *TrueVal, *FalseVal, *Cond;
2474 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002475 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
Dan Gohmanc5d28922008-09-16 01:01:33 +00002476 getValueTypePair(Record, OpNum, NextValueNo, Cond))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002477 return Error(InvalidRecord);
Dan Gohmanc579d972008-09-09 01:02:47 +00002478
2479 // select condition can be either i1 or [N x i1]
Chris Lattner229907c2011-07-18 04:54:35 +00002480 if (VectorType* vector_type =
2481 dyn_cast<VectorType>(Cond->getType())) {
Dan Gohmanc579d972008-09-09 01:02:47 +00002482 // expect <n x i1>
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002483 if (vector_type->getElementType() != Type::getInt1Ty(Context))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002484 return Error(InvalidTypeForValue);
Dan Gohmanc579d972008-09-09 01:02:47 +00002485 } else {
2486 // expect i1
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002487 if (Cond->getType() != Type::getInt1Ty(Context))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002488 return Error(InvalidTypeForValue);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002489 }
2490
Gabor Greife9ecc682008-04-06 20:25:17 +00002491 I = SelectInst::Create(Cond, TrueVal, FalseVal);
Devang Patelaf206b82009-09-18 19:26:43 +00002492 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002493 break;
2494 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002495
Chris Lattner1fc27f02007-05-02 05:16:49 +00002496 case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval]
Chris Lattnere9759c22007-05-06 00:21:25 +00002497 unsigned OpNum = 0;
2498 Value *Vec, *Idx;
2499 if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002500 getValueTypePair(Record, OpNum, NextValueNo, Idx))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002501 return Error(InvalidRecord);
Eric Christopherc9742252009-07-25 02:28:41 +00002502 I = ExtractElementInst::Create(Vec, Idx);
Devang Patelaf206b82009-09-18 19:26:43 +00002503 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002504 break;
2505 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002506
Chris Lattner1fc27f02007-05-02 05:16:49 +00002507 case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval]
Chris Lattnere9759c22007-05-06 00:21:25 +00002508 unsigned OpNum = 0;
2509 Value *Vec, *Elt, *Idx;
2510 if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002511 popValue(Record, OpNum, NextValueNo,
Chris Lattnere9759c22007-05-06 00:21:25 +00002512 cast<VectorType>(Vec->getType())->getElementType(), Elt) ||
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002513 getValueTypePair(Record, OpNum, NextValueNo, Idx))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002514 return Error(InvalidRecord);
Gabor Greife9ecc682008-04-06 20:25:17 +00002515 I = InsertElementInst::Create(Vec, Elt, Idx);
Devang Patelaf206b82009-09-18 19:26:43 +00002516 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002517 break;
2518 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002519
Chris Lattnere9759c22007-05-06 00:21:25 +00002520 case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval]
2521 unsigned OpNum = 0;
2522 Value *Vec1, *Vec2, *Mask;
2523 if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002524 popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002525 return Error(InvalidRecord);
Chris Lattnere9759c22007-05-06 00:21:25 +00002526
Mon P Wang25f01062008-11-10 04:46:22 +00002527 if (getValueTypePair(Record, OpNum, NextValueNo, Mask))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002528 return Error(InvalidRecord);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002529 I = new ShuffleVectorInst(Vec1, Vec2, Mask);
Devang Patelaf206b82009-09-18 19:26:43 +00002530 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002531 break;
2532 }
Mon P Wang25f01062008-11-10 04:46:22 +00002533
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002534 case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred]
2535 // Old form of ICmp/FCmp returning bool
2536 // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were
2537 // both legal on vectors but had different behaviour.
2538 case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred]
2539 // FCmp/ICmp returning bool or vector of bool
2540
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002541 unsigned OpNum = 0;
2542 Value *LHS, *RHS;
2543 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002544 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002545 OpNum+1 != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002546 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002547
Duncan Sands9dff9be2010-02-15 16:12:20 +00002548 if (LHS->getType()->isFPOrFPVectorTy())
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002549 I = new FCmpInst((FCmpInst::Predicate)Record[OpNum], LHS, RHS);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002550 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002551 I = new ICmpInst((ICmpInst::Predicate)Record[OpNum], LHS, RHS);
Devang Patelaf206b82009-09-18 19:26:43 +00002552 InstructionList.push_back(I);
Dan Gohmanc579d972008-09-09 01:02:47 +00002553 break;
2554 }
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002555
Chris Lattnere53603e2007-05-02 04:27:25 +00002556 case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>]
Devang Patelbbfd8742008-02-26 01:29:32 +00002557 {
2558 unsigned Size = Record.size();
2559 if (Size == 0) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002560 I = ReturnInst::Create(Context);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002561 InstructionList.push_back(I);
Devang Patelbbfd8742008-02-26 01:29:32 +00002562 break;
Dan Gohmanfa1211f2008-07-23 00:34:11 +00002563 }
Devang Patelbbfd8742008-02-26 01:29:32 +00002564
Dan Gohmanfa1211f2008-07-23 00:34:11 +00002565 unsigned OpNum = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +00002566 Value *Op = nullptr;
Chris Lattnerf1c87102011-06-17 18:09:11 +00002567 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002568 return Error(InvalidRecord);
Chris Lattnerf1c87102011-06-17 18:09:11 +00002569 if (OpNum != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002570 return Error(InvalidRecord);
Dan Gohmanfa1211f2008-07-23 00:34:11 +00002571
Chris Lattnerf1c87102011-06-17 18:09:11 +00002572 I = ReturnInst::Create(Context, Op);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002573 InstructionList.push_back(I);
Dan Gohmanfa1211f2008-07-23 00:34:11 +00002574 break;
Chris Lattnere53603e2007-05-02 04:27:25 +00002575 }
Chris Lattner5285b5e2007-05-02 05:46:45 +00002576 case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#]
Chris Lattner6ce15cb2007-05-03 22:09:51 +00002577 if (Record.size() != 1 && Record.size() != 3)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002578 return Error(InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002579 BasicBlock *TrueDest = getBasicBlock(Record[0]);
Craig Topper2617dcc2014-04-15 06:32:26 +00002580 if (!TrueDest)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002581 return Error(InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002582
Devang Patelaf206b82009-09-18 19:26:43 +00002583 if (Record.size() == 1) {
Gabor Greife9ecc682008-04-06 20:25:17 +00002584 I = BranchInst::Create(TrueDest);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002585 InstructionList.push_back(I);
Devang Patelaf206b82009-09-18 19:26:43 +00002586 }
Chris Lattner5285b5e2007-05-02 05:46:45 +00002587 else {
2588 BasicBlock *FalseDest = getBasicBlock(Record[1]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002589 Value *Cond = getValue(Record, 2, NextValueNo,
2590 Type::getInt1Ty(Context));
Craig Topper2617dcc2014-04-15 06:32:26 +00002591 if (!FalseDest || !Cond)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002592 return Error(InvalidRecord);
Gabor Greife9ecc682008-04-06 20:25:17 +00002593 I = BranchInst::Create(TrueDest, FalseDest, Cond);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002594 InstructionList.push_back(I);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002595 }
2596 break;
2597 }
Chris Lattner3ed871f2009-10-27 19:13:16 +00002598 case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...]
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002599 // Check magic
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002600 if ((Record[0] >> 16) == SWITCH_INST_MAGIC) {
Bob Wilsone4077362013-09-09 19:14:35 +00002601 // "New" SwitchInst format with case ranges. The changes to write this
2602 // format were reverted but we still recognize bitcode that uses it.
2603 // Hopefully someday we will have support for case ranges and can use
2604 // this format again.
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002605
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002606 Type *OpTy = getTypeByID(Record[1]);
2607 unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth();
2608
Jan Wen Voungafaced02012-10-11 20:20:40 +00002609 Value *Cond = getValue(Record, 2, NextValueNo, OpTy);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002610 BasicBlock *Default = getBasicBlock(Record[3]);
Craig Topper2617dcc2014-04-15 06:32:26 +00002611 if (!OpTy || !Cond || !Default)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002612 return Error(InvalidRecord);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002613
2614 unsigned NumCases = Record[4];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002615
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002616 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
2617 InstructionList.push_back(SI);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002618
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002619 unsigned CurIdx = 5;
2620 for (unsigned i = 0; i != NumCases; ++i) {
Bob Wilsone4077362013-09-09 19:14:35 +00002621 SmallVector<ConstantInt*, 1> CaseVals;
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002622 unsigned NumItems = Record[CurIdx++];
2623 for (unsigned ci = 0; ci != NumItems; ++ci) {
2624 bool isSingleNumber = Record[CurIdx++];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002625
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002626 APInt Low;
2627 unsigned ActiveWords = 1;
2628 if (ValueBitWidth > 64)
2629 ActiveWords = Record[CurIdx++];
Benjamin Kramer9704ed02012-05-28 14:10:31 +00002630 Low = ReadWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
2631 ValueBitWidth);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002632 CurIdx += ActiveWords;
Stepan Dyatkovskiye3e19cb2012-05-28 12:39:09 +00002633
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002634 if (!isSingleNumber) {
2635 ActiveWords = 1;
2636 if (ValueBitWidth > 64)
2637 ActiveWords = Record[CurIdx++];
2638 APInt High =
Benjamin Kramer9704ed02012-05-28 14:10:31 +00002639 ReadWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
2640 ValueBitWidth);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002641 CurIdx += ActiveWords;
Bob Wilsone4077362013-09-09 19:14:35 +00002642
2643 // FIXME: It is not clear whether values in the range should be
2644 // compared as signed or unsigned values. The partially
2645 // implemented changes that used this format in the past used
2646 // unsigned comparisons.
2647 for ( ; Low.ule(High); ++Low)
2648 CaseVals.push_back(ConstantInt::get(Context, Low));
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002649 } else
Bob Wilsone4077362013-09-09 19:14:35 +00002650 CaseVals.push_back(ConstantInt::get(Context, Low));
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002651 }
2652 BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]);
Bob Wilsone4077362013-09-09 19:14:35 +00002653 for (SmallVector<ConstantInt*, 1>::iterator cvi = CaseVals.begin(),
2654 cve = CaseVals.end(); cvi != cve; ++cvi)
2655 SI->addCase(*cvi, DestBB);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002656 }
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002657 I = SI;
2658 break;
2659 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002660
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002661 // Old SwitchInst format without case ranges.
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002662
Chris Lattner5285b5e2007-05-02 05:46:45 +00002663 if (Record.size() < 3 || (Record.size() & 1) == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002664 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00002665 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002666 Value *Cond = getValue(Record, 1, NextValueNo, OpTy);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002667 BasicBlock *Default = getBasicBlock(Record[2]);
Craig Topper2617dcc2014-04-15 06:32:26 +00002668 if (!OpTy || !Cond || !Default)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002669 return Error(InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002670 unsigned NumCases = (Record.size()-3)/2;
Gabor Greife9ecc682008-04-06 20:25:17 +00002671 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
Devang Patelaf206b82009-09-18 19:26:43 +00002672 InstructionList.push_back(SI);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002673 for (unsigned i = 0, e = NumCases; i != e; ++i) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002674 ConstantInt *CaseVal =
Chris Lattner5285b5e2007-05-02 05:46:45 +00002675 dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy));
2676 BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]);
Craig Topper2617dcc2014-04-15 06:32:26 +00002677 if (!CaseVal || !DestBB) {
Chris Lattner5285b5e2007-05-02 05:46:45 +00002678 delete SI;
Rafael Espindola48da4f42013-11-04 16:16:24 +00002679 return Error(InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002680 }
2681 SI->addCase(CaseVal, DestBB);
2682 }
2683 I = SI;
2684 break;
2685 }
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00002686 case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...]
Chris Lattner3ed871f2009-10-27 19:13:16 +00002687 if (Record.size() < 2)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002688 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00002689 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002690 Value *Address = getValue(Record, 1, NextValueNo, OpTy);
Craig Topper2617dcc2014-04-15 06:32:26 +00002691 if (!OpTy || !Address)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002692 return Error(InvalidRecord);
Chris Lattner3ed871f2009-10-27 19:13:16 +00002693 unsigned NumDests = Record.size()-2;
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00002694 IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests);
Chris Lattner3ed871f2009-10-27 19:13:16 +00002695 InstructionList.push_back(IBI);
2696 for (unsigned i = 0, e = NumDests; i != e; ++i) {
2697 if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) {
2698 IBI->addDestination(DestBB);
2699 } else {
2700 delete IBI;
Rafael Espindola48da4f42013-11-04 16:16:24 +00002701 return Error(InvalidRecord);
Chris Lattner3ed871f2009-10-27 19:13:16 +00002702 }
2703 }
2704 I = IBI;
2705 break;
2706 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002707
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00002708 case bitc::FUNC_CODE_INST_INVOKE: {
2709 // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
Rafael Espindola48da4f42013-11-04 16:16:24 +00002710 if (Record.size() < 4)
2711 return Error(InvalidRecord);
Bill Wendlinge94d8432012-12-07 23:16:57 +00002712 AttributeSet PAL = getAttributes(Record[0]);
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002713 unsigned CCInfo = Record[1];
2714 BasicBlock *NormalBB = getBasicBlock(Record[2]);
2715 BasicBlock *UnwindBB = getBasicBlock(Record[3]);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002716
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002717 unsigned OpNum = 4;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002718 Value *Callee;
2719 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002720 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002721
Chris Lattner229907c2011-07-18 04:54:35 +00002722 PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType());
Craig Topper2617dcc2014-04-15 06:32:26 +00002723 FunctionType *FTy = !CalleeTy ? nullptr :
Chris Lattner5285b5e2007-05-02 05:46:45 +00002724 dyn_cast<FunctionType>(CalleeTy->getElementType());
2725
2726 // Check that the right number of fixed parameters are here.
Craig Topper2617dcc2014-04-15 06:32:26 +00002727 if (!FTy || !NormalBB || !UnwindBB ||
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002728 Record.size() < OpNum+FTy->getNumParams())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002729 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002730
Chris Lattner5285b5e2007-05-02 05:46:45 +00002731 SmallVector<Value*, 16> Ops;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002732 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00002733 Ops.push_back(getValue(Record, OpNum, NextValueNo,
2734 FTy->getParamType(i)));
Craig Topper2617dcc2014-04-15 06:32:26 +00002735 if (!Ops.back())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002736 return Error(InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002737 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002738
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002739 if (!FTy->isVarArg()) {
2740 if (Record.size() != OpNum)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002741 return Error(InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002742 } else {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002743 // Read type/value pairs for varargs params.
2744 while (OpNum != Record.size()) {
2745 Value *Op;
2746 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002747 return Error(InvalidRecord);
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002748 Ops.push_back(Op);
2749 }
Chris Lattner5285b5e2007-05-02 05:46:45 +00002750 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002751
Jay Foad5bd375a2011-07-15 08:37:34 +00002752 I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops);
Devang Patelaf206b82009-09-18 19:26:43 +00002753 InstructionList.push_back(I);
Sandeep Patel68c5f472009-09-02 08:44:58 +00002754 cast<InvokeInst>(I)->setCallingConv(
2755 static_cast<CallingConv::ID>(CCInfo));
Devang Patel4c758ea2008-09-25 21:00:45 +00002756 cast<InvokeInst>(I)->setAttributes(PAL);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002757 break;
2758 }
Bill Wendlingf891bf82011-07-31 06:30:59 +00002759 case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval]
2760 unsigned Idx = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +00002761 Value *Val = nullptr;
Bill Wendlingf891bf82011-07-31 06:30:59 +00002762 if (getValueTypePair(Record, Idx, NextValueNo, Val))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002763 return Error(InvalidRecord);
Bill Wendlingf891bf82011-07-31 06:30:59 +00002764 I = ResumeInst::Create(Val);
Bill Wendlingb9a89992011-09-01 00:50:20 +00002765 InstructionList.push_back(I);
Bill Wendlingf891bf82011-07-31 06:30:59 +00002766 break;
2767 }
Chris Lattnere53603e2007-05-02 04:27:25 +00002768 case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE
Owen Anderson55f1c092009-08-13 21:58:54 +00002769 I = new UnreachableInst(Context);
Devang Patelaf206b82009-09-18 19:26:43 +00002770 InstructionList.push_back(I);
Chris Lattnere53603e2007-05-02 04:27:25 +00002771 break;
Chris Lattnere9759c22007-05-06 00:21:25 +00002772 case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...]
Chris Lattnere14cb882007-05-04 19:11:41 +00002773 if (Record.size() < 1 || ((Record.size()-1)&1))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002774 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00002775 Type *Ty = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00002776 if (!Ty)
2777 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002778
Jay Foad52131342011-03-30 11:28:46 +00002779 PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2);
Devang Patelaf206b82009-09-18 19:26:43 +00002780 InstructionList.push_back(PN);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002781
Chris Lattnere14cb882007-05-04 19:11:41 +00002782 for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00002783 Value *V;
2784 // With the new function encoding, it is possible that operands have
2785 // negative IDs (for forward references). Use a signed VBR
2786 // representation to keep the encoding small.
2787 if (UseRelativeIDs)
2788 V = getValueSigned(Record, 1+i, NextValueNo, Ty);
2789 else
2790 V = getValue(Record, 1+i, NextValueNo, Ty);
Chris Lattnere14cb882007-05-04 19:11:41 +00002791 BasicBlock *BB = getBasicBlock(Record[2+i]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00002792 if (!V || !BB)
2793 return Error(InvalidRecord);
Chris Lattnerc332bba2007-05-03 18:58:09 +00002794 PN->addIncoming(V, BB);
2795 }
2796 I = PN;
2797 break;
2798 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002799
Bill Wendlingfae14752011-08-12 20:24:12 +00002800 case bitc::FUNC_CODE_INST_LANDINGPAD: {
2801 // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?]
2802 unsigned Idx = 0;
2803 if (Record.size() < 4)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002804 return Error(InvalidRecord);
Bill Wendlingfae14752011-08-12 20:24:12 +00002805 Type *Ty = getTypeByID(Record[Idx++]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00002806 if (!Ty)
2807 return Error(InvalidRecord);
Craig Topper2617dcc2014-04-15 06:32:26 +00002808 Value *PersFn = nullptr;
Bill Wendlingfae14752011-08-12 20:24:12 +00002809 if (getValueTypePair(Record, Idx, NextValueNo, PersFn))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002810 return Error(InvalidRecord);
Bill Wendlingfae14752011-08-12 20:24:12 +00002811
2812 bool IsCleanup = !!Record[Idx++];
2813 unsigned NumClauses = Record[Idx++];
2814 LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, NumClauses);
2815 LP->setCleanup(IsCleanup);
2816 for (unsigned J = 0; J != NumClauses; ++J) {
2817 LandingPadInst::ClauseType CT =
2818 LandingPadInst::ClauseType(Record[Idx++]); (void)CT;
2819 Value *Val;
2820
2821 if (getValueTypePair(Record, Idx, NextValueNo, Val)) {
2822 delete LP;
Rafael Espindola48da4f42013-11-04 16:16:24 +00002823 return Error(InvalidRecord);
Bill Wendlingfae14752011-08-12 20:24:12 +00002824 }
2825
2826 assert((CT != LandingPadInst::Catch ||
2827 !isa<ArrayType>(Val->getType())) &&
2828 "Catch clause has a invalid type!");
2829 assert((CT != LandingPadInst::Filter ||
2830 isa<ArrayType>(Val->getType())) &&
2831 "Filter clause has invalid type!");
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +00002832 LP->addClause(cast<Constant>(Val));
Bill Wendlingfae14752011-08-12 20:24:12 +00002833 }
2834
2835 I = LP;
Bill Wendlingb9a89992011-09-01 00:50:20 +00002836 InstructionList.push_back(I);
Bill Wendlingfae14752011-08-12 20:24:12 +00002837 break;
2838 }
2839
Chris Lattnerf1c87102011-06-17 18:09:11 +00002840 case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]
2841 if (Record.size() != 4)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002842 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00002843 PointerType *Ty =
Chris Lattnerc332bba2007-05-03 18:58:09 +00002844 dyn_cast_or_null<PointerType>(getTypeByID(Record[0]));
Chris Lattner229907c2011-07-18 04:54:35 +00002845 Type *OpTy = getTypeByID(Record[1]);
Chris Lattnerf1c87102011-06-17 18:09:11 +00002846 Value *Size = getFnValueByID(Record[2], OpTy);
2847 unsigned Align = Record[3];
Rafael Espindola48da4f42013-11-04 16:16:24 +00002848 if (!Ty || !Size)
2849 return Error(InvalidRecord);
Owen Anderson4fdeba92009-07-15 23:53:25 +00002850 I = new AllocaInst(Ty->getElementType(), Size, (1 << Align) >> 1);
Devang Patelaf206b82009-09-18 19:26:43 +00002851 InstructionList.push_back(I);
Chris Lattnerc332bba2007-05-03 18:58:09 +00002852 break;
2853 }
Chris Lattner9f600c52007-05-03 22:04:19 +00002854 case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol]
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002855 unsigned OpNum = 0;
2856 Value *Op;
2857 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
2858 OpNum+2 != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002859 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002860
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002861 I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1);
Devang Patelaf206b82009-09-18 19:26:43 +00002862 InstructionList.push_back(I);
Chris Lattner83930552007-05-01 07:01:57 +00002863 break;
Chris Lattner9f600c52007-05-03 22:04:19 +00002864 }
Eli Friedman59b66882011-08-09 23:02:53 +00002865 case bitc::FUNC_CODE_INST_LOADATOMIC: {
2866 // LOADATOMIC: [opty, op, align, vol, ordering, synchscope]
2867 unsigned OpNum = 0;
2868 Value *Op;
2869 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
2870 OpNum+4 != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002871 return Error(InvalidRecord);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002872
Eli Friedman59b66882011-08-09 23:02:53 +00002873
2874 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
2875 if (Ordering == NotAtomic || Ordering == Release ||
2876 Ordering == AcquireRelease)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002877 return Error(InvalidRecord);
Eli Friedman59b66882011-08-09 23:02:53 +00002878 if (Ordering != NotAtomic && Record[OpNum] == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002879 return Error(InvalidRecord);
Eli Friedman59b66882011-08-09 23:02:53 +00002880 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
2881
2882 I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1,
2883 Ordering, SynchScope);
2884 InstructionList.push_back(I);
2885 break;
2886 }
Chris Lattnerc44070802011-06-17 18:17:37 +00002887 case bitc::FUNC_CODE_INST_STORE: { // STORE2:[ptrty, ptr, val, align, vol]
Christopher Lamb54dd24c2007-12-11 08:59:05 +00002888 unsigned OpNum = 0;
2889 Value *Val, *Ptr;
2890 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002891 popValue(Record, OpNum, NextValueNo,
Christopher Lamb54dd24c2007-12-11 08:59:05 +00002892 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
2893 OpNum+2 != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002894 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002895
Christopher Lamb54dd24c2007-12-11 08:59:05 +00002896 I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1);
Devang Patelaf206b82009-09-18 19:26:43 +00002897 InstructionList.push_back(I);
Christopher Lamb54dd24c2007-12-11 08:59:05 +00002898 break;
2899 }
Eli Friedman59b66882011-08-09 23:02:53 +00002900 case bitc::FUNC_CODE_INST_STOREATOMIC: {
2901 // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, synchscope]
2902 unsigned OpNum = 0;
2903 Value *Val, *Ptr;
2904 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002905 popValue(Record, OpNum, NextValueNo,
Eli Friedman59b66882011-08-09 23:02:53 +00002906 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
2907 OpNum+4 != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002908 return Error(InvalidRecord);
Eli Friedman59b66882011-08-09 23:02:53 +00002909
2910 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
Eli Friedman222b5a42011-09-19 19:41:28 +00002911 if (Ordering == NotAtomic || Ordering == Acquire ||
Eli Friedman59b66882011-08-09 23:02:53 +00002912 Ordering == AcquireRelease)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002913 return Error(InvalidRecord);
Eli Friedman59b66882011-08-09 23:02:53 +00002914 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
2915 if (Ordering != NotAtomic && Record[OpNum] == 0)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002916 return Error(InvalidRecord);
Eli Friedman59b66882011-08-09 23:02:53 +00002917
2918 I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1,
2919 Ordering, SynchScope);
2920 InstructionList.push_back(I);
2921 break;
2922 }
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002923 case bitc::FUNC_CODE_INST_CMPXCHG: {
Tim Northovere94a5182014-03-11 10:48:52 +00002924 // CMPXCHG:[ptrty, ptr, cmp, new, vol, successordering, synchscope,
Tim Northover420a2162014-06-13 14:24:07 +00002925 // failureordering?, isweak?]
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002926 unsigned OpNum = 0;
2927 Value *Ptr, *Cmp, *New;
2928 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002929 popValue(Record, OpNum, NextValueNo,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002930 cast<PointerType>(Ptr->getType())->getElementType(), Cmp) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002931 popValue(Record, OpNum, NextValueNo,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002932 cast<PointerType>(Ptr->getType())->getElementType(), New) ||
Tim Northover420a2162014-06-13 14:24:07 +00002933 (Record.size() < OpNum + 3 || Record.size() > OpNum + 5))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002934 return Error(InvalidRecord);
Tim Northovere94a5182014-03-11 10:48:52 +00002935 AtomicOrdering SuccessOrdering = GetDecodedOrdering(Record[OpNum+1]);
2936 if (SuccessOrdering == NotAtomic || SuccessOrdering == Unordered)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002937 return Error(InvalidRecord);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002938 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+2]);
Tim Northovere94a5182014-03-11 10:48:52 +00002939
2940 AtomicOrdering FailureOrdering;
2941 if (Record.size() < 7)
2942 FailureOrdering =
2943 AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering);
2944 else
2945 FailureOrdering = GetDecodedOrdering(Record[OpNum+3]);
2946
2947 I = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering, FailureOrdering,
2948 SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002949 cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]);
Tim Northover420a2162014-06-13 14:24:07 +00002950
2951 if (Record.size() < 8) {
2952 // Before weak cmpxchgs existed, the instruction simply returned the
2953 // value loaded from memory, so bitcode files from that era will be
2954 // expecting the first component of a modern cmpxchg.
2955 CurBB->getInstList().push_back(I);
2956 I = ExtractValueInst::Create(I, 0);
2957 } else {
2958 cast<AtomicCmpXchgInst>(I)->setWeak(Record[OpNum+4]);
2959 }
2960
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002961 InstructionList.push_back(I);
2962 break;
2963 }
2964 case bitc::FUNC_CODE_INST_ATOMICRMW: {
2965 // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, synchscope]
2966 unsigned OpNum = 0;
2967 Value *Ptr, *Val;
2968 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002969 popValue(Record, OpNum, NextValueNo,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002970 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
2971 OpNum+4 != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002972 return Error(InvalidRecord);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002973 AtomicRMWInst::BinOp Operation = GetDecodedRMWOperation(Record[OpNum]);
2974 if (Operation < AtomicRMWInst::FIRST_BINOP ||
2975 Operation > AtomicRMWInst::LAST_BINOP)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002976 return Error(InvalidRecord);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002977 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
Eli Friedman59b66882011-08-09 23:02:53 +00002978 if (Ordering == NotAtomic || Ordering == Unordered)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002979 return Error(InvalidRecord);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002980 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
2981 I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SynchScope);
2982 cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]);
2983 InstructionList.push_back(I);
2984 break;
2985 }
Eli Friedmanfee02c62011-07-25 23:16:38 +00002986 case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, synchscope]
2987 if (2 != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002988 return Error(InvalidRecord);
Eli Friedmanfee02c62011-07-25 23:16:38 +00002989 AtomicOrdering Ordering = GetDecodedOrdering(Record[0]);
2990 if (Ordering == NotAtomic || Ordering == Unordered ||
2991 Ordering == Monotonic)
Rafael Espindola48da4f42013-11-04 16:16:24 +00002992 return Error(InvalidRecord);
Eli Friedmanfee02c62011-07-25 23:16:38 +00002993 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[1]);
2994 I = new FenceInst(Context, Ordering, SynchScope);
2995 InstructionList.push_back(I);
2996 break;
2997 }
Chris Lattnerc44070802011-06-17 18:17:37 +00002998 case bitc::FUNC_CODE_INST_CALL: {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00002999 // CALL: [paramattrs, cc, fnty, fnid, arg0, arg1...]
3000 if (Record.size() < 3)
Rafael Espindola48da4f42013-11-04 16:16:24 +00003001 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003002
Bill Wendlinge94d8432012-12-07 23:16:57 +00003003 AttributeSet PAL = getAttributes(Record[0]);
Chris Lattner4c0a6d62007-05-08 05:38:01 +00003004 unsigned CCInfo = Record[1];
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003005
Chris Lattner4c0a6d62007-05-08 05:38:01 +00003006 unsigned OpNum = 2;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00003007 Value *Callee;
3008 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
Rafael Espindola48da4f42013-11-04 16:16:24 +00003009 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003010
Chris Lattner229907c2011-07-18 04:54:35 +00003011 PointerType *OpTy = dyn_cast<PointerType>(Callee->getType());
Craig Topper2617dcc2014-04-15 06:32:26 +00003012 FunctionType *FTy = nullptr;
Chris Lattner9f600c52007-05-03 22:04:19 +00003013 if (OpTy) FTy = dyn_cast<FunctionType>(OpTy->getElementType());
Chris Lattnerdf1233d2007-05-06 00:00:00 +00003014 if (!FTy || Record.size() < FTy->getNumParams()+OpNum)
Rafael Espindola48da4f42013-11-04 16:16:24 +00003015 return Error(InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003016
Chris Lattner9f600c52007-05-03 22:04:19 +00003017 SmallVector<Value*, 16> Args;
3018 // Read the fixed params.
Chris Lattnerdf1233d2007-05-06 00:00:00 +00003019 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003020 if (FTy->getParamType(i)->isLabelTy())
Dale Johannesen4646aa32007-11-05 21:20:28 +00003021 Args.push_back(getBasicBlock(Record[OpNum]));
Dan Gohmanbbcd04d2010-09-13 18:00:48 +00003022 else
Jan Wen Voungafaced02012-10-11 20:20:40 +00003023 Args.push_back(getValue(Record, OpNum, NextValueNo,
3024 FTy->getParamType(i)));
Craig Topper2617dcc2014-04-15 06:32:26 +00003025 if (!Args.back())
Rafael Espindola48da4f42013-11-04 16:16:24 +00003026 return Error(InvalidRecord);
Chris Lattner9f600c52007-05-03 22:04:19 +00003027 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003028
Chris Lattner9f600c52007-05-03 22:04:19 +00003029 // Read type/value pairs for varargs params.
Chris Lattner9f600c52007-05-03 22:04:19 +00003030 if (!FTy->isVarArg()) {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00003031 if (OpNum != Record.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00003032 return Error(InvalidRecord);
Chris Lattner9f600c52007-05-03 22:04:19 +00003033 } else {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00003034 while (OpNum != Record.size()) {
3035 Value *Op;
3036 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindola48da4f42013-11-04 16:16:24 +00003037 return Error(InvalidRecord);
Chris Lattnerdf1233d2007-05-06 00:00:00 +00003038 Args.push_back(Op);
Chris Lattner9f600c52007-05-03 22:04:19 +00003039 }
3040 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003041
Jay Foad5bd375a2011-07-15 08:37:34 +00003042 I = CallInst::Create(Callee, Args);
Devang Patelaf206b82009-09-18 19:26:43 +00003043 InstructionList.push_back(I);
Sandeep Patel68c5f472009-09-02 08:44:58 +00003044 cast<CallInst>(I)->setCallingConv(
Reid Kleckner5772b772014-04-24 20:14:34 +00003045 static_cast<CallingConv::ID>((~(1U << 14) & CCInfo) >> 1));
3046 CallInst::TailCallKind TCK = CallInst::TCK_None;
3047 if (CCInfo & 1)
3048 TCK = CallInst::TCK_Tail;
3049 if (CCInfo & (1 << 14))
3050 TCK = CallInst::TCK_MustTail;
3051 cast<CallInst>(I)->setTailCallKind(TCK);
Devang Patel4c758ea2008-09-25 21:00:45 +00003052 cast<CallInst>(I)->setAttributes(PAL);
Chris Lattner9f600c52007-05-03 22:04:19 +00003053 break;
3054 }
3055 case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty]
3056 if (Record.size() < 3)
Rafael Espindola48da4f42013-11-04 16:16:24 +00003057 return Error(InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00003058 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00003059 Value *Op = getValue(Record, 1, NextValueNo, OpTy);
Chris Lattner229907c2011-07-18 04:54:35 +00003060 Type *ResTy = getTypeByID(Record[2]);
Chris Lattner9f600c52007-05-03 22:04:19 +00003061 if (!OpTy || !Op || !ResTy)
Rafael Espindola48da4f42013-11-04 16:16:24 +00003062 return Error(InvalidRecord);
Chris Lattner9f600c52007-05-03 22:04:19 +00003063 I = new VAArgInst(Op, ResTy);
Devang Patelaf206b82009-09-18 19:26:43 +00003064 InstructionList.push_back(I);
Chris Lattner9f600c52007-05-03 22:04:19 +00003065 break;
3066 }
Chris Lattner83930552007-05-01 07:01:57 +00003067 }
3068
3069 // Add instruction to end of current BB. If there is no current BB, reject
3070 // this file.
Craig Topper2617dcc2014-04-15 06:32:26 +00003071 if (!CurBB) {
Chris Lattner83930552007-05-01 07:01:57 +00003072 delete I;
Rafael Espindola48da4f42013-11-04 16:16:24 +00003073 return Error(InvalidInstructionWithNoBB);
Chris Lattner83930552007-05-01 07:01:57 +00003074 }
3075 CurBB->getInstList().push_back(I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003076
Chris Lattner83930552007-05-01 07:01:57 +00003077 // If this was a terminator instruction, move to the next block.
3078 if (isa<TerminatorInst>(I)) {
3079 ++CurBBNo;
Craig Topper2617dcc2014-04-15 06:32:26 +00003080 CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr;
Chris Lattner83930552007-05-01 07:01:57 +00003081 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003082
Chris Lattner83930552007-05-01 07:01:57 +00003083 // Non-void values get registered in the value table for future use.
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00003084 if (I && !I->getType()->isVoidTy())
Chris Lattner83930552007-05-01 07:01:57 +00003085 ValueList.AssignValue(I, NextValueNo++);
Chris Lattner85b7b402007-05-01 05:52:21 +00003086 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003087
Chris Lattner27d38752013-01-20 02:13:19 +00003088OutOfRecordLoop:
Joe Abbey97b7a172013-02-06 22:14:06 +00003089
Chris Lattner83930552007-05-01 07:01:57 +00003090 // Check the function list for unresolved values.
3091 if (Argument *A = dyn_cast<Argument>(ValueList.back())) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003092 if (!A->getParent()) {
Chris Lattner83930552007-05-01 07:01:57 +00003093 // We found at least one unresolved value. Nuke them all to avoid leaks.
3094 for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){
Craig Topper2617dcc2014-04-15 06:32:26 +00003095 if ((A = dyn_cast_or_null<Argument>(ValueList[i])) && !A->getParent()) {
Owen Andersonb292b8c2009-07-30 23:03:37 +00003096 A->replaceAllUsesWith(UndefValue::get(A->getType()));
Chris Lattner83930552007-05-01 07:01:57 +00003097 delete A;
3098 }
3099 }
Rafael Espindola48da4f42013-11-04 16:16:24 +00003100 return Error(NeverResolvedValueFoundInFunction);
Chris Lattner83930552007-05-01 07:01:57 +00003101 }
Chris Lattner83930552007-05-01 07:01:57 +00003102 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003103
Dan Gohman9b9ff462010-08-25 20:23:38 +00003104 // FIXME: Check for unresolved forward-declared metadata references
3105 // and clean up leaks.
3106
Chris Lattner5956dc82009-10-28 05:53:48 +00003107 // See if anything took the address of blocks in this function. If so,
3108 // resolve them now.
Chris Lattner5956dc82009-10-28 05:53:48 +00003109 DenseMap<Function*, std::vector<BlockAddrRefTy> >::iterator BAFRI =
3110 BlockAddrFwdRefs.find(F);
3111 if (BAFRI != BlockAddrFwdRefs.end()) {
3112 std::vector<BlockAddrRefTy> &RefList = BAFRI->second;
3113 for (unsigned i = 0, e = RefList.size(); i != e; ++i) {
3114 unsigned BlockIdx = RefList[i].first;
Chris Lattneraa99c942009-11-01 01:27:45 +00003115 if (BlockIdx >= FunctionBBs.size())
Rafael Espindola48da4f42013-11-04 16:16:24 +00003116 return Error(InvalidID);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003117
Chris Lattner5956dc82009-10-28 05:53:48 +00003118 GlobalVariable *FwdRef = RefList[i].second;
Chris Lattneraa99c942009-11-01 01:27:45 +00003119 FwdRef->replaceAllUsesWith(BlockAddress::get(F, FunctionBBs[BlockIdx]));
Chris Lattner5956dc82009-10-28 05:53:48 +00003120 FwdRef->eraseFromParent();
3121 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003122
Chris Lattner5956dc82009-10-28 05:53:48 +00003123 BlockAddrFwdRefs.erase(BAFRI);
3124 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003125
Chris Lattner85b7b402007-05-01 05:52:21 +00003126 // Trim the value list down to the size it was before we parsed this function.
3127 ValueList.shrinkTo(ModuleValueListSize);
Dan Gohman26d837d2010-08-25 20:22:53 +00003128 MDValueList.shrinkTo(ModuleMDValueListSize);
Chris Lattner85b7b402007-05-01 05:52:21 +00003129 std::vector<BasicBlock*>().swap(FunctionBBs);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003130 return std::error_code();
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003131}
3132
Rafael Espindola7d712032013-11-05 17:16:08 +00003133/// Find the function body in the bitcode stream
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003134std::error_code BitcodeReader::FindFunctionInStream(
3135 Function *F,
3136 DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003137 while (DeferredFunctionInfoIterator->second == 0) {
3138 if (Stream.AtEndOfStream())
Rafael Espindola48da4f42013-11-04 16:16:24 +00003139 return Error(CouldNotFindFunctionInStream);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003140 // ParseModule will parse the next body in the stream and set its
3141 // position in the DeferredFunctionInfo map.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003142 if (std::error_code EC = ParseModule(true))
Rafael Espindola7d712032013-11-05 17:16:08 +00003143 return EC;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003144 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003145 return std::error_code();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003146}
3147
Chris Lattner9eeada92007-05-18 04:02:46 +00003148//===----------------------------------------------------------------------===//
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003149// GVMaterializer implementation
Chris Lattner9eeada92007-05-18 04:02:46 +00003150//===----------------------------------------------------------------------===//
3151
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +00003152void BitcodeReader::releaseBuffer() { Buffer.release(); }
Chris Lattner9eeada92007-05-18 04:02:46 +00003153
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003154bool BitcodeReader::isMaterializable(const GlobalValue *GV) const {
3155 if (const Function *F = dyn_cast<Function>(GV)) {
3156 return F->isDeclaration() &&
3157 DeferredFunctionInfo.count(const_cast<Function*>(F));
3158 }
3159 return false;
3160}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003161
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003162std::error_code BitcodeReader::Materialize(GlobalValue *GV) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003163 Function *F = dyn_cast<Function>(GV);
3164 // If it's not a function or is already material, ignore the request.
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003165 if (!F || !F->isMaterializable())
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003166 return std::error_code();
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003167
3168 DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F);
Chris Lattner9eeada92007-05-18 04:02:46 +00003169 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003170 // If its position is recorded as 0, its body is somewhere in the stream
3171 // but we haven't seen it yet.
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003172 if (DFII->second == 0 && LazyStreamer)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003173 if (std::error_code EC = FindFunctionInStream(F, DFII))
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003174 return EC;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003175
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003176 // Move the bit stream to the saved position of the deferred function body.
3177 Stream.JumpToBit(DFII->second);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003178
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003179 if (std::error_code EC = ParseFunctionBody(F))
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003180 return EC;
Chandler Carruth7132e002007-08-04 01:51:18 +00003181
3182 // Upgrade any old intrinsic calls in the function.
3183 for (UpgradedIntrinsicMap::iterator I = UpgradedIntrinsics.begin(),
3184 E = UpgradedIntrinsics.end(); I != E; ++I) {
3185 if (I->first != I->second) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00003186 for (auto UI = I->first->user_begin(), UE = I->first->user_end();
3187 UI != UE;) {
Chandler Carruth7132e002007-08-04 01:51:18 +00003188 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
3189 UpgradeIntrinsicCall(CI, I->second);
3190 }
3191 }
3192 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003193
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003194 return std::error_code();
Chris Lattner9eeada92007-05-18 04:02:46 +00003195}
3196
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003197bool BitcodeReader::isDematerializable(const GlobalValue *GV) const {
3198 const Function *F = dyn_cast<Function>(GV);
3199 if (!F || F->isDeclaration())
3200 return false;
3201 return DeferredFunctionInfo.count(const_cast<Function*>(F));
3202}
3203
3204void BitcodeReader::Dematerialize(GlobalValue *GV) {
3205 Function *F = dyn_cast<Function>(GV);
3206 // If this function isn't dematerializable, this is a noop.
3207 if (!F || !isDematerializable(F))
Chris Lattner9eeada92007-05-18 04:02:46 +00003208 return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003209
Chris Lattner9eeada92007-05-18 04:02:46 +00003210 assert(DeferredFunctionInfo.count(F) && "No info to read function later?");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003211
Chris Lattner9eeada92007-05-18 04:02:46 +00003212 // Just forget the function body, we can remat it later.
3213 F->deleteBody();
Chris Lattner9eeada92007-05-18 04:02:46 +00003214}
3215
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003216std::error_code BitcodeReader::MaterializeModule(Module *M) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003217 assert(M == TheModule &&
3218 "Can only Materialize the Module this BitcodeReader is attached to.");
Chris Lattner06310bf2009-06-16 05:15:21 +00003219 // Iterate over the module, deserializing any functions that are still on
3220 // disk.
3221 for (Module::iterator F = TheModule->begin(), E = TheModule->end();
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003222 F != E; ++F) {
3223 if (F->isMaterializable()) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003224 if (std::error_code EC = Materialize(F))
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003225 return EC;
3226 }
3227 }
Derek Schuff92ef9752012-02-29 00:07:09 +00003228 // At this point, if there are any function bodies, the current bit is
3229 // pointing to the END_BLOCK record after them. Now make sure the rest
3230 // of the bits in the module have been read.
3231 if (NextUnreadBit)
3232 ParseModule(true);
3233
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003234 // Upgrade any intrinsic calls that slipped through (should not happen!) and
3235 // delete the old functions to clean up. We can't do this unless the entire
3236 // module is materialized because there could always be another function body
Chandler Carruth7132e002007-08-04 01:51:18 +00003237 // with calls to the old function.
3238 for (std::vector<std::pair<Function*, Function*> >::iterator I =
3239 UpgradedIntrinsics.begin(), E = UpgradedIntrinsics.end(); I != E; ++I) {
3240 if (I->first != I->second) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00003241 for (auto UI = I->first->user_begin(), UE = I->first->user_end();
3242 UI != UE;) {
Chandler Carruth7132e002007-08-04 01:51:18 +00003243 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
3244 UpgradeIntrinsicCall(CI, I->second);
3245 }
Chris Lattner647cffb2009-04-01 01:43:03 +00003246 if (!I->first->use_empty())
3247 I->first->replaceAllUsesWith(I->second);
Chandler Carruth7132e002007-08-04 01:51:18 +00003248 I->first->eraseFromParent();
3249 }
3250 }
3251 std::vector<std::pair<Function*, Function*> >().swap(UpgradedIntrinsics);
Devang Patel80ae3492009-08-28 23:24:31 +00003252
Manman Ren209b17c2013-09-28 00:22:27 +00003253 for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++)
3254 UpgradeInstWithTBAATag(InstsWithTBAATag[I]);
3255
Manman Ren8b4306c2013-12-02 21:29:56 +00003256 UpgradeDebugInfo(*M);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003257 return std::error_code();
Chris Lattner9eeada92007-05-18 04:02:46 +00003258}
3259
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003260std::error_code BitcodeReader::InitStream() {
Rafael Espindola48da4f42013-11-04 16:16:24 +00003261 if (LazyStreamer)
3262 return InitLazyStream();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003263 return InitStreamFromBuffer();
3264}
3265
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003266std::error_code BitcodeReader::InitStreamFromBuffer() {
Roman Divacky4717a8d2012-09-06 15:42:13 +00003267 const unsigned char *BufPtr = (const unsigned char*)Buffer->getBufferStart();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003268 const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
3269
3270 if (Buffer->getBufferSize() & 3) {
3271 if (!isRawBitcode(BufPtr, BufEnd) && !isBitcodeWrapper(BufPtr, BufEnd))
Rafael Espindola48da4f42013-11-04 16:16:24 +00003272 return Error(InvalidBitcodeSignature);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003273 else
Rafael Espindola48da4f42013-11-04 16:16:24 +00003274 return Error(BitcodeStreamInvalidSize);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003275 }
3276
3277 // If we have a wrapper header, parse it and ignore the non-bc file contents.
3278 // The magic number is 0x0B17C0DE stored in little endian.
3279 if (isBitcodeWrapper(BufPtr, BufEnd))
3280 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
Rafael Espindola48da4f42013-11-04 16:16:24 +00003281 return Error(InvalidBitcodeWrapperHeader);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003282
3283 StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
3284 Stream.init(*StreamFile);
3285
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003286 return std::error_code();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003287}
3288
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003289std::error_code BitcodeReader::InitLazyStream() {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003290 // Check and strip off the bitcode wrapper; BitstreamReader expects never to
3291 // see it.
3292 StreamingMemoryObject *Bytes = new StreamingMemoryObject(LazyStreamer);
3293 StreamFile.reset(new BitstreamReader(Bytes));
3294 Stream.init(*StreamFile);
3295
3296 unsigned char buf[16];
Benjamin Kramer534d3a42013-05-24 10:54:58 +00003297 if (Bytes->readBytes(0, 16, buf) == -1)
Rafael Espindola48da4f42013-11-04 16:16:24 +00003298 return Error(BitcodeStreamInvalidSize);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003299
3300 if (!isBitcode(buf, buf + 16))
Rafael Espindola48da4f42013-11-04 16:16:24 +00003301 return Error(InvalidBitcodeSignature);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003302
3303 if (isBitcodeWrapper(buf, buf + 4)) {
3304 const unsigned char *bitcodeStart = buf;
3305 const unsigned char *bitcodeEnd = buf + 16;
3306 SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
3307 Bytes->dropLeadingBytes(bitcodeStart - buf);
3308 Bytes->setKnownObjectSize(bitcodeEnd - bitcodeStart);
3309 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003310 return std::error_code();
Rafael Espindola48da4f42013-11-04 16:16:24 +00003311}
3312
3313namespace {
Rafael Espindola25188c92014-06-12 01:45:43 +00003314class BitcodeErrorCategoryType : public std::error_category {
Rafael Espindolaf5d07fa2014-06-10 21:26:47 +00003315 const char *name() const LLVM_NOEXCEPT override {
Rafael Espindola48da4f42013-11-04 16:16:24 +00003316 return "llvm.bitcode";
3317 }
Craig Topper73156022014-03-02 09:09:27 +00003318 std::string message(int IE) const override {
Rafael Espindola48da4f42013-11-04 16:16:24 +00003319 BitcodeReader::ErrorType E = static_cast<BitcodeReader::ErrorType>(IE);
3320 switch (E) {
3321 case BitcodeReader::BitcodeStreamInvalidSize:
3322 return "Bitcode stream length should be >= 16 bytes and a multiple of 4";
3323 case BitcodeReader::ConflictingMETADATA_KINDRecords:
3324 return "Conflicting METADATA_KIND records";
3325 case BitcodeReader::CouldNotFindFunctionInStream:
3326 return "Could not find function in stream";
3327 case BitcodeReader::ExpectedConstant:
3328 return "Expected a constant";
3329 case BitcodeReader::InsufficientFunctionProtos:
3330 return "Insufficient function protos";
3331 case BitcodeReader::InvalidBitcodeSignature:
3332 return "Invalid bitcode signature";
3333 case BitcodeReader::InvalidBitcodeWrapperHeader:
3334 return "Invalid bitcode wrapper header";
3335 case BitcodeReader::InvalidConstantReference:
3336 return "Invalid ronstant reference";
3337 case BitcodeReader::InvalidID:
3338 return "Invalid ID";
3339 case BitcodeReader::InvalidInstructionWithNoBB:
3340 return "Invalid instruction with no BB";
3341 case BitcodeReader::InvalidRecord:
3342 return "Invalid record";
3343 case BitcodeReader::InvalidTypeForValue:
3344 return "Invalid type for value";
3345 case BitcodeReader::InvalidTYPETable:
3346 return "Invalid TYPE table";
3347 case BitcodeReader::InvalidType:
3348 return "Invalid type";
3349 case BitcodeReader::MalformedBlock:
3350 return "Malformed block";
3351 case BitcodeReader::MalformedGlobalInitializerSet:
3352 return "Malformed global initializer set";
3353 case BitcodeReader::InvalidMultipleBlocks:
3354 return "Invalid multiple blocks";
3355 case BitcodeReader::NeverResolvedValueFoundInFunction:
3356 return "Never resolved value found in function";
3357 case BitcodeReader::InvalidValue:
3358 return "Invalid value";
3359 }
Benjamin Kramer77db1632013-11-05 13:45:09 +00003360 llvm_unreachable("Unknown error type!");
Rafael Espindola48da4f42013-11-04 16:16:24 +00003361 }
3362};
3363}
3364
Rafael Espindola25188c92014-06-12 01:45:43 +00003365const std::error_category &BitcodeReader::BitcodeErrorCategory() {
Rafael Espindola48da4f42013-11-04 16:16:24 +00003366 static BitcodeErrorCategoryType O;
3367 return O;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003368}
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003369
Chris Lattner6694f602007-04-29 07:54:31 +00003370//===----------------------------------------------------------------------===//
3371// External interface
3372//===----------------------------------------------------------------------===//
3373
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003374/// getLazyBitcodeModule - lazy function-at-a-time loading from a file.
Chris Lattner6694f602007-04-29 07:54:31 +00003375///
Rafael Espindola5b6c1e82014-01-13 18:31:04 +00003376ErrorOr<Module *> llvm::getLazyBitcodeModule(MemoryBuffer *Buffer,
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +00003377 LLVMContext &Context) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003378 Module *M = new Module(Buffer->getBufferIdentifier(), Context);
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +00003379 BitcodeReader *R = new BitcodeReader(Buffer, Context);
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003380 M->setMaterializer(R);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003381 if (std::error_code EC = R->ParseBitcodeInto(M)) {
Rafael Espindola8fb31112014-06-18 20:07:35 +00003382 R->releaseBuffer(); // Never take ownership on error.
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003383 delete M; // Also deletes R.
Rafael Espindola5b6c1e82014-01-13 18:31:04 +00003384 return EC;
Chris Lattner6694f602007-04-29 07:54:31 +00003385 }
Rafael Espindolab7993462012-01-02 07:49:53 +00003386
3387 R->materializeForwardReferencedFunctions();
3388
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003389 return M;
Chris Lattner6694f602007-04-29 07:54:31 +00003390}
3391
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003392
3393Module *llvm::getStreamedBitcodeModule(const std::string &name,
3394 DataStreamer *streamer,
3395 LLVMContext &Context,
3396 std::string *ErrMsg) {
3397 Module *M = new Module(name, Context);
3398 BitcodeReader *R = new BitcodeReader(streamer, Context);
3399 M->setMaterializer(R);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003400 if (std::error_code EC = R->ParseBitcodeInto(M)) {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003401 if (ErrMsg)
Rafael Espindola48da4f42013-11-04 16:16:24 +00003402 *ErrMsg = EC.message();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003403 delete M; // Also deletes R.
Craig Topper2617dcc2014-04-15 06:32:26 +00003404 return nullptr;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003405 }
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003406 return M;
3407}
3408
Rafael Espindola8f31e212014-01-15 01:08:23 +00003409ErrorOr<Module *> llvm::parseBitcodeFile(MemoryBuffer *Buffer,
3410 LLVMContext &Context) {
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +00003411 ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
Rafael Espindola8f31e212014-01-15 01:08:23 +00003412 if (!ModuleOrErr)
3413 return ModuleOrErr;
Rafael Espindola5b6c1e82014-01-13 18:31:04 +00003414 Module *M = ModuleOrErr.get();
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003415 // Read in the entire module, and destroy the BitcodeReader.
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +00003416 if (std::error_code EC = M->materializeAllPermanently(true)) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003417 delete M;
Rafael Espindola8f31e212014-01-15 01:08:23 +00003418 return EC;
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003419 }
Bill Wendling0198ce02010-10-06 01:22:42 +00003420
Chad Rosierca2567b2011-12-07 21:44:12 +00003421 // TODO: Restore the use-lists to the in-memory state when the bitcode was
3422 // written. We must defer until the Module has been fully materialized.
3423
Chris Lattner6694f602007-04-29 07:54:31 +00003424 return M;
3425}
Bill Wendling0198ce02010-10-06 01:22:42 +00003426
3427std::string llvm::getBitcodeTargetTriple(MemoryBuffer *Buffer,
3428 LLVMContext& Context,
3429 std::string *ErrMsg) {
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +00003430 BitcodeReader *R = new BitcodeReader(Buffer, Context);
Bill Wendling0198ce02010-10-06 01:22:42 +00003431
3432 std::string Triple("");
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003433 if (std::error_code EC = R->ParseTriple(Triple))
Bill Wendling0198ce02010-10-06 01:22:42 +00003434 if (ErrMsg)
Rafael Espindola48da4f42013-11-04 16:16:24 +00003435 *ErrMsg = EC.message();
Bill Wendling0198ce02010-10-06 01:22:42 +00003436
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +00003437 R->releaseBuffer();
Bill Wendling0198ce02010-10-06 01:22:42 +00003438 delete R;
3439 return Triple;
3440}