blob: 93654a1dc56757cd80f3360519e5e7299adae87a [file] [log] [blame]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001//===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Lattnercaee0dc2007-04-22 06:23:29 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This header defines the BitcodeReader class.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerc453f762007-04-29 07:54:31 +000014#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattnercaee0dc2007-04-22 06:23:29 +000015#include "BitcodeReader.h"
Chris Lattnere16504e2007-04-24 03:30:34 +000016#include "llvm/Constants.h"
Chris Lattnercaee0dc2007-04-22 06:23:29 +000017#include "llvm/DerivedTypes.h"
Chris Lattner2bce93a2007-05-06 01:58:20 +000018#include "llvm/InlineAsm.h"
Devang Patele4b27562009-08-28 23:24:31 +000019#include "llvm/IntrinsicInst.h"
Chris Lattnercaee0dc2007-04-22 06:23:29 +000020#include "llvm/Module.h"
Dan Gohman1224c382009-07-20 21:19:07 +000021#include "llvm/Operator.h"
Chandler Carruth69940402007-08-04 01:51:18 +000022#include "llvm/AutoUpgrade.h"
Chris Lattner0b2482a2007-04-23 21:26:05 +000023#include "llvm/ADT/SmallString.h"
Devang Patelf4511cd2008-02-26 19:38:17 +000024#include "llvm/ADT/SmallVector.h"
Chris Lattner0eef0802007-04-24 04:04:35 +000025#include "llvm/Support/MathExtras.h"
Chris Lattnerc453f762007-04-29 07:54:31 +000026#include "llvm/Support/MemoryBuffer.h"
Gabor Greifefe65362008-05-10 08:32:32 +000027#include "llvm/OperandTraits.h"
Chris Lattnercaee0dc2007-04-22 06:23:29 +000028using namespace llvm;
29
Chris Lattnerb348bb82007-05-18 04:02:46 +000030void BitcodeReader::FreeState() {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000031 if (BufferOwned)
32 delete Buffer;
Chris Lattnerb348bb82007-05-18 04:02:46 +000033 Buffer = 0;
Chris Lattner1afcace2011-07-09 17:41:24 +000034 std::vector<Type*>().swap(TypeList);
Chris Lattnerb348bb82007-05-18 04:02:46 +000035 ValueList.clear();
Devang Pateld5ac4042009-08-04 06:00:18 +000036 MDValueList.clear();
Daniel Dunbara279bc32009-09-20 02:20:51 +000037
Devang Patel19c87462008-09-26 22:53:05 +000038 std::vector<AttrListPtr>().swap(MAttributes);
Chris Lattnerb348bb82007-05-18 04:02:46 +000039 std::vector<BasicBlock*>().swap(FunctionBBs);
40 std::vector<Function*>().swap(FunctionsWithBodies);
41 DeferredFunctionInfo.clear();
Dan Gohman19538d12010-07-20 21:42:28 +000042 MDKindMap.clear();
Chris Lattnerc453f762007-04-29 07:54:31 +000043}
44
Chris Lattner48c85b82007-05-04 03:30:17 +000045//===----------------------------------------------------------------------===//
46// Helper functions to implement forward reference resolution, etc.
47//===----------------------------------------------------------------------===//
Chris Lattnerc453f762007-04-29 07:54:31 +000048
Chris Lattnercaee0dc2007-04-22 06:23:29 +000049/// ConvertToString - Convert a string from a record into an std::string, return
50/// true on failure.
Chris Lattner0b2482a2007-04-23 21:26:05 +000051template<typename StrTy>
Chris Lattnercaee0dc2007-04-22 06:23:29 +000052static bool ConvertToString(SmallVector<uint64_t, 64> &Record, unsigned Idx,
Chris Lattner0b2482a2007-04-23 21:26:05 +000053 StrTy &Result) {
Chris Lattner15e6d172007-05-04 19:11:41 +000054 if (Idx > Record.size())
Chris Lattnercaee0dc2007-04-22 06:23:29 +000055 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +000056
Chris Lattner15e6d172007-05-04 19:11:41 +000057 for (unsigned i = Idx, e = Record.size(); i != e; ++i)
58 Result += (char)Record[i];
Chris Lattnercaee0dc2007-04-22 06:23:29 +000059 return false;
60}
61
62static GlobalValue::LinkageTypes GetDecodedLinkage(unsigned Val) {
63 switch (Val) {
64 default: // Map unknown/new linkages to external
Bill Wendling3d10a5a2009-07-20 01:03:30 +000065 case 0: return GlobalValue::ExternalLinkage;
66 case 1: return GlobalValue::WeakAnyLinkage;
67 case 2: return GlobalValue::AppendingLinkage;
68 case 3: return GlobalValue::InternalLinkage;
69 case 4: return GlobalValue::LinkOnceAnyLinkage;
70 case 5: return GlobalValue::DLLImportLinkage;
71 case 6: return GlobalValue::DLLExportLinkage;
72 case 7: return GlobalValue::ExternalWeakLinkage;
73 case 8: return GlobalValue::CommonLinkage;
74 case 9: return GlobalValue::PrivateLinkage;
Duncan Sands667d4b82009-03-07 15:45:40 +000075 case 10: return GlobalValue::WeakODRLinkage;
76 case 11: return GlobalValue::LinkOnceODRLinkage;
Chris Lattner266c7bb2009-04-13 05:44:34 +000077 case 12: return GlobalValue::AvailableExternallyLinkage;
Bill Wendling3d10a5a2009-07-20 01:03:30 +000078 case 13: return GlobalValue::LinkerPrivateLinkage;
Bill Wendling5e721d72010-07-01 21:55:59 +000079 case 14: return GlobalValue::LinkerPrivateWeakLinkage;
Bill Wendling55ae5152010-08-20 22:05:50 +000080 case 15: return GlobalValue::LinkerPrivateWeakDefAutoLinkage;
Chris Lattnercaee0dc2007-04-22 06:23:29 +000081 }
82}
83
84static GlobalValue::VisibilityTypes GetDecodedVisibility(unsigned Val) {
85 switch (Val) {
86 default: // Map unknown visibilities to default.
87 case 0: return GlobalValue::DefaultVisibility;
88 case 1: return GlobalValue::HiddenVisibility;
Anton Korobeynikov9cd3ccf2007-04-29 20:56:48 +000089 case 2: return GlobalValue::ProtectedVisibility;
Chris Lattnercaee0dc2007-04-22 06:23:29 +000090 }
91}
92
Chris Lattnerf581c3b2007-04-24 07:07:11 +000093static int GetDecodedCastOpcode(unsigned Val) {
94 switch (Val) {
95 default: return -1;
96 case bitc::CAST_TRUNC : return Instruction::Trunc;
97 case bitc::CAST_ZEXT : return Instruction::ZExt;
98 case bitc::CAST_SEXT : return Instruction::SExt;
99 case bitc::CAST_FPTOUI : return Instruction::FPToUI;
100 case bitc::CAST_FPTOSI : return Instruction::FPToSI;
101 case bitc::CAST_UITOFP : return Instruction::UIToFP;
102 case bitc::CAST_SITOFP : return Instruction::SIToFP;
103 case bitc::CAST_FPTRUNC : return Instruction::FPTrunc;
104 case bitc::CAST_FPEXT : return Instruction::FPExt;
105 case bitc::CAST_PTRTOINT: return Instruction::PtrToInt;
106 case bitc::CAST_INTTOPTR: return Instruction::IntToPtr;
107 case bitc::CAST_BITCAST : return Instruction::BitCast;
108 }
109}
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000110static int GetDecodedBinaryOpcode(unsigned Val, Type *Ty) {
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000111 switch (Val) {
112 default: return -1;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000113 case bitc::BINOP_ADD:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000114 return Ty->isFPOrFPVectorTy() ? Instruction::FAdd : Instruction::Add;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000115 case bitc::BINOP_SUB:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000116 return Ty->isFPOrFPVectorTy() ? Instruction::FSub : Instruction::Sub;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000117 case bitc::BINOP_MUL:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000118 return Ty->isFPOrFPVectorTy() ? Instruction::FMul : Instruction::Mul;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000119 case bitc::BINOP_UDIV: return Instruction::UDiv;
120 case bitc::BINOP_SDIV:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000121 return Ty->isFPOrFPVectorTy() ? Instruction::FDiv : Instruction::SDiv;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000122 case bitc::BINOP_UREM: return Instruction::URem;
123 case bitc::BINOP_SREM:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000124 return Ty->isFPOrFPVectorTy() ? Instruction::FRem : Instruction::SRem;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000125 case bitc::BINOP_SHL: return Instruction::Shl;
126 case bitc::BINOP_LSHR: return Instruction::LShr;
127 case bitc::BINOP_ASHR: return Instruction::AShr;
128 case bitc::BINOP_AND: return Instruction::And;
129 case bitc::BINOP_OR: return Instruction::Or;
130 case bitc::BINOP_XOR: return Instruction::Xor;
131 }
132}
133
Eli Friedmanff030482011-07-28 21:48:00 +0000134static AtomicRMWInst::BinOp GetDecodedRMWOperation(unsigned Val) {
135 switch (Val) {
136 default: return AtomicRMWInst::BAD_BINOP;
137 case bitc::RMW_XCHG: return AtomicRMWInst::Xchg;
138 case bitc::RMW_ADD: return AtomicRMWInst::Add;
139 case bitc::RMW_SUB: return AtomicRMWInst::Sub;
140 case bitc::RMW_AND: return AtomicRMWInst::And;
141 case bitc::RMW_NAND: return AtomicRMWInst::Nand;
142 case bitc::RMW_OR: return AtomicRMWInst::Or;
143 case bitc::RMW_XOR: return AtomicRMWInst::Xor;
144 case bitc::RMW_MAX: return AtomicRMWInst::Max;
145 case bitc::RMW_MIN: return AtomicRMWInst::Min;
146 case bitc::RMW_UMAX: return AtomicRMWInst::UMax;
147 case bitc::RMW_UMIN: return AtomicRMWInst::UMin;
148 }
149}
150
Eli Friedman47f35132011-07-25 23:16:38 +0000151static AtomicOrdering GetDecodedOrdering(unsigned Val) {
152 switch (Val) {
153 case bitc::ORDERING_NOTATOMIC: return NotAtomic;
154 case bitc::ORDERING_UNORDERED: return Unordered;
155 case bitc::ORDERING_MONOTONIC: return Monotonic;
156 case bitc::ORDERING_ACQUIRE: return Acquire;
157 case bitc::ORDERING_RELEASE: return Release;
158 case bitc::ORDERING_ACQREL: return AcquireRelease;
159 default: // Map unknown orderings to sequentially-consistent.
160 case bitc::ORDERING_SEQCST: return SequentiallyConsistent;
161 }
162}
163
164static SynchronizationScope GetDecodedSynchScope(unsigned Val) {
165 switch (Val) {
166 case bitc::SYNCHSCOPE_SINGLETHREAD: return SingleThread;
167 default: // Map unknown scopes to cross-thread.
168 case bitc::SYNCHSCOPE_CROSSTHREAD: return CrossThread;
169 }
170}
171
Gabor Greifefe65362008-05-10 08:32:32 +0000172namespace llvm {
Chris Lattner522b7b12007-04-24 05:48:56 +0000173namespace {
174 /// @brief A class for maintaining the slot number definition
175 /// as a placeholder for the actual definition for forward constants defs.
176 class ConstantPlaceHolder : public ConstantExpr {
Argyrios Kyrtzidis8c8b9ee2010-08-15 10:27:23 +0000177 void operator=(const ConstantPlaceHolder &); // DO NOT IMPLEMENT
Gabor Greif051a9502008-04-06 20:25:17 +0000178 public:
179 // allocate space for exactly one operand
180 void *operator new(size_t s) {
181 return User::operator new(s, 1);
182 }
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000183 explicit ConstantPlaceHolder(Type *Ty, LLVMContext& Context)
Gabor Greifefe65362008-05-10 08:32:32 +0000184 : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000185 Op<0>() = UndefValue::get(Type::getInt32Ty(Context));
Chris Lattner522b7b12007-04-24 05:48:56 +0000186 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000187
Chris Lattnerea693df2008-08-21 02:34:16 +0000188 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
Chris Lattner17aa6802010-09-04 18:12:00 +0000189 //static inline bool classof(const ConstantPlaceHolder *) { return true; }
Chris Lattnerea693df2008-08-21 02:34:16 +0000190 static bool classof(const Value *V) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000191 return isa<ConstantExpr>(V) &&
Chris Lattnerea693df2008-08-21 02:34:16 +0000192 cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1;
193 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000194
195
Gabor Greifefe65362008-05-10 08:32:32 +0000196 /// Provide fast operand accessors
Chris Lattner46e77402009-03-31 22:55:09 +0000197 //DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Chris Lattner522b7b12007-04-24 05:48:56 +0000198 };
199}
200
Chris Lattner46e77402009-03-31 22:55:09 +0000201// FIXME: can we inherit this from ConstantExpr?
Gabor Greifefe65362008-05-10 08:32:32 +0000202template <>
Jay Foad67c619b2011-01-11 15:07:38 +0000203struct OperandTraits<ConstantPlaceHolder> :
204 public FixedNumOperandTraits<ConstantPlaceHolder, 1> {
Gabor Greifefe65362008-05-10 08:32:32 +0000205};
Gabor Greifefe65362008-05-10 08:32:32 +0000206}
207
Chris Lattner46e77402009-03-31 22:55:09 +0000208
209void BitcodeReaderValueList::AssignValue(Value *V, unsigned Idx) {
210 if (Idx == size()) {
211 push_back(V);
212 return;
213 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000214
Chris Lattner46e77402009-03-31 22:55:09 +0000215 if (Idx >= size())
216 resize(Idx+1);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000217
Chris Lattner46e77402009-03-31 22:55:09 +0000218 WeakVH &OldV = ValuePtrs[Idx];
219 if (OldV == 0) {
220 OldV = V;
221 return;
222 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000223
Chris Lattner46e77402009-03-31 22:55:09 +0000224 // Handle constants and non-constants (e.g. instrs) differently for
225 // efficiency.
226 if (Constant *PHC = dyn_cast<Constant>(&*OldV)) {
227 ResolveConstants.push_back(std::make_pair(PHC, Idx));
228 OldV = V;
229 } else {
230 // If there was a forward reference to this value, replace it.
231 Value *PrevVal = OldV;
232 OldV->replaceAllUsesWith(V);
233 delete PrevVal;
Gabor Greifefe65362008-05-10 08:32:32 +0000234 }
235}
Daniel Dunbara279bc32009-09-20 02:20:51 +0000236
Gabor Greifefe65362008-05-10 08:32:32 +0000237
Chris Lattner522b7b12007-04-24 05:48:56 +0000238Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000239 Type *Ty) {
Chris Lattner46e77402009-03-31 22:55:09 +0000240 if (Idx >= size())
Gabor Greifefe65362008-05-10 08:32:32 +0000241 resize(Idx + 1);
Chris Lattner522b7b12007-04-24 05:48:56 +0000242
Chris Lattner46e77402009-03-31 22:55:09 +0000243 if (Value *V = ValuePtrs[Idx]) {
Chris Lattnera7c49aa2007-05-01 07:01:57 +0000244 assert(Ty == V->getType() && "Type mismatch in constant table!");
245 return cast<Constant>(V);
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000246 }
Chris Lattner522b7b12007-04-24 05:48:56 +0000247
248 // Create and return a placeholder, which will later be RAUW'd.
Owen Anderson74a77812009-07-07 20:18:58 +0000249 Constant *C = new ConstantPlaceHolder(Ty, Context);
Chris Lattner46e77402009-03-31 22:55:09 +0000250 ValuePtrs[Idx] = C;
Chris Lattner522b7b12007-04-24 05:48:56 +0000251 return C;
252}
253
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000254Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, Type *Ty) {
Chris Lattner46e77402009-03-31 22:55:09 +0000255 if (Idx >= size())
Gabor Greifefe65362008-05-10 08:32:32 +0000256 resize(Idx + 1);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000257
Chris Lattner46e77402009-03-31 22:55:09 +0000258 if (Value *V = ValuePtrs[Idx]) {
Chris Lattnera7c49aa2007-05-01 07:01:57 +0000259 assert((Ty == 0 || Ty == V->getType()) && "Type mismatch in value table!");
260 return V;
261 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000262
Chris Lattner01ff65f2007-05-02 05:16:49 +0000263 // No type specified, must be invalid reference.
264 if (Ty == 0) return 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000265
Chris Lattnera7c49aa2007-05-01 07:01:57 +0000266 // Create and return a placeholder, which will later be RAUW'd.
267 Value *V = new Argument(Ty);
Chris Lattner46e77402009-03-31 22:55:09 +0000268 ValuePtrs[Idx] = V;
Chris Lattnera7c49aa2007-05-01 07:01:57 +0000269 return V;
270}
271
Chris Lattnerea693df2008-08-21 02:34:16 +0000272/// ResolveConstantForwardRefs - Once all constants are read, this method bulk
273/// resolves any forward references. The idea behind this is that we sometimes
274/// get constants (such as large arrays) which reference *many* forward ref
275/// constants. Replacing each of these causes a lot of thrashing when
276/// building/reuniquing the constant. Instead of doing this, we look at all the
277/// uses and rewrite all the place holders at once for any constant that uses
278/// a placeholder.
279void BitcodeReaderValueList::ResolveConstantForwardRefs() {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000280 // Sort the values by-pointer so that they are efficient to look up with a
Chris Lattnerea693df2008-08-21 02:34:16 +0000281 // binary search.
282 std::sort(ResolveConstants.begin(), ResolveConstants.end());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000283
Chris Lattnerea693df2008-08-21 02:34:16 +0000284 SmallVector<Constant*, 64> NewOps;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000285
Chris Lattnerea693df2008-08-21 02:34:16 +0000286 while (!ResolveConstants.empty()) {
Chris Lattner46e77402009-03-31 22:55:09 +0000287 Value *RealVal = operator[](ResolveConstants.back().second);
Chris Lattnerea693df2008-08-21 02:34:16 +0000288 Constant *Placeholder = ResolveConstants.back().first;
289 ResolveConstants.pop_back();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000290
Chris Lattnerea693df2008-08-21 02:34:16 +0000291 // Loop over all users of the placeholder, updating them to reference the
292 // new value. If they reference more than one placeholder, update them all
293 // at once.
294 while (!Placeholder->use_empty()) {
Chris Lattnerb6135a02008-08-21 17:31:45 +0000295 Value::use_iterator UI = Placeholder->use_begin();
Gabor Greifc654d1b2010-07-09 16:01:21 +0000296 User *U = *UI;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000297
Chris Lattnerea693df2008-08-21 02:34:16 +0000298 // If the using object isn't uniqued, just update the operands. This
299 // handles instructions and initializers for global variables.
Gabor Greifc654d1b2010-07-09 16:01:21 +0000300 if (!isa<Constant>(U) || isa<GlobalValue>(U)) {
Chris Lattnerb6135a02008-08-21 17:31:45 +0000301 UI.getUse().set(RealVal);
Chris Lattnerea693df2008-08-21 02:34:16 +0000302 continue;
303 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000304
Chris Lattnerea693df2008-08-21 02:34:16 +0000305 // Otherwise, we have a constant that uses the placeholder. Replace that
306 // constant with a new constant that has *all* placeholder uses updated.
Gabor Greifc654d1b2010-07-09 16:01:21 +0000307 Constant *UserC = cast<Constant>(U);
Chris Lattnerea693df2008-08-21 02:34:16 +0000308 for (User::op_iterator I = UserC->op_begin(), E = UserC->op_end();
309 I != E; ++I) {
310 Value *NewOp;
311 if (!isa<ConstantPlaceHolder>(*I)) {
312 // Not a placeholder reference.
313 NewOp = *I;
314 } else if (*I == Placeholder) {
315 // Common case is that it just references this one placeholder.
316 NewOp = RealVal;
317 } else {
318 // Otherwise, look up the placeholder in ResolveConstants.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000319 ResolveConstantsTy::iterator It =
320 std::lower_bound(ResolveConstants.begin(), ResolveConstants.end(),
Chris Lattnerea693df2008-08-21 02:34:16 +0000321 std::pair<Constant*, unsigned>(cast<Constant>(*I),
322 0));
323 assert(It != ResolveConstants.end() && It->first == *I);
Chris Lattner46e77402009-03-31 22:55:09 +0000324 NewOp = operator[](It->second);
Chris Lattnerea693df2008-08-21 02:34:16 +0000325 }
326
327 NewOps.push_back(cast<Constant>(NewOp));
328 }
329
330 // Make the new constant.
331 Constant *NewC;
332 if (ConstantArray *UserCA = dyn_cast<ConstantArray>(UserC)) {
Jay Foad26701082011-06-22 09:24:39 +0000333 NewC = ConstantArray::get(UserCA->getType(), NewOps);
Chris Lattnerea693df2008-08-21 02:34:16 +0000334 } else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) {
Chris Lattnerb065b062011-06-20 04:01:31 +0000335 NewC = ConstantStruct::get(UserCS->getType(), NewOps);
Chris Lattnerea693df2008-08-21 02:34:16 +0000336 } else if (isa<ConstantVector>(UserC)) {
Chris Lattner2ca5c862011-02-15 00:14:00 +0000337 NewC = ConstantVector::get(NewOps);
Nick Lewyckycb337992009-05-10 20:57:05 +0000338 } else {
339 assert(isa<ConstantExpr>(UserC) && "Must be a ConstantExpr.");
Jay Foadb81e4572011-04-13 13:46:01 +0000340 NewC = cast<ConstantExpr>(UserC)->getWithOperands(NewOps);
Chris Lattnerea693df2008-08-21 02:34:16 +0000341 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000342
Chris Lattnerea693df2008-08-21 02:34:16 +0000343 UserC->replaceAllUsesWith(NewC);
344 UserC->destroyConstant();
345 NewOps.clear();
346 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000347
Nick Lewyckycb337992009-05-10 20:57:05 +0000348 // Update all ValueHandles, they should be the only users at this point.
349 Placeholder->replaceAllUsesWith(RealVal);
Chris Lattnerea693df2008-08-21 02:34:16 +0000350 delete Placeholder;
351 }
352}
353
Devang Pateld5ac4042009-08-04 06:00:18 +0000354void BitcodeReaderMDValueList::AssignValue(Value *V, unsigned Idx) {
355 if (Idx == size()) {
356 push_back(V);
357 return;
358 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000359
Devang Pateld5ac4042009-08-04 06:00:18 +0000360 if (Idx >= size())
361 resize(Idx+1);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000362
Devang Pateld5ac4042009-08-04 06:00:18 +0000363 WeakVH &OldV = MDValuePtrs[Idx];
364 if (OldV == 0) {
365 OldV = V;
366 return;
367 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000368
Devang Pateld5ac4042009-08-04 06:00:18 +0000369 // If there was a forward reference to this value, replace it.
Dan Gohman489b29b2010-08-20 22:02:26 +0000370 MDNode *PrevVal = cast<MDNode>(OldV);
Devang Pateld5ac4042009-08-04 06:00:18 +0000371 OldV->replaceAllUsesWith(V);
Dan Gohman489b29b2010-08-20 22:02:26 +0000372 MDNode::deleteTemporary(PrevVal);
Devang Patelc0ff8c82009-09-03 01:38:02 +0000373 // Deleting PrevVal sets Idx value in MDValuePtrs to null. Set new
374 // value for Idx.
375 MDValuePtrs[Idx] = V;
Devang Pateld5ac4042009-08-04 06:00:18 +0000376}
377
378Value *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) {
379 if (Idx >= size())
380 resize(Idx + 1);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000381
Devang Pateld5ac4042009-08-04 06:00:18 +0000382 if (Value *V = MDValuePtrs[Idx]) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000383 assert(V->getType()->isMetadataTy() && "Type mismatch in value table!");
Devang Pateld5ac4042009-08-04 06:00:18 +0000384 return V;
385 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000386
Devang Pateld5ac4042009-08-04 06:00:18 +0000387 // Create and return a placeholder, which will later be RAUW'd.
Jay Foadec9186b2011-04-21 19:59:31 +0000388 Value *V = MDNode::getTemporary(Context, ArrayRef<Value*>());
Devang Pateld5ac4042009-08-04 06:00:18 +0000389 MDValuePtrs[Idx] = V;
390 return V;
391}
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000392
Chris Lattner1afcace2011-07-09 17:41:24 +0000393Type *BitcodeReader::getTypeByID(unsigned ID) {
394 // The type table size is always specified correctly.
395 if (ID >= TypeList.size())
396 return 0;
397
398 if (Type *Ty = TypeList[ID])
399 return Ty;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000400
Chris Lattner1afcace2011-07-09 17:41:24 +0000401 // If we have a forward reference, the only possible case is when it is to a
402 // named struct. Just create a placeholder for now.
403 return TypeList[ID] = StructType::createNamed(Context, "");
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000404}
405
Chris Lattner1afcace2011-07-09 17:41:24 +0000406/// FIXME: Remove in LLVM 3.1, only used by ParseOldTypeTable.
407Type *BitcodeReader::getTypeByIDOrNull(unsigned ID) {
408 if (ID >= TypeList.size())
409 TypeList.resize(ID+1);
410
411 return TypeList[ID];
412}
413
414
Chris Lattner48c85b82007-05-04 03:30:17 +0000415//===----------------------------------------------------------------------===//
416// Functions for parsing blocks from the bitcode file
417//===----------------------------------------------------------------------===//
418
Devang Patel05988662008-09-25 21:00:45 +0000419bool BitcodeReader::ParseAttributeBlock() {
Chris Lattnere17b6582007-05-05 00:17:00 +0000420 if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))
Chris Lattner48c85b82007-05-04 03:30:17 +0000421 return Error("Malformed block record");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000422
Devang Patel19c87462008-09-26 22:53:05 +0000423 if (!MAttributes.empty())
Chris Lattner48c85b82007-05-04 03:30:17 +0000424 return Error("Multiple PARAMATTR blocks found!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000425
Chris Lattner48c85b82007-05-04 03:30:17 +0000426 SmallVector<uint64_t, 64> Record;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000427
Devang Patel05988662008-09-25 21:00:45 +0000428 SmallVector<AttributeWithIndex, 8> Attrs;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000429
Chris Lattner48c85b82007-05-04 03:30:17 +0000430 // Read all the records.
431 while (1) {
432 unsigned Code = Stream.ReadCode();
433 if (Code == bitc::END_BLOCK) {
434 if (Stream.ReadBlockEnd())
435 return Error("Error at end of PARAMATTR block");
436 return false;
437 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000438
Chris Lattner48c85b82007-05-04 03:30:17 +0000439 if (Code == bitc::ENTER_SUBBLOCK) {
440 // No known subblocks, always skip them.
441 Stream.ReadSubBlockID();
442 if (Stream.SkipBlock())
443 return Error("Malformed block record");
444 continue;
445 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000446
Chris Lattner48c85b82007-05-04 03:30:17 +0000447 if (Code == bitc::DEFINE_ABBREV) {
448 Stream.ReadAbbrevRecord();
449 continue;
450 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000451
Chris Lattner48c85b82007-05-04 03:30:17 +0000452 // Read a record.
453 Record.clear();
454 switch (Stream.ReadRecord(Code, Record)) {
455 default: // Default behavior: ignore.
456 break;
457 case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [paramidx0, attr0, ...]
458 if (Record.size() & 1)
459 return Error("Invalid ENTRY record");
460
Chris Lattner9a6cb152008-10-05 18:22:09 +0000461 // FIXME : Remove this autoupgrade code in LLVM 3.0.
Devang Patel19c87462008-09-26 22:53:05 +0000462 // If Function attributes are using index 0 then transfer them
Chris Lattner9a6cb152008-10-05 18:22:09 +0000463 // to index ~0. Index 0 is used for return value attributes but used to be
464 // used for function attributes.
Devang Patel19c87462008-09-26 22:53:05 +0000465 Attributes RetAttribute = Attribute::None;
466 Attributes FnAttribute = Attribute::None;
Chris Lattner48c85b82007-05-04 03:30:17 +0000467 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000468 // FIXME: remove in LLVM 3.0
469 // The alignment is stored as a 16-bit raw value from bits 31--16.
470 // We shift the bits above 31 down by 11 bits.
471
472 unsigned Alignment = (Record[i+1] & (0xffffull << 16)) >> 16;
473 if (Alignment && !isPowerOf2_32(Alignment))
474 return Error("Alignment is not a power of two.");
475
476 Attributes ReconstitutedAttr = Record[i+1] & 0xffff;
477 if (Alignment)
478 ReconstitutedAttr |= Attribute::constructAlignmentFromInt(Alignment);
479 ReconstitutedAttr |= (Record[i+1] & (0xffffull << 32)) >> 11;
480 Record[i+1] = ReconstitutedAttr;
481
Devang Patel19c87462008-09-26 22:53:05 +0000482 if (Record[i] == 0)
483 RetAttribute = Record[i+1];
484 else if (Record[i] == ~0U)
485 FnAttribute = Record[i+1];
486 }
Chris Lattner9a6cb152008-10-05 18:22:09 +0000487
488 unsigned OldRetAttrs = (Attribute::NoUnwind|Attribute::NoReturn|
489 Attribute::ReadOnly|Attribute::ReadNone);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000490
Chris Lattner9a6cb152008-10-05 18:22:09 +0000491 if (FnAttribute == Attribute::None && RetAttribute != Attribute::None &&
492 (RetAttribute & OldRetAttrs) != 0) {
493 if (FnAttribute == Attribute::None) { // add a slot so they get added.
494 Record.push_back(~0U);
495 Record.push_back(0);
Devang Patel19c87462008-09-26 22:53:05 +0000496 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000497
Chris Lattner9a6cb152008-10-05 18:22:09 +0000498 FnAttribute |= RetAttribute & OldRetAttrs;
499 RetAttribute &= ~OldRetAttrs;
Chris Lattner48c85b82007-05-04 03:30:17 +0000500 }
Chris Lattner461edd92008-03-12 02:25:52 +0000501
Devang Patel19c87462008-09-26 22:53:05 +0000502 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
Chris Lattner9a6cb152008-10-05 18:22:09 +0000503 if (Record[i] == 0) {
504 if (RetAttribute != Attribute::None)
505 Attrs.push_back(AttributeWithIndex::get(0, RetAttribute));
506 } else if (Record[i] == ~0U) {
507 if (FnAttribute != Attribute::None)
508 Attrs.push_back(AttributeWithIndex::get(~0U, FnAttribute));
509 } else if (Record[i+1] != Attribute::None)
Devang Patel19c87462008-09-26 22:53:05 +0000510 Attrs.push_back(AttributeWithIndex::get(Record[i], Record[i+1]));
511 }
Devang Patel19c87462008-09-26 22:53:05 +0000512
513 MAttributes.push_back(AttrListPtr::get(Attrs.begin(), Attrs.end()));
Chris Lattner48c85b82007-05-04 03:30:17 +0000514 Attrs.clear();
515 break;
516 }
Duncan Sands5e41f652007-11-20 14:09:29 +0000517 }
Chris Lattner48c85b82007-05-04 03:30:17 +0000518 }
519}
520
Chris Lattner86697142007-05-01 05:01:34 +0000521bool BitcodeReader::ParseTypeTable() {
Chris Lattner1afcace2011-07-09 17:41:24 +0000522 if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW))
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000523 return Error("Malformed block record");
Chris Lattner1afcace2011-07-09 17:41:24 +0000524
525 return ParseTypeTableBody();
526}
Daniel Dunbara279bc32009-09-20 02:20:51 +0000527
Chris Lattner1afcace2011-07-09 17:41:24 +0000528bool BitcodeReader::ParseTypeTableBody() {
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000529 if (!TypeList.empty())
530 return Error("Multiple TYPE_BLOCKs found!");
531
532 SmallVector<uint64_t, 64> Record;
533 unsigned NumRecords = 0;
534
Chris Lattner1afcace2011-07-09 17:41:24 +0000535 SmallString<64> TypeName;
536
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000537 // Read all the records for this type table.
538 while (1) {
539 unsigned Code = Stream.ReadCode();
540 if (Code == bitc::END_BLOCK) {
541 if (NumRecords != TypeList.size())
542 return Error("Invalid type forward reference in TYPE_BLOCK");
Chris Lattnerf66d20d2007-04-24 18:15:21 +0000543 if (Stream.ReadBlockEnd())
544 return Error("Error at end of type table block");
545 return false;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000546 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000547
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000548 if (Code == bitc::ENTER_SUBBLOCK) {
549 // No known subblocks, always skip them.
550 Stream.ReadSubBlockID();
551 if (Stream.SkipBlock())
552 return Error("Malformed block record");
553 continue;
554 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000555
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000556 if (Code == bitc::DEFINE_ABBREV) {
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000557 Stream.ReadAbbrevRecord();
558 continue;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000559 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000560
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000561 // Read a record.
562 Record.clear();
Chris Lattner1afcace2011-07-09 17:41:24 +0000563 Type *ResultTy = 0;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000564 switch (Stream.ReadRecord(Code, Record)) {
Chris Lattner1afcace2011-07-09 17:41:24 +0000565 default: return Error("unknown type in type table");
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000566 case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
567 // TYPE_CODE_NUMENTRY contains a count of the number of types in the
568 // type list. This allows us to reserve space.
569 if (Record.size() < 1)
570 return Error("Invalid TYPE_CODE_NUMENTRY record");
Chris Lattner1afcace2011-07-09 17:41:24 +0000571 TypeList.resize(Record[0]);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000572 continue;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000573 case bitc::TYPE_CODE_VOID: // VOID
Owen Anderson1d0be152009-08-13 21:58:54 +0000574 ResultTy = Type::getVoidTy(Context);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000575 break;
576 case bitc::TYPE_CODE_FLOAT: // FLOAT
Owen Anderson1d0be152009-08-13 21:58:54 +0000577 ResultTy = Type::getFloatTy(Context);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000578 break;
579 case bitc::TYPE_CODE_DOUBLE: // DOUBLE
Owen Anderson1d0be152009-08-13 21:58:54 +0000580 ResultTy = Type::getDoubleTy(Context);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000581 break;
Dale Johannesen320fc8a2007-08-03 01:03:46 +0000582 case bitc::TYPE_CODE_X86_FP80: // X86_FP80
Owen Anderson1d0be152009-08-13 21:58:54 +0000583 ResultTy = Type::getX86_FP80Ty(Context);
Dale Johannesen320fc8a2007-08-03 01:03:46 +0000584 break;
585 case bitc::TYPE_CODE_FP128: // FP128
Owen Anderson1d0be152009-08-13 21:58:54 +0000586 ResultTy = Type::getFP128Ty(Context);
Dale Johannesen320fc8a2007-08-03 01:03:46 +0000587 break;
588 case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128
Owen Anderson1d0be152009-08-13 21:58:54 +0000589 ResultTy = Type::getPPC_FP128Ty(Context);
Dale Johannesen320fc8a2007-08-03 01:03:46 +0000590 break;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000591 case bitc::TYPE_CODE_LABEL: // LABEL
Owen Anderson1d0be152009-08-13 21:58:54 +0000592 ResultTy = Type::getLabelTy(Context);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000593 break;
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000594 case bitc::TYPE_CODE_METADATA: // METADATA
Owen Anderson1d0be152009-08-13 21:58:54 +0000595 ResultTy = Type::getMetadataTy(Context);
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000596 break;
Dale Johannesenbb811a22010-09-10 20:55:01 +0000597 case bitc::TYPE_CODE_X86_MMX: // X86_MMX
598 ResultTy = Type::getX86_MMXTy(Context);
599 break;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000600 case bitc::TYPE_CODE_INTEGER: // INTEGER: [width]
601 if (Record.size() < 1)
602 return Error("Invalid Integer type record");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000603
Owen Anderson1d0be152009-08-13 21:58:54 +0000604 ResultTy = IntegerType::get(Context, Record[0]);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000605 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000606 case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or
Christopher Lambfe63fb92007-12-11 08:59:05 +0000607 // [pointee type, address space]
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000608 if (Record.size() < 1)
609 return Error("Invalid POINTER type record");
Christopher Lambfe63fb92007-12-11 08:59:05 +0000610 unsigned AddressSpace = 0;
611 if (Record.size() == 2)
612 AddressSpace = Record[1];
Chris Lattner1afcace2011-07-09 17:41:24 +0000613 ResultTy = getTypeByID(Record[0]);
614 if (ResultTy == 0) return Error("invalid element type in pointer type");
615 ResultTy = PointerType::get(ResultTy, AddressSpace);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000616 break;
Christopher Lambfe63fb92007-12-11 08:59:05 +0000617 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000618 case bitc::TYPE_CODE_FUNCTION: {
Chris Lattnera1afde72007-11-27 17:48:06 +0000619 // FIXME: attrid is dead, remove it in LLVM 3.0
620 // FUNCTION: [vararg, attrid, retty, paramty x N]
621 if (Record.size() < 3)
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000622 return Error("Invalid FUNCTION type record");
Jay Foad5fdd6c82011-07-12 14:06:48 +0000623 std::vector<Type*> ArgTys;
Chris Lattner1afcace2011-07-09 17:41:24 +0000624 for (unsigned i = 3, e = Record.size(); i != e; ++i) {
625 if (Type *T = getTypeByID(Record[i]))
626 ArgTys.push_back(T);
627 else
628 break;
629 }
630
631 ResultTy = getTypeByID(Record[2]);
632 if (ResultTy == 0 || ArgTys.size() < Record.size()-3)
633 return Error("invalid type in function type");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000634
Chris Lattner1afcace2011-07-09 17:41:24 +0000635 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000636 break;
637 }
Chris Lattner1afcace2011-07-09 17:41:24 +0000638 case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N]
Chris Lattner7108dce2007-05-06 08:21:50 +0000639 if (Record.size() < 1)
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000640 return Error("Invalid STRUCT type record");
Chris Lattner1afcace2011-07-09 17:41:24 +0000641 std::vector<Type*> EltTys;
642 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
643 if (Type *T = getTypeByID(Record[i]))
644 EltTys.push_back(T);
645 else
646 break;
647 }
648 if (EltTys.size() != Record.size()-1)
649 return Error("invalid type in struct type");
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000650 ResultTy = StructType::get(Context, EltTys, Record[0]);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000651 break;
652 }
Chris Lattner1afcace2011-07-09 17:41:24 +0000653 case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N]
654 if (ConvertToString(Record, 0, TypeName))
655 return Error("Invalid STRUCT_NAME record");
656 continue;
657
658 case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N]
659 if (Record.size() < 1)
660 return Error("Invalid STRUCT type record");
661
662 if (NumRecords >= TypeList.size())
663 return Error("invalid TYPE table");
664
665 // Check to see if this was forward referenced, if so fill in the temp.
666 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
667 if (Res) {
668 Res->setName(TypeName);
669 TypeList[NumRecords] = 0;
670 } else // Otherwise, create a new struct.
671 Res = StructType::createNamed(Context, TypeName);
672 TypeName.clear();
673
674 SmallVector<Type*, 8> EltTys;
675 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
676 if (Type *T = getTypeByID(Record[i]))
677 EltTys.push_back(T);
678 else
679 break;
680 }
681 if (EltTys.size() != Record.size()-1)
682 return Error("invalid STRUCT type record");
683 Res->setBody(EltTys, Record[0]);
684 ResultTy = Res;
685 break;
686 }
687 case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: []
688 if (Record.size() != 1)
689 return Error("Invalid OPAQUE type record");
690
691 if (NumRecords >= TypeList.size())
692 return Error("invalid TYPE table");
693
694 // Check to see if this was forward referenced, if so fill in the temp.
695 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
696 if (Res) {
697 Res->setName(TypeName);
698 TypeList[NumRecords] = 0;
699 } else // Otherwise, create a new struct with no body.
700 Res = StructType::createNamed(Context, TypeName);
701 TypeName.clear();
702 ResultTy = Res;
703 break;
704 }
705 case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty]
706 if (Record.size() < 2)
707 return Error("Invalid ARRAY type record");
708 if ((ResultTy = getTypeByID(Record[1])))
709 ResultTy = ArrayType::get(ResultTy, Record[0]);
710 else
711 return Error("Invalid ARRAY type element");
712 break;
713 case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty]
714 if (Record.size() < 2)
715 return Error("Invalid VECTOR type record");
716 if ((ResultTy = getTypeByID(Record[1])))
717 ResultTy = VectorType::get(ResultTy, Record[0]);
718 else
719 return Error("Invalid ARRAY type element");
720 break;
721 }
722
723 if (NumRecords >= TypeList.size())
724 return Error("invalid TYPE table");
725 assert(ResultTy && "Didn't read a type?");
726 assert(TypeList[NumRecords] == 0 && "Already read type?");
727 TypeList[NumRecords++] = ResultTy;
728 }
729}
730
731// FIXME: Remove in LLVM 3.1
732bool BitcodeReader::ParseOldTypeTable() {
733 if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_OLD))
734 return Error("Malformed block record");
735
736 if (!TypeList.empty())
737 return Error("Multiple TYPE_BLOCKs found!");
738
739
740 // While horrible, we have no good ordering of types in the bc file. Just
741 // iteratively parse types out of the bc file in multiple passes until we get
742 // them all. Do this by saving a cursor for the start of the type block.
743 BitstreamCursor StartOfTypeBlockCursor(Stream);
744
745 unsigned NumTypesRead = 0;
746
747 SmallVector<uint64_t, 64> Record;
748RestartScan:
749 unsigned NextTypeID = 0;
750 bool ReadAnyTypes = false;
751
752 // Read all the records for this type table.
753 while (1) {
754 unsigned Code = Stream.ReadCode();
755 if (Code == bitc::END_BLOCK) {
756 if (NextTypeID != TypeList.size())
757 return Error("Invalid type forward reference in TYPE_BLOCK_ID_OLD");
758
759 // If we haven't read all of the types yet, iterate again.
760 if (NumTypesRead != TypeList.size()) {
761 // If we didn't successfully read any types in this pass, then we must
762 // have an unhandled forward reference.
763 if (!ReadAnyTypes)
764 return Error("Obsolete bitcode contains unhandled recursive type");
765
766 Stream = StartOfTypeBlockCursor;
767 goto RestartScan;
768 }
769
770 if (Stream.ReadBlockEnd())
771 return Error("Error at end of type table block");
772 return false;
773 }
774
775 if (Code == bitc::ENTER_SUBBLOCK) {
776 // No known subblocks, always skip them.
777 Stream.ReadSubBlockID();
778 if (Stream.SkipBlock())
779 return Error("Malformed block record");
780 continue;
781 }
782
783 if (Code == bitc::DEFINE_ABBREV) {
784 Stream.ReadAbbrevRecord();
785 continue;
786 }
787
788 // Read a record.
789 Record.clear();
790 Type *ResultTy = 0;
791 switch (Stream.ReadRecord(Code, Record)) {
792 default: return Error("unknown type in type table");
793 case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
794 // TYPE_CODE_NUMENTRY contains a count of the number of types in the
795 // type list. This allows us to reserve space.
796 if (Record.size() < 1)
797 return Error("Invalid TYPE_CODE_NUMENTRY record");
798 TypeList.resize(Record[0]);
799 continue;
800 case bitc::TYPE_CODE_VOID: // VOID
801 ResultTy = Type::getVoidTy(Context);
802 break;
803 case bitc::TYPE_CODE_FLOAT: // FLOAT
804 ResultTy = Type::getFloatTy(Context);
805 break;
806 case bitc::TYPE_CODE_DOUBLE: // DOUBLE
807 ResultTy = Type::getDoubleTy(Context);
808 break;
809 case bitc::TYPE_CODE_X86_FP80: // X86_FP80
810 ResultTy = Type::getX86_FP80Ty(Context);
811 break;
812 case bitc::TYPE_CODE_FP128: // FP128
813 ResultTy = Type::getFP128Ty(Context);
814 break;
815 case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128
816 ResultTy = Type::getPPC_FP128Ty(Context);
817 break;
818 case bitc::TYPE_CODE_LABEL: // LABEL
819 ResultTy = Type::getLabelTy(Context);
820 break;
821 case bitc::TYPE_CODE_METADATA: // METADATA
822 ResultTy = Type::getMetadataTy(Context);
823 break;
824 case bitc::TYPE_CODE_X86_MMX: // X86_MMX
825 ResultTy = Type::getX86_MMXTy(Context);
826 break;
827 case bitc::TYPE_CODE_INTEGER: // INTEGER: [width]
828 if (Record.size() < 1)
829 return Error("Invalid Integer type record");
830 ResultTy = IntegerType::get(Context, Record[0]);
831 break;
832 case bitc::TYPE_CODE_OPAQUE: // OPAQUE
833 if (NextTypeID < TypeList.size() && TypeList[NextTypeID] == 0)
834 ResultTy = StructType::createNamed(Context, "");
835 break;
836 case bitc::TYPE_CODE_STRUCT_OLD: {// STRUCT_OLD
837 if (NextTypeID >= TypeList.size()) break;
838 // If we already read it, don't reprocess.
839 if (TypeList[NextTypeID] &&
840 !cast<StructType>(TypeList[NextTypeID])->isOpaque())
841 break;
842
843 // Set a type.
844 if (TypeList[NextTypeID] == 0)
845 TypeList[NextTypeID] = StructType::createNamed(Context, "");
846
847 std::vector<Type*> EltTys;
848 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
849 if (Type *Elt = getTypeByIDOrNull(Record[i]))
850 EltTys.push_back(Elt);
851 else
852 break;
853 }
854
855 if (EltTys.size() != Record.size()-1)
856 break; // Not all elements are ready.
857
858 cast<StructType>(TypeList[NextTypeID])->setBody(EltTys, Record[0]);
859 ResultTy = TypeList[NextTypeID];
860 TypeList[NextTypeID] = 0;
861 break;
862 }
863 case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or
864 // [pointee type, address space]
865 if (Record.size() < 1)
866 return Error("Invalid POINTER type record");
867 unsigned AddressSpace = 0;
868 if (Record.size() == 2)
869 AddressSpace = Record[1];
870 if ((ResultTy = getTypeByIDOrNull(Record[0])))
871 ResultTy = PointerType::get(ResultTy, AddressSpace);
872 break;
873 }
874 case bitc::TYPE_CODE_FUNCTION: {
875 // FIXME: attrid is dead, remove it in LLVM 3.0
876 // FUNCTION: [vararg, attrid, retty, paramty x N]
877 if (Record.size() < 3)
878 return Error("Invalid FUNCTION type record");
Jay Foad5fdd6c82011-07-12 14:06:48 +0000879 std::vector<Type*> ArgTys;
Chris Lattner1afcace2011-07-09 17:41:24 +0000880 for (unsigned i = 3, e = Record.size(); i != e; ++i) {
881 if (Type *Elt = getTypeByIDOrNull(Record[i]))
882 ArgTys.push_back(Elt);
883 else
884 break;
885 }
886 if (ArgTys.size()+3 != Record.size())
887 break; // Something was null.
888 if ((ResultTy = getTypeByIDOrNull(Record[2])))
889 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
890 break;
891 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000892 case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty]
893 if (Record.size() < 2)
894 return Error("Invalid ARRAY type record");
Chris Lattner1afcace2011-07-09 17:41:24 +0000895 if ((ResultTy = getTypeByIDOrNull(Record[1])))
896 ResultTy = ArrayType::get(ResultTy, Record[0]);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000897 break;
898 case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty]
899 if (Record.size() < 2)
900 return Error("Invalid VECTOR type record");
Chris Lattner1afcace2011-07-09 17:41:24 +0000901 if ((ResultTy = getTypeByIDOrNull(Record[1])))
902 ResultTy = VectorType::get(ResultTy, Record[0]);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000903 break;
904 }
Chris Lattner1afcace2011-07-09 17:41:24 +0000905
906 if (NextTypeID >= TypeList.size())
907 return Error("invalid TYPE table");
908
909 if (ResultTy && TypeList[NextTypeID] == 0) {
910 ++NumTypesRead;
911 ReadAnyTypes = true;
912
913 TypeList[NextTypeID] = ResultTy;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000914 }
Chris Lattner1afcace2011-07-09 17:41:24 +0000915
916 ++NextTypeID;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000917 }
918}
919
920
Chris Lattner1afcace2011-07-09 17:41:24 +0000921bool BitcodeReader::ParseOldTypeSymbolTable() {
922 if (Stream.EnterSubBlock(bitc::TYPE_SYMTAB_BLOCK_ID_OLD))
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000923 return Error("Malformed block record");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000924
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000925 SmallVector<uint64_t, 64> Record;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000926
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000927 // Read all the records for this type table.
928 std::string TypeName;
929 while (1) {
930 unsigned Code = Stream.ReadCode();
Chris Lattnerf66d20d2007-04-24 18:15:21 +0000931 if (Code == bitc::END_BLOCK) {
932 if (Stream.ReadBlockEnd())
933 return Error("Error at end of type symbol table block");
934 return false;
935 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000936
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000937 if (Code == bitc::ENTER_SUBBLOCK) {
938 // No known subblocks, always skip them.
939 Stream.ReadSubBlockID();
940 if (Stream.SkipBlock())
941 return Error("Malformed block record");
942 continue;
943 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000944
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000945 if (Code == bitc::DEFINE_ABBREV) {
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000946 Stream.ReadAbbrevRecord();
947 continue;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000948 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000949
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000950 // Read a record.
951 Record.clear();
952 switch (Stream.ReadRecord(Code, Record)) {
953 default: // Default behavior: unknown type.
954 break;
Chris Lattner15e6d172007-05-04 19:11:41 +0000955 case bitc::TST_CODE_ENTRY: // TST_ENTRY: [typeid, namechar x N]
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000956 if (ConvertToString(Record, 1, TypeName))
957 return Error("Invalid TST_ENTRY record");
958 unsigned TypeID = Record[0];
959 if (TypeID >= TypeList.size())
960 return Error("Invalid Type ID in TST_ENTRY record");
961
Chris Lattner1afcace2011-07-09 17:41:24 +0000962 // Only apply the type name to a struct type with no name.
963 if (StructType *STy = dyn_cast<StructType>(TypeList[TypeID]))
964 if (!STy->isAnonymous() && !STy->hasName())
965 STy->setName(TypeName);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000966 TypeName.clear();
967 break;
968 }
969 }
970}
971
Chris Lattner86697142007-05-01 05:01:34 +0000972bool BitcodeReader::ParseValueSymbolTable() {
Chris Lattnere17b6582007-05-05 00:17:00 +0000973 if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
Chris Lattner0b2482a2007-04-23 21:26:05 +0000974 return Error("Malformed block record");
975
976 SmallVector<uint64_t, 64> Record;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000977
Chris Lattner0b2482a2007-04-23 21:26:05 +0000978 // Read all the records for this value table.
979 SmallString<128> ValueName;
980 while (1) {
981 unsigned Code = Stream.ReadCode();
Chris Lattnerf66d20d2007-04-24 18:15:21 +0000982 if (Code == bitc::END_BLOCK) {
983 if (Stream.ReadBlockEnd())
984 return Error("Error at end of value symbol table block");
985 return false;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000986 }
Chris Lattner0b2482a2007-04-23 21:26:05 +0000987 if (Code == bitc::ENTER_SUBBLOCK) {
988 // No known subblocks, always skip them.
989 Stream.ReadSubBlockID();
990 if (Stream.SkipBlock())
991 return Error("Malformed block record");
992 continue;
993 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000994
Chris Lattner0b2482a2007-04-23 21:26:05 +0000995 if (Code == bitc::DEFINE_ABBREV) {
996 Stream.ReadAbbrevRecord();
997 continue;
998 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000999
Chris Lattner0b2482a2007-04-23 21:26:05 +00001000 // Read a record.
1001 Record.clear();
Bill Wendling5d7a5a42011-04-10 23:18:04 +00001002 switch (Stream.ReadRecord(Code, Record)) {
Chris Lattner0b2482a2007-04-23 21:26:05 +00001003 default: // Default behavior: unknown type.
1004 break;
Chris Lattner15e6d172007-05-04 19:11:41 +00001005 case bitc::VST_CODE_ENTRY: { // VST_ENTRY: [valueid, namechar x N]
Chris Lattner0b2482a2007-04-23 21:26:05 +00001006 if (ConvertToString(Record, 1, ValueName))
Nick Lewycky88b72932009-05-31 06:07:28 +00001007 return Error("Invalid VST_ENTRY record");
Chris Lattner0b2482a2007-04-23 21:26:05 +00001008 unsigned ValueID = Record[0];
1009 if (ValueID >= ValueList.size())
1010 return Error("Invalid Value ID in VST_ENTRY record");
1011 Value *V = ValueList[ValueID];
Daniel Dunbara279bc32009-09-20 02:20:51 +00001012
Daniel Dunbar3f53fa92009-07-26 00:34:27 +00001013 V->setName(StringRef(ValueName.data(), ValueName.size()));
Chris Lattner0b2482a2007-04-23 21:26:05 +00001014 ValueName.clear();
1015 break;
Reid Spencerc8f8a242007-05-04 01:43:33 +00001016 }
Bill Wendling5d7a5a42011-04-10 23:18:04 +00001017 case bitc::VST_CODE_BBENTRY: {
Chris Lattnere825ed52007-05-03 22:18:21 +00001018 if (ConvertToString(Record, 1, ValueName))
1019 return Error("Invalid VST_BBENTRY record");
1020 BasicBlock *BB = getBasicBlock(Record[0]);
1021 if (BB == 0)
1022 return Error("Invalid BB ID in VST_BBENTRY record");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001023
Daniel Dunbar3f53fa92009-07-26 00:34:27 +00001024 BB->setName(StringRef(ValueName.data(), ValueName.size()));
Chris Lattnere825ed52007-05-03 22:18:21 +00001025 ValueName.clear();
1026 break;
Chris Lattner0b2482a2007-04-23 21:26:05 +00001027 }
Reid Spencerc8f8a242007-05-04 01:43:33 +00001028 }
Chris Lattner0b2482a2007-04-23 21:26:05 +00001029 }
1030}
1031
Devang Patele54abc92009-07-22 17:43:22 +00001032bool BitcodeReader::ParseMetadata() {
Devang Patel23598502010-01-11 18:52:33 +00001033 unsigned NextMDValueNo = MDValueList.size();
Devang Patele54abc92009-07-22 17:43:22 +00001034
1035 if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
1036 return Error("Malformed block record");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001037
Devang Patele54abc92009-07-22 17:43:22 +00001038 SmallVector<uint64_t, 64> Record;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001039
Devang Patele54abc92009-07-22 17:43:22 +00001040 // Read all the records.
1041 while (1) {
1042 unsigned Code = Stream.ReadCode();
1043 if (Code == bitc::END_BLOCK) {
1044 if (Stream.ReadBlockEnd())
1045 return Error("Error at end of PARAMATTR block");
1046 return false;
1047 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001048
Devang Patele54abc92009-07-22 17:43:22 +00001049 if (Code == bitc::ENTER_SUBBLOCK) {
1050 // No known subblocks, always skip them.
1051 Stream.ReadSubBlockID();
1052 if (Stream.SkipBlock())
1053 return Error("Malformed block record");
1054 continue;
1055 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001056
Devang Patele54abc92009-07-22 17:43:22 +00001057 if (Code == bitc::DEFINE_ABBREV) {
1058 Stream.ReadAbbrevRecord();
1059 continue;
1060 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001061
Victor Hernandez24e64df2010-01-10 07:14:18 +00001062 bool IsFunctionLocal = false;
Devang Patele54abc92009-07-22 17:43:22 +00001063 // Read a record.
1064 Record.clear();
Dan Gohman9b10dfb2010-09-13 18:00:48 +00001065 Code = Stream.ReadRecord(Code, Record);
1066 switch (Code) {
Devang Patele54abc92009-07-22 17:43:22 +00001067 default: // Default behavior: ignore.
1068 break;
Devang Patelaa993142009-07-29 22:34:41 +00001069 case bitc::METADATA_NAME: {
1070 // Read named of the named metadata.
1071 unsigned NameLength = Record.size();
1072 SmallString<8> Name;
1073 Name.resize(NameLength);
1074 for (unsigned i = 0; i != NameLength; ++i)
1075 Name[i] = Record[i];
1076 Record.clear();
1077 Code = Stream.ReadCode();
1078
Chris Lattner9d61dd92011-06-17 17:50:30 +00001079 // METADATA_NAME is always followed by METADATA_NAMED_NODE.
Dan Gohman70c2fc02010-09-09 23:12:39 +00001080 unsigned NextBitCode = Stream.ReadRecord(Code, Record);
Chris Lattner9d61dd92011-06-17 17:50:30 +00001081 assert(NextBitCode == bitc::METADATA_NAMED_NODE); (void)NextBitCode;
Devang Patelaa993142009-07-29 22:34:41 +00001082
1083 // Read named metadata elements.
1084 unsigned Size = Record.size();
Dan Gohman17aa92c2010-07-21 23:38:33 +00001085 NamedMDNode *NMD = TheModule->getOrInsertNamedMetadata(Name);
Devang Patelaa993142009-07-29 22:34:41 +00001086 for (unsigned i = 0; i != Size; ++i) {
Chris Lattner70644e92010-01-09 02:02:37 +00001087 MDNode *MD = dyn_cast<MDNode>(MDValueList.getValueFwdRef(Record[i]));
1088 if (MD == 0)
1089 return Error("Malformed metadata record");
Dan Gohman17aa92c2010-07-21 23:38:33 +00001090 NMD->addOperand(MD);
Devang Patelaa993142009-07-29 22:34:41 +00001091 }
Devang Patelaa993142009-07-29 22:34:41 +00001092 break;
1093 }
Chris Lattner9d61dd92011-06-17 17:50:30 +00001094 case bitc::METADATA_FN_NODE:
Victor Hernandez24e64df2010-01-10 07:14:18 +00001095 IsFunctionLocal = true;
1096 // fall-through
Chris Lattner9d61dd92011-06-17 17:50:30 +00001097 case bitc::METADATA_NODE: {
Dan Gohmanac809752010-07-13 19:33:27 +00001098 if (Record.size() % 2 == 1)
Chris Lattner9d61dd92011-06-17 17:50:30 +00001099 return Error("Invalid METADATA_NODE record");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001100
Devang Patel104cf9e2009-07-23 01:07:34 +00001101 unsigned Size = Record.size();
1102 SmallVector<Value*, 8> Elts;
1103 for (unsigned i = 0; i != Size; i += 2) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001104 Type *Ty = getTypeByID(Record[i]);
Chris Lattner9d61dd92011-06-17 17:50:30 +00001105 if (!Ty) return Error("Invalid METADATA_NODE record");
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001106 if (Ty->isMetadataTy())
Devang Pateld5ac4042009-08-04 06:00:18 +00001107 Elts.push_back(MDValueList.getValueFwdRef(Record[i+1]));
Benjamin Kramerf0127052010-01-05 13:12:22 +00001108 else if (!Ty->isVoidTy())
Devang Patel104cf9e2009-07-23 01:07:34 +00001109 Elts.push_back(ValueList.getValueFwdRef(Record[i+1], Ty));
1110 else
1111 Elts.push_back(NULL);
1112 }
Jay Foadec9186b2011-04-21 19:59:31 +00001113 Value *V = MDNode::getWhenValsUnresolved(Context, Elts, IsFunctionLocal);
Victor Hernandez24e64df2010-01-10 07:14:18 +00001114 IsFunctionLocal = false;
Devang Patel23598502010-01-11 18:52:33 +00001115 MDValueList.AssignValue(V, NextMDValueNo++);
Devang Patel104cf9e2009-07-23 01:07:34 +00001116 break;
1117 }
Devang Patele54abc92009-07-22 17:43:22 +00001118 case bitc::METADATA_STRING: {
1119 unsigned MDStringLength = Record.size();
1120 SmallString<8> String;
1121 String.resize(MDStringLength);
1122 for (unsigned i = 0; i != MDStringLength; ++i)
1123 String[i] = Record[i];
Daniel Dunbara279bc32009-09-20 02:20:51 +00001124 Value *V = MDString::get(Context,
Owen Anderson647e3012009-07-31 21:35:40 +00001125 StringRef(String.data(), String.size()));
Devang Patel23598502010-01-11 18:52:33 +00001126 MDValueList.AssignValue(V, NextMDValueNo++);
Devang Patele54abc92009-07-22 17:43:22 +00001127 break;
1128 }
Devang Patele8e02132009-09-18 19:26:43 +00001129 case bitc::METADATA_KIND: {
1130 unsigned RecordLength = Record.size();
1131 if (Record.empty() || RecordLength < 2)
Daniel Dunbara279bc32009-09-20 02:20:51 +00001132 return Error("Invalid METADATA_KIND record");
Devang Patele8e02132009-09-18 19:26:43 +00001133 SmallString<8> Name;
1134 Name.resize(RecordLength-1);
Devang Patela2148402009-09-28 21:14:55 +00001135 unsigned Kind = Record[0];
Devang Patele8e02132009-09-18 19:26:43 +00001136 for (unsigned i = 1; i != RecordLength; ++i)
Daniel Dunbara279bc32009-09-20 02:20:51 +00001137 Name[i-1] = Record[i];
Chris Lattner0eb41982009-12-28 20:45:51 +00001138
Chris Lattner08113472009-12-29 09:01:33 +00001139 unsigned NewKind = TheModule->getMDKindID(Name.str());
Dan Gohman19538d12010-07-20 21:42:28 +00001140 if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second)
1141 return Error("Conflicting METADATA_KIND records");
Devang Patele8e02132009-09-18 19:26:43 +00001142 break;
1143 }
Devang Patele54abc92009-07-22 17:43:22 +00001144 }
1145 }
1146}
1147
Chris Lattner0eef0802007-04-24 04:04:35 +00001148/// DecodeSignRotatedValue - Decode a signed value stored with the sign bit in
1149/// the LSB for dense VBR encoding.
1150static uint64_t DecodeSignRotatedValue(uint64_t V) {
1151 if ((V & 1) == 0)
1152 return V >> 1;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001153 if (V != 1)
Chris Lattner0eef0802007-04-24 04:04:35 +00001154 return -(V >> 1);
1155 // There is no such thing as -0 with integers. "-0" really means MININT.
1156 return 1ULL << 63;
1157}
1158
Chris Lattner07d98b42007-04-26 02:46:40 +00001159/// ResolveGlobalAndAliasInits - Resolve all of the initializers for global
1160/// values and aliases that we can.
1161bool BitcodeReader::ResolveGlobalAndAliasInits() {
1162 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist;
1163 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001164
Chris Lattner07d98b42007-04-26 02:46:40 +00001165 GlobalInitWorklist.swap(GlobalInits);
1166 AliasInitWorklist.swap(AliasInits);
1167
1168 while (!GlobalInitWorklist.empty()) {
Chris Lattner198f34a2007-04-26 03:27:58 +00001169 unsigned ValID = GlobalInitWorklist.back().second;
Chris Lattner07d98b42007-04-26 02:46:40 +00001170 if (ValID >= ValueList.size()) {
1171 // Not ready to resolve this yet, it requires something later in the file.
Chris Lattner198f34a2007-04-26 03:27:58 +00001172 GlobalInits.push_back(GlobalInitWorklist.back());
Chris Lattner07d98b42007-04-26 02:46:40 +00001173 } else {
1174 if (Constant *C = dyn_cast<Constant>(ValueList[ValID]))
1175 GlobalInitWorklist.back().first->setInitializer(C);
1176 else
1177 return Error("Global variable initializer is not a constant!");
1178 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001179 GlobalInitWorklist.pop_back();
Chris Lattner07d98b42007-04-26 02:46:40 +00001180 }
1181
1182 while (!AliasInitWorklist.empty()) {
1183 unsigned ValID = AliasInitWorklist.back().second;
1184 if (ValID >= ValueList.size()) {
1185 AliasInits.push_back(AliasInitWorklist.back());
1186 } else {
1187 if (Constant *C = dyn_cast<Constant>(ValueList[ValID]))
Anton Korobeynikov7dde0ff2007-04-28 14:57:59 +00001188 AliasInitWorklist.back().first->setAliasee(C);
Chris Lattner07d98b42007-04-26 02:46:40 +00001189 else
1190 return Error("Alias initializer is not a constant!");
1191 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001192 AliasInitWorklist.pop_back();
Chris Lattner07d98b42007-04-26 02:46:40 +00001193 }
1194 return false;
1195}
1196
Chris Lattner86697142007-05-01 05:01:34 +00001197bool BitcodeReader::ParseConstants() {
Chris Lattnere17b6582007-05-05 00:17:00 +00001198 if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
Chris Lattnere16504e2007-04-24 03:30:34 +00001199 return Error("Malformed block record");
1200
1201 SmallVector<uint64_t, 64> Record;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001202
Chris Lattnere16504e2007-04-24 03:30:34 +00001203 // Read all the records for this value table.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001204 Type *CurTy = Type::getInt32Ty(Context);
Chris Lattner522b7b12007-04-24 05:48:56 +00001205 unsigned NextCstNo = ValueList.size();
Chris Lattnere16504e2007-04-24 03:30:34 +00001206 while (1) {
1207 unsigned Code = Stream.ReadCode();
Chris Lattnerea693df2008-08-21 02:34:16 +00001208 if (Code == bitc::END_BLOCK)
1209 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001210
Chris Lattnere16504e2007-04-24 03:30:34 +00001211 if (Code == bitc::ENTER_SUBBLOCK) {
1212 // No known subblocks, always skip them.
1213 Stream.ReadSubBlockID();
1214 if (Stream.SkipBlock())
1215 return Error("Malformed block record");
1216 continue;
1217 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001218
Chris Lattnere16504e2007-04-24 03:30:34 +00001219 if (Code == bitc::DEFINE_ABBREV) {
1220 Stream.ReadAbbrevRecord();
1221 continue;
1222 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001223
Chris Lattnere16504e2007-04-24 03:30:34 +00001224 // Read a record.
1225 Record.clear();
1226 Value *V = 0;
Dan Gohman1224c382009-07-20 21:19:07 +00001227 unsigned BitCode = Stream.ReadRecord(Code, Record);
1228 switch (BitCode) {
Chris Lattnere16504e2007-04-24 03:30:34 +00001229 default: // Default behavior: unknown constant
1230 case bitc::CST_CODE_UNDEF: // UNDEF
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001231 V = UndefValue::get(CurTy);
Chris Lattnere16504e2007-04-24 03:30:34 +00001232 break;
1233 case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid]
1234 if (Record.empty())
1235 return Error("Malformed CST_SETTYPE record");
1236 if (Record[0] >= TypeList.size())
1237 return Error("Invalid Type ID in CST_SETTYPE record");
1238 CurTy = TypeList[Record[0]];
Chris Lattner0eef0802007-04-24 04:04:35 +00001239 continue; // Skip the ValueList manipulation.
Chris Lattnere16504e2007-04-24 03:30:34 +00001240 case bitc::CST_CODE_NULL: // NULL
Owen Andersona7235ea2009-07-31 20:28:14 +00001241 V = Constant::getNullValue(CurTy);
Chris Lattnere16504e2007-04-24 03:30:34 +00001242 break;
1243 case bitc::CST_CODE_INTEGER: // INTEGER: [intval]
Duncan Sands1df98592010-02-16 11:11:14 +00001244 if (!CurTy->isIntegerTy() || Record.empty())
Chris Lattner0eef0802007-04-24 04:04:35 +00001245 return Error("Invalid CST_INTEGER record");
Owen Andersoneed707b2009-07-24 23:12:02 +00001246 V = ConstantInt::get(CurTy, DecodeSignRotatedValue(Record[0]));
Chris Lattner0eef0802007-04-24 04:04:35 +00001247 break;
Chris Lattner15e6d172007-05-04 19:11:41 +00001248 case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
Duncan Sands1df98592010-02-16 11:11:14 +00001249 if (!CurTy->isIntegerTy() || Record.empty())
Chris Lattner0eef0802007-04-24 04:04:35 +00001250 return Error("Invalid WIDE_INTEGER record");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001251
Chris Lattner15e6d172007-05-04 19:11:41 +00001252 unsigned NumWords = Record.size();
Chris Lattner084a8442007-04-24 17:22:05 +00001253 SmallVector<uint64_t, 8> Words;
1254 Words.resize(NumWords);
Chris Lattner0eef0802007-04-24 04:04:35 +00001255 for (unsigned i = 0; i != NumWords; ++i)
Chris Lattner15e6d172007-05-04 19:11:41 +00001256 Words[i] = DecodeSignRotatedValue(Record[i]);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001257 V = ConstantInt::get(Context,
Owen Andersoneed707b2009-07-24 23:12:02 +00001258 APInt(cast<IntegerType>(CurTy)->getBitWidth(),
Jeffrey Yasskin3ba292d2011-07-18 21:45:40 +00001259 Words));
Chris Lattner0eef0802007-04-24 04:04:35 +00001260 break;
1261 }
Dale Johannesen3f6eb742007-09-11 18:32:33 +00001262 case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval]
Chris Lattner0eef0802007-04-24 04:04:35 +00001263 if (Record.empty())
1264 return Error("Invalid FLOAT record");
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001265 if (CurTy->isFloatTy())
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001266 V = ConstantFP::get(Context, APFloat(APInt(32, (uint32_t)Record[0])));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001267 else if (CurTy->isDoubleTy())
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001268 V = ConstantFP::get(Context, APFloat(APInt(64, Record[0])));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001269 else if (CurTy->isX86_FP80Ty()) {
Dale Johannesen1b25cb22009-03-23 21:16:53 +00001270 // Bits are not stored the same way as a normal i80 APInt, compensate.
1271 uint64_t Rearrange[2];
1272 Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);
1273 Rearrange[1] = Record[0] >> 48;
Jeffrey Yasskin3ba292d2011-07-18 21:45:40 +00001274 V = ConstantFP::get(Context, APFloat(APInt(80, Rearrange)));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001275 } else if (CurTy->isFP128Ty())
Jeffrey Yasskin3ba292d2011-07-18 21:45:40 +00001276 V = ConstantFP::get(Context, APFloat(APInt(128, Record), true));
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001277 else if (CurTy->isPPC_FP128Ty())
Jeffrey Yasskin3ba292d2011-07-18 21:45:40 +00001278 V = ConstantFP::get(Context, APFloat(APInt(128, Record)));
Chris Lattnere16504e2007-04-24 03:30:34 +00001279 else
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001280 V = UndefValue::get(CurTy);
Chris Lattnere16504e2007-04-24 03:30:34 +00001281 break;
Dale Johannesen3f6eb742007-09-11 18:32:33 +00001282 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001283
Chris Lattner15e6d172007-05-04 19:11:41 +00001284 case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]
1285 if (Record.empty())
Chris Lattner522b7b12007-04-24 05:48:56 +00001286 return Error("Invalid CST_AGGREGATE record");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001287
Chris Lattner15e6d172007-05-04 19:11:41 +00001288 unsigned Size = Record.size();
Chris Lattner522b7b12007-04-24 05:48:56 +00001289 std::vector<Constant*> Elts;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001290
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001291 if (StructType *STy = dyn_cast<StructType>(CurTy)) {
Chris Lattner522b7b12007-04-24 05:48:56 +00001292 for (unsigned i = 0; i != Size; ++i)
Chris Lattner15e6d172007-05-04 19:11:41 +00001293 Elts.push_back(ValueList.getConstantFwdRef(Record[i],
Chris Lattner522b7b12007-04-24 05:48:56 +00001294 STy->getElementType(i)));
Owen Anderson8fa33382009-07-27 22:29:26 +00001295 V = ConstantStruct::get(STy, Elts);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001296 } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) {
1297 Type *EltTy = ATy->getElementType();
Chris Lattner522b7b12007-04-24 05:48:56 +00001298 for (unsigned i = 0; i != Size; ++i)
Chris Lattner15e6d172007-05-04 19:11:41 +00001299 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
Owen Anderson1fd70962009-07-28 18:32:17 +00001300 V = ConstantArray::get(ATy, Elts);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001301 } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) {
1302 Type *EltTy = VTy->getElementType();
Chris Lattner522b7b12007-04-24 05:48:56 +00001303 for (unsigned i = 0; i != Size; ++i)
Chris Lattner15e6d172007-05-04 19:11:41 +00001304 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00001305 V = ConstantVector::get(Elts);
Chris Lattner522b7b12007-04-24 05:48:56 +00001306 } else {
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001307 V = UndefValue::get(CurTy);
Chris Lattner522b7b12007-04-24 05:48:56 +00001308 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001309 break;
1310 }
Chris Lattnerff7fc5d2007-05-06 00:35:24 +00001311 case bitc::CST_CODE_STRING: { // STRING: [values]
1312 if (Record.empty())
1313 return Error("Invalid CST_AGGREGATE record");
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001314
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001315 ArrayType *ATy = cast<ArrayType>(CurTy);
1316 Type *EltTy = ATy->getElementType();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001317
Chris Lattnerff7fc5d2007-05-06 00:35:24 +00001318 unsigned Size = Record.size();
1319 std::vector<Constant*> Elts;
Chris Lattnerff7fc5d2007-05-06 00:35:24 +00001320 for (unsigned i = 0; i != Size; ++i)
Owen Andersoneed707b2009-07-24 23:12:02 +00001321 Elts.push_back(ConstantInt::get(EltTy, Record[i]));
Owen Anderson1fd70962009-07-28 18:32:17 +00001322 V = ConstantArray::get(ATy, Elts);
Chris Lattnerff7fc5d2007-05-06 00:35:24 +00001323 break;
1324 }
Chris Lattnercb3d91b2007-05-06 00:53:07 +00001325 case bitc::CST_CODE_CSTRING: { // CSTRING: [values]
1326 if (Record.empty())
1327 return Error("Invalid CST_AGGREGATE record");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001328
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001329 ArrayType *ATy = cast<ArrayType>(CurTy);
1330 Type *EltTy = ATy->getElementType();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001331
Chris Lattnercb3d91b2007-05-06 00:53:07 +00001332 unsigned Size = Record.size();
1333 std::vector<Constant*> Elts;
1334 for (unsigned i = 0; i != Size; ++i)
Owen Andersoneed707b2009-07-24 23:12:02 +00001335 Elts.push_back(ConstantInt::get(EltTy, Record[i]));
Owen Andersona7235ea2009-07-31 20:28:14 +00001336 Elts.push_back(Constant::getNullValue(EltTy));
Owen Anderson1fd70962009-07-28 18:32:17 +00001337 V = ConstantArray::get(ATy, Elts);
Chris Lattnercb3d91b2007-05-06 00:53:07 +00001338 break;
1339 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001340 case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval]
1341 if (Record.size() < 3) return Error("Invalid CE_BINOP record");
1342 int Opc = GetDecodedBinaryOpcode(Record[0], CurTy);
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001343 if (Opc < 0) {
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001344 V = UndefValue::get(CurTy); // Unknown binop.
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001345 } else {
1346 Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy);
1347 Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy);
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001348 unsigned Flags = 0;
1349 if (Record.size() >= 4) {
1350 if (Opc == Instruction::Add ||
1351 Opc == Instruction::Sub ||
Chris Lattnerf067d582011-02-07 16:40:21 +00001352 Opc == Instruction::Mul ||
1353 Opc == Instruction::Shl) {
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001354 if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP))
1355 Flags |= OverflowingBinaryOperator::NoSignedWrap;
1356 if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
1357 Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
Chris Lattner35bda892011-02-06 21:44:57 +00001358 } else if (Opc == Instruction::SDiv ||
Chris Lattnerf067d582011-02-07 16:40:21 +00001359 Opc == Instruction::UDiv ||
1360 Opc == Instruction::LShr ||
1361 Opc == Instruction::AShr) {
Chris Lattner35bda892011-02-06 21:44:57 +00001362 if (Record[3] & (1 << bitc::PEO_EXACT))
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001363 Flags |= SDivOperator::IsExact;
1364 }
1365 }
1366 V = ConstantExpr::get(Opc, LHS, RHS, Flags);
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001367 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001368 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001369 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001370 case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval]
1371 if (Record.size() < 3) return Error("Invalid CE_CAST record");
1372 int Opc = GetDecodedCastOpcode(Record[0]);
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001373 if (Opc < 0) {
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001374 V = UndefValue::get(CurTy); // Unknown cast.
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001375 } else {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001376 Type *OpTy = getTypeByID(Record[1]);
Chris Lattnerbfcc3802007-05-06 07:33:01 +00001377 if (!OpTy) return Error("Invalid CE_CAST record");
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001378 Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy);
Owen Andersonbaf3c402009-07-29 18:55:55 +00001379 V = ConstantExpr::getCast(Opc, Op, CurTy);
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001380 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001381 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001382 }
Dan Gohmandd8004d2009-07-27 21:53:46 +00001383 case bitc::CST_CODE_CE_INBOUNDS_GEP:
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001384 case bitc::CST_CODE_CE_GEP: { // CE_GEP: [n x operands]
Chris Lattner15e6d172007-05-04 19:11:41 +00001385 if (Record.size() & 1) return Error("Invalid CE_GEP record");
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001386 SmallVector<Constant*, 16> Elts;
Chris Lattner15e6d172007-05-04 19:11:41 +00001387 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001388 Type *ElTy = getTypeByID(Record[i]);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001389 if (!ElTy) return Error("Invalid CE_GEP record");
1390 Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], ElTy));
1391 }
Jay Foaddab3d292011-07-21 14:31:17 +00001392 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
Jay Foad4b5e2072011-07-21 15:15:37 +00001393 V = ConstantExpr::getGetElementPtr(Elts[0], Indices,
1394 BitCode ==
1395 bitc::CST_CODE_CE_INBOUNDS_GEP);
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001396 break;
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001397 }
1398 case bitc::CST_CODE_CE_SELECT: // CE_SELECT: [opval#, opval#, opval#]
1399 if (Record.size() < 3) return Error("Invalid CE_SELECT record");
Owen Andersonbaf3c402009-07-29 18:55:55 +00001400 V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0],
Owen Anderson1d0be152009-08-13 21:58:54 +00001401 Type::getInt1Ty(Context)),
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001402 ValueList.getConstantFwdRef(Record[1],CurTy),
1403 ValueList.getConstantFwdRef(Record[2],CurTy));
1404 break;
1405 case bitc::CST_CODE_CE_EXTRACTELT: { // CE_EXTRACTELT: [opty, opval, opval]
1406 if (Record.size() < 3) return Error("Invalid CE_EXTRACTELT record");
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001407 VectorType *OpTy =
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001408 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
1409 if (OpTy == 0) return Error("Invalid CE_EXTRACTELT record");
1410 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
Owen Anderson1d0be152009-08-13 21:58:54 +00001411 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
Owen Andersonbaf3c402009-07-29 18:55:55 +00001412 V = ConstantExpr::getExtractElement(Op0, Op1);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001413 break;
1414 }
1415 case bitc::CST_CODE_CE_INSERTELT: { // CE_INSERTELT: [opval, opval, opval]
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001416 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001417 if (Record.size() < 3 || OpTy == 0)
1418 return Error("Invalid CE_INSERTELT record");
1419 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
1420 Constant *Op1 = ValueList.getConstantFwdRef(Record[1],
1421 OpTy->getElementType());
Owen Anderson1d0be152009-08-13 21:58:54 +00001422 Constant *Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
Owen Andersonbaf3c402009-07-29 18:55:55 +00001423 V = ConstantExpr::getInsertElement(Op0, Op1, Op2);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001424 break;
1425 }
1426 case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval]
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001427 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001428 if (Record.size() < 3 || OpTy == 0)
Nate Begeman0f123cf2009-02-12 21:28:33 +00001429 return Error("Invalid CE_SHUFFLEVEC record");
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001430 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
1431 Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001432 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
Owen Anderson74a77812009-07-07 20:18:58 +00001433 OpTy->getNumElements());
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001434 Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy);
Owen Andersonbaf3c402009-07-29 18:55:55 +00001435 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001436 break;
1437 }
Nate Begeman0f123cf2009-02-12 21:28:33 +00001438 case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval]
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001439 VectorType *RTy = dyn_cast<VectorType>(CurTy);
1440 VectorType *OpTy =
Duncan Sandsf22b7462010-10-28 15:47:26 +00001441 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
Nate Begeman0f123cf2009-02-12 21:28:33 +00001442 if (Record.size() < 4 || RTy == 0 || OpTy == 0)
1443 return Error("Invalid CE_SHUFVEC_EX record");
1444 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
1445 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001446 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
Owen Anderson74a77812009-07-07 20:18:58 +00001447 RTy->getNumElements());
Nate Begeman0f123cf2009-02-12 21:28:33 +00001448 Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy);
Owen Andersonbaf3c402009-07-29 18:55:55 +00001449 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Nate Begeman0f123cf2009-02-12 21:28:33 +00001450 break;
1451 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001452 case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred]
1453 if (Record.size() < 4) return Error("Invalid CE_CMP record");
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001454 Type *OpTy = getTypeByID(Record[0]);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001455 if (OpTy == 0) return Error("Invalid CE_CMP record");
1456 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
1457 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
1458
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001459 if (OpTy->isFPOrFPVectorTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +00001460 V = ConstantExpr::getFCmp(Record[3], Op0, Op1);
Nate Begemanac80ade2008-05-12 19:01:56 +00001461 else
Owen Andersonbaf3c402009-07-29 18:55:55 +00001462 V = ConstantExpr::getICmp(Record[3], Op0, Op1);
Chris Lattnerf581c3b2007-04-24 07:07:11 +00001463 break;
Chris Lattner522b7b12007-04-24 05:48:56 +00001464 }
Chris Lattner2bce93a2007-05-06 01:58:20 +00001465 case bitc::CST_CODE_INLINEASM: {
1466 if (Record.size() < 2) return Error("Invalid INLINEASM record");
1467 std::string AsmStr, ConstrStr;
Dale Johannesen43602982009-10-13 20:46:56 +00001468 bool HasSideEffects = Record[0] & 1;
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +00001469 bool IsAlignStack = Record[0] >> 1;
Chris Lattner2bce93a2007-05-06 01:58:20 +00001470 unsigned AsmStrSize = Record[1];
1471 if (2+AsmStrSize >= Record.size())
1472 return Error("Invalid INLINEASM record");
1473 unsigned ConstStrSize = Record[2+AsmStrSize];
1474 if (3+AsmStrSize+ConstStrSize > Record.size())
1475 return Error("Invalid INLINEASM record");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001476
Chris Lattner2bce93a2007-05-06 01:58:20 +00001477 for (unsigned i = 0; i != AsmStrSize; ++i)
1478 AsmStr += (char)Record[2+i];
1479 for (unsigned i = 0; i != ConstStrSize; ++i)
1480 ConstrStr += (char)Record[3+AsmStrSize+i];
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001481 PointerType *PTy = cast<PointerType>(CurTy);
Chris Lattner2bce93a2007-05-06 01:58:20 +00001482 V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +00001483 AsmStr, ConstrStr, HasSideEffects, IsAlignStack);
Chris Lattner2bce93a2007-05-06 01:58:20 +00001484 break;
1485 }
Chris Lattner50b136d2009-10-28 05:53:48 +00001486 case bitc::CST_CODE_BLOCKADDRESS:{
1487 if (Record.size() < 3) return Error("Invalid CE_BLOCKADDRESS record");
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001488 Type *FnTy = getTypeByID(Record[0]);
Chris Lattner50b136d2009-10-28 05:53:48 +00001489 if (FnTy == 0) return Error("Invalid CE_BLOCKADDRESS record");
1490 Function *Fn =
1491 dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy));
1492 if (Fn == 0) return Error("Invalid CE_BLOCKADDRESS record");
1493
1494 GlobalVariable *FwdRef = new GlobalVariable(*Fn->getParent(),
1495 Type::getInt8Ty(Context),
1496 false, GlobalValue::InternalLinkage,
1497 0, "");
1498 BlockAddrFwdRefs[Fn].push_back(std::make_pair(Record[2], FwdRef));
1499 V = FwdRef;
1500 break;
1501 }
Chris Lattnere16504e2007-04-24 03:30:34 +00001502 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001503
Chris Lattnera7c49aa2007-05-01 07:01:57 +00001504 ValueList.AssignValue(V, NextCstNo);
Chris Lattner522b7b12007-04-24 05:48:56 +00001505 ++NextCstNo;
Chris Lattnere16504e2007-04-24 03:30:34 +00001506 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001507
Chris Lattnerea693df2008-08-21 02:34:16 +00001508 if (NextCstNo != ValueList.size())
1509 return Error("Invalid constant reference!");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001510
Chris Lattnerea693df2008-08-21 02:34:16 +00001511 if (Stream.ReadBlockEnd())
1512 return Error("Error at end of constants block");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001513
Chris Lattnerea693df2008-08-21 02:34:16 +00001514 // Once all the constants have been read, go through and resolve forward
1515 // references.
1516 ValueList.ResolveConstantForwardRefs();
1517 return false;
Chris Lattnere16504e2007-04-24 03:30:34 +00001518}
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001519
Chris Lattner980e5aa2007-05-01 05:52:21 +00001520/// RememberAndSkipFunctionBody - When we see the block for a function body,
1521/// remember where it is and then skip it. This lets us lazily deserialize the
1522/// functions.
1523bool BitcodeReader::RememberAndSkipFunctionBody() {
Chris Lattner48f84872007-05-01 04:59:48 +00001524 // Get the function we are talking about.
1525 if (FunctionsWithBodies.empty())
1526 return Error("Insufficient function protos");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001527
Chris Lattner48f84872007-05-01 04:59:48 +00001528 Function *Fn = FunctionsWithBodies.back();
1529 FunctionsWithBodies.pop_back();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001530
Chris Lattner48f84872007-05-01 04:59:48 +00001531 // Save the current stream state.
1532 uint64_t CurBit = Stream.GetCurrentBitNo();
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00001533 DeferredFunctionInfo[Fn] = CurBit;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001534
Chris Lattner48f84872007-05-01 04:59:48 +00001535 // Skip over the function block for now.
1536 if (Stream.SkipBlock())
1537 return Error("Malformed block record");
1538 return false;
1539}
1540
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00001541bool BitcodeReader::ParseModule() {
Chris Lattnere17b6582007-05-05 00:17:00 +00001542 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001543 return Error("Malformed block record");
1544
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001545 SmallVector<uint64_t, 64> Record;
1546 std::vector<std::string> SectionTable;
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001547 std::vector<std::string> GCTable;
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001548
1549 // Read all the records for this module.
1550 while (!Stream.AtEndOfStream()) {
1551 unsigned Code = Stream.ReadCode();
Chris Lattnere84bcb92007-04-24 00:21:45 +00001552 if (Code == bitc::END_BLOCK) {
Chris Lattner980e5aa2007-05-01 05:52:21 +00001553 if (Stream.ReadBlockEnd())
1554 return Error("Error at end of module block");
1555
1556 // Patch the initializers for globals and aliases up.
Chris Lattner07d98b42007-04-26 02:46:40 +00001557 ResolveGlobalAndAliasInits();
1558 if (!GlobalInits.empty() || !AliasInits.empty())
Chris Lattnere84bcb92007-04-24 00:21:45 +00001559 return Error("Malformed global initializer set");
Chris Lattner48f84872007-05-01 04:59:48 +00001560 if (!FunctionsWithBodies.empty())
1561 return Error("Too few function bodies found");
Chris Lattner980e5aa2007-05-01 05:52:21 +00001562
Chandler Carruth69940402007-08-04 01:51:18 +00001563 // Look for intrinsic functions which need to be upgraded at some point
1564 for (Module::iterator FI = TheModule->begin(), FE = TheModule->end();
1565 FI != FE; ++FI) {
Evan Chengf9b83fc2007-12-17 22:33:23 +00001566 Function* NewFn;
1567 if (UpgradeIntrinsicFunction(FI, NewFn))
Chandler Carruth69940402007-08-04 01:51:18 +00001568 UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn));
1569 }
1570
Bill Wendlingde49f362010-09-10 18:51:56 +00001571 // Look for global variables which need to be renamed.
1572 for (Module::global_iterator
1573 GI = TheModule->global_begin(), GE = TheModule->global_end();
1574 GI != GE; ++GI)
1575 UpgradeGlobalVariable(GI);
1576
Chris Lattner980e5aa2007-05-01 05:52:21 +00001577 // Force deallocation of memory for these vectors to favor the client that
1578 // want lazy deserialization.
1579 std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits);
1580 std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits);
1581 std::vector<Function*>().swap(FunctionsWithBodies);
Chris Lattnerf66d20d2007-04-24 18:15:21 +00001582 return false;
Chris Lattnere84bcb92007-04-24 00:21:45 +00001583 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001584
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001585 if (Code == bitc::ENTER_SUBBLOCK) {
1586 switch (Stream.ReadSubBlockID()) {
1587 default: // Skip unknown content.
1588 if (Stream.SkipBlock())
1589 return Error("Malformed block record");
1590 break;
Chris Lattner3f799802007-05-05 18:57:30 +00001591 case bitc::BLOCKINFO_BLOCK_ID:
1592 if (Stream.ReadBlockInfoBlock())
1593 return Error("Malformed BlockInfoBlock");
1594 break;
Chris Lattner48c85b82007-05-04 03:30:17 +00001595 case bitc::PARAMATTR_BLOCK_ID:
Devang Patel05988662008-09-25 21:00:45 +00001596 if (ParseAttributeBlock())
Chris Lattner48c85b82007-05-04 03:30:17 +00001597 return true;
1598 break;
Chris Lattner1afcace2011-07-09 17:41:24 +00001599 case bitc::TYPE_BLOCK_ID_NEW:
Chris Lattner86697142007-05-01 05:01:34 +00001600 if (ParseTypeTable())
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001601 return true;
1602 break;
Chris Lattner1afcace2011-07-09 17:41:24 +00001603 case bitc::TYPE_BLOCK_ID_OLD:
1604 if (ParseOldTypeTable())
1605 return true;
1606 break;
1607 case bitc::TYPE_SYMTAB_BLOCK_ID_OLD:
1608 if (ParseOldTypeSymbolTable())
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001609 return true;
1610 break;
Chris Lattner0b2482a2007-04-23 21:26:05 +00001611 case bitc::VALUE_SYMTAB_BLOCK_ID:
Chris Lattner86697142007-05-01 05:01:34 +00001612 if (ParseValueSymbolTable())
Chris Lattner0b2482a2007-04-23 21:26:05 +00001613 return true;
1614 break;
Chris Lattnere16504e2007-04-24 03:30:34 +00001615 case bitc::CONSTANTS_BLOCK_ID:
Chris Lattner86697142007-05-01 05:01:34 +00001616 if (ParseConstants() || ResolveGlobalAndAliasInits())
Chris Lattnere16504e2007-04-24 03:30:34 +00001617 return true;
1618 break;
Devang Patele54abc92009-07-22 17:43:22 +00001619 case bitc::METADATA_BLOCK_ID:
1620 if (ParseMetadata())
1621 return true;
1622 break;
Chris Lattner48f84872007-05-01 04:59:48 +00001623 case bitc::FUNCTION_BLOCK_ID:
1624 // If this is the first function body we've seen, reverse the
1625 // FunctionsWithBodies list.
1626 if (!HasReversedFunctionsWithBodies) {
1627 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
1628 HasReversedFunctionsWithBodies = true;
1629 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001630
Chris Lattner980e5aa2007-05-01 05:52:21 +00001631 if (RememberAndSkipFunctionBody())
Chris Lattner48f84872007-05-01 04:59:48 +00001632 return true;
1633 break;
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001634 }
1635 continue;
1636 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001637
Chris Lattner36d5e7d2007-04-23 16:04:05 +00001638 if (Code == bitc::DEFINE_ABBREV) {
Chris Lattnerd127c1b2007-04-23 18:58:34 +00001639 Stream.ReadAbbrevRecord();
1640 continue;
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001641 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001642
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001643 // Read a record.
1644 switch (Stream.ReadRecord(Code, Record)) {
1645 default: break; // Default behavior, ignore unknown content.
1646 case bitc::MODULE_CODE_VERSION: // VERSION: [version#]
1647 if (Record.size() < 1)
1648 return Error("Malformed MODULE_CODE_VERSION");
1649 // Only version #0 is supported so far.
1650 if (Record[0] != 0)
1651 return Error("Unknown bitstream version!");
1652 break;
Chris Lattner15e6d172007-05-04 19:11:41 +00001653 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001654 std::string S;
1655 if (ConvertToString(Record, 0, S))
1656 return Error("Invalid MODULE_CODE_TRIPLE record");
1657 TheModule->setTargetTriple(S);
1658 break;
1659 }
Chris Lattner15e6d172007-05-04 19:11:41 +00001660 case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001661 std::string S;
1662 if (ConvertToString(Record, 0, S))
1663 return Error("Invalid MODULE_CODE_DATALAYOUT record");
1664 TheModule->setDataLayout(S);
1665 break;
1666 }
Chris Lattner15e6d172007-05-04 19:11:41 +00001667 case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001668 std::string S;
1669 if (ConvertToString(Record, 0, S))
1670 return Error("Invalid MODULE_CODE_ASM record");
1671 TheModule->setModuleInlineAsm(S);
1672 break;
1673 }
Chris Lattner15e6d172007-05-04 19:11:41 +00001674 case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001675 std::string S;
1676 if (ConvertToString(Record, 0, S))
1677 return Error("Invalid MODULE_CODE_DEPLIB record");
1678 TheModule->addLibrary(S);
1679 break;
1680 }
Chris Lattner15e6d172007-05-04 19:11:41 +00001681 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001682 std::string S;
1683 if (ConvertToString(Record, 0, S))
1684 return Error("Invalid MODULE_CODE_SECTIONNAME record");
1685 SectionTable.push_back(S);
1686 break;
1687 }
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001688 case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N]
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001689 std::string S;
1690 if (ConvertToString(Record, 0, S))
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001691 return Error("Invalid MODULE_CODE_GCNAME record");
1692 GCTable.push_back(S);
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001693 break;
1694 }
Christopher Lambfe63fb92007-12-11 08:59:05 +00001695 // GLOBALVAR: [pointer type, isconst, initid,
Rafael Espindolabea46262011-01-08 16:42:36 +00001696 // linkage, alignment, section, visibility, threadlocal,
1697 // unnamed_addr]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001698 case bitc::MODULE_CODE_GLOBALVAR: {
Chris Lattner36d5e7d2007-04-23 16:04:05 +00001699 if (Record.size() < 6)
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001700 return Error("Invalid MODULE_CODE_GLOBALVAR record");
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001701 Type *Ty = getTypeByID(Record[0]);
Duncan Sandsf22b7462010-10-28 15:47:26 +00001702 if (!Ty) return Error("Invalid MODULE_CODE_GLOBALVAR record");
Duncan Sands1df98592010-02-16 11:11:14 +00001703 if (!Ty->isPointerTy())
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001704 return Error("Global not a pointer type!");
Christopher Lambfe63fb92007-12-11 08:59:05 +00001705 unsigned AddressSpace = cast<PointerType>(Ty)->getAddressSpace();
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001706 Ty = cast<PointerType>(Ty)->getElementType();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001707
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001708 bool isConstant = Record[1];
1709 GlobalValue::LinkageTypes Linkage = GetDecodedLinkage(Record[3]);
1710 unsigned Alignment = (1 << Record[4]) >> 1;
1711 std::string Section;
1712 if (Record[5]) {
1713 if (Record[5]-1 >= SectionTable.size())
1714 return Error("Invalid section ID");
1715 Section = SectionTable[Record[5]-1];
1716 }
Chris Lattner36d5e7d2007-04-23 16:04:05 +00001717 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
Chris Lattner5f32c012007-05-06 19:27:46 +00001718 if (Record.size() > 6)
1719 Visibility = GetDecodedVisibility(Record[6]);
Chris Lattner36d5e7d2007-04-23 16:04:05 +00001720 bool isThreadLocal = false;
Chris Lattner5f32c012007-05-06 19:27:46 +00001721 if (Record.size() > 7)
1722 isThreadLocal = Record[7];
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001723
Rafael Espindolabea46262011-01-08 16:42:36 +00001724 bool UnnamedAddr = false;
1725 if (Record.size() > 8)
1726 UnnamedAddr = Record[8];
1727
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001728 GlobalVariable *NewGV =
Daniel Dunbara279bc32009-09-20 02:20:51 +00001729 new GlobalVariable(*TheModule, Ty, isConstant, Linkage, 0, "", 0,
Christopher Lambfe63fb92007-12-11 08:59:05 +00001730 isThreadLocal, AddressSpace);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001731 NewGV->setAlignment(Alignment);
1732 if (!Section.empty())
1733 NewGV->setSection(Section);
1734 NewGV->setVisibility(Visibility);
1735 NewGV->setThreadLocal(isThreadLocal);
Rafael Espindolabea46262011-01-08 16:42:36 +00001736 NewGV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001737
Chris Lattner0b2482a2007-04-23 21:26:05 +00001738 ValueList.push_back(NewGV);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001739
Chris Lattner6dbfd7b2007-04-24 00:18:21 +00001740 // Remember which value to use for the global initializer.
1741 if (unsigned InitID = Record[2])
1742 GlobalInits.push_back(std::make_pair(NewGV, InitID-1));
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001743 break;
1744 }
Chris Lattnera9bb7132007-05-08 05:38:01 +00001745 // FUNCTION: [type, callingconv, isproto, linkage, paramattr,
Rafael Espindolabea46262011-01-08 16:42:36 +00001746 // alignment, section, visibility, gc, unnamed_addr]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001747 case bitc::MODULE_CODE_FUNCTION: {
Chris Lattnera9bb7132007-05-08 05:38:01 +00001748 if (Record.size() < 8)
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001749 return Error("Invalid MODULE_CODE_FUNCTION record");
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001750 Type *Ty = getTypeByID(Record[0]);
Duncan Sandsf22b7462010-10-28 15:47:26 +00001751 if (!Ty) return Error("Invalid MODULE_CODE_FUNCTION record");
Duncan Sands1df98592010-02-16 11:11:14 +00001752 if (!Ty->isPointerTy())
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001753 return Error("Function not a pointer type!");
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001754 FunctionType *FTy =
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001755 dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType());
1756 if (!FTy)
1757 return Error("Function not a pointer to function type!");
1758
Gabor Greif051a9502008-04-06 20:25:17 +00001759 Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage,
1760 "", TheModule);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001761
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001762 Func->setCallingConv(static_cast<CallingConv::ID>(Record[1]));
Chris Lattner48f84872007-05-01 04:59:48 +00001763 bool isProto = Record[2];
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001764 Func->setLinkage(GetDecodedLinkage(Record[3]));
Devang Patel05988662008-09-25 21:00:45 +00001765 Func->setAttributes(getAttributes(Record[4]));
Daniel Dunbara279bc32009-09-20 02:20:51 +00001766
Chris Lattnera9bb7132007-05-08 05:38:01 +00001767 Func->setAlignment((1 << Record[5]) >> 1);
1768 if (Record[6]) {
1769 if (Record[6]-1 >= SectionTable.size())
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001770 return Error("Invalid section ID");
Chris Lattnera9bb7132007-05-08 05:38:01 +00001771 Func->setSection(SectionTable[Record[6]-1]);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001772 }
Chris Lattnera9bb7132007-05-08 05:38:01 +00001773 Func->setVisibility(GetDecodedVisibility(Record[7]));
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001774 if (Record.size() > 8 && Record[8]) {
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001775 if (Record[8]-1 > GCTable.size())
1776 return Error("Invalid GC ID");
1777 Func->setGC(GCTable[Record[8]-1].c_str());
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001778 }
Rafael Espindolabea46262011-01-08 16:42:36 +00001779 bool UnnamedAddr = false;
1780 if (Record.size() > 9)
1781 UnnamedAddr = Record[9];
1782 Func->setUnnamedAddr(UnnamedAddr);
Chris Lattner0b2482a2007-04-23 21:26:05 +00001783 ValueList.push_back(Func);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001784
Chris Lattner48f84872007-05-01 04:59:48 +00001785 // If this is a function with a body, remember the prototype we are
1786 // creating now, so that we can match up the body with them later.
1787 if (!isProto)
1788 FunctionsWithBodies.push_back(Func);
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001789 break;
1790 }
Anton Korobeynikov91342d82008-03-12 00:49:19 +00001791 // ALIAS: [alias type, aliasee val#, linkage]
Anton Korobeynikovf8342b92008-03-11 21:40:17 +00001792 // ALIAS: [alias type, aliasee val#, linkage, visibility]
Chris Lattner198f34a2007-04-26 03:27:58 +00001793 case bitc::MODULE_CODE_ALIAS: {
Chris Lattner07d98b42007-04-26 02:46:40 +00001794 if (Record.size() < 3)
1795 return Error("Invalid MODULE_ALIAS record");
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001796 Type *Ty = getTypeByID(Record[0]);
Duncan Sandsf22b7462010-10-28 15:47:26 +00001797 if (!Ty) return Error("Invalid MODULE_ALIAS record");
Duncan Sands1df98592010-02-16 11:11:14 +00001798 if (!Ty->isPointerTy())
Chris Lattner07d98b42007-04-26 02:46:40 +00001799 return Error("Function not a pointer type!");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001800
Chris Lattner07d98b42007-04-26 02:46:40 +00001801 GlobalAlias *NewGA = new GlobalAlias(Ty, GetDecodedLinkage(Record[2]),
1802 "", 0, TheModule);
Anton Korobeynikov91342d82008-03-12 00:49:19 +00001803 // Old bitcode files didn't have visibility field.
1804 if (Record.size() > 3)
1805 NewGA->setVisibility(GetDecodedVisibility(Record[3]));
Chris Lattner07d98b42007-04-26 02:46:40 +00001806 ValueList.push_back(NewGA);
1807 AliasInits.push_back(std::make_pair(NewGA, Record[1]));
1808 break;
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001809 }
Chris Lattner198f34a2007-04-26 03:27:58 +00001810 /// MODULE_CODE_PURGEVALS: [numvals]
1811 case bitc::MODULE_CODE_PURGEVALS:
1812 // Trim down the value list to the specified size.
1813 if (Record.size() < 1 || Record[0] > ValueList.size())
1814 return Error("Invalid MODULE_PURGEVALS record");
1815 ValueList.shrinkTo(Record[0]);
1816 break;
1817 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001818 Record.clear();
1819 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001820
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001821 return Error("Premature end of bitstream");
1822}
1823
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00001824bool BitcodeReader::ParseBitcodeInto(Module *M) {
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001825 TheModule = 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001826
Chris Lattnerc453f762007-04-29 07:54:31 +00001827 unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart();
Chris Lattner6fa6a322008-07-09 05:14:23 +00001828 unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001829
Dan Gohmana4ae3a12010-04-02 00:03:51 +00001830 if (Buffer->getBufferSize() & 3) {
1831 if (!isRawBitcode(BufPtr, BufEnd) && !isBitcodeWrapper(BufPtr, BufEnd))
1832 return Error("Invalid bitcode signature");
1833 else
1834 return Error("Bitcode stream should be a multiple of 4 bytes in length");
1835 }
1836
Chris Lattner6fa6a322008-07-09 05:14:23 +00001837 // If we have a wrapper header, parse it and ignore the non-bc file contents.
1838 // The magic number is 0x0B17C0DE stored in little endian.
Chris Lattnere2a466b2009-04-06 20:54:32 +00001839 if (isBitcodeWrapper(BufPtr, BufEnd))
1840 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd))
Chris Lattner6fa6a322008-07-09 05:14:23 +00001841 return Error("Invalid bitcode wrapper header");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001842
Chris Lattner962dde32009-04-26 20:59:02 +00001843 StreamFile.init(BufPtr, BufEnd);
1844 Stream.init(StreamFile);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001845
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001846 // Sniff for the signature.
1847 if (Stream.Read(8) != 'B' ||
1848 Stream.Read(8) != 'C' ||
1849 Stream.Read(4) != 0x0 ||
1850 Stream.Read(4) != 0xC ||
1851 Stream.Read(4) != 0xE ||
1852 Stream.Read(4) != 0xD)
1853 return Error("Invalid bitcode signature");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001854
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001855 // We expect a number of well-defined blocks, though we don't necessarily
1856 // need to understand them all.
1857 while (!Stream.AtEndOfStream()) {
1858 unsigned Code = Stream.ReadCode();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001859
Rafael Espindolac9687b32011-05-26 18:59:54 +00001860 if (Code != bitc::ENTER_SUBBLOCK) {
1861
Chad Rosier6ff9aa22011-08-09 22:23:40 +00001862 // The ranlib in xcode 4 will align archive members by appending newlines
1863 // to the end of them. If this file size is a multiple of 4 but not 8, we
1864 // have to read and ignore these final 4 bytes :-(
Rafael Espindolac9687b32011-05-26 18:59:54 +00001865 if (Stream.GetAbbrevIDWidth() == 2 && Code == 2 &&
1866 Stream.Read(6) == 2 && Stream.Read(24) == 0xa0a0a &&
1867 Stream.AtEndOfStream())
1868 return false;
1869
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001870 return Error("Invalid record at top-level");
Rafael Espindolac9687b32011-05-26 18:59:54 +00001871 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001872
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001873 unsigned BlockID = Stream.ReadSubBlockID();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001874
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001875 // We only know the MODULE subblock ID.
Chris Lattnere17b6582007-05-05 00:17:00 +00001876 switch (BlockID) {
1877 case bitc::BLOCKINFO_BLOCK_ID:
1878 if (Stream.ReadBlockInfoBlock())
1879 return Error("Malformed BlockInfoBlock");
1880 break;
1881 case bitc::MODULE_BLOCK_ID:
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00001882 // Reject multiple MODULE_BLOCK's in a single bitstream.
1883 if (TheModule)
1884 return Error("Multiple MODULE_BLOCKs in same stream");
1885 TheModule = M;
1886 if (ParseModule())
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001887 return true;
Chris Lattnere17b6582007-05-05 00:17:00 +00001888 break;
1889 default:
1890 if (Stream.SkipBlock())
1891 return Error("Malformed block record");
1892 break;
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001893 }
1894 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001895
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001896 return false;
1897}
Chris Lattnerc453f762007-04-29 07:54:31 +00001898
Bill Wendling34711742010-10-06 01:22:42 +00001899bool BitcodeReader::ParseModuleTriple(std::string &Triple) {
1900 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
1901 return Error("Malformed block record");
1902
1903 SmallVector<uint64_t, 64> Record;
1904
1905 // Read all the records for this module.
1906 while (!Stream.AtEndOfStream()) {
1907 unsigned Code = Stream.ReadCode();
1908 if (Code == bitc::END_BLOCK) {
1909 if (Stream.ReadBlockEnd())
1910 return Error("Error at end of module block");
1911
1912 return false;
1913 }
1914
1915 if (Code == bitc::ENTER_SUBBLOCK) {
1916 switch (Stream.ReadSubBlockID()) {
1917 default: // Skip unknown content.
1918 if (Stream.SkipBlock())
1919 return Error("Malformed block record");
1920 break;
1921 }
1922 continue;
1923 }
1924
1925 if (Code == bitc::DEFINE_ABBREV) {
1926 Stream.ReadAbbrevRecord();
1927 continue;
1928 }
1929
1930 // Read a record.
1931 switch (Stream.ReadRecord(Code, Record)) {
1932 default: break; // Default behavior, ignore unknown content.
1933 case bitc::MODULE_CODE_VERSION: // VERSION: [version#]
1934 if (Record.size() < 1)
1935 return Error("Malformed MODULE_CODE_VERSION");
1936 // Only version #0 is supported so far.
1937 if (Record[0] != 0)
1938 return Error("Unknown bitstream version!");
1939 break;
1940 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
1941 std::string S;
1942 if (ConvertToString(Record, 0, S))
1943 return Error("Invalid MODULE_CODE_TRIPLE record");
1944 Triple = S;
1945 break;
1946 }
1947 }
1948 Record.clear();
1949 }
1950
1951 return Error("Premature end of bitstream");
1952}
1953
1954bool BitcodeReader::ParseTriple(std::string &Triple) {
1955 if (Buffer->getBufferSize() & 3)
1956 return Error("Bitcode stream should be a multiple of 4 bytes in length");
1957
1958 unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart();
1959 unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
1960
1961 // If we have a wrapper header, parse it and ignore the non-bc file contents.
1962 // The magic number is 0x0B17C0DE stored in little endian.
1963 if (isBitcodeWrapper(BufPtr, BufEnd))
1964 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd))
1965 return Error("Invalid bitcode wrapper header");
1966
1967 StreamFile.init(BufPtr, BufEnd);
1968 Stream.init(StreamFile);
1969
1970 // Sniff for the signature.
1971 if (Stream.Read(8) != 'B' ||
1972 Stream.Read(8) != 'C' ||
1973 Stream.Read(4) != 0x0 ||
1974 Stream.Read(4) != 0xC ||
1975 Stream.Read(4) != 0xE ||
1976 Stream.Read(4) != 0xD)
1977 return Error("Invalid bitcode signature");
1978
1979 // We expect a number of well-defined blocks, though we don't necessarily
1980 // need to understand them all.
1981 while (!Stream.AtEndOfStream()) {
1982 unsigned Code = Stream.ReadCode();
1983
1984 if (Code != bitc::ENTER_SUBBLOCK)
1985 return Error("Invalid record at top-level");
1986
1987 unsigned BlockID = Stream.ReadSubBlockID();
1988
1989 // We only know the MODULE subblock ID.
1990 switch (BlockID) {
1991 case bitc::MODULE_BLOCK_ID:
1992 if (ParseModuleTriple(Triple))
1993 return true;
1994 break;
1995 default:
1996 if (Stream.SkipBlock())
1997 return Error("Malformed block record");
1998 break;
1999 }
2000 }
2001
2002 return false;
2003}
2004
Devang Patele8e02132009-09-18 19:26:43 +00002005/// ParseMetadataAttachment - Parse metadata attachments.
2006bool BitcodeReader::ParseMetadataAttachment() {
2007 if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
2008 return Error("Malformed block record");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002009
Devang Patele8e02132009-09-18 19:26:43 +00002010 SmallVector<uint64_t, 64> Record;
2011 while(1) {
2012 unsigned Code = Stream.ReadCode();
2013 if (Code == bitc::END_BLOCK) {
2014 if (Stream.ReadBlockEnd())
Daniel Dunbara279bc32009-09-20 02:20:51 +00002015 return Error("Error at end of PARAMATTR block");
Devang Patele8e02132009-09-18 19:26:43 +00002016 break;
2017 }
2018 if (Code == bitc::DEFINE_ABBREV) {
2019 Stream.ReadAbbrevRecord();
2020 continue;
2021 }
2022 // Read a metadata attachment record.
2023 Record.clear();
2024 switch (Stream.ReadRecord(Code, Record)) {
2025 default: // Default behavior: ignore.
2026 break;
Chris Lattner9d61dd92011-06-17 17:50:30 +00002027 case bitc::METADATA_ATTACHMENT: {
Devang Patele8e02132009-09-18 19:26:43 +00002028 unsigned RecordLength = Record.size();
2029 if (Record.empty() || (RecordLength - 1) % 2 == 1)
Daniel Dunbara279bc32009-09-20 02:20:51 +00002030 return Error ("Invalid METADATA_ATTACHMENT reader!");
Devang Patele8e02132009-09-18 19:26:43 +00002031 Instruction *Inst = InstructionList[Record[0]];
2032 for (unsigned i = 1; i != RecordLength; i = i+2) {
Devang Patela2148402009-09-28 21:14:55 +00002033 unsigned Kind = Record[i];
Dan Gohman19538d12010-07-20 21:42:28 +00002034 DenseMap<unsigned, unsigned>::iterator I =
2035 MDKindMap.find(Kind);
2036 if (I == MDKindMap.end())
2037 return Error("Invalid metadata kind ID");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002038 Value *Node = MDValueList.getValueFwdRef(Record[i+1]);
Dan Gohman19538d12010-07-20 21:42:28 +00002039 Inst->setMetadata(I->second, cast<MDNode>(Node));
Devang Patele8e02132009-09-18 19:26:43 +00002040 }
2041 break;
2042 }
2043 }
2044 }
2045 return false;
2046}
Chris Lattner48f84872007-05-01 04:59:48 +00002047
Chris Lattner980e5aa2007-05-01 05:52:21 +00002048/// ParseFunctionBody - Lazily parse the specified function body block.
2049bool BitcodeReader::ParseFunctionBody(Function *F) {
Chris Lattnere17b6582007-05-05 00:17:00 +00002050 if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
Chris Lattner980e5aa2007-05-01 05:52:21 +00002051 return Error("Malformed block record");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002052
Nick Lewycky9a49f152010-02-25 08:30:17 +00002053 InstructionList.clear();
Chris Lattner980e5aa2007-05-01 05:52:21 +00002054 unsigned ModuleValueListSize = ValueList.size();
Dan Gohman69813832010-08-25 20:22:53 +00002055 unsigned ModuleMDValueListSize = MDValueList.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +00002056
Chris Lattner980e5aa2007-05-01 05:52:21 +00002057 // Add all the function arguments to the value table.
2058 for(Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
2059 ValueList.push_back(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002060
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002061 unsigned NextValueNo = ValueList.size();
Chris Lattner231cbcb2007-05-02 04:27:25 +00002062 BasicBlock *CurBB = 0;
2063 unsigned CurBBNo = 0;
2064
Chris Lattnera6245242010-04-03 02:17:50 +00002065 DebugLoc LastLoc;
2066
Chris Lattner980e5aa2007-05-01 05:52:21 +00002067 // Read all the records.
2068 SmallVector<uint64_t, 64> Record;
2069 while (1) {
2070 unsigned Code = Stream.ReadCode();
2071 if (Code == bitc::END_BLOCK) {
2072 if (Stream.ReadBlockEnd())
2073 return Error("Error at end of function block");
2074 break;
2075 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002076
Chris Lattner980e5aa2007-05-01 05:52:21 +00002077 if (Code == bitc::ENTER_SUBBLOCK) {
2078 switch (Stream.ReadSubBlockID()) {
2079 default: // Skip unknown content.
2080 if (Stream.SkipBlock())
2081 return Error("Malformed block record");
2082 break;
2083 case bitc::CONSTANTS_BLOCK_ID:
2084 if (ParseConstants()) return true;
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002085 NextValueNo = ValueList.size();
Chris Lattner980e5aa2007-05-01 05:52:21 +00002086 break;
2087 case bitc::VALUE_SYMTAB_BLOCK_ID:
2088 if (ParseValueSymbolTable()) return true;
2089 break;
Devang Patele8e02132009-09-18 19:26:43 +00002090 case bitc::METADATA_ATTACHMENT_ID:
Daniel Dunbara279bc32009-09-20 02:20:51 +00002091 if (ParseMetadataAttachment()) return true;
2092 break;
Victor Hernandezfab9e99c2010-01-13 19:34:08 +00002093 case bitc::METADATA_BLOCK_ID:
2094 if (ParseMetadata()) return true;
2095 break;
Chris Lattner980e5aa2007-05-01 05:52:21 +00002096 }
2097 continue;
2098 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002099
Chris Lattner980e5aa2007-05-01 05:52:21 +00002100 if (Code == bitc::DEFINE_ABBREV) {
2101 Stream.ReadAbbrevRecord();
2102 continue;
2103 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002104
Chris Lattner980e5aa2007-05-01 05:52:21 +00002105 // Read a record.
2106 Record.clear();
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002107 Instruction *I = 0;
Dan Gohman1224c382009-07-20 21:19:07 +00002108 unsigned BitCode = Stream.ReadRecord(Code, Record);
2109 switch (BitCode) {
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002110 default: // Default behavior: reject
2111 return Error("Unknown instruction");
Chris Lattner980e5aa2007-05-01 05:52:21 +00002112 case bitc::FUNC_CODE_DECLAREBLOCKS: // DECLAREBLOCKS: [nblocks]
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002113 if (Record.size() < 1 || Record[0] == 0)
2114 return Error("Invalid DECLAREBLOCKS record");
Chris Lattner980e5aa2007-05-01 05:52:21 +00002115 // Create all the basic blocks for the function.
Chris Lattnerf61e6452007-05-03 22:09:51 +00002116 FunctionBBs.resize(Record[0]);
Chris Lattner980e5aa2007-05-01 05:52:21 +00002117 for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i)
Owen Anderson1d0be152009-08-13 21:58:54 +00002118 FunctionBBs[i] = BasicBlock::Create(Context, "", F);
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002119 CurBB = FunctionBBs[0];
2120 continue;
Chris Lattnera6245242010-04-03 02:17:50 +00002121
2122 case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN
2123 // This record indicates that the last instruction is at the same
2124 // location as the previous instruction with a location.
2125 I = 0;
2126
2127 // Get the last instruction emitted.
2128 if (CurBB && !CurBB->empty())
2129 I = &CurBB->back();
2130 else if (CurBBNo && FunctionBBs[CurBBNo-1] &&
2131 !FunctionBBs[CurBBNo-1]->empty())
2132 I = &FunctionBBs[CurBBNo-1]->back();
2133
2134 if (I == 0) return Error("Invalid DEBUG_LOC_AGAIN record");
2135 I->setDebugLoc(LastLoc);
2136 I = 0;
2137 continue;
2138
Chris Lattner4f6bab92011-06-17 18:17:37 +00002139 case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia]
Chris Lattnera6245242010-04-03 02:17:50 +00002140 I = 0; // Get the last instruction emitted.
2141 if (CurBB && !CurBB->empty())
2142 I = &CurBB->back();
2143 else if (CurBBNo && FunctionBBs[CurBBNo-1] &&
2144 !FunctionBBs[CurBBNo-1]->empty())
2145 I = &FunctionBBs[CurBBNo-1]->back();
2146 if (I == 0 || Record.size() < 4)
2147 return Error("Invalid FUNC_CODE_DEBUG_LOC record");
2148
2149 unsigned Line = Record[0], Col = Record[1];
2150 unsigned ScopeID = Record[2], IAID = Record[3];
2151
2152 MDNode *Scope = 0, *IA = 0;
2153 if (ScopeID) Scope = cast<MDNode>(MDValueList.getValueFwdRef(ScopeID-1));
2154 if (IAID) IA = cast<MDNode>(MDValueList.getValueFwdRef(IAID-1));
2155 LastLoc = DebugLoc::get(Line, Col, Scope, IA);
2156 I->setDebugLoc(LastLoc);
2157 I = 0;
2158 continue;
2159 }
2160
Chris Lattnerabfbf852007-05-06 00:21:25 +00002161 case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode]
2162 unsigned OpNum = 0;
2163 Value *LHS, *RHS;
2164 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
2165 getValue(Record, OpNum, LHS->getType(), RHS) ||
Dan Gohman1224c382009-07-20 21:19:07 +00002166 OpNum+1 > Record.size())
Chris Lattnerabfbf852007-05-06 00:21:25 +00002167 return Error("Invalid BINOP record");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002168
Dan Gohman1224c382009-07-20 21:19:07 +00002169 int Opc = GetDecodedBinaryOpcode(Record[OpNum++], LHS->getType());
Chris Lattnerabfbf852007-05-06 00:21:25 +00002170 if (Opc == -1) return Error("Invalid BINOP record");
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002171 I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
Devang Patele8e02132009-09-18 19:26:43 +00002172 InstructionList.push_back(I);
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002173 if (OpNum < Record.size()) {
2174 if (Opc == Instruction::Add ||
2175 Opc == Instruction::Sub ||
Chris Lattnerf067d582011-02-07 16:40:21 +00002176 Opc == Instruction::Mul ||
2177 Opc == Instruction::Shl) {
Dan Gohman26793ed2010-01-25 21:55:39 +00002178 if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP))
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002179 cast<BinaryOperator>(I)->setHasNoSignedWrap(true);
Dan Gohman26793ed2010-01-25 21:55:39 +00002180 if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002181 cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true);
Chris Lattner35bda892011-02-06 21:44:57 +00002182 } else if (Opc == Instruction::SDiv ||
Chris Lattnerf067d582011-02-07 16:40:21 +00002183 Opc == Instruction::UDiv ||
2184 Opc == Instruction::LShr ||
2185 Opc == Instruction::AShr) {
Chris Lattner35bda892011-02-06 21:44:57 +00002186 if (Record[OpNum] & (1 << bitc::PEO_EXACT))
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002187 cast<BinaryOperator>(I)->setIsExact(true);
2188 }
2189 }
Chris Lattner980e5aa2007-05-01 05:52:21 +00002190 break;
2191 }
Chris Lattnerabfbf852007-05-06 00:21:25 +00002192 case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc]
2193 unsigned OpNum = 0;
2194 Value *Op;
2195 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
2196 OpNum+2 != Record.size())
2197 return Error("Invalid CAST record");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002198
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002199 Type *ResTy = getTypeByID(Record[OpNum]);
Chris Lattnerabfbf852007-05-06 00:21:25 +00002200 int Opc = GetDecodedCastOpcode(Record[OpNum+1]);
2201 if (Opc == -1 || ResTy == 0)
Chris Lattner231cbcb2007-05-02 04:27:25 +00002202 return Error("Invalid CAST record");
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002203 I = CastInst::Create((Instruction::CastOps)Opc, Op, ResTy);
Devang Patele8e02132009-09-18 19:26:43 +00002204 InstructionList.push_back(I);
Chris Lattner231cbcb2007-05-02 04:27:25 +00002205 break;
2206 }
Dan Gohmandd8004d2009-07-27 21:53:46 +00002207 case bitc::FUNC_CODE_INST_INBOUNDS_GEP:
Chris Lattner15e6d172007-05-04 19:11:41 +00002208 case bitc::FUNC_CODE_INST_GEP: { // GEP: [n x operands]
Chris Lattner7337ab92007-05-06 00:00:00 +00002209 unsigned OpNum = 0;
2210 Value *BasePtr;
2211 if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr))
Chris Lattner01ff65f2007-05-02 05:16:49 +00002212 return Error("Invalid GEP record");
2213
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002214 SmallVector<Value*, 16> GEPIdx;
Chris Lattner7337ab92007-05-06 00:00:00 +00002215 while (OpNum != Record.size()) {
2216 Value *Op;
2217 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
Chris Lattner01ff65f2007-05-02 05:16:49 +00002218 return Error("Invalid GEP record");
Chris Lattner7337ab92007-05-06 00:00:00 +00002219 GEPIdx.push_back(Op);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002220 }
2221
Jay Foada9203102011-07-25 09:48:08 +00002222 I = GetElementPtrInst::Create(BasePtr, GEPIdx);
Devang Patele8e02132009-09-18 19:26:43 +00002223 InstructionList.push_back(I);
Dan Gohmandd8004d2009-07-27 21:53:46 +00002224 if (BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP)
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002225 cast<GetElementPtrInst>(I)->setIsInBounds(true);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002226 break;
2227 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002228
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002229 case bitc::FUNC_CODE_INST_EXTRACTVAL: {
2230 // EXTRACTVAL: [opty, opval, n x indices]
Dan Gohmane4977cf2008-05-23 01:55:30 +00002231 unsigned OpNum = 0;
2232 Value *Agg;
2233 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
2234 return Error("Invalid EXTRACTVAL record");
2235
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002236 SmallVector<unsigned, 4> EXTRACTVALIdx;
2237 for (unsigned RecSize = Record.size();
2238 OpNum != RecSize; ++OpNum) {
2239 uint64_t Index = Record[OpNum];
2240 if ((unsigned)Index != Index)
2241 return Error("Invalid EXTRACTVAL index");
2242 EXTRACTVALIdx.push_back((unsigned)Index);
Dan Gohmane4977cf2008-05-23 01:55:30 +00002243 }
2244
Jay Foadfc6d3a42011-07-13 10:26:04 +00002245 I = ExtractValueInst::Create(Agg, EXTRACTVALIdx);
Devang Patele8e02132009-09-18 19:26:43 +00002246 InstructionList.push_back(I);
Dan Gohmane4977cf2008-05-23 01:55:30 +00002247 break;
2248 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002249
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002250 case bitc::FUNC_CODE_INST_INSERTVAL: {
2251 // INSERTVAL: [opty, opval, opty, opval, n x indices]
Dan Gohmane4977cf2008-05-23 01:55:30 +00002252 unsigned OpNum = 0;
2253 Value *Agg;
2254 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
2255 return Error("Invalid INSERTVAL record");
2256 Value *Val;
2257 if (getValueTypePair(Record, OpNum, NextValueNo, Val))
2258 return Error("Invalid INSERTVAL record");
2259
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002260 SmallVector<unsigned, 4> INSERTVALIdx;
2261 for (unsigned RecSize = Record.size();
2262 OpNum != RecSize; ++OpNum) {
2263 uint64_t Index = Record[OpNum];
2264 if ((unsigned)Index != Index)
2265 return Error("Invalid INSERTVAL index");
2266 INSERTVALIdx.push_back((unsigned)Index);
Dan Gohmane4977cf2008-05-23 01:55:30 +00002267 }
2268
Jay Foadfc6d3a42011-07-13 10:26:04 +00002269 I = InsertValueInst::Create(Agg, Val, INSERTVALIdx);
Devang Patele8e02132009-09-18 19:26:43 +00002270 InstructionList.push_back(I);
Dan Gohmane4977cf2008-05-23 01:55:30 +00002271 break;
2272 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002273
Chris Lattnerabfbf852007-05-06 00:21:25 +00002274 case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval]
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +00002275 // obsolete form of select
2276 // handles select i1 ... in old bitcode
Chris Lattnerabfbf852007-05-06 00:21:25 +00002277 unsigned OpNum = 0;
2278 Value *TrueVal, *FalseVal, *Cond;
2279 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
2280 getValue(Record, OpNum, TrueVal->getType(), FalseVal) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00002281 getValue(Record, OpNum, Type::getInt1Ty(Context), Cond))
Chris Lattner01ff65f2007-05-02 05:16:49 +00002282 return Error("Invalid SELECT record");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002283
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +00002284 I = SelectInst::Create(Cond, TrueVal, FalseVal);
Devang Patele8e02132009-09-18 19:26:43 +00002285 InstructionList.push_back(I);
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +00002286 break;
2287 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002288
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +00002289 case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred]
2290 // new form of select
2291 // handles select i1 or select [N x i1]
2292 unsigned OpNum = 0;
2293 Value *TrueVal, *FalseVal, *Cond;
2294 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
2295 getValue(Record, OpNum, TrueVal->getType(), FalseVal) ||
2296 getValueTypePair(Record, OpNum, NextValueNo, Cond))
2297 return Error("Invalid SELECT record");
Dan Gohmanf72fb672008-09-09 01:02:47 +00002298
2299 // select condition can be either i1 or [N x i1]
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002300 if (VectorType* vector_type =
2301 dyn_cast<VectorType>(Cond->getType())) {
Dan Gohmanf72fb672008-09-09 01:02:47 +00002302 // expect <n x i1>
Daniel Dunbara279bc32009-09-20 02:20:51 +00002303 if (vector_type->getElementType() != Type::getInt1Ty(Context))
Dan Gohmanf72fb672008-09-09 01:02:47 +00002304 return Error("Invalid SELECT condition type");
2305 } else {
2306 // expect i1
Daniel Dunbara279bc32009-09-20 02:20:51 +00002307 if (Cond->getType() != Type::getInt1Ty(Context))
Dan Gohmanf72fb672008-09-09 01:02:47 +00002308 return Error("Invalid SELECT condition type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002309 }
2310
Gabor Greif051a9502008-04-06 20:25:17 +00002311 I = SelectInst::Create(Cond, TrueVal, FalseVal);
Devang Patele8e02132009-09-18 19:26:43 +00002312 InstructionList.push_back(I);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002313 break;
2314 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002315
Chris Lattner01ff65f2007-05-02 05:16:49 +00002316 case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval]
Chris Lattnerabfbf852007-05-06 00:21:25 +00002317 unsigned OpNum = 0;
2318 Value *Vec, *Idx;
2319 if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00002320 getValue(Record, OpNum, Type::getInt32Ty(Context), Idx))
Chris Lattner01ff65f2007-05-02 05:16:49 +00002321 return Error("Invalid EXTRACTELT record");
Eric Christophera3500da2009-07-25 02:28:41 +00002322 I = ExtractElementInst::Create(Vec, Idx);
Devang Patele8e02132009-09-18 19:26:43 +00002323 InstructionList.push_back(I);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002324 break;
2325 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002326
Chris Lattner01ff65f2007-05-02 05:16:49 +00002327 case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval]
Chris Lattnerabfbf852007-05-06 00:21:25 +00002328 unsigned OpNum = 0;
2329 Value *Vec, *Elt, *Idx;
2330 if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
Daniel Dunbara279bc32009-09-20 02:20:51 +00002331 getValue(Record, OpNum,
Chris Lattnerabfbf852007-05-06 00:21:25 +00002332 cast<VectorType>(Vec->getType())->getElementType(), Elt) ||
Owen Anderson1d0be152009-08-13 21:58:54 +00002333 getValue(Record, OpNum, Type::getInt32Ty(Context), Idx))
Chris Lattner01ff65f2007-05-02 05:16:49 +00002334 return Error("Invalid INSERTELT record");
Gabor Greif051a9502008-04-06 20:25:17 +00002335 I = InsertElementInst::Create(Vec, Elt, Idx);
Devang Patele8e02132009-09-18 19:26:43 +00002336 InstructionList.push_back(I);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002337 break;
2338 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002339
Chris Lattnerabfbf852007-05-06 00:21:25 +00002340 case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval]
2341 unsigned OpNum = 0;
2342 Value *Vec1, *Vec2, *Mask;
2343 if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) ||
2344 getValue(Record, OpNum, Vec1->getType(), Vec2))
2345 return Error("Invalid SHUFFLEVEC record");
2346
Mon P Wangaeb06d22008-11-10 04:46:22 +00002347 if (getValueTypePair(Record, OpNum, NextValueNo, Mask))
Chris Lattner01ff65f2007-05-02 05:16:49 +00002348 return Error("Invalid SHUFFLEVEC record");
2349 I = new ShuffleVectorInst(Vec1, Vec2, Mask);
Devang Patele8e02132009-09-18 19:26:43 +00002350 InstructionList.push_back(I);
Chris Lattner01ff65f2007-05-02 05:16:49 +00002351 break;
2352 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002353
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002354 case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred]
2355 // Old form of ICmp/FCmp returning bool
2356 // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were
2357 // both legal on vectors but had different behaviour.
2358 case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred]
2359 // FCmp/ICmp returning bool or vector of bool
2360
Chris Lattner7337ab92007-05-06 00:00:00 +00002361 unsigned OpNum = 0;
2362 Value *LHS, *RHS;
2363 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
2364 getValue(Record, OpNum, LHS->getType(), RHS) ||
2365 OpNum+1 != Record.size())
Chris Lattner01ff65f2007-05-02 05:16:49 +00002366 return Error("Invalid CMP record");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002367
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002368 if (LHS->getType()->isFPOrFPVectorTy())
Dan Gohman1c8a23c2009-08-25 23:17:54 +00002369 I = new FCmpInst((FCmpInst::Predicate)Record[OpNum], LHS, RHS);
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002370 else
Dan Gohman1c8a23c2009-08-25 23:17:54 +00002371 I = new ICmpInst((ICmpInst::Predicate)Record[OpNum], LHS, RHS);
Devang Patele8e02132009-09-18 19:26:43 +00002372 InstructionList.push_back(I);
Dan Gohmanf72fb672008-09-09 01:02:47 +00002373 break;
2374 }
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002375
Chris Lattner231cbcb2007-05-02 04:27:25 +00002376 case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>]
Devang Pateld9d99ff2008-02-26 01:29:32 +00002377 {
2378 unsigned Size = Record.size();
2379 if (Size == 0) {
Owen Anderson1d0be152009-08-13 21:58:54 +00002380 I = ReturnInst::Create(Context);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002381 InstructionList.push_back(I);
Devang Pateld9d99ff2008-02-26 01:29:32 +00002382 break;
Dan Gohmanfc74abf2008-07-23 00:34:11 +00002383 }
Devang Pateld9d99ff2008-02-26 01:29:32 +00002384
Dan Gohmanfc74abf2008-07-23 00:34:11 +00002385 unsigned OpNum = 0;
Chris Lattner96a74c52011-06-17 18:09:11 +00002386 Value *Op = NULL;
2387 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
2388 return Error("Invalid RET record");
2389 if (OpNum != Record.size())
2390 return Error("Invalid RET record");
Dan Gohmanfc74abf2008-07-23 00:34:11 +00002391
Chris Lattner96a74c52011-06-17 18:09:11 +00002392 I = ReturnInst::Create(Context, Op);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002393 InstructionList.push_back(I);
Dan Gohmanfc74abf2008-07-23 00:34:11 +00002394 break;
Chris Lattner231cbcb2007-05-02 04:27:25 +00002395 }
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002396 case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#]
Chris Lattnerf61e6452007-05-03 22:09:51 +00002397 if (Record.size() != 1 && Record.size() != 3)
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002398 return Error("Invalid BR record");
2399 BasicBlock *TrueDest = getBasicBlock(Record[0]);
2400 if (TrueDest == 0)
2401 return Error("Invalid BR record");
2402
Devang Patele8e02132009-09-18 19:26:43 +00002403 if (Record.size() == 1) {
Gabor Greif051a9502008-04-06 20:25:17 +00002404 I = BranchInst::Create(TrueDest);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002405 InstructionList.push_back(I);
Devang Patele8e02132009-09-18 19:26:43 +00002406 }
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002407 else {
2408 BasicBlock *FalseDest = getBasicBlock(Record[1]);
Owen Anderson1d0be152009-08-13 21:58:54 +00002409 Value *Cond = getFnValueByID(Record[2], Type::getInt1Ty(Context));
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002410 if (FalseDest == 0 || Cond == 0)
2411 return Error("Invalid BR record");
Gabor Greif051a9502008-04-06 20:25:17 +00002412 I = BranchInst::Create(TrueDest, FalseDest, Cond);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002413 InstructionList.push_back(I);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002414 }
2415 break;
2416 }
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002417 case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...]
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002418 if (Record.size() < 3 || (Record.size() & 1) == 0)
2419 return Error("Invalid SWITCH record");
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002420 Type *OpTy = getTypeByID(Record[0]);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002421 Value *Cond = getFnValueByID(Record[1], OpTy);
2422 BasicBlock *Default = getBasicBlock(Record[2]);
2423 if (OpTy == 0 || Cond == 0 || Default == 0)
2424 return Error("Invalid SWITCH record");
2425 unsigned NumCases = (Record.size()-3)/2;
Gabor Greif051a9502008-04-06 20:25:17 +00002426 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
Devang Patele8e02132009-09-18 19:26:43 +00002427 InstructionList.push_back(SI);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002428 for (unsigned i = 0, e = NumCases; i != e; ++i) {
Daniel Dunbara279bc32009-09-20 02:20:51 +00002429 ConstantInt *CaseVal =
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002430 dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy));
2431 BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]);
2432 if (CaseVal == 0 || DestBB == 0) {
2433 delete SI;
2434 return Error("Invalid SWITCH record!");
2435 }
2436 SI->addCase(CaseVal, DestBB);
2437 }
2438 I = SI;
2439 break;
2440 }
Chris Lattnerab21db72009-10-28 00:19:10 +00002441 case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...]
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002442 if (Record.size() < 2)
Chris Lattnerab21db72009-10-28 00:19:10 +00002443 return Error("Invalid INDIRECTBR record");
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002444 Type *OpTy = getTypeByID(Record[0]);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002445 Value *Address = getFnValueByID(Record[1], OpTy);
2446 if (OpTy == 0 || Address == 0)
Chris Lattnerab21db72009-10-28 00:19:10 +00002447 return Error("Invalid INDIRECTBR record");
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002448 unsigned NumDests = Record.size()-2;
Chris Lattnerab21db72009-10-28 00:19:10 +00002449 IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002450 InstructionList.push_back(IBI);
2451 for (unsigned i = 0, e = NumDests; i != e; ++i) {
2452 if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) {
2453 IBI->addDestination(DestBB);
2454 } else {
2455 delete IBI;
Chris Lattnerab21db72009-10-28 00:19:10 +00002456 return Error("Invalid INDIRECTBR record!");
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002457 }
2458 }
2459 I = IBI;
2460 break;
2461 }
2462
Duncan Sandsdc024672007-11-27 13:23:08 +00002463 case bitc::FUNC_CODE_INST_INVOKE: {
2464 // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
Chris Lattnera9bb7132007-05-08 05:38:01 +00002465 if (Record.size() < 4) return Error("Invalid INVOKE record");
Devang Patel05988662008-09-25 21:00:45 +00002466 AttrListPtr PAL = getAttributes(Record[0]);
Chris Lattnera9bb7132007-05-08 05:38:01 +00002467 unsigned CCInfo = Record[1];
2468 BasicBlock *NormalBB = getBasicBlock(Record[2]);
2469 BasicBlock *UnwindBB = getBasicBlock(Record[3]);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002470
Chris Lattnera9bb7132007-05-08 05:38:01 +00002471 unsigned OpNum = 4;
Chris Lattner7337ab92007-05-06 00:00:00 +00002472 Value *Callee;
2473 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002474 return Error("Invalid INVOKE record");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002475
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002476 PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType());
2477 FunctionType *FTy = !CalleeTy ? 0 :
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002478 dyn_cast<FunctionType>(CalleeTy->getElementType());
2479
2480 // Check that the right number of fixed parameters are here.
Chris Lattner7337ab92007-05-06 00:00:00 +00002481 if (FTy == 0 || NormalBB == 0 || UnwindBB == 0 ||
2482 Record.size() < OpNum+FTy->getNumParams())
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002483 return Error("Invalid INVOKE record");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002484
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002485 SmallVector<Value*, 16> Ops;
Chris Lattner7337ab92007-05-06 00:00:00 +00002486 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
2487 Ops.push_back(getFnValueByID(Record[OpNum], FTy->getParamType(i)));
2488 if (Ops.back() == 0) return Error("Invalid INVOKE record");
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002489 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002490
Chris Lattner7337ab92007-05-06 00:00:00 +00002491 if (!FTy->isVarArg()) {
2492 if (Record.size() != OpNum)
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002493 return Error("Invalid INVOKE record");
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002494 } else {
Chris Lattner7337ab92007-05-06 00:00:00 +00002495 // Read type/value pairs for varargs params.
2496 while (OpNum != Record.size()) {
2497 Value *Op;
2498 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
2499 return Error("Invalid INVOKE record");
2500 Ops.push_back(Op);
2501 }
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002502 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002503
Jay Foada3efbb12011-07-15 08:37:34 +00002504 I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops);
Devang Patele8e02132009-09-18 19:26:43 +00002505 InstructionList.push_back(I);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002506 cast<InvokeInst>(I)->setCallingConv(
2507 static_cast<CallingConv::ID>(CCInfo));
Devang Patel05988662008-09-25 21:00:45 +00002508 cast<InvokeInst>(I)->setAttributes(PAL);
Chris Lattnerf4c8e522007-05-02 05:46:45 +00002509 break;
2510 }
Bill Wendlingdccc03b2011-07-31 06:30:59 +00002511 case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval]
2512 unsigned Idx = 0;
2513 Value *Val = 0;
2514 if (getValueTypePair(Record, Idx, NextValueNo, Val))
2515 return Error("Invalid RESUME record");
2516 I = ResumeInst::Create(Val);
2517 break;
2518 }
Chris Lattner231cbcb2007-05-02 04:27:25 +00002519 case bitc::FUNC_CODE_INST_UNWIND: // UNWIND
Owen Anderson1d0be152009-08-13 21:58:54 +00002520 I = new UnwindInst(Context);
Devang Patele8e02132009-09-18 19:26:43 +00002521 InstructionList.push_back(I);
Chris Lattner231cbcb2007-05-02 04:27:25 +00002522 break;
2523 case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE
Owen Anderson1d0be152009-08-13 21:58:54 +00002524 I = new UnreachableInst(Context);
Devang Patele8e02132009-09-18 19:26:43 +00002525 InstructionList.push_back(I);
Chris Lattner231cbcb2007-05-02 04:27:25 +00002526 break;
Chris Lattnerabfbf852007-05-06 00:21:25 +00002527 case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...]
Chris Lattner15e6d172007-05-04 19:11:41 +00002528 if (Record.size() < 1 || ((Record.size()-1)&1))
Chris Lattner2a98cca2007-05-03 18:58:09 +00002529 return Error("Invalid PHI record");
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002530 Type *Ty = getTypeByID(Record[0]);
Chris Lattner2a98cca2007-05-03 18:58:09 +00002531 if (!Ty) return Error("Invalid PHI record");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002532
Jay Foad3ecfc862011-03-30 11:28:46 +00002533 PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2);
Devang Patele8e02132009-09-18 19:26:43 +00002534 InstructionList.push_back(PN);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002535
Chris Lattner15e6d172007-05-04 19:11:41 +00002536 for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) {
2537 Value *V = getFnValueByID(Record[1+i], Ty);
2538 BasicBlock *BB = getBasicBlock(Record[2+i]);
Chris Lattner2a98cca2007-05-03 18:58:09 +00002539 if (!V || !BB) return Error("Invalid PHI record");
2540 PN->addIncoming(V, BB);
2541 }
2542 I = PN;
2543 break;
2544 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002545
Chris Lattner96a74c52011-06-17 18:09:11 +00002546 case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]
2547 if (Record.size() != 4)
2548 return Error("Invalid ALLOCA record");
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002549 PointerType *Ty =
Chris Lattner2a98cca2007-05-03 18:58:09 +00002550 dyn_cast_or_null<PointerType>(getTypeByID(Record[0]));
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002551 Type *OpTy = getTypeByID(Record[1]);
Chris Lattner96a74c52011-06-17 18:09:11 +00002552 Value *Size = getFnValueByID(Record[2], OpTy);
2553 unsigned Align = Record[3];
Chris Lattner2a98cca2007-05-03 18:58:09 +00002554 if (!Ty || !Size) return Error("Invalid ALLOCA record");
Owen Anderson50dead02009-07-15 23:53:25 +00002555 I = new AllocaInst(Ty->getElementType(), Size, (1 << Align) >> 1);
Devang Patele8e02132009-09-18 19:26:43 +00002556 InstructionList.push_back(I);
Chris Lattner2a98cca2007-05-03 18:58:09 +00002557 break;
2558 }
Chris Lattner0579f7f2007-05-03 22:04:19 +00002559 case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol]
Chris Lattner7337ab92007-05-06 00:00:00 +00002560 unsigned OpNum = 0;
2561 Value *Op;
2562 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
2563 OpNum+2 != Record.size())
Chris Lattnerabfbf852007-05-06 00:21:25 +00002564 return Error("Invalid LOAD record");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002565
Chris Lattner7337ab92007-05-06 00:00:00 +00002566 I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1);
Devang Patele8e02132009-09-18 19:26:43 +00002567 InstructionList.push_back(I);
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002568 break;
Chris Lattner0579f7f2007-05-03 22:04:19 +00002569 }
Eli Friedman21006d42011-08-09 23:02:53 +00002570 case bitc::FUNC_CODE_INST_LOADATOMIC: {
2571 // LOADATOMIC: [opty, op, align, vol, ordering, synchscope]
2572 unsigned OpNum = 0;
2573 Value *Op;
2574 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
2575 OpNum+4 != Record.size())
2576 return Error("Invalid LOADATOMIC record");
2577
2578
2579 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
2580 if (Ordering == NotAtomic || Ordering == Release ||
2581 Ordering == AcquireRelease)
2582 return Error("Invalid LOADATOMIC record");
2583 if (Ordering != NotAtomic && Record[OpNum] == 0)
2584 return Error("Invalid LOADATOMIC record");
2585 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
2586
2587 I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1,
2588 Ordering, SynchScope);
2589 InstructionList.push_back(I);
2590 break;
2591 }
Chris Lattner4f6bab92011-06-17 18:17:37 +00002592 case bitc::FUNC_CODE_INST_STORE: { // STORE2:[ptrty, ptr, val, align, vol]
Christopher Lambfe63fb92007-12-11 08:59:05 +00002593 unsigned OpNum = 0;
2594 Value *Val, *Ptr;
2595 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
Daniel Dunbara279bc32009-09-20 02:20:51 +00002596 getValue(Record, OpNum,
Christopher Lambfe63fb92007-12-11 08:59:05 +00002597 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
2598 OpNum+2 != Record.size())
2599 return Error("Invalid STORE record");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002600
Christopher Lambfe63fb92007-12-11 08:59:05 +00002601 I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1);
Devang Patele8e02132009-09-18 19:26:43 +00002602 InstructionList.push_back(I);
Christopher Lambfe63fb92007-12-11 08:59:05 +00002603 break;
2604 }
Eli Friedman21006d42011-08-09 23:02:53 +00002605 case bitc::FUNC_CODE_INST_STOREATOMIC: {
2606 // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, synchscope]
2607 unsigned OpNum = 0;
2608 Value *Val, *Ptr;
2609 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
2610 getValue(Record, OpNum,
2611 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
2612 OpNum+4 != Record.size())
2613 return Error("Invalid STOREATOMIC record");
2614
2615 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
2616 if (Ordering == NotAtomic || Ordering == Release ||
2617 Ordering == AcquireRelease)
2618 return Error("Invalid STOREATOMIC record");
2619 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
2620 if (Ordering != NotAtomic && Record[OpNum] == 0)
2621 return Error("Invalid STOREATOMIC record");
2622
2623 I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1,
2624 Ordering, SynchScope);
2625 InstructionList.push_back(I);
2626 break;
2627 }
Eli Friedmanff030482011-07-28 21:48:00 +00002628 case bitc::FUNC_CODE_INST_CMPXCHG: {
2629 // CMPXCHG:[ptrty, ptr, cmp, new, vol, ordering, synchscope]
2630 unsigned OpNum = 0;
2631 Value *Ptr, *Cmp, *New;
2632 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
2633 getValue(Record, OpNum,
2634 cast<PointerType>(Ptr->getType())->getElementType(), Cmp) ||
2635 getValue(Record, OpNum,
2636 cast<PointerType>(Ptr->getType())->getElementType(), New) ||
2637 OpNum+3 != Record.size())
2638 return Error("Invalid CMPXCHG record");
2639 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+1]);
Eli Friedman21006d42011-08-09 23:02:53 +00002640 if (Ordering == NotAtomic || Ordering == Unordered)
Eli Friedmanff030482011-07-28 21:48:00 +00002641 return Error("Invalid CMPXCHG record");
2642 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+2]);
2643 I = new AtomicCmpXchgInst(Ptr, Cmp, New, Ordering, SynchScope);
2644 cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]);
2645 InstructionList.push_back(I);
2646 break;
2647 }
2648 case bitc::FUNC_CODE_INST_ATOMICRMW: {
2649 // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, synchscope]
2650 unsigned OpNum = 0;
2651 Value *Ptr, *Val;
2652 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
2653 getValue(Record, OpNum,
2654 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
2655 OpNum+4 != Record.size())
2656 return Error("Invalid ATOMICRMW record");
2657 AtomicRMWInst::BinOp Operation = GetDecodedRMWOperation(Record[OpNum]);
2658 if (Operation < AtomicRMWInst::FIRST_BINOP ||
2659 Operation > AtomicRMWInst::LAST_BINOP)
2660 return Error("Invalid ATOMICRMW record");
2661 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
Eli Friedman21006d42011-08-09 23:02:53 +00002662 if (Ordering == NotAtomic || Ordering == Unordered)
Eli Friedmanff030482011-07-28 21:48:00 +00002663 return Error("Invalid ATOMICRMW record");
2664 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
2665 I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SynchScope);
2666 cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]);
2667 InstructionList.push_back(I);
2668 break;
2669 }
Eli Friedman47f35132011-07-25 23:16:38 +00002670 case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, synchscope]
2671 if (2 != Record.size())
2672 return Error("Invalid FENCE record");
2673 AtomicOrdering Ordering = GetDecodedOrdering(Record[0]);
2674 if (Ordering == NotAtomic || Ordering == Unordered ||
2675 Ordering == Monotonic)
2676 return Error("Invalid FENCE record");
2677 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[1]);
2678 I = new FenceInst(Context, Ordering, SynchScope);
2679 InstructionList.push_back(I);
2680 break;
2681 }
Chris Lattner4f6bab92011-06-17 18:17:37 +00002682 case bitc::FUNC_CODE_INST_CALL: {
Duncan Sandsdc024672007-11-27 13:23:08 +00002683 // CALL: [paramattrs, cc, fnty, fnid, arg0, arg1...]
2684 if (Record.size() < 3)
Chris Lattner0579f7f2007-05-03 22:04:19 +00002685 return Error("Invalid CALL record");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002686
Devang Patel05988662008-09-25 21:00:45 +00002687 AttrListPtr PAL = getAttributes(Record[0]);
Chris Lattnera9bb7132007-05-08 05:38:01 +00002688 unsigned CCInfo = Record[1];
Daniel Dunbara279bc32009-09-20 02:20:51 +00002689
Chris Lattnera9bb7132007-05-08 05:38:01 +00002690 unsigned OpNum = 2;
Chris Lattner7337ab92007-05-06 00:00:00 +00002691 Value *Callee;
2692 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
2693 return Error("Invalid CALL record");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002694
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002695 PointerType *OpTy = dyn_cast<PointerType>(Callee->getType());
2696 FunctionType *FTy = 0;
Chris Lattner0579f7f2007-05-03 22:04:19 +00002697 if (OpTy) FTy = dyn_cast<FunctionType>(OpTy->getElementType());
Chris Lattner7337ab92007-05-06 00:00:00 +00002698 if (!FTy || Record.size() < FTy->getNumParams()+OpNum)
Chris Lattner0579f7f2007-05-03 22:04:19 +00002699 return Error("Invalid CALL record");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002700
Chris Lattner0579f7f2007-05-03 22:04:19 +00002701 SmallVector<Value*, 16> Args;
2702 // Read the fixed params.
Chris Lattner7337ab92007-05-06 00:00:00 +00002703 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
Chris Lattner1afcace2011-07-09 17:41:24 +00002704 if (FTy->getParamType(i)->isLabelTy())
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002705 Args.push_back(getBasicBlock(Record[OpNum]));
Dan Gohman9b10dfb2010-09-13 18:00:48 +00002706 else
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002707 Args.push_back(getFnValueByID(Record[OpNum], FTy->getParamType(i)));
Chris Lattner0579f7f2007-05-03 22:04:19 +00002708 if (Args.back() == 0) return Error("Invalid CALL record");
2709 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002710
Chris Lattner0579f7f2007-05-03 22:04:19 +00002711 // Read type/value pairs for varargs params.
Chris Lattner0579f7f2007-05-03 22:04:19 +00002712 if (!FTy->isVarArg()) {
Chris Lattner7337ab92007-05-06 00:00:00 +00002713 if (OpNum != Record.size())
Chris Lattner0579f7f2007-05-03 22:04:19 +00002714 return Error("Invalid CALL record");
2715 } else {
Chris Lattner7337ab92007-05-06 00:00:00 +00002716 while (OpNum != Record.size()) {
2717 Value *Op;
2718 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
2719 return Error("Invalid CALL record");
2720 Args.push_back(Op);
Chris Lattner0579f7f2007-05-03 22:04:19 +00002721 }
2722 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002723
Jay Foada3efbb12011-07-15 08:37:34 +00002724 I = CallInst::Create(Callee, Args);
Devang Patele8e02132009-09-18 19:26:43 +00002725 InstructionList.push_back(I);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002726 cast<CallInst>(I)->setCallingConv(
2727 static_cast<CallingConv::ID>(CCInfo>>1));
Chris Lattner76520192007-05-03 22:34:03 +00002728 cast<CallInst>(I)->setTailCall(CCInfo & 1);
Devang Patel05988662008-09-25 21:00:45 +00002729 cast<CallInst>(I)->setAttributes(PAL);
Chris Lattner0579f7f2007-05-03 22:04:19 +00002730 break;
2731 }
2732 case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty]
2733 if (Record.size() < 3)
2734 return Error("Invalid VAARG record");
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002735 Type *OpTy = getTypeByID(Record[0]);
Chris Lattner0579f7f2007-05-03 22:04:19 +00002736 Value *Op = getFnValueByID(Record[1], OpTy);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002737 Type *ResTy = getTypeByID(Record[2]);
Chris Lattner0579f7f2007-05-03 22:04:19 +00002738 if (!OpTy || !Op || !ResTy)
2739 return Error("Invalid VAARG record");
2740 I = new VAArgInst(Op, ResTy);
Devang Patele8e02132009-09-18 19:26:43 +00002741 InstructionList.push_back(I);
Chris Lattner0579f7f2007-05-03 22:04:19 +00002742 break;
2743 }
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002744 }
2745
2746 // Add instruction to end of current BB. If there is no current BB, reject
2747 // this file.
2748 if (CurBB == 0) {
2749 delete I;
2750 return Error("Invalid instruction with no BB");
2751 }
2752 CurBB->getInstList().push_back(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002753
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002754 // If this was a terminator instruction, move to the next block.
2755 if (isa<TerminatorInst>(I)) {
2756 ++CurBBNo;
2757 CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : 0;
2758 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002759
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002760 // Non-void values get registered in the value table for future use.
Benjamin Kramerf0127052010-01-05 13:12:22 +00002761 if (I && !I->getType()->isVoidTy())
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002762 ValueList.AssignValue(I, NextValueNo++);
Chris Lattner980e5aa2007-05-01 05:52:21 +00002763 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002764
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002765 // Check the function list for unresolved values.
2766 if (Argument *A = dyn_cast<Argument>(ValueList.back())) {
2767 if (A->getParent() == 0) {
2768 // We found at least one unresolved value. Nuke them all to avoid leaks.
2769 for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){
Dan Gohman56e2a572010-08-25 20:20:21 +00002770 if ((A = dyn_cast<Argument>(ValueList[i])) && A->getParent() == 0) {
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002771 A->replaceAllUsesWith(UndefValue::get(A->getType()));
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002772 delete A;
2773 }
2774 }
Chris Lattner35a04702007-05-04 03:50:29 +00002775 return Error("Never resolved value found in function!");
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002776 }
Chris Lattnera7c49aa2007-05-01 07:01:57 +00002777 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002778
Dan Gohman064ff3e2010-08-25 20:23:38 +00002779 // FIXME: Check for unresolved forward-declared metadata references
2780 // and clean up leaks.
2781
Chris Lattner50b136d2009-10-28 05:53:48 +00002782 // See if anything took the address of blocks in this function. If so,
2783 // resolve them now.
Chris Lattner50b136d2009-10-28 05:53:48 +00002784 DenseMap<Function*, std::vector<BlockAddrRefTy> >::iterator BAFRI =
2785 BlockAddrFwdRefs.find(F);
2786 if (BAFRI != BlockAddrFwdRefs.end()) {
2787 std::vector<BlockAddrRefTy> &RefList = BAFRI->second;
2788 for (unsigned i = 0, e = RefList.size(); i != e; ++i) {
2789 unsigned BlockIdx = RefList[i].first;
Chris Lattnercdfc9402009-11-01 01:27:45 +00002790 if (BlockIdx >= FunctionBBs.size())
Chris Lattner50b136d2009-10-28 05:53:48 +00002791 return Error("Invalid blockaddress block #");
2792
2793 GlobalVariable *FwdRef = RefList[i].second;
Chris Lattnercdfc9402009-11-01 01:27:45 +00002794 FwdRef->replaceAllUsesWith(BlockAddress::get(F, FunctionBBs[BlockIdx]));
Chris Lattner50b136d2009-10-28 05:53:48 +00002795 FwdRef->eraseFromParent();
2796 }
2797
2798 BlockAddrFwdRefs.erase(BAFRI);
2799 }
2800
Chris Lattner980e5aa2007-05-01 05:52:21 +00002801 // Trim the value list down to the size it was before we parsed this function.
2802 ValueList.shrinkTo(ModuleValueListSize);
Dan Gohman69813832010-08-25 20:22:53 +00002803 MDValueList.shrinkTo(ModuleMDValueListSize);
Chris Lattner980e5aa2007-05-01 05:52:21 +00002804 std::vector<BasicBlock*>().swap(FunctionBBs);
Chris Lattner48f84872007-05-01 04:59:48 +00002805 return false;
2806}
2807
Chris Lattnerb348bb82007-05-18 04:02:46 +00002808//===----------------------------------------------------------------------===//
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00002809// GVMaterializer implementation
Chris Lattnerb348bb82007-05-18 04:02:46 +00002810//===----------------------------------------------------------------------===//
2811
2812
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00002813bool BitcodeReader::isMaterializable(const GlobalValue *GV) const {
2814 if (const Function *F = dyn_cast<Function>(GV)) {
2815 return F->isDeclaration() &&
2816 DeferredFunctionInfo.count(const_cast<Function*>(F));
2817 }
2818 return false;
2819}
Daniel Dunbara279bc32009-09-20 02:20:51 +00002820
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00002821bool BitcodeReader::Materialize(GlobalValue *GV, std::string *ErrInfo) {
2822 Function *F = dyn_cast<Function>(GV);
2823 // If it's not a function or is already material, ignore the request.
2824 if (!F || !F->isMaterializable()) return false;
2825
2826 DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F);
Chris Lattnerb348bb82007-05-18 04:02:46 +00002827 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002828
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00002829 // Move the bit stream to the saved position of the deferred function body.
2830 Stream.JumpToBit(DFII->second);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002831
Chris Lattnerb348bb82007-05-18 04:02:46 +00002832 if (ParseFunctionBody(F)) {
2833 if (ErrInfo) *ErrInfo = ErrorString;
2834 return true;
2835 }
Chandler Carruth69940402007-08-04 01:51:18 +00002836
2837 // Upgrade any old intrinsic calls in the function.
2838 for (UpgradedIntrinsicMap::iterator I = UpgradedIntrinsics.begin(),
2839 E = UpgradedIntrinsics.end(); I != E; ++I) {
2840 if (I->first != I->second) {
2841 for (Value::use_iterator UI = I->first->use_begin(),
2842 UE = I->first->use_end(); UI != UE; ) {
2843 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
2844 UpgradeIntrinsicCall(CI, I->second);
2845 }
2846 }
2847 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002848
Chris Lattnerb348bb82007-05-18 04:02:46 +00002849 return false;
2850}
2851
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00002852bool BitcodeReader::isDematerializable(const GlobalValue *GV) const {
2853 const Function *F = dyn_cast<Function>(GV);
2854 if (!F || F->isDeclaration())
2855 return false;
2856 return DeferredFunctionInfo.count(const_cast<Function*>(F));
2857}
2858
2859void BitcodeReader::Dematerialize(GlobalValue *GV) {
2860 Function *F = dyn_cast<Function>(GV);
2861 // If this function isn't dematerializable, this is a noop.
2862 if (!F || !isDematerializable(F))
Chris Lattnerb348bb82007-05-18 04:02:46 +00002863 return;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002864
Chris Lattnerb348bb82007-05-18 04:02:46 +00002865 assert(DeferredFunctionInfo.count(F) && "No info to read function later?");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002866
Chris Lattnerb348bb82007-05-18 04:02:46 +00002867 // Just forget the function body, we can remat it later.
2868 F->deleteBody();
Chris Lattnerb348bb82007-05-18 04:02:46 +00002869}
2870
2871
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00002872bool BitcodeReader::MaterializeModule(Module *M, std::string *ErrInfo) {
2873 assert(M == TheModule &&
2874 "Can only Materialize the Module this BitcodeReader is attached to.");
Chris Lattner714fa952009-06-16 05:15:21 +00002875 // Iterate over the module, deserializing any functions that are still on
2876 // disk.
2877 for (Module::iterator F = TheModule->begin(), E = TheModule->end();
2878 F != E; ++F)
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00002879 if (F->isMaterializable() &&
2880 Materialize(F, ErrInfo))
2881 return true;
Chandler Carruth69940402007-08-04 01:51:18 +00002882
Daniel Dunbara279bc32009-09-20 02:20:51 +00002883 // Upgrade any intrinsic calls that slipped through (should not happen!) and
2884 // delete the old functions to clean up. We can't do this unless the entire
2885 // module is materialized because there could always be another function body
Chandler Carruth69940402007-08-04 01:51:18 +00002886 // with calls to the old function.
2887 for (std::vector<std::pair<Function*, Function*> >::iterator I =
2888 UpgradedIntrinsics.begin(), E = UpgradedIntrinsics.end(); I != E; ++I) {
2889 if (I->first != I->second) {
2890 for (Value::use_iterator UI = I->first->use_begin(),
2891 UE = I->first->use_end(); UI != UE; ) {
2892 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
2893 UpgradeIntrinsicCall(CI, I->second);
2894 }
Chris Lattner7d9eb582009-04-01 01:43:03 +00002895 if (!I->first->use_empty())
2896 I->first->replaceAllUsesWith(I->second);
Chandler Carruth69940402007-08-04 01:51:18 +00002897 I->first->eraseFromParent();
2898 }
2899 }
2900 std::vector<std::pair<Function*, Function*> >().swap(UpgradedIntrinsics);
Devang Patele4b27562009-08-28 23:24:31 +00002901
2902 // Check debug info intrinsics.
2903 CheckDebugInfoIntrinsics(TheModule);
2904
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00002905 return false;
Chris Lattnerb348bb82007-05-18 04:02:46 +00002906}
2907
Chris Lattner48f84872007-05-01 04:59:48 +00002908
Chris Lattnerc453f762007-04-29 07:54:31 +00002909//===----------------------------------------------------------------------===//
2910// External interface
2911//===----------------------------------------------------------------------===//
2912
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00002913/// getLazyBitcodeModule - lazy function-at-a-time loading from a file.
Chris Lattnerc453f762007-04-29 07:54:31 +00002914///
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00002915Module *llvm::getLazyBitcodeModule(MemoryBuffer *Buffer,
2916 LLVMContext& Context,
2917 std::string *ErrMsg) {
2918 Module *M = new Module(Buffer->getBufferIdentifier(), Context);
Owen Anderson8b477ed2009-07-01 16:58:40 +00002919 BitcodeReader *R = new BitcodeReader(Buffer, Context);
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00002920 M->setMaterializer(R);
2921 if (R->ParseBitcodeInto(M)) {
Chris Lattnerc453f762007-04-29 07:54:31 +00002922 if (ErrMsg)
2923 *ErrMsg = R->getErrorString();
Daniel Dunbara279bc32009-09-20 02:20:51 +00002924
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00002925 delete M; // Also deletes R.
Chris Lattnerc453f762007-04-29 07:54:31 +00002926 return 0;
2927 }
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00002928 // Have the BitcodeReader dtor delete 'Buffer'.
2929 R->setBufferOwned(true);
2930 return M;
Chris Lattnerc453f762007-04-29 07:54:31 +00002931}
2932
2933/// ParseBitcodeFile - Read the specified bitcode file, returning the module.
2934/// If an error occurs, return null and fill in *ErrMsg if non-null.
Daniel Dunbara279bc32009-09-20 02:20:51 +00002935Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
Owen Anderson8b477ed2009-07-01 16:58:40 +00002936 std::string *ErrMsg){
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00002937 Module *M = getLazyBitcodeModule(Buffer, Context, ErrMsg);
2938 if (!M) return 0;
Chris Lattnerb348bb82007-05-18 04:02:46 +00002939
2940 // Don't let the BitcodeReader dtor delete 'Buffer', regardless of whether
2941 // there was an error.
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00002942 static_cast<BitcodeReader*>(M->getMaterializer())->setBufferOwned(false);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002943
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00002944 // Read in the entire module, and destroy the BitcodeReader.
2945 if (M->MaterializeAllPermanently(ErrMsg)) {
2946 delete M;
Bill Wendling34711742010-10-06 01:22:42 +00002947 return 0;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00002948 }
Bill Wendling34711742010-10-06 01:22:42 +00002949
Chris Lattnerc453f762007-04-29 07:54:31 +00002950 return M;
2951}
Bill Wendling34711742010-10-06 01:22:42 +00002952
2953std::string llvm::getBitcodeTargetTriple(MemoryBuffer *Buffer,
2954 LLVMContext& Context,
2955 std::string *ErrMsg) {
2956 BitcodeReader *R = new BitcodeReader(Buffer, Context);
2957 // Don't let the BitcodeReader dtor delete 'Buffer'.
2958 R->setBufferOwned(false);
2959
2960 std::string Triple("");
2961 if (R->ParseTriple(Triple))
2962 if (ErrMsg)
2963 *ErrMsg = R->getErrorString();
2964
2965 delete R;
2966 return Triple;
2967}