blob: e5dfa723b04df5c610c4a74767ed15bbe7774774 [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 Smith5a511b52014-08-05 17:49:48 +00001638 auto &FwdBBs = BasicBlockFwdRefs[Fn];
1639 if (FwdBBs.empty())
1640 BasicBlockFwdRefQueue.push_back(Fn);
Duncan P. N. Exon Smith5a5fd7b2014-08-16 01:54:37 +00001641 if (FwdBBs.size() < BBID + 1)
1642 FwdBBs.resize(BBID + 1);
1643 if (!FwdBBs[BBID])
1644 FwdBBs[BBID] = BasicBlock::Create(Context);
1645 BB = FwdBBs[BBID];
Benjamin Kramer736a4fc2012-09-21 14:34:31 +00001646 }
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00001647 V = BlockAddress::get(Fn, BB);
Chris Lattner5956dc82009-10-28 05:53:48 +00001648 break;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001649 }
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001650 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001651
Chris Lattner83930552007-05-01 07:01:57 +00001652 ValueList.AssignValue(V, NextCstNo);
Chris Lattner1663cca2007-04-24 05:48:56 +00001653 ++NextCstNo;
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001654 }
1655}
Chris Lattner1314b992007-04-22 06:23:29 +00001656
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001657std::error_code BitcodeReader::ParseUseLists() {
Chad Rosierca2567b2011-12-07 21:44:12 +00001658 if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001659 return Error(BitcodeError::InvalidRecord);
Chad Rosierca2567b2011-12-07 21:44:12 +00001660
Chad Rosierca2567b2011-12-07 21:44:12 +00001661 // Read all the records.
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00001662 SmallVector<uint64_t, 64> Record;
Chad Rosierca2567b2011-12-07 21:44:12 +00001663 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00001664 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00001665
Chris Lattner27d38752013-01-20 02:13:19 +00001666 switch (Entry.Kind) {
1667 case BitstreamEntry::SubBlock: // Handled for us already.
1668 case BitstreamEntry::Error:
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001669 return Error(BitcodeError::MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00001670 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001671 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00001672 case BitstreamEntry::Record:
1673 // The interesting case.
1674 break;
Chad Rosierca2567b2011-12-07 21:44:12 +00001675 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001676
Chad Rosierca2567b2011-12-07 21:44:12 +00001677 // Read a use list record.
1678 Record.clear();
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00001679 bool IsBB = false;
Chris Lattner27d38752013-01-20 02:13:19 +00001680 switch (Stream.readRecord(Entry.ID, Record)) {
Chad Rosierca2567b2011-12-07 21:44:12 +00001681 default: // Default behavior: unknown type.
1682 break;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00001683 case bitc::USELIST_CODE_BB:
1684 IsBB = true;
1685 // fallthrough
1686 case bitc::USELIST_CODE_DEFAULT: {
Chad Rosierca2567b2011-12-07 21:44:12 +00001687 unsigned RecordLength = Record.size();
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00001688 if (RecordLength < 3)
1689 // Records should have at least an ID and two indexes.
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001690 return Error(BitcodeError::InvalidRecord);
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00001691 unsigned ID = Record.back();
1692 Record.pop_back();
1693
1694 Value *V;
1695 if (IsBB) {
1696 assert(ID < FunctionBBs.size() && "Basic block not found");
1697 V = FunctionBBs[ID];
1698 } else
1699 V = ValueList[ID];
1700 unsigned NumUses = 0;
1701 SmallDenseMap<const Use *, unsigned, 16> Order;
1702 for (const Use &U : V->uses()) {
Duncan P. N. Exon Smith13183642014-08-16 01:54:34 +00001703 if (++NumUses > Record.size())
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00001704 break;
Duncan P. N. Exon Smith13183642014-08-16 01:54:34 +00001705 Order[&U] = Record[NumUses - 1];
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00001706 }
1707 if (Order.size() != Record.size() || NumUses > Record.size())
1708 // Mismatches can happen if the functions are being materialized lazily
1709 // (out-of-order), or a value has been upgraded.
1710 break;
1711
1712 V->sortUseList([&](const Use &L, const Use &R) {
1713 return Order.lookup(&L) < Order.lookup(&R);
1714 });
Chad Rosierca2567b2011-12-07 21:44:12 +00001715 break;
1716 }
1717 }
1718 }
1719}
1720
Chris Lattner85b7b402007-05-01 05:52:21 +00001721/// RememberAndSkipFunctionBody - When we see the block for a function body,
1722/// remember where it is and then skip it. This lets us lazily deserialize the
1723/// functions.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001724std::error_code BitcodeReader::RememberAndSkipFunctionBody() {
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001725 // Get the function we are talking about.
1726 if (FunctionsWithBodies.empty())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001727 return Error(BitcodeError::InsufficientFunctionProtos);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001728
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001729 Function *Fn = FunctionsWithBodies.back();
1730 FunctionsWithBodies.pop_back();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001731
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001732 // Save the current stream state.
1733 uint64_t CurBit = Stream.GetCurrentBitNo();
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001734 DeferredFunctionInfo[Fn] = CurBit;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001735
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001736 // Skip over the function block for now.
1737 if (Stream.SkipBlock())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001738 return Error(BitcodeError::InvalidRecord);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001739 return std::error_code();
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001740}
1741
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001742std::error_code BitcodeReader::GlobalCleanup() {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001743 // Patch the initializers for globals and aliases up.
1744 ResolveGlobalAndAliasInits();
1745 if (!GlobalInits.empty() || !AliasInits.empty())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001746 return Error(BitcodeError::MalformedGlobalInitializerSet);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001747
1748 // Look for intrinsic functions which need to be upgraded at some point
1749 for (Module::iterator FI = TheModule->begin(), FE = TheModule->end();
1750 FI != FE; ++FI) {
1751 Function *NewFn;
1752 if (UpgradeIntrinsicFunction(FI, NewFn))
1753 UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn));
1754 }
1755
1756 // Look for global variables which need to be renamed.
1757 for (Module::global_iterator
1758 GI = TheModule->global_begin(), GE = TheModule->global_end();
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001759 GI != GE;) {
1760 GlobalVariable *GV = GI++;
1761 UpgradeGlobalVariable(GV);
1762 }
1763
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001764 // Force deallocation of memory for these vectors to favor the client that
1765 // want lazy deserialization.
1766 std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits);
1767 std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001768 return std::error_code();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001769}
1770
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001771std::error_code BitcodeReader::ParseModule(bool Resume) {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001772 if (Resume)
1773 Stream.JumpToBit(NextUnreadBit);
1774 else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001775 return Error(BitcodeError::InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001776
Chris Lattner1314b992007-04-22 06:23:29 +00001777 SmallVector<uint64_t, 64> Record;
1778 std::vector<std::string> SectionTable;
Gordon Henriksend930f912008-08-17 18:44:35 +00001779 std::vector<std::string> GCTable;
Chris Lattner1314b992007-04-22 06:23:29 +00001780
1781 // Read all the records for this module.
Chris Lattner27d38752013-01-20 02:13:19 +00001782 while (1) {
1783 BitstreamEntry Entry = Stream.advance();
Joe Abbey97b7a172013-02-06 22:14:06 +00001784
Chris Lattner27d38752013-01-20 02:13:19 +00001785 switch (Entry.Kind) {
1786 case BitstreamEntry::Error:
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001787 return Error(BitcodeError::MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00001788 case BitstreamEntry::EndBlock:
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001789 return GlobalCleanup();
Joe Abbey97b7a172013-02-06 22:14:06 +00001790
Chris Lattner27d38752013-01-20 02:13:19 +00001791 case BitstreamEntry::SubBlock:
1792 switch (Entry.ID) {
Chris Lattner1314b992007-04-22 06:23:29 +00001793 default: // Skip unknown content.
1794 if (Stream.SkipBlock())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001795 return Error(BitcodeError::InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001796 break;
Chris Lattner6eeea5d2007-05-05 18:57:30 +00001797 case bitc::BLOCKINFO_BLOCK_ID:
1798 if (Stream.ReadBlockInfoBlock())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001799 return Error(BitcodeError::MalformedBlock);
Chris Lattner6eeea5d2007-05-05 18:57:30 +00001800 break;
Chris Lattnerfee5a372007-05-04 03:30:17 +00001801 case bitc::PARAMATTR_BLOCK_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001802 if (std::error_code EC = ParseAttributeBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001803 return EC;
Chris Lattnerfee5a372007-05-04 03:30:17 +00001804 break;
Bill Wendlingba629332013-02-10 23:24:25 +00001805 case bitc::PARAMATTR_GROUP_BLOCK_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001806 if (std::error_code EC = ParseAttributeGroupBlock())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001807 return EC;
Bill Wendlingba629332013-02-10 23:24:25 +00001808 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001809 case bitc::TYPE_BLOCK_ID_NEW:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001810 if (std::error_code EC = ParseTypeTable())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001811 return EC;
Chris Lattner1314b992007-04-22 06:23:29 +00001812 break;
Chris Lattnerccaa4482007-04-23 21:26:05 +00001813 case bitc::VALUE_SYMTAB_BLOCK_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001814 if (std::error_code EC = ParseValueSymbolTable())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001815 return EC;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001816 SeenValueSymbolTable = true;
Chris Lattnerccaa4482007-04-23 21:26:05 +00001817 break;
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001818 case bitc::CONSTANTS_BLOCK_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001819 if (std::error_code EC = ParseConstants())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001820 return EC;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001821 if (std::error_code EC = ResolveGlobalAndAliasInits())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001822 return EC;
Chris Lattnerfbc1d332007-04-24 03:30:34 +00001823 break;
Devang Patel7428d8a2009-07-22 17:43:22 +00001824 case bitc::METADATA_BLOCK_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001825 if (std::error_code EC = ParseMetadata())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001826 return EC;
Devang Patel7428d8a2009-07-22 17:43:22 +00001827 break;
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001828 case bitc::FUNCTION_BLOCK_ID:
1829 // If this is the first function body we've seen, reverse the
1830 // FunctionsWithBodies list.
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001831 if (!SeenFirstFunctionBody) {
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001832 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001833 if (std::error_code EC = GlobalCleanup())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001834 return EC;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001835 SeenFirstFunctionBody = true;
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001836 }
Joe Abbey97b7a172013-02-06 22:14:06 +00001837
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001838 if (std::error_code EC = RememberAndSkipFunctionBody())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001839 return EC;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001840 // For streaming bitcode, suspend parsing when we reach the function
1841 // bodies. Subsequent materialization calls will resume it when
1842 // necessary. For streaming, the function bodies must be at the end of
1843 // the bitcode. If the bitcode file is old, the symbol table will be
1844 // at the end instead and will not have been seen yet. In this case,
1845 // just finish the parse now.
1846 if (LazyStreamer && SeenValueSymbolTable) {
1847 NextUnreadBit = Stream.GetCurrentBitNo();
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001848 return std::error_code();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00001849 }
Chris Lattner51ffe7c2007-05-01 04:59:48 +00001850 break;
Chad Rosierca2567b2011-12-07 21:44:12 +00001851 case bitc::USELIST_BLOCK_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001852 if (std::error_code EC = ParseUseLists())
Rafael Espindola48da4f42013-11-04 16:16:24 +00001853 return EC;
Chad Rosierca2567b2011-12-07 21:44:12 +00001854 break;
Chris Lattner1314b992007-04-22 06:23:29 +00001855 }
1856 continue;
Joe Abbey97b7a172013-02-06 22:14:06 +00001857
Chris Lattner27d38752013-01-20 02:13:19 +00001858 case BitstreamEntry::Record:
1859 // The interesting case.
1860 break;
Chris Lattner1314b992007-04-22 06:23:29 +00001861 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001862
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001863
Chris Lattner1314b992007-04-22 06:23:29 +00001864 // Read a record.
Chris Lattner27d38752013-01-20 02:13:19 +00001865 switch (Stream.readRecord(Entry.ID, Record)) {
Chris Lattner1314b992007-04-22 06:23:29 +00001866 default: break; // Default behavior, ignore unknown content.
Jan Wen Voungafaced02012-10-11 20:20:40 +00001867 case bitc::MODULE_CODE_VERSION: { // VERSION: [version#]
Chris Lattner1314b992007-04-22 06:23:29 +00001868 if (Record.size() < 1)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001869 return Error(BitcodeError::InvalidRecord);
Jan Wen Voungafaced02012-10-11 20:20:40 +00001870 // Only version #0 and #1 are supported so far.
1871 unsigned module_version = Record[0];
1872 switch (module_version) {
Rafael Espindola48da4f42013-11-04 16:16:24 +00001873 default:
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001874 return Error(BitcodeError::InvalidValue);
Jan Wen Voungafaced02012-10-11 20:20:40 +00001875 case 0:
1876 UseRelativeIDs = false;
1877 break;
1878 case 1:
1879 UseRelativeIDs = true;
1880 break;
1881 }
Chris Lattner1314b992007-04-22 06:23:29 +00001882 break;
Jan Wen Voungafaced02012-10-11 20:20:40 +00001883 }
Chris Lattnere14cb882007-05-04 19:11:41 +00001884 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00001885 std::string S;
1886 if (ConvertToString(Record, 0, S))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001887 return Error(BitcodeError::InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001888 TheModule->setTargetTriple(S);
1889 break;
1890 }
Chris Lattnere14cb882007-05-04 19:11:41 +00001891 case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00001892 std::string S;
1893 if (ConvertToString(Record, 0, S))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001894 return Error(BitcodeError::InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001895 TheModule->setDataLayout(S);
1896 break;
1897 }
Chris Lattnere14cb882007-05-04 19:11:41 +00001898 case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00001899 std::string S;
1900 if (ConvertToString(Record, 0, S))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001901 return Error(BitcodeError::InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001902 TheModule->setModuleInlineAsm(S);
1903 break;
1904 }
Bill Wendling706d3d62012-11-28 08:41:48 +00001905 case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N]
1906 // FIXME: Remove in 4.0.
1907 std::string S;
1908 if (ConvertToString(Record, 0, S))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001909 return Error(BitcodeError::InvalidRecord);
Bill Wendling706d3d62012-11-28 08:41:48 +00001910 // Ignore value.
1911 break;
1912 }
Chris Lattnere14cb882007-05-04 19:11:41 +00001913 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]
Chris Lattner1314b992007-04-22 06:23:29 +00001914 std::string S;
1915 if (ConvertToString(Record, 0, S))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001916 return Error(BitcodeError::InvalidRecord);
Chris Lattner1314b992007-04-22 06:23:29 +00001917 SectionTable.push_back(S);
1918 break;
1919 }
Gordon Henriksend930f912008-08-17 18:44:35 +00001920 case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N]
Gordon Henriksen71183b62007-12-10 03:18:06 +00001921 std::string S;
1922 if (ConvertToString(Record, 0, S))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001923 return Error(BitcodeError::InvalidRecord);
Gordon Henriksend930f912008-08-17 18:44:35 +00001924 GCTable.push_back(S);
Gordon Henriksen71183b62007-12-10 03:18:06 +00001925 break;
1926 }
David Majnemerdad0a642014-06-27 18:19:56 +00001927 case bitc::MODULE_CODE_COMDAT: { // COMDAT: [selection_kind, name]
1928 if (Record.size() < 2)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001929 return Error(BitcodeError::InvalidRecord);
David Majnemerdad0a642014-06-27 18:19:56 +00001930 Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]);
1931 unsigned ComdatNameSize = Record[1];
1932 std::string ComdatName;
1933 ComdatName.reserve(ComdatNameSize);
1934 for (unsigned i = 0; i != ComdatNameSize; ++i)
1935 ComdatName += (char)Record[2 + i];
1936 Comdat *C = TheModule->getOrInsertComdat(ComdatName);
1937 C->setSelectionKind(SK);
1938 ComdatList.push_back(C);
1939 break;
1940 }
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001941 // GLOBALVAR: [pointer type, isconst, initid,
Rafael Espindola45e6c192011-01-08 16:42:36 +00001942 // linkage, alignment, section, visibility, threadlocal,
Nico Rieck7157bb72014-01-14 15:22:47 +00001943 // unnamed_addr, dllstorageclass]
Chris Lattner1314b992007-04-22 06:23:29 +00001944 case bitc::MODULE_CODE_GLOBALVAR: {
Chris Lattner4b00d922007-04-23 16:04:05 +00001945 if (Record.size() < 6)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001946 return Error(BitcodeError::InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00001947 Type *Ty = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00001948 if (!Ty)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001949 return Error(BitcodeError::InvalidRecord);
Duncan Sands19d0b472010-02-16 11:11:14 +00001950 if (!Ty->isPointerTy())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001951 return Error(BitcodeError::InvalidTypeForValue);
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001952 unsigned AddressSpace = cast<PointerType>(Ty)->getAddressSpace();
Chris Lattner1314b992007-04-22 06:23:29 +00001953 Ty = cast<PointerType>(Ty)->getElementType();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001954
Chris Lattner1314b992007-04-22 06:23:29 +00001955 bool isConstant = Record[1];
1956 GlobalValue::LinkageTypes Linkage = GetDecodedLinkage(Record[3]);
1957 unsigned Alignment = (1 << Record[4]) >> 1;
1958 std::string Section;
1959 if (Record[5]) {
1960 if (Record[5]-1 >= SectionTable.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00001961 return Error(BitcodeError::InvalidID);
Chris Lattner1314b992007-04-22 06:23:29 +00001962 Section = SectionTable[Record[5]-1];
1963 }
Chris Lattner4b00d922007-04-23 16:04:05 +00001964 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00001965 // Local linkage must have default visibility.
1966 if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage))
1967 // FIXME: Change to an error if non-default in 4.0.
Chris Lattner53862f72007-05-06 19:27:46 +00001968 Visibility = GetDecodedVisibility(Record[6]);
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001969
1970 GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal;
Chris Lattner53862f72007-05-06 19:27:46 +00001971 if (Record.size() > 7)
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001972 TLM = GetDecodedThreadLocalMode(Record[7]);
Chris Lattner1314b992007-04-22 06:23:29 +00001973
Rafael Espindola45e6c192011-01-08 16:42:36 +00001974 bool UnnamedAddr = false;
1975 if (Record.size() > 8)
1976 UnnamedAddr = Record[8];
1977
Michael Gottesman27e7ef32013-02-05 05:57:38 +00001978 bool ExternallyInitialized = false;
1979 if (Record.size() > 9)
1980 ExternallyInitialized = Record[9];
1981
Chris Lattner1314b992007-04-22 06:23:29 +00001982 GlobalVariable *NewGV =
Craig Topper2617dcc2014-04-15 06:32:26 +00001983 new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, "", nullptr,
Michael Gottesman27e7ef32013-02-05 05:57:38 +00001984 TLM, AddressSpace, ExternallyInitialized);
Chris Lattner1314b992007-04-22 06:23:29 +00001985 NewGV->setAlignment(Alignment);
1986 if (!Section.empty())
1987 NewGV->setSection(Section);
1988 NewGV->setVisibility(Visibility);
Rafael Espindola45e6c192011-01-08 16:42:36 +00001989 NewGV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001990
Nico Rieck7157bb72014-01-14 15:22:47 +00001991 if (Record.size() > 10)
1992 NewGV->setDLLStorageClass(GetDecodedDLLStorageClass(Record[10]));
1993 else
1994 UpgradeDLLImportExportLinkage(NewGV, Record[3]);
1995
Chris Lattnerccaa4482007-04-23 21:26:05 +00001996 ValueList.push_back(NewGV);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001997
Chris Lattner47d131b2007-04-24 00:18:21 +00001998 // Remember which value to use for the global initializer.
1999 if (unsigned InitID = Record[2])
2000 GlobalInits.push_back(std::make_pair(NewGV, InitID-1));
David Majnemerdad0a642014-06-27 18:19:56 +00002001
2002 if (Record.size() > 11)
2003 if (unsigned ComdatID = Record[11]) {
2004 assert(ComdatID <= ComdatList.size());
2005 NewGV->setComdat(ComdatList[ComdatID - 1]);
2006 }
Chris Lattner1314b992007-04-22 06:23:29 +00002007 break;
2008 }
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002009 // FUNCTION: [type, callingconv, isproto, linkage, paramattr,
Nico Rieck7157bb72014-01-14 15:22:47 +00002010 // alignment, section, visibility, gc, unnamed_addr,
2011 // dllstorageclass]
Chris Lattner1314b992007-04-22 06:23:29 +00002012 case bitc::MODULE_CODE_FUNCTION: {
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002013 if (Record.size() < 8)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002014 return Error(BitcodeError::InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00002015 Type *Ty = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00002016 if (!Ty)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002017 return Error(BitcodeError::InvalidRecord);
Duncan Sands19d0b472010-02-16 11:11:14 +00002018 if (!Ty->isPointerTy())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002019 return Error(BitcodeError::InvalidTypeForValue);
Chris Lattner229907c2011-07-18 04:54:35 +00002020 FunctionType *FTy =
Chris Lattner1314b992007-04-22 06:23:29 +00002021 dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType());
2022 if (!FTy)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002023 return Error(BitcodeError::InvalidTypeForValue);
Chris Lattner1314b992007-04-22 06:23:29 +00002024
Gabor Greife9ecc682008-04-06 20:25:17 +00002025 Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage,
2026 "", TheModule);
Chris Lattner1314b992007-04-22 06:23:29 +00002027
Sandeep Patel68c5f472009-09-02 08:44:58 +00002028 Func->setCallingConv(static_cast<CallingConv::ID>(Record[1]));
Chris Lattner51ffe7c2007-05-01 04:59:48 +00002029 bool isProto = Record[2];
Chris Lattner1314b992007-04-22 06:23:29 +00002030 Func->setLinkage(GetDecodedLinkage(Record[3]));
Devang Patel4c758ea2008-09-25 21:00:45 +00002031 Func->setAttributes(getAttributes(Record[4]));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002032
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002033 Func->setAlignment((1 << Record[5]) >> 1);
2034 if (Record[6]) {
2035 if (Record[6]-1 >= SectionTable.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002036 return Error(BitcodeError::InvalidID);
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002037 Func->setSection(SectionTable[Record[6]-1]);
Chris Lattner1314b992007-04-22 06:23:29 +00002038 }
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00002039 // Local linkage must have default visibility.
2040 if (!Func->hasLocalLinkage())
2041 // FIXME: Change to an error if non-default in 4.0.
2042 Func->setVisibility(GetDecodedVisibility(Record[7]));
Gordon Henriksen71183b62007-12-10 03:18:06 +00002043 if (Record.size() > 8 && Record[8]) {
Gordon Henriksend930f912008-08-17 18:44:35 +00002044 if (Record[8]-1 > GCTable.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002045 return Error(BitcodeError::InvalidID);
Gordon Henriksend930f912008-08-17 18:44:35 +00002046 Func->setGC(GCTable[Record[8]-1].c_str());
Gordon Henriksen71183b62007-12-10 03:18:06 +00002047 }
Rafael Espindola45e6c192011-01-08 16:42:36 +00002048 bool UnnamedAddr = false;
2049 if (Record.size() > 9)
2050 UnnamedAddr = Record[9];
2051 Func->setUnnamedAddr(UnnamedAddr);
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00002052 if (Record.size() > 10 && Record[10] != 0)
2053 FunctionPrefixes.push_back(std::make_pair(Func, Record[10]-1));
Nico Rieck7157bb72014-01-14 15:22:47 +00002054
2055 if (Record.size() > 11)
2056 Func->setDLLStorageClass(GetDecodedDLLStorageClass(Record[11]));
2057 else
2058 UpgradeDLLImportExportLinkage(Func, Record[3]);
2059
David Majnemerdad0a642014-06-27 18:19:56 +00002060 if (Record.size() > 12)
2061 if (unsigned ComdatID = Record[12]) {
2062 assert(ComdatID <= ComdatList.size());
2063 Func->setComdat(ComdatList[ComdatID - 1]);
2064 }
2065
Chris Lattnerccaa4482007-04-23 21:26:05 +00002066 ValueList.push_back(Func);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002067
Chris Lattner51ffe7c2007-05-01 04:59:48 +00002068 // If this is a function with a body, remember the prototype we are
2069 // creating now, so that we can match up the body with them later.
Derek Schuff8b2dcad2012-02-06 22:30:29 +00002070 if (!isProto) {
Chris Lattner51ffe7c2007-05-01 04:59:48 +00002071 FunctionsWithBodies.push_back(Func);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00002072 if (LazyStreamer) DeferredFunctionInfo[Func] = 0;
2073 }
Chris Lattner1314b992007-04-22 06:23:29 +00002074 break;
2075 }
Anton Korobeynikov2f22e3f2008-03-12 00:49:19 +00002076 // ALIAS: [alias type, aliasee val#, linkage]
Nico Rieck7157bb72014-01-14 15:22:47 +00002077 // ALIAS: [alias type, aliasee val#, linkage, visibility, dllstorageclass]
Chris Lattner831d4202007-04-26 03:27:58 +00002078 case bitc::MODULE_CODE_ALIAS: {
Chris Lattner44c17072007-04-26 02:46:40 +00002079 if (Record.size() < 3)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002080 return Error(BitcodeError::InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00002081 Type *Ty = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00002082 if (!Ty)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002083 return Error(BitcodeError::InvalidRecord);
Rafael Espindolaa8004452014-05-16 14:22:33 +00002084 auto *PTy = dyn_cast<PointerType>(Ty);
2085 if (!PTy)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002086 return Error(BitcodeError::InvalidTypeForValue);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002087
Rafael Espindolaa8004452014-05-16 14:22:33 +00002088 auto *NewGA =
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +00002089 GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
2090 GetDecodedLinkage(Record[2]), "", TheModule);
Anton Korobeynikov2f22e3f2008-03-12 00:49:19 +00002091 // Old bitcode files didn't have visibility field.
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00002092 // Local linkage must have default visibility.
2093 if (Record.size() > 3 && !NewGA->hasLocalLinkage())
2094 // FIXME: Change to an error if non-default in 4.0.
Anton Korobeynikov2f22e3f2008-03-12 00:49:19 +00002095 NewGA->setVisibility(GetDecodedVisibility(Record[3]));
Nico Rieck7157bb72014-01-14 15:22:47 +00002096 if (Record.size() > 4)
2097 NewGA->setDLLStorageClass(GetDecodedDLLStorageClass(Record[4]));
2098 else
2099 UpgradeDLLImportExportLinkage(NewGA, Record[2]);
Rafael Espindola59f7eba2014-05-28 18:15:43 +00002100 if (Record.size() > 5)
2101 NewGA->setThreadLocalMode(GetDecodedThreadLocalMode(Record[5]));
Rafael Espindola42a4c9f2014-06-06 01:20:28 +00002102 if (Record.size() > 6)
2103 NewGA->setUnnamedAddr(Record[6]);
Chris Lattner44c17072007-04-26 02:46:40 +00002104 ValueList.push_back(NewGA);
2105 AliasInits.push_back(std::make_pair(NewGA, Record[1]));
2106 break;
Chris Lattner1314b992007-04-22 06:23:29 +00002107 }
Chris Lattner831d4202007-04-26 03:27:58 +00002108 /// MODULE_CODE_PURGEVALS: [numvals]
2109 case bitc::MODULE_CODE_PURGEVALS:
2110 // Trim down the value list to the specified size.
2111 if (Record.size() < 1 || Record[0] > ValueList.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002112 return Error(BitcodeError::InvalidRecord);
Chris Lattner831d4202007-04-26 03:27:58 +00002113 ValueList.shrinkTo(Record[0]);
2114 break;
2115 }
Chris Lattner1314b992007-04-22 06:23:29 +00002116 Record.clear();
2117 }
Chris Lattner1314b992007-04-22 06:23:29 +00002118}
2119
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002120std::error_code BitcodeReader::ParseBitcodeInto(Module *M) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002121 TheModule = nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002122
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002123 if (std::error_code EC = InitStream())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002124 return EC;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002125
Chris Lattner1314b992007-04-22 06:23:29 +00002126 // Sniff for the signature.
2127 if (Stream.Read(8) != 'B' ||
2128 Stream.Read(8) != 'C' ||
2129 Stream.Read(4) != 0x0 ||
2130 Stream.Read(4) != 0xC ||
2131 Stream.Read(4) != 0xE ||
2132 Stream.Read(4) != 0xD)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002133 return Error(BitcodeError::InvalidBitcodeSignature);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002134
Chris Lattner1314b992007-04-22 06:23:29 +00002135 // We expect a number of well-defined blocks, though we don't necessarily
2136 // need to understand them all.
Chris Lattner27d38752013-01-20 02:13:19 +00002137 while (1) {
2138 if (Stream.AtEndOfStream())
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002139 return std::error_code();
Joe Abbey97b7a172013-02-06 22:14:06 +00002140
Chris Lattner27d38752013-01-20 02:13:19 +00002141 BitstreamEntry Entry =
2142 Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
Joe Abbey97b7a172013-02-06 22:14:06 +00002143
Chris Lattner27d38752013-01-20 02:13:19 +00002144 switch (Entry.Kind) {
2145 case BitstreamEntry::Error:
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002146 return Error(BitcodeError::MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002147 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002148 return std::error_code();
Joe Abbey97b7a172013-02-06 22:14:06 +00002149
Chris Lattner27d38752013-01-20 02:13:19 +00002150 case BitstreamEntry::SubBlock:
2151 switch (Entry.ID) {
2152 case bitc::BLOCKINFO_BLOCK_ID:
2153 if (Stream.ReadBlockInfoBlock())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002154 return Error(BitcodeError::MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002155 break;
2156 case bitc::MODULE_BLOCK_ID:
2157 // Reject multiple MODULE_BLOCK's in a single bitstream.
2158 if (TheModule)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002159 return Error(BitcodeError::InvalidMultipleBlocks);
Chris Lattner27d38752013-01-20 02:13:19 +00002160 TheModule = M;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002161 if (std::error_code EC = ParseModule(false))
Rafael Espindola48da4f42013-11-04 16:16:24 +00002162 return EC;
2163 if (LazyStreamer)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002164 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00002165 break;
2166 default:
2167 if (Stream.SkipBlock())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002168 return Error(BitcodeError::InvalidRecord);
Chris Lattner27d38752013-01-20 02:13:19 +00002169 break;
2170 }
2171 continue;
2172 case BitstreamEntry::Record:
2173 // There should be no records in the top-level of blocks.
Joe Abbey97b7a172013-02-06 22:14:06 +00002174
Chris Lattner27d38752013-01-20 02:13:19 +00002175 // The ranlib in Xcode 4 will align archive members by appending newlines
Chad Rosiera15e3aa2011-08-09 22:23:40 +00002176 // to the end of them. If this file size is a multiple of 4 but not 8, we
2177 // have to read and ignore these final 4 bytes :-(
Chris Lattner27d38752013-01-20 02:13:19 +00002178 if (Stream.getAbbrevIDWidth() == 2 && Entry.ID == 2 &&
Rafael Espindolaa97b2382011-05-26 18:59:54 +00002179 Stream.Read(6) == 2 && Stream.Read(24) == 0xa0a0a &&
Bill Wendling318f03f2012-07-19 00:15:11 +00002180 Stream.AtEndOfStream())
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002181 return std::error_code();
Joe Abbey97b7a172013-02-06 22:14:06 +00002182
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002183 return Error(BitcodeError::InvalidRecord);
Rafael Espindolaa97b2382011-05-26 18:59:54 +00002184 }
Chris Lattner1314b992007-04-22 06:23:29 +00002185 }
Chris Lattner1314b992007-04-22 06:23:29 +00002186}
Chris Lattner6694f602007-04-29 07:54:31 +00002187
Rafael Espindolac75c4fa2014-07-04 20:02:42 +00002188ErrorOr<std::string> BitcodeReader::parseModuleTriple() {
Bill Wendling0198ce02010-10-06 01:22:42 +00002189 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002190 return Error(BitcodeError::InvalidRecord);
Bill Wendling0198ce02010-10-06 01:22:42 +00002191
2192 SmallVector<uint64_t, 64> Record;
2193
Rafael Espindolac75c4fa2014-07-04 20:02:42 +00002194 std::string Triple;
Bill Wendling0198ce02010-10-06 01:22:42 +00002195 // Read all the records for this module.
Chris Lattner27d38752013-01-20 02:13:19 +00002196 while (1) {
2197 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00002198
Chris Lattner27d38752013-01-20 02:13:19 +00002199 switch (Entry.Kind) {
2200 case BitstreamEntry::SubBlock: // Handled for us already.
2201 case BitstreamEntry::Error:
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002202 return Error(BitcodeError::MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002203 case BitstreamEntry::EndBlock:
Rafael Espindolae6107792014-07-04 20:05:56 +00002204 return Triple;
Chris Lattner27d38752013-01-20 02:13:19 +00002205 case BitstreamEntry::Record:
2206 // The interesting case.
2207 break;
Bill Wendling0198ce02010-10-06 01:22:42 +00002208 }
2209
2210 // Read a record.
Chris Lattner27d38752013-01-20 02:13:19 +00002211 switch (Stream.readRecord(Entry.ID, Record)) {
Bill Wendling0198ce02010-10-06 01:22:42 +00002212 default: break; // Default behavior, ignore unknown content.
Bill Wendling0198ce02010-10-06 01:22:42 +00002213 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
Rafael Espindolac75c4fa2014-07-04 20:02:42 +00002214 std::string S;
2215 if (ConvertToString(Record, 0, S))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002216 return Error(BitcodeError::InvalidRecord);
Rafael Espindolac75c4fa2014-07-04 20:02:42 +00002217 Triple = S;
Bill Wendling0198ce02010-10-06 01:22:42 +00002218 break;
2219 }
2220 }
2221 Record.clear();
2222 }
Rafael Espindolae6107792014-07-04 20:05:56 +00002223 llvm_unreachable("Exit infinite loop");
Bill Wendling0198ce02010-10-06 01:22:42 +00002224}
2225
Rafael Espindolac75c4fa2014-07-04 20:02:42 +00002226ErrorOr<std::string> BitcodeReader::parseTriple() {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002227 if (std::error_code EC = InitStream())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002228 return EC;
Bill Wendling0198ce02010-10-06 01:22:42 +00002229
2230 // Sniff for the signature.
2231 if (Stream.Read(8) != 'B' ||
2232 Stream.Read(8) != 'C' ||
2233 Stream.Read(4) != 0x0 ||
2234 Stream.Read(4) != 0xC ||
2235 Stream.Read(4) != 0xE ||
2236 Stream.Read(4) != 0xD)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002237 return Error(BitcodeError::InvalidBitcodeSignature);
Bill Wendling0198ce02010-10-06 01:22:42 +00002238
2239 // We expect a number of well-defined blocks, though we don't necessarily
2240 // need to understand them all.
Chris Lattner27d38752013-01-20 02:13:19 +00002241 while (1) {
2242 BitstreamEntry Entry = Stream.advance();
Joe Abbey97b7a172013-02-06 22:14:06 +00002243
Chris Lattner27d38752013-01-20 02:13:19 +00002244 switch (Entry.Kind) {
2245 case BitstreamEntry::Error:
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002246 return Error(BitcodeError::MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002247 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002248 return std::error_code();
Joe Abbey97b7a172013-02-06 22:14:06 +00002249
Chris Lattner27d38752013-01-20 02:13:19 +00002250 case BitstreamEntry::SubBlock:
2251 if (Entry.ID == bitc::MODULE_BLOCK_ID)
Rafael Espindolad346cc82014-07-04 13:52:01 +00002252 return parseModuleTriple();
Joe Abbey97b7a172013-02-06 22:14:06 +00002253
Chris Lattner27d38752013-01-20 02:13:19 +00002254 // Ignore other sub-blocks.
Rafael Espindola48da4f42013-11-04 16:16:24 +00002255 if (Stream.SkipBlock())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002256 return Error(BitcodeError::MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002257 continue;
Joe Abbey97b7a172013-02-06 22:14:06 +00002258
Chris Lattner27d38752013-01-20 02:13:19 +00002259 case BitstreamEntry::Record:
2260 Stream.skipRecord(Entry.ID);
2261 continue;
Bill Wendling0198ce02010-10-06 01:22:42 +00002262 }
2263 }
Bill Wendling0198ce02010-10-06 01:22:42 +00002264}
2265
Devang Patelaf206b82009-09-18 19:26:43 +00002266/// ParseMetadataAttachment - Parse metadata attachments.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002267std::error_code BitcodeReader::ParseMetadataAttachment() {
Devang Patelaf206b82009-09-18 19:26:43 +00002268 if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002269 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002270
Devang Patelaf206b82009-09-18 19:26:43 +00002271 SmallVector<uint64_t, 64> Record;
Chris Lattner27d38752013-01-20 02:13:19 +00002272 while (1) {
2273 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Joe Abbey97b7a172013-02-06 22:14:06 +00002274
Chris Lattner27d38752013-01-20 02:13:19 +00002275 switch (Entry.Kind) {
2276 case BitstreamEntry::SubBlock: // Handled for us already.
2277 case BitstreamEntry::Error:
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002278 return Error(BitcodeError::MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002279 case BitstreamEntry::EndBlock:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002280 return std::error_code();
Chris Lattner27d38752013-01-20 02:13:19 +00002281 case BitstreamEntry::Record:
2282 // The interesting case.
Devang Patelaf206b82009-09-18 19:26:43 +00002283 break;
2284 }
Chris Lattner27d38752013-01-20 02:13:19 +00002285
Devang Patelaf206b82009-09-18 19:26:43 +00002286 // Read a metadata attachment record.
2287 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +00002288 switch (Stream.readRecord(Entry.ID, Record)) {
Devang Patelaf206b82009-09-18 19:26:43 +00002289 default: // Default behavior: ignore.
2290 break;
Chris Lattnerb8778552011-06-17 17:50:30 +00002291 case bitc::METADATA_ATTACHMENT: {
Devang Patelaf206b82009-09-18 19:26:43 +00002292 unsigned RecordLength = Record.size();
2293 if (Record.empty() || (RecordLength - 1) % 2 == 1)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002294 return Error(BitcodeError::InvalidRecord);
Devang Patelaf206b82009-09-18 19:26:43 +00002295 Instruction *Inst = InstructionList[Record[0]];
2296 for (unsigned i = 1; i != RecordLength; i = i+2) {
Devang Patelb1a44772009-09-28 21:14:55 +00002297 unsigned Kind = Record[i];
Dan Gohman43aa8f02010-07-20 21:42:28 +00002298 DenseMap<unsigned, unsigned>::iterator I =
2299 MDKindMap.find(Kind);
2300 if (I == MDKindMap.end())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002301 return Error(BitcodeError::InvalidID);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002302 Value *Node = MDValueList.getValueFwdRef(Record[i+1]);
Dan Gohman43aa8f02010-07-20 21:42:28 +00002303 Inst->setMetadata(I->second, cast<MDNode>(Node));
Manman Ren209b17c2013-09-28 00:22:27 +00002304 if (I->second == LLVMContext::MD_tbaa)
2305 InstsWithTBAATag.push_back(Inst);
Devang Patelaf206b82009-09-18 19:26:43 +00002306 }
2307 break;
2308 }
2309 }
2310 }
Devang Patelaf206b82009-09-18 19:26:43 +00002311}
Chris Lattner51ffe7c2007-05-01 04:59:48 +00002312
Chris Lattner85b7b402007-05-01 05:52:21 +00002313/// ParseFunctionBody - Lazily parse the specified function body block.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002314std::error_code BitcodeReader::ParseFunctionBody(Function *F) {
Chris Lattner982ec1e2007-05-05 00:17:00 +00002315 if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002316 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002317
Nick Lewyckya72e1af2010-02-25 08:30:17 +00002318 InstructionList.clear();
Chris Lattner85b7b402007-05-01 05:52:21 +00002319 unsigned ModuleValueListSize = ValueList.size();
Dan Gohman26d837d2010-08-25 20:22:53 +00002320 unsigned ModuleMDValueListSize = MDValueList.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002321
Chris Lattner85b7b402007-05-01 05:52:21 +00002322 // Add all the function arguments to the value table.
2323 for(Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
2324 ValueList.push_back(I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002325
Chris Lattner83930552007-05-01 07:01:57 +00002326 unsigned NextValueNo = ValueList.size();
Craig Topper2617dcc2014-04-15 06:32:26 +00002327 BasicBlock *CurBB = nullptr;
Chris Lattnere53603e2007-05-02 04:27:25 +00002328 unsigned CurBBNo = 0;
2329
Chris Lattner07d09ed2010-04-03 02:17:50 +00002330 DebugLoc LastLoc;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002331
Chris Lattner85b7b402007-05-01 05:52:21 +00002332 // Read all the records.
2333 SmallVector<uint64_t, 64> Record;
2334 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +00002335 BitstreamEntry Entry = Stream.advance();
Joe Abbey97b7a172013-02-06 22:14:06 +00002336
Chris Lattner27d38752013-01-20 02:13:19 +00002337 switch (Entry.Kind) {
2338 case BitstreamEntry::Error:
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002339 return Error(BitcodeError::MalformedBlock);
Chris Lattner27d38752013-01-20 02:13:19 +00002340 case BitstreamEntry::EndBlock:
2341 goto OutOfRecordLoop;
Joe Abbey97b7a172013-02-06 22:14:06 +00002342
Chris Lattner27d38752013-01-20 02:13:19 +00002343 case BitstreamEntry::SubBlock:
2344 switch (Entry.ID) {
Chris Lattner85b7b402007-05-01 05:52:21 +00002345 default: // Skip unknown content.
2346 if (Stream.SkipBlock())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002347 return Error(BitcodeError::InvalidRecord);
Chris Lattner85b7b402007-05-01 05:52:21 +00002348 break;
2349 case bitc::CONSTANTS_BLOCK_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002350 if (std::error_code EC = ParseConstants())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002351 return EC;
Chris Lattner83930552007-05-01 07:01:57 +00002352 NextValueNo = ValueList.size();
Chris Lattner85b7b402007-05-01 05:52:21 +00002353 break;
2354 case bitc::VALUE_SYMTAB_BLOCK_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002355 if (std::error_code EC = ParseValueSymbolTable())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002356 return EC;
Chris Lattner85b7b402007-05-01 05:52:21 +00002357 break;
Devang Patelaf206b82009-09-18 19:26:43 +00002358 case bitc::METADATA_ATTACHMENT_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002359 if (std::error_code EC = ParseMetadataAttachment())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002360 return EC;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002361 break;
Victor Hernandez108d3ac2010-01-13 19:34:08 +00002362 case bitc::METADATA_BLOCK_ID:
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00002363 if (std::error_code EC = ParseMetadata())
Rafael Espindola48da4f42013-11-04 16:16:24 +00002364 return EC;
Victor Hernandez108d3ac2010-01-13 19:34:08 +00002365 break;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +00002366 case bitc::USELIST_BLOCK_ID:
2367 if (std::error_code EC = ParseUseLists())
2368 return EC;
2369 break;
Chris Lattner85b7b402007-05-01 05:52:21 +00002370 }
2371 continue;
Joe Abbey97b7a172013-02-06 22:14:06 +00002372
Chris Lattner27d38752013-01-20 02:13:19 +00002373 case BitstreamEntry::Record:
2374 // The interesting case.
2375 break;
Chris Lattner85b7b402007-05-01 05:52:21 +00002376 }
Joe Abbey97b7a172013-02-06 22:14:06 +00002377
Chris Lattner85b7b402007-05-01 05:52:21 +00002378 // Read a record.
2379 Record.clear();
Craig Topper2617dcc2014-04-15 06:32:26 +00002380 Instruction *I = nullptr;
Chris Lattner27d38752013-01-20 02:13:19 +00002381 unsigned BitCode = Stream.readRecord(Entry.ID, Record);
Dan Gohman0ebd6962009-07-20 21:19:07 +00002382 switch (BitCode) {
Chris Lattner83930552007-05-01 07:01:57 +00002383 default: // Default behavior: reject
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002384 return Error(BitcodeError::InvalidValue);
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00002385 case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks]
Chris Lattner83930552007-05-01 07:01:57 +00002386 if (Record.size() < 1 || Record[0] == 0)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002387 return Error(BitcodeError::InvalidRecord);
Chris Lattner85b7b402007-05-01 05:52:21 +00002388 // Create all the basic blocks for the function.
Chris Lattner6ce15cb2007-05-03 22:09:51 +00002389 FunctionBBs.resize(Record[0]);
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00002390
2391 // See if anything took the address of blocks in this function.
2392 auto BBFRI = BasicBlockFwdRefs.find(F);
2393 if (BBFRI == BasicBlockFwdRefs.end()) {
2394 for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i)
2395 FunctionBBs[i] = BasicBlock::Create(Context, "", F);
2396 } else {
2397 auto &BBRefs = BBFRI->second;
Duncan P. N. Exon Smith5a5fd7b2014-08-16 01:54:37 +00002398 // Check for invalid basic block references.
2399 if (BBRefs.size() > FunctionBBs.size())
2400 return Error(BitcodeError::InvalidID);
2401 assert(!BBRefs.empty() && "Unexpected empty array");
2402 assert(!BBRefs.front() && "Invalid reference to entry block");
2403 for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E;
2404 ++I)
2405 if (I < RE && BBRefs[I]) {
2406 BBRefs[I]->insertInto(F);
2407 FunctionBBs[I] = BBRefs[I];
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00002408 } else {
2409 FunctionBBs[I] = BasicBlock::Create(Context, "", F);
2410 }
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00002411
2412 // Erase from the table.
2413 BasicBlockFwdRefs.erase(BBFRI);
2414 }
2415
Chris Lattner83930552007-05-01 07:01:57 +00002416 CurBB = FunctionBBs[0];
2417 continue;
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00002418 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002419
Chris Lattner07d09ed2010-04-03 02:17:50 +00002420 case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN
2421 // This record indicates that the last instruction is at the same
2422 // location as the previous instruction with a location.
Craig Topper2617dcc2014-04-15 06:32:26 +00002423 I = nullptr;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002424
Chris Lattner07d09ed2010-04-03 02:17:50 +00002425 // Get the last instruction emitted.
2426 if (CurBB && !CurBB->empty())
2427 I = &CurBB->back();
2428 else if (CurBBNo && FunctionBBs[CurBBNo-1] &&
2429 !FunctionBBs[CurBBNo-1]->empty())
2430 I = &FunctionBBs[CurBBNo-1]->back();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002431
Craig Topper2617dcc2014-04-15 06:32:26 +00002432 if (!I)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002433 return Error(BitcodeError::InvalidRecord);
Chris Lattner07d09ed2010-04-03 02:17:50 +00002434 I->setDebugLoc(LastLoc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002435 I = nullptr;
Chris Lattner07d09ed2010-04-03 02:17:50 +00002436 continue;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002437
Chris Lattnerc44070802011-06-17 18:17:37 +00002438 case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia]
Craig Topper2617dcc2014-04-15 06:32:26 +00002439 I = nullptr; // Get the last instruction emitted.
Chris Lattner07d09ed2010-04-03 02:17:50 +00002440 if (CurBB && !CurBB->empty())
2441 I = &CurBB->back();
2442 else if (CurBBNo && FunctionBBs[CurBBNo-1] &&
2443 !FunctionBBs[CurBBNo-1]->empty())
2444 I = &FunctionBBs[CurBBNo-1]->back();
Craig Topper2617dcc2014-04-15 06:32:26 +00002445 if (!I || Record.size() < 4)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002446 return Error(BitcodeError::InvalidRecord);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002447
Chris Lattner07d09ed2010-04-03 02:17:50 +00002448 unsigned Line = Record[0], Col = Record[1];
2449 unsigned ScopeID = Record[2], IAID = Record[3];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002450
Craig Topper2617dcc2014-04-15 06:32:26 +00002451 MDNode *Scope = nullptr, *IA = nullptr;
Chris Lattner07d09ed2010-04-03 02:17:50 +00002452 if (ScopeID) Scope = cast<MDNode>(MDValueList.getValueFwdRef(ScopeID-1));
2453 if (IAID) IA = cast<MDNode>(MDValueList.getValueFwdRef(IAID-1));
2454 LastLoc = DebugLoc::get(Line, Col, Scope, IA);
2455 I->setDebugLoc(LastLoc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002456 I = nullptr;
Chris Lattner07d09ed2010-04-03 02:17:50 +00002457 continue;
2458 }
2459
Chris Lattnere9759c22007-05-06 00:21:25 +00002460 case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode]
2461 unsigned OpNum = 0;
2462 Value *LHS, *RHS;
2463 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002464 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
Dan Gohman0ebd6962009-07-20 21:19:07 +00002465 OpNum+1 > Record.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002466 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002467
Dan Gohman0ebd6962009-07-20 21:19:07 +00002468 int Opc = GetDecodedBinaryOpcode(Record[OpNum++], LHS->getType());
Rafael Espindola48da4f42013-11-04 16:16:24 +00002469 if (Opc == -1)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002470 return Error(BitcodeError::InvalidRecord);
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002471 I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
Devang Patelaf206b82009-09-18 19:26:43 +00002472 InstructionList.push_back(I);
Dan Gohman1b849082009-09-07 23:54:19 +00002473 if (OpNum < Record.size()) {
2474 if (Opc == Instruction::Add ||
2475 Opc == Instruction::Sub ||
Chris Lattnera676c0f2011-02-07 16:40:21 +00002476 Opc == Instruction::Mul ||
2477 Opc == Instruction::Shl) {
Dan Gohman00f47472010-01-25 21:55:39 +00002478 if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP))
Dan Gohman1b849082009-09-07 23:54:19 +00002479 cast<BinaryOperator>(I)->setHasNoSignedWrap(true);
Dan Gohman00f47472010-01-25 21:55:39 +00002480 if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
Dan Gohman1b849082009-09-07 23:54:19 +00002481 cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true);
Chris Lattner35315d02011-02-06 21:44:57 +00002482 } else if (Opc == Instruction::SDiv ||
Chris Lattnera676c0f2011-02-07 16:40:21 +00002483 Opc == Instruction::UDiv ||
2484 Opc == Instruction::LShr ||
2485 Opc == Instruction::AShr) {
Chris Lattner35315d02011-02-06 21:44:57 +00002486 if (Record[OpNum] & (1 << bitc::PEO_EXACT))
Dan Gohman1b849082009-09-07 23:54:19 +00002487 cast<BinaryOperator>(I)->setIsExact(true);
Michael Ilseman9978d7e2012-11-27 00:43:38 +00002488 } else if (isa<FPMathOperator>(I)) {
2489 FastMathFlags FMF;
Michael Ilseman65f14352012-12-09 21:12:04 +00002490 if (0 != (Record[OpNum] & FastMathFlags::UnsafeAlgebra))
2491 FMF.setUnsafeAlgebra();
2492 if (0 != (Record[OpNum] & FastMathFlags::NoNaNs))
2493 FMF.setNoNaNs();
2494 if (0 != (Record[OpNum] & FastMathFlags::NoInfs))
2495 FMF.setNoInfs();
2496 if (0 != (Record[OpNum] & FastMathFlags::NoSignedZeros))
2497 FMF.setNoSignedZeros();
2498 if (0 != (Record[OpNum] & FastMathFlags::AllowReciprocal))
2499 FMF.setAllowReciprocal();
Michael Ilseman9978d7e2012-11-27 00:43:38 +00002500 if (FMF.any())
2501 I->setFastMathFlags(FMF);
Dan Gohman1b849082009-09-07 23:54:19 +00002502 }
Michael Ilseman9978d7e2012-11-27 00:43:38 +00002503
Dan Gohman1b849082009-09-07 23:54:19 +00002504 }
Chris Lattner85b7b402007-05-01 05:52:21 +00002505 break;
2506 }
Chris Lattnere9759c22007-05-06 00:21:25 +00002507 case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc]
2508 unsigned OpNum = 0;
2509 Value *Op;
2510 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
2511 OpNum+2 != Record.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002512 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002513
Chris Lattner229907c2011-07-18 04:54:35 +00002514 Type *ResTy = getTypeByID(Record[OpNum]);
Chris Lattnere9759c22007-05-06 00:21:25 +00002515 int Opc = GetDecodedCastOpcode(Record[OpNum+1]);
Craig Topper2617dcc2014-04-15 06:32:26 +00002516 if (Opc == -1 || !ResTy)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002517 return Error(BitcodeError::InvalidRecord);
Craig Topper2617dcc2014-04-15 06:32:26 +00002518 Instruction *Temp = nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002519 if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) {
2520 if (Temp) {
2521 InstructionList.push_back(Temp);
2522 CurBB->getInstList().push_back(Temp);
2523 }
2524 } else {
2525 I = CastInst::Create((Instruction::CastOps)Opc, Op, ResTy);
2526 }
Devang Patelaf206b82009-09-18 19:26:43 +00002527 InstructionList.push_back(I);
Chris Lattnere53603e2007-05-02 04:27:25 +00002528 break;
2529 }
Dan Gohman1639c392009-07-27 21:53:46 +00002530 case bitc::FUNC_CODE_INST_INBOUNDS_GEP:
Chris Lattnere14cb882007-05-04 19:11:41 +00002531 case bitc::FUNC_CODE_INST_GEP: { // GEP: [n x operands]
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002532 unsigned OpNum = 0;
2533 Value *BasePtr;
2534 if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002535 return Error(BitcodeError::InvalidRecord);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002536
Chris Lattner5285b5e2007-05-02 05:46:45 +00002537 SmallVector<Value*, 16> GEPIdx;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002538 while (OpNum != Record.size()) {
2539 Value *Op;
2540 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002541 return Error(BitcodeError::InvalidRecord);
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002542 GEPIdx.push_back(Op);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002543 }
2544
Jay Foadd1b78492011-07-25 09:48:08 +00002545 I = GetElementPtrInst::Create(BasePtr, GEPIdx);
Devang Patelaf206b82009-09-18 19:26:43 +00002546 InstructionList.push_back(I);
Dan Gohman1639c392009-07-27 21:53:46 +00002547 if (BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP)
Dan Gohman1b849082009-09-07 23:54:19 +00002548 cast<GetElementPtrInst>(I)->setIsInBounds(true);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002549 break;
2550 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002551
Dan Gohman1ecaf452008-05-31 00:58:22 +00002552 case bitc::FUNC_CODE_INST_EXTRACTVAL: {
2553 // EXTRACTVAL: [opty, opval, n x indices]
Dan Gohman30499842008-05-23 01:55:30 +00002554 unsigned OpNum = 0;
2555 Value *Agg;
2556 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002557 return Error(BitcodeError::InvalidRecord);
Dan Gohman30499842008-05-23 01:55:30 +00002558
Dan Gohman1ecaf452008-05-31 00:58:22 +00002559 SmallVector<unsigned, 4> EXTRACTVALIdx;
2560 for (unsigned RecSize = Record.size();
2561 OpNum != RecSize; ++OpNum) {
2562 uint64_t Index = Record[OpNum];
2563 if ((unsigned)Index != Index)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002564 return Error(BitcodeError::InvalidValue);
Dan Gohman1ecaf452008-05-31 00:58:22 +00002565 EXTRACTVALIdx.push_back((unsigned)Index);
Dan Gohman30499842008-05-23 01:55:30 +00002566 }
2567
Jay Foad57aa6362011-07-13 10:26:04 +00002568 I = ExtractValueInst::Create(Agg, EXTRACTVALIdx);
Devang Patelaf206b82009-09-18 19:26:43 +00002569 InstructionList.push_back(I);
Dan Gohman30499842008-05-23 01:55:30 +00002570 break;
2571 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002572
Dan Gohman1ecaf452008-05-31 00:58:22 +00002573 case bitc::FUNC_CODE_INST_INSERTVAL: {
2574 // INSERTVAL: [opty, opval, opty, opval, n x indices]
Dan Gohman30499842008-05-23 01:55:30 +00002575 unsigned OpNum = 0;
2576 Value *Agg;
2577 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002578 return Error(BitcodeError::InvalidRecord);
Dan Gohman30499842008-05-23 01:55:30 +00002579 Value *Val;
2580 if (getValueTypePair(Record, OpNum, NextValueNo, Val))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002581 return Error(BitcodeError::InvalidRecord);
Dan Gohman30499842008-05-23 01:55:30 +00002582
Dan Gohman1ecaf452008-05-31 00:58:22 +00002583 SmallVector<unsigned, 4> INSERTVALIdx;
2584 for (unsigned RecSize = Record.size();
2585 OpNum != RecSize; ++OpNum) {
2586 uint64_t Index = Record[OpNum];
2587 if ((unsigned)Index != Index)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002588 return Error(BitcodeError::InvalidValue);
Dan Gohman1ecaf452008-05-31 00:58:22 +00002589 INSERTVALIdx.push_back((unsigned)Index);
Dan Gohman30499842008-05-23 01:55:30 +00002590 }
2591
Jay Foad57aa6362011-07-13 10:26:04 +00002592 I = InsertValueInst::Create(Agg, Val, INSERTVALIdx);
Devang Patelaf206b82009-09-18 19:26:43 +00002593 InstructionList.push_back(I);
Dan Gohman30499842008-05-23 01:55:30 +00002594 break;
2595 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002596
Chris Lattnere9759c22007-05-06 00:21:25 +00002597 case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval]
Dan Gohmanc5d28922008-09-16 01:01:33 +00002598 // obsolete form of select
2599 // handles select i1 ... in old bitcode
Chris Lattnere9759c22007-05-06 00:21:25 +00002600 unsigned OpNum = 0;
2601 Value *TrueVal, *FalseVal, *Cond;
2602 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002603 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
2604 popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002605 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002606
Dan Gohmanc5d28922008-09-16 01:01:33 +00002607 I = SelectInst::Create(Cond, TrueVal, FalseVal);
Devang Patelaf206b82009-09-18 19:26:43 +00002608 InstructionList.push_back(I);
Dan Gohmanc5d28922008-09-16 01:01:33 +00002609 break;
2610 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002611
Dan Gohmanc5d28922008-09-16 01:01:33 +00002612 case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred]
2613 // new form of select
2614 // handles select i1 or select [N x i1]
2615 unsigned OpNum = 0;
2616 Value *TrueVal, *FalseVal, *Cond;
2617 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002618 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
Dan Gohmanc5d28922008-09-16 01:01:33 +00002619 getValueTypePair(Record, OpNum, NextValueNo, Cond))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002620 return Error(BitcodeError::InvalidRecord);
Dan Gohmanc579d972008-09-09 01:02:47 +00002621
2622 // select condition can be either i1 or [N x i1]
Chris Lattner229907c2011-07-18 04:54:35 +00002623 if (VectorType* vector_type =
2624 dyn_cast<VectorType>(Cond->getType())) {
Dan Gohmanc579d972008-09-09 01:02:47 +00002625 // expect <n x i1>
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002626 if (vector_type->getElementType() != Type::getInt1Ty(Context))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002627 return Error(BitcodeError::InvalidTypeForValue);
Dan Gohmanc579d972008-09-09 01:02:47 +00002628 } else {
2629 // expect i1
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002630 if (Cond->getType() != Type::getInt1Ty(Context))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002631 return Error(BitcodeError::InvalidTypeForValue);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002632 }
2633
Gabor Greife9ecc682008-04-06 20:25:17 +00002634 I = SelectInst::Create(Cond, TrueVal, FalseVal);
Devang Patelaf206b82009-09-18 19:26:43 +00002635 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002636 break;
2637 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002638
Chris Lattner1fc27f02007-05-02 05:16:49 +00002639 case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval]
Chris Lattnere9759c22007-05-06 00:21:25 +00002640 unsigned OpNum = 0;
2641 Value *Vec, *Idx;
2642 if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002643 getValueTypePair(Record, OpNum, NextValueNo, Idx))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002644 return Error(BitcodeError::InvalidRecord);
Eric Christopherc9742252009-07-25 02:28:41 +00002645 I = ExtractElementInst::Create(Vec, Idx);
Devang Patelaf206b82009-09-18 19:26:43 +00002646 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002647 break;
2648 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002649
Chris Lattner1fc27f02007-05-02 05:16:49 +00002650 case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval]
Chris Lattnere9759c22007-05-06 00:21:25 +00002651 unsigned OpNum = 0;
2652 Value *Vec, *Elt, *Idx;
2653 if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002654 popValue(Record, OpNum, NextValueNo,
Chris Lattnere9759c22007-05-06 00:21:25 +00002655 cast<VectorType>(Vec->getType())->getElementType(), Elt) ||
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002656 getValueTypePair(Record, OpNum, NextValueNo, Idx))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002657 return Error(BitcodeError::InvalidRecord);
Gabor Greife9ecc682008-04-06 20:25:17 +00002658 I = InsertElementInst::Create(Vec, Elt, Idx);
Devang Patelaf206b82009-09-18 19:26:43 +00002659 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002660 break;
2661 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002662
Chris Lattnere9759c22007-05-06 00:21:25 +00002663 case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval]
2664 unsigned OpNum = 0;
2665 Value *Vec1, *Vec2, *Mask;
2666 if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002667 popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002668 return Error(BitcodeError::InvalidRecord);
Chris Lattnere9759c22007-05-06 00:21:25 +00002669
Mon P Wang25f01062008-11-10 04:46:22 +00002670 if (getValueTypePair(Record, OpNum, NextValueNo, Mask))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002671 return Error(BitcodeError::InvalidRecord);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002672 I = new ShuffleVectorInst(Vec1, Vec2, Mask);
Devang Patelaf206b82009-09-18 19:26:43 +00002673 InstructionList.push_back(I);
Chris Lattner1fc27f02007-05-02 05:16:49 +00002674 break;
2675 }
Mon P Wang25f01062008-11-10 04:46:22 +00002676
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002677 case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred]
2678 // Old form of ICmp/FCmp returning bool
2679 // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were
2680 // both legal on vectors but had different behaviour.
2681 case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred]
2682 // FCmp/ICmp returning bool or vector of bool
2683
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002684 unsigned OpNum = 0;
2685 Value *LHS, *RHS;
2686 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00002687 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002688 OpNum+1 != Record.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002689 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002690
Duncan Sands9dff9be2010-02-15 16:12:20 +00002691 if (LHS->getType()->isFPOrFPVectorTy())
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002692 I = new FCmpInst((FCmpInst::Predicate)Record[OpNum], LHS, RHS);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002693 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002694 I = new ICmpInst((ICmpInst::Predicate)Record[OpNum], LHS, RHS);
Devang Patelaf206b82009-09-18 19:26:43 +00002695 InstructionList.push_back(I);
Dan Gohmanc579d972008-09-09 01:02:47 +00002696 break;
2697 }
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002698
Chris Lattnere53603e2007-05-02 04:27:25 +00002699 case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>]
Devang Patelbbfd8742008-02-26 01:29:32 +00002700 {
2701 unsigned Size = Record.size();
2702 if (Size == 0) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002703 I = ReturnInst::Create(Context);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002704 InstructionList.push_back(I);
Devang Patelbbfd8742008-02-26 01:29:32 +00002705 break;
Dan Gohmanfa1211f2008-07-23 00:34:11 +00002706 }
Devang Patelbbfd8742008-02-26 01:29:32 +00002707
Dan Gohmanfa1211f2008-07-23 00:34:11 +00002708 unsigned OpNum = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +00002709 Value *Op = nullptr;
Chris Lattnerf1c87102011-06-17 18:09:11 +00002710 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002711 return Error(BitcodeError::InvalidRecord);
Chris Lattnerf1c87102011-06-17 18:09:11 +00002712 if (OpNum != Record.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002713 return Error(BitcodeError::InvalidRecord);
Dan Gohmanfa1211f2008-07-23 00:34:11 +00002714
Chris Lattnerf1c87102011-06-17 18:09:11 +00002715 I = ReturnInst::Create(Context, Op);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002716 InstructionList.push_back(I);
Dan Gohmanfa1211f2008-07-23 00:34:11 +00002717 break;
Chris Lattnere53603e2007-05-02 04:27:25 +00002718 }
Chris Lattner5285b5e2007-05-02 05:46:45 +00002719 case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#]
Chris Lattner6ce15cb2007-05-03 22:09:51 +00002720 if (Record.size() != 1 && Record.size() != 3)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002721 return Error(BitcodeError::InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002722 BasicBlock *TrueDest = getBasicBlock(Record[0]);
Craig Topper2617dcc2014-04-15 06:32:26 +00002723 if (!TrueDest)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002724 return Error(BitcodeError::InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002725
Devang Patelaf206b82009-09-18 19:26:43 +00002726 if (Record.size() == 1) {
Gabor Greife9ecc682008-04-06 20:25:17 +00002727 I = BranchInst::Create(TrueDest);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002728 InstructionList.push_back(I);
Devang Patelaf206b82009-09-18 19:26:43 +00002729 }
Chris Lattner5285b5e2007-05-02 05:46:45 +00002730 else {
2731 BasicBlock *FalseDest = getBasicBlock(Record[1]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002732 Value *Cond = getValue(Record, 2, NextValueNo,
2733 Type::getInt1Ty(Context));
Craig Topper2617dcc2014-04-15 06:32:26 +00002734 if (!FalseDest || !Cond)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002735 return Error(BitcodeError::InvalidRecord);
Gabor Greife9ecc682008-04-06 20:25:17 +00002736 I = BranchInst::Create(TrueDest, FalseDest, Cond);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002737 InstructionList.push_back(I);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002738 }
2739 break;
2740 }
Chris Lattner3ed871f2009-10-27 19:13:16 +00002741 case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...]
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002742 // Check magic
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002743 if ((Record[0] >> 16) == SWITCH_INST_MAGIC) {
Bob Wilsone4077362013-09-09 19:14:35 +00002744 // "New" SwitchInst format with case ranges. The changes to write this
2745 // format were reverted but we still recognize bitcode that uses it.
2746 // Hopefully someday we will have support for case ranges and can use
2747 // this format again.
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002748
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002749 Type *OpTy = getTypeByID(Record[1]);
2750 unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth();
2751
Jan Wen Voungafaced02012-10-11 20:20:40 +00002752 Value *Cond = getValue(Record, 2, NextValueNo, OpTy);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002753 BasicBlock *Default = getBasicBlock(Record[3]);
Craig Topper2617dcc2014-04-15 06:32:26 +00002754 if (!OpTy || !Cond || !Default)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002755 return Error(BitcodeError::InvalidRecord);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002756
2757 unsigned NumCases = Record[4];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002758
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002759 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
2760 InstructionList.push_back(SI);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002761
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002762 unsigned CurIdx = 5;
2763 for (unsigned i = 0; i != NumCases; ++i) {
Bob Wilsone4077362013-09-09 19:14:35 +00002764 SmallVector<ConstantInt*, 1> CaseVals;
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002765 unsigned NumItems = Record[CurIdx++];
2766 for (unsigned ci = 0; ci != NumItems; ++ci) {
2767 bool isSingleNumber = Record[CurIdx++];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002768
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002769 APInt Low;
2770 unsigned ActiveWords = 1;
2771 if (ValueBitWidth > 64)
2772 ActiveWords = Record[CurIdx++];
Benjamin Kramer9704ed02012-05-28 14:10:31 +00002773 Low = ReadWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
2774 ValueBitWidth);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002775 CurIdx += ActiveWords;
Stepan Dyatkovskiye3e19cb2012-05-28 12:39:09 +00002776
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002777 if (!isSingleNumber) {
2778 ActiveWords = 1;
2779 if (ValueBitWidth > 64)
2780 ActiveWords = Record[CurIdx++];
2781 APInt High =
Benjamin Kramer9704ed02012-05-28 14:10:31 +00002782 ReadWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
2783 ValueBitWidth);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002784 CurIdx += ActiveWords;
Bob Wilsone4077362013-09-09 19:14:35 +00002785
2786 // FIXME: It is not clear whether values in the range should be
2787 // compared as signed or unsigned values. The partially
2788 // implemented changes that used this format in the past used
2789 // unsigned comparisons.
2790 for ( ; Low.ule(High); ++Low)
2791 CaseVals.push_back(ConstantInt::get(Context, Low));
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002792 } else
Bob Wilsone4077362013-09-09 19:14:35 +00002793 CaseVals.push_back(ConstantInt::get(Context, Low));
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002794 }
2795 BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]);
Bob Wilsone4077362013-09-09 19:14:35 +00002796 for (SmallVector<ConstantInt*, 1>::iterator cvi = CaseVals.begin(),
2797 cve = CaseVals.end(); cvi != cve; ++cvi)
2798 SI->addCase(*cvi, DestBB);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002799 }
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002800 I = SI;
2801 break;
2802 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002803
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00002804 // Old SwitchInst format without case ranges.
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002805
Chris Lattner5285b5e2007-05-02 05:46:45 +00002806 if (Record.size() < 3 || (Record.size() & 1) == 0)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002807 return Error(BitcodeError::InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00002808 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002809 Value *Cond = getValue(Record, 1, NextValueNo, OpTy);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002810 BasicBlock *Default = getBasicBlock(Record[2]);
Craig Topper2617dcc2014-04-15 06:32:26 +00002811 if (!OpTy || !Cond || !Default)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002812 return Error(BitcodeError::InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002813 unsigned NumCases = (Record.size()-3)/2;
Gabor Greife9ecc682008-04-06 20:25:17 +00002814 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
Devang Patelaf206b82009-09-18 19:26:43 +00002815 InstructionList.push_back(SI);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002816 for (unsigned i = 0, e = NumCases; i != e; ++i) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002817 ConstantInt *CaseVal =
Chris Lattner5285b5e2007-05-02 05:46:45 +00002818 dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy));
2819 BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]);
Craig Topper2617dcc2014-04-15 06:32:26 +00002820 if (!CaseVal || !DestBB) {
Chris Lattner5285b5e2007-05-02 05:46:45 +00002821 delete SI;
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002822 return Error(BitcodeError::InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002823 }
2824 SI->addCase(CaseVal, DestBB);
2825 }
2826 I = SI;
2827 break;
2828 }
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00002829 case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...]
Chris Lattner3ed871f2009-10-27 19:13:16 +00002830 if (Record.size() < 2)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002831 return Error(BitcodeError::InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00002832 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00002833 Value *Address = getValue(Record, 1, NextValueNo, OpTy);
Craig Topper2617dcc2014-04-15 06:32:26 +00002834 if (!OpTy || !Address)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002835 return Error(BitcodeError::InvalidRecord);
Chris Lattner3ed871f2009-10-27 19:13:16 +00002836 unsigned NumDests = Record.size()-2;
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00002837 IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests);
Chris Lattner3ed871f2009-10-27 19:13:16 +00002838 InstructionList.push_back(IBI);
2839 for (unsigned i = 0, e = NumDests; i != e; ++i) {
2840 if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) {
2841 IBI->addDestination(DestBB);
2842 } else {
2843 delete IBI;
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002844 return Error(BitcodeError::InvalidRecord);
Chris Lattner3ed871f2009-10-27 19:13:16 +00002845 }
2846 }
2847 I = IBI;
2848 break;
2849 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002850
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00002851 case bitc::FUNC_CODE_INST_INVOKE: {
2852 // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
Rafael Espindola48da4f42013-11-04 16:16:24 +00002853 if (Record.size() < 4)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002854 return Error(BitcodeError::InvalidRecord);
Bill Wendlinge94d8432012-12-07 23:16:57 +00002855 AttributeSet PAL = getAttributes(Record[0]);
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002856 unsigned CCInfo = Record[1];
2857 BasicBlock *NormalBB = getBasicBlock(Record[2]);
2858 BasicBlock *UnwindBB = getBasicBlock(Record[3]);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002859
Chris Lattner4c0a6d62007-05-08 05:38:01 +00002860 unsigned OpNum = 4;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002861 Value *Callee;
2862 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002863 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002864
Chris Lattner229907c2011-07-18 04:54:35 +00002865 PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType());
Craig Topper2617dcc2014-04-15 06:32:26 +00002866 FunctionType *FTy = !CalleeTy ? nullptr :
Chris Lattner5285b5e2007-05-02 05:46:45 +00002867 dyn_cast<FunctionType>(CalleeTy->getElementType());
2868
2869 // Check that the right number of fixed parameters are here.
Craig Topper2617dcc2014-04-15 06:32:26 +00002870 if (!FTy || !NormalBB || !UnwindBB ||
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002871 Record.size() < OpNum+FTy->getNumParams())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002872 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002873
Chris Lattner5285b5e2007-05-02 05:46:45 +00002874 SmallVector<Value*, 16> Ops;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002875 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00002876 Ops.push_back(getValue(Record, OpNum, NextValueNo,
2877 FTy->getParamType(i)));
Craig Topper2617dcc2014-04-15 06:32:26 +00002878 if (!Ops.back())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002879 return Error(BitcodeError::InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002880 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002881
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002882 if (!FTy->isVarArg()) {
2883 if (Record.size() != OpNum)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002884 return Error(BitcodeError::InvalidRecord);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002885 } else {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002886 // Read type/value pairs for varargs params.
2887 while (OpNum != Record.size()) {
2888 Value *Op;
2889 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002890 return Error(BitcodeError::InvalidRecord);
Chris Lattnerdf1233d2007-05-06 00:00:00 +00002891 Ops.push_back(Op);
2892 }
Chris Lattner5285b5e2007-05-02 05:46:45 +00002893 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002894
Jay Foad5bd375a2011-07-15 08:37:34 +00002895 I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops);
Devang Patelaf206b82009-09-18 19:26:43 +00002896 InstructionList.push_back(I);
Sandeep Patel68c5f472009-09-02 08:44:58 +00002897 cast<InvokeInst>(I)->setCallingConv(
2898 static_cast<CallingConv::ID>(CCInfo));
Devang Patel4c758ea2008-09-25 21:00:45 +00002899 cast<InvokeInst>(I)->setAttributes(PAL);
Chris Lattner5285b5e2007-05-02 05:46:45 +00002900 break;
2901 }
Bill Wendlingf891bf82011-07-31 06:30:59 +00002902 case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval]
2903 unsigned Idx = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +00002904 Value *Val = nullptr;
Bill Wendlingf891bf82011-07-31 06:30:59 +00002905 if (getValueTypePair(Record, Idx, NextValueNo, Val))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002906 return Error(BitcodeError::InvalidRecord);
Bill Wendlingf891bf82011-07-31 06:30:59 +00002907 I = ResumeInst::Create(Val);
Bill Wendlingb9a89992011-09-01 00:50:20 +00002908 InstructionList.push_back(I);
Bill Wendlingf891bf82011-07-31 06:30:59 +00002909 break;
2910 }
Chris Lattnere53603e2007-05-02 04:27:25 +00002911 case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE
Owen Anderson55f1c092009-08-13 21:58:54 +00002912 I = new UnreachableInst(Context);
Devang Patelaf206b82009-09-18 19:26:43 +00002913 InstructionList.push_back(I);
Chris Lattnere53603e2007-05-02 04:27:25 +00002914 break;
Chris Lattnere9759c22007-05-06 00:21:25 +00002915 case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...]
Chris Lattnere14cb882007-05-04 19:11:41 +00002916 if (Record.size() < 1 || ((Record.size()-1)&1))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002917 return Error(BitcodeError::InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00002918 Type *Ty = getTypeByID(Record[0]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00002919 if (!Ty)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002920 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002921
Jay Foad52131342011-03-30 11:28:46 +00002922 PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2);
Devang Patelaf206b82009-09-18 19:26:43 +00002923 InstructionList.push_back(PN);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002924
Chris Lattnere14cb882007-05-04 19:11:41 +00002925 for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) {
Jan Wen Voungafaced02012-10-11 20:20:40 +00002926 Value *V;
2927 // With the new function encoding, it is possible that operands have
2928 // negative IDs (for forward references). Use a signed VBR
2929 // representation to keep the encoding small.
2930 if (UseRelativeIDs)
2931 V = getValueSigned(Record, 1+i, NextValueNo, Ty);
2932 else
2933 V = getValue(Record, 1+i, NextValueNo, Ty);
Chris Lattnere14cb882007-05-04 19:11:41 +00002934 BasicBlock *BB = getBasicBlock(Record[2+i]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00002935 if (!V || !BB)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002936 return Error(BitcodeError::InvalidRecord);
Chris Lattnerc332bba2007-05-03 18:58:09 +00002937 PN->addIncoming(V, BB);
2938 }
2939 I = PN;
2940 break;
2941 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002942
Bill Wendlingfae14752011-08-12 20:24:12 +00002943 case bitc::FUNC_CODE_INST_LANDINGPAD: {
2944 // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?]
2945 unsigned Idx = 0;
2946 if (Record.size() < 4)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002947 return Error(BitcodeError::InvalidRecord);
Bill Wendlingfae14752011-08-12 20:24:12 +00002948 Type *Ty = getTypeByID(Record[Idx++]);
Rafael Espindola48da4f42013-11-04 16:16:24 +00002949 if (!Ty)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002950 return Error(BitcodeError::InvalidRecord);
Craig Topper2617dcc2014-04-15 06:32:26 +00002951 Value *PersFn = nullptr;
Bill Wendlingfae14752011-08-12 20:24:12 +00002952 if (getValueTypePair(Record, Idx, NextValueNo, PersFn))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002953 return Error(BitcodeError::InvalidRecord);
Bill Wendlingfae14752011-08-12 20:24:12 +00002954
2955 bool IsCleanup = !!Record[Idx++];
2956 unsigned NumClauses = Record[Idx++];
2957 LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, NumClauses);
2958 LP->setCleanup(IsCleanup);
2959 for (unsigned J = 0; J != NumClauses; ++J) {
2960 LandingPadInst::ClauseType CT =
2961 LandingPadInst::ClauseType(Record[Idx++]); (void)CT;
2962 Value *Val;
2963
2964 if (getValueTypePair(Record, Idx, NextValueNo, Val)) {
2965 delete LP;
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002966 return Error(BitcodeError::InvalidRecord);
Bill Wendlingfae14752011-08-12 20:24:12 +00002967 }
2968
2969 assert((CT != LandingPadInst::Catch ||
2970 !isa<ArrayType>(Val->getType())) &&
2971 "Catch clause has a invalid type!");
2972 assert((CT != LandingPadInst::Filter ||
2973 isa<ArrayType>(Val->getType())) &&
2974 "Filter clause has invalid type!");
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +00002975 LP->addClause(cast<Constant>(Val));
Bill Wendlingfae14752011-08-12 20:24:12 +00002976 }
2977
2978 I = LP;
Bill Wendlingb9a89992011-09-01 00:50:20 +00002979 InstructionList.push_back(I);
Bill Wendlingfae14752011-08-12 20:24:12 +00002980 break;
2981 }
2982
Chris Lattnerf1c87102011-06-17 18:09:11 +00002983 case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]
2984 if (Record.size() != 4)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002985 return Error(BitcodeError::InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00002986 PointerType *Ty =
Chris Lattnerc332bba2007-05-03 18:58:09 +00002987 dyn_cast_or_null<PointerType>(getTypeByID(Record[0]));
Chris Lattner229907c2011-07-18 04:54:35 +00002988 Type *OpTy = getTypeByID(Record[1]);
Chris Lattnerf1c87102011-06-17 18:09:11 +00002989 Value *Size = getFnValueByID(Record[2], OpTy);
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002990 unsigned AlignRecord = Record[3];
2991 bool InAlloca = AlignRecord & (1 << 5);
2992 unsigned Align = AlignRecord & ((1 << 5) - 1);
Rafael Espindola48da4f42013-11-04 16:16:24 +00002993 if (!Ty || !Size)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00002994 return Error(BitcodeError::InvalidRecord);
Reid Kleckner56b56ea2014-07-16 01:34:27 +00002995 AllocaInst *AI = new AllocaInst(Ty->getElementType(), Size, (1 << Align) >> 1);
2996 AI->setUsedWithInAlloca(InAlloca);
2997 I = AI;
Devang Patelaf206b82009-09-18 19:26:43 +00002998 InstructionList.push_back(I);
Chris Lattnerc332bba2007-05-03 18:58:09 +00002999 break;
3000 }
Chris Lattner9f600c52007-05-03 22:04:19 +00003001 case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol]
Chris Lattnerdf1233d2007-05-06 00:00:00 +00003002 unsigned OpNum = 0;
3003 Value *Op;
3004 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
3005 OpNum+2 != Record.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003006 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003007
Chris Lattnerdf1233d2007-05-06 00:00:00 +00003008 I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1);
Devang Patelaf206b82009-09-18 19:26:43 +00003009 InstructionList.push_back(I);
Chris Lattner83930552007-05-01 07:01:57 +00003010 break;
Chris Lattner9f600c52007-05-03 22:04:19 +00003011 }
Eli Friedman59b66882011-08-09 23:02:53 +00003012 case bitc::FUNC_CODE_INST_LOADATOMIC: {
3013 // LOADATOMIC: [opty, op, align, vol, ordering, synchscope]
3014 unsigned OpNum = 0;
3015 Value *Op;
3016 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
3017 OpNum+4 != Record.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003018 return Error(BitcodeError::InvalidRecord);
Eli Friedman59b66882011-08-09 23:02:53 +00003019
3020 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
3021 if (Ordering == NotAtomic || Ordering == Release ||
3022 Ordering == AcquireRelease)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003023 return Error(BitcodeError::InvalidRecord);
Eli Friedman59b66882011-08-09 23:02:53 +00003024 if (Ordering != NotAtomic && Record[OpNum] == 0)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003025 return Error(BitcodeError::InvalidRecord);
Eli Friedman59b66882011-08-09 23:02:53 +00003026 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
3027
3028 I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1,
3029 Ordering, SynchScope);
3030 InstructionList.push_back(I);
3031 break;
3032 }
Chris Lattnerc44070802011-06-17 18:17:37 +00003033 case bitc::FUNC_CODE_INST_STORE: { // STORE2:[ptrty, ptr, val, align, vol]
Christopher Lamb54dd24c2007-12-11 08:59:05 +00003034 unsigned OpNum = 0;
3035 Value *Val, *Ptr;
3036 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00003037 popValue(Record, OpNum, NextValueNo,
Christopher Lamb54dd24c2007-12-11 08:59:05 +00003038 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
3039 OpNum+2 != Record.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003040 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003041
Christopher Lamb54dd24c2007-12-11 08:59:05 +00003042 I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1);
Devang Patelaf206b82009-09-18 19:26:43 +00003043 InstructionList.push_back(I);
Christopher Lamb54dd24c2007-12-11 08:59:05 +00003044 break;
3045 }
Eli Friedman59b66882011-08-09 23:02:53 +00003046 case bitc::FUNC_CODE_INST_STOREATOMIC: {
3047 // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, synchscope]
3048 unsigned OpNum = 0;
3049 Value *Val, *Ptr;
3050 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00003051 popValue(Record, OpNum, NextValueNo,
Eli Friedman59b66882011-08-09 23:02:53 +00003052 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
3053 OpNum+4 != Record.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003054 return Error(BitcodeError::InvalidRecord);
Eli Friedman59b66882011-08-09 23:02:53 +00003055
3056 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
Eli Friedman222b5a42011-09-19 19:41:28 +00003057 if (Ordering == NotAtomic || Ordering == Acquire ||
Eli Friedman59b66882011-08-09 23:02:53 +00003058 Ordering == AcquireRelease)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003059 return Error(BitcodeError::InvalidRecord);
Eli Friedman59b66882011-08-09 23:02:53 +00003060 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
3061 if (Ordering != NotAtomic && Record[OpNum] == 0)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003062 return Error(BitcodeError::InvalidRecord);
Eli Friedman59b66882011-08-09 23:02:53 +00003063
3064 I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1,
3065 Ordering, SynchScope);
3066 InstructionList.push_back(I);
3067 break;
3068 }
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003069 case bitc::FUNC_CODE_INST_CMPXCHG: {
Tim Northovere94a5182014-03-11 10:48:52 +00003070 // CMPXCHG:[ptrty, ptr, cmp, new, vol, successordering, synchscope,
Tim Northover420a2162014-06-13 14:24:07 +00003071 // failureordering?, isweak?]
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003072 unsigned OpNum = 0;
3073 Value *Ptr, *Cmp, *New;
3074 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00003075 popValue(Record, OpNum, NextValueNo,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003076 cast<PointerType>(Ptr->getType())->getElementType(), Cmp) ||
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(), New) ||
Tim Northover420a2162014-06-13 14:24:07 +00003079 (Record.size() < OpNum + 3 || Record.size() > OpNum + 5))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003080 return Error(BitcodeError::InvalidRecord);
Tim Northovere94a5182014-03-11 10:48:52 +00003081 AtomicOrdering SuccessOrdering = GetDecodedOrdering(Record[OpNum+1]);
3082 if (SuccessOrdering == NotAtomic || SuccessOrdering == Unordered)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003083 return Error(BitcodeError::InvalidRecord);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003084 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+2]);
Tim Northovere94a5182014-03-11 10:48:52 +00003085
3086 AtomicOrdering FailureOrdering;
3087 if (Record.size() < 7)
3088 FailureOrdering =
3089 AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering);
3090 else
3091 FailureOrdering = GetDecodedOrdering(Record[OpNum+3]);
3092
3093 I = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering, FailureOrdering,
3094 SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003095 cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]);
Tim Northover420a2162014-06-13 14:24:07 +00003096
3097 if (Record.size() < 8) {
3098 // Before weak cmpxchgs existed, the instruction simply returned the
3099 // value loaded from memory, so bitcode files from that era will be
3100 // expecting the first component of a modern cmpxchg.
3101 CurBB->getInstList().push_back(I);
3102 I = ExtractValueInst::Create(I, 0);
3103 } else {
3104 cast<AtomicCmpXchgInst>(I)->setWeak(Record[OpNum+4]);
3105 }
3106
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003107 InstructionList.push_back(I);
3108 break;
3109 }
3110 case bitc::FUNC_CODE_INST_ATOMICRMW: {
3111 // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, synchscope]
3112 unsigned OpNum = 0;
3113 Value *Ptr, *Val;
3114 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Jan Wen Voungafaced02012-10-11 20:20:40 +00003115 popValue(Record, OpNum, NextValueNo,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003116 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
3117 OpNum+4 != Record.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003118 return Error(BitcodeError::InvalidRecord);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003119 AtomicRMWInst::BinOp Operation = GetDecodedRMWOperation(Record[OpNum]);
3120 if (Operation < AtomicRMWInst::FIRST_BINOP ||
3121 Operation > AtomicRMWInst::LAST_BINOP)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003122 return Error(BitcodeError::InvalidRecord);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003123 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
Eli Friedman59b66882011-08-09 23:02:53 +00003124 if (Ordering == NotAtomic || Ordering == Unordered)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003125 return Error(BitcodeError::InvalidRecord);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003126 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
3127 I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SynchScope);
3128 cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]);
3129 InstructionList.push_back(I);
3130 break;
3131 }
Eli Friedmanfee02c62011-07-25 23:16:38 +00003132 case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, synchscope]
3133 if (2 != Record.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003134 return Error(BitcodeError::InvalidRecord);
Eli Friedmanfee02c62011-07-25 23:16:38 +00003135 AtomicOrdering Ordering = GetDecodedOrdering(Record[0]);
3136 if (Ordering == NotAtomic || Ordering == Unordered ||
3137 Ordering == Monotonic)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003138 return Error(BitcodeError::InvalidRecord);
Eli Friedmanfee02c62011-07-25 23:16:38 +00003139 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[1]);
3140 I = new FenceInst(Context, Ordering, SynchScope);
3141 InstructionList.push_back(I);
3142 break;
3143 }
Chris Lattnerc44070802011-06-17 18:17:37 +00003144 case bitc::FUNC_CODE_INST_CALL: {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00003145 // CALL: [paramattrs, cc, fnty, fnid, arg0, arg1...]
3146 if (Record.size() < 3)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003147 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003148
Bill Wendlinge94d8432012-12-07 23:16:57 +00003149 AttributeSet PAL = getAttributes(Record[0]);
Chris Lattner4c0a6d62007-05-08 05:38:01 +00003150 unsigned CCInfo = Record[1];
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003151
Chris Lattner4c0a6d62007-05-08 05:38:01 +00003152 unsigned OpNum = 2;
Chris Lattnerdf1233d2007-05-06 00:00:00 +00003153 Value *Callee;
3154 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003155 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003156
Chris Lattner229907c2011-07-18 04:54:35 +00003157 PointerType *OpTy = dyn_cast<PointerType>(Callee->getType());
Craig Topper2617dcc2014-04-15 06:32:26 +00003158 FunctionType *FTy = nullptr;
Chris Lattner9f600c52007-05-03 22:04:19 +00003159 if (OpTy) FTy = dyn_cast<FunctionType>(OpTy->getElementType());
Chris Lattnerdf1233d2007-05-06 00:00:00 +00003160 if (!FTy || Record.size() < FTy->getNumParams()+OpNum)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003161 return Error(BitcodeError::InvalidRecord);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003162
Chris Lattner9f600c52007-05-03 22:04:19 +00003163 SmallVector<Value*, 16> Args;
3164 // Read the fixed params.
Chris Lattnerdf1233d2007-05-06 00:00:00 +00003165 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003166 if (FTy->getParamType(i)->isLabelTy())
Dale Johannesen4646aa32007-11-05 21:20:28 +00003167 Args.push_back(getBasicBlock(Record[OpNum]));
Dan Gohmanbbcd04d2010-09-13 18:00:48 +00003168 else
Jan Wen Voungafaced02012-10-11 20:20:40 +00003169 Args.push_back(getValue(Record, OpNum, NextValueNo,
3170 FTy->getParamType(i)));
Craig Topper2617dcc2014-04-15 06:32:26 +00003171 if (!Args.back())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003172 return Error(BitcodeError::InvalidRecord);
Chris Lattner9f600c52007-05-03 22:04:19 +00003173 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003174
Chris Lattner9f600c52007-05-03 22:04:19 +00003175 // Read type/value pairs for varargs params.
Chris Lattner9f600c52007-05-03 22:04:19 +00003176 if (!FTy->isVarArg()) {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00003177 if (OpNum != Record.size())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003178 return Error(BitcodeError::InvalidRecord);
Chris Lattner9f600c52007-05-03 22:04:19 +00003179 } else {
Chris Lattnerdf1233d2007-05-06 00:00:00 +00003180 while (OpNum != Record.size()) {
3181 Value *Op;
3182 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003183 return Error(BitcodeError::InvalidRecord);
Chris Lattnerdf1233d2007-05-06 00:00:00 +00003184 Args.push_back(Op);
Chris Lattner9f600c52007-05-03 22:04:19 +00003185 }
3186 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003187
Jay Foad5bd375a2011-07-15 08:37:34 +00003188 I = CallInst::Create(Callee, Args);
Devang Patelaf206b82009-09-18 19:26:43 +00003189 InstructionList.push_back(I);
Sandeep Patel68c5f472009-09-02 08:44:58 +00003190 cast<CallInst>(I)->setCallingConv(
Reid Kleckner5772b772014-04-24 20:14:34 +00003191 static_cast<CallingConv::ID>((~(1U << 14) & CCInfo) >> 1));
3192 CallInst::TailCallKind TCK = CallInst::TCK_None;
3193 if (CCInfo & 1)
3194 TCK = CallInst::TCK_Tail;
3195 if (CCInfo & (1 << 14))
3196 TCK = CallInst::TCK_MustTail;
3197 cast<CallInst>(I)->setTailCallKind(TCK);
Devang Patel4c758ea2008-09-25 21:00:45 +00003198 cast<CallInst>(I)->setAttributes(PAL);
Chris Lattner9f600c52007-05-03 22:04:19 +00003199 break;
3200 }
3201 case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty]
3202 if (Record.size() < 3)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003203 return Error(BitcodeError::InvalidRecord);
Chris Lattner229907c2011-07-18 04:54:35 +00003204 Type *OpTy = getTypeByID(Record[0]);
Jan Wen Voungafaced02012-10-11 20:20:40 +00003205 Value *Op = getValue(Record, 1, NextValueNo, OpTy);
Chris Lattner229907c2011-07-18 04:54:35 +00003206 Type *ResTy = getTypeByID(Record[2]);
Chris Lattner9f600c52007-05-03 22:04:19 +00003207 if (!OpTy || !Op || !ResTy)
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003208 return Error(BitcodeError::InvalidRecord);
Chris Lattner9f600c52007-05-03 22:04:19 +00003209 I = new VAArgInst(Op, ResTy);
Devang Patelaf206b82009-09-18 19:26:43 +00003210 InstructionList.push_back(I);
Chris Lattner9f600c52007-05-03 22:04:19 +00003211 break;
3212 }
Chris Lattner83930552007-05-01 07:01:57 +00003213 }
3214
3215 // Add instruction to end of current BB. If there is no current BB, reject
3216 // this file.
Craig Topper2617dcc2014-04-15 06:32:26 +00003217 if (!CurBB) {
Chris Lattner83930552007-05-01 07:01:57 +00003218 delete I;
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003219 return Error(BitcodeError::InvalidInstructionWithNoBB);
Chris Lattner83930552007-05-01 07:01:57 +00003220 }
3221 CurBB->getInstList().push_back(I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003222
Chris Lattner83930552007-05-01 07:01:57 +00003223 // If this was a terminator instruction, move to the next block.
3224 if (isa<TerminatorInst>(I)) {
3225 ++CurBBNo;
Craig Topper2617dcc2014-04-15 06:32:26 +00003226 CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr;
Chris Lattner83930552007-05-01 07:01:57 +00003227 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003228
Chris Lattner83930552007-05-01 07:01:57 +00003229 // Non-void values get registered in the value table for future use.
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00003230 if (I && !I->getType()->isVoidTy())
Chris Lattner83930552007-05-01 07:01:57 +00003231 ValueList.AssignValue(I, NextValueNo++);
Chris Lattner85b7b402007-05-01 05:52:21 +00003232 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003233
Chris Lattner27d38752013-01-20 02:13:19 +00003234OutOfRecordLoop:
Joe Abbey97b7a172013-02-06 22:14:06 +00003235
Chris Lattner83930552007-05-01 07:01:57 +00003236 // Check the function list for unresolved values.
3237 if (Argument *A = dyn_cast<Argument>(ValueList.back())) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003238 if (!A->getParent()) {
Chris Lattner83930552007-05-01 07:01:57 +00003239 // We found at least one unresolved value. Nuke them all to avoid leaks.
3240 for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){
Craig Topper2617dcc2014-04-15 06:32:26 +00003241 if ((A = dyn_cast_or_null<Argument>(ValueList[i])) && !A->getParent()) {
Owen Andersonb292b8c2009-07-30 23:03:37 +00003242 A->replaceAllUsesWith(UndefValue::get(A->getType()));
Chris Lattner83930552007-05-01 07:01:57 +00003243 delete A;
3244 }
3245 }
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003246 return Error(BitcodeError::NeverResolvedValueFoundInFunction);
Chris Lattner83930552007-05-01 07:01:57 +00003247 }
Chris Lattner83930552007-05-01 07:01:57 +00003248 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003249
Dan Gohman9b9ff462010-08-25 20:23:38 +00003250 // FIXME: Check for unresolved forward-declared metadata references
3251 // and clean up leaks.
3252
Chris Lattner85b7b402007-05-01 05:52:21 +00003253 // Trim the value list down to the size it was before we parsed this function.
3254 ValueList.shrinkTo(ModuleValueListSize);
Dan Gohman26d837d2010-08-25 20:22:53 +00003255 MDValueList.shrinkTo(ModuleMDValueListSize);
Chris Lattner85b7b402007-05-01 05:52:21 +00003256 std::vector<BasicBlock*>().swap(FunctionBBs);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003257 return std::error_code();
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003258}
3259
Rafael Espindola7d712032013-11-05 17:16:08 +00003260/// Find the function body in the bitcode stream
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003261std::error_code BitcodeReader::FindFunctionInStream(
3262 Function *F,
3263 DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003264 while (DeferredFunctionInfoIterator->second == 0) {
3265 if (Stream.AtEndOfStream())
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003266 return Error(BitcodeError::CouldNotFindFunctionInStream);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003267 // ParseModule will parse the next body in the stream and set its
3268 // position in the DeferredFunctionInfo map.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003269 if (std::error_code EC = ParseModule(true))
Rafael Espindola7d712032013-11-05 17:16:08 +00003270 return EC;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003271 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003272 return std::error_code();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003273}
3274
Chris Lattner9eeada92007-05-18 04:02:46 +00003275//===----------------------------------------------------------------------===//
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003276// GVMaterializer implementation
Chris Lattner9eeada92007-05-18 04:02:46 +00003277//===----------------------------------------------------------------------===//
3278
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +00003279void BitcodeReader::releaseBuffer() { Buffer.release(); }
Chris Lattner9eeada92007-05-18 04:02:46 +00003280
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003281bool BitcodeReader::isMaterializable(const GlobalValue *GV) const {
3282 if (const Function *F = dyn_cast<Function>(GV)) {
3283 return F->isDeclaration() &&
3284 DeferredFunctionInfo.count(const_cast<Function*>(F));
3285 }
3286 return false;
3287}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003288
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003289std::error_code BitcodeReader::Materialize(GlobalValue *GV) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003290 Function *F = dyn_cast<Function>(GV);
3291 // If it's not a function or is already material, ignore the request.
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003292 if (!F || !F->isMaterializable())
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003293 return std::error_code();
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003294
3295 DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F);
Chris Lattner9eeada92007-05-18 04:02:46 +00003296 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003297 // If its position is recorded as 0, its body is somewhere in the stream
3298 // but we haven't seen it yet.
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003299 if (DFII->second == 0 && LazyStreamer)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003300 if (std::error_code EC = FindFunctionInStream(F, DFII))
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003301 return EC;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003302
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003303 // Move the bit stream to the saved position of the deferred function body.
3304 Stream.JumpToBit(DFII->second);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003305
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003306 if (std::error_code EC = ParseFunctionBody(F))
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003307 return EC;
Chandler Carruth7132e002007-08-04 01:51:18 +00003308
3309 // Upgrade any old intrinsic calls in the function.
3310 for (UpgradedIntrinsicMap::iterator I = UpgradedIntrinsics.begin(),
3311 E = UpgradedIntrinsics.end(); I != E; ++I) {
3312 if (I->first != I->second) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00003313 for (auto UI = I->first->user_begin(), UE = I->first->user_end();
3314 UI != UE;) {
Chandler Carruth7132e002007-08-04 01:51:18 +00003315 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
3316 UpgradeIntrinsicCall(CI, I->second);
3317 }
3318 }
3319 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003320
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00003321 // Bring in any functions that this function forward-referenced via
3322 // blockaddresses.
3323 return materializeForwardReferencedFunctions();
Chris Lattner9eeada92007-05-18 04:02:46 +00003324}
3325
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003326bool BitcodeReader::isDematerializable(const GlobalValue *GV) const {
3327 const Function *F = dyn_cast<Function>(GV);
3328 if (!F || F->isDeclaration())
3329 return false;
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00003330
3331 // Dematerializing F would leave dangling references that wouldn't be
3332 // reconnected on re-materialization.
3333 if (BlockAddressesTaken.count(F))
3334 return false;
3335
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003336 return DeferredFunctionInfo.count(const_cast<Function*>(F));
3337}
3338
3339void BitcodeReader::Dematerialize(GlobalValue *GV) {
3340 Function *F = dyn_cast<Function>(GV);
3341 // If this function isn't dematerializable, this is a noop.
3342 if (!F || !isDematerializable(F))
Chris Lattner9eeada92007-05-18 04:02:46 +00003343 return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003344
Chris Lattner9eeada92007-05-18 04:02:46 +00003345 assert(DeferredFunctionInfo.count(F) && "No info to read function later?");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003346
Chris Lattner9eeada92007-05-18 04:02:46 +00003347 // Just forget the function body, we can remat it later.
3348 F->deleteBody();
Chris Lattner9eeada92007-05-18 04:02:46 +00003349}
3350
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003351std::error_code BitcodeReader::MaterializeModule(Module *M) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003352 assert(M == TheModule &&
3353 "Can only Materialize the Module this BitcodeReader is attached to.");
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00003354
3355 // Promise to materialize all forward references.
3356 WillMaterializeAllForwardRefs = true;
3357
Chris Lattner06310bf2009-06-16 05:15:21 +00003358 // Iterate over the module, deserializing any functions that are still on
3359 // disk.
3360 for (Module::iterator F = TheModule->begin(), E = TheModule->end();
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003361 F != E; ++F) {
3362 if (F->isMaterializable()) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003363 if (std::error_code EC = Materialize(F))
Rafael Espindola2b11ad42013-11-05 19:36:34 +00003364 return EC;
3365 }
3366 }
Derek Schuff92ef9752012-02-29 00:07:09 +00003367 // At this point, if there are any function bodies, the current bit is
3368 // pointing to the END_BLOCK record after them. Now make sure the rest
3369 // of the bits in the module have been read.
3370 if (NextUnreadBit)
3371 ParseModule(true);
3372
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00003373 // Check that all block address forward references got resolved (as we
3374 // promised above).
Duncan P. N. Exon Smith00f20ac2014-08-01 21:51:52 +00003375 if (!BasicBlockFwdRefs.empty())
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00003376 return Error(BitcodeError::NeverResolvedFunctionFromBlockAddress);
3377
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003378 // Upgrade any intrinsic calls that slipped through (should not happen!) and
3379 // delete the old functions to clean up. We can't do this unless the entire
3380 // module is materialized because there could always be another function body
Chandler Carruth7132e002007-08-04 01:51:18 +00003381 // with calls to the old function.
3382 for (std::vector<std::pair<Function*, Function*> >::iterator I =
3383 UpgradedIntrinsics.begin(), E = UpgradedIntrinsics.end(); I != E; ++I) {
3384 if (I->first != I->second) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00003385 for (auto UI = I->first->user_begin(), UE = I->first->user_end();
3386 UI != UE;) {
Chandler Carruth7132e002007-08-04 01:51:18 +00003387 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
3388 UpgradeIntrinsicCall(CI, I->second);
3389 }
Chris Lattner647cffb2009-04-01 01:43:03 +00003390 if (!I->first->use_empty())
3391 I->first->replaceAllUsesWith(I->second);
Chandler Carruth7132e002007-08-04 01:51:18 +00003392 I->first->eraseFromParent();
3393 }
3394 }
3395 std::vector<std::pair<Function*, Function*> >().swap(UpgradedIntrinsics);
Devang Patel80ae3492009-08-28 23:24:31 +00003396
Manman Ren209b17c2013-09-28 00:22:27 +00003397 for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++)
3398 UpgradeInstWithTBAATag(InstsWithTBAATag[I]);
3399
Manman Ren8b4306c2013-12-02 21:29:56 +00003400 UpgradeDebugInfo(*M);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003401 return std::error_code();
Chris Lattner9eeada92007-05-18 04:02:46 +00003402}
3403
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003404std::error_code BitcodeReader::InitStream() {
Rafael Espindola48da4f42013-11-04 16:16:24 +00003405 if (LazyStreamer)
3406 return InitLazyStream();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003407 return InitStreamFromBuffer();
3408}
3409
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003410std::error_code BitcodeReader::InitStreamFromBuffer() {
Roman Divacky4717a8d2012-09-06 15:42:13 +00003411 const unsigned char *BufPtr = (const unsigned char*)Buffer->getBufferStart();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003412 const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
3413
Rafael Espindola27435252014-07-29 21:01:24 +00003414 if (Buffer->getBufferSize() & 3)
3415 return Error(BitcodeError::InvalidBitcodeSignature);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003416
3417 // If we have a wrapper header, parse it and ignore the non-bc file contents.
3418 // The magic number is 0x0B17C0DE stored in little endian.
3419 if (isBitcodeWrapper(BufPtr, BufEnd))
3420 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003421 return Error(BitcodeError::InvalidBitcodeWrapperHeader);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003422
3423 StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
3424 Stream.init(*StreamFile);
3425
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003426 return std::error_code();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003427}
3428
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003429std::error_code BitcodeReader::InitLazyStream() {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003430 // Check and strip off the bitcode wrapper; BitstreamReader expects never to
3431 // see it.
3432 StreamingMemoryObject *Bytes = new StreamingMemoryObject(LazyStreamer);
3433 StreamFile.reset(new BitstreamReader(Bytes));
3434 Stream.init(*StreamFile);
3435
3436 unsigned char buf[16];
Benjamin Kramer534d3a42013-05-24 10:54:58 +00003437 if (Bytes->readBytes(0, 16, buf) == -1)
Rafael Espindola27435252014-07-29 21:01:24 +00003438 return Error(BitcodeError::InvalidBitcodeSignature);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003439
3440 if (!isBitcode(buf, buf + 16))
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003441 return Error(BitcodeError::InvalidBitcodeSignature);
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003442
3443 if (isBitcodeWrapper(buf, buf + 4)) {
3444 const unsigned char *bitcodeStart = buf;
3445 const unsigned char *bitcodeEnd = buf + 16;
3446 SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
3447 Bytes->dropLeadingBytes(bitcodeStart - buf);
3448 Bytes->setKnownObjectSize(bitcodeEnd - bitcodeStart);
3449 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003450 return std::error_code();
Rafael Espindola48da4f42013-11-04 16:16:24 +00003451}
3452
3453namespace {
Rafael Espindola25188c92014-06-12 01:45:43 +00003454class BitcodeErrorCategoryType : public std::error_category {
Rafael Espindolaf5d07fa2014-06-10 21:26:47 +00003455 const char *name() const LLVM_NOEXCEPT override {
Rafael Espindola48da4f42013-11-04 16:16:24 +00003456 return "llvm.bitcode";
3457 }
Craig Topper73156022014-03-02 09:09:27 +00003458 std::string message(int IE) const override {
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003459 BitcodeError E = static_cast<BitcodeError>(IE);
Rafael Espindola48da4f42013-11-04 16:16:24 +00003460 switch (E) {
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003461 case BitcodeError::ConflictingMETADATA_KINDRecords:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003462 return "Conflicting METADATA_KIND records";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003463 case BitcodeError::CouldNotFindFunctionInStream:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003464 return "Could not find function in stream";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003465 case BitcodeError::ExpectedConstant:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003466 return "Expected a constant";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003467 case BitcodeError::InsufficientFunctionProtos:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003468 return "Insufficient function protos";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003469 case BitcodeError::InvalidBitcodeSignature:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003470 return "Invalid bitcode signature";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003471 case BitcodeError::InvalidBitcodeWrapperHeader:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003472 return "Invalid bitcode wrapper header";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003473 case BitcodeError::InvalidConstantReference:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003474 return "Invalid ronstant reference";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003475 case BitcodeError::InvalidID:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003476 return "Invalid ID";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003477 case BitcodeError::InvalidInstructionWithNoBB:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003478 return "Invalid instruction with no BB";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003479 case BitcodeError::InvalidRecord:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003480 return "Invalid record";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003481 case BitcodeError::InvalidTypeForValue:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003482 return "Invalid type for value";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003483 case BitcodeError::InvalidTYPETable:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003484 return "Invalid TYPE table";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003485 case BitcodeError::InvalidType:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003486 return "Invalid type";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003487 case BitcodeError::MalformedBlock:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003488 return "Malformed block";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003489 case BitcodeError::MalformedGlobalInitializerSet:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003490 return "Malformed global initializer set";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003491 case BitcodeError::InvalidMultipleBlocks:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003492 return "Invalid multiple blocks";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003493 case BitcodeError::NeverResolvedValueFoundInFunction:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003494 return "Never resolved value found in function";
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00003495 case BitcodeError::NeverResolvedFunctionFromBlockAddress:
3496 return "Never resolved function from blockaddress";
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003497 case BitcodeError::InvalidValue:
Rafael Espindola48da4f42013-11-04 16:16:24 +00003498 return "Invalid value";
3499 }
Benjamin Kramer77db1632013-11-05 13:45:09 +00003500 llvm_unreachable("Unknown error type!");
Rafael Espindola48da4f42013-11-04 16:16:24 +00003501 }
3502};
3503}
3504
Rafael Espindolac3f2e732014-07-29 20:22:46 +00003505const std::error_category &llvm::BitcodeErrorCategory() {
Rafael Espindola48da4f42013-11-04 16:16:24 +00003506 static BitcodeErrorCategoryType O;
3507 return O;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003508}
Chris Lattner51ffe7c2007-05-01 04:59:48 +00003509
Chris Lattner6694f602007-04-29 07:54:31 +00003510//===----------------------------------------------------------------------===//
3511// External interface
3512//===----------------------------------------------------------------------===//
3513
Duncan P. N. Exon Smith6e1009b2014-08-01 22:27:19 +00003514/// \brief Get a lazy one-at-time loading module from bitcode.
Chris Lattner6694f602007-04-29 07:54:31 +00003515///
Duncan P. N. Exon Smith6e1009b2014-08-01 22:27:19 +00003516/// This isn't always used in a lazy context. In particular, it's also used by
3517/// \a parseBitcodeFile(). If this is truly lazy, then we need to eagerly pull
3518/// in forward-referenced functions from block address references.
3519///
3520/// \param[in] WillMaterializeAll Set to \c true if the caller promises to
3521/// materialize everything -- in particular, if this isn't truly lazy.
3522static ErrorOr<Module *> getLazyBitcodeModuleImpl(MemoryBuffer *Buffer,
3523 LLVMContext &Context,
3524 bool WillMaterializeAll) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003525 Module *M = new Module(Buffer->getBufferIdentifier(), Context);
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +00003526 BitcodeReader *R = new BitcodeReader(Buffer, Context);
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003527 M->setMaterializer(R);
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00003528
3529 auto cleanupOnError = [&](std::error_code EC) {
Rafael Espindola8fb31112014-06-18 20:07:35 +00003530 R->releaseBuffer(); // Never take ownership on error.
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003531 delete M; // Also deletes R.
Rafael Espindola5b6c1e82014-01-13 18:31:04 +00003532 return EC;
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00003533 };
Rafael Espindolab7993462012-01-02 07:49:53 +00003534
Duncan P. N. Exon Smith908d8092014-08-01 21:11:34 +00003535 if (std::error_code EC = R->ParseBitcodeInto(M))
3536 return cleanupOnError(EC);
3537
Duncan P. N. Exon Smith6e1009b2014-08-01 22:27:19 +00003538 if (!WillMaterializeAll)
3539 // Resolve forward references from blockaddresses.
3540 if (std::error_code EC = R->materializeForwardReferencedFunctions())
3541 return cleanupOnError(EC);
Rafael Espindolab7993462012-01-02 07:49:53 +00003542
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003543 return M;
Chris Lattner6694f602007-04-29 07:54:31 +00003544}
3545
Duncan P. N. Exon Smith6e1009b2014-08-01 22:27:19 +00003546ErrorOr<Module *> llvm::getLazyBitcodeModule(MemoryBuffer *Buffer,
3547 LLVMContext &Context) {
3548 return getLazyBitcodeModuleImpl(Buffer, Context, false);
3549}
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003550
3551Module *llvm::getStreamedBitcodeModule(const std::string &name,
3552 DataStreamer *streamer,
3553 LLVMContext &Context,
3554 std::string *ErrMsg) {
3555 Module *M = new Module(name, Context);
3556 BitcodeReader *R = new BitcodeReader(streamer, Context);
3557 M->setMaterializer(R);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00003558 if (std::error_code EC = R->ParseBitcodeInto(M)) {
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003559 if (ErrMsg)
Rafael Espindola48da4f42013-11-04 16:16:24 +00003560 *ErrMsg = EC.message();
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003561 delete M; // Also deletes R.
Craig Topper2617dcc2014-04-15 06:32:26 +00003562 return nullptr;
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003563 }
Derek Schuff8b2dcad2012-02-06 22:30:29 +00003564 return M;
3565}
3566
Alp Tokerde4c0092014-06-27 09:19:14 +00003567ErrorOr<Module *> llvm::parseBitcodeFile(MemoryBuffer *Buffer,
Rafael Espindola8f31e212014-01-15 01:08:23 +00003568 LLVMContext &Context) {
Duncan P. N. Exon Smith6e1009b2014-08-01 22:27:19 +00003569 ErrorOr<Module *> ModuleOrErr =
3570 getLazyBitcodeModuleImpl(Buffer, Context, true);
Rafael Espindola8f31e212014-01-15 01:08:23 +00003571 if (!ModuleOrErr)
3572 return ModuleOrErr;
Rafael Espindola5b6c1e82014-01-13 18:31:04 +00003573 Module *M = ModuleOrErr.get();
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003574 // Read in the entire module, and destroy the BitcodeReader.
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +00003575 if (std::error_code EC = M->materializeAllPermanently(true)) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003576 delete M;
Rafael Espindola8f31e212014-01-15 01:08:23 +00003577 return EC;
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003578 }
Bill Wendling0198ce02010-10-06 01:22:42 +00003579
Chad Rosierca2567b2011-12-07 21:44:12 +00003580 // TODO: Restore the use-lists to the in-memory state when the bitcode was
3581 // written. We must defer until the Module has been fully materialized.
3582
Chris Lattner6694f602007-04-29 07:54:31 +00003583 return M;
3584}
Bill Wendling0198ce02010-10-06 01:22:42 +00003585
Rafael Espindolac75c4fa2014-07-04 20:02:42 +00003586std::string llvm::getBitcodeTargetTriple(MemoryBuffer *Buffer,
3587 LLVMContext &Context) {
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +00003588 BitcodeReader *R = new BitcodeReader(Buffer, Context);
Rafael Espindolac75c4fa2014-07-04 20:02:42 +00003589 ErrorOr<std::string> Triple = R->parseTriple();
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +00003590 R->releaseBuffer();
Bill Wendling0198ce02010-10-06 01:22:42 +00003591 delete R;
Rafael Espindolad346cc82014-07-04 13:52:01 +00003592 if (Triple.getError())
3593 return "";
3594 return Triple.get();
Bill Wendling0198ce02010-10-06 01:22:42 +00003595}