Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1 | //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 9 | |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 10 | #include "llvm/Bitcode/ReaderWriter.h" |
Benjamin Kramer | 0a446fd | 2015-03-01 21:28:53 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/SmallString.h" |
| 13 | #include "llvm/ADT/SmallVector.h" |
David Majnemer | 3087b22 | 2015-01-20 05:58:07 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/Triple.h" |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 15 | #include "llvm/Bitcode/BitstreamReader.h" |
Tobias Grosser | 0a8e12f | 2013-07-26 04:16:55 +0000 | [diff] [blame] | 16 | #include "llvm/Bitcode/LLVMBitCodes.h" |
Chandler Carruth | 9106521 | 2014-03-05 10:34:14 +0000 | [diff] [blame] | 17 | #include "llvm/IR/AutoUpgrade.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Constants.h" |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 19 | #include "llvm/IR/DebugInfo.h" |
Duncan P. N. Exon Smith | d9901ff | 2015-02-02 18:53:21 +0000 | [diff] [blame] | 20 | #include "llvm/IR/DebugInfoMetadata.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DerivedTypes.h" |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 22 | #include "llvm/IR/DiagnosticPrinter.h" |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 23 | #include "llvm/IR/GVMaterializer.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/InlineAsm.h" |
| 25 | #include "llvm/IR/IntrinsicInst.h" |
Manman Ren | 209b17c | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 26 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Module.h" |
| 28 | #include "llvm/IR/OperandTraits.h" |
| 29 | #include "llvm/IR/Operator.h" |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 30 | #include "llvm/IR/FunctionInfo.h" |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 31 | #include "llvm/IR/ValueHandle.h" |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 32 | #include "llvm/Support/DataStream.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 33 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 34 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 35 | #include "llvm/Support/MemoryBuffer.h" |
Tobias Grosser | 0a8e12f | 2013-07-26 04:16:55 +0000 | [diff] [blame] | 36 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 37 | #include <deque> |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 39 | using namespace llvm; |
| 40 | |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 41 | namespace { |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 42 | enum { |
| 43 | SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex |
| 44 | }; |
| 45 | |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 46 | class BitcodeReaderValueList { |
| 47 | std::vector<WeakVH> ValuePtrs; |
| 48 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 49 | /// As we resolve forward-referenced constants, we add information about them |
| 50 | /// to this vector. This allows us to resolve them in bulk instead of |
| 51 | /// resolving each reference at a time. See the code in |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 52 | /// ResolveConstantForwardRefs for more information about this. |
| 53 | /// |
| 54 | /// The key of this vector is the placeholder constant, the value is the slot |
| 55 | /// number that holds the resolved value. |
| 56 | typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy; |
| 57 | ResolveConstantsTy ResolveConstants; |
| 58 | LLVMContext &Context; |
| 59 | public: |
| 60 | BitcodeReaderValueList(LLVMContext &C) : Context(C) {} |
| 61 | ~BitcodeReaderValueList() { |
| 62 | assert(ResolveConstants.empty() && "Constants not resolved?"); |
| 63 | } |
| 64 | |
| 65 | // vector compatibility methods |
| 66 | unsigned size() const { return ValuePtrs.size(); } |
| 67 | void resize(unsigned N) { ValuePtrs.resize(N); } |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 68 | void push_back(Value *V) { ValuePtrs.emplace_back(V); } |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 69 | |
| 70 | void clear() { |
| 71 | assert(ResolveConstants.empty() && "Constants not resolved?"); |
| 72 | ValuePtrs.clear(); |
| 73 | } |
| 74 | |
| 75 | Value *operator[](unsigned i) const { |
| 76 | assert(i < ValuePtrs.size()); |
| 77 | return ValuePtrs[i]; |
| 78 | } |
| 79 | |
| 80 | Value *back() const { return ValuePtrs.back(); } |
| 81 | void pop_back() { ValuePtrs.pop_back(); } |
| 82 | bool empty() const { return ValuePtrs.empty(); } |
| 83 | void shrinkTo(unsigned N) { |
| 84 | assert(N <= size() && "Invalid shrinkTo request!"); |
| 85 | ValuePtrs.resize(N); |
| 86 | } |
| 87 | |
| 88 | Constant *getConstantFwdRef(unsigned Idx, Type *Ty); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 89 | Value *getValueFwdRef(unsigned Idx, Type *Ty); |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 90 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 91 | void assignValue(Value *V, unsigned Idx); |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 92 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 93 | /// Once all constants are read, this method bulk resolves any forward |
| 94 | /// references. |
| 95 | void resolveConstantForwardRefs(); |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 98 | class BitcodeReaderMetadataList { |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 99 | unsigned NumFwdRefs; |
| 100 | bool AnyFwdRefs; |
| 101 | unsigned MinFwdRef; |
| 102 | unsigned MaxFwdRef; |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 103 | std::vector<TrackingMDRef> MetadataPtrs; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 104 | |
| 105 | LLVMContext &Context; |
| 106 | public: |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 107 | BitcodeReaderMetadataList(LLVMContext &C) |
Teresa Johnson | 3470295 | 2015-12-21 15:38:13 +0000 | [diff] [blame] | 108 | : NumFwdRefs(0), AnyFwdRefs(false), Context(C) {} |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 109 | |
| 110 | // vector compatibility methods |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 111 | unsigned size() const { return MetadataPtrs.size(); } |
| 112 | void resize(unsigned N) { MetadataPtrs.resize(N); } |
| 113 | void push_back(Metadata *MD) { MetadataPtrs.emplace_back(MD); } |
| 114 | void clear() { MetadataPtrs.clear(); } |
| 115 | Metadata *back() const { return MetadataPtrs.back(); } |
| 116 | void pop_back() { MetadataPtrs.pop_back(); } |
| 117 | bool empty() const { return MetadataPtrs.empty(); } |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 118 | |
| 119 | Metadata *operator[](unsigned i) const { |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 120 | assert(i < MetadataPtrs.size()); |
| 121 | return MetadataPtrs[i]; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void shrinkTo(unsigned N) { |
| 125 | assert(N <= size() && "Invalid shrinkTo request!"); |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 126 | MetadataPtrs.resize(N); |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | Metadata *getValueFwdRef(unsigned Idx); |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 130 | void assignValue(Metadata *MD, unsigned Idx); |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 131 | void tryToResolveCycles(); |
| 132 | }; |
| 133 | |
| 134 | class BitcodeReader : public GVMaterializer { |
| 135 | LLVMContext &Context; |
Rafael Espindola | 4223a1f | 2015-06-15 20:08:17 +0000 | [diff] [blame] | 136 | Module *TheModule = nullptr; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 137 | std::unique_ptr<MemoryBuffer> Buffer; |
| 138 | std::unique_ptr<BitstreamReader> StreamFile; |
| 139 | BitstreamCursor Stream; |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 140 | // Next offset to start scanning for lazy parsing of function bodies. |
Rafael Espindola | 4223a1f | 2015-06-15 20:08:17 +0000 | [diff] [blame] | 141 | uint64_t NextUnreadBit = 0; |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 142 | // Last function offset found in the VST. |
| 143 | uint64_t LastFunctionBlockBit = 0; |
Rafael Espindola | 4223a1f | 2015-06-15 20:08:17 +0000 | [diff] [blame] | 144 | bool SeenValueSymbolTable = false; |
Peter Collingbourne | 128a976 | 2015-10-27 23:01:25 +0000 | [diff] [blame] | 145 | uint64_t VSTOffset = 0; |
Mehdi Amini | 5d30328 | 2015-10-26 18:37:00 +0000 | [diff] [blame] | 146 | // Contains an arbitrary and optional string identifying the bitcode producer |
| 147 | std::string ProducerIdentification; |
Teresa Johnson | d4d3dfd | 2015-11-20 14:51:27 +0000 | [diff] [blame] | 148 | // Number of module level metadata records specified by the |
| 149 | // MODULE_CODE_METADATA_VALUES record. |
| 150 | unsigned NumModuleMDs = 0; |
| 151 | // Support older bitcode without the MODULE_CODE_METADATA_VALUES record. |
| 152 | bool SeenModuleValuesRecord = false; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 153 | |
| 154 | std::vector<Type*> TypeList; |
| 155 | BitcodeReaderValueList ValueList; |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 156 | BitcodeReaderMetadataList MetadataList; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 157 | std::vector<Comdat *> ComdatList; |
| 158 | SmallVector<Instruction *, 64> InstructionList; |
| 159 | |
| 160 | std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits; |
| 161 | std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits; |
| 162 | std::vector<std::pair<Function*, unsigned> > FunctionPrefixes; |
| 163 | std::vector<std::pair<Function*, unsigned> > FunctionPrologues; |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 164 | std::vector<std::pair<Function*, unsigned> > FunctionPersonalityFns; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 165 | |
| 166 | SmallVector<Instruction*, 64> InstsWithTBAATag; |
| 167 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 168 | /// The set of attributes by index. Index zero in the file is for null, and |
| 169 | /// is thus not represented here. As such all indices are off by one. |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 170 | std::vector<AttributeSet> MAttributes; |
| 171 | |
Karl Schimpf | 3644008 | 2015-08-31 16:43:55 +0000 | [diff] [blame] | 172 | /// The set of attribute groups. |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 173 | std::map<unsigned, AttributeSet> MAttributeGroups; |
| 174 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 175 | /// While parsing a function body, this is a list of the basic blocks for the |
| 176 | /// function. |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 177 | std::vector<BasicBlock*> FunctionBBs; |
| 178 | |
| 179 | // When reading the module header, this list is populated with functions that |
| 180 | // have bodies later in the file. |
| 181 | std::vector<Function*> FunctionsWithBodies; |
| 182 | |
| 183 | // When intrinsic functions are encountered which require upgrading they are |
| 184 | // stored here with their replacement function. |
Rafael Espindola | 4e72121 | 2015-07-02 16:22:40 +0000 | [diff] [blame] | 185 | typedef DenseMap<Function*, Function*> UpgradedIntrinsicMap; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 186 | UpgradedIntrinsicMap UpgradedIntrinsics; |
| 187 | |
| 188 | // Map the bitcode's custom MDKind ID to the Module's MDKind ID. |
| 189 | DenseMap<unsigned, unsigned> MDKindMap; |
| 190 | |
| 191 | // Several operations happen after the module header has been read, but |
| 192 | // before function bodies are processed. This keeps track of whether |
| 193 | // we've done this yet. |
Rafael Espindola | 4223a1f | 2015-06-15 20:08:17 +0000 | [diff] [blame] | 194 | bool SeenFirstFunctionBody = false; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 195 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 196 | /// When function bodies are initially scanned, this map contains info about |
| 197 | /// where to find deferred function body in the stream. |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 198 | DenseMap<Function*, uint64_t> DeferredFunctionInfo; |
| 199 | |
| 200 | /// When Metadata block is initially scanned when parsing the module, we may |
| 201 | /// choose to defer parsing of the metadata. This vector contains info about |
| 202 | /// which Metadata blocks are deferred. |
| 203 | std::vector<uint64_t> DeferredMetadataInfo; |
| 204 | |
| 205 | /// These are basic blocks forward-referenced by block addresses. They are |
| 206 | /// inserted lazily into functions when they're loaded. The basic block ID is |
| 207 | /// its index into the vector. |
| 208 | DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs; |
| 209 | std::deque<Function *> BasicBlockFwdRefQueue; |
| 210 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 211 | /// Indicates that we are using a new encoding for instruction operands where |
| 212 | /// most operands in the current FUNCTION_BLOCK are encoded relative to the |
| 213 | /// instruction number, for a more compact encoding. Some instruction |
| 214 | /// operands are not relative to the instruction ID: basic block numbers, and |
| 215 | /// types. Once the old style function blocks have been phased out, we would |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 216 | /// not need this flag. |
Rafael Espindola | 4223a1f | 2015-06-15 20:08:17 +0000 | [diff] [blame] | 217 | bool UseRelativeIDs = false; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 218 | |
| 219 | /// True if all functions will be materialized, negating the need to process |
| 220 | /// (e.g.) blockaddress forward references. |
Rafael Espindola | 4223a1f | 2015-06-15 20:08:17 +0000 | [diff] [blame] | 221 | bool WillMaterializeAllForwardRefs = false; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 222 | |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 223 | /// True if any Metadata block has been materialized. |
Rafael Espindola | 4223a1f | 2015-06-15 20:08:17 +0000 | [diff] [blame] | 224 | bool IsMetadataMaterialized = false; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 225 | |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 226 | bool StripDebugInfo = false; |
| 227 | |
Peter Collingbourne | d4bff30 | 2015-11-05 22:03:56 +0000 | [diff] [blame] | 228 | /// Functions that need to be matched with subprograms when upgrading old |
| 229 | /// metadata. |
| 230 | SmallDenseMap<Function *, DISubprogram *, 16> FunctionsWithSPs; |
| 231 | |
Sanjoy Das | b513a9f | 2015-09-24 23:34:52 +0000 | [diff] [blame] | 232 | std::vector<std::string> BundleTags; |
| 233 | |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 234 | public: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 235 | std::error_code error(BitcodeError E, const Twine &Message); |
| 236 | std::error_code error(BitcodeError E); |
| 237 | std::error_code error(const Twine &Message); |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 238 | |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 239 | BitcodeReader(MemoryBuffer *Buffer, LLVMContext &Context); |
| 240 | BitcodeReader(LLVMContext &Context); |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 241 | ~BitcodeReader() override { freeState(); } |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 242 | |
| 243 | std::error_code materializeForwardReferencedFunctions(); |
| 244 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 245 | void freeState(); |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 246 | |
| 247 | void releaseBuffer(); |
| 248 | |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 249 | std::error_code materialize(GlobalValue *GV) override; |
Rafael Espindola | 79753a0 | 2015-12-18 21:18:57 +0000 | [diff] [blame] | 250 | std::error_code materializeModule() override; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 251 | std::vector<StructType *> getIdentifiedStructTypes() const override; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 252 | |
Rafael Espindola | 6ace685 | 2015-06-15 21:02:49 +0000 | [diff] [blame] | 253 | /// \brief Main interface to parsing a bitcode buffer. |
| 254 | /// \returns true if an error occurred. |
Rafael Espindola | 1aabf98 | 2015-06-16 23:29:49 +0000 | [diff] [blame] | 255 | std::error_code parseBitcodeInto(std::unique_ptr<DataStreamer> Streamer, |
| 256 | Module *M, |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 257 | bool ShouldLazyLoadMetadata = false); |
| 258 | |
Rafael Espindola | 6ace685 | 2015-06-15 21:02:49 +0000 | [diff] [blame] | 259 | /// \brief Cheap mechanism to just extract module triple |
| 260 | /// \returns true if an error occurred. |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 261 | ErrorOr<std::string> parseTriple(); |
| 262 | |
Mehdi Amini | 3383ccc | 2015-11-09 02:46:41 +0000 | [diff] [blame] | 263 | /// Cheap mechanism to just extract the identification block out of bitcode. |
| 264 | ErrorOr<std::string> parseIdentificationBlock(); |
| 265 | |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 266 | static uint64_t decodeSignRotatedValue(uint64_t V); |
| 267 | |
| 268 | /// Materialize any deferred Metadata block. |
| 269 | std::error_code materializeMetadata() override; |
| 270 | |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 271 | void setStripDebugInfo() override; |
| 272 | |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 273 | /// Save the mapping between the metadata values and the corresponding |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 274 | /// value id that were recorded in the MetadataList during parsing. If |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 275 | /// OnlyTempMD is true, then only record those entries that are still |
| 276 | /// temporary metadata. This interface is used when metadata linking is |
| 277 | /// performed as a postpass, such as during function importing. |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 278 | void saveMetadataList(DenseMap<const Metadata *, unsigned> &MetadataToIDs, |
| 279 | bool OnlyTempMD) override; |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 280 | |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 281 | private: |
Mehdi Amini | 5d30328 | 2015-10-26 18:37:00 +0000 | [diff] [blame] | 282 | /// Parse the "IDENTIFICATION_BLOCK_ID" block, populate the |
| 283 | // ProducerIdentification data member, and do some basic enforcement on the |
| 284 | // "epoch" encoded in the bitcode. |
| 285 | std::error_code parseBitcodeVersion(); |
| 286 | |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 287 | std::vector<StructType *> IdentifiedStructTypes; |
| 288 | StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name); |
| 289 | StructType *createIdentifiedStructType(LLVMContext &Context); |
| 290 | |
| 291 | Type *getTypeByID(unsigned ID); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 292 | Value *getFnValueByID(unsigned ID, Type *Ty) { |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 293 | if (Ty && Ty->isMetadataTy()) |
| 294 | return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID)); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 295 | return ValueList.getValueFwdRef(ID, Ty); |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 296 | } |
| 297 | Metadata *getFnMetadataByID(unsigned ID) { |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 298 | return MetadataList.getValueFwdRef(ID); |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 299 | } |
| 300 | BasicBlock *getBasicBlock(unsigned ID) const { |
| 301 | if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID |
| 302 | return FunctionBBs[ID]; |
| 303 | } |
| 304 | AttributeSet getAttributes(unsigned i) const { |
| 305 | if (i-1 < MAttributes.size()) |
| 306 | return MAttributes[i-1]; |
| 307 | return AttributeSet(); |
| 308 | } |
| 309 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 310 | /// Read a value/type pair out of the specified record from slot 'Slot'. |
| 311 | /// Increment Slot past the number of slots used in the record. Return true on |
| 312 | /// failure. |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 313 | bool getValueTypePair(SmallVectorImpl<uint64_t> &Record, unsigned &Slot, |
| 314 | unsigned InstNum, Value *&ResVal) { |
| 315 | if (Slot == Record.size()) return true; |
| 316 | unsigned ValNo = (unsigned)Record[Slot++]; |
| 317 | // Adjust the ValNo, if it was encoded relative to the InstNum. |
| 318 | if (UseRelativeIDs) |
| 319 | ValNo = InstNum - ValNo; |
| 320 | if (ValNo < InstNum) { |
| 321 | // If this is not a forward reference, just return the value we already |
| 322 | // have. |
| 323 | ResVal = getFnValueByID(ValNo, nullptr); |
| 324 | return ResVal == nullptr; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 325 | } |
David Blaikie | dbe6e0f | 2015-04-17 06:40:14 +0000 | [diff] [blame] | 326 | if (Slot == Record.size()) |
| 327 | return true; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 328 | |
| 329 | unsigned TypeNo = (unsigned)Record[Slot++]; |
| 330 | ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo)); |
| 331 | return ResVal == nullptr; |
| 332 | } |
| 333 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 334 | /// Read a value out of the specified record from slot 'Slot'. Increment Slot |
| 335 | /// past the number of slots used by the value in the record. Return true if |
| 336 | /// there is an error. |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 337 | bool popValue(SmallVectorImpl<uint64_t> &Record, unsigned &Slot, |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 338 | unsigned InstNum, Type *Ty, Value *&ResVal) { |
| 339 | if (getValue(Record, Slot, InstNum, Ty, ResVal)) |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 340 | return true; |
| 341 | // All values currently take a single record slot. |
| 342 | ++Slot; |
| 343 | return false; |
| 344 | } |
| 345 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 346 | /// Like popValue, but does not increment the Slot number. |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 347 | bool getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot, |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 348 | unsigned InstNum, Type *Ty, Value *&ResVal) { |
| 349 | ResVal = getValue(Record, Slot, InstNum, Ty); |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 350 | return ResVal == nullptr; |
| 351 | } |
| 352 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 353 | /// Version of getValue that returns ResVal directly, or 0 if there is an |
| 354 | /// error. |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 355 | Value *getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot, |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 356 | unsigned InstNum, Type *Ty) { |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 357 | if (Slot == Record.size()) return nullptr; |
| 358 | unsigned ValNo = (unsigned)Record[Slot]; |
| 359 | // Adjust the ValNo, if it was encoded relative to the InstNum. |
| 360 | if (UseRelativeIDs) |
| 361 | ValNo = InstNum - ValNo; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 362 | return getFnValueByID(ValNo, Ty); |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 365 | /// Like getValue, but decodes signed VBRs. |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 366 | Value *getValueSigned(SmallVectorImpl<uint64_t> &Record, unsigned Slot, |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 367 | unsigned InstNum, Type *Ty) { |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 368 | if (Slot == Record.size()) return nullptr; |
| 369 | unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]); |
| 370 | // Adjust the ValNo, if it was encoded relative to the InstNum. |
| 371 | if (UseRelativeIDs) |
| 372 | ValNo = InstNum - ValNo; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 373 | return getFnValueByID(ValNo, Ty); |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /// Converts alignment exponent (i.e. power of two (or zero)) to the |
| 377 | /// corresponding alignment to use. If alignment is too large, returns |
| 378 | /// a corresponding error code. |
| 379 | std::error_code parseAlignmentValue(uint64_t Exponent, unsigned &Alignment); |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 380 | std::error_code parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind); |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 381 | std::error_code parseModule(uint64_t ResumeBit, |
| 382 | bool ShouldLazyLoadMetadata = false); |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 383 | std::error_code parseAttributeBlock(); |
| 384 | std::error_code parseAttributeGroupBlock(); |
| 385 | std::error_code parseTypeTable(); |
| 386 | std::error_code parseTypeTableBody(); |
Sanjoy Das | b513a9f | 2015-09-24 23:34:52 +0000 | [diff] [blame] | 387 | std::error_code parseOperandBundleTags(); |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 388 | |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 389 | ErrorOr<Value *> recordValue(SmallVectorImpl<uint64_t> &Record, |
| 390 | unsigned NameIndex, Triple &TT); |
Peter Collingbourne | 128a976 | 2015-10-27 23:01:25 +0000 | [diff] [blame] | 391 | std::error_code parseValueSymbolTable(uint64_t Offset = 0); |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 392 | std::error_code parseConstants(); |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 393 | std::error_code rememberAndSkipFunctionBodies(); |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 394 | std::error_code rememberAndSkipFunctionBody(); |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 395 | /// Save the positions of the Metadata blocks and skip parsing the blocks. |
| 396 | std::error_code rememberAndSkipMetadata(); |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 397 | std::error_code parseFunctionBody(Function *F); |
| 398 | std::error_code globalCleanup(); |
| 399 | std::error_code resolveGlobalAndAliasInits(); |
Teresa Johnson | d4d3dfd | 2015-11-20 14:51:27 +0000 | [diff] [blame] | 400 | std::error_code parseMetadata(bool ModuleLevel = false); |
Teresa Johnson | 1254507 | 2015-11-15 02:00:09 +0000 | [diff] [blame] | 401 | std::error_code parseMetadataKinds(); |
| 402 | std::error_code parseMetadataKindRecord(SmallVectorImpl<uint64_t> &Record); |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 403 | std::error_code parseMetadataAttachment(Function &F); |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 404 | ErrorOr<std::string> parseModuleTriple(); |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 405 | std::error_code parseUseLists(); |
Rafael Espindola | 1aabf98 | 2015-06-16 23:29:49 +0000 | [diff] [blame] | 406 | std::error_code initStream(std::unique_ptr<DataStreamer> Streamer); |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 407 | std::error_code initStreamFromBuffer(); |
Rafael Espindola | 1aabf98 | 2015-06-16 23:29:49 +0000 | [diff] [blame] | 408 | std::error_code initLazyStream(std::unique_ptr<DataStreamer> Streamer); |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 409 | std::error_code findFunctionInStream( |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 410 | Function *F, |
| 411 | DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator); |
| 412 | }; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 413 | |
| 414 | /// Class to manage reading and parsing function summary index bitcode |
| 415 | /// files/sections. |
| 416 | class FunctionIndexBitcodeReader { |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 417 | DiagnosticHandlerFunction DiagnosticHandler; |
| 418 | |
| 419 | /// Eventually points to the function index built during parsing. |
| 420 | FunctionInfoIndex *TheIndex = nullptr; |
| 421 | |
| 422 | std::unique_ptr<MemoryBuffer> Buffer; |
| 423 | std::unique_ptr<BitstreamReader> StreamFile; |
| 424 | BitstreamCursor Stream; |
| 425 | |
| 426 | /// \brief Used to indicate whether we are doing lazy parsing of summary data. |
| 427 | /// |
| 428 | /// If false, the summary section is fully parsed into the index during |
| 429 | /// the initial parse. Otherwise, if true, the caller is expected to |
| 430 | /// invoke \a readFunctionSummary for each summary needed, and the summary |
| 431 | /// section is thus parsed lazily. |
| 432 | bool IsLazy = false; |
| 433 | |
| 434 | /// Used to indicate whether caller only wants to check for the presence |
| 435 | /// of the function summary bitcode section. All blocks are skipped, |
| 436 | /// but the SeenFuncSummary boolean is set. |
| 437 | bool CheckFuncSummaryPresenceOnly = false; |
| 438 | |
| 439 | /// Indicates whether we have encountered a function summary section |
| 440 | /// yet during parsing, used when checking if file contains function |
| 441 | /// summary section. |
| 442 | bool SeenFuncSummary = false; |
| 443 | |
| 444 | /// \brief Map populated during function summary section parsing, and |
| 445 | /// consumed during ValueSymbolTable parsing. |
| 446 | /// |
| 447 | /// Used to correlate summary records with VST entries. For the per-module |
| 448 | /// index this maps the ValueID to the parsed function summary, and |
| 449 | /// for the combined index this maps the summary record's bitcode |
| 450 | /// offset to the function summary (since in the combined index the |
| 451 | /// VST records do not hold value IDs but rather hold the function |
| 452 | /// summary record offset). |
| 453 | DenseMap<uint64_t, std::unique_ptr<FunctionSummary>> SummaryMap; |
| 454 | |
| 455 | /// Map populated during module path string table parsing, from the |
| 456 | /// module ID to a string reference owned by the index's module |
| 457 | /// path string table, used to correlate with combined index function |
| 458 | /// summary records. |
| 459 | DenseMap<uint64_t, StringRef> ModuleIdMap; |
| 460 | |
Teresa Johnson | e1164de | 2016-02-10 21:55:02 +0000 | [diff] [blame] | 461 | /// Original source file name recorded in a bitcode record. |
| 462 | std::string SourceFileName; |
| 463 | |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 464 | public: |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 465 | std::error_code error(BitcodeError E, const Twine &Message); |
| 466 | std::error_code error(BitcodeError E); |
| 467 | std::error_code error(const Twine &Message); |
| 468 | |
Mehdi Amini | 354f520 | 2015-11-19 05:52:29 +0000 | [diff] [blame] | 469 | FunctionIndexBitcodeReader(MemoryBuffer *Buffer, |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 470 | DiagnosticHandlerFunction DiagnosticHandler, |
| 471 | bool IsLazy = false, |
| 472 | bool CheckFuncSummaryPresenceOnly = false); |
Mehdi Amini | 354f520 | 2015-11-19 05:52:29 +0000 | [diff] [blame] | 473 | FunctionIndexBitcodeReader(DiagnosticHandlerFunction DiagnosticHandler, |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 474 | bool IsLazy = false, |
| 475 | bool CheckFuncSummaryPresenceOnly = false); |
| 476 | ~FunctionIndexBitcodeReader() { freeState(); } |
| 477 | |
| 478 | void freeState(); |
| 479 | |
| 480 | void releaseBuffer(); |
| 481 | |
| 482 | /// Check if the parser has encountered a function summary section. |
| 483 | bool foundFuncSummary() { return SeenFuncSummary; } |
| 484 | |
| 485 | /// \brief Main interface to parsing a bitcode buffer. |
| 486 | /// \returns true if an error occurred. |
| 487 | std::error_code parseSummaryIndexInto(std::unique_ptr<DataStreamer> Streamer, |
| 488 | FunctionInfoIndex *I); |
| 489 | |
| 490 | /// \brief Interface for parsing a function summary lazily. |
| 491 | std::error_code parseFunctionSummary(std::unique_ptr<DataStreamer> Streamer, |
| 492 | FunctionInfoIndex *I, |
| 493 | size_t FunctionSummaryOffset); |
| 494 | |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 495 | private: |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 496 | std::error_code parseModule(); |
| 497 | std::error_code parseValueSymbolTable(); |
| 498 | std::error_code parseEntireSummary(); |
| 499 | std::error_code parseModuleStringTable(); |
| 500 | std::error_code initStream(std::unique_ptr<DataStreamer> Streamer); |
| 501 | std::error_code initStreamFromBuffer(); |
| 502 | std::error_code initLazyStream(std::unique_ptr<DataStreamer> Streamer); |
| 503 | }; |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 504 | } // end anonymous namespace |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 505 | |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 506 | BitcodeDiagnosticInfo::BitcodeDiagnosticInfo(std::error_code EC, |
| 507 | DiagnosticSeverity Severity, |
| 508 | const Twine &Msg) |
| 509 | : DiagnosticInfo(DK_Bitcode, Severity), Msg(Msg), EC(EC) {} |
| 510 | |
| 511 | void BitcodeDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; } |
| 512 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 513 | static std::error_code error(DiagnosticHandlerFunction DiagnosticHandler, |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 514 | std::error_code EC, const Twine &Message) { |
| 515 | BitcodeDiagnosticInfo DI(EC, DS_Error, Message); |
| 516 | DiagnosticHandler(DI); |
| 517 | return EC; |
| 518 | } |
| 519 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 520 | static std::error_code error(DiagnosticHandlerFunction DiagnosticHandler, |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 521 | std::error_code EC) { |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 522 | return error(DiagnosticHandler, EC, EC.message()); |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 525 | static std::error_code error(LLVMContext &Context, std::error_code EC, |
Filipe Cabecinhas | 11bb849 | 2015-05-18 21:48:55 +0000 | [diff] [blame] | 526 | const Twine &Message) { |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 527 | return error([&](const DiagnosticInfo &DI) { Context.diagnose(DI); }, EC, |
| 528 | Message); |
| 529 | } |
| 530 | |
| 531 | static std::error_code error(LLVMContext &Context, std::error_code EC) { |
| 532 | return error(Context, EC, EC.message()); |
| 533 | } |
| 534 | |
| 535 | static std::error_code error(LLVMContext &Context, const Twine &Message) { |
| 536 | return error(Context, make_error_code(BitcodeError::CorruptedBitcode), |
| 537 | Message); |
Filipe Cabecinhas | 11bb849 | 2015-05-18 21:48:55 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 540 | std::error_code BitcodeReader::error(BitcodeError E, const Twine &Message) { |
Mehdi Amini | 5d30328 | 2015-10-26 18:37:00 +0000 | [diff] [blame] | 541 | if (!ProducerIdentification.empty()) { |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 542 | return ::error(Context, make_error_code(E), |
Filipe Cabecinhas | f3e167a | 2015-11-03 13:48:21 +0000 | [diff] [blame] | 543 | Message + " (Producer: '" + ProducerIdentification + |
| 544 | "' Reader: 'LLVM " + LLVM_VERSION_STRING "')"); |
Mehdi Amini | 5d30328 | 2015-10-26 18:37:00 +0000 | [diff] [blame] | 545 | } |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 546 | return ::error(Context, make_error_code(E), Message); |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 549 | std::error_code BitcodeReader::error(const Twine &Message) { |
Mehdi Amini | 5d30328 | 2015-10-26 18:37:00 +0000 | [diff] [blame] | 550 | if (!ProducerIdentification.empty()) { |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 551 | return ::error(Context, make_error_code(BitcodeError::CorruptedBitcode), |
Filipe Cabecinhas | f3e167a | 2015-11-03 13:48:21 +0000 | [diff] [blame] | 552 | Message + " (Producer: '" + ProducerIdentification + |
| 553 | "' Reader: 'LLVM " + LLVM_VERSION_STRING "')"); |
Mehdi Amini | 5d30328 | 2015-10-26 18:37:00 +0000 | [diff] [blame] | 554 | } |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 555 | return ::error(Context, make_error_code(BitcodeError::CorruptedBitcode), |
| 556 | Message); |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 559 | std::error_code BitcodeReader::error(BitcodeError E) { |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 560 | return ::error(Context, make_error_code(E)); |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 563 | BitcodeReader::BitcodeReader(MemoryBuffer *Buffer, LLVMContext &Context) |
| 564 | : Context(Context), Buffer(Buffer), ValueList(Context), |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 565 | MetadataList(Context) {} |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 566 | |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 567 | BitcodeReader::BitcodeReader(LLVMContext &Context) |
| 568 | : Context(Context), Buffer(nullptr), ValueList(Context), |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 569 | MetadataList(Context) {} |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 570 | |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 571 | std::error_code BitcodeReader::materializeForwardReferencedFunctions() { |
| 572 | if (WillMaterializeAllForwardRefs) |
| 573 | return std::error_code(); |
| 574 | |
| 575 | // Prevent recursion. |
| 576 | WillMaterializeAllForwardRefs = true; |
| 577 | |
Duncan P. N. Exon Smith | 5a511b5 | 2014-08-05 17:49:48 +0000 | [diff] [blame] | 578 | while (!BasicBlockFwdRefQueue.empty()) { |
| 579 | Function *F = BasicBlockFwdRefQueue.front(); |
| 580 | BasicBlockFwdRefQueue.pop_front(); |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 581 | assert(F && "Expected valid function"); |
Duncan P. N. Exon Smith | 5a511b5 | 2014-08-05 17:49:48 +0000 | [diff] [blame] | 582 | if (!BasicBlockFwdRefs.count(F)) |
| 583 | // Already materialized. |
| 584 | continue; |
| 585 | |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 586 | // Check for a function that isn't materializable to prevent an infinite |
| 587 | // loop. When parsing a blockaddress stored in a global variable, there |
| 588 | // isn't a trivial way to check if a function will have a body without a |
| 589 | // linear search through FunctionsWithBodies, so just check it here. |
| 590 | if (!F->isMaterializable()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 591 | return error("Never resolved function from blockaddress"); |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 592 | |
| 593 | // Try to materialize F. |
Rafael Espindola | 5a52e6d | 2014-10-24 22:50:48 +0000 | [diff] [blame] | 594 | if (std::error_code EC = materialize(F)) |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 595 | return EC; |
Rafael Espindola | b799346 | 2012-01-02 07:49:53 +0000 | [diff] [blame] | 596 | } |
Duncan P. N. Exon Smith | 5a511b5 | 2014-08-05 17:49:48 +0000 | [diff] [blame] | 597 | assert(BasicBlockFwdRefs.empty() && "Function missing from queue"); |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 598 | |
| 599 | // Reset state. |
| 600 | WillMaterializeAllForwardRefs = false; |
| 601 | return std::error_code(); |
Rafael Espindola | b799346 | 2012-01-02 07:49:53 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 604 | void BitcodeReader::freeState() { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 605 | Buffer = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 606 | std::vector<Type*>().swap(TypeList); |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 607 | ValueList.clear(); |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 608 | MetadataList.clear(); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 609 | std::vector<Comdat *>().swap(ComdatList); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 610 | |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 611 | std::vector<AttributeSet>().swap(MAttributes); |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 612 | std::vector<BasicBlock*>().swap(FunctionBBs); |
| 613 | std::vector<Function*>().swap(FunctionsWithBodies); |
| 614 | DeferredFunctionInfo.clear(); |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 615 | DeferredMetadataInfo.clear(); |
Dan Gohman | 43aa8f0 | 2010-07-20 21:42:28 +0000 | [diff] [blame] | 616 | MDKindMap.clear(); |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 617 | |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 618 | assert(BasicBlockFwdRefs.empty() && "Unresolved blockaddress fwd references"); |
Duncan P. N. Exon Smith | 5a511b5 | 2014-08-05 17:49:48 +0000 | [diff] [blame] | 619 | BasicBlockFwdRefQueue.clear(); |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 622 | //===----------------------------------------------------------------------===// |
| 623 | // Helper functions to implement forward reference resolution, etc. |
| 624 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 625 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 626 | /// Convert a string from a record into an std::string, return true on failure. |
| 627 | template <typename StrTy> |
| 628 | static bool convertToString(ArrayRef<uint64_t> Record, unsigned Idx, |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 629 | StrTy &Result) { |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 630 | if (Idx > Record.size()) |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 631 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 632 | |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 633 | for (unsigned i = Idx, e = Record.size(); i != e; ++i) |
| 634 | Result += (char)Record[i]; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 635 | return false; |
| 636 | } |
| 637 | |
Rafael Espindola | 12ca34f | 2015-01-19 15:16:06 +0000 | [diff] [blame] | 638 | static bool hasImplicitComdat(size_t Val) { |
| 639 | switch (Val) { |
| 640 | default: |
| 641 | return false; |
| 642 | case 1: // Old WeakAnyLinkage |
| 643 | case 4: // Old LinkOnceAnyLinkage |
| 644 | case 10: // Old WeakODRLinkage |
| 645 | case 11: // Old LinkOnceODRLinkage |
| 646 | return true; |
| 647 | } |
| 648 | } |
| 649 | |
Rafael Espindola | 7b4b2dc | 2015-01-08 15:36:32 +0000 | [diff] [blame] | 650 | static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) { |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 651 | switch (Val) { |
| 652 | default: // Map unknown/new linkages to external |
Rafael Espindola | 7b4b2dc | 2015-01-08 15:36:32 +0000 | [diff] [blame] | 653 | case 0: |
| 654 | return GlobalValue::ExternalLinkage; |
Rafael Espindola | 7b4b2dc | 2015-01-08 15:36:32 +0000 | [diff] [blame] | 655 | case 2: |
| 656 | return GlobalValue::AppendingLinkage; |
| 657 | case 3: |
| 658 | return GlobalValue::InternalLinkage; |
Rafael Espindola | 7b4b2dc | 2015-01-08 15:36:32 +0000 | [diff] [blame] | 659 | case 5: |
| 660 | return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage |
| 661 | case 6: |
| 662 | return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage |
| 663 | case 7: |
| 664 | return GlobalValue::ExternalWeakLinkage; |
| 665 | case 8: |
| 666 | return GlobalValue::CommonLinkage; |
| 667 | case 9: |
| 668 | return GlobalValue::PrivateLinkage; |
Rafael Espindola | 7b4b2dc | 2015-01-08 15:36:32 +0000 | [diff] [blame] | 669 | case 12: |
| 670 | return GlobalValue::AvailableExternallyLinkage; |
Rafael Espindola | 2fb5bc3 | 2014-03-13 23:18:37 +0000 | [diff] [blame] | 671 | case 13: |
| 672 | return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage |
| 673 | case 14: |
| 674 | return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage |
Rafael Espindola | bec6af6 | 2015-01-08 15:39:50 +0000 | [diff] [blame] | 675 | case 15: |
| 676 | return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage |
Rafael Espindola | 12ca34f | 2015-01-19 15:16:06 +0000 | [diff] [blame] | 677 | case 1: // Old value with implicit comdat. |
| 678 | case 16: |
| 679 | return GlobalValue::WeakAnyLinkage; |
| 680 | case 10: // Old value with implicit comdat. |
| 681 | case 17: |
| 682 | return GlobalValue::WeakODRLinkage; |
| 683 | case 4: // Old value with implicit comdat. |
| 684 | case 18: |
| 685 | return GlobalValue::LinkOnceAnyLinkage; |
| 686 | case 11: // Old value with implicit comdat. |
| 687 | case 19: |
| 688 | return GlobalValue::LinkOnceODRLinkage; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 692 | static GlobalValue::VisibilityTypes getDecodedVisibility(unsigned Val) { |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 693 | switch (Val) { |
| 694 | default: // Map unknown visibilities to default. |
| 695 | case 0: return GlobalValue::DefaultVisibility; |
| 696 | case 1: return GlobalValue::HiddenVisibility; |
Anton Korobeynikov | 31fc4f9 | 2007-04-29 20:56:48 +0000 | [diff] [blame] | 697 | case 2: return GlobalValue::ProtectedVisibility; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 698 | } |
| 699 | } |
| 700 | |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 701 | static GlobalValue::DLLStorageClassTypes |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 702 | getDecodedDLLStorageClass(unsigned Val) { |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 703 | switch (Val) { |
| 704 | default: // Map unknown values to default. |
| 705 | case 0: return GlobalValue::DefaultStorageClass; |
| 706 | case 1: return GlobalValue::DLLImportStorageClass; |
| 707 | case 2: return GlobalValue::DLLExportStorageClass; |
| 708 | } |
| 709 | } |
| 710 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 711 | static GlobalVariable::ThreadLocalMode getDecodedThreadLocalMode(unsigned Val) { |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 712 | switch (Val) { |
| 713 | case 0: return GlobalVariable::NotThreadLocal; |
| 714 | default: // Map unknown non-zero value to general dynamic. |
| 715 | case 1: return GlobalVariable::GeneralDynamicTLSModel; |
| 716 | case 2: return GlobalVariable::LocalDynamicTLSModel; |
| 717 | case 3: return GlobalVariable::InitialExecTLSModel; |
| 718 | case 4: return GlobalVariable::LocalExecTLSModel; |
| 719 | } |
| 720 | } |
| 721 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 722 | static int getDecodedCastOpcode(unsigned Val) { |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 723 | switch (Val) { |
| 724 | default: return -1; |
| 725 | case bitc::CAST_TRUNC : return Instruction::Trunc; |
| 726 | case bitc::CAST_ZEXT : return Instruction::ZExt; |
| 727 | case bitc::CAST_SEXT : return Instruction::SExt; |
| 728 | case bitc::CAST_FPTOUI : return Instruction::FPToUI; |
| 729 | case bitc::CAST_FPTOSI : return Instruction::FPToSI; |
| 730 | case bitc::CAST_UITOFP : return Instruction::UIToFP; |
| 731 | case bitc::CAST_SITOFP : return Instruction::SIToFP; |
| 732 | case bitc::CAST_FPTRUNC : return Instruction::FPTrunc; |
| 733 | case bitc::CAST_FPEXT : return Instruction::FPExt; |
| 734 | case bitc::CAST_PTRTOINT: return Instruction::PtrToInt; |
| 735 | case bitc::CAST_INTTOPTR: return Instruction::IntToPtr; |
| 736 | case bitc::CAST_BITCAST : return Instruction::BitCast; |
Matt Arsenault | 3aa9b03 | 2013-11-18 02:51:33 +0000 | [diff] [blame] | 737 | case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast; |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 738 | } |
| 739 | } |
Filipe Cabecinhas | ea79c5b | 2015-04-22 09:06:21 +0000 | [diff] [blame] | 740 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 741 | static int getDecodedBinaryOpcode(unsigned Val, Type *Ty) { |
Filipe Cabecinhas | ea79c5b | 2015-04-22 09:06:21 +0000 | [diff] [blame] | 742 | bool IsFP = Ty->isFPOrFPVectorTy(); |
| 743 | // BinOps are only valid for int/fp or vector of int/fp types |
| 744 | if (!IsFP && !Ty->isIntOrIntVectorTy()) |
| 745 | return -1; |
| 746 | |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 747 | switch (Val) { |
Filipe Cabecinhas | ea79c5b | 2015-04-22 09:06:21 +0000 | [diff] [blame] | 748 | default: |
| 749 | return -1; |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 750 | case bitc::BINOP_ADD: |
Filipe Cabecinhas | ea79c5b | 2015-04-22 09:06:21 +0000 | [diff] [blame] | 751 | return IsFP ? Instruction::FAdd : Instruction::Add; |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 752 | case bitc::BINOP_SUB: |
Filipe Cabecinhas | ea79c5b | 2015-04-22 09:06:21 +0000 | [diff] [blame] | 753 | return IsFP ? Instruction::FSub : Instruction::Sub; |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 754 | case bitc::BINOP_MUL: |
Filipe Cabecinhas | ea79c5b | 2015-04-22 09:06:21 +0000 | [diff] [blame] | 755 | return IsFP ? Instruction::FMul : Instruction::Mul; |
| 756 | case bitc::BINOP_UDIV: |
| 757 | return IsFP ? -1 : Instruction::UDiv; |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 758 | case bitc::BINOP_SDIV: |
Filipe Cabecinhas | ea79c5b | 2015-04-22 09:06:21 +0000 | [diff] [blame] | 759 | return IsFP ? Instruction::FDiv : Instruction::SDiv; |
| 760 | case bitc::BINOP_UREM: |
| 761 | return IsFP ? -1 : Instruction::URem; |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 762 | case bitc::BINOP_SREM: |
Filipe Cabecinhas | ea79c5b | 2015-04-22 09:06:21 +0000 | [diff] [blame] | 763 | return IsFP ? Instruction::FRem : Instruction::SRem; |
| 764 | case bitc::BINOP_SHL: |
| 765 | return IsFP ? -1 : Instruction::Shl; |
| 766 | case bitc::BINOP_LSHR: |
| 767 | return IsFP ? -1 : Instruction::LShr; |
| 768 | case bitc::BINOP_ASHR: |
| 769 | return IsFP ? -1 : Instruction::AShr; |
| 770 | case bitc::BINOP_AND: |
| 771 | return IsFP ? -1 : Instruction::And; |
| 772 | case bitc::BINOP_OR: |
| 773 | return IsFP ? -1 : Instruction::Or; |
| 774 | case bitc::BINOP_XOR: |
| 775 | return IsFP ? -1 : Instruction::Xor; |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 776 | } |
| 777 | } |
| 778 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 779 | static AtomicRMWInst::BinOp getDecodedRMWOperation(unsigned Val) { |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 780 | switch (Val) { |
| 781 | default: return AtomicRMWInst::BAD_BINOP; |
| 782 | case bitc::RMW_XCHG: return AtomicRMWInst::Xchg; |
| 783 | case bitc::RMW_ADD: return AtomicRMWInst::Add; |
| 784 | case bitc::RMW_SUB: return AtomicRMWInst::Sub; |
| 785 | case bitc::RMW_AND: return AtomicRMWInst::And; |
| 786 | case bitc::RMW_NAND: return AtomicRMWInst::Nand; |
| 787 | case bitc::RMW_OR: return AtomicRMWInst::Or; |
| 788 | case bitc::RMW_XOR: return AtomicRMWInst::Xor; |
| 789 | case bitc::RMW_MAX: return AtomicRMWInst::Max; |
| 790 | case bitc::RMW_MIN: return AtomicRMWInst::Min; |
| 791 | case bitc::RMW_UMAX: return AtomicRMWInst::UMax; |
| 792 | case bitc::RMW_UMIN: return AtomicRMWInst::UMin; |
| 793 | } |
| 794 | } |
| 795 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 796 | static AtomicOrdering getDecodedOrdering(unsigned Val) { |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 797 | switch (Val) { |
| 798 | case bitc::ORDERING_NOTATOMIC: return NotAtomic; |
| 799 | case bitc::ORDERING_UNORDERED: return Unordered; |
| 800 | case bitc::ORDERING_MONOTONIC: return Monotonic; |
| 801 | case bitc::ORDERING_ACQUIRE: return Acquire; |
| 802 | case bitc::ORDERING_RELEASE: return Release; |
| 803 | case bitc::ORDERING_ACQREL: return AcquireRelease; |
| 804 | default: // Map unknown orderings to sequentially-consistent. |
| 805 | case bitc::ORDERING_SEQCST: return SequentiallyConsistent; |
| 806 | } |
| 807 | } |
| 808 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 809 | static SynchronizationScope getDecodedSynchScope(unsigned Val) { |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 810 | switch (Val) { |
| 811 | case bitc::SYNCHSCOPE_SINGLETHREAD: return SingleThread; |
| 812 | default: // Map unknown scopes to cross-thread. |
| 813 | case bitc::SYNCHSCOPE_CROSSTHREAD: return CrossThread; |
| 814 | } |
| 815 | } |
| 816 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 817 | static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) { |
| 818 | switch (Val) { |
| 819 | default: // Map unknown selection kinds to any. |
| 820 | case bitc::COMDAT_SELECTION_KIND_ANY: |
| 821 | return Comdat::Any; |
| 822 | case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH: |
| 823 | return Comdat::ExactMatch; |
| 824 | case bitc::COMDAT_SELECTION_KIND_LARGEST: |
| 825 | return Comdat::Largest; |
| 826 | case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES: |
| 827 | return Comdat::NoDuplicates; |
| 828 | case bitc::COMDAT_SELECTION_KIND_SAME_SIZE: |
| 829 | return Comdat::SameSize; |
| 830 | } |
| 831 | } |
| 832 | |
James Molloy | 88eb535 | 2015-07-10 12:52:00 +0000 | [diff] [blame] | 833 | static FastMathFlags getDecodedFastMathFlags(unsigned Val) { |
| 834 | FastMathFlags FMF; |
| 835 | if (0 != (Val & FastMathFlags::UnsafeAlgebra)) |
| 836 | FMF.setUnsafeAlgebra(); |
| 837 | if (0 != (Val & FastMathFlags::NoNaNs)) |
| 838 | FMF.setNoNaNs(); |
| 839 | if (0 != (Val & FastMathFlags::NoInfs)) |
| 840 | FMF.setNoInfs(); |
| 841 | if (0 != (Val & FastMathFlags::NoSignedZeros)) |
| 842 | FMF.setNoSignedZeros(); |
| 843 | if (0 != (Val & FastMathFlags::AllowReciprocal)) |
| 844 | FMF.setAllowReciprocal(); |
| 845 | return FMF; |
| 846 | } |
| 847 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 848 | static void upgradeDLLImportExportLinkage(llvm::GlobalValue *GV, unsigned Val) { |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 849 | switch (Val) { |
| 850 | case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break; |
| 851 | case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break; |
| 852 | } |
| 853 | } |
| 854 | |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 855 | namespace llvm { |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 856 | namespace { |
Rafael Espindola | 64a27fb | 2015-06-15 21:04:27 +0000 | [diff] [blame] | 857 | /// \brief A class for maintaining the slot number definition |
| 858 | /// as a placeholder for the actual definition for forward constants defs. |
| 859 | class ConstantPlaceHolder : public ConstantExpr { |
| 860 | void operator=(const ConstantPlaceHolder &) = delete; |
| 861 | |
| 862 | public: |
| 863 | // allocate space for exactly one operand |
| 864 | void *operator new(size_t s) { return User::operator new(s, 1); } |
| 865 | explicit ConstantPlaceHolder(Type *Ty, LLVMContext &Context) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 866 | : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) { |
Rafael Espindola | 64a27fb | 2015-06-15 21:04:27 +0000 | [diff] [blame] | 867 | Op<0>() = UndefValue::get(Type::getInt32Ty(Context)); |
| 868 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 869 | |
Rafael Espindola | 64a27fb | 2015-06-15 21:04:27 +0000 | [diff] [blame] | 870 | /// \brief Methods to support type inquiry through isa, cast, and dyn_cast. |
| 871 | static bool classof(const Value *V) { |
| 872 | return isa<ConstantExpr>(V) && |
| 873 | cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1; |
| 874 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 875 | |
Rafael Espindola | 64a27fb | 2015-06-15 21:04:27 +0000 | [diff] [blame] | 876 | /// Provide fast operand accessors |
| 877 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
| 878 | }; |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 879 | } // end anonymous namespace |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 880 | |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 881 | // FIXME: can we inherit this from ConstantExpr? |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 882 | template <> |
Jay Foad | c8adf5f | 2011-01-11 15:07:38 +0000 | [diff] [blame] | 883 | struct OperandTraits<ConstantPlaceHolder> : |
| 884 | public FixedNumOperandTraits<ConstantPlaceHolder, 1> { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 885 | }; |
Richard Trieu | e3d126c | 2014-11-21 02:42:08 +0000 | [diff] [blame] | 886 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantPlaceHolder, Value) |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 887 | } // end namespace llvm |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 888 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 889 | void BitcodeReaderValueList::assignValue(Value *V, unsigned Idx) { |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 890 | if (Idx == size()) { |
| 891 | push_back(V); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 892 | return; |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 893 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 894 | |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 895 | if (Idx >= size()) |
| 896 | resize(Idx+1); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 897 | |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 898 | WeakVH &OldV = ValuePtrs[Idx]; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 899 | if (!OldV) { |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 900 | OldV = V; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 901 | return; |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 902 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 903 | |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 904 | // Handle constants and non-constants (e.g. instrs) differently for |
| 905 | // efficiency. |
| 906 | if (Constant *PHC = dyn_cast<Constant>(&*OldV)) { |
| 907 | ResolveConstants.push_back(std::make_pair(PHC, Idx)); |
| 908 | OldV = V; |
| 909 | } else { |
| 910 | // If there was a forward reference to this value, replace it. |
| 911 | Value *PrevVal = OldV; |
| 912 | OldV->replaceAllUsesWith(V); |
| 913 | delete PrevVal; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 914 | } |
| 915 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 916 | |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 917 | Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 918 | Type *Ty) { |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 919 | if (Idx >= size()) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 920 | resize(Idx + 1); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 921 | |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 922 | if (Value *V = ValuePtrs[Idx]) { |
Filipe Cabecinhas | 6a92a3f | 2015-05-27 01:05:40 +0000 | [diff] [blame] | 923 | if (Ty != V->getType()) |
| 924 | report_fatal_error("Type mismatch in constant table!"); |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 925 | return cast<Constant>(V); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 926 | } |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 927 | |
| 928 | // Create and return a placeholder, which will later be RAUW'd. |
Owen Anderson | e9f9804 | 2009-07-07 20:18:58 +0000 | [diff] [blame] | 929 | Constant *C = new ConstantPlaceHolder(Ty, Context); |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 930 | ValuePtrs[Idx] = C; |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 931 | return C; |
| 932 | } |
| 933 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 934 | Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, Type *Ty) { |
Filipe Cabecinhas | bad0779 | 2015-04-30 00:52:42 +0000 | [diff] [blame] | 935 | // Bail out for a clearly invalid value. This would make us call resize(0) |
| 936 | if (Idx == UINT_MAX) |
| 937 | return nullptr; |
| 938 | |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 939 | if (Idx >= size()) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 940 | resize(Idx + 1); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 941 | |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 942 | if (Value *V = ValuePtrs[Idx]) { |
Filipe Cabecinhas | b435d0f | 2015-04-28 20:18:47 +0000 | [diff] [blame] | 943 | // If the types don't match, it's invalid. |
| 944 | if (Ty && Ty != V->getType()) |
| 945 | return nullptr; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 946 | return V; |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 947 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 948 | |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 949 | // No type specified, must be invalid reference. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 950 | if (!Ty) return nullptr; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 951 | |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 952 | // Create and return a placeholder, which will later be RAUW'd. |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 953 | Value *V = new Argument(Ty); |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 954 | ValuePtrs[Idx] = V; |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 955 | return V; |
| 956 | } |
| 957 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 958 | /// Once all constants are read, this method bulk resolves any forward |
| 959 | /// references. The idea behind this is that we sometimes get constants (such |
| 960 | /// as large arrays) which reference *many* forward ref constants. Replacing |
| 961 | /// each of these causes a lot of thrashing when building/reuniquing the |
| 962 | /// constant. Instead of doing this, we look at all the uses and rewrite all |
| 963 | /// the place holders at once for any constant that uses a placeholder. |
| 964 | void BitcodeReaderValueList::resolveConstantForwardRefs() { |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 965 | // Sort the values by-pointer so that they are efficient to look up with a |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 966 | // binary search. |
| 967 | std::sort(ResolveConstants.begin(), ResolveConstants.end()); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 968 | |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 969 | SmallVector<Constant*, 64> NewOps; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 970 | |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 971 | while (!ResolveConstants.empty()) { |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 972 | Value *RealVal = operator[](ResolveConstants.back().second); |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 973 | Constant *Placeholder = ResolveConstants.back().first; |
| 974 | ResolveConstants.pop_back(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 975 | |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 976 | // Loop over all users of the placeholder, updating them to reference the |
| 977 | // new value. If they reference more than one placeholder, update them all |
| 978 | // at once. |
| 979 | while (!Placeholder->use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 980 | auto UI = Placeholder->user_begin(); |
Gabor Greif | 2c0ab48 | 2010-07-09 16:01:21 +0000 | [diff] [blame] | 981 | User *U = *UI; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 982 | |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 983 | // If the using object isn't uniqued, just update the operands. This |
| 984 | // handles instructions and initializers for global variables. |
Gabor Greif | 2c0ab48 | 2010-07-09 16:01:21 +0000 | [diff] [blame] | 985 | if (!isa<Constant>(U) || isa<GlobalValue>(U)) { |
Chris Lattner | 479c5d9 | 2008-08-21 17:31:45 +0000 | [diff] [blame] | 986 | UI.getUse().set(RealVal); |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 987 | continue; |
| 988 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 989 | |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 990 | // Otherwise, we have a constant that uses the placeholder. Replace that |
| 991 | // constant with a new constant that has *all* placeholder uses updated. |
Gabor Greif | 2c0ab48 | 2010-07-09 16:01:21 +0000 | [diff] [blame] | 992 | Constant *UserC = cast<Constant>(U); |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 993 | for (User::op_iterator I = UserC->op_begin(), E = UserC->op_end(); |
| 994 | I != E; ++I) { |
| 995 | Value *NewOp; |
| 996 | if (!isa<ConstantPlaceHolder>(*I)) { |
| 997 | // Not a placeholder reference. |
| 998 | NewOp = *I; |
| 999 | } else if (*I == Placeholder) { |
| 1000 | // Common case is that it just references this one placeholder. |
| 1001 | NewOp = RealVal; |
| 1002 | } else { |
| 1003 | // Otherwise, look up the placeholder in ResolveConstants. |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1004 | ResolveConstantsTy::iterator It = |
| 1005 | std::lower_bound(ResolveConstants.begin(), ResolveConstants.end(), |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 1006 | std::pair<Constant*, unsigned>(cast<Constant>(*I), |
| 1007 | 0)); |
| 1008 | assert(It != ResolveConstants.end() && It->first == *I); |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 1009 | NewOp = operator[](It->second); |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | NewOps.push_back(cast<Constant>(NewOp)); |
| 1013 | } |
| 1014 | |
| 1015 | // Make the new constant. |
| 1016 | Constant *NewC; |
| 1017 | if (ConstantArray *UserCA = dyn_cast<ConstantArray>(UserC)) { |
Jay Foad | 83be361 | 2011-06-22 09:24:39 +0000 | [diff] [blame] | 1018 | NewC = ConstantArray::get(UserCA->getType(), NewOps); |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 1019 | } else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) { |
Chris Lattner | cc19efa | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 1020 | NewC = ConstantStruct::get(UserCS->getType(), NewOps); |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 1021 | } else if (isa<ConstantVector>(UserC)) { |
Chris Lattner | 6922931 | 2011-02-15 00:14:00 +0000 | [diff] [blame] | 1022 | NewC = ConstantVector::get(NewOps); |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 1023 | } else { |
| 1024 | assert(isa<ConstantExpr>(UserC) && "Must be a ConstantExpr."); |
Jay Foad | 5c984e56 | 2011-04-13 13:46:01 +0000 | [diff] [blame] | 1025 | NewC = cast<ConstantExpr>(UserC)->getWithOperands(NewOps); |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 1026 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1027 | |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 1028 | UserC->replaceAllUsesWith(NewC); |
| 1029 | UserC->destroyConstant(); |
| 1030 | NewOps.clear(); |
| 1031 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1032 | |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 1033 | // Update all ValueHandles, they should be the only users at this point. |
| 1034 | Placeholder->replaceAllUsesWith(RealVal); |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 1035 | delete Placeholder; |
| 1036 | } |
| 1037 | } |
| 1038 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 1039 | void BitcodeReaderMetadataList::assignValue(Metadata *MD, unsigned Idx) { |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 1040 | if (Idx == size()) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1041 | push_back(MD); |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 1042 | return; |
| 1043 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1044 | |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 1045 | if (Idx >= size()) |
| 1046 | resize(Idx+1); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1047 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 1048 | TrackingMDRef &OldMD = MetadataPtrs[Idx]; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1049 | if (!OldMD) { |
| 1050 | OldMD.reset(MD); |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 1051 | return; |
| 1052 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1053 | |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 1054 | // If there was a forward reference to this value, replace it. |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 1055 | TempMDTuple PrevMD(cast<MDTuple>(OldMD.get())); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1056 | PrevMD->replaceAllUsesWith(MD); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1057 | --NumFwdRefs; |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 1060 | Metadata *BitcodeReaderMetadataList::getValueFwdRef(unsigned Idx) { |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 1061 | if (Idx >= size()) |
| 1062 | resize(Idx + 1); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1063 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 1064 | if (Metadata *MD = MetadataPtrs[Idx]) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1065 | return MD; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1066 | |
Duncan P. N. Exon Smith | 060ee62 | 2015-02-16 19:18:01 +0000 | [diff] [blame] | 1067 | // Track forward refs to be resolved later. |
| 1068 | if (AnyFwdRefs) { |
| 1069 | MinFwdRef = std::min(MinFwdRef, Idx); |
| 1070 | MaxFwdRef = std::max(MaxFwdRef, Idx); |
| 1071 | } else { |
| 1072 | AnyFwdRefs = true; |
| 1073 | MinFwdRef = MaxFwdRef = Idx; |
| 1074 | } |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1075 | ++NumFwdRefs; |
Duncan P. N. Exon Smith | 060ee62 | 2015-02-16 19:18:01 +0000 | [diff] [blame] | 1076 | |
| 1077 | // Create and return a placeholder, which will later be RAUW'd. |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 1078 | Metadata *MD = MDNode::getTemporary(Context, None).release(); |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 1079 | MetadataPtrs[Idx].reset(MD); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1080 | return MD; |
| 1081 | } |
| 1082 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 1083 | void BitcodeReaderMetadataList::tryToResolveCycles() { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1084 | if (!AnyFwdRefs) |
| 1085 | // Nothing to do. |
| 1086 | return; |
| 1087 | |
| 1088 | if (NumFwdRefs) |
| 1089 | // Still forward references... can't resolve cycles. |
| 1090 | return; |
| 1091 | |
| 1092 | // Resolve any cycles. |
Duncan P. N. Exon Smith | 060ee62 | 2015-02-16 19:18:01 +0000 | [diff] [blame] | 1093 | for (unsigned I = MinFwdRef, E = MaxFwdRef + 1; I != E; ++I) { |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 1094 | auto &MD = MetadataPtrs[I]; |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 1095 | auto *N = dyn_cast_or_null<MDNode>(MD); |
Duncan P. N. Exon Smith | 946fdcc | 2015-01-19 20:36:39 +0000 | [diff] [blame] | 1096 | if (!N) |
| 1097 | continue; |
| 1098 | |
| 1099 | assert(!N->isTemporary() && "Unexpected forward reference"); |
| 1100 | N->resolveCycles(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1101 | } |
Duncan P. N. Exon Smith | 060ee62 | 2015-02-16 19:18:01 +0000 | [diff] [blame] | 1102 | |
| 1103 | // Make sure we return early again until there's another forward ref. |
| 1104 | AnyFwdRefs = false; |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 1105 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1106 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1107 | Type *BitcodeReader::getTypeByID(unsigned ID) { |
| 1108 | // The type table size is always specified correctly. |
| 1109 | if (ID >= TypeList.size()) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1110 | return nullptr; |
Derek Schuff | 206dddd | 2012-02-06 19:03:04 +0000 | [diff] [blame] | 1111 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1112 | if (Type *Ty = TypeList[ID]) |
| 1113 | return Ty; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1114 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1115 | // If we have a forward reference, the only possible case is when it is to a |
| 1116 | // named struct. Just create a placeholder for now. |
Rafael Espindola | 2fa1e43 | 2014-12-03 07:18:23 +0000 | [diff] [blame] | 1117 | return TypeList[ID] = createIdentifiedStructType(Context); |
| 1118 | } |
| 1119 | |
| 1120 | StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context, |
| 1121 | StringRef Name) { |
| 1122 | auto *Ret = StructType::create(Context, Name); |
| 1123 | IdentifiedStructTypes.push_back(Ret); |
| 1124 | return Ret; |
| 1125 | } |
| 1126 | |
| 1127 | StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) { |
| 1128 | auto *Ret = StructType::create(Context); |
| 1129 | IdentifiedStructTypes.push_back(Ret); |
| 1130 | return Ret; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1133 | //===----------------------------------------------------------------------===// |
| 1134 | // Functions for parsing blocks from the bitcode file |
| 1135 | //===----------------------------------------------------------------------===// |
| 1136 | |
Bill Wendling | 56aeccc | 2013-02-04 23:32:23 +0000 | [diff] [blame] | 1137 | |
| 1138 | /// \brief This fills an AttrBuilder object with the LLVM attributes that have |
| 1139 | /// been decoded from the given integer. This function must stay in sync with |
| 1140 | /// 'encodeLLVMAttributesForBitcode'. |
| 1141 | static void decodeLLVMAttributesForBitcode(AttrBuilder &B, |
| 1142 | uint64_t EncodedAttrs) { |
| 1143 | // FIXME: Remove in 4.0. |
| 1144 | |
| 1145 | // The alignment is stored as a 16-bit raw value from bits 31--16. We shift |
| 1146 | // the bits above 31 down by 11 bits. |
| 1147 | unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16; |
| 1148 | assert((!Alignment || isPowerOf2_32(Alignment)) && |
| 1149 | "Alignment must be a power of two."); |
| 1150 | |
| 1151 | if (Alignment) |
| 1152 | B.addAlignmentAttr(Alignment); |
Kostya Serebryany | d688bab | 2013-02-11 08:13:54 +0000 | [diff] [blame] | 1153 | B.addRawValue(((EncodedAttrs & (0xfffffULL << 32)) >> 11) | |
Bill Wendling | 56aeccc | 2013-02-04 23:32:23 +0000 | [diff] [blame] | 1154 | (EncodedAttrs & 0xffff)); |
| 1155 | } |
| 1156 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1157 | std::error_code BitcodeReader::parseAttributeBlock() { |
Chris Lattner | 982ec1e | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1158 | if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1159 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1160 | |
Devang Patel | a05633e | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 1161 | if (!MAttributes.empty()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1162 | return error("Invalid multiple blocks"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1163 | |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1164 | SmallVector<uint64_t, 64> Record; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1165 | |
Bill Wendling | 71173cb | 2013-01-27 00:36:48 +0000 | [diff] [blame] | 1166 | SmallVector<AttributeSet, 8> Attrs; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1167 | |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1168 | // Read all the records. |
| 1169 | while (1) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1170 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 1171 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1172 | switch (Entry.Kind) { |
| 1173 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 1174 | case BitstreamEntry::Error: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1175 | return error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1176 | case BitstreamEntry::EndBlock: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1177 | return std::error_code(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1178 | case BitstreamEntry::Record: |
| 1179 | // The interesting case. |
| 1180 | break; |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1181 | } |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 1182 | |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1183 | // Read a record. |
| 1184 | Record.clear(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1185 | switch (Stream.readRecord(Entry.ID, Record)) { |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1186 | default: // Default behavior: ignore. |
| 1187 | break; |
Bill Wendling | 56aeccc | 2013-02-04 23:32:23 +0000 | [diff] [blame] | 1188 | case bitc::PARAMATTR_CODE_ENTRY_OLD: { // ENTRY: [paramidx0, attr0, ...] |
| 1189 | // FIXME: Remove in 4.0. |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1190 | if (Record.size() & 1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1191 | return error("Invalid record"); |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1192 | |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1193 | for (unsigned i = 0, e = Record.size(); i != e; i += 2) { |
Bill Wendling | 60011b8 | 2013-01-29 01:43:29 +0000 | [diff] [blame] | 1194 | AttrBuilder B; |
Bill Wendling | 56aeccc | 2013-02-04 23:32:23 +0000 | [diff] [blame] | 1195 | decodeLLVMAttributesForBitcode(B, Record[i+1]); |
Bill Wendling | 60011b8 | 2013-01-29 01:43:29 +0000 | [diff] [blame] | 1196 | Attrs.push_back(AttributeSet::get(Context, Record[i], B)); |
Devang Patel | a05633e | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 1197 | } |
Devang Patel | a05633e | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 1198 | |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 1199 | MAttributes.push_back(AttributeSet::get(Context, Attrs)); |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1200 | Attrs.clear(); |
| 1201 | break; |
| 1202 | } |
Bill Wendling | 0dc0891 | 2013-02-12 08:13:50 +0000 | [diff] [blame] | 1203 | case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [attrgrp0, attrgrp1, ...] |
| 1204 | for (unsigned i = 0, e = Record.size(); i != e; ++i) |
| 1205 | Attrs.push_back(MAttributeGroups[Record[i]]); |
| 1206 | |
| 1207 | MAttributes.push_back(AttributeSet::get(Context, Attrs)); |
| 1208 | Attrs.clear(); |
| 1209 | break; |
| 1210 | } |
Duncan Sands | 04eb67e | 2007-11-20 14:09:29 +0000 | [diff] [blame] | 1211 | } |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1212 | } |
| 1213 | } |
| 1214 | |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1215 | // Returns Attribute::None on unrecognized codes. |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1216 | static Attribute::AttrKind getAttrFromCode(uint64_t Code) { |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1217 | switch (Code) { |
| 1218 | default: |
| 1219 | return Attribute::None; |
| 1220 | case bitc::ATTR_KIND_ALIGNMENT: |
| 1221 | return Attribute::Alignment; |
| 1222 | case bitc::ATTR_KIND_ALWAYS_INLINE: |
| 1223 | return Attribute::AlwaysInline; |
Igor Laevsky | 39d662f | 2015-07-11 10:30:36 +0000 | [diff] [blame] | 1224 | case bitc::ATTR_KIND_ARGMEMONLY: |
| 1225 | return Attribute::ArgMemOnly; |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1226 | case bitc::ATTR_KIND_BUILTIN: |
| 1227 | return Attribute::Builtin; |
| 1228 | case bitc::ATTR_KIND_BY_VAL: |
| 1229 | return Attribute::ByVal; |
Reid Kleckner | a534a38 | 2013-12-19 02:14:12 +0000 | [diff] [blame] | 1230 | case bitc::ATTR_KIND_IN_ALLOCA: |
| 1231 | return Attribute::InAlloca; |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1232 | case bitc::ATTR_KIND_COLD: |
| 1233 | return Attribute::Cold; |
Owen Anderson | 85fa7d5 | 2015-05-26 23:48:40 +0000 | [diff] [blame] | 1234 | case bitc::ATTR_KIND_CONVERGENT: |
| 1235 | return Attribute::Convergent; |
Vaivaswatha Nagaraj | fb3f490 | 2015-12-16 16:16:19 +0000 | [diff] [blame] | 1236 | case bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY: |
| 1237 | return Attribute::InaccessibleMemOnly; |
| 1238 | case bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY: |
| 1239 | return Attribute::InaccessibleMemOrArgMemOnly; |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1240 | case bitc::ATTR_KIND_INLINE_HINT: |
| 1241 | return Attribute::InlineHint; |
| 1242 | case bitc::ATTR_KIND_IN_REG: |
| 1243 | return Attribute::InReg; |
Tom Roeder | 44cb65f | 2014-06-05 19:29:43 +0000 | [diff] [blame] | 1244 | case bitc::ATTR_KIND_JUMP_TABLE: |
| 1245 | return Attribute::JumpTable; |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1246 | case bitc::ATTR_KIND_MIN_SIZE: |
| 1247 | return Attribute::MinSize; |
| 1248 | case bitc::ATTR_KIND_NAKED: |
| 1249 | return Attribute::Naked; |
| 1250 | case bitc::ATTR_KIND_NEST: |
| 1251 | return Attribute::Nest; |
| 1252 | case bitc::ATTR_KIND_NO_ALIAS: |
| 1253 | return Attribute::NoAlias; |
| 1254 | case bitc::ATTR_KIND_NO_BUILTIN: |
| 1255 | return Attribute::NoBuiltin; |
| 1256 | case bitc::ATTR_KIND_NO_CAPTURE: |
| 1257 | return Attribute::NoCapture; |
| 1258 | case bitc::ATTR_KIND_NO_DUPLICATE: |
| 1259 | return Attribute::NoDuplicate; |
| 1260 | case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT: |
| 1261 | return Attribute::NoImplicitFloat; |
| 1262 | case bitc::ATTR_KIND_NO_INLINE: |
| 1263 | return Attribute::NoInline; |
James Molloy | e6f87ca | 2015-11-06 10:32:53 +0000 | [diff] [blame] | 1264 | case bitc::ATTR_KIND_NO_RECURSE: |
| 1265 | return Attribute::NoRecurse; |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1266 | case bitc::ATTR_KIND_NON_LAZY_BIND: |
| 1267 | return Attribute::NonLazyBind; |
Nick Lewycky | d52b152 | 2014-05-20 01:23:40 +0000 | [diff] [blame] | 1268 | case bitc::ATTR_KIND_NON_NULL: |
| 1269 | return Attribute::NonNull; |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1270 | case bitc::ATTR_KIND_DEREFERENCEABLE: |
| 1271 | return Attribute::Dereferenceable; |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 1272 | case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL: |
| 1273 | return Attribute::DereferenceableOrNull; |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1274 | case bitc::ATTR_KIND_NO_RED_ZONE: |
| 1275 | return Attribute::NoRedZone; |
| 1276 | case bitc::ATTR_KIND_NO_RETURN: |
| 1277 | return Attribute::NoReturn; |
| 1278 | case bitc::ATTR_KIND_NO_UNWIND: |
| 1279 | return Attribute::NoUnwind; |
| 1280 | case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE: |
| 1281 | return Attribute::OptimizeForSize; |
| 1282 | case bitc::ATTR_KIND_OPTIMIZE_NONE: |
| 1283 | return Attribute::OptimizeNone; |
| 1284 | case bitc::ATTR_KIND_READ_NONE: |
| 1285 | return Attribute::ReadNone; |
| 1286 | case bitc::ATTR_KIND_READ_ONLY: |
| 1287 | return Attribute::ReadOnly; |
| 1288 | case bitc::ATTR_KIND_RETURNED: |
| 1289 | return Attribute::Returned; |
| 1290 | case bitc::ATTR_KIND_RETURNS_TWICE: |
| 1291 | return Attribute::ReturnsTwice; |
| 1292 | case bitc::ATTR_KIND_S_EXT: |
| 1293 | return Attribute::SExt; |
| 1294 | case bitc::ATTR_KIND_STACK_ALIGNMENT: |
| 1295 | return Attribute::StackAlignment; |
| 1296 | case bitc::ATTR_KIND_STACK_PROTECT: |
| 1297 | return Attribute::StackProtect; |
| 1298 | case bitc::ATTR_KIND_STACK_PROTECT_REQ: |
| 1299 | return Attribute::StackProtectReq; |
| 1300 | case bitc::ATTR_KIND_STACK_PROTECT_STRONG: |
| 1301 | return Attribute::StackProtectStrong; |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 1302 | case bitc::ATTR_KIND_SAFESTACK: |
| 1303 | return Attribute::SafeStack; |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1304 | case bitc::ATTR_KIND_STRUCT_RET: |
| 1305 | return Attribute::StructRet; |
| 1306 | case bitc::ATTR_KIND_SANITIZE_ADDRESS: |
| 1307 | return Attribute::SanitizeAddress; |
| 1308 | case bitc::ATTR_KIND_SANITIZE_THREAD: |
| 1309 | return Attribute::SanitizeThread; |
| 1310 | case bitc::ATTR_KIND_SANITIZE_MEMORY: |
| 1311 | return Attribute::SanitizeMemory; |
| 1312 | case bitc::ATTR_KIND_UW_TABLE: |
| 1313 | return Attribute::UWTable; |
| 1314 | case bitc::ATTR_KIND_Z_EXT: |
| 1315 | return Attribute::ZExt; |
| 1316 | } |
| 1317 | } |
| 1318 | |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 1319 | std::error_code BitcodeReader::parseAlignmentValue(uint64_t Exponent, |
| 1320 | unsigned &Alignment) { |
| 1321 | // Note: Alignment in bitcode files is incremented by 1, so that zero |
| 1322 | // can be used for default alignment. |
| 1323 | if (Exponent > Value::MaxAlignmentExponent + 1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1324 | return error("Invalid alignment value"); |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 1325 | Alignment = (1 << static_cast<unsigned>(Exponent)) >> 1; |
| 1326 | return std::error_code(); |
| 1327 | } |
| 1328 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1329 | std::error_code BitcodeReader::parseAttrKind(uint64_t Code, |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1330 | Attribute::AttrKind *Kind) { |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1331 | *Kind = getAttrFromCode(Code); |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1332 | if (*Kind == Attribute::None) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1333 | return error(BitcodeError::CorruptedBitcode, |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1334 | "Unknown attribute kind (" + Twine(Code) + ")"); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1335 | return std::error_code(); |
Tobias Grosser | 0a8e12f | 2013-07-26 04:16:55 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1338 | std::error_code BitcodeReader::parseAttributeGroupBlock() { |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1339 | if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1340 | return error("Invalid record"); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1341 | |
| 1342 | if (!MAttributeGroups.empty()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1343 | return error("Invalid multiple blocks"); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1344 | |
| 1345 | SmallVector<uint64_t, 64> Record; |
| 1346 | |
| 1347 | // Read all the records. |
| 1348 | while (1) { |
| 1349 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| 1350 | |
| 1351 | switch (Entry.Kind) { |
| 1352 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 1353 | case BitstreamEntry::Error: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1354 | return error("Malformed block"); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1355 | case BitstreamEntry::EndBlock: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1356 | return std::error_code(); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1357 | case BitstreamEntry::Record: |
| 1358 | // The interesting case. |
| 1359 | break; |
| 1360 | } |
| 1361 | |
| 1362 | // Read a record. |
| 1363 | Record.clear(); |
| 1364 | switch (Stream.readRecord(Entry.ID, Record)) { |
| 1365 | default: // Default behavior: ignore. |
| 1366 | break; |
| 1367 | case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...] |
| 1368 | if (Record.size() < 3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1369 | return error("Invalid record"); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1370 | |
Bill Wendling | e46707e | 2013-02-11 22:32:29 +0000 | [diff] [blame] | 1371 | uint64_t GrpID = Record[0]; |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1372 | uint64_t Idx = Record[1]; // Index of the object this attribute refers to. |
| 1373 | |
| 1374 | AttrBuilder B; |
| 1375 | for (unsigned i = 2, e = Record.size(); i != e; ++i) { |
| 1376 | if (Record[i] == 0) { // Enum attribute |
Tobias Grosser | 0a8e12f | 2013-07-26 04:16:55 +0000 | [diff] [blame] | 1377 | Attribute::AttrKind Kind; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1378 | if (std::error_code EC = parseAttrKind(Record[++i], &Kind)) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1379 | return EC; |
Tobias Grosser | 0a8e12f | 2013-07-26 04:16:55 +0000 | [diff] [blame] | 1380 | |
| 1381 | B.addAttribute(Kind); |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 1382 | } else if (Record[i] == 1) { // Integer attribute |
Tobias Grosser | 0a8e12f | 2013-07-26 04:16:55 +0000 | [diff] [blame] | 1383 | Attribute::AttrKind Kind; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1384 | if (std::error_code EC = parseAttrKind(Record[++i], &Kind)) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1385 | return EC; |
Tobias Grosser | 0a8e12f | 2013-07-26 04:16:55 +0000 | [diff] [blame] | 1386 | if (Kind == Attribute::Alignment) |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1387 | B.addAlignmentAttr(Record[++i]); |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1388 | else if (Kind == Attribute::StackAlignment) |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1389 | B.addStackAlignmentAttr(Record[++i]); |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1390 | else if (Kind == Attribute::Dereferenceable) |
| 1391 | B.addDereferenceableAttr(Record[++i]); |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 1392 | else if (Kind == Attribute::DereferenceableOrNull) |
| 1393 | B.addDereferenceableOrNullAttr(Record[++i]); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1394 | } else { // String attribute |
Bill Wendling | e46707e | 2013-02-11 22:32:29 +0000 | [diff] [blame] | 1395 | assert((Record[i] == 3 || Record[i] == 4) && |
| 1396 | "Invalid attribute group entry"); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1397 | bool HasValue = (Record[i++] == 4); |
| 1398 | SmallString<64> KindStr; |
| 1399 | SmallString<64> ValStr; |
| 1400 | |
| 1401 | while (Record[i] != 0 && i != e) |
| 1402 | KindStr += Record[i++]; |
Bill Wendling | e46707e | 2013-02-11 22:32:29 +0000 | [diff] [blame] | 1403 | assert(Record[i] == 0 && "Kind string not null terminated"); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1404 | |
| 1405 | if (HasValue) { |
| 1406 | // Has a value associated with it. |
Bill Wendling | e46707e | 2013-02-11 22:32:29 +0000 | [diff] [blame] | 1407 | ++i; // Skip the '0' that terminates the "kind" string. |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1408 | while (Record[i] != 0 && i != e) |
| 1409 | ValStr += Record[i++]; |
Bill Wendling | e46707e | 2013-02-11 22:32:29 +0000 | [diff] [blame] | 1410 | assert(Record[i] == 0 && "Value string not null terminated"); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | B.addAttribute(KindStr.str(), ValStr.str()); |
| 1414 | } |
| 1415 | } |
| 1416 | |
Bill Wendling | e46707e | 2013-02-11 22:32:29 +0000 | [diff] [blame] | 1417 | MAttributeGroups[GrpID] = AttributeSet::get(Context, Idx, B); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1418 | break; |
| 1419 | } |
| 1420 | } |
| 1421 | } |
| 1422 | } |
| 1423 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1424 | std::error_code BitcodeReader::parseTypeTable() { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1425 | if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1426 | return error("Invalid record"); |
Derek Schuff | 206dddd | 2012-02-06 19:03:04 +0000 | [diff] [blame] | 1427 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1428 | return parseTypeTableBody(); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1429 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1430 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1431 | std::error_code BitcodeReader::parseTypeTableBody() { |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1432 | if (!TypeList.empty()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1433 | return error("Invalid multiple blocks"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1434 | |
| 1435 | SmallVector<uint64_t, 64> Record; |
| 1436 | unsigned NumRecords = 0; |
| 1437 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1438 | SmallString<64> TypeName; |
Derek Schuff | 206dddd | 2012-02-06 19:03:04 +0000 | [diff] [blame] | 1439 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1440 | // Read all the records for this type table. |
| 1441 | while (1) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1442 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 1443 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1444 | switch (Entry.Kind) { |
| 1445 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 1446 | case BitstreamEntry::Error: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1447 | return error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1448 | case BitstreamEntry::EndBlock: |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1449 | if (NumRecords != TypeList.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1450 | return error("Malformed block"); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1451 | return std::error_code(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1452 | case BitstreamEntry::Record: |
| 1453 | // The interesting case. |
| 1454 | break; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1455 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1456 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1457 | // Read a record. |
| 1458 | Record.clear(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1459 | Type *ResultTy = nullptr; |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1460 | switch (Stream.readRecord(Entry.ID, Record)) { |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1461 | default: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1462 | return error("Invalid value"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1463 | case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries] |
| 1464 | // TYPE_CODE_NUMENTRY contains a count of the number of types in the |
| 1465 | // type list. This allows us to reserve space. |
| 1466 | if (Record.size() < 1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1467 | return error("Invalid record"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1468 | TypeList.resize(Record[0]); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1469 | continue; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1470 | case bitc::TYPE_CODE_VOID: // VOID |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1471 | ResultTy = Type::getVoidTy(Context); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1472 | break; |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 1473 | case bitc::TYPE_CODE_HALF: // HALF |
| 1474 | ResultTy = Type::getHalfTy(Context); |
| 1475 | break; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1476 | case bitc::TYPE_CODE_FLOAT: // FLOAT |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1477 | ResultTy = Type::getFloatTy(Context); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1478 | break; |
| 1479 | case bitc::TYPE_CODE_DOUBLE: // DOUBLE |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1480 | ResultTy = Type::getDoubleTy(Context); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1481 | break; |
Dale Johannesen | ff4c3be | 2007-08-03 01:03:46 +0000 | [diff] [blame] | 1482 | case bitc::TYPE_CODE_X86_FP80: // X86_FP80 |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1483 | ResultTy = Type::getX86_FP80Ty(Context); |
Dale Johannesen | ff4c3be | 2007-08-03 01:03:46 +0000 | [diff] [blame] | 1484 | break; |
| 1485 | case bitc::TYPE_CODE_FP128: // FP128 |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1486 | ResultTy = Type::getFP128Ty(Context); |
Dale Johannesen | ff4c3be | 2007-08-03 01:03:46 +0000 | [diff] [blame] | 1487 | break; |
| 1488 | case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128 |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1489 | ResultTy = Type::getPPC_FP128Ty(Context); |
Dale Johannesen | ff4c3be | 2007-08-03 01:03:46 +0000 | [diff] [blame] | 1490 | break; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1491 | case bitc::TYPE_CODE_LABEL: // LABEL |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1492 | ResultTy = Type::getLabelTy(Context); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1493 | break; |
Nick Lewycky | adbc284 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 1494 | case bitc::TYPE_CODE_METADATA: // METADATA |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1495 | ResultTy = Type::getMetadataTy(Context); |
Nick Lewycky | adbc284 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 1496 | break; |
Dale Johannesen | baa5d04 | 2010-09-10 20:55:01 +0000 | [diff] [blame] | 1497 | case bitc::TYPE_CODE_X86_MMX: // X86_MMX |
| 1498 | ResultTy = Type::getX86_MMXTy(Context); |
| 1499 | break; |
David Majnemer | b611e3f | 2015-08-14 05:09:07 +0000 | [diff] [blame] | 1500 | case bitc::TYPE_CODE_TOKEN: // TOKEN |
| 1501 | ResultTy = Type::getTokenTy(Context); |
| 1502 | break; |
Filipe Cabecinhas | fcd044b | 2015-01-30 18:13:50 +0000 | [diff] [blame] | 1503 | case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1504 | if (Record.size() < 1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1505 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1506 | |
Filipe Cabecinhas | fcd044b | 2015-01-30 18:13:50 +0000 | [diff] [blame] | 1507 | uint64_t NumBits = Record[0]; |
| 1508 | if (NumBits < IntegerType::MIN_INT_BITS || |
| 1509 | NumBits > IntegerType::MAX_INT_BITS) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1510 | return error("Bitwidth for integer type out of range"); |
Filipe Cabecinhas | fcd044b | 2015-01-30 18:13:50 +0000 | [diff] [blame] | 1511 | ResultTy = IntegerType::get(Context, NumBits); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1512 | break; |
Filipe Cabecinhas | fcd044b | 2015-01-30 18:13:50 +0000 | [diff] [blame] | 1513 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1514 | case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 1515 | // [pointee type, address space] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1516 | if (Record.size() < 1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1517 | return error("Invalid record"); |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 1518 | unsigned AddressSpace = 0; |
| 1519 | if (Record.size() == 2) |
| 1520 | AddressSpace = Record[1]; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1521 | ResultTy = getTypeByID(Record[0]); |
Filipe Cabecinhas | d8a1bcd | 2015-04-29 02:27:28 +0000 | [diff] [blame] | 1522 | if (!ResultTy || |
| 1523 | !PointerType::isValidElementType(ResultTy)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1524 | return error("Invalid type"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1525 | ResultTy = PointerType::get(ResultTy, AddressSpace); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1526 | break; |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 1527 | } |
Nuno Lopes | 561dae0 | 2012-05-23 15:19:39 +0000 | [diff] [blame] | 1528 | case bitc::TYPE_CODE_FUNCTION_OLD: { |
| 1529 | // FIXME: attrid is dead, remove it in LLVM 4.0 |
| 1530 | // FUNCTION: [vararg, attrid, retty, paramty x N] |
| 1531 | if (Record.size() < 3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1532 | return error("Invalid record"); |
Nuno Lopes | 561dae0 | 2012-05-23 15:19:39 +0000 | [diff] [blame] | 1533 | SmallVector<Type*, 8> ArgTys; |
| 1534 | for (unsigned i = 3, e = Record.size(); i != e; ++i) { |
| 1535 | if (Type *T = getTypeByID(Record[i])) |
| 1536 | ArgTys.push_back(T); |
| 1537 | else |
| 1538 | break; |
| 1539 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1540 | |
Nuno Lopes | 561dae0 | 2012-05-23 15:19:39 +0000 | [diff] [blame] | 1541 | ResultTy = getTypeByID(Record[2]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1542 | if (!ResultTy || ArgTys.size() < Record.size()-3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1543 | return error("Invalid type"); |
Nuno Lopes | 561dae0 | 2012-05-23 15:19:39 +0000 | [diff] [blame] | 1544 | |
| 1545 | ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); |
| 1546 | break; |
| 1547 | } |
Chad Rosier | 9589872 | 2011-11-03 00:14:01 +0000 | [diff] [blame] | 1548 | case bitc::TYPE_CODE_FUNCTION: { |
| 1549 | // FUNCTION: [vararg, retty, paramty x N] |
| 1550 | if (Record.size() < 2) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1551 | return error("Invalid record"); |
Chris Lattner | cc3aaf1 | 2012-01-27 03:15:49 +0000 | [diff] [blame] | 1552 | SmallVector<Type*, 8> ArgTys; |
Chad Rosier | 9589872 | 2011-11-03 00:14:01 +0000 | [diff] [blame] | 1553 | for (unsigned i = 2, e = Record.size(); i != e; ++i) { |
Filipe Cabecinhas | 32af542 | 2015-05-19 01:21:06 +0000 | [diff] [blame] | 1554 | if (Type *T = getTypeByID(Record[i])) { |
| 1555 | if (!FunctionType::isValidArgumentType(T)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1556 | return error("Invalid function argument type"); |
Chad Rosier | 9589872 | 2011-11-03 00:14:01 +0000 | [diff] [blame] | 1557 | ArgTys.push_back(T); |
Filipe Cabecinhas | 32af542 | 2015-05-19 01:21:06 +0000 | [diff] [blame] | 1558 | } |
Chad Rosier | 9589872 | 2011-11-03 00:14:01 +0000 | [diff] [blame] | 1559 | else |
| 1560 | break; |
| 1561 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1562 | |
Chad Rosier | 9589872 | 2011-11-03 00:14:01 +0000 | [diff] [blame] | 1563 | ResultTy = getTypeByID(Record[1]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1564 | if (!ResultTy || ArgTys.size() < Record.size()-2) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1565 | return error("Invalid type"); |
Chad Rosier | 9589872 | 2011-11-03 00:14:01 +0000 | [diff] [blame] | 1566 | |
| 1567 | ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); |
| 1568 | break; |
| 1569 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1570 | case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N] |
Chris Lattner | 3c5616e | 2007-05-06 08:21:50 +0000 | [diff] [blame] | 1571 | if (Record.size() < 1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1572 | return error("Invalid record"); |
Chris Lattner | cc3aaf1 | 2012-01-27 03:15:49 +0000 | [diff] [blame] | 1573 | SmallVector<Type*, 8> EltTys; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1574 | for (unsigned i = 1, e = Record.size(); i != e; ++i) { |
| 1575 | if (Type *T = getTypeByID(Record[i])) |
| 1576 | EltTys.push_back(T); |
| 1577 | else |
| 1578 | break; |
| 1579 | } |
| 1580 | if (EltTys.size() != Record.size()-1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1581 | return error("Invalid type"); |
Owen Anderson | 03cb69f | 2009-08-05 23:16:16 +0000 | [diff] [blame] | 1582 | ResultTy = StructType::get(Context, EltTys, Record[0]); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1583 | break; |
| 1584 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1585 | case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N] |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1586 | if (convertToString(Record, 0, TypeName)) |
| 1587 | return error("Invalid record"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1588 | continue; |
| 1589 | |
| 1590 | case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N] |
| 1591 | if (Record.size() < 1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1592 | return error("Invalid record"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1593 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1594 | if (NumRecords >= TypeList.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1595 | return error("Invalid TYPE table"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1596 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1597 | // Check to see if this was forward referenced, if so fill in the temp. |
| 1598 | StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); |
| 1599 | if (Res) { |
| 1600 | Res->setName(TypeName); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1601 | TypeList[NumRecords] = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1602 | } else // Otherwise, create a new struct. |
Rafael Espindola | 2fa1e43 | 2014-12-03 07:18:23 +0000 | [diff] [blame] | 1603 | Res = createIdentifiedStructType(Context, TypeName); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1604 | TypeName.clear(); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1605 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1606 | SmallVector<Type*, 8> EltTys; |
| 1607 | for (unsigned i = 1, e = Record.size(); i != e; ++i) { |
| 1608 | if (Type *T = getTypeByID(Record[i])) |
| 1609 | EltTys.push_back(T); |
| 1610 | else |
| 1611 | break; |
| 1612 | } |
| 1613 | if (EltTys.size() != Record.size()-1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1614 | return error("Invalid record"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1615 | Res->setBody(EltTys, Record[0]); |
| 1616 | ResultTy = Res; |
| 1617 | break; |
| 1618 | } |
| 1619 | case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: [] |
| 1620 | if (Record.size() != 1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1621 | return error("Invalid record"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1622 | |
| 1623 | if (NumRecords >= TypeList.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1624 | return error("Invalid TYPE table"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1625 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1626 | // Check to see if this was forward referenced, if so fill in the temp. |
| 1627 | StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); |
| 1628 | if (Res) { |
| 1629 | Res->setName(TypeName); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1630 | TypeList[NumRecords] = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1631 | } else // Otherwise, create a new struct with no body. |
Rafael Espindola | 2fa1e43 | 2014-12-03 07:18:23 +0000 | [diff] [blame] | 1632 | Res = createIdentifiedStructType(Context, TypeName); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1633 | TypeName.clear(); |
| 1634 | ResultTy = Res; |
| 1635 | break; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1636 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1637 | case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty] |
| 1638 | if (Record.size() < 2) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1639 | return error("Invalid record"); |
Filipe Cabecinhas | 6fe8aab | 2015-04-29 02:36:08 +0000 | [diff] [blame] | 1640 | ResultTy = getTypeByID(Record[1]); |
| 1641 | if (!ResultTy || !ArrayType::isValidElementType(ResultTy)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1642 | return error("Invalid type"); |
Filipe Cabecinhas | 6fe8aab | 2015-04-29 02:36:08 +0000 | [diff] [blame] | 1643 | ResultTy = ArrayType::get(ResultTy, Record[0]); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1644 | break; |
| 1645 | case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] |
| 1646 | if (Record.size() < 2) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1647 | return error("Invalid record"); |
Filipe Cabecinhas | 8e42190 | 2015-06-03 00:05:30 +0000 | [diff] [blame] | 1648 | if (Record[0] == 0) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1649 | return error("Invalid vector length"); |
Filipe Cabecinhas | 6fe8aab | 2015-04-29 02:36:08 +0000 | [diff] [blame] | 1650 | ResultTy = getTypeByID(Record[1]); |
| 1651 | if (!ResultTy || !StructType::isValidElementType(ResultTy)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1652 | return error("Invalid type"); |
Filipe Cabecinhas | 6fe8aab | 2015-04-29 02:36:08 +0000 | [diff] [blame] | 1653 | ResultTy = VectorType::get(ResultTy, Record[0]); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1654 | break; |
| 1655 | } |
| 1656 | |
| 1657 | if (NumRecords >= TypeList.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1658 | return error("Invalid TYPE table"); |
Filipe Cabecinhas | d0858e1 | 2015-01-30 10:57:58 +0000 | [diff] [blame] | 1659 | if (TypeList[NumRecords]) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1660 | return error( |
Filipe Cabecinhas | d0858e1 | 2015-01-30 10:57:58 +0000 | [diff] [blame] | 1661 | "Invalid TYPE table: Only named structs can be forward referenced"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1662 | assert(ResultTy && "Didn't read a type?"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1663 | TypeList[NumRecords++] = ResultTy; |
| 1664 | } |
| 1665 | } |
| 1666 | |
Sanjoy Das | b513a9f | 2015-09-24 23:34:52 +0000 | [diff] [blame] | 1667 | std::error_code BitcodeReader::parseOperandBundleTags() { |
| 1668 | if (Stream.EnterSubBlock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID)) |
| 1669 | return error("Invalid record"); |
| 1670 | |
| 1671 | if (!BundleTags.empty()) |
| 1672 | return error("Invalid multiple blocks"); |
| 1673 | |
| 1674 | SmallVector<uint64_t, 64> Record; |
| 1675 | |
| 1676 | while (1) { |
| 1677 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| 1678 | |
| 1679 | switch (Entry.Kind) { |
| 1680 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 1681 | case BitstreamEntry::Error: |
| 1682 | return error("Malformed block"); |
| 1683 | case BitstreamEntry::EndBlock: |
| 1684 | return std::error_code(); |
| 1685 | case BitstreamEntry::Record: |
| 1686 | // The interesting case. |
| 1687 | break; |
| 1688 | } |
| 1689 | |
| 1690 | // Tags are implicitly mapped to integers by their order. |
| 1691 | |
| 1692 | if (Stream.readRecord(Entry.ID, Record) != bitc::OPERAND_BUNDLE_TAG) |
| 1693 | return error("Invalid record"); |
| 1694 | |
| 1695 | // OPERAND_BUNDLE_TAG: [strchr x N] |
| 1696 | BundleTags.emplace_back(); |
| 1697 | if (convertToString(Record, 0, BundleTags.back())) |
| 1698 | return error("Invalid record"); |
| 1699 | Record.clear(); |
| 1700 | } |
| 1701 | } |
| 1702 | |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 1703 | /// Associate a value with its name from the given index in the provided record. |
| 1704 | ErrorOr<Value *> BitcodeReader::recordValue(SmallVectorImpl<uint64_t> &Record, |
| 1705 | unsigned NameIndex, Triple &TT) { |
| 1706 | SmallString<128> ValueName; |
| 1707 | if (convertToString(Record, NameIndex, ValueName)) |
| 1708 | return error("Invalid record"); |
| 1709 | unsigned ValueID = Record[0]; |
| 1710 | if (ValueID >= ValueList.size() || !ValueList[ValueID]) |
| 1711 | return error("Invalid record"); |
| 1712 | Value *V = ValueList[ValueID]; |
| 1713 | |
Filipe Cabecinhas | a2b0ac4 | 2015-11-04 14:53:36 +0000 | [diff] [blame] | 1714 | StringRef NameStr(ValueName.data(), ValueName.size()); |
| 1715 | if (NameStr.find_first_of(0) != StringRef::npos) |
| 1716 | return error("Invalid value name"); |
| 1717 | V->setName(NameStr); |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 1718 | auto *GO = dyn_cast<GlobalObject>(V); |
| 1719 | if (GO) { |
| 1720 | if (GO->getComdat() == reinterpret_cast<Comdat *>(1)) { |
| 1721 | if (TT.isOSBinFormatMachO()) |
| 1722 | GO->setComdat(nullptr); |
| 1723 | else |
| 1724 | GO->setComdat(TheModule->getOrInsertComdat(V->getName())); |
| 1725 | } |
| 1726 | } |
| 1727 | return V; |
| 1728 | } |
| 1729 | |
| 1730 | /// Parse the value symbol table at either the current parsing location or |
| 1731 | /// at the given bit offset if provided. |
Peter Collingbourne | 128a976 | 2015-10-27 23:01:25 +0000 | [diff] [blame] | 1732 | std::error_code BitcodeReader::parseValueSymbolTable(uint64_t Offset) { |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 1733 | uint64_t CurrentBit; |
| 1734 | // Pass in the Offset to distinguish between calling for the module-level |
| 1735 | // VST (where we want to jump to the VST offset) and the function-level |
| 1736 | // VST (where we don't). |
| 1737 | if (Offset > 0) { |
| 1738 | // Save the current parsing location so we can jump back at the end |
| 1739 | // of the VST read. |
| 1740 | CurrentBit = Stream.GetCurrentBitNo(); |
| 1741 | Stream.JumpToBit(Offset * 32); |
| 1742 | #ifndef NDEBUG |
| 1743 | // Do some checking if we are in debug mode. |
| 1744 | BitstreamEntry Entry = Stream.advance(); |
| 1745 | assert(Entry.Kind == BitstreamEntry::SubBlock); |
| 1746 | assert(Entry.ID == bitc::VALUE_SYMTAB_BLOCK_ID); |
| 1747 | #else |
| 1748 | // In NDEBUG mode ignore the output so we don't get an unused variable |
| 1749 | // warning. |
| 1750 | Stream.advance(); |
| 1751 | #endif |
| 1752 | } |
| 1753 | |
| 1754 | // Compute the delta between the bitcode indices in the VST (the word offset |
| 1755 | // to the word-aligned ENTER_SUBBLOCK for the function block, and that |
| 1756 | // expected by the lazy reader. The reader's EnterSubBlock expects to have |
| 1757 | // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID |
| 1758 | // (size BlockIDWidth). Note that we access the stream's AbbrevID width here |
| 1759 | // just before entering the VST subblock because: 1) the EnterSubBlock |
| 1760 | // changes the AbbrevID width; 2) the VST block is nested within the same |
| 1761 | // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same |
| 1762 | // AbbrevID width before calling EnterSubBlock; and 3) when we want to |
| 1763 | // jump to the FUNCTION_BLOCK using this offset later, we don't want |
| 1764 | // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK. |
| 1765 | unsigned FuncBitcodeOffsetDelta = |
| 1766 | Stream.getAbbrevIDWidth() + bitc::BlockIDWidth; |
| 1767 | |
Chris Lattner | 982ec1e | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1768 | if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1769 | return error("Invalid record"); |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1770 | |
| 1771 | SmallVector<uint64_t, 64> Record; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1772 | |
David Majnemer | 3087b22 | 2015-01-20 05:58:07 +0000 | [diff] [blame] | 1773 | Triple TT(TheModule->getTargetTriple()); |
| 1774 | |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1775 | // Read all the records for this value table. |
| 1776 | SmallString<128> ValueName; |
| 1777 | while (1) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1778 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 1779 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1780 | switch (Entry.Kind) { |
| 1781 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 1782 | case BitstreamEntry::Error: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1783 | return error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1784 | case BitstreamEntry::EndBlock: |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 1785 | if (Offset > 0) |
| 1786 | Stream.JumpToBit(CurrentBit); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1787 | return std::error_code(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1788 | case BitstreamEntry::Record: |
| 1789 | // The interesting case. |
| 1790 | break; |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1791 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1792 | |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1793 | // Read a record. |
| 1794 | Record.clear(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1795 | switch (Stream.readRecord(Entry.ID, Record)) { |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1796 | default: // Default behavior: unknown type. |
| 1797 | break; |
Teresa Johnson | 79d4e2f | 2016-02-10 15:02:51 +0000 | [diff] [blame] | 1798 | case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N] |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 1799 | ErrorOr<Value *> ValOrErr = recordValue(Record, 1, TT); |
| 1800 | if (std::error_code EC = ValOrErr.getError()) |
| 1801 | return EC; |
| 1802 | ValOrErr.get(); |
| 1803 | break; |
| 1804 | } |
| 1805 | case bitc::VST_CODE_FNENTRY: { |
Teresa Johnson | 79d4e2f | 2016-02-10 15:02:51 +0000 | [diff] [blame] | 1806 | // VST_CODE_FNENTRY: [valueid, offset, namechar x N] |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 1807 | ErrorOr<Value *> ValOrErr = recordValue(Record, 2, TT); |
| 1808 | if (std::error_code EC = ValOrErr.getError()) |
| 1809 | return EC; |
| 1810 | Value *V = ValOrErr.get(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1811 | |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 1812 | auto *GO = dyn_cast<GlobalObject>(V); |
| 1813 | if (!GO) { |
| 1814 | // If this is an alias, need to get the actual Function object |
| 1815 | // it aliases, in order to set up the DeferredFunctionInfo entry below. |
| 1816 | auto *GA = dyn_cast<GlobalAlias>(V); |
| 1817 | if (GA) |
| 1818 | GO = GA->getBaseObject(); |
| 1819 | assert(GO); |
Rafael Espindola | 12ca34f | 2015-01-19 15:16:06 +0000 | [diff] [blame] | 1820 | } |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 1821 | |
| 1822 | uint64_t FuncWordOffset = Record[1]; |
| 1823 | Function *F = dyn_cast<Function>(GO); |
| 1824 | assert(F); |
| 1825 | uint64_t FuncBitOffset = FuncWordOffset * 32; |
| 1826 | DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta; |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 1827 | // Set the LastFunctionBlockBit to point to the last function block. |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 1828 | // Later when parsing is resumed after function materialization, |
| 1829 | // we can simply skip that last function block. |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 1830 | if (FuncBitOffset > LastFunctionBlockBit) |
| 1831 | LastFunctionBlockBit = FuncBitOffset; |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1832 | break; |
Reid Spencer | dea02bd | 2007-05-04 01:43:33 +0000 | [diff] [blame] | 1833 | } |
Bill Wendling | 35a9c3c | 2011-04-10 23:18:04 +0000 | [diff] [blame] | 1834 | case bitc::VST_CODE_BBENTRY: { |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1835 | if (convertToString(Record, 1, ValueName)) |
| 1836 | return error("Invalid record"); |
Chris Lattner | 6be58c6 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 1837 | BasicBlock *BB = getBasicBlock(Record[0]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1838 | if (!BB) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1839 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1840 | |
Daniel Dunbar | d786b51 | 2009-07-26 00:34:27 +0000 | [diff] [blame] | 1841 | BB->setName(StringRef(ValueName.data(), ValueName.size())); |
Chris Lattner | 6be58c6 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 1842 | ValueName.clear(); |
| 1843 | break; |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1844 | } |
Reid Spencer | dea02bd | 2007-05-04 01:43:33 +0000 | [diff] [blame] | 1845 | } |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1846 | } |
| 1847 | } |
| 1848 | |
Teresa Johnson | 1254507 | 2015-11-15 02:00:09 +0000 | [diff] [blame] | 1849 | /// Parse a single METADATA_KIND record, inserting result in MDKindMap. |
| 1850 | std::error_code |
| 1851 | BitcodeReader::parseMetadataKindRecord(SmallVectorImpl<uint64_t> &Record) { |
| 1852 | if (Record.size() < 2) |
| 1853 | return error("Invalid record"); |
| 1854 | |
| 1855 | unsigned Kind = Record[0]; |
| 1856 | SmallString<8> Name(Record.begin() + 1, Record.end()); |
| 1857 | |
| 1858 | unsigned NewKind = TheModule->getMDKindID(Name.str()); |
| 1859 | if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second) |
| 1860 | return error("Conflicting METADATA_KIND records"); |
| 1861 | return std::error_code(); |
| 1862 | } |
| 1863 | |
Duncan P. N. Exon Smith | c7363f1 | 2015-02-13 01:10:38 +0000 | [diff] [blame] | 1864 | static int64_t unrotateSign(uint64_t U) { return U & 1 ? ~(U >> 1) : U >> 1; } |
| 1865 | |
Teresa Johnson | d4d3dfd | 2015-11-20 14:51:27 +0000 | [diff] [blame] | 1866 | /// Parse a METADATA_BLOCK. If ModuleLevel is true then we are parsing |
| 1867 | /// module level metadata. |
| 1868 | std::error_code BitcodeReader::parseMetadata(bool ModuleLevel) { |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 1869 | IsMetadataMaterialized = true; |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 1870 | unsigned NextMetadataNo = MetadataList.size(); |
Teresa Johnson | d4d3dfd | 2015-11-20 14:51:27 +0000 | [diff] [blame] | 1871 | if (ModuleLevel && SeenModuleValuesRecord) { |
| 1872 | // Now that we are parsing the module level metadata, we want to restart |
| 1873 | // the numbering of the MD values, and replace temp MD created earlier |
| 1874 | // with their real values. If we saw a METADATA_VALUE record then we |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 1875 | // would have set the MetadataList size to the number specified in that |
Teresa Johnson | d4d3dfd | 2015-11-20 14:51:27 +0000 | [diff] [blame] | 1876 | // record, to support parsing function-level metadata first, and we need |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 1877 | // to reset back to 0 to fill the MetadataList in with the parsed module |
| 1878 | // The function-level metadata parsing should have reset the MetadataList |
Teresa Johnson | d4d3dfd | 2015-11-20 14:51:27 +0000 | [diff] [blame] | 1879 | // size back to the value reported by the METADATA_VALUE record, saved in |
| 1880 | // NumModuleMDs. |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 1881 | assert(NumModuleMDs == MetadataList.size() && |
| 1882 | "Expected MetadataList to only contain module level values"); |
| 1883 | NextMetadataNo = 0; |
Teresa Johnson | d4d3dfd | 2015-11-20 14:51:27 +0000 | [diff] [blame] | 1884 | } |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1885 | |
| 1886 | if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1887 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1888 | |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1889 | SmallVector<uint64_t, 64> Record; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1890 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 1891 | auto getMD = [&](unsigned ID) -> Metadata * { |
| 1892 | return MetadataList.getValueFwdRef(ID); |
| 1893 | }; |
Duncan P. N. Exon Smith | 171d077 | 2015-02-13 01:20:38 +0000 | [diff] [blame] | 1894 | auto getMDOrNull = [&](unsigned ID) -> Metadata *{ |
| 1895 | if (ID) |
| 1896 | return getMD(ID - 1); |
| 1897 | return nullptr; |
| 1898 | }; |
Duncan P. N. Exon Smith | 4e4aa70 | 2015-02-03 21:54:14 +0000 | [diff] [blame] | 1899 | auto getMDString = [&](unsigned ID) -> MDString *{ |
| 1900 | // This requires that the ID is not really a forward reference. In |
| 1901 | // particular, the MDString must already have been resolved. |
Duncan P. N. Exon Smith | 171d077 | 2015-02-13 01:20:38 +0000 | [diff] [blame] | 1902 | return cast_or_null<MDString>(getMDOrNull(ID)); |
Duncan P. N. Exon Smith | 4e4aa70 | 2015-02-03 21:54:14 +0000 | [diff] [blame] | 1903 | }; |
| 1904 | |
| 1905 | #define GET_OR_DISTINCT(CLASS, DISTINCT, ARGS) \ |
| 1906 | (DISTINCT ? CLASS::getDistinct ARGS : CLASS::get ARGS) |
| 1907 | |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1908 | // Read all the records. |
| 1909 | while (1) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1910 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 1911 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1912 | switch (Entry.Kind) { |
| 1913 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 1914 | case BitstreamEntry::Error: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1915 | return error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1916 | case BitstreamEntry::EndBlock: |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 1917 | MetadataList.tryToResolveCycles(); |
Teresa Johnson | 16e2a9e | 2015-11-21 03:51:23 +0000 | [diff] [blame] | 1918 | assert((!(ModuleLevel && SeenModuleValuesRecord) || |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 1919 | NumModuleMDs == MetadataList.size()) && |
Teresa Johnson | 16e2a9e | 2015-11-21 03:51:23 +0000 | [diff] [blame] | 1920 | "Inconsistent bitcode: METADATA_VALUES mismatch"); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1921 | return std::error_code(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1922 | case BitstreamEntry::Record: |
| 1923 | // The interesting case. |
| 1924 | break; |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1925 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1926 | |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1927 | // Read a record. |
| 1928 | Record.clear(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1929 | unsigned Code = Stream.readRecord(Entry.ID, Record); |
Duncan P. N. Exon Smith | 090a19b | 2015-01-08 22:38:29 +0000 | [diff] [blame] | 1930 | bool IsDistinct = false; |
Dan Gohman | bbcd04d | 2010-09-13 18:00:48 +0000 | [diff] [blame] | 1931 | switch (Code) { |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1932 | default: // Default behavior: ignore. |
| 1933 | break; |
Devang Patel | 27c87ff | 2009-07-29 22:34:41 +0000 | [diff] [blame] | 1934 | case bitc::METADATA_NAME: { |
Chris Lattner | 8d14053 | 2013-01-20 02:54:05 +0000 | [diff] [blame] | 1935 | // Read name of the named metadata. |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 1936 | SmallString<8> Name(Record.begin(), Record.end()); |
Devang Patel | 27c87ff | 2009-07-29 22:34:41 +0000 | [diff] [blame] | 1937 | Record.clear(); |
| 1938 | Code = Stream.ReadCode(); |
| 1939 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1940 | unsigned NextBitCode = Stream.readRecord(Code, Record); |
Filipe Cabecinhas | 14e6867 | 2015-05-30 00:17:20 +0000 | [diff] [blame] | 1941 | if (NextBitCode != bitc::METADATA_NAMED_NODE) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1942 | return error("METADATA_NAME not followed by METADATA_NAMED_NODE"); |
Devang Patel | 27c87ff | 2009-07-29 22:34:41 +0000 | [diff] [blame] | 1943 | |
| 1944 | // Read named metadata elements. |
| 1945 | unsigned Size = Record.size(); |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 1946 | NamedMDNode *NMD = TheModule->getOrInsertNamedMetadata(Name); |
Devang Patel | 27c87ff | 2009-07-29 22:34:41 +0000 | [diff] [blame] | 1947 | for (unsigned i = 0; i != Size; ++i) { |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 1948 | MDNode *MD = |
| 1949 | dyn_cast_or_null<MDNode>(MetadataList.getValueFwdRef(Record[i])); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1950 | if (!MD) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1951 | return error("Invalid record"); |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 1952 | NMD->addOperand(MD); |
Devang Patel | 27c87ff | 2009-07-29 22:34:41 +0000 | [diff] [blame] | 1953 | } |
Devang Patel | 27c87ff | 2009-07-29 22:34:41 +0000 | [diff] [blame] | 1954 | break; |
| 1955 | } |
Duncan P. N. Exon Smith | 005f9f4 | 2014-12-11 22:30:48 +0000 | [diff] [blame] | 1956 | case bitc::METADATA_OLD_FN_NODE: { |
Duncan P. N. Exon Smith | 5bd34e5 | 2014-12-12 02:11:31 +0000 | [diff] [blame] | 1957 | // FIXME: Remove in 4.0. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1958 | // This is a LocalAsMetadata record, the only type of function-local |
| 1959 | // metadata. |
Duncan P. N. Exon Smith | da41af9 | 2014-12-06 01:26:49 +0000 | [diff] [blame] | 1960 | if (Record.size() % 2 == 1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1961 | return error("Invalid record"); |
Duncan P. N. Exon Smith | da41af9 | 2014-12-06 01:26:49 +0000 | [diff] [blame] | 1962 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1963 | // If this isn't a LocalAsMetadata record, we're dropping it. This used |
| 1964 | // to be legal, but there's no upgrade path. |
Duncan P. N. Exon Smith | da41af9 | 2014-12-06 01:26:49 +0000 | [diff] [blame] | 1965 | auto dropRecord = [&] { |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 1966 | MetadataList.assignValue(MDNode::get(Context, None), NextMetadataNo++); |
Duncan P. N. Exon Smith | da41af9 | 2014-12-06 01:26:49 +0000 | [diff] [blame] | 1967 | }; |
| 1968 | if (Record.size() != 2) { |
| 1969 | dropRecord(); |
| 1970 | break; |
| 1971 | } |
| 1972 | |
| 1973 | Type *Ty = getTypeByID(Record[0]); |
| 1974 | if (Ty->isMetadataTy() || Ty->isVoidTy()) { |
| 1975 | dropRecord(); |
| 1976 | break; |
| 1977 | } |
| 1978 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 1979 | MetadataList.assignValue( |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1980 | LocalAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)), |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 1981 | NextMetadataNo++); |
Duncan P. N. Exon Smith | da41af9 | 2014-12-06 01:26:49 +0000 | [diff] [blame] | 1982 | break; |
| 1983 | } |
Duncan P. N. Exon Smith | 005f9f4 | 2014-12-11 22:30:48 +0000 | [diff] [blame] | 1984 | case bitc::METADATA_OLD_NODE: { |
Duncan P. N. Exon Smith | 5bd34e5 | 2014-12-12 02:11:31 +0000 | [diff] [blame] | 1985 | // FIXME: Remove in 4.0. |
Dan Gohman | 1e0213a | 2010-07-13 19:33:27 +0000 | [diff] [blame] | 1986 | if (Record.size() % 2 == 1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1987 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1988 | |
Devang Patel | e059ba6e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 1989 | unsigned Size = Record.size(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1990 | SmallVector<Metadata *, 8> Elts; |
Devang Patel | e059ba6e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 1991 | for (unsigned i = 0; i != Size; i += 2) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1992 | Type *Ty = getTypeByID(Record[i]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1993 | if (!Ty) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1994 | return error("Invalid record"); |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 1995 | if (Ty->isMetadataTy()) |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 1996 | Elts.push_back(MetadataList.getValueFwdRef(Record[i + 1])); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1997 | else if (!Ty->isVoidTy()) { |
| 1998 | auto *MD = |
| 1999 | ValueAsMetadata::get(ValueList.getValueFwdRef(Record[i + 1], Ty)); |
| 2000 | assert(isa<ConstantAsMetadata>(MD) && |
| 2001 | "Expected non-function-local metadata"); |
| 2002 | Elts.push_back(MD); |
| 2003 | } else |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2004 | Elts.push_back(nullptr); |
Devang Patel | e059ba6e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 2005 | } |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2006 | MetadataList.assignValue(MDNode::get(Context, Elts), NextMetadataNo++); |
Devang Patel | e059ba6e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 2007 | break; |
| 2008 | } |
Duncan P. N. Exon Smith | 5c7006e | 2014-12-11 23:02:24 +0000 | [diff] [blame] | 2009 | case bitc::METADATA_VALUE: { |
| 2010 | if (Record.size() != 2) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2011 | return error("Invalid record"); |
Duncan P. N. Exon Smith | 5c7006e | 2014-12-11 23:02:24 +0000 | [diff] [blame] | 2012 | |
| 2013 | Type *Ty = getTypeByID(Record[0]); |
| 2014 | if (Ty->isMetadataTy() || Ty->isVoidTy()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2015 | return error("Invalid record"); |
Duncan P. N. Exon Smith | 5c7006e | 2014-12-11 23:02:24 +0000 | [diff] [blame] | 2016 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2017 | MetadataList.assignValue( |
Duncan P. N. Exon Smith | 5c7006e | 2014-12-11 23:02:24 +0000 | [diff] [blame] | 2018 | ValueAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)), |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2019 | NextMetadataNo++); |
Duncan P. N. Exon Smith | 5c7006e | 2014-12-11 23:02:24 +0000 | [diff] [blame] | 2020 | break; |
| 2021 | } |
Duncan P. N. Exon Smith | 090a19b | 2015-01-08 22:38:29 +0000 | [diff] [blame] | 2022 | case bitc::METADATA_DISTINCT_NODE: |
| 2023 | IsDistinct = true; |
| 2024 | // fallthrough... |
Duncan P. N. Exon Smith | 5c7006e | 2014-12-11 23:02:24 +0000 | [diff] [blame] | 2025 | case bitc::METADATA_NODE: { |
| 2026 | SmallVector<Metadata *, 8> Elts; |
| 2027 | Elts.reserve(Record.size()); |
| 2028 | for (unsigned ID : Record) |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2029 | Elts.push_back(ID ? MetadataList.getValueFwdRef(ID - 1) : nullptr); |
| 2030 | MetadataList.assignValue(IsDistinct ? MDNode::getDistinct(Context, Elts) |
| 2031 | : MDNode::get(Context, Elts), |
| 2032 | NextMetadataNo++); |
Duncan P. N. Exon Smith | 5c7006e | 2014-12-11 23:02:24 +0000 | [diff] [blame] | 2033 | break; |
| 2034 | } |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 2035 | case bitc::METADATA_LOCATION: { |
| 2036 | if (Record.size() != 5) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2037 | return error("Invalid record"); |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 2038 | |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 2039 | unsigned Line = Record[1]; |
| 2040 | unsigned Column = Record[2]; |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2041 | MDNode *Scope = cast<MDNode>(MetadataList.getValueFwdRef(Record[3])); |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 2042 | Metadata *InlinedAt = |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2043 | Record[4] ? MetadataList.getValueFwdRef(Record[4] - 1) : nullptr; |
| 2044 | MetadataList.assignValue( |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 2045 | GET_OR_DISTINCT(DILocation, Record[0], |
Duncan P. N. Exon Smith | 2648998 | 2015-03-26 22:05:04 +0000 | [diff] [blame] | 2046 | (Context, Line, Column, Scope, InlinedAt)), |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2047 | NextMetadataNo++); |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 2048 | break; |
| 2049 | } |
Duncan P. N. Exon Smith | 4e4aa70 | 2015-02-03 21:54:14 +0000 | [diff] [blame] | 2050 | case bitc::METADATA_GENERIC_DEBUG: { |
| 2051 | if (Record.size() < 4) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2052 | return error("Invalid record"); |
Duncan P. N. Exon Smith | 4e4aa70 | 2015-02-03 21:54:14 +0000 | [diff] [blame] | 2053 | |
| 2054 | unsigned Tag = Record[1]; |
| 2055 | unsigned Version = Record[2]; |
| 2056 | |
| 2057 | if (Tag >= 1u << 16 || Version != 0) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2058 | return error("Invalid record"); |
Duncan P. N. Exon Smith | 4e4aa70 | 2015-02-03 21:54:14 +0000 | [diff] [blame] | 2059 | |
| 2060 | auto *Header = getMDString(Record[3]); |
| 2061 | SmallVector<Metadata *, 8> DwarfOps; |
| 2062 | for (unsigned I = 4, E = Record.size(); I != E; ++I) |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2063 | DwarfOps.push_back( |
| 2064 | Record[I] ? MetadataList.getValueFwdRef(Record[I] - 1) : nullptr); |
| 2065 | MetadataList.assignValue( |
| 2066 | GET_OR_DISTINCT(GenericDINode, Record[0], |
| 2067 | (Context, Tag, Header, DwarfOps)), |
| 2068 | NextMetadataNo++); |
Duncan P. N. Exon Smith | 4e4aa70 | 2015-02-03 21:54:14 +0000 | [diff] [blame] | 2069 | break; |
| 2070 | } |
Duncan P. N. Exon Smith | c7363f1 | 2015-02-13 01:10:38 +0000 | [diff] [blame] | 2071 | case bitc::METADATA_SUBRANGE: { |
| 2072 | if (Record.size() != 3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2073 | return error("Invalid record"); |
Duncan P. N. Exon Smith | c7363f1 | 2015-02-13 01:10:38 +0000 | [diff] [blame] | 2074 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2075 | MetadataList.assignValue( |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 2076 | GET_OR_DISTINCT(DISubrange, Record[0], |
Duncan P. N. Exon Smith | c7363f1 | 2015-02-13 01:10:38 +0000 | [diff] [blame] | 2077 | (Context, Record[1], unrotateSign(Record[2]))), |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2078 | NextMetadataNo++); |
Duncan P. N. Exon Smith | c7363f1 | 2015-02-13 01:10:38 +0000 | [diff] [blame] | 2079 | break; |
| 2080 | } |
Duncan P. N. Exon Smith | 8775476 | 2015-02-13 01:14:11 +0000 | [diff] [blame] | 2081 | case bitc::METADATA_ENUMERATOR: { |
| 2082 | if (Record.size() != 3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2083 | return error("Invalid record"); |
Duncan P. N. Exon Smith | 8775476 | 2015-02-13 01:14:11 +0000 | [diff] [blame] | 2084 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2085 | MetadataList.assignValue( |
| 2086 | GET_OR_DISTINCT( |
| 2087 | DIEnumerator, Record[0], |
| 2088 | (Context, unrotateSign(Record[1]), getMDString(Record[2]))), |
| 2089 | NextMetadataNo++); |
Duncan P. N. Exon Smith | 8775476 | 2015-02-13 01:14:11 +0000 | [diff] [blame] | 2090 | break; |
| 2091 | } |
Duncan P. N. Exon Smith | 09e03f3 | 2015-02-13 01:14:58 +0000 | [diff] [blame] | 2092 | case bitc::METADATA_BASIC_TYPE: { |
| 2093 | if (Record.size() != 6) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2094 | return error("Invalid record"); |
Duncan P. N. Exon Smith | 09e03f3 | 2015-02-13 01:14:58 +0000 | [diff] [blame] | 2095 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2096 | MetadataList.assignValue( |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 2097 | GET_OR_DISTINCT(DIBasicType, Record[0], |
Duncan P. N. Exon Smith | 09e03f3 | 2015-02-13 01:14:58 +0000 | [diff] [blame] | 2098 | (Context, Record[1], getMDString(Record[2]), |
| 2099 | Record[3], Record[4], Record[5])), |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2100 | NextMetadataNo++); |
Duncan P. N. Exon Smith | 09e03f3 | 2015-02-13 01:14:58 +0000 | [diff] [blame] | 2101 | break; |
| 2102 | } |
Duncan P. N. Exon Smith | 171d077 | 2015-02-13 01:20:38 +0000 | [diff] [blame] | 2103 | case bitc::METADATA_DERIVED_TYPE: { |
| 2104 | if (Record.size() != 12) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2105 | return error("Invalid record"); |
Duncan P. N. Exon Smith | 171d077 | 2015-02-13 01:20:38 +0000 | [diff] [blame] | 2106 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2107 | MetadataList.assignValue( |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 2108 | GET_OR_DISTINCT(DIDerivedType, Record[0], |
Duncan P. N. Exon Smith | 171d077 | 2015-02-13 01:20:38 +0000 | [diff] [blame] | 2109 | (Context, Record[1], getMDString(Record[2]), |
| 2110 | getMDOrNull(Record[3]), Record[4], |
Duncan P. N. Exon Smith | ad6eb127 | 2015-02-20 03:17:58 +0000 | [diff] [blame] | 2111 | getMDOrNull(Record[5]), getMDOrNull(Record[6]), |
| 2112 | Record[7], Record[8], Record[9], Record[10], |
Duncan P. N. Exon Smith | 171d077 | 2015-02-13 01:20:38 +0000 | [diff] [blame] | 2113 | getMDOrNull(Record[11]))), |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2114 | NextMetadataNo++); |
Duncan P. N. Exon Smith | 171d077 | 2015-02-13 01:20:38 +0000 | [diff] [blame] | 2115 | break; |
| 2116 | } |
| 2117 | case bitc::METADATA_COMPOSITE_TYPE: { |
| 2118 | if (Record.size() != 16) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2119 | return error("Invalid record"); |
Duncan P. N. Exon Smith | 171d077 | 2015-02-13 01:20:38 +0000 | [diff] [blame] | 2120 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2121 | MetadataList.assignValue( |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 2122 | GET_OR_DISTINCT(DICompositeType, Record[0], |
Duncan P. N. Exon Smith | 171d077 | 2015-02-13 01:20:38 +0000 | [diff] [blame] | 2123 | (Context, Record[1], getMDString(Record[2]), |
| 2124 | getMDOrNull(Record[3]), Record[4], |
| 2125 | getMDOrNull(Record[5]), getMDOrNull(Record[6]), |
| 2126 | Record[7], Record[8], Record[9], Record[10], |
| 2127 | getMDOrNull(Record[11]), Record[12], |
| 2128 | getMDOrNull(Record[13]), getMDOrNull(Record[14]), |
| 2129 | getMDString(Record[15]))), |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2130 | NextMetadataNo++); |
Duncan P. N. Exon Smith | 171d077 | 2015-02-13 01:20:38 +0000 | [diff] [blame] | 2131 | break; |
| 2132 | } |
Duncan P. N. Exon Smith | 54e2bc6 | 2015-02-13 01:22:59 +0000 | [diff] [blame] | 2133 | case bitc::METADATA_SUBROUTINE_TYPE: { |
| 2134 | if (Record.size() != 3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2135 | return error("Invalid record"); |
Duncan P. N. Exon Smith | 54e2bc6 | 2015-02-13 01:22:59 +0000 | [diff] [blame] | 2136 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2137 | MetadataList.assignValue( |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 2138 | GET_OR_DISTINCT(DISubroutineType, Record[0], |
Duncan P. N. Exon Smith | 54e2bc6 | 2015-02-13 01:22:59 +0000 | [diff] [blame] | 2139 | (Context, Record[1], getMDOrNull(Record[2]))), |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2140 | NextMetadataNo++); |
Duncan P. N. Exon Smith | 54e2bc6 | 2015-02-13 01:22:59 +0000 | [diff] [blame] | 2141 | break; |
| 2142 | } |
Adrian Prantl | ab1243f | 2015-06-29 23:03:47 +0000 | [diff] [blame] | 2143 | |
| 2144 | case bitc::METADATA_MODULE: { |
| 2145 | if (Record.size() != 6) |
| 2146 | return error("Invalid record"); |
| 2147 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2148 | MetadataList.assignValue( |
Adrian Prantl | ab1243f | 2015-06-29 23:03:47 +0000 | [diff] [blame] | 2149 | GET_OR_DISTINCT(DIModule, Record[0], |
| 2150 | (Context, getMDOrNull(Record[1]), |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2151 | getMDString(Record[2]), getMDString(Record[3]), |
| 2152 | getMDString(Record[4]), getMDString(Record[5]))), |
| 2153 | NextMetadataNo++); |
Adrian Prantl | ab1243f | 2015-06-29 23:03:47 +0000 | [diff] [blame] | 2154 | break; |
| 2155 | } |
| 2156 | |
Duncan P. N. Exon Smith | f14b9c7 | 2015-02-13 01:19:14 +0000 | [diff] [blame] | 2157 | case bitc::METADATA_FILE: { |
| 2158 | if (Record.size() != 3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2159 | return error("Invalid record"); |
Duncan P. N. Exon Smith | f14b9c7 | 2015-02-13 01:19:14 +0000 | [diff] [blame] | 2160 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2161 | MetadataList.assignValue( |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 2162 | GET_OR_DISTINCT(DIFile, Record[0], (Context, getMDString(Record[1]), |
Duncan P. N. Exon Smith | f14b9c7 | 2015-02-13 01:19:14 +0000 | [diff] [blame] | 2163 | getMDString(Record[2]))), |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2164 | NextMetadataNo++); |
Duncan P. N. Exon Smith | f14b9c7 | 2015-02-13 01:19:14 +0000 | [diff] [blame] | 2165 | break; |
| 2166 | } |
Duncan P. N. Exon Smith | c1f1acc | 2015-02-13 01:25:10 +0000 | [diff] [blame] | 2167 | case bitc::METADATA_COMPILE_UNIT: { |
Amjad Aboud | a9bcf16 | 2015-12-10 12:56:35 +0000 | [diff] [blame] | 2168 | if (Record.size() < 14 || Record.size() > 16) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2169 | return error("Invalid record"); |
Duncan P. N. Exon Smith | c1f1acc | 2015-02-13 01:25:10 +0000 | [diff] [blame] | 2170 | |
Amjad Aboud | a9bcf16 | 2015-12-10 12:56:35 +0000 | [diff] [blame] | 2171 | // Ignore Record[0], which indicates whether this compile unit is |
Duncan P. N. Exon Smith | 55ca964 | 2015-08-03 17:26:41 +0000 | [diff] [blame] | 2172 | // distinct. It's always distinct. |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2173 | MetadataList.assignValue( |
Duncan P. N. Exon Smith | 55ca964 | 2015-08-03 17:26:41 +0000 | [diff] [blame] | 2174 | DICompileUnit::getDistinct( |
| 2175 | Context, Record[1], getMDOrNull(Record[2]), |
| 2176 | getMDString(Record[3]), Record[4], getMDString(Record[5]), |
| 2177 | Record[6], getMDString(Record[7]), Record[8], |
| 2178 | getMDOrNull(Record[9]), getMDOrNull(Record[10]), |
| 2179 | getMDOrNull(Record[11]), getMDOrNull(Record[12]), |
Amjad Aboud | a9bcf16 | 2015-12-10 12:56:35 +0000 | [diff] [blame] | 2180 | getMDOrNull(Record[13]), |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 2181 | Record.size() <= 15 ? nullptr : getMDOrNull(Record[15]), |
Amjad Aboud | a9bcf16 | 2015-12-10 12:56:35 +0000 | [diff] [blame] | 2182 | Record.size() <= 14 ? 0 : Record[14]), |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2183 | NextMetadataNo++); |
Duncan P. N. Exon Smith | c1f1acc | 2015-02-13 01:25:10 +0000 | [diff] [blame] | 2184 | break; |
| 2185 | } |
Duncan P. N. Exon Smith | 19fc5ed | 2015-02-13 01:26:47 +0000 | [diff] [blame] | 2186 | case bitc::METADATA_SUBPROGRAM: { |
Peter Collingbourne | d4bff30 | 2015-11-05 22:03:56 +0000 | [diff] [blame] | 2187 | if (Record.size() != 18 && Record.size() != 19) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2188 | return error("Invalid record"); |
Duncan P. N. Exon Smith | 19fc5ed | 2015-02-13 01:26:47 +0000 | [diff] [blame] | 2189 | |
Peter Collingbourne | d4bff30 | 2015-11-05 22:03:56 +0000 | [diff] [blame] | 2190 | bool HasFn = Record.size() == 19; |
| 2191 | DISubprogram *SP = GET_OR_DISTINCT( |
| 2192 | DISubprogram, |
| 2193 | Record[0] || Record[8], // All definitions should be distinct. |
| 2194 | (Context, getMDOrNull(Record[1]), getMDString(Record[2]), |
| 2195 | getMDString(Record[3]), getMDOrNull(Record[4]), Record[5], |
| 2196 | getMDOrNull(Record[6]), Record[7], Record[8], Record[9], |
| 2197 | getMDOrNull(Record[10]), Record[11], Record[12], Record[13], |
| 2198 | Record[14], getMDOrNull(Record[15 + HasFn]), |
| 2199 | getMDOrNull(Record[16 + HasFn]), getMDOrNull(Record[17 + HasFn]))); |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2200 | MetadataList.assignValue(SP, NextMetadataNo++); |
Peter Collingbourne | d4bff30 | 2015-11-05 22:03:56 +0000 | [diff] [blame] | 2201 | |
| 2202 | // Upgrade sp->function mapping to function->sp mapping. |
| 2203 | if (HasFn && Record[15]) { |
| 2204 | if (auto *CMD = dyn_cast<ConstantAsMetadata>(getMDOrNull(Record[15]))) |
| 2205 | if (auto *F = dyn_cast<Function>(CMD->getValue())) { |
| 2206 | if (F->isMaterializable()) |
| 2207 | // Defer until materialized; unmaterialized functions may not have |
| 2208 | // metadata. |
| 2209 | FunctionsWithSPs[F] = SP; |
| 2210 | else if (!F->empty()) |
| 2211 | F->setSubprogram(SP); |
| 2212 | } |
| 2213 | } |
Duncan P. N. Exon Smith | 19fc5ed | 2015-02-13 01:26:47 +0000 | [diff] [blame] | 2214 | break; |
| 2215 | } |
Duncan P. N. Exon Smith | a96d409 | 2015-02-13 01:29:28 +0000 | [diff] [blame] | 2216 | case bitc::METADATA_LEXICAL_BLOCK: { |
| 2217 | if (Record.size() != 5) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2218 | return error("Invalid record"); |
Duncan P. N. Exon Smith | a96d409 | 2015-02-13 01:29:28 +0000 | [diff] [blame] | 2219 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2220 | MetadataList.assignValue( |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 2221 | GET_OR_DISTINCT(DILexicalBlock, Record[0], |
Duncan P. N. Exon Smith | a96d409 | 2015-02-13 01:29:28 +0000 | [diff] [blame] | 2222 | (Context, getMDOrNull(Record[1]), |
| 2223 | getMDOrNull(Record[2]), Record[3], Record[4])), |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2224 | NextMetadataNo++); |
Duncan P. N. Exon Smith | a96d409 | 2015-02-13 01:29:28 +0000 | [diff] [blame] | 2225 | break; |
| 2226 | } |
Duncan P. N. Exon Smith | 06a0702 | 2015-02-13 01:30:42 +0000 | [diff] [blame] | 2227 | case bitc::METADATA_LEXICAL_BLOCK_FILE: { |
| 2228 | if (Record.size() != 4) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2229 | return error("Invalid record"); |
Duncan P. N. Exon Smith | 06a0702 | 2015-02-13 01:30:42 +0000 | [diff] [blame] | 2230 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2231 | MetadataList.assignValue( |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 2232 | GET_OR_DISTINCT(DILexicalBlockFile, Record[0], |
Duncan P. N. Exon Smith | 06a0702 | 2015-02-13 01:30:42 +0000 | [diff] [blame] | 2233 | (Context, getMDOrNull(Record[1]), |
| 2234 | getMDOrNull(Record[2]), Record[3])), |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2235 | NextMetadataNo++); |
Duncan P. N. Exon Smith | 06a0702 | 2015-02-13 01:30:42 +0000 | [diff] [blame] | 2236 | break; |
| 2237 | } |
Duncan P. N. Exon Smith | e146000 | 2015-02-13 01:32:09 +0000 | [diff] [blame] | 2238 | case bitc::METADATA_NAMESPACE: { |
| 2239 | if (Record.size() != 5) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2240 | return error("Invalid record"); |
Duncan P. N. Exon Smith | e146000 | 2015-02-13 01:32:09 +0000 | [diff] [blame] | 2241 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2242 | MetadataList.assignValue( |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 2243 | GET_OR_DISTINCT(DINamespace, Record[0], |
Duncan P. N. Exon Smith | e146000 | 2015-02-13 01:32:09 +0000 | [diff] [blame] | 2244 | (Context, getMDOrNull(Record[1]), |
| 2245 | getMDOrNull(Record[2]), getMDString(Record[3]), |
| 2246 | Record[4])), |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2247 | NextMetadataNo++); |
Duncan P. N. Exon Smith | e146000 | 2015-02-13 01:32:09 +0000 | [diff] [blame] | 2248 | break; |
| 2249 | } |
Amjad Aboud | a9bcf16 | 2015-12-10 12:56:35 +0000 | [diff] [blame] | 2250 | case bitc::METADATA_MACRO: { |
| 2251 | if (Record.size() != 5) |
| 2252 | return error("Invalid record"); |
| 2253 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2254 | MetadataList.assignValue( |
Amjad Aboud | a9bcf16 | 2015-12-10 12:56:35 +0000 | [diff] [blame] | 2255 | GET_OR_DISTINCT(DIMacro, Record[0], |
| 2256 | (Context, Record[1], Record[2], |
| 2257 | getMDString(Record[3]), getMDString(Record[4]))), |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2258 | NextMetadataNo++); |
Amjad Aboud | a9bcf16 | 2015-12-10 12:56:35 +0000 | [diff] [blame] | 2259 | break; |
| 2260 | } |
| 2261 | case bitc::METADATA_MACRO_FILE: { |
| 2262 | if (Record.size() != 5) |
| 2263 | return error("Invalid record"); |
| 2264 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2265 | MetadataList.assignValue( |
Amjad Aboud | a9bcf16 | 2015-12-10 12:56:35 +0000 | [diff] [blame] | 2266 | GET_OR_DISTINCT(DIMacroFile, Record[0], |
| 2267 | (Context, Record[1], Record[2], |
| 2268 | getMDOrNull(Record[3]), getMDOrNull(Record[4]))), |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2269 | NextMetadataNo++); |
Amjad Aboud | a9bcf16 | 2015-12-10 12:56:35 +0000 | [diff] [blame] | 2270 | break; |
| 2271 | } |
Duncan P. N. Exon Smith | 2847f38 | 2015-02-13 01:34:32 +0000 | [diff] [blame] | 2272 | case bitc::METADATA_TEMPLATE_TYPE: { |
Duncan P. N. Exon Smith | 3d62bba | 2015-02-19 00:37:21 +0000 | [diff] [blame] | 2273 | if (Record.size() != 3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2274 | return error("Invalid record"); |
Duncan P. N. Exon Smith | 2847f38 | 2015-02-13 01:34:32 +0000 | [diff] [blame] | 2275 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2276 | MetadataList.assignValue(GET_OR_DISTINCT(DITemplateTypeParameter, |
| 2277 | Record[0], |
| 2278 | (Context, getMDString(Record[1]), |
| 2279 | getMDOrNull(Record[2]))), |
| 2280 | NextMetadataNo++); |
Duncan P. N. Exon Smith | 2847f38 | 2015-02-13 01:34:32 +0000 | [diff] [blame] | 2281 | break; |
| 2282 | } |
| 2283 | case bitc::METADATA_TEMPLATE_VALUE: { |
Duncan P. N. Exon Smith | 3d62bba | 2015-02-19 00:37:21 +0000 | [diff] [blame] | 2284 | if (Record.size() != 5) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2285 | return error("Invalid record"); |
Duncan P. N. Exon Smith | 2847f38 | 2015-02-13 01:34:32 +0000 | [diff] [blame] | 2286 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2287 | MetadataList.assignValue( |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 2288 | GET_OR_DISTINCT(DITemplateValueParameter, Record[0], |
Duncan P. N. Exon Smith | 3d62bba | 2015-02-19 00:37:21 +0000 | [diff] [blame] | 2289 | (Context, Record[1], getMDString(Record[2]), |
| 2290 | getMDOrNull(Record[3]), getMDOrNull(Record[4]))), |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2291 | NextMetadataNo++); |
Duncan P. N. Exon Smith | 2847f38 | 2015-02-13 01:34:32 +0000 | [diff] [blame] | 2292 | break; |
| 2293 | } |
Duncan P. N. Exon Smith | c8f810a | 2015-02-13 01:35:40 +0000 | [diff] [blame] | 2294 | case bitc::METADATA_GLOBAL_VAR: { |
| 2295 | if (Record.size() != 11) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2296 | return error("Invalid record"); |
Duncan P. N. Exon Smith | c8f810a | 2015-02-13 01:35:40 +0000 | [diff] [blame] | 2297 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2298 | MetadataList.assignValue( |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 2299 | GET_OR_DISTINCT(DIGlobalVariable, Record[0], |
Duncan P. N. Exon Smith | c8f810a | 2015-02-13 01:35:40 +0000 | [diff] [blame] | 2300 | (Context, getMDOrNull(Record[1]), |
| 2301 | getMDString(Record[2]), getMDString(Record[3]), |
| 2302 | getMDOrNull(Record[4]), Record[5], |
| 2303 | getMDOrNull(Record[6]), Record[7], Record[8], |
| 2304 | getMDOrNull(Record[9]), getMDOrNull(Record[10]))), |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2305 | NextMetadataNo++); |
Duncan P. N. Exon Smith | c8f810a | 2015-02-13 01:35:40 +0000 | [diff] [blame] | 2306 | break; |
| 2307 | } |
Duncan P. N. Exon Smith | 72fe2d0 | 2015-02-13 01:39:44 +0000 | [diff] [blame] | 2308 | case bitc::METADATA_LOCAL_VAR: { |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 2309 | // 10th field is for the obseleted 'inlinedAt:' field. |
Duncan P. N. Exon Smith | ed013cd | 2015-07-31 18:58:39 +0000 | [diff] [blame] | 2310 | if (Record.size() < 8 || Record.size() > 10) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2311 | return error("Invalid record"); |
Duncan P. N. Exon Smith | 72fe2d0 | 2015-02-13 01:39:44 +0000 | [diff] [blame] | 2312 | |
Duncan P. N. Exon Smith | ed013cd | 2015-07-31 18:58:39 +0000 | [diff] [blame] | 2313 | // 2nd field used to be an artificial tag, either DW_TAG_auto_variable or |
| 2314 | // DW_TAG_arg_variable. |
| 2315 | bool HasTag = Record.size() > 8; |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2316 | MetadataList.assignValue( |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 2317 | GET_OR_DISTINCT(DILocalVariable, Record[0], |
Duncan P. N. Exon Smith | ed013cd | 2015-07-31 18:58:39 +0000 | [diff] [blame] | 2318 | (Context, getMDOrNull(Record[1 + HasTag]), |
| 2319 | getMDString(Record[2 + HasTag]), |
| 2320 | getMDOrNull(Record[3 + HasTag]), Record[4 + HasTag], |
| 2321 | getMDOrNull(Record[5 + HasTag]), Record[6 + HasTag], |
| 2322 | Record[7 + HasTag])), |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2323 | NextMetadataNo++); |
Duncan P. N. Exon Smith | 72fe2d0 | 2015-02-13 01:39:44 +0000 | [diff] [blame] | 2324 | break; |
| 2325 | } |
Duncan P. N. Exon Smith | 0c5c012 | 2015-02-13 01:42:09 +0000 | [diff] [blame] | 2326 | case bitc::METADATA_EXPRESSION: { |
| 2327 | if (Record.size() < 1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2328 | return error("Invalid record"); |
Duncan P. N. Exon Smith | 0c5c012 | 2015-02-13 01:42:09 +0000 | [diff] [blame] | 2329 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2330 | MetadataList.assignValue( |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 2331 | GET_OR_DISTINCT(DIExpression, Record[0], |
Duncan P. N. Exon Smith | 0c5c012 | 2015-02-13 01:42:09 +0000 | [diff] [blame] | 2332 | (Context, makeArrayRef(Record).slice(1))), |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2333 | NextMetadataNo++); |
Duncan P. N. Exon Smith | 0c5c012 | 2015-02-13 01:42:09 +0000 | [diff] [blame] | 2334 | break; |
| 2335 | } |
Duncan P. N. Exon Smith | d45ce96 | 2015-02-13 01:43:22 +0000 | [diff] [blame] | 2336 | case bitc::METADATA_OBJC_PROPERTY: { |
| 2337 | if (Record.size() != 8) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2338 | return error("Invalid record"); |
Duncan P. N. Exon Smith | d45ce96 | 2015-02-13 01:43:22 +0000 | [diff] [blame] | 2339 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2340 | MetadataList.assignValue( |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 2341 | GET_OR_DISTINCT(DIObjCProperty, Record[0], |
Duncan P. N. Exon Smith | d45ce96 | 2015-02-13 01:43:22 +0000 | [diff] [blame] | 2342 | (Context, getMDString(Record[1]), |
| 2343 | getMDOrNull(Record[2]), Record[3], |
| 2344 | getMDString(Record[4]), getMDString(Record[5]), |
| 2345 | Record[6], getMDOrNull(Record[7]))), |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2346 | NextMetadataNo++); |
Duncan P. N. Exon Smith | d45ce96 | 2015-02-13 01:43:22 +0000 | [diff] [blame] | 2347 | break; |
| 2348 | } |
Duncan P. N. Exon Smith | 1c93116 | 2015-02-13 01:46:02 +0000 | [diff] [blame] | 2349 | case bitc::METADATA_IMPORTED_ENTITY: { |
| 2350 | if (Record.size() != 6) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2351 | return error("Invalid record"); |
Duncan P. N. Exon Smith | 1c93116 | 2015-02-13 01:46:02 +0000 | [diff] [blame] | 2352 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2353 | MetadataList.assignValue( |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 2354 | GET_OR_DISTINCT(DIImportedEntity, Record[0], |
Duncan P. N. Exon Smith | 1c93116 | 2015-02-13 01:46:02 +0000 | [diff] [blame] | 2355 | (Context, Record[1], getMDOrNull(Record[2]), |
| 2356 | getMDOrNull(Record[3]), Record[4], |
| 2357 | getMDString(Record[5]))), |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2358 | NextMetadataNo++); |
Duncan P. N. Exon Smith | 1c93116 | 2015-02-13 01:46:02 +0000 | [diff] [blame] | 2359 | break; |
| 2360 | } |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 2361 | case bitc::METADATA_STRING: { |
Eli Bendersky | 5d5e18d | 2014-06-25 15:41:00 +0000 | [diff] [blame] | 2362 | std::string String(Record.begin(), Record.end()); |
| 2363 | llvm::UpgradeMDStringConstant(String); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 2364 | Metadata *MD = MDString::get(Context, String); |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 2365 | MetadataList.assignValue(MD, NextMetadataNo++); |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 2366 | break; |
| 2367 | } |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2368 | case bitc::METADATA_KIND: { |
Teresa Johnson | 1254507 | 2015-11-15 02:00:09 +0000 | [diff] [blame] | 2369 | // Support older bitcode files that had METADATA_KIND records in a |
| 2370 | // block with METADATA_BLOCK_ID. |
| 2371 | if (std::error_code EC = parseMetadataKindRecord(Record)) |
| 2372 | return EC; |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 2373 | break; |
| 2374 | } |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 2375 | } |
| 2376 | } |
Duncan P. N. Exon Smith | 4e4aa70 | 2015-02-03 21:54:14 +0000 | [diff] [blame] | 2377 | #undef GET_OR_DISTINCT |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 2378 | } |
| 2379 | |
Teresa Johnson | 1254507 | 2015-11-15 02:00:09 +0000 | [diff] [blame] | 2380 | /// Parse the metadata kinds out of the METADATA_KIND_BLOCK. |
| 2381 | std::error_code BitcodeReader::parseMetadataKinds() { |
| 2382 | if (Stream.EnterSubBlock(bitc::METADATA_KIND_BLOCK_ID)) |
| 2383 | return error("Invalid record"); |
| 2384 | |
| 2385 | SmallVector<uint64_t, 64> Record; |
| 2386 | |
| 2387 | // Read all the records. |
| 2388 | while (1) { |
| 2389 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| 2390 | |
| 2391 | switch (Entry.Kind) { |
| 2392 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 2393 | case BitstreamEntry::Error: |
| 2394 | return error("Malformed block"); |
| 2395 | case BitstreamEntry::EndBlock: |
| 2396 | return std::error_code(); |
| 2397 | case BitstreamEntry::Record: |
| 2398 | // The interesting case. |
| 2399 | break; |
| 2400 | } |
| 2401 | |
| 2402 | // Read a record. |
| 2403 | Record.clear(); |
| 2404 | unsigned Code = Stream.readRecord(Entry.ID, Record); |
| 2405 | switch (Code) { |
| 2406 | default: // Default behavior: ignore. |
| 2407 | break; |
| 2408 | case bitc::METADATA_KIND: { |
| 2409 | if (std::error_code EC = parseMetadataKindRecord(Record)) |
| 2410 | return EC; |
| 2411 | break; |
| 2412 | } |
| 2413 | } |
| 2414 | } |
| 2415 | } |
| 2416 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2417 | /// Decode a signed value stored with the sign bit in the LSB for dense VBR |
| 2418 | /// encoding. |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 2419 | uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) { |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 2420 | if ((V & 1) == 0) |
| 2421 | return V >> 1; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2422 | if (V != 1) |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 2423 | return -(V >> 1); |
| 2424 | // There is no such thing as -0 with integers. "-0" really means MININT. |
| 2425 | return 1ULL << 63; |
| 2426 | } |
| 2427 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2428 | /// Resolve all of the initializers for global values and aliases that we can. |
| 2429 | std::error_code BitcodeReader::resolveGlobalAndAliasInits() { |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 2430 | std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist; |
| 2431 | std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist; |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 2432 | std::vector<std::pair<Function*, unsigned> > FunctionPrefixWorklist; |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 2433 | std::vector<std::pair<Function*, unsigned> > FunctionPrologueWorklist; |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 2434 | std::vector<std::pair<Function*, unsigned> > FunctionPersonalityFnWorklist; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2435 | |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 2436 | GlobalInitWorklist.swap(GlobalInits); |
| 2437 | AliasInitWorklist.swap(AliasInits); |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 2438 | FunctionPrefixWorklist.swap(FunctionPrefixes); |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 2439 | FunctionPrologueWorklist.swap(FunctionPrologues); |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 2440 | FunctionPersonalityFnWorklist.swap(FunctionPersonalityFns); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 2441 | |
| 2442 | while (!GlobalInitWorklist.empty()) { |
Chris Lattner | 831d420 | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 2443 | unsigned ValID = GlobalInitWorklist.back().second; |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 2444 | if (ValID >= ValueList.size()) { |
| 2445 | // Not ready to resolve this yet, it requires something later in the file. |
Chris Lattner | 831d420 | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 2446 | GlobalInits.push_back(GlobalInitWorklist.back()); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 2447 | } else { |
Karthik Bhat | 82540e9 | 2014-03-27 12:08:23 +0000 | [diff] [blame] | 2448 | if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 2449 | GlobalInitWorklist.back().first->setInitializer(C); |
| 2450 | else |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2451 | return error("Expected a constant"); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 2452 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2453 | GlobalInitWorklist.pop_back(); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 2454 | } |
| 2455 | |
| 2456 | while (!AliasInitWorklist.empty()) { |
| 2457 | unsigned ValID = AliasInitWorklist.back().second; |
| 2458 | if (ValID >= ValueList.size()) { |
| 2459 | AliasInits.push_back(AliasInitWorklist.back()); |
| 2460 | } else { |
Filipe Cabecinhas | a911af0 | 2015-06-06 20:44:53 +0000 | [diff] [blame] | 2461 | Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]); |
| 2462 | if (!C) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2463 | return error("Expected a constant"); |
Filipe Cabecinhas | a911af0 | 2015-06-06 20:44:53 +0000 | [diff] [blame] | 2464 | GlobalAlias *Alias = AliasInitWorklist.back().first; |
| 2465 | if (C->getType() != Alias->getType()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2466 | return error("Alias and aliasee types don't match"); |
Filipe Cabecinhas | a911af0 | 2015-06-06 20:44:53 +0000 | [diff] [blame] | 2467 | Alias->setAliasee(C); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 2468 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2469 | AliasInitWorklist.pop_back(); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 2470 | } |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 2471 | |
| 2472 | while (!FunctionPrefixWorklist.empty()) { |
| 2473 | unsigned ValID = FunctionPrefixWorklist.back().second; |
| 2474 | if (ValID >= ValueList.size()) { |
| 2475 | FunctionPrefixes.push_back(FunctionPrefixWorklist.back()); |
| 2476 | } else { |
Karthik Bhat | 82540e9 | 2014-03-27 12:08:23 +0000 | [diff] [blame] | 2477 | if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 2478 | FunctionPrefixWorklist.back().first->setPrefixData(C); |
| 2479 | else |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2480 | return error("Expected a constant"); |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 2481 | } |
| 2482 | FunctionPrefixWorklist.pop_back(); |
| 2483 | } |
| 2484 | |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 2485 | while (!FunctionPrologueWorklist.empty()) { |
| 2486 | unsigned ValID = FunctionPrologueWorklist.back().second; |
| 2487 | if (ValID >= ValueList.size()) { |
| 2488 | FunctionPrologues.push_back(FunctionPrologueWorklist.back()); |
| 2489 | } else { |
| 2490 | if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) |
| 2491 | FunctionPrologueWorklist.back().first->setPrologueData(C); |
| 2492 | else |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2493 | return error("Expected a constant"); |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 2494 | } |
| 2495 | FunctionPrologueWorklist.pop_back(); |
| 2496 | } |
| 2497 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 2498 | while (!FunctionPersonalityFnWorklist.empty()) { |
| 2499 | unsigned ValID = FunctionPersonalityFnWorklist.back().second; |
| 2500 | if (ValID >= ValueList.size()) { |
| 2501 | FunctionPersonalityFns.push_back(FunctionPersonalityFnWorklist.back()); |
| 2502 | } else { |
| 2503 | if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) |
| 2504 | FunctionPersonalityFnWorklist.back().first->setPersonalityFn(C); |
| 2505 | else |
| 2506 | return error("Expected a constant"); |
| 2507 | } |
| 2508 | FunctionPersonalityFnWorklist.pop_back(); |
| 2509 | } |
| 2510 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2511 | return std::error_code(); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 2512 | } |
| 2513 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2514 | static APInt readWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) { |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 2515 | SmallVector<uint64_t, 8> Words(Vals.size()); |
| 2516 | std::transform(Vals.begin(), Vals.end(), Words.begin(), |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 2517 | BitcodeReader::decodeSignRotatedValue); |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 2518 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 2519 | return APInt(TypeBits, Words); |
| 2520 | } |
| 2521 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2522 | std::error_code BitcodeReader::parseConstants() { |
Chris Lattner | 982ec1e | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 2523 | if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2524 | return error("Invalid record"); |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2525 | |
| 2526 | SmallVector<uint64_t, 64> Record; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2527 | |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2528 | // Read all the records for this value table. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2529 | Type *CurTy = Type::getInt32Ty(Context); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 2530 | unsigned NextCstNo = ValueList.size(); |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2531 | while (1) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2532 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2533 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2534 | switch (Entry.Kind) { |
| 2535 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 2536 | case BitstreamEntry::Error: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2537 | return error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2538 | case BitstreamEntry::EndBlock: |
| 2539 | if (NextCstNo != ValueList.size()) |
George Burgess IV | 1030d68 | 2016-01-20 22:15:23 +0000 | [diff] [blame] | 2540 | return error("Invalid constant reference"); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2541 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2542 | // Once all the constants have been read, go through and resolve forward |
| 2543 | // references. |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2544 | ValueList.resolveConstantForwardRefs(); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2545 | return std::error_code(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2546 | case BitstreamEntry::Record: |
| 2547 | // The interesting case. |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 2548 | break; |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2549 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2550 | |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2551 | // Read a record. |
| 2552 | Record.clear(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2553 | Value *V = nullptr; |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2554 | unsigned BitCode = Stream.readRecord(Entry.ID, Record); |
Dan Gohman | 0ebd696 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 2555 | switch (BitCode) { |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2556 | default: // Default behavior: unknown constant |
| 2557 | case bitc::CST_CODE_UNDEF: // UNDEF |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2558 | V = UndefValue::get(CurTy); |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2559 | break; |
| 2560 | case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid] |
| 2561 | if (Record.empty()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2562 | return error("Invalid record"); |
Karthik Bhat | 82540e9 | 2014-03-27 12:08:23 +0000 | [diff] [blame] | 2563 | if (Record[0] >= TypeList.size() || !TypeList[Record[0]]) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2564 | return error("Invalid record"); |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2565 | CurTy = TypeList[Record[0]]; |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 2566 | continue; // Skip the ValueList manipulation. |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2567 | case bitc::CST_CODE_NULL: // NULL |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 2568 | V = Constant::getNullValue(CurTy); |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2569 | break; |
| 2570 | case bitc::CST_CODE_INTEGER: // INTEGER: [intval] |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2571 | if (!CurTy->isIntegerTy() || Record.empty()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2572 | return error("Invalid record"); |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 2573 | V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0])); |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 2574 | break; |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 2575 | case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval] |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2576 | if (!CurTy->isIntegerTy() || Record.empty()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2577 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2578 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2579 | APInt VInt = |
| 2580 | readWideAPInt(Record, cast<IntegerType>(CurTy)->getBitWidth()); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 2581 | V = ConstantInt::get(Context, VInt); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2582 | |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 2583 | break; |
| 2584 | } |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 2585 | case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval] |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 2586 | if (Record.empty()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2587 | return error("Invalid record"); |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 2588 | if (CurTy->isHalfTy()) |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 2589 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf, |
| 2590 | APInt(16, (uint16_t)Record[0]))); |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 2591 | else if (CurTy->isFloatTy()) |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 2592 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle, |
| 2593 | APInt(32, (uint32_t)Record[0]))); |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 2594 | else if (CurTy->isDoubleTy()) |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 2595 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble, |
| 2596 | APInt(64, Record[0]))); |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 2597 | else if (CurTy->isX86_FP80Ty()) { |
Dale Johannesen | 93eefa0 | 2009-03-23 21:16:53 +0000 | [diff] [blame] | 2598 | // Bits are not stored the same way as a normal i80 APInt, compensate. |
| 2599 | uint64_t Rearrange[2]; |
| 2600 | Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16); |
| 2601 | Rearrange[1] = Record[0] >> 48; |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 2602 | V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended, |
| 2603 | APInt(80, Rearrange))); |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 2604 | } else if (CurTy->isFP128Ty()) |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 2605 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad, |
| 2606 | APInt(128, Record))); |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 2607 | else if (CurTy->isPPC_FP128Ty()) |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 2608 | V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble, |
| 2609 | APInt(128, Record))); |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2610 | else |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2611 | V = UndefValue::get(CurTy); |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2612 | break; |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 2613 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2614 | |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 2615 | case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number] |
| 2616 | if (Record.empty()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2617 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2618 | |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 2619 | unsigned Size = Record.size(); |
Chris Lattner | cc3aaf1 | 2012-01-27 03:15:49 +0000 | [diff] [blame] | 2620 | SmallVector<Constant*, 16> Elts; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2621 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2622 | if (StructType *STy = dyn_cast<StructType>(CurTy)) { |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 2623 | for (unsigned i = 0; i != Size; ++i) |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 2624 | Elts.push_back(ValueList.getConstantFwdRef(Record[i], |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 2625 | STy->getElementType(i))); |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2626 | V = ConstantStruct::get(STy, Elts); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2627 | } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) { |
| 2628 | Type *EltTy = ATy->getElementType(); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 2629 | for (unsigned i = 0; i != Size; ++i) |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 2630 | Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 2631 | V = ConstantArray::get(ATy, Elts); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2632 | } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) { |
| 2633 | Type *EltTy = VTy->getElementType(); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 2634 | for (unsigned i = 0; i != Size; ++i) |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 2635 | Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 2636 | V = ConstantVector::get(Elts); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 2637 | } else { |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2638 | V = UndefValue::get(CurTy); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 2639 | } |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2640 | break; |
| 2641 | } |
Chris Lattner | bb8278a | 2012-02-05 02:41:35 +0000 | [diff] [blame] | 2642 | case bitc::CST_CODE_STRING: // STRING: [values] |
Chris Lattner | f25f710 | 2007-05-06 00:53:07 +0000 | [diff] [blame] | 2643 | case bitc::CST_CODE_CSTRING: { // CSTRING: [values] |
| 2644 | if (Record.empty()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2645 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2646 | |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 2647 | SmallString<16> Elts(Record.begin(), Record.end()); |
Chris Lattner | bb8278a | 2012-02-05 02:41:35 +0000 | [diff] [blame] | 2648 | V = ConstantDataArray::getString(Context, Elts, |
| 2649 | BitCode == bitc::CST_CODE_CSTRING); |
Chris Lattner | f25f710 | 2007-05-06 00:53:07 +0000 | [diff] [blame] | 2650 | break; |
| 2651 | } |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2652 | case bitc::CST_CODE_DATA: {// DATA: [n x value] |
| 2653 | if (Record.empty()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2654 | return error("Invalid record"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2655 | |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2656 | Type *EltTy = cast<SequentialType>(CurTy)->getElementType(); |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2657 | if (EltTy->isIntegerTy(8)) { |
| 2658 | SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end()); |
| 2659 | if (isa<VectorType>(CurTy)) |
| 2660 | V = ConstantDataVector::get(Context, Elts); |
| 2661 | else |
| 2662 | V = ConstantDataArray::get(Context, Elts); |
| 2663 | } else if (EltTy->isIntegerTy(16)) { |
| 2664 | SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); |
| 2665 | if (isa<VectorType>(CurTy)) |
| 2666 | V = ConstantDataVector::get(Context, Elts); |
| 2667 | else |
| 2668 | V = ConstantDataArray::get(Context, Elts); |
| 2669 | } else if (EltTy->isIntegerTy(32)) { |
| 2670 | SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); |
| 2671 | if (isa<VectorType>(CurTy)) |
| 2672 | V = ConstantDataVector::get(Context, Elts); |
| 2673 | else |
| 2674 | V = ConstantDataArray::get(Context, Elts); |
| 2675 | } else if (EltTy->isIntegerTy(64)) { |
| 2676 | SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); |
| 2677 | if (isa<VectorType>(CurTy)) |
| 2678 | V = ConstantDataVector::get(Context, Elts); |
| 2679 | else |
| 2680 | V = ConstantDataArray::get(Context, Elts); |
Justin Bogner | a43eacb | 2016-01-06 22:31:32 +0000 | [diff] [blame] | 2681 | } else if (EltTy->isHalfTy()) { |
| 2682 | SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); |
| 2683 | if (isa<VectorType>(CurTy)) |
| 2684 | V = ConstantDataVector::getFP(Context, Elts); |
| 2685 | else |
| 2686 | V = ConstantDataArray::getFP(Context, Elts); |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2687 | } else if (EltTy->isFloatTy()) { |
Justin Bogner | a43eacb | 2016-01-06 22:31:32 +0000 | [diff] [blame] | 2688 | SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2689 | if (isa<VectorType>(CurTy)) |
Justin Bogner | a43eacb | 2016-01-06 22:31:32 +0000 | [diff] [blame] | 2690 | V = ConstantDataVector::getFP(Context, Elts); |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2691 | else |
Justin Bogner | a43eacb | 2016-01-06 22:31:32 +0000 | [diff] [blame] | 2692 | V = ConstantDataArray::getFP(Context, Elts); |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2693 | } else if (EltTy->isDoubleTy()) { |
Justin Bogner | a43eacb | 2016-01-06 22:31:32 +0000 | [diff] [blame] | 2694 | SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2695 | if (isa<VectorType>(CurTy)) |
Justin Bogner | a43eacb | 2016-01-06 22:31:32 +0000 | [diff] [blame] | 2696 | V = ConstantDataVector::getFP(Context, Elts); |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2697 | else |
Justin Bogner | a43eacb | 2016-01-06 22:31:32 +0000 | [diff] [blame] | 2698 | V = ConstantDataArray::getFP(Context, Elts); |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2699 | } else { |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2700 | return error("Invalid type for value"); |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2701 | } |
| 2702 | break; |
| 2703 | } |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2704 | case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval] |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2705 | if (Record.size() < 3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2706 | return error("Invalid record"); |
| 2707 | int Opc = getDecodedBinaryOpcode(Record[0], CurTy); |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 2708 | if (Opc < 0) { |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2709 | V = UndefValue::get(CurTy); // Unknown binop. |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 2710 | } else { |
| 2711 | Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy); |
| 2712 | Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy); |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2713 | unsigned Flags = 0; |
| 2714 | if (Record.size() >= 4) { |
| 2715 | if (Opc == Instruction::Add || |
| 2716 | Opc == Instruction::Sub || |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2717 | Opc == Instruction::Mul || |
| 2718 | Opc == Instruction::Shl) { |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2719 | if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP)) |
| 2720 | Flags |= OverflowingBinaryOperator::NoSignedWrap; |
| 2721 | if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) |
| 2722 | Flags |= OverflowingBinaryOperator::NoUnsignedWrap; |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 2723 | } else if (Opc == Instruction::SDiv || |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2724 | Opc == Instruction::UDiv || |
| 2725 | Opc == Instruction::LShr || |
| 2726 | Opc == Instruction::AShr) { |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 2727 | if (Record[3] & (1 << bitc::PEO_EXACT)) |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2728 | Flags |= SDivOperator::IsExact; |
| 2729 | } |
| 2730 | } |
| 2731 | V = ConstantExpr::get(Opc, LHS, RHS, Flags); |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 2732 | } |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2733 | break; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2734 | } |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2735 | case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval] |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2736 | if (Record.size() < 3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2737 | return error("Invalid record"); |
| 2738 | int Opc = getDecodedCastOpcode(Record[0]); |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 2739 | if (Opc < 0) { |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2740 | V = UndefValue::get(CurTy); // Unknown cast. |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 2741 | } else { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2742 | Type *OpTy = getTypeByID(Record[1]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2743 | if (!OpTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2744 | return error("Invalid record"); |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 2745 | Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy); |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2746 | V = UpgradeBitCastExpr(Opc, Op, CurTy); |
| 2747 | if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy); |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 2748 | } |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2749 | break; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2750 | } |
Dan Gohman | 1639c39 | 2009-07-27 21:53:46 +0000 | [diff] [blame] | 2751 | case bitc::CST_CODE_CE_INBOUNDS_GEP: |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2752 | case bitc::CST_CODE_CE_GEP: { // CE_GEP: [n x operands] |
David Blaikie | b926357 | 2015-03-13 21:03:36 +0000 | [diff] [blame] | 2753 | unsigned OpNum = 0; |
| 2754 | Type *PointeeType = nullptr; |
| 2755 | if (Record.size() % 2) |
| 2756 | PointeeType = getTypeByID(Record[OpNum++]); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2757 | SmallVector<Constant*, 16> Elts; |
David Blaikie | b926357 | 2015-03-13 21:03:36 +0000 | [diff] [blame] | 2758 | while (OpNum != Record.size()) { |
| 2759 | Type *ElTy = getTypeByID(Record[OpNum++]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2760 | if (!ElTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2761 | return error("Invalid record"); |
David Blaikie | b926357 | 2015-03-13 21:03:36 +0000 | [diff] [blame] | 2762 | Elts.push_back(ValueList.getConstantFwdRef(Record[OpNum++], ElTy)); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2763 | } |
David Blaikie | b926357 | 2015-03-13 21:03:36 +0000 | [diff] [blame] | 2764 | |
David Blaikie | b926357 | 2015-03-13 21:03:36 +0000 | [diff] [blame] | 2765 | if (PointeeType && |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 2766 | PointeeType != |
| 2767 | cast<SequentialType>(Elts[0]->getType()->getScalarType()) |
| 2768 | ->getElementType()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2769 | return error("Explicit gep operator type does not match pointee type " |
David Blaikie | 12cf5d70 | 2015-03-16 22:03:50 +0000 | [diff] [blame] | 2770 | "of pointer operand"); |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 2771 | |
| 2772 | ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end()); |
| 2773 | V = ConstantExpr::getGetElementPtr(PointeeType, Elts[0], Indices, |
| 2774 | BitCode == |
| 2775 | bitc::CST_CODE_CE_INBOUNDS_GEP); |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 2776 | break; |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2777 | } |
Joe Abbey | 1a6e770 | 2013-09-12 22:02:31 +0000 | [diff] [blame] | 2778 | case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#] |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2779 | if (Record.size() < 3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2780 | return error("Invalid record"); |
Joe Abbey | 1a6e770 | 2013-09-12 22:02:31 +0000 | [diff] [blame] | 2781 | |
| 2782 | Type *SelectorTy = Type::getInt1Ty(Context); |
| 2783 | |
Filipe Cabecinhas | 984fefd | 2015-08-31 18:00:30 +0000 | [diff] [blame] | 2784 | // The selector might be an i1 or an <n x i1> |
| 2785 | // Get the type from the ValueList before getting a forward ref. |
Joe Abbey | 1a6e770 | 2013-09-12 22:02:31 +0000 | [diff] [blame] | 2786 | if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) |
Filipe Cabecinhas | 984fefd | 2015-08-31 18:00:30 +0000 | [diff] [blame] | 2787 | if (Value *V = ValueList[Record[0]]) |
| 2788 | if (SelectorTy != V->getType()) |
| 2789 | SelectorTy = VectorType::get(SelectorTy, VTy->getNumElements()); |
Joe Abbey | 1a6e770 | 2013-09-12 22:02:31 +0000 | [diff] [blame] | 2790 | |
| 2791 | V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0], |
| 2792 | SelectorTy), |
| 2793 | ValueList.getConstantFwdRef(Record[1],CurTy), |
| 2794 | ValueList.getConstantFwdRef(Record[2],CurTy)); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2795 | break; |
Joe Abbey | 1a6e770 | 2013-09-12 22:02:31 +0000 | [diff] [blame] | 2796 | } |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 2797 | case bitc::CST_CODE_CE_EXTRACTELT |
| 2798 | : { // CE_EXTRACTELT: [opty, opval, opty, opval] |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2799 | if (Record.size() < 3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2800 | return error("Invalid record"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2801 | VectorType *OpTy = |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2802 | dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2803 | if (!OpTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2804 | return error("Invalid record"); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2805 | Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 2806 | Constant *Op1 = nullptr; |
| 2807 | if (Record.size() == 4) { |
| 2808 | Type *IdxTy = getTypeByID(Record[2]); |
| 2809 | if (!IdxTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2810 | return error("Invalid record"); |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 2811 | Op1 = ValueList.getConstantFwdRef(Record[3], IdxTy); |
| 2812 | } else // TODO: Remove with llvm 4.0 |
| 2813 | Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context)); |
| 2814 | if (!Op1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2815 | return error("Invalid record"); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2816 | V = ConstantExpr::getExtractElement(Op0, Op1); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2817 | break; |
| 2818 | } |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 2819 | case bitc::CST_CODE_CE_INSERTELT |
| 2820 | : { // CE_INSERTELT: [opval, opval, opty, opval] |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2821 | VectorType *OpTy = dyn_cast<VectorType>(CurTy); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2822 | if (Record.size() < 3 || !OpTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2823 | return error("Invalid record"); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2824 | Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); |
| 2825 | Constant *Op1 = ValueList.getConstantFwdRef(Record[1], |
| 2826 | OpTy->getElementType()); |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 2827 | Constant *Op2 = nullptr; |
| 2828 | if (Record.size() == 4) { |
| 2829 | Type *IdxTy = getTypeByID(Record[2]); |
| 2830 | if (!IdxTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2831 | return error("Invalid record"); |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 2832 | Op2 = ValueList.getConstantFwdRef(Record[3], IdxTy); |
| 2833 | } else // TODO: Remove with llvm 4.0 |
| 2834 | Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context)); |
| 2835 | if (!Op2) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2836 | return error("Invalid record"); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2837 | V = ConstantExpr::getInsertElement(Op0, Op1, Op2); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2838 | break; |
| 2839 | } |
| 2840 | case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval] |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2841 | VectorType *OpTy = dyn_cast<VectorType>(CurTy); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2842 | if (Record.size() < 3 || !OpTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2843 | return error("Invalid record"); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2844 | Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); |
| 2845 | Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2846 | Type *ShufTy = VectorType::get(Type::getInt32Ty(Context), |
Owen Anderson | e9f9804 | 2009-07-07 20:18:58 +0000 | [diff] [blame] | 2847 | OpTy->getNumElements()); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2848 | Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2849 | V = ConstantExpr::getShuffleVector(Op0, Op1, Op2); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2850 | break; |
| 2851 | } |
Nate Begeman | 94aa38d | 2009-02-12 21:28:33 +0000 | [diff] [blame] | 2852 | case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval] |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2853 | VectorType *RTy = dyn_cast<VectorType>(CurTy); |
| 2854 | VectorType *OpTy = |
Duncan Sands | 89d412a | 2010-10-28 15:47:26 +0000 | [diff] [blame] | 2855 | dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2856 | if (Record.size() < 4 || !RTy || !OpTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2857 | return error("Invalid record"); |
Nate Begeman | 94aa38d | 2009-02-12 21:28:33 +0000 | [diff] [blame] | 2858 | Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); |
| 2859 | Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2860 | Type *ShufTy = VectorType::get(Type::getInt32Ty(Context), |
Owen Anderson | e9f9804 | 2009-07-07 20:18:58 +0000 | [diff] [blame] | 2861 | RTy->getNumElements()); |
Nate Begeman | 94aa38d | 2009-02-12 21:28:33 +0000 | [diff] [blame] | 2862 | Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2863 | V = ConstantExpr::getShuffleVector(Op0, Op1, Op2); |
Nate Begeman | 94aa38d | 2009-02-12 21:28:33 +0000 | [diff] [blame] | 2864 | break; |
| 2865 | } |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2866 | case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred] |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2867 | if (Record.size() < 4) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2868 | return error("Invalid record"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2869 | Type *OpTy = getTypeByID(Record[0]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2870 | if (!OpTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2871 | return error("Invalid record"); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2872 | Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); |
| 2873 | Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); |
| 2874 | |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2875 | if (OpTy->isFPOrFPVectorTy()) |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2876 | V = ConstantExpr::getFCmp(Record[3], Op0, Op1); |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 2877 | else |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2878 | V = ConstantExpr::getICmp(Record[3], Op0, Op1); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2879 | break; |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 2880 | } |
Chad Rosier | d8c7610 | 2012-09-05 19:00:49 +0000 | [diff] [blame] | 2881 | // This maintains backward compatibility, pre-asm dialect keywords. |
Chad Rosier | 5895eda | 2012-09-05 06:28:52 +0000 | [diff] [blame] | 2882 | // FIXME: Remove with the 4.0 release. |
Chad Rosier | 18fcdcf | 2012-09-05 00:56:20 +0000 | [diff] [blame] | 2883 | case bitc::CST_CODE_INLINEASM_OLD: { |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2884 | if (Record.size() < 2) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2885 | return error("Invalid record"); |
Chris Lattner | af8fffc | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 2886 | std::string AsmStr, ConstrStr; |
Dale Johannesen | fd04c74 | 2009-10-13 20:46:56 +0000 | [diff] [blame] | 2887 | bool HasSideEffects = Record[0] & 1; |
Dale Johannesen | 1cfb958 | 2009-10-21 23:28:00 +0000 | [diff] [blame] | 2888 | bool IsAlignStack = Record[0] >> 1; |
Chris Lattner | af8fffc | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 2889 | unsigned AsmStrSize = Record[1]; |
| 2890 | if (2+AsmStrSize >= Record.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2891 | return error("Invalid record"); |
Chris Lattner | af8fffc | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 2892 | unsigned ConstStrSize = Record[2+AsmStrSize]; |
| 2893 | if (3+AsmStrSize+ConstStrSize > Record.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2894 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2895 | |
Chris Lattner | af8fffc | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 2896 | for (unsigned i = 0; i != AsmStrSize; ++i) |
| 2897 | AsmStr += (char)Record[2+i]; |
| 2898 | for (unsigned i = 0; i != ConstStrSize; ++i) |
| 2899 | ConstrStr += (char)Record[3+AsmStrSize+i]; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2900 | PointerType *PTy = cast<PointerType>(CurTy); |
Chris Lattner | af8fffc | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 2901 | V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()), |
Dale Johannesen | 1cfb958 | 2009-10-21 23:28:00 +0000 | [diff] [blame] | 2902 | AsmStr, ConstrStr, HasSideEffects, IsAlignStack); |
Chris Lattner | af8fffc | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 2903 | break; |
| 2904 | } |
Chad Rosier | d8c7610 | 2012-09-05 19:00:49 +0000 | [diff] [blame] | 2905 | // This version adds support for the asm dialect keywords (e.g., |
| 2906 | // inteldialect). |
Chad Rosier | 18fcdcf | 2012-09-05 00:56:20 +0000 | [diff] [blame] | 2907 | case bitc::CST_CODE_INLINEASM: { |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2908 | if (Record.size() < 2) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2909 | return error("Invalid record"); |
Chad Rosier | 18fcdcf | 2012-09-05 00:56:20 +0000 | [diff] [blame] | 2910 | std::string AsmStr, ConstrStr; |
| 2911 | bool HasSideEffects = Record[0] & 1; |
| 2912 | bool IsAlignStack = (Record[0] >> 1) & 1; |
| 2913 | unsigned AsmDialect = Record[0] >> 2; |
| 2914 | unsigned AsmStrSize = Record[1]; |
| 2915 | if (2+AsmStrSize >= Record.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2916 | return error("Invalid record"); |
Chad Rosier | 18fcdcf | 2012-09-05 00:56:20 +0000 | [diff] [blame] | 2917 | unsigned ConstStrSize = Record[2+AsmStrSize]; |
| 2918 | if (3+AsmStrSize+ConstStrSize > Record.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2919 | return error("Invalid record"); |
Chad Rosier | 18fcdcf | 2012-09-05 00:56:20 +0000 | [diff] [blame] | 2920 | |
| 2921 | for (unsigned i = 0; i != AsmStrSize; ++i) |
| 2922 | AsmStr += (char)Record[2+i]; |
| 2923 | for (unsigned i = 0; i != ConstStrSize; ++i) |
| 2924 | ConstrStr += (char)Record[3+AsmStrSize+i]; |
| 2925 | PointerType *PTy = cast<PointerType>(CurTy); |
| 2926 | V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()), |
| 2927 | AsmStr, ConstrStr, HasSideEffects, IsAlignStack, |
Chad Rosier | d8c7610 | 2012-09-05 19:00:49 +0000 | [diff] [blame] | 2928 | InlineAsm::AsmDialect(AsmDialect)); |
Chad Rosier | 18fcdcf | 2012-09-05 00:56:20 +0000 | [diff] [blame] | 2929 | break; |
| 2930 | } |
Chris Lattner | 5956dc8 | 2009-10-28 05:53:48 +0000 | [diff] [blame] | 2931 | case bitc::CST_CODE_BLOCKADDRESS:{ |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2932 | if (Record.size() < 3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2933 | return error("Invalid record"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2934 | Type *FnTy = getTypeByID(Record[0]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2935 | if (!FnTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2936 | return error("Invalid record"); |
Chris Lattner | 5956dc8 | 2009-10-28 05:53:48 +0000 | [diff] [blame] | 2937 | Function *Fn = |
| 2938 | dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy)); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2939 | if (!Fn) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2940 | return error("Invalid record"); |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 2941 | |
| 2942 | // If the function is already parsed we can insert the block address right |
| 2943 | // away. |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 2944 | BasicBlock *BB; |
| 2945 | unsigned BBID = Record[2]; |
| 2946 | if (!BBID) |
| 2947 | // Invalid reference to entry block. |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2948 | return error("Invalid ID"); |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 2949 | if (!Fn->empty()) { |
| 2950 | Function::iterator BBI = Fn->begin(), BBE = Fn->end(); |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 2951 | for (size_t I = 0, E = BBID; I != E; ++I) { |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 2952 | if (BBI == BBE) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2953 | return error("Invalid ID"); |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 2954 | ++BBI; |
| 2955 | } |
Duncan P. N. Exon Smith | fb1743a3 | 2015-10-13 16:48:55 +0000 | [diff] [blame] | 2956 | BB = &*BBI; |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 2957 | } else { |
| 2958 | // Otherwise insert a placeholder and remember it so it can be inserted |
| 2959 | // when the function is parsed. |
Duncan P. N. Exon Smith | 5a511b5 | 2014-08-05 17:49:48 +0000 | [diff] [blame] | 2960 | auto &FwdBBs = BasicBlockFwdRefs[Fn]; |
| 2961 | if (FwdBBs.empty()) |
| 2962 | BasicBlockFwdRefQueue.push_back(Fn); |
Duncan P. N. Exon Smith | 5a5fd7b | 2014-08-16 01:54:37 +0000 | [diff] [blame] | 2963 | if (FwdBBs.size() < BBID + 1) |
| 2964 | FwdBBs.resize(BBID + 1); |
| 2965 | if (!FwdBBs[BBID]) |
| 2966 | FwdBBs[BBID] = BasicBlock::Create(Context); |
| 2967 | BB = FwdBBs[BBID]; |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 2968 | } |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 2969 | V = BlockAddress::get(Fn, BB); |
Chris Lattner | 5956dc8 | 2009-10-28 05:53:48 +0000 | [diff] [blame] | 2970 | break; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2971 | } |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2972 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2973 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 2974 | ValueList.assignValue(V, NextCstNo); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 2975 | ++NextCstNo; |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2976 | } |
| 2977 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2978 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2979 | std::error_code BitcodeReader::parseUseLists() { |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 2980 | if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2981 | return error("Invalid record"); |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 2982 | |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 2983 | // Read all the records. |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 2984 | SmallVector<uint64_t, 64> Record; |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 2985 | while (1) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2986 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2987 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2988 | switch (Entry.Kind) { |
| 2989 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 2990 | case BitstreamEntry::Error: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2991 | return error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2992 | case BitstreamEntry::EndBlock: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2993 | return std::error_code(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2994 | case BitstreamEntry::Record: |
| 2995 | // The interesting case. |
| 2996 | break; |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 2997 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2998 | |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 2999 | // Read a use list record. |
| 3000 | Record.clear(); |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 3001 | bool IsBB = false; |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3002 | switch (Stream.readRecord(Entry.ID, Record)) { |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 3003 | default: // Default behavior: unknown type. |
| 3004 | break; |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 3005 | case bitc::USELIST_CODE_BB: |
| 3006 | IsBB = true; |
| 3007 | // fallthrough |
| 3008 | case bitc::USELIST_CODE_DEFAULT: { |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 3009 | unsigned RecordLength = Record.size(); |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 3010 | if (RecordLength < 3) |
| 3011 | // Records should have at least an ID and two indexes. |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3012 | return error("Invalid record"); |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 3013 | unsigned ID = Record.back(); |
| 3014 | Record.pop_back(); |
| 3015 | |
| 3016 | Value *V; |
| 3017 | if (IsBB) { |
| 3018 | assert(ID < FunctionBBs.size() && "Basic block not found"); |
| 3019 | V = FunctionBBs[ID]; |
| 3020 | } else |
| 3021 | V = ValueList[ID]; |
| 3022 | unsigned NumUses = 0; |
| 3023 | SmallDenseMap<const Use *, unsigned, 16> Order; |
Rafael Espindola | 257a353 | 2016-01-15 19:00:20 +0000 | [diff] [blame] | 3024 | for (const Use &U : V->materialized_uses()) { |
Duncan P. N. Exon Smith | 1318364 | 2014-08-16 01:54:34 +0000 | [diff] [blame] | 3025 | if (++NumUses > Record.size()) |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 3026 | break; |
Duncan P. N. Exon Smith | 1318364 | 2014-08-16 01:54:34 +0000 | [diff] [blame] | 3027 | Order[&U] = Record[NumUses - 1]; |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 3028 | } |
| 3029 | if (Order.size() != Record.size() || NumUses > Record.size()) |
| 3030 | // Mismatches can happen if the functions are being materialized lazily |
| 3031 | // (out-of-order), or a value has been upgraded. |
| 3032 | break; |
| 3033 | |
| 3034 | V->sortUseList([&](const Use &L, const Use &R) { |
| 3035 | return Order.lookup(&L) < Order.lookup(&R); |
| 3036 | }); |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 3037 | break; |
| 3038 | } |
| 3039 | } |
| 3040 | } |
| 3041 | } |
| 3042 | |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 3043 | /// When we see the block for metadata, remember where it is and then skip it. |
| 3044 | /// This lets us lazily deserialize the metadata. |
| 3045 | std::error_code BitcodeReader::rememberAndSkipMetadata() { |
| 3046 | // Save the current stream state. |
| 3047 | uint64_t CurBit = Stream.GetCurrentBitNo(); |
| 3048 | DeferredMetadataInfo.push_back(CurBit); |
| 3049 | |
| 3050 | // Skip over the block for now. |
| 3051 | if (Stream.SkipBlock()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3052 | return error("Invalid record"); |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 3053 | return std::error_code(); |
| 3054 | } |
| 3055 | |
| 3056 | std::error_code BitcodeReader::materializeMetadata() { |
| 3057 | for (uint64_t BitPos : DeferredMetadataInfo) { |
| 3058 | // Move the bit stream to the saved position. |
| 3059 | Stream.JumpToBit(BitPos); |
Teresa Johnson | d4d3dfd | 2015-11-20 14:51:27 +0000 | [diff] [blame] | 3060 | if (std::error_code EC = parseMetadata(true)) |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 3061 | return EC; |
| 3062 | } |
| 3063 | DeferredMetadataInfo.clear(); |
| 3064 | return std::error_code(); |
| 3065 | } |
| 3066 | |
Rafael Espindola | 468b868 | 2015-04-01 14:44:59 +0000 | [diff] [blame] | 3067 | void BitcodeReader::setStripDebugInfo() { StripDebugInfo = true; } |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 3068 | |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 3069 | void BitcodeReader::saveMetadataList( |
| 3070 | DenseMap<const Metadata *, unsigned> &MetadataToIDs, bool OnlyTempMD) { |
| 3071 | for (unsigned ID = 0; ID < MetadataList.size(); ++ID) { |
| 3072 | Metadata *MD = MetadataList[ID]; |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 3073 | auto *N = dyn_cast_or_null<MDNode>(MD); |
Teresa Johnson | 26aa935 | 2015-12-30 19:13:57 +0000 | [diff] [blame] | 3074 | assert((!N || (N->isResolved() || N->isTemporary())) && |
| 3075 | "Found non-resolved non-temp MDNode while saving metadata"); |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 3076 | // Save all values if !OnlyTempMD, otherwise just the temporary metadata. |
Teresa Johnson | 26aa935 | 2015-12-30 19:13:57 +0000 | [diff] [blame] | 3077 | // Note that in the !OnlyTempMD case we need to save all Metadata, not |
| 3078 | // just MDNode, as we may have references to other types of module-level |
| 3079 | // metadata (e.g. ValueAsMetadata) from instructions. |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 3080 | if (!OnlyTempMD || (N && N->isTemporary())) { |
| 3081 | // Will call this after materializing each function, in order to |
| 3082 | // handle remapping of the function's instructions/metadata. |
Teresa Johnson | 6f508af | 2016-01-21 16:46:40 +0000 | [diff] [blame] | 3083 | auto IterBool = MetadataToIDs.insert(std::make_pair(MD, ID)); |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 3084 | // See if we already have an entry in that case. |
Teresa Johnson | 6f508af | 2016-01-21 16:46:40 +0000 | [diff] [blame] | 3085 | if (OnlyTempMD && !IterBool.second) { |
| 3086 | assert(IterBool.first->second == ID && |
| 3087 | "Inconsistent metadata value id"); |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 3088 | continue; |
| 3089 | } |
Teresa Johnson | cc42857 | 2015-12-30 19:32:24 +0000 | [diff] [blame] | 3090 | if (N && N->isTemporary()) |
| 3091 | // Ensure that we assert if someone tries to RAUW this temporary |
| 3092 | // metadata while it is the key of a map. The flag will be set back |
| 3093 | // to true when the saved metadata list is destroyed. |
| 3094 | N->setCanReplace(false); |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 3095 | } |
| 3096 | } |
| 3097 | } |
| 3098 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3099 | /// When we see the block for a function body, remember where it is and then |
| 3100 | /// skip it. This lets us lazily deserialize the functions. |
| 3101 | std::error_code BitcodeReader::rememberAndSkipFunctionBody() { |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 3102 | // Get the function we are talking about. |
| 3103 | if (FunctionsWithBodies.empty()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3104 | return error("Insufficient function protos"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3105 | |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 3106 | Function *Fn = FunctionsWithBodies.back(); |
| 3107 | FunctionsWithBodies.pop_back(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3108 | |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 3109 | // Save the current stream state. |
| 3110 | uint64_t CurBit = Stream.GetCurrentBitNo(); |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 3111 | assert( |
| 3112 | (DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) && |
| 3113 | "Mismatch between VST and scanned function offsets"); |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 3114 | DeferredFunctionInfo[Fn] = CurBit; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3115 | |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 3116 | // Skip over the function block for now. |
| 3117 | if (Stream.SkipBlock()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3118 | return error("Invalid record"); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3119 | return std::error_code(); |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 3120 | } |
| 3121 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3122 | std::error_code BitcodeReader::globalCleanup() { |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3123 | // Patch the initializers for globals and aliases up. |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3124 | resolveGlobalAndAliasInits(); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3125 | if (!GlobalInits.empty() || !AliasInits.empty()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3126 | return error("Malformed global initializer set"); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3127 | |
| 3128 | // Look for intrinsic functions which need to be upgraded at some point |
Yaron Keren | ef5e7ad | 2015-06-12 18:13:20 +0000 | [diff] [blame] | 3129 | for (Function &F : *TheModule) { |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3130 | Function *NewFn; |
Yaron Keren | ef5e7ad | 2015-06-12 18:13:20 +0000 | [diff] [blame] | 3131 | if (UpgradeIntrinsicFunction(&F, NewFn)) |
Rafael Espindola | 4e72121 | 2015-07-02 16:22:40 +0000 | [diff] [blame] | 3132 | UpgradedIntrinsics[&F] = NewFn; |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3133 | } |
| 3134 | |
| 3135 | // Look for global variables which need to be renamed. |
Yaron Keren | ef5e7ad | 2015-06-12 18:13:20 +0000 | [diff] [blame] | 3136 | for (GlobalVariable &GV : TheModule->globals()) |
| 3137 | UpgradeGlobalVariable(&GV); |
Reid Kleckner | fceb76f | 2014-05-16 20:39:27 +0000 | [diff] [blame] | 3138 | |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3139 | // Force deallocation of memory for these vectors to favor the client that |
| 3140 | // want lazy deserialization. |
| 3141 | std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits); |
| 3142 | std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3143 | return std::error_code(); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3144 | } |
| 3145 | |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 3146 | /// Support for lazy parsing of function bodies. This is required if we |
| 3147 | /// either have an old bitcode file without a VST forward declaration record, |
| 3148 | /// or if we have an anonymous function being materialized, since anonymous |
| 3149 | /// functions do not have a name and are therefore not in the VST. |
| 3150 | std::error_code BitcodeReader::rememberAndSkipFunctionBodies() { |
| 3151 | Stream.JumpToBit(NextUnreadBit); |
| 3152 | |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 3153 | if (Stream.AtEndOfStream()) |
| 3154 | return error("Could not find function in stream"); |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 3155 | |
Filipe Cabecinhas | 7aae2f2 | 2015-11-03 13:48:26 +0000 | [diff] [blame] | 3156 | if (!SeenFirstFunctionBody) |
| 3157 | return error("Trying to materialize functions before seeing function blocks"); |
| 3158 | |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 3159 | // An old bitcode file with the symbol table at the end would have |
| 3160 | // finished the parse greedily. |
| 3161 | assert(SeenValueSymbolTable); |
| 3162 | |
| 3163 | SmallVector<uint64_t, 64> Record; |
| 3164 | |
| 3165 | while (1) { |
| 3166 | BitstreamEntry Entry = Stream.advance(); |
| 3167 | switch (Entry.Kind) { |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 3168 | default: |
| 3169 | return error("Expect SubBlock"); |
| 3170 | case BitstreamEntry::SubBlock: |
| 3171 | switch (Entry.ID) { |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 3172 | default: |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 3173 | return error("Expect function block"); |
| 3174 | case bitc::FUNCTION_BLOCK_ID: |
| 3175 | if (std::error_code EC = rememberAndSkipFunctionBody()) |
| 3176 | return EC; |
| 3177 | NextUnreadBit = Stream.GetCurrentBitNo(); |
| 3178 | return std::error_code(); |
| 3179 | } |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 3180 | } |
| 3181 | } |
| 3182 | } |
| 3183 | |
Mehdi Amini | 5d30328 | 2015-10-26 18:37:00 +0000 | [diff] [blame] | 3184 | std::error_code BitcodeReader::parseBitcodeVersion() { |
| 3185 | if (Stream.EnterSubBlock(bitc::IDENTIFICATION_BLOCK_ID)) |
| 3186 | return error("Invalid record"); |
| 3187 | |
| 3188 | // Read all the records. |
| 3189 | SmallVector<uint64_t, 64> Record; |
| 3190 | while (1) { |
| 3191 | BitstreamEntry Entry = Stream.advance(); |
| 3192 | |
| 3193 | switch (Entry.Kind) { |
| 3194 | default: |
| 3195 | case BitstreamEntry::Error: |
| 3196 | return error("Malformed block"); |
| 3197 | case BitstreamEntry::EndBlock: |
| 3198 | return std::error_code(); |
| 3199 | case BitstreamEntry::Record: |
| 3200 | // The interesting case. |
| 3201 | break; |
| 3202 | } |
| 3203 | |
| 3204 | // Read a record. |
| 3205 | Record.clear(); |
| 3206 | unsigned BitCode = Stream.readRecord(Entry.ID, Record); |
| 3207 | switch (BitCode) { |
| 3208 | default: // Default behavior: reject |
| 3209 | return error("Invalid value"); |
| 3210 | case bitc::IDENTIFICATION_CODE_STRING: { // IDENTIFICATION: [strchr x |
| 3211 | // N] |
| 3212 | convertToString(Record, 0, ProducerIdentification); |
| 3213 | break; |
| 3214 | } |
| 3215 | case bitc::IDENTIFICATION_CODE_EPOCH: { // EPOCH: [epoch#] |
| 3216 | unsigned epoch = (unsigned)Record[0]; |
| 3217 | if (epoch != bitc::BITCODE_CURRENT_EPOCH) { |
Oleksiy Vyalov | 6c2403f | 2015-10-26 22:37:36 +0000 | [diff] [blame] | 3218 | return error( |
| 3219 | Twine("Incompatible epoch: Bitcode '") + Twine(epoch) + |
| 3220 | "' vs current: '" + Twine(bitc::BITCODE_CURRENT_EPOCH) + "'"); |
Mehdi Amini | 5d30328 | 2015-10-26 18:37:00 +0000 | [diff] [blame] | 3221 | } |
| 3222 | } |
| 3223 | } |
| 3224 | } |
| 3225 | } |
| 3226 | |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 3227 | std::error_code BitcodeReader::parseModule(uint64_t ResumeBit, |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 3228 | bool ShouldLazyLoadMetadata) { |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 3229 | if (ResumeBit) |
| 3230 | Stream.JumpToBit(ResumeBit); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3231 | else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3232 | return error("Invalid record"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3233 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3234 | SmallVector<uint64_t, 64> Record; |
| 3235 | std::vector<std::string> SectionTable; |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 3236 | std::vector<std::string> GCTable; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3237 | |
| 3238 | // Read all the records for this module. |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3239 | while (1) { |
| 3240 | BitstreamEntry Entry = Stream.advance(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3241 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3242 | switch (Entry.Kind) { |
| 3243 | case BitstreamEntry::Error: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3244 | return error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3245 | case BitstreamEntry::EndBlock: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3246 | return globalCleanup(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3247 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3248 | case BitstreamEntry::SubBlock: |
| 3249 | switch (Entry.ID) { |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3250 | default: // Skip unknown content. |
| 3251 | if (Stream.SkipBlock()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3252 | return error("Invalid record"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3253 | break; |
Chris Lattner | 6eeea5d | 2007-05-05 18:57:30 +0000 | [diff] [blame] | 3254 | case bitc::BLOCKINFO_BLOCK_ID: |
| 3255 | if (Stream.ReadBlockInfoBlock()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3256 | return error("Malformed block"); |
Chris Lattner | 6eeea5d | 2007-05-05 18:57:30 +0000 | [diff] [blame] | 3257 | break; |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 3258 | case bitc::PARAMATTR_BLOCK_ID: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3259 | if (std::error_code EC = parseAttributeBlock()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3260 | return EC; |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 3261 | break; |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 3262 | case bitc::PARAMATTR_GROUP_BLOCK_ID: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3263 | if (std::error_code EC = parseAttributeGroupBlock()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3264 | return EC; |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 3265 | break; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3266 | case bitc::TYPE_BLOCK_ID_NEW: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3267 | if (std::error_code EC = parseTypeTable()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3268 | return EC; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3269 | break; |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 3270 | case bitc::VALUE_SYMTAB_BLOCK_ID: |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 3271 | if (!SeenValueSymbolTable) { |
| 3272 | // Either this is an old form VST without function index and an |
| 3273 | // associated VST forward declaration record (which would have caused |
| 3274 | // the VST to be jumped to and parsed before it was encountered |
| 3275 | // normally in the stream), or there were no function blocks to |
| 3276 | // trigger an earlier parsing of the VST. |
| 3277 | assert(VSTOffset == 0 || FunctionsWithBodies.empty()); |
| 3278 | if (std::error_code EC = parseValueSymbolTable()) |
| 3279 | return EC; |
| 3280 | SeenValueSymbolTable = true; |
| 3281 | } else { |
| 3282 | // We must have had a VST forward declaration record, which caused |
| 3283 | // the parser to jump to and parse the VST earlier. |
| 3284 | assert(VSTOffset > 0); |
| 3285 | if (Stream.SkipBlock()) |
| 3286 | return error("Invalid record"); |
| 3287 | } |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 3288 | break; |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 3289 | case bitc::CONSTANTS_BLOCK_ID: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3290 | if (std::error_code EC = parseConstants()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3291 | return EC; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3292 | if (std::error_code EC = resolveGlobalAndAliasInits()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3293 | return EC; |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 3294 | break; |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 3295 | case bitc::METADATA_BLOCK_ID: |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 3296 | if (ShouldLazyLoadMetadata && !IsMetadataMaterialized) { |
| 3297 | if (std::error_code EC = rememberAndSkipMetadata()) |
| 3298 | return EC; |
| 3299 | break; |
| 3300 | } |
| 3301 | assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata"); |
Teresa Johnson | d4d3dfd | 2015-11-20 14:51:27 +0000 | [diff] [blame] | 3302 | if (std::error_code EC = parseMetadata(true)) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3303 | return EC; |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 3304 | break; |
Teresa Johnson | 1254507 | 2015-11-15 02:00:09 +0000 | [diff] [blame] | 3305 | case bitc::METADATA_KIND_BLOCK_ID: |
| 3306 | if (std::error_code EC = parseMetadataKinds()) |
| 3307 | return EC; |
| 3308 | break; |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 3309 | case bitc::FUNCTION_BLOCK_ID: |
| 3310 | // If this is the first function body we've seen, reverse the |
| 3311 | // FunctionsWithBodies list. |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3312 | if (!SeenFirstFunctionBody) { |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 3313 | std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end()); |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3314 | if (std::error_code EC = globalCleanup()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3315 | return EC; |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3316 | SeenFirstFunctionBody = true; |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 3317 | } |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3318 | |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 3319 | if (VSTOffset > 0) { |
| 3320 | // If we have a VST forward declaration record, make sure we |
| 3321 | // parse the VST now if we haven't already. It is needed to |
| 3322 | // set up the DeferredFunctionInfo vector for lazy reading. |
| 3323 | if (!SeenValueSymbolTable) { |
| 3324 | if (std::error_code EC = |
| 3325 | BitcodeReader::parseValueSymbolTable(VSTOffset)) |
| 3326 | return EC; |
| 3327 | SeenValueSymbolTable = true; |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 3328 | // Fall through so that we record the NextUnreadBit below. |
| 3329 | // This is necessary in case we have an anonymous function that |
| 3330 | // is later materialized. Since it will not have a VST entry we |
| 3331 | // need to fall back to the lazy parse to find its offset. |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 3332 | } else { |
| 3333 | // If we have a VST forward declaration record, but have already |
| 3334 | // parsed the VST (just above, when the first function body was |
| 3335 | // encountered here), then we are resuming the parse after |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 3336 | // materializing functions. The ResumeBit points to the |
| 3337 | // start of the last function block recorded in the |
| 3338 | // DeferredFunctionInfo map. Skip it. |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 3339 | if (Stream.SkipBlock()) |
| 3340 | return error("Invalid record"); |
| 3341 | continue; |
| 3342 | } |
| 3343 | } |
| 3344 | |
| 3345 | // Support older bitcode files that did not have the function |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 3346 | // index in the VST, nor a VST forward declaration record, as |
| 3347 | // well as anonymous functions that do not have VST entries. |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 3348 | // Build the DeferredFunctionInfo vector on the fly. |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3349 | if (std::error_code EC = rememberAndSkipFunctionBody()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3350 | return EC; |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 3351 | |
Rafael Espindola | 1c863ca | 2015-06-22 18:06:15 +0000 | [diff] [blame] | 3352 | // Suspend parsing when we reach the function bodies. Subsequent |
| 3353 | // materialization calls will resume it when necessary. If the bitcode |
| 3354 | // file is old, the symbol table will be at the end instead and will not |
| 3355 | // have been seen yet. In this case, just finish the parse now. |
| 3356 | if (SeenValueSymbolTable) { |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3357 | NextUnreadBit = Stream.GetCurrentBitNo(); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3358 | return std::error_code(); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3359 | } |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 3360 | break; |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 3361 | case bitc::USELIST_BLOCK_ID: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3362 | if (std::error_code EC = parseUseLists()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3363 | return EC; |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 3364 | break; |
Sanjoy Das | b513a9f | 2015-09-24 23:34:52 +0000 | [diff] [blame] | 3365 | case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID: |
| 3366 | if (std::error_code EC = parseOperandBundleTags()) |
| 3367 | return EC; |
| 3368 | break; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3369 | } |
| 3370 | continue; |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3371 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3372 | case BitstreamEntry::Record: |
| 3373 | // The interesting case. |
| 3374 | break; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3375 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3376 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3377 | // Read a record. |
David Blaikie | 6a51dbd | 2015-09-17 22:18:59 +0000 | [diff] [blame] | 3378 | auto BitCode = Stream.readRecord(Entry.ID, Record); |
| 3379 | switch (BitCode) { |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3380 | default: break; // Default behavior, ignore unknown content. |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3381 | case bitc::MODULE_CODE_VERSION: { // VERSION: [version#] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3382 | if (Record.size() < 1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3383 | return error("Invalid record"); |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3384 | // Only version #0 and #1 are supported so far. |
| 3385 | unsigned module_version = Record[0]; |
| 3386 | switch (module_version) { |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3387 | default: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3388 | return error("Invalid value"); |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3389 | case 0: |
| 3390 | UseRelativeIDs = false; |
| 3391 | break; |
| 3392 | case 1: |
| 3393 | UseRelativeIDs = true; |
| 3394 | break; |
| 3395 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3396 | break; |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3397 | } |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 3398 | case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3399 | std::string S; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3400 | if (convertToString(Record, 0, S)) |
| 3401 | return error("Invalid record"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3402 | TheModule->setTargetTriple(S); |
| 3403 | break; |
| 3404 | } |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 3405 | case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3406 | std::string S; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3407 | if (convertToString(Record, 0, S)) |
| 3408 | return error("Invalid record"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3409 | TheModule->setDataLayout(S); |
| 3410 | break; |
| 3411 | } |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 3412 | case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3413 | std::string S; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3414 | if (convertToString(Record, 0, S)) |
| 3415 | return error("Invalid record"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3416 | TheModule->setModuleInlineAsm(S); |
| 3417 | break; |
| 3418 | } |
Bill Wendling | 706d3d6 | 2012-11-28 08:41:48 +0000 | [diff] [blame] | 3419 | case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N] |
| 3420 | // FIXME: Remove in 4.0. |
| 3421 | std::string S; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3422 | if (convertToString(Record, 0, S)) |
| 3423 | return error("Invalid record"); |
Bill Wendling | 706d3d6 | 2012-11-28 08:41:48 +0000 | [diff] [blame] | 3424 | // Ignore value. |
| 3425 | break; |
| 3426 | } |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 3427 | case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3428 | std::string S; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3429 | if (convertToString(Record, 0, S)) |
| 3430 | return error("Invalid record"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3431 | SectionTable.push_back(S); |
| 3432 | break; |
| 3433 | } |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 3434 | case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N] |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 3435 | std::string S; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3436 | if (convertToString(Record, 0, S)) |
| 3437 | return error("Invalid record"); |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 3438 | GCTable.push_back(S); |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 3439 | break; |
| 3440 | } |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 3441 | case bitc::MODULE_CODE_COMDAT: { // COMDAT: [selection_kind, name] |
| 3442 | if (Record.size() < 2) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3443 | return error("Invalid record"); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 3444 | Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]); |
| 3445 | unsigned ComdatNameSize = Record[1]; |
| 3446 | std::string ComdatName; |
| 3447 | ComdatName.reserve(ComdatNameSize); |
| 3448 | for (unsigned i = 0; i != ComdatNameSize; ++i) |
| 3449 | ComdatName += (char)Record[2 + i]; |
| 3450 | Comdat *C = TheModule->getOrInsertComdat(ComdatName); |
| 3451 | C->setSelectionKind(SK); |
| 3452 | ComdatList.push_back(C); |
| 3453 | break; |
| 3454 | } |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 3455 | // GLOBALVAR: [pointer type, isconst, initid, |
Rafael Espindola | 45e6c19 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 3456 | // linkage, alignment, section, visibility, threadlocal, |
Peter Collingbourne | 69ba016 | 2015-02-04 00:42:45 +0000 | [diff] [blame] | 3457 | // unnamed_addr, externally_initialized, dllstorageclass, |
| 3458 | // comdat] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3459 | case bitc::MODULE_CODE_GLOBALVAR: { |
Chris Lattner | 4b00d92 | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 3460 | if (Record.size() < 6) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3461 | return error("Invalid record"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3462 | Type *Ty = getTypeByID(Record[0]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3463 | if (!Ty) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3464 | return error("Invalid record"); |
David Blaikie | 1a848da | 2015-04-27 19:58:56 +0000 | [diff] [blame] | 3465 | bool isConstant = Record[1] & 1; |
| 3466 | bool explicitType = Record[1] & 2; |
| 3467 | unsigned AddressSpace; |
| 3468 | if (explicitType) { |
| 3469 | AddressSpace = Record[1] >> 2; |
| 3470 | } else { |
| 3471 | if (!Ty->isPointerTy()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3472 | return error("Invalid type for value"); |
David Blaikie | 1a848da | 2015-04-27 19:58:56 +0000 | [diff] [blame] | 3473 | AddressSpace = cast<PointerType>(Ty)->getAddressSpace(); |
| 3474 | Ty = cast<PointerType>(Ty)->getElementType(); |
| 3475 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3476 | |
Rafael Espindola | 12ca34f | 2015-01-19 15:16:06 +0000 | [diff] [blame] | 3477 | uint64_t RawLinkage = Record[3]; |
| 3478 | GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage); |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 3479 | unsigned Alignment; |
| 3480 | if (std::error_code EC = parseAlignmentValue(Record[4], Alignment)) |
| 3481 | return EC; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3482 | std::string Section; |
| 3483 | if (Record[5]) { |
| 3484 | if (Record[5]-1 >= SectionTable.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3485 | return error("Invalid ID"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3486 | Section = SectionTable[Record[5]-1]; |
| 3487 | } |
Chris Lattner | 4b00d92 | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 3488 | GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility; |
Duncan P. N. Exon Smith | b80de10 | 2014-05-07 22:57:20 +0000 | [diff] [blame] | 3489 | // Local linkage must have default visibility. |
| 3490 | if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage)) |
| 3491 | // FIXME: Change to an error if non-default in 4.0. |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3492 | Visibility = getDecodedVisibility(Record[6]); |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 3493 | |
| 3494 | GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal; |
Chris Lattner | 53862f7 | 2007-05-06 19:27:46 +0000 | [diff] [blame] | 3495 | if (Record.size() > 7) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3496 | TLM = getDecodedThreadLocalMode(Record[7]); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3497 | |
Rafael Espindola | 45e6c19 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 3498 | bool UnnamedAddr = false; |
| 3499 | if (Record.size() > 8) |
| 3500 | UnnamedAddr = Record[8]; |
| 3501 | |
Michael Gottesman | 27e7ef3 | 2013-02-05 05:57:38 +0000 | [diff] [blame] | 3502 | bool ExternallyInitialized = false; |
| 3503 | if (Record.size() > 9) |
| 3504 | ExternallyInitialized = Record[9]; |
| 3505 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3506 | GlobalVariable *NewGV = |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3507 | new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, "", nullptr, |
Michael Gottesman | 27e7ef3 | 2013-02-05 05:57:38 +0000 | [diff] [blame] | 3508 | TLM, AddressSpace, ExternallyInitialized); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3509 | NewGV->setAlignment(Alignment); |
| 3510 | if (!Section.empty()) |
| 3511 | NewGV->setSection(Section); |
| 3512 | NewGV->setVisibility(Visibility); |
Rafael Espindola | 45e6c19 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 3513 | NewGV->setUnnamedAddr(UnnamedAddr); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3514 | |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 3515 | if (Record.size() > 10) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3516 | NewGV->setDLLStorageClass(getDecodedDLLStorageClass(Record[10])); |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 3517 | else |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3518 | upgradeDLLImportExportLinkage(NewGV, RawLinkage); |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 3519 | |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 3520 | ValueList.push_back(NewGV); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3521 | |
Chris Lattner | 47d131b | 2007-04-24 00:18:21 +0000 | [diff] [blame] | 3522 | // Remember which value to use for the global initializer. |
| 3523 | if (unsigned InitID = Record[2]) |
| 3524 | GlobalInits.push_back(std::make_pair(NewGV, InitID-1)); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 3525 | |
Rafael Espindola | 12ca34f | 2015-01-19 15:16:06 +0000 | [diff] [blame] | 3526 | if (Record.size() > 11) { |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 3527 | if (unsigned ComdatID = Record[11]) { |
Filipe Cabecinhas | 0eb8a59 | 2015-05-26 23:00:56 +0000 | [diff] [blame] | 3528 | if (ComdatID > ComdatList.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3529 | return error("Invalid global variable comdat ID"); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 3530 | NewGV->setComdat(ComdatList[ComdatID - 1]); |
| 3531 | } |
Rafael Espindola | 12ca34f | 2015-01-19 15:16:06 +0000 | [diff] [blame] | 3532 | } else if (hasImplicitComdat(RawLinkage)) { |
| 3533 | NewGV->setComdat(reinterpret_cast<Comdat *>(1)); |
| 3534 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3535 | break; |
| 3536 | } |
Chris Lattner | 4c0a6d6 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 3537 | // FUNCTION: [type, callingconv, isproto, linkage, paramattr, |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 3538 | // alignment, section, visibility, gc, unnamed_addr, |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 3539 | // prologuedata, dllstorageclass, comdat, prefixdata] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3540 | case bitc::MODULE_CODE_FUNCTION: { |
Chris Lattner | 4c0a6d6 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 3541 | if (Record.size() < 8) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3542 | return error("Invalid record"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3543 | Type *Ty = getTypeByID(Record[0]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3544 | if (!Ty) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3545 | return error("Invalid record"); |
David Blaikie | 561a157 | 2015-04-17 16:28:26 +0000 | [diff] [blame] | 3546 | if (auto *PTy = dyn_cast<PointerType>(Ty)) |
| 3547 | Ty = PTy->getElementType(); |
| 3548 | auto *FTy = dyn_cast<FunctionType>(Ty); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3549 | if (!FTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3550 | return error("Invalid type for value"); |
Vedant Kumar | ad6d6e7 | 2015-10-27 21:17:06 +0000 | [diff] [blame] | 3551 | auto CC = static_cast<CallingConv::ID>(Record[1]); |
| 3552 | if (CC & ~CallingConv::MaxID) |
| 3553 | return error("Invalid calling convention ID"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3554 | |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 3555 | Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage, |
| 3556 | "", TheModule); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3557 | |
Vedant Kumar | ad6d6e7 | 2015-10-27 21:17:06 +0000 | [diff] [blame] | 3558 | Func->setCallingConv(CC); |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 3559 | bool isProto = Record[2]; |
Rafael Espindola | 12ca34f | 2015-01-19 15:16:06 +0000 | [diff] [blame] | 3560 | uint64_t RawLinkage = Record[3]; |
| 3561 | Func->setLinkage(getDecodedLinkage(RawLinkage)); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 3562 | Func->setAttributes(getAttributes(Record[4])); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3563 | |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 3564 | unsigned Alignment; |
| 3565 | if (std::error_code EC = parseAlignmentValue(Record[5], Alignment)) |
| 3566 | return EC; |
| 3567 | Func->setAlignment(Alignment); |
Chris Lattner | 4c0a6d6 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 3568 | if (Record[6]) { |
| 3569 | if (Record[6]-1 >= SectionTable.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3570 | return error("Invalid ID"); |
Chris Lattner | 4c0a6d6 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 3571 | Func->setSection(SectionTable[Record[6]-1]); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3572 | } |
Duncan P. N. Exon Smith | b80de10 | 2014-05-07 22:57:20 +0000 | [diff] [blame] | 3573 | // Local linkage must have default visibility. |
| 3574 | if (!Func->hasLocalLinkage()) |
| 3575 | // FIXME: Change to an error if non-default in 4.0. |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3576 | Func->setVisibility(getDecodedVisibility(Record[7])); |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 3577 | if (Record.size() > 8 && Record[8]) { |
Filipe Cabecinhas | f8a16a9 | 2015-04-30 04:09:41 +0000 | [diff] [blame] | 3578 | if (Record[8]-1 >= GCTable.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3579 | return error("Invalid ID"); |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 3580 | Func->setGC(GCTable[Record[8]-1].c_str()); |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 3581 | } |
Rafael Espindola | 45e6c19 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 3582 | bool UnnamedAddr = false; |
| 3583 | if (Record.size() > 9) |
| 3584 | UnnamedAddr = Record[9]; |
| 3585 | Func->setUnnamedAddr(UnnamedAddr); |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 3586 | if (Record.size() > 10 && Record[10] != 0) |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 3587 | FunctionPrologues.push_back(std::make_pair(Func, Record[10]-1)); |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 3588 | |
| 3589 | if (Record.size() > 11) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3590 | Func->setDLLStorageClass(getDecodedDLLStorageClass(Record[11])); |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 3591 | else |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3592 | upgradeDLLImportExportLinkage(Func, RawLinkage); |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 3593 | |
Rafael Espindola | 12ca34f | 2015-01-19 15:16:06 +0000 | [diff] [blame] | 3594 | if (Record.size() > 12) { |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 3595 | if (unsigned ComdatID = Record[12]) { |
Filipe Cabecinhas | 0eb8a59 | 2015-05-26 23:00:56 +0000 | [diff] [blame] | 3596 | if (ComdatID > ComdatList.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3597 | return error("Invalid function comdat ID"); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 3598 | Func->setComdat(ComdatList[ComdatID - 1]); |
| 3599 | } |
Rafael Espindola | 12ca34f | 2015-01-19 15:16:06 +0000 | [diff] [blame] | 3600 | } else if (hasImplicitComdat(RawLinkage)) { |
| 3601 | Func->setComdat(reinterpret_cast<Comdat *>(1)); |
| 3602 | } |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 3603 | |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 3604 | if (Record.size() > 13 && Record[13] != 0) |
| 3605 | FunctionPrefixes.push_back(std::make_pair(Func, Record[13]-1)); |
| 3606 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 3607 | if (Record.size() > 14 && Record[14] != 0) |
| 3608 | FunctionPersonalityFns.push_back(std::make_pair(Func, Record[14] - 1)); |
| 3609 | |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 3610 | ValueList.push_back(Func); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3611 | |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 3612 | // If this is a function with a body, remember the prototype we are |
| 3613 | // creating now, so that we can match up the body with them later. |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3614 | if (!isProto) { |
Rafael Espindola | d4bcefc | 2014-10-24 18:13:04 +0000 | [diff] [blame] | 3615 | Func->setIsMaterializable(true); |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 3616 | FunctionsWithBodies.push_back(Func); |
Rafael Espindola | 1c863ca | 2015-06-22 18:06:15 +0000 | [diff] [blame] | 3617 | DeferredFunctionInfo[Func] = 0; |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3618 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3619 | break; |
| 3620 | } |
David Blaikie | 6a51dbd | 2015-09-17 22:18:59 +0000 | [diff] [blame] | 3621 | // ALIAS: [alias type, addrspace, aliasee val#, linkage] |
| 3622 | // ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility, dllstorageclass] |
| 3623 | case bitc::MODULE_CODE_ALIAS: |
| 3624 | case bitc::MODULE_CODE_ALIAS_OLD: { |
| 3625 | bool NewRecord = BitCode == bitc::MODULE_CODE_ALIAS; |
Aaron Ballman | 2d0f38c | 2015-09-18 13:31:42 +0000 | [diff] [blame] | 3626 | if (Record.size() < (3 + (unsigned)NewRecord)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3627 | return error("Invalid record"); |
David Blaikie | 6a51dbd | 2015-09-17 22:18:59 +0000 | [diff] [blame] | 3628 | unsigned OpNum = 0; |
| 3629 | Type *Ty = getTypeByID(Record[OpNum++]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3630 | if (!Ty) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3631 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3632 | |
David Blaikie | 6a51dbd | 2015-09-17 22:18:59 +0000 | [diff] [blame] | 3633 | unsigned AddrSpace; |
| 3634 | if (!NewRecord) { |
| 3635 | auto *PTy = dyn_cast<PointerType>(Ty); |
| 3636 | if (!PTy) |
| 3637 | return error("Invalid type for value"); |
| 3638 | Ty = PTy->getElementType(); |
| 3639 | AddrSpace = PTy->getAddressSpace(); |
| 3640 | } else { |
| 3641 | AddrSpace = Record[OpNum++]; |
| 3642 | } |
| 3643 | |
| 3644 | auto Val = Record[OpNum++]; |
| 3645 | auto Linkage = Record[OpNum++]; |
| 3646 | auto *NewGA = GlobalAlias::create( |
| 3647 | Ty, AddrSpace, getDecodedLinkage(Linkage), "", TheModule); |
Anton Korobeynikov | 2f22e3f | 2008-03-12 00:49:19 +0000 | [diff] [blame] | 3648 | // Old bitcode files didn't have visibility field. |
Duncan P. N. Exon Smith | b80de10 | 2014-05-07 22:57:20 +0000 | [diff] [blame] | 3649 | // Local linkage must have default visibility. |
David Blaikie | 6a51dbd | 2015-09-17 22:18:59 +0000 | [diff] [blame] | 3650 | if (OpNum != Record.size()) { |
| 3651 | auto VisInd = OpNum++; |
| 3652 | if (!NewGA->hasLocalLinkage()) |
| 3653 | // FIXME: Change to an error if non-default in 4.0. |
| 3654 | NewGA->setVisibility(getDecodedVisibility(Record[VisInd])); |
| 3655 | } |
| 3656 | if (OpNum != Record.size()) |
| 3657 | NewGA->setDLLStorageClass(getDecodedDLLStorageClass(Record[OpNum++])); |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 3658 | else |
David Blaikie | 6a51dbd | 2015-09-17 22:18:59 +0000 | [diff] [blame] | 3659 | upgradeDLLImportExportLinkage(NewGA, Linkage); |
| 3660 | if (OpNum != Record.size()) |
| 3661 | NewGA->setThreadLocalMode(getDecodedThreadLocalMode(Record[OpNum++])); |
| 3662 | if (OpNum != Record.size()) |
| 3663 | NewGA->setUnnamedAddr(Record[OpNum++]); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 3664 | ValueList.push_back(NewGA); |
David Blaikie | 6a51dbd | 2015-09-17 22:18:59 +0000 | [diff] [blame] | 3665 | AliasInits.push_back(std::make_pair(NewGA, Val)); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 3666 | break; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3667 | } |
Chris Lattner | 831d420 | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 3668 | /// MODULE_CODE_PURGEVALS: [numvals] |
| 3669 | case bitc::MODULE_CODE_PURGEVALS: |
| 3670 | // Trim down the value list to the specified size. |
| 3671 | if (Record.size() < 1 || Record[0] > ValueList.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3672 | return error("Invalid record"); |
Chris Lattner | 831d420 | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 3673 | ValueList.shrinkTo(Record[0]); |
| 3674 | break; |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 3675 | /// MODULE_CODE_VSTOFFSET: [offset] |
| 3676 | case bitc::MODULE_CODE_VSTOFFSET: |
| 3677 | if (Record.size() < 1) |
| 3678 | return error("Invalid record"); |
| 3679 | VSTOffset = Record[0]; |
| 3680 | break; |
Teresa Johnson | d4d3dfd | 2015-11-20 14:51:27 +0000 | [diff] [blame] | 3681 | /// MODULE_CODE_METADATA_VALUES: [numvals] |
| 3682 | case bitc::MODULE_CODE_METADATA_VALUES: |
| 3683 | if (Record.size() < 1) |
| 3684 | return error("Invalid record"); |
| 3685 | assert(!IsMetadataMaterialized); |
| 3686 | // This record contains the number of metadata values in the module-level |
| 3687 | // METADATA_BLOCK. It is used to support lazy parsing of metadata as |
| 3688 | // a postpass, where we will parse function-level metadata first. |
| 3689 | // This is needed because the ids of metadata are assigned implicitly |
| 3690 | // based on their ordering in the bitcode, with the function-level |
| 3691 | // metadata ids starting after the module-level metadata ids. Otherwise, |
| 3692 | // we would have to parse the module-level metadata block to prime the |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 3693 | // MetadataList when we are lazy loading metadata during function |
| 3694 | // importing. Initialize the MetadataList size here based on the |
Teresa Johnson | d4d3dfd | 2015-11-20 14:51:27 +0000 | [diff] [blame] | 3695 | // record value, regardless of whether we are doing lazy metadata |
| 3696 | // loading, so that we have consistent handling and assertion |
| 3697 | // checking in parseMetadata for module-level metadata. |
| 3698 | NumModuleMDs = Record[0]; |
| 3699 | SeenModuleValuesRecord = true; |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 3700 | assert(MetadataList.size() == 0); |
| 3701 | MetadataList.resize(NumModuleMDs); |
Teresa Johnson | d4d3dfd | 2015-11-20 14:51:27 +0000 | [diff] [blame] | 3702 | break; |
Teresa Johnson | e1164de | 2016-02-10 21:55:02 +0000 | [diff] [blame] | 3703 | /// MODULE_CODE_SOURCE_FILENAME: [namechar x N] |
| 3704 | case bitc::MODULE_CODE_SOURCE_FILENAME: |
| 3705 | SmallString<128> ValueName; |
| 3706 | if (convertToString(Record, 0, ValueName)) |
| 3707 | return error("Invalid record"); |
| 3708 | TheModule->setSourceFileName(ValueName); |
| 3709 | break; |
Chris Lattner | 831d420 | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 3710 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3711 | Record.clear(); |
| 3712 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3713 | } |
| 3714 | |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 3715 | /// Helper to read the header common to all bitcode files. |
| 3716 | static bool hasValidBitcodeHeader(BitstreamCursor &Stream) { |
| 3717 | // Sniff for the signature. |
| 3718 | if (Stream.Read(8) != 'B' || |
| 3719 | Stream.Read(8) != 'C' || |
| 3720 | Stream.Read(4) != 0x0 || |
| 3721 | Stream.Read(4) != 0xC || |
| 3722 | Stream.Read(4) != 0xE || |
| 3723 | Stream.Read(4) != 0xD) |
| 3724 | return false; |
| 3725 | return true; |
| 3726 | } |
| 3727 | |
Rafael Espindola | 1aabf98 | 2015-06-16 23:29:49 +0000 | [diff] [blame] | 3728 | std::error_code |
| 3729 | BitcodeReader::parseBitcodeInto(std::unique_ptr<DataStreamer> Streamer, |
| 3730 | Module *M, bool ShouldLazyLoadMetadata) { |
Rafael Espindola | c6afe0d | 2015-06-16 20:03:39 +0000 | [diff] [blame] | 3731 | TheModule = M; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3732 | |
Rafael Espindola | 1aabf98 | 2015-06-16 23:29:49 +0000 | [diff] [blame] | 3733 | if (std::error_code EC = initStream(std::move(Streamer))) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3734 | return EC; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3735 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3736 | // Sniff for the signature. |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 3737 | if (!hasValidBitcodeHeader(Stream)) |
| 3738 | return error("Invalid bitcode signature"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3739 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3740 | // We expect a number of well-defined blocks, though we don't necessarily |
| 3741 | // need to understand them all. |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3742 | while (1) { |
Filipe Cabecinhas | 2255427 | 2015-04-14 14:07:15 +0000 | [diff] [blame] | 3743 | if (Stream.AtEndOfStream()) { |
Filipe Cabecinhas | 2255427 | 2015-04-14 14:07:15 +0000 | [diff] [blame] | 3744 | // We didn't really read a proper Module. |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3745 | return error("Malformed IR file"); |
Filipe Cabecinhas | 2255427 | 2015-04-14 14:07:15 +0000 | [diff] [blame] | 3746 | } |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3747 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3748 | BitstreamEntry Entry = |
| 3749 | Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3750 | |
Rafael Espindola | c6afe0d | 2015-06-16 20:03:39 +0000 | [diff] [blame] | 3751 | if (Entry.Kind != BitstreamEntry::SubBlock) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3752 | return error("Malformed block"); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3753 | |
Mehdi Amini | 5d30328 | 2015-10-26 18:37:00 +0000 | [diff] [blame] | 3754 | if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) { |
| 3755 | parseBitcodeVersion(); |
| 3756 | continue; |
| 3757 | } |
| 3758 | |
Rafael Espindola | c6afe0d | 2015-06-16 20:03:39 +0000 | [diff] [blame] | 3759 | if (Entry.ID == bitc::MODULE_BLOCK_ID) |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 3760 | return parseModule(0, ShouldLazyLoadMetadata); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3761 | |
Rafael Espindola | c6afe0d | 2015-06-16 20:03:39 +0000 | [diff] [blame] | 3762 | if (Stream.SkipBlock()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3763 | return error("Invalid record"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3764 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3765 | } |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 3766 | |
Rafael Espindola | c75c4fa | 2014-07-04 20:02:42 +0000 | [diff] [blame] | 3767 | ErrorOr<std::string> BitcodeReader::parseModuleTriple() { |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3768 | if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3769 | return error("Invalid record"); |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3770 | |
| 3771 | SmallVector<uint64_t, 64> Record; |
| 3772 | |
Rafael Espindola | c75c4fa | 2014-07-04 20:02:42 +0000 | [diff] [blame] | 3773 | std::string Triple; |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3774 | // Read all the records for this module. |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3775 | while (1) { |
| 3776 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3777 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3778 | switch (Entry.Kind) { |
| 3779 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 3780 | case BitstreamEntry::Error: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3781 | return error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3782 | case BitstreamEntry::EndBlock: |
Rafael Espindola | e610779 | 2014-07-04 20:05:56 +0000 | [diff] [blame] | 3783 | return Triple; |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3784 | case BitstreamEntry::Record: |
| 3785 | // The interesting case. |
| 3786 | break; |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3787 | } |
| 3788 | |
| 3789 | // Read a record. |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3790 | switch (Stream.readRecord(Entry.ID, Record)) { |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3791 | default: break; // Default behavior, ignore unknown content. |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3792 | case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] |
Rafael Espindola | c75c4fa | 2014-07-04 20:02:42 +0000 | [diff] [blame] | 3793 | std::string S; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3794 | if (convertToString(Record, 0, S)) |
| 3795 | return error("Invalid record"); |
Rafael Espindola | c75c4fa | 2014-07-04 20:02:42 +0000 | [diff] [blame] | 3796 | Triple = S; |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3797 | break; |
| 3798 | } |
| 3799 | } |
| 3800 | Record.clear(); |
| 3801 | } |
Rafael Espindola | e610779 | 2014-07-04 20:05:56 +0000 | [diff] [blame] | 3802 | llvm_unreachable("Exit infinite loop"); |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3803 | } |
| 3804 | |
Rafael Espindola | c75c4fa | 2014-07-04 20:02:42 +0000 | [diff] [blame] | 3805 | ErrorOr<std::string> BitcodeReader::parseTriple() { |
Rafael Espindola | 1aabf98 | 2015-06-16 23:29:49 +0000 | [diff] [blame] | 3806 | if (std::error_code EC = initStream(nullptr)) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3807 | return EC; |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3808 | |
| 3809 | // Sniff for the signature. |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 3810 | if (!hasValidBitcodeHeader(Stream)) |
| 3811 | return error("Invalid bitcode signature"); |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3812 | |
| 3813 | // We expect a number of well-defined blocks, though we don't necessarily |
| 3814 | // need to understand them all. |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3815 | while (1) { |
| 3816 | BitstreamEntry Entry = Stream.advance(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3817 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3818 | switch (Entry.Kind) { |
| 3819 | case BitstreamEntry::Error: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3820 | return error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3821 | case BitstreamEntry::EndBlock: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3822 | return std::error_code(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3823 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3824 | case BitstreamEntry::SubBlock: |
| 3825 | if (Entry.ID == bitc::MODULE_BLOCK_ID) |
Rafael Espindola | d346cc8 | 2014-07-04 13:52:01 +0000 | [diff] [blame] | 3826 | return parseModuleTriple(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3827 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3828 | // Ignore other sub-blocks. |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3829 | if (Stream.SkipBlock()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3830 | return error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3831 | continue; |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3832 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3833 | case BitstreamEntry::Record: |
| 3834 | Stream.skipRecord(Entry.ID); |
| 3835 | continue; |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3836 | } |
| 3837 | } |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3838 | } |
| 3839 | |
Mehdi Amini | 3383ccc | 2015-11-09 02:46:41 +0000 | [diff] [blame] | 3840 | ErrorOr<std::string> BitcodeReader::parseIdentificationBlock() { |
| 3841 | if (std::error_code EC = initStream(nullptr)) |
| 3842 | return EC; |
| 3843 | |
| 3844 | // Sniff for the signature. |
| 3845 | if (!hasValidBitcodeHeader(Stream)) |
| 3846 | return error("Invalid bitcode signature"); |
| 3847 | |
| 3848 | // We expect a number of well-defined blocks, though we don't necessarily |
| 3849 | // need to understand them all. |
| 3850 | while (1) { |
| 3851 | BitstreamEntry Entry = Stream.advance(); |
| 3852 | switch (Entry.Kind) { |
| 3853 | case BitstreamEntry::Error: |
| 3854 | return error("Malformed block"); |
| 3855 | case BitstreamEntry::EndBlock: |
| 3856 | return std::error_code(); |
| 3857 | |
| 3858 | case BitstreamEntry::SubBlock: |
| 3859 | if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) { |
| 3860 | if (std::error_code EC = parseBitcodeVersion()) |
| 3861 | return EC; |
| 3862 | return ProducerIdentification; |
| 3863 | } |
| 3864 | // Ignore other sub-blocks. |
| 3865 | if (Stream.SkipBlock()) |
| 3866 | return error("Malformed block"); |
| 3867 | continue; |
| 3868 | case BitstreamEntry::Record: |
| 3869 | Stream.skipRecord(Entry.ID); |
| 3870 | continue; |
| 3871 | } |
| 3872 | } |
| 3873 | } |
| 3874 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3875 | /// Parse metadata attachments. |
| 3876 | std::error_code BitcodeReader::parseMetadataAttachment(Function &F) { |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3877 | if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3878 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3879 | |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3880 | SmallVector<uint64_t, 64> Record; |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3881 | while (1) { |
| 3882 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3883 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3884 | switch (Entry.Kind) { |
| 3885 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 3886 | case BitstreamEntry::Error: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3887 | return error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3888 | case BitstreamEntry::EndBlock: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3889 | return std::error_code(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3890 | case BitstreamEntry::Record: |
| 3891 | // The interesting case. |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3892 | break; |
| 3893 | } |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3894 | |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3895 | // Read a metadata attachment record. |
| 3896 | Record.clear(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3897 | switch (Stream.readRecord(Entry.ID, Record)) { |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3898 | default: // Default behavior: ignore. |
| 3899 | break; |
Chris Lattner | b877855 | 2011-06-17 17:50:30 +0000 | [diff] [blame] | 3900 | case bitc::METADATA_ATTACHMENT: { |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3901 | unsigned RecordLength = Record.size(); |
Duncan P. N. Exon Smith | 3d4cd75 | 2015-04-24 22:04:41 +0000 | [diff] [blame] | 3902 | if (Record.empty()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3903 | return error("Invalid record"); |
Duncan P. N. Exon Smith | 3d4cd75 | 2015-04-24 22:04:41 +0000 | [diff] [blame] | 3904 | if (RecordLength % 2 == 0) { |
| 3905 | // A function attachment. |
| 3906 | for (unsigned I = 0; I != RecordLength; I += 2) { |
| 3907 | auto K = MDKindMap.find(Record[I]); |
| 3908 | if (K == MDKindMap.end()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3909 | return error("Invalid ID"); |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 3910 | Metadata *MD = MetadataList.getValueFwdRef(Record[I + 1]); |
Duncan P. N. Exon Smith | 3d4cd75 | 2015-04-24 22:04:41 +0000 | [diff] [blame] | 3911 | F.setMetadata(K->second, cast<MDNode>(MD)); |
| 3912 | } |
| 3913 | continue; |
| 3914 | } |
| 3915 | |
| 3916 | // An instruction attachment. |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3917 | Instruction *Inst = InstructionList[Record[0]]; |
| 3918 | for (unsigned i = 1; i != RecordLength; i = i+2) { |
Devang Patel | b1a4477 | 2009-09-28 21:14:55 +0000 | [diff] [blame] | 3919 | unsigned Kind = Record[i]; |
Dan Gohman | 43aa8f0 | 2010-07-20 21:42:28 +0000 | [diff] [blame] | 3920 | DenseMap<unsigned, unsigned>::iterator I = |
| 3921 | MDKindMap.find(Kind); |
| 3922 | if (I == MDKindMap.end()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3923 | return error("Invalid ID"); |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 3924 | Metadata *Node = MetadataList.getValueFwdRef(Record[i + 1]); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 3925 | if (isa<LocalAsMetadata>(Node)) |
Duncan P. N. Exon Smith | 35303fd | 2014-12-06 02:29:44 +0000 | [diff] [blame] | 3926 | // Drop the attachment. This used to be legal, but there's no |
| 3927 | // upgrade path. |
| 3928 | break; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 3929 | Inst->setMetadata(I->second, cast<MDNode>(Node)); |
Manman Ren | 209b17c | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 3930 | if (I->second == LLVMContext::MD_tbaa) |
| 3931 | InstsWithTBAATag.push_back(Inst); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3932 | } |
| 3933 | break; |
| 3934 | } |
| 3935 | } |
| 3936 | } |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3937 | } |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 3938 | |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 3939 | static std::error_code typeCheckLoadStoreInst(Type *ValType, Type *PtrType) { |
| 3940 | LLVMContext &Context = PtrType->getContext(); |
Filipe Cabecinhas | 11bb849 | 2015-05-18 21:48:55 +0000 | [diff] [blame] | 3941 | if (!isa<PointerType>(PtrType)) |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 3942 | return error(Context, "Load/Store operand is not a pointer type"); |
Filipe Cabecinhas | 11bb849 | 2015-05-18 21:48:55 +0000 | [diff] [blame] | 3943 | Type *ElemType = cast<PointerType>(PtrType)->getElementType(); |
| 3944 | |
| 3945 | if (ValType && ValType != ElemType) |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 3946 | return error(Context, "Explicit load/store type does not match pointee " |
| 3947 | "type of pointer operand"); |
Filipe Cabecinhas | 11bb849 | 2015-05-18 21:48:55 +0000 | [diff] [blame] | 3948 | if (!PointerType::isLoadableOrStorableType(ElemType)) |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 3949 | return error(Context, "Cannot load/store from pointer"); |
Filipe Cabecinhas | 11bb849 | 2015-05-18 21:48:55 +0000 | [diff] [blame] | 3950 | return std::error_code(); |
| 3951 | } |
| 3952 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3953 | /// Lazily parse the specified function body block. |
| 3954 | std::error_code BitcodeReader::parseFunctionBody(Function *F) { |
Chris Lattner | 982ec1e | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 3955 | if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3956 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3957 | |
Nick Lewycky | a72e1af | 2010-02-25 08:30:17 +0000 | [diff] [blame] | 3958 | InstructionList.clear(); |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3959 | unsigned ModuleValueListSize = ValueList.size(); |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 3960 | unsigned ModuleMetadataListSize = MetadataList.size(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3961 | |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3962 | // Add all the function arguments to the value table. |
Duncan P. N. Exon Smith | fb1743a3 | 2015-10-13 16:48:55 +0000 | [diff] [blame] | 3963 | for (Argument &I : F->args()) |
| 3964 | ValueList.push_back(&I); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3965 | |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 3966 | unsigned NextValueNo = ValueList.size(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3967 | BasicBlock *CurBB = nullptr; |
Chris Lattner | e53603e | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 3968 | unsigned CurBBNo = 0; |
| 3969 | |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 3970 | DebugLoc LastLoc; |
Duncan P. N. Exon Smith | 52d0f16 | 2015-01-09 02:51:45 +0000 | [diff] [blame] | 3971 | auto getLastInstruction = [&]() -> Instruction * { |
| 3972 | if (CurBB && !CurBB->empty()) |
| 3973 | return &CurBB->back(); |
| 3974 | else if (CurBBNo && FunctionBBs[CurBBNo - 1] && |
| 3975 | !FunctionBBs[CurBBNo - 1]->empty()) |
| 3976 | return &FunctionBBs[CurBBNo - 1]->back(); |
| 3977 | return nullptr; |
| 3978 | }; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3979 | |
Sanjoy Das | b513a9f | 2015-09-24 23:34:52 +0000 | [diff] [blame] | 3980 | std::vector<OperandBundleDef> OperandBundles; |
| 3981 | |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3982 | // Read all the records. |
| 3983 | SmallVector<uint64_t, 64> Record; |
| 3984 | while (1) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3985 | BitstreamEntry Entry = Stream.advance(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3986 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3987 | switch (Entry.Kind) { |
| 3988 | case BitstreamEntry::Error: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3989 | return error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3990 | case BitstreamEntry::EndBlock: |
| 3991 | goto OutOfRecordLoop; |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3992 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3993 | case BitstreamEntry::SubBlock: |
| 3994 | switch (Entry.ID) { |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3995 | default: // Skip unknown content. |
| 3996 | if (Stream.SkipBlock()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3997 | return error("Invalid record"); |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3998 | break; |
| 3999 | case bitc::CONSTANTS_BLOCK_ID: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4000 | if (std::error_code EC = parseConstants()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 4001 | return EC; |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4002 | NextValueNo = ValueList.size(); |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 4003 | break; |
| 4004 | case bitc::VALUE_SYMTAB_BLOCK_ID: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4005 | if (std::error_code EC = parseValueSymbolTable()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 4006 | return EC; |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 4007 | break; |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4008 | case bitc::METADATA_ATTACHMENT_ID: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4009 | if (std::error_code EC = parseMetadataAttachment(*F)) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 4010 | return EC; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4011 | break; |
Victor Hernandez | 108d3ac | 2010-01-13 19:34:08 +0000 | [diff] [blame] | 4012 | case bitc::METADATA_BLOCK_ID: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4013 | if (std::error_code EC = parseMetadata()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 4014 | return EC; |
Victor Hernandez | 108d3ac | 2010-01-13 19:34:08 +0000 | [diff] [blame] | 4015 | break; |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 4016 | case bitc::USELIST_BLOCK_ID: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4017 | if (std::error_code EC = parseUseLists()) |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 4018 | return EC; |
| 4019 | break; |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 4020 | } |
| 4021 | continue; |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 4022 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 4023 | case BitstreamEntry::Record: |
| 4024 | // The interesting case. |
| 4025 | break; |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 4026 | } |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 4027 | |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 4028 | // Read a record. |
| 4029 | Record.clear(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4030 | Instruction *I = nullptr; |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 4031 | unsigned BitCode = Stream.readRecord(Entry.ID, Record); |
Dan Gohman | 0ebd696 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 4032 | switch (BitCode) { |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4033 | default: // Default behavior: reject |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4034 | return error("Invalid value"); |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 4035 | case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks] |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4036 | if (Record.size() < 1 || Record[0] == 0) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4037 | return error("Invalid record"); |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 4038 | // Create all the basic blocks for the function. |
Chris Lattner | 6ce15cb | 2007-05-03 22:09:51 +0000 | [diff] [blame] | 4039 | FunctionBBs.resize(Record[0]); |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 4040 | |
| 4041 | // See if anything took the address of blocks in this function. |
| 4042 | auto BBFRI = BasicBlockFwdRefs.find(F); |
| 4043 | if (BBFRI == BasicBlockFwdRefs.end()) { |
| 4044 | for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i) |
| 4045 | FunctionBBs[i] = BasicBlock::Create(Context, "", F); |
| 4046 | } else { |
| 4047 | auto &BBRefs = BBFRI->second; |
Duncan P. N. Exon Smith | 5a5fd7b | 2014-08-16 01:54:37 +0000 | [diff] [blame] | 4048 | // Check for invalid basic block references. |
| 4049 | if (BBRefs.size() > FunctionBBs.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4050 | return error("Invalid ID"); |
Duncan P. N. Exon Smith | 5a5fd7b | 2014-08-16 01:54:37 +0000 | [diff] [blame] | 4051 | assert(!BBRefs.empty() && "Unexpected empty array"); |
| 4052 | assert(!BBRefs.front() && "Invalid reference to entry block"); |
| 4053 | for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E; |
| 4054 | ++I) |
| 4055 | if (I < RE && BBRefs[I]) { |
| 4056 | BBRefs[I]->insertInto(F); |
| 4057 | FunctionBBs[I] = BBRefs[I]; |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 4058 | } else { |
| 4059 | FunctionBBs[I] = BasicBlock::Create(Context, "", F); |
| 4060 | } |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 4061 | |
| 4062 | // Erase from the table. |
| 4063 | BasicBlockFwdRefs.erase(BBFRI); |
| 4064 | } |
| 4065 | |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4066 | CurBB = FunctionBBs[0]; |
| 4067 | continue; |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 4068 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4069 | |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 4070 | case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN |
| 4071 | // This record indicates that the last instruction is at the same |
| 4072 | // location as the previous instruction with a location. |
Duncan P. N. Exon Smith | 52d0f16 | 2015-01-09 02:51:45 +0000 | [diff] [blame] | 4073 | I = getLastInstruction(); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4074 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4075 | if (!I) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4076 | return error("Invalid record"); |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 4077 | I->setDebugLoc(LastLoc); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4078 | I = nullptr; |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 4079 | continue; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4080 | |
Duncan P. N. Exon Smith | 9ed1966 | 2015-01-09 17:53:27 +0000 | [diff] [blame] | 4081 | case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia] |
Duncan P. N. Exon Smith | 52d0f16 | 2015-01-09 02:51:45 +0000 | [diff] [blame] | 4082 | I = getLastInstruction(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4083 | if (!I || Record.size() < 4) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4084 | return error("Invalid record"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4085 | |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 4086 | unsigned Line = Record[0], Col = Record[1]; |
| 4087 | unsigned ScopeID = Record[2], IAID = Record[3]; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4088 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4089 | MDNode *Scope = nullptr, *IA = nullptr; |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 4090 | if (ScopeID) |
| 4091 | Scope = cast<MDNode>(MetadataList.getValueFwdRef(ScopeID - 1)); |
| 4092 | if (IAID) |
| 4093 | IA = cast<MDNode>(MetadataList.getValueFwdRef(IAID - 1)); |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 4094 | LastLoc = DebugLoc::get(Line, Col, Scope, IA); |
| 4095 | I->setDebugLoc(LastLoc); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4096 | I = nullptr; |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 4097 | continue; |
| 4098 | } |
| 4099 | |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 4100 | case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode] |
| 4101 | unsigned OpNum = 0; |
| 4102 | Value *LHS, *RHS; |
| 4103 | if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 4104 | popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) || |
Dan Gohman | 0ebd696 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 4105 | OpNum+1 > Record.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4106 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4107 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4108 | int Opc = getDecodedBinaryOpcode(Record[OpNum++], LHS->getType()); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 4109 | if (Opc == -1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4110 | return error("Invalid record"); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4111 | I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4112 | InstructionList.push_back(I); |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 4113 | if (OpNum < Record.size()) { |
| 4114 | if (Opc == Instruction::Add || |
| 4115 | Opc == Instruction::Sub || |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 4116 | Opc == Instruction::Mul || |
| 4117 | Opc == Instruction::Shl) { |
Dan Gohman | 00f4747 | 2010-01-25 21:55:39 +0000 | [diff] [blame] | 4118 | if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP)) |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 4119 | cast<BinaryOperator>(I)->setHasNoSignedWrap(true); |
Dan Gohman | 00f4747 | 2010-01-25 21:55:39 +0000 | [diff] [blame] | 4120 | if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 4121 | cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true); |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 4122 | } else if (Opc == Instruction::SDiv || |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 4123 | Opc == Instruction::UDiv || |
| 4124 | Opc == Instruction::LShr || |
| 4125 | Opc == Instruction::AShr) { |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 4126 | if (Record[OpNum] & (1 << bitc::PEO_EXACT)) |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 4127 | cast<BinaryOperator>(I)->setIsExact(true); |
Michael Ilseman | 9978d7e | 2012-11-27 00:43:38 +0000 | [diff] [blame] | 4128 | } else if (isa<FPMathOperator>(I)) { |
James Molloy | 88eb535 | 2015-07-10 12:52:00 +0000 | [diff] [blame] | 4129 | FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]); |
Michael Ilseman | 9978d7e | 2012-11-27 00:43:38 +0000 | [diff] [blame] | 4130 | if (FMF.any()) |
| 4131 | I->setFastMathFlags(FMF); |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 4132 | } |
Michael Ilseman | 9978d7e | 2012-11-27 00:43:38 +0000 | [diff] [blame] | 4133 | |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 4134 | } |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 4135 | break; |
| 4136 | } |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 4137 | case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc] |
| 4138 | unsigned OpNum = 0; |
| 4139 | Value *Op; |
| 4140 | if (getValueTypePair(Record, OpNum, NextValueNo, Op) || |
| 4141 | OpNum+2 != Record.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4142 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4143 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 4144 | Type *ResTy = getTypeByID(Record[OpNum]); |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4145 | int Opc = getDecodedCastOpcode(Record[OpNum + 1]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4146 | if (Opc == -1 || !ResTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4147 | return error("Invalid record"); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4148 | Instruction *Temp = nullptr; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 4149 | if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) { |
| 4150 | if (Temp) { |
| 4151 | InstructionList.push_back(Temp); |
| 4152 | CurBB->getInstList().push_back(Temp); |
| 4153 | } |
| 4154 | } else { |
Filipe Cabecinhas | b70fd87 | 2015-10-06 12:37:54 +0000 | [diff] [blame] | 4155 | auto CastOp = (Instruction::CastOps)Opc; |
| 4156 | if (!CastInst::castIsValid(CastOp, Op, ResTy)) |
| 4157 | return error("Invalid cast"); |
| 4158 | I = CastInst::Create(CastOp, Op, ResTy); |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 4159 | } |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4160 | InstructionList.push_back(I); |
Chris Lattner | e53603e | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 4161 | break; |
| 4162 | } |
David Blaikie | b5b5efd | 2015-02-25 01:08:52 +0000 | [diff] [blame] | 4163 | case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD: |
| 4164 | case bitc::FUNC_CODE_INST_GEP_OLD: |
| 4165 | case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands] |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 4166 | unsigned OpNum = 0; |
David Blaikie | b5b5efd | 2015-02-25 01:08:52 +0000 | [diff] [blame] | 4167 | |
| 4168 | Type *Ty; |
| 4169 | bool InBounds; |
| 4170 | |
| 4171 | if (BitCode == bitc::FUNC_CODE_INST_GEP) { |
| 4172 | InBounds = Record[OpNum++]; |
| 4173 | Ty = getTypeByID(Record[OpNum++]); |
| 4174 | } else { |
| 4175 | InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD; |
| 4176 | Ty = nullptr; |
| 4177 | } |
| 4178 | |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 4179 | Value *BasePtr; |
| 4180 | if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4181 | return error("Invalid record"); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 4182 | |
David Blaikie | 60310f2 | 2015-05-08 00:42:26 +0000 | [diff] [blame] | 4183 | if (!Ty) |
| 4184 | Ty = cast<SequentialType>(BasePtr->getType()->getScalarType()) |
| 4185 | ->getElementType(); |
| 4186 | else if (Ty != |
| 4187 | cast<SequentialType>(BasePtr->getType()->getScalarType()) |
| 4188 | ->getElementType()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4189 | return error( |
David Blaikie | 675e8cb | 2015-03-16 21:35:48 +0000 | [diff] [blame] | 4190 | "Explicit gep type does not match pointee type of pointer operand"); |
| 4191 | |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 4192 | SmallVector<Value*, 16> GEPIdx; |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 4193 | while (OpNum != Record.size()) { |
| 4194 | Value *Op; |
| 4195 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4196 | return error("Invalid record"); |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 4197 | GEPIdx.push_back(Op); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 4198 | } |
| 4199 | |
David Blaikie | 096b1da | 2015-03-14 19:53:33 +0000 | [diff] [blame] | 4200 | I = GetElementPtrInst::Create(Ty, BasePtr, GEPIdx); |
David Blaikie | 675e8cb | 2015-03-16 21:35:48 +0000 | [diff] [blame] | 4201 | |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4202 | InstructionList.push_back(I); |
David Blaikie | b5b5efd | 2015-02-25 01:08:52 +0000 | [diff] [blame] | 4203 | if (InBounds) |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 4204 | cast<GetElementPtrInst>(I)->setIsInBounds(true); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 4205 | break; |
| 4206 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4207 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 4208 | case bitc::FUNC_CODE_INST_EXTRACTVAL: { |
| 4209 | // EXTRACTVAL: [opty, opval, n x indices] |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 4210 | unsigned OpNum = 0; |
| 4211 | Value *Agg; |
| 4212 | if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4213 | return error("Invalid record"); |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 4214 | |
Filipe Cabecinhas | 1c299d0 | 2015-05-16 00:33:12 +0000 | [diff] [blame] | 4215 | unsigned RecSize = Record.size(); |
| 4216 | if (OpNum == RecSize) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4217 | return error("EXTRACTVAL: Invalid instruction with 0 indices"); |
Filipe Cabecinhas | 1c299d0 | 2015-05-16 00:33:12 +0000 | [diff] [blame] | 4218 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 4219 | SmallVector<unsigned, 4> EXTRACTVALIdx; |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 4220 | Type *CurTy = Agg->getType(); |
Filipe Cabecinhas | 1c299d0 | 2015-05-16 00:33:12 +0000 | [diff] [blame] | 4221 | for (; OpNum != RecSize; ++OpNum) { |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 4222 | bool IsArray = CurTy->isArrayTy(); |
| 4223 | bool IsStruct = CurTy->isStructTy(); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 4224 | uint64_t Index = Record[OpNum]; |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 4225 | |
| 4226 | if (!IsStruct && !IsArray) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4227 | return error("EXTRACTVAL: Invalid type"); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 4228 | if ((unsigned)Index != Index) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4229 | return error("Invalid value"); |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 4230 | if (IsStruct && Index >= CurTy->subtypes().size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4231 | return error("EXTRACTVAL: Invalid struct index"); |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 4232 | if (IsArray && Index >= CurTy->getArrayNumElements()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4233 | return error("EXTRACTVAL: Invalid array index"); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 4234 | EXTRACTVALIdx.push_back((unsigned)Index); |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 4235 | |
| 4236 | if (IsStruct) |
| 4237 | CurTy = CurTy->subtypes()[Index]; |
| 4238 | else |
| 4239 | CurTy = CurTy->subtypes()[0]; |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 4240 | } |
| 4241 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 4242 | I = ExtractValueInst::Create(Agg, EXTRACTVALIdx); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4243 | InstructionList.push_back(I); |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 4244 | break; |
| 4245 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4246 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 4247 | case bitc::FUNC_CODE_INST_INSERTVAL: { |
| 4248 | // INSERTVAL: [opty, opval, opty, opval, n x indices] |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 4249 | unsigned OpNum = 0; |
| 4250 | Value *Agg; |
| 4251 | if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4252 | return error("Invalid record"); |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 4253 | Value *Val; |
| 4254 | if (getValueTypePair(Record, OpNum, NextValueNo, Val)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4255 | return error("Invalid record"); |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 4256 | |
Filipe Cabecinhas | 1c299d0 | 2015-05-16 00:33:12 +0000 | [diff] [blame] | 4257 | unsigned RecSize = Record.size(); |
| 4258 | if (OpNum == RecSize) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4259 | return error("INSERTVAL: Invalid instruction with 0 indices"); |
Filipe Cabecinhas | 1c299d0 | 2015-05-16 00:33:12 +0000 | [diff] [blame] | 4260 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 4261 | SmallVector<unsigned, 4> INSERTVALIdx; |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 4262 | Type *CurTy = Agg->getType(); |
Filipe Cabecinhas | 1c299d0 | 2015-05-16 00:33:12 +0000 | [diff] [blame] | 4263 | for (; OpNum != RecSize; ++OpNum) { |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 4264 | bool IsArray = CurTy->isArrayTy(); |
| 4265 | bool IsStruct = CurTy->isStructTy(); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 4266 | uint64_t Index = Record[OpNum]; |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 4267 | |
| 4268 | if (!IsStruct && !IsArray) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4269 | return error("INSERTVAL: Invalid type"); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 4270 | if ((unsigned)Index != Index) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4271 | return error("Invalid value"); |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 4272 | if (IsStruct && Index >= CurTy->subtypes().size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4273 | return error("INSERTVAL: Invalid struct index"); |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 4274 | if (IsArray && Index >= CurTy->getArrayNumElements()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4275 | return error("INSERTVAL: Invalid array index"); |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 4276 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 4277 | INSERTVALIdx.push_back((unsigned)Index); |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 4278 | if (IsStruct) |
| 4279 | CurTy = CurTy->subtypes()[Index]; |
| 4280 | else |
| 4281 | CurTy = CurTy->subtypes()[0]; |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 4282 | } |
| 4283 | |
Filipe Cabecinhas | 4708a02 | 2015-05-18 22:27:11 +0000 | [diff] [blame] | 4284 | if (CurTy != Val->getType()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4285 | return error("Inserted value type doesn't match aggregate type"); |
Filipe Cabecinhas | 4708a02 | 2015-05-18 22:27:11 +0000 | [diff] [blame] | 4286 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 4287 | I = InsertValueInst::Create(Agg, Val, INSERTVALIdx); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4288 | InstructionList.push_back(I); |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 4289 | break; |
| 4290 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4291 | |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 4292 | case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval] |
Dan Gohman | c5d2892 | 2008-09-16 01:01:33 +0000 | [diff] [blame] | 4293 | // obsolete form of select |
| 4294 | // handles select i1 ... in old bitcode |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 4295 | unsigned OpNum = 0; |
| 4296 | Value *TrueVal, *FalseVal, *Cond; |
| 4297 | if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 4298 | popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) || |
| 4299 | popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4300 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4301 | |
Dan Gohman | c5d2892 | 2008-09-16 01:01:33 +0000 | [diff] [blame] | 4302 | I = SelectInst::Create(Cond, TrueVal, FalseVal); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4303 | InstructionList.push_back(I); |
Dan Gohman | c5d2892 | 2008-09-16 01:01:33 +0000 | [diff] [blame] | 4304 | break; |
| 4305 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4306 | |
Dan Gohman | c5d2892 | 2008-09-16 01:01:33 +0000 | [diff] [blame] | 4307 | case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred] |
| 4308 | // new form of select |
| 4309 | // handles select i1 or select [N x i1] |
| 4310 | unsigned OpNum = 0; |
| 4311 | Value *TrueVal, *FalseVal, *Cond; |
| 4312 | if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 4313 | popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) || |
Dan Gohman | c5d2892 | 2008-09-16 01:01:33 +0000 | [diff] [blame] | 4314 | getValueTypePair(Record, OpNum, NextValueNo, Cond)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4315 | return error("Invalid record"); |
Dan Gohman | c579d97 | 2008-09-09 01:02:47 +0000 | [diff] [blame] | 4316 | |
| 4317 | // select condition can be either i1 or [N x i1] |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 4318 | if (VectorType* vector_type = |
| 4319 | dyn_cast<VectorType>(Cond->getType())) { |
Dan Gohman | c579d97 | 2008-09-09 01:02:47 +0000 | [diff] [blame] | 4320 | // expect <n x i1> |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4321 | if (vector_type->getElementType() != Type::getInt1Ty(Context)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4322 | return error("Invalid type for value"); |
Dan Gohman | c579d97 | 2008-09-09 01:02:47 +0000 | [diff] [blame] | 4323 | } else { |
| 4324 | // expect i1 |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4325 | if (Cond->getType() != Type::getInt1Ty(Context)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4326 | return error("Invalid type for value"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4327 | } |
| 4328 | |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 4329 | I = SelectInst::Create(Cond, TrueVal, FalseVal); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4330 | InstructionList.push_back(I); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 4331 | break; |
| 4332 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4333 | |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 4334 | case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval] |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 4335 | unsigned OpNum = 0; |
| 4336 | Value *Vec, *Idx; |
| 4337 | if (getValueTypePair(Record, OpNum, NextValueNo, Vec) || |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 4338 | getValueTypePair(Record, OpNum, NextValueNo, Idx)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4339 | return error("Invalid record"); |
Filipe Cabecinhas | ff1e234 | 2015-04-24 11:30:15 +0000 | [diff] [blame] | 4340 | if (!Vec->getType()->isVectorTy()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4341 | return error("Invalid type for value"); |
Eric Christopher | c974225 | 2009-07-25 02:28:41 +0000 | [diff] [blame] | 4342 | I = ExtractElementInst::Create(Vec, Idx); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4343 | InstructionList.push_back(I); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 4344 | break; |
| 4345 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4346 | |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 4347 | case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval] |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 4348 | unsigned OpNum = 0; |
| 4349 | Value *Vec, *Elt, *Idx; |
Filipe Cabecinhas | ff1e234 | 2015-04-24 11:30:15 +0000 | [diff] [blame] | 4350 | if (getValueTypePair(Record, OpNum, NextValueNo, Vec)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4351 | return error("Invalid record"); |
Filipe Cabecinhas | ff1e234 | 2015-04-24 11:30:15 +0000 | [diff] [blame] | 4352 | if (!Vec->getType()->isVectorTy()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4353 | return error("Invalid type for value"); |
Filipe Cabecinhas | ff1e234 | 2015-04-24 11:30:15 +0000 | [diff] [blame] | 4354 | if (popValue(Record, OpNum, NextValueNo, |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 4355 | cast<VectorType>(Vec->getType())->getElementType(), Elt) || |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 4356 | getValueTypePair(Record, OpNum, NextValueNo, Idx)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4357 | return error("Invalid record"); |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 4358 | I = InsertElementInst::Create(Vec, Elt, Idx); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4359 | InstructionList.push_back(I); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 4360 | break; |
| 4361 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4362 | |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 4363 | case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval] |
| 4364 | unsigned OpNum = 0; |
| 4365 | Value *Vec1, *Vec2, *Mask; |
| 4366 | if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 4367 | popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4368 | return error("Invalid record"); |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 4369 | |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 4370 | if (getValueTypePair(Record, OpNum, NextValueNo, Mask)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4371 | return error("Invalid record"); |
Filipe Cabecinhas | ff1e234 | 2015-04-24 11:30:15 +0000 | [diff] [blame] | 4372 | if (!Vec1->getType()->isVectorTy() || !Vec2->getType()->isVectorTy()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4373 | return error("Invalid type for value"); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 4374 | I = new ShuffleVectorInst(Vec1, Vec2, Mask); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4375 | InstructionList.push_back(I); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 4376 | break; |
| 4377 | } |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 4378 | |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 4379 | case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred] |
| 4380 | // Old form of ICmp/FCmp returning bool |
| 4381 | // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were |
| 4382 | // both legal on vectors but had different behaviour. |
| 4383 | case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred] |
| 4384 | // FCmp/ICmp returning bool or vector of bool |
| 4385 | |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 4386 | unsigned OpNum = 0; |
| 4387 | Value *LHS, *RHS; |
| 4388 | if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || |
James Molloy | 88eb535 | 2015-07-10 12:52:00 +0000 | [diff] [blame] | 4389 | popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS)) |
| 4390 | return error("Invalid record"); |
| 4391 | |
| 4392 | unsigned PredVal = Record[OpNum]; |
| 4393 | bool IsFP = LHS->getType()->isFPOrFPVectorTy(); |
| 4394 | FastMathFlags FMF; |
| 4395 | if (IsFP && Record.size() > OpNum+1) |
| 4396 | FMF = getDecodedFastMathFlags(Record[++OpNum]); |
| 4397 | |
| 4398 | if (OpNum+1 != Record.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4399 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4400 | |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 4401 | if (LHS->getType()->isFPOrFPVectorTy()) |
James Molloy | 88eb535 | 2015-07-10 12:52:00 +0000 | [diff] [blame] | 4402 | I = new FCmpInst((FCmpInst::Predicate)PredVal, LHS, RHS); |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 4403 | else |
James Molloy | 88eb535 | 2015-07-10 12:52:00 +0000 | [diff] [blame] | 4404 | I = new ICmpInst((ICmpInst::Predicate)PredVal, LHS, RHS); |
| 4405 | |
| 4406 | if (FMF.any()) |
| 4407 | I->setFastMathFlags(FMF); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4408 | InstructionList.push_back(I); |
Dan Gohman | c579d97 | 2008-09-09 01:02:47 +0000 | [diff] [blame] | 4409 | break; |
| 4410 | } |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 4411 | |
Chris Lattner | e53603e | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 4412 | case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>] |
Devang Patel | bbfd874 | 2008-02-26 01:29:32 +0000 | [diff] [blame] | 4413 | { |
| 4414 | unsigned Size = Record.size(); |
| 4415 | if (Size == 0) { |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4416 | I = ReturnInst::Create(Context); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4417 | InstructionList.push_back(I); |
Devang Patel | bbfd874 | 2008-02-26 01:29:32 +0000 | [diff] [blame] | 4418 | break; |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 4419 | } |
Devang Patel | bbfd874 | 2008-02-26 01:29:32 +0000 | [diff] [blame] | 4420 | |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 4421 | unsigned OpNum = 0; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4422 | Value *Op = nullptr; |
Chris Lattner | f1c8710 | 2011-06-17 18:09:11 +0000 | [diff] [blame] | 4423 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4424 | return error("Invalid record"); |
Chris Lattner | f1c8710 | 2011-06-17 18:09:11 +0000 | [diff] [blame] | 4425 | if (OpNum != Record.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4426 | return error("Invalid record"); |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 4427 | |
Chris Lattner | f1c8710 | 2011-06-17 18:09:11 +0000 | [diff] [blame] | 4428 | I = ReturnInst::Create(Context, Op); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4429 | InstructionList.push_back(I); |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 4430 | break; |
Chris Lattner | e53603e | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 4431 | } |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 4432 | case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#] |
Chris Lattner | 6ce15cb | 2007-05-03 22:09:51 +0000 | [diff] [blame] | 4433 | if (Record.size() != 1 && Record.size() != 3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4434 | return error("Invalid record"); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 4435 | BasicBlock *TrueDest = getBasicBlock(Record[0]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4436 | if (!TrueDest) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4437 | return error("Invalid record"); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 4438 | |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4439 | if (Record.size() == 1) { |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 4440 | I = BranchInst::Create(TrueDest); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4441 | InstructionList.push_back(I); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4442 | } |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 4443 | else { |
| 4444 | BasicBlock *FalseDest = getBasicBlock(Record[1]); |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 4445 | Value *Cond = getValue(Record, 2, NextValueNo, |
| 4446 | Type::getInt1Ty(Context)); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4447 | if (!FalseDest || !Cond) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4448 | return error("Invalid record"); |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 4449 | I = BranchInst::Create(TrueDest, FalseDest, Cond); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4450 | InstructionList.push_back(I); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 4451 | } |
| 4452 | break; |
| 4453 | } |
David Majnemer | b01aa9f | 2015-08-23 19:22:31 +0000 | [diff] [blame] | 4454 | case bitc::FUNC_CODE_INST_CLEANUPRET: { // CLEANUPRET: [val] or [val,bb#] |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 4455 | if (Record.size() != 1 && Record.size() != 2) |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 4456 | return error("Invalid record"); |
| 4457 | unsigned Idx = 0; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 4458 | Value *CleanupPad = |
| 4459 | getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 4460 | if (!CleanupPad) |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 4461 | return error("Invalid record"); |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 4462 | BasicBlock *UnwindDest = nullptr; |
| 4463 | if (Record.size() == 2) { |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 4464 | UnwindDest = getBasicBlock(Record[Idx++]); |
| 4465 | if (!UnwindDest) |
| 4466 | return error("Invalid record"); |
| 4467 | } |
| 4468 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 4469 | I = CleanupReturnInst::Create(CleanupPad, UnwindDest); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 4470 | InstructionList.push_back(I); |
| 4471 | break; |
| 4472 | } |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 4473 | case bitc::FUNC_CODE_INST_CATCHRET: { // CATCHRET: [val,bb#] |
| 4474 | if (Record.size() != 2) |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 4475 | return error("Invalid record"); |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 4476 | unsigned Idx = 0; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 4477 | Value *CatchPad = |
| 4478 | getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 4479 | if (!CatchPad) |
| 4480 | return error("Invalid record"); |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 4481 | BasicBlock *BB = getBasicBlock(Record[Idx++]); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 4482 | if (!BB) |
| 4483 | return error("Invalid record"); |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 4484 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 4485 | I = CatchReturnInst::Create(CatchPad, BB); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 4486 | InstructionList.push_back(I); |
| 4487 | break; |
| 4488 | } |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 4489 | case bitc::FUNC_CODE_INST_CATCHSWITCH: { // CATCHSWITCH: [tok,num,(bb)*,bb?] |
| 4490 | // We must have, at minimum, the outer scope and the number of arguments. |
| 4491 | if (Record.size() < 2) |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 4492 | return error("Invalid record"); |
| 4493 | |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 4494 | unsigned Idx = 0; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 4495 | |
| 4496 | Value *ParentPad = |
| 4497 | getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); |
| 4498 | |
| 4499 | unsigned NumHandlers = Record[Idx++]; |
| 4500 | |
| 4501 | SmallVector<BasicBlock *, 2> Handlers; |
| 4502 | for (unsigned Op = 0; Op != NumHandlers; ++Op) { |
| 4503 | BasicBlock *BB = getBasicBlock(Record[Idx++]); |
| 4504 | if (!BB) |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 4505 | return error("Invalid record"); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 4506 | Handlers.push_back(BB); |
| 4507 | } |
| 4508 | |
| 4509 | BasicBlock *UnwindDest = nullptr; |
| 4510 | if (Idx + 1 == Record.size()) { |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 4511 | UnwindDest = getBasicBlock(Record[Idx++]); |
| 4512 | if (!UnwindDest) |
| 4513 | return error("Invalid record"); |
| 4514 | } |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 4515 | |
| 4516 | if (Record.size() != Idx) |
| 4517 | return error("Invalid record"); |
| 4518 | |
| 4519 | auto *CatchSwitch = |
| 4520 | CatchSwitchInst::Create(ParentPad, UnwindDest, NumHandlers); |
| 4521 | for (BasicBlock *Handler : Handlers) |
| 4522 | CatchSwitch->addHandler(Handler); |
| 4523 | I = CatchSwitch; |
| 4524 | InstructionList.push_back(I); |
| 4525 | break; |
| 4526 | } |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 4527 | case bitc::FUNC_CODE_INST_CATCHPAD: |
| 4528 | case bitc::FUNC_CODE_INST_CLEANUPPAD: { // [tok,num,(ty,val)*] |
| 4529 | // We must have, at minimum, the outer scope and the number of arguments. |
| 4530 | if (Record.size() < 2) |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 4531 | return error("Invalid record"); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 4532 | |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 4533 | unsigned Idx = 0; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 4534 | |
| 4535 | Value *ParentPad = |
| 4536 | getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); |
| 4537 | |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 4538 | unsigned NumArgOperands = Record[Idx++]; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 4539 | |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 4540 | SmallVector<Value *, 2> Args; |
| 4541 | for (unsigned Op = 0; Op != NumArgOperands; ++Op) { |
| 4542 | Value *Val; |
| 4543 | if (getValueTypePair(Record, Idx, NextValueNo, Val)) |
| 4544 | return error("Invalid record"); |
| 4545 | Args.push_back(Val); |
| 4546 | } |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 4547 | |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 4548 | if (Record.size() != Idx) |
| 4549 | return error("Invalid record"); |
| 4550 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 4551 | if (BitCode == bitc::FUNC_CODE_INST_CLEANUPPAD) |
| 4552 | I = CleanupPadInst::Create(ParentPad, Args); |
| 4553 | else |
| 4554 | I = CatchPadInst::Create(ParentPad, Args); |
Joseph Tremoulet | 9ce71f7 | 2015-09-03 09:09:43 +0000 | [diff] [blame] | 4555 | InstructionList.push_back(I); |
| 4556 | break; |
| 4557 | } |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4558 | case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...] |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4559 | // Check magic |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 4560 | if ((Record[0] >> 16) == SWITCH_INST_MAGIC) { |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 4561 | // "New" SwitchInst format with case ranges. The changes to write this |
| 4562 | // format were reverted but we still recognize bitcode that uses it. |
| 4563 | // Hopefully someday we will have support for case ranges and can use |
| 4564 | // this format again. |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4565 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 4566 | Type *OpTy = getTypeByID(Record[1]); |
| 4567 | unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth(); |
| 4568 | |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 4569 | Value *Cond = getValue(Record, 2, NextValueNo, OpTy); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 4570 | BasicBlock *Default = getBasicBlock(Record[3]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4571 | if (!OpTy || !Cond || !Default) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4572 | return error("Invalid record"); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 4573 | |
| 4574 | unsigned NumCases = Record[4]; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4575 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 4576 | SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); |
| 4577 | InstructionList.push_back(SI); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4578 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 4579 | unsigned CurIdx = 5; |
| 4580 | for (unsigned i = 0; i != NumCases; ++i) { |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 4581 | SmallVector<ConstantInt*, 1> CaseVals; |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 4582 | unsigned NumItems = Record[CurIdx++]; |
| 4583 | for (unsigned ci = 0; ci != NumItems; ++ci) { |
| 4584 | bool isSingleNumber = Record[CurIdx++]; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4585 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 4586 | APInt Low; |
| 4587 | unsigned ActiveWords = 1; |
| 4588 | if (ValueBitWidth > 64) |
| 4589 | ActiveWords = Record[CurIdx++]; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4590 | Low = readWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords), |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 4591 | ValueBitWidth); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 4592 | CurIdx += ActiveWords; |
Stepan Dyatkovskiy | e3e19cb | 2012-05-28 12:39:09 +0000 | [diff] [blame] | 4593 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 4594 | if (!isSingleNumber) { |
| 4595 | ActiveWords = 1; |
| 4596 | if (ValueBitWidth > 64) |
| 4597 | ActiveWords = Record[CurIdx++]; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4598 | APInt High = readWideAPInt( |
| 4599 | makeArrayRef(&Record[CurIdx], ActiveWords), ValueBitWidth); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 4600 | CurIdx += ActiveWords; |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 4601 | |
| 4602 | // FIXME: It is not clear whether values in the range should be |
| 4603 | // compared as signed or unsigned values. The partially |
| 4604 | // implemented changes that used this format in the past used |
| 4605 | // unsigned comparisons. |
| 4606 | for ( ; Low.ule(High); ++Low) |
| 4607 | CaseVals.push_back(ConstantInt::get(Context, Low)); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 4608 | } else |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 4609 | CaseVals.push_back(ConstantInt::get(Context, Low)); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 4610 | } |
| 4611 | BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]); |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 4612 | for (SmallVector<ConstantInt*, 1>::iterator cvi = CaseVals.begin(), |
| 4613 | cve = CaseVals.end(); cvi != cve; ++cvi) |
| 4614 | SI->addCase(*cvi, DestBB); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 4615 | } |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 4616 | I = SI; |
| 4617 | break; |
| 4618 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4619 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 4620 | // Old SwitchInst format without case ranges. |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4621 | |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 4622 | if (Record.size() < 3 || (Record.size() & 1) == 0) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4623 | return error("Invalid record"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 4624 | Type *OpTy = getTypeByID(Record[0]); |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 4625 | Value *Cond = getValue(Record, 1, NextValueNo, OpTy); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 4626 | BasicBlock *Default = getBasicBlock(Record[2]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4627 | if (!OpTy || !Cond || !Default) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4628 | return error("Invalid record"); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 4629 | unsigned NumCases = (Record.size()-3)/2; |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 4630 | SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4631 | InstructionList.push_back(SI); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 4632 | for (unsigned i = 0, e = NumCases; i != e; ++i) { |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4633 | ConstantInt *CaseVal = |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 4634 | dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy)); |
| 4635 | BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4636 | if (!CaseVal || !DestBB) { |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 4637 | delete SI; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4638 | return error("Invalid record"); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 4639 | } |
| 4640 | SI->addCase(CaseVal, DestBB); |
| 4641 | } |
| 4642 | I = SI; |
| 4643 | break; |
| 4644 | } |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 4645 | case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...] |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4646 | if (Record.size() < 2) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4647 | return error("Invalid record"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 4648 | Type *OpTy = getTypeByID(Record[0]); |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 4649 | Value *Address = getValue(Record, 1, NextValueNo, OpTy); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4650 | if (!OpTy || !Address) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4651 | return error("Invalid record"); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4652 | unsigned NumDests = Record.size()-2; |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 4653 | IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4654 | InstructionList.push_back(IBI); |
| 4655 | for (unsigned i = 0, e = NumDests; i != e; ++i) { |
| 4656 | if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) { |
| 4657 | IBI->addDestination(DestBB); |
| 4658 | } else { |
| 4659 | delete IBI; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4660 | return error("Invalid record"); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4661 | } |
| 4662 | } |
| 4663 | I = IBI; |
| 4664 | break; |
| 4665 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4666 | |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 4667 | case bitc::FUNC_CODE_INST_INVOKE: { |
| 4668 | // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...] |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 4669 | if (Record.size() < 4) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4670 | return error("Invalid record"); |
David Blaikie | 5ea1f7b | 2015-04-24 18:06:06 +0000 | [diff] [blame] | 4671 | unsigned OpNum = 0; |
| 4672 | AttributeSet PAL = getAttributes(Record[OpNum++]); |
| 4673 | unsigned CCInfo = Record[OpNum++]; |
| 4674 | BasicBlock *NormalBB = getBasicBlock(Record[OpNum++]); |
| 4675 | BasicBlock *UnwindBB = getBasicBlock(Record[OpNum++]); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4676 | |
David Blaikie | 5ea1f7b | 2015-04-24 18:06:06 +0000 | [diff] [blame] | 4677 | FunctionType *FTy = nullptr; |
| 4678 | if (CCInfo >> 13 & 1 && |
| 4679 | !(FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++])))) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4680 | return error("Explicit invoke type is not a function type"); |
David Blaikie | 5ea1f7b | 2015-04-24 18:06:06 +0000 | [diff] [blame] | 4681 | |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 4682 | Value *Callee; |
| 4683 | if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4684 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4685 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 4686 | PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType()); |
David Blaikie | 5ea1f7b | 2015-04-24 18:06:06 +0000 | [diff] [blame] | 4687 | if (!CalleeTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4688 | return error("Callee is not a pointer"); |
David Blaikie | 5ea1f7b | 2015-04-24 18:06:06 +0000 | [diff] [blame] | 4689 | if (!FTy) { |
| 4690 | FTy = dyn_cast<FunctionType>(CalleeTy->getElementType()); |
| 4691 | if (!FTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4692 | return error("Callee is not of pointer to function type"); |
David Blaikie | 5ea1f7b | 2015-04-24 18:06:06 +0000 | [diff] [blame] | 4693 | } else if (CalleeTy->getElementType() != FTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4694 | return error("Explicit invoke type does not match pointee type of " |
David Blaikie | 5ea1f7b | 2015-04-24 18:06:06 +0000 | [diff] [blame] | 4695 | "callee operand"); |
| 4696 | if (Record.size() < FTy->getNumParams() + OpNum) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4697 | return error("Insufficient operands to call"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4698 | |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 4699 | SmallVector<Value*, 16> Ops; |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 4700 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 4701 | Ops.push_back(getValue(Record, OpNum, NextValueNo, |
| 4702 | FTy->getParamType(i))); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4703 | if (!Ops.back()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4704 | return error("Invalid record"); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 4705 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4706 | |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 4707 | if (!FTy->isVarArg()) { |
| 4708 | if (Record.size() != OpNum) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4709 | return error("Invalid record"); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 4710 | } else { |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 4711 | // Read type/value pairs for varargs params. |
| 4712 | while (OpNum != Record.size()) { |
| 4713 | Value *Op; |
| 4714 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4715 | return error("Invalid record"); |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 4716 | Ops.push_back(Op); |
| 4717 | } |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 4718 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4719 | |
Sanjoy Das | b513a9f | 2015-09-24 23:34:52 +0000 | [diff] [blame] | 4720 | I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops, OperandBundles); |
| 4721 | OperandBundles.clear(); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4722 | InstructionList.push_back(I); |
Vedant Kumar | ad6d6e7 | 2015-10-27 21:17:06 +0000 | [diff] [blame] | 4723 | cast<InvokeInst>(I)->setCallingConv( |
| 4724 | static_cast<CallingConv::ID>(CallingConv::MaxID & CCInfo)); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 4725 | cast<InvokeInst>(I)->setAttributes(PAL); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 4726 | break; |
| 4727 | } |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 4728 | case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval] |
| 4729 | unsigned Idx = 0; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4730 | Value *Val = nullptr; |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 4731 | if (getValueTypePair(Record, Idx, NextValueNo, Val)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4732 | return error("Invalid record"); |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 4733 | I = ResumeInst::Create(Val); |
Bill Wendling | b9a8999 | 2011-09-01 00:50:20 +0000 | [diff] [blame] | 4734 | InstructionList.push_back(I); |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 4735 | break; |
| 4736 | } |
Chris Lattner | e53603e | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 4737 | case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4738 | I = new UnreachableInst(Context); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4739 | InstructionList.push_back(I); |
Chris Lattner | e53603e | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 4740 | break; |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 4741 | case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...] |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 4742 | if (Record.size() < 1 || ((Record.size()-1)&1)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4743 | return error("Invalid record"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 4744 | Type *Ty = getTypeByID(Record[0]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 4745 | if (!Ty) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4746 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4747 | |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 4748 | PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4749 | InstructionList.push_back(PN); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4750 | |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 4751 | for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) { |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 4752 | Value *V; |
| 4753 | // With the new function encoding, it is possible that operands have |
| 4754 | // negative IDs (for forward references). Use a signed VBR |
| 4755 | // representation to keep the encoding small. |
| 4756 | if (UseRelativeIDs) |
| 4757 | V = getValueSigned(Record, 1+i, NextValueNo, Ty); |
| 4758 | else |
| 4759 | V = getValue(Record, 1+i, NextValueNo, Ty); |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 4760 | BasicBlock *BB = getBasicBlock(Record[2+i]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 4761 | if (!V || !BB) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4762 | return error("Invalid record"); |
Chris Lattner | c332bba | 2007-05-03 18:58:09 +0000 | [diff] [blame] | 4763 | PN->addIncoming(V, BB); |
| 4764 | } |
| 4765 | I = PN; |
| 4766 | break; |
| 4767 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4768 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 4769 | case bitc::FUNC_CODE_INST_LANDINGPAD: |
| 4770 | case bitc::FUNC_CODE_INST_LANDINGPAD_OLD: { |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4771 | // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?] |
| 4772 | unsigned Idx = 0; |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 4773 | if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD) { |
| 4774 | if (Record.size() < 3) |
| 4775 | return error("Invalid record"); |
| 4776 | } else { |
| 4777 | assert(BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD); |
| 4778 | if (Record.size() < 4) |
| 4779 | return error("Invalid record"); |
| 4780 | } |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4781 | Type *Ty = getTypeByID(Record[Idx++]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 4782 | if (!Ty) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4783 | return error("Invalid record"); |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 4784 | if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD) { |
| 4785 | Value *PersFn = nullptr; |
| 4786 | if (getValueTypePair(Record, Idx, NextValueNo, PersFn)) |
| 4787 | return error("Invalid record"); |
| 4788 | |
| 4789 | if (!F->hasPersonalityFn()) |
| 4790 | F->setPersonalityFn(cast<Constant>(PersFn)); |
| 4791 | else if (F->getPersonalityFn() != cast<Constant>(PersFn)) |
| 4792 | return error("Personality function mismatch"); |
| 4793 | } |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4794 | |
| 4795 | bool IsCleanup = !!Record[Idx++]; |
| 4796 | unsigned NumClauses = Record[Idx++]; |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 4797 | LandingPadInst *LP = LandingPadInst::Create(Ty, NumClauses); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4798 | LP->setCleanup(IsCleanup); |
| 4799 | for (unsigned J = 0; J != NumClauses; ++J) { |
| 4800 | LandingPadInst::ClauseType CT = |
| 4801 | LandingPadInst::ClauseType(Record[Idx++]); (void)CT; |
| 4802 | Value *Val; |
| 4803 | |
| 4804 | if (getValueTypePair(Record, Idx, NextValueNo, Val)) { |
| 4805 | delete LP; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4806 | return error("Invalid record"); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4807 | } |
| 4808 | |
| 4809 | assert((CT != LandingPadInst::Catch || |
| 4810 | !isa<ArrayType>(Val->getType())) && |
| 4811 | "Catch clause has a invalid type!"); |
| 4812 | assert((CT != LandingPadInst::Filter || |
| 4813 | isa<ArrayType>(Val->getType())) && |
| 4814 | "Filter clause has invalid type!"); |
Rafael Espindola | 4dc5dfc | 2014-06-04 18:51:31 +0000 | [diff] [blame] | 4815 | LP->addClause(cast<Constant>(Val)); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4816 | } |
| 4817 | |
| 4818 | I = LP; |
Bill Wendling | b9a8999 | 2011-09-01 00:50:20 +0000 | [diff] [blame] | 4819 | InstructionList.push_back(I); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4820 | break; |
| 4821 | } |
| 4822 | |
Chris Lattner | f1c8710 | 2011-06-17 18:09:11 +0000 | [diff] [blame] | 4823 | case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align] |
| 4824 | if (Record.size() != 4) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4825 | return error("Invalid record"); |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 4826 | uint64_t AlignRecord = Record[3]; |
| 4827 | const uint64_t InAllocaMask = uint64_t(1) << 5; |
David Blaikie | bdb4910 | 2015-04-28 16:51:01 +0000 | [diff] [blame] | 4828 | const uint64_t ExplicitTypeMask = uint64_t(1) << 6; |
Bob Wilson | 043ee65 | 2015-07-28 04:05:45 +0000 | [diff] [blame] | 4829 | // Reserve bit 7 for SwiftError flag. |
| 4830 | // const uint64_t SwiftErrorMask = uint64_t(1) << 7; |
David Blaikie | bdb4910 | 2015-04-28 16:51:01 +0000 | [diff] [blame] | 4831 | const uint64_t FlagMask = InAllocaMask | ExplicitTypeMask; |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 4832 | bool InAlloca = AlignRecord & InAllocaMask; |
David Blaikie | bdb4910 | 2015-04-28 16:51:01 +0000 | [diff] [blame] | 4833 | Type *Ty = getTypeByID(Record[0]); |
| 4834 | if ((AlignRecord & ExplicitTypeMask) == 0) { |
| 4835 | auto *PTy = dyn_cast_or_null<PointerType>(Ty); |
| 4836 | if (!PTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4837 | return error("Old-style alloca with a non-pointer type"); |
David Blaikie | bdb4910 | 2015-04-28 16:51:01 +0000 | [diff] [blame] | 4838 | Ty = PTy->getElementType(); |
| 4839 | } |
| 4840 | Type *OpTy = getTypeByID(Record[1]); |
| 4841 | Value *Size = getFnValueByID(Record[2], OpTy); |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 4842 | unsigned Align; |
| 4843 | if (std::error_code EC = |
David Blaikie | bdb4910 | 2015-04-28 16:51:01 +0000 | [diff] [blame] | 4844 | parseAlignmentValue(AlignRecord & ~FlagMask, Align)) { |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 4845 | return EC; |
| 4846 | } |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 4847 | if (!Ty || !Size) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4848 | return error("Invalid record"); |
David Blaikie | bdb4910 | 2015-04-28 16:51:01 +0000 | [diff] [blame] | 4849 | AllocaInst *AI = new AllocaInst(Ty, Size, Align); |
Reid Kleckner | 56b56ea | 2014-07-16 01:34:27 +0000 | [diff] [blame] | 4850 | AI->setUsedWithInAlloca(InAlloca); |
| 4851 | I = AI; |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4852 | InstructionList.push_back(I); |
Chris Lattner | c332bba | 2007-05-03 18:58:09 +0000 | [diff] [blame] | 4853 | break; |
| 4854 | } |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 4855 | case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol] |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 4856 | unsigned OpNum = 0; |
| 4857 | Value *Op; |
| 4858 | if (getValueTypePair(Record, OpNum, NextValueNo, Op) || |
David Blaikie | 8503565 | 2015-02-25 01:07:20 +0000 | [diff] [blame] | 4859 | (OpNum + 2 != Record.size() && OpNum + 3 != Record.size())) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4860 | return error("Invalid record"); |
David Blaikie | 8503565 | 2015-02-25 01:07:20 +0000 | [diff] [blame] | 4861 | |
| 4862 | Type *Ty = nullptr; |
| 4863 | if (OpNum + 3 == Record.size()) |
| 4864 | Ty = getTypeByID(Record[OpNum++]); |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 4865 | if (std::error_code EC = typeCheckLoadStoreInst(Ty, Op->getType())) |
Filipe Cabecinhas | 11bb849 | 2015-05-18 21:48:55 +0000 | [diff] [blame] | 4866 | return EC; |
David Blaikie | b7a02987 | 2015-04-17 19:56:21 +0000 | [diff] [blame] | 4867 | if (!Ty) |
| 4868 | Ty = cast<PointerType>(Op->getType())->getElementType(); |
David Blaikie | 8503565 | 2015-02-25 01:07:20 +0000 | [diff] [blame] | 4869 | |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 4870 | unsigned Align; |
| 4871 | if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align)) |
| 4872 | return EC; |
David Blaikie | b7a02987 | 2015-04-17 19:56:21 +0000 | [diff] [blame] | 4873 | I = new LoadInst(Ty, Op, "", Record[OpNum + 1], Align); |
David Blaikie | 8503565 | 2015-02-25 01:07:20 +0000 | [diff] [blame] | 4874 | |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4875 | InstructionList.push_back(I); |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4876 | break; |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 4877 | } |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4878 | case bitc::FUNC_CODE_INST_LOADATOMIC: { |
| 4879 | // LOADATOMIC: [opty, op, align, vol, ordering, synchscope] |
| 4880 | unsigned OpNum = 0; |
| 4881 | Value *Op; |
| 4882 | if (getValueTypePair(Record, OpNum, NextValueNo, Op) || |
David Blaikie | 8503565 | 2015-02-25 01:07:20 +0000 | [diff] [blame] | 4883 | (OpNum + 4 != Record.size() && OpNum + 5 != Record.size())) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4884 | return error("Invalid record"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4885 | |
David Blaikie | 8503565 | 2015-02-25 01:07:20 +0000 | [diff] [blame] | 4886 | Type *Ty = nullptr; |
| 4887 | if (OpNum + 5 == Record.size()) |
| 4888 | Ty = getTypeByID(Record[OpNum++]); |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 4889 | if (std::error_code EC = typeCheckLoadStoreInst(Ty, Op->getType())) |
Filipe Cabecinhas | 11bb849 | 2015-05-18 21:48:55 +0000 | [diff] [blame] | 4890 | return EC; |
| 4891 | if (!Ty) |
| 4892 | Ty = cast<PointerType>(Op->getType())->getElementType(); |
David Blaikie | 8503565 | 2015-02-25 01:07:20 +0000 | [diff] [blame] | 4893 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4894 | AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4895 | if (Ordering == NotAtomic || Ordering == Release || |
| 4896 | Ordering == AcquireRelease) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4897 | return error("Invalid record"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4898 | if (Ordering != NotAtomic && Record[OpNum] == 0) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4899 | return error("Invalid record"); |
| 4900 | SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4901 | |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 4902 | unsigned Align; |
| 4903 | if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align)) |
| 4904 | return EC; |
| 4905 | I = new LoadInst(Op, "", Record[OpNum+1], Align, Ordering, SynchScope); |
David Blaikie | 8503565 | 2015-02-25 01:07:20 +0000 | [diff] [blame] | 4906 | |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4907 | InstructionList.push_back(I); |
| 4908 | break; |
| 4909 | } |
David Blaikie | 612ddbf | 2015-04-22 04:14:42 +0000 | [diff] [blame] | 4910 | case bitc::FUNC_CODE_INST_STORE: |
| 4911 | case bitc::FUNC_CODE_INST_STORE_OLD: { // STORE2:[ptrty, ptr, val, align, vol] |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 4912 | unsigned OpNum = 0; |
| 4913 | Value *Val, *Ptr; |
| 4914 | if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || |
David Blaikie | 612ddbf | 2015-04-22 04:14:42 +0000 | [diff] [blame] | 4915 | (BitCode == bitc::FUNC_CODE_INST_STORE |
| 4916 | ? getValueTypePair(Record, OpNum, NextValueNo, Val) |
| 4917 | : popValue(Record, OpNum, NextValueNo, |
| 4918 | cast<PointerType>(Ptr->getType())->getElementType(), |
| 4919 | Val)) || |
| 4920 | OpNum + 2 != Record.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4921 | return error("Invalid record"); |
Filipe Cabecinhas | 11bb849 | 2015-05-18 21:48:55 +0000 | [diff] [blame] | 4922 | |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 4923 | if (std::error_code EC = |
| 4924 | typeCheckLoadStoreInst(Val->getType(), Ptr->getType())) |
Filipe Cabecinhas | 11bb849 | 2015-05-18 21:48:55 +0000 | [diff] [blame] | 4925 | return EC; |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 4926 | unsigned Align; |
| 4927 | if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align)) |
| 4928 | return EC; |
| 4929 | I = new StoreInst(Val, Ptr, Record[OpNum+1], Align); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4930 | InstructionList.push_back(I); |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 4931 | break; |
| 4932 | } |
David Blaikie | 50a0615 | 2015-04-22 04:14:46 +0000 | [diff] [blame] | 4933 | case bitc::FUNC_CODE_INST_STOREATOMIC: |
| 4934 | case bitc::FUNC_CODE_INST_STOREATOMIC_OLD: { |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4935 | // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, synchscope] |
| 4936 | unsigned OpNum = 0; |
| 4937 | Value *Val, *Ptr; |
| 4938 | if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || |
David Blaikie | 50a0615 | 2015-04-22 04:14:46 +0000 | [diff] [blame] | 4939 | (BitCode == bitc::FUNC_CODE_INST_STOREATOMIC |
| 4940 | ? getValueTypePair(Record, OpNum, NextValueNo, Val) |
| 4941 | : popValue(Record, OpNum, NextValueNo, |
| 4942 | cast<PointerType>(Ptr->getType())->getElementType(), |
| 4943 | Val)) || |
| 4944 | OpNum + 4 != Record.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4945 | return error("Invalid record"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4946 | |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 4947 | if (std::error_code EC = |
| 4948 | typeCheckLoadStoreInst(Val->getType(), Ptr->getType())) |
Filipe Cabecinhas | 11bb849 | 2015-05-18 21:48:55 +0000 | [diff] [blame] | 4949 | return EC; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4950 | AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); |
Eli Friedman | 222b5a4 | 2011-09-19 19:41:28 +0000 | [diff] [blame] | 4951 | if (Ordering == NotAtomic || Ordering == Acquire || |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4952 | Ordering == AcquireRelease) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4953 | return error("Invalid record"); |
| 4954 | SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4955 | if (Ordering != NotAtomic && Record[OpNum] == 0) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4956 | return error("Invalid record"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4957 | |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 4958 | unsigned Align; |
| 4959 | if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align)) |
| 4960 | return EC; |
| 4961 | I = new StoreInst(Val, Ptr, Record[OpNum+1], Align, Ordering, SynchScope); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4962 | InstructionList.push_back(I); |
| 4963 | break; |
| 4964 | } |
David Blaikie | 2a661cd | 2015-04-28 04:30:29 +0000 | [diff] [blame] | 4965 | case bitc::FUNC_CODE_INST_CMPXCHG_OLD: |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4966 | case bitc::FUNC_CODE_INST_CMPXCHG: { |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 4967 | // CMPXCHG:[ptrty, ptr, cmp, new, vol, successordering, synchscope, |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 4968 | // failureordering?, isweak?] |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4969 | unsigned OpNum = 0; |
| 4970 | Value *Ptr, *Cmp, *New; |
| 4971 | if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || |
David Blaikie | 2a661cd | 2015-04-28 04:30:29 +0000 | [diff] [blame] | 4972 | (BitCode == bitc::FUNC_CODE_INST_CMPXCHG |
| 4973 | ? getValueTypePair(Record, OpNum, NextValueNo, Cmp) |
| 4974 | : popValue(Record, OpNum, NextValueNo, |
| 4975 | cast<PointerType>(Ptr->getType())->getElementType(), |
| 4976 | Cmp)) || |
| 4977 | popValue(Record, OpNum, NextValueNo, Cmp->getType(), New) || |
| 4978 | Record.size() < OpNum + 3 || Record.size() > OpNum + 5) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4979 | return error("Invalid record"); |
| 4980 | AtomicOrdering SuccessOrdering = getDecodedOrdering(Record[OpNum + 1]); |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 4981 | if (SuccessOrdering == NotAtomic || SuccessOrdering == Unordered) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4982 | return error("Invalid record"); |
| 4983 | SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 2]); |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 4984 | |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 4985 | if (std::error_code EC = |
| 4986 | typeCheckLoadStoreInst(Cmp->getType(), Ptr->getType())) |
Filipe Cabecinhas | 11bb849 | 2015-05-18 21:48:55 +0000 | [diff] [blame] | 4987 | return EC; |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 4988 | AtomicOrdering FailureOrdering; |
| 4989 | if (Record.size() < 7) |
| 4990 | FailureOrdering = |
| 4991 | AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering); |
| 4992 | else |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4993 | FailureOrdering = getDecodedOrdering(Record[OpNum + 3]); |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 4994 | |
| 4995 | I = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering, FailureOrdering, |
| 4996 | SynchScope); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4997 | cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]); |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 4998 | |
| 4999 | if (Record.size() < 8) { |
| 5000 | // Before weak cmpxchgs existed, the instruction simply returned the |
| 5001 | // value loaded from memory, so bitcode files from that era will be |
| 5002 | // expecting the first component of a modern cmpxchg. |
| 5003 | CurBB->getInstList().push_back(I); |
| 5004 | I = ExtractValueInst::Create(I, 0); |
| 5005 | } else { |
| 5006 | cast<AtomicCmpXchgInst>(I)->setWeak(Record[OpNum+4]); |
| 5007 | } |
| 5008 | |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 5009 | InstructionList.push_back(I); |
| 5010 | break; |
| 5011 | } |
| 5012 | case bitc::FUNC_CODE_INST_ATOMICRMW: { |
| 5013 | // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, synchscope] |
| 5014 | unsigned OpNum = 0; |
| 5015 | Value *Ptr, *Val; |
| 5016 | if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 5017 | popValue(Record, OpNum, NextValueNo, |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 5018 | cast<PointerType>(Ptr->getType())->getElementType(), Val) || |
| 5019 | OpNum+4 != Record.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5020 | return error("Invalid record"); |
| 5021 | AtomicRMWInst::BinOp Operation = getDecodedRMWOperation(Record[OpNum]); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 5022 | if (Operation < AtomicRMWInst::FIRST_BINOP || |
| 5023 | Operation > AtomicRMWInst::LAST_BINOP) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5024 | return error("Invalid record"); |
| 5025 | AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 5026 | if (Ordering == NotAtomic || Ordering == Unordered) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5027 | return error("Invalid record"); |
| 5028 | SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 5029 | I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SynchScope); |
| 5030 | cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]); |
| 5031 | InstructionList.push_back(I); |
| 5032 | break; |
| 5033 | } |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 5034 | case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, synchscope] |
| 5035 | if (2 != Record.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5036 | return error("Invalid record"); |
| 5037 | AtomicOrdering Ordering = getDecodedOrdering(Record[0]); |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 5038 | if (Ordering == NotAtomic || Ordering == Unordered || |
| 5039 | Ordering == Monotonic) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5040 | return error("Invalid record"); |
| 5041 | SynchronizationScope SynchScope = getDecodedSynchScope(Record[1]); |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 5042 | I = new FenceInst(Context, Ordering, SynchScope); |
| 5043 | InstructionList.push_back(I); |
| 5044 | break; |
| 5045 | } |
Chris Lattner | c4407080 | 2011-06-17 18:17:37 +0000 | [diff] [blame] | 5046 | case bitc::FUNC_CODE_INST_CALL: { |
Sanjay Patel | fa54ace | 2015-12-14 21:59:03 +0000 | [diff] [blame] | 5047 | // CALL: [paramattrs, cc, fmf, fnty, fnid, arg0, arg1...] |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 5048 | if (Record.size() < 3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5049 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 5050 | |
David Blaikie | dbe6e0f | 2015-04-17 06:40:14 +0000 | [diff] [blame] | 5051 | unsigned OpNum = 0; |
| 5052 | AttributeSet PAL = getAttributes(Record[OpNum++]); |
| 5053 | unsigned CCInfo = Record[OpNum++]; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 5054 | |
Sanjay Patel | fa54ace | 2015-12-14 21:59:03 +0000 | [diff] [blame] | 5055 | FastMathFlags FMF; |
| 5056 | if ((CCInfo >> bitc::CALL_FMF) & 1) { |
| 5057 | FMF = getDecodedFastMathFlags(Record[OpNum++]); |
| 5058 | if (!FMF.any()) |
| 5059 | return error("Fast math flags indicator set for call with no FMF"); |
| 5060 | } |
| 5061 | |
David Blaikie | dbe6e0f | 2015-04-17 06:40:14 +0000 | [diff] [blame] | 5062 | FunctionType *FTy = nullptr; |
Akira Hatanaka | 97cb397 | 2015-11-07 02:48:49 +0000 | [diff] [blame] | 5063 | if (CCInfo >> bitc::CALL_EXPLICIT_TYPE & 1 && |
David Blaikie | dbe6e0f | 2015-04-17 06:40:14 +0000 | [diff] [blame] | 5064 | !(FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++])))) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5065 | return error("Explicit call type is not a function type"); |
David Blaikie | dbe6e0f | 2015-04-17 06:40:14 +0000 | [diff] [blame] | 5066 | |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 5067 | Value *Callee; |
| 5068 | if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5069 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 5070 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5071 | PointerType *OpTy = dyn_cast<PointerType>(Callee->getType()); |
David Blaikie | dbe6e0f | 2015-04-17 06:40:14 +0000 | [diff] [blame] | 5072 | if (!OpTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5073 | return error("Callee is not a pointer type"); |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 5074 | if (!FTy) { |
| 5075 | FTy = dyn_cast<FunctionType>(OpTy->getElementType()); |
| 5076 | if (!FTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5077 | return error("Callee is not of pointer to function type"); |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 5078 | } else if (OpTy->getElementType() != FTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5079 | return error("Explicit call type does not match pointee type of " |
David Blaikie | dbe6e0f | 2015-04-17 06:40:14 +0000 | [diff] [blame] | 5080 | "callee operand"); |
| 5081 | if (Record.size() < FTy->getNumParams() + OpNum) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5082 | return error("Insufficient operands to call"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 5083 | |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 5084 | SmallVector<Value*, 16> Args; |
| 5085 | // Read the fixed params. |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 5086 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 5087 | if (FTy->getParamType(i)->isLabelTy()) |
Dale Johannesen | 4646aa3 | 2007-11-05 21:20:28 +0000 | [diff] [blame] | 5088 | Args.push_back(getBasicBlock(Record[OpNum])); |
Dan Gohman | bbcd04d | 2010-09-13 18:00:48 +0000 | [diff] [blame] | 5089 | else |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 5090 | Args.push_back(getValue(Record, OpNum, NextValueNo, |
| 5091 | FTy->getParamType(i))); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 5092 | if (!Args.back()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5093 | return error("Invalid record"); |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 5094 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 5095 | |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 5096 | // Read type/value pairs for varargs params. |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 5097 | if (!FTy->isVarArg()) { |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 5098 | if (OpNum != Record.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5099 | return error("Invalid record"); |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 5100 | } else { |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 5101 | while (OpNum != Record.size()) { |
| 5102 | Value *Op; |
| 5103 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5104 | return error("Invalid record"); |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 5105 | Args.push_back(Op); |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 5106 | } |
| 5107 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 5108 | |
Sanjoy Das | b513a9f | 2015-09-24 23:34:52 +0000 | [diff] [blame] | 5109 | I = CallInst::Create(FTy, Callee, Args, OperandBundles); |
| 5110 | OperandBundles.clear(); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 5111 | InstructionList.push_back(I); |
Sandeep Patel | 68c5f47 | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 5112 | cast<CallInst>(I)->setCallingConv( |
Akira Hatanaka | 97cb397 | 2015-11-07 02:48:49 +0000 | [diff] [blame] | 5113 | static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV)); |
Reid Kleckner | 5772b77 | 2014-04-24 20:14:34 +0000 | [diff] [blame] | 5114 | CallInst::TailCallKind TCK = CallInst::TCK_None; |
Akira Hatanaka | 97cb397 | 2015-11-07 02:48:49 +0000 | [diff] [blame] | 5115 | if (CCInfo & 1 << bitc::CALL_TAIL) |
Reid Kleckner | 5772b77 | 2014-04-24 20:14:34 +0000 | [diff] [blame] | 5116 | TCK = CallInst::TCK_Tail; |
Akira Hatanaka | 97cb397 | 2015-11-07 02:48:49 +0000 | [diff] [blame] | 5117 | if (CCInfo & (1 << bitc::CALL_MUSTTAIL)) |
Reid Kleckner | 5772b77 | 2014-04-24 20:14:34 +0000 | [diff] [blame] | 5118 | TCK = CallInst::TCK_MustTail; |
Akira Hatanaka | 97cb397 | 2015-11-07 02:48:49 +0000 | [diff] [blame] | 5119 | if (CCInfo & (1 << bitc::CALL_NOTAIL)) |
Akira Hatanaka | 5cfcce12 | 2015-11-06 23:55:38 +0000 | [diff] [blame] | 5120 | TCK = CallInst::TCK_NoTail; |
Reid Kleckner | 5772b77 | 2014-04-24 20:14:34 +0000 | [diff] [blame] | 5121 | cast<CallInst>(I)->setTailCallKind(TCK); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 5122 | cast<CallInst>(I)->setAttributes(PAL); |
Sanjay Patel | fa54ace | 2015-12-14 21:59:03 +0000 | [diff] [blame] | 5123 | if (FMF.any()) { |
| 5124 | if (!isa<FPMathOperator>(I)) |
| 5125 | return error("Fast-math-flags specified for call without " |
| 5126 | "floating-point scalar or vector return type"); |
| 5127 | I->setFastMathFlags(FMF); |
| 5128 | } |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 5129 | break; |
| 5130 | } |
| 5131 | case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty] |
| 5132 | if (Record.size() < 3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5133 | return error("Invalid record"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5134 | Type *OpTy = getTypeByID(Record[0]); |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 5135 | Value *Op = getValue(Record, 1, NextValueNo, OpTy); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5136 | Type *ResTy = getTypeByID(Record[2]); |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 5137 | if (!OpTy || !Op || !ResTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5138 | return error("Invalid record"); |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 5139 | I = new VAArgInst(Op, ResTy); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 5140 | InstructionList.push_back(I); |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 5141 | break; |
| 5142 | } |
Sanjoy Das | b513a9f | 2015-09-24 23:34:52 +0000 | [diff] [blame] | 5143 | |
| 5144 | case bitc::FUNC_CODE_OPERAND_BUNDLE: { |
| 5145 | // A call or an invoke can be optionally prefixed with some variable |
| 5146 | // number of operand bundle blocks. These blocks are read into |
| 5147 | // OperandBundles and consumed at the next call or invoke instruction. |
| 5148 | |
| 5149 | if (Record.size() < 1 || Record[0] >= BundleTags.size()) |
| 5150 | return error("Invalid record"); |
| 5151 | |
Sanjoy Das | f79d344 | 2015-11-18 08:30:07 +0000 | [diff] [blame] | 5152 | std::vector<Value *> Inputs; |
Sanjoy Das | b513a9f | 2015-09-24 23:34:52 +0000 | [diff] [blame] | 5153 | |
| 5154 | unsigned OpNum = 1; |
| 5155 | while (OpNum != Record.size()) { |
| 5156 | Value *Op; |
| 5157 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) |
| 5158 | return error("Invalid record"); |
| 5159 | Inputs.push_back(Op); |
| 5160 | } |
| 5161 | |
Sanjoy Das | f79d344 | 2015-11-18 08:30:07 +0000 | [diff] [blame] | 5162 | OperandBundles.emplace_back(BundleTags[Record[0]], std::move(Inputs)); |
Sanjoy Das | b513a9f | 2015-09-24 23:34:52 +0000 | [diff] [blame] | 5163 | continue; |
| 5164 | } |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 5165 | } |
| 5166 | |
| 5167 | // Add instruction to end of current BB. If there is no current BB, reject |
| 5168 | // this file. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 5169 | if (!CurBB) { |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 5170 | delete I; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5171 | return error("Invalid instruction with no BB"); |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 5172 | } |
Sanjoy Das | b513a9f | 2015-09-24 23:34:52 +0000 | [diff] [blame] | 5173 | if (!OperandBundles.empty()) { |
| 5174 | delete I; |
| 5175 | return error("Operand bundles found with no consumer"); |
| 5176 | } |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 5177 | CurBB->getInstList().push_back(I); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 5178 | |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 5179 | // If this was a terminator instruction, move to the next block. |
| 5180 | if (isa<TerminatorInst>(I)) { |
| 5181 | ++CurBBNo; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 5182 | CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr; |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 5183 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 5184 | |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 5185 | // Non-void values get registered in the value table for future use. |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 5186 | if (I && !I->getType()->isVoidTy()) |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 5187 | ValueList.assignValue(I, NextValueNo++); |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 5188 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 5189 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 5190 | OutOfRecordLoop: |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 5191 | |
Sanjoy Das | b513a9f | 2015-09-24 23:34:52 +0000 | [diff] [blame] | 5192 | if (!OperandBundles.empty()) |
| 5193 | return error("Operand bundles found with no consumer"); |
| 5194 | |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 5195 | // Check the function list for unresolved values. |
| 5196 | if (Argument *A = dyn_cast<Argument>(ValueList.back())) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 5197 | if (!A->getParent()) { |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 5198 | // We found at least one unresolved value. Nuke them all to avoid leaks. |
| 5199 | for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){ |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 5200 | if ((A = dyn_cast_or_null<Argument>(ValueList[i])) && !A->getParent()) { |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 5201 | A->replaceAllUsesWith(UndefValue::get(A->getType())); |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 5202 | delete A; |
| 5203 | } |
| 5204 | } |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5205 | return error("Never resolved value found in function"); |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 5206 | } |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 5207 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 5208 | |
Dan Gohman | 9b9ff46 | 2010-08-25 20:23:38 +0000 | [diff] [blame] | 5209 | // FIXME: Check for unresolved forward-declared metadata references |
| 5210 | // and clean up leaks. |
| 5211 | |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 5212 | // Trim the value list down to the size it was before we parsed this function. |
| 5213 | ValueList.shrinkTo(ModuleValueListSize); |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 5214 | MetadataList.shrinkTo(ModuleMetadataListSize); |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 5215 | std::vector<BasicBlock*>().swap(FunctionBBs); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 5216 | return std::error_code(); |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 5217 | } |
| 5218 | |
Rafael Espindola | 7d71203 | 2013-11-05 17:16:08 +0000 | [diff] [blame] | 5219 | /// Find the function body in the bitcode stream |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5220 | std::error_code BitcodeReader::findFunctionInStream( |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 5221 | Function *F, |
| 5222 | DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) { |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 5223 | while (DeferredFunctionInfoIterator->second == 0) { |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 5224 | // This is the fallback handling for the old format bitcode that |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 5225 | // didn't contain the function index in the VST, or when we have |
| 5226 | // an anonymous function which would not have a VST entry. |
| 5227 | // Assert that we have one of those two cases. |
| 5228 | assert(VSTOffset == 0 || !F->hasName()); |
| 5229 | // Parse the next body in the stream and set its position in the |
| 5230 | // DeferredFunctionInfo map. |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5231 | if (std::error_code EC = rememberAndSkipFunctionBodies()) |
| 5232 | return EC; |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 5233 | } |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 5234 | return std::error_code(); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 5235 | } |
| 5236 | |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 5237 | //===----------------------------------------------------------------------===// |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 5238 | // GVMaterializer implementation |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 5239 | //===----------------------------------------------------------------------===// |
| 5240 | |
Rafael Espindola | c3f9b5a | 2014-06-23 21:53:12 +0000 | [diff] [blame] | 5241 | void BitcodeReader::releaseBuffer() { Buffer.release(); } |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 5242 | |
Rafael Espindola | 5a52e6d | 2014-10-24 22:50:48 +0000 | [diff] [blame] | 5243 | std::error_code BitcodeReader::materialize(GlobalValue *GV) { |
Teresa Johnson | d4d3dfd | 2015-11-20 14:51:27 +0000 | [diff] [blame] | 5244 | // In older bitcode we must materialize the metadata before parsing |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 5245 | // any functions, in order to set up the MetadataList properly. |
Teresa Johnson | d4d3dfd | 2015-11-20 14:51:27 +0000 | [diff] [blame] | 5246 | if (!SeenModuleValuesRecord) { |
| 5247 | if (std::error_code EC = materializeMetadata()) |
| 5248 | return EC; |
| 5249 | } |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 5250 | |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 5251 | Function *F = dyn_cast<Function>(GV); |
| 5252 | // If it's not a function or is already material, ignore the request. |
Rafael Espindola | 2b11ad4 | 2013-11-05 19:36:34 +0000 | [diff] [blame] | 5253 | if (!F || !F->isMaterializable()) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 5254 | return std::error_code(); |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 5255 | |
| 5256 | DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F); |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 5257 | assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!"); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 5258 | // If its position is recorded as 0, its body is somewhere in the stream |
| 5259 | // but we haven't seen it yet. |
Rafael Espindola | 1c863ca | 2015-06-22 18:06:15 +0000 | [diff] [blame] | 5260 | if (DFII->second == 0) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5261 | if (std::error_code EC = findFunctionInStream(F, DFII)) |
Rafael Espindola | 2b11ad4 | 2013-11-05 19:36:34 +0000 | [diff] [blame] | 5262 | return EC; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 5263 | |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 5264 | // Move the bit stream to the saved position of the deferred function body. |
| 5265 | Stream.JumpToBit(DFII->second); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 5266 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5267 | if (std::error_code EC = parseFunctionBody(F)) |
Rafael Espindola | 2b11ad4 | 2013-11-05 19:36:34 +0000 | [diff] [blame] | 5268 | return EC; |
Rafael Espindola | d4bcefc | 2014-10-24 18:13:04 +0000 | [diff] [blame] | 5269 | F->setIsMaterializable(false); |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 5270 | |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 5271 | if (StripDebugInfo) |
| 5272 | stripDebugInfo(*F); |
| 5273 | |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 5274 | // Upgrade any old intrinsic calls in the function. |
Rafael Espindola | 86e3340 | 2015-07-02 15:55:09 +0000 | [diff] [blame] | 5275 | for (auto &I : UpgradedIntrinsics) { |
Rafael Espindola | 257a353 | 2016-01-15 19:00:20 +0000 | [diff] [blame] | 5276 | for (auto UI = I.first->materialized_user_begin(), UE = I.first->user_end(); |
| 5277 | UI != UE;) { |
Filipe Cabecinhas | 0011c58 | 2015-07-03 20:12:01 +0000 | [diff] [blame] | 5278 | User *U = *UI; |
| 5279 | ++UI; |
| 5280 | if (CallInst *CI = dyn_cast<CallInst>(U)) |
| 5281 | UpgradeIntrinsicCall(CI, I.second); |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 5282 | } |
| 5283 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 5284 | |
Peter Collingbourne | d4bff30 | 2015-11-05 22:03:56 +0000 | [diff] [blame] | 5285 | // Finish fn->subprogram upgrade for materialized functions. |
| 5286 | if (DISubprogram *SP = FunctionsWithSPs.lookup(F)) |
| 5287 | F->setSubprogram(SP); |
| 5288 | |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 5289 | // Bring in any functions that this function forward-referenced via |
| 5290 | // blockaddresses. |
| 5291 | return materializeForwardReferencedFunctions(); |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 5292 | } |
| 5293 | |
Rafael Espindola | 79753a0 | 2015-12-18 21:18:57 +0000 | [diff] [blame] | 5294 | std::error_code BitcodeReader::materializeModule() { |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 5295 | if (std::error_code EC = materializeMetadata()) |
| 5296 | return EC; |
| 5297 | |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 5298 | // Promise to materialize all forward references. |
| 5299 | WillMaterializeAllForwardRefs = true; |
| 5300 | |
Chris Lattner | 06310bf | 2009-06-16 05:15:21 +0000 | [diff] [blame] | 5301 | // Iterate over the module, deserializing any functions that are still on |
| 5302 | // disk. |
Duncan P. N. Exon Smith | fb1743a3 | 2015-10-13 16:48:55 +0000 | [diff] [blame] | 5303 | for (Function &F : *TheModule) { |
| 5304 | if (std::error_code EC = materialize(&F)) |
Rafael Espindola | 246c4fb | 2014-11-01 16:46:18 +0000 | [diff] [blame] | 5305 | return EC; |
Rafael Espindola | 2b11ad4 | 2013-11-05 19:36:34 +0000 | [diff] [blame] | 5306 | } |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 5307 | // At this point, if there are any function bodies, parse the rest of |
| 5308 | // the bits in the module past the last function block we have recorded |
| 5309 | // through either lazy scanning or the VST. |
| 5310 | if (LastFunctionBlockBit || NextUnreadBit) |
| 5311 | parseModule(LastFunctionBlockBit > NextUnreadBit ? LastFunctionBlockBit |
| 5312 | : NextUnreadBit); |
Derek Schuff | 92ef975 | 2012-02-29 00:07:09 +0000 | [diff] [blame] | 5313 | |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 5314 | // Check that all block address forward references got resolved (as we |
| 5315 | // promised above). |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 5316 | if (!BasicBlockFwdRefs.empty()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5317 | return error("Never resolved function from blockaddress"); |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 5318 | |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 5319 | // Upgrade any intrinsic calls that slipped through (should not happen!) and |
| 5320 | // delete the old functions to clean up. We can't do this unless the entire |
| 5321 | // module is materialized because there could always be another function body |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 5322 | // with calls to the old function. |
Rafael Espindola | 86e3340 | 2015-07-02 15:55:09 +0000 | [diff] [blame] | 5323 | for (auto &I : UpgradedIntrinsics) { |
Filipe Cabecinhas | 0011c58 | 2015-07-03 20:12:01 +0000 | [diff] [blame] | 5324 | for (auto *U : I.first->users()) { |
| 5325 | if (CallInst *CI = dyn_cast<CallInst>(U)) |
| 5326 | UpgradeIntrinsicCall(CI, I.second); |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 5327 | } |
Filipe Cabecinhas | 0011c58 | 2015-07-03 20:12:01 +0000 | [diff] [blame] | 5328 | if (!I.first->use_empty()) |
| 5329 | I.first->replaceAllUsesWith(I.second); |
| 5330 | I.first->eraseFromParent(); |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 5331 | } |
Rafael Espindola | 4e72121 | 2015-07-02 16:22:40 +0000 | [diff] [blame] | 5332 | UpgradedIntrinsics.clear(); |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 5333 | |
Manman Ren | 209b17c | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 5334 | for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++) |
| 5335 | UpgradeInstWithTBAATag(InstsWithTBAATag[I]); |
| 5336 | |
Rafael Espindola | 79753a0 | 2015-12-18 21:18:57 +0000 | [diff] [blame] | 5337 | UpgradeDebugInfo(*TheModule); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 5338 | return std::error_code(); |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 5339 | } |
| 5340 | |
Rafael Espindola | 2fa1e43 | 2014-12-03 07:18:23 +0000 | [diff] [blame] | 5341 | std::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const { |
| 5342 | return IdentifiedStructTypes; |
| 5343 | } |
| 5344 | |
Rafael Espindola | 1aabf98 | 2015-06-16 23:29:49 +0000 | [diff] [blame] | 5345 | std::error_code |
| 5346 | BitcodeReader::initStream(std::unique_ptr<DataStreamer> Streamer) { |
Rafael Espindola | 4223a1f | 2015-06-15 20:08:17 +0000 | [diff] [blame] | 5347 | if (Streamer) |
Rafael Espindola | 1aabf98 | 2015-06-16 23:29:49 +0000 | [diff] [blame] | 5348 | return initLazyStream(std::move(Streamer)); |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5349 | return initStreamFromBuffer(); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 5350 | } |
| 5351 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5352 | std::error_code BitcodeReader::initStreamFromBuffer() { |
Roman Divacky | 4717a8d | 2012-09-06 15:42:13 +0000 | [diff] [blame] | 5353 | const unsigned char *BufPtr = (const unsigned char*)Buffer->getBufferStart(); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 5354 | const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize(); |
| 5355 | |
Rafael Espindola | 2743525 | 2014-07-29 21:01:24 +0000 | [diff] [blame] | 5356 | if (Buffer->getBufferSize() & 3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5357 | return error("Invalid bitcode signature"); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 5358 | |
| 5359 | // If we have a wrapper header, parse it and ignore the non-bc file contents. |
| 5360 | // The magic number is 0x0B17C0DE stored in little endian. |
| 5361 | if (isBitcodeWrapper(BufPtr, BufEnd)) |
| 5362 | if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5363 | return error("Invalid bitcode wrapper header"); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 5364 | |
| 5365 | StreamFile.reset(new BitstreamReader(BufPtr, BufEnd)); |
Rafael Espindola | de1e5b8 | 2014-11-12 14:48:38 +0000 | [diff] [blame] | 5366 | Stream.init(&*StreamFile); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 5367 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 5368 | return std::error_code(); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 5369 | } |
| 5370 | |
Rafael Espindola | 1aabf98 | 2015-06-16 23:29:49 +0000 | [diff] [blame] | 5371 | std::error_code |
| 5372 | BitcodeReader::initLazyStream(std::unique_ptr<DataStreamer> Streamer) { |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 5373 | // Check and strip off the bitcode wrapper; BitstreamReader expects never to |
| 5374 | // see it. |
Rafael Espindola | 1aabf98 | 2015-06-16 23:29:49 +0000 | [diff] [blame] | 5375 | auto OwnedBytes = |
| 5376 | llvm::make_unique<StreamingMemoryObject>(std::move(Streamer)); |
Rafael Espindola | 7d727b5 | 2014-12-18 05:08:43 +0000 | [diff] [blame] | 5377 | StreamingMemoryObject &Bytes = *OwnedBytes; |
Yaron Keren | 06d6930 | 2014-12-18 10:03:35 +0000 | [diff] [blame] | 5378 | StreamFile = llvm::make_unique<BitstreamReader>(std::move(OwnedBytes)); |
Rafael Espindola | de1e5b8 | 2014-11-12 14:48:38 +0000 | [diff] [blame] | 5379 | Stream.init(&*StreamFile); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 5380 | |
| 5381 | unsigned char buf[16]; |
Rafael Espindola | 7d727b5 | 2014-12-18 05:08:43 +0000 | [diff] [blame] | 5382 | if (Bytes.readBytes(buf, 16, 0) != 16) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5383 | return error("Invalid bitcode signature"); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 5384 | |
| 5385 | if (!isBitcode(buf, buf + 16)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 5386 | return error("Invalid bitcode signature"); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 5387 | |
| 5388 | if (isBitcodeWrapper(buf, buf + 4)) { |
| 5389 | const unsigned char *bitcodeStart = buf; |
| 5390 | const unsigned char *bitcodeEnd = buf + 16; |
| 5391 | SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false); |
Rafael Espindola | 7d727b5 | 2014-12-18 05:08:43 +0000 | [diff] [blame] | 5392 | Bytes.dropLeadingBytes(bitcodeStart - buf); |
| 5393 | Bytes.setKnownObjectSize(bitcodeEnd - bitcodeStart); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 5394 | } |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 5395 | return std::error_code(); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 5396 | } |
| 5397 | |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5398 | std::error_code FunctionIndexBitcodeReader::error(BitcodeError E, |
| 5399 | const Twine &Message) { |
| 5400 | return ::error(DiagnosticHandler, make_error_code(E), Message); |
| 5401 | } |
| 5402 | |
| 5403 | std::error_code FunctionIndexBitcodeReader::error(const Twine &Message) { |
| 5404 | return ::error(DiagnosticHandler, |
| 5405 | make_error_code(BitcodeError::CorruptedBitcode), Message); |
| 5406 | } |
| 5407 | |
| 5408 | std::error_code FunctionIndexBitcodeReader::error(BitcodeError E) { |
| 5409 | return ::error(DiagnosticHandler, make_error_code(E)); |
| 5410 | } |
| 5411 | |
| 5412 | FunctionIndexBitcodeReader::FunctionIndexBitcodeReader( |
Mehdi Amini | 354f520 | 2015-11-19 05:52:29 +0000 | [diff] [blame] | 5413 | MemoryBuffer *Buffer, DiagnosticHandlerFunction DiagnosticHandler, |
| 5414 | bool IsLazy, bool CheckFuncSummaryPresenceOnly) |
| 5415 | : DiagnosticHandler(DiagnosticHandler), Buffer(Buffer), IsLazy(IsLazy), |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5416 | CheckFuncSummaryPresenceOnly(CheckFuncSummaryPresenceOnly) {} |
| 5417 | |
| 5418 | FunctionIndexBitcodeReader::FunctionIndexBitcodeReader( |
Mehdi Amini | 354f520 | 2015-11-19 05:52:29 +0000 | [diff] [blame] | 5419 | DiagnosticHandlerFunction DiagnosticHandler, bool IsLazy, |
| 5420 | bool CheckFuncSummaryPresenceOnly) |
| 5421 | : DiagnosticHandler(DiagnosticHandler), Buffer(nullptr), IsLazy(IsLazy), |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5422 | CheckFuncSummaryPresenceOnly(CheckFuncSummaryPresenceOnly) {} |
| 5423 | |
| 5424 | void FunctionIndexBitcodeReader::freeState() { Buffer = nullptr; } |
| 5425 | |
| 5426 | void FunctionIndexBitcodeReader::releaseBuffer() { Buffer.release(); } |
| 5427 | |
| 5428 | // Specialized value symbol table parser used when reading function index |
| 5429 | // blocks where we don't actually create global values. |
| 5430 | // At the end of this routine the function index is populated with a map |
| 5431 | // from function name to FunctionInfo. The function info contains |
| 5432 | // the function block's bitcode offset as well as the offset into the |
| 5433 | // function summary section. |
| 5434 | std::error_code FunctionIndexBitcodeReader::parseValueSymbolTable() { |
| 5435 | if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) |
| 5436 | return error("Invalid record"); |
| 5437 | |
| 5438 | SmallVector<uint64_t, 64> Record; |
| 5439 | |
| 5440 | // Read all the records for this value table. |
| 5441 | SmallString<128> ValueName; |
| 5442 | while (1) { |
| 5443 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| 5444 | |
| 5445 | switch (Entry.Kind) { |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5446 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 5447 | case BitstreamEntry::Error: |
| 5448 | return error("Malformed block"); |
| 5449 | case BitstreamEntry::EndBlock: |
| 5450 | return std::error_code(); |
| 5451 | case BitstreamEntry::Record: |
| 5452 | // The interesting case. |
| 5453 | break; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5454 | } |
| 5455 | |
| 5456 | // Read a record. |
| 5457 | Record.clear(); |
| 5458 | switch (Stream.readRecord(Entry.ID, Record)) { |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5459 | default: // Default behavior: ignore (e.g. VST_CODE_BBENTRY records). |
| 5460 | break; |
| 5461 | case bitc::VST_CODE_FNENTRY: { |
Teresa Johnson | 79d4e2f | 2016-02-10 15:02:51 +0000 | [diff] [blame] | 5462 | // VST_CODE_FNENTRY: [valueid, offset, namechar x N] |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5463 | if (convertToString(Record, 2, ValueName)) |
| 5464 | return error("Invalid record"); |
| 5465 | unsigned ValueID = Record[0]; |
| 5466 | uint64_t FuncOffset = Record[1]; |
Teresa Johnson | e1164de | 2016-02-10 21:55:02 +0000 | [diff] [blame] | 5467 | assert(!IsLazy && "Lazy summary read only supported for combined index"); |
| 5468 | // Gracefully handle bitcode without a function summary section, |
| 5469 | // which will simply not populate the index. |
| 5470 | if (foundFuncSummary()) { |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5471 | DenseMap<uint64_t, std::unique_ptr<FunctionSummary>>::iterator SMI = |
| 5472 | SummaryMap.find(ValueID); |
| 5473 | assert(SMI != SummaryMap.end() && "Summary info not found"); |
Teresa Johnson | e1164de | 2016-02-10 21:55:02 +0000 | [diff] [blame] | 5474 | std::unique_ptr<FunctionInfo> FuncInfo = |
| 5475 | llvm::make_unique<FunctionInfo>(FuncOffset); |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5476 | FuncInfo->setFunctionSummary(std::move(SMI->second)); |
Teresa Johnson | e1164de | 2016-02-10 21:55:02 +0000 | [diff] [blame] | 5477 | assert(!SourceFileName.empty()); |
| 5478 | std::string FunctionGlobalId = Function::getGlobalIdentifier( |
| 5479 | ValueName, FuncInfo->functionSummary()->getFunctionLinkage(), |
| 5480 | SourceFileName); |
| 5481 | TheIndex->addFunctionInfo(FunctionGlobalId, std::move(FuncInfo)); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5482 | } |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5483 | |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5484 | ValueName.clear(); |
| 5485 | break; |
| 5486 | } |
| 5487 | case bitc::VST_CODE_COMBINED_FNENTRY: { |
Teresa Johnson | e1164de | 2016-02-10 21:55:02 +0000 | [diff] [blame] | 5488 | // VST_CODE_COMBINED_FNENTRY: [offset, funcguid] |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5489 | uint64_t FuncSummaryOffset = Record[0]; |
Teresa Johnson | e1164de | 2016-02-10 21:55:02 +0000 | [diff] [blame] | 5490 | uint64_t FuncGUID = Record[1]; |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5491 | std::unique_ptr<FunctionInfo> FuncInfo = |
| 5492 | llvm::make_unique<FunctionInfo>(FuncSummaryOffset); |
| 5493 | if (foundFuncSummary() && !IsLazy) { |
| 5494 | DenseMap<uint64_t, std::unique_ptr<FunctionSummary>>::iterator SMI = |
| 5495 | SummaryMap.find(FuncSummaryOffset); |
| 5496 | assert(SMI != SummaryMap.end() && "Summary info not found"); |
| 5497 | FuncInfo->setFunctionSummary(std::move(SMI->second)); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5498 | } |
Teresa Johnson | e1164de | 2016-02-10 21:55:02 +0000 | [diff] [blame] | 5499 | TheIndex->addFunctionInfo(FuncGUID, std::move(FuncInfo)); |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5500 | |
| 5501 | ValueName.clear(); |
| 5502 | break; |
| 5503 | } |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5504 | } |
| 5505 | } |
| 5506 | } |
| 5507 | |
| 5508 | // Parse just the blocks needed for function index building out of the module. |
| 5509 | // At the end of this routine the function Index is populated with a map |
| 5510 | // from function name to FunctionInfo. The function info contains |
| 5511 | // either the parsed function summary information (when parsing summaries |
| 5512 | // eagerly), or just to the function summary record's offset |
| 5513 | // if parsing lazily (IsLazy). |
| 5514 | std::error_code FunctionIndexBitcodeReader::parseModule() { |
| 5515 | if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) |
| 5516 | return error("Invalid record"); |
| 5517 | |
Teresa Johnson | e1164de | 2016-02-10 21:55:02 +0000 | [diff] [blame] | 5518 | SmallVector<uint64_t, 64> Record; |
| 5519 | |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5520 | // Read the function index for this module. |
| 5521 | while (1) { |
| 5522 | BitstreamEntry Entry = Stream.advance(); |
| 5523 | |
| 5524 | switch (Entry.Kind) { |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5525 | case BitstreamEntry::Error: |
| 5526 | return error("Malformed block"); |
| 5527 | case BitstreamEntry::EndBlock: |
| 5528 | return std::error_code(); |
| 5529 | |
| 5530 | case BitstreamEntry::SubBlock: |
| 5531 | if (CheckFuncSummaryPresenceOnly) { |
Teresa Johnson | 6290dbc | 2015-11-21 21:55:48 +0000 | [diff] [blame] | 5532 | if (Entry.ID == bitc::FUNCTION_SUMMARY_BLOCK_ID) { |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5533 | SeenFuncSummary = true; |
Teresa Johnson | 6290dbc | 2015-11-21 21:55:48 +0000 | [diff] [blame] | 5534 | // No need to parse the rest since we found the summary. |
| 5535 | return std::error_code(); |
| 5536 | } |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5537 | if (Stream.SkipBlock()) |
| 5538 | return error("Invalid record"); |
Teresa Johnson | 6290dbc | 2015-11-21 21:55:48 +0000 | [diff] [blame] | 5539 | continue; |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5540 | } |
| 5541 | switch (Entry.ID) { |
| 5542 | default: // Skip unknown content. |
| 5543 | if (Stream.SkipBlock()) |
| 5544 | return error("Invalid record"); |
| 5545 | break; |
| 5546 | case bitc::BLOCKINFO_BLOCK_ID: |
| 5547 | // Need to parse these to get abbrev ids (e.g. for VST) |
| 5548 | if (Stream.ReadBlockInfoBlock()) |
| 5549 | return error("Malformed block"); |
| 5550 | break; |
| 5551 | case bitc::VALUE_SYMTAB_BLOCK_ID: |
| 5552 | if (std::error_code EC = parseValueSymbolTable()) |
| 5553 | return EC; |
| 5554 | break; |
| 5555 | case bitc::FUNCTION_SUMMARY_BLOCK_ID: |
| 5556 | SeenFuncSummary = true; |
| 5557 | if (IsLazy) { |
| 5558 | // Lazy parsing of summary info, skip it. |
| 5559 | if (Stream.SkipBlock()) |
| 5560 | return error("Invalid record"); |
| 5561 | } else if (std::error_code EC = parseEntireSummary()) |
| 5562 | return EC; |
| 5563 | break; |
| 5564 | case bitc::MODULE_STRTAB_BLOCK_ID: |
| 5565 | if (std::error_code EC = parseModuleStringTable()) |
| 5566 | return EC; |
| 5567 | break; |
| 5568 | } |
| 5569 | continue; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5570 | |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5571 | case BitstreamEntry::Record: |
Teresa Johnson | e1164de | 2016-02-10 21:55:02 +0000 | [diff] [blame] | 5572 | // Once we find the single record of interest, skip the rest. |
| 5573 | if (!SourceFileName.empty()) |
| 5574 | Stream.skipRecord(Entry.ID); |
| 5575 | else { |
| 5576 | Record.clear(); |
| 5577 | auto BitCode = Stream.readRecord(Entry.ID, Record); |
| 5578 | switch (BitCode) { |
| 5579 | default: |
| 5580 | break; // Default behavior, ignore unknown content. |
| 5581 | /// MODULE_CODE_SOURCE_FILENAME: [namechar x N] |
| 5582 | case bitc::MODULE_CODE_SOURCE_FILENAME: |
| 5583 | SmallString<128> ValueName; |
| 5584 | if (convertToString(Record, 0, ValueName)) |
| 5585 | return error("Invalid record"); |
| 5586 | SourceFileName = ValueName.c_str(); |
| 5587 | break; |
| 5588 | } |
| 5589 | } |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5590 | continue; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5591 | } |
| 5592 | } |
| 5593 | } |
| 5594 | |
| 5595 | // Eagerly parse the entire function summary block (i.e. for all functions |
| 5596 | // in the index). This populates the FunctionSummary objects in |
| 5597 | // the index. |
| 5598 | std::error_code FunctionIndexBitcodeReader::parseEntireSummary() { |
| 5599 | if (Stream.EnterSubBlock(bitc::FUNCTION_SUMMARY_BLOCK_ID)) |
| 5600 | return error("Invalid record"); |
| 5601 | |
| 5602 | SmallVector<uint64_t, 64> Record; |
| 5603 | |
| 5604 | while (1) { |
| 5605 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| 5606 | |
| 5607 | switch (Entry.Kind) { |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5608 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 5609 | case BitstreamEntry::Error: |
| 5610 | return error("Malformed block"); |
| 5611 | case BitstreamEntry::EndBlock: |
| 5612 | return std::error_code(); |
| 5613 | case BitstreamEntry::Record: |
| 5614 | // The interesting case. |
| 5615 | break; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5616 | } |
| 5617 | |
| 5618 | // Read a record. The record format depends on whether this |
| 5619 | // is a per-module index or a combined index file. In the per-module |
| 5620 | // case the records contain the associated value's ID for correlation |
| 5621 | // with VST entries. In the combined index the correlation is done |
| 5622 | // via the bitcode offset of the summary records (which were saved |
| 5623 | // in the combined index VST entries). The records also contain |
| 5624 | // information used for ThinLTO renaming and importing. |
| 5625 | Record.clear(); |
| 5626 | uint64_t CurRecordBit = Stream.GetCurrentBitNo(); |
| 5627 | switch (Stream.readRecord(Entry.ID, Record)) { |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5628 | default: // Default behavior: ignore. |
| 5629 | break; |
Teresa Johnson | 5e22e44 | 2016-02-06 16:07:35 +0000 | [diff] [blame] | 5630 | // FS_PERMODULE_ENTRY: [valueid, linkage, instcount] |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5631 | case bitc::FS_CODE_PERMODULE_ENTRY: { |
| 5632 | unsigned ValueID = Record[0]; |
Teresa Johnson | 5e22e44 | 2016-02-06 16:07:35 +0000 | [diff] [blame] | 5633 | uint64_t RawLinkage = Record[1]; |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5634 | unsigned InstCount = Record[2]; |
| 5635 | std::unique_ptr<FunctionSummary> FS = |
| 5636 | llvm::make_unique<FunctionSummary>(InstCount); |
Teresa Johnson | 5e22e44 | 2016-02-06 16:07:35 +0000 | [diff] [blame] | 5637 | FS->setFunctionLinkage(getDecodedLinkage(RawLinkage)); |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5638 | // The module path string ref set in the summary must be owned by the |
| 5639 | // index's module string table. Since we don't have a module path |
| 5640 | // string table section in the per-module index, we create a single |
| 5641 | // module path string table entry with an empty (0) ID to take |
| 5642 | // ownership. |
| 5643 | FS->setModulePath( |
| 5644 | TheIndex->addModulePath(Buffer->getBufferIdentifier(), 0)); |
| 5645 | SummaryMap[ValueID] = std::move(FS); |
Teresa Johnson | bbe0545 | 2016-02-24 17:57:28 +0000 | [diff] [blame^] | 5646 | break; |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5647 | } |
Teresa Johnson | 5e22e44 | 2016-02-06 16:07:35 +0000 | [diff] [blame] | 5648 | // FS_COMBINED_ENTRY: [modid, linkage, instcount] |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5649 | case bitc::FS_CODE_COMBINED_ENTRY: { |
| 5650 | uint64_t ModuleId = Record[0]; |
Teresa Johnson | 5e22e44 | 2016-02-06 16:07:35 +0000 | [diff] [blame] | 5651 | uint64_t RawLinkage = Record[1]; |
| 5652 | unsigned InstCount = Record[2]; |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5653 | std::unique_ptr<FunctionSummary> FS = |
| 5654 | llvm::make_unique<FunctionSummary>(InstCount); |
Teresa Johnson | 5e22e44 | 2016-02-06 16:07:35 +0000 | [diff] [blame] | 5655 | FS->setFunctionLinkage(getDecodedLinkage(RawLinkage)); |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5656 | FS->setModulePath(ModuleIdMap[ModuleId]); |
| 5657 | SummaryMap[CurRecordBit] = std::move(FS); |
Teresa Johnson | bbe0545 | 2016-02-24 17:57:28 +0000 | [diff] [blame^] | 5658 | break; |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5659 | } |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5660 | } |
| 5661 | } |
| 5662 | llvm_unreachable("Exit infinite loop"); |
| 5663 | } |
| 5664 | |
| 5665 | // Parse the module string table block into the Index. |
| 5666 | // This populates the ModulePathStringTable map in the index. |
| 5667 | std::error_code FunctionIndexBitcodeReader::parseModuleStringTable() { |
| 5668 | if (Stream.EnterSubBlock(bitc::MODULE_STRTAB_BLOCK_ID)) |
| 5669 | return error("Invalid record"); |
| 5670 | |
| 5671 | SmallVector<uint64_t, 64> Record; |
| 5672 | |
| 5673 | SmallString<128> ModulePath; |
| 5674 | while (1) { |
| 5675 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| 5676 | |
| 5677 | switch (Entry.Kind) { |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5678 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 5679 | case BitstreamEntry::Error: |
| 5680 | return error("Malformed block"); |
| 5681 | case BitstreamEntry::EndBlock: |
| 5682 | return std::error_code(); |
| 5683 | case BitstreamEntry::Record: |
| 5684 | // The interesting case. |
| 5685 | break; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5686 | } |
| 5687 | |
| 5688 | Record.clear(); |
| 5689 | switch (Stream.readRecord(Entry.ID, Record)) { |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5690 | default: // Default behavior: ignore. |
| 5691 | break; |
| 5692 | case bitc::MST_CODE_ENTRY: { |
| 5693 | // MST_ENTRY: [modid, namechar x N] |
| 5694 | if (convertToString(Record, 1, ModulePath)) |
| 5695 | return error("Invalid record"); |
| 5696 | uint64_t ModuleId = Record[0]; |
| 5697 | StringRef ModulePathInMap = TheIndex->addModulePath(ModulePath, ModuleId); |
| 5698 | ModuleIdMap[ModuleId] = ModulePathInMap; |
| 5699 | ModulePath.clear(); |
| 5700 | break; |
| 5701 | } |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5702 | } |
| 5703 | } |
| 5704 | llvm_unreachable("Exit infinite loop"); |
| 5705 | } |
| 5706 | |
| 5707 | // Parse the function info index from the bitcode streamer into the given index. |
| 5708 | std::error_code FunctionIndexBitcodeReader::parseSummaryIndexInto( |
| 5709 | std::unique_ptr<DataStreamer> Streamer, FunctionInfoIndex *I) { |
| 5710 | TheIndex = I; |
| 5711 | |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5712 | if (std::error_code EC = initStream(std::move(Streamer))) |
| 5713 | return EC; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5714 | |
| 5715 | // Sniff for the signature. |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5716 | if (!hasValidBitcodeHeader(Stream)) |
| 5717 | return error("Invalid bitcode signature"); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5718 | |
| 5719 | // We expect a number of well-defined blocks, though we don't necessarily |
| 5720 | // need to understand them all. |
| 5721 | while (1) { |
| 5722 | if (Stream.AtEndOfStream()) { |
| 5723 | // We didn't really read a proper Module block. |
| 5724 | return error("Malformed block"); |
| 5725 | } |
| 5726 | |
| 5727 | BitstreamEntry Entry = |
| 5728 | Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs); |
| 5729 | |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5730 | if (Entry.Kind != BitstreamEntry::SubBlock) |
| 5731 | return error("Malformed block"); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5732 | |
| 5733 | // If we see a MODULE_BLOCK, parse it to find the blocks needed for |
| 5734 | // building the function summary index. |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5735 | if (Entry.ID == bitc::MODULE_BLOCK_ID) |
| 5736 | return parseModule(); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5737 | |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5738 | if (Stream.SkipBlock()) |
| 5739 | return error("Invalid record"); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5740 | } |
| 5741 | } |
| 5742 | |
| 5743 | // Parse the function information at the given offset in the buffer into |
| 5744 | // the index. Used to support lazy parsing of function summaries from the |
| 5745 | // combined index during importing. |
| 5746 | // TODO: This function is not yet complete as it won't have a consumer |
| 5747 | // until ThinLTO function importing is added. |
| 5748 | std::error_code FunctionIndexBitcodeReader::parseFunctionSummary( |
| 5749 | std::unique_ptr<DataStreamer> Streamer, FunctionInfoIndex *I, |
| 5750 | size_t FunctionSummaryOffset) { |
| 5751 | TheIndex = I; |
| 5752 | |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5753 | if (std::error_code EC = initStream(std::move(Streamer))) |
| 5754 | return EC; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5755 | |
| 5756 | // Sniff for the signature. |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5757 | if (!hasValidBitcodeHeader(Stream)) |
| 5758 | return error("Invalid bitcode signature"); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5759 | |
| 5760 | Stream.JumpToBit(FunctionSummaryOffset); |
| 5761 | |
| 5762 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| 5763 | |
| 5764 | switch (Entry.Kind) { |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5765 | default: |
| 5766 | return error("Malformed block"); |
| 5767 | case BitstreamEntry::Record: |
| 5768 | // The expected case. |
| 5769 | break; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5770 | } |
| 5771 | |
| 5772 | // TODO: Read a record. This interface will be completed when ThinLTO |
| 5773 | // importing is added so that it can be tested. |
| 5774 | SmallVector<uint64_t, 64> Record; |
| 5775 | switch (Stream.readRecord(Entry.ID, Record)) { |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5776 | case bitc::FS_CODE_COMBINED_ENTRY: |
| 5777 | default: |
| 5778 | return error("Invalid record"); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5779 | } |
| 5780 | |
| 5781 | return std::error_code(); |
| 5782 | } |
| 5783 | |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5784 | std::error_code |
| 5785 | FunctionIndexBitcodeReader::initStream(std::unique_ptr<DataStreamer> Streamer) { |
| 5786 | if (Streamer) |
| 5787 | return initLazyStream(std::move(Streamer)); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5788 | return initStreamFromBuffer(); |
| 5789 | } |
| 5790 | |
| 5791 | std::error_code FunctionIndexBitcodeReader::initStreamFromBuffer() { |
| 5792 | const unsigned char *BufPtr = (const unsigned char *)Buffer->getBufferStart(); |
| 5793 | const unsigned char *BufEnd = BufPtr + Buffer->getBufferSize(); |
| 5794 | |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5795 | if (Buffer->getBufferSize() & 3) |
| 5796 | return error("Invalid bitcode signature"); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5797 | |
| 5798 | // If we have a wrapper header, parse it and ignore the non-bc file contents. |
| 5799 | // The magic number is 0x0B17C0DE stored in little endian. |
| 5800 | if (isBitcodeWrapper(BufPtr, BufEnd)) |
| 5801 | if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true)) |
| 5802 | return error("Invalid bitcode wrapper header"); |
| 5803 | |
| 5804 | StreamFile.reset(new BitstreamReader(BufPtr, BufEnd)); |
| 5805 | Stream.init(&*StreamFile); |
| 5806 | |
| 5807 | return std::error_code(); |
| 5808 | } |
| 5809 | |
| 5810 | std::error_code FunctionIndexBitcodeReader::initLazyStream( |
| 5811 | std::unique_ptr<DataStreamer> Streamer) { |
| 5812 | // Check and strip off the bitcode wrapper; BitstreamReader expects never to |
| 5813 | // see it. |
| 5814 | auto OwnedBytes = |
| 5815 | llvm::make_unique<StreamingMemoryObject>(std::move(Streamer)); |
| 5816 | StreamingMemoryObject &Bytes = *OwnedBytes; |
| 5817 | StreamFile = llvm::make_unique<BitstreamReader>(std::move(OwnedBytes)); |
| 5818 | Stream.init(&*StreamFile); |
| 5819 | |
| 5820 | unsigned char buf[16]; |
| 5821 | if (Bytes.readBytes(buf, 16, 0) != 16) |
| 5822 | return error("Invalid bitcode signature"); |
| 5823 | |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5824 | if (!isBitcode(buf, buf + 16)) |
| 5825 | return error("Invalid bitcode signature"); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5826 | |
| 5827 | if (isBitcodeWrapper(buf, buf + 4)) { |
| 5828 | const unsigned char *bitcodeStart = buf; |
| 5829 | const unsigned char *bitcodeEnd = buf + 16; |
| 5830 | SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false); |
| 5831 | Bytes.dropLeadingBytes(bitcodeStart - buf); |
| 5832 | Bytes.setKnownObjectSize(bitcodeEnd - bitcodeStart); |
| 5833 | } |
| 5834 | return std::error_code(); |
| 5835 | } |
| 5836 | |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 5837 | namespace { |
Rafael Espindola | 25188c9 | 2014-06-12 01:45:43 +0000 | [diff] [blame] | 5838 | class BitcodeErrorCategoryType : public std::error_category { |
Rafael Espindola | f5d07fa | 2014-06-10 21:26:47 +0000 | [diff] [blame] | 5839 | const char *name() const LLVM_NOEXCEPT override { |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 5840 | return "llvm.bitcode"; |
| 5841 | } |
Craig Topper | 7315602 | 2014-03-02 09:09:27 +0000 | [diff] [blame] | 5842 | std::string message(int IE) const override { |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 5843 | BitcodeError E = static_cast<BitcodeError>(IE); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 5844 | switch (E) { |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 5845 | case BitcodeError::InvalidBitcodeSignature: |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 5846 | return "Invalid bitcode signature"; |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 5847 | case BitcodeError::CorruptedBitcode: |
| 5848 | return "Corrupted bitcode"; |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 5849 | } |
Benjamin Kramer | 77db163 | 2013-11-05 13:45:09 +0000 | [diff] [blame] | 5850 | llvm_unreachable("Unknown error type!"); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 5851 | } |
| 5852 | }; |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 5853 | } // end anonymous namespace |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 5854 | |
Chris Bieneman | 770163e | 2014-09-19 20:29:02 +0000 | [diff] [blame] | 5855 | static ManagedStatic<BitcodeErrorCategoryType> ErrorCategory; |
| 5856 | |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 5857 | const std::error_category &llvm::BitcodeErrorCategory() { |
Chris Bieneman | 770163e | 2014-09-19 20:29:02 +0000 | [diff] [blame] | 5858 | return *ErrorCategory; |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 5859 | } |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 5860 | |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 5861 | //===----------------------------------------------------------------------===// |
| 5862 | // External interface |
| 5863 | //===----------------------------------------------------------------------===// |
| 5864 | |
Rafael Espindola | 456baad | 2015-06-17 01:15:47 +0000 | [diff] [blame] | 5865 | static ErrorOr<std::unique_ptr<Module>> |
| 5866 | getBitcodeModuleImpl(std::unique_ptr<DataStreamer> Streamer, StringRef Name, |
| 5867 | BitcodeReader *R, LLVMContext &Context, |
| 5868 | bool MaterializeAll, bool ShouldLazyLoadMetadata) { |
| 5869 | std::unique_ptr<Module> M = make_unique<Module>(Name, Context); |
| 5870 | M->setMaterializer(R); |
| 5871 | |
| 5872 | auto cleanupOnError = [&](std::error_code EC) { |
| 5873 | R->releaseBuffer(); // Never take ownership on error. |
| 5874 | return EC; |
| 5875 | }; |
| 5876 | |
| 5877 | // Delay parsing Metadata if ShouldLazyLoadMetadata is true. |
| 5878 | if (std::error_code EC = R->parseBitcodeInto(std::move(Streamer), M.get(), |
| 5879 | ShouldLazyLoadMetadata)) |
| 5880 | return cleanupOnError(EC); |
| 5881 | |
| 5882 | if (MaterializeAll) { |
| 5883 | // Read in the entire module, and destroy the BitcodeReader. |
Rafael Espindola | c4a0348 | 2015-12-18 20:13:39 +0000 | [diff] [blame] | 5884 | if (std::error_code EC = M->materializeAll()) |
Rafael Espindola | 456baad | 2015-06-17 01:15:47 +0000 | [diff] [blame] | 5885 | return cleanupOnError(EC); |
| 5886 | } else { |
| 5887 | // Resolve forward references from blockaddresses. |
| 5888 | if (std::error_code EC = R->materializeForwardReferencedFunctions()) |
| 5889 | return cleanupOnError(EC); |
| 5890 | } |
| 5891 | return std::move(M); |
| 5892 | } |
| 5893 | |
Duncan P. N. Exon Smith | 6e1009b | 2014-08-01 22:27:19 +0000 | [diff] [blame] | 5894 | /// \brief Get a lazy one-at-time loading module from bitcode. |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 5895 | /// |
Duncan P. N. Exon Smith | 6e1009b | 2014-08-01 22:27:19 +0000 | [diff] [blame] | 5896 | /// This isn't always used in a lazy context. In particular, it's also used by |
| 5897 | /// \a parseBitcodeFile(). If this is truly lazy, then we need to eagerly pull |
| 5898 | /// in forward-referenced functions from block address references. |
| 5899 | /// |
Rafael Espindola | 728074b | 2015-06-17 00:40:56 +0000 | [diff] [blame] | 5900 | /// \param[in] MaterializeAll Set to \c true if we should materialize |
| 5901 | /// everything. |
Rafael Espindola | dcd1dca | 2015-06-16 22:27:55 +0000 | [diff] [blame] | 5902 | static ErrorOr<std::unique_ptr<Module>> |
Rafael Espindola | 6881215 | 2014-09-03 17:31:46 +0000 | [diff] [blame] | 5903 | getLazyBitcodeModuleImpl(std::unique_ptr<MemoryBuffer> &&Buffer, |
Rafael Espindola | 728074b | 2015-06-17 00:40:56 +0000 | [diff] [blame] | 5904 | LLVMContext &Context, bool MaterializeAll, |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 5905 | bool ShouldLazyLoadMetadata = false) { |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 5906 | BitcodeReader *R = new BitcodeReader(Buffer.get(), Context); |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 5907 | |
Rafael Espindola | 456baad | 2015-06-17 01:15:47 +0000 | [diff] [blame] | 5908 | ErrorOr<std::unique_ptr<Module>> Ret = |
| 5909 | getBitcodeModuleImpl(nullptr, Buffer->getBufferIdentifier(), R, Context, |
| 5910 | MaterializeAll, ShouldLazyLoadMetadata); |
| 5911 | if (!Ret) |
| 5912 | return Ret; |
Rafael Espindola | b799346 | 2012-01-02 07:49:53 +0000 | [diff] [blame] | 5913 | |
Rafael Espindola | e2c1d77 | 2014-08-26 22:00:09 +0000 | [diff] [blame] | 5914 | Buffer.release(); // The BitcodeReader owns it now. |
Rafael Espindola | 456baad | 2015-06-17 01:15:47 +0000 | [diff] [blame] | 5915 | return Ret; |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 5916 | } |
| 5917 | |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 5918 | ErrorOr<std::unique_ptr<Module>> |
| 5919 | llvm::getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer, |
| 5920 | LLVMContext &Context, bool ShouldLazyLoadMetadata) { |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 5921 | return getLazyBitcodeModuleImpl(std::move(Buffer), Context, false, |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 5922 | ShouldLazyLoadMetadata); |
Duncan P. N. Exon Smith | 6e1009b | 2014-08-01 22:27:19 +0000 | [diff] [blame] | 5923 | } |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 5924 | |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 5925 | ErrorOr<std::unique_ptr<Module>> |
| 5926 | llvm::getStreamedBitcodeModule(StringRef Name, |
| 5927 | std::unique_ptr<DataStreamer> Streamer, |
| 5928 | LLVMContext &Context) { |
Rafael Espindola | 7d727b5 | 2014-12-18 05:08:43 +0000 | [diff] [blame] | 5929 | std::unique_ptr<Module> M = make_unique<Module>(Name, Context); |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 5930 | BitcodeReader *R = new BitcodeReader(Context); |
Rafael Espindola | 456baad | 2015-06-17 01:15:47 +0000 | [diff] [blame] | 5931 | |
| 5932 | return getBitcodeModuleImpl(std::move(Streamer), Name, R, Context, false, |
| 5933 | false); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 5934 | } |
| 5935 | |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 5936 | ErrorOr<std::unique_ptr<Module>> llvm::parseBitcodeFile(MemoryBufferRef Buffer, |
| 5937 | LLVMContext &Context) { |
Rafael Espindola | d96d553 | 2014-08-26 21:49:01 +0000 | [diff] [blame] | 5938 | std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false); |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 5939 | return getLazyBitcodeModuleImpl(std::move(Buf), Context, true); |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 5940 | // TODO: Restore the use-lists to the in-memory state when the bitcode was |
| 5941 | // written. We must defer until the Module has been fully materialized. |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 5942 | } |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 5943 | |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 5944 | std::string llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer, |
| 5945 | LLVMContext &Context) { |
Rafael Espindola | d96d553 | 2014-08-26 21:49:01 +0000 | [diff] [blame] | 5946 | std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false); |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 5947 | auto R = llvm::make_unique<BitcodeReader>(Buf.release(), Context); |
Rafael Espindola | c75c4fa | 2014-07-04 20:02:42 +0000 | [diff] [blame] | 5948 | ErrorOr<std::string> Triple = R->parseTriple(); |
Rafael Espindola | d346cc8 | 2014-07-04 13:52:01 +0000 | [diff] [blame] | 5949 | if (Triple.getError()) |
| 5950 | return ""; |
| 5951 | return Triple.get(); |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 5952 | } |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5953 | |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 5954 | std::string llvm::getBitcodeProducerString(MemoryBufferRef Buffer, |
| 5955 | LLVMContext &Context) { |
Mehdi Amini | 3383ccc | 2015-11-09 02:46:41 +0000 | [diff] [blame] | 5956 | std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false); |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 5957 | BitcodeReader R(Buf.release(), Context); |
Mehdi Amini | 3383ccc | 2015-11-09 02:46:41 +0000 | [diff] [blame] | 5958 | ErrorOr<std::string> ProducerString = R.parseIdentificationBlock(); |
| 5959 | if (ProducerString.getError()) |
| 5960 | return ""; |
| 5961 | return ProducerString.get(); |
| 5962 | } |
| 5963 | |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5964 | // Parse the specified bitcode buffer, returning the function info index. |
| 5965 | // If IsLazy is false, parse the entire function summary into |
| 5966 | // the index. Otherwise skip the function summary section, and only create |
| 5967 | // an index object with a map from function name to function summary offset. |
| 5968 | // The index is used to perform lazy function summary reading later. |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5969 | ErrorOr<std::unique_ptr<FunctionInfoIndex>> |
Mehdi Amini | 354f520 | 2015-11-19 05:52:29 +0000 | [diff] [blame] | 5970 | llvm::getFunctionInfoIndex(MemoryBufferRef Buffer, |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5971 | DiagnosticHandlerFunction DiagnosticHandler, |
Mehdi Amini | 9abe108 | 2015-12-03 02:37:23 +0000 | [diff] [blame] | 5972 | bool IsLazy) { |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5973 | std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false); |
Mehdi Amini | 354f520 | 2015-11-19 05:52:29 +0000 | [diff] [blame] | 5974 | FunctionIndexBitcodeReader R(Buf.get(), DiagnosticHandler, IsLazy); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5975 | |
Mehdi Amini | 9abe108 | 2015-12-03 02:37:23 +0000 | [diff] [blame] | 5976 | auto Index = llvm::make_unique<FunctionInfoIndex>(); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5977 | |
| 5978 | auto cleanupOnError = [&](std::error_code EC) { |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5979 | R.releaseBuffer(); // Never take ownership on error. |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5980 | return EC; |
| 5981 | }; |
| 5982 | |
| 5983 | if (std::error_code EC = R.parseSummaryIndexInto(nullptr, Index.get())) |
| 5984 | return cleanupOnError(EC); |
| 5985 | |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5986 | Buf.release(); // The FunctionIndexBitcodeReader owns it now. |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5987 | return std::move(Index); |
| 5988 | } |
| 5989 | |
| 5990 | // Check if the given bitcode buffer contains a function summary block. |
Mehdi Amini | 354f520 | 2015-11-19 05:52:29 +0000 | [diff] [blame] | 5991 | bool llvm::hasFunctionSummary(MemoryBufferRef Buffer, |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5992 | DiagnosticHandlerFunction DiagnosticHandler) { |
| 5993 | std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false); |
Mehdi Amini | 354f520 | 2015-11-19 05:52:29 +0000 | [diff] [blame] | 5994 | FunctionIndexBitcodeReader R(Buf.get(), DiagnosticHandler, false, true); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5995 | |
| 5996 | auto cleanupOnError = [&](std::error_code EC) { |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5997 | R.releaseBuffer(); // Never take ownership on error. |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5998 | return false; |
| 5999 | }; |
| 6000 | |
| 6001 | if (std::error_code EC = R.parseSummaryIndexInto(nullptr, nullptr)) |
| 6002 | return cleanupOnError(EC); |
| 6003 | |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 6004 | Buf.release(); // The FunctionIndexBitcodeReader owns it now. |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 6005 | return R.foundFuncSummary(); |
| 6006 | } |
| 6007 | |
| 6008 | // This method supports lazy reading of function summary data from the combined |
| 6009 | // index during ThinLTO function importing. When reading the combined index |
| 6010 | // file, getFunctionInfoIndex is first invoked with IsLazy=true. |
| 6011 | // Then this method is called for each function considered for importing, |
| 6012 | // to parse the summary information for the given function name into |
| 6013 | // the index. |
Mehdi Amini | 354f520 | 2015-11-19 05:52:29 +0000 | [diff] [blame] | 6014 | std::error_code llvm::readFunctionSummary( |
| 6015 | MemoryBufferRef Buffer, DiagnosticHandlerFunction DiagnosticHandler, |
| 6016 | StringRef FunctionName, std::unique_ptr<FunctionInfoIndex> Index) { |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 6017 | std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false); |
Mehdi Amini | 354f520 | 2015-11-19 05:52:29 +0000 | [diff] [blame] | 6018 | FunctionIndexBitcodeReader R(Buf.get(), DiagnosticHandler); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 6019 | |
| 6020 | auto cleanupOnError = [&](std::error_code EC) { |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 6021 | R.releaseBuffer(); // Never take ownership on error. |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 6022 | return EC; |
| 6023 | }; |
| 6024 | |
| 6025 | // Lookup the given function name in the FunctionMap, which may |
| 6026 | // contain a list of function infos in the case of a COMDAT. Walk through |
| 6027 | // and parse each function summary info at the function summary offset |
| 6028 | // recorded when parsing the value symbol table. |
| 6029 | for (const auto &FI : Index->getFunctionInfoList(FunctionName)) { |
| 6030 | size_t FunctionSummaryOffset = FI->bitcodeIndex(); |
| 6031 | if (std::error_code EC = |
| 6032 | R.parseFunctionSummary(nullptr, Index.get(), FunctionSummaryOffset)) |
| 6033 | return cleanupOnError(EC); |
| 6034 | } |
| 6035 | |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 6036 | Buf.release(); // The FunctionIndexBitcodeReader owns it now. |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 6037 | return std::error_code(); |
| 6038 | } |