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