1*0b57cec5SDimitry Andric //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric 9*0b57cec5SDimitry Andric #include "llvm/Bitcode/BitcodeReader.h" 10*0b57cec5SDimitry Andric #include "MetadataLoader.h" 11*0b57cec5SDimitry Andric #include "ValueList.h" 12*0b57cec5SDimitry Andric #include "llvm/ADT/APFloat.h" 13*0b57cec5SDimitry Andric #include "llvm/ADT/APInt.h" 14*0b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 15*0b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 16*0b57cec5SDimitry Andric #include "llvm/ADT/Optional.h" 17*0b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 18*0b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h" 19*0b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 20*0b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 21*0b57cec5SDimitry Andric #include "llvm/ADT/Triple.h" 22*0b57cec5SDimitry Andric #include "llvm/ADT/Twine.h" 23e8d8bef9SDimitry Andric #include "llvm/Bitcode/BitcodeCommon.h" 24*0b57cec5SDimitry Andric #include "llvm/Bitcode/LLVMBitCodes.h" 25e8d8bef9SDimitry Andric #include "llvm/Bitstream/BitstreamReader.h" 26*0b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h" 27*0b57cec5SDimitry Andric #include "llvm/IR/Argument.h" 28*0b57cec5SDimitry Andric #include "llvm/IR/Attributes.h" 29*0b57cec5SDimitry Andric #include "llvm/IR/AutoUpgrade.h" 30*0b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h" 31*0b57cec5SDimitry Andric #include "llvm/IR/CallingConv.h" 32*0b57cec5SDimitry Andric #include "llvm/IR/Comdat.h" 33*0b57cec5SDimitry Andric #include "llvm/IR/Constant.h" 34*0b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 35*0b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 36*0b57cec5SDimitry Andric #include "llvm/IR/DebugInfo.h" 37*0b57cec5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h" 38*0b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h" 39*0b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h" 40*0b57cec5SDimitry Andric #include "llvm/IR/Function.h" 41*0b57cec5SDimitry Andric #include "llvm/IR/GVMaterializer.h" 4281ad6265SDimitry Andric #include "llvm/IR/GetElementPtrTypeIterator.h" 43*0b57cec5SDimitry Andric #include "llvm/IR/GlobalAlias.h" 44*0b57cec5SDimitry Andric #include "llvm/IR/GlobalIFunc.h" 45*0b57cec5SDimitry Andric #include "llvm/IR/GlobalObject.h" 46*0b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h" 47*0b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h" 48*0b57cec5SDimitry Andric #include "llvm/IR/InlineAsm.h" 49*0b57cec5SDimitry Andric #include "llvm/IR/InstIterator.h" 50*0b57cec5SDimitry Andric #include "llvm/IR/InstrTypes.h" 51*0b57cec5SDimitry Andric #include "llvm/IR/Instruction.h" 52*0b57cec5SDimitry Andric #include "llvm/IR/Instructions.h" 53*0b57cec5SDimitry Andric #include "llvm/IR/Intrinsics.h" 5481ad6265SDimitry Andric #include "llvm/IR/IntrinsicsAArch64.h" 5581ad6265SDimitry Andric #include "llvm/IR/IntrinsicsARM.h" 56*0b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h" 57*0b57cec5SDimitry Andric #include "llvm/IR/Metadata.h" 58*0b57cec5SDimitry Andric #include "llvm/IR/Module.h" 59*0b57cec5SDimitry Andric #include "llvm/IR/ModuleSummaryIndex.h" 60*0b57cec5SDimitry Andric #include "llvm/IR/Operator.h" 61*0b57cec5SDimitry Andric #include "llvm/IR/Type.h" 62*0b57cec5SDimitry Andric #include "llvm/IR/Value.h" 63*0b57cec5SDimitry Andric #include "llvm/IR/Verifier.h" 64*0b57cec5SDimitry Andric #include "llvm/Support/AtomicOrdering.h" 65*0b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 66*0b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 67*0b57cec5SDimitry Andric #include "llvm/Support/Compiler.h" 68*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 69*0b57cec5SDimitry Andric #include "llvm/Support/Error.h" 70*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 71*0b57cec5SDimitry Andric #include "llvm/Support/ErrorOr.h" 72*0b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h" 73*0b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h" 74*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 75*0b57cec5SDimitry Andric #include <algorithm> 76*0b57cec5SDimitry Andric #include <cassert> 77*0b57cec5SDimitry Andric #include <cstddef> 78*0b57cec5SDimitry Andric #include <cstdint> 79*0b57cec5SDimitry Andric #include <deque> 80*0b57cec5SDimitry Andric #include <map> 81*0b57cec5SDimitry Andric #include <memory> 82*0b57cec5SDimitry Andric #include <set> 83*0b57cec5SDimitry Andric #include <string> 84*0b57cec5SDimitry Andric #include <system_error> 85*0b57cec5SDimitry Andric #include <tuple> 86*0b57cec5SDimitry Andric #include <utility> 87*0b57cec5SDimitry Andric #include <vector> 88*0b57cec5SDimitry Andric 89*0b57cec5SDimitry Andric using namespace llvm; 90*0b57cec5SDimitry Andric 91*0b57cec5SDimitry Andric static cl::opt<bool> PrintSummaryGUIDs( 92*0b57cec5SDimitry Andric "print-summary-global-ids", cl::init(false), cl::Hidden, 93*0b57cec5SDimitry Andric cl::desc( 94*0b57cec5SDimitry Andric "Print the global id for each value when reading the module summary")); 95*0b57cec5SDimitry Andric 9681ad6265SDimitry Andric static cl::opt<bool> ExpandConstantExprs( 9781ad6265SDimitry Andric "expand-constant-exprs", cl::Hidden, 9881ad6265SDimitry Andric cl::desc( 9981ad6265SDimitry Andric "Expand constant expressions to instructions for testing purposes")); 10081ad6265SDimitry Andric 101*0b57cec5SDimitry Andric namespace { 102*0b57cec5SDimitry Andric 103*0b57cec5SDimitry Andric enum { 104*0b57cec5SDimitry Andric SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex 105*0b57cec5SDimitry Andric }; 106*0b57cec5SDimitry Andric 107*0b57cec5SDimitry Andric } // end anonymous namespace 108*0b57cec5SDimitry Andric 109*0b57cec5SDimitry Andric static Error error(const Twine &Message) { 110*0b57cec5SDimitry Andric return make_error<StringError>( 111*0b57cec5SDimitry Andric Message, make_error_code(BitcodeError::CorruptedBitcode)); 112*0b57cec5SDimitry Andric } 113*0b57cec5SDimitry Andric 114*0b57cec5SDimitry Andric static Error hasInvalidBitcodeHeader(BitstreamCursor &Stream) { 115*0b57cec5SDimitry Andric if (!Stream.canSkipToPos(4)) 116*0b57cec5SDimitry Andric return createStringError(std::errc::illegal_byte_sequence, 117*0b57cec5SDimitry Andric "file too small to contain bitcode header"); 118*0b57cec5SDimitry Andric for (unsigned C : {'B', 'C'}) 119*0b57cec5SDimitry Andric if (Expected<SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) { 120*0b57cec5SDimitry Andric if (Res.get() != C) 121*0b57cec5SDimitry Andric return createStringError(std::errc::illegal_byte_sequence, 122*0b57cec5SDimitry Andric "file doesn't start with bitcode header"); 123*0b57cec5SDimitry Andric } else 124*0b57cec5SDimitry Andric return Res.takeError(); 125*0b57cec5SDimitry Andric for (unsigned C : {0x0, 0xC, 0xE, 0xD}) 126*0b57cec5SDimitry Andric if (Expected<SimpleBitstreamCursor::word_t> Res = Stream.Read(4)) { 127*0b57cec5SDimitry Andric if (Res.get() != C) 128*0b57cec5SDimitry Andric return createStringError(std::errc::illegal_byte_sequence, 129*0b57cec5SDimitry Andric "file doesn't start with bitcode header"); 130*0b57cec5SDimitry Andric } else 131*0b57cec5SDimitry Andric return Res.takeError(); 132*0b57cec5SDimitry Andric return Error::success(); 133*0b57cec5SDimitry Andric } 134*0b57cec5SDimitry Andric 135*0b57cec5SDimitry Andric static Expected<BitstreamCursor> initStream(MemoryBufferRef Buffer) { 136*0b57cec5SDimitry Andric const unsigned char *BufPtr = (const unsigned char *)Buffer.getBufferStart(); 137*0b57cec5SDimitry Andric const unsigned char *BufEnd = BufPtr + Buffer.getBufferSize(); 138*0b57cec5SDimitry Andric 139*0b57cec5SDimitry Andric if (Buffer.getBufferSize() & 3) 140*0b57cec5SDimitry Andric return error("Invalid bitcode signature"); 141*0b57cec5SDimitry Andric 142*0b57cec5SDimitry Andric // If we have a wrapper header, parse it and ignore the non-bc file contents. 143*0b57cec5SDimitry Andric // The magic number is 0x0B17C0DE stored in little endian. 144*0b57cec5SDimitry Andric if (isBitcodeWrapper(BufPtr, BufEnd)) 145*0b57cec5SDimitry Andric if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true)) 146*0b57cec5SDimitry Andric return error("Invalid bitcode wrapper header"); 147*0b57cec5SDimitry Andric 148*0b57cec5SDimitry Andric BitstreamCursor Stream(ArrayRef<uint8_t>(BufPtr, BufEnd)); 149*0b57cec5SDimitry Andric if (Error Err = hasInvalidBitcodeHeader(Stream)) 150*0b57cec5SDimitry Andric return std::move(Err); 151*0b57cec5SDimitry Andric 152*0b57cec5SDimitry Andric return std::move(Stream); 153*0b57cec5SDimitry Andric } 154*0b57cec5SDimitry Andric 155*0b57cec5SDimitry Andric /// Convert a string from a record into an std::string, return true on failure. 156*0b57cec5SDimitry Andric template <typename StrTy> 157*0b57cec5SDimitry Andric static bool convertToString(ArrayRef<uint64_t> Record, unsigned Idx, 158*0b57cec5SDimitry Andric StrTy &Result) { 159*0b57cec5SDimitry Andric if (Idx > Record.size()) 160*0b57cec5SDimitry Andric return true; 161*0b57cec5SDimitry Andric 1625ffd83dbSDimitry Andric Result.append(Record.begin() + Idx, Record.end()); 163*0b57cec5SDimitry Andric return false; 164*0b57cec5SDimitry Andric } 165*0b57cec5SDimitry Andric 166*0b57cec5SDimitry Andric // Strip all the TBAA attachment for the module. 167*0b57cec5SDimitry Andric static void stripTBAA(Module *M) { 168*0b57cec5SDimitry Andric for (auto &F : *M) { 169*0b57cec5SDimitry Andric if (F.isMaterializable()) 170*0b57cec5SDimitry Andric continue; 171*0b57cec5SDimitry Andric for (auto &I : instructions(F)) 172*0b57cec5SDimitry Andric I.setMetadata(LLVMContext::MD_tbaa, nullptr); 173*0b57cec5SDimitry Andric } 174*0b57cec5SDimitry Andric } 175*0b57cec5SDimitry Andric 176*0b57cec5SDimitry Andric /// Read the "IDENTIFICATION_BLOCK_ID" block, do some basic enforcement on the 177*0b57cec5SDimitry Andric /// "epoch" encoded in the bitcode, and return the producer name if any. 178*0b57cec5SDimitry Andric static Expected<std::string> readIdentificationBlock(BitstreamCursor &Stream) { 179*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::IDENTIFICATION_BLOCK_ID)) 180*0b57cec5SDimitry Andric return std::move(Err); 181*0b57cec5SDimitry Andric 182*0b57cec5SDimitry Andric // Read all the records. 183*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 184*0b57cec5SDimitry Andric 185*0b57cec5SDimitry Andric std::string ProducerIdentification; 186*0b57cec5SDimitry Andric 187*0b57cec5SDimitry Andric while (true) { 188*0b57cec5SDimitry Andric BitstreamEntry Entry; 189349cc55cSDimitry Andric if (Error E = Stream.advance().moveInto(Entry)) 190349cc55cSDimitry Andric return std::move(E); 191*0b57cec5SDimitry Andric 192*0b57cec5SDimitry Andric switch (Entry.Kind) { 193*0b57cec5SDimitry Andric default: 194*0b57cec5SDimitry Andric case BitstreamEntry::Error: 195*0b57cec5SDimitry Andric return error("Malformed block"); 196*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 197*0b57cec5SDimitry Andric return ProducerIdentification; 198*0b57cec5SDimitry Andric case BitstreamEntry::Record: 199*0b57cec5SDimitry Andric // The interesting case. 200*0b57cec5SDimitry Andric break; 201*0b57cec5SDimitry Andric } 202*0b57cec5SDimitry Andric 203*0b57cec5SDimitry Andric // Read a record. 204*0b57cec5SDimitry Andric Record.clear(); 205*0b57cec5SDimitry Andric Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); 206*0b57cec5SDimitry Andric if (!MaybeBitCode) 207*0b57cec5SDimitry Andric return MaybeBitCode.takeError(); 208*0b57cec5SDimitry Andric switch (MaybeBitCode.get()) { 209*0b57cec5SDimitry Andric default: // Default behavior: reject 210*0b57cec5SDimitry Andric return error("Invalid value"); 211*0b57cec5SDimitry Andric case bitc::IDENTIFICATION_CODE_STRING: // IDENTIFICATION: [strchr x N] 212*0b57cec5SDimitry Andric convertToString(Record, 0, ProducerIdentification); 213*0b57cec5SDimitry Andric break; 214*0b57cec5SDimitry Andric case bitc::IDENTIFICATION_CODE_EPOCH: { // EPOCH: [epoch#] 215*0b57cec5SDimitry Andric unsigned epoch = (unsigned)Record[0]; 216*0b57cec5SDimitry Andric if (epoch != bitc::BITCODE_CURRENT_EPOCH) { 217*0b57cec5SDimitry Andric return error( 218*0b57cec5SDimitry Andric Twine("Incompatible epoch: Bitcode '") + Twine(epoch) + 219*0b57cec5SDimitry Andric "' vs current: '" + Twine(bitc::BITCODE_CURRENT_EPOCH) + "'"); 220*0b57cec5SDimitry Andric } 221*0b57cec5SDimitry Andric } 222*0b57cec5SDimitry Andric } 223*0b57cec5SDimitry Andric } 224*0b57cec5SDimitry Andric } 225*0b57cec5SDimitry Andric 226*0b57cec5SDimitry Andric static Expected<std::string> readIdentificationCode(BitstreamCursor &Stream) { 227*0b57cec5SDimitry Andric // We expect a number of well-defined blocks, though we don't necessarily 228*0b57cec5SDimitry Andric // need to understand them all. 229*0b57cec5SDimitry Andric while (true) { 230*0b57cec5SDimitry Andric if (Stream.AtEndOfStream()) 231*0b57cec5SDimitry Andric return ""; 232*0b57cec5SDimitry Andric 233*0b57cec5SDimitry Andric BitstreamEntry Entry; 234349cc55cSDimitry Andric if (Error E = Stream.advance().moveInto(Entry)) 235349cc55cSDimitry Andric return std::move(E); 236*0b57cec5SDimitry Andric 237*0b57cec5SDimitry Andric switch (Entry.Kind) { 238*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 239*0b57cec5SDimitry Andric case BitstreamEntry::Error: 240*0b57cec5SDimitry Andric return error("Malformed block"); 241*0b57cec5SDimitry Andric 242*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: 243*0b57cec5SDimitry Andric if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) 244*0b57cec5SDimitry Andric return readIdentificationBlock(Stream); 245*0b57cec5SDimitry Andric 246*0b57cec5SDimitry Andric // Ignore other sub-blocks. 247*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 248*0b57cec5SDimitry Andric return std::move(Err); 249*0b57cec5SDimitry Andric continue; 250*0b57cec5SDimitry Andric case BitstreamEntry::Record: 251349cc55cSDimitry Andric if (Error E = Stream.skipRecord(Entry.ID).takeError()) 252349cc55cSDimitry Andric return std::move(E); 253*0b57cec5SDimitry Andric continue; 254*0b57cec5SDimitry Andric } 255*0b57cec5SDimitry Andric } 256*0b57cec5SDimitry Andric } 257*0b57cec5SDimitry Andric 258*0b57cec5SDimitry Andric static Expected<bool> hasObjCCategoryInModule(BitstreamCursor &Stream) { 259*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 260*0b57cec5SDimitry Andric return std::move(Err); 261*0b57cec5SDimitry Andric 262*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 263*0b57cec5SDimitry Andric // Read all the records for this module. 264*0b57cec5SDimitry Andric 265*0b57cec5SDimitry Andric while (true) { 266*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 267*0b57cec5SDimitry Andric if (!MaybeEntry) 268*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 269*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 270*0b57cec5SDimitry Andric 271*0b57cec5SDimitry Andric switch (Entry.Kind) { 272*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 273*0b57cec5SDimitry Andric case BitstreamEntry::Error: 274*0b57cec5SDimitry Andric return error("Malformed block"); 275*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 276*0b57cec5SDimitry Andric return false; 277*0b57cec5SDimitry Andric case BitstreamEntry::Record: 278*0b57cec5SDimitry Andric // The interesting case. 279*0b57cec5SDimitry Andric break; 280*0b57cec5SDimitry Andric } 281*0b57cec5SDimitry Andric 282*0b57cec5SDimitry Andric // Read a record. 283*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 284*0b57cec5SDimitry Andric if (!MaybeRecord) 285*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 286*0b57cec5SDimitry Andric switch (MaybeRecord.get()) { 287*0b57cec5SDimitry Andric default: 288*0b57cec5SDimitry Andric break; // Default behavior, ignore unknown content. 289*0b57cec5SDimitry Andric case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] 290*0b57cec5SDimitry Andric std::string S; 291*0b57cec5SDimitry Andric if (convertToString(Record, 0, S)) 29281ad6265SDimitry Andric return error("Invalid section name record"); 293*0b57cec5SDimitry Andric // Check for the i386 and other (x86_64, ARM) conventions 294*0b57cec5SDimitry Andric if (S.find("__DATA,__objc_catlist") != std::string::npos || 295*0b57cec5SDimitry Andric S.find("__OBJC,__category") != std::string::npos) 296*0b57cec5SDimitry Andric return true; 297*0b57cec5SDimitry Andric break; 298*0b57cec5SDimitry Andric } 299*0b57cec5SDimitry Andric } 300*0b57cec5SDimitry Andric Record.clear(); 301*0b57cec5SDimitry Andric } 302*0b57cec5SDimitry Andric llvm_unreachable("Exit infinite loop"); 303*0b57cec5SDimitry Andric } 304*0b57cec5SDimitry Andric 305*0b57cec5SDimitry Andric static Expected<bool> hasObjCCategory(BitstreamCursor &Stream) { 306*0b57cec5SDimitry Andric // We expect a number of well-defined blocks, though we don't necessarily 307*0b57cec5SDimitry Andric // need to understand them all. 308*0b57cec5SDimitry Andric while (true) { 309*0b57cec5SDimitry Andric BitstreamEntry Entry; 310349cc55cSDimitry Andric if (Error E = Stream.advance().moveInto(Entry)) 311349cc55cSDimitry Andric return std::move(E); 312*0b57cec5SDimitry Andric 313*0b57cec5SDimitry Andric switch (Entry.Kind) { 314*0b57cec5SDimitry Andric case BitstreamEntry::Error: 315*0b57cec5SDimitry Andric return error("Malformed block"); 316*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 317*0b57cec5SDimitry Andric return false; 318*0b57cec5SDimitry Andric 319*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: 320*0b57cec5SDimitry Andric if (Entry.ID == bitc::MODULE_BLOCK_ID) 321*0b57cec5SDimitry Andric return hasObjCCategoryInModule(Stream); 322*0b57cec5SDimitry Andric 323*0b57cec5SDimitry Andric // Ignore other sub-blocks. 324*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 325*0b57cec5SDimitry Andric return std::move(Err); 326*0b57cec5SDimitry Andric continue; 327*0b57cec5SDimitry Andric 328*0b57cec5SDimitry Andric case BitstreamEntry::Record: 329349cc55cSDimitry Andric if (Error E = Stream.skipRecord(Entry.ID).takeError()) 330349cc55cSDimitry Andric return std::move(E); 331*0b57cec5SDimitry Andric continue; 332*0b57cec5SDimitry Andric } 333*0b57cec5SDimitry Andric } 334*0b57cec5SDimitry Andric } 335*0b57cec5SDimitry Andric 336*0b57cec5SDimitry Andric static Expected<std::string> readModuleTriple(BitstreamCursor &Stream) { 337*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 338*0b57cec5SDimitry Andric return std::move(Err); 339*0b57cec5SDimitry Andric 340*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 341*0b57cec5SDimitry Andric 342*0b57cec5SDimitry Andric std::string Triple; 343*0b57cec5SDimitry Andric 344*0b57cec5SDimitry Andric // Read all the records for this module. 345*0b57cec5SDimitry Andric while (true) { 346*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 347*0b57cec5SDimitry Andric if (!MaybeEntry) 348*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 349*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 350*0b57cec5SDimitry Andric 351*0b57cec5SDimitry Andric switch (Entry.Kind) { 352*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 353*0b57cec5SDimitry Andric case BitstreamEntry::Error: 354*0b57cec5SDimitry Andric return error("Malformed block"); 355*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 356*0b57cec5SDimitry Andric return Triple; 357*0b57cec5SDimitry Andric case BitstreamEntry::Record: 358*0b57cec5SDimitry Andric // The interesting case. 359*0b57cec5SDimitry Andric break; 360*0b57cec5SDimitry Andric } 361*0b57cec5SDimitry Andric 362*0b57cec5SDimitry Andric // Read a record. 363*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 364*0b57cec5SDimitry Andric if (!MaybeRecord) 365*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 366*0b57cec5SDimitry Andric switch (MaybeRecord.get()) { 367*0b57cec5SDimitry Andric default: break; // Default behavior, ignore unknown content. 368*0b57cec5SDimitry Andric case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] 369*0b57cec5SDimitry Andric std::string S; 370*0b57cec5SDimitry Andric if (convertToString(Record, 0, S)) 37181ad6265SDimitry Andric return error("Invalid triple record"); 372*0b57cec5SDimitry Andric Triple = S; 373*0b57cec5SDimitry Andric break; 374*0b57cec5SDimitry Andric } 375*0b57cec5SDimitry Andric } 376*0b57cec5SDimitry Andric Record.clear(); 377*0b57cec5SDimitry Andric } 378*0b57cec5SDimitry Andric llvm_unreachable("Exit infinite loop"); 379*0b57cec5SDimitry Andric } 380*0b57cec5SDimitry Andric 381*0b57cec5SDimitry Andric static Expected<std::string> readTriple(BitstreamCursor &Stream) { 382*0b57cec5SDimitry Andric // We expect a number of well-defined blocks, though we don't necessarily 383*0b57cec5SDimitry Andric // need to understand them all. 384*0b57cec5SDimitry Andric while (true) { 385*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advance(); 386*0b57cec5SDimitry Andric if (!MaybeEntry) 387*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 388*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 389*0b57cec5SDimitry Andric 390*0b57cec5SDimitry Andric switch (Entry.Kind) { 391*0b57cec5SDimitry Andric case BitstreamEntry::Error: 392*0b57cec5SDimitry Andric return error("Malformed block"); 393*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 394*0b57cec5SDimitry Andric return ""; 395*0b57cec5SDimitry Andric 396*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: 397*0b57cec5SDimitry Andric if (Entry.ID == bitc::MODULE_BLOCK_ID) 398*0b57cec5SDimitry Andric return readModuleTriple(Stream); 399*0b57cec5SDimitry Andric 400*0b57cec5SDimitry Andric // Ignore other sub-blocks. 401*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 402*0b57cec5SDimitry Andric return std::move(Err); 403*0b57cec5SDimitry Andric continue; 404*0b57cec5SDimitry Andric 405*0b57cec5SDimitry Andric case BitstreamEntry::Record: 406*0b57cec5SDimitry Andric if (llvm::Expected<unsigned> Skipped = Stream.skipRecord(Entry.ID)) 407*0b57cec5SDimitry Andric continue; 408*0b57cec5SDimitry Andric else 409*0b57cec5SDimitry Andric return Skipped.takeError(); 410*0b57cec5SDimitry Andric } 411*0b57cec5SDimitry Andric } 412*0b57cec5SDimitry Andric } 413*0b57cec5SDimitry Andric 414*0b57cec5SDimitry Andric namespace { 415*0b57cec5SDimitry Andric 416*0b57cec5SDimitry Andric class BitcodeReaderBase { 417*0b57cec5SDimitry Andric protected: 418*0b57cec5SDimitry Andric BitcodeReaderBase(BitstreamCursor Stream, StringRef Strtab) 419*0b57cec5SDimitry Andric : Stream(std::move(Stream)), Strtab(Strtab) { 420*0b57cec5SDimitry Andric this->Stream.setBlockInfo(&BlockInfo); 421*0b57cec5SDimitry Andric } 422*0b57cec5SDimitry Andric 423*0b57cec5SDimitry Andric BitstreamBlockInfo BlockInfo; 424*0b57cec5SDimitry Andric BitstreamCursor Stream; 425*0b57cec5SDimitry Andric StringRef Strtab; 426*0b57cec5SDimitry Andric 427*0b57cec5SDimitry Andric /// In version 2 of the bitcode we store names of global values and comdats in 428*0b57cec5SDimitry Andric /// a string table rather than in the VST. 429*0b57cec5SDimitry Andric bool UseStrtab = false; 430*0b57cec5SDimitry Andric 431*0b57cec5SDimitry Andric Expected<unsigned> parseVersionRecord(ArrayRef<uint64_t> Record); 432*0b57cec5SDimitry Andric 433*0b57cec5SDimitry Andric /// If this module uses a string table, pop the reference to the string table 434*0b57cec5SDimitry Andric /// and return the referenced string and the rest of the record. Otherwise 435*0b57cec5SDimitry Andric /// just return the record itself. 436*0b57cec5SDimitry Andric std::pair<StringRef, ArrayRef<uint64_t>> 437*0b57cec5SDimitry Andric readNameFromStrtab(ArrayRef<uint64_t> Record); 438*0b57cec5SDimitry Andric 43981ad6265SDimitry Andric Error readBlockInfo(); 440*0b57cec5SDimitry Andric 441*0b57cec5SDimitry Andric // Contains an arbitrary and optional string identifying the bitcode producer 442*0b57cec5SDimitry Andric std::string ProducerIdentification; 443*0b57cec5SDimitry Andric 444*0b57cec5SDimitry Andric Error error(const Twine &Message); 445*0b57cec5SDimitry Andric }; 446*0b57cec5SDimitry Andric 447*0b57cec5SDimitry Andric } // end anonymous namespace 448*0b57cec5SDimitry Andric 449*0b57cec5SDimitry Andric Error BitcodeReaderBase::error(const Twine &Message) { 450*0b57cec5SDimitry Andric std::string FullMsg = Message.str(); 451*0b57cec5SDimitry Andric if (!ProducerIdentification.empty()) 452*0b57cec5SDimitry Andric FullMsg += " (Producer: '" + ProducerIdentification + "' Reader: 'LLVM " + 453*0b57cec5SDimitry Andric LLVM_VERSION_STRING "')"; 454*0b57cec5SDimitry Andric return ::error(FullMsg); 455*0b57cec5SDimitry Andric } 456*0b57cec5SDimitry Andric 457*0b57cec5SDimitry Andric Expected<unsigned> 458*0b57cec5SDimitry Andric BitcodeReaderBase::parseVersionRecord(ArrayRef<uint64_t> Record) { 459*0b57cec5SDimitry Andric if (Record.empty()) 46081ad6265SDimitry Andric return error("Invalid version record"); 461*0b57cec5SDimitry Andric unsigned ModuleVersion = Record[0]; 462*0b57cec5SDimitry Andric if (ModuleVersion > 2) 463*0b57cec5SDimitry Andric return error("Invalid value"); 464*0b57cec5SDimitry Andric UseStrtab = ModuleVersion >= 2; 465*0b57cec5SDimitry Andric return ModuleVersion; 466*0b57cec5SDimitry Andric } 467*0b57cec5SDimitry Andric 468*0b57cec5SDimitry Andric std::pair<StringRef, ArrayRef<uint64_t>> 469*0b57cec5SDimitry Andric BitcodeReaderBase::readNameFromStrtab(ArrayRef<uint64_t> Record) { 470*0b57cec5SDimitry Andric if (!UseStrtab) 471*0b57cec5SDimitry Andric return {"", Record}; 472*0b57cec5SDimitry Andric // Invalid reference. Let the caller complain about the record being empty. 473*0b57cec5SDimitry Andric if (Record[0] + Record[1] > Strtab.size()) 474*0b57cec5SDimitry Andric return {"", {}}; 475*0b57cec5SDimitry Andric return {StringRef(Strtab.data() + Record[0], Record[1]), Record.slice(2)}; 476*0b57cec5SDimitry Andric } 477*0b57cec5SDimitry Andric 478*0b57cec5SDimitry Andric namespace { 479*0b57cec5SDimitry Andric 48081ad6265SDimitry Andric /// This represents a constant expression or constant aggregate using a custom 48181ad6265SDimitry Andric /// structure internal to the bitcode reader. Later, this structure will be 48281ad6265SDimitry Andric /// expanded by materializeValue() either into a constant expression/aggregate, 48381ad6265SDimitry Andric /// or into an instruction sequence at the point of use. This allows us to 48481ad6265SDimitry Andric /// upgrade bitcode using constant expressions even if this kind of constant 48581ad6265SDimitry Andric /// expression is no longer supported. 48681ad6265SDimitry Andric class BitcodeConstant final : public Value, 48781ad6265SDimitry Andric TrailingObjects<BitcodeConstant, unsigned> { 48881ad6265SDimitry Andric friend TrailingObjects; 48981ad6265SDimitry Andric 49081ad6265SDimitry Andric // Value subclass ID: Pick largest possible value to avoid any clashes. 49181ad6265SDimitry Andric static constexpr uint8_t SubclassID = 255; 49281ad6265SDimitry Andric 49381ad6265SDimitry Andric public: 49481ad6265SDimitry Andric // Opcodes used for non-expressions. This includes constant aggregates 49581ad6265SDimitry Andric // (struct, array, vector) that might need expansion, as well as non-leaf 49681ad6265SDimitry Andric // constants that don't need expansion (no_cfi, dso_local, blockaddress), 49781ad6265SDimitry Andric // but still go through BitcodeConstant to avoid different uselist orders 49881ad6265SDimitry Andric // between the two cases. 49981ad6265SDimitry Andric static constexpr uint8_t ConstantStructOpcode = 255; 50081ad6265SDimitry Andric static constexpr uint8_t ConstantArrayOpcode = 254; 50181ad6265SDimitry Andric static constexpr uint8_t ConstantVectorOpcode = 253; 50281ad6265SDimitry Andric static constexpr uint8_t NoCFIOpcode = 252; 50381ad6265SDimitry Andric static constexpr uint8_t DSOLocalEquivalentOpcode = 251; 50481ad6265SDimitry Andric static constexpr uint8_t BlockAddressOpcode = 250; 50581ad6265SDimitry Andric static constexpr uint8_t FirstSpecialOpcode = BlockAddressOpcode; 50681ad6265SDimitry Andric 50781ad6265SDimitry Andric // Separate struct to make passing different number of parameters to 50881ad6265SDimitry Andric // BitcodeConstant::create() more convenient. 50981ad6265SDimitry Andric struct ExtraInfo { 51081ad6265SDimitry Andric uint8_t Opcode; 51181ad6265SDimitry Andric uint8_t Flags; 51281ad6265SDimitry Andric unsigned Extra; 51381ad6265SDimitry Andric Type *SrcElemTy; 51481ad6265SDimitry Andric 51581ad6265SDimitry Andric ExtraInfo(uint8_t Opcode, uint8_t Flags = 0, unsigned Extra = 0, 51681ad6265SDimitry Andric Type *SrcElemTy = nullptr) 51781ad6265SDimitry Andric : Opcode(Opcode), Flags(Flags), Extra(Extra), SrcElemTy(SrcElemTy) {} 51881ad6265SDimitry Andric }; 51981ad6265SDimitry Andric 52081ad6265SDimitry Andric uint8_t Opcode; 52181ad6265SDimitry Andric uint8_t Flags; 52281ad6265SDimitry Andric unsigned NumOperands; 52381ad6265SDimitry Andric unsigned Extra; // GEP inrange index or blockaddress BB id. 52481ad6265SDimitry Andric Type *SrcElemTy; // GEP source element type. 52581ad6265SDimitry Andric 52681ad6265SDimitry Andric private: 52781ad6265SDimitry Andric BitcodeConstant(Type *Ty, const ExtraInfo &Info, ArrayRef<unsigned> OpIDs) 52881ad6265SDimitry Andric : Value(Ty, SubclassID), Opcode(Info.Opcode), Flags(Info.Flags), 52981ad6265SDimitry Andric NumOperands(OpIDs.size()), Extra(Info.Extra), 53081ad6265SDimitry Andric SrcElemTy(Info.SrcElemTy) { 53181ad6265SDimitry Andric std::uninitialized_copy(OpIDs.begin(), OpIDs.end(), 53281ad6265SDimitry Andric getTrailingObjects<unsigned>()); 53381ad6265SDimitry Andric } 53481ad6265SDimitry Andric 53581ad6265SDimitry Andric BitcodeConstant &operator=(const BitcodeConstant &) = delete; 53681ad6265SDimitry Andric 53781ad6265SDimitry Andric public: 53881ad6265SDimitry Andric static BitcodeConstant *create(BumpPtrAllocator &A, Type *Ty, 53981ad6265SDimitry Andric const ExtraInfo &Info, 54081ad6265SDimitry Andric ArrayRef<unsigned> OpIDs) { 54181ad6265SDimitry Andric void *Mem = A.Allocate(totalSizeToAlloc<unsigned>(OpIDs.size()), 54281ad6265SDimitry Andric alignof(BitcodeConstant)); 54381ad6265SDimitry Andric return new (Mem) BitcodeConstant(Ty, Info, OpIDs); 54481ad6265SDimitry Andric } 54581ad6265SDimitry Andric 54681ad6265SDimitry Andric static bool classof(const Value *V) { return V->getValueID() == SubclassID; } 54781ad6265SDimitry Andric 54881ad6265SDimitry Andric ArrayRef<unsigned> getOperandIDs() const { 54981ad6265SDimitry Andric return makeArrayRef(getTrailingObjects<unsigned>(), NumOperands); 55081ad6265SDimitry Andric } 55181ad6265SDimitry Andric 55281ad6265SDimitry Andric Optional<unsigned> getInRangeIndex() const { 55381ad6265SDimitry Andric assert(Opcode == Instruction::GetElementPtr); 55481ad6265SDimitry Andric if (Extra == (unsigned)-1) 55581ad6265SDimitry Andric return None; 55681ad6265SDimitry Andric return Extra; 55781ad6265SDimitry Andric } 55881ad6265SDimitry Andric 55981ad6265SDimitry Andric const char *getOpcodeName() const { 56081ad6265SDimitry Andric return Instruction::getOpcodeName(Opcode); 56181ad6265SDimitry Andric } 56281ad6265SDimitry Andric }; 56381ad6265SDimitry Andric 564*0b57cec5SDimitry Andric class BitcodeReader : public BitcodeReaderBase, public GVMaterializer { 565*0b57cec5SDimitry Andric LLVMContext &Context; 566*0b57cec5SDimitry Andric Module *TheModule = nullptr; 567*0b57cec5SDimitry Andric // Next offset to start scanning for lazy parsing of function bodies. 568*0b57cec5SDimitry Andric uint64_t NextUnreadBit = 0; 569*0b57cec5SDimitry Andric // Last function offset found in the VST. 570*0b57cec5SDimitry Andric uint64_t LastFunctionBlockBit = 0; 571*0b57cec5SDimitry Andric bool SeenValueSymbolTable = false; 572*0b57cec5SDimitry Andric uint64_t VSTOffset = 0; 573*0b57cec5SDimitry Andric 574*0b57cec5SDimitry Andric std::vector<std::string> SectionTable; 575*0b57cec5SDimitry Andric std::vector<std::string> GCTable; 576*0b57cec5SDimitry Andric 577*0b57cec5SDimitry Andric std::vector<Type *> TypeList; 57881ad6265SDimitry Andric /// Track type IDs of contained types. Order is the same as the contained 57981ad6265SDimitry Andric /// types of a Type*. This is used during upgrades of typed pointer IR in 58081ad6265SDimitry Andric /// opaque pointer mode. 58181ad6265SDimitry Andric DenseMap<unsigned, SmallVector<unsigned, 1>> ContainedTypeIDs; 58281ad6265SDimitry Andric /// In some cases, we need to create a type ID for a type that was not 58381ad6265SDimitry Andric /// explicitly encoded in the bitcode, or we don't know about at the current 58481ad6265SDimitry Andric /// point. For example, a global may explicitly encode the value type ID, but 58581ad6265SDimitry Andric /// not have a type ID for the pointer to value type, for which we create a 58681ad6265SDimitry Andric /// virtual type ID instead. This map stores the new type ID that was created 58781ad6265SDimitry Andric /// for the given pair of Type and contained type ID. 58881ad6265SDimitry Andric DenseMap<std::pair<Type *, unsigned>, unsigned> VirtualTypeIDs; 58981ad6265SDimitry Andric DenseMap<Function *, unsigned> FunctionTypeIDs; 59081ad6265SDimitry Andric /// Allocator for BitcodeConstants. This should come before ValueList, 59181ad6265SDimitry Andric /// because the ValueList might hold ValueHandles to these constants, so 59281ad6265SDimitry Andric /// ValueList must be destroyed before Alloc. 59381ad6265SDimitry Andric BumpPtrAllocator Alloc; 594*0b57cec5SDimitry Andric BitcodeReaderValueList ValueList; 595*0b57cec5SDimitry Andric Optional<MetadataLoader> MDLoader; 596*0b57cec5SDimitry Andric std::vector<Comdat *> ComdatList; 5970eae32dcSDimitry Andric DenseSet<GlobalObject *> ImplicitComdatObjects; 598*0b57cec5SDimitry Andric SmallVector<Instruction *, 64> InstructionList; 599*0b57cec5SDimitry Andric 600*0b57cec5SDimitry Andric std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInits; 601349cc55cSDimitry Andric std::vector<std::pair<GlobalValue *, unsigned>> IndirectSymbolInits; 602349cc55cSDimitry Andric 603349cc55cSDimitry Andric struct FunctionOperandInfo { 604349cc55cSDimitry Andric Function *F; 605349cc55cSDimitry Andric unsigned PersonalityFn; 606349cc55cSDimitry Andric unsigned Prefix; 607349cc55cSDimitry Andric unsigned Prologue; 608349cc55cSDimitry Andric }; 609349cc55cSDimitry Andric std::vector<FunctionOperandInfo> FunctionOperands; 610*0b57cec5SDimitry Andric 611*0b57cec5SDimitry Andric /// The set of attributes by index. Index zero in the file is for null, and 612*0b57cec5SDimitry Andric /// is thus not represented here. As such all indices are off by one. 613*0b57cec5SDimitry Andric std::vector<AttributeList> MAttributes; 614*0b57cec5SDimitry Andric 615*0b57cec5SDimitry Andric /// The set of attribute groups. 616*0b57cec5SDimitry Andric std::map<unsigned, AttributeList> MAttributeGroups; 617*0b57cec5SDimitry Andric 618*0b57cec5SDimitry Andric /// While parsing a function body, this is a list of the basic blocks for the 619*0b57cec5SDimitry Andric /// function. 620*0b57cec5SDimitry Andric std::vector<BasicBlock*> FunctionBBs; 621*0b57cec5SDimitry Andric 622*0b57cec5SDimitry Andric // When reading the module header, this list is populated with functions that 623*0b57cec5SDimitry Andric // have bodies later in the file. 624*0b57cec5SDimitry Andric std::vector<Function*> FunctionsWithBodies; 625*0b57cec5SDimitry Andric 626*0b57cec5SDimitry Andric // When intrinsic functions are encountered which require upgrading they are 627*0b57cec5SDimitry Andric // stored here with their replacement function. 628*0b57cec5SDimitry Andric using UpdatedIntrinsicMap = DenseMap<Function *, Function *>; 629*0b57cec5SDimitry Andric UpdatedIntrinsicMap UpgradedIntrinsics; 630*0b57cec5SDimitry Andric // Intrinsics which were remangled because of types rename 631*0b57cec5SDimitry Andric UpdatedIntrinsicMap RemangledIntrinsics; 632*0b57cec5SDimitry Andric 633*0b57cec5SDimitry Andric // Several operations happen after the module header has been read, but 634*0b57cec5SDimitry Andric // before function bodies are processed. This keeps track of whether 635*0b57cec5SDimitry Andric // we've done this yet. 636*0b57cec5SDimitry Andric bool SeenFirstFunctionBody = false; 637*0b57cec5SDimitry Andric 638*0b57cec5SDimitry Andric /// When function bodies are initially scanned, this map contains info about 639*0b57cec5SDimitry Andric /// where to find deferred function body in the stream. 640*0b57cec5SDimitry Andric DenseMap<Function*, uint64_t> DeferredFunctionInfo; 641*0b57cec5SDimitry Andric 642*0b57cec5SDimitry Andric /// When Metadata block is initially scanned when parsing the module, we may 643*0b57cec5SDimitry Andric /// choose to defer parsing of the metadata. This vector contains info about 644*0b57cec5SDimitry Andric /// which Metadata blocks are deferred. 645*0b57cec5SDimitry Andric std::vector<uint64_t> DeferredMetadataInfo; 646*0b57cec5SDimitry Andric 647*0b57cec5SDimitry Andric /// These are basic blocks forward-referenced by block addresses. They are 648*0b57cec5SDimitry Andric /// inserted lazily into functions when they're loaded. The basic block ID is 649*0b57cec5SDimitry Andric /// its index into the vector. 650*0b57cec5SDimitry Andric DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs; 651*0b57cec5SDimitry Andric std::deque<Function *> BasicBlockFwdRefQueue; 652*0b57cec5SDimitry Andric 65381ad6265SDimitry Andric /// These are Functions that contain BlockAddresses which refer a different 65481ad6265SDimitry Andric /// Function. When parsing the different Function, queue Functions that refer 65581ad6265SDimitry Andric /// to the different Function. Those Functions must be materialized in order 65681ad6265SDimitry Andric /// to resolve their BlockAddress constants before the different Function 65781ad6265SDimitry Andric /// gets moved into another Module. 65881ad6265SDimitry Andric std::vector<Function *> BackwardRefFunctions; 65981ad6265SDimitry Andric 660*0b57cec5SDimitry Andric /// Indicates that we are using a new encoding for instruction operands where 661*0b57cec5SDimitry Andric /// most operands in the current FUNCTION_BLOCK are encoded relative to the 662*0b57cec5SDimitry Andric /// instruction number, for a more compact encoding. Some instruction 663*0b57cec5SDimitry Andric /// operands are not relative to the instruction ID: basic block numbers, and 664*0b57cec5SDimitry Andric /// types. Once the old style function blocks have been phased out, we would 665*0b57cec5SDimitry Andric /// not need this flag. 666*0b57cec5SDimitry Andric bool UseRelativeIDs = false; 667*0b57cec5SDimitry Andric 668*0b57cec5SDimitry Andric /// True if all functions will be materialized, negating the need to process 669*0b57cec5SDimitry Andric /// (e.g.) blockaddress forward references. 670*0b57cec5SDimitry Andric bool WillMaterializeAllForwardRefs = false; 671*0b57cec5SDimitry Andric 672*0b57cec5SDimitry Andric bool StripDebugInfo = false; 673*0b57cec5SDimitry Andric TBAAVerifier TBAAVerifyHelper; 674*0b57cec5SDimitry Andric 675*0b57cec5SDimitry Andric std::vector<std::string> BundleTags; 676*0b57cec5SDimitry Andric SmallVector<SyncScope::ID, 8> SSIDs; 677*0b57cec5SDimitry Andric 678*0b57cec5SDimitry Andric public: 679*0b57cec5SDimitry Andric BitcodeReader(BitstreamCursor Stream, StringRef Strtab, 680*0b57cec5SDimitry Andric StringRef ProducerIdentification, LLVMContext &Context); 681*0b57cec5SDimitry Andric 682*0b57cec5SDimitry Andric Error materializeForwardReferencedFunctions(); 683*0b57cec5SDimitry Andric 684*0b57cec5SDimitry Andric Error materialize(GlobalValue *GV) override; 685*0b57cec5SDimitry Andric Error materializeModule() override; 686*0b57cec5SDimitry Andric std::vector<StructType *> getIdentifiedStructTypes() const override; 687*0b57cec5SDimitry Andric 688*0b57cec5SDimitry Andric /// Main interface to parsing a bitcode buffer. 689*0b57cec5SDimitry Andric /// \returns true if an error occurred. 6905ffd83dbSDimitry Andric Error parseBitcodeInto( 69181ad6265SDimitry Andric Module *M, bool ShouldLazyLoadMetadata, bool IsImporting, 69281ad6265SDimitry Andric DataLayoutCallbackTy DataLayoutCallback); 693*0b57cec5SDimitry Andric 694*0b57cec5SDimitry Andric static uint64_t decodeSignRotatedValue(uint64_t V); 695*0b57cec5SDimitry Andric 696*0b57cec5SDimitry Andric /// Materialize any deferred Metadata block. 697*0b57cec5SDimitry Andric Error materializeMetadata() override; 698*0b57cec5SDimitry Andric 699*0b57cec5SDimitry Andric void setStripDebugInfo() override; 700*0b57cec5SDimitry Andric 701*0b57cec5SDimitry Andric private: 702*0b57cec5SDimitry Andric std::vector<StructType *> IdentifiedStructTypes; 703*0b57cec5SDimitry Andric StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name); 704*0b57cec5SDimitry Andric StructType *createIdentifiedStructType(LLVMContext &Context); 705*0b57cec5SDimitry Andric 70681ad6265SDimitry Andric static constexpr unsigned InvalidTypeID = ~0u; 707*0b57cec5SDimitry Andric 70881ad6265SDimitry Andric Type *getTypeByID(unsigned ID); 70981ad6265SDimitry Andric Type *getPtrElementTypeByID(unsigned ID); 71081ad6265SDimitry Andric unsigned getContainedTypeID(unsigned ID, unsigned Idx = 0); 71181ad6265SDimitry Andric unsigned getVirtualTypeID(Type *Ty, ArrayRef<unsigned> ContainedTypeIDs = {}); 71281ad6265SDimitry Andric 71381ad6265SDimitry Andric Expected<Value *> materializeValue(unsigned ValID, BasicBlock *InsertBB); 71481ad6265SDimitry Andric Expected<Constant *> getValueForInitializer(unsigned ID); 71581ad6265SDimitry Andric 71681ad6265SDimitry Andric Value *getFnValueByID(unsigned ID, Type *Ty, unsigned TyID, 71781ad6265SDimitry Andric BasicBlock *ConstExprInsertBB) { 718*0b57cec5SDimitry Andric if (Ty && Ty->isMetadataTy()) 719*0b57cec5SDimitry Andric return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID)); 72081ad6265SDimitry Andric return ValueList.getValueFwdRef(ID, Ty, TyID, ConstExprInsertBB); 721*0b57cec5SDimitry Andric } 722*0b57cec5SDimitry Andric 723*0b57cec5SDimitry Andric Metadata *getFnMetadataByID(unsigned ID) { 724*0b57cec5SDimitry Andric return MDLoader->getMetadataFwdRefOrLoad(ID); 725*0b57cec5SDimitry Andric } 726*0b57cec5SDimitry Andric 727*0b57cec5SDimitry Andric BasicBlock *getBasicBlock(unsigned ID) const { 728*0b57cec5SDimitry Andric if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID 729*0b57cec5SDimitry Andric return FunctionBBs[ID]; 730*0b57cec5SDimitry Andric } 731*0b57cec5SDimitry Andric 732*0b57cec5SDimitry Andric AttributeList getAttributes(unsigned i) const { 733*0b57cec5SDimitry Andric if (i-1 < MAttributes.size()) 734*0b57cec5SDimitry Andric return MAttributes[i-1]; 735*0b57cec5SDimitry Andric return AttributeList(); 736*0b57cec5SDimitry Andric } 737*0b57cec5SDimitry Andric 738*0b57cec5SDimitry Andric /// Read a value/type pair out of the specified record from slot 'Slot'. 739*0b57cec5SDimitry Andric /// Increment Slot past the number of slots used in the record. Return true on 740*0b57cec5SDimitry Andric /// failure. 741e8d8bef9SDimitry Andric bool getValueTypePair(const SmallVectorImpl<uint64_t> &Record, unsigned &Slot, 74281ad6265SDimitry Andric unsigned InstNum, Value *&ResVal, unsigned &TypeID, 74381ad6265SDimitry Andric BasicBlock *ConstExprInsertBB) { 744*0b57cec5SDimitry Andric if (Slot == Record.size()) return true; 745*0b57cec5SDimitry Andric unsigned ValNo = (unsigned)Record[Slot++]; 746*0b57cec5SDimitry Andric // Adjust the ValNo, if it was encoded relative to the InstNum. 747*0b57cec5SDimitry Andric if (UseRelativeIDs) 748*0b57cec5SDimitry Andric ValNo = InstNum - ValNo; 749*0b57cec5SDimitry Andric if (ValNo < InstNum) { 750*0b57cec5SDimitry Andric // If this is not a forward reference, just return the value we already 751*0b57cec5SDimitry Andric // have. 75281ad6265SDimitry Andric TypeID = ValueList.getTypeID(ValNo); 75381ad6265SDimitry Andric ResVal = getFnValueByID(ValNo, nullptr, TypeID, ConstExprInsertBB); 75481ad6265SDimitry Andric assert((!ResVal || ResVal->getType() == getTypeByID(TypeID)) && 75581ad6265SDimitry Andric "Incorrect type ID stored for value"); 756*0b57cec5SDimitry Andric return ResVal == nullptr; 757*0b57cec5SDimitry Andric } 758*0b57cec5SDimitry Andric if (Slot == Record.size()) 759*0b57cec5SDimitry Andric return true; 760*0b57cec5SDimitry Andric 76181ad6265SDimitry Andric TypeID = (unsigned)Record[Slot++]; 76281ad6265SDimitry Andric ResVal = getFnValueByID(ValNo, getTypeByID(TypeID), TypeID, 76381ad6265SDimitry Andric ConstExprInsertBB); 764*0b57cec5SDimitry Andric return ResVal == nullptr; 765*0b57cec5SDimitry Andric } 766*0b57cec5SDimitry Andric 767*0b57cec5SDimitry Andric /// Read a value out of the specified record from slot 'Slot'. Increment Slot 768*0b57cec5SDimitry Andric /// past the number of slots used by the value in the record. Return true if 769*0b57cec5SDimitry Andric /// there is an error. 770e8d8bef9SDimitry Andric bool popValue(const SmallVectorImpl<uint64_t> &Record, unsigned &Slot, 77181ad6265SDimitry Andric unsigned InstNum, Type *Ty, unsigned TyID, Value *&ResVal, 77281ad6265SDimitry Andric BasicBlock *ConstExprInsertBB) { 77381ad6265SDimitry Andric if (getValue(Record, Slot, InstNum, Ty, TyID, ResVal, ConstExprInsertBB)) 774*0b57cec5SDimitry Andric return true; 775*0b57cec5SDimitry Andric // All values currently take a single record slot. 776*0b57cec5SDimitry Andric ++Slot; 777*0b57cec5SDimitry Andric return false; 778*0b57cec5SDimitry Andric } 779*0b57cec5SDimitry Andric 780*0b57cec5SDimitry Andric /// Like popValue, but does not increment the Slot number. 781e8d8bef9SDimitry Andric bool getValue(const SmallVectorImpl<uint64_t> &Record, unsigned Slot, 78281ad6265SDimitry Andric unsigned InstNum, Type *Ty, unsigned TyID, Value *&ResVal, 78381ad6265SDimitry Andric BasicBlock *ConstExprInsertBB) { 78481ad6265SDimitry Andric ResVal = getValue(Record, Slot, InstNum, Ty, TyID, ConstExprInsertBB); 785*0b57cec5SDimitry Andric return ResVal == nullptr; 786*0b57cec5SDimitry Andric } 787*0b57cec5SDimitry Andric 788*0b57cec5SDimitry Andric /// Version of getValue that returns ResVal directly, or 0 if there is an 789*0b57cec5SDimitry Andric /// error. 790e8d8bef9SDimitry Andric Value *getValue(const SmallVectorImpl<uint64_t> &Record, unsigned Slot, 79181ad6265SDimitry Andric unsigned InstNum, Type *Ty, unsigned TyID, 79281ad6265SDimitry Andric BasicBlock *ConstExprInsertBB) { 793*0b57cec5SDimitry Andric if (Slot == Record.size()) return nullptr; 794*0b57cec5SDimitry Andric unsigned ValNo = (unsigned)Record[Slot]; 795*0b57cec5SDimitry Andric // Adjust the ValNo, if it was encoded relative to the InstNum. 796*0b57cec5SDimitry Andric if (UseRelativeIDs) 797*0b57cec5SDimitry Andric ValNo = InstNum - ValNo; 79881ad6265SDimitry Andric return getFnValueByID(ValNo, Ty, TyID, ConstExprInsertBB); 799*0b57cec5SDimitry Andric } 800*0b57cec5SDimitry Andric 801*0b57cec5SDimitry Andric /// Like getValue, but decodes signed VBRs. 802e8d8bef9SDimitry Andric Value *getValueSigned(const SmallVectorImpl<uint64_t> &Record, unsigned Slot, 80381ad6265SDimitry Andric unsigned InstNum, Type *Ty, unsigned TyID, 80481ad6265SDimitry Andric BasicBlock *ConstExprInsertBB) { 805*0b57cec5SDimitry Andric if (Slot == Record.size()) return nullptr; 806*0b57cec5SDimitry Andric unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]); 807*0b57cec5SDimitry Andric // Adjust the ValNo, if it was encoded relative to the InstNum. 808*0b57cec5SDimitry Andric if (UseRelativeIDs) 809*0b57cec5SDimitry Andric ValNo = InstNum - ValNo; 81081ad6265SDimitry Andric return getFnValueByID(ValNo, Ty, TyID, ConstExprInsertBB); 811*0b57cec5SDimitry Andric } 812*0b57cec5SDimitry Andric 813fe6060f1SDimitry Andric /// Upgrades old-style typeless byval/sret/inalloca attributes by adding the 814fe6060f1SDimitry Andric /// corresponding argument's pointee type. Also upgrades intrinsics that now 815fe6060f1SDimitry Andric /// require an elementtype attribute. 81681ad6265SDimitry Andric Error propagateAttributeTypes(CallBase *CB, ArrayRef<unsigned> ArgsTys); 817*0b57cec5SDimitry Andric 818*0b57cec5SDimitry Andric /// Converts alignment exponent (i.e. power of two (or zero)) to the 819*0b57cec5SDimitry Andric /// corresponding alignment to use. If alignment is too large, returns 820*0b57cec5SDimitry Andric /// a corresponding error code. 8218bcb0991SDimitry Andric Error parseAlignmentValue(uint64_t Exponent, MaybeAlign &Alignment); 822*0b57cec5SDimitry Andric Error parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind); 8235ffd83dbSDimitry Andric Error parseModule( 8245ffd83dbSDimitry Andric uint64_t ResumeBit, bool ShouldLazyLoadMetadata = false, 8255ffd83dbSDimitry Andric DataLayoutCallbackTy DataLayoutCallback = [](StringRef) { return None; }); 826*0b57cec5SDimitry Andric 827*0b57cec5SDimitry Andric Error parseComdatRecord(ArrayRef<uint64_t> Record); 828*0b57cec5SDimitry Andric Error parseGlobalVarRecord(ArrayRef<uint64_t> Record); 829*0b57cec5SDimitry Andric Error parseFunctionRecord(ArrayRef<uint64_t> Record); 830*0b57cec5SDimitry Andric Error parseGlobalIndirectSymbolRecord(unsigned BitCode, 831*0b57cec5SDimitry Andric ArrayRef<uint64_t> Record); 832*0b57cec5SDimitry Andric 833*0b57cec5SDimitry Andric Error parseAttributeBlock(); 834*0b57cec5SDimitry Andric Error parseAttributeGroupBlock(); 835*0b57cec5SDimitry Andric Error parseTypeTable(); 836*0b57cec5SDimitry Andric Error parseTypeTableBody(); 837*0b57cec5SDimitry Andric Error parseOperandBundleTags(); 838*0b57cec5SDimitry Andric Error parseSyncScopeNames(); 839*0b57cec5SDimitry Andric 840*0b57cec5SDimitry Andric Expected<Value *> recordValue(SmallVectorImpl<uint64_t> &Record, 841*0b57cec5SDimitry Andric unsigned NameIndex, Triple &TT); 842*0b57cec5SDimitry Andric void setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta, Function *F, 843*0b57cec5SDimitry Andric ArrayRef<uint64_t> Record); 844*0b57cec5SDimitry Andric Error parseValueSymbolTable(uint64_t Offset = 0); 845*0b57cec5SDimitry Andric Error parseGlobalValueSymbolTable(); 846*0b57cec5SDimitry Andric Error parseConstants(); 847*0b57cec5SDimitry Andric Error rememberAndSkipFunctionBodies(); 848*0b57cec5SDimitry Andric Error rememberAndSkipFunctionBody(); 849*0b57cec5SDimitry Andric /// Save the positions of the Metadata blocks and skip parsing the blocks. 850*0b57cec5SDimitry Andric Error rememberAndSkipMetadata(); 851*0b57cec5SDimitry Andric Error typeCheckLoadStoreInst(Type *ValType, Type *PtrType); 852*0b57cec5SDimitry Andric Error parseFunctionBody(Function *F); 853*0b57cec5SDimitry Andric Error globalCleanup(); 854*0b57cec5SDimitry Andric Error resolveGlobalAndIndirectSymbolInits(); 855*0b57cec5SDimitry Andric Error parseUseLists(); 856*0b57cec5SDimitry Andric Error findFunctionInStream( 857*0b57cec5SDimitry Andric Function *F, 858*0b57cec5SDimitry Andric DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator); 859*0b57cec5SDimitry Andric 860*0b57cec5SDimitry Andric SyncScope::ID getDecodedSyncScopeID(unsigned Val); 861*0b57cec5SDimitry Andric }; 862*0b57cec5SDimitry Andric 863*0b57cec5SDimitry Andric /// Class to manage reading and parsing function summary index bitcode 864*0b57cec5SDimitry Andric /// files/sections. 865*0b57cec5SDimitry Andric class ModuleSummaryIndexBitcodeReader : public BitcodeReaderBase { 866*0b57cec5SDimitry Andric /// The module index built during parsing. 867*0b57cec5SDimitry Andric ModuleSummaryIndex &TheIndex; 868*0b57cec5SDimitry Andric 869*0b57cec5SDimitry Andric /// Indicates whether we have encountered a global value summary section 870*0b57cec5SDimitry Andric /// yet during parsing. 871*0b57cec5SDimitry Andric bool SeenGlobalValSummary = false; 872*0b57cec5SDimitry Andric 873*0b57cec5SDimitry Andric /// Indicates whether we have already parsed the VST, used for error checking. 874*0b57cec5SDimitry Andric bool SeenValueSymbolTable = false; 875*0b57cec5SDimitry Andric 876*0b57cec5SDimitry Andric /// Set to the offset of the VST recorded in the MODULE_CODE_VSTOFFSET record. 877*0b57cec5SDimitry Andric /// Used to enable on-demand parsing of the VST. 878*0b57cec5SDimitry Andric uint64_t VSTOffset = 0; 879*0b57cec5SDimitry Andric 880*0b57cec5SDimitry Andric // Map to save ValueId to ValueInfo association that was recorded in the 881*0b57cec5SDimitry Andric // ValueSymbolTable. It is used after the VST is parsed to convert 882*0b57cec5SDimitry Andric // call graph edges read from the function summary from referencing 883*0b57cec5SDimitry Andric // callees by their ValueId to using the ValueInfo instead, which is how 884*0b57cec5SDimitry Andric // they are recorded in the summary index being built. 885*0b57cec5SDimitry Andric // We save a GUID which refers to the same global as the ValueInfo, but 886*0b57cec5SDimitry Andric // ignoring the linkage, i.e. for values other than local linkage they are 887*0b57cec5SDimitry Andric // identical. 888*0b57cec5SDimitry Andric DenseMap<unsigned, std::pair<ValueInfo, GlobalValue::GUID>> 889*0b57cec5SDimitry Andric ValueIdToValueInfoMap; 890*0b57cec5SDimitry Andric 891*0b57cec5SDimitry Andric /// Map populated during module path string table parsing, from the 892*0b57cec5SDimitry Andric /// module ID to a string reference owned by the index's module 893*0b57cec5SDimitry Andric /// path string table, used to correlate with combined index 894*0b57cec5SDimitry Andric /// summary records. 895*0b57cec5SDimitry Andric DenseMap<uint64_t, StringRef> ModuleIdMap; 896*0b57cec5SDimitry Andric 897*0b57cec5SDimitry Andric /// Original source file name recorded in a bitcode record. 898*0b57cec5SDimitry Andric std::string SourceFileName; 899*0b57cec5SDimitry Andric 900*0b57cec5SDimitry Andric /// The string identifier given to this module by the client, normally the 901*0b57cec5SDimitry Andric /// path to the bitcode file. 902*0b57cec5SDimitry Andric StringRef ModulePath; 903*0b57cec5SDimitry Andric 904*0b57cec5SDimitry Andric /// For per-module summary indexes, the unique numerical identifier given to 905*0b57cec5SDimitry Andric /// this module by the client. 906*0b57cec5SDimitry Andric unsigned ModuleId; 907*0b57cec5SDimitry Andric 908*0b57cec5SDimitry Andric public: 909*0b57cec5SDimitry Andric ModuleSummaryIndexBitcodeReader(BitstreamCursor Stream, StringRef Strtab, 910*0b57cec5SDimitry Andric ModuleSummaryIndex &TheIndex, 911*0b57cec5SDimitry Andric StringRef ModulePath, unsigned ModuleId); 912*0b57cec5SDimitry Andric 913*0b57cec5SDimitry Andric Error parseModule(); 914*0b57cec5SDimitry Andric 915*0b57cec5SDimitry Andric private: 916*0b57cec5SDimitry Andric void setValueGUID(uint64_t ValueID, StringRef ValueName, 917*0b57cec5SDimitry Andric GlobalValue::LinkageTypes Linkage, 918*0b57cec5SDimitry Andric StringRef SourceFileName); 919*0b57cec5SDimitry Andric Error parseValueSymbolTable( 920*0b57cec5SDimitry Andric uint64_t Offset, 921*0b57cec5SDimitry Andric DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap); 922*0b57cec5SDimitry Andric std::vector<ValueInfo> makeRefList(ArrayRef<uint64_t> Record); 923*0b57cec5SDimitry Andric std::vector<FunctionSummary::EdgeTy> makeCallList(ArrayRef<uint64_t> Record, 924*0b57cec5SDimitry Andric bool IsOldProfileFormat, 925*0b57cec5SDimitry Andric bool HasProfile, 926*0b57cec5SDimitry Andric bool HasRelBF); 927*0b57cec5SDimitry Andric Error parseEntireSummary(unsigned ID); 928*0b57cec5SDimitry Andric Error parseModuleStringTable(); 929*0b57cec5SDimitry Andric void parseTypeIdCompatibleVtableSummaryRecord(ArrayRef<uint64_t> Record); 930*0b57cec5SDimitry Andric void parseTypeIdCompatibleVtableInfo(ArrayRef<uint64_t> Record, size_t &Slot, 931*0b57cec5SDimitry Andric TypeIdCompatibleVtableInfo &TypeId); 932e8d8bef9SDimitry Andric std::vector<FunctionSummary::ParamAccess> 933e8d8bef9SDimitry Andric parseParamAccesses(ArrayRef<uint64_t> Record); 934*0b57cec5SDimitry Andric 935*0b57cec5SDimitry Andric std::pair<ValueInfo, GlobalValue::GUID> 936*0b57cec5SDimitry Andric getValueInfoFromValueId(unsigned ValueId); 937*0b57cec5SDimitry Andric 938*0b57cec5SDimitry Andric void addThisModule(); 939*0b57cec5SDimitry Andric ModuleSummaryIndex::ModuleInfo *getThisModule(); 940*0b57cec5SDimitry Andric }; 941*0b57cec5SDimitry Andric 942*0b57cec5SDimitry Andric } // end anonymous namespace 943*0b57cec5SDimitry Andric 944*0b57cec5SDimitry Andric std::error_code llvm::errorToErrorCodeAndEmitErrors(LLVMContext &Ctx, 945*0b57cec5SDimitry Andric Error Err) { 946*0b57cec5SDimitry Andric if (Err) { 947*0b57cec5SDimitry Andric std::error_code EC; 948*0b57cec5SDimitry Andric handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) { 949*0b57cec5SDimitry Andric EC = EIB.convertToErrorCode(); 950*0b57cec5SDimitry Andric Ctx.emitError(EIB.message()); 951*0b57cec5SDimitry Andric }); 952*0b57cec5SDimitry Andric return EC; 953*0b57cec5SDimitry Andric } 954*0b57cec5SDimitry Andric return std::error_code(); 955*0b57cec5SDimitry Andric } 956*0b57cec5SDimitry Andric 957*0b57cec5SDimitry Andric BitcodeReader::BitcodeReader(BitstreamCursor Stream, StringRef Strtab, 958*0b57cec5SDimitry Andric StringRef ProducerIdentification, 959*0b57cec5SDimitry Andric LLVMContext &Context) 960*0b57cec5SDimitry Andric : BitcodeReaderBase(std::move(Stream), Strtab), Context(Context), 96181ad6265SDimitry Andric ValueList(this->Stream.SizeInBytes(), 96281ad6265SDimitry Andric [this](unsigned ValID, BasicBlock *InsertBB) { 96381ad6265SDimitry Andric return materializeValue(ValID, InsertBB); 96481ad6265SDimitry Andric }) { 9655ffd83dbSDimitry Andric this->ProducerIdentification = std::string(ProducerIdentification); 966*0b57cec5SDimitry Andric } 967*0b57cec5SDimitry Andric 968*0b57cec5SDimitry Andric Error BitcodeReader::materializeForwardReferencedFunctions() { 969*0b57cec5SDimitry Andric if (WillMaterializeAllForwardRefs) 970*0b57cec5SDimitry Andric return Error::success(); 971*0b57cec5SDimitry Andric 972*0b57cec5SDimitry Andric // Prevent recursion. 973*0b57cec5SDimitry Andric WillMaterializeAllForwardRefs = true; 974*0b57cec5SDimitry Andric 975*0b57cec5SDimitry Andric while (!BasicBlockFwdRefQueue.empty()) { 976*0b57cec5SDimitry Andric Function *F = BasicBlockFwdRefQueue.front(); 977*0b57cec5SDimitry Andric BasicBlockFwdRefQueue.pop_front(); 978*0b57cec5SDimitry Andric assert(F && "Expected valid function"); 979*0b57cec5SDimitry Andric if (!BasicBlockFwdRefs.count(F)) 980*0b57cec5SDimitry Andric // Already materialized. 981*0b57cec5SDimitry Andric continue; 982*0b57cec5SDimitry Andric 983*0b57cec5SDimitry Andric // Check for a function that isn't materializable to prevent an infinite 984*0b57cec5SDimitry Andric // loop. When parsing a blockaddress stored in a global variable, there 985*0b57cec5SDimitry Andric // isn't a trivial way to check if a function will have a body without a 986*0b57cec5SDimitry Andric // linear search through FunctionsWithBodies, so just check it here. 987*0b57cec5SDimitry Andric if (!F->isMaterializable()) 988*0b57cec5SDimitry Andric return error("Never resolved function from blockaddress"); 989*0b57cec5SDimitry Andric 990*0b57cec5SDimitry Andric // Try to materialize F. 991*0b57cec5SDimitry Andric if (Error Err = materialize(F)) 992*0b57cec5SDimitry Andric return Err; 993*0b57cec5SDimitry Andric } 994*0b57cec5SDimitry Andric assert(BasicBlockFwdRefs.empty() && "Function missing from queue"); 995*0b57cec5SDimitry Andric 99681ad6265SDimitry Andric for (Function *F : BackwardRefFunctions) 99781ad6265SDimitry Andric if (Error Err = materialize(F)) 99881ad6265SDimitry Andric return Err; 99981ad6265SDimitry Andric BackwardRefFunctions.clear(); 100081ad6265SDimitry Andric 1001*0b57cec5SDimitry Andric // Reset state. 1002*0b57cec5SDimitry Andric WillMaterializeAllForwardRefs = false; 1003*0b57cec5SDimitry Andric return Error::success(); 1004*0b57cec5SDimitry Andric } 1005*0b57cec5SDimitry Andric 1006*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1007*0b57cec5SDimitry Andric // Helper functions to implement forward reference resolution, etc. 1008*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1009*0b57cec5SDimitry Andric 1010*0b57cec5SDimitry Andric static bool hasImplicitComdat(size_t Val) { 1011*0b57cec5SDimitry Andric switch (Val) { 1012*0b57cec5SDimitry Andric default: 1013*0b57cec5SDimitry Andric return false; 1014*0b57cec5SDimitry Andric case 1: // Old WeakAnyLinkage 1015*0b57cec5SDimitry Andric case 4: // Old LinkOnceAnyLinkage 1016*0b57cec5SDimitry Andric case 10: // Old WeakODRLinkage 1017*0b57cec5SDimitry Andric case 11: // Old LinkOnceODRLinkage 1018*0b57cec5SDimitry Andric return true; 1019*0b57cec5SDimitry Andric } 1020*0b57cec5SDimitry Andric } 1021*0b57cec5SDimitry Andric 1022*0b57cec5SDimitry Andric static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) { 1023*0b57cec5SDimitry Andric switch (Val) { 1024*0b57cec5SDimitry Andric default: // Map unknown/new linkages to external 1025*0b57cec5SDimitry Andric case 0: 1026*0b57cec5SDimitry Andric return GlobalValue::ExternalLinkage; 1027*0b57cec5SDimitry Andric case 2: 1028*0b57cec5SDimitry Andric return GlobalValue::AppendingLinkage; 1029*0b57cec5SDimitry Andric case 3: 1030*0b57cec5SDimitry Andric return GlobalValue::InternalLinkage; 1031*0b57cec5SDimitry Andric case 5: 1032*0b57cec5SDimitry Andric return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage 1033*0b57cec5SDimitry Andric case 6: 1034*0b57cec5SDimitry Andric return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage 1035*0b57cec5SDimitry Andric case 7: 1036*0b57cec5SDimitry Andric return GlobalValue::ExternalWeakLinkage; 1037*0b57cec5SDimitry Andric case 8: 1038*0b57cec5SDimitry Andric return GlobalValue::CommonLinkage; 1039*0b57cec5SDimitry Andric case 9: 1040*0b57cec5SDimitry Andric return GlobalValue::PrivateLinkage; 1041*0b57cec5SDimitry Andric case 12: 1042*0b57cec5SDimitry Andric return GlobalValue::AvailableExternallyLinkage; 1043*0b57cec5SDimitry Andric case 13: 1044*0b57cec5SDimitry Andric return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage 1045*0b57cec5SDimitry Andric case 14: 1046*0b57cec5SDimitry Andric return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage 1047*0b57cec5SDimitry Andric case 15: 1048*0b57cec5SDimitry Andric return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage 1049*0b57cec5SDimitry Andric case 1: // Old value with implicit comdat. 1050*0b57cec5SDimitry Andric case 16: 1051*0b57cec5SDimitry Andric return GlobalValue::WeakAnyLinkage; 1052*0b57cec5SDimitry Andric case 10: // Old value with implicit comdat. 1053*0b57cec5SDimitry Andric case 17: 1054*0b57cec5SDimitry Andric return GlobalValue::WeakODRLinkage; 1055*0b57cec5SDimitry Andric case 4: // Old value with implicit comdat. 1056*0b57cec5SDimitry Andric case 18: 1057*0b57cec5SDimitry Andric return GlobalValue::LinkOnceAnyLinkage; 1058*0b57cec5SDimitry Andric case 11: // Old value with implicit comdat. 1059*0b57cec5SDimitry Andric case 19: 1060*0b57cec5SDimitry Andric return GlobalValue::LinkOnceODRLinkage; 1061*0b57cec5SDimitry Andric } 1062*0b57cec5SDimitry Andric } 1063*0b57cec5SDimitry Andric 1064*0b57cec5SDimitry Andric static FunctionSummary::FFlags getDecodedFFlags(uint64_t RawFlags) { 1065*0b57cec5SDimitry Andric FunctionSummary::FFlags Flags; 1066*0b57cec5SDimitry Andric Flags.ReadNone = RawFlags & 0x1; 1067*0b57cec5SDimitry Andric Flags.ReadOnly = (RawFlags >> 1) & 0x1; 1068*0b57cec5SDimitry Andric Flags.NoRecurse = (RawFlags >> 2) & 0x1; 1069*0b57cec5SDimitry Andric Flags.ReturnDoesNotAlias = (RawFlags >> 3) & 0x1; 1070*0b57cec5SDimitry Andric Flags.NoInline = (RawFlags >> 4) & 0x1; 1071480093f4SDimitry Andric Flags.AlwaysInline = (RawFlags >> 5) & 0x1; 1072349cc55cSDimitry Andric Flags.NoUnwind = (RawFlags >> 6) & 0x1; 1073349cc55cSDimitry Andric Flags.MayThrow = (RawFlags >> 7) & 0x1; 1074349cc55cSDimitry Andric Flags.HasUnknownCall = (RawFlags >> 8) & 0x1; 10750eae32dcSDimitry Andric Flags.MustBeUnreachable = (RawFlags >> 9) & 0x1; 1076*0b57cec5SDimitry Andric return Flags; 1077*0b57cec5SDimitry Andric } 1078*0b57cec5SDimitry Andric 1079fe6060f1SDimitry Andric // Decode the flags for GlobalValue in the summary. The bits for each attribute: 1080fe6060f1SDimitry Andric // 1081fe6060f1SDimitry Andric // linkage: [0,4), notEligibleToImport: 4, live: 5, local: 6, canAutoHide: 7, 1082fe6060f1SDimitry Andric // visibility: [8, 10). 1083*0b57cec5SDimitry Andric static GlobalValueSummary::GVFlags getDecodedGVSummaryFlags(uint64_t RawFlags, 1084*0b57cec5SDimitry Andric uint64_t Version) { 1085*0b57cec5SDimitry Andric // Summary were not emitted before LLVM 3.9, we don't need to upgrade Linkage 1086*0b57cec5SDimitry Andric // like getDecodedLinkage() above. Any future change to the linkage enum and 1087*0b57cec5SDimitry Andric // to getDecodedLinkage() will need to be taken into account here as above. 1088*0b57cec5SDimitry Andric auto Linkage = GlobalValue::LinkageTypes(RawFlags & 0xF); // 4 bits 1089fe6060f1SDimitry Andric auto Visibility = GlobalValue::VisibilityTypes((RawFlags >> 8) & 3); // 2 bits 1090*0b57cec5SDimitry Andric RawFlags = RawFlags >> 4; 1091*0b57cec5SDimitry Andric bool NotEligibleToImport = (RawFlags & 0x1) || Version < 3; 1092*0b57cec5SDimitry Andric // The Live flag wasn't introduced until version 3. For dead stripping 1093*0b57cec5SDimitry Andric // to work correctly on earlier versions, we must conservatively treat all 1094*0b57cec5SDimitry Andric // values as live. 1095*0b57cec5SDimitry Andric bool Live = (RawFlags & 0x2) || Version < 3; 1096*0b57cec5SDimitry Andric bool Local = (RawFlags & 0x4); 1097*0b57cec5SDimitry Andric bool AutoHide = (RawFlags & 0x8); 1098*0b57cec5SDimitry Andric 1099fe6060f1SDimitry Andric return GlobalValueSummary::GVFlags(Linkage, Visibility, NotEligibleToImport, 1100fe6060f1SDimitry Andric Live, Local, AutoHide); 1101*0b57cec5SDimitry Andric } 1102*0b57cec5SDimitry Andric 1103*0b57cec5SDimitry Andric // Decode the flags for GlobalVariable in the summary 1104*0b57cec5SDimitry Andric static GlobalVarSummary::GVarFlags getDecodedGVarFlags(uint64_t RawFlags) { 11055ffd83dbSDimitry Andric return GlobalVarSummary::GVarFlags( 11065ffd83dbSDimitry Andric (RawFlags & 0x1) ? true : false, (RawFlags & 0x2) ? true : false, 11075ffd83dbSDimitry Andric (RawFlags & 0x4) ? true : false, 11085ffd83dbSDimitry Andric (GlobalObject::VCallVisibility)(RawFlags >> 3)); 1109*0b57cec5SDimitry Andric } 1110*0b57cec5SDimitry Andric 1111*0b57cec5SDimitry Andric static GlobalValue::VisibilityTypes getDecodedVisibility(unsigned Val) { 1112*0b57cec5SDimitry Andric switch (Val) { 1113*0b57cec5SDimitry Andric default: // Map unknown visibilities to default. 1114*0b57cec5SDimitry Andric case 0: return GlobalValue::DefaultVisibility; 1115*0b57cec5SDimitry Andric case 1: return GlobalValue::HiddenVisibility; 1116*0b57cec5SDimitry Andric case 2: return GlobalValue::ProtectedVisibility; 1117*0b57cec5SDimitry Andric } 1118*0b57cec5SDimitry Andric } 1119*0b57cec5SDimitry Andric 1120*0b57cec5SDimitry Andric static GlobalValue::DLLStorageClassTypes 1121*0b57cec5SDimitry Andric getDecodedDLLStorageClass(unsigned Val) { 1122*0b57cec5SDimitry Andric switch (Val) { 1123*0b57cec5SDimitry Andric default: // Map unknown values to default. 1124*0b57cec5SDimitry Andric case 0: return GlobalValue::DefaultStorageClass; 1125*0b57cec5SDimitry Andric case 1: return GlobalValue::DLLImportStorageClass; 1126*0b57cec5SDimitry Andric case 2: return GlobalValue::DLLExportStorageClass; 1127*0b57cec5SDimitry Andric } 1128*0b57cec5SDimitry Andric } 1129*0b57cec5SDimitry Andric 1130*0b57cec5SDimitry Andric static bool getDecodedDSOLocal(unsigned Val) { 1131*0b57cec5SDimitry Andric switch(Val) { 1132*0b57cec5SDimitry Andric default: // Map unknown values to preemptable. 1133*0b57cec5SDimitry Andric case 0: return false; 1134*0b57cec5SDimitry Andric case 1: return true; 1135*0b57cec5SDimitry Andric } 1136*0b57cec5SDimitry Andric } 1137*0b57cec5SDimitry Andric 1138*0b57cec5SDimitry Andric static GlobalVariable::ThreadLocalMode getDecodedThreadLocalMode(unsigned Val) { 1139*0b57cec5SDimitry Andric switch (Val) { 1140*0b57cec5SDimitry Andric case 0: return GlobalVariable::NotThreadLocal; 1141*0b57cec5SDimitry Andric default: // Map unknown non-zero value to general dynamic. 1142*0b57cec5SDimitry Andric case 1: return GlobalVariable::GeneralDynamicTLSModel; 1143*0b57cec5SDimitry Andric case 2: return GlobalVariable::LocalDynamicTLSModel; 1144*0b57cec5SDimitry Andric case 3: return GlobalVariable::InitialExecTLSModel; 1145*0b57cec5SDimitry Andric case 4: return GlobalVariable::LocalExecTLSModel; 1146*0b57cec5SDimitry Andric } 1147*0b57cec5SDimitry Andric } 1148*0b57cec5SDimitry Andric 1149*0b57cec5SDimitry Andric static GlobalVariable::UnnamedAddr getDecodedUnnamedAddrType(unsigned Val) { 1150*0b57cec5SDimitry Andric switch (Val) { 1151*0b57cec5SDimitry Andric default: // Map unknown to UnnamedAddr::None. 1152*0b57cec5SDimitry Andric case 0: return GlobalVariable::UnnamedAddr::None; 1153*0b57cec5SDimitry Andric case 1: return GlobalVariable::UnnamedAddr::Global; 1154*0b57cec5SDimitry Andric case 2: return GlobalVariable::UnnamedAddr::Local; 1155*0b57cec5SDimitry Andric } 1156*0b57cec5SDimitry Andric } 1157*0b57cec5SDimitry Andric 1158*0b57cec5SDimitry Andric static int getDecodedCastOpcode(unsigned Val) { 1159*0b57cec5SDimitry Andric switch (Val) { 1160*0b57cec5SDimitry Andric default: return -1; 1161*0b57cec5SDimitry Andric case bitc::CAST_TRUNC : return Instruction::Trunc; 1162*0b57cec5SDimitry Andric case bitc::CAST_ZEXT : return Instruction::ZExt; 1163*0b57cec5SDimitry Andric case bitc::CAST_SEXT : return Instruction::SExt; 1164*0b57cec5SDimitry Andric case bitc::CAST_FPTOUI : return Instruction::FPToUI; 1165*0b57cec5SDimitry Andric case bitc::CAST_FPTOSI : return Instruction::FPToSI; 1166*0b57cec5SDimitry Andric case bitc::CAST_UITOFP : return Instruction::UIToFP; 1167*0b57cec5SDimitry Andric case bitc::CAST_SITOFP : return Instruction::SIToFP; 1168*0b57cec5SDimitry Andric case bitc::CAST_FPTRUNC : return Instruction::FPTrunc; 1169*0b57cec5SDimitry Andric case bitc::CAST_FPEXT : return Instruction::FPExt; 1170*0b57cec5SDimitry Andric case bitc::CAST_PTRTOINT: return Instruction::PtrToInt; 1171*0b57cec5SDimitry Andric case bitc::CAST_INTTOPTR: return Instruction::IntToPtr; 1172*0b57cec5SDimitry Andric case bitc::CAST_BITCAST : return Instruction::BitCast; 1173*0b57cec5SDimitry Andric case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast; 1174*0b57cec5SDimitry Andric } 1175*0b57cec5SDimitry Andric } 1176*0b57cec5SDimitry Andric 1177*0b57cec5SDimitry Andric static int getDecodedUnaryOpcode(unsigned Val, Type *Ty) { 1178*0b57cec5SDimitry Andric bool IsFP = Ty->isFPOrFPVectorTy(); 1179*0b57cec5SDimitry Andric // UnOps are only valid for int/fp or vector of int/fp types 1180*0b57cec5SDimitry Andric if (!IsFP && !Ty->isIntOrIntVectorTy()) 1181*0b57cec5SDimitry Andric return -1; 1182*0b57cec5SDimitry Andric 1183*0b57cec5SDimitry Andric switch (Val) { 1184*0b57cec5SDimitry Andric default: 1185*0b57cec5SDimitry Andric return -1; 11868bcb0991SDimitry Andric case bitc::UNOP_FNEG: 1187*0b57cec5SDimitry Andric return IsFP ? Instruction::FNeg : -1; 1188*0b57cec5SDimitry Andric } 1189*0b57cec5SDimitry Andric } 1190*0b57cec5SDimitry Andric 1191*0b57cec5SDimitry Andric static int getDecodedBinaryOpcode(unsigned Val, Type *Ty) { 1192*0b57cec5SDimitry Andric bool IsFP = Ty->isFPOrFPVectorTy(); 1193*0b57cec5SDimitry Andric // BinOps are only valid for int/fp or vector of int/fp types 1194*0b57cec5SDimitry Andric if (!IsFP && !Ty->isIntOrIntVectorTy()) 1195*0b57cec5SDimitry Andric return -1; 1196*0b57cec5SDimitry Andric 1197*0b57cec5SDimitry Andric switch (Val) { 1198*0b57cec5SDimitry Andric default: 1199*0b57cec5SDimitry Andric return -1; 1200*0b57cec5SDimitry Andric case bitc::BINOP_ADD: 1201*0b57cec5SDimitry Andric return IsFP ? Instruction::FAdd : Instruction::Add; 1202*0b57cec5SDimitry Andric case bitc::BINOP_SUB: 1203*0b57cec5SDimitry Andric return IsFP ? Instruction::FSub : Instruction::Sub; 1204*0b57cec5SDimitry Andric case bitc::BINOP_MUL: 1205*0b57cec5SDimitry Andric return IsFP ? Instruction::FMul : Instruction::Mul; 1206*0b57cec5SDimitry Andric case bitc::BINOP_UDIV: 1207*0b57cec5SDimitry Andric return IsFP ? -1 : Instruction::UDiv; 1208*0b57cec5SDimitry Andric case bitc::BINOP_SDIV: 1209*0b57cec5SDimitry Andric return IsFP ? Instruction::FDiv : Instruction::SDiv; 1210*0b57cec5SDimitry Andric case bitc::BINOP_UREM: 1211*0b57cec5SDimitry Andric return IsFP ? -1 : Instruction::URem; 1212*0b57cec5SDimitry Andric case bitc::BINOP_SREM: 1213*0b57cec5SDimitry Andric return IsFP ? Instruction::FRem : Instruction::SRem; 1214*0b57cec5SDimitry Andric case bitc::BINOP_SHL: 1215*0b57cec5SDimitry Andric return IsFP ? -1 : Instruction::Shl; 1216*0b57cec5SDimitry Andric case bitc::BINOP_LSHR: 1217*0b57cec5SDimitry Andric return IsFP ? -1 : Instruction::LShr; 1218*0b57cec5SDimitry Andric case bitc::BINOP_ASHR: 1219*0b57cec5SDimitry Andric return IsFP ? -1 : Instruction::AShr; 1220*0b57cec5SDimitry Andric case bitc::BINOP_AND: 1221*0b57cec5SDimitry Andric return IsFP ? -1 : Instruction::And; 1222*0b57cec5SDimitry Andric case bitc::BINOP_OR: 1223*0b57cec5SDimitry Andric return IsFP ? -1 : Instruction::Or; 1224*0b57cec5SDimitry Andric case bitc::BINOP_XOR: 1225*0b57cec5SDimitry Andric return IsFP ? -1 : Instruction::Xor; 1226*0b57cec5SDimitry Andric } 1227*0b57cec5SDimitry Andric } 1228*0b57cec5SDimitry Andric 1229*0b57cec5SDimitry Andric static AtomicRMWInst::BinOp getDecodedRMWOperation(unsigned Val) { 1230*0b57cec5SDimitry Andric switch (Val) { 1231*0b57cec5SDimitry Andric default: return AtomicRMWInst::BAD_BINOP; 1232*0b57cec5SDimitry Andric case bitc::RMW_XCHG: return AtomicRMWInst::Xchg; 1233*0b57cec5SDimitry Andric case bitc::RMW_ADD: return AtomicRMWInst::Add; 1234*0b57cec5SDimitry Andric case bitc::RMW_SUB: return AtomicRMWInst::Sub; 1235*0b57cec5SDimitry Andric case bitc::RMW_AND: return AtomicRMWInst::And; 1236*0b57cec5SDimitry Andric case bitc::RMW_NAND: return AtomicRMWInst::Nand; 1237*0b57cec5SDimitry Andric case bitc::RMW_OR: return AtomicRMWInst::Or; 1238*0b57cec5SDimitry Andric case bitc::RMW_XOR: return AtomicRMWInst::Xor; 1239*0b57cec5SDimitry Andric case bitc::RMW_MAX: return AtomicRMWInst::Max; 1240*0b57cec5SDimitry Andric case bitc::RMW_MIN: return AtomicRMWInst::Min; 1241*0b57cec5SDimitry Andric case bitc::RMW_UMAX: return AtomicRMWInst::UMax; 1242*0b57cec5SDimitry Andric case bitc::RMW_UMIN: return AtomicRMWInst::UMin; 1243*0b57cec5SDimitry Andric case bitc::RMW_FADD: return AtomicRMWInst::FAdd; 1244*0b57cec5SDimitry Andric case bitc::RMW_FSUB: return AtomicRMWInst::FSub; 1245753f127fSDimitry Andric case bitc::RMW_FMAX: return AtomicRMWInst::FMax; 1246753f127fSDimitry Andric case bitc::RMW_FMIN: return AtomicRMWInst::FMin; 1247*0b57cec5SDimitry Andric } 1248*0b57cec5SDimitry Andric } 1249*0b57cec5SDimitry Andric 1250*0b57cec5SDimitry Andric static AtomicOrdering getDecodedOrdering(unsigned Val) { 1251*0b57cec5SDimitry Andric switch (Val) { 1252*0b57cec5SDimitry Andric case bitc::ORDERING_NOTATOMIC: return AtomicOrdering::NotAtomic; 1253*0b57cec5SDimitry Andric case bitc::ORDERING_UNORDERED: return AtomicOrdering::Unordered; 1254*0b57cec5SDimitry Andric case bitc::ORDERING_MONOTONIC: return AtomicOrdering::Monotonic; 1255*0b57cec5SDimitry Andric case bitc::ORDERING_ACQUIRE: return AtomicOrdering::Acquire; 1256*0b57cec5SDimitry Andric case bitc::ORDERING_RELEASE: return AtomicOrdering::Release; 1257*0b57cec5SDimitry Andric case bitc::ORDERING_ACQREL: return AtomicOrdering::AcquireRelease; 1258*0b57cec5SDimitry Andric default: // Map unknown orderings to sequentially-consistent. 1259*0b57cec5SDimitry Andric case bitc::ORDERING_SEQCST: return AtomicOrdering::SequentiallyConsistent; 1260*0b57cec5SDimitry Andric } 1261*0b57cec5SDimitry Andric } 1262*0b57cec5SDimitry Andric 1263*0b57cec5SDimitry Andric static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) { 1264*0b57cec5SDimitry Andric switch (Val) { 1265*0b57cec5SDimitry Andric default: // Map unknown selection kinds to any. 1266*0b57cec5SDimitry Andric case bitc::COMDAT_SELECTION_KIND_ANY: 1267*0b57cec5SDimitry Andric return Comdat::Any; 1268*0b57cec5SDimitry Andric case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH: 1269*0b57cec5SDimitry Andric return Comdat::ExactMatch; 1270*0b57cec5SDimitry Andric case bitc::COMDAT_SELECTION_KIND_LARGEST: 1271*0b57cec5SDimitry Andric return Comdat::Largest; 1272*0b57cec5SDimitry Andric case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES: 1273fe6060f1SDimitry Andric return Comdat::NoDeduplicate; 1274*0b57cec5SDimitry Andric case bitc::COMDAT_SELECTION_KIND_SAME_SIZE: 1275*0b57cec5SDimitry Andric return Comdat::SameSize; 1276*0b57cec5SDimitry Andric } 1277*0b57cec5SDimitry Andric } 1278*0b57cec5SDimitry Andric 1279*0b57cec5SDimitry Andric static FastMathFlags getDecodedFastMathFlags(unsigned Val) { 1280*0b57cec5SDimitry Andric FastMathFlags FMF; 1281*0b57cec5SDimitry Andric if (0 != (Val & bitc::UnsafeAlgebra)) 1282*0b57cec5SDimitry Andric FMF.setFast(); 1283*0b57cec5SDimitry Andric if (0 != (Val & bitc::AllowReassoc)) 1284*0b57cec5SDimitry Andric FMF.setAllowReassoc(); 1285*0b57cec5SDimitry Andric if (0 != (Val & bitc::NoNaNs)) 1286*0b57cec5SDimitry Andric FMF.setNoNaNs(); 1287*0b57cec5SDimitry Andric if (0 != (Val & bitc::NoInfs)) 1288*0b57cec5SDimitry Andric FMF.setNoInfs(); 1289*0b57cec5SDimitry Andric if (0 != (Val & bitc::NoSignedZeros)) 1290*0b57cec5SDimitry Andric FMF.setNoSignedZeros(); 1291*0b57cec5SDimitry Andric if (0 != (Val & bitc::AllowReciprocal)) 1292*0b57cec5SDimitry Andric FMF.setAllowReciprocal(); 1293*0b57cec5SDimitry Andric if (0 != (Val & bitc::AllowContract)) 1294*0b57cec5SDimitry Andric FMF.setAllowContract(true); 1295*0b57cec5SDimitry Andric if (0 != (Val & bitc::ApproxFunc)) 1296*0b57cec5SDimitry Andric FMF.setApproxFunc(); 1297*0b57cec5SDimitry Andric return FMF; 1298*0b57cec5SDimitry Andric } 1299*0b57cec5SDimitry Andric 1300*0b57cec5SDimitry Andric static void upgradeDLLImportExportLinkage(GlobalValue *GV, unsigned Val) { 1301*0b57cec5SDimitry Andric switch (Val) { 1302*0b57cec5SDimitry Andric case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break; 1303*0b57cec5SDimitry Andric case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break; 1304*0b57cec5SDimitry Andric } 1305*0b57cec5SDimitry Andric } 1306*0b57cec5SDimitry Andric 1307fe6060f1SDimitry Andric Type *BitcodeReader::getTypeByID(unsigned ID) { 1308*0b57cec5SDimitry Andric // The type table size is always specified correctly. 1309*0b57cec5SDimitry Andric if (ID >= TypeList.size()) 1310*0b57cec5SDimitry Andric return nullptr; 1311*0b57cec5SDimitry Andric 1312*0b57cec5SDimitry Andric if (Type *Ty = TypeList[ID]) 1313*0b57cec5SDimitry Andric return Ty; 1314*0b57cec5SDimitry Andric 1315*0b57cec5SDimitry Andric // If we have a forward reference, the only possible case is when it is to a 1316*0b57cec5SDimitry Andric // named struct. Just create a placeholder for now. 1317*0b57cec5SDimitry Andric return TypeList[ID] = createIdentifiedStructType(Context); 1318*0b57cec5SDimitry Andric } 1319*0b57cec5SDimitry Andric 132081ad6265SDimitry Andric unsigned BitcodeReader::getContainedTypeID(unsigned ID, unsigned Idx) { 132181ad6265SDimitry Andric auto It = ContainedTypeIDs.find(ID); 132281ad6265SDimitry Andric if (It == ContainedTypeIDs.end()) 132381ad6265SDimitry Andric return InvalidTypeID; 132481ad6265SDimitry Andric 132581ad6265SDimitry Andric if (Idx >= It->second.size()) 132681ad6265SDimitry Andric return InvalidTypeID; 132781ad6265SDimitry Andric 132881ad6265SDimitry Andric return It->second[Idx]; 132981ad6265SDimitry Andric } 133081ad6265SDimitry Andric 133181ad6265SDimitry Andric Type *BitcodeReader::getPtrElementTypeByID(unsigned ID) { 133281ad6265SDimitry Andric if (ID >= TypeList.size()) 133381ad6265SDimitry Andric return nullptr; 133481ad6265SDimitry Andric 133581ad6265SDimitry Andric Type *Ty = TypeList[ID]; 133681ad6265SDimitry Andric if (!Ty->isPointerTy()) 133781ad6265SDimitry Andric return nullptr; 133881ad6265SDimitry Andric 133981ad6265SDimitry Andric Type *ElemTy = getTypeByID(getContainedTypeID(ID, 0)); 134081ad6265SDimitry Andric if (!ElemTy) 134181ad6265SDimitry Andric return nullptr; 134281ad6265SDimitry Andric 134381ad6265SDimitry Andric assert(cast<PointerType>(Ty)->isOpaqueOrPointeeTypeMatches(ElemTy) && 134481ad6265SDimitry Andric "Incorrect element type"); 134581ad6265SDimitry Andric return ElemTy; 134681ad6265SDimitry Andric } 134781ad6265SDimitry Andric 134881ad6265SDimitry Andric unsigned BitcodeReader::getVirtualTypeID(Type *Ty, 134981ad6265SDimitry Andric ArrayRef<unsigned> ChildTypeIDs) { 135081ad6265SDimitry Andric unsigned ChildTypeID = ChildTypeIDs.empty() ? InvalidTypeID : ChildTypeIDs[0]; 135181ad6265SDimitry Andric auto CacheKey = std::make_pair(Ty, ChildTypeID); 135281ad6265SDimitry Andric auto It = VirtualTypeIDs.find(CacheKey); 135381ad6265SDimitry Andric if (It != VirtualTypeIDs.end()) { 135481ad6265SDimitry Andric // The cmpxchg return value is the only place we need more than one 135581ad6265SDimitry Andric // contained type ID, however the second one will always be the same (i1), 135681ad6265SDimitry Andric // so we don't need to include it in the cache key. This asserts that the 135781ad6265SDimitry Andric // contained types are indeed as expected and there are no collisions. 135881ad6265SDimitry Andric assert((ChildTypeIDs.empty() || 135981ad6265SDimitry Andric ContainedTypeIDs[It->second] == ChildTypeIDs) && 136081ad6265SDimitry Andric "Incorrect cached contained type IDs"); 136181ad6265SDimitry Andric return It->second; 136281ad6265SDimitry Andric } 136381ad6265SDimitry Andric 136481ad6265SDimitry Andric #ifndef NDEBUG 136581ad6265SDimitry Andric if (!Ty->isOpaquePointerTy()) { 136681ad6265SDimitry Andric assert(Ty->getNumContainedTypes() == ChildTypeIDs.size() && 136781ad6265SDimitry Andric "Wrong number of contained types"); 136881ad6265SDimitry Andric for (auto Pair : zip(Ty->subtypes(), ChildTypeIDs)) { 136981ad6265SDimitry Andric assert(std::get<0>(Pair) == getTypeByID(std::get<1>(Pair)) && 137081ad6265SDimitry Andric "Incorrect contained type ID"); 137181ad6265SDimitry Andric } 137281ad6265SDimitry Andric } 137381ad6265SDimitry Andric #endif 137481ad6265SDimitry Andric 137581ad6265SDimitry Andric unsigned TypeID = TypeList.size(); 137681ad6265SDimitry Andric TypeList.push_back(Ty); 137781ad6265SDimitry Andric if (!ChildTypeIDs.empty()) 137881ad6265SDimitry Andric append_range(ContainedTypeIDs[TypeID], ChildTypeIDs); 137981ad6265SDimitry Andric VirtualTypeIDs.insert({CacheKey, TypeID}); 138081ad6265SDimitry Andric return TypeID; 138181ad6265SDimitry Andric } 138281ad6265SDimitry Andric 138381ad6265SDimitry Andric static bool isConstExprSupported(uint8_t Opcode) { 138481ad6265SDimitry Andric // These are not real constant expressions, always consider them supported. 138581ad6265SDimitry Andric if (Opcode >= BitcodeConstant::FirstSpecialOpcode) 138681ad6265SDimitry Andric return true; 138781ad6265SDimitry Andric 1388753f127fSDimitry Andric if (Instruction::isBinaryOp(Opcode)) 1389753f127fSDimitry Andric return ConstantExpr::isSupportedBinOp(Opcode); 1390753f127fSDimitry Andric 139181ad6265SDimitry Andric return !ExpandConstantExprs; 139281ad6265SDimitry Andric } 139381ad6265SDimitry Andric 139481ad6265SDimitry Andric Expected<Value *> BitcodeReader::materializeValue(unsigned StartValID, 139581ad6265SDimitry Andric BasicBlock *InsertBB) { 139681ad6265SDimitry Andric // Quickly handle the case where there is no BitcodeConstant to resolve. 139781ad6265SDimitry Andric if (StartValID < ValueList.size() && ValueList[StartValID] && 139881ad6265SDimitry Andric !isa<BitcodeConstant>(ValueList[StartValID])) 139981ad6265SDimitry Andric return ValueList[StartValID]; 140081ad6265SDimitry Andric 140181ad6265SDimitry Andric SmallDenseMap<unsigned, Value *> MaterializedValues; 140281ad6265SDimitry Andric SmallVector<unsigned> Worklist; 140381ad6265SDimitry Andric Worklist.push_back(StartValID); 140481ad6265SDimitry Andric while (!Worklist.empty()) { 140581ad6265SDimitry Andric unsigned ValID = Worklist.back(); 140681ad6265SDimitry Andric if (MaterializedValues.count(ValID)) { 140781ad6265SDimitry Andric // Duplicate expression that was already handled. 140881ad6265SDimitry Andric Worklist.pop_back(); 140981ad6265SDimitry Andric continue; 141081ad6265SDimitry Andric } 141181ad6265SDimitry Andric 141281ad6265SDimitry Andric if (ValID >= ValueList.size() || !ValueList[ValID]) 141381ad6265SDimitry Andric return error("Invalid value ID"); 141481ad6265SDimitry Andric 141581ad6265SDimitry Andric Value *V = ValueList[ValID]; 141681ad6265SDimitry Andric auto *BC = dyn_cast<BitcodeConstant>(V); 141781ad6265SDimitry Andric if (!BC) { 141881ad6265SDimitry Andric MaterializedValues.insert({ValID, V}); 141981ad6265SDimitry Andric Worklist.pop_back(); 142081ad6265SDimitry Andric continue; 142181ad6265SDimitry Andric } 142281ad6265SDimitry Andric 142381ad6265SDimitry Andric // Iterate in reverse, so values will get popped from the worklist in 142481ad6265SDimitry Andric // expected order. 142581ad6265SDimitry Andric SmallVector<Value *> Ops; 142681ad6265SDimitry Andric for (unsigned OpID : reverse(BC->getOperandIDs())) { 142781ad6265SDimitry Andric auto It = MaterializedValues.find(OpID); 142881ad6265SDimitry Andric if (It != MaterializedValues.end()) 142981ad6265SDimitry Andric Ops.push_back(It->second); 143081ad6265SDimitry Andric else 143181ad6265SDimitry Andric Worklist.push_back(OpID); 143281ad6265SDimitry Andric } 143381ad6265SDimitry Andric 143481ad6265SDimitry Andric // Some expressions have not been resolved yet, handle them first and then 143581ad6265SDimitry Andric // revisit this one. 143681ad6265SDimitry Andric if (Ops.size() != BC->getOperandIDs().size()) 143781ad6265SDimitry Andric continue; 143881ad6265SDimitry Andric std::reverse(Ops.begin(), Ops.end()); 143981ad6265SDimitry Andric 144081ad6265SDimitry Andric SmallVector<Constant *> ConstOps; 144181ad6265SDimitry Andric for (Value *Op : Ops) 144281ad6265SDimitry Andric if (auto *C = dyn_cast<Constant>(Op)) 144381ad6265SDimitry Andric ConstOps.push_back(C); 144481ad6265SDimitry Andric 144581ad6265SDimitry Andric // Materialize as constant expression if possible. 144681ad6265SDimitry Andric if (isConstExprSupported(BC->Opcode) && ConstOps.size() == Ops.size()) { 144781ad6265SDimitry Andric Constant *C; 144881ad6265SDimitry Andric if (Instruction::isCast(BC->Opcode)) { 144981ad6265SDimitry Andric C = UpgradeBitCastExpr(BC->Opcode, ConstOps[0], BC->getType()); 145081ad6265SDimitry Andric if (!C) 145181ad6265SDimitry Andric C = ConstantExpr::getCast(BC->Opcode, ConstOps[0], BC->getType()); 145281ad6265SDimitry Andric } else if (Instruction::isUnaryOp(BC->Opcode)) { 145381ad6265SDimitry Andric C = ConstantExpr::get(BC->Opcode, ConstOps[0], BC->Flags); 145481ad6265SDimitry Andric } else if (Instruction::isBinaryOp(BC->Opcode)) { 145581ad6265SDimitry Andric C = ConstantExpr::get(BC->Opcode, ConstOps[0], ConstOps[1], BC->Flags); 145681ad6265SDimitry Andric } else { 145781ad6265SDimitry Andric switch (BC->Opcode) { 145881ad6265SDimitry Andric case BitcodeConstant::NoCFIOpcode: { 145981ad6265SDimitry Andric auto *GV = dyn_cast<GlobalValue>(ConstOps[0]); 146081ad6265SDimitry Andric if (!GV) 146181ad6265SDimitry Andric return error("no_cfi operand must be GlobalValue"); 146281ad6265SDimitry Andric C = NoCFIValue::get(GV); 146381ad6265SDimitry Andric break; 146481ad6265SDimitry Andric } 146581ad6265SDimitry Andric case BitcodeConstant::DSOLocalEquivalentOpcode: { 146681ad6265SDimitry Andric auto *GV = dyn_cast<GlobalValue>(ConstOps[0]); 146781ad6265SDimitry Andric if (!GV) 146881ad6265SDimitry Andric return error("dso_local operand must be GlobalValue"); 146981ad6265SDimitry Andric C = DSOLocalEquivalent::get(GV); 147081ad6265SDimitry Andric break; 147181ad6265SDimitry Andric } 147281ad6265SDimitry Andric case BitcodeConstant::BlockAddressOpcode: { 147381ad6265SDimitry Andric Function *Fn = dyn_cast<Function>(ConstOps[0]); 147481ad6265SDimitry Andric if (!Fn) 147581ad6265SDimitry Andric return error("blockaddress operand must be a function"); 147681ad6265SDimitry Andric 147781ad6265SDimitry Andric // If the function is already parsed we can insert the block address 147881ad6265SDimitry Andric // right away. 147981ad6265SDimitry Andric BasicBlock *BB; 148081ad6265SDimitry Andric unsigned BBID = BC->Extra; 148181ad6265SDimitry Andric if (!BBID) 148281ad6265SDimitry Andric // Invalid reference to entry block. 148381ad6265SDimitry Andric return error("Invalid ID"); 148481ad6265SDimitry Andric if (!Fn->empty()) { 148581ad6265SDimitry Andric Function::iterator BBI = Fn->begin(), BBE = Fn->end(); 148681ad6265SDimitry Andric for (size_t I = 0, E = BBID; I != E; ++I) { 148781ad6265SDimitry Andric if (BBI == BBE) 148881ad6265SDimitry Andric return error("Invalid ID"); 148981ad6265SDimitry Andric ++BBI; 149081ad6265SDimitry Andric } 149181ad6265SDimitry Andric BB = &*BBI; 149281ad6265SDimitry Andric } else { 149381ad6265SDimitry Andric // Otherwise insert a placeholder and remember it so it can be 149481ad6265SDimitry Andric // inserted when the function is parsed. 149581ad6265SDimitry Andric auto &FwdBBs = BasicBlockFwdRefs[Fn]; 149681ad6265SDimitry Andric if (FwdBBs.empty()) 149781ad6265SDimitry Andric BasicBlockFwdRefQueue.push_back(Fn); 149881ad6265SDimitry Andric if (FwdBBs.size() < BBID + 1) 149981ad6265SDimitry Andric FwdBBs.resize(BBID + 1); 150081ad6265SDimitry Andric if (!FwdBBs[BBID]) 150181ad6265SDimitry Andric FwdBBs[BBID] = BasicBlock::Create(Context); 150281ad6265SDimitry Andric BB = FwdBBs[BBID]; 150381ad6265SDimitry Andric } 150481ad6265SDimitry Andric C = BlockAddress::get(Fn, BB); 150581ad6265SDimitry Andric break; 150681ad6265SDimitry Andric } 150781ad6265SDimitry Andric case BitcodeConstant::ConstantStructOpcode: 150881ad6265SDimitry Andric C = ConstantStruct::get(cast<StructType>(BC->getType()), ConstOps); 150981ad6265SDimitry Andric break; 151081ad6265SDimitry Andric case BitcodeConstant::ConstantArrayOpcode: 151181ad6265SDimitry Andric C = ConstantArray::get(cast<ArrayType>(BC->getType()), ConstOps); 151281ad6265SDimitry Andric break; 151381ad6265SDimitry Andric case BitcodeConstant::ConstantVectorOpcode: 151481ad6265SDimitry Andric C = ConstantVector::get(ConstOps); 151581ad6265SDimitry Andric break; 151681ad6265SDimitry Andric case Instruction::ICmp: 151781ad6265SDimitry Andric case Instruction::FCmp: 151881ad6265SDimitry Andric C = ConstantExpr::getCompare(BC->Flags, ConstOps[0], ConstOps[1]); 151981ad6265SDimitry Andric break; 152081ad6265SDimitry Andric case Instruction::GetElementPtr: 152181ad6265SDimitry Andric C = ConstantExpr::getGetElementPtr( 152281ad6265SDimitry Andric BC->SrcElemTy, ConstOps[0], makeArrayRef(ConstOps).drop_front(), 152381ad6265SDimitry Andric BC->Flags, BC->getInRangeIndex()); 152481ad6265SDimitry Andric break; 152581ad6265SDimitry Andric case Instruction::Select: 152681ad6265SDimitry Andric C = ConstantExpr::getSelect(ConstOps[0], ConstOps[1], ConstOps[2]); 152781ad6265SDimitry Andric break; 152881ad6265SDimitry Andric case Instruction::ExtractElement: 152981ad6265SDimitry Andric C = ConstantExpr::getExtractElement(ConstOps[0], ConstOps[1]); 153081ad6265SDimitry Andric break; 153181ad6265SDimitry Andric case Instruction::InsertElement: 153281ad6265SDimitry Andric C = ConstantExpr::getInsertElement(ConstOps[0], ConstOps[1], 153381ad6265SDimitry Andric ConstOps[2]); 153481ad6265SDimitry Andric break; 153581ad6265SDimitry Andric case Instruction::ShuffleVector: { 153681ad6265SDimitry Andric SmallVector<int, 16> Mask; 153781ad6265SDimitry Andric ShuffleVectorInst::getShuffleMask(ConstOps[2], Mask); 153881ad6265SDimitry Andric C = ConstantExpr::getShuffleVector(ConstOps[0], ConstOps[1], Mask); 153981ad6265SDimitry Andric break; 154081ad6265SDimitry Andric } 154181ad6265SDimitry Andric default: 154281ad6265SDimitry Andric llvm_unreachable("Unhandled bitcode constant"); 154381ad6265SDimitry Andric } 154481ad6265SDimitry Andric } 154581ad6265SDimitry Andric 154681ad6265SDimitry Andric // Cache resolved constant. 154781ad6265SDimitry Andric ValueList.replaceValueWithoutRAUW(ValID, C); 154881ad6265SDimitry Andric MaterializedValues.insert({ValID, C}); 154981ad6265SDimitry Andric Worklist.pop_back(); 155081ad6265SDimitry Andric continue; 155181ad6265SDimitry Andric } 155281ad6265SDimitry Andric 155381ad6265SDimitry Andric if (!InsertBB) 155481ad6265SDimitry Andric return error(Twine("Value referenced by initializer is an unsupported " 155581ad6265SDimitry Andric "constant expression of type ") + 155681ad6265SDimitry Andric BC->getOpcodeName()); 155781ad6265SDimitry Andric 155881ad6265SDimitry Andric // Materialize as instructions if necessary. 155981ad6265SDimitry Andric Instruction *I; 156081ad6265SDimitry Andric if (Instruction::isCast(BC->Opcode)) { 156181ad6265SDimitry Andric I = CastInst::Create((Instruction::CastOps)BC->Opcode, Ops[0], 156281ad6265SDimitry Andric BC->getType(), "constexpr", InsertBB); 156381ad6265SDimitry Andric } else if (Instruction::isUnaryOp(BC->Opcode)) { 156481ad6265SDimitry Andric I = UnaryOperator::Create((Instruction::UnaryOps)BC->Opcode, Ops[0], 156581ad6265SDimitry Andric "constexpr", InsertBB); 156681ad6265SDimitry Andric } else if (Instruction::isBinaryOp(BC->Opcode)) { 156781ad6265SDimitry Andric I = BinaryOperator::Create((Instruction::BinaryOps)BC->Opcode, Ops[0], 156881ad6265SDimitry Andric Ops[1], "constexpr", InsertBB); 156981ad6265SDimitry Andric if (isa<OverflowingBinaryOperator>(I)) { 157081ad6265SDimitry Andric if (BC->Flags & OverflowingBinaryOperator::NoSignedWrap) 157181ad6265SDimitry Andric I->setHasNoSignedWrap(); 157281ad6265SDimitry Andric if (BC->Flags & OverflowingBinaryOperator::NoUnsignedWrap) 157381ad6265SDimitry Andric I->setHasNoUnsignedWrap(); 157481ad6265SDimitry Andric } 157581ad6265SDimitry Andric if (isa<PossiblyExactOperator>(I) && 157681ad6265SDimitry Andric (BC->Flags & PossiblyExactOperator::IsExact)) 157781ad6265SDimitry Andric I->setIsExact(); 157881ad6265SDimitry Andric } else { 157981ad6265SDimitry Andric switch (BC->Opcode) { 158081ad6265SDimitry Andric case BitcodeConstant::ConstantStructOpcode: 158181ad6265SDimitry Andric case BitcodeConstant::ConstantArrayOpcode: 158281ad6265SDimitry Andric case BitcodeConstant::ConstantVectorOpcode: { 158381ad6265SDimitry Andric Type *IdxTy = Type::getInt32Ty(BC->getContext()); 158481ad6265SDimitry Andric Value *V = PoisonValue::get(BC->getType()); 158581ad6265SDimitry Andric for (auto Pair : enumerate(Ops)) { 158681ad6265SDimitry Andric Value *Idx = ConstantInt::get(IdxTy, Pair.index()); 158781ad6265SDimitry Andric V = InsertElementInst::Create(V, Pair.value(), Idx, "constexpr.ins", 158881ad6265SDimitry Andric InsertBB); 158981ad6265SDimitry Andric } 159081ad6265SDimitry Andric I = cast<Instruction>(V); 159181ad6265SDimitry Andric break; 159281ad6265SDimitry Andric } 159381ad6265SDimitry Andric case Instruction::ICmp: 159481ad6265SDimitry Andric case Instruction::FCmp: 159581ad6265SDimitry Andric I = CmpInst::Create((Instruction::OtherOps)BC->Opcode, 159681ad6265SDimitry Andric (CmpInst::Predicate)BC->Flags, Ops[0], Ops[1], 159781ad6265SDimitry Andric "constexpr", InsertBB); 159881ad6265SDimitry Andric break; 159981ad6265SDimitry Andric case Instruction::GetElementPtr: 160081ad6265SDimitry Andric I = GetElementPtrInst::Create(BC->SrcElemTy, Ops[0], 160181ad6265SDimitry Andric makeArrayRef(Ops).drop_front(), 160281ad6265SDimitry Andric "constexpr", InsertBB); 160381ad6265SDimitry Andric if (BC->Flags) 160481ad6265SDimitry Andric cast<GetElementPtrInst>(I)->setIsInBounds(); 160581ad6265SDimitry Andric break; 160681ad6265SDimitry Andric case Instruction::Select: 160781ad6265SDimitry Andric I = SelectInst::Create(Ops[0], Ops[1], Ops[2], "constexpr", InsertBB); 160881ad6265SDimitry Andric break; 160981ad6265SDimitry Andric case Instruction::ExtractElement: 161081ad6265SDimitry Andric I = ExtractElementInst::Create(Ops[0], Ops[1], "constexpr", InsertBB); 161181ad6265SDimitry Andric break; 161281ad6265SDimitry Andric case Instruction::InsertElement: 161381ad6265SDimitry Andric I = InsertElementInst::Create(Ops[0], Ops[1], Ops[2], "constexpr", 161481ad6265SDimitry Andric InsertBB); 161581ad6265SDimitry Andric break; 161681ad6265SDimitry Andric case Instruction::ShuffleVector: 161781ad6265SDimitry Andric I = new ShuffleVectorInst(Ops[0], Ops[1], Ops[2], "constexpr", 161881ad6265SDimitry Andric InsertBB); 161981ad6265SDimitry Andric break; 162081ad6265SDimitry Andric default: 162181ad6265SDimitry Andric llvm_unreachable("Unhandled bitcode constant"); 162281ad6265SDimitry Andric } 162381ad6265SDimitry Andric } 162481ad6265SDimitry Andric 162581ad6265SDimitry Andric MaterializedValues.insert({ValID, I}); 162681ad6265SDimitry Andric Worklist.pop_back(); 162781ad6265SDimitry Andric } 162881ad6265SDimitry Andric 162981ad6265SDimitry Andric return MaterializedValues[StartValID]; 163081ad6265SDimitry Andric } 163181ad6265SDimitry Andric 163281ad6265SDimitry Andric Expected<Constant *> BitcodeReader::getValueForInitializer(unsigned ID) { 163381ad6265SDimitry Andric Expected<Value *> MaybeV = materializeValue(ID, /* InsertBB */ nullptr); 163481ad6265SDimitry Andric if (!MaybeV) 163581ad6265SDimitry Andric return MaybeV.takeError(); 163681ad6265SDimitry Andric 163781ad6265SDimitry Andric // Result must be Constant if InsertBB is nullptr. 163881ad6265SDimitry Andric return cast<Constant>(MaybeV.get()); 163981ad6265SDimitry Andric } 164081ad6265SDimitry Andric 1641*0b57cec5SDimitry Andric StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context, 1642*0b57cec5SDimitry Andric StringRef Name) { 1643*0b57cec5SDimitry Andric auto *Ret = StructType::create(Context, Name); 1644*0b57cec5SDimitry Andric IdentifiedStructTypes.push_back(Ret); 1645*0b57cec5SDimitry Andric return Ret; 1646*0b57cec5SDimitry Andric } 1647*0b57cec5SDimitry Andric 1648*0b57cec5SDimitry Andric StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) { 1649*0b57cec5SDimitry Andric auto *Ret = StructType::create(Context); 1650*0b57cec5SDimitry Andric IdentifiedStructTypes.push_back(Ret); 1651*0b57cec5SDimitry Andric return Ret; 1652*0b57cec5SDimitry Andric } 1653*0b57cec5SDimitry Andric 1654*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1655*0b57cec5SDimitry Andric // Functions for parsing blocks from the bitcode file 1656*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1657*0b57cec5SDimitry Andric 1658*0b57cec5SDimitry Andric static uint64_t getRawAttributeMask(Attribute::AttrKind Val) { 1659*0b57cec5SDimitry Andric switch (Val) { 1660*0b57cec5SDimitry Andric case Attribute::EndAttrKinds: 16615ffd83dbSDimitry Andric case Attribute::EmptyKey: 16625ffd83dbSDimitry Andric case Attribute::TombstoneKey: 1663*0b57cec5SDimitry Andric llvm_unreachable("Synthetic enumerators which should never get here"); 1664*0b57cec5SDimitry Andric 1665*0b57cec5SDimitry Andric case Attribute::None: return 0; 1666*0b57cec5SDimitry Andric case Attribute::ZExt: return 1 << 0; 1667*0b57cec5SDimitry Andric case Attribute::SExt: return 1 << 1; 1668*0b57cec5SDimitry Andric case Attribute::NoReturn: return 1 << 2; 1669*0b57cec5SDimitry Andric case Attribute::InReg: return 1 << 3; 1670*0b57cec5SDimitry Andric case Attribute::StructRet: return 1 << 4; 1671*0b57cec5SDimitry Andric case Attribute::NoUnwind: return 1 << 5; 1672*0b57cec5SDimitry Andric case Attribute::NoAlias: return 1 << 6; 1673*0b57cec5SDimitry Andric case Attribute::ByVal: return 1 << 7; 1674*0b57cec5SDimitry Andric case Attribute::Nest: return 1 << 8; 1675*0b57cec5SDimitry Andric case Attribute::ReadNone: return 1 << 9; 1676*0b57cec5SDimitry Andric case Attribute::ReadOnly: return 1 << 10; 1677*0b57cec5SDimitry Andric case Attribute::NoInline: return 1 << 11; 1678*0b57cec5SDimitry Andric case Attribute::AlwaysInline: return 1 << 12; 1679*0b57cec5SDimitry Andric case Attribute::OptimizeForSize: return 1 << 13; 1680*0b57cec5SDimitry Andric case Attribute::StackProtect: return 1 << 14; 1681*0b57cec5SDimitry Andric case Attribute::StackProtectReq: return 1 << 15; 1682*0b57cec5SDimitry Andric case Attribute::Alignment: return 31 << 16; 1683*0b57cec5SDimitry Andric case Attribute::NoCapture: return 1 << 21; 1684*0b57cec5SDimitry Andric case Attribute::NoRedZone: return 1 << 22; 1685*0b57cec5SDimitry Andric case Attribute::NoImplicitFloat: return 1 << 23; 1686*0b57cec5SDimitry Andric case Attribute::Naked: return 1 << 24; 1687*0b57cec5SDimitry Andric case Attribute::InlineHint: return 1 << 25; 1688*0b57cec5SDimitry Andric case Attribute::StackAlignment: return 7 << 26; 1689*0b57cec5SDimitry Andric case Attribute::ReturnsTwice: return 1 << 29; 1690*0b57cec5SDimitry Andric case Attribute::UWTable: return 1 << 30; 1691*0b57cec5SDimitry Andric case Attribute::NonLazyBind: return 1U << 31; 1692*0b57cec5SDimitry Andric case Attribute::SanitizeAddress: return 1ULL << 32; 1693*0b57cec5SDimitry Andric case Attribute::MinSize: return 1ULL << 33; 1694*0b57cec5SDimitry Andric case Attribute::NoDuplicate: return 1ULL << 34; 1695*0b57cec5SDimitry Andric case Attribute::StackProtectStrong: return 1ULL << 35; 1696*0b57cec5SDimitry Andric case Attribute::SanitizeThread: return 1ULL << 36; 1697*0b57cec5SDimitry Andric case Attribute::SanitizeMemory: return 1ULL << 37; 1698*0b57cec5SDimitry Andric case Attribute::NoBuiltin: return 1ULL << 38; 1699*0b57cec5SDimitry Andric case Attribute::Returned: return 1ULL << 39; 1700*0b57cec5SDimitry Andric case Attribute::Cold: return 1ULL << 40; 1701*0b57cec5SDimitry Andric case Attribute::Builtin: return 1ULL << 41; 1702*0b57cec5SDimitry Andric case Attribute::OptimizeNone: return 1ULL << 42; 1703*0b57cec5SDimitry Andric case Attribute::InAlloca: return 1ULL << 43; 1704*0b57cec5SDimitry Andric case Attribute::NonNull: return 1ULL << 44; 1705*0b57cec5SDimitry Andric case Attribute::JumpTable: return 1ULL << 45; 1706*0b57cec5SDimitry Andric case Attribute::Convergent: return 1ULL << 46; 1707*0b57cec5SDimitry Andric case Attribute::SafeStack: return 1ULL << 47; 1708*0b57cec5SDimitry Andric case Attribute::NoRecurse: return 1ULL << 48; 1709*0b57cec5SDimitry Andric case Attribute::InaccessibleMemOnly: return 1ULL << 49; 1710*0b57cec5SDimitry Andric case Attribute::InaccessibleMemOrArgMemOnly: return 1ULL << 50; 1711*0b57cec5SDimitry Andric case Attribute::SwiftSelf: return 1ULL << 51; 1712*0b57cec5SDimitry Andric case Attribute::SwiftError: return 1ULL << 52; 1713*0b57cec5SDimitry Andric case Attribute::WriteOnly: return 1ULL << 53; 1714*0b57cec5SDimitry Andric case Attribute::Speculatable: return 1ULL << 54; 1715*0b57cec5SDimitry Andric case Attribute::StrictFP: return 1ULL << 55; 1716*0b57cec5SDimitry Andric case Attribute::SanitizeHWAddress: return 1ULL << 56; 1717*0b57cec5SDimitry Andric case Attribute::NoCfCheck: return 1ULL << 57; 1718*0b57cec5SDimitry Andric case Attribute::OptForFuzzing: return 1ULL << 58; 1719*0b57cec5SDimitry Andric case Attribute::ShadowCallStack: return 1ULL << 59; 1720*0b57cec5SDimitry Andric case Attribute::SpeculativeLoadHardening: 1721*0b57cec5SDimitry Andric return 1ULL << 60; 1722*0b57cec5SDimitry Andric case Attribute::ImmArg: 1723*0b57cec5SDimitry Andric return 1ULL << 61; 1724*0b57cec5SDimitry Andric case Attribute::WillReturn: 1725*0b57cec5SDimitry Andric return 1ULL << 62; 1726*0b57cec5SDimitry Andric case Attribute::NoFree: 1727*0b57cec5SDimitry Andric return 1ULL << 63; 17285ffd83dbSDimitry Andric default: 17295ffd83dbSDimitry Andric // Other attributes are not supported in the raw format, 17305ffd83dbSDimitry Andric // as we ran out of space. 17315ffd83dbSDimitry Andric return 0; 1732*0b57cec5SDimitry Andric } 1733*0b57cec5SDimitry Andric llvm_unreachable("Unsupported attribute type"); 1734*0b57cec5SDimitry Andric } 1735*0b57cec5SDimitry Andric 1736*0b57cec5SDimitry Andric static void addRawAttributeValue(AttrBuilder &B, uint64_t Val) { 1737*0b57cec5SDimitry Andric if (!Val) return; 1738*0b57cec5SDimitry Andric 1739*0b57cec5SDimitry Andric for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds; 1740*0b57cec5SDimitry Andric I = Attribute::AttrKind(I + 1)) { 1741*0b57cec5SDimitry Andric if (uint64_t A = (Val & getRawAttributeMask(I))) { 1742*0b57cec5SDimitry Andric if (I == Attribute::Alignment) 1743*0b57cec5SDimitry Andric B.addAlignmentAttr(1ULL << ((A >> 16) - 1)); 1744*0b57cec5SDimitry Andric else if (I == Attribute::StackAlignment) 1745*0b57cec5SDimitry Andric B.addStackAlignmentAttr(1ULL << ((A >> 26)-1)); 1746fe6060f1SDimitry Andric else if (Attribute::isTypeAttrKind(I)) 1747fe6060f1SDimitry Andric B.addTypeAttr(I, nullptr); // Type will be auto-upgraded. 1748*0b57cec5SDimitry Andric else 1749*0b57cec5SDimitry Andric B.addAttribute(I); 1750*0b57cec5SDimitry Andric } 1751*0b57cec5SDimitry Andric } 1752*0b57cec5SDimitry Andric } 1753*0b57cec5SDimitry Andric 1754*0b57cec5SDimitry Andric /// This fills an AttrBuilder object with the LLVM attributes that have 1755*0b57cec5SDimitry Andric /// been decoded from the given integer. This function must stay in sync with 1756*0b57cec5SDimitry Andric /// 'encodeLLVMAttributesForBitcode'. 1757*0b57cec5SDimitry Andric static void decodeLLVMAttributesForBitcode(AttrBuilder &B, 1758*0b57cec5SDimitry Andric uint64_t EncodedAttrs) { 1759*0b57cec5SDimitry Andric // The alignment is stored as a 16-bit raw value from bits 31--16. We shift 1760*0b57cec5SDimitry Andric // the bits above 31 down by 11 bits. 1761*0b57cec5SDimitry Andric unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16; 1762*0b57cec5SDimitry Andric assert((!Alignment || isPowerOf2_32(Alignment)) && 1763*0b57cec5SDimitry Andric "Alignment must be a power of two."); 1764*0b57cec5SDimitry Andric 1765*0b57cec5SDimitry Andric if (Alignment) 1766*0b57cec5SDimitry Andric B.addAlignmentAttr(Alignment); 1767*0b57cec5SDimitry Andric addRawAttributeValue(B, ((EncodedAttrs & (0xfffffULL << 32)) >> 11) | 1768*0b57cec5SDimitry Andric (EncodedAttrs & 0xffff)); 1769*0b57cec5SDimitry Andric } 1770*0b57cec5SDimitry Andric 1771*0b57cec5SDimitry Andric Error BitcodeReader::parseAttributeBlock() { 1772*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID)) 1773*0b57cec5SDimitry Andric return Err; 1774*0b57cec5SDimitry Andric 1775*0b57cec5SDimitry Andric if (!MAttributes.empty()) 1776*0b57cec5SDimitry Andric return error("Invalid multiple blocks"); 1777*0b57cec5SDimitry Andric 1778*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 1779*0b57cec5SDimitry Andric 1780*0b57cec5SDimitry Andric SmallVector<AttributeList, 8> Attrs; 1781*0b57cec5SDimitry Andric 1782*0b57cec5SDimitry Andric // Read all the records. 1783*0b57cec5SDimitry Andric while (true) { 1784*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 1785*0b57cec5SDimitry Andric if (!MaybeEntry) 1786*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 1787*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 1788*0b57cec5SDimitry Andric 1789*0b57cec5SDimitry Andric switch (Entry.Kind) { 1790*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 1791*0b57cec5SDimitry Andric case BitstreamEntry::Error: 1792*0b57cec5SDimitry Andric return error("Malformed block"); 1793*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 1794*0b57cec5SDimitry Andric return Error::success(); 1795*0b57cec5SDimitry Andric case BitstreamEntry::Record: 1796*0b57cec5SDimitry Andric // The interesting case. 1797*0b57cec5SDimitry Andric break; 1798*0b57cec5SDimitry Andric } 1799*0b57cec5SDimitry Andric 1800*0b57cec5SDimitry Andric // Read a record. 1801*0b57cec5SDimitry Andric Record.clear(); 1802*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 1803*0b57cec5SDimitry Andric if (!MaybeRecord) 1804*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 1805*0b57cec5SDimitry Andric switch (MaybeRecord.get()) { 1806*0b57cec5SDimitry Andric default: // Default behavior: ignore. 1807*0b57cec5SDimitry Andric break; 1808*0b57cec5SDimitry Andric case bitc::PARAMATTR_CODE_ENTRY_OLD: // ENTRY: [paramidx0, attr0, ...] 18095ffd83dbSDimitry Andric // Deprecated, but still needed to read old bitcode files. 1810*0b57cec5SDimitry Andric if (Record.size() & 1) 181181ad6265SDimitry Andric return error("Invalid parameter attribute record"); 1812*0b57cec5SDimitry Andric 1813*0b57cec5SDimitry Andric for (unsigned i = 0, e = Record.size(); i != e; i += 2) { 181404eeddc0SDimitry Andric AttrBuilder B(Context); 1815*0b57cec5SDimitry Andric decodeLLVMAttributesForBitcode(B, Record[i+1]); 1816*0b57cec5SDimitry Andric Attrs.push_back(AttributeList::get(Context, Record[i], B)); 1817*0b57cec5SDimitry Andric } 1818*0b57cec5SDimitry Andric 1819*0b57cec5SDimitry Andric MAttributes.push_back(AttributeList::get(Context, Attrs)); 1820*0b57cec5SDimitry Andric Attrs.clear(); 1821*0b57cec5SDimitry Andric break; 1822*0b57cec5SDimitry Andric case bitc::PARAMATTR_CODE_ENTRY: // ENTRY: [attrgrp0, attrgrp1, ...] 1823*0b57cec5SDimitry Andric for (unsigned i = 0, e = Record.size(); i != e; ++i) 1824*0b57cec5SDimitry Andric Attrs.push_back(MAttributeGroups[Record[i]]); 1825*0b57cec5SDimitry Andric 1826*0b57cec5SDimitry Andric MAttributes.push_back(AttributeList::get(Context, Attrs)); 1827*0b57cec5SDimitry Andric Attrs.clear(); 1828*0b57cec5SDimitry Andric break; 1829*0b57cec5SDimitry Andric } 1830*0b57cec5SDimitry Andric } 1831*0b57cec5SDimitry Andric } 1832*0b57cec5SDimitry Andric 1833*0b57cec5SDimitry Andric // Returns Attribute::None on unrecognized codes. 1834*0b57cec5SDimitry Andric static Attribute::AttrKind getAttrFromCode(uint64_t Code) { 1835*0b57cec5SDimitry Andric switch (Code) { 1836*0b57cec5SDimitry Andric default: 1837*0b57cec5SDimitry Andric return Attribute::None; 1838*0b57cec5SDimitry Andric case bitc::ATTR_KIND_ALIGNMENT: 1839*0b57cec5SDimitry Andric return Attribute::Alignment; 1840*0b57cec5SDimitry Andric case bitc::ATTR_KIND_ALWAYS_INLINE: 1841*0b57cec5SDimitry Andric return Attribute::AlwaysInline; 1842*0b57cec5SDimitry Andric case bitc::ATTR_KIND_ARGMEMONLY: 1843*0b57cec5SDimitry Andric return Attribute::ArgMemOnly; 1844*0b57cec5SDimitry Andric case bitc::ATTR_KIND_BUILTIN: 1845*0b57cec5SDimitry Andric return Attribute::Builtin; 1846*0b57cec5SDimitry Andric case bitc::ATTR_KIND_BY_VAL: 1847*0b57cec5SDimitry Andric return Attribute::ByVal; 1848*0b57cec5SDimitry Andric case bitc::ATTR_KIND_IN_ALLOCA: 1849*0b57cec5SDimitry Andric return Attribute::InAlloca; 1850*0b57cec5SDimitry Andric case bitc::ATTR_KIND_COLD: 1851*0b57cec5SDimitry Andric return Attribute::Cold; 1852*0b57cec5SDimitry Andric case bitc::ATTR_KIND_CONVERGENT: 1853*0b57cec5SDimitry Andric return Attribute::Convergent; 1854349cc55cSDimitry Andric case bitc::ATTR_KIND_DISABLE_SANITIZER_INSTRUMENTATION: 1855349cc55cSDimitry Andric return Attribute::DisableSanitizerInstrumentation; 1856fe6060f1SDimitry Andric case bitc::ATTR_KIND_ELEMENTTYPE: 1857fe6060f1SDimitry Andric return Attribute::ElementType; 1858753f127fSDimitry Andric case bitc::ATTR_KIND_FNRETTHUNK_EXTERN: 1859753f127fSDimitry Andric return Attribute::FnRetThunkExtern; 1860*0b57cec5SDimitry Andric case bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY: 1861*0b57cec5SDimitry Andric return Attribute::InaccessibleMemOnly; 1862*0b57cec5SDimitry Andric case bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY: 1863*0b57cec5SDimitry Andric return Attribute::InaccessibleMemOrArgMemOnly; 1864*0b57cec5SDimitry Andric case bitc::ATTR_KIND_INLINE_HINT: 1865*0b57cec5SDimitry Andric return Attribute::InlineHint; 1866*0b57cec5SDimitry Andric case bitc::ATTR_KIND_IN_REG: 1867*0b57cec5SDimitry Andric return Attribute::InReg; 1868*0b57cec5SDimitry Andric case bitc::ATTR_KIND_JUMP_TABLE: 1869*0b57cec5SDimitry Andric return Attribute::JumpTable; 1870*0b57cec5SDimitry Andric case bitc::ATTR_KIND_MIN_SIZE: 1871*0b57cec5SDimitry Andric return Attribute::MinSize; 1872*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NAKED: 1873*0b57cec5SDimitry Andric return Attribute::Naked; 1874*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NEST: 1875*0b57cec5SDimitry Andric return Attribute::Nest; 1876*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NO_ALIAS: 1877*0b57cec5SDimitry Andric return Attribute::NoAlias; 1878*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NO_BUILTIN: 1879*0b57cec5SDimitry Andric return Attribute::NoBuiltin; 1880e8d8bef9SDimitry Andric case bitc::ATTR_KIND_NO_CALLBACK: 1881e8d8bef9SDimitry Andric return Attribute::NoCallback; 1882*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NO_CAPTURE: 1883*0b57cec5SDimitry Andric return Attribute::NoCapture; 1884*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NO_DUPLICATE: 1885*0b57cec5SDimitry Andric return Attribute::NoDuplicate; 1886*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NOFREE: 1887*0b57cec5SDimitry Andric return Attribute::NoFree; 1888*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT: 1889*0b57cec5SDimitry Andric return Attribute::NoImplicitFloat; 1890*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NO_INLINE: 1891*0b57cec5SDimitry Andric return Attribute::NoInline; 1892*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NO_RECURSE: 1893*0b57cec5SDimitry Andric return Attribute::NoRecurse; 18945ffd83dbSDimitry Andric case bitc::ATTR_KIND_NO_MERGE: 18955ffd83dbSDimitry Andric return Attribute::NoMerge; 1896*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NON_LAZY_BIND: 1897*0b57cec5SDimitry Andric return Attribute::NonLazyBind; 1898*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NON_NULL: 1899*0b57cec5SDimitry Andric return Attribute::NonNull; 1900*0b57cec5SDimitry Andric case bitc::ATTR_KIND_DEREFERENCEABLE: 1901*0b57cec5SDimitry Andric return Attribute::Dereferenceable; 1902*0b57cec5SDimitry Andric case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL: 1903*0b57cec5SDimitry Andric return Attribute::DereferenceableOrNull; 190481ad6265SDimitry Andric case bitc::ATTR_KIND_ALLOC_ALIGN: 190581ad6265SDimitry Andric return Attribute::AllocAlign; 190681ad6265SDimitry Andric case bitc::ATTR_KIND_ALLOC_KIND: 190781ad6265SDimitry Andric return Attribute::AllocKind; 1908*0b57cec5SDimitry Andric case bitc::ATTR_KIND_ALLOC_SIZE: 1909*0b57cec5SDimitry Andric return Attribute::AllocSize; 191081ad6265SDimitry Andric case bitc::ATTR_KIND_ALLOCATED_POINTER: 191181ad6265SDimitry Andric return Attribute::AllocatedPointer; 1912*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NO_RED_ZONE: 1913*0b57cec5SDimitry Andric return Attribute::NoRedZone; 1914*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NO_RETURN: 1915*0b57cec5SDimitry Andric return Attribute::NoReturn; 1916*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NOSYNC: 1917*0b57cec5SDimitry Andric return Attribute::NoSync; 1918*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NOCF_CHECK: 1919*0b57cec5SDimitry Andric return Attribute::NoCfCheck; 1920fe6060f1SDimitry Andric case bitc::ATTR_KIND_NO_PROFILE: 1921fe6060f1SDimitry Andric return Attribute::NoProfile; 1922*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NO_UNWIND: 1923*0b57cec5SDimitry Andric return Attribute::NoUnwind; 192481ad6265SDimitry Andric case bitc::ATTR_KIND_NO_SANITIZE_BOUNDS: 192581ad6265SDimitry Andric return Attribute::NoSanitizeBounds; 1926fe6060f1SDimitry Andric case bitc::ATTR_KIND_NO_SANITIZE_COVERAGE: 1927fe6060f1SDimitry Andric return Attribute::NoSanitizeCoverage; 19285ffd83dbSDimitry Andric case bitc::ATTR_KIND_NULL_POINTER_IS_VALID: 19295ffd83dbSDimitry Andric return Attribute::NullPointerIsValid; 1930*0b57cec5SDimitry Andric case bitc::ATTR_KIND_OPT_FOR_FUZZING: 1931*0b57cec5SDimitry Andric return Attribute::OptForFuzzing; 1932*0b57cec5SDimitry Andric case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE: 1933*0b57cec5SDimitry Andric return Attribute::OptimizeForSize; 1934*0b57cec5SDimitry Andric case bitc::ATTR_KIND_OPTIMIZE_NONE: 1935*0b57cec5SDimitry Andric return Attribute::OptimizeNone; 1936*0b57cec5SDimitry Andric case bitc::ATTR_KIND_READ_NONE: 1937*0b57cec5SDimitry Andric return Attribute::ReadNone; 1938*0b57cec5SDimitry Andric case bitc::ATTR_KIND_READ_ONLY: 1939*0b57cec5SDimitry Andric return Attribute::ReadOnly; 1940*0b57cec5SDimitry Andric case bitc::ATTR_KIND_RETURNED: 1941*0b57cec5SDimitry Andric return Attribute::Returned; 1942*0b57cec5SDimitry Andric case bitc::ATTR_KIND_RETURNS_TWICE: 1943*0b57cec5SDimitry Andric return Attribute::ReturnsTwice; 1944*0b57cec5SDimitry Andric case bitc::ATTR_KIND_S_EXT: 1945*0b57cec5SDimitry Andric return Attribute::SExt; 1946*0b57cec5SDimitry Andric case bitc::ATTR_KIND_SPECULATABLE: 1947*0b57cec5SDimitry Andric return Attribute::Speculatable; 1948*0b57cec5SDimitry Andric case bitc::ATTR_KIND_STACK_ALIGNMENT: 1949*0b57cec5SDimitry Andric return Attribute::StackAlignment; 1950*0b57cec5SDimitry Andric case bitc::ATTR_KIND_STACK_PROTECT: 1951*0b57cec5SDimitry Andric return Attribute::StackProtect; 1952*0b57cec5SDimitry Andric case bitc::ATTR_KIND_STACK_PROTECT_REQ: 1953*0b57cec5SDimitry Andric return Attribute::StackProtectReq; 1954*0b57cec5SDimitry Andric case bitc::ATTR_KIND_STACK_PROTECT_STRONG: 1955*0b57cec5SDimitry Andric return Attribute::StackProtectStrong; 1956*0b57cec5SDimitry Andric case bitc::ATTR_KIND_SAFESTACK: 1957*0b57cec5SDimitry Andric return Attribute::SafeStack; 1958*0b57cec5SDimitry Andric case bitc::ATTR_KIND_SHADOWCALLSTACK: 1959*0b57cec5SDimitry Andric return Attribute::ShadowCallStack; 1960*0b57cec5SDimitry Andric case bitc::ATTR_KIND_STRICT_FP: 1961*0b57cec5SDimitry Andric return Attribute::StrictFP; 1962*0b57cec5SDimitry Andric case bitc::ATTR_KIND_STRUCT_RET: 1963*0b57cec5SDimitry Andric return Attribute::StructRet; 1964*0b57cec5SDimitry Andric case bitc::ATTR_KIND_SANITIZE_ADDRESS: 1965*0b57cec5SDimitry Andric return Attribute::SanitizeAddress; 1966*0b57cec5SDimitry Andric case bitc::ATTR_KIND_SANITIZE_HWADDRESS: 1967*0b57cec5SDimitry Andric return Attribute::SanitizeHWAddress; 1968*0b57cec5SDimitry Andric case bitc::ATTR_KIND_SANITIZE_THREAD: 1969*0b57cec5SDimitry Andric return Attribute::SanitizeThread; 1970*0b57cec5SDimitry Andric case bitc::ATTR_KIND_SANITIZE_MEMORY: 1971*0b57cec5SDimitry Andric return Attribute::SanitizeMemory; 1972*0b57cec5SDimitry Andric case bitc::ATTR_KIND_SPECULATIVE_LOAD_HARDENING: 1973*0b57cec5SDimitry Andric return Attribute::SpeculativeLoadHardening; 1974*0b57cec5SDimitry Andric case bitc::ATTR_KIND_SWIFT_ERROR: 1975*0b57cec5SDimitry Andric return Attribute::SwiftError; 1976*0b57cec5SDimitry Andric case bitc::ATTR_KIND_SWIFT_SELF: 1977*0b57cec5SDimitry Andric return Attribute::SwiftSelf; 1978fe6060f1SDimitry Andric case bitc::ATTR_KIND_SWIFT_ASYNC: 1979fe6060f1SDimitry Andric return Attribute::SwiftAsync; 1980*0b57cec5SDimitry Andric case bitc::ATTR_KIND_UW_TABLE: 1981*0b57cec5SDimitry Andric return Attribute::UWTable; 1982fe6060f1SDimitry Andric case bitc::ATTR_KIND_VSCALE_RANGE: 1983fe6060f1SDimitry Andric return Attribute::VScaleRange; 1984*0b57cec5SDimitry Andric case bitc::ATTR_KIND_WILLRETURN: 1985*0b57cec5SDimitry Andric return Attribute::WillReturn; 1986*0b57cec5SDimitry Andric case bitc::ATTR_KIND_WRITEONLY: 1987*0b57cec5SDimitry Andric return Attribute::WriteOnly; 1988*0b57cec5SDimitry Andric case bitc::ATTR_KIND_Z_EXT: 1989*0b57cec5SDimitry Andric return Attribute::ZExt; 1990*0b57cec5SDimitry Andric case bitc::ATTR_KIND_IMMARG: 1991*0b57cec5SDimitry Andric return Attribute::ImmArg; 1992*0b57cec5SDimitry Andric case bitc::ATTR_KIND_SANITIZE_MEMTAG: 1993*0b57cec5SDimitry Andric return Attribute::SanitizeMemTag; 19945ffd83dbSDimitry Andric case bitc::ATTR_KIND_PREALLOCATED: 19955ffd83dbSDimitry Andric return Attribute::Preallocated; 19965ffd83dbSDimitry Andric case bitc::ATTR_KIND_NOUNDEF: 19975ffd83dbSDimitry Andric return Attribute::NoUndef; 1998e8d8bef9SDimitry Andric case bitc::ATTR_KIND_BYREF: 1999e8d8bef9SDimitry Andric return Attribute::ByRef; 2000e8d8bef9SDimitry Andric case bitc::ATTR_KIND_MUSTPROGRESS: 2001e8d8bef9SDimitry Andric return Attribute::MustProgress; 2002e8d8bef9SDimitry Andric case bitc::ATTR_KIND_HOT: 2003e8d8bef9SDimitry Andric return Attribute::Hot; 200481ad6265SDimitry Andric case bitc::ATTR_KIND_PRESPLIT_COROUTINE: 200581ad6265SDimitry Andric return Attribute::PresplitCoroutine; 2006*0b57cec5SDimitry Andric } 2007*0b57cec5SDimitry Andric } 2008*0b57cec5SDimitry Andric 2009*0b57cec5SDimitry Andric Error BitcodeReader::parseAlignmentValue(uint64_t Exponent, 20108bcb0991SDimitry Andric MaybeAlign &Alignment) { 2011*0b57cec5SDimitry Andric // Note: Alignment in bitcode files is incremented by 1, so that zero 2012*0b57cec5SDimitry Andric // can be used for default alignment. 2013*0b57cec5SDimitry Andric if (Exponent > Value::MaxAlignmentExponent + 1) 2014*0b57cec5SDimitry Andric return error("Invalid alignment value"); 20158bcb0991SDimitry Andric Alignment = decodeMaybeAlign(Exponent); 2016*0b57cec5SDimitry Andric return Error::success(); 2017*0b57cec5SDimitry Andric } 2018*0b57cec5SDimitry Andric 2019*0b57cec5SDimitry Andric Error BitcodeReader::parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind) { 2020*0b57cec5SDimitry Andric *Kind = getAttrFromCode(Code); 2021*0b57cec5SDimitry Andric if (*Kind == Attribute::None) 2022*0b57cec5SDimitry Andric return error("Unknown attribute kind (" + Twine(Code) + ")"); 2023*0b57cec5SDimitry Andric return Error::success(); 2024*0b57cec5SDimitry Andric } 2025*0b57cec5SDimitry Andric 2026*0b57cec5SDimitry Andric Error BitcodeReader::parseAttributeGroupBlock() { 2027*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID)) 2028*0b57cec5SDimitry Andric return Err; 2029*0b57cec5SDimitry Andric 2030*0b57cec5SDimitry Andric if (!MAttributeGroups.empty()) 2031*0b57cec5SDimitry Andric return error("Invalid multiple blocks"); 2032*0b57cec5SDimitry Andric 2033*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 2034*0b57cec5SDimitry Andric 2035*0b57cec5SDimitry Andric // Read all the records. 2036*0b57cec5SDimitry Andric while (true) { 2037*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 2038*0b57cec5SDimitry Andric if (!MaybeEntry) 2039*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 2040*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 2041*0b57cec5SDimitry Andric 2042*0b57cec5SDimitry Andric switch (Entry.Kind) { 2043*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 2044*0b57cec5SDimitry Andric case BitstreamEntry::Error: 2045*0b57cec5SDimitry Andric return error("Malformed block"); 2046*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 2047*0b57cec5SDimitry Andric return Error::success(); 2048*0b57cec5SDimitry Andric case BitstreamEntry::Record: 2049*0b57cec5SDimitry Andric // The interesting case. 2050*0b57cec5SDimitry Andric break; 2051*0b57cec5SDimitry Andric } 2052*0b57cec5SDimitry Andric 2053*0b57cec5SDimitry Andric // Read a record. 2054*0b57cec5SDimitry Andric Record.clear(); 2055*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 2056*0b57cec5SDimitry Andric if (!MaybeRecord) 2057*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 2058*0b57cec5SDimitry Andric switch (MaybeRecord.get()) { 2059*0b57cec5SDimitry Andric default: // Default behavior: ignore. 2060*0b57cec5SDimitry Andric break; 2061*0b57cec5SDimitry Andric case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...] 2062*0b57cec5SDimitry Andric if (Record.size() < 3) 206381ad6265SDimitry Andric return error("Invalid grp record"); 2064*0b57cec5SDimitry Andric 2065*0b57cec5SDimitry Andric uint64_t GrpID = Record[0]; 2066*0b57cec5SDimitry Andric uint64_t Idx = Record[1]; // Index of the object this attribute refers to. 2067*0b57cec5SDimitry Andric 206804eeddc0SDimitry Andric AttrBuilder B(Context); 2069*0b57cec5SDimitry Andric for (unsigned i = 2, e = Record.size(); i != e; ++i) { 2070*0b57cec5SDimitry Andric if (Record[i] == 0) { // Enum attribute 2071*0b57cec5SDimitry Andric Attribute::AttrKind Kind; 2072*0b57cec5SDimitry Andric if (Error Err = parseAttrKind(Record[++i], &Kind)) 2073*0b57cec5SDimitry Andric return Err; 2074*0b57cec5SDimitry Andric 2075*0b57cec5SDimitry Andric // Upgrade old-style byval attribute to one with a type, even if it's 2076*0b57cec5SDimitry Andric // nullptr. We will have to insert the real type when we associate 2077*0b57cec5SDimitry Andric // this AttributeList with a function. 2078*0b57cec5SDimitry Andric if (Kind == Attribute::ByVal) 2079*0b57cec5SDimitry Andric B.addByValAttr(nullptr); 2080e8d8bef9SDimitry Andric else if (Kind == Attribute::StructRet) 2081e8d8bef9SDimitry Andric B.addStructRetAttr(nullptr); 2082fe6060f1SDimitry Andric else if (Kind == Attribute::InAlloca) 2083fe6060f1SDimitry Andric B.addInAllocaAttr(nullptr); 208481ad6265SDimitry Andric else if (Kind == Attribute::UWTable) 208581ad6265SDimitry Andric B.addUWTableAttr(UWTableKind::Default); 2086fe6060f1SDimitry Andric else if (Attribute::isEnumAttrKind(Kind)) 2087*0b57cec5SDimitry Andric B.addAttribute(Kind); 2088fe6060f1SDimitry Andric else 2089fe6060f1SDimitry Andric return error("Not an enum attribute"); 2090*0b57cec5SDimitry Andric } else if (Record[i] == 1) { // Integer attribute 2091*0b57cec5SDimitry Andric Attribute::AttrKind Kind; 2092*0b57cec5SDimitry Andric if (Error Err = parseAttrKind(Record[++i], &Kind)) 2093*0b57cec5SDimitry Andric return Err; 2094fe6060f1SDimitry Andric if (!Attribute::isIntAttrKind(Kind)) 2095fe6060f1SDimitry Andric return error("Not an int attribute"); 2096*0b57cec5SDimitry Andric if (Kind == Attribute::Alignment) 2097*0b57cec5SDimitry Andric B.addAlignmentAttr(Record[++i]); 2098*0b57cec5SDimitry Andric else if (Kind == Attribute::StackAlignment) 2099*0b57cec5SDimitry Andric B.addStackAlignmentAttr(Record[++i]); 2100*0b57cec5SDimitry Andric else if (Kind == Attribute::Dereferenceable) 2101*0b57cec5SDimitry Andric B.addDereferenceableAttr(Record[++i]); 2102*0b57cec5SDimitry Andric else if (Kind == Attribute::DereferenceableOrNull) 2103*0b57cec5SDimitry Andric B.addDereferenceableOrNullAttr(Record[++i]); 2104*0b57cec5SDimitry Andric else if (Kind == Attribute::AllocSize) 2105*0b57cec5SDimitry Andric B.addAllocSizeAttrFromRawRepr(Record[++i]); 2106fe6060f1SDimitry Andric else if (Kind == Attribute::VScaleRange) 2107fe6060f1SDimitry Andric B.addVScaleRangeAttrFromRawRepr(Record[++i]); 210881ad6265SDimitry Andric else if (Kind == Attribute::UWTable) 210981ad6265SDimitry Andric B.addUWTableAttr(UWTableKind(Record[++i])); 211081ad6265SDimitry Andric else if (Kind == Attribute::AllocKind) 211181ad6265SDimitry Andric B.addAllocKindAttr(static_cast<AllocFnKind>(Record[++i])); 2112*0b57cec5SDimitry Andric } else if (Record[i] == 3 || Record[i] == 4) { // String attribute 2113*0b57cec5SDimitry Andric bool HasValue = (Record[i++] == 4); 2114*0b57cec5SDimitry Andric SmallString<64> KindStr; 2115*0b57cec5SDimitry Andric SmallString<64> ValStr; 2116*0b57cec5SDimitry Andric 2117*0b57cec5SDimitry Andric while (Record[i] != 0 && i != e) 2118*0b57cec5SDimitry Andric KindStr += Record[i++]; 2119*0b57cec5SDimitry Andric assert(Record[i] == 0 && "Kind string not null terminated"); 2120*0b57cec5SDimitry Andric 2121*0b57cec5SDimitry Andric if (HasValue) { 2122*0b57cec5SDimitry Andric // Has a value associated with it. 2123*0b57cec5SDimitry Andric ++i; // Skip the '0' that terminates the "kind" string. 2124*0b57cec5SDimitry Andric while (Record[i] != 0 && i != e) 2125*0b57cec5SDimitry Andric ValStr += Record[i++]; 2126*0b57cec5SDimitry Andric assert(Record[i] == 0 && "Value string not null terminated"); 2127*0b57cec5SDimitry Andric } 2128*0b57cec5SDimitry Andric 2129*0b57cec5SDimitry Andric B.addAttribute(KindStr.str(), ValStr.str()); 213081ad6265SDimitry Andric } else if (Record[i] == 5 || Record[i] == 6) { 2131*0b57cec5SDimitry Andric bool HasType = Record[i] == 6; 2132*0b57cec5SDimitry Andric Attribute::AttrKind Kind; 2133*0b57cec5SDimitry Andric if (Error Err = parseAttrKind(Record[++i], &Kind)) 2134*0b57cec5SDimitry Andric return Err; 2135fe6060f1SDimitry Andric if (!Attribute::isTypeAttrKind(Kind)) 2136fe6060f1SDimitry Andric return error("Not a type attribute"); 2137fe6060f1SDimitry Andric 2138fe6060f1SDimitry Andric B.addTypeAttr(Kind, HasType ? getTypeByID(Record[++i]) : nullptr); 213981ad6265SDimitry Andric } else { 214081ad6265SDimitry Andric return error("Invalid attribute group entry"); 2141*0b57cec5SDimitry Andric } 2142*0b57cec5SDimitry Andric } 2143*0b57cec5SDimitry Andric 21445ffd83dbSDimitry Andric UpgradeAttributes(B); 2145*0b57cec5SDimitry Andric MAttributeGroups[GrpID] = AttributeList::get(Context, Idx, B); 2146*0b57cec5SDimitry Andric break; 2147*0b57cec5SDimitry Andric } 2148*0b57cec5SDimitry Andric } 2149*0b57cec5SDimitry Andric } 2150*0b57cec5SDimitry Andric } 2151*0b57cec5SDimitry Andric 2152*0b57cec5SDimitry Andric Error BitcodeReader::parseTypeTable() { 2153*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW)) 2154*0b57cec5SDimitry Andric return Err; 2155*0b57cec5SDimitry Andric 2156*0b57cec5SDimitry Andric return parseTypeTableBody(); 2157*0b57cec5SDimitry Andric } 2158*0b57cec5SDimitry Andric 2159*0b57cec5SDimitry Andric Error BitcodeReader::parseTypeTableBody() { 2160*0b57cec5SDimitry Andric if (!TypeList.empty()) 2161*0b57cec5SDimitry Andric return error("Invalid multiple blocks"); 2162*0b57cec5SDimitry Andric 2163*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 2164*0b57cec5SDimitry Andric unsigned NumRecords = 0; 2165*0b57cec5SDimitry Andric 2166*0b57cec5SDimitry Andric SmallString<64> TypeName; 2167*0b57cec5SDimitry Andric 2168*0b57cec5SDimitry Andric // Read all the records for this type table. 2169*0b57cec5SDimitry Andric while (true) { 2170*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 2171*0b57cec5SDimitry Andric if (!MaybeEntry) 2172*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 2173*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 2174*0b57cec5SDimitry Andric 2175*0b57cec5SDimitry Andric switch (Entry.Kind) { 2176*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 2177*0b57cec5SDimitry Andric case BitstreamEntry::Error: 2178*0b57cec5SDimitry Andric return error("Malformed block"); 2179*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 2180*0b57cec5SDimitry Andric if (NumRecords != TypeList.size()) 2181*0b57cec5SDimitry Andric return error("Malformed block"); 2182*0b57cec5SDimitry Andric return Error::success(); 2183*0b57cec5SDimitry Andric case BitstreamEntry::Record: 2184*0b57cec5SDimitry Andric // The interesting case. 2185*0b57cec5SDimitry Andric break; 2186*0b57cec5SDimitry Andric } 2187*0b57cec5SDimitry Andric 2188*0b57cec5SDimitry Andric // Read a record. 2189*0b57cec5SDimitry Andric Record.clear(); 2190*0b57cec5SDimitry Andric Type *ResultTy = nullptr; 219181ad6265SDimitry Andric SmallVector<unsigned> ContainedIDs; 2192*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 2193*0b57cec5SDimitry Andric if (!MaybeRecord) 2194*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 2195*0b57cec5SDimitry Andric switch (MaybeRecord.get()) { 2196*0b57cec5SDimitry Andric default: 2197*0b57cec5SDimitry Andric return error("Invalid value"); 2198*0b57cec5SDimitry Andric case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries] 2199*0b57cec5SDimitry Andric // TYPE_CODE_NUMENTRY contains a count of the number of types in the 2200*0b57cec5SDimitry Andric // type list. This allows us to reserve space. 2201e8d8bef9SDimitry Andric if (Record.empty()) 220281ad6265SDimitry Andric return error("Invalid numentry record"); 2203*0b57cec5SDimitry Andric TypeList.resize(Record[0]); 2204*0b57cec5SDimitry Andric continue; 2205*0b57cec5SDimitry Andric case bitc::TYPE_CODE_VOID: // VOID 2206*0b57cec5SDimitry Andric ResultTy = Type::getVoidTy(Context); 2207*0b57cec5SDimitry Andric break; 2208*0b57cec5SDimitry Andric case bitc::TYPE_CODE_HALF: // HALF 2209*0b57cec5SDimitry Andric ResultTy = Type::getHalfTy(Context); 2210*0b57cec5SDimitry Andric break; 22115ffd83dbSDimitry Andric case bitc::TYPE_CODE_BFLOAT: // BFLOAT 22125ffd83dbSDimitry Andric ResultTy = Type::getBFloatTy(Context); 22135ffd83dbSDimitry Andric break; 2214*0b57cec5SDimitry Andric case bitc::TYPE_CODE_FLOAT: // FLOAT 2215*0b57cec5SDimitry Andric ResultTy = Type::getFloatTy(Context); 2216*0b57cec5SDimitry Andric break; 2217*0b57cec5SDimitry Andric case bitc::TYPE_CODE_DOUBLE: // DOUBLE 2218*0b57cec5SDimitry Andric ResultTy = Type::getDoubleTy(Context); 2219*0b57cec5SDimitry Andric break; 2220*0b57cec5SDimitry Andric case bitc::TYPE_CODE_X86_FP80: // X86_FP80 2221*0b57cec5SDimitry Andric ResultTy = Type::getX86_FP80Ty(Context); 2222*0b57cec5SDimitry Andric break; 2223*0b57cec5SDimitry Andric case bitc::TYPE_CODE_FP128: // FP128 2224*0b57cec5SDimitry Andric ResultTy = Type::getFP128Ty(Context); 2225*0b57cec5SDimitry Andric break; 2226*0b57cec5SDimitry Andric case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128 2227*0b57cec5SDimitry Andric ResultTy = Type::getPPC_FP128Ty(Context); 2228*0b57cec5SDimitry Andric break; 2229*0b57cec5SDimitry Andric case bitc::TYPE_CODE_LABEL: // LABEL 2230*0b57cec5SDimitry Andric ResultTy = Type::getLabelTy(Context); 2231*0b57cec5SDimitry Andric break; 2232*0b57cec5SDimitry Andric case bitc::TYPE_CODE_METADATA: // METADATA 2233*0b57cec5SDimitry Andric ResultTy = Type::getMetadataTy(Context); 2234*0b57cec5SDimitry Andric break; 2235*0b57cec5SDimitry Andric case bitc::TYPE_CODE_X86_MMX: // X86_MMX 2236*0b57cec5SDimitry Andric ResultTy = Type::getX86_MMXTy(Context); 2237*0b57cec5SDimitry Andric break; 2238e8d8bef9SDimitry Andric case bitc::TYPE_CODE_X86_AMX: // X86_AMX 2239e8d8bef9SDimitry Andric ResultTy = Type::getX86_AMXTy(Context); 2240e8d8bef9SDimitry Andric break; 2241*0b57cec5SDimitry Andric case bitc::TYPE_CODE_TOKEN: // TOKEN 2242*0b57cec5SDimitry Andric ResultTy = Type::getTokenTy(Context); 2243*0b57cec5SDimitry Andric break; 2244*0b57cec5SDimitry Andric case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width] 2245e8d8bef9SDimitry Andric if (Record.empty()) 224681ad6265SDimitry Andric return error("Invalid integer record"); 2247*0b57cec5SDimitry Andric 2248*0b57cec5SDimitry Andric uint64_t NumBits = Record[0]; 2249*0b57cec5SDimitry Andric if (NumBits < IntegerType::MIN_INT_BITS || 2250*0b57cec5SDimitry Andric NumBits > IntegerType::MAX_INT_BITS) 2251*0b57cec5SDimitry Andric return error("Bitwidth for integer type out of range"); 2252*0b57cec5SDimitry Andric ResultTy = IntegerType::get(Context, NumBits); 2253*0b57cec5SDimitry Andric break; 2254*0b57cec5SDimitry Andric } 2255*0b57cec5SDimitry Andric case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or 2256*0b57cec5SDimitry Andric // [pointee type, address space] 2257e8d8bef9SDimitry Andric if (Record.empty()) 225881ad6265SDimitry Andric return error("Invalid pointer record"); 2259*0b57cec5SDimitry Andric unsigned AddressSpace = 0; 2260*0b57cec5SDimitry Andric if (Record.size() == 2) 2261*0b57cec5SDimitry Andric AddressSpace = Record[1]; 2262*0b57cec5SDimitry Andric ResultTy = getTypeByID(Record[0]); 2263*0b57cec5SDimitry Andric if (!ResultTy || 2264*0b57cec5SDimitry Andric !PointerType::isValidElementType(ResultTy)) 2265*0b57cec5SDimitry Andric return error("Invalid type"); 226681ad6265SDimitry Andric if (LLVM_UNLIKELY(!Context.hasSetOpaquePointersValue())) 226781ad6265SDimitry Andric Context.setOpaquePointers(false); 226881ad6265SDimitry Andric ContainedIDs.push_back(Record[0]); 2269*0b57cec5SDimitry Andric ResultTy = PointerType::get(ResultTy, AddressSpace); 2270*0b57cec5SDimitry Andric break; 2271*0b57cec5SDimitry Andric } 2272fe6060f1SDimitry Andric case bitc::TYPE_CODE_OPAQUE_POINTER: { // OPAQUE_POINTER: [addrspace] 2273fe6060f1SDimitry Andric if (Record.size() != 1) 227481ad6265SDimitry Andric return error("Invalid opaque pointer record"); 227581ad6265SDimitry Andric if (LLVM_UNLIKELY(!Context.hasSetOpaquePointersValue())) { 227681ad6265SDimitry Andric Context.setOpaquePointers(true); 227781ad6265SDimitry Andric } else if (Context.supportsTypedPointers()) 2278349cc55cSDimitry Andric return error( 2279349cc55cSDimitry Andric "Opaque pointers are only supported in -opaque-pointers mode"); 2280fe6060f1SDimitry Andric unsigned AddressSpace = Record[0]; 2281fe6060f1SDimitry Andric ResultTy = PointerType::get(Context, AddressSpace); 2282fe6060f1SDimitry Andric break; 2283fe6060f1SDimitry Andric } 2284*0b57cec5SDimitry Andric case bitc::TYPE_CODE_FUNCTION_OLD: { 22855ffd83dbSDimitry Andric // Deprecated, but still needed to read old bitcode files. 2286*0b57cec5SDimitry Andric // FUNCTION: [vararg, attrid, retty, paramty x N] 2287*0b57cec5SDimitry Andric if (Record.size() < 3) 228881ad6265SDimitry Andric return error("Invalid function record"); 2289*0b57cec5SDimitry Andric SmallVector<Type*, 8> ArgTys; 2290*0b57cec5SDimitry Andric for (unsigned i = 3, e = Record.size(); i != e; ++i) { 2291*0b57cec5SDimitry Andric if (Type *T = getTypeByID(Record[i])) 2292*0b57cec5SDimitry Andric ArgTys.push_back(T); 2293*0b57cec5SDimitry Andric else 2294*0b57cec5SDimitry Andric break; 2295*0b57cec5SDimitry Andric } 2296*0b57cec5SDimitry Andric 2297*0b57cec5SDimitry Andric ResultTy = getTypeByID(Record[2]); 2298*0b57cec5SDimitry Andric if (!ResultTy || ArgTys.size() < Record.size()-3) 2299*0b57cec5SDimitry Andric return error("Invalid type"); 2300*0b57cec5SDimitry Andric 230181ad6265SDimitry Andric ContainedIDs.append(Record.begin() + 2, Record.end()); 2302*0b57cec5SDimitry Andric ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); 2303*0b57cec5SDimitry Andric break; 2304*0b57cec5SDimitry Andric } 2305*0b57cec5SDimitry Andric case bitc::TYPE_CODE_FUNCTION: { 2306*0b57cec5SDimitry Andric // FUNCTION: [vararg, retty, paramty x N] 2307*0b57cec5SDimitry Andric if (Record.size() < 2) 230881ad6265SDimitry Andric return error("Invalid function record"); 2309*0b57cec5SDimitry Andric SmallVector<Type*, 8> ArgTys; 2310*0b57cec5SDimitry Andric for (unsigned i = 2, e = Record.size(); i != e; ++i) { 2311*0b57cec5SDimitry Andric if (Type *T = getTypeByID(Record[i])) { 2312*0b57cec5SDimitry Andric if (!FunctionType::isValidArgumentType(T)) 2313*0b57cec5SDimitry Andric return error("Invalid function argument type"); 2314*0b57cec5SDimitry Andric ArgTys.push_back(T); 2315*0b57cec5SDimitry Andric } 2316*0b57cec5SDimitry Andric else 2317*0b57cec5SDimitry Andric break; 2318*0b57cec5SDimitry Andric } 2319*0b57cec5SDimitry Andric 2320*0b57cec5SDimitry Andric ResultTy = getTypeByID(Record[1]); 2321*0b57cec5SDimitry Andric if (!ResultTy || ArgTys.size() < Record.size()-2) 2322*0b57cec5SDimitry Andric return error("Invalid type"); 2323*0b57cec5SDimitry Andric 232481ad6265SDimitry Andric ContainedIDs.append(Record.begin() + 1, Record.end()); 2325*0b57cec5SDimitry Andric ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); 2326*0b57cec5SDimitry Andric break; 2327*0b57cec5SDimitry Andric } 2328*0b57cec5SDimitry Andric case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N] 2329e8d8bef9SDimitry Andric if (Record.empty()) 233081ad6265SDimitry Andric return error("Invalid anon struct record"); 2331*0b57cec5SDimitry Andric SmallVector<Type*, 8> EltTys; 2332*0b57cec5SDimitry Andric for (unsigned i = 1, e = Record.size(); i != e; ++i) { 2333*0b57cec5SDimitry Andric if (Type *T = getTypeByID(Record[i])) 2334*0b57cec5SDimitry Andric EltTys.push_back(T); 2335*0b57cec5SDimitry Andric else 2336*0b57cec5SDimitry Andric break; 2337*0b57cec5SDimitry Andric } 2338*0b57cec5SDimitry Andric if (EltTys.size() != Record.size()-1) 2339*0b57cec5SDimitry Andric return error("Invalid type"); 234081ad6265SDimitry Andric ContainedIDs.append(Record.begin() + 1, Record.end()); 2341*0b57cec5SDimitry Andric ResultTy = StructType::get(Context, EltTys, Record[0]); 2342*0b57cec5SDimitry Andric break; 2343*0b57cec5SDimitry Andric } 2344*0b57cec5SDimitry Andric case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N] 2345*0b57cec5SDimitry Andric if (convertToString(Record, 0, TypeName)) 234681ad6265SDimitry Andric return error("Invalid struct name record"); 2347*0b57cec5SDimitry Andric continue; 2348*0b57cec5SDimitry Andric 2349*0b57cec5SDimitry Andric case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N] 2350e8d8bef9SDimitry Andric if (Record.empty()) 235181ad6265SDimitry Andric return error("Invalid named struct record"); 2352*0b57cec5SDimitry Andric 2353*0b57cec5SDimitry Andric if (NumRecords >= TypeList.size()) 2354*0b57cec5SDimitry Andric return error("Invalid TYPE table"); 2355*0b57cec5SDimitry Andric 2356*0b57cec5SDimitry Andric // Check to see if this was forward referenced, if so fill in the temp. 2357*0b57cec5SDimitry Andric StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); 2358*0b57cec5SDimitry Andric if (Res) { 2359*0b57cec5SDimitry Andric Res->setName(TypeName); 2360*0b57cec5SDimitry Andric TypeList[NumRecords] = nullptr; 2361*0b57cec5SDimitry Andric } else // Otherwise, create a new struct. 2362*0b57cec5SDimitry Andric Res = createIdentifiedStructType(Context, TypeName); 2363*0b57cec5SDimitry Andric TypeName.clear(); 2364*0b57cec5SDimitry Andric 2365*0b57cec5SDimitry Andric SmallVector<Type*, 8> EltTys; 2366*0b57cec5SDimitry Andric for (unsigned i = 1, e = Record.size(); i != e; ++i) { 2367*0b57cec5SDimitry Andric if (Type *T = getTypeByID(Record[i])) 2368*0b57cec5SDimitry Andric EltTys.push_back(T); 2369*0b57cec5SDimitry Andric else 2370*0b57cec5SDimitry Andric break; 2371*0b57cec5SDimitry Andric } 2372*0b57cec5SDimitry Andric if (EltTys.size() != Record.size()-1) 237381ad6265SDimitry Andric return error("Invalid named struct record"); 2374*0b57cec5SDimitry Andric Res->setBody(EltTys, Record[0]); 237581ad6265SDimitry Andric ContainedIDs.append(Record.begin() + 1, Record.end()); 2376*0b57cec5SDimitry Andric ResultTy = Res; 2377*0b57cec5SDimitry Andric break; 2378*0b57cec5SDimitry Andric } 2379*0b57cec5SDimitry Andric case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: [] 2380*0b57cec5SDimitry Andric if (Record.size() != 1) 238181ad6265SDimitry Andric return error("Invalid opaque type record"); 2382*0b57cec5SDimitry Andric 2383*0b57cec5SDimitry Andric if (NumRecords >= TypeList.size()) 2384*0b57cec5SDimitry Andric return error("Invalid TYPE table"); 2385*0b57cec5SDimitry Andric 2386*0b57cec5SDimitry Andric // Check to see if this was forward referenced, if so fill in the temp. 2387*0b57cec5SDimitry Andric StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); 2388*0b57cec5SDimitry Andric if (Res) { 2389*0b57cec5SDimitry Andric Res->setName(TypeName); 2390*0b57cec5SDimitry Andric TypeList[NumRecords] = nullptr; 2391*0b57cec5SDimitry Andric } else // Otherwise, create a new struct with no body. 2392*0b57cec5SDimitry Andric Res = createIdentifiedStructType(Context, TypeName); 2393*0b57cec5SDimitry Andric TypeName.clear(); 2394*0b57cec5SDimitry Andric ResultTy = Res; 2395*0b57cec5SDimitry Andric break; 2396*0b57cec5SDimitry Andric } 2397*0b57cec5SDimitry Andric case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty] 2398*0b57cec5SDimitry Andric if (Record.size() < 2) 239981ad6265SDimitry Andric return error("Invalid array type record"); 2400*0b57cec5SDimitry Andric ResultTy = getTypeByID(Record[1]); 2401*0b57cec5SDimitry Andric if (!ResultTy || !ArrayType::isValidElementType(ResultTy)) 2402*0b57cec5SDimitry Andric return error("Invalid type"); 240381ad6265SDimitry Andric ContainedIDs.push_back(Record[1]); 2404*0b57cec5SDimitry Andric ResultTy = ArrayType::get(ResultTy, Record[0]); 2405*0b57cec5SDimitry Andric break; 2406*0b57cec5SDimitry Andric case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] or 2407*0b57cec5SDimitry Andric // [numelts, eltty, scalable] 2408*0b57cec5SDimitry Andric if (Record.size() < 2) 240981ad6265SDimitry Andric return error("Invalid vector type record"); 2410*0b57cec5SDimitry Andric if (Record[0] == 0) 2411*0b57cec5SDimitry Andric return error("Invalid vector length"); 2412*0b57cec5SDimitry Andric ResultTy = getTypeByID(Record[1]); 2413349cc55cSDimitry Andric if (!ResultTy || !VectorType::isValidElementType(ResultTy)) 2414*0b57cec5SDimitry Andric return error("Invalid type"); 2415*0b57cec5SDimitry Andric bool Scalable = Record.size() > 2 ? Record[2] : false; 241681ad6265SDimitry Andric ContainedIDs.push_back(Record[1]); 2417*0b57cec5SDimitry Andric ResultTy = VectorType::get(ResultTy, Record[0], Scalable); 2418*0b57cec5SDimitry Andric break; 2419*0b57cec5SDimitry Andric } 2420*0b57cec5SDimitry Andric 2421*0b57cec5SDimitry Andric if (NumRecords >= TypeList.size()) 2422*0b57cec5SDimitry Andric return error("Invalid TYPE table"); 2423*0b57cec5SDimitry Andric if (TypeList[NumRecords]) 2424*0b57cec5SDimitry Andric return error( 2425*0b57cec5SDimitry Andric "Invalid TYPE table: Only named structs can be forward referenced"); 2426*0b57cec5SDimitry Andric assert(ResultTy && "Didn't read a type?"); 242781ad6265SDimitry Andric TypeList[NumRecords] = ResultTy; 242881ad6265SDimitry Andric if (!ContainedIDs.empty()) 242981ad6265SDimitry Andric ContainedTypeIDs[NumRecords] = std::move(ContainedIDs); 243081ad6265SDimitry Andric ++NumRecords; 2431*0b57cec5SDimitry Andric } 2432*0b57cec5SDimitry Andric } 2433*0b57cec5SDimitry Andric 2434*0b57cec5SDimitry Andric Error BitcodeReader::parseOperandBundleTags() { 2435*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID)) 2436*0b57cec5SDimitry Andric return Err; 2437*0b57cec5SDimitry Andric 2438*0b57cec5SDimitry Andric if (!BundleTags.empty()) 2439*0b57cec5SDimitry Andric return error("Invalid multiple blocks"); 2440*0b57cec5SDimitry Andric 2441*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 2442*0b57cec5SDimitry Andric 2443*0b57cec5SDimitry Andric while (true) { 2444*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 2445*0b57cec5SDimitry Andric if (!MaybeEntry) 2446*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 2447*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 2448*0b57cec5SDimitry Andric 2449*0b57cec5SDimitry Andric switch (Entry.Kind) { 2450*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 2451*0b57cec5SDimitry Andric case BitstreamEntry::Error: 2452*0b57cec5SDimitry Andric return error("Malformed block"); 2453*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 2454*0b57cec5SDimitry Andric return Error::success(); 2455*0b57cec5SDimitry Andric case BitstreamEntry::Record: 2456*0b57cec5SDimitry Andric // The interesting case. 2457*0b57cec5SDimitry Andric break; 2458*0b57cec5SDimitry Andric } 2459*0b57cec5SDimitry Andric 2460*0b57cec5SDimitry Andric // Tags are implicitly mapped to integers by their order. 2461*0b57cec5SDimitry Andric 2462*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 2463*0b57cec5SDimitry Andric if (!MaybeRecord) 2464*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 2465*0b57cec5SDimitry Andric if (MaybeRecord.get() != bitc::OPERAND_BUNDLE_TAG) 246681ad6265SDimitry Andric return error("Invalid operand bundle record"); 2467*0b57cec5SDimitry Andric 2468*0b57cec5SDimitry Andric // OPERAND_BUNDLE_TAG: [strchr x N] 2469*0b57cec5SDimitry Andric BundleTags.emplace_back(); 2470*0b57cec5SDimitry Andric if (convertToString(Record, 0, BundleTags.back())) 247181ad6265SDimitry Andric return error("Invalid operand bundle record"); 2472*0b57cec5SDimitry Andric Record.clear(); 2473*0b57cec5SDimitry Andric } 2474*0b57cec5SDimitry Andric } 2475*0b57cec5SDimitry Andric 2476*0b57cec5SDimitry Andric Error BitcodeReader::parseSyncScopeNames() { 2477*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::SYNC_SCOPE_NAMES_BLOCK_ID)) 2478*0b57cec5SDimitry Andric return Err; 2479*0b57cec5SDimitry Andric 2480*0b57cec5SDimitry Andric if (!SSIDs.empty()) 2481*0b57cec5SDimitry Andric return error("Invalid multiple synchronization scope names blocks"); 2482*0b57cec5SDimitry Andric 2483*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 2484*0b57cec5SDimitry Andric while (true) { 2485*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 2486*0b57cec5SDimitry Andric if (!MaybeEntry) 2487*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 2488*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 2489*0b57cec5SDimitry Andric 2490*0b57cec5SDimitry Andric switch (Entry.Kind) { 2491*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 2492*0b57cec5SDimitry Andric case BitstreamEntry::Error: 2493*0b57cec5SDimitry Andric return error("Malformed block"); 2494*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 2495*0b57cec5SDimitry Andric if (SSIDs.empty()) 2496*0b57cec5SDimitry Andric return error("Invalid empty synchronization scope names block"); 2497*0b57cec5SDimitry Andric return Error::success(); 2498*0b57cec5SDimitry Andric case BitstreamEntry::Record: 2499*0b57cec5SDimitry Andric // The interesting case. 2500*0b57cec5SDimitry Andric break; 2501*0b57cec5SDimitry Andric } 2502*0b57cec5SDimitry Andric 2503*0b57cec5SDimitry Andric // Synchronization scope names are implicitly mapped to synchronization 2504*0b57cec5SDimitry Andric // scope IDs by their order. 2505*0b57cec5SDimitry Andric 2506*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 2507*0b57cec5SDimitry Andric if (!MaybeRecord) 2508*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 2509*0b57cec5SDimitry Andric if (MaybeRecord.get() != bitc::SYNC_SCOPE_NAME) 251081ad6265SDimitry Andric return error("Invalid sync scope record"); 2511*0b57cec5SDimitry Andric 2512*0b57cec5SDimitry Andric SmallString<16> SSN; 2513*0b57cec5SDimitry Andric if (convertToString(Record, 0, SSN)) 251481ad6265SDimitry Andric return error("Invalid sync scope record"); 2515*0b57cec5SDimitry Andric 2516*0b57cec5SDimitry Andric SSIDs.push_back(Context.getOrInsertSyncScopeID(SSN)); 2517*0b57cec5SDimitry Andric Record.clear(); 2518*0b57cec5SDimitry Andric } 2519*0b57cec5SDimitry Andric } 2520*0b57cec5SDimitry Andric 2521*0b57cec5SDimitry Andric /// Associate a value with its name from the given index in the provided record. 2522*0b57cec5SDimitry Andric Expected<Value *> BitcodeReader::recordValue(SmallVectorImpl<uint64_t> &Record, 2523*0b57cec5SDimitry Andric unsigned NameIndex, Triple &TT) { 2524*0b57cec5SDimitry Andric SmallString<128> ValueName; 2525*0b57cec5SDimitry Andric if (convertToString(Record, NameIndex, ValueName)) 2526*0b57cec5SDimitry Andric return error("Invalid record"); 2527*0b57cec5SDimitry Andric unsigned ValueID = Record[0]; 2528*0b57cec5SDimitry Andric if (ValueID >= ValueList.size() || !ValueList[ValueID]) 2529*0b57cec5SDimitry Andric return error("Invalid record"); 2530*0b57cec5SDimitry Andric Value *V = ValueList[ValueID]; 2531*0b57cec5SDimitry Andric 2532*0b57cec5SDimitry Andric StringRef NameStr(ValueName.data(), ValueName.size()); 2533*0b57cec5SDimitry Andric if (NameStr.find_first_of(0) != StringRef::npos) 2534*0b57cec5SDimitry Andric return error("Invalid value name"); 2535*0b57cec5SDimitry Andric V->setName(NameStr); 2536*0b57cec5SDimitry Andric auto *GO = dyn_cast<GlobalObject>(V); 25370eae32dcSDimitry Andric if (GO && ImplicitComdatObjects.contains(GO) && TT.supportsCOMDAT()) 2538*0b57cec5SDimitry Andric GO->setComdat(TheModule->getOrInsertComdat(V->getName())); 2539*0b57cec5SDimitry Andric return V; 2540*0b57cec5SDimitry Andric } 2541*0b57cec5SDimitry Andric 2542*0b57cec5SDimitry Andric /// Helper to note and return the current location, and jump to the given 2543*0b57cec5SDimitry Andric /// offset. 2544*0b57cec5SDimitry Andric static Expected<uint64_t> jumpToValueSymbolTable(uint64_t Offset, 2545*0b57cec5SDimitry Andric BitstreamCursor &Stream) { 2546*0b57cec5SDimitry Andric // Save the current parsing location so we can jump back at the end 2547*0b57cec5SDimitry Andric // of the VST read. 2548*0b57cec5SDimitry Andric uint64_t CurrentBit = Stream.GetCurrentBitNo(); 2549*0b57cec5SDimitry Andric if (Error JumpFailed = Stream.JumpToBit(Offset * 32)) 2550*0b57cec5SDimitry Andric return std::move(JumpFailed); 2551*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advance(); 2552*0b57cec5SDimitry Andric if (!MaybeEntry) 2553*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 255481ad6265SDimitry Andric if (MaybeEntry.get().Kind != BitstreamEntry::SubBlock || 255581ad6265SDimitry Andric MaybeEntry.get().ID != bitc::VALUE_SYMTAB_BLOCK_ID) 255681ad6265SDimitry Andric return error("Expected value symbol table subblock"); 2557*0b57cec5SDimitry Andric return CurrentBit; 2558*0b57cec5SDimitry Andric } 2559*0b57cec5SDimitry Andric 2560*0b57cec5SDimitry Andric void BitcodeReader::setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta, 2561*0b57cec5SDimitry Andric Function *F, 2562*0b57cec5SDimitry Andric ArrayRef<uint64_t> Record) { 2563*0b57cec5SDimitry Andric // Note that we subtract 1 here because the offset is relative to one word 2564*0b57cec5SDimitry Andric // before the start of the identification or module block, which was 2565*0b57cec5SDimitry Andric // historically always the start of the regular bitcode header. 2566*0b57cec5SDimitry Andric uint64_t FuncWordOffset = Record[1] - 1; 2567*0b57cec5SDimitry Andric uint64_t FuncBitOffset = FuncWordOffset * 32; 2568*0b57cec5SDimitry Andric DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta; 2569*0b57cec5SDimitry Andric // Set the LastFunctionBlockBit to point to the last function block. 2570*0b57cec5SDimitry Andric // Later when parsing is resumed after function materialization, 2571*0b57cec5SDimitry Andric // we can simply skip that last function block. 2572*0b57cec5SDimitry Andric if (FuncBitOffset > LastFunctionBlockBit) 2573*0b57cec5SDimitry Andric LastFunctionBlockBit = FuncBitOffset; 2574*0b57cec5SDimitry Andric } 2575*0b57cec5SDimitry Andric 2576*0b57cec5SDimitry Andric /// Read a new-style GlobalValue symbol table. 2577*0b57cec5SDimitry Andric Error BitcodeReader::parseGlobalValueSymbolTable() { 2578*0b57cec5SDimitry Andric unsigned FuncBitcodeOffsetDelta = 2579*0b57cec5SDimitry Andric Stream.getAbbrevIDWidth() + bitc::BlockIDWidth; 2580*0b57cec5SDimitry Andric 2581*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) 2582*0b57cec5SDimitry Andric return Err; 2583*0b57cec5SDimitry Andric 2584*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 2585*0b57cec5SDimitry Andric while (true) { 2586*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 2587*0b57cec5SDimitry Andric if (!MaybeEntry) 2588*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 2589*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 2590*0b57cec5SDimitry Andric 2591*0b57cec5SDimitry Andric switch (Entry.Kind) { 2592*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: 2593*0b57cec5SDimitry Andric case BitstreamEntry::Error: 2594*0b57cec5SDimitry Andric return error("Malformed block"); 2595*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 2596*0b57cec5SDimitry Andric return Error::success(); 2597*0b57cec5SDimitry Andric case BitstreamEntry::Record: 2598*0b57cec5SDimitry Andric break; 2599*0b57cec5SDimitry Andric } 2600*0b57cec5SDimitry Andric 2601*0b57cec5SDimitry Andric Record.clear(); 2602*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 2603*0b57cec5SDimitry Andric if (!MaybeRecord) 2604*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 2605*0b57cec5SDimitry Andric switch (MaybeRecord.get()) { 260681ad6265SDimitry Andric case bitc::VST_CODE_FNENTRY: { // [valueid, offset] 260781ad6265SDimitry Andric unsigned ValueID = Record[0]; 260881ad6265SDimitry Andric if (ValueID >= ValueList.size() || !ValueList[ValueID]) 260981ad6265SDimitry Andric return error("Invalid value reference in symbol table"); 2610*0b57cec5SDimitry Andric setDeferredFunctionInfo(FuncBitcodeOffsetDelta, 261181ad6265SDimitry Andric cast<Function>(ValueList[ValueID]), Record); 2612*0b57cec5SDimitry Andric break; 2613*0b57cec5SDimitry Andric } 2614*0b57cec5SDimitry Andric } 2615*0b57cec5SDimitry Andric } 261681ad6265SDimitry Andric } 2617*0b57cec5SDimitry Andric 2618*0b57cec5SDimitry Andric /// Parse the value symbol table at either the current parsing location or 2619*0b57cec5SDimitry Andric /// at the given bit offset if provided. 2620*0b57cec5SDimitry Andric Error BitcodeReader::parseValueSymbolTable(uint64_t Offset) { 2621*0b57cec5SDimitry Andric uint64_t CurrentBit; 2622*0b57cec5SDimitry Andric // Pass in the Offset to distinguish between calling for the module-level 2623*0b57cec5SDimitry Andric // VST (where we want to jump to the VST offset) and the function-level 2624*0b57cec5SDimitry Andric // VST (where we don't). 2625*0b57cec5SDimitry Andric if (Offset > 0) { 2626*0b57cec5SDimitry Andric Expected<uint64_t> MaybeCurrentBit = jumpToValueSymbolTable(Offset, Stream); 2627*0b57cec5SDimitry Andric if (!MaybeCurrentBit) 2628*0b57cec5SDimitry Andric return MaybeCurrentBit.takeError(); 2629*0b57cec5SDimitry Andric CurrentBit = MaybeCurrentBit.get(); 2630*0b57cec5SDimitry Andric // If this module uses a string table, read this as a module-level VST. 2631*0b57cec5SDimitry Andric if (UseStrtab) { 2632*0b57cec5SDimitry Andric if (Error Err = parseGlobalValueSymbolTable()) 2633*0b57cec5SDimitry Andric return Err; 2634*0b57cec5SDimitry Andric if (Error JumpFailed = Stream.JumpToBit(CurrentBit)) 2635*0b57cec5SDimitry Andric return JumpFailed; 2636*0b57cec5SDimitry Andric return Error::success(); 2637*0b57cec5SDimitry Andric } 2638*0b57cec5SDimitry Andric // Otherwise, the VST will be in a similar format to a function-level VST, 2639*0b57cec5SDimitry Andric // and will contain symbol names. 2640*0b57cec5SDimitry Andric } 2641*0b57cec5SDimitry Andric 2642*0b57cec5SDimitry Andric // Compute the delta between the bitcode indices in the VST (the word offset 2643*0b57cec5SDimitry Andric // to the word-aligned ENTER_SUBBLOCK for the function block, and that 2644*0b57cec5SDimitry Andric // expected by the lazy reader. The reader's EnterSubBlock expects to have 2645*0b57cec5SDimitry Andric // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID 2646*0b57cec5SDimitry Andric // (size BlockIDWidth). Note that we access the stream's AbbrevID width here 2647*0b57cec5SDimitry Andric // just before entering the VST subblock because: 1) the EnterSubBlock 2648*0b57cec5SDimitry Andric // changes the AbbrevID width; 2) the VST block is nested within the same 2649*0b57cec5SDimitry Andric // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same 2650*0b57cec5SDimitry Andric // AbbrevID width before calling EnterSubBlock; and 3) when we want to 2651*0b57cec5SDimitry Andric // jump to the FUNCTION_BLOCK using this offset later, we don't want 2652*0b57cec5SDimitry Andric // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK. 2653*0b57cec5SDimitry Andric unsigned FuncBitcodeOffsetDelta = 2654*0b57cec5SDimitry Andric Stream.getAbbrevIDWidth() + bitc::BlockIDWidth; 2655*0b57cec5SDimitry Andric 2656*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) 2657*0b57cec5SDimitry Andric return Err; 2658*0b57cec5SDimitry Andric 2659*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 2660*0b57cec5SDimitry Andric 2661*0b57cec5SDimitry Andric Triple TT(TheModule->getTargetTriple()); 2662*0b57cec5SDimitry Andric 2663*0b57cec5SDimitry Andric // Read all the records for this value table. 2664*0b57cec5SDimitry Andric SmallString<128> ValueName; 2665*0b57cec5SDimitry Andric 2666*0b57cec5SDimitry Andric while (true) { 2667*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 2668*0b57cec5SDimitry Andric if (!MaybeEntry) 2669*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 2670*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 2671*0b57cec5SDimitry Andric 2672*0b57cec5SDimitry Andric switch (Entry.Kind) { 2673*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 2674*0b57cec5SDimitry Andric case BitstreamEntry::Error: 2675*0b57cec5SDimitry Andric return error("Malformed block"); 2676*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 2677*0b57cec5SDimitry Andric if (Offset > 0) 2678*0b57cec5SDimitry Andric if (Error JumpFailed = Stream.JumpToBit(CurrentBit)) 2679*0b57cec5SDimitry Andric return JumpFailed; 2680*0b57cec5SDimitry Andric return Error::success(); 2681*0b57cec5SDimitry Andric case BitstreamEntry::Record: 2682*0b57cec5SDimitry Andric // The interesting case. 2683*0b57cec5SDimitry Andric break; 2684*0b57cec5SDimitry Andric } 2685*0b57cec5SDimitry Andric 2686*0b57cec5SDimitry Andric // Read a record. 2687*0b57cec5SDimitry Andric Record.clear(); 2688*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 2689*0b57cec5SDimitry Andric if (!MaybeRecord) 2690*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 2691*0b57cec5SDimitry Andric switch (MaybeRecord.get()) { 2692*0b57cec5SDimitry Andric default: // Default behavior: unknown type. 2693*0b57cec5SDimitry Andric break; 2694*0b57cec5SDimitry Andric case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N] 2695*0b57cec5SDimitry Andric Expected<Value *> ValOrErr = recordValue(Record, 1, TT); 2696*0b57cec5SDimitry Andric if (Error Err = ValOrErr.takeError()) 2697*0b57cec5SDimitry Andric return Err; 2698*0b57cec5SDimitry Andric ValOrErr.get(); 2699*0b57cec5SDimitry Andric break; 2700*0b57cec5SDimitry Andric } 2701*0b57cec5SDimitry Andric case bitc::VST_CODE_FNENTRY: { 2702*0b57cec5SDimitry Andric // VST_CODE_FNENTRY: [valueid, offset, namechar x N] 2703*0b57cec5SDimitry Andric Expected<Value *> ValOrErr = recordValue(Record, 2, TT); 2704*0b57cec5SDimitry Andric if (Error Err = ValOrErr.takeError()) 2705*0b57cec5SDimitry Andric return Err; 2706*0b57cec5SDimitry Andric Value *V = ValOrErr.get(); 2707*0b57cec5SDimitry Andric 2708*0b57cec5SDimitry Andric // Ignore function offsets emitted for aliases of functions in older 2709*0b57cec5SDimitry Andric // versions of LLVM. 2710*0b57cec5SDimitry Andric if (auto *F = dyn_cast<Function>(V)) 2711*0b57cec5SDimitry Andric setDeferredFunctionInfo(FuncBitcodeOffsetDelta, F, Record); 2712*0b57cec5SDimitry Andric break; 2713*0b57cec5SDimitry Andric } 2714*0b57cec5SDimitry Andric case bitc::VST_CODE_BBENTRY: { 2715*0b57cec5SDimitry Andric if (convertToString(Record, 1, ValueName)) 271681ad6265SDimitry Andric return error("Invalid bbentry record"); 2717*0b57cec5SDimitry Andric BasicBlock *BB = getBasicBlock(Record[0]); 2718*0b57cec5SDimitry Andric if (!BB) 271981ad6265SDimitry Andric return error("Invalid bbentry record"); 2720*0b57cec5SDimitry Andric 2721*0b57cec5SDimitry Andric BB->setName(StringRef(ValueName.data(), ValueName.size())); 2722*0b57cec5SDimitry Andric ValueName.clear(); 2723*0b57cec5SDimitry Andric break; 2724*0b57cec5SDimitry Andric } 2725*0b57cec5SDimitry Andric } 2726*0b57cec5SDimitry Andric } 2727*0b57cec5SDimitry Andric } 2728*0b57cec5SDimitry Andric 2729*0b57cec5SDimitry Andric /// Decode a signed value stored with the sign bit in the LSB for dense VBR 2730*0b57cec5SDimitry Andric /// encoding. 2731*0b57cec5SDimitry Andric uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) { 2732*0b57cec5SDimitry Andric if ((V & 1) == 0) 2733*0b57cec5SDimitry Andric return V >> 1; 2734*0b57cec5SDimitry Andric if (V != 1) 2735*0b57cec5SDimitry Andric return -(V >> 1); 2736*0b57cec5SDimitry Andric // There is no such thing as -0 with integers. "-0" really means MININT. 2737*0b57cec5SDimitry Andric return 1ULL << 63; 2738*0b57cec5SDimitry Andric } 2739*0b57cec5SDimitry Andric 2740*0b57cec5SDimitry Andric /// Resolve all of the initializers for global values and aliases that we can. 2741*0b57cec5SDimitry Andric Error BitcodeReader::resolveGlobalAndIndirectSymbolInits() { 2742*0b57cec5SDimitry Andric std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInitWorklist; 2743349cc55cSDimitry Andric std::vector<std::pair<GlobalValue *, unsigned>> IndirectSymbolInitWorklist; 2744349cc55cSDimitry Andric std::vector<FunctionOperandInfo> FunctionOperandWorklist; 2745*0b57cec5SDimitry Andric 2746*0b57cec5SDimitry Andric GlobalInitWorklist.swap(GlobalInits); 2747*0b57cec5SDimitry Andric IndirectSymbolInitWorklist.swap(IndirectSymbolInits); 2748349cc55cSDimitry Andric FunctionOperandWorklist.swap(FunctionOperands); 2749*0b57cec5SDimitry Andric 2750*0b57cec5SDimitry Andric while (!GlobalInitWorklist.empty()) { 2751*0b57cec5SDimitry Andric unsigned ValID = GlobalInitWorklist.back().second; 2752*0b57cec5SDimitry Andric if (ValID >= ValueList.size()) { 2753*0b57cec5SDimitry Andric // Not ready to resolve this yet, it requires something later in the file. 2754*0b57cec5SDimitry Andric GlobalInits.push_back(GlobalInitWorklist.back()); 2755*0b57cec5SDimitry Andric } else { 275681ad6265SDimitry Andric Expected<Constant *> MaybeC = getValueForInitializer(ValID); 275781ad6265SDimitry Andric if (!MaybeC) 275881ad6265SDimitry Andric return MaybeC.takeError(); 275981ad6265SDimitry Andric GlobalInitWorklist.back().first->setInitializer(MaybeC.get()); 2760*0b57cec5SDimitry Andric } 2761*0b57cec5SDimitry Andric GlobalInitWorklist.pop_back(); 2762*0b57cec5SDimitry Andric } 2763*0b57cec5SDimitry Andric 2764*0b57cec5SDimitry Andric while (!IndirectSymbolInitWorklist.empty()) { 2765*0b57cec5SDimitry Andric unsigned ValID = IndirectSymbolInitWorklist.back().second; 2766*0b57cec5SDimitry Andric if (ValID >= ValueList.size()) { 2767*0b57cec5SDimitry Andric IndirectSymbolInits.push_back(IndirectSymbolInitWorklist.back()); 2768*0b57cec5SDimitry Andric } else { 276981ad6265SDimitry Andric Expected<Constant *> MaybeC = getValueForInitializer(ValID); 277081ad6265SDimitry Andric if (!MaybeC) 277181ad6265SDimitry Andric return MaybeC.takeError(); 277281ad6265SDimitry Andric Constant *C = MaybeC.get(); 2773349cc55cSDimitry Andric GlobalValue *GV = IndirectSymbolInitWorklist.back().first; 2774349cc55cSDimitry Andric if (auto *GA = dyn_cast<GlobalAlias>(GV)) { 2775349cc55cSDimitry Andric if (C->getType() != GV->getType()) 2776*0b57cec5SDimitry Andric return error("Alias and aliasee types don't match"); 2777349cc55cSDimitry Andric GA->setAliasee(C); 2778349cc55cSDimitry Andric } else if (auto *GI = dyn_cast<GlobalIFunc>(GV)) { 2779349cc55cSDimitry Andric Type *ResolverFTy = 2780349cc55cSDimitry Andric GlobalIFunc::getResolverFunctionType(GI->getValueType()); 2781349cc55cSDimitry Andric // Transparently fix up the type for compatiblity with older bitcode 2782349cc55cSDimitry Andric GI->setResolver( 2783349cc55cSDimitry Andric ConstantExpr::getBitCast(C, ResolverFTy->getPointerTo())); 2784349cc55cSDimitry Andric } else { 2785349cc55cSDimitry Andric return error("Expected an alias or an ifunc"); 2786349cc55cSDimitry Andric } 2787*0b57cec5SDimitry Andric } 2788*0b57cec5SDimitry Andric IndirectSymbolInitWorklist.pop_back(); 2789*0b57cec5SDimitry Andric } 2790*0b57cec5SDimitry Andric 2791349cc55cSDimitry Andric while (!FunctionOperandWorklist.empty()) { 2792349cc55cSDimitry Andric FunctionOperandInfo &Info = FunctionOperandWorklist.back(); 2793349cc55cSDimitry Andric if (Info.PersonalityFn) { 2794349cc55cSDimitry Andric unsigned ValID = Info.PersonalityFn - 1; 2795349cc55cSDimitry Andric if (ValID < ValueList.size()) { 279681ad6265SDimitry Andric Expected<Constant *> MaybeC = getValueForInitializer(ValID); 279781ad6265SDimitry Andric if (!MaybeC) 279881ad6265SDimitry Andric return MaybeC.takeError(); 279981ad6265SDimitry Andric Info.F->setPersonalityFn(MaybeC.get()); 2800349cc55cSDimitry Andric Info.PersonalityFn = 0; 2801*0b57cec5SDimitry Andric } 2802*0b57cec5SDimitry Andric } 2803349cc55cSDimitry Andric if (Info.Prefix) { 2804349cc55cSDimitry Andric unsigned ValID = Info.Prefix - 1; 2805349cc55cSDimitry Andric if (ValID < ValueList.size()) { 280681ad6265SDimitry Andric Expected<Constant *> MaybeC = getValueForInitializer(ValID); 280781ad6265SDimitry Andric if (!MaybeC) 280881ad6265SDimitry Andric return MaybeC.takeError(); 280981ad6265SDimitry Andric Info.F->setPrefixData(MaybeC.get()); 2810349cc55cSDimitry Andric Info.Prefix = 0; 2811*0b57cec5SDimitry Andric } 2812*0b57cec5SDimitry Andric } 2813349cc55cSDimitry Andric if (Info.Prologue) { 2814349cc55cSDimitry Andric unsigned ValID = Info.Prologue - 1; 2815349cc55cSDimitry Andric if (ValID < ValueList.size()) { 281681ad6265SDimitry Andric Expected<Constant *> MaybeC = getValueForInitializer(ValID); 281781ad6265SDimitry Andric if (!MaybeC) 281881ad6265SDimitry Andric return MaybeC.takeError(); 281981ad6265SDimitry Andric Info.F->setPrologueData(MaybeC.get()); 2820349cc55cSDimitry Andric Info.Prologue = 0; 2821*0b57cec5SDimitry Andric } 2822349cc55cSDimitry Andric } 2823349cc55cSDimitry Andric if (Info.PersonalityFn || Info.Prefix || Info.Prologue) 2824349cc55cSDimitry Andric FunctionOperands.push_back(Info); 2825349cc55cSDimitry Andric FunctionOperandWorklist.pop_back(); 2826*0b57cec5SDimitry Andric } 2827*0b57cec5SDimitry Andric 2828*0b57cec5SDimitry Andric return Error::success(); 2829*0b57cec5SDimitry Andric } 2830*0b57cec5SDimitry Andric 28315ffd83dbSDimitry Andric APInt llvm::readWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) { 2832*0b57cec5SDimitry Andric SmallVector<uint64_t, 8> Words(Vals.size()); 2833*0b57cec5SDimitry Andric transform(Vals, Words.begin(), 2834*0b57cec5SDimitry Andric BitcodeReader::decodeSignRotatedValue); 2835*0b57cec5SDimitry Andric 2836*0b57cec5SDimitry Andric return APInt(TypeBits, Words); 2837*0b57cec5SDimitry Andric } 2838*0b57cec5SDimitry Andric 2839*0b57cec5SDimitry Andric Error BitcodeReader::parseConstants() { 2840*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID)) 2841*0b57cec5SDimitry Andric return Err; 2842*0b57cec5SDimitry Andric 2843*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 2844*0b57cec5SDimitry Andric 2845*0b57cec5SDimitry Andric // Read all the records for this value table. 2846*0b57cec5SDimitry Andric Type *CurTy = Type::getInt32Ty(Context); 284781ad6265SDimitry Andric unsigned Int32TyID = getVirtualTypeID(CurTy); 284881ad6265SDimitry Andric unsigned CurTyID = Int32TyID; 284981ad6265SDimitry Andric Type *CurElemTy = nullptr; 2850*0b57cec5SDimitry Andric unsigned NextCstNo = ValueList.size(); 2851*0b57cec5SDimitry Andric 2852*0b57cec5SDimitry Andric while (true) { 2853*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 2854*0b57cec5SDimitry Andric if (!MaybeEntry) 2855*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 2856*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 2857*0b57cec5SDimitry Andric 2858*0b57cec5SDimitry Andric switch (Entry.Kind) { 2859*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 2860*0b57cec5SDimitry Andric case BitstreamEntry::Error: 2861*0b57cec5SDimitry Andric return error("Malformed block"); 2862*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 2863*0b57cec5SDimitry Andric if (NextCstNo != ValueList.size()) 2864*0b57cec5SDimitry Andric return error("Invalid constant reference"); 2865*0b57cec5SDimitry Andric return Error::success(); 2866*0b57cec5SDimitry Andric case BitstreamEntry::Record: 2867*0b57cec5SDimitry Andric // The interesting case. 2868*0b57cec5SDimitry Andric break; 2869*0b57cec5SDimitry Andric } 2870*0b57cec5SDimitry Andric 2871*0b57cec5SDimitry Andric // Read a record. 2872*0b57cec5SDimitry Andric Record.clear(); 2873*0b57cec5SDimitry Andric Type *VoidType = Type::getVoidTy(Context); 2874*0b57cec5SDimitry Andric Value *V = nullptr; 2875*0b57cec5SDimitry Andric Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); 2876*0b57cec5SDimitry Andric if (!MaybeBitCode) 2877*0b57cec5SDimitry Andric return MaybeBitCode.takeError(); 2878*0b57cec5SDimitry Andric switch (unsigned BitCode = MaybeBitCode.get()) { 2879*0b57cec5SDimitry Andric default: // Default behavior: unknown constant 2880*0b57cec5SDimitry Andric case bitc::CST_CODE_UNDEF: // UNDEF 2881*0b57cec5SDimitry Andric V = UndefValue::get(CurTy); 2882*0b57cec5SDimitry Andric break; 2883e8d8bef9SDimitry Andric case bitc::CST_CODE_POISON: // POISON 2884e8d8bef9SDimitry Andric V = PoisonValue::get(CurTy); 2885e8d8bef9SDimitry Andric break; 2886*0b57cec5SDimitry Andric case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid] 2887*0b57cec5SDimitry Andric if (Record.empty()) 288881ad6265SDimitry Andric return error("Invalid settype record"); 2889*0b57cec5SDimitry Andric if (Record[0] >= TypeList.size() || !TypeList[Record[0]]) 289081ad6265SDimitry Andric return error("Invalid settype record"); 2891*0b57cec5SDimitry Andric if (TypeList[Record[0]] == VoidType) 2892*0b57cec5SDimitry Andric return error("Invalid constant type"); 289381ad6265SDimitry Andric CurTyID = Record[0]; 289481ad6265SDimitry Andric CurTy = TypeList[CurTyID]; 289581ad6265SDimitry Andric CurElemTy = getPtrElementTypeByID(CurTyID); 2896*0b57cec5SDimitry Andric continue; // Skip the ValueList manipulation. 2897*0b57cec5SDimitry Andric case bitc::CST_CODE_NULL: // NULL 28988bcb0991SDimitry Andric if (CurTy->isVoidTy() || CurTy->isFunctionTy() || CurTy->isLabelTy()) 28998bcb0991SDimitry Andric return error("Invalid type for a constant null value"); 2900*0b57cec5SDimitry Andric V = Constant::getNullValue(CurTy); 2901*0b57cec5SDimitry Andric break; 2902*0b57cec5SDimitry Andric case bitc::CST_CODE_INTEGER: // INTEGER: [intval] 2903*0b57cec5SDimitry Andric if (!CurTy->isIntegerTy() || Record.empty()) 290481ad6265SDimitry Andric return error("Invalid integer const record"); 2905*0b57cec5SDimitry Andric V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0])); 2906*0b57cec5SDimitry Andric break; 2907*0b57cec5SDimitry Andric case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval] 2908*0b57cec5SDimitry Andric if (!CurTy->isIntegerTy() || Record.empty()) 290981ad6265SDimitry Andric return error("Invalid wide integer const record"); 2910*0b57cec5SDimitry Andric 2911*0b57cec5SDimitry Andric APInt VInt = 2912*0b57cec5SDimitry Andric readWideAPInt(Record, cast<IntegerType>(CurTy)->getBitWidth()); 2913*0b57cec5SDimitry Andric V = ConstantInt::get(Context, VInt); 2914*0b57cec5SDimitry Andric 2915*0b57cec5SDimitry Andric break; 2916*0b57cec5SDimitry Andric } 2917*0b57cec5SDimitry Andric case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval] 2918*0b57cec5SDimitry Andric if (Record.empty()) 291981ad6265SDimitry Andric return error("Invalid float const record"); 2920*0b57cec5SDimitry Andric if (CurTy->isHalfTy()) 2921*0b57cec5SDimitry Andric V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf(), 2922*0b57cec5SDimitry Andric APInt(16, (uint16_t)Record[0]))); 29235ffd83dbSDimitry Andric else if (CurTy->isBFloatTy()) 29245ffd83dbSDimitry Andric V = ConstantFP::get(Context, APFloat(APFloat::BFloat(), 29255ffd83dbSDimitry Andric APInt(16, (uint32_t)Record[0]))); 2926*0b57cec5SDimitry Andric else if (CurTy->isFloatTy()) 2927*0b57cec5SDimitry Andric V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle(), 2928*0b57cec5SDimitry Andric APInt(32, (uint32_t)Record[0]))); 2929*0b57cec5SDimitry Andric else if (CurTy->isDoubleTy()) 2930*0b57cec5SDimitry Andric V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble(), 2931*0b57cec5SDimitry Andric APInt(64, Record[0]))); 2932*0b57cec5SDimitry Andric else if (CurTy->isX86_FP80Ty()) { 2933*0b57cec5SDimitry Andric // Bits are not stored the same way as a normal i80 APInt, compensate. 2934*0b57cec5SDimitry Andric uint64_t Rearrange[2]; 2935*0b57cec5SDimitry Andric Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16); 2936*0b57cec5SDimitry Andric Rearrange[1] = Record[0] >> 48; 2937*0b57cec5SDimitry Andric V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended(), 2938*0b57cec5SDimitry Andric APInt(80, Rearrange))); 2939*0b57cec5SDimitry Andric } else if (CurTy->isFP128Ty()) 2940*0b57cec5SDimitry Andric V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad(), 2941*0b57cec5SDimitry Andric APInt(128, Record))); 2942*0b57cec5SDimitry Andric else if (CurTy->isPPC_FP128Ty()) 2943*0b57cec5SDimitry Andric V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble(), 2944*0b57cec5SDimitry Andric APInt(128, Record))); 2945*0b57cec5SDimitry Andric else 2946*0b57cec5SDimitry Andric V = UndefValue::get(CurTy); 2947*0b57cec5SDimitry Andric break; 2948*0b57cec5SDimitry Andric } 2949*0b57cec5SDimitry Andric 2950*0b57cec5SDimitry Andric case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number] 2951*0b57cec5SDimitry Andric if (Record.empty()) 295281ad6265SDimitry Andric return error("Invalid aggregate record"); 2953*0b57cec5SDimitry Andric 2954*0b57cec5SDimitry Andric unsigned Size = Record.size(); 295581ad6265SDimitry Andric SmallVector<unsigned, 16> Elts; 295681ad6265SDimitry Andric for (unsigned i = 0; i != Size; ++i) 295781ad6265SDimitry Andric Elts.push_back(Record[i]); 2958*0b57cec5SDimitry Andric 295981ad6265SDimitry Andric if (isa<StructType>(CurTy)) { 296081ad6265SDimitry Andric V = BitcodeConstant::create( 296181ad6265SDimitry Andric Alloc, CurTy, BitcodeConstant::ConstantStructOpcode, Elts); 296281ad6265SDimitry Andric } else if (isa<ArrayType>(CurTy)) { 296381ad6265SDimitry Andric V = BitcodeConstant::create(Alloc, CurTy, 296481ad6265SDimitry Andric BitcodeConstant::ConstantArrayOpcode, Elts); 296581ad6265SDimitry Andric } else if (isa<VectorType>(CurTy)) { 296681ad6265SDimitry Andric V = BitcodeConstant::create( 296781ad6265SDimitry Andric Alloc, CurTy, BitcodeConstant::ConstantVectorOpcode, Elts); 2968*0b57cec5SDimitry Andric } else { 2969*0b57cec5SDimitry Andric V = UndefValue::get(CurTy); 2970*0b57cec5SDimitry Andric } 2971*0b57cec5SDimitry Andric break; 2972*0b57cec5SDimitry Andric } 2973*0b57cec5SDimitry Andric case bitc::CST_CODE_STRING: // STRING: [values] 2974*0b57cec5SDimitry Andric case bitc::CST_CODE_CSTRING: { // CSTRING: [values] 2975*0b57cec5SDimitry Andric if (Record.empty()) 297681ad6265SDimitry Andric return error("Invalid string record"); 2977*0b57cec5SDimitry Andric 2978*0b57cec5SDimitry Andric SmallString<16> Elts(Record.begin(), Record.end()); 2979*0b57cec5SDimitry Andric V = ConstantDataArray::getString(Context, Elts, 2980*0b57cec5SDimitry Andric BitCode == bitc::CST_CODE_CSTRING); 2981*0b57cec5SDimitry Andric break; 2982*0b57cec5SDimitry Andric } 2983*0b57cec5SDimitry Andric case bitc::CST_CODE_DATA: {// DATA: [n x value] 2984*0b57cec5SDimitry Andric if (Record.empty()) 298581ad6265SDimitry Andric return error("Invalid data record"); 2986*0b57cec5SDimitry Andric 29875ffd83dbSDimitry Andric Type *EltTy; 29885ffd83dbSDimitry Andric if (auto *Array = dyn_cast<ArrayType>(CurTy)) 29895ffd83dbSDimitry Andric EltTy = Array->getElementType(); 29905ffd83dbSDimitry Andric else 29915ffd83dbSDimitry Andric EltTy = cast<VectorType>(CurTy)->getElementType(); 2992*0b57cec5SDimitry Andric if (EltTy->isIntegerTy(8)) { 2993*0b57cec5SDimitry Andric SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end()); 2994*0b57cec5SDimitry Andric if (isa<VectorType>(CurTy)) 2995*0b57cec5SDimitry Andric V = ConstantDataVector::get(Context, Elts); 2996*0b57cec5SDimitry Andric else 2997*0b57cec5SDimitry Andric V = ConstantDataArray::get(Context, Elts); 2998*0b57cec5SDimitry Andric } else if (EltTy->isIntegerTy(16)) { 2999*0b57cec5SDimitry Andric SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); 3000*0b57cec5SDimitry Andric if (isa<VectorType>(CurTy)) 3001*0b57cec5SDimitry Andric V = ConstantDataVector::get(Context, Elts); 3002*0b57cec5SDimitry Andric else 3003*0b57cec5SDimitry Andric V = ConstantDataArray::get(Context, Elts); 3004*0b57cec5SDimitry Andric } else if (EltTy->isIntegerTy(32)) { 3005*0b57cec5SDimitry Andric SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); 3006*0b57cec5SDimitry Andric if (isa<VectorType>(CurTy)) 3007*0b57cec5SDimitry Andric V = ConstantDataVector::get(Context, Elts); 3008*0b57cec5SDimitry Andric else 3009*0b57cec5SDimitry Andric V = ConstantDataArray::get(Context, Elts); 3010*0b57cec5SDimitry Andric } else if (EltTy->isIntegerTy(64)) { 3011*0b57cec5SDimitry Andric SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); 3012*0b57cec5SDimitry Andric if (isa<VectorType>(CurTy)) 3013*0b57cec5SDimitry Andric V = ConstantDataVector::get(Context, Elts); 3014*0b57cec5SDimitry Andric else 3015*0b57cec5SDimitry Andric V = ConstantDataArray::get(Context, Elts); 3016*0b57cec5SDimitry Andric } else if (EltTy->isHalfTy()) { 3017*0b57cec5SDimitry Andric SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); 3018*0b57cec5SDimitry Andric if (isa<VectorType>(CurTy)) 30195ffd83dbSDimitry Andric V = ConstantDataVector::getFP(EltTy, Elts); 3020*0b57cec5SDimitry Andric else 30215ffd83dbSDimitry Andric V = ConstantDataArray::getFP(EltTy, Elts); 30225ffd83dbSDimitry Andric } else if (EltTy->isBFloatTy()) { 30235ffd83dbSDimitry Andric SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); 30245ffd83dbSDimitry Andric if (isa<VectorType>(CurTy)) 30255ffd83dbSDimitry Andric V = ConstantDataVector::getFP(EltTy, Elts); 30265ffd83dbSDimitry Andric else 30275ffd83dbSDimitry Andric V = ConstantDataArray::getFP(EltTy, Elts); 3028*0b57cec5SDimitry Andric } else if (EltTy->isFloatTy()) { 3029*0b57cec5SDimitry Andric SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); 3030*0b57cec5SDimitry Andric if (isa<VectorType>(CurTy)) 30315ffd83dbSDimitry Andric V = ConstantDataVector::getFP(EltTy, Elts); 3032*0b57cec5SDimitry Andric else 30335ffd83dbSDimitry Andric V = ConstantDataArray::getFP(EltTy, Elts); 3034*0b57cec5SDimitry Andric } else if (EltTy->isDoubleTy()) { 3035*0b57cec5SDimitry Andric SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); 3036*0b57cec5SDimitry Andric if (isa<VectorType>(CurTy)) 30375ffd83dbSDimitry Andric V = ConstantDataVector::getFP(EltTy, Elts); 3038*0b57cec5SDimitry Andric else 30395ffd83dbSDimitry Andric V = ConstantDataArray::getFP(EltTy, Elts); 3040*0b57cec5SDimitry Andric } else { 3041*0b57cec5SDimitry Andric return error("Invalid type for value"); 3042*0b57cec5SDimitry Andric } 3043*0b57cec5SDimitry Andric break; 3044*0b57cec5SDimitry Andric } 3045*0b57cec5SDimitry Andric case bitc::CST_CODE_CE_UNOP: { // CE_UNOP: [opcode, opval] 3046*0b57cec5SDimitry Andric if (Record.size() < 2) 304781ad6265SDimitry Andric return error("Invalid unary op constexpr record"); 3048*0b57cec5SDimitry Andric int Opc = getDecodedUnaryOpcode(Record[0], CurTy); 3049*0b57cec5SDimitry Andric if (Opc < 0) { 3050*0b57cec5SDimitry Andric V = UndefValue::get(CurTy); // Unknown unop. 3051*0b57cec5SDimitry Andric } else { 305281ad6265SDimitry Andric V = BitcodeConstant::create(Alloc, CurTy, Opc, (unsigned)Record[1]); 3053*0b57cec5SDimitry Andric } 3054*0b57cec5SDimitry Andric break; 3055*0b57cec5SDimitry Andric } 3056*0b57cec5SDimitry Andric case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval] 3057*0b57cec5SDimitry Andric if (Record.size() < 3) 305881ad6265SDimitry Andric return error("Invalid binary op constexpr record"); 3059*0b57cec5SDimitry Andric int Opc = getDecodedBinaryOpcode(Record[0], CurTy); 3060*0b57cec5SDimitry Andric if (Opc < 0) { 3061*0b57cec5SDimitry Andric V = UndefValue::get(CurTy); // Unknown binop. 3062*0b57cec5SDimitry Andric } else { 306381ad6265SDimitry Andric uint8_t Flags = 0; 3064*0b57cec5SDimitry Andric if (Record.size() >= 4) { 3065*0b57cec5SDimitry Andric if (Opc == Instruction::Add || 3066*0b57cec5SDimitry Andric Opc == Instruction::Sub || 3067*0b57cec5SDimitry Andric Opc == Instruction::Mul || 3068*0b57cec5SDimitry Andric Opc == Instruction::Shl) { 3069*0b57cec5SDimitry Andric if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP)) 3070*0b57cec5SDimitry Andric Flags |= OverflowingBinaryOperator::NoSignedWrap; 3071*0b57cec5SDimitry Andric if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) 3072*0b57cec5SDimitry Andric Flags |= OverflowingBinaryOperator::NoUnsignedWrap; 3073*0b57cec5SDimitry Andric } else if (Opc == Instruction::SDiv || 3074*0b57cec5SDimitry Andric Opc == Instruction::UDiv || 3075*0b57cec5SDimitry Andric Opc == Instruction::LShr || 3076*0b57cec5SDimitry Andric Opc == Instruction::AShr) { 3077*0b57cec5SDimitry Andric if (Record[3] & (1 << bitc::PEO_EXACT)) 3078*0b57cec5SDimitry Andric Flags |= SDivOperator::IsExact; 3079*0b57cec5SDimitry Andric } 3080*0b57cec5SDimitry Andric } 308181ad6265SDimitry Andric V = BitcodeConstant::create(Alloc, CurTy, {(uint8_t)Opc, Flags}, 308281ad6265SDimitry Andric {(unsigned)Record[1], (unsigned)Record[2]}); 3083*0b57cec5SDimitry Andric } 3084*0b57cec5SDimitry Andric break; 3085*0b57cec5SDimitry Andric } 3086*0b57cec5SDimitry Andric case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval] 3087*0b57cec5SDimitry Andric if (Record.size() < 3) 308881ad6265SDimitry Andric return error("Invalid cast constexpr record"); 3089*0b57cec5SDimitry Andric int Opc = getDecodedCastOpcode(Record[0]); 3090*0b57cec5SDimitry Andric if (Opc < 0) { 3091*0b57cec5SDimitry Andric V = UndefValue::get(CurTy); // Unknown cast. 3092*0b57cec5SDimitry Andric } else { 309381ad6265SDimitry Andric unsigned OpTyID = Record[1]; 309481ad6265SDimitry Andric Type *OpTy = getTypeByID(OpTyID); 3095*0b57cec5SDimitry Andric if (!OpTy) 309681ad6265SDimitry Andric return error("Invalid cast constexpr record"); 309781ad6265SDimitry Andric V = BitcodeConstant::create(Alloc, CurTy, Opc, (unsigned)Record[2]); 3098*0b57cec5SDimitry Andric } 3099*0b57cec5SDimitry Andric break; 3100*0b57cec5SDimitry Andric } 3101*0b57cec5SDimitry Andric case bitc::CST_CODE_CE_INBOUNDS_GEP: // [ty, n x operands] 3102*0b57cec5SDimitry Andric case bitc::CST_CODE_CE_GEP: // [ty, n x operands] 3103*0b57cec5SDimitry Andric case bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX: { // [ty, flags, n x 3104*0b57cec5SDimitry Andric // operands] 310581ad6265SDimitry Andric if (Record.size() < 2) 310681ad6265SDimitry Andric return error("Constant GEP record must have at least two elements"); 3107*0b57cec5SDimitry Andric unsigned OpNum = 0; 3108*0b57cec5SDimitry Andric Type *PointeeType = nullptr; 3109*0b57cec5SDimitry Andric if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX || 3110*0b57cec5SDimitry Andric Record.size() % 2) 3111*0b57cec5SDimitry Andric PointeeType = getTypeByID(Record[OpNum++]); 3112*0b57cec5SDimitry Andric 3113*0b57cec5SDimitry Andric bool InBounds = false; 3114*0b57cec5SDimitry Andric Optional<unsigned> InRangeIndex; 3115*0b57cec5SDimitry Andric if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX) { 3116*0b57cec5SDimitry Andric uint64_t Op = Record[OpNum++]; 3117*0b57cec5SDimitry Andric InBounds = Op & 1; 3118*0b57cec5SDimitry Andric InRangeIndex = Op >> 1; 3119*0b57cec5SDimitry Andric } else if (BitCode == bitc::CST_CODE_CE_INBOUNDS_GEP) 3120*0b57cec5SDimitry Andric InBounds = true; 3121*0b57cec5SDimitry Andric 312281ad6265SDimitry Andric SmallVector<unsigned, 16> Elts; 312381ad6265SDimitry Andric unsigned BaseTypeID = Record[OpNum]; 3124*0b57cec5SDimitry Andric while (OpNum != Record.size()) { 312581ad6265SDimitry Andric unsigned ElTyID = Record[OpNum++]; 312681ad6265SDimitry Andric Type *ElTy = getTypeByID(ElTyID); 3127*0b57cec5SDimitry Andric if (!ElTy) 312881ad6265SDimitry Andric return error("Invalid getelementptr constexpr record"); 312981ad6265SDimitry Andric Elts.push_back(Record[OpNum++]); 3130*0b57cec5SDimitry Andric } 3131*0b57cec5SDimitry Andric 3132*0b57cec5SDimitry Andric if (Elts.size() < 1) 3133*0b57cec5SDimitry Andric return error("Invalid gep with no operands"); 3134*0b57cec5SDimitry Andric 313581ad6265SDimitry Andric Type *BaseType = getTypeByID(BaseTypeID); 313681ad6265SDimitry Andric if (isa<VectorType>(BaseType)) { 313781ad6265SDimitry Andric BaseTypeID = getContainedTypeID(BaseTypeID, 0); 313881ad6265SDimitry Andric BaseType = getTypeByID(BaseTypeID); 313981ad6265SDimitry Andric } 314081ad6265SDimitry Andric 314181ad6265SDimitry Andric PointerType *OrigPtrTy = dyn_cast_or_null<PointerType>(BaseType); 314281ad6265SDimitry Andric if (!OrigPtrTy) 314381ad6265SDimitry Andric return error("GEP base operand must be pointer or vector of pointer"); 314481ad6265SDimitry Andric 314581ad6265SDimitry Andric if (!PointeeType) { 314681ad6265SDimitry Andric PointeeType = getPtrElementTypeByID(BaseTypeID); 3147*0b57cec5SDimitry Andric if (!PointeeType) 314881ad6265SDimitry Andric return error("Missing element type for old-style constant GEP"); 314981ad6265SDimitry Andric } else if (!OrigPtrTy->isOpaqueOrPointeeTypeMatches(PointeeType)) 3150*0b57cec5SDimitry Andric return error("Explicit gep operator type does not match pointee type " 3151*0b57cec5SDimitry Andric "of pointer operand"); 3152*0b57cec5SDimitry Andric 315381ad6265SDimitry Andric V = BitcodeConstant::create(Alloc, CurTy, 315481ad6265SDimitry Andric {Instruction::GetElementPtr, InBounds, 315581ad6265SDimitry Andric InRangeIndex.value_or(-1), PointeeType}, 315681ad6265SDimitry Andric Elts); 3157*0b57cec5SDimitry Andric break; 3158*0b57cec5SDimitry Andric } 3159*0b57cec5SDimitry Andric case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#] 3160*0b57cec5SDimitry Andric if (Record.size() < 3) 316181ad6265SDimitry Andric return error("Invalid select constexpr record"); 3162*0b57cec5SDimitry Andric 316381ad6265SDimitry Andric V = BitcodeConstant::create( 316481ad6265SDimitry Andric Alloc, CurTy, Instruction::Select, 316581ad6265SDimitry Andric {(unsigned)Record[0], (unsigned)Record[1], (unsigned)Record[2]}); 316681ad6265SDimitry Andric break; 3167*0b57cec5SDimitry Andric } 3168*0b57cec5SDimitry Andric case bitc::CST_CODE_CE_EXTRACTELT 3169*0b57cec5SDimitry Andric : { // CE_EXTRACTELT: [opty, opval, opty, opval] 3170*0b57cec5SDimitry Andric if (Record.size() < 3) 317181ad6265SDimitry Andric return error("Invalid extractelement constexpr record"); 317281ad6265SDimitry Andric unsigned OpTyID = Record[0]; 3173*0b57cec5SDimitry Andric VectorType *OpTy = 317481ad6265SDimitry Andric dyn_cast_or_null<VectorType>(getTypeByID(OpTyID)); 3175*0b57cec5SDimitry Andric if (!OpTy) 317681ad6265SDimitry Andric return error("Invalid extractelement constexpr record"); 317781ad6265SDimitry Andric unsigned IdxRecord; 3178*0b57cec5SDimitry Andric if (Record.size() == 4) { 317981ad6265SDimitry Andric unsigned IdxTyID = Record[2]; 318081ad6265SDimitry Andric Type *IdxTy = getTypeByID(IdxTyID); 3181*0b57cec5SDimitry Andric if (!IdxTy) 318281ad6265SDimitry Andric return error("Invalid extractelement constexpr record"); 318381ad6265SDimitry Andric IdxRecord = Record[3]; 31845ffd83dbSDimitry Andric } else { 31855ffd83dbSDimitry Andric // Deprecated, but still needed to read old bitcode files. 318681ad6265SDimitry Andric IdxRecord = Record[2]; 31875ffd83dbSDimitry Andric } 318881ad6265SDimitry Andric V = BitcodeConstant::create(Alloc, CurTy, Instruction::ExtractElement, 318981ad6265SDimitry Andric {(unsigned)Record[1], IdxRecord}); 3190*0b57cec5SDimitry Andric break; 3191*0b57cec5SDimitry Andric } 3192*0b57cec5SDimitry Andric case bitc::CST_CODE_CE_INSERTELT 3193*0b57cec5SDimitry Andric : { // CE_INSERTELT: [opval, opval, opty, opval] 3194*0b57cec5SDimitry Andric VectorType *OpTy = dyn_cast<VectorType>(CurTy); 3195*0b57cec5SDimitry Andric if (Record.size() < 3 || !OpTy) 319681ad6265SDimitry Andric return error("Invalid insertelement constexpr record"); 319781ad6265SDimitry Andric unsigned IdxRecord; 3198*0b57cec5SDimitry Andric if (Record.size() == 4) { 319981ad6265SDimitry Andric unsigned IdxTyID = Record[2]; 320081ad6265SDimitry Andric Type *IdxTy = getTypeByID(IdxTyID); 3201*0b57cec5SDimitry Andric if (!IdxTy) 320281ad6265SDimitry Andric return error("Invalid insertelement constexpr record"); 320381ad6265SDimitry Andric IdxRecord = Record[3]; 32045ffd83dbSDimitry Andric } else { 32055ffd83dbSDimitry Andric // Deprecated, but still needed to read old bitcode files. 320681ad6265SDimitry Andric IdxRecord = Record[2]; 32075ffd83dbSDimitry Andric } 320881ad6265SDimitry Andric V = BitcodeConstant::create( 320981ad6265SDimitry Andric Alloc, CurTy, Instruction::InsertElement, 321081ad6265SDimitry Andric {(unsigned)Record[0], (unsigned)Record[1], IdxRecord}); 3211*0b57cec5SDimitry Andric break; 3212*0b57cec5SDimitry Andric } 3213*0b57cec5SDimitry Andric case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval] 3214*0b57cec5SDimitry Andric VectorType *OpTy = dyn_cast<VectorType>(CurTy); 3215*0b57cec5SDimitry Andric if (Record.size() < 3 || !OpTy) 321681ad6265SDimitry Andric return error("Invalid shufflevector constexpr record"); 321781ad6265SDimitry Andric V = BitcodeConstant::create( 321881ad6265SDimitry Andric Alloc, CurTy, Instruction::ShuffleVector, 321981ad6265SDimitry Andric {(unsigned)Record[0], (unsigned)Record[1], (unsigned)Record[2]}); 322081ad6265SDimitry Andric break; 3221*0b57cec5SDimitry Andric } 3222*0b57cec5SDimitry Andric case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval] 3223*0b57cec5SDimitry Andric VectorType *RTy = dyn_cast<VectorType>(CurTy); 3224*0b57cec5SDimitry Andric VectorType *OpTy = 3225*0b57cec5SDimitry Andric dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); 3226*0b57cec5SDimitry Andric if (Record.size() < 4 || !RTy || !OpTy) 322781ad6265SDimitry Andric return error("Invalid shufflevector constexpr record"); 322881ad6265SDimitry Andric V = BitcodeConstant::create( 322981ad6265SDimitry Andric Alloc, CurTy, Instruction::ShuffleVector, 323081ad6265SDimitry Andric {(unsigned)Record[1], (unsigned)Record[2], (unsigned)Record[3]}); 323181ad6265SDimitry Andric break; 3232*0b57cec5SDimitry Andric } 3233*0b57cec5SDimitry Andric case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred] 3234*0b57cec5SDimitry Andric if (Record.size() < 4) 323581ad6265SDimitry Andric return error("Invalid cmp constexpt record"); 323681ad6265SDimitry Andric unsigned OpTyID = Record[0]; 323781ad6265SDimitry Andric Type *OpTy = getTypeByID(OpTyID); 3238*0b57cec5SDimitry Andric if (!OpTy) 323981ad6265SDimitry Andric return error("Invalid cmp constexpr record"); 324081ad6265SDimitry Andric V = BitcodeConstant::create( 324181ad6265SDimitry Andric Alloc, CurTy, 324281ad6265SDimitry Andric {(uint8_t)(OpTy->isFPOrFPVectorTy() ? Instruction::FCmp 324381ad6265SDimitry Andric : Instruction::ICmp), 324481ad6265SDimitry Andric (uint8_t)Record[3]}, 324581ad6265SDimitry Andric {(unsigned)Record[1], (unsigned)Record[2]}); 3246*0b57cec5SDimitry Andric break; 3247*0b57cec5SDimitry Andric } 3248*0b57cec5SDimitry Andric // This maintains backward compatibility, pre-asm dialect keywords. 32495ffd83dbSDimitry Andric // Deprecated, but still needed to read old bitcode files. 3250*0b57cec5SDimitry Andric case bitc::CST_CODE_INLINEASM_OLD: { 3251*0b57cec5SDimitry Andric if (Record.size() < 2) 325281ad6265SDimitry Andric return error("Invalid inlineasm record"); 3253*0b57cec5SDimitry Andric std::string AsmStr, ConstrStr; 3254*0b57cec5SDimitry Andric bool HasSideEffects = Record[0] & 1; 3255*0b57cec5SDimitry Andric bool IsAlignStack = Record[0] >> 1; 3256*0b57cec5SDimitry Andric unsigned AsmStrSize = Record[1]; 3257*0b57cec5SDimitry Andric if (2+AsmStrSize >= Record.size()) 325881ad6265SDimitry Andric return error("Invalid inlineasm record"); 3259*0b57cec5SDimitry Andric unsigned ConstStrSize = Record[2+AsmStrSize]; 3260*0b57cec5SDimitry Andric if (3+AsmStrSize+ConstStrSize > Record.size()) 326181ad6265SDimitry Andric return error("Invalid inlineasm record"); 3262*0b57cec5SDimitry Andric 3263*0b57cec5SDimitry Andric for (unsigned i = 0; i != AsmStrSize; ++i) 3264*0b57cec5SDimitry Andric AsmStr += (char)Record[2+i]; 3265*0b57cec5SDimitry Andric for (unsigned i = 0; i != ConstStrSize; ++i) 3266*0b57cec5SDimitry Andric ConstrStr += (char)Record[3+AsmStrSize+i]; 3267*0b57cec5SDimitry Andric UpgradeInlineAsmString(&AsmStr); 326881ad6265SDimitry Andric if (!CurElemTy) 326981ad6265SDimitry Andric return error("Missing element type for old-style inlineasm"); 327081ad6265SDimitry Andric V = InlineAsm::get(cast<FunctionType>(CurElemTy), AsmStr, ConstrStr, 327181ad6265SDimitry Andric HasSideEffects, IsAlignStack); 3272*0b57cec5SDimitry Andric break; 3273*0b57cec5SDimitry Andric } 3274*0b57cec5SDimitry Andric // This version adds support for the asm dialect keywords (e.g., 3275*0b57cec5SDimitry Andric // inteldialect). 3276fe6060f1SDimitry Andric case bitc::CST_CODE_INLINEASM_OLD2: { 3277*0b57cec5SDimitry Andric if (Record.size() < 2) 327881ad6265SDimitry Andric return error("Invalid inlineasm record"); 3279*0b57cec5SDimitry Andric std::string AsmStr, ConstrStr; 3280*0b57cec5SDimitry Andric bool HasSideEffects = Record[0] & 1; 3281*0b57cec5SDimitry Andric bool IsAlignStack = (Record[0] >> 1) & 1; 3282*0b57cec5SDimitry Andric unsigned AsmDialect = Record[0] >> 2; 3283*0b57cec5SDimitry Andric unsigned AsmStrSize = Record[1]; 3284*0b57cec5SDimitry Andric if (2+AsmStrSize >= Record.size()) 328581ad6265SDimitry Andric return error("Invalid inlineasm record"); 3286*0b57cec5SDimitry Andric unsigned ConstStrSize = Record[2+AsmStrSize]; 3287*0b57cec5SDimitry Andric if (3+AsmStrSize+ConstStrSize > Record.size()) 328881ad6265SDimitry Andric return error("Invalid inlineasm record"); 3289*0b57cec5SDimitry Andric 3290*0b57cec5SDimitry Andric for (unsigned i = 0; i != AsmStrSize; ++i) 3291*0b57cec5SDimitry Andric AsmStr += (char)Record[2+i]; 3292*0b57cec5SDimitry Andric for (unsigned i = 0; i != ConstStrSize; ++i) 3293*0b57cec5SDimitry Andric ConstrStr += (char)Record[3+AsmStrSize+i]; 3294*0b57cec5SDimitry Andric UpgradeInlineAsmString(&AsmStr); 329581ad6265SDimitry Andric if (!CurElemTy) 329681ad6265SDimitry Andric return error("Missing element type for old-style inlineasm"); 329781ad6265SDimitry Andric V = InlineAsm::get(cast<FunctionType>(CurElemTy), AsmStr, ConstrStr, 329881ad6265SDimitry Andric HasSideEffects, IsAlignStack, 3299*0b57cec5SDimitry Andric InlineAsm::AsmDialect(AsmDialect)); 3300*0b57cec5SDimitry Andric break; 3301*0b57cec5SDimitry Andric } 3302fe6060f1SDimitry Andric // This version adds support for the unwind keyword. 330304eeddc0SDimitry Andric case bitc::CST_CODE_INLINEASM_OLD3: { 3304fe6060f1SDimitry Andric if (Record.size() < 2) 330581ad6265SDimitry Andric return error("Invalid inlineasm record"); 330604eeddc0SDimitry Andric unsigned OpNum = 0; 3307fe6060f1SDimitry Andric std::string AsmStr, ConstrStr; 330804eeddc0SDimitry Andric bool HasSideEffects = Record[OpNum] & 1; 330904eeddc0SDimitry Andric bool IsAlignStack = (Record[OpNum] >> 1) & 1; 331004eeddc0SDimitry Andric unsigned AsmDialect = (Record[OpNum] >> 2) & 1; 331104eeddc0SDimitry Andric bool CanThrow = (Record[OpNum] >> 3) & 1; 331204eeddc0SDimitry Andric ++OpNum; 331304eeddc0SDimitry Andric unsigned AsmStrSize = Record[OpNum]; 331404eeddc0SDimitry Andric ++OpNum; 331504eeddc0SDimitry Andric if (OpNum + AsmStrSize >= Record.size()) 331681ad6265SDimitry Andric return error("Invalid inlineasm record"); 331704eeddc0SDimitry Andric unsigned ConstStrSize = Record[OpNum + AsmStrSize]; 331804eeddc0SDimitry Andric if (OpNum + 1 + AsmStrSize + ConstStrSize > Record.size()) 331981ad6265SDimitry Andric return error("Invalid inlineasm record"); 3320fe6060f1SDimitry Andric 3321fe6060f1SDimitry Andric for (unsigned i = 0; i != AsmStrSize; ++i) 332204eeddc0SDimitry Andric AsmStr += (char)Record[OpNum + i]; 332304eeddc0SDimitry Andric ++OpNum; 3324fe6060f1SDimitry Andric for (unsigned i = 0; i != ConstStrSize; ++i) 332504eeddc0SDimitry Andric ConstrStr += (char)Record[OpNum + AsmStrSize + i]; 3326fe6060f1SDimitry Andric UpgradeInlineAsmString(&AsmStr); 332781ad6265SDimitry Andric if (!CurElemTy) 332881ad6265SDimitry Andric return error("Missing element type for old-style inlineasm"); 332981ad6265SDimitry Andric V = InlineAsm::get(cast<FunctionType>(CurElemTy), AsmStr, ConstrStr, 333081ad6265SDimitry Andric HasSideEffects, IsAlignStack, 3331fe6060f1SDimitry Andric InlineAsm::AsmDialect(AsmDialect), CanThrow); 3332fe6060f1SDimitry Andric break; 3333fe6060f1SDimitry Andric } 333404eeddc0SDimitry Andric // This version adds explicit function type. 333504eeddc0SDimitry Andric case bitc::CST_CODE_INLINEASM: { 333604eeddc0SDimitry Andric if (Record.size() < 3) 333781ad6265SDimitry Andric return error("Invalid inlineasm record"); 333804eeddc0SDimitry Andric unsigned OpNum = 0; 333904eeddc0SDimitry Andric auto *FnTy = dyn_cast_or_null<FunctionType>(getTypeByID(Record[OpNum])); 334004eeddc0SDimitry Andric ++OpNum; 334104eeddc0SDimitry Andric if (!FnTy) 334281ad6265SDimitry Andric return error("Invalid inlineasm record"); 334304eeddc0SDimitry Andric std::string AsmStr, ConstrStr; 334404eeddc0SDimitry Andric bool HasSideEffects = Record[OpNum] & 1; 334504eeddc0SDimitry Andric bool IsAlignStack = (Record[OpNum] >> 1) & 1; 334604eeddc0SDimitry Andric unsigned AsmDialect = (Record[OpNum] >> 2) & 1; 334704eeddc0SDimitry Andric bool CanThrow = (Record[OpNum] >> 3) & 1; 334804eeddc0SDimitry Andric ++OpNum; 334904eeddc0SDimitry Andric unsigned AsmStrSize = Record[OpNum]; 335004eeddc0SDimitry Andric ++OpNum; 335104eeddc0SDimitry Andric if (OpNum + AsmStrSize >= Record.size()) 335281ad6265SDimitry Andric return error("Invalid inlineasm record"); 335304eeddc0SDimitry Andric unsigned ConstStrSize = Record[OpNum + AsmStrSize]; 335404eeddc0SDimitry Andric if (OpNum + 1 + AsmStrSize + ConstStrSize > Record.size()) 335581ad6265SDimitry Andric return error("Invalid inlineasm record"); 335604eeddc0SDimitry Andric 335704eeddc0SDimitry Andric for (unsigned i = 0; i != AsmStrSize; ++i) 335804eeddc0SDimitry Andric AsmStr += (char)Record[OpNum + i]; 335904eeddc0SDimitry Andric ++OpNum; 336004eeddc0SDimitry Andric for (unsigned i = 0; i != ConstStrSize; ++i) 336104eeddc0SDimitry Andric ConstrStr += (char)Record[OpNum + AsmStrSize + i]; 336204eeddc0SDimitry Andric UpgradeInlineAsmString(&AsmStr); 336304eeddc0SDimitry Andric V = InlineAsm::get(FnTy, AsmStr, ConstrStr, HasSideEffects, IsAlignStack, 336404eeddc0SDimitry Andric InlineAsm::AsmDialect(AsmDialect), CanThrow); 336504eeddc0SDimitry Andric break; 336604eeddc0SDimitry Andric } 3367*0b57cec5SDimitry Andric case bitc::CST_CODE_BLOCKADDRESS:{ 3368*0b57cec5SDimitry Andric if (Record.size() < 3) 336981ad6265SDimitry Andric return error("Invalid blockaddress record"); 337081ad6265SDimitry Andric unsigned FnTyID = Record[0]; 337181ad6265SDimitry Andric Type *FnTy = getTypeByID(FnTyID); 3372*0b57cec5SDimitry Andric if (!FnTy) 337381ad6265SDimitry Andric return error("Invalid blockaddress record"); 337481ad6265SDimitry Andric V = BitcodeConstant::create( 337581ad6265SDimitry Andric Alloc, CurTy, 337681ad6265SDimitry Andric {BitcodeConstant::BlockAddressOpcode, 0, (unsigned)Record[2]}, 337781ad6265SDimitry Andric Record[1]); 3378*0b57cec5SDimitry Andric break; 3379*0b57cec5SDimitry Andric } 3380fe6060f1SDimitry Andric case bitc::CST_CODE_DSO_LOCAL_EQUIVALENT: { 3381fe6060f1SDimitry Andric if (Record.size() < 2) 338281ad6265SDimitry Andric return error("Invalid dso_local record"); 338381ad6265SDimitry Andric unsigned GVTyID = Record[0]; 338481ad6265SDimitry Andric Type *GVTy = getTypeByID(GVTyID); 3385fe6060f1SDimitry Andric if (!GVTy) 338681ad6265SDimitry Andric return error("Invalid dso_local record"); 338781ad6265SDimitry Andric V = BitcodeConstant::create( 338881ad6265SDimitry Andric Alloc, CurTy, BitcodeConstant::DSOLocalEquivalentOpcode, Record[1]); 3389fe6060f1SDimitry Andric break; 3390fe6060f1SDimitry Andric } 33910eae32dcSDimitry Andric case bitc::CST_CODE_NO_CFI_VALUE: { 33920eae32dcSDimitry Andric if (Record.size() < 2) 339381ad6265SDimitry Andric return error("Invalid no_cfi record"); 339481ad6265SDimitry Andric unsigned GVTyID = Record[0]; 339581ad6265SDimitry Andric Type *GVTy = getTypeByID(GVTyID); 33960eae32dcSDimitry Andric if (!GVTy) 339781ad6265SDimitry Andric return error("Invalid no_cfi record"); 339881ad6265SDimitry Andric V = BitcodeConstant::create(Alloc, CurTy, BitcodeConstant::NoCFIOpcode, 339981ad6265SDimitry Andric Record[1]); 34000eae32dcSDimitry Andric break; 34010eae32dcSDimitry Andric } 3402*0b57cec5SDimitry Andric } 3403*0b57cec5SDimitry Andric 340481ad6265SDimitry Andric assert(V->getType() == getTypeByID(CurTyID) && "Incorrect result type ID"); 340581ad6265SDimitry Andric if (Error Err = ValueList.assignValue(NextCstNo, V, CurTyID)) 340681ad6265SDimitry Andric return Err; 3407*0b57cec5SDimitry Andric ++NextCstNo; 3408*0b57cec5SDimitry Andric } 3409*0b57cec5SDimitry Andric } 3410*0b57cec5SDimitry Andric 3411*0b57cec5SDimitry Andric Error BitcodeReader::parseUseLists() { 3412*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID)) 3413*0b57cec5SDimitry Andric return Err; 3414*0b57cec5SDimitry Andric 3415*0b57cec5SDimitry Andric // Read all the records. 3416*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 3417*0b57cec5SDimitry Andric 3418*0b57cec5SDimitry Andric while (true) { 3419*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 3420*0b57cec5SDimitry Andric if (!MaybeEntry) 3421*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 3422*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 3423*0b57cec5SDimitry Andric 3424*0b57cec5SDimitry Andric switch (Entry.Kind) { 3425*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 3426*0b57cec5SDimitry Andric case BitstreamEntry::Error: 3427*0b57cec5SDimitry Andric return error("Malformed block"); 3428*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 3429*0b57cec5SDimitry Andric return Error::success(); 3430*0b57cec5SDimitry Andric case BitstreamEntry::Record: 3431*0b57cec5SDimitry Andric // The interesting case. 3432*0b57cec5SDimitry Andric break; 3433*0b57cec5SDimitry Andric } 3434*0b57cec5SDimitry Andric 3435*0b57cec5SDimitry Andric // Read a use list record. 3436*0b57cec5SDimitry Andric Record.clear(); 3437*0b57cec5SDimitry Andric bool IsBB = false; 3438*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 3439*0b57cec5SDimitry Andric if (!MaybeRecord) 3440*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 3441*0b57cec5SDimitry Andric switch (MaybeRecord.get()) { 3442*0b57cec5SDimitry Andric default: // Default behavior: unknown type. 3443*0b57cec5SDimitry Andric break; 3444*0b57cec5SDimitry Andric case bitc::USELIST_CODE_BB: 3445*0b57cec5SDimitry Andric IsBB = true; 3446*0b57cec5SDimitry Andric LLVM_FALLTHROUGH; 3447*0b57cec5SDimitry Andric case bitc::USELIST_CODE_DEFAULT: { 3448*0b57cec5SDimitry Andric unsigned RecordLength = Record.size(); 3449*0b57cec5SDimitry Andric if (RecordLength < 3) 3450*0b57cec5SDimitry Andric // Records should have at least an ID and two indexes. 3451*0b57cec5SDimitry Andric return error("Invalid record"); 3452e8d8bef9SDimitry Andric unsigned ID = Record.pop_back_val(); 3453*0b57cec5SDimitry Andric 3454*0b57cec5SDimitry Andric Value *V; 3455*0b57cec5SDimitry Andric if (IsBB) { 3456*0b57cec5SDimitry Andric assert(ID < FunctionBBs.size() && "Basic block not found"); 3457*0b57cec5SDimitry Andric V = FunctionBBs[ID]; 3458*0b57cec5SDimitry Andric } else 3459*0b57cec5SDimitry Andric V = ValueList[ID]; 3460*0b57cec5SDimitry Andric unsigned NumUses = 0; 3461*0b57cec5SDimitry Andric SmallDenseMap<const Use *, unsigned, 16> Order; 3462*0b57cec5SDimitry Andric for (const Use &U : V->materialized_uses()) { 3463*0b57cec5SDimitry Andric if (++NumUses > Record.size()) 3464*0b57cec5SDimitry Andric break; 3465*0b57cec5SDimitry Andric Order[&U] = Record[NumUses - 1]; 3466*0b57cec5SDimitry Andric } 3467*0b57cec5SDimitry Andric if (Order.size() != Record.size() || NumUses > Record.size()) 3468*0b57cec5SDimitry Andric // Mismatches can happen if the functions are being materialized lazily 3469*0b57cec5SDimitry Andric // (out-of-order), or a value has been upgraded. 3470*0b57cec5SDimitry Andric break; 3471*0b57cec5SDimitry Andric 3472*0b57cec5SDimitry Andric V->sortUseList([&](const Use &L, const Use &R) { 3473*0b57cec5SDimitry Andric return Order.lookup(&L) < Order.lookup(&R); 3474*0b57cec5SDimitry Andric }); 3475*0b57cec5SDimitry Andric break; 3476*0b57cec5SDimitry Andric } 3477*0b57cec5SDimitry Andric } 3478*0b57cec5SDimitry Andric } 3479*0b57cec5SDimitry Andric } 3480*0b57cec5SDimitry Andric 3481*0b57cec5SDimitry Andric /// When we see the block for metadata, remember where it is and then skip it. 3482*0b57cec5SDimitry Andric /// This lets us lazily deserialize the metadata. 3483*0b57cec5SDimitry Andric Error BitcodeReader::rememberAndSkipMetadata() { 3484*0b57cec5SDimitry Andric // Save the current stream state. 3485*0b57cec5SDimitry Andric uint64_t CurBit = Stream.GetCurrentBitNo(); 3486*0b57cec5SDimitry Andric DeferredMetadataInfo.push_back(CurBit); 3487*0b57cec5SDimitry Andric 3488*0b57cec5SDimitry Andric // Skip over the block for now. 3489*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 3490*0b57cec5SDimitry Andric return Err; 3491*0b57cec5SDimitry Andric return Error::success(); 3492*0b57cec5SDimitry Andric } 3493*0b57cec5SDimitry Andric 3494*0b57cec5SDimitry Andric Error BitcodeReader::materializeMetadata() { 3495*0b57cec5SDimitry Andric for (uint64_t BitPos : DeferredMetadataInfo) { 3496*0b57cec5SDimitry Andric // Move the bit stream to the saved position. 3497*0b57cec5SDimitry Andric if (Error JumpFailed = Stream.JumpToBit(BitPos)) 3498*0b57cec5SDimitry Andric return JumpFailed; 3499*0b57cec5SDimitry Andric if (Error Err = MDLoader->parseModuleMetadata()) 3500*0b57cec5SDimitry Andric return Err; 3501*0b57cec5SDimitry Andric } 3502*0b57cec5SDimitry Andric 3503*0b57cec5SDimitry Andric // Upgrade "Linker Options" module flag to "llvm.linker.options" module-level 3504e8d8bef9SDimitry Andric // metadata. Only upgrade if the new option doesn't exist to avoid upgrade 3505e8d8bef9SDimitry Andric // multiple times. 3506e8d8bef9SDimitry Andric if (!TheModule->getNamedMetadata("llvm.linker.options")) { 3507*0b57cec5SDimitry Andric if (Metadata *Val = TheModule->getModuleFlag("Linker Options")) { 3508*0b57cec5SDimitry Andric NamedMDNode *LinkerOpts = 3509*0b57cec5SDimitry Andric TheModule->getOrInsertNamedMetadata("llvm.linker.options"); 3510*0b57cec5SDimitry Andric for (const MDOperand &MDOptions : cast<MDNode>(Val)->operands()) 3511*0b57cec5SDimitry Andric LinkerOpts->addOperand(cast<MDNode>(MDOptions)); 3512*0b57cec5SDimitry Andric } 3513e8d8bef9SDimitry Andric } 3514*0b57cec5SDimitry Andric 3515*0b57cec5SDimitry Andric DeferredMetadataInfo.clear(); 3516*0b57cec5SDimitry Andric return Error::success(); 3517*0b57cec5SDimitry Andric } 3518*0b57cec5SDimitry Andric 3519*0b57cec5SDimitry Andric void BitcodeReader::setStripDebugInfo() { StripDebugInfo = true; } 3520*0b57cec5SDimitry Andric 3521*0b57cec5SDimitry Andric /// When we see the block for a function body, remember where it is and then 3522*0b57cec5SDimitry Andric /// skip it. This lets us lazily deserialize the functions. 3523*0b57cec5SDimitry Andric Error BitcodeReader::rememberAndSkipFunctionBody() { 3524*0b57cec5SDimitry Andric // Get the function we are talking about. 3525*0b57cec5SDimitry Andric if (FunctionsWithBodies.empty()) 3526*0b57cec5SDimitry Andric return error("Insufficient function protos"); 3527*0b57cec5SDimitry Andric 3528*0b57cec5SDimitry Andric Function *Fn = FunctionsWithBodies.back(); 3529*0b57cec5SDimitry Andric FunctionsWithBodies.pop_back(); 3530*0b57cec5SDimitry Andric 3531*0b57cec5SDimitry Andric // Save the current stream state. 3532*0b57cec5SDimitry Andric uint64_t CurBit = Stream.GetCurrentBitNo(); 3533*0b57cec5SDimitry Andric assert( 3534*0b57cec5SDimitry Andric (DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) && 3535*0b57cec5SDimitry Andric "Mismatch between VST and scanned function offsets"); 3536*0b57cec5SDimitry Andric DeferredFunctionInfo[Fn] = CurBit; 3537*0b57cec5SDimitry Andric 3538*0b57cec5SDimitry Andric // Skip over the function block for now. 3539*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 3540*0b57cec5SDimitry Andric return Err; 3541*0b57cec5SDimitry Andric return Error::success(); 3542*0b57cec5SDimitry Andric } 3543*0b57cec5SDimitry Andric 3544*0b57cec5SDimitry Andric Error BitcodeReader::globalCleanup() { 3545*0b57cec5SDimitry Andric // Patch the initializers for globals and aliases up. 3546*0b57cec5SDimitry Andric if (Error Err = resolveGlobalAndIndirectSymbolInits()) 3547*0b57cec5SDimitry Andric return Err; 3548*0b57cec5SDimitry Andric if (!GlobalInits.empty() || !IndirectSymbolInits.empty()) 3549*0b57cec5SDimitry Andric return error("Malformed global initializer set"); 3550*0b57cec5SDimitry Andric 3551*0b57cec5SDimitry Andric // Look for intrinsic functions which need to be upgraded at some point 35525ffd83dbSDimitry Andric // and functions that need to have their function attributes upgraded. 3553*0b57cec5SDimitry Andric for (Function &F : *TheModule) { 3554*0b57cec5SDimitry Andric MDLoader->upgradeDebugIntrinsics(F); 3555*0b57cec5SDimitry Andric Function *NewFn; 3556*0b57cec5SDimitry Andric if (UpgradeIntrinsicFunction(&F, NewFn)) 3557*0b57cec5SDimitry Andric UpgradedIntrinsics[&F] = NewFn; 3558*0b57cec5SDimitry Andric else if (auto Remangled = Intrinsic::remangleIntrinsicFunction(&F)) 3559*0b57cec5SDimitry Andric // Some types could be renamed during loading if several modules are 3560*0b57cec5SDimitry Andric // loaded in the same LLVMContext (LTO scenario). In this case we should 3561*0b57cec5SDimitry Andric // remangle intrinsics names as well. 356281ad6265SDimitry Andric RemangledIntrinsics[&F] = *Remangled; 35635ffd83dbSDimitry Andric // Look for functions that rely on old function attribute behavior. 35645ffd83dbSDimitry Andric UpgradeFunctionAttributes(F); 3565*0b57cec5SDimitry Andric } 3566*0b57cec5SDimitry Andric 3567*0b57cec5SDimitry Andric // Look for global variables which need to be renamed. 3568*0b57cec5SDimitry Andric std::vector<std::pair<GlobalVariable *, GlobalVariable *>> UpgradedVariables; 3569*0b57cec5SDimitry Andric for (GlobalVariable &GV : TheModule->globals()) 3570*0b57cec5SDimitry Andric if (GlobalVariable *Upgraded = UpgradeGlobalVariable(&GV)) 3571*0b57cec5SDimitry Andric UpgradedVariables.emplace_back(&GV, Upgraded); 3572*0b57cec5SDimitry Andric for (auto &Pair : UpgradedVariables) { 3573*0b57cec5SDimitry Andric Pair.first->eraseFromParent(); 3574*0b57cec5SDimitry Andric TheModule->getGlobalList().push_back(Pair.second); 3575*0b57cec5SDimitry Andric } 3576*0b57cec5SDimitry Andric 3577*0b57cec5SDimitry Andric // Force deallocation of memory for these vectors to favor the client that 3578*0b57cec5SDimitry Andric // want lazy deserialization. 3579*0b57cec5SDimitry Andric std::vector<std::pair<GlobalVariable *, unsigned>>().swap(GlobalInits); 3580349cc55cSDimitry Andric std::vector<std::pair<GlobalValue *, unsigned>>().swap(IndirectSymbolInits); 3581*0b57cec5SDimitry Andric return Error::success(); 3582*0b57cec5SDimitry Andric } 3583*0b57cec5SDimitry Andric 3584*0b57cec5SDimitry Andric /// Support for lazy parsing of function bodies. This is required if we 3585*0b57cec5SDimitry Andric /// either have an old bitcode file without a VST forward declaration record, 3586*0b57cec5SDimitry Andric /// or if we have an anonymous function being materialized, since anonymous 3587*0b57cec5SDimitry Andric /// functions do not have a name and are therefore not in the VST. 3588*0b57cec5SDimitry Andric Error BitcodeReader::rememberAndSkipFunctionBodies() { 3589*0b57cec5SDimitry Andric if (Error JumpFailed = Stream.JumpToBit(NextUnreadBit)) 3590*0b57cec5SDimitry Andric return JumpFailed; 3591*0b57cec5SDimitry Andric 3592*0b57cec5SDimitry Andric if (Stream.AtEndOfStream()) 3593*0b57cec5SDimitry Andric return error("Could not find function in stream"); 3594*0b57cec5SDimitry Andric 3595*0b57cec5SDimitry Andric if (!SeenFirstFunctionBody) 3596*0b57cec5SDimitry Andric return error("Trying to materialize functions before seeing function blocks"); 3597*0b57cec5SDimitry Andric 3598*0b57cec5SDimitry Andric // An old bitcode file with the symbol table at the end would have 3599*0b57cec5SDimitry Andric // finished the parse greedily. 3600*0b57cec5SDimitry Andric assert(SeenValueSymbolTable); 3601*0b57cec5SDimitry Andric 3602*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 3603*0b57cec5SDimitry Andric 3604*0b57cec5SDimitry Andric while (true) { 3605*0b57cec5SDimitry Andric Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 3606*0b57cec5SDimitry Andric if (!MaybeEntry) 3607*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 3608*0b57cec5SDimitry Andric llvm::BitstreamEntry Entry = MaybeEntry.get(); 3609*0b57cec5SDimitry Andric 3610*0b57cec5SDimitry Andric switch (Entry.Kind) { 3611*0b57cec5SDimitry Andric default: 3612*0b57cec5SDimitry Andric return error("Expect SubBlock"); 3613*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: 3614*0b57cec5SDimitry Andric switch (Entry.ID) { 3615*0b57cec5SDimitry Andric default: 3616*0b57cec5SDimitry Andric return error("Expect function block"); 3617*0b57cec5SDimitry Andric case bitc::FUNCTION_BLOCK_ID: 3618*0b57cec5SDimitry Andric if (Error Err = rememberAndSkipFunctionBody()) 3619*0b57cec5SDimitry Andric return Err; 3620*0b57cec5SDimitry Andric NextUnreadBit = Stream.GetCurrentBitNo(); 3621*0b57cec5SDimitry Andric return Error::success(); 3622*0b57cec5SDimitry Andric } 3623*0b57cec5SDimitry Andric } 3624*0b57cec5SDimitry Andric } 3625*0b57cec5SDimitry Andric } 3626*0b57cec5SDimitry Andric 362781ad6265SDimitry Andric Error BitcodeReaderBase::readBlockInfo() { 3628*0b57cec5SDimitry Andric Expected<Optional<BitstreamBlockInfo>> MaybeNewBlockInfo = 3629*0b57cec5SDimitry Andric Stream.ReadBlockInfoBlock(); 3630*0b57cec5SDimitry Andric if (!MaybeNewBlockInfo) 363181ad6265SDimitry Andric return MaybeNewBlockInfo.takeError(); 3632*0b57cec5SDimitry Andric Optional<BitstreamBlockInfo> NewBlockInfo = 3633*0b57cec5SDimitry Andric std::move(MaybeNewBlockInfo.get()); 3634*0b57cec5SDimitry Andric if (!NewBlockInfo) 363581ad6265SDimitry Andric return error("Malformed block"); 3636*0b57cec5SDimitry Andric BlockInfo = std::move(*NewBlockInfo); 363781ad6265SDimitry Andric return Error::success(); 3638*0b57cec5SDimitry Andric } 3639*0b57cec5SDimitry Andric 3640*0b57cec5SDimitry Andric Error BitcodeReader::parseComdatRecord(ArrayRef<uint64_t> Record) { 3641*0b57cec5SDimitry Andric // v1: [selection_kind, name] 3642*0b57cec5SDimitry Andric // v2: [strtab_offset, strtab_size, selection_kind] 3643*0b57cec5SDimitry Andric StringRef Name; 3644*0b57cec5SDimitry Andric std::tie(Name, Record) = readNameFromStrtab(Record); 3645*0b57cec5SDimitry Andric 3646*0b57cec5SDimitry Andric if (Record.empty()) 3647*0b57cec5SDimitry Andric return error("Invalid record"); 3648*0b57cec5SDimitry Andric Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]); 3649*0b57cec5SDimitry Andric std::string OldFormatName; 3650*0b57cec5SDimitry Andric if (!UseStrtab) { 3651*0b57cec5SDimitry Andric if (Record.size() < 2) 3652*0b57cec5SDimitry Andric return error("Invalid record"); 3653*0b57cec5SDimitry Andric unsigned ComdatNameSize = Record[1]; 365481ad6265SDimitry Andric if (ComdatNameSize > Record.size() - 2) 365581ad6265SDimitry Andric return error("Comdat name size too large"); 3656*0b57cec5SDimitry Andric OldFormatName.reserve(ComdatNameSize); 3657*0b57cec5SDimitry Andric for (unsigned i = 0; i != ComdatNameSize; ++i) 3658*0b57cec5SDimitry Andric OldFormatName += (char)Record[2 + i]; 3659*0b57cec5SDimitry Andric Name = OldFormatName; 3660*0b57cec5SDimitry Andric } 3661*0b57cec5SDimitry Andric Comdat *C = TheModule->getOrInsertComdat(Name); 3662*0b57cec5SDimitry Andric C->setSelectionKind(SK); 3663*0b57cec5SDimitry Andric ComdatList.push_back(C); 3664*0b57cec5SDimitry Andric return Error::success(); 3665*0b57cec5SDimitry Andric } 3666*0b57cec5SDimitry Andric 3667*0b57cec5SDimitry Andric static void inferDSOLocal(GlobalValue *GV) { 3668*0b57cec5SDimitry Andric // infer dso_local from linkage and visibility if it is not encoded. 3669*0b57cec5SDimitry Andric if (GV->hasLocalLinkage() || 3670*0b57cec5SDimitry Andric (!GV->hasDefaultVisibility() && !GV->hasExternalWeakLinkage())) 3671*0b57cec5SDimitry Andric GV->setDSOLocal(true); 3672*0b57cec5SDimitry Andric } 3673*0b57cec5SDimitry Andric 367481ad6265SDimitry Andric GlobalValue::SanitizerMetadata deserializeSanitizerMetadata(unsigned V) { 367581ad6265SDimitry Andric GlobalValue::SanitizerMetadata Meta; 367681ad6265SDimitry Andric if (V & (1 << 0)) 367781ad6265SDimitry Andric Meta.NoAddress = true; 367881ad6265SDimitry Andric if (V & (1 << 1)) 367981ad6265SDimitry Andric Meta.NoHWAddress = true; 368081ad6265SDimitry Andric if (V & (1 << 2)) 3681753f127fSDimitry Andric Meta.Memtag = true; 368281ad6265SDimitry Andric if (V & (1 << 3)) 368381ad6265SDimitry Andric Meta.IsDynInit = true; 368481ad6265SDimitry Andric return Meta; 368581ad6265SDimitry Andric } 368681ad6265SDimitry Andric 3687*0b57cec5SDimitry Andric Error BitcodeReader::parseGlobalVarRecord(ArrayRef<uint64_t> Record) { 3688*0b57cec5SDimitry Andric // v1: [pointer type, isconst, initid, linkage, alignment, section, 3689*0b57cec5SDimitry Andric // visibility, threadlocal, unnamed_addr, externally_initialized, 3690*0b57cec5SDimitry Andric // dllstorageclass, comdat, attributes, preemption specifier, 3691*0b57cec5SDimitry Andric // partition strtab offset, partition strtab size] (name in VST) 3692*0b57cec5SDimitry Andric // v2: [strtab_offset, strtab_size, v1] 3693*0b57cec5SDimitry Andric StringRef Name; 3694*0b57cec5SDimitry Andric std::tie(Name, Record) = readNameFromStrtab(Record); 3695*0b57cec5SDimitry Andric 3696*0b57cec5SDimitry Andric if (Record.size() < 6) 3697*0b57cec5SDimitry Andric return error("Invalid record"); 369881ad6265SDimitry Andric unsigned TyID = Record[0]; 369981ad6265SDimitry Andric Type *Ty = getTypeByID(TyID); 3700*0b57cec5SDimitry Andric if (!Ty) 3701*0b57cec5SDimitry Andric return error("Invalid record"); 3702*0b57cec5SDimitry Andric bool isConstant = Record[1] & 1; 3703*0b57cec5SDimitry Andric bool explicitType = Record[1] & 2; 3704*0b57cec5SDimitry Andric unsigned AddressSpace; 3705*0b57cec5SDimitry Andric if (explicitType) { 3706*0b57cec5SDimitry Andric AddressSpace = Record[1] >> 2; 3707*0b57cec5SDimitry Andric } else { 3708*0b57cec5SDimitry Andric if (!Ty->isPointerTy()) 3709*0b57cec5SDimitry Andric return error("Invalid type for value"); 3710*0b57cec5SDimitry Andric AddressSpace = cast<PointerType>(Ty)->getAddressSpace(); 371181ad6265SDimitry Andric TyID = getContainedTypeID(TyID); 371281ad6265SDimitry Andric Ty = getTypeByID(TyID); 371381ad6265SDimitry Andric if (!Ty) 371481ad6265SDimitry Andric return error("Missing element type for old-style global"); 3715*0b57cec5SDimitry Andric } 3716*0b57cec5SDimitry Andric 3717*0b57cec5SDimitry Andric uint64_t RawLinkage = Record[3]; 3718*0b57cec5SDimitry Andric GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage); 37198bcb0991SDimitry Andric MaybeAlign Alignment; 3720*0b57cec5SDimitry Andric if (Error Err = parseAlignmentValue(Record[4], Alignment)) 3721*0b57cec5SDimitry Andric return Err; 3722*0b57cec5SDimitry Andric std::string Section; 3723*0b57cec5SDimitry Andric if (Record[5]) { 3724*0b57cec5SDimitry Andric if (Record[5] - 1 >= SectionTable.size()) 3725*0b57cec5SDimitry Andric return error("Invalid ID"); 3726*0b57cec5SDimitry Andric Section = SectionTable[Record[5] - 1]; 3727*0b57cec5SDimitry Andric } 3728*0b57cec5SDimitry Andric GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility; 3729*0b57cec5SDimitry Andric // Local linkage must have default visibility. 37305ffd83dbSDimitry Andric // auto-upgrade `hidden` and `protected` for old bitcode. 3731*0b57cec5SDimitry Andric if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage)) 3732*0b57cec5SDimitry Andric Visibility = getDecodedVisibility(Record[6]); 3733*0b57cec5SDimitry Andric 3734*0b57cec5SDimitry Andric GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal; 3735*0b57cec5SDimitry Andric if (Record.size() > 7) 3736*0b57cec5SDimitry Andric TLM = getDecodedThreadLocalMode(Record[7]); 3737*0b57cec5SDimitry Andric 3738*0b57cec5SDimitry Andric GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None; 3739*0b57cec5SDimitry Andric if (Record.size() > 8) 3740*0b57cec5SDimitry Andric UnnamedAddr = getDecodedUnnamedAddrType(Record[8]); 3741*0b57cec5SDimitry Andric 3742*0b57cec5SDimitry Andric bool ExternallyInitialized = false; 3743*0b57cec5SDimitry Andric if (Record.size() > 9) 3744*0b57cec5SDimitry Andric ExternallyInitialized = Record[9]; 3745*0b57cec5SDimitry Andric 3746*0b57cec5SDimitry Andric GlobalVariable *NewGV = 3747*0b57cec5SDimitry Andric new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, Name, 3748*0b57cec5SDimitry Andric nullptr, TLM, AddressSpace, ExternallyInitialized); 3749*0b57cec5SDimitry Andric NewGV->setAlignment(Alignment); 3750*0b57cec5SDimitry Andric if (!Section.empty()) 3751*0b57cec5SDimitry Andric NewGV->setSection(Section); 3752*0b57cec5SDimitry Andric NewGV->setVisibility(Visibility); 3753*0b57cec5SDimitry Andric NewGV->setUnnamedAddr(UnnamedAddr); 3754*0b57cec5SDimitry Andric 3755*0b57cec5SDimitry Andric if (Record.size() > 10) 3756*0b57cec5SDimitry Andric NewGV->setDLLStorageClass(getDecodedDLLStorageClass(Record[10])); 3757*0b57cec5SDimitry Andric else 3758*0b57cec5SDimitry Andric upgradeDLLImportExportLinkage(NewGV, RawLinkage); 3759*0b57cec5SDimitry Andric 376081ad6265SDimitry Andric ValueList.push_back(NewGV, getVirtualTypeID(NewGV->getType(), TyID)); 3761*0b57cec5SDimitry Andric 3762*0b57cec5SDimitry Andric // Remember which value to use for the global initializer. 3763*0b57cec5SDimitry Andric if (unsigned InitID = Record[2]) 3764*0b57cec5SDimitry Andric GlobalInits.push_back(std::make_pair(NewGV, InitID - 1)); 3765*0b57cec5SDimitry Andric 3766*0b57cec5SDimitry Andric if (Record.size() > 11) { 3767*0b57cec5SDimitry Andric if (unsigned ComdatID = Record[11]) { 3768*0b57cec5SDimitry Andric if (ComdatID > ComdatList.size()) 3769*0b57cec5SDimitry Andric return error("Invalid global variable comdat ID"); 3770*0b57cec5SDimitry Andric NewGV->setComdat(ComdatList[ComdatID - 1]); 3771*0b57cec5SDimitry Andric } 3772*0b57cec5SDimitry Andric } else if (hasImplicitComdat(RawLinkage)) { 37730eae32dcSDimitry Andric ImplicitComdatObjects.insert(NewGV); 3774*0b57cec5SDimitry Andric } 3775*0b57cec5SDimitry Andric 3776*0b57cec5SDimitry Andric if (Record.size() > 12) { 3777349cc55cSDimitry Andric auto AS = getAttributes(Record[12]).getFnAttrs(); 3778*0b57cec5SDimitry Andric NewGV->setAttributes(AS); 3779*0b57cec5SDimitry Andric } 3780*0b57cec5SDimitry Andric 3781*0b57cec5SDimitry Andric if (Record.size() > 13) { 3782*0b57cec5SDimitry Andric NewGV->setDSOLocal(getDecodedDSOLocal(Record[13])); 3783*0b57cec5SDimitry Andric } 3784*0b57cec5SDimitry Andric inferDSOLocal(NewGV); 3785*0b57cec5SDimitry Andric 3786*0b57cec5SDimitry Andric // Check whether we have enough values to read a partition name. 3787*0b57cec5SDimitry Andric if (Record.size() > 15) 3788*0b57cec5SDimitry Andric NewGV->setPartition(StringRef(Strtab.data() + Record[14], Record[15])); 3789*0b57cec5SDimitry Andric 379081ad6265SDimitry Andric if (Record.size() > 16 && Record[16]) { 379181ad6265SDimitry Andric llvm::GlobalValue::SanitizerMetadata Meta = 379281ad6265SDimitry Andric deserializeSanitizerMetadata(Record[16]); 379381ad6265SDimitry Andric NewGV->setSanitizerMetadata(Meta); 379481ad6265SDimitry Andric } 379581ad6265SDimitry Andric 3796*0b57cec5SDimitry Andric return Error::success(); 3797*0b57cec5SDimitry Andric } 3798*0b57cec5SDimitry Andric 3799*0b57cec5SDimitry Andric Error BitcodeReader::parseFunctionRecord(ArrayRef<uint64_t> Record) { 3800*0b57cec5SDimitry Andric // v1: [type, callingconv, isproto, linkage, paramattr, alignment, section, 3801*0b57cec5SDimitry Andric // visibility, gc, unnamed_addr, prologuedata, dllstorageclass, comdat, 3802*0b57cec5SDimitry Andric // prefixdata, personalityfn, preemption specifier, addrspace] (name in VST) 3803*0b57cec5SDimitry Andric // v2: [strtab_offset, strtab_size, v1] 3804*0b57cec5SDimitry Andric StringRef Name; 3805*0b57cec5SDimitry Andric std::tie(Name, Record) = readNameFromStrtab(Record); 3806*0b57cec5SDimitry Andric 3807*0b57cec5SDimitry Andric if (Record.size() < 8) 3808*0b57cec5SDimitry Andric return error("Invalid record"); 380981ad6265SDimitry Andric unsigned FTyID = Record[0]; 381081ad6265SDimitry Andric Type *FTy = getTypeByID(FTyID); 3811*0b57cec5SDimitry Andric if (!FTy) 3812*0b57cec5SDimitry Andric return error("Invalid record"); 381381ad6265SDimitry Andric if (isa<PointerType>(FTy)) { 381481ad6265SDimitry Andric FTyID = getContainedTypeID(FTyID, 0); 381581ad6265SDimitry Andric FTy = getTypeByID(FTyID); 381681ad6265SDimitry Andric if (!FTy) 381781ad6265SDimitry Andric return error("Missing element type for old-style function"); 381881ad6265SDimitry Andric } 3819*0b57cec5SDimitry Andric 3820*0b57cec5SDimitry Andric if (!isa<FunctionType>(FTy)) 3821*0b57cec5SDimitry Andric return error("Invalid type for value"); 3822*0b57cec5SDimitry Andric auto CC = static_cast<CallingConv::ID>(Record[1]); 3823*0b57cec5SDimitry Andric if (CC & ~CallingConv::MaxID) 3824*0b57cec5SDimitry Andric return error("Invalid calling convention ID"); 3825*0b57cec5SDimitry Andric 3826*0b57cec5SDimitry Andric unsigned AddrSpace = TheModule->getDataLayout().getProgramAddressSpace(); 3827*0b57cec5SDimitry Andric if (Record.size() > 16) 3828*0b57cec5SDimitry Andric AddrSpace = Record[16]; 3829*0b57cec5SDimitry Andric 3830*0b57cec5SDimitry Andric Function *Func = 3831*0b57cec5SDimitry Andric Function::Create(cast<FunctionType>(FTy), GlobalValue::ExternalLinkage, 3832*0b57cec5SDimitry Andric AddrSpace, Name, TheModule); 3833*0b57cec5SDimitry Andric 3834fe6060f1SDimitry Andric assert(Func->getFunctionType() == FTy && 3835*0b57cec5SDimitry Andric "Incorrect fully specified type provided for function"); 383681ad6265SDimitry Andric FunctionTypeIDs[Func] = FTyID; 3837*0b57cec5SDimitry Andric 3838*0b57cec5SDimitry Andric Func->setCallingConv(CC); 3839*0b57cec5SDimitry Andric bool isProto = Record[2]; 3840*0b57cec5SDimitry Andric uint64_t RawLinkage = Record[3]; 3841*0b57cec5SDimitry Andric Func->setLinkage(getDecodedLinkage(RawLinkage)); 3842*0b57cec5SDimitry Andric Func->setAttributes(getAttributes(Record[4])); 3843*0b57cec5SDimitry Andric 3844e8d8bef9SDimitry Andric // Upgrade any old-style byval or sret without a type by propagating the 3845e8d8bef9SDimitry Andric // argument's pointee type. There should be no opaque pointers where the byval 3846e8d8bef9SDimitry Andric // type is implicit. 3847*0b57cec5SDimitry Andric for (unsigned i = 0; i != Func->arg_size(); ++i) { 3848fe6060f1SDimitry Andric for (Attribute::AttrKind Kind : {Attribute::ByVal, Attribute::StructRet, 3849fe6060f1SDimitry Andric Attribute::InAlloca}) { 3850e8d8bef9SDimitry Andric if (!Func->hasParamAttribute(i, Kind)) 3851*0b57cec5SDimitry Andric continue; 3852*0b57cec5SDimitry Andric 3853fe6060f1SDimitry Andric if (Func->getParamAttribute(i, Kind).getValueAsType()) 3854fe6060f1SDimitry Andric continue; 3855fe6060f1SDimitry Andric 3856e8d8bef9SDimitry Andric Func->removeParamAttr(i, Kind); 3857e8d8bef9SDimitry Andric 385881ad6265SDimitry Andric unsigned ParamTypeID = getContainedTypeID(FTyID, i + 1); 385981ad6265SDimitry Andric Type *PtrEltTy = getPtrElementTypeByID(ParamTypeID); 386081ad6265SDimitry Andric if (!PtrEltTy) 386181ad6265SDimitry Andric return error("Missing param element type for attribute upgrade"); 386281ad6265SDimitry Andric 3863fe6060f1SDimitry Andric Attribute NewAttr; 3864fe6060f1SDimitry Andric switch (Kind) { 3865fe6060f1SDimitry Andric case Attribute::ByVal: 3866fe6060f1SDimitry Andric NewAttr = Attribute::getWithByValType(Context, PtrEltTy); 3867fe6060f1SDimitry Andric break; 3868fe6060f1SDimitry Andric case Attribute::StructRet: 3869fe6060f1SDimitry Andric NewAttr = Attribute::getWithStructRetType(Context, PtrEltTy); 3870fe6060f1SDimitry Andric break; 3871fe6060f1SDimitry Andric case Attribute::InAlloca: 3872fe6060f1SDimitry Andric NewAttr = Attribute::getWithInAllocaType(Context, PtrEltTy); 3873fe6060f1SDimitry Andric break; 3874fe6060f1SDimitry Andric default: 3875fe6060f1SDimitry Andric llvm_unreachable("not an upgraded type attribute"); 3876fe6060f1SDimitry Andric } 3877fe6060f1SDimitry Andric 3878e8d8bef9SDimitry Andric Func->addParamAttr(i, NewAttr); 3879e8d8bef9SDimitry Andric } 3880*0b57cec5SDimitry Andric } 3881*0b57cec5SDimitry Andric 388281ad6265SDimitry Andric if (Func->getCallingConv() == CallingConv::X86_INTR && 388381ad6265SDimitry Andric !Func->arg_empty() && !Func->hasParamAttribute(0, Attribute::ByVal)) { 388481ad6265SDimitry Andric unsigned ParamTypeID = getContainedTypeID(FTyID, 1); 388581ad6265SDimitry Andric Type *ByValTy = getPtrElementTypeByID(ParamTypeID); 388681ad6265SDimitry Andric if (!ByValTy) 388781ad6265SDimitry Andric return error("Missing param element type for x86_intrcc upgrade"); 388881ad6265SDimitry Andric Attribute NewAttr = Attribute::getWithByValType(Context, ByValTy); 388981ad6265SDimitry Andric Func->addParamAttr(0, NewAttr); 389081ad6265SDimitry Andric } 389181ad6265SDimitry Andric 38928bcb0991SDimitry Andric MaybeAlign Alignment; 3893*0b57cec5SDimitry Andric if (Error Err = parseAlignmentValue(Record[5], Alignment)) 3894*0b57cec5SDimitry Andric return Err; 3895*0b57cec5SDimitry Andric Func->setAlignment(Alignment); 3896*0b57cec5SDimitry Andric if (Record[6]) { 3897*0b57cec5SDimitry Andric if (Record[6] - 1 >= SectionTable.size()) 3898*0b57cec5SDimitry Andric return error("Invalid ID"); 3899*0b57cec5SDimitry Andric Func->setSection(SectionTable[Record[6] - 1]); 3900*0b57cec5SDimitry Andric } 3901*0b57cec5SDimitry Andric // Local linkage must have default visibility. 39025ffd83dbSDimitry Andric // auto-upgrade `hidden` and `protected` for old bitcode. 3903*0b57cec5SDimitry Andric if (!Func->hasLocalLinkage()) 3904*0b57cec5SDimitry Andric Func->setVisibility(getDecodedVisibility(Record[7])); 3905*0b57cec5SDimitry Andric if (Record.size() > 8 && Record[8]) { 3906*0b57cec5SDimitry Andric if (Record[8] - 1 >= GCTable.size()) 3907*0b57cec5SDimitry Andric return error("Invalid ID"); 3908*0b57cec5SDimitry Andric Func->setGC(GCTable[Record[8] - 1]); 3909*0b57cec5SDimitry Andric } 3910*0b57cec5SDimitry Andric GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None; 3911*0b57cec5SDimitry Andric if (Record.size() > 9) 3912*0b57cec5SDimitry Andric UnnamedAddr = getDecodedUnnamedAddrType(Record[9]); 3913*0b57cec5SDimitry Andric Func->setUnnamedAddr(UnnamedAddr); 3914349cc55cSDimitry Andric 3915349cc55cSDimitry Andric FunctionOperandInfo OperandInfo = {Func, 0, 0, 0}; 3916349cc55cSDimitry Andric if (Record.size() > 10) 3917349cc55cSDimitry Andric OperandInfo.Prologue = Record[10]; 3918*0b57cec5SDimitry Andric 3919*0b57cec5SDimitry Andric if (Record.size() > 11) 3920*0b57cec5SDimitry Andric Func->setDLLStorageClass(getDecodedDLLStorageClass(Record[11])); 3921*0b57cec5SDimitry Andric else 3922*0b57cec5SDimitry Andric upgradeDLLImportExportLinkage(Func, RawLinkage); 3923*0b57cec5SDimitry Andric 3924*0b57cec5SDimitry Andric if (Record.size() > 12) { 3925*0b57cec5SDimitry Andric if (unsigned ComdatID = Record[12]) { 3926*0b57cec5SDimitry Andric if (ComdatID > ComdatList.size()) 3927*0b57cec5SDimitry Andric return error("Invalid function comdat ID"); 3928*0b57cec5SDimitry Andric Func->setComdat(ComdatList[ComdatID - 1]); 3929*0b57cec5SDimitry Andric } 3930*0b57cec5SDimitry Andric } else if (hasImplicitComdat(RawLinkage)) { 39310eae32dcSDimitry Andric ImplicitComdatObjects.insert(Func); 3932*0b57cec5SDimitry Andric } 3933*0b57cec5SDimitry Andric 3934349cc55cSDimitry Andric if (Record.size() > 13) 3935349cc55cSDimitry Andric OperandInfo.Prefix = Record[13]; 3936*0b57cec5SDimitry Andric 3937349cc55cSDimitry Andric if (Record.size() > 14) 3938349cc55cSDimitry Andric OperandInfo.PersonalityFn = Record[14]; 3939*0b57cec5SDimitry Andric 3940*0b57cec5SDimitry Andric if (Record.size() > 15) { 3941*0b57cec5SDimitry Andric Func->setDSOLocal(getDecodedDSOLocal(Record[15])); 3942*0b57cec5SDimitry Andric } 3943*0b57cec5SDimitry Andric inferDSOLocal(Func); 3944*0b57cec5SDimitry Andric 3945*0b57cec5SDimitry Andric // Record[16] is the address space number. 3946*0b57cec5SDimitry Andric 3947fe6060f1SDimitry Andric // Check whether we have enough values to read a partition name. Also make 3948fe6060f1SDimitry Andric // sure Strtab has enough values. 3949fe6060f1SDimitry Andric if (Record.size() > 18 && Strtab.data() && 3950fe6060f1SDimitry Andric Record[17] + Record[18] <= Strtab.size()) { 3951*0b57cec5SDimitry Andric Func->setPartition(StringRef(Strtab.data() + Record[17], Record[18])); 3952fe6060f1SDimitry Andric } 3953*0b57cec5SDimitry Andric 395481ad6265SDimitry Andric ValueList.push_back(Func, getVirtualTypeID(Func->getType(), FTyID)); 3955*0b57cec5SDimitry Andric 3956349cc55cSDimitry Andric if (OperandInfo.PersonalityFn || OperandInfo.Prefix || OperandInfo.Prologue) 3957349cc55cSDimitry Andric FunctionOperands.push_back(OperandInfo); 3958349cc55cSDimitry Andric 3959*0b57cec5SDimitry Andric // If this is a function with a body, remember the prototype we are 3960*0b57cec5SDimitry Andric // creating now, so that we can match up the body with them later. 3961*0b57cec5SDimitry Andric if (!isProto) { 3962*0b57cec5SDimitry Andric Func->setIsMaterializable(true); 3963*0b57cec5SDimitry Andric FunctionsWithBodies.push_back(Func); 3964*0b57cec5SDimitry Andric DeferredFunctionInfo[Func] = 0; 3965*0b57cec5SDimitry Andric } 3966*0b57cec5SDimitry Andric return Error::success(); 3967*0b57cec5SDimitry Andric } 3968*0b57cec5SDimitry Andric 3969*0b57cec5SDimitry Andric Error BitcodeReader::parseGlobalIndirectSymbolRecord( 3970*0b57cec5SDimitry Andric unsigned BitCode, ArrayRef<uint64_t> Record) { 3971*0b57cec5SDimitry Andric // v1 ALIAS_OLD: [alias type, aliasee val#, linkage] (name in VST) 3972*0b57cec5SDimitry Andric // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility, 3973*0b57cec5SDimitry Andric // dllstorageclass, threadlocal, unnamed_addr, 3974*0b57cec5SDimitry Andric // preemption specifier] (name in VST) 3975*0b57cec5SDimitry Andric // v1 IFUNC: [alias type, addrspace, aliasee val#, linkage, 3976*0b57cec5SDimitry Andric // visibility, dllstorageclass, threadlocal, unnamed_addr, 3977*0b57cec5SDimitry Andric // preemption specifier] (name in VST) 3978*0b57cec5SDimitry Andric // v2: [strtab_offset, strtab_size, v1] 3979*0b57cec5SDimitry Andric StringRef Name; 3980*0b57cec5SDimitry Andric std::tie(Name, Record) = readNameFromStrtab(Record); 3981*0b57cec5SDimitry Andric 3982*0b57cec5SDimitry Andric bool NewRecord = BitCode != bitc::MODULE_CODE_ALIAS_OLD; 3983*0b57cec5SDimitry Andric if (Record.size() < (3 + (unsigned)NewRecord)) 3984*0b57cec5SDimitry Andric return error("Invalid record"); 3985*0b57cec5SDimitry Andric unsigned OpNum = 0; 398681ad6265SDimitry Andric unsigned TypeID = Record[OpNum++]; 398781ad6265SDimitry Andric Type *Ty = getTypeByID(TypeID); 3988*0b57cec5SDimitry Andric if (!Ty) 3989*0b57cec5SDimitry Andric return error("Invalid record"); 3990*0b57cec5SDimitry Andric 3991*0b57cec5SDimitry Andric unsigned AddrSpace; 3992*0b57cec5SDimitry Andric if (!NewRecord) { 3993*0b57cec5SDimitry Andric auto *PTy = dyn_cast<PointerType>(Ty); 3994*0b57cec5SDimitry Andric if (!PTy) 3995*0b57cec5SDimitry Andric return error("Invalid type for value"); 3996*0b57cec5SDimitry Andric AddrSpace = PTy->getAddressSpace(); 399781ad6265SDimitry Andric TypeID = getContainedTypeID(TypeID); 399881ad6265SDimitry Andric Ty = getTypeByID(TypeID); 399981ad6265SDimitry Andric if (!Ty) 400081ad6265SDimitry Andric return error("Missing element type for old-style indirect symbol"); 4001*0b57cec5SDimitry Andric } else { 4002*0b57cec5SDimitry Andric AddrSpace = Record[OpNum++]; 4003*0b57cec5SDimitry Andric } 4004*0b57cec5SDimitry Andric 4005*0b57cec5SDimitry Andric auto Val = Record[OpNum++]; 4006*0b57cec5SDimitry Andric auto Linkage = Record[OpNum++]; 4007349cc55cSDimitry Andric GlobalValue *NewGA; 4008*0b57cec5SDimitry Andric if (BitCode == bitc::MODULE_CODE_ALIAS || 4009*0b57cec5SDimitry Andric BitCode == bitc::MODULE_CODE_ALIAS_OLD) 4010*0b57cec5SDimitry Andric NewGA = GlobalAlias::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name, 4011*0b57cec5SDimitry Andric TheModule); 4012*0b57cec5SDimitry Andric else 4013*0b57cec5SDimitry Andric NewGA = GlobalIFunc::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name, 4014*0b57cec5SDimitry Andric nullptr, TheModule); 4015*0b57cec5SDimitry Andric 4016*0b57cec5SDimitry Andric // Local linkage must have default visibility. 40175ffd83dbSDimitry Andric // auto-upgrade `hidden` and `protected` for old bitcode. 4018*0b57cec5SDimitry Andric if (OpNum != Record.size()) { 4019*0b57cec5SDimitry Andric auto VisInd = OpNum++; 4020*0b57cec5SDimitry Andric if (!NewGA->hasLocalLinkage()) 4021*0b57cec5SDimitry Andric NewGA->setVisibility(getDecodedVisibility(Record[VisInd])); 4022*0b57cec5SDimitry Andric } 4023*0b57cec5SDimitry Andric if (BitCode == bitc::MODULE_CODE_ALIAS || 4024*0b57cec5SDimitry Andric BitCode == bitc::MODULE_CODE_ALIAS_OLD) { 4025*0b57cec5SDimitry Andric if (OpNum != Record.size()) 4026*0b57cec5SDimitry Andric NewGA->setDLLStorageClass(getDecodedDLLStorageClass(Record[OpNum++])); 4027*0b57cec5SDimitry Andric else 4028*0b57cec5SDimitry Andric upgradeDLLImportExportLinkage(NewGA, Linkage); 4029*0b57cec5SDimitry Andric if (OpNum != Record.size()) 4030*0b57cec5SDimitry Andric NewGA->setThreadLocalMode(getDecodedThreadLocalMode(Record[OpNum++])); 4031*0b57cec5SDimitry Andric if (OpNum != Record.size()) 4032*0b57cec5SDimitry Andric NewGA->setUnnamedAddr(getDecodedUnnamedAddrType(Record[OpNum++])); 4033*0b57cec5SDimitry Andric } 4034*0b57cec5SDimitry Andric if (OpNum != Record.size()) 4035*0b57cec5SDimitry Andric NewGA->setDSOLocal(getDecodedDSOLocal(Record[OpNum++])); 4036*0b57cec5SDimitry Andric inferDSOLocal(NewGA); 4037*0b57cec5SDimitry Andric 4038*0b57cec5SDimitry Andric // Check whether we have enough values to read a partition name. 4039*0b57cec5SDimitry Andric if (OpNum + 1 < Record.size()) { 4040*0b57cec5SDimitry Andric NewGA->setPartition( 4041*0b57cec5SDimitry Andric StringRef(Strtab.data() + Record[OpNum], Record[OpNum + 1])); 4042*0b57cec5SDimitry Andric OpNum += 2; 4043*0b57cec5SDimitry Andric } 4044*0b57cec5SDimitry Andric 404581ad6265SDimitry Andric ValueList.push_back(NewGA, getVirtualTypeID(NewGA->getType(), TypeID)); 4046*0b57cec5SDimitry Andric IndirectSymbolInits.push_back(std::make_pair(NewGA, Val)); 4047*0b57cec5SDimitry Andric return Error::success(); 4048*0b57cec5SDimitry Andric } 4049*0b57cec5SDimitry Andric 4050*0b57cec5SDimitry Andric Error BitcodeReader::parseModule(uint64_t ResumeBit, 40515ffd83dbSDimitry Andric bool ShouldLazyLoadMetadata, 40525ffd83dbSDimitry Andric DataLayoutCallbackTy DataLayoutCallback) { 4053*0b57cec5SDimitry Andric if (ResumeBit) { 4054*0b57cec5SDimitry Andric if (Error JumpFailed = Stream.JumpToBit(ResumeBit)) 4055*0b57cec5SDimitry Andric return JumpFailed; 4056*0b57cec5SDimitry Andric } else if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 4057*0b57cec5SDimitry Andric return Err; 4058*0b57cec5SDimitry Andric 4059*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 4060*0b57cec5SDimitry Andric 40615ffd83dbSDimitry Andric // Parts of bitcode parsing depend on the datalayout. Make sure we 40625ffd83dbSDimitry Andric // finalize the datalayout before we run any of that code. 40635ffd83dbSDimitry Andric bool ResolvedDataLayout = false; 40645ffd83dbSDimitry Andric auto ResolveDataLayout = [&] { 40655ffd83dbSDimitry Andric if (ResolvedDataLayout) 40665ffd83dbSDimitry Andric return; 40675ffd83dbSDimitry Andric 40685ffd83dbSDimitry Andric // datalayout and triple can't be parsed after this point. 40695ffd83dbSDimitry Andric ResolvedDataLayout = true; 40705ffd83dbSDimitry Andric 40715ffd83dbSDimitry Andric // Upgrade data layout string. 40725ffd83dbSDimitry Andric std::string DL = llvm::UpgradeDataLayoutString( 40735ffd83dbSDimitry Andric TheModule->getDataLayoutStr(), TheModule->getTargetTriple()); 40745ffd83dbSDimitry Andric TheModule->setDataLayout(DL); 40755ffd83dbSDimitry Andric 40765ffd83dbSDimitry Andric if (auto LayoutOverride = 40775ffd83dbSDimitry Andric DataLayoutCallback(TheModule->getTargetTriple())) 40785ffd83dbSDimitry Andric TheModule->setDataLayout(*LayoutOverride); 40795ffd83dbSDimitry Andric }; 40805ffd83dbSDimitry Andric 4081*0b57cec5SDimitry Andric // Read all the records for this module. 4082*0b57cec5SDimitry Andric while (true) { 4083*0b57cec5SDimitry Andric Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4084*0b57cec5SDimitry Andric if (!MaybeEntry) 4085*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 4086*0b57cec5SDimitry Andric llvm::BitstreamEntry Entry = MaybeEntry.get(); 4087*0b57cec5SDimitry Andric 4088*0b57cec5SDimitry Andric switch (Entry.Kind) { 4089*0b57cec5SDimitry Andric case BitstreamEntry::Error: 4090*0b57cec5SDimitry Andric return error("Malformed block"); 4091*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 40925ffd83dbSDimitry Andric ResolveDataLayout(); 4093*0b57cec5SDimitry Andric return globalCleanup(); 4094*0b57cec5SDimitry Andric 4095*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: 4096*0b57cec5SDimitry Andric switch (Entry.ID) { 4097*0b57cec5SDimitry Andric default: // Skip unknown content. 4098*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 4099*0b57cec5SDimitry Andric return Err; 4100*0b57cec5SDimitry Andric break; 4101*0b57cec5SDimitry Andric case bitc::BLOCKINFO_BLOCK_ID: 410281ad6265SDimitry Andric if (Error Err = readBlockInfo()) 410381ad6265SDimitry Andric return Err; 4104*0b57cec5SDimitry Andric break; 4105*0b57cec5SDimitry Andric case bitc::PARAMATTR_BLOCK_ID: 4106*0b57cec5SDimitry Andric if (Error Err = parseAttributeBlock()) 4107*0b57cec5SDimitry Andric return Err; 4108*0b57cec5SDimitry Andric break; 4109*0b57cec5SDimitry Andric case bitc::PARAMATTR_GROUP_BLOCK_ID: 4110*0b57cec5SDimitry Andric if (Error Err = parseAttributeGroupBlock()) 4111*0b57cec5SDimitry Andric return Err; 4112*0b57cec5SDimitry Andric break; 4113*0b57cec5SDimitry Andric case bitc::TYPE_BLOCK_ID_NEW: 4114*0b57cec5SDimitry Andric if (Error Err = parseTypeTable()) 4115*0b57cec5SDimitry Andric return Err; 4116*0b57cec5SDimitry Andric break; 4117*0b57cec5SDimitry Andric case bitc::VALUE_SYMTAB_BLOCK_ID: 4118*0b57cec5SDimitry Andric if (!SeenValueSymbolTable) { 4119*0b57cec5SDimitry Andric // Either this is an old form VST without function index and an 4120*0b57cec5SDimitry Andric // associated VST forward declaration record (which would have caused 4121*0b57cec5SDimitry Andric // the VST to be jumped to and parsed before it was encountered 4122*0b57cec5SDimitry Andric // normally in the stream), or there were no function blocks to 4123*0b57cec5SDimitry Andric // trigger an earlier parsing of the VST. 4124*0b57cec5SDimitry Andric assert(VSTOffset == 0 || FunctionsWithBodies.empty()); 4125*0b57cec5SDimitry Andric if (Error Err = parseValueSymbolTable()) 4126*0b57cec5SDimitry Andric return Err; 4127*0b57cec5SDimitry Andric SeenValueSymbolTable = true; 4128*0b57cec5SDimitry Andric } else { 4129*0b57cec5SDimitry Andric // We must have had a VST forward declaration record, which caused 4130*0b57cec5SDimitry Andric // the parser to jump to and parse the VST earlier. 4131*0b57cec5SDimitry Andric assert(VSTOffset > 0); 4132*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 4133*0b57cec5SDimitry Andric return Err; 4134*0b57cec5SDimitry Andric } 4135*0b57cec5SDimitry Andric break; 4136*0b57cec5SDimitry Andric case bitc::CONSTANTS_BLOCK_ID: 4137*0b57cec5SDimitry Andric if (Error Err = parseConstants()) 4138*0b57cec5SDimitry Andric return Err; 4139*0b57cec5SDimitry Andric if (Error Err = resolveGlobalAndIndirectSymbolInits()) 4140*0b57cec5SDimitry Andric return Err; 4141*0b57cec5SDimitry Andric break; 4142*0b57cec5SDimitry Andric case bitc::METADATA_BLOCK_ID: 4143*0b57cec5SDimitry Andric if (ShouldLazyLoadMetadata) { 4144*0b57cec5SDimitry Andric if (Error Err = rememberAndSkipMetadata()) 4145*0b57cec5SDimitry Andric return Err; 4146*0b57cec5SDimitry Andric break; 4147*0b57cec5SDimitry Andric } 4148*0b57cec5SDimitry Andric assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata"); 4149*0b57cec5SDimitry Andric if (Error Err = MDLoader->parseModuleMetadata()) 4150*0b57cec5SDimitry Andric return Err; 4151*0b57cec5SDimitry Andric break; 4152*0b57cec5SDimitry Andric case bitc::METADATA_KIND_BLOCK_ID: 4153*0b57cec5SDimitry Andric if (Error Err = MDLoader->parseMetadataKinds()) 4154*0b57cec5SDimitry Andric return Err; 4155*0b57cec5SDimitry Andric break; 4156*0b57cec5SDimitry Andric case bitc::FUNCTION_BLOCK_ID: 41575ffd83dbSDimitry Andric ResolveDataLayout(); 41585ffd83dbSDimitry Andric 4159*0b57cec5SDimitry Andric // If this is the first function body we've seen, reverse the 4160*0b57cec5SDimitry Andric // FunctionsWithBodies list. 4161*0b57cec5SDimitry Andric if (!SeenFirstFunctionBody) { 4162*0b57cec5SDimitry Andric std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end()); 4163*0b57cec5SDimitry Andric if (Error Err = globalCleanup()) 4164*0b57cec5SDimitry Andric return Err; 4165*0b57cec5SDimitry Andric SeenFirstFunctionBody = true; 4166*0b57cec5SDimitry Andric } 4167*0b57cec5SDimitry Andric 4168*0b57cec5SDimitry Andric if (VSTOffset > 0) { 4169*0b57cec5SDimitry Andric // If we have a VST forward declaration record, make sure we 4170*0b57cec5SDimitry Andric // parse the VST now if we haven't already. It is needed to 4171*0b57cec5SDimitry Andric // set up the DeferredFunctionInfo vector for lazy reading. 4172*0b57cec5SDimitry Andric if (!SeenValueSymbolTable) { 4173*0b57cec5SDimitry Andric if (Error Err = BitcodeReader::parseValueSymbolTable(VSTOffset)) 4174*0b57cec5SDimitry Andric return Err; 4175*0b57cec5SDimitry Andric SeenValueSymbolTable = true; 4176*0b57cec5SDimitry Andric // Fall through so that we record the NextUnreadBit below. 4177*0b57cec5SDimitry Andric // This is necessary in case we have an anonymous function that 4178*0b57cec5SDimitry Andric // is later materialized. Since it will not have a VST entry we 4179*0b57cec5SDimitry Andric // need to fall back to the lazy parse to find its offset. 4180*0b57cec5SDimitry Andric } else { 4181*0b57cec5SDimitry Andric // If we have a VST forward declaration record, but have already 4182*0b57cec5SDimitry Andric // parsed the VST (just above, when the first function body was 4183*0b57cec5SDimitry Andric // encountered here), then we are resuming the parse after 4184*0b57cec5SDimitry Andric // materializing functions. The ResumeBit points to the 4185*0b57cec5SDimitry Andric // start of the last function block recorded in the 4186*0b57cec5SDimitry Andric // DeferredFunctionInfo map. Skip it. 4187*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 4188*0b57cec5SDimitry Andric return Err; 4189*0b57cec5SDimitry Andric continue; 4190*0b57cec5SDimitry Andric } 4191*0b57cec5SDimitry Andric } 4192*0b57cec5SDimitry Andric 4193*0b57cec5SDimitry Andric // Support older bitcode files that did not have the function 4194*0b57cec5SDimitry Andric // index in the VST, nor a VST forward declaration record, as 4195*0b57cec5SDimitry Andric // well as anonymous functions that do not have VST entries. 4196*0b57cec5SDimitry Andric // Build the DeferredFunctionInfo vector on the fly. 4197*0b57cec5SDimitry Andric if (Error Err = rememberAndSkipFunctionBody()) 4198*0b57cec5SDimitry Andric return Err; 4199*0b57cec5SDimitry Andric 4200*0b57cec5SDimitry Andric // Suspend parsing when we reach the function bodies. Subsequent 4201*0b57cec5SDimitry Andric // materialization calls will resume it when necessary. If the bitcode 4202*0b57cec5SDimitry Andric // file is old, the symbol table will be at the end instead and will not 4203*0b57cec5SDimitry Andric // have been seen yet. In this case, just finish the parse now. 4204*0b57cec5SDimitry Andric if (SeenValueSymbolTable) { 4205*0b57cec5SDimitry Andric NextUnreadBit = Stream.GetCurrentBitNo(); 4206*0b57cec5SDimitry Andric // After the VST has been parsed, we need to make sure intrinsic name 4207*0b57cec5SDimitry Andric // are auto-upgraded. 4208*0b57cec5SDimitry Andric return globalCleanup(); 4209*0b57cec5SDimitry Andric } 4210*0b57cec5SDimitry Andric break; 4211*0b57cec5SDimitry Andric case bitc::USELIST_BLOCK_ID: 4212*0b57cec5SDimitry Andric if (Error Err = parseUseLists()) 4213*0b57cec5SDimitry Andric return Err; 4214*0b57cec5SDimitry Andric break; 4215*0b57cec5SDimitry Andric case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID: 4216*0b57cec5SDimitry Andric if (Error Err = parseOperandBundleTags()) 4217*0b57cec5SDimitry Andric return Err; 4218*0b57cec5SDimitry Andric break; 4219*0b57cec5SDimitry Andric case bitc::SYNC_SCOPE_NAMES_BLOCK_ID: 4220*0b57cec5SDimitry Andric if (Error Err = parseSyncScopeNames()) 4221*0b57cec5SDimitry Andric return Err; 4222*0b57cec5SDimitry Andric break; 4223*0b57cec5SDimitry Andric } 4224*0b57cec5SDimitry Andric continue; 4225*0b57cec5SDimitry Andric 4226*0b57cec5SDimitry Andric case BitstreamEntry::Record: 4227*0b57cec5SDimitry Andric // The interesting case. 4228*0b57cec5SDimitry Andric break; 4229*0b57cec5SDimitry Andric } 4230*0b57cec5SDimitry Andric 4231*0b57cec5SDimitry Andric // Read a record. 4232*0b57cec5SDimitry Andric Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); 4233*0b57cec5SDimitry Andric if (!MaybeBitCode) 4234*0b57cec5SDimitry Andric return MaybeBitCode.takeError(); 4235*0b57cec5SDimitry Andric switch (unsigned BitCode = MaybeBitCode.get()) { 4236*0b57cec5SDimitry Andric default: break; // Default behavior, ignore unknown content. 4237*0b57cec5SDimitry Andric case bitc::MODULE_CODE_VERSION: { 4238*0b57cec5SDimitry Andric Expected<unsigned> VersionOrErr = parseVersionRecord(Record); 4239*0b57cec5SDimitry Andric if (!VersionOrErr) 4240*0b57cec5SDimitry Andric return VersionOrErr.takeError(); 4241*0b57cec5SDimitry Andric UseRelativeIDs = *VersionOrErr >= 1; 4242*0b57cec5SDimitry Andric break; 4243*0b57cec5SDimitry Andric } 4244*0b57cec5SDimitry Andric case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] 42455ffd83dbSDimitry Andric if (ResolvedDataLayout) 42465ffd83dbSDimitry Andric return error("target triple too late in module"); 4247*0b57cec5SDimitry Andric std::string S; 4248*0b57cec5SDimitry Andric if (convertToString(Record, 0, S)) 4249*0b57cec5SDimitry Andric return error("Invalid record"); 4250*0b57cec5SDimitry Andric TheModule->setTargetTriple(S); 4251*0b57cec5SDimitry Andric break; 4252*0b57cec5SDimitry Andric } 4253*0b57cec5SDimitry Andric case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N] 42545ffd83dbSDimitry Andric if (ResolvedDataLayout) 42555ffd83dbSDimitry Andric return error("datalayout too late in module"); 4256*0b57cec5SDimitry Andric std::string S; 4257*0b57cec5SDimitry Andric if (convertToString(Record, 0, S)) 4258*0b57cec5SDimitry Andric return error("Invalid record"); 425981ad6265SDimitry Andric Expected<DataLayout> MaybeDL = DataLayout::parse(S); 426081ad6265SDimitry Andric if (!MaybeDL) 426181ad6265SDimitry Andric return MaybeDL.takeError(); 426281ad6265SDimitry Andric TheModule->setDataLayout(MaybeDL.get()); 4263*0b57cec5SDimitry Andric break; 4264*0b57cec5SDimitry Andric } 4265*0b57cec5SDimitry Andric case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N] 4266*0b57cec5SDimitry Andric std::string S; 4267*0b57cec5SDimitry Andric if (convertToString(Record, 0, S)) 4268*0b57cec5SDimitry Andric return error("Invalid record"); 4269*0b57cec5SDimitry Andric TheModule->setModuleInlineAsm(S); 4270*0b57cec5SDimitry Andric break; 4271*0b57cec5SDimitry Andric } 4272*0b57cec5SDimitry Andric case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N] 42735ffd83dbSDimitry Andric // Deprecated, but still needed to read old bitcode files. 4274*0b57cec5SDimitry Andric std::string S; 4275*0b57cec5SDimitry Andric if (convertToString(Record, 0, S)) 4276*0b57cec5SDimitry Andric return error("Invalid record"); 4277*0b57cec5SDimitry Andric // Ignore value. 4278*0b57cec5SDimitry Andric break; 4279*0b57cec5SDimitry Andric } 4280*0b57cec5SDimitry Andric case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] 4281*0b57cec5SDimitry Andric std::string S; 4282*0b57cec5SDimitry Andric if (convertToString(Record, 0, S)) 4283*0b57cec5SDimitry Andric return error("Invalid record"); 4284*0b57cec5SDimitry Andric SectionTable.push_back(S); 4285*0b57cec5SDimitry Andric break; 4286*0b57cec5SDimitry Andric } 4287*0b57cec5SDimitry Andric case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N] 4288*0b57cec5SDimitry Andric std::string S; 4289*0b57cec5SDimitry Andric if (convertToString(Record, 0, S)) 4290*0b57cec5SDimitry Andric return error("Invalid record"); 4291*0b57cec5SDimitry Andric GCTable.push_back(S); 4292*0b57cec5SDimitry Andric break; 4293*0b57cec5SDimitry Andric } 4294*0b57cec5SDimitry Andric case bitc::MODULE_CODE_COMDAT: 4295*0b57cec5SDimitry Andric if (Error Err = parseComdatRecord(Record)) 4296*0b57cec5SDimitry Andric return Err; 4297*0b57cec5SDimitry Andric break; 429804eeddc0SDimitry Andric // FIXME: BitcodeReader should handle {GLOBALVAR, FUNCTION, ALIAS, IFUNC} 429904eeddc0SDimitry Andric // written by ThinLinkBitcodeWriter. See 430004eeddc0SDimitry Andric // `ThinLinkBitcodeWriter::writeSimplifiedModuleInfo` for the format of each 430104eeddc0SDimitry Andric // record 430204eeddc0SDimitry Andric // (https://github.com/llvm/llvm-project/blob/b6a93967d9c11e79802b5e75cec1584d6c8aa472/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp#L4714) 4303*0b57cec5SDimitry Andric case bitc::MODULE_CODE_GLOBALVAR: 4304*0b57cec5SDimitry Andric if (Error Err = parseGlobalVarRecord(Record)) 4305*0b57cec5SDimitry Andric return Err; 4306*0b57cec5SDimitry Andric break; 4307*0b57cec5SDimitry Andric case bitc::MODULE_CODE_FUNCTION: 43085ffd83dbSDimitry Andric ResolveDataLayout(); 4309*0b57cec5SDimitry Andric if (Error Err = parseFunctionRecord(Record)) 4310*0b57cec5SDimitry Andric return Err; 4311*0b57cec5SDimitry Andric break; 4312*0b57cec5SDimitry Andric case bitc::MODULE_CODE_IFUNC: 4313*0b57cec5SDimitry Andric case bitc::MODULE_CODE_ALIAS: 4314*0b57cec5SDimitry Andric case bitc::MODULE_CODE_ALIAS_OLD: 4315*0b57cec5SDimitry Andric if (Error Err = parseGlobalIndirectSymbolRecord(BitCode, Record)) 4316*0b57cec5SDimitry Andric return Err; 4317*0b57cec5SDimitry Andric break; 4318*0b57cec5SDimitry Andric /// MODULE_CODE_VSTOFFSET: [offset] 4319*0b57cec5SDimitry Andric case bitc::MODULE_CODE_VSTOFFSET: 4320e8d8bef9SDimitry Andric if (Record.empty()) 4321*0b57cec5SDimitry Andric return error("Invalid record"); 4322*0b57cec5SDimitry Andric // Note that we subtract 1 here because the offset is relative to one word 4323*0b57cec5SDimitry Andric // before the start of the identification or module block, which was 4324*0b57cec5SDimitry Andric // historically always the start of the regular bitcode header. 4325*0b57cec5SDimitry Andric VSTOffset = Record[0] - 1; 4326*0b57cec5SDimitry Andric break; 4327*0b57cec5SDimitry Andric /// MODULE_CODE_SOURCE_FILENAME: [namechar x N] 4328*0b57cec5SDimitry Andric case bitc::MODULE_CODE_SOURCE_FILENAME: 4329*0b57cec5SDimitry Andric SmallString<128> ValueName; 4330*0b57cec5SDimitry Andric if (convertToString(Record, 0, ValueName)) 4331*0b57cec5SDimitry Andric return error("Invalid record"); 4332*0b57cec5SDimitry Andric TheModule->setSourceFileName(ValueName); 4333*0b57cec5SDimitry Andric break; 4334*0b57cec5SDimitry Andric } 4335*0b57cec5SDimitry Andric Record.clear(); 4336*0b57cec5SDimitry Andric } 4337*0b57cec5SDimitry Andric } 4338*0b57cec5SDimitry Andric 4339*0b57cec5SDimitry Andric Error BitcodeReader::parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata, 43405ffd83dbSDimitry Andric bool IsImporting, 43415ffd83dbSDimitry Andric DataLayoutCallbackTy DataLayoutCallback) { 4342*0b57cec5SDimitry Andric TheModule = M; 4343*0b57cec5SDimitry Andric MDLoader = MetadataLoader(Stream, *M, ValueList, IsImporting, 4344*0b57cec5SDimitry Andric [&](unsigned ID) { return getTypeByID(ID); }); 43455ffd83dbSDimitry Andric return parseModule(0, ShouldLazyLoadMetadata, DataLayoutCallback); 4346*0b57cec5SDimitry Andric } 4347*0b57cec5SDimitry Andric 4348*0b57cec5SDimitry Andric Error BitcodeReader::typeCheckLoadStoreInst(Type *ValType, Type *PtrType) { 4349*0b57cec5SDimitry Andric if (!isa<PointerType>(PtrType)) 4350*0b57cec5SDimitry Andric return error("Load/Store operand is not a pointer type"); 4351*0b57cec5SDimitry Andric 4352fe6060f1SDimitry Andric if (!cast<PointerType>(PtrType)->isOpaqueOrPointeeTypeMatches(ValType)) 4353*0b57cec5SDimitry Andric return error("Explicit load/store type does not match pointee " 4354*0b57cec5SDimitry Andric "type of pointer operand"); 4355fe6060f1SDimitry Andric if (!PointerType::isLoadableOrStorableType(ValType)) 4356*0b57cec5SDimitry Andric return error("Cannot load/store from pointer"); 4357*0b57cec5SDimitry Andric return Error::success(); 4358*0b57cec5SDimitry Andric } 4359*0b57cec5SDimitry Andric 436081ad6265SDimitry Andric Error BitcodeReader::propagateAttributeTypes(CallBase *CB, 436181ad6265SDimitry Andric ArrayRef<unsigned> ArgTyIDs) { 436281ad6265SDimitry Andric AttributeList Attrs = CB->getAttributes(); 4363*0b57cec5SDimitry Andric for (unsigned i = 0; i != CB->arg_size(); ++i) { 4364fe6060f1SDimitry Andric for (Attribute::AttrKind Kind : {Attribute::ByVal, Attribute::StructRet, 4365fe6060f1SDimitry Andric Attribute::InAlloca}) { 436681ad6265SDimitry Andric if (!Attrs.hasParamAttr(i, Kind) || 436781ad6265SDimitry Andric Attrs.getParamAttr(i, Kind).getValueAsType()) 4368*0b57cec5SDimitry Andric continue; 4369*0b57cec5SDimitry Andric 437081ad6265SDimitry Andric Type *PtrEltTy = getPtrElementTypeByID(ArgTyIDs[i]); 437181ad6265SDimitry Andric if (!PtrEltTy) 437281ad6265SDimitry Andric return error("Missing element type for typed attribute upgrade"); 4373e8d8bef9SDimitry Andric 4374fe6060f1SDimitry Andric Attribute NewAttr; 4375fe6060f1SDimitry Andric switch (Kind) { 4376fe6060f1SDimitry Andric case Attribute::ByVal: 4377fe6060f1SDimitry Andric NewAttr = Attribute::getWithByValType(Context, PtrEltTy); 4378fe6060f1SDimitry Andric break; 4379fe6060f1SDimitry Andric case Attribute::StructRet: 4380fe6060f1SDimitry Andric NewAttr = Attribute::getWithStructRetType(Context, PtrEltTy); 4381fe6060f1SDimitry Andric break; 4382fe6060f1SDimitry Andric case Attribute::InAlloca: 4383fe6060f1SDimitry Andric NewAttr = Attribute::getWithInAllocaType(Context, PtrEltTy); 4384fe6060f1SDimitry Andric break; 4385fe6060f1SDimitry Andric default: 4386fe6060f1SDimitry Andric llvm_unreachable("not an upgraded type attribute"); 4387fe6060f1SDimitry Andric } 4388fe6060f1SDimitry Andric 438981ad6265SDimitry Andric Attrs = Attrs.addParamAttribute(Context, i, NewAttr); 4390e8d8bef9SDimitry Andric } 4391*0b57cec5SDimitry Andric } 4392fe6060f1SDimitry Andric 439304eeddc0SDimitry Andric if (CB->isInlineAsm()) { 439404eeddc0SDimitry Andric const InlineAsm *IA = cast<InlineAsm>(CB->getCalledOperand()); 439504eeddc0SDimitry Andric unsigned ArgNo = 0; 439604eeddc0SDimitry Andric for (const InlineAsm::ConstraintInfo &CI : IA->ParseConstraints()) { 439704eeddc0SDimitry Andric if (!CI.hasArg()) 439804eeddc0SDimitry Andric continue; 439904eeddc0SDimitry Andric 440081ad6265SDimitry Andric if (CI.isIndirect && !Attrs.getParamElementType(ArgNo)) { 440181ad6265SDimitry Andric Type *ElemTy = getPtrElementTypeByID(ArgTyIDs[ArgNo]); 440281ad6265SDimitry Andric if (!ElemTy) 440381ad6265SDimitry Andric return error("Missing element type for inline asm upgrade"); 440481ad6265SDimitry Andric Attrs = Attrs.addParamAttribute( 440581ad6265SDimitry Andric Context, ArgNo, 440681ad6265SDimitry Andric Attribute::get(Context, Attribute::ElementType, ElemTy)); 440704eeddc0SDimitry Andric } 440804eeddc0SDimitry Andric 440904eeddc0SDimitry Andric ArgNo++; 441004eeddc0SDimitry Andric } 441104eeddc0SDimitry Andric } 441204eeddc0SDimitry Andric 4413fe6060f1SDimitry Andric switch (CB->getIntrinsicID()) { 4414fe6060f1SDimitry Andric case Intrinsic::preserve_array_access_index: 4415fe6060f1SDimitry Andric case Intrinsic::preserve_struct_access_index: 441681ad6265SDimitry Andric case Intrinsic::aarch64_ldaxr: 441781ad6265SDimitry Andric case Intrinsic::aarch64_ldxr: 441881ad6265SDimitry Andric case Intrinsic::aarch64_stlxr: 441981ad6265SDimitry Andric case Intrinsic::aarch64_stxr: 442081ad6265SDimitry Andric case Intrinsic::arm_ldaex: 442181ad6265SDimitry Andric case Intrinsic::arm_ldrex: 442281ad6265SDimitry Andric case Intrinsic::arm_stlex: 442381ad6265SDimitry Andric case Intrinsic::arm_strex: { 442481ad6265SDimitry Andric unsigned ArgNo; 442581ad6265SDimitry Andric switch (CB->getIntrinsicID()) { 442681ad6265SDimitry Andric case Intrinsic::aarch64_stlxr: 442781ad6265SDimitry Andric case Intrinsic::aarch64_stxr: 442881ad6265SDimitry Andric case Intrinsic::arm_stlex: 442981ad6265SDimitry Andric case Intrinsic::arm_strex: 443081ad6265SDimitry Andric ArgNo = 1; 443181ad6265SDimitry Andric break; 443281ad6265SDimitry Andric default: 443381ad6265SDimitry Andric ArgNo = 0; 443481ad6265SDimitry Andric break; 443581ad6265SDimitry Andric } 443681ad6265SDimitry Andric if (!Attrs.getParamElementType(ArgNo)) { 443781ad6265SDimitry Andric Type *ElTy = getPtrElementTypeByID(ArgTyIDs[ArgNo]); 443881ad6265SDimitry Andric if (!ElTy) 443981ad6265SDimitry Andric return error("Missing element type for elementtype upgrade"); 4440fe6060f1SDimitry Andric Attribute NewAttr = Attribute::get(Context, Attribute::ElementType, ElTy); 444181ad6265SDimitry Andric Attrs = Attrs.addParamAttribute(Context, ArgNo, NewAttr); 4442fe6060f1SDimitry Andric } 4443fe6060f1SDimitry Andric break; 444481ad6265SDimitry Andric } 4445fe6060f1SDimitry Andric default: 4446fe6060f1SDimitry Andric break; 4447fe6060f1SDimitry Andric } 444881ad6265SDimitry Andric 444981ad6265SDimitry Andric CB->setAttributes(Attrs); 445081ad6265SDimitry Andric return Error::success(); 4451*0b57cec5SDimitry Andric } 4452*0b57cec5SDimitry Andric 4453*0b57cec5SDimitry Andric /// Lazily parse the specified function body block. 4454*0b57cec5SDimitry Andric Error BitcodeReader::parseFunctionBody(Function *F) { 4455*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID)) 4456*0b57cec5SDimitry Andric return Err; 4457*0b57cec5SDimitry Andric 4458*0b57cec5SDimitry Andric // Unexpected unresolved metadata when parsing function. 4459*0b57cec5SDimitry Andric if (MDLoader->hasFwdRefs()) 4460*0b57cec5SDimitry Andric return error("Invalid function metadata: incoming forward references"); 4461*0b57cec5SDimitry Andric 4462*0b57cec5SDimitry Andric InstructionList.clear(); 4463*0b57cec5SDimitry Andric unsigned ModuleValueListSize = ValueList.size(); 4464*0b57cec5SDimitry Andric unsigned ModuleMDLoaderSize = MDLoader->size(); 4465*0b57cec5SDimitry Andric 4466*0b57cec5SDimitry Andric // Add all the function arguments to the value table. 4467*0b57cec5SDimitry Andric unsigned ArgNo = 0; 446881ad6265SDimitry Andric unsigned FTyID = FunctionTypeIDs[F]; 4469*0b57cec5SDimitry Andric for (Argument &I : F->args()) { 447081ad6265SDimitry Andric unsigned ArgTyID = getContainedTypeID(FTyID, ArgNo + 1); 447181ad6265SDimitry Andric assert(I.getType() == getTypeByID(ArgTyID) && 4472*0b57cec5SDimitry Andric "Incorrect fully specified type for Function Argument"); 447381ad6265SDimitry Andric ValueList.push_back(&I, ArgTyID); 447481ad6265SDimitry Andric ++ArgNo; 4475*0b57cec5SDimitry Andric } 4476*0b57cec5SDimitry Andric unsigned NextValueNo = ValueList.size(); 4477*0b57cec5SDimitry Andric BasicBlock *CurBB = nullptr; 4478*0b57cec5SDimitry Andric unsigned CurBBNo = 0; 447981ad6265SDimitry Andric // Block into which constant expressions from phi nodes are materialized. 448081ad6265SDimitry Andric BasicBlock *PhiConstExprBB = nullptr; 448181ad6265SDimitry Andric // Edge blocks for phi nodes into which constant expressions have been 448281ad6265SDimitry Andric // expanded. 448381ad6265SDimitry Andric SmallMapVector<std::pair<BasicBlock *, BasicBlock *>, BasicBlock *, 4> 448481ad6265SDimitry Andric ConstExprEdgeBBs; 4485*0b57cec5SDimitry Andric 4486*0b57cec5SDimitry Andric DebugLoc LastLoc; 4487*0b57cec5SDimitry Andric auto getLastInstruction = [&]() -> Instruction * { 4488*0b57cec5SDimitry Andric if (CurBB && !CurBB->empty()) 4489*0b57cec5SDimitry Andric return &CurBB->back(); 4490*0b57cec5SDimitry Andric else if (CurBBNo && FunctionBBs[CurBBNo - 1] && 4491*0b57cec5SDimitry Andric !FunctionBBs[CurBBNo - 1]->empty()) 4492*0b57cec5SDimitry Andric return &FunctionBBs[CurBBNo - 1]->back(); 4493*0b57cec5SDimitry Andric return nullptr; 4494*0b57cec5SDimitry Andric }; 4495*0b57cec5SDimitry Andric 4496*0b57cec5SDimitry Andric std::vector<OperandBundleDef> OperandBundles; 4497*0b57cec5SDimitry Andric 4498*0b57cec5SDimitry Andric // Read all the records. 4499*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 4500*0b57cec5SDimitry Andric 4501*0b57cec5SDimitry Andric while (true) { 4502*0b57cec5SDimitry Andric Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4503*0b57cec5SDimitry Andric if (!MaybeEntry) 4504*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 4505*0b57cec5SDimitry Andric llvm::BitstreamEntry Entry = MaybeEntry.get(); 4506*0b57cec5SDimitry Andric 4507*0b57cec5SDimitry Andric switch (Entry.Kind) { 4508*0b57cec5SDimitry Andric case BitstreamEntry::Error: 4509*0b57cec5SDimitry Andric return error("Malformed block"); 4510*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 4511*0b57cec5SDimitry Andric goto OutOfRecordLoop; 4512*0b57cec5SDimitry Andric 4513*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: 4514*0b57cec5SDimitry Andric switch (Entry.ID) { 4515*0b57cec5SDimitry Andric default: // Skip unknown content. 4516*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 4517*0b57cec5SDimitry Andric return Err; 4518*0b57cec5SDimitry Andric break; 4519*0b57cec5SDimitry Andric case bitc::CONSTANTS_BLOCK_ID: 4520*0b57cec5SDimitry Andric if (Error Err = parseConstants()) 4521*0b57cec5SDimitry Andric return Err; 4522*0b57cec5SDimitry Andric NextValueNo = ValueList.size(); 4523*0b57cec5SDimitry Andric break; 4524*0b57cec5SDimitry Andric case bitc::VALUE_SYMTAB_BLOCK_ID: 4525*0b57cec5SDimitry Andric if (Error Err = parseValueSymbolTable()) 4526*0b57cec5SDimitry Andric return Err; 4527*0b57cec5SDimitry Andric break; 4528*0b57cec5SDimitry Andric case bitc::METADATA_ATTACHMENT_ID: 4529*0b57cec5SDimitry Andric if (Error Err = MDLoader->parseMetadataAttachment(*F, InstructionList)) 4530*0b57cec5SDimitry Andric return Err; 4531*0b57cec5SDimitry Andric break; 4532*0b57cec5SDimitry Andric case bitc::METADATA_BLOCK_ID: 4533*0b57cec5SDimitry Andric assert(DeferredMetadataInfo.empty() && 4534*0b57cec5SDimitry Andric "Must read all module-level metadata before function-level"); 4535*0b57cec5SDimitry Andric if (Error Err = MDLoader->parseFunctionMetadata()) 4536*0b57cec5SDimitry Andric return Err; 4537*0b57cec5SDimitry Andric break; 4538*0b57cec5SDimitry Andric case bitc::USELIST_BLOCK_ID: 4539*0b57cec5SDimitry Andric if (Error Err = parseUseLists()) 4540*0b57cec5SDimitry Andric return Err; 4541*0b57cec5SDimitry Andric break; 4542*0b57cec5SDimitry Andric } 4543*0b57cec5SDimitry Andric continue; 4544*0b57cec5SDimitry Andric 4545*0b57cec5SDimitry Andric case BitstreamEntry::Record: 4546*0b57cec5SDimitry Andric // The interesting case. 4547*0b57cec5SDimitry Andric break; 4548*0b57cec5SDimitry Andric } 4549*0b57cec5SDimitry Andric 4550*0b57cec5SDimitry Andric // Read a record. 4551*0b57cec5SDimitry Andric Record.clear(); 4552*0b57cec5SDimitry Andric Instruction *I = nullptr; 455381ad6265SDimitry Andric unsigned ResTypeID = InvalidTypeID; 4554*0b57cec5SDimitry Andric Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); 4555*0b57cec5SDimitry Andric if (!MaybeBitCode) 4556*0b57cec5SDimitry Andric return MaybeBitCode.takeError(); 4557*0b57cec5SDimitry Andric switch (unsigned BitCode = MaybeBitCode.get()) { 4558*0b57cec5SDimitry Andric default: // Default behavior: reject 4559*0b57cec5SDimitry Andric return error("Invalid value"); 4560*0b57cec5SDimitry Andric case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks] 4561e8d8bef9SDimitry Andric if (Record.empty() || Record[0] == 0) 4562*0b57cec5SDimitry Andric return error("Invalid record"); 4563*0b57cec5SDimitry Andric // Create all the basic blocks for the function. 4564*0b57cec5SDimitry Andric FunctionBBs.resize(Record[0]); 4565*0b57cec5SDimitry Andric 4566*0b57cec5SDimitry Andric // See if anything took the address of blocks in this function. 4567*0b57cec5SDimitry Andric auto BBFRI = BasicBlockFwdRefs.find(F); 4568*0b57cec5SDimitry Andric if (BBFRI == BasicBlockFwdRefs.end()) { 45694824e7fdSDimitry Andric for (BasicBlock *&BB : FunctionBBs) 45704824e7fdSDimitry Andric BB = BasicBlock::Create(Context, "", F); 4571*0b57cec5SDimitry Andric } else { 4572*0b57cec5SDimitry Andric auto &BBRefs = BBFRI->second; 4573*0b57cec5SDimitry Andric // Check for invalid basic block references. 4574*0b57cec5SDimitry Andric if (BBRefs.size() > FunctionBBs.size()) 4575*0b57cec5SDimitry Andric return error("Invalid ID"); 4576*0b57cec5SDimitry Andric assert(!BBRefs.empty() && "Unexpected empty array"); 4577*0b57cec5SDimitry Andric assert(!BBRefs.front() && "Invalid reference to entry block"); 4578*0b57cec5SDimitry Andric for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E; 4579*0b57cec5SDimitry Andric ++I) 4580*0b57cec5SDimitry Andric if (I < RE && BBRefs[I]) { 4581*0b57cec5SDimitry Andric BBRefs[I]->insertInto(F); 4582*0b57cec5SDimitry Andric FunctionBBs[I] = BBRefs[I]; 4583*0b57cec5SDimitry Andric } else { 4584*0b57cec5SDimitry Andric FunctionBBs[I] = BasicBlock::Create(Context, "", F); 4585*0b57cec5SDimitry Andric } 4586*0b57cec5SDimitry Andric 4587*0b57cec5SDimitry Andric // Erase from the table. 4588*0b57cec5SDimitry Andric BasicBlockFwdRefs.erase(BBFRI); 4589*0b57cec5SDimitry Andric } 4590*0b57cec5SDimitry Andric 4591*0b57cec5SDimitry Andric CurBB = FunctionBBs[0]; 4592*0b57cec5SDimitry Andric continue; 4593*0b57cec5SDimitry Andric } 4594*0b57cec5SDimitry Andric 459581ad6265SDimitry Andric case bitc::FUNC_CODE_BLOCKADDR_USERS: // BLOCKADDR_USERS: [vals...] 459681ad6265SDimitry Andric // The record should not be emitted if it's an empty list. 459781ad6265SDimitry Andric if (Record.empty()) 459881ad6265SDimitry Andric return error("Invalid record"); 459981ad6265SDimitry Andric // When we have the RARE case of a BlockAddress Constant that is not 460081ad6265SDimitry Andric // scoped to the Function it refers to, we need to conservatively 460181ad6265SDimitry Andric // materialize the referred to Function, regardless of whether or not 460281ad6265SDimitry Andric // that Function will ultimately be linked, otherwise users of 460381ad6265SDimitry Andric // BitcodeReader might start splicing out Function bodies such that we 460481ad6265SDimitry Andric // might no longer be able to materialize the BlockAddress since the 460581ad6265SDimitry Andric // BasicBlock (and entire body of the Function) the BlockAddress refers 460681ad6265SDimitry Andric // to may have been moved. In the case that the user of BitcodeReader 460781ad6265SDimitry Andric // decides ultimately not to link the Function body, materializing here 460881ad6265SDimitry Andric // could be considered wasteful, but it's better than a deserialization 460981ad6265SDimitry Andric // failure as described. This keeps BitcodeReader unaware of complex 461081ad6265SDimitry Andric // linkage policy decisions such as those use by LTO, leaving those 461181ad6265SDimitry Andric // decisions "one layer up." 461281ad6265SDimitry Andric for (uint64_t ValID : Record) 461381ad6265SDimitry Andric if (auto *F = dyn_cast<Function>(ValueList[ValID])) 461481ad6265SDimitry Andric BackwardRefFunctions.push_back(F); 461581ad6265SDimitry Andric else 461681ad6265SDimitry Andric return error("Invalid record"); 461781ad6265SDimitry Andric 461881ad6265SDimitry Andric continue; 461981ad6265SDimitry Andric 4620*0b57cec5SDimitry Andric case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN 4621*0b57cec5SDimitry Andric // This record indicates that the last instruction is at the same 4622*0b57cec5SDimitry Andric // location as the previous instruction with a location. 4623*0b57cec5SDimitry Andric I = getLastInstruction(); 4624*0b57cec5SDimitry Andric 4625*0b57cec5SDimitry Andric if (!I) 4626*0b57cec5SDimitry Andric return error("Invalid record"); 4627*0b57cec5SDimitry Andric I->setDebugLoc(LastLoc); 4628*0b57cec5SDimitry Andric I = nullptr; 4629*0b57cec5SDimitry Andric continue; 4630*0b57cec5SDimitry Andric 4631*0b57cec5SDimitry Andric case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia] 4632*0b57cec5SDimitry Andric I = getLastInstruction(); 4633*0b57cec5SDimitry Andric if (!I || Record.size() < 4) 4634*0b57cec5SDimitry Andric return error("Invalid record"); 4635*0b57cec5SDimitry Andric 4636*0b57cec5SDimitry Andric unsigned Line = Record[0], Col = Record[1]; 4637*0b57cec5SDimitry Andric unsigned ScopeID = Record[2], IAID = Record[3]; 4638*0b57cec5SDimitry Andric bool isImplicitCode = Record.size() == 5 && Record[4]; 4639*0b57cec5SDimitry Andric 4640*0b57cec5SDimitry Andric MDNode *Scope = nullptr, *IA = nullptr; 4641*0b57cec5SDimitry Andric if (ScopeID) { 4642*0b57cec5SDimitry Andric Scope = dyn_cast_or_null<MDNode>( 4643*0b57cec5SDimitry Andric MDLoader->getMetadataFwdRefOrLoad(ScopeID - 1)); 4644*0b57cec5SDimitry Andric if (!Scope) 4645*0b57cec5SDimitry Andric return error("Invalid record"); 4646*0b57cec5SDimitry Andric } 4647*0b57cec5SDimitry Andric if (IAID) { 4648*0b57cec5SDimitry Andric IA = dyn_cast_or_null<MDNode>( 4649*0b57cec5SDimitry Andric MDLoader->getMetadataFwdRefOrLoad(IAID - 1)); 4650*0b57cec5SDimitry Andric if (!IA) 4651*0b57cec5SDimitry Andric return error("Invalid record"); 4652*0b57cec5SDimitry Andric } 4653e8d8bef9SDimitry Andric LastLoc = DILocation::get(Scope->getContext(), Line, Col, Scope, IA, 4654e8d8bef9SDimitry Andric isImplicitCode); 4655*0b57cec5SDimitry Andric I->setDebugLoc(LastLoc); 4656*0b57cec5SDimitry Andric I = nullptr; 4657*0b57cec5SDimitry Andric continue; 4658*0b57cec5SDimitry Andric } 4659*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_UNOP: { // UNOP: [opval, ty, opcode] 4660*0b57cec5SDimitry Andric unsigned OpNum = 0; 4661*0b57cec5SDimitry Andric Value *LHS; 466281ad6265SDimitry Andric unsigned TypeID; 466381ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, LHS, TypeID, CurBB) || 4664*0b57cec5SDimitry Andric OpNum+1 > Record.size()) 4665*0b57cec5SDimitry Andric return error("Invalid record"); 4666*0b57cec5SDimitry Andric 4667*0b57cec5SDimitry Andric int Opc = getDecodedUnaryOpcode(Record[OpNum++], LHS->getType()); 4668*0b57cec5SDimitry Andric if (Opc == -1) 4669*0b57cec5SDimitry Andric return error("Invalid record"); 4670*0b57cec5SDimitry Andric I = UnaryOperator::Create((Instruction::UnaryOps)Opc, LHS); 467181ad6265SDimitry Andric ResTypeID = TypeID; 4672*0b57cec5SDimitry Andric InstructionList.push_back(I); 4673*0b57cec5SDimitry Andric if (OpNum < Record.size()) { 4674*0b57cec5SDimitry Andric if (isa<FPMathOperator>(I)) { 4675*0b57cec5SDimitry Andric FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]); 4676*0b57cec5SDimitry Andric if (FMF.any()) 4677*0b57cec5SDimitry Andric I->setFastMathFlags(FMF); 4678*0b57cec5SDimitry Andric } 4679*0b57cec5SDimitry Andric } 4680*0b57cec5SDimitry Andric break; 4681*0b57cec5SDimitry Andric } 4682*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode] 4683*0b57cec5SDimitry Andric unsigned OpNum = 0; 4684*0b57cec5SDimitry Andric Value *LHS, *RHS; 468581ad6265SDimitry Andric unsigned TypeID; 468681ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, LHS, TypeID, CurBB) || 468781ad6265SDimitry Andric popValue(Record, OpNum, NextValueNo, LHS->getType(), TypeID, RHS, 468881ad6265SDimitry Andric CurBB) || 4689*0b57cec5SDimitry Andric OpNum+1 > Record.size()) 4690*0b57cec5SDimitry Andric return error("Invalid record"); 4691*0b57cec5SDimitry Andric 4692*0b57cec5SDimitry Andric int Opc = getDecodedBinaryOpcode(Record[OpNum++], LHS->getType()); 4693*0b57cec5SDimitry Andric if (Opc == -1) 4694*0b57cec5SDimitry Andric return error("Invalid record"); 4695*0b57cec5SDimitry Andric I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); 469681ad6265SDimitry Andric ResTypeID = TypeID; 4697*0b57cec5SDimitry Andric InstructionList.push_back(I); 4698*0b57cec5SDimitry Andric if (OpNum < Record.size()) { 4699*0b57cec5SDimitry Andric if (Opc == Instruction::Add || 4700*0b57cec5SDimitry Andric Opc == Instruction::Sub || 4701*0b57cec5SDimitry Andric Opc == Instruction::Mul || 4702*0b57cec5SDimitry Andric Opc == Instruction::Shl) { 4703*0b57cec5SDimitry Andric if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP)) 4704*0b57cec5SDimitry Andric cast<BinaryOperator>(I)->setHasNoSignedWrap(true); 4705*0b57cec5SDimitry Andric if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) 4706*0b57cec5SDimitry Andric cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true); 4707*0b57cec5SDimitry Andric } else if (Opc == Instruction::SDiv || 4708*0b57cec5SDimitry Andric Opc == Instruction::UDiv || 4709*0b57cec5SDimitry Andric Opc == Instruction::LShr || 4710*0b57cec5SDimitry Andric Opc == Instruction::AShr) { 4711*0b57cec5SDimitry Andric if (Record[OpNum] & (1 << bitc::PEO_EXACT)) 4712*0b57cec5SDimitry Andric cast<BinaryOperator>(I)->setIsExact(true); 4713*0b57cec5SDimitry Andric } else if (isa<FPMathOperator>(I)) { 4714*0b57cec5SDimitry Andric FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]); 4715*0b57cec5SDimitry Andric if (FMF.any()) 4716*0b57cec5SDimitry Andric I->setFastMathFlags(FMF); 4717*0b57cec5SDimitry Andric } 4718*0b57cec5SDimitry Andric 4719*0b57cec5SDimitry Andric } 4720*0b57cec5SDimitry Andric break; 4721*0b57cec5SDimitry Andric } 4722*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc] 4723*0b57cec5SDimitry Andric unsigned OpNum = 0; 4724*0b57cec5SDimitry Andric Value *Op; 472581ad6265SDimitry Andric unsigned OpTypeID; 472681ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB) || 4727*0b57cec5SDimitry Andric OpNum+2 != Record.size()) 4728*0b57cec5SDimitry Andric return error("Invalid record"); 4729*0b57cec5SDimitry Andric 473081ad6265SDimitry Andric ResTypeID = Record[OpNum]; 473181ad6265SDimitry Andric Type *ResTy = getTypeByID(ResTypeID); 4732*0b57cec5SDimitry Andric int Opc = getDecodedCastOpcode(Record[OpNum + 1]); 4733*0b57cec5SDimitry Andric if (Opc == -1 || !ResTy) 4734*0b57cec5SDimitry Andric return error("Invalid record"); 4735*0b57cec5SDimitry Andric Instruction *Temp = nullptr; 4736*0b57cec5SDimitry Andric if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) { 4737*0b57cec5SDimitry Andric if (Temp) { 4738*0b57cec5SDimitry Andric InstructionList.push_back(Temp); 4739480093f4SDimitry Andric assert(CurBB && "No current BB?"); 4740*0b57cec5SDimitry Andric CurBB->getInstList().push_back(Temp); 4741*0b57cec5SDimitry Andric } 4742*0b57cec5SDimitry Andric } else { 4743*0b57cec5SDimitry Andric auto CastOp = (Instruction::CastOps)Opc; 4744*0b57cec5SDimitry Andric if (!CastInst::castIsValid(CastOp, Op, ResTy)) 4745*0b57cec5SDimitry Andric return error("Invalid cast"); 4746*0b57cec5SDimitry Andric I = CastInst::Create(CastOp, Op, ResTy); 4747*0b57cec5SDimitry Andric } 4748*0b57cec5SDimitry Andric InstructionList.push_back(I); 4749*0b57cec5SDimitry Andric break; 4750*0b57cec5SDimitry Andric } 4751*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD: 4752*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_GEP_OLD: 4753*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands] 4754*0b57cec5SDimitry Andric unsigned OpNum = 0; 4755*0b57cec5SDimitry Andric 475681ad6265SDimitry Andric unsigned TyID; 4757*0b57cec5SDimitry Andric Type *Ty; 4758*0b57cec5SDimitry Andric bool InBounds; 4759*0b57cec5SDimitry Andric 4760*0b57cec5SDimitry Andric if (BitCode == bitc::FUNC_CODE_INST_GEP) { 4761*0b57cec5SDimitry Andric InBounds = Record[OpNum++]; 476281ad6265SDimitry Andric TyID = Record[OpNum++]; 476381ad6265SDimitry Andric Ty = getTypeByID(TyID); 4764*0b57cec5SDimitry Andric } else { 4765*0b57cec5SDimitry Andric InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD; 476681ad6265SDimitry Andric TyID = InvalidTypeID; 4767*0b57cec5SDimitry Andric Ty = nullptr; 4768*0b57cec5SDimitry Andric } 4769*0b57cec5SDimitry Andric 4770*0b57cec5SDimitry Andric Value *BasePtr; 477181ad6265SDimitry Andric unsigned BasePtrTypeID; 477281ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr, BasePtrTypeID, 477381ad6265SDimitry Andric CurBB)) 4774*0b57cec5SDimitry Andric return error("Invalid record"); 4775*0b57cec5SDimitry Andric 4776*0b57cec5SDimitry Andric if (!Ty) { 477781ad6265SDimitry Andric TyID = getContainedTypeID(BasePtrTypeID); 477881ad6265SDimitry Andric if (BasePtr->getType()->isVectorTy()) 477981ad6265SDimitry Andric TyID = getContainedTypeID(TyID); 478081ad6265SDimitry Andric Ty = getTypeByID(TyID); 4781fe6060f1SDimitry Andric } else if (!cast<PointerType>(BasePtr->getType()->getScalarType()) 4782fe6060f1SDimitry Andric ->isOpaqueOrPointeeTypeMatches(Ty)) { 4783*0b57cec5SDimitry Andric return error( 4784*0b57cec5SDimitry Andric "Explicit gep type does not match pointee type of pointer operand"); 4785fe6060f1SDimitry Andric } 4786*0b57cec5SDimitry Andric 4787*0b57cec5SDimitry Andric SmallVector<Value*, 16> GEPIdx; 4788*0b57cec5SDimitry Andric while (OpNum != Record.size()) { 4789*0b57cec5SDimitry Andric Value *Op; 479081ad6265SDimitry Andric unsigned OpTypeID; 479181ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB)) 4792*0b57cec5SDimitry Andric return error("Invalid record"); 4793*0b57cec5SDimitry Andric GEPIdx.push_back(Op); 4794*0b57cec5SDimitry Andric } 4795*0b57cec5SDimitry Andric 4796*0b57cec5SDimitry Andric I = GetElementPtrInst::Create(Ty, BasePtr, GEPIdx); 4797*0b57cec5SDimitry Andric 479881ad6265SDimitry Andric ResTypeID = TyID; 479981ad6265SDimitry Andric if (cast<GEPOperator>(I)->getNumIndices() != 0) { 480081ad6265SDimitry Andric auto GTI = std::next(gep_type_begin(I)); 480181ad6265SDimitry Andric for (Value *Idx : drop_begin(cast<GEPOperator>(I)->indices())) { 480281ad6265SDimitry Andric unsigned SubType = 0; 480381ad6265SDimitry Andric if (GTI.isStruct()) { 480481ad6265SDimitry Andric ConstantInt *IdxC = 480581ad6265SDimitry Andric Idx->getType()->isVectorTy() 480681ad6265SDimitry Andric ? cast<ConstantInt>(cast<Constant>(Idx)->getSplatValue()) 480781ad6265SDimitry Andric : cast<ConstantInt>(Idx); 480881ad6265SDimitry Andric SubType = IdxC->getZExtValue(); 480981ad6265SDimitry Andric } 481081ad6265SDimitry Andric ResTypeID = getContainedTypeID(ResTypeID, SubType); 481181ad6265SDimitry Andric ++GTI; 481281ad6265SDimitry Andric } 481381ad6265SDimitry Andric } 481481ad6265SDimitry Andric 481581ad6265SDimitry Andric // At this point ResTypeID is the result element type. We need a pointer 481681ad6265SDimitry Andric // or vector of pointer to it. 481781ad6265SDimitry Andric ResTypeID = getVirtualTypeID(I->getType()->getScalarType(), ResTypeID); 481881ad6265SDimitry Andric if (I->getType()->isVectorTy()) 481981ad6265SDimitry Andric ResTypeID = getVirtualTypeID(I->getType(), ResTypeID); 482081ad6265SDimitry Andric 4821*0b57cec5SDimitry Andric InstructionList.push_back(I); 4822*0b57cec5SDimitry Andric if (InBounds) 4823*0b57cec5SDimitry Andric cast<GetElementPtrInst>(I)->setIsInBounds(true); 4824*0b57cec5SDimitry Andric break; 4825*0b57cec5SDimitry Andric } 4826*0b57cec5SDimitry Andric 4827*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_EXTRACTVAL: { 4828*0b57cec5SDimitry Andric // EXTRACTVAL: [opty, opval, n x indices] 4829*0b57cec5SDimitry Andric unsigned OpNum = 0; 4830*0b57cec5SDimitry Andric Value *Agg; 483181ad6265SDimitry Andric unsigned AggTypeID; 483281ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Agg, AggTypeID, CurBB)) 4833*0b57cec5SDimitry Andric return error("Invalid record"); 4834fe6060f1SDimitry Andric Type *Ty = Agg->getType(); 4835*0b57cec5SDimitry Andric 4836*0b57cec5SDimitry Andric unsigned RecSize = Record.size(); 4837*0b57cec5SDimitry Andric if (OpNum == RecSize) 4838*0b57cec5SDimitry Andric return error("EXTRACTVAL: Invalid instruction with 0 indices"); 4839*0b57cec5SDimitry Andric 4840*0b57cec5SDimitry Andric SmallVector<unsigned, 4> EXTRACTVALIdx; 484181ad6265SDimitry Andric ResTypeID = AggTypeID; 4842*0b57cec5SDimitry Andric for (; OpNum != RecSize; ++OpNum) { 4843fe6060f1SDimitry Andric bool IsArray = Ty->isArrayTy(); 4844fe6060f1SDimitry Andric bool IsStruct = Ty->isStructTy(); 4845*0b57cec5SDimitry Andric uint64_t Index = Record[OpNum]; 4846*0b57cec5SDimitry Andric 4847*0b57cec5SDimitry Andric if (!IsStruct && !IsArray) 4848*0b57cec5SDimitry Andric return error("EXTRACTVAL: Invalid type"); 4849*0b57cec5SDimitry Andric if ((unsigned)Index != Index) 4850*0b57cec5SDimitry Andric return error("Invalid value"); 4851fe6060f1SDimitry Andric if (IsStruct && Index >= Ty->getStructNumElements()) 4852*0b57cec5SDimitry Andric return error("EXTRACTVAL: Invalid struct index"); 4853fe6060f1SDimitry Andric if (IsArray && Index >= Ty->getArrayNumElements()) 4854*0b57cec5SDimitry Andric return error("EXTRACTVAL: Invalid array index"); 4855*0b57cec5SDimitry Andric EXTRACTVALIdx.push_back((unsigned)Index); 4856*0b57cec5SDimitry Andric 485781ad6265SDimitry Andric if (IsStruct) { 4858fe6060f1SDimitry Andric Ty = Ty->getStructElementType(Index); 485981ad6265SDimitry Andric ResTypeID = getContainedTypeID(ResTypeID, Index); 486081ad6265SDimitry Andric } else { 4861fe6060f1SDimitry Andric Ty = Ty->getArrayElementType(); 486281ad6265SDimitry Andric ResTypeID = getContainedTypeID(ResTypeID); 486381ad6265SDimitry Andric } 4864*0b57cec5SDimitry Andric } 4865*0b57cec5SDimitry Andric 4866*0b57cec5SDimitry Andric I = ExtractValueInst::Create(Agg, EXTRACTVALIdx); 4867*0b57cec5SDimitry Andric InstructionList.push_back(I); 4868*0b57cec5SDimitry Andric break; 4869*0b57cec5SDimitry Andric } 4870*0b57cec5SDimitry Andric 4871*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_INSERTVAL: { 4872*0b57cec5SDimitry Andric // INSERTVAL: [opty, opval, opty, opval, n x indices] 4873*0b57cec5SDimitry Andric unsigned OpNum = 0; 4874*0b57cec5SDimitry Andric Value *Agg; 487581ad6265SDimitry Andric unsigned AggTypeID; 487681ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Agg, AggTypeID, CurBB)) 4877*0b57cec5SDimitry Andric return error("Invalid record"); 4878*0b57cec5SDimitry Andric Value *Val; 487981ad6265SDimitry Andric unsigned ValTypeID; 488081ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Val, ValTypeID, CurBB)) 4881*0b57cec5SDimitry Andric return error("Invalid record"); 4882*0b57cec5SDimitry Andric 4883*0b57cec5SDimitry Andric unsigned RecSize = Record.size(); 4884*0b57cec5SDimitry Andric if (OpNum == RecSize) 4885*0b57cec5SDimitry Andric return error("INSERTVAL: Invalid instruction with 0 indices"); 4886*0b57cec5SDimitry Andric 4887*0b57cec5SDimitry Andric SmallVector<unsigned, 4> INSERTVALIdx; 4888*0b57cec5SDimitry Andric Type *CurTy = Agg->getType(); 4889*0b57cec5SDimitry Andric for (; OpNum != RecSize; ++OpNum) { 4890*0b57cec5SDimitry Andric bool IsArray = CurTy->isArrayTy(); 4891*0b57cec5SDimitry Andric bool IsStruct = CurTy->isStructTy(); 4892*0b57cec5SDimitry Andric uint64_t Index = Record[OpNum]; 4893*0b57cec5SDimitry Andric 4894*0b57cec5SDimitry Andric if (!IsStruct && !IsArray) 4895*0b57cec5SDimitry Andric return error("INSERTVAL: Invalid type"); 4896*0b57cec5SDimitry Andric if ((unsigned)Index != Index) 4897*0b57cec5SDimitry Andric return error("Invalid value"); 4898*0b57cec5SDimitry Andric if (IsStruct && Index >= CurTy->getStructNumElements()) 4899*0b57cec5SDimitry Andric return error("INSERTVAL: Invalid struct index"); 4900*0b57cec5SDimitry Andric if (IsArray && Index >= CurTy->getArrayNumElements()) 4901*0b57cec5SDimitry Andric return error("INSERTVAL: Invalid array index"); 4902*0b57cec5SDimitry Andric 4903*0b57cec5SDimitry Andric INSERTVALIdx.push_back((unsigned)Index); 4904*0b57cec5SDimitry Andric if (IsStruct) 4905*0b57cec5SDimitry Andric CurTy = CurTy->getStructElementType(Index); 4906*0b57cec5SDimitry Andric else 4907*0b57cec5SDimitry Andric CurTy = CurTy->getArrayElementType(); 4908*0b57cec5SDimitry Andric } 4909*0b57cec5SDimitry Andric 4910*0b57cec5SDimitry Andric if (CurTy != Val->getType()) 4911*0b57cec5SDimitry Andric return error("Inserted value type doesn't match aggregate type"); 4912*0b57cec5SDimitry Andric 4913*0b57cec5SDimitry Andric I = InsertValueInst::Create(Agg, Val, INSERTVALIdx); 491481ad6265SDimitry Andric ResTypeID = AggTypeID; 4915*0b57cec5SDimitry Andric InstructionList.push_back(I); 4916*0b57cec5SDimitry Andric break; 4917*0b57cec5SDimitry Andric } 4918*0b57cec5SDimitry Andric 4919*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval] 4920*0b57cec5SDimitry Andric // obsolete form of select 4921*0b57cec5SDimitry Andric // handles select i1 ... in old bitcode 4922*0b57cec5SDimitry Andric unsigned OpNum = 0; 4923*0b57cec5SDimitry Andric Value *TrueVal, *FalseVal, *Cond; 492481ad6265SDimitry Andric unsigned TypeID; 492581ad6265SDimitry Andric Type *CondType = Type::getInt1Ty(Context); 492681ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal, TypeID, 492781ad6265SDimitry Andric CurBB) || 492881ad6265SDimitry Andric popValue(Record, OpNum, NextValueNo, TrueVal->getType(), TypeID, 492981ad6265SDimitry Andric FalseVal, CurBB) || 493081ad6265SDimitry Andric popValue(Record, OpNum, NextValueNo, CondType, 493181ad6265SDimitry Andric getVirtualTypeID(CondType), Cond, CurBB)) 4932*0b57cec5SDimitry Andric return error("Invalid record"); 4933*0b57cec5SDimitry Andric 4934*0b57cec5SDimitry Andric I = SelectInst::Create(Cond, TrueVal, FalseVal); 493581ad6265SDimitry Andric ResTypeID = TypeID; 4936*0b57cec5SDimitry Andric InstructionList.push_back(I); 4937*0b57cec5SDimitry Andric break; 4938*0b57cec5SDimitry Andric } 4939*0b57cec5SDimitry Andric 4940*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred] 4941*0b57cec5SDimitry Andric // new form of select 4942*0b57cec5SDimitry Andric // handles select i1 or select [N x i1] 4943*0b57cec5SDimitry Andric unsigned OpNum = 0; 4944*0b57cec5SDimitry Andric Value *TrueVal, *FalseVal, *Cond; 494581ad6265SDimitry Andric unsigned ValTypeID, CondTypeID; 494681ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal, ValTypeID, 494781ad6265SDimitry Andric CurBB) || 494881ad6265SDimitry Andric popValue(Record, OpNum, NextValueNo, TrueVal->getType(), ValTypeID, 494981ad6265SDimitry Andric FalseVal, CurBB) || 495081ad6265SDimitry Andric getValueTypePair(Record, OpNum, NextValueNo, Cond, CondTypeID, CurBB)) 4951*0b57cec5SDimitry Andric return error("Invalid record"); 4952*0b57cec5SDimitry Andric 4953*0b57cec5SDimitry Andric // select condition can be either i1 or [N x i1] 4954*0b57cec5SDimitry Andric if (VectorType* vector_type = 4955*0b57cec5SDimitry Andric dyn_cast<VectorType>(Cond->getType())) { 4956*0b57cec5SDimitry Andric // expect <n x i1> 4957*0b57cec5SDimitry Andric if (vector_type->getElementType() != Type::getInt1Ty(Context)) 4958*0b57cec5SDimitry Andric return error("Invalid type for value"); 4959*0b57cec5SDimitry Andric } else { 4960*0b57cec5SDimitry Andric // expect i1 4961*0b57cec5SDimitry Andric if (Cond->getType() != Type::getInt1Ty(Context)) 4962*0b57cec5SDimitry Andric return error("Invalid type for value"); 4963*0b57cec5SDimitry Andric } 4964*0b57cec5SDimitry Andric 4965*0b57cec5SDimitry Andric I = SelectInst::Create(Cond, TrueVal, FalseVal); 496681ad6265SDimitry Andric ResTypeID = ValTypeID; 4967*0b57cec5SDimitry Andric InstructionList.push_back(I); 4968*0b57cec5SDimitry Andric if (OpNum < Record.size() && isa<FPMathOperator>(I)) { 4969*0b57cec5SDimitry Andric FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]); 4970*0b57cec5SDimitry Andric if (FMF.any()) 4971*0b57cec5SDimitry Andric I->setFastMathFlags(FMF); 4972*0b57cec5SDimitry Andric } 4973*0b57cec5SDimitry Andric break; 4974*0b57cec5SDimitry Andric } 4975*0b57cec5SDimitry Andric 4976*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval] 4977*0b57cec5SDimitry Andric unsigned OpNum = 0; 4978*0b57cec5SDimitry Andric Value *Vec, *Idx; 497981ad6265SDimitry Andric unsigned VecTypeID, IdxTypeID; 498081ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Vec, VecTypeID, CurBB) || 498181ad6265SDimitry Andric getValueTypePair(Record, OpNum, NextValueNo, Idx, IdxTypeID, CurBB)) 4982*0b57cec5SDimitry Andric return error("Invalid record"); 4983*0b57cec5SDimitry Andric if (!Vec->getType()->isVectorTy()) 4984*0b57cec5SDimitry Andric return error("Invalid type for value"); 4985*0b57cec5SDimitry Andric I = ExtractElementInst::Create(Vec, Idx); 498681ad6265SDimitry Andric ResTypeID = getContainedTypeID(VecTypeID); 4987*0b57cec5SDimitry Andric InstructionList.push_back(I); 4988*0b57cec5SDimitry Andric break; 4989*0b57cec5SDimitry Andric } 4990*0b57cec5SDimitry Andric 4991*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval] 4992*0b57cec5SDimitry Andric unsigned OpNum = 0; 4993*0b57cec5SDimitry Andric Value *Vec, *Elt, *Idx; 499481ad6265SDimitry Andric unsigned VecTypeID, IdxTypeID; 499581ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Vec, VecTypeID, CurBB)) 4996*0b57cec5SDimitry Andric return error("Invalid record"); 4997*0b57cec5SDimitry Andric if (!Vec->getType()->isVectorTy()) 4998*0b57cec5SDimitry Andric return error("Invalid type for value"); 4999*0b57cec5SDimitry Andric if (popValue(Record, OpNum, NextValueNo, 500081ad6265SDimitry Andric cast<VectorType>(Vec->getType())->getElementType(), 500181ad6265SDimitry Andric getContainedTypeID(VecTypeID), Elt, CurBB) || 500281ad6265SDimitry Andric getValueTypePair(Record, OpNum, NextValueNo, Idx, IdxTypeID, CurBB)) 5003*0b57cec5SDimitry Andric return error("Invalid record"); 5004*0b57cec5SDimitry Andric I = InsertElementInst::Create(Vec, Elt, Idx); 500581ad6265SDimitry Andric ResTypeID = VecTypeID; 5006*0b57cec5SDimitry Andric InstructionList.push_back(I); 5007*0b57cec5SDimitry Andric break; 5008*0b57cec5SDimitry Andric } 5009*0b57cec5SDimitry Andric 5010*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval] 5011*0b57cec5SDimitry Andric unsigned OpNum = 0; 5012*0b57cec5SDimitry Andric Value *Vec1, *Vec2, *Mask; 501381ad6265SDimitry Andric unsigned Vec1TypeID; 501481ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Vec1, Vec1TypeID, 501581ad6265SDimitry Andric CurBB) || 501681ad6265SDimitry Andric popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec1TypeID, 501781ad6265SDimitry Andric Vec2, CurBB)) 5018*0b57cec5SDimitry Andric return error("Invalid record"); 5019*0b57cec5SDimitry Andric 502081ad6265SDimitry Andric unsigned MaskTypeID; 502181ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Mask, MaskTypeID, CurBB)) 5022*0b57cec5SDimitry Andric return error("Invalid record"); 5023*0b57cec5SDimitry Andric if (!Vec1->getType()->isVectorTy() || !Vec2->getType()->isVectorTy()) 5024*0b57cec5SDimitry Andric return error("Invalid type for value"); 50255ffd83dbSDimitry Andric 5026*0b57cec5SDimitry Andric I = new ShuffleVectorInst(Vec1, Vec2, Mask); 502781ad6265SDimitry Andric ResTypeID = 502881ad6265SDimitry Andric getVirtualTypeID(I->getType(), getContainedTypeID(Vec1TypeID)); 5029*0b57cec5SDimitry Andric InstructionList.push_back(I); 5030*0b57cec5SDimitry Andric break; 5031*0b57cec5SDimitry Andric } 5032*0b57cec5SDimitry Andric 5033*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred] 5034*0b57cec5SDimitry Andric // Old form of ICmp/FCmp returning bool 5035*0b57cec5SDimitry Andric // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were 5036*0b57cec5SDimitry Andric // both legal on vectors but had different behaviour. 5037*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred] 5038*0b57cec5SDimitry Andric // FCmp/ICmp returning bool or vector of bool 5039*0b57cec5SDimitry Andric 5040*0b57cec5SDimitry Andric unsigned OpNum = 0; 5041*0b57cec5SDimitry Andric Value *LHS, *RHS; 504281ad6265SDimitry Andric unsigned LHSTypeID; 504381ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, LHS, LHSTypeID, CurBB) || 504481ad6265SDimitry Andric popValue(Record, OpNum, NextValueNo, LHS->getType(), LHSTypeID, RHS, 504581ad6265SDimitry Andric CurBB)) 5046*0b57cec5SDimitry Andric return error("Invalid record"); 5047*0b57cec5SDimitry Andric 5048*0b57cec5SDimitry Andric if (OpNum >= Record.size()) 5049*0b57cec5SDimitry Andric return error( 5050*0b57cec5SDimitry Andric "Invalid record: operand number exceeded available operands"); 5051*0b57cec5SDimitry Andric 5052*0b57cec5SDimitry Andric unsigned PredVal = Record[OpNum]; 5053*0b57cec5SDimitry Andric bool IsFP = LHS->getType()->isFPOrFPVectorTy(); 5054*0b57cec5SDimitry Andric FastMathFlags FMF; 5055*0b57cec5SDimitry Andric if (IsFP && Record.size() > OpNum+1) 5056*0b57cec5SDimitry Andric FMF = getDecodedFastMathFlags(Record[++OpNum]); 5057*0b57cec5SDimitry Andric 5058*0b57cec5SDimitry Andric if (OpNum+1 != Record.size()) 5059*0b57cec5SDimitry Andric return error("Invalid record"); 5060*0b57cec5SDimitry Andric 5061*0b57cec5SDimitry Andric if (LHS->getType()->isFPOrFPVectorTy()) 5062*0b57cec5SDimitry Andric I = new FCmpInst((FCmpInst::Predicate)PredVal, LHS, RHS); 5063*0b57cec5SDimitry Andric else 5064*0b57cec5SDimitry Andric I = new ICmpInst((ICmpInst::Predicate)PredVal, LHS, RHS); 5065*0b57cec5SDimitry Andric 506681ad6265SDimitry Andric ResTypeID = getVirtualTypeID(I->getType()->getScalarType()); 506781ad6265SDimitry Andric if (LHS->getType()->isVectorTy()) 506881ad6265SDimitry Andric ResTypeID = getVirtualTypeID(I->getType(), ResTypeID); 506981ad6265SDimitry Andric 5070*0b57cec5SDimitry Andric if (FMF.any()) 5071*0b57cec5SDimitry Andric I->setFastMathFlags(FMF); 5072*0b57cec5SDimitry Andric InstructionList.push_back(I); 5073*0b57cec5SDimitry Andric break; 5074*0b57cec5SDimitry Andric } 5075*0b57cec5SDimitry Andric 5076*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>] 5077*0b57cec5SDimitry Andric { 5078*0b57cec5SDimitry Andric unsigned Size = Record.size(); 5079*0b57cec5SDimitry Andric if (Size == 0) { 5080*0b57cec5SDimitry Andric I = ReturnInst::Create(Context); 5081*0b57cec5SDimitry Andric InstructionList.push_back(I); 5082*0b57cec5SDimitry Andric break; 5083*0b57cec5SDimitry Andric } 5084*0b57cec5SDimitry Andric 5085*0b57cec5SDimitry Andric unsigned OpNum = 0; 5086*0b57cec5SDimitry Andric Value *Op = nullptr; 508781ad6265SDimitry Andric unsigned OpTypeID; 508881ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB)) 5089*0b57cec5SDimitry Andric return error("Invalid record"); 5090*0b57cec5SDimitry Andric if (OpNum != Record.size()) 5091*0b57cec5SDimitry Andric return error("Invalid record"); 5092*0b57cec5SDimitry Andric 5093*0b57cec5SDimitry Andric I = ReturnInst::Create(Context, Op); 5094*0b57cec5SDimitry Andric InstructionList.push_back(I); 5095*0b57cec5SDimitry Andric break; 5096*0b57cec5SDimitry Andric } 5097*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#] 5098*0b57cec5SDimitry Andric if (Record.size() != 1 && Record.size() != 3) 5099*0b57cec5SDimitry Andric return error("Invalid record"); 5100*0b57cec5SDimitry Andric BasicBlock *TrueDest = getBasicBlock(Record[0]); 5101*0b57cec5SDimitry Andric if (!TrueDest) 5102*0b57cec5SDimitry Andric return error("Invalid record"); 5103*0b57cec5SDimitry Andric 5104*0b57cec5SDimitry Andric if (Record.size() == 1) { 5105*0b57cec5SDimitry Andric I = BranchInst::Create(TrueDest); 5106*0b57cec5SDimitry Andric InstructionList.push_back(I); 5107*0b57cec5SDimitry Andric } 5108*0b57cec5SDimitry Andric else { 5109*0b57cec5SDimitry Andric BasicBlock *FalseDest = getBasicBlock(Record[1]); 511081ad6265SDimitry Andric Type *CondType = Type::getInt1Ty(Context); 511181ad6265SDimitry Andric Value *Cond = getValue(Record, 2, NextValueNo, CondType, 511281ad6265SDimitry Andric getVirtualTypeID(CondType), CurBB); 5113*0b57cec5SDimitry Andric if (!FalseDest || !Cond) 5114*0b57cec5SDimitry Andric return error("Invalid record"); 5115*0b57cec5SDimitry Andric I = BranchInst::Create(TrueDest, FalseDest, Cond); 5116*0b57cec5SDimitry Andric InstructionList.push_back(I); 5117*0b57cec5SDimitry Andric } 5118*0b57cec5SDimitry Andric break; 5119*0b57cec5SDimitry Andric } 5120*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_CLEANUPRET: { // CLEANUPRET: [val] or [val,bb#] 5121*0b57cec5SDimitry Andric if (Record.size() != 1 && Record.size() != 2) 5122*0b57cec5SDimitry Andric return error("Invalid record"); 5123*0b57cec5SDimitry Andric unsigned Idx = 0; 512481ad6265SDimitry Andric Type *TokenTy = Type::getTokenTy(Context); 512581ad6265SDimitry Andric Value *CleanupPad = getValue(Record, Idx++, NextValueNo, TokenTy, 512681ad6265SDimitry Andric getVirtualTypeID(TokenTy), CurBB); 5127*0b57cec5SDimitry Andric if (!CleanupPad) 5128*0b57cec5SDimitry Andric return error("Invalid record"); 5129*0b57cec5SDimitry Andric BasicBlock *UnwindDest = nullptr; 5130*0b57cec5SDimitry Andric if (Record.size() == 2) { 5131*0b57cec5SDimitry Andric UnwindDest = getBasicBlock(Record[Idx++]); 5132*0b57cec5SDimitry Andric if (!UnwindDest) 5133*0b57cec5SDimitry Andric return error("Invalid record"); 5134*0b57cec5SDimitry Andric } 5135*0b57cec5SDimitry Andric 5136*0b57cec5SDimitry Andric I = CleanupReturnInst::Create(CleanupPad, UnwindDest); 5137*0b57cec5SDimitry Andric InstructionList.push_back(I); 5138*0b57cec5SDimitry Andric break; 5139*0b57cec5SDimitry Andric } 5140*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_CATCHRET: { // CATCHRET: [val,bb#] 5141*0b57cec5SDimitry Andric if (Record.size() != 2) 5142*0b57cec5SDimitry Andric return error("Invalid record"); 5143*0b57cec5SDimitry Andric unsigned Idx = 0; 514481ad6265SDimitry Andric Type *TokenTy = Type::getTokenTy(Context); 514581ad6265SDimitry Andric Value *CatchPad = getValue(Record, Idx++, NextValueNo, TokenTy, 514681ad6265SDimitry Andric getVirtualTypeID(TokenTy), CurBB); 5147*0b57cec5SDimitry Andric if (!CatchPad) 5148*0b57cec5SDimitry Andric return error("Invalid record"); 5149*0b57cec5SDimitry Andric BasicBlock *BB = getBasicBlock(Record[Idx++]); 5150*0b57cec5SDimitry Andric if (!BB) 5151*0b57cec5SDimitry Andric return error("Invalid record"); 5152*0b57cec5SDimitry Andric 5153*0b57cec5SDimitry Andric I = CatchReturnInst::Create(CatchPad, BB); 5154*0b57cec5SDimitry Andric InstructionList.push_back(I); 5155*0b57cec5SDimitry Andric break; 5156*0b57cec5SDimitry Andric } 5157*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_CATCHSWITCH: { // CATCHSWITCH: [tok,num,(bb)*,bb?] 5158*0b57cec5SDimitry Andric // We must have, at minimum, the outer scope and the number of arguments. 5159*0b57cec5SDimitry Andric if (Record.size() < 2) 5160*0b57cec5SDimitry Andric return error("Invalid record"); 5161*0b57cec5SDimitry Andric 5162*0b57cec5SDimitry Andric unsigned Idx = 0; 5163*0b57cec5SDimitry Andric 516481ad6265SDimitry Andric Type *TokenTy = Type::getTokenTy(Context); 516581ad6265SDimitry Andric Value *ParentPad = getValue(Record, Idx++, NextValueNo, TokenTy, 516681ad6265SDimitry Andric getVirtualTypeID(TokenTy), CurBB); 5167*0b57cec5SDimitry Andric 5168*0b57cec5SDimitry Andric unsigned NumHandlers = Record[Idx++]; 5169*0b57cec5SDimitry Andric 5170*0b57cec5SDimitry Andric SmallVector<BasicBlock *, 2> Handlers; 5171*0b57cec5SDimitry Andric for (unsigned Op = 0; Op != NumHandlers; ++Op) { 5172*0b57cec5SDimitry Andric BasicBlock *BB = getBasicBlock(Record[Idx++]); 5173*0b57cec5SDimitry Andric if (!BB) 5174*0b57cec5SDimitry Andric return error("Invalid record"); 5175*0b57cec5SDimitry Andric Handlers.push_back(BB); 5176*0b57cec5SDimitry Andric } 5177*0b57cec5SDimitry Andric 5178*0b57cec5SDimitry Andric BasicBlock *UnwindDest = nullptr; 5179*0b57cec5SDimitry Andric if (Idx + 1 == Record.size()) { 5180*0b57cec5SDimitry Andric UnwindDest = getBasicBlock(Record[Idx++]); 5181*0b57cec5SDimitry Andric if (!UnwindDest) 5182*0b57cec5SDimitry Andric return error("Invalid record"); 5183*0b57cec5SDimitry Andric } 5184*0b57cec5SDimitry Andric 5185*0b57cec5SDimitry Andric if (Record.size() != Idx) 5186*0b57cec5SDimitry Andric return error("Invalid record"); 5187*0b57cec5SDimitry Andric 5188*0b57cec5SDimitry Andric auto *CatchSwitch = 5189*0b57cec5SDimitry Andric CatchSwitchInst::Create(ParentPad, UnwindDest, NumHandlers); 5190*0b57cec5SDimitry Andric for (BasicBlock *Handler : Handlers) 5191*0b57cec5SDimitry Andric CatchSwitch->addHandler(Handler); 5192*0b57cec5SDimitry Andric I = CatchSwitch; 519381ad6265SDimitry Andric ResTypeID = getVirtualTypeID(I->getType()); 5194*0b57cec5SDimitry Andric InstructionList.push_back(I); 5195*0b57cec5SDimitry Andric break; 5196*0b57cec5SDimitry Andric } 5197*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_CATCHPAD: 5198*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_CLEANUPPAD: { // [tok,num,(ty,val)*] 5199*0b57cec5SDimitry Andric // We must have, at minimum, the outer scope and the number of arguments. 5200*0b57cec5SDimitry Andric if (Record.size() < 2) 5201*0b57cec5SDimitry Andric return error("Invalid record"); 5202*0b57cec5SDimitry Andric 5203*0b57cec5SDimitry Andric unsigned Idx = 0; 5204*0b57cec5SDimitry Andric 520581ad6265SDimitry Andric Type *TokenTy = Type::getTokenTy(Context); 520681ad6265SDimitry Andric Value *ParentPad = getValue(Record, Idx++, NextValueNo, TokenTy, 520781ad6265SDimitry Andric getVirtualTypeID(TokenTy), CurBB); 5208*0b57cec5SDimitry Andric 5209*0b57cec5SDimitry Andric unsigned NumArgOperands = Record[Idx++]; 5210*0b57cec5SDimitry Andric 5211*0b57cec5SDimitry Andric SmallVector<Value *, 2> Args; 5212*0b57cec5SDimitry Andric for (unsigned Op = 0; Op != NumArgOperands; ++Op) { 5213*0b57cec5SDimitry Andric Value *Val; 521481ad6265SDimitry Andric unsigned ValTypeID; 521581ad6265SDimitry Andric if (getValueTypePair(Record, Idx, NextValueNo, Val, ValTypeID, nullptr)) 5216*0b57cec5SDimitry Andric return error("Invalid record"); 5217*0b57cec5SDimitry Andric Args.push_back(Val); 5218*0b57cec5SDimitry Andric } 5219*0b57cec5SDimitry Andric 5220*0b57cec5SDimitry Andric if (Record.size() != Idx) 5221*0b57cec5SDimitry Andric return error("Invalid record"); 5222*0b57cec5SDimitry Andric 5223*0b57cec5SDimitry Andric if (BitCode == bitc::FUNC_CODE_INST_CLEANUPPAD) 5224*0b57cec5SDimitry Andric I = CleanupPadInst::Create(ParentPad, Args); 5225*0b57cec5SDimitry Andric else 5226*0b57cec5SDimitry Andric I = CatchPadInst::Create(ParentPad, Args); 522781ad6265SDimitry Andric ResTypeID = getVirtualTypeID(I->getType()); 5228*0b57cec5SDimitry Andric InstructionList.push_back(I); 5229*0b57cec5SDimitry Andric break; 5230*0b57cec5SDimitry Andric } 5231*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...] 5232*0b57cec5SDimitry Andric // Check magic 5233*0b57cec5SDimitry Andric if ((Record[0] >> 16) == SWITCH_INST_MAGIC) { 5234*0b57cec5SDimitry Andric // "New" SwitchInst format with case ranges. The changes to write this 5235*0b57cec5SDimitry Andric // format were reverted but we still recognize bitcode that uses it. 5236*0b57cec5SDimitry Andric // Hopefully someday we will have support for case ranges and can use 5237*0b57cec5SDimitry Andric // this format again. 5238*0b57cec5SDimitry Andric 523981ad6265SDimitry Andric unsigned OpTyID = Record[1]; 524081ad6265SDimitry Andric Type *OpTy = getTypeByID(OpTyID); 5241*0b57cec5SDimitry Andric unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth(); 5242*0b57cec5SDimitry Andric 524381ad6265SDimitry Andric Value *Cond = getValue(Record, 2, NextValueNo, OpTy, OpTyID, CurBB); 5244*0b57cec5SDimitry Andric BasicBlock *Default = getBasicBlock(Record[3]); 5245*0b57cec5SDimitry Andric if (!OpTy || !Cond || !Default) 5246*0b57cec5SDimitry Andric return error("Invalid record"); 5247*0b57cec5SDimitry Andric 5248*0b57cec5SDimitry Andric unsigned NumCases = Record[4]; 5249*0b57cec5SDimitry Andric 5250*0b57cec5SDimitry Andric SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); 5251*0b57cec5SDimitry Andric InstructionList.push_back(SI); 5252*0b57cec5SDimitry Andric 5253*0b57cec5SDimitry Andric unsigned CurIdx = 5; 5254*0b57cec5SDimitry Andric for (unsigned i = 0; i != NumCases; ++i) { 5255*0b57cec5SDimitry Andric SmallVector<ConstantInt*, 1> CaseVals; 5256*0b57cec5SDimitry Andric unsigned NumItems = Record[CurIdx++]; 5257*0b57cec5SDimitry Andric for (unsigned ci = 0; ci != NumItems; ++ci) { 5258*0b57cec5SDimitry Andric bool isSingleNumber = Record[CurIdx++]; 5259*0b57cec5SDimitry Andric 5260*0b57cec5SDimitry Andric APInt Low; 5261*0b57cec5SDimitry Andric unsigned ActiveWords = 1; 5262*0b57cec5SDimitry Andric if (ValueBitWidth > 64) 5263*0b57cec5SDimitry Andric ActiveWords = Record[CurIdx++]; 5264*0b57cec5SDimitry Andric Low = readWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords), 5265*0b57cec5SDimitry Andric ValueBitWidth); 5266*0b57cec5SDimitry Andric CurIdx += ActiveWords; 5267*0b57cec5SDimitry Andric 5268*0b57cec5SDimitry Andric if (!isSingleNumber) { 5269*0b57cec5SDimitry Andric ActiveWords = 1; 5270*0b57cec5SDimitry Andric if (ValueBitWidth > 64) 5271*0b57cec5SDimitry Andric ActiveWords = Record[CurIdx++]; 5272*0b57cec5SDimitry Andric APInt High = readWideAPInt( 5273*0b57cec5SDimitry Andric makeArrayRef(&Record[CurIdx], ActiveWords), ValueBitWidth); 5274*0b57cec5SDimitry Andric CurIdx += ActiveWords; 5275*0b57cec5SDimitry Andric 5276*0b57cec5SDimitry Andric // FIXME: It is not clear whether values in the range should be 5277*0b57cec5SDimitry Andric // compared as signed or unsigned values. The partially 5278*0b57cec5SDimitry Andric // implemented changes that used this format in the past used 5279*0b57cec5SDimitry Andric // unsigned comparisons. 5280*0b57cec5SDimitry Andric for ( ; Low.ule(High); ++Low) 5281*0b57cec5SDimitry Andric CaseVals.push_back(ConstantInt::get(Context, Low)); 5282*0b57cec5SDimitry Andric } else 5283*0b57cec5SDimitry Andric CaseVals.push_back(ConstantInt::get(Context, Low)); 5284*0b57cec5SDimitry Andric } 5285*0b57cec5SDimitry Andric BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]); 52864824e7fdSDimitry Andric for (ConstantInt *Cst : CaseVals) 52874824e7fdSDimitry Andric SI->addCase(Cst, DestBB); 5288*0b57cec5SDimitry Andric } 5289*0b57cec5SDimitry Andric I = SI; 5290*0b57cec5SDimitry Andric break; 5291*0b57cec5SDimitry Andric } 5292*0b57cec5SDimitry Andric 5293*0b57cec5SDimitry Andric // Old SwitchInst format without case ranges. 5294*0b57cec5SDimitry Andric 5295*0b57cec5SDimitry Andric if (Record.size() < 3 || (Record.size() & 1) == 0) 5296*0b57cec5SDimitry Andric return error("Invalid record"); 529781ad6265SDimitry Andric unsigned OpTyID = Record[0]; 529881ad6265SDimitry Andric Type *OpTy = getTypeByID(OpTyID); 529981ad6265SDimitry Andric Value *Cond = getValue(Record, 1, NextValueNo, OpTy, OpTyID, CurBB); 5300*0b57cec5SDimitry Andric BasicBlock *Default = getBasicBlock(Record[2]); 5301*0b57cec5SDimitry Andric if (!OpTy || !Cond || !Default) 5302*0b57cec5SDimitry Andric return error("Invalid record"); 5303*0b57cec5SDimitry Andric unsigned NumCases = (Record.size()-3)/2; 5304*0b57cec5SDimitry Andric SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); 5305*0b57cec5SDimitry Andric InstructionList.push_back(SI); 5306*0b57cec5SDimitry Andric for (unsigned i = 0, e = NumCases; i != e; ++i) { 530781ad6265SDimitry Andric ConstantInt *CaseVal = dyn_cast_or_null<ConstantInt>( 530881ad6265SDimitry Andric getFnValueByID(Record[3+i*2], OpTy, OpTyID, nullptr)); 5309*0b57cec5SDimitry Andric BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]); 5310*0b57cec5SDimitry Andric if (!CaseVal || !DestBB) { 5311*0b57cec5SDimitry Andric delete SI; 5312*0b57cec5SDimitry Andric return error("Invalid record"); 5313*0b57cec5SDimitry Andric } 5314*0b57cec5SDimitry Andric SI->addCase(CaseVal, DestBB); 5315*0b57cec5SDimitry Andric } 5316*0b57cec5SDimitry Andric I = SI; 5317*0b57cec5SDimitry Andric break; 5318*0b57cec5SDimitry Andric } 5319*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...] 5320*0b57cec5SDimitry Andric if (Record.size() < 2) 5321*0b57cec5SDimitry Andric return error("Invalid record"); 532281ad6265SDimitry Andric unsigned OpTyID = Record[0]; 532381ad6265SDimitry Andric Type *OpTy = getTypeByID(OpTyID); 532481ad6265SDimitry Andric Value *Address = getValue(Record, 1, NextValueNo, OpTy, OpTyID, CurBB); 5325*0b57cec5SDimitry Andric if (!OpTy || !Address) 5326*0b57cec5SDimitry Andric return error("Invalid record"); 5327*0b57cec5SDimitry Andric unsigned NumDests = Record.size()-2; 5328*0b57cec5SDimitry Andric IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests); 5329*0b57cec5SDimitry Andric InstructionList.push_back(IBI); 5330*0b57cec5SDimitry Andric for (unsigned i = 0, e = NumDests; i != e; ++i) { 5331*0b57cec5SDimitry Andric if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) { 5332*0b57cec5SDimitry Andric IBI->addDestination(DestBB); 5333*0b57cec5SDimitry Andric } else { 5334*0b57cec5SDimitry Andric delete IBI; 5335*0b57cec5SDimitry Andric return error("Invalid record"); 5336*0b57cec5SDimitry Andric } 5337*0b57cec5SDimitry Andric } 5338*0b57cec5SDimitry Andric I = IBI; 5339*0b57cec5SDimitry Andric break; 5340*0b57cec5SDimitry Andric } 5341*0b57cec5SDimitry Andric 5342*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_INVOKE: { 5343*0b57cec5SDimitry Andric // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...] 5344*0b57cec5SDimitry Andric if (Record.size() < 4) 5345*0b57cec5SDimitry Andric return error("Invalid record"); 5346*0b57cec5SDimitry Andric unsigned OpNum = 0; 5347*0b57cec5SDimitry Andric AttributeList PAL = getAttributes(Record[OpNum++]); 5348*0b57cec5SDimitry Andric unsigned CCInfo = Record[OpNum++]; 5349*0b57cec5SDimitry Andric BasicBlock *NormalBB = getBasicBlock(Record[OpNum++]); 5350*0b57cec5SDimitry Andric BasicBlock *UnwindBB = getBasicBlock(Record[OpNum++]); 5351*0b57cec5SDimitry Andric 535281ad6265SDimitry Andric unsigned FTyID = InvalidTypeID; 5353*0b57cec5SDimitry Andric FunctionType *FTy = nullptr; 5354*0b57cec5SDimitry Andric if ((CCInfo >> 13) & 1) { 535581ad6265SDimitry Andric FTyID = Record[OpNum++]; 535681ad6265SDimitry Andric FTy = dyn_cast<FunctionType>(getTypeByID(FTyID)); 5357fe6060f1SDimitry Andric if (!FTy) 5358*0b57cec5SDimitry Andric return error("Explicit invoke type is not a function type"); 5359*0b57cec5SDimitry Andric } 5360*0b57cec5SDimitry Andric 5361*0b57cec5SDimitry Andric Value *Callee; 536281ad6265SDimitry Andric unsigned CalleeTypeID; 536381ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Callee, CalleeTypeID, 536481ad6265SDimitry Andric CurBB)) 5365*0b57cec5SDimitry Andric return error("Invalid record"); 5366*0b57cec5SDimitry Andric 5367*0b57cec5SDimitry Andric PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType()); 5368*0b57cec5SDimitry Andric if (!CalleeTy) 5369*0b57cec5SDimitry Andric return error("Callee is not a pointer"); 5370*0b57cec5SDimitry Andric if (!FTy) { 537181ad6265SDimitry Andric FTyID = getContainedTypeID(CalleeTypeID); 537281ad6265SDimitry Andric FTy = dyn_cast_or_null<FunctionType>(getTypeByID(FTyID)); 5373fe6060f1SDimitry Andric if (!FTy) 5374*0b57cec5SDimitry Andric return error("Callee is not of pointer to function type"); 5375fe6060f1SDimitry Andric } else if (!CalleeTy->isOpaqueOrPointeeTypeMatches(FTy)) 5376*0b57cec5SDimitry Andric return error("Explicit invoke type does not match pointee type of " 5377*0b57cec5SDimitry Andric "callee operand"); 5378*0b57cec5SDimitry Andric if (Record.size() < FTy->getNumParams() + OpNum) 5379*0b57cec5SDimitry Andric return error("Insufficient operands to call"); 5380*0b57cec5SDimitry Andric 5381*0b57cec5SDimitry Andric SmallVector<Value*, 16> Ops; 538281ad6265SDimitry Andric SmallVector<unsigned, 16> ArgTyIDs; 5383*0b57cec5SDimitry Andric for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { 538481ad6265SDimitry Andric unsigned ArgTyID = getContainedTypeID(FTyID, i + 1); 538581ad6265SDimitry Andric Ops.push_back(getValue(Record, OpNum, NextValueNo, FTy->getParamType(i), 538681ad6265SDimitry Andric ArgTyID, CurBB)); 538781ad6265SDimitry Andric ArgTyIDs.push_back(ArgTyID); 5388*0b57cec5SDimitry Andric if (!Ops.back()) 5389*0b57cec5SDimitry Andric return error("Invalid record"); 5390*0b57cec5SDimitry Andric } 5391*0b57cec5SDimitry Andric 5392*0b57cec5SDimitry Andric if (!FTy->isVarArg()) { 5393*0b57cec5SDimitry Andric if (Record.size() != OpNum) 5394*0b57cec5SDimitry Andric return error("Invalid record"); 5395*0b57cec5SDimitry Andric } else { 5396*0b57cec5SDimitry Andric // Read type/value pairs for varargs params. 5397*0b57cec5SDimitry Andric while (OpNum != Record.size()) { 5398*0b57cec5SDimitry Andric Value *Op; 539981ad6265SDimitry Andric unsigned OpTypeID; 540081ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB)) 5401*0b57cec5SDimitry Andric return error("Invalid record"); 5402*0b57cec5SDimitry Andric Ops.push_back(Op); 540381ad6265SDimitry Andric ArgTyIDs.push_back(OpTypeID); 5404*0b57cec5SDimitry Andric } 5405*0b57cec5SDimitry Andric } 5406*0b57cec5SDimitry Andric 540781ad6265SDimitry Andric // Upgrade the bundles if needed. 540881ad6265SDimitry Andric if (!OperandBundles.empty()) 540981ad6265SDimitry Andric UpgradeOperandBundles(OperandBundles); 541081ad6265SDimitry Andric 5411*0b57cec5SDimitry Andric I = InvokeInst::Create(FTy, Callee, NormalBB, UnwindBB, Ops, 5412*0b57cec5SDimitry Andric OperandBundles); 541381ad6265SDimitry Andric ResTypeID = getContainedTypeID(FTyID); 5414*0b57cec5SDimitry Andric OperandBundles.clear(); 5415*0b57cec5SDimitry Andric InstructionList.push_back(I); 5416*0b57cec5SDimitry Andric cast<InvokeInst>(I)->setCallingConv( 5417*0b57cec5SDimitry Andric static_cast<CallingConv::ID>(CallingConv::MaxID & CCInfo)); 5418*0b57cec5SDimitry Andric cast<InvokeInst>(I)->setAttributes(PAL); 541981ad6265SDimitry Andric if (Error Err = propagateAttributeTypes(cast<CallBase>(I), ArgTyIDs)) { 542081ad6265SDimitry Andric I->deleteValue(); 542181ad6265SDimitry Andric return Err; 542281ad6265SDimitry Andric } 5423*0b57cec5SDimitry Andric 5424*0b57cec5SDimitry Andric break; 5425*0b57cec5SDimitry Andric } 5426*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval] 5427*0b57cec5SDimitry Andric unsigned Idx = 0; 5428*0b57cec5SDimitry Andric Value *Val = nullptr; 542981ad6265SDimitry Andric unsigned ValTypeID; 543081ad6265SDimitry Andric if (getValueTypePair(Record, Idx, NextValueNo, Val, ValTypeID, CurBB)) 5431*0b57cec5SDimitry Andric return error("Invalid record"); 5432*0b57cec5SDimitry Andric I = ResumeInst::Create(Val); 5433*0b57cec5SDimitry Andric InstructionList.push_back(I); 5434*0b57cec5SDimitry Andric break; 5435*0b57cec5SDimitry Andric } 5436*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_CALLBR: { 5437*0b57cec5SDimitry Andric // CALLBR: [attr, cc, norm, transfs, fty, fnid, args] 5438*0b57cec5SDimitry Andric unsigned OpNum = 0; 5439*0b57cec5SDimitry Andric AttributeList PAL = getAttributes(Record[OpNum++]); 5440*0b57cec5SDimitry Andric unsigned CCInfo = Record[OpNum++]; 5441*0b57cec5SDimitry Andric 5442*0b57cec5SDimitry Andric BasicBlock *DefaultDest = getBasicBlock(Record[OpNum++]); 5443*0b57cec5SDimitry Andric unsigned NumIndirectDests = Record[OpNum++]; 5444*0b57cec5SDimitry Andric SmallVector<BasicBlock *, 16> IndirectDests; 5445*0b57cec5SDimitry Andric for (unsigned i = 0, e = NumIndirectDests; i != e; ++i) 5446*0b57cec5SDimitry Andric IndirectDests.push_back(getBasicBlock(Record[OpNum++])); 5447*0b57cec5SDimitry Andric 544881ad6265SDimitry Andric unsigned FTyID = InvalidTypeID; 5449*0b57cec5SDimitry Andric FunctionType *FTy = nullptr; 5450*0b57cec5SDimitry Andric if ((CCInfo >> bitc::CALL_EXPLICIT_TYPE) & 1) { 545181ad6265SDimitry Andric FTyID = Record[OpNum++]; 545281ad6265SDimitry Andric FTy = dyn_cast_or_null<FunctionType>(getTypeByID(FTyID)); 5453fe6060f1SDimitry Andric if (!FTy) 5454*0b57cec5SDimitry Andric return error("Explicit call type is not a function type"); 5455*0b57cec5SDimitry Andric } 5456*0b57cec5SDimitry Andric 5457*0b57cec5SDimitry Andric Value *Callee; 545881ad6265SDimitry Andric unsigned CalleeTypeID; 545981ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Callee, CalleeTypeID, 546081ad6265SDimitry Andric CurBB)) 5461*0b57cec5SDimitry Andric return error("Invalid record"); 5462*0b57cec5SDimitry Andric 5463*0b57cec5SDimitry Andric PointerType *OpTy = dyn_cast<PointerType>(Callee->getType()); 5464*0b57cec5SDimitry Andric if (!OpTy) 5465*0b57cec5SDimitry Andric return error("Callee is not a pointer type"); 5466*0b57cec5SDimitry Andric if (!FTy) { 546781ad6265SDimitry Andric FTyID = getContainedTypeID(CalleeTypeID); 546881ad6265SDimitry Andric FTy = dyn_cast_or_null<FunctionType>(getTypeByID(FTyID)); 5469fe6060f1SDimitry Andric if (!FTy) 5470*0b57cec5SDimitry Andric return error("Callee is not of pointer to function type"); 547104eeddc0SDimitry Andric } else if (!OpTy->isOpaqueOrPointeeTypeMatches(FTy)) 5472*0b57cec5SDimitry Andric return error("Explicit call type does not match pointee type of " 5473*0b57cec5SDimitry Andric "callee operand"); 5474*0b57cec5SDimitry Andric if (Record.size() < FTy->getNumParams() + OpNum) 5475*0b57cec5SDimitry Andric return error("Insufficient operands to call"); 5476*0b57cec5SDimitry Andric 5477*0b57cec5SDimitry Andric SmallVector<Value*, 16> Args; 547881ad6265SDimitry Andric SmallVector<unsigned, 16> ArgTyIDs; 5479*0b57cec5SDimitry Andric // Read the fixed params. 5480*0b57cec5SDimitry Andric for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { 548104eeddc0SDimitry Andric Value *Arg; 548281ad6265SDimitry Andric unsigned ArgTyID = getContainedTypeID(FTyID, i + 1); 5483*0b57cec5SDimitry Andric if (FTy->getParamType(i)->isLabelTy()) 548404eeddc0SDimitry Andric Arg = getBasicBlock(Record[OpNum]); 5485*0b57cec5SDimitry Andric else 548681ad6265SDimitry Andric Arg = getValue(Record, OpNum, NextValueNo, FTy->getParamType(i), 548781ad6265SDimitry Andric ArgTyID, CurBB); 548804eeddc0SDimitry Andric if (!Arg) 5489*0b57cec5SDimitry Andric return error("Invalid record"); 549004eeddc0SDimitry Andric Args.push_back(Arg); 549181ad6265SDimitry Andric ArgTyIDs.push_back(ArgTyID); 5492*0b57cec5SDimitry Andric } 5493*0b57cec5SDimitry Andric 5494*0b57cec5SDimitry Andric // Read type/value pairs for varargs params. 5495*0b57cec5SDimitry Andric if (!FTy->isVarArg()) { 5496*0b57cec5SDimitry Andric if (OpNum != Record.size()) 5497*0b57cec5SDimitry Andric return error("Invalid record"); 5498*0b57cec5SDimitry Andric } else { 5499*0b57cec5SDimitry Andric while (OpNum != Record.size()) { 5500*0b57cec5SDimitry Andric Value *Op; 550181ad6265SDimitry Andric unsigned OpTypeID; 550281ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB)) 5503*0b57cec5SDimitry Andric return error("Invalid record"); 5504*0b57cec5SDimitry Andric Args.push_back(Op); 550581ad6265SDimitry Andric ArgTyIDs.push_back(OpTypeID); 5506*0b57cec5SDimitry Andric } 5507*0b57cec5SDimitry Andric } 5508*0b57cec5SDimitry Andric 550981ad6265SDimitry Andric // Upgrade the bundles if needed. 551081ad6265SDimitry Andric if (!OperandBundles.empty()) 551181ad6265SDimitry Andric UpgradeOperandBundles(OperandBundles); 551281ad6265SDimitry Andric 5513fcaf7f86SDimitry Andric if (auto *IA = dyn_cast<InlineAsm>(Callee)) { 5514fcaf7f86SDimitry Andric InlineAsm::ConstraintInfoVector ConstraintInfo = IA->ParseConstraints(); 5515fcaf7f86SDimitry Andric auto IsLabelConstraint = [](const InlineAsm::ConstraintInfo &CI) { 5516fcaf7f86SDimitry Andric return CI.Type == InlineAsm::isLabel; 5517fcaf7f86SDimitry Andric }; 5518fcaf7f86SDimitry Andric if (none_of(ConstraintInfo, IsLabelConstraint)) { 5519fcaf7f86SDimitry Andric // Upgrade explicit blockaddress arguments to label constraints. 5520fcaf7f86SDimitry Andric // Verify that the last arguments are blockaddress arguments that 5521fcaf7f86SDimitry Andric // match the indirect destinations. Clang always generates callbr 5522fcaf7f86SDimitry Andric // in this form. We could support reordering with more effort. 5523fcaf7f86SDimitry Andric unsigned FirstBlockArg = Args.size() - IndirectDests.size(); 5524fcaf7f86SDimitry Andric for (unsigned ArgNo = FirstBlockArg; ArgNo < Args.size(); ++ArgNo) { 5525fcaf7f86SDimitry Andric unsigned LabelNo = ArgNo - FirstBlockArg; 5526fcaf7f86SDimitry Andric auto *BA = dyn_cast<BlockAddress>(Args[ArgNo]); 5527fcaf7f86SDimitry Andric if (!BA || BA->getFunction() != F || 5528fcaf7f86SDimitry Andric LabelNo > IndirectDests.size() || 5529fcaf7f86SDimitry Andric BA->getBasicBlock() != IndirectDests[LabelNo]) 5530fcaf7f86SDimitry Andric return error("callbr argument does not match indirect dest"); 5531fcaf7f86SDimitry Andric } 5532fcaf7f86SDimitry Andric 5533fcaf7f86SDimitry Andric // Remove blockaddress arguments. 5534fcaf7f86SDimitry Andric Args.erase(Args.begin() + FirstBlockArg, Args.end()); 5535fcaf7f86SDimitry Andric ArgTyIDs.erase(ArgTyIDs.begin() + FirstBlockArg, ArgTyIDs.end()); 5536fcaf7f86SDimitry Andric 5537fcaf7f86SDimitry Andric // Recreate the function type with less arguments. 5538fcaf7f86SDimitry Andric SmallVector<Type *> ArgTys; 5539fcaf7f86SDimitry Andric for (Value *Arg : Args) 5540fcaf7f86SDimitry Andric ArgTys.push_back(Arg->getType()); 5541fcaf7f86SDimitry Andric FTy = 5542fcaf7f86SDimitry Andric FunctionType::get(FTy->getReturnType(), ArgTys, FTy->isVarArg()); 5543fcaf7f86SDimitry Andric 5544fcaf7f86SDimitry Andric // Update constraint string to use label constraints. 5545fcaf7f86SDimitry Andric std::string Constraints = IA->getConstraintString(); 5546fcaf7f86SDimitry Andric unsigned ArgNo = 0; 5547fcaf7f86SDimitry Andric size_t Pos = 0; 5548fcaf7f86SDimitry Andric for (const auto &CI : ConstraintInfo) { 5549fcaf7f86SDimitry Andric if (CI.hasArg()) { 5550fcaf7f86SDimitry Andric if (ArgNo >= FirstBlockArg) 5551fcaf7f86SDimitry Andric Constraints.insert(Pos, "!"); 5552fcaf7f86SDimitry Andric ++ArgNo; 5553fcaf7f86SDimitry Andric } 5554fcaf7f86SDimitry Andric 5555fcaf7f86SDimitry Andric // Go to next constraint in string. 5556fcaf7f86SDimitry Andric Pos = Constraints.find(',', Pos); 5557fcaf7f86SDimitry Andric if (Pos == std::string::npos) 5558fcaf7f86SDimitry Andric break; 5559fcaf7f86SDimitry Andric ++Pos; 5560fcaf7f86SDimitry Andric } 5561fcaf7f86SDimitry Andric 5562fcaf7f86SDimitry Andric Callee = InlineAsm::get(FTy, IA->getAsmString(), Constraints, 5563fcaf7f86SDimitry Andric IA->hasSideEffects(), IA->isAlignStack(), 5564fcaf7f86SDimitry Andric IA->getDialect(), IA->canThrow()); 5565fcaf7f86SDimitry Andric } 5566fcaf7f86SDimitry Andric } 5567fcaf7f86SDimitry Andric 5568*0b57cec5SDimitry Andric I = CallBrInst::Create(FTy, Callee, DefaultDest, IndirectDests, Args, 5569*0b57cec5SDimitry Andric OperandBundles); 557081ad6265SDimitry Andric ResTypeID = getContainedTypeID(FTyID); 5571*0b57cec5SDimitry Andric OperandBundles.clear(); 5572*0b57cec5SDimitry Andric InstructionList.push_back(I); 5573*0b57cec5SDimitry Andric cast<CallBrInst>(I)->setCallingConv( 5574*0b57cec5SDimitry Andric static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV)); 5575*0b57cec5SDimitry Andric cast<CallBrInst>(I)->setAttributes(PAL); 557681ad6265SDimitry Andric if (Error Err = propagateAttributeTypes(cast<CallBase>(I), ArgTyIDs)) { 557781ad6265SDimitry Andric I->deleteValue(); 557881ad6265SDimitry Andric return Err; 557981ad6265SDimitry Andric } 5580*0b57cec5SDimitry Andric break; 5581*0b57cec5SDimitry Andric } 5582*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE 5583*0b57cec5SDimitry Andric I = new UnreachableInst(Context); 5584*0b57cec5SDimitry Andric InstructionList.push_back(I); 5585*0b57cec5SDimitry Andric break; 5586*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...] 5587e8d8bef9SDimitry Andric if (Record.empty()) 558881ad6265SDimitry Andric return error("Invalid phi record"); 55898bcb0991SDimitry Andric // The first record specifies the type. 559081ad6265SDimitry Andric unsigned TyID = Record[0]; 559181ad6265SDimitry Andric Type *Ty = getTypeByID(TyID); 5592*0b57cec5SDimitry Andric if (!Ty) 559381ad6265SDimitry Andric return error("Invalid phi record"); 5594*0b57cec5SDimitry Andric 55958bcb0991SDimitry Andric // Phi arguments are pairs of records of [value, basic block]. 55968bcb0991SDimitry Andric // There is an optional final record for fast-math-flags if this phi has a 55978bcb0991SDimitry Andric // floating-point type. 55988bcb0991SDimitry Andric size_t NumArgs = (Record.size() - 1) / 2; 55998bcb0991SDimitry Andric PHINode *PN = PHINode::Create(Ty, NumArgs); 560081ad6265SDimitry Andric if ((Record.size() - 1) % 2 == 1 && !isa<FPMathOperator>(PN)) { 560181ad6265SDimitry Andric PN->deleteValue(); 560281ad6265SDimitry Andric return error("Invalid phi record"); 560381ad6265SDimitry Andric } 5604*0b57cec5SDimitry Andric InstructionList.push_back(PN); 5605*0b57cec5SDimitry Andric 560681ad6265SDimitry Andric SmallDenseMap<BasicBlock *, Value *> Args; 56078bcb0991SDimitry Andric for (unsigned i = 0; i != NumArgs; i++) { 560881ad6265SDimitry Andric BasicBlock *BB = getBasicBlock(Record[i * 2 + 2]); 560981ad6265SDimitry Andric if (!BB) { 561081ad6265SDimitry Andric PN->deleteValue(); 561181ad6265SDimitry Andric return error("Invalid phi BB"); 561281ad6265SDimitry Andric } 561381ad6265SDimitry Andric 561481ad6265SDimitry Andric // Phi nodes may contain the same predecessor multiple times, in which 561581ad6265SDimitry Andric // case the incoming value must be identical. Directly reuse the already 561681ad6265SDimitry Andric // seen value here, to avoid expanding a constant expression multiple 561781ad6265SDimitry Andric // times. 561881ad6265SDimitry Andric auto It = Args.find(BB); 561981ad6265SDimitry Andric if (It != Args.end()) { 562081ad6265SDimitry Andric PN->addIncoming(It->second, BB); 562181ad6265SDimitry Andric continue; 562281ad6265SDimitry Andric } 562381ad6265SDimitry Andric 562481ad6265SDimitry Andric // If there already is a block for this edge (from a different phi), 562581ad6265SDimitry Andric // use it. 562681ad6265SDimitry Andric BasicBlock *EdgeBB = ConstExprEdgeBBs.lookup({BB, CurBB}); 562781ad6265SDimitry Andric if (!EdgeBB) { 562881ad6265SDimitry Andric // Otherwise, use a temporary block (that we will discard if it 562981ad6265SDimitry Andric // turns out to be unnecessary). 563081ad6265SDimitry Andric if (!PhiConstExprBB) 563181ad6265SDimitry Andric PhiConstExprBB = BasicBlock::Create(Context, "phi.constexpr", F); 563281ad6265SDimitry Andric EdgeBB = PhiConstExprBB; 563381ad6265SDimitry Andric } 563481ad6265SDimitry Andric 5635*0b57cec5SDimitry Andric // With the new function encoding, it is possible that operands have 5636*0b57cec5SDimitry Andric // negative IDs (for forward references). Use a signed VBR 5637*0b57cec5SDimitry Andric // representation to keep the encoding small. 563881ad6265SDimitry Andric Value *V; 5639*0b57cec5SDimitry Andric if (UseRelativeIDs) 564081ad6265SDimitry Andric V = getValueSigned(Record, i * 2 + 1, NextValueNo, Ty, TyID, EdgeBB); 5641*0b57cec5SDimitry Andric else 564281ad6265SDimitry Andric V = getValue(Record, i * 2 + 1, NextValueNo, Ty, TyID, EdgeBB); 564381ad6265SDimitry Andric if (!V) { 564481ad6265SDimitry Andric PN->deleteValue(); 564581ad6265SDimitry Andric PhiConstExprBB->eraseFromParent(); 564681ad6265SDimitry Andric return error("Invalid phi record"); 564781ad6265SDimitry Andric } 564881ad6265SDimitry Andric 564981ad6265SDimitry Andric if (EdgeBB == PhiConstExprBB && !EdgeBB->empty()) { 565081ad6265SDimitry Andric ConstExprEdgeBBs.insert({{BB, CurBB}, EdgeBB}); 565181ad6265SDimitry Andric PhiConstExprBB = nullptr; 565281ad6265SDimitry Andric } 5653*0b57cec5SDimitry Andric PN->addIncoming(V, BB); 565481ad6265SDimitry Andric Args.insert({BB, V}); 5655*0b57cec5SDimitry Andric } 5656*0b57cec5SDimitry Andric I = PN; 565781ad6265SDimitry Andric ResTypeID = TyID; 56588bcb0991SDimitry Andric 56598bcb0991SDimitry Andric // If there are an even number of records, the final record must be FMF. 56608bcb0991SDimitry Andric if (Record.size() % 2 == 0) { 56618bcb0991SDimitry Andric assert(isa<FPMathOperator>(I) && "Unexpected phi type"); 56628bcb0991SDimitry Andric FastMathFlags FMF = getDecodedFastMathFlags(Record[Record.size() - 1]); 56638bcb0991SDimitry Andric if (FMF.any()) 56648bcb0991SDimitry Andric I->setFastMathFlags(FMF); 56658bcb0991SDimitry Andric } 56668bcb0991SDimitry Andric 5667*0b57cec5SDimitry Andric break; 5668*0b57cec5SDimitry Andric } 5669*0b57cec5SDimitry Andric 5670*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_LANDINGPAD: 5671*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_LANDINGPAD_OLD: { 5672*0b57cec5SDimitry Andric // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?] 5673*0b57cec5SDimitry Andric unsigned Idx = 0; 5674*0b57cec5SDimitry Andric if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD) { 5675*0b57cec5SDimitry Andric if (Record.size() < 3) 5676*0b57cec5SDimitry Andric return error("Invalid record"); 5677*0b57cec5SDimitry Andric } else { 5678*0b57cec5SDimitry Andric assert(BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD); 5679*0b57cec5SDimitry Andric if (Record.size() < 4) 5680*0b57cec5SDimitry Andric return error("Invalid record"); 5681*0b57cec5SDimitry Andric } 568281ad6265SDimitry Andric ResTypeID = Record[Idx++]; 568381ad6265SDimitry Andric Type *Ty = getTypeByID(ResTypeID); 5684*0b57cec5SDimitry Andric if (!Ty) 5685*0b57cec5SDimitry Andric return error("Invalid record"); 5686*0b57cec5SDimitry Andric if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD) { 5687*0b57cec5SDimitry Andric Value *PersFn = nullptr; 568881ad6265SDimitry Andric unsigned PersFnTypeID; 568981ad6265SDimitry Andric if (getValueTypePair(Record, Idx, NextValueNo, PersFn, PersFnTypeID, 569081ad6265SDimitry Andric nullptr)) 5691*0b57cec5SDimitry Andric return error("Invalid record"); 5692*0b57cec5SDimitry Andric 5693*0b57cec5SDimitry Andric if (!F->hasPersonalityFn()) 5694*0b57cec5SDimitry Andric F->setPersonalityFn(cast<Constant>(PersFn)); 5695*0b57cec5SDimitry Andric else if (F->getPersonalityFn() != cast<Constant>(PersFn)) 5696*0b57cec5SDimitry Andric return error("Personality function mismatch"); 5697*0b57cec5SDimitry Andric } 5698*0b57cec5SDimitry Andric 5699*0b57cec5SDimitry Andric bool IsCleanup = !!Record[Idx++]; 5700*0b57cec5SDimitry Andric unsigned NumClauses = Record[Idx++]; 5701*0b57cec5SDimitry Andric LandingPadInst *LP = LandingPadInst::Create(Ty, NumClauses); 5702*0b57cec5SDimitry Andric LP->setCleanup(IsCleanup); 5703*0b57cec5SDimitry Andric for (unsigned J = 0; J != NumClauses; ++J) { 5704*0b57cec5SDimitry Andric LandingPadInst::ClauseType CT = 5705*0b57cec5SDimitry Andric LandingPadInst::ClauseType(Record[Idx++]); (void)CT; 5706*0b57cec5SDimitry Andric Value *Val; 570781ad6265SDimitry Andric unsigned ValTypeID; 5708*0b57cec5SDimitry Andric 570981ad6265SDimitry Andric if (getValueTypePair(Record, Idx, NextValueNo, Val, ValTypeID, 571081ad6265SDimitry Andric nullptr)) { 5711*0b57cec5SDimitry Andric delete LP; 5712*0b57cec5SDimitry Andric return error("Invalid record"); 5713*0b57cec5SDimitry Andric } 5714*0b57cec5SDimitry Andric 5715*0b57cec5SDimitry Andric assert((CT != LandingPadInst::Catch || 5716*0b57cec5SDimitry Andric !isa<ArrayType>(Val->getType())) && 5717*0b57cec5SDimitry Andric "Catch clause has a invalid type!"); 5718*0b57cec5SDimitry Andric assert((CT != LandingPadInst::Filter || 5719*0b57cec5SDimitry Andric isa<ArrayType>(Val->getType())) && 5720*0b57cec5SDimitry Andric "Filter clause has invalid type!"); 5721*0b57cec5SDimitry Andric LP->addClause(cast<Constant>(Val)); 5722*0b57cec5SDimitry Andric } 5723*0b57cec5SDimitry Andric 5724*0b57cec5SDimitry Andric I = LP; 5725*0b57cec5SDimitry Andric InstructionList.push_back(I); 5726*0b57cec5SDimitry Andric break; 5727*0b57cec5SDimitry Andric } 5728*0b57cec5SDimitry Andric 5729*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align] 573081ad6265SDimitry Andric if (Record.size() != 4 && Record.size() != 5) 5731*0b57cec5SDimitry Andric return error("Invalid record"); 5732e8d8bef9SDimitry Andric using APV = AllocaPackedValues; 5733e8d8bef9SDimitry Andric const uint64_t Rec = Record[3]; 5734e8d8bef9SDimitry Andric const bool InAlloca = Bitfield::get<APV::UsedWithInAlloca>(Rec); 5735e8d8bef9SDimitry Andric const bool SwiftError = Bitfield::get<APV::SwiftError>(Rec); 573681ad6265SDimitry Andric unsigned TyID = Record[0]; 573781ad6265SDimitry Andric Type *Ty = getTypeByID(TyID); 5738e8d8bef9SDimitry Andric if (!Bitfield::get<APV::ExplicitType>(Rec)) { 573981ad6265SDimitry Andric TyID = getContainedTypeID(TyID); 574081ad6265SDimitry Andric Ty = getTypeByID(TyID); 574181ad6265SDimitry Andric if (!Ty) 574281ad6265SDimitry Andric return error("Missing element type for old-style alloca"); 5743*0b57cec5SDimitry Andric } 574481ad6265SDimitry Andric unsigned OpTyID = Record[1]; 574581ad6265SDimitry Andric Type *OpTy = getTypeByID(OpTyID); 574681ad6265SDimitry Andric Value *Size = getFnValueByID(Record[2], OpTy, OpTyID, CurBB); 57478bcb0991SDimitry Andric MaybeAlign Align; 5748349cc55cSDimitry Andric uint64_t AlignExp = 5749349cc55cSDimitry Andric Bitfield::get<APV::AlignLower>(Rec) | 5750349cc55cSDimitry Andric (Bitfield::get<APV::AlignUpper>(Rec) << APV::AlignLower::Bits); 5751349cc55cSDimitry Andric if (Error Err = parseAlignmentValue(AlignExp, Align)) { 5752*0b57cec5SDimitry Andric return Err; 5753*0b57cec5SDimitry Andric } 5754*0b57cec5SDimitry Andric if (!Ty || !Size) 5755*0b57cec5SDimitry Andric return error("Invalid record"); 5756*0b57cec5SDimitry Andric 5757*0b57cec5SDimitry Andric const DataLayout &DL = TheModule->getDataLayout(); 575881ad6265SDimitry Andric unsigned AS = Record.size() == 5 ? Record[4] : DL.getAllocaAddrSpace(); 5759*0b57cec5SDimitry Andric 57605ffd83dbSDimitry Andric SmallPtrSet<Type *, 4> Visited; 57615ffd83dbSDimitry Andric if (!Align && !Ty->isSized(&Visited)) 57625ffd83dbSDimitry Andric return error("alloca of unsized type"); 57635ffd83dbSDimitry Andric if (!Align) 57645ffd83dbSDimitry Andric Align = DL.getPrefTypeAlign(Ty); 57655ffd83dbSDimitry Andric 57665ffd83dbSDimitry Andric AllocaInst *AI = new AllocaInst(Ty, AS, Size, *Align); 5767*0b57cec5SDimitry Andric AI->setUsedWithInAlloca(InAlloca); 5768*0b57cec5SDimitry Andric AI->setSwiftError(SwiftError); 5769*0b57cec5SDimitry Andric I = AI; 577081ad6265SDimitry Andric ResTypeID = getVirtualTypeID(AI->getType(), TyID); 5771*0b57cec5SDimitry Andric InstructionList.push_back(I); 5772*0b57cec5SDimitry Andric break; 5773*0b57cec5SDimitry Andric } 5774*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol] 5775*0b57cec5SDimitry Andric unsigned OpNum = 0; 5776*0b57cec5SDimitry Andric Value *Op; 577781ad6265SDimitry Andric unsigned OpTypeID; 577881ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB) || 5779*0b57cec5SDimitry Andric (OpNum + 2 != Record.size() && OpNum + 3 != Record.size())) 5780*0b57cec5SDimitry Andric return error("Invalid record"); 5781*0b57cec5SDimitry Andric 5782*0b57cec5SDimitry Andric if (!isa<PointerType>(Op->getType())) 5783*0b57cec5SDimitry Andric return error("Load operand is not a pointer type"); 5784*0b57cec5SDimitry Andric 5785*0b57cec5SDimitry Andric Type *Ty = nullptr; 5786*0b57cec5SDimitry Andric if (OpNum + 3 == Record.size()) { 578781ad6265SDimitry Andric ResTypeID = Record[OpNum++]; 578881ad6265SDimitry Andric Ty = getTypeByID(ResTypeID); 5789fe6060f1SDimitry Andric } else { 579081ad6265SDimitry Andric ResTypeID = getContainedTypeID(OpTypeID); 579181ad6265SDimitry Andric Ty = getTypeByID(ResTypeID); 579281ad6265SDimitry Andric if (!Ty) 579381ad6265SDimitry Andric return error("Missing element type for old-style load"); 5794fe6060f1SDimitry Andric } 5795*0b57cec5SDimitry Andric 5796*0b57cec5SDimitry Andric if (Error Err = typeCheckLoadStoreInst(Ty, Op->getType())) 5797*0b57cec5SDimitry Andric return Err; 5798*0b57cec5SDimitry Andric 57998bcb0991SDimitry Andric MaybeAlign Align; 5800*0b57cec5SDimitry Andric if (Error Err = parseAlignmentValue(Record[OpNum], Align)) 5801*0b57cec5SDimitry Andric return Err; 58025ffd83dbSDimitry Andric SmallPtrSet<Type *, 4> Visited; 58035ffd83dbSDimitry Andric if (!Align && !Ty->isSized(&Visited)) 58045ffd83dbSDimitry Andric return error("load of unsized type"); 58055ffd83dbSDimitry Andric if (!Align) 58065ffd83dbSDimitry Andric Align = TheModule->getDataLayout().getABITypeAlign(Ty); 58075ffd83dbSDimitry Andric I = new LoadInst(Ty, Op, "", Record[OpNum + 1], *Align); 5808*0b57cec5SDimitry Andric InstructionList.push_back(I); 5809*0b57cec5SDimitry Andric break; 5810*0b57cec5SDimitry Andric } 5811*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_LOADATOMIC: { 5812*0b57cec5SDimitry Andric // LOADATOMIC: [opty, op, align, vol, ordering, ssid] 5813*0b57cec5SDimitry Andric unsigned OpNum = 0; 5814*0b57cec5SDimitry Andric Value *Op; 581581ad6265SDimitry Andric unsigned OpTypeID; 581681ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB) || 5817*0b57cec5SDimitry Andric (OpNum + 4 != Record.size() && OpNum + 5 != Record.size())) 5818*0b57cec5SDimitry Andric return error("Invalid record"); 5819*0b57cec5SDimitry Andric 5820*0b57cec5SDimitry Andric if (!isa<PointerType>(Op->getType())) 5821*0b57cec5SDimitry Andric return error("Load operand is not a pointer type"); 5822*0b57cec5SDimitry Andric 5823*0b57cec5SDimitry Andric Type *Ty = nullptr; 5824*0b57cec5SDimitry Andric if (OpNum + 5 == Record.size()) { 582581ad6265SDimitry Andric ResTypeID = Record[OpNum++]; 582681ad6265SDimitry Andric Ty = getTypeByID(ResTypeID); 5827fe6060f1SDimitry Andric } else { 582881ad6265SDimitry Andric ResTypeID = getContainedTypeID(OpTypeID); 582981ad6265SDimitry Andric Ty = getTypeByID(ResTypeID); 583081ad6265SDimitry Andric if (!Ty) 583181ad6265SDimitry Andric return error("Missing element type for old style atomic load"); 5832fe6060f1SDimitry Andric } 5833*0b57cec5SDimitry Andric 5834*0b57cec5SDimitry Andric if (Error Err = typeCheckLoadStoreInst(Ty, Op->getType())) 5835*0b57cec5SDimitry Andric return Err; 5836*0b57cec5SDimitry Andric 5837*0b57cec5SDimitry Andric AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); 5838*0b57cec5SDimitry Andric if (Ordering == AtomicOrdering::NotAtomic || 5839*0b57cec5SDimitry Andric Ordering == AtomicOrdering::Release || 5840*0b57cec5SDimitry Andric Ordering == AtomicOrdering::AcquireRelease) 5841*0b57cec5SDimitry Andric return error("Invalid record"); 5842*0b57cec5SDimitry Andric if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0) 5843*0b57cec5SDimitry Andric return error("Invalid record"); 5844*0b57cec5SDimitry Andric SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]); 5845*0b57cec5SDimitry Andric 58468bcb0991SDimitry Andric MaybeAlign Align; 5847*0b57cec5SDimitry Andric if (Error Err = parseAlignmentValue(Record[OpNum], Align)) 5848*0b57cec5SDimitry Andric return Err; 58495ffd83dbSDimitry Andric if (!Align) 58505ffd83dbSDimitry Andric return error("Alignment missing from atomic load"); 58515ffd83dbSDimitry Andric I = new LoadInst(Ty, Op, "", Record[OpNum + 1], *Align, Ordering, SSID); 5852*0b57cec5SDimitry Andric InstructionList.push_back(I); 5853*0b57cec5SDimitry Andric break; 5854*0b57cec5SDimitry Andric } 5855*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_STORE: 5856*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_STORE_OLD: { // STORE2:[ptrty, ptr, val, align, vol] 5857*0b57cec5SDimitry Andric unsigned OpNum = 0; 5858*0b57cec5SDimitry Andric Value *Val, *Ptr; 585981ad6265SDimitry Andric unsigned PtrTypeID, ValTypeID; 586081ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, PtrTypeID, CurBB)) 586181ad6265SDimitry Andric return error("Invalid record"); 586281ad6265SDimitry Andric 586381ad6265SDimitry Andric if (BitCode == bitc::FUNC_CODE_INST_STORE) { 586481ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Val, ValTypeID, CurBB)) 586581ad6265SDimitry Andric return error("Invalid record"); 586681ad6265SDimitry Andric } else { 586781ad6265SDimitry Andric ValTypeID = getContainedTypeID(PtrTypeID); 586881ad6265SDimitry Andric if (popValue(Record, OpNum, NextValueNo, getTypeByID(ValTypeID), 586981ad6265SDimitry Andric ValTypeID, Val, CurBB)) 587081ad6265SDimitry Andric return error("Invalid record"); 587181ad6265SDimitry Andric } 587281ad6265SDimitry Andric 587381ad6265SDimitry Andric if (OpNum + 2 != Record.size()) 5874*0b57cec5SDimitry Andric return error("Invalid record"); 5875*0b57cec5SDimitry Andric 5876*0b57cec5SDimitry Andric if (Error Err = typeCheckLoadStoreInst(Val->getType(), Ptr->getType())) 5877*0b57cec5SDimitry Andric return Err; 58788bcb0991SDimitry Andric MaybeAlign Align; 5879*0b57cec5SDimitry Andric if (Error Err = parseAlignmentValue(Record[OpNum], Align)) 5880*0b57cec5SDimitry Andric return Err; 58815ffd83dbSDimitry Andric SmallPtrSet<Type *, 4> Visited; 58825ffd83dbSDimitry Andric if (!Align && !Val->getType()->isSized(&Visited)) 58835ffd83dbSDimitry Andric return error("store of unsized type"); 58845ffd83dbSDimitry Andric if (!Align) 58855ffd83dbSDimitry Andric Align = TheModule->getDataLayout().getABITypeAlign(Val->getType()); 58865ffd83dbSDimitry Andric I = new StoreInst(Val, Ptr, Record[OpNum + 1], *Align); 5887*0b57cec5SDimitry Andric InstructionList.push_back(I); 5888*0b57cec5SDimitry Andric break; 5889*0b57cec5SDimitry Andric } 5890*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_STOREATOMIC: 5891*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_STOREATOMIC_OLD: { 5892*0b57cec5SDimitry Andric // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, ssid] 5893*0b57cec5SDimitry Andric unsigned OpNum = 0; 5894*0b57cec5SDimitry Andric Value *Val, *Ptr; 589581ad6265SDimitry Andric unsigned PtrTypeID, ValTypeID; 589681ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, PtrTypeID, CurBB) || 589781ad6265SDimitry Andric !isa<PointerType>(Ptr->getType())) 589881ad6265SDimitry Andric return error("Invalid record"); 589981ad6265SDimitry Andric if (BitCode == bitc::FUNC_CODE_INST_STOREATOMIC) { 590081ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Val, ValTypeID, CurBB)) 590181ad6265SDimitry Andric return error("Invalid record"); 590281ad6265SDimitry Andric } else { 590381ad6265SDimitry Andric ValTypeID = getContainedTypeID(PtrTypeID); 590481ad6265SDimitry Andric if (popValue(Record, OpNum, NextValueNo, getTypeByID(ValTypeID), 590581ad6265SDimitry Andric ValTypeID, Val, CurBB)) 590681ad6265SDimitry Andric return error("Invalid record"); 590781ad6265SDimitry Andric } 590881ad6265SDimitry Andric 590981ad6265SDimitry Andric if (OpNum + 4 != Record.size()) 5910*0b57cec5SDimitry Andric return error("Invalid record"); 5911*0b57cec5SDimitry Andric 5912*0b57cec5SDimitry Andric if (Error Err = typeCheckLoadStoreInst(Val->getType(), Ptr->getType())) 5913*0b57cec5SDimitry Andric return Err; 5914*0b57cec5SDimitry Andric AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); 5915*0b57cec5SDimitry Andric if (Ordering == AtomicOrdering::NotAtomic || 5916*0b57cec5SDimitry Andric Ordering == AtomicOrdering::Acquire || 5917*0b57cec5SDimitry Andric Ordering == AtomicOrdering::AcquireRelease) 5918*0b57cec5SDimitry Andric return error("Invalid record"); 5919*0b57cec5SDimitry Andric SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]); 5920*0b57cec5SDimitry Andric if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0) 5921*0b57cec5SDimitry Andric return error("Invalid record"); 5922*0b57cec5SDimitry Andric 59238bcb0991SDimitry Andric MaybeAlign Align; 5924*0b57cec5SDimitry Andric if (Error Err = parseAlignmentValue(Record[OpNum], Align)) 5925*0b57cec5SDimitry Andric return Err; 59265ffd83dbSDimitry Andric if (!Align) 59275ffd83dbSDimitry Andric return error("Alignment missing from atomic store"); 59285ffd83dbSDimitry Andric I = new StoreInst(Val, Ptr, Record[OpNum + 1], *Align, Ordering, SSID); 5929*0b57cec5SDimitry Andric InstructionList.push_back(I); 5930*0b57cec5SDimitry Andric break; 5931*0b57cec5SDimitry Andric } 5932e8d8bef9SDimitry Andric case bitc::FUNC_CODE_INST_CMPXCHG_OLD: { 5933e8d8bef9SDimitry Andric // CMPXCHG_OLD: [ptrty, ptr, cmp, val, vol, ordering, synchscope, 5934e8d8bef9SDimitry Andric // failure_ordering?, weak?] 5935e8d8bef9SDimitry Andric const size_t NumRecords = Record.size(); 5936*0b57cec5SDimitry Andric unsigned OpNum = 0; 5937e8d8bef9SDimitry Andric Value *Ptr = nullptr; 593881ad6265SDimitry Andric unsigned PtrTypeID; 593981ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, PtrTypeID, CurBB)) 5940*0b57cec5SDimitry Andric return error("Invalid record"); 5941*0b57cec5SDimitry Andric 5942*0b57cec5SDimitry Andric if (!isa<PointerType>(Ptr->getType())) 5943*0b57cec5SDimitry Andric return error("Cmpxchg operand is not a pointer type"); 5944*0b57cec5SDimitry Andric 5945e8d8bef9SDimitry Andric Value *Cmp = nullptr; 594681ad6265SDimitry Andric unsigned CmpTypeID = getContainedTypeID(PtrTypeID); 594781ad6265SDimitry Andric if (popValue(Record, OpNum, NextValueNo, getTypeByID(CmpTypeID), 594881ad6265SDimitry Andric CmpTypeID, Cmp, CurBB)) 5949*0b57cec5SDimitry Andric return error("Invalid record"); 5950e8d8bef9SDimitry Andric 5951e8d8bef9SDimitry Andric Value *New = nullptr; 595281ad6265SDimitry Andric if (popValue(Record, OpNum, NextValueNo, Cmp->getType(), CmpTypeID, 595381ad6265SDimitry Andric New, CurBB) || 5954e8d8bef9SDimitry Andric NumRecords < OpNum + 3 || NumRecords > OpNum + 5) 5955*0b57cec5SDimitry Andric return error("Invalid record"); 5956*0b57cec5SDimitry Andric 5957e8d8bef9SDimitry Andric const AtomicOrdering SuccessOrdering = 5958e8d8bef9SDimitry Andric getDecodedOrdering(Record[OpNum + 1]); 5959*0b57cec5SDimitry Andric if (SuccessOrdering == AtomicOrdering::NotAtomic || 5960*0b57cec5SDimitry Andric SuccessOrdering == AtomicOrdering::Unordered) 5961*0b57cec5SDimitry Andric return error("Invalid record"); 5962e8d8bef9SDimitry Andric 5963e8d8bef9SDimitry Andric const SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 2]); 5964*0b57cec5SDimitry Andric 5965*0b57cec5SDimitry Andric if (Error Err = typeCheckLoadStoreInst(Cmp->getType(), Ptr->getType())) 5966*0b57cec5SDimitry Andric return Err; 5967*0b57cec5SDimitry Andric 5968e8d8bef9SDimitry Andric const AtomicOrdering FailureOrdering = 5969e8d8bef9SDimitry Andric NumRecords < 7 5970e8d8bef9SDimitry Andric ? AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering) 5971e8d8bef9SDimitry Andric : getDecodedOrdering(Record[OpNum + 3]); 5972e8d8bef9SDimitry Andric 5973fe6060f1SDimitry Andric if (FailureOrdering == AtomicOrdering::NotAtomic || 5974fe6060f1SDimitry Andric FailureOrdering == AtomicOrdering::Unordered) 5975fe6060f1SDimitry Andric return error("Invalid record"); 5976fe6060f1SDimitry Andric 5977e8d8bef9SDimitry Andric const Align Alignment( 59785ffd83dbSDimitry Andric TheModule->getDataLayout().getTypeStoreSize(Cmp->getType())); 5979e8d8bef9SDimitry Andric 59805ffd83dbSDimitry Andric I = new AtomicCmpXchgInst(Ptr, Cmp, New, Alignment, SuccessOrdering, 59815ffd83dbSDimitry Andric FailureOrdering, SSID); 5982*0b57cec5SDimitry Andric cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]); 5983*0b57cec5SDimitry Andric 5984e8d8bef9SDimitry Andric if (NumRecords < 8) { 5985*0b57cec5SDimitry Andric // Before weak cmpxchgs existed, the instruction simply returned the 5986*0b57cec5SDimitry Andric // value loaded from memory, so bitcode files from that era will be 5987*0b57cec5SDimitry Andric // expecting the first component of a modern cmpxchg. 5988*0b57cec5SDimitry Andric CurBB->getInstList().push_back(I); 5989*0b57cec5SDimitry Andric I = ExtractValueInst::Create(I, 0); 599081ad6265SDimitry Andric ResTypeID = CmpTypeID; 5991*0b57cec5SDimitry Andric } else { 5992*0b57cec5SDimitry Andric cast<AtomicCmpXchgInst>(I)->setWeak(Record[OpNum + 4]); 599381ad6265SDimitry Andric unsigned I1TypeID = getVirtualTypeID(Type::getInt1Ty(Context)); 599481ad6265SDimitry Andric ResTypeID = getVirtualTypeID(I->getType(), {CmpTypeID, I1TypeID}); 5995*0b57cec5SDimitry Andric } 5996*0b57cec5SDimitry Andric 5997*0b57cec5SDimitry Andric InstructionList.push_back(I); 5998*0b57cec5SDimitry Andric break; 5999*0b57cec5SDimitry Andric } 6000e8d8bef9SDimitry Andric case bitc::FUNC_CODE_INST_CMPXCHG: { 6001e8d8bef9SDimitry Andric // CMPXCHG: [ptrty, ptr, cmp, val, vol, success_ordering, synchscope, 6002fe6060f1SDimitry Andric // failure_ordering, weak, align?] 6003e8d8bef9SDimitry Andric const size_t NumRecords = Record.size(); 6004e8d8bef9SDimitry Andric unsigned OpNum = 0; 6005e8d8bef9SDimitry Andric Value *Ptr = nullptr; 600681ad6265SDimitry Andric unsigned PtrTypeID; 600781ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, PtrTypeID, CurBB)) 6008e8d8bef9SDimitry Andric return error("Invalid record"); 6009e8d8bef9SDimitry Andric 6010e8d8bef9SDimitry Andric if (!isa<PointerType>(Ptr->getType())) 6011e8d8bef9SDimitry Andric return error("Cmpxchg operand is not a pointer type"); 6012e8d8bef9SDimitry Andric 6013e8d8bef9SDimitry Andric Value *Cmp = nullptr; 601481ad6265SDimitry Andric unsigned CmpTypeID; 601581ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Cmp, CmpTypeID, CurBB)) 6016e8d8bef9SDimitry Andric return error("Invalid record"); 6017e8d8bef9SDimitry Andric 6018e8d8bef9SDimitry Andric Value *Val = nullptr; 601981ad6265SDimitry Andric if (popValue(Record, OpNum, NextValueNo, Cmp->getType(), CmpTypeID, Val, 602081ad6265SDimitry Andric CurBB)) 6021e8d8bef9SDimitry Andric return error("Invalid record"); 6022e8d8bef9SDimitry Andric 6023fe6060f1SDimitry Andric if (NumRecords < OpNum + 3 || NumRecords > OpNum + 6) 6024fe6060f1SDimitry Andric return error("Invalid record"); 6025fe6060f1SDimitry Andric 6026fe6060f1SDimitry Andric const bool IsVol = Record[OpNum]; 6027fe6060f1SDimitry Andric 6028e8d8bef9SDimitry Andric const AtomicOrdering SuccessOrdering = 6029e8d8bef9SDimitry Andric getDecodedOrdering(Record[OpNum + 1]); 6030fe6060f1SDimitry Andric if (!AtomicCmpXchgInst::isValidSuccessOrdering(SuccessOrdering)) 6031fe6060f1SDimitry Andric return error("Invalid cmpxchg success ordering"); 6032e8d8bef9SDimitry Andric 6033e8d8bef9SDimitry Andric const SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 2]); 6034e8d8bef9SDimitry Andric 6035e8d8bef9SDimitry Andric if (Error Err = typeCheckLoadStoreInst(Cmp->getType(), Ptr->getType())) 6036e8d8bef9SDimitry Andric return Err; 6037e8d8bef9SDimitry Andric 6038e8d8bef9SDimitry Andric const AtomicOrdering FailureOrdering = 6039e8d8bef9SDimitry Andric getDecodedOrdering(Record[OpNum + 3]); 6040fe6060f1SDimitry Andric if (!AtomicCmpXchgInst::isValidFailureOrdering(FailureOrdering)) 6041fe6060f1SDimitry Andric return error("Invalid cmpxchg failure ordering"); 6042e8d8bef9SDimitry Andric 6043fe6060f1SDimitry Andric const bool IsWeak = Record[OpNum + 4]; 6044e8d8bef9SDimitry Andric 6045fe6060f1SDimitry Andric MaybeAlign Alignment; 6046fe6060f1SDimitry Andric 6047fe6060f1SDimitry Andric if (NumRecords == (OpNum + 6)) { 6048fe6060f1SDimitry Andric if (Error Err = parseAlignmentValue(Record[OpNum + 5], Alignment)) 6049fe6060f1SDimitry Andric return Err; 6050fe6060f1SDimitry Andric } 6051fe6060f1SDimitry Andric if (!Alignment) 6052fe6060f1SDimitry Andric Alignment = 6053fe6060f1SDimitry Andric Align(TheModule->getDataLayout().getTypeStoreSize(Cmp->getType())); 6054fe6060f1SDimitry Andric 6055fe6060f1SDimitry Andric I = new AtomicCmpXchgInst(Ptr, Cmp, Val, *Alignment, SuccessOrdering, 6056e8d8bef9SDimitry Andric FailureOrdering, SSID); 6057fe6060f1SDimitry Andric cast<AtomicCmpXchgInst>(I)->setVolatile(IsVol); 6058fe6060f1SDimitry Andric cast<AtomicCmpXchgInst>(I)->setWeak(IsWeak); 6059e8d8bef9SDimitry Andric 606081ad6265SDimitry Andric unsigned I1TypeID = getVirtualTypeID(Type::getInt1Ty(Context)); 606181ad6265SDimitry Andric ResTypeID = getVirtualTypeID(I->getType(), {CmpTypeID, I1TypeID}); 606281ad6265SDimitry Andric 6063e8d8bef9SDimitry Andric InstructionList.push_back(I); 6064e8d8bef9SDimitry Andric break; 6065e8d8bef9SDimitry Andric } 6066fe6060f1SDimitry Andric case bitc::FUNC_CODE_INST_ATOMICRMW_OLD: 6067*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_ATOMICRMW: { 6068fe6060f1SDimitry Andric // ATOMICRMW_OLD: [ptrty, ptr, val, op, vol, ordering, ssid, align?] 6069fe6060f1SDimitry Andric // ATOMICRMW: [ptrty, ptr, valty, val, op, vol, ordering, ssid, align?] 6070fe6060f1SDimitry Andric const size_t NumRecords = Record.size(); 6071*0b57cec5SDimitry Andric unsigned OpNum = 0; 6072fe6060f1SDimitry Andric 6073fe6060f1SDimitry Andric Value *Ptr = nullptr; 607481ad6265SDimitry Andric unsigned PtrTypeID; 607581ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, PtrTypeID, CurBB)) 6076*0b57cec5SDimitry Andric return error("Invalid record"); 6077fe6060f1SDimitry Andric 6078fe6060f1SDimitry Andric if (!isa<PointerType>(Ptr->getType())) 6079fe6060f1SDimitry Andric return error("Invalid record"); 6080fe6060f1SDimitry Andric 6081fe6060f1SDimitry Andric Value *Val = nullptr; 608281ad6265SDimitry Andric unsigned ValTypeID = InvalidTypeID; 6083fe6060f1SDimitry Andric if (BitCode == bitc::FUNC_CODE_INST_ATOMICRMW_OLD) { 608481ad6265SDimitry Andric ValTypeID = getContainedTypeID(PtrTypeID); 6085fe6060f1SDimitry Andric if (popValue(Record, OpNum, NextValueNo, 608681ad6265SDimitry Andric getTypeByID(ValTypeID), ValTypeID, Val, CurBB)) 6087fe6060f1SDimitry Andric return error("Invalid record"); 6088fe6060f1SDimitry Andric } else { 608981ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Val, ValTypeID, CurBB)) 6090fe6060f1SDimitry Andric return error("Invalid record"); 6091fe6060f1SDimitry Andric } 6092fe6060f1SDimitry Andric 6093fe6060f1SDimitry Andric if (!(NumRecords == (OpNum + 4) || NumRecords == (OpNum + 5))) 6094fe6060f1SDimitry Andric return error("Invalid record"); 6095fe6060f1SDimitry Andric 6096fe6060f1SDimitry Andric const AtomicRMWInst::BinOp Operation = 6097fe6060f1SDimitry Andric getDecodedRMWOperation(Record[OpNum]); 6098*0b57cec5SDimitry Andric if (Operation < AtomicRMWInst::FIRST_BINOP || 6099*0b57cec5SDimitry Andric Operation > AtomicRMWInst::LAST_BINOP) 6100*0b57cec5SDimitry Andric return error("Invalid record"); 6101fe6060f1SDimitry Andric 6102fe6060f1SDimitry Andric const bool IsVol = Record[OpNum + 1]; 6103fe6060f1SDimitry Andric 6104fe6060f1SDimitry Andric const AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); 6105*0b57cec5SDimitry Andric if (Ordering == AtomicOrdering::NotAtomic || 6106*0b57cec5SDimitry Andric Ordering == AtomicOrdering::Unordered) 6107*0b57cec5SDimitry Andric return error("Invalid record"); 6108fe6060f1SDimitry Andric 6109fe6060f1SDimitry Andric const SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]); 6110fe6060f1SDimitry Andric 6111fe6060f1SDimitry Andric MaybeAlign Alignment; 6112fe6060f1SDimitry Andric 6113fe6060f1SDimitry Andric if (NumRecords == (OpNum + 5)) { 6114fe6060f1SDimitry Andric if (Error Err = parseAlignmentValue(Record[OpNum + 4], Alignment)) 6115fe6060f1SDimitry Andric return Err; 6116fe6060f1SDimitry Andric } 6117fe6060f1SDimitry Andric 6118fe6060f1SDimitry Andric if (!Alignment) 6119fe6060f1SDimitry Andric Alignment = 6120fe6060f1SDimitry Andric Align(TheModule->getDataLayout().getTypeStoreSize(Val->getType())); 6121fe6060f1SDimitry Andric 6122fe6060f1SDimitry Andric I = new AtomicRMWInst(Operation, Ptr, Val, *Alignment, Ordering, SSID); 612381ad6265SDimitry Andric ResTypeID = ValTypeID; 6124fe6060f1SDimitry Andric cast<AtomicRMWInst>(I)->setVolatile(IsVol); 6125fe6060f1SDimitry Andric 6126*0b57cec5SDimitry Andric InstructionList.push_back(I); 6127*0b57cec5SDimitry Andric break; 6128*0b57cec5SDimitry Andric } 6129*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, ssid] 6130*0b57cec5SDimitry Andric if (2 != Record.size()) 6131*0b57cec5SDimitry Andric return error("Invalid record"); 6132*0b57cec5SDimitry Andric AtomicOrdering Ordering = getDecodedOrdering(Record[0]); 6133*0b57cec5SDimitry Andric if (Ordering == AtomicOrdering::NotAtomic || 6134*0b57cec5SDimitry Andric Ordering == AtomicOrdering::Unordered || 6135*0b57cec5SDimitry Andric Ordering == AtomicOrdering::Monotonic) 6136*0b57cec5SDimitry Andric return error("Invalid record"); 6137*0b57cec5SDimitry Andric SyncScope::ID SSID = getDecodedSyncScopeID(Record[1]); 6138*0b57cec5SDimitry Andric I = new FenceInst(Context, Ordering, SSID); 6139*0b57cec5SDimitry Andric InstructionList.push_back(I); 6140*0b57cec5SDimitry Andric break; 6141*0b57cec5SDimitry Andric } 6142*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_CALL: { 6143*0b57cec5SDimitry Andric // CALL: [paramattrs, cc, fmf, fnty, fnid, arg0, arg1...] 6144*0b57cec5SDimitry Andric if (Record.size() < 3) 6145*0b57cec5SDimitry Andric return error("Invalid record"); 6146*0b57cec5SDimitry Andric 6147*0b57cec5SDimitry Andric unsigned OpNum = 0; 6148*0b57cec5SDimitry Andric AttributeList PAL = getAttributes(Record[OpNum++]); 6149*0b57cec5SDimitry Andric unsigned CCInfo = Record[OpNum++]; 6150*0b57cec5SDimitry Andric 6151*0b57cec5SDimitry Andric FastMathFlags FMF; 6152*0b57cec5SDimitry Andric if ((CCInfo >> bitc::CALL_FMF) & 1) { 6153*0b57cec5SDimitry Andric FMF = getDecodedFastMathFlags(Record[OpNum++]); 6154*0b57cec5SDimitry Andric if (!FMF.any()) 6155*0b57cec5SDimitry Andric return error("Fast math flags indicator set for call with no FMF"); 6156*0b57cec5SDimitry Andric } 6157*0b57cec5SDimitry Andric 615881ad6265SDimitry Andric unsigned FTyID = InvalidTypeID; 6159*0b57cec5SDimitry Andric FunctionType *FTy = nullptr; 6160*0b57cec5SDimitry Andric if ((CCInfo >> bitc::CALL_EXPLICIT_TYPE) & 1) { 616181ad6265SDimitry Andric FTyID = Record[OpNum++]; 616281ad6265SDimitry Andric FTy = dyn_cast_or_null<FunctionType>(getTypeByID(FTyID)); 6163fe6060f1SDimitry Andric if (!FTy) 6164*0b57cec5SDimitry Andric return error("Explicit call type is not a function type"); 6165*0b57cec5SDimitry Andric } 6166*0b57cec5SDimitry Andric 6167*0b57cec5SDimitry Andric Value *Callee; 616881ad6265SDimitry Andric unsigned CalleeTypeID; 616981ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Callee, CalleeTypeID, 617081ad6265SDimitry Andric CurBB)) 6171*0b57cec5SDimitry Andric return error("Invalid record"); 6172*0b57cec5SDimitry Andric 6173*0b57cec5SDimitry Andric PointerType *OpTy = dyn_cast<PointerType>(Callee->getType()); 6174*0b57cec5SDimitry Andric if (!OpTy) 6175*0b57cec5SDimitry Andric return error("Callee is not a pointer type"); 6176*0b57cec5SDimitry Andric if (!FTy) { 617781ad6265SDimitry Andric FTyID = getContainedTypeID(CalleeTypeID); 617881ad6265SDimitry Andric FTy = dyn_cast_or_null<FunctionType>(getTypeByID(FTyID)); 6179fe6060f1SDimitry Andric if (!FTy) 6180*0b57cec5SDimitry Andric return error("Callee is not of pointer to function type"); 6181fe6060f1SDimitry Andric } else if (!OpTy->isOpaqueOrPointeeTypeMatches(FTy)) 6182*0b57cec5SDimitry Andric return error("Explicit call type does not match pointee type of " 6183*0b57cec5SDimitry Andric "callee operand"); 6184*0b57cec5SDimitry Andric if (Record.size() < FTy->getNumParams() + OpNum) 6185*0b57cec5SDimitry Andric return error("Insufficient operands to call"); 6186*0b57cec5SDimitry Andric 6187*0b57cec5SDimitry Andric SmallVector<Value*, 16> Args; 618881ad6265SDimitry Andric SmallVector<unsigned, 16> ArgTyIDs; 6189*0b57cec5SDimitry Andric // Read the fixed params. 6190*0b57cec5SDimitry Andric for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { 619181ad6265SDimitry Andric unsigned ArgTyID = getContainedTypeID(FTyID, i + 1); 6192*0b57cec5SDimitry Andric if (FTy->getParamType(i)->isLabelTy()) 6193*0b57cec5SDimitry Andric Args.push_back(getBasicBlock(Record[OpNum])); 6194*0b57cec5SDimitry Andric else 6195*0b57cec5SDimitry Andric Args.push_back(getValue(Record, OpNum, NextValueNo, 619681ad6265SDimitry Andric FTy->getParamType(i), ArgTyID, CurBB)); 619781ad6265SDimitry Andric ArgTyIDs.push_back(ArgTyID); 6198*0b57cec5SDimitry Andric if (!Args.back()) 6199*0b57cec5SDimitry Andric return error("Invalid record"); 6200*0b57cec5SDimitry Andric } 6201*0b57cec5SDimitry Andric 6202*0b57cec5SDimitry Andric // Read type/value pairs for varargs params. 6203*0b57cec5SDimitry Andric if (!FTy->isVarArg()) { 6204*0b57cec5SDimitry Andric if (OpNum != Record.size()) 6205*0b57cec5SDimitry Andric return error("Invalid record"); 6206*0b57cec5SDimitry Andric } else { 6207*0b57cec5SDimitry Andric while (OpNum != Record.size()) { 6208*0b57cec5SDimitry Andric Value *Op; 620981ad6265SDimitry Andric unsigned OpTypeID; 621081ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB)) 6211*0b57cec5SDimitry Andric return error("Invalid record"); 6212*0b57cec5SDimitry Andric Args.push_back(Op); 621381ad6265SDimitry Andric ArgTyIDs.push_back(OpTypeID); 6214*0b57cec5SDimitry Andric } 6215*0b57cec5SDimitry Andric } 6216*0b57cec5SDimitry Andric 621781ad6265SDimitry Andric // Upgrade the bundles if needed. 621881ad6265SDimitry Andric if (!OperandBundles.empty()) 621981ad6265SDimitry Andric UpgradeOperandBundles(OperandBundles); 622081ad6265SDimitry Andric 6221*0b57cec5SDimitry Andric I = CallInst::Create(FTy, Callee, Args, OperandBundles); 622281ad6265SDimitry Andric ResTypeID = getContainedTypeID(FTyID); 6223*0b57cec5SDimitry Andric OperandBundles.clear(); 6224*0b57cec5SDimitry Andric InstructionList.push_back(I); 6225*0b57cec5SDimitry Andric cast<CallInst>(I)->setCallingConv( 6226*0b57cec5SDimitry Andric static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV)); 6227*0b57cec5SDimitry Andric CallInst::TailCallKind TCK = CallInst::TCK_None; 6228*0b57cec5SDimitry Andric if (CCInfo & 1 << bitc::CALL_TAIL) 6229*0b57cec5SDimitry Andric TCK = CallInst::TCK_Tail; 6230*0b57cec5SDimitry Andric if (CCInfo & (1 << bitc::CALL_MUSTTAIL)) 6231*0b57cec5SDimitry Andric TCK = CallInst::TCK_MustTail; 6232*0b57cec5SDimitry Andric if (CCInfo & (1 << bitc::CALL_NOTAIL)) 6233*0b57cec5SDimitry Andric TCK = CallInst::TCK_NoTail; 6234*0b57cec5SDimitry Andric cast<CallInst>(I)->setTailCallKind(TCK); 6235*0b57cec5SDimitry Andric cast<CallInst>(I)->setAttributes(PAL); 623681ad6265SDimitry Andric if (Error Err = propagateAttributeTypes(cast<CallBase>(I), ArgTyIDs)) { 623781ad6265SDimitry Andric I->deleteValue(); 623881ad6265SDimitry Andric return Err; 623981ad6265SDimitry Andric } 6240*0b57cec5SDimitry Andric if (FMF.any()) { 6241*0b57cec5SDimitry Andric if (!isa<FPMathOperator>(I)) 6242*0b57cec5SDimitry Andric return error("Fast-math-flags specified for call without " 6243*0b57cec5SDimitry Andric "floating-point scalar or vector return type"); 6244*0b57cec5SDimitry Andric I->setFastMathFlags(FMF); 6245*0b57cec5SDimitry Andric } 6246*0b57cec5SDimitry Andric break; 6247*0b57cec5SDimitry Andric } 6248*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty] 6249*0b57cec5SDimitry Andric if (Record.size() < 3) 6250*0b57cec5SDimitry Andric return error("Invalid record"); 625181ad6265SDimitry Andric unsigned OpTyID = Record[0]; 625281ad6265SDimitry Andric Type *OpTy = getTypeByID(OpTyID); 625381ad6265SDimitry Andric Value *Op = getValue(Record, 1, NextValueNo, OpTy, OpTyID, CurBB); 625481ad6265SDimitry Andric ResTypeID = Record[2]; 625581ad6265SDimitry Andric Type *ResTy = getTypeByID(ResTypeID); 6256*0b57cec5SDimitry Andric if (!OpTy || !Op || !ResTy) 6257*0b57cec5SDimitry Andric return error("Invalid record"); 6258*0b57cec5SDimitry Andric I = new VAArgInst(Op, ResTy); 6259*0b57cec5SDimitry Andric InstructionList.push_back(I); 6260*0b57cec5SDimitry Andric break; 6261*0b57cec5SDimitry Andric } 6262*0b57cec5SDimitry Andric 6263*0b57cec5SDimitry Andric case bitc::FUNC_CODE_OPERAND_BUNDLE: { 6264*0b57cec5SDimitry Andric // A call or an invoke can be optionally prefixed with some variable 6265*0b57cec5SDimitry Andric // number of operand bundle blocks. These blocks are read into 6266*0b57cec5SDimitry Andric // OperandBundles and consumed at the next call or invoke instruction. 6267*0b57cec5SDimitry Andric 6268e8d8bef9SDimitry Andric if (Record.empty() || Record[0] >= BundleTags.size()) 6269*0b57cec5SDimitry Andric return error("Invalid record"); 6270*0b57cec5SDimitry Andric 6271*0b57cec5SDimitry Andric std::vector<Value *> Inputs; 6272*0b57cec5SDimitry Andric 6273*0b57cec5SDimitry Andric unsigned OpNum = 1; 6274*0b57cec5SDimitry Andric while (OpNum != Record.size()) { 6275*0b57cec5SDimitry Andric Value *Op; 627681ad6265SDimitry Andric unsigned OpTypeID; 627781ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB)) 6278*0b57cec5SDimitry Andric return error("Invalid record"); 6279*0b57cec5SDimitry Andric Inputs.push_back(Op); 6280*0b57cec5SDimitry Andric } 6281*0b57cec5SDimitry Andric 6282*0b57cec5SDimitry Andric OperandBundles.emplace_back(BundleTags[Record[0]], std::move(Inputs)); 6283*0b57cec5SDimitry Andric continue; 6284*0b57cec5SDimitry Andric } 6285480093f4SDimitry Andric 6286480093f4SDimitry Andric case bitc::FUNC_CODE_INST_FREEZE: { // FREEZE: [opty,opval] 6287480093f4SDimitry Andric unsigned OpNum = 0; 6288480093f4SDimitry Andric Value *Op = nullptr; 628981ad6265SDimitry Andric unsigned OpTypeID; 629081ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB)) 6291480093f4SDimitry Andric return error("Invalid record"); 6292480093f4SDimitry Andric if (OpNum != Record.size()) 6293480093f4SDimitry Andric return error("Invalid record"); 6294480093f4SDimitry Andric 6295480093f4SDimitry Andric I = new FreezeInst(Op); 629681ad6265SDimitry Andric ResTypeID = OpTypeID; 6297480093f4SDimitry Andric InstructionList.push_back(I); 6298480093f4SDimitry Andric break; 6299480093f4SDimitry Andric } 6300*0b57cec5SDimitry Andric } 6301*0b57cec5SDimitry Andric 6302*0b57cec5SDimitry Andric // Add instruction to end of current BB. If there is no current BB, reject 6303*0b57cec5SDimitry Andric // this file. 6304*0b57cec5SDimitry Andric if (!CurBB) { 6305*0b57cec5SDimitry Andric I->deleteValue(); 6306*0b57cec5SDimitry Andric return error("Invalid instruction with no BB"); 6307*0b57cec5SDimitry Andric } 6308*0b57cec5SDimitry Andric if (!OperandBundles.empty()) { 6309*0b57cec5SDimitry Andric I->deleteValue(); 6310*0b57cec5SDimitry Andric return error("Operand bundles found with no consumer"); 6311*0b57cec5SDimitry Andric } 6312*0b57cec5SDimitry Andric CurBB->getInstList().push_back(I); 6313*0b57cec5SDimitry Andric 6314*0b57cec5SDimitry Andric // If this was a terminator instruction, move to the next block. 6315*0b57cec5SDimitry Andric if (I->isTerminator()) { 6316*0b57cec5SDimitry Andric ++CurBBNo; 6317*0b57cec5SDimitry Andric CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr; 6318*0b57cec5SDimitry Andric } 6319*0b57cec5SDimitry Andric 6320*0b57cec5SDimitry Andric // Non-void values get registered in the value table for future use. 632181ad6265SDimitry Andric if (!I->getType()->isVoidTy()) { 632281ad6265SDimitry Andric assert(I->getType() == getTypeByID(ResTypeID) && 632381ad6265SDimitry Andric "Incorrect result type ID"); 632481ad6265SDimitry Andric if (Error Err = ValueList.assignValue(NextValueNo++, I, ResTypeID)) 632581ad6265SDimitry Andric return Err; 632681ad6265SDimitry Andric } 6327*0b57cec5SDimitry Andric } 6328*0b57cec5SDimitry Andric 6329*0b57cec5SDimitry Andric OutOfRecordLoop: 6330*0b57cec5SDimitry Andric 6331*0b57cec5SDimitry Andric if (!OperandBundles.empty()) 6332*0b57cec5SDimitry Andric return error("Operand bundles found with no consumer"); 6333*0b57cec5SDimitry Andric 6334*0b57cec5SDimitry Andric // Check the function list for unresolved values. 6335*0b57cec5SDimitry Andric if (Argument *A = dyn_cast<Argument>(ValueList.back())) { 6336*0b57cec5SDimitry Andric if (!A->getParent()) { 6337*0b57cec5SDimitry Andric // We found at least one unresolved value. Nuke them all to avoid leaks. 6338*0b57cec5SDimitry Andric for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){ 6339*0b57cec5SDimitry Andric if ((A = dyn_cast_or_null<Argument>(ValueList[i])) && !A->getParent()) { 6340*0b57cec5SDimitry Andric A->replaceAllUsesWith(UndefValue::get(A->getType())); 6341*0b57cec5SDimitry Andric delete A; 6342*0b57cec5SDimitry Andric } 6343*0b57cec5SDimitry Andric } 6344*0b57cec5SDimitry Andric return error("Never resolved value found in function"); 6345*0b57cec5SDimitry Andric } 6346*0b57cec5SDimitry Andric } 6347*0b57cec5SDimitry Andric 6348*0b57cec5SDimitry Andric // Unexpected unresolved metadata about to be dropped. 6349*0b57cec5SDimitry Andric if (MDLoader->hasFwdRefs()) 6350*0b57cec5SDimitry Andric return error("Invalid function metadata: outgoing forward refs"); 6351*0b57cec5SDimitry Andric 635281ad6265SDimitry Andric if (PhiConstExprBB) 635381ad6265SDimitry Andric PhiConstExprBB->eraseFromParent(); 635481ad6265SDimitry Andric 635581ad6265SDimitry Andric for (const auto &Pair : ConstExprEdgeBBs) { 635681ad6265SDimitry Andric BasicBlock *From = Pair.first.first; 635781ad6265SDimitry Andric BasicBlock *To = Pair.first.second; 635881ad6265SDimitry Andric BasicBlock *EdgeBB = Pair.second; 635981ad6265SDimitry Andric BranchInst::Create(To, EdgeBB); 636081ad6265SDimitry Andric From->getTerminator()->replaceSuccessorWith(To, EdgeBB); 636181ad6265SDimitry Andric To->replacePhiUsesWith(From, EdgeBB); 636281ad6265SDimitry Andric EdgeBB->moveBefore(To); 636381ad6265SDimitry Andric } 636481ad6265SDimitry Andric 6365*0b57cec5SDimitry Andric // Trim the value list down to the size it was before we parsed this function. 6366*0b57cec5SDimitry Andric ValueList.shrinkTo(ModuleValueListSize); 6367*0b57cec5SDimitry Andric MDLoader->shrinkTo(ModuleMDLoaderSize); 6368*0b57cec5SDimitry Andric std::vector<BasicBlock*>().swap(FunctionBBs); 6369*0b57cec5SDimitry Andric return Error::success(); 6370*0b57cec5SDimitry Andric } 6371*0b57cec5SDimitry Andric 6372*0b57cec5SDimitry Andric /// Find the function body in the bitcode stream 6373*0b57cec5SDimitry Andric Error BitcodeReader::findFunctionInStream( 6374*0b57cec5SDimitry Andric Function *F, 6375*0b57cec5SDimitry Andric DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) { 6376*0b57cec5SDimitry Andric while (DeferredFunctionInfoIterator->second == 0) { 6377*0b57cec5SDimitry Andric // This is the fallback handling for the old format bitcode that 6378*0b57cec5SDimitry Andric // didn't contain the function index in the VST, or when we have 6379*0b57cec5SDimitry Andric // an anonymous function which would not have a VST entry. 6380*0b57cec5SDimitry Andric // Assert that we have one of those two cases. 6381*0b57cec5SDimitry Andric assert(VSTOffset == 0 || !F->hasName()); 6382*0b57cec5SDimitry Andric // Parse the next body in the stream and set its position in the 6383*0b57cec5SDimitry Andric // DeferredFunctionInfo map. 6384*0b57cec5SDimitry Andric if (Error Err = rememberAndSkipFunctionBodies()) 6385*0b57cec5SDimitry Andric return Err; 6386*0b57cec5SDimitry Andric } 6387*0b57cec5SDimitry Andric return Error::success(); 6388*0b57cec5SDimitry Andric } 6389*0b57cec5SDimitry Andric 6390*0b57cec5SDimitry Andric SyncScope::ID BitcodeReader::getDecodedSyncScopeID(unsigned Val) { 6391*0b57cec5SDimitry Andric if (Val == SyncScope::SingleThread || Val == SyncScope::System) 6392*0b57cec5SDimitry Andric return SyncScope::ID(Val); 6393*0b57cec5SDimitry Andric if (Val >= SSIDs.size()) 6394*0b57cec5SDimitry Andric return SyncScope::System; // Map unknown synchronization scopes to system. 6395*0b57cec5SDimitry Andric return SSIDs[Val]; 6396*0b57cec5SDimitry Andric } 6397*0b57cec5SDimitry Andric 6398*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 6399*0b57cec5SDimitry Andric // GVMaterializer implementation 6400*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 6401*0b57cec5SDimitry Andric 6402*0b57cec5SDimitry Andric Error BitcodeReader::materialize(GlobalValue *GV) { 6403*0b57cec5SDimitry Andric Function *F = dyn_cast<Function>(GV); 6404*0b57cec5SDimitry Andric // If it's not a function or is already material, ignore the request. 6405*0b57cec5SDimitry Andric if (!F || !F->isMaterializable()) 6406*0b57cec5SDimitry Andric return Error::success(); 6407*0b57cec5SDimitry Andric 6408*0b57cec5SDimitry Andric DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F); 6409*0b57cec5SDimitry Andric assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!"); 6410*0b57cec5SDimitry Andric // If its position is recorded as 0, its body is somewhere in the stream 6411*0b57cec5SDimitry Andric // but we haven't seen it yet. 6412*0b57cec5SDimitry Andric if (DFII->second == 0) 6413*0b57cec5SDimitry Andric if (Error Err = findFunctionInStream(F, DFII)) 6414*0b57cec5SDimitry Andric return Err; 6415*0b57cec5SDimitry Andric 6416*0b57cec5SDimitry Andric // Materialize metadata before parsing any function bodies. 6417*0b57cec5SDimitry Andric if (Error Err = materializeMetadata()) 6418*0b57cec5SDimitry Andric return Err; 6419*0b57cec5SDimitry Andric 6420*0b57cec5SDimitry Andric // Move the bit stream to the saved position of the deferred function body. 6421*0b57cec5SDimitry Andric if (Error JumpFailed = Stream.JumpToBit(DFII->second)) 6422*0b57cec5SDimitry Andric return JumpFailed; 6423*0b57cec5SDimitry Andric if (Error Err = parseFunctionBody(F)) 6424*0b57cec5SDimitry Andric return Err; 6425*0b57cec5SDimitry Andric F->setIsMaterializable(false); 6426*0b57cec5SDimitry Andric 6427*0b57cec5SDimitry Andric if (StripDebugInfo) 6428*0b57cec5SDimitry Andric stripDebugInfo(*F); 6429*0b57cec5SDimitry Andric 6430*0b57cec5SDimitry Andric // Upgrade any old intrinsic calls in the function. 6431*0b57cec5SDimitry Andric for (auto &I : UpgradedIntrinsics) { 6432349cc55cSDimitry Andric for (User *U : llvm::make_early_inc_range(I.first->materialized_users())) 6433*0b57cec5SDimitry Andric if (CallInst *CI = dyn_cast<CallInst>(U)) 6434*0b57cec5SDimitry Andric UpgradeIntrinsicCall(CI, I.second); 6435*0b57cec5SDimitry Andric } 6436*0b57cec5SDimitry Andric 6437*0b57cec5SDimitry Andric // Update calls to the remangled intrinsics 6438*0b57cec5SDimitry Andric for (auto &I : RemangledIntrinsics) 6439349cc55cSDimitry Andric for (User *U : llvm::make_early_inc_range(I.first->materialized_users())) 6440*0b57cec5SDimitry Andric // Don't expect any other users than call sites 6441349cc55cSDimitry Andric cast<CallBase>(U)->setCalledFunction(I.second); 6442*0b57cec5SDimitry Andric 6443*0b57cec5SDimitry Andric // Finish fn->subprogram upgrade for materialized functions. 6444*0b57cec5SDimitry Andric if (DISubprogram *SP = MDLoader->lookupSubprogramForFunction(F)) 6445*0b57cec5SDimitry Andric F->setSubprogram(SP); 6446*0b57cec5SDimitry Andric 6447*0b57cec5SDimitry Andric // Check if the TBAA Metadata are valid, otherwise we will need to strip them. 6448*0b57cec5SDimitry Andric if (!MDLoader->isStrippingTBAA()) { 6449*0b57cec5SDimitry Andric for (auto &I : instructions(F)) { 6450*0b57cec5SDimitry Andric MDNode *TBAA = I.getMetadata(LLVMContext::MD_tbaa); 6451*0b57cec5SDimitry Andric if (!TBAA || TBAAVerifyHelper.visitTBAAMetadata(I, TBAA)) 6452*0b57cec5SDimitry Andric continue; 6453*0b57cec5SDimitry Andric MDLoader->setStripTBAA(true); 6454*0b57cec5SDimitry Andric stripTBAA(F->getParent()); 6455*0b57cec5SDimitry Andric } 6456*0b57cec5SDimitry Andric } 6457*0b57cec5SDimitry Andric 6458e8d8bef9SDimitry Andric for (auto &I : instructions(F)) { 6459fe6060f1SDimitry Andric // "Upgrade" older incorrect branch weights by dropping them. 6460e8d8bef9SDimitry Andric if (auto *MD = I.getMetadata(LLVMContext::MD_prof)) { 6461e8d8bef9SDimitry Andric if (MD->getOperand(0) != nullptr && isa<MDString>(MD->getOperand(0))) { 6462e8d8bef9SDimitry Andric MDString *MDS = cast<MDString>(MD->getOperand(0)); 6463e8d8bef9SDimitry Andric StringRef ProfName = MDS->getString(); 6464e8d8bef9SDimitry Andric // Check consistency of !prof branch_weights metadata. 6465e8d8bef9SDimitry Andric if (!ProfName.equals("branch_weights")) 6466e8d8bef9SDimitry Andric continue; 6467e8d8bef9SDimitry Andric unsigned ExpectedNumOperands = 0; 6468e8d8bef9SDimitry Andric if (BranchInst *BI = dyn_cast<BranchInst>(&I)) 6469e8d8bef9SDimitry Andric ExpectedNumOperands = BI->getNumSuccessors(); 6470e8d8bef9SDimitry Andric else if (SwitchInst *SI = dyn_cast<SwitchInst>(&I)) 6471e8d8bef9SDimitry Andric ExpectedNumOperands = SI->getNumSuccessors(); 6472e8d8bef9SDimitry Andric else if (isa<CallInst>(&I)) 6473e8d8bef9SDimitry Andric ExpectedNumOperands = 1; 6474e8d8bef9SDimitry Andric else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(&I)) 6475e8d8bef9SDimitry Andric ExpectedNumOperands = IBI->getNumDestinations(); 6476e8d8bef9SDimitry Andric else if (isa<SelectInst>(&I)) 6477e8d8bef9SDimitry Andric ExpectedNumOperands = 2; 6478e8d8bef9SDimitry Andric else 6479e8d8bef9SDimitry Andric continue; // ignore and continue. 6480e8d8bef9SDimitry Andric 6481e8d8bef9SDimitry Andric // If branch weight doesn't match, just strip branch weight. 6482e8d8bef9SDimitry Andric if (MD->getNumOperands() != 1 + ExpectedNumOperands) 6483e8d8bef9SDimitry Andric I.setMetadata(LLVMContext::MD_prof, nullptr); 6484e8d8bef9SDimitry Andric } 6485e8d8bef9SDimitry Andric } 6486fe6060f1SDimitry Andric 6487fe6060f1SDimitry Andric // Remove incompatible attributes on function calls. 6488fe6060f1SDimitry Andric if (auto *CI = dyn_cast<CallBase>(&I)) { 6489349cc55cSDimitry Andric CI->removeRetAttrs(AttributeFuncs::typeIncompatible( 6490fe6060f1SDimitry Andric CI->getFunctionType()->getReturnType())); 6491fe6060f1SDimitry Andric 6492fe6060f1SDimitry Andric for (unsigned ArgNo = 0; ArgNo < CI->arg_size(); ++ArgNo) 6493fe6060f1SDimitry Andric CI->removeParamAttrs(ArgNo, AttributeFuncs::typeIncompatible( 6494fe6060f1SDimitry Andric CI->getArgOperand(ArgNo)->getType())); 6495fe6060f1SDimitry Andric } 6496e8d8bef9SDimitry Andric } 6497e8d8bef9SDimitry Andric 64985ffd83dbSDimitry Andric // Look for functions that rely on old function attribute behavior. 64995ffd83dbSDimitry Andric UpgradeFunctionAttributes(*F); 65005ffd83dbSDimitry Andric 6501*0b57cec5SDimitry Andric // Bring in any functions that this function forward-referenced via 6502*0b57cec5SDimitry Andric // blockaddresses. 6503*0b57cec5SDimitry Andric return materializeForwardReferencedFunctions(); 6504*0b57cec5SDimitry Andric } 6505*0b57cec5SDimitry Andric 6506*0b57cec5SDimitry Andric Error BitcodeReader::materializeModule() { 6507*0b57cec5SDimitry Andric if (Error Err = materializeMetadata()) 6508*0b57cec5SDimitry Andric return Err; 6509*0b57cec5SDimitry Andric 6510*0b57cec5SDimitry Andric // Promise to materialize all forward references. 6511*0b57cec5SDimitry Andric WillMaterializeAllForwardRefs = true; 6512*0b57cec5SDimitry Andric 6513*0b57cec5SDimitry Andric // Iterate over the module, deserializing any functions that are still on 6514*0b57cec5SDimitry Andric // disk. 6515*0b57cec5SDimitry Andric for (Function &F : *TheModule) { 6516*0b57cec5SDimitry Andric if (Error Err = materialize(&F)) 6517*0b57cec5SDimitry Andric return Err; 6518*0b57cec5SDimitry Andric } 6519*0b57cec5SDimitry Andric // At this point, if there are any function bodies, parse the rest of 6520*0b57cec5SDimitry Andric // the bits in the module past the last function block we have recorded 6521*0b57cec5SDimitry Andric // through either lazy scanning or the VST. 6522*0b57cec5SDimitry Andric if (LastFunctionBlockBit || NextUnreadBit) 6523*0b57cec5SDimitry Andric if (Error Err = parseModule(LastFunctionBlockBit > NextUnreadBit 6524*0b57cec5SDimitry Andric ? LastFunctionBlockBit 6525*0b57cec5SDimitry Andric : NextUnreadBit)) 6526*0b57cec5SDimitry Andric return Err; 6527*0b57cec5SDimitry Andric 6528*0b57cec5SDimitry Andric // Check that all block address forward references got resolved (as we 6529*0b57cec5SDimitry Andric // promised above). 6530*0b57cec5SDimitry Andric if (!BasicBlockFwdRefs.empty()) 6531*0b57cec5SDimitry Andric return error("Never resolved function from blockaddress"); 6532*0b57cec5SDimitry Andric 6533*0b57cec5SDimitry Andric // Upgrade any intrinsic calls that slipped through (should not happen!) and 6534*0b57cec5SDimitry Andric // delete the old functions to clean up. We can't do this unless the entire 6535*0b57cec5SDimitry Andric // module is materialized because there could always be another function body 6536*0b57cec5SDimitry Andric // with calls to the old function. 6537*0b57cec5SDimitry Andric for (auto &I : UpgradedIntrinsics) { 6538*0b57cec5SDimitry Andric for (auto *U : I.first->users()) { 6539*0b57cec5SDimitry Andric if (CallInst *CI = dyn_cast<CallInst>(U)) 6540*0b57cec5SDimitry Andric UpgradeIntrinsicCall(CI, I.second); 6541*0b57cec5SDimitry Andric } 6542*0b57cec5SDimitry Andric if (!I.first->use_empty()) 6543*0b57cec5SDimitry Andric I.first->replaceAllUsesWith(I.second); 6544*0b57cec5SDimitry Andric I.first->eraseFromParent(); 6545*0b57cec5SDimitry Andric } 6546*0b57cec5SDimitry Andric UpgradedIntrinsics.clear(); 6547*0b57cec5SDimitry Andric // Do the same for remangled intrinsics 6548*0b57cec5SDimitry Andric for (auto &I : RemangledIntrinsics) { 6549*0b57cec5SDimitry Andric I.first->replaceAllUsesWith(I.second); 6550*0b57cec5SDimitry Andric I.first->eraseFromParent(); 6551*0b57cec5SDimitry Andric } 6552*0b57cec5SDimitry Andric RemangledIntrinsics.clear(); 6553*0b57cec5SDimitry Andric 6554*0b57cec5SDimitry Andric UpgradeDebugInfo(*TheModule); 6555*0b57cec5SDimitry Andric 6556*0b57cec5SDimitry Andric UpgradeModuleFlags(*TheModule); 6557*0b57cec5SDimitry Andric 65588bcb0991SDimitry Andric UpgradeARCRuntime(*TheModule); 6559*0b57cec5SDimitry Andric 6560*0b57cec5SDimitry Andric return Error::success(); 6561*0b57cec5SDimitry Andric } 6562*0b57cec5SDimitry Andric 6563*0b57cec5SDimitry Andric std::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const { 6564*0b57cec5SDimitry Andric return IdentifiedStructTypes; 6565*0b57cec5SDimitry Andric } 6566*0b57cec5SDimitry Andric 6567*0b57cec5SDimitry Andric ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader( 6568*0b57cec5SDimitry Andric BitstreamCursor Cursor, StringRef Strtab, ModuleSummaryIndex &TheIndex, 6569*0b57cec5SDimitry Andric StringRef ModulePath, unsigned ModuleId) 6570*0b57cec5SDimitry Andric : BitcodeReaderBase(std::move(Cursor), Strtab), TheIndex(TheIndex), 6571*0b57cec5SDimitry Andric ModulePath(ModulePath), ModuleId(ModuleId) {} 6572*0b57cec5SDimitry Andric 6573*0b57cec5SDimitry Andric void ModuleSummaryIndexBitcodeReader::addThisModule() { 6574*0b57cec5SDimitry Andric TheIndex.addModule(ModulePath, ModuleId); 6575*0b57cec5SDimitry Andric } 6576*0b57cec5SDimitry Andric 6577*0b57cec5SDimitry Andric ModuleSummaryIndex::ModuleInfo * 6578*0b57cec5SDimitry Andric ModuleSummaryIndexBitcodeReader::getThisModule() { 6579*0b57cec5SDimitry Andric return TheIndex.getModule(ModulePath); 6580*0b57cec5SDimitry Andric } 6581*0b57cec5SDimitry Andric 6582*0b57cec5SDimitry Andric std::pair<ValueInfo, GlobalValue::GUID> 6583*0b57cec5SDimitry Andric ModuleSummaryIndexBitcodeReader::getValueInfoFromValueId(unsigned ValueId) { 6584*0b57cec5SDimitry Andric auto VGI = ValueIdToValueInfoMap[ValueId]; 6585*0b57cec5SDimitry Andric assert(VGI.first); 6586*0b57cec5SDimitry Andric return VGI; 6587*0b57cec5SDimitry Andric } 6588*0b57cec5SDimitry Andric 6589*0b57cec5SDimitry Andric void ModuleSummaryIndexBitcodeReader::setValueGUID( 6590*0b57cec5SDimitry Andric uint64_t ValueID, StringRef ValueName, GlobalValue::LinkageTypes Linkage, 6591*0b57cec5SDimitry Andric StringRef SourceFileName) { 6592*0b57cec5SDimitry Andric std::string GlobalId = 6593*0b57cec5SDimitry Andric GlobalValue::getGlobalIdentifier(ValueName, Linkage, SourceFileName); 6594*0b57cec5SDimitry Andric auto ValueGUID = GlobalValue::getGUID(GlobalId); 6595*0b57cec5SDimitry Andric auto OriginalNameID = ValueGUID; 6596*0b57cec5SDimitry Andric if (GlobalValue::isLocalLinkage(Linkage)) 6597*0b57cec5SDimitry Andric OriginalNameID = GlobalValue::getGUID(ValueName); 6598*0b57cec5SDimitry Andric if (PrintSummaryGUIDs) 6599*0b57cec5SDimitry Andric dbgs() << "GUID " << ValueGUID << "(" << OriginalNameID << ") is " 6600*0b57cec5SDimitry Andric << ValueName << "\n"; 6601*0b57cec5SDimitry Andric 6602*0b57cec5SDimitry Andric // UseStrtab is false for legacy summary formats and value names are 6603*0b57cec5SDimitry Andric // created on stack. In that case we save the name in a string saver in 6604*0b57cec5SDimitry Andric // the index so that the value name can be recorded. 6605*0b57cec5SDimitry Andric ValueIdToValueInfoMap[ValueID] = std::make_pair( 6606*0b57cec5SDimitry Andric TheIndex.getOrInsertValueInfo( 6607*0b57cec5SDimitry Andric ValueGUID, 6608*0b57cec5SDimitry Andric UseStrtab ? ValueName : TheIndex.saveString(ValueName)), 6609*0b57cec5SDimitry Andric OriginalNameID); 6610*0b57cec5SDimitry Andric } 6611*0b57cec5SDimitry Andric 6612*0b57cec5SDimitry Andric // Specialized value symbol table parser used when reading module index 6613*0b57cec5SDimitry Andric // blocks where we don't actually create global values. The parsed information 6614*0b57cec5SDimitry Andric // is saved in the bitcode reader for use when later parsing summaries. 6615*0b57cec5SDimitry Andric Error ModuleSummaryIndexBitcodeReader::parseValueSymbolTable( 6616*0b57cec5SDimitry Andric uint64_t Offset, 6617*0b57cec5SDimitry Andric DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap) { 6618*0b57cec5SDimitry Andric // With a strtab the VST is not required to parse the summary. 6619*0b57cec5SDimitry Andric if (UseStrtab) 6620*0b57cec5SDimitry Andric return Error::success(); 6621*0b57cec5SDimitry Andric 6622*0b57cec5SDimitry Andric assert(Offset > 0 && "Expected non-zero VST offset"); 6623*0b57cec5SDimitry Andric Expected<uint64_t> MaybeCurrentBit = jumpToValueSymbolTable(Offset, Stream); 6624*0b57cec5SDimitry Andric if (!MaybeCurrentBit) 6625*0b57cec5SDimitry Andric return MaybeCurrentBit.takeError(); 6626*0b57cec5SDimitry Andric uint64_t CurrentBit = MaybeCurrentBit.get(); 6627*0b57cec5SDimitry Andric 6628*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) 6629*0b57cec5SDimitry Andric return Err; 6630*0b57cec5SDimitry Andric 6631*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 6632*0b57cec5SDimitry Andric 6633*0b57cec5SDimitry Andric // Read all the records for this value table. 6634*0b57cec5SDimitry Andric SmallString<128> ValueName; 6635*0b57cec5SDimitry Andric 6636*0b57cec5SDimitry Andric while (true) { 6637*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 6638*0b57cec5SDimitry Andric if (!MaybeEntry) 6639*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 6640*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 6641*0b57cec5SDimitry Andric 6642*0b57cec5SDimitry Andric switch (Entry.Kind) { 6643*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 6644*0b57cec5SDimitry Andric case BitstreamEntry::Error: 6645*0b57cec5SDimitry Andric return error("Malformed block"); 6646*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 6647*0b57cec5SDimitry Andric // Done parsing VST, jump back to wherever we came from. 6648*0b57cec5SDimitry Andric if (Error JumpFailed = Stream.JumpToBit(CurrentBit)) 6649*0b57cec5SDimitry Andric return JumpFailed; 6650*0b57cec5SDimitry Andric return Error::success(); 6651*0b57cec5SDimitry Andric case BitstreamEntry::Record: 6652*0b57cec5SDimitry Andric // The interesting case. 6653*0b57cec5SDimitry Andric break; 6654*0b57cec5SDimitry Andric } 6655*0b57cec5SDimitry Andric 6656*0b57cec5SDimitry Andric // Read a record. 6657*0b57cec5SDimitry Andric Record.clear(); 6658*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 6659*0b57cec5SDimitry Andric if (!MaybeRecord) 6660*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 6661*0b57cec5SDimitry Andric switch (MaybeRecord.get()) { 6662*0b57cec5SDimitry Andric default: // Default behavior: ignore (e.g. VST_CODE_BBENTRY records). 6663*0b57cec5SDimitry Andric break; 6664*0b57cec5SDimitry Andric case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N] 6665*0b57cec5SDimitry Andric if (convertToString(Record, 1, ValueName)) 6666*0b57cec5SDimitry Andric return error("Invalid record"); 6667*0b57cec5SDimitry Andric unsigned ValueID = Record[0]; 6668*0b57cec5SDimitry Andric assert(!SourceFileName.empty()); 6669*0b57cec5SDimitry Andric auto VLI = ValueIdToLinkageMap.find(ValueID); 6670*0b57cec5SDimitry Andric assert(VLI != ValueIdToLinkageMap.end() && 6671*0b57cec5SDimitry Andric "No linkage found for VST entry?"); 6672*0b57cec5SDimitry Andric auto Linkage = VLI->second; 6673*0b57cec5SDimitry Andric setValueGUID(ValueID, ValueName, Linkage, SourceFileName); 6674*0b57cec5SDimitry Andric ValueName.clear(); 6675*0b57cec5SDimitry Andric break; 6676*0b57cec5SDimitry Andric } 6677*0b57cec5SDimitry Andric case bitc::VST_CODE_FNENTRY: { 6678*0b57cec5SDimitry Andric // VST_CODE_FNENTRY: [valueid, offset, namechar x N] 6679*0b57cec5SDimitry Andric if (convertToString(Record, 2, ValueName)) 6680*0b57cec5SDimitry Andric return error("Invalid record"); 6681*0b57cec5SDimitry Andric unsigned ValueID = Record[0]; 6682*0b57cec5SDimitry Andric assert(!SourceFileName.empty()); 6683*0b57cec5SDimitry Andric auto VLI = ValueIdToLinkageMap.find(ValueID); 6684*0b57cec5SDimitry Andric assert(VLI != ValueIdToLinkageMap.end() && 6685*0b57cec5SDimitry Andric "No linkage found for VST entry?"); 6686*0b57cec5SDimitry Andric auto Linkage = VLI->second; 6687*0b57cec5SDimitry Andric setValueGUID(ValueID, ValueName, Linkage, SourceFileName); 6688*0b57cec5SDimitry Andric ValueName.clear(); 6689*0b57cec5SDimitry Andric break; 6690*0b57cec5SDimitry Andric } 6691*0b57cec5SDimitry Andric case bitc::VST_CODE_COMBINED_ENTRY: { 6692*0b57cec5SDimitry Andric // VST_CODE_COMBINED_ENTRY: [valueid, refguid] 6693*0b57cec5SDimitry Andric unsigned ValueID = Record[0]; 6694*0b57cec5SDimitry Andric GlobalValue::GUID RefGUID = Record[1]; 6695*0b57cec5SDimitry Andric // The "original name", which is the second value of the pair will be 6696*0b57cec5SDimitry Andric // overriden later by a FS_COMBINED_ORIGINAL_NAME in the combined index. 6697*0b57cec5SDimitry Andric ValueIdToValueInfoMap[ValueID] = 6698*0b57cec5SDimitry Andric std::make_pair(TheIndex.getOrInsertValueInfo(RefGUID), RefGUID); 6699*0b57cec5SDimitry Andric break; 6700*0b57cec5SDimitry Andric } 6701*0b57cec5SDimitry Andric } 6702*0b57cec5SDimitry Andric } 6703*0b57cec5SDimitry Andric } 6704*0b57cec5SDimitry Andric 6705*0b57cec5SDimitry Andric // Parse just the blocks needed for building the index out of the module. 6706*0b57cec5SDimitry Andric // At the end of this routine the module Index is populated with a map 6707*0b57cec5SDimitry Andric // from global value id to GlobalValueSummary objects. 6708*0b57cec5SDimitry Andric Error ModuleSummaryIndexBitcodeReader::parseModule() { 6709*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 6710*0b57cec5SDimitry Andric return Err; 6711*0b57cec5SDimitry Andric 6712*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 6713*0b57cec5SDimitry Andric DenseMap<unsigned, GlobalValue::LinkageTypes> ValueIdToLinkageMap; 6714*0b57cec5SDimitry Andric unsigned ValueId = 0; 6715*0b57cec5SDimitry Andric 6716*0b57cec5SDimitry Andric // Read the index for this module. 6717*0b57cec5SDimitry Andric while (true) { 6718*0b57cec5SDimitry Andric Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 6719*0b57cec5SDimitry Andric if (!MaybeEntry) 6720*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 6721*0b57cec5SDimitry Andric llvm::BitstreamEntry Entry = MaybeEntry.get(); 6722*0b57cec5SDimitry Andric 6723*0b57cec5SDimitry Andric switch (Entry.Kind) { 6724*0b57cec5SDimitry Andric case BitstreamEntry::Error: 6725*0b57cec5SDimitry Andric return error("Malformed block"); 6726*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 6727*0b57cec5SDimitry Andric return Error::success(); 6728*0b57cec5SDimitry Andric 6729*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: 6730*0b57cec5SDimitry Andric switch (Entry.ID) { 6731*0b57cec5SDimitry Andric default: // Skip unknown content. 6732*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 6733*0b57cec5SDimitry Andric return Err; 6734*0b57cec5SDimitry Andric break; 6735*0b57cec5SDimitry Andric case bitc::BLOCKINFO_BLOCK_ID: 6736*0b57cec5SDimitry Andric // Need to parse these to get abbrev ids (e.g. for VST) 673781ad6265SDimitry Andric if (Error Err = readBlockInfo()) 673881ad6265SDimitry Andric return Err; 6739*0b57cec5SDimitry Andric break; 6740*0b57cec5SDimitry Andric case bitc::VALUE_SYMTAB_BLOCK_ID: 6741*0b57cec5SDimitry Andric // Should have been parsed earlier via VSTOffset, unless there 6742*0b57cec5SDimitry Andric // is no summary section. 6743*0b57cec5SDimitry Andric assert(((SeenValueSymbolTable && VSTOffset > 0) || 6744*0b57cec5SDimitry Andric !SeenGlobalValSummary) && 6745*0b57cec5SDimitry Andric "Expected early VST parse via VSTOffset record"); 6746*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 6747*0b57cec5SDimitry Andric return Err; 6748*0b57cec5SDimitry Andric break; 6749*0b57cec5SDimitry Andric case bitc::GLOBALVAL_SUMMARY_BLOCK_ID: 6750*0b57cec5SDimitry Andric case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID: 6751*0b57cec5SDimitry Andric // Add the module if it is a per-module index (has a source file name). 6752*0b57cec5SDimitry Andric if (!SourceFileName.empty()) 6753*0b57cec5SDimitry Andric addThisModule(); 6754*0b57cec5SDimitry Andric assert(!SeenValueSymbolTable && 6755*0b57cec5SDimitry Andric "Already read VST when parsing summary block?"); 6756*0b57cec5SDimitry Andric // We might not have a VST if there were no values in the 6757*0b57cec5SDimitry Andric // summary. An empty summary block generated when we are 6758*0b57cec5SDimitry Andric // performing ThinLTO compiles so we don't later invoke 6759*0b57cec5SDimitry Andric // the regular LTO process on them. 6760*0b57cec5SDimitry Andric if (VSTOffset > 0) { 6761*0b57cec5SDimitry Andric if (Error Err = parseValueSymbolTable(VSTOffset, ValueIdToLinkageMap)) 6762*0b57cec5SDimitry Andric return Err; 6763*0b57cec5SDimitry Andric SeenValueSymbolTable = true; 6764*0b57cec5SDimitry Andric } 6765*0b57cec5SDimitry Andric SeenGlobalValSummary = true; 6766*0b57cec5SDimitry Andric if (Error Err = parseEntireSummary(Entry.ID)) 6767*0b57cec5SDimitry Andric return Err; 6768*0b57cec5SDimitry Andric break; 6769*0b57cec5SDimitry Andric case bitc::MODULE_STRTAB_BLOCK_ID: 6770*0b57cec5SDimitry Andric if (Error Err = parseModuleStringTable()) 6771*0b57cec5SDimitry Andric return Err; 6772*0b57cec5SDimitry Andric break; 6773*0b57cec5SDimitry Andric } 6774*0b57cec5SDimitry Andric continue; 6775*0b57cec5SDimitry Andric 6776*0b57cec5SDimitry Andric case BitstreamEntry::Record: { 6777*0b57cec5SDimitry Andric Record.clear(); 6778*0b57cec5SDimitry Andric Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); 6779*0b57cec5SDimitry Andric if (!MaybeBitCode) 6780*0b57cec5SDimitry Andric return MaybeBitCode.takeError(); 6781*0b57cec5SDimitry Andric switch (MaybeBitCode.get()) { 6782*0b57cec5SDimitry Andric default: 6783*0b57cec5SDimitry Andric break; // Default behavior, ignore unknown content. 6784*0b57cec5SDimitry Andric case bitc::MODULE_CODE_VERSION: { 6785*0b57cec5SDimitry Andric if (Error Err = parseVersionRecord(Record).takeError()) 6786*0b57cec5SDimitry Andric return Err; 6787*0b57cec5SDimitry Andric break; 6788*0b57cec5SDimitry Andric } 6789*0b57cec5SDimitry Andric /// MODULE_CODE_SOURCE_FILENAME: [namechar x N] 6790*0b57cec5SDimitry Andric case bitc::MODULE_CODE_SOURCE_FILENAME: { 6791*0b57cec5SDimitry Andric SmallString<128> ValueName; 6792*0b57cec5SDimitry Andric if (convertToString(Record, 0, ValueName)) 6793*0b57cec5SDimitry Andric return error("Invalid record"); 6794*0b57cec5SDimitry Andric SourceFileName = ValueName.c_str(); 6795*0b57cec5SDimitry Andric break; 6796*0b57cec5SDimitry Andric } 6797*0b57cec5SDimitry Andric /// MODULE_CODE_HASH: [5*i32] 6798*0b57cec5SDimitry Andric case bitc::MODULE_CODE_HASH: { 6799*0b57cec5SDimitry Andric if (Record.size() != 5) 6800*0b57cec5SDimitry Andric return error("Invalid hash length " + Twine(Record.size()).str()); 6801*0b57cec5SDimitry Andric auto &Hash = getThisModule()->second.second; 6802*0b57cec5SDimitry Andric int Pos = 0; 6803*0b57cec5SDimitry Andric for (auto &Val : Record) { 6804*0b57cec5SDimitry Andric assert(!(Val >> 32) && "Unexpected high bits set"); 6805*0b57cec5SDimitry Andric Hash[Pos++] = Val; 6806*0b57cec5SDimitry Andric } 6807*0b57cec5SDimitry Andric break; 6808*0b57cec5SDimitry Andric } 6809*0b57cec5SDimitry Andric /// MODULE_CODE_VSTOFFSET: [offset] 6810*0b57cec5SDimitry Andric case bitc::MODULE_CODE_VSTOFFSET: 6811e8d8bef9SDimitry Andric if (Record.empty()) 6812*0b57cec5SDimitry Andric return error("Invalid record"); 6813*0b57cec5SDimitry Andric // Note that we subtract 1 here because the offset is relative to one 6814*0b57cec5SDimitry Andric // word before the start of the identification or module block, which 6815*0b57cec5SDimitry Andric // was historically always the start of the regular bitcode header. 6816*0b57cec5SDimitry Andric VSTOffset = Record[0] - 1; 6817*0b57cec5SDimitry Andric break; 6818*0b57cec5SDimitry Andric // v1 GLOBALVAR: [pointer type, isconst, initid, linkage, ...] 6819*0b57cec5SDimitry Andric // v1 FUNCTION: [type, callingconv, isproto, linkage, ...] 6820*0b57cec5SDimitry Andric // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, ...] 6821*0b57cec5SDimitry Andric // v2: [strtab offset, strtab size, v1] 6822*0b57cec5SDimitry Andric case bitc::MODULE_CODE_GLOBALVAR: 6823*0b57cec5SDimitry Andric case bitc::MODULE_CODE_FUNCTION: 6824*0b57cec5SDimitry Andric case bitc::MODULE_CODE_ALIAS: { 6825*0b57cec5SDimitry Andric StringRef Name; 6826*0b57cec5SDimitry Andric ArrayRef<uint64_t> GVRecord; 6827*0b57cec5SDimitry Andric std::tie(Name, GVRecord) = readNameFromStrtab(Record); 6828*0b57cec5SDimitry Andric if (GVRecord.size() <= 3) 6829*0b57cec5SDimitry Andric return error("Invalid record"); 6830*0b57cec5SDimitry Andric uint64_t RawLinkage = GVRecord[3]; 6831*0b57cec5SDimitry Andric GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage); 6832*0b57cec5SDimitry Andric if (!UseStrtab) { 6833*0b57cec5SDimitry Andric ValueIdToLinkageMap[ValueId++] = Linkage; 6834*0b57cec5SDimitry Andric break; 6835*0b57cec5SDimitry Andric } 6836*0b57cec5SDimitry Andric 6837*0b57cec5SDimitry Andric setValueGUID(ValueId++, Name, Linkage, SourceFileName); 6838*0b57cec5SDimitry Andric break; 6839*0b57cec5SDimitry Andric } 6840*0b57cec5SDimitry Andric } 6841*0b57cec5SDimitry Andric } 6842*0b57cec5SDimitry Andric continue; 6843*0b57cec5SDimitry Andric } 6844*0b57cec5SDimitry Andric } 6845*0b57cec5SDimitry Andric } 6846*0b57cec5SDimitry Andric 6847*0b57cec5SDimitry Andric std::vector<ValueInfo> 6848*0b57cec5SDimitry Andric ModuleSummaryIndexBitcodeReader::makeRefList(ArrayRef<uint64_t> Record) { 6849*0b57cec5SDimitry Andric std::vector<ValueInfo> Ret; 6850*0b57cec5SDimitry Andric Ret.reserve(Record.size()); 6851*0b57cec5SDimitry Andric for (uint64_t RefValueId : Record) 6852*0b57cec5SDimitry Andric Ret.push_back(getValueInfoFromValueId(RefValueId).first); 6853*0b57cec5SDimitry Andric return Ret; 6854*0b57cec5SDimitry Andric } 6855*0b57cec5SDimitry Andric 6856*0b57cec5SDimitry Andric std::vector<FunctionSummary::EdgeTy> 6857*0b57cec5SDimitry Andric ModuleSummaryIndexBitcodeReader::makeCallList(ArrayRef<uint64_t> Record, 6858*0b57cec5SDimitry Andric bool IsOldProfileFormat, 6859*0b57cec5SDimitry Andric bool HasProfile, bool HasRelBF) { 6860*0b57cec5SDimitry Andric std::vector<FunctionSummary::EdgeTy> Ret; 6861*0b57cec5SDimitry Andric Ret.reserve(Record.size()); 6862*0b57cec5SDimitry Andric for (unsigned I = 0, E = Record.size(); I != E; ++I) { 6863*0b57cec5SDimitry Andric CalleeInfo::HotnessType Hotness = CalleeInfo::HotnessType::Unknown; 6864*0b57cec5SDimitry Andric uint64_t RelBF = 0; 6865*0b57cec5SDimitry Andric ValueInfo Callee = getValueInfoFromValueId(Record[I]).first; 6866*0b57cec5SDimitry Andric if (IsOldProfileFormat) { 6867*0b57cec5SDimitry Andric I += 1; // Skip old callsitecount field 6868*0b57cec5SDimitry Andric if (HasProfile) 6869*0b57cec5SDimitry Andric I += 1; // Skip old profilecount field 6870*0b57cec5SDimitry Andric } else if (HasProfile) 6871*0b57cec5SDimitry Andric Hotness = static_cast<CalleeInfo::HotnessType>(Record[++I]); 6872*0b57cec5SDimitry Andric else if (HasRelBF) 6873*0b57cec5SDimitry Andric RelBF = Record[++I]; 6874*0b57cec5SDimitry Andric Ret.push_back(FunctionSummary::EdgeTy{Callee, CalleeInfo(Hotness, RelBF)}); 6875*0b57cec5SDimitry Andric } 6876*0b57cec5SDimitry Andric return Ret; 6877*0b57cec5SDimitry Andric } 6878*0b57cec5SDimitry Andric 6879*0b57cec5SDimitry Andric static void 6880*0b57cec5SDimitry Andric parseWholeProgramDevirtResolutionByArg(ArrayRef<uint64_t> Record, size_t &Slot, 6881*0b57cec5SDimitry Andric WholeProgramDevirtResolution &Wpd) { 6882*0b57cec5SDimitry Andric uint64_t ArgNum = Record[Slot++]; 6883*0b57cec5SDimitry Andric WholeProgramDevirtResolution::ByArg &B = 6884*0b57cec5SDimitry Andric Wpd.ResByArg[{Record.begin() + Slot, Record.begin() + Slot + ArgNum}]; 6885*0b57cec5SDimitry Andric Slot += ArgNum; 6886*0b57cec5SDimitry Andric 6887*0b57cec5SDimitry Andric B.TheKind = 6888*0b57cec5SDimitry Andric static_cast<WholeProgramDevirtResolution::ByArg::Kind>(Record[Slot++]); 6889*0b57cec5SDimitry Andric B.Info = Record[Slot++]; 6890*0b57cec5SDimitry Andric B.Byte = Record[Slot++]; 6891*0b57cec5SDimitry Andric B.Bit = Record[Slot++]; 6892*0b57cec5SDimitry Andric } 6893*0b57cec5SDimitry Andric 6894*0b57cec5SDimitry Andric static void parseWholeProgramDevirtResolution(ArrayRef<uint64_t> Record, 6895*0b57cec5SDimitry Andric StringRef Strtab, size_t &Slot, 6896*0b57cec5SDimitry Andric TypeIdSummary &TypeId) { 6897*0b57cec5SDimitry Andric uint64_t Id = Record[Slot++]; 6898*0b57cec5SDimitry Andric WholeProgramDevirtResolution &Wpd = TypeId.WPDRes[Id]; 6899*0b57cec5SDimitry Andric 6900*0b57cec5SDimitry Andric Wpd.TheKind = static_cast<WholeProgramDevirtResolution::Kind>(Record[Slot++]); 6901*0b57cec5SDimitry Andric Wpd.SingleImplName = {Strtab.data() + Record[Slot], 6902*0b57cec5SDimitry Andric static_cast<size_t>(Record[Slot + 1])}; 6903*0b57cec5SDimitry Andric Slot += 2; 6904*0b57cec5SDimitry Andric 6905*0b57cec5SDimitry Andric uint64_t ResByArgNum = Record[Slot++]; 6906*0b57cec5SDimitry Andric for (uint64_t I = 0; I != ResByArgNum; ++I) 6907*0b57cec5SDimitry Andric parseWholeProgramDevirtResolutionByArg(Record, Slot, Wpd); 6908*0b57cec5SDimitry Andric } 6909*0b57cec5SDimitry Andric 6910*0b57cec5SDimitry Andric static void parseTypeIdSummaryRecord(ArrayRef<uint64_t> Record, 6911*0b57cec5SDimitry Andric StringRef Strtab, 6912*0b57cec5SDimitry Andric ModuleSummaryIndex &TheIndex) { 6913*0b57cec5SDimitry Andric size_t Slot = 0; 6914*0b57cec5SDimitry Andric TypeIdSummary &TypeId = TheIndex.getOrInsertTypeIdSummary( 6915*0b57cec5SDimitry Andric {Strtab.data() + Record[Slot], static_cast<size_t>(Record[Slot + 1])}); 6916*0b57cec5SDimitry Andric Slot += 2; 6917*0b57cec5SDimitry Andric 6918*0b57cec5SDimitry Andric TypeId.TTRes.TheKind = static_cast<TypeTestResolution::Kind>(Record[Slot++]); 6919*0b57cec5SDimitry Andric TypeId.TTRes.SizeM1BitWidth = Record[Slot++]; 6920*0b57cec5SDimitry Andric TypeId.TTRes.AlignLog2 = Record[Slot++]; 6921*0b57cec5SDimitry Andric TypeId.TTRes.SizeM1 = Record[Slot++]; 6922*0b57cec5SDimitry Andric TypeId.TTRes.BitMask = Record[Slot++]; 6923*0b57cec5SDimitry Andric TypeId.TTRes.InlineBits = Record[Slot++]; 6924*0b57cec5SDimitry Andric 6925*0b57cec5SDimitry Andric while (Slot < Record.size()) 6926*0b57cec5SDimitry Andric parseWholeProgramDevirtResolution(Record, Strtab, Slot, TypeId); 6927*0b57cec5SDimitry Andric } 6928*0b57cec5SDimitry Andric 6929e8d8bef9SDimitry Andric std::vector<FunctionSummary::ParamAccess> 6930e8d8bef9SDimitry Andric ModuleSummaryIndexBitcodeReader::parseParamAccesses(ArrayRef<uint64_t> Record) { 69315ffd83dbSDimitry Andric auto ReadRange = [&]() { 69325ffd83dbSDimitry Andric APInt Lower(FunctionSummary::ParamAccess::RangeWidth, 69335ffd83dbSDimitry Andric BitcodeReader::decodeSignRotatedValue(Record.front())); 69345ffd83dbSDimitry Andric Record = Record.drop_front(); 69355ffd83dbSDimitry Andric APInt Upper(FunctionSummary::ParamAccess::RangeWidth, 69365ffd83dbSDimitry Andric BitcodeReader::decodeSignRotatedValue(Record.front())); 69375ffd83dbSDimitry Andric Record = Record.drop_front(); 69385ffd83dbSDimitry Andric ConstantRange Range{Lower, Upper}; 69395ffd83dbSDimitry Andric assert(!Range.isFullSet()); 69405ffd83dbSDimitry Andric assert(!Range.isUpperSignWrapped()); 69415ffd83dbSDimitry Andric return Range; 69425ffd83dbSDimitry Andric }; 69435ffd83dbSDimitry Andric 69445ffd83dbSDimitry Andric std::vector<FunctionSummary::ParamAccess> PendingParamAccesses; 69455ffd83dbSDimitry Andric while (!Record.empty()) { 69465ffd83dbSDimitry Andric PendingParamAccesses.emplace_back(); 69475ffd83dbSDimitry Andric FunctionSummary::ParamAccess &ParamAccess = PendingParamAccesses.back(); 69485ffd83dbSDimitry Andric ParamAccess.ParamNo = Record.front(); 69495ffd83dbSDimitry Andric Record = Record.drop_front(); 69505ffd83dbSDimitry Andric ParamAccess.Use = ReadRange(); 69515ffd83dbSDimitry Andric ParamAccess.Calls.resize(Record.front()); 69525ffd83dbSDimitry Andric Record = Record.drop_front(); 69535ffd83dbSDimitry Andric for (auto &Call : ParamAccess.Calls) { 69545ffd83dbSDimitry Andric Call.ParamNo = Record.front(); 69555ffd83dbSDimitry Andric Record = Record.drop_front(); 6956e8d8bef9SDimitry Andric Call.Callee = getValueInfoFromValueId(Record.front()).first; 69575ffd83dbSDimitry Andric Record = Record.drop_front(); 69585ffd83dbSDimitry Andric Call.Offsets = ReadRange(); 69595ffd83dbSDimitry Andric } 69605ffd83dbSDimitry Andric } 69615ffd83dbSDimitry Andric return PendingParamAccesses; 69625ffd83dbSDimitry Andric } 69635ffd83dbSDimitry Andric 6964*0b57cec5SDimitry Andric void ModuleSummaryIndexBitcodeReader::parseTypeIdCompatibleVtableInfo( 6965*0b57cec5SDimitry Andric ArrayRef<uint64_t> Record, size_t &Slot, 6966*0b57cec5SDimitry Andric TypeIdCompatibleVtableInfo &TypeId) { 6967*0b57cec5SDimitry Andric uint64_t Offset = Record[Slot++]; 6968*0b57cec5SDimitry Andric ValueInfo Callee = getValueInfoFromValueId(Record[Slot++]).first; 6969*0b57cec5SDimitry Andric TypeId.push_back({Offset, Callee}); 6970*0b57cec5SDimitry Andric } 6971*0b57cec5SDimitry Andric 6972*0b57cec5SDimitry Andric void ModuleSummaryIndexBitcodeReader::parseTypeIdCompatibleVtableSummaryRecord( 6973*0b57cec5SDimitry Andric ArrayRef<uint64_t> Record) { 6974*0b57cec5SDimitry Andric size_t Slot = 0; 6975*0b57cec5SDimitry Andric TypeIdCompatibleVtableInfo &TypeId = 6976*0b57cec5SDimitry Andric TheIndex.getOrInsertTypeIdCompatibleVtableSummary( 6977*0b57cec5SDimitry Andric {Strtab.data() + Record[Slot], 6978*0b57cec5SDimitry Andric static_cast<size_t>(Record[Slot + 1])}); 6979*0b57cec5SDimitry Andric Slot += 2; 6980*0b57cec5SDimitry Andric 6981*0b57cec5SDimitry Andric while (Slot < Record.size()) 6982*0b57cec5SDimitry Andric parseTypeIdCompatibleVtableInfo(Record, Slot, TypeId); 6983*0b57cec5SDimitry Andric } 6984*0b57cec5SDimitry Andric 6985*0b57cec5SDimitry Andric static void setSpecialRefs(std::vector<ValueInfo> &Refs, unsigned ROCnt, 6986*0b57cec5SDimitry Andric unsigned WOCnt) { 6987*0b57cec5SDimitry Andric // Readonly and writeonly refs are in the end of the refs list. 6988*0b57cec5SDimitry Andric assert(ROCnt + WOCnt <= Refs.size()); 6989*0b57cec5SDimitry Andric unsigned FirstWORef = Refs.size() - WOCnt; 6990*0b57cec5SDimitry Andric unsigned RefNo = FirstWORef - ROCnt; 6991*0b57cec5SDimitry Andric for (; RefNo < FirstWORef; ++RefNo) 6992*0b57cec5SDimitry Andric Refs[RefNo].setReadOnly(); 6993*0b57cec5SDimitry Andric for (; RefNo < Refs.size(); ++RefNo) 6994*0b57cec5SDimitry Andric Refs[RefNo].setWriteOnly(); 6995*0b57cec5SDimitry Andric } 6996*0b57cec5SDimitry Andric 6997*0b57cec5SDimitry Andric // Eagerly parse the entire summary block. This populates the GlobalValueSummary 6998*0b57cec5SDimitry Andric // objects in the index. 6999*0b57cec5SDimitry Andric Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { 7000*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(ID)) 7001*0b57cec5SDimitry Andric return Err; 7002*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 7003*0b57cec5SDimitry Andric 7004*0b57cec5SDimitry Andric // Parse version 7005*0b57cec5SDimitry Andric { 7006*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 7007*0b57cec5SDimitry Andric if (!MaybeEntry) 7008*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 7009*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 7010*0b57cec5SDimitry Andric 7011*0b57cec5SDimitry Andric if (Entry.Kind != BitstreamEntry::Record) 7012*0b57cec5SDimitry Andric return error("Invalid Summary Block: record for version expected"); 7013*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 7014*0b57cec5SDimitry Andric if (!MaybeRecord) 7015*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 7016*0b57cec5SDimitry Andric if (MaybeRecord.get() != bitc::FS_VERSION) 7017*0b57cec5SDimitry Andric return error("Invalid Summary Block: version expected"); 7018*0b57cec5SDimitry Andric } 7019*0b57cec5SDimitry Andric const uint64_t Version = Record[0]; 7020*0b57cec5SDimitry Andric const bool IsOldProfileFormat = Version == 1; 7021480093f4SDimitry Andric if (Version < 1 || Version > ModuleSummaryIndex::BitcodeSummaryVersion) 7022*0b57cec5SDimitry Andric return error("Invalid summary version " + Twine(Version) + 7023480093f4SDimitry Andric ". Version should be in the range [1-" + 7024480093f4SDimitry Andric Twine(ModuleSummaryIndex::BitcodeSummaryVersion) + 7025480093f4SDimitry Andric "]."); 7026*0b57cec5SDimitry Andric Record.clear(); 7027*0b57cec5SDimitry Andric 7028*0b57cec5SDimitry Andric // Keep around the last seen summary to be used when we see an optional 7029*0b57cec5SDimitry Andric // "OriginalName" attachement. 7030*0b57cec5SDimitry Andric GlobalValueSummary *LastSeenSummary = nullptr; 7031*0b57cec5SDimitry Andric GlobalValue::GUID LastSeenGUID = 0; 7032*0b57cec5SDimitry Andric 7033*0b57cec5SDimitry Andric // We can expect to see any number of type ID information records before 7034*0b57cec5SDimitry Andric // each function summary records; these variables store the information 7035*0b57cec5SDimitry Andric // collected so far so that it can be used to create the summary object. 7036*0b57cec5SDimitry Andric std::vector<GlobalValue::GUID> PendingTypeTests; 7037*0b57cec5SDimitry Andric std::vector<FunctionSummary::VFuncId> PendingTypeTestAssumeVCalls, 7038*0b57cec5SDimitry Andric PendingTypeCheckedLoadVCalls; 7039*0b57cec5SDimitry Andric std::vector<FunctionSummary::ConstVCall> PendingTypeTestAssumeConstVCalls, 7040*0b57cec5SDimitry Andric PendingTypeCheckedLoadConstVCalls; 70415ffd83dbSDimitry Andric std::vector<FunctionSummary::ParamAccess> PendingParamAccesses; 7042*0b57cec5SDimitry Andric 7043*0b57cec5SDimitry Andric while (true) { 7044*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 7045*0b57cec5SDimitry Andric if (!MaybeEntry) 7046*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 7047*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 7048*0b57cec5SDimitry Andric 7049*0b57cec5SDimitry Andric switch (Entry.Kind) { 7050*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 7051*0b57cec5SDimitry Andric case BitstreamEntry::Error: 7052*0b57cec5SDimitry Andric return error("Malformed block"); 7053*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 7054*0b57cec5SDimitry Andric return Error::success(); 7055*0b57cec5SDimitry Andric case BitstreamEntry::Record: 7056*0b57cec5SDimitry Andric // The interesting case. 7057*0b57cec5SDimitry Andric break; 7058*0b57cec5SDimitry Andric } 7059*0b57cec5SDimitry Andric 7060*0b57cec5SDimitry Andric // Read a record. The record format depends on whether this 7061*0b57cec5SDimitry Andric // is a per-module index or a combined index file. In the per-module 7062*0b57cec5SDimitry Andric // case the records contain the associated value's ID for correlation 7063*0b57cec5SDimitry Andric // with VST entries. In the combined index the correlation is done 7064*0b57cec5SDimitry Andric // via the bitcode offset of the summary records (which were saved 7065*0b57cec5SDimitry Andric // in the combined index VST entries). The records also contain 7066*0b57cec5SDimitry Andric // information used for ThinLTO renaming and importing. 7067*0b57cec5SDimitry Andric Record.clear(); 7068*0b57cec5SDimitry Andric Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); 7069*0b57cec5SDimitry Andric if (!MaybeBitCode) 7070*0b57cec5SDimitry Andric return MaybeBitCode.takeError(); 7071*0b57cec5SDimitry Andric switch (unsigned BitCode = MaybeBitCode.get()) { 7072*0b57cec5SDimitry Andric default: // Default behavior: ignore. 7073*0b57cec5SDimitry Andric break; 7074*0b57cec5SDimitry Andric case bitc::FS_FLAGS: { // [flags] 70755ffd83dbSDimitry Andric TheIndex.setFlags(Record[0]); 7076*0b57cec5SDimitry Andric break; 7077*0b57cec5SDimitry Andric } 7078*0b57cec5SDimitry Andric case bitc::FS_VALUE_GUID: { // [valueid, refguid] 7079*0b57cec5SDimitry Andric uint64_t ValueID = Record[0]; 7080*0b57cec5SDimitry Andric GlobalValue::GUID RefGUID = Record[1]; 7081*0b57cec5SDimitry Andric ValueIdToValueInfoMap[ValueID] = 7082*0b57cec5SDimitry Andric std::make_pair(TheIndex.getOrInsertValueInfo(RefGUID), RefGUID); 7083*0b57cec5SDimitry Andric break; 7084*0b57cec5SDimitry Andric } 7085*0b57cec5SDimitry Andric // FS_PERMODULE: [valueid, flags, instcount, fflags, numrefs, 7086*0b57cec5SDimitry Andric // numrefs x valueid, n x (valueid)] 7087*0b57cec5SDimitry Andric // FS_PERMODULE_PROFILE: [valueid, flags, instcount, fflags, numrefs, 7088*0b57cec5SDimitry Andric // numrefs x valueid, 7089*0b57cec5SDimitry Andric // n x (valueid, hotness)] 7090*0b57cec5SDimitry Andric // FS_PERMODULE_RELBF: [valueid, flags, instcount, fflags, numrefs, 7091*0b57cec5SDimitry Andric // numrefs x valueid, 7092*0b57cec5SDimitry Andric // n x (valueid, relblockfreq)] 7093*0b57cec5SDimitry Andric case bitc::FS_PERMODULE: 7094*0b57cec5SDimitry Andric case bitc::FS_PERMODULE_RELBF: 7095*0b57cec5SDimitry Andric case bitc::FS_PERMODULE_PROFILE: { 7096*0b57cec5SDimitry Andric unsigned ValueID = Record[0]; 7097*0b57cec5SDimitry Andric uint64_t RawFlags = Record[1]; 7098*0b57cec5SDimitry Andric unsigned InstCount = Record[2]; 7099*0b57cec5SDimitry Andric uint64_t RawFunFlags = 0; 7100*0b57cec5SDimitry Andric unsigned NumRefs = Record[3]; 7101*0b57cec5SDimitry Andric unsigned NumRORefs = 0, NumWORefs = 0; 7102*0b57cec5SDimitry Andric int RefListStartIndex = 4; 7103*0b57cec5SDimitry Andric if (Version >= 4) { 7104*0b57cec5SDimitry Andric RawFunFlags = Record[3]; 7105*0b57cec5SDimitry Andric NumRefs = Record[4]; 7106*0b57cec5SDimitry Andric RefListStartIndex = 5; 7107*0b57cec5SDimitry Andric if (Version >= 5) { 7108*0b57cec5SDimitry Andric NumRORefs = Record[5]; 7109*0b57cec5SDimitry Andric RefListStartIndex = 6; 7110*0b57cec5SDimitry Andric if (Version >= 7) { 7111*0b57cec5SDimitry Andric NumWORefs = Record[6]; 7112*0b57cec5SDimitry Andric RefListStartIndex = 7; 7113*0b57cec5SDimitry Andric } 7114*0b57cec5SDimitry Andric } 7115*0b57cec5SDimitry Andric } 7116*0b57cec5SDimitry Andric 7117*0b57cec5SDimitry Andric auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 7118*0b57cec5SDimitry Andric // The module path string ref set in the summary must be owned by the 7119*0b57cec5SDimitry Andric // index's module string table. Since we don't have a module path 7120*0b57cec5SDimitry Andric // string table section in the per-module index, we create a single 7121*0b57cec5SDimitry Andric // module path string table entry with an empty (0) ID to take 7122*0b57cec5SDimitry Andric // ownership. 7123*0b57cec5SDimitry Andric int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs; 7124*0b57cec5SDimitry Andric assert(Record.size() >= RefListStartIndex + NumRefs && 7125*0b57cec5SDimitry Andric "Record size inconsistent with number of references"); 7126*0b57cec5SDimitry Andric std::vector<ValueInfo> Refs = makeRefList( 7127*0b57cec5SDimitry Andric ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs)); 7128*0b57cec5SDimitry Andric bool HasProfile = (BitCode == bitc::FS_PERMODULE_PROFILE); 7129*0b57cec5SDimitry Andric bool HasRelBF = (BitCode == bitc::FS_PERMODULE_RELBF); 7130*0b57cec5SDimitry Andric std::vector<FunctionSummary::EdgeTy> Calls = makeCallList( 7131*0b57cec5SDimitry Andric ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex), 7132*0b57cec5SDimitry Andric IsOldProfileFormat, HasProfile, HasRelBF); 7133*0b57cec5SDimitry Andric setSpecialRefs(Refs, NumRORefs, NumWORefs); 71348bcb0991SDimitry Andric auto FS = std::make_unique<FunctionSummary>( 7135*0b57cec5SDimitry Andric Flags, InstCount, getDecodedFFlags(RawFunFlags), /*EntryCount=*/0, 7136*0b57cec5SDimitry Andric std::move(Refs), std::move(Calls), std::move(PendingTypeTests), 7137*0b57cec5SDimitry Andric std::move(PendingTypeTestAssumeVCalls), 7138*0b57cec5SDimitry Andric std::move(PendingTypeCheckedLoadVCalls), 7139*0b57cec5SDimitry Andric std::move(PendingTypeTestAssumeConstVCalls), 71405ffd83dbSDimitry Andric std::move(PendingTypeCheckedLoadConstVCalls), 71415ffd83dbSDimitry Andric std::move(PendingParamAccesses)); 7142*0b57cec5SDimitry Andric auto VIAndOriginalGUID = getValueInfoFromValueId(ValueID); 7143*0b57cec5SDimitry Andric FS->setModulePath(getThisModule()->first()); 7144*0b57cec5SDimitry Andric FS->setOriginalName(VIAndOriginalGUID.second); 7145*0b57cec5SDimitry Andric TheIndex.addGlobalValueSummary(VIAndOriginalGUID.first, std::move(FS)); 7146*0b57cec5SDimitry Andric break; 7147*0b57cec5SDimitry Andric } 7148*0b57cec5SDimitry Andric // FS_ALIAS: [valueid, flags, valueid] 7149*0b57cec5SDimitry Andric // Aliases must be emitted (and parsed) after all FS_PERMODULE entries, as 7150*0b57cec5SDimitry Andric // they expect all aliasee summaries to be available. 7151*0b57cec5SDimitry Andric case bitc::FS_ALIAS: { 7152*0b57cec5SDimitry Andric unsigned ValueID = Record[0]; 7153*0b57cec5SDimitry Andric uint64_t RawFlags = Record[1]; 7154*0b57cec5SDimitry Andric unsigned AliaseeID = Record[2]; 7155*0b57cec5SDimitry Andric auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 71568bcb0991SDimitry Andric auto AS = std::make_unique<AliasSummary>(Flags); 7157*0b57cec5SDimitry Andric // The module path string ref set in the summary must be owned by the 7158*0b57cec5SDimitry Andric // index's module string table. Since we don't have a module path 7159*0b57cec5SDimitry Andric // string table section in the per-module index, we create a single 7160*0b57cec5SDimitry Andric // module path string table entry with an empty (0) ID to take 7161*0b57cec5SDimitry Andric // ownership. 7162*0b57cec5SDimitry Andric AS->setModulePath(getThisModule()->first()); 7163*0b57cec5SDimitry Andric 7164*0b57cec5SDimitry Andric auto AliaseeVI = getValueInfoFromValueId(AliaseeID).first; 7165*0b57cec5SDimitry Andric auto AliaseeInModule = TheIndex.findSummaryInModule(AliaseeVI, ModulePath); 7166*0b57cec5SDimitry Andric if (!AliaseeInModule) 7167*0b57cec5SDimitry Andric return error("Alias expects aliasee summary to be parsed"); 7168*0b57cec5SDimitry Andric AS->setAliasee(AliaseeVI, AliaseeInModule); 7169*0b57cec5SDimitry Andric 7170*0b57cec5SDimitry Andric auto GUID = getValueInfoFromValueId(ValueID); 7171*0b57cec5SDimitry Andric AS->setOriginalName(GUID.second); 7172*0b57cec5SDimitry Andric TheIndex.addGlobalValueSummary(GUID.first, std::move(AS)); 7173*0b57cec5SDimitry Andric break; 7174*0b57cec5SDimitry Andric } 7175*0b57cec5SDimitry Andric // FS_PERMODULE_GLOBALVAR_INIT_REFS: [valueid, flags, varflags, n x valueid] 7176*0b57cec5SDimitry Andric case bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS: { 7177*0b57cec5SDimitry Andric unsigned ValueID = Record[0]; 7178*0b57cec5SDimitry Andric uint64_t RawFlags = Record[1]; 7179*0b57cec5SDimitry Andric unsigned RefArrayStart = 2; 7180*0b57cec5SDimitry Andric GlobalVarSummary::GVarFlags GVF(/* ReadOnly */ false, 71815ffd83dbSDimitry Andric /* WriteOnly */ false, 71825ffd83dbSDimitry Andric /* Constant */ false, 71835ffd83dbSDimitry Andric GlobalObject::VCallVisibilityPublic); 7184*0b57cec5SDimitry Andric auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 7185*0b57cec5SDimitry Andric if (Version >= 5) { 7186*0b57cec5SDimitry Andric GVF = getDecodedGVarFlags(Record[2]); 7187*0b57cec5SDimitry Andric RefArrayStart = 3; 7188*0b57cec5SDimitry Andric } 7189*0b57cec5SDimitry Andric std::vector<ValueInfo> Refs = 7190*0b57cec5SDimitry Andric makeRefList(ArrayRef<uint64_t>(Record).slice(RefArrayStart)); 7191*0b57cec5SDimitry Andric auto FS = 71928bcb0991SDimitry Andric std::make_unique<GlobalVarSummary>(Flags, GVF, std::move(Refs)); 7193*0b57cec5SDimitry Andric FS->setModulePath(getThisModule()->first()); 7194*0b57cec5SDimitry Andric auto GUID = getValueInfoFromValueId(ValueID); 7195*0b57cec5SDimitry Andric FS->setOriginalName(GUID.second); 7196*0b57cec5SDimitry Andric TheIndex.addGlobalValueSummary(GUID.first, std::move(FS)); 7197*0b57cec5SDimitry Andric break; 7198*0b57cec5SDimitry Andric } 7199*0b57cec5SDimitry Andric // FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS: [valueid, flags, varflags, 7200*0b57cec5SDimitry Andric // numrefs, numrefs x valueid, 7201*0b57cec5SDimitry Andric // n x (valueid, offset)] 7202*0b57cec5SDimitry Andric case bitc::FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS: { 7203*0b57cec5SDimitry Andric unsigned ValueID = Record[0]; 7204*0b57cec5SDimitry Andric uint64_t RawFlags = Record[1]; 7205*0b57cec5SDimitry Andric GlobalVarSummary::GVarFlags GVF = getDecodedGVarFlags(Record[2]); 7206*0b57cec5SDimitry Andric unsigned NumRefs = Record[3]; 7207*0b57cec5SDimitry Andric unsigned RefListStartIndex = 4; 7208*0b57cec5SDimitry Andric unsigned VTableListStartIndex = RefListStartIndex + NumRefs; 7209*0b57cec5SDimitry Andric auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 7210*0b57cec5SDimitry Andric std::vector<ValueInfo> Refs = makeRefList( 7211*0b57cec5SDimitry Andric ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs)); 7212*0b57cec5SDimitry Andric VTableFuncList VTableFuncs; 7213*0b57cec5SDimitry Andric for (unsigned I = VTableListStartIndex, E = Record.size(); I != E; ++I) { 7214*0b57cec5SDimitry Andric ValueInfo Callee = getValueInfoFromValueId(Record[I]).first; 7215*0b57cec5SDimitry Andric uint64_t Offset = Record[++I]; 7216*0b57cec5SDimitry Andric VTableFuncs.push_back({Callee, Offset}); 7217*0b57cec5SDimitry Andric } 7218*0b57cec5SDimitry Andric auto VS = 72198bcb0991SDimitry Andric std::make_unique<GlobalVarSummary>(Flags, GVF, std::move(Refs)); 7220*0b57cec5SDimitry Andric VS->setModulePath(getThisModule()->first()); 7221*0b57cec5SDimitry Andric VS->setVTableFuncs(VTableFuncs); 7222*0b57cec5SDimitry Andric auto GUID = getValueInfoFromValueId(ValueID); 7223*0b57cec5SDimitry Andric VS->setOriginalName(GUID.second); 7224*0b57cec5SDimitry Andric TheIndex.addGlobalValueSummary(GUID.first, std::move(VS)); 7225*0b57cec5SDimitry Andric break; 7226*0b57cec5SDimitry Andric } 7227*0b57cec5SDimitry Andric // FS_COMBINED: [valueid, modid, flags, instcount, fflags, numrefs, 7228*0b57cec5SDimitry Andric // numrefs x valueid, n x (valueid)] 7229*0b57cec5SDimitry Andric // FS_COMBINED_PROFILE: [valueid, modid, flags, instcount, fflags, numrefs, 7230*0b57cec5SDimitry Andric // numrefs x valueid, n x (valueid, hotness)] 7231*0b57cec5SDimitry Andric case bitc::FS_COMBINED: 7232*0b57cec5SDimitry Andric case bitc::FS_COMBINED_PROFILE: { 7233*0b57cec5SDimitry Andric unsigned ValueID = Record[0]; 7234*0b57cec5SDimitry Andric uint64_t ModuleId = Record[1]; 7235*0b57cec5SDimitry Andric uint64_t RawFlags = Record[2]; 7236*0b57cec5SDimitry Andric unsigned InstCount = Record[3]; 7237*0b57cec5SDimitry Andric uint64_t RawFunFlags = 0; 7238*0b57cec5SDimitry Andric uint64_t EntryCount = 0; 7239*0b57cec5SDimitry Andric unsigned NumRefs = Record[4]; 7240*0b57cec5SDimitry Andric unsigned NumRORefs = 0, NumWORefs = 0; 7241*0b57cec5SDimitry Andric int RefListStartIndex = 5; 7242*0b57cec5SDimitry Andric 7243*0b57cec5SDimitry Andric if (Version >= 4) { 7244*0b57cec5SDimitry Andric RawFunFlags = Record[4]; 7245*0b57cec5SDimitry Andric RefListStartIndex = 6; 7246*0b57cec5SDimitry Andric size_t NumRefsIndex = 5; 7247*0b57cec5SDimitry Andric if (Version >= 5) { 7248*0b57cec5SDimitry Andric unsigned NumRORefsOffset = 1; 7249*0b57cec5SDimitry Andric RefListStartIndex = 7; 7250*0b57cec5SDimitry Andric if (Version >= 6) { 7251*0b57cec5SDimitry Andric NumRefsIndex = 6; 7252*0b57cec5SDimitry Andric EntryCount = Record[5]; 7253*0b57cec5SDimitry Andric RefListStartIndex = 8; 7254*0b57cec5SDimitry Andric if (Version >= 7) { 7255*0b57cec5SDimitry Andric RefListStartIndex = 9; 7256*0b57cec5SDimitry Andric NumWORefs = Record[8]; 7257*0b57cec5SDimitry Andric NumRORefsOffset = 2; 7258*0b57cec5SDimitry Andric } 7259*0b57cec5SDimitry Andric } 7260*0b57cec5SDimitry Andric NumRORefs = Record[RefListStartIndex - NumRORefsOffset]; 7261*0b57cec5SDimitry Andric } 7262*0b57cec5SDimitry Andric NumRefs = Record[NumRefsIndex]; 7263*0b57cec5SDimitry Andric } 7264*0b57cec5SDimitry Andric 7265*0b57cec5SDimitry Andric auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 7266*0b57cec5SDimitry Andric int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs; 7267*0b57cec5SDimitry Andric assert(Record.size() >= RefListStartIndex + NumRefs && 7268*0b57cec5SDimitry Andric "Record size inconsistent with number of references"); 7269*0b57cec5SDimitry Andric std::vector<ValueInfo> Refs = makeRefList( 7270*0b57cec5SDimitry Andric ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs)); 7271*0b57cec5SDimitry Andric bool HasProfile = (BitCode == bitc::FS_COMBINED_PROFILE); 7272*0b57cec5SDimitry Andric std::vector<FunctionSummary::EdgeTy> Edges = makeCallList( 7273*0b57cec5SDimitry Andric ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex), 7274*0b57cec5SDimitry Andric IsOldProfileFormat, HasProfile, false); 7275*0b57cec5SDimitry Andric ValueInfo VI = getValueInfoFromValueId(ValueID).first; 7276*0b57cec5SDimitry Andric setSpecialRefs(Refs, NumRORefs, NumWORefs); 72778bcb0991SDimitry Andric auto FS = std::make_unique<FunctionSummary>( 7278*0b57cec5SDimitry Andric Flags, InstCount, getDecodedFFlags(RawFunFlags), EntryCount, 7279*0b57cec5SDimitry Andric std::move(Refs), std::move(Edges), std::move(PendingTypeTests), 7280*0b57cec5SDimitry Andric std::move(PendingTypeTestAssumeVCalls), 7281*0b57cec5SDimitry Andric std::move(PendingTypeCheckedLoadVCalls), 7282*0b57cec5SDimitry Andric std::move(PendingTypeTestAssumeConstVCalls), 72835ffd83dbSDimitry Andric std::move(PendingTypeCheckedLoadConstVCalls), 72845ffd83dbSDimitry Andric std::move(PendingParamAccesses)); 7285*0b57cec5SDimitry Andric LastSeenSummary = FS.get(); 7286*0b57cec5SDimitry Andric LastSeenGUID = VI.getGUID(); 7287*0b57cec5SDimitry Andric FS->setModulePath(ModuleIdMap[ModuleId]); 7288*0b57cec5SDimitry Andric TheIndex.addGlobalValueSummary(VI, std::move(FS)); 7289*0b57cec5SDimitry Andric break; 7290*0b57cec5SDimitry Andric } 7291*0b57cec5SDimitry Andric // FS_COMBINED_ALIAS: [valueid, modid, flags, valueid] 7292*0b57cec5SDimitry Andric // Aliases must be emitted (and parsed) after all FS_COMBINED entries, as 7293*0b57cec5SDimitry Andric // they expect all aliasee summaries to be available. 7294*0b57cec5SDimitry Andric case bitc::FS_COMBINED_ALIAS: { 7295*0b57cec5SDimitry Andric unsigned ValueID = Record[0]; 7296*0b57cec5SDimitry Andric uint64_t ModuleId = Record[1]; 7297*0b57cec5SDimitry Andric uint64_t RawFlags = Record[2]; 7298*0b57cec5SDimitry Andric unsigned AliaseeValueId = Record[3]; 7299*0b57cec5SDimitry Andric auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 73008bcb0991SDimitry Andric auto AS = std::make_unique<AliasSummary>(Flags); 7301*0b57cec5SDimitry Andric LastSeenSummary = AS.get(); 7302*0b57cec5SDimitry Andric AS->setModulePath(ModuleIdMap[ModuleId]); 7303*0b57cec5SDimitry Andric 7304*0b57cec5SDimitry Andric auto AliaseeVI = getValueInfoFromValueId(AliaseeValueId).first; 7305*0b57cec5SDimitry Andric auto AliaseeInModule = TheIndex.findSummaryInModule(AliaseeVI, AS->modulePath()); 7306*0b57cec5SDimitry Andric AS->setAliasee(AliaseeVI, AliaseeInModule); 7307*0b57cec5SDimitry Andric 7308*0b57cec5SDimitry Andric ValueInfo VI = getValueInfoFromValueId(ValueID).first; 7309*0b57cec5SDimitry Andric LastSeenGUID = VI.getGUID(); 7310*0b57cec5SDimitry Andric TheIndex.addGlobalValueSummary(VI, std::move(AS)); 7311*0b57cec5SDimitry Andric break; 7312*0b57cec5SDimitry Andric } 7313*0b57cec5SDimitry Andric // FS_COMBINED_GLOBALVAR_INIT_REFS: [valueid, modid, flags, n x valueid] 7314*0b57cec5SDimitry Andric case bitc::FS_COMBINED_GLOBALVAR_INIT_REFS: { 7315*0b57cec5SDimitry Andric unsigned ValueID = Record[0]; 7316*0b57cec5SDimitry Andric uint64_t ModuleId = Record[1]; 7317*0b57cec5SDimitry Andric uint64_t RawFlags = Record[2]; 7318*0b57cec5SDimitry Andric unsigned RefArrayStart = 3; 7319*0b57cec5SDimitry Andric GlobalVarSummary::GVarFlags GVF(/* ReadOnly */ false, 73205ffd83dbSDimitry Andric /* WriteOnly */ false, 73215ffd83dbSDimitry Andric /* Constant */ false, 73225ffd83dbSDimitry Andric GlobalObject::VCallVisibilityPublic); 7323*0b57cec5SDimitry Andric auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 7324*0b57cec5SDimitry Andric if (Version >= 5) { 7325*0b57cec5SDimitry Andric GVF = getDecodedGVarFlags(Record[3]); 7326*0b57cec5SDimitry Andric RefArrayStart = 4; 7327*0b57cec5SDimitry Andric } 7328*0b57cec5SDimitry Andric std::vector<ValueInfo> Refs = 7329*0b57cec5SDimitry Andric makeRefList(ArrayRef<uint64_t>(Record).slice(RefArrayStart)); 7330*0b57cec5SDimitry Andric auto FS = 73318bcb0991SDimitry Andric std::make_unique<GlobalVarSummary>(Flags, GVF, std::move(Refs)); 7332*0b57cec5SDimitry Andric LastSeenSummary = FS.get(); 7333*0b57cec5SDimitry Andric FS->setModulePath(ModuleIdMap[ModuleId]); 7334*0b57cec5SDimitry Andric ValueInfo VI = getValueInfoFromValueId(ValueID).first; 7335*0b57cec5SDimitry Andric LastSeenGUID = VI.getGUID(); 7336*0b57cec5SDimitry Andric TheIndex.addGlobalValueSummary(VI, std::move(FS)); 7337*0b57cec5SDimitry Andric break; 7338*0b57cec5SDimitry Andric } 7339*0b57cec5SDimitry Andric // FS_COMBINED_ORIGINAL_NAME: [original_name] 7340*0b57cec5SDimitry Andric case bitc::FS_COMBINED_ORIGINAL_NAME: { 7341*0b57cec5SDimitry Andric uint64_t OriginalName = Record[0]; 7342*0b57cec5SDimitry Andric if (!LastSeenSummary) 7343*0b57cec5SDimitry Andric return error("Name attachment that does not follow a combined record"); 7344*0b57cec5SDimitry Andric LastSeenSummary->setOriginalName(OriginalName); 7345*0b57cec5SDimitry Andric TheIndex.addOriginalName(LastSeenGUID, OriginalName); 7346*0b57cec5SDimitry Andric // Reset the LastSeenSummary 7347*0b57cec5SDimitry Andric LastSeenSummary = nullptr; 7348*0b57cec5SDimitry Andric LastSeenGUID = 0; 7349*0b57cec5SDimitry Andric break; 7350*0b57cec5SDimitry Andric } 7351*0b57cec5SDimitry Andric case bitc::FS_TYPE_TESTS: 7352*0b57cec5SDimitry Andric assert(PendingTypeTests.empty()); 7353e8d8bef9SDimitry Andric llvm::append_range(PendingTypeTests, Record); 7354*0b57cec5SDimitry Andric break; 7355*0b57cec5SDimitry Andric 7356*0b57cec5SDimitry Andric case bitc::FS_TYPE_TEST_ASSUME_VCALLS: 7357*0b57cec5SDimitry Andric assert(PendingTypeTestAssumeVCalls.empty()); 7358*0b57cec5SDimitry Andric for (unsigned I = 0; I != Record.size(); I += 2) 7359*0b57cec5SDimitry Andric PendingTypeTestAssumeVCalls.push_back({Record[I], Record[I+1]}); 7360*0b57cec5SDimitry Andric break; 7361*0b57cec5SDimitry Andric 7362*0b57cec5SDimitry Andric case bitc::FS_TYPE_CHECKED_LOAD_VCALLS: 7363*0b57cec5SDimitry Andric assert(PendingTypeCheckedLoadVCalls.empty()); 7364*0b57cec5SDimitry Andric for (unsigned I = 0; I != Record.size(); I += 2) 7365*0b57cec5SDimitry Andric PendingTypeCheckedLoadVCalls.push_back({Record[I], Record[I+1]}); 7366*0b57cec5SDimitry Andric break; 7367*0b57cec5SDimitry Andric 7368*0b57cec5SDimitry Andric case bitc::FS_TYPE_TEST_ASSUME_CONST_VCALL: 7369*0b57cec5SDimitry Andric PendingTypeTestAssumeConstVCalls.push_back( 7370*0b57cec5SDimitry Andric {{Record[0], Record[1]}, {Record.begin() + 2, Record.end()}}); 7371*0b57cec5SDimitry Andric break; 7372*0b57cec5SDimitry Andric 7373*0b57cec5SDimitry Andric case bitc::FS_TYPE_CHECKED_LOAD_CONST_VCALL: 7374*0b57cec5SDimitry Andric PendingTypeCheckedLoadConstVCalls.push_back( 7375*0b57cec5SDimitry Andric {{Record[0], Record[1]}, {Record.begin() + 2, Record.end()}}); 7376*0b57cec5SDimitry Andric break; 7377*0b57cec5SDimitry Andric 7378*0b57cec5SDimitry Andric case bitc::FS_CFI_FUNCTION_DEFS: { 7379*0b57cec5SDimitry Andric std::set<std::string> &CfiFunctionDefs = TheIndex.cfiFunctionDefs(); 7380*0b57cec5SDimitry Andric for (unsigned I = 0; I != Record.size(); I += 2) 7381*0b57cec5SDimitry Andric CfiFunctionDefs.insert( 7382*0b57cec5SDimitry Andric {Strtab.data() + Record[I], static_cast<size_t>(Record[I + 1])}); 7383*0b57cec5SDimitry Andric break; 7384*0b57cec5SDimitry Andric } 7385*0b57cec5SDimitry Andric 7386*0b57cec5SDimitry Andric case bitc::FS_CFI_FUNCTION_DECLS: { 7387*0b57cec5SDimitry Andric std::set<std::string> &CfiFunctionDecls = TheIndex.cfiFunctionDecls(); 7388*0b57cec5SDimitry Andric for (unsigned I = 0; I != Record.size(); I += 2) 7389*0b57cec5SDimitry Andric CfiFunctionDecls.insert( 7390*0b57cec5SDimitry Andric {Strtab.data() + Record[I], static_cast<size_t>(Record[I + 1])}); 7391*0b57cec5SDimitry Andric break; 7392*0b57cec5SDimitry Andric } 7393*0b57cec5SDimitry Andric 7394*0b57cec5SDimitry Andric case bitc::FS_TYPE_ID: 7395*0b57cec5SDimitry Andric parseTypeIdSummaryRecord(Record, Strtab, TheIndex); 7396*0b57cec5SDimitry Andric break; 7397*0b57cec5SDimitry Andric 7398*0b57cec5SDimitry Andric case bitc::FS_TYPE_ID_METADATA: 7399*0b57cec5SDimitry Andric parseTypeIdCompatibleVtableSummaryRecord(Record); 7400*0b57cec5SDimitry Andric break; 74015ffd83dbSDimitry Andric 74025ffd83dbSDimitry Andric case bitc::FS_BLOCK_COUNT: 74035ffd83dbSDimitry Andric TheIndex.addBlockCount(Record[0]); 74045ffd83dbSDimitry Andric break; 74055ffd83dbSDimitry Andric 74065ffd83dbSDimitry Andric case bitc::FS_PARAM_ACCESS: { 74075ffd83dbSDimitry Andric PendingParamAccesses = parseParamAccesses(Record); 74085ffd83dbSDimitry Andric break; 74095ffd83dbSDimitry Andric } 7410*0b57cec5SDimitry Andric } 7411*0b57cec5SDimitry Andric } 7412*0b57cec5SDimitry Andric llvm_unreachable("Exit infinite loop"); 7413*0b57cec5SDimitry Andric } 7414*0b57cec5SDimitry Andric 7415*0b57cec5SDimitry Andric // Parse the module string table block into the Index. 7416*0b57cec5SDimitry Andric // This populates the ModulePathStringTable map in the index. 7417*0b57cec5SDimitry Andric Error ModuleSummaryIndexBitcodeReader::parseModuleStringTable() { 7418*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::MODULE_STRTAB_BLOCK_ID)) 7419*0b57cec5SDimitry Andric return Err; 7420*0b57cec5SDimitry Andric 7421*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 7422*0b57cec5SDimitry Andric 7423*0b57cec5SDimitry Andric SmallString<128> ModulePath; 7424*0b57cec5SDimitry Andric ModuleSummaryIndex::ModuleInfo *LastSeenModule = nullptr; 7425*0b57cec5SDimitry Andric 7426*0b57cec5SDimitry Andric while (true) { 7427*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 7428*0b57cec5SDimitry Andric if (!MaybeEntry) 7429*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 7430*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 7431*0b57cec5SDimitry Andric 7432*0b57cec5SDimitry Andric switch (Entry.Kind) { 7433*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 7434*0b57cec5SDimitry Andric case BitstreamEntry::Error: 7435*0b57cec5SDimitry Andric return error("Malformed block"); 7436*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 7437*0b57cec5SDimitry Andric return Error::success(); 7438*0b57cec5SDimitry Andric case BitstreamEntry::Record: 7439*0b57cec5SDimitry Andric // The interesting case. 7440*0b57cec5SDimitry Andric break; 7441*0b57cec5SDimitry Andric } 7442*0b57cec5SDimitry Andric 7443*0b57cec5SDimitry Andric Record.clear(); 7444*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 7445*0b57cec5SDimitry Andric if (!MaybeRecord) 7446*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 7447*0b57cec5SDimitry Andric switch (MaybeRecord.get()) { 7448*0b57cec5SDimitry Andric default: // Default behavior: ignore. 7449*0b57cec5SDimitry Andric break; 7450*0b57cec5SDimitry Andric case bitc::MST_CODE_ENTRY: { 7451*0b57cec5SDimitry Andric // MST_ENTRY: [modid, namechar x N] 7452*0b57cec5SDimitry Andric uint64_t ModuleId = Record[0]; 7453*0b57cec5SDimitry Andric 7454*0b57cec5SDimitry Andric if (convertToString(Record, 1, ModulePath)) 7455*0b57cec5SDimitry Andric return error("Invalid record"); 7456*0b57cec5SDimitry Andric 7457*0b57cec5SDimitry Andric LastSeenModule = TheIndex.addModule(ModulePath, ModuleId); 7458*0b57cec5SDimitry Andric ModuleIdMap[ModuleId] = LastSeenModule->first(); 7459*0b57cec5SDimitry Andric 7460*0b57cec5SDimitry Andric ModulePath.clear(); 7461*0b57cec5SDimitry Andric break; 7462*0b57cec5SDimitry Andric } 7463*0b57cec5SDimitry Andric /// MST_CODE_HASH: [5*i32] 7464*0b57cec5SDimitry Andric case bitc::MST_CODE_HASH: { 7465*0b57cec5SDimitry Andric if (Record.size() != 5) 7466*0b57cec5SDimitry Andric return error("Invalid hash length " + Twine(Record.size()).str()); 7467*0b57cec5SDimitry Andric if (!LastSeenModule) 7468*0b57cec5SDimitry Andric return error("Invalid hash that does not follow a module path"); 7469*0b57cec5SDimitry Andric int Pos = 0; 7470*0b57cec5SDimitry Andric for (auto &Val : Record) { 7471*0b57cec5SDimitry Andric assert(!(Val >> 32) && "Unexpected high bits set"); 7472*0b57cec5SDimitry Andric LastSeenModule->second.second[Pos++] = Val; 7473*0b57cec5SDimitry Andric } 7474*0b57cec5SDimitry Andric // Reset LastSeenModule to avoid overriding the hash unexpectedly. 7475*0b57cec5SDimitry Andric LastSeenModule = nullptr; 7476*0b57cec5SDimitry Andric break; 7477*0b57cec5SDimitry Andric } 7478*0b57cec5SDimitry Andric } 7479*0b57cec5SDimitry Andric } 7480*0b57cec5SDimitry Andric llvm_unreachable("Exit infinite loop"); 7481*0b57cec5SDimitry Andric } 7482*0b57cec5SDimitry Andric 7483*0b57cec5SDimitry Andric namespace { 7484*0b57cec5SDimitry Andric 7485*0b57cec5SDimitry Andric // FIXME: This class is only here to support the transition to llvm::Error. It 7486*0b57cec5SDimitry Andric // will be removed once this transition is complete. Clients should prefer to 7487*0b57cec5SDimitry Andric // deal with the Error value directly, rather than converting to error_code. 7488*0b57cec5SDimitry Andric class BitcodeErrorCategoryType : public std::error_category { 7489*0b57cec5SDimitry Andric const char *name() const noexcept override { 7490*0b57cec5SDimitry Andric return "llvm.bitcode"; 7491*0b57cec5SDimitry Andric } 7492*0b57cec5SDimitry Andric 7493*0b57cec5SDimitry Andric std::string message(int IE) const override { 7494*0b57cec5SDimitry Andric BitcodeError E = static_cast<BitcodeError>(IE); 7495*0b57cec5SDimitry Andric switch (E) { 7496*0b57cec5SDimitry Andric case BitcodeError::CorruptedBitcode: 7497*0b57cec5SDimitry Andric return "Corrupted bitcode"; 7498*0b57cec5SDimitry Andric } 7499*0b57cec5SDimitry Andric llvm_unreachable("Unknown error type!"); 7500*0b57cec5SDimitry Andric } 7501*0b57cec5SDimitry Andric }; 7502*0b57cec5SDimitry Andric 7503*0b57cec5SDimitry Andric } // end anonymous namespace 7504*0b57cec5SDimitry Andric 7505*0b57cec5SDimitry Andric const std::error_category &llvm::BitcodeErrorCategory() { 7506753f127fSDimitry Andric static BitcodeErrorCategoryType ErrorCategory; 7507753f127fSDimitry Andric return ErrorCategory; 7508*0b57cec5SDimitry Andric } 7509*0b57cec5SDimitry Andric 7510*0b57cec5SDimitry Andric static Expected<StringRef> readBlobInRecord(BitstreamCursor &Stream, 7511*0b57cec5SDimitry Andric unsigned Block, unsigned RecordID) { 7512*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(Block)) 7513*0b57cec5SDimitry Andric return std::move(Err); 7514*0b57cec5SDimitry Andric 7515*0b57cec5SDimitry Andric StringRef Strtab; 7516*0b57cec5SDimitry Andric while (true) { 7517*0b57cec5SDimitry Andric Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 7518*0b57cec5SDimitry Andric if (!MaybeEntry) 7519*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 7520*0b57cec5SDimitry Andric llvm::BitstreamEntry Entry = MaybeEntry.get(); 7521*0b57cec5SDimitry Andric 7522*0b57cec5SDimitry Andric switch (Entry.Kind) { 7523*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 7524*0b57cec5SDimitry Andric return Strtab; 7525*0b57cec5SDimitry Andric 7526*0b57cec5SDimitry Andric case BitstreamEntry::Error: 7527*0b57cec5SDimitry Andric return error("Malformed block"); 7528*0b57cec5SDimitry Andric 7529*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: 7530*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 7531*0b57cec5SDimitry Andric return std::move(Err); 7532*0b57cec5SDimitry Andric break; 7533*0b57cec5SDimitry Andric 7534*0b57cec5SDimitry Andric case BitstreamEntry::Record: 7535*0b57cec5SDimitry Andric StringRef Blob; 7536*0b57cec5SDimitry Andric SmallVector<uint64_t, 1> Record; 7537*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = 7538*0b57cec5SDimitry Andric Stream.readRecord(Entry.ID, Record, &Blob); 7539*0b57cec5SDimitry Andric if (!MaybeRecord) 7540*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 7541*0b57cec5SDimitry Andric if (MaybeRecord.get() == RecordID) 7542*0b57cec5SDimitry Andric Strtab = Blob; 7543*0b57cec5SDimitry Andric break; 7544*0b57cec5SDimitry Andric } 7545*0b57cec5SDimitry Andric } 7546*0b57cec5SDimitry Andric } 7547*0b57cec5SDimitry Andric 7548*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 7549*0b57cec5SDimitry Andric // External interface 7550*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 7551*0b57cec5SDimitry Andric 7552*0b57cec5SDimitry Andric Expected<std::vector<BitcodeModule>> 7553*0b57cec5SDimitry Andric llvm::getBitcodeModuleList(MemoryBufferRef Buffer) { 7554*0b57cec5SDimitry Andric auto FOrErr = getBitcodeFileContents(Buffer); 7555*0b57cec5SDimitry Andric if (!FOrErr) 7556*0b57cec5SDimitry Andric return FOrErr.takeError(); 7557*0b57cec5SDimitry Andric return std::move(FOrErr->Mods); 7558*0b57cec5SDimitry Andric } 7559*0b57cec5SDimitry Andric 7560*0b57cec5SDimitry Andric Expected<BitcodeFileContents> 7561*0b57cec5SDimitry Andric llvm::getBitcodeFileContents(MemoryBufferRef Buffer) { 7562*0b57cec5SDimitry Andric Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); 7563*0b57cec5SDimitry Andric if (!StreamOrErr) 7564*0b57cec5SDimitry Andric return StreamOrErr.takeError(); 7565*0b57cec5SDimitry Andric BitstreamCursor &Stream = *StreamOrErr; 7566*0b57cec5SDimitry Andric 7567*0b57cec5SDimitry Andric BitcodeFileContents F; 7568*0b57cec5SDimitry Andric while (true) { 7569*0b57cec5SDimitry Andric uint64_t BCBegin = Stream.getCurrentByteNo(); 7570*0b57cec5SDimitry Andric 7571*0b57cec5SDimitry Andric // We may be consuming bitcode from a client that leaves garbage at the end 7572*0b57cec5SDimitry Andric // of the bitcode stream (e.g. Apple's ar tool). If we are close enough to 7573*0b57cec5SDimitry Andric // the end that there cannot possibly be another module, stop looking. 7574*0b57cec5SDimitry Andric if (BCBegin + 8 >= Stream.getBitcodeBytes().size()) 7575*0b57cec5SDimitry Andric return F; 7576*0b57cec5SDimitry Andric 7577*0b57cec5SDimitry Andric Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 7578*0b57cec5SDimitry Andric if (!MaybeEntry) 7579*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 7580*0b57cec5SDimitry Andric llvm::BitstreamEntry Entry = MaybeEntry.get(); 7581*0b57cec5SDimitry Andric 7582*0b57cec5SDimitry Andric switch (Entry.Kind) { 7583*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 7584*0b57cec5SDimitry Andric case BitstreamEntry::Error: 7585*0b57cec5SDimitry Andric return error("Malformed block"); 7586*0b57cec5SDimitry Andric 7587*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: { 7588*0b57cec5SDimitry Andric uint64_t IdentificationBit = -1ull; 7589*0b57cec5SDimitry Andric if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) { 7590*0b57cec5SDimitry Andric IdentificationBit = Stream.GetCurrentBitNo() - BCBegin * 8; 7591*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 7592*0b57cec5SDimitry Andric return std::move(Err); 7593*0b57cec5SDimitry Andric 7594*0b57cec5SDimitry Andric { 7595*0b57cec5SDimitry Andric Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 7596*0b57cec5SDimitry Andric if (!MaybeEntry) 7597*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 7598*0b57cec5SDimitry Andric Entry = MaybeEntry.get(); 7599*0b57cec5SDimitry Andric } 7600*0b57cec5SDimitry Andric 7601*0b57cec5SDimitry Andric if (Entry.Kind != BitstreamEntry::SubBlock || 7602*0b57cec5SDimitry Andric Entry.ID != bitc::MODULE_BLOCK_ID) 7603*0b57cec5SDimitry Andric return error("Malformed block"); 7604*0b57cec5SDimitry Andric } 7605*0b57cec5SDimitry Andric 7606*0b57cec5SDimitry Andric if (Entry.ID == bitc::MODULE_BLOCK_ID) { 7607*0b57cec5SDimitry Andric uint64_t ModuleBit = Stream.GetCurrentBitNo() - BCBegin * 8; 7608*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 7609*0b57cec5SDimitry Andric return std::move(Err); 7610*0b57cec5SDimitry Andric 7611*0b57cec5SDimitry Andric F.Mods.push_back({Stream.getBitcodeBytes().slice( 7612*0b57cec5SDimitry Andric BCBegin, Stream.getCurrentByteNo() - BCBegin), 7613*0b57cec5SDimitry Andric Buffer.getBufferIdentifier(), IdentificationBit, 7614*0b57cec5SDimitry Andric ModuleBit}); 7615*0b57cec5SDimitry Andric continue; 7616*0b57cec5SDimitry Andric } 7617*0b57cec5SDimitry Andric 7618*0b57cec5SDimitry Andric if (Entry.ID == bitc::STRTAB_BLOCK_ID) { 7619*0b57cec5SDimitry Andric Expected<StringRef> Strtab = 7620*0b57cec5SDimitry Andric readBlobInRecord(Stream, bitc::STRTAB_BLOCK_ID, bitc::STRTAB_BLOB); 7621*0b57cec5SDimitry Andric if (!Strtab) 7622*0b57cec5SDimitry Andric return Strtab.takeError(); 7623*0b57cec5SDimitry Andric // This string table is used by every preceding bitcode module that does 7624*0b57cec5SDimitry Andric // not have its own string table. A bitcode file may have multiple 7625*0b57cec5SDimitry Andric // string tables if it was created by binary concatenation, for example 7626*0b57cec5SDimitry Andric // with "llvm-cat -b". 76270eae32dcSDimitry Andric for (BitcodeModule &I : llvm::reverse(F.Mods)) { 76280eae32dcSDimitry Andric if (!I.Strtab.empty()) 7629*0b57cec5SDimitry Andric break; 76300eae32dcSDimitry Andric I.Strtab = *Strtab; 7631*0b57cec5SDimitry Andric } 7632*0b57cec5SDimitry Andric // Similarly, the string table is used by every preceding symbol table; 7633*0b57cec5SDimitry Andric // normally there will be just one unless the bitcode file was created 7634*0b57cec5SDimitry Andric // by binary concatenation. 7635*0b57cec5SDimitry Andric if (!F.Symtab.empty() && F.StrtabForSymtab.empty()) 7636*0b57cec5SDimitry Andric F.StrtabForSymtab = *Strtab; 7637*0b57cec5SDimitry Andric continue; 7638*0b57cec5SDimitry Andric } 7639*0b57cec5SDimitry Andric 7640*0b57cec5SDimitry Andric if (Entry.ID == bitc::SYMTAB_BLOCK_ID) { 7641*0b57cec5SDimitry Andric Expected<StringRef> SymtabOrErr = 7642*0b57cec5SDimitry Andric readBlobInRecord(Stream, bitc::SYMTAB_BLOCK_ID, bitc::SYMTAB_BLOB); 7643*0b57cec5SDimitry Andric if (!SymtabOrErr) 7644*0b57cec5SDimitry Andric return SymtabOrErr.takeError(); 7645*0b57cec5SDimitry Andric 7646*0b57cec5SDimitry Andric // We can expect the bitcode file to have multiple symbol tables if it 7647*0b57cec5SDimitry Andric // was created by binary concatenation. In that case we silently 7648*0b57cec5SDimitry Andric // ignore any subsequent symbol tables, which is fine because this is a 7649*0b57cec5SDimitry Andric // low level function. The client is expected to notice that the number 7650*0b57cec5SDimitry Andric // of modules in the symbol table does not match the number of modules 7651*0b57cec5SDimitry Andric // in the input file and regenerate the symbol table. 7652*0b57cec5SDimitry Andric if (F.Symtab.empty()) 7653*0b57cec5SDimitry Andric F.Symtab = *SymtabOrErr; 7654*0b57cec5SDimitry Andric continue; 7655*0b57cec5SDimitry Andric } 7656*0b57cec5SDimitry Andric 7657*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 7658*0b57cec5SDimitry Andric return std::move(Err); 7659*0b57cec5SDimitry Andric continue; 7660*0b57cec5SDimitry Andric } 7661*0b57cec5SDimitry Andric case BitstreamEntry::Record: 7662349cc55cSDimitry Andric if (Error E = Stream.skipRecord(Entry.ID).takeError()) 7663349cc55cSDimitry Andric return std::move(E); 7664*0b57cec5SDimitry Andric continue; 7665*0b57cec5SDimitry Andric } 7666*0b57cec5SDimitry Andric } 7667*0b57cec5SDimitry Andric } 7668*0b57cec5SDimitry Andric 7669*0b57cec5SDimitry Andric /// Get a lazy one-at-time loading module from bitcode. 7670*0b57cec5SDimitry Andric /// 7671*0b57cec5SDimitry Andric /// This isn't always used in a lazy context. In particular, it's also used by 7672*0b57cec5SDimitry Andric /// \a parseModule(). If this is truly lazy, then we need to eagerly pull 7673*0b57cec5SDimitry Andric /// in forward-referenced functions from block address references. 7674*0b57cec5SDimitry Andric /// 7675*0b57cec5SDimitry Andric /// \param[in] MaterializeAll Set to \c true if we should materialize 7676*0b57cec5SDimitry Andric /// everything. 7677*0b57cec5SDimitry Andric Expected<std::unique_ptr<Module>> 7678*0b57cec5SDimitry Andric BitcodeModule::getModuleImpl(LLVMContext &Context, bool MaterializeAll, 76795ffd83dbSDimitry Andric bool ShouldLazyLoadMetadata, bool IsImporting, 76805ffd83dbSDimitry Andric DataLayoutCallbackTy DataLayoutCallback) { 7681*0b57cec5SDimitry Andric BitstreamCursor Stream(Buffer); 7682*0b57cec5SDimitry Andric 7683*0b57cec5SDimitry Andric std::string ProducerIdentification; 7684*0b57cec5SDimitry Andric if (IdentificationBit != -1ull) { 7685*0b57cec5SDimitry Andric if (Error JumpFailed = Stream.JumpToBit(IdentificationBit)) 7686*0b57cec5SDimitry Andric return std::move(JumpFailed); 7687349cc55cSDimitry Andric if (Error E = 7688349cc55cSDimitry Andric readIdentificationBlock(Stream).moveInto(ProducerIdentification)) 7689349cc55cSDimitry Andric return std::move(E); 7690*0b57cec5SDimitry Andric } 7691*0b57cec5SDimitry Andric 7692*0b57cec5SDimitry Andric if (Error JumpFailed = Stream.JumpToBit(ModuleBit)) 7693*0b57cec5SDimitry Andric return std::move(JumpFailed); 7694*0b57cec5SDimitry Andric auto *R = new BitcodeReader(std::move(Stream), Strtab, ProducerIdentification, 7695*0b57cec5SDimitry Andric Context); 7696*0b57cec5SDimitry Andric 7697*0b57cec5SDimitry Andric std::unique_ptr<Module> M = 76988bcb0991SDimitry Andric std::make_unique<Module>(ModuleIdentifier, Context); 7699*0b57cec5SDimitry Andric M->setMaterializer(R); 7700*0b57cec5SDimitry Andric 7701*0b57cec5SDimitry Andric // Delay parsing Metadata if ShouldLazyLoadMetadata is true. 77025ffd83dbSDimitry Andric if (Error Err = R->parseBitcodeInto(M.get(), ShouldLazyLoadMetadata, 77035ffd83dbSDimitry Andric IsImporting, DataLayoutCallback)) 7704*0b57cec5SDimitry Andric return std::move(Err); 7705*0b57cec5SDimitry Andric 7706*0b57cec5SDimitry Andric if (MaterializeAll) { 7707*0b57cec5SDimitry Andric // Read in the entire module, and destroy the BitcodeReader. 7708*0b57cec5SDimitry Andric if (Error Err = M->materializeAll()) 7709*0b57cec5SDimitry Andric return std::move(Err); 7710*0b57cec5SDimitry Andric } else { 7711*0b57cec5SDimitry Andric // Resolve forward references from blockaddresses. 7712*0b57cec5SDimitry Andric if (Error Err = R->materializeForwardReferencedFunctions()) 7713*0b57cec5SDimitry Andric return std::move(Err); 7714*0b57cec5SDimitry Andric } 7715*0b57cec5SDimitry Andric return std::move(M); 7716*0b57cec5SDimitry Andric } 7717*0b57cec5SDimitry Andric 7718*0b57cec5SDimitry Andric Expected<std::unique_ptr<Module>> 7719*0b57cec5SDimitry Andric BitcodeModule::getLazyModule(LLVMContext &Context, bool ShouldLazyLoadMetadata, 7720*0b57cec5SDimitry Andric bool IsImporting) { 77215ffd83dbSDimitry Andric return getModuleImpl(Context, false, ShouldLazyLoadMetadata, IsImporting, 77225ffd83dbSDimitry Andric [](StringRef) { return None; }); 7723*0b57cec5SDimitry Andric } 7724*0b57cec5SDimitry Andric 7725*0b57cec5SDimitry Andric // Parse the specified bitcode buffer and merge the index into CombinedIndex. 7726*0b57cec5SDimitry Andric // We don't use ModuleIdentifier here because the client may need to control the 7727*0b57cec5SDimitry Andric // module path used in the combined summary (e.g. when reading summaries for 7728*0b57cec5SDimitry Andric // regular LTO modules). 7729*0b57cec5SDimitry Andric Error BitcodeModule::readSummary(ModuleSummaryIndex &CombinedIndex, 7730*0b57cec5SDimitry Andric StringRef ModulePath, uint64_t ModuleId) { 7731*0b57cec5SDimitry Andric BitstreamCursor Stream(Buffer); 7732*0b57cec5SDimitry Andric if (Error JumpFailed = Stream.JumpToBit(ModuleBit)) 7733*0b57cec5SDimitry Andric return JumpFailed; 7734*0b57cec5SDimitry Andric 7735*0b57cec5SDimitry Andric ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, CombinedIndex, 7736*0b57cec5SDimitry Andric ModulePath, ModuleId); 7737*0b57cec5SDimitry Andric return R.parseModule(); 7738*0b57cec5SDimitry Andric } 7739*0b57cec5SDimitry Andric 7740*0b57cec5SDimitry Andric // Parse the specified bitcode buffer, returning the function info index. 7741*0b57cec5SDimitry Andric Expected<std::unique_ptr<ModuleSummaryIndex>> BitcodeModule::getSummary() { 7742*0b57cec5SDimitry Andric BitstreamCursor Stream(Buffer); 7743*0b57cec5SDimitry Andric if (Error JumpFailed = Stream.JumpToBit(ModuleBit)) 7744*0b57cec5SDimitry Andric return std::move(JumpFailed); 7745*0b57cec5SDimitry Andric 77468bcb0991SDimitry Andric auto Index = std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false); 7747*0b57cec5SDimitry Andric ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, *Index, 7748*0b57cec5SDimitry Andric ModuleIdentifier, 0); 7749*0b57cec5SDimitry Andric 7750*0b57cec5SDimitry Andric if (Error Err = R.parseModule()) 7751*0b57cec5SDimitry Andric return std::move(Err); 7752*0b57cec5SDimitry Andric 7753*0b57cec5SDimitry Andric return std::move(Index); 7754*0b57cec5SDimitry Andric } 7755*0b57cec5SDimitry Andric 7756*0b57cec5SDimitry Andric static Expected<bool> getEnableSplitLTOUnitFlag(BitstreamCursor &Stream, 7757*0b57cec5SDimitry Andric unsigned ID) { 7758*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(ID)) 7759*0b57cec5SDimitry Andric return std::move(Err); 7760*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 7761*0b57cec5SDimitry Andric 7762*0b57cec5SDimitry Andric while (true) { 7763349cc55cSDimitry Andric BitstreamEntry Entry; 7764349cc55cSDimitry Andric if (Error E = Stream.advanceSkippingSubblocks().moveInto(Entry)) 7765349cc55cSDimitry Andric return std::move(E); 7766*0b57cec5SDimitry Andric 7767*0b57cec5SDimitry Andric switch (Entry.Kind) { 7768*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 7769*0b57cec5SDimitry Andric case BitstreamEntry::Error: 7770*0b57cec5SDimitry Andric return error("Malformed block"); 7771*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 7772*0b57cec5SDimitry Andric // If no flags record found, conservatively return true to mimic 7773*0b57cec5SDimitry Andric // behavior before this flag was added. 7774*0b57cec5SDimitry Andric return true; 7775*0b57cec5SDimitry Andric case BitstreamEntry::Record: 7776*0b57cec5SDimitry Andric // The interesting case. 7777*0b57cec5SDimitry Andric break; 7778*0b57cec5SDimitry Andric } 7779*0b57cec5SDimitry Andric 7780*0b57cec5SDimitry Andric // Look for the FS_FLAGS record. 7781*0b57cec5SDimitry Andric Record.clear(); 7782*0b57cec5SDimitry Andric Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); 7783*0b57cec5SDimitry Andric if (!MaybeBitCode) 7784*0b57cec5SDimitry Andric return MaybeBitCode.takeError(); 7785*0b57cec5SDimitry Andric switch (MaybeBitCode.get()) { 7786*0b57cec5SDimitry Andric default: // Default behavior: ignore. 7787*0b57cec5SDimitry Andric break; 7788*0b57cec5SDimitry Andric case bitc::FS_FLAGS: { // [flags] 7789*0b57cec5SDimitry Andric uint64_t Flags = Record[0]; 7790*0b57cec5SDimitry Andric // Scan flags. 7791fe6060f1SDimitry Andric assert(Flags <= 0x7f && "Unexpected bits in flag"); 7792*0b57cec5SDimitry Andric 7793*0b57cec5SDimitry Andric return Flags & 0x8; 7794*0b57cec5SDimitry Andric } 7795*0b57cec5SDimitry Andric } 7796*0b57cec5SDimitry Andric } 7797*0b57cec5SDimitry Andric llvm_unreachable("Exit infinite loop"); 7798*0b57cec5SDimitry Andric } 7799*0b57cec5SDimitry Andric 7800*0b57cec5SDimitry Andric // Check if the given bitcode buffer contains a global value summary block. 7801*0b57cec5SDimitry Andric Expected<BitcodeLTOInfo> BitcodeModule::getLTOInfo() { 7802*0b57cec5SDimitry Andric BitstreamCursor Stream(Buffer); 7803*0b57cec5SDimitry Andric if (Error JumpFailed = Stream.JumpToBit(ModuleBit)) 7804*0b57cec5SDimitry Andric return std::move(JumpFailed); 7805*0b57cec5SDimitry Andric 7806*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 7807*0b57cec5SDimitry Andric return std::move(Err); 7808*0b57cec5SDimitry Andric 7809*0b57cec5SDimitry Andric while (true) { 7810349cc55cSDimitry Andric llvm::BitstreamEntry Entry; 7811349cc55cSDimitry Andric if (Error E = Stream.advance().moveInto(Entry)) 7812349cc55cSDimitry Andric return std::move(E); 7813*0b57cec5SDimitry Andric 7814*0b57cec5SDimitry Andric switch (Entry.Kind) { 7815*0b57cec5SDimitry Andric case BitstreamEntry::Error: 7816*0b57cec5SDimitry Andric return error("Malformed block"); 7817*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 7818*0b57cec5SDimitry Andric return BitcodeLTOInfo{/*IsThinLTO=*/false, /*HasSummary=*/false, 7819*0b57cec5SDimitry Andric /*EnableSplitLTOUnit=*/false}; 7820*0b57cec5SDimitry Andric 7821*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: 7822*0b57cec5SDimitry Andric if (Entry.ID == bitc::GLOBALVAL_SUMMARY_BLOCK_ID) { 7823*0b57cec5SDimitry Andric Expected<bool> EnableSplitLTOUnit = 7824*0b57cec5SDimitry Andric getEnableSplitLTOUnitFlag(Stream, Entry.ID); 7825*0b57cec5SDimitry Andric if (!EnableSplitLTOUnit) 7826*0b57cec5SDimitry Andric return EnableSplitLTOUnit.takeError(); 7827*0b57cec5SDimitry Andric return BitcodeLTOInfo{/*IsThinLTO=*/true, /*HasSummary=*/true, 7828*0b57cec5SDimitry Andric *EnableSplitLTOUnit}; 7829*0b57cec5SDimitry Andric } 7830*0b57cec5SDimitry Andric 7831*0b57cec5SDimitry Andric if (Entry.ID == bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID) { 7832*0b57cec5SDimitry Andric Expected<bool> EnableSplitLTOUnit = 7833*0b57cec5SDimitry Andric getEnableSplitLTOUnitFlag(Stream, Entry.ID); 7834*0b57cec5SDimitry Andric if (!EnableSplitLTOUnit) 7835*0b57cec5SDimitry Andric return EnableSplitLTOUnit.takeError(); 7836*0b57cec5SDimitry Andric return BitcodeLTOInfo{/*IsThinLTO=*/false, /*HasSummary=*/true, 7837*0b57cec5SDimitry Andric *EnableSplitLTOUnit}; 7838*0b57cec5SDimitry Andric } 7839*0b57cec5SDimitry Andric 7840*0b57cec5SDimitry Andric // Ignore other sub-blocks. 7841*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 7842*0b57cec5SDimitry Andric return std::move(Err); 7843*0b57cec5SDimitry Andric continue; 7844*0b57cec5SDimitry Andric 7845*0b57cec5SDimitry Andric case BitstreamEntry::Record: 7846*0b57cec5SDimitry Andric if (Expected<unsigned> StreamFailed = Stream.skipRecord(Entry.ID)) 7847*0b57cec5SDimitry Andric continue; 7848*0b57cec5SDimitry Andric else 7849*0b57cec5SDimitry Andric return StreamFailed.takeError(); 7850*0b57cec5SDimitry Andric } 7851*0b57cec5SDimitry Andric } 7852*0b57cec5SDimitry Andric } 7853*0b57cec5SDimitry Andric 7854*0b57cec5SDimitry Andric static Expected<BitcodeModule> getSingleModule(MemoryBufferRef Buffer) { 7855*0b57cec5SDimitry Andric Expected<std::vector<BitcodeModule>> MsOrErr = getBitcodeModuleList(Buffer); 7856*0b57cec5SDimitry Andric if (!MsOrErr) 7857*0b57cec5SDimitry Andric return MsOrErr.takeError(); 7858*0b57cec5SDimitry Andric 7859*0b57cec5SDimitry Andric if (MsOrErr->size() != 1) 7860*0b57cec5SDimitry Andric return error("Expected a single module"); 7861*0b57cec5SDimitry Andric 7862*0b57cec5SDimitry Andric return (*MsOrErr)[0]; 7863*0b57cec5SDimitry Andric } 7864*0b57cec5SDimitry Andric 7865*0b57cec5SDimitry Andric Expected<std::unique_ptr<Module>> 7866*0b57cec5SDimitry Andric llvm::getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context, 7867*0b57cec5SDimitry Andric bool ShouldLazyLoadMetadata, bool IsImporting) { 7868*0b57cec5SDimitry Andric Expected<BitcodeModule> BM = getSingleModule(Buffer); 7869*0b57cec5SDimitry Andric if (!BM) 7870*0b57cec5SDimitry Andric return BM.takeError(); 7871*0b57cec5SDimitry Andric 7872*0b57cec5SDimitry Andric return BM->getLazyModule(Context, ShouldLazyLoadMetadata, IsImporting); 7873*0b57cec5SDimitry Andric } 7874*0b57cec5SDimitry Andric 7875*0b57cec5SDimitry Andric Expected<std::unique_ptr<Module>> llvm::getOwningLazyBitcodeModule( 7876*0b57cec5SDimitry Andric std::unique_ptr<MemoryBuffer> &&Buffer, LLVMContext &Context, 7877*0b57cec5SDimitry Andric bool ShouldLazyLoadMetadata, bool IsImporting) { 7878*0b57cec5SDimitry Andric auto MOrErr = getLazyBitcodeModule(*Buffer, Context, ShouldLazyLoadMetadata, 7879*0b57cec5SDimitry Andric IsImporting); 7880*0b57cec5SDimitry Andric if (MOrErr) 7881*0b57cec5SDimitry Andric (*MOrErr)->setOwnedMemoryBuffer(std::move(Buffer)); 7882*0b57cec5SDimitry Andric return MOrErr; 7883*0b57cec5SDimitry Andric } 7884*0b57cec5SDimitry Andric 7885*0b57cec5SDimitry Andric Expected<std::unique_ptr<Module>> 78865ffd83dbSDimitry Andric BitcodeModule::parseModule(LLVMContext &Context, 78875ffd83dbSDimitry Andric DataLayoutCallbackTy DataLayoutCallback) { 78885ffd83dbSDimitry Andric return getModuleImpl(Context, true, false, false, DataLayoutCallback); 7889*0b57cec5SDimitry Andric // TODO: Restore the use-lists to the in-memory state when the bitcode was 7890*0b57cec5SDimitry Andric // written. We must defer until the Module has been fully materialized. 7891*0b57cec5SDimitry Andric } 7892*0b57cec5SDimitry Andric 78935ffd83dbSDimitry Andric Expected<std::unique_ptr<Module>> 78945ffd83dbSDimitry Andric llvm::parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context, 78955ffd83dbSDimitry Andric DataLayoutCallbackTy DataLayoutCallback) { 7896*0b57cec5SDimitry Andric Expected<BitcodeModule> BM = getSingleModule(Buffer); 7897*0b57cec5SDimitry Andric if (!BM) 7898*0b57cec5SDimitry Andric return BM.takeError(); 7899*0b57cec5SDimitry Andric 79005ffd83dbSDimitry Andric return BM->parseModule(Context, DataLayoutCallback); 7901*0b57cec5SDimitry Andric } 7902*0b57cec5SDimitry Andric 7903*0b57cec5SDimitry Andric Expected<std::string> llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer) { 7904*0b57cec5SDimitry Andric Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); 7905*0b57cec5SDimitry Andric if (!StreamOrErr) 7906*0b57cec5SDimitry Andric return StreamOrErr.takeError(); 7907*0b57cec5SDimitry Andric 7908*0b57cec5SDimitry Andric return readTriple(*StreamOrErr); 7909*0b57cec5SDimitry Andric } 7910*0b57cec5SDimitry Andric 7911*0b57cec5SDimitry Andric Expected<bool> llvm::isBitcodeContainingObjCCategory(MemoryBufferRef Buffer) { 7912*0b57cec5SDimitry Andric Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); 7913*0b57cec5SDimitry Andric if (!StreamOrErr) 7914*0b57cec5SDimitry Andric return StreamOrErr.takeError(); 7915*0b57cec5SDimitry Andric 7916*0b57cec5SDimitry Andric return hasObjCCategory(*StreamOrErr); 7917*0b57cec5SDimitry Andric } 7918*0b57cec5SDimitry Andric 7919*0b57cec5SDimitry Andric Expected<std::string> llvm::getBitcodeProducerString(MemoryBufferRef Buffer) { 7920*0b57cec5SDimitry Andric Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); 7921*0b57cec5SDimitry Andric if (!StreamOrErr) 7922*0b57cec5SDimitry Andric return StreamOrErr.takeError(); 7923*0b57cec5SDimitry Andric 7924*0b57cec5SDimitry Andric return readIdentificationCode(*StreamOrErr); 7925*0b57cec5SDimitry Andric } 7926*0b57cec5SDimitry Andric 7927*0b57cec5SDimitry Andric Error llvm::readModuleSummaryIndex(MemoryBufferRef Buffer, 7928*0b57cec5SDimitry Andric ModuleSummaryIndex &CombinedIndex, 7929*0b57cec5SDimitry Andric uint64_t ModuleId) { 7930*0b57cec5SDimitry Andric Expected<BitcodeModule> BM = getSingleModule(Buffer); 7931*0b57cec5SDimitry Andric if (!BM) 7932*0b57cec5SDimitry Andric return BM.takeError(); 7933*0b57cec5SDimitry Andric 7934*0b57cec5SDimitry Andric return BM->readSummary(CombinedIndex, BM->getModuleIdentifier(), ModuleId); 7935*0b57cec5SDimitry Andric } 7936*0b57cec5SDimitry Andric 7937*0b57cec5SDimitry Andric Expected<std::unique_ptr<ModuleSummaryIndex>> 7938*0b57cec5SDimitry Andric llvm::getModuleSummaryIndex(MemoryBufferRef Buffer) { 7939*0b57cec5SDimitry Andric Expected<BitcodeModule> BM = getSingleModule(Buffer); 7940*0b57cec5SDimitry Andric if (!BM) 7941*0b57cec5SDimitry Andric return BM.takeError(); 7942*0b57cec5SDimitry Andric 7943*0b57cec5SDimitry Andric return BM->getSummary(); 7944*0b57cec5SDimitry Andric } 7945*0b57cec5SDimitry Andric 7946*0b57cec5SDimitry Andric Expected<BitcodeLTOInfo> llvm::getBitcodeLTOInfo(MemoryBufferRef Buffer) { 7947*0b57cec5SDimitry Andric Expected<BitcodeModule> BM = getSingleModule(Buffer); 7948*0b57cec5SDimitry Andric if (!BM) 7949*0b57cec5SDimitry Andric return BM.takeError(); 7950*0b57cec5SDimitry Andric 7951*0b57cec5SDimitry Andric return BM->getLTOInfo(); 7952*0b57cec5SDimitry Andric } 7953*0b57cec5SDimitry Andric 7954*0b57cec5SDimitry Andric Expected<std::unique_ptr<ModuleSummaryIndex>> 7955*0b57cec5SDimitry Andric llvm::getModuleSummaryIndexForFile(StringRef Path, 7956*0b57cec5SDimitry Andric bool IgnoreEmptyThinLTOIndexFile) { 7957*0b57cec5SDimitry Andric ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = 7958*0b57cec5SDimitry Andric MemoryBuffer::getFileOrSTDIN(Path); 7959*0b57cec5SDimitry Andric if (!FileOrErr) 7960*0b57cec5SDimitry Andric return errorCodeToError(FileOrErr.getError()); 7961*0b57cec5SDimitry Andric if (IgnoreEmptyThinLTOIndexFile && !(*FileOrErr)->getBufferSize()) 7962*0b57cec5SDimitry Andric return nullptr; 7963*0b57cec5SDimitry Andric return getModuleSummaryIndex(**FileOrErr); 7964*0b57cec5SDimitry Andric } 7965