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" 31d88c1a5aSDimitry Andric #include "llvm/IR/CallingConv.h" 323ca95b02SDimitry Andric #include "llvm/IR/CallSite.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" 43d88c1a5aSDimitry Andric #include "llvm/IR/GlobalAlias.h" 44d88c1a5aSDimitry Andric #include "llvm/IR/GlobalIFunc.h" 45d88c1a5aSDimitry Andric #include "llvm/IR/GlobalIndirectSymbol.h" 46d88c1a5aSDimitry Andric #include "llvm/IR/GlobalObject.h" 47d88c1a5aSDimitry Andric #include "llvm/IR/GlobalValue.h" 48d88c1a5aSDimitry Andric #include "llvm/IR/GlobalVariable.h" 49ff0cc061SDimitry Andric #include "llvm/IR/GVMaterializer.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: 375d88c1a5aSDimitry Andric BitcodeReaderBase(BitstreamCursor Stream) : Stream(std::move(Stream)) { 376d88c1a5aSDimitry Andric this->Stream.setBlockInfo(&BlockInfo); 377d88c1a5aSDimitry Andric } 378d88c1a5aSDimitry Andric 379d88c1a5aSDimitry Andric BitstreamBlockInfo BlockInfo; 380d88c1a5aSDimitry Andric BitstreamCursor Stream; 381d88c1a5aSDimitry Andric 382d88c1a5aSDimitry Andric bool readBlockInfo(); 383d88c1a5aSDimitry Andric 384d88c1a5aSDimitry Andric // Contains an arbitrary and optional string identifying the bitcode producer 385d88c1a5aSDimitry Andric std::string ProducerIdentification; 386d88c1a5aSDimitry Andric 387d88c1a5aSDimitry Andric Error error(const Twine &Message); 388ff0cc061SDimitry Andric }; 389ff0cc061SDimitry Andric 390d88c1a5aSDimitry Andric Error BitcodeReaderBase::error(const Twine &Message) { 391d88c1a5aSDimitry Andric std::string FullMsg = Message.str(); 392d88c1a5aSDimitry Andric if (!ProducerIdentification.empty()) 393d88c1a5aSDimitry Andric FullMsg += " (Producer: '" + ProducerIdentification + "' Reader: 'LLVM " + 394d88c1a5aSDimitry Andric LLVM_VERSION_STRING "')"; 395d88c1a5aSDimitry Andric return ::error(FullMsg); 396ff0cc061SDimitry Andric } 397ff0cc061SDimitry Andric 398d88c1a5aSDimitry Andric class BitcodeReader : public BitcodeReaderBase, public GVMaterializer { 399ff0cc061SDimitry Andric LLVMContext &Context; 4008f0fd8f6SDimitry Andric Module *TheModule = nullptr; 4017d523365SDimitry Andric // Next offset to start scanning for lazy parsing of function bodies. 4028f0fd8f6SDimitry Andric uint64_t NextUnreadBit = 0; 4037d523365SDimitry Andric // Last function offset found in the VST. 4047d523365SDimitry Andric uint64_t LastFunctionBlockBit = 0; 4058f0fd8f6SDimitry Andric bool SeenValueSymbolTable = false; 4067d523365SDimitry Andric uint64_t VSTOffset = 0; 407ff0cc061SDimitry Andric 408ff0cc061SDimitry Andric std::vector<Type*> TypeList; 409ff0cc061SDimitry Andric BitcodeReaderValueList ValueList; 410d88c1a5aSDimitry Andric Optional<MetadataLoader> MDLoader; 411ff0cc061SDimitry Andric std::vector<Comdat *> ComdatList; 412ff0cc061SDimitry Andric SmallVector<Instruction *, 64> InstructionList; 413ff0cc061SDimitry Andric 414ff0cc061SDimitry Andric std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits; 4153ca95b02SDimitry Andric std::vector<std::pair<GlobalIndirectSymbol*, unsigned> > IndirectSymbolInits; 416ff0cc061SDimitry Andric std::vector<std::pair<Function*, unsigned> > FunctionPrefixes; 417ff0cc061SDimitry Andric std::vector<std::pair<Function*, unsigned> > FunctionPrologues; 4188f0fd8f6SDimitry Andric std::vector<std::pair<Function*, unsigned> > FunctionPersonalityFns; 419ff0cc061SDimitry Andric 4208f0fd8f6SDimitry Andric /// The set of attributes by index. Index zero in the file is for null, and 4218f0fd8f6SDimitry Andric /// is thus not represented here. As such all indices are off by one. 422ff0cc061SDimitry Andric std::vector<AttributeSet> MAttributes; 423ff0cc061SDimitry Andric 4247d523365SDimitry Andric /// The set of attribute groups. 425ff0cc061SDimitry Andric std::map<unsigned, AttributeSet> MAttributeGroups; 426ff0cc061SDimitry Andric 4278f0fd8f6SDimitry Andric /// While parsing a function body, this is a list of the basic blocks for the 4288f0fd8f6SDimitry Andric /// function. 429ff0cc061SDimitry Andric std::vector<BasicBlock*> FunctionBBs; 430ff0cc061SDimitry Andric 431ff0cc061SDimitry Andric // When reading the module header, this list is populated with functions that 432ff0cc061SDimitry Andric // have bodies later in the file. 433ff0cc061SDimitry Andric std::vector<Function*> FunctionsWithBodies; 434ff0cc061SDimitry Andric 435ff0cc061SDimitry Andric // When intrinsic functions are encountered which require upgrading they are 436ff0cc061SDimitry Andric // stored here with their replacement function. 4373ca95b02SDimitry Andric typedef DenseMap<Function*, Function*> UpdatedIntrinsicMap; 4383ca95b02SDimitry Andric UpdatedIntrinsicMap UpgradedIntrinsics; 4393ca95b02SDimitry Andric // Intrinsics which were remangled because of types rename 4403ca95b02SDimitry Andric UpdatedIntrinsicMap RemangledIntrinsics; 441ff0cc061SDimitry Andric 442ff0cc061SDimitry Andric // Several operations happen after the module header has been read, but 443ff0cc061SDimitry Andric // before function bodies are processed. This keeps track of whether 444ff0cc061SDimitry Andric // we've done this yet. 4458f0fd8f6SDimitry Andric bool SeenFirstFunctionBody = false; 446ff0cc061SDimitry Andric 4478f0fd8f6SDimitry Andric /// When function bodies are initially scanned, this map contains info about 4488f0fd8f6SDimitry Andric /// where to find deferred function body in the stream. 449ff0cc061SDimitry Andric DenseMap<Function*, uint64_t> DeferredFunctionInfo; 450ff0cc061SDimitry Andric 451ff0cc061SDimitry Andric /// When Metadata block is initially scanned when parsing the module, we may 452ff0cc061SDimitry Andric /// choose to defer parsing of the metadata. This vector contains info about 453ff0cc061SDimitry Andric /// which Metadata blocks are deferred. 454ff0cc061SDimitry Andric std::vector<uint64_t> DeferredMetadataInfo; 455ff0cc061SDimitry Andric 456ff0cc061SDimitry Andric /// These are basic blocks forward-referenced by block addresses. They are 457ff0cc061SDimitry Andric /// inserted lazily into functions when they're loaded. The basic block ID is 458ff0cc061SDimitry Andric /// its index into the vector. 459ff0cc061SDimitry Andric DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs; 460ff0cc061SDimitry Andric std::deque<Function *> BasicBlockFwdRefQueue; 461ff0cc061SDimitry Andric 4628f0fd8f6SDimitry Andric /// Indicates that we are using a new encoding for instruction operands where 4638f0fd8f6SDimitry Andric /// most operands in the current FUNCTION_BLOCK are encoded relative to the 4648f0fd8f6SDimitry Andric /// instruction number, for a more compact encoding. Some instruction 4658f0fd8f6SDimitry Andric /// operands are not relative to the instruction ID: basic block numbers, and 4668f0fd8f6SDimitry Andric /// types. Once the old style function blocks have been phased out, we would 467ff0cc061SDimitry Andric /// not need this flag. 4688f0fd8f6SDimitry Andric bool UseRelativeIDs = false; 469ff0cc061SDimitry Andric 470ff0cc061SDimitry Andric /// True if all functions will be materialized, negating the need to process 471ff0cc061SDimitry Andric /// (e.g.) blockaddress forward references. 4728f0fd8f6SDimitry Andric bool WillMaterializeAllForwardRefs = false; 473ff0cc061SDimitry Andric 474ff0cc061SDimitry Andric bool StripDebugInfo = false; 475d88c1a5aSDimitry Andric TBAAVerifier TBAAVerifyHelper; 4767d523365SDimitry Andric 4777d523365SDimitry Andric std::vector<std::string> BundleTags; 4787d523365SDimitry Andric 479ff0cc061SDimitry Andric public: 480d88c1a5aSDimitry Andric BitcodeReader(BitstreamCursor Stream, StringRef ProducerIdentification, 481d88c1a5aSDimitry Andric LLVMContext &Context); 482ff0cc061SDimitry Andric 483d88c1a5aSDimitry Andric Error materializeForwardReferencedFunctions(); 484ff0cc061SDimitry Andric 485d88c1a5aSDimitry Andric Error materialize(GlobalValue *GV) override; 486d88c1a5aSDimitry Andric Error materializeModule() override; 487ff0cc061SDimitry Andric std::vector<StructType *> getIdentifiedStructTypes() const override; 488ff0cc061SDimitry Andric 4898f0fd8f6SDimitry Andric /// \brief Main interface to parsing a bitcode buffer. 4908f0fd8f6SDimitry Andric /// \returns true if an error occurred. 491d88c1a5aSDimitry Andric Error parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata = false, 492d88c1a5aSDimitry Andric bool IsImporting = false); 4933ca95b02SDimitry Andric 494ff0cc061SDimitry Andric static uint64_t decodeSignRotatedValue(uint64_t V); 495ff0cc061SDimitry Andric 496ff0cc061SDimitry Andric /// Materialize any deferred Metadata block. 497d88c1a5aSDimitry Andric Error materializeMetadata() override; 498ff0cc061SDimitry Andric 499ff0cc061SDimitry Andric void setStripDebugInfo() override; 500ff0cc061SDimitry Andric 501ff0cc061SDimitry Andric private: 502ff0cc061SDimitry Andric std::vector<StructType *> IdentifiedStructTypes; 503ff0cc061SDimitry Andric StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name); 504ff0cc061SDimitry Andric StructType *createIdentifiedStructType(LLVMContext &Context); 505ff0cc061SDimitry Andric 506ff0cc061SDimitry Andric Type *getTypeByID(unsigned ID); 507d88c1a5aSDimitry Andric 508ff0cc061SDimitry Andric Value *getFnValueByID(unsigned ID, Type *Ty) { 509ff0cc061SDimitry Andric if (Ty && Ty->isMetadataTy()) 510ff0cc061SDimitry Andric return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID)); 511ff0cc061SDimitry Andric return ValueList.getValueFwdRef(ID, Ty); 512ff0cc061SDimitry Andric } 513d88c1a5aSDimitry Andric 514ff0cc061SDimitry Andric Metadata *getFnMetadataByID(unsigned ID) { 515d88c1a5aSDimitry Andric return MDLoader->getMetadataFwdRef(ID); 516ff0cc061SDimitry Andric } 517d88c1a5aSDimitry Andric 518ff0cc061SDimitry Andric BasicBlock *getBasicBlock(unsigned ID) const { 519ff0cc061SDimitry Andric if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID 520ff0cc061SDimitry Andric return FunctionBBs[ID]; 521ff0cc061SDimitry Andric } 522d88c1a5aSDimitry Andric 523ff0cc061SDimitry Andric AttributeSet getAttributes(unsigned i) const { 524ff0cc061SDimitry Andric if (i-1 < MAttributes.size()) 525ff0cc061SDimitry Andric return MAttributes[i-1]; 526ff0cc061SDimitry Andric return AttributeSet(); 527ff0cc061SDimitry Andric } 528ff0cc061SDimitry Andric 5298f0fd8f6SDimitry Andric /// Read a value/type pair out of the specified record from slot 'Slot'. 5308f0fd8f6SDimitry Andric /// Increment Slot past the number of slots used in the record. Return true on 5318f0fd8f6SDimitry Andric /// failure. 532ff0cc061SDimitry Andric bool getValueTypePair(SmallVectorImpl<uint64_t> &Record, unsigned &Slot, 533ff0cc061SDimitry Andric unsigned InstNum, Value *&ResVal) { 534ff0cc061SDimitry Andric if (Slot == Record.size()) return true; 535ff0cc061SDimitry Andric unsigned ValNo = (unsigned)Record[Slot++]; 536ff0cc061SDimitry Andric // Adjust the ValNo, if it was encoded relative to the InstNum. 537ff0cc061SDimitry Andric if (UseRelativeIDs) 538ff0cc061SDimitry Andric ValNo = InstNum - ValNo; 539ff0cc061SDimitry Andric if (ValNo < InstNum) { 540ff0cc061SDimitry Andric // If this is not a forward reference, just return the value we already 541ff0cc061SDimitry Andric // have. 542ff0cc061SDimitry Andric ResVal = getFnValueByID(ValNo, nullptr); 543ff0cc061SDimitry Andric return ResVal == nullptr; 544ff0cc061SDimitry Andric } 545ff0cc061SDimitry Andric if (Slot == Record.size()) 546ff0cc061SDimitry Andric return true; 547ff0cc061SDimitry Andric 548ff0cc061SDimitry Andric unsigned TypeNo = (unsigned)Record[Slot++]; 549ff0cc061SDimitry Andric ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo)); 550ff0cc061SDimitry Andric return ResVal == nullptr; 551ff0cc061SDimitry Andric } 552ff0cc061SDimitry Andric 5538f0fd8f6SDimitry Andric /// Read a value out of the specified record from slot 'Slot'. Increment Slot 5548f0fd8f6SDimitry Andric /// past the number of slots used by the value in the record. Return true if 5558f0fd8f6SDimitry Andric /// there is an error. 556ff0cc061SDimitry Andric bool popValue(SmallVectorImpl<uint64_t> &Record, unsigned &Slot, 557ff0cc061SDimitry Andric unsigned InstNum, Type *Ty, Value *&ResVal) { 558ff0cc061SDimitry Andric if (getValue(Record, Slot, InstNum, Ty, ResVal)) 559ff0cc061SDimitry Andric return true; 560ff0cc061SDimitry Andric // All values currently take a single record slot. 561ff0cc061SDimitry Andric ++Slot; 562ff0cc061SDimitry Andric return false; 563ff0cc061SDimitry Andric } 564ff0cc061SDimitry Andric 5658f0fd8f6SDimitry Andric /// Like popValue, but does not increment the Slot number. 566ff0cc061SDimitry Andric bool getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot, 567ff0cc061SDimitry Andric unsigned InstNum, Type *Ty, Value *&ResVal) { 568ff0cc061SDimitry Andric ResVal = getValue(Record, Slot, InstNum, Ty); 569ff0cc061SDimitry Andric return ResVal == nullptr; 570ff0cc061SDimitry Andric } 571ff0cc061SDimitry Andric 5728f0fd8f6SDimitry Andric /// Version of getValue that returns ResVal directly, or 0 if there is an 5738f0fd8f6SDimitry Andric /// error. 574ff0cc061SDimitry Andric Value *getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot, 575ff0cc061SDimitry Andric unsigned InstNum, Type *Ty) { 576ff0cc061SDimitry Andric if (Slot == Record.size()) return nullptr; 577ff0cc061SDimitry Andric unsigned ValNo = (unsigned)Record[Slot]; 578ff0cc061SDimitry Andric // Adjust the ValNo, if it was encoded relative to the InstNum. 579ff0cc061SDimitry Andric if (UseRelativeIDs) 580ff0cc061SDimitry Andric ValNo = InstNum - ValNo; 581ff0cc061SDimitry Andric return getFnValueByID(ValNo, Ty); 582ff0cc061SDimitry Andric } 583ff0cc061SDimitry Andric 5848f0fd8f6SDimitry Andric /// Like getValue, but decodes signed VBRs. 585ff0cc061SDimitry Andric Value *getValueSigned(SmallVectorImpl<uint64_t> &Record, unsigned Slot, 586ff0cc061SDimitry Andric unsigned InstNum, Type *Ty) { 587ff0cc061SDimitry Andric if (Slot == Record.size()) return nullptr; 588ff0cc061SDimitry Andric unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]); 589ff0cc061SDimitry Andric // Adjust the ValNo, if it was encoded relative to the InstNum. 590ff0cc061SDimitry Andric if (UseRelativeIDs) 591ff0cc061SDimitry Andric ValNo = InstNum - ValNo; 592ff0cc061SDimitry Andric return getFnValueByID(ValNo, Ty); 593ff0cc061SDimitry Andric } 594ff0cc061SDimitry Andric 595ff0cc061SDimitry Andric /// Converts alignment exponent (i.e. power of two (or zero)) to the 596ff0cc061SDimitry Andric /// corresponding alignment to use. If alignment is too large, returns 597ff0cc061SDimitry Andric /// a corresponding error code. 598d88c1a5aSDimitry Andric Error parseAlignmentValue(uint64_t Exponent, unsigned &Alignment); 599d88c1a5aSDimitry Andric Error parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind); 600d88c1a5aSDimitry Andric Error parseModule(uint64_t ResumeBit, bool ShouldLazyLoadMetadata = false); 601d88c1a5aSDimitry Andric Error parseAttributeBlock(); 602d88c1a5aSDimitry Andric Error parseAttributeGroupBlock(); 603d88c1a5aSDimitry Andric Error parseTypeTable(); 604d88c1a5aSDimitry Andric Error parseTypeTableBody(); 605d88c1a5aSDimitry Andric Error parseOperandBundleTags(); 606ff0cc061SDimitry Andric 607d88c1a5aSDimitry Andric Expected<Value *> recordValue(SmallVectorImpl<uint64_t> &Record, 6087d523365SDimitry Andric unsigned NameIndex, Triple &TT); 609d88c1a5aSDimitry Andric Error parseValueSymbolTable(uint64_t Offset = 0); 610d88c1a5aSDimitry Andric Error parseConstants(); 611d88c1a5aSDimitry Andric Error rememberAndSkipFunctionBodies(); 612d88c1a5aSDimitry Andric Error rememberAndSkipFunctionBody(); 613ff0cc061SDimitry Andric /// Save the positions of the Metadata blocks and skip parsing the blocks. 614d88c1a5aSDimitry Andric Error rememberAndSkipMetadata(); 615d88c1a5aSDimitry Andric Error typeCheckLoadStoreInst(Type *ValType, Type *PtrType); 616d88c1a5aSDimitry Andric Error parseFunctionBody(Function *F); 617d88c1a5aSDimitry Andric Error globalCleanup(); 618d88c1a5aSDimitry Andric Error resolveGlobalAndIndirectSymbolInits(); 619d88c1a5aSDimitry Andric Error parseUseLists(); 620d88c1a5aSDimitry Andric Error findFunctionInStream( 621ff0cc061SDimitry Andric Function *F, 622ff0cc061SDimitry Andric DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator); 623ff0cc061SDimitry Andric }; 6247d523365SDimitry Andric 6257d523365SDimitry Andric /// Class to manage reading and parsing function summary index bitcode 6267d523365SDimitry Andric /// files/sections. 627d88c1a5aSDimitry Andric class ModuleSummaryIndexBitcodeReader : public BitcodeReaderBase { 628d88c1a5aSDimitry Andric /// The module index built during parsing. 629d88c1a5aSDimitry Andric ModuleSummaryIndex &TheIndex; 6307d523365SDimitry Andric 6313ca95b02SDimitry Andric /// Indicates whether we have encountered a global value summary section 632d88c1a5aSDimitry Andric /// yet during parsing. 6333ca95b02SDimitry Andric bool SeenGlobalValSummary = false; 6347d523365SDimitry Andric 6353ca95b02SDimitry Andric /// Indicates whether we have already parsed the VST, used for error checking. 6363ca95b02SDimitry Andric bool SeenValueSymbolTable = false; 6373ca95b02SDimitry Andric 6383ca95b02SDimitry Andric /// Set to the offset of the VST recorded in the MODULE_CODE_VSTOFFSET record. 6393ca95b02SDimitry Andric /// Used to enable on-demand parsing of the VST. 6403ca95b02SDimitry Andric uint64_t VSTOffset = 0; 6413ca95b02SDimitry Andric 6423ca95b02SDimitry Andric // Map to save ValueId to GUID association that was recorded in the 6433ca95b02SDimitry Andric // ValueSymbolTable. It is used after the VST is parsed to convert 6443ca95b02SDimitry Andric // call graph edges read from the function summary from referencing 6453ca95b02SDimitry Andric // callees by their ValueId to using the GUID instead, which is how 6463ca95b02SDimitry Andric // they are recorded in the summary index being built. 6473ca95b02SDimitry Andric // We save a second GUID which is the same as the first one, but ignoring the 6483ca95b02SDimitry Andric // linkage, i.e. for value other than local linkage they are identical. 6493ca95b02SDimitry Andric DenseMap<unsigned, std::pair<GlobalValue::GUID, GlobalValue::GUID>> 6503ca95b02SDimitry Andric ValueIdToCallGraphGUIDMap; 6517d523365SDimitry Andric 6527d523365SDimitry Andric /// Map populated during module path string table parsing, from the 6537d523365SDimitry Andric /// module ID to a string reference owned by the index's module 6543ca95b02SDimitry Andric /// path string table, used to correlate with combined index 6557d523365SDimitry Andric /// summary records. 6567d523365SDimitry Andric DenseMap<uint64_t, StringRef> ModuleIdMap; 6577d523365SDimitry Andric 6583ca95b02SDimitry Andric /// Original source file name recorded in a bitcode record. 6593ca95b02SDimitry Andric std::string SourceFileName; 6603ca95b02SDimitry Andric 6617d523365SDimitry Andric public: 6623ca95b02SDimitry Andric ModuleSummaryIndexBitcodeReader( 663d88c1a5aSDimitry Andric BitstreamCursor Stream, ModuleSummaryIndex &TheIndex); 6647d523365SDimitry Andric 665d88c1a5aSDimitry Andric Error parseModule(StringRef ModulePath); 6667d523365SDimitry Andric 6677d523365SDimitry Andric private: 668d88c1a5aSDimitry Andric Error parseValueSymbolTable( 6693ca95b02SDimitry Andric uint64_t Offset, 6703ca95b02SDimitry Andric DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap); 671d88c1a5aSDimitry Andric std::vector<ValueInfo> makeRefList(ArrayRef<uint64_t> Record); 672d88c1a5aSDimitry Andric std::vector<FunctionSummary::EdgeTy> makeCallList(ArrayRef<uint64_t> Record, 673d88c1a5aSDimitry Andric bool IsOldProfileFormat, 674d88c1a5aSDimitry Andric bool HasProfile); 675d88c1a5aSDimitry Andric Error parseEntireSummary(StringRef ModulePath); 676d88c1a5aSDimitry Andric Error parseModuleStringTable(); 677d88c1a5aSDimitry Andric 6783ca95b02SDimitry Andric std::pair<GlobalValue::GUID, GlobalValue::GUID> 6793ca95b02SDimitry Andric getGUIDFromValueId(unsigned ValueId); 6807d523365SDimitry Andric }; 681d88c1a5aSDimitry Andric 6823ca95b02SDimitry Andric } // end anonymous namespace 683ff0cc061SDimitry Andric 684d88c1a5aSDimitry Andric std::error_code llvm::errorToErrorCodeAndEmitErrors(LLVMContext &Ctx, 685d88c1a5aSDimitry Andric Error Err) { 686d88c1a5aSDimitry Andric if (Err) { 687d88c1a5aSDimitry Andric std::error_code EC; 688d88c1a5aSDimitry Andric handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) { 689d88c1a5aSDimitry Andric EC = EIB.convertToErrorCode(); 690d88c1a5aSDimitry Andric Ctx.emitError(EIB.message()); 691d88c1a5aSDimitry Andric }); 69239d628a0SDimitry Andric return EC; 693dff0c46cSDimitry Andric } 69439d628a0SDimitry Andric return std::error_code(); 695d88c1a5aSDimitry Andric } 696d88c1a5aSDimitry Andric 697d88c1a5aSDimitry Andric BitcodeReader::BitcodeReader(BitstreamCursor Stream, 698d88c1a5aSDimitry Andric StringRef ProducerIdentification, 699d88c1a5aSDimitry Andric LLVMContext &Context) 700d88c1a5aSDimitry Andric : BitcodeReaderBase(std::move(Stream)), Context(Context), 701d88c1a5aSDimitry Andric ValueList(Context) { 702d88c1a5aSDimitry Andric this->ProducerIdentification = ProducerIdentification; 703d88c1a5aSDimitry Andric } 704d88c1a5aSDimitry Andric 705d88c1a5aSDimitry Andric Error BitcodeReader::materializeForwardReferencedFunctions() { 706d88c1a5aSDimitry Andric if (WillMaterializeAllForwardRefs) 707d88c1a5aSDimitry Andric return Error::success(); 70839d628a0SDimitry Andric 70939d628a0SDimitry Andric // Prevent recursion. 71039d628a0SDimitry Andric WillMaterializeAllForwardRefs = true; 71139d628a0SDimitry Andric 71239d628a0SDimitry Andric while (!BasicBlockFwdRefQueue.empty()) { 71339d628a0SDimitry Andric Function *F = BasicBlockFwdRefQueue.front(); 71439d628a0SDimitry Andric BasicBlockFwdRefQueue.pop_front(); 71539d628a0SDimitry Andric assert(F && "Expected valid function"); 71639d628a0SDimitry Andric if (!BasicBlockFwdRefs.count(F)) 71739d628a0SDimitry Andric // Already materialized. 71839d628a0SDimitry Andric continue; 71939d628a0SDimitry Andric 72039d628a0SDimitry Andric // Check for a function that isn't materializable to prevent an infinite 72139d628a0SDimitry Andric // loop. When parsing a blockaddress stored in a global variable, there 72239d628a0SDimitry Andric // isn't a trivial way to check if a function will have a body without a 72339d628a0SDimitry Andric // linear search through FunctionsWithBodies, so just check it here. 72439d628a0SDimitry Andric if (!F->isMaterializable()) 7258f0fd8f6SDimitry Andric return error("Never resolved function from blockaddress"); 72639d628a0SDimitry Andric 72739d628a0SDimitry Andric // Try to materialize F. 728d88c1a5aSDimitry Andric if (Error Err = materialize(F)) 729d88c1a5aSDimitry Andric return Err; 73039d628a0SDimitry Andric } 73139d628a0SDimitry Andric assert(BasicBlockFwdRefs.empty() && "Function missing from queue"); 73239d628a0SDimitry Andric 73339d628a0SDimitry Andric // Reset state. 73439d628a0SDimitry Andric WillMaterializeAllForwardRefs = false; 735d88c1a5aSDimitry Andric return Error::success(); 736f22ef01cSRoman Divacky } 737f22ef01cSRoman Divacky 738f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 739f22ef01cSRoman Divacky // Helper functions to implement forward reference resolution, etc. 740f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 741f22ef01cSRoman Divacky 742ff0cc061SDimitry Andric static bool hasImplicitComdat(size_t Val) { 743ff0cc061SDimitry Andric switch (Val) { 744ff0cc061SDimitry Andric default: 745ff0cc061SDimitry Andric return false; 746ff0cc061SDimitry Andric case 1: // Old WeakAnyLinkage 747ff0cc061SDimitry Andric case 4: // Old LinkOnceAnyLinkage 748ff0cc061SDimitry Andric case 10: // Old WeakODRLinkage 749ff0cc061SDimitry Andric case 11: // Old LinkOnceODRLinkage 750ff0cc061SDimitry Andric return true; 751ff0cc061SDimitry Andric } 752ff0cc061SDimitry Andric } 753ff0cc061SDimitry Andric 75439d628a0SDimitry Andric static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) { 755f22ef01cSRoman Divacky switch (Val) { 756f22ef01cSRoman Divacky default: // Map unknown/new linkages to external 75739d628a0SDimitry Andric case 0: 75839d628a0SDimitry Andric return GlobalValue::ExternalLinkage; 75939d628a0SDimitry Andric case 2: 76039d628a0SDimitry Andric return GlobalValue::AppendingLinkage; 76139d628a0SDimitry Andric case 3: 76239d628a0SDimitry Andric return GlobalValue::InternalLinkage; 76339d628a0SDimitry Andric case 5: 76439d628a0SDimitry Andric return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage 76539d628a0SDimitry Andric case 6: 76639d628a0SDimitry Andric return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage 76739d628a0SDimitry Andric case 7: 76839d628a0SDimitry Andric return GlobalValue::ExternalWeakLinkage; 76939d628a0SDimitry Andric case 8: 77039d628a0SDimitry Andric return GlobalValue::CommonLinkage; 77139d628a0SDimitry Andric case 9: 77239d628a0SDimitry Andric return GlobalValue::PrivateLinkage; 77339d628a0SDimitry Andric case 12: 77439d628a0SDimitry Andric return GlobalValue::AvailableExternallyLinkage; 77591bc56edSDimitry Andric case 13: 77691bc56edSDimitry Andric return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage 77791bc56edSDimitry Andric case 14: 77891bc56edSDimitry Andric return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage 77939d628a0SDimitry Andric case 15: 78039d628a0SDimitry Andric return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage 781ff0cc061SDimitry Andric case 1: // Old value with implicit comdat. 782ff0cc061SDimitry Andric case 16: 783ff0cc061SDimitry Andric return GlobalValue::WeakAnyLinkage; 784ff0cc061SDimitry Andric case 10: // Old value with implicit comdat. 785ff0cc061SDimitry Andric case 17: 786ff0cc061SDimitry Andric return GlobalValue::WeakODRLinkage; 787ff0cc061SDimitry Andric case 4: // Old value with implicit comdat. 788ff0cc061SDimitry Andric case 18: 789ff0cc061SDimitry Andric return GlobalValue::LinkOnceAnyLinkage; 790ff0cc061SDimitry Andric case 11: // Old value with implicit comdat. 791ff0cc061SDimitry Andric case 19: 792ff0cc061SDimitry Andric return GlobalValue::LinkOnceODRLinkage; 793f22ef01cSRoman Divacky } 794f22ef01cSRoman Divacky } 795f22ef01cSRoman Divacky 796d88c1a5aSDimitry Andric /// Decode the flags for GlobalValue in the summary. 7973ca95b02SDimitry Andric static GlobalValueSummary::GVFlags getDecodedGVSummaryFlags(uint64_t RawFlags, 7983ca95b02SDimitry Andric uint64_t Version) { 7993ca95b02SDimitry Andric // Summary were not emitted before LLVM 3.9, we don't need to upgrade Linkage 8003ca95b02SDimitry Andric // like getDecodedLinkage() above. Any future change to the linkage enum and 8013ca95b02SDimitry Andric // to getDecodedLinkage() will need to be taken into account here as above. 8023ca95b02SDimitry Andric auto Linkage = GlobalValue::LinkageTypes(RawFlags & 0xF); // 4 bits 8033ca95b02SDimitry Andric RawFlags = RawFlags >> 4; 80495ec533aSDimitry Andric bool NotEligibleToImport = (RawFlags & 0x1) || Version < 3; 80595ec533aSDimitry Andric // The LiveRoot flag wasn't introduced until version 3. For dead stripping 80695ec533aSDimitry Andric // to work correctly on earlier versions, we must conservatively treat all 80795ec533aSDimitry Andric // values as live. 80895ec533aSDimitry Andric bool LiveRoot = (RawFlags & 0x2) || Version < 3; 80995ec533aSDimitry Andric return GlobalValueSummary::GVFlags(Linkage, NotEligibleToImport, LiveRoot); 8103ca95b02SDimitry Andric } 8113ca95b02SDimitry Andric 8128f0fd8f6SDimitry Andric static GlobalValue::VisibilityTypes getDecodedVisibility(unsigned Val) { 813f22ef01cSRoman Divacky switch (Val) { 814f22ef01cSRoman Divacky default: // Map unknown visibilities to default. 815f22ef01cSRoman Divacky case 0: return GlobalValue::DefaultVisibility; 816f22ef01cSRoman Divacky case 1: return GlobalValue::HiddenVisibility; 817f22ef01cSRoman Divacky case 2: return GlobalValue::ProtectedVisibility; 818f22ef01cSRoman Divacky } 819f22ef01cSRoman Divacky } 820f22ef01cSRoman Divacky 82191bc56edSDimitry Andric static GlobalValue::DLLStorageClassTypes 8228f0fd8f6SDimitry Andric getDecodedDLLStorageClass(unsigned Val) { 82391bc56edSDimitry Andric switch (Val) { 82491bc56edSDimitry Andric default: // Map unknown values to default. 82591bc56edSDimitry Andric case 0: return GlobalValue::DefaultStorageClass; 82691bc56edSDimitry Andric case 1: return GlobalValue::DLLImportStorageClass; 82791bc56edSDimitry Andric case 2: return GlobalValue::DLLExportStorageClass; 82891bc56edSDimitry Andric } 82991bc56edSDimitry Andric } 83091bc56edSDimitry Andric 8318f0fd8f6SDimitry Andric static GlobalVariable::ThreadLocalMode getDecodedThreadLocalMode(unsigned Val) { 8327ae0e2c9SDimitry Andric switch (Val) { 8337ae0e2c9SDimitry Andric case 0: return GlobalVariable::NotThreadLocal; 8347ae0e2c9SDimitry Andric default: // Map unknown non-zero value to general dynamic. 8357ae0e2c9SDimitry Andric case 1: return GlobalVariable::GeneralDynamicTLSModel; 8367ae0e2c9SDimitry Andric case 2: return GlobalVariable::LocalDynamicTLSModel; 8377ae0e2c9SDimitry Andric case 3: return GlobalVariable::InitialExecTLSModel; 8387ae0e2c9SDimitry Andric case 4: return GlobalVariable::LocalExecTLSModel; 8397ae0e2c9SDimitry Andric } 8407ae0e2c9SDimitry Andric } 8417ae0e2c9SDimitry Andric 8423ca95b02SDimitry Andric static GlobalVariable::UnnamedAddr getDecodedUnnamedAddrType(unsigned Val) { 8433ca95b02SDimitry Andric switch (Val) { 8443ca95b02SDimitry Andric default: // Map unknown to UnnamedAddr::None. 8453ca95b02SDimitry Andric case 0: return GlobalVariable::UnnamedAddr::None; 8463ca95b02SDimitry Andric case 1: return GlobalVariable::UnnamedAddr::Global; 8473ca95b02SDimitry Andric case 2: return GlobalVariable::UnnamedAddr::Local; 8483ca95b02SDimitry Andric } 8493ca95b02SDimitry Andric } 8503ca95b02SDimitry Andric 8518f0fd8f6SDimitry Andric static int getDecodedCastOpcode(unsigned Val) { 852f22ef01cSRoman Divacky switch (Val) { 853f22ef01cSRoman Divacky default: return -1; 854f22ef01cSRoman Divacky case bitc::CAST_TRUNC : return Instruction::Trunc; 855f22ef01cSRoman Divacky case bitc::CAST_ZEXT : return Instruction::ZExt; 856f22ef01cSRoman Divacky case bitc::CAST_SEXT : return Instruction::SExt; 857f22ef01cSRoman Divacky case bitc::CAST_FPTOUI : return Instruction::FPToUI; 858f22ef01cSRoman Divacky case bitc::CAST_FPTOSI : return Instruction::FPToSI; 859f22ef01cSRoman Divacky case bitc::CAST_UITOFP : return Instruction::UIToFP; 860f22ef01cSRoman Divacky case bitc::CAST_SITOFP : return Instruction::SIToFP; 861f22ef01cSRoman Divacky case bitc::CAST_FPTRUNC : return Instruction::FPTrunc; 862f22ef01cSRoman Divacky case bitc::CAST_FPEXT : return Instruction::FPExt; 863f22ef01cSRoman Divacky case bitc::CAST_PTRTOINT: return Instruction::PtrToInt; 864f22ef01cSRoman Divacky case bitc::CAST_INTTOPTR: return Instruction::IntToPtr; 865f22ef01cSRoman Divacky case bitc::CAST_BITCAST : return Instruction::BitCast; 866f785676fSDimitry Andric case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast; 867f22ef01cSRoman Divacky } 868f22ef01cSRoman Divacky } 869ff0cc061SDimitry Andric 8708f0fd8f6SDimitry Andric static int getDecodedBinaryOpcode(unsigned Val, Type *Ty) { 871ff0cc061SDimitry Andric bool IsFP = Ty->isFPOrFPVectorTy(); 872ff0cc061SDimitry Andric // BinOps are only valid for int/fp or vector of int/fp types 873ff0cc061SDimitry Andric if (!IsFP && !Ty->isIntOrIntVectorTy()) 874ff0cc061SDimitry Andric return -1; 875ff0cc061SDimitry Andric 876f22ef01cSRoman Divacky switch (Val) { 877ff0cc061SDimitry Andric default: 878ff0cc061SDimitry Andric return -1; 879f22ef01cSRoman Divacky case bitc::BINOP_ADD: 880ff0cc061SDimitry Andric return IsFP ? Instruction::FAdd : Instruction::Add; 881f22ef01cSRoman Divacky case bitc::BINOP_SUB: 882ff0cc061SDimitry Andric return IsFP ? Instruction::FSub : Instruction::Sub; 883f22ef01cSRoman Divacky case bitc::BINOP_MUL: 884ff0cc061SDimitry Andric return IsFP ? Instruction::FMul : Instruction::Mul; 885ff0cc061SDimitry Andric case bitc::BINOP_UDIV: 886ff0cc061SDimitry Andric return IsFP ? -1 : Instruction::UDiv; 887f22ef01cSRoman Divacky case bitc::BINOP_SDIV: 888ff0cc061SDimitry Andric return IsFP ? Instruction::FDiv : Instruction::SDiv; 889ff0cc061SDimitry Andric case bitc::BINOP_UREM: 890ff0cc061SDimitry Andric return IsFP ? -1 : Instruction::URem; 891f22ef01cSRoman Divacky case bitc::BINOP_SREM: 892ff0cc061SDimitry Andric return IsFP ? Instruction::FRem : Instruction::SRem; 893ff0cc061SDimitry Andric case bitc::BINOP_SHL: 894ff0cc061SDimitry Andric return IsFP ? -1 : Instruction::Shl; 895ff0cc061SDimitry Andric case bitc::BINOP_LSHR: 896ff0cc061SDimitry Andric return IsFP ? -1 : Instruction::LShr; 897ff0cc061SDimitry Andric case bitc::BINOP_ASHR: 898ff0cc061SDimitry Andric return IsFP ? -1 : Instruction::AShr; 899ff0cc061SDimitry Andric case bitc::BINOP_AND: 900ff0cc061SDimitry Andric return IsFP ? -1 : Instruction::And; 901ff0cc061SDimitry Andric case bitc::BINOP_OR: 902ff0cc061SDimitry Andric return IsFP ? -1 : Instruction::Or; 903ff0cc061SDimitry Andric case bitc::BINOP_XOR: 904ff0cc061SDimitry Andric return IsFP ? -1 : Instruction::Xor; 905f22ef01cSRoman Divacky } 906f22ef01cSRoman Divacky } 907f22ef01cSRoman Divacky 9088f0fd8f6SDimitry Andric static AtomicRMWInst::BinOp getDecodedRMWOperation(unsigned Val) { 9096122f3e6SDimitry Andric switch (Val) { 9106122f3e6SDimitry Andric default: return AtomicRMWInst::BAD_BINOP; 9116122f3e6SDimitry Andric case bitc::RMW_XCHG: return AtomicRMWInst::Xchg; 9126122f3e6SDimitry Andric case bitc::RMW_ADD: return AtomicRMWInst::Add; 9136122f3e6SDimitry Andric case bitc::RMW_SUB: return AtomicRMWInst::Sub; 9146122f3e6SDimitry Andric case bitc::RMW_AND: return AtomicRMWInst::And; 9156122f3e6SDimitry Andric case bitc::RMW_NAND: return AtomicRMWInst::Nand; 9166122f3e6SDimitry Andric case bitc::RMW_OR: return AtomicRMWInst::Or; 9176122f3e6SDimitry Andric case bitc::RMW_XOR: return AtomicRMWInst::Xor; 9186122f3e6SDimitry Andric case bitc::RMW_MAX: return AtomicRMWInst::Max; 9196122f3e6SDimitry Andric case bitc::RMW_MIN: return AtomicRMWInst::Min; 9206122f3e6SDimitry Andric case bitc::RMW_UMAX: return AtomicRMWInst::UMax; 9216122f3e6SDimitry Andric case bitc::RMW_UMIN: return AtomicRMWInst::UMin; 9226122f3e6SDimitry Andric } 9236122f3e6SDimitry Andric } 9246122f3e6SDimitry Andric 9258f0fd8f6SDimitry Andric static AtomicOrdering getDecodedOrdering(unsigned Val) { 9266122f3e6SDimitry Andric switch (Val) { 9273ca95b02SDimitry Andric case bitc::ORDERING_NOTATOMIC: return AtomicOrdering::NotAtomic; 9283ca95b02SDimitry Andric case bitc::ORDERING_UNORDERED: return AtomicOrdering::Unordered; 9293ca95b02SDimitry Andric case bitc::ORDERING_MONOTONIC: return AtomicOrdering::Monotonic; 9303ca95b02SDimitry Andric case bitc::ORDERING_ACQUIRE: return AtomicOrdering::Acquire; 9313ca95b02SDimitry Andric case bitc::ORDERING_RELEASE: return AtomicOrdering::Release; 9323ca95b02SDimitry Andric case bitc::ORDERING_ACQREL: return AtomicOrdering::AcquireRelease; 9336122f3e6SDimitry Andric default: // Map unknown orderings to sequentially-consistent. 9343ca95b02SDimitry Andric case bitc::ORDERING_SEQCST: return AtomicOrdering::SequentiallyConsistent; 9356122f3e6SDimitry Andric } 9366122f3e6SDimitry Andric } 9376122f3e6SDimitry Andric 9388f0fd8f6SDimitry Andric static SynchronizationScope getDecodedSynchScope(unsigned Val) { 9396122f3e6SDimitry Andric switch (Val) { 9406122f3e6SDimitry Andric case bitc::SYNCHSCOPE_SINGLETHREAD: return SingleThread; 9416122f3e6SDimitry Andric default: // Map unknown scopes to cross-thread. 9426122f3e6SDimitry Andric case bitc::SYNCHSCOPE_CROSSTHREAD: return CrossThread; 9436122f3e6SDimitry Andric } 9446122f3e6SDimitry Andric } 9456122f3e6SDimitry Andric 94691bc56edSDimitry Andric static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) { 94791bc56edSDimitry Andric switch (Val) { 94891bc56edSDimitry Andric default: // Map unknown selection kinds to any. 94991bc56edSDimitry Andric case bitc::COMDAT_SELECTION_KIND_ANY: 95091bc56edSDimitry Andric return Comdat::Any; 95191bc56edSDimitry Andric case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH: 95291bc56edSDimitry Andric return Comdat::ExactMatch; 95391bc56edSDimitry Andric case bitc::COMDAT_SELECTION_KIND_LARGEST: 95491bc56edSDimitry Andric return Comdat::Largest; 95591bc56edSDimitry Andric case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES: 95691bc56edSDimitry Andric return Comdat::NoDuplicates; 95791bc56edSDimitry Andric case bitc::COMDAT_SELECTION_KIND_SAME_SIZE: 95891bc56edSDimitry Andric return Comdat::SameSize; 95991bc56edSDimitry Andric } 96091bc56edSDimitry Andric } 96191bc56edSDimitry Andric 962875ed548SDimitry Andric static FastMathFlags getDecodedFastMathFlags(unsigned Val) { 963875ed548SDimitry Andric FastMathFlags FMF; 964875ed548SDimitry Andric if (0 != (Val & FastMathFlags::UnsafeAlgebra)) 965875ed548SDimitry Andric FMF.setUnsafeAlgebra(); 966875ed548SDimitry Andric if (0 != (Val & FastMathFlags::NoNaNs)) 967875ed548SDimitry Andric FMF.setNoNaNs(); 968875ed548SDimitry Andric if (0 != (Val & FastMathFlags::NoInfs)) 969875ed548SDimitry Andric FMF.setNoInfs(); 970875ed548SDimitry Andric if (0 != (Val & FastMathFlags::NoSignedZeros)) 971875ed548SDimitry Andric FMF.setNoSignedZeros(); 972875ed548SDimitry Andric if (0 != (Val & FastMathFlags::AllowReciprocal)) 973875ed548SDimitry Andric FMF.setAllowReciprocal(); 974875ed548SDimitry Andric return FMF; 975875ed548SDimitry Andric } 976875ed548SDimitry Andric 977d88c1a5aSDimitry Andric static void upgradeDLLImportExportLinkage(GlobalValue *GV, unsigned Val) { 97891bc56edSDimitry Andric switch (Val) { 97991bc56edSDimitry Andric case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break; 98091bc56edSDimitry Andric case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break; 98191bc56edSDimitry Andric } 98291bc56edSDimitry Andric } 98391bc56edSDimitry Andric 9843ca95b02SDimitry Andric 98517a519f9SDimitry Andric Type *BitcodeReader::getTypeByID(unsigned ID) { 98617a519f9SDimitry Andric // The type table size is always specified correctly. 98717a519f9SDimitry Andric if (ID >= TypeList.size()) 98891bc56edSDimitry Andric return nullptr; 989f22ef01cSRoman Divacky 99017a519f9SDimitry Andric if (Type *Ty = TypeList[ID]) 99117a519f9SDimitry Andric return Ty; 99217a519f9SDimitry Andric 99317a519f9SDimitry Andric // If we have a forward reference, the only possible case is when it is to a 99417a519f9SDimitry Andric // named struct. Just create a placeholder for now. 99539d628a0SDimitry Andric return TypeList[ID] = createIdentifiedStructType(Context); 99639d628a0SDimitry Andric } 99739d628a0SDimitry Andric 99839d628a0SDimitry Andric StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context, 99939d628a0SDimitry Andric StringRef Name) { 100039d628a0SDimitry Andric auto *Ret = StructType::create(Context, Name); 100139d628a0SDimitry Andric IdentifiedStructTypes.push_back(Ret); 100239d628a0SDimitry Andric return Ret; 100339d628a0SDimitry Andric } 100439d628a0SDimitry Andric 100539d628a0SDimitry Andric StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) { 100639d628a0SDimitry Andric auto *Ret = StructType::create(Context); 100739d628a0SDimitry Andric IdentifiedStructTypes.push_back(Ret); 100839d628a0SDimitry Andric return Ret; 1009f22ef01cSRoman Divacky } 1010f22ef01cSRoman Divacky 1011f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 1012f22ef01cSRoman Divacky // Functions for parsing blocks from the bitcode file 1013f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 1014f22ef01cSRoman Divacky 1015d88c1a5aSDimitry Andric static uint64_t getRawAttributeMask(Attribute::AttrKind Val) { 1016d88c1a5aSDimitry Andric switch (Val) { 1017d88c1a5aSDimitry Andric case Attribute::EndAttrKinds: 1018d88c1a5aSDimitry Andric llvm_unreachable("Synthetic enumerators which should never get here"); 1019d88c1a5aSDimitry Andric 1020d88c1a5aSDimitry Andric case Attribute::None: return 0; 1021d88c1a5aSDimitry Andric case Attribute::ZExt: return 1 << 0; 1022d88c1a5aSDimitry Andric case Attribute::SExt: return 1 << 1; 1023d88c1a5aSDimitry Andric case Attribute::NoReturn: return 1 << 2; 1024d88c1a5aSDimitry Andric case Attribute::InReg: return 1 << 3; 1025d88c1a5aSDimitry Andric case Attribute::StructRet: return 1 << 4; 1026d88c1a5aSDimitry Andric case Attribute::NoUnwind: return 1 << 5; 1027d88c1a5aSDimitry Andric case Attribute::NoAlias: return 1 << 6; 1028d88c1a5aSDimitry Andric case Attribute::ByVal: return 1 << 7; 1029d88c1a5aSDimitry Andric case Attribute::Nest: return 1 << 8; 1030d88c1a5aSDimitry Andric case Attribute::ReadNone: return 1 << 9; 1031d88c1a5aSDimitry Andric case Attribute::ReadOnly: return 1 << 10; 1032d88c1a5aSDimitry Andric case Attribute::NoInline: return 1 << 11; 1033d88c1a5aSDimitry Andric case Attribute::AlwaysInline: return 1 << 12; 1034d88c1a5aSDimitry Andric case Attribute::OptimizeForSize: return 1 << 13; 1035d88c1a5aSDimitry Andric case Attribute::StackProtect: return 1 << 14; 1036d88c1a5aSDimitry Andric case Attribute::StackProtectReq: return 1 << 15; 1037d88c1a5aSDimitry Andric case Attribute::Alignment: return 31 << 16; 1038d88c1a5aSDimitry Andric case Attribute::NoCapture: return 1 << 21; 1039d88c1a5aSDimitry Andric case Attribute::NoRedZone: return 1 << 22; 1040d88c1a5aSDimitry Andric case Attribute::NoImplicitFloat: return 1 << 23; 1041d88c1a5aSDimitry Andric case Attribute::Naked: return 1 << 24; 1042d88c1a5aSDimitry Andric case Attribute::InlineHint: return 1 << 25; 1043d88c1a5aSDimitry Andric case Attribute::StackAlignment: return 7 << 26; 1044d88c1a5aSDimitry Andric case Attribute::ReturnsTwice: return 1 << 29; 1045d88c1a5aSDimitry Andric case Attribute::UWTable: return 1 << 30; 1046d88c1a5aSDimitry Andric case Attribute::NonLazyBind: return 1U << 31; 1047d88c1a5aSDimitry Andric case Attribute::SanitizeAddress: return 1ULL << 32; 1048d88c1a5aSDimitry Andric case Attribute::MinSize: return 1ULL << 33; 1049d88c1a5aSDimitry Andric case Attribute::NoDuplicate: return 1ULL << 34; 1050d88c1a5aSDimitry Andric case Attribute::StackProtectStrong: return 1ULL << 35; 1051d88c1a5aSDimitry Andric case Attribute::SanitizeThread: return 1ULL << 36; 1052d88c1a5aSDimitry Andric case Attribute::SanitizeMemory: return 1ULL << 37; 1053d88c1a5aSDimitry Andric case Attribute::NoBuiltin: return 1ULL << 38; 1054d88c1a5aSDimitry Andric case Attribute::Returned: return 1ULL << 39; 1055d88c1a5aSDimitry Andric case Attribute::Cold: return 1ULL << 40; 1056d88c1a5aSDimitry Andric case Attribute::Builtin: return 1ULL << 41; 1057d88c1a5aSDimitry Andric case Attribute::OptimizeNone: return 1ULL << 42; 1058d88c1a5aSDimitry Andric case Attribute::InAlloca: return 1ULL << 43; 1059d88c1a5aSDimitry Andric case Attribute::NonNull: return 1ULL << 44; 1060d88c1a5aSDimitry Andric case Attribute::JumpTable: return 1ULL << 45; 1061d88c1a5aSDimitry Andric case Attribute::Convergent: return 1ULL << 46; 1062d88c1a5aSDimitry Andric case Attribute::SafeStack: return 1ULL << 47; 1063d88c1a5aSDimitry Andric case Attribute::NoRecurse: return 1ULL << 48; 1064d88c1a5aSDimitry Andric case Attribute::InaccessibleMemOnly: return 1ULL << 49; 1065d88c1a5aSDimitry Andric case Attribute::InaccessibleMemOrArgMemOnly: return 1ULL << 50; 1066d88c1a5aSDimitry Andric case Attribute::SwiftSelf: return 1ULL << 51; 1067d88c1a5aSDimitry Andric case Attribute::SwiftError: return 1ULL << 52; 1068d88c1a5aSDimitry Andric case Attribute::WriteOnly: return 1ULL << 53; 1069d88c1a5aSDimitry Andric case Attribute::Dereferenceable: 1070d88c1a5aSDimitry Andric llvm_unreachable("dereferenceable attribute not supported in raw format"); 1071d88c1a5aSDimitry Andric break; 1072d88c1a5aSDimitry Andric case Attribute::DereferenceableOrNull: 1073d88c1a5aSDimitry Andric llvm_unreachable("dereferenceable_or_null attribute not supported in raw " 1074d88c1a5aSDimitry Andric "format"); 1075d88c1a5aSDimitry Andric break; 1076d88c1a5aSDimitry Andric case Attribute::ArgMemOnly: 1077d88c1a5aSDimitry Andric llvm_unreachable("argmemonly attribute not supported in raw format"); 1078d88c1a5aSDimitry Andric break; 1079d88c1a5aSDimitry Andric case Attribute::AllocSize: 1080d88c1a5aSDimitry Andric llvm_unreachable("allocsize not supported in raw format"); 1081d88c1a5aSDimitry Andric break; 1082d88c1a5aSDimitry Andric } 1083d88c1a5aSDimitry Andric llvm_unreachable("Unsupported attribute type"); 1084d88c1a5aSDimitry Andric } 1085d88c1a5aSDimitry Andric 1086d88c1a5aSDimitry Andric static void addRawAttributeValue(AttrBuilder &B, uint64_t Val) { 1087d88c1a5aSDimitry Andric if (!Val) return; 1088d88c1a5aSDimitry Andric 1089d88c1a5aSDimitry Andric for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds; 1090d88c1a5aSDimitry Andric I = Attribute::AttrKind(I + 1)) { 1091d88c1a5aSDimitry Andric if (I == Attribute::Dereferenceable || 1092d88c1a5aSDimitry Andric I == Attribute::DereferenceableOrNull || 1093d88c1a5aSDimitry Andric I == Attribute::ArgMemOnly || 1094d88c1a5aSDimitry Andric I == Attribute::AllocSize) 1095d88c1a5aSDimitry Andric continue; 1096d88c1a5aSDimitry Andric if (uint64_t A = (Val & getRawAttributeMask(I))) { 1097d88c1a5aSDimitry Andric if (I == Attribute::Alignment) 1098d88c1a5aSDimitry Andric B.addAlignmentAttr(1ULL << ((A >> 16) - 1)); 1099d88c1a5aSDimitry Andric else if (I == Attribute::StackAlignment) 1100d88c1a5aSDimitry Andric B.addStackAlignmentAttr(1ULL << ((A >> 26)-1)); 1101d88c1a5aSDimitry Andric else 1102d88c1a5aSDimitry Andric B.addAttribute(I); 1103d88c1a5aSDimitry Andric } 1104d88c1a5aSDimitry Andric } 1105d88c1a5aSDimitry Andric } 1106139f7f9bSDimitry Andric 1107139f7f9bSDimitry Andric /// \brief This fills an AttrBuilder object with the LLVM attributes that have 1108139f7f9bSDimitry Andric /// been decoded from the given integer. This function must stay in sync with 1109139f7f9bSDimitry Andric /// 'encodeLLVMAttributesForBitcode'. 1110139f7f9bSDimitry Andric static void decodeLLVMAttributesForBitcode(AttrBuilder &B, 1111139f7f9bSDimitry Andric uint64_t EncodedAttrs) { 1112139f7f9bSDimitry Andric // FIXME: Remove in 4.0. 1113139f7f9bSDimitry Andric 1114139f7f9bSDimitry Andric // The alignment is stored as a 16-bit raw value from bits 31--16. We shift 1115139f7f9bSDimitry Andric // the bits above 31 down by 11 bits. 1116139f7f9bSDimitry Andric unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16; 1117139f7f9bSDimitry Andric assert((!Alignment || isPowerOf2_32(Alignment)) && 1118139f7f9bSDimitry Andric "Alignment must be a power of two."); 1119139f7f9bSDimitry Andric 1120139f7f9bSDimitry Andric if (Alignment) 1121139f7f9bSDimitry Andric B.addAlignmentAttr(Alignment); 1122d88c1a5aSDimitry Andric addRawAttributeValue(B, ((EncodedAttrs & (0xfffffULL << 32)) >> 11) | 1123139f7f9bSDimitry Andric (EncodedAttrs & 0xffff)); 1124139f7f9bSDimitry Andric } 1125139f7f9bSDimitry Andric 1126d88c1a5aSDimitry Andric Error BitcodeReader::parseAttributeBlock() { 1127f22ef01cSRoman Divacky if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID)) 11288f0fd8f6SDimitry Andric return error("Invalid record"); 1129f22ef01cSRoman Divacky 1130f22ef01cSRoman Divacky if (!MAttributes.empty()) 11318f0fd8f6SDimitry Andric return error("Invalid multiple blocks"); 1132f22ef01cSRoman Divacky 1133f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 1134f22ef01cSRoman Divacky 1135139f7f9bSDimitry Andric SmallVector<AttributeSet, 8> Attrs; 1136f22ef01cSRoman Divacky 1137f22ef01cSRoman Divacky // Read all the records. 1138d88c1a5aSDimitry Andric while (true) { 1139139f7f9bSDimitry Andric BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 1140139f7f9bSDimitry Andric 1141139f7f9bSDimitry Andric switch (Entry.Kind) { 1142139f7f9bSDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 1143139f7f9bSDimitry Andric case BitstreamEntry::Error: 11448f0fd8f6SDimitry Andric return error("Malformed block"); 1145139f7f9bSDimitry Andric case BitstreamEntry::EndBlock: 1146d88c1a5aSDimitry Andric return Error::success(); 1147139f7f9bSDimitry Andric case BitstreamEntry::Record: 1148139f7f9bSDimitry Andric // The interesting case. 1149139f7f9bSDimitry Andric break; 1150f22ef01cSRoman Divacky } 1151f22ef01cSRoman Divacky 1152f22ef01cSRoman Divacky // Read a record. 1153f22ef01cSRoman Divacky Record.clear(); 1154139f7f9bSDimitry Andric switch (Stream.readRecord(Entry.ID, Record)) { 1155f22ef01cSRoman Divacky default: // Default behavior: ignore. 1156f22ef01cSRoman Divacky break; 1157139f7f9bSDimitry Andric case bitc::PARAMATTR_CODE_ENTRY_OLD: { // ENTRY: [paramidx0, attr0, ...] 1158139f7f9bSDimitry Andric // FIXME: Remove in 4.0. 1159f22ef01cSRoman Divacky if (Record.size() & 1) 11608f0fd8f6SDimitry Andric return error("Invalid record"); 1161f22ef01cSRoman Divacky 1162f22ef01cSRoman Divacky for (unsigned i = 0, e = Record.size(); i != e; i += 2) { 1163139f7f9bSDimitry Andric AttrBuilder B; 1164139f7f9bSDimitry Andric decodeLLVMAttributesForBitcode(B, Record[i+1]); 1165139f7f9bSDimitry Andric Attrs.push_back(AttributeSet::get(Context, Record[i], B)); 1166f22ef01cSRoman Divacky } 1167f22ef01cSRoman Divacky 1168139f7f9bSDimitry Andric MAttributes.push_back(AttributeSet::get(Context, Attrs)); 1169f22ef01cSRoman Divacky Attrs.clear(); 1170f22ef01cSRoman Divacky break; 1171f22ef01cSRoman Divacky } 1172139f7f9bSDimitry Andric case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [attrgrp0, attrgrp1, ...] 1173139f7f9bSDimitry Andric for (unsigned i = 0, e = Record.size(); i != e; ++i) 1174139f7f9bSDimitry Andric Attrs.push_back(MAttributeGroups[Record[i]]); 1175139f7f9bSDimitry Andric 1176139f7f9bSDimitry Andric MAttributes.push_back(AttributeSet::get(Context, Attrs)); 1177139f7f9bSDimitry Andric Attrs.clear(); 1178139f7f9bSDimitry Andric break; 1179139f7f9bSDimitry Andric } 1180139f7f9bSDimitry Andric } 1181139f7f9bSDimitry Andric } 1182139f7f9bSDimitry Andric } 1183139f7f9bSDimitry Andric 1184f785676fSDimitry Andric // Returns Attribute::None on unrecognized codes. 11858f0fd8f6SDimitry Andric static Attribute::AttrKind getAttrFromCode(uint64_t Code) { 1186f785676fSDimitry Andric switch (Code) { 1187f785676fSDimitry Andric default: 1188f785676fSDimitry Andric return Attribute::None; 1189f785676fSDimitry Andric case bitc::ATTR_KIND_ALIGNMENT: 1190f785676fSDimitry Andric return Attribute::Alignment; 1191f785676fSDimitry Andric case bitc::ATTR_KIND_ALWAYS_INLINE: 1192f785676fSDimitry Andric return Attribute::AlwaysInline; 1193875ed548SDimitry Andric case bitc::ATTR_KIND_ARGMEMONLY: 1194875ed548SDimitry Andric return Attribute::ArgMemOnly; 1195f785676fSDimitry Andric case bitc::ATTR_KIND_BUILTIN: 1196f785676fSDimitry Andric return Attribute::Builtin; 1197f785676fSDimitry Andric case bitc::ATTR_KIND_BY_VAL: 1198f785676fSDimitry Andric return Attribute::ByVal; 119991bc56edSDimitry Andric case bitc::ATTR_KIND_IN_ALLOCA: 120091bc56edSDimitry Andric return Attribute::InAlloca; 1201f785676fSDimitry Andric case bitc::ATTR_KIND_COLD: 1202f785676fSDimitry Andric return Attribute::Cold; 1203ff0cc061SDimitry Andric case bitc::ATTR_KIND_CONVERGENT: 1204ff0cc061SDimitry Andric return Attribute::Convergent; 12057d523365SDimitry Andric case bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY: 12067d523365SDimitry Andric return Attribute::InaccessibleMemOnly; 12077d523365SDimitry Andric case bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY: 12087d523365SDimitry Andric return Attribute::InaccessibleMemOrArgMemOnly; 1209f785676fSDimitry Andric case bitc::ATTR_KIND_INLINE_HINT: 1210f785676fSDimitry Andric return Attribute::InlineHint; 1211f785676fSDimitry Andric case bitc::ATTR_KIND_IN_REG: 1212f785676fSDimitry Andric return Attribute::InReg; 121391bc56edSDimitry Andric case bitc::ATTR_KIND_JUMP_TABLE: 121491bc56edSDimitry Andric return Attribute::JumpTable; 1215f785676fSDimitry Andric case bitc::ATTR_KIND_MIN_SIZE: 1216f785676fSDimitry Andric return Attribute::MinSize; 1217f785676fSDimitry Andric case bitc::ATTR_KIND_NAKED: 1218f785676fSDimitry Andric return Attribute::Naked; 1219f785676fSDimitry Andric case bitc::ATTR_KIND_NEST: 1220f785676fSDimitry Andric return Attribute::Nest; 1221f785676fSDimitry Andric case bitc::ATTR_KIND_NO_ALIAS: 1222f785676fSDimitry Andric return Attribute::NoAlias; 1223f785676fSDimitry Andric case bitc::ATTR_KIND_NO_BUILTIN: 1224f785676fSDimitry Andric return Attribute::NoBuiltin; 1225f785676fSDimitry Andric case bitc::ATTR_KIND_NO_CAPTURE: 1226f785676fSDimitry Andric return Attribute::NoCapture; 1227f785676fSDimitry Andric case bitc::ATTR_KIND_NO_DUPLICATE: 1228f785676fSDimitry Andric return Attribute::NoDuplicate; 1229f785676fSDimitry Andric case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT: 1230f785676fSDimitry Andric return Attribute::NoImplicitFloat; 1231f785676fSDimitry Andric case bitc::ATTR_KIND_NO_INLINE: 1232f785676fSDimitry Andric return Attribute::NoInline; 12337d523365SDimitry Andric case bitc::ATTR_KIND_NO_RECURSE: 12347d523365SDimitry Andric return Attribute::NoRecurse; 1235f785676fSDimitry Andric case bitc::ATTR_KIND_NON_LAZY_BIND: 1236f785676fSDimitry Andric return Attribute::NonLazyBind; 123791bc56edSDimitry Andric case bitc::ATTR_KIND_NON_NULL: 123891bc56edSDimitry Andric return Attribute::NonNull; 123991bc56edSDimitry Andric case bitc::ATTR_KIND_DEREFERENCEABLE: 124091bc56edSDimitry Andric return Attribute::Dereferenceable; 1241ff0cc061SDimitry Andric case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL: 1242ff0cc061SDimitry Andric return Attribute::DereferenceableOrNull; 12433ca95b02SDimitry Andric case bitc::ATTR_KIND_ALLOC_SIZE: 12443ca95b02SDimitry Andric return Attribute::AllocSize; 1245f785676fSDimitry Andric case bitc::ATTR_KIND_NO_RED_ZONE: 1246f785676fSDimitry Andric return Attribute::NoRedZone; 1247f785676fSDimitry Andric case bitc::ATTR_KIND_NO_RETURN: 1248f785676fSDimitry Andric return Attribute::NoReturn; 1249f785676fSDimitry Andric case bitc::ATTR_KIND_NO_UNWIND: 1250f785676fSDimitry Andric return Attribute::NoUnwind; 1251f785676fSDimitry Andric case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE: 1252f785676fSDimitry Andric return Attribute::OptimizeForSize; 1253f785676fSDimitry Andric case bitc::ATTR_KIND_OPTIMIZE_NONE: 1254f785676fSDimitry Andric return Attribute::OptimizeNone; 1255f785676fSDimitry Andric case bitc::ATTR_KIND_READ_NONE: 1256f785676fSDimitry Andric return Attribute::ReadNone; 1257f785676fSDimitry Andric case bitc::ATTR_KIND_READ_ONLY: 1258f785676fSDimitry Andric return Attribute::ReadOnly; 1259f785676fSDimitry Andric case bitc::ATTR_KIND_RETURNED: 1260f785676fSDimitry Andric return Attribute::Returned; 1261f785676fSDimitry Andric case bitc::ATTR_KIND_RETURNS_TWICE: 1262f785676fSDimitry Andric return Attribute::ReturnsTwice; 1263f785676fSDimitry Andric case bitc::ATTR_KIND_S_EXT: 1264f785676fSDimitry Andric return Attribute::SExt; 1265f785676fSDimitry Andric case bitc::ATTR_KIND_STACK_ALIGNMENT: 1266f785676fSDimitry Andric return Attribute::StackAlignment; 1267f785676fSDimitry Andric case bitc::ATTR_KIND_STACK_PROTECT: 1268f785676fSDimitry Andric return Attribute::StackProtect; 1269f785676fSDimitry Andric case bitc::ATTR_KIND_STACK_PROTECT_REQ: 1270f785676fSDimitry Andric return Attribute::StackProtectReq; 1271f785676fSDimitry Andric case bitc::ATTR_KIND_STACK_PROTECT_STRONG: 1272f785676fSDimitry Andric return Attribute::StackProtectStrong; 12738f0fd8f6SDimitry Andric case bitc::ATTR_KIND_SAFESTACK: 12748f0fd8f6SDimitry Andric return Attribute::SafeStack; 1275f785676fSDimitry Andric case bitc::ATTR_KIND_STRUCT_RET: 1276f785676fSDimitry Andric return Attribute::StructRet; 1277f785676fSDimitry Andric case bitc::ATTR_KIND_SANITIZE_ADDRESS: 1278f785676fSDimitry Andric return Attribute::SanitizeAddress; 1279f785676fSDimitry Andric case bitc::ATTR_KIND_SANITIZE_THREAD: 1280f785676fSDimitry Andric return Attribute::SanitizeThread; 1281f785676fSDimitry Andric case bitc::ATTR_KIND_SANITIZE_MEMORY: 1282f785676fSDimitry Andric return Attribute::SanitizeMemory; 12833ca95b02SDimitry Andric case bitc::ATTR_KIND_SWIFT_ERROR: 12843ca95b02SDimitry Andric return Attribute::SwiftError; 12853ca95b02SDimitry Andric case bitc::ATTR_KIND_SWIFT_SELF: 12863ca95b02SDimitry Andric return Attribute::SwiftSelf; 1287f785676fSDimitry Andric case bitc::ATTR_KIND_UW_TABLE: 1288f785676fSDimitry Andric return Attribute::UWTable; 12893ca95b02SDimitry Andric case bitc::ATTR_KIND_WRITEONLY: 12903ca95b02SDimitry Andric return Attribute::WriteOnly; 1291f785676fSDimitry Andric case bitc::ATTR_KIND_Z_EXT: 1292f785676fSDimitry Andric return Attribute::ZExt; 1293f785676fSDimitry Andric } 1294f785676fSDimitry Andric } 1295f785676fSDimitry Andric 1296d88c1a5aSDimitry Andric Error BitcodeReader::parseAlignmentValue(uint64_t Exponent, 1297ff0cc061SDimitry Andric unsigned &Alignment) { 1298ff0cc061SDimitry Andric // Note: Alignment in bitcode files is incremented by 1, so that zero 1299ff0cc061SDimitry Andric // can be used for default alignment. 1300ff0cc061SDimitry Andric if (Exponent > Value::MaxAlignmentExponent + 1) 13018f0fd8f6SDimitry Andric return error("Invalid alignment value"); 1302ff0cc061SDimitry Andric Alignment = (1 << static_cast<unsigned>(Exponent)) >> 1; 1303d88c1a5aSDimitry Andric return Error::success(); 1304ff0cc061SDimitry Andric } 1305ff0cc061SDimitry Andric 1306d88c1a5aSDimitry Andric Error BitcodeReader::parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind) { 13078f0fd8f6SDimitry Andric *Kind = getAttrFromCode(Code); 1308f785676fSDimitry Andric if (*Kind == Attribute::None) 1309d88c1a5aSDimitry Andric return error("Unknown attribute kind (" + Twine(Code) + ")"); 1310d88c1a5aSDimitry Andric return Error::success(); 1311f785676fSDimitry Andric } 1312f785676fSDimitry Andric 1313d88c1a5aSDimitry Andric Error BitcodeReader::parseAttributeGroupBlock() { 1314139f7f9bSDimitry Andric if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID)) 13158f0fd8f6SDimitry Andric return error("Invalid record"); 1316139f7f9bSDimitry Andric 1317139f7f9bSDimitry Andric if (!MAttributeGroups.empty()) 13188f0fd8f6SDimitry Andric return error("Invalid multiple blocks"); 1319139f7f9bSDimitry Andric 1320139f7f9bSDimitry Andric SmallVector<uint64_t, 64> Record; 1321139f7f9bSDimitry Andric 1322139f7f9bSDimitry Andric // Read all the records. 1323d88c1a5aSDimitry Andric while (true) { 1324139f7f9bSDimitry Andric BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 1325139f7f9bSDimitry Andric 1326139f7f9bSDimitry Andric switch (Entry.Kind) { 1327139f7f9bSDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 1328139f7f9bSDimitry Andric case BitstreamEntry::Error: 13298f0fd8f6SDimitry Andric return error("Malformed block"); 1330139f7f9bSDimitry Andric case BitstreamEntry::EndBlock: 1331d88c1a5aSDimitry Andric return Error::success(); 1332139f7f9bSDimitry Andric case BitstreamEntry::Record: 1333139f7f9bSDimitry Andric // The interesting case. 1334139f7f9bSDimitry Andric break; 1335139f7f9bSDimitry Andric } 1336139f7f9bSDimitry Andric 1337139f7f9bSDimitry Andric // Read a record. 1338139f7f9bSDimitry Andric Record.clear(); 1339139f7f9bSDimitry Andric switch (Stream.readRecord(Entry.ID, Record)) { 1340139f7f9bSDimitry Andric default: // Default behavior: ignore. 1341139f7f9bSDimitry Andric break; 1342139f7f9bSDimitry Andric case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...] 1343139f7f9bSDimitry Andric if (Record.size() < 3) 13448f0fd8f6SDimitry Andric return error("Invalid record"); 1345139f7f9bSDimitry Andric 1346139f7f9bSDimitry Andric uint64_t GrpID = Record[0]; 1347139f7f9bSDimitry Andric uint64_t Idx = Record[1]; // Index of the object this attribute refers to. 1348139f7f9bSDimitry Andric 1349139f7f9bSDimitry Andric AttrBuilder B; 1350139f7f9bSDimitry Andric for (unsigned i = 2, e = Record.size(); i != e; ++i) { 1351139f7f9bSDimitry Andric if (Record[i] == 0) { // Enum attribute 1352f785676fSDimitry Andric Attribute::AttrKind Kind; 1353d88c1a5aSDimitry Andric if (Error Err = parseAttrKind(Record[++i], &Kind)) 1354d88c1a5aSDimitry Andric return Err; 1355f785676fSDimitry Andric 1356f785676fSDimitry Andric B.addAttribute(Kind); 135791bc56edSDimitry Andric } else if (Record[i] == 1) { // Integer attribute 1358f785676fSDimitry Andric Attribute::AttrKind Kind; 1359d88c1a5aSDimitry Andric if (Error Err = parseAttrKind(Record[++i], &Kind)) 1360d88c1a5aSDimitry Andric return Err; 1361f785676fSDimitry Andric if (Kind == Attribute::Alignment) 1362139f7f9bSDimitry Andric B.addAlignmentAttr(Record[++i]); 136391bc56edSDimitry Andric else if (Kind == Attribute::StackAlignment) 1364139f7f9bSDimitry Andric B.addStackAlignmentAttr(Record[++i]); 136591bc56edSDimitry Andric else if (Kind == Attribute::Dereferenceable) 136691bc56edSDimitry Andric B.addDereferenceableAttr(Record[++i]); 1367ff0cc061SDimitry Andric else if (Kind == Attribute::DereferenceableOrNull) 1368ff0cc061SDimitry Andric B.addDereferenceableOrNullAttr(Record[++i]); 13693ca95b02SDimitry Andric else if (Kind == Attribute::AllocSize) 13703ca95b02SDimitry Andric B.addAllocSizeAttrFromRawRepr(Record[++i]); 1371139f7f9bSDimitry Andric } else { // String attribute 1372139f7f9bSDimitry Andric assert((Record[i] == 3 || Record[i] == 4) && 1373139f7f9bSDimitry Andric "Invalid attribute group entry"); 1374139f7f9bSDimitry Andric bool HasValue = (Record[i++] == 4); 1375139f7f9bSDimitry Andric SmallString<64> KindStr; 1376139f7f9bSDimitry Andric SmallString<64> ValStr; 1377139f7f9bSDimitry Andric 1378139f7f9bSDimitry Andric while (Record[i] != 0 && i != e) 1379139f7f9bSDimitry Andric KindStr += Record[i++]; 1380139f7f9bSDimitry Andric assert(Record[i] == 0 && "Kind string not null terminated"); 1381139f7f9bSDimitry Andric 1382139f7f9bSDimitry Andric if (HasValue) { 1383139f7f9bSDimitry Andric // Has a value associated with it. 1384139f7f9bSDimitry Andric ++i; // Skip the '0' that terminates the "kind" string. 1385139f7f9bSDimitry Andric while (Record[i] != 0 && i != e) 1386139f7f9bSDimitry Andric ValStr += Record[i++]; 1387139f7f9bSDimitry Andric assert(Record[i] == 0 && "Value string not null terminated"); 1388139f7f9bSDimitry Andric } 1389139f7f9bSDimitry Andric 1390139f7f9bSDimitry Andric B.addAttribute(KindStr.str(), ValStr.str()); 1391139f7f9bSDimitry Andric } 1392139f7f9bSDimitry Andric } 1393139f7f9bSDimitry Andric 1394139f7f9bSDimitry Andric MAttributeGroups[GrpID] = AttributeSet::get(Context, Idx, B); 1395139f7f9bSDimitry Andric break; 1396139f7f9bSDimitry Andric } 1397f22ef01cSRoman Divacky } 1398f22ef01cSRoman Divacky } 1399f22ef01cSRoman Divacky } 1400f22ef01cSRoman Divacky 1401d88c1a5aSDimitry Andric Error BitcodeReader::parseTypeTable() { 140217a519f9SDimitry Andric if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW)) 14038f0fd8f6SDimitry Andric return error("Invalid record"); 1404f22ef01cSRoman Divacky 14058f0fd8f6SDimitry Andric return parseTypeTableBody(); 140617a519f9SDimitry Andric } 140717a519f9SDimitry Andric 1408d88c1a5aSDimitry Andric Error BitcodeReader::parseTypeTableBody() { 1409f22ef01cSRoman Divacky if (!TypeList.empty()) 14108f0fd8f6SDimitry Andric return error("Invalid multiple blocks"); 1411f22ef01cSRoman Divacky 1412f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 1413f22ef01cSRoman Divacky unsigned NumRecords = 0; 1414f22ef01cSRoman Divacky 141517a519f9SDimitry Andric SmallString<64> TypeName; 141617a519f9SDimitry Andric 1417f22ef01cSRoman Divacky // Read all the records for this type table. 1418d88c1a5aSDimitry Andric while (true) { 1419139f7f9bSDimitry Andric BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 1420139f7f9bSDimitry Andric 1421139f7f9bSDimitry Andric switch (Entry.Kind) { 1422139f7f9bSDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 1423139f7f9bSDimitry Andric case BitstreamEntry::Error: 14248f0fd8f6SDimitry Andric return error("Malformed block"); 1425139f7f9bSDimitry Andric case BitstreamEntry::EndBlock: 1426f22ef01cSRoman Divacky if (NumRecords != TypeList.size()) 14278f0fd8f6SDimitry Andric return error("Malformed block"); 1428d88c1a5aSDimitry Andric return Error::success(); 1429139f7f9bSDimitry Andric case BitstreamEntry::Record: 1430139f7f9bSDimitry Andric // The interesting case. 1431139f7f9bSDimitry Andric break; 1432f22ef01cSRoman Divacky } 1433f22ef01cSRoman Divacky 1434f22ef01cSRoman Divacky // Read a record. 1435f22ef01cSRoman Divacky Record.clear(); 143691bc56edSDimitry Andric Type *ResultTy = nullptr; 1437139f7f9bSDimitry Andric switch (Stream.readRecord(Entry.ID, Record)) { 1438f785676fSDimitry Andric default: 14398f0fd8f6SDimitry Andric return error("Invalid value"); 1440f22ef01cSRoman Divacky case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries] 1441f22ef01cSRoman Divacky // TYPE_CODE_NUMENTRY contains a count of the number of types in the 1442f22ef01cSRoman Divacky // type list. This allows us to reserve space. 1443f22ef01cSRoman Divacky if (Record.size() < 1) 14448f0fd8f6SDimitry Andric return error("Invalid record"); 144517a519f9SDimitry Andric TypeList.resize(Record[0]); 1446f22ef01cSRoman Divacky continue; 1447f22ef01cSRoman Divacky case bitc::TYPE_CODE_VOID: // VOID 1448f22ef01cSRoman Divacky ResultTy = Type::getVoidTy(Context); 1449f22ef01cSRoman Divacky break; 1450dff0c46cSDimitry Andric case bitc::TYPE_CODE_HALF: // HALF 1451dff0c46cSDimitry Andric ResultTy = Type::getHalfTy(Context); 1452dff0c46cSDimitry Andric break; 1453f22ef01cSRoman Divacky case bitc::TYPE_CODE_FLOAT: // FLOAT 1454f22ef01cSRoman Divacky ResultTy = Type::getFloatTy(Context); 1455f22ef01cSRoman Divacky break; 1456f22ef01cSRoman Divacky case bitc::TYPE_CODE_DOUBLE: // DOUBLE 1457f22ef01cSRoman Divacky ResultTy = Type::getDoubleTy(Context); 1458f22ef01cSRoman Divacky break; 1459f22ef01cSRoman Divacky case bitc::TYPE_CODE_X86_FP80: // X86_FP80 1460f22ef01cSRoman Divacky ResultTy = Type::getX86_FP80Ty(Context); 1461f22ef01cSRoman Divacky break; 1462f22ef01cSRoman Divacky case bitc::TYPE_CODE_FP128: // FP128 1463f22ef01cSRoman Divacky ResultTy = Type::getFP128Ty(Context); 1464f22ef01cSRoman Divacky break; 1465f22ef01cSRoman Divacky case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128 1466f22ef01cSRoman Divacky ResultTy = Type::getPPC_FP128Ty(Context); 1467f22ef01cSRoman Divacky break; 1468f22ef01cSRoman Divacky case bitc::TYPE_CODE_LABEL: // LABEL 1469f22ef01cSRoman Divacky ResultTy = Type::getLabelTy(Context); 1470f22ef01cSRoman Divacky break; 1471f22ef01cSRoman Divacky case bitc::TYPE_CODE_METADATA: // METADATA 1472f22ef01cSRoman Divacky ResultTy = Type::getMetadataTy(Context); 1473f22ef01cSRoman Divacky break; 14742754fe60SDimitry Andric case bitc::TYPE_CODE_X86_MMX: // X86_MMX 14752754fe60SDimitry Andric ResultTy = Type::getX86_MMXTy(Context); 14762754fe60SDimitry Andric break; 14777d523365SDimitry Andric case bitc::TYPE_CODE_TOKEN: // TOKEN 14787d523365SDimitry Andric ResultTy = Type::getTokenTy(Context); 14797d523365SDimitry Andric break; 1480ff0cc061SDimitry Andric case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width] 1481f22ef01cSRoman Divacky if (Record.size() < 1) 14828f0fd8f6SDimitry Andric return error("Invalid record"); 1483f22ef01cSRoman Divacky 1484ff0cc061SDimitry Andric uint64_t NumBits = Record[0]; 1485ff0cc061SDimitry Andric if (NumBits < IntegerType::MIN_INT_BITS || 1486ff0cc061SDimitry Andric NumBits > IntegerType::MAX_INT_BITS) 14878f0fd8f6SDimitry Andric return error("Bitwidth for integer type out of range"); 1488ff0cc061SDimitry Andric ResultTy = IntegerType::get(Context, NumBits); 1489f22ef01cSRoman Divacky break; 1490ff0cc061SDimitry Andric } 1491f22ef01cSRoman Divacky case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or 1492f22ef01cSRoman Divacky // [pointee type, address space] 1493f22ef01cSRoman Divacky if (Record.size() < 1) 14948f0fd8f6SDimitry Andric return error("Invalid record"); 1495f22ef01cSRoman Divacky unsigned AddressSpace = 0; 1496f22ef01cSRoman Divacky if (Record.size() == 2) 1497f22ef01cSRoman Divacky AddressSpace = Record[1]; 149817a519f9SDimitry Andric ResultTy = getTypeByID(Record[0]); 1499ff0cc061SDimitry Andric if (!ResultTy || 1500ff0cc061SDimitry Andric !PointerType::isValidElementType(ResultTy)) 15018f0fd8f6SDimitry Andric return error("Invalid type"); 150217a519f9SDimitry Andric ResultTy = PointerType::get(ResultTy, AddressSpace); 1503f22ef01cSRoman Divacky break; 1504f22ef01cSRoman Divacky } 1505dff0c46cSDimitry Andric case bitc::TYPE_CODE_FUNCTION_OLD: { 15067ae0e2c9SDimitry Andric // FIXME: attrid is dead, remove it in LLVM 4.0 1507f22ef01cSRoman Divacky // FUNCTION: [vararg, attrid, retty, paramty x N] 1508f22ef01cSRoman Divacky if (Record.size() < 3) 15098f0fd8f6SDimitry Andric return error("Invalid record"); 1510dff0c46cSDimitry Andric SmallVector<Type*, 8> ArgTys; 151117a519f9SDimitry Andric for (unsigned i = 3, e = Record.size(); i != e; ++i) { 151217a519f9SDimitry Andric if (Type *T = getTypeByID(Record[i])) 151317a519f9SDimitry Andric ArgTys.push_back(T); 151417a519f9SDimitry Andric else 1515f22ef01cSRoman Divacky break; 1516f22ef01cSRoman Divacky } 151717a519f9SDimitry Andric 151817a519f9SDimitry Andric ResultTy = getTypeByID(Record[2]); 151991bc56edSDimitry Andric if (!ResultTy || ArgTys.size() < Record.size()-3) 15208f0fd8f6SDimitry Andric return error("Invalid type"); 152117a519f9SDimitry Andric 152217a519f9SDimitry Andric ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); 152317a519f9SDimitry Andric break; 152417a519f9SDimitry Andric } 1525dff0c46cSDimitry Andric case bitc::TYPE_CODE_FUNCTION: { 1526dff0c46cSDimitry Andric // FUNCTION: [vararg, retty, paramty x N] 1527dff0c46cSDimitry Andric if (Record.size() < 2) 15288f0fd8f6SDimitry Andric return error("Invalid record"); 1529dff0c46cSDimitry Andric SmallVector<Type*, 8> ArgTys; 1530dff0c46cSDimitry Andric for (unsigned i = 2, e = Record.size(); i != e; ++i) { 1531ff0cc061SDimitry Andric if (Type *T = getTypeByID(Record[i])) { 1532ff0cc061SDimitry Andric if (!FunctionType::isValidArgumentType(T)) 15338f0fd8f6SDimitry Andric return error("Invalid function argument type"); 1534dff0c46cSDimitry Andric ArgTys.push_back(T); 1535ff0cc061SDimitry Andric } 1536dff0c46cSDimitry Andric else 1537dff0c46cSDimitry Andric break; 1538dff0c46cSDimitry Andric } 1539dff0c46cSDimitry Andric 1540dff0c46cSDimitry Andric ResultTy = getTypeByID(Record[1]); 154191bc56edSDimitry Andric if (!ResultTy || ArgTys.size() < Record.size()-2) 15428f0fd8f6SDimitry Andric return error("Invalid type"); 1543dff0c46cSDimitry Andric 1544dff0c46cSDimitry Andric ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); 1545dff0c46cSDimitry Andric break; 1546dff0c46cSDimitry Andric } 154717a519f9SDimitry Andric case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N] 1548f22ef01cSRoman Divacky if (Record.size() < 1) 15498f0fd8f6SDimitry Andric return error("Invalid record"); 1550dff0c46cSDimitry Andric SmallVector<Type*, 8> EltTys; 155117a519f9SDimitry Andric for (unsigned i = 1, e = Record.size(); i != e; ++i) { 155217a519f9SDimitry Andric if (Type *T = getTypeByID(Record[i])) 155317a519f9SDimitry Andric EltTys.push_back(T); 155417a519f9SDimitry Andric else 155517a519f9SDimitry Andric break; 155617a519f9SDimitry Andric } 155717a519f9SDimitry Andric if (EltTys.size() != Record.size()-1) 15588f0fd8f6SDimitry Andric return error("Invalid type"); 1559f22ef01cSRoman Divacky ResultTy = StructType::get(Context, EltTys, Record[0]); 1560f22ef01cSRoman Divacky break; 1561f22ef01cSRoman Divacky } 156217a519f9SDimitry Andric case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N] 15638f0fd8f6SDimitry Andric if (convertToString(Record, 0, TypeName)) 15648f0fd8f6SDimitry Andric return error("Invalid record"); 156517a519f9SDimitry Andric continue; 156617a519f9SDimitry Andric 156717a519f9SDimitry Andric case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N] 156817a519f9SDimitry Andric if (Record.size() < 1) 15698f0fd8f6SDimitry Andric return error("Invalid record"); 157017a519f9SDimitry Andric 157117a519f9SDimitry Andric if (NumRecords >= TypeList.size()) 15728f0fd8f6SDimitry Andric return error("Invalid TYPE table"); 157317a519f9SDimitry Andric 157417a519f9SDimitry Andric // Check to see if this was forward referenced, if so fill in the temp. 157517a519f9SDimitry Andric StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); 157617a519f9SDimitry Andric if (Res) { 157717a519f9SDimitry Andric Res->setName(TypeName); 157891bc56edSDimitry Andric TypeList[NumRecords] = nullptr; 157917a519f9SDimitry Andric } else // Otherwise, create a new struct. 158039d628a0SDimitry Andric Res = createIdentifiedStructType(Context, TypeName); 158117a519f9SDimitry Andric TypeName.clear(); 158217a519f9SDimitry Andric 158317a519f9SDimitry Andric SmallVector<Type*, 8> EltTys; 158417a519f9SDimitry Andric for (unsigned i = 1, e = Record.size(); i != e; ++i) { 158517a519f9SDimitry Andric if (Type *T = getTypeByID(Record[i])) 158617a519f9SDimitry Andric EltTys.push_back(T); 158717a519f9SDimitry Andric else 158817a519f9SDimitry Andric break; 158917a519f9SDimitry Andric } 159017a519f9SDimitry Andric if (EltTys.size() != Record.size()-1) 15918f0fd8f6SDimitry Andric return error("Invalid record"); 159217a519f9SDimitry Andric Res->setBody(EltTys, Record[0]); 159317a519f9SDimitry Andric ResultTy = Res; 159417a519f9SDimitry Andric break; 159517a519f9SDimitry Andric } 159617a519f9SDimitry Andric case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: [] 159717a519f9SDimitry Andric if (Record.size() != 1) 15988f0fd8f6SDimitry Andric return error("Invalid record"); 159917a519f9SDimitry Andric 160017a519f9SDimitry Andric if (NumRecords >= TypeList.size()) 16018f0fd8f6SDimitry Andric return error("Invalid TYPE table"); 160217a519f9SDimitry Andric 160317a519f9SDimitry Andric // Check to see if this was forward referenced, if so fill in the temp. 160417a519f9SDimitry Andric StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); 160517a519f9SDimitry Andric if (Res) { 160617a519f9SDimitry Andric Res->setName(TypeName); 160791bc56edSDimitry Andric TypeList[NumRecords] = nullptr; 160817a519f9SDimitry Andric } else // Otherwise, create a new struct with no body. 160939d628a0SDimitry Andric Res = createIdentifiedStructType(Context, TypeName); 161017a519f9SDimitry Andric TypeName.clear(); 161117a519f9SDimitry Andric ResultTy = Res; 161217a519f9SDimitry Andric break; 161317a519f9SDimitry Andric } 1614f22ef01cSRoman Divacky case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty] 1615f22ef01cSRoman Divacky if (Record.size() < 2) 16168f0fd8f6SDimitry Andric return error("Invalid record"); 1617ff0cc061SDimitry Andric ResultTy = getTypeByID(Record[1]); 1618ff0cc061SDimitry Andric if (!ResultTy || !ArrayType::isValidElementType(ResultTy)) 16198f0fd8f6SDimitry Andric return error("Invalid type"); 1620ff0cc061SDimitry Andric ResultTy = ArrayType::get(ResultTy, Record[0]); 1621f22ef01cSRoman Divacky break; 1622f22ef01cSRoman Divacky case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] 1623f22ef01cSRoman Divacky if (Record.size() < 2) 16248f0fd8f6SDimitry Andric return error("Invalid record"); 162597bc6c73SDimitry Andric if (Record[0] == 0) 16268f0fd8f6SDimitry Andric return error("Invalid vector length"); 1627ff0cc061SDimitry Andric ResultTy = getTypeByID(Record[1]); 1628ff0cc061SDimitry Andric if (!ResultTy || !StructType::isValidElementType(ResultTy)) 16298f0fd8f6SDimitry Andric return error("Invalid type"); 1630ff0cc061SDimitry Andric ResultTy = VectorType::get(ResultTy, Record[0]); 1631f22ef01cSRoman Divacky break; 1632f22ef01cSRoman Divacky } 1633f22ef01cSRoman Divacky 163417a519f9SDimitry Andric if (NumRecords >= TypeList.size()) 16358f0fd8f6SDimitry Andric return error("Invalid TYPE table"); 1636ff0cc061SDimitry Andric if (TypeList[NumRecords]) 16378f0fd8f6SDimitry Andric return error( 1638ff0cc061SDimitry Andric "Invalid TYPE table: Only named structs can be forward referenced"); 163917a519f9SDimitry Andric assert(ResultTy && "Didn't read a type?"); 164017a519f9SDimitry Andric TypeList[NumRecords++] = ResultTy; 1641f22ef01cSRoman Divacky } 1642f22ef01cSRoman Divacky } 164317a519f9SDimitry Andric 1644d88c1a5aSDimitry Andric Error BitcodeReader::parseOperandBundleTags() { 16457d523365SDimitry Andric if (Stream.EnterSubBlock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID)) 16467d523365SDimitry Andric return error("Invalid record"); 16477d523365SDimitry Andric 16487d523365SDimitry Andric if (!BundleTags.empty()) 16497d523365SDimitry Andric return error("Invalid multiple blocks"); 16507d523365SDimitry Andric 16517d523365SDimitry Andric SmallVector<uint64_t, 64> Record; 16527d523365SDimitry Andric 1653d88c1a5aSDimitry Andric while (true) { 16547d523365SDimitry Andric BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 16557d523365SDimitry Andric 16567d523365SDimitry Andric switch (Entry.Kind) { 16577d523365SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 16587d523365SDimitry Andric case BitstreamEntry::Error: 16597d523365SDimitry Andric return error("Malformed block"); 16607d523365SDimitry Andric case BitstreamEntry::EndBlock: 1661d88c1a5aSDimitry Andric return Error::success(); 16627d523365SDimitry Andric case BitstreamEntry::Record: 16637d523365SDimitry Andric // The interesting case. 16647d523365SDimitry Andric break; 16657d523365SDimitry Andric } 16667d523365SDimitry Andric 16677d523365SDimitry Andric // Tags are implicitly mapped to integers by their order. 16687d523365SDimitry Andric 16697d523365SDimitry Andric if (Stream.readRecord(Entry.ID, Record) != bitc::OPERAND_BUNDLE_TAG) 16707d523365SDimitry Andric return error("Invalid record"); 16717d523365SDimitry Andric 16727d523365SDimitry Andric // OPERAND_BUNDLE_TAG: [strchr x N] 16737d523365SDimitry Andric BundleTags.emplace_back(); 16747d523365SDimitry Andric if (convertToString(Record, 0, BundleTags.back())) 16757d523365SDimitry Andric return error("Invalid record"); 16767d523365SDimitry Andric Record.clear(); 16777d523365SDimitry Andric } 16787d523365SDimitry Andric } 16797d523365SDimitry Andric 16807d523365SDimitry Andric /// Associate a value with its name from the given index in the provided record. 1681d88c1a5aSDimitry Andric Expected<Value *> BitcodeReader::recordValue(SmallVectorImpl<uint64_t> &Record, 16827d523365SDimitry Andric unsigned NameIndex, Triple &TT) { 16837d523365SDimitry Andric SmallString<128> ValueName; 16847d523365SDimitry Andric if (convertToString(Record, NameIndex, ValueName)) 16857d523365SDimitry Andric return error("Invalid record"); 16867d523365SDimitry Andric unsigned ValueID = Record[0]; 16877d523365SDimitry Andric if (ValueID >= ValueList.size() || !ValueList[ValueID]) 16887d523365SDimitry Andric return error("Invalid record"); 16897d523365SDimitry Andric Value *V = ValueList[ValueID]; 16907d523365SDimitry Andric 16917d523365SDimitry Andric StringRef NameStr(ValueName.data(), ValueName.size()); 16927d523365SDimitry Andric if (NameStr.find_first_of(0) != StringRef::npos) 16937d523365SDimitry Andric return error("Invalid value name"); 16947d523365SDimitry Andric V->setName(NameStr); 16957d523365SDimitry Andric auto *GO = dyn_cast<GlobalObject>(V); 16967d523365SDimitry Andric if (GO) { 16977d523365SDimitry Andric if (GO->getComdat() == reinterpret_cast<Comdat *>(1)) { 16987d523365SDimitry Andric if (TT.isOSBinFormatMachO()) 16997d523365SDimitry Andric GO->setComdat(nullptr); 17007d523365SDimitry Andric else 17017d523365SDimitry Andric GO->setComdat(TheModule->getOrInsertComdat(V->getName())); 17027d523365SDimitry Andric } 17037d523365SDimitry Andric } 17047d523365SDimitry Andric return V; 17057d523365SDimitry Andric } 17067d523365SDimitry Andric 17073ca95b02SDimitry Andric /// Helper to note and return the current location, and jump to the given 17083ca95b02SDimitry Andric /// offset. 17093ca95b02SDimitry Andric static uint64_t jumpToValueSymbolTable(uint64_t Offset, 17103ca95b02SDimitry Andric BitstreamCursor &Stream) { 17117d523365SDimitry Andric // Save the current parsing location so we can jump back at the end 17127d523365SDimitry Andric // of the VST read. 17133ca95b02SDimitry Andric uint64_t CurrentBit = Stream.GetCurrentBitNo(); 17147d523365SDimitry Andric Stream.JumpToBit(Offset * 32); 17157d523365SDimitry Andric #ifndef NDEBUG 17167d523365SDimitry Andric // Do some checking if we are in debug mode. 17177d523365SDimitry Andric BitstreamEntry Entry = Stream.advance(); 17187d523365SDimitry Andric assert(Entry.Kind == BitstreamEntry::SubBlock); 17197d523365SDimitry Andric assert(Entry.ID == bitc::VALUE_SYMTAB_BLOCK_ID); 17207d523365SDimitry Andric #else 17217d523365SDimitry Andric // In NDEBUG mode ignore the output so we don't get an unused variable 17227d523365SDimitry Andric // warning. 17237d523365SDimitry Andric Stream.advance(); 17247d523365SDimitry Andric #endif 17253ca95b02SDimitry Andric return CurrentBit; 17267d523365SDimitry Andric } 17277d523365SDimitry Andric 17283ca95b02SDimitry Andric /// Parse the value symbol table at either the current parsing location or 17293ca95b02SDimitry Andric /// at the given bit offset if provided. 1730d88c1a5aSDimitry Andric Error BitcodeReader::parseValueSymbolTable(uint64_t Offset) { 17313ca95b02SDimitry Andric uint64_t CurrentBit; 17323ca95b02SDimitry Andric // Pass in the Offset to distinguish between calling for the module-level 17333ca95b02SDimitry Andric // VST (where we want to jump to the VST offset) and the function-level 17343ca95b02SDimitry Andric // VST (where we don't). 17353ca95b02SDimitry Andric if (Offset > 0) 17363ca95b02SDimitry Andric CurrentBit = jumpToValueSymbolTable(Offset, Stream); 17373ca95b02SDimitry Andric 17387d523365SDimitry Andric // Compute the delta between the bitcode indices in the VST (the word offset 17397d523365SDimitry Andric // to the word-aligned ENTER_SUBBLOCK for the function block, and that 17407d523365SDimitry Andric // expected by the lazy reader. The reader's EnterSubBlock expects to have 17417d523365SDimitry Andric // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID 17427d523365SDimitry Andric // (size BlockIDWidth). Note that we access the stream's AbbrevID width here 17437d523365SDimitry Andric // just before entering the VST subblock because: 1) the EnterSubBlock 17447d523365SDimitry Andric // changes the AbbrevID width; 2) the VST block is nested within the same 17457d523365SDimitry Andric // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same 17467d523365SDimitry Andric // AbbrevID width before calling EnterSubBlock; and 3) when we want to 17477d523365SDimitry Andric // jump to the FUNCTION_BLOCK using this offset later, we don't want 17487d523365SDimitry Andric // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK. 17497d523365SDimitry Andric unsigned FuncBitcodeOffsetDelta = 17507d523365SDimitry Andric Stream.getAbbrevIDWidth() + bitc::BlockIDWidth; 17517d523365SDimitry Andric 1752f22ef01cSRoman Divacky if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) 17538f0fd8f6SDimitry Andric return error("Invalid record"); 1754f22ef01cSRoman Divacky 1755f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 1756f22ef01cSRoman Divacky 1757ff0cc061SDimitry Andric Triple TT(TheModule->getTargetTriple()); 1758ff0cc061SDimitry Andric 1759f22ef01cSRoman Divacky // Read all the records for this value table. 1760f22ef01cSRoman Divacky SmallString<128> ValueName; 1761d88c1a5aSDimitry Andric 1762d88c1a5aSDimitry Andric while (true) { 1763139f7f9bSDimitry Andric BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 1764f22ef01cSRoman Divacky 1765139f7f9bSDimitry Andric switch (Entry.Kind) { 1766139f7f9bSDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 1767139f7f9bSDimitry Andric case BitstreamEntry::Error: 17688f0fd8f6SDimitry Andric return error("Malformed block"); 1769139f7f9bSDimitry Andric case BitstreamEntry::EndBlock: 17707d523365SDimitry Andric if (Offset > 0) 17717d523365SDimitry Andric Stream.JumpToBit(CurrentBit); 1772d88c1a5aSDimitry Andric return Error::success(); 1773139f7f9bSDimitry Andric case BitstreamEntry::Record: 1774139f7f9bSDimitry Andric // The interesting case. 1775139f7f9bSDimitry Andric break; 1776f22ef01cSRoman Divacky } 1777f22ef01cSRoman Divacky 1778f22ef01cSRoman Divacky // Read a record. 1779f22ef01cSRoman Divacky Record.clear(); 1780139f7f9bSDimitry Andric switch (Stream.readRecord(Entry.ID, Record)) { 1781f22ef01cSRoman Divacky default: // Default behavior: unknown type. 1782f22ef01cSRoman Divacky break; 17833ca95b02SDimitry Andric case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N] 1784d88c1a5aSDimitry Andric Expected<Value *> ValOrErr = recordValue(Record, 1, TT); 1785d88c1a5aSDimitry Andric if (Error Err = ValOrErr.takeError()) 1786d88c1a5aSDimitry Andric return Err; 17877d523365SDimitry Andric ValOrErr.get(); 17887d523365SDimitry Andric break; 17897d523365SDimitry Andric } 17907d523365SDimitry Andric case bitc::VST_CODE_FNENTRY: { 17913ca95b02SDimitry Andric // VST_CODE_FNENTRY: [valueid, offset, namechar x N] 1792d88c1a5aSDimitry Andric Expected<Value *> ValOrErr = recordValue(Record, 2, TT); 1793d88c1a5aSDimitry Andric if (Error Err = ValOrErr.takeError()) 1794d88c1a5aSDimitry Andric return Err; 17957d523365SDimitry Andric Value *V = ValOrErr.get(); 1796f22ef01cSRoman Divacky 17977d523365SDimitry Andric auto *GO = dyn_cast<GlobalObject>(V); 17987d523365SDimitry Andric if (!GO) { 17997d523365SDimitry Andric // If this is an alias, need to get the actual Function object 18007d523365SDimitry Andric // it aliases, in order to set up the DeferredFunctionInfo entry below. 18017d523365SDimitry Andric auto *GA = dyn_cast<GlobalAlias>(V); 18027d523365SDimitry Andric if (GA) 18037d523365SDimitry Andric GO = GA->getBaseObject(); 18047d523365SDimitry Andric assert(GO); 1805ff0cc061SDimitry Andric } 18067d523365SDimitry Andric 1807d88c1a5aSDimitry Andric // Note that we subtract 1 here because the offset is relative to one word 1808d88c1a5aSDimitry Andric // before the start of the identification or module block, which was 1809d88c1a5aSDimitry Andric // historically always the start of the regular bitcode header. 1810d88c1a5aSDimitry Andric uint64_t FuncWordOffset = Record[1] - 1; 18117d523365SDimitry Andric Function *F = dyn_cast<Function>(GO); 18127d523365SDimitry Andric assert(F); 18137d523365SDimitry Andric uint64_t FuncBitOffset = FuncWordOffset * 32; 18147d523365SDimitry Andric DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta; 18157d523365SDimitry Andric // Set the LastFunctionBlockBit to point to the last function block. 18167d523365SDimitry Andric // Later when parsing is resumed after function materialization, 18177d523365SDimitry Andric // we can simply skip that last function block. 18187d523365SDimitry Andric if (FuncBitOffset > LastFunctionBlockBit) 18197d523365SDimitry Andric LastFunctionBlockBit = FuncBitOffset; 1820f22ef01cSRoman Divacky break; 1821f22ef01cSRoman Divacky } 1822f22ef01cSRoman Divacky case bitc::VST_CODE_BBENTRY: { 18238f0fd8f6SDimitry Andric if (convertToString(Record, 1, ValueName)) 18248f0fd8f6SDimitry Andric return error("Invalid record"); 1825f22ef01cSRoman Divacky BasicBlock *BB = getBasicBlock(Record[0]); 182691bc56edSDimitry Andric if (!BB) 18278f0fd8f6SDimitry Andric return error("Invalid record"); 1828f22ef01cSRoman Divacky 1829f22ef01cSRoman Divacky BB->setName(StringRef(ValueName.data(), ValueName.size())); 1830f22ef01cSRoman Divacky ValueName.clear(); 1831f22ef01cSRoman Divacky break; 1832f22ef01cSRoman Divacky } 1833f22ef01cSRoman Divacky } 1834f22ef01cSRoman Divacky } 1835f22ef01cSRoman Divacky } 1836f22ef01cSRoman Divacky 18378f0fd8f6SDimitry Andric /// Decode a signed value stored with the sign bit in the LSB for dense VBR 18388f0fd8f6SDimitry Andric /// encoding. 18393861d79fSDimitry Andric uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) { 1840f22ef01cSRoman Divacky if ((V & 1) == 0) 1841f22ef01cSRoman Divacky return V >> 1; 1842f22ef01cSRoman Divacky if (V != 1) 1843f22ef01cSRoman Divacky return -(V >> 1); 1844f22ef01cSRoman Divacky // There is no such thing as -0 with integers. "-0" really means MININT. 1845f22ef01cSRoman Divacky return 1ULL << 63; 1846f22ef01cSRoman Divacky } 1847f22ef01cSRoman Divacky 18488f0fd8f6SDimitry Andric /// Resolve all of the initializers for global values and aliases that we can. 1849d88c1a5aSDimitry Andric Error BitcodeReader::resolveGlobalAndIndirectSymbolInits() { 1850f22ef01cSRoman Divacky std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist; 18513ca95b02SDimitry Andric std::vector<std::pair<GlobalIndirectSymbol*, unsigned> > 18523ca95b02SDimitry Andric IndirectSymbolInitWorklist; 1853f785676fSDimitry Andric std::vector<std::pair<Function*, unsigned> > FunctionPrefixWorklist; 185439d628a0SDimitry Andric std::vector<std::pair<Function*, unsigned> > FunctionPrologueWorklist; 18558f0fd8f6SDimitry Andric std::vector<std::pair<Function*, unsigned> > FunctionPersonalityFnWorklist; 1856f22ef01cSRoman Divacky 1857f22ef01cSRoman Divacky GlobalInitWorklist.swap(GlobalInits); 18583ca95b02SDimitry Andric IndirectSymbolInitWorklist.swap(IndirectSymbolInits); 1859f785676fSDimitry Andric FunctionPrefixWorklist.swap(FunctionPrefixes); 186039d628a0SDimitry Andric FunctionPrologueWorklist.swap(FunctionPrologues); 18618f0fd8f6SDimitry Andric FunctionPersonalityFnWorklist.swap(FunctionPersonalityFns); 1862f22ef01cSRoman Divacky 1863f22ef01cSRoman Divacky while (!GlobalInitWorklist.empty()) { 1864f22ef01cSRoman Divacky unsigned ValID = GlobalInitWorklist.back().second; 1865f22ef01cSRoman Divacky if (ValID >= ValueList.size()) { 1866f22ef01cSRoman Divacky // Not ready to resolve this yet, it requires something later in the file. 1867f22ef01cSRoman Divacky GlobalInits.push_back(GlobalInitWorklist.back()); 1868f22ef01cSRoman Divacky } else { 186991bc56edSDimitry Andric if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) 1870f22ef01cSRoman Divacky GlobalInitWorklist.back().first->setInitializer(C); 1871f22ef01cSRoman Divacky else 18728f0fd8f6SDimitry Andric return error("Expected a constant"); 1873f22ef01cSRoman Divacky } 1874f22ef01cSRoman Divacky GlobalInitWorklist.pop_back(); 1875f22ef01cSRoman Divacky } 1876f22ef01cSRoman Divacky 18773ca95b02SDimitry Andric while (!IndirectSymbolInitWorklist.empty()) { 18783ca95b02SDimitry Andric unsigned ValID = IndirectSymbolInitWorklist.back().second; 1879f22ef01cSRoman Divacky if (ValID >= ValueList.size()) { 18803ca95b02SDimitry Andric IndirectSymbolInits.push_back(IndirectSymbolInitWorklist.back()); 1881f22ef01cSRoman Divacky } else { 188297bc6c73SDimitry Andric Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]); 188397bc6c73SDimitry Andric if (!C) 18848f0fd8f6SDimitry Andric return error("Expected a constant"); 18853ca95b02SDimitry Andric GlobalIndirectSymbol *GIS = IndirectSymbolInitWorklist.back().first; 18863ca95b02SDimitry Andric if (isa<GlobalAlias>(GIS) && C->getType() != GIS->getType()) 18878f0fd8f6SDimitry Andric return error("Alias and aliasee types don't match"); 18883ca95b02SDimitry Andric GIS->setIndirectSymbol(C); 1889f22ef01cSRoman Divacky } 18903ca95b02SDimitry Andric IndirectSymbolInitWorklist.pop_back(); 1891f22ef01cSRoman Divacky } 1892f785676fSDimitry Andric 1893f785676fSDimitry Andric while (!FunctionPrefixWorklist.empty()) { 1894f785676fSDimitry Andric unsigned ValID = FunctionPrefixWorklist.back().second; 1895f785676fSDimitry Andric if (ValID >= ValueList.size()) { 1896f785676fSDimitry Andric FunctionPrefixes.push_back(FunctionPrefixWorklist.back()); 1897f785676fSDimitry Andric } else { 189891bc56edSDimitry Andric if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) 1899f785676fSDimitry Andric FunctionPrefixWorklist.back().first->setPrefixData(C); 1900f785676fSDimitry Andric else 19018f0fd8f6SDimitry Andric return error("Expected a constant"); 1902f785676fSDimitry Andric } 1903f785676fSDimitry Andric FunctionPrefixWorklist.pop_back(); 1904f785676fSDimitry Andric } 1905f785676fSDimitry Andric 190639d628a0SDimitry Andric while (!FunctionPrologueWorklist.empty()) { 190739d628a0SDimitry Andric unsigned ValID = FunctionPrologueWorklist.back().second; 190839d628a0SDimitry Andric if (ValID >= ValueList.size()) { 190939d628a0SDimitry Andric FunctionPrologues.push_back(FunctionPrologueWorklist.back()); 191039d628a0SDimitry Andric } else { 191139d628a0SDimitry Andric if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) 191239d628a0SDimitry Andric FunctionPrologueWorklist.back().first->setPrologueData(C); 191339d628a0SDimitry Andric else 19148f0fd8f6SDimitry Andric return error("Expected a constant"); 191539d628a0SDimitry Andric } 191639d628a0SDimitry Andric FunctionPrologueWorklist.pop_back(); 191739d628a0SDimitry Andric } 191839d628a0SDimitry Andric 19198f0fd8f6SDimitry Andric while (!FunctionPersonalityFnWorklist.empty()) { 19208f0fd8f6SDimitry Andric unsigned ValID = FunctionPersonalityFnWorklist.back().second; 19218f0fd8f6SDimitry Andric if (ValID >= ValueList.size()) { 19228f0fd8f6SDimitry Andric FunctionPersonalityFns.push_back(FunctionPersonalityFnWorklist.back()); 19238f0fd8f6SDimitry Andric } else { 19248f0fd8f6SDimitry Andric if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) 19258f0fd8f6SDimitry Andric FunctionPersonalityFnWorklist.back().first->setPersonalityFn(C); 19268f0fd8f6SDimitry Andric else 19278f0fd8f6SDimitry Andric return error("Expected a constant"); 19288f0fd8f6SDimitry Andric } 19298f0fd8f6SDimitry Andric FunctionPersonalityFnWorklist.pop_back(); 19308f0fd8f6SDimitry Andric } 19318f0fd8f6SDimitry Andric 1932d88c1a5aSDimitry Andric return Error::success(); 1933f22ef01cSRoman Divacky } 1934f22ef01cSRoman Divacky 19358f0fd8f6SDimitry Andric static APInt readWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) { 19367ae0e2c9SDimitry Andric SmallVector<uint64_t, 8> Words(Vals.size()); 1937d88c1a5aSDimitry Andric transform(Vals, Words.begin(), 19383861d79fSDimitry Andric BitcodeReader::decodeSignRotatedValue); 19397ae0e2c9SDimitry Andric 19407ae0e2c9SDimitry Andric return APInt(TypeBits, Words); 19417ae0e2c9SDimitry Andric } 19427ae0e2c9SDimitry Andric 1943d88c1a5aSDimitry Andric Error BitcodeReader::parseConstants() { 1944f22ef01cSRoman Divacky if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID)) 19458f0fd8f6SDimitry Andric return error("Invalid record"); 1946f22ef01cSRoman Divacky 1947f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 1948f22ef01cSRoman Divacky 1949f22ef01cSRoman Divacky // Read all the records for this value table. 19506122f3e6SDimitry Andric Type *CurTy = Type::getInt32Ty(Context); 1951f22ef01cSRoman Divacky unsigned NextCstNo = ValueList.size(); 1952d88c1a5aSDimitry Andric 1953d88c1a5aSDimitry Andric while (true) { 1954139f7f9bSDimitry Andric BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 1955139f7f9bSDimitry Andric 1956139f7f9bSDimitry Andric switch (Entry.Kind) { 1957139f7f9bSDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 1958139f7f9bSDimitry Andric case BitstreamEntry::Error: 19598f0fd8f6SDimitry Andric return error("Malformed block"); 1960139f7f9bSDimitry Andric case BitstreamEntry::EndBlock: 1961139f7f9bSDimitry Andric if (NextCstNo != ValueList.size()) 19623ca95b02SDimitry Andric return error("Invalid constant reference"); 1963139f7f9bSDimitry Andric 1964139f7f9bSDimitry Andric // Once all the constants have been read, go through and resolve forward 1965139f7f9bSDimitry Andric // references. 19668f0fd8f6SDimitry Andric ValueList.resolveConstantForwardRefs(); 1967d88c1a5aSDimitry Andric return Error::success(); 1968139f7f9bSDimitry Andric case BitstreamEntry::Record: 1969139f7f9bSDimitry Andric // The interesting case. 1970f22ef01cSRoman Divacky break; 1971f22ef01cSRoman Divacky } 1972f22ef01cSRoman Divacky 1973f22ef01cSRoman Divacky // Read a record. 1974f22ef01cSRoman Divacky Record.clear(); 19753ca95b02SDimitry Andric Type *VoidType = Type::getVoidTy(Context); 197691bc56edSDimitry Andric Value *V = nullptr; 1977139f7f9bSDimitry Andric unsigned BitCode = Stream.readRecord(Entry.ID, Record); 1978f22ef01cSRoman Divacky switch (BitCode) { 1979f22ef01cSRoman Divacky default: // Default behavior: unknown constant 1980f22ef01cSRoman Divacky case bitc::CST_CODE_UNDEF: // UNDEF 1981f22ef01cSRoman Divacky V = UndefValue::get(CurTy); 1982f22ef01cSRoman Divacky break; 1983f22ef01cSRoman Divacky case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid] 1984f22ef01cSRoman Divacky if (Record.empty()) 19858f0fd8f6SDimitry Andric return error("Invalid record"); 198691bc56edSDimitry Andric if (Record[0] >= TypeList.size() || !TypeList[Record[0]]) 19878f0fd8f6SDimitry Andric return error("Invalid record"); 19883ca95b02SDimitry Andric if (TypeList[Record[0]] == VoidType) 19893ca95b02SDimitry Andric return error("Invalid constant type"); 1990f22ef01cSRoman Divacky CurTy = TypeList[Record[0]]; 1991f22ef01cSRoman Divacky continue; // Skip the ValueList manipulation. 1992f22ef01cSRoman Divacky case bitc::CST_CODE_NULL: // NULL 1993f22ef01cSRoman Divacky V = Constant::getNullValue(CurTy); 1994f22ef01cSRoman Divacky break; 1995f22ef01cSRoman Divacky case bitc::CST_CODE_INTEGER: // INTEGER: [intval] 1996f22ef01cSRoman Divacky if (!CurTy->isIntegerTy() || Record.empty()) 19978f0fd8f6SDimitry Andric return error("Invalid record"); 19983861d79fSDimitry Andric V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0])); 1999f22ef01cSRoman Divacky break; 2000f22ef01cSRoman Divacky case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval] 2001f22ef01cSRoman Divacky if (!CurTy->isIntegerTy() || Record.empty()) 20028f0fd8f6SDimitry Andric return error("Invalid record"); 2003f22ef01cSRoman Divacky 20048f0fd8f6SDimitry Andric APInt VInt = 20058f0fd8f6SDimitry Andric readWideAPInt(Record, cast<IntegerType>(CurTy)->getBitWidth()); 20067ae0e2c9SDimitry Andric V = ConstantInt::get(Context, VInt); 20077ae0e2c9SDimitry Andric 2008f22ef01cSRoman Divacky break; 2009f22ef01cSRoman Divacky } 2010f22ef01cSRoman Divacky case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval] 2011f22ef01cSRoman Divacky if (Record.empty()) 20128f0fd8f6SDimitry Andric return error("Invalid record"); 2013dff0c46cSDimitry Andric if (CurTy->isHalfTy()) 2014d88c1a5aSDimitry Andric V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf(), 2015139f7f9bSDimitry Andric APInt(16, (uint16_t)Record[0]))); 2016dff0c46cSDimitry Andric else if (CurTy->isFloatTy()) 2017d88c1a5aSDimitry Andric V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle(), 2018139f7f9bSDimitry Andric APInt(32, (uint32_t)Record[0]))); 2019f22ef01cSRoman Divacky else if (CurTy->isDoubleTy()) 2020d88c1a5aSDimitry Andric V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble(), 2021139f7f9bSDimitry Andric APInt(64, Record[0]))); 2022f22ef01cSRoman Divacky else if (CurTy->isX86_FP80Ty()) { 2023f22ef01cSRoman Divacky // Bits are not stored the same way as a normal i80 APInt, compensate. 2024f22ef01cSRoman Divacky uint64_t Rearrange[2]; 2025f22ef01cSRoman Divacky Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16); 2026f22ef01cSRoman Divacky Rearrange[1] = Record[0] >> 48; 2027d88c1a5aSDimitry Andric V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended(), 2028139f7f9bSDimitry Andric APInt(80, Rearrange))); 2029f22ef01cSRoman Divacky } else if (CurTy->isFP128Ty()) 2030d88c1a5aSDimitry Andric V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad(), 2031139f7f9bSDimitry Andric APInt(128, Record))); 2032f22ef01cSRoman Divacky else if (CurTy->isPPC_FP128Ty()) 2033d88c1a5aSDimitry Andric V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble(), 2034139f7f9bSDimitry Andric APInt(128, Record))); 2035f22ef01cSRoman Divacky else 2036f22ef01cSRoman Divacky V = UndefValue::get(CurTy); 2037f22ef01cSRoman Divacky break; 2038f22ef01cSRoman Divacky } 2039f22ef01cSRoman Divacky 2040f22ef01cSRoman Divacky case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number] 2041f22ef01cSRoman Divacky if (Record.empty()) 20428f0fd8f6SDimitry Andric return error("Invalid record"); 2043f22ef01cSRoman Divacky 2044f22ef01cSRoman Divacky unsigned Size = Record.size(); 2045dff0c46cSDimitry Andric SmallVector<Constant*, 16> Elts; 2046f22ef01cSRoman Divacky 20476122f3e6SDimitry Andric if (StructType *STy = dyn_cast<StructType>(CurTy)) { 2048f22ef01cSRoman Divacky for (unsigned i = 0; i != Size; ++i) 2049f22ef01cSRoman Divacky Elts.push_back(ValueList.getConstantFwdRef(Record[i], 2050f22ef01cSRoman Divacky STy->getElementType(i))); 2051f22ef01cSRoman Divacky V = ConstantStruct::get(STy, Elts); 20526122f3e6SDimitry Andric } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) { 20536122f3e6SDimitry Andric Type *EltTy = ATy->getElementType(); 2054f22ef01cSRoman Divacky for (unsigned i = 0; i != Size; ++i) 2055f22ef01cSRoman Divacky Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); 2056f22ef01cSRoman Divacky V = ConstantArray::get(ATy, Elts); 20576122f3e6SDimitry Andric } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) { 20586122f3e6SDimitry Andric Type *EltTy = VTy->getElementType(); 2059f22ef01cSRoman Divacky for (unsigned i = 0; i != Size; ++i) 2060f22ef01cSRoman Divacky Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); 2061f22ef01cSRoman Divacky V = ConstantVector::get(Elts); 2062f22ef01cSRoman Divacky } else { 2063f22ef01cSRoman Divacky V = UndefValue::get(CurTy); 2064f22ef01cSRoman Divacky } 2065f22ef01cSRoman Divacky break; 2066f22ef01cSRoman Divacky } 2067dff0c46cSDimitry Andric case bitc::CST_CODE_STRING: // STRING: [values] 2068f22ef01cSRoman Divacky case bitc::CST_CODE_CSTRING: { // CSTRING: [values] 2069f22ef01cSRoman Divacky if (Record.empty()) 20708f0fd8f6SDimitry Andric return error("Invalid record"); 2071f22ef01cSRoman Divacky 20727ae0e2c9SDimitry Andric SmallString<16> Elts(Record.begin(), Record.end()); 2073dff0c46cSDimitry Andric V = ConstantDataArray::getString(Context, Elts, 2074dff0c46cSDimitry Andric BitCode == bitc::CST_CODE_CSTRING); 2075f22ef01cSRoman Divacky break; 2076f22ef01cSRoman Divacky } 2077dff0c46cSDimitry Andric case bitc::CST_CODE_DATA: {// DATA: [n x value] 2078dff0c46cSDimitry Andric if (Record.empty()) 20798f0fd8f6SDimitry Andric return error("Invalid record"); 2080dff0c46cSDimitry Andric 2081dff0c46cSDimitry Andric Type *EltTy = cast<SequentialType>(CurTy)->getElementType(); 2082dff0c46cSDimitry Andric if (EltTy->isIntegerTy(8)) { 2083dff0c46cSDimitry Andric SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end()); 2084dff0c46cSDimitry Andric if (isa<VectorType>(CurTy)) 2085dff0c46cSDimitry Andric V = ConstantDataVector::get(Context, Elts); 2086dff0c46cSDimitry Andric else 2087dff0c46cSDimitry Andric V = ConstantDataArray::get(Context, Elts); 2088dff0c46cSDimitry Andric } else if (EltTy->isIntegerTy(16)) { 2089dff0c46cSDimitry Andric SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); 2090dff0c46cSDimitry Andric if (isa<VectorType>(CurTy)) 2091dff0c46cSDimitry Andric V = ConstantDataVector::get(Context, Elts); 2092dff0c46cSDimitry Andric else 2093dff0c46cSDimitry Andric V = ConstantDataArray::get(Context, Elts); 2094dff0c46cSDimitry Andric } else if (EltTy->isIntegerTy(32)) { 2095dff0c46cSDimitry Andric SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); 2096dff0c46cSDimitry Andric if (isa<VectorType>(CurTy)) 2097dff0c46cSDimitry Andric V = ConstantDataVector::get(Context, Elts); 2098dff0c46cSDimitry Andric else 2099dff0c46cSDimitry Andric V = ConstantDataArray::get(Context, Elts); 2100dff0c46cSDimitry Andric } else if (EltTy->isIntegerTy(64)) { 2101dff0c46cSDimitry Andric SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); 2102dff0c46cSDimitry Andric if (isa<VectorType>(CurTy)) 2103dff0c46cSDimitry Andric V = ConstantDataVector::get(Context, Elts); 2104dff0c46cSDimitry Andric else 2105dff0c46cSDimitry Andric V = ConstantDataArray::get(Context, Elts); 2106444ed5c5SDimitry Andric } else if (EltTy->isHalfTy()) { 2107444ed5c5SDimitry Andric SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); 2108444ed5c5SDimitry Andric if (isa<VectorType>(CurTy)) 2109444ed5c5SDimitry Andric V = ConstantDataVector::getFP(Context, Elts); 2110444ed5c5SDimitry Andric else 2111444ed5c5SDimitry Andric V = ConstantDataArray::getFP(Context, Elts); 2112dff0c46cSDimitry Andric } else if (EltTy->isFloatTy()) { 2113444ed5c5SDimitry Andric SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); 2114dff0c46cSDimitry Andric if (isa<VectorType>(CurTy)) 2115444ed5c5SDimitry Andric V = ConstantDataVector::getFP(Context, Elts); 2116dff0c46cSDimitry Andric else 2117444ed5c5SDimitry Andric V = ConstantDataArray::getFP(Context, Elts); 2118dff0c46cSDimitry Andric } else if (EltTy->isDoubleTy()) { 2119444ed5c5SDimitry Andric SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); 2120dff0c46cSDimitry Andric if (isa<VectorType>(CurTy)) 2121444ed5c5SDimitry Andric V = ConstantDataVector::getFP(Context, Elts); 2122dff0c46cSDimitry Andric else 2123444ed5c5SDimitry Andric V = ConstantDataArray::getFP(Context, Elts); 2124dff0c46cSDimitry Andric } else { 21258f0fd8f6SDimitry Andric return error("Invalid type for value"); 2126dff0c46cSDimitry Andric } 2127dff0c46cSDimitry Andric break; 2128dff0c46cSDimitry Andric } 2129f22ef01cSRoman Divacky case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval] 2130f785676fSDimitry Andric if (Record.size() < 3) 21318f0fd8f6SDimitry Andric return error("Invalid record"); 21328f0fd8f6SDimitry Andric int Opc = getDecodedBinaryOpcode(Record[0], CurTy); 2133f22ef01cSRoman Divacky if (Opc < 0) { 2134f22ef01cSRoman Divacky V = UndefValue::get(CurTy); // Unknown binop. 2135f22ef01cSRoman Divacky } else { 2136f22ef01cSRoman Divacky Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy); 2137f22ef01cSRoman Divacky Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy); 2138f22ef01cSRoman Divacky unsigned Flags = 0; 2139f22ef01cSRoman Divacky if (Record.size() >= 4) { 2140f22ef01cSRoman Divacky if (Opc == Instruction::Add || 2141f22ef01cSRoman Divacky Opc == Instruction::Sub || 21422754fe60SDimitry Andric Opc == Instruction::Mul || 21432754fe60SDimitry Andric Opc == Instruction::Shl) { 2144f22ef01cSRoman Divacky if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP)) 2145f22ef01cSRoman Divacky Flags |= OverflowingBinaryOperator::NoSignedWrap; 2146f22ef01cSRoman Divacky if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) 2147f22ef01cSRoman Divacky Flags |= OverflowingBinaryOperator::NoUnsignedWrap; 21482754fe60SDimitry Andric } else if (Opc == Instruction::SDiv || 21492754fe60SDimitry Andric Opc == Instruction::UDiv || 21502754fe60SDimitry Andric Opc == Instruction::LShr || 21512754fe60SDimitry Andric Opc == Instruction::AShr) { 21522754fe60SDimitry Andric if (Record[3] & (1 << bitc::PEO_EXACT)) 2153f22ef01cSRoman Divacky Flags |= SDivOperator::IsExact; 2154f22ef01cSRoman Divacky } 2155f22ef01cSRoman Divacky } 2156f22ef01cSRoman Divacky V = ConstantExpr::get(Opc, LHS, RHS, Flags); 2157f22ef01cSRoman Divacky } 2158f22ef01cSRoman Divacky break; 2159f22ef01cSRoman Divacky } 2160f22ef01cSRoman Divacky case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval] 2161f785676fSDimitry Andric if (Record.size() < 3) 21628f0fd8f6SDimitry Andric return error("Invalid record"); 21638f0fd8f6SDimitry Andric int Opc = getDecodedCastOpcode(Record[0]); 2164f22ef01cSRoman Divacky if (Opc < 0) { 2165f22ef01cSRoman Divacky V = UndefValue::get(CurTy); // Unknown cast. 2166f22ef01cSRoman Divacky } else { 21676122f3e6SDimitry Andric Type *OpTy = getTypeByID(Record[1]); 2168f785676fSDimitry Andric if (!OpTy) 21698f0fd8f6SDimitry Andric return error("Invalid record"); 2170f22ef01cSRoman Divacky Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy); 2171f785676fSDimitry Andric V = UpgradeBitCastExpr(Opc, Op, CurTy); 2172f785676fSDimitry Andric if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy); 2173f22ef01cSRoman Divacky } 2174f22ef01cSRoman Divacky break; 2175f22ef01cSRoman Divacky } 2176d88c1a5aSDimitry Andric case bitc::CST_CODE_CE_INBOUNDS_GEP: // [ty, n x operands] 2177d88c1a5aSDimitry Andric case bitc::CST_CODE_CE_GEP: // [ty, n x operands] 2178d88c1a5aSDimitry Andric case bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX: { // [ty, flags, n x 2179d88c1a5aSDimitry Andric // operands] 2180ff0cc061SDimitry Andric unsigned OpNum = 0; 2181ff0cc061SDimitry Andric Type *PointeeType = nullptr; 2182d88c1a5aSDimitry Andric if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX || 2183d88c1a5aSDimitry Andric Record.size() % 2) 2184ff0cc061SDimitry Andric PointeeType = getTypeByID(Record[OpNum++]); 2185d88c1a5aSDimitry Andric 2186d88c1a5aSDimitry Andric bool InBounds = false; 2187d88c1a5aSDimitry Andric Optional<unsigned> InRangeIndex; 2188d88c1a5aSDimitry Andric if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX) { 2189d88c1a5aSDimitry Andric uint64_t Op = Record[OpNum++]; 2190d88c1a5aSDimitry Andric InBounds = Op & 1; 2191d88c1a5aSDimitry Andric InRangeIndex = Op >> 1; 2192d88c1a5aSDimitry Andric } else if (BitCode == bitc::CST_CODE_CE_INBOUNDS_GEP) 2193d88c1a5aSDimitry Andric InBounds = true; 2194d88c1a5aSDimitry Andric 2195f22ef01cSRoman Divacky SmallVector<Constant*, 16> Elts; 2196ff0cc061SDimitry Andric while (OpNum != Record.size()) { 2197ff0cc061SDimitry Andric Type *ElTy = getTypeByID(Record[OpNum++]); 2198f785676fSDimitry Andric if (!ElTy) 21998f0fd8f6SDimitry Andric return error("Invalid record"); 2200ff0cc061SDimitry Andric Elts.push_back(ValueList.getConstantFwdRef(Record[OpNum++], ElTy)); 2201f22ef01cSRoman Divacky } 2202ff0cc061SDimitry Andric 2203ff0cc061SDimitry Andric if (PointeeType && 2204ff0cc061SDimitry Andric PointeeType != 2205d88c1a5aSDimitry Andric cast<PointerType>(Elts[0]->getType()->getScalarType()) 2206ff0cc061SDimitry Andric ->getElementType()) 22078f0fd8f6SDimitry Andric return error("Explicit gep operator type does not match pointee type " 2208ff0cc061SDimitry Andric "of pointer operand"); 2209ff0cc061SDimitry Andric 22103ca95b02SDimitry Andric if (Elts.size() < 1) 22113ca95b02SDimitry Andric return error("Invalid gep with no operands"); 22123ca95b02SDimitry Andric 22136122f3e6SDimitry Andric ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end()); 2214ff0cc061SDimitry Andric V = ConstantExpr::getGetElementPtr(PointeeType, Elts[0], Indices, 2215d88c1a5aSDimitry Andric InBounds, InRangeIndex); 2216f22ef01cSRoman Divacky break; 2217f22ef01cSRoman Divacky } 2218f785676fSDimitry Andric case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#] 2219f785676fSDimitry Andric if (Record.size() < 3) 22208f0fd8f6SDimitry Andric return error("Invalid record"); 2221f785676fSDimitry Andric 2222f785676fSDimitry Andric Type *SelectorTy = Type::getInt1Ty(Context); 2223f785676fSDimitry Andric 22247d523365SDimitry Andric // The selector might be an i1 or an <n x i1> 22257d523365SDimitry Andric // Get the type from the ValueList before getting a forward ref. 2226f785676fSDimitry Andric if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) 22277d523365SDimitry Andric if (Value *V = ValueList[Record[0]]) 22287d523365SDimitry Andric if (SelectorTy != V->getType()) 22297d523365SDimitry Andric SelectorTy = VectorType::get(SelectorTy, VTy->getNumElements()); 2230f785676fSDimitry Andric 2231f785676fSDimitry Andric V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0], 2232f785676fSDimitry Andric SelectorTy), 2233f22ef01cSRoman Divacky ValueList.getConstantFwdRef(Record[1],CurTy), 2234f22ef01cSRoman Divacky ValueList.getConstantFwdRef(Record[2],CurTy)); 2235f22ef01cSRoman Divacky break; 2236f785676fSDimitry Andric } 223791bc56edSDimitry Andric case bitc::CST_CODE_CE_EXTRACTELT 223891bc56edSDimitry Andric : { // CE_EXTRACTELT: [opty, opval, opty, opval] 2239f785676fSDimitry Andric if (Record.size() < 3) 22408f0fd8f6SDimitry Andric return error("Invalid record"); 22416122f3e6SDimitry Andric VectorType *OpTy = 2242f22ef01cSRoman Divacky dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); 224391bc56edSDimitry Andric if (!OpTy) 22448f0fd8f6SDimitry Andric return error("Invalid record"); 2245f22ef01cSRoman Divacky Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); 224691bc56edSDimitry Andric Constant *Op1 = nullptr; 224791bc56edSDimitry Andric if (Record.size() == 4) { 224891bc56edSDimitry Andric Type *IdxTy = getTypeByID(Record[2]); 224991bc56edSDimitry Andric if (!IdxTy) 22508f0fd8f6SDimitry Andric return error("Invalid record"); 225191bc56edSDimitry Andric Op1 = ValueList.getConstantFwdRef(Record[3], IdxTy); 225291bc56edSDimitry Andric } else // TODO: Remove with llvm 4.0 225391bc56edSDimitry Andric Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context)); 225491bc56edSDimitry Andric if (!Op1) 22558f0fd8f6SDimitry Andric return error("Invalid record"); 2256f22ef01cSRoman Divacky V = ConstantExpr::getExtractElement(Op0, Op1); 2257f22ef01cSRoman Divacky break; 2258f22ef01cSRoman Divacky } 225991bc56edSDimitry Andric case bitc::CST_CODE_CE_INSERTELT 226091bc56edSDimitry Andric : { // CE_INSERTELT: [opval, opval, opty, opval] 22616122f3e6SDimitry Andric VectorType *OpTy = dyn_cast<VectorType>(CurTy); 226291bc56edSDimitry Andric if (Record.size() < 3 || !OpTy) 22638f0fd8f6SDimitry Andric return error("Invalid record"); 2264f22ef01cSRoman Divacky Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); 2265f22ef01cSRoman Divacky Constant *Op1 = ValueList.getConstantFwdRef(Record[1], 2266f22ef01cSRoman Divacky OpTy->getElementType()); 226791bc56edSDimitry Andric Constant *Op2 = nullptr; 226891bc56edSDimitry Andric if (Record.size() == 4) { 226991bc56edSDimitry Andric Type *IdxTy = getTypeByID(Record[2]); 227091bc56edSDimitry Andric if (!IdxTy) 22718f0fd8f6SDimitry Andric return error("Invalid record"); 227291bc56edSDimitry Andric Op2 = ValueList.getConstantFwdRef(Record[3], IdxTy); 227391bc56edSDimitry Andric } else // TODO: Remove with llvm 4.0 227491bc56edSDimitry Andric Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context)); 227591bc56edSDimitry Andric if (!Op2) 22768f0fd8f6SDimitry Andric return error("Invalid record"); 2277f22ef01cSRoman Divacky V = ConstantExpr::getInsertElement(Op0, Op1, Op2); 2278f22ef01cSRoman Divacky break; 2279f22ef01cSRoman Divacky } 2280f22ef01cSRoman Divacky case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval] 22816122f3e6SDimitry Andric VectorType *OpTy = dyn_cast<VectorType>(CurTy); 228291bc56edSDimitry Andric if (Record.size() < 3 || !OpTy) 22838f0fd8f6SDimitry Andric return error("Invalid record"); 2284f22ef01cSRoman Divacky Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); 2285f22ef01cSRoman Divacky Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy); 22866122f3e6SDimitry Andric Type *ShufTy = VectorType::get(Type::getInt32Ty(Context), 2287f22ef01cSRoman Divacky OpTy->getNumElements()); 2288f22ef01cSRoman Divacky Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy); 2289f22ef01cSRoman Divacky V = ConstantExpr::getShuffleVector(Op0, Op1, Op2); 2290f22ef01cSRoman Divacky break; 2291f22ef01cSRoman Divacky } 2292f22ef01cSRoman Divacky case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval] 22936122f3e6SDimitry Andric VectorType *RTy = dyn_cast<VectorType>(CurTy); 22946122f3e6SDimitry Andric VectorType *OpTy = 22952754fe60SDimitry Andric dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); 229691bc56edSDimitry Andric if (Record.size() < 4 || !RTy || !OpTy) 22978f0fd8f6SDimitry Andric return error("Invalid record"); 2298f22ef01cSRoman Divacky Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); 2299f22ef01cSRoman Divacky Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); 23006122f3e6SDimitry Andric Type *ShufTy = VectorType::get(Type::getInt32Ty(Context), 2301f22ef01cSRoman Divacky RTy->getNumElements()); 2302f22ef01cSRoman Divacky Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy); 2303f22ef01cSRoman Divacky V = ConstantExpr::getShuffleVector(Op0, Op1, Op2); 2304f22ef01cSRoman Divacky break; 2305f22ef01cSRoman Divacky } 2306f22ef01cSRoman Divacky case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred] 2307f785676fSDimitry Andric if (Record.size() < 4) 23088f0fd8f6SDimitry Andric return error("Invalid record"); 23096122f3e6SDimitry Andric Type *OpTy = getTypeByID(Record[0]); 231091bc56edSDimitry Andric if (!OpTy) 23118f0fd8f6SDimitry Andric return error("Invalid record"); 2312f22ef01cSRoman Divacky Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); 2313f22ef01cSRoman Divacky Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); 2314f22ef01cSRoman Divacky 2315f22ef01cSRoman Divacky if (OpTy->isFPOrFPVectorTy()) 2316f22ef01cSRoman Divacky V = ConstantExpr::getFCmp(Record[3], Op0, Op1); 2317f22ef01cSRoman Divacky else 2318f22ef01cSRoman Divacky V = ConstantExpr::getICmp(Record[3], Op0, Op1); 2319f22ef01cSRoman Divacky break; 2320f22ef01cSRoman Divacky } 23213861d79fSDimitry Andric // This maintains backward compatibility, pre-asm dialect keywords. 23223861d79fSDimitry Andric // FIXME: Remove with the 4.0 release. 23233861d79fSDimitry Andric case bitc::CST_CODE_INLINEASM_OLD: { 2324f785676fSDimitry Andric if (Record.size() < 2) 23258f0fd8f6SDimitry Andric return error("Invalid record"); 2326f22ef01cSRoman Divacky std::string AsmStr, ConstrStr; 2327f22ef01cSRoman Divacky bool HasSideEffects = Record[0] & 1; 2328f22ef01cSRoman Divacky bool IsAlignStack = Record[0] >> 1; 2329f22ef01cSRoman Divacky unsigned AsmStrSize = Record[1]; 2330f22ef01cSRoman Divacky if (2+AsmStrSize >= Record.size()) 23318f0fd8f6SDimitry Andric return error("Invalid record"); 2332f22ef01cSRoman Divacky unsigned ConstStrSize = Record[2+AsmStrSize]; 2333f22ef01cSRoman Divacky if (3+AsmStrSize+ConstStrSize > Record.size()) 23348f0fd8f6SDimitry Andric return error("Invalid record"); 2335f22ef01cSRoman Divacky 2336f22ef01cSRoman Divacky for (unsigned i = 0; i != AsmStrSize; ++i) 2337f22ef01cSRoman Divacky AsmStr += (char)Record[2+i]; 2338f22ef01cSRoman Divacky for (unsigned i = 0; i != ConstStrSize; ++i) 2339f22ef01cSRoman Divacky ConstrStr += (char)Record[3+AsmStrSize+i]; 23406122f3e6SDimitry Andric PointerType *PTy = cast<PointerType>(CurTy); 2341f22ef01cSRoman Divacky V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()), 2342f22ef01cSRoman Divacky AsmStr, ConstrStr, HasSideEffects, IsAlignStack); 2343f22ef01cSRoman Divacky break; 2344f22ef01cSRoman Divacky } 23453861d79fSDimitry Andric // This version adds support for the asm dialect keywords (e.g., 23463861d79fSDimitry Andric // inteldialect). 23473861d79fSDimitry Andric case bitc::CST_CODE_INLINEASM: { 2348f785676fSDimitry Andric if (Record.size() < 2) 23498f0fd8f6SDimitry Andric return error("Invalid record"); 23503861d79fSDimitry Andric std::string AsmStr, ConstrStr; 23513861d79fSDimitry Andric bool HasSideEffects = Record[0] & 1; 23523861d79fSDimitry Andric bool IsAlignStack = (Record[0] >> 1) & 1; 23533861d79fSDimitry Andric unsigned AsmDialect = Record[0] >> 2; 23543861d79fSDimitry Andric unsigned AsmStrSize = Record[1]; 23553861d79fSDimitry Andric if (2+AsmStrSize >= Record.size()) 23568f0fd8f6SDimitry Andric return error("Invalid record"); 23573861d79fSDimitry Andric unsigned ConstStrSize = Record[2+AsmStrSize]; 23583861d79fSDimitry Andric if (3+AsmStrSize+ConstStrSize > Record.size()) 23598f0fd8f6SDimitry Andric return error("Invalid record"); 23603861d79fSDimitry Andric 23613861d79fSDimitry Andric for (unsigned i = 0; i != AsmStrSize; ++i) 23623861d79fSDimitry Andric AsmStr += (char)Record[2+i]; 23633861d79fSDimitry Andric for (unsigned i = 0; i != ConstStrSize; ++i) 23643861d79fSDimitry Andric ConstrStr += (char)Record[3+AsmStrSize+i]; 23653861d79fSDimitry Andric PointerType *PTy = cast<PointerType>(CurTy); 23663861d79fSDimitry Andric V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()), 23673861d79fSDimitry Andric AsmStr, ConstrStr, HasSideEffects, IsAlignStack, 23683861d79fSDimitry Andric InlineAsm::AsmDialect(AsmDialect)); 23693861d79fSDimitry Andric break; 23703861d79fSDimitry Andric } 2371f22ef01cSRoman Divacky case bitc::CST_CODE_BLOCKADDRESS:{ 2372f785676fSDimitry Andric if (Record.size() < 3) 23738f0fd8f6SDimitry Andric return error("Invalid record"); 23746122f3e6SDimitry Andric Type *FnTy = getTypeByID(Record[0]); 237591bc56edSDimitry Andric if (!FnTy) 23768f0fd8f6SDimitry Andric return error("Invalid record"); 2377f22ef01cSRoman Divacky Function *Fn = 2378f22ef01cSRoman Divacky dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy)); 237991bc56edSDimitry Andric if (!Fn) 23808f0fd8f6SDimitry Andric return error("Invalid record"); 238139d628a0SDimitry Andric 23823861d79fSDimitry Andric // If the function is already parsed we can insert the block address right 23833861d79fSDimitry Andric // away. 238439d628a0SDimitry Andric BasicBlock *BB; 238539d628a0SDimitry Andric unsigned BBID = Record[2]; 238639d628a0SDimitry Andric if (!BBID) 238739d628a0SDimitry Andric // Invalid reference to entry block. 23888f0fd8f6SDimitry Andric return error("Invalid ID"); 23893861d79fSDimitry Andric if (!Fn->empty()) { 23903861d79fSDimitry Andric Function::iterator BBI = Fn->begin(), BBE = Fn->end(); 239139d628a0SDimitry Andric for (size_t I = 0, E = BBID; I != E; ++I) { 23923861d79fSDimitry Andric if (BBI == BBE) 23938f0fd8f6SDimitry Andric return error("Invalid ID"); 23943861d79fSDimitry Andric ++BBI; 23953861d79fSDimitry Andric } 23967d523365SDimitry Andric BB = &*BBI; 23973861d79fSDimitry Andric } else { 23983861d79fSDimitry Andric // Otherwise insert a placeholder and remember it so it can be inserted 23993861d79fSDimitry Andric // when the function is parsed. 240039d628a0SDimitry Andric auto &FwdBBs = BasicBlockFwdRefs[Fn]; 240139d628a0SDimitry Andric if (FwdBBs.empty()) 240239d628a0SDimitry Andric BasicBlockFwdRefQueue.push_back(Fn); 240339d628a0SDimitry Andric if (FwdBBs.size() < BBID + 1) 240439d628a0SDimitry Andric FwdBBs.resize(BBID + 1); 240539d628a0SDimitry Andric if (!FwdBBs[BBID]) 240639d628a0SDimitry Andric FwdBBs[BBID] = BasicBlock::Create(Context); 240739d628a0SDimitry Andric BB = FwdBBs[BBID]; 24083861d79fSDimitry Andric } 240939d628a0SDimitry Andric V = BlockAddress::get(Fn, BB); 2410f22ef01cSRoman Divacky break; 2411f22ef01cSRoman Divacky } 2412f22ef01cSRoman Divacky } 2413f22ef01cSRoman Divacky 24148f0fd8f6SDimitry Andric ValueList.assignValue(V, NextCstNo); 2415f22ef01cSRoman Divacky ++NextCstNo; 2416f22ef01cSRoman Divacky } 2417f22ef01cSRoman Divacky } 2418f22ef01cSRoman Divacky 2419d88c1a5aSDimitry Andric Error BitcodeReader::parseUseLists() { 2420dff0c46cSDimitry Andric if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID)) 24218f0fd8f6SDimitry Andric return error("Invalid record"); 2422dff0c46cSDimitry Andric 2423dff0c46cSDimitry Andric // Read all the records. 242439d628a0SDimitry Andric SmallVector<uint64_t, 64> Record; 2425d88c1a5aSDimitry Andric 2426d88c1a5aSDimitry Andric while (true) { 2427139f7f9bSDimitry Andric BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 2428139f7f9bSDimitry Andric 2429139f7f9bSDimitry Andric switch (Entry.Kind) { 2430139f7f9bSDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 2431139f7f9bSDimitry Andric case BitstreamEntry::Error: 24328f0fd8f6SDimitry Andric return error("Malformed block"); 2433139f7f9bSDimitry Andric case BitstreamEntry::EndBlock: 2434d88c1a5aSDimitry Andric return Error::success(); 2435139f7f9bSDimitry Andric case BitstreamEntry::Record: 2436139f7f9bSDimitry Andric // The interesting case. 2437139f7f9bSDimitry Andric break; 2438dff0c46cSDimitry Andric } 2439dff0c46cSDimitry Andric 2440dff0c46cSDimitry Andric // Read a use list record. 2441dff0c46cSDimitry Andric Record.clear(); 244239d628a0SDimitry Andric bool IsBB = false; 2443139f7f9bSDimitry Andric switch (Stream.readRecord(Entry.ID, Record)) { 2444dff0c46cSDimitry Andric default: // Default behavior: unknown type. 2445dff0c46cSDimitry Andric break; 244639d628a0SDimitry Andric case bitc::USELIST_CODE_BB: 244739d628a0SDimitry Andric IsBB = true; 2448d88c1a5aSDimitry Andric LLVM_FALLTHROUGH; 244939d628a0SDimitry Andric case bitc::USELIST_CODE_DEFAULT: { 2450dff0c46cSDimitry Andric unsigned RecordLength = Record.size(); 245139d628a0SDimitry Andric if (RecordLength < 3) 245239d628a0SDimitry Andric // Records should have at least an ID and two indexes. 24538f0fd8f6SDimitry Andric return error("Invalid record"); 245439d628a0SDimitry Andric unsigned ID = Record.back(); 245539d628a0SDimitry Andric Record.pop_back(); 245639d628a0SDimitry Andric 245739d628a0SDimitry Andric Value *V; 245839d628a0SDimitry Andric if (IsBB) { 245939d628a0SDimitry Andric assert(ID < FunctionBBs.size() && "Basic block not found"); 246039d628a0SDimitry Andric V = FunctionBBs[ID]; 246139d628a0SDimitry Andric } else 246239d628a0SDimitry Andric V = ValueList[ID]; 246339d628a0SDimitry Andric unsigned NumUses = 0; 246439d628a0SDimitry Andric SmallDenseMap<const Use *, unsigned, 16> Order; 24657d523365SDimitry Andric for (const Use &U : V->materialized_uses()) { 246639d628a0SDimitry Andric if (++NumUses > Record.size()) 246739d628a0SDimitry Andric break; 246839d628a0SDimitry Andric Order[&U] = Record[NumUses - 1]; 246939d628a0SDimitry Andric } 247039d628a0SDimitry Andric if (Order.size() != Record.size() || NumUses > Record.size()) 247139d628a0SDimitry Andric // Mismatches can happen if the functions are being materialized lazily 247239d628a0SDimitry Andric // (out-of-order), or a value has been upgraded. 247339d628a0SDimitry Andric break; 247439d628a0SDimitry Andric 247539d628a0SDimitry Andric V->sortUseList([&](const Use &L, const Use &R) { 247639d628a0SDimitry Andric return Order.lookup(&L) < Order.lookup(&R); 247739d628a0SDimitry Andric }); 2478dff0c46cSDimitry Andric break; 2479dff0c46cSDimitry Andric } 2480dff0c46cSDimitry Andric } 2481dff0c46cSDimitry Andric } 2482dff0c46cSDimitry Andric } 2483dff0c46cSDimitry Andric 2484ff0cc061SDimitry Andric /// When we see the block for metadata, remember where it is and then skip it. 2485ff0cc061SDimitry Andric /// This lets us lazily deserialize the metadata. 2486d88c1a5aSDimitry Andric Error BitcodeReader::rememberAndSkipMetadata() { 2487ff0cc061SDimitry Andric // Save the current stream state. 2488ff0cc061SDimitry Andric uint64_t CurBit = Stream.GetCurrentBitNo(); 2489ff0cc061SDimitry Andric DeferredMetadataInfo.push_back(CurBit); 2490ff0cc061SDimitry Andric 2491ff0cc061SDimitry Andric // Skip over the block for now. 2492ff0cc061SDimitry Andric if (Stream.SkipBlock()) 24938f0fd8f6SDimitry Andric return error("Invalid record"); 2494d88c1a5aSDimitry Andric return Error::success(); 2495ff0cc061SDimitry Andric } 2496ff0cc061SDimitry Andric 2497d88c1a5aSDimitry Andric Error BitcodeReader::materializeMetadata() { 2498ff0cc061SDimitry Andric for (uint64_t BitPos : DeferredMetadataInfo) { 2499ff0cc061SDimitry Andric // Move the bit stream to the saved position. 2500ff0cc061SDimitry Andric Stream.JumpToBit(BitPos); 2501d88c1a5aSDimitry Andric if (Error Err = MDLoader->parseModuleMetadata()) 2502d88c1a5aSDimitry Andric return Err; 2503ff0cc061SDimitry Andric } 2504ff0cc061SDimitry Andric DeferredMetadataInfo.clear(); 2505d88c1a5aSDimitry Andric return Error::success(); 2506ff0cc061SDimitry Andric } 2507ff0cc061SDimitry Andric 2508ff0cc061SDimitry Andric void BitcodeReader::setStripDebugInfo() { StripDebugInfo = true; } 2509ff0cc061SDimitry Andric 25108f0fd8f6SDimitry Andric /// When we see the block for a function body, remember where it is and then 25118f0fd8f6SDimitry Andric /// skip it. This lets us lazily deserialize the functions. 2512d88c1a5aSDimitry Andric Error BitcodeReader::rememberAndSkipFunctionBody() { 2513f22ef01cSRoman Divacky // Get the function we are talking about. 2514f22ef01cSRoman Divacky if (FunctionsWithBodies.empty()) 25158f0fd8f6SDimitry Andric return error("Insufficient function protos"); 2516f22ef01cSRoman Divacky 2517f22ef01cSRoman Divacky Function *Fn = FunctionsWithBodies.back(); 2518f22ef01cSRoman Divacky FunctionsWithBodies.pop_back(); 2519f22ef01cSRoman Divacky 2520f22ef01cSRoman Divacky // Save the current stream state. 2521f22ef01cSRoman Divacky uint64_t CurBit = Stream.GetCurrentBitNo(); 25227d523365SDimitry Andric assert( 25237d523365SDimitry Andric (DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) && 25247d523365SDimitry Andric "Mismatch between VST and scanned function offsets"); 2525f22ef01cSRoman Divacky DeferredFunctionInfo[Fn] = CurBit; 2526f22ef01cSRoman Divacky 2527f22ef01cSRoman Divacky // Skip over the function block for now. 2528f22ef01cSRoman Divacky if (Stream.SkipBlock()) 25298f0fd8f6SDimitry Andric return error("Invalid record"); 2530d88c1a5aSDimitry Andric return Error::success(); 2531f22ef01cSRoman Divacky } 2532f22ef01cSRoman Divacky 2533d88c1a5aSDimitry Andric Error BitcodeReader::globalCleanup() { 2534f22ef01cSRoman Divacky // Patch the initializers for globals and aliases up. 2535d88c1a5aSDimitry Andric if (Error Err = resolveGlobalAndIndirectSymbolInits()) 2536d88c1a5aSDimitry Andric return Err; 25373ca95b02SDimitry Andric if (!GlobalInits.empty() || !IndirectSymbolInits.empty()) 25388f0fd8f6SDimitry Andric return error("Malformed global initializer set"); 2539f22ef01cSRoman Divacky 2540f22ef01cSRoman Divacky // Look for intrinsic functions which need to be upgraded at some point 25418f0fd8f6SDimitry Andric for (Function &F : *TheModule) { 2542f22ef01cSRoman Divacky Function *NewFn; 25438f0fd8f6SDimitry Andric if (UpgradeIntrinsicFunction(&F, NewFn)) 25443dac3a9bSDimitry Andric UpgradedIntrinsics[&F] = NewFn; 25453ca95b02SDimitry Andric else if (auto Remangled = Intrinsic::remangleIntrinsicFunction(&F)) 25463ca95b02SDimitry Andric // Some types could be renamed during loading if several modules are 25473ca95b02SDimitry Andric // loaded in the same LLVMContext (LTO scenario). In this case we should 25483ca95b02SDimitry Andric // remangle intrinsics names as well. 25493ca95b02SDimitry Andric RemangledIntrinsics[&F] = Remangled.getValue(); 2550f22ef01cSRoman Divacky } 2551f22ef01cSRoman Divacky 2552e580952dSDimitry Andric // Look for global variables which need to be renamed. 25538f0fd8f6SDimitry Andric for (GlobalVariable &GV : TheModule->globals()) 25548f0fd8f6SDimitry Andric UpgradeGlobalVariable(&GV); 255591bc56edSDimitry Andric 2556f22ef01cSRoman Divacky // Force deallocation of memory for these vectors to favor the client that 2557f22ef01cSRoman Divacky // want lazy deserialization. 2558f22ef01cSRoman Divacky std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits); 25593ca95b02SDimitry Andric std::vector<std::pair<GlobalIndirectSymbol*, unsigned> >().swap( 25603ca95b02SDimitry Andric IndirectSymbolInits); 2561d88c1a5aSDimitry Andric return Error::success(); 2562f22ef01cSRoman Divacky } 2563f22ef01cSRoman Divacky 25647d523365SDimitry Andric /// Support for lazy parsing of function bodies. This is required if we 25657d523365SDimitry Andric /// either have an old bitcode file without a VST forward declaration record, 25667d523365SDimitry Andric /// or if we have an anonymous function being materialized, since anonymous 25677d523365SDimitry Andric /// functions do not have a name and are therefore not in the VST. 2568d88c1a5aSDimitry Andric Error BitcodeReader::rememberAndSkipFunctionBodies() { 2569dff0c46cSDimitry Andric Stream.JumpToBit(NextUnreadBit); 25707d523365SDimitry Andric 25717d523365SDimitry Andric if (Stream.AtEndOfStream()) 25727d523365SDimitry Andric return error("Could not find function in stream"); 25737d523365SDimitry Andric 25747d523365SDimitry Andric if (!SeenFirstFunctionBody) 25757d523365SDimitry Andric return error("Trying to materialize functions before seeing function blocks"); 25767d523365SDimitry Andric 25777d523365SDimitry Andric // An old bitcode file with the symbol table at the end would have 25787d523365SDimitry Andric // finished the parse greedily. 25797d523365SDimitry Andric assert(SeenValueSymbolTable); 25807d523365SDimitry Andric 25817d523365SDimitry Andric SmallVector<uint64_t, 64> Record; 25827d523365SDimitry Andric 2583d88c1a5aSDimitry Andric while (true) { 25847d523365SDimitry Andric BitstreamEntry Entry = Stream.advance(); 25857d523365SDimitry Andric switch (Entry.Kind) { 25867d523365SDimitry Andric default: 25877d523365SDimitry Andric return error("Expect SubBlock"); 25887d523365SDimitry Andric case BitstreamEntry::SubBlock: 25897d523365SDimitry Andric switch (Entry.ID) { 25907d523365SDimitry Andric default: 25917d523365SDimitry Andric return error("Expect function block"); 25927d523365SDimitry Andric case bitc::FUNCTION_BLOCK_ID: 2593d88c1a5aSDimitry Andric if (Error Err = rememberAndSkipFunctionBody()) 2594d88c1a5aSDimitry Andric return Err; 25957d523365SDimitry Andric NextUnreadBit = Stream.GetCurrentBitNo(); 2596d88c1a5aSDimitry Andric return Error::success(); 25977d523365SDimitry Andric } 25987d523365SDimitry Andric } 25997d523365SDimitry Andric } 26007d523365SDimitry Andric } 26017d523365SDimitry Andric 2602d88c1a5aSDimitry Andric bool BitcodeReaderBase::readBlockInfo() { 2603d88c1a5aSDimitry Andric Optional<BitstreamBlockInfo> NewBlockInfo = Stream.ReadBlockInfoBlock(); 2604d88c1a5aSDimitry Andric if (!NewBlockInfo) 2605d88c1a5aSDimitry Andric return true; 2606d88c1a5aSDimitry Andric BlockInfo = std::move(*NewBlockInfo); 2607d88c1a5aSDimitry Andric return false; 26087d523365SDimitry Andric } 26097d523365SDimitry Andric 2610d88c1a5aSDimitry Andric Error BitcodeReader::parseModule(uint64_t ResumeBit, 26117d523365SDimitry Andric bool ShouldLazyLoadMetadata) { 26127d523365SDimitry Andric if (ResumeBit) 26137d523365SDimitry Andric Stream.JumpToBit(ResumeBit); 2614dff0c46cSDimitry Andric else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 26158f0fd8f6SDimitry Andric return error("Invalid record"); 2616dff0c46cSDimitry Andric 2617dff0c46cSDimitry Andric SmallVector<uint64_t, 64> Record; 2618dff0c46cSDimitry Andric std::vector<std::string> SectionTable; 2619dff0c46cSDimitry Andric std::vector<std::string> GCTable; 2620dff0c46cSDimitry Andric 2621dff0c46cSDimitry Andric // Read all the records for this module. 2622d88c1a5aSDimitry Andric while (true) { 2623139f7f9bSDimitry Andric BitstreamEntry Entry = Stream.advance(); 2624dff0c46cSDimitry Andric 2625139f7f9bSDimitry Andric switch (Entry.Kind) { 2626139f7f9bSDimitry Andric case BitstreamEntry::Error: 26278f0fd8f6SDimitry Andric return error("Malformed block"); 2628139f7f9bSDimitry Andric case BitstreamEntry::EndBlock: 26298f0fd8f6SDimitry Andric return globalCleanup(); 2630dff0c46cSDimitry Andric 2631139f7f9bSDimitry Andric case BitstreamEntry::SubBlock: 2632139f7f9bSDimitry Andric switch (Entry.ID) { 2633f22ef01cSRoman Divacky default: // Skip unknown content. 2634f22ef01cSRoman Divacky if (Stream.SkipBlock()) 26358f0fd8f6SDimitry Andric return error("Invalid record"); 2636f22ef01cSRoman Divacky break; 2637f22ef01cSRoman Divacky case bitc::BLOCKINFO_BLOCK_ID: 2638d88c1a5aSDimitry Andric if (readBlockInfo()) 26398f0fd8f6SDimitry Andric return error("Malformed block"); 2640f22ef01cSRoman Divacky break; 2641f22ef01cSRoman Divacky case bitc::PARAMATTR_BLOCK_ID: 2642d88c1a5aSDimitry Andric if (Error Err = parseAttributeBlock()) 2643d88c1a5aSDimitry Andric return Err; 2644f22ef01cSRoman Divacky break; 2645139f7f9bSDimitry Andric case bitc::PARAMATTR_GROUP_BLOCK_ID: 2646d88c1a5aSDimitry Andric if (Error Err = parseAttributeGroupBlock()) 2647d88c1a5aSDimitry Andric return Err; 2648139f7f9bSDimitry Andric break; 264917a519f9SDimitry Andric case bitc::TYPE_BLOCK_ID_NEW: 2650d88c1a5aSDimitry Andric if (Error Err = parseTypeTable()) 2651d88c1a5aSDimitry Andric return Err; 2652f22ef01cSRoman Divacky break; 2653f22ef01cSRoman Divacky case bitc::VALUE_SYMTAB_BLOCK_ID: 26547d523365SDimitry Andric if (!SeenValueSymbolTable) { 26557d523365SDimitry Andric // Either this is an old form VST without function index and an 26567d523365SDimitry Andric // associated VST forward declaration record (which would have caused 26577d523365SDimitry Andric // the VST to be jumped to and parsed before it was encountered 26587d523365SDimitry Andric // normally in the stream), or there were no function blocks to 26597d523365SDimitry Andric // trigger an earlier parsing of the VST. 26607d523365SDimitry Andric assert(VSTOffset == 0 || FunctionsWithBodies.empty()); 2661d88c1a5aSDimitry Andric if (Error Err = parseValueSymbolTable()) 2662d88c1a5aSDimitry Andric return Err; 2663dff0c46cSDimitry Andric SeenValueSymbolTable = true; 26647d523365SDimitry Andric } else { 26657d523365SDimitry Andric // We must have had a VST forward declaration record, which caused 26667d523365SDimitry Andric // the parser to jump to and parse the VST earlier. 26677d523365SDimitry Andric assert(VSTOffset > 0); 26687d523365SDimitry Andric if (Stream.SkipBlock()) 26697d523365SDimitry Andric return error("Invalid record"); 26707d523365SDimitry Andric } 2671f22ef01cSRoman Divacky break; 2672f22ef01cSRoman Divacky case bitc::CONSTANTS_BLOCK_ID: 2673d88c1a5aSDimitry Andric if (Error Err = parseConstants()) 2674d88c1a5aSDimitry Andric return Err; 2675d88c1a5aSDimitry Andric if (Error Err = resolveGlobalAndIndirectSymbolInits()) 2676d88c1a5aSDimitry Andric return Err; 2677f22ef01cSRoman Divacky break; 2678f22ef01cSRoman Divacky case bitc::METADATA_BLOCK_ID: 2679d88c1a5aSDimitry Andric if (ShouldLazyLoadMetadata) { 2680d88c1a5aSDimitry Andric if (Error Err = rememberAndSkipMetadata()) 2681d88c1a5aSDimitry Andric return Err; 2682ff0cc061SDimitry Andric break; 2683ff0cc061SDimitry Andric } 2684ff0cc061SDimitry Andric assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata"); 2685d88c1a5aSDimitry Andric if (Error Err = MDLoader->parseModuleMetadata()) 2686d88c1a5aSDimitry Andric return Err; 26877d523365SDimitry Andric break; 26887d523365SDimitry Andric case bitc::METADATA_KIND_BLOCK_ID: 2689d88c1a5aSDimitry Andric if (Error Err = MDLoader->parseMetadataKinds()) 2690d88c1a5aSDimitry Andric return Err; 2691f22ef01cSRoman Divacky break; 2692f22ef01cSRoman Divacky case bitc::FUNCTION_BLOCK_ID: 2693f22ef01cSRoman Divacky // If this is the first function body we've seen, reverse the 2694f22ef01cSRoman Divacky // FunctionsWithBodies list. 2695dff0c46cSDimitry Andric if (!SeenFirstFunctionBody) { 2696f22ef01cSRoman Divacky std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end()); 2697d88c1a5aSDimitry Andric if (Error Err = globalCleanup()) 2698d88c1a5aSDimitry Andric return Err; 2699dff0c46cSDimitry Andric SeenFirstFunctionBody = true; 2700f22ef01cSRoman Divacky } 2701f22ef01cSRoman Divacky 27027d523365SDimitry Andric if (VSTOffset > 0) { 27037d523365SDimitry Andric // If we have a VST forward declaration record, make sure we 27047d523365SDimitry Andric // parse the VST now if we haven't already. It is needed to 27057d523365SDimitry Andric // set up the DeferredFunctionInfo vector for lazy reading. 27067d523365SDimitry Andric if (!SeenValueSymbolTable) { 2707d88c1a5aSDimitry Andric if (Error Err = BitcodeReader::parseValueSymbolTable(VSTOffset)) 2708d88c1a5aSDimitry Andric return Err; 27097d523365SDimitry Andric SeenValueSymbolTable = true; 27107d523365SDimitry Andric // Fall through so that we record the NextUnreadBit below. 27117d523365SDimitry Andric // This is necessary in case we have an anonymous function that 27127d523365SDimitry Andric // is later materialized. Since it will not have a VST entry we 27137d523365SDimitry Andric // need to fall back to the lazy parse to find its offset. 27147d523365SDimitry Andric } else { 27157d523365SDimitry Andric // If we have a VST forward declaration record, but have already 27167d523365SDimitry Andric // parsed the VST (just above, when the first function body was 27177d523365SDimitry Andric // encountered here), then we are resuming the parse after 27187d523365SDimitry Andric // materializing functions. The ResumeBit points to the 27197d523365SDimitry Andric // start of the last function block recorded in the 27207d523365SDimitry Andric // DeferredFunctionInfo map. Skip it. 27217d523365SDimitry Andric if (Stream.SkipBlock()) 27227d523365SDimitry Andric return error("Invalid record"); 27237d523365SDimitry Andric continue; 27247d523365SDimitry Andric } 27257d523365SDimitry Andric } 27267d523365SDimitry Andric 27277d523365SDimitry Andric // Support older bitcode files that did not have the function 27287d523365SDimitry Andric // index in the VST, nor a VST forward declaration record, as 27297d523365SDimitry Andric // well as anonymous functions that do not have VST entries. 27307d523365SDimitry Andric // Build the DeferredFunctionInfo vector on the fly. 2731d88c1a5aSDimitry Andric if (Error Err = rememberAndSkipFunctionBody()) 2732d88c1a5aSDimitry Andric return Err; 27337d523365SDimitry Andric 27343dac3a9bSDimitry Andric // Suspend parsing when we reach the function bodies. Subsequent 27353dac3a9bSDimitry Andric // materialization calls will resume it when necessary. If the bitcode 27363dac3a9bSDimitry Andric // file is old, the symbol table will be at the end instead and will not 27373dac3a9bSDimitry Andric // have been seen yet. In this case, just finish the parse now. 27383dac3a9bSDimitry Andric if (SeenValueSymbolTable) { 2739dff0c46cSDimitry Andric NextUnreadBit = Stream.GetCurrentBitNo(); 2740d88c1a5aSDimitry Andric // After the VST has been parsed, we need to make sure intrinsic name 2741d88c1a5aSDimitry Andric // are auto-upgraded. 2742d88c1a5aSDimitry Andric return globalCleanup(); 2743dff0c46cSDimitry Andric } 2744dff0c46cSDimitry Andric break; 2745dff0c46cSDimitry Andric case bitc::USELIST_BLOCK_ID: 2746d88c1a5aSDimitry Andric if (Error Err = parseUseLists()) 2747d88c1a5aSDimitry Andric return Err; 2748f22ef01cSRoman Divacky break; 27497d523365SDimitry Andric case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID: 2750d88c1a5aSDimitry Andric if (Error Err = parseOperandBundleTags()) 2751d88c1a5aSDimitry Andric return Err; 27527d523365SDimitry Andric break; 2753f22ef01cSRoman Divacky } 2754f22ef01cSRoman Divacky continue; 2755139f7f9bSDimitry Andric 2756139f7f9bSDimitry Andric case BitstreamEntry::Record: 2757139f7f9bSDimitry Andric // The interesting case. 2758139f7f9bSDimitry Andric break; 2759f22ef01cSRoman Divacky } 2760f22ef01cSRoman Divacky 2761f22ef01cSRoman Divacky // Read a record. 27627d523365SDimitry Andric auto BitCode = Stream.readRecord(Entry.ID, Record); 27637d523365SDimitry Andric switch (BitCode) { 2764f22ef01cSRoman Divacky default: break; // Default behavior, ignore unknown content. 27653861d79fSDimitry Andric case bitc::MODULE_CODE_VERSION: { // VERSION: [version#] 2766f22ef01cSRoman Divacky if (Record.size() < 1) 27678f0fd8f6SDimitry Andric return error("Invalid record"); 27683861d79fSDimitry Andric // Only version #0 and #1 are supported so far. 27693861d79fSDimitry Andric unsigned module_version = Record[0]; 27703861d79fSDimitry Andric switch (module_version) { 2771f785676fSDimitry Andric default: 27728f0fd8f6SDimitry Andric return error("Invalid value"); 27733861d79fSDimitry Andric case 0: 27743861d79fSDimitry Andric UseRelativeIDs = false; 2775f22ef01cSRoman Divacky break; 27763861d79fSDimitry Andric case 1: 27773861d79fSDimitry Andric UseRelativeIDs = true; 27783861d79fSDimitry Andric break; 27793861d79fSDimitry Andric } 27803861d79fSDimitry Andric break; 27813861d79fSDimitry Andric } 2782f22ef01cSRoman Divacky case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] 2783f22ef01cSRoman Divacky std::string S; 27848f0fd8f6SDimitry Andric if (convertToString(Record, 0, S)) 27858f0fd8f6SDimitry Andric return error("Invalid record"); 2786f22ef01cSRoman Divacky TheModule->setTargetTriple(S); 2787f22ef01cSRoman Divacky break; 2788f22ef01cSRoman Divacky } 2789f22ef01cSRoman Divacky case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N] 2790f22ef01cSRoman Divacky std::string S; 27918f0fd8f6SDimitry Andric if (convertToString(Record, 0, S)) 27928f0fd8f6SDimitry Andric return error("Invalid record"); 2793f22ef01cSRoman Divacky TheModule->setDataLayout(S); 2794f22ef01cSRoman Divacky break; 2795f22ef01cSRoman Divacky } 2796f22ef01cSRoman Divacky case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N] 2797f22ef01cSRoman Divacky std::string S; 27988f0fd8f6SDimitry Andric if (convertToString(Record, 0, S)) 27998f0fd8f6SDimitry Andric return error("Invalid record"); 2800f22ef01cSRoman Divacky TheModule->setModuleInlineAsm(S); 2801f22ef01cSRoman Divacky break; 2802f22ef01cSRoman Divacky } 2803f22ef01cSRoman Divacky case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N] 2804139f7f9bSDimitry Andric // FIXME: Remove in 4.0. 2805f22ef01cSRoman Divacky std::string S; 28068f0fd8f6SDimitry Andric if (convertToString(Record, 0, S)) 28078f0fd8f6SDimitry Andric return error("Invalid record"); 2808139f7f9bSDimitry Andric // Ignore value. 2809f22ef01cSRoman Divacky break; 2810f22ef01cSRoman Divacky } 2811f22ef01cSRoman Divacky case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] 2812f22ef01cSRoman Divacky std::string S; 28138f0fd8f6SDimitry Andric if (convertToString(Record, 0, S)) 28148f0fd8f6SDimitry Andric return error("Invalid record"); 2815f22ef01cSRoman Divacky SectionTable.push_back(S); 2816f22ef01cSRoman Divacky break; 2817f22ef01cSRoman Divacky } 2818f22ef01cSRoman Divacky case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N] 2819f22ef01cSRoman Divacky std::string S; 28208f0fd8f6SDimitry Andric if (convertToString(Record, 0, S)) 28218f0fd8f6SDimitry Andric return error("Invalid record"); 2822f22ef01cSRoman Divacky GCTable.push_back(S); 2823f22ef01cSRoman Divacky break; 2824f22ef01cSRoman Divacky } 282591bc56edSDimitry Andric case bitc::MODULE_CODE_COMDAT: { // COMDAT: [selection_kind, name] 282691bc56edSDimitry Andric if (Record.size() < 2) 28278f0fd8f6SDimitry Andric return error("Invalid record"); 282891bc56edSDimitry Andric Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]); 282991bc56edSDimitry Andric unsigned ComdatNameSize = Record[1]; 283091bc56edSDimitry Andric std::string ComdatName; 283191bc56edSDimitry Andric ComdatName.reserve(ComdatNameSize); 283291bc56edSDimitry Andric for (unsigned i = 0; i != ComdatNameSize; ++i) 283391bc56edSDimitry Andric ComdatName += (char)Record[2 + i]; 283491bc56edSDimitry Andric Comdat *C = TheModule->getOrInsertComdat(ComdatName); 283591bc56edSDimitry Andric C->setSelectionKind(SK); 283691bc56edSDimitry Andric ComdatList.push_back(C); 283791bc56edSDimitry Andric break; 283891bc56edSDimitry Andric } 2839f22ef01cSRoman Divacky // GLOBALVAR: [pointer type, isconst, initid, 28402754fe60SDimitry Andric // linkage, alignment, section, visibility, threadlocal, 2841ff0cc061SDimitry Andric // unnamed_addr, externally_initialized, dllstorageclass, 2842ff0cc061SDimitry Andric // comdat] 2843f22ef01cSRoman Divacky case bitc::MODULE_CODE_GLOBALVAR: { 2844f22ef01cSRoman Divacky if (Record.size() < 6) 28458f0fd8f6SDimitry Andric return error("Invalid record"); 28466122f3e6SDimitry Andric Type *Ty = getTypeByID(Record[0]); 2847f785676fSDimitry Andric if (!Ty) 28488f0fd8f6SDimitry Andric return error("Invalid record"); 2849ff0cc061SDimitry Andric bool isConstant = Record[1] & 1; 2850ff0cc061SDimitry Andric bool explicitType = Record[1] & 2; 2851ff0cc061SDimitry Andric unsigned AddressSpace; 2852ff0cc061SDimitry Andric if (explicitType) { 2853ff0cc061SDimitry Andric AddressSpace = Record[1] >> 2; 2854ff0cc061SDimitry Andric } else { 2855f22ef01cSRoman Divacky if (!Ty->isPointerTy()) 28568f0fd8f6SDimitry Andric return error("Invalid type for value"); 2857ff0cc061SDimitry Andric AddressSpace = cast<PointerType>(Ty)->getAddressSpace(); 2858f22ef01cSRoman Divacky Ty = cast<PointerType>(Ty)->getElementType(); 2859ff0cc061SDimitry Andric } 2860f22ef01cSRoman Divacky 2861ff0cc061SDimitry Andric uint64_t RawLinkage = Record[3]; 2862ff0cc061SDimitry Andric GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage); 2863ff0cc061SDimitry Andric unsigned Alignment; 2864d88c1a5aSDimitry Andric if (Error Err = parseAlignmentValue(Record[4], Alignment)) 2865d88c1a5aSDimitry Andric return Err; 2866f22ef01cSRoman Divacky std::string Section; 2867f22ef01cSRoman Divacky if (Record[5]) { 2868f22ef01cSRoman Divacky if (Record[5]-1 >= SectionTable.size()) 28698f0fd8f6SDimitry Andric return error("Invalid ID"); 2870f22ef01cSRoman Divacky Section = SectionTable[Record[5]-1]; 2871f22ef01cSRoman Divacky } 2872f22ef01cSRoman Divacky GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility; 287391bc56edSDimitry Andric // Local linkage must have default visibility. 287491bc56edSDimitry Andric if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage)) 287591bc56edSDimitry Andric // FIXME: Change to an error if non-default in 4.0. 28768f0fd8f6SDimitry Andric Visibility = getDecodedVisibility(Record[6]); 28777ae0e2c9SDimitry Andric 28787ae0e2c9SDimitry Andric GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal; 2879f22ef01cSRoman Divacky if (Record.size() > 7) 28808f0fd8f6SDimitry Andric TLM = getDecodedThreadLocalMode(Record[7]); 2881f22ef01cSRoman Divacky 28823ca95b02SDimitry Andric GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None; 28832754fe60SDimitry Andric if (Record.size() > 8) 28843ca95b02SDimitry Andric UnnamedAddr = getDecodedUnnamedAddrType(Record[8]); 28852754fe60SDimitry Andric 2886139f7f9bSDimitry Andric bool ExternallyInitialized = false; 2887139f7f9bSDimitry Andric if (Record.size() > 9) 2888139f7f9bSDimitry Andric ExternallyInitialized = Record[9]; 2889139f7f9bSDimitry Andric 2890f22ef01cSRoman Divacky GlobalVariable *NewGV = 289191bc56edSDimitry Andric new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, "", nullptr, 2892139f7f9bSDimitry Andric TLM, AddressSpace, ExternallyInitialized); 2893f22ef01cSRoman Divacky NewGV->setAlignment(Alignment); 2894f22ef01cSRoman Divacky if (!Section.empty()) 2895f22ef01cSRoman Divacky NewGV->setSection(Section); 2896f22ef01cSRoman Divacky NewGV->setVisibility(Visibility); 28972754fe60SDimitry Andric NewGV->setUnnamedAddr(UnnamedAddr); 2898f22ef01cSRoman Divacky 289991bc56edSDimitry Andric if (Record.size() > 10) 29008f0fd8f6SDimitry Andric NewGV->setDLLStorageClass(getDecodedDLLStorageClass(Record[10])); 290191bc56edSDimitry Andric else 29028f0fd8f6SDimitry Andric upgradeDLLImportExportLinkage(NewGV, RawLinkage); 290391bc56edSDimitry Andric 2904f22ef01cSRoman Divacky ValueList.push_back(NewGV); 2905f22ef01cSRoman Divacky 2906f22ef01cSRoman Divacky // Remember which value to use for the global initializer. 2907f22ef01cSRoman Divacky if (unsigned InitID = Record[2]) 2908f22ef01cSRoman Divacky GlobalInits.push_back(std::make_pair(NewGV, InitID-1)); 290991bc56edSDimitry Andric 2910ff0cc061SDimitry Andric if (Record.size() > 11) { 291191bc56edSDimitry Andric if (unsigned ComdatID = Record[11]) { 2912ff0cc061SDimitry Andric if (ComdatID > ComdatList.size()) 29138f0fd8f6SDimitry Andric return error("Invalid global variable comdat ID"); 291491bc56edSDimitry Andric NewGV->setComdat(ComdatList[ComdatID - 1]); 291591bc56edSDimitry Andric } 2916ff0cc061SDimitry Andric } else if (hasImplicitComdat(RawLinkage)) { 2917ff0cc061SDimitry Andric NewGV->setComdat(reinterpret_cast<Comdat *>(1)); 2918ff0cc061SDimitry Andric } 29193ca95b02SDimitry Andric 2920f22ef01cSRoman Divacky break; 2921f22ef01cSRoman Divacky } 2922f22ef01cSRoman Divacky // FUNCTION: [type, callingconv, isproto, linkage, paramattr, 292391bc56edSDimitry Andric // alignment, section, visibility, gc, unnamed_addr, 292439d628a0SDimitry Andric // prologuedata, dllstorageclass, comdat, prefixdata] 2925f22ef01cSRoman Divacky case bitc::MODULE_CODE_FUNCTION: { 2926f22ef01cSRoman Divacky if (Record.size() < 8) 29278f0fd8f6SDimitry Andric return error("Invalid record"); 29286122f3e6SDimitry Andric Type *Ty = getTypeByID(Record[0]); 2929f785676fSDimitry Andric if (!Ty) 29308f0fd8f6SDimitry Andric return error("Invalid record"); 2931ff0cc061SDimitry Andric if (auto *PTy = dyn_cast<PointerType>(Ty)) 2932ff0cc061SDimitry Andric Ty = PTy->getElementType(); 2933ff0cc061SDimitry Andric auto *FTy = dyn_cast<FunctionType>(Ty); 2934f22ef01cSRoman Divacky if (!FTy) 29358f0fd8f6SDimitry Andric return error("Invalid type for value"); 29367d523365SDimitry Andric auto CC = static_cast<CallingConv::ID>(Record[1]); 29377d523365SDimitry Andric if (CC & ~CallingConv::MaxID) 29387d523365SDimitry Andric return error("Invalid calling convention ID"); 2939f22ef01cSRoman Divacky 2940f22ef01cSRoman Divacky Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage, 2941f22ef01cSRoman Divacky "", TheModule); 2942f22ef01cSRoman Divacky 29437d523365SDimitry Andric Func->setCallingConv(CC); 2944f22ef01cSRoman Divacky bool isProto = Record[2]; 2945ff0cc061SDimitry Andric uint64_t RawLinkage = Record[3]; 2946ff0cc061SDimitry Andric Func->setLinkage(getDecodedLinkage(RawLinkage)); 2947f22ef01cSRoman Divacky Func->setAttributes(getAttributes(Record[4])); 2948f22ef01cSRoman Divacky 2949ff0cc061SDimitry Andric unsigned Alignment; 2950d88c1a5aSDimitry Andric if (Error Err = parseAlignmentValue(Record[5], Alignment)) 2951d88c1a5aSDimitry Andric return Err; 2952ff0cc061SDimitry Andric Func->setAlignment(Alignment); 2953f22ef01cSRoman Divacky if (Record[6]) { 2954f22ef01cSRoman Divacky if (Record[6]-1 >= SectionTable.size()) 29558f0fd8f6SDimitry Andric return error("Invalid ID"); 2956f22ef01cSRoman Divacky Func->setSection(SectionTable[Record[6]-1]); 2957f22ef01cSRoman Divacky } 295891bc56edSDimitry Andric // Local linkage must have default visibility. 295991bc56edSDimitry Andric if (!Func->hasLocalLinkage()) 296091bc56edSDimitry Andric // FIXME: Change to an error if non-default in 4.0. 29618f0fd8f6SDimitry Andric Func->setVisibility(getDecodedVisibility(Record[7])); 2962f22ef01cSRoman Divacky if (Record.size() > 8 && Record[8]) { 2963ff0cc061SDimitry Andric if (Record[8]-1 >= GCTable.size()) 29648f0fd8f6SDimitry Andric return error("Invalid ID"); 29653ca95b02SDimitry Andric Func->setGC(GCTable[Record[8] - 1]); 2966f22ef01cSRoman Divacky } 29673ca95b02SDimitry Andric GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None; 29682754fe60SDimitry Andric if (Record.size() > 9) 29693ca95b02SDimitry Andric UnnamedAddr = getDecodedUnnamedAddrType(Record[9]); 29702754fe60SDimitry Andric Func->setUnnamedAddr(UnnamedAddr); 2971f785676fSDimitry Andric if (Record.size() > 10 && Record[10] != 0) 297239d628a0SDimitry Andric FunctionPrologues.push_back(std::make_pair(Func, Record[10]-1)); 297391bc56edSDimitry Andric 297491bc56edSDimitry Andric if (Record.size() > 11) 29758f0fd8f6SDimitry Andric Func->setDLLStorageClass(getDecodedDLLStorageClass(Record[11])); 297691bc56edSDimitry Andric else 29778f0fd8f6SDimitry Andric upgradeDLLImportExportLinkage(Func, RawLinkage); 297891bc56edSDimitry Andric 2979ff0cc061SDimitry Andric if (Record.size() > 12) { 298091bc56edSDimitry Andric if (unsigned ComdatID = Record[12]) { 2981ff0cc061SDimitry Andric if (ComdatID > ComdatList.size()) 29828f0fd8f6SDimitry Andric return error("Invalid function comdat ID"); 298391bc56edSDimitry Andric Func->setComdat(ComdatList[ComdatID - 1]); 298491bc56edSDimitry Andric } 2985ff0cc061SDimitry Andric } else if (hasImplicitComdat(RawLinkage)) { 2986ff0cc061SDimitry Andric Func->setComdat(reinterpret_cast<Comdat *>(1)); 2987ff0cc061SDimitry Andric } 298891bc56edSDimitry Andric 298939d628a0SDimitry Andric if (Record.size() > 13 && Record[13] != 0) 299039d628a0SDimitry Andric FunctionPrefixes.push_back(std::make_pair(Func, Record[13]-1)); 299139d628a0SDimitry Andric 29928f0fd8f6SDimitry Andric if (Record.size() > 14 && Record[14] != 0) 29938f0fd8f6SDimitry Andric FunctionPersonalityFns.push_back(std::make_pair(Func, Record[14] - 1)); 29948f0fd8f6SDimitry Andric 2995f22ef01cSRoman Divacky ValueList.push_back(Func); 2996f22ef01cSRoman Divacky 2997f22ef01cSRoman Divacky // If this is a function with a body, remember the prototype we are 2998f22ef01cSRoman Divacky // creating now, so that we can match up the body with them later. 2999dff0c46cSDimitry Andric if (!isProto) { 300039d628a0SDimitry Andric Func->setIsMaterializable(true); 3001f22ef01cSRoman Divacky FunctionsWithBodies.push_back(Func); 300239d628a0SDimitry Andric DeferredFunctionInfo[Func] = 0; 3003dff0c46cSDimitry Andric } 3004f22ef01cSRoman Divacky break; 3005f22ef01cSRoman Divacky } 30067d523365SDimitry Andric // ALIAS: [alias type, addrspace, aliasee val#, linkage] 30077d523365SDimitry Andric // ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility, dllstorageclass] 30083ca95b02SDimitry Andric // IFUNC: [alias type, addrspace, aliasee val#, linkage, visibility, dllstorageclass] 30093ca95b02SDimitry Andric case bitc::MODULE_CODE_IFUNC: 30107d523365SDimitry Andric case bitc::MODULE_CODE_ALIAS: 30117d523365SDimitry Andric case bitc::MODULE_CODE_ALIAS_OLD: { 30123ca95b02SDimitry Andric bool NewRecord = BitCode != bitc::MODULE_CODE_ALIAS_OLD; 30137d523365SDimitry Andric if (Record.size() < (3 + (unsigned)NewRecord)) 30148f0fd8f6SDimitry Andric return error("Invalid record"); 30157d523365SDimitry Andric unsigned OpNum = 0; 30167d523365SDimitry Andric Type *Ty = getTypeByID(Record[OpNum++]); 3017f785676fSDimitry Andric if (!Ty) 30188f0fd8f6SDimitry Andric return error("Invalid record"); 30197d523365SDimitry Andric 30207d523365SDimitry Andric unsigned AddrSpace; 30217d523365SDimitry Andric if (!NewRecord) { 302291bc56edSDimitry Andric auto *PTy = dyn_cast<PointerType>(Ty); 302391bc56edSDimitry Andric if (!PTy) 30248f0fd8f6SDimitry Andric return error("Invalid type for value"); 30257d523365SDimitry Andric Ty = PTy->getElementType(); 30267d523365SDimitry Andric AddrSpace = PTy->getAddressSpace(); 30277d523365SDimitry Andric } else { 30287d523365SDimitry Andric AddrSpace = Record[OpNum++]; 30297d523365SDimitry Andric } 3030f22ef01cSRoman Divacky 30317d523365SDimitry Andric auto Val = Record[OpNum++]; 30327d523365SDimitry Andric auto Linkage = Record[OpNum++]; 30333ca95b02SDimitry Andric GlobalIndirectSymbol *NewGA; 30343ca95b02SDimitry Andric if (BitCode == bitc::MODULE_CODE_ALIAS || 30353ca95b02SDimitry Andric BitCode == bitc::MODULE_CODE_ALIAS_OLD) 30363ca95b02SDimitry Andric NewGA = GlobalAlias::create(Ty, AddrSpace, getDecodedLinkage(Linkage), 30373ca95b02SDimitry Andric "", TheModule); 30383ca95b02SDimitry Andric else 30393ca95b02SDimitry Andric NewGA = GlobalIFunc::create(Ty, AddrSpace, getDecodedLinkage(Linkage), 30403ca95b02SDimitry Andric "", nullptr, TheModule); 3041f22ef01cSRoman Divacky // Old bitcode files didn't have visibility field. 304291bc56edSDimitry Andric // Local linkage must have default visibility. 30437d523365SDimitry Andric if (OpNum != Record.size()) { 30447d523365SDimitry Andric auto VisInd = OpNum++; 30457d523365SDimitry Andric if (!NewGA->hasLocalLinkage()) 304691bc56edSDimitry Andric // FIXME: Change to an error if non-default in 4.0. 30477d523365SDimitry Andric NewGA->setVisibility(getDecodedVisibility(Record[VisInd])); 30487d523365SDimitry Andric } 30497d523365SDimitry Andric if (OpNum != Record.size()) 30507d523365SDimitry Andric NewGA->setDLLStorageClass(getDecodedDLLStorageClass(Record[OpNum++])); 305191bc56edSDimitry Andric else 30527d523365SDimitry Andric upgradeDLLImportExportLinkage(NewGA, Linkage); 30537d523365SDimitry Andric if (OpNum != Record.size()) 30547d523365SDimitry Andric NewGA->setThreadLocalMode(getDecodedThreadLocalMode(Record[OpNum++])); 30557d523365SDimitry Andric if (OpNum != Record.size()) 30563ca95b02SDimitry Andric NewGA->setUnnamedAddr(getDecodedUnnamedAddrType(Record[OpNum++])); 3057f22ef01cSRoman Divacky ValueList.push_back(NewGA); 30583ca95b02SDimitry Andric IndirectSymbolInits.push_back(std::make_pair(NewGA, Val)); 3059f22ef01cSRoman Divacky break; 3060f22ef01cSRoman Divacky } 3061f22ef01cSRoman Divacky /// MODULE_CODE_PURGEVALS: [numvals] 3062f22ef01cSRoman Divacky case bitc::MODULE_CODE_PURGEVALS: 3063f22ef01cSRoman Divacky // Trim down the value list to the specified size. 3064f22ef01cSRoman Divacky if (Record.size() < 1 || Record[0] > ValueList.size()) 30658f0fd8f6SDimitry Andric return error("Invalid record"); 3066f22ef01cSRoman Divacky ValueList.shrinkTo(Record[0]); 3067f22ef01cSRoman Divacky break; 30687d523365SDimitry Andric /// MODULE_CODE_VSTOFFSET: [offset] 30697d523365SDimitry Andric case bitc::MODULE_CODE_VSTOFFSET: 30707d523365SDimitry Andric if (Record.size() < 1) 30717d523365SDimitry Andric return error("Invalid record"); 3072d88c1a5aSDimitry Andric // Note that we subtract 1 here because the offset is relative to one word 3073d88c1a5aSDimitry Andric // before the start of the identification or module block, which was 3074d88c1a5aSDimitry Andric // historically always the start of the regular bitcode header. 3075d88c1a5aSDimitry Andric VSTOffset = Record[0] - 1; 30767d523365SDimitry Andric break; 30773ca95b02SDimitry Andric /// MODULE_CODE_SOURCE_FILENAME: [namechar x N] 30783ca95b02SDimitry Andric case bitc::MODULE_CODE_SOURCE_FILENAME: 30793ca95b02SDimitry Andric SmallString<128> ValueName; 30803ca95b02SDimitry Andric if (convertToString(Record, 0, ValueName)) 30817d523365SDimitry Andric return error("Invalid record"); 30823ca95b02SDimitry Andric TheModule->setSourceFileName(ValueName); 30837d523365SDimitry Andric break; 3084f22ef01cSRoman Divacky } 3085f22ef01cSRoman Divacky Record.clear(); 3086f22ef01cSRoman Divacky } 3087f22ef01cSRoman Divacky } 3088f22ef01cSRoman Divacky 3089d88c1a5aSDimitry Andric Error BitcodeReader::parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata, 3090d88c1a5aSDimitry Andric bool IsImporting) { 30918f0fd8f6SDimitry Andric TheModule = M; 3092d88c1a5aSDimitry Andric MDLoader = MetadataLoader(Stream, *M, ValueList, IsImporting, 3093d88c1a5aSDimitry Andric [&](unsigned ID) { return getTypeByID(ID); }); 30947d523365SDimitry Andric return parseModule(0, ShouldLazyLoadMetadata); 3095f22ef01cSRoman Divacky } 3096f22ef01cSRoman Divacky 30972754fe60SDimitry Andric 3098d88c1a5aSDimitry Andric Error BitcodeReader::typeCheckLoadStoreInst(Type *ValType, Type *PtrType) { 3099ff0cc061SDimitry Andric if (!isa<PointerType>(PtrType)) 3100d88c1a5aSDimitry Andric return error("Load/Store operand is not a pointer type"); 3101ff0cc061SDimitry Andric Type *ElemType = cast<PointerType>(PtrType)->getElementType(); 3102ff0cc061SDimitry Andric 3103ff0cc061SDimitry Andric if (ValType && ValType != ElemType) 3104d88c1a5aSDimitry Andric return error("Explicit load/store type does not match pointee " 31057d523365SDimitry Andric "type of pointer operand"); 3106ff0cc061SDimitry Andric if (!PointerType::isLoadableOrStorableType(ElemType)) 3107d88c1a5aSDimitry Andric return error("Cannot load/store from pointer"); 3108d88c1a5aSDimitry Andric return Error::success(); 3109ff0cc061SDimitry Andric } 3110ff0cc061SDimitry Andric 31118f0fd8f6SDimitry Andric /// Lazily parse the specified function body block. 3112d88c1a5aSDimitry Andric Error BitcodeReader::parseFunctionBody(Function *F) { 3113f22ef01cSRoman Divacky if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID)) 31148f0fd8f6SDimitry Andric return error("Invalid record"); 3115f22ef01cSRoman Divacky 31163ca95b02SDimitry Andric // Unexpected unresolved metadata when parsing function. 3117d88c1a5aSDimitry Andric if (MDLoader->hasFwdRefs()) 31183ca95b02SDimitry Andric return error("Invalid function metadata: incoming forward references"); 31193ca95b02SDimitry Andric 3120f22ef01cSRoman Divacky InstructionList.clear(); 3121f22ef01cSRoman Divacky unsigned ModuleValueListSize = ValueList.size(); 3122d88c1a5aSDimitry Andric unsigned ModuleMDLoaderSize = MDLoader->size(); 3123f22ef01cSRoman Divacky 3124f22ef01cSRoman Divacky // Add all the function arguments to the value table. 31257d523365SDimitry Andric for (Argument &I : F->args()) 31267d523365SDimitry Andric ValueList.push_back(&I); 3127f22ef01cSRoman Divacky 3128f22ef01cSRoman Divacky unsigned NextValueNo = ValueList.size(); 312991bc56edSDimitry Andric BasicBlock *CurBB = nullptr; 3130f22ef01cSRoman Divacky unsigned CurBBNo = 0; 3131f22ef01cSRoman Divacky 3132f22ef01cSRoman Divacky DebugLoc LastLoc; 313339d628a0SDimitry Andric auto getLastInstruction = [&]() -> Instruction * { 313439d628a0SDimitry Andric if (CurBB && !CurBB->empty()) 313539d628a0SDimitry Andric return &CurBB->back(); 313639d628a0SDimitry Andric else if (CurBBNo && FunctionBBs[CurBBNo - 1] && 313739d628a0SDimitry Andric !FunctionBBs[CurBBNo - 1]->empty()) 313839d628a0SDimitry Andric return &FunctionBBs[CurBBNo - 1]->back(); 313939d628a0SDimitry Andric return nullptr; 314039d628a0SDimitry Andric }; 3141f22ef01cSRoman Divacky 31427d523365SDimitry Andric std::vector<OperandBundleDef> OperandBundles; 31437d523365SDimitry Andric 3144f22ef01cSRoman Divacky // Read all the records. 3145f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 3146d88c1a5aSDimitry Andric 3147d88c1a5aSDimitry Andric while (true) { 3148139f7f9bSDimitry Andric BitstreamEntry Entry = Stream.advance(); 3149f22ef01cSRoman Divacky 3150139f7f9bSDimitry Andric switch (Entry.Kind) { 3151139f7f9bSDimitry Andric case BitstreamEntry::Error: 31528f0fd8f6SDimitry Andric return error("Malformed block"); 3153139f7f9bSDimitry Andric case BitstreamEntry::EndBlock: 3154139f7f9bSDimitry Andric goto OutOfRecordLoop; 3155139f7f9bSDimitry Andric 3156139f7f9bSDimitry Andric case BitstreamEntry::SubBlock: 3157139f7f9bSDimitry Andric switch (Entry.ID) { 3158f22ef01cSRoman Divacky default: // Skip unknown content. 3159f22ef01cSRoman Divacky if (Stream.SkipBlock()) 31608f0fd8f6SDimitry Andric return error("Invalid record"); 3161f22ef01cSRoman Divacky break; 3162f22ef01cSRoman Divacky case bitc::CONSTANTS_BLOCK_ID: 3163d88c1a5aSDimitry Andric if (Error Err = parseConstants()) 3164d88c1a5aSDimitry Andric return Err; 3165f22ef01cSRoman Divacky NextValueNo = ValueList.size(); 3166f22ef01cSRoman Divacky break; 3167f22ef01cSRoman Divacky case bitc::VALUE_SYMTAB_BLOCK_ID: 3168d88c1a5aSDimitry Andric if (Error Err = parseValueSymbolTable()) 3169d88c1a5aSDimitry Andric return Err; 3170f22ef01cSRoman Divacky break; 3171f22ef01cSRoman Divacky case bitc::METADATA_ATTACHMENT_ID: 3172d88c1a5aSDimitry Andric if (Error Err = MDLoader->parseMetadataAttachment(*F, InstructionList)) 3173d88c1a5aSDimitry Andric return Err; 3174f22ef01cSRoman Divacky break; 3175f22ef01cSRoman Divacky case bitc::METADATA_BLOCK_ID: 3176d88c1a5aSDimitry Andric assert(DeferredMetadataInfo.empty() && 3177d88c1a5aSDimitry Andric "Must read all module-level metadata before function-level"); 3178d88c1a5aSDimitry Andric if (Error Err = MDLoader->parseFunctionMetadata()) 3179d88c1a5aSDimitry Andric return Err; 3180f22ef01cSRoman Divacky break; 318139d628a0SDimitry Andric case bitc::USELIST_BLOCK_ID: 3182d88c1a5aSDimitry Andric if (Error Err = parseUseLists()) 3183d88c1a5aSDimitry Andric return Err; 318439d628a0SDimitry Andric break; 3185f22ef01cSRoman Divacky } 3186f22ef01cSRoman Divacky continue; 3187f22ef01cSRoman Divacky 3188139f7f9bSDimitry Andric case BitstreamEntry::Record: 3189139f7f9bSDimitry Andric // The interesting case. 3190139f7f9bSDimitry Andric break; 3191f22ef01cSRoman Divacky } 3192f22ef01cSRoman Divacky 3193f22ef01cSRoman Divacky // Read a record. 3194f22ef01cSRoman Divacky Record.clear(); 319591bc56edSDimitry Andric Instruction *I = nullptr; 3196139f7f9bSDimitry Andric unsigned BitCode = Stream.readRecord(Entry.ID, Record); 3197f22ef01cSRoman Divacky switch (BitCode) { 3198f22ef01cSRoman Divacky default: // Default behavior: reject 31998f0fd8f6SDimitry Andric return error("Invalid value"); 320039d628a0SDimitry Andric case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks] 3201f22ef01cSRoman Divacky if (Record.size() < 1 || Record[0] == 0) 32028f0fd8f6SDimitry Andric return error("Invalid record"); 3203f22ef01cSRoman Divacky // Create all the basic blocks for the function. 3204f22ef01cSRoman Divacky FunctionBBs.resize(Record[0]); 320539d628a0SDimitry Andric 320639d628a0SDimitry Andric // See if anything took the address of blocks in this function. 320739d628a0SDimitry Andric auto BBFRI = BasicBlockFwdRefs.find(F); 320839d628a0SDimitry Andric if (BBFRI == BasicBlockFwdRefs.end()) { 3209f22ef01cSRoman Divacky for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i) 3210f22ef01cSRoman Divacky FunctionBBs[i] = BasicBlock::Create(Context, "", F); 321139d628a0SDimitry Andric } else { 321239d628a0SDimitry Andric auto &BBRefs = BBFRI->second; 321339d628a0SDimitry Andric // Check for invalid basic block references. 321439d628a0SDimitry Andric if (BBRefs.size() > FunctionBBs.size()) 32158f0fd8f6SDimitry Andric return error("Invalid ID"); 321639d628a0SDimitry Andric assert(!BBRefs.empty() && "Unexpected empty array"); 321739d628a0SDimitry Andric assert(!BBRefs.front() && "Invalid reference to entry block"); 321839d628a0SDimitry Andric for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E; 321939d628a0SDimitry Andric ++I) 322039d628a0SDimitry Andric if (I < RE && BBRefs[I]) { 322139d628a0SDimitry Andric BBRefs[I]->insertInto(F); 322239d628a0SDimitry Andric FunctionBBs[I] = BBRefs[I]; 322339d628a0SDimitry Andric } else { 322439d628a0SDimitry Andric FunctionBBs[I] = BasicBlock::Create(Context, "", F); 322539d628a0SDimitry Andric } 322639d628a0SDimitry Andric 322739d628a0SDimitry Andric // Erase from the table. 322839d628a0SDimitry Andric BasicBlockFwdRefs.erase(BBFRI); 322939d628a0SDimitry Andric } 323039d628a0SDimitry Andric 3231f22ef01cSRoman Divacky CurBB = FunctionBBs[0]; 3232f22ef01cSRoman Divacky continue; 323339d628a0SDimitry Andric } 3234f22ef01cSRoman Divacky 3235f22ef01cSRoman Divacky case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN 3236f22ef01cSRoman Divacky // This record indicates that the last instruction is at the same 3237f22ef01cSRoman Divacky // location as the previous instruction with a location. 323839d628a0SDimitry Andric I = getLastInstruction(); 3239f22ef01cSRoman Divacky 324091bc56edSDimitry Andric if (!I) 32418f0fd8f6SDimitry Andric return error("Invalid record"); 3242f22ef01cSRoman Divacky I->setDebugLoc(LastLoc); 324391bc56edSDimitry Andric I = nullptr; 3244f22ef01cSRoman Divacky continue; 3245f22ef01cSRoman Divacky 324617a519f9SDimitry Andric case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia] 324739d628a0SDimitry Andric I = getLastInstruction(); 324891bc56edSDimitry Andric if (!I || Record.size() < 4) 32498f0fd8f6SDimitry Andric return error("Invalid record"); 3250f22ef01cSRoman Divacky 3251f22ef01cSRoman Divacky unsigned Line = Record[0], Col = Record[1]; 3252f22ef01cSRoman Divacky unsigned ScopeID = Record[2], IAID = Record[3]; 3253f22ef01cSRoman Divacky 325491bc56edSDimitry Andric MDNode *Scope = nullptr, *IA = nullptr; 32553ca95b02SDimitry Andric if (ScopeID) { 3256d88c1a5aSDimitry Andric Scope = MDLoader->getMDNodeFwdRefOrNull(ScopeID - 1); 32573ca95b02SDimitry Andric if (!Scope) 32583ca95b02SDimitry Andric return error("Invalid record"); 32593ca95b02SDimitry Andric } 32603ca95b02SDimitry Andric if (IAID) { 3261d88c1a5aSDimitry Andric IA = MDLoader->getMDNodeFwdRefOrNull(IAID - 1); 32623ca95b02SDimitry Andric if (!IA) 32633ca95b02SDimitry Andric return error("Invalid record"); 32643ca95b02SDimitry Andric } 3265f22ef01cSRoman Divacky LastLoc = DebugLoc::get(Line, Col, Scope, IA); 3266f22ef01cSRoman Divacky I->setDebugLoc(LastLoc); 326791bc56edSDimitry Andric I = nullptr; 3268f22ef01cSRoman Divacky continue; 3269f22ef01cSRoman Divacky } 3270f22ef01cSRoman Divacky 3271f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode] 3272f22ef01cSRoman Divacky unsigned OpNum = 0; 3273f22ef01cSRoman Divacky Value *LHS, *RHS; 3274f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || 32753861d79fSDimitry Andric popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) || 3276f22ef01cSRoman Divacky OpNum+1 > Record.size()) 32778f0fd8f6SDimitry Andric return error("Invalid record"); 3278f22ef01cSRoman Divacky 32798f0fd8f6SDimitry Andric int Opc = getDecodedBinaryOpcode(Record[OpNum++], LHS->getType()); 3280f785676fSDimitry Andric if (Opc == -1) 32818f0fd8f6SDimitry Andric return error("Invalid record"); 3282f22ef01cSRoman Divacky I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); 3283f22ef01cSRoman Divacky InstructionList.push_back(I); 3284f22ef01cSRoman Divacky if (OpNum < Record.size()) { 3285f22ef01cSRoman Divacky if (Opc == Instruction::Add || 3286f22ef01cSRoman Divacky Opc == Instruction::Sub || 32872754fe60SDimitry Andric Opc == Instruction::Mul || 32882754fe60SDimitry Andric Opc == Instruction::Shl) { 3289f22ef01cSRoman Divacky if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP)) 3290f22ef01cSRoman Divacky cast<BinaryOperator>(I)->setHasNoSignedWrap(true); 3291f22ef01cSRoman Divacky if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) 3292f22ef01cSRoman Divacky cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true); 32932754fe60SDimitry Andric } else if (Opc == Instruction::SDiv || 32942754fe60SDimitry Andric Opc == Instruction::UDiv || 32952754fe60SDimitry Andric Opc == Instruction::LShr || 32962754fe60SDimitry Andric Opc == Instruction::AShr) { 32972754fe60SDimitry Andric if (Record[OpNum] & (1 << bitc::PEO_EXACT)) 3298f22ef01cSRoman Divacky cast<BinaryOperator>(I)->setIsExact(true); 3299139f7f9bSDimitry Andric } else if (isa<FPMathOperator>(I)) { 3300875ed548SDimitry Andric FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]); 3301139f7f9bSDimitry Andric if (FMF.any()) 3302139f7f9bSDimitry Andric I->setFastMathFlags(FMF); 3303f22ef01cSRoman Divacky } 3304139f7f9bSDimitry Andric 3305f22ef01cSRoman Divacky } 3306f22ef01cSRoman Divacky break; 3307f22ef01cSRoman Divacky } 3308f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc] 3309f22ef01cSRoman Divacky unsigned OpNum = 0; 3310f22ef01cSRoman Divacky Value *Op; 3311f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Op) || 3312f22ef01cSRoman Divacky OpNum+2 != Record.size()) 33138f0fd8f6SDimitry Andric return error("Invalid record"); 3314f22ef01cSRoman Divacky 33156122f3e6SDimitry Andric Type *ResTy = getTypeByID(Record[OpNum]); 33168f0fd8f6SDimitry Andric int Opc = getDecodedCastOpcode(Record[OpNum + 1]); 331791bc56edSDimitry Andric if (Opc == -1 || !ResTy) 33188f0fd8f6SDimitry Andric return error("Invalid record"); 331991bc56edSDimitry Andric Instruction *Temp = nullptr; 3320f785676fSDimitry Andric if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) { 3321f785676fSDimitry Andric if (Temp) { 3322f785676fSDimitry Andric InstructionList.push_back(Temp); 3323f785676fSDimitry Andric CurBB->getInstList().push_back(Temp); 3324f785676fSDimitry Andric } 3325f785676fSDimitry Andric } else { 33267d523365SDimitry Andric auto CastOp = (Instruction::CastOps)Opc; 33277d523365SDimitry Andric if (!CastInst::castIsValid(CastOp, Op, ResTy)) 33287d523365SDimitry Andric return error("Invalid cast"); 33297d523365SDimitry Andric I = CastInst::Create(CastOp, Op, ResTy); 3330f785676fSDimitry Andric } 3331f22ef01cSRoman Divacky InstructionList.push_back(I); 3332f22ef01cSRoman Divacky break; 3333f22ef01cSRoman Divacky } 3334ff0cc061SDimitry Andric case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD: 3335ff0cc061SDimitry Andric case bitc::FUNC_CODE_INST_GEP_OLD: 3336ff0cc061SDimitry Andric case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands] 3337f22ef01cSRoman Divacky unsigned OpNum = 0; 3338ff0cc061SDimitry Andric 3339ff0cc061SDimitry Andric Type *Ty; 3340ff0cc061SDimitry Andric bool InBounds; 3341ff0cc061SDimitry Andric 3342ff0cc061SDimitry Andric if (BitCode == bitc::FUNC_CODE_INST_GEP) { 3343ff0cc061SDimitry Andric InBounds = Record[OpNum++]; 3344ff0cc061SDimitry Andric Ty = getTypeByID(Record[OpNum++]); 3345ff0cc061SDimitry Andric } else { 3346ff0cc061SDimitry Andric InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD; 3347ff0cc061SDimitry Andric Ty = nullptr; 3348ff0cc061SDimitry Andric } 3349ff0cc061SDimitry Andric 3350f22ef01cSRoman Divacky Value *BasePtr; 3351f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr)) 33528f0fd8f6SDimitry Andric return error("Invalid record"); 3353f22ef01cSRoman Divacky 3354ff0cc061SDimitry Andric if (!Ty) 3355d88c1a5aSDimitry Andric Ty = cast<PointerType>(BasePtr->getType()->getScalarType()) 3356ff0cc061SDimitry Andric ->getElementType(); 3357ff0cc061SDimitry Andric else if (Ty != 3358d88c1a5aSDimitry Andric cast<PointerType>(BasePtr->getType()->getScalarType()) 3359ff0cc061SDimitry Andric ->getElementType()) 33608f0fd8f6SDimitry Andric return error( 3361ff0cc061SDimitry Andric "Explicit gep type does not match pointee type of pointer operand"); 3362ff0cc061SDimitry Andric 3363f22ef01cSRoman Divacky SmallVector<Value*, 16> GEPIdx; 3364f22ef01cSRoman Divacky while (OpNum != Record.size()) { 3365f22ef01cSRoman Divacky Value *Op; 3366f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Op)) 33678f0fd8f6SDimitry Andric return error("Invalid record"); 3368f22ef01cSRoman Divacky GEPIdx.push_back(Op); 3369f22ef01cSRoman Divacky } 3370f22ef01cSRoman Divacky 3371ff0cc061SDimitry Andric I = GetElementPtrInst::Create(Ty, BasePtr, GEPIdx); 3372ff0cc061SDimitry Andric 3373f22ef01cSRoman Divacky InstructionList.push_back(I); 3374ff0cc061SDimitry Andric if (InBounds) 3375f22ef01cSRoman Divacky cast<GetElementPtrInst>(I)->setIsInBounds(true); 3376f22ef01cSRoman Divacky break; 3377f22ef01cSRoman Divacky } 3378f22ef01cSRoman Divacky 3379f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_EXTRACTVAL: { 3380f22ef01cSRoman Divacky // EXTRACTVAL: [opty, opval, n x indices] 3381f22ef01cSRoman Divacky unsigned OpNum = 0; 3382f22ef01cSRoman Divacky Value *Agg; 3383f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) 33848f0fd8f6SDimitry Andric return error("Invalid record"); 3385f22ef01cSRoman Divacky 3386ff0cc061SDimitry Andric unsigned RecSize = Record.size(); 3387ff0cc061SDimitry Andric if (OpNum == RecSize) 33888f0fd8f6SDimitry Andric return error("EXTRACTVAL: Invalid instruction with 0 indices"); 3389ff0cc061SDimitry Andric 3390f22ef01cSRoman Divacky SmallVector<unsigned, 4> EXTRACTVALIdx; 3391ff0cc061SDimitry Andric Type *CurTy = Agg->getType(); 3392ff0cc061SDimitry Andric for (; OpNum != RecSize; ++OpNum) { 3393ff0cc061SDimitry Andric bool IsArray = CurTy->isArrayTy(); 3394ff0cc061SDimitry Andric bool IsStruct = CurTy->isStructTy(); 3395f22ef01cSRoman Divacky uint64_t Index = Record[OpNum]; 3396ff0cc061SDimitry Andric 3397ff0cc061SDimitry Andric if (!IsStruct && !IsArray) 33988f0fd8f6SDimitry Andric return error("EXTRACTVAL: Invalid type"); 3399f22ef01cSRoman Divacky if ((unsigned)Index != Index) 34008f0fd8f6SDimitry Andric return error("Invalid value"); 3401ff0cc061SDimitry Andric if (IsStruct && Index >= CurTy->subtypes().size()) 34028f0fd8f6SDimitry Andric return error("EXTRACTVAL: Invalid struct index"); 3403ff0cc061SDimitry Andric if (IsArray && Index >= CurTy->getArrayNumElements()) 34048f0fd8f6SDimitry Andric return error("EXTRACTVAL: Invalid array index"); 3405f22ef01cSRoman Divacky EXTRACTVALIdx.push_back((unsigned)Index); 3406ff0cc061SDimitry Andric 3407ff0cc061SDimitry Andric if (IsStruct) 3408ff0cc061SDimitry Andric CurTy = CurTy->subtypes()[Index]; 3409ff0cc061SDimitry Andric else 3410ff0cc061SDimitry Andric CurTy = CurTy->subtypes()[0]; 3411f22ef01cSRoman Divacky } 3412f22ef01cSRoman Divacky 341317a519f9SDimitry Andric I = ExtractValueInst::Create(Agg, EXTRACTVALIdx); 3414f22ef01cSRoman Divacky InstructionList.push_back(I); 3415f22ef01cSRoman Divacky break; 3416f22ef01cSRoman Divacky } 3417f22ef01cSRoman Divacky 3418f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_INSERTVAL: { 3419f22ef01cSRoman Divacky // INSERTVAL: [opty, opval, opty, opval, n x indices] 3420f22ef01cSRoman Divacky unsigned OpNum = 0; 3421f22ef01cSRoman Divacky Value *Agg; 3422f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) 34238f0fd8f6SDimitry Andric return error("Invalid record"); 3424f22ef01cSRoman Divacky Value *Val; 3425f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Val)) 34268f0fd8f6SDimitry Andric return error("Invalid record"); 3427f22ef01cSRoman Divacky 3428ff0cc061SDimitry Andric unsigned RecSize = Record.size(); 3429ff0cc061SDimitry Andric if (OpNum == RecSize) 34308f0fd8f6SDimitry Andric return error("INSERTVAL: Invalid instruction with 0 indices"); 3431ff0cc061SDimitry Andric 3432f22ef01cSRoman Divacky SmallVector<unsigned, 4> INSERTVALIdx; 3433ff0cc061SDimitry Andric Type *CurTy = Agg->getType(); 3434ff0cc061SDimitry Andric for (; OpNum != RecSize; ++OpNum) { 3435ff0cc061SDimitry Andric bool IsArray = CurTy->isArrayTy(); 3436ff0cc061SDimitry Andric bool IsStruct = CurTy->isStructTy(); 3437f22ef01cSRoman Divacky uint64_t Index = Record[OpNum]; 3438ff0cc061SDimitry Andric 3439ff0cc061SDimitry Andric if (!IsStruct && !IsArray) 34408f0fd8f6SDimitry Andric return error("INSERTVAL: Invalid type"); 3441f22ef01cSRoman Divacky if ((unsigned)Index != Index) 34428f0fd8f6SDimitry Andric return error("Invalid value"); 3443ff0cc061SDimitry Andric if (IsStruct && Index >= CurTy->subtypes().size()) 34448f0fd8f6SDimitry Andric return error("INSERTVAL: Invalid struct index"); 3445ff0cc061SDimitry Andric if (IsArray && Index >= CurTy->getArrayNumElements()) 34468f0fd8f6SDimitry Andric return error("INSERTVAL: Invalid array index"); 3447ff0cc061SDimitry Andric 3448f22ef01cSRoman Divacky INSERTVALIdx.push_back((unsigned)Index); 3449ff0cc061SDimitry Andric if (IsStruct) 3450ff0cc061SDimitry Andric CurTy = CurTy->subtypes()[Index]; 3451ff0cc061SDimitry Andric else 3452ff0cc061SDimitry Andric CurTy = CurTy->subtypes()[0]; 3453f22ef01cSRoman Divacky } 3454f22ef01cSRoman Divacky 3455ff0cc061SDimitry Andric if (CurTy != Val->getType()) 34568f0fd8f6SDimitry Andric return error("Inserted value type doesn't match aggregate type"); 3457ff0cc061SDimitry Andric 345817a519f9SDimitry Andric I = InsertValueInst::Create(Agg, Val, INSERTVALIdx); 3459f22ef01cSRoman Divacky InstructionList.push_back(I); 3460f22ef01cSRoman Divacky break; 3461f22ef01cSRoman Divacky } 3462f22ef01cSRoman Divacky 3463f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval] 3464f22ef01cSRoman Divacky // obsolete form of select 3465f22ef01cSRoman Divacky // handles select i1 ... in old bitcode 3466f22ef01cSRoman Divacky unsigned OpNum = 0; 3467f22ef01cSRoman Divacky Value *TrueVal, *FalseVal, *Cond; 3468f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) || 34693861d79fSDimitry Andric popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) || 34703861d79fSDimitry Andric popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond)) 34718f0fd8f6SDimitry Andric return error("Invalid record"); 3472f22ef01cSRoman Divacky 3473f22ef01cSRoman Divacky I = SelectInst::Create(Cond, TrueVal, FalseVal); 3474f22ef01cSRoman Divacky InstructionList.push_back(I); 3475f22ef01cSRoman Divacky break; 3476f22ef01cSRoman Divacky } 3477f22ef01cSRoman Divacky 3478f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred] 3479f22ef01cSRoman Divacky // new form of select 3480f22ef01cSRoman Divacky // handles select i1 or select [N x i1] 3481f22ef01cSRoman Divacky unsigned OpNum = 0; 3482f22ef01cSRoman Divacky Value *TrueVal, *FalseVal, *Cond; 3483f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) || 34843861d79fSDimitry Andric popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) || 3485f22ef01cSRoman Divacky getValueTypePair(Record, OpNum, NextValueNo, Cond)) 34868f0fd8f6SDimitry Andric return error("Invalid record"); 3487f22ef01cSRoman Divacky 3488f22ef01cSRoman Divacky // select condition can be either i1 or [N x i1] 34896122f3e6SDimitry Andric if (VectorType* vector_type = 34906122f3e6SDimitry Andric dyn_cast<VectorType>(Cond->getType())) { 3491f22ef01cSRoman Divacky // expect <n x i1> 3492f22ef01cSRoman Divacky if (vector_type->getElementType() != Type::getInt1Ty(Context)) 34938f0fd8f6SDimitry Andric return error("Invalid type for value"); 3494f22ef01cSRoman Divacky } else { 3495f22ef01cSRoman Divacky // expect i1 3496f22ef01cSRoman Divacky if (Cond->getType() != Type::getInt1Ty(Context)) 34978f0fd8f6SDimitry Andric return error("Invalid type for value"); 3498f22ef01cSRoman Divacky } 3499f22ef01cSRoman Divacky 3500f22ef01cSRoman Divacky I = SelectInst::Create(Cond, TrueVal, FalseVal); 3501f22ef01cSRoman Divacky InstructionList.push_back(I); 3502f22ef01cSRoman Divacky break; 3503f22ef01cSRoman Divacky } 3504f22ef01cSRoman Divacky 3505f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval] 3506f22ef01cSRoman Divacky unsigned OpNum = 0; 3507f22ef01cSRoman Divacky Value *Vec, *Idx; 3508f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Vec) || 350991bc56edSDimitry Andric getValueTypePair(Record, OpNum, NextValueNo, Idx)) 35108f0fd8f6SDimitry Andric return error("Invalid record"); 3511ff0cc061SDimitry Andric if (!Vec->getType()->isVectorTy()) 35128f0fd8f6SDimitry Andric return error("Invalid type for value"); 3513f22ef01cSRoman Divacky I = ExtractElementInst::Create(Vec, Idx); 3514f22ef01cSRoman Divacky InstructionList.push_back(I); 3515f22ef01cSRoman Divacky break; 3516f22ef01cSRoman Divacky } 3517f22ef01cSRoman Divacky 3518f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval] 3519f22ef01cSRoman Divacky unsigned OpNum = 0; 3520f22ef01cSRoman Divacky Value *Vec, *Elt, *Idx; 3521ff0cc061SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Vec)) 35228f0fd8f6SDimitry Andric return error("Invalid record"); 3523ff0cc061SDimitry Andric if (!Vec->getType()->isVectorTy()) 35248f0fd8f6SDimitry Andric return error("Invalid type for value"); 3525ff0cc061SDimitry Andric if (popValue(Record, OpNum, NextValueNo, 3526f22ef01cSRoman Divacky cast<VectorType>(Vec->getType())->getElementType(), Elt) || 352791bc56edSDimitry Andric getValueTypePair(Record, OpNum, NextValueNo, Idx)) 35288f0fd8f6SDimitry Andric return error("Invalid record"); 3529f22ef01cSRoman Divacky I = InsertElementInst::Create(Vec, Elt, Idx); 3530f22ef01cSRoman Divacky InstructionList.push_back(I); 3531f22ef01cSRoman Divacky break; 3532f22ef01cSRoman Divacky } 3533f22ef01cSRoman Divacky 3534f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval] 3535f22ef01cSRoman Divacky unsigned OpNum = 0; 3536f22ef01cSRoman Divacky Value *Vec1, *Vec2, *Mask; 3537f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) || 35383861d79fSDimitry Andric popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2)) 35398f0fd8f6SDimitry Andric return error("Invalid record"); 3540f22ef01cSRoman Divacky 3541f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Mask)) 35428f0fd8f6SDimitry Andric return error("Invalid record"); 3543ff0cc061SDimitry Andric if (!Vec1->getType()->isVectorTy() || !Vec2->getType()->isVectorTy()) 35448f0fd8f6SDimitry Andric return error("Invalid type for value"); 3545f22ef01cSRoman Divacky I = new ShuffleVectorInst(Vec1, Vec2, Mask); 3546f22ef01cSRoman Divacky InstructionList.push_back(I); 3547f22ef01cSRoman Divacky break; 3548f22ef01cSRoman Divacky } 3549f22ef01cSRoman Divacky 3550f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred] 3551f22ef01cSRoman Divacky // Old form of ICmp/FCmp returning bool 3552f22ef01cSRoman Divacky // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were 3553f22ef01cSRoman Divacky // both legal on vectors but had different behaviour. 3554f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred] 3555f22ef01cSRoman Divacky // FCmp/ICmp returning bool or vector of bool 3556f22ef01cSRoman Divacky 3557f22ef01cSRoman Divacky unsigned OpNum = 0; 3558f22ef01cSRoman Divacky Value *LHS, *RHS; 3559f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || 3560875ed548SDimitry Andric popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS)) 3561875ed548SDimitry Andric return error("Invalid record"); 3562875ed548SDimitry Andric 3563875ed548SDimitry Andric unsigned PredVal = Record[OpNum]; 3564875ed548SDimitry Andric bool IsFP = LHS->getType()->isFPOrFPVectorTy(); 3565875ed548SDimitry Andric FastMathFlags FMF; 3566875ed548SDimitry Andric if (IsFP && Record.size() > OpNum+1) 3567875ed548SDimitry Andric FMF = getDecodedFastMathFlags(Record[++OpNum]); 3568875ed548SDimitry Andric 3569875ed548SDimitry Andric if (OpNum+1 != Record.size()) 35708f0fd8f6SDimitry Andric return error("Invalid record"); 3571f22ef01cSRoman Divacky 3572f22ef01cSRoman Divacky if (LHS->getType()->isFPOrFPVectorTy()) 3573875ed548SDimitry Andric I = new FCmpInst((FCmpInst::Predicate)PredVal, LHS, RHS); 3574f22ef01cSRoman Divacky else 3575875ed548SDimitry Andric I = new ICmpInst((ICmpInst::Predicate)PredVal, LHS, RHS); 3576875ed548SDimitry Andric 3577875ed548SDimitry Andric if (FMF.any()) 3578875ed548SDimitry Andric I->setFastMathFlags(FMF); 3579f22ef01cSRoman Divacky InstructionList.push_back(I); 3580f22ef01cSRoman Divacky break; 3581f22ef01cSRoman Divacky } 3582f22ef01cSRoman Divacky 3583f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>] 3584f22ef01cSRoman Divacky { 3585f22ef01cSRoman Divacky unsigned Size = Record.size(); 3586f22ef01cSRoman Divacky if (Size == 0) { 3587f22ef01cSRoman Divacky I = ReturnInst::Create(Context); 3588f22ef01cSRoman Divacky InstructionList.push_back(I); 3589f22ef01cSRoman Divacky break; 3590f22ef01cSRoman Divacky } 3591f22ef01cSRoman Divacky 3592f22ef01cSRoman Divacky unsigned OpNum = 0; 359391bc56edSDimitry Andric Value *Op = nullptr; 3594f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Op)) 35958f0fd8f6SDimitry Andric return error("Invalid record"); 359617a519f9SDimitry Andric if (OpNum != Record.size()) 35978f0fd8f6SDimitry Andric return error("Invalid record"); 3598f22ef01cSRoman Divacky 359917a519f9SDimitry Andric I = ReturnInst::Create(Context, Op); 3600f22ef01cSRoman Divacky InstructionList.push_back(I); 3601f22ef01cSRoman Divacky break; 3602f22ef01cSRoman Divacky } 3603f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#] 3604f22ef01cSRoman Divacky if (Record.size() != 1 && Record.size() != 3) 36058f0fd8f6SDimitry Andric return error("Invalid record"); 3606f22ef01cSRoman Divacky BasicBlock *TrueDest = getBasicBlock(Record[0]); 360791bc56edSDimitry Andric if (!TrueDest) 36088f0fd8f6SDimitry Andric return error("Invalid record"); 3609f22ef01cSRoman Divacky 3610f22ef01cSRoman Divacky if (Record.size() == 1) { 3611f22ef01cSRoman Divacky I = BranchInst::Create(TrueDest); 3612f22ef01cSRoman Divacky InstructionList.push_back(I); 3613f22ef01cSRoman Divacky } 3614f22ef01cSRoman Divacky else { 3615f22ef01cSRoman Divacky BasicBlock *FalseDest = getBasicBlock(Record[1]); 36163861d79fSDimitry Andric Value *Cond = getValue(Record, 2, NextValueNo, 36173861d79fSDimitry Andric Type::getInt1Ty(Context)); 361891bc56edSDimitry Andric if (!FalseDest || !Cond) 36198f0fd8f6SDimitry Andric return error("Invalid record"); 3620f22ef01cSRoman Divacky I = BranchInst::Create(TrueDest, FalseDest, Cond); 3621f22ef01cSRoman Divacky InstructionList.push_back(I); 3622f22ef01cSRoman Divacky } 3623f22ef01cSRoman Divacky break; 3624f22ef01cSRoman Divacky } 36257d523365SDimitry Andric case bitc::FUNC_CODE_INST_CLEANUPRET: { // CLEANUPRET: [val] or [val,bb#] 36267d523365SDimitry Andric if (Record.size() != 1 && Record.size() != 2) 36277d523365SDimitry Andric return error("Invalid record"); 36287d523365SDimitry Andric unsigned Idx = 0; 36297d523365SDimitry Andric Value *CleanupPad = 36307d523365SDimitry Andric getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); 36317d523365SDimitry Andric if (!CleanupPad) 36327d523365SDimitry Andric return error("Invalid record"); 36337d523365SDimitry Andric BasicBlock *UnwindDest = nullptr; 36347d523365SDimitry Andric if (Record.size() == 2) { 36357d523365SDimitry Andric UnwindDest = getBasicBlock(Record[Idx++]); 36367d523365SDimitry Andric if (!UnwindDest) 36377d523365SDimitry Andric return error("Invalid record"); 36387d523365SDimitry Andric } 36397d523365SDimitry Andric 36407d523365SDimitry Andric I = CleanupReturnInst::Create(CleanupPad, UnwindDest); 36417d523365SDimitry Andric InstructionList.push_back(I); 36427d523365SDimitry Andric break; 36437d523365SDimitry Andric } 36447d523365SDimitry Andric case bitc::FUNC_CODE_INST_CATCHRET: { // CATCHRET: [val,bb#] 36457d523365SDimitry Andric if (Record.size() != 2) 36467d523365SDimitry Andric return error("Invalid record"); 36477d523365SDimitry Andric unsigned Idx = 0; 36487d523365SDimitry Andric Value *CatchPad = 36497d523365SDimitry Andric getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); 36507d523365SDimitry Andric if (!CatchPad) 36517d523365SDimitry Andric return error("Invalid record"); 36527d523365SDimitry Andric BasicBlock *BB = getBasicBlock(Record[Idx++]); 36537d523365SDimitry Andric if (!BB) 36547d523365SDimitry Andric return error("Invalid record"); 36557d523365SDimitry Andric 36567d523365SDimitry Andric I = CatchReturnInst::Create(CatchPad, BB); 36577d523365SDimitry Andric InstructionList.push_back(I); 36587d523365SDimitry Andric break; 36597d523365SDimitry Andric } 36607d523365SDimitry Andric case bitc::FUNC_CODE_INST_CATCHSWITCH: { // CATCHSWITCH: [tok,num,(bb)*,bb?] 36617d523365SDimitry Andric // We must have, at minimum, the outer scope and the number of arguments. 36627d523365SDimitry Andric if (Record.size() < 2) 36637d523365SDimitry Andric return error("Invalid record"); 36647d523365SDimitry Andric 36657d523365SDimitry Andric unsigned Idx = 0; 36667d523365SDimitry Andric 36677d523365SDimitry Andric Value *ParentPad = 36687d523365SDimitry Andric getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); 36697d523365SDimitry Andric 36707d523365SDimitry Andric unsigned NumHandlers = Record[Idx++]; 36717d523365SDimitry Andric 36727d523365SDimitry Andric SmallVector<BasicBlock *, 2> Handlers; 36737d523365SDimitry Andric for (unsigned Op = 0; Op != NumHandlers; ++Op) { 36747d523365SDimitry Andric BasicBlock *BB = getBasicBlock(Record[Idx++]); 36757d523365SDimitry Andric if (!BB) 36767d523365SDimitry Andric return error("Invalid record"); 36777d523365SDimitry Andric Handlers.push_back(BB); 36787d523365SDimitry Andric } 36797d523365SDimitry Andric 36807d523365SDimitry Andric BasicBlock *UnwindDest = nullptr; 36817d523365SDimitry Andric if (Idx + 1 == Record.size()) { 36827d523365SDimitry Andric UnwindDest = getBasicBlock(Record[Idx++]); 36837d523365SDimitry Andric if (!UnwindDest) 36847d523365SDimitry Andric return error("Invalid record"); 36857d523365SDimitry Andric } 36867d523365SDimitry Andric 36877d523365SDimitry Andric if (Record.size() != Idx) 36887d523365SDimitry Andric return error("Invalid record"); 36897d523365SDimitry Andric 36907d523365SDimitry Andric auto *CatchSwitch = 36917d523365SDimitry Andric CatchSwitchInst::Create(ParentPad, UnwindDest, NumHandlers); 36927d523365SDimitry Andric for (BasicBlock *Handler : Handlers) 36937d523365SDimitry Andric CatchSwitch->addHandler(Handler); 36947d523365SDimitry Andric I = CatchSwitch; 36957d523365SDimitry Andric InstructionList.push_back(I); 36967d523365SDimitry Andric break; 36977d523365SDimitry Andric } 36987d523365SDimitry Andric case bitc::FUNC_CODE_INST_CATCHPAD: 36997d523365SDimitry Andric case bitc::FUNC_CODE_INST_CLEANUPPAD: { // [tok,num,(ty,val)*] 37007d523365SDimitry Andric // We must have, at minimum, the outer scope and the number of arguments. 37017d523365SDimitry Andric if (Record.size() < 2) 37027d523365SDimitry Andric return error("Invalid record"); 37037d523365SDimitry Andric 37047d523365SDimitry Andric unsigned Idx = 0; 37057d523365SDimitry Andric 37067d523365SDimitry Andric Value *ParentPad = 37077d523365SDimitry Andric getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); 37087d523365SDimitry Andric 37097d523365SDimitry Andric unsigned NumArgOperands = Record[Idx++]; 37107d523365SDimitry Andric 37117d523365SDimitry Andric SmallVector<Value *, 2> Args; 37127d523365SDimitry Andric for (unsigned Op = 0; Op != NumArgOperands; ++Op) { 37137d523365SDimitry Andric Value *Val; 37147d523365SDimitry Andric if (getValueTypePair(Record, Idx, NextValueNo, Val)) 37157d523365SDimitry Andric return error("Invalid record"); 37167d523365SDimitry Andric Args.push_back(Val); 37177d523365SDimitry Andric } 37187d523365SDimitry Andric 37197d523365SDimitry Andric if (Record.size() != Idx) 37207d523365SDimitry Andric return error("Invalid record"); 37217d523365SDimitry Andric 37227d523365SDimitry Andric if (BitCode == bitc::FUNC_CODE_INST_CLEANUPPAD) 37237d523365SDimitry Andric I = CleanupPadInst::Create(ParentPad, Args); 37247d523365SDimitry Andric else 37257d523365SDimitry Andric I = CatchPadInst::Create(ParentPad, Args); 37267d523365SDimitry Andric InstructionList.push_back(I); 37277d523365SDimitry Andric break; 37287d523365SDimitry Andric } 3729f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...] 37307ae0e2c9SDimitry Andric // Check magic 37317ae0e2c9SDimitry Andric if ((Record[0] >> 16) == SWITCH_INST_MAGIC) { 3732f785676fSDimitry Andric // "New" SwitchInst format with case ranges. The changes to write this 3733f785676fSDimitry Andric // format were reverted but we still recognize bitcode that uses it. 3734f785676fSDimitry Andric // Hopefully someday we will have support for case ranges and can use 3735f785676fSDimitry Andric // this format again. 37367ae0e2c9SDimitry Andric 37377ae0e2c9SDimitry Andric Type *OpTy = getTypeByID(Record[1]); 37387ae0e2c9SDimitry Andric unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth(); 37397ae0e2c9SDimitry Andric 37403861d79fSDimitry Andric Value *Cond = getValue(Record, 2, NextValueNo, OpTy); 37417ae0e2c9SDimitry Andric BasicBlock *Default = getBasicBlock(Record[3]); 374291bc56edSDimitry Andric if (!OpTy || !Cond || !Default) 37438f0fd8f6SDimitry Andric return error("Invalid record"); 37447ae0e2c9SDimitry Andric 37457ae0e2c9SDimitry Andric unsigned NumCases = Record[4]; 37467ae0e2c9SDimitry Andric 37477ae0e2c9SDimitry Andric SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); 37487ae0e2c9SDimitry Andric InstructionList.push_back(SI); 37497ae0e2c9SDimitry Andric 37507ae0e2c9SDimitry Andric unsigned CurIdx = 5; 37517ae0e2c9SDimitry Andric for (unsigned i = 0; i != NumCases; ++i) { 3752f785676fSDimitry Andric SmallVector<ConstantInt*, 1> CaseVals; 37537ae0e2c9SDimitry Andric unsigned NumItems = Record[CurIdx++]; 37547ae0e2c9SDimitry Andric for (unsigned ci = 0; ci != NumItems; ++ci) { 37557ae0e2c9SDimitry Andric bool isSingleNumber = Record[CurIdx++]; 37567ae0e2c9SDimitry Andric 37577ae0e2c9SDimitry Andric APInt Low; 37587ae0e2c9SDimitry Andric unsigned ActiveWords = 1; 37597ae0e2c9SDimitry Andric if (ValueBitWidth > 64) 37607ae0e2c9SDimitry Andric ActiveWords = Record[CurIdx++]; 37618f0fd8f6SDimitry Andric Low = readWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords), 37627ae0e2c9SDimitry Andric ValueBitWidth); 37637ae0e2c9SDimitry Andric CurIdx += ActiveWords; 37647ae0e2c9SDimitry Andric 37657ae0e2c9SDimitry Andric if (!isSingleNumber) { 37667ae0e2c9SDimitry Andric ActiveWords = 1; 37677ae0e2c9SDimitry Andric if (ValueBitWidth > 64) 37687ae0e2c9SDimitry Andric ActiveWords = Record[CurIdx++]; 37698f0fd8f6SDimitry Andric APInt High = readWideAPInt( 37708f0fd8f6SDimitry Andric makeArrayRef(&Record[CurIdx], ActiveWords), ValueBitWidth); 37717ae0e2c9SDimitry Andric CurIdx += ActiveWords; 3772f785676fSDimitry Andric 3773f785676fSDimitry Andric // FIXME: It is not clear whether values in the range should be 3774f785676fSDimitry Andric // compared as signed or unsigned values. The partially 3775f785676fSDimitry Andric // implemented changes that used this format in the past used 3776f785676fSDimitry Andric // unsigned comparisons. 3777f785676fSDimitry Andric for ( ; Low.ule(High); ++Low) 3778f785676fSDimitry Andric CaseVals.push_back(ConstantInt::get(Context, Low)); 37797ae0e2c9SDimitry Andric } else 3780f785676fSDimitry Andric CaseVals.push_back(ConstantInt::get(Context, Low)); 37817ae0e2c9SDimitry Andric } 37827ae0e2c9SDimitry Andric BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]); 3783f785676fSDimitry Andric for (SmallVector<ConstantInt*, 1>::iterator cvi = CaseVals.begin(), 3784f785676fSDimitry Andric cve = CaseVals.end(); cvi != cve; ++cvi) 3785f785676fSDimitry Andric SI->addCase(*cvi, DestBB); 37867ae0e2c9SDimitry Andric } 37877ae0e2c9SDimitry Andric I = SI; 37887ae0e2c9SDimitry Andric break; 37897ae0e2c9SDimitry Andric } 37907ae0e2c9SDimitry Andric 37917ae0e2c9SDimitry Andric // Old SwitchInst format without case ranges. 37927ae0e2c9SDimitry Andric 3793f22ef01cSRoman Divacky if (Record.size() < 3 || (Record.size() & 1) == 0) 37948f0fd8f6SDimitry Andric return error("Invalid record"); 37956122f3e6SDimitry Andric Type *OpTy = getTypeByID(Record[0]); 37963861d79fSDimitry Andric Value *Cond = getValue(Record, 1, NextValueNo, OpTy); 3797f22ef01cSRoman Divacky BasicBlock *Default = getBasicBlock(Record[2]); 379891bc56edSDimitry Andric if (!OpTy || !Cond || !Default) 37998f0fd8f6SDimitry Andric return error("Invalid record"); 3800f22ef01cSRoman Divacky unsigned NumCases = (Record.size()-3)/2; 3801f22ef01cSRoman Divacky SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); 3802f22ef01cSRoman Divacky InstructionList.push_back(SI); 3803f22ef01cSRoman Divacky for (unsigned i = 0, e = NumCases; i != e; ++i) { 3804f22ef01cSRoman Divacky ConstantInt *CaseVal = 3805f22ef01cSRoman Divacky dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy)); 3806f22ef01cSRoman Divacky BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]); 380791bc56edSDimitry Andric if (!CaseVal || !DestBB) { 3808f22ef01cSRoman Divacky delete SI; 38098f0fd8f6SDimitry Andric return error("Invalid record"); 3810f22ef01cSRoman Divacky } 3811f22ef01cSRoman Divacky SI->addCase(CaseVal, DestBB); 3812f22ef01cSRoman Divacky } 3813f22ef01cSRoman Divacky I = SI; 3814f22ef01cSRoman Divacky break; 3815f22ef01cSRoman Divacky } 3816f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...] 3817f22ef01cSRoman Divacky if (Record.size() < 2) 38188f0fd8f6SDimitry Andric return error("Invalid record"); 38196122f3e6SDimitry Andric Type *OpTy = getTypeByID(Record[0]); 38203861d79fSDimitry Andric Value *Address = getValue(Record, 1, NextValueNo, OpTy); 382191bc56edSDimitry Andric if (!OpTy || !Address) 38228f0fd8f6SDimitry Andric return error("Invalid record"); 3823f22ef01cSRoman Divacky unsigned NumDests = Record.size()-2; 3824f22ef01cSRoman Divacky IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests); 3825f22ef01cSRoman Divacky InstructionList.push_back(IBI); 3826f22ef01cSRoman Divacky for (unsigned i = 0, e = NumDests; i != e; ++i) { 3827f22ef01cSRoman Divacky if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) { 3828f22ef01cSRoman Divacky IBI->addDestination(DestBB); 3829f22ef01cSRoman Divacky } else { 3830f22ef01cSRoman Divacky delete IBI; 38318f0fd8f6SDimitry Andric return error("Invalid record"); 3832f22ef01cSRoman Divacky } 3833f22ef01cSRoman Divacky } 3834f22ef01cSRoman Divacky I = IBI; 3835f22ef01cSRoman Divacky break; 3836f22ef01cSRoman Divacky } 3837f22ef01cSRoman Divacky 3838f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_INVOKE: { 3839f22ef01cSRoman Divacky // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...] 3840f785676fSDimitry Andric if (Record.size() < 4) 38418f0fd8f6SDimitry Andric return error("Invalid record"); 3842ff0cc061SDimitry Andric unsigned OpNum = 0; 3843ff0cc061SDimitry Andric AttributeSet PAL = getAttributes(Record[OpNum++]); 3844ff0cc061SDimitry Andric unsigned CCInfo = Record[OpNum++]; 3845ff0cc061SDimitry Andric BasicBlock *NormalBB = getBasicBlock(Record[OpNum++]); 3846ff0cc061SDimitry Andric BasicBlock *UnwindBB = getBasicBlock(Record[OpNum++]); 3847f22ef01cSRoman Divacky 3848ff0cc061SDimitry Andric FunctionType *FTy = nullptr; 3849ff0cc061SDimitry Andric if (CCInfo >> 13 & 1 && 3850ff0cc061SDimitry Andric !(FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++])))) 38518f0fd8f6SDimitry Andric return error("Explicit invoke type is not a function type"); 3852ff0cc061SDimitry Andric 3853f22ef01cSRoman Divacky Value *Callee; 3854f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) 38558f0fd8f6SDimitry Andric return error("Invalid record"); 3856f22ef01cSRoman Divacky 38576122f3e6SDimitry Andric PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType()); 3858ff0cc061SDimitry Andric if (!CalleeTy) 38598f0fd8f6SDimitry Andric return error("Callee is not a pointer"); 3860ff0cc061SDimitry Andric if (!FTy) { 3861ff0cc061SDimitry Andric FTy = dyn_cast<FunctionType>(CalleeTy->getElementType()); 3862ff0cc061SDimitry Andric if (!FTy) 38638f0fd8f6SDimitry Andric return error("Callee is not of pointer to function type"); 3864ff0cc061SDimitry Andric } else if (CalleeTy->getElementType() != FTy) 38658f0fd8f6SDimitry Andric return error("Explicit invoke type does not match pointee type of " 3866ff0cc061SDimitry Andric "callee operand"); 3867ff0cc061SDimitry Andric if (Record.size() < FTy->getNumParams() + OpNum) 38688f0fd8f6SDimitry Andric return error("Insufficient operands to call"); 3869f22ef01cSRoman Divacky 3870f22ef01cSRoman Divacky SmallVector<Value*, 16> Ops; 3871f22ef01cSRoman Divacky for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { 38723861d79fSDimitry Andric Ops.push_back(getValue(Record, OpNum, NextValueNo, 38733861d79fSDimitry Andric FTy->getParamType(i))); 387491bc56edSDimitry Andric if (!Ops.back()) 38758f0fd8f6SDimitry Andric return error("Invalid record"); 3876f22ef01cSRoman Divacky } 3877f22ef01cSRoman Divacky 3878f22ef01cSRoman Divacky if (!FTy->isVarArg()) { 3879f22ef01cSRoman Divacky if (Record.size() != OpNum) 38808f0fd8f6SDimitry Andric return error("Invalid record"); 3881f22ef01cSRoman Divacky } else { 3882f22ef01cSRoman Divacky // Read type/value pairs for varargs params. 3883f22ef01cSRoman Divacky while (OpNum != Record.size()) { 3884f22ef01cSRoman Divacky Value *Op; 3885f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Op)) 38868f0fd8f6SDimitry Andric return error("Invalid record"); 3887f22ef01cSRoman Divacky Ops.push_back(Op); 3888f22ef01cSRoman Divacky } 3889f22ef01cSRoman Divacky } 3890f22ef01cSRoman Divacky 38917d523365SDimitry Andric I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops, OperandBundles); 38927d523365SDimitry Andric OperandBundles.clear(); 3893f22ef01cSRoman Divacky InstructionList.push_back(I); 38947d523365SDimitry Andric cast<InvokeInst>(I)->setCallingConv( 38957d523365SDimitry Andric static_cast<CallingConv::ID>(CallingConv::MaxID & CCInfo)); 3896f22ef01cSRoman Divacky cast<InvokeInst>(I)->setAttributes(PAL); 3897f22ef01cSRoman Divacky break; 3898f22ef01cSRoman Divacky } 38996122f3e6SDimitry Andric case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval] 39006122f3e6SDimitry Andric unsigned Idx = 0; 390191bc56edSDimitry Andric Value *Val = nullptr; 39026122f3e6SDimitry Andric if (getValueTypePair(Record, Idx, NextValueNo, Val)) 39038f0fd8f6SDimitry Andric return error("Invalid record"); 39046122f3e6SDimitry Andric I = ResumeInst::Create(Val); 39056122f3e6SDimitry Andric InstructionList.push_back(I); 39066122f3e6SDimitry Andric break; 39076122f3e6SDimitry Andric } 3908f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE 3909f22ef01cSRoman Divacky I = new UnreachableInst(Context); 3910f22ef01cSRoman Divacky InstructionList.push_back(I); 3911f22ef01cSRoman Divacky break; 3912f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...] 3913f22ef01cSRoman Divacky if (Record.size() < 1 || ((Record.size()-1)&1)) 39148f0fd8f6SDimitry Andric return error("Invalid record"); 39156122f3e6SDimitry Andric Type *Ty = getTypeByID(Record[0]); 3916f785676fSDimitry Andric if (!Ty) 39178f0fd8f6SDimitry Andric return error("Invalid record"); 3918f22ef01cSRoman Divacky 39193b0f4066SDimitry Andric PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2); 3920f22ef01cSRoman Divacky InstructionList.push_back(PN); 3921f22ef01cSRoman Divacky 3922f22ef01cSRoman Divacky for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) { 39233861d79fSDimitry Andric Value *V; 39243861d79fSDimitry Andric // With the new function encoding, it is possible that operands have 39253861d79fSDimitry Andric // negative IDs (for forward references). Use a signed VBR 39263861d79fSDimitry Andric // representation to keep the encoding small. 39273861d79fSDimitry Andric if (UseRelativeIDs) 39283861d79fSDimitry Andric V = getValueSigned(Record, 1+i, NextValueNo, Ty); 39293861d79fSDimitry Andric else 39303861d79fSDimitry Andric V = getValue(Record, 1+i, NextValueNo, Ty); 3931f22ef01cSRoman Divacky BasicBlock *BB = getBasicBlock(Record[2+i]); 3932f785676fSDimitry Andric if (!V || !BB) 39338f0fd8f6SDimitry Andric return error("Invalid record"); 3934f22ef01cSRoman Divacky PN->addIncoming(V, BB); 3935f22ef01cSRoman Divacky } 3936f22ef01cSRoman Divacky I = PN; 3937f22ef01cSRoman Divacky break; 3938f22ef01cSRoman Divacky } 3939f22ef01cSRoman Divacky 39408f0fd8f6SDimitry Andric case bitc::FUNC_CODE_INST_LANDINGPAD: 39418f0fd8f6SDimitry Andric case bitc::FUNC_CODE_INST_LANDINGPAD_OLD: { 39426122f3e6SDimitry Andric // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?] 39436122f3e6SDimitry Andric unsigned Idx = 0; 39448f0fd8f6SDimitry Andric if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD) { 39458f0fd8f6SDimitry Andric if (Record.size() < 3) 39468f0fd8f6SDimitry Andric return error("Invalid record"); 39478f0fd8f6SDimitry Andric } else { 39488f0fd8f6SDimitry Andric assert(BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD); 39496122f3e6SDimitry Andric if (Record.size() < 4) 39508f0fd8f6SDimitry Andric return error("Invalid record"); 39518f0fd8f6SDimitry Andric } 39526122f3e6SDimitry Andric Type *Ty = getTypeByID(Record[Idx++]); 3953f785676fSDimitry Andric if (!Ty) 39548f0fd8f6SDimitry Andric return error("Invalid record"); 39558f0fd8f6SDimitry Andric if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD) { 395691bc56edSDimitry Andric Value *PersFn = nullptr; 39576122f3e6SDimitry Andric if (getValueTypePair(Record, Idx, NextValueNo, PersFn)) 39588f0fd8f6SDimitry Andric return error("Invalid record"); 39598f0fd8f6SDimitry Andric 39608f0fd8f6SDimitry Andric if (!F->hasPersonalityFn()) 39618f0fd8f6SDimitry Andric F->setPersonalityFn(cast<Constant>(PersFn)); 39628f0fd8f6SDimitry Andric else if (F->getPersonalityFn() != cast<Constant>(PersFn)) 39638f0fd8f6SDimitry Andric return error("Personality function mismatch"); 39648f0fd8f6SDimitry Andric } 39656122f3e6SDimitry Andric 39666122f3e6SDimitry Andric bool IsCleanup = !!Record[Idx++]; 39676122f3e6SDimitry Andric unsigned NumClauses = Record[Idx++]; 39688f0fd8f6SDimitry Andric LandingPadInst *LP = LandingPadInst::Create(Ty, NumClauses); 39696122f3e6SDimitry Andric LP->setCleanup(IsCleanup); 39706122f3e6SDimitry Andric for (unsigned J = 0; J != NumClauses; ++J) { 39716122f3e6SDimitry Andric LandingPadInst::ClauseType CT = 39726122f3e6SDimitry Andric LandingPadInst::ClauseType(Record[Idx++]); (void)CT; 39736122f3e6SDimitry Andric Value *Val; 39746122f3e6SDimitry Andric 39756122f3e6SDimitry Andric if (getValueTypePair(Record, Idx, NextValueNo, Val)) { 39766122f3e6SDimitry Andric delete LP; 39778f0fd8f6SDimitry Andric return error("Invalid record"); 39786122f3e6SDimitry Andric } 39796122f3e6SDimitry Andric 39806122f3e6SDimitry Andric assert((CT != LandingPadInst::Catch || 39816122f3e6SDimitry Andric !isa<ArrayType>(Val->getType())) && 39826122f3e6SDimitry Andric "Catch clause has a invalid type!"); 39836122f3e6SDimitry Andric assert((CT != LandingPadInst::Filter || 39846122f3e6SDimitry Andric isa<ArrayType>(Val->getType())) && 39856122f3e6SDimitry Andric "Filter clause has invalid type!"); 398691bc56edSDimitry Andric LP->addClause(cast<Constant>(Val)); 39876122f3e6SDimitry Andric } 39886122f3e6SDimitry Andric 39896122f3e6SDimitry Andric I = LP; 39906122f3e6SDimitry Andric InstructionList.push_back(I); 39916122f3e6SDimitry Andric break; 39926122f3e6SDimitry Andric } 39936122f3e6SDimitry Andric 399417a519f9SDimitry Andric case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align] 399517a519f9SDimitry Andric if (Record.size() != 4) 39968f0fd8f6SDimitry Andric return error("Invalid record"); 3997ff0cc061SDimitry Andric uint64_t AlignRecord = Record[3]; 3998ff0cc061SDimitry Andric const uint64_t InAllocaMask = uint64_t(1) << 5; 3999ff0cc061SDimitry Andric const uint64_t ExplicitTypeMask = uint64_t(1) << 6; 40003ca95b02SDimitry Andric const uint64_t SwiftErrorMask = uint64_t(1) << 7; 40013ca95b02SDimitry Andric const uint64_t FlagMask = InAllocaMask | ExplicitTypeMask | 40023ca95b02SDimitry Andric SwiftErrorMask; 4003ff0cc061SDimitry Andric bool InAlloca = AlignRecord & InAllocaMask; 40043ca95b02SDimitry Andric bool SwiftError = AlignRecord & SwiftErrorMask; 4005ff0cc061SDimitry Andric Type *Ty = getTypeByID(Record[0]); 4006ff0cc061SDimitry Andric if ((AlignRecord & ExplicitTypeMask) == 0) { 4007ff0cc061SDimitry Andric auto *PTy = dyn_cast_or_null<PointerType>(Ty); 4008ff0cc061SDimitry Andric if (!PTy) 40098f0fd8f6SDimitry Andric return error("Old-style alloca with a non-pointer type"); 4010ff0cc061SDimitry Andric Ty = PTy->getElementType(); 4011ff0cc061SDimitry Andric } 40126122f3e6SDimitry Andric Type *OpTy = getTypeByID(Record[1]); 401317a519f9SDimitry Andric Value *Size = getFnValueByID(Record[2], OpTy); 4014ff0cc061SDimitry Andric unsigned Align; 4015d88c1a5aSDimitry Andric if (Error Err = parseAlignmentValue(AlignRecord & ~FlagMask, Align)) { 4016d88c1a5aSDimitry Andric return Err; 4017ff0cc061SDimitry Andric } 4018f785676fSDimitry Andric if (!Ty || !Size) 40198f0fd8f6SDimitry Andric return error("Invalid record"); 4020ff0cc061SDimitry Andric AllocaInst *AI = new AllocaInst(Ty, Size, Align); 402191bc56edSDimitry Andric AI->setUsedWithInAlloca(InAlloca); 40223ca95b02SDimitry Andric AI->setSwiftError(SwiftError); 402391bc56edSDimitry Andric I = AI; 4024f22ef01cSRoman Divacky InstructionList.push_back(I); 4025f22ef01cSRoman Divacky break; 4026f22ef01cSRoman Divacky } 4027f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol] 4028f22ef01cSRoman Divacky unsigned OpNum = 0; 4029f22ef01cSRoman Divacky Value *Op; 4030f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Op) || 4031ff0cc061SDimitry Andric (OpNum + 2 != Record.size() && OpNum + 3 != Record.size())) 40328f0fd8f6SDimitry Andric return error("Invalid record"); 4033f22ef01cSRoman Divacky 4034ff0cc061SDimitry Andric Type *Ty = nullptr; 4035ff0cc061SDimitry Andric if (OpNum + 3 == Record.size()) 4036ff0cc061SDimitry Andric Ty = getTypeByID(Record[OpNum++]); 4037d88c1a5aSDimitry Andric if (Error Err = typeCheckLoadStoreInst(Ty, Op->getType())) 4038d88c1a5aSDimitry Andric return Err; 4039ff0cc061SDimitry Andric if (!Ty) 4040ff0cc061SDimitry Andric Ty = cast<PointerType>(Op->getType())->getElementType(); 4041ff0cc061SDimitry Andric 4042ff0cc061SDimitry Andric unsigned Align; 4043d88c1a5aSDimitry Andric if (Error Err = parseAlignmentValue(Record[OpNum], Align)) 4044d88c1a5aSDimitry Andric return Err; 4045ff0cc061SDimitry Andric I = new LoadInst(Ty, Op, "", Record[OpNum + 1], Align); 4046ff0cc061SDimitry Andric 4047f22ef01cSRoman Divacky InstructionList.push_back(I); 4048f22ef01cSRoman Divacky break; 4049f22ef01cSRoman Divacky } 40506122f3e6SDimitry Andric case bitc::FUNC_CODE_INST_LOADATOMIC: { 40516122f3e6SDimitry Andric // LOADATOMIC: [opty, op, align, vol, ordering, synchscope] 40526122f3e6SDimitry Andric unsigned OpNum = 0; 40536122f3e6SDimitry Andric Value *Op; 40546122f3e6SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Op) || 4055ff0cc061SDimitry Andric (OpNum + 4 != Record.size() && OpNum + 5 != Record.size())) 40568f0fd8f6SDimitry Andric return error("Invalid record"); 40576122f3e6SDimitry Andric 4058ff0cc061SDimitry Andric Type *Ty = nullptr; 4059ff0cc061SDimitry Andric if (OpNum + 5 == Record.size()) 4060ff0cc061SDimitry Andric Ty = getTypeByID(Record[OpNum++]); 4061d88c1a5aSDimitry Andric if (Error Err = typeCheckLoadStoreInst(Ty, Op->getType())) 4062d88c1a5aSDimitry Andric return Err; 4063ff0cc061SDimitry Andric if (!Ty) 4064ff0cc061SDimitry Andric Ty = cast<PointerType>(Op->getType())->getElementType(); 4065ff0cc061SDimitry Andric 40668f0fd8f6SDimitry Andric AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); 40673ca95b02SDimitry Andric if (Ordering == AtomicOrdering::NotAtomic || 40683ca95b02SDimitry Andric Ordering == AtomicOrdering::Release || 40693ca95b02SDimitry Andric Ordering == AtomicOrdering::AcquireRelease) 40708f0fd8f6SDimitry Andric return error("Invalid record"); 40713ca95b02SDimitry Andric if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0) 40728f0fd8f6SDimitry Andric return error("Invalid record"); 40738f0fd8f6SDimitry Andric SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]); 40746122f3e6SDimitry Andric 4075ff0cc061SDimitry Andric unsigned Align; 4076d88c1a5aSDimitry Andric if (Error Err = parseAlignmentValue(Record[OpNum], Align)) 4077d88c1a5aSDimitry Andric return Err; 4078ff0cc061SDimitry Andric I = new LoadInst(Op, "", Record[OpNum+1], Align, Ordering, SynchScope); 4079ff0cc061SDimitry Andric 40806122f3e6SDimitry Andric InstructionList.push_back(I); 40816122f3e6SDimitry Andric break; 40826122f3e6SDimitry Andric } 4083ff0cc061SDimitry Andric case bitc::FUNC_CODE_INST_STORE: 4084ff0cc061SDimitry Andric case bitc::FUNC_CODE_INST_STORE_OLD: { // STORE2:[ptrty, ptr, val, align, vol] 4085f22ef01cSRoman Divacky unsigned OpNum = 0; 4086f22ef01cSRoman Divacky Value *Val, *Ptr; 4087f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || 4088ff0cc061SDimitry Andric (BitCode == bitc::FUNC_CODE_INST_STORE 4089ff0cc061SDimitry Andric ? getValueTypePair(Record, OpNum, NextValueNo, Val) 4090ff0cc061SDimitry Andric : popValue(Record, OpNum, NextValueNo, 4091ff0cc061SDimitry Andric cast<PointerType>(Ptr->getType())->getElementType(), 4092ff0cc061SDimitry Andric Val)) || 4093f22ef01cSRoman Divacky OpNum + 2 != Record.size()) 40948f0fd8f6SDimitry Andric return error("Invalid record"); 4095f22ef01cSRoman Divacky 4096d88c1a5aSDimitry Andric if (Error Err = typeCheckLoadStoreInst(Val->getType(), Ptr->getType())) 4097d88c1a5aSDimitry Andric return Err; 4098ff0cc061SDimitry Andric unsigned Align; 4099d88c1a5aSDimitry Andric if (Error Err = parseAlignmentValue(Record[OpNum], Align)) 4100d88c1a5aSDimitry Andric return Err; 4101ff0cc061SDimitry Andric I = new StoreInst(Val, Ptr, Record[OpNum+1], Align); 4102f22ef01cSRoman Divacky InstructionList.push_back(I); 4103f22ef01cSRoman Divacky break; 4104f22ef01cSRoman Divacky } 4105ff0cc061SDimitry Andric case bitc::FUNC_CODE_INST_STOREATOMIC: 4106ff0cc061SDimitry Andric case bitc::FUNC_CODE_INST_STOREATOMIC_OLD: { 41076122f3e6SDimitry Andric // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, synchscope] 41086122f3e6SDimitry Andric unsigned OpNum = 0; 41096122f3e6SDimitry Andric Value *Val, *Ptr; 41106122f3e6SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || 41113ca95b02SDimitry Andric !isa<PointerType>(Ptr->getType()) || 4112ff0cc061SDimitry Andric (BitCode == bitc::FUNC_CODE_INST_STOREATOMIC 4113ff0cc061SDimitry Andric ? getValueTypePair(Record, OpNum, NextValueNo, Val) 4114ff0cc061SDimitry Andric : popValue(Record, OpNum, NextValueNo, 4115ff0cc061SDimitry Andric cast<PointerType>(Ptr->getType())->getElementType(), 4116ff0cc061SDimitry Andric Val)) || 41176122f3e6SDimitry Andric OpNum + 4 != Record.size()) 41188f0fd8f6SDimitry Andric return error("Invalid record"); 41196122f3e6SDimitry Andric 4120d88c1a5aSDimitry Andric if (Error Err = typeCheckLoadStoreInst(Val->getType(), Ptr->getType())) 4121d88c1a5aSDimitry Andric return Err; 41228f0fd8f6SDimitry Andric AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); 41233ca95b02SDimitry Andric if (Ordering == AtomicOrdering::NotAtomic || 41243ca95b02SDimitry Andric Ordering == AtomicOrdering::Acquire || 41253ca95b02SDimitry Andric Ordering == AtomicOrdering::AcquireRelease) 41268f0fd8f6SDimitry Andric return error("Invalid record"); 41278f0fd8f6SDimitry Andric SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]); 41283ca95b02SDimitry Andric if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0) 41298f0fd8f6SDimitry Andric return error("Invalid record"); 41306122f3e6SDimitry Andric 4131ff0cc061SDimitry Andric unsigned Align; 4132d88c1a5aSDimitry Andric if (Error Err = parseAlignmentValue(Record[OpNum], Align)) 4133d88c1a5aSDimitry Andric return Err; 4134ff0cc061SDimitry Andric I = new StoreInst(Val, Ptr, Record[OpNum+1], Align, Ordering, SynchScope); 41356122f3e6SDimitry Andric InstructionList.push_back(I); 41366122f3e6SDimitry Andric break; 41376122f3e6SDimitry Andric } 4138ff0cc061SDimitry Andric case bitc::FUNC_CODE_INST_CMPXCHG_OLD: 41396122f3e6SDimitry Andric case bitc::FUNC_CODE_INST_CMPXCHG: { 414091bc56edSDimitry Andric // CMPXCHG:[ptrty, ptr, cmp, new, vol, successordering, synchscope, 414191bc56edSDimitry Andric // failureordering?, isweak?] 41426122f3e6SDimitry Andric unsigned OpNum = 0; 41436122f3e6SDimitry Andric Value *Ptr, *Cmp, *New; 41446122f3e6SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || 4145ff0cc061SDimitry Andric (BitCode == bitc::FUNC_CODE_INST_CMPXCHG 4146ff0cc061SDimitry Andric ? getValueTypePair(Record, OpNum, NextValueNo, Cmp) 4147ff0cc061SDimitry Andric : popValue(Record, OpNum, NextValueNo, 4148ff0cc061SDimitry Andric cast<PointerType>(Ptr->getType())->getElementType(), 4149ff0cc061SDimitry Andric Cmp)) || 4150ff0cc061SDimitry Andric popValue(Record, OpNum, NextValueNo, Cmp->getType(), New) || 4151ff0cc061SDimitry Andric Record.size() < OpNum + 3 || Record.size() > OpNum + 5) 41528f0fd8f6SDimitry Andric return error("Invalid record"); 41538f0fd8f6SDimitry Andric AtomicOrdering SuccessOrdering = getDecodedOrdering(Record[OpNum + 1]); 41543ca95b02SDimitry Andric if (SuccessOrdering == AtomicOrdering::NotAtomic || 41553ca95b02SDimitry Andric SuccessOrdering == AtomicOrdering::Unordered) 41568f0fd8f6SDimitry Andric return error("Invalid record"); 41578f0fd8f6SDimitry Andric SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 2]); 415891bc56edSDimitry Andric 4159d88c1a5aSDimitry Andric if (Error Err = typeCheckLoadStoreInst(Cmp->getType(), Ptr->getType())) 4160d88c1a5aSDimitry Andric return Err; 416191bc56edSDimitry Andric AtomicOrdering FailureOrdering; 416291bc56edSDimitry Andric if (Record.size() < 7) 416391bc56edSDimitry Andric FailureOrdering = 416491bc56edSDimitry Andric AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering); 416591bc56edSDimitry Andric else 41668f0fd8f6SDimitry Andric FailureOrdering = getDecodedOrdering(Record[OpNum + 3]); 416791bc56edSDimitry Andric 416891bc56edSDimitry Andric I = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering, FailureOrdering, 416991bc56edSDimitry Andric SynchScope); 41706122f3e6SDimitry Andric cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]); 417191bc56edSDimitry Andric 417291bc56edSDimitry Andric if (Record.size() < 8) { 417391bc56edSDimitry Andric // Before weak cmpxchgs existed, the instruction simply returned the 417491bc56edSDimitry Andric // value loaded from memory, so bitcode files from that era will be 417591bc56edSDimitry Andric // expecting the first component of a modern cmpxchg. 417691bc56edSDimitry Andric CurBB->getInstList().push_back(I); 417791bc56edSDimitry Andric I = ExtractValueInst::Create(I, 0); 417891bc56edSDimitry Andric } else { 417991bc56edSDimitry Andric cast<AtomicCmpXchgInst>(I)->setWeak(Record[OpNum+4]); 418091bc56edSDimitry Andric } 418191bc56edSDimitry Andric 41826122f3e6SDimitry Andric InstructionList.push_back(I); 41836122f3e6SDimitry Andric break; 41846122f3e6SDimitry Andric } 41856122f3e6SDimitry Andric case bitc::FUNC_CODE_INST_ATOMICRMW: { 41866122f3e6SDimitry Andric // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, synchscope] 41876122f3e6SDimitry Andric unsigned OpNum = 0; 41886122f3e6SDimitry Andric Value *Ptr, *Val; 41896122f3e6SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || 41903ca95b02SDimitry Andric !isa<PointerType>(Ptr->getType()) || 41913861d79fSDimitry Andric popValue(Record, OpNum, NextValueNo, 41926122f3e6SDimitry Andric cast<PointerType>(Ptr->getType())->getElementType(), Val) || 41936122f3e6SDimitry Andric OpNum+4 != Record.size()) 41948f0fd8f6SDimitry Andric return error("Invalid record"); 41958f0fd8f6SDimitry Andric AtomicRMWInst::BinOp Operation = getDecodedRMWOperation(Record[OpNum]); 41966122f3e6SDimitry Andric if (Operation < AtomicRMWInst::FIRST_BINOP || 41976122f3e6SDimitry Andric Operation > AtomicRMWInst::LAST_BINOP) 41988f0fd8f6SDimitry Andric return error("Invalid record"); 41998f0fd8f6SDimitry Andric AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); 42003ca95b02SDimitry Andric if (Ordering == AtomicOrdering::NotAtomic || 42013ca95b02SDimitry Andric Ordering == AtomicOrdering::Unordered) 42028f0fd8f6SDimitry Andric return error("Invalid record"); 42038f0fd8f6SDimitry Andric SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]); 42046122f3e6SDimitry Andric I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SynchScope); 42056122f3e6SDimitry Andric cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]); 42066122f3e6SDimitry Andric InstructionList.push_back(I); 42076122f3e6SDimitry Andric break; 42086122f3e6SDimitry Andric } 42096122f3e6SDimitry Andric case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, synchscope] 42106122f3e6SDimitry Andric if (2 != Record.size()) 42118f0fd8f6SDimitry Andric return error("Invalid record"); 42128f0fd8f6SDimitry Andric AtomicOrdering Ordering = getDecodedOrdering(Record[0]); 42133ca95b02SDimitry Andric if (Ordering == AtomicOrdering::NotAtomic || 42143ca95b02SDimitry Andric Ordering == AtomicOrdering::Unordered || 42153ca95b02SDimitry Andric Ordering == AtomicOrdering::Monotonic) 42168f0fd8f6SDimitry Andric return error("Invalid record"); 42178f0fd8f6SDimitry Andric SynchronizationScope SynchScope = getDecodedSynchScope(Record[1]); 42186122f3e6SDimitry Andric I = new FenceInst(Context, Ordering, SynchScope); 42196122f3e6SDimitry Andric InstructionList.push_back(I); 42206122f3e6SDimitry Andric break; 42216122f3e6SDimitry Andric } 422217a519f9SDimitry Andric case bitc::FUNC_CODE_INST_CALL: { 42237d523365SDimitry Andric // CALL: [paramattrs, cc, fmf, fnty, fnid, arg0, arg1...] 4224f22ef01cSRoman Divacky if (Record.size() < 3) 42258f0fd8f6SDimitry Andric return error("Invalid record"); 4226f22ef01cSRoman Divacky 4227ff0cc061SDimitry Andric unsigned OpNum = 0; 4228ff0cc061SDimitry Andric AttributeSet PAL = getAttributes(Record[OpNum++]); 4229ff0cc061SDimitry Andric unsigned CCInfo = Record[OpNum++]; 4230f22ef01cSRoman Divacky 42317d523365SDimitry Andric FastMathFlags FMF; 42327d523365SDimitry Andric if ((CCInfo >> bitc::CALL_FMF) & 1) { 42337d523365SDimitry Andric FMF = getDecodedFastMathFlags(Record[OpNum++]); 42347d523365SDimitry Andric if (!FMF.any()) 42357d523365SDimitry Andric return error("Fast math flags indicator set for call with no FMF"); 42367d523365SDimitry Andric } 42377d523365SDimitry Andric 4238ff0cc061SDimitry Andric FunctionType *FTy = nullptr; 42397d523365SDimitry Andric if (CCInfo >> bitc::CALL_EXPLICIT_TYPE & 1 && 4240ff0cc061SDimitry Andric !(FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++])))) 42418f0fd8f6SDimitry Andric return error("Explicit call type is not a function type"); 4242ff0cc061SDimitry Andric 4243f22ef01cSRoman Divacky Value *Callee; 4244f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) 42458f0fd8f6SDimitry Andric return error("Invalid record"); 4246f22ef01cSRoman Divacky 42476122f3e6SDimitry Andric PointerType *OpTy = dyn_cast<PointerType>(Callee->getType()); 4248ff0cc061SDimitry Andric if (!OpTy) 42498f0fd8f6SDimitry Andric return error("Callee is not a pointer type"); 4250ff0cc061SDimitry Andric if (!FTy) { 4251ff0cc061SDimitry Andric FTy = dyn_cast<FunctionType>(OpTy->getElementType()); 4252ff0cc061SDimitry Andric if (!FTy) 42538f0fd8f6SDimitry Andric return error("Callee is not of pointer to function type"); 4254ff0cc061SDimitry Andric } else if (OpTy->getElementType() != FTy) 42558f0fd8f6SDimitry Andric return error("Explicit call type does not match pointee type of " 4256ff0cc061SDimitry Andric "callee operand"); 4257ff0cc061SDimitry Andric if (Record.size() < FTy->getNumParams() + OpNum) 42588f0fd8f6SDimitry Andric return error("Insufficient operands to call"); 4259f22ef01cSRoman Divacky 4260f22ef01cSRoman Divacky SmallVector<Value*, 16> Args; 4261f22ef01cSRoman Divacky // Read the fixed params. 4262f22ef01cSRoman Divacky for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { 426317a519f9SDimitry Andric if (FTy->getParamType(i)->isLabelTy()) 4264f22ef01cSRoman Divacky Args.push_back(getBasicBlock(Record[OpNum])); 4265f22ef01cSRoman Divacky else 42663861d79fSDimitry Andric Args.push_back(getValue(Record, OpNum, NextValueNo, 42673861d79fSDimitry Andric FTy->getParamType(i))); 426891bc56edSDimitry Andric if (!Args.back()) 42698f0fd8f6SDimitry Andric return error("Invalid record"); 4270f22ef01cSRoman Divacky } 4271f22ef01cSRoman Divacky 4272f22ef01cSRoman Divacky // Read type/value pairs for varargs params. 4273f22ef01cSRoman Divacky if (!FTy->isVarArg()) { 4274f22ef01cSRoman Divacky if (OpNum != Record.size()) 42758f0fd8f6SDimitry Andric return error("Invalid record"); 4276f22ef01cSRoman Divacky } else { 4277f22ef01cSRoman Divacky while (OpNum != Record.size()) { 4278f22ef01cSRoman Divacky Value *Op; 4279f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Op)) 42808f0fd8f6SDimitry Andric return error("Invalid record"); 4281f22ef01cSRoman Divacky Args.push_back(Op); 4282f22ef01cSRoman Divacky } 4283f22ef01cSRoman Divacky } 4284f22ef01cSRoman Divacky 42857d523365SDimitry Andric I = CallInst::Create(FTy, Callee, Args, OperandBundles); 42867d523365SDimitry Andric OperandBundles.clear(); 4287f22ef01cSRoman Divacky InstructionList.push_back(I); 4288f22ef01cSRoman Divacky cast<CallInst>(I)->setCallingConv( 42897d523365SDimitry Andric static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV)); 429091bc56edSDimitry Andric CallInst::TailCallKind TCK = CallInst::TCK_None; 42917d523365SDimitry Andric if (CCInfo & 1 << bitc::CALL_TAIL) 429291bc56edSDimitry Andric TCK = CallInst::TCK_Tail; 42937d523365SDimitry Andric if (CCInfo & (1 << bitc::CALL_MUSTTAIL)) 429491bc56edSDimitry Andric TCK = CallInst::TCK_MustTail; 42957d523365SDimitry Andric if (CCInfo & (1 << bitc::CALL_NOTAIL)) 42967d523365SDimitry Andric TCK = CallInst::TCK_NoTail; 429791bc56edSDimitry Andric cast<CallInst>(I)->setTailCallKind(TCK); 4298f22ef01cSRoman Divacky cast<CallInst>(I)->setAttributes(PAL); 42997d523365SDimitry Andric if (FMF.any()) { 43007d523365SDimitry Andric if (!isa<FPMathOperator>(I)) 43017d523365SDimitry Andric return error("Fast-math-flags specified for call without " 43027d523365SDimitry Andric "floating-point scalar or vector return type"); 43037d523365SDimitry Andric I->setFastMathFlags(FMF); 43047d523365SDimitry Andric } 4305f22ef01cSRoman Divacky break; 4306f22ef01cSRoman Divacky } 4307f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty] 4308f22ef01cSRoman Divacky if (Record.size() < 3) 43098f0fd8f6SDimitry Andric return error("Invalid record"); 43106122f3e6SDimitry Andric Type *OpTy = getTypeByID(Record[0]); 43113861d79fSDimitry Andric Value *Op = getValue(Record, 1, NextValueNo, OpTy); 43126122f3e6SDimitry Andric Type *ResTy = getTypeByID(Record[2]); 4313f22ef01cSRoman Divacky if (!OpTy || !Op || !ResTy) 43148f0fd8f6SDimitry Andric return error("Invalid record"); 4315f22ef01cSRoman Divacky I = new VAArgInst(Op, ResTy); 4316f22ef01cSRoman Divacky InstructionList.push_back(I); 4317f22ef01cSRoman Divacky break; 4318f22ef01cSRoman Divacky } 43197d523365SDimitry Andric 43207d523365SDimitry Andric case bitc::FUNC_CODE_OPERAND_BUNDLE: { 43217d523365SDimitry Andric // A call or an invoke can be optionally prefixed with some variable 43227d523365SDimitry Andric // number of operand bundle blocks. These blocks are read into 43237d523365SDimitry Andric // OperandBundles and consumed at the next call or invoke instruction. 43247d523365SDimitry Andric 43257d523365SDimitry Andric if (Record.size() < 1 || Record[0] >= BundleTags.size()) 43267d523365SDimitry Andric return error("Invalid record"); 43277d523365SDimitry Andric 43287d523365SDimitry Andric std::vector<Value *> Inputs; 43297d523365SDimitry Andric 43307d523365SDimitry Andric unsigned OpNum = 1; 43317d523365SDimitry Andric while (OpNum != Record.size()) { 43327d523365SDimitry Andric Value *Op; 43337d523365SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Op)) 43347d523365SDimitry Andric return error("Invalid record"); 43357d523365SDimitry Andric Inputs.push_back(Op); 43367d523365SDimitry Andric } 43377d523365SDimitry Andric 43387d523365SDimitry Andric OperandBundles.emplace_back(BundleTags[Record[0]], std::move(Inputs)); 43397d523365SDimitry Andric continue; 43407d523365SDimitry Andric } 4341f22ef01cSRoman Divacky } 4342f22ef01cSRoman Divacky 4343f22ef01cSRoman Divacky // Add instruction to end of current BB. If there is no current BB, reject 4344f22ef01cSRoman Divacky // this file. 434591bc56edSDimitry Andric if (!CurBB) { 4346f22ef01cSRoman Divacky delete I; 43478f0fd8f6SDimitry Andric return error("Invalid instruction with no BB"); 4348f22ef01cSRoman Divacky } 43497d523365SDimitry Andric if (!OperandBundles.empty()) { 43507d523365SDimitry Andric delete I; 43517d523365SDimitry Andric return error("Operand bundles found with no consumer"); 43527d523365SDimitry Andric } 4353f22ef01cSRoman Divacky CurBB->getInstList().push_back(I); 4354f22ef01cSRoman Divacky 4355f22ef01cSRoman Divacky // If this was a terminator instruction, move to the next block. 4356f22ef01cSRoman Divacky if (isa<TerminatorInst>(I)) { 4357f22ef01cSRoman Divacky ++CurBBNo; 435891bc56edSDimitry Andric CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr; 4359f22ef01cSRoman Divacky } 4360f22ef01cSRoman Divacky 4361f22ef01cSRoman Divacky // Non-void values get registered in the value table for future use. 4362f22ef01cSRoman Divacky if (I && !I->getType()->isVoidTy()) 43638f0fd8f6SDimitry Andric ValueList.assignValue(I, NextValueNo++); 4364f22ef01cSRoman Divacky } 4365f22ef01cSRoman Divacky 4366139f7f9bSDimitry Andric OutOfRecordLoop: 4367139f7f9bSDimitry Andric 43687d523365SDimitry Andric if (!OperandBundles.empty()) 43697d523365SDimitry Andric return error("Operand bundles found with no consumer"); 43707d523365SDimitry Andric 4371f22ef01cSRoman Divacky // Check the function list for unresolved values. 4372f22ef01cSRoman Divacky if (Argument *A = dyn_cast<Argument>(ValueList.back())) { 437391bc56edSDimitry Andric if (!A->getParent()) { 4374f22ef01cSRoman Divacky // We found at least one unresolved value. Nuke them all to avoid leaks. 4375f22ef01cSRoman Divacky for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){ 437691bc56edSDimitry Andric if ((A = dyn_cast_or_null<Argument>(ValueList[i])) && !A->getParent()) { 4377f22ef01cSRoman Divacky A->replaceAllUsesWith(UndefValue::get(A->getType())); 4378f22ef01cSRoman Divacky delete A; 4379f22ef01cSRoman Divacky } 4380f22ef01cSRoman Divacky } 43818f0fd8f6SDimitry Andric return error("Never resolved value found in function"); 4382f22ef01cSRoman Divacky } 4383f22ef01cSRoman Divacky } 4384f22ef01cSRoman Divacky 43853ca95b02SDimitry Andric // Unexpected unresolved metadata about to be dropped. 4386d88c1a5aSDimitry Andric if (MDLoader->hasFwdRefs()) 43873ca95b02SDimitry Andric return error("Invalid function metadata: outgoing forward refs"); 4388e580952dSDimitry Andric 4389f22ef01cSRoman Divacky // Trim the value list down to the size it was before we parsed this function. 4390f22ef01cSRoman Divacky ValueList.shrinkTo(ModuleValueListSize); 4391d88c1a5aSDimitry Andric MDLoader->shrinkTo(ModuleMDLoaderSize); 4392f22ef01cSRoman Divacky std::vector<BasicBlock*>().swap(FunctionBBs); 4393d88c1a5aSDimitry Andric return Error::success(); 4394f22ef01cSRoman Divacky } 4395f22ef01cSRoman Divacky 4396f785676fSDimitry Andric /// Find the function body in the bitcode stream 4397d88c1a5aSDimitry Andric Error BitcodeReader::findFunctionInStream( 439891bc56edSDimitry Andric Function *F, 4399dff0c46cSDimitry Andric DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) { 4400dff0c46cSDimitry Andric while (DeferredFunctionInfoIterator->second == 0) { 44017d523365SDimitry Andric // This is the fallback handling for the old format bitcode that 44027d523365SDimitry Andric // didn't contain the function index in the VST, or when we have 44037d523365SDimitry Andric // an anonymous function which would not have a VST entry. 44047d523365SDimitry Andric // Assert that we have one of those two cases. 44057d523365SDimitry Andric assert(VSTOffset == 0 || !F->hasName()); 44067d523365SDimitry Andric // Parse the next body in the stream and set its position in the 44077d523365SDimitry Andric // DeferredFunctionInfo map. 4408d88c1a5aSDimitry Andric if (Error Err = rememberAndSkipFunctionBodies()) 4409d88c1a5aSDimitry Andric return Err; 4410dff0c46cSDimitry Andric } 4411d88c1a5aSDimitry Andric return Error::success(); 4412dff0c46cSDimitry Andric } 4413dff0c46cSDimitry Andric 4414f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 4415f22ef01cSRoman Divacky // GVMaterializer implementation 4416f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 4417f22ef01cSRoman Divacky 4418d88c1a5aSDimitry Andric Error BitcodeReader::materialize(GlobalValue *GV) { 4419f22ef01cSRoman Divacky Function *F = dyn_cast<Function>(GV); 4420f22ef01cSRoman Divacky // If it's not a function or is already material, ignore the request. 4421f785676fSDimitry Andric if (!F || !F->isMaterializable()) 4422d88c1a5aSDimitry Andric return Error::success(); 4423f22ef01cSRoman Divacky 4424f22ef01cSRoman Divacky DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F); 4425f22ef01cSRoman Divacky assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!"); 4426dff0c46cSDimitry Andric // If its position is recorded as 0, its body is somewhere in the stream 4427dff0c46cSDimitry Andric // but we haven't seen it yet. 44283dac3a9bSDimitry Andric if (DFII->second == 0) 4429d88c1a5aSDimitry Andric if (Error Err = findFunctionInStream(F, DFII)) 4430d88c1a5aSDimitry Andric return Err; 4431f22ef01cSRoman Divacky 44323ca95b02SDimitry Andric // Materialize metadata before parsing any function bodies. 4433d88c1a5aSDimitry Andric if (Error Err = materializeMetadata()) 4434d88c1a5aSDimitry Andric return Err; 44353ca95b02SDimitry Andric 4436f22ef01cSRoman Divacky // Move the bit stream to the saved position of the deferred function body. 4437f22ef01cSRoman Divacky Stream.JumpToBit(DFII->second); 4438f22ef01cSRoman Divacky 4439d88c1a5aSDimitry Andric if (Error Err = parseFunctionBody(F)) 4440d88c1a5aSDimitry Andric return Err; 444139d628a0SDimitry Andric F->setIsMaterializable(false); 4442f22ef01cSRoman Divacky 4443ff0cc061SDimitry Andric if (StripDebugInfo) 4444ff0cc061SDimitry Andric stripDebugInfo(*F); 4445ff0cc061SDimitry Andric 4446f22ef01cSRoman Divacky // Upgrade any old intrinsic calls in the function. 44473dac3a9bSDimitry Andric for (auto &I : UpgradedIntrinsics) { 44487d523365SDimitry Andric for (auto UI = I.first->materialized_user_begin(), UE = I.first->user_end(); 44497d523365SDimitry Andric UI != UE;) { 44503dac3a9bSDimitry Andric User *U = *UI; 44513dac3a9bSDimitry Andric ++UI; 44523dac3a9bSDimitry Andric if (CallInst *CI = dyn_cast<CallInst>(U)) 44533dac3a9bSDimitry Andric UpgradeIntrinsicCall(CI, I.second); 4454f22ef01cSRoman Divacky } 4455f22ef01cSRoman Divacky } 4456f22ef01cSRoman Divacky 44573ca95b02SDimitry Andric // Update calls to the remangled intrinsics 44583ca95b02SDimitry Andric for (auto &I : RemangledIntrinsics) 44593ca95b02SDimitry Andric for (auto UI = I.first->materialized_user_begin(), UE = I.first->user_end(); 44603ca95b02SDimitry Andric UI != UE;) 44613ca95b02SDimitry Andric // Don't expect any other users than call sites 44623ca95b02SDimitry Andric CallSite(*UI++).setCalledFunction(I.second); 44633ca95b02SDimitry Andric 44647d523365SDimitry Andric // Finish fn->subprogram upgrade for materialized functions. 4465d88c1a5aSDimitry Andric if (DISubprogram *SP = MDLoader->lookupSubprogramForFunction(F)) 44667d523365SDimitry Andric F->setSubprogram(SP); 44677d523365SDimitry Andric 4468d88c1a5aSDimitry Andric // Check if the TBAA Metadata are valid, otherwise we will need to strip them. 4469d88c1a5aSDimitry Andric if (!MDLoader->isStrippingTBAA()) { 4470d88c1a5aSDimitry Andric for (auto &I : instructions(F)) { 4471d88c1a5aSDimitry Andric MDNode *TBAA = I.getMetadata(LLVMContext::MD_tbaa); 4472d88c1a5aSDimitry Andric if (!TBAA || TBAAVerifyHelper.visitTBAAMetadata(I, TBAA)) 4473d88c1a5aSDimitry Andric continue; 4474d88c1a5aSDimitry Andric MDLoader->setStripTBAA(true); 4475d88c1a5aSDimitry Andric stripTBAA(F->getParent()); 4476d88c1a5aSDimitry Andric } 4477d88c1a5aSDimitry Andric } 4478d88c1a5aSDimitry Andric 447939d628a0SDimitry Andric // Bring in any functions that this function forward-referenced via 448039d628a0SDimitry Andric // blockaddresses. 448139d628a0SDimitry Andric return materializeForwardReferencedFunctions(); 4482f22ef01cSRoman Divacky } 4483f22ef01cSRoman Divacky 4484d88c1a5aSDimitry Andric Error BitcodeReader::materializeModule() { 4485d88c1a5aSDimitry Andric if (Error Err = materializeMetadata()) 4486d88c1a5aSDimitry Andric return Err; 4487ff0cc061SDimitry Andric 448839d628a0SDimitry Andric // Promise to materialize all forward references. 448939d628a0SDimitry Andric WillMaterializeAllForwardRefs = true; 449039d628a0SDimitry Andric 4491f22ef01cSRoman Divacky // Iterate over the module, deserializing any functions that are still on 4492f22ef01cSRoman Divacky // disk. 44937d523365SDimitry Andric for (Function &F : *TheModule) { 4494d88c1a5aSDimitry Andric if (Error Err = materialize(&F)) 4495d88c1a5aSDimitry Andric return Err; 4496f785676fSDimitry Andric } 44977d523365SDimitry Andric // At this point, if there are any function bodies, parse the rest of 44987d523365SDimitry Andric // the bits in the module past the last function block we have recorded 44997d523365SDimitry Andric // through either lazy scanning or the VST. 45007d523365SDimitry Andric if (LastFunctionBlockBit || NextUnreadBit) 4501d88c1a5aSDimitry Andric if (Error Err = parseModule(LastFunctionBlockBit > NextUnreadBit 4502d88c1a5aSDimitry Andric ? LastFunctionBlockBit 4503d88c1a5aSDimitry Andric : NextUnreadBit)) 4504d88c1a5aSDimitry Andric return Err; 4505dff0c46cSDimitry Andric 450639d628a0SDimitry Andric // Check that all block address forward references got resolved (as we 450739d628a0SDimitry Andric // promised above). 450839d628a0SDimitry Andric if (!BasicBlockFwdRefs.empty()) 45098f0fd8f6SDimitry Andric return error("Never resolved function from blockaddress"); 451039d628a0SDimitry Andric 4511f22ef01cSRoman Divacky // Upgrade any intrinsic calls that slipped through (should not happen!) and 4512f22ef01cSRoman Divacky // delete the old functions to clean up. We can't do this unless the entire 4513f22ef01cSRoman Divacky // module is materialized because there could always be another function body 4514f22ef01cSRoman Divacky // with calls to the old function. 45153dac3a9bSDimitry Andric for (auto &I : UpgradedIntrinsics) { 45163dac3a9bSDimitry Andric for (auto *U : I.first->users()) { 45173dac3a9bSDimitry Andric if (CallInst *CI = dyn_cast<CallInst>(U)) 45183dac3a9bSDimitry Andric UpgradeIntrinsicCall(CI, I.second); 4519f22ef01cSRoman Divacky } 45203dac3a9bSDimitry Andric if (!I.first->use_empty()) 45213dac3a9bSDimitry Andric I.first->replaceAllUsesWith(I.second); 45223dac3a9bSDimitry Andric I.first->eraseFromParent(); 4523f22ef01cSRoman Divacky } 45243dac3a9bSDimitry Andric UpgradedIntrinsics.clear(); 45253ca95b02SDimitry Andric // Do the same for remangled intrinsics 45263ca95b02SDimitry Andric for (auto &I : RemangledIntrinsics) { 45273ca95b02SDimitry Andric I.first->replaceAllUsesWith(I.second); 45283ca95b02SDimitry Andric I.first->eraseFromParent(); 45293ca95b02SDimitry Andric } 45303ca95b02SDimitry Andric RemangledIntrinsics.clear(); 4531f785676fSDimitry Andric 45327d523365SDimitry Andric UpgradeDebugInfo(*TheModule); 45333ca95b02SDimitry Andric 45343ca95b02SDimitry Andric UpgradeModuleFlags(*TheModule); 4535d88c1a5aSDimitry Andric return Error::success(); 4536dff0c46cSDimitry Andric } 45376122f3e6SDimitry Andric 453839d628a0SDimitry Andric std::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const { 453939d628a0SDimitry Andric return IdentifiedStructTypes; 454039d628a0SDimitry Andric } 454139d628a0SDimitry Andric 45423ca95b02SDimitry Andric ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader( 4543d88c1a5aSDimitry Andric BitstreamCursor Cursor, ModuleSummaryIndex &TheIndex) 4544d88c1a5aSDimitry Andric : BitcodeReaderBase(std::move(Cursor)), TheIndex(TheIndex) {} 45453ca95b02SDimitry Andric 45463ca95b02SDimitry Andric std::pair<GlobalValue::GUID, GlobalValue::GUID> 45473ca95b02SDimitry Andric ModuleSummaryIndexBitcodeReader::getGUIDFromValueId(unsigned ValueId) { 45483ca95b02SDimitry Andric auto VGI = ValueIdToCallGraphGUIDMap.find(ValueId); 45493ca95b02SDimitry Andric assert(VGI != ValueIdToCallGraphGUIDMap.end()); 45503ca95b02SDimitry Andric return VGI->second; 45517d523365SDimitry Andric } 45527d523365SDimitry Andric 45533ca95b02SDimitry Andric // Specialized value symbol table parser used when reading module index 45543ca95b02SDimitry Andric // blocks where we don't actually create global values. The parsed information 45553ca95b02SDimitry Andric // is saved in the bitcode reader for use when later parsing summaries. 4556d88c1a5aSDimitry Andric Error ModuleSummaryIndexBitcodeReader::parseValueSymbolTable( 45573ca95b02SDimitry Andric uint64_t Offset, 45583ca95b02SDimitry Andric DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap) { 45593ca95b02SDimitry Andric assert(Offset > 0 && "Expected non-zero VST offset"); 45603ca95b02SDimitry Andric uint64_t CurrentBit = jumpToValueSymbolTable(Offset, Stream); 45617d523365SDimitry Andric 45627d523365SDimitry Andric if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) 45637d523365SDimitry Andric return error("Invalid record"); 45647d523365SDimitry Andric 45657d523365SDimitry Andric SmallVector<uint64_t, 64> Record; 45667d523365SDimitry Andric 45677d523365SDimitry Andric // Read all the records for this value table. 45687d523365SDimitry Andric SmallString<128> ValueName; 4569d88c1a5aSDimitry Andric 4570d88c1a5aSDimitry Andric while (true) { 45717d523365SDimitry Andric BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 45727d523365SDimitry Andric 45737d523365SDimitry Andric switch (Entry.Kind) { 45747d523365SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 45757d523365SDimitry Andric case BitstreamEntry::Error: 45767d523365SDimitry Andric return error("Malformed block"); 45777d523365SDimitry Andric case BitstreamEntry::EndBlock: 45783ca95b02SDimitry Andric // Done parsing VST, jump back to wherever we came from. 45793ca95b02SDimitry Andric Stream.JumpToBit(CurrentBit); 4580d88c1a5aSDimitry Andric return Error::success(); 45817d523365SDimitry Andric case BitstreamEntry::Record: 45827d523365SDimitry Andric // The interesting case. 45837d523365SDimitry Andric break; 45847d523365SDimitry Andric } 45857d523365SDimitry Andric 45867d523365SDimitry Andric // Read a record. 45877d523365SDimitry Andric Record.clear(); 45887d523365SDimitry Andric switch (Stream.readRecord(Entry.ID, Record)) { 45897d523365SDimitry Andric default: // Default behavior: ignore (e.g. VST_CODE_BBENTRY records). 45907d523365SDimitry Andric break; 45913ca95b02SDimitry Andric case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N] 45923ca95b02SDimitry Andric if (convertToString(Record, 1, ValueName)) 45933ca95b02SDimitry Andric return error("Invalid record"); 45943ca95b02SDimitry Andric unsigned ValueID = Record[0]; 45953ca95b02SDimitry Andric assert(!SourceFileName.empty()); 45963ca95b02SDimitry Andric auto VLI = ValueIdToLinkageMap.find(ValueID); 45973ca95b02SDimitry Andric assert(VLI != ValueIdToLinkageMap.end() && 45983ca95b02SDimitry Andric "No linkage found for VST entry?"); 45993ca95b02SDimitry Andric auto Linkage = VLI->second; 46003ca95b02SDimitry Andric std::string GlobalId = 46013ca95b02SDimitry Andric GlobalValue::getGlobalIdentifier(ValueName, Linkage, SourceFileName); 46023ca95b02SDimitry Andric auto ValueGUID = GlobalValue::getGUID(GlobalId); 46033ca95b02SDimitry Andric auto OriginalNameID = ValueGUID; 46043ca95b02SDimitry Andric if (GlobalValue::isLocalLinkage(Linkage)) 46053ca95b02SDimitry Andric OriginalNameID = GlobalValue::getGUID(ValueName); 46063ca95b02SDimitry Andric if (PrintSummaryGUIDs) 46073ca95b02SDimitry Andric dbgs() << "GUID " << ValueGUID << "(" << OriginalNameID << ") is " 46083ca95b02SDimitry Andric << ValueName << "\n"; 46093ca95b02SDimitry Andric ValueIdToCallGraphGUIDMap[ValueID] = 46103ca95b02SDimitry Andric std::make_pair(ValueGUID, OriginalNameID); 46113ca95b02SDimitry Andric ValueName.clear(); 46123ca95b02SDimitry Andric break; 46133ca95b02SDimitry Andric } 46147d523365SDimitry Andric case bitc::VST_CODE_FNENTRY: { 46153ca95b02SDimitry Andric // VST_CODE_FNENTRY: [valueid, offset, namechar x N] 46167d523365SDimitry Andric if (convertToString(Record, 2, ValueName)) 46177d523365SDimitry Andric return error("Invalid record"); 46187d523365SDimitry Andric unsigned ValueID = Record[0]; 46193ca95b02SDimitry Andric assert(!SourceFileName.empty()); 46203ca95b02SDimitry Andric auto VLI = ValueIdToLinkageMap.find(ValueID); 46213ca95b02SDimitry Andric assert(VLI != ValueIdToLinkageMap.end() && 46223ca95b02SDimitry Andric "No linkage found for VST entry?"); 46233ca95b02SDimitry Andric auto Linkage = VLI->second; 46243ca95b02SDimitry Andric std::string FunctionGlobalId = GlobalValue::getGlobalIdentifier( 46253ca95b02SDimitry Andric ValueName, VLI->second, SourceFileName); 46263ca95b02SDimitry Andric auto FunctionGUID = GlobalValue::getGUID(FunctionGlobalId); 46273ca95b02SDimitry Andric auto OriginalNameID = FunctionGUID; 46283ca95b02SDimitry Andric if (GlobalValue::isLocalLinkage(Linkage)) 46293ca95b02SDimitry Andric OriginalNameID = GlobalValue::getGUID(ValueName); 46303ca95b02SDimitry Andric if (PrintSummaryGUIDs) 46313ca95b02SDimitry Andric dbgs() << "GUID " << FunctionGUID << "(" << OriginalNameID << ") is " 46323ca95b02SDimitry Andric << ValueName << "\n"; 46333ca95b02SDimitry Andric ValueIdToCallGraphGUIDMap[ValueID] = 46343ca95b02SDimitry Andric std::make_pair(FunctionGUID, OriginalNameID); 46357d523365SDimitry Andric 46367d523365SDimitry Andric ValueName.clear(); 46377d523365SDimitry Andric break; 46387d523365SDimitry Andric } 46393ca95b02SDimitry Andric case bitc::VST_CODE_COMBINED_ENTRY: { 46403ca95b02SDimitry Andric // VST_CODE_COMBINED_ENTRY: [valueid, refguid] 46413ca95b02SDimitry Andric unsigned ValueID = Record[0]; 46423ca95b02SDimitry Andric GlobalValue::GUID RefGUID = Record[1]; 46433ca95b02SDimitry Andric // The "original name", which is the second value of the pair will be 46443ca95b02SDimitry Andric // overriden later by a FS_COMBINED_ORIGINAL_NAME in the combined index. 46453ca95b02SDimitry Andric ValueIdToCallGraphGUIDMap[ValueID] = std::make_pair(RefGUID, RefGUID); 46467d523365SDimitry Andric break; 46477d523365SDimitry Andric } 46487d523365SDimitry Andric } 46497d523365SDimitry Andric } 46507d523365SDimitry Andric } 46517d523365SDimitry Andric 46523ca95b02SDimitry Andric // Parse just the blocks needed for building the index out of the module. 46533ca95b02SDimitry Andric // At the end of this routine the module Index is populated with a map 46543ca95b02SDimitry Andric // from global value id to GlobalValueSummary objects. 4655d88c1a5aSDimitry Andric Error ModuleSummaryIndexBitcodeReader::parseModule(StringRef ModulePath) { 46567d523365SDimitry Andric if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 46577d523365SDimitry Andric return error("Invalid record"); 46587d523365SDimitry Andric 46593ca95b02SDimitry Andric SmallVector<uint64_t, 64> Record; 46603ca95b02SDimitry Andric DenseMap<unsigned, GlobalValue::LinkageTypes> ValueIdToLinkageMap; 46613ca95b02SDimitry Andric unsigned ValueId = 0; 46623ca95b02SDimitry Andric 46633ca95b02SDimitry Andric // Read the index for this module. 4664d88c1a5aSDimitry Andric while (true) { 46657d523365SDimitry Andric BitstreamEntry Entry = Stream.advance(); 46667d523365SDimitry Andric 46677d523365SDimitry Andric switch (Entry.Kind) { 46687d523365SDimitry Andric case BitstreamEntry::Error: 46697d523365SDimitry Andric return error("Malformed block"); 46707d523365SDimitry Andric case BitstreamEntry::EndBlock: 4671d88c1a5aSDimitry Andric return Error::success(); 46727d523365SDimitry Andric 46737d523365SDimitry Andric case BitstreamEntry::SubBlock: 46747d523365SDimitry Andric switch (Entry.ID) { 46757d523365SDimitry Andric default: // Skip unknown content. 46767d523365SDimitry Andric if (Stream.SkipBlock()) 46777d523365SDimitry Andric return error("Invalid record"); 46787d523365SDimitry Andric break; 46797d523365SDimitry Andric case bitc::BLOCKINFO_BLOCK_ID: 46807d523365SDimitry Andric // Need to parse these to get abbrev ids (e.g. for VST) 4681d88c1a5aSDimitry Andric if (readBlockInfo()) 46827d523365SDimitry Andric return error("Malformed block"); 46837d523365SDimitry Andric break; 46847d523365SDimitry Andric case bitc::VALUE_SYMTAB_BLOCK_ID: 46853ca95b02SDimitry Andric // Should have been parsed earlier via VSTOffset, unless there 46863ca95b02SDimitry Andric // is no summary section. 46873ca95b02SDimitry Andric assert(((SeenValueSymbolTable && VSTOffset > 0) || 46883ca95b02SDimitry Andric !SeenGlobalValSummary) && 46893ca95b02SDimitry Andric "Expected early VST parse via VSTOffset record"); 46907d523365SDimitry Andric if (Stream.SkipBlock()) 46917d523365SDimitry Andric return error("Invalid record"); 46923ca95b02SDimitry Andric break; 46933ca95b02SDimitry Andric case bitc::GLOBALVAL_SUMMARY_BLOCK_ID: 46943ca95b02SDimitry Andric assert(!SeenValueSymbolTable && 46953ca95b02SDimitry Andric "Already read VST when parsing summary block?"); 4696d88c1a5aSDimitry Andric // We might not have a VST if there were no values in the 4697d88c1a5aSDimitry Andric // summary. An empty summary block generated when we are 4698d88c1a5aSDimitry Andric // performing ThinLTO compiles so we don't later invoke 4699d88c1a5aSDimitry Andric // the regular LTO process on them. 4700d88c1a5aSDimitry Andric if (VSTOffset > 0) { 4701d88c1a5aSDimitry Andric if (Error Err = parseValueSymbolTable(VSTOffset, ValueIdToLinkageMap)) 4702d88c1a5aSDimitry Andric return Err; 47033ca95b02SDimitry Andric SeenValueSymbolTable = true; 4704d88c1a5aSDimitry Andric } 47053ca95b02SDimitry Andric SeenGlobalValSummary = true; 4706d88c1a5aSDimitry Andric if (Error Err = parseEntireSummary(ModulePath)) 4707d88c1a5aSDimitry Andric return Err; 47087d523365SDimitry Andric break; 47097d523365SDimitry Andric case bitc::MODULE_STRTAB_BLOCK_ID: 4710d88c1a5aSDimitry Andric if (Error Err = parseModuleStringTable()) 4711d88c1a5aSDimitry Andric return Err; 47127d523365SDimitry Andric break; 47137d523365SDimitry Andric } 47147d523365SDimitry Andric continue; 47157d523365SDimitry Andric 47163ca95b02SDimitry Andric case BitstreamEntry::Record: { 47173ca95b02SDimitry Andric Record.clear(); 47183ca95b02SDimitry Andric auto BitCode = Stream.readRecord(Entry.ID, Record); 47193ca95b02SDimitry Andric switch (BitCode) { 47203ca95b02SDimitry Andric default: 47213ca95b02SDimitry Andric break; // Default behavior, ignore unknown content. 47223ca95b02SDimitry Andric /// MODULE_CODE_SOURCE_FILENAME: [namechar x N] 47233ca95b02SDimitry Andric case bitc::MODULE_CODE_SOURCE_FILENAME: { 47243ca95b02SDimitry Andric SmallString<128> ValueName; 47253ca95b02SDimitry Andric if (convertToString(Record, 0, ValueName)) 47263ca95b02SDimitry Andric return error("Invalid record"); 47273ca95b02SDimitry Andric SourceFileName = ValueName.c_str(); 47283ca95b02SDimitry Andric break; 47293ca95b02SDimitry Andric } 47303ca95b02SDimitry Andric /// MODULE_CODE_HASH: [5*i32] 47313ca95b02SDimitry Andric case bitc::MODULE_CODE_HASH: { 47323ca95b02SDimitry Andric if (Record.size() != 5) 47333ca95b02SDimitry Andric return error("Invalid hash length " + Twine(Record.size()).str()); 4734d88c1a5aSDimitry Andric if (TheIndex.modulePaths().empty()) 4735d88c1a5aSDimitry Andric // We always seed the index with the module. 4736d88c1a5aSDimitry Andric TheIndex.addModulePath(ModulePath, 0); 4737d88c1a5aSDimitry Andric if (TheIndex.modulePaths().size() != 1) 47383ca95b02SDimitry Andric return error("Don't expect multiple modules defined?"); 4739d88c1a5aSDimitry Andric auto &Hash = TheIndex.modulePaths().begin()->second.second; 47403ca95b02SDimitry Andric int Pos = 0; 47413ca95b02SDimitry Andric for (auto &Val : Record) { 47423ca95b02SDimitry Andric assert(!(Val >> 32) && "Unexpected high bits set"); 47433ca95b02SDimitry Andric Hash[Pos++] = Val; 47443ca95b02SDimitry Andric } 47453ca95b02SDimitry Andric break; 47463ca95b02SDimitry Andric } 47473ca95b02SDimitry Andric /// MODULE_CODE_VSTOFFSET: [offset] 47483ca95b02SDimitry Andric case bitc::MODULE_CODE_VSTOFFSET: 47493ca95b02SDimitry Andric if (Record.size() < 1) 47503ca95b02SDimitry Andric return error("Invalid record"); 4751d88c1a5aSDimitry Andric // Note that we subtract 1 here because the offset is relative to one 4752d88c1a5aSDimitry Andric // word before the start of the identification or module block, which 4753d88c1a5aSDimitry Andric // was historically always the start of the regular bitcode header. 4754d88c1a5aSDimitry Andric VSTOffset = Record[0] - 1; 47553ca95b02SDimitry Andric break; 47563ca95b02SDimitry Andric // GLOBALVAR: [pointer type, isconst, initid, 47573ca95b02SDimitry Andric // linkage, alignment, section, visibility, threadlocal, 47583ca95b02SDimitry Andric // unnamed_addr, externally_initialized, dllstorageclass, 47593ca95b02SDimitry Andric // comdat] 47603ca95b02SDimitry Andric case bitc::MODULE_CODE_GLOBALVAR: { 47613ca95b02SDimitry Andric if (Record.size() < 6) 47623ca95b02SDimitry Andric return error("Invalid record"); 47633ca95b02SDimitry Andric uint64_t RawLinkage = Record[3]; 47643ca95b02SDimitry Andric GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage); 47653ca95b02SDimitry Andric ValueIdToLinkageMap[ValueId++] = Linkage; 47663ca95b02SDimitry Andric break; 47673ca95b02SDimitry Andric } 47683ca95b02SDimitry Andric // FUNCTION: [type, callingconv, isproto, linkage, paramattr, 47693ca95b02SDimitry Andric // alignment, section, visibility, gc, unnamed_addr, 47703ca95b02SDimitry Andric // prologuedata, dllstorageclass, comdat, prefixdata] 47713ca95b02SDimitry Andric case bitc::MODULE_CODE_FUNCTION: { 47723ca95b02SDimitry Andric if (Record.size() < 8) 47733ca95b02SDimitry Andric return error("Invalid record"); 47743ca95b02SDimitry Andric uint64_t RawLinkage = Record[3]; 47753ca95b02SDimitry Andric GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage); 47763ca95b02SDimitry Andric ValueIdToLinkageMap[ValueId++] = Linkage; 47773ca95b02SDimitry Andric break; 47783ca95b02SDimitry Andric } 47793ca95b02SDimitry Andric // ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility, 47803ca95b02SDimitry Andric // dllstorageclass] 47813ca95b02SDimitry Andric case bitc::MODULE_CODE_ALIAS: { 47823ca95b02SDimitry Andric if (Record.size() < 6) 47833ca95b02SDimitry Andric return error("Invalid record"); 47843ca95b02SDimitry Andric uint64_t RawLinkage = Record[3]; 47853ca95b02SDimitry Andric GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage); 47863ca95b02SDimitry Andric ValueIdToLinkageMap[ValueId++] = Linkage; 47873ca95b02SDimitry Andric break; 47883ca95b02SDimitry Andric } 47893ca95b02SDimitry Andric } 47903ca95b02SDimitry Andric } 47917d523365SDimitry Andric continue; 47927d523365SDimitry Andric } 47937d523365SDimitry Andric } 47947d523365SDimitry Andric } 47957d523365SDimitry Andric 4796d88c1a5aSDimitry Andric std::vector<ValueInfo> 4797d88c1a5aSDimitry Andric ModuleSummaryIndexBitcodeReader::makeRefList(ArrayRef<uint64_t> Record) { 4798d88c1a5aSDimitry Andric std::vector<ValueInfo> Ret; 4799d88c1a5aSDimitry Andric Ret.reserve(Record.size()); 4800d88c1a5aSDimitry Andric for (uint64_t RefValueId : Record) 4801d88c1a5aSDimitry Andric Ret.push_back(getGUIDFromValueId(RefValueId).first); 4802d88c1a5aSDimitry Andric return Ret; 4803d88c1a5aSDimitry Andric } 4804d88c1a5aSDimitry Andric 4805d88c1a5aSDimitry Andric std::vector<FunctionSummary::EdgeTy> ModuleSummaryIndexBitcodeReader::makeCallList( 4806d88c1a5aSDimitry Andric ArrayRef<uint64_t> Record, bool IsOldProfileFormat, bool HasProfile) { 4807d88c1a5aSDimitry Andric std::vector<FunctionSummary::EdgeTy> Ret; 4808d88c1a5aSDimitry Andric Ret.reserve(Record.size()); 4809d88c1a5aSDimitry Andric for (unsigned I = 0, E = Record.size(); I != E; ++I) { 4810d88c1a5aSDimitry Andric CalleeInfo::HotnessType Hotness = CalleeInfo::HotnessType::Unknown; 4811d88c1a5aSDimitry Andric GlobalValue::GUID CalleeGUID = getGUIDFromValueId(Record[I]).first; 4812d88c1a5aSDimitry Andric if (IsOldProfileFormat) { 4813d88c1a5aSDimitry Andric I += 1; // Skip old callsitecount field 4814d88c1a5aSDimitry Andric if (HasProfile) 4815d88c1a5aSDimitry Andric I += 1; // Skip old profilecount field 4816d88c1a5aSDimitry Andric } else if (HasProfile) 4817d88c1a5aSDimitry Andric Hotness = static_cast<CalleeInfo::HotnessType>(Record[++I]); 4818d88c1a5aSDimitry Andric Ret.push_back(FunctionSummary::EdgeTy{CalleeGUID, CalleeInfo{Hotness}}); 4819d88c1a5aSDimitry Andric } 4820d88c1a5aSDimitry Andric return Ret; 4821d88c1a5aSDimitry Andric } 4822d88c1a5aSDimitry Andric 48233ca95b02SDimitry Andric // Eagerly parse the entire summary block. This populates the GlobalValueSummary 48243ca95b02SDimitry Andric // objects in the index. 4825d88c1a5aSDimitry Andric Error ModuleSummaryIndexBitcodeReader::parseEntireSummary( 4826d88c1a5aSDimitry Andric StringRef ModulePath) { 48273ca95b02SDimitry Andric if (Stream.EnterSubBlock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID)) 48287d523365SDimitry Andric return error("Invalid record"); 48297d523365SDimitry Andric SmallVector<uint64_t, 64> Record; 48307d523365SDimitry Andric 48313ca95b02SDimitry Andric // Parse version 48323ca95b02SDimitry Andric { 48333ca95b02SDimitry Andric BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 48343ca95b02SDimitry Andric if (Entry.Kind != BitstreamEntry::Record) 48353ca95b02SDimitry Andric return error("Invalid Summary Block: record for version expected"); 48363ca95b02SDimitry Andric if (Stream.readRecord(Entry.ID, Record) != bitc::FS_VERSION) 48373ca95b02SDimitry Andric return error("Invalid Summary Block: version expected"); 48383ca95b02SDimitry Andric } 48393ca95b02SDimitry Andric const uint64_t Version = Record[0]; 4840d88c1a5aSDimitry Andric const bool IsOldProfileFormat = Version == 1; 484195ec533aSDimitry Andric if (Version < 1 || Version > 3) 4842d88c1a5aSDimitry Andric return error("Invalid summary version " + Twine(Version) + 484395ec533aSDimitry Andric ", 1, 2 or 3 expected"); 48443ca95b02SDimitry Andric Record.clear(); 48453ca95b02SDimitry Andric 48463ca95b02SDimitry Andric // Keep around the last seen summary to be used when we see an optional 48473ca95b02SDimitry Andric // "OriginalName" attachement. 48483ca95b02SDimitry Andric GlobalValueSummary *LastSeenSummary = nullptr; 48493ca95b02SDimitry Andric bool Combined = false; 4850d88c1a5aSDimitry Andric std::vector<GlobalValue::GUID> PendingTypeTests; 4851d88c1a5aSDimitry Andric 4852d88c1a5aSDimitry Andric while (true) { 48537d523365SDimitry Andric BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 48547d523365SDimitry Andric 48557d523365SDimitry Andric switch (Entry.Kind) { 48567d523365SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 48577d523365SDimitry Andric case BitstreamEntry::Error: 48587d523365SDimitry Andric return error("Malformed block"); 48597d523365SDimitry Andric case BitstreamEntry::EndBlock: 48603ca95b02SDimitry Andric // For a per-module index, remove any entries that still have empty 48613ca95b02SDimitry Andric // summaries. The VST parsing creates entries eagerly for all symbols, 48623ca95b02SDimitry Andric // but not all have associated summaries (e.g. it doesn't know how to 48633ca95b02SDimitry Andric // distinguish between VST_CODE_ENTRY for function declarations vs global 48643ca95b02SDimitry Andric // variables with initializers that end up with a summary). Remove those 48653ca95b02SDimitry Andric // entries now so that we don't need to rely on the combined index merger 48663ca95b02SDimitry Andric // to clean them up (especially since that may not run for the first 48673ca95b02SDimitry Andric // module's index if we merge into that). 48683ca95b02SDimitry Andric if (!Combined) 4869d88c1a5aSDimitry Andric TheIndex.removeEmptySummaryEntries(); 4870d88c1a5aSDimitry Andric return Error::success(); 48717d523365SDimitry Andric case BitstreamEntry::Record: 48727d523365SDimitry Andric // The interesting case. 48737d523365SDimitry Andric break; 48747d523365SDimitry Andric } 48757d523365SDimitry Andric 48767d523365SDimitry Andric // Read a record. The record format depends on whether this 48777d523365SDimitry Andric // is a per-module index or a combined index file. In the per-module 48787d523365SDimitry Andric // case the records contain the associated value's ID for correlation 48797d523365SDimitry Andric // with VST entries. In the combined index the correlation is done 48807d523365SDimitry Andric // via the bitcode offset of the summary records (which were saved 48817d523365SDimitry Andric // in the combined index VST entries). The records also contain 48827d523365SDimitry Andric // information used for ThinLTO renaming and importing. 48837d523365SDimitry Andric Record.clear(); 48843ca95b02SDimitry Andric auto BitCode = Stream.readRecord(Entry.ID, Record); 48853ca95b02SDimitry Andric switch (BitCode) { 48867d523365SDimitry Andric default: // Default behavior: ignore. 48877d523365SDimitry Andric break; 48883ca95b02SDimitry Andric // FS_PERMODULE: [valueid, flags, instcount, numrefs, numrefs x valueid, 4889d88c1a5aSDimitry Andric // n x (valueid)] 48903ca95b02SDimitry Andric // FS_PERMODULE_PROFILE: [valueid, flags, instcount, numrefs, 48913ca95b02SDimitry Andric // numrefs x valueid, 4892d88c1a5aSDimitry Andric // n x (valueid, hotness)] 48933ca95b02SDimitry Andric case bitc::FS_PERMODULE: 48943ca95b02SDimitry Andric case bitc::FS_PERMODULE_PROFILE: { 48957d523365SDimitry Andric unsigned ValueID = Record[0]; 48963ca95b02SDimitry Andric uint64_t RawFlags = Record[1]; 48977d523365SDimitry Andric unsigned InstCount = Record[2]; 48983ca95b02SDimitry Andric unsigned NumRefs = Record[3]; 48993ca95b02SDimitry Andric auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 49007d523365SDimitry Andric // The module path string ref set in the summary must be owned by the 49017d523365SDimitry Andric // index's module string table. Since we don't have a module path 49027d523365SDimitry Andric // string table section in the per-module index, we create a single 49037d523365SDimitry Andric // module path string table entry with an empty (0) ID to take 49047d523365SDimitry Andric // ownership. 49053ca95b02SDimitry Andric static int RefListStartIndex = 4; 49063ca95b02SDimitry Andric int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs; 49073ca95b02SDimitry Andric assert(Record.size() >= RefListStartIndex + NumRefs && 49083ca95b02SDimitry Andric "Record size inconsistent with number of references"); 4909d88c1a5aSDimitry Andric std::vector<ValueInfo> Refs = makeRefList( 4910d88c1a5aSDimitry Andric ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs)); 49113ca95b02SDimitry Andric bool HasProfile = (BitCode == bitc::FS_PERMODULE_PROFILE); 4912d88c1a5aSDimitry Andric std::vector<FunctionSummary::EdgeTy> Calls = makeCallList( 4913d88c1a5aSDimitry Andric ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex), 4914d88c1a5aSDimitry Andric IsOldProfileFormat, HasProfile); 4915d88c1a5aSDimitry Andric auto FS = llvm::make_unique<FunctionSummary>( 4916d88c1a5aSDimitry Andric Flags, InstCount, std::move(Refs), std::move(Calls), 4917d88c1a5aSDimitry Andric std::move(PendingTypeTests)); 4918d88c1a5aSDimitry Andric PendingTypeTests.clear(); 49193ca95b02SDimitry Andric auto GUID = getGUIDFromValueId(ValueID); 4920d88c1a5aSDimitry Andric FS->setModulePath(TheIndex.addModulePath(ModulePath, 0)->first()); 49213ca95b02SDimitry Andric FS->setOriginalName(GUID.second); 4922d88c1a5aSDimitry Andric TheIndex.addGlobalValueSummary(GUID.first, std::move(FS)); 49233ca95b02SDimitry Andric break; 49243ca95b02SDimitry Andric } 49253ca95b02SDimitry Andric // FS_ALIAS: [valueid, flags, valueid] 49263ca95b02SDimitry Andric // Aliases must be emitted (and parsed) after all FS_PERMODULE entries, as 49273ca95b02SDimitry Andric // they expect all aliasee summaries to be available. 49283ca95b02SDimitry Andric case bitc::FS_ALIAS: { 49293ca95b02SDimitry Andric unsigned ValueID = Record[0]; 49303ca95b02SDimitry Andric uint64_t RawFlags = Record[1]; 49313ca95b02SDimitry Andric unsigned AliaseeID = Record[2]; 49323ca95b02SDimitry Andric auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 4933d88c1a5aSDimitry Andric auto AS = 4934d88c1a5aSDimitry Andric llvm::make_unique<AliasSummary>(Flags, std::vector<ValueInfo>{}); 49353ca95b02SDimitry Andric // The module path string ref set in the summary must be owned by the 49363ca95b02SDimitry Andric // index's module string table. Since we don't have a module path 49373ca95b02SDimitry Andric // string table section in the per-module index, we create a single 49383ca95b02SDimitry Andric // module path string table entry with an empty (0) ID to take 49393ca95b02SDimitry Andric // ownership. 4940d88c1a5aSDimitry Andric AS->setModulePath(TheIndex.addModulePath(ModulePath, 0)->first()); 49413ca95b02SDimitry Andric 49423ca95b02SDimitry Andric GlobalValue::GUID AliaseeGUID = getGUIDFromValueId(AliaseeID).first; 4943d88c1a5aSDimitry Andric auto *AliaseeSummary = TheIndex.getGlobalValueSummary(AliaseeGUID); 49443ca95b02SDimitry Andric if (!AliaseeSummary) 49453ca95b02SDimitry Andric return error("Alias expects aliasee summary to be parsed"); 49463ca95b02SDimitry Andric AS->setAliasee(AliaseeSummary); 49473ca95b02SDimitry Andric 49483ca95b02SDimitry Andric auto GUID = getGUIDFromValueId(ValueID); 49493ca95b02SDimitry Andric AS->setOriginalName(GUID.second); 4950d88c1a5aSDimitry Andric TheIndex.addGlobalValueSummary(GUID.first, std::move(AS)); 49513ca95b02SDimitry Andric break; 49523ca95b02SDimitry Andric } 49533ca95b02SDimitry Andric // FS_PERMODULE_GLOBALVAR_INIT_REFS: [valueid, flags, n x valueid] 49543ca95b02SDimitry Andric case bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS: { 49553ca95b02SDimitry Andric unsigned ValueID = Record[0]; 49563ca95b02SDimitry Andric uint64_t RawFlags = Record[1]; 49573ca95b02SDimitry Andric auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 4958d88c1a5aSDimitry Andric std::vector<ValueInfo> Refs = 4959d88c1a5aSDimitry Andric makeRefList(ArrayRef<uint64_t>(Record).slice(2)); 4960d88c1a5aSDimitry Andric auto FS = llvm::make_unique<GlobalVarSummary>(Flags, std::move(Refs)); 4961d88c1a5aSDimitry Andric FS->setModulePath(TheIndex.addModulePath(ModulePath, 0)->first()); 49623ca95b02SDimitry Andric auto GUID = getGUIDFromValueId(ValueID); 49633ca95b02SDimitry Andric FS->setOriginalName(GUID.second); 4964d88c1a5aSDimitry Andric TheIndex.addGlobalValueSummary(GUID.first, std::move(FS)); 49653ca95b02SDimitry Andric break; 49663ca95b02SDimitry Andric } 49673ca95b02SDimitry Andric // FS_COMBINED: [valueid, modid, flags, instcount, numrefs, 4968d88c1a5aSDimitry Andric // numrefs x valueid, n x (valueid)] 49693ca95b02SDimitry Andric // FS_COMBINED_PROFILE: [valueid, modid, flags, instcount, numrefs, 4970d88c1a5aSDimitry Andric // numrefs x valueid, n x (valueid, hotness)] 49713ca95b02SDimitry Andric case bitc::FS_COMBINED: 49723ca95b02SDimitry Andric case bitc::FS_COMBINED_PROFILE: { 49733ca95b02SDimitry Andric unsigned ValueID = Record[0]; 49743ca95b02SDimitry Andric uint64_t ModuleId = Record[1]; 49753ca95b02SDimitry Andric uint64_t RawFlags = Record[2]; 49763ca95b02SDimitry Andric unsigned InstCount = Record[3]; 49773ca95b02SDimitry Andric unsigned NumRefs = Record[4]; 49783ca95b02SDimitry Andric auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 49793ca95b02SDimitry Andric static int RefListStartIndex = 5; 49803ca95b02SDimitry Andric int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs; 49813ca95b02SDimitry Andric assert(Record.size() >= RefListStartIndex + NumRefs && 49823ca95b02SDimitry Andric "Record size inconsistent with number of references"); 4983d88c1a5aSDimitry Andric std::vector<ValueInfo> Refs = makeRefList( 4984d88c1a5aSDimitry Andric ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs)); 49853ca95b02SDimitry Andric bool HasProfile = (BitCode == bitc::FS_COMBINED_PROFILE); 4986d88c1a5aSDimitry Andric std::vector<FunctionSummary::EdgeTy> Edges = makeCallList( 4987d88c1a5aSDimitry Andric ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex), 4988d88c1a5aSDimitry Andric IsOldProfileFormat, HasProfile); 49893ca95b02SDimitry Andric GlobalValue::GUID GUID = getGUIDFromValueId(ValueID).first; 4990d88c1a5aSDimitry Andric auto FS = llvm::make_unique<FunctionSummary>( 4991d88c1a5aSDimitry Andric Flags, InstCount, std::move(Refs), std::move(Edges), 4992d88c1a5aSDimitry Andric std::move(PendingTypeTests)); 4993d88c1a5aSDimitry Andric PendingTypeTests.clear(); 4994d88c1a5aSDimitry Andric LastSeenSummary = FS.get(); 4995d88c1a5aSDimitry Andric FS->setModulePath(ModuleIdMap[ModuleId]); 4996d88c1a5aSDimitry Andric TheIndex.addGlobalValueSummary(GUID, std::move(FS)); 49973ca95b02SDimitry Andric Combined = true; 49983ca95b02SDimitry Andric break; 49993ca95b02SDimitry Andric } 50003ca95b02SDimitry Andric // FS_COMBINED_ALIAS: [valueid, modid, flags, valueid] 50013ca95b02SDimitry Andric // Aliases must be emitted (and parsed) after all FS_COMBINED entries, as 50023ca95b02SDimitry Andric // they expect all aliasee summaries to be available. 50033ca95b02SDimitry Andric case bitc::FS_COMBINED_ALIAS: { 50043ca95b02SDimitry Andric unsigned ValueID = Record[0]; 50053ca95b02SDimitry Andric uint64_t ModuleId = Record[1]; 50063ca95b02SDimitry Andric uint64_t RawFlags = Record[2]; 50073ca95b02SDimitry Andric unsigned AliaseeValueId = Record[3]; 50083ca95b02SDimitry Andric auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 5009d88c1a5aSDimitry Andric auto AS = llvm::make_unique<AliasSummary>(Flags, std::vector<ValueInfo>{}); 50103ca95b02SDimitry Andric LastSeenSummary = AS.get(); 50113ca95b02SDimitry Andric AS->setModulePath(ModuleIdMap[ModuleId]); 50123ca95b02SDimitry Andric 50133ca95b02SDimitry Andric auto AliaseeGUID = getGUIDFromValueId(AliaseeValueId).first; 50143ca95b02SDimitry Andric auto AliaseeInModule = 5015d88c1a5aSDimitry Andric TheIndex.findSummaryInModule(AliaseeGUID, AS->modulePath()); 50163ca95b02SDimitry Andric if (!AliaseeInModule) 50173ca95b02SDimitry Andric return error("Alias expects aliasee summary to be parsed"); 50183ca95b02SDimitry Andric AS->setAliasee(AliaseeInModule); 50193ca95b02SDimitry Andric 50203ca95b02SDimitry Andric GlobalValue::GUID GUID = getGUIDFromValueId(ValueID).first; 5021d88c1a5aSDimitry Andric TheIndex.addGlobalValueSummary(GUID, std::move(AS)); 50223ca95b02SDimitry Andric Combined = true; 50233ca95b02SDimitry Andric break; 50243ca95b02SDimitry Andric } 50253ca95b02SDimitry Andric // FS_COMBINED_GLOBALVAR_INIT_REFS: [valueid, modid, flags, n x valueid] 50263ca95b02SDimitry Andric case bitc::FS_COMBINED_GLOBALVAR_INIT_REFS: { 50273ca95b02SDimitry Andric unsigned ValueID = Record[0]; 50283ca95b02SDimitry Andric uint64_t ModuleId = Record[1]; 50293ca95b02SDimitry Andric uint64_t RawFlags = Record[2]; 50303ca95b02SDimitry Andric auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 5031d88c1a5aSDimitry Andric std::vector<ValueInfo> Refs = 5032d88c1a5aSDimitry Andric makeRefList(ArrayRef<uint64_t>(Record).slice(3)); 5033d88c1a5aSDimitry Andric auto FS = llvm::make_unique<GlobalVarSummary>(Flags, std::move(Refs)); 50343ca95b02SDimitry Andric LastSeenSummary = FS.get(); 50353ca95b02SDimitry Andric FS->setModulePath(ModuleIdMap[ModuleId]); 50363ca95b02SDimitry Andric GlobalValue::GUID GUID = getGUIDFromValueId(ValueID).first; 5037d88c1a5aSDimitry Andric TheIndex.addGlobalValueSummary(GUID, std::move(FS)); 50383ca95b02SDimitry Andric Combined = true; 50393ca95b02SDimitry Andric break; 50403ca95b02SDimitry Andric } 50413ca95b02SDimitry Andric // FS_COMBINED_ORIGINAL_NAME: [original_name] 50423ca95b02SDimitry Andric case bitc::FS_COMBINED_ORIGINAL_NAME: { 50433ca95b02SDimitry Andric uint64_t OriginalName = Record[0]; 50443ca95b02SDimitry Andric if (!LastSeenSummary) 50453ca95b02SDimitry Andric return error("Name attachment that does not follow a combined record"); 50463ca95b02SDimitry Andric LastSeenSummary->setOriginalName(OriginalName); 50473ca95b02SDimitry Andric // Reset the LastSeenSummary 50483ca95b02SDimitry Andric LastSeenSummary = nullptr; 5049d88c1a5aSDimitry Andric break; 5050d88c1a5aSDimitry Andric } 5051d88c1a5aSDimitry Andric case bitc::FS_TYPE_TESTS: { 5052d88c1a5aSDimitry Andric assert(PendingTypeTests.empty()); 5053d88c1a5aSDimitry Andric PendingTypeTests.insert(PendingTypeTests.end(), Record.begin(), 5054d88c1a5aSDimitry Andric Record.end()); 5055d88c1a5aSDimitry Andric break; 50567d523365SDimitry Andric } 50577d523365SDimitry Andric } 50587d523365SDimitry Andric } 50597d523365SDimitry Andric llvm_unreachable("Exit infinite loop"); 50607d523365SDimitry Andric } 50617d523365SDimitry Andric 50627d523365SDimitry Andric // Parse the module string table block into the Index. 50637d523365SDimitry Andric // This populates the ModulePathStringTable map in the index. 5064d88c1a5aSDimitry Andric Error ModuleSummaryIndexBitcodeReader::parseModuleStringTable() { 50657d523365SDimitry Andric if (Stream.EnterSubBlock(bitc::MODULE_STRTAB_BLOCK_ID)) 50667d523365SDimitry Andric return error("Invalid record"); 50677d523365SDimitry Andric 50687d523365SDimitry Andric SmallVector<uint64_t, 64> Record; 50697d523365SDimitry Andric 50707d523365SDimitry Andric SmallString<128> ModulePath; 50713ca95b02SDimitry Andric ModulePathStringTableTy::iterator LastSeenModulePath; 5072d88c1a5aSDimitry Andric 5073d88c1a5aSDimitry Andric while (true) { 50747d523365SDimitry Andric BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 50757d523365SDimitry Andric 50767d523365SDimitry Andric switch (Entry.Kind) { 50777d523365SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 50787d523365SDimitry Andric case BitstreamEntry::Error: 50797d523365SDimitry Andric return error("Malformed block"); 50807d523365SDimitry Andric case BitstreamEntry::EndBlock: 5081d88c1a5aSDimitry Andric return Error::success(); 50827d523365SDimitry Andric case BitstreamEntry::Record: 50837d523365SDimitry Andric // The interesting case. 50847d523365SDimitry Andric break; 50857d523365SDimitry Andric } 50867d523365SDimitry Andric 50877d523365SDimitry Andric Record.clear(); 50887d523365SDimitry Andric switch (Stream.readRecord(Entry.ID, Record)) { 50897d523365SDimitry Andric default: // Default behavior: ignore. 50907d523365SDimitry Andric break; 50917d523365SDimitry Andric case bitc::MST_CODE_ENTRY: { 50927d523365SDimitry Andric // MST_ENTRY: [modid, namechar x N] 50933ca95b02SDimitry Andric uint64_t ModuleId = Record[0]; 50943ca95b02SDimitry Andric 50957d523365SDimitry Andric if (convertToString(Record, 1, ModulePath)) 50967d523365SDimitry Andric return error("Invalid record"); 50973ca95b02SDimitry Andric 5098d88c1a5aSDimitry Andric LastSeenModulePath = TheIndex.addModulePath(ModulePath, ModuleId); 50993ca95b02SDimitry Andric ModuleIdMap[ModuleId] = LastSeenModulePath->first(); 51003ca95b02SDimitry Andric 51017d523365SDimitry Andric ModulePath.clear(); 51027d523365SDimitry Andric break; 51037d523365SDimitry Andric } 51043ca95b02SDimitry Andric /// MST_CODE_HASH: [5*i32] 51053ca95b02SDimitry Andric case bitc::MST_CODE_HASH: { 51063ca95b02SDimitry Andric if (Record.size() != 5) 51073ca95b02SDimitry Andric return error("Invalid hash length " + Twine(Record.size()).str()); 5108d88c1a5aSDimitry Andric if (LastSeenModulePath == TheIndex.modulePaths().end()) 51093ca95b02SDimitry Andric return error("Invalid hash that does not follow a module path"); 51103ca95b02SDimitry Andric int Pos = 0; 51113ca95b02SDimitry Andric for (auto &Val : Record) { 51123ca95b02SDimitry Andric assert(!(Val >> 32) && "Unexpected high bits set"); 51133ca95b02SDimitry Andric LastSeenModulePath->second.second[Pos++] = Val; 51143ca95b02SDimitry Andric } 51153ca95b02SDimitry Andric // Reset LastSeenModulePath to avoid overriding the hash unexpectedly. 5116d88c1a5aSDimitry Andric LastSeenModulePath = TheIndex.modulePaths().end(); 51173ca95b02SDimitry Andric break; 51183ca95b02SDimitry Andric } 51197d523365SDimitry Andric } 51207d523365SDimitry Andric } 51217d523365SDimitry Andric llvm_unreachable("Exit infinite loop"); 51227d523365SDimitry Andric } 51237d523365SDimitry Andric 5124f785676fSDimitry Andric namespace { 5125d88c1a5aSDimitry Andric 51263ca95b02SDimitry Andric // FIXME: This class is only here to support the transition to llvm::Error. It 51273ca95b02SDimitry Andric // will be removed once this transition is complete. Clients should prefer to 51283ca95b02SDimitry Andric // deal with the Error value directly, rather than converting to error_code. 512991bc56edSDimitry Andric class BitcodeErrorCategoryType : public std::error_category { 5130d88c1a5aSDimitry Andric const char *name() const noexcept override { 5131f785676fSDimitry Andric return "llvm.bitcode"; 5132f785676fSDimitry Andric } 513391bc56edSDimitry Andric std::string message(int IE) const override { 513439d628a0SDimitry Andric BitcodeError E = static_cast<BitcodeError>(IE); 5135f785676fSDimitry Andric switch (E) { 513639d628a0SDimitry Andric case BitcodeError::CorruptedBitcode: 513739d628a0SDimitry Andric return "Corrupted bitcode"; 5138f785676fSDimitry Andric } 5139f785676fSDimitry Andric llvm_unreachable("Unknown error type!"); 5140f785676fSDimitry Andric } 5141f785676fSDimitry Andric }; 5142d88c1a5aSDimitry Andric 51433ca95b02SDimitry Andric } // end anonymous namespace 5144f785676fSDimitry Andric 514539d628a0SDimitry Andric static ManagedStatic<BitcodeErrorCategoryType> ErrorCategory; 514639d628a0SDimitry Andric 514739d628a0SDimitry Andric const std::error_category &llvm::BitcodeErrorCategory() { 514839d628a0SDimitry Andric return *ErrorCategory; 5149dff0c46cSDimitry Andric } 5150f22ef01cSRoman Divacky 5151f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 5152f22ef01cSRoman Divacky // External interface 5153f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 5154f22ef01cSRoman Divacky 5155d88c1a5aSDimitry Andric Expected<std::vector<BitcodeModule>> 5156d88c1a5aSDimitry Andric llvm::getBitcodeModuleList(MemoryBufferRef Buffer) { 5157d88c1a5aSDimitry Andric Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); 5158d88c1a5aSDimitry Andric if (!StreamOrErr) 5159d88c1a5aSDimitry Andric return StreamOrErr.takeError(); 5160d88c1a5aSDimitry Andric BitstreamCursor &Stream = *StreamOrErr; 51618f0fd8f6SDimitry Andric 5162d88c1a5aSDimitry Andric std::vector<BitcodeModule> Modules; 5163d88c1a5aSDimitry Andric while (true) { 5164d88c1a5aSDimitry Andric uint64_t BCBegin = Stream.getCurrentByteNo(); 51658f0fd8f6SDimitry Andric 5166d88c1a5aSDimitry Andric // We may be consuming bitcode from a client that leaves garbage at the end 5167d88c1a5aSDimitry Andric // of the bitcode stream (e.g. Apple's ar tool). If we are close enough to 5168d88c1a5aSDimitry Andric // the end that there cannot possibly be another module, stop looking. 5169d88c1a5aSDimitry Andric if (BCBegin + 8 >= Stream.getBitcodeBytes().size()) 5170d88c1a5aSDimitry Andric return Modules; 51718f0fd8f6SDimitry Andric 5172d88c1a5aSDimitry Andric BitstreamEntry Entry = Stream.advance(); 5173d88c1a5aSDimitry Andric switch (Entry.Kind) { 5174d88c1a5aSDimitry Andric case BitstreamEntry::EndBlock: 5175d88c1a5aSDimitry Andric case BitstreamEntry::Error: 5176d88c1a5aSDimitry Andric return error("Malformed block"); 5177d88c1a5aSDimitry Andric 5178d88c1a5aSDimitry Andric case BitstreamEntry::SubBlock: { 5179d88c1a5aSDimitry Andric uint64_t IdentificationBit = -1ull; 5180d88c1a5aSDimitry Andric if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) { 5181d88c1a5aSDimitry Andric IdentificationBit = Stream.GetCurrentBitNo() - BCBegin * 8; 5182d88c1a5aSDimitry Andric if (Stream.SkipBlock()) 5183d88c1a5aSDimitry Andric return error("Malformed block"); 5184d88c1a5aSDimitry Andric 5185d88c1a5aSDimitry Andric Entry = Stream.advance(); 5186d88c1a5aSDimitry Andric if (Entry.Kind != BitstreamEntry::SubBlock || 5187d88c1a5aSDimitry Andric Entry.ID != bitc::MODULE_BLOCK_ID) 5188d88c1a5aSDimitry Andric return error("Malformed block"); 51898f0fd8f6SDimitry Andric } 5190d88c1a5aSDimitry Andric 5191d88c1a5aSDimitry Andric if (Entry.ID == bitc::MODULE_BLOCK_ID) { 5192d88c1a5aSDimitry Andric uint64_t ModuleBit = Stream.GetCurrentBitNo() - BCBegin * 8; 5193d88c1a5aSDimitry Andric if (Stream.SkipBlock()) 5194d88c1a5aSDimitry Andric return error("Malformed block"); 5195d88c1a5aSDimitry Andric 5196d88c1a5aSDimitry Andric Modules.push_back({Stream.getBitcodeBytes().slice( 5197d88c1a5aSDimitry Andric BCBegin, Stream.getCurrentByteNo() - BCBegin), 5198d88c1a5aSDimitry Andric Buffer.getBufferIdentifier(), IdentificationBit, 5199d88c1a5aSDimitry Andric ModuleBit}); 5200d88c1a5aSDimitry Andric continue; 5201d88c1a5aSDimitry Andric } 5202d88c1a5aSDimitry Andric 5203d88c1a5aSDimitry Andric if (Stream.SkipBlock()) 5204d88c1a5aSDimitry Andric return error("Malformed block"); 5205d88c1a5aSDimitry Andric continue; 5206d88c1a5aSDimitry Andric } 5207d88c1a5aSDimitry Andric case BitstreamEntry::Record: 5208d88c1a5aSDimitry Andric Stream.skipRecord(Entry.ID); 5209d88c1a5aSDimitry Andric continue; 5210d88c1a5aSDimitry Andric } 5211d88c1a5aSDimitry Andric } 52128f0fd8f6SDimitry Andric } 52138f0fd8f6SDimitry Andric 521439d628a0SDimitry Andric /// \brief Get a lazy one-at-time loading module from bitcode. 5215f22ef01cSRoman Divacky /// 521639d628a0SDimitry Andric /// This isn't always used in a lazy context. In particular, it's also used by 5217d88c1a5aSDimitry Andric /// \a parseModule(). If this is truly lazy, then we need to eagerly pull 521839d628a0SDimitry Andric /// in forward-referenced functions from block address references. 521939d628a0SDimitry Andric /// 52208f0fd8f6SDimitry Andric /// \param[in] MaterializeAll Set to \c true if we should materialize 52218f0fd8f6SDimitry Andric /// everything. 5222d88c1a5aSDimitry Andric Expected<std::unique_ptr<Module>> 5223d88c1a5aSDimitry Andric BitcodeModule::getModuleImpl(LLVMContext &Context, bool MaterializeAll, 5224d88c1a5aSDimitry Andric bool ShouldLazyLoadMetadata, bool IsImporting) { 5225d88c1a5aSDimitry Andric BitstreamCursor Stream(Buffer); 522639d628a0SDimitry Andric 5227d88c1a5aSDimitry Andric std::string ProducerIdentification; 5228d88c1a5aSDimitry Andric if (IdentificationBit != -1ull) { 5229d88c1a5aSDimitry Andric Stream.JumpToBit(IdentificationBit); 5230d88c1a5aSDimitry Andric Expected<std::string> ProducerIdentificationOrErr = 5231d88c1a5aSDimitry Andric readIdentificationBlock(Stream); 5232d88c1a5aSDimitry Andric if (!ProducerIdentificationOrErr) 5233d88c1a5aSDimitry Andric return ProducerIdentificationOrErr.takeError(); 523439d628a0SDimitry Andric 5235d88c1a5aSDimitry Andric ProducerIdentification = *ProducerIdentificationOrErr; 5236dff0c46cSDimitry Andric } 5237dff0c46cSDimitry Andric 5238d88c1a5aSDimitry Andric Stream.JumpToBit(ModuleBit); 5239d88c1a5aSDimitry Andric auto *R = 5240d88c1a5aSDimitry Andric new BitcodeReader(std::move(Stream), ProducerIdentification, Context); 5241d88c1a5aSDimitry Andric 5242d88c1a5aSDimitry Andric std::unique_ptr<Module> M = 5243d88c1a5aSDimitry Andric llvm::make_unique<Module>(ModuleIdentifier, Context); 5244d88c1a5aSDimitry Andric M->setMaterializer(R); 5245d88c1a5aSDimitry Andric 5246d88c1a5aSDimitry Andric // Delay parsing Metadata if ShouldLazyLoadMetadata is true. 5247d88c1a5aSDimitry Andric if (Error Err = 5248d88c1a5aSDimitry Andric R->parseBitcodeInto(M.get(), ShouldLazyLoadMetadata, IsImporting)) 5249d88c1a5aSDimitry Andric return std::move(Err); 5250d88c1a5aSDimitry Andric 5251d88c1a5aSDimitry Andric if (MaterializeAll) { 5252d88c1a5aSDimitry Andric // Read in the entire module, and destroy the BitcodeReader. 5253d88c1a5aSDimitry Andric if (Error Err = M->materializeAll()) 5254d88c1a5aSDimitry Andric return std::move(Err); 5255d88c1a5aSDimitry Andric } else { 5256d88c1a5aSDimitry Andric // Resolve forward references from blockaddresses. 5257d88c1a5aSDimitry Andric if (Error Err = R->materializeForwardReferencedFunctions()) 5258d88c1a5aSDimitry Andric return std::move(Err); 5259d88c1a5aSDimitry Andric } 5260d88c1a5aSDimitry Andric return std::move(M); 5261f22ef01cSRoman Divacky } 5262f22ef01cSRoman Divacky 5263d88c1a5aSDimitry Andric Expected<std::unique_ptr<Module>> 5264d88c1a5aSDimitry Andric BitcodeModule::getLazyModule(LLVMContext &Context, bool ShouldLazyLoadMetadata, 5265d88c1a5aSDimitry Andric bool IsImporting) { 5266d88c1a5aSDimitry Andric return getModuleImpl(Context, false, ShouldLazyLoadMetadata, IsImporting); 52677d523365SDimitry Andric } 52687d523365SDimitry Andric 52697d523365SDimitry Andric // Parse the specified bitcode buffer, returning the function info index. 5270d88c1a5aSDimitry Andric Expected<std::unique_ptr<ModuleSummaryIndex>> BitcodeModule::getSummary() { 5271d88c1a5aSDimitry Andric BitstreamCursor Stream(Buffer); 5272d88c1a5aSDimitry Andric Stream.JumpToBit(ModuleBit); 52737d523365SDimitry Andric 52743ca95b02SDimitry Andric auto Index = llvm::make_unique<ModuleSummaryIndex>(); 5275d88c1a5aSDimitry Andric ModuleSummaryIndexBitcodeReader R(std::move(Stream), *Index); 52767d523365SDimitry Andric 5277d88c1a5aSDimitry Andric if (Error Err = R.parseModule(ModuleIdentifier)) 5278d88c1a5aSDimitry Andric return std::move(Err); 52797d523365SDimitry Andric 52807d523365SDimitry Andric return std::move(Index); 52817d523365SDimitry Andric } 52827d523365SDimitry Andric 52833ca95b02SDimitry Andric // Check if the given bitcode buffer contains a global value summary block. 5284d88c1a5aSDimitry Andric Expected<bool> BitcodeModule::hasSummary() { 5285d88c1a5aSDimitry Andric BitstreamCursor Stream(Buffer); 5286d88c1a5aSDimitry Andric Stream.JumpToBit(ModuleBit); 52877d523365SDimitry Andric 5288d88c1a5aSDimitry Andric if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 5289d88c1a5aSDimitry Andric return error("Invalid record"); 5290d88c1a5aSDimitry Andric 5291d88c1a5aSDimitry Andric while (true) { 5292d88c1a5aSDimitry Andric BitstreamEntry Entry = Stream.advance(); 5293d88c1a5aSDimitry Andric 5294d88c1a5aSDimitry Andric switch (Entry.Kind) { 5295d88c1a5aSDimitry Andric case BitstreamEntry::Error: 5296d88c1a5aSDimitry Andric return error("Malformed block"); 5297d88c1a5aSDimitry Andric case BitstreamEntry::EndBlock: 52987d523365SDimitry Andric return false; 52997d523365SDimitry Andric 5300d88c1a5aSDimitry Andric case BitstreamEntry::SubBlock: 5301d88c1a5aSDimitry Andric if (Entry.ID == bitc::GLOBALVAL_SUMMARY_BLOCK_ID) 5302d88c1a5aSDimitry Andric return true; 53037d523365SDimitry Andric 5304d88c1a5aSDimitry Andric // Ignore other sub-blocks. 5305d88c1a5aSDimitry Andric if (Stream.SkipBlock()) 5306d88c1a5aSDimitry Andric return error("Malformed block"); 5307d88c1a5aSDimitry Andric continue; 5308d88c1a5aSDimitry Andric 5309d88c1a5aSDimitry Andric case BitstreamEntry::Record: 5310d88c1a5aSDimitry Andric Stream.skipRecord(Entry.ID); 5311d88c1a5aSDimitry Andric continue; 5312d88c1a5aSDimitry Andric } 5313d88c1a5aSDimitry Andric } 5314d88c1a5aSDimitry Andric } 5315d88c1a5aSDimitry Andric 5316d88c1a5aSDimitry Andric static Expected<BitcodeModule> getSingleModule(MemoryBufferRef Buffer) { 5317d88c1a5aSDimitry Andric Expected<std::vector<BitcodeModule>> MsOrErr = getBitcodeModuleList(Buffer); 5318d88c1a5aSDimitry Andric if (!MsOrErr) 5319d88c1a5aSDimitry Andric return MsOrErr.takeError(); 5320d88c1a5aSDimitry Andric 5321d88c1a5aSDimitry Andric if (MsOrErr->size() != 1) 5322d88c1a5aSDimitry Andric return error("Expected a single module"); 5323d88c1a5aSDimitry Andric 5324d88c1a5aSDimitry Andric return (*MsOrErr)[0]; 5325d88c1a5aSDimitry Andric } 5326d88c1a5aSDimitry Andric 5327d88c1a5aSDimitry Andric Expected<std::unique_ptr<Module>> 5328d88c1a5aSDimitry Andric llvm::getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context, 5329d88c1a5aSDimitry Andric bool ShouldLazyLoadMetadata, bool IsImporting) { 5330d88c1a5aSDimitry Andric Expected<BitcodeModule> BM = getSingleModule(Buffer); 5331d88c1a5aSDimitry Andric if (!BM) 5332d88c1a5aSDimitry Andric return BM.takeError(); 5333d88c1a5aSDimitry Andric 5334d88c1a5aSDimitry Andric return BM->getLazyModule(Context, ShouldLazyLoadMetadata, IsImporting); 5335d88c1a5aSDimitry Andric } 5336d88c1a5aSDimitry Andric 5337d88c1a5aSDimitry Andric Expected<std::unique_ptr<Module>> llvm::getOwningLazyBitcodeModule( 5338d88c1a5aSDimitry Andric std::unique_ptr<MemoryBuffer> &&Buffer, LLVMContext &Context, 5339d88c1a5aSDimitry Andric bool ShouldLazyLoadMetadata, bool IsImporting) { 5340d88c1a5aSDimitry Andric auto MOrErr = getLazyBitcodeModule(*Buffer, Context, ShouldLazyLoadMetadata, 5341d88c1a5aSDimitry Andric IsImporting); 5342d88c1a5aSDimitry Andric if (MOrErr) 5343d88c1a5aSDimitry Andric (*MOrErr)->setOwnedMemoryBuffer(std::move(Buffer)); 5344d88c1a5aSDimitry Andric return MOrErr; 5345d88c1a5aSDimitry Andric } 5346d88c1a5aSDimitry Andric 5347d88c1a5aSDimitry Andric Expected<std::unique_ptr<Module>> 5348d88c1a5aSDimitry Andric BitcodeModule::parseModule(LLVMContext &Context) { 5349d88c1a5aSDimitry Andric return getModuleImpl(Context, true, false, false); 5350d88c1a5aSDimitry Andric // TODO: Restore the use-lists to the in-memory state when the bitcode was 5351d88c1a5aSDimitry Andric // written. We must defer until the Module has been fully materialized. 5352d88c1a5aSDimitry Andric } 5353d88c1a5aSDimitry Andric 5354d88c1a5aSDimitry Andric Expected<std::unique_ptr<Module>> llvm::parseBitcodeFile(MemoryBufferRef Buffer, 5355d88c1a5aSDimitry Andric LLVMContext &Context) { 5356d88c1a5aSDimitry Andric Expected<BitcodeModule> BM = getSingleModule(Buffer); 5357d88c1a5aSDimitry Andric if (!BM) 5358d88c1a5aSDimitry Andric return BM.takeError(); 5359d88c1a5aSDimitry Andric 5360d88c1a5aSDimitry Andric return BM->parseModule(Context); 5361d88c1a5aSDimitry Andric } 5362d88c1a5aSDimitry Andric 5363d88c1a5aSDimitry Andric Expected<std::string> llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer) { 5364d88c1a5aSDimitry Andric Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); 5365d88c1a5aSDimitry Andric if (!StreamOrErr) 5366d88c1a5aSDimitry Andric return StreamOrErr.takeError(); 5367d88c1a5aSDimitry Andric 5368d88c1a5aSDimitry Andric return readTriple(*StreamOrErr); 5369d88c1a5aSDimitry Andric } 5370d88c1a5aSDimitry Andric 5371d88c1a5aSDimitry Andric Expected<bool> llvm::isBitcodeContainingObjCCategory(MemoryBufferRef Buffer) { 5372d88c1a5aSDimitry Andric Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); 5373d88c1a5aSDimitry Andric if (!StreamOrErr) 5374d88c1a5aSDimitry Andric return StreamOrErr.takeError(); 5375d88c1a5aSDimitry Andric 5376d88c1a5aSDimitry Andric return hasObjCCategory(*StreamOrErr); 5377d88c1a5aSDimitry Andric } 5378d88c1a5aSDimitry Andric 5379d88c1a5aSDimitry Andric Expected<std::string> llvm::getBitcodeProducerString(MemoryBufferRef Buffer) { 5380d88c1a5aSDimitry Andric Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); 5381d88c1a5aSDimitry Andric if (!StreamOrErr) 5382d88c1a5aSDimitry Andric return StreamOrErr.takeError(); 5383d88c1a5aSDimitry Andric 5384d88c1a5aSDimitry Andric return readIdentificationCode(*StreamOrErr); 5385d88c1a5aSDimitry Andric } 5386d88c1a5aSDimitry Andric 5387d88c1a5aSDimitry Andric Expected<std::unique_ptr<ModuleSummaryIndex>> 5388d88c1a5aSDimitry Andric llvm::getModuleSummaryIndex(MemoryBufferRef Buffer) { 5389d88c1a5aSDimitry Andric Expected<BitcodeModule> BM = getSingleModule(Buffer); 5390d88c1a5aSDimitry Andric if (!BM) 5391d88c1a5aSDimitry Andric return BM.takeError(); 5392d88c1a5aSDimitry Andric 5393d88c1a5aSDimitry Andric return BM->getSummary(); 5394d88c1a5aSDimitry Andric } 5395d88c1a5aSDimitry Andric 5396d88c1a5aSDimitry Andric Expected<bool> llvm::hasGlobalValueSummary(MemoryBufferRef Buffer) { 5397d88c1a5aSDimitry Andric Expected<BitcodeModule> BM = getSingleModule(Buffer); 5398d88c1a5aSDimitry Andric if (!BM) 5399d88c1a5aSDimitry Andric return BM.takeError(); 5400d88c1a5aSDimitry Andric 5401d88c1a5aSDimitry Andric return BM->hasSummary(); 54027d523365SDimitry Andric } 5403