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" |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 30 | #include "llvm/IR/ValueHandle.h" |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 31 | #include "llvm/Support/DataStream.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 32 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 33 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 34 | #include "llvm/Support/MemoryBuffer.h" |
Tobias Grosser | 0a8e12f | 2013-07-26 04:16:55 +0000 | [diff] [blame] | 35 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 36 | #include <deque> |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 37 | using namespace llvm; |
| 38 | |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 39 | namespace { |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 40 | enum { |
| 41 | SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex |
| 42 | }; |
| 43 | |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 44 | class BitcodeReaderValueList { |
| 45 | std::vector<WeakVH> ValuePtrs; |
| 46 | |
| 47 | /// ResolveConstants - As we resolve forward-referenced constants, we add |
| 48 | /// information about them to this vector. This allows us to resolve them in |
| 49 | /// bulk instead of resolving each reference at a time. See the code in |
| 50 | /// ResolveConstantForwardRefs for more information about this. |
| 51 | /// |
| 52 | /// The key of this vector is the placeholder constant, the value is the slot |
| 53 | /// number that holds the resolved value. |
| 54 | typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy; |
| 55 | ResolveConstantsTy ResolveConstants; |
| 56 | LLVMContext &Context; |
| 57 | public: |
| 58 | BitcodeReaderValueList(LLVMContext &C) : Context(C) {} |
| 59 | ~BitcodeReaderValueList() { |
| 60 | assert(ResolveConstants.empty() && "Constants not resolved?"); |
| 61 | } |
| 62 | |
| 63 | // vector compatibility methods |
| 64 | unsigned size() const { return ValuePtrs.size(); } |
| 65 | void resize(unsigned N) { ValuePtrs.resize(N); } |
| 66 | void push_back(Value *V) { |
| 67 | ValuePtrs.push_back(V); |
| 68 | } |
| 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); |
| 89 | Value *getValueFwdRef(unsigned Idx, Type *Ty); |
| 90 | |
| 91 | void AssignValue(Value *V, unsigned Idx); |
| 92 | |
| 93 | /// ResolveConstantForwardRefs - Once all constants are read, this method bulk |
| 94 | /// resolves any forward references. |
| 95 | void ResolveConstantForwardRefs(); |
| 96 | }; |
| 97 | |
| 98 | class BitcodeReaderMDValueList { |
| 99 | unsigned NumFwdRefs; |
| 100 | bool AnyFwdRefs; |
| 101 | unsigned MinFwdRef; |
| 102 | unsigned MaxFwdRef; |
| 103 | std::vector<TrackingMDRef> MDValuePtrs; |
| 104 | |
| 105 | LLVMContext &Context; |
| 106 | public: |
| 107 | BitcodeReaderMDValueList(LLVMContext &C) |
| 108 | : NumFwdRefs(0), AnyFwdRefs(false), Context(C) {} |
| 109 | |
| 110 | // vector compatibility methods |
| 111 | unsigned size() const { return MDValuePtrs.size(); } |
| 112 | void resize(unsigned N) { MDValuePtrs.resize(N); } |
| 113 | void push_back(Metadata *MD) { MDValuePtrs.emplace_back(MD); } |
| 114 | void clear() { MDValuePtrs.clear(); } |
| 115 | Metadata *back() const { return MDValuePtrs.back(); } |
| 116 | void pop_back() { MDValuePtrs.pop_back(); } |
| 117 | bool empty() const { return MDValuePtrs.empty(); } |
| 118 | |
| 119 | Metadata *operator[](unsigned i) const { |
| 120 | assert(i < MDValuePtrs.size()); |
| 121 | return MDValuePtrs[i]; |
| 122 | } |
| 123 | |
| 124 | void shrinkTo(unsigned N) { |
| 125 | assert(N <= size() && "Invalid shrinkTo request!"); |
| 126 | MDValuePtrs.resize(N); |
| 127 | } |
| 128 | |
| 129 | Metadata *getValueFwdRef(unsigned Idx); |
| 130 | void AssignValue(Metadata *MD, unsigned Idx); |
| 131 | void tryToResolveCycles(); |
| 132 | }; |
| 133 | |
| 134 | class BitcodeReader : public GVMaterializer { |
| 135 | LLVMContext &Context; |
| 136 | DiagnosticHandlerFunction DiagnosticHandler; |
| 137 | Module *TheModule; |
| 138 | std::unique_ptr<MemoryBuffer> Buffer; |
| 139 | std::unique_ptr<BitstreamReader> StreamFile; |
| 140 | BitstreamCursor Stream; |
| 141 | DataStreamer *LazyStreamer; |
| 142 | uint64_t NextUnreadBit; |
| 143 | bool SeenValueSymbolTable; |
| 144 | |
| 145 | std::vector<Type*> TypeList; |
| 146 | BitcodeReaderValueList ValueList; |
| 147 | BitcodeReaderMDValueList MDValueList; |
| 148 | std::vector<Comdat *> ComdatList; |
| 149 | SmallVector<Instruction *, 64> InstructionList; |
| 150 | |
| 151 | std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits; |
| 152 | std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits; |
| 153 | std::vector<std::pair<Function*, unsigned> > FunctionPrefixes; |
| 154 | std::vector<std::pair<Function*, unsigned> > FunctionPrologues; |
| 155 | |
| 156 | SmallVector<Instruction*, 64> InstsWithTBAATag; |
| 157 | |
| 158 | /// MAttributes - The set of attributes by index. Index zero in the |
| 159 | /// file is for null, and is thus not represented here. As such all indices |
| 160 | /// are off by one. |
| 161 | std::vector<AttributeSet> MAttributes; |
| 162 | |
| 163 | /// \brief The set of attribute groups. |
| 164 | std::map<unsigned, AttributeSet> MAttributeGroups; |
| 165 | |
| 166 | /// FunctionBBs - While parsing a function body, this is a list of the basic |
| 167 | /// blocks for the function. |
| 168 | std::vector<BasicBlock*> FunctionBBs; |
| 169 | |
| 170 | // When reading the module header, this list is populated with functions that |
| 171 | // have bodies later in the file. |
| 172 | std::vector<Function*> FunctionsWithBodies; |
| 173 | |
| 174 | // When intrinsic functions are encountered which require upgrading they are |
| 175 | // stored here with their replacement function. |
| 176 | typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap; |
| 177 | UpgradedIntrinsicMap UpgradedIntrinsics; |
| 178 | |
| 179 | // Map the bitcode's custom MDKind ID to the Module's MDKind ID. |
| 180 | DenseMap<unsigned, unsigned> MDKindMap; |
| 181 | |
| 182 | // Several operations happen after the module header has been read, but |
| 183 | // before function bodies are processed. This keeps track of whether |
| 184 | // we've done this yet. |
| 185 | bool SeenFirstFunctionBody; |
| 186 | |
| 187 | /// DeferredFunctionInfo - When function bodies are initially scanned, this |
| 188 | /// map contains info about where to find deferred function body in the |
| 189 | /// stream. |
| 190 | DenseMap<Function*, uint64_t> DeferredFunctionInfo; |
| 191 | |
| 192 | /// When Metadata block is initially scanned when parsing the module, we may |
| 193 | /// choose to defer parsing of the metadata. This vector contains info about |
| 194 | /// which Metadata blocks are deferred. |
| 195 | std::vector<uint64_t> DeferredMetadataInfo; |
| 196 | |
| 197 | /// These are basic blocks forward-referenced by block addresses. They are |
| 198 | /// inserted lazily into functions when they're loaded. The basic block ID is |
| 199 | /// its index into the vector. |
| 200 | DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs; |
| 201 | std::deque<Function *> BasicBlockFwdRefQueue; |
| 202 | |
| 203 | /// UseRelativeIDs - Indicates that we are using a new encoding for |
| 204 | /// instruction operands where most operands in the current |
| 205 | /// FUNCTION_BLOCK are encoded relative to the instruction number, |
| 206 | /// for a more compact encoding. Some instruction operands are not |
| 207 | /// relative to the instruction ID: basic block numbers, and types. |
| 208 | /// Once the old style function blocks have been phased out, we would |
| 209 | /// not need this flag. |
| 210 | bool UseRelativeIDs; |
| 211 | |
| 212 | /// True if all functions will be materialized, negating the need to process |
| 213 | /// (e.g.) blockaddress forward references. |
| 214 | bool WillMaterializeAllForwardRefs; |
| 215 | |
| 216 | /// Functions that have block addresses taken. This is usually empty. |
| 217 | SmallPtrSet<const Function *, 4> BlockAddressesTaken; |
| 218 | |
| 219 | /// True if any Metadata block has been materialized. |
| 220 | bool IsMetadataMaterialized; |
| 221 | |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 222 | bool StripDebugInfo = false; |
| 223 | |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 224 | public: |
| 225 | std::error_code Error(BitcodeError E, const Twine &Message); |
| 226 | std::error_code Error(BitcodeError E); |
| 227 | std::error_code Error(const Twine &Message); |
| 228 | |
| 229 | explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C, |
| 230 | DiagnosticHandlerFunction DiagnosticHandler); |
| 231 | explicit BitcodeReader(DataStreamer *streamer, LLVMContext &C, |
| 232 | DiagnosticHandlerFunction DiagnosticHandler); |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 233 | ~BitcodeReader() override { FreeState(); } |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 234 | |
| 235 | std::error_code materializeForwardReferencedFunctions(); |
| 236 | |
| 237 | void FreeState(); |
| 238 | |
| 239 | void releaseBuffer(); |
| 240 | |
| 241 | bool isDematerializable(const GlobalValue *GV) const override; |
| 242 | std::error_code materialize(GlobalValue *GV) override; |
| 243 | std::error_code MaterializeModule(Module *M) override; |
| 244 | std::vector<StructType *> getIdentifiedStructTypes() const override; |
| 245 | void Dematerialize(GlobalValue *GV) override; |
| 246 | |
| 247 | /// @brief Main interface to parsing a bitcode buffer. |
| 248 | /// @returns true if an error occurred. |
| 249 | std::error_code ParseBitcodeInto(Module *M, |
| 250 | bool ShouldLazyLoadMetadata = false); |
| 251 | |
| 252 | /// @brief Cheap mechanism to just extract module triple |
| 253 | /// @returns true if an error occurred. |
| 254 | ErrorOr<std::string> parseTriple(); |
| 255 | |
| 256 | static uint64_t decodeSignRotatedValue(uint64_t V); |
| 257 | |
| 258 | /// Materialize any deferred Metadata block. |
| 259 | std::error_code materializeMetadata() override; |
| 260 | |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 261 | void setStripDebugInfo() override; |
| 262 | |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 263 | private: |
| 264 | std::vector<StructType *> IdentifiedStructTypes; |
| 265 | StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name); |
| 266 | StructType *createIdentifiedStructType(LLVMContext &Context); |
| 267 | |
| 268 | Type *getTypeByID(unsigned ID); |
| 269 | Value *getFnValueByID(unsigned ID, Type *Ty) { |
| 270 | if (Ty && Ty->isMetadataTy()) |
| 271 | return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID)); |
| 272 | return ValueList.getValueFwdRef(ID, Ty); |
| 273 | } |
| 274 | Metadata *getFnMetadataByID(unsigned ID) { |
| 275 | return MDValueList.getValueFwdRef(ID); |
| 276 | } |
| 277 | BasicBlock *getBasicBlock(unsigned ID) const { |
| 278 | if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID |
| 279 | return FunctionBBs[ID]; |
| 280 | } |
| 281 | AttributeSet getAttributes(unsigned i) const { |
| 282 | if (i-1 < MAttributes.size()) |
| 283 | return MAttributes[i-1]; |
| 284 | return AttributeSet(); |
| 285 | } |
| 286 | |
| 287 | /// getValueTypePair - Read a value/type pair out of the specified record from |
| 288 | /// slot 'Slot'. Increment Slot past the number of slots used in the record. |
| 289 | /// Return true on failure. |
| 290 | bool getValueTypePair(SmallVectorImpl<uint64_t> &Record, unsigned &Slot, |
| 291 | unsigned InstNum, Value *&ResVal) { |
| 292 | if (Slot == Record.size()) return true; |
| 293 | unsigned ValNo = (unsigned)Record[Slot++]; |
| 294 | // Adjust the ValNo, if it was encoded relative to the InstNum. |
| 295 | if (UseRelativeIDs) |
| 296 | ValNo = InstNum - ValNo; |
| 297 | if (ValNo < InstNum) { |
| 298 | // If this is not a forward reference, just return the value we already |
| 299 | // have. |
| 300 | ResVal = getFnValueByID(ValNo, nullptr); |
| 301 | return ResVal == nullptr; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 302 | } |
David Blaikie | dbe6e0f | 2015-04-17 06:40:14 +0000 | [diff] [blame] | 303 | if (Slot == Record.size()) |
| 304 | return true; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 305 | |
| 306 | unsigned TypeNo = (unsigned)Record[Slot++]; |
| 307 | ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo)); |
| 308 | return ResVal == nullptr; |
| 309 | } |
| 310 | |
| 311 | /// popValue - Read a value out of the specified record from slot 'Slot'. |
| 312 | /// Increment Slot past the number of slots used by the value in the record. |
| 313 | /// Return true if there is an error. |
| 314 | bool popValue(SmallVectorImpl<uint64_t> &Record, unsigned &Slot, |
| 315 | unsigned InstNum, Type *Ty, Value *&ResVal) { |
| 316 | if (getValue(Record, Slot, InstNum, Ty, ResVal)) |
| 317 | return true; |
| 318 | // All values currently take a single record slot. |
| 319 | ++Slot; |
| 320 | return false; |
| 321 | } |
| 322 | |
| 323 | /// getValue -- Like popValue, but does not increment the Slot number. |
| 324 | bool getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot, |
| 325 | unsigned InstNum, Type *Ty, Value *&ResVal) { |
| 326 | ResVal = getValue(Record, Slot, InstNum, Ty); |
| 327 | return ResVal == nullptr; |
| 328 | } |
| 329 | |
| 330 | /// getValue -- Version of getValue that returns ResVal directly, |
| 331 | /// or 0 if there is an error. |
| 332 | Value *getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot, |
| 333 | unsigned InstNum, Type *Ty) { |
| 334 | if (Slot == Record.size()) return nullptr; |
| 335 | unsigned ValNo = (unsigned)Record[Slot]; |
| 336 | // Adjust the ValNo, if it was encoded relative to the InstNum. |
| 337 | if (UseRelativeIDs) |
| 338 | ValNo = InstNum - ValNo; |
| 339 | return getFnValueByID(ValNo, Ty); |
| 340 | } |
| 341 | |
| 342 | /// getValueSigned -- Like getValue, but decodes signed VBRs. |
| 343 | Value *getValueSigned(SmallVectorImpl<uint64_t> &Record, unsigned Slot, |
| 344 | unsigned InstNum, Type *Ty) { |
| 345 | if (Slot == Record.size()) return nullptr; |
| 346 | unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]); |
| 347 | // Adjust the ValNo, if it was encoded relative to the InstNum. |
| 348 | if (UseRelativeIDs) |
| 349 | ValNo = InstNum - ValNo; |
| 350 | return getFnValueByID(ValNo, Ty); |
| 351 | } |
| 352 | |
| 353 | /// Converts alignment exponent (i.e. power of two (or zero)) to the |
| 354 | /// corresponding alignment to use. If alignment is too large, returns |
| 355 | /// a corresponding error code. |
| 356 | std::error_code parseAlignmentValue(uint64_t Exponent, unsigned &Alignment); |
| 357 | std::error_code ParseAttrKind(uint64_t Code, Attribute::AttrKind *Kind); |
| 358 | std::error_code ParseModule(bool Resume, bool ShouldLazyLoadMetadata = false); |
| 359 | std::error_code ParseAttributeBlock(); |
| 360 | std::error_code ParseAttributeGroupBlock(); |
| 361 | std::error_code ParseTypeTable(); |
| 362 | std::error_code ParseTypeTableBody(); |
| 363 | |
| 364 | std::error_code ParseValueSymbolTable(); |
| 365 | std::error_code ParseConstants(); |
| 366 | std::error_code RememberAndSkipFunctionBody(); |
| 367 | /// Save the positions of the Metadata blocks and skip parsing the blocks. |
| 368 | std::error_code rememberAndSkipMetadata(); |
| 369 | std::error_code ParseFunctionBody(Function *F); |
| 370 | std::error_code GlobalCleanup(); |
| 371 | std::error_code ResolveGlobalAndAliasInits(); |
| 372 | std::error_code ParseMetadata(); |
| 373 | std::error_code ParseMetadataAttachment(); |
| 374 | ErrorOr<std::string> parseModuleTriple(); |
| 375 | std::error_code ParseUseLists(); |
| 376 | std::error_code InitStream(); |
| 377 | std::error_code InitStreamFromBuffer(); |
| 378 | std::error_code InitLazyStream(); |
| 379 | std::error_code FindFunctionInStream( |
| 380 | Function *F, |
| 381 | DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator); |
| 382 | }; |
| 383 | } // namespace |
| 384 | |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 385 | BitcodeDiagnosticInfo::BitcodeDiagnosticInfo(std::error_code EC, |
| 386 | DiagnosticSeverity Severity, |
| 387 | const Twine &Msg) |
| 388 | : DiagnosticInfo(DK_Bitcode, Severity), Msg(Msg), EC(EC) {} |
| 389 | |
| 390 | void BitcodeDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; } |
| 391 | |
| 392 | static std::error_code Error(DiagnosticHandlerFunction DiagnosticHandler, |
| 393 | std::error_code EC, const Twine &Message) { |
| 394 | BitcodeDiagnosticInfo DI(EC, DS_Error, Message); |
| 395 | DiagnosticHandler(DI); |
| 396 | return EC; |
| 397 | } |
| 398 | |
| 399 | static std::error_code Error(DiagnosticHandlerFunction DiagnosticHandler, |
| 400 | std::error_code EC) { |
| 401 | return Error(DiagnosticHandler, EC, EC.message()); |
| 402 | } |
| 403 | |
| 404 | std::error_code BitcodeReader::Error(BitcodeError E, const Twine &Message) { |
| 405 | return ::Error(DiagnosticHandler, make_error_code(E), Message); |
| 406 | } |
| 407 | |
| 408 | std::error_code BitcodeReader::Error(const Twine &Message) { |
| 409 | return ::Error(DiagnosticHandler, |
| 410 | make_error_code(BitcodeError::CorruptedBitcode), Message); |
| 411 | } |
| 412 | |
| 413 | std::error_code BitcodeReader::Error(BitcodeError E) { |
| 414 | return ::Error(DiagnosticHandler, make_error_code(E)); |
| 415 | } |
| 416 | |
| 417 | static DiagnosticHandlerFunction getDiagHandler(DiagnosticHandlerFunction F, |
| 418 | LLVMContext &C) { |
| 419 | if (F) |
| 420 | return F; |
| 421 | return [&C](const DiagnosticInfo &DI) { C.diagnose(DI); }; |
| 422 | } |
| 423 | |
| 424 | BitcodeReader::BitcodeReader(MemoryBuffer *buffer, LLVMContext &C, |
| 425 | DiagnosticHandlerFunction DiagnosticHandler) |
| 426 | : Context(C), DiagnosticHandler(getDiagHandler(DiagnosticHandler, C)), |
| 427 | TheModule(nullptr), Buffer(buffer), LazyStreamer(nullptr), |
| 428 | NextUnreadBit(0), SeenValueSymbolTable(false), ValueList(C), |
| 429 | MDValueList(C), SeenFirstFunctionBody(false), UseRelativeIDs(false), |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 430 | WillMaterializeAllForwardRefs(false), IsMetadataMaterialized(false) {} |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 431 | |
| 432 | BitcodeReader::BitcodeReader(DataStreamer *streamer, LLVMContext &C, |
| 433 | DiagnosticHandlerFunction DiagnosticHandler) |
| 434 | : Context(C), DiagnosticHandler(getDiagHandler(DiagnosticHandler, C)), |
| 435 | TheModule(nullptr), Buffer(nullptr), LazyStreamer(streamer), |
| 436 | NextUnreadBit(0), SeenValueSymbolTable(false), ValueList(C), |
| 437 | MDValueList(C), SeenFirstFunctionBody(false), UseRelativeIDs(false), |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 438 | WillMaterializeAllForwardRefs(false), IsMetadataMaterialized(false) {} |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 439 | |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 440 | std::error_code BitcodeReader::materializeForwardReferencedFunctions() { |
| 441 | if (WillMaterializeAllForwardRefs) |
| 442 | return std::error_code(); |
| 443 | |
| 444 | // Prevent recursion. |
| 445 | WillMaterializeAllForwardRefs = true; |
| 446 | |
Duncan P. N. Exon Smith | 5a511b5 | 2014-08-05 17:49:48 +0000 | [diff] [blame] | 447 | while (!BasicBlockFwdRefQueue.empty()) { |
| 448 | Function *F = BasicBlockFwdRefQueue.front(); |
| 449 | BasicBlockFwdRefQueue.pop_front(); |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 450 | assert(F && "Expected valid function"); |
Duncan P. N. Exon Smith | 5a511b5 | 2014-08-05 17:49:48 +0000 | [diff] [blame] | 451 | if (!BasicBlockFwdRefs.count(F)) |
| 452 | // Already materialized. |
| 453 | continue; |
| 454 | |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 455 | // Check for a function that isn't materializable to prevent an infinite |
| 456 | // loop. When parsing a blockaddress stored in a global variable, there |
| 457 | // isn't a trivial way to check if a function will have a body without a |
| 458 | // linear search through FunctionsWithBodies, so just check it here. |
| 459 | if (!F->isMaterializable()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 460 | return Error("Never resolved function from blockaddress"); |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 461 | |
| 462 | // Try to materialize F. |
Rafael Espindola | 5a52e6d | 2014-10-24 22:50:48 +0000 | [diff] [blame] | 463 | if (std::error_code EC = materialize(F)) |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 464 | return EC; |
Rafael Espindola | b799346 | 2012-01-02 07:49:53 +0000 | [diff] [blame] | 465 | } |
Duncan P. N. Exon Smith | 5a511b5 | 2014-08-05 17:49:48 +0000 | [diff] [blame] | 466 | assert(BasicBlockFwdRefs.empty() && "Function missing from queue"); |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 467 | |
| 468 | // Reset state. |
| 469 | WillMaterializeAllForwardRefs = false; |
| 470 | return std::error_code(); |
Rafael Espindola | b799346 | 2012-01-02 07:49:53 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 473 | void BitcodeReader::FreeState() { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 474 | Buffer = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 475 | std::vector<Type*>().swap(TypeList); |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 476 | ValueList.clear(); |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 477 | MDValueList.clear(); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 478 | std::vector<Comdat *>().swap(ComdatList); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 479 | |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 480 | std::vector<AttributeSet>().swap(MAttributes); |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 481 | std::vector<BasicBlock*>().swap(FunctionBBs); |
| 482 | std::vector<Function*>().swap(FunctionsWithBodies); |
| 483 | DeferredFunctionInfo.clear(); |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 484 | DeferredMetadataInfo.clear(); |
Dan Gohman | 43aa8f0 | 2010-07-20 21:42:28 +0000 | [diff] [blame] | 485 | MDKindMap.clear(); |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 486 | |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 487 | assert(BasicBlockFwdRefs.empty() && "Unresolved blockaddress fwd references"); |
Duncan P. N. Exon Smith | 5a511b5 | 2014-08-05 17:49:48 +0000 | [diff] [blame] | 488 | BasicBlockFwdRefQueue.clear(); |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 491 | //===----------------------------------------------------------------------===// |
| 492 | // Helper functions to implement forward reference resolution, etc. |
| 493 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 494 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 495 | /// ConvertToString - Convert a string from a record into an std::string, return |
| 496 | /// true on failure. |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 497 | template<typename StrTy> |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 498 | static bool ConvertToString(ArrayRef<uint64_t> Record, unsigned Idx, |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 499 | StrTy &Result) { |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 500 | if (Idx > Record.size()) |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 501 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 502 | |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 503 | for (unsigned i = Idx, e = Record.size(); i != e; ++i) |
| 504 | Result += (char)Record[i]; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 505 | return false; |
| 506 | } |
| 507 | |
Rafael Espindola | 12ca34f | 2015-01-19 15:16:06 +0000 | [diff] [blame] | 508 | static bool hasImplicitComdat(size_t Val) { |
| 509 | switch (Val) { |
| 510 | default: |
| 511 | return false; |
| 512 | case 1: // Old WeakAnyLinkage |
| 513 | case 4: // Old LinkOnceAnyLinkage |
| 514 | case 10: // Old WeakODRLinkage |
| 515 | case 11: // Old LinkOnceODRLinkage |
| 516 | return true; |
| 517 | } |
| 518 | } |
| 519 | |
Rafael Espindola | 7b4b2dc | 2015-01-08 15:36:32 +0000 | [diff] [blame] | 520 | static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) { |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 521 | switch (Val) { |
| 522 | default: // Map unknown/new linkages to external |
Rafael Espindola | 7b4b2dc | 2015-01-08 15:36:32 +0000 | [diff] [blame] | 523 | case 0: |
| 524 | return GlobalValue::ExternalLinkage; |
Rafael Espindola | 7b4b2dc | 2015-01-08 15:36:32 +0000 | [diff] [blame] | 525 | case 2: |
| 526 | return GlobalValue::AppendingLinkage; |
| 527 | case 3: |
| 528 | return GlobalValue::InternalLinkage; |
Rafael Espindola | 7b4b2dc | 2015-01-08 15:36:32 +0000 | [diff] [blame] | 529 | case 5: |
| 530 | return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage |
| 531 | case 6: |
| 532 | return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage |
| 533 | case 7: |
| 534 | return GlobalValue::ExternalWeakLinkage; |
| 535 | case 8: |
| 536 | return GlobalValue::CommonLinkage; |
| 537 | case 9: |
| 538 | return GlobalValue::PrivateLinkage; |
Rafael Espindola | 7b4b2dc | 2015-01-08 15:36:32 +0000 | [diff] [blame] | 539 | case 12: |
| 540 | return GlobalValue::AvailableExternallyLinkage; |
Rafael Espindola | 2fb5bc3 | 2014-03-13 23:18:37 +0000 | [diff] [blame] | 541 | case 13: |
| 542 | return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage |
| 543 | case 14: |
| 544 | return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage |
Rafael Espindola | bec6af6 | 2015-01-08 15:39:50 +0000 | [diff] [blame] | 545 | case 15: |
| 546 | return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage |
Rafael Espindola | 12ca34f | 2015-01-19 15:16:06 +0000 | [diff] [blame] | 547 | case 1: // Old value with implicit comdat. |
| 548 | case 16: |
| 549 | return GlobalValue::WeakAnyLinkage; |
| 550 | case 10: // Old value with implicit comdat. |
| 551 | case 17: |
| 552 | return GlobalValue::WeakODRLinkage; |
| 553 | case 4: // Old value with implicit comdat. |
| 554 | case 18: |
| 555 | return GlobalValue::LinkOnceAnyLinkage; |
| 556 | case 11: // Old value with implicit comdat. |
| 557 | case 19: |
| 558 | return GlobalValue::LinkOnceODRLinkage; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | |
| 562 | static GlobalValue::VisibilityTypes GetDecodedVisibility(unsigned Val) { |
| 563 | switch (Val) { |
| 564 | default: // Map unknown visibilities to default. |
| 565 | case 0: return GlobalValue::DefaultVisibility; |
| 566 | case 1: return GlobalValue::HiddenVisibility; |
Anton Korobeynikov | 31fc4f9 | 2007-04-29 20:56:48 +0000 | [diff] [blame] | 567 | case 2: return GlobalValue::ProtectedVisibility; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 571 | static GlobalValue::DLLStorageClassTypes |
| 572 | GetDecodedDLLStorageClass(unsigned Val) { |
| 573 | switch (Val) { |
| 574 | default: // Map unknown values to default. |
| 575 | case 0: return GlobalValue::DefaultStorageClass; |
| 576 | case 1: return GlobalValue::DLLImportStorageClass; |
| 577 | case 2: return GlobalValue::DLLExportStorageClass; |
| 578 | } |
| 579 | } |
| 580 | |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 581 | static GlobalVariable::ThreadLocalMode GetDecodedThreadLocalMode(unsigned Val) { |
| 582 | switch (Val) { |
| 583 | case 0: return GlobalVariable::NotThreadLocal; |
| 584 | default: // Map unknown non-zero value to general dynamic. |
| 585 | case 1: return GlobalVariable::GeneralDynamicTLSModel; |
| 586 | case 2: return GlobalVariable::LocalDynamicTLSModel; |
| 587 | case 3: return GlobalVariable::InitialExecTLSModel; |
| 588 | case 4: return GlobalVariable::LocalExecTLSModel; |
| 589 | } |
| 590 | } |
| 591 | |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 592 | static int GetDecodedCastOpcode(unsigned Val) { |
| 593 | switch (Val) { |
| 594 | default: return -1; |
| 595 | case bitc::CAST_TRUNC : return Instruction::Trunc; |
| 596 | case bitc::CAST_ZEXT : return Instruction::ZExt; |
| 597 | case bitc::CAST_SEXT : return Instruction::SExt; |
| 598 | case bitc::CAST_FPTOUI : return Instruction::FPToUI; |
| 599 | case bitc::CAST_FPTOSI : return Instruction::FPToSI; |
| 600 | case bitc::CAST_UITOFP : return Instruction::UIToFP; |
| 601 | case bitc::CAST_SITOFP : return Instruction::SIToFP; |
| 602 | case bitc::CAST_FPTRUNC : return Instruction::FPTrunc; |
| 603 | case bitc::CAST_FPEXT : return Instruction::FPExt; |
| 604 | case bitc::CAST_PTRTOINT: return Instruction::PtrToInt; |
| 605 | case bitc::CAST_INTTOPTR: return Instruction::IntToPtr; |
| 606 | case bitc::CAST_BITCAST : return Instruction::BitCast; |
Matt Arsenault | 3aa9b03 | 2013-11-18 02:51:33 +0000 | [diff] [blame] | 607 | case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast; |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 608 | } |
| 609 | } |
Filipe Cabecinhas | ea79c5b | 2015-04-22 09:06:21 +0000 | [diff] [blame] | 610 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 611 | static int GetDecodedBinaryOpcode(unsigned Val, Type *Ty) { |
Filipe Cabecinhas | ea79c5b | 2015-04-22 09:06:21 +0000 | [diff] [blame] | 612 | bool IsFP = Ty->isFPOrFPVectorTy(); |
| 613 | // BinOps are only valid for int/fp or vector of int/fp types |
| 614 | if (!IsFP && !Ty->isIntOrIntVectorTy()) |
| 615 | return -1; |
| 616 | |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 617 | switch (Val) { |
Filipe Cabecinhas | ea79c5b | 2015-04-22 09:06:21 +0000 | [diff] [blame] | 618 | default: |
| 619 | return -1; |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 620 | case bitc::BINOP_ADD: |
Filipe Cabecinhas | ea79c5b | 2015-04-22 09:06:21 +0000 | [diff] [blame] | 621 | return IsFP ? Instruction::FAdd : Instruction::Add; |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 622 | case bitc::BINOP_SUB: |
Filipe Cabecinhas | ea79c5b | 2015-04-22 09:06:21 +0000 | [diff] [blame] | 623 | return IsFP ? Instruction::FSub : Instruction::Sub; |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 624 | case bitc::BINOP_MUL: |
Filipe Cabecinhas | ea79c5b | 2015-04-22 09:06:21 +0000 | [diff] [blame] | 625 | return IsFP ? Instruction::FMul : Instruction::Mul; |
| 626 | case bitc::BINOP_UDIV: |
| 627 | return IsFP ? -1 : Instruction::UDiv; |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 628 | case bitc::BINOP_SDIV: |
Filipe Cabecinhas | ea79c5b | 2015-04-22 09:06:21 +0000 | [diff] [blame] | 629 | return IsFP ? Instruction::FDiv : Instruction::SDiv; |
| 630 | case bitc::BINOP_UREM: |
| 631 | return IsFP ? -1 : Instruction::URem; |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 632 | case bitc::BINOP_SREM: |
Filipe Cabecinhas | ea79c5b | 2015-04-22 09:06:21 +0000 | [diff] [blame] | 633 | return IsFP ? Instruction::FRem : Instruction::SRem; |
| 634 | case bitc::BINOP_SHL: |
| 635 | return IsFP ? -1 : Instruction::Shl; |
| 636 | case bitc::BINOP_LSHR: |
| 637 | return IsFP ? -1 : Instruction::LShr; |
| 638 | case bitc::BINOP_ASHR: |
| 639 | return IsFP ? -1 : Instruction::AShr; |
| 640 | case bitc::BINOP_AND: |
| 641 | return IsFP ? -1 : Instruction::And; |
| 642 | case bitc::BINOP_OR: |
| 643 | return IsFP ? -1 : Instruction::Or; |
| 644 | case bitc::BINOP_XOR: |
| 645 | return IsFP ? -1 : Instruction::Xor; |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 649 | static AtomicRMWInst::BinOp GetDecodedRMWOperation(unsigned Val) { |
| 650 | switch (Val) { |
| 651 | default: return AtomicRMWInst::BAD_BINOP; |
| 652 | case bitc::RMW_XCHG: return AtomicRMWInst::Xchg; |
| 653 | case bitc::RMW_ADD: return AtomicRMWInst::Add; |
| 654 | case bitc::RMW_SUB: return AtomicRMWInst::Sub; |
| 655 | case bitc::RMW_AND: return AtomicRMWInst::And; |
| 656 | case bitc::RMW_NAND: return AtomicRMWInst::Nand; |
| 657 | case bitc::RMW_OR: return AtomicRMWInst::Or; |
| 658 | case bitc::RMW_XOR: return AtomicRMWInst::Xor; |
| 659 | case bitc::RMW_MAX: return AtomicRMWInst::Max; |
| 660 | case bitc::RMW_MIN: return AtomicRMWInst::Min; |
| 661 | case bitc::RMW_UMAX: return AtomicRMWInst::UMax; |
| 662 | case bitc::RMW_UMIN: return AtomicRMWInst::UMin; |
| 663 | } |
| 664 | } |
| 665 | |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 666 | static AtomicOrdering GetDecodedOrdering(unsigned Val) { |
| 667 | switch (Val) { |
| 668 | case bitc::ORDERING_NOTATOMIC: return NotAtomic; |
| 669 | case bitc::ORDERING_UNORDERED: return Unordered; |
| 670 | case bitc::ORDERING_MONOTONIC: return Monotonic; |
| 671 | case bitc::ORDERING_ACQUIRE: return Acquire; |
| 672 | case bitc::ORDERING_RELEASE: return Release; |
| 673 | case bitc::ORDERING_ACQREL: return AcquireRelease; |
| 674 | default: // Map unknown orderings to sequentially-consistent. |
| 675 | case bitc::ORDERING_SEQCST: return SequentiallyConsistent; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | static SynchronizationScope GetDecodedSynchScope(unsigned Val) { |
| 680 | switch (Val) { |
| 681 | case bitc::SYNCHSCOPE_SINGLETHREAD: return SingleThread; |
| 682 | default: // Map unknown scopes to cross-thread. |
| 683 | case bitc::SYNCHSCOPE_CROSSTHREAD: return CrossThread; |
| 684 | } |
| 685 | } |
| 686 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 687 | static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) { |
| 688 | switch (Val) { |
| 689 | default: // Map unknown selection kinds to any. |
| 690 | case bitc::COMDAT_SELECTION_KIND_ANY: |
| 691 | return Comdat::Any; |
| 692 | case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH: |
| 693 | return Comdat::ExactMatch; |
| 694 | case bitc::COMDAT_SELECTION_KIND_LARGEST: |
| 695 | return Comdat::Largest; |
| 696 | case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES: |
| 697 | return Comdat::NoDuplicates; |
| 698 | case bitc::COMDAT_SELECTION_KIND_SAME_SIZE: |
| 699 | return Comdat::SameSize; |
| 700 | } |
| 701 | } |
| 702 | |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 703 | static void UpgradeDLLImportExportLinkage(llvm::GlobalValue *GV, unsigned Val) { |
| 704 | switch (Val) { |
| 705 | case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break; |
| 706 | case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break; |
| 707 | } |
| 708 | } |
| 709 | |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 710 | namespace llvm { |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 711 | namespace { |
| 712 | /// @brief A class for maintaining the slot number definition |
| 713 | /// as a placeholder for the actual definition for forward constants defs. |
| 714 | class ConstantPlaceHolder : public ConstantExpr { |
Aaron Ballman | f9a1897 | 2015-02-15 22:54:22 +0000 | [diff] [blame] | 715 | void operator=(const ConstantPlaceHolder &) = delete; |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 716 | public: |
| 717 | // allocate space for exactly one operand |
| 718 | void *operator new(size_t s) { |
| 719 | return User::operator new(s, 1); |
| 720 | } |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 721 | explicit ConstantPlaceHolder(Type *Ty, LLVMContext& Context) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 722 | : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) { |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 723 | Op<0>() = UndefValue::get(Type::getInt32Ty(Context)); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 724 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 725 | |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 726 | /// @brief Methods to support type inquiry through isa, cast, and dyn_cast. |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 727 | static bool classof(const Value *V) { |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 728 | return isa<ConstantExpr>(V) && |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 729 | cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1; |
| 730 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 731 | |
| 732 | |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 733 | /// Provide fast operand accessors |
Richard Trieu | e3d126c | 2014-11-21 02:42:08 +0000 | [diff] [blame] | 734 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 735 | }; |
| 736 | } |
| 737 | |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 738 | // FIXME: can we inherit this from ConstantExpr? |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 739 | template <> |
Jay Foad | c8adf5f | 2011-01-11 15:07:38 +0000 | [diff] [blame] | 740 | struct OperandTraits<ConstantPlaceHolder> : |
| 741 | public FixedNumOperandTraits<ConstantPlaceHolder, 1> { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 742 | }; |
Richard Trieu | e3d126c | 2014-11-21 02:42:08 +0000 | [diff] [blame] | 743 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantPlaceHolder, Value) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 746 | |
| 747 | void BitcodeReaderValueList::AssignValue(Value *V, unsigned Idx) { |
| 748 | if (Idx == size()) { |
| 749 | push_back(V); |
| 750 | return; |
| 751 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 752 | |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 753 | if (Idx >= size()) |
| 754 | resize(Idx+1); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 755 | |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 756 | WeakVH &OldV = ValuePtrs[Idx]; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 757 | if (!OldV) { |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 758 | OldV = V; |
| 759 | return; |
| 760 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 761 | |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 762 | // Handle constants and non-constants (e.g. instrs) differently for |
| 763 | // efficiency. |
| 764 | if (Constant *PHC = dyn_cast<Constant>(&*OldV)) { |
| 765 | ResolveConstants.push_back(std::make_pair(PHC, Idx)); |
| 766 | OldV = V; |
| 767 | } else { |
| 768 | // If there was a forward reference to this value, replace it. |
| 769 | Value *PrevVal = OldV; |
| 770 | OldV->replaceAllUsesWith(V); |
| 771 | delete PrevVal; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 772 | } |
| 773 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 774 | |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 775 | |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 776 | Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 777 | Type *Ty) { |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 778 | if (Idx >= size()) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 779 | resize(Idx + 1); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 780 | |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 781 | if (Value *V = ValuePtrs[Idx]) { |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 782 | assert(Ty == V->getType() && "Type mismatch in constant table!"); |
| 783 | return cast<Constant>(V); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 784 | } |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 785 | |
| 786 | // Create and return a placeholder, which will later be RAUW'd. |
Owen Anderson | e9f9804 | 2009-07-07 20:18:58 +0000 | [diff] [blame] | 787 | Constant *C = new ConstantPlaceHolder(Ty, Context); |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 788 | ValuePtrs[Idx] = C; |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 789 | return C; |
| 790 | } |
| 791 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 792 | Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, Type *Ty) { |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 793 | if (Idx >= size()) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 794 | resize(Idx + 1); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 795 | |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 796 | if (Value *V = ValuePtrs[Idx]) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 797 | assert((!Ty || Ty == V->getType()) && "Type mismatch in value table!"); |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 798 | return V; |
| 799 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 800 | |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 801 | // No type specified, must be invalid reference. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 802 | if (!Ty) return nullptr; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 803 | |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 804 | // Create and return a placeholder, which will later be RAUW'd. |
| 805 | Value *V = new Argument(Ty); |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 806 | ValuePtrs[Idx] = V; |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 807 | return V; |
| 808 | } |
| 809 | |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 810 | /// ResolveConstantForwardRefs - Once all constants are read, this method bulk |
| 811 | /// resolves any forward references. The idea behind this is that we sometimes |
| 812 | /// get constants (such as large arrays) which reference *many* forward ref |
| 813 | /// constants. Replacing each of these causes a lot of thrashing when |
| 814 | /// building/reuniquing the constant. Instead of doing this, we look at all the |
| 815 | /// uses and rewrite all the place holders at once for any constant that uses |
| 816 | /// a placeholder. |
| 817 | void BitcodeReaderValueList::ResolveConstantForwardRefs() { |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 818 | // 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] | 819 | // binary search. |
| 820 | std::sort(ResolveConstants.begin(), ResolveConstants.end()); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 821 | |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 822 | SmallVector<Constant*, 64> NewOps; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 823 | |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 824 | while (!ResolveConstants.empty()) { |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 825 | Value *RealVal = operator[](ResolveConstants.back().second); |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 826 | Constant *Placeholder = ResolveConstants.back().first; |
| 827 | ResolveConstants.pop_back(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 828 | |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 829 | // Loop over all users of the placeholder, updating them to reference the |
| 830 | // new value. If they reference more than one placeholder, update them all |
| 831 | // at once. |
| 832 | while (!Placeholder->use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 833 | auto UI = Placeholder->user_begin(); |
Gabor Greif | 2c0ab48 | 2010-07-09 16:01:21 +0000 | [diff] [blame] | 834 | User *U = *UI; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 835 | |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 836 | // If the using object isn't uniqued, just update the operands. This |
| 837 | // handles instructions and initializers for global variables. |
Gabor Greif | 2c0ab48 | 2010-07-09 16:01:21 +0000 | [diff] [blame] | 838 | if (!isa<Constant>(U) || isa<GlobalValue>(U)) { |
Chris Lattner | 479c5d9 | 2008-08-21 17:31:45 +0000 | [diff] [blame] | 839 | UI.getUse().set(RealVal); |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 840 | continue; |
| 841 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 842 | |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 843 | // Otherwise, we have a constant that uses the placeholder. Replace that |
| 844 | // constant with a new constant that has *all* placeholder uses updated. |
Gabor Greif | 2c0ab48 | 2010-07-09 16:01:21 +0000 | [diff] [blame] | 845 | Constant *UserC = cast<Constant>(U); |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 846 | for (User::op_iterator I = UserC->op_begin(), E = UserC->op_end(); |
| 847 | I != E; ++I) { |
| 848 | Value *NewOp; |
| 849 | if (!isa<ConstantPlaceHolder>(*I)) { |
| 850 | // Not a placeholder reference. |
| 851 | NewOp = *I; |
| 852 | } else if (*I == Placeholder) { |
| 853 | // Common case is that it just references this one placeholder. |
| 854 | NewOp = RealVal; |
| 855 | } else { |
| 856 | // Otherwise, look up the placeholder in ResolveConstants. |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 857 | ResolveConstantsTy::iterator It = |
| 858 | std::lower_bound(ResolveConstants.begin(), ResolveConstants.end(), |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 859 | std::pair<Constant*, unsigned>(cast<Constant>(*I), |
| 860 | 0)); |
| 861 | assert(It != ResolveConstants.end() && It->first == *I); |
Chris Lattner | 2d8cd80 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 862 | NewOp = operator[](It->second); |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | NewOps.push_back(cast<Constant>(NewOp)); |
| 866 | } |
| 867 | |
| 868 | // Make the new constant. |
| 869 | Constant *NewC; |
| 870 | if (ConstantArray *UserCA = dyn_cast<ConstantArray>(UserC)) { |
Jay Foad | 83be361 | 2011-06-22 09:24:39 +0000 | [diff] [blame] | 871 | NewC = ConstantArray::get(UserCA->getType(), NewOps); |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 872 | } else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) { |
Chris Lattner | cc19efa | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 873 | NewC = ConstantStruct::get(UserCS->getType(), NewOps); |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 874 | } else if (isa<ConstantVector>(UserC)) { |
Chris Lattner | 6922931 | 2011-02-15 00:14:00 +0000 | [diff] [blame] | 875 | NewC = ConstantVector::get(NewOps); |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 876 | } else { |
| 877 | assert(isa<ConstantExpr>(UserC) && "Must be a ConstantExpr."); |
Jay Foad | 5c984e56 | 2011-04-13 13:46:01 +0000 | [diff] [blame] | 878 | NewC = cast<ConstantExpr>(UserC)->getWithOperands(NewOps); |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 879 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 880 | |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 881 | UserC->replaceAllUsesWith(NewC); |
| 882 | UserC->destroyConstant(); |
| 883 | NewOps.clear(); |
| 884 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 885 | |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 886 | // Update all ValueHandles, they should be the only users at this point. |
| 887 | Placeholder->replaceAllUsesWith(RealVal); |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 888 | delete Placeholder; |
| 889 | } |
| 890 | } |
| 891 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 892 | void BitcodeReaderMDValueList::AssignValue(Metadata *MD, unsigned Idx) { |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 893 | if (Idx == size()) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 894 | push_back(MD); |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 895 | return; |
| 896 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 897 | |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 898 | if (Idx >= size()) |
| 899 | resize(Idx+1); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 900 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 901 | TrackingMDRef &OldMD = MDValuePtrs[Idx]; |
| 902 | if (!OldMD) { |
| 903 | OldMD.reset(MD); |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 904 | return; |
| 905 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 906 | |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 907 | // 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] | 908 | TempMDTuple PrevMD(cast<MDTuple>(OldMD.get())); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 909 | PrevMD->replaceAllUsesWith(MD); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 910 | --NumFwdRefs; |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 913 | Metadata *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) { |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 914 | if (Idx >= size()) |
| 915 | resize(Idx + 1); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 916 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 917 | if (Metadata *MD = MDValuePtrs[Idx]) |
| 918 | return MD; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 919 | |
Duncan P. N. Exon Smith | 060ee62 | 2015-02-16 19:18:01 +0000 | [diff] [blame] | 920 | // Track forward refs to be resolved later. |
| 921 | if (AnyFwdRefs) { |
| 922 | MinFwdRef = std::min(MinFwdRef, Idx); |
| 923 | MaxFwdRef = std::max(MaxFwdRef, Idx); |
| 924 | } else { |
| 925 | AnyFwdRefs = true; |
| 926 | MinFwdRef = MaxFwdRef = Idx; |
| 927 | } |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 928 | ++NumFwdRefs; |
Duncan P. N. Exon Smith | 060ee62 | 2015-02-16 19:18:01 +0000 | [diff] [blame] | 929 | |
| 930 | // 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] | 931 | Metadata *MD = MDNode::getTemporary(Context, None).release(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 932 | MDValuePtrs[Idx].reset(MD); |
| 933 | return MD; |
| 934 | } |
| 935 | |
| 936 | void BitcodeReaderMDValueList::tryToResolveCycles() { |
| 937 | if (!AnyFwdRefs) |
| 938 | // Nothing to do. |
| 939 | return; |
| 940 | |
| 941 | if (NumFwdRefs) |
| 942 | // Still forward references... can't resolve cycles. |
| 943 | return; |
| 944 | |
| 945 | // Resolve any cycles. |
Duncan P. N. Exon Smith | 060ee62 | 2015-02-16 19:18:01 +0000 | [diff] [blame] | 946 | for (unsigned I = MinFwdRef, E = MaxFwdRef + 1; I != E; ++I) { |
| 947 | auto &MD = MDValuePtrs[I]; |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 948 | auto *N = dyn_cast_or_null<MDNode>(MD); |
Duncan P. N. Exon Smith | 946fdcc | 2015-01-19 20:36:39 +0000 | [diff] [blame] | 949 | if (!N) |
| 950 | continue; |
| 951 | |
| 952 | assert(!N->isTemporary() && "Unexpected forward reference"); |
| 953 | N->resolveCycles(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 954 | } |
Duncan P. N. Exon Smith | 060ee62 | 2015-02-16 19:18:01 +0000 | [diff] [blame] | 955 | |
| 956 | // Make sure we return early again until there's another forward ref. |
| 957 | AnyFwdRefs = false; |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 958 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 959 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 960 | Type *BitcodeReader::getTypeByID(unsigned ID) { |
| 961 | // The type table size is always specified correctly. |
| 962 | if (ID >= TypeList.size()) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 963 | return nullptr; |
Derek Schuff | 206dddd | 2012-02-06 19:03:04 +0000 | [diff] [blame] | 964 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 965 | if (Type *Ty = TypeList[ID]) |
| 966 | return Ty; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 967 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 968 | // If we have a forward reference, the only possible case is when it is to a |
| 969 | // named struct. Just create a placeholder for now. |
Rafael Espindola | 2fa1e43 | 2014-12-03 07:18:23 +0000 | [diff] [blame] | 970 | return TypeList[ID] = createIdentifiedStructType(Context); |
| 971 | } |
| 972 | |
| 973 | StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context, |
| 974 | StringRef Name) { |
| 975 | auto *Ret = StructType::create(Context, Name); |
| 976 | IdentifiedStructTypes.push_back(Ret); |
| 977 | return Ret; |
| 978 | } |
| 979 | |
| 980 | StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) { |
| 981 | auto *Ret = StructType::create(Context); |
| 982 | IdentifiedStructTypes.push_back(Ret); |
| 983 | return Ret; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 986 | |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 987 | //===----------------------------------------------------------------------===// |
| 988 | // Functions for parsing blocks from the bitcode file |
| 989 | //===----------------------------------------------------------------------===// |
| 990 | |
Bill Wendling | 56aeccc | 2013-02-04 23:32:23 +0000 | [diff] [blame] | 991 | |
| 992 | /// \brief This fills an AttrBuilder object with the LLVM attributes that have |
| 993 | /// been decoded from the given integer. This function must stay in sync with |
| 994 | /// 'encodeLLVMAttributesForBitcode'. |
| 995 | static void decodeLLVMAttributesForBitcode(AttrBuilder &B, |
| 996 | uint64_t EncodedAttrs) { |
| 997 | // FIXME: Remove in 4.0. |
| 998 | |
| 999 | // The alignment is stored as a 16-bit raw value from bits 31--16. We shift |
| 1000 | // the bits above 31 down by 11 bits. |
| 1001 | unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16; |
| 1002 | assert((!Alignment || isPowerOf2_32(Alignment)) && |
| 1003 | "Alignment must be a power of two."); |
| 1004 | |
| 1005 | if (Alignment) |
| 1006 | B.addAlignmentAttr(Alignment); |
Kostya Serebryany | d688bab | 2013-02-11 08:13:54 +0000 | [diff] [blame] | 1007 | B.addRawValue(((EncodedAttrs & (0xfffffULL << 32)) >> 11) | |
Bill Wendling | 56aeccc | 2013-02-04 23:32:23 +0000 | [diff] [blame] | 1008 | (EncodedAttrs & 0xffff)); |
| 1009 | } |
| 1010 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1011 | std::error_code BitcodeReader::ParseAttributeBlock() { |
Chris Lattner | 982ec1e | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1012 | if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1013 | return Error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1014 | |
Devang Patel | a05633e | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 1015 | if (!MAttributes.empty()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1016 | return Error("Invalid multiple blocks"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1017 | |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1018 | SmallVector<uint64_t, 64> Record; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1019 | |
Bill Wendling | 71173cb | 2013-01-27 00:36:48 +0000 | [diff] [blame] | 1020 | SmallVector<AttributeSet, 8> Attrs; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1021 | |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1022 | // Read all the records. |
| 1023 | while (1) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1024 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 1025 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1026 | switch (Entry.Kind) { |
| 1027 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 1028 | case BitstreamEntry::Error: |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1029 | return Error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1030 | case BitstreamEntry::EndBlock: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1031 | return std::error_code(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1032 | case BitstreamEntry::Record: |
| 1033 | // The interesting case. |
| 1034 | break; |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1035 | } |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 1036 | |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1037 | // Read a record. |
| 1038 | Record.clear(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1039 | switch (Stream.readRecord(Entry.ID, Record)) { |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1040 | default: // Default behavior: ignore. |
| 1041 | break; |
Bill Wendling | 56aeccc | 2013-02-04 23:32:23 +0000 | [diff] [blame] | 1042 | case bitc::PARAMATTR_CODE_ENTRY_OLD: { // ENTRY: [paramidx0, attr0, ...] |
| 1043 | // FIXME: Remove in 4.0. |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1044 | if (Record.size() & 1) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1045 | return Error("Invalid record"); |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1046 | |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1047 | for (unsigned i = 0, e = Record.size(); i != e; i += 2) { |
Bill Wendling | 60011b8 | 2013-01-29 01:43:29 +0000 | [diff] [blame] | 1048 | AttrBuilder B; |
Bill Wendling | 56aeccc | 2013-02-04 23:32:23 +0000 | [diff] [blame] | 1049 | decodeLLVMAttributesForBitcode(B, Record[i+1]); |
Bill Wendling | 60011b8 | 2013-01-29 01:43:29 +0000 | [diff] [blame] | 1050 | Attrs.push_back(AttributeSet::get(Context, Record[i], B)); |
Devang Patel | a05633e | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 1051 | } |
Devang Patel | a05633e | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 1052 | |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 1053 | MAttributes.push_back(AttributeSet::get(Context, Attrs)); |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1054 | Attrs.clear(); |
| 1055 | break; |
| 1056 | } |
Bill Wendling | 0dc0891 | 2013-02-12 08:13:50 +0000 | [diff] [blame] | 1057 | case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [attrgrp0, attrgrp1, ...] |
| 1058 | for (unsigned i = 0, e = Record.size(); i != e; ++i) |
| 1059 | Attrs.push_back(MAttributeGroups[Record[i]]); |
| 1060 | |
| 1061 | MAttributes.push_back(AttributeSet::get(Context, Attrs)); |
| 1062 | Attrs.clear(); |
| 1063 | break; |
| 1064 | } |
Duncan Sands | 04eb67e | 2007-11-20 14:09:29 +0000 | [diff] [blame] | 1065 | } |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1066 | } |
| 1067 | } |
| 1068 | |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1069 | // Returns Attribute::None on unrecognized codes. |
| 1070 | static Attribute::AttrKind GetAttrFromCode(uint64_t Code) { |
| 1071 | switch (Code) { |
| 1072 | default: |
| 1073 | return Attribute::None; |
| 1074 | case bitc::ATTR_KIND_ALIGNMENT: |
| 1075 | return Attribute::Alignment; |
| 1076 | case bitc::ATTR_KIND_ALWAYS_INLINE: |
| 1077 | return Attribute::AlwaysInline; |
| 1078 | case bitc::ATTR_KIND_BUILTIN: |
| 1079 | return Attribute::Builtin; |
| 1080 | case bitc::ATTR_KIND_BY_VAL: |
| 1081 | return Attribute::ByVal; |
Reid Kleckner | a534a38 | 2013-12-19 02:14:12 +0000 | [diff] [blame] | 1082 | case bitc::ATTR_KIND_IN_ALLOCA: |
| 1083 | return Attribute::InAlloca; |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1084 | case bitc::ATTR_KIND_COLD: |
| 1085 | return Attribute::Cold; |
| 1086 | case bitc::ATTR_KIND_INLINE_HINT: |
| 1087 | return Attribute::InlineHint; |
| 1088 | case bitc::ATTR_KIND_IN_REG: |
| 1089 | return Attribute::InReg; |
Tom Roeder | 44cb65f | 2014-06-05 19:29:43 +0000 | [diff] [blame] | 1090 | case bitc::ATTR_KIND_JUMP_TABLE: |
| 1091 | return Attribute::JumpTable; |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1092 | case bitc::ATTR_KIND_MIN_SIZE: |
| 1093 | return Attribute::MinSize; |
| 1094 | case bitc::ATTR_KIND_NAKED: |
| 1095 | return Attribute::Naked; |
| 1096 | case bitc::ATTR_KIND_NEST: |
| 1097 | return Attribute::Nest; |
| 1098 | case bitc::ATTR_KIND_NO_ALIAS: |
| 1099 | return Attribute::NoAlias; |
| 1100 | case bitc::ATTR_KIND_NO_BUILTIN: |
| 1101 | return Attribute::NoBuiltin; |
| 1102 | case bitc::ATTR_KIND_NO_CAPTURE: |
| 1103 | return Attribute::NoCapture; |
| 1104 | case bitc::ATTR_KIND_NO_DUPLICATE: |
| 1105 | return Attribute::NoDuplicate; |
| 1106 | case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT: |
| 1107 | return Attribute::NoImplicitFloat; |
| 1108 | case bitc::ATTR_KIND_NO_INLINE: |
| 1109 | return Attribute::NoInline; |
| 1110 | case bitc::ATTR_KIND_NON_LAZY_BIND: |
| 1111 | return Attribute::NonLazyBind; |
Nick Lewycky | d52b152 | 2014-05-20 01:23:40 +0000 | [diff] [blame] | 1112 | case bitc::ATTR_KIND_NON_NULL: |
| 1113 | return Attribute::NonNull; |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1114 | case bitc::ATTR_KIND_DEREFERENCEABLE: |
| 1115 | return Attribute::Dereferenceable; |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 1116 | case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL: |
| 1117 | return Attribute::DereferenceableOrNull; |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1118 | case bitc::ATTR_KIND_NO_RED_ZONE: |
| 1119 | return Attribute::NoRedZone; |
| 1120 | case bitc::ATTR_KIND_NO_RETURN: |
| 1121 | return Attribute::NoReturn; |
| 1122 | case bitc::ATTR_KIND_NO_UNWIND: |
| 1123 | return Attribute::NoUnwind; |
| 1124 | case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE: |
| 1125 | return Attribute::OptimizeForSize; |
| 1126 | case bitc::ATTR_KIND_OPTIMIZE_NONE: |
| 1127 | return Attribute::OptimizeNone; |
| 1128 | case bitc::ATTR_KIND_READ_NONE: |
| 1129 | return Attribute::ReadNone; |
| 1130 | case bitc::ATTR_KIND_READ_ONLY: |
| 1131 | return Attribute::ReadOnly; |
| 1132 | case bitc::ATTR_KIND_RETURNED: |
| 1133 | return Attribute::Returned; |
| 1134 | case bitc::ATTR_KIND_RETURNS_TWICE: |
| 1135 | return Attribute::ReturnsTwice; |
| 1136 | case bitc::ATTR_KIND_S_EXT: |
| 1137 | return Attribute::SExt; |
| 1138 | case bitc::ATTR_KIND_STACK_ALIGNMENT: |
| 1139 | return Attribute::StackAlignment; |
| 1140 | case bitc::ATTR_KIND_STACK_PROTECT: |
| 1141 | return Attribute::StackProtect; |
| 1142 | case bitc::ATTR_KIND_STACK_PROTECT_REQ: |
| 1143 | return Attribute::StackProtectReq; |
| 1144 | case bitc::ATTR_KIND_STACK_PROTECT_STRONG: |
| 1145 | return Attribute::StackProtectStrong; |
| 1146 | case bitc::ATTR_KIND_STRUCT_RET: |
| 1147 | return Attribute::StructRet; |
| 1148 | case bitc::ATTR_KIND_SANITIZE_ADDRESS: |
| 1149 | return Attribute::SanitizeAddress; |
| 1150 | case bitc::ATTR_KIND_SANITIZE_THREAD: |
| 1151 | return Attribute::SanitizeThread; |
| 1152 | case bitc::ATTR_KIND_SANITIZE_MEMORY: |
| 1153 | return Attribute::SanitizeMemory; |
| 1154 | case bitc::ATTR_KIND_UW_TABLE: |
| 1155 | return Attribute::UWTable; |
| 1156 | case bitc::ATTR_KIND_Z_EXT: |
| 1157 | return Attribute::ZExt; |
| 1158 | } |
| 1159 | } |
| 1160 | |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 1161 | std::error_code BitcodeReader::parseAlignmentValue(uint64_t Exponent, |
| 1162 | unsigned &Alignment) { |
| 1163 | // Note: Alignment in bitcode files is incremented by 1, so that zero |
| 1164 | // can be used for default alignment. |
| 1165 | if (Exponent > Value::MaxAlignmentExponent + 1) |
| 1166 | return Error("Invalid alignment value"); |
| 1167 | Alignment = (1 << static_cast<unsigned>(Exponent)) >> 1; |
| 1168 | return std::error_code(); |
| 1169 | } |
| 1170 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1171 | std::error_code BitcodeReader::ParseAttrKind(uint64_t Code, |
| 1172 | Attribute::AttrKind *Kind) { |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1173 | *Kind = GetAttrFromCode(Code); |
| 1174 | if (*Kind == Attribute::None) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1175 | return Error(BitcodeError::CorruptedBitcode, |
| 1176 | "Unknown attribute kind (" + Twine(Code) + ")"); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1177 | return std::error_code(); |
Tobias Grosser | 0a8e12f | 2013-07-26 04:16:55 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1180 | std::error_code BitcodeReader::ParseAttributeGroupBlock() { |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1181 | if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1182 | return Error("Invalid record"); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1183 | |
| 1184 | if (!MAttributeGroups.empty()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1185 | return Error("Invalid multiple blocks"); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1186 | |
| 1187 | SmallVector<uint64_t, 64> Record; |
| 1188 | |
| 1189 | // Read all the records. |
| 1190 | while (1) { |
| 1191 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| 1192 | |
| 1193 | switch (Entry.Kind) { |
| 1194 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 1195 | case BitstreamEntry::Error: |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1196 | return Error("Malformed block"); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1197 | case BitstreamEntry::EndBlock: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1198 | return std::error_code(); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1199 | case BitstreamEntry::Record: |
| 1200 | // The interesting case. |
| 1201 | break; |
| 1202 | } |
| 1203 | |
| 1204 | // Read a record. |
| 1205 | Record.clear(); |
| 1206 | switch (Stream.readRecord(Entry.ID, Record)) { |
| 1207 | default: // Default behavior: ignore. |
| 1208 | break; |
| 1209 | case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...] |
| 1210 | if (Record.size() < 3) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1211 | return Error("Invalid record"); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1212 | |
Bill Wendling | e46707e | 2013-02-11 22:32:29 +0000 | [diff] [blame] | 1213 | uint64_t GrpID = Record[0]; |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1214 | uint64_t Idx = Record[1]; // Index of the object this attribute refers to. |
| 1215 | |
| 1216 | AttrBuilder B; |
| 1217 | for (unsigned i = 2, e = Record.size(); i != e; ++i) { |
| 1218 | if (Record[i] == 0) { // Enum attribute |
Tobias Grosser | 0a8e12f | 2013-07-26 04:16:55 +0000 | [diff] [blame] | 1219 | Attribute::AttrKind Kind; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1220 | if (std::error_code EC = ParseAttrKind(Record[++i], &Kind)) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1221 | return EC; |
Tobias Grosser | 0a8e12f | 2013-07-26 04:16:55 +0000 | [diff] [blame] | 1222 | |
| 1223 | B.addAttribute(Kind); |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 1224 | } else if (Record[i] == 1) { // Integer attribute |
Tobias Grosser | 0a8e12f | 2013-07-26 04:16:55 +0000 | [diff] [blame] | 1225 | Attribute::AttrKind Kind; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1226 | if (std::error_code EC = ParseAttrKind(Record[++i], &Kind)) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1227 | return EC; |
Tobias Grosser | 0a8e12f | 2013-07-26 04:16:55 +0000 | [diff] [blame] | 1228 | if (Kind == Attribute::Alignment) |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1229 | B.addAlignmentAttr(Record[++i]); |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1230 | else if (Kind == Attribute::StackAlignment) |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1231 | B.addStackAlignmentAttr(Record[++i]); |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1232 | else if (Kind == Attribute::Dereferenceable) |
| 1233 | B.addDereferenceableAttr(Record[++i]); |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 1234 | else if (Kind == Attribute::DereferenceableOrNull) |
| 1235 | B.addDereferenceableOrNullAttr(Record[++i]); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1236 | } else { // String attribute |
Bill Wendling | e46707e | 2013-02-11 22:32:29 +0000 | [diff] [blame] | 1237 | assert((Record[i] == 3 || Record[i] == 4) && |
| 1238 | "Invalid attribute group entry"); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1239 | bool HasValue = (Record[i++] == 4); |
| 1240 | SmallString<64> KindStr; |
| 1241 | SmallString<64> ValStr; |
| 1242 | |
| 1243 | while (Record[i] != 0 && i != e) |
| 1244 | KindStr += Record[i++]; |
Bill Wendling | e46707e | 2013-02-11 22:32:29 +0000 | [diff] [blame] | 1245 | assert(Record[i] == 0 && "Kind string not null terminated"); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1246 | |
| 1247 | if (HasValue) { |
| 1248 | // Has a value associated with it. |
Bill Wendling | e46707e | 2013-02-11 22:32:29 +0000 | [diff] [blame] | 1249 | ++i; // Skip the '0' that terminates the "kind" string. |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1250 | while (Record[i] != 0 && i != e) |
| 1251 | ValStr += Record[i++]; |
Bill Wendling | e46707e | 2013-02-11 22:32:29 +0000 | [diff] [blame] | 1252 | assert(Record[i] == 0 && "Value string not null terminated"); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | B.addAttribute(KindStr.str(), ValStr.str()); |
| 1256 | } |
| 1257 | } |
| 1258 | |
Bill Wendling | e46707e | 2013-02-11 22:32:29 +0000 | [diff] [blame] | 1259 | MAttributeGroups[GrpID] = AttributeSet::get(Context, Idx, B); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1260 | break; |
| 1261 | } |
| 1262 | } |
| 1263 | } |
| 1264 | } |
| 1265 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1266 | std::error_code BitcodeReader::ParseTypeTable() { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1267 | if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1268 | return Error("Invalid record"); |
Derek Schuff | 206dddd | 2012-02-06 19:03:04 +0000 | [diff] [blame] | 1269 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1270 | return ParseTypeTableBody(); |
| 1271 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1272 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1273 | std::error_code BitcodeReader::ParseTypeTableBody() { |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1274 | if (!TypeList.empty()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1275 | return Error("Invalid multiple blocks"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1276 | |
| 1277 | SmallVector<uint64_t, 64> Record; |
| 1278 | unsigned NumRecords = 0; |
| 1279 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1280 | SmallString<64> TypeName; |
Derek Schuff | 206dddd | 2012-02-06 19:03:04 +0000 | [diff] [blame] | 1281 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1282 | // Read all the records for this type table. |
| 1283 | while (1) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1284 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 1285 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1286 | switch (Entry.Kind) { |
| 1287 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 1288 | case BitstreamEntry::Error: |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1289 | return Error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1290 | case BitstreamEntry::EndBlock: |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1291 | if (NumRecords != TypeList.size()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1292 | return Error("Malformed block"); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1293 | return std::error_code(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1294 | case BitstreamEntry::Record: |
| 1295 | // The interesting case. |
| 1296 | break; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1297 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1298 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1299 | // Read a record. |
| 1300 | Record.clear(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1301 | Type *ResultTy = nullptr; |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1302 | switch (Stream.readRecord(Entry.ID, Record)) { |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1303 | default: |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1304 | return Error("Invalid value"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1305 | case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries] |
| 1306 | // TYPE_CODE_NUMENTRY contains a count of the number of types in the |
| 1307 | // type list. This allows us to reserve space. |
| 1308 | if (Record.size() < 1) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1309 | return Error("Invalid record"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1310 | TypeList.resize(Record[0]); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1311 | continue; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1312 | case bitc::TYPE_CODE_VOID: // VOID |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1313 | ResultTy = Type::getVoidTy(Context); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1314 | break; |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 1315 | case bitc::TYPE_CODE_HALF: // HALF |
| 1316 | ResultTy = Type::getHalfTy(Context); |
| 1317 | break; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1318 | case bitc::TYPE_CODE_FLOAT: // FLOAT |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1319 | ResultTy = Type::getFloatTy(Context); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1320 | break; |
| 1321 | case bitc::TYPE_CODE_DOUBLE: // DOUBLE |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1322 | ResultTy = Type::getDoubleTy(Context); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1323 | break; |
Dale Johannesen | ff4c3be | 2007-08-03 01:03:46 +0000 | [diff] [blame] | 1324 | case bitc::TYPE_CODE_X86_FP80: // X86_FP80 |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1325 | ResultTy = Type::getX86_FP80Ty(Context); |
Dale Johannesen | ff4c3be | 2007-08-03 01:03:46 +0000 | [diff] [blame] | 1326 | break; |
| 1327 | case bitc::TYPE_CODE_FP128: // FP128 |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1328 | ResultTy = Type::getFP128Ty(Context); |
Dale Johannesen | ff4c3be | 2007-08-03 01:03:46 +0000 | [diff] [blame] | 1329 | break; |
| 1330 | case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128 |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1331 | ResultTy = Type::getPPC_FP128Ty(Context); |
Dale Johannesen | ff4c3be | 2007-08-03 01:03:46 +0000 | [diff] [blame] | 1332 | break; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1333 | case bitc::TYPE_CODE_LABEL: // LABEL |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1334 | ResultTy = Type::getLabelTy(Context); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1335 | break; |
Nick Lewycky | adbc284 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 1336 | case bitc::TYPE_CODE_METADATA: // METADATA |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1337 | ResultTy = Type::getMetadataTy(Context); |
Nick Lewycky | adbc284 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 1338 | break; |
Dale Johannesen | baa5d04 | 2010-09-10 20:55:01 +0000 | [diff] [blame] | 1339 | case bitc::TYPE_CODE_X86_MMX: // X86_MMX |
| 1340 | ResultTy = Type::getX86_MMXTy(Context); |
| 1341 | break; |
Filipe Cabecinhas | fcd044b | 2015-01-30 18:13:50 +0000 | [diff] [blame] | 1342 | case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1343 | if (Record.size() < 1) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1344 | return Error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1345 | |
Filipe Cabecinhas | fcd044b | 2015-01-30 18:13:50 +0000 | [diff] [blame] | 1346 | uint64_t NumBits = Record[0]; |
| 1347 | if (NumBits < IntegerType::MIN_INT_BITS || |
| 1348 | NumBits > IntegerType::MAX_INT_BITS) |
| 1349 | return Error("Bitwidth for integer type out of range"); |
| 1350 | ResultTy = IntegerType::get(Context, NumBits); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1351 | break; |
Filipe Cabecinhas | fcd044b | 2015-01-30 18:13:50 +0000 | [diff] [blame] | 1352 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1353 | case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 1354 | // [pointee type, address space] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1355 | if (Record.size() < 1) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1356 | return Error("Invalid record"); |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 1357 | unsigned AddressSpace = 0; |
| 1358 | if (Record.size() == 2) |
| 1359 | AddressSpace = Record[1]; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1360 | ResultTy = getTypeByID(Record[0]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1361 | if (!ResultTy) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1362 | return Error("Invalid type"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1363 | ResultTy = PointerType::get(ResultTy, AddressSpace); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1364 | break; |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 1365 | } |
Nuno Lopes | 561dae0 | 2012-05-23 15:19:39 +0000 | [diff] [blame] | 1366 | case bitc::TYPE_CODE_FUNCTION_OLD: { |
| 1367 | // FIXME: attrid is dead, remove it in LLVM 4.0 |
| 1368 | // FUNCTION: [vararg, attrid, retty, paramty x N] |
| 1369 | if (Record.size() < 3) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1370 | return Error("Invalid record"); |
Nuno Lopes | 561dae0 | 2012-05-23 15:19:39 +0000 | [diff] [blame] | 1371 | SmallVector<Type*, 8> ArgTys; |
| 1372 | for (unsigned i = 3, e = Record.size(); i != e; ++i) { |
| 1373 | if (Type *T = getTypeByID(Record[i])) |
| 1374 | ArgTys.push_back(T); |
| 1375 | else |
| 1376 | break; |
| 1377 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1378 | |
Nuno Lopes | 561dae0 | 2012-05-23 15:19:39 +0000 | [diff] [blame] | 1379 | ResultTy = getTypeByID(Record[2]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1380 | if (!ResultTy || ArgTys.size() < Record.size()-3) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1381 | return Error("Invalid type"); |
Nuno Lopes | 561dae0 | 2012-05-23 15:19:39 +0000 | [diff] [blame] | 1382 | |
| 1383 | ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); |
| 1384 | break; |
| 1385 | } |
Chad Rosier | 9589872 | 2011-11-03 00:14:01 +0000 | [diff] [blame] | 1386 | case bitc::TYPE_CODE_FUNCTION: { |
| 1387 | // FUNCTION: [vararg, retty, paramty x N] |
| 1388 | if (Record.size() < 2) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1389 | return Error("Invalid record"); |
Chris Lattner | cc3aaf1 | 2012-01-27 03:15:49 +0000 | [diff] [blame] | 1390 | SmallVector<Type*, 8> ArgTys; |
Chad Rosier | 9589872 | 2011-11-03 00:14:01 +0000 | [diff] [blame] | 1391 | for (unsigned i = 2, e = Record.size(); i != e; ++i) { |
| 1392 | if (Type *T = getTypeByID(Record[i])) |
| 1393 | ArgTys.push_back(T); |
| 1394 | else |
| 1395 | break; |
| 1396 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1397 | |
Chad Rosier | 9589872 | 2011-11-03 00:14:01 +0000 | [diff] [blame] | 1398 | ResultTy = getTypeByID(Record[1]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1399 | if (!ResultTy || ArgTys.size() < Record.size()-2) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1400 | return Error("Invalid type"); |
Chad Rosier | 9589872 | 2011-11-03 00:14:01 +0000 | [diff] [blame] | 1401 | |
| 1402 | ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); |
| 1403 | break; |
| 1404 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1405 | case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N] |
Chris Lattner | 3c5616e | 2007-05-06 08:21:50 +0000 | [diff] [blame] | 1406 | if (Record.size() < 1) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1407 | return Error("Invalid record"); |
Chris Lattner | cc3aaf1 | 2012-01-27 03:15:49 +0000 | [diff] [blame] | 1408 | SmallVector<Type*, 8> EltTys; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1409 | for (unsigned i = 1, e = Record.size(); i != e; ++i) { |
| 1410 | if (Type *T = getTypeByID(Record[i])) |
| 1411 | EltTys.push_back(T); |
| 1412 | else |
| 1413 | break; |
| 1414 | } |
| 1415 | if (EltTys.size() != Record.size()-1) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1416 | return Error("Invalid type"); |
Owen Anderson | 03cb69f | 2009-08-05 23:16:16 +0000 | [diff] [blame] | 1417 | ResultTy = StructType::get(Context, EltTys, Record[0]); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1418 | break; |
| 1419 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1420 | case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N] |
| 1421 | if (ConvertToString(Record, 0, TypeName)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1422 | return Error("Invalid record"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1423 | continue; |
| 1424 | |
| 1425 | case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N] |
| 1426 | if (Record.size() < 1) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1427 | return Error("Invalid record"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1428 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1429 | if (NumRecords >= TypeList.size()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1430 | return Error("Invalid TYPE table"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1431 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1432 | // Check to see if this was forward referenced, if so fill in the temp. |
| 1433 | StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); |
| 1434 | if (Res) { |
| 1435 | Res->setName(TypeName); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1436 | TypeList[NumRecords] = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1437 | } else // Otherwise, create a new struct. |
Rafael Espindola | 2fa1e43 | 2014-12-03 07:18:23 +0000 | [diff] [blame] | 1438 | Res = createIdentifiedStructType(Context, TypeName); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1439 | TypeName.clear(); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1440 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1441 | SmallVector<Type*, 8> EltTys; |
| 1442 | for (unsigned i = 1, e = Record.size(); i != e; ++i) { |
| 1443 | if (Type *T = getTypeByID(Record[i])) |
| 1444 | EltTys.push_back(T); |
| 1445 | else |
| 1446 | break; |
| 1447 | } |
| 1448 | if (EltTys.size() != Record.size()-1) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1449 | return Error("Invalid record"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1450 | Res->setBody(EltTys, Record[0]); |
| 1451 | ResultTy = Res; |
| 1452 | break; |
| 1453 | } |
| 1454 | case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: [] |
| 1455 | if (Record.size() != 1) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1456 | return Error("Invalid record"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1457 | |
| 1458 | if (NumRecords >= TypeList.size()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1459 | return Error("Invalid TYPE table"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1460 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1461 | // Check to see if this was forward referenced, if so fill in the temp. |
| 1462 | StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); |
| 1463 | if (Res) { |
| 1464 | Res->setName(TypeName); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1465 | TypeList[NumRecords] = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1466 | } else // Otherwise, create a new struct with no body. |
Rafael Espindola | 2fa1e43 | 2014-12-03 07:18:23 +0000 | [diff] [blame] | 1467 | Res = createIdentifiedStructType(Context, TypeName); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1468 | TypeName.clear(); |
| 1469 | ResultTy = Res; |
| 1470 | break; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1471 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1472 | case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty] |
| 1473 | if (Record.size() < 2) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1474 | return Error("Invalid record"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1475 | if ((ResultTy = getTypeByID(Record[1]))) |
| 1476 | ResultTy = ArrayType::get(ResultTy, Record[0]); |
| 1477 | else |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1478 | return Error("Invalid type"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1479 | break; |
| 1480 | case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] |
| 1481 | if (Record.size() < 2) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1482 | return Error("Invalid record"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1483 | if ((ResultTy = getTypeByID(Record[1]))) |
| 1484 | ResultTy = VectorType::get(ResultTy, Record[0]); |
| 1485 | else |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1486 | return Error("Invalid type"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1487 | break; |
| 1488 | } |
| 1489 | |
| 1490 | if (NumRecords >= TypeList.size()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1491 | return Error("Invalid TYPE table"); |
Filipe Cabecinhas | d0858e1 | 2015-01-30 10:57:58 +0000 | [diff] [blame] | 1492 | if (TypeList[NumRecords]) |
| 1493 | return Error( |
| 1494 | "Invalid TYPE table: Only named structs can be forward referenced"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1495 | assert(ResultTy && "Didn't read a type?"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1496 | TypeList[NumRecords++] = ResultTy; |
| 1497 | } |
| 1498 | } |
| 1499 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1500 | std::error_code BitcodeReader::ParseValueSymbolTable() { |
Chris Lattner | 982ec1e | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1501 | if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1502 | return Error("Invalid record"); |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1503 | |
| 1504 | SmallVector<uint64_t, 64> Record; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1505 | |
David Majnemer | 3087b22 | 2015-01-20 05:58:07 +0000 | [diff] [blame] | 1506 | Triple TT(TheModule->getTargetTriple()); |
| 1507 | |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1508 | // Read all the records for this value table. |
| 1509 | SmallString<128> ValueName; |
| 1510 | while (1) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1511 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 1512 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1513 | switch (Entry.Kind) { |
| 1514 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 1515 | case BitstreamEntry::Error: |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1516 | return Error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1517 | case BitstreamEntry::EndBlock: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1518 | return std::error_code(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1519 | case BitstreamEntry::Record: |
| 1520 | // The interesting case. |
| 1521 | break; |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1522 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1523 | |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1524 | // Read a record. |
| 1525 | Record.clear(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1526 | switch (Stream.readRecord(Entry.ID, Record)) { |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1527 | default: // Default behavior: unknown type. |
| 1528 | break; |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 1529 | case bitc::VST_CODE_ENTRY: { // VST_ENTRY: [valueid, namechar x N] |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1530 | if (ConvertToString(Record, 1, ValueName)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1531 | return Error("Invalid record"); |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1532 | unsigned ValueID = Record[0]; |
Karthik Bhat | 82540e9 | 2014-03-27 12:08:23 +0000 | [diff] [blame] | 1533 | if (ValueID >= ValueList.size() || !ValueList[ValueID]) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1534 | return Error("Invalid record"); |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1535 | Value *V = ValueList[ValueID]; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1536 | |
Daniel Dunbar | d786b51 | 2009-07-26 00:34:27 +0000 | [diff] [blame] | 1537 | V->setName(StringRef(ValueName.data(), ValueName.size())); |
Rafael Espindola | 12ca34f | 2015-01-19 15:16:06 +0000 | [diff] [blame] | 1538 | if (auto *GO = dyn_cast<GlobalObject>(V)) { |
David Majnemer | 3087b22 | 2015-01-20 05:58:07 +0000 | [diff] [blame] | 1539 | if (GO->getComdat() == reinterpret_cast<Comdat *>(1)) { |
| 1540 | if (TT.isOSBinFormatMachO()) |
| 1541 | GO->setComdat(nullptr); |
| 1542 | else |
| 1543 | GO->setComdat(TheModule->getOrInsertComdat(V->getName())); |
| 1544 | } |
Rafael Espindola | 12ca34f | 2015-01-19 15:16:06 +0000 | [diff] [blame] | 1545 | } |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1546 | ValueName.clear(); |
| 1547 | break; |
Reid Spencer | dea02bd | 2007-05-04 01:43:33 +0000 | [diff] [blame] | 1548 | } |
Bill Wendling | 35a9c3c | 2011-04-10 23:18:04 +0000 | [diff] [blame] | 1549 | case bitc::VST_CODE_BBENTRY: { |
Chris Lattner | 6be58c6 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 1550 | if (ConvertToString(Record, 1, ValueName)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1551 | return Error("Invalid record"); |
Chris Lattner | 6be58c6 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 1552 | BasicBlock *BB = getBasicBlock(Record[0]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1553 | if (!BB) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1554 | return Error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1555 | |
Daniel Dunbar | d786b51 | 2009-07-26 00:34:27 +0000 | [diff] [blame] | 1556 | BB->setName(StringRef(ValueName.data(), ValueName.size())); |
Chris Lattner | 6be58c6 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 1557 | ValueName.clear(); |
| 1558 | break; |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1559 | } |
Reid Spencer | dea02bd | 2007-05-04 01:43:33 +0000 | [diff] [blame] | 1560 | } |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1561 | } |
| 1562 | } |
| 1563 | |
Duncan P. N. Exon Smith | c7363f1 | 2015-02-13 01:10:38 +0000 | [diff] [blame] | 1564 | static int64_t unrotateSign(uint64_t U) { return U & 1 ? ~(U >> 1) : U >> 1; } |
| 1565 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1566 | std::error_code BitcodeReader::ParseMetadata() { |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 1567 | IsMetadataMaterialized = true; |
Devang Patel | 8992323 | 2010-01-11 18:52:33 +0000 | [diff] [blame] | 1568 | unsigned NextMDValueNo = MDValueList.size(); |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1569 | |
| 1570 | if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1571 | return Error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1572 | |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1573 | SmallVector<uint64_t, 64> Record; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1574 | |
Duncan P. N. Exon Smith | 171d077 | 2015-02-13 01:20:38 +0000 | [diff] [blame] | 1575 | auto getMD = |
| 1576 | [&](unsigned ID) -> Metadata *{ return MDValueList.getValueFwdRef(ID); }; |
| 1577 | auto getMDOrNull = [&](unsigned ID) -> Metadata *{ |
| 1578 | if (ID) |
| 1579 | return getMD(ID - 1); |
| 1580 | return nullptr; |
| 1581 | }; |
Duncan P. N. Exon Smith | 4e4aa70 | 2015-02-03 21:54:14 +0000 | [diff] [blame] | 1582 | auto getMDString = [&](unsigned ID) -> MDString *{ |
| 1583 | // This requires that the ID is not really a forward reference. In |
| 1584 | // particular, the MDString must already have been resolved. |
Duncan P. N. Exon Smith | 171d077 | 2015-02-13 01:20:38 +0000 | [diff] [blame] | 1585 | return cast_or_null<MDString>(getMDOrNull(ID)); |
Duncan P. N. Exon Smith | 4e4aa70 | 2015-02-03 21:54:14 +0000 | [diff] [blame] | 1586 | }; |
| 1587 | |
| 1588 | #define GET_OR_DISTINCT(CLASS, DISTINCT, ARGS) \ |
| 1589 | (DISTINCT ? CLASS::getDistinct ARGS : CLASS::get ARGS) |
| 1590 | |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1591 | // Read all the records. |
| 1592 | while (1) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1593 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 1594 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1595 | switch (Entry.Kind) { |
| 1596 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 1597 | case BitstreamEntry::Error: |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1598 | return Error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1599 | case BitstreamEntry::EndBlock: |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1600 | MDValueList.tryToResolveCycles(); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1601 | return std::error_code(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1602 | case BitstreamEntry::Record: |
| 1603 | // The interesting case. |
| 1604 | break; |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1605 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1606 | |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1607 | // Read a record. |
| 1608 | Record.clear(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1609 | unsigned Code = Stream.readRecord(Entry.ID, Record); |
Duncan P. N. Exon Smith | 090a19b | 2015-01-08 22:38:29 +0000 | [diff] [blame] | 1610 | bool IsDistinct = false; |
Dan Gohman | bbcd04d | 2010-09-13 18:00:48 +0000 | [diff] [blame] | 1611 | switch (Code) { |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1612 | default: // Default behavior: ignore. |
| 1613 | break; |
Devang Patel | 27c87ff | 2009-07-29 22:34:41 +0000 | [diff] [blame] | 1614 | case bitc::METADATA_NAME: { |
Chris Lattner | 8d14053 | 2013-01-20 02:54:05 +0000 | [diff] [blame] | 1615 | // Read name of the named metadata. |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 1616 | SmallString<8> Name(Record.begin(), Record.end()); |
Devang Patel | 27c87ff | 2009-07-29 22:34:41 +0000 | [diff] [blame] | 1617 | Record.clear(); |
| 1618 | Code = Stream.ReadCode(); |
| 1619 | |
Chris Lattner | b877855 | 2011-06-17 17:50:30 +0000 | [diff] [blame] | 1620 | // METADATA_NAME is always followed by METADATA_NAMED_NODE. |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1621 | unsigned NextBitCode = Stream.readRecord(Code, Record); |
Chris Lattner | b877855 | 2011-06-17 17:50:30 +0000 | [diff] [blame] | 1622 | assert(NextBitCode == bitc::METADATA_NAMED_NODE); (void)NextBitCode; |
Devang Patel | 27c87ff | 2009-07-29 22:34:41 +0000 | [diff] [blame] | 1623 | |
| 1624 | // Read named metadata elements. |
| 1625 | unsigned Size = Record.size(); |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 1626 | NamedMDNode *NMD = TheModule->getOrInsertNamedMetadata(Name); |
Devang Patel | 27c87ff | 2009-07-29 22:34:41 +0000 | [diff] [blame] | 1627 | for (unsigned i = 0; i != Size; ++i) { |
Karthik Bhat | 82540e9 | 2014-03-27 12:08:23 +0000 | [diff] [blame] | 1628 | MDNode *MD = dyn_cast_or_null<MDNode>(MDValueList.getValueFwdRef(Record[i])); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1629 | if (!MD) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1630 | return Error("Invalid record"); |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 1631 | NMD->addOperand(MD); |
Devang Patel | 27c87ff | 2009-07-29 22:34:41 +0000 | [diff] [blame] | 1632 | } |
Devang Patel | 27c87ff | 2009-07-29 22:34:41 +0000 | [diff] [blame] | 1633 | break; |
| 1634 | } |
Duncan P. N. Exon Smith | 005f9f4 | 2014-12-11 22:30:48 +0000 | [diff] [blame] | 1635 | case bitc::METADATA_OLD_FN_NODE: { |
Duncan P. N. Exon Smith | 5bd34e5 | 2014-12-12 02:11:31 +0000 | [diff] [blame] | 1636 | // FIXME: Remove in 4.0. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1637 | // This is a LocalAsMetadata record, the only type of function-local |
| 1638 | // metadata. |
Duncan P. N. Exon Smith | da41af9 | 2014-12-06 01:26:49 +0000 | [diff] [blame] | 1639 | if (Record.size() % 2 == 1) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1640 | return Error("Invalid record"); |
Duncan P. N. Exon Smith | da41af9 | 2014-12-06 01:26:49 +0000 | [diff] [blame] | 1641 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1642 | // If this isn't a LocalAsMetadata record, we're dropping it. This used |
| 1643 | // to be legal, but there's no upgrade path. |
Duncan P. N. Exon Smith | da41af9 | 2014-12-06 01:26:49 +0000 | [diff] [blame] | 1644 | auto dropRecord = [&] { |
| 1645 | MDValueList.AssignValue(MDNode::get(Context, None), NextMDValueNo++); |
| 1646 | }; |
| 1647 | if (Record.size() != 2) { |
| 1648 | dropRecord(); |
| 1649 | break; |
| 1650 | } |
| 1651 | |
| 1652 | Type *Ty = getTypeByID(Record[0]); |
| 1653 | if (Ty->isMetadataTy() || Ty->isVoidTy()) { |
| 1654 | dropRecord(); |
| 1655 | break; |
| 1656 | } |
| 1657 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1658 | MDValueList.AssignValue( |
| 1659 | LocalAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)), |
| 1660 | NextMDValueNo++); |
Duncan P. N. Exon Smith | da41af9 | 2014-12-06 01:26:49 +0000 | [diff] [blame] | 1661 | break; |
| 1662 | } |
Duncan P. N. Exon Smith | 005f9f4 | 2014-12-11 22:30:48 +0000 | [diff] [blame] | 1663 | case bitc::METADATA_OLD_NODE: { |
Duncan P. N. Exon Smith | 5bd34e5 | 2014-12-12 02:11:31 +0000 | [diff] [blame] | 1664 | // FIXME: Remove in 4.0. |
Dan Gohman | 1e0213a | 2010-07-13 19:33:27 +0000 | [diff] [blame] | 1665 | if (Record.size() % 2 == 1) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1666 | return Error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1667 | |
Devang Patel | e059ba6e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 1668 | unsigned Size = Record.size(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1669 | SmallVector<Metadata *, 8> Elts; |
Devang Patel | e059ba6e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 1670 | for (unsigned i = 0; i != Size; i += 2) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1671 | Type *Ty = getTypeByID(Record[i]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1672 | if (!Ty) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1673 | return Error("Invalid record"); |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 1674 | if (Ty->isMetadataTy()) |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 1675 | Elts.push_back(MDValueList.getValueFwdRef(Record[i+1])); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1676 | else if (!Ty->isVoidTy()) { |
| 1677 | auto *MD = |
| 1678 | ValueAsMetadata::get(ValueList.getValueFwdRef(Record[i + 1], Ty)); |
| 1679 | assert(isa<ConstantAsMetadata>(MD) && |
| 1680 | "Expected non-function-local metadata"); |
| 1681 | Elts.push_back(MD); |
| 1682 | } else |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1683 | Elts.push_back(nullptr); |
Devang Patel | e059ba6e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 1684 | } |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1685 | MDValueList.AssignValue(MDNode::get(Context, Elts), NextMDValueNo++); |
Devang Patel | e059ba6e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 1686 | break; |
| 1687 | } |
Duncan P. N. Exon Smith | 5c7006e | 2014-12-11 23:02:24 +0000 | [diff] [blame] | 1688 | case bitc::METADATA_VALUE: { |
| 1689 | if (Record.size() != 2) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1690 | return Error("Invalid record"); |
Duncan P. N. Exon Smith | 5c7006e | 2014-12-11 23:02:24 +0000 | [diff] [blame] | 1691 | |
| 1692 | Type *Ty = getTypeByID(Record[0]); |
| 1693 | if (Ty->isMetadataTy() || Ty->isVoidTy()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1694 | return Error("Invalid record"); |
Duncan P. N. Exon Smith | 5c7006e | 2014-12-11 23:02:24 +0000 | [diff] [blame] | 1695 | |
| 1696 | MDValueList.AssignValue( |
| 1697 | ValueAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)), |
| 1698 | NextMDValueNo++); |
| 1699 | break; |
| 1700 | } |
Duncan P. N. Exon Smith | 090a19b | 2015-01-08 22:38:29 +0000 | [diff] [blame] | 1701 | case bitc::METADATA_DISTINCT_NODE: |
| 1702 | IsDistinct = true; |
| 1703 | // fallthrough... |
Duncan P. N. Exon Smith | 5c7006e | 2014-12-11 23:02:24 +0000 | [diff] [blame] | 1704 | case bitc::METADATA_NODE: { |
| 1705 | SmallVector<Metadata *, 8> Elts; |
| 1706 | Elts.reserve(Record.size()); |
| 1707 | for (unsigned ID : Record) |
| 1708 | Elts.push_back(ID ? MDValueList.getValueFwdRef(ID - 1) : nullptr); |
Duncan P. N. Exon Smith | 090a19b | 2015-01-08 22:38:29 +0000 | [diff] [blame] | 1709 | MDValueList.AssignValue(IsDistinct ? MDNode::getDistinct(Context, Elts) |
| 1710 | : MDNode::get(Context, Elts), |
| 1711 | NextMDValueNo++); |
Duncan P. N. Exon Smith | 5c7006e | 2014-12-11 23:02:24 +0000 | [diff] [blame] | 1712 | break; |
| 1713 | } |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 1714 | case bitc::METADATA_LOCATION: { |
| 1715 | if (Record.size() != 5) |
| 1716 | return Error("Invalid record"); |
| 1717 | |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 1718 | unsigned Line = Record[1]; |
| 1719 | unsigned Column = Record[2]; |
| 1720 | MDNode *Scope = cast<MDNode>(MDValueList.getValueFwdRef(Record[3])); |
| 1721 | Metadata *InlinedAt = |
| 1722 | Record[4] ? MDValueList.getValueFwdRef(Record[4] - 1) : nullptr; |
Duncan P. N. Exon Smith | 2648998 | 2015-03-26 22:05:04 +0000 | [diff] [blame] | 1723 | MDValueList.AssignValue( |
| 1724 | GET_OR_DISTINCT(MDLocation, Record[0], |
| 1725 | (Context, Line, Column, Scope, InlinedAt)), |
| 1726 | NextMDValueNo++); |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 1727 | break; |
| 1728 | } |
Duncan P. N. Exon Smith | 4e4aa70 | 2015-02-03 21:54:14 +0000 | [diff] [blame] | 1729 | case bitc::METADATA_GENERIC_DEBUG: { |
| 1730 | if (Record.size() < 4) |
| 1731 | return Error("Invalid record"); |
| 1732 | |
| 1733 | unsigned Tag = Record[1]; |
| 1734 | unsigned Version = Record[2]; |
| 1735 | |
| 1736 | if (Tag >= 1u << 16 || Version != 0) |
| 1737 | return Error("Invalid record"); |
| 1738 | |
| 1739 | auto *Header = getMDString(Record[3]); |
| 1740 | SmallVector<Metadata *, 8> DwarfOps; |
| 1741 | for (unsigned I = 4, E = Record.size(); I != E; ++I) |
| 1742 | DwarfOps.push_back(Record[I] ? MDValueList.getValueFwdRef(Record[I] - 1) |
| 1743 | : nullptr); |
| 1744 | MDValueList.AssignValue(GET_OR_DISTINCT(GenericDebugNode, Record[0], |
| 1745 | (Context, Tag, Header, DwarfOps)), |
| 1746 | NextMDValueNo++); |
| 1747 | break; |
| 1748 | } |
Duncan P. N. Exon Smith | c7363f1 | 2015-02-13 01:10:38 +0000 | [diff] [blame] | 1749 | case bitc::METADATA_SUBRANGE: { |
| 1750 | if (Record.size() != 3) |
| 1751 | return Error("Invalid record"); |
| 1752 | |
| 1753 | MDValueList.AssignValue( |
| 1754 | GET_OR_DISTINCT(MDSubrange, Record[0], |
| 1755 | (Context, Record[1], unrotateSign(Record[2]))), |
| 1756 | NextMDValueNo++); |
| 1757 | break; |
| 1758 | } |
Duncan P. N. Exon Smith | 8775476 | 2015-02-13 01:14:11 +0000 | [diff] [blame] | 1759 | case bitc::METADATA_ENUMERATOR: { |
| 1760 | if (Record.size() != 3) |
| 1761 | return Error("Invalid record"); |
| 1762 | |
| 1763 | MDValueList.AssignValue(GET_OR_DISTINCT(MDEnumerator, Record[0], |
| 1764 | (Context, unrotateSign(Record[1]), |
| 1765 | getMDString(Record[2]))), |
| 1766 | NextMDValueNo++); |
| 1767 | break; |
| 1768 | } |
Duncan P. N. Exon Smith | 09e03f3 | 2015-02-13 01:14:58 +0000 | [diff] [blame] | 1769 | case bitc::METADATA_BASIC_TYPE: { |
| 1770 | if (Record.size() != 6) |
| 1771 | return Error("Invalid record"); |
| 1772 | |
| 1773 | MDValueList.AssignValue( |
| 1774 | GET_OR_DISTINCT(MDBasicType, Record[0], |
| 1775 | (Context, Record[1], getMDString(Record[2]), |
| 1776 | Record[3], Record[4], Record[5])), |
| 1777 | NextMDValueNo++); |
| 1778 | break; |
| 1779 | } |
Duncan P. N. Exon Smith | 171d077 | 2015-02-13 01:20:38 +0000 | [diff] [blame] | 1780 | case bitc::METADATA_DERIVED_TYPE: { |
| 1781 | if (Record.size() != 12) |
| 1782 | return Error("Invalid record"); |
| 1783 | |
| 1784 | MDValueList.AssignValue( |
| 1785 | GET_OR_DISTINCT(MDDerivedType, Record[0], |
| 1786 | (Context, Record[1], getMDString(Record[2]), |
| 1787 | getMDOrNull(Record[3]), Record[4], |
Duncan P. N. Exon Smith | ad6eb127 | 2015-02-20 03:17:58 +0000 | [diff] [blame] | 1788 | getMDOrNull(Record[5]), getMDOrNull(Record[6]), |
| 1789 | Record[7], Record[8], Record[9], Record[10], |
Duncan P. N. Exon Smith | 171d077 | 2015-02-13 01:20:38 +0000 | [diff] [blame] | 1790 | getMDOrNull(Record[11]))), |
| 1791 | NextMDValueNo++); |
| 1792 | break; |
| 1793 | } |
| 1794 | case bitc::METADATA_COMPOSITE_TYPE: { |
| 1795 | if (Record.size() != 16) |
| 1796 | return Error("Invalid record"); |
| 1797 | |
| 1798 | MDValueList.AssignValue( |
| 1799 | GET_OR_DISTINCT(MDCompositeType, Record[0], |
| 1800 | (Context, Record[1], getMDString(Record[2]), |
| 1801 | getMDOrNull(Record[3]), Record[4], |
| 1802 | getMDOrNull(Record[5]), getMDOrNull(Record[6]), |
| 1803 | Record[7], Record[8], Record[9], Record[10], |
| 1804 | getMDOrNull(Record[11]), Record[12], |
| 1805 | getMDOrNull(Record[13]), getMDOrNull(Record[14]), |
| 1806 | getMDString(Record[15]))), |
| 1807 | NextMDValueNo++); |
| 1808 | break; |
| 1809 | } |
Duncan P. N. Exon Smith | 54e2bc6 | 2015-02-13 01:22:59 +0000 | [diff] [blame] | 1810 | case bitc::METADATA_SUBROUTINE_TYPE: { |
| 1811 | if (Record.size() != 3) |
| 1812 | return Error("Invalid record"); |
| 1813 | |
| 1814 | MDValueList.AssignValue( |
| 1815 | GET_OR_DISTINCT(MDSubroutineType, Record[0], |
| 1816 | (Context, Record[1], getMDOrNull(Record[2]))), |
| 1817 | NextMDValueNo++); |
| 1818 | break; |
| 1819 | } |
Duncan P. N. Exon Smith | f14b9c7 | 2015-02-13 01:19:14 +0000 | [diff] [blame] | 1820 | case bitc::METADATA_FILE: { |
| 1821 | if (Record.size() != 3) |
| 1822 | return Error("Invalid record"); |
| 1823 | |
| 1824 | MDValueList.AssignValue( |
| 1825 | GET_OR_DISTINCT(MDFile, Record[0], (Context, getMDString(Record[1]), |
| 1826 | getMDString(Record[2]))), |
| 1827 | NextMDValueNo++); |
| 1828 | break; |
| 1829 | } |
Duncan P. N. Exon Smith | c1f1acc | 2015-02-13 01:25:10 +0000 | [diff] [blame] | 1830 | case bitc::METADATA_COMPILE_UNIT: { |
| 1831 | if (Record.size() != 14) |
| 1832 | return Error("Invalid record"); |
| 1833 | |
| 1834 | MDValueList.AssignValue( |
Duncan P. N. Exon Smith | ad6eb127 | 2015-02-20 03:17:58 +0000 | [diff] [blame] | 1835 | GET_OR_DISTINCT(MDCompileUnit, Record[0], |
| 1836 | (Context, Record[1], getMDOrNull(Record[2]), |
| 1837 | getMDString(Record[3]), Record[4], |
| 1838 | getMDString(Record[5]), Record[6], |
| 1839 | getMDString(Record[7]), Record[8], |
| 1840 | getMDOrNull(Record[9]), getMDOrNull(Record[10]), |
| 1841 | getMDOrNull(Record[11]), getMDOrNull(Record[12]), |
| 1842 | getMDOrNull(Record[13]))), |
Duncan P. N. Exon Smith | c1f1acc | 2015-02-13 01:25:10 +0000 | [diff] [blame] | 1843 | NextMDValueNo++); |
| 1844 | break; |
| 1845 | } |
Duncan P. N. Exon Smith | 19fc5ed | 2015-02-13 01:26:47 +0000 | [diff] [blame] | 1846 | case bitc::METADATA_SUBPROGRAM: { |
| 1847 | if (Record.size() != 19) |
| 1848 | return Error("Invalid record"); |
| 1849 | |
| 1850 | MDValueList.AssignValue( |
| 1851 | GET_OR_DISTINCT( |
| 1852 | MDSubprogram, Record[0], |
| 1853 | (Context, getMDOrNull(Record[1]), getMDString(Record[2]), |
| 1854 | getMDString(Record[3]), getMDOrNull(Record[4]), Record[5], |
| 1855 | getMDOrNull(Record[6]), Record[7], Record[8], Record[9], |
| 1856 | getMDOrNull(Record[10]), Record[11], Record[12], Record[13], |
| 1857 | Record[14], getMDOrNull(Record[15]), getMDOrNull(Record[16]), |
| 1858 | getMDOrNull(Record[17]), getMDOrNull(Record[18]))), |
| 1859 | NextMDValueNo++); |
| 1860 | break; |
| 1861 | } |
Duncan P. N. Exon Smith | a96d409 | 2015-02-13 01:29:28 +0000 | [diff] [blame] | 1862 | case bitc::METADATA_LEXICAL_BLOCK: { |
| 1863 | if (Record.size() != 5) |
| 1864 | return Error("Invalid record"); |
| 1865 | |
| 1866 | MDValueList.AssignValue( |
| 1867 | GET_OR_DISTINCT(MDLexicalBlock, Record[0], |
| 1868 | (Context, getMDOrNull(Record[1]), |
| 1869 | getMDOrNull(Record[2]), Record[3], Record[4])), |
| 1870 | NextMDValueNo++); |
| 1871 | break; |
| 1872 | } |
Duncan P. N. Exon Smith | 06a0702 | 2015-02-13 01:30:42 +0000 | [diff] [blame] | 1873 | case bitc::METADATA_LEXICAL_BLOCK_FILE: { |
| 1874 | if (Record.size() != 4) |
| 1875 | return Error("Invalid record"); |
| 1876 | |
| 1877 | MDValueList.AssignValue( |
| 1878 | GET_OR_DISTINCT(MDLexicalBlockFile, Record[0], |
| 1879 | (Context, getMDOrNull(Record[1]), |
| 1880 | getMDOrNull(Record[2]), Record[3])), |
| 1881 | NextMDValueNo++); |
| 1882 | break; |
| 1883 | } |
Duncan P. N. Exon Smith | e146000 | 2015-02-13 01:32:09 +0000 | [diff] [blame] | 1884 | case bitc::METADATA_NAMESPACE: { |
| 1885 | if (Record.size() != 5) |
| 1886 | return Error("Invalid record"); |
| 1887 | |
| 1888 | MDValueList.AssignValue( |
| 1889 | GET_OR_DISTINCT(MDNamespace, Record[0], |
| 1890 | (Context, getMDOrNull(Record[1]), |
| 1891 | getMDOrNull(Record[2]), getMDString(Record[3]), |
| 1892 | Record[4])), |
| 1893 | NextMDValueNo++); |
| 1894 | break; |
| 1895 | } |
Duncan P. N. Exon Smith | 2847f38 | 2015-02-13 01:34:32 +0000 | [diff] [blame] | 1896 | case bitc::METADATA_TEMPLATE_TYPE: { |
Duncan P. N. Exon Smith | 3d62bba | 2015-02-19 00:37:21 +0000 | [diff] [blame] | 1897 | if (Record.size() != 3) |
Duncan P. N. Exon Smith | 2847f38 | 2015-02-13 01:34:32 +0000 | [diff] [blame] | 1898 | return Error("Invalid record"); |
| 1899 | |
Duncan P. N. Exon Smith | 3d62bba | 2015-02-19 00:37:21 +0000 | [diff] [blame] | 1900 | MDValueList.AssignValue(GET_OR_DISTINCT(MDTemplateTypeParameter, |
| 1901 | Record[0], |
| 1902 | (Context, getMDString(Record[1]), |
| 1903 | getMDOrNull(Record[2]))), |
| 1904 | NextMDValueNo++); |
Duncan P. N. Exon Smith | 2847f38 | 2015-02-13 01:34:32 +0000 | [diff] [blame] | 1905 | break; |
| 1906 | } |
| 1907 | case bitc::METADATA_TEMPLATE_VALUE: { |
Duncan P. N. Exon Smith | 3d62bba | 2015-02-19 00:37:21 +0000 | [diff] [blame] | 1908 | if (Record.size() != 5) |
Duncan P. N. Exon Smith | 2847f38 | 2015-02-13 01:34:32 +0000 | [diff] [blame] | 1909 | return Error("Invalid record"); |
| 1910 | |
| 1911 | MDValueList.AssignValue( |
| 1912 | GET_OR_DISTINCT(MDTemplateValueParameter, Record[0], |
Duncan P. N. Exon Smith | 3d62bba | 2015-02-19 00:37:21 +0000 | [diff] [blame] | 1913 | (Context, Record[1], getMDString(Record[2]), |
| 1914 | getMDOrNull(Record[3]), getMDOrNull(Record[4]))), |
Duncan P. N. Exon Smith | 2847f38 | 2015-02-13 01:34:32 +0000 | [diff] [blame] | 1915 | NextMDValueNo++); |
| 1916 | break; |
| 1917 | } |
Duncan P. N. Exon Smith | c8f810a | 2015-02-13 01:35:40 +0000 | [diff] [blame] | 1918 | case bitc::METADATA_GLOBAL_VAR: { |
| 1919 | if (Record.size() != 11) |
| 1920 | return Error("Invalid record"); |
| 1921 | |
| 1922 | MDValueList.AssignValue( |
| 1923 | GET_OR_DISTINCT(MDGlobalVariable, Record[0], |
| 1924 | (Context, getMDOrNull(Record[1]), |
| 1925 | getMDString(Record[2]), getMDString(Record[3]), |
| 1926 | getMDOrNull(Record[4]), Record[5], |
| 1927 | getMDOrNull(Record[6]), Record[7], Record[8], |
| 1928 | getMDOrNull(Record[9]), getMDOrNull(Record[10]))), |
| 1929 | NextMDValueNo++); |
| 1930 | break; |
| 1931 | } |
Duncan P. N. Exon Smith | 72fe2d0 | 2015-02-13 01:39:44 +0000 | [diff] [blame] | 1932 | case bitc::METADATA_LOCAL_VAR: { |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 1933 | // 10th field is for the obseleted 'inlinedAt:' field. |
| 1934 | if (Record.size() != 9 && Record.size() != 10) |
Duncan P. N. Exon Smith | 72fe2d0 | 2015-02-13 01:39:44 +0000 | [diff] [blame] | 1935 | return Error("Invalid record"); |
| 1936 | |
| 1937 | MDValueList.AssignValue( |
| 1938 | GET_OR_DISTINCT(MDLocalVariable, Record[0], |
| 1939 | (Context, Record[1], getMDOrNull(Record[2]), |
| 1940 | getMDString(Record[3]), getMDOrNull(Record[4]), |
| 1941 | Record[5], getMDOrNull(Record[6]), Record[7], |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 1942 | Record[8])), |
Duncan P. N. Exon Smith | 72fe2d0 | 2015-02-13 01:39:44 +0000 | [diff] [blame] | 1943 | NextMDValueNo++); |
| 1944 | break; |
| 1945 | } |
Duncan P. N. Exon Smith | 0c5c012 | 2015-02-13 01:42:09 +0000 | [diff] [blame] | 1946 | case bitc::METADATA_EXPRESSION: { |
| 1947 | if (Record.size() < 1) |
| 1948 | return Error("Invalid record"); |
| 1949 | |
| 1950 | MDValueList.AssignValue( |
| 1951 | GET_OR_DISTINCT(MDExpression, Record[0], |
| 1952 | (Context, makeArrayRef(Record).slice(1))), |
| 1953 | NextMDValueNo++); |
| 1954 | break; |
| 1955 | } |
Duncan P. N. Exon Smith | d45ce96 | 2015-02-13 01:43:22 +0000 | [diff] [blame] | 1956 | case bitc::METADATA_OBJC_PROPERTY: { |
| 1957 | if (Record.size() != 8) |
| 1958 | return Error("Invalid record"); |
| 1959 | |
| 1960 | MDValueList.AssignValue( |
| 1961 | GET_OR_DISTINCT(MDObjCProperty, Record[0], |
| 1962 | (Context, getMDString(Record[1]), |
| 1963 | getMDOrNull(Record[2]), Record[3], |
| 1964 | getMDString(Record[4]), getMDString(Record[5]), |
| 1965 | Record[6], getMDOrNull(Record[7]))), |
| 1966 | NextMDValueNo++); |
| 1967 | break; |
| 1968 | } |
Duncan P. N. Exon Smith | 1c93116 | 2015-02-13 01:46:02 +0000 | [diff] [blame] | 1969 | case bitc::METADATA_IMPORTED_ENTITY: { |
| 1970 | if (Record.size() != 6) |
| 1971 | return Error("Invalid record"); |
| 1972 | |
| 1973 | MDValueList.AssignValue( |
| 1974 | GET_OR_DISTINCT(MDImportedEntity, Record[0], |
| 1975 | (Context, Record[1], getMDOrNull(Record[2]), |
| 1976 | getMDOrNull(Record[3]), Record[4], |
| 1977 | getMDString(Record[5]))), |
| 1978 | NextMDValueNo++); |
| 1979 | break; |
| 1980 | } |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1981 | case bitc::METADATA_STRING: { |
Eli Bendersky | 5d5e18d | 2014-06-25 15:41:00 +0000 | [diff] [blame] | 1982 | std::string String(Record.begin(), Record.end()); |
| 1983 | llvm::UpgradeMDStringConstant(String); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1984 | Metadata *MD = MDString::get(Context, String); |
| 1985 | MDValueList.AssignValue(MD, NextMDValueNo++); |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1986 | break; |
| 1987 | } |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 1988 | case bitc::METADATA_KIND: { |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 1989 | if (Record.size() < 2) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1990 | return Error("Invalid record"); |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 1991 | |
Devang Patel | b1a4477 | 2009-09-28 21:14:55 +0000 | [diff] [blame] | 1992 | unsigned Kind = Record[0]; |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 1993 | SmallString<8> Name(Record.begin()+1, Record.end()); |
| 1994 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 1995 | unsigned NewKind = TheModule->getMDKindID(Name.str()); |
Dan Gohman | 43aa8f0 | 2010-07-20 21:42:28 +0000 | [diff] [blame] | 1996 | if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 1997 | return Error("Conflicting METADATA_KIND records"); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 1998 | break; |
| 1999 | } |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 2000 | } |
| 2001 | } |
Duncan P. N. Exon Smith | 4e4aa70 | 2015-02-03 21:54:14 +0000 | [diff] [blame] | 2002 | #undef GET_OR_DISTINCT |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 2005 | /// decodeSignRotatedValue - Decode a signed value stored with the sign bit in |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 2006 | /// the LSB for dense VBR encoding. |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 2007 | uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) { |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 2008 | if ((V & 1) == 0) |
| 2009 | return V >> 1; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2010 | if (V != 1) |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 2011 | return -(V >> 1); |
| 2012 | // There is no such thing as -0 with integers. "-0" really means MININT. |
| 2013 | return 1ULL << 63; |
| 2014 | } |
| 2015 | |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 2016 | /// ResolveGlobalAndAliasInits - Resolve all of the initializers for global |
| 2017 | /// values and aliases that we can. |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2018 | std::error_code BitcodeReader::ResolveGlobalAndAliasInits() { |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 2019 | std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist; |
| 2020 | std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist; |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 2021 | std::vector<std::pair<Function*, unsigned> > FunctionPrefixWorklist; |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 2022 | std::vector<std::pair<Function*, unsigned> > FunctionPrologueWorklist; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2023 | |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 2024 | GlobalInitWorklist.swap(GlobalInits); |
| 2025 | AliasInitWorklist.swap(AliasInits); |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 2026 | FunctionPrefixWorklist.swap(FunctionPrefixes); |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 2027 | FunctionPrologueWorklist.swap(FunctionPrologues); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 2028 | |
| 2029 | while (!GlobalInitWorklist.empty()) { |
Chris Lattner | 831d420 | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 2030 | unsigned ValID = GlobalInitWorklist.back().second; |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 2031 | if (ValID >= ValueList.size()) { |
| 2032 | // 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] | 2033 | GlobalInits.push_back(GlobalInitWorklist.back()); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 2034 | } else { |
Karthik Bhat | 82540e9 | 2014-03-27 12:08:23 +0000 | [diff] [blame] | 2035 | if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 2036 | GlobalInitWorklist.back().first->setInitializer(C); |
| 2037 | else |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2038 | return Error("Expected a constant"); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 2039 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2040 | GlobalInitWorklist.pop_back(); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | while (!AliasInitWorklist.empty()) { |
| 2044 | unsigned ValID = AliasInitWorklist.back().second; |
| 2045 | if (ValID >= ValueList.size()) { |
| 2046 | AliasInits.push_back(AliasInitWorklist.back()); |
| 2047 | } else { |
Karthik Bhat | 82540e9 | 2014-03-27 12:08:23 +0000 | [diff] [blame] | 2048 | if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) |
Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 2049 | AliasInitWorklist.back().first->setAliasee(C); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 2050 | else |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2051 | return Error("Expected a constant"); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 2052 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2053 | AliasInitWorklist.pop_back(); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 2054 | } |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 2055 | |
| 2056 | while (!FunctionPrefixWorklist.empty()) { |
| 2057 | unsigned ValID = FunctionPrefixWorklist.back().second; |
| 2058 | if (ValID >= ValueList.size()) { |
| 2059 | FunctionPrefixes.push_back(FunctionPrefixWorklist.back()); |
| 2060 | } else { |
Karthik Bhat | 82540e9 | 2014-03-27 12:08:23 +0000 | [diff] [blame] | 2061 | if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 2062 | FunctionPrefixWorklist.back().first->setPrefixData(C); |
| 2063 | else |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2064 | return Error("Expected a constant"); |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 2065 | } |
| 2066 | FunctionPrefixWorklist.pop_back(); |
| 2067 | } |
| 2068 | |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 2069 | while (!FunctionPrologueWorklist.empty()) { |
| 2070 | unsigned ValID = FunctionPrologueWorklist.back().second; |
| 2071 | if (ValID >= ValueList.size()) { |
| 2072 | FunctionPrologues.push_back(FunctionPrologueWorklist.back()); |
| 2073 | } else { |
| 2074 | if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) |
| 2075 | FunctionPrologueWorklist.back().first->setPrologueData(C); |
| 2076 | else |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2077 | return Error("Expected a constant"); |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 2078 | } |
| 2079 | FunctionPrologueWorklist.pop_back(); |
| 2080 | } |
| 2081 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2082 | return std::error_code(); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 2083 | } |
| 2084 | |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 2085 | static APInt ReadWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) { |
| 2086 | SmallVector<uint64_t, 8> Words(Vals.size()); |
| 2087 | std::transform(Vals.begin(), Vals.end(), Words.begin(), |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 2088 | BitcodeReader::decodeSignRotatedValue); |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 2089 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 2090 | return APInt(TypeBits, Words); |
| 2091 | } |
| 2092 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2093 | std::error_code BitcodeReader::ParseConstants() { |
Chris Lattner | 982ec1e | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 2094 | if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2095 | return Error("Invalid record"); |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2096 | |
| 2097 | SmallVector<uint64_t, 64> Record; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2098 | |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2099 | // Read all the records for this value table. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2100 | Type *CurTy = Type::getInt32Ty(Context); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 2101 | unsigned NextCstNo = ValueList.size(); |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2102 | while (1) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2103 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2104 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2105 | switch (Entry.Kind) { |
| 2106 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 2107 | case BitstreamEntry::Error: |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2108 | return Error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2109 | case BitstreamEntry::EndBlock: |
| 2110 | if (NextCstNo != ValueList.size()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2111 | return Error("Invalid ronstant reference"); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2112 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2113 | // Once all the constants have been read, go through and resolve forward |
| 2114 | // references. |
| 2115 | ValueList.ResolveConstantForwardRefs(); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2116 | return std::error_code(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2117 | case BitstreamEntry::Record: |
| 2118 | // The interesting case. |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 2119 | break; |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2120 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2121 | |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2122 | // Read a record. |
| 2123 | Record.clear(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2124 | Value *V = nullptr; |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2125 | unsigned BitCode = Stream.readRecord(Entry.ID, Record); |
Dan Gohman | 0ebd696 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 2126 | switch (BitCode) { |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2127 | default: // Default behavior: unknown constant |
| 2128 | case bitc::CST_CODE_UNDEF: // UNDEF |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2129 | V = UndefValue::get(CurTy); |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2130 | break; |
| 2131 | case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid] |
| 2132 | if (Record.empty()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2133 | return Error("Invalid record"); |
Karthik Bhat | 82540e9 | 2014-03-27 12:08:23 +0000 | [diff] [blame] | 2134 | if (Record[0] >= TypeList.size() || !TypeList[Record[0]]) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2135 | return Error("Invalid record"); |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2136 | CurTy = TypeList[Record[0]]; |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 2137 | continue; // Skip the ValueList manipulation. |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2138 | case bitc::CST_CODE_NULL: // NULL |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 2139 | V = Constant::getNullValue(CurTy); |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2140 | break; |
| 2141 | case bitc::CST_CODE_INTEGER: // INTEGER: [intval] |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2142 | if (!CurTy->isIntegerTy() || Record.empty()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2143 | return Error("Invalid record"); |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 2144 | V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0])); |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 2145 | break; |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 2146 | case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval] |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2147 | if (!CurTy->isIntegerTy() || Record.empty()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2148 | return Error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2149 | |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 2150 | APInt VInt = ReadWideAPInt(Record, |
| 2151 | cast<IntegerType>(CurTy)->getBitWidth()); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 2152 | V = ConstantInt::get(Context, VInt); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2153 | |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 2154 | break; |
| 2155 | } |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 2156 | case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval] |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 2157 | if (Record.empty()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2158 | return Error("Invalid record"); |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 2159 | if (CurTy->isHalfTy()) |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 2160 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf, |
| 2161 | APInt(16, (uint16_t)Record[0]))); |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 2162 | else if (CurTy->isFloatTy()) |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 2163 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle, |
| 2164 | APInt(32, (uint32_t)Record[0]))); |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 2165 | else if (CurTy->isDoubleTy()) |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 2166 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble, |
| 2167 | APInt(64, Record[0]))); |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 2168 | else if (CurTy->isX86_FP80Ty()) { |
Dale Johannesen | 93eefa0 | 2009-03-23 21:16:53 +0000 | [diff] [blame] | 2169 | // Bits are not stored the same way as a normal i80 APInt, compensate. |
| 2170 | uint64_t Rearrange[2]; |
| 2171 | Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16); |
| 2172 | Rearrange[1] = Record[0] >> 48; |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 2173 | V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended, |
| 2174 | APInt(80, Rearrange))); |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 2175 | } else if (CurTy->isFP128Ty()) |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 2176 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad, |
| 2177 | APInt(128, Record))); |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 2178 | else if (CurTy->isPPC_FP128Ty()) |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 2179 | V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble, |
| 2180 | APInt(128, Record))); |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2181 | else |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2182 | V = UndefValue::get(CurTy); |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2183 | break; |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 2184 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2185 | |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 2186 | case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number] |
| 2187 | if (Record.empty()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2188 | return Error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2189 | |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 2190 | unsigned Size = Record.size(); |
Chris Lattner | cc3aaf1 | 2012-01-27 03:15:49 +0000 | [diff] [blame] | 2191 | SmallVector<Constant*, 16> Elts; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2192 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2193 | if (StructType *STy = dyn_cast<StructType>(CurTy)) { |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 2194 | for (unsigned i = 0; i != Size; ++i) |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 2195 | Elts.push_back(ValueList.getConstantFwdRef(Record[i], |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 2196 | STy->getElementType(i))); |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2197 | V = ConstantStruct::get(STy, Elts); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2198 | } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) { |
| 2199 | Type *EltTy = ATy->getElementType(); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 2200 | for (unsigned i = 0; i != Size; ++i) |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 2201 | Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 2202 | V = ConstantArray::get(ATy, Elts); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2203 | } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) { |
| 2204 | Type *EltTy = VTy->getElementType(); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 2205 | for (unsigned i = 0; i != Size; ++i) |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 2206 | Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 2207 | V = ConstantVector::get(Elts); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 2208 | } else { |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2209 | V = UndefValue::get(CurTy); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 2210 | } |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2211 | break; |
| 2212 | } |
Chris Lattner | bb8278a | 2012-02-05 02:41:35 +0000 | [diff] [blame] | 2213 | case bitc::CST_CODE_STRING: // STRING: [values] |
Chris Lattner | f25f710 | 2007-05-06 00:53:07 +0000 | [diff] [blame] | 2214 | case bitc::CST_CODE_CSTRING: { // CSTRING: [values] |
| 2215 | if (Record.empty()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2216 | return Error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2217 | |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 2218 | SmallString<16> Elts(Record.begin(), Record.end()); |
Chris Lattner | bb8278a | 2012-02-05 02:41:35 +0000 | [diff] [blame] | 2219 | V = ConstantDataArray::getString(Context, Elts, |
| 2220 | BitCode == bitc::CST_CODE_CSTRING); |
Chris Lattner | f25f710 | 2007-05-06 00:53:07 +0000 | [diff] [blame] | 2221 | break; |
| 2222 | } |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2223 | case bitc::CST_CODE_DATA: {// DATA: [n x value] |
| 2224 | if (Record.empty()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2225 | return Error("Invalid record"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2226 | |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2227 | Type *EltTy = cast<SequentialType>(CurTy)->getElementType(); |
| 2228 | unsigned Size = Record.size(); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2229 | |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2230 | if (EltTy->isIntegerTy(8)) { |
| 2231 | SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end()); |
| 2232 | if (isa<VectorType>(CurTy)) |
| 2233 | V = ConstantDataVector::get(Context, Elts); |
| 2234 | else |
| 2235 | V = ConstantDataArray::get(Context, Elts); |
| 2236 | } else if (EltTy->isIntegerTy(16)) { |
| 2237 | SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); |
| 2238 | if (isa<VectorType>(CurTy)) |
| 2239 | V = ConstantDataVector::get(Context, Elts); |
| 2240 | else |
| 2241 | V = ConstantDataArray::get(Context, Elts); |
| 2242 | } else if (EltTy->isIntegerTy(32)) { |
| 2243 | SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); |
| 2244 | if (isa<VectorType>(CurTy)) |
| 2245 | V = ConstantDataVector::get(Context, Elts); |
| 2246 | else |
| 2247 | V = ConstantDataArray::get(Context, Elts); |
| 2248 | } else if (EltTy->isIntegerTy(64)) { |
| 2249 | SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); |
| 2250 | if (isa<VectorType>(CurTy)) |
| 2251 | V = ConstantDataVector::get(Context, Elts); |
| 2252 | else |
| 2253 | V = ConstantDataArray::get(Context, Elts); |
| 2254 | } else if (EltTy->isFloatTy()) { |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 2255 | SmallVector<float, 16> Elts(Size); |
| 2256 | std::transform(Record.begin(), Record.end(), Elts.begin(), BitsToFloat); |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2257 | if (isa<VectorType>(CurTy)) |
| 2258 | V = ConstantDataVector::get(Context, Elts); |
| 2259 | else |
| 2260 | V = ConstantDataArray::get(Context, Elts); |
| 2261 | } else if (EltTy->isDoubleTy()) { |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 2262 | SmallVector<double, 16> Elts(Size); |
| 2263 | std::transform(Record.begin(), Record.end(), Elts.begin(), |
| 2264 | BitsToDouble); |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2265 | if (isa<VectorType>(CurTy)) |
| 2266 | V = ConstantDataVector::get(Context, Elts); |
| 2267 | else |
| 2268 | V = ConstantDataArray::get(Context, Elts); |
| 2269 | } else { |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2270 | return Error("Invalid type for value"); |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2271 | } |
| 2272 | break; |
| 2273 | } |
| 2274 | |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2275 | case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval] |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2276 | if (Record.size() < 3) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2277 | return Error("Invalid record"); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2278 | int Opc = GetDecodedBinaryOpcode(Record[0], CurTy); |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 2279 | if (Opc < 0) { |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2280 | V = UndefValue::get(CurTy); // Unknown binop. |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 2281 | } else { |
| 2282 | Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy); |
| 2283 | Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy); |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2284 | unsigned Flags = 0; |
| 2285 | if (Record.size() >= 4) { |
| 2286 | if (Opc == Instruction::Add || |
| 2287 | Opc == Instruction::Sub || |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2288 | Opc == Instruction::Mul || |
| 2289 | Opc == Instruction::Shl) { |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2290 | if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP)) |
| 2291 | Flags |= OverflowingBinaryOperator::NoSignedWrap; |
| 2292 | if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) |
| 2293 | Flags |= OverflowingBinaryOperator::NoUnsignedWrap; |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 2294 | } else if (Opc == Instruction::SDiv || |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2295 | Opc == Instruction::UDiv || |
| 2296 | Opc == Instruction::LShr || |
| 2297 | Opc == Instruction::AShr) { |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 2298 | if (Record[3] & (1 << bitc::PEO_EXACT)) |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2299 | Flags |= SDivOperator::IsExact; |
| 2300 | } |
| 2301 | } |
| 2302 | V = ConstantExpr::get(Opc, LHS, RHS, Flags); |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 2303 | } |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2304 | break; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2305 | } |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2306 | case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval] |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2307 | if (Record.size() < 3) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2308 | return Error("Invalid record"); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2309 | int Opc = GetDecodedCastOpcode(Record[0]); |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 2310 | if (Opc < 0) { |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2311 | V = UndefValue::get(CurTy); // Unknown cast. |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 2312 | } else { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2313 | Type *OpTy = getTypeByID(Record[1]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2314 | if (!OpTy) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2315 | return Error("Invalid record"); |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 2316 | Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy); |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2317 | V = UpgradeBitCastExpr(Opc, Op, CurTy); |
| 2318 | if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy); |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 2319 | } |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2320 | break; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2321 | } |
Dan Gohman | 1639c39 | 2009-07-27 21:53:46 +0000 | [diff] [blame] | 2322 | case bitc::CST_CODE_CE_INBOUNDS_GEP: |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2323 | case bitc::CST_CODE_CE_GEP: { // CE_GEP: [n x operands] |
David Blaikie | b926357 | 2015-03-13 21:03:36 +0000 | [diff] [blame] | 2324 | unsigned OpNum = 0; |
| 2325 | Type *PointeeType = nullptr; |
| 2326 | if (Record.size() % 2) |
| 2327 | PointeeType = getTypeByID(Record[OpNum++]); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2328 | SmallVector<Constant*, 16> Elts; |
David Blaikie | b926357 | 2015-03-13 21:03:36 +0000 | [diff] [blame] | 2329 | while (OpNum != Record.size()) { |
| 2330 | Type *ElTy = getTypeByID(Record[OpNum++]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2331 | if (!ElTy) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2332 | return Error("Invalid record"); |
David Blaikie | b926357 | 2015-03-13 21:03:36 +0000 | [diff] [blame] | 2333 | Elts.push_back(ValueList.getConstantFwdRef(Record[OpNum++], ElTy)); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2334 | } |
David Blaikie | b926357 | 2015-03-13 21:03:36 +0000 | [diff] [blame] | 2335 | |
David Blaikie | b926357 | 2015-03-13 21:03:36 +0000 | [diff] [blame] | 2336 | if (PointeeType && |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 2337 | PointeeType != |
| 2338 | cast<SequentialType>(Elts[0]->getType()->getScalarType()) |
| 2339 | ->getElementType()) |
David Blaikie | 12cf5d70 | 2015-03-16 22:03:50 +0000 | [diff] [blame] | 2340 | return Error("Explicit gep operator type does not match pointee type " |
| 2341 | "of pointer operand"); |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 2342 | |
| 2343 | ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end()); |
| 2344 | V = ConstantExpr::getGetElementPtr(PointeeType, Elts[0], Indices, |
| 2345 | BitCode == |
| 2346 | bitc::CST_CODE_CE_INBOUNDS_GEP); |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 2347 | break; |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2348 | } |
Joe Abbey | 1a6e770 | 2013-09-12 22:02:31 +0000 | [diff] [blame] | 2349 | case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#] |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2350 | if (Record.size() < 3) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2351 | return Error("Invalid record"); |
Joe Abbey | 1a6e770 | 2013-09-12 22:02:31 +0000 | [diff] [blame] | 2352 | |
| 2353 | Type *SelectorTy = Type::getInt1Ty(Context); |
| 2354 | |
| 2355 | // If CurTy is a vector of length n, then Record[0] must be a <n x i1> |
| 2356 | // vector. Otherwise, it must be a single bit. |
| 2357 | if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) |
| 2358 | SelectorTy = VectorType::get(Type::getInt1Ty(Context), |
| 2359 | VTy->getNumElements()); |
| 2360 | |
| 2361 | V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0], |
| 2362 | SelectorTy), |
| 2363 | ValueList.getConstantFwdRef(Record[1],CurTy), |
| 2364 | ValueList.getConstantFwdRef(Record[2],CurTy)); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2365 | break; |
Joe Abbey | 1a6e770 | 2013-09-12 22:02:31 +0000 | [diff] [blame] | 2366 | } |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 2367 | case bitc::CST_CODE_CE_EXTRACTELT |
| 2368 | : { // CE_EXTRACTELT: [opty, opval, opty, opval] |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2369 | if (Record.size() < 3) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2370 | return Error("Invalid record"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2371 | VectorType *OpTy = |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2372 | dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2373 | if (!OpTy) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2374 | return Error("Invalid record"); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2375 | Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 2376 | Constant *Op1 = nullptr; |
| 2377 | if (Record.size() == 4) { |
| 2378 | Type *IdxTy = getTypeByID(Record[2]); |
| 2379 | if (!IdxTy) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2380 | return Error("Invalid record"); |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 2381 | Op1 = ValueList.getConstantFwdRef(Record[3], IdxTy); |
| 2382 | } else // TODO: Remove with llvm 4.0 |
| 2383 | Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context)); |
| 2384 | if (!Op1) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2385 | return Error("Invalid record"); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2386 | V = ConstantExpr::getExtractElement(Op0, Op1); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2387 | break; |
| 2388 | } |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 2389 | case bitc::CST_CODE_CE_INSERTELT |
| 2390 | : { // CE_INSERTELT: [opval, opval, opty, opval] |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2391 | VectorType *OpTy = dyn_cast<VectorType>(CurTy); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2392 | if (Record.size() < 3 || !OpTy) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2393 | return Error("Invalid record"); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2394 | Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); |
| 2395 | Constant *Op1 = ValueList.getConstantFwdRef(Record[1], |
| 2396 | OpTy->getElementType()); |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 2397 | Constant *Op2 = nullptr; |
| 2398 | if (Record.size() == 4) { |
| 2399 | Type *IdxTy = getTypeByID(Record[2]); |
| 2400 | if (!IdxTy) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2401 | return Error("Invalid record"); |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 2402 | Op2 = ValueList.getConstantFwdRef(Record[3], IdxTy); |
| 2403 | } else // TODO: Remove with llvm 4.0 |
| 2404 | Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context)); |
| 2405 | if (!Op2) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2406 | return Error("Invalid record"); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2407 | V = ConstantExpr::getInsertElement(Op0, Op1, Op2); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2408 | break; |
| 2409 | } |
| 2410 | case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval] |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2411 | VectorType *OpTy = dyn_cast<VectorType>(CurTy); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2412 | if (Record.size() < 3 || !OpTy) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2413 | return Error("Invalid record"); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2414 | Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); |
| 2415 | Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2416 | Type *ShufTy = VectorType::get(Type::getInt32Ty(Context), |
Owen Anderson | e9f9804 | 2009-07-07 20:18:58 +0000 | [diff] [blame] | 2417 | OpTy->getNumElements()); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2418 | Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2419 | V = ConstantExpr::getShuffleVector(Op0, Op1, Op2); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2420 | break; |
| 2421 | } |
Nate Begeman | 94aa38d | 2009-02-12 21:28:33 +0000 | [diff] [blame] | 2422 | case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval] |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2423 | VectorType *RTy = dyn_cast<VectorType>(CurTy); |
| 2424 | VectorType *OpTy = |
Duncan Sands | 89d412a | 2010-10-28 15:47:26 +0000 | [diff] [blame] | 2425 | dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2426 | if (Record.size() < 4 || !RTy || !OpTy) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2427 | return Error("Invalid record"); |
Nate Begeman | 94aa38d | 2009-02-12 21:28:33 +0000 | [diff] [blame] | 2428 | Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); |
| 2429 | Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2430 | Type *ShufTy = VectorType::get(Type::getInt32Ty(Context), |
Owen Anderson | e9f9804 | 2009-07-07 20:18:58 +0000 | [diff] [blame] | 2431 | RTy->getNumElements()); |
Nate Begeman | 94aa38d | 2009-02-12 21:28:33 +0000 | [diff] [blame] | 2432 | Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2433 | V = ConstantExpr::getShuffleVector(Op0, Op1, Op2); |
Nate Begeman | 94aa38d | 2009-02-12 21:28:33 +0000 | [diff] [blame] | 2434 | break; |
| 2435 | } |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2436 | case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred] |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2437 | if (Record.size() < 4) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2438 | return Error("Invalid record"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2439 | Type *OpTy = getTypeByID(Record[0]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2440 | if (!OpTy) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2441 | return Error("Invalid record"); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2442 | Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); |
| 2443 | Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); |
| 2444 | |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2445 | if (OpTy->isFPOrFPVectorTy()) |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2446 | V = ConstantExpr::getFCmp(Record[3], Op0, Op1); |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 2447 | else |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2448 | V = ConstantExpr::getICmp(Record[3], Op0, Op1); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2449 | break; |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 2450 | } |
Chad Rosier | d8c7610 | 2012-09-05 19:00:49 +0000 | [diff] [blame] | 2451 | // This maintains backward compatibility, pre-asm dialect keywords. |
Chad Rosier | 5895eda | 2012-09-05 06:28:52 +0000 | [diff] [blame] | 2452 | // FIXME: Remove with the 4.0 release. |
Chad Rosier | 18fcdcf | 2012-09-05 00:56:20 +0000 | [diff] [blame] | 2453 | case bitc::CST_CODE_INLINEASM_OLD: { |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2454 | if (Record.size() < 2) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2455 | return Error("Invalid record"); |
Chris Lattner | af8fffc | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 2456 | std::string AsmStr, ConstrStr; |
Dale Johannesen | fd04c74 | 2009-10-13 20:46:56 +0000 | [diff] [blame] | 2457 | bool HasSideEffects = Record[0] & 1; |
Dale Johannesen | 1cfb958 | 2009-10-21 23:28:00 +0000 | [diff] [blame] | 2458 | bool IsAlignStack = Record[0] >> 1; |
Chris Lattner | af8fffc | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 2459 | unsigned AsmStrSize = Record[1]; |
| 2460 | if (2+AsmStrSize >= Record.size()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2461 | return Error("Invalid record"); |
Chris Lattner | af8fffc | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 2462 | unsigned ConstStrSize = Record[2+AsmStrSize]; |
| 2463 | if (3+AsmStrSize+ConstStrSize > Record.size()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2464 | return Error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2465 | |
Chris Lattner | af8fffc | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 2466 | for (unsigned i = 0; i != AsmStrSize; ++i) |
| 2467 | AsmStr += (char)Record[2+i]; |
| 2468 | for (unsigned i = 0; i != ConstStrSize; ++i) |
| 2469 | ConstrStr += (char)Record[3+AsmStrSize+i]; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2470 | PointerType *PTy = cast<PointerType>(CurTy); |
Chris Lattner | af8fffc | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 2471 | V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()), |
Dale Johannesen | 1cfb958 | 2009-10-21 23:28:00 +0000 | [diff] [blame] | 2472 | AsmStr, ConstrStr, HasSideEffects, IsAlignStack); |
Chris Lattner | af8fffc | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 2473 | break; |
| 2474 | } |
Chad Rosier | d8c7610 | 2012-09-05 19:00:49 +0000 | [diff] [blame] | 2475 | // This version adds support for the asm dialect keywords (e.g., |
| 2476 | // inteldialect). |
Chad Rosier | 18fcdcf | 2012-09-05 00:56:20 +0000 | [diff] [blame] | 2477 | case bitc::CST_CODE_INLINEASM: { |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2478 | if (Record.size() < 2) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2479 | return Error("Invalid record"); |
Chad Rosier | 18fcdcf | 2012-09-05 00:56:20 +0000 | [diff] [blame] | 2480 | std::string AsmStr, ConstrStr; |
| 2481 | bool HasSideEffects = Record[0] & 1; |
| 2482 | bool IsAlignStack = (Record[0] >> 1) & 1; |
| 2483 | unsigned AsmDialect = Record[0] >> 2; |
| 2484 | unsigned AsmStrSize = Record[1]; |
| 2485 | if (2+AsmStrSize >= Record.size()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2486 | return Error("Invalid record"); |
Chad Rosier | 18fcdcf | 2012-09-05 00:56:20 +0000 | [diff] [blame] | 2487 | unsigned ConstStrSize = Record[2+AsmStrSize]; |
| 2488 | if (3+AsmStrSize+ConstStrSize > Record.size()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2489 | return Error("Invalid record"); |
Chad Rosier | 18fcdcf | 2012-09-05 00:56:20 +0000 | [diff] [blame] | 2490 | |
| 2491 | for (unsigned i = 0; i != AsmStrSize; ++i) |
| 2492 | AsmStr += (char)Record[2+i]; |
| 2493 | for (unsigned i = 0; i != ConstStrSize; ++i) |
| 2494 | ConstrStr += (char)Record[3+AsmStrSize+i]; |
| 2495 | PointerType *PTy = cast<PointerType>(CurTy); |
| 2496 | V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()), |
| 2497 | AsmStr, ConstrStr, HasSideEffects, IsAlignStack, |
Chad Rosier | d8c7610 | 2012-09-05 19:00:49 +0000 | [diff] [blame] | 2498 | InlineAsm::AsmDialect(AsmDialect)); |
Chad Rosier | 18fcdcf | 2012-09-05 00:56:20 +0000 | [diff] [blame] | 2499 | break; |
| 2500 | } |
Chris Lattner | 5956dc8 | 2009-10-28 05:53:48 +0000 | [diff] [blame] | 2501 | case bitc::CST_CODE_BLOCKADDRESS:{ |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2502 | if (Record.size() < 3) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2503 | return Error("Invalid record"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2504 | Type *FnTy = getTypeByID(Record[0]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2505 | if (!FnTy) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2506 | return Error("Invalid record"); |
Chris Lattner | 5956dc8 | 2009-10-28 05:53:48 +0000 | [diff] [blame] | 2507 | Function *Fn = |
| 2508 | dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy)); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2509 | if (!Fn) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2510 | return Error("Invalid record"); |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 2511 | |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 2512 | // Don't let Fn get dematerialized. |
| 2513 | BlockAddressesTaken.insert(Fn); |
| 2514 | |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 2515 | // If the function is already parsed we can insert the block address right |
| 2516 | // away. |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 2517 | BasicBlock *BB; |
| 2518 | unsigned BBID = Record[2]; |
| 2519 | if (!BBID) |
| 2520 | // Invalid reference to entry block. |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2521 | return Error("Invalid ID"); |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 2522 | if (!Fn->empty()) { |
| 2523 | Function::iterator BBI = Fn->begin(), BBE = Fn->end(); |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 2524 | for (size_t I = 0, E = BBID; I != E; ++I) { |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 2525 | if (BBI == BBE) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2526 | return Error("Invalid ID"); |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 2527 | ++BBI; |
| 2528 | } |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 2529 | BB = BBI; |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 2530 | } else { |
| 2531 | // Otherwise insert a placeholder and remember it so it can be inserted |
| 2532 | // when the function is parsed. |
Duncan P. N. Exon Smith | 5a511b5 | 2014-08-05 17:49:48 +0000 | [diff] [blame] | 2533 | auto &FwdBBs = BasicBlockFwdRefs[Fn]; |
| 2534 | if (FwdBBs.empty()) |
| 2535 | BasicBlockFwdRefQueue.push_back(Fn); |
Duncan P. N. Exon Smith | 5a5fd7b | 2014-08-16 01:54:37 +0000 | [diff] [blame] | 2536 | if (FwdBBs.size() < BBID + 1) |
| 2537 | FwdBBs.resize(BBID + 1); |
| 2538 | if (!FwdBBs[BBID]) |
| 2539 | FwdBBs[BBID] = BasicBlock::Create(Context); |
| 2540 | BB = FwdBBs[BBID]; |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 2541 | } |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 2542 | V = BlockAddress::get(Fn, BB); |
Chris Lattner | 5956dc8 | 2009-10-28 05:53:48 +0000 | [diff] [blame] | 2543 | break; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2544 | } |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2545 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2546 | |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 2547 | ValueList.AssignValue(V, NextCstNo); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 2548 | ++NextCstNo; |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2549 | } |
| 2550 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2551 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2552 | std::error_code BitcodeReader::ParseUseLists() { |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 2553 | if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2554 | return Error("Invalid record"); |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 2555 | |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 2556 | // Read all the records. |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 2557 | SmallVector<uint64_t, 64> Record; |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 2558 | while (1) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2559 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2560 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2561 | switch (Entry.Kind) { |
| 2562 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 2563 | case BitstreamEntry::Error: |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2564 | return Error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2565 | case BitstreamEntry::EndBlock: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2566 | return std::error_code(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2567 | case BitstreamEntry::Record: |
| 2568 | // The interesting case. |
| 2569 | break; |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 2570 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2571 | |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 2572 | // Read a use list record. |
| 2573 | Record.clear(); |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 2574 | bool IsBB = false; |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2575 | switch (Stream.readRecord(Entry.ID, Record)) { |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 2576 | default: // Default behavior: unknown type. |
| 2577 | break; |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 2578 | case bitc::USELIST_CODE_BB: |
| 2579 | IsBB = true; |
| 2580 | // fallthrough |
| 2581 | case bitc::USELIST_CODE_DEFAULT: { |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 2582 | unsigned RecordLength = Record.size(); |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 2583 | if (RecordLength < 3) |
| 2584 | // Records should have at least an ID and two indexes. |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2585 | return Error("Invalid record"); |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 2586 | unsigned ID = Record.back(); |
| 2587 | Record.pop_back(); |
| 2588 | |
| 2589 | Value *V; |
| 2590 | if (IsBB) { |
| 2591 | assert(ID < FunctionBBs.size() && "Basic block not found"); |
| 2592 | V = FunctionBBs[ID]; |
| 2593 | } else |
| 2594 | V = ValueList[ID]; |
| 2595 | unsigned NumUses = 0; |
| 2596 | SmallDenseMap<const Use *, unsigned, 16> Order; |
| 2597 | for (const Use &U : V->uses()) { |
Duncan P. N. Exon Smith | 1318364 | 2014-08-16 01:54:34 +0000 | [diff] [blame] | 2598 | if (++NumUses > Record.size()) |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 2599 | break; |
Duncan P. N. Exon Smith | 1318364 | 2014-08-16 01:54:34 +0000 | [diff] [blame] | 2600 | Order[&U] = Record[NumUses - 1]; |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 2601 | } |
| 2602 | if (Order.size() != Record.size() || NumUses > Record.size()) |
| 2603 | // Mismatches can happen if the functions are being materialized lazily |
| 2604 | // (out-of-order), or a value has been upgraded. |
| 2605 | break; |
| 2606 | |
| 2607 | V->sortUseList([&](const Use &L, const Use &R) { |
| 2608 | return Order.lookup(&L) < Order.lookup(&R); |
| 2609 | }); |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 2610 | break; |
| 2611 | } |
| 2612 | } |
| 2613 | } |
| 2614 | } |
| 2615 | |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 2616 | /// When we see the block for metadata, remember where it is and then skip it. |
| 2617 | /// This lets us lazily deserialize the metadata. |
| 2618 | std::error_code BitcodeReader::rememberAndSkipMetadata() { |
| 2619 | // Save the current stream state. |
| 2620 | uint64_t CurBit = Stream.GetCurrentBitNo(); |
| 2621 | DeferredMetadataInfo.push_back(CurBit); |
| 2622 | |
| 2623 | // Skip over the block for now. |
| 2624 | if (Stream.SkipBlock()) |
| 2625 | return Error("Invalid record"); |
| 2626 | return std::error_code(); |
| 2627 | } |
| 2628 | |
| 2629 | std::error_code BitcodeReader::materializeMetadata() { |
| 2630 | for (uint64_t BitPos : DeferredMetadataInfo) { |
| 2631 | // Move the bit stream to the saved position. |
| 2632 | Stream.JumpToBit(BitPos); |
| 2633 | if (std::error_code EC = ParseMetadata()) |
| 2634 | return EC; |
| 2635 | } |
| 2636 | DeferredMetadataInfo.clear(); |
| 2637 | return std::error_code(); |
| 2638 | } |
| 2639 | |
Rafael Espindola | 468b868 | 2015-04-01 14:44:59 +0000 | [diff] [blame] | 2640 | void BitcodeReader::setStripDebugInfo() { StripDebugInfo = true; } |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 2641 | |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 2642 | /// RememberAndSkipFunctionBody - When we see the block for a function body, |
| 2643 | /// remember where it is and then skip it. This lets us lazily deserialize the |
| 2644 | /// functions. |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2645 | std::error_code BitcodeReader::RememberAndSkipFunctionBody() { |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 2646 | // Get the function we are talking about. |
| 2647 | if (FunctionsWithBodies.empty()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2648 | return Error("Insufficient function protos"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2649 | |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 2650 | Function *Fn = FunctionsWithBodies.back(); |
| 2651 | FunctionsWithBodies.pop_back(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2652 | |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 2653 | // Save the current stream state. |
| 2654 | uint64_t CurBit = Stream.GetCurrentBitNo(); |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 2655 | DeferredFunctionInfo[Fn] = CurBit; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2656 | |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 2657 | // Skip over the function block for now. |
| 2658 | if (Stream.SkipBlock()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2659 | return Error("Invalid record"); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2660 | return std::error_code(); |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 2661 | } |
| 2662 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2663 | std::error_code BitcodeReader::GlobalCleanup() { |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 2664 | // Patch the initializers for globals and aliases up. |
| 2665 | ResolveGlobalAndAliasInits(); |
| 2666 | if (!GlobalInits.empty() || !AliasInits.empty()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2667 | return Error("Malformed global initializer set"); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 2668 | |
| 2669 | // Look for intrinsic functions which need to be upgraded at some point |
| 2670 | for (Module::iterator FI = TheModule->begin(), FE = TheModule->end(); |
| 2671 | FI != FE; ++FI) { |
| 2672 | Function *NewFn; |
| 2673 | if (UpgradeIntrinsicFunction(FI, NewFn)) |
| 2674 | UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn)); |
| 2675 | } |
| 2676 | |
| 2677 | // Look for global variables which need to be renamed. |
| 2678 | for (Module::global_iterator |
| 2679 | GI = TheModule->global_begin(), GE = TheModule->global_end(); |
Reid Kleckner | fceb76f | 2014-05-16 20:39:27 +0000 | [diff] [blame] | 2680 | GI != GE;) { |
| 2681 | GlobalVariable *GV = GI++; |
| 2682 | UpgradeGlobalVariable(GV); |
| 2683 | } |
| 2684 | |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 2685 | // Force deallocation of memory for these vectors to favor the client that |
| 2686 | // want lazy deserialization. |
| 2687 | std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits); |
| 2688 | std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2689 | return std::error_code(); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 2692 | std::error_code BitcodeReader::ParseModule(bool Resume, |
| 2693 | bool ShouldLazyLoadMetadata) { |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 2694 | if (Resume) |
| 2695 | Stream.JumpToBit(NextUnreadBit); |
| 2696 | else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2697 | return Error("Invalid record"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2698 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2699 | SmallVector<uint64_t, 64> Record; |
| 2700 | std::vector<std::string> SectionTable; |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 2701 | std::vector<std::string> GCTable; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2702 | |
| 2703 | // Read all the records for this module. |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2704 | while (1) { |
| 2705 | BitstreamEntry Entry = Stream.advance(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2706 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2707 | switch (Entry.Kind) { |
| 2708 | case BitstreamEntry::Error: |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2709 | return Error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2710 | case BitstreamEntry::EndBlock: |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 2711 | return GlobalCleanup(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2712 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2713 | case BitstreamEntry::SubBlock: |
| 2714 | switch (Entry.ID) { |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2715 | default: // Skip unknown content. |
| 2716 | if (Stream.SkipBlock()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2717 | return Error("Invalid record"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2718 | break; |
Chris Lattner | 6eeea5d | 2007-05-05 18:57:30 +0000 | [diff] [blame] | 2719 | case bitc::BLOCKINFO_BLOCK_ID: |
| 2720 | if (Stream.ReadBlockInfoBlock()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2721 | return Error("Malformed block"); |
Chris Lattner | 6eeea5d | 2007-05-05 18:57:30 +0000 | [diff] [blame] | 2722 | break; |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 2723 | case bitc::PARAMATTR_BLOCK_ID: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2724 | if (std::error_code EC = ParseAttributeBlock()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2725 | return EC; |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 2726 | break; |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 2727 | case bitc::PARAMATTR_GROUP_BLOCK_ID: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2728 | if (std::error_code EC = ParseAttributeGroupBlock()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2729 | return EC; |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 2730 | break; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2731 | case bitc::TYPE_BLOCK_ID_NEW: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2732 | if (std::error_code EC = ParseTypeTable()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2733 | return EC; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2734 | break; |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 2735 | case bitc::VALUE_SYMTAB_BLOCK_ID: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2736 | if (std::error_code EC = ParseValueSymbolTable()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2737 | return EC; |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 2738 | SeenValueSymbolTable = true; |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 2739 | break; |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2740 | case bitc::CONSTANTS_BLOCK_ID: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2741 | if (std::error_code EC = ParseConstants()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2742 | return EC; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2743 | if (std::error_code EC = ResolveGlobalAndAliasInits()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2744 | return EC; |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2745 | break; |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 2746 | case bitc::METADATA_BLOCK_ID: |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 2747 | if (ShouldLazyLoadMetadata && !IsMetadataMaterialized) { |
| 2748 | if (std::error_code EC = rememberAndSkipMetadata()) |
| 2749 | return EC; |
| 2750 | break; |
| 2751 | } |
| 2752 | assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata"); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2753 | if (std::error_code EC = ParseMetadata()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2754 | return EC; |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 2755 | break; |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 2756 | case bitc::FUNCTION_BLOCK_ID: |
| 2757 | // If this is the first function body we've seen, reverse the |
| 2758 | // FunctionsWithBodies list. |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 2759 | if (!SeenFirstFunctionBody) { |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 2760 | std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end()); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2761 | if (std::error_code EC = GlobalCleanup()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2762 | return EC; |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 2763 | SeenFirstFunctionBody = true; |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 2764 | } |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2765 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2766 | if (std::error_code EC = RememberAndSkipFunctionBody()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2767 | return EC; |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 2768 | // For streaming bitcode, suspend parsing when we reach the function |
| 2769 | // bodies. Subsequent materialization calls will resume it when |
| 2770 | // necessary. For streaming, the function bodies must be at the end of |
| 2771 | // the bitcode. If the bitcode file is old, the symbol table will be |
| 2772 | // at the end instead and will not have been seen yet. In this case, |
| 2773 | // just finish the parse now. |
| 2774 | if (LazyStreamer && SeenValueSymbolTable) { |
| 2775 | NextUnreadBit = Stream.GetCurrentBitNo(); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2776 | return std::error_code(); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 2777 | } |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 2778 | break; |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 2779 | case bitc::USELIST_BLOCK_ID: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 2780 | if (std::error_code EC = ParseUseLists()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2781 | return EC; |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 2782 | break; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2783 | } |
| 2784 | continue; |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2785 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2786 | case BitstreamEntry::Record: |
| 2787 | // The interesting case. |
| 2788 | break; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2789 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2790 | |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2791 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2792 | // Read a record. |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2793 | switch (Stream.readRecord(Entry.ID, Record)) { |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2794 | default: break; // Default behavior, ignore unknown content. |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 2795 | case bitc::MODULE_CODE_VERSION: { // VERSION: [version#] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2796 | if (Record.size() < 1) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2797 | return Error("Invalid record"); |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 2798 | // Only version #0 and #1 are supported so far. |
| 2799 | unsigned module_version = Record[0]; |
| 2800 | switch (module_version) { |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2801 | default: |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2802 | return Error("Invalid value"); |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 2803 | case 0: |
| 2804 | UseRelativeIDs = false; |
| 2805 | break; |
| 2806 | case 1: |
| 2807 | UseRelativeIDs = true; |
| 2808 | break; |
| 2809 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2810 | break; |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 2811 | } |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 2812 | case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2813 | std::string S; |
| 2814 | if (ConvertToString(Record, 0, S)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2815 | return Error("Invalid record"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2816 | TheModule->setTargetTriple(S); |
| 2817 | break; |
| 2818 | } |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 2819 | case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2820 | std::string S; |
| 2821 | if (ConvertToString(Record, 0, S)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2822 | return Error("Invalid record"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2823 | TheModule->setDataLayout(S); |
| 2824 | break; |
| 2825 | } |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 2826 | case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2827 | std::string S; |
| 2828 | if (ConvertToString(Record, 0, S)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2829 | return Error("Invalid record"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2830 | TheModule->setModuleInlineAsm(S); |
| 2831 | break; |
| 2832 | } |
Bill Wendling | 706d3d6 | 2012-11-28 08:41:48 +0000 | [diff] [blame] | 2833 | case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N] |
| 2834 | // FIXME: Remove in 4.0. |
| 2835 | std::string S; |
| 2836 | if (ConvertToString(Record, 0, S)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2837 | return Error("Invalid record"); |
Bill Wendling | 706d3d6 | 2012-11-28 08:41:48 +0000 | [diff] [blame] | 2838 | // Ignore value. |
| 2839 | break; |
| 2840 | } |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 2841 | case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2842 | std::string S; |
| 2843 | if (ConvertToString(Record, 0, S)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2844 | return Error("Invalid record"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2845 | SectionTable.push_back(S); |
| 2846 | break; |
| 2847 | } |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 2848 | case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N] |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 2849 | std::string S; |
| 2850 | if (ConvertToString(Record, 0, S)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2851 | return Error("Invalid record"); |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 2852 | GCTable.push_back(S); |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 2853 | break; |
| 2854 | } |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 2855 | case bitc::MODULE_CODE_COMDAT: { // COMDAT: [selection_kind, name] |
| 2856 | if (Record.size() < 2) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2857 | return Error("Invalid record"); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 2858 | Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]); |
| 2859 | unsigned ComdatNameSize = Record[1]; |
| 2860 | std::string ComdatName; |
| 2861 | ComdatName.reserve(ComdatNameSize); |
| 2862 | for (unsigned i = 0; i != ComdatNameSize; ++i) |
| 2863 | ComdatName += (char)Record[2 + i]; |
| 2864 | Comdat *C = TheModule->getOrInsertComdat(ComdatName); |
| 2865 | C->setSelectionKind(SK); |
| 2866 | ComdatList.push_back(C); |
| 2867 | break; |
| 2868 | } |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 2869 | // GLOBALVAR: [pointer type, isconst, initid, |
Rafael Espindola | 45e6c19 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 2870 | // linkage, alignment, section, visibility, threadlocal, |
Peter Collingbourne | 69ba016 | 2015-02-04 00:42:45 +0000 | [diff] [blame] | 2871 | // unnamed_addr, externally_initialized, dllstorageclass, |
| 2872 | // comdat] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2873 | case bitc::MODULE_CODE_GLOBALVAR: { |
Chris Lattner | 4b00d92 | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 2874 | if (Record.size() < 6) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2875 | return Error("Invalid record"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2876 | Type *Ty = getTypeByID(Record[0]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2877 | if (!Ty) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2878 | return Error("Invalid record"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2879 | if (!Ty->isPointerTy()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2880 | return Error("Invalid type for value"); |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 2881 | unsigned AddressSpace = cast<PointerType>(Ty)->getAddressSpace(); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2882 | Ty = cast<PointerType>(Ty)->getElementType(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2883 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2884 | bool isConstant = Record[1]; |
Rafael Espindola | 12ca34f | 2015-01-19 15:16:06 +0000 | [diff] [blame] | 2885 | uint64_t RawLinkage = Record[3]; |
| 2886 | GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage); |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 2887 | unsigned Alignment; |
| 2888 | if (std::error_code EC = parseAlignmentValue(Record[4], Alignment)) |
| 2889 | return EC; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2890 | std::string Section; |
| 2891 | if (Record[5]) { |
| 2892 | if (Record[5]-1 >= SectionTable.size()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2893 | return Error("Invalid ID"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2894 | Section = SectionTable[Record[5]-1]; |
| 2895 | } |
Chris Lattner | 4b00d92 | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 2896 | GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility; |
Duncan P. N. Exon Smith | b80de10 | 2014-05-07 22:57:20 +0000 | [diff] [blame] | 2897 | // Local linkage must have default visibility. |
| 2898 | if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage)) |
| 2899 | // FIXME: Change to an error if non-default in 4.0. |
Chris Lattner | 53862f7 | 2007-05-06 19:27:46 +0000 | [diff] [blame] | 2900 | Visibility = GetDecodedVisibility(Record[6]); |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 2901 | |
| 2902 | GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal; |
Chris Lattner | 53862f7 | 2007-05-06 19:27:46 +0000 | [diff] [blame] | 2903 | if (Record.size() > 7) |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 2904 | TLM = GetDecodedThreadLocalMode(Record[7]); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2905 | |
Rafael Espindola | 45e6c19 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 2906 | bool UnnamedAddr = false; |
| 2907 | if (Record.size() > 8) |
| 2908 | UnnamedAddr = Record[8]; |
| 2909 | |
Michael Gottesman | 27e7ef3 | 2013-02-05 05:57:38 +0000 | [diff] [blame] | 2910 | bool ExternallyInitialized = false; |
| 2911 | if (Record.size() > 9) |
| 2912 | ExternallyInitialized = Record[9]; |
| 2913 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2914 | GlobalVariable *NewGV = |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2915 | new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, "", nullptr, |
Michael Gottesman | 27e7ef3 | 2013-02-05 05:57:38 +0000 | [diff] [blame] | 2916 | TLM, AddressSpace, ExternallyInitialized); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2917 | NewGV->setAlignment(Alignment); |
| 2918 | if (!Section.empty()) |
| 2919 | NewGV->setSection(Section); |
| 2920 | NewGV->setVisibility(Visibility); |
Rafael Espindola | 45e6c19 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 2921 | NewGV->setUnnamedAddr(UnnamedAddr); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2922 | |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 2923 | if (Record.size() > 10) |
| 2924 | NewGV->setDLLStorageClass(GetDecodedDLLStorageClass(Record[10])); |
| 2925 | else |
Rafael Espindola | 12ca34f | 2015-01-19 15:16:06 +0000 | [diff] [blame] | 2926 | UpgradeDLLImportExportLinkage(NewGV, RawLinkage); |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 2927 | |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 2928 | ValueList.push_back(NewGV); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2929 | |
Chris Lattner | 47d131b | 2007-04-24 00:18:21 +0000 | [diff] [blame] | 2930 | // Remember which value to use for the global initializer. |
| 2931 | if (unsigned InitID = Record[2]) |
| 2932 | GlobalInits.push_back(std::make_pair(NewGV, InitID-1)); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 2933 | |
Rafael Espindola | 12ca34f | 2015-01-19 15:16:06 +0000 | [diff] [blame] | 2934 | if (Record.size() > 11) { |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 2935 | if (unsigned ComdatID = Record[11]) { |
| 2936 | assert(ComdatID <= ComdatList.size()); |
| 2937 | NewGV->setComdat(ComdatList[ComdatID - 1]); |
| 2938 | } |
Rafael Espindola | 12ca34f | 2015-01-19 15:16:06 +0000 | [diff] [blame] | 2939 | } else if (hasImplicitComdat(RawLinkage)) { |
| 2940 | NewGV->setComdat(reinterpret_cast<Comdat *>(1)); |
| 2941 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2942 | break; |
| 2943 | } |
Chris Lattner | 4c0a6d6 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 2944 | // FUNCTION: [type, callingconv, isproto, linkage, paramattr, |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 2945 | // alignment, section, visibility, gc, unnamed_addr, |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 2946 | // prologuedata, dllstorageclass, comdat, prefixdata] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2947 | case bitc::MODULE_CODE_FUNCTION: { |
Chris Lattner | 4c0a6d6 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 2948 | if (Record.size() < 8) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2949 | return Error("Invalid record"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2950 | Type *Ty = getTypeByID(Record[0]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2951 | if (!Ty) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2952 | return Error("Invalid record"); |
David Blaikie | 561a157 | 2015-04-17 16:28:26 +0000 | [diff] [blame] | 2953 | if (auto *PTy = dyn_cast<PointerType>(Ty)) |
| 2954 | Ty = PTy->getElementType(); |
| 2955 | auto *FTy = dyn_cast<FunctionType>(Ty); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2956 | if (!FTy) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2957 | return Error("Invalid type for value"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2958 | |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2959 | Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage, |
| 2960 | "", TheModule); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2961 | |
Sandeep Patel | 68c5f47 | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 2962 | Func->setCallingConv(static_cast<CallingConv::ID>(Record[1])); |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 2963 | bool isProto = Record[2]; |
Rafael Espindola | 12ca34f | 2015-01-19 15:16:06 +0000 | [diff] [blame] | 2964 | uint64_t RawLinkage = Record[3]; |
| 2965 | Func->setLinkage(getDecodedLinkage(RawLinkage)); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 2966 | Func->setAttributes(getAttributes(Record[4])); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2967 | |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 2968 | unsigned Alignment; |
| 2969 | if (std::error_code EC = parseAlignmentValue(Record[5], Alignment)) |
| 2970 | return EC; |
| 2971 | Func->setAlignment(Alignment); |
Chris Lattner | 4c0a6d6 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 2972 | if (Record[6]) { |
| 2973 | if (Record[6]-1 >= SectionTable.size()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2974 | return Error("Invalid ID"); |
Chris Lattner | 4c0a6d6 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 2975 | Func->setSection(SectionTable[Record[6]-1]); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2976 | } |
Duncan P. N. Exon Smith | b80de10 | 2014-05-07 22:57:20 +0000 | [diff] [blame] | 2977 | // Local linkage must have default visibility. |
| 2978 | if (!Func->hasLocalLinkage()) |
| 2979 | // FIXME: Change to an error if non-default in 4.0. |
| 2980 | Func->setVisibility(GetDecodedVisibility(Record[7])); |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 2981 | if (Record.size() > 8 && Record[8]) { |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 2982 | if (Record[8]-1 > GCTable.size()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 2983 | return Error("Invalid ID"); |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 2984 | Func->setGC(GCTable[Record[8]-1].c_str()); |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 2985 | } |
Rafael Espindola | 45e6c19 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 2986 | bool UnnamedAddr = false; |
| 2987 | if (Record.size() > 9) |
| 2988 | UnnamedAddr = Record[9]; |
| 2989 | Func->setUnnamedAddr(UnnamedAddr); |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 2990 | if (Record.size() > 10 && Record[10] != 0) |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 2991 | FunctionPrologues.push_back(std::make_pair(Func, Record[10]-1)); |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 2992 | |
| 2993 | if (Record.size() > 11) |
| 2994 | Func->setDLLStorageClass(GetDecodedDLLStorageClass(Record[11])); |
| 2995 | else |
Rafael Espindola | 12ca34f | 2015-01-19 15:16:06 +0000 | [diff] [blame] | 2996 | UpgradeDLLImportExportLinkage(Func, RawLinkage); |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 2997 | |
Rafael Espindola | 12ca34f | 2015-01-19 15:16:06 +0000 | [diff] [blame] | 2998 | if (Record.size() > 12) { |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 2999 | if (unsigned ComdatID = Record[12]) { |
| 3000 | assert(ComdatID <= ComdatList.size()); |
| 3001 | Func->setComdat(ComdatList[ComdatID - 1]); |
| 3002 | } |
Rafael Espindola | 12ca34f | 2015-01-19 15:16:06 +0000 | [diff] [blame] | 3003 | } else if (hasImplicitComdat(RawLinkage)) { |
| 3004 | Func->setComdat(reinterpret_cast<Comdat *>(1)); |
| 3005 | } |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 3006 | |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 3007 | if (Record.size() > 13 && Record[13] != 0) |
| 3008 | FunctionPrefixes.push_back(std::make_pair(Func, Record[13]-1)); |
| 3009 | |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 3010 | ValueList.push_back(Func); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3011 | |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 3012 | // If this is a function with a body, remember the prototype we are |
| 3013 | // 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] | 3014 | if (!isProto) { |
Rafael Espindola | d4bcefc | 2014-10-24 18:13:04 +0000 | [diff] [blame] | 3015 | Func->setIsMaterializable(true); |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 3016 | FunctionsWithBodies.push_back(Func); |
Rafael Espindola | 1b47a28 | 2014-10-23 15:20:05 +0000 | [diff] [blame] | 3017 | if (LazyStreamer) |
| 3018 | DeferredFunctionInfo[Func] = 0; |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3019 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3020 | break; |
| 3021 | } |
Anton Korobeynikov | 2f22e3f | 2008-03-12 00:49:19 +0000 | [diff] [blame] | 3022 | // ALIAS: [alias type, aliasee val#, linkage] |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 3023 | // ALIAS: [alias type, aliasee val#, linkage, visibility, dllstorageclass] |
Chris Lattner | 831d420 | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 3024 | case bitc::MODULE_CODE_ALIAS: { |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 3025 | if (Record.size() < 3) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3026 | return Error("Invalid record"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3027 | Type *Ty = getTypeByID(Record[0]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3028 | if (!Ty) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3029 | return Error("Invalid record"); |
Rafael Espindola | a800445 | 2014-05-16 14:22:33 +0000 | [diff] [blame] | 3030 | auto *PTy = dyn_cast<PointerType>(Ty); |
| 3031 | if (!PTy) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3032 | return Error("Invalid type for value"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3033 | |
Rafael Espindola | a800445 | 2014-05-16 14:22:33 +0000 | [diff] [blame] | 3034 | auto *NewGA = |
Rafael Espindola | f1bedd374 | 2014-05-17 21:29:57 +0000 | [diff] [blame] | 3035 | GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(), |
Rafael Espindola | 7b4b2dc | 2015-01-08 15:36:32 +0000 | [diff] [blame] | 3036 | getDecodedLinkage(Record[2]), "", TheModule); |
Anton Korobeynikov | 2f22e3f | 2008-03-12 00:49:19 +0000 | [diff] [blame] | 3037 | // Old bitcode files didn't have visibility field. |
Duncan P. N. Exon Smith | b80de10 | 2014-05-07 22:57:20 +0000 | [diff] [blame] | 3038 | // Local linkage must have default visibility. |
| 3039 | if (Record.size() > 3 && !NewGA->hasLocalLinkage()) |
| 3040 | // FIXME: Change to an error if non-default in 4.0. |
Anton Korobeynikov | 2f22e3f | 2008-03-12 00:49:19 +0000 | [diff] [blame] | 3041 | NewGA->setVisibility(GetDecodedVisibility(Record[3])); |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 3042 | if (Record.size() > 4) |
| 3043 | NewGA->setDLLStorageClass(GetDecodedDLLStorageClass(Record[4])); |
| 3044 | else |
| 3045 | UpgradeDLLImportExportLinkage(NewGA, Record[2]); |
Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 3046 | if (Record.size() > 5) |
NAKAMURA Takumi | 32c87ac | 2014-10-29 23:44:35 +0000 | [diff] [blame] | 3047 | NewGA->setThreadLocalMode(GetDecodedThreadLocalMode(Record[5])); |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 3048 | if (Record.size() > 6) |
NAKAMURA Takumi | 32c87ac | 2014-10-29 23:44:35 +0000 | [diff] [blame] | 3049 | NewGA->setUnnamedAddr(Record[6]); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 3050 | ValueList.push_back(NewGA); |
| 3051 | AliasInits.push_back(std::make_pair(NewGA, Record[1])); |
| 3052 | break; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3053 | } |
Chris Lattner | 831d420 | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 3054 | /// MODULE_CODE_PURGEVALS: [numvals] |
| 3055 | case bitc::MODULE_CODE_PURGEVALS: |
| 3056 | // Trim down the value list to the specified size. |
| 3057 | if (Record.size() < 1 || Record[0] > ValueList.size()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3058 | return Error("Invalid record"); |
Chris Lattner | 831d420 | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 3059 | ValueList.shrinkTo(Record[0]); |
| 3060 | break; |
| 3061 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3062 | Record.clear(); |
| 3063 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3064 | } |
| 3065 | |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 3066 | std::error_code BitcodeReader::ParseBitcodeInto(Module *M, |
| 3067 | bool ShouldLazyLoadMetadata) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3068 | TheModule = nullptr; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3069 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3070 | if (std::error_code EC = InitStream()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3071 | return EC; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3072 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3073 | // Sniff for the signature. |
| 3074 | if (Stream.Read(8) != 'B' || |
| 3075 | Stream.Read(8) != 'C' || |
| 3076 | Stream.Read(4) != 0x0 || |
| 3077 | Stream.Read(4) != 0xC || |
| 3078 | Stream.Read(4) != 0xE || |
| 3079 | Stream.Read(4) != 0xD) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3080 | return Error("Invalid bitcode signature"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3081 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3082 | // We expect a number of well-defined blocks, though we don't necessarily |
| 3083 | // need to understand them all. |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3084 | while (1) { |
Filipe Cabecinhas | 2255427 | 2015-04-14 14:07:15 +0000 | [diff] [blame] | 3085 | if (Stream.AtEndOfStream()) { |
| 3086 | if (TheModule) |
| 3087 | return std::error_code(); |
| 3088 | // We didn't really read a proper Module. |
| 3089 | return Error("Malformed IR file"); |
| 3090 | } |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3091 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3092 | BitstreamEntry Entry = |
| 3093 | Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3094 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3095 | switch (Entry.Kind) { |
| 3096 | case BitstreamEntry::Error: |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3097 | return Error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3098 | case BitstreamEntry::EndBlock: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3099 | return std::error_code(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3100 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3101 | case BitstreamEntry::SubBlock: |
| 3102 | switch (Entry.ID) { |
| 3103 | case bitc::BLOCKINFO_BLOCK_ID: |
| 3104 | if (Stream.ReadBlockInfoBlock()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3105 | return Error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3106 | break; |
| 3107 | case bitc::MODULE_BLOCK_ID: |
| 3108 | // Reject multiple MODULE_BLOCK's in a single bitstream. |
| 3109 | if (TheModule) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3110 | return Error("Invalid multiple blocks"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3111 | TheModule = M; |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 3112 | if (std::error_code EC = ParseModule(false, ShouldLazyLoadMetadata)) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3113 | return EC; |
| 3114 | if (LazyStreamer) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3115 | return std::error_code(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3116 | break; |
| 3117 | default: |
| 3118 | if (Stream.SkipBlock()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3119 | return Error("Invalid record"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3120 | break; |
| 3121 | } |
| 3122 | continue; |
| 3123 | case BitstreamEntry::Record: |
| 3124 | // There should be no records in the top-level of blocks. |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3125 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3126 | // The ranlib in Xcode 4 will align archive members by appending newlines |
Chad Rosier | a15e3aa | 2011-08-09 22:23:40 +0000 | [diff] [blame] | 3127 | // to the end of them. If this file size is a multiple of 4 but not 8, we |
| 3128 | // have to read and ignore these final 4 bytes :-( |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3129 | if (Stream.getAbbrevIDWidth() == 2 && Entry.ID == 2 && |
Rafael Espindola | a97b238 | 2011-05-26 18:59:54 +0000 | [diff] [blame] | 3130 | Stream.Read(6) == 2 && Stream.Read(24) == 0xa0a0a && |
Bill Wendling | 318f03f | 2012-07-19 00:15:11 +0000 | [diff] [blame] | 3131 | Stream.AtEndOfStream()) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3132 | return std::error_code(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3133 | |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3134 | return Error("Invalid record"); |
Rafael Espindola | a97b238 | 2011-05-26 18:59:54 +0000 | [diff] [blame] | 3135 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3136 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3137 | } |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 3138 | |
Rafael Espindola | c75c4fa | 2014-07-04 20:02:42 +0000 | [diff] [blame] | 3139 | ErrorOr<std::string> BitcodeReader::parseModuleTriple() { |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3140 | if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3141 | return Error("Invalid record"); |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3142 | |
| 3143 | SmallVector<uint64_t, 64> Record; |
| 3144 | |
Rafael Espindola | c75c4fa | 2014-07-04 20:02:42 +0000 | [diff] [blame] | 3145 | std::string Triple; |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3146 | // Read all the records for this module. |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3147 | while (1) { |
| 3148 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3149 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3150 | switch (Entry.Kind) { |
| 3151 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 3152 | case BitstreamEntry::Error: |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3153 | return Error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3154 | case BitstreamEntry::EndBlock: |
Rafael Espindola | e610779 | 2014-07-04 20:05:56 +0000 | [diff] [blame] | 3155 | return Triple; |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3156 | case BitstreamEntry::Record: |
| 3157 | // The interesting case. |
| 3158 | break; |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3159 | } |
| 3160 | |
| 3161 | // Read a record. |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3162 | switch (Stream.readRecord(Entry.ID, Record)) { |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3163 | default: break; // Default behavior, ignore unknown content. |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3164 | case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] |
Rafael Espindola | c75c4fa | 2014-07-04 20:02:42 +0000 | [diff] [blame] | 3165 | std::string S; |
| 3166 | if (ConvertToString(Record, 0, S)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3167 | return Error("Invalid record"); |
Rafael Espindola | c75c4fa | 2014-07-04 20:02:42 +0000 | [diff] [blame] | 3168 | Triple = S; |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3169 | break; |
| 3170 | } |
| 3171 | } |
| 3172 | Record.clear(); |
| 3173 | } |
Rafael Espindola | e610779 | 2014-07-04 20:05:56 +0000 | [diff] [blame] | 3174 | llvm_unreachable("Exit infinite loop"); |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3175 | } |
| 3176 | |
Rafael Espindola | c75c4fa | 2014-07-04 20:02:42 +0000 | [diff] [blame] | 3177 | ErrorOr<std::string> BitcodeReader::parseTriple() { |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3178 | if (std::error_code EC = InitStream()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3179 | return EC; |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3180 | |
| 3181 | // Sniff for the signature. |
| 3182 | if (Stream.Read(8) != 'B' || |
| 3183 | Stream.Read(8) != 'C' || |
| 3184 | Stream.Read(4) != 0x0 || |
| 3185 | Stream.Read(4) != 0xC || |
| 3186 | Stream.Read(4) != 0xE || |
| 3187 | Stream.Read(4) != 0xD) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3188 | return Error("Invalid bitcode signature"); |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3189 | |
| 3190 | // We expect a number of well-defined blocks, though we don't necessarily |
| 3191 | // need to understand them all. |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3192 | while (1) { |
| 3193 | BitstreamEntry Entry = Stream.advance(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3194 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3195 | switch (Entry.Kind) { |
| 3196 | case BitstreamEntry::Error: |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3197 | return Error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3198 | case BitstreamEntry::EndBlock: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3199 | return std::error_code(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3200 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3201 | case BitstreamEntry::SubBlock: |
| 3202 | if (Entry.ID == bitc::MODULE_BLOCK_ID) |
Rafael Espindola | d346cc8 | 2014-07-04 13:52:01 +0000 | [diff] [blame] | 3203 | return parseModuleTriple(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3204 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3205 | // Ignore other sub-blocks. |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3206 | if (Stream.SkipBlock()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3207 | return Error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3208 | continue; |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3209 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3210 | case BitstreamEntry::Record: |
| 3211 | Stream.skipRecord(Entry.ID); |
| 3212 | continue; |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3213 | } |
| 3214 | } |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 3215 | } |
| 3216 | |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3217 | /// ParseMetadataAttachment - Parse metadata attachments. |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3218 | std::error_code BitcodeReader::ParseMetadataAttachment() { |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3219 | if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3220 | return Error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3221 | |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3222 | SmallVector<uint64_t, 64> Record; |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3223 | while (1) { |
| 3224 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3225 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3226 | switch (Entry.Kind) { |
| 3227 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 3228 | case BitstreamEntry::Error: |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3229 | return Error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3230 | case BitstreamEntry::EndBlock: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3231 | return std::error_code(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3232 | case BitstreamEntry::Record: |
| 3233 | // The interesting case. |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3234 | break; |
| 3235 | } |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3236 | |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3237 | // Read a metadata attachment record. |
| 3238 | Record.clear(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3239 | switch (Stream.readRecord(Entry.ID, Record)) { |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3240 | default: // Default behavior: ignore. |
| 3241 | break; |
Chris Lattner | b877855 | 2011-06-17 17:50:30 +0000 | [diff] [blame] | 3242 | case bitc::METADATA_ATTACHMENT: { |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3243 | unsigned RecordLength = Record.size(); |
| 3244 | if (Record.empty() || (RecordLength - 1) % 2 == 1) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3245 | return Error("Invalid record"); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3246 | Instruction *Inst = InstructionList[Record[0]]; |
| 3247 | for (unsigned i = 1; i != RecordLength; i = i+2) { |
Devang Patel | b1a4477 | 2009-09-28 21:14:55 +0000 | [diff] [blame] | 3248 | unsigned Kind = Record[i]; |
Dan Gohman | 43aa8f0 | 2010-07-20 21:42:28 +0000 | [diff] [blame] | 3249 | DenseMap<unsigned, unsigned>::iterator I = |
| 3250 | MDKindMap.find(Kind); |
| 3251 | if (I == MDKindMap.end()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3252 | return Error("Invalid ID"); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 3253 | Metadata *Node = MDValueList.getValueFwdRef(Record[i + 1]); |
| 3254 | if (isa<LocalAsMetadata>(Node)) |
Duncan P. N. Exon Smith | 35303fd | 2014-12-06 02:29:44 +0000 | [diff] [blame] | 3255 | // Drop the attachment. This used to be legal, but there's no |
| 3256 | // upgrade path. |
| 3257 | break; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 3258 | Inst->setMetadata(I->second, cast<MDNode>(Node)); |
Manman Ren | 209b17c | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 3259 | if (I->second == LLVMContext::MD_tbaa) |
| 3260 | InstsWithTBAATag.push_back(Inst); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3261 | } |
| 3262 | break; |
| 3263 | } |
| 3264 | } |
| 3265 | } |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3266 | } |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 3267 | |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3268 | /// ParseFunctionBody - Lazily parse the specified function body block. |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3269 | std::error_code BitcodeReader::ParseFunctionBody(Function *F) { |
Chris Lattner | 982ec1e | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 3270 | if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3271 | return Error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3272 | |
Nick Lewycky | a72e1af | 2010-02-25 08:30:17 +0000 | [diff] [blame] | 3273 | InstructionList.clear(); |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3274 | unsigned ModuleValueListSize = ValueList.size(); |
Dan Gohman | 26d837d | 2010-08-25 20:22:53 +0000 | [diff] [blame] | 3275 | unsigned ModuleMDValueListSize = MDValueList.size(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3276 | |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3277 | // Add all the function arguments to the value table. |
| 3278 | for(Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I) |
| 3279 | ValueList.push_back(I); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3280 | |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 3281 | unsigned NextValueNo = ValueList.size(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3282 | BasicBlock *CurBB = nullptr; |
Chris Lattner | e53603e | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 3283 | unsigned CurBBNo = 0; |
| 3284 | |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 3285 | DebugLoc LastLoc; |
Duncan P. N. Exon Smith | 52d0f16 | 2015-01-09 02:51:45 +0000 | [diff] [blame] | 3286 | auto getLastInstruction = [&]() -> Instruction * { |
| 3287 | if (CurBB && !CurBB->empty()) |
| 3288 | return &CurBB->back(); |
| 3289 | else if (CurBBNo && FunctionBBs[CurBBNo - 1] && |
| 3290 | !FunctionBBs[CurBBNo - 1]->empty()) |
| 3291 | return &FunctionBBs[CurBBNo - 1]->back(); |
| 3292 | return nullptr; |
| 3293 | }; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3294 | |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3295 | // Read all the records. |
| 3296 | SmallVector<uint64_t, 64> Record; |
| 3297 | while (1) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3298 | BitstreamEntry Entry = Stream.advance(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3299 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3300 | switch (Entry.Kind) { |
| 3301 | case BitstreamEntry::Error: |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3302 | return Error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3303 | case BitstreamEntry::EndBlock: |
| 3304 | goto OutOfRecordLoop; |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3305 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3306 | case BitstreamEntry::SubBlock: |
| 3307 | switch (Entry.ID) { |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3308 | default: // Skip unknown content. |
| 3309 | if (Stream.SkipBlock()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3310 | return Error("Invalid record"); |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3311 | break; |
| 3312 | case bitc::CONSTANTS_BLOCK_ID: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3313 | if (std::error_code EC = ParseConstants()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3314 | return EC; |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 3315 | NextValueNo = ValueList.size(); |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3316 | break; |
| 3317 | case bitc::VALUE_SYMTAB_BLOCK_ID: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3318 | if (std::error_code EC = ParseValueSymbolTable()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3319 | return EC; |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3320 | break; |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3321 | case bitc::METADATA_ATTACHMENT_ID: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3322 | if (std::error_code EC = ParseMetadataAttachment()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3323 | return EC; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3324 | break; |
Victor Hernandez | 108d3ac | 2010-01-13 19:34:08 +0000 | [diff] [blame] | 3325 | case bitc::METADATA_BLOCK_ID: |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 3326 | if (std::error_code EC = ParseMetadata()) |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3327 | return EC; |
Victor Hernandez | 108d3ac | 2010-01-13 19:34:08 +0000 | [diff] [blame] | 3328 | break; |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 3329 | case bitc::USELIST_BLOCK_ID: |
| 3330 | if (std::error_code EC = ParseUseLists()) |
| 3331 | return EC; |
| 3332 | break; |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3333 | } |
| 3334 | continue; |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3335 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3336 | case BitstreamEntry::Record: |
| 3337 | // The interesting case. |
| 3338 | break; |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3339 | } |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3340 | |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3341 | // Read a record. |
| 3342 | Record.clear(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3343 | Instruction *I = nullptr; |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3344 | unsigned BitCode = Stream.readRecord(Entry.ID, Record); |
Dan Gohman | 0ebd696 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 3345 | switch (BitCode) { |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 3346 | default: // Default behavior: reject |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3347 | return Error("Invalid value"); |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 3348 | case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks] |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 3349 | if (Record.size() < 1 || Record[0] == 0) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3350 | return Error("Invalid record"); |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3351 | // Create all the basic blocks for the function. |
Chris Lattner | 6ce15cb | 2007-05-03 22:09:51 +0000 | [diff] [blame] | 3352 | FunctionBBs.resize(Record[0]); |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 3353 | |
| 3354 | // See if anything took the address of blocks in this function. |
| 3355 | auto BBFRI = BasicBlockFwdRefs.find(F); |
| 3356 | if (BBFRI == BasicBlockFwdRefs.end()) { |
| 3357 | for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i) |
| 3358 | FunctionBBs[i] = BasicBlock::Create(Context, "", F); |
| 3359 | } else { |
| 3360 | auto &BBRefs = BBFRI->second; |
Duncan P. N. Exon Smith | 5a5fd7b | 2014-08-16 01:54:37 +0000 | [diff] [blame] | 3361 | // Check for invalid basic block references. |
| 3362 | if (BBRefs.size() > FunctionBBs.size()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3363 | return Error("Invalid ID"); |
Duncan P. N. Exon Smith | 5a5fd7b | 2014-08-16 01:54:37 +0000 | [diff] [blame] | 3364 | assert(!BBRefs.empty() && "Unexpected empty array"); |
| 3365 | assert(!BBRefs.front() && "Invalid reference to entry block"); |
| 3366 | for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E; |
| 3367 | ++I) |
| 3368 | if (I < RE && BBRefs[I]) { |
| 3369 | BBRefs[I]->insertInto(F); |
| 3370 | FunctionBBs[I] = BBRefs[I]; |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 3371 | } else { |
| 3372 | FunctionBBs[I] = BasicBlock::Create(Context, "", F); |
| 3373 | } |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 3374 | |
| 3375 | // Erase from the table. |
| 3376 | BasicBlockFwdRefs.erase(BBFRI); |
| 3377 | } |
| 3378 | |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 3379 | CurBB = FunctionBBs[0]; |
| 3380 | continue; |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 3381 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3382 | |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 3383 | case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN |
| 3384 | // This record indicates that the last instruction is at the same |
| 3385 | // location as the previous instruction with a location. |
Duncan P. N. Exon Smith | 52d0f16 | 2015-01-09 02:51:45 +0000 | [diff] [blame] | 3386 | I = getLastInstruction(); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3387 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3388 | if (!I) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3389 | return Error("Invalid record"); |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 3390 | I->setDebugLoc(LastLoc); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3391 | I = nullptr; |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 3392 | continue; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3393 | |
Duncan P. N. Exon Smith | 9ed1966 | 2015-01-09 17:53:27 +0000 | [diff] [blame] | 3394 | 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] | 3395 | I = getLastInstruction(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3396 | if (!I || Record.size() < 4) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3397 | return Error("Invalid record"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3398 | |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 3399 | unsigned Line = Record[0], Col = Record[1]; |
| 3400 | unsigned ScopeID = Record[2], IAID = Record[3]; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3401 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3402 | MDNode *Scope = nullptr, *IA = nullptr; |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 3403 | if (ScopeID) Scope = cast<MDNode>(MDValueList.getValueFwdRef(ScopeID-1)); |
| 3404 | if (IAID) IA = cast<MDNode>(MDValueList.getValueFwdRef(IAID-1)); |
| 3405 | LastLoc = DebugLoc::get(Line, Col, Scope, IA); |
| 3406 | I->setDebugLoc(LastLoc); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3407 | I = nullptr; |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 3408 | continue; |
| 3409 | } |
| 3410 | |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 3411 | case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode] |
| 3412 | unsigned OpNum = 0; |
| 3413 | Value *LHS, *RHS; |
| 3414 | if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3415 | popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) || |
Dan Gohman | 0ebd696 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 3416 | OpNum+1 > Record.size()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3417 | return Error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3418 | |
Dan Gohman | 0ebd696 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 3419 | int Opc = GetDecodedBinaryOpcode(Record[OpNum++], LHS->getType()); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3420 | if (Opc == -1) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3421 | return Error("Invalid record"); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3422 | I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3423 | InstructionList.push_back(I); |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 3424 | if (OpNum < Record.size()) { |
| 3425 | if (Opc == Instruction::Add || |
| 3426 | Opc == Instruction::Sub || |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 3427 | Opc == Instruction::Mul || |
| 3428 | Opc == Instruction::Shl) { |
Dan Gohman | 00f4747 | 2010-01-25 21:55:39 +0000 | [diff] [blame] | 3429 | if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP)) |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 3430 | cast<BinaryOperator>(I)->setHasNoSignedWrap(true); |
Dan Gohman | 00f4747 | 2010-01-25 21:55:39 +0000 | [diff] [blame] | 3431 | if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 3432 | cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true); |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 3433 | } else if (Opc == Instruction::SDiv || |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 3434 | Opc == Instruction::UDiv || |
| 3435 | Opc == Instruction::LShr || |
| 3436 | Opc == Instruction::AShr) { |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 3437 | if (Record[OpNum] & (1 << bitc::PEO_EXACT)) |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 3438 | cast<BinaryOperator>(I)->setIsExact(true); |
Michael Ilseman | 9978d7e | 2012-11-27 00:43:38 +0000 | [diff] [blame] | 3439 | } else if (isa<FPMathOperator>(I)) { |
| 3440 | FastMathFlags FMF; |
Michael Ilseman | 65f1435 | 2012-12-09 21:12:04 +0000 | [diff] [blame] | 3441 | if (0 != (Record[OpNum] & FastMathFlags::UnsafeAlgebra)) |
| 3442 | FMF.setUnsafeAlgebra(); |
| 3443 | if (0 != (Record[OpNum] & FastMathFlags::NoNaNs)) |
| 3444 | FMF.setNoNaNs(); |
| 3445 | if (0 != (Record[OpNum] & FastMathFlags::NoInfs)) |
| 3446 | FMF.setNoInfs(); |
| 3447 | if (0 != (Record[OpNum] & FastMathFlags::NoSignedZeros)) |
| 3448 | FMF.setNoSignedZeros(); |
| 3449 | if (0 != (Record[OpNum] & FastMathFlags::AllowReciprocal)) |
| 3450 | FMF.setAllowReciprocal(); |
Michael Ilseman | 9978d7e | 2012-11-27 00:43:38 +0000 | [diff] [blame] | 3451 | if (FMF.any()) |
| 3452 | I->setFastMathFlags(FMF); |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 3453 | } |
Michael Ilseman | 9978d7e | 2012-11-27 00:43:38 +0000 | [diff] [blame] | 3454 | |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 3455 | } |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3456 | break; |
| 3457 | } |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 3458 | case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc] |
| 3459 | unsigned OpNum = 0; |
| 3460 | Value *Op; |
| 3461 | if (getValueTypePair(Record, OpNum, NextValueNo, Op) || |
| 3462 | OpNum+2 != Record.size()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3463 | return Error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3464 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3465 | Type *ResTy = getTypeByID(Record[OpNum]); |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 3466 | int Opc = GetDecodedCastOpcode(Record[OpNum+1]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3467 | if (Opc == -1 || !ResTy) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3468 | return Error("Invalid record"); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3469 | Instruction *Temp = nullptr; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3470 | if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) { |
| 3471 | if (Temp) { |
| 3472 | InstructionList.push_back(Temp); |
| 3473 | CurBB->getInstList().push_back(Temp); |
| 3474 | } |
| 3475 | } else { |
| 3476 | I = CastInst::Create((Instruction::CastOps)Opc, Op, ResTy); |
| 3477 | } |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3478 | InstructionList.push_back(I); |
Chris Lattner | e53603e | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 3479 | break; |
| 3480 | } |
David Blaikie | b5b5efd | 2015-02-25 01:08:52 +0000 | [diff] [blame] | 3481 | case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD: |
| 3482 | case bitc::FUNC_CODE_INST_GEP_OLD: |
| 3483 | case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands] |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 3484 | unsigned OpNum = 0; |
David Blaikie | b5b5efd | 2015-02-25 01:08:52 +0000 | [diff] [blame] | 3485 | |
| 3486 | Type *Ty; |
| 3487 | bool InBounds; |
| 3488 | |
| 3489 | if (BitCode == bitc::FUNC_CODE_INST_GEP) { |
| 3490 | InBounds = Record[OpNum++]; |
| 3491 | Ty = getTypeByID(Record[OpNum++]); |
| 3492 | } else { |
| 3493 | InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD; |
| 3494 | Ty = nullptr; |
| 3495 | } |
| 3496 | |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 3497 | Value *BasePtr; |
| 3498 | if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3499 | return Error("Invalid record"); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 3500 | |
David Blaikie | 675e8cb | 2015-03-16 21:35:48 +0000 | [diff] [blame] | 3501 | if (Ty && |
| 3502 | Ty != |
| 3503 | cast<SequentialType>(BasePtr->getType()->getScalarType()) |
| 3504 | ->getElementType()) |
| 3505 | return Error( |
| 3506 | "Explicit gep type does not match pointee type of pointer operand"); |
| 3507 | |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3508 | SmallVector<Value*, 16> GEPIdx; |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 3509 | while (OpNum != Record.size()) { |
| 3510 | Value *Op; |
| 3511 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3512 | return Error("Invalid record"); |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 3513 | GEPIdx.push_back(Op); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 3514 | } |
| 3515 | |
David Blaikie | 096b1da | 2015-03-14 19:53:33 +0000 | [diff] [blame] | 3516 | I = GetElementPtrInst::Create(Ty, BasePtr, GEPIdx); |
David Blaikie | 675e8cb | 2015-03-16 21:35:48 +0000 | [diff] [blame] | 3517 | |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3518 | InstructionList.push_back(I); |
David Blaikie | b5b5efd | 2015-02-25 01:08:52 +0000 | [diff] [blame] | 3519 | if (InBounds) |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 3520 | cast<GetElementPtrInst>(I)->setIsInBounds(true); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 3521 | break; |
| 3522 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3523 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 3524 | case bitc::FUNC_CODE_INST_EXTRACTVAL: { |
| 3525 | // EXTRACTVAL: [opty, opval, n x indices] |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3526 | unsigned OpNum = 0; |
| 3527 | Value *Agg; |
| 3528 | if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3529 | return Error("Invalid record"); |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3530 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 3531 | SmallVector<unsigned, 4> EXTRACTVALIdx; |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 3532 | Type *CurTy = Agg->getType(); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 3533 | for (unsigned RecSize = Record.size(); |
| 3534 | OpNum != RecSize; ++OpNum) { |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 3535 | bool IsArray = CurTy->isArrayTy(); |
| 3536 | bool IsStruct = CurTy->isStructTy(); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 3537 | uint64_t Index = Record[OpNum]; |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 3538 | |
| 3539 | if (!IsStruct && !IsArray) |
| 3540 | return Error("EXTRACTVAL: Invalid type"); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 3541 | if ((unsigned)Index != Index) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3542 | return Error("Invalid value"); |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 3543 | if (IsStruct && Index >= CurTy->subtypes().size()) |
| 3544 | return Error("EXTRACTVAL: Invalid struct index"); |
| 3545 | if (IsArray && Index >= CurTy->getArrayNumElements()) |
| 3546 | return Error("EXTRACTVAL: Invalid array index"); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 3547 | EXTRACTVALIdx.push_back((unsigned)Index); |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 3548 | |
| 3549 | if (IsStruct) |
| 3550 | CurTy = CurTy->subtypes()[Index]; |
| 3551 | else |
| 3552 | CurTy = CurTy->subtypes()[0]; |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3553 | } |
| 3554 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3555 | I = ExtractValueInst::Create(Agg, EXTRACTVALIdx); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3556 | InstructionList.push_back(I); |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3557 | break; |
| 3558 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3559 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 3560 | case bitc::FUNC_CODE_INST_INSERTVAL: { |
| 3561 | // INSERTVAL: [opty, opval, opty, opval, n x indices] |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3562 | unsigned OpNum = 0; |
| 3563 | Value *Agg; |
| 3564 | if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3565 | return Error("Invalid record"); |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3566 | Value *Val; |
| 3567 | if (getValueTypePair(Record, OpNum, NextValueNo, Val)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3568 | return Error("Invalid record"); |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3569 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 3570 | SmallVector<unsigned, 4> INSERTVALIdx; |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 3571 | Type *CurTy = Agg->getType(); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 3572 | for (unsigned RecSize = Record.size(); |
| 3573 | OpNum != RecSize; ++OpNum) { |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 3574 | bool IsArray = CurTy->isArrayTy(); |
| 3575 | bool IsStruct = CurTy->isStructTy(); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 3576 | uint64_t Index = Record[OpNum]; |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 3577 | |
| 3578 | if (!IsStruct && !IsArray) |
| 3579 | return Error("INSERTVAL: Invalid type"); |
| 3580 | if (!CurTy->isStructTy() && !CurTy->isArrayTy()) |
| 3581 | return Error("Invalid type"); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 3582 | if ((unsigned)Index != Index) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3583 | return Error("Invalid value"); |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 3584 | if (IsStruct && Index >= CurTy->subtypes().size()) |
| 3585 | return Error("INSERTVAL: Invalid struct index"); |
| 3586 | if (IsArray && Index >= CurTy->getArrayNumElements()) |
| 3587 | return Error("INSERTVAL: Invalid array index"); |
| 3588 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 3589 | INSERTVALIdx.push_back((unsigned)Index); |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 3590 | if (IsStruct) |
| 3591 | CurTy = CurTy->subtypes()[Index]; |
| 3592 | else |
| 3593 | CurTy = CurTy->subtypes()[0]; |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3594 | } |
| 3595 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3596 | I = InsertValueInst::Create(Agg, Val, INSERTVALIdx); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3597 | InstructionList.push_back(I); |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3598 | break; |
| 3599 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3600 | |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 3601 | case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval] |
Dan Gohman | c5d2892 | 2008-09-16 01:01:33 +0000 | [diff] [blame] | 3602 | // obsolete form of select |
| 3603 | // handles select i1 ... in old bitcode |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 3604 | unsigned OpNum = 0; |
| 3605 | Value *TrueVal, *FalseVal, *Cond; |
| 3606 | if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3607 | popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) || |
| 3608 | popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3609 | return Error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3610 | |
Dan Gohman | c5d2892 | 2008-09-16 01:01:33 +0000 | [diff] [blame] | 3611 | I = SelectInst::Create(Cond, TrueVal, FalseVal); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3612 | InstructionList.push_back(I); |
Dan Gohman | c5d2892 | 2008-09-16 01:01:33 +0000 | [diff] [blame] | 3613 | break; |
| 3614 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3615 | |
Dan Gohman | c5d2892 | 2008-09-16 01:01:33 +0000 | [diff] [blame] | 3616 | case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred] |
| 3617 | // new form of select |
| 3618 | // handles select i1 or select [N x i1] |
| 3619 | unsigned OpNum = 0; |
| 3620 | Value *TrueVal, *FalseVal, *Cond; |
| 3621 | if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3622 | popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) || |
Dan Gohman | c5d2892 | 2008-09-16 01:01:33 +0000 | [diff] [blame] | 3623 | getValueTypePair(Record, OpNum, NextValueNo, Cond)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3624 | return Error("Invalid record"); |
Dan Gohman | c579d97 | 2008-09-09 01:02:47 +0000 | [diff] [blame] | 3625 | |
| 3626 | // select condition can be either i1 or [N x i1] |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3627 | if (VectorType* vector_type = |
| 3628 | dyn_cast<VectorType>(Cond->getType())) { |
Dan Gohman | c579d97 | 2008-09-09 01:02:47 +0000 | [diff] [blame] | 3629 | // expect <n x i1> |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3630 | if (vector_type->getElementType() != Type::getInt1Ty(Context)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3631 | return Error("Invalid type for value"); |
Dan Gohman | c579d97 | 2008-09-09 01:02:47 +0000 | [diff] [blame] | 3632 | } else { |
| 3633 | // expect i1 |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3634 | if (Cond->getType() != Type::getInt1Ty(Context)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3635 | return Error("Invalid type for value"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3636 | } |
| 3637 | |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 3638 | I = SelectInst::Create(Cond, TrueVal, FalseVal); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3639 | InstructionList.push_back(I); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 3640 | break; |
| 3641 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3642 | |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 3643 | case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval] |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 3644 | unsigned OpNum = 0; |
| 3645 | Value *Vec, *Idx; |
| 3646 | if (getValueTypePair(Record, OpNum, NextValueNo, Vec) || |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 3647 | getValueTypePair(Record, OpNum, NextValueNo, Idx)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3648 | return Error("Invalid record"); |
Filipe Cabecinhas | ff1e234 | 2015-04-24 11:30:15 +0000 | [diff] [blame] | 3649 | if (!Vec->getType()->isVectorTy()) |
| 3650 | return Error("Invalid type for value"); |
Eric Christopher | c974225 | 2009-07-25 02:28:41 +0000 | [diff] [blame] | 3651 | I = ExtractElementInst::Create(Vec, Idx); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3652 | InstructionList.push_back(I); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 3653 | break; |
| 3654 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3655 | |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 3656 | case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval] |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 3657 | unsigned OpNum = 0; |
| 3658 | Value *Vec, *Elt, *Idx; |
Filipe Cabecinhas | ff1e234 | 2015-04-24 11:30:15 +0000 | [diff] [blame] | 3659 | if (getValueTypePair(Record, OpNum, NextValueNo, Vec)) |
| 3660 | return Error("Invalid record"); |
| 3661 | if (!Vec->getType()->isVectorTy()) |
| 3662 | return Error("Invalid type for value"); |
| 3663 | if (popValue(Record, OpNum, NextValueNo, |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 3664 | cast<VectorType>(Vec->getType())->getElementType(), Elt) || |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 3665 | getValueTypePair(Record, OpNum, NextValueNo, Idx)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3666 | return Error("Invalid record"); |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 3667 | I = InsertElementInst::Create(Vec, Elt, Idx); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3668 | InstructionList.push_back(I); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 3669 | break; |
| 3670 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3671 | |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 3672 | case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval] |
| 3673 | unsigned OpNum = 0; |
| 3674 | Value *Vec1, *Vec2, *Mask; |
| 3675 | if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3676 | popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3677 | return Error("Invalid record"); |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 3678 | |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3679 | if (getValueTypePair(Record, OpNum, NextValueNo, Mask)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3680 | return Error("Invalid record"); |
Filipe Cabecinhas | ff1e234 | 2015-04-24 11:30:15 +0000 | [diff] [blame] | 3681 | if (!Vec1->getType()->isVectorTy() || !Vec2->getType()->isVectorTy()) |
| 3682 | return Error("Invalid type for value"); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 3683 | I = new ShuffleVectorInst(Vec1, Vec2, Mask); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3684 | InstructionList.push_back(I); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 3685 | break; |
| 3686 | } |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3687 | |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 3688 | case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred] |
| 3689 | // Old form of ICmp/FCmp returning bool |
| 3690 | // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were |
| 3691 | // both legal on vectors but had different behaviour. |
| 3692 | case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred] |
| 3693 | // FCmp/ICmp returning bool or vector of bool |
| 3694 | |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 3695 | unsigned OpNum = 0; |
| 3696 | Value *LHS, *RHS; |
| 3697 | if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3698 | popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) || |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 3699 | OpNum+1 != Record.size()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3700 | return Error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3701 | |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 3702 | if (LHS->getType()->isFPOrFPVectorTy()) |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 3703 | I = new FCmpInst((FCmpInst::Predicate)Record[OpNum], LHS, RHS); |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 3704 | else |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 3705 | I = new ICmpInst((ICmpInst::Predicate)Record[OpNum], LHS, RHS); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3706 | InstructionList.push_back(I); |
Dan Gohman | c579d97 | 2008-09-09 01:02:47 +0000 | [diff] [blame] | 3707 | break; |
| 3708 | } |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 3709 | |
Chris Lattner | e53603e | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 3710 | case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>] |
Devang Patel | bbfd874 | 2008-02-26 01:29:32 +0000 | [diff] [blame] | 3711 | { |
| 3712 | unsigned Size = Record.size(); |
| 3713 | if (Size == 0) { |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3714 | I = ReturnInst::Create(Context); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3715 | InstructionList.push_back(I); |
Devang Patel | bbfd874 | 2008-02-26 01:29:32 +0000 | [diff] [blame] | 3716 | break; |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 3717 | } |
Devang Patel | bbfd874 | 2008-02-26 01:29:32 +0000 | [diff] [blame] | 3718 | |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 3719 | unsigned OpNum = 0; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3720 | Value *Op = nullptr; |
Chris Lattner | f1c8710 | 2011-06-17 18:09:11 +0000 | [diff] [blame] | 3721 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3722 | return Error("Invalid record"); |
Chris Lattner | f1c8710 | 2011-06-17 18:09:11 +0000 | [diff] [blame] | 3723 | if (OpNum != Record.size()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3724 | return Error("Invalid record"); |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 3725 | |
Chris Lattner | f1c8710 | 2011-06-17 18:09:11 +0000 | [diff] [blame] | 3726 | I = ReturnInst::Create(Context, Op); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3727 | InstructionList.push_back(I); |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 3728 | break; |
Chris Lattner | e53603e | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 3729 | } |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3730 | case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#] |
Chris Lattner | 6ce15cb | 2007-05-03 22:09:51 +0000 | [diff] [blame] | 3731 | if (Record.size() != 1 && Record.size() != 3) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3732 | return Error("Invalid record"); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3733 | BasicBlock *TrueDest = getBasicBlock(Record[0]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3734 | if (!TrueDest) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3735 | return Error("Invalid record"); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3736 | |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3737 | if (Record.size() == 1) { |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 3738 | I = BranchInst::Create(TrueDest); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3739 | InstructionList.push_back(I); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3740 | } |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3741 | else { |
| 3742 | BasicBlock *FalseDest = getBasicBlock(Record[1]); |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3743 | Value *Cond = getValue(Record, 2, NextValueNo, |
| 3744 | Type::getInt1Ty(Context)); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3745 | if (!FalseDest || !Cond) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3746 | return Error("Invalid record"); |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 3747 | I = BranchInst::Create(TrueDest, FalseDest, Cond); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3748 | InstructionList.push_back(I); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3749 | } |
| 3750 | break; |
| 3751 | } |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3752 | case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...] |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3753 | // Check magic |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3754 | if ((Record[0] >> 16) == SWITCH_INST_MAGIC) { |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 3755 | // "New" SwitchInst format with case ranges. The changes to write this |
| 3756 | // format were reverted but we still recognize bitcode that uses it. |
| 3757 | // Hopefully someday we will have support for case ranges and can use |
| 3758 | // this format again. |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3759 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3760 | Type *OpTy = getTypeByID(Record[1]); |
| 3761 | unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth(); |
| 3762 | |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3763 | Value *Cond = getValue(Record, 2, NextValueNo, OpTy); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3764 | BasicBlock *Default = getBasicBlock(Record[3]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3765 | if (!OpTy || !Cond || !Default) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3766 | return Error("Invalid record"); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3767 | |
| 3768 | unsigned NumCases = Record[4]; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3769 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3770 | SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); |
| 3771 | InstructionList.push_back(SI); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3772 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3773 | unsigned CurIdx = 5; |
| 3774 | for (unsigned i = 0; i != NumCases; ++i) { |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 3775 | SmallVector<ConstantInt*, 1> CaseVals; |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3776 | unsigned NumItems = Record[CurIdx++]; |
| 3777 | for (unsigned ci = 0; ci != NumItems; ++ci) { |
| 3778 | bool isSingleNumber = Record[CurIdx++]; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3779 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3780 | APInt Low; |
| 3781 | unsigned ActiveWords = 1; |
| 3782 | if (ValueBitWidth > 64) |
| 3783 | ActiveWords = Record[CurIdx++]; |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 3784 | Low = ReadWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords), |
| 3785 | ValueBitWidth); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3786 | CurIdx += ActiveWords; |
Stepan Dyatkovskiy | e3e19cb | 2012-05-28 12:39:09 +0000 | [diff] [blame] | 3787 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3788 | if (!isSingleNumber) { |
| 3789 | ActiveWords = 1; |
| 3790 | if (ValueBitWidth > 64) |
| 3791 | ActiveWords = Record[CurIdx++]; |
| 3792 | APInt High = |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 3793 | ReadWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords), |
| 3794 | ValueBitWidth); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3795 | CurIdx += ActiveWords; |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 3796 | |
| 3797 | // FIXME: It is not clear whether values in the range should be |
| 3798 | // compared as signed or unsigned values. The partially |
| 3799 | // implemented changes that used this format in the past used |
| 3800 | // unsigned comparisons. |
| 3801 | for ( ; Low.ule(High); ++Low) |
| 3802 | CaseVals.push_back(ConstantInt::get(Context, Low)); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3803 | } else |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 3804 | CaseVals.push_back(ConstantInt::get(Context, Low)); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3805 | } |
| 3806 | BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]); |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 3807 | for (SmallVector<ConstantInt*, 1>::iterator cvi = CaseVals.begin(), |
| 3808 | cve = CaseVals.end(); cvi != cve; ++cvi) |
| 3809 | SI->addCase(*cvi, DestBB); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3810 | } |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3811 | I = SI; |
| 3812 | break; |
| 3813 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3814 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3815 | // Old SwitchInst format without case ranges. |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3816 | |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3817 | if (Record.size() < 3 || (Record.size() & 1) == 0) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3818 | return Error("Invalid record"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3819 | Type *OpTy = getTypeByID(Record[0]); |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3820 | Value *Cond = getValue(Record, 1, NextValueNo, OpTy); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3821 | BasicBlock *Default = getBasicBlock(Record[2]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3822 | if (!OpTy || !Cond || !Default) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3823 | return Error("Invalid record"); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3824 | unsigned NumCases = (Record.size()-3)/2; |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 3825 | SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3826 | InstructionList.push_back(SI); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3827 | for (unsigned i = 0, e = NumCases; i != e; ++i) { |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3828 | ConstantInt *CaseVal = |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3829 | dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy)); |
| 3830 | BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3831 | if (!CaseVal || !DestBB) { |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3832 | delete SI; |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3833 | return Error("Invalid record"); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3834 | } |
| 3835 | SI->addCase(CaseVal, DestBB); |
| 3836 | } |
| 3837 | I = SI; |
| 3838 | break; |
| 3839 | } |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3840 | case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...] |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3841 | if (Record.size() < 2) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3842 | return Error("Invalid record"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3843 | Type *OpTy = getTypeByID(Record[0]); |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3844 | Value *Address = getValue(Record, 1, NextValueNo, OpTy); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3845 | if (!OpTy || !Address) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3846 | return Error("Invalid record"); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3847 | unsigned NumDests = Record.size()-2; |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3848 | IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3849 | InstructionList.push_back(IBI); |
| 3850 | for (unsigned i = 0, e = NumDests; i != e; ++i) { |
| 3851 | if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) { |
| 3852 | IBI->addDestination(DestBB); |
| 3853 | } else { |
| 3854 | delete IBI; |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3855 | return Error("Invalid record"); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3856 | } |
| 3857 | } |
| 3858 | I = IBI; |
| 3859 | break; |
| 3860 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3861 | |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3862 | case bitc::FUNC_CODE_INST_INVOKE: { |
| 3863 | // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...] |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3864 | if (Record.size() < 4) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3865 | return Error("Invalid record"); |
David Blaikie | 5ea1f7b | 2015-04-24 18:06:06 +0000 | [diff] [blame^] | 3866 | unsigned OpNum = 0; |
| 3867 | AttributeSet PAL = getAttributes(Record[OpNum++]); |
| 3868 | unsigned CCInfo = Record[OpNum++]; |
| 3869 | BasicBlock *NormalBB = getBasicBlock(Record[OpNum++]); |
| 3870 | BasicBlock *UnwindBB = getBasicBlock(Record[OpNum++]); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3871 | |
David Blaikie | 5ea1f7b | 2015-04-24 18:06:06 +0000 | [diff] [blame^] | 3872 | FunctionType *FTy = nullptr; |
| 3873 | if (CCInfo >> 13 & 1 && |
| 3874 | !(FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++])))) |
| 3875 | return Error("Explicit invoke type is not a function type"); |
| 3876 | |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 3877 | Value *Callee; |
| 3878 | if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3879 | return Error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3880 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3881 | PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType()); |
David Blaikie | 5ea1f7b | 2015-04-24 18:06:06 +0000 | [diff] [blame^] | 3882 | if (!CalleeTy) |
| 3883 | return Error("Callee is not a pointer"); |
| 3884 | if (!FTy) { |
| 3885 | FTy = dyn_cast<FunctionType>(CalleeTy->getElementType()); |
| 3886 | if (!FTy) |
| 3887 | return Error("Callee is not of pointer to function type"); |
| 3888 | } else if (CalleeTy->getElementType() != FTy) |
| 3889 | return Error("Explicit invoke type does not match pointee type of " |
| 3890 | "callee operand"); |
| 3891 | if (Record.size() < FTy->getNumParams() + OpNum) |
| 3892 | return Error("Insufficient operands to call"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3893 | |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3894 | SmallVector<Value*, 16> Ops; |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 3895 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3896 | Ops.push_back(getValue(Record, OpNum, NextValueNo, |
| 3897 | FTy->getParamType(i))); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3898 | if (!Ops.back()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3899 | return Error("Invalid record"); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3900 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3901 | |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 3902 | if (!FTy->isVarArg()) { |
| 3903 | if (Record.size() != OpNum) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3904 | return Error("Invalid record"); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3905 | } else { |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 3906 | // Read type/value pairs for varargs params. |
| 3907 | while (OpNum != Record.size()) { |
| 3908 | Value *Op; |
| 3909 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3910 | return Error("Invalid record"); |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 3911 | Ops.push_back(Op); |
| 3912 | } |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3913 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3914 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 3915 | I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3916 | InstructionList.push_back(I); |
David Blaikie | 5ea1f7b | 2015-04-24 18:06:06 +0000 | [diff] [blame^] | 3917 | cast<InvokeInst>(I) |
| 3918 | ->setCallingConv(static_cast<CallingConv::ID>(~(1U << 13) & CCInfo)); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 3919 | cast<InvokeInst>(I)->setAttributes(PAL); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3920 | break; |
| 3921 | } |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 3922 | case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval] |
| 3923 | unsigned Idx = 0; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3924 | Value *Val = nullptr; |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 3925 | if (getValueTypePair(Record, Idx, NextValueNo, Val)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3926 | return Error("Invalid record"); |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 3927 | I = ResumeInst::Create(Val); |
Bill Wendling | b9a8999 | 2011-09-01 00:50:20 +0000 | [diff] [blame] | 3928 | InstructionList.push_back(I); |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 3929 | break; |
| 3930 | } |
Chris Lattner | e53603e | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 3931 | case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3932 | I = new UnreachableInst(Context); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3933 | InstructionList.push_back(I); |
Chris Lattner | e53603e | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 3934 | break; |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 3935 | case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...] |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 3936 | if (Record.size() < 1 || ((Record.size()-1)&1)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3937 | return Error("Invalid record"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3938 | Type *Ty = getTypeByID(Record[0]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3939 | if (!Ty) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3940 | return Error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3941 | |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 3942 | PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3943 | InstructionList.push_back(PN); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3944 | |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 3945 | 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] | 3946 | Value *V; |
| 3947 | // With the new function encoding, it is possible that operands have |
| 3948 | // negative IDs (for forward references). Use a signed VBR |
| 3949 | // representation to keep the encoding small. |
| 3950 | if (UseRelativeIDs) |
| 3951 | V = getValueSigned(Record, 1+i, NextValueNo, Ty); |
| 3952 | else |
| 3953 | V = getValue(Record, 1+i, NextValueNo, Ty); |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 3954 | BasicBlock *BB = getBasicBlock(Record[2+i]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3955 | if (!V || !BB) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3956 | return Error("Invalid record"); |
Chris Lattner | c332bba | 2007-05-03 18:58:09 +0000 | [diff] [blame] | 3957 | PN->addIncoming(V, BB); |
| 3958 | } |
| 3959 | I = PN; |
| 3960 | break; |
| 3961 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3962 | |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 3963 | case bitc::FUNC_CODE_INST_LANDINGPAD: { |
| 3964 | // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?] |
| 3965 | unsigned Idx = 0; |
| 3966 | if (Record.size() < 4) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3967 | return Error("Invalid record"); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 3968 | Type *Ty = getTypeByID(Record[Idx++]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3969 | if (!Ty) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3970 | return Error("Invalid record"); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3971 | Value *PersFn = nullptr; |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 3972 | if (getValueTypePair(Record, Idx, NextValueNo, PersFn)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3973 | return Error("Invalid record"); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 3974 | |
| 3975 | bool IsCleanup = !!Record[Idx++]; |
| 3976 | unsigned NumClauses = Record[Idx++]; |
| 3977 | LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, NumClauses); |
| 3978 | LP->setCleanup(IsCleanup); |
| 3979 | for (unsigned J = 0; J != NumClauses; ++J) { |
| 3980 | LandingPadInst::ClauseType CT = |
| 3981 | LandingPadInst::ClauseType(Record[Idx++]); (void)CT; |
| 3982 | Value *Val; |
| 3983 | |
| 3984 | if (getValueTypePair(Record, Idx, NextValueNo, Val)) { |
| 3985 | delete LP; |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 3986 | return Error("Invalid record"); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 3987 | } |
| 3988 | |
| 3989 | assert((CT != LandingPadInst::Catch || |
| 3990 | !isa<ArrayType>(Val->getType())) && |
| 3991 | "Catch clause has a invalid type!"); |
| 3992 | assert((CT != LandingPadInst::Filter || |
| 3993 | isa<ArrayType>(Val->getType())) && |
| 3994 | "Filter clause has invalid type!"); |
Rafael Espindola | 4dc5dfc | 2014-06-04 18:51:31 +0000 | [diff] [blame] | 3995 | LP->addClause(cast<Constant>(Val)); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 3996 | } |
| 3997 | |
| 3998 | I = LP; |
Bill Wendling | b9a8999 | 2011-09-01 00:50:20 +0000 | [diff] [blame] | 3999 | InstructionList.push_back(I); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4000 | break; |
| 4001 | } |
| 4002 | |
Chris Lattner | f1c8710 | 2011-06-17 18:09:11 +0000 | [diff] [blame] | 4003 | case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align] |
| 4004 | if (Record.size() != 4) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4005 | return Error("Invalid record"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 4006 | PointerType *Ty = |
Chris Lattner | c332bba | 2007-05-03 18:58:09 +0000 | [diff] [blame] | 4007 | dyn_cast_or_null<PointerType>(getTypeByID(Record[0])); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 4008 | Type *OpTy = getTypeByID(Record[1]); |
Chris Lattner | f1c8710 | 2011-06-17 18:09:11 +0000 | [diff] [blame] | 4009 | Value *Size = getFnValueByID(Record[2], OpTy); |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 4010 | uint64_t AlignRecord = Record[3]; |
| 4011 | const uint64_t InAllocaMask = uint64_t(1) << 5; |
| 4012 | bool InAlloca = AlignRecord & InAllocaMask; |
| 4013 | unsigned Align; |
| 4014 | if (std::error_code EC = |
| 4015 | parseAlignmentValue(AlignRecord & ~InAllocaMask, Align)) { |
| 4016 | return EC; |
| 4017 | } |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 4018 | if (!Ty || !Size) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4019 | return Error("Invalid record"); |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 4020 | AllocaInst *AI = new AllocaInst(Ty->getElementType(), Size, Align); |
Reid Kleckner | 56b56ea | 2014-07-16 01:34:27 +0000 | [diff] [blame] | 4021 | AI->setUsedWithInAlloca(InAlloca); |
| 4022 | I = AI; |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4023 | InstructionList.push_back(I); |
Chris Lattner | c332bba | 2007-05-03 18:58:09 +0000 | [diff] [blame] | 4024 | break; |
| 4025 | } |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 4026 | case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol] |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 4027 | unsigned OpNum = 0; |
| 4028 | Value *Op; |
| 4029 | if (getValueTypePair(Record, OpNum, NextValueNo, Op) || |
David Blaikie | 8503565 | 2015-02-25 01:07:20 +0000 | [diff] [blame] | 4030 | (OpNum + 2 != Record.size() && OpNum + 3 != Record.size())) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4031 | return Error("Invalid record"); |
David Blaikie | 8503565 | 2015-02-25 01:07:20 +0000 | [diff] [blame] | 4032 | |
| 4033 | Type *Ty = nullptr; |
| 4034 | if (OpNum + 3 == Record.size()) |
| 4035 | Ty = getTypeByID(Record[OpNum++]); |
David Blaikie | b7a02987 | 2015-04-17 19:56:21 +0000 | [diff] [blame] | 4036 | if (!Ty) |
| 4037 | Ty = cast<PointerType>(Op->getType())->getElementType(); |
| 4038 | else if (Ty != cast<PointerType>(Op->getType())->getElementType()) |
| 4039 | return Error("Explicit load type does not match pointee type of " |
| 4040 | "pointer operand"); |
David Blaikie | 8503565 | 2015-02-25 01:07:20 +0000 | [diff] [blame] | 4041 | |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 4042 | unsigned Align; |
| 4043 | if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align)) |
| 4044 | return EC; |
David Blaikie | b7a02987 | 2015-04-17 19:56:21 +0000 | [diff] [blame] | 4045 | I = new LoadInst(Ty, Op, "", Record[OpNum + 1], Align); |
David Blaikie | 8503565 | 2015-02-25 01:07:20 +0000 | [diff] [blame] | 4046 | |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4047 | InstructionList.push_back(I); |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4048 | break; |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 4049 | } |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4050 | case bitc::FUNC_CODE_INST_LOADATOMIC: { |
| 4051 | // LOADATOMIC: [opty, op, align, vol, ordering, synchscope] |
| 4052 | unsigned OpNum = 0; |
| 4053 | Value *Op; |
| 4054 | if (getValueTypePair(Record, OpNum, NextValueNo, Op) || |
David Blaikie | 8503565 | 2015-02-25 01:07:20 +0000 | [diff] [blame] | 4055 | (OpNum + 4 != Record.size() && OpNum + 5 != Record.size())) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4056 | return Error("Invalid record"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4057 | |
David Blaikie | 8503565 | 2015-02-25 01:07:20 +0000 | [diff] [blame] | 4058 | Type *Ty = nullptr; |
| 4059 | if (OpNum + 5 == Record.size()) |
| 4060 | Ty = getTypeByID(Record[OpNum++]); |
| 4061 | |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4062 | AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]); |
| 4063 | if (Ordering == NotAtomic || Ordering == Release || |
| 4064 | Ordering == AcquireRelease) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4065 | return Error("Invalid record"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4066 | if (Ordering != NotAtomic && Record[OpNum] == 0) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4067 | return Error("Invalid record"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4068 | SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]); |
| 4069 | |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 4070 | unsigned Align; |
| 4071 | if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align)) |
| 4072 | return EC; |
| 4073 | I = new LoadInst(Op, "", Record[OpNum+1], Align, Ordering, SynchScope); |
David Blaikie | 8503565 | 2015-02-25 01:07:20 +0000 | [diff] [blame] | 4074 | |
Yaron Keren | d602c35 | 2015-02-28 15:29:17 +0000 | [diff] [blame] | 4075 | (void)Ty; |
David Blaikie | 8503565 | 2015-02-25 01:07:20 +0000 | [diff] [blame] | 4076 | assert((!Ty || Ty == I->getType()) && |
| 4077 | "Explicit type doesn't match pointee type of the first operand"); |
| 4078 | |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4079 | InstructionList.push_back(I); |
| 4080 | break; |
| 4081 | } |
David Blaikie | 612ddbf | 2015-04-22 04:14:42 +0000 | [diff] [blame] | 4082 | case bitc::FUNC_CODE_INST_STORE: |
| 4083 | 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] | 4084 | unsigned OpNum = 0; |
| 4085 | Value *Val, *Ptr; |
| 4086 | if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || |
David Blaikie | 612ddbf | 2015-04-22 04:14:42 +0000 | [diff] [blame] | 4087 | (BitCode == bitc::FUNC_CODE_INST_STORE |
| 4088 | ? getValueTypePair(Record, OpNum, NextValueNo, Val) |
| 4089 | : popValue(Record, OpNum, NextValueNo, |
| 4090 | cast<PointerType>(Ptr->getType())->getElementType(), |
| 4091 | Val)) || |
| 4092 | OpNum + 2 != Record.size()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4093 | return Error("Invalid record"); |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 4094 | unsigned Align; |
| 4095 | if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align)) |
| 4096 | return EC; |
| 4097 | I = new StoreInst(Val, Ptr, Record[OpNum+1], Align); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4098 | InstructionList.push_back(I); |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 4099 | break; |
| 4100 | } |
David Blaikie | 50a0615 | 2015-04-22 04:14:46 +0000 | [diff] [blame] | 4101 | case bitc::FUNC_CODE_INST_STOREATOMIC: |
| 4102 | case bitc::FUNC_CODE_INST_STOREATOMIC_OLD: { |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4103 | // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, synchscope] |
| 4104 | unsigned OpNum = 0; |
| 4105 | Value *Val, *Ptr; |
| 4106 | if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || |
David Blaikie | 50a0615 | 2015-04-22 04:14:46 +0000 | [diff] [blame] | 4107 | (BitCode == bitc::FUNC_CODE_INST_STOREATOMIC |
| 4108 | ? getValueTypePair(Record, OpNum, NextValueNo, Val) |
| 4109 | : popValue(Record, OpNum, NextValueNo, |
| 4110 | cast<PointerType>(Ptr->getType())->getElementType(), |
| 4111 | Val)) || |
| 4112 | OpNum + 4 != Record.size()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4113 | return Error("Invalid record"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4114 | |
| 4115 | AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]); |
Eli Friedman | 222b5a4 | 2011-09-19 19:41:28 +0000 | [diff] [blame] | 4116 | if (Ordering == NotAtomic || Ordering == Acquire || |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4117 | Ordering == AcquireRelease) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4118 | return Error("Invalid record"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4119 | SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]); |
| 4120 | if (Ordering != NotAtomic && Record[OpNum] == 0) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4121 | return Error("Invalid record"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4122 | |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 4123 | unsigned Align; |
| 4124 | if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align)) |
| 4125 | return EC; |
| 4126 | I = new StoreInst(Val, Ptr, Record[OpNum+1], Align, Ordering, SynchScope); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4127 | InstructionList.push_back(I); |
| 4128 | break; |
| 4129 | } |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4130 | case bitc::FUNC_CODE_INST_CMPXCHG: { |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 4131 | // CMPXCHG:[ptrty, ptr, cmp, new, vol, successordering, synchscope, |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 4132 | // failureordering?, isweak?] |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4133 | unsigned OpNum = 0; |
| 4134 | Value *Ptr, *Cmp, *New; |
| 4135 | if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 4136 | popValue(Record, OpNum, NextValueNo, |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4137 | cast<PointerType>(Ptr->getType())->getElementType(), Cmp) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 4138 | popValue(Record, OpNum, NextValueNo, |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4139 | cast<PointerType>(Ptr->getType())->getElementType(), New) || |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 4140 | (Record.size() < OpNum + 3 || Record.size() > OpNum + 5)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4141 | return Error("Invalid record"); |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 4142 | AtomicOrdering SuccessOrdering = GetDecodedOrdering(Record[OpNum+1]); |
| 4143 | if (SuccessOrdering == NotAtomic || SuccessOrdering == Unordered) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4144 | return Error("Invalid record"); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4145 | SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+2]); |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 4146 | |
| 4147 | AtomicOrdering FailureOrdering; |
| 4148 | if (Record.size() < 7) |
| 4149 | FailureOrdering = |
| 4150 | AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering); |
| 4151 | else |
| 4152 | FailureOrdering = GetDecodedOrdering(Record[OpNum+3]); |
| 4153 | |
| 4154 | I = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering, FailureOrdering, |
| 4155 | SynchScope); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4156 | cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]); |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 4157 | |
| 4158 | if (Record.size() < 8) { |
| 4159 | // Before weak cmpxchgs existed, the instruction simply returned the |
| 4160 | // value loaded from memory, so bitcode files from that era will be |
| 4161 | // expecting the first component of a modern cmpxchg. |
| 4162 | CurBB->getInstList().push_back(I); |
| 4163 | I = ExtractValueInst::Create(I, 0); |
| 4164 | } else { |
| 4165 | cast<AtomicCmpXchgInst>(I)->setWeak(Record[OpNum+4]); |
| 4166 | } |
| 4167 | |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4168 | InstructionList.push_back(I); |
| 4169 | break; |
| 4170 | } |
| 4171 | case bitc::FUNC_CODE_INST_ATOMICRMW: { |
| 4172 | // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, synchscope] |
| 4173 | unsigned OpNum = 0; |
| 4174 | Value *Ptr, *Val; |
| 4175 | if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 4176 | popValue(Record, OpNum, NextValueNo, |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4177 | cast<PointerType>(Ptr->getType())->getElementType(), Val) || |
| 4178 | OpNum+4 != Record.size()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4179 | return Error("Invalid record"); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4180 | AtomicRMWInst::BinOp Operation = GetDecodedRMWOperation(Record[OpNum]); |
| 4181 | if (Operation < AtomicRMWInst::FIRST_BINOP || |
| 4182 | Operation > AtomicRMWInst::LAST_BINOP) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4183 | return Error("Invalid record"); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4184 | AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4185 | if (Ordering == NotAtomic || Ordering == Unordered) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4186 | return Error("Invalid record"); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4187 | SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]); |
| 4188 | I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SynchScope); |
| 4189 | cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]); |
| 4190 | InstructionList.push_back(I); |
| 4191 | break; |
| 4192 | } |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 4193 | case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, synchscope] |
| 4194 | if (2 != Record.size()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4195 | return Error("Invalid record"); |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 4196 | AtomicOrdering Ordering = GetDecodedOrdering(Record[0]); |
| 4197 | if (Ordering == NotAtomic || Ordering == Unordered || |
| 4198 | Ordering == Monotonic) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4199 | return Error("Invalid record"); |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 4200 | SynchronizationScope SynchScope = GetDecodedSynchScope(Record[1]); |
| 4201 | I = new FenceInst(Context, Ordering, SynchScope); |
| 4202 | InstructionList.push_back(I); |
| 4203 | break; |
| 4204 | } |
Chris Lattner | c4407080 | 2011-06-17 18:17:37 +0000 | [diff] [blame] | 4205 | case bitc::FUNC_CODE_INST_CALL: { |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 4206 | // CALL: [paramattrs, cc, fnty, fnid, arg0, arg1...] |
| 4207 | if (Record.size() < 3) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4208 | return Error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4209 | |
David Blaikie | dbe6e0f | 2015-04-17 06:40:14 +0000 | [diff] [blame] | 4210 | unsigned OpNum = 0; |
| 4211 | AttributeSet PAL = getAttributes(Record[OpNum++]); |
| 4212 | unsigned CCInfo = Record[OpNum++]; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4213 | |
David Blaikie | dbe6e0f | 2015-04-17 06:40:14 +0000 | [diff] [blame] | 4214 | FunctionType *FTy = nullptr; |
| 4215 | if (CCInfo >> 15 & 1 && |
| 4216 | !(FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++])))) |
| 4217 | return Error("Explicit call type is not a function type"); |
| 4218 | |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 4219 | Value *Callee; |
| 4220 | if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4221 | return Error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4222 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 4223 | PointerType *OpTy = dyn_cast<PointerType>(Callee->getType()); |
David Blaikie | dbe6e0f | 2015-04-17 06:40:14 +0000 | [diff] [blame] | 4224 | if (!OpTy) |
| 4225 | return Error("Callee is not a pointer type"); |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 4226 | if (!FTy) { |
| 4227 | FTy = dyn_cast<FunctionType>(OpTy->getElementType()); |
| 4228 | if (!FTy) |
| 4229 | return Error("Callee is not of pointer to function type"); |
| 4230 | } else if (OpTy->getElementType() != FTy) |
David Blaikie | dbe6e0f | 2015-04-17 06:40:14 +0000 | [diff] [blame] | 4231 | return Error("Explicit call type does not match pointee type of " |
| 4232 | "callee operand"); |
| 4233 | if (Record.size() < FTy->getNumParams() + OpNum) |
| 4234 | return Error("Insufficient operands to call"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4235 | |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 4236 | SmallVector<Value*, 16> Args; |
| 4237 | // Read the fixed params. |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 4238 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 4239 | if (FTy->getParamType(i)->isLabelTy()) |
Dale Johannesen | 4646aa3 | 2007-11-05 21:20:28 +0000 | [diff] [blame] | 4240 | Args.push_back(getBasicBlock(Record[OpNum])); |
Dan Gohman | bbcd04d | 2010-09-13 18:00:48 +0000 | [diff] [blame] | 4241 | else |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 4242 | Args.push_back(getValue(Record, OpNum, NextValueNo, |
| 4243 | FTy->getParamType(i))); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4244 | if (!Args.back()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4245 | return Error("Invalid record"); |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 4246 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4247 | |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 4248 | // Read type/value pairs for varargs params. |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 4249 | if (!FTy->isVarArg()) { |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 4250 | if (OpNum != Record.size()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4251 | return Error("Invalid record"); |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 4252 | } else { |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 4253 | while (OpNum != Record.size()) { |
| 4254 | Value *Op; |
| 4255 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4256 | return Error("Invalid record"); |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 4257 | Args.push_back(Op); |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 4258 | } |
| 4259 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4260 | |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 4261 | I = CallInst::Create(FTy, Callee, Args); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4262 | InstructionList.push_back(I); |
Sandeep Patel | 68c5f47 | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 4263 | cast<CallInst>(I)->setCallingConv( |
Reid Kleckner | 5772b77 | 2014-04-24 20:14:34 +0000 | [diff] [blame] | 4264 | static_cast<CallingConv::ID>((~(1U << 14) & CCInfo) >> 1)); |
| 4265 | CallInst::TailCallKind TCK = CallInst::TCK_None; |
| 4266 | if (CCInfo & 1) |
| 4267 | TCK = CallInst::TCK_Tail; |
| 4268 | if (CCInfo & (1 << 14)) |
| 4269 | TCK = CallInst::TCK_MustTail; |
| 4270 | cast<CallInst>(I)->setTailCallKind(TCK); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 4271 | cast<CallInst>(I)->setAttributes(PAL); |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 4272 | break; |
| 4273 | } |
| 4274 | case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty] |
| 4275 | if (Record.size() < 3) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4276 | return Error("Invalid record"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 4277 | Type *OpTy = getTypeByID(Record[0]); |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 4278 | Value *Op = getValue(Record, 1, NextValueNo, OpTy); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 4279 | Type *ResTy = getTypeByID(Record[2]); |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 4280 | if (!OpTy || !Op || !ResTy) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4281 | return Error("Invalid record"); |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 4282 | I = new VAArgInst(Op, ResTy); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4283 | InstructionList.push_back(I); |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 4284 | break; |
| 4285 | } |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4286 | } |
| 4287 | |
| 4288 | // Add instruction to end of current BB. If there is no current BB, reject |
| 4289 | // this file. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4290 | if (!CurBB) { |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4291 | delete I; |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4292 | return Error("Invalid instruction with no BB"); |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4293 | } |
| 4294 | CurBB->getInstList().push_back(I); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4295 | |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4296 | // If this was a terminator instruction, move to the next block. |
| 4297 | if (isa<TerminatorInst>(I)) { |
| 4298 | ++CurBBNo; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4299 | CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr; |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4300 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4301 | |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4302 | // Non-void values get registered in the value table for future use. |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 4303 | if (I && !I->getType()->isVoidTy()) |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4304 | ValueList.AssignValue(I, NextValueNo++); |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 4305 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4306 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 4307 | OutOfRecordLoop: |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 4308 | |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4309 | // Check the function list for unresolved values. |
| 4310 | if (Argument *A = dyn_cast<Argument>(ValueList.back())) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4311 | if (!A->getParent()) { |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4312 | // We found at least one unresolved value. Nuke them all to avoid leaks. |
| 4313 | for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){ |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4314 | if ((A = dyn_cast_or_null<Argument>(ValueList[i])) && !A->getParent()) { |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 4315 | A->replaceAllUsesWith(UndefValue::get(A->getType())); |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4316 | delete A; |
| 4317 | } |
| 4318 | } |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4319 | return Error("Never resolved value found in function"); |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4320 | } |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4321 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4322 | |
Dan Gohman | 9b9ff46 | 2010-08-25 20:23:38 +0000 | [diff] [blame] | 4323 | // FIXME: Check for unresolved forward-declared metadata references |
| 4324 | // and clean up leaks. |
| 4325 | |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 4326 | // Trim the value list down to the size it was before we parsed this function. |
| 4327 | ValueList.shrinkTo(ModuleValueListSize); |
Dan Gohman | 26d837d | 2010-08-25 20:22:53 +0000 | [diff] [blame] | 4328 | MDValueList.shrinkTo(ModuleMDValueListSize); |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 4329 | std::vector<BasicBlock*>().swap(FunctionBBs); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 4330 | return std::error_code(); |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 4331 | } |
| 4332 | |
Rafael Espindola | 7d71203 | 2013-11-05 17:16:08 +0000 | [diff] [blame] | 4333 | /// Find the function body in the bitcode stream |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 4334 | std::error_code BitcodeReader::FindFunctionInStream( |
| 4335 | Function *F, |
| 4336 | DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) { |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 4337 | while (DeferredFunctionInfoIterator->second == 0) { |
| 4338 | if (Stream.AtEndOfStream()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4339 | return Error("Could not find function in stream"); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 4340 | // ParseModule will parse the next body in the stream and set its |
| 4341 | // position in the DeferredFunctionInfo map. |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 4342 | if (std::error_code EC = ParseModule(true)) |
Rafael Espindola | 7d71203 | 2013-11-05 17:16:08 +0000 | [diff] [blame] | 4343 | return EC; |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 4344 | } |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 4345 | return std::error_code(); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 4346 | } |
| 4347 | |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 4348 | //===----------------------------------------------------------------------===// |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 4349 | // GVMaterializer implementation |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 4350 | //===----------------------------------------------------------------------===// |
| 4351 | |
Rafael Espindola | c3f9b5a | 2014-06-23 21:53:12 +0000 | [diff] [blame] | 4352 | void BitcodeReader::releaseBuffer() { Buffer.release(); } |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 4353 | |
Rafael Espindola | 5a52e6d | 2014-10-24 22:50:48 +0000 | [diff] [blame] | 4354 | std::error_code BitcodeReader::materialize(GlobalValue *GV) { |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 4355 | if (std::error_code EC = materializeMetadata()) |
| 4356 | return EC; |
| 4357 | |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 4358 | Function *F = dyn_cast<Function>(GV); |
| 4359 | // 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] | 4360 | if (!F || !F->isMaterializable()) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 4361 | return std::error_code(); |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 4362 | |
| 4363 | DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F); |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 4364 | assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!"); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 4365 | // If its position is recorded as 0, its body is somewhere in the stream |
| 4366 | // but we haven't seen it yet. |
Rafael Espindola | 2b11ad4 | 2013-11-05 19:36:34 +0000 | [diff] [blame] | 4367 | if (DFII->second == 0 && LazyStreamer) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 4368 | if (std::error_code EC = FindFunctionInStream(F, DFII)) |
Rafael Espindola | 2b11ad4 | 2013-11-05 19:36:34 +0000 | [diff] [blame] | 4369 | return EC; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4370 | |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 4371 | // Move the bit stream to the saved position of the deferred function body. |
| 4372 | Stream.JumpToBit(DFII->second); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4373 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 4374 | if (std::error_code EC = ParseFunctionBody(F)) |
Rafael Espindola | 2b11ad4 | 2013-11-05 19:36:34 +0000 | [diff] [blame] | 4375 | return EC; |
Rafael Espindola | d4bcefc | 2014-10-24 18:13:04 +0000 | [diff] [blame] | 4376 | F->setIsMaterializable(false); |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 4377 | |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 4378 | if (StripDebugInfo) |
| 4379 | stripDebugInfo(*F); |
| 4380 | |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 4381 | // Upgrade any old intrinsic calls in the function. |
| 4382 | for (UpgradedIntrinsicMap::iterator I = UpgradedIntrinsics.begin(), |
| 4383 | E = UpgradedIntrinsics.end(); I != E; ++I) { |
| 4384 | if (I->first != I->second) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 4385 | for (auto UI = I->first->user_begin(), UE = I->first->user_end(); |
| 4386 | UI != UE;) { |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 4387 | if (CallInst* CI = dyn_cast<CallInst>(*UI++)) |
| 4388 | UpgradeIntrinsicCall(CI, I->second); |
| 4389 | } |
| 4390 | } |
| 4391 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4392 | |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 4393 | // Bring in any functions that this function forward-referenced via |
| 4394 | // blockaddresses. |
| 4395 | return materializeForwardReferencedFunctions(); |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 4396 | } |
| 4397 | |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 4398 | bool BitcodeReader::isDematerializable(const GlobalValue *GV) const { |
| 4399 | const Function *F = dyn_cast<Function>(GV); |
| 4400 | if (!F || F->isDeclaration()) |
| 4401 | return false; |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 4402 | |
| 4403 | // Dematerializing F would leave dangling references that wouldn't be |
| 4404 | // reconnected on re-materialization. |
| 4405 | if (BlockAddressesTaken.count(F)) |
| 4406 | return false; |
| 4407 | |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 4408 | return DeferredFunctionInfo.count(const_cast<Function*>(F)); |
| 4409 | } |
| 4410 | |
| 4411 | void BitcodeReader::Dematerialize(GlobalValue *GV) { |
| 4412 | Function *F = dyn_cast<Function>(GV); |
| 4413 | // If this function isn't dematerializable, this is a noop. |
| 4414 | if (!F || !isDematerializable(F)) |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 4415 | return; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4416 | |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 4417 | assert(DeferredFunctionInfo.count(F) && "No info to read function later?"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4418 | |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 4419 | // Just forget the function body, we can remat it later. |
Petar Jovanovic | 7480e4d | 2014-09-23 12:54:19 +0000 | [diff] [blame] | 4420 | F->dropAllReferences(); |
Rafael Espindola | d4bcefc | 2014-10-24 18:13:04 +0000 | [diff] [blame] | 4421 | F->setIsMaterializable(true); |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 4422 | } |
| 4423 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 4424 | std::error_code BitcodeReader::MaterializeModule(Module *M) { |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 4425 | assert(M == TheModule && |
| 4426 | "Can only Materialize the Module this BitcodeReader is attached to."); |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 4427 | |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 4428 | if (std::error_code EC = materializeMetadata()) |
| 4429 | return EC; |
| 4430 | |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 4431 | // Promise to materialize all forward references. |
| 4432 | WillMaterializeAllForwardRefs = true; |
| 4433 | |
Chris Lattner | 06310bf | 2009-06-16 05:15:21 +0000 | [diff] [blame] | 4434 | // Iterate over the module, deserializing any functions that are still on |
| 4435 | // disk. |
| 4436 | for (Module::iterator F = TheModule->begin(), E = TheModule->end(); |
Rafael Espindola | 2b11ad4 | 2013-11-05 19:36:34 +0000 | [diff] [blame] | 4437 | F != E; ++F) { |
Rafael Espindola | 246c4fb | 2014-11-01 16:46:18 +0000 | [diff] [blame] | 4438 | if (std::error_code EC = materialize(F)) |
| 4439 | return EC; |
Rafael Espindola | 2b11ad4 | 2013-11-05 19:36:34 +0000 | [diff] [blame] | 4440 | } |
Derek Schuff | 92ef975 | 2012-02-29 00:07:09 +0000 | [diff] [blame] | 4441 | // At this point, if there are any function bodies, the current bit is |
| 4442 | // pointing to the END_BLOCK record after them. Now make sure the rest |
| 4443 | // of the bits in the module have been read. |
| 4444 | if (NextUnreadBit) |
| 4445 | ParseModule(true); |
| 4446 | |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 4447 | // Check that all block address forward references got resolved (as we |
| 4448 | // promised above). |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 4449 | if (!BasicBlockFwdRefs.empty()) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4450 | return Error("Never resolved function from blockaddress"); |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 4451 | |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4452 | // Upgrade any intrinsic calls that slipped through (should not happen!) and |
| 4453 | // delete the old functions to clean up. We can't do this unless the entire |
| 4454 | // module is materialized because there could always be another function body |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 4455 | // with calls to the old function. |
| 4456 | for (std::vector<std::pair<Function*, Function*> >::iterator I = |
| 4457 | UpgradedIntrinsics.begin(), E = UpgradedIntrinsics.end(); I != E; ++I) { |
| 4458 | if (I->first != I->second) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 4459 | for (auto UI = I->first->user_begin(), UE = I->first->user_end(); |
| 4460 | UI != UE;) { |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 4461 | if (CallInst* CI = dyn_cast<CallInst>(*UI++)) |
| 4462 | UpgradeIntrinsicCall(CI, I->second); |
| 4463 | } |
Chris Lattner | 647cffb | 2009-04-01 01:43:03 +0000 | [diff] [blame] | 4464 | if (!I->first->use_empty()) |
| 4465 | I->first->replaceAllUsesWith(I->second); |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 4466 | I->first->eraseFromParent(); |
| 4467 | } |
| 4468 | } |
| 4469 | std::vector<std::pair<Function*, Function*> >().swap(UpgradedIntrinsics); |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 4470 | |
Manman Ren | 209b17c | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 4471 | for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++) |
| 4472 | UpgradeInstWithTBAATag(InstsWithTBAATag[I]); |
| 4473 | |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 4474 | UpgradeDebugInfo(*M); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 4475 | return std::error_code(); |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 4476 | } |
| 4477 | |
Rafael Espindola | 2fa1e43 | 2014-12-03 07:18:23 +0000 | [diff] [blame] | 4478 | std::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const { |
| 4479 | return IdentifiedStructTypes; |
| 4480 | } |
| 4481 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 4482 | std::error_code BitcodeReader::InitStream() { |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 4483 | if (LazyStreamer) |
| 4484 | return InitLazyStream(); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 4485 | return InitStreamFromBuffer(); |
| 4486 | } |
| 4487 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 4488 | std::error_code BitcodeReader::InitStreamFromBuffer() { |
Roman Divacky | 4717a8d | 2012-09-06 15:42:13 +0000 | [diff] [blame] | 4489 | const unsigned char *BufPtr = (const unsigned char*)Buffer->getBufferStart(); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 4490 | const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize(); |
| 4491 | |
Rafael Espindola | 2743525 | 2014-07-29 21:01:24 +0000 | [diff] [blame] | 4492 | if (Buffer->getBufferSize() & 3) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4493 | return Error("Invalid bitcode signature"); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 4494 | |
| 4495 | // If we have a wrapper header, parse it and ignore the non-bc file contents. |
| 4496 | // The magic number is 0x0B17C0DE stored in little endian. |
| 4497 | if (isBitcodeWrapper(BufPtr, BufEnd)) |
| 4498 | if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4499 | return Error("Invalid bitcode wrapper header"); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 4500 | |
| 4501 | StreamFile.reset(new BitstreamReader(BufPtr, BufEnd)); |
Rafael Espindola | de1e5b8 | 2014-11-12 14:48:38 +0000 | [diff] [blame] | 4502 | Stream.init(&*StreamFile); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 4503 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 4504 | return std::error_code(); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 4505 | } |
| 4506 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 4507 | std::error_code BitcodeReader::InitLazyStream() { |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 4508 | // Check and strip off the bitcode wrapper; BitstreamReader expects never to |
| 4509 | // see it. |
Yaron Keren | 06d6930 | 2014-12-18 10:03:35 +0000 | [diff] [blame] | 4510 | auto OwnedBytes = llvm::make_unique<StreamingMemoryObject>(LazyStreamer); |
Rafael Espindola | 7d727b5 | 2014-12-18 05:08:43 +0000 | [diff] [blame] | 4511 | StreamingMemoryObject &Bytes = *OwnedBytes; |
Yaron Keren | 06d6930 | 2014-12-18 10:03:35 +0000 | [diff] [blame] | 4512 | StreamFile = llvm::make_unique<BitstreamReader>(std::move(OwnedBytes)); |
Rafael Espindola | de1e5b8 | 2014-11-12 14:48:38 +0000 | [diff] [blame] | 4513 | Stream.init(&*StreamFile); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 4514 | |
| 4515 | unsigned char buf[16]; |
Rafael Espindola | 7d727b5 | 2014-12-18 05:08:43 +0000 | [diff] [blame] | 4516 | if (Bytes.readBytes(buf, 16, 0) != 16) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4517 | return Error("Invalid bitcode signature"); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 4518 | |
| 4519 | if (!isBitcode(buf, buf + 16)) |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4520 | return Error("Invalid bitcode signature"); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 4521 | |
| 4522 | if (isBitcodeWrapper(buf, buf + 4)) { |
| 4523 | const unsigned char *bitcodeStart = buf; |
| 4524 | const unsigned char *bitcodeEnd = buf + 16; |
| 4525 | SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false); |
Rafael Espindola | 7d727b5 | 2014-12-18 05:08:43 +0000 | [diff] [blame] | 4526 | Bytes.dropLeadingBytes(bitcodeStart - buf); |
| 4527 | Bytes.setKnownObjectSize(bitcodeEnd - bitcodeStart); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 4528 | } |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 4529 | return std::error_code(); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 4530 | } |
| 4531 | |
| 4532 | namespace { |
Rafael Espindola | 25188c9 | 2014-06-12 01:45:43 +0000 | [diff] [blame] | 4533 | class BitcodeErrorCategoryType : public std::error_category { |
Rafael Espindola | f5d07fa | 2014-06-10 21:26:47 +0000 | [diff] [blame] | 4534 | const char *name() const LLVM_NOEXCEPT override { |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 4535 | return "llvm.bitcode"; |
| 4536 | } |
Craig Topper | 7315602 | 2014-03-02 09:09:27 +0000 | [diff] [blame] | 4537 | std::string message(int IE) const override { |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 4538 | BitcodeError E = static_cast<BitcodeError>(IE); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 4539 | switch (E) { |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 4540 | case BitcodeError::InvalidBitcodeSignature: |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 4541 | return "Invalid bitcode signature"; |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4542 | case BitcodeError::CorruptedBitcode: |
| 4543 | return "Corrupted bitcode"; |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 4544 | } |
Benjamin Kramer | 77db163 | 2013-11-05 13:45:09 +0000 | [diff] [blame] | 4545 | llvm_unreachable("Unknown error type!"); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 4546 | } |
| 4547 | }; |
| 4548 | } |
| 4549 | |
Chris Bieneman | 770163e | 2014-09-19 20:29:02 +0000 | [diff] [blame] | 4550 | static ManagedStatic<BitcodeErrorCategoryType> ErrorCategory; |
| 4551 | |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 4552 | const std::error_category &llvm::BitcodeErrorCategory() { |
Chris Bieneman | 770163e | 2014-09-19 20:29:02 +0000 | [diff] [blame] | 4553 | return *ErrorCategory; |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 4554 | } |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 4555 | |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 4556 | //===----------------------------------------------------------------------===// |
| 4557 | // External interface |
| 4558 | //===----------------------------------------------------------------------===// |
| 4559 | |
Duncan P. N. Exon Smith | 6e1009b | 2014-08-01 22:27:19 +0000 | [diff] [blame] | 4560 | /// \brief Get a lazy one-at-time loading module from bitcode. |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 4561 | /// |
Duncan P. N. Exon Smith | 6e1009b | 2014-08-01 22:27:19 +0000 | [diff] [blame] | 4562 | /// This isn't always used in a lazy context. In particular, it's also used by |
| 4563 | /// \a parseBitcodeFile(). If this is truly lazy, then we need to eagerly pull |
| 4564 | /// in forward-referenced functions from block address references. |
| 4565 | /// |
| 4566 | /// \param[in] WillMaterializeAll Set to \c true if the caller promises to |
| 4567 | /// materialize everything -- in particular, if this isn't truly lazy. |
Rafael Espindola | e2c1d77 | 2014-08-26 22:00:09 +0000 | [diff] [blame] | 4568 | static ErrorOr<Module *> |
Rafael Espindola | 6881215 | 2014-09-03 17:31:46 +0000 | [diff] [blame] | 4569 | getLazyBitcodeModuleImpl(std::unique_ptr<MemoryBuffer> &&Buffer, |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4570 | LLVMContext &Context, bool WillMaterializeAll, |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 4571 | DiagnosticHandlerFunction DiagnosticHandler, |
| 4572 | bool ShouldLazyLoadMetadata = false) { |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 4573 | Module *M = new Module(Buffer->getBufferIdentifier(), Context); |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4574 | BitcodeReader *R = |
| 4575 | new BitcodeReader(Buffer.get(), Context, DiagnosticHandler); |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 4576 | M->setMaterializer(R); |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 4577 | |
| 4578 | auto cleanupOnError = [&](std::error_code EC) { |
Rafael Espindola | 8fb3111 | 2014-06-18 20:07:35 +0000 | [diff] [blame] | 4579 | R->releaseBuffer(); // Never take ownership on error. |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 4580 | delete M; // Also deletes R. |
Rafael Espindola | 5b6c1e8 | 2014-01-13 18:31:04 +0000 | [diff] [blame] | 4581 | return EC; |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 4582 | }; |
Rafael Espindola | b799346 | 2012-01-02 07:49:53 +0000 | [diff] [blame] | 4583 | |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 4584 | // Delay parsing Metadata if ShouldLazyLoadMetadata is true. |
| 4585 | if (std::error_code EC = R->ParseBitcodeInto(M, ShouldLazyLoadMetadata)) |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 4586 | return cleanupOnError(EC); |
| 4587 | |
Duncan P. N. Exon Smith | 6e1009b | 2014-08-01 22:27:19 +0000 | [diff] [blame] | 4588 | if (!WillMaterializeAll) |
| 4589 | // Resolve forward references from blockaddresses. |
| 4590 | if (std::error_code EC = R->materializeForwardReferencedFunctions()) |
| 4591 | return cleanupOnError(EC); |
Rafael Espindola | b799346 | 2012-01-02 07:49:53 +0000 | [diff] [blame] | 4592 | |
Rafael Espindola | e2c1d77 | 2014-08-26 22:00:09 +0000 | [diff] [blame] | 4593 | Buffer.release(); // The BitcodeReader owns it now. |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 4594 | return M; |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 4595 | } |
| 4596 | |
Rafael Espindola | e2c1d77 | 2014-08-26 22:00:09 +0000 | [diff] [blame] | 4597 | ErrorOr<Module *> |
Rafael Espindola | 6881215 | 2014-09-03 17:31:46 +0000 | [diff] [blame] | 4598 | llvm::getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer, |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4599 | LLVMContext &Context, |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 4600 | DiagnosticHandlerFunction DiagnosticHandler, |
| 4601 | bool ShouldLazyLoadMetadata) { |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4602 | return getLazyBitcodeModuleImpl(std::move(Buffer), Context, false, |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 4603 | DiagnosticHandler, ShouldLazyLoadMetadata); |
Duncan P. N. Exon Smith | 6e1009b | 2014-08-01 22:27:19 +0000 | [diff] [blame] | 4604 | } |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 4605 | |
Rafael Espindola | 7d727b5 | 2014-12-18 05:08:43 +0000 | [diff] [blame] | 4606 | ErrorOr<std::unique_ptr<Module>> |
| 4607 | llvm::getStreamedBitcodeModule(StringRef Name, DataStreamer *Streamer, |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4608 | LLVMContext &Context, |
| 4609 | DiagnosticHandlerFunction DiagnosticHandler) { |
Rafael Espindola | 7d727b5 | 2014-12-18 05:08:43 +0000 | [diff] [blame] | 4610 | std::unique_ptr<Module> M = make_unique<Module>(Name, Context); |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4611 | BitcodeReader *R = new BitcodeReader(Streamer, Context, DiagnosticHandler); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 4612 | M->setMaterializer(R); |
Rafael Espindola | 7d727b5 | 2014-12-18 05:08:43 +0000 | [diff] [blame] | 4613 | if (std::error_code EC = R->ParseBitcodeInto(M.get())) |
| 4614 | return EC; |
| 4615 | return std::move(M); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 4616 | } |
| 4617 | |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4618 | ErrorOr<Module *> |
| 4619 | llvm::parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context, |
| 4620 | DiagnosticHandlerFunction DiagnosticHandler) { |
Rafael Espindola | d96d553 | 2014-08-26 21:49:01 +0000 | [diff] [blame] | 4621 | std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false); |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4622 | ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModuleImpl( |
| 4623 | std::move(Buf), Context, true, DiagnosticHandler); |
Rafael Espindola | 8f31e21 | 2014-01-15 01:08:23 +0000 | [diff] [blame] | 4624 | if (!ModuleOrErr) |
| 4625 | return ModuleOrErr; |
Rafael Espindola | 5b6c1e8 | 2014-01-13 18:31:04 +0000 | [diff] [blame] | 4626 | Module *M = ModuleOrErr.get(); |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 4627 | // Read in the entire module, and destroy the BitcodeReader. |
Rafael Espindola | d96d553 | 2014-08-26 21:49:01 +0000 | [diff] [blame] | 4628 | if (std::error_code EC = M->materializeAllPermanently()) { |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 4629 | delete M; |
Rafael Espindola | 8f31e21 | 2014-01-15 01:08:23 +0000 | [diff] [blame] | 4630 | return EC; |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 4631 | } |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 4632 | |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 4633 | // TODO: Restore the use-lists to the in-memory state when the bitcode was |
| 4634 | // written. We must defer until the Module has been fully materialized. |
| 4635 | |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 4636 | return M; |
| 4637 | } |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 4638 | |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4639 | std::string |
| 4640 | llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer, LLVMContext &Context, |
| 4641 | DiagnosticHandlerFunction DiagnosticHandler) { |
Rafael Espindola | d96d553 | 2014-08-26 21:49:01 +0000 | [diff] [blame] | 4642 | std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false); |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 4643 | auto R = llvm::make_unique<BitcodeReader>(Buf.release(), Context, |
| 4644 | DiagnosticHandler); |
Rafael Espindola | c75c4fa | 2014-07-04 20:02:42 +0000 | [diff] [blame] | 4645 | ErrorOr<std::string> Triple = R->parseTriple(); |
Rafael Espindola | d346cc8 | 2014-07-04 13:52:01 +0000 | [diff] [blame] | 4646 | if (Triple.getError()) |
| 4647 | return ""; |
| 4648 | return Triple.get(); |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 4649 | } |