Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1 | //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 9 | |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 10 | #include "llvm/Bitcode/ReaderWriter.h" |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 11 | #include "BitcodeReader.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/SmallString.h" |
| 13 | #include "llvm/ADT/SmallVector.h" |
Tobias Grosser | 0a8e12f | 2013-07-26 04:16:55 +0000 | [diff] [blame] | 14 | #include "llvm/Bitcode/LLVMBitCodes.h" |
Chandler Carruth | 9106521 | 2014-03-05 10:34:14 +0000 | [diff] [blame] | 15 | #include "llvm/IR/AutoUpgrade.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Constants.h" |
| 17 | #include "llvm/IR/DerivedTypes.h" |
| 18 | #include "llvm/IR/InlineAsm.h" |
| 19 | #include "llvm/IR/IntrinsicInst.h" |
Manman Ren | 209b17c | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 20 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Module.h" |
| 22 | #include "llvm/IR/OperandTraits.h" |
| 23 | #include "llvm/IR/Operator.h" |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 24 | #include "llvm/Support/DataStream.h" |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 26 | #include "llvm/Support/MemoryBuffer.h" |
Tobias Grosser | 0a8e12f | 2013-07-26 04:16:55 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 30 | enum { |
| 31 | SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex |
| 32 | }; |
| 33 | |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 34 | std::error_code BitcodeReader::materializeForwardReferencedFunctions() { |
| 35 | if (WillMaterializeAllForwardRefs) |
| 36 | return std::error_code(); |
| 37 | |
| 38 | // Prevent recursion. |
| 39 | WillMaterializeAllForwardRefs = true; |
| 40 | |
Duncan P. N. Exon Smith | 5a511b5 | 2014-08-05 17:49:48 +0000 | [diff] [blame] | 41 | while (!BasicBlockFwdRefQueue.empty()) { |
| 42 | Function *F = BasicBlockFwdRefQueue.front(); |
| 43 | BasicBlockFwdRefQueue.pop_front(); |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 44 | assert(F && "Expected valid function"); |
Duncan P. N. Exon Smith | 5a511b5 | 2014-08-05 17:49:48 +0000 | [diff] [blame] | 45 | if (!BasicBlockFwdRefs.count(F)) |
| 46 | // Already materialized. |
| 47 | continue; |
| 48 | |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 49 | // Check for a function that isn't materializable to prevent an infinite |
| 50 | // loop. When parsing a blockaddress stored in a global variable, there |
| 51 | // isn't a trivial way to check if a function will have a body without a |
| 52 | // linear search through FunctionsWithBodies, so just check it here. |
| 53 | if (!F->isMaterializable()) |
| 54 | return Error(BitcodeError::NeverResolvedFunctionFromBlockAddress); |
| 55 | |
| 56 | // Try to materialize F. |
| 57 | if (std::error_code EC = Materialize(F)) |
| 58 | return EC; |
Rafael Espindola | b799346 | 2012-01-02 07:49:53 +0000 | [diff] [blame] | 59 | } |
Duncan P. N. Exon Smith | 5a511b5 | 2014-08-05 17:49:48 +0000 | [diff] [blame] | 60 | assert(BasicBlockFwdRefs.empty() && "Function missing from queue"); |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 61 | |
| 62 | // Reset state. |
| 63 | WillMaterializeAllForwardRefs = false; |
| 64 | return std::error_code(); |
Rafael Espindola | b799346 | 2012-01-02 07:49:53 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 67 | void BitcodeReader::FreeState() { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 68 | Buffer = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 69 | std::vector<Type*>().swap(TypeList); |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 70 | ValueList.clear(); |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 71 | MDValueList.clear(); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 72 | std::vector<Comdat *>().swap(ComdatList); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 73 | |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 74 | std::vector<AttributeSet>().swap(MAttributes); |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 75 | std::vector<BasicBlock*>().swap(FunctionBBs); |
| 76 | std::vector<Function*>().swap(FunctionsWithBodies); |
| 77 | DeferredFunctionInfo.clear(); |
Dan Gohman | 43aa8f0 | 2010-07-20 21:42:28 +0000 | [diff] [blame] | 78 | MDKindMap.clear(); |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 79 | |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 80 | assert(BasicBlockFwdRefs.empty() && "Unresolved blockaddress fwd references"); |
Duncan P. N. Exon Smith | 5a511b5 | 2014-08-05 17:49:48 +0000 | [diff] [blame] | 81 | BasicBlockFwdRefQueue.clear(); |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 84 | //===----------------------------------------------------------------------===// |
| 85 | // Helper functions to implement forward reference resolution, etc. |
| 86 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 88 | /// ConvertToString - Convert a string from a record into an std::string, return |
| 89 | /// true on failure. |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 90 | template<typename StrTy> |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 91 | static bool ConvertToString(ArrayRef<uint64_t> Record, unsigned Idx, |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 92 | StrTy &Result) { |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 93 | if (Idx > Record.size()) |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 94 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 95 | |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 96 | for (unsigned i = Idx, e = Record.size(); i != e; ++i) |
| 97 | Result += (char)Record[i]; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 98 | return false; |
| 99 | } |
| 100 | |
| 101 | static GlobalValue::LinkageTypes GetDecodedLinkage(unsigned Val) { |
| 102 | switch (Val) { |
| 103 | default: // Map unknown/new linkages to external |
Bill Wendling | a3c6f6b | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 104 | case 0: return GlobalValue::ExternalLinkage; |
| 105 | case 1: return GlobalValue::WeakAnyLinkage; |
| 106 | case 2: return GlobalValue::AppendingLinkage; |
| 107 | case 3: return GlobalValue::InternalLinkage; |
| 108 | case 4: return GlobalValue::LinkOnceAnyLinkage; |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 109 | case 5: return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage |
| 110 | case 6: return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage |
Bill Wendling | a3c6f6b | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 111 | case 7: return GlobalValue::ExternalWeakLinkage; |
| 112 | case 8: return GlobalValue::CommonLinkage; |
| 113 | case 9: return GlobalValue::PrivateLinkage; |
Duncan Sands | 12da8ce | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 114 | case 10: return GlobalValue::WeakODRLinkage; |
| 115 | case 11: return GlobalValue::LinkOnceODRLinkage; |
Chris Lattner | 184f1be | 2009-04-13 05:44:34 +0000 | [diff] [blame] | 116 | case 12: return GlobalValue::AvailableExternallyLinkage; |
Rafael Espindola | 2fb5bc3 | 2014-03-13 23:18:37 +0000 | [diff] [blame] | 117 | case 13: |
| 118 | return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage |
| 119 | case 14: |
| 120 | return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
| 124 | static GlobalValue::VisibilityTypes GetDecodedVisibility(unsigned Val) { |
| 125 | switch (Val) { |
| 126 | default: // Map unknown visibilities to default. |
| 127 | case 0: return GlobalValue::DefaultVisibility; |
| 128 | case 1: return GlobalValue::HiddenVisibility; |
Anton Korobeynikov | 31fc4f9 | 2007-04-29 20:56:48 +0000 | [diff] [blame] | 129 | case 2: return GlobalValue::ProtectedVisibility; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 133 | static GlobalValue::DLLStorageClassTypes |
| 134 | GetDecodedDLLStorageClass(unsigned Val) { |
| 135 | switch (Val) { |
| 136 | default: // Map unknown values to default. |
| 137 | case 0: return GlobalValue::DefaultStorageClass; |
| 138 | case 1: return GlobalValue::DLLImportStorageClass; |
| 139 | case 2: return GlobalValue::DLLExportStorageClass; |
| 140 | } |
| 141 | } |
| 142 | |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 143 | static GlobalVariable::ThreadLocalMode GetDecodedThreadLocalMode(unsigned Val) { |
| 144 | switch (Val) { |
| 145 | case 0: return GlobalVariable::NotThreadLocal; |
| 146 | default: // Map unknown non-zero value to general dynamic. |
| 147 | case 1: return GlobalVariable::GeneralDynamicTLSModel; |
| 148 | case 2: return GlobalVariable::LocalDynamicTLSModel; |
| 149 | case 3: return GlobalVariable::InitialExecTLSModel; |
| 150 | case 4: return GlobalVariable::LocalExecTLSModel; |
| 151 | } |
| 152 | } |
| 153 | |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 154 | static int GetDecodedCastOpcode(unsigned Val) { |
| 155 | switch (Val) { |
| 156 | default: return -1; |
| 157 | case bitc::CAST_TRUNC : return Instruction::Trunc; |
| 158 | case bitc::CAST_ZEXT : return Instruction::ZExt; |
| 159 | case bitc::CAST_SEXT : return Instruction::SExt; |
| 160 | case bitc::CAST_FPTOUI : return Instruction::FPToUI; |
| 161 | case bitc::CAST_FPTOSI : return Instruction::FPToSI; |
| 162 | case bitc::CAST_UITOFP : return Instruction::UIToFP; |
| 163 | case bitc::CAST_SITOFP : return Instruction::SIToFP; |
| 164 | case bitc::CAST_FPTRUNC : return Instruction::FPTrunc; |
| 165 | case bitc::CAST_FPEXT : return Instruction::FPExt; |
| 166 | case bitc::CAST_PTRTOINT: return Instruction::PtrToInt; |
| 167 | case bitc::CAST_INTTOPTR: return Instruction::IntToPtr; |
| 168 | case bitc::CAST_BITCAST : return Instruction::BitCast; |
Matt Arsenault | 3aa9b03 | 2013-11-18 02:51:33 +0000 | [diff] [blame] | 169 | case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast; |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 170 | } |
| 171 | } |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 172 | static int GetDecodedBinaryOpcode(unsigned Val, Type *Ty) { |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 173 | switch (Val) { |
| 174 | default: return -1; |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 175 | case bitc::BINOP_ADD: |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 176 | return Ty->isFPOrFPVectorTy() ? Instruction::FAdd : Instruction::Add; |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 177 | case bitc::BINOP_SUB: |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 178 | return Ty->isFPOrFPVectorTy() ? Instruction::FSub : Instruction::Sub; |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 179 | case bitc::BINOP_MUL: |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 180 | return Ty->isFPOrFPVectorTy() ? Instruction::FMul : Instruction::Mul; |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 181 | case bitc::BINOP_UDIV: return Instruction::UDiv; |
| 182 | case bitc::BINOP_SDIV: |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 183 | return Ty->isFPOrFPVectorTy() ? Instruction::FDiv : Instruction::SDiv; |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 184 | case bitc::BINOP_UREM: return Instruction::URem; |
| 185 | case bitc::BINOP_SREM: |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 186 | return Ty->isFPOrFPVectorTy() ? Instruction::FRem : Instruction::SRem; |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 187 | case bitc::BINOP_SHL: return Instruction::Shl; |
| 188 | case bitc::BINOP_LSHR: return Instruction::LShr; |
| 189 | case bitc::BINOP_ASHR: return Instruction::AShr; |
| 190 | case bitc::BINOP_AND: return Instruction::And; |
| 191 | case bitc::BINOP_OR: return Instruction::Or; |
| 192 | case bitc::BINOP_XOR: return Instruction::Xor; |
| 193 | } |
| 194 | } |
| 195 | |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 196 | static AtomicRMWInst::BinOp GetDecodedRMWOperation(unsigned Val) { |
| 197 | switch (Val) { |
| 198 | default: return AtomicRMWInst::BAD_BINOP; |
| 199 | case bitc::RMW_XCHG: return AtomicRMWInst::Xchg; |
| 200 | case bitc::RMW_ADD: return AtomicRMWInst::Add; |
| 201 | case bitc::RMW_SUB: return AtomicRMWInst::Sub; |
| 202 | case bitc::RMW_AND: return AtomicRMWInst::And; |
| 203 | case bitc::RMW_NAND: return AtomicRMWInst::Nand; |
| 204 | case bitc::RMW_OR: return AtomicRMWInst::Or; |
| 205 | case bitc::RMW_XOR: return AtomicRMWInst::Xor; |
| 206 | case bitc::RMW_MAX: return AtomicRMWInst::Max; |
| 207 | case bitc::RMW_MIN: return AtomicRMWInst::Min; |
| 208 | case bitc::RMW_UMAX: return AtomicRMWInst::UMax; |
| 209 | case bitc::RMW_UMIN: return AtomicRMWInst::UMin; |
| 210 | } |
| 211 | } |
| 212 | |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 213 | static AtomicOrdering GetDecodedOrdering(unsigned Val) { |
| 214 | switch (Val) { |
| 215 | case bitc::ORDERING_NOTATOMIC: return NotAtomic; |
| 216 | case bitc::ORDERING_UNORDERED: return Unordered; |
| 217 | case bitc::ORDERING_MONOTONIC: return Monotonic; |
| 218 | case bitc::ORDERING_ACQUIRE: return Acquire; |
| 219 | case bitc::ORDERING_RELEASE: return Release; |
| 220 | case bitc::ORDERING_ACQREL: return AcquireRelease; |
| 221 | default: // Map unknown orderings to sequentially-consistent. |
| 222 | case bitc::ORDERING_SEQCST: return SequentiallyConsistent; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | static SynchronizationScope GetDecodedSynchScope(unsigned Val) { |
| 227 | switch (Val) { |
| 228 | case bitc::SYNCHSCOPE_SINGLETHREAD: return SingleThread; |
| 229 | default: // Map unknown scopes to cross-thread. |
| 230 | case bitc::SYNCHSCOPE_CROSSTHREAD: return CrossThread; |
| 231 | } |
| 232 | } |
| 233 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 234 | static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) { |
| 235 | switch (Val) { |
| 236 | default: // Map unknown selection kinds to any. |
| 237 | case bitc::COMDAT_SELECTION_KIND_ANY: |
| 238 | return Comdat::Any; |
| 239 | case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH: |
| 240 | return Comdat::ExactMatch; |
| 241 | case bitc::COMDAT_SELECTION_KIND_LARGEST: |
| 242 | return Comdat::Largest; |
| 243 | case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES: |
| 244 | return Comdat::NoDuplicates; |
| 245 | case bitc::COMDAT_SELECTION_KIND_SAME_SIZE: |
| 246 | return Comdat::SameSize; |
| 247 | } |
| 248 | } |
| 249 | |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 250 | static void UpgradeDLLImportExportLinkage(llvm::GlobalValue *GV, unsigned Val) { |
| 251 | switch (Val) { |
| 252 | case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break; |
| 253 | case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break; |
| 254 | } |
| 255 | } |
| 256 | |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 257 | namespace llvm { |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 258 | namespace { |
| 259 | /// @brief A class for maintaining the slot number definition |
| 260 | /// as a placeholder for the actual definition for forward constants defs. |
| 261 | class ConstantPlaceHolder : public ConstantExpr { |
Craig Topper | a60c0f1 | 2012-09-15 17:09:36 +0000 | [diff] [blame] | 262 | void operator=(const ConstantPlaceHolder &) LLVM_DELETED_FUNCTION; |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 263 | public: |
| 264 | // allocate space for exactly one operand |
| 265 | void *operator new(size_t s) { |
| 266 | return User::operator new(s, 1); |
| 267 | } |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 268 | explicit ConstantPlaceHolder(Type *Ty, LLVMContext& Context) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 269 | : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) { |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 270 | Op<0>() = UndefValue::get(Type::getInt32Ty(Context)); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 271 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 272 | |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 273 | /// @brief Methods to support type inquiry through isa, cast, and dyn_cast. |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 274 | static bool classof(const Value *V) { |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 275 | return isa<ConstantExpr>(V) && |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 276 | cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1; |
| 277 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 278 | |
| 279 | |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 280 | /// Provide fast operand accessors |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 281 | //DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 282 | }; |
| 283 | } |
| 284 | |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 285 | // FIXME: can we inherit this from ConstantExpr? |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 286 | template <> |
Jay Foad | c8adf5f | 2011-01-11 15:07:38 +0000 | [diff] [blame] | 287 | struct OperandTraits<ConstantPlaceHolder> : |
| 288 | public FixedNumOperandTraits<ConstantPlaceHolder, 1> { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 289 | }; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 292 | |
| 293 | void BitcodeReaderValueList::AssignValue(Value *V, unsigned Idx) { |
| 294 | if (Idx == size()) { |
| 295 | push_back(V); |
| 296 | return; |
| 297 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 298 | |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 299 | if (Idx >= size()) |
| 300 | resize(Idx+1); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 301 | |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 302 | WeakVH &OldV = ValuePtrs[Idx]; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 303 | if (!OldV) { |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 304 | OldV = V; |
| 305 | return; |
| 306 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 307 | |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 308 | // Handle constants and non-constants (e.g. instrs) differently for |
| 309 | // efficiency. |
| 310 | if (Constant *PHC = dyn_cast<Constant>(&*OldV)) { |
| 311 | ResolveConstants.push_back(std::make_pair(PHC, Idx)); |
| 312 | OldV = V; |
| 313 | } else { |
| 314 | // If there was a forward reference to this value, replace it. |
| 315 | Value *PrevVal = OldV; |
| 316 | OldV->replaceAllUsesWith(V); |
| 317 | delete PrevVal; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 318 | } |
| 319 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 320 | |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 321 | |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 322 | Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 323 | Type *Ty) { |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 324 | if (Idx >= size()) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 325 | resize(Idx + 1); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 326 | |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 327 | if (Value *V = ValuePtrs[Idx]) { |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 328 | assert(Ty == V->getType() && "Type mismatch in constant table!"); |
| 329 | return cast<Constant>(V); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 330 | } |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 331 | |
| 332 | // Create and return a placeholder, which will later be RAUW'd. |
Owen Anderson | e9f9804 | 2009-07-07 20:18:58 +0000 | [diff] [blame] | 333 | Constant *C = new ConstantPlaceHolder(Ty, Context); |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 334 | ValuePtrs[Idx] = C; |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 335 | return C; |
| 336 | } |
| 337 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 338 | Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, Type *Ty) { |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 339 | if (Idx >= size()) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 340 | resize(Idx + 1); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 341 | |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 342 | if (Value *V = ValuePtrs[Idx]) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 343 | assert((!Ty || Ty == V->getType()) && "Type mismatch in value table!"); |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 344 | return V; |
| 345 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 346 | |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 347 | // No type specified, must be invalid reference. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 348 | if (!Ty) return nullptr; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 349 | |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 350 | // Create and return a placeholder, which will later be RAUW'd. |
| 351 | Value *V = new Argument(Ty); |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 352 | ValuePtrs[Idx] = V; |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 353 | return V; |
| 354 | } |
| 355 | |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 356 | /// ResolveConstantForwardRefs - Once all constants are read, this method bulk |
| 357 | /// resolves any forward references. The idea behind this is that we sometimes |
| 358 | /// get constants (such as large arrays) which reference *many* forward ref |
| 359 | /// constants. Replacing each of these causes a lot of thrashing when |
| 360 | /// building/reuniquing the constant. Instead of doing this, we look at all the |
| 361 | /// uses and rewrite all the place holders at once for any constant that uses |
| 362 | /// a placeholder. |
| 363 | void BitcodeReaderValueList::ResolveConstantForwardRefs() { |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 364 | // Sort the values by-pointer so that they are efficient to look up with a |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 365 | // binary search. |
| 366 | std::sort(ResolveConstants.begin(), ResolveConstants.end()); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 367 | |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 368 | SmallVector<Constant*, 64> NewOps; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 369 | |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 370 | while (!ResolveConstants.empty()) { |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 371 | Value *RealVal = operator[](ResolveConstants.back().second); |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 372 | Constant *Placeholder = ResolveConstants.back().first; |
| 373 | ResolveConstants.pop_back(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 374 | |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 375 | // Loop over all users of the placeholder, updating them to reference the |
| 376 | // new value. If they reference more than one placeholder, update them all |
| 377 | // at once. |
| 378 | while (!Placeholder->use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 379 | auto UI = Placeholder->user_begin(); |
Gabor Greif | 2c0ab48 | 2010-07-09 16:01:21 +0000 | [diff] [blame] | 380 | User *U = *UI; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 381 | |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 382 | // If the using object isn't uniqued, just update the operands. This |
| 383 | // handles instructions and initializers for global variables. |
Gabor Greif | 2c0ab48 | 2010-07-09 16:01:21 +0000 | [diff] [blame] | 384 | if (!isa<Constant>(U) || isa<GlobalValue>(U)) { |
Chris Lattner | 479c5d9 | 2008-08-21 17:31:45 +0000 | [diff] [blame] | 385 | UI.getUse().set(RealVal); |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 386 | continue; |
| 387 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 388 | |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 389 | // Otherwise, we have a constant that uses the placeholder. Replace that |
| 390 | // constant with a new constant that has *all* placeholder uses updated. |
Gabor Greif | 2c0ab48 | 2010-07-09 16:01:21 +0000 | [diff] [blame] | 391 | Constant *UserC = cast<Constant>(U); |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 392 | for (User::op_iterator I = UserC->op_begin(), E = UserC->op_end(); |
| 393 | I != E; ++I) { |
| 394 | Value *NewOp; |
| 395 | if (!isa<ConstantPlaceHolder>(*I)) { |
| 396 | // Not a placeholder reference. |
| 397 | NewOp = *I; |
| 398 | } else if (*I == Placeholder) { |
| 399 | // Common case is that it just references this one placeholder. |
| 400 | NewOp = RealVal; |
| 401 | } else { |
| 402 | // Otherwise, look up the placeholder in ResolveConstants. |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 403 | ResolveConstantsTy::iterator It = |
| 404 | std::lower_bound(ResolveConstants.begin(), ResolveConstants.end(), |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 405 | std::pair<Constant*, unsigned>(cast<Constant>(*I), |
| 406 | 0)); |
| 407 | assert(It != ResolveConstants.end() && It->first == *I); |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 408 | NewOp = operator[](It->second); |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | NewOps.push_back(cast<Constant>(NewOp)); |
| 412 | } |
| 413 | |
| 414 | // Make the new constant. |
| 415 | Constant *NewC; |
| 416 | if (ConstantArray *UserCA = dyn_cast<ConstantArray>(UserC)) { |
Jay Foad | 83be361 | 2011-06-22 09:24:39 +0000 | [diff] [blame] | 417 | NewC = ConstantArray::get(UserCA->getType(), NewOps); |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 418 | } else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) { |
Chris Lattner | cc19efa | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 419 | NewC = ConstantStruct::get(UserCS->getType(), NewOps); |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 420 | } else if (isa<ConstantVector>(UserC)) { |
Chris Lattner | 6922931 | 2011-02-15 00:14:00 +0000 | [diff] [blame] | 421 | NewC = ConstantVector::get(NewOps); |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 422 | } else { |
| 423 | assert(isa<ConstantExpr>(UserC) && "Must be a ConstantExpr."); |
Jay Foad | 5c984e56 | 2011-04-13 13:46:01 +0000 | [diff] [blame] | 424 | NewC = cast<ConstantExpr>(UserC)->getWithOperands(NewOps); |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 425 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 426 | |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 427 | UserC->replaceAllUsesWith(NewC); |
| 428 | UserC->destroyConstant(); |
| 429 | NewOps.clear(); |
| 430 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 431 | |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 432 | // Update all ValueHandles, they should be the only users at this point. |
| 433 | Placeholder->replaceAllUsesWith(RealVal); |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 434 | delete Placeholder; |
| 435 | } |
| 436 | } |
| 437 | |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 438 | void BitcodeReaderMDValueList::AssignValue(Value *V, unsigned Idx) { |
| 439 | if (Idx == size()) { |
| 440 | push_back(V); |
| 441 | return; |
| 442 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 443 | |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 444 | if (Idx >= size()) |
| 445 | resize(Idx+1); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 446 | |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 447 | WeakVH &OldV = MDValuePtrs[Idx]; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 448 | if (!OldV) { |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 449 | OldV = V; |
| 450 | return; |
| 451 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 452 | |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 453 | // If there was a forward reference to this value, replace it. |
Dan Gohman | 16a5d98 | 2010-08-20 22:02:26 +0000 | [diff] [blame] | 454 | MDNode *PrevVal = cast<MDNode>(OldV); |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 455 | OldV->replaceAllUsesWith(V); |
Dan Gohman | 16a5d98 | 2010-08-20 22:02:26 +0000 | [diff] [blame] | 456 | MDNode::deleteTemporary(PrevVal); |
Devang Patel | 116b4a0 | 2009-09-03 01:38:02 +0000 | [diff] [blame] | 457 | // Deleting PrevVal sets Idx value in MDValuePtrs to null. Set new |
| 458 | // value for Idx. |
| 459 | MDValuePtrs[Idx] = V; |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | Value *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) { |
| 463 | if (Idx >= size()) |
| 464 | resize(Idx + 1); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 465 | |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 466 | if (Value *V = MDValuePtrs[Idx]) { |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 467 | assert(V->getType()->isMetadataTy() && "Type mismatch in value table!"); |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 468 | return V; |
| 469 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 470 | |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 471 | // Create and return a placeholder, which will later be RAUW'd. |
Dmitri Gribenko | 3238fb7 | 2013-05-05 00:40:33 +0000 | [diff] [blame] | 472 | Value *V = MDNode::getTemporary(Context, None); |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 473 | MDValuePtrs[Idx] = V; |
| 474 | return V; |
| 475 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 476 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 477 | Type *BitcodeReader::getTypeByID(unsigned ID) { |
| 478 | // The type table size is always specified correctly. |
| 479 | if (ID >= TypeList.size()) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 480 | return nullptr; |
Derek Schuff | 206dddd | 2012-02-06 19:03:04 +0000 | [diff] [blame] | 481 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 482 | if (Type *Ty = TypeList[ID]) |
| 483 | return Ty; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 484 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 485 | // If we have a forward reference, the only possible case is when it is to a |
| 486 | // named struct. Just create a placeholder for now. |
Chris Lattner | 335d399 | 2011-08-12 18:06:37 +0000 | [diff] [blame] | 487 | return TypeList[ID] = StructType::create(Context); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 490 | |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 491 | //===----------------------------------------------------------------------===// |
| 492 | // Functions for parsing blocks from the bitcode file |
| 493 | //===----------------------------------------------------------------------===// |
| 494 | |
Bill Wendling | 56aeccc | 2013-02-04 23:32:23 +0000 | [diff] [blame] | 495 | |
| 496 | /// \brief This fills an AttrBuilder object with the LLVM attributes that have |
| 497 | /// been decoded from the given integer. This function must stay in sync with |
| 498 | /// 'encodeLLVMAttributesForBitcode'. |
| 499 | static void decodeLLVMAttributesForBitcode(AttrBuilder &B, |
| 500 | uint64_t EncodedAttrs) { |
| 501 | // FIXME: Remove in 4.0. |
| 502 | |
| 503 | // The alignment is stored as a 16-bit raw value from bits 31--16. We shift |
| 504 | // the bits above 31 down by 11 bits. |
| 505 | unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16; |
| 506 | assert((!Alignment || isPowerOf2_32(Alignment)) && |
| 507 | "Alignment must be a power of two."); |
| 508 | |
| 509 | if (Alignment) |
| 510 | B.addAlignmentAttr(Alignment); |
Kostya Serebryany | d688bab | 2013-02-11 08:13:54 +0000 | [diff] [blame] | 511 | B.addRawValue(((EncodedAttrs & (0xfffffULL << 32)) >> 11) | |
Bill Wendling | 56aeccc | 2013-02-04 23:32:23 +0000 | [diff] [blame] | 512 | (EncodedAttrs & 0xffff)); |
| 513 | } |
| 514 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 515 | std::error_code BitcodeReader::ParseAttributeBlock() { |
Chris Lattner | 982ec1e | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 516 | if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 517 | return Error(BitcodeError::InvalidRecord); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 518 | |
Devang Patel | a05633e | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 519 | if (!MAttributes.empty()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 520 | return Error(BitcodeError::InvalidMultipleBlocks); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 521 | |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 522 | SmallVector<uint64_t, 64> Record; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 523 | |
Bill Wendling | 71173cb | 2013-01-27 00:36:48 +0000 | [diff] [blame] | 524 | SmallVector<AttributeSet, 8> Attrs; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 525 | |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 526 | // Read all the records. |
| 527 | while (1) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 528 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 529 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 530 | switch (Entry.Kind) { |
| 531 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 532 | case BitstreamEntry::Error: |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 533 | return Error(BitcodeError::MalformedBlock); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 534 | case BitstreamEntry::EndBlock: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 535 | return std::error_code(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 536 | case BitstreamEntry::Record: |
| 537 | // The interesting case. |
| 538 | break; |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 539 | } |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 540 | |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 541 | // Read a record. |
| 542 | Record.clear(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 543 | switch (Stream.readRecord(Entry.ID, Record)) { |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 544 | default: // Default behavior: ignore. |
| 545 | break; |
Bill Wendling | 56aeccc | 2013-02-04 23:32:23 +0000 | [diff] [blame] | 546 | case bitc::PARAMATTR_CODE_ENTRY_OLD: { // ENTRY: [paramidx0, attr0, ...] |
| 547 | // FIXME: Remove in 4.0. |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 548 | if (Record.size() & 1) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 549 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 550 | |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 551 | for (unsigned i = 0, e = Record.size(); i != e; i += 2) { |
Bill Wendling | 60011b8 | 2013-01-29 01:43:29 +0000 | [diff] [blame] | 552 | AttrBuilder B; |
Bill Wendling | 56aeccc | 2013-02-04 23:32:23 +0000 | [diff] [blame] | 553 | decodeLLVMAttributesForBitcode(B, Record[i+1]); |
Bill Wendling | 60011b8 | 2013-01-29 01:43:29 +0000 | [diff] [blame] | 554 | Attrs.push_back(AttributeSet::get(Context, Record[i], B)); |
Devang Patel | a05633e | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 555 | } |
Devang Patel | a05633e | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 556 | |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 557 | MAttributes.push_back(AttributeSet::get(Context, Attrs)); |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 558 | Attrs.clear(); |
| 559 | break; |
| 560 | } |
Bill Wendling | 0dc0891 | 2013-02-12 08:13:50 +0000 | [diff] [blame] | 561 | case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [attrgrp0, attrgrp1, ...] |
| 562 | for (unsigned i = 0, e = Record.size(); i != e; ++i) |
| 563 | Attrs.push_back(MAttributeGroups[Record[i]]); |
| 564 | |
| 565 | MAttributes.push_back(AttributeSet::get(Context, Attrs)); |
| 566 | Attrs.clear(); |
| 567 | break; |
| 568 | } |
Duncan Sands | 04eb67e | 2007-11-20 14:09:29 +0000 | [diff] [blame] | 569 | } |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 573 | // Returns Attribute::None on unrecognized codes. |
| 574 | static Attribute::AttrKind GetAttrFromCode(uint64_t Code) { |
| 575 | switch (Code) { |
| 576 | default: |
| 577 | return Attribute::None; |
| 578 | case bitc::ATTR_KIND_ALIGNMENT: |
| 579 | return Attribute::Alignment; |
| 580 | case bitc::ATTR_KIND_ALWAYS_INLINE: |
| 581 | return Attribute::AlwaysInline; |
| 582 | case bitc::ATTR_KIND_BUILTIN: |
| 583 | return Attribute::Builtin; |
| 584 | case bitc::ATTR_KIND_BY_VAL: |
| 585 | return Attribute::ByVal; |
Reid Kleckner | a534a38 | 2013-12-19 02:14:12 +0000 | [diff] [blame] | 586 | case bitc::ATTR_KIND_IN_ALLOCA: |
| 587 | return Attribute::InAlloca; |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 588 | case bitc::ATTR_KIND_COLD: |
| 589 | return Attribute::Cold; |
| 590 | case bitc::ATTR_KIND_INLINE_HINT: |
| 591 | return Attribute::InlineHint; |
| 592 | case bitc::ATTR_KIND_IN_REG: |
| 593 | return Attribute::InReg; |
Tom Roeder | 44cb65f | 2014-06-05 19:29:43 +0000 | [diff] [blame] | 594 | case bitc::ATTR_KIND_JUMP_TABLE: |
| 595 | return Attribute::JumpTable; |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 596 | case bitc::ATTR_KIND_MIN_SIZE: |
| 597 | return Attribute::MinSize; |
| 598 | case bitc::ATTR_KIND_NAKED: |
| 599 | return Attribute::Naked; |
| 600 | case bitc::ATTR_KIND_NEST: |
| 601 | return Attribute::Nest; |
| 602 | case bitc::ATTR_KIND_NO_ALIAS: |
| 603 | return Attribute::NoAlias; |
| 604 | case bitc::ATTR_KIND_NO_BUILTIN: |
| 605 | return Attribute::NoBuiltin; |
| 606 | case bitc::ATTR_KIND_NO_CAPTURE: |
| 607 | return Attribute::NoCapture; |
| 608 | case bitc::ATTR_KIND_NO_DUPLICATE: |
| 609 | return Attribute::NoDuplicate; |
| 610 | case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT: |
| 611 | return Attribute::NoImplicitFloat; |
| 612 | case bitc::ATTR_KIND_NO_INLINE: |
| 613 | return Attribute::NoInline; |
| 614 | case bitc::ATTR_KIND_NON_LAZY_BIND: |
| 615 | return Attribute::NonLazyBind; |
Nick Lewycky | d52b152 | 2014-05-20 01:23:40 +0000 | [diff] [blame] | 616 | case bitc::ATTR_KIND_NON_NULL: |
| 617 | return Attribute::NonNull; |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 618 | case bitc::ATTR_KIND_DEREFERENCEABLE: |
| 619 | return Attribute::Dereferenceable; |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 620 | case bitc::ATTR_KIND_NO_RED_ZONE: |
| 621 | return Attribute::NoRedZone; |
| 622 | case bitc::ATTR_KIND_NO_RETURN: |
| 623 | return Attribute::NoReturn; |
| 624 | case bitc::ATTR_KIND_NO_UNWIND: |
| 625 | return Attribute::NoUnwind; |
| 626 | case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE: |
| 627 | return Attribute::OptimizeForSize; |
| 628 | case bitc::ATTR_KIND_OPTIMIZE_NONE: |
| 629 | return Attribute::OptimizeNone; |
| 630 | case bitc::ATTR_KIND_READ_NONE: |
| 631 | return Attribute::ReadNone; |
| 632 | case bitc::ATTR_KIND_READ_ONLY: |
| 633 | return Attribute::ReadOnly; |
| 634 | case bitc::ATTR_KIND_RETURNED: |
| 635 | return Attribute::Returned; |
| 636 | case bitc::ATTR_KIND_RETURNS_TWICE: |
| 637 | return Attribute::ReturnsTwice; |
| 638 | case bitc::ATTR_KIND_S_EXT: |
| 639 | return Attribute::SExt; |
| 640 | case bitc::ATTR_KIND_STACK_ALIGNMENT: |
| 641 | return Attribute::StackAlignment; |
| 642 | case bitc::ATTR_KIND_STACK_PROTECT: |
| 643 | return Attribute::StackProtect; |
| 644 | case bitc::ATTR_KIND_STACK_PROTECT_REQ: |
| 645 | return Attribute::StackProtectReq; |
| 646 | case bitc::ATTR_KIND_STACK_PROTECT_STRONG: |
| 647 | return Attribute::StackProtectStrong; |
| 648 | case bitc::ATTR_KIND_STRUCT_RET: |
| 649 | return Attribute::StructRet; |
| 650 | case bitc::ATTR_KIND_SANITIZE_ADDRESS: |
| 651 | return Attribute::SanitizeAddress; |
| 652 | case bitc::ATTR_KIND_SANITIZE_THREAD: |
| 653 | return Attribute::SanitizeThread; |
| 654 | case bitc::ATTR_KIND_SANITIZE_MEMORY: |
| 655 | return Attribute::SanitizeMemory; |
| 656 | case bitc::ATTR_KIND_UW_TABLE: |
| 657 | return Attribute::UWTable; |
| 658 | case bitc::ATTR_KIND_Z_EXT: |
| 659 | return Attribute::ZExt; |
| 660 | } |
| 661 | } |
| 662 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 663 | std::error_code BitcodeReader::ParseAttrKind(uint64_t Code, |
| 664 | Attribute::AttrKind *Kind) { |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 665 | *Kind = GetAttrFromCode(Code); |
| 666 | if (*Kind == Attribute::None) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 667 | return Error(BitcodeError::InvalidValue); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 668 | return std::error_code(); |
Tobias Grosser | 0a8e12f | 2013-07-26 04:16:55 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 671 | std::error_code BitcodeReader::ParseAttributeGroupBlock() { |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 672 | if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 673 | return Error(BitcodeError::InvalidRecord); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 674 | |
| 675 | if (!MAttributeGroups.empty()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 676 | return Error(BitcodeError::InvalidMultipleBlocks); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 677 | |
| 678 | SmallVector<uint64_t, 64> Record; |
| 679 | |
| 680 | // Read all the records. |
| 681 | while (1) { |
| 682 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| 683 | |
| 684 | switch (Entry.Kind) { |
| 685 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 686 | case BitstreamEntry::Error: |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 687 | return Error(BitcodeError::MalformedBlock); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 688 | case BitstreamEntry::EndBlock: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 689 | return std::error_code(); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 690 | case BitstreamEntry::Record: |
| 691 | // The interesting case. |
| 692 | break; |
| 693 | } |
| 694 | |
| 695 | // Read a record. |
| 696 | Record.clear(); |
| 697 | switch (Stream.readRecord(Entry.ID, Record)) { |
| 698 | default: // Default behavior: ignore. |
| 699 | break; |
| 700 | case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...] |
| 701 | if (Record.size() < 3) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 702 | return Error(BitcodeError::InvalidRecord); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 703 | |
Bill Wendling | e46707e | 2013-02-11 22:32:29 +0000 | [diff] [blame] | 704 | uint64_t GrpID = Record[0]; |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 705 | uint64_t Idx = Record[1]; // Index of the object this attribute refers to. |
| 706 | |
| 707 | AttrBuilder B; |
| 708 | for (unsigned i = 2, e = Record.size(); i != e; ++i) { |
| 709 | if (Record[i] == 0) { // Enum attribute |
Tobias Grosser | 0a8e12f | 2013-07-26 04:16:55 +0000 | [diff] [blame] | 710 | Attribute::AttrKind Kind; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 711 | if (std::error_code EC = ParseAttrKind(Record[++i], &Kind)) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 712 | return EC; |
Tobias Grosser | 0a8e12f | 2013-07-26 04:16:55 +0000 | [diff] [blame] | 713 | |
| 714 | B.addAttribute(Kind); |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 715 | } else if (Record[i] == 1) { // Integer attribute |
Tobias Grosser | 0a8e12f | 2013-07-26 04:16:55 +0000 | [diff] [blame] | 716 | Attribute::AttrKind Kind; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 717 | if (std::error_code EC = ParseAttrKind(Record[++i], &Kind)) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 718 | return EC; |
Tobias Grosser | 0a8e12f | 2013-07-26 04:16:55 +0000 | [diff] [blame] | 719 | if (Kind == Attribute::Alignment) |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 720 | B.addAlignmentAttr(Record[++i]); |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 721 | else if (Kind == Attribute::StackAlignment) |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 722 | B.addStackAlignmentAttr(Record[++i]); |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 723 | else if (Kind == Attribute::Dereferenceable) |
| 724 | B.addDereferenceableAttr(Record[++i]); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 725 | } else { // String attribute |
Bill Wendling | e46707e | 2013-02-11 22:32:29 +0000 | [diff] [blame] | 726 | assert((Record[i] == 3 || Record[i] == 4) && |
| 727 | "Invalid attribute group entry"); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 728 | bool HasValue = (Record[i++] == 4); |
| 729 | SmallString<64> KindStr; |
| 730 | SmallString<64> ValStr; |
| 731 | |
| 732 | while (Record[i] != 0 && i != e) |
| 733 | KindStr += Record[i++]; |
Bill Wendling | e46707e | 2013-02-11 22:32:29 +0000 | [diff] [blame] | 734 | assert(Record[i] == 0 && "Kind string not null terminated"); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 735 | |
| 736 | if (HasValue) { |
| 737 | // Has a value associated with it. |
Bill Wendling | e46707e | 2013-02-11 22:32:29 +0000 | [diff] [blame] | 738 | ++i; // Skip the '0' that terminates the "kind" string. |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 739 | while (Record[i] != 0 && i != e) |
| 740 | ValStr += Record[i++]; |
Bill Wendling | e46707e | 2013-02-11 22:32:29 +0000 | [diff] [blame] | 741 | assert(Record[i] == 0 && "Value string not null terminated"); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | B.addAttribute(KindStr.str(), ValStr.str()); |
| 745 | } |
| 746 | } |
| 747 | |
Bill Wendling | e46707e | 2013-02-11 22:32:29 +0000 | [diff] [blame] | 748 | MAttributeGroups[GrpID] = AttributeSet::get(Context, Idx, B); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 749 | break; |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 755 | std::error_code BitcodeReader::ParseTypeTable() { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 756 | if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 757 | return Error(BitcodeError::InvalidRecord); |
Derek Schuff | 206dddd | 2012-02-06 19:03:04 +0000 | [diff] [blame] | 758 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 759 | return ParseTypeTableBody(); |
| 760 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 761 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 762 | std::error_code BitcodeReader::ParseTypeTableBody() { |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 763 | if (!TypeList.empty()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 764 | return Error(BitcodeError::InvalidMultipleBlocks); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 765 | |
| 766 | SmallVector<uint64_t, 64> Record; |
| 767 | unsigned NumRecords = 0; |
| 768 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 769 | SmallString<64> TypeName; |
Derek Schuff | 206dddd | 2012-02-06 19:03:04 +0000 | [diff] [blame] | 770 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 771 | // Read all the records for this type table. |
| 772 | while (1) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 773 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 774 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 775 | switch (Entry.Kind) { |
| 776 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 777 | case BitstreamEntry::Error: |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 778 | return Error(BitcodeError::MalformedBlock); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 779 | case BitstreamEntry::EndBlock: |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 780 | if (NumRecords != TypeList.size()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 781 | return Error(BitcodeError::MalformedBlock); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 782 | return std::error_code(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 783 | case BitstreamEntry::Record: |
| 784 | // The interesting case. |
| 785 | break; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 786 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 787 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 788 | // Read a record. |
| 789 | Record.clear(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 790 | Type *ResultTy = nullptr; |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 791 | switch (Stream.readRecord(Entry.ID, Record)) { |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 792 | default: |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 793 | return Error(BitcodeError::InvalidValue); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 794 | case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries] |
| 795 | // TYPE_CODE_NUMENTRY contains a count of the number of types in the |
| 796 | // type list. This allows us to reserve space. |
| 797 | if (Record.size() < 1) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 798 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 799 | TypeList.resize(Record[0]); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 800 | continue; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 801 | case bitc::TYPE_CODE_VOID: // VOID |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 802 | ResultTy = Type::getVoidTy(Context); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 803 | break; |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 804 | case bitc::TYPE_CODE_HALF: // HALF |
| 805 | ResultTy = Type::getHalfTy(Context); |
| 806 | break; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 807 | case bitc::TYPE_CODE_FLOAT: // FLOAT |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 808 | ResultTy = Type::getFloatTy(Context); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 809 | break; |
| 810 | case bitc::TYPE_CODE_DOUBLE: // DOUBLE |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 811 | ResultTy = Type::getDoubleTy(Context); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 812 | break; |
Dale Johannesen | ff4c3be | 2007-08-03 01:03:46 +0000 | [diff] [blame] | 813 | case bitc::TYPE_CODE_X86_FP80: // X86_FP80 |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 814 | ResultTy = Type::getX86_FP80Ty(Context); |
Dale Johannesen | ff4c3be | 2007-08-03 01:03:46 +0000 | [diff] [blame] | 815 | break; |
| 816 | case bitc::TYPE_CODE_FP128: // FP128 |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 817 | ResultTy = Type::getFP128Ty(Context); |
Dale Johannesen | ff4c3be | 2007-08-03 01:03:46 +0000 | [diff] [blame] | 818 | break; |
| 819 | case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128 |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 820 | ResultTy = Type::getPPC_FP128Ty(Context); |
Dale Johannesen | ff4c3be | 2007-08-03 01:03:46 +0000 | [diff] [blame] | 821 | break; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 822 | case bitc::TYPE_CODE_LABEL: // LABEL |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 823 | ResultTy = Type::getLabelTy(Context); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 824 | break; |
Nick Lewycky | adbc284 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 825 | case bitc::TYPE_CODE_METADATA: // METADATA |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 826 | ResultTy = Type::getMetadataTy(Context); |
Nick Lewycky | adbc284 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 827 | break; |
Dale Johannesen | baa5d04 | 2010-09-10 20:55:01 +0000 | [diff] [blame] | 828 | case bitc::TYPE_CODE_X86_MMX: // X86_MMX |
| 829 | ResultTy = Type::getX86_MMXTy(Context); |
| 830 | break; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 831 | case bitc::TYPE_CODE_INTEGER: // INTEGER: [width] |
| 832 | if (Record.size() < 1) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 833 | return Error(BitcodeError::InvalidRecord); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 834 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 835 | ResultTy = IntegerType::get(Context, Record[0]); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 836 | break; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 837 | case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 838 | // [pointee type, address space] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 839 | if (Record.size() < 1) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 840 | return Error(BitcodeError::InvalidRecord); |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 841 | unsigned AddressSpace = 0; |
| 842 | if (Record.size() == 2) |
| 843 | AddressSpace = Record[1]; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 844 | ResultTy = getTypeByID(Record[0]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 845 | if (!ResultTy) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 846 | return Error(BitcodeError::InvalidType); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 847 | ResultTy = PointerType::get(ResultTy, AddressSpace); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 848 | break; |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 849 | } |
Nuno Lopes | 561dae0 | 2012-05-23 15:19:39 +0000 | [diff] [blame] | 850 | case bitc::TYPE_CODE_FUNCTION_OLD: { |
| 851 | // FIXME: attrid is dead, remove it in LLVM 4.0 |
| 852 | // FUNCTION: [vararg, attrid, retty, paramty x N] |
| 853 | if (Record.size() < 3) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 854 | return Error(BitcodeError::InvalidRecord); |
Nuno Lopes | 561dae0 | 2012-05-23 15:19:39 +0000 | [diff] [blame] | 855 | SmallVector<Type*, 8> ArgTys; |
| 856 | for (unsigned i = 3, e = Record.size(); i != e; ++i) { |
| 857 | if (Type *T = getTypeByID(Record[i])) |
| 858 | ArgTys.push_back(T); |
| 859 | else |
| 860 | break; |
| 861 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 862 | |
Nuno Lopes | 561dae0 | 2012-05-23 15:19:39 +0000 | [diff] [blame] | 863 | ResultTy = getTypeByID(Record[2]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 864 | if (!ResultTy || ArgTys.size() < Record.size()-3) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 865 | return Error(BitcodeError::InvalidType); |
Nuno Lopes | 561dae0 | 2012-05-23 15:19:39 +0000 | [diff] [blame] | 866 | |
| 867 | ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); |
| 868 | break; |
| 869 | } |
Chad Rosier | 9589872 | 2011-11-03 00:14:01 +0000 | [diff] [blame] | 870 | case bitc::TYPE_CODE_FUNCTION: { |
| 871 | // FUNCTION: [vararg, retty, paramty x N] |
| 872 | if (Record.size() < 2) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 873 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | cc3aaf1 | 2012-01-27 03:15:49 +0000 | [diff] [blame] | 874 | SmallVector<Type*, 8> ArgTys; |
Chad Rosier | 9589872 | 2011-11-03 00:14:01 +0000 | [diff] [blame] | 875 | for (unsigned i = 2, e = Record.size(); i != e; ++i) { |
| 876 | if (Type *T = getTypeByID(Record[i])) |
| 877 | ArgTys.push_back(T); |
| 878 | else |
| 879 | break; |
| 880 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 881 | |
Chad Rosier | 9589872 | 2011-11-03 00:14:01 +0000 | [diff] [blame] | 882 | ResultTy = getTypeByID(Record[1]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 883 | if (!ResultTy || ArgTys.size() < Record.size()-2) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 884 | return Error(BitcodeError::InvalidType); |
Chad Rosier | 9589872 | 2011-11-03 00:14:01 +0000 | [diff] [blame] | 885 | |
| 886 | ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); |
| 887 | break; |
| 888 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 889 | case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N] |
Chris Lattner | 3c5616e | 2007-05-06 08:21:50 +0000 | [diff] [blame] | 890 | if (Record.size() < 1) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 891 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | cc3aaf1 | 2012-01-27 03:15:49 +0000 | [diff] [blame] | 892 | SmallVector<Type*, 8> EltTys; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 893 | for (unsigned i = 1, e = Record.size(); i != e; ++i) { |
| 894 | if (Type *T = getTypeByID(Record[i])) |
| 895 | EltTys.push_back(T); |
| 896 | else |
| 897 | break; |
| 898 | } |
| 899 | if (EltTys.size() != Record.size()-1) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 900 | return Error(BitcodeError::InvalidType); |
Owen Anderson | 03cb69f | 2009-08-05 23:16:16 +0000 | [diff] [blame] | 901 | ResultTy = StructType::get(Context, EltTys, Record[0]); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 902 | break; |
| 903 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 904 | case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N] |
| 905 | if (ConvertToString(Record, 0, TypeName)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 906 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 907 | continue; |
| 908 | |
| 909 | case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N] |
| 910 | if (Record.size() < 1) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 911 | return Error(BitcodeError::InvalidRecord); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 912 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 913 | if (NumRecords >= TypeList.size()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 914 | return Error(BitcodeError::InvalidTYPETable); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 915 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 916 | // Check to see if this was forward referenced, if so fill in the temp. |
| 917 | StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); |
| 918 | if (Res) { |
| 919 | Res->setName(TypeName); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 920 | TypeList[NumRecords] = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 921 | } else // Otherwise, create a new struct. |
Chris Lattner | 335d399 | 2011-08-12 18:06:37 +0000 | [diff] [blame] | 922 | Res = StructType::create(Context, TypeName); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 923 | TypeName.clear(); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 924 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 925 | SmallVector<Type*, 8> EltTys; |
| 926 | for (unsigned i = 1, e = Record.size(); i != e; ++i) { |
| 927 | if (Type *T = getTypeByID(Record[i])) |
| 928 | EltTys.push_back(T); |
| 929 | else |
| 930 | break; |
| 931 | } |
| 932 | if (EltTys.size() != Record.size()-1) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 933 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 934 | Res->setBody(EltTys, Record[0]); |
| 935 | ResultTy = Res; |
| 936 | break; |
| 937 | } |
| 938 | case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: [] |
| 939 | if (Record.size() != 1) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 940 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 941 | |
| 942 | if (NumRecords >= TypeList.size()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 943 | return Error(BitcodeError::InvalidTYPETable); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 944 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 945 | // Check to see if this was forward referenced, if so fill in the temp. |
| 946 | StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); |
| 947 | if (Res) { |
| 948 | Res->setName(TypeName); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 949 | TypeList[NumRecords] = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 950 | } else // Otherwise, create a new struct with no body. |
Chris Lattner | 335d399 | 2011-08-12 18:06:37 +0000 | [diff] [blame] | 951 | Res = StructType::create(Context, TypeName); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 952 | TypeName.clear(); |
| 953 | ResultTy = Res; |
| 954 | break; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 955 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 956 | case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty] |
| 957 | if (Record.size() < 2) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 958 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 959 | if ((ResultTy = getTypeByID(Record[1]))) |
| 960 | ResultTy = ArrayType::get(ResultTy, Record[0]); |
| 961 | else |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 962 | return Error(BitcodeError::InvalidType); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 963 | break; |
| 964 | case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] |
| 965 | if (Record.size() < 2) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 966 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 967 | if ((ResultTy = getTypeByID(Record[1]))) |
| 968 | ResultTy = VectorType::get(ResultTy, Record[0]); |
| 969 | else |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 970 | return Error(BitcodeError::InvalidType); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 971 | break; |
| 972 | } |
| 973 | |
| 974 | if (NumRecords >= TypeList.size()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 975 | return Error(BitcodeError::InvalidTYPETable); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 976 | assert(ResultTy && "Didn't read a type?"); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 977 | assert(!TypeList[NumRecords] && "Already read type?"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 978 | TypeList[NumRecords++] = ResultTy; |
| 979 | } |
| 980 | } |
| 981 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 982 | std::error_code BitcodeReader::ParseValueSymbolTable() { |
Chris Lattner | 982ec1e | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 983 | if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 984 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 985 | |
| 986 | SmallVector<uint64_t, 64> Record; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 987 | |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 988 | // Read all the records for this value table. |
| 989 | SmallString<128> ValueName; |
| 990 | while (1) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 991 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 992 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 993 | switch (Entry.Kind) { |
| 994 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 995 | case BitstreamEntry::Error: |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 996 | return Error(BitcodeError::MalformedBlock); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 997 | case BitstreamEntry::EndBlock: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 998 | return std::error_code(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 999 | case BitstreamEntry::Record: |
| 1000 | // The interesting case. |
| 1001 | break; |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1002 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1003 | |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1004 | // Read a record. |
| 1005 | Record.clear(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1006 | switch (Stream.readRecord(Entry.ID, Record)) { |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1007 | default: // Default behavior: unknown type. |
| 1008 | break; |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 1009 | case bitc::VST_CODE_ENTRY: { // VST_ENTRY: [valueid, namechar x N] |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1010 | if (ConvertToString(Record, 1, ValueName)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1011 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1012 | unsigned ValueID = Record[0]; |
Karthik Bhat | 82540e9 | 2014-03-27 12:08:23 +0000 | [diff] [blame] | 1013 | if (ValueID >= ValueList.size() || !ValueList[ValueID]) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1014 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1015 | Value *V = ValueList[ValueID]; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1016 | |
Daniel Dunbar | d786b51 | 2009-07-26 00:34:27 +0000 | [diff] [blame] | 1017 | V->setName(StringRef(ValueName.data(), ValueName.size())); |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1018 | ValueName.clear(); |
| 1019 | break; |
Reid Spencer | dea02bd | 2007-05-04 01:43:33 +0000 | [diff] [blame] | 1020 | } |
Bill Wendling | 35a9c3c | 2011-04-10 23:18:04 +0000 | [diff] [blame] | 1021 | case bitc::VST_CODE_BBENTRY: { |
Chris Lattner | 6be58c6 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 1022 | if (ConvertToString(Record, 1, ValueName)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1023 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 6be58c6 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 1024 | BasicBlock *BB = getBasicBlock(Record[0]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1025 | if (!BB) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1026 | return Error(BitcodeError::InvalidRecord); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1027 | |
Daniel Dunbar | d786b51 | 2009-07-26 00:34:27 +0000 | [diff] [blame] | 1028 | BB->setName(StringRef(ValueName.data(), ValueName.size())); |
Chris Lattner | 6be58c6 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 1029 | ValueName.clear(); |
| 1030 | break; |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1031 | } |
Reid Spencer | dea02bd | 2007-05-04 01:43:33 +0000 | [diff] [blame] | 1032 | } |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1033 | } |
| 1034 | } |
| 1035 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1036 | std::error_code BitcodeReader::ParseMetadata() { |
Devang Patel | 8992323 | 2010-01-11 18:52:33 +0000 | [diff] [blame] | 1037 | unsigned NextMDValueNo = MDValueList.size(); |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1038 | |
| 1039 | if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1040 | return Error(BitcodeError::InvalidRecord); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1041 | |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1042 | SmallVector<uint64_t, 64> Record; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1043 | |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1044 | // Read all the records. |
| 1045 | while (1) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1046 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 1047 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1048 | switch (Entry.Kind) { |
| 1049 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 1050 | case BitstreamEntry::Error: |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1051 | return Error(BitcodeError::MalformedBlock); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1052 | case BitstreamEntry::EndBlock: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1053 | return std::error_code(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1054 | case BitstreamEntry::Record: |
| 1055 | // The interesting case. |
| 1056 | break; |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1057 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1058 | |
Victor Hernandez | b8fd152 | 2010-01-10 07:14:18 +0000 | [diff] [blame] | 1059 | bool IsFunctionLocal = false; |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1060 | // Read a record. |
| 1061 | Record.clear(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1062 | unsigned Code = Stream.readRecord(Entry.ID, Record); |
Dan Gohman | bbcd04d | 2010-09-13 18:00:48 +0000 | [diff] [blame] | 1063 | switch (Code) { |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1064 | default: // Default behavior: ignore. |
| 1065 | break; |
Devang Patel | 27c87ff | 2009-07-29 22:34:41 +0000 | [diff] [blame] | 1066 | case bitc::METADATA_NAME: { |
Chris Lattner | 8d14053 | 2013-01-20 02:54:05 +0000 | [diff] [blame] | 1067 | // Read name of the named metadata. |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 1068 | SmallString<8> Name(Record.begin(), Record.end()); |
Devang Patel | 27c87ff | 2009-07-29 22:34:41 +0000 | [diff] [blame] | 1069 | Record.clear(); |
| 1070 | Code = Stream.ReadCode(); |
| 1071 | |
Chris Lattner | b877855 | 2011-06-17 17:50:30 +0000 | [diff] [blame] | 1072 | // METADATA_NAME is always followed by METADATA_NAMED_NODE. |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1073 | unsigned NextBitCode = Stream.readRecord(Code, Record); |
Chris Lattner | b877855 | 2011-06-17 17:50:30 +0000 | [diff] [blame] | 1074 | assert(NextBitCode == bitc::METADATA_NAMED_NODE); (void)NextBitCode; |
Devang Patel | 27c87ff | 2009-07-29 22:34:41 +0000 | [diff] [blame] | 1075 | |
| 1076 | // Read named metadata elements. |
| 1077 | unsigned Size = Record.size(); |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 1078 | NamedMDNode *NMD = TheModule->getOrInsertNamedMetadata(Name); |
Devang Patel | 27c87ff | 2009-07-29 22:34:41 +0000 | [diff] [blame] | 1079 | for (unsigned i = 0; i != Size; ++i) { |
Karthik Bhat | 82540e9 | 2014-03-27 12:08:23 +0000 | [diff] [blame] | 1080 | MDNode *MD = dyn_cast_or_null<MDNode>(MDValueList.getValueFwdRef(Record[i])); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1081 | if (!MD) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1082 | return Error(BitcodeError::InvalidRecord); |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 1083 | NMD->addOperand(MD); |
Devang Patel | 27c87ff | 2009-07-29 22:34:41 +0000 | [diff] [blame] | 1084 | } |
Devang Patel | 27c87ff | 2009-07-29 22:34:41 +0000 | [diff] [blame] | 1085 | break; |
| 1086 | } |
Chris Lattner | b877855 | 2011-06-17 17:50:30 +0000 | [diff] [blame] | 1087 | case bitc::METADATA_FN_NODE: |
Victor Hernandez | b8fd152 | 2010-01-10 07:14:18 +0000 | [diff] [blame] | 1088 | IsFunctionLocal = true; |
| 1089 | // fall-through |
Chris Lattner | b877855 | 2011-06-17 17:50:30 +0000 | [diff] [blame] | 1090 | case bitc::METADATA_NODE: { |
Dan Gohman | 1e0213a | 2010-07-13 19:33:27 +0000 | [diff] [blame] | 1091 | if (Record.size() % 2 == 1) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1092 | return Error(BitcodeError::InvalidRecord); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1093 | |
Devang Patel | e059ba6e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 1094 | unsigned Size = Record.size(); |
| 1095 | SmallVector<Value*, 8> Elts; |
| 1096 | for (unsigned i = 0; i != Size; i += 2) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1097 | Type *Ty = getTypeByID(Record[i]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1098 | if (!Ty) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1099 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 1100 | if (Ty->isMetadataTy()) |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 1101 | Elts.push_back(MDValueList.getValueFwdRef(Record[i+1])); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 1102 | else if (!Ty->isVoidTy()) |
Devang Patel | e059ba6e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 1103 | Elts.push_back(ValueList.getValueFwdRef(Record[i+1], Ty)); |
| 1104 | else |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1105 | Elts.push_back(nullptr); |
Devang Patel | e059ba6e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 1106 | } |
Jay Foad | 5514afe | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 1107 | Value *V = MDNode::getWhenValsUnresolved(Context, Elts, IsFunctionLocal); |
Victor Hernandez | b8fd152 | 2010-01-10 07:14:18 +0000 | [diff] [blame] | 1108 | IsFunctionLocal = false; |
Devang Patel | 8992323 | 2010-01-11 18:52:33 +0000 | [diff] [blame] | 1109 | MDValueList.AssignValue(V, NextMDValueNo++); |
Devang Patel | e059ba6e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 1110 | break; |
| 1111 | } |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1112 | case bitc::METADATA_STRING: { |
Eli Bendersky | 5d5e18d | 2014-06-25 15:41:00 +0000 | [diff] [blame] | 1113 | std::string String(Record.begin(), Record.end()); |
| 1114 | llvm::UpgradeMDStringConstant(String); |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 1115 | Value *V = MDString::get(Context, String); |
Devang Patel | 8992323 | 2010-01-11 18:52:33 +0000 | [diff] [blame] | 1116 | MDValueList.AssignValue(V, NextMDValueNo++); |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1117 | break; |
| 1118 | } |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 1119 | case bitc::METADATA_KIND: { |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 1120 | if (Record.size() < 2) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1121 | return Error(BitcodeError::InvalidRecord); |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 1122 | |
Devang Patel | b1a4477 | 2009-09-28 21:14:55 +0000 | [diff] [blame] | 1123 | unsigned Kind = Record[0]; |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 1124 | SmallString<8> Name(Record.begin()+1, Record.end()); |
| 1125 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 1126 | unsigned NewKind = TheModule->getMDKindID(Name.str()); |
Dan Gohman | 43aa8f0 | 2010-07-20 21:42:28 +0000 | [diff] [blame] | 1127 | if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1128 | return Error(BitcodeError::ConflictingMETADATA_KINDRecords); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 1129 | break; |
| 1130 | } |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1131 | } |
| 1132 | } |
| 1133 | } |
| 1134 | |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 1135 | /// decodeSignRotatedValue - Decode a signed value stored with the sign bit in |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 1136 | /// the LSB for dense VBR encoding. |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 1137 | uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) { |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 1138 | if ((V & 1) == 0) |
| 1139 | return V >> 1; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1140 | if (V != 1) |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 1141 | return -(V >> 1); |
| 1142 | // There is no such thing as -0 with integers. "-0" really means MININT. |
| 1143 | return 1ULL << 63; |
| 1144 | } |
| 1145 | |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1146 | /// ResolveGlobalAndAliasInits - Resolve all of the initializers for global |
| 1147 | /// values and aliases that we can. |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1148 | std::error_code BitcodeReader::ResolveGlobalAndAliasInits() { |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1149 | std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist; |
| 1150 | std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist; |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 1151 | std::vector<std::pair<Function*, unsigned> > FunctionPrefixWorklist; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1152 | |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1153 | GlobalInitWorklist.swap(GlobalInits); |
| 1154 | AliasInitWorklist.swap(AliasInits); |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 1155 | FunctionPrefixWorklist.swap(FunctionPrefixes); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1156 | |
| 1157 | while (!GlobalInitWorklist.empty()) { |
Chris Lattner | 831d420 | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 1158 | unsigned ValID = GlobalInitWorklist.back().second; |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1159 | if (ValID >= ValueList.size()) { |
| 1160 | // Not ready to resolve this yet, it requires something later in the file. |
Chris Lattner | 831d420 | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 1161 | GlobalInits.push_back(GlobalInitWorklist.back()); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1162 | } else { |
Karthik Bhat | 82540e9 | 2014-03-27 12:08:23 +0000 | [diff] [blame] | 1163 | if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1164 | GlobalInitWorklist.back().first->setInitializer(C); |
| 1165 | else |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1166 | return Error(BitcodeError::ExpectedConstant); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1167 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1168 | GlobalInitWorklist.pop_back(); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | while (!AliasInitWorklist.empty()) { |
| 1172 | unsigned ValID = AliasInitWorklist.back().second; |
| 1173 | if (ValID >= ValueList.size()) { |
| 1174 | AliasInits.push_back(AliasInitWorklist.back()); |
| 1175 | } else { |
Karthik Bhat | 82540e9 | 2014-03-27 12:08:23 +0000 | [diff] [blame] | 1176 | if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) |
Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 1177 | AliasInitWorklist.back().first->setAliasee(C); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1178 | else |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1179 | return Error(BitcodeError::ExpectedConstant); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1180 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1181 | AliasInitWorklist.pop_back(); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1182 | } |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 1183 | |
| 1184 | while (!FunctionPrefixWorklist.empty()) { |
| 1185 | unsigned ValID = FunctionPrefixWorklist.back().second; |
| 1186 | if (ValID >= ValueList.size()) { |
| 1187 | FunctionPrefixes.push_back(FunctionPrefixWorklist.back()); |
| 1188 | } else { |
Karthik Bhat | 82540e9 | 2014-03-27 12:08:23 +0000 | [diff] [blame] | 1189 | if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 1190 | FunctionPrefixWorklist.back().first->setPrefixData(C); |
| 1191 | else |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1192 | return Error(BitcodeError::ExpectedConstant); |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 1193 | } |
| 1194 | FunctionPrefixWorklist.pop_back(); |
| 1195 | } |
| 1196 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1197 | return std::error_code(); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 1200 | static APInt ReadWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) { |
| 1201 | SmallVector<uint64_t, 8> Words(Vals.size()); |
| 1202 | std::transform(Vals.begin(), Vals.end(), Words.begin(), |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 1203 | BitcodeReader::decodeSignRotatedValue); |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 1204 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 1205 | return APInt(TypeBits, Words); |
| 1206 | } |
| 1207 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1208 | std::error_code BitcodeReader::ParseConstants() { |
Chris Lattner | 982ec1e | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1209 | if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1210 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 1211 | |
| 1212 | SmallVector<uint64_t, 64> Record; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1213 | |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 1214 | // Read all the records for this value table. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1215 | Type *CurTy = Type::getInt32Ty(Context); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 1216 | unsigned NextCstNo = ValueList.size(); |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 1217 | while (1) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1218 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 1219 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1220 | switch (Entry.Kind) { |
| 1221 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 1222 | case BitstreamEntry::Error: |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1223 | return Error(BitcodeError::MalformedBlock); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1224 | case BitstreamEntry::EndBlock: |
| 1225 | if (NextCstNo != ValueList.size()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1226 | return Error(BitcodeError::InvalidConstantReference); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 1227 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1228 | // Once all the constants have been read, go through and resolve forward |
| 1229 | // references. |
| 1230 | ValueList.ResolveConstantForwardRefs(); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1231 | return std::error_code(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1232 | case BitstreamEntry::Record: |
| 1233 | // The interesting case. |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 1234 | break; |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 1235 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1236 | |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 1237 | // Read a record. |
| 1238 | Record.clear(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1239 | Value *V = nullptr; |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1240 | unsigned BitCode = Stream.readRecord(Entry.ID, Record); |
Dan Gohman | 0ebd696 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 1241 | switch (BitCode) { |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 1242 | default: // Default behavior: unknown constant |
| 1243 | case bitc::CST_CODE_UNDEF: // UNDEF |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1244 | V = UndefValue::get(CurTy); |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 1245 | break; |
| 1246 | case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid] |
| 1247 | if (Record.empty()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1248 | return Error(BitcodeError::InvalidRecord); |
Karthik Bhat | 82540e9 | 2014-03-27 12:08:23 +0000 | [diff] [blame] | 1249 | if (Record[0] >= TypeList.size() || !TypeList[Record[0]]) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1250 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 1251 | CurTy = TypeList[Record[0]]; |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 1252 | continue; // Skip the ValueList manipulation. |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 1253 | case bitc::CST_CODE_NULL: // NULL |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1254 | V = Constant::getNullValue(CurTy); |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 1255 | break; |
| 1256 | case bitc::CST_CODE_INTEGER: // INTEGER: [intval] |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1257 | if (!CurTy->isIntegerTy() || Record.empty()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1258 | return Error(BitcodeError::InvalidRecord); |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 1259 | V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0])); |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 1260 | break; |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 1261 | case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval] |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1262 | if (!CurTy->isIntegerTy() || Record.empty()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1263 | return Error(BitcodeError::InvalidRecord); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1264 | |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 1265 | APInt VInt = ReadWideAPInt(Record, |
| 1266 | cast<IntegerType>(CurTy)->getBitWidth()); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 1267 | V = ConstantInt::get(Context, VInt); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1268 | |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 1269 | break; |
| 1270 | } |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 1271 | case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval] |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 1272 | if (Record.empty()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1273 | return Error(BitcodeError::InvalidRecord); |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 1274 | if (CurTy->isHalfTy()) |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 1275 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf, |
| 1276 | APInt(16, (uint16_t)Record[0]))); |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 1277 | else if (CurTy->isFloatTy()) |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 1278 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle, |
| 1279 | APInt(32, (uint32_t)Record[0]))); |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 1280 | else if (CurTy->isDoubleTy()) |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 1281 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble, |
| 1282 | APInt(64, Record[0]))); |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 1283 | else if (CurTy->isX86_FP80Ty()) { |
Dale Johannesen | 93eefa0 | 2009-03-23 21:16:53 +0000 | [diff] [blame] | 1284 | // Bits are not stored the same way as a normal i80 APInt, compensate. |
| 1285 | uint64_t Rearrange[2]; |
| 1286 | Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16); |
| 1287 | Rearrange[1] = Record[0] >> 48; |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 1288 | V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended, |
| 1289 | APInt(80, Rearrange))); |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 1290 | } else if (CurTy->isFP128Ty()) |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 1291 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad, |
| 1292 | APInt(128, Record))); |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 1293 | else if (CurTy->isPPC_FP128Ty()) |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 1294 | V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble, |
| 1295 | APInt(128, Record))); |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 1296 | else |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1297 | V = UndefValue::get(CurTy); |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 1298 | break; |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 1299 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1300 | |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 1301 | case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number] |
| 1302 | if (Record.empty()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1303 | return Error(BitcodeError::InvalidRecord); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1304 | |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 1305 | unsigned Size = Record.size(); |
Chris Lattner | cc3aaf1 | 2012-01-27 03:15:49 +0000 | [diff] [blame] | 1306 | SmallVector<Constant*, 16> Elts; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1307 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1308 | if (StructType *STy = dyn_cast<StructType>(CurTy)) { |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 1309 | for (unsigned i = 0; i != Size; ++i) |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 1310 | Elts.push_back(ValueList.getConstantFwdRef(Record[i], |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 1311 | STy->getElementType(i))); |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 1312 | V = ConstantStruct::get(STy, Elts); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1313 | } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) { |
| 1314 | Type *EltTy = ATy->getElementType(); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 1315 | for (unsigned i = 0; i != Size; ++i) |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 1316 | Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 1317 | V = ConstantArray::get(ATy, Elts); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1318 | } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) { |
| 1319 | Type *EltTy = VTy->getElementType(); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 1320 | for (unsigned i = 0; i != Size; ++i) |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 1321 | Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1322 | V = ConstantVector::get(Elts); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 1323 | } else { |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1324 | V = UndefValue::get(CurTy); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 1325 | } |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 1326 | break; |
| 1327 | } |
Chris Lattner | bb8278a | 2012-02-05 02:41:35 +0000 | [diff] [blame] | 1328 | case bitc::CST_CODE_STRING: // STRING: [values] |
Chris Lattner | f25f710 | 2007-05-06 00:53:07 +0000 | [diff] [blame] | 1329 | case bitc::CST_CODE_CSTRING: { // CSTRING: [values] |
| 1330 | if (Record.empty()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1331 | return Error(BitcodeError::InvalidRecord); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1332 | |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 1333 | SmallString<16> Elts(Record.begin(), Record.end()); |
Chris Lattner | bb8278a | 2012-02-05 02:41:35 +0000 | [diff] [blame] | 1334 | V = ConstantDataArray::getString(Context, Elts, |
| 1335 | BitCode == bitc::CST_CODE_CSTRING); |
Chris Lattner | f25f710 | 2007-05-06 00:53:07 +0000 | [diff] [blame] | 1336 | break; |
| 1337 | } |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 1338 | case bitc::CST_CODE_DATA: {// DATA: [n x value] |
| 1339 | if (Record.empty()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1340 | return Error(BitcodeError::InvalidRecord); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1341 | |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 1342 | Type *EltTy = cast<SequentialType>(CurTy)->getElementType(); |
| 1343 | unsigned Size = Record.size(); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1344 | |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 1345 | if (EltTy->isIntegerTy(8)) { |
| 1346 | SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end()); |
| 1347 | if (isa<VectorType>(CurTy)) |
| 1348 | V = ConstantDataVector::get(Context, Elts); |
| 1349 | else |
| 1350 | V = ConstantDataArray::get(Context, Elts); |
| 1351 | } else if (EltTy->isIntegerTy(16)) { |
| 1352 | SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); |
| 1353 | if (isa<VectorType>(CurTy)) |
| 1354 | V = ConstantDataVector::get(Context, Elts); |
| 1355 | else |
| 1356 | V = ConstantDataArray::get(Context, Elts); |
| 1357 | } else if (EltTy->isIntegerTy(32)) { |
| 1358 | SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); |
| 1359 | if (isa<VectorType>(CurTy)) |
| 1360 | V = ConstantDataVector::get(Context, Elts); |
| 1361 | else |
| 1362 | V = ConstantDataArray::get(Context, Elts); |
| 1363 | } else if (EltTy->isIntegerTy(64)) { |
| 1364 | SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); |
| 1365 | if (isa<VectorType>(CurTy)) |
| 1366 | V = ConstantDataVector::get(Context, Elts); |
| 1367 | else |
| 1368 | V = ConstantDataArray::get(Context, Elts); |
| 1369 | } else if (EltTy->isFloatTy()) { |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 1370 | SmallVector<float, 16> Elts(Size); |
| 1371 | std::transform(Record.begin(), Record.end(), Elts.begin(), BitsToFloat); |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 1372 | if (isa<VectorType>(CurTy)) |
| 1373 | V = ConstantDataVector::get(Context, Elts); |
| 1374 | else |
| 1375 | V = ConstantDataArray::get(Context, Elts); |
| 1376 | } else if (EltTy->isDoubleTy()) { |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 1377 | SmallVector<double, 16> Elts(Size); |
| 1378 | std::transform(Record.begin(), Record.end(), Elts.begin(), |
| 1379 | BitsToDouble); |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 1380 | if (isa<VectorType>(CurTy)) |
| 1381 | V = ConstantDataVector::get(Context, Elts); |
| 1382 | else |
| 1383 | V = ConstantDataArray::get(Context, Elts); |
| 1384 | } else { |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1385 | return Error(BitcodeError::InvalidTypeForValue); |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 1386 | } |
| 1387 | break; |
| 1388 | } |
| 1389 | |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 1390 | case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval] |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1391 | if (Record.size() < 3) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1392 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 1393 | int Opc = GetDecodedBinaryOpcode(Record[0], CurTy); |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 1394 | if (Opc < 0) { |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1395 | V = UndefValue::get(CurTy); // Unknown binop. |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 1396 | } else { |
| 1397 | Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy); |
| 1398 | Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy); |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1399 | unsigned Flags = 0; |
| 1400 | if (Record.size() >= 4) { |
| 1401 | if (Opc == Instruction::Add || |
| 1402 | Opc == Instruction::Sub || |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 1403 | Opc == Instruction::Mul || |
| 1404 | Opc == Instruction::Shl) { |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1405 | if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP)) |
| 1406 | Flags |= OverflowingBinaryOperator::NoSignedWrap; |
| 1407 | if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) |
| 1408 | Flags |= OverflowingBinaryOperator::NoUnsignedWrap; |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 1409 | } else if (Opc == Instruction::SDiv || |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 1410 | Opc == Instruction::UDiv || |
| 1411 | Opc == Instruction::LShr || |
| 1412 | Opc == Instruction::AShr) { |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 1413 | if (Record[3] & (1 << bitc::PEO_EXACT)) |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1414 | Flags |= SDivOperator::IsExact; |
| 1415 | } |
| 1416 | } |
| 1417 | V = ConstantExpr::get(Opc, LHS, RHS, Flags); |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 1418 | } |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 1419 | break; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1420 | } |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 1421 | case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval] |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1422 | if (Record.size() < 3) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1423 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 1424 | int Opc = GetDecodedCastOpcode(Record[0]); |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 1425 | if (Opc < 0) { |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1426 | V = UndefValue::get(CurTy); // Unknown cast. |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 1427 | } else { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1428 | Type *OpTy = getTypeByID(Record[1]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1429 | if (!OpTy) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1430 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 1431 | Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy); |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 1432 | V = UpgradeBitCastExpr(Opc, Op, CurTy); |
| 1433 | if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy); |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 1434 | } |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 1435 | break; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1436 | } |
Dan Gohman | 1639c39 | 2009-07-27 21:53:46 +0000 | [diff] [blame] | 1437 | case bitc::CST_CODE_CE_INBOUNDS_GEP: |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 1438 | case bitc::CST_CODE_CE_GEP: { // CE_GEP: [n x operands] |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1439 | if (Record.size() & 1) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1440 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 1441 | SmallVector<Constant*, 16> Elts; |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 1442 | for (unsigned i = 0, e = Record.size(); i != e; i += 2) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1443 | Type *ElTy = getTypeByID(Record[i]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1444 | if (!ElTy) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1445 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 1446 | Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], ElTy)); |
| 1447 | } |
Jay Foad | ed8db7d | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 1448 | ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end()); |
Jay Foad | 2f5fc8c | 2011-07-21 15:15:37 +0000 | [diff] [blame] | 1449 | V = ConstantExpr::getGetElementPtr(Elts[0], Indices, |
| 1450 | BitCode == |
| 1451 | bitc::CST_CODE_CE_INBOUNDS_GEP); |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 1452 | break; |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 1453 | } |
Joe Abbey | 1a6e770 | 2013-09-12 22:02:31 +0000 | [diff] [blame] | 1454 | case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#] |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1455 | if (Record.size() < 3) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1456 | return Error(BitcodeError::InvalidRecord); |
Joe Abbey | 1a6e770 | 2013-09-12 22:02:31 +0000 | [diff] [blame] | 1457 | |
| 1458 | Type *SelectorTy = Type::getInt1Ty(Context); |
| 1459 | |
| 1460 | // If CurTy is a vector of length n, then Record[0] must be a <n x i1> |
| 1461 | // vector. Otherwise, it must be a single bit. |
| 1462 | if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) |
| 1463 | SelectorTy = VectorType::get(Type::getInt1Ty(Context), |
| 1464 | VTy->getNumElements()); |
| 1465 | |
| 1466 | V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0], |
| 1467 | SelectorTy), |
| 1468 | ValueList.getConstantFwdRef(Record[1],CurTy), |
| 1469 | ValueList.getConstantFwdRef(Record[2],CurTy)); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 1470 | break; |
Joe Abbey | 1a6e770 | 2013-09-12 22:02:31 +0000 | [diff] [blame] | 1471 | } |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 1472 | case bitc::CST_CODE_CE_EXTRACTELT |
| 1473 | : { // CE_EXTRACTELT: [opty, opval, opty, opval] |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1474 | if (Record.size() < 3) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1475 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1476 | VectorType *OpTy = |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 1477 | dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1478 | if (!OpTy) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1479 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 1480 | Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 1481 | Constant *Op1 = nullptr; |
| 1482 | if (Record.size() == 4) { |
| 1483 | Type *IdxTy = getTypeByID(Record[2]); |
| 1484 | if (!IdxTy) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1485 | return Error(BitcodeError::InvalidRecord); |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 1486 | Op1 = ValueList.getConstantFwdRef(Record[3], IdxTy); |
| 1487 | } else // TODO: Remove with llvm 4.0 |
| 1488 | Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context)); |
| 1489 | if (!Op1) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1490 | return Error(BitcodeError::InvalidRecord); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1491 | V = ConstantExpr::getExtractElement(Op0, Op1); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 1492 | break; |
| 1493 | } |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 1494 | case bitc::CST_CODE_CE_INSERTELT |
| 1495 | : { // CE_INSERTELT: [opval, opval, opty, opval] |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1496 | VectorType *OpTy = dyn_cast<VectorType>(CurTy); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1497 | if (Record.size() < 3 || !OpTy) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1498 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 1499 | Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); |
| 1500 | Constant *Op1 = ValueList.getConstantFwdRef(Record[1], |
| 1501 | OpTy->getElementType()); |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 1502 | Constant *Op2 = nullptr; |
| 1503 | if (Record.size() == 4) { |
| 1504 | Type *IdxTy = getTypeByID(Record[2]); |
| 1505 | if (!IdxTy) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1506 | return Error(BitcodeError::InvalidRecord); |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 1507 | Op2 = ValueList.getConstantFwdRef(Record[3], IdxTy); |
| 1508 | } else // TODO: Remove with llvm 4.0 |
| 1509 | Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context)); |
| 1510 | if (!Op2) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1511 | return Error(BitcodeError::InvalidRecord); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1512 | V = ConstantExpr::getInsertElement(Op0, Op1, Op2); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 1513 | break; |
| 1514 | } |
| 1515 | case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval] |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1516 | VectorType *OpTy = dyn_cast<VectorType>(CurTy); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1517 | if (Record.size() < 3 || !OpTy) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1518 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 1519 | Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); |
| 1520 | Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1521 | Type *ShufTy = VectorType::get(Type::getInt32Ty(Context), |
Owen Anderson | e9f9804 | 2009-07-07 20:18:58 +0000 | [diff] [blame] | 1522 | OpTy->getNumElements()); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 1523 | Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1524 | V = ConstantExpr::getShuffleVector(Op0, Op1, Op2); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 1525 | break; |
| 1526 | } |
Nate Begeman | 94aa38d | 2009-02-12 21:28:33 +0000 | [diff] [blame] | 1527 | case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval] |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1528 | VectorType *RTy = dyn_cast<VectorType>(CurTy); |
| 1529 | VectorType *OpTy = |
Duncan Sands | 89d412a | 2010-10-28 15:47:26 +0000 | [diff] [blame] | 1530 | dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1531 | if (Record.size() < 4 || !RTy || !OpTy) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1532 | return Error(BitcodeError::InvalidRecord); |
Nate Begeman | 94aa38d | 2009-02-12 21:28:33 +0000 | [diff] [blame] | 1533 | Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); |
| 1534 | Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1535 | Type *ShufTy = VectorType::get(Type::getInt32Ty(Context), |
Owen Anderson | e9f9804 | 2009-07-07 20:18:58 +0000 | [diff] [blame] | 1536 | RTy->getNumElements()); |
Nate Begeman | 94aa38d | 2009-02-12 21:28:33 +0000 | [diff] [blame] | 1537 | Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1538 | V = ConstantExpr::getShuffleVector(Op0, Op1, Op2); |
Nate Begeman | 94aa38d | 2009-02-12 21:28:33 +0000 | [diff] [blame] | 1539 | break; |
| 1540 | } |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 1541 | case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred] |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1542 | if (Record.size() < 4) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1543 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1544 | Type *OpTy = getTypeByID(Record[0]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1545 | if (!OpTy) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1546 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 1547 | Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); |
| 1548 | Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); |
| 1549 | |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1550 | if (OpTy->isFPOrFPVectorTy()) |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1551 | V = ConstantExpr::getFCmp(Record[3], Op0, Op1); |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 1552 | else |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1553 | V = ConstantExpr::getICmp(Record[3], Op0, Op1); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 1554 | break; |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 1555 | } |
Chad Rosier | d8c7610 | 2012-09-05 19:00:49 +0000 | [diff] [blame] | 1556 | // This maintains backward compatibility, pre-asm dialect keywords. |
Chad Rosier | 5895eda | 2012-09-05 06:28:52 +0000 | [diff] [blame] | 1557 | // FIXME: Remove with the 4.0 release. |
Chad Rosier | 18fcdcf | 2012-09-05 00:56:20 +0000 | [diff] [blame] | 1558 | case bitc::CST_CODE_INLINEASM_OLD: { |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1559 | if (Record.size() < 2) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1560 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | af8fffc | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 1561 | std::string AsmStr, ConstrStr; |
Dale Johannesen | fd04c74 | 2009-10-13 20:46:56 +0000 | [diff] [blame] | 1562 | bool HasSideEffects = Record[0] & 1; |
Dale Johannesen | 1cfb958 | 2009-10-21 23:28:00 +0000 | [diff] [blame] | 1563 | bool IsAlignStack = Record[0] >> 1; |
Chris Lattner | af8fffc | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 1564 | unsigned AsmStrSize = Record[1]; |
| 1565 | if (2+AsmStrSize >= Record.size()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1566 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | af8fffc | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 1567 | unsigned ConstStrSize = Record[2+AsmStrSize]; |
| 1568 | if (3+AsmStrSize+ConstStrSize > Record.size()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1569 | return Error(BitcodeError::InvalidRecord); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1570 | |
Chris Lattner | af8fffc | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 1571 | for (unsigned i = 0; i != AsmStrSize; ++i) |
| 1572 | AsmStr += (char)Record[2+i]; |
| 1573 | for (unsigned i = 0; i != ConstStrSize; ++i) |
| 1574 | ConstrStr += (char)Record[3+AsmStrSize+i]; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1575 | PointerType *PTy = cast<PointerType>(CurTy); |
Chris Lattner | af8fffc | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 1576 | V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()), |
Dale Johannesen | 1cfb958 | 2009-10-21 23:28:00 +0000 | [diff] [blame] | 1577 | AsmStr, ConstrStr, HasSideEffects, IsAlignStack); |
Chris Lattner | af8fffc | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 1578 | break; |
| 1579 | } |
Chad Rosier | d8c7610 | 2012-09-05 19:00:49 +0000 | [diff] [blame] | 1580 | // This version adds support for the asm dialect keywords (e.g., |
| 1581 | // inteldialect). |
Chad Rosier | 18fcdcf | 2012-09-05 00:56:20 +0000 | [diff] [blame] | 1582 | case bitc::CST_CODE_INLINEASM: { |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1583 | if (Record.size() < 2) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1584 | return Error(BitcodeError::InvalidRecord); |
Chad Rosier | 18fcdcf | 2012-09-05 00:56:20 +0000 | [diff] [blame] | 1585 | std::string AsmStr, ConstrStr; |
| 1586 | bool HasSideEffects = Record[0] & 1; |
| 1587 | bool IsAlignStack = (Record[0] >> 1) & 1; |
| 1588 | unsigned AsmDialect = Record[0] >> 2; |
| 1589 | unsigned AsmStrSize = Record[1]; |
| 1590 | if (2+AsmStrSize >= Record.size()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1591 | return Error(BitcodeError::InvalidRecord); |
Chad Rosier | 18fcdcf | 2012-09-05 00:56:20 +0000 | [diff] [blame] | 1592 | unsigned ConstStrSize = Record[2+AsmStrSize]; |
| 1593 | if (3+AsmStrSize+ConstStrSize > Record.size()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1594 | return Error(BitcodeError::InvalidRecord); |
Chad Rosier | 18fcdcf | 2012-09-05 00:56:20 +0000 | [diff] [blame] | 1595 | |
| 1596 | for (unsigned i = 0; i != AsmStrSize; ++i) |
| 1597 | AsmStr += (char)Record[2+i]; |
| 1598 | for (unsigned i = 0; i != ConstStrSize; ++i) |
| 1599 | ConstrStr += (char)Record[3+AsmStrSize+i]; |
| 1600 | PointerType *PTy = cast<PointerType>(CurTy); |
| 1601 | V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()), |
| 1602 | AsmStr, ConstrStr, HasSideEffects, IsAlignStack, |
Chad Rosier | d8c7610 | 2012-09-05 19:00:49 +0000 | [diff] [blame] | 1603 | InlineAsm::AsmDialect(AsmDialect)); |
Chad Rosier | 18fcdcf | 2012-09-05 00:56:20 +0000 | [diff] [blame] | 1604 | break; |
| 1605 | } |
Chris Lattner | 5956dc8 | 2009-10-28 05:53:48 +0000 | [diff] [blame] | 1606 | case bitc::CST_CODE_BLOCKADDRESS:{ |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1607 | if (Record.size() < 3) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1608 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1609 | Type *FnTy = getTypeByID(Record[0]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1610 | if (!FnTy) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1611 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 5956dc8 | 2009-10-28 05:53:48 +0000 | [diff] [blame] | 1612 | Function *Fn = |
| 1613 | dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy)); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1614 | if (!Fn) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1615 | return Error(BitcodeError::InvalidRecord); |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 1616 | |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 1617 | // Don't let Fn get dematerialized. |
| 1618 | BlockAddressesTaken.insert(Fn); |
| 1619 | |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 1620 | // If the function is already parsed we can insert the block address right |
| 1621 | // away. |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 1622 | BasicBlock *BB; |
| 1623 | unsigned BBID = Record[2]; |
| 1624 | if (!BBID) |
| 1625 | // Invalid reference to entry block. |
| 1626 | return Error(BitcodeError::InvalidID); |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 1627 | if (!Fn->empty()) { |
| 1628 | Function::iterator BBI = Fn->begin(), BBE = Fn->end(); |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 1629 | for (size_t I = 0, E = BBID; I != E; ++I) { |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 1630 | if (BBI == BBE) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1631 | return Error(BitcodeError::InvalidID); |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 1632 | ++BBI; |
| 1633 | } |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 1634 | BB = BBI; |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 1635 | } else { |
| 1636 | // Otherwise insert a placeholder and remember it so it can be inserted |
| 1637 | // when the function is parsed. |
Duncan P. N. Exon Smith | 5a511b5 | 2014-08-05 17:49:48 +0000 | [diff] [blame] | 1638 | auto &FwdBBs = BasicBlockFwdRefs[Fn]; |
| 1639 | if (FwdBBs.empty()) |
| 1640 | BasicBlockFwdRefQueue.push_back(Fn); |
Duncan P. N. Exon Smith | 5a5fd7b | 2014-08-16 01:54:37 +0000 | [diff] [blame^] | 1641 | if (FwdBBs.size() < BBID + 1) |
| 1642 | FwdBBs.resize(BBID + 1); |
| 1643 | if (!FwdBBs[BBID]) |
| 1644 | FwdBBs[BBID] = BasicBlock::Create(Context); |
| 1645 | BB = FwdBBs[BBID]; |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 1646 | } |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 1647 | V = BlockAddress::get(Fn, BB); |
Chris Lattner | 5956dc8 | 2009-10-28 05:53:48 +0000 | [diff] [blame] | 1648 | break; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1649 | } |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 1650 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1651 | |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1652 | ValueList.AssignValue(V, NextCstNo); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 1653 | ++NextCstNo; |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 1654 | } |
| 1655 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1656 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1657 | std::error_code BitcodeReader::ParseUseLists() { |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 1658 | if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1659 | return Error(BitcodeError::InvalidRecord); |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 1660 | |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 1661 | // Read all the records. |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 1662 | SmallVector<uint64_t, 64> Record; |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 1663 | while (1) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1664 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 1665 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1666 | switch (Entry.Kind) { |
| 1667 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 1668 | case BitstreamEntry::Error: |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1669 | return Error(BitcodeError::MalformedBlock); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1670 | case BitstreamEntry::EndBlock: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1671 | return std::error_code(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1672 | case BitstreamEntry::Record: |
| 1673 | // The interesting case. |
| 1674 | break; |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 1675 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1676 | |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 1677 | // Read a use list record. |
| 1678 | Record.clear(); |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 1679 | bool IsBB = false; |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1680 | switch (Stream.readRecord(Entry.ID, Record)) { |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 1681 | default: // Default behavior: unknown type. |
| 1682 | break; |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 1683 | case bitc::USELIST_CODE_BB: |
| 1684 | IsBB = true; |
| 1685 | // fallthrough |
| 1686 | case bitc::USELIST_CODE_DEFAULT: { |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 1687 | unsigned RecordLength = Record.size(); |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 1688 | if (RecordLength < 3) |
| 1689 | // Records should have at least an ID and two indexes. |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1690 | return Error(BitcodeError::InvalidRecord); |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 1691 | unsigned ID = Record.back(); |
| 1692 | Record.pop_back(); |
| 1693 | |
| 1694 | Value *V; |
| 1695 | if (IsBB) { |
| 1696 | assert(ID < FunctionBBs.size() && "Basic block not found"); |
| 1697 | V = FunctionBBs[ID]; |
| 1698 | } else |
| 1699 | V = ValueList[ID]; |
| 1700 | unsigned NumUses = 0; |
| 1701 | SmallDenseMap<const Use *, unsigned, 16> Order; |
| 1702 | for (const Use &U : V->uses()) { |
Duncan P. N. Exon Smith | 1318364 | 2014-08-16 01:54:34 +0000 | [diff] [blame] | 1703 | if (++NumUses > Record.size()) |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 1704 | break; |
Duncan P. N. Exon Smith | 1318364 | 2014-08-16 01:54:34 +0000 | [diff] [blame] | 1705 | Order[&U] = Record[NumUses - 1]; |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 1706 | } |
| 1707 | if (Order.size() != Record.size() || NumUses > Record.size()) |
| 1708 | // Mismatches can happen if the functions are being materialized lazily |
| 1709 | // (out-of-order), or a value has been upgraded. |
| 1710 | break; |
| 1711 | |
| 1712 | V->sortUseList([&](const Use &L, const Use &R) { |
| 1713 | return Order.lookup(&L) < Order.lookup(&R); |
| 1714 | }); |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 1715 | break; |
| 1716 | } |
| 1717 | } |
| 1718 | } |
| 1719 | } |
| 1720 | |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1721 | /// RememberAndSkipFunctionBody - When we see the block for a function body, |
| 1722 | /// remember where it is and then skip it. This lets us lazily deserialize the |
| 1723 | /// functions. |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1724 | std::error_code BitcodeReader::RememberAndSkipFunctionBody() { |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 1725 | // Get the function we are talking about. |
| 1726 | if (FunctionsWithBodies.empty()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1727 | return Error(BitcodeError::InsufficientFunctionProtos); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1728 | |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 1729 | Function *Fn = FunctionsWithBodies.back(); |
| 1730 | FunctionsWithBodies.pop_back(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1731 | |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 1732 | // Save the current stream state. |
| 1733 | uint64_t CurBit = Stream.GetCurrentBitNo(); |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 1734 | DeferredFunctionInfo[Fn] = CurBit; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1735 | |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 1736 | // Skip over the function block for now. |
| 1737 | if (Stream.SkipBlock()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1738 | return Error(BitcodeError::InvalidRecord); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1739 | return std::error_code(); |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1742 | std::error_code BitcodeReader::GlobalCleanup() { |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 1743 | // Patch the initializers for globals and aliases up. |
| 1744 | ResolveGlobalAndAliasInits(); |
| 1745 | if (!GlobalInits.empty() || !AliasInits.empty()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1746 | return Error(BitcodeError::MalformedGlobalInitializerSet); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 1747 | |
| 1748 | // Look for intrinsic functions which need to be upgraded at some point |
| 1749 | for (Module::iterator FI = TheModule->begin(), FE = TheModule->end(); |
| 1750 | FI != FE; ++FI) { |
| 1751 | Function *NewFn; |
| 1752 | if (UpgradeIntrinsicFunction(FI, NewFn)) |
| 1753 | UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn)); |
| 1754 | } |
| 1755 | |
| 1756 | // Look for global variables which need to be renamed. |
| 1757 | for (Module::global_iterator |
| 1758 | GI = TheModule->global_begin(), GE = TheModule->global_end(); |
Reid Kleckner | fceb76f | 2014-05-16 20:39:27 +0000 | [diff] [blame] | 1759 | GI != GE;) { |
| 1760 | GlobalVariable *GV = GI++; |
| 1761 | UpgradeGlobalVariable(GV); |
| 1762 | } |
| 1763 | |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 1764 | // Force deallocation of memory for these vectors to favor the client that |
| 1765 | // want lazy deserialization. |
| 1766 | std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits); |
| 1767 | std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1768 | return std::error_code(); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1771 | std::error_code BitcodeReader::ParseModule(bool Resume) { |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 1772 | if (Resume) |
| 1773 | Stream.JumpToBit(NextUnreadBit); |
| 1774 | else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1775 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1776 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1777 | SmallVector<uint64_t, 64> Record; |
| 1778 | std::vector<std::string> SectionTable; |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 1779 | std::vector<std::string> GCTable; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1780 | |
| 1781 | // Read all the records for this module. |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1782 | while (1) { |
| 1783 | BitstreamEntry Entry = Stream.advance(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 1784 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1785 | switch (Entry.Kind) { |
| 1786 | case BitstreamEntry::Error: |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1787 | return Error(BitcodeError::MalformedBlock); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1788 | case BitstreamEntry::EndBlock: |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 1789 | return GlobalCleanup(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 1790 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1791 | case BitstreamEntry::SubBlock: |
| 1792 | switch (Entry.ID) { |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1793 | default: // Skip unknown content. |
| 1794 | if (Stream.SkipBlock()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1795 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1796 | break; |
Chris Lattner | 6eeea5d | 2007-05-05 18:57:30 +0000 | [diff] [blame] | 1797 | case bitc::BLOCKINFO_BLOCK_ID: |
| 1798 | if (Stream.ReadBlockInfoBlock()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1799 | return Error(BitcodeError::MalformedBlock); |
Chris Lattner | 6eeea5d | 2007-05-05 18:57:30 +0000 | [diff] [blame] | 1800 | break; |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1801 | case bitc::PARAMATTR_BLOCK_ID: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1802 | if (std::error_code EC = ParseAttributeBlock()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1803 | return EC; |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1804 | break; |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1805 | case bitc::PARAMATTR_GROUP_BLOCK_ID: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1806 | if (std::error_code EC = ParseAttributeGroupBlock()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1807 | return EC; |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1808 | break; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1809 | case bitc::TYPE_BLOCK_ID_NEW: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1810 | if (std::error_code EC = ParseTypeTable()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1811 | return EC; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1812 | break; |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1813 | case bitc::VALUE_SYMTAB_BLOCK_ID: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1814 | if (std::error_code EC = ParseValueSymbolTable()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1815 | return EC; |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 1816 | SeenValueSymbolTable = true; |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1817 | break; |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 1818 | case bitc::CONSTANTS_BLOCK_ID: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1819 | if (std::error_code EC = ParseConstants()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1820 | return EC; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1821 | if (std::error_code EC = ResolveGlobalAndAliasInits()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1822 | return EC; |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 1823 | break; |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1824 | case bitc::METADATA_BLOCK_ID: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1825 | if (std::error_code EC = ParseMetadata()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1826 | return EC; |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1827 | break; |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 1828 | case bitc::FUNCTION_BLOCK_ID: |
| 1829 | // If this is the first function body we've seen, reverse the |
| 1830 | // FunctionsWithBodies list. |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 1831 | if (!SeenFirstFunctionBody) { |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 1832 | std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end()); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1833 | if (std::error_code EC = GlobalCleanup()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1834 | return EC; |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 1835 | SeenFirstFunctionBody = true; |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 1836 | } |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 1837 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1838 | if (std::error_code EC = RememberAndSkipFunctionBody()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1839 | return EC; |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 1840 | // For streaming bitcode, suspend parsing when we reach the function |
| 1841 | // bodies. Subsequent materialization calls will resume it when |
| 1842 | // necessary. For streaming, the function bodies must be at the end of |
| 1843 | // the bitcode. If the bitcode file is old, the symbol table will be |
| 1844 | // at the end instead and will not have been seen yet. In this case, |
| 1845 | // just finish the parse now. |
| 1846 | if (LazyStreamer && SeenValueSymbolTable) { |
| 1847 | NextUnreadBit = Stream.GetCurrentBitNo(); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1848 | return std::error_code(); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 1849 | } |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 1850 | break; |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 1851 | case bitc::USELIST_BLOCK_ID: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1852 | if (std::error_code EC = ParseUseLists()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1853 | return EC; |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 1854 | break; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1855 | } |
| 1856 | continue; |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 1857 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1858 | case BitstreamEntry::Record: |
| 1859 | // The interesting case. |
| 1860 | break; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1861 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1862 | |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1863 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1864 | // Read a record. |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1865 | switch (Stream.readRecord(Entry.ID, Record)) { |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1866 | default: break; // Default behavior, ignore unknown content. |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 1867 | case bitc::MODULE_CODE_VERSION: { // VERSION: [version#] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1868 | if (Record.size() < 1) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1869 | return Error(BitcodeError::InvalidRecord); |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 1870 | // Only version #0 and #1 are supported so far. |
| 1871 | unsigned module_version = Record[0]; |
| 1872 | switch (module_version) { |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1873 | default: |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1874 | return Error(BitcodeError::InvalidValue); |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 1875 | case 0: |
| 1876 | UseRelativeIDs = false; |
| 1877 | break; |
| 1878 | case 1: |
| 1879 | UseRelativeIDs = true; |
| 1880 | break; |
| 1881 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1882 | break; |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 1883 | } |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 1884 | case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1885 | std::string S; |
| 1886 | if (ConvertToString(Record, 0, S)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1887 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1888 | TheModule->setTargetTriple(S); |
| 1889 | break; |
| 1890 | } |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 1891 | case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1892 | std::string S; |
| 1893 | if (ConvertToString(Record, 0, S)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1894 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1895 | TheModule->setDataLayout(S); |
| 1896 | break; |
| 1897 | } |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 1898 | case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1899 | std::string S; |
| 1900 | if (ConvertToString(Record, 0, S)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1901 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1902 | TheModule->setModuleInlineAsm(S); |
| 1903 | break; |
| 1904 | } |
Bill Wendling | 706d3d6 | 2012-11-28 08:41:48 +0000 | [diff] [blame] | 1905 | case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N] |
| 1906 | // FIXME: Remove in 4.0. |
| 1907 | std::string S; |
| 1908 | if (ConvertToString(Record, 0, S)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1909 | return Error(BitcodeError::InvalidRecord); |
Bill Wendling | 706d3d6 | 2012-11-28 08:41:48 +0000 | [diff] [blame] | 1910 | // Ignore value. |
| 1911 | break; |
| 1912 | } |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 1913 | case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1914 | std::string S; |
| 1915 | if (ConvertToString(Record, 0, S)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1916 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1917 | SectionTable.push_back(S); |
| 1918 | break; |
| 1919 | } |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 1920 | case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N] |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 1921 | std::string S; |
| 1922 | if (ConvertToString(Record, 0, S)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1923 | return Error(BitcodeError::InvalidRecord); |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 1924 | GCTable.push_back(S); |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 1925 | break; |
| 1926 | } |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1927 | case bitc::MODULE_CODE_COMDAT: { // COMDAT: [selection_kind, name] |
| 1928 | if (Record.size() < 2) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1929 | return Error(BitcodeError::InvalidRecord); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1930 | Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]); |
| 1931 | unsigned ComdatNameSize = Record[1]; |
| 1932 | std::string ComdatName; |
| 1933 | ComdatName.reserve(ComdatNameSize); |
| 1934 | for (unsigned i = 0; i != ComdatNameSize; ++i) |
| 1935 | ComdatName += (char)Record[2 + i]; |
| 1936 | Comdat *C = TheModule->getOrInsertComdat(ComdatName); |
| 1937 | C->setSelectionKind(SK); |
| 1938 | ComdatList.push_back(C); |
| 1939 | break; |
| 1940 | } |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 1941 | // GLOBALVAR: [pointer type, isconst, initid, |
Rafael Espindola | 45e6c19 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 1942 | // linkage, alignment, section, visibility, threadlocal, |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 1943 | // unnamed_addr, dllstorageclass] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1944 | case bitc::MODULE_CODE_GLOBALVAR: { |
Chris Lattner | 4b00d92 | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 1945 | if (Record.size() < 6) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1946 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1947 | Type *Ty = getTypeByID(Record[0]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1948 | if (!Ty) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1949 | return Error(BitcodeError::InvalidRecord); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1950 | if (!Ty->isPointerTy()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1951 | return Error(BitcodeError::InvalidTypeForValue); |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 1952 | unsigned AddressSpace = cast<PointerType>(Ty)->getAddressSpace(); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1953 | Ty = cast<PointerType>(Ty)->getElementType(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1954 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1955 | bool isConstant = Record[1]; |
| 1956 | GlobalValue::LinkageTypes Linkage = GetDecodedLinkage(Record[3]); |
| 1957 | unsigned Alignment = (1 << Record[4]) >> 1; |
| 1958 | std::string Section; |
| 1959 | if (Record[5]) { |
| 1960 | if (Record[5]-1 >= SectionTable.size()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 1961 | return Error(BitcodeError::InvalidID); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1962 | Section = SectionTable[Record[5]-1]; |
| 1963 | } |
Chris Lattner | 4b00d92 | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 1964 | GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility; |
Duncan P. N. Exon Smith | b80de10 | 2014-05-07 22:57:20 +0000 | [diff] [blame] | 1965 | // Local linkage must have default visibility. |
| 1966 | if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage)) |
| 1967 | // FIXME: Change to an error if non-default in 4.0. |
Chris Lattner | 53862f7 | 2007-05-06 19:27:46 +0000 | [diff] [blame] | 1968 | Visibility = GetDecodedVisibility(Record[6]); |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 1969 | |
| 1970 | GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal; |
Chris Lattner | 53862f7 | 2007-05-06 19:27:46 +0000 | [diff] [blame] | 1971 | if (Record.size() > 7) |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 1972 | TLM = GetDecodedThreadLocalMode(Record[7]); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1973 | |
Rafael Espindola | 45e6c19 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 1974 | bool UnnamedAddr = false; |
| 1975 | if (Record.size() > 8) |
| 1976 | UnnamedAddr = Record[8]; |
| 1977 | |
Michael Gottesman | 27e7ef3 | 2013-02-05 05:57:38 +0000 | [diff] [blame] | 1978 | bool ExternallyInitialized = false; |
| 1979 | if (Record.size() > 9) |
| 1980 | ExternallyInitialized = Record[9]; |
| 1981 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1982 | GlobalVariable *NewGV = |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1983 | new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, "", nullptr, |
Michael Gottesman | 27e7ef3 | 2013-02-05 05:57:38 +0000 | [diff] [blame] | 1984 | TLM, AddressSpace, ExternallyInitialized); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1985 | NewGV->setAlignment(Alignment); |
| 1986 | if (!Section.empty()) |
| 1987 | NewGV->setSection(Section); |
| 1988 | NewGV->setVisibility(Visibility); |
Rafael Espindola | 45e6c19 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 1989 | NewGV->setUnnamedAddr(UnnamedAddr); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1990 | |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 1991 | if (Record.size() > 10) |
| 1992 | NewGV->setDLLStorageClass(GetDecodedDLLStorageClass(Record[10])); |
| 1993 | else |
| 1994 | UpgradeDLLImportExportLinkage(NewGV, Record[3]); |
| 1995 | |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1996 | ValueList.push_back(NewGV); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1997 | |
Chris Lattner | 47d131b | 2007-04-24 00:18:21 +0000 | [diff] [blame] | 1998 | // Remember which value to use for the global initializer. |
| 1999 | if (unsigned InitID = Record[2]) |
| 2000 | GlobalInits.push_back(std::make_pair(NewGV, InitID-1)); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 2001 | |
| 2002 | if (Record.size() > 11) |
| 2003 | if (unsigned ComdatID = Record[11]) { |
| 2004 | assert(ComdatID <= ComdatList.size()); |
| 2005 | NewGV->setComdat(ComdatList[ComdatID - 1]); |
| 2006 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2007 | break; |
| 2008 | } |
Chris Lattner | 4c0a6d6 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 2009 | // FUNCTION: [type, callingconv, isproto, linkage, paramattr, |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 2010 | // alignment, section, visibility, gc, unnamed_addr, |
| 2011 | // dllstorageclass] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2012 | case bitc::MODULE_CODE_FUNCTION: { |
Chris Lattner | 4c0a6d6 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 2013 | if (Record.size() < 8) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2014 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2015 | Type *Ty = getTypeByID(Record[0]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2016 | if (!Ty) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2017 | return Error(BitcodeError::InvalidRecord); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2018 | if (!Ty->isPointerTy()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2019 | return Error(BitcodeError::InvalidTypeForValue); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2020 | FunctionType *FTy = |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2021 | dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType()); |
| 2022 | if (!FTy) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2023 | return Error(BitcodeError::InvalidTypeForValue); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2024 | |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2025 | Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage, |
| 2026 | "", TheModule); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2027 | |
Sandeep Patel | 68c5f47 | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 2028 | Func->setCallingConv(static_cast<CallingConv::ID>(Record[1])); |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 2029 | bool isProto = Record[2]; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2030 | Func->setLinkage(GetDecodedLinkage(Record[3])); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 2031 | Func->setAttributes(getAttributes(Record[4])); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2032 | |
Chris Lattner | 4c0a6d6 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 2033 | Func->setAlignment((1 << Record[5]) >> 1); |
| 2034 | if (Record[6]) { |
| 2035 | if (Record[6]-1 >= SectionTable.size()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2036 | return Error(BitcodeError::InvalidID); |
Chris Lattner | 4c0a6d6 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 2037 | Func->setSection(SectionTable[Record[6]-1]); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2038 | } |
Duncan P. N. Exon Smith | b80de10 | 2014-05-07 22:57:20 +0000 | [diff] [blame] | 2039 | // Local linkage must have default visibility. |
| 2040 | if (!Func->hasLocalLinkage()) |
| 2041 | // FIXME: Change to an error if non-default in 4.0. |
| 2042 | Func->setVisibility(GetDecodedVisibility(Record[7])); |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 2043 | if (Record.size() > 8 && Record[8]) { |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 2044 | if (Record[8]-1 > GCTable.size()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2045 | return Error(BitcodeError::InvalidID); |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 2046 | Func->setGC(GCTable[Record[8]-1].c_str()); |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 2047 | } |
Rafael Espindola | 45e6c19 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 2048 | bool UnnamedAddr = false; |
| 2049 | if (Record.size() > 9) |
| 2050 | UnnamedAddr = Record[9]; |
| 2051 | Func->setUnnamedAddr(UnnamedAddr); |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 2052 | if (Record.size() > 10 && Record[10] != 0) |
| 2053 | FunctionPrefixes.push_back(std::make_pair(Func, Record[10]-1)); |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 2054 | |
| 2055 | if (Record.size() > 11) |
| 2056 | Func->setDLLStorageClass(GetDecodedDLLStorageClass(Record[11])); |
| 2057 | else |
| 2058 | UpgradeDLLImportExportLinkage(Func, Record[3]); |
| 2059 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 2060 | if (Record.size() > 12) |
| 2061 | if (unsigned ComdatID = Record[12]) { |
| 2062 | assert(ComdatID <= ComdatList.size()); |
| 2063 | Func->setComdat(ComdatList[ComdatID - 1]); |
| 2064 | } |
| 2065 | |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 2066 | ValueList.push_back(Func); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2067 | |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 2068 | // If this is a function with a body, remember the prototype we are |
| 2069 | // creating now, so that we can match up the body with them later. |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 2070 | if (!isProto) { |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 2071 | FunctionsWithBodies.push_back(Func); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 2072 | if (LazyStreamer) DeferredFunctionInfo[Func] = 0; |
| 2073 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2074 | break; |
| 2075 | } |
Anton Korobeynikov | 2f22e3f | 2008-03-12 00:49:19 +0000 | [diff] [blame] | 2076 | // ALIAS: [alias type, aliasee val#, linkage] |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 2077 | // ALIAS: [alias type, aliasee val#, linkage, visibility, dllstorageclass] |
Chris Lattner | 831d420 | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 2078 | case bitc::MODULE_CODE_ALIAS: { |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 2079 | if (Record.size() < 3) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2080 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2081 | Type *Ty = getTypeByID(Record[0]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2082 | if (!Ty) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2083 | return Error(BitcodeError::InvalidRecord); |
Rafael Espindola | a800445 | 2014-05-16 14:22:33 +0000 | [diff] [blame] | 2084 | auto *PTy = dyn_cast<PointerType>(Ty); |
| 2085 | if (!PTy) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2086 | return Error(BitcodeError::InvalidTypeForValue); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2087 | |
Rafael Espindola | a800445 | 2014-05-16 14:22:33 +0000 | [diff] [blame] | 2088 | auto *NewGA = |
Rafael Espindola | f1bedd374 | 2014-05-17 21:29:57 +0000 | [diff] [blame] | 2089 | GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(), |
| 2090 | GetDecodedLinkage(Record[2]), "", TheModule); |
Anton Korobeynikov | 2f22e3f | 2008-03-12 00:49:19 +0000 | [diff] [blame] | 2091 | // Old bitcode files didn't have visibility field. |
Duncan P. N. Exon Smith | b80de10 | 2014-05-07 22:57:20 +0000 | [diff] [blame] | 2092 | // Local linkage must have default visibility. |
| 2093 | if (Record.size() > 3 && !NewGA->hasLocalLinkage()) |
| 2094 | // FIXME: Change to an error if non-default in 4.0. |
Anton Korobeynikov | 2f22e3f | 2008-03-12 00:49:19 +0000 | [diff] [blame] | 2095 | NewGA->setVisibility(GetDecodedVisibility(Record[3])); |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 2096 | if (Record.size() > 4) |
| 2097 | NewGA->setDLLStorageClass(GetDecodedDLLStorageClass(Record[4])); |
| 2098 | else |
| 2099 | UpgradeDLLImportExportLinkage(NewGA, Record[2]); |
Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 2100 | if (Record.size() > 5) |
| 2101 | NewGA->setThreadLocalMode(GetDecodedThreadLocalMode(Record[5])); |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 2102 | if (Record.size() > 6) |
| 2103 | NewGA->setUnnamedAddr(Record[6]); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 2104 | ValueList.push_back(NewGA); |
| 2105 | AliasInits.push_back(std::make_pair(NewGA, Record[1])); |
| 2106 | break; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2107 | } |
Chris Lattner | 831d420 | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 2108 | /// MODULE_CODE_PURGEVALS: [numvals] |
| 2109 | case bitc::MODULE_CODE_PURGEVALS: |
| 2110 | // Trim down the value list to the specified size. |
| 2111 | if (Record.size() < 1 || Record[0] > ValueList.size()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2112 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 831d420 | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 2113 | ValueList.shrinkTo(Record[0]); |
| 2114 | break; |
| 2115 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2116 | Record.clear(); |
| 2117 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2120 | std::error_code BitcodeReader::ParseBitcodeInto(Module *M) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2121 | TheModule = nullptr; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2122 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2123 | if (std::error_code EC = InitStream()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2124 | return EC; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2125 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2126 | // Sniff for the signature. |
| 2127 | if (Stream.Read(8) != 'B' || |
| 2128 | Stream.Read(8) != 'C' || |
| 2129 | Stream.Read(4) != 0x0 || |
| 2130 | Stream.Read(4) != 0xC || |
| 2131 | Stream.Read(4) != 0xE || |
| 2132 | Stream.Read(4) != 0xD) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2133 | return Error(BitcodeError::InvalidBitcodeSignature); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2134 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2135 | // We expect a number of well-defined blocks, though we don't necessarily |
| 2136 | // need to understand them all. |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2137 | while (1) { |
| 2138 | if (Stream.AtEndOfStream()) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2139 | return std::error_code(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2140 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2141 | BitstreamEntry Entry = |
| 2142 | Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2143 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2144 | switch (Entry.Kind) { |
| 2145 | case BitstreamEntry::Error: |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2146 | return Error(BitcodeError::MalformedBlock); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2147 | case BitstreamEntry::EndBlock: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2148 | return std::error_code(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2149 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2150 | case BitstreamEntry::SubBlock: |
| 2151 | switch (Entry.ID) { |
| 2152 | case bitc::BLOCKINFO_BLOCK_ID: |
| 2153 | if (Stream.ReadBlockInfoBlock()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2154 | return Error(BitcodeError::MalformedBlock); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2155 | break; |
| 2156 | case bitc::MODULE_BLOCK_ID: |
| 2157 | // Reject multiple MODULE_BLOCK's in a single bitstream. |
| 2158 | if (TheModule) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2159 | return Error(BitcodeError::InvalidMultipleBlocks); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2160 | TheModule = M; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2161 | if (std::error_code EC = ParseModule(false)) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2162 | return EC; |
| 2163 | if (LazyStreamer) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2164 | return std::error_code(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2165 | break; |
| 2166 | default: |
| 2167 | if (Stream.SkipBlock()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2168 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2169 | break; |
| 2170 | } |
| 2171 | continue; |
| 2172 | case BitstreamEntry::Record: |
| 2173 | // There should be no records in the top-level of blocks. |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2174 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2175 | // The ranlib in Xcode 4 will align archive members by appending newlines |
Chad Rosier | a15e3aa | 2011-08-09 22:23:40 +0000 | [diff] [blame] | 2176 | // to the end of them. If this file size is a multiple of 4 but not 8, we |
| 2177 | // have to read and ignore these final 4 bytes :-( |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2178 | if (Stream.getAbbrevIDWidth() == 2 && Entry.ID == 2 && |
Rafael Espindola | a97b238 | 2011-05-26 18:59:54 +0000 | [diff] [blame] | 2179 | Stream.Read(6) == 2 && Stream.Read(24) == 0xa0a0a && |
Bill Wendling | 318f03f | 2012-07-19 00:15:11 +0000 | [diff] [blame] | 2180 | Stream.AtEndOfStream()) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2181 | return std::error_code(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2182 | |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2183 | return Error(BitcodeError::InvalidRecord); |
Rafael Espindola | a97b238 | 2011-05-26 18:59:54 +0000 | [diff] [blame] | 2184 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2185 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2186 | } |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 2187 | |
Rafael Espindola | c75c4fa | 2014-07-04 20:02:42 +0000 | [diff] [blame] | 2188 | ErrorOr<std::string> BitcodeReader::parseModuleTriple() { |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 2189 | if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2190 | return Error(BitcodeError::InvalidRecord); |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 2191 | |
| 2192 | SmallVector<uint64_t, 64> Record; |
| 2193 | |
Rafael Espindola | c75c4fa | 2014-07-04 20:02:42 +0000 | [diff] [blame] | 2194 | std::string Triple; |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 2195 | // Read all the records for this module. |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2196 | while (1) { |
| 2197 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2198 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2199 | switch (Entry.Kind) { |
| 2200 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 2201 | case BitstreamEntry::Error: |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2202 | return Error(BitcodeError::MalformedBlock); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2203 | case BitstreamEntry::EndBlock: |
Rafael Espindola | e610779 | 2014-07-04 20:05:56 +0000 | [diff] [blame] | 2204 | return Triple; |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2205 | case BitstreamEntry::Record: |
| 2206 | // The interesting case. |
| 2207 | break; |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
| 2210 | // Read a record. |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2211 | switch (Stream.readRecord(Entry.ID, Record)) { |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 2212 | default: break; // Default behavior, ignore unknown content. |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 2213 | case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] |
Rafael Espindola | c75c4fa | 2014-07-04 20:02:42 +0000 | [diff] [blame] | 2214 | std::string S; |
| 2215 | if (ConvertToString(Record, 0, S)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2216 | return Error(BitcodeError::InvalidRecord); |
Rafael Espindola | c75c4fa | 2014-07-04 20:02:42 +0000 | [diff] [blame] | 2217 | Triple = S; |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 2218 | break; |
| 2219 | } |
| 2220 | } |
| 2221 | Record.clear(); |
| 2222 | } |
Rafael Espindola | e610779 | 2014-07-04 20:05:56 +0000 | [diff] [blame] | 2223 | llvm_unreachable("Exit infinite loop"); |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 2224 | } |
| 2225 | |
Rafael Espindola | c75c4fa | 2014-07-04 20:02:42 +0000 | [diff] [blame] | 2226 | ErrorOr<std::string> BitcodeReader::parseTriple() { |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2227 | if (std::error_code EC = InitStream()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2228 | return EC; |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 2229 | |
| 2230 | // Sniff for the signature. |
| 2231 | if (Stream.Read(8) != 'B' || |
| 2232 | Stream.Read(8) != 'C' || |
| 2233 | Stream.Read(4) != 0x0 || |
| 2234 | Stream.Read(4) != 0xC || |
| 2235 | Stream.Read(4) != 0xE || |
| 2236 | Stream.Read(4) != 0xD) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2237 | return Error(BitcodeError::InvalidBitcodeSignature); |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 2238 | |
| 2239 | // We expect a number of well-defined blocks, though we don't necessarily |
| 2240 | // need to understand them all. |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2241 | while (1) { |
| 2242 | BitstreamEntry Entry = Stream.advance(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2243 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2244 | switch (Entry.Kind) { |
| 2245 | case BitstreamEntry::Error: |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2246 | return Error(BitcodeError::MalformedBlock); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2247 | case BitstreamEntry::EndBlock: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2248 | return std::error_code(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2249 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2250 | case BitstreamEntry::SubBlock: |
| 2251 | if (Entry.ID == bitc::MODULE_BLOCK_ID) |
Rafael Espindola | d346cc8 | 2014-07-04 13:52:01 +0000 | [diff] [blame] | 2252 | return parseModuleTriple(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2253 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2254 | // Ignore other sub-blocks. |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2255 | if (Stream.SkipBlock()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2256 | return Error(BitcodeError::MalformedBlock); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2257 | continue; |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2258 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2259 | case BitstreamEntry::Record: |
| 2260 | Stream.skipRecord(Entry.ID); |
| 2261 | continue; |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 2262 | } |
| 2263 | } |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 2264 | } |
| 2265 | |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2266 | /// ParseMetadataAttachment - Parse metadata attachments. |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2267 | std::error_code BitcodeReader::ParseMetadataAttachment() { |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2268 | if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2269 | return Error(BitcodeError::InvalidRecord); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2270 | |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2271 | SmallVector<uint64_t, 64> Record; |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2272 | while (1) { |
| 2273 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2274 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2275 | switch (Entry.Kind) { |
| 2276 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 2277 | case BitstreamEntry::Error: |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2278 | return Error(BitcodeError::MalformedBlock); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2279 | case BitstreamEntry::EndBlock: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2280 | return std::error_code(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2281 | case BitstreamEntry::Record: |
| 2282 | // The interesting case. |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2283 | break; |
| 2284 | } |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2285 | |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2286 | // Read a metadata attachment record. |
| 2287 | Record.clear(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2288 | switch (Stream.readRecord(Entry.ID, Record)) { |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2289 | default: // Default behavior: ignore. |
| 2290 | break; |
Chris Lattner | b877855 | 2011-06-17 17:50:30 +0000 | [diff] [blame] | 2291 | case bitc::METADATA_ATTACHMENT: { |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2292 | unsigned RecordLength = Record.size(); |
| 2293 | if (Record.empty() || (RecordLength - 1) % 2 == 1) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2294 | return Error(BitcodeError::InvalidRecord); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2295 | Instruction *Inst = InstructionList[Record[0]]; |
| 2296 | for (unsigned i = 1; i != RecordLength; i = i+2) { |
Devang Patel | b1a4477 | 2009-09-28 21:14:55 +0000 | [diff] [blame] | 2297 | unsigned Kind = Record[i]; |
Dan Gohman | 43aa8f0 | 2010-07-20 21:42:28 +0000 | [diff] [blame] | 2298 | DenseMap<unsigned, unsigned>::iterator I = |
| 2299 | MDKindMap.find(Kind); |
| 2300 | if (I == MDKindMap.end()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2301 | return Error(BitcodeError::InvalidID); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2302 | Value *Node = MDValueList.getValueFwdRef(Record[i+1]); |
Dan Gohman | 43aa8f0 | 2010-07-20 21:42:28 +0000 | [diff] [blame] | 2303 | Inst->setMetadata(I->second, cast<MDNode>(Node)); |
Manman Ren | 209b17c | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 2304 | if (I->second == LLVMContext::MD_tbaa) |
| 2305 | InstsWithTBAATag.push_back(Inst); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2306 | } |
| 2307 | break; |
| 2308 | } |
| 2309 | } |
| 2310 | } |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2311 | } |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 2312 | |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 2313 | /// ParseFunctionBody - Lazily parse the specified function body block. |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2314 | std::error_code BitcodeReader::ParseFunctionBody(Function *F) { |
Chris Lattner | 982ec1e | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 2315 | if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2316 | return Error(BitcodeError::InvalidRecord); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2317 | |
Nick Lewycky | a72e1af | 2010-02-25 08:30:17 +0000 | [diff] [blame] | 2318 | InstructionList.clear(); |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 2319 | unsigned ModuleValueListSize = ValueList.size(); |
Dan Gohman | 26d837d | 2010-08-25 20:22:53 +0000 | [diff] [blame] | 2320 | unsigned ModuleMDValueListSize = MDValueList.size(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2321 | |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 2322 | // Add all the function arguments to the value table. |
| 2323 | for(Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I) |
| 2324 | ValueList.push_back(I); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2325 | |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 2326 | unsigned NextValueNo = ValueList.size(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2327 | BasicBlock *CurBB = nullptr; |
Chris Lattner | e53603e | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 2328 | unsigned CurBBNo = 0; |
| 2329 | |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 2330 | DebugLoc LastLoc; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2331 | |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 2332 | // Read all the records. |
| 2333 | SmallVector<uint64_t, 64> Record; |
| 2334 | while (1) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2335 | BitstreamEntry Entry = Stream.advance(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2336 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2337 | switch (Entry.Kind) { |
| 2338 | case BitstreamEntry::Error: |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2339 | return Error(BitcodeError::MalformedBlock); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2340 | case BitstreamEntry::EndBlock: |
| 2341 | goto OutOfRecordLoop; |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2342 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2343 | case BitstreamEntry::SubBlock: |
| 2344 | switch (Entry.ID) { |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 2345 | default: // Skip unknown content. |
| 2346 | if (Stream.SkipBlock()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2347 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 2348 | break; |
| 2349 | case bitc::CONSTANTS_BLOCK_ID: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2350 | if (std::error_code EC = ParseConstants()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2351 | return EC; |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 2352 | NextValueNo = ValueList.size(); |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 2353 | break; |
| 2354 | case bitc::VALUE_SYMTAB_BLOCK_ID: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2355 | if (std::error_code EC = ParseValueSymbolTable()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2356 | return EC; |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 2357 | break; |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2358 | case bitc::METADATA_ATTACHMENT_ID: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2359 | if (std::error_code EC = ParseMetadataAttachment()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2360 | return EC; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2361 | break; |
Victor Hernandez | 108d3ac | 2010-01-13 19:34:08 +0000 | [diff] [blame] | 2362 | case bitc::METADATA_BLOCK_ID: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2363 | if (std::error_code EC = ParseMetadata()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2364 | return EC; |
Victor Hernandez | 108d3ac | 2010-01-13 19:34:08 +0000 | [diff] [blame] | 2365 | break; |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 2366 | case bitc::USELIST_BLOCK_ID: |
| 2367 | if (std::error_code EC = ParseUseLists()) |
| 2368 | return EC; |
| 2369 | break; |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 2370 | } |
| 2371 | continue; |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2372 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2373 | case BitstreamEntry::Record: |
| 2374 | // The interesting case. |
| 2375 | break; |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 2376 | } |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2377 | |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 2378 | // Read a record. |
| 2379 | Record.clear(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2380 | Instruction *I = nullptr; |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2381 | unsigned BitCode = Stream.readRecord(Entry.ID, Record); |
Dan Gohman | 0ebd696 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 2382 | switch (BitCode) { |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 2383 | default: // Default behavior: reject |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2384 | return Error(BitcodeError::InvalidValue); |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 2385 | case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks] |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 2386 | if (Record.size() < 1 || Record[0] == 0) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2387 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 2388 | // Create all the basic blocks for the function. |
Chris Lattner | 6ce15cb | 2007-05-03 22:09:51 +0000 | [diff] [blame] | 2389 | FunctionBBs.resize(Record[0]); |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 2390 | |
| 2391 | // See if anything took the address of blocks in this function. |
| 2392 | auto BBFRI = BasicBlockFwdRefs.find(F); |
| 2393 | if (BBFRI == BasicBlockFwdRefs.end()) { |
| 2394 | for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i) |
| 2395 | FunctionBBs[i] = BasicBlock::Create(Context, "", F); |
| 2396 | } else { |
| 2397 | auto &BBRefs = BBFRI->second; |
Duncan P. N. Exon Smith | 5a5fd7b | 2014-08-16 01:54:37 +0000 | [diff] [blame^] | 2398 | // Check for invalid basic block references. |
| 2399 | if (BBRefs.size() > FunctionBBs.size()) |
| 2400 | return Error(BitcodeError::InvalidID); |
| 2401 | assert(!BBRefs.empty() && "Unexpected empty array"); |
| 2402 | assert(!BBRefs.front() && "Invalid reference to entry block"); |
| 2403 | for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E; |
| 2404 | ++I) |
| 2405 | if (I < RE && BBRefs[I]) { |
| 2406 | BBRefs[I]->insertInto(F); |
| 2407 | FunctionBBs[I] = BBRefs[I]; |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 2408 | } else { |
| 2409 | FunctionBBs[I] = BasicBlock::Create(Context, "", F); |
| 2410 | } |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 2411 | |
| 2412 | // Erase from the table. |
| 2413 | BasicBlockFwdRefs.erase(BBFRI); |
| 2414 | } |
| 2415 | |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 2416 | CurBB = FunctionBBs[0]; |
| 2417 | continue; |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 2418 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2419 | |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 2420 | case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN |
| 2421 | // This record indicates that the last instruction is at the same |
| 2422 | // location as the previous instruction with a location. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2423 | I = nullptr; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2424 | |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 2425 | // Get the last instruction emitted. |
| 2426 | if (CurBB && !CurBB->empty()) |
| 2427 | I = &CurBB->back(); |
| 2428 | else if (CurBBNo && FunctionBBs[CurBBNo-1] && |
| 2429 | !FunctionBBs[CurBBNo-1]->empty()) |
| 2430 | I = &FunctionBBs[CurBBNo-1]->back(); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2431 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2432 | if (!I) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2433 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 2434 | I->setDebugLoc(LastLoc); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2435 | I = nullptr; |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 2436 | continue; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2437 | |
Chris Lattner | c4407080 | 2011-06-17 18:17:37 +0000 | [diff] [blame] | 2438 | case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia] |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2439 | I = nullptr; // Get the last instruction emitted. |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 2440 | if (CurBB && !CurBB->empty()) |
| 2441 | I = &CurBB->back(); |
| 2442 | else if (CurBBNo && FunctionBBs[CurBBNo-1] && |
| 2443 | !FunctionBBs[CurBBNo-1]->empty()) |
| 2444 | I = &FunctionBBs[CurBBNo-1]->back(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2445 | if (!I || Record.size() < 4) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2446 | return Error(BitcodeError::InvalidRecord); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2447 | |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 2448 | unsigned Line = Record[0], Col = Record[1]; |
| 2449 | unsigned ScopeID = Record[2], IAID = Record[3]; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2450 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2451 | MDNode *Scope = nullptr, *IA = nullptr; |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 2452 | if (ScopeID) Scope = cast<MDNode>(MDValueList.getValueFwdRef(ScopeID-1)); |
| 2453 | if (IAID) IA = cast<MDNode>(MDValueList.getValueFwdRef(IAID-1)); |
| 2454 | LastLoc = DebugLoc::get(Line, Col, Scope, IA); |
| 2455 | I->setDebugLoc(LastLoc); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2456 | I = nullptr; |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 2457 | continue; |
| 2458 | } |
| 2459 | |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 2460 | case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode] |
| 2461 | unsigned OpNum = 0; |
| 2462 | Value *LHS, *RHS; |
| 2463 | if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 2464 | popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) || |
Dan Gohman | 0ebd696 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 2465 | OpNum+1 > Record.size()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2466 | return Error(BitcodeError::InvalidRecord); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2467 | |
Dan Gohman | 0ebd696 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 2468 | int Opc = GetDecodedBinaryOpcode(Record[OpNum++], LHS->getType()); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2469 | if (Opc == -1) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2470 | return Error(BitcodeError::InvalidRecord); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2471 | I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2472 | InstructionList.push_back(I); |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2473 | if (OpNum < Record.size()) { |
| 2474 | if (Opc == Instruction::Add || |
| 2475 | Opc == Instruction::Sub || |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2476 | Opc == Instruction::Mul || |
| 2477 | Opc == Instruction::Shl) { |
Dan Gohman | 00f4747 | 2010-01-25 21:55:39 +0000 | [diff] [blame] | 2478 | if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP)) |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2479 | cast<BinaryOperator>(I)->setHasNoSignedWrap(true); |
Dan Gohman | 00f4747 | 2010-01-25 21:55:39 +0000 | [diff] [blame] | 2480 | if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2481 | cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true); |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 2482 | } else if (Opc == Instruction::SDiv || |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2483 | Opc == Instruction::UDiv || |
| 2484 | Opc == Instruction::LShr || |
| 2485 | Opc == Instruction::AShr) { |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 2486 | if (Record[OpNum] & (1 << bitc::PEO_EXACT)) |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2487 | cast<BinaryOperator>(I)->setIsExact(true); |
Michael Ilseman | 9978d7e | 2012-11-27 00:43:38 +0000 | [diff] [blame] | 2488 | } else if (isa<FPMathOperator>(I)) { |
| 2489 | FastMathFlags FMF; |
Michael Ilseman | 65f1435 | 2012-12-09 21:12:04 +0000 | [diff] [blame] | 2490 | if (0 != (Record[OpNum] & FastMathFlags::UnsafeAlgebra)) |
| 2491 | FMF.setUnsafeAlgebra(); |
| 2492 | if (0 != (Record[OpNum] & FastMathFlags::NoNaNs)) |
| 2493 | FMF.setNoNaNs(); |
| 2494 | if (0 != (Record[OpNum] & FastMathFlags::NoInfs)) |
| 2495 | FMF.setNoInfs(); |
| 2496 | if (0 != (Record[OpNum] & FastMathFlags::NoSignedZeros)) |
| 2497 | FMF.setNoSignedZeros(); |
| 2498 | if (0 != (Record[OpNum] & FastMathFlags::AllowReciprocal)) |
| 2499 | FMF.setAllowReciprocal(); |
Michael Ilseman | 9978d7e | 2012-11-27 00:43:38 +0000 | [diff] [blame] | 2500 | if (FMF.any()) |
| 2501 | I->setFastMathFlags(FMF); |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2502 | } |
Michael Ilseman | 9978d7e | 2012-11-27 00:43:38 +0000 | [diff] [blame] | 2503 | |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2504 | } |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 2505 | break; |
| 2506 | } |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 2507 | case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc] |
| 2508 | unsigned OpNum = 0; |
| 2509 | Value *Op; |
| 2510 | if (getValueTypePair(Record, OpNum, NextValueNo, Op) || |
| 2511 | OpNum+2 != Record.size()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2512 | return Error(BitcodeError::InvalidRecord); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2513 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2514 | Type *ResTy = getTypeByID(Record[OpNum]); |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 2515 | int Opc = GetDecodedCastOpcode(Record[OpNum+1]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2516 | if (Opc == -1 || !ResTy) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2517 | return Error(BitcodeError::InvalidRecord); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2518 | Instruction *Temp = nullptr; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2519 | if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) { |
| 2520 | if (Temp) { |
| 2521 | InstructionList.push_back(Temp); |
| 2522 | CurBB->getInstList().push_back(Temp); |
| 2523 | } |
| 2524 | } else { |
| 2525 | I = CastInst::Create((Instruction::CastOps)Opc, Op, ResTy); |
| 2526 | } |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2527 | InstructionList.push_back(I); |
Chris Lattner | e53603e | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 2528 | break; |
| 2529 | } |
Dan Gohman | 1639c39 | 2009-07-27 21:53:46 +0000 | [diff] [blame] | 2530 | case bitc::FUNC_CODE_INST_INBOUNDS_GEP: |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 2531 | case bitc::FUNC_CODE_INST_GEP: { // GEP: [n x operands] |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 2532 | unsigned OpNum = 0; |
| 2533 | Value *BasePtr; |
| 2534 | if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2535 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 2536 | |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 2537 | SmallVector<Value*, 16> GEPIdx; |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 2538 | while (OpNum != Record.size()) { |
| 2539 | Value *Op; |
| 2540 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2541 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 2542 | GEPIdx.push_back(Op); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 2543 | } |
| 2544 | |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 2545 | I = GetElementPtrInst::Create(BasePtr, GEPIdx); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2546 | InstructionList.push_back(I); |
Dan Gohman | 1639c39 | 2009-07-27 21:53:46 +0000 | [diff] [blame] | 2547 | if (BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP) |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2548 | cast<GetElementPtrInst>(I)->setIsInBounds(true); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 2549 | break; |
| 2550 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2551 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2552 | case bitc::FUNC_CODE_INST_EXTRACTVAL: { |
| 2553 | // EXTRACTVAL: [opty, opval, n x indices] |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 2554 | unsigned OpNum = 0; |
| 2555 | Value *Agg; |
| 2556 | if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2557 | return Error(BitcodeError::InvalidRecord); |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 2558 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2559 | SmallVector<unsigned, 4> EXTRACTVALIdx; |
| 2560 | for (unsigned RecSize = Record.size(); |
| 2561 | OpNum != RecSize; ++OpNum) { |
| 2562 | uint64_t Index = Record[OpNum]; |
| 2563 | if ((unsigned)Index != Index) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2564 | return Error(BitcodeError::InvalidValue); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2565 | EXTRACTVALIdx.push_back((unsigned)Index); |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 2566 | } |
| 2567 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2568 | I = ExtractValueInst::Create(Agg, EXTRACTVALIdx); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2569 | InstructionList.push_back(I); |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 2570 | break; |
| 2571 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2572 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2573 | case bitc::FUNC_CODE_INST_INSERTVAL: { |
| 2574 | // INSERTVAL: [opty, opval, opty, opval, n x indices] |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 2575 | unsigned OpNum = 0; |
| 2576 | Value *Agg; |
| 2577 | if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2578 | return Error(BitcodeError::InvalidRecord); |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 2579 | Value *Val; |
| 2580 | if (getValueTypePair(Record, OpNum, NextValueNo, Val)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2581 | return Error(BitcodeError::InvalidRecord); |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 2582 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2583 | SmallVector<unsigned, 4> INSERTVALIdx; |
| 2584 | for (unsigned RecSize = Record.size(); |
| 2585 | OpNum != RecSize; ++OpNum) { |
| 2586 | uint64_t Index = Record[OpNum]; |
| 2587 | if ((unsigned)Index != Index) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2588 | return Error(BitcodeError::InvalidValue); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2589 | INSERTVALIdx.push_back((unsigned)Index); |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 2590 | } |
| 2591 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2592 | I = InsertValueInst::Create(Agg, Val, INSERTVALIdx); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2593 | InstructionList.push_back(I); |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 2594 | break; |
| 2595 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2596 | |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 2597 | case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval] |
Dan Gohman | c5d2892 | 2008-09-16 01:01:33 +0000 | [diff] [blame] | 2598 | // obsolete form of select |
| 2599 | // handles select i1 ... in old bitcode |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 2600 | unsigned OpNum = 0; |
| 2601 | Value *TrueVal, *FalseVal, *Cond; |
| 2602 | if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 2603 | popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) || |
| 2604 | popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2605 | return Error(BitcodeError::InvalidRecord); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2606 | |
Dan Gohman | c5d2892 | 2008-09-16 01:01:33 +0000 | [diff] [blame] | 2607 | I = SelectInst::Create(Cond, TrueVal, FalseVal); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2608 | InstructionList.push_back(I); |
Dan Gohman | c5d2892 | 2008-09-16 01:01:33 +0000 | [diff] [blame] | 2609 | break; |
| 2610 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2611 | |
Dan Gohman | c5d2892 | 2008-09-16 01:01:33 +0000 | [diff] [blame] | 2612 | case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred] |
| 2613 | // new form of select |
| 2614 | // handles select i1 or select [N x i1] |
| 2615 | unsigned OpNum = 0; |
| 2616 | Value *TrueVal, *FalseVal, *Cond; |
| 2617 | if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 2618 | popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) || |
Dan Gohman | c5d2892 | 2008-09-16 01:01:33 +0000 | [diff] [blame] | 2619 | getValueTypePair(Record, OpNum, NextValueNo, Cond)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2620 | return Error(BitcodeError::InvalidRecord); |
Dan Gohman | c579d97 | 2008-09-09 01:02:47 +0000 | [diff] [blame] | 2621 | |
| 2622 | // select condition can be either i1 or [N x i1] |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2623 | if (VectorType* vector_type = |
| 2624 | dyn_cast<VectorType>(Cond->getType())) { |
Dan Gohman | c579d97 | 2008-09-09 01:02:47 +0000 | [diff] [blame] | 2625 | // expect <n x i1> |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2626 | if (vector_type->getElementType() != Type::getInt1Ty(Context)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2627 | return Error(BitcodeError::InvalidTypeForValue); |
Dan Gohman | c579d97 | 2008-09-09 01:02:47 +0000 | [diff] [blame] | 2628 | } else { |
| 2629 | // expect i1 |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2630 | if (Cond->getType() != Type::getInt1Ty(Context)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2631 | return Error(BitcodeError::InvalidTypeForValue); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2632 | } |
| 2633 | |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2634 | I = SelectInst::Create(Cond, TrueVal, FalseVal); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2635 | InstructionList.push_back(I); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 2636 | break; |
| 2637 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2638 | |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 2639 | case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval] |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 2640 | unsigned OpNum = 0; |
| 2641 | Value *Vec, *Idx; |
| 2642 | if (getValueTypePair(Record, OpNum, NextValueNo, Vec) || |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 2643 | getValueTypePair(Record, OpNum, NextValueNo, Idx)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2644 | return Error(BitcodeError::InvalidRecord); |
Eric Christopher | c974225 | 2009-07-25 02:28:41 +0000 | [diff] [blame] | 2645 | I = ExtractElementInst::Create(Vec, Idx); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2646 | InstructionList.push_back(I); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 2647 | break; |
| 2648 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2649 | |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 2650 | case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval] |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 2651 | unsigned OpNum = 0; |
| 2652 | Value *Vec, *Elt, *Idx; |
| 2653 | if (getValueTypePair(Record, OpNum, NextValueNo, Vec) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 2654 | popValue(Record, OpNum, NextValueNo, |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 2655 | cast<VectorType>(Vec->getType())->getElementType(), Elt) || |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 2656 | getValueTypePair(Record, OpNum, NextValueNo, Idx)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2657 | return Error(BitcodeError::InvalidRecord); |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2658 | I = InsertElementInst::Create(Vec, Elt, Idx); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2659 | InstructionList.push_back(I); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 2660 | break; |
| 2661 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2662 | |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 2663 | case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval] |
| 2664 | unsigned OpNum = 0; |
| 2665 | Value *Vec1, *Vec2, *Mask; |
| 2666 | if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 2667 | popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2668 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 2669 | |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2670 | if (getValueTypePair(Record, OpNum, NextValueNo, Mask)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2671 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 2672 | I = new ShuffleVectorInst(Vec1, Vec2, Mask); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2673 | InstructionList.push_back(I); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 2674 | break; |
| 2675 | } |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2676 | |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 2677 | case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred] |
| 2678 | // Old form of ICmp/FCmp returning bool |
| 2679 | // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were |
| 2680 | // both legal on vectors but had different behaviour. |
| 2681 | case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred] |
| 2682 | // FCmp/ICmp returning bool or vector of bool |
| 2683 | |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 2684 | unsigned OpNum = 0; |
| 2685 | Value *LHS, *RHS; |
| 2686 | if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 2687 | popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) || |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 2688 | OpNum+1 != Record.size()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2689 | return Error(BitcodeError::InvalidRecord); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2690 | |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2691 | if (LHS->getType()->isFPOrFPVectorTy()) |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2692 | I = new FCmpInst((FCmpInst::Predicate)Record[OpNum], LHS, RHS); |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 2693 | else |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2694 | I = new ICmpInst((ICmpInst::Predicate)Record[OpNum], LHS, RHS); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2695 | InstructionList.push_back(I); |
Dan Gohman | c579d97 | 2008-09-09 01:02:47 +0000 | [diff] [blame] | 2696 | break; |
| 2697 | } |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 2698 | |
Chris Lattner | e53603e | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 2699 | case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>] |
Devang Patel | bbfd874 | 2008-02-26 01:29:32 +0000 | [diff] [blame] | 2700 | { |
| 2701 | unsigned Size = Record.size(); |
| 2702 | if (Size == 0) { |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2703 | I = ReturnInst::Create(Context); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2704 | InstructionList.push_back(I); |
Devang Patel | bbfd874 | 2008-02-26 01:29:32 +0000 | [diff] [blame] | 2705 | break; |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 2706 | } |
Devang Patel | bbfd874 | 2008-02-26 01:29:32 +0000 | [diff] [blame] | 2707 | |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 2708 | unsigned OpNum = 0; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2709 | Value *Op = nullptr; |
Chris Lattner | f1c8710 | 2011-06-17 18:09:11 +0000 | [diff] [blame] | 2710 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2711 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | f1c8710 | 2011-06-17 18:09:11 +0000 | [diff] [blame] | 2712 | if (OpNum != Record.size()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2713 | return Error(BitcodeError::InvalidRecord); |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 2714 | |
Chris Lattner | f1c8710 | 2011-06-17 18:09:11 +0000 | [diff] [blame] | 2715 | I = ReturnInst::Create(Context, Op); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2716 | InstructionList.push_back(I); |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 2717 | break; |
Chris Lattner | e53603e | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 2718 | } |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 2719 | case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#] |
Chris Lattner | 6ce15cb | 2007-05-03 22:09:51 +0000 | [diff] [blame] | 2720 | if (Record.size() != 1 && Record.size() != 3) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2721 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 2722 | BasicBlock *TrueDest = getBasicBlock(Record[0]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2723 | if (!TrueDest) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2724 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 2725 | |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2726 | if (Record.size() == 1) { |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2727 | I = BranchInst::Create(TrueDest); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2728 | InstructionList.push_back(I); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2729 | } |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 2730 | else { |
| 2731 | BasicBlock *FalseDest = getBasicBlock(Record[1]); |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 2732 | Value *Cond = getValue(Record, 2, NextValueNo, |
| 2733 | Type::getInt1Ty(Context)); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2734 | if (!FalseDest || !Cond) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2735 | return Error(BitcodeError::InvalidRecord); |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2736 | I = BranchInst::Create(TrueDest, FalseDest, Cond); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2737 | InstructionList.push_back(I); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 2738 | } |
| 2739 | break; |
| 2740 | } |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 2741 | case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...] |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2742 | // Check magic |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 2743 | if ((Record[0] >> 16) == SWITCH_INST_MAGIC) { |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 2744 | // "New" SwitchInst format with case ranges. The changes to write this |
| 2745 | // format were reverted but we still recognize bitcode that uses it. |
| 2746 | // Hopefully someday we will have support for case ranges and can use |
| 2747 | // this format again. |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2748 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 2749 | Type *OpTy = getTypeByID(Record[1]); |
| 2750 | unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth(); |
| 2751 | |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 2752 | Value *Cond = getValue(Record, 2, NextValueNo, OpTy); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 2753 | BasicBlock *Default = getBasicBlock(Record[3]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2754 | if (!OpTy || !Cond || !Default) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2755 | return Error(BitcodeError::InvalidRecord); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 2756 | |
| 2757 | unsigned NumCases = Record[4]; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2758 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 2759 | SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); |
| 2760 | InstructionList.push_back(SI); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2761 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 2762 | unsigned CurIdx = 5; |
| 2763 | for (unsigned i = 0; i != NumCases; ++i) { |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 2764 | SmallVector<ConstantInt*, 1> CaseVals; |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 2765 | unsigned NumItems = Record[CurIdx++]; |
| 2766 | for (unsigned ci = 0; ci != NumItems; ++ci) { |
| 2767 | bool isSingleNumber = Record[CurIdx++]; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2768 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 2769 | APInt Low; |
| 2770 | unsigned ActiveWords = 1; |
| 2771 | if (ValueBitWidth > 64) |
| 2772 | ActiveWords = Record[CurIdx++]; |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 2773 | Low = ReadWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords), |
| 2774 | ValueBitWidth); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 2775 | CurIdx += ActiveWords; |
Stepan Dyatkovskiy | e3e19cb | 2012-05-28 12:39:09 +0000 | [diff] [blame] | 2776 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 2777 | if (!isSingleNumber) { |
| 2778 | ActiveWords = 1; |
| 2779 | if (ValueBitWidth > 64) |
| 2780 | ActiveWords = Record[CurIdx++]; |
| 2781 | APInt High = |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 2782 | ReadWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords), |
| 2783 | ValueBitWidth); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 2784 | CurIdx += ActiveWords; |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 2785 | |
| 2786 | // FIXME: It is not clear whether values in the range should be |
| 2787 | // compared as signed or unsigned values. The partially |
| 2788 | // implemented changes that used this format in the past used |
| 2789 | // unsigned comparisons. |
| 2790 | for ( ; Low.ule(High); ++Low) |
| 2791 | CaseVals.push_back(ConstantInt::get(Context, Low)); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 2792 | } else |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 2793 | CaseVals.push_back(ConstantInt::get(Context, Low)); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 2794 | } |
| 2795 | BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]); |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 2796 | for (SmallVector<ConstantInt*, 1>::iterator cvi = CaseVals.begin(), |
| 2797 | cve = CaseVals.end(); cvi != cve; ++cvi) |
| 2798 | SI->addCase(*cvi, DestBB); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 2799 | } |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 2800 | I = SI; |
| 2801 | break; |
| 2802 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2803 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 2804 | // Old SwitchInst format without case ranges. |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2805 | |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 2806 | if (Record.size() < 3 || (Record.size() & 1) == 0) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2807 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2808 | Type *OpTy = getTypeByID(Record[0]); |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 2809 | Value *Cond = getValue(Record, 1, NextValueNo, OpTy); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 2810 | BasicBlock *Default = getBasicBlock(Record[2]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2811 | if (!OpTy || !Cond || !Default) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2812 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 2813 | unsigned NumCases = (Record.size()-3)/2; |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2814 | SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2815 | InstructionList.push_back(SI); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 2816 | for (unsigned i = 0, e = NumCases; i != e; ++i) { |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2817 | ConstantInt *CaseVal = |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 2818 | dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy)); |
| 2819 | BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2820 | if (!CaseVal || !DestBB) { |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 2821 | delete SI; |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2822 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 2823 | } |
| 2824 | SI->addCase(CaseVal, DestBB); |
| 2825 | } |
| 2826 | I = SI; |
| 2827 | break; |
| 2828 | } |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 2829 | case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...] |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 2830 | if (Record.size() < 2) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2831 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2832 | Type *OpTy = getTypeByID(Record[0]); |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 2833 | Value *Address = getValue(Record, 1, NextValueNo, OpTy); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2834 | if (!OpTy || !Address) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2835 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 2836 | unsigned NumDests = Record.size()-2; |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 2837 | IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 2838 | InstructionList.push_back(IBI); |
| 2839 | for (unsigned i = 0, e = NumDests; i != e; ++i) { |
| 2840 | if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) { |
| 2841 | IBI->addDestination(DestBB); |
| 2842 | } else { |
| 2843 | delete IBI; |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2844 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 2845 | } |
| 2846 | } |
| 2847 | I = IBI; |
| 2848 | break; |
| 2849 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2850 | |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 2851 | case bitc::FUNC_CODE_INST_INVOKE: { |
| 2852 | // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...] |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2853 | if (Record.size() < 4) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2854 | return Error(BitcodeError::InvalidRecord); |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 2855 | AttributeSet PAL = getAttributes(Record[0]); |
Chris Lattner | 4c0a6d6 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 2856 | unsigned CCInfo = Record[1]; |
| 2857 | BasicBlock *NormalBB = getBasicBlock(Record[2]); |
| 2858 | BasicBlock *UnwindBB = getBasicBlock(Record[3]); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2859 | |
Chris Lattner | 4c0a6d6 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 2860 | unsigned OpNum = 4; |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 2861 | Value *Callee; |
| 2862 | if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2863 | return Error(BitcodeError::InvalidRecord); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2864 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2865 | PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType()); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2866 | FunctionType *FTy = !CalleeTy ? nullptr : |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 2867 | dyn_cast<FunctionType>(CalleeTy->getElementType()); |
| 2868 | |
| 2869 | // Check that the right number of fixed parameters are here. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2870 | if (!FTy || !NormalBB || !UnwindBB || |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 2871 | Record.size() < OpNum+FTy->getNumParams()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2872 | return Error(BitcodeError::InvalidRecord); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2873 | |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 2874 | SmallVector<Value*, 16> Ops; |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 2875 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 2876 | Ops.push_back(getValue(Record, OpNum, NextValueNo, |
| 2877 | FTy->getParamType(i))); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2878 | if (!Ops.back()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2879 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 2880 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2881 | |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 2882 | if (!FTy->isVarArg()) { |
| 2883 | if (Record.size() != OpNum) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2884 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 2885 | } else { |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 2886 | // Read type/value pairs for varargs params. |
| 2887 | while (OpNum != Record.size()) { |
| 2888 | Value *Op; |
| 2889 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2890 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 2891 | Ops.push_back(Op); |
| 2892 | } |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 2893 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2894 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 2895 | I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2896 | InstructionList.push_back(I); |
Sandeep Patel | 68c5f47 | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 2897 | cast<InvokeInst>(I)->setCallingConv( |
| 2898 | static_cast<CallingConv::ID>(CCInfo)); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 2899 | cast<InvokeInst>(I)->setAttributes(PAL); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 2900 | break; |
| 2901 | } |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 2902 | case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval] |
| 2903 | unsigned Idx = 0; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2904 | Value *Val = nullptr; |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 2905 | if (getValueTypePair(Record, Idx, NextValueNo, Val)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2906 | return Error(BitcodeError::InvalidRecord); |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 2907 | I = ResumeInst::Create(Val); |
Bill Wendling | b9a8999 | 2011-09-01 00:50:20 +0000 | [diff] [blame] | 2908 | InstructionList.push_back(I); |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 2909 | break; |
| 2910 | } |
Chris Lattner | e53603e | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 2911 | case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2912 | I = new UnreachableInst(Context); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2913 | InstructionList.push_back(I); |
Chris Lattner | e53603e | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 2914 | break; |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 2915 | case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...] |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 2916 | if (Record.size() < 1 || ((Record.size()-1)&1)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2917 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2918 | Type *Ty = getTypeByID(Record[0]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2919 | if (!Ty) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2920 | return Error(BitcodeError::InvalidRecord); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2921 | |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 2922 | PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2923 | InstructionList.push_back(PN); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2924 | |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 2925 | for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) { |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 2926 | Value *V; |
| 2927 | // With the new function encoding, it is possible that operands have |
| 2928 | // negative IDs (for forward references). Use a signed VBR |
| 2929 | // representation to keep the encoding small. |
| 2930 | if (UseRelativeIDs) |
| 2931 | V = getValueSigned(Record, 1+i, NextValueNo, Ty); |
| 2932 | else |
| 2933 | V = getValue(Record, 1+i, NextValueNo, Ty); |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 2934 | BasicBlock *BB = getBasicBlock(Record[2+i]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2935 | if (!V || !BB) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2936 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | c332bba | 2007-05-03 18:58:09 +0000 | [diff] [blame] | 2937 | PN->addIncoming(V, BB); |
| 2938 | } |
| 2939 | I = PN; |
| 2940 | break; |
| 2941 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2942 | |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 2943 | case bitc::FUNC_CODE_INST_LANDINGPAD: { |
| 2944 | // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?] |
| 2945 | unsigned Idx = 0; |
| 2946 | if (Record.size() < 4) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2947 | return Error(BitcodeError::InvalidRecord); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 2948 | Type *Ty = getTypeByID(Record[Idx++]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2949 | if (!Ty) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2950 | return Error(BitcodeError::InvalidRecord); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2951 | Value *PersFn = nullptr; |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 2952 | if (getValueTypePair(Record, Idx, NextValueNo, PersFn)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2953 | return Error(BitcodeError::InvalidRecord); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 2954 | |
| 2955 | bool IsCleanup = !!Record[Idx++]; |
| 2956 | unsigned NumClauses = Record[Idx++]; |
| 2957 | LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, NumClauses); |
| 2958 | LP->setCleanup(IsCleanup); |
| 2959 | for (unsigned J = 0; J != NumClauses; ++J) { |
| 2960 | LandingPadInst::ClauseType CT = |
| 2961 | LandingPadInst::ClauseType(Record[Idx++]); (void)CT; |
| 2962 | Value *Val; |
| 2963 | |
| 2964 | if (getValueTypePair(Record, Idx, NextValueNo, Val)) { |
| 2965 | delete LP; |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2966 | return Error(BitcodeError::InvalidRecord); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 2967 | } |
| 2968 | |
| 2969 | assert((CT != LandingPadInst::Catch || |
| 2970 | !isa<ArrayType>(Val->getType())) && |
| 2971 | "Catch clause has a invalid type!"); |
| 2972 | assert((CT != LandingPadInst::Filter || |
| 2973 | isa<ArrayType>(Val->getType())) && |
| 2974 | "Filter clause has invalid type!"); |
Rafael Espindola | 4dc5dfc | 2014-06-04 18:51:31 +0000 | [diff] [blame] | 2975 | LP->addClause(cast<Constant>(Val)); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 2976 | } |
| 2977 | |
| 2978 | I = LP; |
Bill Wendling | b9a8999 | 2011-09-01 00:50:20 +0000 | [diff] [blame] | 2979 | InstructionList.push_back(I); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 2980 | break; |
| 2981 | } |
| 2982 | |
Chris Lattner | f1c8710 | 2011-06-17 18:09:11 +0000 | [diff] [blame] | 2983 | case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align] |
| 2984 | if (Record.size() != 4) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2985 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2986 | PointerType *Ty = |
Chris Lattner | c332bba | 2007-05-03 18:58:09 +0000 | [diff] [blame] | 2987 | dyn_cast_or_null<PointerType>(getTypeByID(Record[0])); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2988 | Type *OpTy = getTypeByID(Record[1]); |
Chris Lattner | f1c8710 | 2011-06-17 18:09:11 +0000 | [diff] [blame] | 2989 | Value *Size = getFnValueByID(Record[2], OpTy); |
Reid Kleckner | 56b56ea | 2014-07-16 01:34:27 +0000 | [diff] [blame] | 2990 | unsigned AlignRecord = Record[3]; |
| 2991 | bool InAlloca = AlignRecord & (1 << 5); |
| 2992 | unsigned Align = AlignRecord & ((1 << 5) - 1); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2993 | if (!Ty || !Size) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 2994 | return Error(BitcodeError::InvalidRecord); |
Reid Kleckner | 56b56ea | 2014-07-16 01:34:27 +0000 | [diff] [blame] | 2995 | AllocaInst *AI = new AllocaInst(Ty->getElementType(), Size, (1 << Align) >> 1); |
| 2996 | AI->setUsedWithInAlloca(InAlloca); |
| 2997 | I = AI; |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2998 | InstructionList.push_back(I); |
Chris Lattner | c332bba | 2007-05-03 18:58:09 +0000 | [diff] [blame] | 2999 | break; |
| 3000 | } |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 3001 | case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol] |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 3002 | unsigned OpNum = 0; |
| 3003 | Value *Op; |
| 3004 | if (getValueTypePair(Record, OpNum, NextValueNo, Op) || |
| 3005 | OpNum+2 != Record.size()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3006 | return Error(BitcodeError::InvalidRecord); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3007 | |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 3008 | I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3009 | InstructionList.push_back(I); |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 3010 | break; |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 3011 | } |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 3012 | case bitc::FUNC_CODE_INST_LOADATOMIC: { |
| 3013 | // LOADATOMIC: [opty, op, align, vol, ordering, synchscope] |
| 3014 | unsigned OpNum = 0; |
| 3015 | Value *Op; |
| 3016 | if (getValueTypePair(Record, OpNum, NextValueNo, Op) || |
| 3017 | OpNum+4 != Record.size()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3018 | return Error(BitcodeError::InvalidRecord); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 3019 | |
| 3020 | AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]); |
| 3021 | if (Ordering == NotAtomic || Ordering == Release || |
| 3022 | Ordering == AcquireRelease) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3023 | return Error(BitcodeError::InvalidRecord); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 3024 | if (Ordering != NotAtomic && Record[OpNum] == 0) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3025 | return Error(BitcodeError::InvalidRecord); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 3026 | SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]); |
| 3027 | |
| 3028 | I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1, |
| 3029 | Ordering, SynchScope); |
| 3030 | InstructionList.push_back(I); |
| 3031 | break; |
| 3032 | } |
Chris Lattner | c4407080 | 2011-06-17 18:17:37 +0000 | [diff] [blame] | 3033 | case bitc::FUNC_CODE_INST_STORE: { // STORE2:[ptrty, ptr, val, align, vol] |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 3034 | unsigned OpNum = 0; |
| 3035 | Value *Val, *Ptr; |
| 3036 | if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3037 | popValue(Record, OpNum, NextValueNo, |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 3038 | cast<PointerType>(Ptr->getType())->getElementType(), Val) || |
| 3039 | OpNum+2 != Record.size()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3040 | return Error(BitcodeError::InvalidRecord); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3041 | |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 3042 | I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3043 | InstructionList.push_back(I); |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 3044 | break; |
| 3045 | } |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 3046 | case bitc::FUNC_CODE_INST_STOREATOMIC: { |
| 3047 | // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, synchscope] |
| 3048 | unsigned OpNum = 0; |
| 3049 | Value *Val, *Ptr; |
| 3050 | if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3051 | popValue(Record, OpNum, NextValueNo, |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 3052 | cast<PointerType>(Ptr->getType())->getElementType(), Val) || |
| 3053 | OpNum+4 != Record.size()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3054 | return Error(BitcodeError::InvalidRecord); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 3055 | |
| 3056 | AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]); |
Eli Friedman | 222b5a4 | 2011-09-19 19:41:28 +0000 | [diff] [blame] | 3057 | if (Ordering == NotAtomic || Ordering == Acquire || |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 3058 | Ordering == AcquireRelease) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3059 | return Error(BitcodeError::InvalidRecord); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 3060 | SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]); |
| 3061 | if (Ordering != NotAtomic && Record[OpNum] == 0) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3062 | return Error(BitcodeError::InvalidRecord); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 3063 | |
| 3064 | I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1, |
| 3065 | Ordering, SynchScope); |
| 3066 | InstructionList.push_back(I); |
| 3067 | break; |
| 3068 | } |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3069 | case bitc::FUNC_CODE_INST_CMPXCHG: { |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 3070 | // CMPXCHG:[ptrty, ptr, cmp, new, vol, successordering, synchscope, |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 3071 | // failureordering?, isweak?] |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3072 | unsigned OpNum = 0; |
| 3073 | Value *Ptr, *Cmp, *New; |
| 3074 | if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3075 | popValue(Record, OpNum, NextValueNo, |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3076 | cast<PointerType>(Ptr->getType())->getElementType(), Cmp) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3077 | popValue(Record, OpNum, NextValueNo, |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3078 | cast<PointerType>(Ptr->getType())->getElementType(), New) || |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 3079 | (Record.size() < OpNum + 3 || Record.size() > OpNum + 5)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3080 | return Error(BitcodeError::InvalidRecord); |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 3081 | AtomicOrdering SuccessOrdering = GetDecodedOrdering(Record[OpNum+1]); |
| 3082 | if (SuccessOrdering == NotAtomic || SuccessOrdering == Unordered) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3083 | return Error(BitcodeError::InvalidRecord); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3084 | SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+2]); |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 3085 | |
| 3086 | AtomicOrdering FailureOrdering; |
| 3087 | if (Record.size() < 7) |
| 3088 | FailureOrdering = |
| 3089 | AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering); |
| 3090 | else |
| 3091 | FailureOrdering = GetDecodedOrdering(Record[OpNum+3]); |
| 3092 | |
| 3093 | I = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering, FailureOrdering, |
| 3094 | SynchScope); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3095 | cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]); |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 3096 | |
| 3097 | if (Record.size() < 8) { |
| 3098 | // Before weak cmpxchgs existed, the instruction simply returned the |
| 3099 | // value loaded from memory, so bitcode files from that era will be |
| 3100 | // expecting the first component of a modern cmpxchg. |
| 3101 | CurBB->getInstList().push_back(I); |
| 3102 | I = ExtractValueInst::Create(I, 0); |
| 3103 | } else { |
| 3104 | cast<AtomicCmpXchgInst>(I)->setWeak(Record[OpNum+4]); |
| 3105 | } |
| 3106 | |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3107 | InstructionList.push_back(I); |
| 3108 | break; |
| 3109 | } |
| 3110 | case bitc::FUNC_CODE_INST_ATOMICRMW: { |
| 3111 | // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, synchscope] |
| 3112 | unsigned OpNum = 0; |
| 3113 | Value *Ptr, *Val; |
| 3114 | if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3115 | popValue(Record, OpNum, NextValueNo, |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3116 | cast<PointerType>(Ptr->getType())->getElementType(), Val) || |
| 3117 | OpNum+4 != Record.size()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3118 | return Error(BitcodeError::InvalidRecord); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3119 | AtomicRMWInst::BinOp Operation = GetDecodedRMWOperation(Record[OpNum]); |
| 3120 | if (Operation < AtomicRMWInst::FIRST_BINOP || |
| 3121 | Operation > AtomicRMWInst::LAST_BINOP) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3122 | return Error(BitcodeError::InvalidRecord); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3123 | AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 3124 | if (Ordering == NotAtomic || Ordering == Unordered) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3125 | return Error(BitcodeError::InvalidRecord); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3126 | SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]); |
| 3127 | I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SynchScope); |
| 3128 | cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]); |
| 3129 | InstructionList.push_back(I); |
| 3130 | break; |
| 3131 | } |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 3132 | case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, synchscope] |
| 3133 | if (2 != Record.size()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3134 | return Error(BitcodeError::InvalidRecord); |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 3135 | AtomicOrdering Ordering = GetDecodedOrdering(Record[0]); |
| 3136 | if (Ordering == NotAtomic || Ordering == Unordered || |
| 3137 | Ordering == Monotonic) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3138 | return Error(BitcodeError::InvalidRecord); |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 3139 | SynchronizationScope SynchScope = GetDecodedSynchScope(Record[1]); |
| 3140 | I = new FenceInst(Context, Ordering, SynchScope); |
| 3141 | InstructionList.push_back(I); |
| 3142 | break; |
| 3143 | } |
Chris Lattner | c4407080 | 2011-06-17 18:17:37 +0000 | [diff] [blame] | 3144 | case bitc::FUNC_CODE_INST_CALL: { |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3145 | // CALL: [paramattrs, cc, fnty, fnid, arg0, arg1...] |
| 3146 | if (Record.size() < 3) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3147 | return Error(BitcodeError::InvalidRecord); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3148 | |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 3149 | AttributeSet PAL = getAttributes(Record[0]); |
Chris Lattner | 4c0a6d6 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 3150 | unsigned CCInfo = Record[1]; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3151 | |
Chris Lattner | 4c0a6d6 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 3152 | unsigned OpNum = 2; |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 3153 | Value *Callee; |
| 3154 | if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3155 | return Error(BitcodeError::InvalidRecord); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3156 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3157 | PointerType *OpTy = dyn_cast<PointerType>(Callee->getType()); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3158 | FunctionType *FTy = nullptr; |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 3159 | if (OpTy) FTy = dyn_cast<FunctionType>(OpTy->getElementType()); |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 3160 | if (!FTy || Record.size() < FTy->getNumParams()+OpNum) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3161 | return Error(BitcodeError::InvalidRecord); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3162 | |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 3163 | SmallVector<Value*, 16> Args; |
| 3164 | // Read the fixed params. |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 3165 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3166 | if (FTy->getParamType(i)->isLabelTy()) |
Dale Johannesen | 4646aa3 | 2007-11-05 21:20:28 +0000 | [diff] [blame] | 3167 | Args.push_back(getBasicBlock(Record[OpNum])); |
Dan Gohman | bbcd04d | 2010-09-13 18:00:48 +0000 | [diff] [blame] | 3168 | else |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3169 | Args.push_back(getValue(Record, OpNum, NextValueNo, |
| 3170 | FTy->getParamType(i))); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3171 | if (!Args.back()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3172 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 3173 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3174 | |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 3175 | // Read type/value pairs for varargs params. |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 3176 | if (!FTy->isVarArg()) { |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 3177 | if (OpNum != Record.size()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3178 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 3179 | } else { |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 3180 | while (OpNum != Record.size()) { |
| 3181 | Value *Op; |
| 3182 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3183 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 3184 | Args.push_back(Op); |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 3185 | } |
| 3186 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3187 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 3188 | I = CallInst::Create(Callee, Args); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3189 | InstructionList.push_back(I); |
Sandeep Patel | 68c5f47 | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 3190 | cast<CallInst>(I)->setCallingConv( |
Reid Kleckner | 5772b77 | 2014-04-24 20:14:34 +0000 | [diff] [blame] | 3191 | static_cast<CallingConv::ID>((~(1U << 14) & CCInfo) >> 1)); |
| 3192 | CallInst::TailCallKind TCK = CallInst::TCK_None; |
| 3193 | if (CCInfo & 1) |
| 3194 | TCK = CallInst::TCK_Tail; |
| 3195 | if (CCInfo & (1 << 14)) |
| 3196 | TCK = CallInst::TCK_MustTail; |
| 3197 | cast<CallInst>(I)->setTailCallKind(TCK); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 3198 | cast<CallInst>(I)->setAttributes(PAL); |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 3199 | break; |
| 3200 | } |
| 3201 | case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty] |
| 3202 | if (Record.size() < 3) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3203 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3204 | Type *OpTy = getTypeByID(Record[0]); |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3205 | Value *Op = getValue(Record, 1, NextValueNo, OpTy); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3206 | Type *ResTy = getTypeByID(Record[2]); |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 3207 | if (!OpTy || !Op || !ResTy) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3208 | return Error(BitcodeError::InvalidRecord); |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 3209 | I = new VAArgInst(Op, ResTy); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3210 | InstructionList.push_back(I); |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 3211 | break; |
| 3212 | } |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 3213 | } |
| 3214 | |
| 3215 | // Add instruction to end of current BB. If there is no current BB, reject |
| 3216 | // this file. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3217 | if (!CurBB) { |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 3218 | delete I; |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3219 | return Error(BitcodeError::InvalidInstructionWithNoBB); |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 3220 | } |
| 3221 | CurBB->getInstList().push_back(I); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3222 | |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 3223 | // If this was a terminator instruction, move to the next block. |
| 3224 | if (isa<TerminatorInst>(I)) { |
| 3225 | ++CurBBNo; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3226 | CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr; |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 3227 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3228 | |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 3229 | // Non-void values get registered in the value table for future use. |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 3230 | if (I && !I->getType()->isVoidTy()) |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 3231 | ValueList.AssignValue(I, NextValueNo++); |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3232 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3233 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3234 | OutOfRecordLoop: |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3235 | |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 3236 | // Check the function list for unresolved values. |
| 3237 | if (Argument *A = dyn_cast<Argument>(ValueList.back())) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3238 | if (!A->getParent()) { |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 3239 | // We found at least one unresolved value. Nuke them all to avoid leaks. |
| 3240 | for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){ |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3241 | if ((A = dyn_cast_or_null<Argument>(ValueList[i])) && !A->getParent()) { |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 3242 | A->replaceAllUsesWith(UndefValue::get(A->getType())); |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 3243 | delete A; |
| 3244 | } |
| 3245 | } |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3246 | return Error(BitcodeError::NeverResolvedValueFoundInFunction); |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 3247 | } |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 3248 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3249 | |
Dan Gohman | 9b9ff46 | 2010-08-25 20:23:38 +0000 | [diff] [blame] | 3250 | // FIXME: Check for unresolved forward-declared metadata references |
| 3251 | // and clean up leaks. |
| 3252 | |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3253 | // Trim the value list down to the size it was before we parsed this function. |
| 3254 | ValueList.shrinkTo(ModuleValueListSize); |
Dan Gohman | 26d837d | 2010-08-25 20:22:53 +0000 | [diff] [blame] | 3255 | MDValueList.shrinkTo(ModuleMDValueListSize); |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3256 | std::vector<BasicBlock*>().swap(FunctionBBs); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3257 | return std::error_code(); |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 3258 | } |
| 3259 | |
Rafael Espindola | 7d71203 | 2013-11-05 17:16:08 +0000 | [diff] [blame] | 3260 | /// Find the function body in the bitcode stream |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3261 | std::error_code BitcodeReader::FindFunctionInStream( |
| 3262 | Function *F, |
| 3263 | DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) { |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3264 | while (DeferredFunctionInfoIterator->second == 0) { |
| 3265 | if (Stream.AtEndOfStream()) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3266 | return Error(BitcodeError::CouldNotFindFunctionInStream); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3267 | // ParseModule will parse the next body in the stream and set its |
| 3268 | // position in the DeferredFunctionInfo map. |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3269 | if (std::error_code EC = ParseModule(true)) |
Rafael Espindola | 7d71203 | 2013-11-05 17:16:08 +0000 | [diff] [blame] | 3270 | return EC; |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3271 | } |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3272 | return std::error_code(); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3273 | } |
| 3274 | |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 3275 | //===----------------------------------------------------------------------===// |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 3276 | // GVMaterializer implementation |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 3277 | //===----------------------------------------------------------------------===// |
| 3278 | |
Rafael Espindola | c3f9b5a | 2014-06-23 21:53:12 +0000 | [diff] [blame] | 3279 | void BitcodeReader::releaseBuffer() { Buffer.release(); } |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 3280 | |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 3281 | bool BitcodeReader::isMaterializable(const GlobalValue *GV) const { |
| 3282 | if (const Function *F = dyn_cast<Function>(GV)) { |
| 3283 | return F->isDeclaration() && |
| 3284 | DeferredFunctionInfo.count(const_cast<Function*>(F)); |
| 3285 | } |
| 3286 | return false; |
| 3287 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3288 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3289 | std::error_code BitcodeReader::Materialize(GlobalValue *GV) { |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 3290 | Function *F = dyn_cast<Function>(GV); |
| 3291 | // If it's not a function or is already material, ignore the request. |
Rafael Espindola | 2b11ad4 | 2013-11-05 19:36:34 +0000 | [diff] [blame] | 3292 | if (!F || !F->isMaterializable()) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3293 | return std::error_code(); |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 3294 | |
| 3295 | DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F); |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 3296 | assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!"); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3297 | // If its position is recorded as 0, its body is somewhere in the stream |
| 3298 | // but we haven't seen it yet. |
Rafael Espindola | 2b11ad4 | 2013-11-05 19:36:34 +0000 | [diff] [blame] | 3299 | if (DFII->second == 0 && LazyStreamer) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3300 | if (std::error_code EC = FindFunctionInStream(F, DFII)) |
Rafael Espindola | 2b11ad4 | 2013-11-05 19:36:34 +0000 | [diff] [blame] | 3301 | return EC; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3302 | |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 3303 | // Move the bit stream to the saved position of the deferred function body. |
| 3304 | Stream.JumpToBit(DFII->second); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3305 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3306 | if (std::error_code EC = ParseFunctionBody(F)) |
Rafael Espindola | 2b11ad4 | 2013-11-05 19:36:34 +0000 | [diff] [blame] | 3307 | return EC; |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 3308 | |
| 3309 | // Upgrade any old intrinsic calls in the function. |
| 3310 | for (UpgradedIntrinsicMap::iterator I = UpgradedIntrinsics.begin(), |
| 3311 | E = UpgradedIntrinsics.end(); I != E; ++I) { |
| 3312 | if (I->first != I->second) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3313 | for (auto UI = I->first->user_begin(), UE = I->first->user_end(); |
| 3314 | UI != UE;) { |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 3315 | if (CallInst* CI = dyn_cast<CallInst>(*UI++)) |
| 3316 | UpgradeIntrinsicCall(CI, I->second); |
| 3317 | } |
| 3318 | } |
| 3319 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3320 | |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 3321 | // Bring in any functions that this function forward-referenced via |
| 3322 | // blockaddresses. |
| 3323 | return materializeForwardReferencedFunctions(); |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 3324 | } |
| 3325 | |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 3326 | bool BitcodeReader::isDematerializable(const GlobalValue *GV) const { |
| 3327 | const Function *F = dyn_cast<Function>(GV); |
| 3328 | if (!F || F->isDeclaration()) |
| 3329 | return false; |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 3330 | |
| 3331 | // Dematerializing F would leave dangling references that wouldn't be |
| 3332 | // reconnected on re-materialization. |
| 3333 | if (BlockAddressesTaken.count(F)) |
| 3334 | return false; |
| 3335 | |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 3336 | return DeferredFunctionInfo.count(const_cast<Function*>(F)); |
| 3337 | } |
| 3338 | |
| 3339 | void BitcodeReader::Dematerialize(GlobalValue *GV) { |
| 3340 | Function *F = dyn_cast<Function>(GV); |
| 3341 | // If this function isn't dematerializable, this is a noop. |
| 3342 | if (!F || !isDematerializable(F)) |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 3343 | return; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3344 | |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 3345 | assert(DeferredFunctionInfo.count(F) && "No info to read function later?"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3346 | |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 3347 | // Just forget the function body, we can remat it later. |
| 3348 | F->deleteBody(); |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 3349 | } |
| 3350 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3351 | std::error_code BitcodeReader::MaterializeModule(Module *M) { |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 3352 | assert(M == TheModule && |
| 3353 | "Can only Materialize the Module this BitcodeReader is attached to."); |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 3354 | |
| 3355 | // Promise to materialize all forward references. |
| 3356 | WillMaterializeAllForwardRefs = true; |
| 3357 | |
Chris Lattner | 06310bf | 2009-06-16 05:15:21 +0000 | [diff] [blame] | 3358 | // Iterate over the module, deserializing any functions that are still on |
| 3359 | // disk. |
| 3360 | for (Module::iterator F = TheModule->begin(), E = TheModule->end(); |
Rafael Espindola | 2b11ad4 | 2013-11-05 19:36:34 +0000 | [diff] [blame] | 3361 | F != E; ++F) { |
| 3362 | if (F->isMaterializable()) { |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3363 | if (std::error_code EC = Materialize(F)) |
Rafael Espindola | 2b11ad4 | 2013-11-05 19:36:34 +0000 | [diff] [blame] | 3364 | return EC; |
| 3365 | } |
| 3366 | } |
Derek Schuff | 92ef975 | 2012-02-29 00:07:09 +0000 | [diff] [blame] | 3367 | // At this point, if there are any function bodies, the current bit is |
| 3368 | // pointing to the END_BLOCK record after them. Now make sure the rest |
| 3369 | // of the bits in the module have been read. |
| 3370 | if (NextUnreadBit) |
| 3371 | ParseModule(true); |
| 3372 | |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 3373 | // Check that all block address forward references got resolved (as we |
| 3374 | // promised above). |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 3375 | if (!BasicBlockFwdRefs.empty()) |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 3376 | return Error(BitcodeError::NeverResolvedFunctionFromBlockAddress); |
| 3377 | |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3378 | // Upgrade any intrinsic calls that slipped through (should not happen!) and |
| 3379 | // delete the old functions to clean up. We can't do this unless the entire |
| 3380 | // module is materialized because there could always be another function body |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 3381 | // with calls to the old function. |
| 3382 | for (std::vector<std::pair<Function*, Function*> >::iterator I = |
| 3383 | UpgradedIntrinsics.begin(), E = UpgradedIntrinsics.end(); I != E; ++I) { |
| 3384 | if (I->first != I->second) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3385 | for (auto UI = I->first->user_begin(), UE = I->first->user_end(); |
| 3386 | UI != UE;) { |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 3387 | if (CallInst* CI = dyn_cast<CallInst>(*UI++)) |
| 3388 | UpgradeIntrinsicCall(CI, I->second); |
| 3389 | } |
Chris Lattner | 647cffb | 2009-04-01 01:43:03 +0000 | [diff] [blame] | 3390 | if (!I->first->use_empty()) |
| 3391 | I->first->replaceAllUsesWith(I->second); |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 3392 | I->first->eraseFromParent(); |
| 3393 | } |
| 3394 | } |
| 3395 | std::vector<std::pair<Function*, Function*> >().swap(UpgradedIntrinsics); |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 3396 | |
Manman Ren | 209b17c | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 3397 | for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++) |
| 3398 | UpgradeInstWithTBAATag(InstsWithTBAATag[I]); |
| 3399 | |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 3400 | UpgradeDebugInfo(*M); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3401 | return std::error_code(); |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 3402 | } |
| 3403 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3404 | std::error_code BitcodeReader::InitStream() { |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3405 | if (LazyStreamer) |
| 3406 | return InitLazyStream(); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3407 | return InitStreamFromBuffer(); |
| 3408 | } |
| 3409 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3410 | std::error_code BitcodeReader::InitStreamFromBuffer() { |
Roman Divacky | 4717a8d | 2012-09-06 15:42:13 +0000 | [diff] [blame] | 3411 | const unsigned char *BufPtr = (const unsigned char*)Buffer->getBufferStart(); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3412 | const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize(); |
| 3413 | |
Rafael Espindola | 2743525 | 2014-07-29 21:01:24 +0000 | [diff] [blame] | 3414 | if (Buffer->getBufferSize() & 3) |
| 3415 | return Error(BitcodeError::InvalidBitcodeSignature); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3416 | |
| 3417 | // If we have a wrapper header, parse it and ignore the non-bc file contents. |
| 3418 | // The magic number is 0x0B17C0DE stored in little endian. |
| 3419 | if (isBitcodeWrapper(BufPtr, BufEnd)) |
| 3420 | if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3421 | return Error(BitcodeError::InvalidBitcodeWrapperHeader); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3422 | |
| 3423 | StreamFile.reset(new BitstreamReader(BufPtr, BufEnd)); |
| 3424 | Stream.init(*StreamFile); |
| 3425 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3426 | return std::error_code(); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3427 | } |
| 3428 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3429 | std::error_code BitcodeReader::InitLazyStream() { |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3430 | // Check and strip off the bitcode wrapper; BitstreamReader expects never to |
| 3431 | // see it. |
| 3432 | StreamingMemoryObject *Bytes = new StreamingMemoryObject(LazyStreamer); |
| 3433 | StreamFile.reset(new BitstreamReader(Bytes)); |
| 3434 | Stream.init(*StreamFile); |
| 3435 | |
| 3436 | unsigned char buf[16]; |
Benjamin Kramer | 534d3a4 | 2013-05-24 10:54:58 +0000 | [diff] [blame] | 3437 | if (Bytes->readBytes(0, 16, buf) == -1) |
Rafael Espindola | 2743525 | 2014-07-29 21:01:24 +0000 | [diff] [blame] | 3438 | return Error(BitcodeError::InvalidBitcodeSignature); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3439 | |
| 3440 | if (!isBitcode(buf, buf + 16)) |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3441 | return Error(BitcodeError::InvalidBitcodeSignature); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3442 | |
| 3443 | if (isBitcodeWrapper(buf, buf + 4)) { |
| 3444 | const unsigned char *bitcodeStart = buf; |
| 3445 | const unsigned char *bitcodeEnd = buf + 16; |
| 3446 | SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false); |
| 3447 | Bytes->dropLeadingBytes(bitcodeStart - buf); |
| 3448 | Bytes->setKnownObjectSize(bitcodeEnd - bitcodeStart); |
| 3449 | } |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3450 | return std::error_code(); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3451 | } |
| 3452 | |
| 3453 | namespace { |
Rafael Espindola | 25188c9 | 2014-06-12 01:45:43 +0000 | [diff] [blame] | 3454 | class BitcodeErrorCategoryType : public std::error_category { |
Rafael Espindola | f5d07fa | 2014-06-10 21:26:47 +0000 | [diff] [blame] | 3455 | const char *name() const LLVM_NOEXCEPT override { |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3456 | return "llvm.bitcode"; |
| 3457 | } |
Craig Topper | 7315602 | 2014-03-02 09:09:27 +0000 | [diff] [blame] | 3458 | std::string message(int IE) const override { |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3459 | BitcodeError E = static_cast<BitcodeError>(IE); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3460 | switch (E) { |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3461 | case BitcodeError::ConflictingMETADATA_KINDRecords: |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3462 | return "Conflicting METADATA_KIND records"; |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3463 | case BitcodeError::CouldNotFindFunctionInStream: |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3464 | return "Could not find function in stream"; |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3465 | case BitcodeError::ExpectedConstant: |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3466 | return "Expected a constant"; |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3467 | case BitcodeError::InsufficientFunctionProtos: |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3468 | return "Insufficient function protos"; |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3469 | case BitcodeError::InvalidBitcodeSignature: |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3470 | return "Invalid bitcode signature"; |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3471 | case BitcodeError::InvalidBitcodeWrapperHeader: |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3472 | return "Invalid bitcode wrapper header"; |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3473 | case BitcodeError::InvalidConstantReference: |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3474 | return "Invalid ronstant reference"; |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3475 | case BitcodeError::InvalidID: |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3476 | return "Invalid ID"; |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3477 | case BitcodeError::InvalidInstructionWithNoBB: |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3478 | return "Invalid instruction with no BB"; |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3479 | case BitcodeError::InvalidRecord: |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3480 | return "Invalid record"; |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3481 | case BitcodeError::InvalidTypeForValue: |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3482 | return "Invalid type for value"; |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3483 | case BitcodeError::InvalidTYPETable: |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3484 | return "Invalid TYPE table"; |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3485 | case BitcodeError::InvalidType: |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3486 | return "Invalid type"; |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3487 | case BitcodeError::MalformedBlock: |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3488 | return "Malformed block"; |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3489 | case BitcodeError::MalformedGlobalInitializerSet: |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3490 | return "Malformed global initializer set"; |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3491 | case BitcodeError::InvalidMultipleBlocks: |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3492 | return "Invalid multiple blocks"; |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3493 | case BitcodeError::NeverResolvedValueFoundInFunction: |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3494 | return "Never resolved value found in function"; |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 3495 | case BitcodeError::NeverResolvedFunctionFromBlockAddress: |
| 3496 | return "Never resolved function from blockaddress"; |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3497 | case BitcodeError::InvalidValue: |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3498 | return "Invalid value"; |
| 3499 | } |
Benjamin Kramer | 77db163 | 2013-11-05 13:45:09 +0000 | [diff] [blame] | 3500 | llvm_unreachable("Unknown error type!"); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3501 | } |
| 3502 | }; |
| 3503 | } |
| 3504 | |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 3505 | const std::error_category &llvm::BitcodeErrorCategory() { |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3506 | static BitcodeErrorCategoryType O; |
| 3507 | return O; |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3508 | } |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 3509 | |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 3510 | //===----------------------------------------------------------------------===// |
| 3511 | // External interface |
| 3512 | //===----------------------------------------------------------------------===// |
| 3513 | |
Duncan P. N. Exon Smith | 6e1009b | 2014-08-01 22:27:19 +0000 | [diff] [blame] | 3514 | /// \brief Get a lazy one-at-time loading module from bitcode. |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 3515 | /// |
Duncan P. N. Exon Smith | 6e1009b | 2014-08-01 22:27:19 +0000 | [diff] [blame] | 3516 | /// This isn't always used in a lazy context. In particular, it's also used by |
| 3517 | /// \a parseBitcodeFile(). If this is truly lazy, then we need to eagerly pull |
| 3518 | /// in forward-referenced functions from block address references. |
| 3519 | /// |
| 3520 | /// \param[in] WillMaterializeAll Set to \c true if the caller promises to |
| 3521 | /// materialize everything -- in particular, if this isn't truly lazy. |
| 3522 | static ErrorOr<Module *> getLazyBitcodeModuleImpl(MemoryBuffer *Buffer, |
| 3523 | LLVMContext &Context, |
| 3524 | bool WillMaterializeAll) { |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 3525 | Module *M = new Module(Buffer->getBufferIdentifier(), Context); |
Rafael Espindola | c3f9b5a | 2014-06-23 21:53:12 +0000 | [diff] [blame] | 3526 | BitcodeReader *R = new BitcodeReader(Buffer, Context); |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 3527 | M->setMaterializer(R); |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 3528 | |
| 3529 | auto cleanupOnError = [&](std::error_code EC) { |
Rafael Espindola | 8fb3111 | 2014-06-18 20:07:35 +0000 | [diff] [blame] | 3530 | R->releaseBuffer(); // Never take ownership on error. |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 3531 | delete M; // Also deletes R. |
Rafael Espindola | 5b6c1e8 | 2014-01-13 18:31:04 +0000 | [diff] [blame] | 3532 | return EC; |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 3533 | }; |
Rafael Espindola | b799346 | 2012-01-02 07:49:53 +0000 | [diff] [blame] | 3534 | |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 3535 | if (std::error_code EC = R->ParseBitcodeInto(M)) |
| 3536 | return cleanupOnError(EC); |
| 3537 | |
Duncan P. N. Exon Smith | 6e1009b | 2014-08-01 22:27:19 +0000 | [diff] [blame] | 3538 | if (!WillMaterializeAll) |
| 3539 | // Resolve forward references from blockaddresses. |
| 3540 | if (std::error_code EC = R->materializeForwardReferencedFunctions()) |
| 3541 | return cleanupOnError(EC); |
Rafael Espindola | b799346 | 2012-01-02 07:49:53 +0000 | [diff] [blame] | 3542 | |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 3543 | return M; |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 3544 | } |
| 3545 | |
Duncan P. N. Exon Smith | 6e1009b | 2014-08-01 22:27:19 +0000 | [diff] [blame] | 3546 | ErrorOr<Module *> llvm::getLazyBitcodeModule(MemoryBuffer *Buffer, |
| 3547 | LLVMContext &Context) { |
| 3548 | return getLazyBitcodeModuleImpl(Buffer, Context, false); |
| 3549 | } |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3550 | |
| 3551 | Module *llvm::getStreamedBitcodeModule(const std::string &name, |
| 3552 | DataStreamer *streamer, |
| 3553 | LLVMContext &Context, |
| 3554 | std::string *ErrMsg) { |
| 3555 | Module *M = new Module(name, Context); |
| 3556 | BitcodeReader *R = new BitcodeReader(streamer, Context); |
| 3557 | M->setMaterializer(R); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3558 | if (std::error_code EC = R->ParseBitcodeInto(M)) { |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3559 | if (ErrMsg) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3560 | *ErrMsg = EC.message(); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3561 | delete M; // Also deletes R. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3562 | return nullptr; |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3563 | } |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3564 | return M; |
| 3565 | } |
| 3566 | |
Alp Toker | de4c009 | 2014-06-27 09:19:14 +0000 | [diff] [blame] | 3567 | ErrorOr<Module *> llvm::parseBitcodeFile(MemoryBuffer *Buffer, |
Rafael Espindola | 8f31e21 | 2014-01-15 01:08:23 +0000 | [diff] [blame] | 3568 | LLVMContext &Context) { |
Duncan P. N. Exon Smith | 6e1009b | 2014-08-01 22:27:19 +0000 | [diff] [blame] | 3569 | ErrorOr<Module *> ModuleOrErr = |
| 3570 | getLazyBitcodeModuleImpl(Buffer, Context, true); |
Rafael Espindola | 8f31e21 | 2014-01-15 01:08:23 +0000 | [diff] [blame] | 3571 | if (!ModuleOrErr) |
| 3572 | return ModuleOrErr; |
Rafael Espindola | 5b6c1e8 | 2014-01-13 18:31:04 +0000 | [diff] [blame] | 3573 | Module *M = ModuleOrErr.get(); |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 3574 | // Read in the entire module, and destroy the BitcodeReader. |
Rafael Espindola | c3f9b5a | 2014-06-23 21:53:12 +0000 | [diff] [blame] | 3575 | if (std::error_code EC = M->materializeAllPermanently(true)) { |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 3576 | delete M; |
Rafael Espindola | 8f31e21 | 2014-01-15 01:08:23 +0000 | [diff] [blame] | 3577 | return EC; |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 3578 | } |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3579 | |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 3580 | // TODO: Restore the use-lists to the in-memory state when the bitcode was |
| 3581 | // written. We must defer until the Module has been fully materialized. |
| 3582 | |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 3583 | return M; |
| 3584 | } |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3585 | |
Rafael Espindola | c75c4fa | 2014-07-04 20:02:42 +0000 | [diff] [blame] | 3586 | std::string llvm::getBitcodeTargetTriple(MemoryBuffer *Buffer, |
| 3587 | LLVMContext &Context) { |
Rafael Espindola | c3f9b5a | 2014-06-23 21:53:12 +0000 | [diff] [blame] | 3588 | BitcodeReader *R = new BitcodeReader(Buffer, Context); |
Rafael Espindola | c75c4fa | 2014-07-04 20:02:42 +0000 | [diff] [blame] | 3589 | ErrorOr<std::string> Triple = R->parseTriple(); |
Rafael Espindola | c3f9b5a | 2014-06-23 21:53:12 +0000 | [diff] [blame] | 3590 | R->releaseBuffer(); |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3591 | delete R; |
Rafael Espindola | d346cc8 | 2014-07-04 13:52:01 +0000 | [diff] [blame] | 3592 | if (Triple.getError()) |
| 3593 | return ""; |
| 3594 | return Triple.get(); |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3595 | } |