blob: 66426c83c6693e68aa2307ce332e740933367da7 [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
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +000034std::error_code BitcodeReader::materializeForwardReferencedFunctions() {
35 if (WillMaterializeAllForwardRefs)
36 return std::error_code();
37
38 // Prevent recursion.
39 WillMaterializeAllForwardRefs = true;
40
Duncan P. N. Exon Smith5a511b52014-08-05 17:49:48 +000041 while (!BasicBlockFwdRefQueue.empty()) {
42 Function *F = BasicBlockFwdRefQueue.front();
43 BasicBlockFwdRefQueue.pop_front();
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +000044 assert(F && "Expected valid function");
Duncan P. N. Exon Smith5a511b52014-08-05 17:49:48 +000045 if (!BasicBlockFwdRefs.count(F))
46 // Already materialized.
47 continue;
48
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +000049 // Check for a function that isn't materializable to prevent an infinite
50 // loop. When parsing a blockaddress stored in a global variable, there
51 // isn't a trivial way to check if a function will have a body without a
52 // linear search through FunctionsWithBodies, so just check it here.
53 if (!F->isMaterializable())
54 return Error(BitcodeError::NeverResolvedFunctionFromBlockAddress);
55
56 // Try to materialize F.
57 if (std::error_code EC = Materialize(F))
58 return EC;
Rafael Espindolab7993462012-01-02 07:49:53 +000059 }
Duncan P. N. Exon Smith5a511b52014-08-05 17:49:48 +000060 assert(BasicBlockFwdRefs.empty() && "Function missing from queue");
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +000061
62 // Reset state.
63 WillMaterializeAllForwardRefs = false;
64 return std::error_code();
Rafael Espindolab7993462012-01-02 07:49:53 +000065}
66
Chris Lattner9eeada92007-05-18 04:02:46 +000067void BitcodeReader::FreeState() {
Craig Topper2617dcc2014-04-15 06:32:26 +000068 Buffer = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000069 std::vector<Type*>().swap(TypeList);
Chris Lattner9eeada92007-05-18 04:02:46 +000070 ValueList.clear();
Devang Patel05eb6172009-08-04 06:00:18 +000071 MDValueList.clear();
David Majnemerdad0a642014-06-27 18:19:56 +000072 std::vector<Comdat *>().swap(ComdatList);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000073
Bill Wendlinge94d8432012-12-07 23:16:57 +000074 std::vector<AttributeSet>().swap(MAttributes);
Chris Lattner9eeada92007-05-18 04:02:46 +000075 std::vector<BasicBlock*>().swap(FunctionBBs);
76 std::vector<Function*>().swap(FunctionsWithBodies);
77 DeferredFunctionInfo.clear();
Dan Gohman43aa8f02010-07-20 21:42:28 +000078 MDKindMap.clear();
Benjamin Kramer736a4fc2012-09-21 14:34:31 +000079
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +000080 assert(BasicBlockFwdRefs.empty() && "Unresolved blockaddress fwd references");
Duncan P. N. Exon Smith5a511b52014-08-05 17:49:48 +000081 BasicBlockFwdRefQueue.clear();
Chris Lattner6694f602007-04-29 07:54:31 +000082}
83
Chris Lattnerfee5a372007-05-04 03:30:17 +000084//===----------------------------------------------------------------------===//
85// Helper functions to implement forward reference resolution, etc.
86//===----------------------------------------------------------------------===//
Chris Lattner6694f602007-04-29 07:54:31 +000087
Chris Lattner1314b992007-04-22 06:23:29 +000088/// ConvertToString - Convert a string from a record into an std::string, return
89/// true on failure.
Chris Lattnerccaa4482007-04-23 21:26:05 +000090template<typename StrTy>
Benjamin Kramer9704ed02012-05-28 14:10:31 +000091static bool ConvertToString(ArrayRef<uint64_t> Record, unsigned Idx,
Chris Lattnerccaa4482007-04-23 21:26:05 +000092 StrTy &Result) {
Chris Lattnere14cb882007-05-04 19:11:41 +000093 if (Idx > Record.size())
Chris Lattner1314b992007-04-22 06:23:29 +000094 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000095
Chris Lattnere14cb882007-05-04 19:11:41 +000096 for (unsigned i = Idx, e = Record.size(); i != e; ++i)
97 Result += (char)Record[i];
Chris Lattner1314b992007-04-22 06:23:29 +000098 return false;
99}
100
101static GlobalValue::LinkageTypes GetDecodedLinkage(unsigned Val) {
102 switch (Val) {
103 default: // Map unknown/new linkages to external
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000104 case 0: return GlobalValue::ExternalLinkage;
105 case 1: return GlobalValue::WeakAnyLinkage;
106 case 2: return GlobalValue::AppendingLinkage;
107 case 3: return GlobalValue::InternalLinkage;
108 case 4: return GlobalValue::LinkOnceAnyLinkage;
Nico Rieck7157bb72014-01-14 15:22:47 +0000109 case 5: return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage
110 case 6: return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000111 case 7: return GlobalValue::ExternalWeakLinkage;
112 case 8: return GlobalValue::CommonLinkage;
113 case 9: return GlobalValue::PrivateLinkage;
Duncan Sands12da8ce2009-03-07 15:45:40 +0000114 case 10: return GlobalValue::WeakODRLinkage;
115 case 11: return GlobalValue::LinkOnceODRLinkage;
Chris Lattner184f1be2009-04-13 05:44:34 +0000116 case 12: return GlobalValue::AvailableExternallyLinkage;
Rafael Espindola2fb5bc32014-03-13 23:18:37 +0000117 case 13:
118 return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage
119 case 14:
120 return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage
Chris Lattner1314b992007-04-22 06:23:29 +0000121 }
122}
123
124static GlobalValue::VisibilityTypes GetDecodedVisibility(unsigned Val) {
125 switch (Val) {
126 default: // Map unknown visibilities to default.
127 case 0: return GlobalValue::DefaultVisibility;
128 case 1: return GlobalValue::HiddenVisibility;
Anton Korobeynikov31fc4f92007-04-29 20:56:48 +0000129 case 2: return GlobalValue::ProtectedVisibility;
Chris Lattner1314b992007-04-22 06:23:29 +0000130 }
131}
132
Nico Rieck7157bb72014-01-14 15:22:47 +0000133static GlobalValue::DLLStorageClassTypes
134GetDecodedDLLStorageClass(unsigned Val) {
135 switch (Val) {
136 default: // Map unknown values to default.
137 case 0: return GlobalValue::DefaultStorageClass;
138 case 1: return GlobalValue::DLLImportStorageClass;
139 case 2: return GlobalValue::DLLExportStorageClass;
140 }
141}
142
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000143static GlobalVariable::ThreadLocalMode GetDecodedThreadLocalMode(unsigned Val) {
144 switch (Val) {
145 case 0: return GlobalVariable::NotThreadLocal;
146 default: // Map unknown non-zero value to general dynamic.
147 case 1: return GlobalVariable::GeneralDynamicTLSModel;
148 case 2: return GlobalVariable::LocalDynamicTLSModel;
149 case 3: return GlobalVariable::InitialExecTLSModel;
150 case 4: return GlobalVariable::LocalExecTLSModel;
151 }
152}
153
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000154static int GetDecodedCastOpcode(unsigned Val) {
155 switch (Val) {
156 default: return -1;
157 case bitc::CAST_TRUNC : return Instruction::Trunc;
158 case bitc::CAST_ZEXT : return Instruction::ZExt;
159 case bitc::CAST_SEXT : return Instruction::SExt;
160 case bitc::CAST_FPTOUI : return Instruction::FPToUI;
161 case bitc::CAST_FPTOSI : return Instruction::FPToSI;
162 case bitc::CAST_UITOFP : return Instruction::UIToFP;
163 case bitc::CAST_SITOFP : return Instruction::SIToFP;
164 case bitc::CAST_FPTRUNC : return Instruction::FPTrunc;
165 case bitc::CAST_FPEXT : return Instruction::FPExt;
166 case bitc::CAST_PTRTOINT: return Instruction::PtrToInt;
167 case bitc::CAST_INTTOPTR: return Instruction::IntToPtr;
168 case bitc::CAST_BITCAST : return Instruction::BitCast;
Matt Arsenault3aa9b032013-11-18 02:51:33 +0000169 case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000170 }
171}
Chris Lattner229907c2011-07-18 04:54:35 +0000172static int GetDecodedBinaryOpcode(unsigned Val, Type *Ty) {
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000173 switch (Val) {
174 default: return -1;
Dan Gohmana5b96452009-06-04 22:49:04 +0000175 case bitc::BINOP_ADD:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000176 return Ty->isFPOrFPVectorTy() ? Instruction::FAdd : Instruction::Add;
Dan Gohmana5b96452009-06-04 22:49:04 +0000177 case bitc::BINOP_SUB:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000178 return Ty->isFPOrFPVectorTy() ? Instruction::FSub : Instruction::Sub;
Dan Gohmana5b96452009-06-04 22:49:04 +0000179 case bitc::BINOP_MUL:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000180 return Ty->isFPOrFPVectorTy() ? Instruction::FMul : Instruction::Mul;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000181 case bitc::BINOP_UDIV: return Instruction::UDiv;
182 case bitc::BINOP_SDIV:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000183 return Ty->isFPOrFPVectorTy() ? Instruction::FDiv : Instruction::SDiv;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000184 case bitc::BINOP_UREM: return Instruction::URem;
185 case bitc::BINOP_SREM:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000186 return Ty->isFPOrFPVectorTy() ? Instruction::FRem : Instruction::SRem;
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000187 case bitc::BINOP_SHL: return Instruction::Shl;
188 case bitc::BINOP_LSHR: return Instruction::LShr;
189 case bitc::BINOP_ASHR: return Instruction::AShr;
190 case bitc::BINOP_AND: return Instruction::And;
191 case bitc::BINOP_OR: return Instruction::Or;
192 case bitc::BINOP_XOR: return Instruction::Xor;
193 }
194}
195
Eli Friedmanc9a551e2011-07-28 21:48:00 +0000196static AtomicRMWInst::BinOp GetDecodedRMWOperation(unsigned Val) {
197 switch (Val) {
198 default: return AtomicRMWInst::BAD_BINOP;
199 case bitc::RMW_XCHG: return AtomicRMWInst::Xchg;
200 case bitc::RMW_ADD: return AtomicRMWInst::Add;
201 case bitc::RMW_SUB: return AtomicRMWInst::Sub;
202 case bitc::RMW_AND: return AtomicRMWInst::And;
203 case bitc::RMW_NAND: return AtomicRMWInst::Nand;
204 case bitc::RMW_OR: return AtomicRMWInst::Or;
205 case bitc::RMW_XOR: return AtomicRMWInst::Xor;
206 case bitc::RMW_MAX: return AtomicRMWInst::Max;
207 case bitc::RMW_MIN: return AtomicRMWInst::Min;
208 case bitc::RMW_UMAX: return AtomicRMWInst::UMax;
209 case bitc::RMW_UMIN: return AtomicRMWInst::UMin;
210 }
211}
212
Eli Friedmanfee02c62011-07-25 23:16:38 +0000213static AtomicOrdering GetDecodedOrdering(unsigned Val) {
214 switch (Val) {
215 case bitc::ORDERING_NOTATOMIC: return NotAtomic;
216 case bitc::ORDERING_UNORDERED: return Unordered;
217 case bitc::ORDERING_MONOTONIC: return Monotonic;
218 case bitc::ORDERING_ACQUIRE: return Acquire;
219 case bitc::ORDERING_RELEASE: return Release;
220 case bitc::ORDERING_ACQREL: return AcquireRelease;
221 default: // Map unknown orderings to sequentially-consistent.
222 case bitc::ORDERING_SEQCST: return SequentiallyConsistent;
223 }
224}
225
226static SynchronizationScope GetDecodedSynchScope(unsigned Val) {
227 switch (Val) {
228 case bitc::SYNCHSCOPE_SINGLETHREAD: return SingleThread;
229 default: // Map unknown scopes to cross-thread.
230 case bitc::SYNCHSCOPE_CROSSTHREAD: return CrossThread;
231 }
232}
233
David Majnemerdad0a642014-06-27 18:19:56 +0000234static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) {
235 switch (Val) {
236 default: // Map unknown selection kinds to any.
237 case bitc::COMDAT_SELECTION_KIND_ANY:
238 return Comdat::Any;
239 case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH:
240 return Comdat::ExactMatch;
241 case bitc::COMDAT_SELECTION_KIND_LARGEST:
242 return Comdat::Largest;
243 case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES:
244 return Comdat::NoDuplicates;
245 case bitc::COMDAT_SELECTION_KIND_SAME_SIZE:
246 return Comdat::SameSize;
247 }
248}
249
Nico Rieck7157bb72014-01-14 15:22:47 +0000250static void UpgradeDLLImportExportLinkage(llvm::GlobalValue *GV, unsigned Val) {
251 switch (Val) {
252 case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break;
253 case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break;
254 }
255}
256
Gabor Greiff6caff662008-05-10 08:32:32 +0000257namespace llvm {
Chris Lattner1663cca2007-04-24 05:48:56 +0000258namespace {
259 /// @brief A class for maintaining the slot number definition
260 /// as a placeholder for the actual definition for forward constants defs.
261 class ConstantPlaceHolder : public ConstantExpr {
Craig Toppera60c0f12012-09-15 17:09:36 +0000262 void operator=(const ConstantPlaceHolder &) LLVM_DELETED_FUNCTION;
Gabor Greife9ecc682008-04-06 20:25:17 +0000263 public:
264 // allocate space for exactly one operand
265 void *operator new(size_t s) {
266 return User::operator new(s, 1);
267 }
Chris Lattner229907c2011-07-18 04:54:35 +0000268 explicit ConstantPlaceHolder(Type *Ty, LLVMContext& Context)
Gabor Greiff6caff662008-05-10 08:32:32 +0000269 : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000270 Op<0>() = UndefValue::get(Type::getInt32Ty(Context));
Chris Lattner1663cca2007-04-24 05:48:56 +0000271 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000272
Chris Lattner74429932008-08-21 02:34:16 +0000273 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
Chris Lattner74429932008-08-21 02:34:16 +0000274 static bool classof(const Value *V) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000275 return isa<ConstantExpr>(V) &&
Chris Lattner74429932008-08-21 02:34:16 +0000276 cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1;
277 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000278
279
Gabor Greiff6caff662008-05-10 08:32:32 +0000280 /// Provide fast operand accessors
Chris Lattner2d8cd802009-03-31 22:55:09 +0000281 //DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Chris Lattner1663cca2007-04-24 05:48:56 +0000282 };
283}
284
Chris Lattner2d8cd802009-03-31 22:55:09 +0000285// FIXME: can we inherit this from ConstantExpr?
Gabor Greiff6caff662008-05-10 08:32:32 +0000286template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000287struct OperandTraits<ConstantPlaceHolder> :
288 public FixedNumOperandTraits<ConstantPlaceHolder, 1> {
Gabor Greiff6caff662008-05-10 08:32:32 +0000289};
Gabor Greiff6caff662008-05-10 08:32:32 +0000290}
291
Chris Lattner2d8cd802009-03-31 22:55:09 +0000292
293void BitcodeReaderValueList::AssignValue(Value *V, unsigned Idx) {
294 if (Idx == size()) {
295 push_back(V);
296 return;
297 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000298
Chris Lattner2d8cd802009-03-31 22:55:09 +0000299 if (Idx >= size())
300 resize(Idx+1);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000301
Chris Lattner2d8cd802009-03-31 22:55:09 +0000302 WeakVH &OldV = ValuePtrs[Idx];
Craig Topper2617dcc2014-04-15 06:32:26 +0000303 if (!OldV) {
Chris Lattner2d8cd802009-03-31 22:55:09 +0000304 OldV = V;
305 return;
306 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000307
Chris Lattner2d8cd802009-03-31 22:55:09 +0000308 // Handle constants and non-constants (e.g. instrs) differently for
309 // efficiency.
310 if (Constant *PHC = dyn_cast<Constant>(&*OldV)) {
311 ResolveConstants.push_back(std::make_pair(PHC, Idx));
312 OldV = V;
313 } else {
314 // If there was a forward reference to this value, replace it.
315 Value *PrevVal = OldV;
316 OldV->replaceAllUsesWith(V);
317 delete PrevVal;
Gabor Greiff6caff662008-05-10 08:32:32 +0000318 }
319}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000320
Gabor Greiff6caff662008-05-10 08:32:32 +0000321
Chris Lattner1663cca2007-04-24 05:48:56 +0000322Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx,
Chris Lattner229907c2011-07-18 04:54:35 +0000323 Type *Ty) {
Chris Lattner2d8cd802009-03-31 22:55:09 +0000324 if (Idx >= size())
Gabor Greiff6caff662008-05-10 08:32:32 +0000325 resize(Idx + 1);
Chris Lattner1663cca2007-04-24 05:48:56 +0000326
Chris Lattner2d8cd802009-03-31 22:55:09 +0000327 if (Value *V = ValuePtrs[Idx]) {
Chris Lattner83930552007-05-01 07:01:57 +0000328 assert(Ty == V->getType() && "Type mismatch in constant table!");
329 return cast<Constant>(V);
Chris Lattner1e16bcf72007-04-24 07:07:11 +0000330 }
Chris Lattner1663cca2007-04-24 05:48:56 +0000331
332 // Create and return a placeholder, which will later be RAUW'd.
Owen Andersone9f98042009-07-07 20:18:58 +0000333 Constant *C = new ConstantPlaceHolder(Ty, Context);
Chris Lattner2d8cd802009-03-31 22:55:09 +0000334 ValuePtrs[Idx] = C;
Chris Lattner1663cca2007-04-24 05:48:56 +0000335 return C;
336}
337
Chris Lattner229907c2011-07-18 04:54:35 +0000338Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, Type *Ty) {
Chris Lattner2d8cd802009-03-31 22:55:09 +0000339 if (Idx >= size())
Gabor Greiff6caff662008-05-10 08:32:32 +0000340 resize(Idx + 1);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000341
Chris Lattner2d8cd802009-03-31 22:55:09 +0000342 if (Value *V = ValuePtrs[Idx]) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000343 assert((!Ty || Ty == V->getType()) && "Type mismatch in value table!");
Chris Lattner83930552007-05-01 07:01:57 +0000344 return V;
345 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000346
Chris Lattner1fc27f02007-05-02 05:16:49 +0000347 // No type specified, must be invalid reference.
Craig Topper2617dcc2014-04-15 06:32:26 +0000348 if (!Ty) return nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000349
Chris Lattner83930552007-05-01 07:01:57 +0000350 // Create and return a placeholder, which will later be RAUW'd.
351 Value *V = new Argument(Ty);
Chris Lattner2d8cd802009-03-31 22:55:09 +0000352 ValuePtrs[Idx] = V;
Chris Lattner83930552007-05-01 07:01:57 +0000353 return V;
354}
355
Chris Lattner74429932008-08-21 02:34:16 +0000356/// ResolveConstantForwardRefs - Once all constants are read, this method bulk
357/// resolves any forward references. The idea behind this is that we sometimes
358/// get constants (such as large arrays) which reference *many* forward ref
359/// constants. Replacing each of these causes a lot of thrashing when
360/// building/reuniquing the constant. Instead of doing this, we look at all the
361/// uses and rewrite all the place holders at once for any constant that uses
362/// a placeholder.
363void BitcodeReaderValueList::ResolveConstantForwardRefs() {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000364 // Sort the values by-pointer so that they are efficient to look up with a
Chris Lattner74429932008-08-21 02:34:16 +0000365 // binary search.
366 std::sort(ResolveConstants.begin(), ResolveConstants.end());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000367
Chris Lattner74429932008-08-21 02:34:16 +0000368 SmallVector<Constant*, 64> NewOps;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000369
Chris Lattner74429932008-08-21 02:34:16 +0000370 while (!ResolveConstants.empty()) {
Chris Lattner2d8cd802009-03-31 22:55:09 +0000371 Value *RealVal = operator[](ResolveConstants.back().second);
Chris Lattner74429932008-08-21 02:34:16 +0000372 Constant *Placeholder = ResolveConstants.back().first;
373 ResolveConstants.pop_back();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000374
Chris Lattner74429932008-08-21 02:34:16 +0000375 // Loop over all users of the placeholder, updating them to reference the
376 // new value. If they reference more than one placeholder, update them all
377 // at once.
378 while (!Placeholder->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000379 auto UI = Placeholder->user_begin();
Gabor Greif2c0ab482010-07-09 16:01:21 +0000380 User *U = *UI;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000381
Chris Lattner74429932008-08-21 02:34:16 +0000382 // If the using object isn't uniqued, just update the operands. This
383 // handles instructions and initializers for global variables.
Gabor Greif2c0ab482010-07-09 16:01:21 +0000384 if (!isa<Constant>(U) || isa<GlobalValue>(U)) {
Chris Lattner479c5d92008-08-21 17:31:45 +0000385 UI.getUse().set(RealVal);
Chris Lattner74429932008-08-21 02:34:16 +0000386 continue;
387 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000388
Chris Lattner74429932008-08-21 02:34:16 +0000389 // Otherwise, we have a constant that uses the placeholder. Replace that
390 // constant with a new constant that has *all* placeholder uses updated.
Gabor Greif2c0ab482010-07-09 16:01:21 +0000391 Constant *UserC = cast<Constant>(U);
Chris Lattner74429932008-08-21 02:34:16 +0000392 for (User::op_iterator I = UserC->op_begin(), E = UserC->op_end();
393 I != E; ++I) {
394 Value *NewOp;
395 if (!isa<ConstantPlaceHolder>(*I)) {
396 // Not a placeholder reference.
397 NewOp = *I;
398 } else if (*I == Placeholder) {
399 // Common case is that it just references this one placeholder.
400 NewOp = RealVal;
401 } else {
402 // Otherwise, look up the placeholder in ResolveConstants.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000403 ResolveConstantsTy::iterator It =
404 std::lower_bound(ResolveConstants.begin(), ResolveConstants.end(),
Chris Lattner74429932008-08-21 02:34:16 +0000405 std::pair<Constant*, unsigned>(cast<Constant>(*I),
406 0));
407 assert(It != ResolveConstants.end() && It->first == *I);
Chris Lattner2d8cd802009-03-31 22:55:09 +0000408 NewOp = operator[](It->second);
Chris Lattner74429932008-08-21 02:34:16 +0000409 }
410
411 NewOps.push_back(cast<Constant>(NewOp));
412 }
413
414 // Make the new constant.
415 Constant *NewC;
416 if (ConstantArray *UserCA = dyn_cast<ConstantArray>(UserC)) {
Jay Foad83be3612011-06-22 09:24:39 +0000417 NewC = ConstantArray::get(UserCA->getType(), NewOps);
Chris Lattner74429932008-08-21 02:34:16 +0000418 } else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) {
Chris Lattnercc19efa2011-06-20 04:01:31 +0000419 NewC = ConstantStruct::get(UserCS->getType(), NewOps);
Chris Lattner74429932008-08-21 02:34:16 +0000420 } else if (isa<ConstantVector>(UserC)) {
Chris Lattner69229312011-02-15 00:14:00 +0000421 NewC = ConstantVector::get(NewOps);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000422 } else {
423 assert(isa<ConstantExpr>(UserC) && "Must be a ConstantExpr.");
Jay Foad5c984e562011-04-13 13:46:01 +0000424 NewC = cast<ConstantExpr>(UserC)->getWithOperands(NewOps);
Chris Lattner74429932008-08-21 02:34:16 +0000425 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000426
Chris Lattner74429932008-08-21 02:34:16 +0000427 UserC->replaceAllUsesWith(NewC);
428 UserC->destroyConstant();
429 NewOps.clear();
430 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000431
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000432 // Update all ValueHandles, they should be the only users at this point.
433 Placeholder->replaceAllUsesWith(RealVal);
Chris Lattner74429932008-08-21 02:34:16 +0000434 delete Placeholder;
435 }
436}
437
Devang Patel05eb6172009-08-04 06:00:18 +0000438void BitcodeReaderMDValueList::AssignValue(Value *V, unsigned Idx) {
439 if (Idx == size()) {
440 push_back(V);
441 return;
442 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000443
Devang Patel05eb6172009-08-04 06:00:18 +0000444 if (Idx >= size())
445 resize(Idx+1);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000446
Devang Patel05eb6172009-08-04 06:00:18 +0000447 WeakVH &OldV = MDValuePtrs[Idx];
Craig Topper2617dcc2014-04-15 06:32:26 +0000448 if (!OldV) {
Devang Patel05eb6172009-08-04 06:00:18 +0000449 OldV = V;
450 return;
451 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000452
Devang Patel05eb6172009-08-04 06:00:18 +0000453 // If there was a forward reference to this value, replace it.
Dan Gohman16a5d982010-08-20 22:02:26 +0000454 MDNode *PrevVal = cast<MDNode>(OldV);
Devang Patel05eb6172009-08-04 06:00:18 +0000455 OldV->replaceAllUsesWith(V);
Dan Gohman16a5d982010-08-20 22:02:26 +0000456 MDNode::deleteTemporary(PrevVal);
Devang Patel116b4a02009-09-03 01:38:02 +0000457 // Deleting PrevVal sets Idx value in MDValuePtrs to null. Set new
458 // value for Idx.
459 MDValuePtrs[Idx] = V;
Devang Patel05eb6172009-08-04 06:00:18 +0000460}
461
462Value *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) {
463 if (Idx >= size())
464 resize(Idx + 1);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000465
Devang Patel05eb6172009-08-04 06:00:18 +0000466 if (Value *V = MDValuePtrs[Idx]) {
Chris Lattnerfdd87902009-10-05 05:54:46 +0000467 assert(V->getType()->isMetadataTy() && "Type mismatch in value table!");
Devang Patel05eb6172009-08-04 06:00:18 +0000468 return V;
469 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000470
Devang Patel05eb6172009-08-04 06:00:18 +0000471 // Create and return a placeholder, which will later be RAUW'd.
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000472 Value *V = MDNode::getTemporary(Context, None);
Devang Patel05eb6172009-08-04 06:00:18 +0000473 MDValuePtrs[Idx] = V;
474 return V;
475}
Chris Lattner1314b992007-04-22 06:23:29 +0000476
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000477Type *BitcodeReader::getTypeByID(unsigned ID) {
478 // The type table size is always specified correctly.
479 if (ID >= TypeList.size())
Craig Topper2617dcc2014-04-15 06:32:26 +0000480 return nullptr;
Derek Schuff206dddd2012-02-06 19:03:04 +0000481
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000482 if (Type *Ty = TypeList[ID])
483 return Ty;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000484
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000485 // If we have a forward reference, the only possible case is when it is to a
486 // named struct. Just create a placeholder for now.
Chris Lattner335d3992011-08-12 18:06:37 +0000487 return TypeList[ID] = StructType::create(Context);
Chris Lattner1314b992007-04-22 06:23:29 +0000488}
489
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000490
Chris Lattnerfee5a372007-05-04 03:30:17 +0000491//===----------------------------------------------------------------------===//
492// Functions for parsing blocks from the bitcode file
493//===----------------------------------------------------------------------===//
494
Bill Wendling56aeccc2013-02-04 23:32:23 +0000495
496/// \brief This fills an AttrBuilder object with the LLVM attributes that have
497/// been decoded from the given integer. This function must stay in sync with
498/// 'encodeLLVMAttributesForBitcode'.
499static void decodeLLVMAttributesForBitcode(AttrBuilder &B,
500 uint64_t EncodedAttrs) {
501 // FIXME: Remove in 4.0.
502
503 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
504 // the bits above 31 down by 11 bits.
505 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
506 assert((!Alignment || isPowerOf2_32(Alignment)) &&
507 "Alignment must be a power of two.");
508
509 if (Alignment)
510 B.addAlignmentAttr(Alignment);
Kostya Serebryanyd688bab2013-02-11 08:13:54 +0000511 B.addRawValue(((EncodedAttrs & (0xfffffULL << 32)) >> 11) |
Bill Wendling56aeccc2013-02-04 23:32:23 +0000512 (EncodedAttrs & 0xffff));
513}
514
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000515std::error_code BitcodeReader::ParseAttributeBlock() {
Chris Lattner982ec1e2007-05-05 00:17:00 +0000516 if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000517 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000518
Devang Patela05633e2008-09-26 22:53:05 +0000519 if (!MAttributes.empty())
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000520 return Error(BitcodeError::InvalidMultipleBlocks);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000521
Chris Lattnerfee5a372007-05-04 03:30:17 +0000522 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000523
Bill Wendling71173cb2013-01-27 00:36:48 +0000524 SmallVector<AttributeSet, 8> Attrs;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000525
Chris Lattnerfee5a372007-05-04 03:30:17 +0000526 // Read all the records.
527 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +0000528 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +0000529
Chris Lattner27d38752013-01-20 02:13:19 +0000530 switch (Entry.Kind) {
531 case BitstreamEntry::SubBlock: // Handled for us already.
532 case BitstreamEntry::Error:
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000533 return Error(BitcodeError::MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +0000534 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000535 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +0000536 case BitstreamEntry::Record:
537 // The interesting case.
538 break;
Chris Lattnerfee5a372007-05-04 03:30:17 +0000539 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000540
Chris Lattnerfee5a372007-05-04 03:30:17 +0000541 // Read a record.
542 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +0000543 switch (Stream.readRecord(Entry.ID, Record)) {
Chris Lattnerfee5a372007-05-04 03:30:17 +0000544 default: // Default behavior: ignore.
545 break;
Bill Wendling56aeccc2013-02-04 23:32:23 +0000546 case bitc::PARAMATTR_CODE_ENTRY_OLD: { // ENTRY: [paramidx0, attr0, ...]
547 // FIXME: Remove in 4.0.
Chris Lattnerfee5a372007-05-04 03:30:17 +0000548 if (Record.size() & 1)
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000549 return Error(BitcodeError::InvalidRecord);
Chris Lattnerfee5a372007-05-04 03:30:17 +0000550
Chris Lattnerfee5a372007-05-04 03:30:17 +0000551 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
Bill Wendling60011b82013-01-29 01:43:29 +0000552 AttrBuilder B;
Bill Wendling56aeccc2013-02-04 23:32:23 +0000553 decodeLLVMAttributesForBitcode(B, Record[i+1]);
Bill Wendling60011b82013-01-29 01:43:29 +0000554 Attrs.push_back(AttributeSet::get(Context, Record[i], B));
Devang Patela05633e2008-09-26 22:53:05 +0000555 }
Devang Patela05633e2008-09-26 22:53:05 +0000556
Bill Wendlinge94d8432012-12-07 23:16:57 +0000557 MAttributes.push_back(AttributeSet::get(Context, Attrs));
Chris Lattnerfee5a372007-05-04 03:30:17 +0000558 Attrs.clear();
559 break;
560 }
Bill Wendling0dc08912013-02-12 08:13:50 +0000561 case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [attrgrp0, attrgrp1, ...]
562 for (unsigned i = 0, e = Record.size(); i != e; ++i)
563 Attrs.push_back(MAttributeGroups[Record[i]]);
564
565 MAttributes.push_back(AttributeSet::get(Context, Attrs));
566 Attrs.clear();
567 break;
568 }
Duncan Sands04eb67e2007-11-20 14:09:29 +0000569 }
Chris Lattnerfee5a372007-05-04 03:30:17 +0000570 }
571}
572
Reid Klecknere9f36af2013-11-12 01:31:00 +0000573// Returns Attribute::None on unrecognized codes.
574static Attribute::AttrKind GetAttrFromCode(uint64_t Code) {
575 switch (Code) {
576 default:
577 return Attribute::None;
578 case bitc::ATTR_KIND_ALIGNMENT:
579 return Attribute::Alignment;
580 case bitc::ATTR_KIND_ALWAYS_INLINE:
581 return Attribute::AlwaysInline;
582 case bitc::ATTR_KIND_BUILTIN:
583 return Attribute::Builtin;
584 case bitc::ATTR_KIND_BY_VAL:
585 return Attribute::ByVal;
Reid Klecknera534a382013-12-19 02:14:12 +0000586 case bitc::ATTR_KIND_IN_ALLOCA:
587 return Attribute::InAlloca;
Reid Klecknere9f36af2013-11-12 01:31:00 +0000588 case bitc::ATTR_KIND_COLD:
589 return Attribute::Cold;
590 case bitc::ATTR_KIND_INLINE_HINT:
591 return Attribute::InlineHint;
592 case bitc::ATTR_KIND_IN_REG:
593 return Attribute::InReg;
Tom Roeder44cb65f2014-06-05 19:29:43 +0000594 case bitc::ATTR_KIND_JUMP_TABLE:
595 return Attribute::JumpTable;
Reid Klecknere9f36af2013-11-12 01:31:00 +0000596 case bitc::ATTR_KIND_MIN_SIZE:
597 return Attribute::MinSize;
598 case bitc::ATTR_KIND_NAKED:
599 return Attribute::Naked;
600 case bitc::ATTR_KIND_NEST:
601 return Attribute::Nest;
602 case bitc::ATTR_KIND_NO_ALIAS:
603 return Attribute::NoAlias;
604 case bitc::ATTR_KIND_NO_BUILTIN:
605 return Attribute::NoBuiltin;
606 case bitc::ATTR_KIND_NO_CAPTURE:
607 return Attribute::NoCapture;
608 case bitc::ATTR_KIND_NO_DUPLICATE:
609 return Attribute::NoDuplicate;
610 case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT:
611 return Attribute::NoImplicitFloat;
612 case bitc::ATTR_KIND_NO_INLINE:
613 return Attribute::NoInline;
614 case bitc::ATTR_KIND_NON_LAZY_BIND:
615 return Attribute::NonLazyBind;
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000616 case bitc::ATTR_KIND_NON_NULL:
617 return Attribute::NonNull;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000618 case bitc::ATTR_KIND_DEREFERENCEABLE:
619 return Attribute::Dereferenceable;
Reid Klecknere9f36af2013-11-12 01:31:00 +0000620 case bitc::ATTR_KIND_NO_RED_ZONE:
621 return Attribute::NoRedZone;
622 case bitc::ATTR_KIND_NO_RETURN:
623 return Attribute::NoReturn;
624 case bitc::ATTR_KIND_NO_UNWIND:
625 return Attribute::NoUnwind;
626 case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE:
627 return Attribute::OptimizeForSize;
628 case bitc::ATTR_KIND_OPTIMIZE_NONE:
629 return Attribute::OptimizeNone;
630 case bitc::ATTR_KIND_READ_NONE:
631 return Attribute::ReadNone;
632 case bitc::ATTR_KIND_READ_ONLY:
633 return Attribute::ReadOnly;
634 case bitc::ATTR_KIND_RETURNED:
635 return Attribute::Returned;
636 case bitc::ATTR_KIND_RETURNS_TWICE:
637 return Attribute::ReturnsTwice;
638 case bitc::ATTR_KIND_S_EXT:
639 return Attribute::SExt;
640 case bitc::ATTR_KIND_STACK_ALIGNMENT:
641 return Attribute::StackAlignment;
642 case bitc::ATTR_KIND_STACK_PROTECT:
643 return Attribute::StackProtect;
644 case bitc::ATTR_KIND_STACK_PROTECT_REQ:
645 return Attribute::StackProtectReq;
646 case bitc::ATTR_KIND_STACK_PROTECT_STRONG:
647 return Attribute::StackProtectStrong;
648 case bitc::ATTR_KIND_STRUCT_RET:
649 return Attribute::StructRet;
650 case bitc::ATTR_KIND_SANITIZE_ADDRESS:
651 return Attribute::SanitizeAddress;
652 case bitc::ATTR_KIND_SANITIZE_THREAD:
653 return Attribute::SanitizeThread;
654 case bitc::ATTR_KIND_SANITIZE_MEMORY:
655 return Attribute::SanitizeMemory;
656 case bitc::ATTR_KIND_UW_TABLE:
657 return Attribute::UWTable;
658 case bitc::ATTR_KIND_Z_EXT:
659 return Attribute::ZExt;
660 }
661}
662
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000663std::error_code BitcodeReader::ParseAttrKind(uint64_t Code,
664 Attribute::AttrKind *Kind) {
Reid Klecknere9f36af2013-11-12 01:31:00 +0000665 *Kind = GetAttrFromCode(Code);
666 if (*Kind == Attribute::None)
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000667 return Error(BitcodeError::InvalidValue);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000668 return std::error_code();
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000669}
670
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000671std::error_code BitcodeReader::ParseAttributeGroupBlock() {
Bill Wendlingba629332013-02-10 23:24:25 +0000672 if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID))
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000673 return Error(BitcodeError::InvalidRecord);
Bill Wendlingba629332013-02-10 23:24:25 +0000674
675 if (!MAttributeGroups.empty())
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000676 return Error(BitcodeError::InvalidMultipleBlocks);
Bill Wendlingba629332013-02-10 23:24:25 +0000677
678 SmallVector<uint64_t, 64> Record;
679
680 // Read all the records.
681 while (1) {
682 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
683
684 switch (Entry.Kind) {
685 case BitstreamEntry::SubBlock: // Handled for us already.
686 case BitstreamEntry::Error:
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000687 return Error(BitcodeError::MalformedBlock);
Bill Wendlingba629332013-02-10 23:24:25 +0000688 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000689 return std::error_code();
Bill Wendlingba629332013-02-10 23:24:25 +0000690 case BitstreamEntry::Record:
691 // The interesting case.
692 break;
693 }
694
695 // Read a record.
696 Record.clear();
697 switch (Stream.readRecord(Entry.ID, Record)) {
698 default: // Default behavior: ignore.
699 break;
700 case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...]
701 if (Record.size() < 3)
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000702 return Error(BitcodeError::InvalidRecord);
Bill Wendlingba629332013-02-10 23:24:25 +0000703
Bill Wendlinge46707e2013-02-11 22:32:29 +0000704 uint64_t GrpID = Record[0];
Bill Wendlingba629332013-02-10 23:24:25 +0000705 uint64_t Idx = Record[1]; // Index of the object this attribute refers to.
706
707 AttrBuilder B;
708 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
709 if (Record[i] == 0) { // Enum attribute
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000710 Attribute::AttrKind Kind;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000711 if (std::error_code EC = ParseAttrKind(Record[++i], &Kind))
Rafael Espindola48da4f42013-11-04 16:16:24 +0000712 return EC;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000713
714 B.addAttribute(Kind);
Hal Finkele15442c2014-07-18 06:51:55 +0000715 } else if (Record[i] == 1) { // Integer attribute
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000716 Attribute::AttrKind Kind;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000717 if (std::error_code EC = ParseAttrKind(Record[++i], &Kind))
Rafael Espindola48da4f42013-11-04 16:16:24 +0000718 return EC;
Tobias Grosser0a8e12f2013-07-26 04:16:55 +0000719 if (Kind == Attribute::Alignment)
Bill Wendlingba629332013-02-10 23:24:25 +0000720 B.addAlignmentAttr(Record[++i]);
Hal Finkelb0407ba2014-07-18 15:51:28 +0000721 else if (Kind == Attribute::StackAlignment)
Bill Wendlingba629332013-02-10 23:24:25 +0000722 B.addStackAlignmentAttr(Record[++i]);
Hal Finkelb0407ba2014-07-18 15:51:28 +0000723 else if (Kind == Attribute::Dereferenceable)
724 B.addDereferenceableAttr(Record[++i]);
Bill Wendlingba629332013-02-10 23:24:25 +0000725 } else { // String attribute
Bill Wendlinge46707e2013-02-11 22:32:29 +0000726 assert((Record[i] == 3 || Record[i] == 4) &&
727 "Invalid attribute group entry");
Bill Wendlingba629332013-02-10 23:24:25 +0000728 bool HasValue = (Record[i++] == 4);
729 SmallString<64> KindStr;
730 SmallString<64> ValStr;
731
732 while (Record[i] != 0 && i != e)
733 KindStr += Record[i++];
Bill Wendlinge46707e2013-02-11 22:32:29 +0000734 assert(Record[i] == 0 && "Kind string not null terminated");
Bill Wendlingba629332013-02-10 23:24:25 +0000735
736 if (HasValue) {
737 // Has a value associated with it.
Bill Wendlinge46707e2013-02-11 22:32:29 +0000738 ++i; // Skip the '0' that terminates the "kind" string.
Bill Wendlingba629332013-02-10 23:24:25 +0000739 while (Record[i] != 0 && i != e)
740 ValStr += Record[i++];
Bill Wendlinge46707e2013-02-11 22:32:29 +0000741 assert(Record[i] == 0 && "Value string not null terminated");
Bill Wendlingba629332013-02-10 23:24:25 +0000742 }
743
744 B.addAttribute(KindStr.str(), ValStr.str());
745 }
746 }
747
Bill Wendlinge46707e2013-02-11 22:32:29 +0000748 MAttributeGroups[GrpID] = AttributeSet::get(Context, Idx, B);
Bill Wendlingba629332013-02-10 23:24:25 +0000749 break;
750 }
751 }
752 }
753}
754
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000755std::error_code BitcodeReader::ParseTypeTable() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000756 if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW))
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000757 return Error(BitcodeError::InvalidRecord);
Derek Schuff206dddd2012-02-06 19:03:04 +0000758
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000759 return ParseTypeTableBody();
760}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000761
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000762std::error_code BitcodeReader::ParseTypeTableBody() {
Chris Lattner1314b992007-04-22 06:23:29 +0000763 if (!TypeList.empty())
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000764 return Error(BitcodeError::InvalidMultipleBlocks);
Chris Lattner1314b992007-04-22 06:23:29 +0000765
766 SmallVector<uint64_t, 64> Record;
767 unsigned NumRecords = 0;
768
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000769 SmallString<64> TypeName;
Derek Schuff206dddd2012-02-06 19:03:04 +0000770
Chris Lattner1314b992007-04-22 06:23:29 +0000771 // Read all the records for this type table.
772 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +0000773 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +0000774
Chris Lattner27d38752013-01-20 02:13:19 +0000775 switch (Entry.Kind) {
776 case BitstreamEntry::SubBlock: // Handled for us already.
777 case BitstreamEntry::Error:
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000778 return Error(BitcodeError::MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +0000779 case BitstreamEntry::EndBlock:
Chris Lattner1314b992007-04-22 06:23:29 +0000780 if (NumRecords != TypeList.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000781 return Error(BitcodeError::MalformedBlock);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000782 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +0000783 case BitstreamEntry::Record:
784 // The interesting case.
785 break;
Chris Lattner1314b992007-04-22 06:23:29 +0000786 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000787
Chris Lattner1314b992007-04-22 06:23:29 +0000788 // Read a record.
789 Record.clear();
Craig Topper2617dcc2014-04-15 06:32:26 +0000790 Type *ResultTy = nullptr;
Chris Lattner27d38752013-01-20 02:13:19 +0000791 switch (Stream.readRecord(Entry.ID, Record)) {
Rafael Espindola48da4f42013-11-04 16:16:24 +0000792 default:
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000793 return Error(BitcodeError::InvalidValue);
Chris Lattner1314b992007-04-22 06:23:29 +0000794 case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
795 // TYPE_CODE_NUMENTRY contains a count of the number of types in the
796 // type list. This allows us to reserve space.
797 if (Record.size() < 1)
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000798 return Error(BitcodeError::InvalidRecord);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000799 TypeList.resize(Record[0]);
Chris Lattner1314b992007-04-22 06:23:29 +0000800 continue;
Chris Lattner1314b992007-04-22 06:23:29 +0000801 case bitc::TYPE_CODE_VOID: // VOID
Owen Anderson55f1c092009-08-13 21:58:54 +0000802 ResultTy = Type::getVoidTy(Context);
Chris Lattner1314b992007-04-22 06:23:29 +0000803 break;
Dan Gohman518cda42011-12-17 00:04:22 +0000804 case bitc::TYPE_CODE_HALF: // HALF
805 ResultTy = Type::getHalfTy(Context);
806 break;
Chris Lattner1314b992007-04-22 06:23:29 +0000807 case bitc::TYPE_CODE_FLOAT: // FLOAT
Owen Anderson55f1c092009-08-13 21:58:54 +0000808 ResultTy = Type::getFloatTy(Context);
Chris Lattner1314b992007-04-22 06:23:29 +0000809 break;
810 case bitc::TYPE_CODE_DOUBLE: // DOUBLE
Owen Anderson55f1c092009-08-13 21:58:54 +0000811 ResultTy = Type::getDoubleTy(Context);
Chris Lattner1314b992007-04-22 06:23:29 +0000812 break;
Dale Johannesenff4c3be2007-08-03 01:03:46 +0000813 case bitc::TYPE_CODE_X86_FP80: // X86_FP80
Owen Anderson55f1c092009-08-13 21:58:54 +0000814 ResultTy = Type::getX86_FP80Ty(Context);
Dale Johannesenff4c3be2007-08-03 01:03:46 +0000815 break;
816 case bitc::TYPE_CODE_FP128: // FP128
Owen Anderson55f1c092009-08-13 21:58:54 +0000817 ResultTy = Type::getFP128Ty(Context);
Dale Johannesenff4c3be2007-08-03 01:03:46 +0000818 break;
819 case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128
Owen Anderson55f1c092009-08-13 21:58:54 +0000820 ResultTy = Type::getPPC_FP128Ty(Context);
Dale Johannesenff4c3be2007-08-03 01:03:46 +0000821 break;
Chris Lattner1314b992007-04-22 06:23:29 +0000822 case bitc::TYPE_CODE_LABEL: // LABEL
Owen Anderson55f1c092009-08-13 21:58:54 +0000823 ResultTy = Type::getLabelTy(Context);
Chris Lattner1314b992007-04-22 06:23:29 +0000824 break;
Nick Lewyckyadbc2842009-05-30 05:06:04 +0000825 case bitc::TYPE_CODE_METADATA: // METADATA
Owen Anderson55f1c092009-08-13 21:58:54 +0000826 ResultTy = Type::getMetadataTy(Context);
Nick Lewyckyadbc2842009-05-30 05:06:04 +0000827 break;
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000828 case bitc::TYPE_CODE_X86_MMX: // X86_MMX
829 ResultTy = Type::getX86_MMXTy(Context);
830 break;
Chris Lattner1314b992007-04-22 06:23:29 +0000831 case bitc::TYPE_CODE_INTEGER: // INTEGER: [width]
832 if (Record.size() < 1)
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000833 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000834
Owen Anderson55f1c092009-08-13 21:58:54 +0000835 ResultTy = IntegerType::get(Context, Record[0]);
Chris Lattner1314b992007-04-22 06:23:29 +0000836 break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000837 case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000838 // [pointee type, address space]
Chris Lattner1314b992007-04-22 06:23:29 +0000839 if (Record.size() < 1)
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000840 return Error(BitcodeError::InvalidRecord);
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000841 unsigned AddressSpace = 0;
842 if (Record.size() == 2)
843 AddressSpace = Record[1];
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000844 ResultTy = getTypeByID(Record[0]);
Craig Topper2617dcc2014-04-15 06:32:26 +0000845 if (!ResultTy)
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000846 return Error(BitcodeError::InvalidType);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000847 ResultTy = PointerType::get(ResultTy, AddressSpace);
Chris Lattner1314b992007-04-22 06:23:29 +0000848 break;
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000849 }
Nuno Lopes561dae02012-05-23 15:19:39 +0000850 case bitc::TYPE_CODE_FUNCTION_OLD: {
851 // FIXME: attrid is dead, remove it in LLVM 4.0
852 // FUNCTION: [vararg, attrid, retty, paramty x N]
853 if (Record.size() < 3)
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000854 return Error(BitcodeError::InvalidRecord);
Nuno Lopes561dae02012-05-23 15:19:39 +0000855 SmallVector<Type*, 8> ArgTys;
856 for (unsigned i = 3, e = Record.size(); i != e; ++i) {
857 if (Type *T = getTypeByID(Record[i]))
858 ArgTys.push_back(T);
859 else
860 break;
861 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000862
Nuno Lopes561dae02012-05-23 15:19:39 +0000863 ResultTy = getTypeByID(Record[2]);
Craig Topper2617dcc2014-04-15 06:32:26 +0000864 if (!ResultTy || ArgTys.size() < Record.size()-3)
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000865 return Error(BitcodeError::InvalidType);
Nuno Lopes561dae02012-05-23 15:19:39 +0000866
867 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
868 break;
869 }
Chad Rosier95898722011-11-03 00:14:01 +0000870 case bitc::TYPE_CODE_FUNCTION: {
871 // FUNCTION: [vararg, retty, paramty x N]
872 if (Record.size() < 2)
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000873 return Error(BitcodeError::InvalidRecord);
Chris Lattnercc3aaf12012-01-27 03:15:49 +0000874 SmallVector<Type*, 8> ArgTys;
Chad Rosier95898722011-11-03 00:14:01 +0000875 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
876 if (Type *T = getTypeByID(Record[i]))
877 ArgTys.push_back(T);
878 else
879 break;
880 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000881
Chad Rosier95898722011-11-03 00:14:01 +0000882 ResultTy = getTypeByID(Record[1]);
Craig Topper2617dcc2014-04-15 06:32:26 +0000883 if (!ResultTy || ArgTys.size() < Record.size()-2)
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000884 return Error(BitcodeError::InvalidType);
Chad Rosier95898722011-11-03 00:14:01 +0000885
886 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
887 break;
888 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000889 case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N]
Chris Lattner3c5616e2007-05-06 08:21:50 +0000890 if (Record.size() < 1)
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000891 return Error(BitcodeError::InvalidRecord);
Chris Lattnercc3aaf12012-01-27 03:15:49 +0000892 SmallVector<Type*, 8> EltTys;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000893 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
894 if (Type *T = getTypeByID(Record[i]))
895 EltTys.push_back(T);
896 else
897 break;
898 }
899 if (EltTys.size() != Record.size()-1)
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000900 return Error(BitcodeError::InvalidType);
Owen Anderson03cb69f2009-08-05 23:16:16 +0000901 ResultTy = StructType::get(Context, EltTys, Record[0]);
Chris Lattner1314b992007-04-22 06:23:29 +0000902 break;
903 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000904 case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N]
905 if (ConvertToString(Record, 0, TypeName))
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000906 return Error(BitcodeError::InvalidRecord);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000907 continue;
908
909 case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N]
910 if (Record.size() < 1)
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000911 return Error(BitcodeError::InvalidRecord);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000912
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000913 if (NumRecords >= TypeList.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000914 return Error(BitcodeError::InvalidTYPETable);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000915
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000916 // Check to see if this was forward referenced, if so fill in the temp.
917 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
918 if (Res) {
919 Res->setName(TypeName);
Craig Topper2617dcc2014-04-15 06:32:26 +0000920 TypeList[NumRecords] = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000921 } else // Otherwise, create a new struct.
Chris Lattner335d3992011-08-12 18:06:37 +0000922 Res = StructType::create(Context, TypeName);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000923 TypeName.clear();
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000924
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000925 SmallVector<Type*, 8> EltTys;
926 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
927 if (Type *T = getTypeByID(Record[i]))
928 EltTys.push_back(T);
929 else
930 break;
931 }
932 if (EltTys.size() != Record.size()-1)
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000933 return Error(BitcodeError::InvalidRecord);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000934 Res->setBody(EltTys, Record[0]);
935 ResultTy = Res;
936 break;
937 }
938 case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: []
939 if (Record.size() != 1)
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000940 return Error(BitcodeError::InvalidRecord);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000941
942 if (NumRecords >= TypeList.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000943 return Error(BitcodeError::InvalidTYPETable);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000944
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000945 // Check to see if this was forward referenced, if so fill in the temp.
946 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
947 if (Res) {
948 Res->setName(TypeName);
Craig Topper2617dcc2014-04-15 06:32:26 +0000949 TypeList[NumRecords] = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000950 } else // Otherwise, create a new struct with no body.
Chris Lattner335d3992011-08-12 18:06:37 +0000951 Res = StructType::create(Context, TypeName);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000952 TypeName.clear();
953 ResultTy = Res;
954 break;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000955 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000956 case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty]
957 if (Record.size() < 2)
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000958 return Error(BitcodeError::InvalidRecord);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000959 if ((ResultTy = getTypeByID(Record[1])))
960 ResultTy = ArrayType::get(ResultTy, Record[0]);
961 else
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000962 return Error(BitcodeError::InvalidType);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000963 break;
964 case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty]
965 if (Record.size() < 2)
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000966 return Error(BitcodeError::InvalidRecord);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000967 if ((ResultTy = getTypeByID(Record[1])))
968 ResultTy = VectorType::get(ResultTy, Record[0]);
969 else
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000970 return Error(BitcodeError::InvalidType);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000971 break;
972 }
973
974 if (NumRecords >= TypeList.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000975 return Error(BitcodeError::InvalidTYPETable);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000976 assert(ResultTy && "Didn't read a type?");
Craig Topper2617dcc2014-04-15 06:32:26 +0000977 assert(!TypeList[NumRecords] && "Already read type?");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000978 TypeList[NumRecords++] = ResultTy;
979 }
980}
981
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000982std::error_code BitcodeReader::ParseValueSymbolTable() {
Chris Lattner982ec1e2007-05-05 00:17:00 +0000983 if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000984 return Error(BitcodeError::InvalidRecord);
Chris Lattnerccaa4482007-04-23 21:26:05 +0000985
986 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000987
Chris Lattnerccaa4482007-04-23 21:26:05 +0000988 // Read all the records for this value table.
989 SmallString<128> ValueName;
990 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +0000991 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +0000992
Chris Lattner27d38752013-01-20 02:13:19 +0000993 switch (Entry.Kind) {
994 case BitstreamEntry::SubBlock: // Handled for us already.
995 case BitstreamEntry::Error:
Rafael Espindolac3f2e732014-07-29 20:22:46 +0000996 return Error(BitcodeError::MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +0000997 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000998 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +0000999 case BitstreamEntry::Record:
1000 // The interesting case.
1001 break;
Chris Lattnerccaa4482007-04-23 21:26:05 +00001002 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001003
Chris Lattnerccaa4482007-04-23 21:26:05 +00001004 // Read a record.
1005 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +00001006 switch (Stream.readRecord(Entry.ID, Record)) {
Chris Lattnerccaa4482007-04-23 21:26:05 +00001007 default: // Default behavior: unknown type.
1008 break;
Chris Lattnere14cb882007-05-04 19:11:41 +00001009 case bitc::VST_CODE_ENTRY: { // VST_ENTRY: [valueid, namechar x N]
Chris Lattnerccaa4482007-04-23 21:26:05 +00001010 if (ConvertToString(Record, 1, ValueName))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001011 return Error(BitcodeError::InvalidRecord);
Chris Lattnerccaa4482007-04-23 21:26:05 +00001012 unsigned ValueID = Record[0];
Karthik Bhat82540e92014-03-27 12:08:23 +00001013 if (ValueID >= ValueList.size() || !ValueList[ValueID])
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001014 return Error(BitcodeError::InvalidRecord);
Chris Lattnerccaa4482007-04-23 21:26:05 +00001015 Value *V = ValueList[ValueID];
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001016
Daniel Dunbard786b512009-07-26 00:34:27 +00001017 V->setName(StringRef(ValueName.data(), ValueName.size()));
Chris Lattnerccaa4482007-04-23 21:26:05 +00001018 ValueName.clear();
1019 break;
Reid Spencerdea02bd2007-05-04 01:43:33 +00001020 }
Bill Wendling35a9c3c2011-04-10 23:18:04 +00001021 case bitc::VST_CODE_BBENTRY: {
Chris Lattner6be58c62007-05-03 22:18:21 +00001022 if (ConvertToString(Record, 1, ValueName))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001023 return Error(BitcodeError::InvalidRecord);
Chris Lattner6be58c62007-05-03 22:18:21 +00001024 BasicBlock *BB = getBasicBlock(Record[0]);
Craig Topper2617dcc2014-04-15 06:32:26 +00001025 if (!BB)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001026 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001027
Daniel Dunbard786b512009-07-26 00:34:27 +00001028 BB->setName(StringRef(ValueName.data(), ValueName.size()));
Chris Lattner6be58c62007-05-03 22:18:21 +00001029 ValueName.clear();
1030 break;
Chris Lattnerccaa4482007-04-23 21:26:05 +00001031 }
Reid Spencerdea02bd2007-05-04 01:43:33 +00001032 }
Chris Lattnerccaa4482007-04-23 21:26:05 +00001033 }
1034}
1035
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001036std::error_code BitcodeReader::ParseMetadata() {
Devang Patel89923232010-01-11 18:52:33 +00001037 unsigned NextMDValueNo = MDValueList.size();
Devang Patel7428d8a2009-07-22 17:43:22 +00001038
1039 if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001040 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001041
Devang Patel7428d8a2009-07-22 17:43:22 +00001042 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001043
Devang Patel7428d8a2009-07-22 17:43:22 +00001044 // Read all the records.
1045 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00001046 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00001047
Chris Lattner27d38752013-01-20 02:13:19 +00001048 switch (Entry.Kind) {
1049 case BitstreamEntry::SubBlock: // Handled for us already.
1050 case BitstreamEntry::Error:
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001051 return Error(BitcodeError::MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00001052 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001053 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00001054 case BitstreamEntry::Record:
1055 // The interesting case.
1056 break;
Devang Patel7428d8a2009-07-22 17:43:22 +00001057 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001058
Victor Hernandezb8fd1522010-01-10 07:14:18 +00001059 bool IsFunctionLocal = false;
Devang Patel7428d8a2009-07-22 17:43:22 +00001060 // Read a record.
1061 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +00001062 unsigned Code = Stream.readRecord(Entry.ID, Record);
Dan Gohmanbbcd04d2010-09-13 18:00:48 +00001063 switch (Code) {
Devang Patel7428d8a2009-07-22 17:43:22 +00001064 default: // Default behavior: ignore.
1065 break;
Devang Patel27c87ff2009-07-29 22:34:41 +00001066 case bitc::METADATA_NAME: {
Chris Lattner8d140532013-01-20 02:54:05 +00001067 // Read name of the named metadata.
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001068 SmallString<8> Name(Record.begin(), Record.end());
Devang Patel27c87ff2009-07-29 22:34:41 +00001069 Record.clear();
1070 Code = Stream.ReadCode();
1071
Chris Lattnerb8778552011-06-17 17:50:30 +00001072 // METADATA_NAME is always followed by METADATA_NAMED_NODE.
Chris Lattner27d38752013-01-20 02:13:19 +00001073 unsigned NextBitCode = Stream.readRecord(Code, Record);
Chris Lattnerb8778552011-06-17 17:50:30 +00001074 assert(NextBitCode == bitc::METADATA_NAMED_NODE); (void)NextBitCode;
Devang Patel27c87ff2009-07-29 22:34:41 +00001075
1076 // Read named metadata elements.
1077 unsigned Size = Record.size();
Dan Gohman2637cc12010-07-21 23:38:33 +00001078 NamedMDNode *NMD = TheModule->getOrInsertNamedMetadata(Name);
Devang Patel27c87ff2009-07-29 22:34:41 +00001079 for (unsigned i = 0; i != Size; ++i) {
Karthik Bhat82540e92014-03-27 12:08:23 +00001080 MDNode *MD = dyn_cast_or_null<MDNode>(MDValueList.getValueFwdRef(Record[i]));
Craig Topper2617dcc2014-04-15 06:32:26 +00001081 if (!MD)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001082 return Error(BitcodeError::InvalidRecord);
Dan Gohman2637cc12010-07-21 23:38:33 +00001083 NMD->addOperand(MD);
Devang Patel27c87ff2009-07-29 22:34:41 +00001084 }
Devang Patel27c87ff2009-07-29 22:34:41 +00001085 break;
1086 }
Chris Lattnerb8778552011-06-17 17:50:30 +00001087 case bitc::METADATA_FN_NODE:
Victor Hernandezb8fd1522010-01-10 07:14:18 +00001088 IsFunctionLocal = true;
1089 // fall-through
Chris Lattnerb8778552011-06-17 17:50:30 +00001090 case bitc::METADATA_NODE: {
Dan Gohman1e0213a2010-07-13 19:33:27 +00001091 if (Record.size() % 2 == 1)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001092 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001093
Devang Patele059ba6e2009-07-23 01:07:34 +00001094 unsigned Size = Record.size();
1095 SmallVector<Value*, 8> Elts;
1096 for (unsigned i = 0; i != Size; i += 2) {
Chris Lattner229907c2011-07-18 04:54:35 +00001097 Type *Ty = getTypeByID(Record[i]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001098 if (!Ty)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001099 return Error(BitcodeError::InvalidRecord);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001100 if (Ty->isMetadataTy())
Devang Patel05eb6172009-08-04 06:00:18 +00001101 Elts.push_back(MDValueList.getValueFwdRef(Record[i+1]));
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001102 else if (!Ty->isVoidTy())
Devang Patele059ba6e2009-07-23 01:07:34 +00001103 Elts.push_back(ValueList.getValueFwdRef(Record[i+1], Ty));
1104 else
Craig Topper2617dcc2014-04-15 06:32:26 +00001105 Elts.push_back(nullptr);
Devang Patele059ba6e2009-07-23 01:07:34 +00001106 }
Jay Foad5514afe2011-04-21 19:59:31 +00001107 Value *V = MDNode::getWhenValsUnresolved(Context, Elts, IsFunctionLocal);
Victor Hernandezb8fd1522010-01-10 07:14:18 +00001108 IsFunctionLocal = false;
Devang Patel89923232010-01-11 18:52:33 +00001109 MDValueList.AssignValue(V, NextMDValueNo++);
Devang Patele059ba6e2009-07-23 01:07:34 +00001110 break;
1111 }
Devang Patel7428d8a2009-07-22 17:43:22 +00001112 case bitc::METADATA_STRING: {
Eli Bendersky5d5e18d2014-06-25 15:41:00 +00001113 std::string String(Record.begin(), Record.end());
1114 llvm::UpgradeMDStringConstant(String);
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001115 Value *V = MDString::get(Context, String);
Devang Patel89923232010-01-11 18:52:33 +00001116 MDValueList.AssignValue(V, NextMDValueNo++);
Devang Patel7428d8a2009-07-22 17:43:22 +00001117 break;
1118 }
Devang Patelaf206b82009-09-18 19:26:43 +00001119 case bitc::METADATA_KIND: {
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001120 if (Record.size() < 2)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001121 return Error(BitcodeError::InvalidRecord);
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001122
Devang Patelb1a44772009-09-28 21:14:55 +00001123 unsigned Kind = Record[0];
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001124 SmallString<8> Name(Record.begin()+1, Record.end());
1125
Chris Lattnera0566972009-12-29 09:01:33 +00001126 unsigned NewKind = TheModule->getMDKindID(Name.str());
Dan Gohman43aa8f02010-07-20 21:42:28 +00001127 if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001128 return Error(BitcodeError::ConflictingMETADATA_KINDRecords);
Devang Patelaf206b82009-09-18 19:26:43 +00001129 break;
1130 }
Devang Patel7428d8a2009-07-22 17:43:22 +00001131 }
1132 }
1133}
1134
Jan Wen Voungafaced02012-10-11 20:20:40 +00001135/// decodeSignRotatedValue - Decode a signed value stored with the sign bit in
Chris Lattner08feb1e2007-04-24 04:04:35 +00001136/// the LSB for dense VBR encoding.
Jan Wen Voungafaced02012-10-11 20:20:40 +00001137uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) {
Chris Lattner08feb1e2007-04-24 04:04:35 +00001138 if ((V & 1) == 0)
1139 return V >> 1;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001140 if (V != 1)
Chris Lattner08feb1e2007-04-24 04:04:35 +00001141 return -(V >> 1);
1142 // There is no such thing as -0 with integers. "-0" really means MININT.
1143 return 1ULL << 63;
1144}
1145
Chris Lattner44c17072007-04-26 02:46:40 +00001146/// ResolveGlobalAndAliasInits - Resolve all of the initializers for global
1147/// values and aliases that we can.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001148std::error_code BitcodeReader::ResolveGlobalAndAliasInits() {
Chris Lattner44c17072007-04-26 02:46:40 +00001149 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist;
1150 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist;
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001151 std::vector<std::pair<Function*, unsigned> > FunctionPrefixWorklist;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001152
Chris Lattner44c17072007-04-26 02:46:40 +00001153 GlobalInitWorklist.swap(GlobalInits);
1154 AliasInitWorklist.swap(AliasInits);
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001155 FunctionPrefixWorklist.swap(FunctionPrefixes);
Chris Lattner44c17072007-04-26 02:46:40 +00001156
1157 while (!GlobalInitWorklist.empty()) {
Chris Lattner831d4202007-04-26 03:27:58 +00001158 unsigned ValID = GlobalInitWorklist.back().second;
Chris Lattner44c17072007-04-26 02:46:40 +00001159 if (ValID >= ValueList.size()) {
1160 // Not ready to resolve this yet, it requires something later in the file.
Chris Lattner831d4202007-04-26 03:27:58 +00001161 GlobalInits.push_back(GlobalInitWorklist.back());
Chris Lattner44c17072007-04-26 02:46:40 +00001162 } else {
Karthik Bhat82540e92014-03-27 12:08:23 +00001163 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
Chris Lattner44c17072007-04-26 02:46:40 +00001164 GlobalInitWorklist.back().first->setInitializer(C);
1165 else
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001166 return Error(BitcodeError::ExpectedConstant);
Chris Lattner44c17072007-04-26 02:46:40 +00001167 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001168 GlobalInitWorklist.pop_back();
Chris Lattner44c17072007-04-26 02:46:40 +00001169 }
1170
1171 while (!AliasInitWorklist.empty()) {
1172 unsigned ValID = AliasInitWorklist.back().second;
1173 if (ValID >= ValueList.size()) {
1174 AliasInits.push_back(AliasInitWorklist.back());
1175 } else {
Karthik Bhat82540e92014-03-27 12:08:23 +00001176 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
Rafael Espindola64c1e182014-06-03 02:41:57 +00001177 AliasInitWorklist.back().first->setAliasee(C);
Chris Lattner44c17072007-04-26 02:46:40 +00001178 else
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001179 return Error(BitcodeError::ExpectedConstant);
Chris Lattner44c17072007-04-26 02:46:40 +00001180 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001181 AliasInitWorklist.pop_back();
Chris Lattner44c17072007-04-26 02:46:40 +00001182 }
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001183
1184 while (!FunctionPrefixWorklist.empty()) {
1185 unsigned ValID = FunctionPrefixWorklist.back().second;
1186 if (ValID >= ValueList.size()) {
1187 FunctionPrefixes.push_back(FunctionPrefixWorklist.back());
1188 } else {
Karthik Bhat82540e92014-03-27 12:08:23 +00001189 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001190 FunctionPrefixWorklist.back().first->setPrefixData(C);
1191 else
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001192 return Error(BitcodeError::ExpectedConstant);
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001193 }
1194 FunctionPrefixWorklist.pop_back();
1195 }
1196
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001197 return std::error_code();
Chris Lattner44c17072007-04-26 02:46:40 +00001198}
1199
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001200static APInt ReadWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) {
1201 SmallVector<uint64_t, 8> Words(Vals.size());
1202 std::transform(Vals.begin(), Vals.end(), Words.begin(),
Jan Wen Voungafaced02012-10-11 20:20:40 +00001203 BitcodeReader::decodeSignRotatedValue);
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001204
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00001205 return APInt(TypeBits, Words);
1206}
1207
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001208std::error_code BitcodeReader::ParseConstants() {
Chris Lattner982ec1e2007-05-05 00:17:00 +00001209 if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001210 return Error(BitcodeError::InvalidRecord);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001211
1212 SmallVector<uint64_t, 64> Record;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001213
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001214 // Read all the records for this value table.
Chris Lattner229907c2011-07-18 04:54:35 +00001215 Type *CurTy = Type::getInt32Ty(Context);
Chris Lattner1663cca2007-04-24 05:48:56 +00001216 unsigned NextCstNo = ValueList.size();
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001217 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00001218 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00001219
Chris Lattner27d38752013-01-20 02:13:19 +00001220 switch (Entry.Kind) {
1221 case BitstreamEntry::SubBlock: // Handled for us already.
1222 case BitstreamEntry::Error:
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001223 return Error(BitcodeError::MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00001224 case BitstreamEntry::EndBlock:
1225 if (NextCstNo != ValueList.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001226 return Error(BitcodeError::InvalidConstantReference);
Joe Abbey97b7a172013-02-06 22:14:06 +00001227
Chris Lattner27d38752013-01-20 02:13:19 +00001228 // Once all the constants have been read, go through and resolve forward
1229 // references.
1230 ValueList.ResolveConstantForwardRefs();
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001231 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00001232 case BitstreamEntry::Record:
1233 // The interesting case.
Chris Lattner74429932008-08-21 02:34:16 +00001234 break;
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001235 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001236
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001237 // Read a record.
1238 Record.clear();
Craig Topper2617dcc2014-04-15 06:32:26 +00001239 Value *V = nullptr;
Chris Lattner27d38752013-01-20 02:13:19 +00001240 unsigned BitCode = Stream.readRecord(Entry.ID, Record);
Dan Gohman0ebd6962009-07-20 21:19:07 +00001241 switch (BitCode) {
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001242 default: // Default behavior: unknown constant
1243 case bitc::CST_CODE_UNDEF: // UNDEF
Owen Andersonb292b8c2009-07-30 23:03:37 +00001244 V = UndefValue::get(CurTy);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001245 break;
1246 case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid]
1247 if (Record.empty())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001248 return Error(BitcodeError::InvalidRecord);
Karthik Bhat82540e92014-03-27 12:08:23 +00001249 if (Record[0] >= TypeList.size() || !TypeList[Record[0]])
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001250 return Error(BitcodeError::InvalidRecord);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001251 CurTy = TypeList[Record[0]];
Chris Lattner08feb1e2007-04-24 04:04:35 +00001252 continue; // Skip the ValueList manipulation.
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001253 case bitc::CST_CODE_NULL: // NULL
Owen Anderson5a1acd92009-07-31 20:28:14 +00001254 V = Constant::getNullValue(CurTy);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001255 break;
1256 case bitc::CST_CODE_INTEGER: // INTEGER: [intval]
Duncan Sands19d0b472010-02-16 11:11:14 +00001257 if (!CurTy->isIntegerTy() || Record.empty())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001258 return Error(BitcodeError::InvalidRecord);
Jan Wen Voungafaced02012-10-11 20:20:40 +00001259 V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0]));
Chris Lattner08feb1e2007-04-24 04:04:35 +00001260 break;
Chris Lattnere14cb882007-05-04 19:11:41 +00001261 case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
Duncan Sands19d0b472010-02-16 11:11:14 +00001262 if (!CurTy->isIntegerTy() || Record.empty())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001263 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001264
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001265 APInt VInt = ReadWideAPInt(Record,
1266 cast<IntegerType>(CurTy)->getBitWidth());
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00001267 V = ConstantInt::get(Context, VInt);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001268
Chris Lattner08feb1e2007-04-24 04:04:35 +00001269 break;
1270 }
Dale Johannesen245dceb2007-09-11 18:32:33 +00001271 case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval]
Chris Lattner08feb1e2007-04-24 04:04:35 +00001272 if (Record.empty())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001273 return Error(BitcodeError::InvalidRecord);
Dan Gohman518cda42011-12-17 00:04:22 +00001274 if (CurTy->isHalfTy())
Tim Northover29178a32013-01-22 09:46:31 +00001275 V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf,
1276 APInt(16, (uint16_t)Record[0])));
Dan Gohman518cda42011-12-17 00:04:22 +00001277 else if (CurTy->isFloatTy())
Tim Northover29178a32013-01-22 09:46:31 +00001278 V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle,
1279 APInt(32, (uint32_t)Record[0])));
Chris Lattnerfdd87902009-10-05 05:54:46 +00001280 else if (CurTy->isDoubleTy())
Tim Northover29178a32013-01-22 09:46:31 +00001281 V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble,
1282 APInt(64, Record[0])));
Chris Lattnerfdd87902009-10-05 05:54:46 +00001283 else if (CurTy->isX86_FP80Ty()) {
Dale Johannesen93eefa02009-03-23 21:16:53 +00001284 // Bits are not stored the same way as a normal i80 APInt, compensate.
1285 uint64_t Rearrange[2];
1286 Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);
1287 Rearrange[1] = Record[0] >> 48;
Tim Northover29178a32013-01-22 09:46:31 +00001288 V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended,
1289 APInt(80, Rearrange)));
Chris Lattnerfdd87902009-10-05 05:54:46 +00001290 } else if (CurTy->isFP128Ty())
Tim Northover29178a32013-01-22 09:46:31 +00001291 V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad,
1292 APInt(128, Record)));
Chris Lattnerfdd87902009-10-05 05:54:46 +00001293 else if (CurTy->isPPC_FP128Ty())
Tim Northover29178a32013-01-22 09:46:31 +00001294 V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble,
1295 APInt(128, Record)));
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001296 else
Owen Andersonb292b8c2009-07-30 23:03:37 +00001297 V = UndefValue::get(CurTy);
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001298 break;
Dale Johannesen245dceb2007-09-11 18:32:33 +00001299 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001300
Chris Lattnere14cb882007-05-04 19:11:41 +00001301 case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]
1302 if (Record.empty())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001303 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001304
Chris Lattnere14cb882007-05-04 19:11:41 +00001305 unsigned Size = Record.size();
Chris Lattnercc3aaf12012-01-27 03:15:49 +00001306 SmallVector<Constant*, 16> Elts;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001307
Chris Lattner229907c2011-07-18 04:54:35 +00001308 if (StructType *STy = dyn_cast<StructType>(CurTy)) {
Chris Lattner1663cca2007-04-24 05:48:56 +00001309 for (unsigned i = 0; i != Size; ++i)
Chris Lattnere14cb882007-05-04 19:11:41 +00001310 Elts.push_back(ValueList.getConstantFwdRef(Record[i],
Chris Lattner1663cca2007-04-24 05:48:56 +00001311 STy->getElementType(i)));
Owen Anderson45308b52009-07-27 22:29:26 +00001312 V = ConstantStruct::get(STy, Elts);
Chris Lattner229907c2011-07-18 04:54:35 +00001313 } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) {
1314 Type *EltTy = ATy->getElementType();
Chris Lattner1663cca2007-04-24 05:48:56 +00001315 for (unsigned i = 0; i != Size; ++i)
Chris Lattnere14cb882007-05-04 19:11:41 +00001316 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
Owen Andersonc2c79322009-07-28 18:32:17 +00001317 V = ConstantArray::get(ATy, Elts);
Chris Lattner229907c2011-07-18 04:54:35 +00001318 } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) {
1319 Type *EltTy = VTy->getElementType();
Chris Lattner1663cca2007-04-24 05:48:56 +00001320 for (unsigned i = 0; i != Size; ++i)
Chris Lattnere14cb882007-05-04 19:11:41 +00001321 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
Owen Anderson4aa32952009-07-28 21:19:26 +00001322 V = ConstantVector::get(Elts);
Chris Lattner1663cca2007-04-24 05:48:56 +00001323 } else {
Owen Andersonb292b8c2009-07-30 23:03:37 +00001324 V = UndefValue::get(CurTy);
Chris Lattner1663cca2007-04-24 05:48:56 +00001325 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001326 break;
1327 }
Chris Lattnerbb8278a2012-02-05 02:41:35 +00001328 case bitc::CST_CODE_STRING: // STRING: [values]
Chris Lattnerf25f7102007-05-06 00:53:07 +00001329 case bitc::CST_CODE_CSTRING: { // CSTRING: [values]
1330 if (Record.empty())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001331 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001332
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001333 SmallString<16> Elts(Record.begin(), Record.end());
Chris Lattnerbb8278a2012-02-05 02:41:35 +00001334 V = ConstantDataArray::getString(Context, Elts,
1335 BitCode == bitc::CST_CODE_CSTRING);
Chris Lattnerf25f7102007-05-06 00:53:07 +00001336 break;
1337 }
Chris Lattner372dd1e2012-01-30 00:51:16 +00001338 case bitc::CST_CODE_DATA: {// DATA: [n x value]
1339 if (Record.empty())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001340 return Error(BitcodeError::InvalidRecord);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001341
Chris Lattner372dd1e2012-01-30 00:51:16 +00001342 Type *EltTy = cast<SequentialType>(CurTy)->getElementType();
1343 unsigned Size = Record.size();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001344
Chris Lattner372dd1e2012-01-30 00:51:16 +00001345 if (EltTy->isIntegerTy(8)) {
1346 SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end());
1347 if (isa<VectorType>(CurTy))
1348 V = ConstantDataVector::get(Context, Elts);
1349 else
1350 V = ConstantDataArray::get(Context, Elts);
1351 } else if (EltTy->isIntegerTy(16)) {
1352 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
1353 if (isa<VectorType>(CurTy))
1354 V = ConstantDataVector::get(Context, Elts);
1355 else
1356 V = ConstantDataArray::get(Context, Elts);
1357 } else if (EltTy->isIntegerTy(32)) {
1358 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
1359 if (isa<VectorType>(CurTy))
1360 V = ConstantDataVector::get(Context, Elts);
1361 else
1362 V = ConstantDataArray::get(Context, Elts);
1363 } else if (EltTy->isIntegerTy(64)) {
1364 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
1365 if (isa<VectorType>(CurTy))
1366 V = ConstantDataVector::get(Context, Elts);
1367 else
1368 V = ConstantDataArray::get(Context, Elts);
1369 } else if (EltTy->isFloatTy()) {
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001370 SmallVector<float, 16> Elts(Size);
1371 std::transform(Record.begin(), Record.end(), Elts.begin(), BitsToFloat);
Chris Lattner372dd1e2012-01-30 00:51:16 +00001372 if (isa<VectorType>(CurTy))
1373 V = ConstantDataVector::get(Context, Elts);
1374 else
1375 V = ConstantDataArray::get(Context, Elts);
1376 } else if (EltTy->isDoubleTy()) {
Benjamin Kramer9704ed02012-05-28 14:10:31 +00001377 SmallVector<double, 16> Elts(Size);
1378 std::transform(Record.begin(), Record.end(), Elts.begin(),
1379 BitsToDouble);
Chris Lattner372dd1e2012-01-30 00:51:16 +00001380 if (isa<VectorType>(CurTy))
1381 V = ConstantDataVector::get(Context, Elts);
1382 else
1383 V = ConstantDataArray::get(Context, Elts);
1384 } else {
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001385 return Error(BitcodeError::InvalidTypeForValue);
Chris Lattner372dd1e2012-01-30 00:51:16 +00001386 }
1387 break;
1388 }
1389
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001390 case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval]
Rafael Espindola48da4f42013-11-04 16:16:24 +00001391 if (Record.size() < 3)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001392 return Error(BitcodeError::InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001393 int Opc = GetDecodedBinaryOpcode(Record[0], CurTy);
Chris Lattner890683d2007-04-24 18:15:21 +00001394 if (Opc < 0) {
Owen Andersonb292b8c2009-07-30 23:03:37 +00001395 V = UndefValue::get(CurTy); // Unknown binop.
Chris Lattner890683d2007-04-24 18:15:21 +00001396 } else {
1397 Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy);
1398 Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy);
Dan Gohman1b849082009-09-07 23:54:19 +00001399 unsigned Flags = 0;
1400 if (Record.size() >= 4) {
1401 if (Opc == Instruction::Add ||
1402 Opc == Instruction::Sub ||
Chris Lattnera676c0f2011-02-07 16:40:21 +00001403 Opc == Instruction::Mul ||
1404 Opc == Instruction::Shl) {
Dan Gohman1b849082009-09-07 23:54:19 +00001405 if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP))
1406 Flags |= OverflowingBinaryOperator::NoSignedWrap;
1407 if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
1408 Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
Chris Lattner35315d02011-02-06 21:44:57 +00001409 } else if (Opc == Instruction::SDiv ||
Chris Lattnera676c0f2011-02-07 16:40:21 +00001410 Opc == Instruction::UDiv ||
1411 Opc == Instruction::LShr ||
1412 Opc == Instruction::AShr) {
Chris Lattner35315d02011-02-06 21:44:57 +00001413 if (Record[3] & (1 << bitc::PEO_EXACT))
Dan Gohman1b849082009-09-07 23:54:19 +00001414 Flags |= SDivOperator::IsExact;
1415 }
1416 }
1417 V = ConstantExpr::get(Opc, LHS, RHS, Flags);
Chris Lattner890683d2007-04-24 18:15:21 +00001418 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001419 break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001420 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001421 case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval]
Rafael Espindola48da4f42013-11-04 16:16:24 +00001422 if (Record.size() < 3)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001423 return Error(BitcodeError::InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001424 int Opc = GetDecodedCastOpcode(Record[0]);
Chris Lattner890683d2007-04-24 18:15:21 +00001425 if (Opc < 0) {
Owen Andersonb292b8c2009-07-30 23:03:37 +00001426 V = UndefValue::get(CurTy); // Unknown cast.
Chris Lattner890683d2007-04-24 18:15:21 +00001427 } else {
Chris Lattner229907c2011-07-18 04:54:35 +00001428 Type *OpTy = getTypeByID(Record[1]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001429 if (!OpTy)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001430 return Error(BitcodeError::InvalidRecord);
Chris Lattner890683d2007-04-24 18:15:21 +00001431 Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001432 V = UpgradeBitCastExpr(Opc, Op, CurTy);
1433 if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy);
Chris Lattner890683d2007-04-24 18:15:21 +00001434 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001435 break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001436 }
Dan Gohman1639c392009-07-27 21:53:46 +00001437 case bitc::CST_CODE_CE_INBOUNDS_GEP:
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001438 case bitc::CST_CODE_CE_GEP: { // CE_GEP: [n x operands]
Rafael Espindola48da4f42013-11-04 16:16:24 +00001439 if (Record.size() & 1)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001440 return Error(BitcodeError::InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001441 SmallVector<Constant*, 16> Elts;
Chris Lattnere14cb882007-05-04 19:11:41 +00001442 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
Chris Lattner229907c2011-07-18 04:54:35 +00001443 Type *ElTy = getTypeByID(Record[i]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001444 if (!ElTy)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001445 return Error(BitcodeError::InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001446 Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], ElTy));
1447 }
Jay Foaded8db7d2011-07-21 14:31:17 +00001448 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
Jay Foad2f5fc8c2011-07-21 15:15:37 +00001449 V = ConstantExpr::getGetElementPtr(Elts[0], Indices,
1450 BitCode ==
1451 bitc::CST_CODE_CE_INBOUNDS_GEP);
Chris Lattner890683d2007-04-24 18:15:21 +00001452 break;
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001453 }
Joe Abbey1a6e7702013-09-12 22:02:31 +00001454 case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#]
Rafael Espindola48da4f42013-11-04 16:16:24 +00001455 if (Record.size() < 3)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001456 return Error(BitcodeError::InvalidRecord);
Joe Abbey1a6e7702013-09-12 22:02:31 +00001457
1458 Type *SelectorTy = Type::getInt1Ty(Context);
1459
1460 // If CurTy is a vector of length n, then Record[0] must be a <n x i1>
1461 // vector. Otherwise, it must be a single bit.
1462 if (VectorType *VTy = dyn_cast<VectorType>(CurTy))
1463 SelectorTy = VectorType::get(Type::getInt1Ty(Context),
1464 VTy->getNumElements());
1465
1466 V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0],
1467 SelectorTy),
1468 ValueList.getConstantFwdRef(Record[1],CurTy),
1469 ValueList.getConstantFwdRef(Record[2],CurTy));
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001470 break;
Joe Abbey1a6e7702013-09-12 22:02:31 +00001471 }
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001472 case bitc::CST_CODE_CE_EXTRACTELT
1473 : { // CE_EXTRACTELT: [opty, opval, opty, opval]
Rafael Espindola48da4f42013-11-04 16:16:24 +00001474 if (Record.size() < 3)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001475 return Error(BitcodeError::InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00001476 VectorType *OpTy =
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001477 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
Craig Topper2617dcc2014-04-15 06:32:26 +00001478 if (!OpTy)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001479 return Error(BitcodeError::InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001480 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001481 Constant *Op1 = nullptr;
1482 if (Record.size() == 4) {
1483 Type *IdxTy = getTypeByID(Record[2]);
1484 if (!IdxTy)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001485 return Error(BitcodeError::InvalidRecord);
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001486 Op1 = ValueList.getConstantFwdRef(Record[3], IdxTy);
1487 } else // TODO: Remove with llvm 4.0
1488 Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
1489 if (!Op1)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001490 return Error(BitcodeError::InvalidRecord);
Owen Anderson487375e2009-07-29 18:55:55 +00001491 V = ConstantExpr::getExtractElement(Op0, Op1);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001492 break;
1493 }
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001494 case bitc::CST_CODE_CE_INSERTELT
1495 : { // CE_INSERTELT: [opval, opval, opty, opval]
Chris Lattner229907c2011-07-18 04:54:35 +00001496 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
Craig Topper2617dcc2014-04-15 06:32:26 +00001497 if (Record.size() < 3 || !OpTy)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001498 return Error(BitcodeError::InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001499 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
1500 Constant *Op1 = ValueList.getConstantFwdRef(Record[1],
1501 OpTy->getElementType());
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001502 Constant *Op2 = nullptr;
1503 if (Record.size() == 4) {
1504 Type *IdxTy = getTypeByID(Record[2]);
1505 if (!IdxTy)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001506 return Error(BitcodeError::InvalidRecord);
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001507 Op2 = ValueList.getConstantFwdRef(Record[3], IdxTy);
1508 } else // TODO: Remove with llvm 4.0
1509 Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
1510 if (!Op2)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001511 return Error(BitcodeError::InvalidRecord);
Owen Anderson487375e2009-07-29 18:55:55 +00001512 V = ConstantExpr::getInsertElement(Op0, Op1, Op2);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001513 break;
1514 }
1515 case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval]
Chris Lattner229907c2011-07-18 04:54:35 +00001516 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
Craig Topper2617dcc2014-04-15 06:32:26 +00001517 if (Record.size() < 3 || !OpTy)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001518 return Error(BitcodeError::InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001519 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
1520 Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy);
Chris Lattner229907c2011-07-18 04:54:35 +00001521 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
Owen Andersone9f98042009-07-07 20:18:58 +00001522 OpTy->getNumElements());
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001523 Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy);
Owen Anderson487375e2009-07-29 18:55:55 +00001524 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001525 break;
1526 }
Nate Begeman94aa38d2009-02-12 21:28:33 +00001527 case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval]
Chris Lattner229907c2011-07-18 04:54:35 +00001528 VectorType *RTy = dyn_cast<VectorType>(CurTy);
1529 VectorType *OpTy =
Duncan Sands89d412a2010-10-28 15:47:26 +00001530 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
Craig Topper2617dcc2014-04-15 06:32:26 +00001531 if (Record.size() < 4 || !RTy || !OpTy)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001532 return Error(BitcodeError::InvalidRecord);
Nate Begeman94aa38d2009-02-12 21:28:33 +00001533 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
1534 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
Chris Lattner229907c2011-07-18 04:54:35 +00001535 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
Owen Andersone9f98042009-07-07 20:18:58 +00001536 RTy->getNumElements());
Nate Begeman94aa38d2009-02-12 21:28:33 +00001537 Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy);
Owen Anderson487375e2009-07-29 18:55:55 +00001538 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Nate Begeman94aa38d2009-02-12 21:28:33 +00001539 break;
1540 }
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001541 case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred]
Rafael Espindola48da4f42013-11-04 16:16:24 +00001542 if (Record.size() < 4)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001543 return Error(BitcodeError::InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00001544 Type *OpTy = getTypeByID(Record[0]);
Craig Topper2617dcc2014-04-15 06:32:26 +00001545 if (!OpTy)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001546 return Error(BitcodeError::InvalidRecord);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001547 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
1548 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
1549
Duncan Sands9dff9be2010-02-15 16:12:20 +00001550 if (OpTy->isFPOrFPVectorTy())
Owen Anderson487375e2009-07-29 18:55:55 +00001551 V = ConstantExpr::getFCmp(Record[3], Op0, Op1);
Nate Begemand2195702008-05-12 19:01:56 +00001552 else
Owen Anderson487375e2009-07-29 18:55:55 +00001553 V = ConstantExpr::getICmp(Record[3], Op0, Op1);
Chris Lattner1e16bcf72007-04-24 07:07:11 +00001554 break;
Chris Lattner1663cca2007-04-24 05:48:56 +00001555 }
Chad Rosierd8c76102012-09-05 19:00:49 +00001556 // This maintains backward compatibility, pre-asm dialect keywords.
Chad Rosier5895eda2012-09-05 06:28:52 +00001557 // FIXME: Remove with the 4.0 release.
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001558 case bitc::CST_CODE_INLINEASM_OLD: {
Rafael Espindola48da4f42013-11-04 16:16:24 +00001559 if (Record.size() < 2)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001560 return Error(BitcodeError::InvalidRecord);
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001561 std::string AsmStr, ConstrStr;
Dale Johannesenfd04c742009-10-13 20:46:56 +00001562 bool HasSideEffects = Record[0] & 1;
Dale Johannesen1cfb9582009-10-21 23:28:00 +00001563 bool IsAlignStack = Record[0] >> 1;
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001564 unsigned AsmStrSize = Record[1];
1565 if (2+AsmStrSize >= Record.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001566 return Error(BitcodeError::InvalidRecord);
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001567 unsigned ConstStrSize = Record[2+AsmStrSize];
1568 if (3+AsmStrSize+ConstStrSize > Record.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001569 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001570
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001571 for (unsigned i = 0; i != AsmStrSize; ++i)
1572 AsmStr += (char)Record[2+i];
1573 for (unsigned i = 0; i != ConstStrSize; ++i)
1574 ConstrStr += (char)Record[3+AsmStrSize+i];
Chris Lattner229907c2011-07-18 04:54:35 +00001575 PointerType *PTy = cast<PointerType>(CurTy);
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001576 V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
Dale Johannesen1cfb9582009-10-21 23:28:00 +00001577 AsmStr, ConstrStr, HasSideEffects, IsAlignStack);
Chris Lattneraf8fffc2007-05-06 01:58:20 +00001578 break;
1579 }
Chad Rosierd8c76102012-09-05 19:00:49 +00001580 // This version adds support for the asm dialect keywords (e.g.,
1581 // inteldialect).
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001582 case bitc::CST_CODE_INLINEASM: {
Rafael Espindola48da4f42013-11-04 16:16:24 +00001583 if (Record.size() < 2)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001584 return Error(BitcodeError::InvalidRecord);
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001585 std::string AsmStr, ConstrStr;
1586 bool HasSideEffects = Record[0] & 1;
1587 bool IsAlignStack = (Record[0] >> 1) & 1;
1588 unsigned AsmDialect = Record[0] >> 2;
1589 unsigned AsmStrSize = Record[1];
1590 if (2+AsmStrSize >= Record.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001591 return Error(BitcodeError::InvalidRecord);
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001592 unsigned ConstStrSize = Record[2+AsmStrSize];
1593 if (3+AsmStrSize+ConstStrSize > Record.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001594 return Error(BitcodeError::InvalidRecord);
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001595
1596 for (unsigned i = 0; i != AsmStrSize; ++i)
1597 AsmStr += (char)Record[2+i];
1598 for (unsigned i = 0; i != ConstStrSize; ++i)
1599 ConstrStr += (char)Record[3+AsmStrSize+i];
1600 PointerType *PTy = cast<PointerType>(CurTy);
1601 V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
1602 AsmStr, ConstrStr, HasSideEffects, IsAlignStack,
Chad Rosierd8c76102012-09-05 19:00:49 +00001603 InlineAsm::AsmDialect(AsmDialect));
Chad Rosier18fcdcf2012-09-05 00:56:20 +00001604 break;
1605 }
Chris Lattner5956dc82009-10-28 05:53:48 +00001606 case bitc::CST_CODE_BLOCKADDRESS:{
Rafael Espindola48da4f42013-11-04 16:16:24 +00001607 if (Record.size() < 3)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001608 return Error(BitcodeError::InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00001609 Type *FnTy = getTypeByID(Record[0]);
Craig Topper2617dcc2014-04-15 06:32:26 +00001610 if (!FnTy)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001611 return Error(BitcodeError::InvalidRecord);
Chris Lattner5956dc82009-10-28 05:53:48 +00001612 Function *Fn =
1613 dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy));
Craig Topper2617dcc2014-04-15 06:32:26 +00001614 if (!Fn)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001615 return Error(BitcodeError::InvalidRecord);
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00001616
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00001617 // Don't let Fn get dematerialized.
1618 BlockAddressesTaken.insert(Fn);
1619
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00001620 // If the function is already parsed we can insert the block address right
1621 // away.
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00001622 BasicBlock *BB;
1623 unsigned BBID = Record[2];
1624 if (!BBID)
1625 // Invalid reference to entry block.
1626 return Error(BitcodeError::InvalidID);
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00001627 if (!Fn->empty()) {
1628 Function::iterator BBI = Fn->begin(), BBE = Fn->end();
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00001629 for (size_t I = 0, E = BBID; I != E; ++I) {
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00001630 if (BBI == BBE)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001631 return Error(BitcodeError::InvalidID);
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00001632 ++BBI;
1633 }
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00001634 BB = BBI;
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00001635 } else {
1636 // Otherwise insert a placeholder and remember it so it can be inserted
1637 // when the function is parsed.
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00001638 BB = BasicBlock::Create(Context);
Duncan P. N. Exon Smith5a511b52014-08-05 17:49:48 +00001639 auto &FwdBBs = BasicBlockFwdRefs[Fn];
1640 if (FwdBBs.empty())
1641 BasicBlockFwdRefQueue.push_back(Fn);
1642 FwdBBs.emplace_back(BBID, BB);
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00001643 }
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00001644 V = BlockAddress::get(Fn, BB);
Chris Lattner5956dc82009-10-28 05:53:48 +00001645 break;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001646 }
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001647 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001648
Chris Lattner83930552007-05-01 07:01:57 +00001649 ValueList.AssignValue(V, NextCstNo);
Chris Lattner1663cca2007-04-24 05:48:56 +00001650 ++NextCstNo;
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001651 }
1652}
Chris Lattner1314b992007-04-22 06:23:29 +00001653
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001654std::error_code BitcodeReader::ParseUseLists() {
Chad Rosierca2567b2011-12-07 21:44:12 +00001655 if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001656 return Error(BitcodeError::InvalidRecord);
Chad Rosierca2567b2011-12-07 21:44:12 +00001657
Chad Rosierca2567b2011-12-07 21:44:12 +00001658 // Read all the records.
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00001659 SmallVector<uint64_t, 64> Record;
Chad Rosierca2567b2011-12-07 21:44:12 +00001660 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00001661 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00001662
Chris Lattner27d38752013-01-20 02:13:19 +00001663 switch (Entry.Kind) {
1664 case BitstreamEntry::SubBlock: // Handled for us already.
1665 case BitstreamEntry::Error:
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001666 return Error(BitcodeError::MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00001667 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001668 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00001669 case BitstreamEntry::Record:
1670 // The interesting case.
1671 break;
Chad Rosierca2567b2011-12-07 21:44:12 +00001672 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001673
Chad Rosierca2567b2011-12-07 21:44:12 +00001674 // Read a use list record.
1675 Record.clear();
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00001676 bool IsBB = false;
Chris Lattner27d38752013-01-20 02:13:19 +00001677 switch (Stream.readRecord(Entry.ID, Record)) {
Chad Rosierca2567b2011-12-07 21:44:12 +00001678 default: // Default behavior: unknown type.
1679 break;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00001680 case bitc::USELIST_CODE_BB:
1681 IsBB = true;
1682 // fallthrough
1683 case bitc::USELIST_CODE_DEFAULT: {
Chad Rosierca2567b2011-12-07 21:44:12 +00001684 unsigned RecordLength = Record.size();
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00001685 if (RecordLength < 3)
1686 // Records should have at least an ID and two indexes.
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001687 return Error(BitcodeError::InvalidRecord);
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00001688 unsigned ID = Record.back();
1689 Record.pop_back();
1690
1691 Value *V;
1692 if (IsBB) {
1693 assert(ID < FunctionBBs.size() && "Basic block not found");
1694 V = FunctionBBs[ID];
1695 } else
1696 V = ValueList[ID];
1697 unsigned NumUses = 0;
1698 SmallDenseMap<const Use *, unsigned, 16> Order;
1699 for (const Use &U : V->uses()) {
Duncan P. N. Exon Smith13183642014-08-16 01:54:34 +00001700 if (++NumUses > Record.size())
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00001701 break;
Duncan P. N. Exon Smith13183642014-08-16 01:54:34 +00001702 Order[&U] = Record[NumUses - 1];
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00001703 }
1704 if (Order.size() != Record.size() || NumUses > Record.size())
1705 // Mismatches can happen if the functions are being materialized lazily
1706 // (out-of-order), or a value has been upgraded.
1707 break;
1708
1709 V->sortUseList([&](const Use &L, const Use &R) {
1710 return Order.lookup(&L) < Order.lookup(&R);
1711 });
Chad Rosierca2567b2011-12-07 21:44:12 +00001712 break;
1713 }
1714 }
1715 }
1716}
1717
Chris Lattner85b7b402007-05-01 05:52:21 +00001718/// RememberAndSkipFunctionBody - When we see the block for a function body,
1719/// remember where it is and then skip it. This lets us lazily deserialize the
1720/// functions.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001721std::error_code BitcodeReader::RememberAndSkipFunctionBody() {
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001722 // Get the function we are talking about.
1723 if (FunctionsWithBodies.empty())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001724 return Error(BitcodeError::InsufficientFunctionProtos);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001725
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001726 Function *Fn = FunctionsWithBodies.back();
1727 FunctionsWithBodies.pop_back();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001728
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001729 // Save the current stream state.
1730 uint64_t CurBit = Stream.GetCurrentBitNo();
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001731 DeferredFunctionInfo[Fn] = CurBit;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001732
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001733 // Skip over the function block for now.
1734 if (Stream.SkipBlock())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001735 return Error(BitcodeError::InvalidRecord);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001736 return std::error_code();
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001737}
1738
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001739std::error_code BitcodeReader::GlobalCleanup() {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001740 // Patch the initializers for globals and aliases up.
1741 ResolveGlobalAndAliasInits();
1742 if (!GlobalInits.empty() || !AliasInits.empty())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001743 return Error(BitcodeError::MalformedGlobalInitializerSet);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001744
1745 // Look for intrinsic functions which need to be upgraded at some point
1746 for (Module::iterator FI = TheModule->begin(), FE = TheModule->end();
1747 FI != FE; ++FI) {
1748 Function *NewFn;
1749 if (UpgradeIntrinsicFunction(FI, NewFn))
1750 UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn));
1751 }
1752
1753 // Look for global variables which need to be renamed.
1754 for (Module::global_iterator
1755 GI = TheModule->global_begin(), GE = TheModule->global_end();
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001756 GI != GE;) {
1757 GlobalVariable *GV = GI++;
1758 UpgradeGlobalVariable(GV);
1759 }
1760
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001761 // Force deallocation of memory for these vectors to favor the client that
1762 // want lazy deserialization.
1763 std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits);
1764 std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001765 return std::error_code();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001766}
1767
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001768std::error_code BitcodeReader::ParseModule(bool Resume) {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001769 if (Resume)
1770 Stream.JumpToBit(NextUnreadBit);
1771 else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001772 return Error(BitcodeError::InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001773
Chris Lattner1314b992007-04-22 06:23:29 +00001774 SmallVector<uint64_t, 64> Record;
1775 std::vector<std::string> SectionTable;
Gordon Henriksend930f912008-08-17 18:44:35 +00001776 std::vector<std::string> GCTable;
Chris Lattner1314b992007-04-22 06:23:29 +00001777
1778 // Read all the records for this module.
Chris Lattner27d38752013-01-20 02:13:19 +00001779 while (1) {
1780 BitstreamEntry Entry = Stream.advance();
Joe Abbey97b7a172013-02-06 22:14:06 +00001781
Chris Lattner27d38752013-01-20 02:13:19 +00001782 switch (Entry.Kind) {
1783 case BitstreamEntry::Error:
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001784 return Error(BitcodeError::MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00001785 case BitstreamEntry::EndBlock:
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001786 return GlobalCleanup();
Joe Abbey97b7a172013-02-06 22:14:06 +00001787
Chris Lattner27d38752013-01-20 02:13:19 +00001788 case BitstreamEntry::SubBlock:
1789 switch (Entry.ID) {
Chris Lattner1314b992007-04-22 06:23:29 +00001790 default: // Skip unknown content.
1791 if (Stream.SkipBlock())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001792 return Error(BitcodeError::InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001793 break;
Chris Lattner6eeea5d2007-05-05 18:57:30 +00001794 case bitc::BLOCKINFO_BLOCK_ID:
1795 if (Stream.ReadBlockInfoBlock())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001796 return Error(BitcodeError::MalformedBlock);
Chris Lattner6eeea5d2007-05-05 18:57:30 +00001797 break;
Chris Lattnerfee5a372007-05-04 03:30:17 +00001798 case bitc::PARAMATTR_BLOCK_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001799 if (std::error_code EC = ParseAttributeBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001800 return EC;
Chris Lattnerfee5a372007-05-04 03:30:17 +00001801 break;
Bill Wendlingba629332013-02-10 23:24:25 +00001802 case bitc::PARAMATTR_GROUP_BLOCK_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001803 if (std::error_code EC = ParseAttributeGroupBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001804 return EC;
Bill Wendlingba629332013-02-10 23:24:25 +00001805 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001806 case bitc::TYPE_BLOCK_ID_NEW:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001807 if (std::error_code EC = ParseTypeTable())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001808 return EC;
Chris Lattner1314b992007-04-22 06:23:29 +00001809 break;
Chris Lattnerccaa4482007-04-23 21:26:05 +00001810 case bitc::VALUE_SYMTAB_BLOCK_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001811 if (std::error_code EC = ParseValueSymbolTable())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001812 return EC;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001813 SeenValueSymbolTable = true;
Chris Lattnerccaa4482007-04-23 21:26:05 +00001814 break;
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001815 case bitc::CONSTANTS_BLOCK_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001816 if (std::error_code EC = ParseConstants())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001817 return EC;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001818 if (std::error_code EC = ResolveGlobalAndAliasInits())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001819 return EC;
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001820 break;
Devang Patel7428d8a2009-07-22 17:43:22 +00001821 case bitc::METADATA_BLOCK_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001822 if (std::error_code EC = ParseMetadata())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001823 return EC;
Devang Patel7428d8a2009-07-22 17:43:22 +00001824 break;
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001825 case bitc::FUNCTION_BLOCK_ID:
1826 // If this is the first function body we've seen, reverse the
1827 // FunctionsWithBodies list.
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001828 if (!SeenFirstFunctionBody) {
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001829 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001830 if (std::error_code EC = GlobalCleanup())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001831 return EC;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001832 SeenFirstFunctionBody = true;
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001833 }
Joe Abbey97b7a172013-02-06 22:14:06 +00001834
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001835 if (std::error_code EC = RememberAndSkipFunctionBody())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001836 return EC;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001837 // For streaming bitcode, suspend parsing when we reach the function
1838 // bodies. Subsequent materialization calls will resume it when
1839 // necessary. For streaming, the function bodies must be at the end of
1840 // the bitcode. If the bitcode file is old, the symbol table will be
1841 // at the end instead and will not have been seen yet. In this case,
1842 // just finish the parse now.
1843 if (LazyStreamer && SeenValueSymbolTable) {
1844 NextUnreadBit = Stream.GetCurrentBitNo();
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001845 return std::error_code();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001846 }
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001847 break;
Chad Rosierca2567b2011-12-07 21:44:12 +00001848 case bitc::USELIST_BLOCK_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001849 if (std::error_code EC = ParseUseLists())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001850 return EC;
Chad Rosierca2567b2011-12-07 21:44:12 +00001851 break;
Chris Lattner1314b992007-04-22 06:23:29 +00001852 }
1853 continue;
Joe Abbey97b7a172013-02-06 22:14:06 +00001854
Chris Lattner27d38752013-01-20 02:13:19 +00001855 case BitstreamEntry::Record:
1856 // The interesting case.
1857 break;
Chris Lattner1314b992007-04-22 06:23:29 +00001858 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001859
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001860
Chris Lattner1314b992007-04-22 06:23:29 +00001861 // Read a record.
Chris Lattner27d38752013-01-20 02:13:19 +00001862 switch (Stream.readRecord(Entry.ID, Record)) {
Chris Lattner1314b992007-04-22 06:23:29 +00001863 default: break; // Default behavior, ignore unknown content.
Jan Wen Voungafaced02012-10-11 20:20:40 +00001864 case bitc::MODULE_CODE_VERSION: { // VERSION: [version#]
Chris Lattner1314b992007-04-22 06:23:29 +00001865 if (Record.size() < 1)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001866 return Error(BitcodeError::InvalidRecord);
Jan Wen Voungafaced02012-10-11 20:20:40 +00001867 // Only version #0 and #1 are supported so far.
1868 unsigned module_version = Record[0];
1869 switch (module_version) {
Rafael Espindola48da4f42013-11-04 16:16:24 +00001870 default:
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001871 return Error(BitcodeError::InvalidValue);
Jan Wen Voungafaced02012-10-11 20:20:40 +00001872 case 0:
1873 UseRelativeIDs = false;
1874 break;
1875 case 1:
1876 UseRelativeIDs = true;
1877 break;
1878 }
Chris Lattner1314b992007-04-22 06:23:29 +00001879 break;
Jan Wen Voungafaced02012-10-11 20:20:40 +00001880 }
Chris Lattnere14cb882007-05-04 19:11:41 +00001881 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00001882 std::string S;
1883 if (ConvertToString(Record, 0, S))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001884 return Error(BitcodeError::InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001885 TheModule->setTargetTriple(S);
1886 break;
1887 }
Chris Lattnere14cb882007-05-04 19:11:41 +00001888 case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00001889 std::string S;
1890 if (ConvertToString(Record, 0, S))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001891 return Error(BitcodeError::InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001892 TheModule->setDataLayout(S);
1893 break;
1894 }
Chris Lattnere14cb882007-05-04 19:11:41 +00001895 case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00001896 std::string S;
1897 if (ConvertToString(Record, 0, S))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001898 return Error(BitcodeError::InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001899 TheModule->setModuleInlineAsm(S);
1900 break;
1901 }
Bill Wendling706d3d62012-11-28 08:41:48 +00001902 case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N]
1903 // FIXME: Remove in 4.0.
1904 std::string S;
1905 if (ConvertToString(Record, 0, S))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001906 return Error(BitcodeError::InvalidRecord);
Bill Wendling706d3d62012-11-28 08:41:48 +00001907 // Ignore value.
1908 break;
1909 }
Chris Lattnere14cb882007-05-04 19:11:41 +00001910 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00001911 std::string S;
1912 if (ConvertToString(Record, 0, S))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001913 return Error(BitcodeError::InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001914 SectionTable.push_back(S);
1915 break;
1916 }
Gordon Henriksend930f912008-08-17 18:44:35 +00001917 case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N]
Gordon Henriksen71183b62007-12-10 03:18:06 +00001918 std::string S;
1919 if (ConvertToString(Record, 0, S))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001920 return Error(BitcodeError::InvalidRecord);
Gordon Henriksend930f912008-08-17 18:44:35 +00001921 GCTable.push_back(S);
Gordon Henriksen71183b62007-12-10 03:18:06 +00001922 break;
1923 }
David Majnemerdad0a642014-06-27 18:19:56 +00001924 case bitc::MODULE_CODE_COMDAT: { // COMDAT: [selection_kind, name]
1925 if (Record.size() < 2)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001926 return Error(BitcodeError::InvalidRecord);
David Majnemerdad0a642014-06-27 18:19:56 +00001927 Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]);
1928 unsigned ComdatNameSize = Record[1];
1929 std::string ComdatName;
1930 ComdatName.reserve(ComdatNameSize);
1931 for (unsigned i = 0; i != ComdatNameSize; ++i)
1932 ComdatName += (char)Record[2 + i];
1933 Comdat *C = TheModule->getOrInsertComdat(ComdatName);
1934 C->setSelectionKind(SK);
1935 ComdatList.push_back(C);
1936 break;
1937 }
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001938 // GLOBALVAR: [pointer type, isconst, initid,
Rafael Espindola45e6c192011-01-08 16:42:36 +00001939 // linkage, alignment, section, visibility, threadlocal,
Nico Rieck7157bb72014-01-14 15:22:47 +00001940 // unnamed_addr, dllstorageclass]
Chris Lattner1314b992007-04-22 06:23:29 +00001941 case bitc::MODULE_CODE_GLOBALVAR: {
Chris Lattner4b00d922007-04-23 16:04:05 +00001942 if (Record.size() < 6)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001943 return Error(BitcodeError::InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00001944 Type *Ty = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001945 if (!Ty)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001946 return Error(BitcodeError::InvalidRecord);
Duncan Sands19d0b472010-02-16 11:11:14 +00001947 if (!Ty->isPointerTy())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001948 return Error(BitcodeError::InvalidTypeForValue);
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001949 unsigned AddressSpace = cast<PointerType>(Ty)->getAddressSpace();
Chris Lattner1314b992007-04-22 06:23:29 +00001950 Ty = cast<PointerType>(Ty)->getElementType();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001951
Chris Lattner1314b992007-04-22 06:23:29 +00001952 bool isConstant = Record[1];
1953 GlobalValue::LinkageTypes Linkage = GetDecodedLinkage(Record[3]);
1954 unsigned Alignment = (1 << Record[4]) >> 1;
1955 std::string Section;
1956 if (Record[5]) {
1957 if (Record[5]-1 >= SectionTable.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001958 return Error(BitcodeError::InvalidID);
Chris Lattner1314b992007-04-22 06:23:29 +00001959 Section = SectionTable[Record[5]-1];
1960 }
Chris Lattner4b00d922007-04-23 16:04:05 +00001961 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00001962 // Local linkage must have default visibility.
1963 if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage))
1964 // FIXME: Change to an error if non-default in 4.0.
Chris Lattner53862f72007-05-06 19:27:46 +00001965 Visibility = GetDecodedVisibility(Record[6]);
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001966
1967 GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal;
Chris Lattner53862f72007-05-06 19:27:46 +00001968 if (Record.size() > 7)
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001969 TLM = GetDecodedThreadLocalMode(Record[7]);
Chris Lattner1314b992007-04-22 06:23:29 +00001970
Rafael Espindola45e6c192011-01-08 16:42:36 +00001971 bool UnnamedAddr = false;
1972 if (Record.size() > 8)
1973 UnnamedAddr = Record[8];
1974
Michael Gottesman27e7ef32013-02-05 05:57:38 +00001975 bool ExternallyInitialized = false;
1976 if (Record.size() > 9)
1977 ExternallyInitialized = Record[9];
1978
Chris Lattner1314b992007-04-22 06:23:29 +00001979 GlobalVariable *NewGV =
Craig Topper2617dcc2014-04-15 06:32:26 +00001980 new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, "", nullptr,
Michael Gottesman27e7ef32013-02-05 05:57:38 +00001981 TLM, AddressSpace, ExternallyInitialized);
Chris Lattner1314b992007-04-22 06:23:29 +00001982 NewGV->setAlignment(Alignment);
1983 if (!Section.empty())
1984 NewGV->setSection(Section);
1985 NewGV->setVisibility(Visibility);
Rafael Espindola45e6c192011-01-08 16:42:36 +00001986 NewGV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001987
Nico Rieck7157bb72014-01-14 15:22:47 +00001988 if (Record.size() > 10)
1989 NewGV->setDLLStorageClass(GetDecodedDLLStorageClass(Record[10]));
1990 else
1991 UpgradeDLLImportExportLinkage(NewGV, Record[3]);
1992
Chris Lattnerccaa4482007-04-23 21:26:05 +00001993 ValueList.push_back(NewGV);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001994
Chris Lattner47d131b2007-04-24 00:18:21 +00001995 // Remember which value to use for the global initializer.
1996 if (unsigned InitID = Record[2])
1997 GlobalInits.push_back(std::make_pair(NewGV, InitID-1));
David Majnemerdad0a642014-06-27 18:19:56 +00001998
1999 if (Record.size() > 11)
2000 if (unsigned ComdatID = Record[11]) {
2001 assert(ComdatID <= ComdatList.size());
2002 NewGV->setComdat(ComdatList[ComdatID - 1]);
2003 }
Chris Lattner1314b992007-04-22 06:23:29 +00002004 break;
2005 }
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002006 // FUNCTION: [type, callingconv, isproto, linkage, paramattr,
Nico Rieck7157bb72014-01-14 15:22:47 +00002007 // alignment, section, visibility, gc, unnamed_addr,
2008 // dllstorageclass]
Chris Lattner1314b992007-04-22 06:23:29 +00002009 case bitc::MODULE_CODE_FUNCTION: {
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002010 if (Record.size() < 8)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002011 return Error(BitcodeError::InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00002012 Type *Ty = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00002013 if (!Ty)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002014 return Error(BitcodeError::InvalidRecord);
Duncan Sands19d0b472010-02-16 11:11:14 +00002015 if (!Ty->isPointerTy())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002016 return Error(BitcodeError::InvalidTypeForValue);
Chris Lattner229907c2011-07-18 04:54:35 +00002017 FunctionType *FTy =
Chris Lattner1314b992007-04-22 06:23:29 +00002018 dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType());
2019 if (!FTy)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002020 return Error(BitcodeError::InvalidTypeForValue);
Chris Lattner1314b992007-04-22 06:23:29 +00002021
Gabor Greife9ecc682008-04-06 20:25:17 +00002022 Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage,
2023 "", TheModule);
Chris Lattner1314b992007-04-22 06:23:29 +00002024
Sandeep Patel68c5f472009-09-02 08:44:58 +00002025 Func->setCallingConv(static_cast<CallingConv::ID>(Record[1]));
Chris Lattner51ffe7c2007-05-01 04:59:48 +00002026 bool isProto = Record[2];
Chris Lattner1314b992007-04-22 06:23:29 +00002027 Func->setLinkage(GetDecodedLinkage(Record[3]));
Devang Patel4c758ea2008-09-25 21:00:45 +00002028 Func->setAttributes(getAttributes(Record[4]));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002029
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002030 Func->setAlignment((1 << Record[5]) >> 1);
2031 if (Record[6]) {
2032 if (Record[6]-1 >= SectionTable.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002033 return Error(BitcodeError::InvalidID);
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002034 Func->setSection(SectionTable[Record[6]-1]);
Chris Lattner1314b992007-04-22 06:23:29 +00002035 }
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00002036 // Local linkage must have default visibility.
2037 if (!Func->hasLocalLinkage())
2038 // FIXME: Change to an error if non-default in 4.0.
2039 Func->setVisibility(GetDecodedVisibility(Record[7]));
Gordon Henriksen71183b62007-12-10 03:18:06 +00002040 if (Record.size() > 8 && Record[8]) {
Gordon Henriksend930f912008-08-17 18:44:35 +00002041 if (Record[8]-1 > GCTable.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002042 return Error(BitcodeError::InvalidID);
Gordon Henriksend930f912008-08-17 18:44:35 +00002043 Func->setGC(GCTable[Record[8]-1].c_str());
Gordon Henriksen71183b62007-12-10 03:18:06 +00002044 }
Rafael Espindola45e6c192011-01-08 16:42:36 +00002045 bool UnnamedAddr = false;
2046 if (Record.size() > 9)
2047 UnnamedAddr = Record[9];
2048 Func->setUnnamedAddr(UnnamedAddr);
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00002049 if (Record.size() > 10 && Record[10] != 0)
2050 FunctionPrefixes.push_back(std::make_pair(Func, Record[10]-1));
Nico Rieck7157bb72014-01-14 15:22:47 +00002051
2052 if (Record.size() > 11)
2053 Func->setDLLStorageClass(GetDecodedDLLStorageClass(Record[11]));
2054 else
2055 UpgradeDLLImportExportLinkage(Func, Record[3]);
2056
David Majnemerdad0a642014-06-27 18:19:56 +00002057 if (Record.size() > 12)
2058 if (unsigned ComdatID = Record[12]) {
2059 assert(ComdatID <= ComdatList.size());
2060 Func->setComdat(ComdatList[ComdatID - 1]);
2061 }
2062
Chris Lattnerccaa4482007-04-23 21:26:05 +00002063 ValueList.push_back(Func);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002064
Chris Lattner51ffe7c2007-05-01 04:59:48 +00002065 // If this is a function with a body, remember the prototype we are
2066 // creating now, so that we can match up the body with them later.
Derek Schuff8b2dcad2012-02-06 22:30:29 +00002067 if (!isProto) {
Chris Lattner51ffe7c2007-05-01 04:59:48 +00002068 FunctionsWithBodies.push_back(Func);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00002069 if (LazyStreamer) DeferredFunctionInfo[Func] = 0;
2070 }
Chris Lattner1314b992007-04-22 06:23:29 +00002071 break;
2072 }
Anton Korobeynikov2f22e3f2008-03-12 00:49:19 +00002073 // ALIAS: [alias type, aliasee val#, linkage]
Nico Rieck7157bb72014-01-14 15:22:47 +00002074 // ALIAS: [alias type, aliasee val#, linkage, visibility, dllstorageclass]
Chris Lattner831d4202007-04-26 03:27:58 +00002075 case bitc::MODULE_CODE_ALIAS: {
Chris Lattner44c17072007-04-26 02:46:40 +00002076 if (Record.size() < 3)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002077 return Error(BitcodeError::InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00002078 Type *Ty = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00002079 if (!Ty)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002080 return Error(BitcodeError::InvalidRecord);
Rafael Espindolaa8004452014-05-16 14:22:33 +00002081 auto *PTy = dyn_cast<PointerType>(Ty);
2082 if (!PTy)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002083 return Error(BitcodeError::InvalidTypeForValue);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002084
Rafael Espindolaa8004452014-05-16 14:22:33 +00002085 auto *NewGA =
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +00002086 GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
2087 GetDecodedLinkage(Record[2]), "", TheModule);
Anton Korobeynikov2f22e3f2008-03-12 00:49:19 +00002088 // Old bitcode files didn't have visibility field.
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00002089 // Local linkage must have default visibility.
2090 if (Record.size() > 3 && !NewGA->hasLocalLinkage())
2091 // FIXME: Change to an error if non-default in 4.0.
Anton Korobeynikov2f22e3f2008-03-12 00:49:19 +00002092 NewGA->setVisibility(GetDecodedVisibility(Record[3]));
Nico Rieck7157bb72014-01-14 15:22:47 +00002093 if (Record.size() > 4)
2094 NewGA->setDLLStorageClass(GetDecodedDLLStorageClass(Record[4]));
2095 else
2096 UpgradeDLLImportExportLinkage(NewGA, Record[2]);
Rafael Espindola59f7eba2014-05-28 18:15:43 +00002097 if (Record.size() > 5)
2098 NewGA->setThreadLocalMode(GetDecodedThreadLocalMode(Record[5]));
Rafael Espindola42a4c9f2014-06-06 01:20:28 +00002099 if (Record.size() > 6)
2100 NewGA->setUnnamedAddr(Record[6]);
Chris Lattner44c17072007-04-26 02:46:40 +00002101 ValueList.push_back(NewGA);
2102 AliasInits.push_back(std::make_pair(NewGA, Record[1]));
2103 break;
Chris Lattner1314b992007-04-22 06:23:29 +00002104 }
Chris Lattner831d4202007-04-26 03:27:58 +00002105 /// MODULE_CODE_PURGEVALS: [numvals]
2106 case bitc::MODULE_CODE_PURGEVALS:
2107 // Trim down the value list to the specified size.
2108 if (Record.size() < 1 || Record[0] > ValueList.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002109 return Error(BitcodeError::InvalidRecord);
Chris Lattner831d4202007-04-26 03:27:58 +00002110 ValueList.shrinkTo(Record[0]);
2111 break;
2112 }
Chris Lattner1314b992007-04-22 06:23:29 +00002113 Record.clear();
2114 }
Chris Lattner1314b992007-04-22 06:23:29 +00002115}
2116
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002117std::error_code BitcodeReader::ParseBitcodeInto(Module *M) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002118 TheModule = nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002119
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002120 if (std::error_code EC = InitStream())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002121 return EC;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002122
Chris Lattner1314b992007-04-22 06:23:29 +00002123 // Sniff for the signature.
2124 if (Stream.Read(8) != 'B' ||
2125 Stream.Read(8) != 'C' ||
2126 Stream.Read(4) != 0x0 ||
2127 Stream.Read(4) != 0xC ||
2128 Stream.Read(4) != 0xE ||
2129 Stream.Read(4) != 0xD)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002130 return Error(BitcodeError::InvalidBitcodeSignature);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002131
Chris Lattner1314b992007-04-22 06:23:29 +00002132 // We expect a number of well-defined blocks, though we don't necessarily
2133 // need to understand them all.
Chris Lattner27d38752013-01-20 02:13:19 +00002134 while (1) {
2135 if (Stream.AtEndOfStream())
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002136 return std::error_code();
Joe Abbey97b7a172013-02-06 22:14:06 +00002137
Chris Lattner27d38752013-01-20 02:13:19 +00002138 BitstreamEntry Entry =
2139 Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
Joe Abbey97b7a172013-02-06 22:14:06 +00002140
Chris Lattner27d38752013-01-20 02:13:19 +00002141 switch (Entry.Kind) {
2142 case BitstreamEntry::Error:
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002143 return Error(BitcodeError::MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002144 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002145 return std::error_code();
Joe Abbey97b7a172013-02-06 22:14:06 +00002146
Chris Lattner27d38752013-01-20 02:13:19 +00002147 case BitstreamEntry::SubBlock:
2148 switch (Entry.ID) {
2149 case bitc::BLOCKINFO_BLOCK_ID:
2150 if (Stream.ReadBlockInfoBlock())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002151 return Error(BitcodeError::MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002152 break;
2153 case bitc::MODULE_BLOCK_ID:
2154 // Reject multiple MODULE_BLOCK's in a single bitstream.
2155 if (TheModule)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002156 return Error(BitcodeError::InvalidMultipleBlocks);
Chris Lattner27d38752013-01-20 02:13:19 +00002157 TheModule = M;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002158 if (std::error_code EC = ParseModule(false))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002159 return EC;
2160 if (LazyStreamer)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002161 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00002162 break;
2163 default:
2164 if (Stream.SkipBlock())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002165 return Error(BitcodeError::InvalidRecord);
Chris Lattner27d38752013-01-20 02:13:19 +00002166 break;
2167 }
2168 continue;
2169 case BitstreamEntry::Record:
2170 // There should be no records in the top-level of blocks.
Joe Abbey97b7a172013-02-06 22:14:06 +00002171
Chris Lattner27d38752013-01-20 02:13:19 +00002172 // The ranlib in Xcode 4 will align archive members by appending newlines
Chad Rosiera15e3aa2011-08-09 22:23:40 +00002173 // to the end of them. If this file size is a multiple of 4 but not 8, we
2174 // have to read and ignore these final 4 bytes :-(
Chris Lattner27d38752013-01-20 02:13:19 +00002175 if (Stream.getAbbrevIDWidth() == 2 && Entry.ID == 2 &&
Rafael Espindolaa97b2382011-05-26 18:59:54 +00002176 Stream.Read(6) == 2 && Stream.Read(24) == 0xa0a0a &&
Bill Wendling318f03f2012-07-19 00:15:11 +00002177 Stream.AtEndOfStream())
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002178 return std::error_code();
Joe Abbey97b7a172013-02-06 22:14:06 +00002179
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002180 return Error(BitcodeError::InvalidRecord);
Rafael Espindolaa97b2382011-05-26 18:59:54 +00002181 }
Chris Lattner1314b992007-04-22 06:23:29 +00002182 }
Chris Lattner1314b992007-04-22 06:23:29 +00002183}
Chris Lattner6694f602007-04-29 07:54:31 +00002184
Rafael Espindolac75c4fa2014-07-04 20:02:42 +00002185ErrorOr<std::string> BitcodeReader::parseModuleTriple() {
Bill Wendling0198ce02010-10-06 01:22:42 +00002186 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002187 return Error(BitcodeError::InvalidRecord);
Bill Wendling0198ce02010-10-06 01:22:42 +00002188
2189 SmallVector<uint64_t, 64> Record;
2190
Rafael Espindolac75c4fa2014-07-04 20:02:42 +00002191 std::string Triple;
Bill Wendling0198ce02010-10-06 01:22:42 +00002192 // Read all the records for this module.
Chris Lattner27d38752013-01-20 02:13:19 +00002193 while (1) {
2194 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00002195
Chris Lattner27d38752013-01-20 02:13:19 +00002196 switch (Entry.Kind) {
2197 case BitstreamEntry::SubBlock: // Handled for us already.
2198 case BitstreamEntry::Error:
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002199 return Error(BitcodeError::MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002200 case BitstreamEntry::EndBlock:
Rafael Espindolae6107792014-07-04 20:05:56 +00002201 return Triple;
Chris Lattner27d38752013-01-20 02:13:19 +00002202 case BitstreamEntry::Record:
2203 // The interesting case.
2204 break;
Bill Wendling0198ce02010-10-06 01:22:42 +00002205 }
2206
2207 // Read a record.
Chris Lattner27d38752013-01-20 02:13:19 +00002208 switch (Stream.readRecord(Entry.ID, Record)) {
Bill Wendling0198ce02010-10-06 01:22:42 +00002209 default: break; // Default behavior, ignore unknown content.
Bill Wendling0198ce02010-10-06 01:22:42 +00002210 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
Rafael Espindolac75c4fa2014-07-04 20:02:42 +00002211 std::string S;
2212 if (ConvertToString(Record, 0, S))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002213 return Error(BitcodeError::InvalidRecord);
Rafael Espindolac75c4fa2014-07-04 20:02:42 +00002214 Triple = S;
Bill Wendling0198ce02010-10-06 01:22:42 +00002215 break;
2216 }
2217 }
2218 Record.clear();
2219 }
Rafael Espindolae6107792014-07-04 20:05:56 +00002220 llvm_unreachable("Exit infinite loop");
Bill Wendling0198ce02010-10-06 01:22:42 +00002221}
2222
Rafael Espindolac75c4fa2014-07-04 20:02:42 +00002223ErrorOr<std::string> BitcodeReader::parseTriple() {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002224 if (std::error_code EC = InitStream())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002225 return EC;
Bill Wendling0198ce02010-10-06 01:22:42 +00002226
2227 // Sniff for the signature.
2228 if (Stream.Read(8) != 'B' ||
2229 Stream.Read(8) != 'C' ||
2230 Stream.Read(4) != 0x0 ||
2231 Stream.Read(4) != 0xC ||
2232 Stream.Read(4) != 0xE ||
2233 Stream.Read(4) != 0xD)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002234 return Error(BitcodeError::InvalidBitcodeSignature);
Bill Wendling0198ce02010-10-06 01:22:42 +00002235
2236 // We expect a number of well-defined blocks, though we don't necessarily
2237 // need to understand them all.
Chris Lattner27d38752013-01-20 02:13:19 +00002238 while (1) {
2239 BitstreamEntry Entry = Stream.advance();
Joe Abbey97b7a172013-02-06 22:14:06 +00002240
Chris Lattner27d38752013-01-20 02:13:19 +00002241 switch (Entry.Kind) {
2242 case BitstreamEntry::Error:
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002243 return Error(BitcodeError::MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002244 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002245 return std::error_code();
Joe Abbey97b7a172013-02-06 22:14:06 +00002246
Chris Lattner27d38752013-01-20 02:13:19 +00002247 case BitstreamEntry::SubBlock:
2248 if (Entry.ID == bitc::MODULE_BLOCK_ID)
Rafael Espindolad346cc82014-07-04 13:52:01 +00002249 return parseModuleTriple();
Joe Abbey97b7a172013-02-06 22:14:06 +00002250
Chris Lattner27d38752013-01-20 02:13:19 +00002251 // Ignore other sub-blocks.
Rafael Espindola48da4f42013-11-04 16:16:24 +00002252 if (Stream.SkipBlock())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002253 return Error(BitcodeError::MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002254 continue;
Joe Abbey97b7a172013-02-06 22:14:06 +00002255
Chris Lattner27d38752013-01-20 02:13:19 +00002256 case BitstreamEntry::Record:
2257 Stream.skipRecord(Entry.ID);
2258 continue;
Bill Wendling0198ce02010-10-06 01:22:42 +00002259 }
2260 }
Bill Wendling0198ce02010-10-06 01:22:42 +00002261}
2262
Devang Patelaf206b82009-09-18 19:26:43 +00002263/// ParseMetadataAttachment - Parse metadata attachments.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002264std::error_code BitcodeReader::ParseMetadataAttachment() {
Devang Patelaf206b82009-09-18 19:26:43 +00002265 if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002266 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002267
Devang Patelaf206b82009-09-18 19:26:43 +00002268 SmallVector<uint64_t, 64> Record;
Chris Lattner27d38752013-01-20 02:13:19 +00002269 while (1) {
2270 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00002271
Chris Lattner27d38752013-01-20 02:13:19 +00002272 switch (Entry.Kind) {
2273 case BitstreamEntry::SubBlock: // Handled for us already.
2274 case BitstreamEntry::Error:
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002275 return Error(BitcodeError::MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002276 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002277 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00002278 case BitstreamEntry::Record:
2279 // The interesting case.
Devang Patelaf206b82009-09-18 19:26:43 +00002280 break;
2281 }
Chris Lattner27d38752013-01-20 02:13:19 +00002282
Devang Patelaf206b82009-09-18 19:26:43 +00002283 // Read a metadata attachment record.
2284 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +00002285 switch (Stream.readRecord(Entry.ID, Record)) {
Devang Patelaf206b82009-09-18 19:26:43 +00002286 default: // Default behavior: ignore.
2287 break;
Chris Lattnerb8778552011-06-17 17:50:30 +00002288 case bitc::METADATA_ATTACHMENT: {
Devang Patelaf206b82009-09-18 19:26:43 +00002289 unsigned RecordLength = Record.size();
2290 if (Record.empty() || (RecordLength - 1) % 2 == 1)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002291 return Error(BitcodeError::InvalidRecord);
Devang Patelaf206b82009-09-18 19:26:43 +00002292 Instruction *Inst = InstructionList[Record[0]];
2293 for (unsigned i = 1; i != RecordLength; i = i+2) {
Devang Patelb1a44772009-09-28 21:14:55 +00002294 unsigned Kind = Record[i];
Dan Gohman43aa8f02010-07-20 21:42:28 +00002295 DenseMap<unsigned, unsigned>::iterator I =
2296 MDKindMap.find(Kind);
2297 if (I == MDKindMap.end())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002298 return Error(BitcodeError::InvalidID);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002299 Value *Node = MDValueList.getValueFwdRef(Record[i+1]);
Dan Gohman43aa8f02010-07-20 21:42:28 +00002300 Inst->setMetadata(I->second, cast<MDNode>(Node));
Manman Ren209b17c2013-09-28 00:22:27 +00002301 if (I->second == LLVMContext::MD_tbaa)
2302 InstsWithTBAATag.push_back(Inst);
Devang Patelaf206b82009-09-18 19:26:43 +00002303 }
2304 break;
2305 }
2306 }
2307 }
Devang Patelaf206b82009-09-18 19:26:43 +00002308}
Chris Lattner51ffe7c2007-05-01 04:59:48 +00002309
Chris Lattner85b7b402007-05-01 05:52:21 +00002310/// ParseFunctionBody - Lazily parse the specified function body block.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002311std::error_code BitcodeReader::ParseFunctionBody(Function *F) {
Chris Lattner982ec1e2007-05-05 00:17:00 +00002312 if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002313 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002314
Nick Lewyckya72e1af2010-02-25 08:30:17 +00002315 InstructionList.clear();
Chris Lattner85b7b402007-05-01 05:52:21 +00002316 unsigned ModuleValueListSize = ValueList.size();
Dan Gohman26d837d2010-08-25 20:22:53 +00002317 unsigned ModuleMDValueListSize = MDValueList.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002318
Chris Lattner85b7b402007-05-01 05:52:21 +00002319 // Add all the function arguments to the value table.
2320 for(Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
2321 ValueList.push_back(I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002322
Chris Lattner83930552007-05-01 07:01:57 +00002323 unsigned NextValueNo = ValueList.size();
Craig Topper2617dcc2014-04-15 06:32:26 +00002324 BasicBlock *CurBB = nullptr;
Chris Lattnere53603e2007-05-02 04:27:25 +00002325 unsigned CurBBNo = 0;
2326
Chris Lattner07d09ed2010-04-03 02:17:50 +00002327 DebugLoc LastLoc;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002328
Chris Lattner85b7b402007-05-01 05:52:21 +00002329 // Read all the records.
2330 SmallVector<uint64_t, 64> Record;
2331 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00002332 BitstreamEntry Entry = Stream.advance();
Joe Abbey97b7a172013-02-06 22:14:06 +00002333
Chris Lattner27d38752013-01-20 02:13:19 +00002334 switch (Entry.Kind) {
2335 case BitstreamEntry::Error:
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002336 return Error(BitcodeError::MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002337 case BitstreamEntry::EndBlock:
2338 goto OutOfRecordLoop;
Joe Abbey97b7a172013-02-06 22:14:06 +00002339
Chris Lattner27d38752013-01-20 02:13:19 +00002340 case BitstreamEntry::SubBlock:
2341 switch (Entry.ID) {
Chris Lattner85b7b402007-05-01 05:52:21 +00002342 default: // Skip unknown content.
2343 if (Stream.SkipBlock())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002344 return Error(BitcodeError::InvalidRecord);
Chris Lattner85b7b402007-05-01 05:52:21 +00002345 break;
2346 case bitc::CONSTANTS_BLOCK_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002347 if (std::error_code EC = ParseConstants())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002348 return EC;
Chris Lattner83930552007-05-01 07:01:57 +00002349 NextValueNo = ValueList.size();
Chris Lattner85b7b402007-05-01 05:52:21 +00002350 break;
2351 case bitc::VALUE_SYMTAB_BLOCK_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002352 if (std::error_code EC = ParseValueSymbolTable())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002353 return EC;
Chris Lattner85b7b402007-05-01 05:52:21 +00002354 break;
Devang Patelaf206b82009-09-18 19:26:43 +00002355 case bitc::METADATA_ATTACHMENT_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002356 if (std::error_code EC = ParseMetadataAttachment())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002357 return EC;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002358 break;
Victor Hernandez108d3ac2010-01-13 19:34:08 +00002359 case bitc::METADATA_BLOCK_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002360 if (std::error_code EC = ParseMetadata())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002361 return EC;
Victor Hernandez108d3ac2010-01-13 19:34:08 +00002362 break;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002363 case bitc::USELIST_BLOCK_ID:
2364 if (std::error_code EC = ParseUseLists())
2365 return EC;
2366 break;
Chris Lattner85b7b402007-05-01 05:52:21 +00002367 }
2368 continue;
Joe Abbey97b7a172013-02-06 22:14:06 +00002369
Chris Lattner27d38752013-01-20 02:13:19 +00002370 case BitstreamEntry::Record:
2371 // The interesting case.
2372 break;
Chris Lattner85b7b402007-05-01 05:52:21 +00002373 }
Joe Abbey97b7a172013-02-06 22:14:06 +00002374
Chris Lattner85b7b402007-05-01 05:52:21 +00002375 // Read a record.
2376 Record.clear();
Craig Topper2617dcc2014-04-15 06:32:26 +00002377 Instruction *I = nullptr;
Chris Lattner27d38752013-01-20 02:13:19 +00002378 unsigned BitCode = Stream.readRecord(Entry.ID, Record);
Dan Gohman0ebd6962009-07-20 21:19:07 +00002379 switch (BitCode) {
Chris Lattner83930552007-05-01 07:01:57 +00002380 default: // Default behavior: reject
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002381 return Error(BitcodeError::InvalidValue);
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00002382 case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks]
Chris Lattner83930552007-05-01 07:01:57 +00002383 if (Record.size() < 1 || Record[0] == 0)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002384 return Error(BitcodeError::InvalidRecord);
Chris Lattner85b7b402007-05-01 05:52:21 +00002385 // Create all the basic blocks for the function.
Chris Lattner6ce15cb2007-05-03 22:09:51 +00002386 FunctionBBs.resize(Record[0]);
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00002387
2388 // See if anything took the address of blocks in this function.
2389 auto BBFRI = BasicBlockFwdRefs.find(F);
2390 if (BBFRI == BasicBlockFwdRefs.end()) {
2391 for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i)
2392 FunctionBBs[i] = BasicBlock::Create(Context, "", F);
2393 } else {
2394 auto &BBRefs = BBFRI->second;
2395 std::sort(BBRefs.begin(), BBRefs.end(),
2396 [](const std::pair<unsigned, BasicBlock *> &LHS,
2397 const std::pair<unsigned, BasicBlock *> &RHS) {
2398 return LHS.first < RHS.first;
2399 });
2400 unsigned R = 0, RE = BBRefs.size();
2401 for (unsigned I = 0, E = FunctionBBs.size(); I != E; ++I)
2402 if (R != RE && BBRefs[R].first == I) {
2403 assert(I != 0 && "Invalid reference to entry block");
2404 BasicBlock *BB = BBRefs[R++].second;
2405 BB->insertInto(F);
2406 FunctionBBs[I] = BB;
2407 } else {
2408 FunctionBBs[I] = BasicBlock::Create(Context, "", F);
2409 }
2410 // Check for invalid basic block references.
2411 if (R != RE)
2412 return Error(BitcodeError::InvalidID);
2413
2414 // Erase from the table.
2415 BasicBlockFwdRefs.erase(BBFRI);
2416 }
2417
Chris Lattner83930552007-05-01 07:01:57 +00002418 CurBB = FunctionBBs[0];
2419 continue;
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00002420 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002421
Chris Lattner07d09ed2010-04-03 02:17:50 +00002422 case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN
2423 // This record indicates that the last instruction is at the same
2424 // location as the previous instruction with a location.
Craig Topper2617dcc2014-04-15 06:32:26 +00002425 I = nullptr;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002426
Chris Lattner07d09ed2010-04-03 02:17:50 +00002427 // Get the last instruction emitted.
2428 if (CurBB && !CurBB->empty())
2429 I = &CurBB->back();
2430 else if (CurBBNo && FunctionBBs[CurBBNo-1] &&
2431 !FunctionBBs[CurBBNo-1]->empty())
2432 I = &FunctionBBs[CurBBNo-1]->back();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002433
Craig Topper2617dcc2014-04-15 06:32:26 +00002434 if (!I)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002435 return Error(BitcodeError::InvalidRecord);
Chris Lattner07d09ed2010-04-03 02:17:50 +00002436 I->setDebugLoc(LastLoc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002437 I = nullptr;
Chris Lattner07d09ed2010-04-03 02:17:50 +00002438 continue;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002439
Chris Lattnerc44070802011-06-17 18:17:37 +00002440 case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia]
Craig Topper2617dcc2014-04-15 06:32:26 +00002441 I = nullptr; // Get the last instruction emitted.
Chris Lattner07d09ed2010-04-03 02:17:50 +00002442 if (CurBB && !CurBB->empty())
2443 I = &CurBB->back();
2444 else if (CurBBNo && FunctionBBs[CurBBNo-1] &&
2445 !FunctionBBs[CurBBNo-1]->empty())
2446 I = &FunctionBBs[CurBBNo-1]->back();
Craig Topper2617dcc2014-04-15 06:32:26 +00002447 if (!I || Record.size() < 4)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002448 return Error(BitcodeError::InvalidRecord);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002449
Chris Lattner07d09ed2010-04-03 02:17:50 +00002450 unsigned Line = Record[0], Col = Record[1];
2451 unsigned ScopeID = Record[2], IAID = Record[3];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002452
Craig Topper2617dcc2014-04-15 06:32:26 +00002453 MDNode *Scope = nullptr, *IA = nullptr;
Chris Lattner07d09ed2010-04-03 02:17:50 +00002454 if (ScopeID) Scope = cast<MDNode>(MDValueList.getValueFwdRef(ScopeID-1));
2455 if (IAID) IA = cast<MDNode>(MDValueList.getValueFwdRef(IAID-1));
2456 LastLoc = DebugLoc::get(Line, Col, Scope, IA);
2457 I->setDebugLoc(LastLoc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002458 I = nullptr;
Chris Lattner07d09ed2010-04-03 02:17:50 +00002459 continue;
2460 }
2461
Chris Lattnere9759c22007-05-06 00:21:25 +00002462 case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode]
2463 unsigned OpNum = 0;
2464 Value *LHS, *RHS;
2465 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002466 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
Dan Gohman0ebd6962009-07-20 21:19:07 +00002467 OpNum+1 > Record.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002468 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002469
Dan Gohman0ebd6962009-07-20 21:19:07 +00002470 int Opc = GetDecodedBinaryOpcode(Record[OpNum++], LHS->getType());
Rafael Espindola48da4f42013-11-04 16:16:24 +00002471 if (Opc == -1)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002472 return Error(BitcodeError::InvalidRecord);
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002473 I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
Devang Patelaf206b82009-09-18 19:26:43 +00002474 InstructionList.push_back(I);
Dan Gohman1b849082009-09-07 23:54:19 +00002475 if (OpNum < Record.size()) {
2476 if (Opc == Instruction::Add ||
2477 Opc == Instruction::Sub ||
Chris Lattnera676c0f2011-02-07 16:40:21 +00002478 Opc == Instruction::Mul ||
2479 Opc == Instruction::Shl) {
Dan Gohman00f47472010-01-25 21:55:39 +00002480 if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP))
Dan Gohman1b849082009-09-07 23:54:19 +00002481 cast<BinaryOperator>(I)->setHasNoSignedWrap(true);
Dan Gohman00f47472010-01-25 21:55:39 +00002482 if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
Dan Gohman1b849082009-09-07 23:54:19 +00002483 cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true);
Chris Lattner35315d02011-02-06 21:44:57 +00002484 } else if (Opc == Instruction::SDiv ||
Chris Lattnera676c0f2011-02-07 16:40:21 +00002485 Opc == Instruction::UDiv ||
2486 Opc == Instruction::LShr ||
2487 Opc == Instruction::AShr) {
Chris Lattner35315d02011-02-06 21:44:57 +00002488 if (Record[OpNum] & (1 << bitc::PEO_EXACT))
Dan Gohman1b849082009-09-07 23:54:19 +00002489 cast<BinaryOperator>(I)->setIsExact(true);
Michael Ilseman9978d7e2012-11-27 00:43:38 +00002490 } else if (isa<FPMathOperator>(I)) {
2491 FastMathFlags FMF;
Michael Ilseman65f14352012-12-09 21:12:04 +00002492 if (0 != (Record[OpNum] & FastMathFlags::UnsafeAlgebra))
2493 FMF.setUnsafeAlgebra();
2494 if (0 != (Record[OpNum] & FastMathFlags::NoNaNs))
2495 FMF.setNoNaNs();
2496 if (0 != (Record[OpNum] & FastMathFlags::NoInfs))
2497 FMF.setNoInfs();
2498 if (0 != (Record[OpNum] & FastMathFlags::NoSignedZeros))
2499 FMF.setNoSignedZeros();
2500 if (0 != (Record[OpNum] & FastMathFlags::AllowReciprocal))
2501 FMF.setAllowReciprocal();
Michael Ilseman9978d7e2012-11-27 00:43:38 +00002502 if (FMF.any())
2503 I->setFastMathFlags(FMF);
Dan Gohman1b849082009-09-07 23:54:19 +00002504 }
Michael Ilseman9978d7e2012-11-27 00:43:38 +00002505
Dan Gohman1b849082009-09-07 23:54:19 +00002506 }
Chris Lattner85b7b402007-05-01 05:52:21 +00002507 break;
2508 }
Chris Lattnere9759c22007-05-06 00:21:25 +00002509 case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc]
2510 unsigned OpNum = 0;
2511 Value *Op;
2512 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
2513 OpNum+2 != Record.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002514 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002515
Chris Lattner229907c2011-07-18 04:54:35 +00002516 Type *ResTy = getTypeByID(Record[OpNum]);
Chris Lattnere9759c22007-05-06 00:21:25 +00002517 int Opc = GetDecodedCastOpcode(Record[OpNum+1]);
Craig Topper2617dcc2014-04-15 06:32:26 +00002518 if (Opc == -1 || !ResTy)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002519 return Error(BitcodeError::InvalidRecord);
Craig Topper2617dcc2014-04-15 06:32:26 +00002520 Instruction *Temp = nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002521 if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) {
2522 if (Temp) {
2523 InstructionList.push_back(Temp);
2524 CurBB->getInstList().push_back(Temp);
2525 }
2526 } else {
2527 I = CastInst::Create((Instruction::CastOps)Opc, Op, ResTy);
2528 }
Devang Patelaf206b82009-09-18 19:26:43 +00002529 InstructionList.push_back(I);
Chris Lattnere53603e2007-05-02 04:27:25 +00002530 break;
2531 }
Dan Gohman1639c392009-07-27 21:53:46 +00002532 case bitc::FUNC_CODE_INST_INBOUNDS_GEP:
Chris Lattnere14cb882007-05-04 19:11:41 +00002533 case bitc::FUNC_CODE_INST_GEP: { // GEP: [n x operands]
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002534 unsigned OpNum = 0;
2535 Value *BasePtr;
2536 if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002537 return Error(BitcodeError::InvalidRecord);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002538
Chris Lattner5285b5e2007-05-02 05:46:45 +00002539 SmallVector<Value*, 16> GEPIdx;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002540 while (OpNum != Record.size()) {
2541 Value *Op;
2542 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002543 return Error(BitcodeError::InvalidRecord);
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002544 GEPIdx.push_back(Op);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002545 }
2546
Jay Foadd1b78492011-07-25 09:48:08 +00002547 I = GetElementPtrInst::Create(BasePtr, GEPIdx);
Devang Patelaf206b82009-09-18 19:26:43 +00002548 InstructionList.push_back(I);
Dan Gohman1639c392009-07-27 21:53:46 +00002549 if (BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP)
Dan Gohman1b849082009-09-07 23:54:19 +00002550 cast<GetElementPtrInst>(I)->setIsInBounds(true);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002551 break;
2552 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002553
Dan Gohman1ecaf452008-05-31 00:58:22 +00002554 case bitc::FUNC_CODE_INST_EXTRACTVAL: {
2555 // EXTRACTVAL: [opty, opval, n x indices]
Dan Gohman30499842008-05-23 01:55:30 +00002556 unsigned OpNum = 0;
2557 Value *Agg;
2558 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002559 return Error(BitcodeError::InvalidRecord);
Dan Gohman30499842008-05-23 01:55:30 +00002560
Dan Gohman1ecaf452008-05-31 00:58:22 +00002561 SmallVector<unsigned, 4> EXTRACTVALIdx;
2562 for (unsigned RecSize = Record.size();
2563 OpNum != RecSize; ++OpNum) {
2564 uint64_t Index = Record[OpNum];
2565 if ((unsigned)Index != Index)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002566 return Error(BitcodeError::InvalidValue);
Dan Gohman1ecaf452008-05-31 00:58:22 +00002567 EXTRACTVALIdx.push_back((unsigned)Index);
Dan Gohman30499842008-05-23 01:55:30 +00002568 }
2569
Jay Foad57aa6362011-07-13 10:26:04 +00002570 I = ExtractValueInst::Create(Agg, EXTRACTVALIdx);
Devang Patelaf206b82009-09-18 19:26:43 +00002571 InstructionList.push_back(I);
Dan Gohman30499842008-05-23 01:55:30 +00002572 break;
2573 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002574
Dan Gohman1ecaf452008-05-31 00:58:22 +00002575 case bitc::FUNC_CODE_INST_INSERTVAL: {
2576 // INSERTVAL: [opty, opval, opty, opval, n x indices]
Dan Gohman30499842008-05-23 01:55:30 +00002577 unsigned OpNum = 0;
2578 Value *Agg;
2579 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002580 return Error(BitcodeError::InvalidRecord);
Dan Gohman30499842008-05-23 01:55:30 +00002581 Value *Val;
2582 if (getValueTypePair(Record, OpNum, NextValueNo, Val))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002583 return Error(BitcodeError::InvalidRecord);
Dan Gohman30499842008-05-23 01:55:30 +00002584
Dan Gohman1ecaf452008-05-31 00:58:22 +00002585 SmallVector<unsigned, 4> INSERTVALIdx;
2586 for (unsigned RecSize = Record.size();
2587 OpNum != RecSize; ++OpNum) {
2588 uint64_t Index = Record[OpNum];
2589 if ((unsigned)Index != Index)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002590 return Error(BitcodeError::InvalidValue);
Dan Gohman1ecaf452008-05-31 00:58:22 +00002591 INSERTVALIdx.push_back((unsigned)Index);
Dan Gohman30499842008-05-23 01:55:30 +00002592 }
2593
Jay Foad57aa6362011-07-13 10:26:04 +00002594 I = InsertValueInst::Create(Agg, Val, INSERTVALIdx);
Devang Patelaf206b82009-09-18 19:26:43 +00002595 InstructionList.push_back(I);
Dan Gohman30499842008-05-23 01:55:30 +00002596 break;
2597 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002598
Chris Lattnere9759c22007-05-06 00:21:25 +00002599 case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval]
Dan Gohmanc5d28922008-09-16 01:01:33 +00002600 // obsolete form of select
2601 // handles select i1 ... in old bitcode
Chris Lattnere9759c22007-05-06 00:21:25 +00002602 unsigned OpNum = 0;
2603 Value *TrueVal, *FalseVal, *Cond;
2604 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002605 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
2606 popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002607 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002608
Dan Gohmanc5d28922008-09-16 01:01:33 +00002609 I = SelectInst::Create(Cond, TrueVal, FalseVal);
Devang Patelaf206b82009-09-18 19:26:43 +00002610 InstructionList.push_back(I);
Dan Gohmanc5d28922008-09-16 01:01:33 +00002611 break;
2612 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002613
Dan Gohmanc5d28922008-09-16 01:01:33 +00002614 case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred]
2615 // new form of select
2616 // handles select i1 or select [N x i1]
2617 unsigned OpNum = 0;
2618 Value *TrueVal, *FalseVal, *Cond;
2619 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002620 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
Dan Gohmanc5d28922008-09-16 01:01:33 +00002621 getValueTypePair(Record, OpNum, NextValueNo, Cond))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002622 return Error(BitcodeError::InvalidRecord);
Dan Gohmanc579d972008-09-09 01:02:47 +00002623
2624 // select condition can be either i1 or [N x i1]
Chris Lattner229907c2011-07-18 04:54:35 +00002625 if (VectorType* vector_type =
2626 dyn_cast<VectorType>(Cond->getType())) {
Dan Gohmanc579d972008-09-09 01:02:47 +00002627 // expect <n x i1>
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002628 if (vector_type->getElementType() != Type::getInt1Ty(Context))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002629 return Error(BitcodeError::InvalidTypeForValue);
Dan Gohmanc579d972008-09-09 01:02:47 +00002630 } else {
2631 // expect i1
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002632 if (Cond->getType() != Type::getInt1Ty(Context))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002633 return Error(BitcodeError::InvalidTypeForValue);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002634 }
2635
Gabor Greife9ecc682008-04-06 20:25:17 +00002636 I = SelectInst::Create(Cond, TrueVal, FalseVal);
Devang Patelaf206b82009-09-18 19:26:43 +00002637 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002638 break;
2639 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002640
Chris Lattner1fc27f02007-05-02 05:16:49 +00002641 case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval]
Chris Lattnere9759c22007-05-06 00:21:25 +00002642 unsigned OpNum = 0;
2643 Value *Vec, *Idx;
2644 if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002645 getValueTypePair(Record, OpNum, NextValueNo, Idx))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002646 return Error(BitcodeError::InvalidRecord);
Eric Christopherc9742252009-07-25 02:28:41 +00002647 I = ExtractElementInst::Create(Vec, Idx);
Devang Patelaf206b82009-09-18 19:26:43 +00002648 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002649 break;
2650 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002651
Chris Lattner1fc27f02007-05-02 05:16:49 +00002652 case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval]
Chris Lattnere9759c22007-05-06 00:21:25 +00002653 unsigned OpNum = 0;
2654 Value *Vec, *Elt, *Idx;
2655 if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002656 popValue(Record, OpNum, NextValueNo,
Chris Lattnere9759c22007-05-06 00:21:25 +00002657 cast<VectorType>(Vec->getType())->getElementType(), Elt) ||
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002658 getValueTypePair(Record, OpNum, NextValueNo, Idx))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002659 return Error(BitcodeError::InvalidRecord);
Gabor Greife9ecc682008-04-06 20:25:17 +00002660 I = InsertElementInst::Create(Vec, Elt, Idx);
Devang Patelaf206b82009-09-18 19:26:43 +00002661 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002662 break;
2663 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002664
Chris Lattnere9759c22007-05-06 00:21:25 +00002665 case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval]
2666 unsigned OpNum = 0;
2667 Value *Vec1, *Vec2, *Mask;
2668 if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002669 popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002670 return Error(BitcodeError::InvalidRecord);
Chris Lattnere9759c22007-05-06 00:21:25 +00002671
Mon P Wang25f01062008-11-10 04:46:22 +00002672 if (getValueTypePair(Record, OpNum, NextValueNo, Mask))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002673 return Error(BitcodeError::InvalidRecord);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002674 I = new ShuffleVectorInst(Vec1, Vec2, Mask);
Devang Patelaf206b82009-09-18 19:26:43 +00002675 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002676 break;
2677 }
Mon P Wang25f01062008-11-10 04:46:22 +00002678
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002679 case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred]
2680 // Old form of ICmp/FCmp returning bool
2681 // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were
2682 // both legal on vectors but had different behaviour.
2683 case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred]
2684 // FCmp/ICmp returning bool or vector of bool
2685
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002686 unsigned OpNum = 0;
2687 Value *LHS, *RHS;
2688 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002689 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002690 OpNum+1 != Record.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002691 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002692
Duncan Sands9dff9be2010-02-15 16:12:20 +00002693 if (LHS->getType()->isFPOrFPVectorTy())
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002694 I = new FCmpInst((FCmpInst::Predicate)Record[OpNum], LHS, RHS);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002695 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002696 I = new ICmpInst((ICmpInst::Predicate)Record[OpNum], LHS, RHS);
Devang Patelaf206b82009-09-18 19:26:43 +00002697 InstructionList.push_back(I);
Dan Gohmanc579d972008-09-09 01:02:47 +00002698 break;
2699 }
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002700
Chris Lattnere53603e2007-05-02 04:27:25 +00002701 case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>]
Devang Patelbbfd8742008-02-26 01:29:32 +00002702 {
2703 unsigned Size = Record.size();
2704 if (Size == 0) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002705 I = ReturnInst::Create(Context);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002706 InstructionList.push_back(I);
Devang Patelbbfd8742008-02-26 01:29:32 +00002707 break;
Dan Gohmanfa1211f2008-07-23 00:34:11 +00002708 }
Devang Patelbbfd8742008-02-26 01:29:32 +00002709
Dan Gohmanfa1211f2008-07-23 00:34:11 +00002710 unsigned OpNum = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +00002711 Value *Op = nullptr;
Chris Lattnerf1c87102011-06-17 18:09:11 +00002712 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002713 return Error(BitcodeError::InvalidRecord);
Chris Lattnerf1c87102011-06-17 18:09:11 +00002714 if (OpNum != Record.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002715 return Error(BitcodeError::InvalidRecord);
Dan Gohmanfa1211f2008-07-23 00:34:11 +00002716
Chris Lattnerf1c87102011-06-17 18:09:11 +00002717 I = ReturnInst::Create(Context, Op);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002718 InstructionList.push_back(I);
Dan Gohmanfa1211f2008-07-23 00:34:11 +00002719 break;
Chris Lattnere53603e2007-05-02 04:27:25 +00002720 }
Chris Lattner5285b5e2007-05-02 05:46:45 +00002721 case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#]
Chris Lattner6ce15cb2007-05-03 22:09:51 +00002722 if (Record.size() != 1 && Record.size() != 3)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002723 return Error(BitcodeError::InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002724 BasicBlock *TrueDest = getBasicBlock(Record[0]);
Craig Topper2617dcc2014-04-15 06:32:26 +00002725 if (!TrueDest)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002726 return Error(BitcodeError::InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002727
Devang Patelaf206b82009-09-18 19:26:43 +00002728 if (Record.size() == 1) {
Gabor Greife9ecc682008-04-06 20:25:17 +00002729 I = BranchInst::Create(TrueDest);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002730 InstructionList.push_back(I);
Devang Patelaf206b82009-09-18 19:26:43 +00002731 }
Chris Lattner5285b5e2007-05-02 05:46:45 +00002732 else {
2733 BasicBlock *FalseDest = getBasicBlock(Record[1]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002734 Value *Cond = getValue(Record, 2, NextValueNo,
2735 Type::getInt1Ty(Context));
Craig Topper2617dcc2014-04-15 06:32:26 +00002736 if (!FalseDest || !Cond)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002737 return Error(BitcodeError::InvalidRecord);
Gabor Greife9ecc682008-04-06 20:25:17 +00002738 I = BranchInst::Create(TrueDest, FalseDest, Cond);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002739 InstructionList.push_back(I);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002740 }
2741 break;
2742 }
Chris Lattner3ed871f2009-10-27 19:13:16 +00002743 case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...]
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002744 // Check magic
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002745 if ((Record[0] >> 16) == SWITCH_INST_MAGIC) {
Bob Wilsone4077362013-09-09 19:14:35 +00002746 // "New" SwitchInst format with case ranges. The changes to write this
2747 // format were reverted but we still recognize bitcode that uses it.
2748 // Hopefully someday we will have support for case ranges and can use
2749 // this format again.
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002750
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002751 Type *OpTy = getTypeByID(Record[1]);
2752 unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth();
2753
Jan Wen Voungafaced02012-10-11 20:20:40 +00002754 Value *Cond = getValue(Record, 2, NextValueNo, OpTy);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002755 BasicBlock *Default = getBasicBlock(Record[3]);
Craig Topper2617dcc2014-04-15 06:32:26 +00002756 if (!OpTy || !Cond || !Default)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002757 return Error(BitcodeError::InvalidRecord);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002758
2759 unsigned NumCases = Record[4];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002760
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002761 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
2762 InstructionList.push_back(SI);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002763
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002764 unsigned CurIdx = 5;
2765 for (unsigned i = 0; i != NumCases; ++i) {
Bob Wilsone4077362013-09-09 19:14:35 +00002766 SmallVector<ConstantInt*, 1> CaseVals;
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002767 unsigned NumItems = Record[CurIdx++];
2768 for (unsigned ci = 0; ci != NumItems; ++ci) {
2769 bool isSingleNumber = Record[CurIdx++];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002770
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002771 APInt Low;
2772 unsigned ActiveWords = 1;
2773 if (ValueBitWidth > 64)
2774 ActiveWords = Record[CurIdx++];
Benjamin Kramer9704ed02012-05-28 14:10:31 +00002775 Low = ReadWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
2776 ValueBitWidth);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002777 CurIdx += ActiveWords;
Stepan Dyatkovskiye3e19cb2012-05-28 12:39:09 +00002778
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002779 if (!isSingleNumber) {
2780 ActiveWords = 1;
2781 if (ValueBitWidth > 64)
2782 ActiveWords = Record[CurIdx++];
2783 APInt High =
Benjamin Kramer9704ed02012-05-28 14:10:31 +00002784 ReadWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
2785 ValueBitWidth);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002786 CurIdx += ActiveWords;
Bob Wilsone4077362013-09-09 19:14:35 +00002787
2788 // FIXME: It is not clear whether values in the range should be
2789 // compared as signed or unsigned values. The partially
2790 // implemented changes that used this format in the past used
2791 // unsigned comparisons.
2792 for ( ; Low.ule(High); ++Low)
2793 CaseVals.push_back(ConstantInt::get(Context, Low));
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002794 } else
Bob Wilsone4077362013-09-09 19:14:35 +00002795 CaseVals.push_back(ConstantInt::get(Context, Low));
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002796 }
2797 BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]);
Bob Wilsone4077362013-09-09 19:14:35 +00002798 for (SmallVector<ConstantInt*, 1>::iterator cvi = CaseVals.begin(),
2799 cve = CaseVals.end(); cvi != cve; ++cvi)
2800 SI->addCase(*cvi, DestBB);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002801 }
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002802 I = SI;
2803 break;
2804 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002805
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002806 // Old SwitchInst format without case ranges.
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002807
Chris Lattner5285b5e2007-05-02 05:46:45 +00002808 if (Record.size() < 3 || (Record.size() & 1) == 0)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002809 return Error(BitcodeError::InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00002810 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002811 Value *Cond = getValue(Record, 1, NextValueNo, OpTy);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002812 BasicBlock *Default = getBasicBlock(Record[2]);
Craig Topper2617dcc2014-04-15 06:32:26 +00002813 if (!OpTy || !Cond || !Default)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002814 return Error(BitcodeError::InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002815 unsigned NumCases = (Record.size()-3)/2;
Gabor Greife9ecc682008-04-06 20:25:17 +00002816 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
Devang Patelaf206b82009-09-18 19:26:43 +00002817 InstructionList.push_back(SI);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002818 for (unsigned i = 0, e = NumCases; i != e; ++i) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002819 ConstantInt *CaseVal =
Chris Lattner5285b5e2007-05-02 05:46:45 +00002820 dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy));
2821 BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]);
Craig Topper2617dcc2014-04-15 06:32:26 +00002822 if (!CaseVal || !DestBB) {
Chris Lattner5285b5e2007-05-02 05:46:45 +00002823 delete SI;
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002824 return Error(BitcodeError::InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002825 }
2826 SI->addCase(CaseVal, DestBB);
2827 }
2828 I = SI;
2829 break;
2830 }
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00002831 case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...]
Chris Lattner3ed871f2009-10-27 19:13:16 +00002832 if (Record.size() < 2)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002833 return Error(BitcodeError::InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00002834 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002835 Value *Address = getValue(Record, 1, NextValueNo, OpTy);
Craig Topper2617dcc2014-04-15 06:32:26 +00002836 if (!OpTy || !Address)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002837 return Error(BitcodeError::InvalidRecord);
Chris Lattner3ed871f2009-10-27 19:13:16 +00002838 unsigned NumDests = Record.size()-2;
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00002839 IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests);
Chris Lattner3ed871f2009-10-27 19:13:16 +00002840 InstructionList.push_back(IBI);
2841 for (unsigned i = 0, e = NumDests; i != e; ++i) {
2842 if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) {
2843 IBI->addDestination(DestBB);
2844 } else {
2845 delete IBI;
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002846 return Error(BitcodeError::InvalidRecord);
Chris Lattner3ed871f2009-10-27 19:13:16 +00002847 }
2848 }
2849 I = IBI;
2850 break;
2851 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002852
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00002853 case bitc::FUNC_CODE_INST_INVOKE: {
2854 // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
Rafael Espindola48da4f42013-11-04 16:16:24 +00002855 if (Record.size() < 4)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002856 return Error(BitcodeError::InvalidRecord);
Bill Wendlinge94d8432012-12-07 23:16:57 +00002857 AttributeSet PAL = getAttributes(Record[0]);
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002858 unsigned CCInfo = Record[1];
2859 BasicBlock *NormalBB = getBasicBlock(Record[2]);
2860 BasicBlock *UnwindBB = getBasicBlock(Record[3]);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002861
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002862 unsigned OpNum = 4;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002863 Value *Callee;
2864 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002865 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002866
Chris Lattner229907c2011-07-18 04:54:35 +00002867 PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType());
Craig Topper2617dcc2014-04-15 06:32:26 +00002868 FunctionType *FTy = !CalleeTy ? nullptr :
Chris Lattner5285b5e2007-05-02 05:46:45 +00002869 dyn_cast<FunctionType>(CalleeTy->getElementType());
2870
2871 // Check that the right number of fixed parameters are here.
Craig Topper2617dcc2014-04-15 06:32:26 +00002872 if (!FTy || !NormalBB || !UnwindBB ||
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002873 Record.size() < OpNum+FTy->getNumParams())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002874 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002875
Chris Lattner5285b5e2007-05-02 05:46:45 +00002876 SmallVector<Value*, 16> Ops;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002877 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00002878 Ops.push_back(getValue(Record, OpNum, NextValueNo,
2879 FTy->getParamType(i)));
Craig Topper2617dcc2014-04-15 06:32:26 +00002880 if (!Ops.back())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002881 return Error(BitcodeError::InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002882 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002883
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002884 if (!FTy->isVarArg()) {
2885 if (Record.size() != OpNum)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002886 return Error(BitcodeError::InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002887 } else {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002888 // Read type/value pairs for varargs params.
2889 while (OpNum != Record.size()) {
2890 Value *Op;
2891 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002892 return Error(BitcodeError::InvalidRecord);
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002893 Ops.push_back(Op);
2894 }
Chris Lattner5285b5e2007-05-02 05:46:45 +00002895 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002896
Jay Foad5bd375a2011-07-15 08:37:34 +00002897 I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops);
Devang Patelaf206b82009-09-18 19:26:43 +00002898 InstructionList.push_back(I);
Sandeep Patel68c5f472009-09-02 08:44:58 +00002899 cast<InvokeInst>(I)->setCallingConv(
2900 static_cast<CallingConv::ID>(CCInfo));
Devang Patel4c758ea2008-09-25 21:00:45 +00002901 cast<InvokeInst>(I)->setAttributes(PAL);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002902 break;
2903 }
Bill Wendlingf891bf82011-07-31 06:30:59 +00002904 case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval]
2905 unsigned Idx = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +00002906 Value *Val = nullptr;
Bill Wendlingf891bf82011-07-31 06:30:59 +00002907 if (getValueTypePair(Record, Idx, NextValueNo, Val))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002908 return Error(BitcodeError::InvalidRecord);
Bill Wendlingf891bf82011-07-31 06:30:59 +00002909 I = ResumeInst::Create(Val);
Bill Wendlingb9a89992011-09-01 00:50:20 +00002910 InstructionList.push_back(I);
Bill Wendlingf891bf82011-07-31 06:30:59 +00002911 break;
2912 }
Chris Lattnere53603e2007-05-02 04:27:25 +00002913 case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE
Owen Anderson55f1c092009-08-13 21:58:54 +00002914 I = new UnreachableInst(Context);
Devang Patelaf206b82009-09-18 19:26:43 +00002915 InstructionList.push_back(I);
Chris Lattnere53603e2007-05-02 04:27:25 +00002916 break;
Chris Lattnere9759c22007-05-06 00:21:25 +00002917 case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...]
Chris Lattnere14cb882007-05-04 19:11:41 +00002918 if (Record.size() < 1 || ((Record.size()-1)&1))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002919 return Error(BitcodeError::InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00002920 Type *Ty = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00002921 if (!Ty)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002922 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002923
Jay Foad52131342011-03-30 11:28:46 +00002924 PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2);
Devang Patelaf206b82009-09-18 19:26:43 +00002925 InstructionList.push_back(PN);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002926
Chris Lattnere14cb882007-05-04 19:11:41 +00002927 for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00002928 Value *V;
2929 // With the new function encoding, it is possible that operands have
2930 // negative IDs (for forward references). Use a signed VBR
2931 // representation to keep the encoding small.
2932 if (UseRelativeIDs)
2933 V = getValueSigned(Record, 1+i, NextValueNo, Ty);
2934 else
2935 V = getValue(Record, 1+i, NextValueNo, Ty);
Chris Lattnere14cb882007-05-04 19:11:41 +00002936 BasicBlock *BB = getBasicBlock(Record[2+i]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00002937 if (!V || !BB)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002938 return Error(BitcodeError::InvalidRecord);
Chris Lattnerc332bba2007-05-03 18:58:09 +00002939 PN->addIncoming(V, BB);
2940 }
2941 I = PN;
2942 break;
2943 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002944
Bill Wendlingfae14752011-08-12 20:24:12 +00002945 case bitc::FUNC_CODE_INST_LANDINGPAD: {
2946 // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?]
2947 unsigned Idx = 0;
2948 if (Record.size() < 4)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002949 return Error(BitcodeError::InvalidRecord);
Bill Wendlingfae14752011-08-12 20:24:12 +00002950 Type *Ty = getTypeByID(Record[Idx++]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00002951 if (!Ty)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002952 return Error(BitcodeError::InvalidRecord);
Craig Topper2617dcc2014-04-15 06:32:26 +00002953 Value *PersFn = nullptr;
Bill Wendlingfae14752011-08-12 20:24:12 +00002954 if (getValueTypePair(Record, Idx, NextValueNo, PersFn))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002955 return Error(BitcodeError::InvalidRecord);
Bill Wendlingfae14752011-08-12 20:24:12 +00002956
2957 bool IsCleanup = !!Record[Idx++];
2958 unsigned NumClauses = Record[Idx++];
2959 LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, NumClauses);
2960 LP->setCleanup(IsCleanup);
2961 for (unsigned J = 0; J != NumClauses; ++J) {
2962 LandingPadInst::ClauseType CT =
2963 LandingPadInst::ClauseType(Record[Idx++]); (void)CT;
2964 Value *Val;
2965
2966 if (getValueTypePair(Record, Idx, NextValueNo, Val)) {
2967 delete LP;
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002968 return Error(BitcodeError::InvalidRecord);
Bill Wendlingfae14752011-08-12 20:24:12 +00002969 }
2970
2971 assert((CT != LandingPadInst::Catch ||
2972 !isa<ArrayType>(Val->getType())) &&
2973 "Catch clause has a invalid type!");
2974 assert((CT != LandingPadInst::Filter ||
2975 isa<ArrayType>(Val->getType())) &&
2976 "Filter clause has invalid type!");
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +00002977 LP->addClause(cast<Constant>(Val));
Bill Wendlingfae14752011-08-12 20:24:12 +00002978 }
2979
2980 I = LP;
Bill Wendlingb9a89992011-09-01 00:50:20 +00002981 InstructionList.push_back(I);
Bill Wendlingfae14752011-08-12 20:24:12 +00002982 break;
2983 }
2984
Chris Lattnerf1c87102011-06-17 18:09:11 +00002985 case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]
2986 if (Record.size() != 4)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002987 return Error(BitcodeError::InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00002988 PointerType *Ty =
Chris Lattnerc332bba2007-05-03 18:58:09 +00002989 dyn_cast_or_null<PointerType>(getTypeByID(Record[0]));
Chris Lattner229907c2011-07-18 04:54:35 +00002990 Type *OpTy = getTypeByID(Record[1]);
Chris Lattnerf1c87102011-06-17 18:09:11 +00002991 Value *Size = getFnValueByID(Record[2], OpTy);
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002992 unsigned AlignRecord = Record[3];
2993 bool InAlloca = AlignRecord & (1 << 5);
2994 unsigned Align = AlignRecord & ((1 << 5) - 1);
Rafael Espindola48da4f42013-11-04 16:16:24 +00002995 if (!Ty || !Size)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002996 return Error(BitcodeError::InvalidRecord);
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002997 AllocaInst *AI = new AllocaInst(Ty->getElementType(), Size, (1 << Align) >> 1);
2998 AI->setUsedWithInAlloca(InAlloca);
2999 I = AI;
Devang Patelaf206b82009-09-18 19:26:43 +00003000 InstructionList.push_back(I);
Chris Lattnerc332bba2007-05-03 18:58:09 +00003001 break;
3002 }
Chris Lattner9f600c52007-05-03 22:04:19 +00003003 case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol]
Chris Lattnerdf1233d2007-05-06 00:00:00 +00003004 unsigned OpNum = 0;
3005 Value *Op;
3006 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
3007 OpNum+2 != Record.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003008 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003009
Chris Lattnerdf1233d2007-05-06 00:00:00 +00003010 I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1);
Devang Patelaf206b82009-09-18 19:26:43 +00003011 InstructionList.push_back(I);
Chris Lattner83930552007-05-01 07:01:57 +00003012 break;
Chris Lattner9f600c52007-05-03 22:04:19 +00003013 }
Eli Friedman59b66882011-08-09 23:02:53 +00003014 case bitc::FUNC_CODE_INST_LOADATOMIC: {
3015 // LOADATOMIC: [opty, op, align, vol, ordering, synchscope]
3016 unsigned OpNum = 0;
3017 Value *Op;
3018 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
3019 OpNum+4 != Record.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003020 return Error(BitcodeError::InvalidRecord);
Eli Friedman59b66882011-08-09 23:02:53 +00003021
3022 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
3023 if (Ordering == NotAtomic || Ordering == Release ||
3024 Ordering == AcquireRelease)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003025 return Error(BitcodeError::InvalidRecord);
Eli Friedman59b66882011-08-09 23:02:53 +00003026 if (Ordering != NotAtomic && Record[OpNum] == 0)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003027 return Error(BitcodeError::InvalidRecord);
Eli Friedman59b66882011-08-09 23:02:53 +00003028 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
3029
3030 I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1,
3031 Ordering, SynchScope);
3032 InstructionList.push_back(I);
3033 break;
3034 }
Chris Lattnerc44070802011-06-17 18:17:37 +00003035 case bitc::FUNC_CODE_INST_STORE: { // STORE2:[ptrty, ptr, val, align, vol]
Christopher Lamb54dd24c2007-12-11 08:59:05 +00003036 unsigned OpNum = 0;
3037 Value *Val, *Ptr;
3038 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00003039 popValue(Record, OpNum, NextValueNo,
Christopher Lamb54dd24c2007-12-11 08:59:05 +00003040 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
3041 OpNum+2 != Record.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003042 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003043
Christopher Lamb54dd24c2007-12-11 08:59:05 +00003044 I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1);
Devang Patelaf206b82009-09-18 19:26:43 +00003045 InstructionList.push_back(I);
Christopher Lamb54dd24c2007-12-11 08:59:05 +00003046 break;
3047 }
Eli Friedman59b66882011-08-09 23:02:53 +00003048 case bitc::FUNC_CODE_INST_STOREATOMIC: {
3049 // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, synchscope]
3050 unsigned OpNum = 0;
3051 Value *Val, *Ptr;
3052 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00003053 popValue(Record, OpNum, NextValueNo,
Eli Friedman59b66882011-08-09 23:02:53 +00003054 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
3055 OpNum+4 != Record.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003056 return Error(BitcodeError::InvalidRecord);
Eli Friedman59b66882011-08-09 23:02:53 +00003057
3058 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
Eli Friedman222b5a42011-09-19 19:41:28 +00003059 if (Ordering == NotAtomic || Ordering == Acquire ||
Eli Friedman59b66882011-08-09 23:02:53 +00003060 Ordering == AcquireRelease)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003061 return Error(BitcodeError::InvalidRecord);
Eli Friedman59b66882011-08-09 23:02:53 +00003062 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
3063 if (Ordering != NotAtomic && Record[OpNum] == 0)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003064 return Error(BitcodeError::InvalidRecord);
Eli Friedman59b66882011-08-09 23:02:53 +00003065
3066 I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1,
3067 Ordering, SynchScope);
3068 InstructionList.push_back(I);
3069 break;
3070 }
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003071 case bitc::FUNC_CODE_INST_CMPXCHG: {
Tim Northovere94a5182014-03-11 10:48:52 +00003072 // CMPXCHG:[ptrty, ptr, cmp, new, vol, successordering, synchscope,
Tim Northover420a2162014-06-13 14:24:07 +00003073 // failureordering?, isweak?]
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003074 unsigned OpNum = 0;
3075 Value *Ptr, *Cmp, *New;
3076 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00003077 popValue(Record, OpNum, NextValueNo,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003078 cast<PointerType>(Ptr->getType())->getElementType(), Cmp) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00003079 popValue(Record, OpNum, NextValueNo,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003080 cast<PointerType>(Ptr->getType())->getElementType(), New) ||
Tim Northover420a2162014-06-13 14:24:07 +00003081 (Record.size() < OpNum + 3 || Record.size() > OpNum + 5))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003082 return Error(BitcodeError::InvalidRecord);
Tim Northovere94a5182014-03-11 10:48:52 +00003083 AtomicOrdering SuccessOrdering = GetDecodedOrdering(Record[OpNum+1]);
3084 if (SuccessOrdering == NotAtomic || SuccessOrdering == Unordered)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003085 return Error(BitcodeError::InvalidRecord);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003086 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+2]);
Tim Northovere94a5182014-03-11 10:48:52 +00003087
3088 AtomicOrdering FailureOrdering;
3089 if (Record.size() < 7)
3090 FailureOrdering =
3091 AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering);
3092 else
3093 FailureOrdering = GetDecodedOrdering(Record[OpNum+3]);
3094
3095 I = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering, FailureOrdering,
3096 SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003097 cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]);
Tim Northover420a2162014-06-13 14:24:07 +00003098
3099 if (Record.size() < 8) {
3100 // Before weak cmpxchgs existed, the instruction simply returned the
3101 // value loaded from memory, so bitcode files from that era will be
3102 // expecting the first component of a modern cmpxchg.
3103 CurBB->getInstList().push_back(I);
3104 I = ExtractValueInst::Create(I, 0);
3105 } else {
3106 cast<AtomicCmpXchgInst>(I)->setWeak(Record[OpNum+4]);
3107 }
3108
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003109 InstructionList.push_back(I);
3110 break;
3111 }
3112 case bitc::FUNC_CODE_INST_ATOMICRMW: {
3113 // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, synchscope]
3114 unsigned OpNum = 0;
3115 Value *Ptr, *Val;
3116 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00003117 popValue(Record, OpNum, NextValueNo,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003118 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
3119 OpNum+4 != Record.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003120 return Error(BitcodeError::InvalidRecord);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003121 AtomicRMWInst::BinOp Operation = GetDecodedRMWOperation(Record[OpNum]);
3122 if (Operation < AtomicRMWInst::FIRST_BINOP ||
3123 Operation > AtomicRMWInst::LAST_BINOP)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003124 return Error(BitcodeError::InvalidRecord);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003125 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
Eli Friedman59b66882011-08-09 23:02:53 +00003126 if (Ordering == NotAtomic || Ordering == Unordered)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003127 return Error(BitcodeError::InvalidRecord);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003128 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
3129 I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SynchScope);
3130 cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]);
3131 InstructionList.push_back(I);
3132 break;
3133 }
Eli Friedmanfee02c62011-07-25 23:16:38 +00003134 case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, synchscope]
3135 if (2 != Record.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003136 return Error(BitcodeError::InvalidRecord);
Eli Friedmanfee02c62011-07-25 23:16:38 +00003137 AtomicOrdering Ordering = GetDecodedOrdering(Record[0]);
3138 if (Ordering == NotAtomic || Ordering == Unordered ||
3139 Ordering == Monotonic)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003140 return Error(BitcodeError::InvalidRecord);
Eli Friedmanfee02c62011-07-25 23:16:38 +00003141 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[1]);
3142 I = new FenceInst(Context, Ordering, SynchScope);
3143 InstructionList.push_back(I);
3144 break;
3145 }
Chris Lattnerc44070802011-06-17 18:17:37 +00003146 case bitc::FUNC_CODE_INST_CALL: {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00003147 // CALL: [paramattrs, cc, fnty, fnid, arg0, arg1...]
3148 if (Record.size() < 3)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003149 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003150
Bill Wendlinge94d8432012-12-07 23:16:57 +00003151 AttributeSet PAL = getAttributes(Record[0]);
Chris Lattner4c0a6d62007-05-08 05:38:01 +00003152 unsigned CCInfo = Record[1];
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003153
Chris Lattner4c0a6d62007-05-08 05:38:01 +00003154 unsigned OpNum = 2;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00003155 Value *Callee;
3156 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003157 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003158
Chris Lattner229907c2011-07-18 04:54:35 +00003159 PointerType *OpTy = dyn_cast<PointerType>(Callee->getType());
Craig Topper2617dcc2014-04-15 06:32:26 +00003160 FunctionType *FTy = nullptr;
Chris Lattner9f600c52007-05-03 22:04:19 +00003161 if (OpTy) FTy = dyn_cast<FunctionType>(OpTy->getElementType());
Chris Lattnerdf1233d2007-05-06 00:00:00 +00003162 if (!FTy || Record.size() < FTy->getNumParams()+OpNum)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003163 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003164
Chris Lattner9f600c52007-05-03 22:04:19 +00003165 SmallVector<Value*, 16> Args;
3166 // Read the fixed params.
Chris Lattnerdf1233d2007-05-06 00:00:00 +00003167 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003168 if (FTy->getParamType(i)->isLabelTy())
Dale Johannesen4646aa32007-11-05 21:20:28 +00003169 Args.push_back(getBasicBlock(Record[OpNum]));
Dan Gohmanbbcd04d2010-09-13 18:00:48 +00003170 else
Jan Wen Voungafaced02012-10-11 20:20:40 +00003171 Args.push_back(getValue(Record, OpNum, NextValueNo,
3172 FTy->getParamType(i)));
Craig Topper2617dcc2014-04-15 06:32:26 +00003173 if (!Args.back())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003174 return Error(BitcodeError::InvalidRecord);
Chris Lattner9f600c52007-05-03 22:04:19 +00003175 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003176
Chris Lattner9f600c52007-05-03 22:04:19 +00003177 // Read type/value pairs for varargs params.
Chris Lattner9f600c52007-05-03 22:04:19 +00003178 if (!FTy->isVarArg()) {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00003179 if (OpNum != Record.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003180 return Error(BitcodeError::InvalidRecord);
Chris Lattner9f600c52007-05-03 22:04:19 +00003181 } else {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00003182 while (OpNum != Record.size()) {
3183 Value *Op;
3184 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003185 return Error(BitcodeError::InvalidRecord);
Chris Lattnerdf1233d2007-05-06 00:00:00 +00003186 Args.push_back(Op);
Chris Lattner9f600c52007-05-03 22:04:19 +00003187 }
3188 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003189
Jay Foad5bd375a2011-07-15 08:37:34 +00003190 I = CallInst::Create(Callee, Args);
Devang Patelaf206b82009-09-18 19:26:43 +00003191 InstructionList.push_back(I);
Sandeep Patel68c5f472009-09-02 08:44:58 +00003192 cast<CallInst>(I)->setCallingConv(
Reid Kleckner5772b772014-04-24 20:14:34 +00003193 static_cast<CallingConv::ID>((~(1U << 14) & CCInfo) >> 1));
3194 CallInst::TailCallKind TCK = CallInst::TCK_None;
3195 if (CCInfo & 1)
3196 TCK = CallInst::TCK_Tail;
3197 if (CCInfo & (1 << 14))
3198 TCK = CallInst::TCK_MustTail;
3199 cast<CallInst>(I)->setTailCallKind(TCK);
Devang Patel4c758ea2008-09-25 21:00:45 +00003200 cast<CallInst>(I)->setAttributes(PAL);
Chris Lattner9f600c52007-05-03 22:04:19 +00003201 break;
3202 }
3203 case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty]
3204 if (Record.size() < 3)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003205 return Error(BitcodeError::InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00003206 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00003207 Value *Op = getValue(Record, 1, NextValueNo, OpTy);
Chris Lattner229907c2011-07-18 04:54:35 +00003208 Type *ResTy = getTypeByID(Record[2]);
Chris Lattner9f600c52007-05-03 22:04:19 +00003209 if (!OpTy || !Op || !ResTy)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003210 return Error(BitcodeError::InvalidRecord);
Chris Lattner9f600c52007-05-03 22:04:19 +00003211 I = new VAArgInst(Op, ResTy);
Devang Patelaf206b82009-09-18 19:26:43 +00003212 InstructionList.push_back(I);
Chris Lattner9f600c52007-05-03 22:04:19 +00003213 break;
3214 }
Chris Lattner83930552007-05-01 07:01:57 +00003215 }
3216
3217 // Add instruction to end of current BB. If there is no current BB, reject
3218 // this file.
Craig Topper2617dcc2014-04-15 06:32:26 +00003219 if (!CurBB) {
Chris Lattner83930552007-05-01 07:01:57 +00003220 delete I;
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003221 return Error(BitcodeError::InvalidInstructionWithNoBB);
Chris Lattner83930552007-05-01 07:01:57 +00003222 }
3223 CurBB->getInstList().push_back(I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003224
Chris Lattner83930552007-05-01 07:01:57 +00003225 // If this was a terminator instruction, move to the next block.
3226 if (isa<TerminatorInst>(I)) {
3227 ++CurBBNo;
Craig Topper2617dcc2014-04-15 06:32:26 +00003228 CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr;
Chris Lattner83930552007-05-01 07:01:57 +00003229 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003230
Chris Lattner83930552007-05-01 07:01:57 +00003231 // Non-void values get registered in the value table for future use.
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00003232 if (I && !I->getType()->isVoidTy())
Chris Lattner83930552007-05-01 07:01:57 +00003233 ValueList.AssignValue(I, NextValueNo++);
Chris Lattner85b7b402007-05-01 05:52:21 +00003234 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003235
Chris Lattner27d38752013-01-20 02:13:19 +00003236OutOfRecordLoop:
Joe Abbey97b7a172013-02-06 22:14:06 +00003237
Chris Lattner83930552007-05-01 07:01:57 +00003238 // Check the function list for unresolved values.
3239 if (Argument *A = dyn_cast<Argument>(ValueList.back())) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003240 if (!A->getParent()) {
Chris Lattner83930552007-05-01 07:01:57 +00003241 // We found at least one unresolved value. Nuke them all to avoid leaks.
3242 for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){
Craig Topper2617dcc2014-04-15 06:32:26 +00003243 if ((A = dyn_cast_or_null<Argument>(ValueList[i])) && !A->getParent()) {
Owen Andersonb292b8c2009-07-30 23:03:37 +00003244 A->replaceAllUsesWith(UndefValue::get(A->getType()));
Chris Lattner83930552007-05-01 07:01:57 +00003245 delete A;
3246 }
3247 }
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003248 return Error(BitcodeError::NeverResolvedValueFoundInFunction);
Chris Lattner83930552007-05-01 07:01:57 +00003249 }
Chris Lattner83930552007-05-01 07:01:57 +00003250 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003251
Dan Gohman9b9ff462010-08-25 20:23:38 +00003252 // FIXME: Check for unresolved forward-declared metadata references
3253 // and clean up leaks.
3254
Chris Lattner85b7b402007-05-01 05:52:21 +00003255 // Trim the value list down to the size it was before we parsed this function.
3256 ValueList.shrinkTo(ModuleValueListSize);
Dan Gohman26d837d2010-08-25 20:22:53 +00003257 MDValueList.shrinkTo(ModuleMDValueListSize);
Chris Lattner85b7b402007-05-01 05:52:21 +00003258 std::vector<BasicBlock*>().swap(FunctionBBs);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003259 return std::error_code();
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003260}
3261
Rafael Espindola7d712032013-11-05 17:16:08 +00003262/// Find the function body in the bitcode stream
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003263std::error_code BitcodeReader::FindFunctionInStream(
3264 Function *F,
3265 DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003266 while (DeferredFunctionInfoIterator->second == 0) {
3267 if (Stream.AtEndOfStream())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003268 return Error(BitcodeError::CouldNotFindFunctionInStream);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003269 // ParseModule will parse the next body in the stream and set its
3270 // position in the DeferredFunctionInfo map.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003271 if (std::error_code EC = ParseModule(true))
Rafael Espindola7d712032013-11-05 17:16:08 +00003272 return EC;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003273 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003274 return std::error_code();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003275}
3276
Chris Lattner9eeada92007-05-18 04:02:46 +00003277//===----------------------------------------------------------------------===//
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003278// GVMaterializer implementation
Chris Lattner9eeada92007-05-18 04:02:46 +00003279//===----------------------------------------------------------------------===//
3280
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +00003281void BitcodeReader::releaseBuffer() { Buffer.release(); }
Chris Lattner9eeada92007-05-18 04:02:46 +00003282
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003283bool BitcodeReader::isMaterializable(const GlobalValue *GV) const {
3284 if (const Function *F = dyn_cast<Function>(GV)) {
3285 return F->isDeclaration() &&
3286 DeferredFunctionInfo.count(const_cast<Function*>(F));
3287 }
3288 return false;
3289}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003290
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003291std::error_code BitcodeReader::Materialize(GlobalValue *GV) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003292 Function *F = dyn_cast<Function>(GV);
3293 // If it's not a function or is already material, ignore the request.
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003294 if (!F || !F->isMaterializable())
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003295 return std::error_code();
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003296
3297 DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F);
Chris Lattner9eeada92007-05-18 04:02:46 +00003298 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003299 // If its position is recorded as 0, its body is somewhere in the stream
3300 // but we haven't seen it yet.
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003301 if (DFII->second == 0 && LazyStreamer)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003302 if (std::error_code EC = FindFunctionInStream(F, DFII))
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003303 return EC;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003304
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003305 // Move the bit stream to the saved position of the deferred function body.
3306 Stream.JumpToBit(DFII->second);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003307
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003308 if (std::error_code EC = ParseFunctionBody(F))
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003309 return EC;
Chandler Carruth7132e002007-08-04 01:51:18 +00003310
3311 // Upgrade any old intrinsic calls in the function.
3312 for (UpgradedIntrinsicMap::iterator I = UpgradedIntrinsics.begin(),
3313 E = UpgradedIntrinsics.end(); I != E; ++I) {
3314 if (I->first != I->second) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00003315 for (auto UI = I->first->user_begin(), UE = I->first->user_end();
3316 UI != UE;) {
Chandler Carruth7132e002007-08-04 01:51:18 +00003317 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
3318 UpgradeIntrinsicCall(CI, I->second);
3319 }
3320 }
3321 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003322
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00003323 // Bring in any functions that this function forward-referenced via
3324 // blockaddresses.
3325 return materializeForwardReferencedFunctions();
Chris Lattner9eeada92007-05-18 04:02:46 +00003326}
3327
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003328bool BitcodeReader::isDematerializable(const GlobalValue *GV) const {
3329 const Function *F = dyn_cast<Function>(GV);
3330 if (!F || F->isDeclaration())
3331 return false;
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00003332
3333 // Dematerializing F would leave dangling references that wouldn't be
3334 // reconnected on re-materialization.
3335 if (BlockAddressesTaken.count(F))
3336 return false;
3337
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003338 return DeferredFunctionInfo.count(const_cast<Function*>(F));
3339}
3340
3341void BitcodeReader::Dematerialize(GlobalValue *GV) {
3342 Function *F = dyn_cast<Function>(GV);
3343 // If this function isn't dematerializable, this is a noop.
3344 if (!F || !isDematerializable(F))
Chris Lattner9eeada92007-05-18 04:02:46 +00003345 return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003346
Chris Lattner9eeada92007-05-18 04:02:46 +00003347 assert(DeferredFunctionInfo.count(F) && "No info to read function later?");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003348
Chris Lattner9eeada92007-05-18 04:02:46 +00003349 // Just forget the function body, we can remat it later.
3350 F->deleteBody();
Chris Lattner9eeada92007-05-18 04:02:46 +00003351}
3352
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003353std::error_code BitcodeReader::MaterializeModule(Module *M) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003354 assert(M == TheModule &&
3355 "Can only Materialize the Module this BitcodeReader is attached to.");
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00003356
3357 // Promise to materialize all forward references.
3358 WillMaterializeAllForwardRefs = true;
3359
Chris Lattner06310bf2009-06-16 05:15:21 +00003360 // Iterate over the module, deserializing any functions that are still on
3361 // disk.
3362 for (Module::iterator F = TheModule->begin(), E = TheModule->end();
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003363 F != E; ++F) {
3364 if (F->isMaterializable()) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003365 if (std::error_code EC = Materialize(F))
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003366 return EC;
3367 }
3368 }
Derek Schuff92ef9752012-02-29 00:07:09 +00003369 // At this point, if there are any function bodies, the current bit is
3370 // pointing to the END_BLOCK record after them. Now make sure the rest
3371 // of the bits in the module have been read.
3372 if (NextUnreadBit)
3373 ParseModule(true);
3374
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00003375 // Check that all block address forward references got resolved (as we
3376 // promised above).
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00003377 if (!BasicBlockFwdRefs.empty())
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00003378 return Error(BitcodeError::NeverResolvedFunctionFromBlockAddress);
3379
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003380 // Upgrade any intrinsic calls that slipped through (should not happen!) and
3381 // delete the old functions to clean up. We can't do this unless the entire
3382 // module is materialized because there could always be another function body
Chandler Carruth7132e002007-08-04 01:51:18 +00003383 // with calls to the old function.
3384 for (std::vector<std::pair<Function*, Function*> >::iterator I =
3385 UpgradedIntrinsics.begin(), E = UpgradedIntrinsics.end(); I != E; ++I) {
3386 if (I->first != I->second) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00003387 for (auto UI = I->first->user_begin(), UE = I->first->user_end();
3388 UI != UE;) {
Chandler Carruth7132e002007-08-04 01:51:18 +00003389 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
3390 UpgradeIntrinsicCall(CI, I->second);
3391 }
Chris Lattner647cffb2009-04-01 01:43:03 +00003392 if (!I->first->use_empty())
3393 I->first->replaceAllUsesWith(I->second);
Chandler Carruth7132e002007-08-04 01:51:18 +00003394 I->first->eraseFromParent();
3395 }
3396 }
3397 std::vector<std::pair<Function*, Function*> >().swap(UpgradedIntrinsics);
Devang Patel80ae3492009-08-28 23:24:31 +00003398
Manman Ren209b17c2013-09-28 00:22:27 +00003399 for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++)
3400 UpgradeInstWithTBAATag(InstsWithTBAATag[I]);
3401
Manman Ren8b4306c2013-12-02 21:29:56 +00003402 UpgradeDebugInfo(*M);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003403 return std::error_code();
Chris Lattner9eeada92007-05-18 04:02:46 +00003404}
3405
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003406std::error_code BitcodeReader::InitStream() {
Rafael Espindola48da4f42013-11-04 16:16:24 +00003407 if (LazyStreamer)
3408 return InitLazyStream();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003409 return InitStreamFromBuffer();
3410}
3411
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003412std::error_code BitcodeReader::InitStreamFromBuffer() {
Roman Divacky4717a8d2012-09-06 15:42:13 +00003413 const unsigned char *BufPtr = (const unsigned char*)Buffer->getBufferStart();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003414 const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
3415
Rafael Espindola27435252014-07-29 21:01:24 +00003416 if (Buffer->getBufferSize() & 3)
3417 return Error(BitcodeError::InvalidBitcodeSignature);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003418
3419 // If we have a wrapper header, parse it and ignore the non-bc file contents.
3420 // The magic number is 0x0B17C0DE stored in little endian.
3421 if (isBitcodeWrapper(BufPtr, BufEnd))
3422 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003423 return Error(BitcodeError::InvalidBitcodeWrapperHeader);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003424
3425 StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
3426 Stream.init(*StreamFile);
3427
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003428 return std::error_code();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003429}
3430
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003431std::error_code BitcodeReader::InitLazyStream() {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003432 // Check and strip off the bitcode wrapper; BitstreamReader expects never to
3433 // see it.
3434 StreamingMemoryObject *Bytes = new StreamingMemoryObject(LazyStreamer);
3435 StreamFile.reset(new BitstreamReader(Bytes));
3436 Stream.init(*StreamFile);
3437
3438 unsigned char buf[16];
Benjamin Kramer534d3a42013-05-24 10:54:58 +00003439 if (Bytes->readBytes(0, 16, buf) == -1)
Rafael Espindola27435252014-07-29 21:01:24 +00003440 return Error(BitcodeError::InvalidBitcodeSignature);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003441
3442 if (!isBitcode(buf, buf + 16))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003443 return Error(BitcodeError::InvalidBitcodeSignature);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003444
3445 if (isBitcodeWrapper(buf, buf + 4)) {
3446 const unsigned char *bitcodeStart = buf;
3447 const unsigned char *bitcodeEnd = buf + 16;
3448 SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
3449 Bytes->dropLeadingBytes(bitcodeStart - buf);
3450 Bytes->setKnownObjectSize(bitcodeEnd - bitcodeStart);
3451 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003452 return std::error_code();
Rafael Espindola48da4f42013-11-04 16:16:24 +00003453}
3454
3455namespace {
Rafael Espindola25188c92014-06-12 01:45:43 +00003456class BitcodeErrorCategoryType : public std::error_category {
Rafael Espindolaf5d07fa2014-06-10 21:26:47 +00003457 const char *name() const LLVM_NOEXCEPT override {
Rafael Espindola48da4f42013-11-04 16:16:24 +00003458 return "llvm.bitcode";
3459 }
Craig Topper73156022014-03-02 09:09:27 +00003460 std::string message(int IE) const override {
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003461 BitcodeError E = static_cast<BitcodeError>(IE);
Rafael Espindola48da4f42013-11-04 16:16:24 +00003462 switch (E) {
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003463 case BitcodeError::ConflictingMETADATA_KINDRecords:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003464 return "Conflicting METADATA_KIND records";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003465 case BitcodeError::CouldNotFindFunctionInStream:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003466 return "Could not find function in stream";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003467 case BitcodeError::ExpectedConstant:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003468 return "Expected a constant";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003469 case BitcodeError::InsufficientFunctionProtos:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003470 return "Insufficient function protos";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003471 case BitcodeError::InvalidBitcodeSignature:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003472 return "Invalid bitcode signature";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003473 case BitcodeError::InvalidBitcodeWrapperHeader:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003474 return "Invalid bitcode wrapper header";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003475 case BitcodeError::InvalidConstantReference:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003476 return "Invalid ronstant reference";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003477 case BitcodeError::InvalidID:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003478 return "Invalid ID";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003479 case BitcodeError::InvalidInstructionWithNoBB:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003480 return "Invalid instruction with no BB";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003481 case BitcodeError::InvalidRecord:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003482 return "Invalid record";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003483 case BitcodeError::InvalidTypeForValue:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003484 return "Invalid type for value";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003485 case BitcodeError::InvalidTYPETable:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003486 return "Invalid TYPE table";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003487 case BitcodeError::InvalidType:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003488 return "Invalid type";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003489 case BitcodeError::MalformedBlock:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003490 return "Malformed block";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003491 case BitcodeError::MalformedGlobalInitializerSet:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003492 return "Malformed global initializer set";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003493 case BitcodeError::InvalidMultipleBlocks:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003494 return "Invalid multiple blocks";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003495 case BitcodeError::NeverResolvedValueFoundInFunction:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003496 return "Never resolved value found in function";
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00003497 case BitcodeError::NeverResolvedFunctionFromBlockAddress:
3498 return "Never resolved function from blockaddress";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003499 case BitcodeError::InvalidValue:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003500 return "Invalid value";
3501 }
Benjamin Kramer77db1632013-11-05 13:45:09 +00003502 llvm_unreachable("Unknown error type!");
Rafael Espindola48da4f42013-11-04 16:16:24 +00003503 }
3504};
3505}
3506
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003507const std::error_category &llvm::BitcodeErrorCategory() {
Rafael Espindola48da4f42013-11-04 16:16:24 +00003508 static BitcodeErrorCategoryType O;
3509 return O;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003510}
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003511
Chris Lattner6694f602007-04-29 07:54:31 +00003512//===----------------------------------------------------------------------===//
3513// External interface
3514//===----------------------------------------------------------------------===//
3515
Duncan P. N. Exon Smith6e1009b2014-08-01 22:27:19 +00003516/// \brief Get a lazy one-at-time loading module from bitcode.
Chris Lattner6694f602007-04-29 07:54:31 +00003517///
Duncan P. N. Exon Smith6e1009b2014-08-01 22:27:19 +00003518/// This isn't always used in a lazy context. In particular, it's also used by
3519/// \a parseBitcodeFile(). If this is truly lazy, then we need to eagerly pull
3520/// in forward-referenced functions from block address references.
3521///
3522/// \param[in] WillMaterializeAll Set to \c true if the caller promises to
3523/// materialize everything -- in particular, if this isn't truly lazy.
3524static ErrorOr<Module *> getLazyBitcodeModuleImpl(MemoryBuffer *Buffer,
3525 LLVMContext &Context,
3526 bool WillMaterializeAll) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003527 Module *M = new Module(Buffer->getBufferIdentifier(), Context);
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +00003528 BitcodeReader *R = new BitcodeReader(Buffer, Context);
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003529 M->setMaterializer(R);
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00003530
3531 auto cleanupOnError = [&](std::error_code EC) {
Rafael Espindola8fb31112014-06-18 20:07:35 +00003532 R->releaseBuffer(); // Never take ownership on error.
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003533 delete M; // Also deletes R.
Rafael Espindola5b6c1e82014-01-13 18:31:04 +00003534 return EC;
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00003535 };
Rafael Espindolab7993462012-01-02 07:49:53 +00003536
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00003537 if (std::error_code EC = R->ParseBitcodeInto(M))
3538 return cleanupOnError(EC);
3539
Duncan P. N. Exon Smith6e1009b2014-08-01 22:27:19 +00003540 if (!WillMaterializeAll)
3541 // Resolve forward references from blockaddresses.
3542 if (std::error_code EC = R->materializeForwardReferencedFunctions())
3543 return cleanupOnError(EC);
Rafael Espindolab7993462012-01-02 07:49:53 +00003544
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003545 return M;
Chris Lattner6694f602007-04-29 07:54:31 +00003546}
3547
Duncan P. N. Exon Smith6e1009b2014-08-01 22:27:19 +00003548ErrorOr<Module *> llvm::getLazyBitcodeModule(MemoryBuffer *Buffer,
3549 LLVMContext &Context) {
3550 return getLazyBitcodeModuleImpl(Buffer, Context, false);
3551}
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003552
3553Module *llvm::getStreamedBitcodeModule(const std::string &name,
3554 DataStreamer *streamer,
3555 LLVMContext &Context,
3556 std::string *ErrMsg) {
3557 Module *M = new Module(name, Context);
3558 BitcodeReader *R = new BitcodeReader(streamer, Context);
3559 M->setMaterializer(R);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003560 if (std::error_code EC = R->ParseBitcodeInto(M)) {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003561 if (ErrMsg)
Rafael Espindola48da4f42013-11-04 16:16:24 +00003562 *ErrMsg = EC.message();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003563 delete M; // Also deletes R.
Craig Topper2617dcc2014-04-15 06:32:26 +00003564 return nullptr;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003565 }
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003566 return M;
3567}
3568
Alp Tokerde4c0092014-06-27 09:19:14 +00003569ErrorOr<Module *> llvm::parseBitcodeFile(MemoryBuffer *Buffer,
Rafael Espindola8f31e212014-01-15 01:08:23 +00003570 LLVMContext &Context) {
Duncan P. N. Exon Smith6e1009b2014-08-01 22:27:19 +00003571 ErrorOr<Module *> ModuleOrErr =
3572 getLazyBitcodeModuleImpl(Buffer, Context, true);
Rafael Espindola8f31e212014-01-15 01:08:23 +00003573 if (!ModuleOrErr)
3574 return ModuleOrErr;
Rafael Espindola5b6c1e82014-01-13 18:31:04 +00003575 Module *M = ModuleOrErr.get();
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003576 // Read in the entire module, and destroy the BitcodeReader.
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +00003577 if (std::error_code EC = M->materializeAllPermanently(true)) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003578 delete M;
Rafael Espindola8f31e212014-01-15 01:08:23 +00003579 return EC;
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003580 }
Bill Wendling0198ce02010-10-06 01:22:42 +00003581
Chad Rosierca2567b2011-12-07 21:44:12 +00003582 // TODO: Restore the use-lists to the in-memory state when the bitcode was
3583 // written. We must defer until the Module has been fully materialized.
3584
Chris Lattner6694f602007-04-29 07:54:31 +00003585 return M;
3586}
Bill Wendling0198ce02010-10-06 01:22:42 +00003587
Rafael Espindolac75c4fa2014-07-04 20:02:42 +00003588std::string llvm::getBitcodeTargetTriple(MemoryBuffer *Buffer,
3589 LLVMContext &Context) {
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +00003590 BitcodeReader *R = new BitcodeReader(Buffer, Context);
Rafael Espindolac75c4fa2014-07-04 20:02:42 +00003591 ErrorOr<std::string> Triple = R->parseTriple();
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +00003592 R->releaseBuffer();
Bill Wendling0198ce02010-10-06 01:22:42 +00003593 delete R;
Rafael Espindolad346cc82014-07-04 13:52:01 +00003594 if (Triple.getError())
3595 return "";
3596 return Triple.get();
Bill Wendling0198ce02010-10-06 01:22:42 +00003597}