1f22ef01cSRoman Divacky //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===// 2f22ef01cSRoman Divacky // 3f22ef01cSRoman Divacky // The LLVM Compiler Infrastructure 4f22ef01cSRoman Divacky // 5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source 6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details. 7f22ef01cSRoman Divacky // 8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 9f22ef01cSRoman Divacky 10d88c1a5aSDimitry Andric #include "llvm/Bitcode/BitcodeReader.h" 11d88c1a5aSDimitry Andric #include "MetadataLoader.h" 12d88c1a5aSDimitry Andric #include "ValueList.h" 13d88c1a5aSDimitry Andric 14d88c1a5aSDimitry Andric #include "llvm/ADT/APFloat.h" 15d88c1a5aSDimitry Andric #include "llvm/ADT/APInt.h" 16d88c1a5aSDimitry Andric #include "llvm/ADT/ArrayRef.h" 17d88c1a5aSDimitry Andric #include "llvm/ADT/DenseMap.h" 18d88c1a5aSDimitry Andric #include "llvm/ADT/None.h" 19ff0cc061SDimitry Andric #include "llvm/ADT/STLExtras.h" 20f22ef01cSRoman Divacky #include "llvm/ADT/SmallString.h" 21f22ef01cSRoman Divacky #include "llvm/ADT/SmallVector.h" 22d88c1a5aSDimitry Andric #include "llvm/ADT/StringRef.h" 23ff0cc061SDimitry Andric #include "llvm/ADT/Triple.h" 24d88c1a5aSDimitry Andric #include "llvm/ADT/Twine.h" 25ff0cc061SDimitry Andric #include "llvm/Bitcode/BitstreamReader.h" 26f785676fSDimitry Andric #include "llvm/Bitcode/LLVMBitCodes.h" 27d88c1a5aSDimitry Andric #include "llvm/IR/Argument.h" 28d88c1a5aSDimitry Andric #include "llvm/IR/Attributes.h" 2991bc56edSDimitry Andric #include "llvm/IR/AutoUpgrade.h" 30d88c1a5aSDimitry Andric #include "llvm/IR/BasicBlock.h" 313ca95b02SDimitry Andric #include "llvm/IR/CallSite.h" 32db17bf38SDimitry Andric #include "llvm/IR/CallingConv.h" 33d88c1a5aSDimitry Andric #include "llvm/IR/Comdat.h" 34d88c1a5aSDimitry Andric #include "llvm/IR/Constant.h" 35139f7f9bSDimitry Andric #include "llvm/IR/Constants.h" 36ff0cc061SDimitry Andric #include "llvm/IR/DebugInfo.h" 37ff0cc061SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h" 38d88c1a5aSDimitry Andric #include "llvm/IR/DebugLoc.h" 39139f7f9bSDimitry Andric #include "llvm/IR/DerivedTypes.h" 40d88c1a5aSDimitry Andric #include "llvm/IR/DiagnosticInfo.h" 4139d628a0SDimitry Andric #include "llvm/IR/DiagnosticPrinter.h" 42d88c1a5aSDimitry Andric #include "llvm/IR/Function.h" 43db17bf38SDimitry Andric #include "llvm/IR/GVMaterializer.h" 44d88c1a5aSDimitry Andric #include "llvm/IR/GlobalAlias.h" 45d88c1a5aSDimitry Andric #include "llvm/IR/GlobalIFunc.h" 46d88c1a5aSDimitry Andric #include "llvm/IR/GlobalIndirectSymbol.h" 47d88c1a5aSDimitry Andric #include "llvm/IR/GlobalObject.h" 48d88c1a5aSDimitry Andric #include "llvm/IR/GlobalValue.h" 49d88c1a5aSDimitry Andric #include "llvm/IR/GlobalVariable.h" 50139f7f9bSDimitry Andric #include "llvm/IR/InlineAsm.h" 51d88c1a5aSDimitry Andric #include "llvm/IR/InstIterator.h" 52d88c1a5aSDimitry Andric #include "llvm/IR/InstrTypes.h" 53d88c1a5aSDimitry Andric #include "llvm/IR/Instruction.h" 54d88c1a5aSDimitry Andric #include "llvm/IR/Instructions.h" 55d88c1a5aSDimitry Andric #include "llvm/IR/Intrinsics.h" 56f785676fSDimitry Andric #include "llvm/IR/LLVMContext.h" 57139f7f9bSDimitry Andric #include "llvm/IR/Module.h" 583ca95b02SDimitry Andric #include "llvm/IR/ModuleSummaryIndex.h" 59139f7f9bSDimitry Andric #include "llvm/IR/OperandTraits.h" 60139f7f9bSDimitry Andric #include "llvm/IR/Operator.h" 61d88c1a5aSDimitry Andric #include "llvm/IR/TrackingMDRef.h" 62d88c1a5aSDimitry Andric #include "llvm/IR/Type.h" 63ff0cc061SDimitry Andric #include "llvm/IR/ValueHandle.h" 64d88c1a5aSDimitry Andric #include "llvm/IR/Verifier.h" 65d88c1a5aSDimitry Andric #include "llvm/Support/AtomicOrdering.h" 66d88c1a5aSDimitry Andric #include "llvm/Support/Casting.h" 673ca95b02SDimitry Andric #include "llvm/Support/CommandLine.h" 68d88c1a5aSDimitry Andric #include "llvm/Support/Compiler.h" 693ca95b02SDimitry Andric #include "llvm/Support/Debug.h" 70d88c1a5aSDimitry Andric #include "llvm/Support/Error.h" 71d88c1a5aSDimitry Andric #include "llvm/Support/ErrorHandling.h" 7239d628a0SDimitry Andric #include "llvm/Support/ManagedStatic.h" 73f22ef01cSRoman Divacky #include "llvm/Support/MemoryBuffer.h" 74f785676fSDimitry Andric #include "llvm/Support/raw_ostream.h" 75d88c1a5aSDimitry Andric #include <algorithm> 76d88c1a5aSDimitry Andric #include <cassert> 77d88c1a5aSDimitry Andric #include <cstddef> 78d88c1a5aSDimitry Andric #include <cstdint> 79ff0cc061SDimitry Andric #include <deque> 80d88c1a5aSDimitry Andric #include <limits> 81d88c1a5aSDimitry Andric #include <map> 82d88c1a5aSDimitry Andric #include <memory> 83d88c1a5aSDimitry Andric #include <string> 84d88c1a5aSDimitry Andric #include <system_error> 85d88c1a5aSDimitry Andric #include <tuple> 863ca95b02SDimitry Andric #include <utility> 87d88c1a5aSDimitry Andric #include <vector> 883ca95b02SDimitry Andric 89f22ef01cSRoman Divacky using namespace llvm; 90f22ef01cSRoman Divacky 913ca95b02SDimitry Andric static cl::opt<bool> PrintSummaryGUIDs( 923ca95b02SDimitry Andric "print-summary-global-ids", cl::init(false), cl::Hidden, 933ca95b02SDimitry Andric cl::desc( 943ca95b02SDimitry Andric "Print the global id for each value when reading the module summary")); 953ca95b02SDimitry Andric 96ff0cc061SDimitry Andric namespace { 97d88c1a5aSDimitry Andric 987ae0e2c9SDimitry Andric enum { 997ae0e2c9SDimitry Andric SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex 1007ae0e2c9SDimitry Andric }; 1017ae0e2c9SDimitry Andric 102d88c1a5aSDimitry Andric Error error(const Twine &Message) { 103d88c1a5aSDimitry Andric return make_error<StringError>( 104d88c1a5aSDimitry Andric Message, make_error_code(BitcodeError::CorruptedBitcode)); 105ff0cc061SDimitry Andric } 106ff0cc061SDimitry Andric 107d88c1a5aSDimitry Andric /// Helper to read the header common to all bitcode files. 108d88c1a5aSDimitry Andric bool hasValidBitcodeHeader(BitstreamCursor &Stream) { 109d88c1a5aSDimitry Andric // Sniff for the signature. 110d88c1a5aSDimitry Andric if (!Stream.canSkipToPos(4) || 111d88c1a5aSDimitry Andric Stream.Read(8) != 'B' || 112d88c1a5aSDimitry Andric Stream.Read(8) != 'C' || 113d88c1a5aSDimitry Andric Stream.Read(4) != 0x0 || 114d88c1a5aSDimitry Andric Stream.Read(4) != 0xC || 115d88c1a5aSDimitry Andric Stream.Read(4) != 0xE || 116d88c1a5aSDimitry Andric Stream.Read(4) != 0xD) 117d88c1a5aSDimitry Andric return false; 118d88c1a5aSDimitry Andric return true; 119ff0cc061SDimitry Andric } 120ff0cc061SDimitry Andric 121d88c1a5aSDimitry Andric Expected<BitstreamCursor> initStream(MemoryBufferRef Buffer) { 122d88c1a5aSDimitry Andric const unsigned char *BufPtr = (const unsigned char *)Buffer.getBufferStart(); 123d88c1a5aSDimitry Andric const unsigned char *BufEnd = BufPtr + Buffer.getBufferSize(); 124d88c1a5aSDimitry Andric 125d88c1a5aSDimitry Andric if (Buffer.getBufferSize() & 3) 126d88c1a5aSDimitry Andric return error("Invalid bitcode signature"); 127d88c1a5aSDimitry Andric 128d88c1a5aSDimitry Andric // If we have a wrapper header, parse it and ignore the non-bc file contents. 129d88c1a5aSDimitry Andric // The magic number is 0x0B17C0DE stored in little endian. 130d88c1a5aSDimitry Andric if (isBitcodeWrapper(BufPtr, BufEnd)) 131d88c1a5aSDimitry Andric if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true)) 132d88c1a5aSDimitry Andric return error("Invalid bitcode wrapper header"); 133d88c1a5aSDimitry Andric 134d88c1a5aSDimitry Andric BitstreamCursor Stream(ArrayRef<uint8_t>(BufPtr, BufEnd)); 135d88c1a5aSDimitry Andric if (!hasValidBitcodeHeader(Stream)) 136d88c1a5aSDimitry Andric return error("Invalid bitcode signature"); 137d88c1a5aSDimitry Andric 138d88c1a5aSDimitry Andric return std::move(Stream); 139ff0cc061SDimitry Andric } 140ff0cc061SDimitry Andric 141d88c1a5aSDimitry Andric /// Convert a string from a record into an std::string, return true on failure. 142d88c1a5aSDimitry Andric template <typename StrTy> 143d88c1a5aSDimitry Andric static bool convertToString(ArrayRef<uint64_t> Record, unsigned Idx, 144d88c1a5aSDimitry Andric StrTy &Result) { 145d88c1a5aSDimitry Andric if (Idx > Record.size()) 146d88c1a5aSDimitry Andric return true; 147d88c1a5aSDimitry Andric 148d88c1a5aSDimitry Andric for (unsigned i = Idx, e = Record.size(); i != e; ++i) 149d88c1a5aSDimitry Andric Result += (char)Record[i]; 150d88c1a5aSDimitry Andric return false; 151ff0cc061SDimitry Andric } 152ff0cc061SDimitry Andric 153d88c1a5aSDimitry Andric // Strip all the TBAA attachment for the module. 154d88c1a5aSDimitry Andric void stripTBAA(Module *M) { 155d88c1a5aSDimitry Andric for (auto &F : *M) { 156d88c1a5aSDimitry Andric if (F.isMaterializable()) 157d88c1a5aSDimitry Andric continue; 158d88c1a5aSDimitry Andric for (auto &I : instructions(F)) 159d88c1a5aSDimitry Andric I.setMetadata(LLVMContext::MD_tbaa, nullptr); 160d88c1a5aSDimitry Andric } 161d88c1a5aSDimitry Andric } 162ff0cc061SDimitry Andric 163d88c1a5aSDimitry Andric /// Read the "IDENTIFICATION_BLOCK_ID" block, do some basic enforcement on the 164d88c1a5aSDimitry Andric /// "epoch" encoded in the bitcode, and return the producer name if any. 165d88c1a5aSDimitry Andric Expected<std::string> readIdentificationBlock(BitstreamCursor &Stream) { 166d88c1a5aSDimitry Andric if (Stream.EnterSubBlock(bitc::IDENTIFICATION_BLOCK_ID)) 167d88c1a5aSDimitry Andric return error("Invalid record"); 168ff0cc061SDimitry Andric 169d88c1a5aSDimitry Andric // Read all the records. 170d88c1a5aSDimitry Andric SmallVector<uint64_t, 64> Record; 171d88c1a5aSDimitry Andric 172d88c1a5aSDimitry Andric std::string ProducerIdentification; 173d88c1a5aSDimitry Andric 174d88c1a5aSDimitry Andric while (true) { 175d88c1a5aSDimitry Andric BitstreamEntry Entry = Stream.advance(); 176d88c1a5aSDimitry Andric 177d88c1a5aSDimitry Andric switch (Entry.Kind) { 178d88c1a5aSDimitry Andric default: 179d88c1a5aSDimitry Andric case BitstreamEntry::Error: 180d88c1a5aSDimitry Andric return error("Malformed block"); 181d88c1a5aSDimitry Andric case BitstreamEntry::EndBlock: 182d88c1a5aSDimitry Andric return ProducerIdentification; 183d88c1a5aSDimitry Andric case BitstreamEntry::Record: 184d88c1a5aSDimitry Andric // The interesting case. 185d88c1a5aSDimitry Andric break; 186d88c1a5aSDimitry Andric } 187d88c1a5aSDimitry Andric 188d88c1a5aSDimitry Andric // Read a record. 189d88c1a5aSDimitry Andric Record.clear(); 190d88c1a5aSDimitry Andric unsigned BitCode = Stream.readRecord(Entry.ID, Record); 191d88c1a5aSDimitry Andric switch (BitCode) { 192d88c1a5aSDimitry Andric default: // Default behavior: reject 193d88c1a5aSDimitry Andric return error("Invalid value"); 194d88c1a5aSDimitry Andric case bitc::IDENTIFICATION_CODE_STRING: // IDENTIFICATION: [strchr x N] 195d88c1a5aSDimitry Andric convertToString(Record, 0, ProducerIdentification); 196d88c1a5aSDimitry Andric break; 197d88c1a5aSDimitry Andric case bitc::IDENTIFICATION_CODE_EPOCH: { // EPOCH: [epoch#] 198d88c1a5aSDimitry Andric unsigned epoch = (unsigned)Record[0]; 199d88c1a5aSDimitry Andric if (epoch != bitc::BITCODE_CURRENT_EPOCH) { 200d88c1a5aSDimitry Andric return error( 201d88c1a5aSDimitry Andric Twine("Incompatible epoch: Bitcode '") + Twine(epoch) + 202d88c1a5aSDimitry Andric "' vs current: '" + Twine(bitc::BITCODE_CURRENT_EPOCH) + "'"); 203d88c1a5aSDimitry Andric } 204d88c1a5aSDimitry Andric } 205d88c1a5aSDimitry Andric } 206d88c1a5aSDimitry Andric } 207d88c1a5aSDimitry Andric } 208d88c1a5aSDimitry Andric 209d88c1a5aSDimitry Andric Expected<std::string> readIdentificationCode(BitstreamCursor &Stream) { 210d88c1a5aSDimitry Andric // We expect a number of well-defined blocks, though we don't necessarily 211d88c1a5aSDimitry Andric // need to understand them all. 212d88c1a5aSDimitry Andric while (true) { 213d88c1a5aSDimitry Andric if (Stream.AtEndOfStream()) 214d88c1a5aSDimitry Andric return ""; 215d88c1a5aSDimitry Andric 216d88c1a5aSDimitry Andric BitstreamEntry Entry = Stream.advance(); 217d88c1a5aSDimitry Andric switch (Entry.Kind) { 218d88c1a5aSDimitry Andric case BitstreamEntry::EndBlock: 219d88c1a5aSDimitry Andric case BitstreamEntry::Error: 220d88c1a5aSDimitry Andric return error("Malformed block"); 221d88c1a5aSDimitry Andric 222d88c1a5aSDimitry Andric case BitstreamEntry::SubBlock: 223d88c1a5aSDimitry Andric if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) 224d88c1a5aSDimitry Andric return readIdentificationBlock(Stream); 225d88c1a5aSDimitry Andric 226d88c1a5aSDimitry Andric // Ignore other sub-blocks. 227d88c1a5aSDimitry Andric if (Stream.SkipBlock()) 228d88c1a5aSDimitry Andric return error("Malformed block"); 229d88c1a5aSDimitry Andric continue; 230d88c1a5aSDimitry Andric case BitstreamEntry::Record: 231d88c1a5aSDimitry Andric Stream.skipRecord(Entry.ID); 232d88c1a5aSDimitry Andric continue; 233d88c1a5aSDimitry Andric } 234d88c1a5aSDimitry Andric } 235d88c1a5aSDimitry Andric } 236d88c1a5aSDimitry Andric 237d88c1a5aSDimitry Andric Expected<bool> hasObjCCategoryInModule(BitstreamCursor &Stream) { 238d88c1a5aSDimitry Andric if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 239d88c1a5aSDimitry Andric return error("Invalid record"); 240d88c1a5aSDimitry Andric 241d88c1a5aSDimitry Andric SmallVector<uint64_t, 64> Record; 242d88c1a5aSDimitry Andric // Read all the records for this module. 243d88c1a5aSDimitry Andric 244d88c1a5aSDimitry Andric while (true) { 245d88c1a5aSDimitry Andric BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 246d88c1a5aSDimitry Andric 247d88c1a5aSDimitry Andric switch (Entry.Kind) { 248d88c1a5aSDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 249d88c1a5aSDimitry Andric case BitstreamEntry::Error: 250d88c1a5aSDimitry Andric return error("Malformed block"); 251d88c1a5aSDimitry Andric case BitstreamEntry::EndBlock: 252d88c1a5aSDimitry Andric return false; 253d88c1a5aSDimitry Andric case BitstreamEntry::Record: 254d88c1a5aSDimitry Andric // The interesting case. 255d88c1a5aSDimitry Andric break; 256d88c1a5aSDimitry Andric } 257d88c1a5aSDimitry Andric 258d88c1a5aSDimitry Andric // Read a record. 259d88c1a5aSDimitry Andric switch (Stream.readRecord(Entry.ID, Record)) { 260d88c1a5aSDimitry Andric default: 261d88c1a5aSDimitry Andric break; // Default behavior, ignore unknown content. 262d88c1a5aSDimitry Andric case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] 263d88c1a5aSDimitry Andric std::string S; 264d88c1a5aSDimitry Andric if (convertToString(Record, 0, S)) 265d88c1a5aSDimitry Andric return error("Invalid record"); 266d88c1a5aSDimitry Andric // Check for the i386 and other (x86_64, ARM) conventions 267d88c1a5aSDimitry Andric if (S.find("__DATA, __objc_catlist") != std::string::npos || 268d88c1a5aSDimitry Andric S.find("__OBJC,__category") != std::string::npos) 269d88c1a5aSDimitry Andric return true; 270d88c1a5aSDimitry Andric break; 271d88c1a5aSDimitry Andric } 272d88c1a5aSDimitry Andric } 273d88c1a5aSDimitry Andric Record.clear(); 274d88c1a5aSDimitry Andric } 275d88c1a5aSDimitry Andric llvm_unreachable("Exit infinite loop"); 276d88c1a5aSDimitry Andric } 277d88c1a5aSDimitry Andric 278d88c1a5aSDimitry Andric Expected<bool> hasObjCCategory(BitstreamCursor &Stream) { 279d88c1a5aSDimitry Andric // We expect a number of well-defined blocks, though we don't necessarily 280d88c1a5aSDimitry Andric // need to understand them all. 281d88c1a5aSDimitry Andric while (true) { 282d88c1a5aSDimitry Andric BitstreamEntry Entry = Stream.advance(); 283d88c1a5aSDimitry Andric 284d88c1a5aSDimitry Andric switch (Entry.Kind) { 285d88c1a5aSDimitry Andric case BitstreamEntry::Error: 286d88c1a5aSDimitry Andric return error("Malformed block"); 287d88c1a5aSDimitry Andric case BitstreamEntry::EndBlock: 288d88c1a5aSDimitry Andric return false; 289d88c1a5aSDimitry Andric 290d88c1a5aSDimitry Andric case BitstreamEntry::SubBlock: 291d88c1a5aSDimitry Andric if (Entry.ID == bitc::MODULE_BLOCK_ID) 292d88c1a5aSDimitry Andric return hasObjCCategoryInModule(Stream); 293d88c1a5aSDimitry Andric 294d88c1a5aSDimitry Andric // Ignore other sub-blocks. 295d88c1a5aSDimitry Andric if (Stream.SkipBlock()) 296d88c1a5aSDimitry Andric return error("Malformed block"); 297d88c1a5aSDimitry Andric continue; 298d88c1a5aSDimitry Andric 299d88c1a5aSDimitry Andric case BitstreamEntry::Record: 300d88c1a5aSDimitry Andric Stream.skipRecord(Entry.ID); 301d88c1a5aSDimitry Andric continue; 302d88c1a5aSDimitry Andric } 303d88c1a5aSDimitry Andric } 304d88c1a5aSDimitry Andric } 305d88c1a5aSDimitry Andric 306d88c1a5aSDimitry Andric Expected<std::string> readModuleTriple(BitstreamCursor &Stream) { 307d88c1a5aSDimitry Andric if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 308d88c1a5aSDimitry Andric return error("Invalid record"); 309d88c1a5aSDimitry Andric 310d88c1a5aSDimitry Andric SmallVector<uint64_t, 64> Record; 311d88c1a5aSDimitry Andric 312d88c1a5aSDimitry Andric std::string Triple; 313d88c1a5aSDimitry Andric 314d88c1a5aSDimitry Andric // Read all the records for this module. 315d88c1a5aSDimitry Andric while (true) { 316d88c1a5aSDimitry Andric BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 317d88c1a5aSDimitry Andric 318d88c1a5aSDimitry Andric switch (Entry.Kind) { 319d88c1a5aSDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 320d88c1a5aSDimitry Andric case BitstreamEntry::Error: 321d88c1a5aSDimitry Andric return error("Malformed block"); 322d88c1a5aSDimitry Andric case BitstreamEntry::EndBlock: 323d88c1a5aSDimitry Andric return Triple; 324d88c1a5aSDimitry Andric case BitstreamEntry::Record: 325d88c1a5aSDimitry Andric // The interesting case. 326d88c1a5aSDimitry Andric break; 327d88c1a5aSDimitry Andric } 328d88c1a5aSDimitry Andric 329d88c1a5aSDimitry Andric // Read a record. 330d88c1a5aSDimitry Andric switch (Stream.readRecord(Entry.ID, Record)) { 331d88c1a5aSDimitry Andric default: break; // Default behavior, ignore unknown content. 332d88c1a5aSDimitry Andric case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] 333d88c1a5aSDimitry Andric std::string S; 334d88c1a5aSDimitry Andric if (convertToString(Record, 0, S)) 335d88c1a5aSDimitry Andric return error("Invalid record"); 336d88c1a5aSDimitry Andric Triple = S; 337d88c1a5aSDimitry Andric break; 338d88c1a5aSDimitry Andric } 339d88c1a5aSDimitry Andric } 340d88c1a5aSDimitry Andric Record.clear(); 341d88c1a5aSDimitry Andric } 342d88c1a5aSDimitry Andric llvm_unreachable("Exit infinite loop"); 343d88c1a5aSDimitry Andric } 344d88c1a5aSDimitry Andric 345d88c1a5aSDimitry Andric Expected<std::string> readTriple(BitstreamCursor &Stream) { 346d88c1a5aSDimitry Andric // We expect a number of well-defined blocks, though we don't necessarily 347d88c1a5aSDimitry Andric // need to understand them all. 348d88c1a5aSDimitry Andric while (true) { 349d88c1a5aSDimitry Andric BitstreamEntry Entry = Stream.advance(); 350d88c1a5aSDimitry Andric 351d88c1a5aSDimitry Andric switch (Entry.Kind) { 352d88c1a5aSDimitry Andric case BitstreamEntry::Error: 353d88c1a5aSDimitry Andric return error("Malformed block"); 354d88c1a5aSDimitry Andric case BitstreamEntry::EndBlock: 355d88c1a5aSDimitry Andric return ""; 356d88c1a5aSDimitry Andric 357d88c1a5aSDimitry Andric case BitstreamEntry::SubBlock: 358d88c1a5aSDimitry Andric if (Entry.ID == bitc::MODULE_BLOCK_ID) 359d88c1a5aSDimitry Andric return readModuleTriple(Stream); 360d88c1a5aSDimitry Andric 361d88c1a5aSDimitry Andric // Ignore other sub-blocks. 362d88c1a5aSDimitry Andric if (Stream.SkipBlock()) 363d88c1a5aSDimitry Andric return error("Malformed block"); 364d88c1a5aSDimitry Andric continue; 365d88c1a5aSDimitry Andric 366d88c1a5aSDimitry Andric case BitstreamEntry::Record: 367d88c1a5aSDimitry Andric Stream.skipRecord(Entry.ID); 368d88c1a5aSDimitry Andric continue; 369d88c1a5aSDimitry Andric } 370d88c1a5aSDimitry Andric } 371d88c1a5aSDimitry Andric } 372d88c1a5aSDimitry Andric 373d88c1a5aSDimitry Andric class BitcodeReaderBase { 374d88c1a5aSDimitry Andric protected: 3756bc11b14SDimitry Andric BitcodeReaderBase(BitstreamCursor Stream, StringRef Strtab) 3766bc11b14SDimitry Andric : Stream(std::move(Stream)), Strtab(Strtab) { 377d88c1a5aSDimitry Andric this->Stream.setBlockInfo(&BlockInfo); 378d88c1a5aSDimitry Andric } 379d88c1a5aSDimitry Andric 380d88c1a5aSDimitry Andric BitstreamBlockInfo BlockInfo; 381d88c1a5aSDimitry Andric BitstreamCursor Stream; 3826bc11b14SDimitry Andric StringRef Strtab; 3836bc11b14SDimitry Andric 3846bc11b14SDimitry Andric /// In version 2 of the bitcode we store names of global values and comdats in 3856bc11b14SDimitry Andric /// a string table rather than in the VST. 3866bc11b14SDimitry Andric bool UseStrtab = false; 387d88c1a5aSDimitry Andric 3887a7e6055SDimitry Andric Expected<unsigned> parseVersionRecord(ArrayRef<uint64_t> Record); 3897a7e6055SDimitry Andric 3906bc11b14SDimitry Andric /// If this module uses a string table, pop the reference to the string table 3916bc11b14SDimitry Andric /// and return the referenced string and the rest of the record. Otherwise 3926bc11b14SDimitry Andric /// just return the record itself. 3936bc11b14SDimitry Andric std::pair<StringRef, ArrayRef<uint64_t>> 3946bc11b14SDimitry Andric readNameFromStrtab(ArrayRef<uint64_t> Record); 3956bc11b14SDimitry Andric 396d88c1a5aSDimitry Andric bool readBlockInfo(); 397d88c1a5aSDimitry Andric 398d88c1a5aSDimitry Andric // Contains an arbitrary and optional string identifying the bitcode producer 399d88c1a5aSDimitry Andric std::string ProducerIdentification; 400d88c1a5aSDimitry Andric 401d88c1a5aSDimitry Andric Error error(const Twine &Message); 402ff0cc061SDimitry Andric }; 403ff0cc061SDimitry Andric 404d88c1a5aSDimitry Andric Error BitcodeReaderBase::error(const Twine &Message) { 405d88c1a5aSDimitry Andric std::string FullMsg = Message.str(); 406d88c1a5aSDimitry Andric if (!ProducerIdentification.empty()) 407d88c1a5aSDimitry Andric FullMsg += " (Producer: '" + ProducerIdentification + "' Reader: 'LLVM " + 408d88c1a5aSDimitry Andric LLVM_VERSION_STRING "')"; 409d88c1a5aSDimitry Andric return ::error(FullMsg); 410ff0cc061SDimitry Andric } 411ff0cc061SDimitry Andric 4127a7e6055SDimitry Andric Expected<unsigned> 4137a7e6055SDimitry Andric BitcodeReaderBase::parseVersionRecord(ArrayRef<uint64_t> Record) { 4147a7e6055SDimitry Andric if (Record.size() < 1) 4157a7e6055SDimitry Andric return error("Invalid record"); 4167a7e6055SDimitry Andric unsigned ModuleVersion = Record[0]; 4176bc11b14SDimitry Andric if (ModuleVersion > 2) 4187a7e6055SDimitry Andric return error("Invalid value"); 4196bc11b14SDimitry Andric UseStrtab = ModuleVersion >= 2; 4207a7e6055SDimitry Andric return ModuleVersion; 4217a7e6055SDimitry Andric } 4227a7e6055SDimitry Andric 4236bc11b14SDimitry Andric std::pair<StringRef, ArrayRef<uint64_t>> 4246bc11b14SDimitry Andric BitcodeReaderBase::readNameFromStrtab(ArrayRef<uint64_t> Record) { 4256bc11b14SDimitry Andric if (!UseStrtab) 4266bc11b14SDimitry Andric return {"", Record}; 4276bc11b14SDimitry Andric // Invalid reference. Let the caller complain about the record being empty. 4286bc11b14SDimitry Andric if (Record[0] + Record[1] > Strtab.size()) 4296bc11b14SDimitry Andric return {"", {}}; 4306bc11b14SDimitry Andric return {StringRef(Strtab.data() + Record[0], Record[1]), Record.slice(2)}; 4316bc11b14SDimitry Andric } 4326bc11b14SDimitry Andric 433d88c1a5aSDimitry Andric class BitcodeReader : public BitcodeReaderBase, public GVMaterializer { 434ff0cc061SDimitry Andric LLVMContext &Context; 4358f0fd8f6SDimitry Andric Module *TheModule = nullptr; 4367d523365SDimitry Andric // Next offset to start scanning for lazy parsing of function bodies. 4378f0fd8f6SDimitry Andric uint64_t NextUnreadBit = 0; 4387d523365SDimitry Andric // Last function offset found in the VST. 4397d523365SDimitry Andric uint64_t LastFunctionBlockBit = 0; 4408f0fd8f6SDimitry Andric bool SeenValueSymbolTable = false; 4417d523365SDimitry Andric uint64_t VSTOffset = 0; 442ff0cc061SDimitry Andric 4437a7e6055SDimitry Andric std::vector<std::string> SectionTable; 4447a7e6055SDimitry Andric std::vector<std::string> GCTable; 4457a7e6055SDimitry Andric 446ff0cc061SDimitry Andric std::vector<Type*> TypeList; 447ff0cc061SDimitry Andric BitcodeReaderValueList ValueList; 448d88c1a5aSDimitry Andric Optional<MetadataLoader> MDLoader; 449ff0cc061SDimitry Andric std::vector<Comdat *> ComdatList; 450ff0cc061SDimitry Andric SmallVector<Instruction *, 64> InstructionList; 451ff0cc061SDimitry Andric 452ff0cc061SDimitry Andric std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits; 4533ca95b02SDimitry Andric std::vector<std::pair<GlobalIndirectSymbol*, unsigned> > IndirectSymbolInits; 454ff0cc061SDimitry Andric std::vector<std::pair<Function*, unsigned> > FunctionPrefixes; 455ff0cc061SDimitry Andric std::vector<std::pair<Function*, unsigned> > FunctionPrologues; 4568f0fd8f6SDimitry Andric std::vector<std::pair<Function*, unsigned> > FunctionPersonalityFns; 457ff0cc061SDimitry Andric 4588f0fd8f6SDimitry Andric /// The set of attributes by index. Index zero in the file is for null, and 4598f0fd8f6SDimitry Andric /// is thus not represented here. As such all indices are off by one. 4607a7e6055SDimitry Andric std::vector<AttributeList> MAttributes; 461ff0cc061SDimitry Andric 4627d523365SDimitry Andric /// The set of attribute groups. 4637a7e6055SDimitry Andric std::map<unsigned, AttributeList> MAttributeGroups; 464ff0cc061SDimitry Andric 4658f0fd8f6SDimitry Andric /// While parsing a function body, this is a list of the basic blocks for the 4668f0fd8f6SDimitry Andric /// function. 467ff0cc061SDimitry Andric std::vector<BasicBlock*> FunctionBBs; 468ff0cc061SDimitry Andric 469ff0cc061SDimitry Andric // When reading the module header, this list is populated with functions that 470ff0cc061SDimitry Andric // have bodies later in the file. 471ff0cc061SDimitry Andric std::vector<Function*> FunctionsWithBodies; 472ff0cc061SDimitry Andric 473ff0cc061SDimitry Andric // When intrinsic functions are encountered which require upgrading they are 474ff0cc061SDimitry Andric // stored here with their replacement function. 4753ca95b02SDimitry Andric typedef DenseMap<Function*, Function*> UpdatedIntrinsicMap; 4763ca95b02SDimitry Andric UpdatedIntrinsicMap UpgradedIntrinsics; 4773ca95b02SDimitry Andric // Intrinsics which were remangled because of types rename 4783ca95b02SDimitry Andric UpdatedIntrinsicMap RemangledIntrinsics; 479ff0cc061SDimitry Andric 480ff0cc061SDimitry Andric // Several operations happen after the module header has been read, but 481ff0cc061SDimitry Andric // before function bodies are processed. This keeps track of whether 482ff0cc061SDimitry Andric // we've done this yet. 4838f0fd8f6SDimitry Andric bool SeenFirstFunctionBody = false; 484ff0cc061SDimitry Andric 4858f0fd8f6SDimitry Andric /// When function bodies are initially scanned, this map contains info about 4868f0fd8f6SDimitry Andric /// where to find deferred function body in the stream. 487ff0cc061SDimitry Andric DenseMap<Function*, uint64_t> DeferredFunctionInfo; 488ff0cc061SDimitry Andric 489ff0cc061SDimitry Andric /// When Metadata block is initially scanned when parsing the module, we may 490ff0cc061SDimitry Andric /// choose to defer parsing of the metadata. This vector contains info about 491ff0cc061SDimitry Andric /// which Metadata blocks are deferred. 492ff0cc061SDimitry Andric std::vector<uint64_t> DeferredMetadataInfo; 493ff0cc061SDimitry Andric 494ff0cc061SDimitry Andric /// These are basic blocks forward-referenced by block addresses. They are 495ff0cc061SDimitry Andric /// inserted lazily into functions when they're loaded. The basic block ID is 496ff0cc061SDimitry Andric /// its index into the vector. 497ff0cc061SDimitry Andric DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs; 498ff0cc061SDimitry Andric std::deque<Function *> BasicBlockFwdRefQueue; 499ff0cc061SDimitry Andric 5008f0fd8f6SDimitry Andric /// Indicates that we are using a new encoding for instruction operands where 5018f0fd8f6SDimitry Andric /// most operands in the current FUNCTION_BLOCK are encoded relative to the 5028f0fd8f6SDimitry Andric /// instruction number, for a more compact encoding. Some instruction 5038f0fd8f6SDimitry Andric /// operands are not relative to the instruction ID: basic block numbers, and 5048f0fd8f6SDimitry Andric /// types. Once the old style function blocks have been phased out, we would 505ff0cc061SDimitry Andric /// not need this flag. 5068f0fd8f6SDimitry Andric bool UseRelativeIDs = false; 507ff0cc061SDimitry Andric 508ff0cc061SDimitry Andric /// True if all functions will be materialized, negating the need to process 509ff0cc061SDimitry Andric /// (e.g.) blockaddress forward references. 5108f0fd8f6SDimitry Andric bool WillMaterializeAllForwardRefs = false; 511ff0cc061SDimitry Andric 512ff0cc061SDimitry Andric bool StripDebugInfo = false; 513d88c1a5aSDimitry Andric TBAAVerifier TBAAVerifyHelper; 5147d523365SDimitry Andric 5157d523365SDimitry Andric std::vector<std::string> BundleTags; 5167d523365SDimitry Andric 517ff0cc061SDimitry Andric public: 5186bc11b14SDimitry Andric BitcodeReader(BitstreamCursor Stream, StringRef Strtab, 5196bc11b14SDimitry Andric StringRef ProducerIdentification, LLVMContext &Context); 520ff0cc061SDimitry Andric 521d88c1a5aSDimitry Andric Error materializeForwardReferencedFunctions(); 522ff0cc061SDimitry Andric 523d88c1a5aSDimitry Andric Error materialize(GlobalValue *GV) override; 524d88c1a5aSDimitry Andric Error materializeModule() override; 525ff0cc061SDimitry Andric std::vector<StructType *> getIdentifiedStructTypes() const override; 526ff0cc061SDimitry Andric 5278f0fd8f6SDimitry Andric /// \brief Main interface to parsing a bitcode buffer. 5288f0fd8f6SDimitry Andric /// \returns true if an error occurred. 529d88c1a5aSDimitry Andric Error parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata = false, 530d88c1a5aSDimitry Andric bool IsImporting = false); 5313ca95b02SDimitry Andric 532ff0cc061SDimitry Andric static uint64_t decodeSignRotatedValue(uint64_t V); 533ff0cc061SDimitry Andric 534ff0cc061SDimitry Andric /// Materialize any deferred Metadata block. 535d88c1a5aSDimitry Andric Error materializeMetadata() override; 536ff0cc061SDimitry Andric 537ff0cc061SDimitry Andric void setStripDebugInfo() override; 538ff0cc061SDimitry Andric 539ff0cc061SDimitry Andric private: 540ff0cc061SDimitry Andric std::vector<StructType *> IdentifiedStructTypes; 541ff0cc061SDimitry Andric StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name); 542ff0cc061SDimitry Andric StructType *createIdentifiedStructType(LLVMContext &Context); 543ff0cc061SDimitry Andric 544ff0cc061SDimitry Andric Type *getTypeByID(unsigned ID); 545d88c1a5aSDimitry Andric 546ff0cc061SDimitry Andric Value *getFnValueByID(unsigned ID, Type *Ty) { 547ff0cc061SDimitry Andric if (Ty && Ty->isMetadataTy()) 548ff0cc061SDimitry Andric return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID)); 549ff0cc061SDimitry Andric return ValueList.getValueFwdRef(ID, Ty); 550ff0cc061SDimitry Andric } 551d88c1a5aSDimitry Andric 552ff0cc061SDimitry Andric Metadata *getFnMetadataByID(unsigned ID) { 553f8496407SDimitry Andric return MDLoader->getMetadataFwdRefOrLoad(ID); 554ff0cc061SDimitry Andric } 555d88c1a5aSDimitry Andric 556ff0cc061SDimitry Andric BasicBlock *getBasicBlock(unsigned ID) const { 557ff0cc061SDimitry Andric if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID 558ff0cc061SDimitry Andric return FunctionBBs[ID]; 559ff0cc061SDimitry Andric } 560d88c1a5aSDimitry Andric 5617a7e6055SDimitry Andric AttributeList getAttributes(unsigned i) const { 562ff0cc061SDimitry Andric if (i-1 < MAttributes.size()) 563ff0cc061SDimitry Andric return MAttributes[i-1]; 5647a7e6055SDimitry Andric return AttributeList(); 565ff0cc061SDimitry Andric } 566ff0cc061SDimitry Andric 5678f0fd8f6SDimitry Andric /// Read a value/type pair out of the specified record from slot 'Slot'. 5688f0fd8f6SDimitry Andric /// Increment Slot past the number of slots used in the record. Return true on 5698f0fd8f6SDimitry Andric /// failure. 570ff0cc061SDimitry Andric bool getValueTypePair(SmallVectorImpl<uint64_t> &Record, unsigned &Slot, 571ff0cc061SDimitry Andric unsigned InstNum, Value *&ResVal) { 572ff0cc061SDimitry Andric if (Slot == Record.size()) return true; 573ff0cc061SDimitry Andric unsigned ValNo = (unsigned)Record[Slot++]; 574ff0cc061SDimitry Andric // Adjust the ValNo, if it was encoded relative to the InstNum. 575ff0cc061SDimitry Andric if (UseRelativeIDs) 576ff0cc061SDimitry Andric ValNo = InstNum - ValNo; 577ff0cc061SDimitry Andric if (ValNo < InstNum) { 578ff0cc061SDimitry Andric // If this is not a forward reference, just return the value we already 579ff0cc061SDimitry Andric // have. 580ff0cc061SDimitry Andric ResVal = getFnValueByID(ValNo, nullptr); 581ff0cc061SDimitry Andric return ResVal == nullptr; 582ff0cc061SDimitry Andric } 583ff0cc061SDimitry Andric if (Slot == Record.size()) 584ff0cc061SDimitry Andric return true; 585ff0cc061SDimitry Andric 586ff0cc061SDimitry Andric unsigned TypeNo = (unsigned)Record[Slot++]; 587ff0cc061SDimitry Andric ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo)); 588ff0cc061SDimitry Andric return ResVal == nullptr; 589ff0cc061SDimitry Andric } 590ff0cc061SDimitry Andric 5918f0fd8f6SDimitry Andric /// Read a value out of the specified record from slot 'Slot'. Increment Slot 5928f0fd8f6SDimitry Andric /// past the number of slots used by the value in the record. Return true if 5938f0fd8f6SDimitry Andric /// there is an error. 594ff0cc061SDimitry Andric bool popValue(SmallVectorImpl<uint64_t> &Record, unsigned &Slot, 595ff0cc061SDimitry Andric unsigned InstNum, Type *Ty, Value *&ResVal) { 596ff0cc061SDimitry Andric if (getValue(Record, Slot, InstNum, Ty, ResVal)) 597ff0cc061SDimitry Andric return true; 598ff0cc061SDimitry Andric // All values currently take a single record slot. 599ff0cc061SDimitry Andric ++Slot; 600ff0cc061SDimitry Andric return false; 601ff0cc061SDimitry Andric } 602ff0cc061SDimitry Andric 6038f0fd8f6SDimitry Andric /// Like popValue, but does not increment the Slot number. 604ff0cc061SDimitry Andric bool getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot, 605ff0cc061SDimitry Andric unsigned InstNum, Type *Ty, Value *&ResVal) { 606ff0cc061SDimitry Andric ResVal = getValue(Record, Slot, InstNum, Ty); 607ff0cc061SDimitry Andric return ResVal == nullptr; 608ff0cc061SDimitry Andric } 609ff0cc061SDimitry Andric 6108f0fd8f6SDimitry Andric /// Version of getValue that returns ResVal directly, or 0 if there is an 6118f0fd8f6SDimitry Andric /// error. 612ff0cc061SDimitry Andric Value *getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot, 613ff0cc061SDimitry Andric unsigned InstNum, Type *Ty) { 614ff0cc061SDimitry Andric if (Slot == Record.size()) return nullptr; 615ff0cc061SDimitry Andric unsigned ValNo = (unsigned)Record[Slot]; 616ff0cc061SDimitry Andric // Adjust the ValNo, if it was encoded relative to the InstNum. 617ff0cc061SDimitry Andric if (UseRelativeIDs) 618ff0cc061SDimitry Andric ValNo = InstNum - ValNo; 619ff0cc061SDimitry Andric return getFnValueByID(ValNo, Ty); 620ff0cc061SDimitry Andric } 621ff0cc061SDimitry Andric 6228f0fd8f6SDimitry Andric /// Like getValue, but decodes signed VBRs. 623ff0cc061SDimitry Andric Value *getValueSigned(SmallVectorImpl<uint64_t> &Record, unsigned Slot, 624ff0cc061SDimitry Andric unsigned InstNum, Type *Ty) { 625ff0cc061SDimitry Andric if (Slot == Record.size()) return nullptr; 626ff0cc061SDimitry Andric unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]); 627ff0cc061SDimitry Andric // Adjust the ValNo, if it was encoded relative to the InstNum. 628ff0cc061SDimitry Andric if (UseRelativeIDs) 629ff0cc061SDimitry Andric ValNo = InstNum - ValNo; 630ff0cc061SDimitry Andric return getFnValueByID(ValNo, Ty); 631ff0cc061SDimitry Andric } 632ff0cc061SDimitry Andric 633ff0cc061SDimitry Andric /// Converts alignment exponent (i.e. power of two (or zero)) to the 634ff0cc061SDimitry Andric /// corresponding alignment to use. If alignment is too large, returns 635ff0cc061SDimitry Andric /// a corresponding error code. 636d88c1a5aSDimitry Andric Error parseAlignmentValue(uint64_t Exponent, unsigned &Alignment); 637d88c1a5aSDimitry Andric Error parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind); 638d88c1a5aSDimitry Andric Error parseModule(uint64_t ResumeBit, bool ShouldLazyLoadMetadata = false); 6397a7e6055SDimitry Andric 6407a7e6055SDimitry Andric Error parseComdatRecord(ArrayRef<uint64_t> Record); 6417a7e6055SDimitry Andric Error parseGlobalVarRecord(ArrayRef<uint64_t> Record); 6427a7e6055SDimitry Andric Error parseFunctionRecord(ArrayRef<uint64_t> Record); 6437a7e6055SDimitry Andric Error parseGlobalIndirectSymbolRecord(unsigned BitCode, 6447a7e6055SDimitry Andric ArrayRef<uint64_t> Record); 6457a7e6055SDimitry Andric 646d88c1a5aSDimitry Andric Error parseAttributeBlock(); 647d88c1a5aSDimitry Andric Error parseAttributeGroupBlock(); 648d88c1a5aSDimitry Andric Error parseTypeTable(); 649d88c1a5aSDimitry Andric Error parseTypeTableBody(); 650d88c1a5aSDimitry Andric Error parseOperandBundleTags(); 651ff0cc061SDimitry Andric 652d88c1a5aSDimitry Andric Expected<Value *> recordValue(SmallVectorImpl<uint64_t> &Record, 6537d523365SDimitry Andric unsigned NameIndex, Triple &TT); 6546bc11b14SDimitry Andric void setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta, Function *F, 6556bc11b14SDimitry Andric ArrayRef<uint64_t> Record); 656d88c1a5aSDimitry Andric Error parseValueSymbolTable(uint64_t Offset = 0); 6576bc11b14SDimitry Andric Error parseGlobalValueSymbolTable(); 658d88c1a5aSDimitry Andric Error parseConstants(); 659d88c1a5aSDimitry Andric Error rememberAndSkipFunctionBodies(); 660d88c1a5aSDimitry Andric Error rememberAndSkipFunctionBody(); 661ff0cc061SDimitry Andric /// Save the positions of the Metadata blocks and skip parsing the blocks. 662d88c1a5aSDimitry Andric Error rememberAndSkipMetadata(); 663d88c1a5aSDimitry Andric Error typeCheckLoadStoreInst(Type *ValType, Type *PtrType); 664d88c1a5aSDimitry Andric Error parseFunctionBody(Function *F); 665d88c1a5aSDimitry Andric Error globalCleanup(); 666d88c1a5aSDimitry Andric Error resolveGlobalAndIndirectSymbolInits(); 667d88c1a5aSDimitry Andric Error parseUseLists(); 668d88c1a5aSDimitry Andric Error findFunctionInStream( 669ff0cc061SDimitry Andric Function *F, 670ff0cc061SDimitry Andric DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator); 671ff0cc061SDimitry Andric }; 6727d523365SDimitry Andric 6737d523365SDimitry Andric /// Class to manage reading and parsing function summary index bitcode 6747d523365SDimitry Andric /// files/sections. 675d88c1a5aSDimitry Andric class ModuleSummaryIndexBitcodeReader : public BitcodeReaderBase { 676d88c1a5aSDimitry Andric /// The module index built during parsing. 677d88c1a5aSDimitry Andric ModuleSummaryIndex &TheIndex; 6787d523365SDimitry Andric 6793ca95b02SDimitry Andric /// Indicates whether we have encountered a global value summary section 680d88c1a5aSDimitry Andric /// yet during parsing. 6813ca95b02SDimitry Andric bool SeenGlobalValSummary = false; 6827d523365SDimitry Andric 6833ca95b02SDimitry Andric /// Indicates whether we have already parsed the VST, used for error checking. 6843ca95b02SDimitry Andric bool SeenValueSymbolTable = false; 6853ca95b02SDimitry Andric 6863ca95b02SDimitry Andric /// Set to the offset of the VST recorded in the MODULE_CODE_VSTOFFSET record. 6873ca95b02SDimitry Andric /// Used to enable on-demand parsing of the VST. 6883ca95b02SDimitry Andric uint64_t VSTOffset = 0; 6893ca95b02SDimitry Andric 6900f5676f4SDimitry Andric // Map to save ValueId to ValueInfo association that was recorded in the 6913ca95b02SDimitry Andric // ValueSymbolTable. It is used after the VST is parsed to convert 6923ca95b02SDimitry Andric // call graph edges read from the function summary from referencing 6930f5676f4SDimitry Andric // callees by their ValueId to using the ValueInfo instead, which is how 6943ca95b02SDimitry Andric // they are recorded in the summary index being built. 6950f5676f4SDimitry Andric // We save a GUID which refers to the same global as the ValueInfo, but 6960f5676f4SDimitry Andric // ignoring the linkage, i.e. for values other than local linkage they are 6970f5676f4SDimitry Andric // identical. 6980f5676f4SDimitry Andric DenseMap<unsigned, std::pair<ValueInfo, GlobalValue::GUID>> 6990f5676f4SDimitry Andric ValueIdToValueInfoMap; 7007d523365SDimitry Andric 7017d523365SDimitry Andric /// Map populated during module path string table parsing, from the 7027d523365SDimitry Andric /// module ID to a string reference owned by the index's module 7033ca95b02SDimitry Andric /// path string table, used to correlate with combined index 7047d523365SDimitry Andric /// summary records. 7057d523365SDimitry Andric DenseMap<uint64_t, StringRef> ModuleIdMap; 7067d523365SDimitry Andric 7073ca95b02SDimitry Andric /// Original source file name recorded in a bitcode record. 7083ca95b02SDimitry Andric std::string SourceFileName; 7093ca95b02SDimitry Andric 710f37b6182SDimitry Andric /// The string identifier given to this module by the client, normally the 711f37b6182SDimitry Andric /// path to the bitcode file. 712f37b6182SDimitry Andric StringRef ModulePath; 713f37b6182SDimitry Andric 714f37b6182SDimitry Andric /// For per-module summary indexes, the unique numerical identifier given to 715f37b6182SDimitry Andric /// this module by the client. 716f37b6182SDimitry Andric unsigned ModuleId; 717f37b6182SDimitry Andric 7187d523365SDimitry Andric public: 7196bc11b14SDimitry Andric ModuleSummaryIndexBitcodeReader(BitstreamCursor Stream, StringRef Strtab, 720f37b6182SDimitry Andric ModuleSummaryIndex &TheIndex, 721f37b6182SDimitry Andric StringRef ModulePath, unsigned ModuleId); 7227d523365SDimitry Andric 723f37b6182SDimitry Andric Error parseModule(); 7247d523365SDimitry Andric 7257d523365SDimitry Andric private: 7266bc11b14SDimitry Andric void setValueGUID(uint64_t ValueID, StringRef ValueName, 7276bc11b14SDimitry Andric GlobalValue::LinkageTypes Linkage, 7286bc11b14SDimitry Andric StringRef SourceFileName); 729d88c1a5aSDimitry Andric Error parseValueSymbolTable( 7303ca95b02SDimitry Andric uint64_t Offset, 7313ca95b02SDimitry Andric DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap); 732d88c1a5aSDimitry Andric std::vector<ValueInfo> makeRefList(ArrayRef<uint64_t> Record); 733d88c1a5aSDimitry Andric std::vector<FunctionSummary::EdgeTy> makeCallList(ArrayRef<uint64_t> Record, 734d88c1a5aSDimitry Andric bool IsOldProfileFormat, 735d88c1a5aSDimitry Andric bool HasProfile); 73624d58133SDimitry Andric Error parseEntireSummary(unsigned ID); 737d88c1a5aSDimitry Andric Error parseModuleStringTable(); 738d88c1a5aSDimitry Andric 7390f5676f4SDimitry Andric std::pair<ValueInfo, GlobalValue::GUID> 7400f5676f4SDimitry Andric getValueInfoFromValueId(unsigned ValueId); 741f37b6182SDimitry Andric 74224d58133SDimitry Andric ModuleSummaryIndex::ModuleInfo *addThisModule(); 7437d523365SDimitry Andric }; 744d88c1a5aSDimitry Andric 7453ca95b02SDimitry Andric } // end anonymous namespace 746ff0cc061SDimitry Andric 747d88c1a5aSDimitry Andric std::error_code llvm::errorToErrorCodeAndEmitErrors(LLVMContext &Ctx, 748d88c1a5aSDimitry Andric Error Err) { 749d88c1a5aSDimitry Andric if (Err) { 750d88c1a5aSDimitry Andric std::error_code EC; 751d88c1a5aSDimitry Andric handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) { 752d88c1a5aSDimitry Andric EC = EIB.convertToErrorCode(); 753d88c1a5aSDimitry Andric Ctx.emitError(EIB.message()); 754d88c1a5aSDimitry Andric }); 75539d628a0SDimitry Andric return EC; 756dff0c46cSDimitry Andric } 75739d628a0SDimitry Andric return std::error_code(); 758d88c1a5aSDimitry Andric } 759d88c1a5aSDimitry Andric 7606bc11b14SDimitry Andric BitcodeReader::BitcodeReader(BitstreamCursor Stream, StringRef Strtab, 761d88c1a5aSDimitry Andric StringRef ProducerIdentification, 762d88c1a5aSDimitry Andric LLVMContext &Context) 7636bc11b14SDimitry Andric : BitcodeReaderBase(std::move(Stream), Strtab), Context(Context), 764d88c1a5aSDimitry Andric ValueList(Context) { 765d88c1a5aSDimitry Andric this->ProducerIdentification = ProducerIdentification; 766d88c1a5aSDimitry Andric } 767d88c1a5aSDimitry Andric 768d88c1a5aSDimitry Andric Error BitcodeReader::materializeForwardReferencedFunctions() { 769d88c1a5aSDimitry Andric if (WillMaterializeAllForwardRefs) 770d88c1a5aSDimitry Andric return Error::success(); 77139d628a0SDimitry Andric 77239d628a0SDimitry Andric // Prevent recursion. 77339d628a0SDimitry Andric WillMaterializeAllForwardRefs = true; 77439d628a0SDimitry Andric 77539d628a0SDimitry Andric while (!BasicBlockFwdRefQueue.empty()) { 77639d628a0SDimitry Andric Function *F = BasicBlockFwdRefQueue.front(); 77739d628a0SDimitry Andric BasicBlockFwdRefQueue.pop_front(); 77839d628a0SDimitry Andric assert(F && "Expected valid function"); 77939d628a0SDimitry Andric if (!BasicBlockFwdRefs.count(F)) 78039d628a0SDimitry Andric // Already materialized. 78139d628a0SDimitry Andric continue; 78239d628a0SDimitry Andric 78339d628a0SDimitry Andric // Check for a function that isn't materializable to prevent an infinite 78439d628a0SDimitry Andric // loop. When parsing a blockaddress stored in a global variable, there 78539d628a0SDimitry Andric // isn't a trivial way to check if a function will have a body without a 78639d628a0SDimitry Andric // linear search through FunctionsWithBodies, so just check it here. 78739d628a0SDimitry Andric if (!F->isMaterializable()) 7888f0fd8f6SDimitry Andric return error("Never resolved function from blockaddress"); 78939d628a0SDimitry Andric 79039d628a0SDimitry Andric // Try to materialize F. 791d88c1a5aSDimitry Andric if (Error Err = materialize(F)) 792d88c1a5aSDimitry Andric return Err; 79339d628a0SDimitry Andric } 79439d628a0SDimitry Andric assert(BasicBlockFwdRefs.empty() && "Function missing from queue"); 79539d628a0SDimitry Andric 79639d628a0SDimitry Andric // Reset state. 79739d628a0SDimitry Andric WillMaterializeAllForwardRefs = false; 798d88c1a5aSDimitry Andric return Error::success(); 799f22ef01cSRoman Divacky } 800f22ef01cSRoman Divacky 801f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 802f22ef01cSRoman Divacky // Helper functions to implement forward reference resolution, etc. 803f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 804f22ef01cSRoman Divacky 805ff0cc061SDimitry Andric static bool hasImplicitComdat(size_t Val) { 806ff0cc061SDimitry Andric switch (Val) { 807ff0cc061SDimitry Andric default: 808ff0cc061SDimitry Andric return false; 809ff0cc061SDimitry Andric case 1: // Old WeakAnyLinkage 810ff0cc061SDimitry Andric case 4: // Old LinkOnceAnyLinkage 811ff0cc061SDimitry Andric case 10: // Old WeakODRLinkage 812ff0cc061SDimitry Andric case 11: // Old LinkOnceODRLinkage 813ff0cc061SDimitry Andric return true; 814ff0cc061SDimitry Andric } 815ff0cc061SDimitry Andric } 816ff0cc061SDimitry Andric 81739d628a0SDimitry Andric static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) { 818f22ef01cSRoman Divacky switch (Val) { 819f22ef01cSRoman Divacky default: // Map unknown/new linkages to external 82039d628a0SDimitry Andric case 0: 82139d628a0SDimitry Andric return GlobalValue::ExternalLinkage; 82239d628a0SDimitry Andric case 2: 82339d628a0SDimitry Andric return GlobalValue::AppendingLinkage; 82439d628a0SDimitry Andric case 3: 82539d628a0SDimitry Andric return GlobalValue::InternalLinkage; 82639d628a0SDimitry Andric case 5: 82739d628a0SDimitry Andric return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage 82839d628a0SDimitry Andric case 6: 82939d628a0SDimitry Andric return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage 83039d628a0SDimitry Andric case 7: 83139d628a0SDimitry Andric return GlobalValue::ExternalWeakLinkage; 83239d628a0SDimitry Andric case 8: 83339d628a0SDimitry Andric return GlobalValue::CommonLinkage; 83439d628a0SDimitry Andric case 9: 83539d628a0SDimitry Andric return GlobalValue::PrivateLinkage; 83639d628a0SDimitry Andric case 12: 83739d628a0SDimitry Andric return GlobalValue::AvailableExternallyLinkage; 83891bc56edSDimitry Andric case 13: 83991bc56edSDimitry Andric return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage 84091bc56edSDimitry Andric case 14: 84191bc56edSDimitry Andric return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage 84239d628a0SDimitry Andric case 15: 84339d628a0SDimitry Andric return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage 844ff0cc061SDimitry Andric case 1: // Old value with implicit comdat. 845ff0cc061SDimitry Andric case 16: 846ff0cc061SDimitry Andric return GlobalValue::WeakAnyLinkage; 847ff0cc061SDimitry Andric case 10: // Old value with implicit comdat. 848ff0cc061SDimitry Andric case 17: 849ff0cc061SDimitry Andric return GlobalValue::WeakODRLinkage; 850ff0cc061SDimitry Andric case 4: // Old value with implicit comdat. 851ff0cc061SDimitry Andric case 18: 852ff0cc061SDimitry Andric return GlobalValue::LinkOnceAnyLinkage; 853ff0cc061SDimitry Andric case 11: // Old value with implicit comdat. 854ff0cc061SDimitry Andric case 19: 855ff0cc061SDimitry Andric return GlobalValue::LinkOnceODRLinkage; 856f22ef01cSRoman Divacky } 857f22ef01cSRoman Divacky } 858f22ef01cSRoman Divacky 859d88c1a5aSDimitry Andric /// Decode the flags for GlobalValue in the summary. 8603ca95b02SDimitry Andric static GlobalValueSummary::GVFlags getDecodedGVSummaryFlags(uint64_t RawFlags, 8613ca95b02SDimitry Andric uint64_t Version) { 8623ca95b02SDimitry Andric // Summary were not emitted before LLVM 3.9, we don't need to upgrade Linkage 8633ca95b02SDimitry Andric // like getDecodedLinkage() above. Any future change to the linkage enum and 8643ca95b02SDimitry Andric // to getDecodedLinkage() will need to be taken into account here as above. 8653ca95b02SDimitry Andric auto Linkage = GlobalValue::LinkageTypes(RawFlags & 0xF); // 4 bits 8663ca95b02SDimitry Andric RawFlags = RawFlags >> 4; 86795ec533aSDimitry Andric bool NotEligibleToImport = (RawFlags & 0x1) || Version < 3; 8686d97bb29SDimitry Andric // The Live flag wasn't introduced until version 3. For dead stripping 86995ec533aSDimitry Andric // to work correctly on earlier versions, we must conservatively treat all 87095ec533aSDimitry Andric // values as live. 8716d97bb29SDimitry Andric bool Live = (RawFlags & 0x2) || Version < 3; 8726d97bb29SDimitry Andric return GlobalValueSummary::GVFlags(Linkage, NotEligibleToImport, Live); 8733ca95b02SDimitry Andric } 8743ca95b02SDimitry Andric 8758f0fd8f6SDimitry Andric static GlobalValue::VisibilityTypes getDecodedVisibility(unsigned Val) { 876f22ef01cSRoman Divacky switch (Val) { 877f22ef01cSRoman Divacky default: // Map unknown visibilities to default. 878f22ef01cSRoman Divacky case 0: return GlobalValue::DefaultVisibility; 879f22ef01cSRoman Divacky case 1: return GlobalValue::HiddenVisibility; 880f22ef01cSRoman Divacky case 2: return GlobalValue::ProtectedVisibility; 881f22ef01cSRoman Divacky } 882f22ef01cSRoman Divacky } 883f22ef01cSRoman Divacky 88491bc56edSDimitry Andric static GlobalValue::DLLStorageClassTypes 8858f0fd8f6SDimitry Andric getDecodedDLLStorageClass(unsigned Val) { 88691bc56edSDimitry Andric switch (Val) { 88791bc56edSDimitry Andric default: // Map unknown values to default. 88891bc56edSDimitry Andric case 0: return GlobalValue::DefaultStorageClass; 88991bc56edSDimitry Andric case 1: return GlobalValue::DLLImportStorageClass; 89091bc56edSDimitry Andric case 2: return GlobalValue::DLLExportStorageClass; 89191bc56edSDimitry Andric } 89291bc56edSDimitry Andric } 89391bc56edSDimitry Andric 8948f0fd8f6SDimitry Andric static GlobalVariable::ThreadLocalMode getDecodedThreadLocalMode(unsigned Val) { 8957ae0e2c9SDimitry Andric switch (Val) { 8967ae0e2c9SDimitry Andric case 0: return GlobalVariable::NotThreadLocal; 8977ae0e2c9SDimitry Andric default: // Map unknown non-zero value to general dynamic. 8987ae0e2c9SDimitry Andric case 1: return GlobalVariable::GeneralDynamicTLSModel; 8997ae0e2c9SDimitry Andric case 2: return GlobalVariable::LocalDynamicTLSModel; 9007ae0e2c9SDimitry Andric case 3: return GlobalVariable::InitialExecTLSModel; 9017ae0e2c9SDimitry Andric case 4: return GlobalVariable::LocalExecTLSModel; 9027ae0e2c9SDimitry Andric } 9037ae0e2c9SDimitry Andric } 9047ae0e2c9SDimitry Andric 9053ca95b02SDimitry Andric static GlobalVariable::UnnamedAddr getDecodedUnnamedAddrType(unsigned Val) { 9063ca95b02SDimitry Andric switch (Val) { 9073ca95b02SDimitry Andric default: // Map unknown to UnnamedAddr::None. 9083ca95b02SDimitry Andric case 0: return GlobalVariable::UnnamedAddr::None; 9093ca95b02SDimitry Andric case 1: return GlobalVariable::UnnamedAddr::Global; 9103ca95b02SDimitry Andric case 2: return GlobalVariable::UnnamedAddr::Local; 9113ca95b02SDimitry Andric } 9123ca95b02SDimitry Andric } 9133ca95b02SDimitry Andric 9148f0fd8f6SDimitry Andric static int getDecodedCastOpcode(unsigned Val) { 915f22ef01cSRoman Divacky switch (Val) { 916f22ef01cSRoman Divacky default: return -1; 917f22ef01cSRoman Divacky case bitc::CAST_TRUNC : return Instruction::Trunc; 918f22ef01cSRoman Divacky case bitc::CAST_ZEXT : return Instruction::ZExt; 919f22ef01cSRoman Divacky case bitc::CAST_SEXT : return Instruction::SExt; 920f22ef01cSRoman Divacky case bitc::CAST_FPTOUI : return Instruction::FPToUI; 921f22ef01cSRoman Divacky case bitc::CAST_FPTOSI : return Instruction::FPToSI; 922f22ef01cSRoman Divacky case bitc::CAST_UITOFP : return Instruction::UIToFP; 923f22ef01cSRoman Divacky case bitc::CAST_SITOFP : return Instruction::SIToFP; 924f22ef01cSRoman Divacky case bitc::CAST_FPTRUNC : return Instruction::FPTrunc; 925f22ef01cSRoman Divacky case bitc::CAST_FPEXT : return Instruction::FPExt; 926f22ef01cSRoman Divacky case bitc::CAST_PTRTOINT: return Instruction::PtrToInt; 927f22ef01cSRoman Divacky case bitc::CAST_INTTOPTR: return Instruction::IntToPtr; 928f22ef01cSRoman Divacky case bitc::CAST_BITCAST : return Instruction::BitCast; 929f785676fSDimitry Andric case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast; 930f22ef01cSRoman Divacky } 931f22ef01cSRoman Divacky } 932ff0cc061SDimitry Andric 9338f0fd8f6SDimitry Andric static int getDecodedBinaryOpcode(unsigned Val, Type *Ty) { 934ff0cc061SDimitry Andric bool IsFP = Ty->isFPOrFPVectorTy(); 935ff0cc061SDimitry Andric // BinOps are only valid for int/fp or vector of int/fp types 936ff0cc061SDimitry Andric if (!IsFP && !Ty->isIntOrIntVectorTy()) 937ff0cc061SDimitry Andric return -1; 938ff0cc061SDimitry Andric 939f22ef01cSRoman Divacky switch (Val) { 940ff0cc061SDimitry Andric default: 941ff0cc061SDimitry Andric return -1; 942f22ef01cSRoman Divacky case bitc::BINOP_ADD: 943ff0cc061SDimitry Andric return IsFP ? Instruction::FAdd : Instruction::Add; 944f22ef01cSRoman Divacky case bitc::BINOP_SUB: 945ff0cc061SDimitry Andric return IsFP ? Instruction::FSub : Instruction::Sub; 946f22ef01cSRoman Divacky case bitc::BINOP_MUL: 947ff0cc061SDimitry Andric return IsFP ? Instruction::FMul : Instruction::Mul; 948ff0cc061SDimitry Andric case bitc::BINOP_UDIV: 949ff0cc061SDimitry Andric return IsFP ? -1 : Instruction::UDiv; 950f22ef01cSRoman Divacky case bitc::BINOP_SDIV: 951ff0cc061SDimitry Andric return IsFP ? Instruction::FDiv : Instruction::SDiv; 952ff0cc061SDimitry Andric case bitc::BINOP_UREM: 953ff0cc061SDimitry Andric return IsFP ? -1 : Instruction::URem; 954f22ef01cSRoman Divacky case bitc::BINOP_SREM: 955ff0cc061SDimitry Andric return IsFP ? Instruction::FRem : Instruction::SRem; 956ff0cc061SDimitry Andric case bitc::BINOP_SHL: 957ff0cc061SDimitry Andric return IsFP ? -1 : Instruction::Shl; 958ff0cc061SDimitry Andric case bitc::BINOP_LSHR: 959ff0cc061SDimitry Andric return IsFP ? -1 : Instruction::LShr; 960ff0cc061SDimitry Andric case bitc::BINOP_ASHR: 961ff0cc061SDimitry Andric return IsFP ? -1 : Instruction::AShr; 962ff0cc061SDimitry Andric case bitc::BINOP_AND: 963ff0cc061SDimitry Andric return IsFP ? -1 : Instruction::And; 964ff0cc061SDimitry Andric case bitc::BINOP_OR: 965ff0cc061SDimitry Andric return IsFP ? -1 : Instruction::Or; 966ff0cc061SDimitry Andric case bitc::BINOP_XOR: 967ff0cc061SDimitry Andric return IsFP ? -1 : Instruction::Xor; 968f22ef01cSRoman Divacky } 969f22ef01cSRoman Divacky } 970f22ef01cSRoman Divacky 9718f0fd8f6SDimitry Andric static AtomicRMWInst::BinOp getDecodedRMWOperation(unsigned Val) { 9726122f3e6SDimitry Andric switch (Val) { 9736122f3e6SDimitry Andric default: return AtomicRMWInst::BAD_BINOP; 9746122f3e6SDimitry Andric case bitc::RMW_XCHG: return AtomicRMWInst::Xchg; 9756122f3e6SDimitry Andric case bitc::RMW_ADD: return AtomicRMWInst::Add; 9766122f3e6SDimitry Andric case bitc::RMW_SUB: return AtomicRMWInst::Sub; 9776122f3e6SDimitry Andric case bitc::RMW_AND: return AtomicRMWInst::And; 9786122f3e6SDimitry Andric case bitc::RMW_NAND: return AtomicRMWInst::Nand; 9796122f3e6SDimitry Andric case bitc::RMW_OR: return AtomicRMWInst::Or; 9806122f3e6SDimitry Andric case bitc::RMW_XOR: return AtomicRMWInst::Xor; 9816122f3e6SDimitry Andric case bitc::RMW_MAX: return AtomicRMWInst::Max; 9826122f3e6SDimitry Andric case bitc::RMW_MIN: return AtomicRMWInst::Min; 9836122f3e6SDimitry Andric case bitc::RMW_UMAX: return AtomicRMWInst::UMax; 9846122f3e6SDimitry Andric case bitc::RMW_UMIN: return AtomicRMWInst::UMin; 9856122f3e6SDimitry Andric } 9866122f3e6SDimitry Andric } 9876122f3e6SDimitry Andric 9888f0fd8f6SDimitry Andric static AtomicOrdering getDecodedOrdering(unsigned Val) { 9896122f3e6SDimitry Andric switch (Val) { 9903ca95b02SDimitry Andric case bitc::ORDERING_NOTATOMIC: return AtomicOrdering::NotAtomic; 9913ca95b02SDimitry Andric case bitc::ORDERING_UNORDERED: return AtomicOrdering::Unordered; 9923ca95b02SDimitry Andric case bitc::ORDERING_MONOTONIC: return AtomicOrdering::Monotonic; 9933ca95b02SDimitry Andric case bitc::ORDERING_ACQUIRE: return AtomicOrdering::Acquire; 9943ca95b02SDimitry Andric case bitc::ORDERING_RELEASE: return AtomicOrdering::Release; 9953ca95b02SDimitry Andric case bitc::ORDERING_ACQREL: return AtomicOrdering::AcquireRelease; 9966122f3e6SDimitry Andric default: // Map unknown orderings to sequentially-consistent. 9973ca95b02SDimitry Andric case bitc::ORDERING_SEQCST: return AtomicOrdering::SequentiallyConsistent; 9986122f3e6SDimitry Andric } 9996122f3e6SDimitry Andric } 10006122f3e6SDimitry Andric 10018f0fd8f6SDimitry Andric static SynchronizationScope getDecodedSynchScope(unsigned Val) { 10026122f3e6SDimitry Andric switch (Val) { 10036122f3e6SDimitry Andric case bitc::SYNCHSCOPE_SINGLETHREAD: return SingleThread; 10046122f3e6SDimitry Andric default: // Map unknown scopes to cross-thread. 10056122f3e6SDimitry Andric case bitc::SYNCHSCOPE_CROSSTHREAD: return CrossThread; 10066122f3e6SDimitry Andric } 10076122f3e6SDimitry Andric } 10086122f3e6SDimitry Andric 100991bc56edSDimitry Andric static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) { 101091bc56edSDimitry Andric switch (Val) { 101191bc56edSDimitry Andric default: // Map unknown selection kinds to any. 101291bc56edSDimitry Andric case bitc::COMDAT_SELECTION_KIND_ANY: 101391bc56edSDimitry Andric return Comdat::Any; 101491bc56edSDimitry Andric case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH: 101591bc56edSDimitry Andric return Comdat::ExactMatch; 101691bc56edSDimitry Andric case bitc::COMDAT_SELECTION_KIND_LARGEST: 101791bc56edSDimitry Andric return Comdat::Largest; 101891bc56edSDimitry Andric case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES: 101991bc56edSDimitry Andric return Comdat::NoDuplicates; 102091bc56edSDimitry Andric case bitc::COMDAT_SELECTION_KIND_SAME_SIZE: 102191bc56edSDimitry Andric return Comdat::SameSize; 102291bc56edSDimitry Andric } 102391bc56edSDimitry Andric } 102491bc56edSDimitry Andric 1025875ed548SDimitry Andric static FastMathFlags getDecodedFastMathFlags(unsigned Val) { 1026875ed548SDimitry Andric FastMathFlags FMF; 1027875ed548SDimitry Andric if (0 != (Val & FastMathFlags::UnsafeAlgebra)) 1028875ed548SDimitry Andric FMF.setUnsafeAlgebra(); 1029875ed548SDimitry Andric if (0 != (Val & FastMathFlags::NoNaNs)) 1030875ed548SDimitry Andric FMF.setNoNaNs(); 1031875ed548SDimitry Andric if (0 != (Val & FastMathFlags::NoInfs)) 1032875ed548SDimitry Andric FMF.setNoInfs(); 1033875ed548SDimitry Andric if (0 != (Val & FastMathFlags::NoSignedZeros)) 1034875ed548SDimitry Andric FMF.setNoSignedZeros(); 1035875ed548SDimitry Andric if (0 != (Val & FastMathFlags::AllowReciprocal)) 1036875ed548SDimitry Andric FMF.setAllowReciprocal(); 10377a7e6055SDimitry Andric if (0 != (Val & FastMathFlags::AllowContract)) 10387a7e6055SDimitry Andric FMF.setAllowContract(true); 1039875ed548SDimitry Andric return FMF; 1040875ed548SDimitry Andric } 1041875ed548SDimitry Andric 1042d88c1a5aSDimitry Andric static void upgradeDLLImportExportLinkage(GlobalValue *GV, unsigned Val) { 104391bc56edSDimitry Andric switch (Val) { 104491bc56edSDimitry Andric case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break; 104591bc56edSDimitry Andric case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break; 104691bc56edSDimitry Andric } 104791bc56edSDimitry Andric } 104891bc56edSDimitry Andric 10493ca95b02SDimitry Andric 105017a519f9SDimitry Andric Type *BitcodeReader::getTypeByID(unsigned ID) { 105117a519f9SDimitry Andric // The type table size is always specified correctly. 105217a519f9SDimitry Andric if (ID >= TypeList.size()) 105391bc56edSDimitry Andric return nullptr; 1054f22ef01cSRoman Divacky 105517a519f9SDimitry Andric if (Type *Ty = TypeList[ID]) 105617a519f9SDimitry Andric return Ty; 105717a519f9SDimitry Andric 105817a519f9SDimitry Andric // If we have a forward reference, the only possible case is when it is to a 105917a519f9SDimitry Andric // named struct. Just create a placeholder for now. 106039d628a0SDimitry Andric return TypeList[ID] = createIdentifiedStructType(Context); 106139d628a0SDimitry Andric } 106239d628a0SDimitry Andric 106339d628a0SDimitry Andric StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context, 106439d628a0SDimitry Andric StringRef Name) { 106539d628a0SDimitry Andric auto *Ret = StructType::create(Context, Name); 106639d628a0SDimitry Andric IdentifiedStructTypes.push_back(Ret); 106739d628a0SDimitry Andric return Ret; 106839d628a0SDimitry Andric } 106939d628a0SDimitry Andric 107039d628a0SDimitry Andric StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) { 107139d628a0SDimitry Andric auto *Ret = StructType::create(Context); 107239d628a0SDimitry Andric IdentifiedStructTypes.push_back(Ret); 107339d628a0SDimitry Andric return Ret; 1074f22ef01cSRoman Divacky } 1075f22ef01cSRoman Divacky 1076f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 1077f22ef01cSRoman Divacky // Functions for parsing blocks from the bitcode file 1078f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 1079f22ef01cSRoman Divacky 1080d88c1a5aSDimitry Andric static uint64_t getRawAttributeMask(Attribute::AttrKind Val) { 1081d88c1a5aSDimitry Andric switch (Val) { 1082d88c1a5aSDimitry Andric case Attribute::EndAttrKinds: 1083d88c1a5aSDimitry Andric llvm_unreachable("Synthetic enumerators which should never get here"); 1084d88c1a5aSDimitry Andric 1085d88c1a5aSDimitry Andric case Attribute::None: return 0; 1086d88c1a5aSDimitry Andric case Attribute::ZExt: return 1 << 0; 1087d88c1a5aSDimitry Andric case Attribute::SExt: return 1 << 1; 1088d88c1a5aSDimitry Andric case Attribute::NoReturn: return 1 << 2; 1089d88c1a5aSDimitry Andric case Attribute::InReg: return 1 << 3; 1090d88c1a5aSDimitry Andric case Attribute::StructRet: return 1 << 4; 1091d88c1a5aSDimitry Andric case Attribute::NoUnwind: return 1 << 5; 1092d88c1a5aSDimitry Andric case Attribute::NoAlias: return 1 << 6; 1093d88c1a5aSDimitry Andric case Attribute::ByVal: return 1 << 7; 1094d88c1a5aSDimitry Andric case Attribute::Nest: return 1 << 8; 1095d88c1a5aSDimitry Andric case Attribute::ReadNone: return 1 << 9; 1096d88c1a5aSDimitry Andric case Attribute::ReadOnly: return 1 << 10; 1097d88c1a5aSDimitry Andric case Attribute::NoInline: return 1 << 11; 1098d88c1a5aSDimitry Andric case Attribute::AlwaysInline: return 1 << 12; 1099d88c1a5aSDimitry Andric case Attribute::OptimizeForSize: return 1 << 13; 1100d88c1a5aSDimitry Andric case Attribute::StackProtect: return 1 << 14; 1101d88c1a5aSDimitry Andric case Attribute::StackProtectReq: return 1 << 15; 1102d88c1a5aSDimitry Andric case Attribute::Alignment: return 31 << 16; 1103d88c1a5aSDimitry Andric case Attribute::NoCapture: return 1 << 21; 1104d88c1a5aSDimitry Andric case Attribute::NoRedZone: return 1 << 22; 1105d88c1a5aSDimitry Andric case Attribute::NoImplicitFloat: return 1 << 23; 1106d88c1a5aSDimitry Andric case Attribute::Naked: return 1 << 24; 1107d88c1a5aSDimitry Andric case Attribute::InlineHint: return 1 << 25; 1108d88c1a5aSDimitry Andric case Attribute::StackAlignment: return 7 << 26; 1109d88c1a5aSDimitry Andric case Attribute::ReturnsTwice: return 1 << 29; 1110d88c1a5aSDimitry Andric case Attribute::UWTable: return 1 << 30; 1111d88c1a5aSDimitry Andric case Attribute::NonLazyBind: return 1U << 31; 1112d88c1a5aSDimitry Andric case Attribute::SanitizeAddress: return 1ULL << 32; 1113d88c1a5aSDimitry Andric case Attribute::MinSize: return 1ULL << 33; 1114d88c1a5aSDimitry Andric case Attribute::NoDuplicate: return 1ULL << 34; 1115d88c1a5aSDimitry Andric case Attribute::StackProtectStrong: return 1ULL << 35; 1116d88c1a5aSDimitry Andric case Attribute::SanitizeThread: return 1ULL << 36; 1117d88c1a5aSDimitry Andric case Attribute::SanitizeMemory: return 1ULL << 37; 1118d88c1a5aSDimitry Andric case Attribute::NoBuiltin: return 1ULL << 38; 1119d88c1a5aSDimitry Andric case Attribute::Returned: return 1ULL << 39; 1120d88c1a5aSDimitry Andric case Attribute::Cold: return 1ULL << 40; 1121d88c1a5aSDimitry Andric case Attribute::Builtin: return 1ULL << 41; 1122d88c1a5aSDimitry Andric case Attribute::OptimizeNone: return 1ULL << 42; 1123d88c1a5aSDimitry Andric case Attribute::InAlloca: return 1ULL << 43; 1124d88c1a5aSDimitry Andric case Attribute::NonNull: return 1ULL << 44; 1125d88c1a5aSDimitry Andric case Attribute::JumpTable: return 1ULL << 45; 1126d88c1a5aSDimitry Andric case Attribute::Convergent: return 1ULL << 46; 1127d88c1a5aSDimitry Andric case Attribute::SafeStack: return 1ULL << 47; 1128d88c1a5aSDimitry Andric case Attribute::NoRecurse: return 1ULL << 48; 1129d88c1a5aSDimitry Andric case Attribute::InaccessibleMemOnly: return 1ULL << 49; 1130d88c1a5aSDimitry Andric case Attribute::InaccessibleMemOrArgMemOnly: return 1ULL << 50; 1131d88c1a5aSDimitry Andric case Attribute::SwiftSelf: return 1ULL << 51; 1132d88c1a5aSDimitry Andric case Attribute::SwiftError: return 1ULL << 52; 1133d88c1a5aSDimitry Andric case Attribute::WriteOnly: return 1ULL << 53; 1134f37b6182SDimitry Andric case Attribute::Speculatable: return 1ULL << 54; 1135d88c1a5aSDimitry Andric case Attribute::Dereferenceable: 1136d88c1a5aSDimitry Andric llvm_unreachable("dereferenceable attribute not supported in raw format"); 1137d88c1a5aSDimitry Andric break; 1138d88c1a5aSDimitry Andric case Attribute::DereferenceableOrNull: 1139d88c1a5aSDimitry Andric llvm_unreachable("dereferenceable_or_null attribute not supported in raw " 1140d88c1a5aSDimitry Andric "format"); 1141d88c1a5aSDimitry Andric break; 1142d88c1a5aSDimitry Andric case Attribute::ArgMemOnly: 1143d88c1a5aSDimitry Andric llvm_unreachable("argmemonly attribute not supported in raw format"); 1144d88c1a5aSDimitry Andric break; 1145d88c1a5aSDimitry Andric case Attribute::AllocSize: 1146d88c1a5aSDimitry Andric llvm_unreachable("allocsize not supported in raw format"); 1147d88c1a5aSDimitry Andric break; 1148d88c1a5aSDimitry Andric } 1149d88c1a5aSDimitry Andric llvm_unreachable("Unsupported attribute type"); 1150d88c1a5aSDimitry Andric } 1151d88c1a5aSDimitry Andric 1152d88c1a5aSDimitry Andric static void addRawAttributeValue(AttrBuilder &B, uint64_t Val) { 1153d88c1a5aSDimitry Andric if (!Val) return; 1154d88c1a5aSDimitry Andric 1155d88c1a5aSDimitry Andric for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds; 1156d88c1a5aSDimitry Andric I = Attribute::AttrKind(I + 1)) { 1157d88c1a5aSDimitry Andric if (I == Attribute::Dereferenceable || 1158d88c1a5aSDimitry Andric I == Attribute::DereferenceableOrNull || 1159d88c1a5aSDimitry Andric I == Attribute::ArgMemOnly || 1160d88c1a5aSDimitry Andric I == Attribute::AllocSize) 1161d88c1a5aSDimitry Andric continue; 1162d88c1a5aSDimitry Andric if (uint64_t A = (Val & getRawAttributeMask(I))) { 1163d88c1a5aSDimitry Andric if (I == Attribute::Alignment) 1164d88c1a5aSDimitry Andric B.addAlignmentAttr(1ULL << ((A >> 16) - 1)); 1165d88c1a5aSDimitry Andric else if (I == Attribute::StackAlignment) 1166d88c1a5aSDimitry Andric B.addStackAlignmentAttr(1ULL << ((A >> 26)-1)); 1167d88c1a5aSDimitry Andric else 1168d88c1a5aSDimitry Andric B.addAttribute(I); 1169d88c1a5aSDimitry Andric } 1170d88c1a5aSDimitry Andric } 1171d88c1a5aSDimitry Andric } 1172139f7f9bSDimitry Andric 1173139f7f9bSDimitry Andric /// \brief This fills an AttrBuilder object with the LLVM attributes that have 1174139f7f9bSDimitry Andric /// been decoded from the given integer. This function must stay in sync with 1175139f7f9bSDimitry Andric /// 'encodeLLVMAttributesForBitcode'. 1176139f7f9bSDimitry Andric static void decodeLLVMAttributesForBitcode(AttrBuilder &B, 1177139f7f9bSDimitry Andric uint64_t EncodedAttrs) { 1178139f7f9bSDimitry Andric // FIXME: Remove in 4.0. 1179139f7f9bSDimitry Andric 1180139f7f9bSDimitry Andric // The alignment is stored as a 16-bit raw value from bits 31--16. We shift 1181139f7f9bSDimitry Andric // the bits above 31 down by 11 bits. 1182139f7f9bSDimitry Andric unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16; 1183139f7f9bSDimitry Andric assert((!Alignment || isPowerOf2_32(Alignment)) && 1184139f7f9bSDimitry Andric "Alignment must be a power of two."); 1185139f7f9bSDimitry Andric 1186139f7f9bSDimitry Andric if (Alignment) 1187139f7f9bSDimitry Andric B.addAlignmentAttr(Alignment); 1188d88c1a5aSDimitry Andric addRawAttributeValue(B, ((EncodedAttrs & (0xfffffULL << 32)) >> 11) | 1189139f7f9bSDimitry Andric (EncodedAttrs & 0xffff)); 1190139f7f9bSDimitry Andric } 1191139f7f9bSDimitry Andric 1192d88c1a5aSDimitry Andric Error BitcodeReader::parseAttributeBlock() { 1193f22ef01cSRoman Divacky if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID)) 11948f0fd8f6SDimitry Andric return error("Invalid record"); 1195f22ef01cSRoman Divacky 1196f22ef01cSRoman Divacky if (!MAttributes.empty()) 11978f0fd8f6SDimitry Andric return error("Invalid multiple blocks"); 1198f22ef01cSRoman Divacky 1199f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 1200f22ef01cSRoman Divacky 12017a7e6055SDimitry Andric SmallVector<AttributeList, 8> Attrs; 1202f22ef01cSRoman Divacky 1203f22ef01cSRoman Divacky // Read all the records. 1204d88c1a5aSDimitry Andric while (true) { 1205139f7f9bSDimitry Andric BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 1206139f7f9bSDimitry Andric 1207139f7f9bSDimitry Andric switch (Entry.Kind) { 1208139f7f9bSDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 1209139f7f9bSDimitry Andric case BitstreamEntry::Error: 12108f0fd8f6SDimitry Andric return error("Malformed block"); 1211139f7f9bSDimitry Andric case BitstreamEntry::EndBlock: 1212d88c1a5aSDimitry Andric return Error::success(); 1213139f7f9bSDimitry Andric case BitstreamEntry::Record: 1214139f7f9bSDimitry Andric // The interesting case. 1215139f7f9bSDimitry Andric break; 1216f22ef01cSRoman Divacky } 1217f22ef01cSRoman Divacky 1218f22ef01cSRoman Divacky // Read a record. 1219f22ef01cSRoman Divacky Record.clear(); 1220139f7f9bSDimitry Andric switch (Stream.readRecord(Entry.ID, Record)) { 1221f22ef01cSRoman Divacky default: // Default behavior: ignore. 1222f22ef01cSRoman Divacky break; 1223139f7f9bSDimitry Andric case bitc::PARAMATTR_CODE_ENTRY_OLD: { // ENTRY: [paramidx0, attr0, ...] 1224139f7f9bSDimitry Andric // FIXME: Remove in 4.0. 1225f22ef01cSRoman Divacky if (Record.size() & 1) 12268f0fd8f6SDimitry Andric return error("Invalid record"); 1227f22ef01cSRoman Divacky 1228f22ef01cSRoman Divacky for (unsigned i = 0, e = Record.size(); i != e; i += 2) { 1229139f7f9bSDimitry Andric AttrBuilder B; 1230139f7f9bSDimitry Andric decodeLLVMAttributesForBitcode(B, Record[i+1]); 12317a7e6055SDimitry Andric Attrs.push_back(AttributeList::get(Context, Record[i], B)); 1232f22ef01cSRoman Divacky } 1233f22ef01cSRoman Divacky 12347a7e6055SDimitry Andric MAttributes.push_back(AttributeList::get(Context, Attrs)); 1235f22ef01cSRoman Divacky Attrs.clear(); 1236f22ef01cSRoman Divacky break; 1237f22ef01cSRoman Divacky } 1238139f7f9bSDimitry Andric case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [attrgrp0, attrgrp1, ...] 1239139f7f9bSDimitry Andric for (unsigned i = 0, e = Record.size(); i != e; ++i) 1240139f7f9bSDimitry Andric Attrs.push_back(MAttributeGroups[Record[i]]); 1241139f7f9bSDimitry Andric 12427a7e6055SDimitry Andric MAttributes.push_back(AttributeList::get(Context, Attrs)); 1243139f7f9bSDimitry Andric Attrs.clear(); 1244139f7f9bSDimitry Andric break; 1245139f7f9bSDimitry Andric } 1246139f7f9bSDimitry Andric } 1247139f7f9bSDimitry Andric } 1248139f7f9bSDimitry Andric } 1249139f7f9bSDimitry Andric 1250f785676fSDimitry Andric // Returns Attribute::None on unrecognized codes. 12518f0fd8f6SDimitry Andric static Attribute::AttrKind getAttrFromCode(uint64_t Code) { 1252f785676fSDimitry Andric switch (Code) { 1253f785676fSDimitry Andric default: 1254f785676fSDimitry Andric return Attribute::None; 1255f785676fSDimitry Andric case bitc::ATTR_KIND_ALIGNMENT: 1256f785676fSDimitry Andric return Attribute::Alignment; 1257f785676fSDimitry Andric case bitc::ATTR_KIND_ALWAYS_INLINE: 1258f785676fSDimitry Andric return Attribute::AlwaysInline; 1259875ed548SDimitry Andric case bitc::ATTR_KIND_ARGMEMONLY: 1260875ed548SDimitry Andric return Attribute::ArgMemOnly; 1261f785676fSDimitry Andric case bitc::ATTR_KIND_BUILTIN: 1262f785676fSDimitry Andric return Attribute::Builtin; 1263f785676fSDimitry Andric case bitc::ATTR_KIND_BY_VAL: 1264f785676fSDimitry Andric return Attribute::ByVal; 126591bc56edSDimitry Andric case bitc::ATTR_KIND_IN_ALLOCA: 126691bc56edSDimitry Andric return Attribute::InAlloca; 1267f785676fSDimitry Andric case bitc::ATTR_KIND_COLD: 1268f785676fSDimitry Andric return Attribute::Cold; 1269ff0cc061SDimitry Andric case bitc::ATTR_KIND_CONVERGENT: 1270ff0cc061SDimitry Andric return Attribute::Convergent; 12717d523365SDimitry Andric case bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY: 12727d523365SDimitry Andric return Attribute::InaccessibleMemOnly; 12737d523365SDimitry Andric case bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY: 12747d523365SDimitry Andric return Attribute::InaccessibleMemOrArgMemOnly; 1275f785676fSDimitry Andric case bitc::ATTR_KIND_INLINE_HINT: 1276f785676fSDimitry Andric return Attribute::InlineHint; 1277f785676fSDimitry Andric case bitc::ATTR_KIND_IN_REG: 1278f785676fSDimitry Andric return Attribute::InReg; 127991bc56edSDimitry Andric case bitc::ATTR_KIND_JUMP_TABLE: 128091bc56edSDimitry Andric return Attribute::JumpTable; 1281f785676fSDimitry Andric case bitc::ATTR_KIND_MIN_SIZE: 1282f785676fSDimitry Andric return Attribute::MinSize; 1283f785676fSDimitry Andric case bitc::ATTR_KIND_NAKED: 1284f785676fSDimitry Andric return Attribute::Naked; 1285f785676fSDimitry Andric case bitc::ATTR_KIND_NEST: 1286f785676fSDimitry Andric return Attribute::Nest; 1287f785676fSDimitry Andric case bitc::ATTR_KIND_NO_ALIAS: 1288f785676fSDimitry Andric return Attribute::NoAlias; 1289f785676fSDimitry Andric case bitc::ATTR_KIND_NO_BUILTIN: 1290f785676fSDimitry Andric return Attribute::NoBuiltin; 1291f785676fSDimitry Andric case bitc::ATTR_KIND_NO_CAPTURE: 1292f785676fSDimitry Andric return Attribute::NoCapture; 1293f785676fSDimitry Andric case bitc::ATTR_KIND_NO_DUPLICATE: 1294f785676fSDimitry Andric return Attribute::NoDuplicate; 1295f785676fSDimitry Andric case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT: 1296f785676fSDimitry Andric return Attribute::NoImplicitFloat; 1297f785676fSDimitry Andric case bitc::ATTR_KIND_NO_INLINE: 1298f785676fSDimitry Andric return Attribute::NoInline; 12997d523365SDimitry Andric case bitc::ATTR_KIND_NO_RECURSE: 13007d523365SDimitry Andric return Attribute::NoRecurse; 1301f785676fSDimitry Andric case bitc::ATTR_KIND_NON_LAZY_BIND: 1302f785676fSDimitry Andric return Attribute::NonLazyBind; 130391bc56edSDimitry Andric case bitc::ATTR_KIND_NON_NULL: 130491bc56edSDimitry Andric return Attribute::NonNull; 130591bc56edSDimitry Andric case bitc::ATTR_KIND_DEREFERENCEABLE: 130691bc56edSDimitry Andric return Attribute::Dereferenceable; 1307ff0cc061SDimitry Andric case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL: 1308ff0cc061SDimitry Andric return Attribute::DereferenceableOrNull; 13093ca95b02SDimitry Andric case bitc::ATTR_KIND_ALLOC_SIZE: 13103ca95b02SDimitry Andric return Attribute::AllocSize; 1311f785676fSDimitry Andric case bitc::ATTR_KIND_NO_RED_ZONE: 1312f785676fSDimitry Andric return Attribute::NoRedZone; 1313f785676fSDimitry Andric case bitc::ATTR_KIND_NO_RETURN: 1314f785676fSDimitry Andric return Attribute::NoReturn; 1315f785676fSDimitry Andric case bitc::ATTR_KIND_NO_UNWIND: 1316f785676fSDimitry Andric return Attribute::NoUnwind; 1317f785676fSDimitry Andric case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE: 1318f785676fSDimitry Andric return Attribute::OptimizeForSize; 1319f785676fSDimitry Andric case bitc::ATTR_KIND_OPTIMIZE_NONE: 1320f785676fSDimitry Andric return Attribute::OptimizeNone; 1321f785676fSDimitry Andric case bitc::ATTR_KIND_READ_NONE: 1322f785676fSDimitry Andric return Attribute::ReadNone; 1323f785676fSDimitry Andric case bitc::ATTR_KIND_READ_ONLY: 1324f785676fSDimitry Andric return Attribute::ReadOnly; 1325f785676fSDimitry Andric case bitc::ATTR_KIND_RETURNED: 1326f785676fSDimitry Andric return Attribute::Returned; 1327f785676fSDimitry Andric case bitc::ATTR_KIND_RETURNS_TWICE: 1328f785676fSDimitry Andric return Attribute::ReturnsTwice; 1329f785676fSDimitry Andric case bitc::ATTR_KIND_S_EXT: 1330f785676fSDimitry Andric return Attribute::SExt; 1331f37b6182SDimitry Andric case bitc::ATTR_KIND_SPECULATABLE: 1332f37b6182SDimitry Andric return Attribute::Speculatable; 1333f785676fSDimitry Andric case bitc::ATTR_KIND_STACK_ALIGNMENT: 1334f785676fSDimitry Andric return Attribute::StackAlignment; 1335f785676fSDimitry Andric case bitc::ATTR_KIND_STACK_PROTECT: 1336f785676fSDimitry Andric return Attribute::StackProtect; 1337f785676fSDimitry Andric case bitc::ATTR_KIND_STACK_PROTECT_REQ: 1338f785676fSDimitry Andric return Attribute::StackProtectReq; 1339f785676fSDimitry Andric case bitc::ATTR_KIND_STACK_PROTECT_STRONG: 1340f785676fSDimitry Andric return Attribute::StackProtectStrong; 13418f0fd8f6SDimitry Andric case bitc::ATTR_KIND_SAFESTACK: 13428f0fd8f6SDimitry Andric return Attribute::SafeStack; 1343f785676fSDimitry Andric case bitc::ATTR_KIND_STRUCT_RET: 1344f785676fSDimitry Andric return Attribute::StructRet; 1345f785676fSDimitry Andric case bitc::ATTR_KIND_SANITIZE_ADDRESS: 1346f785676fSDimitry Andric return Attribute::SanitizeAddress; 1347f785676fSDimitry Andric case bitc::ATTR_KIND_SANITIZE_THREAD: 1348f785676fSDimitry Andric return Attribute::SanitizeThread; 1349f785676fSDimitry Andric case bitc::ATTR_KIND_SANITIZE_MEMORY: 1350f785676fSDimitry Andric return Attribute::SanitizeMemory; 13513ca95b02SDimitry Andric case bitc::ATTR_KIND_SWIFT_ERROR: 13523ca95b02SDimitry Andric return Attribute::SwiftError; 13533ca95b02SDimitry Andric case bitc::ATTR_KIND_SWIFT_SELF: 13543ca95b02SDimitry Andric return Attribute::SwiftSelf; 1355f785676fSDimitry Andric case bitc::ATTR_KIND_UW_TABLE: 1356f785676fSDimitry Andric return Attribute::UWTable; 13573ca95b02SDimitry Andric case bitc::ATTR_KIND_WRITEONLY: 13583ca95b02SDimitry Andric return Attribute::WriteOnly; 1359f785676fSDimitry Andric case bitc::ATTR_KIND_Z_EXT: 1360f785676fSDimitry Andric return Attribute::ZExt; 1361f785676fSDimitry Andric } 1362f785676fSDimitry Andric } 1363f785676fSDimitry Andric 1364d88c1a5aSDimitry Andric Error BitcodeReader::parseAlignmentValue(uint64_t Exponent, 1365ff0cc061SDimitry Andric unsigned &Alignment) { 1366ff0cc061SDimitry Andric // Note: Alignment in bitcode files is incremented by 1, so that zero 1367ff0cc061SDimitry Andric // can be used for default alignment. 1368ff0cc061SDimitry Andric if (Exponent > Value::MaxAlignmentExponent + 1) 13698f0fd8f6SDimitry Andric return error("Invalid alignment value"); 1370ff0cc061SDimitry Andric Alignment = (1 << static_cast<unsigned>(Exponent)) >> 1; 1371d88c1a5aSDimitry Andric return Error::success(); 1372ff0cc061SDimitry Andric } 1373ff0cc061SDimitry Andric 1374d88c1a5aSDimitry Andric Error BitcodeReader::parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind) { 13758f0fd8f6SDimitry Andric *Kind = getAttrFromCode(Code); 1376f785676fSDimitry Andric if (*Kind == Attribute::None) 1377d88c1a5aSDimitry Andric return error("Unknown attribute kind (" + Twine(Code) + ")"); 1378d88c1a5aSDimitry Andric return Error::success(); 1379f785676fSDimitry Andric } 1380f785676fSDimitry Andric 1381d88c1a5aSDimitry Andric Error BitcodeReader::parseAttributeGroupBlock() { 1382139f7f9bSDimitry Andric if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID)) 13838f0fd8f6SDimitry Andric return error("Invalid record"); 1384139f7f9bSDimitry Andric 1385139f7f9bSDimitry Andric if (!MAttributeGroups.empty()) 13868f0fd8f6SDimitry Andric return error("Invalid multiple blocks"); 1387139f7f9bSDimitry Andric 1388139f7f9bSDimitry Andric SmallVector<uint64_t, 64> Record; 1389139f7f9bSDimitry Andric 1390139f7f9bSDimitry Andric // Read all the records. 1391d88c1a5aSDimitry Andric while (true) { 1392139f7f9bSDimitry Andric BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 1393139f7f9bSDimitry Andric 1394139f7f9bSDimitry Andric switch (Entry.Kind) { 1395139f7f9bSDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 1396139f7f9bSDimitry Andric case BitstreamEntry::Error: 13978f0fd8f6SDimitry Andric return error("Malformed block"); 1398139f7f9bSDimitry Andric case BitstreamEntry::EndBlock: 1399d88c1a5aSDimitry Andric return Error::success(); 1400139f7f9bSDimitry Andric case BitstreamEntry::Record: 1401139f7f9bSDimitry Andric // The interesting case. 1402139f7f9bSDimitry Andric break; 1403139f7f9bSDimitry Andric } 1404139f7f9bSDimitry Andric 1405139f7f9bSDimitry Andric // Read a record. 1406139f7f9bSDimitry Andric Record.clear(); 1407139f7f9bSDimitry Andric switch (Stream.readRecord(Entry.ID, Record)) { 1408139f7f9bSDimitry Andric default: // Default behavior: ignore. 1409139f7f9bSDimitry Andric break; 1410139f7f9bSDimitry Andric case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...] 1411139f7f9bSDimitry Andric if (Record.size() < 3) 14128f0fd8f6SDimitry Andric return error("Invalid record"); 1413139f7f9bSDimitry Andric 1414139f7f9bSDimitry Andric uint64_t GrpID = Record[0]; 1415139f7f9bSDimitry Andric uint64_t Idx = Record[1]; // Index of the object this attribute refers to. 1416139f7f9bSDimitry Andric 1417139f7f9bSDimitry Andric AttrBuilder B; 1418139f7f9bSDimitry Andric for (unsigned i = 2, e = Record.size(); i != e; ++i) { 1419139f7f9bSDimitry Andric if (Record[i] == 0) { // Enum attribute 1420f785676fSDimitry Andric Attribute::AttrKind Kind; 1421d88c1a5aSDimitry Andric if (Error Err = parseAttrKind(Record[++i], &Kind)) 1422d88c1a5aSDimitry Andric return Err; 1423f785676fSDimitry Andric 1424f785676fSDimitry Andric B.addAttribute(Kind); 142591bc56edSDimitry Andric } else if (Record[i] == 1) { // Integer attribute 1426f785676fSDimitry Andric Attribute::AttrKind Kind; 1427d88c1a5aSDimitry Andric if (Error Err = parseAttrKind(Record[++i], &Kind)) 1428d88c1a5aSDimitry Andric return Err; 1429f785676fSDimitry Andric if (Kind == Attribute::Alignment) 1430139f7f9bSDimitry Andric B.addAlignmentAttr(Record[++i]); 143191bc56edSDimitry Andric else if (Kind == Attribute::StackAlignment) 1432139f7f9bSDimitry Andric B.addStackAlignmentAttr(Record[++i]); 143391bc56edSDimitry Andric else if (Kind == Attribute::Dereferenceable) 143491bc56edSDimitry Andric B.addDereferenceableAttr(Record[++i]); 1435ff0cc061SDimitry Andric else if (Kind == Attribute::DereferenceableOrNull) 1436ff0cc061SDimitry Andric B.addDereferenceableOrNullAttr(Record[++i]); 14373ca95b02SDimitry Andric else if (Kind == Attribute::AllocSize) 14383ca95b02SDimitry Andric B.addAllocSizeAttrFromRawRepr(Record[++i]); 1439139f7f9bSDimitry Andric } else { // String attribute 1440139f7f9bSDimitry Andric assert((Record[i] == 3 || Record[i] == 4) && 1441139f7f9bSDimitry Andric "Invalid attribute group entry"); 1442139f7f9bSDimitry Andric bool HasValue = (Record[i++] == 4); 1443139f7f9bSDimitry Andric SmallString<64> KindStr; 1444139f7f9bSDimitry Andric SmallString<64> ValStr; 1445139f7f9bSDimitry Andric 1446139f7f9bSDimitry Andric while (Record[i] != 0 && i != e) 1447139f7f9bSDimitry Andric KindStr += Record[i++]; 1448139f7f9bSDimitry Andric assert(Record[i] == 0 && "Kind string not null terminated"); 1449139f7f9bSDimitry Andric 1450139f7f9bSDimitry Andric if (HasValue) { 1451139f7f9bSDimitry Andric // Has a value associated with it. 1452139f7f9bSDimitry Andric ++i; // Skip the '0' that terminates the "kind" string. 1453139f7f9bSDimitry Andric while (Record[i] != 0 && i != e) 1454139f7f9bSDimitry Andric ValStr += Record[i++]; 1455139f7f9bSDimitry Andric assert(Record[i] == 0 && "Value string not null terminated"); 1456139f7f9bSDimitry Andric } 1457139f7f9bSDimitry Andric 1458139f7f9bSDimitry Andric B.addAttribute(KindStr.str(), ValStr.str()); 1459139f7f9bSDimitry Andric } 1460139f7f9bSDimitry Andric } 1461139f7f9bSDimitry Andric 14627a7e6055SDimitry Andric MAttributeGroups[GrpID] = AttributeList::get(Context, Idx, B); 1463139f7f9bSDimitry Andric break; 1464139f7f9bSDimitry Andric } 1465f22ef01cSRoman Divacky } 1466f22ef01cSRoman Divacky } 1467f22ef01cSRoman Divacky } 1468f22ef01cSRoman Divacky 1469d88c1a5aSDimitry Andric Error BitcodeReader::parseTypeTable() { 147017a519f9SDimitry Andric if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW)) 14718f0fd8f6SDimitry Andric return error("Invalid record"); 1472f22ef01cSRoman Divacky 14738f0fd8f6SDimitry Andric return parseTypeTableBody(); 147417a519f9SDimitry Andric } 147517a519f9SDimitry Andric 1476d88c1a5aSDimitry Andric Error BitcodeReader::parseTypeTableBody() { 1477f22ef01cSRoman Divacky if (!TypeList.empty()) 14788f0fd8f6SDimitry Andric return error("Invalid multiple blocks"); 1479f22ef01cSRoman Divacky 1480f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 1481f22ef01cSRoman Divacky unsigned NumRecords = 0; 1482f22ef01cSRoman Divacky 148317a519f9SDimitry Andric SmallString<64> TypeName; 148417a519f9SDimitry Andric 1485f22ef01cSRoman Divacky // Read all the records for this type table. 1486d88c1a5aSDimitry Andric while (true) { 1487139f7f9bSDimitry Andric BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 1488139f7f9bSDimitry Andric 1489139f7f9bSDimitry Andric switch (Entry.Kind) { 1490139f7f9bSDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 1491139f7f9bSDimitry Andric case BitstreamEntry::Error: 14928f0fd8f6SDimitry Andric return error("Malformed block"); 1493139f7f9bSDimitry Andric case BitstreamEntry::EndBlock: 1494f22ef01cSRoman Divacky if (NumRecords != TypeList.size()) 14958f0fd8f6SDimitry Andric return error("Malformed block"); 1496d88c1a5aSDimitry Andric return Error::success(); 1497139f7f9bSDimitry Andric case BitstreamEntry::Record: 1498139f7f9bSDimitry Andric // The interesting case. 1499139f7f9bSDimitry Andric break; 1500f22ef01cSRoman Divacky } 1501f22ef01cSRoman Divacky 1502f22ef01cSRoman Divacky // Read a record. 1503f22ef01cSRoman Divacky Record.clear(); 150491bc56edSDimitry Andric Type *ResultTy = nullptr; 1505139f7f9bSDimitry Andric switch (Stream.readRecord(Entry.ID, Record)) { 1506f785676fSDimitry Andric default: 15078f0fd8f6SDimitry Andric return error("Invalid value"); 1508f22ef01cSRoman Divacky case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries] 1509f22ef01cSRoman Divacky // TYPE_CODE_NUMENTRY contains a count of the number of types in the 1510f22ef01cSRoman Divacky // type list. This allows us to reserve space. 1511f22ef01cSRoman Divacky if (Record.size() < 1) 15128f0fd8f6SDimitry Andric return error("Invalid record"); 151317a519f9SDimitry Andric TypeList.resize(Record[0]); 1514f22ef01cSRoman Divacky continue; 1515f22ef01cSRoman Divacky case bitc::TYPE_CODE_VOID: // VOID 1516f22ef01cSRoman Divacky ResultTy = Type::getVoidTy(Context); 1517f22ef01cSRoman Divacky break; 1518dff0c46cSDimitry Andric case bitc::TYPE_CODE_HALF: // HALF 1519dff0c46cSDimitry Andric ResultTy = Type::getHalfTy(Context); 1520dff0c46cSDimitry Andric break; 1521f22ef01cSRoman Divacky case bitc::TYPE_CODE_FLOAT: // FLOAT 1522f22ef01cSRoman Divacky ResultTy = Type::getFloatTy(Context); 1523f22ef01cSRoman Divacky break; 1524f22ef01cSRoman Divacky case bitc::TYPE_CODE_DOUBLE: // DOUBLE 1525f22ef01cSRoman Divacky ResultTy = Type::getDoubleTy(Context); 1526f22ef01cSRoman Divacky break; 1527f22ef01cSRoman Divacky case bitc::TYPE_CODE_X86_FP80: // X86_FP80 1528f22ef01cSRoman Divacky ResultTy = Type::getX86_FP80Ty(Context); 1529f22ef01cSRoman Divacky break; 1530f22ef01cSRoman Divacky case bitc::TYPE_CODE_FP128: // FP128 1531f22ef01cSRoman Divacky ResultTy = Type::getFP128Ty(Context); 1532f22ef01cSRoman Divacky break; 1533f22ef01cSRoman Divacky case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128 1534f22ef01cSRoman Divacky ResultTy = Type::getPPC_FP128Ty(Context); 1535f22ef01cSRoman Divacky break; 1536f22ef01cSRoman Divacky case bitc::TYPE_CODE_LABEL: // LABEL 1537f22ef01cSRoman Divacky ResultTy = Type::getLabelTy(Context); 1538f22ef01cSRoman Divacky break; 1539f22ef01cSRoman Divacky case bitc::TYPE_CODE_METADATA: // METADATA 1540f22ef01cSRoman Divacky ResultTy = Type::getMetadataTy(Context); 1541f22ef01cSRoman Divacky break; 15422754fe60SDimitry Andric case bitc::TYPE_CODE_X86_MMX: // X86_MMX 15432754fe60SDimitry Andric ResultTy = Type::getX86_MMXTy(Context); 15442754fe60SDimitry Andric break; 15457d523365SDimitry Andric case bitc::TYPE_CODE_TOKEN: // TOKEN 15467d523365SDimitry Andric ResultTy = Type::getTokenTy(Context); 15477d523365SDimitry Andric break; 1548ff0cc061SDimitry Andric case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width] 1549f22ef01cSRoman Divacky if (Record.size() < 1) 15508f0fd8f6SDimitry Andric return error("Invalid record"); 1551f22ef01cSRoman Divacky 1552ff0cc061SDimitry Andric uint64_t NumBits = Record[0]; 1553ff0cc061SDimitry Andric if (NumBits < IntegerType::MIN_INT_BITS || 1554ff0cc061SDimitry Andric NumBits > IntegerType::MAX_INT_BITS) 15558f0fd8f6SDimitry Andric return error("Bitwidth for integer type out of range"); 1556ff0cc061SDimitry Andric ResultTy = IntegerType::get(Context, NumBits); 1557f22ef01cSRoman Divacky break; 1558ff0cc061SDimitry Andric } 1559f22ef01cSRoman Divacky case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or 1560f22ef01cSRoman Divacky // [pointee type, address space] 1561f22ef01cSRoman Divacky if (Record.size() < 1) 15628f0fd8f6SDimitry Andric return error("Invalid record"); 1563f22ef01cSRoman Divacky unsigned AddressSpace = 0; 1564f22ef01cSRoman Divacky if (Record.size() == 2) 1565f22ef01cSRoman Divacky AddressSpace = Record[1]; 156617a519f9SDimitry Andric ResultTy = getTypeByID(Record[0]); 1567ff0cc061SDimitry Andric if (!ResultTy || 1568ff0cc061SDimitry Andric !PointerType::isValidElementType(ResultTy)) 15698f0fd8f6SDimitry Andric return error("Invalid type"); 157017a519f9SDimitry Andric ResultTy = PointerType::get(ResultTy, AddressSpace); 1571f22ef01cSRoman Divacky break; 1572f22ef01cSRoman Divacky } 1573dff0c46cSDimitry Andric case bitc::TYPE_CODE_FUNCTION_OLD: { 15747ae0e2c9SDimitry Andric // FIXME: attrid is dead, remove it in LLVM 4.0 1575f22ef01cSRoman Divacky // FUNCTION: [vararg, attrid, retty, paramty x N] 1576f22ef01cSRoman Divacky if (Record.size() < 3) 15778f0fd8f6SDimitry Andric return error("Invalid record"); 1578dff0c46cSDimitry Andric SmallVector<Type*, 8> ArgTys; 157917a519f9SDimitry Andric for (unsigned i = 3, e = Record.size(); i != e; ++i) { 158017a519f9SDimitry Andric if (Type *T = getTypeByID(Record[i])) 158117a519f9SDimitry Andric ArgTys.push_back(T); 158217a519f9SDimitry Andric else 1583f22ef01cSRoman Divacky break; 1584f22ef01cSRoman Divacky } 158517a519f9SDimitry Andric 158617a519f9SDimitry Andric ResultTy = getTypeByID(Record[2]); 158791bc56edSDimitry Andric if (!ResultTy || ArgTys.size() < Record.size()-3) 15888f0fd8f6SDimitry Andric return error("Invalid type"); 158917a519f9SDimitry Andric 159017a519f9SDimitry Andric ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); 159117a519f9SDimitry Andric break; 159217a519f9SDimitry Andric } 1593dff0c46cSDimitry Andric case bitc::TYPE_CODE_FUNCTION: { 1594dff0c46cSDimitry Andric // FUNCTION: [vararg, retty, paramty x N] 1595dff0c46cSDimitry Andric if (Record.size() < 2) 15968f0fd8f6SDimitry Andric return error("Invalid record"); 1597dff0c46cSDimitry Andric SmallVector<Type*, 8> ArgTys; 1598dff0c46cSDimitry Andric for (unsigned i = 2, e = Record.size(); i != e; ++i) { 1599ff0cc061SDimitry Andric if (Type *T = getTypeByID(Record[i])) { 1600ff0cc061SDimitry Andric if (!FunctionType::isValidArgumentType(T)) 16018f0fd8f6SDimitry Andric return error("Invalid function argument type"); 1602dff0c46cSDimitry Andric ArgTys.push_back(T); 1603ff0cc061SDimitry Andric } 1604dff0c46cSDimitry Andric else 1605dff0c46cSDimitry Andric break; 1606dff0c46cSDimitry Andric } 1607dff0c46cSDimitry Andric 1608dff0c46cSDimitry Andric ResultTy = getTypeByID(Record[1]); 160991bc56edSDimitry Andric if (!ResultTy || ArgTys.size() < Record.size()-2) 16108f0fd8f6SDimitry Andric return error("Invalid type"); 1611dff0c46cSDimitry Andric 1612dff0c46cSDimitry Andric ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); 1613dff0c46cSDimitry Andric break; 1614dff0c46cSDimitry Andric } 161517a519f9SDimitry Andric case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N] 1616f22ef01cSRoman Divacky if (Record.size() < 1) 16178f0fd8f6SDimitry Andric return error("Invalid record"); 1618dff0c46cSDimitry Andric SmallVector<Type*, 8> EltTys; 161917a519f9SDimitry Andric for (unsigned i = 1, e = Record.size(); i != e; ++i) { 162017a519f9SDimitry Andric if (Type *T = getTypeByID(Record[i])) 162117a519f9SDimitry Andric EltTys.push_back(T); 162217a519f9SDimitry Andric else 162317a519f9SDimitry Andric break; 162417a519f9SDimitry Andric } 162517a519f9SDimitry Andric if (EltTys.size() != Record.size()-1) 16268f0fd8f6SDimitry Andric return error("Invalid type"); 1627f22ef01cSRoman Divacky ResultTy = StructType::get(Context, EltTys, Record[0]); 1628f22ef01cSRoman Divacky break; 1629f22ef01cSRoman Divacky } 163017a519f9SDimitry Andric case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N] 16318f0fd8f6SDimitry Andric if (convertToString(Record, 0, TypeName)) 16328f0fd8f6SDimitry Andric return error("Invalid record"); 163317a519f9SDimitry Andric continue; 163417a519f9SDimitry Andric 163517a519f9SDimitry Andric case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N] 163617a519f9SDimitry Andric if (Record.size() < 1) 16378f0fd8f6SDimitry Andric return error("Invalid record"); 163817a519f9SDimitry Andric 163917a519f9SDimitry Andric if (NumRecords >= TypeList.size()) 16408f0fd8f6SDimitry Andric return error("Invalid TYPE table"); 164117a519f9SDimitry Andric 164217a519f9SDimitry Andric // Check to see if this was forward referenced, if so fill in the temp. 164317a519f9SDimitry Andric StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); 164417a519f9SDimitry Andric if (Res) { 164517a519f9SDimitry Andric Res->setName(TypeName); 164691bc56edSDimitry Andric TypeList[NumRecords] = nullptr; 164717a519f9SDimitry Andric } else // Otherwise, create a new struct. 164839d628a0SDimitry Andric Res = createIdentifiedStructType(Context, TypeName); 164917a519f9SDimitry Andric TypeName.clear(); 165017a519f9SDimitry Andric 165117a519f9SDimitry Andric SmallVector<Type*, 8> EltTys; 165217a519f9SDimitry Andric for (unsigned i = 1, e = Record.size(); i != e; ++i) { 165317a519f9SDimitry Andric if (Type *T = getTypeByID(Record[i])) 165417a519f9SDimitry Andric EltTys.push_back(T); 165517a519f9SDimitry Andric else 165617a519f9SDimitry Andric break; 165717a519f9SDimitry Andric } 165817a519f9SDimitry Andric if (EltTys.size() != Record.size()-1) 16598f0fd8f6SDimitry Andric return error("Invalid record"); 166017a519f9SDimitry Andric Res->setBody(EltTys, Record[0]); 166117a519f9SDimitry Andric ResultTy = Res; 166217a519f9SDimitry Andric break; 166317a519f9SDimitry Andric } 166417a519f9SDimitry Andric case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: [] 166517a519f9SDimitry Andric if (Record.size() != 1) 16668f0fd8f6SDimitry Andric return error("Invalid record"); 166717a519f9SDimitry Andric 166817a519f9SDimitry Andric if (NumRecords >= TypeList.size()) 16698f0fd8f6SDimitry Andric return error("Invalid TYPE table"); 167017a519f9SDimitry Andric 167117a519f9SDimitry Andric // Check to see if this was forward referenced, if so fill in the temp. 167217a519f9SDimitry Andric StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); 167317a519f9SDimitry Andric if (Res) { 167417a519f9SDimitry Andric Res->setName(TypeName); 167591bc56edSDimitry Andric TypeList[NumRecords] = nullptr; 167617a519f9SDimitry Andric } else // Otherwise, create a new struct with no body. 167739d628a0SDimitry Andric Res = createIdentifiedStructType(Context, TypeName); 167817a519f9SDimitry Andric TypeName.clear(); 167917a519f9SDimitry Andric ResultTy = Res; 168017a519f9SDimitry Andric break; 168117a519f9SDimitry Andric } 1682f22ef01cSRoman Divacky case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty] 1683f22ef01cSRoman Divacky if (Record.size() < 2) 16848f0fd8f6SDimitry Andric return error("Invalid record"); 1685ff0cc061SDimitry Andric ResultTy = getTypeByID(Record[1]); 1686ff0cc061SDimitry Andric if (!ResultTy || !ArrayType::isValidElementType(ResultTy)) 16878f0fd8f6SDimitry Andric return error("Invalid type"); 1688ff0cc061SDimitry Andric ResultTy = ArrayType::get(ResultTy, Record[0]); 1689f22ef01cSRoman Divacky break; 1690f22ef01cSRoman Divacky case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] 1691f22ef01cSRoman Divacky if (Record.size() < 2) 16928f0fd8f6SDimitry Andric return error("Invalid record"); 169397bc6c73SDimitry Andric if (Record[0] == 0) 16948f0fd8f6SDimitry Andric return error("Invalid vector length"); 1695ff0cc061SDimitry Andric ResultTy = getTypeByID(Record[1]); 1696ff0cc061SDimitry Andric if (!ResultTy || !StructType::isValidElementType(ResultTy)) 16978f0fd8f6SDimitry Andric return error("Invalid type"); 1698ff0cc061SDimitry Andric ResultTy = VectorType::get(ResultTy, Record[0]); 1699f22ef01cSRoman Divacky break; 1700f22ef01cSRoman Divacky } 1701f22ef01cSRoman Divacky 170217a519f9SDimitry Andric if (NumRecords >= TypeList.size()) 17038f0fd8f6SDimitry Andric return error("Invalid TYPE table"); 1704ff0cc061SDimitry Andric if (TypeList[NumRecords]) 17058f0fd8f6SDimitry Andric return error( 1706ff0cc061SDimitry Andric "Invalid TYPE table: Only named structs can be forward referenced"); 170717a519f9SDimitry Andric assert(ResultTy && "Didn't read a type?"); 170817a519f9SDimitry Andric TypeList[NumRecords++] = ResultTy; 1709f22ef01cSRoman Divacky } 1710f22ef01cSRoman Divacky } 171117a519f9SDimitry Andric 1712d88c1a5aSDimitry Andric Error BitcodeReader::parseOperandBundleTags() { 17137d523365SDimitry Andric if (Stream.EnterSubBlock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID)) 17147d523365SDimitry Andric return error("Invalid record"); 17157d523365SDimitry Andric 17167d523365SDimitry Andric if (!BundleTags.empty()) 17177d523365SDimitry Andric return error("Invalid multiple blocks"); 17187d523365SDimitry Andric 17197d523365SDimitry Andric SmallVector<uint64_t, 64> Record; 17207d523365SDimitry Andric 1721d88c1a5aSDimitry Andric while (true) { 17227d523365SDimitry Andric BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 17237d523365SDimitry Andric 17247d523365SDimitry Andric switch (Entry.Kind) { 17257d523365SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 17267d523365SDimitry Andric case BitstreamEntry::Error: 17277d523365SDimitry Andric return error("Malformed block"); 17287d523365SDimitry Andric case BitstreamEntry::EndBlock: 1729d88c1a5aSDimitry Andric return Error::success(); 17307d523365SDimitry Andric case BitstreamEntry::Record: 17317d523365SDimitry Andric // The interesting case. 17327d523365SDimitry Andric break; 17337d523365SDimitry Andric } 17347d523365SDimitry Andric 17357d523365SDimitry Andric // Tags are implicitly mapped to integers by their order. 17367d523365SDimitry Andric 17377d523365SDimitry Andric if (Stream.readRecord(Entry.ID, Record) != bitc::OPERAND_BUNDLE_TAG) 17387d523365SDimitry Andric return error("Invalid record"); 17397d523365SDimitry Andric 17407d523365SDimitry Andric // OPERAND_BUNDLE_TAG: [strchr x N] 17417d523365SDimitry Andric BundleTags.emplace_back(); 17427d523365SDimitry Andric if (convertToString(Record, 0, BundleTags.back())) 17437d523365SDimitry Andric return error("Invalid record"); 17447d523365SDimitry Andric Record.clear(); 17457d523365SDimitry Andric } 17467d523365SDimitry Andric } 17477d523365SDimitry Andric 17487d523365SDimitry Andric /// Associate a value with its name from the given index in the provided record. 1749d88c1a5aSDimitry Andric Expected<Value *> BitcodeReader::recordValue(SmallVectorImpl<uint64_t> &Record, 17507d523365SDimitry Andric unsigned NameIndex, Triple &TT) { 17517d523365SDimitry Andric SmallString<128> ValueName; 17527d523365SDimitry Andric if (convertToString(Record, NameIndex, ValueName)) 17537d523365SDimitry Andric return error("Invalid record"); 17547d523365SDimitry Andric unsigned ValueID = Record[0]; 17557d523365SDimitry Andric if (ValueID >= ValueList.size() || !ValueList[ValueID]) 17567d523365SDimitry Andric return error("Invalid record"); 17577d523365SDimitry Andric Value *V = ValueList[ValueID]; 17587d523365SDimitry Andric 17597d523365SDimitry Andric StringRef NameStr(ValueName.data(), ValueName.size()); 17607d523365SDimitry Andric if (NameStr.find_first_of(0) != StringRef::npos) 17617d523365SDimitry Andric return error("Invalid value name"); 17627d523365SDimitry Andric V->setName(NameStr); 17637d523365SDimitry Andric auto *GO = dyn_cast<GlobalObject>(V); 17647d523365SDimitry Andric if (GO) { 17657d523365SDimitry Andric if (GO->getComdat() == reinterpret_cast<Comdat *>(1)) { 17667d523365SDimitry Andric if (TT.isOSBinFormatMachO()) 17677d523365SDimitry Andric GO->setComdat(nullptr); 17687d523365SDimitry Andric else 17697d523365SDimitry Andric GO->setComdat(TheModule->getOrInsertComdat(V->getName())); 17707d523365SDimitry Andric } 17717d523365SDimitry Andric } 17727d523365SDimitry Andric return V; 17737d523365SDimitry Andric } 17747d523365SDimitry Andric 17753ca95b02SDimitry Andric /// Helper to note and return the current location, and jump to the given 17763ca95b02SDimitry Andric /// offset. 17773ca95b02SDimitry Andric static uint64_t jumpToValueSymbolTable(uint64_t Offset, 17783ca95b02SDimitry Andric BitstreamCursor &Stream) { 17797d523365SDimitry Andric // Save the current parsing location so we can jump back at the end 17807d523365SDimitry Andric // of the VST read. 17813ca95b02SDimitry Andric uint64_t CurrentBit = Stream.GetCurrentBitNo(); 17827d523365SDimitry Andric Stream.JumpToBit(Offset * 32); 17837d523365SDimitry Andric #ifndef NDEBUG 17847d523365SDimitry Andric // Do some checking if we are in debug mode. 17857d523365SDimitry Andric BitstreamEntry Entry = Stream.advance(); 17867d523365SDimitry Andric assert(Entry.Kind == BitstreamEntry::SubBlock); 17877d523365SDimitry Andric assert(Entry.ID == bitc::VALUE_SYMTAB_BLOCK_ID); 17887d523365SDimitry Andric #else 17897d523365SDimitry Andric // In NDEBUG mode ignore the output so we don't get an unused variable 17907d523365SDimitry Andric // warning. 17917d523365SDimitry Andric Stream.advance(); 17927d523365SDimitry Andric #endif 17933ca95b02SDimitry Andric return CurrentBit; 17947d523365SDimitry Andric } 17957d523365SDimitry Andric 17966bc11b14SDimitry Andric void BitcodeReader::setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta, 17976bc11b14SDimitry Andric Function *F, 17986bc11b14SDimitry Andric ArrayRef<uint64_t> Record) { 17996bc11b14SDimitry Andric // Note that we subtract 1 here because the offset is relative to one word 18006bc11b14SDimitry Andric // before the start of the identification or module block, which was 18016bc11b14SDimitry Andric // historically always the start of the regular bitcode header. 18026bc11b14SDimitry Andric uint64_t FuncWordOffset = Record[1] - 1; 18036bc11b14SDimitry Andric uint64_t FuncBitOffset = FuncWordOffset * 32; 18046bc11b14SDimitry Andric DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta; 18056bc11b14SDimitry Andric // Set the LastFunctionBlockBit to point to the last function block. 18066bc11b14SDimitry Andric // Later when parsing is resumed after function materialization, 18076bc11b14SDimitry Andric // we can simply skip that last function block. 18086bc11b14SDimitry Andric if (FuncBitOffset > LastFunctionBlockBit) 18096bc11b14SDimitry Andric LastFunctionBlockBit = FuncBitOffset; 18106bc11b14SDimitry Andric } 18116bc11b14SDimitry Andric 18126bc11b14SDimitry Andric /// Read a new-style GlobalValue symbol table. 18136bc11b14SDimitry Andric Error BitcodeReader::parseGlobalValueSymbolTable() { 18146bc11b14SDimitry Andric unsigned FuncBitcodeOffsetDelta = 18156bc11b14SDimitry Andric Stream.getAbbrevIDWidth() + bitc::BlockIDWidth; 18166bc11b14SDimitry Andric 18176bc11b14SDimitry Andric if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) 18186bc11b14SDimitry Andric return error("Invalid record"); 18196bc11b14SDimitry Andric 18206bc11b14SDimitry Andric SmallVector<uint64_t, 64> Record; 18216bc11b14SDimitry Andric while (true) { 18226bc11b14SDimitry Andric BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 18236bc11b14SDimitry Andric 18246bc11b14SDimitry Andric switch (Entry.Kind) { 18256bc11b14SDimitry Andric case BitstreamEntry::SubBlock: 18266bc11b14SDimitry Andric case BitstreamEntry::Error: 18276bc11b14SDimitry Andric return error("Malformed block"); 18286bc11b14SDimitry Andric case BitstreamEntry::EndBlock: 18296bc11b14SDimitry Andric return Error::success(); 18306bc11b14SDimitry Andric case BitstreamEntry::Record: 18316bc11b14SDimitry Andric break; 18326bc11b14SDimitry Andric } 18336bc11b14SDimitry Andric 18346bc11b14SDimitry Andric Record.clear(); 18356bc11b14SDimitry Andric switch (Stream.readRecord(Entry.ID, Record)) { 18366bc11b14SDimitry Andric case bitc::VST_CODE_FNENTRY: // [valueid, offset] 18376bc11b14SDimitry Andric setDeferredFunctionInfo(FuncBitcodeOffsetDelta, 18386bc11b14SDimitry Andric cast<Function>(ValueList[Record[0]]), Record); 18396bc11b14SDimitry Andric break; 18406bc11b14SDimitry Andric } 18416bc11b14SDimitry Andric } 18426bc11b14SDimitry Andric } 18436bc11b14SDimitry Andric 18443ca95b02SDimitry Andric /// Parse the value symbol table at either the current parsing location or 18453ca95b02SDimitry Andric /// at the given bit offset if provided. 1846d88c1a5aSDimitry Andric Error BitcodeReader::parseValueSymbolTable(uint64_t Offset) { 18473ca95b02SDimitry Andric uint64_t CurrentBit; 18483ca95b02SDimitry Andric // Pass in the Offset to distinguish between calling for the module-level 18493ca95b02SDimitry Andric // VST (where we want to jump to the VST offset) and the function-level 18503ca95b02SDimitry Andric // VST (where we don't). 18516bc11b14SDimitry Andric if (Offset > 0) { 18523ca95b02SDimitry Andric CurrentBit = jumpToValueSymbolTable(Offset, Stream); 18536bc11b14SDimitry Andric // If this module uses a string table, read this as a module-level VST. 18546bc11b14SDimitry Andric if (UseStrtab) { 18556bc11b14SDimitry Andric if (Error Err = parseGlobalValueSymbolTable()) 18566bc11b14SDimitry Andric return Err; 18576bc11b14SDimitry Andric Stream.JumpToBit(CurrentBit); 18586bc11b14SDimitry Andric return Error::success(); 18596bc11b14SDimitry Andric } 18606bc11b14SDimitry Andric // Otherwise, the VST will be in a similar format to a function-level VST, 18616bc11b14SDimitry Andric // and will contain symbol names. 18626bc11b14SDimitry Andric } 18633ca95b02SDimitry Andric 18647d523365SDimitry Andric // Compute the delta between the bitcode indices in the VST (the word offset 18657d523365SDimitry Andric // to the word-aligned ENTER_SUBBLOCK for the function block, and that 18667d523365SDimitry Andric // expected by the lazy reader. The reader's EnterSubBlock expects to have 18677d523365SDimitry Andric // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID 18687d523365SDimitry Andric // (size BlockIDWidth). Note that we access the stream's AbbrevID width here 18697d523365SDimitry Andric // just before entering the VST subblock because: 1) the EnterSubBlock 18707d523365SDimitry Andric // changes the AbbrevID width; 2) the VST block is nested within the same 18717d523365SDimitry Andric // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same 18727d523365SDimitry Andric // AbbrevID width before calling EnterSubBlock; and 3) when we want to 18737d523365SDimitry Andric // jump to the FUNCTION_BLOCK using this offset later, we don't want 18747d523365SDimitry Andric // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK. 18757d523365SDimitry Andric unsigned FuncBitcodeOffsetDelta = 18767d523365SDimitry Andric Stream.getAbbrevIDWidth() + bitc::BlockIDWidth; 18777d523365SDimitry Andric 1878f22ef01cSRoman Divacky if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) 18798f0fd8f6SDimitry Andric return error("Invalid record"); 1880f22ef01cSRoman Divacky 1881f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 1882f22ef01cSRoman Divacky 1883ff0cc061SDimitry Andric Triple TT(TheModule->getTargetTriple()); 1884ff0cc061SDimitry Andric 1885f22ef01cSRoman Divacky // Read all the records for this value table. 1886f22ef01cSRoman Divacky SmallString<128> ValueName; 1887d88c1a5aSDimitry Andric 1888d88c1a5aSDimitry Andric while (true) { 1889139f7f9bSDimitry Andric BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 1890f22ef01cSRoman Divacky 1891139f7f9bSDimitry Andric switch (Entry.Kind) { 1892139f7f9bSDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 1893139f7f9bSDimitry Andric case BitstreamEntry::Error: 18948f0fd8f6SDimitry Andric return error("Malformed block"); 1895139f7f9bSDimitry Andric case BitstreamEntry::EndBlock: 18967d523365SDimitry Andric if (Offset > 0) 18977d523365SDimitry Andric Stream.JumpToBit(CurrentBit); 1898d88c1a5aSDimitry Andric return Error::success(); 1899139f7f9bSDimitry Andric case BitstreamEntry::Record: 1900139f7f9bSDimitry Andric // The interesting case. 1901139f7f9bSDimitry Andric break; 1902f22ef01cSRoman Divacky } 1903f22ef01cSRoman Divacky 1904f22ef01cSRoman Divacky // Read a record. 1905f22ef01cSRoman Divacky Record.clear(); 1906139f7f9bSDimitry Andric switch (Stream.readRecord(Entry.ID, Record)) { 1907f22ef01cSRoman Divacky default: // Default behavior: unknown type. 1908f22ef01cSRoman Divacky break; 19093ca95b02SDimitry Andric case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N] 1910d88c1a5aSDimitry Andric Expected<Value *> ValOrErr = recordValue(Record, 1, TT); 1911d88c1a5aSDimitry Andric if (Error Err = ValOrErr.takeError()) 1912d88c1a5aSDimitry Andric return Err; 19137d523365SDimitry Andric ValOrErr.get(); 19147d523365SDimitry Andric break; 19157d523365SDimitry Andric } 19167d523365SDimitry Andric case bitc::VST_CODE_FNENTRY: { 19173ca95b02SDimitry Andric // VST_CODE_FNENTRY: [valueid, offset, namechar x N] 1918d88c1a5aSDimitry Andric Expected<Value *> ValOrErr = recordValue(Record, 2, TT); 1919d88c1a5aSDimitry Andric if (Error Err = ValOrErr.takeError()) 1920d88c1a5aSDimitry Andric return Err; 19217d523365SDimitry Andric Value *V = ValOrErr.get(); 1922f22ef01cSRoman Divacky 19237a7e6055SDimitry Andric // Ignore function offsets emitted for aliases of functions in older 19247a7e6055SDimitry Andric // versions of LLVM. 19256bc11b14SDimitry Andric if (auto *F = dyn_cast<Function>(V)) 19266bc11b14SDimitry Andric setDeferredFunctionInfo(FuncBitcodeOffsetDelta, F, Record); 1927f22ef01cSRoman Divacky break; 1928f22ef01cSRoman Divacky } 1929f22ef01cSRoman Divacky case bitc::VST_CODE_BBENTRY: { 19308f0fd8f6SDimitry Andric if (convertToString(Record, 1, ValueName)) 19318f0fd8f6SDimitry Andric return error("Invalid record"); 1932f22ef01cSRoman Divacky BasicBlock *BB = getBasicBlock(Record[0]); 193391bc56edSDimitry Andric if (!BB) 19348f0fd8f6SDimitry Andric return error("Invalid record"); 1935f22ef01cSRoman Divacky 1936f22ef01cSRoman Divacky BB->setName(StringRef(ValueName.data(), ValueName.size())); 1937f22ef01cSRoman Divacky ValueName.clear(); 1938f22ef01cSRoman Divacky break; 1939f22ef01cSRoman Divacky } 1940f22ef01cSRoman Divacky } 1941f22ef01cSRoman Divacky } 1942f22ef01cSRoman Divacky } 1943f22ef01cSRoman Divacky 19448f0fd8f6SDimitry Andric /// Decode a signed value stored with the sign bit in the LSB for dense VBR 19458f0fd8f6SDimitry Andric /// encoding. 19463861d79fSDimitry Andric uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) { 1947f22ef01cSRoman Divacky if ((V & 1) == 0) 1948f22ef01cSRoman Divacky return V >> 1; 1949f22ef01cSRoman Divacky if (V != 1) 1950f22ef01cSRoman Divacky return -(V >> 1); 1951f22ef01cSRoman Divacky // There is no such thing as -0 with integers. "-0" really means MININT. 1952f22ef01cSRoman Divacky return 1ULL << 63; 1953f22ef01cSRoman Divacky } 1954f22ef01cSRoman Divacky 19558f0fd8f6SDimitry Andric /// Resolve all of the initializers for global values and aliases that we can. 1956d88c1a5aSDimitry Andric Error BitcodeReader::resolveGlobalAndIndirectSymbolInits() { 1957f22ef01cSRoman Divacky std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist; 19583ca95b02SDimitry Andric std::vector<std::pair<GlobalIndirectSymbol*, unsigned> > 19593ca95b02SDimitry Andric IndirectSymbolInitWorklist; 1960f785676fSDimitry Andric std::vector<std::pair<Function*, unsigned> > FunctionPrefixWorklist; 196139d628a0SDimitry Andric std::vector<std::pair<Function*, unsigned> > FunctionPrologueWorklist; 19628f0fd8f6SDimitry Andric std::vector<std::pair<Function*, unsigned> > FunctionPersonalityFnWorklist; 1963f22ef01cSRoman Divacky 1964f22ef01cSRoman Divacky GlobalInitWorklist.swap(GlobalInits); 19653ca95b02SDimitry Andric IndirectSymbolInitWorklist.swap(IndirectSymbolInits); 1966f785676fSDimitry Andric FunctionPrefixWorklist.swap(FunctionPrefixes); 196739d628a0SDimitry Andric FunctionPrologueWorklist.swap(FunctionPrologues); 19688f0fd8f6SDimitry Andric FunctionPersonalityFnWorklist.swap(FunctionPersonalityFns); 1969f22ef01cSRoman Divacky 1970f22ef01cSRoman Divacky while (!GlobalInitWorklist.empty()) { 1971f22ef01cSRoman Divacky unsigned ValID = GlobalInitWorklist.back().second; 1972f22ef01cSRoman Divacky if (ValID >= ValueList.size()) { 1973f22ef01cSRoman Divacky // Not ready to resolve this yet, it requires something later in the file. 1974f22ef01cSRoman Divacky GlobalInits.push_back(GlobalInitWorklist.back()); 1975f22ef01cSRoman Divacky } else { 197691bc56edSDimitry Andric if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) 1977f22ef01cSRoman Divacky GlobalInitWorklist.back().first->setInitializer(C); 1978f22ef01cSRoman Divacky else 19798f0fd8f6SDimitry Andric return error("Expected a constant"); 1980f22ef01cSRoman Divacky } 1981f22ef01cSRoman Divacky GlobalInitWorklist.pop_back(); 1982f22ef01cSRoman Divacky } 1983f22ef01cSRoman Divacky 19843ca95b02SDimitry Andric while (!IndirectSymbolInitWorklist.empty()) { 19853ca95b02SDimitry Andric unsigned ValID = IndirectSymbolInitWorklist.back().second; 1986f22ef01cSRoman Divacky if (ValID >= ValueList.size()) { 19873ca95b02SDimitry Andric IndirectSymbolInits.push_back(IndirectSymbolInitWorklist.back()); 1988f22ef01cSRoman Divacky } else { 198997bc6c73SDimitry Andric Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]); 199097bc6c73SDimitry Andric if (!C) 19918f0fd8f6SDimitry Andric return error("Expected a constant"); 19923ca95b02SDimitry Andric GlobalIndirectSymbol *GIS = IndirectSymbolInitWorklist.back().first; 19933ca95b02SDimitry Andric if (isa<GlobalAlias>(GIS) && C->getType() != GIS->getType()) 19948f0fd8f6SDimitry Andric return error("Alias and aliasee types don't match"); 19953ca95b02SDimitry Andric GIS->setIndirectSymbol(C); 1996f22ef01cSRoman Divacky } 19973ca95b02SDimitry Andric IndirectSymbolInitWorklist.pop_back(); 1998f22ef01cSRoman Divacky } 1999f785676fSDimitry Andric 2000f785676fSDimitry Andric while (!FunctionPrefixWorklist.empty()) { 2001f785676fSDimitry Andric unsigned ValID = FunctionPrefixWorklist.back().second; 2002f785676fSDimitry Andric if (ValID >= ValueList.size()) { 2003f785676fSDimitry Andric FunctionPrefixes.push_back(FunctionPrefixWorklist.back()); 2004f785676fSDimitry Andric } else { 200591bc56edSDimitry Andric if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) 2006f785676fSDimitry Andric FunctionPrefixWorklist.back().first->setPrefixData(C); 2007f785676fSDimitry Andric else 20088f0fd8f6SDimitry Andric return error("Expected a constant"); 2009f785676fSDimitry Andric } 2010f785676fSDimitry Andric FunctionPrefixWorklist.pop_back(); 2011f785676fSDimitry Andric } 2012f785676fSDimitry Andric 201339d628a0SDimitry Andric while (!FunctionPrologueWorklist.empty()) { 201439d628a0SDimitry Andric unsigned ValID = FunctionPrologueWorklist.back().second; 201539d628a0SDimitry Andric if (ValID >= ValueList.size()) { 201639d628a0SDimitry Andric FunctionPrologues.push_back(FunctionPrologueWorklist.back()); 201739d628a0SDimitry Andric } else { 201839d628a0SDimitry Andric if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) 201939d628a0SDimitry Andric FunctionPrologueWorklist.back().first->setPrologueData(C); 202039d628a0SDimitry Andric else 20218f0fd8f6SDimitry Andric return error("Expected a constant"); 202239d628a0SDimitry Andric } 202339d628a0SDimitry Andric FunctionPrologueWorklist.pop_back(); 202439d628a0SDimitry Andric } 202539d628a0SDimitry Andric 20268f0fd8f6SDimitry Andric while (!FunctionPersonalityFnWorklist.empty()) { 20278f0fd8f6SDimitry Andric unsigned ValID = FunctionPersonalityFnWorklist.back().second; 20288f0fd8f6SDimitry Andric if (ValID >= ValueList.size()) { 20298f0fd8f6SDimitry Andric FunctionPersonalityFns.push_back(FunctionPersonalityFnWorklist.back()); 20308f0fd8f6SDimitry Andric } else { 20318f0fd8f6SDimitry Andric if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) 20328f0fd8f6SDimitry Andric FunctionPersonalityFnWorklist.back().first->setPersonalityFn(C); 20338f0fd8f6SDimitry Andric else 20348f0fd8f6SDimitry Andric return error("Expected a constant"); 20358f0fd8f6SDimitry Andric } 20368f0fd8f6SDimitry Andric FunctionPersonalityFnWorklist.pop_back(); 20378f0fd8f6SDimitry Andric } 20388f0fd8f6SDimitry Andric 2039d88c1a5aSDimitry Andric return Error::success(); 2040f22ef01cSRoman Divacky } 2041f22ef01cSRoman Divacky 20428f0fd8f6SDimitry Andric static APInt readWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) { 20437ae0e2c9SDimitry Andric SmallVector<uint64_t, 8> Words(Vals.size()); 2044d88c1a5aSDimitry Andric transform(Vals, Words.begin(), 20453861d79fSDimitry Andric BitcodeReader::decodeSignRotatedValue); 20467ae0e2c9SDimitry Andric 20477ae0e2c9SDimitry Andric return APInt(TypeBits, Words); 20487ae0e2c9SDimitry Andric } 20497ae0e2c9SDimitry Andric 2050d88c1a5aSDimitry Andric Error BitcodeReader::parseConstants() { 2051f22ef01cSRoman Divacky if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID)) 20528f0fd8f6SDimitry Andric return error("Invalid record"); 2053f22ef01cSRoman Divacky 2054f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 2055f22ef01cSRoman Divacky 2056f22ef01cSRoman Divacky // Read all the records for this value table. 20576122f3e6SDimitry Andric Type *CurTy = Type::getInt32Ty(Context); 2058f22ef01cSRoman Divacky unsigned NextCstNo = ValueList.size(); 2059d88c1a5aSDimitry Andric 2060d88c1a5aSDimitry Andric while (true) { 2061139f7f9bSDimitry Andric BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 2062139f7f9bSDimitry Andric 2063139f7f9bSDimitry Andric switch (Entry.Kind) { 2064139f7f9bSDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 2065139f7f9bSDimitry Andric case BitstreamEntry::Error: 20668f0fd8f6SDimitry Andric return error("Malformed block"); 2067139f7f9bSDimitry Andric case BitstreamEntry::EndBlock: 2068139f7f9bSDimitry Andric if (NextCstNo != ValueList.size()) 20693ca95b02SDimitry Andric return error("Invalid constant reference"); 2070139f7f9bSDimitry Andric 2071139f7f9bSDimitry Andric // Once all the constants have been read, go through and resolve forward 2072139f7f9bSDimitry Andric // references. 20738f0fd8f6SDimitry Andric ValueList.resolveConstantForwardRefs(); 2074d88c1a5aSDimitry Andric return Error::success(); 2075139f7f9bSDimitry Andric case BitstreamEntry::Record: 2076139f7f9bSDimitry Andric // The interesting case. 2077f22ef01cSRoman Divacky break; 2078f22ef01cSRoman Divacky } 2079f22ef01cSRoman Divacky 2080f22ef01cSRoman Divacky // Read a record. 2081f22ef01cSRoman Divacky Record.clear(); 20823ca95b02SDimitry Andric Type *VoidType = Type::getVoidTy(Context); 208391bc56edSDimitry Andric Value *V = nullptr; 2084139f7f9bSDimitry Andric unsigned BitCode = Stream.readRecord(Entry.ID, Record); 2085f22ef01cSRoman Divacky switch (BitCode) { 2086f22ef01cSRoman Divacky default: // Default behavior: unknown constant 2087f22ef01cSRoman Divacky case bitc::CST_CODE_UNDEF: // UNDEF 2088f22ef01cSRoman Divacky V = UndefValue::get(CurTy); 2089f22ef01cSRoman Divacky break; 2090f22ef01cSRoman Divacky case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid] 2091f22ef01cSRoman Divacky if (Record.empty()) 20928f0fd8f6SDimitry Andric return error("Invalid record"); 209391bc56edSDimitry Andric if (Record[0] >= TypeList.size() || !TypeList[Record[0]]) 20948f0fd8f6SDimitry Andric return error("Invalid record"); 20953ca95b02SDimitry Andric if (TypeList[Record[0]] == VoidType) 20963ca95b02SDimitry Andric return error("Invalid constant type"); 2097f22ef01cSRoman Divacky CurTy = TypeList[Record[0]]; 2098f22ef01cSRoman Divacky continue; // Skip the ValueList manipulation. 2099f22ef01cSRoman Divacky case bitc::CST_CODE_NULL: // NULL 2100f22ef01cSRoman Divacky V = Constant::getNullValue(CurTy); 2101f22ef01cSRoman Divacky break; 2102f22ef01cSRoman Divacky case bitc::CST_CODE_INTEGER: // INTEGER: [intval] 2103f22ef01cSRoman Divacky if (!CurTy->isIntegerTy() || Record.empty()) 21048f0fd8f6SDimitry Andric return error("Invalid record"); 21053861d79fSDimitry Andric V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0])); 2106f22ef01cSRoman Divacky break; 2107f22ef01cSRoman Divacky case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval] 2108f22ef01cSRoman Divacky if (!CurTy->isIntegerTy() || Record.empty()) 21098f0fd8f6SDimitry Andric return error("Invalid record"); 2110f22ef01cSRoman Divacky 21118f0fd8f6SDimitry Andric APInt VInt = 21128f0fd8f6SDimitry Andric readWideAPInt(Record, cast<IntegerType>(CurTy)->getBitWidth()); 21137ae0e2c9SDimitry Andric V = ConstantInt::get(Context, VInt); 21147ae0e2c9SDimitry Andric 2115f22ef01cSRoman Divacky break; 2116f22ef01cSRoman Divacky } 2117f22ef01cSRoman Divacky case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval] 2118f22ef01cSRoman Divacky if (Record.empty()) 21198f0fd8f6SDimitry Andric return error("Invalid record"); 2120dff0c46cSDimitry Andric if (CurTy->isHalfTy()) 2121d88c1a5aSDimitry Andric V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf(), 2122139f7f9bSDimitry Andric APInt(16, (uint16_t)Record[0]))); 2123dff0c46cSDimitry Andric else if (CurTy->isFloatTy()) 2124d88c1a5aSDimitry Andric V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle(), 2125139f7f9bSDimitry Andric APInt(32, (uint32_t)Record[0]))); 2126f22ef01cSRoman Divacky else if (CurTy->isDoubleTy()) 2127d88c1a5aSDimitry Andric V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble(), 2128139f7f9bSDimitry Andric APInt(64, Record[0]))); 2129f22ef01cSRoman Divacky else if (CurTy->isX86_FP80Ty()) { 2130f22ef01cSRoman Divacky // Bits are not stored the same way as a normal i80 APInt, compensate. 2131f22ef01cSRoman Divacky uint64_t Rearrange[2]; 2132f22ef01cSRoman Divacky Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16); 2133f22ef01cSRoman Divacky Rearrange[1] = Record[0] >> 48; 2134d88c1a5aSDimitry Andric V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended(), 2135139f7f9bSDimitry Andric APInt(80, Rearrange))); 2136f22ef01cSRoman Divacky } else if (CurTy->isFP128Ty()) 2137d88c1a5aSDimitry Andric V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad(), 2138139f7f9bSDimitry Andric APInt(128, Record))); 2139f22ef01cSRoman Divacky else if (CurTy->isPPC_FP128Ty()) 2140d88c1a5aSDimitry Andric V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble(), 2141139f7f9bSDimitry Andric APInt(128, Record))); 2142f22ef01cSRoman Divacky else 2143f22ef01cSRoman Divacky V = UndefValue::get(CurTy); 2144f22ef01cSRoman Divacky break; 2145f22ef01cSRoman Divacky } 2146f22ef01cSRoman Divacky 2147f22ef01cSRoman Divacky case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number] 2148f22ef01cSRoman Divacky if (Record.empty()) 21498f0fd8f6SDimitry Andric return error("Invalid record"); 2150f22ef01cSRoman Divacky 2151f22ef01cSRoman Divacky unsigned Size = Record.size(); 2152dff0c46cSDimitry Andric SmallVector<Constant*, 16> Elts; 2153f22ef01cSRoman Divacky 21546122f3e6SDimitry Andric if (StructType *STy = dyn_cast<StructType>(CurTy)) { 2155f22ef01cSRoman Divacky for (unsigned i = 0; i != Size; ++i) 2156f22ef01cSRoman Divacky Elts.push_back(ValueList.getConstantFwdRef(Record[i], 2157f22ef01cSRoman Divacky STy->getElementType(i))); 2158f22ef01cSRoman Divacky V = ConstantStruct::get(STy, Elts); 21596122f3e6SDimitry Andric } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) { 21606122f3e6SDimitry Andric Type *EltTy = ATy->getElementType(); 2161f22ef01cSRoman Divacky for (unsigned i = 0; i != Size; ++i) 2162f22ef01cSRoman Divacky Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); 2163f22ef01cSRoman Divacky V = ConstantArray::get(ATy, Elts); 21646122f3e6SDimitry Andric } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) { 21656122f3e6SDimitry Andric Type *EltTy = VTy->getElementType(); 2166f22ef01cSRoman Divacky for (unsigned i = 0; i != Size; ++i) 2167f22ef01cSRoman Divacky Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); 2168f22ef01cSRoman Divacky V = ConstantVector::get(Elts); 2169f22ef01cSRoman Divacky } else { 2170f22ef01cSRoman Divacky V = UndefValue::get(CurTy); 2171f22ef01cSRoman Divacky } 2172f22ef01cSRoman Divacky break; 2173f22ef01cSRoman Divacky } 2174dff0c46cSDimitry Andric case bitc::CST_CODE_STRING: // STRING: [values] 2175f22ef01cSRoman Divacky case bitc::CST_CODE_CSTRING: { // CSTRING: [values] 2176f22ef01cSRoman Divacky if (Record.empty()) 21778f0fd8f6SDimitry Andric return error("Invalid record"); 2178f22ef01cSRoman Divacky 21797ae0e2c9SDimitry Andric SmallString<16> Elts(Record.begin(), Record.end()); 2180dff0c46cSDimitry Andric V = ConstantDataArray::getString(Context, Elts, 2181dff0c46cSDimitry Andric BitCode == bitc::CST_CODE_CSTRING); 2182f22ef01cSRoman Divacky break; 2183f22ef01cSRoman Divacky } 2184dff0c46cSDimitry Andric case bitc::CST_CODE_DATA: {// DATA: [n x value] 2185dff0c46cSDimitry Andric if (Record.empty()) 21868f0fd8f6SDimitry Andric return error("Invalid record"); 2187dff0c46cSDimitry Andric 2188dff0c46cSDimitry Andric Type *EltTy = cast<SequentialType>(CurTy)->getElementType(); 2189dff0c46cSDimitry Andric if (EltTy->isIntegerTy(8)) { 2190dff0c46cSDimitry Andric SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end()); 2191dff0c46cSDimitry Andric if (isa<VectorType>(CurTy)) 2192dff0c46cSDimitry Andric V = ConstantDataVector::get(Context, Elts); 2193dff0c46cSDimitry Andric else 2194dff0c46cSDimitry Andric V = ConstantDataArray::get(Context, Elts); 2195dff0c46cSDimitry Andric } else if (EltTy->isIntegerTy(16)) { 2196dff0c46cSDimitry Andric SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); 2197dff0c46cSDimitry Andric if (isa<VectorType>(CurTy)) 2198dff0c46cSDimitry Andric V = ConstantDataVector::get(Context, Elts); 2199dff0c46cSDimitry Andric else 2200dff0c46cSDimitry Andric V = ConstantDataArray::get(Context, Elts); 2201dff0c46cSDimitry Andric } else if (EltTy->isIntegerTy(32)) { 2202dff0c46cSDimitry Andric SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); 2203dff0c46cSDimitry Andric if (isa<VectorType>(CurTy)) 2204dff0c46cSDimitry Andric V = ConstantDataVector::get(Context, Elts); 2205dff0c46cSDimitry Andric else 2206dff0c46cSDimitry Andric V = ConstantDataArray::get(Context, Elts); 2207dff0c46cSDimitry Andric } else if (EltTy->isIntegerTy(64)) { 2208dff0c46cSDimitry Andric SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); 2209dff0c46cSDimitry Andric if (isa<VectorType>(CurTy)) 2210dff0c46cSDimitry Andric V = ConstantDataVector::get(Context, Elts); 2211dff0c46cSDimitry Andric else 2212dff0c46cSDimitry Andric V = ConstantDataArray::get(Context, Elts); 2213444ed5c5SDimitry Andric } else if (EltTy->isHalfTy()) { 2214444ed5c5SDimitry Andric SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); 2215444ed5c5SDimitry Andric if (isa<VectorType>(CurTy)) 2216444ed5c5SDimitry Andric V = ConstantDataVector::getFP(Context, Elts); 2217444ed5c5SDimitry Andric else 2218444ed5c5SDimitry Andric V = ConstantDataArray::getFP(Context, Elts); 2219dff0c46cSDimitry Andric } else if (EltTy->isFloatTy()) { 2220444ed5c5SDimitry Andric SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); 2221dff0c46cSDimitry Andric if (isa<VectorType>(CurTy)) 2222444ed5c5SDimitry Andric V = ConstantDataVector::getFP(Context, Elts); 2223dff0c46cSDimitry Andric else 2224444ed5c5SDimitry Andric V = ConstantDataArray::getFP(Context, Elts); 2225dff0c46cSDimitry Andric } else if (EltTy->isDoubleTy()) { 2226444ed5c5SDimitry Andric SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); 2227dff0c46cSDimitry Andric if (isa<VectorType>(CurTy)) 2228444ed5c5SDimitry Andric V = ConstantDataVector::getFP(Context, Elts); 2229dff0c46cSDimitry Andric else 2230444ed5c5SDimitry Andric V = ConstantDataArray::getFP(Context, Elts); 2231dff0c46cSDimitry Andric } else { 22328f0fd8f6SDimitry Andric return error("Invalid type for value"); 2233dff0c46cSDimitry Andric } 2234dff0c46cSDimitry Andric break; 2235dff0c46cSDimitry Andric } 2236f22ef01cSRoman Divacky case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval] 2237f785676fSDimitry Andric if (Record.size() < 3) 22388f0fd8f6SDimitry Andric return error("Invalid record"); 22398f0fd8f6SDimitry Andric int Opc = getDecodedBinaryOpcode(Record[0], CurTy); 2240f22ef01cSRoman Divacky if (Opc < 0) { 2241f22ef01cSRoman Divacky V = UndefValue::get(CurTy); // Unknown binop. 2242f22ef01cSRoman Divacky } else { 2243f22ef01cSRoman Divacky Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy); 2244f22ef01cSRoman Divacky Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy); 2245f22ef01cSRoman Divacky unsigned Flags = 0; 2246f22ef01cSRoman Divacky if (Record.size() >= 4) { 2247f22ef01cSRoman Divacky if (Opc == Instruction::Add || 2248f22ef01cSRoman Divacky Opc == Instruction::Sub || 22492754fe60SDimitry Andric Opc == Instruction::Mul || 22502754fe60SDimitry Andric Opc == Instruction::Shl) { 2251f22ef01cSRoman Divacky if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP)) 2252f22ef01cSRoman Divacky Flags |= OverflowingBinaryOperator::NoSignedWrap; 2253f22ef01cSRoman Divacky if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) 2254f22ef01cSRoman Divacky Flags |= OverflowingBinaryOperator::NoUnsignedWrap; 22552754fe60SDimitry Andric } else if (Opc == Instruction::SDiv || 22562754fe60SDimitry Andric Opc == Instruction::UDiv || 22572754fe60SDimitry Andric Opc == Instruction::LShr || 22582754fe60SDimitry Andric Opc == Instruction::AShr) { 22592754fe60SDimitry Andric if (Record[3] & (1 << bitc::PEO_EXACT)) 2260f22ef01cSRoman Divacky Flags |= SDivOperator::IsExact; 2261f22ef01cSRoman Divacky } 2262f22ef01cSRoman Divacky } 2263f22ef01cSRoman Divacky V = ConstantExpr::get(Opc, LHS, RHS, Flags); 2264f22ef01cSRoman Divacky } 2265f22ef01cSRoman Divacky break; 2266f22ef01cSRoman Divacky } 2267f22ef01cSRoman Divacky case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval] 2268f785676fSDimitry Andric if (Record.size() < 3) 22698f0fd8f6SDimitry Andric return error("Invalid record"); 22708f0fd8f6SDimitry Andric int Opc = getDecodedCastOpcode(Record[0]); 2271f22ef01cSRoman Divacky if (Opc < 0) { 2272f22ef01cSRoman Divacky V = UndefValue::get(CurTy); // Unknown cast. 2273f22ef01cSRoman Divacky } else { 22746122f3e6SDimitry Andric Type *OpTy = getTypeByID(Record[1]); 2275f785676fSDimitry Andric if (!OpTy) 22768f0fd8f6SDimitry Andric return error("Invalid record"); 2277f22ef01cSRoman Divacky Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy); 2278f785676fSDimitry Andric V = UpgradeBitCastExpr(Opc, Op, CurTy); 2279f785676fSDimitry Andric if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy); 2280f22ef01cSRoman Divacky } 2281f22ef01cSRoman Divacky break; 2282f22ef01cSRoman Divacky } 2283d88c1a5aSDimitry Andric case bitc::CST_CODE_CE_INBOUNDS_GEP: // [ty, n x operands] 2284d88c1a5aSDimitry Andric case bitc::CST_CODE_CE_GEP: // [ty, n x operands] 2285d88c1a5aSDimitry Andric case bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX: { // [ty, flags, n x 2286d88c1a5aSDimitry Andric // operands] 2287ff0cc061SDimitry Andric unsigned OpNum = 0; 2288ff0cc061SDimitry Andric Type *PointeeType = nullptr; 2289d88c1a5aSDimitry Andric if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX || 2290d88c1a5aSDimitry Andric Record.size() % 2) 2291ff0cc061SDimitry Andric PointeeType = getTypeByID(Record[OpNum++]); 2292d88c1a5aSDimitry Andric 2293d88c1a5aSDimitry Andric bool InBounds = false; 2294d88c1a5aSDimitry Andric Optional<unsigned> InRangeIndex; 2295d88c1a5aSDimitry Andric if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX) { 2296d88c1a5aSDimitry Andric uint64_t Op = Record[OpNum++]; 2297d88c1a5aSDimitry Andric InBounds = Op & 1; 2298d88c1a5aSDimitry Andric InRangeIndex = Op >> 1; 2299d88c1a5aSDimitry Andric } else if (BitCode == bitc::CST_CODE_CE_INBOUNDS_GEP) 2300d88c1a5aSDimitry Andric InBounds = true; 2301d88c1a5aSDimitry Andric 2302f22ef01cSRoman Divacky SmallVector<Constant*, 16> Elts; 2303ff0cc061SDimitry Andric while (OpNum != Record.size()) { 2304ff0cc061SDimitry Andric Type *ElTy = getTypeByID(Record[OpNum++]); 2305f785676fSDimitry Andric if (!ElTy) 23068f0fd8f6SDimitry Andric return error("Invalid record"); 2307ff0cc061SDimitry Andric Elts.push_back(ValueList.getConstantFwdRef(Record[OpNum++], ElTy)); 2308f22ef01cSRoman Divacky } 2309ff0cc061SDimitry Andric 2310ff0cc061SDimitry Andric if (PointeeType && 2311ff0cc061SDimitry Andric PointeeType != 2312d88c1a5aSDimitry Andric cast<PointerType>(Elts[0]->getType()->getScalarType()) 2313ff0cc061SDimitry Andric ->getElementType()) 23148f0fd8f6SDimitry Andric return error("Explicit gep operator type does not match pointee type " 2315ff0cc061SDimitry Andric "of pointer operand"); 2316ff0cc061SDimitry Andric 23173ca95b02SDimitry Andric if (Elts.size() < 1) 23183ca95b02SDimitry Andric return error("Invalid gep with no operands"); 23193ca95b02SDimitry Andric 23206122f3e6SDimitry Andric ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end()); 2321ff0cc061SDimitry Andric V = ConstantExpr::getGetElementPtr(PointeeType, Elts[0], Indices, 2322d88c1a5aSDimitry Andric InBounds, InRangeIndex); 2323f22ef01cSRoman Divacky break; 2324f22ef01cSRoman Divacky } 2325f785676fSDimitry Andric case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#] 2326f785676fSDimitry Andric if (Record.size() < 3) 23278f0fd8f6SDimitry Andric return error("Invalid record"); 2328f785676fSDimitry Andric 2329f785676fSDimitry Andric Type *SelectorTy = Type::getInt1Ty(Context); 2330f785676fSDimitry Andric 23317d523365SDimitry Andric // The selector might be an i1 or an <n x i1> 23327d523365SDimitry Andric // Get the type from the ValueList before getting a forward ref. 2333f785676fSDimitry Andric if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) 23347d523365SDimitry Andric if (Value *V = ValueList[Record[0]]) 23357d523365SDimitry Andric if (SelectorTy != V->getType()) 23367d523365SDimitry Andric SelectorTy = VectorType::get(SelectorTy, VTy->getNumElements()); 2337f785676fSDimitry Andric 2338f785676fSDimitry Andric V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0], 2339f785676fSDimitry Andric SelectorTy), 2340f22ef01cSRoman Divacky ValueList.getConstantFwdRef(Record[1],CurTy), 2341f22ef01cSRoman Divacky ValueList.getConstantFwdRef(Record[2],CurTy)); 2342f22ef01cSRoman Divacky break; 2343f785676fSDimitry Andric } 234491bc56edSDimitry Andric case bitc::CST_CODE_CE_EXTRACTELT 234591bc56edSDimitry Andric : { // CE_EXTRACTELT: [opty, opval, opty, opval] 2346f785676fSDimitry Andric if (Record.size() < 3) 23478f0fd8f6SDimitry Andric return error("Invalid record"); 23486122f3e6SDimitry Andric VectorType *OpTy = 2349f22ef01cSRoman Divacky dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); 235091bc56edSDimitry Andric if (!OpTy) 23518f0fd8f6SDimitry Andric return error("Invalid record"); 2352f22ef01cSRoman Divacky Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); 235391bc56edSDimitry Andric Constant *Op1 = nullptr; 235491bc56edSDimitry Andric if (Record.size() == 4) { 235591bc56edSDimitry Andric Type *IdxTy = getTypeByID(Record[2]); 235691bc56edSDimitry Andric if (!IdxTy) 23578f0fd8f6SDimitry Andric return error("Invalid record"); 235891bc56edSDimitry Andric Op1 = ValueList.getConstantFwdRef(Record[3], IdxTy); 235991bc56edSDimitry Andric } else // TODO: Remove with llvm 4.0 236091bc56edSDimitry Andric Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context)); 236191bc56edSDimitry Andric if (!Op1) 23628f0fd8f6SDimitry Andric return error("Invalid record"); 2363f22ef01cSRoman Divacky V = ConstantExpr::getExtractElement(Op0, Op1); 2364f22ef01cSRoman Divacky break; 2365f22ef01cSRoman Divacky } 236691bc56edSDimitry Andric case bitc::CST_CODE_CE_INSERTELT 236791bc56edSDimitry Andric : { // CE_INSERTELT: [opval, opval, opty, opval] 23686122f3e6SDimitry Andric VectorType *OpTy = dyn_cast<VectorType>(CurTy); 236991bc56edSDimitry Andric if (Record.size() < 3 || !OpTy) 23708f0fd8f6SDimitry Andric return error("Invalid record"); 2371f22ef01cSRoman Divacky Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); 2372f22ef01cSRoman Divacky Constant *Op1 = ValueList.getConstantFwdRef(Record[1], 2373f22ef01cSRoman Divacky OpTy->getElementType()); 237491bc56edSDimitry Andric Constant *Op2 = nullptr; 237591bc56edSDimitry Andric if (Record.size() == 4) { 237691bc56edSDimitry Andric Type *IdxTy = getTypeByID(Record[2]); 237791bc56edSDimitry Andric if (!IdxTy) 23788f0fd8f6SDimitry Andric return error("Invalid record"); 237991bc56edSDimitry Andric Op2 = ValueList.getConstantFwdRef(Record[3], IdxTy); 238091bc56edSDimitry Andric } else // TODO: Remove with llvm 4.0 238191bc56edSDimitry Andric Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context)); 238291bc56edSDimitry Andric if (!Op2) 23838f0fd8f6SDimitry Andric return error("Invalid record"); 2384f22ef01cSRoman Divacky V = ConstantExpr::getInsertElement(Op0, Op1, Op2); 2385f22ef01cSRoman Divacky break; 2386f22ef01cSRoman Divacky } 2387f22ef01cSRoman Divacky case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval] 23886122f3e6SDimitry Andric VectorType *OpTy = dyn_cast<VectorType>(CurTy); 238991bc56edSDimitry Andric if (Record.size() < 3 || !OpTy) 23908f0fd8f6SDimitry Andric return error("Invalid record"); 2391f22ef01cSRoman Divacky Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); 2392f22ef01cSRoman Divacky Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy); 23936122f3e6SDimitry Andric Type *ShufTy = VectorType::get(Type::getInt32Ty(Context), 2394f22ef01cSRoman Divacky OpTy->getNumElements()); 2395f22ef01cSRoman Divacky Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy); 2396f22ef01cSRoman Divacky V = ConstantExpr::getShuffleVector(Op0, Op1, Op2); 2397f22ef01cSRoman Divacky break; 2398f22ef01cSRoman Divacky } 2399f22ef01cSRoman Divacky case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval] 24006122f3e6SDimitry Andric VectorType *RTy = dyn_cast<VectorType>(CurTy); 24016122f3e6SDimitry Andric VectorType *OpTy = 24022754fe60SDimitry Andric dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); 240391bc56edSDimitry Andric if (Record.size() < 4 || !RTy || !OpTy) 24048f0fd8f6SDimitry Andric return error("Invalid record"); 2405f22ef01cSRoman Divacky Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); 2406f22ef01cSRoman Divacky Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); 24076122f3e6SDimitry Andric Type *ShufTy = VectorType::get(Type::getInt32Ty(Context), 2408f22ef01cSRoman Divacky RTy->getNumElements()); 2409f22ef01cSRoman Divacky Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy); 2410f22ef01cSRoman Divacky V = ConstantExpr::getShuffleVector(Op0, Op1, Op2); 2411f22ef01cSRoman Divacky break; 2412f22ef01cSRoman Divacky } 2413f22ef01cSRoman Divacky case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred] 2414f785676fSDimitry Andric if (Record.size() < 4) 24158f0fd8f6SDimitry Andric return error("Invalid record"); 24166122f3e6SDimitry Andric Type *OpTy = getTypeByID(Record[0]); 241791bc56edSDimitry Andric if (!OpTy) 24188f0fd8f6SDimitry Andric return error("Invalid record"); 2419f22ef01cSRoman Divacky Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); 2420f22ef01cSRoman Divacky Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); 2421f22ef01cSRoman Divacky 2422f22ef01cSRoman Divacky if (OpTy->isFPOrFPVectorTy()) 2423f22ef01cSRoman Divacky V = ConstantExpr::getFCmp(Record[3], Op0, Op1); 2424f22ef01cSRoman Divacky else 2425f22ef01cSRoman Divacky V = ConstantExpr::getICmp(Record[3], Op0, Op1); 2426f22ef01cSRoman Divacky break; 2427f22ef01cSRoman Divacky } 24283861d79fSDimitry Andric // This maintains backward compatibility, pre-asm dialect keywords. 24293861d79fSDimitry Andric // FIXME: Remove with the 4.0 release. 24303861d79fSDimitry Andric case bitc::CST_CODE_INLINEASM_OLD: { 2431f785676fSDimitry Andric if (Record.size() < 2) 24328f0fd8f6SDimitry Andric return error("Invalid record"); 2433f22ef01cSRoman Divacky std::string AsmStr, ConstrStr; 2434f22ef01cSRoman Divacky bool HasSideEffects = Record[0] & 1; 2435f22ef01cSRoman Divacky bool IsAlignStack = Record[0] >> 1; 2436f22ef01cSRoman Divacky unsigned AsmStrSize = Record[1]; 2437f22ef01cSRoman Divacky if (2+AsmStrSize >= Record.size()) 24388f0fd8f6SDimitry Andric return error("Invalid record"); 2439f22ef01cSRoman Divacky unsigned ConstStrSize = Record[2+AsmStrSize]; 2440f22ef01cSRoman Divacky if (3+AsmStrSize+ConstStrSize > Record.size()) 24418f0fd8f6SDimitry Andric return error("Invalid record"); 2442f22ef01cSRoman Divacky 2443f22ef01cSRoman Divacky for (unsigned i = 0; i != AsmStrSize; ++i) 2444f22ef01cSRoman Divacky AsmStr += (char)Record[2+i]; 2445f22ef01cSRoman Divacky for (unsigned i = 0; i != ConstStrSize; ++i) 2446f22ef01cSRoman Divacky ConstrStr += (char)Record[3+AsmStrSize+i]; 24476122f3e6SDimitry Andric PointerType *PTy = cast<PointerType>(CurTy); 2448f22ef01cSRoman Divacky V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()), 2449f22ef01cSRoman Divacky AsmStr, ConstrStr, HasSideEffects, IsAlignStack); 2450f22ef01cSRoman Divacky break; 2451f22ef01cSRoman Divacky } 24523861d79fSDimitry Andric // This version adds support for the asm dialect keywords (e.g., 24533861d79fSDimitry Andric // inteldialect). 24543861d79fSDimitry Andric case bitc::CST_CODE_INLINEASM: { 2455f785676fSDimitry Andric if (Record.size() < 2) 24568f0fd8f6SDimitry Andric return error("Invalid record"); 24573861d79fSDimitry Andric std::string AsmStr, ConstrStr; 24583861d79fSDimitry Andric bool HasSideEffects = Record[0] & 1; 24593861d79fSDimitry Andric bool IsAlignStack = (Record[0] >> 1) & 1; 24603861d79fSDimitry Andric unsigned AsmDialect = Record[0] >> 2; 24613861d79fSDimitry Andric unsigned AsmStrSize = Record[1]; 24623861d79fSDimitry Andric if (2+AsmStrSize >= Record.size()) 24638f0fd8f6SDimitry Andric return error("Invalid record"); 24643861d79fSDimitry Andric unsigned ConstStrSize = Record[2+AsmStrSize]; 24653861d79fSDimitry Andric if (3+AsmStrSize+ConstStrSize > Record.size()) 24668f0fd8f6SDimitry Andric return error("Invalid record"); 24673861d79fSDimitry Andric 24683861d79fSDimitry Andric for (unsigned i = 0; i != AsmStrSize; ++i) 24693861d79fSDimitry Andric AsmStr += (char)Record[2+i]; 24703861d79fSDimitry Andric for (unsigned i = 0; i != ConstStrSize; ++i) 24713861d79fSDimitry Andric ConstrStr += (char)Record[3+AsmStrSize+i]; 24723861d79fSDimitry Andric PointerType *PTy = cast<PointerType>(CurTy); 24733861d79fSDimitry Andric V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()), 24743861d79fSDimitry Andric AsmStr, ConstrStr, HasSideEffects, IsAlignStack, 24753861d79fSDimitry Andric InlineAsm::AsmDialect(AsmDialect)); 24763861d79fSDimitry Andric break; 24773861d79fSDimitry Andric } 2478f22ef01cSRoman Divacky case bitc::CST_CODE_BLOCKADDRESS:{ 2479f785676fSDimitry Andric if (Record.size() < 3) 24808f0fd8f6SDimitry Andric return error("Invalid record"); 24816122f3e6SDimitry Andric Type *FnTy = getTypeByID(Record[0]); 248291bc56edSDimitry Andric if (!FnTy) 24838f0fd8f6SDimitry Andric return error("Invalid record"); 2484f22ef01cSRoman Divacky Function *Fn = 2485f22ef01cSRoman Divacky dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy)); 248691bc56edSDimitry Andric if (!Fn) 24878f0fd8f6SDimitry Andric return error("Invalid record"); 248839d628a0SDimitry Andric 24893861d79fSDimitry Andric // If the function is already parsed we can insert the block address right 24903861d79fSDimitry Andric // away. 249139d628a0SDimitry Andric BasicBlock *BB; 249239d628a0SDimitry Andric unsigned BBID = Record[2]; 249339d628a0SDimitry Andric if (!BBID) 249439d628a0SDimitry Andric // Invalid reference to entry block. 24958f0fd8f6SDimitry Andric return error("Invalid ID"); 24963861d79fSDimitry Andric if (!Fn->empty()) { 24973861d79fSDimitry Andric Function::iterator BBI = Fn->begin(), BBE = Fn->end(); 249839d628a0SDimitry Andric for (size_t I = 0, E = BBID; I != E; ++I) { 24993861d79fSDimitry Andric if (BBI == BBE) 25008f0fd8f6SDimitry Andric return error("Invalid ID"); 25013861d79fSDimitry Andric ++BBI; 25023861d79fSDimitry Andric } 25037d523365SDimitry Andric BB = &*BBI; 25043861d79fSDimitry Andric } else { 25053861d79fSDimitry Andric // Otherwise insert a placeholder and remember it so it can be inserted 25063861d79fSDimitry Andric // when the function is parsed. 250739d628a0SDimitry Andric auto &FwdBBs = BasicBlockFwdRefs[Fn]; 250839d628a0SDimitry Andric if (FwdBBs.empty()) 250939d628a0SDimitry Andric BasicBlockFwdRefQueue.push_back(Fn); 251039d628a0SDimitry Andric if (FwdBBs.size() < BBID + 1) 251139d628a0SDimitry Andric FwdBBs.resize(BBID + 1); 251239d628a0SDimitry Andric if (!FwdBBs[BBID]) 251339d628a0SDimitry Andric FwdBBs[BBID] = BasicBlock::Create(Context); 251439d628a0SDimitry Andric BB = FwdBBs[BBID]; 25153861d79fSDimitry Andric } 251639d628a0SDimitry Andric V = BlockAddress::get(Fn, BB); 2517f22ef01cSRoman Divacky break; 2518f22ef01cSRoman Divacky } 2519f22ef01cSRoman Divacky } 2520f22ef01cSRoman Divacky 25218f0fd8f6SDimitry Andric ValueList.assignValue(V, NextCstNo); 2522f22ef01cSRoman Divacky ++NextCstNo; 2523f22ef01cSRoman Divacky } 2524f22ef01cSRoman Divacky } 2525f22ef01cSRoman Divacky 2526d88c1a5aSDimitry Andric Error BitcodeReader::parseUseLists() { 2527dff0c46cSDimitry Andric if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID)) 25288f0fd8f6SDimitry Andric return error("Invalid record"); 2529dff0c46cSDimitry Andric 2530dff0c46cSDimitry Andric // Read all the records. 253139d628a0SDimitry Andric SmallVector<uint64_t, 64> Record; 2532d88c1a5aSDimitry Andric 2533d88c1a5aSDimitry Andric while (true) { 2534139f7f9bSDimitry Andric BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 2535139f7f9bSDimitry Andric 2536139f7f9bSDimitry Andric switch (Entry.Kind) { 2537139f7f9bSDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 2538139f7f9bSDimitry Andric case BitstreamEntry::Error: 25398f0fd8f6SDimitry Andric return error("Malformed block"); 2540139f7f9bSDimitry Andric case BitstreamEntry::EndBlock: 2541d88c1a5aSDimitry Andric return Error::success(); 2542139f7f9bSDimitry Andric case BitstreamEntry::Record: 2543139f7f9bSDimitry Andric // The interesting case. 2544139f7f9bSDimitry Andric break; 2545dff0c46cSDimitry Andric } 2546dff0c46cSDimitry Andric 2547dff0c46cSDimitry Andric // Read a use list record. 2548dff0c46cSDimitry Andric Record.clear(); 254939d628a0SDimitry Andric bool IsBB = false; 2550139f7f9bSDimitry Andric switch (Stream.readRecord(Entry.ID, Record)) { 2551dff0c46cSDimitry Andric default: // Default behavior: unknown type. 2552dff0c46cSDimitry Andric break; 255339d628a0SDimitry Andric case bitc::USELIST_CODE_BB: 255439d628a0SDimitry Andric IsBB = true; 2555d88c1a5aSDimitry Andric LLVM_FALLTHROUGH; 255639d628a0SDimitry Andric case bitc::USELIST_CODE_DEFAULT: { 2557dff0c46cSDimitry Andric unsigned RecordLength = Record.size(); 255839d628a0SDimitry Andric if (RecordLength < 3) 255939d628a0SDimitry Andric // Records should have at least an ID and two indexes. 25608f0fd8f6SDimitry Andric return error("Invalid record"); 256139d628a0SDimitry Andric unsigned ID = Record.back(); 256239d628a0SDimitry Andric Record.pop_back(); 256339d628a0SDimitry Andric 256439d628a0SDimitry Andric Value *V; 256539d628a0SDimitry Andric if (IsBB) { 256639d628a0SDimitry Andric assert(ID < FunctionBBs.size() && "Basic block not found"); 256739d628a0SDimitry Andric V = FunctionBBs[ID]; 256839d628a0SDimitry Andric } else 256939d628a0SDimitry Andric V = ValueList[ID]; 257039d628a0SDimitry Andric unsigned NumUses = 0; 257139d628a0SDimitry Andric SmallDenseMap<const Use *, unsigned, 16> Order; 25727d523365SDimitry Andric for (const Use &U : V->materialized_uses()) { 257339d628a0SDimitry Andric if (++NumUses > Record.size()) 257439d628a0SDimitry Andric break; 257539d628a0SDimitry Andric Order[&U] = Record[NumUses - 1]; 257639d628a0SDimitry Andric } 257739d628a0SDimitry Andric if (Order.size() != Record.size() || NumUses > Record.size()) 257839d628a0SDimitry Andric // Mismatches can happen if the functions are being materialized lazily 257939d628a0SDimitry Andric // (out-of-order), or a value has been upgraded. 258039d628a0SDimitry Andric break; 258139d628a0SDimitry Andric 258239d628a0SDimitry Andric V->sortUseList([&](const Use &L, const Use &R) { 258339d628a0SDimitry Andric return Order.lookup(&L) < Order.lookup(&R); 258439d628a0SDimitry Andric }); 2585dff0c46cSDimitry Andric break; 2586dff0c46cSDimitry Andric } 2587dff0c46cSDimitry Andric } 2588dff0c46cSDimitry Andric } 2589dff0c46cSDimitry Andric } 2590dff0c46cSDimitry Andric 2591ff0cc061SDimitry Andric /// When we see the block for metadata, remember where it is and then skip it. 2592ff0cc061SDimitry Andric /// This lets us lazily deserialize the metadata. 2593d88c1a5aSDimitry Andric Error BitcodeReader::rememberAndSkipMetadata() { 2594ff0cc061SDimitry Andric // Save the current stream state. 2595ff0cc061SDimitry Andric uint64_t CurBit = Stream.GetCurrentBitNo(); 2596ff0cc061SDimitry Andric DeferredMetadataInfo.push_back(CurBit); 2597ff0cc061SDimitry Andric 2598ff0cc061SDimitry Andric // Skip over the block for now. 2599ff0cc061SDimitry Andric if (Stream.SkipBlock()) 26008f0fd8f6SDimitry Andric return error("Invalid record"); 2601d88c1a5aSDimitry Andric return Error::success(); 2602ff0cc061SDimitry Andric } 2603ff0cc061SDimitry Andric 2604d88c1a5aSDimitry Andric Error BitcodeReader::materializeMetadata() { 2605ff0cc061SDimitry Andric for (uint64_t BitPos : DeferredMetadataInfo) { 2606ff0cc061SDimitry Andric // Move the bit stream to the saved position. 2607ff0cc061SDimitry Andric Stream.JumpToBit(BitPos); 2608d88c1a5aSDimitry Andric if (Error Err = MDLoader->parseModuleMetadata()) 2609d88c1a5aSDimitry Andric return Err; 2610ff0cc061SDimitry Andric } 261124d58133SDimitry Andric 261224d58133SDimitry Andric // Upgrade "Linker Options" module flag to "llvm.linker.options" module-level 261324d58133SDimitry Andric // metadata. 261424d58133SDimitry Andric if (Metadata *Val = TheModule->getModuleFlag("Linker Options")) { 261524d58133SDimitry Andric NamedMDNode *LinkerOpts = 261624d58133SDimitry Andric TheModule->getOrInsertNamedMetadata("llvm.linker.options"); 261724d58133SDimitry Andric for (const MDOperand &MDOptions : cast<MDNode>(Val)->operands()) 261824d58133SDimitry Andric LinkerOpts->addOperand(cast<MDNode>(MDOptions)); 261924d58133SDimitry Andric } 262024d58133SDimitry Andric 2621ff0cc061SDimitry Andric DeferredMetadataInfo.clear(); 2622d88c1a5aSDimitry Andric return Error::success(); 2623ff0cc061SDimitry Andric } 2624ff0cc061SDimitry Andric 2625ff0cc061SDimitry Andric void BitcodeReader::setStripDebugInfo() { StripDebugInfo = true; } 2626ff0cc061SDimitry Andric 26278f0fd8f6SDimitry Andric /// When we see the block for a function body, remember where it is and then 26288f0fd8f6SDimitry Andric /// skip it. This lets us lazily deserialize the functions. 2629d88c1a5aSDimitry Andric Error BitcodeReader::rememberAndSkipFunctionBody() { 2630f22ef01cSRoman Divacky // Get the function we are talking about. 2631f22ef01cSRoman Divacky if (FunctionsWithBodies.empty()) 26328f0fd8f6SDimitry Andric return error("Insufficient function protos"); 2633f22ef01cSRoman Divacky 2634f22ef01cSRoman Divacky Function *Fn = FunctionsWithBodies.back(); 2635f22ef01cSRoman Divacky FunctionsWithBodies.pop_back(); 2636f22ef01cSRoman Divacky 2637f22ef01cSRoman Divacky // Save the current stream state. 2638f22ef01cSRoman Divacky uint64_t CurBit = Stream.GetCurrentBitNo(); 26397d523365SDimitry Andric assert( 26407d523365SDimitry Andric (DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) && 26417d523365SDimitry Andric "Mismatch between VST and scanned function offsets"); 2642f22ef01cSRoman Divacky DeferredFunctionInfo[Fn] = CurBit; 2643f22ef01cSRoman Divacky 2644f22ef01cSRoman Divacky // Skip over the function block for now. 2645f22ef01cSRoman Divacky if (Stream.SkipBlock()) 26468f0fd8f6SDimitry Andric return error("Invalid record"); 2647d88c1a5aSDimitry Andric return Error::success(); 2648f22ef01cSRoman Divacky } 2649f22ef01cSRoman Divacky 2650d88c1a5aSDimitry Andric Error BitcodeReader::globalCleanup() { 2651f22ef01cSRoman Divacky // Patch the initializers for globals and aliases up. 2652d88c1a5aSDimitry Andric if (Error Err = resolveGlobalAndIndirectSymbolInits()) 2653d88c1a5aSDimitry Andric return Err; 26543ca95b02SDimitry Andric if (!GlobalInits.empty() || !IndirectSymbolInits.empty()) 26558f0fd8f6SDimitry Andric return error("Malformed global initializer set"); 2656f22ef01cSRoman Divacky 2657f22ef01cSRoman Divacky // Look for intrinsic functions which need to be upgraded at some point 26588f0fd8f6SDimitry Andric for (Function &F : *TheModule) { 26596bc11b14SDimitry Andric MDLoader->upgradeDebugIntrinsics(F); 2660f22ef01cSRoman Divacky Function *NewFn; 26618f0fd8f6SDimitry Andric if (UpgradeIntrinsicFunction(&F, NewFn)) 26623dac3a9bSDimitry Andric UpgradedIntrinsics[&F] = NewFn; 26633ca95b02SDimitry Andric else if (auto Remangled = Intrinsic::remangleIntrinsicFunction(&F)) 26643ca95b02SDimitry Andric // Some types could be renamed during loading if several modules are 26653ca95b02SDimitry Andric // loaded in the same LLVMContext (LTO scenario). In this case we should 26663ca95b02SDimitry Andric // remangle intrinsics names as well. 26673ca95b02SDimitry Andric RemangledIntrinsics[&F] = Remangled.getValue(); 2668f22ef01cSRoman Divacky } 2669f22ef01cSRoman Divacky 2670e580952dSDimitry Andric // Look for global variables which need to be renamed. 26718f0fd8f6SDimitry Andric for (GlobalVariable &GV : TheModule->globals()) 26728f0fd8f6SDimitry Andric UpgradeGlobalVariable(&GV); 267391bc56edSDimitry Andric 2674f22ef01cSRoman Divacky // Force deallocation of memory for these vectors to favor the client that 2675f22ef01cSRoman Divacky // want lazy deserialization. 2676f22ef01cSRoman Divacky std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits); 26773ca95b02SDimitry Andric std::vector<std::pair<GlobalIndirectSymbol*, unsigned> >().swap( 26783ca95b02SDimitry Andric IndirectSymbolInits); 2679d88c1a5aSDimitry Andric return Error::success(); 2680f22ef01cSRoman Divacky } 2681f22ef01cSRoman Divacky 26827d523365SDimitry Andric /// Support for lazy parsing of function bodies. This is required if we 26837d523365SDimitry Andric /// either have an old bitcode file without a VST forward declaration record, 26847d523365SDimitry Andric /// or if we have an anonymous function being materialized, since anonymous 26857d523365SDimitry Andric /// functions do not have a name and are therefore not in the VST. 2686d88c1a5aSDimitry Andric Error BitcodeReader::rememberAndSkipFunctionBodies() { 2687dff0c46cSDimitry Andric Stream.JumpToBit(NextUnreadBit); 26887d523365SDimitry Andric 26897d523365SDimitry Andric if (Stream.AtEndOfStream()) 26907d523365SDimitry Andric return error("Could not find function in stream"); 26917d523365SDimitry Andric 26927d523365SDimitry Andric if (!SeenFirstFunctionBody) 26937d523365SDimitry Andric return error("Trying to materialize functions before seeing function blocks"); 26947d523365SDimitry Andric 26957d523365SDimitry Andric // An old bitcode file with the symbol table at the end would have 26967d523365SDimitry Andric // finished the parse greedily. 26977d523365SDimitry Andric assert(SeenValueSymbolTable); 26987d523365SDimitry Andric 26997d523365SDimitry Andric SmallVector<uint64_t, 64> Record; 27007d523365SDimitry Andric 2701d88c1a5aSDimitry Andric while (true) { 27027d523365SDimitry Andric BitstreamEntry Entry = Stream.advance(); 27037d523365SDimitry Andric switch (Entry.Kind) { 27047d523365SDimitry Andric default: 27057d523365SDimitry Andric return error("Expect SubBlock"); 27067d523365SDimitry Andric case BitstreamEntry::SubBlock: 27077d523365SDimitry Andric switch (Entry.ID) { 27087d523365SDimitry Andric default: 27097d523365SDimitry Andric return error("Expect function block"); 27107d523365SDimitry Andric case bitc::FUNCTION_BLOCK_ID: 2711d88c1a5aSDimitry Andric if (Error Err = rememberAndSkipFunctionBody()) 2712d88c1a5aSDimitry Andric return Err; 27137d523365SDimitry Andric NextUnreadBit = Stream.GetCurrentBitNo(); 2714d88c1a5aSDimitry Andric return Error::success(); 27157d523365SDimitry Andric } 27167d523365SDimitry Andric } 27177d523365SDimitry Andric } 27187d523365SDimitry Andric } 27197d523365SDimitry Andric 2720d88c1a5aSDimitry Andric bool BitcodeReaderBase::readBlockInfo() { 2721d88c1a5aSDimitry Andric Optional<BitstreamBlockInfo> NewBlockInfo = Stream.ReadBlockInfoBlock(); 2722d88c1a5aSDimitry Andric if (!NewBlockInfo) 2723d88c1a5aSDimitry Andric return true; 2724d88c1a5aSDimitry Andric BlockInfo = std::move(*NewBlockInfo); 2725d88c1a5aSDimitry Andric return false; 27267d523365SDimitry Andric } 27277d523365SDimitry Andric 27287a7e6055SDimitry Andric Error BitcodeReader::parseComdatRecord(ArrayRef<uint64_t> Record) { 27296bc11b14SDimitry Andric // v1: [selection_kind, name] 27306bc11b14SDimitry Andric // v2: [strtab_offset, strtab_size, selection_kind] 27316bc11b14SDimitry Andric StringRef Name; 27326bc11b14SDimitry Andric std::tie(Name, Record) = readNameFromStrtab(Record); 27336bc11b14SDimitry Andric 27346bc11b14SDimitry Andric if (Record.size() < 1) 27357a7e6055SDimitry Andric return error("Invalid record"); 27367a7e6055SDimitry Andric Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]); 27376bc11b14SDimitry Andric std::string OldFormatName; 27386bc11b14SDimitry Andric if (!UseStrtab) { 27396bc11b14SDimitry Andric if (Record.size() < 2) 27406bc11b14SDimitry Andric return error("Invalid record"); 27417a7e6055SDimitry Andric unsigned ComdatNameSize = Record[1]; 27426bc11b14SDimitry Andric OldFormatName.reserve(ComdatNameSize); 27437a7e6055SDimitry Andric for (unsigned i = 0; i != ComdatNameSize; ++i) 27446bc11b14SDimitry Andric OldFormatName += (char)Record[2 + i]; 27456bc11b14SDimitry Andric Name = OldFormatName; 27466bc11b14SDimitry Andric } 27477a7e6055SDimitry Andric Comdat *C = TheModule->getOrInsertComdat(Name); 27487a7e6055SDimitry Andric C->setSelectionKind(SK); 27497a7e6055SDimitry Andric ComdatList.push_back(C); 27507a7e6055SDimitry Andric return Error::success(); 27517a7e6055SDimitry Andric } 27527a7e6055SDimitry Andric 27537a7e6055SDimitry Andric Error BitcodeReader::parseGlobalVarRecord(ArrayRef<uint64_t> Record) { 27546bc11b14SDimitry Andric // v1: [pointer type, isconst, initid, linkage, alignment, section, 27557a7e6055SDimitry Andric // visibility, threadlocal, unnamed_addr, externally_initialized, 27565517e702SDimitry Andric // dllstorageclass, comdat, attributes] (name in VST) 27576bc11b14SDimitry Andric // v2: [strtab_offset, strtab_size, v1] 27586bc11b14SDimitry Andric StringRef Name; 27596bc11b14SDimitry Andric std::tie(Name, Record) = readNameFromStrtab(Record); 27606bc11b14SDimitry Andric 27617a7e6055SDimitry Andric if (Record.size() < 6) 27627a7e6055SDimitry Andric return error("Invalid record"); 27637a7e6055SDimitry Andric Type *Ty = getTypeByID(Record[0]); 27647a7e6055SDimitry Andric if (!Ty) 27657a7e6055SDimitry Andric return error("Invalid record"); 27667a7e6055SDimitry Andric bool isConstant = Record[1] & 1; 27677a7e6055SDimitry Andric bool explicitType = Record[1] & 2; 27687a7e6055SDimitry Andric unsigned AddressSpace; 27697a7e6055SDimitry Andric if (explicitType) { 27707a7e6055SDimitry Andric AddressSpace = Record[1] >> 2; 27717a7e6055SDimitry Andric } else { 27727a7e6055SDimitry Andric if (!Ty->isPointerTy()) 27737a7e6055SDimitry Andric return error("Invalid type for value"); 27747a7e6055SDimitry Andric AddressSpace = cast<PointerType>(Ty)->getAddressSpace(); 27757a7e6055SDimitry Andric Ty = cast<PointerType>(Ty)->getElementType(); 27767a7e6055SDimitry Andric } 27777a7e6055SDimitry Andric 27787a7e6055SDimitry Andric uint64_t RawLinkage = Record[3]; 27797a7e6055SDimitry Andric GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage); 27807a7e6055SDimitry Andric unsigned Alignment; 27817a7e6055SDimitry Andric if (Error Err = parseAlignmentValue(Record[4], Alignment)) 27827a7e6055SDimitry Andric return Err; 27837a7e6055SDimitry Andric std::string Section; 27847a7e6055SDimitry Andric if (Record[5]) { 27857a7e6055SDimitry Andric if (Record[5] - 1 >= SectionTable.size()) 27867a7e6055SDimitry Andric return error("Invalid ID"); 27877a7e6055SDimitry Andric Section = SectionTable[Record[5] - 1]; 27887a7e6055SDimitry Andric } 27897a7e6055SDimitry Andric GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility; 27907a7e6055SDimitry Andric // Local linkage must have default visibility. 27917a7e6055SDimitry Andric if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage)) 27927a7e6055SDimitry Andric // FIXME: Change to an error if non-default in 4.0. 27937a7e6055SDimitry Andric Visibility = getDecodedVisibility(Record[6]); 27947a7e6055SDimitry Andric 27957a7e6055SDimitry Andric GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal; 27967a7e6055SDimitry Andric if (Record.size() > 7) 27977a7e6055SDimitry Andric TLM = getDecodedThreadLocalMode(Record[7]); 27987a7e6055SDimitry Andric 27997a7e6055SDimitry Andric GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None; 28007a7e6055SDimitry Andric if (Record.size() > 8) 28017a7e6055SDimitry Andric UnnamedAddr = getDecodedUnnamedAddrType(Record[8]); 28027a7e6055SDimitry Andric 28037a7e6055SDimitry Andric bool ExternallyInitialized = false; 28047a7e6055SDimitry Andric if (Record.size() > 9) 28057a7e6055SDimitry Andric ExternallyInitialized = Record[9]; 28067a7e6055SDimitry Andric 28077a7e6055SDimitry Andric GlobalVariable *NewGV = 28086bc11b14SDimitry Andric new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, Name, 28097a7e6055SDimitry Andric nullptr, TLM, AddressSpace, ExternallyInitialized); 28107a7e6055SDimitry Andric NewGV->setAlignment(Alignment); 28117a7e6055SDimitry Andric if (!Section.empty()) 28127a7e6055SDimitry Andric NewGV->setSection(Section); 28137a7e6055SDimitry Andric NewGV->setVisibility(Visibility); 28147a7e6055SDimitry Andric NewGV->setUnnamedAddr(UnnamedAddr); 28157a7e6055SDimitry Andric 28167a7e6055SDimitry Andric if (Record.size() > 10) 28177a7e6055SDimitry Andric NewGV->setDLLStorageClass(getDecodedDLLStorageClass(Record[10])); 28187a7e6055SDimitry Andric else 28197a7e6055SDimitry Andric upgradeDLLImportExportLinkage(NewGV, RawLinkage); 28207a7e6055SDimitry Andric 28217a7e6055SDimitry Andric ValueList.push_back(NewGV); 28227a7e6055SDimitry Andric 28237a7e6055SDimitry Andric // Remember which value to use for the global initializer. 28247a7e6055SDimitry Andric if (unsigned InitID = Record[2]) 28257a7e6055SDimitry Andric GlobalInits.push_back(std::make_pair(NewGV, InitID - 1)); 28267a7e6055SDimitry Andric 28277a7e6055SDimitry Andric if (Record.size() > 11) { 28287a7e6055SDimitry Andric if (unsigned ComdatID = Record[11]) { 28297a7e6055SDimitry Andric if (ComdatID > ComdatList.size()) 28307a7e6055SDimitry Andric return error("Invalid global variable comdat ID"); 28317a7e6055SDimitry Andric NewGV->setComdat(ComdatList[ComdatID - 1]); 28327a7e6055SDimitry Andric } 28337a7e6055SDimitry Andric } else if (hasImplicitComdat(RawLinkage)) { 28347a7e6055SDimitry Andric NewGV->setComdat(reinterpret_cast<Comdat *>(1)); 28357a7e6055SDimitry Andric } 28365517e702SDimitry Andric 28375517e702SDimitry Andric if (Record.size() > 12) { 28385517e702SDimitry Andric auto AS = getAttributes(Record[12]).getFnAttributes(); 28395517e702SDimitry Andric NewGV->setAttributes(AS); 28405517e702SDimitry Andric } 28417a7e6055SDimitry Andric return Error::success(); 28427a7e6055SDimitry Andric } 28437a7e6055SDimitry Andric 28447a7e6055SDimitry Andric Error BitcodeReader::parseFunctionRecord(ArrayRef<uint64_t> Record) { 28456bc11b14SDimitry Andric // v1: [type, callingconv, isproto, linkage, paramattr, alignment, section, 28467a7e6055SDimitry Andric // visibility, gc, unnamed_addr, prologuedata, dllstorageclass, comdat, 28476bc11b14SDimitry Andric // prefixdata] (name in VST) 28486bc11b14SDimitry Andric // v2: [strtab_offset, strtab_size, v1] 28496bc11b14SDimitry Andric StringRef Name; 28506bc11b14SDimitry Andric std::tie(Name, Record) = readNameFromStrtab(Record); 28516bc11b14SDimitry Andric 28527a7e6055SDimitry Andric if (Record.size() < 8) 28537a7e6055SDimitry Andric return error("Invalid record"); 28547a7e6055SDimitry Andric Type *Ty = getTypeByID(Record[0]); 28557a7e6055SDimitry Andric if (!Ty) 28567a7e6055SDimitry Andric return error("Invalid record"); 28577a7e6055SDimitry Andric if (auto *PTy = dyn_cast<PointerType>(Ty)) 28587a7e6055SDimitry Andric Ty = PTy->getElementType(); 28597a7e6055SDimitry Andric auto *FTy = dyn_cast<FunctionType>(Ty); 28607a7e6055SDimitry Andric if (!FTy) 28617a7e6055SDimitry Andric return error("Invalid type for value"); 28627a7e6055SDimitry Andric auto CC = static_cast<CallingConv::ID>(Record[1]); 28637a7e6055SDimitry Andric if (CC & ~CallingConv::MaxID) 28647a7e6055SDimitry Andric return error("Invalid calling convention ID"); 28657a7e6055SDimitry Andric 28667a7e6055SDimitry Andric Function *Func = 28676bc11b14SDimitry Andric Function::Create(FTy, GlobalValue::ExternalLinkage, Name, TheModule); 28687a7e6055SDimitry Andric 28697a7e6055SDimitry Andric Func->setCallingConv(CC); 28707a7e6055SDimitry Andric bool isProto = Record[2]; 28717a7e6055SDimitry Andric uint64_t RawLinkage = Record[3]; 28727a7e6055SDimitry Andric Func->setLinkage(getDecodedLinkage(RawLinkage)); 28737a7e6055SDimitry Andric Func->setAttributes(getAttributes(Record[4])); 28747a7e6055SDimitry Andric 28757a7e6055SDimitry Andric unsigned Alignment; 28767a7e6055SDimitry Andric if (Error Err = parseAlignmentValue(Record[5], Alignment)) 28777a7e6055SDimitry Andric return Err; 28787a7e6055SDimitry Andric Func->setAlignment(Alignment); 28797a7e6055SDimitry Andric if (Record[6]) { 28807a7e6055SDimitry Andric if (Record[6] - 1 >= SectionTable.size()) 28817a7e6055SDimitry Andric return error("Invalid ID"); 28827a7e6055SDimitry Andric Func->setSection(SectionTable[Record[6] - 1]); 28837a7e6055SDimitry Andric } 28847a7e6055SDimitry Andric // Local linkage must have default visibility. 28857a7e6055SDimitry Andric if (!Func->hasLocalLinkage()) 28867a7e6055SDimitry Andric // FIXME: Change to an error if non-default in 4.0. 28877a7e6055SDimitry Andric Func->setVisibility(getDecodedVisibility(Record[7])); 28887a7e6055SDimitry Andric if (Record.size() > 8 && Record[8]) { 28897a7e6055SDimitry Andric if (Record[8] - 1 >= GCTable.size()) 28907a7e6055SDimitry Andric return error("Invalid ID"); 28917a7e6055SDimitry Andric Func->setGC(GCTable[Record[8] - 1]); 28927a7e6055SDimitry Andric } 28937a7e6055SDimitry Andric GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None; 28947a7e6055SDimitry Andric if (Record.size() > 9) 28957a7e6055SDimitry Andric UnnamedAddr = getDecodedUnnamedAddrType(Record[9]); 28967a7e6055SDimitry Andric Func->setUnnamedAddr(UnnamedAddr); 28977a7e6055SDimitry Andric if (Record.size() > 10 && Record[10] != 0) 28987a7e6055SDimitry Andric FunctionPrologues.push_back(std::make_pair(Func, Record[10] - 1)); 28997a7e6055SDimitry Andric 29007a7e6055SDimitry Andric if (Record.size() > 11) 29017a7e6055SDimitry Andric Func->setDLLStorageClass(getDecodedDLLStorageClass(Record[11])); 29027a7e6055SDimitry Andric else 29037a7e6055SDimitry Andric upgradeDLLImportExportLinkage(Func, RawLinkage); 29047a7e6055SDimitry Andric 29057a7e6055SDimitry Andric if (Record.size() > 12) { 29067a7e6055SDimitry Andric if (unsigned ComdatID = Record[12]) { 29077a7e6055SDimitry Andric if (ComdatID > ComdatList.size()) 29087a7e6055SDimitry Andric return error("Invalid function comdat ID"); 29097a7e6055SDimitry Andric Func->setComdat(ComdatList[ComdatID - 1]); 29107a7e6055SDimitry Andric } 29117a7e6055SDimitry Andric } else if (hasImplicitComdat(RawLinkage)) { 29127a7e6055SDimitry Andric Func->setComdat(reinterpret_cast<Comdat *>(1)); 29137a7e6055SDimitry Andric } 29147a7e6055SDimitry Andric 29157a7e6055SDimitry Andric if (Record.size() > 13 && Record[13] != 0) 29167a7e6055SDimitry Andric FunctionPrefixes.push_back(std::make_pair(Func, Record[13] - 1)); 29177a7e6055SDimitry Andric 29187a7e6055SDimitry Andric if (Record.size() > 14 && Record[14] != 0) 29197a7e6055SDimitry Andric FunctionPersonalityFns.push_back(std::make_pair(Func, Record[14] - 1)); 29207a7e6055SDimitry Andric 29217a7e6055SDimitry Andric ValueList.push_back(Func); 29227a7e6055SDimitry Andric 29237a7e6055SDimitry Andric // If this is a function with a body, remember the prototype we are 29247a7e6055SDimitry Andric // creating now, so that we can match up the body with them later. 29257a7e6055SDimitry Andric if (!isProto) { 29267a7e6055SDimitry Andric Func->setIsMaterializable(true); 29277a7e6055SDimitry Andric FunctionsWithBodies.push_back(Func); 29287a7e6055SDimitry Andric DeferredFunctionInfo[Func] = 0; 29297a7e6055SDimitry Andric } 29307a7e6055SDimitry Andric return Error::success(); 29317a7e6055SDimitry Andric } 29327a7e6055SDimitry Andric 29337a7e6055SDimitry Andric Error BitcodeReader::parseGlobalIndirectSymbolRecord( 29347a7e6055SDimitry Andric unsigned BitCode, ArrayRef<uint64_t> Record) { 29356bc11b14SDimitry Andric // v1 ALIAS_OLD: [alias type, aliasee val#, linkage] (name in VST) 29366bc11b14SDimitry Andric // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility, 29376bc11b14SDimitry Andric // dllstorageclass] (name in VST) 29386bc11b14SDimitry Andric // v1 IFUNC: [alias type, addrspace, aliasee val#, linkage, 29396bc11b14SDimitry Andric // visibility, dllstorageclass] (name in VST) 29406bc11b14SDimitry Andric // v2: [strtab_offset, strtab_size, v1] 29416bc11b14SDimitry Andric StringRef Name; 29426bc11b14SDimitry Andric std::tie(Name, Record) = readNameFromStrtab(Record); 29436bc11b14SDimitry Andric 29447a7e6055SDimitry Andric bool NewRecord = BitCode != bitc::MODULE_CODE_ALIAS_OLD; 29457a7e6055SDimitry Andric if (Record.size() < (3 + (unsigned)NewRecord)) 29467a7e6055SDimitry Andric return error("Invalid record"); 29477a7e6055SDimitry Andric unsigned OpNum = 0; 29487a7e6055SDimitry Andric Type *Ty = getTypeByID(Record[OpNum++]); 29497a7e6055SDimitry Andric if (!Ty) 29507a7e6055SDimitry Andric return error("Invalid record"); 29517a7e6055SDimitry Andric 29527a7e6055SDimitry Andric unsigned AddrSpace; 29537a7e6055SDimitry Andric if (!NewRecord) { 29547a7e6055SDimitry Andric auto *PTy = dyn_cast<PointerType>(Ty); 29557a7e6055SDimitry Andric if (!PTy) 29567a7e6055SDimitry Andric return error("Invalid type for value"); 29577a7e6055SDimitry Andric Ty = PTy->getElementType(); 29587a7e6055SDimitry Andric AddrSpace = PTy->getAddressSpace(); 29597a7e6055SDimitry Andric } else { 29607a7e6055SDimitry Andric AddrSpace = Record[OpNum++]; 29617a7e6055SDimitry Andric } 29627a7e6055SDimitry Andric 29637a7e6055SDimitry Andric auto Val = Record[OpNum++]; 29647a7e6055SDimitry Andric auto Linkage = Record[OpNum++]; 29657a7e6055SDimitry Andric GlobalIndirectSymbol *NewGA; 29667a7e6055SDimitry Andric if (BitCode == bitc::MODULE_CODE_ALIAS || 29677a7e6055SDimitry Andric BitCode == bitc::MODULE_CODE_ALIAS_OLD) 29686bc11b14SDimitry Andric NewGA = GlobalAlias::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name, 29697a7e6055SDimitry Andric TheModule); 29707a7e6055SDimitry Andric else 29716bc11b14SDimitry Andric NewGA = GlobalIFunc::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name, 29727a7e6055SDimitry Andric nullptr, TheModule); 29737a7e6055SDimitry Andric // Old bitcode files didn't have visibility field. 29747a7e6055SDimitry Andric // Local linkage must have default visibility. 29757a7e6055SDimitry Andric if (OpNum != Record.size()) { 29767a7e6055SDimitry Andric auto VisInd = OpNum++; 29777a7e6055SDimitry Andric if (!NewGA->hasLocalLinkage()) 29787a7e6055SDimitry Andric // FIXME: Change to an error if non-default in 4.0. 29797a7e6055SDimitry Andric NewGA->setVisibility(getDecodedVisibility(Record[VisInd])); 29807a7e6055SDimitry Andric } 29817a7e6055SDimitry Andric if (OpNum != Record.size()) 29827a7e6055SDimitry Andric NewGA->setDLLStorageClass(getDecodedDLLStorageClass(Record[OpNum++])); 29837a7e6055SDimitry Andric else 29847a7e6055SDimitry Andric upgradeDLLImportExportLinkage(NewGA, Linkage); 29857a7e6055SDimitry Andric if (OpNum != Record.size()) 29867a7e6055SDimitry Andric NewGA->setThreadLocalMode(getDecodedThreadLocalMode(Record[OpNum++])); 29877a7e6055SDimitry Andric if (OpNum != Record.size()) 29887a7e6055SDimitry Andric NewGA->setUnnamedAddr(getDecodedUnnamedAddrType(Record[OpNum++])); 29897a7e6055SDimitry Andric ValueList.push_back(NewGA); 29907a7e6055SDimitry Andric IndirectSymbolInits.push_back(std::make_pair(NewGA, Val)); 29917a7e6055SDimitry Andric return Error::success(); 29927a7e6055SDimitry Andric } 29937a7e6055SDimitry Andric 2994d88c1a5aSDimitry Andric Error BitcodeReader::parseModule(uint64_t ResumeBit, 29957d523365SDimitry Andric bool ShouldLazyLoadMetadata) { 29967d523365SDimitry Andric if (ResumeBit) 29977d523365SDimitry Andric Stream.JumpToBit(ResumeBit); 2998dff0c46cSDimitry Andric else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 29998f0fd8f6SDimitry Andric return error("Invalid record"); 3000dff0c46cSDimitry Andric 3001dff0c46cSDimitry Andric SmallVector<uint64_t, 64> Record; 3002dff0c46cSDimitry Andric 3003dff0c46cSDimitry Andric // Read all the records for this module. 3004d88c1a5aSDimitry Andric while (true) { 3005139f7f9bSDimitry Andric BitstreamEntry Entry = Stream.advance(); 3006dff0c46cSDimitry Andric 3007139f7f9bSDimitry Andric switch (Entry.Kind) { 3008139f7f9bSDimitry Andric case BitstreamEntry::Error: 30098f0fd8f6SDimitry Andric return error("Malformed block"); 3010139f7f9bSDimitry Andric case BitstreamEntry::EndBlock: 30118f0fd8f6SDimitry Andric return globalCleanup(); 3012dff0c46cSDimitry Andric 3013139f7f9bSDimitry Andric case BitstreamEntry::SubBlock: 3014139f7f9bSDimitry Andric switch (Entry.ID) { 3015f22ef01cSRoman Divacky default: // Skip unknown content. 3016f22ef01cSRoman Divacky if (Stream.SkipBlock()) 30178f0fd8f6SDimitry Andric return error("Invalid record"); 3018f22ef01cSRoman Divacky break; 3019f22ef01cSRoman Divacky case bitc::BLOCKINFO_BLOCK_ID: 3020d88c1a5aSDimitry Andric if (readBlockInfo()) 30218f0fd8f6SDimitry Andric return error("Malformed block"); 3022f22ef01cSRoman Divacky break; 3023f22ef01cSRoman Divacky case bitc::PARAMATTR_BLOCK_ID: 3024d88c1a5aSDimitry Andric if (Error Err = parseAttributeBlock()) 3025d88c1a5aSDimitry Andric return Err; 3026f22ef01cSRoman Divacky break; 3027139f7f9bSDimitry Andric case bitc::PARAMATTR_GROUP_BLOCK_ID: 3028d88c1a5aSDimitry Andric if (Error Err = parseAttributeGroupBlock()) 3029d88c1a5aSDimitry Andric return Err; 3030139f7f9bSDimitry Andric break; 303117a519f9SDimitry Andric case bitc::TYPE_BLOCK_ID_NEW: 3032d88c1a5aSDimitry Andric if (Error Err = parseTypeTable()) 3033d88c1a5aSDimitry Andric return Err; 3034f22ef01cSRoman Divacky break; 3035f22ef01cSRoman Divacky case bitc::VALUE_SYMTAB_BLOCK_ID: 30367d523365SDimitry Andric if (!SeenValueSymbolTable) { 30377d523365SDimitry Andric // Either this is an old form VST without function index and an 30387d523365SDimitry Andric // associated VST forward declaration record (which would have caused 30397d523365SDimitry Andric // the VST to be jumped to and parsed before it was encountered 30407d523365SDimitry Andric // normally in the stream), or there were no function blocks to 30417d523365SDimitry Andric // trigger an earlier parsing of the VST. 30427d523365SDimitry Andric assert(VSTOffset == 0 || FunctionsWithBodies.empty()); 3043d88c1a5aSDimitry Andric if (Error Err = parseValueSymbolTable()) 3044d88c1a5aSDimitry Andric return Err; 3045dff0c46cSDimitry Andric SeenValueSymbolTable = true; 30467d523365SDimitry Andric } else { 30477d523365SDimitry Andric // We must have had a VST forward declaration record, which caused 30487d523365SDimitry Andric // the parser to jump to and parse the VST earlier. 30497d523365SDimitry Andric assert(VSTOffset > 0); 30507d523365SDimitry Andric if (Stream.SkipBlock()) 30517d523365SDimitry Andric return error("Invalid record"); 30527d523365SDimitry Andric } 3053f22ef01cSRoman Divacky break; 3054f22ef01cSRoman Divacky case bitc::CONSTANTS_BLOCK_ID: 3055d88c1a5aSDimitry Andric if (Error Err = parseConstants()) 3056d88c1a5aSDimitry Andric return Err; 3057d88c1a5aSDimitry Andric if (Error Err = resolveGlobalAndIndirectSymbolInits()) 3058d88c1a5aSDimitry Andric return Err; 3059f22ef01cSRoman Divacky break; 3060f22ef01cSRoman Divacky case bitc::METADATA_BLOCK_ID: 3061d88c1a5aSDimitry Andric if (ShouldLazyLoadMetadata) { 3062d88c1a5aSDimitry Andric if (Error Err = rememberAndSkipMetadata()) 3063d88c1a5aSDimitry Andric return Err; 3064ff0cc061SDimitry Andric break; 3065ff0cc061SDimitry Andric } 3066ff0cc061SDimitry Andric assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata"); 3067d88c1a5aSDimitry Andric if (Error Err = MDLoader->parseModuleMetadata()) 3068d88c1a5aSDimitry Andric return Err; 30697d523365SDimitry Andric break; 30707d523365SDimitry Andric case bitc::METADATA_KIND_BLOCK_ID: 3071d88c1a5aSDimitry Andric if (Error Err = MDLoader->parseMetadataKinds()) 3072d88c1a5aSDimitry Andric return Err; 3073f22ef01cSRoman Divacky break; 3074f22ef01cSRoman Divacky case bitc::FUNCTION_BLOCK_ID: 3075f22ef01cSRoman Divacky // If this is the first function body we've seen, reverse the 3076f22ef01cSRoman Divacky // FunctionsWithBodies list. 3077dff0c46cSDimitry Andric if (!SeenFirstFunctionBody) { 3078f22ef01cSRoman Divacky std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end()); 3079d88c1a5aSDimitry Andric if (Error Err = globalCleanup()) 3080d88c1a5aSDimitry Andric return Err; 3081dff0c46cSDimitry Andric SeenFirstFunctionBody = true; 3082f22ef01cSRoman Divacky } 3083f22ef01cSRoman Divacky 30847d523365SDimitry Andric if (VSTOffset > 0) { 30857d523365SDimitry Andric // If we have a VST forward declaration record, make sure we 30867d523365SDimitry Andric // parse the VST now if we haven't already. It is needed to 30877d523365SDimitry Andric // set up the DeferredFunctionInfo vector for lazy reading. 30887d523365SDimitry Andric if (!SeenValueSymbolTable) { 3089d88c1a5aSDimitry Andric if (Error Err = BitcodeReader::parseValueSymbolTable(VSTOffset)) 3090d88c1a5aSDimitry Andric return Err; 30917d523365SDimitry Andric SeenValueSymbolTable = true; 30927d523365SDimitry Andric // Fall through so that we record the NextUnreadBit below. 30937d523365SDimitry Andric // This is necessary in case we have an anonymous function that 30947d523365SDimitry Andric // is later materialized. Since it will not have a VST entry we 30957d523365SDimitry Andric // need to fall back to the lazy parse to find its offset. 30967d523365SDimitry Andric } else { 30977d523365SDimitry Andric // If we have a VST forward declaration record, but have already 30987d523365SDimitry Andric // parsed the VST (just above, when the first function body was 30997d523365SDimitry Andric // encountered here), then we are resuming the parse after 31007d523365SDimitry Andric // materializing functions. The ResumeBit points to the 31017d523365SDimitry Andric // start of the last function block recorded in the 31027d523365SDimitry Andric // DeferredFunctionInfo map. Skip it. 31037d523365SDimitry Andric if (Stream.SkipBlock()) 31047d523365SDimitry Andric return error("Invalid record"); 31057d523365SDimitry Andric continue; 31067d523365SDimitry Andric } 31077d523365SDimitry Andric } 31087d523365SDimitry Andric 31097d523365SDimitry Andric // Support older bitcode files that did not have the function 31107d523365SDimitry Andric // index in the VST, nor a VST forward declaration record, as 31117d523365SDimitry Andric // well as anonymous functions that do not have VST entries. 31127d523365SDimitry Andric // Build the DeferredFunctionInfo vector on the fly. 3113d88c1a5aSDimitry Andric if (Error Err = rememberAndSkipFunctionBody()) 3114d88c1a5aSDimitry Andric return Err; 31157d523365SDimitry Andric 31163dac3a9bSDimitry Andric // Suspend parsing when we reach the function bodies. Subsequent 31173dac3a9bSDimitry Andric // materialization calls will resume it when necessary. If the bitcode 31183dac3a9bSDimitry Andric // file is old, the symbol table will be at the end instead and will not 31193dac3a9bSDimitry Andric // have been seen yet. In this case, just finish the parse now. 31203dac3a9bSDimitry Andric if (SeenValueSymbolTable) { 3121dff0c46cSDimitry Andric NextUnreadBit = Stream.GetCurrentBitNo(); 3122d88c1a5aSDimitry Andric // After the VST has been parsed, we need to make sure intrinsic name 3123d88c1a5aSDimitry Andric // are auto-upgraded. 3124d88c1a5aSDimitry Andric return globalCleanup(); 3125dff0c46cSDimitry Andric } 3126dff0c46cSDimitry Andric break; 3127dff0c46cSDimitry Andric case bitc::USELIST_BLOCK_ID: 3128d88c1a5aSDimitry Andric if (Error Err = parseUseLists()) 3129d88c1a5aSDimitry Andric return Err; 3130f22ef01cSRoman Divacky break; 31317d523365SDimitry Andric case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID: 3132d88c1a5aSDimitry Andric if (Error Err = parseOperandBundleTags()) 3133d88c1a5aSDimitry Andric return Err; 31347d523365SDimitry Andric break; 3135f22ef01cSRoman Divacky } 3136f22ef01cSRoman Divacky continue; 3137139f7f9bSDimitry Andric 3138139f7f9bSDimitry Andric case BitstreamEntry::Record: 3139139f7f9bSDimitry Andric // The interesting case. 3140139f7f9bSDimitry Andric break; 3141f22ef01cSRoman Divacky } 3142f22ef01cSRoman Divacky 3143f22ef01cSRoman Divacky // Read a record. 31447d523365SDimitry Andric auto BitCode = Stream.readRecord(Entry.ID, Record); 31457d523365SDimitry Andric switch (BitCode) { 3146f22ef01cSRoman Divacky default: break; // Default behavior, ignore unknown content. 31477a7e6055SDimitry Andric case bitc::MODULE_CODE_VERSION: { 31487a7e6055SDimitry Andric Expected<unsigned> VersionOrErr = parseVersionRecord(Record); 31497a7e6055SDimitry Andric if (!VersionOrErr) 31507a7e6055SDimitry Andric return VersionOrErr.takeError(); 31517a7e6055SDimitry Andric UseRelativeIDs = *VersionOrErr >= 1; 31523861d79fSDimitry Andric break; 31533861d79fSDimitry Andric } 3154f22ef01cSRoman Divacky case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] 3155f22ef01cSRoman Divacky std::string S; 31568f0fd8f6SDimitry Andric if (convertToString(Record, 0, S)) 31578f0fd8f6SDimitry Andric return error("Invalid record"); 3158f22ef01cSRoman Divacky TheModule->setTargetTriple(S); 3159f22ef01cSRoman Divacky break; 3160f22ef01cSRoman Divacky } 3161f22ef01cSRoman Divacky case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N] 3162f22ef01cSRoman Divacky std::string S; 31638f0fd8f6SDimitry Andric if (convertToString(Record, 0, S)) 31648f0fd8f6SDimitry Andric return error("Invalid record"); 3165f22ef01cSRoman Divacky TheModule->setDataLayout(S); 3166f22ef01cSRoman Divacky break; 3167f22ef01cSRoman Divacky } 3168f22ef01cSRoman Divacky case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N] 3169f22ef01cSRoman Divacky std::string S; 31708f0fd8f6SDimitry Andric if (convertToString(Record, 0, S)) 31718f0fd8f6SDimitry Andric return error("Invalid record"); 3172f22ef01cSRoman Divacky TheModule->setModuleInlineAsm(S); 3173f22ef01cSRoman Divacky break; 3174f22ef01cSRoman Divacky } 3175f22ef01cSRoman Divacky case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N] 3176139f7f9bSDimitry Andric // FIXME: Remove in 4.0. 3177f22ef01cSRoman Divacky std::string S; 31788f0fd8f6SDimitry Andric if (convertToString(Record, 0, S)) 31798f0fd8f6SDimitry Andric return error("Invalid record"); 3180139f7f9bSDimitry Andric // Ignore value. 3181f22ef01cSRoman Divacky break; 3182f22ef01cSRoman Divacky } 3183f22ef01cSRoman Divacky case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] 3184f22ef01cSRoman Divacky std::string S; 31858f0fd8f6SDimitry Andric if (convertToString(Record, 0, S)) 31868f0fd8f6SDimitry Andric return error("Invalid record"); 3187f22ef01cSRoman Divacky SectionTable.push_back(S); 3188f22ef01cSRoman Divacky break; 3189f22ef01cSRoman Divacky } 3190f22ef01cSRoman Divacky case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N] 3191f22ef01cSRoman Divacky std::string S; 31928f0fd8f6SDimitry Andric if (convertToString(Record, 0, S)) 31938f0fd8f6SDimitry Andric return error("Invalid record"); 3194f22ef01cSRoman Divacky GCTable.push_back(S); 3195f22ef01cSRoman Divacky break; 3196f22ef01cSRoman Divacky } 31977a7e6055SDimitry Andric case bitc::MODULE_CODE_COMDAT: { 31987a7e6055SDimitry Andric if (Error Err = parseComdatRecord(Record)) 31997a7e6055SDimitry Andric return Err; 320091bc56edSDimitry Andric break; 320191bc56edSDimitry Andric } 3202f22ef01cSRoman Divacky case bitc::MODULE_CODE_GLOBALVAR: { 32037a7e6055SDimitry Andric if (Error Err = parseGlobalVarRecord(Record)) 3204d88c1a5aSDimitry Andric return Err; 3205f22ef01cSRoman Divacky break; 3206f22ef01cSRoman Divacky } 3207f22ef01cSRoman Divacky case bitc::MODULE_CODE_FUNCTION: { 32087a7e6055SDimitry Andric if (Error Err = parseFunctionRecord(Record)) 3209d88c1a5aSDimitry Andric return Err; 3210f22ef01cSRoman Divacky break; 3211f22ef01cSRoman Divacky } 32123ca95b02SDimitry Andric case bitc::MODULE_CODE_IFUNC: 32137d523365SDimitry Andric case bitc::MODULE_CODE_ALIAS: 32147d523365SDimitry Andric case bitc::MODULE_CODE_ALIAS_OLD: { 32157a7e6055SDimitry Andric if (Error Err = parseGlobalIndirectSymbolRecord(BitCode, Record)) 32167a7e6055SDimitry Andric return Err; 3217f22ef01cSRoman Divacky break; 3218f22ef01cSRoman Divacky } 32197d523365SDimitry Andric /// MODULE_CODE_VSTOFFSET: [offset] 32207d523365SDimitry Andric case bitc::MODULE_CODE_VSTOFFSET: 32217d523365SDimitry Andric if (Record.size() < 1) 32227d523365SDimitry Andric return error("Invalid record"); 3223d88c1a5aSDimitry Andric // Note that we subtract 1 here because the offset is relative to one word 3224d88c1a5aSDimitry Andric // before the start of the identification or module block, which was 3225d88c1a5aSDimitry Andric // historically always the start of the regular bitcode header. 3226d88c1a5aSDimitry Andric VSTOffset = Record[0] - 1; 32277d523365SDimitry Andric break; 32283ca95b02SDimitry Andric /// MODULE_CODE_SOURCE_FILENAME: [namechar x N] 32293ca95b02SDimitry Andric case bitc::MODULE_CODE_SOURCE_FILENAME: 32303ca95b02SDimitry Andric SmallString<128> ValueName; 32313ca95b02SDimitry Andric if (convertToString(Record, 0, ValueName)) 32327d523365SDimitry Andric return error("Invalid record"); 32333ca95b02SDimitry Andric TheModule->setSourceFileName(ValueName); 32347d523365SDimitry Andric break; 3235f22ef01cSRoman Divacky } 3236f22ef01cSRoman Divacky Record.clear(); 3237f22ef01cSRoman Divacky } 3238f22ef01cSRoman Divacky } 3239f22ef01cSRoman Divacky 3240d88c1a5aSDimitry Andric Error BitcodeReader::parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata, 3241d88c1a5aSDimitry Andric bool IsImporting) { 32428f0fd8f6SDimitry Andric TheModule = M; 3243d88c1a5aSDimitry Andric MDLoader = MetadataLoader(Stream, *M, ValueList, IsImporting, 3244d88c1a5aSDimitry Andric [&](unsigned ID) { return getTypeByID(ID); }); 32457d523365SDimitry Andric return parseModule(0, ShouldLazyLoadMetadata); 3246f22ef01cSRoman Divacky } 3247f22ef01cSRoman Divacky 32482754fe60SDimitry Andric 3249d88c1a5aSDimitry Andric Error BitcodeReader::typeCheckLoadStoreInst(Type *ValType, Type *PtrType) { 3250ff0cc061SDimitry Andric if (!isa<PointerType>(PtrType)) 3251d88c1a5aSDimitry Andric return error("Load/Store operand is not a pointer type"); 3252ff0cc061SDimitry Andric Type *ElemType = cast<PointerType>(PtrType)->getElementType(); 3253ff0cc061SDimitry Andric 3254ff0cc061SDimitry Andric if (ValType && ValType != ElemType) 3255d88c1a5aSDimitry Andric return error("Explicit load/store type does not match pointee " 32567d523365SDimitry Andric "type of pointer operand"); 3257ff0cc061SDimitry Andric if (!PointerType::isLoadableOrStorableType(ElemType)) 3258d88c1a5aSDimitry Andric return error("Cannot load/store from pointer"); 3259d88c1a5aSDimitry Andric return Error::success(); 3260ff0cc061SDimitry Andric } 3261ff0cc061SDimitry Andric 32628f0fd8f6SDimitry Andric /// Lazily parse the specified function body block. 3263d88c1a5aSDimitry Andric Error BitcodeReader::parseFunctionBody(Function *F) { 3264f22ef01cSRoman Divacky if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID)) 32658f0fd8f6SDimitry Andric return error("Invalid record"); 3266f22ef01cSRoman Divacky 32673ca95b02SDimitry Andric // Unexpected unresolved metadata when parsing function. 3268d88c1a5aSDimitry Andric if (MDLoader->hasFwdRefs()) 32693ca95b02SDimitry Andric return error("Invalid function metadata: incoming forward references"); 32703ca95b02SDimitry Andric 3271f22ef01cSRoman Divacky InstructionList.clear(); 3272f22ef01cSRoman Divacky unsigned ModuleValueListSize = ValueList.size(); 3273d88c1a5aSDimitry Andric unsigned ModuleMDLoaderSize = MDLoader->size(); 3274f22ef01cSRoman Divacky 3275f22ef01cSRoman Divacky // Add all the function arguments to the value table. 32767d523365SDimitry Andric for (Argument &I : F->args()) 32777d523365SDimitry Andric ValueList.push_back(&I); 3278f22ef01cSRoman Divacky 3279f22ef01cSRoman Divacky unsigned NextValueNo = ValueList.size(); 328091bc56edSDimitry Andric BasicBlock *CurBB = nullptr; 3281f22ef01cSRoman Divacky unsigned CurBBNo = 0; 3282f22ef01cSRoman Divacky 3283f22ef01cSRoman Divacky DebugLoc LastLoc; 328439d628a0SDimitry Andric auto getLastInstruction = [&]() -> Instruction * { 328539d628a0SDimitry Andric if (CurBB && !CurBB->empty()) 328639d628a0SDimitry Andric return &CurBB->back(); 328739d628a0SDimitry Andric else if (CurBBNo && FunctionBBs[CurBBNo - 1] && 328839d628a0SDimitry Andric !FunctionBBs[CurBBNo - 1]->empty()) 328939d628a0SDimitry Andric return &FunctionBBs[CurBBNo - 1]->back(); 329039d628a0SDimitry Andric return nullptr; 329139d628a0SDimitry Andric }; 3292f22ef01cSRoman Divacky 32937d523365SDimitry Andric std::vector<OperandBundleDef> OperandBundles; 32947d523365SDimitry Andric 3295f22ef01cSRoman Divacky // Read all the records. 3296f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 3297d88c1a5aSDimitry Andric 3298d88c1a5aSDimitry Andric while (true) { 3299139f7f9bSDimitry Andric BitstreamEntry Entry = Stream.advance(); 3300f22ef01cSRoman Divacky 3301139f7f9bSDimitry Andric switch (Entry.Kind) { 3302139f7f9bSDimitry Andric case BitstreamEntry::Error: 33038f0fd8f6SDimitry Andric return error("Malformed block"); 3304139f7f9bSDimitry Andric case BitstreamEntry::EndBlock: 3305139f7f9bSDimitry Andric goto OutOfRecordLoop; 3306139f7f9bSDimitry Andric 3307139f7f9bSDimitry Andric case BitstreamEntry::SubBlock: 3308139f7f9bSDimitry Andric switch (Entry.ID) { 3309f22ef01cSRoman Divacky default: // Skip unknown content. 3310f22ef01cSRoman Divacky if (Stream.SkipBlock()) 33118f0fd8f6SDimitry Andric return error("Invalid record"); 3312f22ef01cSRoman Divacky break; 3313f22ef01cSRoman Divacky case bitc::CONSTANTS_BLOCK_ID: 3314d88c1a5aSDimitry Andric if (Error Err = parseConstants()) 3315d88c1a5aSDimitry Andric return Err; 3316f22ef01cSRoman Divacky NextValueNo = ValueList.size(); 3317f22ef01cSRoman Divacky break; 3318f22ef01cSRoman Divacky case bitc::VALUE_SYMTAB_BLOCK_ID: 3319d88c1a5aSDimitry Andric if (Error Err = parseValueSymbolTable()) 3320d88c1a5aSDimitry Andric return Err; 3321f22ef01cSRoman Divacky break; 3322f22ef01cSRoman Divacky case bitc::METADATA_ATTACHMENT_ID: 3323d88c1a5aSDimitry Andric if (Error Err = MDLoader->parseMetadataAttachment(*F, InstructionList)) 3324d88c1a5aSDimitry Andric return Err; 3325f22ef01cSRoman Divacky break; 3326f22ef01cSRoman Divacky case bitc::METADATA_BLOCK_ID: 3327d88c1a5aSDimitry Andric assert(DeferredMetadataInfo.empty() && 3328d88c1a5aSDimitry Andric "Must read all module-level metadata before function-level"); 3329d88c1a5aSDimitry Andric if (Error Err = MDLoader->parseFunctionMetadata()) 3330d88c1a5aSDimitry Andric return Err; 3331f22ef01cSRoman Divacky break; 333239d628a0SDimitry Andric case bitc::USELIST_BLOCK_ID: 3333d88c1a5aSDimitry Andric if (Error Err = parseUseLists()) 3334d88c1a5aSDimitry Andric return Err; 333539d628a0SDimitry Andric break; 3336f22ef01cSRoman Divacky } 3337f22ef01cSRoman Divacky continue; 3338f22ef01cSRoman Divacky 3339139f7f9bSDimitry Andric case BitstreamEntry::Record: 3340139f7f9bSDimitry Andric // The interesting case. 3341139f7f9bSDimitry Andric break; 3342f22ef01cSRoman Divacky } 3343f22ef01cSRoman Divacky 3344f22ef01cSRoman Divacky // Read a record. 3345f22ef01cSRoman Divacky Record.clear(); 334691bc56edSDimitry Andric Instruction *I = nullptr; 3347139f7f9bSDimitry Andric unsigned BitCode = Stream.readRecord(Entry.ID, Record); 3348f22ef01cSRoman Divacky switch (BitCode) { 3349f22ef01cSRoman Divacky default: // Default behavior: reject 33508f0fd8f6SDimitry Andric return error("Invalid value"); 335139d628a0SDimitry Andric case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks] 3352f22ef01cSRoman Divacky if (Record.size() < 1 || Record[0] == 0) 33538f0fd8f6SDimitry Andric return error("Invalid record"); 3354f22ef01cSRoman Divacky // Create all the basic blocks for the function. 3355f22ef01cSRoman Divacky FunctionBBs.resize(Record[0]); 335639d628a0SDimitry Andric 335739d628a0SDimitry Andric // See if anything took the address of blocks in this function. 335839d628a0SDimitry Andric auto BBFRI = BasicBlockFwdRefs.find(F); 335939d628a0SDimitry Andric if (BBFRI == BasicBlockFwdRefs.end()) { 3360f22ef01cSRoman Divacky for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i) 3361f22ef01cSRoman Divacky FunctionBBs[i] = BasicBlock::Create(Context, "", F); 336239d628a0SDimitry Andric } else { 336339d628a0SDimitry Andric auto &BBRefs = BBFRI->second; 336439d628a0SDimitry Andric // Check for invalid basic block references. 336539d628a0SDimitry Andric if (BBRefs.size() > FunctionBBs.size()) 33668f0fd8f6SDimitry Andric return error("Invalid ID"); 336739d628a0SDimitry Andric assert(!BBRefs.empty() && "Unexpected empty array"); 336839d628a0SDimitry Andric assert(!BBRefs.front() && "Invalid reference to entry block"); 336939d628a0SDimitry Andric for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E; 337039d628a0SDimitry Andric ++I) 337139d628a0SDimitry Andric if (I < RE && BBRefs[I]) { 337239d628a0SDimitry Andric BBRefs[I]->insertInto(F); 337339d628a0SDimitry Andric FunctionBBs[I] = BBRefs[I]; 337439d628a0SDimitry Andric } else { 337539d628a0SDimitry Andric FunctionBBs[I] = BasicBlock::Create(Context, "", F); 337639d628a0SDimitry Andric } 337739d628a0SDimitry Andric 337839d628a0SDimitry Andric // Erase from the table. 337939d628a0SDimitry Andric BasicBlockFwdRefs.erase(BBFRI); 338039d628a0SDimitry Andric } 338139d628a0SDimitry Andric 3382f22ef01cSRoman Divacky CurBB = FunctionBBs[0]; 3383f22ef01cSRoman Divacky continue; 338439d628a0SDimitry Andric } 3385f22ef01cSRoman Divacky 3386f22ef01cSRoman Divacky case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN 3387f22ef01cSRoman Divacky // This record indicates that the last instruction is at the same 3388f22ef01cSRoman Divacky // location as the previous instruction with a location. 338939d628a0SDimitry Andric I = getLastInstruction(); 3390f22ef01cSRoman Divacky 339191bc56edSDimitry Andric if (!I) 33928f0fd8f6SDimitry Andric return error("Invalid record"); 3393f22ef01cSRoman Divacky I->setDebugLoc(LastLoc); 339491bc56edSDimitry Andric I = nullptr; 3395f22ef01cSRoman Divacky continue; 3396f22ef01cSRoman Divacky 339717a519f9SDimitry Andric case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia] 339839d628a0SDimitry Andric I = getLastInstruction(); 339991bc56edSDimitry Andric if (!I || Record.size() < 4) 34008f0fd8f6SDimitry Andric return error("Invalid record"); 3401f22ef01cSRoman Divacky 3402f22ef01cSRoman Divacky unsigned Line = Record[0], Col = Record[1]; 3403f22ef01cSRoman Divacky unsigned ScopeID = Record[2], IAID = Record[3]; 3404f22ef01cSRoman Divacky 340591bc56edSDimitry Andric MDNode *Scope = nullptr, *IA = nullptr; 34063ca95b02SDimitry Andric if (ScopeID) { 3407d88c1a5aSDimitry Andric Scope = MDLoader->getMDNodeFwdRefOrNull(ScopeID - 1); 34083ca95b02SDimitry Andric if (!Scope) 34093ca95b02SDimitry Andric return error("Invalid record"); 34103ca95b02SDimitry Andric } 34113ca95b02SDimitry Andric if (IAID) { 3412d88c1a5aSDimitry Andric IA = MDLoader->getMDNodeFwdRefOrNull(IAID - 1); 34133ca95b02SDimitry Andric if (!IA) 34143ca95b02SDimitry Andric return error("Invalid record"); 34153ca95b02SDimitry Andric } 3416f22ef01cSRoman Divacky LastLoc = DebugLoc::get(Line, Col, Scope, IA); 3417f22ef01cSRoman Divacky I->setDebugLoc(LastLoc); 341891bc56edSDimitry Andric I = nullptr; 3419f22ef01cSRoman Divacky continue; 3420f22ef01cSRoman Divacky } 3421f22ef01cSRoman Divacky 3422f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode] 3423f22ef01cSRoman Divacky unsigned OpNum = 0; 3424f22ef01cSRoman Divacky Value *LHS, *RHS; 3425f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || 34263861d79fSDimitry Andric popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) || 3427f22ef01cSRoman Divacky OpNum+1 > Record.size()) 34288f0fd8f6SDimitry Andric return error("Invalid record"); 3429f22ef01cSRoman Divacky 34308f0fd8f6SDimitry Andric int Opc = getDecodedBinaryOpcode(Record[OpNum++], LHS->getType()); 3431f785676fSDimitry Andric if (Opc == -1) 34328f0fd8f6SDimitry Andric return error("Invalid record"); 3433f22ef01cSRoman Divacky I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); 3434f22ef01cSRoman Divacky InstructionList.push_back(I); 3435f22ef01cSRoman Divacky if (OpNum < Record.size()) { 3436f22ef01cSRoman Divacky if (Opc == Instruction::Add || 3437f22ef01cSRoman Divacky Opc == Instruction::Sub || 34382754fe60SDimitry Andric Opc == Instruction::Mul || 34392754fe60SDimitry Andric Opc == Instruction::Shl) { 3440f22ef01cSRoman Divacky if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP)) 3441f22ef01cSRoman Divacky cast<BinaryOperator>(I)->setHasNoSignedWrap(true); 3442f22ef01cSRoman Divacky if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) 3443f22ef01cSRoman Divacky cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true); 34442754fe60SDimitry Andric } else if (Opc == Instruction::SDiv || 34452754fe60SDimitry Andric Opc == Instruction::UDiv || 34462754fe60SDimitry Andric Opc == Instruction::LShr || 34472754fe60SDimitry Andric Opc == Instruction::AShr) { 34482754fe60SDimitry Andric if (Record[OpNum] & (1 << bitc::PEO_EXACT)) 3449f22ef01cSRoman Divacky cast<BinaryOperator>(I)->setIsExact(true); 3450139f7f9bSDimitry Andric } else if (isa<FPMathOperator>(I)) { 3451875ed548SDimitry Andric FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]); 3452139f7f9bSDimitry Andric if (FMF.any()) 3453139f7f9bSDimitry Andric I->setFastMathFlags(FMF); 3454f22ef01cSRoman Divacky } 3455139f7f9bSDimitry Andric 3456f22ef01cSRoman Divacky } 3457f22ef01cSRoman Divacky break; 3458f22ef01cSRoman Divacky } 3459f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc] 3460f22ef01cSRoman Divacky unsigned OpNum = 0; 3461f22ef01cSRoman Divacky Value *Op; 3462f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Op) || 3463f22ef01cSRoman Divacky OpNum+2 != Record.size()) 34648f0fd8f6SDimitry Andric return error("Invalid record"); 3465f22ef01cSRoman Divacky 34666122f3e6SDimitry Andric Type *ResTy = getTypeByID(Record[OpNum]); 34678f0fd8f6SDimitry Andric int Opc = getDecodedCastOpcode(Record[OpNum + 1]); 346891bc56edSDimitry Andric if (Opc == -1 || !ResTy) 34698f0fd8f6SDimitry Andric return error("Invalid record"); 347091bc56edSDimitry Andric Instruction *Temp = nullptr; 3471f785676fSDimitry Andric if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) { 3472f785676fSDimitry Andric if (Temp) { 3473f785676fSDimitry Andric InstructionList.push_back(Temp); 3474f785676fSDimitry Andric CurBB->getInstList().push_back(Temp); 3475f785676fSDimitry Andric } 3476f785676fSDimitry Andric } else { 34777d523365SDimitry Andric auto CastOp = (Instruction::CastOps)Opc; 34787d523365SDimitry Andric if (!CastInst::castIsValid(CastOp, Op, ResTy)) 34797d523365SDimitry Andric return error("Invalid cast"); 34807d523365SDimitry Andric I = CastInst::Create(CastOp, Op, ResTy); 3481f785676fSDimitry Andric } 3482f22ef01cSRoman Divacky InstructionList.push_back(I); 3483f22ef01cSRoman Divacky break; 3484f22ef01cSRoman Divacky } 3485ff0cc061SDimitry Andric case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD: 3486ff0cc061SDimitry Andric case bitc::FUNC_CODE_INST_GEP_OLD: 3487ff0cc061SDimitry Andric case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands] 3488f22ef01cSRoman Divacky unsigned OpNum = 0; 3489ff0cc061SDimitry Andric 3490ff0cc061SDimitry Andric Type *Ty; 3491ff0cc061SDimitry Andric bool InBounds; 3492ff0cc061SDimitry Andric 3493ff0cc061SDimitry Andric if (BitCode == bitc::FUNC_CODE_INST_GEP) { 3494ff0cc061SDimitry Andric InBounds = Record[OpNum++]; 3495ff0cc061SDimitry Andric Ty = getTypeByID(Record[OpNum++]); 3496ff0cc061SDimitry Andric } else { 3497ff0cc061SDimitry Andric InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD; 3498ff0cc061SDimitry Andric Ty = nullptr; 3499ff0cc061SDimitry Andric } 3500ff0cc061SDimitry Andric 3501f22ef01cSRoman Divacky Value *BasePtr; 3502f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr)) 35038f0fd8f6SDimitry Andric return error("Invalid record"); 3504f22ef01cSRoman Divacky 3505ff0cc061SDimitry Andric if (!Ty) 3506d88c1a5aSDimitry Andric Ty = cast<PointerType>(BasePtr->getType()->getScalarType()) 3507ff0cc061SDimitry Andric ->getElementType(); 3508ff0cc061SDimitry Andric else if (Ty != 3509d88c1a5aSDimitry Andric cast<PointerType>(BasePtr->getType()->getScalarType()) 3510ff0cc061SDimitry Andric ->getElementType()) 35118f0fd8f6SDimitry Andric return error( 3512ff0cc061SDimitry Andric "Explicit gep type does not match pointee type of pointer operand"); 3513ff0cc061SDimitry Andric 3514f22ef01cSRoman Divacky SmallVector<Value*, 16> GEPIdx; 3515f22ef01cSRoman Divacky while (OpNum != Record.size()) { 3516f22ef01cSRoman Divacky Value *Op; 3517f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Op)) 35188f0fd8f6SDimitry Andric return error("Invalid record"); 3519f22ef01cSRoman Divacky GEPIdx.push_back(Op); 3520f22ef01cSRoman Divacky } 3521f22ef01cSRoman Divacky 3522ff0cc061SDimitry Andric I = GetElementPtrInst::Create(Ty, BasePtr, GEPIdx); 3523ff0cc061SDimitry Andric 3524f22ef01cSRoman Divacky InstructionList.push_back(I); 3525ff0cc061SDimitry Andric if (InBounds) 3526f22ef01cSRoman Divacky cast<GetElementPtrInst>(I)->setIsInBounds(true); 3527f22ef01cSRoman Divacky break; 3528f22ef01cSRoman Divacky } 3529f22ef01cSRoman Divacky 3530f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_EXTRACTVAL: { 3531f22ef01cSRoman Divacky // EXTRACTVAL: [opty, opval, n x indices] 3532f22ef01cSRoman Divacky unsigned OpNum = 0; 3533f22ef01cSRoman Divacky Value *Agg; 3534f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) 35358f0fd8f6SDimitry Andric return error("Invalid record"); 3536f22ef01cSRoman Divacky 3537ff0cc061SDimitry Andric unsigned RecSize = Record.size(); 3538ff0cc061SDimitry Andric if (OpNum == RecSize) 35398f0fd8f6SDimitry Andric return error("EXTRACTVAL: Invalid instruction with 0 indices"); 3540ff0cc061SDimitry Andric 3541f22ef01cSRoman Divacky SmallVector<unsigned, 4> EXTRACTVALIdx; 3542ff0cc061SDimitry Andric Type *CurTy = Agg->getType(); 3543ff0cc061SDimitry Andric for (; OpNum != RecSize; ++OpNum) { 3544ff0cc061SDimitry Andric bool IsArray = CurTy->isArrayTy(); 3545ff0cc061SDimitry Andric bool IsStruct = CurTy->isStructTy(); 3546f22ef01cSRoman Divacky uint64_t Index = Record[OpNum]; 3547ff0cc061SDimitry Andric 3548ff0cc061SDimitry Andric if (!IsStruct && !IsArray) 35498f0fd8f6SDimitry Andric return error("EXTRACTVAL: Invalid type"); 3550f22ef01cSRoman Divacky if ((unsigned)Index != Index) 35518f0fd8f6SDimitry Andric return error("Invalid value"); 3552ff0cc061SDimitry Andric if (IsStruct && Index >= CurTy->subtypes().size()) 35538f0fd8f6SDimitry Andric return error("EXTRACTVAL: Invalid struct index"); 3554ff0cc061SDimitry Andric if (IsArray && Index >= CurTy->getArrayNumElements()) 35558f0fd8f6SDimitry Andric return error("EXTRACTVAL: Invalid array index"); 3556f22ef01cSRoman Divacky EXTRACTVALIdx.push_back((unsigned)Index); 3557ff0cc061SDimitry Andric 3558ff0cc061SDimitry Andric if (IsStruct) 3559ff0cc061SDimitry Andric CurTy = CurTy->subtypes()[Index]; 3560ff0cc061SDimitry Andric else 3561ff0cc061SDimitry Andric CurTy = CurTy->subtypes()[0]; 3562f22ef01cSRoman Divacky } 3563f22ef01cSRoman Divacky 356417a519f9SDimitry Andric I = ExtractValueInst::Create(Agg, EXTRACTVALIdx); 3565f22ef01cSRoman Divacky InstructionList.push_back(I); 3566f22ef01cSRoman Divacky break; 3567f22ef01cSRoman Divacky } 3568f22ef01cSRoman Divacky 3569f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_INSERTVAL: { 3570f22ef01cSRoman Divacky // INSERTVAL: [opty, opval, opty, opval, n x indices] 3571f22ef01cSRoman Divacky unsigned OpNum = 0; 3572f22ef01cSRoman Divacky Value *Agg; 3573f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) 35748f0fd8f6SDimitry Andric return error("Invalid record"); 3575f22ef01cSRoman Divacky Value *Val; 3576f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Val)) 35778f0fd8f6SDimitry Andric return error("Invalid record"); 3578f22ef01cSRoman Divacky 3579ff0cc061SDimitry Andric unsigned RecSize = Record.size(); 3580ff0cc061SDimitry Andric if (OpNum == RecSize) 35818f0fd8f6SDimitry Andric return error("INSERTVAL: Invalid instruction with 0 indices"); 3582ff0cc061SDimitry Andric 3583f22ef01cSRoman Divacky SmallVector<unsigned, 4> INSERTVALIdx; 3584ff0cc061SDimitry Andric Type *CurTy = Agg->getType(); 3585ff0cc061SDimitry Andric for (; OpNum != RecSize; ++OpNum) { 3586ff0cc061SDimitry Andric bool IsArray = CurTy->isArrayTy(); 3587ff0cc061SDimitry Andric bool IsStruct = CurTy->isStructTy(); 3588f22ef01cSRoman Divacky uint64_t Index = Record[OpNum]; 3589ff0cc061SDimitry Andric 3590ff0cc061SDimitry Andric if (!IsStruct && !IsArray) 35918f0fd8f6SDimitry Andric return error("INSERTVAL: Invalid type"); 3592f22ef01cSRoman Divacky if ((unsigned)Index != Index) 35938f0fd8f6SDimitry Andric return error("Invalid value"); 3594ff0cc061SDimitry Andric if (IsStruct && Index >= CurTy->subtypes().size()) 35958f0fd8f6SDimitry Andric return error("INSERTVAL: Invalid struct index"); 3596ff0cc061SDimitry Andric if (IsArray && Index >= CurTy->getArrayNumElements()) 35978f0fd8f6SDimitry Andric return error("INSERTVAL: Invalid array index"); 3598ff0cc061SDimitry Andric 3599f22ef01cSRoman Divacky INSERTVALIdx.push_back((unsigned)Index); 3600ff0cc061SDimitry Andric if (IsStruct) 3601ff0cc061SDimitry Andric CurTy = CurTy->subtypes()[Index]; 3602ff0cc061SDimitry Andric else 3603ff0cc061SDimitry Andric CurTy = CurTy->subtypes()[0]; 3604f22ef01cSRoman Divacky } 3605f22ef01cSRoman Divacky 3606ff0cc061SDimitry Andric if (CurTy != Val->getType()) 36078f0fd8f6SDimitry Andric return error("Inserted value type doesn't match aggregate type"); 3608ff0cc061SDimitry Andric 360917a519f9SDimitry Andric I = InsertValueInst::Create(Agg, Val, INSERTVALIdx); 3610f22ef01cSRoman Divacky InstructionList.push_back(I); 3611f22ef01cSRoman Divacky break; 3612f22ef01cSRoman Divacky } 3613f22ef01cSRoman Divacky 3614f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval] 3615f22ef01cSRoman Divacky // obsolete form of select 3616f22ef01cSRoman Divacky // handles select i1 ... in old bitcode 3617f22ef01cSRoman Divacky unsigned OpNum = 0; 3618f22ef01cSRoman Divacky Value *TrueVal, *FalseVal, *Cond; 3619f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) || 36203861d79fSDimitry Andric popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) || 36213861d79fSDimitry Andric popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond)) 36228f0fd8f6SDimitry Andric return error("Invalid record"); 3623f22ef01cSRoman Divacky 3624f22ef01cSRoman Divacky I = SelectInst::Create(Cond, TrueVal, FalseVal); 3625f22ef01cSRoman Divacky InstructionList.push_back(I); 3626f22ef01cSRoman Divacky break; 3627f22ef01cSRoman Divacky } 3628f22ef01cSRoman Divacky 3629f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred] 3630f22ef01cSRoman Divacky // new form of select 3631f22ef01cSRoman Divacky // handles select i1 or select [N x i1] 3632f22ef01cSRoman Divacky unsigned OpNum = 0; 3633f22ef01cSRoman Divacky Value *TrueVal, *FalseVal, *Cond; 3634f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) || 36353861d79fSDimitry Andric popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) || 3636f22ef01cSRoman Divacky getValueTypePair(Record, OpNum, NextValueNo, Cond)) 36378f0fd8f6SDimitry Andric return error("Invalid record"); 3638f22ef01cSRoman Divacky 3639f22ef01cSRoman Divacky // select condition can be either i1 or [N x i1] 36406122f3e6SDimitry Andric if (VectorType* vector_type = 36416122f3e6SDimitry Andric dyn_cast<VectorType>(Cond->getType())) { 3642f22ef01cSRoman Divacky // expect <n x i1> 3643f22ef01cSRoman Divacky if (vector_type->getElementType() != Type::getInt1Ty(Context)) 36448f0fd8f6SDimitry Andric return error("Invalid type for value"); 3645f22ef01cSRoman Divacky } else { 3646f22ef01cSRoman Divacky // expect i1 3647f22ef01cSRoman Divacky if (Cond->getType() != Type::getInt1Ty(Context)) 36488f0fd8f6SDimitry Andric return error("Invalid type for value"); 3649f22ef01cSRoman Divacky } 3650f22ef01cSRoman Divacky 3651f22ef01cSRoman Divacky I = SelectInst::Create(Cond, TrueVal, FalseVal); 3652f22ef01cSRoman Divacky InstructionList.push_back(I); 3653f22ef01cSRoman Divacky break; 3654f22ef01cSRoman Divacky } 3655f22ef01cSRoman Divacky 3656f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval] 3657f22ef01cSRoman Divacky unsigned OpNum = 0; 3658f22ef01cSRoman Divacky Value *Vec, *Idx; 3659f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Vec) || 366091bc56edSDimitry Andric getValueTypePair(Record, OpNum, NextValueNo, Idx)) 36618f0fd8f6SDimitry Andric return error("Invalid record"); 3662ff0cc061SDimitry Andric if (!Vec->getType()->isVectorTy()) 36638f0fd8f6SDimitry Andric return error("Invalid type for value"); 3664f22ef01cSRoman Divacky I = ExtractElementInst::Create(Vec, Idx); 3665f22ef01cSRoman Divacky InstructionList.push_back(I); 3666f22ef01cSRoman Divacky break; 3667f22ef01cSRoman Divacky } 3668f22ef01cSRoman Divacky 3669f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval] 3670f22ef01cSRoman Divacky unsigned OpNum = 0; 3671f22ef01cSRoman Divacky Value *Vec, *Elt, *Idx; 3672ff0cc061SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Vec)) 36738f0fd8f6SDimitry Andric return error("Invalid record"); 3674ff0cc061SDimitry Andric if (!Vec->getType()->isVectorTy()) 36758f0fd8f6SDimitry Andric return error("Invalid type for value"); 3676ff0cc061SDimitry Andric if (popValue(Record, OpNum, NextValueNo, 3677f22ef01cSRoman Divacky cast<VectorType>(Vec->getType())->getElementType(), Elt) || 367891bc56edSDimitry Andric getValueTypePair(Record, OpNum, NextValueNo, Idx)) 36798f0fd8f6SDimitry Andric return error("Invalid record"); 3680f22ef01cSRoman Divacky I = InsertElementInst::Create(Vec, Elt, Idx); 3681f22ef01cSRoman Divacky InstructionList.push_back(I); 3682f22ef01cSRoman Divacky break; 3683f22ef01cSRoman Divacky } 3684f22ef01cSRoman Divacky 3685f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval] 3686f22ef01cSRoman Divacky unsigned OpNum = 0; 3687f22ef01cSRoman Divacky Value *Vec1, *Vec2, *Mask; 3688f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) || 36893861d79fSDimitry Andric popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2)) 36908f0fd8f6SDimitry Andric return error("Invalid record"); 3691f22ef01cSRoman Divacky 3692f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Mask)) 36938f0fd8f6SDimitry Andric return error("Invalid record"); 3694ff0cc061SDimitry Andric if (!Vec1->getType()->isVectorTy() || !Vec2->getType()->isVectorTy()) 36958f0fd8f6SDimitry Andric return error("Invalid type for value"); 3696f22ef01cSRoman Divacky I = new ShuffleVectorInst(Vec1, Vec2, Mask); 3697f22ef01cSRoman Divacky InstructionList.push_back(I); 3698f22ef01cSRoman Divacky break; 3699f22ef01cSRoman Divacky } 3700f22ef01cSRoman Divacky 3701f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred] 3702f22ef01cSRoman Divacky // Old form of ICmp/FCmp returning bool 3703f22ef01cSRoman Divacky // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were 3704f22ef01cSRoman Divacky // both legal on vectors but had different behaviour. 3705f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred] 3706f22ef01cSRoman Divacky // FCmp/ICmp returning bool or vector of bool 3707f22ef01cSRoman Divacky 3708f22ef01cSRoman Divacky unsigned OpNum = 0; 3709f22ef01cSRoman Divacky Value *LHS, *RHS; 3710f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || 3711875ed548SDimitry Andric popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS)) 3712875ed548SDimitry Andric return error("Invalid record"); 3713875ed548SDimitry Andric 3714875ed548SDimitry Andric unsigned PredVal = Record[OpNum]; 3715875ed548SDimitry Andric bool IsFP = LHS->getType()->isFPOrFPVectorTy(); 3716875ed548SDimitry Andric FastMathFlags FMF; 3717875ed548SDimitry Andric if (IsFP && Record.size() > OpNum+1) 3718875ed548SDimitry Andric FMF = getDecodedFastMathFlags(Record[++OpNum]); 3719875ed548SDimitry Andric 3720875ed548SDimitry Andric if (OpNum+1 != Record.size()) 37218f0fd8f6SDimitry Andric return error("Invalid record"); 3722f22ef01cSRoman Divacky 3723f22ef01cSRoman Divacky if (LHS->getType()->isFPOrFPVectorTy()) 3724875ed548SDimitry Andric I = new FCmpInst((FCmpInst::Predicate)PredVal, LHS, RHS); 3725f22ef01cSRoman Divacky else 3726875ed548SDimitry Andric I = new ICmpInst((ICmpInst::Predicate)PredVal, LHS, RHS); 3727875ed548SDimitry Andric 3728875ed548SDimitry Andric if (FMF.any()) 3729875ed548SDimitry Andric I->setFastMathFlags(FMF); 3730f22ef01cSRoman Divacky InstructionList.push_back(I); 3731f22ef01cSRoman Divacky break; 3732f22ef01cSRoman Divacky } 3733f22ef01cSRoman Divacky 3734f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>] 3735f22ef01cSRoman Divacky { 3736f22ef01cSRoman Divacky unsigned Size = Record.size(); 3737f22ef01cSRoman Divacky if (Size == 0) { 3738f22ef01cSRoman Divacky I = ReturnInst::Create(Context); 3739f22ef01cSRoman Divacky InstructionList.push_back(I); 3740f22ef01cSRoman Divacky break; 3741f22ef01cSRoman Divacky } 3742f22ef01cSRoman Divacky 3743f22ef01cSRoman Divacky unsigned OpNum = 0; 374491bc56edSDimitry Andric Value *Op = nullptr; 3745f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Op)) 37468f0fd8f6SDimitry Andric return error("Invalid record"); 374717a519f9SDimitry Andric if (OpNum != Record.size()) 37488f0fd8f6SDimitry Andric return error("Invalid record"); 3749f22ef01cSRoman Divacky 375017a519f9SDimitry Andric I = ReturnInst::Create(Context, Op); 3751f22ef01cSRoman Divacky InstructionList.push_back(I); 3752f22ef01cSRoman Divacky break; 3753f22ef01cSRoman Divacky } 3754f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#] 3755f22ef01cSRoman Divacky if (Record.size() != 1 && Record.size() != 3) 37568f0fd8f6SDimitry Andric return error("Invalid record"); 3757f22ef01cSRoman Divacky BasicBlock *TrueDest = getBasicBlock(Record[0]); 375891bc56edSDimitry Andric if (!TrueDest) 37598f0fd8f6SDimitry Andric return error("Invalid record"); 3760f22ef01cSRoman Divacky 3761f22ef01cSRoman Divacky if (Record.size() == 1) { 3762f22ef01cSRoman Divacky I = BranchInst::Create(TrueDest); 3763f22ef01cSRoman Divacky InstructionList.push_back(I); 3764f22ef01cSRoman Divacky } 3765f22ef01cSRoman Divacky else { 3766f22ef01cSRoman Divacky BasicBlock *FalseDest = getBasicBlock(Record[1]); 37673861d79fSDimitry Andric Value *Cond = getValue(Record, 2, NextValueNo, 37683861d79fSDimitry Andric Type::getInt1Ty(Context)); 376991bc56edSDimitry Andric if (!FalseDest || !Cond) 37708f0fd8f6SDimitry Andric return error("Invalid record"); 3771f22ef01cSRoman Divacky I = BranchInst::Create(TrueDest, FalseDest, Cond); 3772f22ef01cSRoman Divacky InstructionList.push_back(I); 3773f22ef01cSRoman Divacky } 3774f22ef01cSRoman Divacky break; 3775f22ef01cSRoman Divacky } 37767d523365SDimitry Andric case bitc::FUNC_CODE_INST_CLEANUPRET: { // CLEANUPRET: [val] or [val,bb#] 37777d523365SDimitry Andric if (Record.size() != 1 && Record.size() != 2) 37787d523365SDimitry Andric return error("Invalid record"); 37797d523365SDimitry Andric unsigned Idx = 0; 37807d523365SDimitry Andric Value *CleanupPad = 37817d523365SDimitry Andric getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); 37827d523365SDimitry Andric if (!CleanupPad) 37837d523365SDimitry Andric return error("Invalid record"); 37847d523365SDimitry Andric BasicBlock *UnwindDest = nullptr; 37857d523365SDimitry Andric if (Record.size() == 2) { 37867d523365SDimitry Andric UnwindDest = getBasicBlock(Record[Idx++]); 37877d523365SDimitry Andric if (!UnwindDest) 37887d523365SDimitry Andric return error("Invalid record"); 37897d523365SDimitry Andric } 37907d523365SDimitry Andric 37917d523365SDimitry Andric I = CleanupReturnInst::Create(CleanupPad, UnwindDest); 37927d523365SDimitry Andric InstructionList.push_back(I); 37937d523365SDimitry Andric break; 37947d523365SDimitry Andric } 37957d523365SDimitry Andric case bitc::FUNC_CODE_INST_CATCHRET: { // CATCHRET: [val,bb#] 37967d523365SDimitry Andric if (Record.size() != 2) 37977d523365SDimitry Andric return error("Invalid record"); 37987d523365SDimitry Andric unsigned Idx = 0; 37997d523365SDimitry Andric Value *CatchPad = 38007d523365SDimitry Andric getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); 38017d523365SDimitry Andric if (!CatchPad) 38027d523365SDimitry Andric return error("Invalid record"); 38037d523365SDimitry Andric BasicBlock *BB = getBasicBlock(Record[Idx++]); 38047d523365SDimitry Andric if (!BB) 38057d523365SDimitry Andric return error("Invalid record"); 38067d523365SDimitry Andric 38077d523365SDimitry Andric I = CatchReturnInst::Create(CatchPad, BB); 38087d523365SDimitry Andric InstructionList.push_back(I); 38097d523365SDimitry Andric break; 38107d523365SDimitry Andric } 38117d523365SDimitry Andric case bitc::FUNC_CODE_INST_CATCHSWITCH: { // CATCHSWITCH: [tok,num,(bb)*,bb?] 38127d523365SDimitry Andric // We must have, at minimum, the outer scope and the number of arguments. 38137d523365SDimitry Andric if (Record.size() < 2) 38147d523365SDimitry Andric return error("Invalid record"); 38157d523365SDimitry Andric 38167d523365SDimitry Andric unsigned Idx = 0; 38177d523365SDimitry Andric 38187d523365SDimitry Andric Value *ParentPad = 38197d523365SDimitry Andric getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); 38207d523365SDimitry Andric 38217d523365SDimitry Andric unsigned NumHandlers = Record[Idx++]; 38227d523365SDimitry Andric 38237d523365SDimitry Andric SmallVector<BasicBlock *, 2> Handlers; 38247d523365SDimitry Andric for (unsigned Op = 0; Op != NumHandlers; ++Op) { 38257d523365SDimitry Andric BasicBlock *BB = getBasicBlock(Record[Idx++]); 38267d523365SDimitry Andric if (!BB) 38277d523365SDimitry Andric return error("Invalid record"); 38287d523365SDimitry Andric Handlers.push_back(BB); 38297d523365SDimitry Andric } 38307d523365SDimitry Andric 38317d523365SDimitry Andric BasicBlock *UnwindDest = nullptr; 38327d523365SDimitry Andric if (Idx + 1 == Record.size()) { 38337d523365SDimitry Andric UnwindDest = getBasicBlock(Record[Idx++]); 38347d523365SDimitry Andric if (!UnwindDest) 38357d523365SDimitry Andric return error("Invalid record"); 38367d523365SDimitry Andric } 38377d523365SDimitry Andric 38387d523365SDimitry Andric if (Record.size() != Idx) 38397d523365SDimitry Andric return error("Invalid record"); 38407d523365SDimitry Andric 38417d523365SDimitry Andric auto *CatchSwitch = 38427d523365SDimitry Andric CatchSwitchInst::Create(ParentPad, UnwindDest, NumHandlers); 38437d523365SDimitry Andric for (BasicBlock *Handler : Handlers) 38447d523365SDimitry Andric CatchSwitch->addHandler(Handler); 38457d523365SDimitry Andric I = CatchSwitch; 38467d523365SDimitry Andric InstructionList.push_back(I); 38477d523365SDimitry Andric break; 38487d523365SDimitry Andric } 38497d523365SDimitry Andric case bitc::FUNC_CODE_INST_CATCHPAD: 38507d523365SDimitry Andric case bitc::FUNC_CODE_INST_CLEANUPPAD: { // [tok,num,(ty,val)*] 38517d523365SDimitry Andric // We must have, at minimum, the outer scope and the number of arguments. 38527d523365SDimitry Andric if (Record.size() < 2) 38537d523365SDimitry Andric return error("Invalid record"); 38547d523365SDimitry Andric 38557d523365SDimitry Andric unsigned Idx = 0; 38567d523365SDimitry Andric 38577d523365SDimitry Andric Value *ParentPad = 38587d523365SDimitry Andric getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); 38597d523365SDimitry Andric 38607d523365SDimitry Andric unsigned NumArgOperands = Record[Idx++]; 38617d523365SDimitry Andric 38627d523365SDimitry Andric SmallVector<Value *, 2> Args; 38637d523365SDimitry Andric for (unsigned Op = 0; Op != NumArgOperands; ++Op) { 38647d523365SDimitry Andric Value *Val; 38657d523365SDimitry Andric if (getValueTypePair(Record, Idx, NextValueNo, Val)) 38667d523365SDimitry Andric return error("Invalid record"); 38677d523365SDimitry Andric Args.push_back(Val); 38687d523365SDimitry Andric } 38697d523365SDimitry Andric 38707d523365SDimitry Andric if (Record.size() != Idx) 38717d523365SDimitry Andric return error("Invalid record"); 38727d523365SDimitry Andric 38737d523365SDimitry Andric if (BitCode == bitc::FUNC_CODE_INST_CLEANUPPAD) 38747d523365SDimitry Andric I = CleanupPadInst::Create(ParentPad, Args); 38757d523365SDimitry Andric else 38767d523365SDimitry Andric I = CatchPadInst::Create(ParentPad, Args); 38777d523365SDimitry Andric InstructionList.push_back(I); 38787d523365SDimitry Andric break; 38797d523365SDimitry Andric } 3880f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...] 38817ae0e2c9SDimitry Andric // Check magic 38827ae0e2c9SDimitry Andric if ((Record[0] >> 16) == SWITCH_INST_MAGIC) { 3883f785676fSDimitry Andric // "New" SwitchInst format with case ranges. The changes to write this 3884f785676fSDimitry Andric // format were reverted but we still recognize bitcode that uses it. 3885f785676fSDimitry Andric // Hopefully someday we will have support for case ranges and can use 3886f785676fSDimitry Andric // this format again. 38877ae0e2c9SDimitry Andric 38887ae0e2c9SDimitry Andric Type *OpTy = getTypeByID(Record[1]); 38897ae0e2c9SDimitry Andric unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth(); 38907ae0e2c9SDimitry Andric 38913861d79fSDimitry Andric Value *Cond = getValue(Record, 2, NextValueNo, OpTy); 38927ae0e2c9SDimitry Andric BasicBlock *Default = getBasicBlock(Record[3]); 389391bc56edSDimitry Andric if (!OpTy || !Cond || !Default) 38948f0fd8f6SDimitry Andric return error("Invalid record"); 38957ae0e2c9SDimitry Andric 38967ae0e2c9SDimitry Andric unsigned NumCases = Record[4]; 38977ae0e2c9SDimitry Andric 38987ae0e2c9SDimitry Andric SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); 38997ae0e2c9SDimitry Andric InstructionList.push_back(SI); 39007ae0e2c9SDimitry Andric 39017ae0e2c9SDimitry Andric unsigned CurIdx = 5; 39027ae0e2c9SDimitry Andric for (unsigned i = 0; i != NumCases; ++i) { 3903f785676fSDimitry Andric SmallVector<ConstantInt*, 1> CaseVals; 39047ae0e2c9SDimitry Andric unsigned NumItems = Record[CurIdx++]; 39057ae0e2c9SDimitry Andric for (unsigned ci = 0; ci != NumItems; ++ci) { 39067ae0e2c9SDimitry Andric bool isSingleNumber = Record[CurIdx++]; 39077ae0e2c9SDimitry Andric 39087ae0e2c9SDimitry Andric APInt Low; 39097ae0e2c9SDimitry Andric unsigned ActiveWords = 1; 39107ae0e2c9SDimitry Andric if (ValueBitWidth > 64) 39117ae0e2c9SDimitry Andric ActiveWords = Record[CurIdx++]; 39128f0fd8f6SDimitry Andric Low = readWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords), 39137ae0e2c9SDimitry Andric ValueBitWidth); 39147ae0e2c9SDimitry Andric CurIdx += ActiveWords; 39157ae0e2c9SDimitry Andric 39167ae0e2c9SDimitry Andric if (!isSingleNumber) { 39177ae0e2c9SDimitry Andric ActiveWords = 1; 39187ae0e2c9SDimitry Andric if (ValueBitWidth > 64) 39197ae0e2c9SDimitry Andric ActiveWords = Record[CurIdx++]; 39208f0fd8f6SDimitry Andric APInt High = readWideAPInt( 39218f0fd8f6SDimitry Andric makeArrayRef(&Record[CurIdx], ActiveWords), ValueBitWidth); 39227ae0e2c9SDimitry Andric CurIdx += ActiveWords; 3923f785676fSDimitry Andric 3924f785676fSDimitry Andric // FIXME: It is not clear whether values in the range should be 3925f785676fSDimitry Andric // compared as signed or unsigned values. The partially 3926f785676fSDimitry Andric // implemented changes that used this format in the past used 3927f785676fSDimitry Andric // unsigned comparisons. 3928f785676fSDimitry Andric for ( ; Low.ule(High); ++Low) 3929f785676fSDimitry Andric CaseVals.push_back(ConstantInt::get(Context, Low)); 39307ae0e2c9SDimitry Andric } else 3931f785676fSDimitry Andric CaseVals.push_back(ConstantInt::get(Context, Low)); 39327ae0e2c9SDimitry Andric } 39337ae0e2c9SDimitry Andric BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]); 3934f785676fSDimitry Andric for (SmallVector<ConstantInt*, 1>::iterator cvi = CaseVals.begin(), 3935f785676fSDimitry Andric cve = CaseVals.end(); cvi != cve; ++cvi) 3936f785676fSDimitry Andric SI->addCase(*cvi, DestBB); 39377ae0e2c9SDimitry Andric } 39387ae0e2c9SDimitry Andric I = SI; 39397ae0e2c9SDimitry Andric break; 39407ae0e2c9SDimitry Andric } 39417ae0e2c9SDimitry Andric 39427ae0e2c9SDimitry Andric // Old SwitchInst format without case ranges. 39437ae0e2c9SDimitry Andric 3944f22ef01cSRoman Divacky if (Record.size() < 3 || (Record.size() & 1) == 0) 39458f0fd8f6SDimitry Andric return error("Invalid record"); 39466122f3e6SDimitry Andric Type *OpTy = getTypeByID(Record[0]); 39473861d79fSDimitry Andric Value *Cond = getValue(Record, 1, NextValueNo, OpTy); 3948f22ef01cSRoman Divacky BasicBlock *Default = getBasicBlock(Record[2]); 394991bc56edSDimitry Andric if (!OpTy || !Cond || !Default) 39508f0fd8f6SDimitry Andric return error("Invalid record"); 3951f22ef01cSRoman Divacky unsigned NumCases = (Record.size()-3)/2; 3952f22ef01cSRoman Divacky SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); 3953f22ef01cSRoman Divacky InstructionList.push_back(SI); 3954f22ef01cSRoman Divacky for (unsigned i = 0, e = NumCases; i != e; ++i) { 3955f22ef01cSRoman Divacky ConstantInt *CaseVal = 3956f22ef01cSRoman Divacky dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy)); 3957f22ef01cSRoman Divacky BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]); 395891bc56edSDimitry Andric if (!CaseVal || !DestBB) { 3959f22ef01cSRoman Divacky delete SI; 39608f0fd8f6SDimitry Andric return error("Invalid record"); 3961f22ef01cSRoman Divacky } 3962f22ef01cSRoman Divacky SI->addCase(CaseVal, DestBB); 3963f22ef01cSRoman Divacky } 3964f22ef01cSRoman Divacky I = SI; 3965f22ef01cSRoman Divacky break; 3966f22ef01cSRoman Divacky } 3967f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...] 3968f22ef01cSRoman Divacky if (Record.size() < 2) 39698f0fd8f6SDimitry Andric return error("Invalid record"); 39706122f3e6SDimitry Andric Type *OpTy = getTypeByID(Record[0]); 39713861d79fSDimitry Andric Value *Address = getValue(Record, 1, NextValueNo, OpTy); 397291bc56edSDimitry Andric if (!OpTy || !Address) 39738f0fd8f6SDimitry Andric return error("Invalid record"); 3974f22ef01cSRoman Divacky unsigned NumDests = Record.size()-2; 3975f22ef01cSRoman Divacky IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests); 3976f22ef01cSRoman Divacky InstructionList.push_back(IBI); 3977f22ef01cSRoman Divacky for (unsigned i = 0, e = NumDests; i != e; ++i) { 3978f22ef01cSRoman Divacky if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) { 3979f22ef01cSRoman Divacky IBI->addDestination(DestBB); 3980f22ef01cSRoman Divacky } else { 3981f22ef01cSRoman Divacky delete IBI; 39828f0fd8f6SDimitry Andric return error("Invalid record"); 3983f22ef01cSRoman Divacky } 3984f22ef01cSRoman Divacky } 3985f22ef01cSRoman Divacky I = IBI; 3986f22ef01cSRoman Divacky break; 3987f22ef01cSRoman Divacky } 3988f22ef01cSRoman Divacky 3989f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_INVOKE: { 3990f22ef01cSRoman Divacky // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...] 3991f785676fSDimitry Andric if (Record.size() < 4) 39928f0fd8f6SDimitry Andric return error("Invalid record"); 3993ff0cc061SDimitry Andric unsigned OpNum = 0; 39947a7e6055SDimitry Andric AttributeList PAL = getAttributes(Record[OpNum++]); 3995ff0cc061SDimitry Andric unsigned CCInfo = Record[OpNum++]; 3996ff0cc061SDimitry Andric BasicBlock *NormalBB = getBasicBlock(Record[OpNum++]); 3997ff0cc061SDimitry Andric BasicBlock *UnwindBB = getBasicBlock(Record[OpNum++]); 3998f22ef01cSRoman Divacky 3999ff0cc061SDimitry Andric FunctionType *FTy = nullptr; 4000ff0cc061SDimitry Andric if (CCInfo >> 13 & 1 && 4001ff0cc061SDimitry Andric !(FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++])))) 40028f0fd8f6SDimitry Andric return error("Explicit invoke type is not a function type"); 4003ff0cc061SDimitry Andric 4004f22ef01cSRoman Divacky Value *Callee; 4005f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) 40068f0fd8f6SDimitry Andric return error("Invalid record"); 4007f22ef01cSRoman Divacky 40086122f3e6SDimitry Andric PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType()); 4009ff0cc061SDimitry Andric if (!CalleeTy) 40108f0fd8f6SDimitry Andric return error("Callee is not a pointer"); 4011ff0cc061SDimitry Andric if (!FTy) { 4012ff0cc061SDimitry Andric FTy = dyn_cast<FunctionType>(CalleeTy->getElementType()); 4013ff0cc061SDimitry Andric if (!FTy) 40148f0fd8f6SDimitry Andric return error("Callee is not of pointer to function type"); 4015ff0cc061SDimitry Andric } else if (CalleeTy->getElementType() != FTy) 40168f0fd8f6SDimitry Andric return error("Explicit invoke type does not match pointee type of " 4017ff0cc061SDimitry Andric "callee operand"); 4018ff0cc061SDimitry Andric if (Record.size() < FTy->getNumParams() + OpNum) 40198f0fd8f6SDimitry Andric return error("Insufficient operands to call"); 4020f22ef01cSRoman Divacky 4021f22ef01cSRoman Divacky SmallVector<Value*, 16> Ops; 4022f22ef01cSRoman Divacky for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { 40233861d79fSDimitry Andric Ops.push_back(getValue(Record, OpNum, NextValueNo, 40243861d79fSDimitry Andric FTy->getParamType(i))); 402591bc56edSDimitry Andric if (!Ops.back()) 40268f0fd8f6SDimitry Andric return error("Invalid record"); 4027f22ef01cSRoman Divacky } 4028f22ef01cSRoman Divacky 4029f22ef01cSRoman Divacky if (!FTy->isVarArg()) { 4030f22ef01cSRoman Divacky if (Record.size() != OpNum) 40318f0fd8f6SDimitry Andric return error("Invalid record"); 4032f22ef01cSRoman Divacky } else { 4033f22ef01cSRoman Divacky // Read type/value pairs for varargs params. 4034f22ef01cSRoman Divacky while (OpNum != Record.size()) { 4035f22ef01cSRoman Divacky Value *Op; 4036f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Op)) 40378f0fd8f6SDimitry Andric return error("Invalid record"); 4038f22ef01cSRoman Divacky Ops.push_back(Op); 4039f22ef01cSRoman Divacky } 4040f22ef01cSRoman Divacky } 4041f22ef01cSRoman Divacky 40427d523365SDimitry Andric I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops, OperandBundles); 40437d523365SDimitry Andric OperandBundles.clear(); 4044f22ef01cSRoman Divacky InstructionList.push_back(I); 40457d523365SDimitry Andric cast<InvokeInst>(I)->setCallingConv( 40467d523365SDimitry Andric static_cast<CallingConv::ID>(CallingConv::MaxID & CCInfo)); 4047f22ef01cSRoman Divacky cast<InvokeInst>(I)->setAttributes(PAL); 4048f22ef01cSRoman Divacky break; 4049f22ef01cSRoman Divacky } 40506122f3e6SDimitry Andric case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval] 40516122f3e6SDimitry Andric unsigned Idx = 0; 405291bc56edSDimitry Andric Value *Val = nullptr; 40536122f3e6SDimitry Andric if (getValueTypePair(Record, Idx, NextValueNo, Val)) 40548f0fd8f6SDimitry Andric return error("Invalid record"); 40556122f3e6SDimitry Andric I = ResumeInst::Create(Val); 40566122f3e6SDimitry Andric InstructionList.push_back(I); 40576122f3e6SDimitry Andric break; 40586122f3e6SDimitry Andric } 4059f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE 4060f22ef01cSRoman Divacky I = new UnreachableInst(Context); 4061f22ef01cSRoman Divacky InstructionList.push_back(I); 4062f22ef01cSRoman Divacky break; 4063f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...] 4064f22ef01cSRoman Divacky if (Record.size() < 1 || ((Record.size()-1)&1)) 40658f0fd8f6SDimitry Andric return error("Invalid record"); 40666122f3e6SDimitry Andric Type *Ty = getTypeByID(Record[0]); 4067f785676fSDimitry Andric if (!Ty) 40688f0fd8f6SDimitry Andric return error("Invalid record"); 4069f22ef01cSRoman Divacky 40703b0f4066SDimitry Andric PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2); 4071f22ef01cSRoman Divacky InstructionList.push_back(PN); 4072f22ef01cSRoman Divacky 4073f22ef01cSRoman Divacky for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) { 40743861d79fSDimitry Andric Value *V; 40753861d79fSDimitry Andric // With the new function encoding, it is possible that operands have 40763861d79fSDimitry Andric // negative IDs (for forward references). Use a signed VBR 40773861d79fSDimitry Andric // representation to keep the encoding small. 40783861d79fSDimitry Andric if (UseRelativeIDs) 40793861d79fSDimitry Andric V = getValueSigned(Record, 1+i, NextValueNo, Ty); 40803861d79fSDimitry Andric else 40813861d79fSDimitry Andric V = getValue(Record, 1+i, NextValueNo, Ty); 4082f22ef01cSRoman Divacky BasicBlock *BB = getBasicBlock(Record[2+i]); 4083f785676fSDimitry Andric if (!V || !BB) 40848f0fd8f6SDimitry Andric return error("Invalid record"); 4085f22ef01cSRoman Divacky PN->addIncoming(V, BB); 4086f22ef01cSRoman Divacky } 4087f22ef01cSRoman Divacky I = PN; 4088f22ef01cSRoman Divacky break; 4089f22ef01cSRoman Divacky } 4090f22ef01cSRoman Divacky 40918f0fd8f6SDimitry Andric case bitc::FUNC_CODE_INST_LANDINGPAD: 40928f0fd8f6SDimitry Andric case bitc::FUNC_CODE_INST_LANDINGPAD_OLD: { 40936122f3e6SDimitry Andric // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?] 40946122f3e6SDimitry Andric unsigned Idx = 0; 40958f0fd8f6SDimitry Andric if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD) { 40968f0fd8f6SDimitry Andric if (Record.size() < 3) 40978f0fd8f6SDimitry Andric return error("Invalid record"); 40988f0fd8f6SDimitry Andric } else { 40998f0fd8f6SDimitry Andric assert(BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD); 41006122f3e6SDimitry Andric if (Record.size() < 4) 41018f0fd8f6SDimitry Andric return error("Invalid record"); 41028f0fd8f6SDimitry Andric } 41036122f3e6SDimitry Andric Type *Ty = getTypeByID(Record[Idx++]); 4104f785676fSDimitry Andric if (!Ty) 41058f0fd8f6SDimitry Andric return error("Invalid record"); 41068f0fd8f6SDimitry Andric if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD) { 410791bc56edSDimitry Andric Value *PersFn = nullptr; 41086122f3e6SDimitry Andric if (getValueTypePair(Record, Idx, NextValueNo, PersFn)) 41098f0fd8f6SDimitry Andric return error("Invalid record"); 41108f0fd8f6SDimitry Andric 41118f0fd8f6SDimitry Andric if (!F->hasPersonalityFn()) 41128f0fd8f6SDimitry Andric F->setPersonalityFn(cast<Constant>(PersFn)); 41138f0fd8f6SDimitry Andric else if (F->getPersonalityFn() != cast<Constant>(PersFn)) 41148f0fd8f6SDimitry Andric return error("Personality function mismatch"); 41158f0fd8f6SDimitry Andric } 41166122f3e6SDimitry Andric 41176122f3e6SDimitry Andric bool IsCleanup = !!Record[Idx++]; 41186122f3e6SDimitry Andric unsigned NumClauses = Record[Idx++]; 41198f0fd8f6SDimitry Andric LandingPadInst *LP = LandingPadInst::Create(Ty, NumClauses); 41206122f3e6SDimitry Andric LP->setCleanup(IsCleanup); 41216122f3e6SDimitry Andric for (unsigned J = 0; J != NumClauses; ++J) { 41226122f3e6SDimitry Andric LandingPadInst::ClauseType CT = 41236122f3e6SDimitry Andric LandingPadInst::ClauseType(Record[Idx++]); (void)CT; 41246122f3e6SDimitry Andric Value *Val; 41256122f3e6SDimitry Andric 41266122f3e6SDimitry Andric if (getValueTypePair(Record, Idx, NextValueNo, Val)) { 41276122f3e6SDimitry Andric delete LP; 41288f0fd8f6SDimitry Andric return error("Invalid record"); 41296122f3e6SDimitry Andric } 41306122f3e6SDimitry Andric 41316122f3e6SDimitry Andric assert((CT != LandingPadInst::Catch || 41326122f3e6SDimitry Andric !isa<ArrayType>(Val->getType())) && 41336122f3e6SDimitry Andric "Catch clause has a invalid type!"); 41346122f3e6SDimitry Andric assert((CT != LandingPadInst::Filter || 41356122f3e6SDimitry Andric isa<ArrayType>(Val->getType())) && 41366122f3e6SDimitry Andric "Filter clause has invalid type!"); 413791bc56edSDimitry Andric LP->addClause(cast<Constant>(Val)); 41386122f3e6SDimitry Andric } 41396122f3e6SDimitry Andric 41406122f3e6SDimitry Andric I = LP; 41416122f3e6SDimitry Andric InstructionList.push_back(I); 41426122f3e6SDimitry Andric break; 41436122f3e6SDimitry Andric } 41446122f3e6SDimitry Andric 414517a519f9SDimitry Andric case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align] 414617a519f9SDimitry Andric if (Record.size() != 4) 41478f0fd8f6SDimitry Andric return error("Invalid record"); 4148ff0cc061SDimitry Andric uint64_t AlignRecord = Record[3]; 4149ff0cc061SDimitry Andric const uint64_t InAllocaMask = uint64_t(1) << 5; 4150ff0cc061SDimitry Andric const uint64_t ExplicitTypeMask = uint64_t(1) << 6; 41513ca95b02SDimitry Andric const uint64_t SwiftErrorMask = uint64_t(1) << 7; 41523ca95b02SDimitry Andric const uint64_t FlagMask = InAllocaMask | ExplicitTypeMask | 41533ca95b02SDimitry Andric SwiftErrorMask; 4154ff0cc061SDimitry Andric bool InAlloca = AlignRecord & InAllocaMask; 41553ca95b02SDimitry Andric bool SwiftError = AlignRecord & SwiftErrorMask; 4156ff0cc061SDimitry Andric Type *Ty = getTypeByID(Record[0]); 4157ff0cc061SDimitry Andric if ((AlignRecord & ExplicitTypeMask) == 0) { 4158ff0cc061SDimitry Andric auto *PTy = dyn_cast_or_null<PointerType>(Ty); 4159ff0cc061SDimitry Andric if (!PTy) 41608f0fd8f6SDimitry Andric return error("Old-style alloca with a non-pointer type"); 4161ff0cc061SDimitry Andric Ty = PTy->getElementType(); 4162ff0cc061SDimitry Andric } 41636122f3e6SDimitry Andric Type *OpTy = getTypeByID(Record[1]); 416417a519f9SDimitry Andric Value *Size = getFnValueByID(Record[2], OpTy); 4165ff0cc061SDimitry Andric unsigned Align; 4166d88c1a5aSDimitry Andric if (Error Err = parseAlignmentValue(AlignRecord & ~FlagMask, Align)) { 4167d88c1a5aSDimitry Andric return Err; 4168ff0cc061SDimitry Andric } 4169f785676fSDimitry Andric if (!Ty || !Size) 41708f0fd8f6SDimitry Andric return error("Invalid record"); 41717a7e6055SDimitry Andric 41727a7e6055SDimitry Andric // FIXME: Make this an optional field. 41737a7e6055SDimitry Andric const DataLayout &DL = TheModule->getDataLayout(); 41747a7e6055SDimitry Andric unsigned AS = DL.getAllocaAddrSpace(); 41757a7e6055SDimitry Andric 41767a7e6055SDimitry Andric AllocaInst *AI = new AllocaInst(Ty, AS, Size, Align); 417791bc56edSDimitry Andric AI->setUsedWithInAlloca(InAlloca); 41783ca95b02SDimitry Andric AI->setSwiftError(SwiftError); 417991bc56edSDimitry Andric I = AI; 4180f22ef01cSRoman Divacky InstructionList.push_back(I); 4181f22ef01cSRoman Divacky break; 4182f22ef01cSRoman Divacky } 4183f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol] 4184f22ef01cSRoman Divacky unsigned OpNum = 0; 4185f22ef01cSRoman Divacky Value *Op; 4186f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Op) || 4187ff0cc061SDimitry Andric (OpNum + 2 != Record.size() && OpNum + 3 != Record.size())) 41888f0fd8f6SDimitry Andric return error("Invalid record"); 4189f22ef01cSRoman Divacky 4190ff0cc061SDimitry Andric Type *Ty = nullptr; 4191ff0cc061SDimitry Andric if (OpNum + 3 == Record.size()) 4192ff0cc061SDimitry Andric Ty = getTypeByID(Record[OpNum++]); 4193d88c1a5aSDimitry Andric if (Error Err = typeCheckLoadStoreInst(Ty, Op->getType())) 4194d88c1a5aSDimitry Andric return Err; 4195ff0cc061SDimitry Andric if (!Ty) 4196ff0cc061SDimitry Andric Ty = cast<PointerType>(Op->getType())->getElementType(); 4197ff0cc061SDimitry Andric 4198ff0cc061SDimitry Andric unsigned Align; 4199d88c1a5aSDimitry Andric if (Error Err = parseAlignmentValue(Record[OpNum], Align)) 4200d88c1a5aSDimitry Andric return Err; 4201ff0cc061SDimitry Andric I = new LoadInst(Ty, Op, "", Record[OpNum + 1], Align); 4202ff0cc061SDimitry Andric 4203f22ef01cSRoman Divacky InstructionList.push_back(I); 4204f22ef01cSRoman Divacky break; 4205f22ef01cSRoman Divacky } 42066122f3e6SDimitry Andric case bitc::FUNC_CODE_INST_LOADATOMIC: { 42076122f3e6SDimitry Andric // LOADATOMIC: [opty, op, align, vol, ordering, synchscope] 42086122f3e6SDimitry Andric unsigned OpNum = 0; 42096122f3e6SDimitry Andric Value *Op; 42106122f3e6SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Op) || 4211ff0cc061SDimitry Andric (OpNum + 4 != Record.size() && OpNum + 5 != Record.size())) 42128f0fd8f6SDimitry Andric return error("Invalid record"); 42136122f3e6SDimitry Andric 4214ff0cc061SDimitry Andric Type *Ty = nullptr; 4215ff0cc061SDimitry Andric if (OpNum + 5 == Record.size()) 4216ff0cc061SDimitry Andric Ty = getTypeByID(Record[OpNum++]); 4217d88c1a5aSDimitry Andric if (Error Err = typeCheckLoadStoreInst(Ty, Op->getType())) 4218d88c1a5aSDimitry Andric return Err; 4219ff0cc061SDimitry Andric if (!Ty) 4220ff0cc061SDimitry Andric Ty = cast<PointerType>(Op->getType())->getElementType(); 4221ff0cc061SDimitry Andric 42228f0fd8f6SDimitry Andric AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); 42233ca95b02SDimitry Andric if (Ordering == AtomicOrdering::NotAtomic || 42243ca95b02SDimitry Andric Ordering == AtomicOrdering::Release || 42253ca95b02SDimitry Andric Ordering == AtomicOrdering::AcquireRelease) 42268f0fd8f6SDimitry Andric return error("Invalid record"); 42273ca95b02SDimitry Andric if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0) 42288f0fd8f6SDimitry Andric return error("Invalid record"); 42298f0fd8f6SDimitry Andric SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]); 42306122f3e6SDimitry Andric 4231ff0cc061SDimitry Andric unsigned Align; 4232d88c1a5aSDimitry Andric if (Error Err = parseAlignmentValue(Record[OpNum], Align)) 4233d88c1a5aSDimitry Andric return Err; 4234ff0cc061SDimitry Andric I = new LoadInst(Op, "", Record[OpNum+1], Align, Ordering, SynchScope); 4235ff0cc061SDimitry Andric 42366122f3e6SDimitry Andric InstructionList.push_back(I); 42376122f3e6SDimitry Andric break; 42386122f3e6SDimitry Andric } 4239ff0cc061SDimitry Andric case bitc::FUNC_CODE_INST_STORE: 4240ff0cc061SDimitry Andric case bitc::FUNC_CODE_INST_STORE_OLD: { // STORE2:[ptrty, ptr, val, align, vol] 4241f22ef01cSRoman Divacky unsigned OpNum = 0; 4242f22ef01cSRoman Divacky Value *Val, *Ptr; 4243f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || 4244ff0cc061SDimitry Andric (BitCode == bitc::FUNC_CODE_INST_STORE 4245ff0cc061SDimitry Andric ? getValueTypePair(Record, OpNum, NextValueNo, Val) 4246ff0cc061SDimitry Andric : popValue(Record, OpNum, NextValueNo, 4247ff0cc061SDimitry Andric cast<PointerType>(Ptr->getType())->getElementType(), 4248ff0cc061SDimitry Andric Val)) || 4249f22ef01cSRoman Divacky OpNum + 2 != Record.size()) 42508f0fd8f6SDimitry Andric return error("Invalid record"); 4251f22ef01cSRoman Divacky 4252d88c1a5aSDimitry Andric if (Error Err = typeCheckLoadStoreInst(Val->getType(), Ptr->getType())) 4253d88c1a5aSDimitry Andric return Err; 4254ff0cc061SDimitry Andric unsigned Align; 4255d88c1a5aSDimitry Andric if (Error Err = parseAlignmentValue(Record[OpNum], Align)) 4256d88c1a5aSDimitry Andric return Err; 4257ff0cc061SDimitry Andric I = new StoreInst(Val, Ptr, Record[OpNum+1], Align); 4258f22ef01cSRoman Divacky InstructionList.push_back(I); 4259f22ef01cSRoman Divacky break; 4260f22ef01cSRoman Divacky } 4261ff0cc061SDimitry Andric case bitc::FUNC_CODE_INST_STOREATOMIC: 4262ff0cc061SDimitry Andric case bitc::FUNC_CODE_INST_STOREATOMIC_OLD: { 42636122f3e6SDimitry Andric // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, synchscope] 42646122f3e6SDimitry Andric unsigned OpNum = 0; 42656122f3e6SDimitry Andric Value *Val, *Ptr; 42666122f3e6SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || 42673ca95b02SDimitry Andric !isa<PointerType>(Ptr->getType()) || 4268ff0cc061SDimitry Andric (BitCode == bitc::FUNC_CODE_INST_STOREATOMIC 4269ff0cc061SDimitry Andric ? getValueTypePair(Record, OpNum, NextValueNo, Val) 4270ff0cc061SDimitry Andric : popValue(Record, OpNum, NextValueNo, 4271ff0cc061SDimitry Andric cast<PointerType>(Ptr->getType())->getElementType(), 4272ff0cc061SDimitry Andric Val)) || 42736122f3e6SDimitry Andric OpNum + 4 != Record.size()) 42748f0fd8f6SDimitry Andric return error("Invalid record"); 42756122f3e6SDimitry Andric 4276d88c1a5aSDimitry Andric if (Error Err = typeCheckLoadStoreInst(Val->getType(), Ptr->getType())) 4277d88c1a5aSDimitry Andric return Err; 42788f0fd8f6SDimitry Andric AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); 42793ca95b02SDimitry Andric if (Ordering == AtomicOrdering::NotAtomic || 42803ca95b02SDimitry Andric Ordering == AtomicOrdering::Acquire || 42813ca95b02SDimitry Andric Ordering == AtomicOrdering::AcquireRelease) 42828f0fd8f6SDimitry Andric return error("Invalid record"); 42838f0fd8f6SDimitry Andric SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]); 42843ca95b02SDimitry Andric if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0) 42858f0fd8f6SDimitry Andric return error("Invalid record"); 42866122f3e6SDimitry Andric 4287ff0cc061SDimitry Andric unsigned Align; 4288d88c1a5aSDimitry Andric if (Error Err = parseAlignmentValue(Record[OpNum], Align)) 4289d88c1a5aSDimitry Andric return Err; 4290ff0cc061SDimitry Andric I = new StoreInst(Val, Ptr, Record[OpNum+1], Align, Ordering, SynchScope); 42916122f3e6SDimitry Andric InstructionList.push_back(I); 42926122f3e6SDimitry Andric break; 42936122f3e6SDimitry Andric } 4294ff0cc061SDimitry Andric case bitc::FUNC_CODE_INST_CMPXCHG_OLD: 42956122f3e6SDimitry Andric case bitc::FUNC_CODE_INST_CMPXCHG: { 429691bc56edSDimitry Andric // CMPXCHG:[ptrty, ptr, cmp, new, vol, successordering, synchscope, 429791bc56edSDimitry Andric // failureordering?, isweak?] 42986122f3e6SDimitry Andric unsigned OpNum = 0; 42996122f3e6SDimitry Andric Value *Ptr, *Cmp, *New; 43006122f3e6SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || 4301ff0cc061SDimitry Andric (BitCode == bitc::FUNC_CODE_INST_CMPXCHG 4302ff0cc061SDimitry Andric ? getValueTypePair(Record, OpNum, NextValueNo, Cmp) 4303ff0cc061SDimitry Andric : popValue(Record, OpNum, NextValueNo, 4304ff0cc061SDimitry Andric cast<PointerType>(Ptr->getType())->getElementType(), 4305ff0cc061SDimitry Andric Cmp)) || 4306ff0cc061SDimitry Andric popValue(Record, OpNum, NextValueNo, Cmp->getType(), New) || 4307ff0cc061SDimitry Andric Record.size() < OpNum + 3 || Record.size() > OpNum + 5) 43088f0fd8f6SDimitry Andric return error("Invalid record"); 43098f0fd8f6SDimitry Andric AtomicOrdering SuccessOrdering = getDecodedOrdering(Record[OpNum + 1]); 43103ca95b02SDimitry Andric if (SuccessOrdering == AtomicOrdering::NotAtomic || 43113ca95b02SDimitry Andric SuccessOrdering == AtomicOrdering::Unordered) 43128f0fd8f6SDimitry Andric return error("Invalid record"); 43138f0fd8f6SDimitry Andric SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 2]); 431491bc56edSDimitry Andric 4315d88c1a5aSDimitry Andric if (Error Err = typeCheckLoadStoreInst(Cmp->getType(), Ptr->getType())) 4316d88c1a5aSDimitry Andric return Err; 431791bc56edSDimitry Andric AtomicOrdering FailureOrdering; 431891bc56edSDimitry Andric if (Record.size() < 7) 431991bc56edSDimitry Andric FailureOrdering = 432091bc56edSDimitry Andric AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering); 432191bc56edSDimitry Andric else 43228f0fd8f6SDimitry Andric FailureOrdering = getDecodedOrdering(Record[OpNum + 3]); 432391bc56edSDimitry Andric 432491bc56edSDimitry Andric I = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering, FailureOrdering, 432591bc56edSDimitry Andric SynchScope); 43266122f3e6SDimitry Andric cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]); 432791bc56edSDimitry Andric 432891bc56edSDimitry Andric if (Record.size() < 8) { 432991bc56edSDimitry Andric // Before weak cmpxchgs existed, the instruction simply returned the 433091bc56edSDimitry Andric // value loaded from memory, so bitcode files from that era will be 433191bc56edSDimitry Andric // expecting the first component of a modern cmpxchg. 433291bc56edSDimitry Andric CurBB->getInstList().push_back(I); 433391bc56edSDimitry Andric I = ExtractValueInst::Create(I, 0); 433491bc56edSDimitry Andric } else { 433591bc56edSDimitry Andric cast<AtomicCmpXchgInst>(I)->setWeak(Record[OpNum+4]); 433691bc56edSDimitry Andric } 433791bc56edSDimitry Andric 43386122f3e6SDimitry Andric InstructionList.push_back(I); 43396122f3e6SDimitry Andric break; 43406122f3e6SDimitry Andric } 43416122f3e6SDimitry Andric case bitc::FUNC_CODE_INST_ATOMICRMW: { 43426122f3e6SDimitry Andric // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, synchscope] 43436122f3e6SDimitry Andric unsigned OpNum = 0; 43446122f3e6SDimitry Andric Value *Ptr, *Val; 43456122f3e6SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || 43463ca95b02SDimitry Andric !isa<PointerType>(Ptr->getType()) || 43473861d79fSDimitry Andric popValue(Record, OpNum, NextValueNo, 43486122f3e6SDimitry Andric cast<PointerType>(Ptr->getType())->getElementType(), Val) || 43496122f3e6SDimitry Andric OpNum+4 != Record.size()) 43508f0fd8f6SDimitry Andric return error("Invalid record"); 43518f0fd8f6SDimitry Andric AtomicRMWInst::BinOp Operation = getDecodedRMWOperation(Record[OpNum]); 43526122f3e6SDimitry Andric if (Operation < AtomicRMWInst::FIRST_BINOP || 43536122f3e6SDimitry Andric Operation > AtomicRMWInst::LAST_BINOP) 43548f0fd8f6SDimitry Andric return error("Invalid record"); 43558f0fd8f6SDimitry Andric AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); 43563ca95b02SDimitry Andric if (Ordering == AtomicOrdering::NotAtomic || 43573ca95b02SDimitry Andric Ordering == AtomicOrdering::Unordered) 43588f0fd8f6SDimitry Andric return error("Invalid record"); 43598f0fd8f6SDimitry Andric SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]); 43606122f3e6SDimitry Andric I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SynchScope); 43616122f3e6SDimitry Andric cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]); 43626122f3e6SDimitry Andric InstructionList.push_back(I); 43636122f3e6SDimitry Andric break; 43646122f3e6SDimitry Andric } 43656122f3e6SDimitry Andric case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, synchscope] 43666122f3e6SDimitry Andric if (2 != Record.size()) 43678f0fd8f6SDimitry Andric return error("Invalid record"); 43688f0fd8f6SDimitry Andric AtomicOrdering Ordering = getDecodedOrdering(Record[0]); 43693ca95b02SDimitry Andric if (Ordering == AtomicOrdering::NotAtomic || 43703ca95b02SDimitry Andric Ordering == AtomicOrdering::Unordered || 43713ca95b02SDimitry Andric Ordering == AtomicOrdering::Monotonic) 43728f0fd8f6SDimitry Andric return error("Invalid record"); 43738f0fd8f6SDimitry Andric SynchronizationScope SynchScope = getDecodedSynchScope(Record[1]); 43746122f3e6SDimitry Andric I = new FenceInst(Context, Ordering, SynchScope); 43756122f3e6SDimitry Andric InstructionList.push_back(I); 43766122f3e6SDimitry Andric break; 43776122f3e6SDimitry Andric } 437817a519f9SDimitry Andric case bitc::FUNC_CODE_INST_CALL: { 43797d523365SDimitry Andric // CALL: [paramattrs, cc, fmf, fnty, fnid, arg0, arg1...] 4380f22ef01cSRoman Divacky if (Record.size() < 3) 43818f0fd8f6SDimitry Andric return error("Invalid record"); 4382f22ef01cSRoman Divacky 4383ff0cc061SDimitry Andric unsigned OpNum = 0; 43847a7e6055SDimitry Andric AttributeList PAL = getAttributes(Record[OpNum++]); 4385ff0cc061SDimitry Andric unsigned CCInfo = Record[OpNum++]; 4386f22ef01cSRoman Divacky 43877d523365SDimitry Andric FastMathFlags FMF; 43887d523365SDimitry Andric if ((CCInfo >> bitc::CALL_FMF) & 1) { 43897d523365SDimitry Andric FMF = getDecodedFastMathFlags(Record[OpNum++]); 43907d523365SDimitry Andric if (!FMF.any()) 43917d523365SDimitry Andric return error("Fast math flags indicator set for call with no FMF"); 43927d523365SDimitry Andric } 43937d523365SDimitry Andric 4394ff0cc061SDimitry Andric FunctionType *FTy = nullptr; 43957d523365SDimitry Andric if (CCInfo >> bitc::CALL_EXPLICIT_TYPE & 1 && 4396ff0cc061SDimitry Andric !(FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++])))) 43978f0fd8f6SDimitry Andric return error("Explicit call type is not a function type"); 4398ff0cc061SDimitry Andric 4399f22ef01cSRoman Divacky Value *Callee; 4400f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) 44018f0fd8f6SDimitry Andric return error("Invalid record"); 4402f22ef01cSRoman Divacky 44036122f3e6SDimitry Andric PointerType *OpTy = dyn_cast<PointerType>(Callee->getType()); 4404ff0cc061SDimitry Andric if (!OpTy) 44058f0fd8f6SDimitry Andric return error("Callee is not a pointer type"); 4406ff0cc061SDimitry Andric if (!FTy) { 4407ff0cc061SDimitry Andric FTy = dyn_cast<FunctionType>(OpTy->getElementType()); 4408ff0cc061SDimitry Andric if (!FTy) 44098f0fd8f6SDimitry Andric return error("Callee is not of pointer to function type"); 4410ff0cc061SDimitry Andric } else if (OpTy->getElementType() != FTy) 44118f0fd8f6SDimitry Andric return error("Explicit call type does not match pointee type of " 4412ff0cc061SDimitry Andric "callee operand"); 4413ff0cc061SDimitry Andric if (Record.size() < FTy->getNumParams() + OpNum) 44148f0fd8f6SDimitry Andric return error("Insufficient operands to call"); 4415f22ef01cSRoman Divacky 4416f22ef01cSRoman Divacky SmallVector<Value*, 16> Args; 4417f22ef01cSRoman Divacky // Read the fixed params. 4418f22ef01cSRoman Divacky for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { 441917a519f9SDimitry Andric if (FTy->getParamType(i)->isLabelTy()) 4420f22ef01cSRoman Divacky Args.push_back(getBasicBlock(Record[OpNum])); 4421f22ef01cSRoman Divacky else 44223861d79fSDimitry Andric Args.push_back(getValue(Record, OpNum, NextValueNo, 44233861d79fSDimitry Andric FTy->getParamType(i))); 442491bc56edSDimitry Andric if (!Args.back()) 44258f0fd8f6SDimitry Andric return error("Invalid record"); 4426f22ef01cSRoman Divacky } 4427f22ef01cSRoman Divacky 4428f22ef01cSRoman Divacky // Read type/value pairs for varargs params. 4429f22ef01cSRoman Divacky if (!FTy->isVarArg()) { 4430f22ef01cSRoman Divacky if (OpNum != Record.size()) 44318f0fd8f6SDimitry Andric return error("Invalid record"); 4432f22ef01cSRoman Divacky } else { 4433f22ef01cSRoman Divacky while (OpNum != Record.size()) { 4434f22ef01cSRoman Divacky Value *Op; 4435f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Op)) 44368f0fd8f6SDimitry Andric return error("Invalid record"); 4437f22ef01cSRoman Divacky Args.push_back(Op); 4438f22ef01cSRoman Divacky } 4439f22ef01cSRoman Divacky } 4440f22ef01cSRoman Divacky 44417d523365SDimitry Andric I = CallInst::Create(FTy, Callee, Args, OperandBundles); 44427d523365SDimitry Andric OperandBundles.clear(); 4443f22ef01cSRoman Divacky InstructionList.push_back(I); 4444f22ef01cSRoman Divacky cast<CallInst>(I)->setCallingConv( 44457d523365SDimitry Andric static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV)); 444691bc56edSDimitry Andric CallInst::TailCallKind TCK = CallInst::TCK_None; 44477d523365SDimitry Andric if (CCInfo & 1 << bitc::CALL_TAIL) 444891bc56edSDimitry Andric TCK = CallInst::TCK_Tail; 44497d523365SDimitry Andric if (CCInfo & (1 << bitc::CALL_MUSTTAIL)) 445091bc56edSDimitry Andric TCK = CallInst::TCK_MustTail; 44517d523365SDimitry Andric if (CCInfo & (1 << bitc::CALL_NOTAIL)) 44527d523365SDimitry Andric TCK = CallInst::TCK_NoTail; 445391bc56edSDimitry Andric cast<CallInst>(I)->setTailCallKind(TCK); 4454f22ef01cSRoman Divacky cast<CallInst>(I)->setAttributes(PAL); 44557d523365SDimitry Andric if (FMF.any()) { 44567d523365SDimitry Andric if (!isa<FPMathOperator>(I)) 44577d523365SDimitry Andric return error("Fast-math-flags specified for call without " 44587d523365SDimitry Andric "floating-point scalar or vector return type"); 44597d523365SDimitry Andric I->setFastMathFlags(FMF); 44607d523365SDimitry Andric } 4461f22ef01cSRoman Divacky break; 4462f22ef01cSRoman Divacky } 4463f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty] 4464f22ef01cSRoman Divacky if (Record.size() < 3) 44658f0fd8f6SDimitry Andric return error("Invalid record"); 44666122f3e6SDimitry Andric Type *OpTy = getTypeByID(Record[0]); 44673861d79fSDimitry Andric Value *Op = getValue(Record, 1, NextValueNo, OpTy); 44686122f3e6SDimitry Andric Type *ResTy = getTypeByID(Record[2]); 4469f22ef01cSRoman Divacky if (!OpTy || !Op || !ResTy) 44708f0fd8f6SDimitry Andric return error("Invalid record"); 4471f22ef01cSRoman Divacky I = new VAArgInst(Op, ResTy); 4472f22ef01cSRoman Divacky InstructionList.push_back(I); 4473f22ef01cSRoman Divacky break; 4474f22ef01cSRoman Divacky } 44757d523365SDimitry Andric 44767d523365SDimitry Andric case bitc::FUNC_CODE_OPERAND_BUNDLE: { 44777d523365SDimitry Andric // A call or an invoke can be optionally prefixed with some variable 44787d523365SDimitry Andric // number of operand bundle blocks. These blocks are read into 44797d523365SDimitry Andric // OperandBundles and consumed at the next call or invoke instruction. 44807d523365SDimitry Andric 44817d523365SDimitry Andric if (Record.size() < 1 || Record[0] >= BundleTags.size()) 44827d523365SDimitry Andric return error("Invalid record"); 44837d523365SDimitry Andric 44847d523365SDimitry Andric std::vector<Value *> Inputs; 44857d523365SDimitry Andric 44867d523365SDimitry Andric unsigned OpNum = 1; 44877d523365SDimitry Andric while (OpNum != Record.size()) { 44887d523365SDimitry Andric Value *Op; 44897d523365SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Op)) 44907d523365SDimitry Andric return error("Invalid record"); 44917d523365SDimitry Andric Inputs.push_back(Op); 44927d523365SDimitry Andric } 44937d523365SDimitry Andric 44947d523365SDimitry Andric OperandBundles.emplace_back(BundleTags[Record[0]], std::move(Inputs)); 44957d523365SDimitry Andric continue; 44967d523365SDimitry Andric } 4497f22ef01cSRoman Divacky } 4498f22ef01cSRoman Divacky 4499f22ef01cSRoman Divacky // Add instruction to end of current BB. If there is no current BB, reject 4500f22ef01cSRoman Divacky // this file. 450191bc56edSDimitry Andric if (!CurBB) { 4502d8866befSDimitry Andric I->deleteValue(); 45038f0fd8f6SDimitry Andric return error("Invalid instruction with no BB"); 4504f22ef01cSRoman Divacky } 45057d523365SDimitry Andric if (!OperandBundles.empty()) { 4506d8866befSDimitry Andric I->deleteValue(); 45077d523365SDimitry Andric return error("Operand bundles found with no consumer"); 45087d523365SDimitry Andric } 4509f22ef01cSRoman Divacky CurBB->getInstList().push_back(I); 4510f22ef01cSRoman Divacky 4511f22ef01cSRoman Divacky // If this was a terminator instruction, move to the next block. 4512f22ef01cSRoman Divacky if (isa<TerminatorInst>(I)) { 4513f22ef01cSRoman Divacky ++CurBBNo; 451491bc56edSDimitry Andric CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr; 4515f22ef01cSRoman Divacky } 4516f22ef01cSRoman Divacky 4517f22ef01cSRoman Divacky // Non-void values get registered in the value table for future use. 4518f22ef01cSRoman Divacky if (I && !I->getType()->isVoidTy()) 45198f0fd8f6SDimitry Andric ValueList.assignValue(I, NextValueNo++); 4520f22ef01cSRoman Divacky } 4521f22ef01cSRoman Divacky 4522139f7f9bSDimitry Andric OutOfRecordLoop: 4523139f7f9bSDimitry Andric 45247d523365SDimitry Andric if (!OperandBundles.empty()) 45257d523365SDimitry Andric return error("Operand bundles found with no consumer"); 45267d523365SDimitry Andric 4527f22ef01cSRoman Divacky // Check the function list for unresolved values. 4528f22ef01cSRoman Divacky if (Argument *A = dyn_cast<Argument>(ValueList.back())) { 452991bc56edSDimitry Andric if (!A->getParent()) { 4530f22ef01cSRoman Divacky // We found at least one unresolved value. Nuke them all to avoid leaks. 4531f22ef01cSRoman Divacky for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){ 453291bc56edSDimitry Andric if ((A = dyn_cast_or_null<Argument>(ValueList[i])) && !A->getParent()) { 4533f22ef01cSRoman Divacky A->replaceAllUsesWith(UndefValue::get(A->getType())); 4534f22ef01cSRoman Divacky delete A; 4535f22ef01cSRoman Divacky } 4536f22ef01cSRoman Divacky } 45378f0fd8f6SDimitry Andric return error("Never resolved value found in function"); 4538f22ef01cSRoman Divacky } 4539f22ef01cSRoman Divacky } 4540f22ef01cSRoman Divacky 45413ca95b02SDimitry Andric // Unexpected unresolved metadata about to be dropped. 4542d88c1a5aSDimitry Andric if (MDLoader->hasFwdRefs()) 45433ca95b02SDimitry Andric return error("Invalid function metadata: outgoing forward refs"); 4544e580952dSDimitry Andric 4545f22ef01cSRoman Divacky // Trim the value list down to the size it was before we parsed this function. 4546f22ef01cSRoman Divacky ValueList.shrinkTo(ModuleValueListSize); 4547d88c1a5aSDimitry Andric MDLoader->shrinkTo(ModuleMDLoaderSize); 4548f22ef01cSRoman Divacky std::vector<BasicBlock*>().swap(FunctionBBs); 4549d88c1a5aSDimitry Andric return Error::success(); 4550f22ef01cSRoman Divacky } 4551f22ef01cSRoman Divacky 4552f785676fSDimitry Andric /// Find the function body in the bitcode stream 4553d88c1a5aSDimitry Andric Error BitcodeReader::findFunctionInStream( 455491bc56edSDimitry Andric Function *F, 4555dff0c46cSDimitry Andric DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) { 4556dff0c46cSDimitry Andric while (DeferredFunctionInfoIterator->second == 0) { 45577d523365SDimitry Andric // This is the fallback handling for the old format bitcode that 45587d523365SDimitry Andric // didn't contain the function index in the VST, or when we have 45597d523365SDimitry Andric // an anonymous function which would not have a VST entry. 45607d523365SDimitry Andric // Assert that we have one of those two cases. 45617d523365SDimitry Andric assert(VSTOffset == 0 || !F->hasName()); 45627d523365SDimitry Andric // Parse the next body in the stream and set its position in the 45637d523365SDimitry Andric // DeferredFunctionInfo map. 4564d88c1a5aSDimitry Andric if (Error Err = rememberAndSkipFunctionBodies()) 4565d88c1a5aSDimitry Andric return Err; 4566dff0c46cSDimitry Andric } 4567d88c1a5aSDimitry Andric return Error::success(); 4568dff0c46cSDimitry Andric } 4569dff0c46cSDimitry Andric 4570f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 4571f22ef01cSRoman Divacky // GVMaterializer implementation 4572f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 4573f22ef01cSRoman Divacky 4574d88c1a5aSDimitry Andric Error BitcodeReader::materialize(GlobalValue *GV) { 4575f22ef01cSRoman Divacky Function *F = dyn_cast<Function>(GV); 4576f22ef01cSRoman Divacky // If it's not a function or is already material, ignore the request. 4577f785676fSDimitry Andric if (!F || !F->isMaterializable()) 4578d88c1a5aSDimitry Andric return Error::success(); 4579f22ef01cSRoman Divacky 4580f22ef01cSRoman Divacky DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F); 4581f22ef01cSRoman Divacky assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!"); 4582dff0c46cSDimitry Andric // If its position is recorded as 0, its body is somewhere in the stream 4583dff0c46cSDimitry Andric // but we haven't seen it yet. 45843dac3a9bSDimitry Andric if (DFII->second == 0) 4585d88c1a5aSDimitry Andric if (Error Err = findFunctionInStream(F, DFII)) 4586d88c1a5aSDimitry Andric return Err; 4587f22ef01cSRoman Divacky 45883ca95b02SDimitry Andric // Materialize metadata before parsing any function bodies. 4589d88c1a5aSDimitry Andric if (Error Err = materializeMetadata()) 4590d88c1a5aSDimitry Andric return Err; 45913ca95b02SDimitry Andric 4592f22ef01cSRoman Divacky // Move the bit stream to the saved position of the deferred function body. 4593f22ef01cSRoman Divacky Stream.JumpToBit(DFII->second); 4594f22ef01cSRoman Divacky 4595d88c1a5aSDimitry Andric if (Error Err = parseFunctionBody(F)) 4596d88c1a5aSDimitry Andric return Err; 459739d628a0SDimitry Andric F->setIsMaterializable(false); 4598f22ef01cSRoman Divacky 4599ff0cc061SDimitry Andric if (StripDebugInfo) 4600ff0cc061SDimitry Andric stripDebugInfo(*F); 4601ff0cc061SDimitry Andric 4602f22ef01cSRoman Divacky // Upgrade any old intrinsic calls in the function. 46033dac3a9bSDimitry Andric for (auto &I : UpgradedIntrinsics) { 46047d523365SDimitry Andric for (auto UI = I.first->materialized_user_begin(), UE = I.first->user_end(); 46057d523365SDimitry Andric UI != UE;) { 46063dac3a9bSDimitry Andric User *U = *UI; 46073dac3a9bSDimitry Andric ++UI; 46083dac3a9bSDimitry Andric if (CallInst *CI = dyn_cast<CallInst>(U)) 46093dac3a9bSDimitry Andric UpgradeIntrinsicCall(CI, I.second); 4610f22ef01cSRoman Divacky } 4611f22ef01cSRoman Divacky } 4612f22ef01cSRoman Divacky 46133ca95b02SDimitry Andric // Update calls to the remangled intrinsics 46143ca95b02SDimitry Andric for (auto &I : RemangledIntrinsics) 46153ca95b02SDimitry Andric for (auto UI = I.first->materialized_user_begin(), UE = I.first->user_end(); 46163ca95b02SDimitry Andric UI != UE;) 46173ca95b02SDimitry Andric // Don't expect any other users than call sites 46183ca95b02SDimitry Andric CallSite(*UI++).setCalledFunction(I.second); 46193ca95b02SDimitry Andric 46207d523365SDimitry Andric // Finish fn->subprogram upgrade for materialized functions. 4621d88c1a5aSDimitry Andric if (DISubprogram *SP = MDLoader->lookupSubprogramForFunction(F)) 46227d523365SDimitry Andric F->setSubprogram(SP); 46237d523365SDimitry Andric 4624d88c1a5aSDimitry Andric // Check if the TBAA Metadata are valid, otherwise we will need to strip them. 4625d88c1a5aSDimitry Andric if (!MDLoader->isStrippingTBAA()) { 4626d88c1a5aSDimitry Andric for (auto &I : instructions(F)) { 4627d88c1a5aSDimitry Andric MDNode *TBAA = I.getMetadata(LLVMContext::MD_tbaa); 4628d88c1a5aSDimitry Andric if (!TBAA || TBAAVerifyHelper.visitTBAAMetadata(I, TBAA)) 4629d88c1a5aSDimitry Andric continue; 4630d88c1a5aSDimitry Andric MDLoader->setStripTBAA(true); 4631d88c1a5aSDimitry Andric stripTBAA(F->getParent()); 4632d88c1a5aSDimitry Andric } 4633d88c1a5aSDimitry Andric } 4634d88c1a5aSDimitry Andric 463539d628a0SDimitry Andric // Bring in any functions that this function forward-referenced via 463639d628a0SDimitry Andric // blockaddresses. 463739d628a0SDimitry Andric return materializeForwardReferencedFunctions(); 4638f22ef01cSRoman Divacky } 4639f22ef01cSRoman Divacky 4640d88c1a5aSDimitry Andric Error BitcodeReader::materializeModule() { 4641d88c1a5aSDimitry Andric if (Error Err = materializeMetadata()) 4642d88c1a5aSDimitry Andric return Err; 4643ff0cc061SDimitry Andric 464439d628a0SDimitry Andric // Promise to materialize all forward references. 464539d628a0SDimitry Andric WillMaterializeAllForwardRefs = true; 464639d628a0SDimitry Andric 4647f22ef01cSRoman Divacky // Iterate over the module, deserializing any functions that are still on 4648f22ef01cSRoman Divacky // disk. 46497d523365SDimitry Andric for (Function &F : *TheModule) { 4650d88c1a5aSDimitry Andric if (Error Err = materialize(&F)) 4651d88c1a5aSDimitry Andric return Err; 4652f785676fSDimitry Andric } 46537d523365SDimitry Andric // At this point, if there are any function bodies, parse the rest of 46547d523365SDimitry Andric // the bits in the module past the last function block we have recorded 46557d523365SDimitry Andric // through either lazy scanning or the VST. 46567d523365SDimitry Andric if (LastFunctionBlockBit || NextUnreadBit) 4657d88c1a5aSDimitry Andric if (Error Err = parseModule(LastFunctionBlockBit > NextUnreadBit 4658d88c1a5aSDimitry Andric ? LastFunctionBlockBit 4659d88c1a5aSDimitry Andric : NextUnreadBit)) 4660d88c1a5aSDimitry Andric return Err; 4661dff0c46cSDimitry Andric 466239d628a0SDimitry Andric // Check that all block address forward references got resolved (as we 466339d628a0SDimitry Andric // promised above). 466439d628a0SDimitry Andric if (!BasicBlockFwdRefs.empty()) 46658f0fd8f6SDimitry Andric return error("Never resolved function from blockaddress"); 466639d628a0SDimitry Andric 4667f22ef01cSRoman Divacky // Upgrade any intrinsic calls that slipped through (should not happen!) and 4668f22ef01cSRoman Divacky // delete the old functions to clean up. We can't do this unless the entire 4669f22ef01cSRoman Divacky // module is materialized because there could always be another function body 4670f22ef01cSRoman Divacky // with calls to the old function. 46713dac3a9bSDimitry Andric for (auto &I : UpgradedIntrinsics) { 46723dac3a9bSDimitry Andric for (auto *U : I.first->users()) { 46733dac3a9bSDimitry Andric if (CallInst *CI = dyn_cast<CallInst>(U)) 46743dac3a9bSDimitry Andric UpgradeIntrinsicCall(CI, I.second); 4675f22ef01cSRoman Divacky } 46763dac3a9bSDimitry Andric if (!I.first->use_empty()) 46773dac3a9bSDimitry Andric I.first->replaceAllUsesWith(I.second); 46783dac3a9bSDimitry Andric I.first->eraseFromParent(); 4679f22ef01cSRoman Divacky } 46803dac3a9bSDimitry Andric UpgradedIntrinsics.clear(); 46813ca95b02SDimitry Andric // Do the same for remangled intrinsics 46823ca95b02SDimitry Andric for (auto &I : RemangledIntrinsics) { 46833ca95b02SDimitry Andric I.first->replaceAllUsesWith(I.second); 46843ca95b02SDimitry Andric I.first->eraseFromParent(); 46853ca95b02SDimitry Andric } 46863ca95b02SDimitry Andric RemangledIntrinsics.clear(); 4687f785676fSDimitry Andric 46887d523365SDimitry Andric UpgradeDebugInfo(*TheModule); 46893ca95b02SDimitry Andric 46903ca95b02SDimitry Andric UpgradeModuleFlags(*TheModule); 4691d88c1a5aSDimitry Andric return Error::success(); 4692dff0c46cSDimitry Andric } 46936122f3e6SDimitry Andric 469439d628a0SDimitry Andric std::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const { 469539d628a0SDimitry Andric return IdentifiedStructTypes; 469639d628a0SDimitry Andric } 469739d628a0SDimitry Andric 46983ca95b02SDimitry Andric ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader( 4699f37b6182SDimitry Andric BitstreamCursor Cursor, StringRef Strtab, ModuleSummaryIndex &TheIndex, 4700f37b6182SDimitry Andric StringRef ModulePath, unsigned ModuleId) 4701f37b6182SDimitry Andric : BitcodeReaderBase(std::move(Cursor), Strtab), TheIndex(TheIndex), 4702f37b6182SDimitry Andric ModulePath(ModulePath), ModuleId(ModuleId) {} 4703f37b6182SDimitry Andric 470424d58133SDimitry Andric ModuleSummaryIndex::ModuleInfo * 470524d58133SDimitry Andric ModuleSummaryIndexBitcodeReader::addThisModule() { 470624d58133SDimitry Andric return TheIndex.addModule(ModulePath, ModuleId); 4707f37b6182SDimitry Andric } 47083ca95b02SDimitry Andric 47090f5676f4SDimitry Andric std::pair<ValueInfo, GlobalValue::GUID> 47100f5676f4SDimitry Andric ModuleSummaryIndexBitcodeReader::getValueInfoFromValueId(unsigned ValueId) { 47110f5676f4SDimitry Andric auto VGI = ValueIdToValueInfoMap[ValueId]; 47120f5676f4SDimitry Andric assert(VGI.first); 47130f5676f4SDimitry Andric return VGI; 47147d523365SDimitry Andric } 47157d523365SDimitry Andric 47166bc11b14SDimitry Andric void ModuleSummaryIndexBitcodeReader::setValueGUID( 47176bc11b14SDimitry Andric uint64_t ValueID, StringRef ValueName, GlobalValue::LinkageTypes Linkage, 47186bc11b14SDimitry Andric StringRef SourceFileName) { 47196bc11b14SDimitry Andric std::string GlobalId = 47206bc11b14SDimitry Andric GlobalValue::getGlobalIdentifier(ValueName, Linkage, SourceFileName); 47216bc11b14SDimitry Andric auto ValueGUID = GlobalValue::getGUID(GlobalId); 47226bc11b14SDimitry Andric auto OriginalNameID = ValueGUID; 47236bc11b14SDimitry Andric if (GlobalValue::isLocalLinkage(Linkage)) 47246bc11b14SDimitry Andric OriginalNameID = GlobalValue::getGUID(ValueName); 47256bc11b14SDimitry Andric if (PrintSummaryGUIDs) 47266bc11b14SDimitry Andric dbgs() << "GUID " << ValueGUID << "(" << OriginalNameID << ") is " 47276bc11b14SDimitry Andric << ValueName << "\n"; 47280f5676f4SDimitry Andric ValueIdToValueInfoMap[ValueID] = 47290f5676f4SDimitry Andric std::make_pair(TheIndex.getOrInsertValueInfo(ValueGUID), OriginalNameID); 47306bc11b14SDimitry Andric } 47316bc11b14SDimitry Andric 47323ca95b02SDimitry Andric // Specialized value symbol table parser used when reading module index 47333ca95b02SDimitry Andric // blocks where we don't actually create global values. The parsed information 47343ca95b02SDimitry Andric // is saved in the bitcode reader for use when later parsing summaries. 4735d88c1a5aSDimitry Andric Error ModuleSummaryIndexBitcodeReader::parseValueSymbolTable( 47363ca95b02SDimitry Andric uint64_t Offset, 47373ca95b02SDimitry Andric DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap) { 47386bc11b14SDimitry Andric // With a strtab the VST is not required to parse the summary. 47396bc11b14SDimitry Andric if (UseStrtab) 47406bc11b14SDimitry Andric return Error::success(); 47416bc11b14SDimitry Andric 47423ca95b02SDimitry Andric assert(Offset > 0 && "Expected non-zero VST offset"); 47433ca95b02SDimitry Andric uint64_t CurrentBit = jumpToValueSymbolTable(Offset, Stream); 47447d523365SDimitry Andric 47457d523365SDimitry Andric if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) 47467d523365SDimitry Andric return error("Invalid record"); 47477d523365SDimitry Andric 47487d523365SDimitry Andric SmallVector<uint64_t, 64> Record; 47497d523365SDimitry Andric 47507d523365SDimitry Andric // Read all the records for this value table. 47517d523365SDimitry Andric SmallString<128> ValueName; 4752d88c1a5aSDimitry Andric 4753d88c1a5aSDimitry Andric while (true) { 47547d523365SDimitry Andric BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 47557d523365SDimitry Andric 47567d523365SDimitry Andric switch (Entry.Kind) { 47577d523365SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 47587d523365SDimitry Andric case BitstreamEntry::Error: 47597d523365SDimitry Andric return error("Malformed block"); 47607d523365SDimitry Andric case BitstreamEntry::EndBlock: 47613ca95b02SDimitry Andric // Done parsing VST, jump back to wherever we came from. 47623ca95b02SDimitry Andric Stream.JumpToBit(CurrentBit); 4763d88c1a5aSDimitry Andric return Error::success(); 47647d523365SDimitry Andric case BitstreamEntry::Record: 47657d523365SDimitry Andric // The interesting case. 47667d523365SDimitry Andric break; 47677d523365SDimitry Andric } 47687d523365SDimitry Andric 47697d523365SDimitry Andric // Read a record. 47707d523365SDimitry Andric Record.clear(); 47717d523365SDimitry Andric switch (Stream.readRecord(Entry.ID, Record)) { 47727d523365SDimitry Andric default: // Default behavior: ignore (e.g. VST_CODE_BBENTRY records). 47737d523365SDimitry Andric break; 47743ca95b02SDimitry Andric case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N] 47753ca95b02SDimitry Andric if (convertToString(Record, 1, ValueName)) 47763ca95b02SDimitry Andric return error("Invalid record"); 47773ca95b02SDimitry Andric unsigned ValueID = Record[0]; 47783ca95b02SDimitry Andric assert(!SourceFileName.empty()); 47793ca95b02SDimitry Andric auto VLI = ValueIdToLinkageMap.find(ValueID); 47803ca95b02SDimitry Andric assert(VLI != ValueIdToLinkageMap.end() && 47813ca95b02SDimitry Andric "No linkage found for VST entry?"); 47823ca95b02SDimitry Andric auto Linkage = VLI->second; 47836bc11b14SDimitry Andric setValueGUID(ValueID, ValueName, Linkage, SourceFileName); 47843ca95b02SDimitry Andric ValueName.clear(); 47853ca95b02SDimitry Andric break; 47863ca95b02SDimitry Andric } 47877d523365SDimitry Andric case bitc::VST_CODE_FNENTRY: { 47883ca95b02SDimitry Andric // VST_CODE_FNENTRY: [valueid, offset, namechar x N] 47897d523365SDimitry Andric if (convertToString(Record, 2, ValueName)) 47907d523365SDimitry Andric return error("Invalid record"); 47917d523365SDimitry Andric unsigned ValueID = Record[0]; 47923ca95b02SDimitry Andric assert(!SourceFileName.empty()); 47933ca95b02SDimitry Andric auto VLI = ValueIdToLinkageMap.find(ValueID); 47943ca95b02SDimitry Andric assert(VLI != ValueIdToLinkageMap.end() && 47953ca95b02SDimitry Andric "No linkage found for VST entry?"); 47963ca95b02SDimitry Andric auto Linkage = VLI->second; 47976bc11b14SDimitry Andric setValueGUID(ValueID, ValueName, Linkage, SourceFileName); 47987d523365SDimitry Andric ValueName.clear(); 47997d523365SDimitry Andric break; 48007d523365SDimitry Andric } 48013ca95b02SDimitry Andric case bitc::VST_CODE_COMBINED_ENTRY: { 48023ca95b02SDimitry Andric // VST_CODE_COMBINED_ENTRY: [valueid, refguid] 48033ca95b02SDimitry Andric unsigned ValueID = Record[0]; 48043ca95b02SDimitry Andric GlobalValue::GUID RefGUID = Record[1]; 48053ca95b02SDimitry Andric // The "original name", which is the second value of the pair will be 48063ca95b02SDimitry Andric // overriden later by a FS_COMBINED_ORIGINAL_NAME in the combined index. 48070f5676f4SDimitry Andric ValueIdToValueInfoMap[ValueID] = 48080f5676f4SDimitry Andric std::make_pair(TheIndex.getOrInsertValueInfo(RefGUID), RefGUID); 48097d523365SDimitry Andric break; 48107d523365SDimitry Andric } 48117d523365SDimitry Andric } 48127d523365SDimitry Andric } 48137d523365SDimitry Andric } 48147d523365SDimitry Andric 48153ca95b02SDimitry Andric // Parse just the blocks needed for building the index out of the module. 48163ca95b02SDimitry Andric // At the end of this routine the module Index is populated with a map 48173ca95b02SDimitry Andric // from global value id to GlobalValueSummary objects. 4818f37b6182SDimitry Andric Error ModuleSummaryIndexBitcodeReader::parseModule() { 48197d523365SDimitry Andric if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 48207d523365SDimitry Andric return error("Invalid record"); 48217d523365SDimitry Andric 48223ca95b02SDimitry Andric SmallVector<uint64_t, 64> Record; 48233ca95b02SDimitry Andric DenseMap<unsigned, GlobalValue::LinkageTypes> ValueIdToLinkageMap; 48243ca95b02SDimitry Andric unsigned ValueId = 0; 48253ca95b02SDimitry Andric 48263ca95b02SDimitry Andric // Read the index for this module. 4827d88c1a5aSDimitry Andric while (true) { 48287d523365SDimitry Andric BitstreamEntry Entry = Stream.advance(); 48297d523365SDimitry Andric 48307d523365SDimitry Andric switch (Entry.Kind) { 48317d523365SDimitry Andric case BitstreamEntry::Error: 48327d523365SDimitry Andric return error("Malformed block"); 48337d523365SDimitry Andric case BitstreamEntry::EndBlock: 4834d88c1a5aSDimitry Andric return Error::success(); 48357d523365SDimitry Andric 48367d523365SDimitry Andric case BitstreamEntry::SubBlock: 48377d523365SDimitry Andric switch (Entry.ID) { 48387d523365SDimitry Andric default: // Skip unknown content. 48397d523365SDimitry Andric if (Stream.SkipBlock()) 48407d523365SDimitry Andric return error("Invalid record"); 48417d523365SDimitry Andric break; 48427d523365SDimitry Andric case bitc::BLOCKINFO_BLOCK_ID: 48437d523365SDimitry Andric // Need to parse these to get abbrev ids (e.g. for VST) 4844d88c1a5aSDimitry Andric if (readBlockInfo()) 48457d523365SDimitry Andric return error("Malformed block"); 48467d523365SDimitry Andric break; 48477d523365SDimitry Andric case bitc::VALUE_SYMTAB_BLOCK_ID: 48483ca95b02SDimitry Andric // Should have been parsed earlier via VSTOffset, unless there 48493ca95b02SDimitry Andric // is no summary section. 48503ca95b02SDimitry Andric assert(((SeenValueSymbolTable && VSTOffset > 0) || 48513ca95b02SDimitry Andric !SeenGlobalValSummary) && 48523ca95b02SDimitry Andric "Expected early VST parse via VSTOffset record"); 48537d523365SDimitry Andric if (Stream.SkipBlock()) 48547d523365SDimitry Andric return error("Invalid record"); 48553ca95b02SDimitry Andric break; 48563ca95b02SDimitry Andric case bitc::GLOBALVAL_SUMMARY_BLOCK_ID: 485724d58133SDimitry Andric case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID: 48583ca95b02SDimitry Andric assert(!SeenValueSymbolTable && 48593ca95b02SDimitry Andric "Already read VST when parsing summary block?"); 4860d88c1a5aSDimitry Andric // We might not have a VST if there were no values in the 4861d88c1a5aSDimitry Andric // summary. An empty summary block generated when we are 4862d88c1a5aSDimitry Andric // performing ThinLTO compiles so we don't later invoke 4863d88c1a5aSDimitry Andric // the regular LTO process on them. 4864d88c1a5aSDimitry Andric if (VSTOffset > 0) { 4865d88c1a5aSDimitry Andric if (Error Err = parseValueSymbolTable(VSTOffset, ValueIdToLinkageMap)) 4866d88c1a5aSDimitry Andric return Err; 48673ca95b02SDimitry Andric SeenValueSymbolTable = true; 4868d88c1a5aSDimitry Andric } 48693ca95b02SDimitry Andric SeenGlobalValSummary = true; 487024d58133SDimitry Andric if (Error Err = parseEntireSummary(Entry.ID)) 4871d88c1a5aSDimitry Andric return Err; 48727d523365SDimitry Andric break; 48737d523365SDimitry Andric case bitc::MODULE_STRTAB_BLOCK_ID: 4874d88c1a5aSDimitry Andric if (Error Err = parseModuleStringTable()) 4875d88c1a5aSDimitry Andric return Err; 48767d523365SDimitry Andric break; 48777d523365SDimitry Andric } 48787d523365SDimitry Andric continue; 48797d523365SDimitry Andric 48803ca95b02SDimitry Andric case BitstreamEntry::Record: { 48813ca95b02SDimitry Andric Record.clear(); 48823ca95b02SDimitry Andric auto BitCode = Stream.readRecord(Entry.ID, Record); 48833ca95b02SDimitry Andric switch (BitCode) { 48843ca95b02SDimitry Andric default: 48853ca95b02SDimitry Andric break; // Default behavior, ignore unknown content. 48866bc11b14SDimitry Andric case bitc::MODULE_CODE_VERSION: { 48876bc11b14SDimitry Andric if (Error Err = parseVersionRecord(Record).takeError()) 48886bc11b14SDimitry Andric return Err; 48896bc11b14SDimitry Andric break; 48906bc11b14SDimitry Andric } 48913ca95b02SDimitry Andric /// MODULE_CODE_SOURCE_FILENAME: [namechar x N] 48923ca95b02SDimitry Andric case bitc::MODULE_CODE_SOURCE_FILENAME: { 48933ca95b02SDimitry Andric SmallString<128> ValueName; 48943ca95b02SDimitry Andric if (convertToString(Record, 0, ValueName)) 48953ca95b02SDimitry Andric return error("Invalid record"); 48963ca95b02SDimitry Andric SourceFileName = ValueName.c_str(); 48973ca95b02SDimitry Andric break; 48983ca95b02SDimitry Andric } 48993ca95b02SDimitry Andric /// MODULE_CODE_HASH: [5*i32] 49003ca95b02SDimitry Andric case bitc::MODULE_CODE_HASH: { 49013ca95b02SDimitry Andric if (Record.size() != 5) 49023ca95b02SDimitry Andric return error("Invalid hash length " + Twine(Record.size()).str()); 490324d58133SDimitry Andric auto &Hash = addThisModule()->second.second; 49043ca95b02SDimitry Andric int Pos = 0; 49053ca95b02SDimitry Andric for (auto &Val : Record) { 49063ca95b02SDimitry Andric assert(!(Val >> 32) && "Unexpected high bits set"); 49073ca95b02SDimitry Andric Hash[Pos++] = Val; 49083ca95b02SDimitry Andric } 49093ca95b02SDimitry Andric break; 49103ca95b02SDimitry Andric } 49113ca95b02SDimitry Andric /// MODULE_CODE_VSTOFFSET: [offset] 49123ca95b02SDimitry Andric case bitc::MODULE_CODE_VSTOFFSET: 49133ca95b02SDimitry Andric if (Record.size() < 1) 49143ca95b02SDimitry Andric return error("Invalid record"); 4915d88c1a5aSDimitry Andric // Note that we subtract 1 here because the offset is relative to one 4916d88c1a5aSDimitry Andric // word before the start of the identification or module block, which 4917d88c1a5aSDimitry Andric // was historically always the start of the regular bitcode header. 4918d88c1a5aSDimitry Andric VSTOffset = Record[0] - 1; 49193ca95b02SDimitry Andric break; 49206bc11b14SDimitry Andric // v1 GLOBALVAR: [pointer type, isconst, initid, linkage, ...] 49216bc11b14SDimitry Andric // v1 FUNCTION: [type, callingconv, isproto, linkage, ...] 49226bc11b14SDimitry Andric // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, ...] 49236bc11b14SDimitry Andric // v2: [strtab offset, strtab size, v1] 49247a7e6055SDimitry Andric case bitc::MODULE_CODE_GLOBALVAR: 49257a7e6055SDimitry Andric case bitc::MODULE_CODE_FUNCTION: 49263ca95b02SDimitry Andric case bitc::MODULE_CODE_ALIAS: { 49276bc11b14SDimitry Andric StringRef Name; 49286bc11b14SDimitry Andric ArrayRef<uint64_t> GVRecord; 49296bc11b14SDimitry Andric std::tie(Name, GVRecord) = readNameFromStrtab(Record); 49306bc11b14SDimitry Andric if (GVRecord.size() <= 3) 49313ca95b02SDimitry Andric return error("Invalid record"); 49326bc11b14SDimitry Andric uint64_t RawLinkage = GVRecord[3]; 49333ca95b02SDimitry Andric GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage); 49346bc11b14SDimitry Andric if (!UseStrtab) { 49353ca95b02SDimitry Andric ValueIdToLinkageMap[ValueId++] = Linkage; 49363ca95b02SDimitry Andric break; 49373ca95b02SDimitry Andric } 49386bc11b14SDimitry Andric 49396bc11b14SDimitry Andric setValueGUID(ValueId++, Name, Linkage, SourceFileName); 49406bc11b14SDimitry Andric break; 49416bc11b14SDimitry Andric } 49423ca95b02SDimitry Andric } 49433ca95b02SDimitry Andric } 49447d523365SDimitry Andric continue; 49457d523365SDimitry Andric } 49467d523365SDimitry Andric } 49477d523365SDimitry Andric } 49487d523365SDimitry Andric 4949d88c1a5aSDimitry Andric std::vector<ValueInfo> 4950d88c1a5aSDimitry Andric ModuleSummaryIndexBitcodeReader::makeRefList(ArrayRef<uint64_t> Record) { 4951d88c1a5aSDimitry Andric std::vector<ValueInfo> Ret; 4952d88c1a5aSDimitry Andric Ret.reserve(Record.size()); 4953d88c1a5aSDimitry Andric for (uint64_t RefValueId : Record) 49540f5676f4SDimitry Andric Ret.push_back(getValueInfoFromValueId(RefValueId).first); 4955d88c1a5aSDimitry Andric return Ret; 4956d88c1a5aSDimitry Andric } 4957d88c1a5aSDimitry Andric 4958d88c1a5aSDimitry Andric std::vector<FunctionSummary::EdgeTy> ModuleSummaryIndexBitcodeReader::makeCallList( 4959d88c1a5aSDimitry Andric ArrayRef<uint64_t> Record, bool IsOldProfileFormat, bool HasProfile) { 4960d88c1a5aSDimitry Andric std::vector<FunctionSummary::EdgeTy> Ret; 4961d88c1a5aSDimitry Andric Ret.reserve(Record.size()); 4962d88c1a5aSDimitry Andric for (unsigned I = 0, E = Record.size(); I != E; ++I) { 4963d88c1a5aSDimitry Andric CalleeInfo::HotnessType Hotness = CalleeInfo::HotnessType::Unknown; 49640f5676f4SDimitry Andric ValueInfo Callee = getValueInfoFromValueId(Record[I]).first; 4965d88c1a5aSDimitry Andric if (IsOldProfileFormat) { 4966d88c1a5aSDimitry Andric I += 1; // Skip old callsitecount field 4967d88c1a5aSDimitry Andric if (HasProfile) 4968d88c1a5aSDimitry Andric I += 1; // Skip old profilecount field 4969d88c1a5aSDimitry Andric } else if (HasProfile) 4970d88c1a5aSDimitry Andric Hotness = static_cast<CalleeInfo::HotnessType>(Record[++I]); 49710f5676f4SDimitry Andric Ret.push_back(FunctionSummary::EdgeTy{Callee, CalleeInfo{Hotness}}); 4972d88c1a5aSDimitry Andric } 4973d88c1a5aSDimitry Andric return Ret; 4974d88c1a5aSDimitry Andric } 4975d88c1a5aSDimitry Andric 49763ca95b02SDimitry Andric // Eagerly parse the entire summary block. This populates the GlobalValueSummary 49773ca95b02SDimitry Andric // objects in the index. 497824d58133SDimitry Andric Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { 497924d58133SDimitry Andric if (Stream.EnterSubBlock(ID)) 49807d523365SDimitry Andric return error("Invalid record"); 49817d523365SDimitry Andric SmallVector<uint64_t, 64> Record; 49827d523365SDimitry Andric 49833ca95b02SDimitry Andric // Parse version 49843ca95b02SDimitry Andric { 49853ca95b02SDimitry Andric BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 49863ca95b02SDimitry Andric if (Entry.Kind != BitstreamEntry::Record) 49873ca95b02SDimitry Andric return error("Invalid Summary Block: record for version expected"); 49883ca95b02SDimitry Andric if (Stream.readRecord(Entry.ID, Record) != bitc::FS_VERSION) 49893ca95b02SDimitry Andric return error("Invalid Summary Block: version expected"); 49903ca95b02SDimitry Andric } 49913ca95b02SDimitry Andric const uint64_t Version = Record[0]; 4992d88c1a5aSDimitry Andric const bool IsOldProfileFormat = Version == 1; 499395ec533aSDimitry Andric if (Version < 1 || Version > 3) 4994d88c1a5aSDimitry Andric return error("Invalid summary version " + Twine(Version) + 499595ec533aSDimitry Andric ", 1, 2 or 3 expected"); 49963ca95b02SDimitry Andric Record.clear(); 49973ca95b02SDimitry Andric 49983ca95b02SDimitry Andric // Keep around the last seen summary to be used when we see an optional 49993ca95b02SDimitry Andric // "OriginalName" attachement. 50003ca95b02SDimitry Andric GlobalValueSummary *LastSeenSummary = nullptr; 50017a7e6055SDimitry Andric GlobalValue::GUID LastSeenGUID = 0; 50027a7e6055SDimitry Andric 50037a7e6055SDimitry Andric // We can expect to see any number of type ID information records before 50047a7e6055SDimitry Andric // each function summary records; these variables store the information 50057a7e6055SDimitry Andric // collected so far so that it can be used to create the summary object. 5006d88c1a5aSDimitry Andric std::vector<GlobalValue::GUID> PendingTypeTests; 50077a7e6055SDimitry Andric std::vector<FunctionSummary::VFuncId> PendingTypeTestAssumeVCalls, 50087a7e6055SDimitry Andric PendingTypeCheckedLoadVCalls; 50097a7e6055SDimitry Andric std::vector<FunctionSummary::ConstVCall> PendingTypeTestAssumeConstVCalls, 50107a7e6055SDimitry Andric PendingTypeCheckedLoadConstVCalls; 5011d88c1a5aSDimitry Andric 5012d88c1a5aSDimitry Andric while (true) { 50137d523365SDimitry Andric BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 50147d523365SDimitry Andric 50157d523365SDimitry Andric switch (Entry.Kind) { 50167d523365SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 50177d523365SDimitry Andric case BitstreamEntry::Error: 50187d523365SDimitry Andric return error("Malformed block"); 50197d523365SDimitry Andric case BitstreamEntry::EndBlock: 5020d88c1a5aSDimitry Andric return Error::success(); 50217d523365SDimitry Andric case BitstreamEntry::Record: 50227d523365SDimitry Andric // The interesting case. 50237d523365SDimitry Andric break; 50247d523365SDimitry Andric } 50257d523365SDimitry Andric 50267d523365SDimitry Andric // Read a record. The record format depends on whether this 50277d523365SDimitry Andric // is a per-module index or a combined index file. In the per-module 50287d523365SDimitry Andric // case the records contain the associated value's ID for correlation 50297d523365SDimitry Andric // with VST entries. In the combined index the correlation is done 50307d523365SDimitry Andric // via the bitcode offset of the summary records (which were saved 50317d523365SDimitry Andric // in the combined index VST entries). The records also contain 50327d523365SDimitry Andric // information used for ThinLTO renaming and importing. 50337d523365SDimitry Andric Record.clear(); 50343ca95b02SDimitry Andric auto BitCode = Stream.readRecord(Entry.ID, Record); 50353ca95b02SDimitry Andric switch (BitCode) { 50367d523365SDimitry Andric default: // Default behavior: ignore. 50377d523365SDimitry Andric break; 50386bc11b14SDimitry Andric case bitc::FS_VALUE_GUID: { // [valueid, refguid] 50396bc11b14SDimitry Andric uint64_t ValueID = Record[0]; 50406bc11b14SDimitry Andric GlobalValue::GUID RefGUID = Record[1]; 50410f5676f4SDimitry Andric ValueIdToValueInfoMap[ValueID] = 50420f5676f4SDimitry Andric std::make_pair(TheIndex.getOrInsertValueInfo(RefGUID), RefGUID); 50436bc11b14SDimitry Andric break; 50446bc11b14SDimitry Andric } 50453ca95b02SDimitry Andric // FS_PERMODULE: [valueid, flags, instcount, numrefs, numrefs x valueid, 5046d88c1a5aSDimitry Andric // n x (valueid)] 50473ca95b02SDimitry Andric // FS_PERMODULE_PROFILE: [valueid, flags, instcount, numrefs, 50483ca95b02SDimitry Andric // numrefs x valueid, 5049d88c1a5aSDimitry Andric // n x (valueid, hotness)] 50503ca95b02SDimitry Andric case bitc::FS_PERMODULE: 50513ca95b02SDimitry Andric case bitc::FS_PERMODULE_PROFILE: { 50527d523365SDimitry Andric unsigned ValueID = Record[0]; 50533ca95b02SDimitry Andric uint64_t RawFlags = Record[1]; 50547d523365SDimitry Andric unsigned InstCount = Record[2]; 50553ca95b02SDimitry Andric unsigned NumRefs = Record[3]; 50563ca95b02SDimitry Andric auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 50577d523365SDimitry Andric // The module path string ref set in the summary must be owned by the 50587d523365SDimitry Andric // index's module string table. Since we don't have a module path 50597d523365SDimitry Andric // string table section in the per-module index, we create a single 50607d523365SDimitry Andric // module path string table entry with an empty (0) ID to take 50617d523365SDimitry Andric // ownership. 50623ca95b02SDimitry Andric static int RefListStartIndex = 4; 50633ca95b02SDimitry Andric int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs; 50643ca95b02SDimitry Andric assert(Record.size() >= RefListStartIndex + NumRefs && 50653ca95b02SDimitry Andric "Record size inconsistent with number of references"); 5066d88c1a5aSDimitry Andric std::vector<ValueInfo> Refs = makeRefList( 5067d88c1a5aSDimitry Andric ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs)); 50683ca95b02SDimitry Andric bool HasProfile = (BitCode == bitc::FS_PERMODULE_PROFILE); 5069d88c1a5aSDimitry Andric std::vector<FunctionSummary::EdgeTy> Calls = makeCallList( 5070d88c1a5aSDimitry Andric ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex), 5071d88c1a5aSDimitry Andric IsOldProfileFormat, HasProfile); 5072d88c1a5aSDimitry Andric auto FS = llvm::make_unique<FunctionSummary>( 5073d88c1a5aSDimitry Andric Flags, InstCount, std::move(Refs), std::move(Calls), 50747a7e6055SDimitry Andric std::move(PendingTypeTests), std::move(PendingTypeTestAssumeVCalls), 50757a7e6055SDimitry Andric std::move(PendingTypeCheckedLoadVCalls), 50767a7e6055SDimitry Andric std::move(PendingTypeTestAssumeConstVCalls), 50777a7e6055SDimitry Andric std::move(PendingTypeCheckedLoadConstVCalls)); 5078d88c1a5aSDimitry Andric PendingTypeTests.clear(); 50797a7e6055SDimitry Andric PendingTypeTestAssumeVCalls.clear(); 50807a7e6055SDimitry Andric PendingTypeCheckedLoadVCalls.clear(); 50817a7e6055SDimitry Andric PendingTypeTestAssumeConstVCalls.clear(); 50827a7e6055SDimitry Andric PendingTypeCheckedLoadConstVCalls.clear(); 50830f5676f4SDimitry Andric auto VIAndOriginalGUID = getValueInfoFromValueId(ValueID); 508424d58133SDimitry Andric FS->setModulePath(addThisModule()->first()); 50850f5676f4SDimitry Andric FS->setOriginalName(VIAndOriginalGUID.second); 50860f5676f4SDimitry Andric TheIndex.addGlobalValueSummary(VIAndOriginalGUID.first, std::move(FS)); 50873ca95b02SDimitry Andric break; 50883ca95b02SDimitry Andric } 50893ca95b02SDimitry Andric // FS_ALIAS: [valueid, flags, valueid] 50903ca95b02SDimitry Andric // Aliases must be emitted (and parsed) after all FS_PERMODULE entries, as 50913ca95b02SDimitry Andric // they expect all aliasee summaries to be available. 50923ca95b02SDimitry Andric case bitc::FS_ALIAS: { 50933ca95b02SDimitry Andric unsigned ValueID = Record[0]; 50943ca95b02SDimitry Andric uint64_t RawFlags = Record[1]; 50953ca95b02SDimitry Andric unsigned AliaseeID = Record[2]; 50963ca95b02SDimitry Andric auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 5097d88c1a5aSDimitry Andric auto AS = 5098d88c1a5aSDimitry Andric llvm::make_unique<AliasSummary>(Flags, std::vector<ValueInfo>{}); 50993ca95b02SDimitry Andric // The module path string ref set in the summary must be owned by the 51003ca95b02SDimitry Andric // index's module string table. Since we don't have a module path 51013ca95b02SDimitry Andric // string table section in the per-module index, we create a single 51023ca95b02SDimitry Andric // module path string table entry with an empty (0) ID to take 51033ca95b02SDimitry Andric // ownership. 510424d58133SDimitry Andric AS->setModulePath(addThisModule()->first()); 51053ca95b02SDimitry Andric 51060f5676f4SDimitry Andric GlobalValue::GUID AliaseeGUID = 51070f5676f4SDimitry Andric getValueInfoFromValueId(AliaseeID).first.getGUID(); 5108f37b6182SDimitry Andric auto AliaseeInModule = 5109f37b6182SDimitry Andric TheIndex.findSummaryInModule(AliaseeGUID, ModulePath); 5110f37b6182SDimitry Andric if (!AliaseeInModule) 51113ca95b02SDimitry Andric return error("Alias expects aliasee summary to be parsed"); 5112f37b6182SDimitry Andric AS->setAliasee(AliaseeInModule); 51133ca95b02SDimitry Andric 51140f5676f4SDimitry Andric auto GUID = getValueInfoFromValueId(ValueID); 51153ca95b02SDimitry Andric AS->setOriginalName(GUID.second); 5116d88c1a5aSDimitry Andric TheIndex.addGlobalValueSummary(GUID.first, std::move(AS)); 51173ca95b02SDimitry Andric break; 51183ca95b02SDimitry Andric } 51193ca95b02SDimitry Andric // FS_PERMODULE_GLOBALVAR_INIT_REFS: [valueid, flags, n x valueid] 51203ca95b02SDimitry Andric case bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS: { 51213ca95b02SDimitry Andric unsigned ValueID = Record[0]; 51223ca95b02SDimitry Andric uint64_t RawFlags = Record[1]; 51233ca95b02SDimitry Andric auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 5124d88c1a5aSDimitry Andric std::vector<ValueInfo> Refs = 5125d88c1a5aSDimitry Andric makeRefList(ArrayRef<uint64_t>(Record).slice(2)); 5126d88c1a5aSDimitry Andric auto FS = llvm::make_unique<GlobalVarSummary>(Flags, std::move(Refs)); 512724d58133SDimitry Andric FS->setModulePath(addThisModule()->first()); 51280f5676f4SDimitry Andric auto GUID = getValueInfoFromValueId(ValueID); 51293ca95b02SDimitry Andric FS->setOriginalName(GUID.second); 5130d88c1a5aSDimitry Andric TheIndex.addGlobalValueSummary(GUID.first, std::move(FS)); 51313ca95b02SDimitry Andric break; 51323ca95b02SDimitry Andric } 51333ca95b02SDimitry Andric // FS_COMBINED: [valueid, modid, flags, instcount, numrefs, 5134d88c1a5aSDimitry Andric // numrefs x valueid, n x (valueid)] 51353ca95b02SDimitry Andric // FS_COMBINED_PROFILE: [valueid, modid, flags, instcount, numrefs, 5136d88c1a5aSDimitry Andric // numrefs x valueid, n x (valueid, hotness)] 51373ca95b02SDimitry Andric case bitc::FS_COMBINED: 51383ca95b02SDimitry Andric case bitc::FS_COMBINED_PROFILE: { 51393ca95b02SDimitry Andric unsigned ValueID = Record[0]; 51403ca95b02SDimitry Andric uint64_t ModuleId = Record[1]; 51413ca95b02SDimitry Andric uint64_t RawFlags = Record[2]; 51423ca95b02SDimitry Andric unsigned InstCount = Record[3]; 51433ca95b02SDimitry Andric unsigned NumRefs = Record[4]; 51443ca95b02SDimitry Andric auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 51453ca95b02SDimitry Andric static int RefListStartIndex = 5; 51463ca95b02SDimitry Andric int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs; 51473ca95b02SDimitry Andric assert(Record.size() >= RefListStartIndex + NumRefs && 51483ca95b02SDimitry Andric "Record size inconsistent with number of references"); 5149d88c1a5aSDimitry Andric std::vector<ValueInfo> Refs = makeRefList( 5150d88c1a5aSDimitry Andric ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs)); 51513ca95b02SDimitry Andric bool HasProfile = (BitCode == bitc::FS_COMBINED_PROFILE); 5152d88c1a5aSDimitry Andric std::vector<FunctionSummary::EdgeTy> Edges = makeCallList( 5153d88c1a5aSDimitry Andric ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex), 5154d88c1a5aSDimitry Andric IsOldProfileFormat, HasProfile); 51550f5676f4SDimitry Andric ValueInfo VI = getValueInfoFromValueId(ValueID).first; 5156d88c1a5aSDimitry Andric auto FS = llvm::make_unique<FunctionSummary>( 5157d88c1a5aSDimitry Andric Flags, InstCount, std::move(Refs), std::move(Edges), 51587a7e6055SDimitry Andric std::move(PendingTypeTests), std::move(PendingTypeTestAssumeVCalls), 51597a7e6055SDimitry Andric std::move(PendingTypeCheckedLoadVCalls), 51607a7e6055SDimitry Andric std::move(PendingTypeTestAssumeConstVCalls), 51617a7e6055SDimitry Andric std::move(PendingTypeCheckedLoadConstVCalls)); 5162d88c1a5aSDimitry Andric PendingTypeTests.clear(); 51637a7e6055SDimitry Andric PendingTypeTestAssumeVCalls.clear(); 51647a7e6055SDimitry Andric PendingTypeCheckedLoadVCalls.clear(); 51657a7e6055SDimitry Andric PendingTypeTestAssumeConstVCalls.clear(); 51667a7e6055SDimitry Andric PendingTypeCheckedLoadConstVCalls.clear(); 5167d88c1a5aSDimitry Andric LastSeenSummary = FS.get(); 51680f5676f4SDimitry Andric LastSeenGUID = VI.getGUID(); 5169d88c1a5aSDimitry Andric FS->setModulePath(ModuleIdMap[ModuleId]); 51700f5676f4SDimitry Andric TheIndex.addGlobalValueSummary(VI, std::move(FS)); 51713ca95b02SDimitry Andric break; 51723ca95b02SDimitry Andric } 51733ca95b02SDimitry Andric // FS_COMBINED_ALIAS: [valueid, modid, flags, valueid] 51743ca95b02SDimitry Andric // Aliases must be emitted (and parsed) after all FS_COMBINED entries, as 51753ca95b02SDimitry Andric // they expect all aliasee summaries to be available. 51763ca95b02SDimitry Andric case bitc::FS_COMBINED_ALIAS: { 51773ca95b02SDimitry Andric unsigned ValueID = Record[0]; 51783ca95b02SDimitry Andric uint64_t ModuleId = Record[1]; 51793ca95b02SDimitry Andric uint64_t RawFlags = Record[2]; 51803ca95b02SDimitry Andric unsigned AliaseeValueId = Record[3]; 51813ca95b02SDimitry Andric auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 5182d88c1a5aSDimitry Andric auto AS = llvm::make_unique<AliasSummary>(Flags, std::vector<ValueInfo>{}); 51833ca95b02SDimitry Andric LastSeenSummary = AS.get(); 51843ca95b02SDimitry Andric AS->setModulePath(ModuleIdMap[ModuleId]); 51853ca95b02SDimitry Andric 51860f5676f4SDimitry Andric auto AliaseeGUID = 51870f5676f4SDimitry Andric getValueInfoFromValueId(AliaseeValueId).first.getGUID(); 51883ca95b02SDimitry Andric auto AliaseeInModule = 5189d88c1a5aSDimitry Andric TheIndex.findSummaryInModule(AliaseeGUID, AS->modulePath()); 51903ca95b02SDimitry Andric if (!AliaseeInModule) 51913ca95b02SDimitry Andric return error("Alias expects aliasee summary to be parsed"); 51923ca95b02SDimitry Andric AS->setAliasee(AliaseeInModule); 51933ca95b02SDimitry Andric 51940f5676f4SDimitry Andric ValueInfo VI = getValueInfoFromValueId(ValueID).first; 51950f5676f4SDimitry Andric LastSeenGUID = VI.getGUID(); 51960f5676f4SDimitry Andric TheIndex.addGlobalValueSummary(VI, std::move(AS)); 51973ca95b02SDimitry Andric break; 51983ca95b02SDimitry Andric } 51993ca95b02SDimitry Andric // FS_COMBINED_GLOBALVAR_INIT_REFS: [valueid, modid, flags, n x valueid] 52003ca95b02SDimitry Andric case bitc::FS_COMBINED_GLOBALVAR_INIT_REFS: { 52013ca95b02SDimitry Andric unsigned ValueID = Record[0]; 52023ca95b02SDimitry Andric uint64_t ModuleId = Record[1]; 52033ca95b02SDimitry Andric uint64_t RawFlags = Record[2]; 52043ca95b02SDimitry Andric auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 5205d88c1a5aSDimitry Andric std::vector<ValueInfo> Refs = 5206d88c1a5aSDimitry Andric makeRefList(ArrayRef<uint64_t>(Record).slice(3)); 5207d88c1a5aSDimitry Andric auto FS = llvm::make_unique<GlobalVarSummary>(Flags, std::move(Refs)); 52083ca95b02SDimitry Andric LastSeenSummary = FS.get(); 52093ca95b02SDimitry Andric FS->setModulePath(ModuleIdMap[ModuleId]); 52100f5676f4SDimitry Andric ValueInfo VI = getValueInfoFromValueId(ValueID).first; 52110f5676f4SDimitry Andric LastSeenGUID = VI.getGUID(); 52120f5676f4SDimitry Andric TheIndex.addGlobalValueSummary(VI, std::move(FS)); 52133ca95b02SDimitry Andric break; 52143ca95b02SDimitry Andric } 52153ca95b02SDimitry Andric // FS_COMBINED_ORIGINAL_NAME: [original_name] 52163ca95b02SDimitry Andric case bitc::FS_COMBINED_ORIGINAL_NAME: { 52173ca95b02SDimitry Andric uint64_t OriginalName = Record[0]; 52183ca95b02SDimitry Andric if (!LastSeenSummary) 52193ca95b02SDimitry Andric return error("Name attachment that does not follow a combined record"); 52203ca95b02SDimitry Andric LastSeenSummary->setOriginalName(OriginalName); 52217a7e6055SDimitry Andric TheIndex.addOriginalName(LastSeenGUID, OriginalName); 52223ca95b02SDimitry Andric // Reset the LastSeenSummary 52233ca95b02SDimitry Andric LastSeenSummary = nullptr; 52247a7e6055SDimitry Andric LastSeenGUID = 0; 5225d88c1a5aSDimitry Andric break; 5226d88c1a5aSDimitry Andric } 5227d88c1a5aSDimitry Andric case bitc::FS_TYPE_TESTS: { 5228d88c1a5aSDimitry Andric assert(PendingTypeTests.empty()); 5229d88c1a5aSDimitry Andric PendingTypeTests.insert(PendingTypeTests.end(), Record.begin(), 5230d88c1a5aSDimitry Andric Record.end()); 5231d88c1a5aSDimitry Andric break; 52327d523365SDimitry Andric } 52337a7e6055SDimitry Andric case bitc::FS_TYPE_TEST_ASSUME_VCALLS: { 52347a7e6055SDimitry Andric assert(PendingTypeTestAssumeVCalls.empty()); 52357a7e6055SDimitry Andric for (unsigned I = 0; I != Record.size(); I += 2) 52367a7e6055SDimitry Andric PendingTypeTestAssumeVCalls.push_back({Record[I], Record[I+1]}); 52377a7e6055SDimitry Andric break; 52387a7e6055SDimitry Andric } 52397a7e6055SDimitry Andric case bitc::FS_TYPE_CHECKED_LOAD_VCALLS: { 52407a7e6055SDimitry Andric assert(PendingTypeCheckedLoadVCalls.empty()); 52417a7e6055SDimitry Andric for (unsigned I = 0; I != Record.size(); I += 2) 52427a7e6055SDimitry Andric PendingTypeCheckedLoadVCalls.push_back({Record[I], Record[I+1]}); 52437a7e6055SDimitry Andric break; 52447a7e6055SDimitry Andric } 52457a7e6055SDimitry Andric case bitc::FS_TYPE_TEST_ASSUME_CONST_VCALL: { 52467a7e6055SDimitry Andric PendingTypeTestAssumeConstVCalls.push_back( 52477a7e6055SDimitry Andric {{Record[0], Record[1]}, {Record.begin() + 2, Record.end()}}); 52487a7e6055SDimitry Andric break; 52497a7e6055SDimitry Andric } 52507a7e6055SDimitry Andric case bitc::FS_TYPE_CHECKED_LOAD_CONST_VCALL: { 52517a7e6055SDimitry Andric PendingTypeCheckedLoadConstVCalls.push_back( 52527a7e6055SDimitry Andric {{Record[0], Record[1]}, {Record.begin() + 2, Record.end()}}); 52537a7e6055SDimitry Andric break; 52547a7e6055SDimitry Andric } 525524d58133SDimitry Andric case bitc::FS_CFI_FUNCTION_DEFS: { 525624d58133SDimitry Andric std::set<std::string> &CfiFunctionDefs = TheIndex.cfiFunctionDefs(); 525724d58133SDimitry Andric for (unsigned I = 0; I != Record.size(); I += 2) 525824d58133SDimitry Andric CfiFunctionDefs.insert( 525924d58133SDimitry Andric {Strtab.data() + Record[I], static_cast<size_t>(Record[I + 1])}); 526024d58133SDimitry Andric break; 526124d58133SDimitry Andric } 526224d58133SDimitry Andric case bitc::FS_CFI_FUNCTION_DECLS: { 526324d58133SDimitry Andric std::set<std::string> &CfiFunctionDecls = TheIndex.cfiFunctionDecls(); 526424d58133SDimitry Andric for (unsigned I = 0; I != Record.size(); I += 2) 526524d58133SDimitry Andric CfiFunctionDecls.insert( 526624d58133SDimitry Andric {Strtab.data() + Record[I], static_cast<size_t>(Record[I + 1])}); 526724d58133SDimitry Andric break; 526824d58133SDimitry Andric } 52697d523365SDimitry Andric } 52707d523365SDimitry Andric } 52717d523365SDimitry Andric llvm_unreachable("Exit infinite loop"); 52727d523365SDimitry Andric } 52737d523365SDimitry Andric 52747d523365SDimitry Andric // Parse the module string table block into the Index. 52757d523365SDimitry Andric // This populates the ModulePathStringTable map in the index. 5276d88c1a5aSDimitry Andric Error ModuleSummaryIndexBitcodeReader::parseModuleStringTable() { 52777d523365SDimitry Andric if (Stream.EnterSubBlock(bitc::MODULE_STRTAB_BLOCK_ID)) 52787d523365SDimitry Andric return error("Invalid record"); 52797d523365SDimitry Andric 52807d523365SDimitry Andric SmallVector<uint64_t, 64> Record; 52817d523365SDimitry Andric 52827d523365SDimitry Andric SmallString<128> ModulePath; 528324d58133SDimitry Andric ModuleSummaryIndex::ModuleInfo *LastSeenModule = nullptr; 5284d88c1a5aSDimitry Andric 5285d88c1a5aSDimitry Andric while (true) { 52867d523365SDimitry Andric BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 52877d523365SDimitry Andric 52887d523365SDimitry Andric switch (Entry.Kind) { 52897d523365SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 52907d523365SDimitry Andric case BitstreamEntry::Error: 52917d523365SDimitry Andric return error("Malformed block"); 52927d523365SDimitry Andric case BitstreamEntry::EndBlock: 5293d88c1a5aSDimitry Andric return Error::success(); 52947d523365SDimitry Andric case BitstreamEntry::Record: 52957d523365SDimitry Andric // The interesting case. 52967d523365SDimitry Andric break; 52977d523365SDimitry Andric } 52987d523365SDimitry Andric 52997d523365SDimitry Andric Record.clear(); 53007d523365SDimitry Andric switch (Stream.readRecord(Entry.ID, Record)) { 53017d523365SDimitry Andric default: // Default behavior: ignore. 53027d523365SDimitry Andric break; 53037d523365SDimitry Andric case bitc::MST_CODE_ENTRY: { 53047d523365SDimitry Andric // MST_ENTRY: [modid, namechar x N] 53053ca95b02SDimitry Andric uint64_t ModuleId = Record[0]; 53063ca95b02SDimitry Andric 53077d523365SDimitry Andric if (convertToString(Record, 1, ModulePath)) 53087d523365SDimitry Andric return error("Invalid record"); 53093ca95b02SDimitry Andric 531024d58133SDimitry Andric LastSeenModule = TheIndex.addModule(ModulePath, ModuleId); 531124d58133SDimitry Andric ModuleIdMap[ModuleId] = LastSeenModule->first(); 53123ca95b02SDimitry Andric 53137d523365SDimitry Andric ModulePath.clear(); 53147d523365SDimitry Andric break; 53157d523365SDimitry Andric } 53163ca95b02SDimitry Andric /// MST_CODE_HASH: [5*i32] 53173ca95b02SDimitry Andric case bitc::MST_CODE_HASH: { 53183ca95b02SDimitry Andric if (Record.size() != 5) 53193ca95b02SDimitry Andric return error("Invalid hash length " + Twine(Record.size()).str()); 532024d58133SDimitry Andric if (!LastSeenModule) 53213ca95b02SDimitry Andric return error("Invalid hash that does not follow a module path"); 53223ca95b02SDimitry Andric int Pos = 0; 53233ca95b02SDimitry Andric for (auto &Val : Record) { 53243ca95b02SDimitry Andric assert(!(Val >> 32) && "Unexpected high bits set"); 532524d58133SDimitry Andric LastSeenModule->second.second[Pos++] = Val; 53263ca95b02SDimitry Andric } 532724d58133SDimitry Andric // Reset LastSeenModule to avoid overriding the hash unexpectedly. 532824d58133SDimitry Andric LastSeenModule = nullptr; 53293ca95b02SDimitry Andric break; 53303ca95b02SDimitry Andric } 53317d523365SDimitry Andric } 53327d523365SDimitry Andric } 53337d523365SDimitry Andric llvm_unreachable("Exit infinite loop"); 53347d523365SDimitry Andric } 53357d523365SDimitry Andric 5336f785676fSDimitry Andric namespace { 5337d88c1a5aSDimitry Andric 53383ca95b02SDimitry Andric // FIXME: This class is only here to support the transition to llvm::Error. It 53393ca95b02SDimitry Andric // will be removed once this transition is complete. Clients should prefer to 53403ca95b02SDimitry Andric // deal with the Error value directly, rather than converting to error_code. 534191bc56edSDimitry Andric class BitcodeErrorCategoryType : public std::error_category { 5342d88c1a5aSDimitry Andric const char *name() const noexcept override { 5343f785676fSDimitry Andric return "llvm.bitcode"; 5344f785676fSDimitry Andric } 534591bc56edSDimitry Andric std::string message(int IE) const override { 534639d628a0SDimitry Andric BitcodeError E = static_cast<BitcodeError>(IE); 5347f785676fSDimitry Andric switch (E) { 534839d628a0SDimitry Andric case BitcodeError::CorruptedBitcode: 534939d628a0SDimitry Andric return "Corrupted bitcode"; 5350f785676fSDimitry Andric } 5351f785676fSDimitry Andric llvm_unreachable("Unknown error type!"); 5352f785676fSDimitry Andric } 5353f785676fSDimitry Andric }; 5354d88c1a5aSDimitry Andric 53553ca95b02SDimitry Andric } // end anonymous namespace 5356f785676fSDimitry Andric 535739d628a0SDimitry Andric static ManagedStatic<BitcodeErrorCategoryType> ErrorCategory; 535839d628a0SDimitry Andric 535939d628a0SDimitry Andric const std::error_category &llvm::BitcodeErrorCategory() { 536039d628a0SDimitry Andric return *ErrorCategory; 5361dff0c46cSDimitry Andric } 5362f22ef01cSRoman Divacky 5363a580b014SDimitry Andric static Expected<StringRef> readBlobInRecord(BitstreamCursor &Stream, 5364a580b014SDimitry Andric unsigned Block, unsigned RecordID) { 5365a580b014SDimitry Andric if (Stream.EnterSubBlock(Block)) 53666bc11b14SDimitry Andric return error("Invalid record"); 53676bc11b14SDimitry Andric 53686bc11b14SDimitry Andric StringRef Strtab; 53696bc11b14SDimitry Andric while (1) { 53706bc11b14SDimitry Andric BitstreamEntry Entry = Stream.advance(); 53716bc11b14SDimitry Andric switch (Entry.Kind) { 53726bc11b14SDimitry Andric case BitstreamEntry::EndBlock: 53736bc11b14SDimitry Andric return Strtab; 53746bc11b14SDimitry Andric 53756bc11b14SDimitry Andric case BitstreamEntry::Error: 53766bc11b14SDimitry Andric return error("Malformed block"); 53776bc11b14SDimitry Andric 53786bc11b14SDimitry Andric case BitstreamEntry::SubBlock: 53796bc11b14SDimitry Andric if (Stream.SkipBlock()) 53806bc11b14SDimitry Andric return error("Malformed block"); 53816bc11b14SDimitry Andric break; 53826bc11b14SDimitry Andric 53836bc11b14SDimitry Andric case BitstreamEntry::Record: 53846bc11b14SDimitry Andric StringRef Blob; 53856bc11b14SDimitry Andric SmallVector<uint64_t, 1> Record; 5386a580b014SDimitry Andric if (Stream.readRecord(Entry.ID, Record, &Blob) == RecordID) 53876bc11b14SDimitry Andric Strtab = Blob; 53886bc11b14SDimitry Andric break; 53896bc11b14SDimitry Andric } 53906bc11b14SDimitry Andric } 53916bc11b14SDimitry Andric } 53926bc11b14SDimitry Andric 5393f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 5394f22ef01cSRoman Divacky // External interface 5395f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 5396f22ef01cSRoman Divacky 5397d88c1a5aSDimitry Andric Expected<std::vector<BitcodeModule>> 5398d88c1a5aSDimitry Andric llvm::getBitcodeModuleList(MemoryBufferRef Buffer) { 5399db17bf38SDimitry Andric auto FOrErr = getBitcodeFileContents(Buffer); 5400db17bf38SDimitry Andric if (!FOrErr) 5401db17bf38SDimitry Andric return FOrErr.takeError(); 5402db17bf38SDimitry Andric return std::move(FOrErr->Mods); 5403db17bf38SDimitry Andric } 5404db17bf38SDimitry Andric 5405db17bf38SDimitry Andric Expected<BitcodeFileContents> 5406db17bf38SDimitry Andric llvm::getBitcodeFileContents(MemoryBufferRef Buffer) { 5407d88c1a5aSDimitry Andric Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); 5408d88c1a5aSDimitry Andric if (!StreamOrErr) 5409d88c1a5aSDimitry Andric return StreamOrErr.takeError(); 5410d88c1a5aSDimitry Andric BitstreamCursor &Stream = *StreamOrErr; 54118f0fd8f6SDimitry Andric 5412db17bf38SDimitry Andric BitcodeFileContents F; 5413d88c1a5aSDimitry Andric while (true) { 5414d88c1a5aSDimitry Andric uint64_t BCBegin = Stream.getCurrentByteNo(); 54158f0fd8f6SDimitry Andric 5416d88c1a5aSDimitry Andric // We may be consuming bitcode from a client that leaves garbage at the end 5417d88c1a5aSDimitry Andric // of the bitcode stream (e.g. Apple's ar tool). If we are close enough to 5418d88c1a5aSDimitry Andric // the end that there cannot possibly be another module, stop looking. 5419d88c1a5aSDimitry Andric if (BCBegin + 8 >= Stream.getBitcodeBytes().size()) 5420db17bf38SDimitry Andric return F; 54218f0fd8f6SDimitry Andric 5422d88c1a5aSDimitry Andric BitstreamEntry Entry = Stream.advance(); 5423d88c1a5aSDimitry Andric switch (Entry.Kind) { 5424d88c1a5aSDimitry Andric case BitstreamEntry::EndBlock: 5425d88c1a5aSDimitry Andric case BitstreamEntry::Error: 5426d88c1a5aSDimitry Andric return error("Malformed block"); 5427d88c1a5aSDimitry Andric 5428d88c1a5aSDimitry Andric case BitstreamEntry::SubBlock: { 5429d88c1a5aSDimitry Andric uint64_t IdentificationBit = -1ull; 5430d88c1a5aSDimitry Andric if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) { 5431d88c1a5aSDimitry Andric IdentificationBit = Stream.GetCurrentBitNo() - BCBegin * 8; 5432d88c1a5aSDimitry Andric if (Stream.SkipBlock()) 5433d88c1a5aSDimitry Andric return error("Malformed block"); 5434d88c1a5aSDimitry Andric 5435d88c1a5aSDimitry Andric Entry = Stream.advance(); 5436d88c1a5aSDimitry Andric if (Entry.Kind != BitstreamEntry::SubBlock || 5437d88c1a5aSDimitry Andric Entry.ID != bitc::MODULE_BLOCK_ID) 5438d88c1a5aSDimitry Andric return error("Malformed block"); 54398f0fd8f6SDimitry Andric } 5440d88c1a5aSDimitry Andric 5441d88c1a5aSDimitry Andric if (Entry.ID == bitc::MODULE_BLOCK_ID) { 5442d88c1a5aSDimitry Andric uint64_t ModuleBit = Stream.GetCurrentBitNo() - BCBegin * 8; 5443d88c1a5aSDimitry Andric if (Stream.SkipBlock()) 5444d88c1a5aSDimitry Andric return error("Malformed block"); 5445d88c1a5aSDimitry Andric 5446db17bf38SDimitry Andric F.Mods.push_back({Stream.getBitcodeBytes().slice( 5447d88c1a5aSDimitry Andric BCBegin, Stream.getCurrentByteNo() - BCBegin), 5448d88c1a5aSDimitry Andric Buffer.getBufferIdentifier(), IdentificationBit, 5449d88c1a5aSDimitry Andric ModuleBit}); 5450d88c1a5aSDimitry Andric continue; 5451d88c1a5aSDimitry Andric } 5452d88c1a5aSDimitry Andric 54536bc11b14SDimitry Andric if (Entry.ID == bitc::STRTAB_BLOCK_ID) { 5454a580b014SDimitry Andric Expected<StringRef> Strtab = 5455a580b014SDimitry Andric readBlobInRecord(Stream, bitc::STRTAB_BLOCK_ID, bitc::STRTAB_BLOB); 54566bc11b14SDimitry Andric if (!Strtab) 54576bc11b14SDimitry Andric return Strtab.takeError(); 54586bc11b14SDimitry Andric // This string table is used by every preceding bitcode module that does 54596bc11b14SDimitry Andric // not have its own string table. A bitcode file may have multiple 54606bc11b14SDimitry Andric // string tables if it was created by binary concatenation, for example 54616bc11b14SDimitry Andric // with "llvm-cat -b". 5462db17bf38SDimitry Andric for (auto I = F.Mods.rbegin(), E = F.Mods.rend(); I != E; ++I) { 54636bc11b14SDimitry Andric if (!I->Strtab.empty()) 54646bc11b14SDimitry Andric break; 54656bc11b14SDimitry Andric I->Strtab = *Strtab; 54666bc11b14SDimitry Andric } 5467a580b014SDimitry Andric // Similarly, the string table is used by every preceding symbol table; 5468a580b014SDimitry Andric // normally there will be just one unless the bitcode file was created 5469a580b014SDimitry Andric // by binary concatenation. 5470a580b014SDimitry Andric if (!F.Symtab.empty() && F.StrtabForSymtab.empty()) 5471a580b014SDimitry Andric F.StrtabForSymtab = *Strtab; 5472a580b014SDimitry Andric continue; 5473a580b014SDimitry Andric } 5474a580b014SDimitry Andric 5475a580b014SDimitry Andric if (Entry.ID == bitc::SYMTAB_BLOCK_ID) { 5476a580b014SDimitry Andric Expected<StringRef> SymtabOrErr = 5477a580b014SDimitry Andric readBlobInRecord(Stream, bitc::SYMTAB_BLOCK_ID, bitc::SYMTAB_BLOB); 5478a580b014SDimitry Andric if (!SymtabOrErr) 5479a580b014SDimitry Andric return SymtabOrErr.takeError(); 5480a580b014SDimitry Andric 5481a580b014SDimitry Andric // We can expect the bitcode file to have multiple symbol tables if it 5482a580b014SDimitry Andric // was created by binary concatenation. In that case we silently 5483a580b014SDimitry Andric // ignore any subsequent symbol tables, which is fine because this is a 5484a580b014SDimitry Andric // low level function. The client is expected to notice that the number 5485a580b014SDimitry Andric // of modules in the symbol table does not match the number of modules 5486a580b014SDimitry Andric // in the input file and regenerate the symbol table. 5487a580b014SDimitry Andric if (F.Symtab.empty()) 5488a580b014SDimitry Andric F.Symtab = *SymtabOrErr; 54896bc11b14SDimitry Andric continue; 54906bc11b14SDimitry Andric } 54916bc11b14SDimitry Andric 5492d88c1a5aSDimitry Andric if (Stream.SkipBlock()) 5493d88c1a5aSDimitry Andric return error("Malformed block"); 5494d88c1a5aSDimitry Andric continue; 5495d88c1a5aSDimitry Andric } 5496d88c1a5aSDimitry Andric case BitstreamEntry::Record: 5497d88c1a5aSDimitry Andric Stream.skipRecord(Entry.ID); 5498d88c1a5aSDimitry Andric continue; 5499d88c1a5aSDimitry Andric } 5500d88c1a5aSDimitry Andric } 55018f0fd8f6SDimitry Andric } 55028f0fd8f6SDimitry Andric 550339d628a0SDimitry Andric /// \brief Get a lazy one-at-time loading module from bitcode. 5504f22ef01cSRoman Divacky /// 550539d628a0SDimitry Andric /// This isn't always used in a lazy context. In particular, it's also used by 5506d88c1a5aSDimitry Andric /// \a parseModule(). If this is truly lazy, then we need to eagerly pull 550739d628a0SDimitry Andric /// in forward-referenced functions from block address references. 550839d628a0SDimitry Andric /// 55098f0fd8f6SDimitry Andric /// \param[in] MaterializeAll Set to \c true if we should materialize 55108f0fd8f6SDimitry Andric /// everything. 5511d88c1a5aSDimitry Andric Expected<std::unique_ptr<Module>> 5512d88c1a5aSDimitry Andric BitcodeModule::getModuleImpl(LLVMContext &Context, bool MaterializeAll, 5513d88c1a5aSDimitry Andric bool ShouldLazyLoadMetadata, bool IsImporting) { 5514d88c1a5aSDimitry Andric BitstreamCursor Stream(Buffer); 551539d628a0SDimitry Andric 5516d88c1a5aSDimitry Andric std::string ProducerIdentification; 5517d88c1a5aSDimitry Andric if (IdentificationBit != -1ull) { 5518d88c1a5aSDimitry Andric Stream.JumpToBit(IdentificationBit); 5519d88c1a5aSDimitry Andric Expected<std::string> ProducerIdentificationOrErr = 5520d88c1a5aSDimitry Andric readIdentificationBlock(Stream); 5521d88c1a5aSDimitry Andric if (!ProducerIdentificationOrErr) 5522d88c1a5aSDimitry Andric return ProducerIdentificationOrErr.takeError(); 552339d628a0SDimitry Andric 5524d88c1a5aSDimitry Andric ProducerIdentification = *ProducerIdentificationOrErr; 5525dff0c46cSDimitry Andric } 5526dff0c46cSDimitry Andric 5527d88c1a5aSDimitry Andric Stream.JumpToBit(ModuleBit); 55286bc11b14SDimitry Andric auto *R = new BitcodeReader(std::move(Stream), Strtab, ProducerIdentification, 55296bc11b14SDimitry Andric Context); 5530d88c1a5aSDimitry Andric 5531d88c1a5aSDimitry Andric std::unique_ptr<Module> M = 5532d88c1a5aSDimitry Andric llvm::make_unique<Module>(ModuleIdentifier, Context); 5533d88c1a5aSDimitry Andric M->setMaterializer(R); 5534d88c1a5aSDimitry Andric 5535d88c1a5aSDimitry Andric // Delay parsing Metadata if ShouldLazyLoadMetadata is true. 5536d88c1a5aSDimitry Andric if (Error Err = 5537d88c1a5aSDimitry Andric R->parseBitcodeInto(M.get(), ShouldLazyLoadMetadata, IsImporting)) 5538d88c1a5aSDimitry Andric return std::move(Err); 5539d88c1a5aSDimitry Andric 5540d88c1a5aSDimitry Andric if (MaterializeAll) { 5541d88c1a5aSDimitry Andric // Read in the entire module, and destroy the BitcodeReader. 5542d88c1a5aSDimitry Andric if (Error Err = M->materializeAll()) 5543d88c1a5aSDimitry Andric return std::move(Err); 5544d88c1a5aSDimitry Andric } else { 5545d88c1a5aSDimitry Andric // Resolve forward references from blockaddresses. 5546d88c1a5aSDimitry Andric if (Error Err = R->materializeForwardReferencedFunctions()) 5547d88c1a5aSDimitry Andric return std::move(Err); 5548d88c1a5aSDimitry Andric } 5549d88c1a5aSDimitry Andric return std::move(M); 5550f22ef01cSRoman Divacky } 5551f22ef01cSRoman Divacky 5552d88c1a5aSDimitry Andric Expected<std::unique_ptr<Module>> 5553d88c1a5aSDimitry Andric BitcodeModule::getLazyModule(LLVMContext &Context, bool ShouldLazyLoadMetadata, 5554d88c1a5aSDimitry Andric bool IsImporting) { 5555d88c1a5aSDimitry Andric return getModuleImpl(Context, false, ShouldLazyLoadMetadata, IsImporting); 55567d523365SDimitry Andric } 55577d523365SDimitry Andric 5558f37b6182SDimitry Andric // Parse the specified bitcode buffer and merge the index into CombinedIndex. 555924d58133SDimitry Andric // We don't use ModuleIdentifier here because the client may need to control the 556024d58133SDimitry Andric // module path used in the combined summary (e.g. when reading summaries for 556124d58133SDimitry Andric // regular LTO modules). 5562f37b6182SDimitry Andric Error BitcodeModule::readSummary(ModuleSummaryIndex &CombinedIndex, 556324d58133SDimitry Andric StringRef ModulePath, uint64_t ModuleId) { 5564f37b6182SDimitry Andric BitstreamCursor Stream(Buffer); 5565f37b6182SDimitry Andric Stream.JumpToBit(ModuleBit); 5566f37b6182SDimitry Andric 5567f37b6182SDimitry Andric ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, CombinedIndex, 556824d58133SDimitry Andric ModulePath, ModuleId); 5569f37b6182SDimitry Andric return R.parseModule(); 5570f37b6182SDimitry Andric } 5571f37b6182SDimitry Andric 55727d523365SDimitry Andric // Parse the specified bitcode buffer, returning the function info index. 5573d88c1a5aSDimitry Andric Expected<std::unique_ptr<ModuleSummaryIndex>> BitcodeModule::getSummary() { 5574d88c1a5aSDimitry Andric BitstreamCursor Stream(Buffer); 5575d88c1a5aSDimitry Andric Stream.JumpToBit(ModuleBit); 55767d523365SDimitry Andric 55773ca95b02SDimitry Andric auto Index = llvm::make_unique<ModuleSummaryIndex>(); 5578f37b6182SDimitry Andric ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, *Index, 5579f37b6182SDimitry Andric ModuleIdentifier, 0); 55807d523365SDimitry Andric 5581f37b6182SDimitry Andric if (Error Err = R.parseModule()) 5582d88c1a5aSDimitry Andric return std::move(Err); 55837d523365SDimitry Andric 55847d523365SDimitry Andric return std::move(Index); 55857d523365SDimitry Andric } 55867d523365SDimitry Andric 55873ca95b02SDimitry Andric // Check if the given bitcode buffer contains a global value summary block. 558824d58133SDimitry Andric Expected<BitcodeLTOInfo> BitcodeModule::getLTOInfo() { 5589d88c1a5aSDimitry Andric BitstreamCursor Stream(Buffer); 5590d88c1a5aSDimitry Andric Stream.JumpToBit(ModuleBit); 55917d523365SDimitry Andric 5592d88c1a5aSDimitry Andric if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 5593d88c1a5aSDimitry Andric return error("Invalid record"); 5594d88c1a5aSDimitry Andric 5595d88c1a5aSDimitry Andric while (true) { 5596d88c1a5aSDimitry Andric BitstreamEntry Entry = Stream.advance(); 5597d88c1a5aSDimitry Andric 5598d88c1a5aSDimitry Andric switch (Entry.Kind) { 5599d88c1a5aSDimitry Andric case BitstreamEntry::Error: 5600d88c1a5aSDimitry Andric return error("Malformed block"); 5601d88c1a5aSDimitry Andric case BitstreamEntry::EndBlock: 560224d58133SDimitry Andric return BitcodeLTOInfo{/*IsThinLTO=*/false, /*HasSummary=*/false}; 56037d523365SDimitry Andric 5604d88c1a5aSDimitry Andric case BitstreamEntry::SubBlock: 5605d88c1a5aSDimitry Andric if (Entry.ID == bitc::GLOBALVAL_SUMMARY_BLOCK_ID) 560624d58133SDimitry Andric return BitcodeLTOInfo{/*IsThinLTO=*/true, /*HasSummary=*/true}; 560724d58133SDimitry Andric 560824d58133SDimitry Andric if (Entry.ID == bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID) 560924d58133SDimitry Andric return BitcodeLTOInfo{/*IsThinLTO=*/false, /*HasSummary=*/true}; 56107d523365SDimitry Andric 5611d88c1a5aSDimitry Andric // Ignore other sub-blocks. 5612d88c1a5aSDimitry Andric if (Stream.SkipBlock()) 5613d88c1a5aSDimitry Andric return error("Malformed block"); 5614d88c1a5aSDimitry Andric continue; 5615d88c1a5aSDimitry Andric 5616d88c1a5aSDimitry Andric case BitstreamEntry::Record: 5617d88c1a5aSDimitry Andric Stream.skipRecord(Entry.ID); 5618d88c1a5aSDimitry Andric continue; 5619d88c1a5aSDimitry Andric } 5620d88c1a5aSDimitry Andric } 5621d88c1a5aSDimitry Andric } 5622d88c1a5aSDimitry Andric 5623d88c1a5aSDimitry Andric static Expected<BitcodeModule> getSingleModule(MemoryBufferRef Buffer) { 5624d88c1a5aSDimitry Andric Expected<std::vector<BitcodeModule>> MsOrErr = getBitcodeModuleList(Buffer); 5625d88c1a5aSDimitry Andric if (!MsOrErr) 5626d88c1a5aSDimitry Andric return MsOrErr.takeError(); 5627d88c1a5aSDimitry Andric 5628d88c1a5aSDimitry Andric if (MsOrErr->size() != 1) 5629d88c1a5aSDimitry Andric return error("Expected a single module"); 5630d88c1a5aSDimitry Andric 5631d88c1a5aSDimitry Andric return (*MsOrErr)[0]; 5632d88c1a5aSDimitry Andric } 5633d88c1a5aSDimitry Andric 5634d88c1a5aSDimitry Andric Expected<std::unique_ptr<Module>> 5635d88c1a5aSDimitry Andric llvm::getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context, 5636d88c1a5aSDimitry Andric bool ShouldLazyLoadMetadata, bool IsImporting) { 5637d88c1a5aSDimitry Andric Expected<BitcodeModule> BM = getSingleModule(Buffer); 5638d88c1a5aSDimitry Andric if (!BM) 5639d88c1a5aSDimitry Andric return BM.takeError(); 5640d88c1a5aSDimitry Andric 5641d88c1a5aSDimitry Andric return BM->getLazyModule(Context, ShouldLazyLoadMetadata, IsImporting); 5642d88c1a5aSDimitry Andric } 5643d88c1a5aSDimitry Andric 5644d88c1a5aSDimitry Andric Expected<std::unique_ptr<Module>> llvm::getOwningLazyBitcodeModule( 5645d88c1a5aSDimitry Andric std::unique_ptr<MemoryBuffer> &&Buffer, LLVMContext &Context, 5646d88c1a5aSDimitry Andric bool ShouldLazyLoadMetadata, bool IsImporting) { 5647d88c1a5aSDimitry Andric auto MOrErr = getLazyBitcodeModule(*Buffer, Context, ShouldLazyLoadMetadata, 5648d88c1a5aSDimitry Andric IsImporting); 5649d88c1a5aSDimitry Andric if (MOrErr) 5650d88c1a5aSDimitry Andric (*MOrErr)->setOwnedMemoryBuffer(std::move(Buffer)); 5651d88c1a5aSDimitry Andric return MOrErr; 5652d88c1a5aSDimitry Andric } 5653d88c1a5aSDimitry Andric 5654d88c1a5aSDimitry Andric Expected<std::unique_ptr<Module>> 5655d88c1a5aSDimitry Andric BitcodeModule::parseModule(LLVMContext &Context) { 5656d88c1a5aSDimitry Andric return getModuleImpl(Context, true, false, false); 5657d88c1a5aSDimitry Andric // TODO: Restore the use-lists to the in-memory state when the bitcode was 5658d88c1a5aSDimitry Andric // written. We must defer until the Module has been fully materialized. 5659d88c1a5aSDimitry Andric } 5660d88c1a5aSDimitry Andric 5661d88c1a5aSDimitry Andric Expected<std::unique_ptr<Module>> llvm::parseBitcodeFile(MemoryBufferRef Buffer, 5662d88c1a5aSDimitry Andric LLVMContext &Context) { 5663d88c1a5aSDimitry Andric Expected<BitcodeModule> BM = getSingleModule(Buffer); 5664d88c1a5aSDimitry Andric if (!BM) 5665d88c1a5aSDimitry Andric return BM.takeError(); 5666d88c1a5aSDimitry Andric 5667d88c1a5aSDimitry Andric return BM->parseModule(Context); 5668d88c1a5aSDimitry Andric } 5669d88c1a5aSDimitry Andric 5670d88c1a5aSDimitry Andric Expected<std::string> llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer) { 5671d88c1a5aSDimitry Andric Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); 5672d88c1a5aSDimitry Andric if (!StreamOrErr) 5673d88c1a5aSDimitry Andric return StreamOrErr.takeError(); 5674d88c1a5aSDimitry Andric 5675d88c1a5aSDimitry Andric return readTriple(*StreamOrErr); 5676d88c1a5aSDimitry Andric } 5677d88c1a5aSDimitry Andric 5678d88c1a5aSDimitry Andric Expected<bool> llvm::isBitcodeContainingObjCCategory(MemoryBufferRef Buffer) { 5679d88c1a5aSDimitry Andric Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); 5680d88c1a5aSDimitry Andric if (!StreamOrErr) 5681d88c1a5aSDimitry Andric return StreamOrErr.takeError(); 5682d88c1a5aSDimitry Andric 5683d88c1a5aSDimitry Andric return hasObjCCategory(*StreamOrErr); 5684d88c1a5aSDimitry Andric } 5685d88c1a5aSDimitry Andric 5686d88c1a5aSDimitry Andric Expected<std::string> llvm::getBitcodeProducerString(MemoryBufferRef Buffer) { 5687d88c1a5aSDimitry Andric Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); 5688d88c1a5aSDimitry Andric if (!StreamOrErr) 5689d88c1a5aSDimitry Andric return StreamOrErr.takeError(); 5690d88c1a5aSDimitry Andric 5691d88c1a5aSDimitry Andric return readIdentificationCode(*StreamOrErr); 5692d88c1a5aSDimitry Andric } 5693d88c1a5aSDimitry Andric 5694f37b6182SDimitry Andric Error llvm::readModuleSummaryIndex(MemoryBufferRef Buffer, 5695f37b6182SDimitry Andric ModuleSummaryIndex &CombinedIndex, 569624d58133SDimitry Andric uint64_t ModuleId) { 5697f37b6182SDimitry Andric Expected<BitcodeModule> BM = getSingleModule(Buffer); 5698f37b6182SDimitry Andric if (!BM) 5699f37b6182SDimitry Andric return BM.takeError(); 5700f37b6182SDimitry Andric 570124d58133SDimitry Andric return BM->readSummary(CombinedIndex, BM->getModuleIdentifier(), ModuleId); 5702f37b6182SDimitry Andric } 5703f37b6182SDimitry Andric 5704d88c1a5aSDimitry Andric Expected<std::unique_ptr<ModuleSummaryIndex>> 5705d88c1a5aSDimitry Andric llvm::getModuleSummaryIndex(MemoryBufferRef Buffer) { 5706d88c1a5aSDimitry Andric Expected<BitcodeModule> BM = getSingleModule(Buffer); 5707d88c1a5aSDimitry Andric if (!BM) 5708d88c1a5aSDimitry Andric return BM.takeError(); 5709d88c1a5aSDimitry Andric 5710d88c1a5aSDimitry Andric return BM->getSummary(); 5711d88c1a5aSDimitry Andric } 5712d88c1a5aSDimitry Andric 571324d58133SDimitry Andric Expected<BitcodeLTOInfo> llvm::getBitcodeLTOInfo(MemoryBufferRef Buffer) { 5714d88c1a5aSDimitry Andric Expected<BitcodeModule> BM = getSingleModule(Buffer); 5715d88c1a5aSDimitry Andric if (!BM) 5716d88c1a5aSDimitry Andric return BM.takeError(); 5717d88c1a5aSDimitry Andric 571824d58133SDimitry Andric return BM->getLTOInfo(); 57197d523365SDimitry Andric } 5720f37b6182SDimitry Andric 5721f37b6182SDimitry Andric Expected<std::unique_ptr<ModuleSummaryIndex>> 57225517e702SDimitry Andric llvm::getModuleSummaryIndexForFile(StringRef Path, 57235517e702SDimitry Andric bool IgnoreEmptyThinLTOIndexFile) { 5724f37b6182SDimitry Andric ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = 5725f37b6182SDimitry Andric MemoryBuffer::getFileOrSTDIN(Path); 5726f37b6182SDimitry Andric if (!FileOrErr) 5727f37b6182SDimitry Andric return errorCodeToError(FileOrErr.getError()); 5728f37b6182SDimitry Andric if (IgnoreEmptyThinLTOIndexFile && !(*FileOrErr)->getBufferSize()) 5729f37b6182SDimitry Andric return nullptr; 5730f37b6182SDimitry Andric return getModuleSummaryIndex(**FileOrErr); 5731f37b6182SDimitry Andric } 5732