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/STLExtras.h" 17*0b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h" 18*0b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 19*0b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 20*0b57cec5SDimitry Andric #include "llvm/ADT/Triple.h" 21*0b57cec5SDimitry Andric #include "llvm/ADT/Twine.h" 22e8d8bef9SDimitry Andric #include "llvm/Bitcode/BitcodeCommon.h" 23*0b57cec5SDimitry Andric #include "llvm/Bitcode/LLVMBitCodes.h" 24e8d8bef9SDimitry Andric #include "llvm/Bitstream/BitstreamReader.h" 25*0b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h" 26*0b57cec5SDimitry Andric #include "llvm/IR/Argument.h" 27*0b57cec5SDimitry Andric #include "llvm/IR/Attributes.h" 28*0b57cec5SDimitry Andric #include "llvm/IR/AutoUpgrade.h" 29*0b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h" 30*0b57cec5SDimitry Andric #include "llvm/IR/CallingConv.h" 31*0b57cec5SDimitry Andric #include "llvm/IR/Comdat.h" 32*0b57cec5SDimitry Andric #include "llvm/IR/Constant.h" 33*0b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 34*0b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 35*0b57cec5SDimitry Andric #include "llvm/IR/DebugInfo.h" 36*0b57cec5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h" 37*0b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h" 38*0b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h" 39*0b57cec5SDimitry Andric #include "llvm/IR/Function.h" 40*0b57cec5SDimitry Andric #include "llvm/IR/GVMaterializer.h" 4181ad6265SDimitry Andric #include "llvm/IR/GetElementPtrTypeIterator.h" 42*0b57cec5SDimitry Andric #include "llvm/IR/GlobalAlias.h" 43*0b57cec5SDimitry Andric #include "llvm/IR/GlobalIFunc.h" 44*0b57cec5SDimitry Andric #include "llvm/IR/GlobalObject.h" 45*0b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h" 46*0b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h" 47*0b57cec5SDimitry Andric #include "llvm/IR/InlineAsm.h" 48*0b57cec5SDimitry Andric #include "llvm/IR/InstIterator.h" 49*0b57cec5SDimitry Andric #include "llvm/IR/InstrTypes.h" 50*0b57cec5SDimitry Andric #include "llvm/IR/Instruction.h" 51*0b57cec5SDimitry Andric #include "llvm/IR/Instructions.h" 52*0b57cec5SDimitry Andric #include "llvm/IR/Intrinsics.h" 5381ad6265SDimitry Andric #include "llvm/IR/IntrinsicsAArch64.h" 5481ad6265SDimitry Andric #include "llvm/IR/IntrinsicsARM.h" 55*0b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h" 56*0b57cec5SDimitry Andric #include "llvm/IR/Metadata.h" 57*0b57cec5SDimitry Andric #include "llvm/IR/Module.h" 58*0b57cec5SDimitry Andric #include "llvm/IR/ModuleSummaryIndex.h" 59*0b57cec5SDimitry Andric #include "llvm/IR/Operator.h" 60*0b57cec5SDimitry Andric #include "llvm/IR/Type.h" 61*0b57cec5SDimitry Andric #include "llvm/IR/Value.h" 62*0b57cec5SDimitry Andric #include "llvm/IR/Verifier.h" 63*0b57cec5SDimitry Andric #include "llvm/Support/AtomicOrdering.h" 64*0b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 65*0b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 66*0b57cec5SDimitry Andric #include "llvm/Support/Compiler.h" 67*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 68*0b57cec5SDimitry Andric #include "llvm/Support/Error.h" 69*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 70*0b57cec5SDimitry Andric #include "llvm/Support/ErrorOr.h" 71*0b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h" 72*0b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h" 73bdd1243dSDimitry Andric #include "llvm/Support/ModRef.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> 82bdd1243dSDimitry Andric #include <optional> 83*0b57cec5SDimitry Andric #include <set> 84*0b57cec5SDimitry Andric #include <string> 85*0b57cec5SDimitry Andric #include <system_error> 86*0b57cec5SDimitry Andric #include <tuple> 87*0b57cec5SDimitry Andric #include <utility> 88*0b57cec5SDimitry Andric #include <vector> 89*0b57cec5SDimitry Andric 90*0b57cec5SDimitry Andric using namespace llvm; 91*0b57cec5SDimitry Andric 92*0b57cec5SDimitry Andric static cl::opt<bool> PrintSummaryGUIDs( 93*0b57cec5SDimitry Andric "print-summary-global-ids", cl::init(false), cl::Hidden, 94*0b57cec5SDimitry Andric cl::desc( 95*0b57cec5SDimitry Andric "Print the global id for each value when reading the module summary")); 96*0b57cec5SDimitry Andric 9781ad6265SDimitry Andric static cl::opt<bool> ExpandConstantExprs( 9881ad6265SDimitry Andric "expand-constant-exprs", cl::Hidden, 9981ad6265SDimitry Andric cl::desc( 10081ad6265SDimitry Andric "Expand constant expressions to instructions for testing purposes")); 10181ad6265SDimitry Andric 102*0b57cec5SDimitry Andric namespace { 103*0b57cec5SDimitry Andric 104*0b57cec5SDimitry Andric enum { 105*0b57cec5SDimitry Andric SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex 106*0b57cec5SDimitry Andric }; 107*0b57cec5SDimitry Andric 108*0b57cec5SDimitry Andric } // end anonymous namespace 109*0b57cec5SDimitry Andric 110*0b57cec5SDimitry Andric static Error error(const Twine &Message) { 111*0b57cec5SDimitry Andric return make_error<StringError>( 112*0b57cec5SDimitry Andric Message, make_error_code(BitcodeError::CorruptedBitcode)); 113*0b57cec5SDimitry Andric } 114*0b57cec5SDimitry Andric 115*0b57cec5SDimitry Andric static Error hasInvalidBitcodeHeader(BitstreamCursor &Stream) { 116*0b57cec5SDimitry Andric if (!Stream.canSkipToPos(4)) 117*0b57cec5SDimitry Andric return createStringError(std::errc::illegal_byte_sequence, 118*0b57cec5SDimitry Andric "file too small to contain bitcode header"); 119*0b57cec5SDimitry Andric for (unsigned C : {'B', 'C'}) 120*0b57cec5SDimitry Andric if (Expected<SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) { 121*0b57cec5SDimitry Andric if (Res.get() != C) 122*0b57cec5SDimitry Andric return createStringError(std::errc::illegal_byte_sequence, 123*0b57cec5SDimitry Andric "file doesn't start with bitcode header"); 124*0b57cec5SDimitry Andric } else 125*0b57cec5SDimitry Andric return Res.takeError(); 126*0b57cec5SDimitry Andric for (unsigned C : {0x0, 0xC, 0xE, 0xD}) 127*0b57cec5SDimitry Andric if (Expected<SimpleBitstreamCursor::word_t> Res = Stream.Read(4)) { 128*0b57cec5SDimitry Andric if (Res.get() != C) 129*0b57cec5SDimitry Andric return createStringError(std::errc::illegal_byte_sequence, 130*0b57cec5SDimitry Andric "file doesn't start with bitcode header"); 131*0b57cec5SDimitry Andric } else 132*0b57cec5SDimitry Andric return Res.takeError(); 133*0b57cec5SDimitry Andric return Error::success(); 134*0b57cec5SDimitry Andric } 135*0b57cec5SDimitry Andric 136*0b57cec5SDimitry Andric static Expected<BitstreamCursor> initStream(MemoryBufferRef Buffer) { 137*0b57cec5SDimitry Andric const unsigned char *BufPtr = (const unsigned char *)Buffer.getBufferStart(); 138*0b57cec5SDimitry Andric const unsigned char *BufEnd = BufPtr + Buffer.getBufferSize(); 139*0b57cec5SDimitry Andric 140*0b57cec5SDimitry Andric if (Buffer.getBufferSize() & 3) 141*0b57cec5SDimitry Andric return error("Invalid bitcode signature"); 142*0b57cec5SDimitry Andric 143*0b57cec5SDimitry Andric // If we have a wrapper header, parse it and ignore the non-bc file contents. 144*0b57cec5SDimitry Andric // The magic number is 0x0B17C0DE stored in little endian. 145*0b57cec5SDimitry Andric if (isBitcodeWrapper(BufPtr, BufEnd)) 146*0b57cec5SDimitry Andric if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true)) 147*0b57cec5SDimitry Andric return error("Invalid bitcode wrapper header"); 148*0b57cec5SDimitry Andric 149*0b57cec5SDimitry Andric BitstreamCursor Stream(ArrayRef<uint8_t>(BufPtr, BufEnd)); 150*0b57cec5SDimitry Andric if (Error Err = hasInvalidBitcodeHeader(Stream)) 151*0b57cec5SDimitry Andric return std::move(Err); 152*0b57cec5SDimitry Andric 153*0b57cec5SDimitry Andric return std::move(Stream); 154*0b57cec5SDimitry Andric } 155*0b57cec5SDimitry Andric 156*0b57cec5SDimitry Andric /// Convert a string from a record into an std::string, return true on failure. 157*0b57cec5SDimitry Andric template <typename StrTy> 158*0b57cec5SDimitry Andric static bool convertToString(ArrayRef<uint64_t> Record, unsigned Idx, 159*0b57cec5SDimitry Andric StrTy &Result) { 160*0b57cec5SDimitry Andric if (Idx > Record.size()) 161*0b57cec5SDimitry Andric return true; 162*0b57cec5SDimitry Andric 1635ffd83dbSDimitry Andric Result.append(Record.begin() + Idx, Record.end()); 164*0b57cec5SDimitry Andric return false; 165*0b57cec5SDimitry Andric } 166*0b57cec5SDimitry Andric 167*0b57cec5SDimitry Andric // Strip all the TBAA attachment for the module. 168*0b57cec5SDimitry Andric static void stripTBAA(Module *M) { 169*0b57cec5SDimitry Andric for (auto &F : *M) { 170*0b57cec5SDimitry Andric if (F.isMaterializable()) 171*0b57cec5SDimitry Andric continue; 172*0b57cec5SDimitry Andric for (auto &I : instructions(F)) 173*0b57cec5SDimitry Andric I.setMetadata(LLVMContext::MD_tbaa, nullptr); 174*0b57cec5SDimitry Andric } 175*0b57cec5SDimitry Andric } 176*0b57cec5SDimitry Andric 177*0b57cec5SDimitry Andric /// Read the "IDENTIFICATION_BLOCK_ID" block, do some basic enforcement on the 178*0b57cec5SDimitry Andric /// "epoch" encoded in the bitcode, and return the producer name if any. 179*0b57cec5SDimitry Andric static Expected<std::string> readIdentificationBlock(BitstreamCursor &Stream) { 180*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::IDENTIFICATION_BLOCK_ID)) 181*0b57cec5SDimitry Andric return std::move(Err); 182*0b57cec5SDimitry Andric 183*0b57cec5SDimitry Andric // Read all the records. 184*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 185*0b57cec5SDimitry Andric 186*0b57cec5SDimitry Andric std::string ProducerIdentification; 187*0b57cec5SDimitry Andric 188*0b57cec5SDimitry Andric while (true) { 189*0b57cec5SDimitry Andric BitstreamEntry Entry; 190349cc55cSDimitry Andric if (Error E = Stream.advance().moveInto(Entry)) 191349cc55cSDimitry Andric return std::move(E); 192*0b57cec5SDimitry Andric 193*0b57cec5SDimitry Andric switch (Entry.Kind) { 194*0b57cec5SDimitry Andric default: 195*0b57cec5SDimitry Andric case BitstreamEntry::Error: 196*0b57cec5SDimitry Andric return error("Malformed block"); 197*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 198*0b57cec5SDimitry Andric return ProducerIdentification; 199*0b57cec5SDimitry Andric case BitstreamEntry::Record: 200*0b57cec5SDimitry Andric // The interesting case. 201*0b57cec5SDimitry Andric break; 202*0b57cec5SDimitry Andric } 203*0b57cec5SDimitry Andric 204*0b57cec5SDimitry Andric // Read a record. 205*0b57cec5SDimitry Andric Record.clear(); 206*0b57cec5SDimitry Andric Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); 207*0b57cec5SDimitry Andric if (!MaybeBitCode) 208*0b57cec5SDimitry Andric return MaybeBitCode.takeError(); 209*0b57cec5SDimitry Andric switch (MaybeBitCode.get()) { 210*0b57cec5SDimitry Andric default: // Default behavior: reject 211*0b57cec5SDimitry Andric return error("Invalid value"); 212*0b57cec5SDimitry Andric case bitc::IDENTIFICATION_CODE_STRING: // IDENTIFICATION: [strchr x N] 213*0b57cec5SDimitry Andric convertToString(Record, 0, ProducerIdentification); 214*0b57cec5SDimitry Andric break; 215*0b57cec5SDimitry Andric case bitc::IDENTIFICATION_CODE_EPOCH: { // EPOCH: [epoch#] 216*0b57cec5SDimitry Andric unsigned epoch = (unsigned)Record[0]; 217*0b57cec5SDimitry Andric if (epoch != bitc::BITCODE_CURRENT_EPOCH) { 218*0b57cec5SDimitry Andric return error( 219*0b57cec5SDimitry Andric Twine("Incompatible epoch: Bitcode '") + Twine(epoch) + 220*0b57cec5SDimitry Andric "' vs current: '" + Twine(bitc::BITCODE_CURRENT_EPOCH) + "'"); 221*0b57cec5SDimitry Andric } 222*0b57cec5SDimitry Andric } 223*0b57cec5SDimitry Andric } 224*0b57cec5SDimitry Andric } 225*0b57cec5SDimitry Andric } 226*0b57cec5SDimitry Andric 227*0b57cec5SDimitry Andric static Expected<std::string> readIdentificationCode(BitstreamCursor &Stream) { 228*0b57cec5SDimitry Andric // We expect a number of well-defined blocks, though we don't necessarily 229*0b57cec5SDimitry Andric // need to understand them all. 230*0b57cec5SDimitry Andric while (true) { 231*0b57cec5SDimitry Andric if (Stream.AtEndOfStream()) 232*0b57cec5SDimitry Andric return ""; 233*0b57cec5SDimitry Andric 234*0b57cec5SDimitry Andric BitstreamEntry Entry; 235349cc55cSDimitry Andric if (Error E = Stream.advance().moveInto(Entry)) 236349cc55cSDimitry Andric return std::move(E); 237*0b57cec5SDimitry Andric 238*0b57cec5SDimitry Andric switch (Entry.Kind) { 239*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 240*0b57cec5SDimitry Andric case BitstreamEntry::Error: 241*0b57cec5SDimitry Andric return error("Malformed block"); 242*0b57cec5SDimitry Andric 243*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: 244*0b57cec5SDimitry Andric if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) 245*0b57cec5SDimitry Andric return readIdentificationBlock(Stream); 246*0b57cec5SDimitry Andric 247*0b57cec5SDimitry Andric // Ignore other sub-blocks. 248*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 249*0b57cec5SDimitry Andric return std::move(Err); 250*0b57cec5SDimitry Andric continue; 251*0b57cec5SDimitry Andric case BitstreamEntry::Record: 252349cc55cSDimitry Andric if (Error E = Stream.skipRecord(Entry.ID).takeError()) 253349cc55cSDimitry Andric return std::move(E); 254*0b57cec5SDimitry Andric continue; 255*0b57cec5SDimitry Andric } 256*0b57cec5SDimitry Andric } 257*0b57cec5SDimitry Andric } 258*0b57cec5SDimitry Andric 259*0b57cec5SDimitry Andric static Expected<bool> hasObjCCategoryInModule(BitstreamCursor &Stream) { 260*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 261*0b57cec5SDimitry Andric return std::move(Err); 262*0b57cec5SDimitry Andric 263*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 264*0b57cec5SDimitry Andric // Read all the records for this module. 265*0b57cec5SDimitry Andric 266*0b57cec5SDimitry Andric while (true) { 267*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 268*0b57cec5SDimitry Andric if (!MaybeEntry) 269*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 270*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 271*0b57cec5SDimitry Andric 272*0b57cec5SDimitry Andric switch (Entry.Kind) { 273*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 274*0b57cec5SDimitry Andric case BitstreamEntry::Error: 275*0b57cec5SDimitry Andric return error("Malformed block"); 276*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 277*0b57cec5SDimitry Andric return false; 278*0b57cec5SDimitry Andric case BitstreamEntry::Record: 279*0b57cec5SDimitry Andric // The interesting case. 280*0b57cec5SDimitry Andric break; 281*0b57cec5SDimitry Andric } 282*0b57cec5SDimitry Andric 283*0b57cec5SDimitry Andric // Read a record. 284*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 285*0b57cec5SDimitry Andric if (!MaybeRecord) 286*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 287*0b57cec5SDimitry Andric switch (MaybeRecord.get()) { 288*0b57cec5SDimitry Andric default: 289*0b57cec5SDimitry Andric break; // Default behavior, ignore unknown content. 290*0b57cec5SDimitry Andric case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] 291*0b57cec5SDimitry Andric std::string S; 292*0b57cec5SDimitry Andric if (convertToString(Record, 0, S)) 29381ad6265SDimitry Andric return error("Invalid section name record"); 294*0b57cec5SDimitry Andric // Check for the i386 and other (x86_64, ARM) conventions 295*0b57cec5SDimitry Andric if (S.find("__DATA,__objc_catlist") != std::string::npos || 296*0b57cec5SDimitry Andric S.find("__OBJC,__category") != std::string::npos) 297*0b57cec5SDimitry Andric return true; 298*0b57cec5SDimitry Andric break; 299*0b57cec5SDimitry Andric } 300*0b57cec5SDimitry Andric } 301*0b57cec5SDimitry Andric Record.clear(); 302*0b57cec5SDimitry Andric } 303*0b57cec5SDimitry Andric llvm_unreachable("Exit infinite loop"); 304*0b57cec5SDimitry Andric } 305*0b57cec5SDimitry Andric 306*0b57cec5SDimitry Andric static Expected<bool> hasObjCCategory(BitstreamCursor &Stream) { 307*0b57cec5SDimitry Andric // We expect a number of well-defined blocks, though we don't necessarily 308*0b57cec5SDimitry Andric // need to understand them all. 309*0b57cec5SDimitry Andric while (true) { 310*0b57cec5SDimitry Andric BitstreamEntry Entry; 311349cc55cSDimitry Andric if (Error E = Stream.advance().moveInto(Entry)) 312349cc55cSDimitry Andric return std::move(E); 313*0b57cec5SDimitry Andric 314*0b57cec5SDimitry Andric switch (Entry.Kind) { 315*0b57cec5SDimitry Andric case BitstreamEntry::Error: 316*0b57cec5SDimitry Andric return error("Malformed block"); 317*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 318*0b57cec5SDimitry Andric return false; 319*0b57cec5SDimitry Andric 320*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: 321*0b57cec5SDimitry Andric if (Entry.ID == bitc::MODULE_BLOCK_ID) 322*0b57cec5SDimitry Andric return hasObjCCategoryInModule(Stream); 323*0b57cec5SDimitry Andric 324*0b57cec5SDimitry Andric // Ignore other sub-blocks. 325*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 326*0b57cec5SDimitry Andric return std::move(Err); 327*0b57cec5SDimitry Andric continue; 328*0b57cec5SDimitry Andric 329*0b57cec5SDimitry Andric case BitstreamEntry::Record: 330349cc55cSDimitry Andric if (Error E = Stream.skipRecord(Entry.ID).takeError()) 331349cc55cSDimitry Andric return std::move(E); 332*0b57cec5SDimitry Andric continue; 333*0b57cec5SDimitry Andric } 334*0b57cec5SDimitry Andric } 335*0b57cec5SDimitry Andric } 336*0b57cec5SDimitry Andric 337*0b57cec5SDimitry Andric static Expected<std::string> readModuleTriple(BitstreamCursor &Stream) { 338*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 339*0b57cec5SDimitry Andric return std::move(Err); 340*0b57cec5SDimitry Andric 341*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 342*0b57cec5SDimitry Andric 343*0b57cec5SDimitry Andric std::string Triple; 344*0b57cec5SDimitry Andric 345*0b57cec5SDimitry Andric // Read all the records for this module. 346*0b57cec5SDimitry Andric while (true) { 347*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 348*0b57cec5SDimitry Andric if (!MaybeEntry) 349*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 350*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 351*0b57cec5SDimitry Andric 352*0b57cec5SDimitry Andric switch (Entry.Kind) { 353*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 354*0b57cec5SDimitry Andric case BitstreamEntry::Error: 355*0b57cec5SDimitry Andric return error("Malformed block"); 356*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 357*0b57cec5SDimitry Andric return Triple; 358*0b57cec5SDimitry Andric case BitstreamEntry::Record: 359*0b57cec5SDimitry Andric // The interesting case. 360*0b57cec5SDimitry Andric break; 361*0b57cec5SDimitry Andric } 362*0b57cec5SDimitry Andric 363*0b57cec5SDimitry Andric // Read a record. 364*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 365*0b57cec5SDimitry Andric if (!MaybeRecord) 366*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 367*0b57cec5SDimitry Andric switch (MaybeRecord.get()) { 368*0b57cec5SDimitry Andric default: break; // Default behavior, ignore unknown content. 369*0b57cec5SDimitry Andric case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] 370*0b57cec5SDimitry Andric std::string S; 371*0b57cec5SDimitry Andric if (convertToString(Record, 0, S)) 37281ad6265SDimitry Andric return error("Invalid triple record"); 373*0b57cec5SDimitry Andric Triple = S; 374*0b57cec5SDimitry Andric break; 375*0b57cec5SDimitry Andric } 376*0b57cec5SDimitry Andric } 377*0b57cec5SDimitry Andric Record.clear(); 378*0b57cec5SDimitry Andric } 379*0b57cec5SDimitry Andric llvm_unreachable("Exit infinite loop"); 380*0b57cec5SDimitry Andric } 381*0b57cec5SDimitry Andric 382*0b57cec5SDimitry Andric static Expected<std::string> readTriple(BitstreamCursor &Stream) { 383*0b57cec5SDimitry Andric // We expect a number of well-defined blocks, though we don't necessarily 384*0b57cec5SDimitry Andric // need to understand them all. 385*0b57cec5SDimitry Andric while (true) { 386*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advance(); 387*0b57cec5SDimitry Andric if (!MaybeEntry) 388*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 389*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 390*0b57cec5SDimitry Andric 391*0b57cec5SDimitry Andric switch (Entry.Kind) { 392*0b57cec5SDimitry Andric case BitstreamEntry::Error: 393*0b57cec5SDimitry Andric return error("Malformed block"); 394*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 395*0b57cec5SDimitry Andric return ""; 396*0b57cec5SDimitry Andric 397*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: 398*0b57cec5SDimitry Andric if (Entry.ID == bitc::MODULE_BLOCK_ID) 399*0b57cec5SDimitry Andric return readModuleTriple(Stream); 400*0b57cec5SDimitry Andric 401*0b57cec5SDimitry Andric // Ignore other sub-blocks. 402*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 403*0b57cec5SDimitry Andric return std::move(Err); 404*0b57cec5SDimitry Andric continue; 405*0b57cec5SDimitry Andric 406*0b57cec5SDimitry Andric case BitstreamEntry::Record: 407*0b57cec5SDimitry Andric if (llvm::Expected<unsigned> Skipped = Stream.skipRecord(Entry.ID)) 408*0b57cec5SDimitry Andric continue; 409*0b57cec5SDimitry Andric else 410*0b57cec5SDimitry Andric return Skipped.takeError(); 411*0b57cec5SDimitry Andric } 412*0b57cec5SDimitry Andric } 413*0b57cec5SDimitry Andric } 414*0b57cec5SDimitry Andric 415*0b57cec5SDimitry Andric namespace { 416*0b57cec5SDimitry Andric 417*0b57cec5SDimitry Andric class BitcodeReaderBase { 418*0b57cec5SDimitry Andric protected: 419*0b57cec5SDimitry Andric BitcodeReaderBase(BitstreamCursor Stream, StringRef Strtab) 420*0b57cec5SDimitry Andric : Stream(std::move(Stream)), Strtab(Strtab) { 421*0b57cec5SDimitry Andric this->Stream.setBlockInfo(&BlockInfo); 422*0b57cec5SDimitry Andric } 423*0b57cec5SDimitry Andric 424*0b57cec5SDimitry Andric BitstreamBlockInfo BlockInfo; 425*0b57cec5SDimitry Andric BitstreamCursor Stream; 426*0b57cec5SDimitry Andric StringRef Strtab; 427*0b57cec5SDimitry Andric 428*0b57cec5SDimitry Andric /// In version 2 of the bitcode we store names of global values and comdats in 429*0b57cec5SDimitry Andric /// a string table rather than in the VST. 430*0b57cec5SDimitry Andric bool UseStrtab = false; 431*0b57cec5SDimitry Andric 432*0b57cec5SDimitry Andric Expected<unsigned> parseVersionRecord(ArrayRef<uint64_t> Record); 433*0b57cec5SDimitry Andric 434*0b57cec5SDimitry Andric /// If this module uses a string table, pop the reference to the string table 435*0b57cec5SDimitry Andric /// and return the referenced string and the rest of the record. Otherwise 436*0b57cec5SDimitry Andric /// just return the record itself. 437*0b57cec5SDimitry Andric std::pair<StringRef, ArrayRef<uint64_t>> 438*0b57cec5SDimitry Andric readNameFromStrtab(ArrayRef<uint64_t> Record); 439*0b57cec5SDimitry Andric 44081ad6265SDimitry Andric Error readBlockInfo(); 441*0b57cec5SDimitry Andric 442*0b57cec5SDimitry Andric // Contains an arbitrary and optional string identifying the bitcode producer 443*0b57cec5SDimitry Andric std::string ProducerIdentification; 444*0b57cec5SDimitry Andric 445*0b57cec5SDimitry Andric Error error(const Twine &Message); 446*0b57cec5SDimitry Andric }; 447*0b57cec5SDimitry Andric 448*0b57cec5SDimitry Andric } // end anonymous namespace 449*0b57cec5SDimitry Andric 450*0b57cec5SDimitry Andric Error BitcodeReaderBase::error(const Twine &Message) { 451*0b57cec5SDimitry Andric std::string FullMsg = Message.str(); 452*0b57cec5SDimitry Andric if (!ProducerIdentification.empty()) 453*0b57cec5SDimitry Andric FullMsg += " (Producer: '" + ProducerIdentification + "' Reader: 'LLVM " + 454*0b57cec5SDimitry Andric LLVM_VERSION_STRING "')"; 455*0b57cec5SDimitry Andric return ::error(FullMsg); 456*0b57cec5SDimitry Andric } 457*0b57cec5SDimitry Andric 458*0b57cec5SDimitry Andric Expected<unsigned> 459*0b57cec5SDimitry Andric BitcodeReaderBase::parseVersionRecord(ArrayRef<uint64_t> Record) { 460*0b57cec5SDimitry Andric if (Record.empty()) 46181ad6265SDimitry Andric return error("Invalid version record"); 462*0b57cec5SDimitry Andric unsigned ModuleVersion = Record[0]; 463*0b57cec5SDimitry Andric if (ModuleVersion > 2) 464*0b57cec5SDimitry Andric return error("Invalid value"); 465*0b57cec5SDimitry Andric UseStrtab = ModuleVersion >= 2; 466*0b57cec5SDimitry Andric return ModuleVersion; 467*0b57cec5SDimitry Andric } 468*0b57cec5SDimitry Andric 469*0b57cec5SDimitry Andric std::pair<StringRef, ArrayRef<uint64_t>> 470*0b57cec5SDimitry Andric BitcodeReaderBase::readNameFromStrtab(ArrayRef<uint64_t> Record) { 471*0b57cec5SDimitry Andric if (!UseStrtab) 472*0b57cec5SDimitry Andric return {"", Record}; 473*0b57cec5SDimitry Andric // Invalid reference. Let the caller complain about the record being empty. 474*0b57cec5SDimitry Andric if (Record[0] + Record[1] > Strtab.size()) 475*0b57cec5SDimitry Andric return {"", {}}; 476*0b57cec5SDimitry Andric return {StringRef(Strtab.data() + Record[0], Record[1]), Record.slice(2)}; 477*0b57cec5SDimitry Andric } 478*0b57cec5SDimitry Andric 479*0b57cec5SDimitry Andric namespace { 480*0b57cec5SDimitry Andric 48181ad6265SDimitry Andric /// This represents a constant expression or constant aggregate using a custom 48281ad6265SDimitry Andric /// structure internal to the bitcode reader. Later, this structure will be 48381ad6265SDimitry Andric /// expanded by materializeValue() either into a constant expression/aggregate, 48481ad6265SDimitry Andric /// or into an instruction sequence at the point of use. This allows us to 48581ad6265SDimitry Andric /// upgrade bitcode using constant expressions even if this kind of constant 48681ad6265SDimitry Andric /// expression is no longer supported. 48781ad6265SDimitry Andric class BitcodeConstant final : public Value, 48881ad6265SDimitry Andric TrailingObjects<BitcodeConstant, unsigned> { 48981ad6265SDimitry Andric friend TrailingObjects; 49081ad6265SDimitry Andric 49181ad6265SDimitry Andric // Value subclass ID: Pick largest possible value to avoid any clashes. 49281ad6265SDimitry Andric static constexpr uint8_t SubclassID = 255; 49381ad6265SDimitry Andric 49481ad6265SDimitry Andric public: 49581ad6265SDimitry Andric // Opcodes used for non-expressions. This includes constant aggregates 49681ad6265SDimitry Andric // (struct, array, vector) that might need expansion, as well as non-leaf 49781ad6265SDimitry Andric // constants that don't need expansion (no_cfi, dso_local, blockaddress), 49881ad6265SDimitry Andric // but still go through BitcodeConstant to avoid different uselist orders 49981ad6265SDimitry Andric // between the two cases. 50081ad6265SDimitry Andric static constexpr uint8_t ConstantStructOpcode = 255; 50181ad6265SDimitry Andric static constexpr uint8_t ConstantArrayOpcode = 254; 50281ad6265SDimitry Andric static constexpr uint8_t ConstantVectorOpcode = 253; 50381ad6265SDimitry Andric static constexpr uint8_t NoCFIOpcode = 252; 50481ad6265SDimitry Andric static constexpr uint8_t DSOLocalEquivalentOpcode = 251; 50581ad6265SDimitry Andric static constexpr uint8_t BlockAddressOpcode = 250; 50681ad6265SDimitry Andric static constexpr uint8_t FirstSpecialOpcode = BlockAddressOpcode; 50781ad6265SDimitry Andric 50881ad6265SDimitry Andric // Separate struct to make passing different number of parameters to 50981ad6265SDimitry Andric // BitcodeConstant::create() more convenient. 51081ad6265SDimitry Andric struct ExtraInfo { 51181ad6265SDimitry Andric uint8_t Opcode; 51281ad6265SDimitry Andric uint8_t Flags; 51381ad6265SDimitry Andric unsigned Extra; 51481ad6265SDimitry Andric Type *SrcElemTy; 51581ad6265SDimitry Andric 51681ad6265SDimitry Andric ExtraInfo(uint8_t Opcode, uint8_t Flags = 0, unsigned Extra = 0, 51781ad6265SDimitry Andric Type *SrcElemTy = nullptr) 51881ad6265SDimitry Andric : Opcode(Opcode), Flags(Flags), Extra(Extra), SrcElemTy(SrcElemTy) {} 51981ad6265SDimitry Andric }; 52081ad6265SDimitry Andric 52181ad6265SDimitry Andric uint8_t Opcode; 52281ad6265SDimitry Andric uint8_t Flags; 52381ad6265SDimitry Andric unsigned NumOperands; 52481ad6265SDimitry Andric unsigned Extra; // GEP inrange index or blockaddress BB id. 52581ad6265SDimitry Andric Type *SrcElemTy; // GEP source element type. 52681ad6265SDimitry Andric 52781ad6265SDimitry Andric private: 52881ad6265SDimitry Andric BitcodeConstant(Type *Ty, const ExtraInfo &Info, ArrayRef<unsigned> OpIDs) 52981ad6265SDimitry Andric : Value(Ty, SubclassID), Opcode(Info.Opcode), Flags(Info.Flags), 53081ad6265SDimitry Andric NumOperands(OpIDs.size()), Extra(Info.Extra), 53181ad6265SDimitry Andric SrcElemTy(Info.SrcElemTy) { 53281ad6265SDimitry Andric std::uninitialized_copy(OpIDs.begin(), OpIDs.end(), 53381ad6265SDimitry Andric getTrailingObjects<unsigned>()); 53481ad6265SDimitry Andric } 53581ad6265SDimitry Andric 53681ad6265SDimitry Andric BitcodeConstant &operator=(const BitcodeConstant &) = delete; 53781ad6265SDimitry Andric 53881ad6265SDimitry Andric public: 53981ad6265SDimitry Andric static BitcodeConstant *create(BumpPtrAllocator &A, Type *Ty, 54081ad6265SDimitry Andric const ExtraInfo &Info, 54181ad6265SDimitry Andric ArrayRef<unsigned> OpIDs) { 54281ad6265SDimitry Andric void *Mem = A.Allocate(totalSizeToAlloc<unsigned>(OpIDs.size()), 54381ad6265SDimitry Andric alignof(BitcodeConstant)); 54481ad6265SDimitry Andric return new (Mem) BitcodeConstant(Ty, Info, OpIDs); 54581ad6265SDimitry Andric } 54681ad6265SDimitry Andric 54781ad6265SDimitry Andric static bool classof(const Value *V) { return V->getValueID() == SubclassID; } 54881ad6265SDimitry Andric 54981ad6265SDimitry Andric ArrayRef<unsigned> getOperandIDs() const { 550bdd1243dSDimitry Andric return ArrayRef(getTrailingObjects<unsigned>(), NumOperands); 55181ad6265SDimitry Andric } 55281ad6265SDimitry Andric 553bdd1243dSDimitry Andric std::optional<unsigned> getInRangeIndex() const { 55481ad6265SDimitry Andric assert(Opcode == Instruction::GetElementPtr); 55581ad6265SDimitry Andric if (Extra == (unsigned)-1) 556bdd1243dSDimitry Andric return std::nullopt; 55781ad6265SDimitry Andric return Extra; 55881ad6265SDimitry Andric } 55981ad6265SDimitry Andric 56081ad6265SDimitry Andric const char *getOpcodeName() const { 56181ad6265SDimitry Andric return Instruction::getOpcodeName(Opcode); 56281ad6265SDimitry Andric } 56381ad6265SDimitry Andric }; 56481ad6265SDimitry Andric 565*0b57cec5SDimitry Andric class BitcodeReader : public BitcodeReaderBase, public GVMaterializer { 566*0b57cec5SDimitry Andric LLVMContext &Context; 567*0b57cec5SDimitry Andric Module *TheModule = nullptr; 568*0b57cec5SDimitry Andric // Next offset to start scanning for lazy parsing of function bodies. 569*0b57cec5SDimitry Andric uint64_t NextUnreadBit = 0; 570*0b57cec5SDimitry Andric // Last function offset found in the VST. 571*0b57cec5SDimitry Andric uint64_t LastFunctionBlockBit = 0; 572*0b57cec5SDimitry Andric bool SeenValueSymbolTable = false; 573*0b57cec5SDimitry Andric uint64_t VSTOffset = 0; 574*0b57cec5SDimitry Andric 575*0b57cec5SDimitry Andric std::vector<std::string> SectionTable; 576*0b57cec5SDimitry Andric std::vector<std::string> GCTable; 577*0b57cec5SDimitry Andric 578*0b57cec5SDimitry Andric std::vector<Type *> TypeList; 57981ad6265SDimitry Andric /// Track type IDs of contained types. Order is the same as the contained 58081ad6265SDimitry Andric /// types of a Type*. This is used during upgrades of typed pointer IR in 58181ad6265SDimitry Andric /// opaque pointer mode. 58281ad6265SDimitry Andric DenseMap<unsigned, SmallVector<unsigned, 1>> ContainedTypeIDs; 58381ad6265SDimitry Andric /// In some cases, we need to create a type ID for a type that was not 58481ad6265SDimitry Andric /// explicitly encoded in the bitcode, or we don't know about at the current 58581ad6265SDimitry Andric /// point. For example, a global may explicitly encode the value type ID, but 58681ad6265SDimitry Andric /// not have a type ID for the pointer to value type, for which we create a 58781ad6265SDimitry Andric /// virtual type ID instead. This map stores the new type ID that was created 58881ad6265SDimitry Andric /// for the given pair of Type and contained type ID. 58981ad6265SDimitry Andric DenseMap<std::pair<Type *, unsigned>, unsigned> VirtualTypeIDs; 59081ad6265SDimitry Andric DenseMap<Function *, unsigned> FunctionTypeIDs; 59181ad6265SDimitry Andric /// Allocator for BitcodeConstants. This should come before ValueList, 59281ad6265SDimitry Andric /// because the ValueList might hold ValueHandles to these constants, so 59381ad6265SDimitry Andric /// ValueList must be destroyed before Alloc. 59481ad6265SDimitry Andric BumpPtrAllocator Alloc; 595*0b57cec5SDimitry Andric BitcodeReaderValueList ValueList; 596bdd1243dSDimitry Andric std::optional<MetadataLoader> MDLoader; 597*0b57cec5SDimitry Andric std::vector<Comdat *> ComdatList; 5980eae32dcSDimitry Andric DenseSet<GlobalObject *> ImplicitComdatObjects; 599*0b57cec5SDimitry Andric SmallVector<Instruction *, 64> InstructionList; 600*0b57cec5SDimitry Andric 601*0b57cec5SDimitry Andric std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInits; 602349cc55cSDimitry Andric std::vector<std::pair<GlobalValue *, unsigned>> IndirectSymbolInits; 603349cc55cSDimitry Andric 604349cc55cSDimitry Andric struct FunctionOperandInfo { 605349cc55cSDimitry Andric Function *F; 606349cc55cSDimitry Andric unsigned PersonalityFn; 607349cc55cSDimitry Andric unsigned Prefix; 608349cc55cSDimitry Andric unsigned Prologue; 609349cc55cSDimitry Andric }; 610349cc55cSDimitry Andric std::vector<FunctionOperandInfo> FunctionOperands; 611*0b57cec5SDimitry Andric 612*0b57cec5SDimitry Andric /// The set of attributes by index. Index zero in the file is for null, and 613*0b57cec5SDimitry Andric /// is thus not represented here. As such all indices are off by one. 614*0b57cec5SDimitry Andric std::vector<AttributeList> MAttributes; 615*0b57cec5SDimitry Andric 616*0b57cec5SDimitry Andric /// The set of attribute groups. 617*0b57cec5SDimitry Andric std::map<unsigned, AttributeList> MAttributeGroups; 618*0b57cec5SDimitry Andric 619*0b57cec5SDimitry Andric /// While parsing a function body, this is a list of the basic blocks for the 620*0b57cec5SDimitry Andric /// function. 621*0b57cec5SDimitry Andric std::vector<BasicBlock*> FunctionBBs; 622*0b57cec5SDimitry Andric 623*0b57cec5SDimitry Andric // When reading the module header, this list is populated with functions that 624*0b57cec5SDimitry Andric // have bodies later in the file. 625*0b57cec5SDimitry Andric std::vector<Function*> FunctionsWithBodies; 626*0b57cec5SDimitry Andric 627*0b57cec5SDimitry Andric // When intrinsic functions are encountered which require upgrading they are 628*0b57cec5SDimitry Andric // stored here with their replacement function. 629*0b57cec5SDimitry Andric using UpdatedIntrinsicMap = DenseMap<Function *, Function *>; 630*0b57cec5SDimitry Andric UpdatedIntrinsicMap UpgradedIntrinsics; 631*0b57cec5SDimitry Andric 632*0b57cec5SDimitry Andric // Several operations happen after the module header has been read, but 633*0b57cec5SDimitry Andric // before function bodies are processed. This keeps track of whether 634*0b57cec5SDimitry Andric // we've done this yet. 635*0b57cec5SDimitry Andric bool SeenFirstFunctionBody = false; 636*0b57cec5SDimitry Andric 637*0b57cec5SDimitry Andric /// When function bodies are initially scanned, this map contains info about 638*0b57cec5SDimitry Andric /// where to find deferred function body in the stream. 639*0b57cec5SDimitry Andric DenseMap<Function*, uint64_t> DeferredFunctionInfo; 640*0b57cec5SDimitry Andric 641*0b57cec5SDimitry Andric /// When Metadata block is initially scanned when parsing the module, we may 642*0b57cec5SDimitry Andric /// choose to defer parsing of the metadata. This vector contains info about 643*0b57cec5SDimitry Andric /// which Metadata blocks are deferred. 644*0b57cec5SDimitry Andric std::vector<uint64_t> DeferredMetadataInfo; 645*0b57cec5SDimitry Andric 646*0b57cec5SDimitry Andric /// These are basic blocks forward-referenced by block addresses. They are 647*0b57cec5SDimitry Andric /// inserted lazily into functions when they're loaded. The basic block ID is 648*0b57cec5SDimitry Andric /// its index into the vector. 649*0b57cec5SDimitry Andric DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs; 650*0b57cec5SDimitry Andric std::deque<Function *> BasicBlockFwdRefQueue; 651*0b57cec5SDimitry Andric 65281ad6265SDimitry Andric /// These are Functions that contain BlockAddresses which refer a different 65381ad6265SDimitry Andric /// Function. When parsing the different Function, queue Functions that refer 65481ad6265SDimitry Andric /// to the different Function. Those Functions must be materialized in order 65581ad6265SDimitry Andric /// to resolve their BlockAddress constants before the different Function 65681ad6265SDimitry Andric /// gets moved into another Module. 65781ad6265SDimitry Andric std::vector<Function *> BackwardRefFunctions; 65881ad6265SDimitry Andric 659*0b57cec5SDimitry Andric /// Indicates that we are using a new encoding for instruction operands where 660*0b57cec5SDimitry Andric /// most operands in the current FUNCTION_BLOCK are encoded relative to the 661*0b57cec5SDimitry Andric /// instruction number, for a more compact encoding. Some instruction 662*0b57cec5SDimitry Andric /// operands are not relative to the instruction ID: basic block numbers, and 663*0b57cec5SDimitry Andric /// types. Once the old style function blocks have been phased out, we would 664*0b57cec5SDimitry Andric /// not need this flag. 665*0b57cec5SDimitry Andric bool UseRelativeIDs = false; 666*0b57cec5SDimitry Andric 667*0b57cec5SDimitry Andric /// True if all functions will be materialized, negating the need to process 668*0b57cec5SDimitry Andric /// (e.g.) blockaddress forward references. 669*0b57cec5SDimitry Andric bool WillMaterializeAllForwardRefs = false; 670*0b57cec5SDimitry Andric 671*0b57cec5SDimitry Andric bool StripDebugInfo = false; 672*0b57cec5SDimitry Andric TBAAVerifier TBAAVerifyHelper; 673*0b57cec5SDimitry Andric 674*0b57cec5SDimitry Andric std::vector<std::string> BundleTags; 675*0b57cec5SDimitry Andric SmallVector<SyncScope::ID, 8> SSIDs; 676*0b57cec5SDimitry Andric 677bdd1243dSDimitry Andric std::optional<ValueTypeCallbackTy> ValueTypeCallback; 678bdd1243dSDimitry Andric 679*0b57cec5SDimitry Andric public: 680*0b57cec5SDimitry Andric BitcodeReader(BitstreamCursor Stream, StringRef Strtab, 681*0b57cec5SDimitry Andric StringRef ProducerIdentification, LLVMContext &Context); 682*0b57cec5SDimitry Andric 683*0b57cec5SDimitry Andric Error materializeForwardReferencedFunctions(); 684*0b57cec5SDimitry Andric 685*0b57cec5SDimitry Andric Error materialize(GlobalValue *GV) override; 686*0b57cec5SDimitry Andric Error materializeModule() override; 687*0b57cec5SDimitry Andric std::vector<StructType *> getIdentifiedStructTypes() const override; 688*0b57cec5SDimitry Andric 689*0b57cec5SDimitry Andric /// Main interface to parsing a bitcode buffer. 690*0b57cec5SDimitry Andric /// \returns true if an error occurred. 691bdd1243dSDimitry Andric Error parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata, 692bdd1243dSDimitry Andric bool IsImporting, ParserCallbacks Callbacks = {}); 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 713bdd1243dSDimitry Andric void callValueTypeCallback(Value *F, unsigned TypeID); 71481ad6265SDimitry Andric Expected<Value *> materializeValue(unsigned ValID, BasicBlock *InsertBB); 71581ad6265SDimitry Andric Expected<Constant *> getValueForInitializer(unsigned ID); 71681ad6265SDimitry Andric 71781ad6265SDimitry Andric Value *getFnValueByID(unsigned ID, Type *Ty, unsigned TyID, 71881ad6265SDimitry Andric BasicBlock *ConstExprInsertBB) { 719*0b57cec5SDimitry Andric if (Ty && Ty->isMetadataTy()) 720*0b57cec5SDimitry Andric return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID)); 72181ad6265SDimitry Andric return ValueList.getValueFwdRef(ID, Ty, TyID, ConstExprInsertBB); 722*0b57cec5SDimitry Andric } 723*0b57cec5SDimitry Andric 724*0b57cec5SDimitry Andric Metadata *getFnMetadataByID(unsigned ID) { 725*0b57cec5SDimitry Andric return MDLoader->getMetadataFwdRefOrLoad(ID); 726*0b57cec5SDimitry Andric } 727*0b57cec5SDimitry Andric 728*0b57cec5SDimitry Andric BasicBlock *getBasicBlock(unsigned ID) const { 729*0b57cec5SDimitry Andric if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID 730*0b57cec5SDimitry Andric return FunctionBBs[ID]; 731*0b57cec5SDimitry Andric } 732*0b57cec5SDimitry Andric 733*0b57cec5SDimitry Andric AttributeList getAttributes(unsigned i) const { 734*0b57cec5SDimitry Andric if (i-1 < MAttributes.size()) 735*0b57cec5SDimitry Andric return MAttributes[i-1]; 736*0b57cec5SDimitry Andric return AttributeList(); 737*0b57cec5SDimitry Andric } 738*0b57cec5SDimitry Andric 739*0b57cec5SDimitry Andric /// Read a value/type pair out of the specified record from slot 'Slot'. 740*0b57cec5SDimitry Andric /// Increment Slot past the number of slots used in the record. Return true on 741*0b57cec5SDimitry Andric /// failure. 742e8d8bef9SDimitry Andric bool getValueTypePair(const SmallVectorImpl<uint64_t> &Record, unsigned &Slot, 74381ad6265SDimitry Andric unsigned InstNum, Value *&ResVal, unsigned &TypeID, 74481ad6265SDimitry Andric BasicBlock *ConstExprInsertBB) { 745*0b57cec5SDimitry Andric if (Slot == Record.size()) return true; 746*0b57cec5SDimitry Andric unsigned ValNo = (unsigned)Record[Slot++]; 747*0b57cec5SDimitry Andric // Adjust the ValNo, if it was encoded relative to the InstNum. 748*0b57cec5SDimitry Andric if (UseRelativeIDs) 749*0b57cec5SDimitry Andric ValNo = InstNum - ValNo; 750*0b57cec5SDimitry Andric if (ValNo < InstNum) { 751*0b57cec5SDimitry Andric // If this is not a forward reference, just return the value we already 752*0b57cec5SDimitry Andric // have. 75381ad6265SDimitry Andric TypeID = ValueList.getTypeID(ValNo); 75481ad6265SDimitry Andric ResVal = getFnValueByID(ValNo, nullptr, TypeID, ConstExprInsertBB); 75581ad6265SDimitry Andric assert((!ResVal || ResVal->getType() == getTypeByID(TypeID)) && 75681ad6265SDimitry Andric "Incorrect type ID stored for value"); 757*0b57cec5SDimitry Andric return ResVal == nullptr; 758*0b57cec5SDimitry Andric } 759*0b57cec5SDimitry Andric if (Slot == Record.size()) 760*0b57cec5SDimitry Andric return true; 761*0b57cec5SDimitry Andric 76281ad6265SDimitry Andric TypeID = (unsigned)Record[Slot++]; 76381ad6265SDimitry Andric ResVal = getFnValueByID(ValNo, getTypeByID(TypeID), TypeID, 76481ad6265SDimitry Andric ConstExprInsertBB); 765*0b57cec5SDimitry Andric return ResVal == nullptr; 766*0b57cec5SDimitry Andric } 767*0b57cec5SDimitry Andric 768*0b57cec5SDimitry Andric /// Read a value out of the specified record from slot 'Slot'. Increment Slot 769*0b57cec5SDimitry Andric /// past the number of slots used by the value in the record. Return true if 770*0b57cec5SDimitry Andric /// there is an error. 771e8d8bef9SDimitry Andric bool popValue(const SmallVectorImpl<uint64_t> &Record, unsigned &Slot, 77281ad6265SDimitry Andric unsigned InstNum, Type *Ty, unsigned TyID, Value *&ResVal, 77381ad6265SDimitry Andric BasicBlock *ConstExprInsertBB) { 77481ad6265SDimitry Andric if (getValue(Record, Slot, InstNum, Ty, TyID, ResVal, ConstExprInsertBB)) 775*0b57cec5SDimitry Andric return true; 776*0b57cec5SDimitry Andric // All values currently take a single record slot. 777*0b57cec5SDimitry Andric ++Slot; 778*0b57cec5SDimitry Andric return false; 779*0b57cec5SDimitry Andric } 780*0b57cec5SDimitry Andric 781*0b57cec5SDimitry Andric /// Like popValue, but does not increment the Slot number. 782e8d8bef9SDimitry Andric bool getValue(const SmallVectorImpl<uint64_t> &Record, unsigned Slot, 78381ad6265SDimitry Andric unsigned InstNum, Type *Ty, unsigned TyID, Value *&ResVal, 78481ad6265SDimitry Andric BasicBlock *ConstExprInsertBB) { 78581ad6265SDimitry Andric ResVal = getValue(Record, Slot, InstNum, Ty, TyID, ConstExprInsertBB); 786*0b57cec5SDimitry Andric return ResVal == nullptr; 787*0b57cec5SDimitry Andric } 788*0b57cec5SDimitry Andric 789*0b57cec5SDimitry Andric /// Version of getValue that returns ResVal directly, or 0 if there is an 790*0b57cec5SDimitry Andric /// error. 791e8d8bef9SDimitry Andric Value *getValue(const SmallVectorImpl<uint64_t> &Record, unsigned Slot, 79281ad6265SDimitry Andric unsigned InstNum, Type *Ty, unsigned TyID, 79381ad6265SDimitry Andric BasicBlock *ConstExprInsertBB) { 794*0b57cec5SDimitry Andric if (Slot == Record.size()) return nullptr; 795*0b57cec5SDimitry Andric unsigned ValNo = (unsigned)Record[Slot]; 796*0b57cec5SDimitry Andric // Adjust the ValNo, if it was encoded relative to the InstNum. 797*0b57cec5SDimitry Andric if (UseRelativeIDs) 798*0b57cec5SDimitry Andric ValNo = InstNum - ValNo; 79981ad6265SDimitry Andric return getFnValueByID(ValNo, Ty, TyID, ConstExprInsertBB); 800*0b57cec5SDimitry Andric } 801*0b57cec5SDimitry Andric 802*0b57cec5SDimitry Andric /// Like getValue, but decodes signed VBRs. 803e8d8bef9SDimitry Andric Value *getValueSigned(const SmallVectorImpl<uint64_t> &Record, unsigned Slot, 80481ad6265SDimitry Andric unsigned InstNum, Type *Ty, unsigned TyID, 80581ad6265SDimitry Andric BasicBlock *ConstExprInsertBB) { 806*0b57cec5SDimitry Andric if (Slot == Record.size()) return nullptr; 807*0b57cec5SDimitry Andric unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]); 808*0b57cec5SDimitry Andric // Adjust the ValNo, if it was encoded relative to the InstNum. 809*0b57cec5SDimitry Andric if (UseRelativeIDs) 810*0b57cec5SDimitry Andric ValNo = InstNum - ValNo; 81181ad6265SDimitry Andric return getFnValueByID(ValNo, Ty, TyID, ConstExprInsertBB); 812*0b57cec5SDimitry Andric } 813*0b57cec5SDimitry Andric 814fe6060f1SDimitry Andric /// Upgrades old-style typeless byval/sret/inalloca attributes by adding the 815fe6060f1SDimitry Andric /// corresponding argument's pointee type. Also upgrades intrinsics that now 816fe6060f1SDimitry Andric /// require an elementtype attribute. 81781ad6265SDimitry Andric Error propagateAttributeTypes(CallBase *CB, ArrayRef<unsigned> ArgsTys); 818*0b57cec5SDimitry Andric 819*0b57cec5SDimitry Andric /// Converts alignment exponent (i.e. power of two (or zero)) to the 820*0b57cec5SDimitry Andric /// corresponding alignment to use. If alignment is too large, returns 821*0b57cec5SDimitry Andric /// a corresponding error code. 8228bcb0991SDimitry Andric Error parseAlignmentValue(uint64_t Exponent, MaybeAlign &Alignment); 823*0b57cec5SDimitry Andric Error parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind); 824bdd1243dSDimitry Andric Error parseModule(uint64_t ResumeBit, bool ShouldLazyLoadMetadata = false, 825bdd1243dSDimitry Andric ParserCallbacks Callbacks = {}); 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 887bdd1243dSDimitry Andric // identical (this is the second tuple member). 888bdd1243dSDimitry Andric // The third tuple member is the real GUID of the ValueInfo. 889bdd1243dSDimitry Andric DenseMap<unsigned, 890bdd1243dSDimitry Andric std::tuple<ValueInfo, GlobalValue::GUID, GlobalValue::GUID>> 891*0b57cec5SDimitry Andric ValueIdToValueInfoMap; 892*0b57cec5SDimitry Andric 893*0b57cec5SDimitry Andric /// Map populated during module path string table parsing, from the 894*0b57cec5SDimitry Andric /// module ID to a string reference owned by the index's module 895*0b57cec5SDimitry Andric /// path string table, used to correlate with combined index 896*0b57cec5SDimitry Andric /// summary records. 897*0b57cec5SDimitry Andric DenseMap<uint64_t, StringRef> ModuleIdMap; 898*0b57cec5SDimitry Andric 899*0b57cec5SDimitry Andric /// Original source file name recorded in a bitcode record. 900*0b57cec5SDimitry Andric std::string SourceFileName; 901*0b57cec5SDimitry Andric 902*0b57cec5SDimitry Andric /// The string identifier given to this module by the client, normally the 903*0b57cec5SDimitry Andric /// path to the bitcode file. 904*0b57cec5SDimitry Andric StringRef ModulePath; 905*0b57cec5SDimitry Andric 906*0b57cec5SDimitry Andric /// For per-module summary indexes, the unique numerical identifier given to 907*0b57cec5SDimitry Andric /// this module by the client. 908*0b57cec5SDimitry Andric unsigned ModuleId; 909*0b57cec5SDimitry Andric 910bdd1243dSDimitry Andric /// Callback to ask whether a symbol is the prevailing copy when invoked 911bdd1243dSDimitry Andric /// during combined index building. 912bdd1243dSDimitry Andric std::function<bool(GlobalValue::GUID)> IsPrevailing; 913bdd1243dSDimitry Andric 914bdd1243dSDimitry Andric /// Saves the stack ids from the STACK_IDS record to consult when adding stack 915bdd1243dSDimitry Andric /// ids from the lists in the callsite and alloc entries to the index. 916bdd1243dSDimitry Andric std::vector<uint64_t> StackIds; 917bdd1243dSDimitry Andric 918*0b57cec5SDimitry Andric public: 919bdd1243dSDimitry Andric ModuleSummaryIndexBitcodeReader( 920bdd1243dSDimitry Andric BitstreamCursor Stream, StringRef Strtab, ModuleSummaryIndex &TheIndex, 921bdd1243dSDimitry Andric StringRef ModulePath, unsigned ModuleId, 922bdd1243dSDimitry Andric std::function<bool(GlobalValue::GUID)> IsPrevailing = nullptr); 923*0b57cec5SDimitry Andric 924*0b57cec5SDimitry Andric Error parseModule(); 925*0b57cec5SDimitry Andric 926*0b57cec5SDimitry Andric private: 927*0b57cec5SDimitry Andric void setValueGUID(uint64_t ValueID, StringRef ValueName, 928*0b57cec5SDimitry Andric GlobalValue::LinkageTypes Linkage, 929*0b57cec5SDimitry Andric StringRef SourceFileName); 930*0b57cec5SDimitry Andric Error parseValueSymbolTable( 931*0b57cec5SDimitry Andric uint64_t Offset, 932*0b57cec5SDimitry Andric DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap); 933*0b57cec5SDimitry Andric std::vector<ValueInfo> makeRefList(ArrayRef<uint64_t> Record); 934*0b57cec5SDimitry Andric std::vector<FunctionSummary::EdgeTy> makeCallList(ArrayRef<uint64_t> Record, 935*0b57cec5SDimitry Andric bool IsOldProfileFormat, 936*0b57cec5SDimitry Andric bool HasProfile, 937*0b57cec5SDimitry Andric bool HasRelBF); 938*0b57cec5SDimitry Andric Error parseEntireSummary(unsigned ID); 939*0b57cec5SDimitry Andric Error parseModuleStringTable(); 940*0b57cec5SDimitry Andric void parseTypeIdCompatibleVtableSummaryRecord(ArrayRef<uint64_t> Record); 941*0b57cec5SDimitry Andric void parseTypeIdCompatibleVtableInfo(ArrayRef<uint64_t> Record, size_t &Slot, 942*0b57cec5SDimitry Andric TypeIdCompatibleVtableInfo &TypeId); 943e8d8bef9SDimitry Andric std::vector<FunctionSummary::ParamAccess> 944e8d8bef9SDimitry Andric parseParamAccesses(ArrayRef<uint64_t> Record); 945*0b57cec5SDimitry Andric 946bdd1243dSDimitry Andric template <bool AllowNullValueInfo = false> 947bdd1243dSDimitry Andric std::tuple<ValueInfo, GlobalValue::GUID, GlobalValue::GUID> 948*0b57cec5SDimitry Andric getValueInfoFromValueId(unsigned ValueId); 949*0b57cec5SDimitry Andric 950*0b57cec5SDimitry Andric void addThisModule(); 951*0b57cec5SDimitry Andric ModuleSummaryIndex::ModuleInfo *getThisModule(); 952*0b57cec5SDimitry Andric }; 953*0b57cec5SDimitry Andric 954*0b57cec5SDimitry Andric } // end anonymous namespace 955*0b57cec5SDimitry Andric 956*0b57cec5SDimitry Andric std::error_code llvm::errorToErrorCodeAndEmitErrors(LLVMContext &Ctx, 957*0b57cec5SDimitry Andric Error Err) { 958*0b57cec5SDimitry Andric if (Err) { 959*0b57cec5SDimitry Andric std::error_code EC; 960*0b57cec5SDimitry Andric handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) { 961*0b57cec5SDimitry Andric EC = EIB.convertToErrorCode(); 962*0b57cec5SDimitry Andric Ctx.emitError(EIB.message()); 963*0b57cec5SDimitry Andric }); 964*0b57cec5SDimitry Andric return EC; 965*0b57cec5SDimitry Andric } 966*0b57cec5SDimitry Andric return std::error_code(); 967*0b57cec5SDimitry Andric } 968*0b57cec5SDimitry Andric 969*0b57cec5SDimitry Andric BitcodeReader::BitcodeReader(BitstreamCursor Stream, StringRef Strtab, 970*0b57cec5SDimitry Andric StringRef ProducerIdentification, 971*0b57cec5SDimitry Andric LLVMContext &Context) 972*0b57cec5SDimitry Andric : BitcodeReaderBase(std::move(Stream), Strtab), Context(Context), 97381ad6265SDimitry Andric ValueList(this->Stream.SizeInBytes(), 97481ad6265SDimitry Andric [this](unsigned ValID, BasicBlock *InsertBB) { 97581ad6265SDimitry Andric return materializeValue(ValID, InsertBB); 97681ad6265SDimitry Andric }) { 9775ffd83dbSDimitry Andric this->ProducerIdentification = std::string(ProducerIdentification); 978*0b57cec5SDimitry Andric } 979*0b57cec5SDimitry Andric 980*0b57cec5SDimitry Andric Error BitcodeReader::materializeForwardReferencedFunctions() { 981*0b57cec5SDimitry Andric if (WillMaterializeAllForwardRefs) 982*0b57cec5SDimitry Andric return Error::success(); 983*0b57cec5SDimitry Andric 984*0b57cec5SDimitry Andric // Prevent recursion. 985*0b57cec5SDimitry Andric WillMaterializeAllForwardRefs = true; 986*0b57cec5SDimitry Andric 987*0b57cec5SDimitry Andric while (!BasicBlockFwdRefQueue.empty()) { 988*0b57cec5SDimitry Andric Function *F = BasicBlockFwdRefQueue.front(); 989*0b57cec5SDimitry Andric BasicBlockFwdRefQueue.pop_front(); 990*0b57cec5SDimitry Andric assert(F && "Expected valid function"); 991*0b57cec5SDimitry Andric if (!BasicBlockFwdRefs.count(F)) 992*0b57cec5SDimitry Andric // Already materialized. 993*0b57cec5SDimitry Andric continue; 994*0b57cec5SDimitry Andric 995*0b57cec5SDimitry Andric // Check for a function that isn't materializable to prevent an infinite 996*0b57cec5SDimitry Andric // loop. When parsing a blockaddress stored in a global variable, there 997*0b57cec5SDimitry Andric // isn't a trivial way to check if a function will have a body without a 998*0b57cec5SDimitry Andric // linear search through FunctionsWithBodies, so just check it here. 999*0b57cec5SDimitry Andric if (!F->isMaterializable()) 1000*0b57cec5SDimitry Andric return error("Never resolved function from blockaddress"); 1001*0b57cec5SDimitry Andric 1002*0b57cec5SDimitry Andric // Try to materialize F. 1003*0b57cec5SDimitry Andric if (Error Err = materialize(F)) 1004*0b57cec5SDimitry Andric return Err; 1005*0b57cec5SDimitry Andric } 1006*0b57cec5SDimitry Andric assert(BasicBlockFwdRefs.empty() && "Function missing from queue"); 1007*0b57cec5SDimitry Andric 100881ad6265SDimitry Andric for (Function *F : BackwardRefFunctions) 100981ad6265SDimitry Andric if (Error Err = materialize(F)) 101081ad6265SDimitry Andric return Err; 101181ad6265SDimitry Andric BackwardRefFunctions.clear(); 101281ad6265SDimitry Andric 1013*0b57cec5SDimitry Andric // Reset state. 1014*0b57cec5SDimitry Andric WillMaterializeAllForwardRefs = false; 1015*0b57cec5SDimitry Andric return Error::success(); 1016*0b57cec5SDimitry Andric } 1017*0b57cec5SDimitry Andric 1018*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1019*0b57cec5SDimitry Andric // Helper functions to implement forward reference resolution, etc. 1020*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1021*0b57cec5SDimitry Andric 1022*0b57cec5SDimitry Andric static bool hasImplicitComdat(size_t Val) { 1023*0b57cec5SDimitry Andric switch (Val) { 1024*0b57cec5SDimitry Andric default: 1025*0b57cec5SDimitry Andric return false; 1026*0b57cec5SDimitry Andric case 1: // Old WeakAnyLinkage 1027*0b57cec5SDimitry Andric case 4: // Old LinkOnceAnyLinkage 1028*0b57cec5SDimitry Andric case 10: // Old WeakODRLinkage 1029*0b57cec5SDimitry Andric case 11: // Old LinkOnceODRLinkage 1030*0b57cec5SDimitry Andric return true; 1031*0b57cec5SDimitry Andric } 1032*0b57cec5SDimitry Andric } 1033*0b57cec5SDimitry Andric 1034*0b57cec5SDimitry Andric static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) { 1035*0b57cec5SDimitry Andric switch (Val) { 1036*0b57cec5SDimitry Andric default: // Map unknown/new linkages to external 1037*0b57cec5SDimitry Andric case 0: 1038*0b57cec5SDimitry Andric return GlobalValue::ExternalLinkage; 1039*0b57cec5SDimitry Andric case 2: 1040*0b57cec5SDimitry Andric return GlobalValue::AppendingLinkage; 1041*0b57cec5SDimitry Andric case 3: 1042*0b57cec5SDimitry Andric return GlobalValue::InternalLinkage; 1043*0b57cec5SDimitry Andric case 5: 1044*0b57cec5SDimitry Andric return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage 1045*0b57cec5SDimitry Andric case 6: 1046*0b57cec5SDimitry Andric return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage 1047*0b57cec5SDimitry Andric case 7: 1048*0b57cec5SDimitry Andric return GlobalValue::ExternalWeakLinkage; 1049*0b57cec5SDimitry Andric case 8: 1050*0b57cec5SDimitry Andric return GlobalValue::CommonLinkage; 1051*0b57cec5SDimitry Andric case 9: 1052*0b57cec5SDimitry Andric return GlobalValue::PrivateLinkage; 1053*0b57cec5SDimitry Andric case 12: 1054*0b57cec5SDimitry Andric return GlobalValue::AvailableExternallyLinkage; 1055*0b57cec5SDimitry Andric case 13: 1056*0b57cec5SDimitry Andric return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage 1057*0b57cec5SDimitry Andric case 14: 1058*0b57cec5SDimitry Andric return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage 1059*0b57cec5SDimitry Andric case 15: 1060*0b57cec5SDimitry Andric return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage 1061*0b57cec5SDimitry Andric case 1: // Old value with implicit comdat. 1062*0b57cec5SDimitry Andric case 16: 1063*0b57cec5SDimitry Andric return GlobalValue::WeakAnyLinkage; 1064*0b57cec5SDimitry Andric case 10: // Old value with implicit comdat. 1065*0b57cec5SDimitry Andric case 17: 1066*0b57cec5SDimitry Andric return GlobalValue::WeakODRLinkage; 1067*0b57cec5SDimitry Andric case 4: // Old value with implicit comdat. 1068*0b57cec5SDimitry Andric case 18: 1069*0b57cec5SDimitry Andric return GlobalValue::LinkOnceAnyLinkage; 1070*0b57cec5SDimitry Andric case 11: // Old value with implicit comdat. 1071*0b57cec5SDimitry Andric case 19: 1072*0b57cec5SDimitry Andric return GlobalValue::LinkOnceODRLinkage; 1073*0b57cec5SDimitry Andric } 1074*0b57cec5SDimitry Andric } 1075*0b57cec5SDimitry Andric 1076*0b57cec5SDimitry Andric static FunctionSummary::FFlags getDecodedFFlags(uint64_t RawFlags) { 1077*0b57cec5SDimitry Andric FunctionSummary::FFlags Flags; 1078*0b57cec5SDimitry Andric Flags.ReadNone = RawFlags & 0x1; 1079*0b57cec5SDimitry Andric Flags.ReadOnly = (RawFlags >> 1) & 0x1; 1080*0b57cec5SDimitry Andric Flags.NoRecurse = (RawFlags >> 2) & 0x1; 1081*0b57cec5SDimitry Andric Flags.ReturnDoesNotAlias = (RawFlags >> 3) & 0x1; 1082*0b57cec5SDimitry Andric Flags.NoInline = (RawFlags >> 4) & 0x1; 1083480093f4SDimitry Andric Flags.AlwaysInline = (RawFlags >> 5) & 0x1; 1084349cc55cSDimitry Andric Flags.NoUnwind = (RawFlags >> 6) & 0x1; 1085349cc55cSDimitry Andric Flags.MayThrow = (RawFlags >> 7) & 0x1; 1086349cc55cSDimitry Andric Flags.HasUnknownCall = (RawFlags >> 8) & 0x1; 10870eae32dcSDimitry Andric Flags.MustBeUnreachable = (RawFlags >> 9) & 0x1; 1088*0b57cec5SDimitry Andric return Flags; 1089*0b57cec5SDimitry Andric } 1090*0b57cec5SDimitry Andric 1091fe6060f1SDimitry Andric // Decode the flags for GlobalValue in the summary. The bits for each attribute: 1092fe6060f1SDimitry Andric // 1093fe6060f1SDimitry Andric // linkage: [0,4), notEligibleToImport: 4, live: 5, local: 6, canAutoHide: 7, 1094fe6060f1SDimitry Andric // visibility: [8, 10). 1095*0b57cec5SDimitry Andric static GlobalValueSummary::GVFlags getDecodedGVSummaryFlags(uint64_t RawFlags, 1096*0b57cec5SDimitry Andric uint64_t Version) { 1097*0b57cec5SDimitry Andric // Summary were not emitted before LLVM 3.9, we don't need to upgrade Linkage 1098*0b57cec5SDimitry Andric // like getDecodedLinkage() above. Any future change to the linkage enum and 1099*0b57cec5SDimitry Andric // to getDecodedLinkage() will need to be taken into account here as above. 1100*0b57cec5SDimitry Andric auto Linkage = GlobalValue::LinkageTypes(RawFlags & 0xF); // 4 bits 1101fe6060f1SDimitry Andric auto Visibility = GlobalValue::VisibilityTypes((RawFlags >> 8) & 3); // 2 bits 1102*0b57cec5SDimitry Andric RawFlags = RawFlags >> 4; 1103*0b57cec5SDimitry Andric bool NotEligibleToImport = (RawFlags & 0x1) || Version < 3; 1104*0b57cec5SDimitry Andric // The Live flag wasn't introduced until version 3. For dead stripping 1105*0b57cec5SDimitry Andric // to work correctly on earlier versions, we must conservatively treat all 1106*0b57cec5SDimitry Andric // values as live. 1107*0b57cec5SDimitry Andric bool Live = (RawFlags & 0x2) || Version < 3; 1108*0b57cec5SDimitry Andric bool Local = (RawFlags & 0x4); 1109*0b57cec5SDimitry Andric bool AutoHide = (RawFlags & 0x8); 1110*0b57cec5SDimitry Andric 1111fe6060f1SDimitry Andric return GlobalValueSummary::GVFlags(Linkage, Visibility, NotEligibleToImport, 1112fe6060f1SDimitry Andric Live, Local, AutoHide); 1113*0b57cec5SDimitry Andric } 1114*0b57cec5SDimitry Andric 1115*0b57cec5SDimitry Andric // Decode the flags for GlobalVariable in the summary 1116*0b57cec5SDimitry Andric static GlobalVarSummary::GVarFlags getDecodedGVarFlags(uint64_t RawFlags) { 11175ffd83dbSDimitry Andric return GlobalVarSummary::GVarFlags( 11185ffd83dbSDimitry Andric (RawFlags & 0x1) ? true : false, (RawFlags & 0x2) ? true : false, 11195ffd83dbSDimitry Andric (RawFlags & 0x4) ? true : false, 11205ffd83dbSDimitry Andric (GlobalObject::VCallVisibility)(RawFlags >> 3)); 1121*0b57cec5SDimitry Andric } 1122*0b57cec5SDimitry Andric 1123*0b57cec5SDimitry Andric static GlobalValue::VisibilityTypes getDecodedVisibility(unsigned Val) { 1124*0b57cec5SDimitry Andric switch (Val) { 1125*0b57cec5SDimitry Andric default: // Map unknown visibilities to default. 1126*0b57cec5SDimitry Andric case 0: return GlobalValue::DefaultVisibility; 1127*0b57cec5SDimitry Andric case 1: return GlobalValue::HiddenVisibility; 1128*0b57cec5SDimitry Andric case 2: return GlobalValue::ProtectedVisibility; 1129*0b57cec5SDimitry Andric } 1130*0b57cec5SDimitry Andric } 1131*0b57cec5SDimitry Andric 1132*0b57cec5SDimitry Andric static GlobalValue::DLLStorageClassTypes 1133*0b57cec5SDimitry Andric getDecodedDLLStorageClass(unsigned Val) { 1134*0b57cec5SDimitry Andric switch (Val) { 1135*0b57cec5SDimitry Andric default: // Map unknown values to default. 1136*0b57cec5SDimitry Andric case 0: return GlobalValue::DefaultStorageClass; 1137*0b57cec5SDimitry Andric case 1: return GlobalValue::DLLImportStorageClass; 1138*0b57cec5SDimitry Andric case 2: return GlobalValue::DLLExportStorageClass; 1139*0b57cec5SDimitry Andric } 1140*0b57cec5SDimitry Andric } 1141*0b57cec5SDimitry Andric 1142*0b57cec5SDimitry Andric static bool getDecodedDSOLocal(unsigned Val) { 1143*0b57cec5SDimitry Andric switch(Val) { 1144*0b57cec5SDimitry Andric default: // Map unknown values to preemptable. 1145*0b57cec5SDimitry Andric case 0: return false; 1146*0b57cec5SDimitry Andric case 1: return true; 1147*0b57cec5SDimitry Andric } 1148*0b57cec5SDimitry Andric } 1149*0b57cec5SDimitry Andric 1150*0b57cec5SDimitry Andric static GlobalVariable::ThreadLocalMode getDecodedThreadLocalMode(unsigned Val) { 1151*0b57cec5SDimitry Andric switch (Val) { 1152*0b57cec5SDimitry Andric case 0: return GlobalVariable::NotThreadLocal; 1153*0b57cec5SDimitry Andric default: // Map unknown non-zero value to general dynamic. 1154*0b57cec5SDimitry Andric case 1: return GlobalVariable::GeneralDynamicTLSModel; 1155*0b57cec5SDimitry Andric case 2: return GlobalVariable::LocalDynamicTLSModel; 1156*0b57cec5SDimitry Andric case 3: return GlobalVariable::InitialExecTLSModel; 1157*0b57cec5SDimitry Andric case 4: return GlobalVariable::LocalExecTLSModel; 1158*0b57cec5SDimitry Andric } 1159*0b57cec5SDimitry Andric } 1160*0b57cec5SDimitry Andric 1161*0b57cec5SDimitry Andric static GlobalVariable::UnnamedAddr getDecodedUnnamedAddrType(unsigned Val) { 1162*0b57cec5SDimitry Andric switch (Val) { 1163*0b57cec5SDimitry Andric default: // Map unknown to UnnamedAddr::None. 1164*0b57cec5SDimitry Andric case 0: return GlobalVariable::UnnamedAddr::None; 1165*0b57cec5SDimitry Andric case 1: return GlobalVariable::UnnamedAddr::Global; 1166*0b57cec5SDimitry Andric case 2: return GlobalVariable::UnnamedAddr::Local; 1167*0b57cec5SDimitry Andric } 1168*0b57cec5SDimitry Andric } 1169*0b57cec5SDimitry Andric 1170*0b57cec5SDimitry Andric static int getDecodedCastOpcode(unsigned Val) { 1171*0b57cec5SDimitry Andric switch (Val) { 1172*0b57cec5SDimitry Andric default: return -1; 1173*0b57cec5SDimitry Andric case bitc::CAST_TRUNC : return Instruction::Trunc; 1174*0b57cec5SDimitry Andric case bitc::CAST_ZEXT : return Instruction::ZExt; 1175*0b57cec5SDimitry Andric case bitc::CAST_SEXT : return Instruction::SExt; 1176*0b57cec5SDimitry Andric case bitc::CAST_FPTOUI : return Instruction::FPToUI; 1177*0b57cec5SDimitry Andric case bitc::CAST_FPTOSI : return Instruction::FPToSI; 1178*0b57cec5SDimitry Andric case bitc::CAST_UITOFP : return Instruction::UIToFP; 1179*0b57cec5SDimitry Andric case bitc::CAST_SITOFP : return Instruction::SIToFP; 1180*0b57cec5SDimitry Andric case bitc::CAST_FPTRUNC : return Instruction::FPTrunc; 1181*0b57cec5SDimitry Andric case bitc::CAST_FPEXT : return Instruction::FPExt; 1182*0b57cec5SDimitry Andric case bitc::CAST_PTRTOINT: return Instruction::PtrToInt; 1183*0b57cec5SDimitry Andric case bitc::CAST_INTTOPTR: return Instruction::IntToPtr; 1184*0b57cec5SDimitry Andric case bitc::CAST_BITCAST : return Instruction::BitCast; 1185*0b57cec5SDimitry Andric case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast; 1186*0b57cec5SDimitry Andric } 1187*0b57cec5SDimitry Andric } 1188*0b57cec5SDimitry Andric 1189*0b57cec5SDimitry Andric static int getDecodedUnaryOpcode(unsigned Val, Type *Ty) { 1190*0b57cec5SDimitry Andric bool IsFP = Ty->isFPOrFPVectorTy(); 1191*0b57cec5SDimitry Andric // UnOps are only valid for int/fp or vector of int/fp types 1192*0b57cec5SDimitry Andric if (!IsFP && !Ty->isIntOrIntVectorTy()) 1193*0b57cec5SDimitry Andric return -1; 1194*0b57cec5SDimitry Andric 1195*0b57cec5SDimitry Andric switch (Val) { 1196*0b57cec5SDimitry Andric default: 1197*0b57cec5SDimitry Andric return -1; 11988bcb0991SDimitry Andric case bitc::UNOP_FNEG: 1199*0b57cec5SDimitry Andric return IsFP ? Instruction::FNeg : -1; 1200*0b57cec5SDimitry Andric } 1201*0b57cec5SDimitry Andric } 1202*0b57cec5SDimitry Andric 1203*0b57cec5SDimitry Andric static int getDecodedBinaryOpcode(unsigned Val, Type *Ty) { 1204*0b57cec5SDimitry Andric bool IsFP = Ty->isFPOrFPVectorTy(); 1205*0b57cec5SDimitry Andric // BinOps are only valid for int/fp or vector of int/fp types 1206*0b57cec5SDimitry Andric if (!IsFP && !Ty->isIntOrIntVectorTy()) 1207*0b57cec5SDimitry Andric return -1; 1208*0b57cec5SDimitry Andric 1209*0b57cec5SDimitry Andric switch (Val) { 1210*0b57cec5SDimitry Andric default: 1211*0b57cec5SDimitry Andric return -1; 1212*0b57cec5SDimitry Andric case bitc::BINOP_ADD: 1213*0b57cec5SDimitry Andric return IsFP ? Instruction::FAdd : Instruction::Add; 1214*0b57cec5SDimitry Andric case bitc::BINOP_SUB: 1215*0b57cec5SDimitry Andric return IsFP ? Instruction::FSub : Instruction::Sub; 1216*0b57cec5SDimitry Andric case bitc::BINOP_MUL: 1217*0b57cec5SDimitry Andric return IsFP ? Instruction::FMul : Instruction::Mul; 1218*0b57cec5SDimitry Andric case bitc::BINOP_UDIV: 1219*0b57cec5SDimitry Andric return IsFP ? -1 : Instruction::UDiv; 1220*0b57cec5SDimitry Andric case bitc::BINOP_SDIV: 1221*0b57cec5SDimitry Andric return IsFP ? Instruction::FDiv : Instruction::SDiv; 1222*0b57cec5SDimitry Andric case bitc::BINOP_UREM: 1223*0b57cec5SDimitry Andric return IsFP ? -1 : Instruction::URem; 1224*0b57cec5SDimitry Andric case bitc::BINOP_SREM: 1225*0b57cec5SDimitry Andric return IsFP ? Instruction::FRem : Instruction::SRem; 1226*0b57cec5SDimitry Andric case bitc::BINOP_SHL: 1227*0b57cec5SDimitry Andric return IsFP ? -1 : Instruction::Shl; 1228*0b57cec5SDimitry Andric case bitc::BINOP_LSHR: 1229*0b57cec5SDimitry Andric return IsFP ? -1 : Instruction::LShr; 1230*0b57cec5SDimitry Andric case bitc::BINOP_ASHR: 1231*0b57cec5SDimitry Andric return IsFP ? -1 : Instruction::AShr; 1232*0b57cec5SDimitry Andric case bitc::BINOP_AND: 1233*0b57cec5SDimitry Andric return IsFP ? -1 : Instruction::And; 1234*0b57cec5SDimitry Andric case bitc::BINOP_OR: 1235*0b57cec5SDimitry Andric return IsFP ? -1 : Instruction::Or; 1236*0b57cec5SDimitry Andric case bitc::BINOP_XOR: 1237*0b57cec5SDimitry Andric return IsFP ? -1 : Instruction::Xor; 1238*0b57cec5SDimitry Andric } 1239*0b57cec5SDimitry Andric } 1240*0b57cec5SDimitry Andric 1241*0b57cec5SDimitry Andric static AtomicRMWInst::BinOp getDecodedRMWOperation(unsigned Val) { 1242*0b57cec5SDimitry Andric switch (Val) { 1243*0b57cec5SDimitry Andric default: return AtomicRMWInst::BAD_BINOP; 1244*0b57cec5SDimitry Andric case bitc::RMW_XCHG: return AtomicRMWInst::Xchg; 1245*0b57cec5SDimitry Andric case bitc::RMW_ADD: return AtomicRMWInst::Add; 1246*0b57cec5SDimitry Andric case bitc::RMW_SUB: return AtomicRMWInst::Sub; 1247*0b57cec5SDimitry Andric case bitc::RMW_AND: return AtomicRMWInst::And; 1248*0b57cec5SDimitry Andric case bitc::RMW_NAND: return AtomicRMWInst::Nand; 1249*0b57cec5SDimitry Andric case bitc::RMW_OR: return AtomicRMWInst::Or; 1250*0b57cec5SDimitry Andric case bitc::RMW_XOR: return AtomicRMWInst::Xor; 1251*0b57cec5SDimitry Andric case bitc::RMW_MAX: return AtomicRMWInst::Max; 1252*0b57cec5SDimitry Andric case bitc::RMW_MIN: return AtomicRMWInst::Min; 1253*0b57cec5SDimitry Andric case bitc::RMW_UMAX: return AtomicRMWInst::UMax; 1254*0b57cec5SDimitry Andric case bitc::RMW_UMIN: return AtomicRMWInst::UMin; 1255*0b57cec5SDimitry Andric case bitc::RMW_FADD: return AtomicRMWInst::FAdd; 1256*0b57cec5SDimitry Andric case bitc::RMW_FSUB: return AtomicRMWInst::FSub; 1257753f127fSDimitry Andric case bitc::RMW_FMAX: return AtomicRMWInst::FMax; 1258753f127fSDimitry Andric case bitc::RMW_FMIN: return AtomicRMWInst::FMin; 1259bdd1243dSDimitry Andric case bitc::RMW_UINC_WRAP: 1260bdd1243dSDimitry Andric return AtomicRMWInst::UIncWrap; 1261bdd1243dSDimitry Andric case bitc::RMW_UDEC_WRAP: 1262bdd1243dSDimitry Andric return AtomicRMWInst::UDecWrap; 1263*0b57cec5SDimitry Andric } 1264*0b57cec5SDimitry Andric } 1265*0b57cec5SDimitry Andric 1266*0b57cec5SDimitry Andric static AtomicOrdering getDecodedOrdering(unsigned Val) { 1267*0b57cec5SDimitry Andric switch (Val) { 1268*0b57cec5SDimitry Andric case bitc::ORDERING_NOTATOMIC: return AtomicOrdering::NotAtomic; 1269*0b57cec5SDimitry Andric case bitc::ORDERING_UNORDERED: return AtomicOrdering::Unordered; 1270*0b57cec5SDimitry Andric case bitc::ORDERING_MONOTONIC: return AtomicOrdering::Monotonic; 1271*0b57cec5SDimitry Andric case bitc::ORDERING_ACQUIRE: return AtomicOrdering::Acquire; 1272*0b57cec5SDimitry Andric case bitc::ORDERING_RELEASE: return AtomicOrdering::Release; 1273*0b57cec5SDimitry Andric case bitc::ORDERING_ACQREL: return AtomicOrdering::AcquireRelease; 1274*0b57cec5SDimitry Andric default: // Map unknown orderings to sequentially-consistent. 1275*0b57cec5SDimitry Andric case bitc::ORDERING_SEQCST: return AtomicOrdering::SequentiallyConsistent; 1276*0b57cec5SDimitry Andric } 1277*0b57cec5SDimitry Andric } 1278*0b57cec5SDimitry Andric 1279*0b57cec5SDimitry Andric static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) { 1280*0b57cec5SDimitry Andric switch (Val) { 1281*0b57cec5SDimitry Andric default: // Map unknown selection kinds to any. 1282*0b57cec5SDimitry Andric case bitc::COMDAT_SELECTION_KIND_ANY: 1283*0b57cec5SDimitry Andric return Comdat::Any; 1284*0b57cec5SDimitry Andric case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH: 1285*0b57cec5SDimitry Andric return Comdat::ExactMatch; 1286*0b57cec5SDimitry Andric case bitc::COMDAT_SELECTION_KIND_LARGEST: 1287*0b57cec5SDimitry Andric return Comdat::Largest; 1288*0b57cec5SDimitry Andric case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES: 1289fe6060f1SDimitry Andric return Comdat::NoDeduplicate; 1290*0b57cec5SDimitry Andric case bitc::COMDAT_SELECTION_KIND_SAME_SIZE: 1291*0b57cec5SDimitry Andric return Comdat::SameSize; 1292*0b57cec5SDimitry Andric } 1293*0b57cec5SDimitry Andric } 1294*0b57cec5SDimitry Andric 1295*0b57cec5SDimitry Andric static FastMathFlags getDecodedFastMathFlags(unsigned Val) { 1296*0b57cec5SDimitry Andric FastMathFlags FMF; 1297*0b57cec5SDimitry Andric if (0 != (Val & bitc::UnsafeAlgebra)) 1298*0b57cec5SDimitry Andric FMF.setFast(); 1299*0b57cec5SDimitry Andric if (0 != (Val & bitc::AllowReassoc)) 1300*0b57cec5SDimitry Andric FMF.setAllowReassoc(); 1301*0b57cec5SDimitry Andric if (0 != (Val & bitc::NoNaNs)) 1302*0b57cec5SDimitry Andric FMF.setNoNaNs(); 1303*0b57cec5SDimitry Andric if (0 != (Val & bitc::NoInfs)) 1304*0b57cec5SDimitry Andric FMF.setNoInfs(); 1305*0b57cec5SDimitry Andric if (0 != (Val & bitc::NoSignedZeros)) 1306*0b57cec5SDimitry Andric FMF.setNoSignedZeros(); 1307*0b57cec5SDimitry Andric if (0 != (Val & bitc::AllowReciprocal)) 1308*0b57cec5SDimitry Andric FMF.setAllowReciprocal(); 1309*0b57cec5SDimitry Andric if (0 != (Val & bitc::AllowContract)) 1310*0b57cec5SDimitry Andric FMF.setAllowContract(true); 1311*0b57cec5SDimitry Andric if (0 != (Val & bitc::ApproxFunc)) 1312*0b57cec5SDimitry Andric FMF.setApproxFunc(); 1313*0b57cec5SDimitry Andric return FMF; 1314*0b57cec5SDimitry Andric } 1315*0b57cec5SDimitry Andric 1316*0b57cec5SDimitry Andric static void upgradeDLLImportExportLinkage(GlobalValue *GV, unsigned Val) { 1317bdd1243dSDimitry Andric // A GlobalValue with local linkage cannot have a DLL storage class. 1318bdd1243dSDimitry Andric if (GV->hasLocalLinkage()) 1319bdd1243dSDimitry Andric return; 1320*0b57cec5SDimitry Andric switch (Val) { 1321*0b57cec5SDimitry Andric case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break; 1322*0b57cec5SDimitry Andric case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break; 1323*0b57cec5SDimitry Andric } 1324*0b57cec5SDimitry Andric } 1325*0b57cec5SDimitry Andric 1326fe6060f1SDimitry Andric Type *BitcodeReader::getTypeByID(unsigned ID) { 1327*0b57cec5SDimitry Andric // The type table size is always specified correctly. 1328*0b57cec5SDimitry Andric if (ID >= TypeList.size()) 1329*0b57cec5SDimitry Andric return nullptr; 1330*0b57cec5SDimitry Andric 1331*0b57cec5SDimitry Andric if (Type *Ty = TypeList[ID]) 1332*0b57cec5SDimitry Andric return Ty; 1333*0b57cec5SDimitry Andric 1334*0b57cec5SDimitry Andric // If we have a forward reference, the only possible case is when it is to a 1335*0b57cec5SDimitry Andric // named struct. Just create a placeholder for now. 1336*0b57cec5SDimitry Andric return TypeList[ID] = createIdentifiedStructType(Context); 1337*0b57cec5SDimitry Andric } 1338*0b57cec5SDimitry Andric 133981ad6265SDimitry Andric unsigned BitcodeReader::getContainedTypeID(unsigned ID, unsigned Idx) { 134081ad6265SDimitry Andric auto It = ContainedTypeIDs.find(ID); 134181ad6265SDimitry Andric if (It == ContainedTypeIDs.end()) 134281ad6265SDimitry Andric return InvalidTypeID; 134381ad6265SDimitry Andric 134481ad6265SDimitry Andric if (Idx >= It->second.size()) 134581ad6265SDimitry Andric return InvalidTypeID; 134681ad6265SDimitry Andric 134781ad6265SDimitry Andric return It->second[Idx]; 134881ad6265SDimitry Andric } 134981ad6265SDimitry Andric 135081ad6265SDimitry Andric Type *BitcodeReader::getPtrElementTypeByID(unsigned ID) { 135181ad6265SDimitry Andric if (ID >= TypeList.size()) 135281ad6265SDimitry Andric return nullptr; 135381ad6265SDimitry Andric 135481ad6265SDimitry Andric Type *Ty = TypeList[ID]; 135581ad6265SDimitry Andric if (!Ty->isPointerTy()) 135681ad6265SDimitry Andric return nullptr; 135781ad6265SDimitry Andric 135881ad6265SDimitry Andric Type *ElemTy = getTypeByID(getContainedTypeID(ID, 0)); 135981ad6265SDimitry Andric if (!ElemTy) 136081ad6265SDimitry Andric return nullptr; 136181ad6265SDimitry Andric 136281ad6265SDimitry Andric assert(cast<PointerType>(Ty)->isOpaqueOrPointeeTypeMatches(ElemTy) && 136381ad6265SDimitry Andric "Incorrect element type"); 136481ad6265SDimitry Andric return ElemTy; 136581ad6265SDimitry Andric } 136681ad6265SDimitry Andric 136781ad6265SDimitry Andric unsigned BitcodeReader::getVirtualTypeID(Type *Ty, 136881ad6265SDimitry Andric ArrayRef<unsigned> ChildTypeIDs) { 136981ad6265SDimitry Andric unsigned ChildTypeID = ChildTypeIDs.empty() ? InvalidTypeID : ChildTypeIDs[0]; 137081ad6265SDimitry Andric auto CacheKey = std::make_pair(Ty, ChildTypeID); 137181ad6265SDimitry Andric auto It = VirtualTypeIDs.find(CacheKey); 137281ad6265SDimitry Andric if (It != VirtualTypeIDs.end()) { 137381ad6265SDimitry Andric // The cmpxchg return value is the only place we need more than one 137481ad6265SDimitry Andric // contained type ID, however the second one will always be the same (i1), 137581ad6265SDimitry Andric // so we don't need to include it in the cache key. This asserts that the 137681ad6265SDimitry Andric // contained types are indeed as expected and there are no collisions. 137781ad6265SDimitry Andric assert((ChildTypeIDs.empty() || 137881ad6265SDimitry Andric ContainedTypeIDs[It->second] == ChildTypeIDs) && 137981ad6265SDimitry Andric "Incorrect cached contained type IDs"); 138081ad6265SDimitry Andric return It->second; 138181ad6265SDimitry Andric } 138281ad6265SDimitry Andric 138381ad6265SDimitry Andric #ifndef NDEBUG 138481ad6265SDimitry Andric if (!Ty->isOpaquePointerTy()) { 138581ad6265SDimitry Andric assert(Ty->getNumContainedTypes() == ChildTypeIDs.size() && 138681ad6265SDimitry Andric "Wrong number of contained types"); 138781ad6265SDimitry Andric for (auto Pair : zip(Ty->subtypes(), ChildTypeIDs)) { 138881ad6265SDimitry Andric assert(std::get<0>(Pair) == getTypeByID(std::get<1>(Pair)) && 138981ad6265SDimitry Andric "Incorrect contained type ID"); 139081ad6265SDimitry Andric } 139181ad6265SDimitry Andric } 139281ad6265SDimitry Andric #endif 139381ad6265SDimitry Andric 139481ad6265SDimitry Andric unsigned TypeID = TypeList.size(); 139581ad6265SDimitry Andric TypeList.push_back(Ty); 139681ad6265SDimitry Andric if (!ChildTypeIDs.empty()) 139781ad6265SDimitry Andric append_range(ContainedTypeIDs[TypeID], ChildTypeIDs); 139881ad6265SDimitry Andric VirtualTypeIDs.insert({CacheKey, TypeID}); 139981ad6265SDimitry Andric return TypeID; 140081ad6265SDimitry Andric } 140181ad6265SDimitry Andric 140281ad6265SDimitry Andric static bool isConstExprSupported(uint8_t Opcode) { 140381ad6265SDimitry Andric // These are not real constant expressions, always consider them supported. 140481ad6265SDimitry Andric if (Opcode >= BitcodeConstant::FirstSpecialOpcode) 140581ad6265SDimitry Andric return true; 140681ad6265SDimitry Andric 1407bdd1243dSDimitry Andric // If -expand-constant-exprs is set, we want to consider all expressions 1408bdd1243dSDimitry Andric // as unsupported. 1409bdd1243dSDimitry Andric if (ExpandConstantExprs) 1410bdd1243dSDimitry Andric return false; 1411bdd1243dSDimitry Andric 1412753f127fSDimitry Andric if (Instruction::isBinaryOp(Opcode)) 1413753f127fSDimitry Andric return ConstantExpr::isSupportedBinOp(Opcode); 1414753f127fSDimitry Andric 1415bdd1243dSDimitry Andric return Opcode != Instruction::FNeg; 141681ad6265SDimitry Andric } 141781ad6265SDimitry Andric 141881ad6265SDimitry Andric Expected<Value *> BitcodeReader::materializeValue(unsigned StartValID, 141981ad6265SDimitry Andric BasicBlock *InsertBB) { 142081ad6265SDimitry Andric // Quickly handle the case where there is no BitcodeConstant to resolve. 142181ad6265SDimitry Andric if (StartValID < ValueList.size() && ValueList[StartValID] && 142281ad6265SDimitry Andric !isa<BitcodeConstant>(ValueList[StartValID])) 142381ad6265SDimitry Andric return ValueList[StartValID]; 142481ad6265SDimitry Andric 142581ad6265SDimitry Andric SmallDenseMap<unsigned, Value *> MaterializedValues; 142681ad6265SDimitry Andric SmallVector<unsigned> Worklist; 142781ad6265SDimitry Andric Worklist.push_back(StartValID); 142881ad6265SDimitry Andric while (!Worklist.empty()) { 142981ad6265SDimitry Andric unsigned ValID = Worklist.back(); 143081ad6265SDimitry Andric if (MaterializedValues.count(ValID)) { 143181ad6265SDimitry Andric // Duplicate expression that was already handled. 143281ad6265SDimitry Andric Worklist.pop_back(); 143381ad6265SDimitry Andric continue; 143481ad6265SDimitry Andric } 143581ad6265SDimitry Andric 143681ad6265SDimitry Andric if (ValID >= ValueList.size() || !ValueList[ValID]) 143781ad6265SDimitry Andric return error("Invalid value ID"); 143881ad6265SDimitry Andric 143981ad6265SDimitry Andric Value *V = ValueList[ValID]; 144081ad6265SDimitry Andric auto *BC = dyn_cast<BitcodeConstant>(V); 144181ad6265SDimitry Andric if (!BC) { 144281ad6265SDimitry Andric MaterializedValues.insert({ValID, V}); 144381ad6265SDimitry Andric Worklist.pop_back(); 144481ad6265SDimitry Andric continue; 144581ad6265SDimitry Andric } 144681ad6265SDimitry Andric 144781ad6265SDimitry Andric // Iterate in reverse, so values will get popped from the worklist in 144881ad6265SDimitry Andric // expected order. 144981ad6265SDimitry Andric SmallVector<Value *> Ops; 145081ad6265SDimitry Andric for (unsigned OpID : reverse(BC->getOperandIDs())) { 145181ad6265SDimitry Andric auto It = MaterializedValues.find(OpID); 145281ad6265SDimitry Andric if (It != MaterializedValues.end()) 145381ad6265SDimitry Andric Ops.push_back(It->second); 145481ad6265SDimitry Andric else 145581ad6265SDimitry Andric Worklist.push_back(OpID); 145681ad6265SDimitry Andric } 145781ad6265SDimitry Andric 145881ad6265SDimitry Andric // Some expressions have not been resolved yet, handle them first and then 145981ad6265SDimitry Andric // revisit this one. 146081ad6265SDimitry Andric if (Ops.size() != BC->getOperandIDs().size()) 146181ad6265SDimitry Andric continue; 146281ad6265SDimitry Andric std::reverse(Ops.begin(), Ops.end()); 146381ad6265SDimitry Andric 146481ad6265SDimitry Andric SmallVector<Constant *> ConstOps; 146581ad6265SDimitry Andric for (Value *Op : Ops) 146681ad6265SDimitry Andric if (auto *C = dyn_cast<Constant>(Op)) 146781ad6265SDimitry Andric ConstOps.push_back(C); 146881ad6265SDimitry Andric 146981ad6265SDimitry Andric // Materialize as constant expression if possible. 147081ad6265SDimitry Andric if (isConstExprSupported(BC->Opcode) && ConstOps.size() == Ops.size()) { 147181ad6265SDimitry Andric Constant *C; 147281ad6265SDimitry Andric if (Instruction::isCast(BC->Opcode)) { 147381ad6265SDimitry Andric C = UpgradeBitCastExpr(BC->Opcode, ConstOps[0], BC->getType()); 147481ad6265SDimitry Andric if (!C) 147581ad6265SDimitry Andric C = ConstantExpr::getCast(BC->Opcode, ConstOps[0], BC->getType()); 147681ad6265SDimitry Andric } else if (Instruction::isBinaryOp(BC->Opcode)) { 147781ad6265SDimitry Andric C = ConstantExpr::get(BC->Opcode, ConstOps[0], ConstOps[1], BC->Flags); 147881ad6265SDimitry Andric } else { 147981ad6265SDimitry Andric switch (BC->Opcode) { 148081ad6265SDimitry Andric case BitcodeConstant::NoCFIOpcode: { 148181ad6265SDimitry Andric auto *GV = dyn_cast<GlobalValue>(ConstOps[0]); 148281ad6265SDimitry Andric if (!GV) 148381ad6265SDimitry Andric return error("no_cfi operand must be GlobalValue"); 148481ad6265SDimitry Andric C = NoCFIValue::get(GV); 148581ad6265SDimitry Andric break; 148681ad6265SDimitry Andric } 148781ad6265SDimitry Andric case BitcodeConstant::DSOLocalEquivalentOpcode: { 148881ad6265SDimitry Andric auto *GV = dyn_cast<GlobalValue>(ConstOps[0]); 148981ad6265SDimitry Andric if (!GV) 149081ad6265SDimitry Andric return error("dso_local operand must be GlobalValue"); 149181ad6265SDimitry Andric C = DSOLocalEquivalent::get(GV); 149281ad6265SDimitry Andric break; 149381ad6265SDimitry Andric } 149481ad6265SDimitry Andric case BitcodeConstant::BlockAddressOpcode: { 149581ad6265SDimitry Andric Function *Fn = dyn_cast<Function>(ConstOps[0]); 149681ad6265SDimitry Andric if (!Fn) 149781ad6265SDimitry Andric return error("blockaddress operand must be a function"); 149881ad6265SDimitry Andric 149981ad6265SDimitry Andric // If the function is already parsed we can insert the block address 150081ad6265SDimitry Andric // right away. 150181ad6265SDimitry Andric BasicBlock *BB; 150281ad6265SDimitry Andric unsigned BBID = BC->Extra; 150381ad6265SDimitry Andric if (!BBID) 150481ad6265SDimitry Andric // Invalid reference to entry block. 150581ad6265SDimitry Andric return error("Invalid ID"); 150681ad6265SDimitry Andric if (!Fn->empty()) { 150781ad6265SDimitry Andric Function::iterator BBI = Fn->begin(), BBE = Fn->end(); 150881ad6265SDimitry Andric for (size_t I = 0, E = BBID; I != E; ++I) { 150981ad6265SDimitry Andric if (BBI == BBE) 151081ad6265SDimitry Andric return error("Invalid ID"); 151181ad6265SDimitry Andric ++BBI; 151281ad6265SDimitry Andric } 151381ad6265SDimitry Andric BB = &*BBI; 151481ad6265SDimitry Andric } else { 151581ad6265SDimitry Andric // Otherwise insert a placeholder and remember it so it can be 151681ad6265SDimitry Andric // inserted when the function is parsed. 151781ad6265SDimitry Andric auto &FwdBBs = BasicBlockFwdRefs[Fn]; 151881ad6265SDimitry Andric if (FwdBBs.empty()) 151981ad6265SDimitry Andric BasicBlockFwdRefQueue.push_back(Fn); 152081ad6265SDimitry Andric if (FwdBBs.size() < BBID + 1) 152181ad6265SDimitry Andric FwdBBs.resize(BBID + 1); 152281ad6265SDimitry Andric if (!FwdBBs[BBID]) 152381ad6265SDimitry Andric FwdBBs[BBID] = BasicBlock::Create(Context); 152481ad6265SDimitry Andric BB = FwdBBs[BBID]; 152581ad6265SDimitry Andric } 152681ad6265SDimitry Andric C = BlockAddress::get(Fn, BB); 152781ad6265SDimitry Andric break; 152881ad6265SDimitry Andric } 152981ad6265SDimitry Andric case BitcodeConstant::ConstantStructOpcode: 153081ad6265SDimitry Andric C = ConstantStruct::get(cast<StructType>(BC->getType()), ConstOps); 153181ad6265SDimitry Andric break; 153281ad6265SDimitry Andric case BitcodeConstant::ConstantArrayOpcode: 153381ad6265SDimitry Andric C = ConstantArray::get(cast<ArrayType>(BC->getType()), ConstOps); 153481ad6265SDimitry Andric break; 153581ad6265SDimitry Andric case BitcodeConstant::ConstantVectorOpcode: 153681ad6265SDimitry Andric C = ConstantVector::get(ConstOps); 153781ad6265SDimitry Andric break; 153881ad6265SDimitry Andric case Instruction::ICmp: 153981ad6265SDimitry Andric case Instruction::FCmp: 154081ad6265SDimitry Andric C = ConstantExpr::getCompare(BC->Flags, ConstOps[0], ConstOps[1]); 154181ad6265SDimitry Andric break; 154281ad6265SDimitry Andric case Instruction::GetElementPtr: 1543bdd1243dSDimitry Andric C = ConstantExpr::getGetElementPtr(BC->SrcElemTy, ConstOps[0], 1544bdd1243dSDimitry Andric ArrayRef(ConstOps).drop_front(), 154581ad6265SDimitry Andric BC->Flags, BC->getInRangeIndex()); 154681ad6265SDimitry Andric break; 154781ad6265SDimitry Andric case Instruction::Select: 154881ad6265SDimitry Andric C = ConstantExpr::getSelect(ConstOps[0], ConstOps[1], ConstOps[2]); 154981ad6265SDimitry Andric break; 155081ad6265SDimitry Andric case Instruction::ExtractElement: 155181ad6265SDimitry Andric C = ConstantExpr::getExtractElement(ConstOps[0], ConstOps[1]); 155281ad6265SDimitry Andric break; 155381ad6265SDimitry Andric case Instruction::InsertElement: 155481ad6265SDimitry Andric C = ConstantExpr::getInsertElement(ConstOps[0], ConstOps[1], 155581ad6265SDimitry Andric ConstOps[2]); 155681ad6265SDimitry Andric break; 155781ad6265SDimitry Andric case Instruction::ShuffleVector: { 155881ad6265SDimitry Andric SmallVector<int, 16> Mask; 155981ad6265SDimitry Andric ShuffleVectorInst::getShuffleMask(ConstOps[2], Mask); 156081ad6265SDimitry Andric C = ConstantExpr::getShuffleVector(ConstOps[0], ConstOps[1], Mask); 156181ad6265SDimitry Andric break; 156281ad6265SDimitry Andric } 156381ad6265SDimitry Andric default: 156481ad6265SDimitry Andric llvm_unreachable("Unhandled bitcode constant"); 156581ad6265SDimitry Andric } 156681ad6265SDimitry Andric } 156781ad6265SDimitry Andric 156881ad6265SDimitry Andric // Cache resolved constant. 156981ad6265SDimitry Andric ValueList.replaceValueWithoutRAUW(ValID, C); 157081ad6265SDimitry Andric MaterializedValues.insert({ValID, C}); 157181ad6265SDimitry Andric Worklist.pop_back(); 157281ad6265SDimitry Andric continue; 157381ad6265SDimitry Andric } 157481ad6265SDimitry Andric 157581ad6265SDimitry Andric if (!InsertBB) 157681ad6265SDimitry Andric return error(Twine("Value referenced by initializer is an unsupported " 157781ad6265SDimitry Andric "constant expression of type ") + 157881ad6265SDimitry Andric BC->getOpcodeName()); 157981ad6265SDimitry Andric 158081ad6265SDimitry Andric // Materialize as instructions if necessary. 158181ad6265SDimitry Andric Instruction *I; 158281ad6265SDimitry Andric if (Instruction::isCast(BC->Opcode)) { 158381ad6265SDimitry Andric I = CastInst::Create((Instruction::CastOps)BC->Opcode, Ops[0], 158481ad6265SDimitry Andric BC->getType(), "constexpr", InsertBB); 158581ad6265SDimitry Andric } else if (Instruction::isUnaryOp(BC->Opcode)) { 158681ad6265SDimitry Andric I = UnaryOperator::Create((Instruction::UnaryOps)BC->Opcode, Ops[0], 158781ad6265SDimitry Andric "constexpr", InsertBB); 158881ad6265SDimitry Andric } else if (Instruction::isBinaryOp(BC->Opcode)) { 158981ad6265SDimitry Andric I = BinaryOperator::Create((Instruction::BinaryOps)BC->Opcode, Ops[0], 159081ad6265SDimitry Andric Ops[1], "constexpr", InsertBB); 159181ad6265SDimitry Andric if (isa<OverflowingBinaryOperator>(I)) { 159281ad6265SDimitry Andric if (BC->Flags & OverflowingBinaryOperator::NoSignedWrap) 159381ad6265SDimitry Andric I->setHasNoSignedWrap(); 159481ad6265SDimitry Andric if (BC->Flags & OverflowingBinaryOperator::NoUnsignedWrap) 159581ad6265SDimitry Andric I->setHasNoUnsignedWrap(); 159681ad6265SDimitry Andric } 159781ad6265SDimitry Andric if (isa<PossiblyExactOperator>(I) && 159881ad6265SDimitry Andric (BC->Flags & PossiblyExactOperator::IsExact)) 159981ad6265SDimitry Andric I->setIsExact(); 160081ad6265SDimitry Andric } else { 160181ad6265SDimitry Andric switch (BC->Opcode) { 160281ad6265SDimitry Andric case BitcodeConstant::ConstantVectorOpcode: { 160381ad6265SDimitry Andric Type *IdxTy = Type::getInt32Ty(BC->getContext()); 160481ad6265SDimitry Andric Value *V = PoisonValue::get(BC->getType()); 160581ad6265SDimitry Andric for (auto Pair : enumerate(Ops)) { 160681ad6265SDimitry Andric Value *Idx = ConstantInt::get(IdxTy, Pair.index()); 160781ad6265SDimitry Andric V = InsertElementInst::Create(V, Pair.value(), Idx, "constexpr.ins", 160881ad6265SDimitry Andric InsertBB); 160981ad6265SDimitry Andric } 161081ad6265SDimitry Andric I = cast<Instruction>(V); 161181ad6265SDimitry Andric break; 161281ad6265SDimitry Andric } 1613bdd1243dSDimitry Andric case BitcodeConstant::ConstantStructOpcode: 1614bdd1243dSDimitry Andric case BitcodeConstant::ConstantArrayOpcode: { 1615bdd1243dSDimitry Andric Value *V = PoisonValue::get(BC->getType()); 1616bdd1243dSDimitry Andric for (auto Pair : enumerate(Ops)) 1617bdd1243dSDimitry Andric V = InsertValueInst::Create(V, Pair.value(), Pair.index(), 1618bdd1243dSDimitry Andric "constexpr.ins", InsertBB); 1619bdd1243dSDimitry Andric I = cast<Instruction>(V); 1620bdd1243dSDimitry Andric break; 1621bdd1243dSDimitry Andric } 162281ad6265SDimitry Andric case Instruction::ICmp: 162381ad6265SDimitry Andric case Instruction::FCmp: 162481ad6265SDimitry Andric I = CmpInst::Create((Instruction::OtherOps)BC->Opcode, 162581ad6265SDimitry Andric (CmpInst::Predicate)BC->Flags, Ops[0], Ops[1], 162681ad6265SDimitry Andric "constexpr", InsertBB); 162781ad6265SDimitry Andric break; 162881ad6265SDimitry Andric case Instruction::GetElementPtr: 162981ad6265SDimitry Andric I = GetElementPtrInst::Create(BC->SrcElemTy, Ops[0], 1630bdd1243dSDimitry Andric ArrayRef(Ops).drop_front(), "constexpr", 1631bdd1243dSDimitry Andric InsertBB); 163281ad6265SDimitry Andric if (BC->Flags) 163381ad6265SDimitry Andric cast<GetElementPtrInst>(I)->setIsInBounds(); 163481ad6265SDimitry Andric break; 163581ad6265SDimitry Andric case Instruction::Select: 163681ad6265SDimitry Andric I = SelectInst::Create(Ops[0], Ops[1], Ops[2], "constexpr", InsertBB); 163781ad6265SDimitry Andric break; 163881ad6265SDimitry Andric case Instruction::ExtractElement: 163981ad6265SDimitry Andric I = ExtractElementInst::Create(Ops[0], Ops[1], "constexpr", InsertBB); 164081ad6265SDimitry Andric break; 164181ad6265SDimitry Andric case Instruction::InsertElement: 164281ad6265SDimitry Andric I = InsertElementInst::Create(Ops[0], Ops[1], Ops[2], "constexpr", 164381ad6265SDimitry Andric InsertBB); 164481ad6265SDimitry Andric break; 164581ad6265SDimitry Andric case Instruction::ShuffleVector: 164681ad6265SDimitry Andric I = new ShuffleVectorInst(Ops[0], Ops[1], Ops[2], "constexpr", 164781ad6265SDimitry Andric InsertBB); 164881ad6265SDimitry Andric break; 164981ad6265SDimitry Andric default: 165081ad6265SDimitry Andric llvm_unreachable("Unhandled bitcode constant"); 165181ad6265SDimitry Andric } 165281ad6265SDimitry Andric } 165381ad6265SDimitry Andric 165481ad6265SDimitry Andric MaterializedValues.insert({ValID, I}); 165581ad6265SDimitry Andric Worklist.pop_back(); 165681ad6265SDimitry Andric } 165781ad6265SDimitry Andric 165881ad6265SDimitry Andric return MaterializedValues[StartValID]; 165981ad6265SDimitry Andric } 166081ad6265SDimitry Andric 166181ad6265SDimitry Andric Expected<Constant *> BitcodeReader::getValueForInitializer(unsigned ID) { 166281ad6265SDimitry Andric Expected<Value *> MaybeV = materializeValue(ID, /* InsertBB */ nullptr); 166381ad6265SDimitry Andric if (!MaybeV) 166481ad6265SDimitry Andric return MaybeV.takeError(); 166581ad6265SDimitry Andric 166681ad6265SDimitry Andric // Result must be Constant if InsertBB is nullptr. 166781ad6265SDimitry Andric return cast<Constant>(MaybeV.get()); 166881ad6265SDimitry Andric } 166981ad6265SDimitry Andric 1670*0b57cec5SDimitry Andric StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context, 1671*0b57cec5SDimitry Andric StringRef Name) { 1672*0b57cec5SDimitry Andric auto *Ret = StructType::create(Context, Name); 1673*0b57cec5SDimitry Andric IdentifiedStructTypes.push_back(Ret); 1674*0b57cec5SDimitry Andric return Ret; 1675*0b57cec5SDimitry Andric } 1676*0b57cec5SDimitry Andric 1677*0b57cec5SDimitry Andric StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) { 1678*0b57cec5SDimitry Andric auto *Ret = StructType::create(Context); 1679*0b57cec5SDimitry Andric IdentifiedStructTypes.push_back(Ret); 1680*0b57cec5SDimitry Andric return Ret; 1681*0b57cec5SDimitry Andric } 1682*0b57cec5SDimitry Andric 1683*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1684*0b57cec5SDimitry Andric // Functions for parsing blocks from the bitcode file 1685*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1686*0b57cec5SDimitry Andric 1687*0b57cec5SDimitry Andric static uint64_t getRawAttributeMask(Attribute::AttrKind Val) { 1688*0b57cec5SDimitry Andric switch (Val) { 1689*0b57cec5SDimitry Andric case Attribute::EndAttrKinds: 16905ffd83dbSDimitry Andric case Attribute::EmptyKey: 16915ffd83dbSDimitry Andric case Attribute::TombstoneKey: 1692*0b57cec5SDimitry Andric llvm_unreachable("Synthetic enumerators which should never get here"); 1693*0b57cec5SDimitry Andric 1694*0b57cec5SDimitry Andric case Attribute::None: return 0; 1695*0b57cec5SDimitry Andric case Attribute::ZExt: return 1 << 0; 1696*0b57cec5SDimitry Andric case Attribute::SExt: return 1 << 1; 1697*0b57cec5SDimitry Andric case Attribute::NoReturn: return 1 << 2; 1698*0b57cec5SDimitry Andric case Attribute::InReg: return 1 << 3; 1699*0b57cec5SDimitry Andric case Attribute::StructRet: return 1 << 4; 1700*0b57cec5SDimitry Andric case Attribute::NoUnwind: return 1 << 5; 1701*0b57cec5SDimitry Andric case Attribute::NoAlias: return 1 << 6; 1702*0b57cec5SDimitry Andric case Attribute::ByVal: return 1 << 7; 1703*0b57cec5SDimitry Andric case Attribute::Nest: return 1 << 8; 1704*0b57cec5SDimitry Andric case Attribute::ReadNone: return 1 << 9; 1705*0b57cec5SDimitry Andric case Attribute::ReadOnly: return 1 << 10; 1706*0b57cec5SDimitry Andric case Attribute::NoInline: return 1 << 11; 1707*0b57cec5SDimitry Andric case Attribute::AlwaysInline: return 1 << 12; 1708*0b57cec5SDimitry Andric case Attribute::OptimizeForSize: return 1 << 13; 1709*0b57cec5SDimitry Andric case Attribute::StackProtect: return 1 << 14; 1710*0b57cec5SDimitry Andric case Attribute::StackProtectReq: return 1 << 15; 1711*0b57cec5SDimitry Andric case Attribute::Alignment: return 31 << 16; 1712*0b57cec5SDimitry Andric case Attribute::NoCapture: return 1 << 21; 1713*0b57cec5SDimitry Andric case Attribute::NoRedZone: return 1 << 22; 1714*0b57cec5SDimitry Andric case Attribute::NoImplicitFloat: return 1 << 23; 1715*0b57cec5SDimitry Andric case Attribute::Naked: return 1 << 24; 1716*0b57cec5SDimitry Andric case Attribute::InlineHint: return 1 << 25; 1717*0b57cec5SDimitry Andric case Attribute::StackAlignment: return 7 << 26; 1718*0b57cec5SDimitry Andric case Attribute::ReturnsTwice: return 1 << 29; 1719*0b57cec5SDimitry Andric case Attribute::UWTable: return 1 << 30; 1720*0b57cec5SDimitry Andric case Attribute::NonLazyBind: return 1U << 31; 1721*0b57cec5SDimitry Andric case Attribute::SanitizeAddress: return 1ULL << 32; 1722*0b57cec5SDimitry Andric case Attribute::MinSize: return 1ULL << 33; 1723*0b57cec5SDimitry Andric case Attribute::NoDuplicate: return 1ULL << 34; 1724*0b57cec5SDimitry Andric case Attribute::StackProtectStrong: return 1ULL << 35; 1725*0b57cec5SDimitry Andric case Attribute::SanitizeThread: return 1ULL << 36; 1726*0b57cec5SDimitry Andric case Attribute::SanitizeMemory: return 1ULL << 37; 1727*0b57cec5SDimitry Andric case Attribute::NoBuiltin: return 1ULL << 38; 1728*0b57cec5SDimitry Andric case Attribute::Returned: return 1ULL << 39; 1729*0b57cec5SDimitry Andric case Attribute::Cold: return 1ULL << 40; 1730*0b57cec5SDimitry Andric case Attribute::Builtin: return 1ULL << 41; 1731*0b57cec5SDimitry Andric case Attribute::OptimizeNone: return 1ULL << 42; 1732*0b57cec5SDimitry Andric case Attribute::InAlloca: return 1ULL << 43; 1733*0b57cec5SDimitry Andric case Attribute::NonNull: return 1ULL << 44; 1734*0b57cec5SDimitry Andric case Attribute::JumpTable: return 1ULL << 45; 1735*0b57cec5SDimitry Andric case Attribute::Convergent: return 1ULL << 46; 1736*0b57cec5SDimitry Andric case Attribute::SafeStack: return 1ULL << 47; 1737*0b57cec5SDimitry Andric case Attribute::NoRecurse: return 1ULL << 48; 1738bdd1243dSDimitry Andric // 1ULL << 49 is InaccessibleMemOnly, which is upgraded separately. 1739bdd1243dSDimitry Andric // 1ULL << 50 is InaccessibleMemOrArgMemOnly, which is upgraded separately. 1740*0b57cec5SDimitry Andric case Attribute::SwiftSelf: return 1ULL << 51; 1741*0b57cec5SDimitry Andric case Attribute::SwiftError: return 1ULL << 52; 1742*0b57cec5SDimitry Andric case Attribute::WriteOnly: return 1ULL << 53; 1743*0b57cec5SDimitry Andric case Attribute::Speculatable: return 1ULL << 54; 1744*0b57cec5SDimitry Andric case Attribute::StrictFP: return 1ULL << 55; 1745*0b57cec5SDimitry Andric case Attribute::SanitizeHWAddress: return 1ULL << 56; 1746*0b57cec5SDimitry Andric case Attribute::NoCfCheck: return 1ULL << 57; 1747*0b57cec5SDimitry Andric case Attribute::OptForFuzzing: return 1ULL << 58; 1748*0b57cec5SDimitry Andric case Attribute::ShadowCallStack: return 1ULL << 59; 1749*0b57cec5SDimitry Andric case Attribute::SpeculativeLoadHardening: 1750*0b57cec5SDimitry Andric return 1ULL << 60; 1751*0b57cec5SDimitry Andric case Attribute::ImmArg: 1752*0b57cec5SDimitry Andric return 1ULL << 61; 1753*0b57cec5SDimitry Andric case Attribute::WillReturn: 1754*0b57cec5SDimitry Andric return 1ULL << 62; 1755*0b57cec5SDimitry Andric case Attribute::NoFree: 1756*0b57cec5SDimitry Andric return 1ULL << 63; 17575ffd83dbSDimitry Andric default: 17585ffd83dbSDimitry Andric // Other attributes are not supported in the raw format, 17595ffd83dbSDimitry Andric // as we ran out of space. 17605ffd83dbSDimitry Andric return 0; 1761*0b57cec5SDimitry Andric } 1762*0b57cec5SDimitry Andric llvm_unreachable("Unsupported attribute type"); 1763*0b57cec5SDimitry Andric } 1764*0b57cec5SDimitry Andric 1765*0b57cec5SDimitry Andric static void addRawAttributeValue(AttrBuilder &B, uint64_t Val) { 1766*0b57cec5SDimitry Andric if (!Val) return; 1767*0b57cec5SDimitry Andric 1768*0b57cec5SDimitry Andric for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds; 1769*0b57cec5SDimitry Andric I = Attribute::AttrKind(I + 1)) { 1770*0b57cec5SDimitry Andric if (uint64_t A = (Val & getRawAttributeMask(I))) { 1771*0b57cec5SDimitry Andric if (I == Attribute::Alignment) 1772*0b57cec5SDimitry Andric B.addAlignmentAttr(1ULL << ((A >> 16) - 1)); 1773*0b57cec5SDimitry Andric else if (I == Attribute::StackAlignment) 1774*0b57cec5SDimitry Andric B.addStackAlignmentAttr(1ULL << ((A >> 26)-1)); 1775fe6060f1SDimitry Andric else if (Attribute::isTypeAttrKind(I)) 1776fe6060f1SDimitry Andric B.addTypeAttr(I, nullptr); // Type will be auto-upgraded. 1777*0b57cec5SDimitry Andric else 1778*0b57cec5SDimitry Andric B.addAttribute(I); 1779*0b57cec5SDimitry Andric } 1780*0b57cec5SDimitry Andric } 1781*0b57cec5SDimitry Andric } 1782*0b57cec5SDimitry Andric 1783*0b57cec5SDimitry Andric /// This fills an AttrBuilder object with the LLVM attributes that have 1784*0b57cec5SDimitry Andric /// been decoded from the given integer. This function must stay in sync with 1785*0b57cec5SDimitry Andric /// 'encodeLLVMAttributesForBitcode'. 1786*0b57cec5SDimitry Andric static void decodeLLVMAttributesForBitcode(AttrBuilder &B, 1787bdd1243dSDimitry Andric uint64_t EncodedAttrs, 1788bdd1243dSDimitry Andric uint64_t AttrIdx) { 1789*0b57cec5SDimitry Andric // The alignment is stored as a 16-bit raw value from bits 31--16. We shift 1790*0b57cec5SDimitry Andric // the bits above 31 down by 11 bits. 1791*0b57cec5SDimitry Andric unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16; 1792*0b57cec5SDimitry Andric assert((!Alignment || isPowerOf2_32(Alignment)) && 1793*0b57cec5SDimitry Andric "Alignment must be a power of two."); 1794*0b57cec5SDimitry Andric 1795*0b57cec5SDimitry Andric if (Alignment) 1796*0b57cec5SDimitry Andric B.addAlignmentAttr(Alignment); 1797bdd1243dSDimitry Andric 1798bdd1243dSDimitry Andric uint64_t Attrs = ((EncodedAttrs & (0xfffffULL << 32)) >> 11) | 1799bdd1243dSDimitry Andric (EncodedAttrs & 0xffff); 1800bdd1243dSDimitry Andric 1801bdd1243dSDimitry Andric if (AttrIdx == AttributeList::FunctionIndex) { 1802bdd1243dSDimitry Andric // Upgrade old memory attributes. 1803bdd1243dSDimitry Andric MemoryEffects ME = MemoryEffects::unknown(); 1804bdd1243dSDimitry Andric if (Attrs & (1ULL << 9)) { 1805bdd1243dSDimitry Andric // ReadNone 1806bdd1243dSDimitry Andric Attrs &= ~(1ULL << 9); 1807bdd1243dSDimitry Andric ME &= MemoryEffects::none(); 1808bdd1243dSDimitry Andric } 1809bdd1243dSDimitry Andric if (Attrs & (1ULL << 10)) { 1810bdd1243dSDimitry Andric // ReadOnly 1811bdd1243dSDimitry Andric Attrs &= ~(1ULL << 10); 1812bdd1243dSDimitry Andric ME &= MemoryEffects::readOnly(); 1813bdd1243dSDimitry Andric } 1814bdd1243dSDimitry Andric if (Attrs & (1ULL << 49)) { 1815bdd1243dSDimitry Andric // InaccessibleMemOnly 1816bdd1243dSDimitry Andric Attrs &= ~(1ULL << 49); 1817bdd1243dSDimitry Andric ME &= MemoryEffects::inaccessibleMemOnly(); 1818bdd1243dSDimitry Andric } 1819bdd1243dSDimitry Andric if (Attrs & (1ULL << 50)) { 1820bdd1243dSDimitry Andric // InaccessibleMemOrArgMemOnly 1821bdd1243dSDimitry Andric Attrs &= ~(1ULL << 50); 1822bdd1243dSDimitry Andric ME &= MemoryEffects::inaccessibleOrArgMemOnly(); 1823bdd1243dSDimitry Andric } 1824bdd1243dSDimitry Andric if (Attrs & (1ULL << 53)) { 1825bdd1243dSDimitry Andric // WriteOnly 1826bdd1243dSDimitry Andric Attrs &= ~(1ULL << 53); 1827bdd1243dSDimitry Andric ME &= MemoryEffects::writeOnly(); 1828bdd1243dSDimitry Andric } 1829bdd1243dSDimitry Andric if (ME != MemoryEffects::unknown()) 1830bdd1243dSDimitry Andric B.addMemoryAttr(ME); 1831bdd1243dSDimitry Andric } 1832bdd1243dSDimitry Andric 1833bdd1243dSDimitry Andric addRawAttributeValue(B, Attrs); 1834*0b57cec5SDimitry Andric } 1835*0b57cec5SDimitry Andric 1836*0b57cec5SDimitry Andric Error BitcodeReader::parseAttributeBlock() { 1837*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID)) 1838*0b57cec5SDimitry Andric return Err; 1839*0b57cec5SDimitry Andric 1840*0b57cec5SDimitry Andric if (!MAttributes.empty()) 1841*0b57cec5SDimitry Andric return error("Invalid multiple blocks"); 1842*0b57cec5SDimitry Andric 1843*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 1844*0b57cec5SDimitry Andric 1845*0b57cec5SDimitry Andric SmallVector<AttributeList, 8> Attrs; 1846*0b57cec5SDimitry Andric 1847*0b57cec5SDimitry Andric // Read all the records. 1848*0b57cec5SDimitry Andric while (true) { 1849*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 1850*0b57cec5SDimitry Andric if (!MaybeEntry) 1851*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 1852*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 1853*0b57cec5SDimitry Andric 1854*0b57cec5SDimitry Andric switch (Entry.Kind) { 1855*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 1856*0b57cec5SDimitry Andric case BitstreamEntry::Error: 1857*0b57cec5SDimitry Andric return error("Malformed block"); 1858*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 1859*0b57cec5SDimitry Andric return Error::success(); 1860*0b57cec5SDimitry Andric case BitstreamEntry::Record: 1861*0b57cec5SDimitry Andric // The interesting case. 1862*0b57cec5SDimitry Andric break; 1863*0b57cec5SDimitry Andric } 1864*0b57cec5SDimitry Andric 1865*0b57cec5SDimitry Andric // Read a record. 1866*0b57cec5SDimitry Andric Record.clear(); 1867*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 1868*0b57cec5SDimitry Andric if (!MaybeRecord) 1869*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 1870*0b57cec5SDimitry Andric switch (MaybeRecord.get()) { 1871*0b57cec5SDimitry Andric default: // Default behavior: ignore. 1872*0b57cec5SDimitry Andric break; 1873*0b57cec5SDimitry Andric case bitc::PARAMATTR_CODE_ENTRY_OLD: // ENTRY: [paramidx0, attr0, ...] 18745ffd83dbSDimitry Andric // Deprecated, but still needed to read old bitcode files. 1875*0b57cec5SDimitry Andric if (Record.size() & 1) 187681ad6265SDimitry Andric return error("Invalid parameter attribute record"); 1877*0b57cec5SDimitry Andric 1878*0b57cec5SDimitry Andric for (unsigned i = 0, e = Record.size(); i != e; i += 2) { 187904eeddc0SDimitry Andric AttrBuilder B(Context); 1880bdd1243dSDimitry Andric decodeLLVMAttributesForBitcode(B, Record[i+1], Record[i]); 1881*0b57cec5SDimitry Andric Attrs.push_back(AttributeList::get(Context, Record[i], B)); 1882*0b57cec5SDimitry Andric } 1883*0b57cec5SDimitry Andric 1884*0b57cec5SDimitry Andric MAttributes.push_back(AttributeList::get(Context, Attrs)); 1885*0b57cec5SDimitry Andric Attrs.clear(); 1886*0b57cec5SDimitry Andric break; 1887*0b57cec5SDimitry Andric case bitc::PARAMATTR_CODE_ENTRY: // ENTRY: [attrgrp0, attrgrp1, ...] 1888*0b57cec5SDimitry Andric for (unsigned i = 0, e = Record.size(); i != e; ++i) 1889*0b57cec5SDimitry Andric Attrs.push_back(MAttributeGroups[Record[i]]); 1890*0b57cec5SDimitry Andric 1891*0b57cec5SDimitry Andric MAttributes.push_back(AttributeList::get(Context, Attrs)); 1892*0b57cec5SDimitry Andric Attrs.clear(); 1893*0b57cec5SDimitry Andric break; 1894*0b57cec5SDimitry Andric } 1895*0b57cec5SDimitry Andric } 1896*0b57cec5SDimitry Andric } 1897*0b57cec5SDimitry Andric 1898*0b57cec5SDimitry Andric // Returns Attribute::None on unrecognized codes. 1899*0b57cec5SDimitry Andric static Attribute::AttrKind getAttrFromCode(uint64_t Code) { 1900*0b57cec5SDimitry Andric switch (Code) { 1901*0b57cec5SDimitry Andric default: 1902*0b57cec5SDimitry Andric return Attribute::None; 1903*0b57cec5SDimitry Andric case bitc::ATTR_KIND_ALIGNMENT: 1904*0b57cec5SDimitry Andric return Attribute::Alignment; 1905*0b57cec5SDimitry Andric case bitc::ATTR_KIND_ALWAYS_INLINE: 1906*0b57cec5SDimitry Andric return Attribute::AlwaysInline; 1907*0b57cec5SDimitry Andric case bitc::ATTR_KIND_BUILTIN: 1908*0b57cec5SDimitry Andric return Attribute::Builtin; 1909*0b57cec5SDimitry Andric case bitc::ATTR_KIND_BY_VAL: 1910*0b57cec5SDimitry Andric return Attribute::ByVal; 1911*0b57cec5SDimitry Andric case bitc::ATTR_KIND_IN_ALLOCA: 1912*0b57cec5SDimitry Andric return Attribute::InAlloca; 1913*0b57cec5SDimitry Andric case bitc::ATTR_KIND_COLD: 1914*0b57cec5SDimitry Andric return Attribute::Cold; 1915*0b57cec5SDimitry Andric case bitc::ATTR_KIND_CONVERGENT: 1916*0b57cec5SDimitry Andric return Attribute::Convergent; 1917349cc55cSDimitry Andric case bitc::ATTR_KIND_DISABLE_SANITIZER_INSTRUMENTATION: 1918349cc55cSDimitry Andric return Attribute::DisableSanitizerInstrumentation; 1919fe6060f1SDimitry Andric case bitc::ATTR_KIND_ELEMENTTYPE: 1920fe6060f1SDimitry Andric return Attribute::ElementType; 1921753f127fSDimitry Andric case bitc::ATTR_KIND_FNRETTHUNK_EXTERN: 1922753f127fSDimitry Andric return Attribute::FnRetThunkExtern; 1923*0b57cec5SDimitry Andric case bitc::ATTR_KIND_INLINE_HINT: 1924*0b57cec5SDimitry Andric return Attribute::InlineHint; 1925*0b57cec5SDimitry Andric case bitc::ATTR_KIND_IN_REG: 1926*0b57cec5SDimitry Andric return Attribute::InReg; 1927*0b57cec5SDimitry Andric case bitc::ATTR_KIND_JUMP_TABLE: 1928*0b57cec5SDimitry Andric return Attribute::JumpTable; 1929bdd1243dSDimitry Andric case bitc::ATTR_KIND_MEMORY: 1930bdd1243dSDimitry Andric return Attribute::Memory; 1931*0b57cec5SDimitry Andric case bitc::ATTR_KIND_MIN_SIZE: 1932*0b57cec5SDimitry Andric return Attribute::MinSize; 1933*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NAKED: 1934*0b57cec5SDimitry Andric return Attribute::Naked; 1935*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NEST: 1936*0b57cec5SDimitry Andric return Attribute::Nest; 1937*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NO_ALIAS: 1938*0b57cec5SDimitry Andric return Attribute::NoAlias; 1939*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NO_BUILTIN: 1940*0b57cec5SDimitry Andric return Attribute::NoBuiltin; 1941e8d8bef9SDimitry Andric case bitc::ATTR_KIND_NO_CALLBACK: 1942e8d8bef9SDimitry Andric return Attribute::NoCallback; 1943*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NO_CAPTURE: 1944*0b57cec5SDimitry Andric return Attribute::NoCapture; 1945*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NO_DUPLICATE: 1946*0b57cec5SDimitry Andric return Attribute::NoDuplicate; 1947*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NOFREE: 1948*0b57cec5SDimitry Andric return Attribute::NoFree; 1949*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT: 1950*0b57cec5SDimitry Andric return Attribute::NoImplicitFloat; 1951*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NO_INLINE: 1952*0b57cec5SDimitry Andric return Attribute::NoInline; 1953*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NO_RECURSE: 1954*0b57cec5SDimitry Andric return Attribute::NoRecurse; 19555ffd83dbSDimitry Andric case bitc::ATTR_KIND_NO_MERGE: 19565ffd83dbSDimitry Andric return Attribute::NoMerge; 1957*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NON_LAZY_BIND: 1958*0b57cec5SDimitry Andric return Attribute::NonLazyBind; 1959*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NON_NULL: 1960*0b57cec5SDimitry Andric return Attribute::NonNull; 1961*0b57cec5SDimitry Andric case bitc::ATTR_KIND_DEREFERENCEABLE: 1962*0b57cec5SDimitry Andric return Attribute::Dereferenceable; 1963*0b57cec5SDimitry Andric case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL: 1964*0b57cec5SDimitry Andric return Attribute::DereferenceableOrNull; 196581ad6265SDimitry Andric case bitc::ATTR_KIND_ALLOC_ALIGN: 196681ad6265SDimitry Andric return Attribute::AllocAlign; 196781ad6265SDimitry Andric case bitc::ATTR_KIND_ALLOC_KIND: 196881ad6265SDimitry Andric return Attribute::AllocKind; 1969*0b57cec5SDimitry Andric case bitc::ATTR_KIND_ALLOC_SIZE: 1970*0b57cec5SDimitry Andric return Attribute::AllocSize; 197181ad6265SDimitry Andric case bitc::ATTR_KIND_ALLOCATED_POINTER: 197281ad6265SDimitry Andric return Attribute::AllocatedPointer; 1973*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NO_RED_ZONE: 1974*0b57cec5SDimitry Andric return Attribute::NoRedZone; 1975*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NO_RETURN: 1976*0b57cec5SDimitry Andric return Attribute::NoReturn; 1977*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NOSYNC: 1978*0b57cec5SDimitry Andric return Attribute::NoSync; 1979*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NOCF_CHECK: 1980*0b57cec5SDimitry Andric return Attribute::NoCfCheck; 1981fe6060f1SDimitry Andric case bitc::ATTR_KIND_NO_PROFILE: 1982fe6060f1SDimitry Andric return Attribute::NoProfile; 1983bdd1243dSDimitry Andric case bitc::ATTR_KIND_SKIP_PROFILE: 1984bdd1243dSDimitry Andric return Attribute::SkipProfile; 1985*0b57cec5SDimitry Andric case bitc::ATTR_KIND_NO_UNWIND: 1986*0b57cec5SDimitry Andric return Attribute::NoUnwind; 198781ad6265SDimitry Andric case bitc::ATTR_KIND_NO_SANITIZE_BOUNDS: 198881ad6265SDimitry Andric return Attribute::NoSanitizeBounds; 1989fe6060f1SDimitry Andric case bitc::ATTR_KIND_NO_SANITIZE_COVERAGE: 1990fe6060f1SDimitry Andric return Attribute::NoSanitizeCoverage; 19915ffd83dbSDimitry Andric case bitc::ATTR_KIND_NULL_POINTER_IS_VALID: 19925ffd83dbSDimitry Andric return Attribute::NullPointerIsValid; 1993*0b57cec5SDimitry Andric case bitc::ATTR_KIND_OPT_FOR_FUZZING: 1994*0b57cec5SDimitry Andric return Attribute::OptForFuzzing; 1995*0b57cec5SDimitry Andric case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE: 1996*0b57cec5SDimitry Andric return Attribute::OptimizeForSize; 1997*0b57cec5SDimitry Andric case bitc::ATTR_KIND_OPTIMIZE_NONE: 1998*0b57cec5SDimitry Andric return Attribute::OptimizeNone; 1999*0b57cec5SDimitry Andric case bitc::ATTR_KIND_READ_NONE: 2000*0b57cec5SDimitry Andric return Attribute::ReadNone; 2001*0b57cec5SDimitry Andric case bitc::ATTR_KIND_READ_ONLY: 2002*0b57cec5SDimitry Andric return Attribute::ReadOnly; 2003*0b57cec5SDimitry Andric case bitc::ATTR_KIND_RETURNED: 2004*0b57cec5SDimitry Andric return Attribute::Returned; 2005*0b57cec5SDimitry Andric case bitc::ATTR_KIND_RETURNS_TWICE: 2006*0b57cec5SDimitry Andric return Attribute::ReturnsTwice; 2007*0b57cec5SDimitry Andric case bitc::ATTR_KIND_S_EXT: 2008*0b57cec5SDimitry Andric return Attribute::SExt; 2009*0b57cec5SDimitry Andric case bitc::ATTR_KIND_SPECULATABLE: 2010*0b57cec5SDimitry Andric return Attribute::Speculatable; 2011*0b57cec5SDimitry Andric case bitc::ATTR_KIND_STACK_ALIGNMENT: 2012*0b57cec5SDimitry Andric return Attribute::StackAlignment; 2013*0b57cec5SDimitry Andric case bitc::ATTR_KIND_STACK_PROTECT: 2014*0b57cec5SDimitry Andric return Attribute::StackProtect; 2015*0b57cec5SDimitry Andric case bitc::ATTR_KIND_STACK_PROTECT_REQ: 2016*0b57cec5SDimitry Andric return Attribute::StackProtectReq; 2017*0b57cec5SDimitry Andric case bitc::ATTR_KIND_STACK_PROTECT_STRONG: 2018*0b57cec5SDimitry Andric return Attribute::StackProtectStrong; 2019*0b57cec5SDimitry Andric case bitc::ATTR_KIND_SAFESTACK: 2020*0b57cec5SDimitry Andric return Attribute::SafeStack; 2021*0b57cec5SDimitry Andric case bitc::ATTR_KIND_SHADOWCALLSTACK: 2022*0b57cec5SDimitry Andric return Attribute::ShadowCallStack; 2023*0b57cec5SDimitry Andric case bitc::ATTR_KIND_STRICT_FP: 2024*0b57cec5SDimitry Andric return Attribute::StrictFP; 2025*0b57cec5SDimitry Andric case bitc::ATTR_KIND_STRUCT_RET: 2026*0b57cec5SDimitry Andric return Attribute::StructRet; 2027*0b57cec5SDimitry Andric case bitc::ATTR_KIND_SANITIZE_ADDRESS: 2028*0b57cec5SDimitry Andric return Attribute::SanitizeAddress; 2029*0b57cec5SDimitry Andric case bitc::ATTR_KIND_SANITIZE_HWADDRESS: 2030*0b57cec5SDimitry Andric return Attribute::SanitizeHWAddress; 2031*0b57cec5SDimitry Andric case bitc::ATTR_KIND_SANITIZE_THREAD: 2032*0b57cec5SDimitry Andric return Attribute::SanitizeThread; 2033*0b57cec5SDimitry Andric case bitc::ATTR_KIND_SANITIZE_MEMORY: 2034*0b57cec5SDimitry Andric return Attribute::SanitizeMemory; 2035*0b57cec5SDimitry Andric case bitc::ATTR_KIND_SPECULATIVE_LOAD_HARDENING: 2036*0b57cec5SDimitry Andric return Attribute::SpeculativeLoadHardening; 2037*0b57cec5SDimitry Andric case bitc::ATTR_KIND_SWIFT_ERROR: 2038*0b57cec5SDimitry Andric return Attribute::SwiftError; 2039*0b57cec5SDimitry Andric case bitc::ATTR_KIND_SWIFT_SELF: 2040*0b57cec5SDimitry Andric return Attribute::SwiftSelf; 2041fe6060f1SDimitry Andric case bitc::ATTR_KIND_SWIFT_ASYNC: 2042fe6060f1SDimitry Andric return Attribute::SwiftAsync; 2043*0b57cec5SDimitry Andric case bitc::ATTR_KIND_UW_TABLE: 2044*0b57cec5SDimitry Andric return Attribute::UWTable; 2045fe6060f1SDimitry Andric case bitc::ATTR_KIND_VSCALE_RANGE: 2046fe6060f1SDimitry Andric return Attribute::VScaleRange; 2047*0b57cec5SDimitry Andric case bitc::ATTR_KIND_WILLRETURN: 2048*0b57cec5SDimitry Andric return Attribute::WillReturn; 2049*0b57cec5SDimitry Andric case bitc::ATTR_KIND_WRITEONLY: 2050*0b57cec5SDimitry Andric return Attribute::WriteOnly; 2051*0b57cec5SDimitry Andric case bitc::ATTR_KIND_Z_EXT: 2052*0b57cec5SDimitry Andric return Attribute::ZExt; 2053*0b57cec5SDimitry Andric case bitc::ATTR_KIND_IMMARG: 2054*0b57cec5SDimitry Andric return Attribute::ImmArg; 2055*0b57cec5SDimitry Andric case bitc::ATTR_KIND_SANITIZE_MEMTAG: 2056*0b57cec5SDimitry Andric return Attribute::SanitizeMemTag; 20575ffd83dbSDimitry Andric case bitc::ATTR_KIND_PREALLOCATED: 20585ffd83dbSDimitry Andric return Attribute::Preallocated; 20595ffd83dbSDimitry Andric case bitc::ATTR_KIND_NOUNDEF: 20605ffd83dbSDimitry Andric return Attribute::NoUndef; 2061e8d8bef9SDimitry Andric case bitc::ATTR_KIND_BYREF: 2062e8d8bef9SDimitry Andric return Attribute::ByRef; 2063e8d8bef9SDimitry Andric case bitc::ATTR_KIND_MUSTPROGRESS: 2064e8d8bef9SDimitry Andric return Attribute::MustProgress; 2065e8d8bef9SDimitry Andric case bitc::ATTR_KIND_HOT: 2066e8d8bef9SDimitry Andric return Attribute::Hot; 206781ad6265SDimitry Andric case bitc::ATTR_KIND_PRESPLIT_COROUTINE: 206881ad6265SDimitry Andric return Attribute::PresplitCoroutine; 2069*0b57cec5SDimitry Andric } 2070*0b57cec5SDimitry Andric } 2071*0b57cec5SDimitry Andric 2072*0b57cec5SDimitry Andric Error BitcodeReader::parseAlignmentValue(uint64_t Exponent, 20738bcb0991SDimitry Andric MaybeAlign &Alignment) { 2074*0b57cec5SDimitry Andric // Note: Alignment in bitcode files is incremented by 1, so that zero 2075*0b57cec5SDimitry Andric // can be used for default alignment. 2076*0b57cec5SDimitry Andric if (Exponent > Value::MaxAlignmentExponent + 1) 2077*0b57cec5SDimitry Andric return error("Invalid alignment value"); 20788bcb0991SDimitry Andric Alignment = decodeMaybeAlign(Exponent); 2079*0b57cec5SDimitry Andric return Error::success(); 2080*0b57cec5SDimitry Andric } 2081*0b57cec5SDimitry Andric 2082*0b57cec5SDimitry Andric Error BitcodeReader::parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind) { 2083*0b57cec5SDimitry Andric *Kind = getAttrFromCode(Code); 2084*0b57cec5SDimitry Andric if (*Kind == Attribute::None) 2085*0b57cec5SDimitry Andric return error("Unknown attribute kind (" + Twine(Code) + ")"); 2086*0b57cec5SDimitry Andric return Error::success(); 2087*0b57cec5SDimitry Andric } 2088*0b57cec5SDimitry Andric 2089bdd1243dSDimitry Andric static bool upgradeOldMemoryAttribute(MemoryEffects &ME, uint64_t EncodedKind) { 2090bdd1243dSDimitry Andric switch (EncodedKind) { 2091bdd1243dSDimitry Andric case bitc::ATTR_KIND_READ_NONE: 2092bdd1243dSDimitry Andric ME &= MemoryEffects::none(); 2093bdd1243dSDimitry Andric return true; 2094bdd1243dSDimitry Andric case bitc::ATTR_KIND_READ_ONLY: 2095bdd1243dSDimitry Andric ME &= MemoryEffects::readOnly(); 2096bdd1243dSDimitry Andric return true; 2097bdd1243dSDimitry Andric case bitc::ATTR_KIND_WRITEONLY: 2098bdd1243dSDimitry Andric ME &= MemoryEffects::writeOnly(); 2099bdd1243dSDimitry Andric return true; 2100bdd1243dSDimitry Andric case bitc::ATTR_KIND_ARGMEMONLY: 2101bdd1243dSDimitry Andric ME &= MemoryEffects::argMemOnly(); 2102bdd1243dSDimitry Andric return true; 2103bdd1243dSDimitry Andric case bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY: 2104bdd1243dSDimitry Andric ME &= MemoryEffects::inaccessibleMemOnly(); 2105bdd1243dSDimitry Andric return true; 2106bdd1243dSDimitry Andric case bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY: 2107bdd1243dSDimitry Andric ME &= MemoryEffects::inaccessibleOrArgMemOnly(); 2108bdd1243dSDimitry Andric return true; 2109bdd1243dSDimitry Andric default: 2110bdd1243dSDimitry Andric return false; 2111bdd1243dSDimitry Andric } 2112bdd1243dSDimitry Andric } 2113bdd1243dSDimitry Andric 2114*0b57cec5SDimitry Andric Error BitcodeReader::parseAttributeGroupBlock() { 2115*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID)) 2116*0b57cec5SDimitry Andric return Err; 2117*0b57cec5SDimitry Andric 2118*0b57cec5SDimitry Andric if (!MAttributeGroups.empty()) 2119*0b57cec5SDimitry Andric return error("Invalid multiple blocks"); 2120*0b57cec5SDimitry Andric 2121*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 2122*0b57cec5SDimitry Andric 2123*0b57cec5SDimitry Andric // Read all the records. 2124*0b57cec5SDimitry Andric while (true) { 2125*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 2126*0b57cec5SDimitry Andric if (!MaybeEntry) 2127*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 2128*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 2129*0b57cec5SDimitry Andric 2130*0b57cec5SDimitry Andric switch (Entry.Kind) { 2131*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 2132*0b57cec5SDimitry Andric case BitstreamEntry::Error: 2133*0b57cec5SDimitry Andric return error("Malformed block"); 2134*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 2135*0b57cec5SDimitry Andric return Error::success(); 2136*0b57cec5SDimitry Andric case BitstreamEntry::Record: 2137*0b57cec5SDimitry Andric // The interesting case. 2138*0b57cec5SDimitry Andric break; 2139*0b57cec5SDimitry Andric } 2140*0b57cec5SDimitry Andric 2141*0b57cec5SDimitry Andric // Read a record. 2142*0b57cec5SDimitry Andric Record.clear(); 2143*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 2144*0b57cec5SDimitry Andric if (!MaybeRecord) 2145*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 2146*0b57cec5SDimitry Andric switch (MaybeRecord.get()) { 2147*0b57cec5SDimitry Andric default: // Default behavior: ignore. 2148*0b57cec5SDimitry Andric break; 2149*0b57cec5SDimitry Andric case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...] 2150*0b57cec5SDimitry Andric if (Record.size() < 3) 215181ad6265SDimitry Andric return error("Invalid grp record"); 2152*0b57cec5SDimitry Andric 2153*0b57cec5SDimitry Andric uint64_t GrpID = Record[0]; 2154*0b57cec5SDimitry Andric uint64_t Idx = Record[1]; // Index of the object this attribute refers to. 2155*0b57cec5SDimitry Andric 215604eeddc0SDimitry Andric AttrBuilder B(Context); 2157bdd1243dSDimitry Andric MemoryEffects ME = MemoryEffects::unknown(); 2158*0b57cec5SDimitry Andric for (unsigned i = 2, e = Record.size(); i != e; ++i) { 2159*0b57cec5SDimitry Andric if (Record[i] == 0) { // Enum attribute 2160*0b57cec5SDimitry Andric Attribute::AttrKind Kind; 2161bdd1243dSDimitry Andric uint64_t EncodedKind = Record[++i]; 2162bdd1243dSDimitry Andric if (Idx == AttributeList::FunctionIndex && 2163bdd1243dSDimitry Andric upgradeOldMemoryAttribute(ME, EncodedKind)) 2164bdd1243dSDimitry Andric continue; 2165bdd1243dSDimitry Andric 2166bdd1243dSDimitry Andric if (Error Err = parseAttrKind(EncodedKind, &Kind)) 2167*0b57cec5SDimitry Andric return Err; 2168*0b57cec5SDimitry Andric 2169*0b57cec5SDimitry Andric // Upgrade old-style byval attribute to one with a type, even if it's 2170*0b57cec5SDimitry Andric // nullptr. We will have to insert the real type when we associate 2171*0b57cec5SDimitry Andric // this AttributeList with a function. 2172*0b57cec5SDimitry Andric if (Kind == Attribute::ByVal) 2173*0b57cec5SDimitry Andric B.addByValAttr(nullptr); 2174e8d8bef9SDimitry Andric else if (Kind == Attribute::StructRet) 2175e8d8bef9SDimitry Andric B.addStructRetAttr(nullptr); 2176fe6060f1SDimitry Andric else if (Kind == Attribute::InAlloca) 2177fe6060f1SDimitry Andric B.addInAllocaAttr(nullptr); 217881ad6265SDimitry Andric else if (Kind == Attribute::UWTable) 217981ad6265SDimitry Andric B.addUWTableAttr(UWTableKind::Default); 2180fe6060f1SDimitry Andric else if (Attribute::isEnumAttrKind(Kind)) 2181*0b57cec5SDimitry Andric B.addAttribute(Kind); 2182fe6060f1SDimitry Andric else 2183fe6060f1SDimitry Andric return error("Not an enum attribute"); 2184*0b57cec5SDimitry Andric } else if (Record[i] == 1) { // Integer attribute 2185*0b57cec5SDimitry Andric Attribute::AttrKind Kind; 2186*0b57cec5SDimitry Andric if (Error Err = parseAttrKind(Record[++i], &Kind)) 2187*0b57cec5SDimitry Andric return Err; 2188fe6060f1SDimitry Andric if (!Attribute::isIntAttrKind(Kind)) 2189fe6060f1SDimitry Andric return error("Not an int attribute"); 2190*0b57cec5SDimitry Andric if (Kind == Attribute::Alignment) 2191*0b57cec5SDimitry Andric B.addAlignmentAttr(Record[++i]); 2192*0b57cec5SDimitry Andric else if (Kind == Attribute::StackAlignment) 2193*0b57cec5SDimitry Andric B.addStackAlignmentAttr(Record[++i]); 2194*0b57cec5SDimitry Andric else if (Kind == Attribute::Dereferenceable) 2195*0b57cec5SDimitry Andric B.addDereferenceableAttr(Record[++i]); 2196*0b57cec5SDimitry Andric else if (Kind == Attribute::DereferenceableOrNull) 2197*0b57cec5SDimitry Andric B.addDereferenceableOrNullAttr(Record[++i]); 2198*0b57cec5SDimitry Andric else if (Kind == Attribute::AllocSize) 2199*0b57cec5SDimitry Andric B.addAllocSizeAttrFromRawRepr(Record[++i]); 2200fe6060f1SDimitry Andric else if (Kind == Attribute::VScaleRange) 2201fe6060f1SDimitry Andric B.addVScaleRangeAttrFromRawRepr(Record[++i]); 220281ad6265SDimitry Andric else if (Kind == Attribute::UWTable) 220381ad6265SDimitry Andric B.addUWTableAttr(UWTableKind(Record[++i])); 220481ad6265SDimitry Andric else if (Kind == Attribute::AllocKind) 220581ad6265SDimitry Andric B.addAllocKindAttr(static_cast<AllocFnKind>(Record[++i])); 2206bdd1243dSDimitry Andric else if (Kind == Attribute::Memory) 2207bdd1243dSDimitry Andric B.addMemoryAttr(MemoryEffects::createFromIntValue(Record[++i])); 2208*0b57cec5SDimitry Andric } else if (Record[i] == 3 || Record[i] == 4) { // String attribute 2209*0b57cec5SDimitry Andric bool HasValue = (Record[i++] == 4); 2210*0b57cec5SDimitry Andric SmallString<64> KindStr; 2211*0b57cec5SDimitry Andric SmallString<64> ValStr; 2212*0b57cec5SDimitry Andric 2213*0b57cec5SDimitry Andric while (Record[i] != 0 && i != e) 2214*0b57cec5SDimitry Andric KindStr += Record[i++]; 2215*0b57cec5SDimitry Andric assert(Record[i] == 0 && "Kind string not null terminated"); 2216*0b57cec5SDimitry Andric 2217*0b57cec5SDimitry Andric if (HasValue) { 2218*0b57cec5SDimitry Andric // Has a value associated with it. 2219*0b57cec5SDimitry Andric ++i; // Skip the '0' that terminates the "kind" string. 2220*0b57cec5SDimitry Andric while (Record[i] != 0 && i != e) 2221*0b57cec5SDimitry Andric ValStr += Record[i++]; 2222*0b57cec5SDimitry Andric assert(Record[i] == 0 && "Value string not null terminated"); 2223*0b57cec5SDimitry Andric } 2224*0b57cec5SDimitry Andric 2225*0b57cec5SDimitry Andric B.addAttribute(KindStr.str(), ValStr.str()); 222681ad6265SDimitry Andric } else if (Record[i] == 5 || Record[i] == 6) { 2227*0b57cec5SDimitry Andric bool HasType = Record[i] == 6; 2228*0b57cec5SDimitry Andric Attribute::AttrKind Kind; 2229*0b57cec5SDimitry Andric if (Error Err = parseAttrKind(Record[++i], &Kind)) 2230*0b57cec5SDimitry Andric return Err; 2231fe6060f1SDimitry Andric if (!Attribute::isTypeAttrKind(Kind)) 2232fe6060f1SDimitry Andric return error("Not a type attribute"); 2233fe6060f1SDimitry Andric 2234fe6060f1SDimitry Andric B.addTypeAttr(Kind, HasType ? getTypeByID(Record[++i]) : nullptr); 223581ad6265SDimitry Andric } else { 223681ad6265SDimitry Andric return error("Invalid attribute group entry"); 2237*0b57cec5SDimitry Andric } 2238*0b57cec5SDimitry Andric } 2239*0b57cec5SDimitry Andric 2240bdd1243dSDimitry Andric if (ME != MemoryEffects::unknown()) 2241bdd1243dSDimitry Andric B.addMemoryAttr(ME); 2242bdd1243dSDimitry Andric 22435ffd83dbSDimitry Andric UpgradeAttributes(B); 2244*0b57cec5SDimitry Andric MAttributeGroups[GrpID] = AttributeList::get(Context, Idx, B); 2245*0b57cec5SDimitry Andric break; 2246*0b57cec5SDimitry Andric } 2247*0b57cec5SDimitry Andric } 2248*0b57cec5SDimitry Andric } 2249*0b57cec5SDimitry Andric } 2250*0b57cec5SDimitry Andric 2251*0b57cec5SDimitry Andric Error BitcodeReader::parseTypeTable() { 2252*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW)) 2253*0b57cec5SDimitry Andric return Err; 2254*0b57cec5SDimitry Andric 2255*0b57cec5SDimitry Andric return parseTypeTableBody(); 2256*0b57cec5SDimitry Andric } 2257*0b57cec5SDimitry Andric 2258*0b57cec5SDimitry Andric Error BitcodeReader::parseTypeTableBody() { 2259*0b57cec5SDimitry Andric if (!TypeList.empty()) 2260*0b57cec5SDimitry Andric return error("Invalid multiple blocks"); 2261*0b57cec5SDimitry Andric 2262*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 2263*0b57cec5SDimitry Andric unsigned NumRecords = 0; 2264*0b57cec5SDimitry Andric 2265*0b57cec5SDimitry Andric SmallString<64> TypeName; 2266*0b57cec5SDimitry Andric 2267*0b57cec5SDimitry Andric // Read all the records for this type table. 2268*0b57cec5SDimitry Andric while (true) { 2269*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 2270*0b57cec5SDimitry Andric if (!MaybeEntry) 2271*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 2272*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 2273*0b57cec5SDimitry Andric 2274*0b57cec5SDimitry Andric switch (Entry.Kind) { 2275*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 2276*0b57cec5SDimitry Andric case BitstreamEntry::Error: 2277*0b57cec5SDimitry Andric return error("Malformed block"); 2278*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 2279*0b57cec5SDimitry Andric if (NumRecords != TypeList.size()) 2280*0b57cec5SDimitry Andric return error("Malformed block"); 2281*0b57cec5SDimitry Andric return Error::success(); 2282*0b57cec5SDimitry Andric case BitstreamEntry::Record: 2283*0b57cec5SDimitry Andric // The interesting case. 2284*0b57cec5SDimitry Andric break; 2285*0b57cec5SDimitry Andric } 2286*0b57cec5SDimitry Andric 2287*0b57cec5SDimitry Andric // Read a record. 2288*0b57cec5SDimitry Andric Record.clear(); 2289*0b57cec5SDimitry Andric Type *ResultTy = nullptr; 229081ad6265SDimitry Andric SmallVector<unsigned> ContainedIDs; 2291*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 2292*0b57cec5SDimitry Andric if (!MaybeRecord) 2293*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 2294*0b57cec5SDimitry Andric switch (MaybeRecord.get()) { 2295*0b57cec5SDimitry Andric default: 2296*0b57cec5SDimitry Andric return error("Invalid value"); 2297*0b57cec5SDimitry Andric case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries] 2298*0b57cec5SDimitry Andric // TYPE_CODE_NUMENTRY contains a count of the number of types in the 2299*0b57cec5SDimitry Andric // type list. This allows us to reserve space. 2300e8d8bef9SDimitry Andric if (Record.empty()) 230181ad6265SDimitry Andric return error("Invalid numentry record"); 2302*0b57cec5SDimitry Andric TypeList.resize(Record[0]); 2303*0b57cec5SDimitry Andric continue; 2304*0b57cec5SDimitry Andric case bitc::TYPE_CODE_VOID: // VOID 2305*0b57cec5SDimitry Andric ResultTy = Type::getVoidTy(Context); 2306*0b57cec5SDimitry Andric break; 2307*0b57cec5SDimitry Andric case bitc::TYPE_CODE_HALF: // HALF 2308*0b57cec5SDimitry Andric ResultTy = Type::getHalfTy(Context); 2309*0b57cec5SDimitry Andric break; 23105ffd83dbSDimitry Andric case bitc::TYPE_CODE_BFLOAT: // BFLOAT 23115ffd83dbSDimitry Andric ResultTy = Type::getBFloatTy(Context); 23125ffd83dbSDimitry Andric break; 2313*0b57cec5SDimitry Andric case bitc::TYPE_CODE_FLOAT: // FLOAT 2314*0b57cec5SDimitry Andric ResultTy = Type::getFloatTy(Context); 2315*0b57cec5SDimitry Andric break; 2316*0b57cec5SDimitry Andric case bitc::TYPE_CODE_DOUBLE: // DOUBLE 2317*0b57cec5SDimitry Andric ResultTy = Type::getDoubleTy(Context); 2318*0b57cec5SDimitry Andric break; 2319*0b57cec5SDimitry Andric case bitc::TYPE_CODE_X86_FP80: // X86_FP80 2320*0b57cec5SDimitry Andric ResultTy = Type::getX86_FP80Ty(Context); 2321*0b57cec5SDimitry Andric break; 2322*0b57cec5SDimitry Andric case bitc::TYPE_CODE_FP128: // FP128 2323*0b57cec5SDimitry Andric ResultTy = Type::getFP128Ty(Context); 2324*0b57cec5SDimitry Andric break; 2325*0b57cec5SDimitry Andric case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128 2326*0b57cec5SDimitry Andric ResultTy = Type::getPPC_FP128Ty(Context); 2327*0b57cec5SDimitry Andric break; 2328*0b57cec5SDimitry Andric case bitc::TYPE_CODE_LABEL: // LABEL 2329*0b57cec5SDimitry Andric ResultTy = Type::getLabelTy(Context); 2330*0b57cec5SDimitry Andric break; 2331*0b57cec5SDimitry Andric case bitc::TYPE_CODE_METADATA: // METADATA 2332*0b57cec5SDimitry Andric ResultTy = Type::getMetadataTy(Context); 2333*0b57cec5SDimitry Andric break; 2334*0b57cec5SDimitry Andric case bitc::TYPE_CODE_X86_MMX: // X86_MMX 2335*0b57cec5SDimitry Andric ResultTy = Type::getX86_MMXTy(Context); 2336*0b57cec5SDimitry Andric break; 2337e8d8bef9SDimitry Andric case bitc::TYPE_CODE_X86_AMX: // X86_AMX 2338e8d8bef9SDimitry Andric ResultTy = Type::getX86_AMXTy(Context); 2339e8d8bef9SDimitry Andric break; 2340*0b57cec5SDimitry Andric case bitc::TYPE_CODE_TOKEN: // TOKEN 2341*0b57cec5SDimitry Andric ResultTy = Type::getTokenTy(Context); 2342*0b57cec5SDimitry Andric break; 2343*0b57cec5SDimitry Andric case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width] 2344e8d8bef9SDimitry Andric if (Record.empty()) 234581ad6265SDimitry Andric return error("Invalid integer record"); 2346*0b57cec5SDimitry Andric 2347*0b57cec5SDimitry Andric uint64_t NumBits = Record[0]; 2348*0b57cec5SDimitry Andric if (NumBits < IntegerType::MIN_INT_BITS || 2349*0b57cec5SDimitry Andric NumBits > IntegerType::MAX_INT_BITS) 2350*0b57cec5SDimitry Andric return error("Bitwidth for integer type out of range"); 2351*0b57cec5SDimitry Andric ResultTy = IntegerType::get(Context, NumBits); 2352*0b57cec5SDimitry Andric break; 2353*0b57cec5SDimitry Andric } 2354*0b57cec5SDimitry Andric case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or 2355*0b57cec5SDimitry Andric // [pointee type, address space] 2356e8d8bef9SDimitry Andric if (Record.empty()) 235781ad6265SDimitry Andric return error("Invalid pointer record"); 2358*0b57cec5SDimitry Andric unsigned AddressSpace = 0; 2359*0b57cec5SDimitry Andric if (Record.size() == 2) 2360*0b57cec5SDimitry Andric AddressSpace = Record[1]; 2361*0b57cec5SDimitry Andric ResultTy = getTypeByID(Record[0]); 2362*0b57cec5SDimitry Andric if (!ResultTy || 2363*0b57cec5SDimitry Andric !PointerType::isValidElementType(ResultTy)) 2364*0b57cec5SDimitry Andric return error("Invalid type"); 236581ad6265SDimitry Andric ContainedIDs.push_back(Record[0]); 2366*0b57cec5SDimitry Andric ResultTy = PointerType::get(ResultTy, AddressSpace); 2367*0b57cec5SDimitry Andric break; 2368*0b57cec5SDimitry Andric } 2369fe6060f1SDimitry Andric case bitc::TYPE_CODE_OPAQUE_POINTER: { // OPAQUE_POINTER: [addrspace] 2370fe6060f1SDimitry Andric if (Record.size() != 1) 237181ad6265SDimitry Andric return error("Invalid opaque pointer record"); 2372bdd1243dSDimitry Andric if (Context.supportsTypedPointers()) 2373349cc55cSDimitry Andric return error( 2374349cc55cSDimitry Andric "Opaque pointers are only supported in -opaque-pointers mode"); 2375fe6060f1SDimitry Andric unsigned AddressSpace = Record[0]; 2376fe6060f1SDimitry Andric ResultTy = PointerType::get(Context, AddressSpace); 2377fe6060f1SDimitry Andric break; 2378fe6060f1SDimitry Andric } 2379*0b57cec5SDimitry Andric case bitc::TYPE_CODE_FUNCTION_OLD: { 23805ffd83dbSDimitry Andric // Deprecated, but still needed to read old bitcode files. 2381*0b57cec5SDimitry Andric // FUNCTION: [vararg, attrid, retty, paramty x N] 2382*0b57cec5SDimitry Andric if (Record.size() < 3) 238381ad6265SDimitry Andric return error("Invalid function record"); 2384*0b57cec5SDimitry Andric SmallVector<Type*, 8> ArgTys; 2385*0b57cec5SDimitry Andric for (unsigned i = 3, e = Record.size(); i != e; ++i) { 2386*0b57cec5SDimitry Andric if (Type *T = getTypeByID(Record[i])) 2387*0b57cec5SDimitry Andric ArgTys.push_back(T); 2388*0b57cec5SDimitry Andric else 2389*0b57cec5SDimitry Andric break; 2390*0b57cec5SDimitry Andric } 2391*0b57cec5SDimitry Andric 2392*0b57cec5SDimitry Andric ResultTy = getTypeByID(Record[2]); 2393*0b57cec5SDimitry Andric if (!ResultTy || ArgTys.size() < Record.size()-3) 2394*0b57cec5SDimitry Andric return error("Invalid type"); 2395*0b57cec5SDimitry Andric 239681ad6265SDimitry Andric ContainedIDs.append(Record.begin() + 2, Record.end()); 2397*0b57cec5SDimitry Andric ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); 2398*0b57cec5SDimitry Andric break; 2399*0b57cec5SDimitry Andric } 2400*0b57cec5SDimitry Andric case bitc::TYPE_CODE_FUNCTION: { 2401*0b57cec5SDimitry Andric // FUNCTION: [vararg, retty, paramty x N] 2402*0b57cec5SDimitry Andric if (Record.size() < 2) 240381ad6265SDimitry Andric return error("Invalid function record"); 2404*0b57cec5SDimitry Andric SmallVector<Type*, 8> ArgTys; 2405*0b57cec5SDimitry Andric for (unsigned i = 2, e = Record.size(); i != e; ++i) { 2406*0b57cec5SDimitry Andric if (Type *T = getTypeByID(Record[i])) { 2407*0b57cec5SDimitry Andric if (!FunctionType::isValidArgumentType(T)) 2408*0b57cec5SDimitry Andric return error("Invalid function argument type"); 2409*0b57cec5SDimitry Andric ArgTys.push_back(T); 2410*0b57cec5SDimitry Andric } 2411*0b57cec5SDimitry Andric else 2412*0b57cec5SDimitry Andric break; 2413*0b57cec5SDimitry Andric } 2414*0b57cec5SDimitry Andric 2415*0b57cec5SDimitry Andric ResultTy = getTypeByID(Record[1]); 2416*0b57cec5SDimitry Andric if (!ResultTy || ArgTys.size() < Record.size()-2) 2417*0b57cec5SDimitry Andric return error("Invalid type"); 2418*0b57cec5SDimitry Andric 241981ad6265SDimitry Andric ContainedIDs.append(Record.begin() + 1, Record.end()); 2420*0b57cec5SDimitry Andric ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); 2421*0b57cec5SDimitry Andric break; 2422*0b57cec5SDimitry Andric } 2423*0b57cec5SDimitry Andric case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N] 2424e8d8bef9SDimitry Andric if (Record.empty()) 242581ad6265SDimitry Andric return error("Invalid anon struct record"); 2426*0b57cec5SDimitry Andric SmallVector<Type*, 8> EltTys; 2427*0b57cec5SDimitry Andric for (unsigned i = 1, e = Record.size(); i != e; ++i) { 2428*0b57cec5SDimitry Andric if (Type *T = getTypeByID(Record[i])) 2429*0b57cec5SDimitry Andric EltTys.push_back(T); 2430*0b57cec5SDimitry Andric else 2431*0b57cec5SDimitry Andric break; 2432*0b57cec5SDimitry Andric } 2433*0b57cec5SDimitry Andric if (EltTys.size() != Record.size()-1) 2434*0b57cec5SDimitry Andric return error("Invalid type"); 243581ad6265SDimitry Andric ContainedIDs.append(Record.begin() + 1, Record.end()); 2436*0b57cec5SDimitry Andric ResultTy = StructType::get(Context, EltTys, Record[0]); 2437*0b57cec5SDimitry Andric break; 2438*0b57cec5SDimitry Andric } 2439*0b57cec5SDimitry Andric case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N] 2440*0b57cec5SDimitry Andric if (convertToString(Record, 0, TypeName)) 244181ad6265SDimitry Andric return error("Invalid struct name record"); 2442*0b57cec5SDimitry Andric continue; 2443*0b57cec5SDimitry Andric 2444*0b57cec5SDimitry Andric case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N] 2445e8d8bef9SDimitry Andric if (Record.empty()) 244681ad6265SDimitry Andric return error("Invalid named struct record"); 2447*0b57cec5SDimitry Andric 2448*0b57cec5SDimitry Andric if (NumRecords >= TypeList.size()) 2449*0b57cec5SDimitry Andric return error("Invalid TYPE table"); 2450*0b57cec5SDimitry Andric 2451*0b57cec5SDimitry Andric // Check to see if this was forward referenced, if so fill in the temp. 2452*0b57cec5SDimitry Andric StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); 2453*0b57cec5SDimitry Andric if (Res) { 2454*0b57cec5SDimitry Andric Res->setName(TypeName); 2455*0b57cec5SDimitry Andric TypeList[NumRecords] = nullptr; 2456*0b57cec5SDimitry Andric } else // Otherwise, create a new struct. 2457*0b57cec5SDimitry Andric Res = createIdentifiedStructType(Context, TypeName); 2458*0b57cec5SDimitry Andric TypeName.clear(); 2459*0b57cec5SDimitry Andric 2460*0b57cec5SDimitry Andric SmallVector<Type*, 8> EltTys; 2461*0b57cec5SDimitry Andric for (unsigned i = 1, e = Record.size(); i != e; ++i) { 2462*0b57cec5SDimitry Andric if (Type *T = getTypeByID(Record[i])) 2463*0b57cec5SDimitry Andric EltTys.push_back(T); 2464*0b57cec5SDimitry Andric else 2465*0b57cec5SDimitry Andric break; 2466*0b57cec5SDimitry Andric } 2467*0b57cec5SDimitry Andric if (EltTys.size() != Record.size()-1) 246881ad6265SDimitry Andric return error("Invalid named struct record"); 2469*0b57cec5SDimitry Andric Res->setBody(EltTys, Record[0]); 247081ad6265SDimitry Andric ContainedIDs.append(Record.begin() + 1, Record.end()); 2471*0b57cec5SDimitry Andric ResultTy = Res; 2472*0b57cec5SDimitry Andric break; 2473*0b57cec5SDimitry Andric } 2474*0b57cec5SDimitry Andric case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: [] 2475*0b57cec5SDimitry Andric if (Record.size() != 1) 247681ad6265SDimitry Andric return error("Invalid opaque type record"); 2477*0b57cec5SDimitry Andric 2478*0b57cec5SDimitry Andric if (NumRecords >= TypeList.size()) 2479*0b57cec5SDimitry Andric return error("Invalid TYPE table"); 2480*0b57cec5SDimitry Andric 2481*0b57cec5SDimitry Andric // Check to see if this was forward referenced, if so fill in the temp. 2482*0b57cec5SDimitry Andric StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); 2483*0b57cec5SDimitry Andric if (Res) { 2484*0b57cec5SDimitry Andric Res->setName(TypeName); 2485*0b57cec5SDimitry Andric TypeList[NumRecords] = nullptr; 2486*0b57cec5SDimitry Andric } else // Otherwise, create a new struct with no body. 2487*0b57cec5SDimitry Andric Res = createIdentifiedStructType(Context, TypeName); 2488*0b57cec5SDimitry Andric TypeName.clear(); 2489*0b57cec5SDimitry Andric ResultTy = Res; 2490*0b57cec5SDimitry Andric break; 2491*0b57cec5SDimitry Andric } 2492bdd1243dSDimitry Andric case bitc::TYPE_CODE_TARGET_TYPE: { // TARGET_TYPE: [NumTy, Tys..., Ints...] 2493bdd1243dSDimitry Andric if (Record.size() < 1) 2494bdd1243dSDimitry Andric return error("Invalid target extension type record"); 2495bdd1243dSDimitry Andric 2496bdd1243dSDimitry Andric if (NumRecords >= TypeList.size()) 2497bdd1243dSDimitry Andric return error("Invalid TYPE table"); 2498bdd1243dSDimitry Andric 2499bdd1243dSDimitry Andric if (Record[0] >= Record.size()) 2500bdd1243dSDimitry Andric return error("Too many type parameters"); 2501bdd1243dSDimitry Andric 2502bdd1243dSDimitry Andric unsigned NumTys = Record[0]; 2503bdd1243dSDimitry Andric SmallVector<Type *, 4> TypeParams; 2504bdd1243dSDimitry Andric SmallVector<unsigned, 8> IntParams; 2505bdd1243dSDimitry Andric for (unsigned i = 0; i < NumTys; i++) { 2506bdd1243dSDimitry Andric if (Type *T = getTypeByID(Record[i + 1])) 2507bdd1243dSDimitry Andric TypeParams.push_back(T); 2508bdd1243dSDimitry Andric else 2509bdd1243dSDimitry Andric return error("Invalid type"); 2510bdd1243dSDimitry Andric } 2511bdd1243dSDimitry Andric 2512bdd1243dSDimitry Andric for (unsigned i = NumTys + 1, e = Record.size(); i < e; i++) { 2513bdd1243dSDimitry Andric if (Record[i] > UINT_MAX) 2514bdd1243dSDimitry Andric return error("Integer parameter too large"); 2515bdd1243dSDimitry Andric IntParams.push_back(Record[i]); 2516bdd1243dSDimitry Andric } 2517bdd1243dSDimitry Andric ResultTy = TargetExtType::get(Context, TypeName, TypeParams, IntParams); 2518bdd1243dSDimitry Andric TypeName.clear(); 2519bdd1243dSDimitry Andric break; 2520bdd1243dSDimitry Andric } 2521*0b57cec5SDimitry Andric case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty] 2522*0b57cec5SDimitry Andric if (Record.size() < 2) 252381ad6265SDimitry Andric return error("Invalid array type record"); 2524*0b57cec5SDimitry Andric ResultTy = getTypeByID(Record[1]); 2525*0b57cec5SDimitry Andric if (!ResultTy || !ArrayType::isValidElementType(ResultTy)) 2526*0b57cec5SDimitry Andric return error("Invalid type"); 252781ad6265SDimitry Andric ContainedIDs.push_back(Record[1]); 2528*0b57cec5SDimitry Andric ResultTy = ArrayType::get(ResultTy, Record[0]); 2529*0b57cec5SDimitry Andric break; 2530*0b57cec5SDimitry Andric case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] or 2531*0b57cec5SDimitry Andric // [numelts, eltty, scalable] 2532*0b57cec5SDimitry Andric if (Record.size() < 2) 253381ad6265SDimitry Andric return error("Invalid vector type record"); 2534*0b57cec5SDimitry Andric if (Record[0] == 0) 2535*0b57cec5SDimitry Andric return error("Invalid vector length"); 2536*0b57cec5SDimitry Andric ResultTy = getTypeByID(Record[1]); 2537349cc55cSDimitry Andric if (!ResultTy || !VectorType::isValidElementType(ResultTy)) 2538*0b57cec5SDimitry Andric return error("Invalid type"); 2539*0b57cec5SDimitry Andric bool Scalable = Record.size() > 2 ? Record[2] : false; 254081ad6265SDimitry Andric ContainedIDs.push_back(Record[1]); 2541*0b57cec5SDimitry Andric ResultTy = VectorType::get(ResultTy, Record[0], Scalable); 2542*0b57cec5SDimitry Andric break; 2543*0b57cec5SDimitry Andric } 2544*0b57cec5SDimitry Andric 2545*0b57cec5SDimitry Andric if (NumRecords >= TypeList.size()) 2546*0b57cec5SDimitry Andric return error("Invalid TYPE table"); 2547*0b57cec5SDimitry Andric if (TypeList[NumRecords]) 2548*0b57cec5SDimitry Andric return error( 2549*0b57cec5SDimitry Andric "Invalid TYPE table: Only named structs can be forward referenced"); 2550*0b57cec5SDimitry Andric assert(ResultTy && "Didn't read a type?"); 255181ad6265SDimitry Andric TypeList[NumRecords] = ResultTy; 255281ad6265SDimitry Andric if (!ContainedIDs.empty()) 255381ad6265SDimitry Andric ContainedTypeIDs[NumRecords] = std::move(ContainedIDs); 255481ad6265SDimitry Andric ++NumRecords; 2555*0b57cec5SDimitry Andric } 2556*0b57cec5SDimitry Andric } 2557*0b57cec5SDimitry Andric 2558*0b57cec5SDimitry Andric Error BitcodeReader::parseOperandBundleTags() { 2559*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID)) 2560*0b57cec5SDimitry Andric return Err; 2561*0b57cec5SDimitry Andric 2562*0b57cec5SDimitry Andric if (!BundleTags.empty()) 2563*0b57cec5SDimitry Andric return error("Invalid multiple blocks"); 2564*0b57cec5SDimitry Andric 2565*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 2566*0b57cec5SDimitry Andric 2567*0b57cec5SDimitry Andric while (true) { 2568*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 2569*0b57cec5SDimitry Andric if (!MaybeEntry) 2570*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 2571*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 2572*0b57cec5SDimitry Andric 2573*0b57cec5SDimitry Andric switch (Entry.Kind) { 2574*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 2575*0b57cec5SDimitry Andric case BitstreamEntry::Error: 2576*0b57cec5SDimitry Andric return error("Malformed block"); 2577*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 2578*0b57cec5SDimitry Andric return Error::success(); 2579*0b57cec5SDimitry Andric case BitstreamEntry::Record: 2580*0b57cec5SDimitry Andric // The interesting case. 2581*0b57cec5SDimitry Andric break; 2582*0b57cec5SDimitry Andric } 2583*0b57cec5SDimitry Andric 2584*0b57cec5SDimitry Andric // Tags are implicitly mapped to integers by their order. 2585*0b57cec5SDimitry Andric 2586*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 2587*0b57cec5SDimitry Andric if (!MaybeRecord) 2588*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 2589*0b57cec5SDimitry Andric if (MaybeRecord.get() != bitc::OPERAND_BUNDLE_TAG) 259081ad6265SDimitry Andric return error("Invalid operand bundle record"); 2591*0b57cec5SDimitry Andric 2592*0b57cec5SDimitry Andric // OPERAND_BUNDLE_TAG: [strchr x N] 2593*0b57cec5SDimitry Andric BundleTags.emplace_back(); 2594*0b57cec5SDimitry Andric if (convertToString(Record, 0, BundleTags.back())) 259581ad6265SDimitry Andric return error("Invalid operand bundle record"); 2596*0b57cec5SDimitry Andric Record.clear(); 2597*0b57cec5SDimitry Andric } 2598*0b57cec5SDimitry Andric } 2599*0b57cec5SDimitry Andric 2600*0b57cec5SDimitry Andric Error BitcodeReader::parseSyncScopeNames() { 2601*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::SYNC_SCOPE_NAMES_BLOCK_ID)) 2602*0b57cec5SDimitry Andric return Err; 2603*0b57cec5SDimitry Andric 2604*0b57cec5SDimitry Andric if (!SSIDs.empty()) 2605*0b57cec5SDimitry Andric return error("Invalid multiple synchronization scope names blocks"); 2606*0b57cec5SDimitry Andric 2607*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 2608*0b57cec5SDimitry Andric while (true) { 2609*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 2610*0b57cec5SDimitry Andric if (!MaybeEntry) 2611*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 2612*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 2613*0b57cec5SDimitry Andric 2614*0b57cec5SDimitry Andric switch (Entry.Kind) { 2615*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 2616*0b57cec5SDimitry Andric case BitstreamEntry::Error: 2617*0b57cec5SDimitry Andric return error("Malformed block"); 2618*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 2619*0b57cec5SDimitry Andric if (SSIDs.empty()) 2620*0b57cec5SDimitry Andric return error("Invalid empty synchronization scope names block"); 2621*0b57cec5SDimitry Andric return Error::success(); 2622*0b57cec5SDimitry Andric case BitstreamEntry::Record: 2623*0b57cec5SDimitry Andric // The interesting case. 2624*0b57cec5SDimitry Andric break; 2625*0b57cec5SDimitry Andric } 2626*0b57cec5SDimitry Andric 2627*0b57cec5SDimitry Andric // Synchronization scope names are implicitly mapped to synchronization 2628*0b57cec5SDimitry Andric // scope IDs by their order. 2629*0b57cec5SDimitry Andric 2630*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 2631*0b57cec5SDimitry Andric if (!MaybeRecord) 2632*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 2633*0b57cec5SDimitry Andric if (MaybeRecord.get() != bitc::SYNC_SCOPE_NAME) 263481ad6265SDimitry Andric return error("Invalid sync scope record"); 2635*0b57cec5SDimitry Andric 2636*0b57cec5SDimitry Andric SmallString<16> SSN; 2637*0b57cec5SDimitry Andric if (convertToString(Record, 0, SSN)) 263881ad6265SDimitry Andric return error("Invalid sync scope record"); 2639*0b57cec5SDimitry Andric 2640*0b57cec5SDimitry Andric SSIDs.push_back(Context.getOrInsertSyncScopeID(SSN)); 2641*0b57cec5SDimitry Andric Record.clear(); 2642*0b57cec5SDimitry Andric } 2643*0b57cec5SDimitry Andric } 2644*0b57cec5SDimitry Andric 2645*0b57cec5SDimitry Andric /// Associate a value with its name from the given index in the provided record. 2646*0b57cec5SDimitry Andric Expected<Value *> BitcodeReader::recordValue(SmallVectorImpl<uint64_t> &Record, 2647*0b57cec5SDimitry Andric unsigned NameIndex, Triple &TT) { 2648*0b57cec5SDimitry Andric SmallString<128> ValueName; 2649*0b57cec5SDimitry Andric if (convertToString(Record, NameIndex, ValueName)) 2650*0b57cec5SDimitry Andric return error("Invalid record"); 2651*0b57cec5SDimitry Andric unsigned ValueID = Record[0]; 2652*0b57cec5SDimitry Andric if (ValueID >= ValueList.size() || !ValueList[ValueID]) 2653*0b57cec5SDimitry Andric return error("Invalid record"); 2654*0b57cec5SDimitry Andric Value *V = ValueList[ValueID]; 2655*0b57cec5SDimitry Andric 2656*0b57cec5SDimitry Andric StringRef NameStr(ValueName.data(), ValueName.size()); 2657*0b57cec5SDimitry Andric if (NameStr.find_first_of(0) != StringRef::npos) 2658*0b57cec5SDimitry Andric return error("Invalid value name"); 2659*0b57cec5SDimitry Andric V->setName(NameStr); 2660*0b57cec5SDimitry Andric auto *GO = dyn_cast<GlobalObject>(V); 26610eae32dcSDimitry Andric if (GO && ImplicitComdatObjects.contains(GO) && TT.supportsCOMDAT()) 2662*0b57cec5SDimitry Andric GO->setComdat(TheModule->getOrInsertComdat(V->getName())); 2663*0b57cec5SDimitry Andric return V; 2664*0b57cec5SDimitry Andric } 2665*0b57cec5SDimitry Andric 2666*0b57cec5SDimitry Andric /// Helper to note and return the current location, and jump to the given 2667*0b57cec5SDimitry Andric /// offset. 2668*0b57cec5SDimitry Andric static Expected<uint64_t> jumpToValueSymbolTable(uint64_t Offset, 2669*0b57cec5SDimitry Andric BitstreamCursor &Stream) { 2670*0b57cec5SDimitry Andric // Save the current parsing location so we can jump back at the end 2671*0b57cec5SDimitry Andric // of the VST read. 2672*0b57cec5SDimitry Andric uint64_t CurrentBit = Stream.GetCurrentBitNo(); 2673*0b57cec5SDimitry Andric if (Error JumpFailed = Stream.JumpToBit(Offset * 32)) 2674*0b57cec5SDimitry Andric return std::move(JumpFailed); 2675*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advance(); 2676*0b57cec5SDimitry Andric if (!MaybeEntry) 2677*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 267881ad6265SDimitry Andric if (MaybeEntry.get().Kind != BitstreamEntry::SubBlock || 267981ad6265SDimitry Andric MaybeEntry.get().ID != bitc::VALUE_SYMTAB_BLOCK_ID) 268081ad6265SDimitry Andric return error("Expected value symbol table subblock"); 2681*0b57cec5SDimitry Andric return CurrentBit; 2682*0b57cec5SDimitry Andric } 2683*0b57cec5SDimitry Andric 2684*0b57cec5SDimitry Andric void BitcodeReader::setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta, 2685*0b57cec5SDimitry Andric Function *F, 2686*0b57cec5SDimitry Andric ArrayRef<uint64_t> Record) { 2687*0b57cec5SDimitry Andric // Note that we subtract 1 here because the offset is relative to one word 2688*0b57cec5SDimitry Andric // before the start of the identification or module block, which was 2689*0b57cec5SDimitry Andric // historically always the start of the regular bitcode header. 2690*0b57cec5SDimitry Andric uint64_t FuncWordOffset = Record[1] - 1; 2691*0b57cec5SDimitry Andric uint64_t FuncBitOffset = FuncWordOffset * 32; 2692*0b57cec5SDimitry Andric DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta; 2693*0b57cec5SDimitry Andric // Set the LastFunctionBlockBit to point to the last function block. 2694*0b57cec5SDimitry Andric // Later when parsing is resumed after function materialization, 2695*0b57cec5SDimitry Andric // we can simply skip that last function block. 2696*0b57cec5SDimitry Andric if (FuncBitOffset > LastFunctionBlockBit) 2697*0b57cec5SDimitry Andric LastFunctionBlockBit = FuncBitOffset; 2698*0b57cec5SDimitry Andric } 2699*0b57cec5SDimitry Andric 2700*0b57cec5SDimitry Andric /// Read a new-style GlobalValue symbol table. 2701*0b57cec5SDimitry Andric Error BitcodeReader::parseGlobalValueSymbolTable() { 2702*0b57cec5SDimitry Andric unsigned FuncBitcodeOffsetDelta = 2703*0b57cec5SDimitry Andric Stream.getAbbrevIDWidth() + bitc::BlockIDWidth; 2704*0b57cec5SDimitry Andric 2705*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) 2706*0b57cec5SDimitry Andric return Err; 2707*0b57cec5SDimitry Andric 2708*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 2709*0b57cec5SDimitry Andric while (true) { 2710*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 2711*0b57cec5SDimitry Andric if (!MaybeEntry) 2712*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 2713*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 2714*0b57cec5SDimitry Andric 2715*0b57cec5SDimitry Andric switch (Entry.Kind) { 2716*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: 2717*0b57cec5SDimitry Andric case BitstreamEntry::Error: 2718*0b57cec5SDimitry Andric return error("Malformed block"); 2719*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 2720*0b57cec5SDimitry Andric return Error::success(); 2721*0b57cec5SDimitry Andric case BitstreamEntry::Record: 2722*0b57cec5SDimitry Andric break; 2723*0b57cec5SDimitry Andric } 2724*0b57cec5SDimitry Andric 2725*0b57cec5SDimitry Andric Record.clear(); 2726*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 2727*0b57cec5SDimitry Andric if (!MaybeRecord) 2728*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 2729*0b57cec5SDimitry Andric switch (MaybeRecord.get()) { 273081ad6265SDimitry Andric case bitc::VST_CODE_FNENTRY: { // [valueid, offset] 273181ad6265SDimitry Andric unsigned ValueID = Record[0]; 273281ad6265SDimitry Andric if (ValueID >= ValueList.size() || !ValueList[ValueID]) 273381ad6265SDimitry Andric return error("Invalid value reference in symbol table"); 2734*0b57cec5SDimitry Andric setDeferredFunctionInfo(FuncBitcodeOffsetDelta, 273581ad6265SDimitry Andric cast<Function>(ValueList[ValueID]), Record); 2736*0b57cec5SDimitry Andric break; 2737*0b57cec5SDimitry Andric } 2738*0b57cec5SDimitry Andric } 2739*0b57cec5SDimitry Andric } 274081ad6265SDimitry Andric } 2741*0b57cec5SDimitry Andric 2742*0b57cec5SDimitry Andric /// Parse the value symbol table at either the current parsing location or 2743*0b57cec5SDimitry Andric /// at the given bit offset if provided. 2744*0b57cec5SDimitry Andric Error BitcodeReader::parseValueSymbolTable(uint64_t Offset) { 2745*0b57cec5SDimitry Andric uint64_t CurrentBit; 2746*0b57cec5SDimitry Andric // Pass in the Offset to distinguish between calling for the module-level 2747*0b57cec5SDimitry Andric // VST (where we want to jump to the VST offset) and the function-level 2748*0b57cec5SDimitry Andric // VST (where we don't). 2749*0b57cec5SDimitry Andric if (Offset > 0) { 2750*0b57cec5SDimitry Andric Expected<uint64_t> MaybeCurrentBit = jumpToValueSymbolTable(Offset, Stream); 2751*0b57cec5SDimitry Andric if (!MaybeCurrentBit) 2752*0b57cec5SDimitry Andric return MaybeCurrentBit.takeError(); 2753*0b57cec5SDimitry Andric CurrentBit = MaybeCurrentBit.get(); 2754*0b57cec5SDimitry Andric // If this module uses a string table, read this as a module-level VST. 2755*0b57cec5SDimitry Andric if (UseStrtab) { 2756*0b57cec5SDimitry Andric if (Error Err = parseGlobalValueSymbolTable()) 2757*0b57cec5SDimitry Andric return Err; 2758*0b57cec5SDimitry Andric if (Error JumpFailed = Stream.JumpToBit(CurrentBit)) 2759*0b57cec5SDimitry Andric return JumpFailed; 2760*0b57cec5SDimitry Andric return Error::success(); 2761*0b57cec5SDimitry Andric } 2762*0b57cec5SDimitry Andric // Otherwise, the VST will be in a similar format to a function-level VST, 2763*0b57cec5SDimitry Andric // and will contain symbol names. 2764*0b57cec5SDimitry Andric } 2765*0b57cec5SDimitry Andric 2766*0b57cec5SDimitry Andric // Compute the delta between the bitcode indices in the VST (the word offset 2767*0b57cec5SDimitry Andric // to the word-aligned ENTER_SUBBLOCK for the function block, and that 2768*0b57cec5SDimitry Andric // expected by the lazy reader. The reader's EnterSubBlock expects to have 2769*0b57cec5SDimitry Andric // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID 2770*0b57cec5SDimitry Andric // (size BlockIDWidth). Note that we access the stream's AbbrevID width here 2771*0b57cec5SDimitry Andric // just before entering the VST subblock because: 1) the EnterSubBlock 2772*0b57cec5SDimitry Andric // changes the AbbrevID width; 2) the VST block is nested within the same 2773*0b57cec5SDimitry Andric // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same 2774*0b57cec5SDimitry Andric // AbbrevID width before calling EnterSubBlock; and 3) when we want to 2775*0b57cec5SDimitry Andric // jump to the FUNCTION_BLOCK using this offset later, we don't want 2776*0b57cec5SDimitry Andric // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK. 2777*0b57cec5SDimitry Andric unsigned FuncBitcodeOffsetDelta = 2778*0b57cec5SDimitry Andric Stream.getAbbrevIDWidth() + bitc::BlockIDWidth; 2779*0b57cec5SDimitry Andric 2780*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) 2781*0b57cec5SDimitry Andric return Err; 2782*0b57cec5SDimitry Andric 2783*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 2784*0b57cec5SDimitry Andric 2785*0b57cec5SDimitry Andric Triple TT(TheModule->getTargetTriple()); 2786*0b57cec5SDimitry Andric 2787*0b57cec5SDimitry Andric // Read all the records for this value table. 2788*0b57cec5SDimitry Andric SmallString<128> ValueName; 2789*0b57cec5SDimitry Andric 2790*0b57cec5SDimitry Andric while (true) { 2791*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 2792*0b57cec5SDimitry Andric if (!MaybeEntry) 2793*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 2794*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 2795*0b57cec5SDimitry Andric 2796*0b57cec5SDimitry Andric switch (Entry.Kind) { 2797*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 2798*0b57cec5SDimitry Andric case BitstreamEntry::Error: 2799*0b57cec5SDimitry Andric return error("Malformed block"); 2800*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 2801*0b57cec5SDimitry Andric if (Offset > 0) 2802*0b57cec5SDimitry Andric if (Error JumpFailed = Stream.JumpToBit(CurrentBit)) 2803*0b57cec5SDimitry Andric return JumpFailed; 2804*0b57cec5SDimitry Andric return Error::success(); 2805*0b57cec5SDimitry Andric case BitstreamEntry::Record: 2806*0b57cec5SDimitry Andric // The interesting case. 2807*0b57cec5SDimitry Andric break; 2808*0b57cec5SDimitry Andric } 2809*0b57cec5SDimitry Andric 2810*0b57cec5SDimitry Andric // Read a record. 2811*0b57cec5SDimitry Andric Record.clear(); 2812*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 2813*0b57cec5SDimitry Andric if (!MaybeRecord) 2814*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 2815*0b57cec5SDimitry Andric switch (MaybeRecord.get()) { 2816*0b57cec5SDimitry Andric default: // Default behavior: unknown type. 2817*0b57cec5SDimitry Andric break; 2818*0b57cec5SDimitry Andric case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N] 2819*0b57cec5SDimitry Andric Expected<Value *> ValOrErr = recordValue(Record, 1, TT); 2820*0b57cec5SDimitry Andric if (Error Err = ValOrErr.takeError()) 2821*0b57cec5SDimitry Andric return Err; 2822*0b57cec5SDimitry Andric ValOrErr.get(); 2823*0b57cec5SDimitry Andric break; 2824*0b57cec5SDimitry Andric } 2825*0b57cec5SDimitry Andric case bitc::VST_CODE_FNENTRY: { 2826*0b57cec5SDimitry Andric // VST_CODE_FNENTRY: [valueid, offset, namechar x N] 2827*0b57cec5SDimitry Andric Expected<Value *> ValOrErr = recordValue(Record, 2, TT); 2828*0b57cec5SDimitry Andric if (Error Err = ValOrErr.takeError()) 2829*0b57cec5SDimitry Andric return Err; 2830*0b57cec5SDimitry Andric Value *V = ValOrErr.get(); 2831*0b57cec5SDimitry Andric 2832*0b57cec5SDimitry Andric // Ignore function offsets emitted for aliases of functions in older 2833*0b57cec5SDimitry Andric // versions of LLVM. 2834*0b57cec5SDimitry Andric if (auto *F = dyn_cast<Function>(V)) 2835*0b57cec5SDimitry Andric setDeferredFunctionInfo(FuncBitcodeOffsetDelta, F, Record); 2836*0b57cec5SDimitry Andric break; 2837*0b57cec5SDimitry Andric } 2838*0b57cec5SDimitry Andric case bitc::VST_CODE_BBENTRY: { 2839*0b57cec5SDimitry Andric if (convertToString(Record, 1, ValueName)) 284081ad6265SDimitry Andric return error("Invalid bbentry record"); 2841*0b57cec5SDimitry Andric BasicBlock *BB = getBasicBlock(Record[0]); 2842*0b57cec5SDimitry Andric if (!BB) 284381ad6265SDimitry Andric return error("Invalid bbentry record"); 2844*0b57cec5SDimitry Andric 2845*0b57cec5SDimitry Andric BB->setName(StringRef(ValueName.data(), ValueName.size())); 2846*0b57cec5SDimitry Andric ValueName.clear(); 2847*0b57cec5SDimitry Andric break; 2848*0b57cec5SDimitry Andric } 2849*0b57cec5SDimitry Andric } 2850*0b57cec5SDimitry Andric } 2851*0b57cec5SDimitry Andric } 2852*0b57cec5SDimitry Andric 2853*0b57cec5SDimitry Andric /// Decode a signed value stored with the sign bit in the LSB for dense VBR 2854*0b57cec5SDimitry Andric /// encoding. 2855*0b57cec5SDimitry Andric uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) { 2856*0b57cec5SDimitry Andric if ((V & 1) == 0) 2857*0b57cec5SDimitry Andric return V >> 1; 2858*0b57cec5SDimitry Andric if (V != 1) 2859*0b57cec5SDimitry Andric return -(V >> 1); 2860*0b57cec5SDimitry Andric // There is no such thing as -0 with integers. "-0" really means MININT. 2861*0b57cec5SDimitry Andric return 1ULL << 63; 2862*0b57cec5SDimitry Andric } 2863*0b57cec5SDimitry Andric 2864*0b57cec5SDimitry Andric /// Resolve all of the initializers for global values and aliases that we can. 2865*0b57cec5SDimitry Andric Error BitcodeReader::resolveGlobalAndIndirectSymbolInits() { 2866*0b57cec5SDimitry Andric std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInitWorklist; 2867349cc55cSDimitry Andric std::vector<std::pair<GlobalValue *, unsigned>> IndirectSymbolInitWorklist; 2868349cc55cSDimitry Andric std::vector<FunctionOperandInfo> FunctionOperandWorklist; 2869*0b57cec5SDimitry Andric 2870*0b57cec5SDimitry Andric GlobalInitWorklist.swap(GlobalInits); 2871*0b57cec5SDimitry Andric IndirectSymbolInitWorklist.swap(IndirectSymbolInits); 2872349cc55cSDimitry Andric FunctionOperandWorklist.swap(FunctionOperands); 2873*0b57cec5SDimitry Andric 2874*0b57cec5SDimitry Andric while (!GlobalInitWorklist.empty()) { 2875*0b57cec5SDimitry Andric unsigned ValID = GlobalInitWorklist.back().second; 2876*0b57cec5SDimitry Andric if (ValID >= ValueList.size()) { 2877*0b57cec5SDimitry Andric // Not ready to resolve this yet, it requires something later in the file. 2878*0b57cec5SDimitry Andric GlobalInits.push_back(GlobalInitWorklist.back()); 2879*0b57cec5SDimitry Andric } else { 288081ad6265SDimitry Andric Expected<Constant *> MaybeC = getValueForInitializer(ValID); 288181ad6265SDimitry Andric if (!MaybeC) 288281ad6265SDimitry Andric return MaybeC.takeError(); 288381ad6265SDimitry Andric GlobalInitWorklist.back().first->setInitializer(MaybeC.get()); 2884*0b57cec5SDimitry Andric } 2885*0b57cec5SDimitry Andric GlobalInitWorklist.pop_back(); 2886*0b57cec5SDimitry Andric } 2887*0b57cec5SDimitry Andric 2888*0b57cec5SDimitry Andric while (!IndirectSymbolInitWorklist.empty()) { 2889*0b57cec5SDimitry Andric unsigned ValID = IndirectSymbolInitWorklist.back().second; 2890*0b57cec5SDimitry Andric if (ValID >= ValueList.size()) { 2891*0b57cec5SDimitry Andric IndirectSymbolInits.push_back(IndirectSymbolInitWorklist.back()); 2892*0b57cec5SDimitry Andric } else { 289381ad6265SDimitry Andric Expected<Constant *> MaybeC = getValueForInitializer(ValID); 289481ad6265SDimitry Andric if (!MaybeC) 289581ad6265SDimitry Andric return MaybeC.takeError(); 289681ad6265SDimitry Andric Constant *C = MaybeC.get(); 2897349cc55cSDimitry Andric GlobalValue *GV = IndirectSymbolInitWorklist.back().first; 2898349cc55cSDimitry Andric if (auto *GA = dyn_cast<GlobalAlias>(GV)) { 2899349cc55cSDimitry Andric if (C->getType() != GV->getType()) 2900*0b57cec5SDimitry Andric return error("Alias and aliasee types don't match"); 2901349cc55cSDimitry Andric GA->setAliasee(C); 2902349cc55cSDimitry Andric } else if (auto *GI = dyn_cast<GlobalIFunc>(GV)) { 2903349cc55cSDimitry Andric Type *ResolverFTy = 2904349cc55cSDimitry Andric GlobalIFunc::getResolverFunctionType(GI->getValueType()); 2905bdd1243dSDimitry Andric // Transparently fix up the type for compatibility with older bitcode 2906bdd1243dSDimitry Andric GI->setResolver(ConstantExpr::getBitCast( 2907bdd1243dSDimitry Andric C, ResolverFTy->getPointerTo(GI->getAddressSpace()))); 2908349cc55cSDimitry Andric } else { 2909349cc55cSDimitry Andric return error("Expected an alias or an ifunc"); 2910349cc55cSDimitry Andric } 2911*0b57cec5SDimitry Andric } 2912*0b57cec5SDimitry Andric IndirectSymbolInitWorklist.pop_back(); 2913*0b57cec5SDimitry Andric } 2914*0b57cec5SDimitry Andric 2915349cc55cSDimitry Andric while (!FunctionOperandWorklist.empty()) { 2916349cc55cSDimitry Andric FunctionOperandInfo &Info = FunctionOperandWorklist.back(); 2917349cc55cSDimitry Andric if (Info.PersonalityFn) { 2918349cc55cSDimitry Andric unsigned ValID = Info.PersonalityFn - 1; 2919349cc55cSDimitry Andric if (ValID < ValueList.size()) { 292081ad6265SDimitry Andric Expected<Constant *> MaybeC = getValueForInitializer(ValID); 292181ad6265SDimitry Andric if (!MaybeC) 292281ad6265SDimitry Andric return MaybeC.takeError(); 292381ad6265SDimitry Andric Info.F->setPersonalityFn(MaybeC.get()); 2924349cc55cSDimitry Andric Info.PersonalityFn = 0; 2925*0b57cec5SDimitry Andric } 2926*0b57cec5SDimitry Andric } 2927349cc55cSDimitry Andric if (Info.Prefix) { 2928349cc55cSDimitry Andric unsigned ValID = Info.Prefix - 1; 2929349cc55cSDimitry Andric if (ValID < ValueList.size()) { 293081ad6265SDimitry Andric Expected<Constant *> MaybeC = getValueForInitializer(ValID); 293181ad6265SDimitry Andric if (!MaybeC) 293281ad6265SDimitry Andric return MaybeC.takeError(); 293381ad6265SDimitry Andric Info.F->setPrefixData(MaybeC.get()); 2934349cc55cSDimitry Andric Info.Prefix = 0; 2935*0b57cec5SDimitry Andric } 2936*0b57cec5SDimitry Andric } 2937349cc55cSDimitry Andric if (Info.Prologue) { 2938349cc55cSDimitry Andric unsigned ValID = Info.Prologue - 1; 2939349cc55cSDimitry Andric if (ValID < ValueList.size()) { 294081ad6265SDimitry Andric Expected<Constant *> MaybeC = getValueForInitializer(ValID); 294181ad6265SDimitry Andric if (!MaybeC) 294281ad6265SDimitry Andric return MaybeC.takeError(); 294381ad6265SDimitry Andric Info.F->setPrologueData(MaybeC.get()); 2944349cc55cSDimitry Andric Info.Prologue = 0; 2945*0b57cec5SDimitry Andric } 2946349cc55cSDimitry Andric } 2947349cc55cSDimitry Andric if (Info.PersonalityFn || Info.Prefix || Info.Prologue) 2948349cc55cSDimitry Andric FunctionOperands.push_back(Info); 2949349cc55cSDimitry Andric FunctionOperandWorklist.pop_back(); 2950*0b57cec5SDimitry Andric } 2951*0b57cec5SDimitry Andric 2952*0b57cec5SDimitry Andric return Error::success(); 2953*0b57cec5SDimitry Andric } 2954*0b57cec5SDimitry Andric 29555ffd83dbSDimitry Andric APInt llvm::readWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) { 2956*0b57cec5SDimitry Andric SmallVector<uint64_t, 8> Words(Vals.size()); 2957*0b57cec5SDimitry Andric transform(Vals, Words.begin(), 2958*0b57cec5SDimitry Andric BitcodeReader::decodeSignRotatedValue); 2959*0b57cec5SDimitry Andric 2960*0b57cec5SDimitry Andric return APInt(TypeBits, Words); 2961*0b57cec5SDimitry Andric } 2962*0b57cec5SDimitry Andric 2963*0b57cec5SDimitry Andric Error BitcodeReader::parseConstants() { 2964*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID)) 2965*0b57cec5SDimitry Andric return Err; 2966*0b57cec5SDimitry Andric 2967*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 2968*0b57cec5SDimitry Andric 2969*0b57cec5SDimitry Andric // Read all the records for this value table. 2970*0b57cec5SDimitry Andric Type *CurTy = Type::getInt32Ty(Context); 297181ad6265SDimitry Andric unsigned Int32TyID = getVirtualTypeID(CurTy); 297281ad6265SDimitry Andric unsigned CurTyID = Int32TyID; 297381ad6265SDimitry Andric Type *CurElemTy = nullptr; 2974*0b57cec5SDimitry Andric unsigned NextCstNo = ValueList.size(); 2975*0b57cec5SDimitry Andric 2976*0b57cec5SDimitry Andric while (true) { 2977*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 2978*0b57cec5SDimitry Andric if (!MaybeEntry) 2979*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 2980*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 2981*0b57cec5SDimitry Andric 2982*0b57cec5SDimitry Andric switch (Entry.Kind) { 2983*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 2984*0b57cec5SDimitry Andric case BitstreamEntry::Error: 2985*0b57cec5SDimitry Andric return error("Malformed block"); 2986*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 2987*0b57cec5SDimitry Andric if (NextCstNo != ValueList.size()) 2988*0b57cec5SDimitry Andric return error("Invalid constant reference"); 2989*0b57cec5SDimitry Andric return Error::success(); 2990*0b57cec5SDimitry Andric case BitstreamEntry::Record: 2991*0b57cec5SDimitry Andric // The interesting case. 2992*0b57cec5SDimitry Andric break; 2993*0b57cec5SDimitry Andric } 2994*0b57cec5SDimitry Andric 2995*0b57cec5SDimitry Andric // Read a record. 2996*0b57cec5SDimitry Andric Record.clear(); 2997*0b57cec5SDimitry Andric Type *VoidType = Type::getVoidTy(Context); 2998*0b57cec5SDimitry Andric Value *V = nullptr; 2999*0b57cec5SDimitry Andric Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); 3000*0b57cec5SDimitry Andric if (!MaybeBitCode) 3001*0b57cec5SDimitry Andric return MaybeBitCode.takeError(); 3002*0b57cec5SDimitry Andric switch (unsigned BitCode = MaybeBitCode.get()) { 3003*0b57cec5SDimitry Andric default: // Default behavior: unknown constant 3004*0b57cec5SDimitry Andric case bitc::CST_CODE_UNDEF: // UNDEF 3005*0b57cec5SDimitry Andric V = UndefValue::get(CurTy); 3006*0b57cec5SDimitry Andric break; 3007e8d8bef9SDimitry Andric case bitc::CST_CODE_POISON: // POISON 3008e8d8bef9SDimitry Andric V = PoisonValue::get(CurTy); 3009e8d8bef9SDimitry Andric break; 3010*0b57cec5SDimitry Andric case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid] 3011*0b57cec5SDimitry Andric if (Record.empty()) 301281ad6265SDimitry Andric return error("Invalid settype record"); 3013*0b57cec5SDimitry Andric if (Record[0] >= TypeList.size() || !TypeList[Record[0]]) 301481ad6265SDimitry Andric return error("Invalid settype record"); 3015*0b57cec5SDimitry Andric if (TypeList[Record[0]] == VoidType) 3016*0b57cec5SDimitry Andric return error("Invalid constant type"); 301781ad6265SDimitry Andric CurTyID = Record[0]; 301881ad6265SDimitry Andric CurTy = TypeList[CurTyID]; 301981ad6265SDimitry Andric CurElemTy = getPtrElementTypeByID(CurTyID); 3020*0b57cec5SDimitry Andric continue; // Skip the ValueList manipulation. 3021*0b57cec5SDimitry Andric case bitc::CST_CODE_NULL: // NULL 30228bcb0991SDimitry Andric if (CurTy->isVoidTy() || CurTy->isFunctionTy() || CurTy->isLabelTy()) 30238bcb0991SDimitry Andric return error("Invalid type for a constant null value"); 3024bdd1243dSDimitry Andric if (auto *TETy = dyn_cast<TargetExtType>(CurTy)) 3025bdd1243dSDimitry Andric if (!TETy->hasProperty(TargetExtType::HasZeroInit)) 3026bdd1243dSDimitry Andric return error("Invalid type for a constant null value"); 3027*0b57cec5SDimitry Andric V = Constant::getNullValue(CurTy); 3028*0b57cec5SDimitry Andric break; 3029*0b57cec5SDimitry Andric case bitc::CST_CODE_INTEGER: // INTEGER: [intval] 3030*0b57cec5SDimitry Andric if (!CurTy->isIntegerTy() || Record.empty()) 303181ad6265SDimitry Andric return error("Invalid integer const record"); 3032*0b57cec5SDimitry Andric V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0])); 3033*0b57cec5SDimitry Andric break; 3034*0b57cec5SDimitry Andric case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval] 3035*0b57cec5SDimitry Andric if (!CurTy->isIntegerTy() || Record.empty()) 303681ad6265SDimitry Andric return error("Invalid wide integer const record"); 3037*0b57cec5SDimitry Andric 3038*0b57cec5SDimitry Andric APInt VInt = 3039*0b57cec5SDimitry Andric readWideAPInt(Record, cast<IntegerType>(CurTy)->getBitWidth()); 3040*0b57cec5SDimitry Andric V = ConstantInt::get(Context, VInt); 3041*0b57cec5SDimitry Andric 3042*0b57cec5SDimitry Andric break; 3043*0b57cec5SDimitry Andric } 3044*0b57cec5SDimitry Andric case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval] 3045*0b57cec5SDimitry Andric if (Record.empty()) 304681ad6265SDimitry Andric return error("Invalid float const record"); 3047*0b57cec5SDimitry Andric if (CurTy->isHalfTy()) 3048*0b57cec5SDimitry Andric V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf(), 3049*0b57cec5SDimitry Andric APInt(16, (uint16_t)Record[0]))); 30505ffd83dbSDimitry Andric else if (CurTy->isBFloatTy()) 30515ffd83dbSDimitry Andric V = ConstantFP::get(Context, APFloat(APFloat::BFloat(), 30525ffd83dbSDimitry Andric APInt(16, (uint32_t)Record[0]))); 3053*0b57cec5SDimitry Andric else if (CurTy->isFloatTy()) 3054*0b57cec5SDimitry Andric V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle(), 3055*0b57cec5SDimitry Andric APInt(32, (uint32_t)Record[0]))); 3056*0b57cec5SDimitry Andric else if (CurTy->isDoubleTy()) 3057*0b57cec5SDimitry Andric V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble(), 3058*0b57cec5SDimitry Andric APInt(64, Record[0]))); 3059*0b57cec5SDimitry Andric else if (CurTy->isX86_FP80Ty()) { 3060*0b57cec5SDimitry Andric // Bits are not stored the same way as a normal i80 APInt, compensate. 3061*0b57cec5SDimitry Andric uint64_t Rearrange[2]; 3062*0b57cec5SDimitry Andric Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16); 3063*0b57cec5SDimitry Andric Rearrange[1] = Record[0] >> 48; 3064*0b57cec5SDimitry Andric V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended(), 3065*0b57cec5SDimitry Andric APInt(80, Rearrange))); 3066*0b57cec5SDimitry Andric } else if (CurTy->isFP128Ty()) 3067*0b57cec5SDimitry Andric V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad(), 3068*0b57cec5SDimitry Andric APInt(128, Record))); 3069*0b57cec5SDimitry Andric else if (CurTy->isPPC_FP128Ty()) 3070*0b57cec5SDimitry Andric V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble(), 3071*0b57cec5SDimitry Andric APInt(128, Record))); 3072*0b57cec5SDimitry Andric else 3073*0b57cec5SDimitry Andric V = UndefValue::get(CurTy); 3074*0b57cec5SDimitry Andric break; 3075*0b57cec5SDimitry Andric } 3076*0b57cec5SDimitry Andric 3077*0b57cec5SDimitry Andric case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number] 3078*0b57cec5SDimitry Andric if (Record.empty()) 307981ad6265SDimitry Andric return error("Invalid aggregate record"); 3080*0b57cec5SDimitry Andric 3081*0b57cec5SDimitry Andric unsigned Size = Record.size(); 308281ad6265SDimitry Andric SmallVector<unsigned, 16> Elts; 308381ad6265SDimitry Andric for (unsigned i = 0; i != Size; ++i) 308481ad6265SDimitry Andric Elts.push_back(Record[i]); 3085*0b57cec5SDimitry Andric 308681ad6265SDimitry Andric if (isa<StructType>(CurTy)) { 308781ad6265SDimitry Andric V = BitcodeConstant::create( 308881ad6265SDimitry Andric Alloc, CurTy, BitcodeConstant::ConstantStructOpcode, Elts); 308981ad6265SDimitry Andric } else if (isa<ArrayType>(CurTy)) { 309081ad6265SDimitry Andric V = BitcodeConstant::create(Alloc, CurTy, 309181ad6265SDimitry Andric BitcodeConstant::ConstantArrayOpcode, Elts); 309281ad6265SDimitry Andric } else if (isa<VectorType>(CurTy)) { 309381ad6265SDimitry Andric V = BitcodeConstant::create( 309481ad6265SDimitry Andric Alloc, CurTy, BitcodeConstant::ConstantVectorOpcode, Elts); 3095*0b57cec5SDimitry Andric } else { 3096*0b57cec5SDimitry Andric V = UndefValue::get(CurTy); 3097*0b57cec5SDimitry Andric } 3098*0b57cec5SDimitry Andric break; 3099*0b57cec5SDimitry Andric } 3100*0b57cec5SDimitry Andric case bitc::CST_CODE_STRING: // STRING: [values] 3101*0b57cec5SDimitry Andric case bitc::CST_CODE_CSTRING: { // CSTRING: [values] 3102*0b57cec5SDimitry Andric if (Record.empty()) 310381ad6265SDimitry Andric return error("Invalid string record"); 3104*0b57cec5SDimitry Andric 3105*0b57cec5SDimitry Andric SmallString<16> Elts(Record.begin(), Record.end()); 3106*0b57cec5SDimitry Andric V = ConstantDataArray::getString(Context, Elts, 3107*0b57cec5SDimitry Andric BitCode == bitc::CST_CODE_CSTRING); 3108*0b57cec5SDimitry Andric break; 3109*0b57cec5SDimitry Andric } 3110*0b57cec5SDimitry Andric case bitc::CST_CODE_DATA: {// DATA: [n x value] 3111*0b57cec5SDimitry Andric if (Record.empty()) 311281ad6265SDimitry Andric return error("Invalid data record"); 3113*0b57cec5SDimitry Andric 31145ffd83dbSDimitry Andric Type *EltTy; 31155ffd83dbSDimitry Andric if (auto *Array = dyn_cast<ArrayType>(CurTy)) 31165ffd83dbSDimitry Andric EltTy = Array->getElementType(); 31175ffd83dbSDimitry Andric else 31185ffd83dbSDimitry Andric EltTy = cast<VectorType>(CurTy)->getElementType(); 3119*0b57cec5SDimitry Andric if (EltTy->isIntegerTy(8)) { 3120*0b57cec5SDimitry Andric SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end()); 3121*0b57cec5SDimitry Andric if (isa<VectorType>(CurTy)) 3122*0b57cec5SDimitry Andric V = ConstantDataVector::get(Context, Elts); 3123*0b57cec5SDimitry Andric else 3124*0b57cec5SDimitry Andric V = ConstantDataArray::get(Context, Elts); 3125*0b57cec5SDimitry Andric } else if (EltTy->isIntegerTy(16)) { 3126*0b57cec5SDimitry Andric SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); 3127*0b57cec5SDimitry Andric if (isa<VectorType>(CurTy)) 3128*0b57cec5SDimitry Andric V = ConstantDataVector::get(Context, Elts); 3129*0b57cec5SDimitry Andric else 3130*0b57cec5SDimitry Andric V = ConstantDataArray::get(Context, Elts); 3131*0b57cec5SDimitry Andric } else if (EltTy->isIntegerTy(32)) { 3132*0b57cec5SDimitry Andric SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); 3133*0b57cec5SDimitry Andric if (isa<VectorType>(CurTy)) 3134*0b57cec5SDimitry Andric V = ConstantDataVector::get(Context, Elts); 3135*0b57cec5SDimitry Andric else 3136*0b57cec5SDimitry Andric V = ConstantDataArray::get(Context, Elts); 3137*0b57cec5SDimitry Andric } else if (EltTy->isIntegerTy(64)) { 3138*0b57cec5SDimitry Andric SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); 3139*0b57cec5SDimitry Andric if (isa<VectorType>(CurTy)) 3140*0b57cec5SDimitry Andric V = ConstantDataVector::get(Context, Elts); 3141*0b57cec5SDimitry Andric else 3142*0b57cec5SDimitry Andric V = ConstantDataArray::get(Context, Elts); 3143*0b57cec5SDimitry Andric } else if (EltTy->isHalfTy()) { 3144*0b57cec5SDimitry Andric SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); 3145*0b57cec5SDimitry Andric if (isa<VectorType>(CurTy)) 31465ffd83dbSDimitry Andric V = ConstantDataVector::getFP(EltTy, Elts); 3147*0b57cec5SDimitry Andric else 31485ffd83dbSDimitry Andric V = ConstantDataArray::getFP(EltTy, Elts); 31495ffd83dbSDimitry Andric } else if (EltTy->isBFloatTy()) { 31505ffd83dbSDimitry Andric SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); 31515ffd83dbSDimitry Andric if (isa<VectorType>(CurTy)) 31525ffd83dbSDimitry Andric V = ConstantDataVector::getFP(EltTy, Elts); 31535ffd83dbSDimitry Andric else 31545ffd83dbSDimitry Andric V = ConstantDataArray::getFP(EltTy, Elts); 3155*0b57cec5SDimitry Andric } else if (EltTy->isFloatTy()) { 3156*0b57cec5SDimitry Andric SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); 3157*0b57cec5SDimitry Andric if (isa<VectorType>(CurTy)) 31585ffd83dbSDimitry Andric V = ConstantDataVector::getFP(EltTy, Elts); 3159*0b57cec5SDimitry Andric else 31605ffd83dbSDimitry Andric V = ConstantDataArray::getFP(EltTy, Elts); 3161*0b57cec5SDimitry Andric } else if (EltTy->isDoubleTy()) { 3162*0b57cec5SDimitry Andric SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); 3163*0b57cec5SDimitry Andric if (isa<VectorType>(CurTy)) 31645ffd83dbSDimitry Andric V = ConstantDataVector::getFP(EltTy, Elts); 3165*0b57cec5SDimitry Andric else 31665ffd83dbSDimitry Andric V = ConstantDataArray::getFP(EltTy, Elts); 3167*0b57cec5SDimitry Andric } else { 3168*0b57cec5SDimitry Andric return error("Invalid type for value"); 3169*0b57cec5SDimitry Andric } 3170*0b57cec5SDimitry Andric break; 3171*0b57cec5SDimitry Andric } 3172*0b57cec5SDimitry Andric case bitc::CST_CODE_CE_UNOP: { // CE_UNOP: [opcode, opval] 3173*0b57cec5SDimitry Andric if (Record.size() < 2) 317481ad6265SDimitry Andric return error("Invalid unary op constexpr record"); 3175*0b57cec5SDimitry Andric int Opc = getDecodedUnaryOpcode(Record[0], CurTy); 3176*0b57cec5SDimitry Andric if (Opc < 0) { 3177*0b57cec5SDimitry Andric V = UndefValue::get(CurTy); // Unknown unop. 3178*0b57cec5SDimitry Andric } else { 317981ad6265SDimitry Andric V = BitcodeConstant::create(Alloc, CurTy, Opc, (unsigned)Record[1]); 3180*0b57cec5SDimitry Andric } 3181*0b57cec5SDimitry Andric break; 3182*0b57cec5SDimitry Andric } 3183*0b57cec5SDimitry Andric case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval] 3184*0b57cec5SDimitry Andric if (Record.size() < 3) 318581ad6265SDimitry Andric return error("Invalid binary op constexpr record"); 3186*0b57cec5SDimitry Andric int Opc = getDecodedBinaryOpcode(Record[0], CurTy); 3187*0b57cec5SDimitry Andric if (Opc < 0) { 3188*0b57cec5SDimitry Andric V = UndefValue::get(CurTy); // Unknown binop. 3189*0b57cec5SDimitry Andric } else { 319081ad6265SDimitry Andric uint8_t Flags = 0; 3191*0b57cec5SDimitry Andric if (Record.size() >= 4) { 3192*0b57cec5SDimitry Andric if (Opc == Instruction::Add || 3193*0b57cec5SDimitry Andric Opc == Instruction::Sub || 3194*0b57cec5SDimitry Andric Opc == Instruction::Mul || 3195*0b57cec5SDimitry Andric Opc == Instruction::Shl) { 3196*0b57cec5SDimitry Andric if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP)) 3197*0b57cec5SDimitry Andric Flags |= OverflowingBinaryOperator::NoSignedWrap; 3198*0b57cec5SDimitry Andric if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) 3199*0b57cec5SDimitry Andric Flags |= OverflowingBinaryOperator::NoUnsignedWrap; 3200*0b57cec5SDimitry Andric } else if (Opc == Instruction::SDiv || 3201*0b57cec5SDimitry Andric Opc == Instruction::UDiv || 3202*0b57cec5SDimitry Andric Opc == Instruction::LShr || 3203*0b57cec5SDimitry Andric Opc == Instruction::AShr) { 3204*0b57cec5SDimitry Andric if (Record[3] & (1 << bitc::PEO_EXACT)) 3205*0b57cec5SDimitry Andric Flags |= SDivOperator::IsExact; 3206*0b57cec5SDimitry Andric } 3207*0b57cec5SDimitry Andric } 320881ad6265SDimitry Andric V = BitcodeConstant::create(Alloc, CurTy, {(uint8_t)Opc, Flags}, 320981ad6265SDimitry Andric {(unsigned)Record[1], (unsigned)Record[2]}); 3210*0b57cec5SDimitry Andric } 3211*0b57cec5SDimitry Andric break; 3212*0b57cec5SDimitry Andric } 3213*0b57cec5SDimitry Andric case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval] 3214*0b57cec5SDimitry Andric if (Record.size() < 3) 321581ad6265SDimitry Andric return error("Invalid cast constexpr record"); 3216*0b57cec5SDimitry Andric int Opc = getDecodedCastOpcode(Record[0]); 3217*0b57cec5SDimitry Andric if (Opc < 0) { 3218*0b57cec5SDimitry Andric V = UndefValue::get(CurTy); // Unknown cast. 3219*0b57cec5SDimitry Andric } else { 322081ad6265SDimitry Andric unsigned OpTyID = Record[1]; 322181ad6265SDimitry Andric Type *OpTy = getTypeByID(OpTyID); 3222*0b57cec5SDimitry Andric if (!OpTy) 322381ad6265SDimitry Andric return error("Invalid cast constexpr record"); 322481ad6265SDimitry Andric V = BitcodeConstant::create(Alloc, CurTy, Opc, (unsigned)Record[2]); 3225*0b57cec5SDimitry Andric } 3226*0b57cec5SDimitry Andric break; 3227*0b57cec5SDimitry Andric } 3228*0b57cec5SDimitry Andric case bitc::CST_CODE_CE_INBOUNDS_GEP: // [ty, n x operands] 3229*0b57cec5SDimitry Andric case bitc::CST_CODE_CE_GEP: // [ty, n x operands] 3230*0b57cec5SDimitry Andric case bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX: { // [ty, flags, n x 3231*0b57cec5SDimitry Andric // operands] 323281ad6265SDimitry Andric if (Record.size() < 2) 323381ad6265SDimitry Andric return error("Constant GEP record must have at least two elements"); 3234*0b57cec5SDimitry Andric unsigned OpNum = 0; 3235*0b57cec5SDimitry Andric Type *PointeeType = nullptr; 3236*0b57cec5SDimitry Andric if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX || 3237*0b57cec5SDimitry Andric Record.size() % 2) 3238*0b57cec5SDimitry Andric PointeeType = getTypeByID(Record[OpNum++]); 3239*0b57cec5SDimitry Andric 3240*0b57cec5SDimitry Andric bool InBounds = false; 3241bdd1243dSDimitry Andric std::optional<unsigned> InRangeIndex; 3242*0b57cec5SDimitry Andric if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX) { 3243*0b57cec5SDimitry Andric uint64_t Op = Record[OpNum++]; 3244*0b57cec5SDimitry Andric InBounds = Op & 1; 3245*0b57cec5SDimitry Andric InRangeIndex = Op >> 1; 3246*0b57cec5SDimitry Andric } else if (BitCode == bitc::CST_CODE_CE_INBOUNDS_GEP) 3247*0b57cec5SDimitry Andric InBounds = true; 3248*0b57cec5SDimitry Andric 324981ad6265SDimitry Andric SmallVector<unsigned, 16> Elts; 325081ad6265SDimitry Andric unsigned BaseTypeID = Record[OpNum]; 3251*0b57cec5SDimitry Andric while (OpNum != Record.size()) { 325281ad6265SDimitry Andric unsigned ElTyID = Record[OpNum++]; 325381ad6265SDimitry Andric Type *ElTy = getTypeByID(ElTyID); 3254*0b57cec5SDimitry Andric if (!ElTy) 325581ad6265SDimitry Andric return error("Invalid getelementptr constexpr record"); 325681ad6265SDimitry Andric Elts.push_back(Record[OpNum++]); 3257*0b57cec5SDimitry Andric } 3258*0b57cec5SDimitry Andric 3259*0b57cec5SDimitry Andric if (Elts.size() < 1) 3260*0b57cec5SDimitry Andric return error("Invalid gep with no operands"); 3261*0b57cec5SDimitry Andric 326281ad6265SDimitry Andric Type *BaseType = getTypeByID(BaseTypeID); 326381ad6265SDimitry Andric if (isa<VectorType>(BaseType)) { 326481ad6265SDimitry Andric BaseTypeID = getContainedTypeID(BaseTypeID, 0); 326581ad6265SDimitry Andric BaseType = getTypeByID(BaseTypeID); 326681ad6265SDimitry Andric } 326781ad6265SDimitry Andric 326881ad6265SDimitry Andric PointerType *OrigPtrTy = dyn_cast_or_null<PointerType>(BaseType); 326981ad6265SDimitry Andric if (!OrigPtrTy) 327081ad6265SDimitry Andric return error("GEP base operand must be pointer or vector of pointer"); 327181ad6265SDimitry Andric 327281ad6265SDimitry Andric if (!PointeeType) { 327381ad6265SDimitry Andric PointeeType = getPtrElementTypeByID(BaseTypeID); 3274*0b57cec5SDimitry Andric if (!PointeeType) 327581ad6265SDimitry Andric return error("Missing element type for old-style constant GEP"); 327681ad6265SDimitry Andric } else if (!OrigPtrTy->isOpaqueOrPointeeTypeMatches(PointeeType)) 3277*0b57cec5SDimitry Andric return error("Explicit gep operator type does not match pointee type " 3278*0b57cec5SDimitry Andric "of pointer operand"); 3279*0b57cec5SDimitry Andric 328081ad6265SDimitry Andric V = BitcodeConstant::create(Alloc, CurTy, 328181ad6265SDimitry Andric {Instruction::GetElementPtr, InBounds, 328281ad6265SDimitry Andric InRangeIndex.value_or(-1), PointeeType}, 328381ad6265SDimitry Andric Elts); 3284*0b57cec5SDimitry Andric break; 3285*0b57cec5SDimitry Andric } 3286*0b57cec5SDimitry Andric case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#] 3287*0b57cec5SDimitry Andric if (Record.size() < 3) 328881ad6265SDimitry Andric return error("Invalid select constexpr record"); 3289*0b57cec5SDimitry Andric 329081ad6265SDimitry Andric V = BitcodeConstant::create( 329181ad6265SDimitry Andric Alloc, CurTy, Instruction::Select, 329281ad6265SDimitry Andric {(unsigned)Record[0], (unsigned)Record[1], (unsigned)Record[2]}); 329381ad6265SDimitry Andric break; 3294*0b57cec5SDimitry Andric } 3295*0b57cec5SDimitry Andric case bitc::CST_CODE_CE_EXTRACTELT 3296*0b57cec5SDimitry Andric : { // CE_EXTRACTELT: [opty, opval, opty, opval] 3297*0b57cec5SDimitry Andric if (Record.size() < 3) 329881ad6265SDimitry Andric return error("Invalid extractelement constexpr record"); 329981ad6265SDimitry Andric unsigned OpTyID = Record[0]; 3300*0b57cec5SDimitry Andric VectorType *OpTy = 330181ad6265SDimitry Andric dyn_cast_or_null<VectorType>(getTypeByID(OpTyID)); 3302*0b57cec5SDimitry Andric if (!OpTy) 330381ad6265SDimitry Andric return error("Invalid extractelement constexpr record"); 330481ad6265SDimitry Andric unsigned IdxRecord; 3305*0b57cec5SDimitry Andric if (Record.size() == 4) { 330681ad6265SDimitry Andric unsigned IdxTyID = Record[2]; 330781ad6265SDimitry Andric Type *IdxTy = getTypeByID(IdxTyID); 3308*0b57cec5SDimitry Andric if (!IdxTy) 330981ad6265SDimitry Andric return error("Invalid extractelement constexpr record"); 331081ad6265SDimitry Andric IdxRecord = Record[3]; 33115ffd83dbSDimitry Andric } else { 33125ffd83dbSDimitry Andric // Deprecated, but still needed to read old bitcode files. 331381ad6265SDimitry Andric IdxRecord = Record[2]; 33145ffd83dbSDimitry Andric } 331581ad6265SDimitry Andric V = BitcodeConstant::create(Alloc, CurTy, Instruction::ExtractElement, 331681ad6265SDimitry Andric {(unsigned)Record[1], IdxRecord}); 3317*0b57cec5SDimitry Andric break; 3318*0b57cec5SDimitry Andric } 3319*0b57cec5SDimitry Andric case bitc::CST_CODE_CE_INSERTELT 3320*0b57cec5SDimitry Andric : { // CE_INSERTELT: [opval, opval, opty, opval] 3321*0b57cec5SDimitry Andric VectorType *OpTy = dyn_cast<VectorType>(CurTy); 3322*0b57cec5SDimitry Andric if (Record.size() < 3 || !OpTy) 332381ad6265SDimitry Andric return error("Invalid insertelement constexpr record"); 332481ad6265SDimitry Andric unsigned IdxRecord; 3325*0b57cec5SDimitry Andric if (Record.size() == 4) { 332681ad6265SDimitry Andric unsigned IdxTyID = Record[2]; 332781ad6265SDimitry Andric Type *IdxTy = getTypeByID(IdxTyID); 3328*0b57cec5SDimitry Andric if (!IdxTy) 332981ad6265SDimitry Andric return error("Invalid insertelement constexpr record"); 333081ad6265SDimitry Andric IdxRecord = Record[3]; 33315ffd83dbSDimitry Andric } else { 33325ffd83dbSDimitry Andric // Deprecated, but still needed to read old bitcode files. 333381ad6265SDimitry Andric IdxRecord = Record[2]; 33345ffd83dbSDimitry Andric } 333581ad6265SDimitry Andric V = BitcodeConstant::create( 333681ad6265SDimitry Andric Alloc, CurTy, Instruction::InsertElement, 333781ad6265SDimitry Andric {(unsigned)Record[0], (unsigned)Record[1], IdxRecord}); 3338*0b57cec5SDimitry Andric break; 3339*0b57cec5SDimitry Andric } 3340*0b57cec5SDimitry Andric case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval] 3341*0b57cec5SDimitry Andric VectorType *OpTy = dyn_cast<VectorType>(CurTy); 3342*0b57cec5SDimitry Andric if (Record.size() < 3 || !OpTy) 334381ad6265SDimitry Andric return error("Invalid shufflevector constexpr record"); 334481ad6265SDimitry Andric V = BitcodeConstant::create( 334581ad6265SDimitry Andric Alloc, CurTy, Instruction::ShuffleVector, 334681ad6265SDimitry Andric {(unsigned)Record[0], (unsigned)Record[1], (unsigned)Record[2]}); 334781ad6265SDimitry Andric break; 3348*0b57cec5SDimitry Andric } 3349*0b57cec5SDimitry Andric case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval] 3350*0b57cec5SDimitry Andric VectorType *RTy = dyn_cast<VectorType>(CurTy); 3351*0b57cec5SDimitry Andric VectorType *OpTy = 3352*0b57cec5SDimitry Andric dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); 3353*0b57cec5SDimitry Andric if (Record.size() < 4 || !RTy || !OpTy) 335481ad6265SDimitry Andric return error("Invalid shufflevector constexpr record"); 335581ad6265SDimitry Andric V = BitcodeConstant::create( 335681ad6265SDimitry Andric Alloc, CurTy, Instruction::ShuffleVector, 335781ad6265SDimitry Andric {(unsigned)Record[1], (unsigned)Record[2], (unsigned)Record[3]}); 335881ad6265SDimitry Andric break; 3359*0b57cec5SDimitry Andric } 3360*0b57cec5SDimitry Andric case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred] 3361*0b57cec5SDimitry Andric if (Record.size() < 4) 336281ad6265SDimitry Andric return error("Invalid cmp constexpt record"); 336381ad6265SDimitry Andric unsigned OpTyID = Record[0]; 336481ad6265SDimitry Andric Type *OpTy = getTypeByID(OpTyID); 3365*0b57cec5SDimitry Andric if (!OpTy) 336681ad6265SDimitry Andric return error("Invalid cmp constexpr record"); 336781ad6265SDimitry Andric V = BitcodeConstant::create( 336881ad6265SDimitry Andric Alloc, CurTy, 336981ad6265SDimitry Andric {(uint8_t)(OpTy->isFPOrFPVectorTy() ? Instruction::FCmp 337081ad6265SDimitry Andric : Instruction::ICmp), 337181ad6265SDimitry Andric (uint8_t)Record[3]}, 337281ad6265SDimitry Andric {(unsigned)Record[1], (unsigned)Record[2]}); 3373*0b57cec5SDimitry Andric break; 3374*0b57cec5SDimitry Andric } 3375*0b57cec5SDimitry Andric // This maintains backward compatibility, pre-asm dialect keywords. 33765ffd83dbSDimitry Andric // Deprecated, but still needed to read old bitcode files. 3377*0b57cec5SDimitry Andric case bitc::CST_CODE_INLINEASM_OLD: { 3378*0b57cec5SDimitry Andric if (Record.size() < 2) 337981ad6265SDimitry Andric return error("Invalid inlineasm record"); 3380*0b57cec5SDimitry Andric std::string AsmStr, ConstrStr; 3381*0b57cec5SDimitry Andric bool HasSideEffects = Record[0] & 1; 3382*0b57cec5SDimitry Andric bool IsAlignStack = Record[0] >> 1; 3383*0b57cec5SDimitry Andric unsigned AsmStrSize = Record[1]; 3384*0b57cec5SDimitry Andric if (2+AsmStrSize >= Record.size()) 338581ad6265SDimitry Andric return error("Invalid inlineasm record"); 3386*0b57cec5SDimitry Andric unsigned ConstStrSize = Record[2+AsmStrSize]; 3387*0b57cec5SDimitry Andric if (3+AsmStrSize+ConstStrSize > Record.size()) 338881ad6265SDimitry Andric return error("Invalid inlineasm record"); 3389*0b57cec5SDimitry Andric 3390*0b57cec5SDimitry Andric for (unsigned i = 0; i != AsmStrSize; ++i) 3391*0b57cec5SDimitry Andric AsmStr += (char)Record[2+i]; 3392*0b57cec5SDimitry Andric for (unsigned i = 0; i != ConstStrSize; ++i) 3393*0b57cec5SDimitry Andric ConstrStr += (char)Record[3+AsmStrSize+i]; 3394*0b57cec5SDimitry Andric UpgradeInlineAsmString(&AsmStr); 339581ad6265SDimitry Andric if (!CurElemTy) 339681ad6265SDimitry Andric return error("Missing element type for old-style inlineasm"); 339781ad6265SDimitry Andric V = InlineAsm::get(cast<FunctionType>(CurElemTy), AsmStr, ConstrStr, 339881ad6265SDimitry Andric HasSideEffects, IsAlignStack); 3399*0b57cec5SDimitry Andric break; 3400*0b57cec5SDimitry Andric } 3401*0b57cec5SDimitry Andric // This version adds support for the asm dialect keywords (e.g., 3402*0b57cec5SDimitry Andric // inteldialect). 3403fe6060f1SDimitry Andric case bitc::CST_CODE_INLINEASM_OLD2: { 3404*0b57cec5SDimitry Andric if (Record.size() < 2) 340581ad6265SDimitry Andric return error("Invalid inlineasm record"); 3406*0b57cec5SDimitry Andric std::string AsmStr, ConstrStr; 3407*0b57cec5SDimitry Andric bool HasSideEffects = Record[0] & 1; 3408*0b57cec5SDimitry Andric bool IsAlignStack = (Record[0] >> 1) & 1; 3409*0b57cec5SDimitry Andric unsigned AsmDialect = Record[0] >> 2; 3410*0b57cec5SDimitry Andric unsigned AsmStrSize = Record[1]; 3411*0b57cec5SDimitry Andric if (2+AsmStrSize >= Record.size()) 341281ad6265SDimitry Andric return error("Invalid inlineasm record"); 3413*0b57cec5SDimitry Andric unsigned ConstStrSize = Record[2+AsmStrSize]; 3414*0b57cec5SDimitry Andric if (3+AsmStrSize+ConstStrSize > Record.size()) 341581ad6265SDimitry Andric return error("Invalid inlineasm record"); 3416*0b57cec5SDimitry Andric 3417*0b57cec5SDimitry Andric for (unsigned i = 0; i != AsmStrSize; ++i) 3418*0b57cec5SDimitry Andric AsmStr += (char)Record[2+i]; 3419*0b57cec5SDimitry Andric for (unsigned i = 0; i != ConstStrSize; ++i) 3420*0b57cec5SDimitry Andric ConstrStr += (char)Record[3+AsmStrSize+i]; 3421*0b57cec5SDimitry Andric UpgradeInlineAsmString(&AsmStr); 342281ad6265SDimitry Andric if (!CurElemTy) 342381ad6265SDimitry Andric return error("Missing element type for old-style inlineasm"); 342481ad6265SDimitry Andric V = InlineAsm::get(cast<FunctionType>(CurElemTy), AsmStr, ConstrStr, 342581ad6265SDimitry Andric HasSideEffects, IsAlignStack, 3426*0b57cec5SDimitry Andric InlineAsm::AsmDialect(AsmDialect)); 3427*0b57cec5SDimitry Andric break; 3428*0b57cec5SDimitry Andric } 3429fe6060f1SDimitry Andric // This version adds support for the unwind keyword. 343004eeddc0SDimitry Andric case bitc::CST_CODE_INLINEASM_OLD3: { 3431fe6060f1SDimitry Andric if (Record.size() < 2) 343281ad6265SDimitry Andric return error("Invalid inlineasm record"); 343304eeddc0SDimitry Andric unsigned OpNum = 0; 3434fe6060f1SDimitry Andric std::string AsmStr, ConstrStr; 343504eeddc0SDimitry Andric bool HasSideEffects = Record[OpNum] & 1; 343604eeddc0SDimitry Andric bool IsAlignStack = (Record[OpNum] >> 1) & 1; 343704eeddc0SDimitry Andric unsigned AsmDialect = (Record[OpNum] >> 2) & 1; 343804eeddc0SDimitry Andric bool CanThrow = (Record[OpNum] >> 3) & 1; 343904eeddc0SDimitry Andric ++OpNum; 344004eeddc0SDimitry Andric unsigned AsmStrSize = Record[OpNum]; 344104eeddc0SDimitry Andric ++OpNum; 344204eeddc0SDimitry Andric if (OpNum + AsmStrSize >= Record.size()) 344381ad6265SDimitry Andric return error("Invalid inlineasm record"); 344404eeddc0SDimitry Andric unsigned ConstStrSize = Record[OpNum + AsmStrSize]; 344504eeddc0SDimitry Andric if (OpNum + 1 + AsmStrSize + ConstStrSize > Record.size()) 344681ad6265SDimitry Andric return error("Invalid inlineasm record"); 3447fe6060f1SDimitry Andric 3448fe6060f1SDimitry Andric for (unsigned i = 0; i != AsmStrSize; ++i) 344904eeddc0SDimitry Andric AsmStr += (char)Record[OpNum + i]; 345004eeddc0SDimitry Andric ++OpNum; 3451fe6060f1SDimitry Andric for (unsigned i = 0; i != ConstStrSize; ++i) 345204eeddc0SDimitry Andric ConstrStr += (char)Record[OpNum + AsmStrSize + i]; 3453fe6060f1SDimitry Andric UpgradeInlineAsmString(&AsmStr); 345481ad6265SDimitry Andric if (!CurElemTy) 345581ad6265SDimitry Andric return error("Missing element type for old-style inlineasm"); 345681ad6265SDimitry Andric V = InlineAsm::get(cast<FunctionType>(CurElemTy), AsmStr, ConstrStr, 345781ad6265SDimitry Andric HasSideEffects, IsAlignStack, 3458fe6060f1SDimitry Andric InlineAsm::AsmDialect(AsmDialect), CanThrow); 3459fe6060f1SDimitry Andric break; 3460fe6060f1SDimitry Andric } 346104eeddc0SDimitry Andric // This version adds explicit function type. 346204eeddc0SDimitry Andric case bitc::CST_CODE_INLINEASM: { 346304eeddc0SDimitry Andric if (Record.size() < 3) 346481ad6265SDimitry Andric return error("Invalid inlineasm record"); 346504eeddc0SDimitry Andric unsigned OpNum = 0; 346604eeddc0SDimitry Andric auto *FnTy = dyn_cast_or_null<FunctionType>(getTypeByID(Record[OpNum])); 346704eeddc0SDimitry Andric ++OpNum; 346804eeddc0SDimitry Andric if (!FnTy) 346981ad6265SDimitry Andric return error("Invalid inlineasm record"); 347004eeddc0SDimitry Andric std::string AsmStr, ConstrStr; 347104eeddc0SDimitry Andric bool HasSideEffects = Record[OpNum] & 1; 347204eeddc0SDimitry Andric bool IsAlignStack = (Record[OpNum] >> 1) & 1; 347304eeddc0SDimitry Andric unsigned AsmDialect = (Record[OpNum] >> 2) & 1; 347404eeddc0SDimitry Andric bool CanThrow = (Record[OpNum] >> 3) & 1; 347504eeddc0SDimitry Andric ++OpNum; 347604eeddc0SDimitry Andric unsigned AsmStrSize = Record[OpNum]; 347704eeddc0SDimitry Andric ++OpNum; 347804eeddc0SDimitry Andric if (OpNum + AsmStrSize >= Record.size()) 347981ad6265SDimitry Andric return error("Invalid inlineasm record"); 348004eeddc0SDimitry Andric unsigned ConstStrSize = Record[OpNum + AsmStrSize]; 348104eeddc0SDimitry Andric if (OpNum + 1 + AsmStrSize + ConstStrSize > Record.size()) 348281ad6265SDimitry Andric return error("Invalid inlineasm record"); 348304eeddc0SDimitry Andric 348404eeddc0SDimitry Andric for (unsigned i = 0; i != AsmStrSize; ++i) 348504eeddc0SDimitry Andric AsmStr += (char)Record[OpNum + i]; 348604eeddc0SDimitry Andric ++OpNum; 348704eeddc0SDimitry Andric for (unsigned i = 0; i != ConstStrSize; ++i) 348804eeddc0SDimitry Andric ConstrStr += (char)Record[OpNum + AsmStrSize + i]; 348904eeddc0SDimitry Andric UpgradeInlineAsmString(&AsmStr); 349004eeddc0SDimitry Andric V = InlineAsm::get(FnTy, AsmStr, ConstrStr, HasSideEffects, IsAlignStack, 349104eeddc0SDimitry Andric InlineAsm::AsmDialect(AsmDialect), CanThrow); 349204eeddc0SDimitry Andric break; 349304eeddc0SDimitry Andric } 3494*0b57cec5SDimitry Andric case bitc::CST_CODE_BLOCKADDRESS:{ 3495*0b57cec5SDimitry Andric if (Record.size() < 3) 349681ad6265SDimitry Andric return error("Invalid blockaddress record"); 349781ad6265SDimitry Andric unsigned FnTyID = Record[0]; 349881ad6265SDimitry Andric Type *FnTy = getTypeByID(FnTyID); 3499*0b57cec5SDimitry Andric if (!FnTy) 350081ad6265SDimitry Andric return error("Invalid blockaddress record"); 350181ad6265SDimitry Andric V = BitcodeConstant::create( 350281ad6265SDimitry Andric Alloc, CurTy, 350381ad6265SDimitry Andric {BitcodeConstant::BlockAddressOpcode, 0, (unsigned)Record[2]}, 350481ad6265SDimitry Andric Record[1]); 3505*0b57cec5SDimitry Andric break; 3506*0b57cec5SDimitry Andric } 3507fe6060f1SDimitry Andric case bitc::CST_CODE_DSO_LOCAL_EQUIVALENT: { 3508fe6060f1SDimitry Andric if (Record.size() < 2) 350981ad6265SDimitry Andric return error("Invalid dso_local record"); 351081ad6265SDimitry Andric unsigned GVTyID = Record[0]; 351181ad6265SDimitry Andric Type *GVTy = getTypeByID(GVTyID); 3512fe6060f1SDimitry Andric if (!GVTy) 351381ad6265SDimitry Andric return error("Invalid dso_local record"); 351481ad6265SDimitry Andric V = BitcodeConstant::create( 351581ad6265SDimitry Andric Alloc, CurTy, BitcodeConstant::DSOLocalEquivalentOpcode, Record[1]); 3516fe6060f1SDimitry Andric break; 3517fe6060f1SDimitry Andric } 35180eae32dcSDimitry Andric case bitc::CST_CODE_NO_CFI_VALUE: { 35190eae32dcSDimitry Andric if (Record.size() < 2) 352081ad6265SDimitry Andric return error("Invalid no_cfi record"); 352181ad6265SDimitry Andric unsigned GVTyID = Record[0]; 352281ad6265SDimitry Andric Type *GVTy = getTypeByID(GVTyID); 35230eae32dcSDimitry Andric if (!GVTy) 352481ad6265SDimitry Andric return error("Invalid no_cfi record"); 352581ad6265SDimitry Andric V = BitcodeConstant::create(Alloc, CurTy, BitcodeConstant::NoCFIOpcode, 352681ad6265SDimitry Andric Record[1]); 35270eae32dcSDimitry Andric break; 35280eae32dcSDimitry Andric } 3529*0b57cec5SDimitry Andric } 3530*0b57cec5SDimitry Andric 353181ad6265SDimitry Andric assert(V->getType() == getTypeByID(CurTyID) && "Incorrect result type ID"); 353281ad6265SDimitry Andric if (Error Err = ValueList.assignValue(NextCstNo, V, CurTyID)) 353381ad6265SDimitry Andric return Err; 3534*0b57cec5SDimitry Andric ++NextCstNo; 3535*0b57cec5SDimitry Andric } 3536*0b57cec5SDimitry Andric } 3537*0b57cec5SDimitry Andric 3538*0b57cec5SDimitry Andric Error BitcodeReader::parseUseLists() { 3539*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID)) 3540*0b57cec5SDimitry Andric return Err; 3541*0b57cec5SDimitry Andric 3542*0b57cec5SDimitry Andric // Read all the records. 3543*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 3544*0b57cec5SDimitry Andric 3545*0b57cec5SDimitry Andric while (true) { 3546*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 3547*0b57cec5SDimitry Andric if (!MaybeEntry) 3548*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 3549*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 3550*0b57cec5SDimitry Andric 3551*0b57cec5SDimitry Andric switch (Entry.Kind) { 3552*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 3553*0b57cec5SDimitry Andric case BitstreamEntry::Error: 3554*0b57cec5SDimitry Andric return error("Malformed block"); 3555*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 3556*0b57cec5SDimitry Andric return Error::success(); 3557*0b57cec5SDimitry Andric case BitstreamEntry::Record: 3558*0b57cec5SDimitry Andric // The interesting case. 3559*0b57cec5SDimitry Andric break; 3560*0b57cec5SDimitry Andric } 3561*0b57cec5SDimitry Andric 3562*0b57cec5SDimitry Andric // Read a use list record. 3563*0b57cec5SDimitry Andric Record.clear(); 3564*0b57cec5SDimitry Andric bool IsBB = false; 3565*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 3566*0b57cec5SDimitry Andric if (!MaybeRecord) 3567*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 3568*0b57cec5SDimitry Andric switch (MaybeRecord.get()) { 3569*0b57cec5SDimitry Andric default: // Default behavior: unknown type. 3570*0b57cec5SDimitry Andric break; 3571*0b57cec5SDimitry Andric case bitc::USELIST_CODE_BB: 3572*0b57cec5SDimitry Andric IsBB = true; 3573bdd1243dSDimitry Andric [[fallthrough]]; 3574*0b57cec5SDimitry Andric case bitc::USELIST_CODE_DEFAULT: { 3575*0b57cec5SDimitry Andric unsigned RecordLength = Record.size(); 3576*0b57cec5SDimitry Andric if (RecordLength < 3) 3577*0b57cec5SDimitry Andric // Records should have at least an ID and two indexes. 3578*0b57cec5SDimitry Andric return error("Invalid record"); 3579e8d8bef9SDimitry Andric unsigned ID = Record.pop_back_val(); 3580*0b57cec5SDimitry Andric 3581*0b57cec5SDimitry Andric Value *V; 3582*0b57cec5SDimitry Andric if (IsBB) { 3583*0b57cec5SDimitry Andric assert(ID < FunctionBBs.size() && "Basic block not found"); 3584*0b57cec5SDimitry Andric V = FunctionBBs[ID]; 3585*0b57cec5SDimitry Andric } else 3586*0b57cec5SDimitry Andric V = ValueList[ID]; 3587*0b57cec5SDimitry Andric unsigned NumUses = 0; 3588*0b57cec5SDimitry Andric SmallDenseMap<const Use *, unsigned, 16> Order; 3589*0b57cec5SDimitry Andric for (const Use &U : V->materialized_uses()) { 3590*0b57cec5SDimitry Andric if (++NumUses > Record.size()) 3591*0b57cec5SDimitry Andric break; 3592*0b57cec5SDimitry Andric Order[&U] = Record[NumUses - 1]; 3593*0b57cec5SDimitry Andric } 3594*0b57cec5SDimitry Andric if (Order.size() != Record.size() || NumUses > Record.size()) 3595*0b57cec5SDimitry Andric // Mismatches can happen if the functions are being materialized lazily 3596*0b57cec5SDimitry Andric // (out-of-order), or a value has been upgraded. 3597*0b57cec5SDimitry Andric break; 3598*0b57cec5SDimitry Andric 3599*0b57cec5SDimitry Andric V->sortUseList([&](const Use &L, const Use &R) { 3600*0b57cec5SDimitry Andric return Order.lookup(&L) < Order.lookup(&R); 3601*0b57cec5SDimitry Andric }); 3602*0b57cec5SDimitry Andric break; 3603*0b57cec5SDimitry Andric } 3604*0b57cec5SDimitry Andric } 3605*0b57cec5SDimitry Andric } 3606*0b57cec5SDimitry Andric } 3607*0b57cec5SDimitry Andric 3608*0b57cec5SDimitry Andric /// When we see the block for metadata, remember where it is and then skip it. 3609*0b57cec5SDimitry Andric /// This lets us lazily deserialize the metadata. 3610*0b57cec5SDimitry Andric Error BitcodeReader::rememberAndSkipMetadata() { 3611*0b57cec5SDimitry Andric // Save the current stream state. 3612*0b57cec5SDimitry Andric uint64_t CurBit = Stream.GetCurrentBitNo(); 3613*0b57cec5SDimitry Andric DeferredMetadataInfo.push_back(CurBit); 3614*0b57cec5SDimitry Andric 3615*0b57cec5SDimitry Andric // Skip over the block for now. 3616*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 3617*0b57cec5SDimitry Andric return Err; 3618*0b57cec5SDimitry Andric return Error::success(); 3619*0b57cec5SDimitry Andric } 3620*0b57cec5SDimitry Andric 3621*0b57cec5SDimitry Andric Error BitcodeReader::materializeMetadata() { 3622*0b57cec5SDimitry Andric for (uint64_t BitPos : DeferredMetadataInfo) { 3623*0b57cec5SDimitry Andric // Move the bit stream to the saved position. 3624*0b57cec5SDimitry Andric if (Error JumpFailed = Stream.JumpToBit(BitPos)) 3625*0b57cec5SDimitry Andric return JumpFailed; 3626*0b57cec5SDimitry Andric if (Error Err = MDLoader->parseModuleMetadata()) 3627*0b57cec5SDimitry Andric return Err; 3628*0b57cec5SDimitry Andric } 3629*0b57cec5SDimitry Andric 3630*0b57cec5SDimitry Andric // Upgrade "Linker Options" module flag to "llvm.linker.options" module-level 3631e8d8bef9SDimitry Andric // metadata. Only upgrade if the new option doesn't exist to avoid upgrade 3632e8d8bef9SDimitry Andric // multiple times. 3633e8d8bef9SDimitry Andric if (!TheModule->getNamedMetadata("llvm.linker.options")) { 3634*0b57cec5SDimitry Andric if (Metadata *Val = TheModule->getModuleFlag("Linker Options")) { 3635*0b57cec5SDimitry Andric NamedMDNode *LinkerOpts = 3636*0b57cec5SDimitry Andric TheModule->getOrInsertNamedMetadata("llvm.linker.options"); 3637*0b57cec5SDimitry Andric for (const MDOperand &MDOptions : cast<MDNode>(Val)->operands()) 3638*0b57cec5SDimitry Andric LinkerOpts->addOperand(cast<MDNode>(MDOptions)); 3639*0b57cec5SDimitry Andric } 3640e8d8bef9SDimitry Andric } 3641*0b57cec5SDimitry Andric 3642*0b57cec5SDimitry Andric DeferredMetadataInfo.clear(); 3643*0b57cec5SDimitry Andric return Error::success(); 3644*0b57cec5SDimitry Andric } 3645*0b57cec5SDimitry Andric 3646*0b57cec5SDimitry Andric void BitcodeReader::setStripDebugInfo() { StripDebugInfo = true; } 3647*0b57cec5SDimitry Andric 3648*0b57cec5SDimitry Andric /// When we see the block for a function body, remember where it is and then 3649*0b57cec5SDimitry Andric /// skip it. This lets us lazily deserialize the functions. 3650*0b57cec5SDimitry Andric Error BitcodeReader::rememberAndSkipFunctionBody() { 3651*0b57cec5SDimitry Andric // Get the function we are talking about. 3652*0b57cec5SDimitry Andric if (FunctionsWithBodies.empty()) 3653*0b57cec5SDimitry Andric return error("Insufficient function protos"); 3654*0b57cec5SDimitry Andric 3655*0b57cec5SDimitry Andric Function *Fn = FunctionsWithBodies.back(); 3656*0b57cec5SDimitry Andric FunctionsWithBodies.pop_back(); 3657*0b57cec5SDimitry Andric 3658*0b57cec5SDimitry Andric // Save the current stream state. 3659*0b57cec5SDimitry Andric uint64_t CurBit = Stream.GetCurrentBitNo(); 3660*0b57cec5SDimitry Andric assert( 3661*0b57cec5SDimitry Andric (DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) && 3662*0b57cec5SDimitry Andric "Mismatch between VST and scanned function offsets"); 3663*0b57cec5SDimitry Andric DeferredFunctionInfo[Fn] = CurBit; 3664*0b57cec5SDimitry Andric 3665*0b57cec5SDimitry Andric // Skip over the function block for now. 3666*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 3667*0b57cec5SDimitry Andric return Err; 3668*0b57cec5SDimitry Andric return Error::success(); 3669*0b57cec5SDimitry Andric } 3670*0b57cec5SDimitry Andric 3671*0b57cec5SDimitry Andric Error BitcodeReader::globalCleanup() { 3672*0b57cec5SDimitry Andric // Patch the initializers for globals and aliases up. 3673*0b57cec5SDimitry Andric if (Error Err = resolveGlobalAndIndirectSymbolInits()) 3674*0b57cec5SDimitry Andric return Err; 3675*0b57cec5SDimitry Andric if (!GlobalInits.empty() || !IndirectSymbolInits.empty()) 3676*0b57cec5SDimitry Andric return error("Malformed global initializer set"); 3677*0b57cec5SDimitry Andric 3678*0b57cec5SDimitry Andric // Look for intrinsic functions which need to be upgraded at some point 36795ffd83dbSDimitry Andric // and functions that need to have their function attributes upgraded. 3680*0b57cec5SDimitry Andric for (Function &F : *TheModule) { 3681*0b57cec5SDimitry Andric MDLoader->upgradeDebugIntrinsics(F); 3682*0b57cec5SDimitry Andric Function *NewFn; 3683*0b57cec5SDimitry Andric if (UpgradeIntrinsicFunction(&F, NewFn)) 3684*0b57cec5SDimitry Andric UpgradedIntrinsics[&F] = NewFn; 36855ffd83dbSDimitry Andric // Look for functions that rely on old function attribute behavior. 36865ffd83dbSDimitry Andric UpgradeFunctionAttributes(F); 3687*0b57cec5SDimitry Andric } 3688*0b57cec5SDimitry Andric 3689*0b57cec5SDimitry Andric // Look for global variables which need to be renamed. 3690*0b57cec5SDimitry Andric std::vector<std::pair<GlobalVariable *, GlobalVariable *>> UpgradedVariables; 3691*0b57cec5SDimitry Andric for (GlobalVariable &GV : TheModule->globals()) 3692*0b57cec5SDimitry Andric if (GlobalVariable *Upgraded = UpgradeGlobalVariable(&GV)) 3693*0b57cec5SDimitry Andric UpgradedVariables.emplace_back(&GV, Upgraded); 3694*0b57cec5SDimitry Andric for (auto &Pair : UpgradedVariables) { 3695*0b57cec5SDimitry Andric Pair.first->eraseFromParent(); 3696*0b57cec5SDimitry Andric TheModule->getGlobalList().push_back(Pair.second); 3697*0b57cec5SDimitry Andric } 3698*0b57cec5SDimitry Andric 3699*0b57cec5SDimitry Andric // Force deallocation of memory for these vectors to favor the client that 3700*0b57cec5SDimitry Andric // want lazy deserialization. 3701*0b57cec5SDimitry Andric std::vector<std::pair<GlobalVariable *, unsigned>>().swap(GlobalInits); 3702349cc55cSDimitry Andric std::vector<std::pair<GlobalValue *, unsigned>>().swap(IndirectSymbolInits); 3703*0b57cec5SDimitry Andric return Error::success(); 3704*0b57cec5SDimitry Andric } 3705*0b57cec5SDimitry Andric 3706*0b57cec5SDimitry Andric /// Support for lazy parsing of function bodies. This is required if we 3707*0b57cec5SDimitry Andric /// either have an old bitcode file without a VST forward declaration record, 3708*0b57cec5SDimitry Andric /// or if we have an anonymous function being materialized, since anonymous 3709*0b57cec5SDimitry Andric /// functions do not have a name and are therefore not in the VST. 3710*0b57cec5SDimitry Andric Error BitcodeReader::rememberAndSkipFunctionBodies() { 3711*0b57cec5SDimitry Andric if (Error JumpFailed = Stream.JumpToBit(NextUnreadBit)) 3712*0b57cec5SDimitry Andric return JumpFailed; 3713*0b57cec5SDimitry Andric 3714*0b57cec5SDimitry Andric if (Stream.AtEndOfStream()) 3715*0b57cec5SDimitry Andric return error("Could not find function in stream"); 3716*0b57cec5SDimitry Andric 3717*0b57cec5SDimitry Andric if (!SeenFirstFunctionBody) 3718*0b57cec5SDimitry Andric return error("Trying to materialize functions before seeing function blocks"); 3719*0b57cec5SDimitry Andric 3720*0b57cec5SDimitry Andric // An old bitcode file with the symbol table at the end would have 3721*0b57cec5SDimitry Andric // finished the parse greedily. 3722*0b57cec5SDimitry Andric assert(SeenValueSymbolTable); 3723*0b57cec5SDimitry Andric 3724*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 3725*0b57cec5SDimitry Andric 3726*0b57cec5SDimitry Andric while (true) { 3727*0b57cec5SDimitry Andric Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 3728*0b57cec5SDimitry Andric if (!MaybeEntry) 3729*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 3730*0b57cec5SDimitry Andric llvm::BitstreamEntry Entry = MaybeEntry.get(); 3731*0b57cec5SDimitry Andric 3732*0b57cec5SDimitry Andric switch (Entry.Kind) { 3733*0b57cec5SDimitry Andric default: 3734*0b57cec5SDimitry Andric return error("Expect SubBlock"); 3735*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: 3736*0b57cec5SDimitry Andric switch (Entry.ID) { 3737*0b57cec5SDimitry Andric default: 3738*0b57cec5SDimitry Andric return error("Expect function block"); 3739*0b57cec5SDimitry Andric case bitc::FUNCTION_BLOCK_ID: 3740*0b57cec5SDimitry Andric if (Error Err = rememberAndSkipFunctionBody()) 3741*0b57cec5SDimitry Andric return Err; 3742*0b57cec5SDimitry Andric NextUnreadBit = Stream.GetCurrentBitNo(); 3743*0b57cec5SDimitry Andric return Error::success(); 3744*0b57cec5SDimitry Andric } 3745*0b57cec5SDimitry Andric } 3746*0b57cec5SDimitry Andric } 3747*0b57cec5SDimitry Andric } 3748*0b57cec5SDimitry Andric 374981ad6265SDimitry Andric Error BitcodeReaderBase::readBlockInfo() { 3750bdd1243dSDimitry Andric Expected<std::optional<BitstreamBlockInfo>> MaybeNewBlockInfo = 3751*0b57cec5SDimitry Andric Stream.ReadBlockInfoBlock(); 3752*0b57cec5SDimitry Andric if (!MaybeNewBlockInfo) 375381ad6265SDimitry Andric return MaybeNewBlockInfo.takeError(); 3754bdd1243dSDimitry Andric std::optional<BitstreamBlockInfo> NewBlockInfo = 3755*0b57cec5SDimitry Andric std::move(MaybeNewBlockInfo.get()); 3756*0b57cec5SDimitry Andric if (!NewBlockInfo) 375781ad6265SDimitry Andric return error("Malformed block"); 3758*0b57cec5SDimitry Andric BlockInfo = std::move(*NewBlockInfo); 375981ad6265SDimitry Andric return Error::success(); 3760*0b57cec5SDimitry Andric } 3761*0b57cec5SDimitry Andric 3762*0b57cec5SDimitry Andric Error BitcodeReader::parseComdatRecord(ArrayRef<uint64_t> Record) { 3763*0b57cec5SDimitry Andric // v1: [selection_kind, name] 3764*0b57cec5SDimitry Andric // v2: [strtab_offset, strtab_size, selection_kind] 3765*0b57cec5SDimitry Andric StringRef Name; 3766*0b57cec5SDimitry Andric std::tie(Name, Record) = readNameFromStrtab(Record); 3767*0b57cec5SDimitry Andric 3768*0b57cec5SDimitry Andric if (Record.empty()) 3769*0b57cec5SDimitry Andric return error("Invalid record"); 3770*0b57cec5SDimitry Andric Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]); 3771*0b57cec5SDimitry Andric std::string OldFormatName; 3772*0b57cec5SDimitry Andric if (!UseStrtab) { 3773*0b57cec5SDimitry Andric if (Record.size() < 2) 3774*0b57cec5SDimitry Andric return error("Invalid record"); 3775*0b57cec5SDimitry Andric unsigned ComdatNameSize = Record[1]; 377681ad6265SDimitry Andric if (ComdatNameSize > Record.size() - 2) 377781ad6265SDimitry Andric return error("Comdat name size too large"); 3778*0b57cec5SDimitry Andric OldFormatName.reserve(ComdatNameSize); 3779*0b57cec5SDimitry Andric for (unsigned i = 0; i != ComdatNameSize; ++i) 3780*0b57cec5SDimitry Andric OldFormatName += (char)Record[2 + i]; 3781*0b57cec5SDimitry Andric Name = OldFormatName; 3782*0b57cec5SDimitry Andric } 3783*0b57cec5SDimitry Andric Comdat *C = TheModule->getOrInsertComdat(Name); 3784*0b57cec5SDimitry Andric C->setSelectionKind(SK); 3785*0b57cec5SDimitry Andric ComdatList.push_back(C); 3786*0b57cec5SDimitry Andric return Error::success(); 3787*0b57cec5SDimitry Andric } 3788*0b57cec5SDimitry Andric 3789*0b57cec5SDimitry Andric static void inferDSOLocal(GlobalValue *GV) { 3790*0b57cec5SDimitry Andric // infer dso_local from linkage and visibility if it is not encoded. 3791*0b57cec5SDimitry Andric if (GV->hasLocalLinkage() || 3792*0b57cec5SDimitry Andric (!GV->hasDefaultVisibility() && !GV->hasExternalWeakLinkage())) 3793*0b57cec5SDimitry Andric GV->setDSOLocal(true); 3794*0b57cec5SDimitry Andric } 3795*0b57cec5SDimitry Andric 379681ad6265SDimitry Andric GlobalValue::SanitizerMetadata deserializeSanitizerMetadata(unsigned V) { 379781ad6265SDimitry Andric GlobalValue::SanitizerMetadata Meta; 379881ad6265SDimitry Andric if (V & (1 << 0)) 379981ad6265SDimitry Andric Meta.NoAddress = true; 380081ad6265SDimitry Andric if (V & (1 << 1)) 380181ad6265SDimitry Andric Meta.NoHWAddress = true; 380281ad6265SDimitry Andric if (V & (1 << 2)) 3803753f127fSDimitry Andric Meta.Memtag = true; 380481ad6265SDimitry Andric if (V & (1 << 3)) 380581ad6265SDimitry Andric Meta.IsDynInit = true; 380681ad6265SDimitry Andric return Meta; 380781ad6265SDimitry Andric } 380881ad6265SDimitry Andric 3809*0b57cec5SDimitry Andric Error BitcodeReader::parseGlobalVarRecord(ArrayRef<uint64_t> Record) { 3810*0b57cec5SDimitry Andric // v1: [pointer type, isconst, initid, linkage, alignment, section, 3811*0b57cec5SDimitry Andric // visibility, threadlocal, unnamed_addr, externally_initialized, 3812*0b57cec5SDimitry Andric // dllstorageclass, comdat, attributes, preemption specifier, 3813*0b57cec5SDimitry Andric // partition strtab offset, partition strtab size] (name in VST) 3814*0b57cec5SDimitry Andric // v2: [strtab_offset, strtab_size, v1] 3815*0b57cec5SDimitry Andric StringRef Name; 3816*0b57cec5SDimitry Andric std::tie(Name, Record) = readNameFromStrtab(Record); 3817*0b57cec5SDimitry Andric 3818*0b57cec5SDimitry Andric if (Record.size() < 6) 3819*0b57cec5SDimitry Andric return error("Invalid record"); 382081ad6265SDimitry Andric unsigned TyID = Record[0]; 382181ad6265SDimitry Andric Type *Ty = getTypeByID(TyID); 3822*0b57cec5SDimitry Andric if (!Ty) 3823*0b57cec5SDimitry Andric return error("Invalid record"); 3824*0b57cec5SDimitry Andric bool isConstant = Record[1] & 1; 3825*0b57cec5SDimitry Andric bool explicitType = Record[1] & 2; 3826*0b57cec5SDimitry Andric unsigned AddressSpace; 3827*0b57cec5SDimitry Andric if (explicitType) { 3828*0b57cec5SDimitry Andric AddressSpace = Record[1] >> 2; 3829*0b57cec5SDimitry Andric } else { 3830*0b57cec5SDimitry Andric if (!Ty->isPointerTy()) 3831*0b57cec5SDimitry Andric return error("Invalid type for value"); 3832*0b57cec5SDimitry Andric AddressSpace = cast<PointerType>(Ty)->getAddressSpace(); 383381ad6265SDimitry Andric TyID = getContainedTypeID(TyID); 383481ad6265SDimitry Andric Ty = getTypeByID(TyID); 383581ad6265SDimitry Andric if (!Ty) 383681ad6265SDimitry Andric return error("Missing element type for old-style global"); 3837*0b57cec5SDimitry Andric } 3838*0b57cec5SDimitry Andric 3839*0b57cec5SDimitry Andric uint64_t RawLinkage = Record[3]; 3840*0b57cec5SDimitry Andric GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage); 38418bcb0991SDimitry Andric MaybeAlign Alignment; 3842*0b57cec5SDimitry Andric if (Error Err = parseAlignmentValue(Record[4], Alignment)) 3843*0b57cec5SDimitry Andric return Err; 3844*0b57cec5SDimitry Andric std::string Section; 3845*0b57cec5SDimitry Andric if (Record[5]) { 3846*0b57cec5SDimitry Andric if (Record[5] - 1 >= SectionTable.size()) 3847*0b57cec5SDimitry Andric return error("Invalid ID"); 3848*0b57cec5SDimitry Andric Section = SectionTable[Record[5] - 1]; 3849*0b57cec5SDimitry Andric } 3850*0b57cec5SDimitry Andric GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility; 3851*0b57cec5SDimitry Andric // Local linkage must have default visibility. 38525ffd83dbSDimitry Andric // auto-upgrade `hidden` and `protected` for old bitcode. 3853*0b57cec5SDimitry Andric if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage)) 3854*0b57cec5SDimitry Andric Visibility = getDecodedVisibility(Record[6]); 3855*0b57cec5SDimitry Andric 3856*0b57cec5SDimitry Andric GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal; 3857*0b57cec5SDimitry Andric if (Record.size() > 7) 3858*0b57cec5SDimitry Andric TLM = getDecodedThreadLocalMode(Record[7]); 3859*0b57cec5SDimitry Andric 3860*0b57cec5SDimitry Andric GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None; 3861*0b57cec5SDimitry Andric if (Record.size() > 8) 3862*0b57cec5SDimitry Andric UnnamedAddr = getDecodedUnnamedAddrType(Record[8]); 3863*0b57cec5SDimitry Andric 3864*0b57cec5SDimitry Andric bool ExternallyInitialized = false; 3865*0b57cec5SDimitry Andric if (Record.size() > 9) 3866*0b57cec5SDimitry Andric ExternallyInitialized = Record[9]; 3867*0b57cec5SDimitry Andric 3868*0b57cec5SDimitry Andric GlobalVariable *NewGV = 3869*0b57cec5SDimitry Andric new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, Name, 3870*0b57cec5SDimitry Andric nullptr, TLM, AddressSpace, ExternallyInitialized); 3871*0b57cec5SDimitry Andric NewGV->setAlignment(Alignment); 3872*0b57cec5SDimitry Andric if (!Section.empty()) 3873*0b57cec5SDimitry Andric NewGV->setSection(Section); 3874*0b57cec5SDimitry Andric NewGV->setVisibility(Visibility); 3875*0b57cec5SDimitry Andric NewGV->setUnnamedAddr(UnnamedAddr); 3876*0b57cec5SDimitry Andric 3877bdd1243dSDimitry Andric if (Record.size() > 10) { 3878bdd1243dSDimitry Andric // A GlobalValue with local linkage cannot have a DLL storage class. 3879bdd1243dSDimitry Andric if (!NewGV->hasLocalLinkage()) { 3880*0b57cec5SDimitry Andric NewGV->setDLLStorageClass(getDecodedDLLStorageClass(Record[10])); 3881bdd1243dSDimitry Andric } 3882bdd1243dSDimitry Andric } else { 3883*0b57cec5SDimitry Andric upgradeDLLImportExportLinkage(NewGV, RawLinkage); 3884bdd1243dSDimitry Andric } 3885*0b57cec5SDimitry Andric 388681ad6265SDimitry Andric ValueList.push_back(NewGV, getVirtualTypeID(NewGV->getType(), TyID)); 3887*0b57cec5SDimitry Andric 3888*0b57cec5SDimitry Andric // Remember which value to use for the global initializer. 3889*0b57cec5SDimitry Andric if (unsigned InitID = Record[2]) 3890*0b57cec5SDimitry Andric GlobalInits.push_back(std::make_pair(NewGV, InitID - 1)); 3891*0b57cec5SDimitry Andric 3892*0b57cec5SDimitry Andric if (Record.size() > 11) { 3893*0b57cec5SDimitry Andric if (unsigned ComdatID = Record[11]) { 3894*0b57cec5SDimitry Andric if (ComdatID > ComdatList.size()) 3895*0b57cec5SDimitry Andric return error("Invalid global variable comdat ID"); 3896*0b57cec5SDimitry Andric NewGV->setComdat(ComdatList[ComdatID - 1]); 3897*0b57cec5SDimitry Andric } 3898*0b57cec5SDimitry Andric } else if (hasImplicitComdat(RawLinkage)) { 38990eae32dcSDimitry Andric ImplicitComdatObjects.insert(NewGV); 3900*0b57cec5SDimitry Andric } 3901*0b57cec5SDimitry Andric 3902*0b57cec5SDimitry Andric if (Record.size() > 12) { 3903349cc55cSDimitry Andric auto AS = getAttributes(Record[12]).getFnAttrs(); 3904*0b57cec5SDimitry Andric NewGV->setAttributes(AS); 3905*0b57cec5SDimitry Andric } 3906*0b57cec5SDimitry Andric 3907*0b57cec5SDimitry Andric if (Record.size() > 13) { 3908*0b57cec5SDimitry Andric NewGV->setDSOLocal(getDecodedDSOLocal(Record[13])); 3909*0b57cec5SDimitry Andric } 3910*0b57cec5SDimitry Andric inferDSOLocal(NewGV); 3911*0b57cec5SDimitry Andric 3912*0b57cec5SDimitry Andric // Check whether we have enough values to read a partition name. 3913*0b57cec5SDimitry Andric if (Record.size() > 15) 3914*0b57cec5SDimitry Andric NewGV->setPartition(StringRef(Strtab.data() + Record[14], Record[15])); 3915*0b57cec5SDimitry Andric 391681ad6265SDimitry Andric if (Record.size() > 16 && Record[16]) { 391781ad6265SDimitry Andric llvm::GlobalValue::SanitizerMetadata Meta = 391881ad6265SDimitry Andric deserializeSanitizerMetadata(Record[16]); 391981ad6265SDimitry Andric NewGV->setSanitizerMetadata(Meta); 392081ad6265SDimitry Andric } 392181ad6265SDimitry Andric 3922*0b57cec5SDimitry Andric return Error::success(); 3923*0b57cec5SDimitry Andric } 3924*0b57cec5SDimitry Andric 3925bdd1243dSDimitry Andric void BitcodeReader::callValueTypeCallback(Value *F, unsigned TypeID) { 3926bdd1243dSDimitry Andric if (ValueTypeCallback) { 3927bdd1243dSDimitry Andric (*ValueTypeCallback)( 3928bdd1243dSDimitry Andric F, TypeID, [this](unsigned I) { return getTypeByID(I); }, 3929bdd1243dSDimitry Andric [this](unsigned I, unsigned J) { return getContainedTypeID(I, J); }); 3930bdd1243dSDimitry Andric } 3931bdd1243dSDimitry Andric } 3932bdd1243dSDimitry Andric 3933*0b57cec5SDimitry Andric Error BitcodeReader::parseFunctionRecord(ArrayRef<uint64_t> Record) { 3934*0b57cec5SDimitry Andric // v1: [type, callingconv, isproto, linkage, paramattr, alignment, section, 3935*0b57cec5SDimitry Andric // visibility, gc, unnamed_addr, prologuedata, dllstorageclass, comdat, 3936*0b57cec5SDimitry Andric // prefixdata, personalityfn, preemption specifier, addrspace] (name in VST) 3937*0b57cec5SDimitry Andric // v2: [strtab_offset, strtab_size, v1] 3938*0b57cec5SDimitry Andric StringRef Name; 3939*0b57cec5SDimitry Andric std::tie(Name, Record) = readNameFromStrtab(Record); 3940*0b57cec5SDimitry Andric 3941*0b57cec5SDimitry Andric if (Record.size() < 8) 3942*0b57cec5SDimitry Andric return error("Invalid record"); 394381ad6265SDimitry Andric unsigned FTyID = Record[0]; 394481ad6265SDimitry Andric Type *FTy = getTypeByID(FTyID); 3945*0b57cec5SDimitry Andric if (!FTy) 3946*0b57cec5SDimitry Andric return error("Invalid record"); 394781ad6265SDimitry Andric if (isa<PointerType>(FTy)) { 394881ad6265SDimitry Andric FTyID = getContainedTypeID(FTyID, 0); 394981ad6265SDimitry Andric FTy = getTypeByID(FTyID); 395081ad6265SDimitry Andric if (!FTy) 395181ad6265SDimitry Andric return error("Missing element type for old-style function"); 395281ad6265SDimitry Andric } 3953*0b57cec5SDimitry Andric 3954*0b57cec5SDimitry Andric if (!isa<FunctionType>(FTy)) 3955*0b57cec5SDimitry Andric return error("Invalid type for value"); 3956*0b57cec5SDimitry Andric auto CC = static_cast<CallingConv::ID>(Record[1]); 3957*0b57cec5SDimitry Andric if (CC & ~CallingConv::MaxID) 3958*0b57cec5SDimitry Andric return error("Invalid calling convention ID"); 3959*0b57cec5SDimitry Andric 3960*0b57cec5SDimitry Andric unsigned AddrSpace = TheModule->getDataLayout().getProgramAddressSpace(); 3961*0b57cec5SDimitry Andric if (Record.size() > 16) 3962*0b57cec5SDimitry Andric AddrSpace = Record[16]; 3963*0b57cec5SDimitry Andric 3964*0b57cec5SDimitry Andric Function *Func = 3965*0b57cec5SDimitry Andric Function::Create(cast<FunctionType>(FTy), GlobalValue::ExternalLinkage, 3966*0b57cec5SDimitry Andric AddrSpace, Name, TheModule); 3967*0b57cec5SDimitry Andric 3968fe6060f1SDimitry Andric assert(Func->getFunctionType() == FTy && 3969*0b57cec5SDimitry Andric "Incorrect fully specified type provided for function"); 397081ad6265SDimitry Andric FunctionTypeIDs[Func] = FTyID; 3971*0b57cec5SDimitry Andric 3972*0b57cec5SDimitry Andric Func->setCallingConv(CC); 3973*0b57cec5SDimitry Andric bool isProto = Record[2]; 3974*0b57cec5SDimitry Andric uint64_t RawLinkage = Record[3]; 3975*0b57cec5SDimitry Andric Func->setLinkage(getDecodedLinkage(RawLinkage)); 3976*0b57cec5SDimitry Andric Func->setAttributes(getAttributes(Record[4])); 3977bdd1243dSDimitry Andric callValueTypeCallback(Func, FTyID); 3978*0b57cec5SDimitry Andric 3979e8d8bef9SDimitry Andric // Upgrade any old-style byval or sret without a type by propagating the 3980e8d8bef9SDimitry Andric // argument's pointee type. There should be no opaque pointers where the byval 3981e8d8bef9SDimitry Andric // type is implicit. 3982*0b57cec5SDimitry Andric for (unsigned i = 0; i != Func->arg_size(); ++i) { 3983fe6060f1SDimitry Andric for (Attribute::AttrKind Kind : {Attribute::ByVal, Attribute::StructRet, 3984fe6060f1SDimitry Andric Attribute::InAlloca}) { 3985e8d8bef9SDimitry Andric if (!Func->hasParamAttribute(i, Kind)) 3986*0b57cec5SDimitry Andric continue; 3987*0b57cec5SDimitry Andric 3988fe6060f1SDimitry Andric if (Func->getParamAttribute(i, Kind).getValueAsType()) 3989fe6060f1SDimitry Andric continue; 3990fe6060f1SDimitry Andric 3991e8d8bef9SDimitry Andric Func->removeParamAttr(i, Kind); 3992e8d8bef9SDimitry Andric 399381ad6265SDimitry Andric unsigned ParamTypeID = getContainedTypeID(FTyID, i + 1); 399481ad6265SDimitry Andric Type *PtrEltTy = getPtrElementTypeByID(ParamTypeID); 399581ad6265SDimitry Andric if (!PtrEltTy) 399681ad6265SDimitry Andric return error("Missing param element type for attribute upgrade"); 399781ad6265SDimitry Andric 3998fe6060f1SDimitry Andric Attribute NewAttr; 3999fe6060f1SDimitry Andric switch (Kind) { 4000fe6060f1SDimitry Andric case Attribute::ByVal: 4001fe6060f1SDimitry Andric NewAttr = Attribute::getWithByValType(Context, PtrEltTy); 4002fe6060f1SDimitry Andric break; 4003fe6060f1SDimitry Andric case Attribute::StructRet: 4004fe6060f1SDimitry Andric NewAttr = Attribute::getWithStructRetType(Context, PtrEltTy); 4005fe6060f1SDimitry Andric break; 4006fe6060f1SDimitry Andric case Attribute::InAlloca: 4007fe6060f1SDimitry Andric NewAttr = Attribute::getWithInAllocaType(Context, PtrEltTy); 4008fe6060f1SDimitry Andric break; 4009fe6060f1SDimitry Andric default: 4010fe6060f1SDimitry Andric llvm_unreachable("not an upgraded type attribute"); 4011fe6060f1SDimitry Andric } 4012fe6060f1SDimitry Andric 4013e8d8bef9SDimitry Andric Func->addParamAttr(i, NewAttr); 4014e8d8bef9SDimitry Andric } 4015*0b57cec5SDimitry Andric } 4016*0b57cec5SDimitry Andric 401781ad6265SDimitry Andric if (Func->getCallingConv() == CallingConv::X86_INTR && 401881ad6265SDimitry Andric !Func->arg_empty() && !Func->hasParamAttribute(0, Attribute::ByVal)) { 401981ad6265SDimitry Andric unsigned ParamTypeID = getContainedTypeID(FTyID, 1); 402081ad6265SDimitry Andric Type *ByValTy = getPtrElementTypeByID(ParamTypeID); 402181ad6265SDimitry Andric if (!ByValTy) 402281ad6265SDimitry Andric return error("Missing param element type for x86_intrcc upgrade"); 402381ad6265SDimitry Andric Attribute NewAttr = Attribute::getWithByValType(Context, ByValTy); 402481ad6265SDimitry Andric Func->addParamAttr(0, NewAttr); 402581ad6265SDimitry Andric } 402681ad6265SDimitry Andric 40278bcb0991SDimitry Andric MaybeAlign Alignment; 4028*0b57cec5SDimitry Andric if (Error Err = parseAlignmentValue(Record[5], Alignment)) 4029*0b57cec5SDimitry Andric return Err; 4030*0b57cec5SDimitry Andric Func->setAlignment(Alignment); 4031*0b57cec5SDimitry Andric if (Record[6]) { 4032*0b57cec5SDimitry Andric if (Record[6] - 1 >= SectionTable.size()) 4033*0b57cec5SDimitry Andric return error("Invalid ID"); 4034*0b57cec5SDimitry Andric Func->setSection(SectionTable[Record[6] - 1]); 4035*0b57cec5SDimitry Andric } 4036*0b57cec5SDimitry Andric // Local linkage must have default visibility. 40375ffd83dbSDimitry Andric // auto-upgrade `hidden` and `protected` for old bitcode. 4038*0b57cec5SDimitry Andric if (!Func->hasLocalLinkage()) 4039*0b57cec5SDimitry Andric Func->setVisibility(getDecodedVisibility(Record[7])); 4040*0b57cec5SDimitry Andric if (Record.size() > 8 && Record[8]) { 4041*0b57cec5SDimitry Andric if (Record[8] - 1 >= GCTable.size()) 4042*0b57cec5SDimitry Andric return error("Invalid ID"); 4043*0b57cec5SDimitry Andric Func->setGC(GCTable[Record[8] - 1]); 4044*0b57cec5SDimitry Andric } 4045*0b57cec5SDimitry Andric GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None; 4046*0b57cec5SDimitry Andric if (Record.size() > 9) 4047*0b57cec5SDimitry Andric UnnamedAddr = getDecodedUnnamedAddrType(Record[9]); 4048*0b57cec5SDimitry Andric Func->setUnnamedAddr(UnnamedAddr); 4049349cc55cSDimitry Andric 4050349cc55cSDimitry Andric FunctionOperandInfo OperandInfo = {Func, 0, 0, 0}; 4051349cc55cSDimitry Andric if (Record.size() > 10) 4052349cc55cSDimitry Andric OperandInfo.Prologue = Record[10]; 4053*0b57cec5SDimitry Andric 4054bdd1243dSDimitry Andric if (Record.size() > 11) { 4055bdd1243dSDimitry Andric // A GlobalValue with local linkage cannot have a DLL storage class. 4056bdd1243dSDimitry Andric if (!Func->hasLocalLinkage()) { 4057*0b57cec5SDimitry Andric Func->setDLLStorageClass(getDecodedDLLStorageClass(Record[11])); 4058bdd1243dSDimitry Andric } 4059bdd1243dSDimitry Andric } else { 4060*0b57cec5SDimitry Andric upgradeDLLImportExportLinkage(Func, RawLinkage); 4061bdd1243dSDimitry Andric } 4062*0b57cec5SDimitry Andric 4063*0b57cec5SDimitry Andric if (Record.size() > 12) { 4064*0b57cec5SDimitry Andric if (unsigned ComdatID = Record[12]) { 4065*0b57cec5SDimitry Andric if (ComdatID > ComdatList.size()) 4066*0b57cec5SDimitry Andric return error("Invalid function comdat ID"); 4067*0b57cec5SDimitry Andric Func->setComdat(ComdatList[ComdatID - 1]); 4068*0b57cec5SDimitry Andric } 4069*0b57cec5SDimitry Andric } else if (hasImplicitComdat(RawLinkage)) { 40700eae32dcSDimitry Andric ImplicitComdatObjects.insert(Func); 4071*0b57cec5SDimitry Andric } 4072*0b57cec5SDimitry Andric 4073349cc55cSDimitry Andric if (Record.size() > 13) 4074349cc55cSDimitry Andric OperandInfo.Prefix = Record[13]; 4075*0b57cec5SDimitry Andric 4076349cc55cSDimitry Andric if (Record.size() > 14) 4077349cc55cSDimitry Andric OperandInfo.PersonalityFn = Record[14]; 4078*0b57cec5SDimitry Andric 4079*0b57cec5SDimitry Andric if (Record.size() > 15) { 4080*0b57cec5SDimitry Andric Func->setDSOLocal(getDecodedDSOLocal(Record[15])); 4081*0b57cec5SDimitry Andric } 4082*0b57cec5SDimitry Andric inferDSOLocal(Func); 4083*0b57cec5SDimitry Andric 4084*0b57cec5SDimitry Andric // Record[16] is the address space number. 4085*0b57cec5SDimitry Andric 4086fe6060f1SDimitry Andric // Check whether we have enough values to read a partition name. Also make 4087fe6060f1SDimitry Andric // sure Strtab has enough values. 4088fe6060f1SDimitry Andric if (Record.size() > 18 && Strtab.data() && 4089fe6060f1SDimitry Andric Record[17] + Record[18] <= Strtab.size()) { 4090*0b57cec5SDimitry Andric Func->setPartition(StringRef(Strtab.data() + Record[17], Record[18])); 4091fe6060f1SDimitry Andric } 4092*0b57cec5SDimitry Andric 409381ad6265SDimitry Andric ValueList.push_back(Func, getVirtualTypeID(Func->getType(), FTyID)); 4094*0b57cec5SDimitry Andric 4095349cc55cSDimitry Andric if (OperandInfo.PersonalityFn || OperandInfo.Prefix || OperandInfo.Prologue) 4096349cc55cSDimitry Andric FunctionOperands.push_back(OperandInfo); 4097349cc55cSDimitry Andric 4098*0b57cec5SDimitry Andric // If this is a function with a body, remember the prototype we are 4099*0b57cec5SDimitry Andric // creating now, so that we can match up the body with them later. 4100*0b57cec5SDimitry Andric if (!isProto) { 4101*0b57cec5SDimitry Andric Func->setIsMaterializable(true); 4102*0b57cec5SDimitry Andric FunctionsWithBodies.push_back(Func); 4103*0b57cec5SDimitry Andric DeferredFunctionInfo[Func] = 0; 4104*0b57cec5SDimitry Andric } 4105*0b57cec5SDimitry Andric return Error::success(); 4106*0b57cec5SDimitry Andric } 4107*0b57cec5SDimitry Andric 4108*0b57cec5SDimitry Andric Error BitcodeReader::parseGlobalIndirectSymbolRecord( 4109*0b57cec5SDimitry Andric unsigned BitCode, ArrayRef<uint64_t> Record) { 4110*0b57cec5SDimitry Andric // v1 ALIAS_OLD: [alias type, aliasee val#, linkage] (name in VST) 4111*0b57cec5SDimitry Andric // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility, 4112*0b57cec5SDimitry Andric // dllstorageclass, threadlocal, unnamed_addr, 4113*0b57cec5SDimitry Andric // preemption specifier] (name in VST) 4114*0b57cec5SDimitry Andric // v1 IFUNC: [alias type, addrspace, aliasee val#, linkage, 4115*0b57cec5SDimitry Andric // visibility, dllstorageclass, threadlocal, unnamed_addr, 4116*0b57cec5SDimitry Andric // preemption specifier] (name in VST) 4117*0b57cec5SDimitry Andric // v2: [strtab_offset, strtab_size, v1] 4118*0b57cec5SDimitry Andric StringRef Name; 4119*0b57cec5SDimitry Andric std::tie(Name, Record) = readNameFromStrtab(Record); 4120*0b57cec5SDimitry Andric 4121*0b57cec5SDimitry Andric bool NewRecord = BitCode != bitc::MODULE_CODE_ALIAS_OLD; 4122*0b57cec5SDimitry Andric if (Record.size() < (3 + (unsigned)NewRecord)) 4123*0b57cec5SDimitry Andric return error("Invalid record"); 4124*0b57cec5SDimitry Andric unsigned OpNum = 0; 412581ad6265SDimitry Andric unsigned TypeID = Record[OpNum++]; 412681ad6265SDimitry Andric Type *Ty = getTypeByID(TypeID); 4127*0b57cec5SDimitry Andric if (!Ty) 4128*0b57cec5SDimitry Andric return error("Invalid record"); 4129*0b57cec5SDimitry Andric 4130*0b57cec5SDimitry Andric unsigned AddrSpace; 4131*0b57cec5SDimitry Andric if (!NewRecord) { 4132*0b57cec5SDimitry Andric auto *PTy = dyn_cast<PointerType>(Ty); 4133*0b57cec5SDimitry Andric if (!PTy) 4134*0b57cec5SDimitry Andric return error("Invalid type for value"); 4135*0b57cec5SDimitry Andric AddrSpace = PTy->getAddressSpace(); 413681ad6265SDimitry Andric TypeID = getContainedTypeID(TypeID); 413781ad6265SDimitry Andric Ty = getTypeByID(TypeID); 413881ad6265SDimitry Andric if (!Ty) 413981ad6265SDimitry Andric return error("Missing element type for old-style indirect symbol"); 4140*0b57cec5SDimitry Andric } else { 4141*0b57cec5SDimitry Andric AddrSpace = Record[OpNum++]; 4142*0b57cec5SDimitry Andric } 4143*0b57cec5SDimitry Andric 4144*0b57cec5SDimitry Andric auto Val = Record[OpNum++]; 4145*0b57cec5SDimitry Andric auto Linkage = Record[OpNum++]; 4146349cc55cSDimitry Andric GlobalValue *NewGA; 4147*0b57cec5SDimitry Andric if (BitCode == bitc::MODULE_CODE_ALIAS || 4148*0b57cec5SDimitry Andric BitCode == bitc::MODULE_CODE_ALIAS_OLD) 4149*0b57cec5SDimitry Andric NewGA = GlobalAlias::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name, 4150*0b57cec5SDimitry Andric TheModule); 4151*0b57cec5SDimitry Andric else 4152*0b57cec5SDimitry Andric NewGA = GlobalIFunc::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name, 4153*0b57cec5SDimitry Andric nullptr, TheModule); 4154*0b57cec5SDimitry Andric 4155*0b57cec5SDimitry Andric // Local linkage must have default visibility. 41565ffd83dbSDimitry Andric // auto-upgrade `hidden` and `protected` for old bitcode. 4157*0b57cec5SDimitry Andric if (OpNum != Record.size()) { 4158*0b57cec5SDimitry Andric auto VisInd = OpNum++; 4159*0b57cec5SDimitry Andric if (!NewGA->hasLocalLinkage()) 4160*0b57cec5SDimitry Andric NewGA->setVisibility(getDecodedVisibility(Record[VisInd])); 4161*0b57cec5SDimitry Andric } 4162*0b57cec5SDimitry Andric if (BitCode == bitc::MODULE_CODE_ALIAS || 4163*0b57cec5SDimitry Andric BitCode == bitc::MODULE_CODE_ALIAS_OLD) { 4164bdd1243dSDimitry Andric if (OpNum != Record.size()) { 4165bdd1243dSDimitry Andric auto S = Record[OpNum++]; 4166bdd1243dSDimitry Andric // A GlobalValue with local linkage cannot have a DLL storage class. 4167bdd1243dSDimitry Andric if (!NewGA->hasLocalLinkage()) 4168bdd1243dSDimitry Andric NewGA->setDLLStorageClass(getDecodedDLLStorageClass(S)); 4169bdd1243dSDimitry Andric } 4170*0b57cec5SDimitry Andric else 4171*0b57cec5SDimitry Andric upgradeDLLImportExportLinkage(NewGA, Linkage); 4172*0b57cec5SDimitry Andric if (OpNum != Record.size()) 4173*0b57cec5SDimitry Andric NewGA->setThreadLocalMode(getDecodedThreadLocalMode(Record[OpNum++])); 4174*0b57cec5SDimitry Andric if (OpNum != Record.size()) 4175*0b57cec5SDimitry Andric NewGA->setUnnamedAddr(getDecodedUnnamedAddrType(Record[OpNum++])); 4176*0b57cec5SDimitry Andric } 4177*0b57cec5SDimitry Andric if (OpNum != Record.size()) 4178*0b57cec5SDimitry Andric NewGA->setDSOLocal(getDecodedDSOLocal(Record[OpNum++])); 4179*0b57cec5SDimitry Andric inferDSOLocal(NewGA); 4180*0b57cec5SDimitry Andric 4181*0b57cec5SDimitry Andric // Check whether we have enough values to read a partition name. 4182*0b57cec5SDimitry Andric if (OpNum + 1 < Record.size()) { 4183*0b57cec5SDimitry Andric NewGA->setPartition( 4184*0b57cec5SDimitry Andric StringRef(Strtab.data() + Record[OpNum], Record[OpNum + 1])); 4185*0b57cec5SDimitry Andric OpNum += 2; 4186*0b57cec5SDimitry Andric } 4187*0b57cec5SDimitry Andric 418881ad6265SDimitry Andric ValueList.push_back(NewGA, getVirtualTypeID(NewGA->getType(), TypeID)); 4189*0b57cec5SDimitry Andric IndirectSymbolInits.push_back(std::make_pair(NewGA, Val)); 4190*0b57cec5SDimitry Andric return Error::success(); 4191*0b57cec5SDimitry Andric } 4192*0b57cec5SDimitry Andric 4193*0b57cec5SDimitry Andric Error BitcodeReader::parseModule(uint64_t ResumeBit, 41945ffd83dbSDimitry Andric bool ShouldLazyLoadMetadata, 4195bdd1243dSDimitry Andric ParserCallbacks Callbacks) { 4196bdd1243dSDimitry Andric this->ValueTypeCallback = std::move(Callbacks.ValueType); 4197*0b57cec5SDimitry Andric if (ResumeBit) { 4198*0b57cec5SDimitry Andric if (Error JumpFailed = Stream.JumpToBit(ResumeBit)) 4199*0b57cec5SDimitry Andric return JumpFailed; 4200*0b57cec5SDimitry Andric } else if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 4201*0b57cec5SDimitry Andric return Err; 4202*0b57cec5SDimitry Andric 4203*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 4204*0b57cec5SDimitry Andric 42055ffd83dbSDimitry Andric // Parts of bitcode parsing depend on the datalayout. Make sure we 42065ffd83dbSDimitry Andric // finalize the datalayout before we run any of that code. 42075ffd83dbSDimitry Andric bool ResolvedDataLayout = false; 4208bdd1243dSDimitry Andric // In order to support importing modules with illegal data layout strings, 4209bdd1243dSDimitry Andric // delay parsing the data layout string until after upgrades and overrides 4210bdd1243dSDimitry Andric // have been applied, allowing to fix illegal data layout strings. 4211bdd1243dSDimitry Andric // Initialize to the current module's layout string in case none is specified. 4212bdd1243dSDimitry Andric std::string TentativeDataLayoutStr = TheModule->getDataLayoutStr(); 42135ffd83dbSDimitry Andric 4214bdd1243dSDimitry Andric auto ResolveDataLayout = [&]() -> Error { 4215bdd1243dSDimitry Andric if (ResolvedDataLayout) 4216bdd1243dSDimitry Andric return Error::success(); 4217bdd1243dSDimitry Andric 4218bdd1243dSDimitry Andric // Datalayout and triple can't be parsed after this point. 42195ffd83dbSDimitry Andric ResolvedDataLayout = true; 42205ffd83dbSDimitry Andric 4221bdd1243dSDimitry Andric // Auto-upgrade the layout string 4222bdd1243dSDimitry Andric TentativeDataLayoutStr = llvm::UpgradeDataLayoutString( 4223bdd1243dSDimitry Andric TentativeDataLayoutStr, TheModule->getTargetTriple()); 42245ffd83dbSDimitry Andric 4225bdd1243dSDimitry Andric // Apply override 4226bdd1243dSDimitry Andric if (Callbacks.DataLayout) { 4227bdd1243dSDimitry Andric if (auto LayoutOverride = (*Callbacks.DataLayout)( 4228bdd1243dSDimitry Andric TheModule->getTargetTriple(), TentativeDataLayoutStr)) 4229bdd1243dSDimitry Andric TentativeDataLayoutStr = *LayoutOverride; 4230bdd1243dSDimitry Andric } 4231bdd1243dSDimitry Andric 4232bdd1243dSDimitry Andric // Now the layout string is finalized in TentativeDataLayoutStr. Parse it. 4233bdd1243dSDimitry Andric Expected<DataLayout> MaybeDL = DataLayout::parse(TentativeDataLayoutStr); 4234bdd1243dSDimitry Andric if (!MaybeDL) 4235bdd1243dSDimitry Andric return MaybeDL.takeError(); 4236bdd1243dSDimitry Andric 4237bdd1243dSDimitry Andric TheModule->setDataLayout(MaybeDL.get()); 4238bdd1243dSDimitry Andric return Error::success(); 42395ffd83dbSDimitry Andric }; 42405ffd83dbSDimitry Andric 4241*0b57cec5SDimitry Andric // Read all the records for this module. 4242*0b57cec5SDimitry Andric while (true) { 4243*0b57cec5SDimitry Andric Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4244*0b57cec5SDimitry Andric if (!MaybeEntry) 4245*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 4246*0b57cec5SDimitry Andric llvm::BitstreamEntry Entry = MaybeEntry.get(); 4247*0b57cec5SDimitry Andric 4248*0b57cec5SDimitry Andric switch (Entry.Kind) { 4249*0b57cec5SDimitry Andric case BitstreamEntry::Error: 4250*0b57cec5SDimitry Andric return error("Malformed block"); 4251*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 4252bdd1243dSDimitry Andric if (Error Err = ResolveDataLayout()) 4253bdd1243dSDimitry Andric return Err; 4254*0b57cec5SDimitry Andric return globalCleanup(); 4255*0b57cec5SDimitry Andric 4256*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: 4257*0b57cec5SDimitry Andric switch (Entry.ID) { 4258*0b57cec5SDimitry Andric default: // Skip unknown content. 4259*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 4260*0b57cec5SDimitry Andric return Err; 4261*0b57cec5SDimitry Andric break; 4262*0b57cec5SDimitry Andric case bitc::BLOCKINFO_BLOCK_ID: 426381ad6265SDimitry Andric if (Error Err = readBlockInfo()) 426481ad6265SDimitry Andric return Err; 4265*0b57cec5SDimitry Andric break; 4266*0b57cec5SDimitry Andric case bitc::PARAMATTR_BLOCK_ID: 4267*0b57cec5SDimitry Andric if (Error Err = parseAttributeBlock()) 4268*0b57cec5SDimitry Andric return Err; 4269*0b57cec5SDimitry Andric break; 4270*0b57cec5SDimitry Andric case bitc::PARAMATTR_GROUP_BLOCK_ID: 4271*0b57cec5SDimitry Andric if (Error Err = parseAttributeGroupBlock()) 4272*0b57cec5SDimitry Andric return Err; 4273*0b57cec5SDimitry Andric break; 4274*0b57cec5SDimitry Andric case bitc::TYPE_BLOCK_ID_NEW: 4275*0b57cec5SDimitry Andric if (Error Err = parseTypeTable()) 4276*0b57cec5SDimitry Andric return Err; 4277*0b57cec5SDimitry Andric break; 4278*0b57cec5SDimitry Andric case bitc::VALUE_SYMTAB_BLOCK_ID: 4279*0b57cec5SDimitry Andric if (!SeenValueSymbolTable) { 4280*0b57cec5SDimitry Andric // Either this is an old form VST without function index and an 4281*0b57cec5SDimitry Andric // associated VST forward declaration record (which would have caused 4282*0b57cec5SDimitry Andric // the VST to be jumped to and parsed before it was encountered 4283*0b57cec5SDimitry Andric // normally in the stream), or there were no function blocks to 4284*0b57cec5SDimitry Andric // trigger an earlier parsing of the VST. 4285*0b57cec5SDimitry Andric assert(VSTOffset == 0 || FunctionsWithBodies.empty()); 4286*0b57cec5SDimitry Andric if (Error Err = parseValueSymbolTable()) 4287*0b57cec5SDimitry Andric return Err; 4288*0b57cec5SDimitry Andric SeenValueSymbolTable = true; 4289*0b57cec5SDimitry Andric } else { 4290*0b57cec5SDimitry Andric // We must have had a VST forward declaration record, which caused 4291*0b57cec5SDimitry Andric // the parser to jump to and parse the VST earlier. 4292*0b57cec5SDimitry Andric assert(VSTOffset > 0); 4293*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 4294*0b57cec5SDimitry Andric return Err; 4295*0b57cec5SDimitry Andric } 4296*0b57cec5SDimitry Andric break; 4297*0b57cec5SDimitry Andric case bitc::CONSTANTS_BLOCK_ID: 4298*0b57cec5SDimitry Andric if (Error Err = parseConstants()) 4299*0b57cec5SDimitry Andric return Err; 4300*0b57cec5SDimitry Andric if (Error Err = resolveGlobalAndIndirectSymbolInits()) 4301*0b57cec5SDimitry Andric return Err; 4302*0b57cec5SDimitry Andric break; 4303*0b57cec5SDimitry Andric case bitc::METADATA_BLOCK_ID: 4304*0b57cec5SDimitry Andric if (ShouldLazyLoadMetadata) { 4305*0b57cec5SDimitry Andric if (Error Err = rememberAndSkipMetadata()) 4306*0b57cec5SDimitry Andric return Err; 4307*0b57cec5SDimitry Andric break; 4308*0b57cec5SDimitry Andric } 4309*0b57cec5SDimitry Andric assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata"); 4310*0b57cec5SDimitry Andric if (Error Err = MDLoader->parseModuleMetadata()) 4311*0b57cec5SDimitry Andric return Err; 4312*0b57cec5SDimitry Andric break; 4313*0b57cec5SDimitry Andric case bitc::METADATA_KIND_BLOCK_ID: 4314*0b57cec5SDimitry Andric if (Error Err = MDLoader->parseMetadataKinds()) 4315*0b57cec5SDimitry Andric return Err; 4316*0b57cec5SDimitry Andric break; 4317*0b57cec5SDimitry Andric case bitc::FUNCTION_BLOCK_ID: 4318bdd1243dSDimitry Andric if (Error Err = ResolveDataLayout()) 4319bdd1243dSDimitry Andric return Err; 43205ffd83dbSDimitry Andric 4321*0b57cec5SDimitry Andric // If this is the first function body we've seen, reverse the 4322*0b57cec5SDimitry Andric // FunctionsWithBodies list. 4323*0b57cec5SDimitry Andric if (!SeenFirstFunctionBody) { 4324*0b57cec5SDimitry Andric std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end()); 4325*0b57cec5SDimitry Andric if (Error Err = globalCleanup()) 4326*0b57cec5SDimitry Andric return Err; 4327*0b57cec5SDimitry Andric SeenFirstFunctionBody = true; 4328*0b57cec5SDimitry Andric } 4329*0b57cec5SDimitry Andric 4330*0b57cec5SDimitry Andric if (VSTOffset > 0) { 4331*0b57cec5SDimitry Andric // If we have a VST forward declaration record, make sure we 4332*0b57cec5SDimitry Andric // parse the VST now if we haven't already. It is needed to 4333*0b57cec5SDimitry Andric // set up the DeferredFunctionInfo vector for lazy reading. 4334*0b57cec5SDimitry Andric if (!SeenValueSymbolTable) { 4335*0b57cec5SDimitry Andric if (Error Err = BitcodeReader::parseValueSymbolTable(VSTOffset)) 4336*0b57cec5SDimitry Andric return Err; 4337*0b57cec5SDimitry Andric SeenValueSymbolTable = true; 4338*0b57cec5SDimitry Andric // Fall through so that we record the NextUnreadBit below. 4339*0b57cec5SDimitry Andric // This is necessary in case we have an anonymous function that 4340*0b57cec5SDimitry Andric // is later materialized. Since it will not have a VST entry we 4341*0b57cec5SDimitry Andric // need to fall back to the lazy parse to find its offset. 4342*0b57cec5SDimitry Andric } else { 4343*0b57cec5SDimitry Andric // If we have a VST forward declaration record, but have already 4344*0b57cec5SDimitry Andric // parsed the VST (just above, when the first function body was 4345*0b57cec5SDimitry Andric // encountered here), then we are resuming the parse after 4346*0b57cec5SDimitry Andric // materializing functions. The ResumeBit points to the 4347*0b57cec5SDimitry Andric // start of the last function block recorded in the 4348*0b57cec5SDimitry Andric // DeferredFunctionInfo map. Skip it. 4349*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 4350*0b57cec5SDimitry Andric return Err; 4351*0b57cec5SDimitry Andric continue; 4352*0b57cec5SDimitry Andric } 4353*0b57cec5SDimitry Andric } 4354*0b57cec5SDimitry Andric 4355*0b57cec5SDimitry Andric // Support older bitcode files that did not have the function 4356*0b57cec5SDimitry Andric // index in the VST, nor a VST forward declaration record, as 4357*0b57cec5SDimitry Andric // well as anonymous functions that do not have VST entries. 4358*0b57cec5SDimitry Andric // Build the DeferredFunctionInfo vector on the fly. 4359*0b57cec5SDimitry Andric if (Error Err = rememberAndSkipFunctionBody()) 4360*0b57cec5SDimitry Andric return Err; 4361*0b57cec5SDimitry Andric 4362*0b57cec5SDimitry Andric // Suspend parsing when we reach the function bodies. Subsequent 4363*0b57cec5SDimitry Andric // materialization calls will resume it when necessary. If the bitcode 4364*0b57cec5SDimitry Andric // file is old, the symbol table will be at the end instead and will not 4365*0b57cec5SDimitry Andric // have been seen yet. In this case, just finish the parse now. 4366*0b57cec5SDimitry Andric if (SeenValueSymbolTable) { 4367*0b57cec5SDimitry Andric NextUnreadBit = Stream.GetCurrentBitNo(); 4368*0b57cec5SDimitry Andric // After the VST has been parsed, we need to make sure intrinsic name 4369*0b57cec5SDimitry Andric // are auto-upgraded. 4370*0b57cec5SDimitry Andric return globalCleanup(); 4371*0b57cec5SDimitry Andric } 4372*0b57cec5SDimitry Andric break; 4373*0b57cec5SDimitry Andric case bitc::USELIST_BLOCK_ID: 4374*0b57cec5SDimitry Andric if (Error Err = parseUseLists()) 4375*0b57cec5SDimitry Andric return Err; 4376*0b57cec5SDimitry Andric break; 4377*0b57cec5SDimitry Andric case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID: 4378*0b57cec5SDimitry Andric if (Error Err = parseOperandBundleTags()) 4379*0b57cec5SDimitry Andric return Err; 4380*0b57cec5SDimitry Andric break; 4381*0b57cec5SDimitry Andric case bitc::SYNC_SCOPE_NAMES_BLOCK_ID: 4382*0b57cec5SDimitry Andric if (Error Err = parseSyncScopeNames()) 4383*0b57cec5SDimitry Andric return Err; 4384*0b57cec5SDimitry Andric break; 4385*0b57cec5SDimitry Andric } 4386*0b57cec5SDimitry Andric continue; 4387*0b57cec5SDimitry Andric 4388*0b57cec5SDimitry Andric case BitstreamEntry::Record: 4389*0b57cec5SDimitry Andric // The interesting case. 4390*0b57cec5SDimitry Andric break; 4391*0b57cec5SDimitry Andric } 4392*0b57cec5SDimitry Andric 4393*0b57cec5SDimitry Andric // Read a record. 4394*0b57cec5SDimitry Andric Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); 4395*0b57cec5SDimitry Andric if (!MaybeBitCode) 4396*0b57cec5SDimitry Andric return MaybeBitCode.takeError(); 4397*0b57cec5SDimitry Andric switch (unsigned BitCode = MaybeBitCode.get()) { 4398*0b57cec5SDimitry Andric default: break; // Default behavior, ignore unknown content. 4399*0b57cec5SDimitry Andric case bitc::MODULE_CODE_VERSION: { 4400*0b57cec5SDimitry Andric Expected<unsigned> VersionOrErr = parseVersionRecord(Record); 4401*0b57cec5SDimitry Andric if (!VersionOrErr) 4402*0b57cec5SDimitry Andric return VersionOrErr.takeError(); 4403*0b57cec5SDimitry Andric UseRelativeIDs = *VersionOrErr >= 1; 4404*0b57cec5SDimitry Andric break; 4405*0b57cec5SDimitry Andric } 4406*0b57cec5SDimitry Andric case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] 44075ffd83dbSDimitry Andric if (ResolvedDataLayout) 44085ffd83dbSDimitry Andric return error("target triple too late in module"); 4409*0b57cec5SDimitry Andric std::string S; 4410*0b57cec5SDimitry Andric if (convertToString(Record, 0, S)) 4411*0b57cec5SDimitry Andric return error("Invalid record"); 4412*0b57cec5SDimitry Andric TheModule->setTargetTriple(S); 4413*0b57cec5SDimitry Andric break; 4414*0b57cec5SDimitry Andric } 4415*0b57cec5SDimitry Andric case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N] 44165ffd83dbSDimitry Andric if (ResolvedDataLayout) 44175ffd83dbSDimitry Andric return error("datalayout too late in module"); 4418bdd1243dSDimitry Andric if (convertToString(Record, 0, TentativeDataLayoutStr)) 4419*0b57cec5SDimitry Andric return error("Invalid record"); 4420*0b57cec5SDimitry Andric break; 4421*0b57cec5SDimitry Andric } 4422*0b57cec5SDimitry Andric case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N] 4423*0b57cec5SDimitry Andric std::string S; 4424*0b57cec5SDimitry Andric if (convertToString(Record, 0, S)) 4425*0b57cec5SDimitry Andric return error("Invalid record"); 4426*0b57cec5SDimitry Andric TheModule->setModuleInlineAsm(S); 4427*0b57cec5SDimitry Andric break; 4428*0b57cec5SDimitry Andric } 4429*0b57cec5SDimitry Andric case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N] 44305ffd83dbSDimitry Andric // Deprecated, but still needed to read old bitcode files. 4431*0b57cec5SDimitry Andric std::string S; 4432*0b57cec5SDimitry Andric if (convertToString(Record, 0, S)) 4433*0b57cec5SDimitry Andric return error("Invalid record"); 4434*0b57cec5SDimitry Andric // Ignore value. 4435*0b57cec5SDimitry Andric break; 4436*0b57cec5SDimitry Andric } 4437*0b57cec5SDimitry Andric case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] 4438*0b57cec5SDimitry Andric std::string S; 4439*0b57cec5SDimitry Andric if (convertToString(Record, 0, S)) 4440*0b57cec5SDimitry Andric return error("Invalid record"); 4441*0b57cec5SDimitry Andric SectionTable.push_back(S); 4442*0b57cec5SDimitry Andric break; 4443*0b57cec5SDimitry Andric } 4444*0b57cec5SDimitry Andric case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N] 4445*0b57cec5SDimitry Andric std::string S; 4446*0b57cec5SDimitry Andric if (convertToString(Record, 0, S)) 4447*0b57cec5SDimitry Andric return error("Invalid record"); 4448*0b57cec5SDimitry Andric GCTable.push_back(S); 4449*0b57cec5SDimitry Andric break; 4450*0b57cec5SDimitry Andric } 4451*0b57cec5SDimitry Andric case bitc::MODULE_CODE_COMDAT: 4452*0b57cec5SDimitry Andric if (Error Err = parseComdatRecord(Record)) 4453*0b57cec5SDimitry Andric return Err; 4454*0b57cec5SDimitry Andric break; 445504eeddc0SDimitry Andric // FIXME: BitcodeReader should handle {GLOBALVAR, FUNCTION, ALIAS, IFUNC} 445604eeddc0SDimitry Andric // written by ThinLinkBitcodeWriter. See 445704eeddc0SDimitry Andric // `ThinLinkBitcodeWriter::writeSimplifiedModuleInfo` for the format of each 445804eeddc0SDimitry Andric // record 445904eeddc0SDimitry Andric // (https://github.com/llvm/llvm-project/blob/b6a93967d9c11e79802b5e75cec1584d6c8aa472/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp#L4714) 4460*0b57cec5SDimitry Andric case bitc::MODULE_CODE_GLOBALVAR: 4461*0b57cec5SDimitry Andric if (Error Err = parseGlobalVarRecord(Record)) 4462*0b57cec5SDimitry Andric return Err; 4463*0b57cec5SDimitry Andric break; 4464*0b57cec5SDimitry Andric case bitc::MODULE_CODE_FUNCTION: 4465bdd1243dSDimitry Andric if (Error Err = ResolveDataLayout()) 4466bdd1243dSDimitry Andric return Err; 4467*0b57cec5SDimitry Andric if (Error Err = parseFunctionRecord(Record)) 4468*0b57cec5SDimitry Andric return Err; 4469*0b57cec5SDimitry Andric break; 4470*0b57cec5SDimitry Andric case bitc::MODULE_CODE_IFUNC: 4471*0b57cec5SDimitry Andric case bitc::MODULE_CODE_ALIAS: 4472*0b57cec5SDimitry Andric case bitc::MODULE_CODE_ALIAS_OLD: 4473*0b57cec5SDimitry Andric if (Error Err = parseGlobalIndirectSymbolRecord(BitCode, Record)) 4474*0b57cec5SDimitry Andric return Err; 4475*0b57cec5SDimitry Andric break; 4476*0b57cec5SDimitry Andric /// MODULE_CODE_VSTOFFSET: [offset] 4477*0b57cec5SDimitry Andric case bitc::MODULE_CODE_VSTOFFSET: 4478e8d8bef9SDimitry Andric if (Record.empty()) 4479*0b57cec5SDimitry Andric return error("Invalid record"); 4480*0b57cec5SDimitry Andric // Note that we subtract 1 here because the offset is relative to one word 4481*0b57cec5SDimitry Andric // before the start of the identification or module block, which was 4482*0b57cec5SDimitry Andric // historically always the start of the regular bitcode header. 4483*0b57cec5SDimitry Andric VSTOffset = Record[0] - 1; 4484*0b57cec5SDimitry Andric break; 4485*0b57cec5SDimitry Andric /// MODULE_CODE_SOURCE_FILENAME: [namechar x N] 4486*0b57cec5SDimitry Andric case bitc::MODULE_CODE_SOURCE_FILENAME: 4487*0b57cec5SDimitry Andric SmallString<128> ValueName; 4488*0b57cec5SDimitry Andric if (convertToString(Record, 0, ValueName)) 4489*0b57cec5SDimitry Andric return error("Invalid record"); 4490*0b57cec5SDimitry Andric TheModule->setSourceFileName(ValueName); 4491*0b57cec5SDimitry Andric break; 4492*0b57cec5SDimitry Andric } 4493*0b57cec5SDimitry Andric Record.clear(); 4494*0b57cec5SDimitry Andric } 4495bdd1243dSDimitry Andric this->ValueTypeCallback = std::nullopt; 4496bdd1243dSDimitry Andric return Error::success(); 4497*0b57cec5SDimitry Andric } 4498*0b57cec5SDimitry Andric 4499*0b57cec5SDimitry Andric Error BitcodeReader::parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata, 45005ffd83dbSDimitry Andric bool IsImporting, 4501bdd1243dSDimitry Andric ParserCallbacks Callbacks) { 4502*0b57cec5SDimitry Andric TheModule = M; 4503bdd1243dSDimitry Andric MetadataLoaderCallbacks MDCallbacks; 4504bdd1243dSDimitry Andric MDCallbacks.GetTypeByID = [&](unsigned ID) { return getTypeByID(ID); }; 4505bdd1243dSDimitry Andric MDCallbacks.GetContainedTypeID = [&](unsigned I, unsigned J) { 4506bdd1243dSDimitry Andric return getContainedTypeID(I, J); 4507bdd1243dSDimitry Andric }; 4508bdd1243dSDimitry Andric MDCallbacks.MDType = Callbacks.MDType; 4509bdd1243dSDimitry Andric MDLoader = MetadataLoader(Stream, *M, ValueList, IsImporting, MDCallbacks); 4510bdd1243dSDimitry Andric return parseModule(0, ShouldLazyLoadMetadata, Callbacks); 4511*0b57cec5SDimitry Andric } 4512*0b57cec5SDimitry Andric 4513*0b57cec5SDimitry Andric Error BitcodeReader::typeCheckLoadStoreInst(Type *ValType, Type *PtrType) { 4514*0b57cec5SDimitry Andric if (!isa<PointerType>(PtrType)) 4515*0b57cec5SDimitry Andric return error("Load/Store operand is not a pointer type"); 4516*0b57cec5SDimitry Andric 4517fe6060f1SDimitry Andric if (!cast<PointerType>(PtrType)->isOpaqueOrPointeeTypeMatches(ValType)) 4518*0b57cec5SDimitry Andric return error("Explicit load/store type does not match pointee " 4519*0b57cec5SDimitry Andric "type of pointer operand"); 4520fe6060f1SDimitry Andric if (!PointerType::isLoadableOrStorableType(ValType)) 4521*0b57cec5SDimitry Andric return error("Cannot load/store from pointer"); 4522*0b57cec5SDimitry Andric return Error::success(); 4523*0b57cec5SDimitry Andric } 4524*0b57cec5SDimitry Andric 452581ad6265SDimitry Andric Error BitcodeReader::propagateAttributeTypes(CallBase *CB, 452681ad6265SDimitry Andric ArrayRef<unsigned> ArgTyIDs) { 452781ad6265SDimitry Andric AttributeList Attrs = CB->getAttributes(); 4528*0b57cec5SDimitry Andric for (unsigned i = 0; i != CB->arg_size(); ++i) { 4529fe6060f1SDimitry Andric for (Attribute::AttrKind Kind : {Attribute::ByVal, Attribute::StructRet, 4530fe6060f1SDimitry Andric Attribute::InAlloca}) { 453181ad6265SDimitry Andric if (!Attrs.hasParamAttr(i, Kind) || 453281ad6265SDimitry Andric Attrs.getParamAttr(i, Kind).getValueAsType()) 4533*0b57cec5SDimitry Andric continue; 4534*0b57cec5SDimitry Andric 453581ad6265SDimitry Andric Type *PtrEltTy = getPtrElementTypeByID(ArgTyIDs[i]); 453681ad6265SDimitry Andric if (!PtrEltTy) 453781ad6265SDimitry Andric return error("Missing element type for typed attribute upgrade"); 4538e8d8bef9SDimitry Andric 4539fe6060f1SDimitry Andric Attribute NewAttr; 4540fe6060f1SDimitry Andric switch (Kind) { 4541fe6060f1SDimitry Andric case Attribute::ByVal: 4542fe6060f1SDimitry Andric NewAttr = Attribute::getWithByValType(Context, PtrEltTy); 4543fe6060f1SDimitry Andric break; 4544fe6060f1SDimitry Andric case Attribute::StructRet: 4545fe6060f1SDimitry Andric NewAttr = Attribute::getWithStructRetType(Context, PtrEltTy); 4546fe6060f1SDimitry Andric break; 4547fe6060f1SDimitry Andric case Attribute::InAlloca: 4548fe6060f1SDimitry Andric NewAttr = Attribute::getWithInAllocaType(Context, PtrEltTy); 4549fe6060f1SDimitry Andric break; 4550fe6060f1SDimitry Andric default: 4551fe6060f1SDimitry Andric llvm_unreachable("not an upgraded type attribute"); 4552fe6060f1SDimitry Andric } 4553fe6060f1SDimitry Andric 455481ad6265SDimitry Andric Attrs = Attrs.addParamAttribute(Context, i, NewAttr); 4555e8d8bef9SDimitry Andric } 4556*0b57cec5SDimitry Andric } 4557fe6060f1SDimitry Andric 455804eeddc0SDimitry Andric if (CB->isInlineAsm()) { 455904eeddc0SDimitry Andric const InlineAsm *IA = cast<InlineAsm>(CB->getCalledOperand()); 456004eeddc0SDimitry Andric unsigned ArgNo = 0; 456104eeddc0SDimitry Andric for (const InlineAsm::ConstraintInfo &CI : IA->ParseConstraints()) { 456204eeddc0SDimitry Andric if (!CI.hasArg()) 456304eeddc0SDimitry Andric continue; 456404eeddc0SDimitry Andric 456581ad6265SDimitry Andric if (CI.isIndirect && !Attrs.getParamElementType(ArgNo)) { 456681ad6265SDimitry Andric Type *ElemTy = getPtrElementTypeByID(ArgTyIDs[ArgNo]); 456781ad6265SDimitry Andric if (!ElemTy) 456881ad6265SDimitry Andric return error("Missing element type for inline asm upgrade"); 456981ad6265SDimitry Andric Attrs = Attrs.addParamAttribute( 457081ad6265SDimitry Andric Context, ArgNo, 457181ad6265SDimitry Andric Attribute::get(Context, Attribute::ElementType, ElemTy)); 457204eeddc0SDimitry Andric } 457304eeddc0SDimitry Andric 457404eeddc0SDimitry Andric ArgNo++; 457504eeddc0SDimitry Andric } 457604eeddc0SDimitry Andric } 457704eeddc0SDimitry Andric 4578fe6060f1SDimitry Andric switch (CB->getIntrinsicID()) { 4579fe6060f1SDimitry Andric case Intrinsic::preserve_array_access_index: 4580fe6060f1SDimitry Andric case Intrinsic::preserve_struct_access_index: 458181ad6265SDimitry Andric case Intrinsic::aarch64_ldaxr: 458281ad6265SDimitry Andric case Intrinsic::aarch64_ldxr: 458381ad6265SDimitry Andric case Intrinsic::aarch64_stlxr: 458481ad6265SDimitry Andric case Intrinsic::aarch64_stxr: 458581ad6265SDimitry Andric case Intrinsic::arm_ldaex: 458681ad6265SDimitry Andric case Intrinsic::arm_ldrex: 458781ad6265SDimitry Andric case Intrinsic::arm_stlex: 458881ad6265SDimitry Andric case Intrinsic::arm_strex: { 458981ad6265SDimitry Andric unsigned ArgNo; 459081ad6265SDimitry Andric switch (CB->getIntrinsicID()) { 459181ad6265SDimitry Andric case Intrinsic::aarch64_stlxr: 459281ad6265SDimitry Andric case Intrinsic::aarch64_stxr: 459381ad6265SDimitry Andric case Intrinsic::arm_stlex: 459481ad6265SDimitry Andric case Intrinsic::arm_strex: 459581ad6265SDimitry Andric ArgNo = 1; 459681ad6265SDimitry Andric break; 459781ad6265SDimitry Andric default: 459881ad6265SDimitry Andric ArgNo = 0; 459981ad6265SDimitry Andric break; 460081ad6265SDimitry Andric } 460181ad6265SDimitry Andric if (!Attrs.getParamElementType(ArgNo)) { 460281ad6265SDimitry Andric Type *ElTy = getPtrElementTypeByID(ArgTyIDs[ArgNo]); 460381ad6265SDimitry Andric if (!ElTy) 460481ad6265SDimitry Andric return error("Missing element type for elementtype upgrade"); 4605fe6060f1SDimitry Andric Attribute NewAttr = Attribute::get(Context, Attribute::ElementType, ElTy); 460681ad6265SDimitry Andric Attrs = Attrs.addParamAttribute(Context, ArgNo, NewAttr); 4607fe6060f1SDimitry Andric } 4608fe6060f1SDimitry Andric break; 460981ad6265SDimitry Andric } 4610fe6060f1SDimitry Andric default: 4611fe6060f1SDimitry Andric break; 4612fe6060f1SDimitry Andric } 461381ad6265SDimitry Andric 461481ad6265SDimitry Andric CB->setAttributes(Attrs); 461581ad6265SDimitry Andric return Error::success(); 4616*0b57cec5SDimitry Andric } 4617*0b57cec5SDimitry Andric 4618*0b57cec5SDimitry Andric /// Lazily parse the specified function body block. 4619*0b57cec5SDimitry Andric Error BitcodeReader::parseFunctionBody(Function *F) { 4620*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID)) 4621*0b57cec5SDimitry Andric return Err; 4622*0b57cec5SDimitry Andric 4623*0b57cec5SDimitry Andric // Unexpected unresolved metadata when parsing function. 4624*0b57cec5SDimitry Andric if (MDLoader->hasFwdRefs()) 4625*0b57cec5SDimitry Andric return error("Invalid function metadata: incoming forward references"); 4626*0b57cec5SDimitry Andric 4627*0b57cec5SDimitry Andric InstructionList.clear(); 4628*0b57cec5SDimitry Andric unsigned ModuleValueListSize = ValueList.size(); 4629*0b57cec5SDimitry Andric unsigned ModuleMDLoaderSize = MDLoader->size(); 4630*0b57cec5SDimitry Andric 4631*0b57cec5SDimitry Andric // Add all the function arguments to the value table. 4632*0b57cec5SDimitry Andric unsigned ArgNo = 0; 463381ad6265SDimitry Andric unsigned FTyID = FunctionTypeIDs[F]; 4634*0b57cec5SDimitry Andric for (Argument &I : F->args()) { 463581ad6265SDimitry Andric unsigned ArgTyID = getContainedTypeID(FTyID, ArgNo + 1); 463681ad6265SDimitry Andric assert(I.getType() == getTypeByID(ArgTyID) && 4637*0b57cec5SDimitry Andric "Incorrect fully specified type for Function Argument"); 463881ad6265SDimitry Andric ValueList.push_back(&I, ArgTyID); 463981ad6265SDimitry Andric ++ArgNo; 4640*0b57cec5SDimitry Andric } 4641*0b57cec5SDimitry Andric unsigned NextValueNo = ValueList.size(); 4642*0b57cec5SDimitry Andric BasicBlock *CurBB = nullptr; 4643*0b57cec5SDimitry Andric unsigned CurBBNo = 0; 464481ad6265SDimitry Andric // Block into which constant expressions from phi nodes are materialized. 464581ad6265SDimitry Andric BasicBlock *PhiConstExprBB = nullptr; 464681ad6265SDimitry Andric // Edge blocks for phi nodes into which constant expressions have been 464781ad6265SDimitry Andric // expanded. 464881ad6265SDimitry Andric SmallMapVector<std::pair<BasicBlock *, BasicBlock *>, BasicBlock *, 4> 464981ad6265SDimitry Andric ConstExprEdgeBBs; 4650*0b57cec5SDimitry Andric 4651*0b57cec5SDimitry Andric DebugLoc LastLoc; 4652*0b57cec5SDimitry Andric auto getLastInstruction = [&]() -> Instruction * { 4653*0b57cec5SDimitry Andric if (CurBB && !CurBB->empty()) 4654*0b57cec5SDimitry Andric return &CurBB->back(); 4655*0b57cec5SDimitry Andric else if (CurBBNo && FunctionBBs[CurBBNo - 1] && 4656*0b57cec5SDimitry Andric !FunctionBBs[CurBBNo - 1]->empty()) 4657*0b57cec5SDimitry Andric return &FunctionBBs[CurBBNo - 1]->back(); 4658*0b57cec5SDimitry Andric return nullptr; 4659*0b57cec5SDimitry Andric }; 4660*0b57cec5SDimitry Andric 4661*0b57cec5SDimitry Andric std::vector<OperandBundleDef> OperandBundles; 4662*0b57cec5SDimitry Andric 4663*0b57cec5SDimitry Andric // Read all the records. 4664*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 4665*0b57cec5SDimitry Andric 4666*0b57cec5SDimitry Andric while (true) { 4667*0b57cec5SDimitry Andric Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4668*0b57cec5SDimitry Andric if (!MaybeEntry) 4669*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 4670*0b57cec5SDimitry Andric llvm::BitstreamEntry Entry = MaybeEntry.get(); 4671*0b57cec5SDimitry Andric 4672*0b57cec5SDimitry Andric switch (Entry.Kind) { 4673*0b57cec5SDimitry Andric case BitstreamEntry::Error: 4674*0b57cec5SDimitry Andric return error("Malformed block"); 4675*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 4676*0b57cec5SDimitry Andric goto OutOfRecordLoop; 4677*0b57cec5SDimitry Andric 4678*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: 4679*0b57cec5SDimitry Andric switch (Entry.ID) { 4680*0b57cec5SDimitry Andric default: // Skip unknown content. 4681*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 4682*0b57cec5SDimitry Andric return Err; 4683*0b57cec5SDimitry Andric break; 4684*0b57cec5SDimitry Andric case bitc::CONSTANTS_BLOCK_ID: 4685*0b57cec5SDimitry Andric if (Error Err = parseConstants()) 4686*0b57cec5SDimitry Andric return Err; 4687*0b57cec5SDimitry Andric NextValueNo = ValueList.size(); 4688*0b57cec5SDimitry Andric break; 4689*0b57cec5SDimitry Andric case bitc::VALUE_SYMTAB_BLOCK_ID: 4690*0b57cec5SDimitry Andric if (Error Err = parseValueSymbolTable()) 4691*0b57cec5SDimitry Andric return Err; 4692*0b57cec5SDimitry Andric break; 4693*0b57cec5SDimitry Andric case bitc::METADATA_ATTACHMENT_ID: 4694*0b57cec5SDimitry Andric if (Error Err = MDLoader->parseMetadataAttachment(*F, InstructionList)) 4695*0b57cec5SDimitry Andric return Err; 4696*0b57cec5SDimitry Andric break; 4697*0b57cec5SDimitry Andric case bitc::METADATA_BLOCK_ID: 4698*0b57cec5SDimitry Andric assert(DeferredMetadataInfo.empty() && 4699*0b57cec5SDimitry Andric "Must read all module-level metadata before function-level"); 4700*0b57cec5SDimitry Andric if (Error Err = MDLoader->parseFunctionMetadata()) 4701*0b57cec5SDimitry Andric return Err; 4702*0b57cec5SDimitry Andric break; 4703*0b57cec5SDimitry Andric case bitc::USELIST_BLOCK_ID: 4704*0b57cec5SDimitry Andric if (Error Err = parseUseLists()) 4705*0b57cec5SDimitry Andric return Err; 4706*0b57cec5SDimitry Andric break; 4707*0b57cec5SDimitry Andric } 4708*0b57cec5SDimitry Andric continue; 4709*0b57cec5SDimitry Andric 4710*0b57cec5SDimitry Andric case BitstreamEntry::Record: 4711*0b57cec5SDimitry Andric // The interesting case. 4712*0b57cec5SDimitry Andric break; 4713*0b57cec5SDimitry Andric } 4714*0b57cec5SDimitry Andric 4715*0b57cec5SDimitry Andric // Read a record. 4716*0b57cec5SDimitry Andric Record.clear(); 4717*0b57cec5SDimitry Andric Instruction *I = nullptr; 471881ad6265SDimitry Andric unsigned ResTypeID = InvalidTypeID; 4719*0b57cec5SDimitry Andric Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); 4720*0b57cec5SDimitry Andric if (!MaybeBitCode) 4721*0b57cec5SDimitry Andric return MaybeBitCode.takeError(); 4722*0b57cec5SDimitry Andric switch (unsigned BitCode = MaybeBitCode.get()) { 4723*0b57cec5SDimitry Andric default: // Default behavior: reject 4724*0b57cec5SDimitry Andric return error("Invalid value"); 4725*0b57cec5SDimitry Andric case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks] 4726e8d8bef9SDimitry Andric if (Record.empty() || Record[0] == 0) 4727*0b57cec5SDimitry Andric return error("Invalid record"); 4728*0b57cec5SDimitry Andric // Create all the basic blocks for the function. 4729*0b57cec5SDimitry Andric FunctionBBs.resize(Record[0]); 4730*0b57cec5SDimitry Andric 4731*0b57cec5SDimitry Andric // See if anything took the address of blocks in this function. 4732*0b57cec5SDimitry Andric auto BBFRI = BasicBlockFwdRefs.find(F); 4733*0b57cec5SDimitry Andric if (BBFRI == BasicBlockFwdRefs.end()) { 47344824e7fdSDimitry Andric for (BasicBlock *&BB : FunctionBBs) 47354824e7fdSDimitry Andric BB = BasicBlock::Create(Context, "", F); 4736*0b57cec5SDimitry Andric } else { 4737*0b57cec5SDimitry Andric auto &BBRefs = BBFRI->second; 4738*0b57cec5SDimitry Andric // Check for invalid basic block references. 4739*0b57cec5SDimitry Andric if (BBRefs.size() > FunctionBBs.size()) 4740*0b57cec5SDimitry Andric return error("Invalid ID"); 4741*0b57cec5SDimitry Andric assert(!BBRefs.empty() && "Unexpected empty array"); 4742*0b57cec5SDimitry Andric assert(!BBRefs.front() && "Invalid reference to entry block"); 4743*0b57cec5SDimitry Andric for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E; 4744*0b57cec5SDimitry Andric ++I) 4745*0b57cec5SDimitry Andric if (I < RE && BBRefs[I]) { 4746*0b57cec5SDimitry Andric BBRefs[I]->insertInto(F); 4747*0b57cec5SDimitry Andric FunctionBBs[I] = BBRefs[I]; 4748*0b57cec5SDimitry Andric } else { 4749*0b57cec5SDimitry Andric FunctionBBs[I] = BasicBlock::Create(Context, "", F); 4750*0b57cec5SDimitry Andric } 4751*0b57cec5SDimitry Andric 4752*0b57cec5SDimitry Andric // Erase from the table. 4753*0b57cec5SDimitry Andric BasicBlockFwdRefs.erase(BBFRI); 4754*0b57cec5SDimitry Andric } 4755*0b57cec5SDimitry Andric 4756*0b57cec5SDimitry Andric CurBB = FunctionBBs[0]; 4757*0b57cec5SDimitry Andric continue; 4758*0b57cec5SDimitry Andric } 4759*0b57cec5SDimitry Andric 476081ad6265SDimitry Andric case bitc::FUNC_CODE_BLOCKADDR_USERS: // BLOCKADDR_USERS: [vals...] 476181ad6265SDimitry Andric // The record should not be emitted if it's an empty list. 476281ad6265SDimitry Andric if (Record.empty()) 476381ad6265SDimitry Andric return error("Invalid record"); 476481ad6265SDimitry Andric // When we have the RARE case of a BlockAddress Constant that is not 476581ad6265SDimitry Andric // scoped to the Function it refers to, we need to conservatively 476681ad6265SDimitry Andric // materialize the referred to Function, regardless of whether or not 476781ad6265SDimitry Andric // that Function will ultimately be linked, otherwise users of 476881ad6265SDimitry Andric // BitcodeReader might start splicing out Function bodies such that we 476981ad6265SDimitry Andric // might no longer be able to materialize the BlockAddress since the 477081ad6265SDimitry Andric // BasicBlock (and entire body of the Function) the BlockAddress refers 477181ad6265SDimitry Andric // to may have been moved. In the case that the user of BitcodeReader 477281ad6265SDimitry Andric // decides ultimately not to link the Function body, materializing here 477381ad6265SDimitry Andric // could be considered wasteful, but it's better than a deserialization 477481ad6265SDimitry Andric // failure as described. This keeps BitcodeReader unaware of complex 477581ad6265SDimitry Andric // linkage policy decisions such as those use by LTO, leaving those 477681ad6265SDimitry Andric // decisions "one layer up." 477781ad6265SDimitry Andric for (uint64_t ValID : Record) 477881ad6265SDimitry Andric if (auto *F = dyn_cast<Function>(ValueList[ValID])) 477981ad6265SDimitry Andric BackwardRefFunctions.push_back(F); 478081ad6265SDimitry Andric else 478181ad6265SDimitry Andric return error("Invalid record"); 478281ad6265SDimitry Andric 478381ad6265SDimitry Andric continue; 478481ad6265SDimitry Andric 4785*0b57cec5SDimitry Andric case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN 4786*0b57cec5SDimitry Andric // This record indicates that the last instruction is at the same 4787*0b57cec5SDimitry Andric // location as the previous instruction with a location. 4788*0b57cec5SDimitry Andric I = getLastInstruction(); 4789*0b57cec5SDimitry Andric 4790*0b57cec5SDimitry Andric if (!I) 4791*0b57cec5SDimitry Andric return error("Invalid record"); 4792*0b57cec5SDimitry Andric I->setDebugLoc(LastLoc); 4793*0b57cec5SDimitry Andric I = nullptr; 4794*0b57cec5SDimitry Andric continue; 4795*0b57cec5SDimitry Andric 4796*0b57cec5SDimitry Andric case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia] 4797*0b57cec5SDimitry Andric I = getLastInstruction(); 4798*0b57cec5SDimitry Andric if (!I || Record.size() < 4) 4799*0b57cec5SDimitry Andric return error("Invalid record"); 4800*0b57cec5SDimitry Andric 4801*0b57cec5SDimitry Andric unsigned Line = Record[0], Col = Record[1]; 4802*0b57cec5SDimitry Andric unsigned ScopeID = Record[2], IAID = Record[3]; 4803*0b57cec5SDimitry Andric bool isImplicitCode = Record.size() == 5 && Record[4]; 4804*0b57cec5SDimitry Andric 4805*0b57cec5SDimitry Andric MDNode *Scope = nullptr, *IA = nullptr; 4806*0b57cec5SDimitry Andric if (ScopeID) { 4807*0b57cec5SDimitry Andric Scope = dyn_cast_or_null<MDNode>( 4808*0b57cec5SDimitry Andric MDLoader->getMetadataFwdRefOrLoad(ScopeID - 1)); 4809*0b57cec5SDimitry Andric if (!Scope) 4810*0b57cec5SDimitry Andric return error("Invalid record"); 4811*0b57cec5SDimitry Andric } 4812*0b57cec5SDimitry Andric if (IAID) { 4813*0b57cec5SDimitry Andric IA = dyn_cast_or_null<MDNode>( 4814*0b57cec5SDimitry Andric MDLoader->getMetadataFwdRefOrLoad(IAID - 1)); 4815*0b57cec5SDimitry Andric if (!IA) 4816*0b57cec5SDimitry Andric return error("Invalid record"); 4817*0b57cec5SDimitry Andric } 4818e8d8bef9SDimitry Andric LastLoc = DILocation::get(Scope->getContext(), Line, Col, Scope, IA, 4819e8d8bef9SDimitry Andric isImplicitCode); 4820*0b57cec5SDimitry Andric I->setDebugLoc(LastLoc); 4821*0b57cec5SDimitry Andric I = nullptr; 4822*0b57cec5SDimitry Andric continue; 4823*0b57cec5SDimitry Andric } 4824*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_UNOP: { // UNOP: [opval, ty, opcode] 4825*0b57cec5SDimitry Andric unsigned OpNum = 0; 4826*0b57cec5SDimitry Andric Value *LHS; 482781ad6265SDimitry Andric unsigned TypeID; 482881ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, LHS, TypeID, CurBB) || 4829*0b57cec5SDimitry Andric OpNum+1 > Record.size()) 4830*0b57cec5SDimitry Andric return error("Invalid record"); 4831*0b57cec5SDimitry Andric 4832*0b57cec5SDimitry Andric int Opc = getDecodedUnaryOpcode(Record[OpNum++], LHS->getType()); 4833*0b57cec5SDimitry Andric if (Opc == -1) 4834*0b57cec5SDimitry Andric return error("Invalid record"); 4835*0b57cec5SDimitry Andric I = UnaryOperator::Create((Instruction::UnaryOps)Opc, LHS); 483681ad6265SDimitry Andric ResTypeID = TypeID; 4837*0b57cec5SDimitry Andric InstructionList.push_back(I); 4838*0b57cec5SDimitry Andric if (OpNum < Record.size()) { 4839*0b57cec5SDimitry Andric if (isa<FPMathOperator>(I)) { 4840*0b57cec5SDimitry Andric FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]); 4841*0b57cec5SDimitry Andric if (FMF.any()) 4842*0b57cec5SDimitry Andric I->setFastMathFlags(FMF); 4843*0b57cec5SDimitry Andric } 4844*0b57cec5SDimitry Andric } 4845*0b57cec5SDimitry Andric break; 4846*0b57cec5SDimitry Andric } 4847*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode] 4848*0b57cec5SDimitry Andric unsigned OpNum = 0; 4849*0b57cec5SDimitry Andric Value *LHS, *RHS; 485081ad6265SDimitry Andric unsigned TypeID; 485181ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, LHS, TypeID, CurBB) || 485281ad6265SDimitry Andric popValue(Record, OpNum, NextValueNo, LHS->getType(), TypeID, RHS, 485381ad6265SDimitry Andric CurBB) || 4854*0b57cec5SDimitry Andric OpNum+1 > Record.size()) 4855*0b57cec5SDimitry Andric return error("Invalid record"); 4856*0b57cec5SDimitry Andric 4857*0b57cec5SDimitry Andric int Opc = getDecodedBinaryOpcode(Record[OpNum++], LHS->getType()); 4858*0b57cec5SDimitry Andric if (Opc == -1) 4859*0b57cec5SDimitry Andric return error("Invalid record"); 4860*0b57cec5SDimitry Andric I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); 486181ad6265SDimitry Andric ResTypeID = TypeID; 4862*0b57cec5SDimitry Andric InstructionList.push_back(I); 4863*0b57cec5SDimitry Andric if (OpNum < Record.size()) { 4864*0b57cec5SDimitry Andric if (Opc == Instruction::Add || 4865*0b57cec5SDimitry Andric Opc == Instruction::Sub || 4866*0b57cec5SDimitry Andric Opc == Instruction::Mul || 4867*0b57cec5SDimitry Andric Opc == Instruction::Shl) { 4868*0b57cec5SDimitry Andric if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP)) 4869*0b57cec5SDimitry Andric cast<BinaryOperator>(I)->setHasNoSignedWrap(true); 4870*0b57cec5SDimitry Andric if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) 4871*0b57cec5SDimitry Andric cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true); 4872*0b57cec5SDimitry Andric } else if (Opc == Instruction::SDiv || 4873*0b57cec5SDimitry Andric Opc == Instruction::UDiv || 4874*0b57cec5SDimitry Andric Opc == Instruction::LShr || 4875*0b57cec5SDimitry Andric Opc == Instruction::AShr) { 4876*0b57cec5SDimitry Andric if (Record[OpNum] & (1 << bitc::PEO_EXACT)) 4877*0b57cec5SDimitry Andric cast<BinaryOperator>(I)->setIsExact(true); 4878*0b57cec5SDimitry Andric } else if (isa<FPMathOperator>(I)) { 4879*0b57cec5SDimitry Andric FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]); 4880*0b57cec5SDimitry Andric if (FMF.any()) 4881*0b57cec5SDimitry Andric I->setFastMathFlags(FMF); 4882*0b57cec5SDimitry Andric } 4883*0b57cec5SDimitry Andric 4884*0b57cec5SDimitry Andric } 4885*0b57cec5SDimitry Andric break; 4886*0b57cec5SDimitry Andric } 4887*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc] 4888*0b57cec5SDimitry Andric unsigned OpNum = 0; 4889*0b57cec5SDimitry Andric Value *Op; 489081ad6265SDimitry Andric unsigned OpTypeID; 489181ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB) || 4892*0b57cec5SDimitry Andric OpNum+2 != Record.size()) 4893*0b57cec5SDimitry Andric return error("Invalid record"); 4894*0b57cec5SDimitry Andric 489581ad6265SDimitry Andric ResTypeID = Record[OpNum]; 489681ad6265SDimitry Andric Type *ResTy = getTypeByID(ResTypeID); 4897*0b57cec5SDimitry Andric int Opc = getDecodedCastOpcode(Record[OpNum + 1]); 4898*0b57cec5SDimitry Andric if (Opc == -1 || !ResTy) 4899*0b57cec5SDimitry Andric return error("Invalid record"); 4900*0b57cec5SDimitry Andric Instruction *Temp = nullptr; 4901*0b57cec5SDimitry Andric if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) { 4902*0b57cec5SDimitry Andric if (Temp) { 4903*0b57cec5SDimitry Andric InstructionList.push_back(Temp); 4904480093f4SDimitry Andric assert(CurBB && "No current BB?"); 4905bdd1243dSDimitry Andric Temp->insertInto(CurBB, CurBB->end()); 4906*0b57cec5SDimitry Andric } 4907*0b57cec5SDimitry Andric } else { 4908*0b57cec5SDimitry Andric auto CastOp = (Instruction::CastOps)Opc; 4909*0b57cec5SDimitry Andric if (!CastInst::castIsValid(CastOp, Op, ResTy)) 4910*0b57cec5SDimitry Andric return error("Invalid cast"); 4911*0b57cec5SDimitry Andric I = CastInst::Create(CastOp, Op, ResTy); 4912*0b57cec5SDimitry Andric } 4913*0b57cec5SDimitry Andric InstructionList.push_back(I); 4914*0b57cec5SDimitry Andric break; 4915*0b57cec5SDimitry Andric } 4916*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD: 4917*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_GEP_OLD: 4918*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands] 4919*0b57cec5SDimitry Andric unsigned OpNum = 0; 4920*0b57cec5SDimitry Andric 492181ad6265SDimitry Andric unsigned TyID; 4922*0b57cec5SDimitry Andric Type *Ty; 4923*0b57cec5SDimitry Andric bool InBounds; 4924*0b57cec5SDimitry Andric 4925*0b57cec5SDimitry Andric if (BitCode == bitc::FUNC_CODE_INST_GEP) { 4926*0b57cec5SDimitry Andric InBounds = Record[OpNum++]; 492781ad6265SDimitry Andric TyID = Record[OpNum++]; 492881ad6265SDimitry Andric Ty = getTypeByID(TyID); 4929*0b57cec5SDimitry Andric } else { 4930*0b57cec5SDimitry Andric InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD; 493181ad6265SDimitry Andric TyID = InvalidTypeID; 4932*0b57cec5SDimitry Andric Ty = nullptr; 4933*0b57cec5SDimitry Andric } 4934*0b57cec5SDimitry Andric 4935*0b57cec5SDimitry Andric Value *BasePtr; 493681ad6265SDimitry Andric unsigned BasePtrTypeID; 493781ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr, BasePtrTypeID, 493881ad6265SDimitry Andric CurBB)) 4939*0b57cec5SDimitry Andric return error("Invalid record"); 4940*0b57cec5SDimitry Andric 4941*0b57cec5SDimitry Andric if (!Ty) { 494281ad6265SDimitry Andric TyID = getContainedTypeID(BasePtrTypeID); 494381ad6265SDimitry Andric if (BasePtr->getType()->isVectorTy()) 494481ad6265SDimitry Andric TyID = getContainedTypeID(TyID); 494581ad6265SDimitry Andric Ty = getTypeByID(TyID); 4946fe6060f1SDimitry Andric } else if (!cast<PointerType>(BasePtr->getType()->getScalarType()) 4947fe6060f1SDimitry Andric ->isOpaqueOrPointeeTypeMatches(Ty)) { 4948*0b57cec5SDimitry Andric return error( 4949*0b57cec5SDimitry Andric "Explicit gep type does not match pointee type of pointer operand"); 4950fe6060f1SDimitry Andric } 4951*0b57cec5SDimitry Andric 4952*0b57cec5SDimitry Andric SmallVector<Value*, 16> GEPIdx; 4953*0b57cec5SDimitry Andric while (OpNum != Record.size()) { 4954*0b57cec5SDimitry Andric Value *Op; 495581ad6265SDimitry Andric unsigned OpTypeID; 495681ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB)) 4957*0b57cec5SDimitry Andric return error("Invalid record"); 4958*0b57cec5SDimitry Andric GEPIdx.push_back(Op); 4959*0b57cec5SDimitry Andric } 4960*0b57cec5SDimitry Andric 4961*0b57cec5SDimitry Andric I = GetElementPtrInst::Create(Ty, BasePtr, GEPIdx); 4962*0b57cec5SDimitry Andric 496381ad6265SDimitry Andric ResTypeID = TyID; 496481ad6265SDimitry Andric if (cast<GEPOperator>(I)->getNumIndices() != 0) { 496581ad6265SDimitry Andric auto GTI = std::next(gep_type_begin(I)); 496681ad6265SDimitry Andric for (Value *Idx : drop_begin(cast<GEPOperator>(I)->indices())) { 496781ad6265SDimitry Andric unsigned SubType = 0; 496881ad6265SDimitry Andric if (GTI.isStruct()) { 496981ad6265SDimitry Andric ConstantInt *IdxC = 497081ad6265SDimitry Andric Idx->getType()->isVectorTy() 497181ad6265SDimitry Andric ? cast<ConstantInt>(cast<Constant>(Idx)->getSplatValue()) 497281ad6265SDimitry Andric : cast<ConstantInt>(Idx); 497381ad6265SDimitry Andric SubType = IdxC->getZExtValue(); 497481ad6265SDimitry Andric } 497581ad6265SDimitry Andric ResTypeID = getContainedTypeID(ResTypeID, SubType); 497681ad6265SDimitry Andric ++GTI; 497781ad6265SDimitry Andric } 497881ad6265SDimitry Andric } 497981ad6265SDimitry Andric 498081ad6265SDimitry Andric // At this point ResTypeID is the result element type. We need a pointer 498181ad6265SDimitry Andric // or vector of pointer to it. 498281ad6265SDimitry Andric ResTypeID = getVirtualTypeID(I->getType()->getScalarType(), ResTypeID); 498381ad6265SDimitry Andric if (I->getType()->isVectorTy()) 498481ad6265SDimitry Andric ResTypeID = getVirtualTypeID(I->getType(), ResTypeID); 498581ad6265SDimitry Andric 4986*0b57cec5SDimitry Andric InstructionList.push_back(I); 4987*0b57cec5SDimitry Andric if (InBounds) 4988*0b57cec5SDimitry Andric cast<GetElementPtrInst>(I)->setIsInBounds(true); 4989*0b57cec5SDimitry Andric break; 4990*0b57cec5SDimitry Andric } 4991*0b57cec5SDimitry Andric 4992*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_EXTRACTVAL: { 4993*0b57cec5SDimitry Andric // EXTRACTVAL: [opty, opval, n x indices] 4994*0b57cec5SDimitry Andric unsigned OpNum = 0; 4995*0b57cec5SDimitry Andric Value *Agg; 499681ad6265SDimitry Andric unsigned AggTypeID; 499781ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Agg, AggTypeID, CurBB)) 4998*0b57cec5SDimitry Andric return error("Invalid record"); 4999fe6060f1SDimitry Andric Type *Ty = Agg->getType(); 5000*0b57cec5SDimitry Andric 5001*0b57cec5SDimitry Andric unsigned RecSize = Record.size(); 5002*0b57cec5SDimitry Andric if (OpNum == RecSize) 5003*0b57cec5SDimitry Andric return error("EXTRACTVAL: Invalid instruction with 0 indices"); 5004*0b57cec5SDimitry Andric 5005*0b57cec5SDimitry Andric SmallVector<unsigned, 4> EXTRACTVALIdx; 500681ad6265SDimitry Andric ResTypeID = AggTypeID; 5007*0b57cec5SDimitry Andric for (; OpNum != RecSize; ++OpNum) { 5008fe6060f1SDimitry Andric bool IsArray = Ty->isArrayTy(); 5009fe6060f1SDimitry Andric bool IsStruct = Ty->isStructTy(); 5010*0b57cec5SDimitry Andric uint64_t Index = Record[OpNum]; 5011*0b57cec5SDimitry Andric 5012*0b57cec5SDimitry Andric if (!IsStruct && !IsArray) 5013*0b57cec5SDimitry Andric return error("EXTRACTVAL: Invalid type"); 5014*0b57cec5SDimitry Andric if ((unsigned)Index != Index) 5015*0b57cec5SDimitry Andric return error("Invalid value"); 5016fe6060f1SDimitry Andric if (IsStruct && Index >= Ty->getStructNumElements()) 5017*0b57cec5SDimitry Andric return error("EXTRACTVAL: Invalid struct index"); 5018fe6060f1SDimitry Andric if (IsArray && Index >= Ty->getArrayNumElements()) 5019*0b57cec5SDimitry Andric return error("EXTRACTVAL: Invalid array index"); 5020*0b57cec5SDimitry Andric EXTRACTVALIdx.push_back((unsigned)Index); 5021*0b57cec5SDimitry Andric 502281ad6265SDimitry Andric if (IsStruct) { 5023fe6060f1SDimitry Andric Ty = Ty->getStructElementType(Index); 502481ad6265SDimitry Andric ResTypeID = getContainedTypeID(ResTypeID, Index); 502581ad6265SDimitry Andric } else { 5026fe6060f1SDimitry Andric Ty = Ty->getArrayElementType(); 502781ad6265SDimitry Andric ResTypeID = getContainedTypeID(ResTypeID); 502881ad6265SDimitry Andric } 5029*0b57cec5SDimitry Andric } 5030*0b57cec5SDimitry Andric 5031*0b57cec5SDimitry Andric I = ExtractValueInst::Create(Agg, EXTRACTVALIdx); 5032*0b57cec5SDimitry Andric InstructionList.push_back(I); 5033*0b57cec5SDimitry Andric break; 5034*0b57cec5SDimitry Andric } 5035*0b57cec5SDimitry Andric 5036*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_INSERTVAL: { 5037*0b57cec5SDimitry Andric // INSERTVAL: [opty, opval, opty, opval, n x indices] 5038*0b57cec5SDimitry Andric unsigned OpNum = 0; 5039*0b57cec5SDimitry Andric Value *Agg; 504081ad6265SDimitry Andric unsigned AggTypeID; 504181ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Agg, AggTypeID, CurBB)) 5042*0b57cec5SDimitry Andric return error("Invalid record"); 5043*0b57cec5SDimitry Andric Value *Val; 504481ad6265SDimitry Andric unsigned ValTypeID; 504581ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Val, ValTypeID, CurBB)) 5046*0b57cec5SDimitry Andric return error("Invalid record"); 5047*0b57cec5SDimitry Andric 5048*0b57cec5SDimitry Andric unsigned RecSize = Record.size(); 5049*0b57cec5SDimitry Andric if (OpNum == RecSize) 5050*0b57cec5SDimitry Andric return error("INSERTVAL: Invalid instruction with 0 indices"); 5051*0b57cec5SDimitry Andric 5052*0b57cec5SDimitry Andric SmallVector<unsigned, 4> INSERTVALIdx; 5053*0b57cec5SDimitry Andric Type *CurTy = Agg->getType(); 5054*0b57cec5SDimitry Andric for (; OpNum != RecSize; ++OpNum) { 5055*0b57cec5SDimitry Andric bool IsArray = CurTy->isArrayTy(); 5056*0b57cec5SDimitry Andric bool IsStruct = CurTy->isStructTy(); 5057*0b57cec5SDimitry Andric uint64_t Index = Record[OpNum]; 5058*0b57cec5SDimitry Andric 5059*0b57cec5SDimitry Andric if (!IsStruct && !IsArray) 5060*0b57cec5SDimitry Andric return error("INSERTVAL: Invalid type"); 5061*0b57cec5SDimitry Andric if ((unsigned)Index != Index) 5062*0b57cec5SDimitry Andric return error("Invalid value"); 5063*0b57cec5SDimitry Andric if (IsStruct && Index >= CurTy->getStructNumElements()) 5064*0b57cec5SDimitry Andric return error("INSERTVAL: Invalid struct index"); 5065*0b57cec5SDimitry Andric if (IsArray && Index >= CurTy->getArrayNumElements()) 5066*0b57cec5SDimitry Andric return error("INSERTVAL: Invalid array index"); 5067*0b57cec5SDimitry Andric 5068*0b57cec5SDimitry Andric INSERTVALIdx.push_back((unsigned)Index); 5069*0b57cec5SDimitry Andric if (IsStruct) 5070*0b57cec5SDimitry Andric CurTy = CurTy->getStructElementType(Index); 5071*0b57cec5SDimitry Andric else 5072*0b57cec5SDimitry Andric CurTy = CurTy->getArrayElementType(); 5073*0b57cec5SDimitry Andric } 5074*0b57cec5SDimitry Andric 5075*0b57cec5SDimitry Andric if (CurTy != Val->getType()) 5076*0b57cec5SDimitry Andric return error("Inserted value type doesn't match aggregate type"); 5077*0b57cec5SDimitry Andric 5078*0b57cec5SDimitry Andric I = InsertValueInst::Create(Agg, Val, INSERTVALIdx); 507981ad6265SDimitry Andric ResTypeID = AggTypeID; 5080*0b57cec5SDimitry Andric InstructionList.push_back(I); 5081*0b57cec5SDimitry Andric break; 5082*0b57cec5SDimitry Andric } 5083*0b57cec5SDimitry Andric 5084*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval] 5085*0b57cec5SDimitry Andric // obsolete form of select 5086*0b57cec5SDimitry Andric // handles select i1 ... in old bitcode 5087*0b57cec5SDimitry Andric unsigned OpNum = 0; 5088*0b57cec5SDimitry Andric Value *TrueVal, *FalseVal, *Cond; 508981ad6265SDimitry Andric unsigned TypeID; 509081ad6265SDimitry Andric Type *CondType = Type::getInt1Ty(Context); 509181ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal, TypeID, 509281ad6265SDimitry Andric CurBB) || 509381ad6265SDimitry Andric popValue(Record, OpNum, NextValueNo, TrueVal->getType(), TypeID, 509481ad6265SDimitry Andric FalseVal, CurBB) || 509581ad6265SDimitry Andric popValue(Record, OpNum, NextValueNo, CondType, 509681ad6265SDimitry Andric getVirtualTypeID(CondType), Cond, CurBB)) 5097*0b57cec5SDimitry Andric return error("Invalid record"); 5098*0b57cec5SDimitry Andric 5099*0b57cec5SDimitry Andric I = SelectInst::Create(Cond, TrueVal, FalseVal); 510081ad6265SDimitry Andric ResTypeID = TypeID; 5101*0b57cec5SDimitry Andric InstructionList.push_back(I); 5102*0b57cec5SDimitry Andric break; 5103*0b57cec5SDimitry Andric } 5104*0b57cec5SDimitry Andric 5105*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred] 5106*0b57cec5SDimitry Andric // new form of select 5107*0b57cec5SDimitry Andric // handles select i1 or select [N x i1] 5108*0b57cec5SDimitry Andric unsigned OpNum = 0; 5109*0b57cec5SDimitry Andric Value *TrueVal, *FalseVal, *Cond; 511081ad6265SDimitry Andric unsigned ValTypeID, CondTypeID; 511181ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal, ValTypeID, 511281ad6265SDimitry Andric CurBB) || 511381ad6265SDimitry Andric popValue(Record, OpNum, NextValueNo, TrueVal->getType(), ValTypeID, 511481ad6265SDimitry Andric FalseVal, CurBB) || 511581ad6265SDimitry Andric getValueTypePair(Record, OpNum, NextValueNo, Cond, CondTypeID, CurBB)) 5116*0b57cec5SDimitry Andric return error("Invalid record"); 5117*0b57cec5SDimitry Andric 5118*0b57cec5SDimitry Andric // select condition can be either i1 or [N x i1] 5119*0b57cec5SDimitry Andric if (VectorType* vector_type = 5120*0b57cec5SDimitry Andric dyn_cast<VectorType>(Cond->getType())) { 5121*0b57cec5SDimitry Andric // expect <n x i1> 5122*0b57cec5SDimitry Andric if (vector_type->getElementType() != Type::getInt1Ty(Context)) 5123*0b57cec5SDimitry Andric return error("Invalid type for value"); 5124*0b57cec5SDimitry Andric } else { 5125*0b57cec5SDimitry Andric // expect i1 5126*0b57cec5SDimitry Andric if (Cond->getType() != Type::getInt1Ty(Context)) 5127*0b57cec5SDimitry Andric return error("Invalid type for value"); 5128*0b57cec5SDimitry Andric } 5129*0b57cec5SDimitry Andric 5130*0b57cec5SDimitry Andric I = SelectInst::Create(Cond, TrueVal, FalseVal); 513181ad6265SDimitry Andric ResTypeID = ValTypeID; 5132*0b57cec5SDimitry Andric InstructionList.push_back(I); 5133*0b57cec5SDimitry Andric if (OpNum < Record.size() && isa<FPMathOperator>(I)) { 5134*0b57cec5SDimitry Andric FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]); 5135*0b57cec5SDimitry Andric if (FMF.any()) 5136*0b57cec5SDimitry Andric I->setFastMathFlags(FMF); 5137*0b57cec5SDimitry Andric } 5138*0b57cec5SDimitry Andric break; 5139*0b57cec5SDimitry Andric } 5140*0b57cec5SDimitry Andric 5141*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval] 5142*0b57cec5SDimitry Andric unsigned OpNum = 0; 5143*0b57cec5SDimitry Andric Value *Vec, *Idx; 514481ad6265SDimitry Andric unsigned VecTypeID, IdxTypeID; 514581ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Vec, VecTypeID, CurBB) || 514681ad6265SDimitry Andric getValueTypePair(Record, OpNum, NextValueNo, Idx, IdxTypeID, CurBB)) 5147*0b57cec5SDimitry Andric return error("Invalid record"); 5148*0b57cec5SDimitry Andric if (!Vec->getType()->isVectorTy()) 5149*0b57cec5SDimitry Andric return error("Invalid type for value"); 5150*0b57cec5SDimitry Andric I = ExtractElementInst::Create(Vec, Idx); 515181ad6265SDimitry Andric ResTypeID = getContainedTypeID(VecTypeID); 5152*0b57cec5SDimitry Andric InstructionList.push_back(I); 5153*0b57cec5SDimitry Andric break; 5154*0b57cec5SDimitry Andric } 5155*0b57cec5SDimitry Andric 5156*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval] 5157*0b57cec5SDimitry Andric unsigned OpNum = 0; 5158*0b57cec5SDimitry Andric Value *Vec, *Elt, *Idx; 515981ad6265SDimitry Andric unsigned VecTypeID, IdxTypeID; 516081ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Vec, VecTypeID, CurBB)) 5161*0b57cec5SDimitry Andric return error("Invalid record"); 5162*0b57cec5SDimitry Andric if (!Vec->getType()->isVectorTy()) 5163*0b57cec5SDimitry Andric return error("Invalid type for value"); 5164*0b57cec5SDimitry Andric if (popValue(Record, OpNum, NextValueNo, 516581ad6265SDimitry Andric cast<VectorType>(Vec->getType())->getElementType(), 516681ad6265SDimitry Andric getContainedTypeID(VecTypeID), Elt, CurBB) || 516781ad6265SDimitry Andric getValueTypePair(Record, OpNum, NextValueNo, Idx, IdxTypeID, CurBB)) 5168*0b57cec5SDimitry Andric return error("Invalid record"); 5169*0b57cec5SDimitry Andric I = InsertElementInst::Create(Vec, Elt, Idx); 517081ad6265SDimitry Andric ResTypeID = VecTypeID; 5171*0b57cec5SDimitry Andric InstructionList.push_back(I); 5172*0b57cec5SDimitry Andric break; 5173*0b57cec5SDimitry Andric } 5174*0b57cec5SDimitry Andric 5175*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval] 5176*0b57cec5SDimitry Andric unsigned OpNum = 0; 5177*0b57cec5SDimitry Andric Value *Vec1, *Vec2, *Mask; 517881ad6265SDimitry Andric unsigned Vec1TypeID; 517981ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Vec1, Vec1TypeID, 518081ad6265SDimitry Andric CurBB) || 518181ad6265SDimitry Andric popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec1TypeID, 518281ad6265SDimitry Andric Vec2, CurBB)) 5183*0b57cec5SDimitry Andric return error("Invalid record"); 5184*0b57cec5SDimitry Andric 518581ad6265SDimitry Andric unsigned MaskTypeID; 518681ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Mask, MaskTypeID, CurBB)) 5187*0b57cec5SDimitry Andric return error("Invalid record"); 5188*0b57cec5SDimitry Andric if (!Vec1->getType()->isVectorTy() || !Vec2->getType()->isVectorTy()) 5189*0b57cec5SDimitry Andric return error("Invalid type for value"); 51905ffd83dbSDimitry Andric 5191*0b57cec5SDimitry Andric I = new ShuffleVectorInst(Vec1, Vec2, Mask); 519281ad6265SDimitry Andric ResTypeID = 519381ad6265SDimitry Andric getVirtualTypeID(I->getType(), getContainedTypeID(Vec1TypeID)); 5194*0b57cec5SDimitry Andric InstructionList.push_back(I); 5195*0b57cec5SDimitry Andric break; 5196*0b57cec5SDimitry Andric } 5197*0b57cec5SDimitry Andric 5198*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred] 5199*0b57cec5SDimitry Andric // Old form of ICmp/FCmp returning bool 5200*0b57cec5SDimitry Andric // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were 5201*0b57cec5SDimitry Andric // both legal on vectors but had different behaviour. 5202*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred] 5203*0b57cec5SDimitry Andric // FCmp/ICmp returning bool or vector of bool 5204*0b57cec5SDimitry Andric 5205*0b57cec5SDimitry Andric unsigned OpNum = 0; 5206*0b57cec5SDimitry Andric Value *LHS, *RHS; 520781ad6265SDimitry Andric unsigned LHSTypeID; 520881ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, LHS, LHSTypeID, CurBB) || 520981ad6265SDimitry Andric popValue(Record, OpNum, NextValueNo, LHS->getType(), LHSTypeID, RHS, 521081ad6265SDimitry Andric CurBB)) 5211*0b57cec5SDimitry Andric return error("Invalid record"); 5212*0b57cec5SDimitry Andric 5213*0b57cec5SDimitry Andric if (OpNum >= Record.size()) 5214*0b57cec5SDimitry Andric return error( 5215*0b57cec5SDimitry Andric "Invalid record: operand number exceeded available operands"); 5216*0b57cec5SDimitry Andric 5217*0b57cec5SDimitry Andric unsigned PredVal = Record[OpNum]; 5218*0b57cec5SDimitry Andric bool IsFP = LHS->getType()->isFPOrFPVectorTy(); 5219*0b57cec5SDimitry Andric FastMathFlags FMF; 5220*0b57cec5SDimitry Andric if (IsFP && Record.size() > OpNum+1) 5221*0b57cec5SDimitry Andric FMF = getDecodedFastMathFlags(Record[++OpNum]); 5222*0b57cec5SDimitry Andric 5223*0b57cec5SDimitry Andric if (OpNum+1 != Record.size()) 5224*0b57cec5SDimitry Andric return error("Invalid record"); 5225*0b57cec5SDimitry Andric 5226*0b57cec5SDimitry Andric if (LHS->getType()->isFPOrFPVectorTy()) 5227*0b57cec5SDimitry Andric I = new FCmpInst((FCmpInst::Predicate)PredVal, LHS, RHS); 5228*0b57cec5SDimitry Andric else 5229*0b57cec5SDimitry Andric I = new ICmpInst((ICmpInst::Predicate)PredVal, LHS, RHS); 5230*0b57cec5SDimitry Andric 523181ad6265SDimitry Andric ResTypeID = getVirtualTypeID(I->getType()->getScalarType()); 523281ad6265SDimitry Andric if (LHS->getType()->isVectorTy()) 523381ad6265SDimitry Andric ResTypeID = getVirtualTypeID(I->getType(), ResTypeID); 523481ad6265SDimitry Andric 5235*0b57cec5SDimitry Andric if (FMF.any()) 5236*0b57cec5SDimitry Andric I->setFastMathFlags(FMF); 5237*0b57cec5SDimitry Andric InstructionList.push_back(I); 5238*0b57cec5SDimitry Andric break; 5239*0b57cec5SDimitry Andric } 5240*0b57cec5SDimitry Andric 5241*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>] 5242*0b57cec5SDimitry Andric { 5243*0b57cec5SDimitry Andric unsigned Size = Record.size(); 5244*0b57cec5SDimitry Andric if (Size == 0) { 5245*0b57cec5SDimitry Andric I = ReturnInst::Create(Context); 5246*0b57cec5SDimitry Andric InstructionList.push_back(I); 5247*0b57cec5SDimitry Andric break; 5248*0b57cec5SDimitry Andric } 5249*0b57cec5SDimitry Andric 5250*0b57cec5SDimitry Andric unsigned OpNum = 0; 5251*0b57cec5SDimitry Andric Value *Op = nullptr; 525281ad6265SDimitry Andric unsigned OpTypeID; 525381ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB)) 5254*0b57cec5SDimitry Andric return error("Invalid record"); 5255*0b57cec5SDimitry Andric if (OpNum != Record.size()) 5256*0b57cec5SDimitry Andric return error("Invalid record"); 5257*0b57cec5SDimitry Andric 5258*0b57cec5SDimitry Andric I = ReturnInst::Create(Context, Op); 5259*0b57cec5SDimitry Andric InstructionList.push_back(I); 5260*0b57cec5SDimitry Andric break; 5261*0b57cec5SDimitry Andric } 5262*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#] 5263*0b57cec5SDimitry Andric if (Record.size() != 1 && Record.size() != 3) 5264*0b57cec5SDimitry Andric return error("Invalid record"); 5265*0b57cec5SDimitry Andric BasicBlock *TrueDest = getBasicBlock(Record[0]); 5266*0b57cec5SDimitry Andric if (!TrueDest) 5267*0b57cec5SDimitry Andric return error("Invalid record"); 5268*0b57cec5SDimitry Andric 5269*0b57cec5SDimitry Andric if (Record.size() == 1) { 5270*0b57cec5SDimitry Andric I = BranchInst::Create(TrueDest); 5271*0b57cec5SDimitry Andric InstructionList.push_back(I); 5272*0b57cec5SDimitry Andric } 5273*0b57cec5SDimitry Andric else { 5274*0b57cec5SDimitry Andric BasicBlock *FalseDest = getBasicBlock(Record[1]); 527581ad6265SDimitry Andric Type *CondType = Type::getInt1Ty(Context); 527681ad6265SDimitry Andric Value *Cond = getValue(Record, 2, NextValueNo, CondType, 527781ad6265SDimitry Andric getVirtualTypeID(CondType), CurBB); 5278*0b57cec5SDimitry Andric if (!FalseDest || !Cond) 5279*0b57cec5SDimitry Andric return error("Invalid record"); 5280*0b57cec5SDimitry Andric I = BranchInst::Create(TrueDest, FalseDest, Cond); 5281*0b57cec5SDimitry Andric InstructionList.push_back(I); 5282*0b57cec5SDimitry Andric } 5283*0b57cec5SDimitry Andric break; 5284*0b57cec5SDimitry Andric } 5285*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_CLEANUPRET: { // CLEANUPRET: [val] or [val,bb#] 5286*0b57cec5SDimitry Andric if (Record.size() != 1 && Record.size() != 2) 5287*0b57cec5SDimitry Andric return error("Invalid record"); 5288*0b57cec5SDimitry Andric unsigned Idx = 0; 528981ad6265SDimitry Andric Type *TokenTy = Type::getTokenTy(Context); 529081ad6265SDimitry Andric Value *CleanupPad = getValue(Record, Idx++, NextValueNo, TokenTy, 529181ad6265SDimitry Andric getVirtualTypeID(TokenTy), CurBB); 5292*0b57cec5SDimitry Andric if (!CleanupPad) 5293*0b57cec5SDimitry Andric return error("Invalid record"); 5294*0b57cec5SDimitry Andric BasicBlock *UnwindDest = nullptr; 5295*0b57cec5SDimitry Andric if (Record.size() == 2) { 5296*0b57cec5SDimitry Andric UnwindDest = getBasicBlock(Record[Idx++]); 5297*0b57cec5SDimitry Andric if (!UnwindDest) 5298*0b57cec5SDimitry Andric return error("Invalid record"); 5299*0b57cec5SDimitry Andric } 5300*0b57cec5SDimitry Andric 5301*0b57cec5SDimitry Andric I = CleanupReturnInst::Create(CleanupPad, UnwindDest); 5302*0b57cec5SDimitry Andric InstructionList.push_back(I); 5303*0b57cec5SDimitry Andric break; 5304*0b57cec5SDimitry Andric } 5305*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_CATCHRET: { // CATCHRET: [val,bb#] 5306*0b57cec5SDimitry Andric if (Record.size() != 2) 5307*0b57cec5SDimitry Andric return error("Invalid record"); 5308*0b57cec5SDimitry Andric unsigned Idx = 0; 530981ad6265SDimitry Andric Type *TokenTy = Type::getTokenTy(Context); 531081ad6265SDimitry Andric Value *CatchPad = getValue(Record, Idx++, NextValueNo, TokenTy, 531181ad6265SDimitry Andric getVirtualTypeID(TokenTy), CurBB); 5312*0b57cec5SDimitry Andric if (!CatchPad) 5313*0b57cec5SDimitry Andric return error("Invalid record"); 5314*0b57cec5SDimitry Andric BasicBlock *BB = getBasicBlock(Record[Idx++]); 5315*0b57cec5SDimitry Andric if (!BB) 5316*0b57cec5SDimitry Andric return error("Invalid record"); 5317*0b57cec5SDimitry Andric 5318*0b57cec5SDimitry Andric I = CatchReturnInst::Create(CatchPad, BB); 5319*0b57cec5SDimitry Andric InstructionList.push_back(I); 5320*0b57cec5SDimitry Andric break; 5321*0b57cec5SDimitry Andric } 5322*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_CATCHSWITCH: { // CATCHSWITCH: [tok,num,(bb)*,bb?] 5323*0b57cec5SDimitry Andric // We must have, at minimum, the outer scope and the number of arguments. 5324*0b57cec5SDimitry Andric if (Record.size() < 2) 5325*0b57cec5SDimitry Andric return error("Invalid record"); 5326*0b57cec5SDimitry Andric 5327*0b57cec5SDimitry Andric unsigned Idx = 0; 5328*0b57cec5SDimitry Andric 532981ad6265SDimitry Andric Type *TokenTy = Type::getTokenTy(Context); 533081ad6265SDimitry Andric Value *ParentPad = getValue(Record, Idx++, NextValueNo, TokenTy, 533181ad6265SDimitry Andric getVirtualTypeID(TokenTy), CurBB); 5332*0b57cec5SDimitry Andric 5333*0b57cec5SDimitry Andric unsigned NumHandlers = Record[Idx++]; 5334*0b57cec5SDimitry Andric 5335*0b57cec5SDimitry Andric SmallVector<BasicBlock *, 2> Handlers; 5336*0b57cec5SDimitry Andric for (unsigned Op = 0; Op != NumHandlers; ++Op) { 5337*0b57cec5SDimitry Andric BasicBlock *BB = getBasicBlock(Record[Idx++]); 5338*0b57cec5SDimitry Andric if (!BB) 5339*0b57cec5SDimitry Andric return error("Invalid record"); 5340*0b57cec5SDimitry Andric Handlers.push_back(BB); 5341*0b57cec5SDimitry Andric } 5342*0b57cec5SDimitry Andric 5343*0b57cec5SDimitry Andric BasicBlock *UnwindDest = nullptr; 5344*0b57cec5SDimitry Andric if (Idx + 1 == Record.size()) { 5345*0b57cec5SDimitry Andric UnwindDest = getBasicBlock(Record[Idx++]); 5346*0b57cec5SDimitry Andric if (!UnwindDest) 5347*0b57cec5SDimitry Andric return error("Invalid record"); 5348*0b57cec5SDimitry Andric } 5349*0b57cec5SDimitry Andric 5350*0b57cec5SDimitry Andric if (Record.size() != Idx) 5351*0b57cec5SDimitry Andric return error("Invalid record"); 5352*0b57cec5SDimitry Andric 5353*0b57cec5SDimitry Andric auto *CatchSwitch = 5354*0b57cec5SDimitry Andric CatchSwitchInst::Create(ParentPad, UnwindDest, NumHandlers); 5355*0b57cec5SDimitry Andric for (BasicBlock *Handler : Handlers) 5356*0b57cec5SDimitry Andric CatchSwitch->addHandler(Handler); 5357*0b57cec5SDimitry Andric I = CatchSwitch; 535881ad6265SDimitry Andric ResTypeID = getVirtualTypeID(I->getType()); 5359*0b57cec5SDimitry Andric InstructionList.push_back(I); 5360*0b57cec5SDimitry Andric break; 5361*0b57cec5SDimitry Andric } 5362*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_CATCHPAD: 5363*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_CLEANUPPAD: { // [tok,num,(ty,val)*] 5364*0b57cec5SDimitry Andric // We must have, at minimum, the outer scope and the number of arguments. 5365*0b57cec5SDimitry Andric if (Record.size() < 2) 5366*0b57cec5SDimitry Andric return error("Invalid record"); 5367*0b57cec5SDimitry Andric 5368*0b57cec5SDimitry Andric unsigned Idx = 0; 5369*0b57cec5SDimitry Andric 537081ad6265SDimitry Andric Type *TokenTy = Type::getTokenTy(Context); 537181ad6265SDimitry Andric Value *ParentPad = getValue(Record, Idx++, NextValueNo, TokenTy, 537281ad6265SDimitry Andric getVirtualTypeID(TokenTy), CurBB); 5373*0b57cec5SDimitry Andric 5374*0b57cec5SDimitry Andric unsigned NumArgOperands = Record[Idx++]; 5375*0b57cec5SDimitry Andric 5376*0b57cec5SDimitry Andric SmallVector<Value *, 2> Args; 5377*0b57cec5SDimitry Andric for (unsigned Op = 0; Op != NumArgOperands; ++Op) { 5378*0b57cec5SDimitry Andric Value *Val; 537981ad6265SDimitry Andric unsigned ValTypeID; 538081ad6265SDimitry Andric if (getValueTypePair(Record, Idx, NextValueNo, Val, ValTypeID, nullptr)) 5381*0b57cec5SDimitry Andric return error("Invalid record"); 5382*0b57cec5SDimitry Andric Args.push_back(Val); 5383*0b57cec5SDimitry Andric } 5384*0b57cec5SDimitry Andric 5385*0b57cec5SDimitry Andric if (Record.size() != Idx) 5386*0b57cec5SDimitry Andric return error("Invalid record"); 5387*0b57cec5SDimitry Andric 5388*0b57cec5SDimitry Andric if (BitCode == bitc::FUNC_CODE_INST_CLEANUPPAD) 5389*0b57cec5SDimitry Andric I = CleanupPadInst::Create(ParentPad, Args); 5390*0b57cec5SDimitry Andric else 5391*0b57cec5SDimitry Andric I = CatchPadInst::Create(ParentPad, Args); 539281ad6265SDimitry Andric ResTypeID = getVirtualTypeID(I->getType()); 5393*0b57cec5SDimitry Andric InstructionList.push_back(I); 5394*0b57cec5SDimitry Andric break; 5395*0b57cec5SDimitry Andric } 5396*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...] 5397*0b57cec5SDimitry Andric // Check magic 5398*0b57cec5SDimitry Andric if ((Record[0] >> 16) == SWITCH_INST_MAGIC) { 5399*0b57cec5SDimitry Andric // "New" SwitchInst format with case ranges. The changes to write this 5400*0b57cec5SDimitry Andric // format were reverted but we still recognize bitcode that uses it. 5401*0b57cec5SDimitry Andric // Hopefully someday we will have support for case ranges and can use 5402*0b57cec5SDimitry Andric // this format again. 5403*0b57cec5SDimitry Andric 540481ad6265SDimitry Andric unsigned OpTyID = Record[1]; 540581ad6265SDimitry Andric Type *OpTy = getTypeByID(OpTyID); 5406*0b57cec5SDimitry Andric unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth(); 5407*0b57cec5SDimitry Andric 540881ad6265SDimitry Andric Value *Cond = getValue(Record, 2, NextValueNo, OpTy, OpTyID, CurBB); 5409*0b57cec5SDimitry Andric BasicBlock *Default = getBasicBlock(Record[3]); 5410*0b57cec5SDimitry Andric if (!OpTy || !Cond || !Default) 5411*0b57cec5SDimitry Andric return error("Invalid record"); 5412*0b57cec5SDimitry Andric 5413*0b57cec5SDimitry Andric unsigned NumCases = Record[4]; 5414*0b57cec5SDimitry Andric 5415*0b57cec5SDimitry Andric SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); 5416*0b57cec5SDimitry Andric InstructionList.push_back(SI); 5417*0b57cec5SDimitry Andric 5418*0b57cec5SDimitry Andric unsigned CurIdx = 5; 5419*0b57cec5SDimitry Andric for (unsigned i = 0; i != NumCases; ++i) { 5420*0b57cec5SDimitry Andric SmallVector<ConstantInt*, 1> CaseVals; 5421*0b57cec5SDimitry Andric unsigned NumItems = Record[CurIdx++]; 5422*0b57cec5SDimitry Andric for (unsigned ci = 0; ci != NumItems; ++ci) { 5423*0b57cec5SDimitry Andric bool isSingleNumber = Record[CurIdx++]; 5424*0b57cec5SDimitry Andric 5425*0b57cec5SDimitry Andric APInt Low; 5426*0b57cec5SDimitry Andric unsigned ActiveWords = 1; 5427*0b57cec5SDimitry Andric if (ValueBitWidth > 64) 5428*0b57cec5SDimitry Andric ActiveWords = Record[CurIdx++]; 5429bdd1243dSDimitry Andric Low = readWideAPInt(ArrayRef(&Record[CurIdx], ActiveWords), 5430*0b57cec5SDimitry Andric ValueBitWidth); 5431*0b57cec5SDimitry Andric CurIdx += ActiveWords; 5432*0b57cec5SDimitry Andric 5433*0b57cec5SDimitry Andric if (!isSingleNumber) { 5434*0b57cec5SDimitry Andric ActiveWords = 1; 5435*0b57cec5SDimitry Andric if (ValueBitWidth > 64) 5436*0b57cec5SDimitry Andric ActiveWords = Record[CurIdx++]; 5437bdd1243dSDimitry Andric APInt High = readWideAPInt(ArrayRef(&Record[CurIdx], ActiveWords), 5438bdd1243dSDimitry Andric ValueBitWidth); 5439*0b57cec5SDimitry Andric CurIdx += ActiveWords; 5440*0b57cec5SDimitry Andric 5441*0b57cec5SDimitry Andric // FIXME: It is not clear whether values in the range should be 5442*0b57cec5SDimitry Andric // compared as signed or unsigned values. The partially 5443*0b57cec5SDimitry Andric // implemented changes that used this format in the past used 5444*0b57cec5SDimitry Andric // unsigned comparisons. 5445*0b57cec5SDimitry Andric for ( ; Low.ule(High); ++Low) 5446*0b57cec5SDimitry Andric CaseVals.push_back(ConstantInt::get(Context, Low)); 5447*0b57cec5SDimitry Andric } else 5448*0b57cec5SDimitry Andric CaseVals.push_back(ConstantInt::get(Context, Low)); 5449*0b57cec5SDimitry Andric } 5450*0b57cec5SDimitry Andric BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]); 54514824e7fdSDimitry Andric for (ConstantInt *Cst : CaseVals) 54524824e7fdSDimitry Andric SI->addCase(Cst, DestBB); 5453*0b57cec5SDimitry Andric } 5454*0b57cec5SDimitry Andric I = SI; 5455*0b57cec5SDimitry Andric break; 5456*0b57cec5SDimitry Andric } 5457*0b57cec5SDimitry Andric 5458*0b57cec5SDimitry Andric // Old SwitchInst format without case ranges. 5459*0b57cec5SDimitry Andric 5460*0b57cec5SDimitry Andric if (Record.size() < 3 || (Record.size() & 1) == 0) 5461*0b57cec5SDimitry Andric return error("Invalid record"); 546281ad6265SDimitry Andric unsigned OpTyID = Record[0]; 546381ad6265SDimitry Andric Type *OpTy = getTypeByID(OpTyID); 546481ad6265SDimitry Andric Value *Cond = getValue(Record, 1, NextValueNo, OpTy, OpTyID, CurBB); 5465*0b57cec5SDimitry Andric BasicBlock *Default = getBasicBlock(Record[2]); 5466*0b57cec5SDimitry Andric if (!OpTy || !Cond || !Default) 5467*0b57cec5SDimitry Andric return error("Invalid record"); 5468*0b57cec5SDimitry Andric unsigned NumCases = (Record.size()-3)/2; 5469*0b57cec5SDimitry Andric SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); 5470*0b57cec5SDimitry Andric InstructionList.push_back(SI); 5471*0b57cec5SDimitry Andric for (unsigned i = 0, e = NumCases; i != e; ++i) { 547281ad6265SDimitry Andric ConstantInt *CaseVal = dyn_cast_or_null<ConstantInt>( 547381ad6265SDimitry Andric getFnValueByID(Record[3+i*2], OpTy, OpTyID, nullptr)); 5474*0b57cec5SDimitry Andric BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]); 5475*0b57cec5SDimitry Andric if (!CaseVal || !DestBB) { 5476*0b57cec5SDimitry Andric delete SI; 5477*0b57cec5SDimitry Andric return error("Invalid record"); 5478*0b57cec5SDimitry Andric } 5479*0b57cec5SDimitry Andric SI->addCase(CaseVal, DestBB); 5480*0b57cec5SDimitry Andric } 5481*0b57cec5SDimitry Andric I = SI; 5482*0b57cec5SDimitry Andric break; 5483*0b57cec5SDimitry Andric } 5484*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...] 5485*0b57cec5SDimitry Andric if (Record.size() < 2) 5486*0b57cec5SDimitry Andric return error("Invalid record"); 548781ad6265SDimitry Andric unsigned OpTyID = Record[0]; 548881ad6265SDimitry Andric Type *OpTy = getTypeByID(OpTyID); 548981ad6265SDimitry Andric Value *Address = getValue(Record, 1, NextValueNo, OpTy, OpTyID, CurBB); 5490*0b57cec5SDimitry Andric if (!OpTy || !Address) 5491*0b57cec5SDimitry Andric return error("Invalid record"); 5492*0b57cec5SDimitry Andric unsigned NumDests = Record.size()-2; 5493*0b57cec5SDimitry Andric IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests); 5494*0b57cec5SDimitry Andric InstructionList.push_back(IBI); 5495*0b57cec5SDimitry Andric for (unsigned i = 0, e = NumDests; i != e; ++i) { 5496*0b57cec5SDimitry Andric if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) { 5497*0b57cec5SDimitry Andric IBI->addDestination(DestBB); 5498*0b57cec5SDimitry Andric } else { 5499*0b57cec5SDimitry Andric delete IBI; 5500*0b57cec5SDimitry Andric return error("Invalid record"); 5501*0b57cec5SDimitry Andric } 5502*0b57cec5SDimitry Andric } 5503*0b57cec5SDimitry Andric I = IBI; 5504*0b57cec5SDimitry Andric break; 5505*0b57cec5SDimitry Andric } 5506*0b57cec5SDimitry Andric 5507*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_INVOKE: { 5508*0b57cec5SDimitry Andric // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...] 5509*0b57cec5SDimitry Andric if (Record.size() < 4) 5510*0b57cec5SDimitry Andric return error("Invalid record"); 5511*0b57cec5SDimitry Andric unsigned OpNum = 0; 5512*0b57cec5SDimitry Andric AttributeList PAL = getAttributes(Record[OpNum++]); 5513*0b57cec5SDimitry Andric unsigned CCInfo = Record[OpNum++]; 5514*0b57cec5SDimitry Andric BasicBlock *NormalBB = getBasicBlock(Record[OpNum++]); 5515*0b57cec5SDimitry Andric BasicBlock *UnwindBB = getBasicBlock(Record[OpNum++]); 5516*0b57cec5SDimitry Andric 551781ad6265SDimitry Andric unsigned FTyID = InvalidTypeID; 5518*0b57cec5SDimitry Andric FunctionType *FTy = nullptr; 5519*0b57cec5SDimitry Andric if ((CCInfo >> 13) & 1) { 552081ad6265SDimitry Andric FTyID = Record[OpNum++]; 552181ad6265SDimitry Andric FTy = dyn_cast<FunctionType>(getTypeByID(FTyID)); 5522fe6060f1SDimitry Andric if (!FTy) 5523*0b57cec5SDimitry Andric return error("Explicit invoke type is not a function type"); 5524*0b57cec5SDimitry Andric } 5525*0b57cec5SDimitry Andric 5526*0b57cec5SDimitry Andric Value *Callee; 552781ad6265SDimitry Andric unsigned CalleeTypeID; 552881ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Callee, CalleeTypeID, 552981ad6265SDimitry Andric CurBB)) 5530*0b57cec5SDimitry Andric return error("Invalid record"); 5531*0b57cec5SDimitry Andric 5532*0b57cec5SDimitry Andric PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType()); 5533*0b57cec5SDimitry Andric if (!CalleeTy) 5534*0b57cec5SDimitry Andric return error("Callee is not a pointer"); 5535*0b57cec5SDimitry Andric if (!FTy) { 553681ad6265SDimitry Andric FTyID = getContainedTypeID(CalleeTypeID); 553781ad6265SDimitry Andric FTy = dyn_cast_or_null<FunctionType>(getTypeByID(FTyID)); 5538fe6060f1SDimitry Andric if (!FTy) 5539*0b57cec5SDimitry Andric return error("Callee is not of pointer to function type"); 5540fe6060f1SDimitry Andric } else if (!CalleeTy->isOpaqueOrPointeeTypeMatches(FTy)) 5541*0b57cec5SDimitry Andric return error("Explicit invoke type does not match pointee type of " 5542*0b57cec5SDimitry Andric "callee operand"); 5543*0b57cec5SDimitry Andric if (Record.size() < FTy->getNumParams() + OpNum) 5544*0b57cec5SDimitry Andric return error("Insufficient operands to call"); 5545*0b57cec5SDimitry Andric 5546*0b57cec5SDimitry Andric SmallVector<Value*, 16> Ops; 554781ad6265SDimitry Andric SmallVector<unsigned, 16> ArgTyIDs; 5548*0b57cec5SDimitry Andric for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { 554981ad6265SDimitry Andric unsigned ArgTyID = getContainedTypeID(FTyID, i + 1); 555081ad6265SDimitry Andric Ops.push_back(getValue(Record, OpNum, NextValueNo, FTy->getParamType(i), 555181ad6265SDimitry Andric ArgTyID, CurBB)); 555281ad6265SDimitry Andric ArgTyIDs.push_back(ArgTyID); 5553*0b57cec5SDimitry Andric if (!Ops.back()) 5554*0b57cec5SDimitry Andric return error("Invalid record"); 5555*0b57cec5SDimitry Andric } 5556*0b57cec5SDimitry Andric 5557*0b57cec5SDimitry Andric if (!FTy->isVarArg()) { 5558*0b57cec5SDimitry Andric if (Record.size() != OpNum) 5559*0b57cec5SDimitry Andric return error("Invalid record"); 5560*0b57cec5SDimitry Andric } else { 5561*0b57cec5SDimitry Andric // Read type/value pairs for varargs params. 5562*0b57cec5SDimitry Andric while (OpNum != Record.size()) { 5563*0b57cec5SDimitry Andric Value *Op; 556481ad6265SDimitry Andric unsigned OpTypeID; 556581ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB)) 5566*0b57cec5SDimitry Andric return error("Invalid record"); 5567*0b57cec5SDimitry Andric Ops.push_back(Op); 556881ad6265SDimitry Andric ArgTyIDs.push_back(OpTypeID); 5569*0b57cec5SDimitry Andric } 5570*0b57cec5SDimitry Andric } 5571*0b57cec5SDimitry Andric 557281ad6265SDimitry Andric // Upgrade the bundles if needed. 557381ad6265SDimitry Andric if (!OperandBundles.empty()) 557481ad6265SDimitry Andric UpgradeOperandBundles(OperandBundles); 557581ad6265SDimitry Andric 5576*0b57cec5SDimitry Andric I = InvokeInst::Create(FTy, Callee, NormalBB, UnwindBB, Ops, 5577*0b57cec5SDimitry Andric OperandBundles); 557881ad6265SDimitry Andric ResTypeID = getContainedTypeID(FTyID); 5579*0b57cec5SDimitry Andric OperandBundles.clear(); 5580*0b57cec5SDimitry Andric InstructionList.push_back(I); 5581*0b57cec5SDimitry Andric cast<InvokeInst>(I)->setCallingConv( 5582*0b57cec5SDimitry Andric static_cast<CallingConv::ID>(CallingConv::MaxID & CCInfo)); 5583*0b57cec5SDimitry Andric cast<InvokeInst>(I)->setAttributes(PAL); 558481ad6265SDimitry Andric if (Error Err = propagateAttributeTypes(cast<CallBase>(I), ArgTyIDs)) { 558581ad6265SDimitry Andric I->deleteValue(); 558681ad6265SDimitry Andric return Err; 558781ad6265SDimitry Andric } 5588*0b57cec5SDimitry Andric 5589*0b57cec5SDimitry Andric break; 5590*0b57cec5SDimitry Andric } 5591*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval] 5592*0b57cec5SDimitry Andric unsigned Idx = 0; 5593*0b57cec5SDimitry Andric Value *Val = nullptr; 559481ad6265SDimitry Andric unsigned ValTypeID; 559581ad6265SDimitry Andric if (getValueTypePair(Record, Idx, NextValueNo, Val, ValTypeID, CurBB)) 5596*0b57cec5SDimitry Andric return error("Invalid record"); 5597*0b57cec5SDimitry Andric I = ResumeInst::Create(Val); 5598*0b57cec5SDimitry Andric InstructionList.push_back(I); 5599*0b57cec5SDimitry Andric break; 5600*0b57cec5SDimitry Andric } 5601*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_CALLBR: { 5602*0b57cec5SDimitry Andric // CALLBR: [attr, cc, norm, transfs, fty, fnid, args] 5603*0b57cec5SDimitry Andric unsigned OpNum = 0; 5604*0b57cec5SDimitry Andric AttributeList PAL = getAttributes(Record[OpNum++]); 5605*0b57cec5SDimitry Andric unsigned CCInfo = Record[OpNum++]; 5606*0b57cec5SDimitry Andric 5607*0b57cec5SDimitry Andric BasicBlock *DefaultDest = getBasicBlock(Record[OpNum++]); 5608*0b57cec5SDimitry Andric unsigned NumIndirectDests = Record[OpNum++]; 5609*0b57cec5SDimitry Andric SmallVector<BasicBlock *, 16> IndirectDests; 5610*0b57cec5SDimitry Andric for (unsigned i = 0, e = NumIndirectDests; i != e; ++i) 5611*0b57cec5SDimitry Andric IndirectDests.push_back(getBasicBlock(Record[OpNum++])); 5612*0b57cec5SDimitry Andric 561381ad6265SDimitry Andric unsigned FTyID = InvalidTypeID; 5614*0b57cec5SDimitry Andric FunctionType *FTy = nullptr; 5615*0b57cec5SDimitry Andric if ((CCInfo >> bitc::CALL_EXPLICIT_TYPE) & 1) { 561681ad6265SDimitry Andric FTyID = Record[OpNum++]; 561781ad6265SDimitry Andric FTy = dyn_cast_or_null<FunctionType>(getTypeByID(FTyID)); 5618fe6060f1SDimitry Andric if (!FTy) 5619*0b57cec5SDimitry Andric return error("Explicit call type is not a function type"); 5620*0b57cec5SDimitry Andric } 5621*0b57cec5SDimitry Andric 5622*0b57cec5SDimitry Andric Value *Callee; 562381ad6265SDimitry Andric unsigned CalleeTypeID; 562481ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Callee, CalleeTypeID, 562581ad6265SDimitry Andric CurBB)) 5626*0b57cec5SDimitry Andric return error("Invalid record"); 5627*0b57cec5SDimitry Andric 5628*0b57cec5SDimitry Andric PointerType *OpTy = dyn_cast<PointerType>(Callee->getType()); 5629*0b57cec5SDimitry Andric if (!OpTy) 5630*0b57cec5SDimitry Andric return error("Callee is not a pointer type"); 5631*0b57cec5SDimitry Andric if (!FTy) { 563281ad6265SDimitry Andric FTyID = getContainedTypeID(CalleeTypeID); 563381ad6265SDimitry Andric FTy = dyn_cast_or_null<FunctionType>(getTypeByID(FTyID)); 5634fe6060f1SDimitry Andric if (!FTy) 5635*0b57cec5SDimitry Andric return error("Callee is not of pointer to function type"); 563604eeddc0SDimitry Andric } else if (!OpTy->isOpaqueOrPointeeTypeMatches(FTy)) 5637*0b57cec5SDimitry Andric return error("Explicit call type does not match pointee type of " 5638*0b57cec5SDimitry Andric "callee operand"); 5639*0b57cec5SDimitry Andric if (Record.size() < FTy->getNumParams() + OpNum) 5640*0b57cec5SDimitry Andric return error("Insufficient operands to call"); 5641*0b57cec5SDimitry Andric 5642*0b57cec5SDimitry Andric SmallVector<Value*, 16> Args; 564381ad6265SDimitry Andric SmallVector<unsigned, 16> ArgTyIDs; 5644*0b57cec5SDimitry Andric // Read the fixed params. 5645*0b57cec5SDimitry Andric for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { 564604eeddc0SDimitry Andric Value *Arg; 564781ad6265SDimitry Andric unsigned ArgTyID = getContainedTypeID(FTyID, i + 1); 5648*0b57cec5SDimitry Andric if (FTy->getParamType(i)->isLabelTy()) 564904eeddc0SDimitry Andric Arg = getBasicBlock(Record[OpNum]); 5650*0b57cec5SDimitry Andric else 565181ad6265SDimitry Andric Arg = getValue(Record, OpNum, NextValueNo, FTy->getParamType(i), 565281ad6265SDimitry Andric ArgTyID, CurBB); 565304eeddc0SDimitry Andric if (!Arg) 5654*0b57cec5SDimitry Andric return error("Invalid record"); 565504eeddc0SDimitry Andric Args.push_back(Arg); 565681ad6265SDimitry Andric ArgTyIDs.push_back(ArgTyID); 5657*0b57cec5SDimitry Andric } 5658*0b57cec5SDimitry Andric 5659*0b57cec5SDimitry Andric // Read type/value pairs for varargs params. 5660*0b57cec5SDimitry Andric if (!FTy->isVarArg()) { 5661*0b57cec5SDimitry Andric if (OpNum != Record.size()) 5662*0b57cec5SDimitry Andric return error("Invalid record"); 5663*0b57cec5SDimitry Andric } else { 5664*0b57cec5SDimitry Andric while (OpNum != Record.size()) { 5665*0b57cec5SDimitry Andric Value *Op; 566681ad6265SDimitry Andric unsigned OpTypeID; 566781ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB)) 5668*0b57cec5SDimitry Andric return error("Invalid record"); 5669*0b57cec5SDimitry Andric Args.push_back(Op); 567081ad6265SDimitry Andric ArgTyIDs.push_back(OpTypeID); 5671*0b57cec5SDimitry Andric } 5672*0b57cec5SDimitry Andric } 5673*0b57cec5SDimitry Andric 567481ad6265SDimitry Andric // Upgrade the bundles if needed. 567581ad6265SDimitry Andric if (!OperandBundles.empty()) 567681ad6265SDimitry Andric UpgradeOperandBundles(OperandBundles); 567781ad6265SDimitry Andric 5678fcaf7f86SDimitry Andric if (auto *IA = dyn_cast<InlineAsm>(Callee)) { 5679fcaf7f86SDimitry Andric InlineAsm::ConstraintInfoVector ConstraintInfo = IA->ParseConstraints(); 5680fcaf7f86SDimitry Andric auto IsLabelConstraint = [](const InlineAsm::ConstraintInfo &CI) { 5681fcaf7f86SDimitry Andric return CI.Type == InlineAsm::isLabel; 5682fcaf7f86SDimitry Andric }; 5683fcaf7f86SDimitry Andric if (none_of(ConstraintInfo, IsLabelConstraint)) { 5684fcaf7f86SDimitry Andric // Upgrade explicit blockaddress arguments to label constraints. 5685fcaf7f86SDimitry Andric // Verify that the last arguments are blockaddress arguments that 5686fcaf7f86SDimitry Andric // match the indirect destinations. Clang always generates callbr 5687fcaf7f86SDimitry Andric // in this form. We could support reordering with more effort. 5688fcaf7f86SDimitry Andric unsigned FirstBlockArg = Args.size() - IndirectDests.size(); 5689fcaf7f86SDimitry Andric for (unsigned ArgNo = FirstBlockArg; ArgNo < Args.size(); ++ArgNo) { 5690fcaf7f86SDimitry Andric unsigned LabelNo = ArgNo - FirstBlockArg; 5691fcaf7f86SDimitry Andric auto *BA = dyn_cast<BlockAddress>(Args[ArgNo]); 5692fcaf7f86SDimitry Andric if (!BA || BA->getFunction() != F || 5693fcaf7f86SDimitry Andric LabelNo > IndirectDests.size() || 5694fcaf7f86SDimitry Andric BA->getBasicBlock() != IndirectDests[LabelNo]) 5695fcaf7f86SDimitry Andric return error("callbr argument does not match indirect dest"); 5696fcaf7f86SDimitry Andric } 5697fcaf7f86SDimitry Andric 5698fcaf7f86SDimitry Andric // Remove blockaddress arguments. 5699fcaf7f86SDimitry Andric Args.erase(Args.begin() + FirstBlockArg, Args.end()); 5700fcaf7f86SDimitry Andric ArgTyIDs.erase(ArgTyIDs.begin() + FirstBlockArg, ArgTyIDs.end()); 5701fcaf7f86SDimitry Andric 5702fcaf7f86SDimitry Andric // Recreate the function type with less arguments. 5703fcaf7f86SDimitry Andric SmallVector<Type *> ArgTys; 5704fcaf7f86SDimitry Andric for (Value *Arg : Args) 5705fcaf7f86SDimitry Andric ArgTys.push_back(Arg->getType()); 5706fcaf7f86SDimitry Andric FTy = 5707fcaf7f86SDimitry Andric FunctionType::get(FTy->getReturnType(), ArgTys, FTy->isVarArg()); 5708fcaf7f86SDimitry Andric 5709fcaf7f86SDimitry Andric // Update constraint string to use label constraints. 5710fcaf7f86SDimitry Andric std::string Constraints = IA->getConstraintString(); 5711fcaf7f86SDimitry Andric unsigned ArgNo = 0; 5712fcaf7f86SDimitry Andric size_t Pos = 0; 5713fcaf7f86SDimitry Andric for (const auto &CI : ConstraintInfo) { 5714fcaf7f86SDimitry Andric if (CI.hasArg()) { 5715fcaf7f86SDimitry Andric if (ArgNo >= FirstBlockArg) 5716fcaf7f86SDimitry Andric Constraints.insert(Pos, "!"); 5717fcaf7f86SDimitry Andric ++ArgNo; 5718fcaf7f86SDimitry Andric } 5719fcaf7f86SDimitry Andric 5720fcaf7f86SDimitry Andric // Go to next constraint in string. 5721fcaf7f86SDimitry Andric Pos = Constraints.find(',', Pos); 5722fcaf7f86SDimitry Andric if (Pos == std::string::npos) 5723fcaf7f86SDimitry Andric break; 5724fcaf7f86SDimitry Andric ++Pos; 5725fcaf7f86SDimitry Andric } 5726fcaf7f86SDimitry Andric 5727fcaf7f86SDimitry Andric Callee = InlineAsm::get(FTy, IA->getAsmString(), Constraints, 5728fcaf7f86SDimitry Andric IA->hasSideEffects(), IA->isAlignStack(), 5729fcaf7f86SDimitry Andric IA->getDialect(), IA->canThrow()); 5730fcaf7f86SDimitry Andric } 5731fcaf7f86SDimitry Andric } 5732fcaf7f86SDimitry Andric 5733*0b57cec5SDimitry Andric I = CallBrInst::Create(FTy, Callee, DefaultDest, IndirectDests, Args, 5734*0b57cec5SDimitry Andric OperandBundles); 573581ad6265SDimitry Andric ResTypeID = getContainedTypeID(FTyID); 5736*0b57cec5SDimitry Andric OperandBundles.clear(); 5737*0b57cec5SDimitry Andric InstructionList.push_back(I); 5738*0b57cec5SDimitry Andric cast<CallBrInst>(I)->setCallingConv( 5739*0b57cec5SDimitry Andric static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV)); 5740*0b57cec5SDimitry Andric cast<CallBrInst>(I)->setAttributes(PAL); 574181ad6265SDimitry Andric if (Error Err = propagateAttributeTypes(cast<CallBase>(I), ArgTyIDs)) { 574281ad6265SDimitry Andric I->deleteValue(); 574381ad6265SDimitry Andric return Err; 574481ad6265SDimitry Andric } 5745*0b57cec5SDimitry Andric break; 5746*0b57cec5SDimitry Andric } 5747*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE 5748*0b57cec5SDimitry Andric I = new UnreachableInst(Context); 5749*0b57cec5SDimitry Andric InstructionList.push_back(I); 5750*0b57cec5SDimitry Andric break; 5751*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...] 5752e8d8bef9SDimitry Andric if (Record.empty()) 575381ad6265SDimitry Andric return error("Invalid phi record"); 57548bcb0991SDimitry Andric // The first record specifies the type. 575581ad6265SDimitry Andric unsigned TyID = Record[0]; 575681ad6265SDimitry Andric Type *Ty = getTypeByID(TyID); 5757*0b57cec5SDimitry Andric if (!Ty) 575881ad6265SDimitry Andric return error("Invalid phi record"); 5759*0b57cec5SDimitry Andric 57608bcb0991SDimitry Andric // Phi arguments are pairs of records of [value, basic block]. 57618bcb0991SDimitry Andric // There is an optional final record for fast-math-flags if this phi has a 57628bcb0991SDimitry Andric // floating-point type. 57638bcb0991SDimitry Andric size_t NumArgs = (Record.size() - 1) / 2; 57648bcb0991SDimitry Andric PHINode *PN = PHINode::Create(Ty, NumArgs); 576581ad6265SDimitry Andric if ((Record.size() - 1) % 2 == 1 && !isa<FPMathOperator>(PN)) { 576681ad6265SDimitry Andric PN->deleteValue(); 576781ad6265SDimitry Andric return error("Invalid phi record"); 576881ad6265SDimitry Andric } 5769*0b57cec5SDimitry Andric InstructionList.push_back(PN); 5770*0b57cec5SDimitry Andric 577181ad6265SDimitry Andric SmallDenseMap<BasicBlock *, Value *> Args; 57728bcb0991SDimitry Andric for (unsigned i = 0; i != NumArgs; i++) { 577381ad6265SDimitry Andric BasicBlock *BB = getBasicBlock(Record[i * 2 + 2]); 577481ad6265SDimitry Andric if (!BB) { 577581ad6265SDimitry Andric PN->deleteValue(); 577681ad6265SDimitry Andric return error("Invalid phi BB"); 577781ad6265SDimitry Andric } 577881ad6265SDimitry Andric 577981ad6265SDimitry Andric // Phi nodes may contain the same predecessor multiple times, in which 578081ad6265SDimitry Andric // case the incoming value must be identical. Directly reuse the already 578181ad6265SDimitry Andric // seen value here, to avoid expanding a constant expression multiple 578281ad6265SDimitry Andric // times. 578381ad6265SDimitry Andric auto It = Args.find(BB); 578481ad6265SDimitry Andric if (It != Args.end()) { 578581ad6265SDimitry Andric PN->addIncoming(It->second, BB); 578681ad6265SDimitry Andric continue; 578781ad6265SDimitry Andric } 578881ad6265SDimitry Andric 578981ad6265SDimitry Andric // If there already is a block for this edge (from a different phi), 579081ad6265SDimitry Andric // use it. 579181ad6265SDimitry Andric BasicBlock *EdgeBB = ConstExprEdgeBBs.lookup({BB, CurBB}); 579281ad6265SDimitry Andric if (!EdgeBB) { 579381ad6265SDimitry Andric // Otherwise, use a temporary block (that we will discard if it 579481ad6265SDimitry Andric // turns out to be unnecessary). 579581ad6265SDimitry Andric if (!PhiConstExprBB) 579681ad6265SDimitry Andric PhiConstExprBB = BasicBlock::Create(Context, "phi.constexpr", F); 579781ad6265SDimitry Andric EdgeBB = PhiConstExprBB; 579881ad6265SDimitry Andric } 579981ad6265SDimitry Andric 5800*0b57cec5SDimitry Andric // With the new function encoding, it is possible that operands have 5801*0b57cec5SDimitry Andric // negative IDs (for forward references). Use a signed VBR 5802*0b57cec5SDimitry Andric // representation to keep the encoding small. 580381ad6265SDimitry Andric Value *V; 5804*0b57cec5SDimitry Andric if (UseRelativeIDs) 580581ad6265SDimitry Andric V = getValueSigned(Record, i * 2 + 1, NextValueNo, Ty, TyID, EdgeBB); 5806*0b57cec5SDimitry Andric else 580781ad6265SDimitry Andric V = getValue(Record, i * 2 + 1, NextValueNo, Ty, TyID, EdgeBB); 580881ad6265SDimitry Andric if (!V) { 580981ad6265SDimitry Andric PN->deleteValue(); 581081ad6265SDimitry Andric PhiConstExprBB->eraseFromParent(); 581181ad6265SDimitry Andric return error("Invalid phi record"); 581281ad6265SDimitry Andric } 581381ad6265SDimitry Andric 581481ad6265SDimitry Andric if (EdgeBB == PhiConstExprBB && !EdgeBB->empty()) { 581581ad6265SDimitry Andric ConstExprEdgeBBs.insert({{BB, CurBB}, EdgeBB}); 581681ad6265SDimitry Andric PhiConstExprBB = nullptr; 581781ad6265SDimitry Andric } 5818*0b57cec5SDimitry Andric PN->addIncoming(V, BB); 581981ad6265SDimitry Andric Args.insert({BB, V}); 5820*0b57cec5SDimitry Andric } 5821*0b57cec5SDimitry Andric I = PN; 582281ad6265SDimitry Andric ResTypeID = TyID; 58238bcb0991SDimitry Andric 58248bcb0991SDimitry Andric // If there are an even number of records, the final record must be FMF. 58258bcb0991SDimitry Andric if (Record.size() % 2 == 0) { 58268bcb0991SDimitry Andric assert(isa<FPMathOperator>(I) && "Unexpected phi type"); 58278bcb0991SDimitry Andric FastMathFlags FMF = getDecodedFastMathFlags(Record[Record.size() - 1]); 58288bcb0991SDimitry Andric if (FMF.any()) 58298bcb0991SDimitry Andric I->setFastMathFlags(FMF); 58308bcb0991SDimitry Andric } 58318bcb0991SDimitry Andric 5832*0b57cec5SDimitry Andric break; 5833*0b57cec5SDimitry Andric } 5834*0b57cec5SDimitry Andric 5835*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_LANDINGPAD: 5836*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_LANDINGPAD_OLD: { 5837*0b57cec5SDimitry Andric // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?] 5838*0b57cec5SDimitry Andric unsigned Idx = 0; 5839*0b57cec5SDimitry Andric if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD) { 5840*0b57cec5SDimitry Andric if (Record.size() < 3) 5841*0b57cec5SDimitry Andric return error("Invalid record"); 5842*0b57cec5SDimitry Andric } else { 5843*0b57cec5SDimitry Andric assert(BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD); 5844*0b57cec5SDimitry Andric if (Record.size() < 4) 5845*0b57cec5SDimitry Andric return error("Invalid record"); 5846*0b57cec5SDimitry Andric } 584781ad6265SDimitry Andric ResTypeID = Record[Idx++]; 584881ad6265SDimitry Andric Type *Ty = getTypeByID(ResTypeID); 5849*0b57cec5SDimitry Andric if (!Ty) 5850*0b57cec5SDimitry Andric return error("Invalid record"); 5851*0b57cec5SDimitry Andric if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD) { 5852*0b57cec5SDimitry Andric Value *PersFn = nullptr; 585381ad6265SDimitry Andric unsigned PersFnTypeID; 585481ad6265SDimitry Andric if (getValueTypePair(Record, Idx, NextValueNo, PersFn, PersFnTypeID, 585581ad6265SDimitry Andric nullptr)) 5856*0b57cec5SDimitry Andric return error("Invalid record"); 5857*0b57cec5SDimitry Andric 5858*0b57cec5SDimitry Andric if (!F->hasPersonalityFn()) 5859*0b57cec5SDimitry Andric F->setPersonalityFn(cast<Constant>(PersFn)); 5860*0b57cec5SDimitry Andric else if (F->getPersonalityFn() != cast<Constant>(PersFn)) 5861*0b57cec5SDimitry Andric return error("Personality function mismatch"); 5862*0b57cec5SDimitry Andric } 5863*0b57cec5SDimitry Andric 5864*0b57cec5SDimitry Andric bool IsCleanup = !!Record[Idx++]; 5865*0b57cec5SDimitry Andric unsigned NumClauses = Record[Idx++]; 5866*0b57cec5SDimitry Andric LandingPadInst *LP = LandingPadInst::Create(Ty, NumClauses); 5867*0b57cec5SDimitry Andric LP->setCleanup(IsCleanup); 5868*0b57cec5SDimitry Andric for (unsigned J = 0; J != NumClauses; ++J) { 5869*0b57cec5SDimitry Andric LandingPadInst::ClauseType CT = 5870*0b57cec5SDimitry Andric LandingPadInst::ClauseType(Record[Idx++]); (void)CT; 5871*0b57cec5SDimitry Andric Value *Val; 587281ad6265SDimitry Andric unsigned ValTypeID; 5873*0b57cec5SDimitry Andric 587481ad6265SDimitry Andric if (getValueTypePair(Record, Idx, NextValueNo, Val, ValTypeID, 587581ad6265SDimitry Andric nullptr)) { 5876*0b57cec5SDimitry Andric delete LP; 5877*0b57cec5SDimitry Andric return error("Invalid record"); 5878*0b57cec5SDimitry Andric } 5879*0b57cec5SDimitry Andric 5880*0b57cec5SDimitry Andric assert((CT != LandingPadInst::Catch || 5881*0b57cec5SDimitry Andric !isa<ArrayType>(Val->getType())) && 5882*0b57cec5SDimitry Andric "Catch clause has a invalid type!"); 5883*0b57cec5SDimitry Andric assert((CT != LandingPadInst::Filter || 5884*0b57cec5SDimitry Andric isa<ArrayType>(Val->getType())) && 5885*0b57cec5SDimitry Andric "Filter clause has invalid type!"); 5886*0b57cec5SDimitry Andric LP->addClause(cast<Constant>(Val)); 5887*0b57cec5SDimitry Andric } 5888*0b57cec5SDimitry Andric 5889*0b57cec5SDimitry Andric I = LP; 5890*0b57cec5SDimitry Andric InstructionList.push_back(I); 5891*0b57cec5SDimitry Andric break; 5892*0b57cec5SDimitry Andric } 5893*0b57cec5SDimitry Andric 5894*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align] 589581ad6265SDimitry Andric if (Record.size() != 4 && Record.size() != 5) 5896*0b57cec5SDimitry Andric return error("Invalid record"); 5897e8d8bef9SDimitry Andric using APV = AllocaPackedValues; 5898e8d8bef9SDimitry Andric const uint64_t Rec = Record[3]; 5899e8d8bef9SDimitry Andric const bool InAlloca = Bitfield::get<APV::UsedWithInAlloca>(Rec); 5900e8d8bef9SDimitry Andric const bool SwiftError = Bitfield::get<APV::SwiftError>(Rec); 590181ad6265SDimitry Andric unsigned TyID = Record[0]; 590281ad6265SDimitry Andric Type *Ty = getTypeByID(TyID); 5903e8d8bef9SDimitry Andric if (!Bitfield::get<APV::ExplicitType>(Rec)) { 590481ad6265SDimitry Andric TyID = getContainedTypeID(TyID); 590581ad6265SDimitry Andric Ty = getTypeByID(TyID); 590681ad6265SDimitry Andric if (!Ty) 590781ad6265SDimitry Andric return error("Missing element type for old-style alloca"); 5908*0b57cec5SDimitry Andric } 590981ad6265SDimitry Andric unsigned OpTyID = Record[1]; 591081ad6265SDimitry Andric Type *OpTy = getTypeByID(OpTyID); 591181ad6265SDimitry Andric Value *Size = getFnValueByID(Record[2], OpTy, OpTyID, CurBB); 59128bcb0991SDimitry Andric MaybeAlign Align; 5913349cc55cSDimitry Andric uint64_t AlignExp = 5914349cc55cSDimitry Andric Bitfield::get<APV::AlignLower>(Rec) | 5915349cc55cSDimitry Andric (Bitfield::get<APV::AlignUpper>(Rec) << APV::AlignLower::Bits); 5916349cc55cSDimitry Andric if (Error Err = parseAlignmentValue(AlignExp, Align)) { 5917*0b57cec5SDimitry Andric return Err; 5918*0b57cec5SDimitry Andric } 5919*0b57cec5SDimitry Andric if (!Ty || !Size) 5920*0b57cec5SDimitry Andric return error("Invalid record"); 5921*0b57cec5SDimitry Andric 5922*0b57cec5SDimitry Andric const DataLayout &DL = TheModule->getDataLayout(); 592381ad6265SDimitry Andric unsigned AS = Record.size() == 5 ? Record[4] : DL.getAllocaAddrSpace(); 5924*0b57cec5SDimitry Andric 59255ffd83dbSDimitry Andric SmallPtrSet<Type *, 4> Visited; 59265ffd83dbSDimitry Andric if (!Align && !Ty->isSized(&Visited)) 59275ffd83dbSDimitry Andric return error("alloca of unsized type"); 59285ffd83dbSDimitry Andric if (!Align) 59295ffd83dbSDimitry Andric Align = DL.getPrefTypeAlign(Ty); 59305ffd83dbSDimitry Andric 59315ffd83dbSDimitry Andric AllocaInst *AI = new AllocaInst(Ty, AS, Size, *Align); 5932*0b57cec5SDimitry Andric AI->setUsedWithInAlloca(InAlloca); 5933*0b57cec5SDimitry Andric AI->setSwiftError(SwiftError); 5934*0b57cec5SDimitry Andric I = AI; 593581ad6265SDimitry Andric ResTypeID = getVirtualTypeID(AI->getType(), TyID); 5936*0b57cec5SDimitry Andric InstructionList.push_back(I); 5937*0b57cec5SDimitry Andric break; 5938*0b57cec5SDimitry Andric } 5939*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol] 5940*0b57cec5SDimitry Andric unsigned OpNum = 0; 5941*0b57cec5SDimitry Andric Value *Op; 594281ad6265SDimitry Andric unsigned OpTypeID; 594381ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB) || 5944*0b57cec5SDimitry Andric (OpNum + 2 != Record.size() && OpNum + 3 != Record.size())) 5945*0b57cec5SDimitry Andric return error("Invalid record"); 5946*0b57cec5SDimitry Andric 5947*0b57cec5SDimitry Andric if (!isa<PointerType>(Op->getType())) 5948*0b57cec5SDimitry Andric return error("Load operand is not a pointer type"); 5949*0b57cec5SDimitry Andric 5950*0b57cec5SDimitry Andric Type *Ty = nullptr; 5951*0b57cec5SDimitry Andric if (OpNum + 3 == Record.size()) { 595281ad6265SDimitry Andric ResTypeID = Record[OpNum++]; 595381ad6265SDimitry Andric Ty = getTypeByID(ResTypeID); 5954fe6060f1SDimitry Andric } else { 595581ad6265SDimitry Andric ResTypeID = getContainedTypeID(OpTypeID); 595681ad6265SDimitry Andric Ty = getTypeByID(ResTypeID); 595781ad6265SDimitry Andric if (!Ty) 595881ad6265SDimitry Andric return error("Missing element type for old-style load"); 5959fe6060f1SDimitry Andric } 5960*0b57cec5SDimitry Andric 5961*0b57cec5SDimitry Andric if (Error Err = typeCheckLoadStoreInst(Ty, Op->getType())) 5962*0b57cec5SDimitry Andric return Err; 5963*0b57cec5SDimitry Andric 59648bcb0991SDimitry Andric MaybeAlign Align; 5965*0b57cec5SDimitry Andric if (Error Err = parseAlignmentValue(Record[OpNum], Align)) 5966*0b57cec5SDimitry Andric return Err; 59675ffd83dbSDimitry Andric SmallPtrSet<Type *, 4> Visited; 59685ffd83dbSDimitry Andric if (!Align && !Ty->isSized(&Visited)) 59695ffd83dbSDimitry Andric return error("load of unsized type"); 59705ffd83dbSDimitry Andric if (!Align) 59715ffd83dbSDimitry Andric Align = TheModule->getDataLayout().getABITypeAlign(Ty); 59725ffd83dbSDimitry Andric I = new LoadInst(Ty, Op, "", Record[OpNum + 1], *Align); 5973*0b57cec5SDimitry Andric InstructionList.push_back(I); 5974*0b57cec5SDimitry Andric break; 5975*0b57cec5SDimitry Andric } 5976*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_LOADATOMIC: { 5977*0b57cec5SDimitry Andric // LOADATOMIC: [opty, op, align, vol, ordering, ssid] 5978*0b57cec5SDimitry Andric unsigned OpNum = 0; 5979*0b57cec5SDimitry Andric Value *Op; 598081ad6265SDimitry Andric unsigned OpTypeID; 598181ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB) || 5982*0b57cec5SDimitry Andric (OpNum + 4 != Record.size() && OpNum + 5 != Record.size())) 5983*0b57cec5SDimitry Andric return error("Invalid record"); 5984*0b57cec5SDimitry Andric 5985*0b57cec5SDimitry Andric if (!isa<PointerType>(Op->getType())) 5986*0b57cec5SDimitry Andric return error("Load operand is not a pointer type"); 5987*0b57cec5SDimitry Andric 5988*0b57cec5SDimitry Andric Type *Ty = nullptr; 5989*0b57cec5SDimitry Andric if (OpNum + 5 == Record.size()) { 599081ad6265SDimitry Andric ResTypeID = Record[OpNum++]; 599181ad6265SDimitry Andric Ty = getTypeByID(ResTypeID); 5992fe6060f1SDimitry Andric } else { 599381ad6265SDimitry Andric ResTypeID = getContainedTypeID(OpTypeID); 599481ad6265SDimitry Andric Ty = getTypeByID(ResTypeID); 599581ad6265SDimitry Andric if (!Ty) 599681ad6265SDimitry Andric return error("Missing element type for old style atomic load"); 5997fe6060f1SDimitry Andric } 5998*0b57cec5SDimitry Andric 5999*0b57cec5SDimitry Andric if (Error Err = typeCheckLoadStoreInst(Ty, Op->getType())) 6000*0b57cec5SDimitry Andric return Err; 6001*0b57cec5SDimitry Andric 6002*0b57cec5SDimitry Andric AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); 6003*0b57cec5SDimitry Andric if (Ordering == AtomicOrdering::NotAtomic || 6004*0b57cec5SDimitry Andric Ordering == AtomicOrdering::Release || 6005*0b57cec5SDimitry Andric Ordering == AtomicOrdering::AcquireRelease) 6006*0b57cec5SDimitry Andric return error("Invalid record"); 6007*0b57cec5SDimitry Andric if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0) 6008*0b57cec5SDimitry Andric return error("Invalid record"); 6009*0b57cec5SDimitry Andric SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]); 6010*0b57cec5SDimitry Andric 60118bcb0991SDimitry Andric MaybeAlign Align; 6012*0b57cec5SDimitry Andric if (Error Err = parseAlignmentValue(Record[OpNum], Align)) 6013*0b57cec5SDimitry Andric return Err; 60145ffd83dbSDimitry Andric if (!Align) 60155ffd83dbSDimitry Andric return error("Alignment missing from atomic load"); 60165ffd83dbSDimitry Andric I = new LoadInst(Ty, Op, "", Record[OpNum + 1], *Align, Ordering, SSID); 6017*0b57cec5SDimitry Andric InstructionList.push_back(I); 6018*0b57cec5SDimitry Andric break; 6019*0b57cec5SDimitry Andric } 6020*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_STORE: 6021*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_STORE_OLD: { // STORE2:[ptrty, ptr, val, align, vol] 6022*0b57cec5SDimitry Andric unsigned OpNum = 0; 6023*0b57cec5SDimitry Andric Value *Val, *Ptr; 602481ad6265SDimitry Andric unsigned PtrTypeID, ValTypeID; 602581ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, PtrTypeID, CurBB)) 602681ad6265SDimitry Andric return error("Invalid record"); 602781ad6265SDimitry Andric 602881ad6265SDimitry Andric if (BitCode == bitc::FUNC_CODE_INST_STORE) { 602981ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Val, ValTypeID, CurBB)) 603081ad6265SDimitry Andric return error("Invalid record"); 603181ad6265SDimitry Andric } else { 603281ad6265SDimitry Andric ValTypeID = getContainedTypeID(PtrTypeID); 603381ad6265SDimitry Andric if (popValue(Record, OpNum, NextValueNo, getTypeByID(ValTypeID), 603481ad6265SDimitry Andric ValTypeID, Val, CurBB)) 603581ad6265SDimitry Andric return error("Invalid record"); 603681ad6265SDimitry Andric } 603781ad6265SDimitry Andric 603881ad6265SDimitry Andric if (OpNum + 2 != Record.size()) 6039*0b57cec5SDimitry Andric return error("Invalid record"); 6040*0b57cec5SDimitry Andric 6041*0b57cec5SDimitry Andric if (Error Err = typeCheckLoadStoreInst(Val->getType(), Ptr->getType())) 6042*0b57cec5SDimitry Andric return Err; 60438bcb0991SDimitry Andric MaybeAlign Align; 6044*0b57cec5SDimitry Andric if (Error Err = parseAlignmentValue(Record[OpNum], Align)) 6045*0b57cec5SDimitry Andric return Err; 60465ffd83dbSDimitry Andric SmallPtrSet<Type *, 4> Visited; 60475ffd83dbSDimitry Andric if (!Align && !Val->getType()->isSized(&Visited)) 60485ffd83dbSDimitry Andric return error("store of unsized type"); 60495ffd83dbSDimitry Andric if (!Align) 60505ffd83dbSDimitry Andric Align = TheModule->getDataLayout().getABITypeAlign(Val->getType()); 60515ffd83dbSDimitry Andric I = new StoreInst(Val, Ptr, Record[OpNum + 1], *Align); 6052*0b57cec5SDimitry Andric InstructionList.push_back(I); 6053*0b57cec5SDimitry Andric break; 6054*0b57cec5SDimitry Andric } 6055*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_STOREATOMIC: 6056*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_STOREATOMIC_OLD: { 6057*0b57cec5SDimitry Andric // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, ssid] 6058*0b57cec5SDimitry Andric unsigned OpNum = 0; 6059*0b57cec5SDimitry Andric Value *Val, *Ptr; 606081ad6265SDimitry Andric unsigned PtrTypeID, ValTypeID; 606181ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, PtrTypeID, CurBB) || 606281ad6265SDimitry Andric !isa<PointerType>(Ptr->getType())) 606381ad6265SDimitry Andric return error("Invalid record"); 606481ad6265SDimitry Andric if (BitCode == bitc::FUNC_CODE_INST_STOREATOMIC) { 606581ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Val, ValTypeID, CurBB)) 606681ad6265SDimitry Andric return error("Invalid record"); 606781ad6265SDimitry Andric } else { 606881ad6265SDimitry Andric ValTypeID = getContainedTypeID(PtrTypeID); 606981ad6265SDimitry Andric if (popValue(Record, OpNum, NextValueNo, getTypeByID(ValTypeID), 607081ad6265SDimitry Andric ValTypeID, Val, CurBB)) 607181ad6265SDimitry Andric return error("Invalid record"); 607281ad6265SDimitry Andric } 607381ad6265SDimitry Andric 607481ad6265SDimitry Andric if (OpNum + 4 != Record.size()) 6075*0b57cec5SDimitry Andric return error("Invalid record"); 6076*0b57cec5SDimitry Andric 6077*0b57cec5SDimitry Andric if (Error Err = typeCheckLoadStoreInst(Val->getType(), Ptr->getType())) 6078*0b57cec5SDimitry Andric return Err; 6079*0b57cec5SDimitry Andric AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); 6080*0b57cec5SDimitry Andric if (Ordering == AtomicOrdering::NotAtomic || 6081*0b57cec5SDimitry Andric Ordering == AtomicOrdering::Acquire || 6082*0b57cec5SDimitry Andric Ordering == AtomicOrdering::AcquireRelease) 6083*0b57cec5SDimitry Andric return error("Invalid record"); 6084*0b57cec5SDimitry Andric SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]); 6085*0b57cec5SDimitry Andric if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0) 6086*0b57cec5SDimitry Andric return error("Invalid record"); 6087*0b57cec5SDimitry Andric 60888bcb0991SDimitry Andric MaybeAlign Align; 6089*0b57cec5SDimitry Andric if (Error Err = parseAlignmentValue(Record[OpNum], Align)) 6090*0b57cec5SDimitry Andric return Err; 60915ffd83dbSDimitry Andric if (!Align) 60925ffd83dbSDimitry Andric return error("Alignment missing from atomic store"); 60935ffd83dbSDimitry Andric I = new StoreInst(Val, Ptr, Record[OpNum + 1], *Align, Ordering, SSID); 6094*0b57cec5SDimitry Andric InstructionList.push_back(I); 6095*0b57cec5SDimitry Andric break; 6096*0b57cec5SDimitry Andric } 6097e8d8bef9SDimitry Andric case bitc::FUNC_CODE_INST_CMPXCHG_OLD: { 6098e8d8bef9SDimitry Andric // CMPXCHG_OLD: [ptrty, ptr, cmp, val, vol, ordering, synchscope, 6099e8d8bef9SDimitry Andric // failure_ordering?, weak?] 6100e8d8bef9SDimitry Andric const size_t NumRecords = Record.size(); 6101*0b57cec5SDimitry Andric unsigned OpNum = 0; 6102e8d8bef9SDimitry Andric Value *Ptr = nullptr; 610381ad6265SDimitry Andric unsigned PtrTypeID; 610481ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, PtrTypeID, CurBB)) 6105*0b57cec5SDimitry Andric return error("Invalid record"); 6106*0b57cec5SDimitry Andric 6107*0b57cec5SDimitry Andric if (!isa<PointerType>(Ptr->getType())) 6108*0b57cec5SDimitry Andric return error("Cmpxchg operand is not a pointer type"); 6109*0b57cec5SDimitry Andric 6110e8d8bef9SDimitry Andric Value *Cmp = nullptr; 611181ad6265SDimitry Andric unsigned CmpTypeID = getContainedTypeID(PtrTypeID); 611281ad6265SDimitry Andric if (popValue(Record, OpNum, NextValueNo, getTypeByID(CmpTypeID), 611381ad6265SDimitry Andric CmpTypeID, Cmp, CurBB)) 6114*0b57cec5SDimitry Andric return error("Invalid record"); 6115e8d8bef9SDimitry Andric 6116e8d8bef9SDimitry Andric Value *New = nullptr; 611781ad6265SDimitry Andric if (popValue(Record, OpNum, NextValueNo, Cmp->getType(), CmpTypeID, 611881ad6265SDimitry Andric New, CurBB) || 6119e8d8bef9SDimitry Andric NumRecords < OpNum + 3 || NumRecords > OpNum + 5) 6120*0b57cec5SDimitry Andric return error("Invalid record"); 6121*0b57cec5SDimitry Andric 6122e8d8bef9SDimitry Andric const AtomicOrdering SuccessOrdering = 6123e8d8bef9SDimitry Andric getDecodedOrdering(Record[OpNum + 1]); 6124*0b57cec5SDimitry Andric if (SuccessOrdering == AtomicOrdering::NotAtomic || 6125*0b57cec5SDimitry Andric SuccessOrdering == AtomicOrdering::Unordered) 6126*0b57cec5SDimitry Andric return error("Invalid record"); 6127e8d8bef9SDimitry Andric 6128e8d8bef9SDimitry Andric const SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 2]); 6129*0b57cec5SDimitry Andric 6130*0b57cec5SDimitry Andric if (Error Err = typeCheckLoadStoreInst(Cmp->getType(), Ptr->getType())) 6131*0b57cec5SDimitry Andric return Err; 6132*0b57cec5SDimitry Andric 6133e8d8bef9SDimitry Andric const AtomicOrdering FailureOrdering = 6134e8d8bef9SDimitry Andric NumRecords < 7 6135e8d8bef9SDimitry Andric ? AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering) 6136e8d8bef9SDimitry Andric : getDecodedOrdering(Record[OpNum + 3]); 6137e8d8bef9SDimitry Andric 6138fe6060f1SDimitry Andric if (FailureOrdering == AtomicOrdering::NotAtomic || 6139fe6060f1SDimitry Andric FailureOrdering == AtomicOrdering::Unordered) 6140fe6060f1SDimitry Andric return error("Invalid record"); 6141fe6060f1SDimitry Andric 6142e8d8bef9SDimitry Andric const Align Alignment( 61435ffd83dbSDimitry Andric TheModule->getDataLayout().getTypeStoreSize(Cmp->getType())); 6144e8d8bef9SDimitry Andric 61455ffd83dbSDimitry Andric I = new AtomicCmpXchgInst(Ptr, Cmp, New, Alignment, SuccessOrdering, 61465ffd83dbSDimitry Andric FailureOrdering, SSID); 6147*0b57cec5SDimitry Andric cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]); 6148*0b57cec5SDimitry Andric 6149e8d8bef9SDimitry Andric if (NumRecords < 8) { 6150*0b57cec5SDimitry Andric // Before weak cmpxchgs existed, the instruction simply returned the 6151*0b57cec5SDimitry Andric // value loaded from memory, so bitcode files from that era will be 6152*0b57cec5SDimitry Andric // expecting the first component of a modern cmpxchg. 6153bdd1243dSDimitry Andric I->insertInto(CurBB, CurBB->end()); 6154*0b57cec5SDimitry Andric I = ExtractValueInst::Create(I, 0); 615581ad6265SDimitry Andric ResTypeID = CmpTypeID; 6156*0b57cec5SDimitry Andric } else { 6157*0b57cec5SDimitry Andric cast<AtomicCmpXchgInst>(I)->setWeak(Record[OpNum + 4]); 615881ad6265SDimitry Andric unsigned I1TypeID = getVirtualTypeID(Type::getInt1Ty(Context)); 615981ad6265SDimitry Andric ResTypeID = getVirtualTypeID(I->getType(), {CmpTypeID, I1TypeID}); 6160*0b57cec5SDimitry Andric } 6161*0b57cec5SDimitry Andric 6162*0b57cec5SDimitry Andric InstructionList.push_back(I); 6163*0b57cec5SDimitry Andric break; 6164*0b57cec5SDimitry Andric } 6165e8d8bef9SDimitry Andric case bitc::FUNC_CODE_INST_CMPXCHG: { 6166e8d8bef9SDimitry Andric // CMPXCHG: [ptrty, ptr, cmp, val, vol, success_ordering, synchscope, 6167fe6060f1SDimitry Andric // failure_ordering, weak, align?] 6168e8d8bef9SDimitry Andric const size_t NumRecords = Record.size(); 6169e8d8bef9SDimitry Andric unsigned OpNum = 0; 6170e8d8bef9SDimitry Andric Value *Ptr = nullptr; 617181ad6265SDimitry Andric unsigned PtrTypeID; 617281ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, PtrTypeID, CurBB)) 6173e8d8bef9SDimitry Andric return error("Invalid record"); 6174e8d8bef9SDimitry Andric 6175e8d8bef9SDimitry Andric if (!isa<PointerType>(Ptr->getType())) 6176e8d8bef9SDimitry Andric return error("Cmpxchg operand is not a pointer type"); 6177e8d8bef9SDimitry Andric 6178e8d8bef9SDimitry Andric Value *Cmp = nullptr; 617981ad6265SDimitry Andric unsigned CmpTypeID; 618081ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Cmp, CmpTypeID, CurBB)) 6181e8d8bef9SDimitry Andric return error("Invalid record"); 6182e8d8bef9SDimitry Andric 6183e8d8bef9SDimitry Andric Value *Val = nullptr; 618481ad6265SDimitry Andric if (popValue(Record, OpNum, NextValueNo, Cmp->getType(), CmpTypeID, Val, 618581ad6265SDimitry Andric CurBB)) 6186e8d8bef9SDimitry Andric return error("Invalid record"); 6187e8d8bef9SDimitry Andric 6188fe6060f1SDimitry Andric if (NumRecords < OpNum + 3 || NumRecords > OpNum + 6) 6189fe6060f1SDimitry Andric return error("Invalid record"); 6190fe6060f1SDimitry Andric 6191fe6060f1SDimitry Andric const bool IsVol = Record[OpNum]; 6192fe6060f1SDimitry Andric 6193e8d8bef9SDimitry Andric const AtomicOrdering SuccessOrdering = 6194e8d8bef9SDimitry Andric getDecodedOrdering(Record[OpNum + 1]); 6195fe6060f1SDimitry Andric if (!AtomicCmpXchgInst::isValidSuccessOrdering(SuccessOrdering)) 6196fe6060f1SDimitry Andric return error("Invalid cmpxchg success ordering"); 6197e8d8bef9SDimitry Andric 6198e8d8bef9SDimitry Andric const SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 2]); 6199e8d8bef9SDimitry Andric 6200e8d8bef9SDimitry Andric if (Error Err = typeCheckLoadStoreInst(Cmp->getType(), Ptr->getType())) 6201e8d8bef9SDimitry Andric return Err; 6202e8d8bef9SDimitry Andric 6203e8d8bef9SDimitry Andric const AtomicOrdering FailureOrdering = 6204e8d8bef9SDimitry Andric getDecodedOrdering(Record[OpNum + 3]); 6205fe6060f1SDimitry Andric if (!AtomicCmpXchgInst::isValidFailureOrdering(FailureOrdering)) 6206fe6060f1SDimitry Andric return error("Invalid cmpxchg failure ordering"); 6207e8d8bef9SDimitry Andric 6208fe6060f1SDimitry Andric const bool IsWeak = Record[OpNum + 4]; 6209e8d8bef9SDimitry Andric 6210fe6060f1SDimitry Andric MaybeAlign Alignment; 6211fe6060f1SDimitry Andric 6212fe6060f1SDimitry Andric if (NumRecords == (OpNum + 6)) { 6213fe6060f1SDimitry Andric if (Error Err = parseAlignmentValue(Record[OpNum + 5], Alignment)) 6214fe6060f1SDimitry Andric return Err; 6215fe6060f1SDimitry Andric } 6216fe6060f1SDimitry Andric if (!Alignment) 6217fe6060f1SDimitry Andric Alignment = 6218fe6060f1SDimitry Andric Align(TheModule->getDataLayout().getTypeStoreSize(Cmp->getType())); 6219fe6060f1SDimitry Andric 6220fe6060f1SDimitry Andric I = new AtomicCmpXchgInst(Ptr, Cmp, Val, *Alignment, SuccessOrdering, 6221e8d8bef9SDimitry Andric FailureOrdering, SSID); 6222fe6060f1SDimitry Andric cast<AtomicCmpXchgInst>(I)->setVolatile(IsVol); 6223fe6060f1SDimitry Andric cast<AtomicCmpXchgInst>(I)->setWeak(IsWeak); 6224e8d8bef9SDimitry Andric 622581ad6265SDimitry Andric unsigned I1TypeID = getVirtualTypeID(Type::getInt1Ty(Context)); 622681ad6265SDimitry Andric ResTypeID = getVirtualTypeID(I->getType(), {CmpTypeID, I1TypeID}); 622781ad6265SDimitry Andric 6228e8d8bef9SDimitry Andric InstructionList.push_back(I); 6229e8d8bef9SDimitry Andric break; 6230e8d8bef9SDimitry Andric } 6231fe6060f1SDimitry Andric case bitc::FUNC_CODE_INST_ATOMICRMW_OLD: 6232*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_ATOMICRMW: { 6233fe6060f1SDimitry Andric // ATOMICRMW_OLD: [ptrty, ptr, val, op, vol, ordering, ssid, align?] 6234fe6060f1SDimitry Andric // ATOMICRMW: [ptrty, ptr, valty, val, op, vol, ordering, ssid, align?] 6235fe6060f1SDimitry Andric const size_t NumRecords = Record.size(); 6236*0b57cec5SDimitry Andric unsigned OpNum = 0; 6237fe6060f1SDimitry Andric 6238fe6060f1SDimitry Andric Value *Ptr = nullptr; 623981ad6265SDimitry Andric unsigned PtrTypeID; 624081ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, PtrTypeID, CurBB)) 6241*0b57cec5SDimitry Andric return error("Invalid record"); 6242fe6060f1SDimitry Andric 6243fe6060f1SDimitry Andric if (!isa<PointerType>(Ptr->getType())) 6244fe6060f1SDimitry Andric return error("Invalid record"); 6245fe6060f1SDimitry Andric 6246fe6060f1SDimitry Andric Value *Val = nullptr; 624781ad6265SDimitry Andric unsigned ValTypeID = InvalidTypeID; 6248fe6060f1SDimitry Andric if (BitCode == bitc::FUNC_CODE_INST_ATOMICRMW_OLD) { 624981ad6265SDimitry Andric ValTypeID = getContainedTypeID(PtrTypeID); 6250fe6060f1SDimitry Andric if (popValue(Record, OpNum, NextValueNo, 625181ad6265SDimitry Andric getTypeByID(ValTypeID), ValTypeID, Val, CurBB)) 6252fe6060f1SDimitry Andric return error("Invalid record"); 6253fe6060f1SDimitry Andric } else { 625481ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Val, ValTypeID, CurBB)) 6255fe6060f1SDimitry Andric return error("Invalid record"); 6256fe6060f1SDimitry Andric } 6257fe6060f1SDimitry Andric 6258fe6060f1SDimitry Andric if (!(NumRecords == (OpNum + 4) || NumRecords == (OpNum + 5))) 6259fe6060f1SDimitry Andric return error("Invalid record"); 6260fe6060f1SDimitry Andric 6261fe6060f1SDimitry Andric const AtomicRMWInst::BinOp Operation = 6262fe6060f1SDimitry Andric getDecodedRMWOperation(Record[OpNum]); 6263*0b57cec5SDimitry Andric if (Operation < AtomicRMWInst::FIRST_BINOP || 6264*0b57cec5SDimitry Andric Operation > AtomicRMWInst::LAST_BINOP) 6265*0b57cec5SDimitry Andric return error("Invalid record"); 6266fe6060f1SDimitry Andric 6267fe6060f1SDimitry Andric const bool IsVol = Record[OpNum + 1]; 6268fe6060f1SDimitry Andric 6269fe6060f1SDimitry Andric const AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); 6270*0b57cec5SDimitry Andric if (Ordering == AtomicOrdering::NotAtomic || 6271*0b57cec5SDimitry Andric Ordering == AtomicOrdering::Unordered) 6272*0b57cec5SDimitry Andric return error("Invalid record"); 6273fe6060f1SDimitry Andric 6274fe6060f1SDimitry Andric const SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]); 6275fe6060f1SDimitry Andric 6276fe6060f1SDimitry Andric MaybeAlign Alignment; 6277fe6060f1SDimitry Andric 6278fe6060f1SDimitry Andric if (NumRecords == (OpNum + 5)) { 6279fe6060f1SDimitry Andric if (Error Err = parseAlignmentValue(Record[OpNum + 4], Alignment)) 6280fe6060f1SDimitry Andric return Err; 6281fe6060f1SDimitry Andric } 6282fe6060f1SDimitry Andric 6283fe6060f1SDimitry Andric if (!Alignment) 6284fe6060f1SDimitry Andric Alignment = 6285fe6060f1SDimitry Andric Align(TheModule->getDataLayout().getTypeStoreSize(Val->getType())); 6286fe6060f1SDimitry Andric 6287fe6060f1SDimitry Andric I = new AtomicRMWInst(Operation, Ptr, Val, *Alignment, Ordering, SSID); 628881ad6265SDimitry Andric ResTypeID = ValTypeID; 6289fe6060f1SDimitry Andric cast<AtomicRMWInst>(I)->setVolatile(IsVol); 6290fe6060f1SDimitry Andric 6291*0b57cec5SDimitry Andric InstructionList.push_back(I); 6292*0b57cec5SDimitry Andric break; 6293*0b57cec5SDimitry Andric } 6294*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, ssid] 6295*0b57cec5SDimitry Andric if (2 != Record.size()) 6296*0b57cec5SDimitry Andric return error("Invalid record"); 6297*0b57cec5SDimitry Andric AtomicOrdering Ordering = getDecodedOrdering(Record[0]); 6298*0b57cec5SDimitry Andric if (Ordering == AtomicOrdering::NotAtomic || 6299*0b57cec5SDimitry Andric Ordering == AtomicOrdering::Unordered || 6300*0b57cec5SDimitry Andric Ordering == AtomicOrdering::Monotonic) 6301*0b57cec5SDimitry Andric return error("Invalid record"); 6302*0b57cec5SDimitry Andric SyncScope::ID SSID = getDecodedSyncScopeID(Record[1]); 6303*0b57cec5SDimitry Andric I = new FenceInst(Context, Ordering, SSID); 6304*0b57cec5SDimitry Andric InstructionList.push_back(I); 6305*0b57cec5SDimitry Andric break; 6306*0b57cec5SDimitry Andric } 6307*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_CALL: { 6308*0b57cec5SDimitry Andric // CALL: [paramattrs, cc, fmf, fnty, fnid, arg0, arg1...] 6309*0b57cec5SDimitry Andric if (Record.size() < 3) 6310*0b57cec5SDimitry Andric return error("Invalid record"); 6311*0b57cec5SDimitry Andric 6312*0b57cec5SDimitry Andric unsigned OpNum = 0; 6313*0b57cec5SDimitry Andric AttributeList PAL = getAttributes(Record[OpNum++]); 6314*0b57cec5SDimitry Andric unsigned CCInfo = Record[OpNum++]; 6315*0b57cec5SDimitry Andric 6316*0b57cec5SDimitry Andric FastMathFlags FMF; 6317*0b57cec5SDimitry Andric if ((CCInfo >> bitc::CALL_FMF) & 1) { 6318*0b57cec5SDimitry Andric FMF = getDecodedFastMathFlags(Record[OpNum++]); 6319*0b57cec5SDimitry Andric if (!FMF.any()) 6320*0b57cec5SDimitry Andric return error("Fast math flags indicator set for call with no FMF"); 6321*0b57cec5SDimitry Andric } 6322*0b57cec5SDimitry Andric 632381ad6265SDimitry Andric unsigned FTyID = InvalidTypeID; 6324*0b57cec5SDimitry Andric FunctionType *FTy = nullptr; 6325*0b57cec5SDimitry Andric if ((CCInfo >> bitc::CALL_EXPLICIT_TYPE) & 1) { 632681ad6265SDimitry Andric FTyID = Record[OpNum++]; 632781ad6265SDimitry Andric FTy = dyn_cast_or_null<FunctionType>(getTypeByID(FTyID)); 6328fe6060f1SDimitry Andric if (!FTy) 6329*0b57cec5SDimitry Andric return error("Explicit call type is not a function type"); 6330*0b57cec5SDimitry Andric } 6331*0b57cec5SDimitry Andric 6332*0b57cec5SDimitry Andric Value *Callee; 633381ad6265SDimitry Andric unsigned CalleeTypeID; 633481ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Callee, CalleeTypeID, 633581ad6265SDimitry Andric CurBB)) 6336*0b57cec5SDimitry Andric return error("Invalid record"); 6337*0b57cec5SDimitry Andric 6338*0b57cec5SDimitry Andric PointerType *OpTy = dyn_cast<PointerType>(Callee->getType()); 6339*0b57cec5SDimitry Andric if (!OpTy) 6340*0b57cec5SDimitry Andric return error("Callee is not a pointer type"); 6341*0b57cec5SDimitry Andric if (!FTy) { 634281ad6265SDimitry Andric FTyID = getContainedTypeID(CalleeTypeID); 634381ad6265SDimitry Andric FTy = dyn_cast_or_null<FunctionType>(getTypeByID(FTyID)); 6344fe6060f1SDimitry Andric if (!FTy) 6345*0b57cec5SDimitry Andric return error("Callee is not of pointer to function type"); 6346fe6060f1SDimitry Andric } else if (!OpTy->isOpaqueOrPointeeTypeMatches(FTy)) 6347*0b57cec5SDimitry Andric return error("Explicit call type does not match pointee type of " 6348*0b57cec5SDimitry Andric "callee operand"); 6349*0b57cec5SDimitry Andric if (Record.size() < FTy->getNumParams() + OpNum) 6350*0b57cec5SDimitry Andric return error("Insufficient operands to call"); 6351*0b57cec5SDimitry Andric 6352*0b57cec5SDimitry Andric SmallVector<Value*, 16> Args; 635381ad6265SDimitry Andric SmallVector<unsigned, 16> ArgTyIDs; 6354*0b57cec5SDimitry Andric // Read the fixed params. 6355*0b57cec5SDimitry Andric for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { 635681ad6265SDimitry Andric unsigned ArgTyID = getContainedTypeID(FTyID, i + 1); 6357*0b57cec5SDimitry Andric if (FTy->getParamType(i)->isLabelTy()) 6358*0b57cec5SDimitry Andric Args.push_back(getBasicBlock(Record[OpNum])); 6359*0b57cec5SDimitry Andric else 6360*0b57cec5SDimitry Andric Args.push_back(getValue(Record, OpNum, NextValueNo, 636181ad6265SDimitry Andric FTy->getParamType(i), ArgTyID, CurBB)); 636281ad6265SDimitry Andric ArgTyIDs.push_back(ArgTyID); 6363*0b57cec5SDimitry Andric if (!Args.back()) 6364*0b57cec5SDimitry Andric return error("Invalid record"); 6365*0b57cec5SDimitry Andric } 6366*0b57cec5SDimitry Andric 6367*0b57cec5SDimitry Andric // Read type/value pairs for varargs params. 6368*0b57cec5SDimitry Andric if (!FTy->isVarArg()) { 6369*0b57cec5SDimitry Andric if (OpNum != Record.size()) 6370*0b57cec5SDimitry Andric return error("Invalid record"); 6371*0b57cec5SDimitry Andric } else { 6372*0b57cec5SDimitry Andric while (OpNum != Record.size()) { 6373*0b57cec5SDimitry Andric Value *Op; 637481ad6265SDimitry Andric unsigned OpTypeID; 637581ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB)) 6376*0b57cec5SDimitry Andric return error("Invalid record"); 6377*0b57cec5SDimitry Andric Args.push_back(Op); 637881ad6265SDimitry Andric ArgTyIDs.push_back(OpTypeID); 6379*0b57cec5SDimitry Andric } 6380*0b57cec5SDimitry Andric } 6381*0b57cec5SDimitry Andric 638281ad6265SDimitry Andric // Upgrade the bundles if needed. 638381ad6265SDimitry Andric if (!OperandBundles.empty()) 638481ad6265SDimitry Andric UpgradeOperandBundles(OperandBundles); 638581ad6265SDimitry Andric 6386*0b57cec5SDimitry Andric I = CallInst::Create(FTy, Callee, Args, OperandBundles); 638781ad6265SDimitry Andric ResTypeID = getContainedTypeID(FTyID); 6388*0b57cec5SDimitry Andric OperandBundles.clear(); 6389*0b57cec5SDimitry Andric InstructionList.push_back(I); 6390*0b57cec5SDimitry Andric cast<CallInst>(I)->setCallingConv( 6391*0b57cec5SDimitry Andric static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV)); 6392*0b57cec5SDimitry Andric CallInst::TailCallKind TCK = CallInst::TCK_None; 6393*0b57cec5SDimitry Andric if (CCInfo & 1 << bitc::CALL_TAIL) 6394*0b57cec5SDimitry Andric TCK = CallInst::TCK_Tail; 6395*0b57cec5SDimitry Andric if (CCInfo & (1 << bitc::CALL_MUSTTAIL)) 6396*0b57cec5SDimitry Andric TCK = CallInst::TCK_MustTail; 6397*0b57cec5SDimitry Andric if (CCInfo & (1 << bitc::CALL_NOTAIL)) 6398*0b57cec5SDimitry Andric TCK = CallInst::TCK_NoTail; 6399*0b57cec5SDimitry Andric cast<CallInst>(I)->setTailCallKind(TCK); 6400*0b57cec5SDimitry Andric cast<CallInst>(I)->setAttributes(PAL); 640181ad6265SDimitry Andric if (Error Err = propagateAttributeTypes(cast<CallBase>(I), ArgTyIDs)) { 640281ad6265SDimitry Andric I->deleteValue(); 640381ad6265SDimitry Andric return Err; 640481ad6265SDimitry Andric } 6405*0b57cec5SDimitry Andric if (FMF.any()) { 6406*0b57cec5SDimitry Andric if (!isa<FPMathOperator>(I)) 6407*0b57cec5SDimitry Andric return error("Fast-math-flags specified for call without " 6408*0b57cec5SDimitry Andric "floating-point scalar or vector return type"); 6409*0b57cec5SDimitry Andric I->setFastMathFlags(FMF); 6410*0b57cec5SDimitry Andric } 6411*0b57cec5SDimitry Andric break; 6412*0b57cec5SDimitry Andric } 6413*0b57cec5SDimitry Andric case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty] 6414*0b57cec5SDimitry Andric if (Record.size() < 3) 6415*0b57cec5SDimitry Andric return error("Invalid record"); 641681ad6265SDimitry Andric unsigned OpTyID = Record[0]; 641781ad6265SDimitry Andric Type *OpTy = getTypeByID(OpTyID); 641881ad6265SDimitry Andric Value *Op = getValue(Record, 1, NextValueNo, OpTy, OpTyID, CurBB); 641981ad6265SDimitry Andric ResTypeID = Record[2]; 642081ad6265SDimitry Andric Type *ResTy = getTypeByID(ResTypeID); 6421*0b57cec5SDimitry Andric if (!OpTy || !Op || !ResTy) 6422*0b57cec5SDimitry Andric return error("Invalid record"); 6423*0b57cec5SDimitry Andric I = new VAArgInst(Op, ResTy); 6424*0b57cec5SDimitry Andric InstructionList.push_back(I); 6425*0b57cec5SDimitry Andric break; 6426*0b57cec5SDimitry Andric } 6427*0b57cec5SDimitry Andric 6428*0b57cec5SDimitry Andric case bitc::FUNC_CODE_OPERAND_BUNDLE: { 6429*0b57cec5SDimitry Andric // A call or an invoke can be optionally prefixed with some variable 6430*0b57cec5SDimitry Andric // number of operand bundle blocks. These blocks are read into 6431*0b57cec5SDimitry Andric // OperandBundles and consumed at the next call or invoke instruction. 6432*0b57cec5SDimitry Andric 6433e8d8bef9SDimitry Andric if (Record.empty() || Record[0] >= BundleTags.size()) 6434*0b57cec5SDimitry Andric return error("Invalid record"); 6435*0b57cec5SDimitry Andric 6436*0b57cec5SDimitry Andric std::vector<Value *> Inputs; 6437*0b57cec5SDimitry Andric 6438*0b57cec5SDimitry Andric unsigned OpNum = 1; 6439*0b57cec5SDimitry Andric while (OpNum != Record.size()) { 6440*0b57cec5SDimitry Andric Value *Op; 644181ad6265SDimitry Andric unsigned OpTypeID; 644281ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB)) 6443*0b57cec5SDimitry Andric return error("Invalid record"); 6444*0b57cec5SDimitry Andric Inputs.push_back(Op); 6445*0b57cec5SDimitry Andric } 6446*0b57cec5SDimitry Andric 6447*0b57cec5SDimitry Andric OperandBundles.emplace_back(BundleTags[Record[0]], std::move(Inputs)); 6448*0b57cec5SDimitry Andric continue; 6449*0b57cec5SDimitry Andric } 6450480093f4SDimitry Andric 6451480093f4SDimitry Andric case bitc::FUNC_CODE_INST_FREEZE: { // FREEZE: [opty,opval] 6452480093f4SDimitry Andric unsigned OpNum = 0; 6453480093f4SDimitry Andric Value *Op = nullptr; 645481ad6265SDimitry Andric unsigned OpTypeID; 645581ad6265SDimitry Andric if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB)) 6456480093f4SDimitry Andric return error("Invalid record"); 6457480093f4SDimitry Andric if (OpNum != Record.size()) 6458480093f4SDimitry Andric return error("Invalid record"); 6459480093f4SDimitry Andric 6460480093f4SDimitry Andric I = new FreezeInst(Op); 646181ad6265SDimitry Andric ResTypeID = OpTypeID; 6462480093f4SDimitry Andric InstructionList.push_back(I); 6463480093f4SDimitry Andric break; 6464480093f4SDimitry Andric } 6465*0b57cec5SDimitry Andric } 6466*0b57cec5SDimitry Andric 6467*0b57cec5SDimitry Andric // Add instruction to end of current BB. If there is no current BB, reject 6468*0b57cec5SDimitry Andric // this file. 6469*0b57cec5SDimitry Andric if (!CurBB) { 6470*0b57cec5SDimitry Andric I->deleteValue(); 6471*0b57cec5SDimitry Andric return error("Invalid instruction with no BB"); 6472*0b57cec5SDimitry Andric } 6473*0b57cec5SDimitry Andric if (!OperandBundles.empty()) { 6474*0b57cec5SDimitry Andric I->deleteValue(); 6475*0b57cec5SDimitry Andric return error("Operand bundles found with no consumer"); 6476*0b57cec5SDimitry Andric } 6477bdd1243dSDimitry Andric I->insertInto(CurBB, CurBB->end()); 6478*0b57cec5SDimitry Andric 6479*0b57cec5SDimitry Andric // If this was a terminator instruction, move to the next block. 6480*0b57cec5SDimitry Andric if (I->isTerminator()) { 6481*0b57cec5SDimitry Andric ++CurBBNo; 6482*0b57cec5SDimitry Andric CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr; 6483*0b57cec5SDimitry Andric } 6484*0b57cec5SDimitry Andric 6485*0b57cec5SDimitry Andric // Non-void values get registered in the value table for future use. 648681ad6265SDimitry Andric if (!I->getType()->isVoidTy()) { 648781ad6265SDimitry Andric assert(I->getType() == getTypeByID(ResTypeID) && 648881ad6265SDimitry Andric "Incorrect result type ID"); 648981ad6265SDimitry Andric if (Error Err = ValueList.assignValue(NextValueNo++, I, ResTypeID)) 649081ad6265SDimitry Andric return Err; 649181ad6265SDimitry Andric } 6492*0b57cec5SDimitry Andric } 6493*0b57cec5SDimitry Andric 6494*0b57cec5SDimitry Andric OutOfRecordLoop: 6495*0b57cec5SDimitry Andric 6496*0b57cec5SDimitry Andric if (!OperandBundles.empty()) 6497*0b57cec5SDimitry Andric return error("Operand bundles found with no consumer"); 6498*0b57cec5SDimitry Andric 6499*0b57cec5SDimitry Andric // Check the function list for unresolved values. 6500*0b57cec5SDimitry Andric if (Argument *A = dyn_cast<Argument>(ValueList.back())) { 6501*0b57cec5SDimitry Andric if (!A->getParent()) { 6502*0b57cec5SDimitry Andric // We found at least one unresolved value. Nuke them all to avoid leaks. 6503*0b57cec5SDimitry Andric for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){ 6504*0b57cec5SDimitry Andric if ((A = dyn_cast_or_null<Argument>(ValueList[i])) && !A->getParent()) { 6505bdd1243dSDimitry Andric A->replaceAllUsesWith(PoisonValue::get(A->getType())); 6506*0b57cec5SDimitry Andric delete A; 6507*0b57cec5SDimitry Andric } 6508*0b57cec5SDimitry Andric } 6509*0b57cec5SDimitry Andric return error("Never resolved value found in function"); 6510*0b57cec5SDimitry Andric } 6511*0b57cec5SDimitry Andric } 6512*0b57cec5SDimitry Andric 6513*0b57cec5SDimitry Andric // Unexpected unresolved metadata about to be dropped. 6514*0b57cec5SDimitry Andric if (MDLoader->hasFwdRefs()) 6515*0b57cec5SDimitry Andric return error("Invalid function metadata: outgoing forward refs"); 6516*0b57cec5SDimitry Andric 651781ad6265SDimitry Andric if (PhiConstExprBB) 651881ad6265SDimitry Andric PhiConstExprBB->eraseFromParent(); 651981ad6265SDimitry Andric 652081ad6265SDimitry Andric for (const auto &Pair : ConstExprEdgeBBs) { 652181ad6265SDimitry Andric BasicBlock *From = Pair.first.first; 652281ad6265SDimitry Andric BasicBlock *To = Pair.first.second; 652381ad6265SDimitry Andric BasicBlock *EdgeBB = Pair.second; 652481ad6265SDimitry Andric BranchInst::Create(To, EdgeBB); 652581ad6265SDimitry Andric From->getTerminator()->replaceSuccessorWith(To, EdgeBB); 652681ad6265SDimitry Andric To->replacePhiUsesWith(From, EdgeBB); 652781ad6265SDimitry Andric EdgeBB->moveBefore(To); 652881ad6265SDimitry Andric } 652981ad6265SDimitry Andric 6530*0b57cec5SDimitry Andric // Trim the value list down to the size it was before we parsed this function. 6531*0b57cec5SDimitry Andric ValueList.shrinkTo(ModuleValueListSize); 6532*0b57cec5SDimitry Andric MDLoader->shrinkTo(ModuleMDLoaderSize); 6533*0b57cec5SDimitry Andric std::vector<BasicBlock*>().swap(FunctionBBs); 6534*0b57cec5SDimitry Andric return Error::success(); 6535*0b57cec5SDimitry Andric } 6536*0b57cec5SDimitry Andric 6537*0b57cec5SDimitry Andric /// Find the function body in the bitcode stream 6538*0b57cec5SDimitry Andric Error BitcodeReader::findFunctionInStream( 6539*0b57cec5SDimitry Andric Function *F, 6540*0b57cec5SDimitry Andric DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) { 6541*0b57cec5SDimitry Andric while (DeferredFunctionInfoIterator->second == 0) { 6542*0b57cec5SDimitry Andric // This is the fallback handling for the old format bitcode that 6543*0b57cec5SDimitry Andric // didn't contain the function index in the VST, or when we have 6544*0b57cec5SDimitry Andric // an anonymous function which would not have a VST entry. 6545*0b57cec5SDimitry Andric // Assert that we have one of those two cases. 6546*0b57cec5SDimitry Andric assert(VSTOffset == 0 || !F->hasName()); 6547*0b57cec5SDimitry Andric // Parse the next body in the stream and set its position in the 6548*0b57cec5SDimitry Andric // DeferredFunctionInfo map. 6549*0b57cec5SDimitry Andric if (Error Err = rememberAndSkipFunctionBodies()) 6550*0b57cec5SDimitry Andric return Err; 6551*0b57cec5SDimitry Andric } 6552*0b57cec5SDimitry Andric return Error::success(); 6553*0b57cec5SDimitry Andric } 6554*0b57cec5SDimitry Andric 6555*0b57cec5SDimitry Andric SyncScope::ID BitcodeReader::getDecodedSyncScopeID(unsigned Val) { 6556*0b57cec5SDimitry Andric if (Val == SyncScope::SingleThread || Val == SyncScope::System) 6557*0b57cec5SDimitry Andric return SyncScope::ID(Val); 6558*0b57cec5SDimitry Andric if (Val >= SSIDs.size()) 6559*0b57cec5SDimitry Andric return SyncScope::System; // Map unknown synchronization scopes to system. 6560*0b57cec5SDimitry Andric return SSIDs[Val]; 6561*0b57cec5SDimitry Andric } 6562*0b57cec5SDimitry Andric 6563*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 6564*0b57cec5SDimitry Andric // GVMaterializer implementation 6565*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 6566*0b57cec5SDimitry Andric 6567*0b57cec5SDimitry Andric Error BitcodeReader::materialize(GlobalValue *GV) { 6568*0b57cec5SDimitry Andric Function *F = dyn_cast<Function>(GV); 6569*0b57cec5SDimitry Andric // If it's not a function or is already material, ignore the request. 6570*0b57cec5SDimitry Andric if (!F || !F->isMaterializable()) 6571*0b57cec5SDimitry Andric return Error::success(); 6572*0b57cec5SDimitry Andric 6573*0b57cec5SDimitry Andric DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F); 6574*0b57cec5SDimitry Andric assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!"); 6575*0b57cec5SDimitry Andric // If its position is recorded as 0, its body is somewhere in the stream 6576*0b57cec5SDimitry Andric // but we haven't seen it yet. 6577*0b57cec5SDimitry Andric if (DFII->second == 0) 6578*0b57cec5SDimitry Andric if (Error Err = findFunctionInStream(F, DFII)) 6579*0b57cec5SDimitry Andric return Err; 6580*0b57cec5SDimitry Andric 6581*0b57cec5SDimitry Andric // Materialize metadata before parsing any function bodies. 6582*0b57cec5SDimitry Andric if (Error Err = materializeMetadata()) 6583*0b57cec5SDimitry Andric return Err; 6584*0b57cec5SDimitry Andric 6585*0b57cec5SDimitry Andric // Move the bit stream to the saved position of the deferred function body. 6586*0b57cec5SDimitry Andric if (Error JumpFailed = Stream.JumpToBit(DFII->second)) 6587*0b57cec5SDimitry Andric return JumpFailed; 6588*0b57cec5SDimitry Andric if (Error Err = parseFunctionBody(F)) 6589*0b57cec5SDimitry Andric return Err; 6590*0b57cec5SDimitry Andric F->setIsMaterializable(false); 6591*0b57cec5SDimitry Andric 6592*0b57cec5SDimitry Andric if (StripDebugInfo) 6593*0b57cec5SDimitry Andric stripDebugInfo(*F); 6594*0b57cec5SDimitry Andric 6595*0b57cec5SDimitry Andric // Upgrade any old intrinsic calls in the function. 6596*0b57cec5SDimitry Andric for (auto &I : UpgradedIntrinsics) { 6597349cc55cSDimitry Andric for (User *U : llvm::make_early_inc_range(I.first->materialized_users())) 6598*0b57cec5SDimitry Andric if (CallInst *CI = dyn_cast<CallInst>(U)) 6599*0b57cec5SDimitry Andric UpgradeIntrinsicCall(CI, I.second); 6600*0b57cec5SDimitry Andric } 6601*0b57cec5SDimitry Andric 6602*0b57cec5SDimitry Andric // Finish fn->subprogram upgrade for materialized functions. 6603*0b57cec5SDimitry Andric if (DISubprogram *SP = MDLoader->lookupSubprogramForFunction(F)) 6604*0b57cec5SDimitry Andric F->setSubprogram(SP); 6605*0b57cec5SDimitry Andric 6606*0b57cec5SDimitry Andric // Check if the TBAA Metadata are valid, otherwise we will need to strip them. 6607*0b57cec5SDimitry Andric if (!MDLoader->isStrippingTBAA()) { 6608*0b57cec5SDimitry Andric for (auto &I : instructions(F)) { 6609*0b57cec5SDimitry Andric MDNode *TBAA = I.getMetadata(LLVMContext::MD_tbaa); 6610*0b57cec5SDimitry Andric if (!TBAA || TBAAVerifyHelper.visitTBAAMetadata(I, TBAA)) 6611*0b57cec5SDimitry Andric continue; 6612*0b57cec5SDimitry Andric MDLoader->setStripTBAA(true); 6613*0b57cec5SDimitry Andric stripTBAA(F->getParent()); 6614*0b57cec5SDimitry Andric } 6615*0b57cec5SDimitry Andric } 6616*0b57cec5SDimitry Andric 6617e8d8bef9SDimitry Andric for (auto &I : instructions(F)) { 6618fe6060f1SDimitry Andric // "Upgrade" older incorrect branch weights by dropping them. 6619e8d8bef9SDimitry Andric if (auto *MD = I.getMetadata(LLVMContext::MD_prof)) { 6620e8d8bef9SDimitry Andric if (MD->getOperand(0) != nullptr && isa<MDString>(MD->getOperand(0))) { 6621e8d8bef9SDimitry Andric MDString *MDS = cast<MDString>(MD->getOperand(0)); 6622e8d8bef9SDimitry Andric StringRef ProfName = MDS->getString(); 6623e8d8bef9SDimitry Andric // Check consistency of !prof branch_weights metadata. 6624e8d8bef9SDimitry Andric if (!ProfName.equals("branch_weights")) 6625e8d8bef9SDimitry Andric continue; 6626e8d8bef9SDimitry Andric unsigned ExpectedNumOperands = 0; 6627e8d8bef9SDimitry Andric if (BranchInst *BI = dyn_cast<BranchInst>(&I)) 6628e8d8bef9SDimitry Andric ExpectedNumOperands = BI->getNumSuccessors(); 6629e8d8bef9SDimitry Andric else if (SwitchInst *SI = dyn_cast<SwitchInst>(&I)) 6630e8d8bef9SDimitry Andric ExpectedNumOperands = SI->getNumSuccessors(); 6631e8d8bef9SDimitry Andric else if (isa<CallInst>(&I)) 6632e8d8bef9SDimitry Andric ExpectedNumOperands = 1; 6633e8d8bef9SDimitry Andric else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(&I)) 6634e8d8bef9SDimitry Andric ExpectedNumOperands = IBI->getNumDestinations(); 6635e8d8bef9SDimitry Andric else if (isa<SelectInst>(&I)) 6636e8d8bef9SDimitry Andric ExpectedNumOperands = 2; 6637e8d8bef9SDimitry Andric else 6638e8d8bef9SDimitry Andric continue; // ignore and continue. 6639e8d8bef9SDimitry Andric 6640e8d8bef9SDimitry Andric // If branch weight doesn't match, just strip branch weight. 6641e8d8bef9SDimitry Andric if (MD->getNumOperands() != 1 + ExpectedNumOperands) 6642e8d8bef9SDimitry Andric I.setMetadata(LLVMContext::MD_prof, nullptr); 6643e8d8bef9SDimitry Andric } 6644e8d8bef9SDimitry Andric } 6645fe6060f1SDimitry Andric 6646fe6060f1SDimitry Andric // Remove incompatible attributes on function calls. 6647fe6060f1SDimitry Andric if (auto *CI = dyn_cast<CallBase>(&I)) { 6648349cc55cSDimitry Andric CI->removeRetAttrs(AttributeFuncs::typeIncompatible( 6649fe6060f1SDimitry Andric CI->getFunctionType()->getReturnType())); 6650fe6060f1SDimitry Andric 6651fe6060f1SDimitry Andric for (unsigned ArgNo = 0; ArgNo < CI->arg_size(); ++ArgNo) 6652fe6060f1SDimitry Andric CI->removeParamAttrs(ArgNo, AttributeFuncs::typeIncompatible( 6653fe6060f1SDimitry Andric CI->getArgOperand(ArgNo)->getType())); 6654fe6060f1SDimitry Andric } 6655e8d8bef9SDimitry Andric } 6656e8d8bef9SDimitry Andric 66575ffd83dbSDimitry Andric // Look for functions that rely on old function attribute behavior. 66585ffd83dbSDimitry Andric UpgradeFunctionAttributes(*F); 66595ffd83dbSDimitry Andric 6660*0b57cec5SDimitry Andric // Bring in any functions that this function forward-referenced via 6661*0b57cec5SDimitry Andric // blockaddresses. 6662*0b57cec5SDimitry Andric return materializeForwardReferencedFunctions(); 6663*0b57cec5SDimitry Andric } 6664*0b57cec5SDimitry Andric 6665*0b57cec5SDimitry Andric Error BitcodeReader::materializeModule() { 6666*0b57cec5SDimitry Andric if (Error Err = materializeMetadata()) 6667*0b57cec5SDimitry Andric return Err; 6668*0b57cec5SDimitry Andric 6669*0b57cec5SDimitry Andric // Promise to materialize all forward references. 6670*0b57cec5SDimitry Andric WillMaterializeAllForwardRefs = true; 6671*0b57cec5SDimitry Andric 6672*0b57cec5SDimitry Andric // Iterate over the module, deserializing any functions that are still on 6673*0b57cec5SDimitry Andric // disk. 6674*0b57cec5SDimitry Andric for (Function &F : *TheModule) { 6675*0b57cec5SDimitry Andric if (Error Err = materialize(&F)) 6676*0b57cec5SDimitry Andric return Err; 6677*0b57cec5SDimitry Andric } 6678*0b57cec5SDimitry Andric // At this point, if there are any function bodies, parse the rest of 6679*0b57cec5SDimitry Andric // the bits in the module past the last function block we have recorded 6680*0b57cec5SDimitry Andric // through either lazy scanning or the VST. 6681*0b57cec5SDimitry Andric if (LastFunctionBlockBit || NextUnreadBit) 6682*0b57cec5SDimitry Andric if (Error Err = parseModule(LastFunctionBlockBit > NextUnreadBit 6683*0b57cec5SDimitry Andric ? LastFunctionBlockBit 6684*0b57cec5SDimitry Andric : NextUnreadBit)) 6685*0b57cec5SDimitry Andric return Err; 6686*0b57cec5SDimitry Andric 6687*0b57cec5SDimitry Andric // Check that all block address forward references got resolved (as we 6688*0b57cec5SDimitry Andric // promised above). 6689*0b57cec5SDimitry Andric if (!BasicBlockFwdRefs.empty()) 6690*0b57cec5SDimitry Andric return error("Never resolved function from blockaddress"); 6691*0b57cec5SDimitry Andric 6692*0b57cec5SDimitry Andric // Upgrade any intrinsic calls that slipped through (should not happen!) and 6693*0b57cec5SDimitry Andric // delete the old functions to clean up. We can't do this unless the entire 6694*0b57cec5SDimitry Andric // module is materialized because there could always be another function body 6695*0b57cec5SDimitry Andric // with calls to the old function. 6696*0b57cec5SDimitry Andric for (auto &I : UpgradedIntrinsics) { 6697*0b57cec5SDimitry Andric for (auto *U : I.first->users()) { 6698*0b57cec5SDimitry Andric if (CallInst *CI = dyn_cast<CallInst>(U)) 6699*0b57cec5SDimitry Andric UpgradeIntrinsicCall(CI, I.second); 6700*0b57cec5SDimitry Andric } 6701*0b57cec5SDimitry Andric if (!I.first->use_empty()) 6702*0b57cec5SDimitry Andric I.first->replaceAllUsesWith(I.second); 6703*0b57cec5SDimitry Andric I.first->eraseFromParent(); 6704*0b57cec5SDimitry Andric } 6705*0b57cec5SDimitry Andric UpgradedIntrinsics.clear(); 6706*0b57cec5SDimitry Andric 6707*0b57cec5SDimitry Andric UpgradeDebugInfo(*TheModule); 6708*0b57cec5SDimitry Andric 6709*0b57cec5SDimitry Andric UpgradeModuleFlags(*TheModule); 6710*0b57cec5SDimitry Andric 67118bcb0991SDimitry Andric UpgradeARCRuntime(*TheModule); 6712*0b57cec5SDimitry Andric 6713*0b57cec5SDimitry Andric return Error::success(); 6714*0b57cec5SDimitry Andric } 6715*0b57cec5SDimitry Andric 6716*0b57cec5SDimitry Andric std::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const { 6717*0b57cec5SDimitry Andric return IdentifiedStructTypes; 6718*0b57cec5SDimitry Andric } 6719*0b57cec5SDimitry Andric 6720*0b57cec5SDimitry Andric ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader( 6721*0b57cec5SDimitry Andric BitstreamCursor Cursor, StringRef Strtab, ModuleSummaryIndex &TheIndex, 6722bdd1243dSDimitry Andric StringRef ModulePath, unsigned ModuleId, 6723bdd1243dSDimitry Andric std::function<bool(GlobalValue::GUID)> IsPrevailing) 6724*0b57cec5SDimitry Andric : BitcodeReaderBase(std::move(Cursor), Strtab), TheIndex(TheIndex), 6725bdd1243dSDimitry Andric ModulePath(ModulePath), ModuleId(ModuleId), IsPrevailing(IsPrevailing) {} 6726*0b57cec5SDimitry Andric 6727*0b57cec5SDimitry Andric void ModuleSummaryIndexBitcodeReader::addThisModule() { 6728*0b57cec5SDimitry Andric TheIndex.addModule(ModulePath, ModuleId); 6729*0b57cec5SDimitry Andric } 6730*0b57cec5SDimitry Andric 6731*0b57cec5SDimitry Andric ModuleSummaryIndex::ModuleInfo * 6732*0b57cec5SDimitry Andric ModuleSummaryIndexBitcodeReader::getThisModule() { 6733*0b57cec5SDimitry Andric return TheIndex.getModule(ModulePath); 6734*0b57cec5SDimitry Andric } 6735*0b57cec5SDimitry Andric 6736bdd1243dSDimitry Andric template <bool AllowNullValueInfo> 6737bdd1243dSDimitry Andric std::tuple<ValueInfo, GlobalValue::GUID, GlobalValue::GUID> 6738*0b57cec5SDimitry Andric ModuleSummaryIndexBitcodeReader::getValueInfoFromValueId(unsigned ValueId) { 6739*0b57cec5SDimitry Andric auto VGI = ValueIdToValueInfoMap[ValueId]; 6740bdd1243dSDimitry Andric // We can have a null value info for memprof callsite info records in 6741bdd1243dSDimitry Andric // distributed ThinLTO index files when the callee function summary is not 6742bdd1243dSDimitry Andric // included in the index. The bitcode writer records 0 in that case, 6743bdd1243dSDimitry Andric // and the caller of this helper will set AllowNullValueInfo to true. 6744bdd1243dSDimitry Andric assert(AllowNullValueInfo || std::get<0>(VGI)); 6745*0b57cec5SDimitry Andric return VGI; 6746*0b57cec5SDimitry Andric } 6747*0b57cec5SDimitry Andric 6748*0b57cec5SDimitry Andric void ModuleSummaryIndexBitcodeReader::setValueGUID( 6749*0b57cec5SDimitry Andric uint64_t ValueID, StringRef ValueName, GlobalValue::LinkageTypes Linkage, 6750*0b57cec5SDimitry Andric StringRef SourceFileName) { 6751*0b57cec5SDimitry Andric std::string GlobalId = 6752*0b57cec5SDimitry Andric GlobalValue::getGlobalIdentifier(ValueName, Linkage, SourceFileName); 6753*0b57cec5SDimitry Andric auto ValueGUID = GlobalValue::getGUID(GlobalId); 6754*0b57cec5SDimitry Andric auto OriginalNameID = ValueGUID; 6755*0b57cec5SDimitry Andric if (GlobalValue::isLocalLinkage(Linkage)) 6756*0b57cec5SDimitry Andric OriginalNameID = GlobalValue::getGUID(ValueName); 6757*0b57cec5SDimitry Andric if (PrintSummaryGUIDs) 6758*0b57cec5SDimitry Andric dbgs() << "GUID " << ValueGUID << "(" << OriginalNameID << ") is " 6759*0b57cec5SDimitry Andric << ValueName << "\n"; 6760*0b57cec5SDimitry Andric 6761*0b57cec5SDimitry Andric // UseStrtab is false for legacy summary formats and value names are 6762*0b57cec5SDimitry Andric // created on stack. In that case we save the name in a string saver in 6763*0b57cec5SDimitry Andric // the index so that the value name can be recorded. 6764bdd1243dSDimitry Andric ValueIdToValueInfoMap[ValueID] = std::make_tuple( 6765*0b57cec5SDimitry Andric TheIndex.getOrInsertValueInfo( 6766bdd1243dSDimitry Andric ValueGUID, UseStrtab ? ValueName : TheIndex.saveString(ValueName)), 6767bdd1243dSDimitry Andric OriginalNameID, ValueGUID); 6768*0b57cec5SDimitry Andric } 6769*0b57cec5SDimitry Andric 6770*0b57cec5SDimitry Andric // Specialized value symbol table parser used when reading module index 6771*0b57cec5SDimitry Andric // blocks where we don't actually create global values. The parsed information 6772*0b57cec5SDimitry Andric // is saved in the bitcode reader for use when later parsing summaries. 6773*0b57cec5SDimitry Andric Error ModuleSummaryIndexBitcodeReader::parseValueSymbolTable( 6774*0b57cec5SDimitry Andric uint64_t Offset, 6775*0b57cec5SDimitry Andric DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap) { 6776*0b57cec5SDimitry Andric // With a strtab the VST is not required to parse the summary. 6777*0b57cec5SDimitry Andric if (UseStrtab) 6778*0b57cec5SDimitry Andric return Error::success(); 6779*0b57cec5SDimitry Andric 6780*0b57cec5SDimitry Andric assert(Offset > 0 && "Expected non-zero VST offset"); 6781*0b57cec5SDimitry Andric Expected<uint64_t> MaybeCurrentBit = jumpToValueSymbolTable(Offset, Stream); 6782*0b57cec5SDimitry Andric if (!MaybeCurrentBit) 6783*0b57cec5SDimitry Andric return MaybeCurrentBit.takeError(); 6784*0b57cec5SDimitry Andric uint64_t CurrentBit = MaybeCurrentBit.get(); 6785*0b57cec5SDimitry Andric 6786*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) 6787*0b57cec5SDimitry Andric return Err; 6788*0b57cec5SDimitry Andric 6789*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 6790*0b57cec5SDimitry Andric 6791*0b57cec5SDimitry Andric // Read all the records for this value table. 6792*0b57cec5SDimitry Andric SmallString<128> ValueName; 6793*0b57cec5SDimitry Andric 6794*0b57cec5SDimitry Andric while (true) { 6795*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 6796*0b57cec5SDimitry Andric if (!MaybeEntry) 6797*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 6798*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 6799*0b57cec5SDimitry Andric 6800*0b57cec5SDimitry Andric switch (Entry.Kind) { 6801*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 6802*0b57cec5SDimitry Andric case BitstreamEntry::Error: 6803*0b57cec5SDimitry Andric return error("Malformed block"); 6804*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 6805*0b57cec5SDimitry Andric // Done parsing VST, jump back to wherever we came from. 6806*0b57cec5SDimitry Andric if (Error JumpFailed = Stream.JumpToBit(CurrentBit)) 6807*0b57cec5SDimitry Andric return JumpFailed; 6808*0b57cec5SDimitry Andric return Error::success(); 6809*0b57cec5SDimitry Andric case BitstreamEntry::Record: 6810*0b57cec5SDimitry Andric // The interesting case. 6811*0b57cec5SDimitry Andric break; 6812*0b57cec5SDimitry Andric } 6813*0b57cec5SDimitry Andric 6814*0b57cec5SDimitry Andric // Read a record. 6815*0b57cec5SDimitry Andric Record.clear(); 6816*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 6817*0b57cec5SDimitry Andric if (!MaybeRecord) 6818*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 6819*0b57cec5SDimitry Andric switch (MaybeRecord.get()) { 6820*0b57cec5SDimitry Andric default: // Default behavior: ignore (e.g. VST_CODE_BBENTRY records). 6821*0b57cec5SDimitry Andric break; 6822*0b57cec5SDimitry Andric case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N] 6823*0b57cec5SDimitry Andric if (convertToString(Record, 1, ValueName)) 6824*0b57cec5SDimitry Andric return error("Invalid record"); 6825*0b57cec5SDimitry Andric unsigned ValueID = Record[0]; 6826*0b57cec5SDimitry Andric assert(!SourceFileName.empty()); 6827*0b57cec5SDimitry Andric auto VLI = ValueIdToLinkageMap.find(ValueID); 6828*0b57cec5SDimitry Andric assert(VLI != ValueIdToLinkageMap.end() && 6829*0b57cec5SDimitry Andric "No linkage found for VST entry?"); 6830*0b57cec5SDimitry Andric auto Linkage = VLI->second; 6831*0b57cec5SDimitry Andric setValueGUID(ValueID, ValueName, Linkage, SourceFileName); 6832*0b57cec5SDimitry Andric ValueName.clear(); 6833*0b57cec5SDimitry Andric break; 6834*0b57cec5SDimitry Andric } 6835*0b57cec5SDimitry Andric case bitc::VST_CODE_FNENTRY: { 6836*0b57cec5SDimitry Andric // VST_CODE_FNENTRY: [valueid, offset, namechar x N] 6837*0b57cec5SDimitry Andric if (convertToString(Record, 2, ValueName)) 6838*0b57cec5SDimitry Andric return error("Invalid record"); 6839*0b57cec5SDimitry Andric unsigned ValueID = Record[0]; 6840*0b57cec5SDimitry Andric assert(!SourceFileName.empty()); 6841*0b57cec5SDimitry Andric auto VLI = ValueIdToLinkageMap.find(ValueID); 6842*0b57cec5SDimitry Andric assert(VLI != ValueIdToLinkageMap.end() && 6843*0b57cec5SDimitry Andric "No linkage found for VST entry?"); 6844*0b57cec5SDimitry Andric auto Linkage = VLI->second; 6845*0b57cec5SDimitry Andric setValueGUID(ValueID, ValueName, Linkage, SourceFileName); 6846*0b57cec5SDimitry Andric ValueName.clear(); 6847*0b57cec5SDimitry Andric break; 6848*0b57cec5SDimitry Andric } 6849*0b57cec5SDimitry Andric case bitc::VST_CODE_COMBINED_ENTRY: { 6850*0b57cec5SDimitry Andric // VST_CODE_COMBINED_ENTRY: [valueid, refguid] 6851*0b57cec5SDimitry Andric unsigned ValueID = Record[0]; 6852*0b57cec5SDimitry Andric GlobalValue::GUID RefGUID = Record[1]; 6853*0b57cec5SDimitry Andric // The "original name", which is the second value of the pair will be 6854*0b57cec5SDimitry Andric // overriden later by a FS_COMBINED_ORIGINAL_NAME in the combined index. 6855bdd1243dSDimitry Andric ValueIdToValueInfoMap[ValueID] = std::make_tuple( 6856bdd1243dSDimitry Andric TheIndex.getOrInsertValueInfo(RefGUID), RefGUID, RefGUID); 6857*0b57cec5SDimitry Andric break; 6858*0b57cec5SDimitry Andric } 6859*0b57cec5SDimitry Andric } 6860*0b57cec5SDimitry Andric } 6861*0b57cec5SDimitry Andric } 6862*0b57cec5SDimitry Andric 6863*0b57cec5SDimitry Andric // Parse just the blocks needed for building the index out of the module. 6864*0b57cec5SDimitry Andric // At the end of this routine the module Index is populated with a map 6865*0b57cec5SDimitry Andric // from global value id to GlobalValueSummary objects. 6866*0b57cec5SDimitry Andric Error ModuleSummaryIndexBitcodeReader::parseModule() { 6867*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 6868*0b57cec5SDimitry Andric return Err; 6869*0b57cec5SDimitry Andric 6870*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 6871*0b57cec5SDimitry Andric DenseMap<unsigned, GlobalValue::LinkageTypes> ValueIdToLinkageMap; 6872*0b57cec5SDimitry Andric unsigned ValueId = 0; 6873*0b57cec5SDimitry Andric 6874*0b57cec5SDimitry Andric // Read the index for this module. 6875*0b57cec5SDimitry Andric while (true) { 6876*0b57cec5SDimitry Andric Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 6877*0b57cec5SDimitry Andric if (!MaybeEntry) 6878*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 6879*0b57cec5SDimitry Andric llvm::BitstreamEntry Entry = MaybeEntry.get(); 6880*0b57cec5SDimitry Andric 6881*0b57cec5SDimitry Andric switch (Entry.Kind) { 6882*0b57cec5SDimitry Andric case BitstreamEntry::Error: 6883*0b57cec5SDimitry Andric return error("Malformed block"); 6884*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 6885*0b57cec5SDimitry Andric return Error::success(); 6886*0b57cec5SDimitry Andric 6887*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: 6888*0b57cec5SDimitry Andric switch (Entry.ID) { 6889*0b57cec5SDimitry Andric default: // Skip unknown content. 6890*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 6891*0b57cec5SDimitry Andric return Err; 6892*0b57cec5SDimitry Andric break; 6893*0b57cec5SDimitry Andric case bitc::BLOCKINFO_BLOCK_ID: 6894*0b57cec5SDimitry Andric // Need to parse these to get abbrev ids (e.g. for VST) 689581ad6265SDimitry Andric if (Error Err = readBlockInfo()) 689681ad6265SDimitry Andric return Err; 6897*0b57cec5SDimitry Andric break; 6898*0b57cec5SDimitry Andric case bitc::VALUE_SYMTAB_BLOCK_ID: 6899*0b57cec5SDimitry Andric // Should have been parsed earlier via VSTOffset, unless there 6900*0b57cec5SDimitry Andric // is no summary section. 6901*0b57cec5SDimitry Andric assert(((SeenValueSymbolTable && VSTOffset > 0) || 6902*0b57cec5SDimitry Andric !SeenGlobalValSummary) && 6903*0b57cec5SDimitry Andric "Expected early VST parse via VSTOffset record"); 6904*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 6905*0b57cec5SDimitry Andric return Err; 6906*0b57cec5SDimitry Andric break; 6907*0b57cec5SDimitry Andric case bitc::GLOBALVAL_SUMMARY_BLOCK_ID: 6908*0b57cec5SDimitry Andric case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID: 6909*0b57cec5SDimitry Andric // Add the module if it is a per-module index (has a source file name). 6910*0b57cec5SDimitry Andric if (!SourceFileName.empty()) 6911*0b57cec5SDimitry Andric addThisModule(); 6912*0b57cec5SDimitry Andric assert(!SeenValueSymbolTable && 6913*0b57cec5SDimitry Andric "Already read VST when parsing summary block?"); 6914*0b57cec5SDimitry Andric // We might not have a VST if there were no values in the 6915*0b57cec5SDimitry Andric // summary. An empty summary block generated when we are 6916*0b57cec5SDimitry Andric // performing ThinLTO compiles so we don't later invoke 6917*0b57cec5SDimitry Andric // the regular LTO process on them. 6918*0b57cec5SDimitry Andric if (VSTOffset > 0) { 6919*0b57cec5SDimitry Andric if (Error Err = parseValueSymbolTable(VSTOffset, ValueIdToLinkageMap)) 6920*0b57cec5SDimitry Andric return Err; 6921*0b57cec5SDimitry Andric SeenValueSymbolTable = true; 6922*0b57cec5SDimitry Andric } 6923*0b57cec5SDimitry Andric SeenGlobalValSummary = true; 6924*0b57cec5SDimitry Andric if (Error Err = parseEntireSummary(Entry.ID)) 6925*0b57cec5SDimitry Andric return Err; 6926*0b57cec5SDimitry Andric break; 6927*0b57cec5SDimitry Andric case bitc::MODULE_STRTAB_BLOCK_ID: 6928*0b57cec5SDimitry Andric if (Error Err = parseModuleStringTable()) 6929*0b57cec5SDimitry Andric return Err; 6930*0b57cec5SDimitry Andric break; 6931*0b57cec5SDimitry Andric } 6932*0b57cec5SDimitry Andric continue; 6933*0b57cec5SDimitry Andric 6934*0b57cec5SDimitry Andric case BitstreamEntry::Record: { 6935*0b57cec5SDimitry Andric Record.clear(); 6936*0b57cec5SDimitry Andric Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); 6937*0b57cec5SDimitry Andric if (!MaybeBitCode) 6938*0b57cec5SDimitry Andric return MaybeBitCode.takeError(); 6939*0b57cec5SDimitry Andric switch (MaybeBitCode.get()) { 6940*0b57cec5SDimitry Andric default: 6941*0b57cec5SDimitry Andric break; // Default behavior, ignore unknown content. 6942*0b57cec5SDimitry Andric case bitc::MODULE_CODE_VERSION: { 6943*0b57cec5SDimitry Andric if (Error Err = parseVersionRecord(Record).takeError()) 6944*0b57cec5SDimitry Andric return Err; 6945*0b57cec5SDimitry Andric break; 6946*0b57cec5SDimitry Andric } 6947*0b57cec5SDimitry Andric /// MODULE_CODE_SOURCE_FILENAME: [namechar x N] 6948*0b57cec5SDimitry Andric case bitc::MODULE_CODE_SOURCE_FILENAME: { 6949*0b57cec5SDimitry Andric SmallString<128> ValueName; 6950*0b57cec5SDimitry Andric if (convertToString(Record, 0, ValueName)) 6951*0b57cec5SDimitry Andric return error("Invalid record"); 6952*0b57cec5SDimitry Andric SourceFileName = ValueName.c_str(); 6953*0b57cec5SDimitry Andric break; 6954*0b57cec5SDimitry Andric } 6955*0b57cec5SDimitry Andric /// MODULE_CODE_HASH: [5*i32] 6956*0b57cec5SDimitry Andric case bitc::MODULE_CODE_HASH: { 6957*0b57cec5SDimitry Andric if (Record.size() != 5) 6958*0b57cec5SDimitry Andric return error("Invalid hash length " + Twine(Record.size()).str()); 6959*0b57cec5SDimitry Andric auto &Hash = getThisModule()->second.second; 6960*0b57cec5SDimitry Andric int Pos = 0; 6961*0b57cec5SDimitry Andric for (auto &Val : Record) { 6962*0b57cec5SDimitry Andric assert(!(Val >> 32) && "Unexpected high bits set"); 6963*0b57cec5SDimitry Andric Hash[Pos++] = Val; 6964*0b57cec5SDimitry Andric } 6965*0b57cec5SDimitry Andric break; 6966*0b57cec5SDimitry Andric } 6967*0b57cec5SDimitry Andric /// MODULE_CODE_VSTOFFSET: [offset] 6968*0b57cec5SDimitry Andric case bitc::MODULE_CODE_VSTOFFSET: 6969e8d8bef9SDimitry Andric if (Record.empty()) 6970*0b57cec5SDimitry Andric return error("Invalid record"); 6971*0b57cec5SDimitry Andric // Note that we subtract 1 here because the offset is relative to one 6972*0b57cec5SDimitry Andric // word before the start of the identification or module block, which 6973*0b57cec5SDimitry Andric // was historically always the start of the regular bitcode header. 6974*0b57cec5SDimitry Andric VSTOffset = Record[0] - 1; 6975*0b57cec5SDimitry Andric break; 6976*0b57cec5SDimitry Andric // v1 GLOBALVAR: [pointer type, isconst, initid, linkage, ...] 6977*0b57cec5SDimitry Andric // v1 FUNCTION: [type, callingconv, isproto, linkage, ...] 6978*0b57cec5SDimitry Andric // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, ...] 6979*0b57cec5SDimitry Andric // v2: [strtab offset, strtab size, v1] 6980*0b57cec5SDimitry Andric case bitc::MODULE_CODE_GLOBALVAR: 6981*0b57cec5SDimitry Andric case bitc::MODULE_CODE_FUNCTION: 6982*0b57cec5SDimitry Andric case bitc::MODULE_CODE_ALIAS: { 6983*0b57cec5SDimitry Andric StringRef Name; 6984*0b57cec5SDimitry Andric ArrayRef<uint64_t> GVRecord; 6985*0b57cec5SDimitry Andric std::tie(Name, GVRecord) = readNameFromStrtab(Record); 6986*0b57cec5SDimitry Andric if (GVRecord.size() <= 3) 6987*0b57cec5SDimitry Andric return error("Invalid record"); 6988*0b57cec5SDimitry Andric uint64_t RawLinkage = GVRecord[3]; 6989*0b57cec5SDimitry Andric GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage); 6990*0b57cec5SDimitry Andric if (!UseStrtab) { 6991*0b57cec5SDimitry Andric ValueIdToLinkageMap[ValueId++] = Linkage; 6992*0b57cec5SDimitry Andric break; 6993*0b57cec5SDimitry Andric } 6994*0b57cec5SDimitry Andric 6995*0b57cec5SDimitry Andric setValueGUID(ValueId++, Name, Linkage, SourceFileName); 6996*0b57cec5SDimitry Andric break; 6997*0b57cec5SDimitry Andric } 6998*0b57cec5SDimitry Andric } 6999*0b57cec5SDimitry Andric } 7000*0b57cec5SDimitry Andric continue; 7001*0b57cec5SDimitry Andric } 7002*0b57cec5SDimitry Andric } 7003*0b57cec5SDimitry Andric } 7004*0b57cec5SDimitry Andric 7005*0b57cec5SDimitry Andric std::vector<ValueInfo> 7006*0b57cec5SDimitry Andric ModuleSummaryIndexBitcodeReader::makeRefList(ArrayRef<uint64_t> Record) { 7007*0b57cec5SDimitry Andric std::vector<ValueInfo> Ret; 7008*0b57cec5SDimitry Andric Ret.reserve(Record.size()); 7009*0b57cec5SDimitry Andric for (uint64_t RefValueId : Record) 7010bdd1243dSDimitry Andric Ret.push_back(std::get<0>(getValueInfoFromValueId(RefValueId))); 7011*0b57cec5SDimitry Andric return Ret; 7012*0b57cec5SDimitry Andric } 7013*0b57cec5SDimitry Andric 7014*0b57cec5SDimitry Andric std::vector<FunctionSummary::EdgeTy> 7015*0b57cec5SDimitry Andric ModuleSummaryIndexBitcodeReader::makeCallList(ArrayRef<uint64_t> Record, 7016*0b57cec5SDimitry Andric bool IsOldProfileFormat, 7017*0b57cec5SDimitry Andric bool HasProfile, bool HasRelBF) { 7018*0b57cec5SDimitry Andric std::vector<FunctionSummary::EdgeTy> Ret; 7019*0b57cec5SDimitry Andric Ret.reserve(Record.size()); 7020*0b57cec5SDimitry Andric for (unsigned I = 0, E = Record.size(); I != E; ++I) { 7021*0b57cec5SDimitry Andric CalleeInfo::HotnessType Hotness = CalleeInfo::HotnessType::Unknown; 7022*0b57cec5SDimitry Andric uint64_t RelBF = 0; 7023bdd1243dSDimitry Andric ValueInfo Callee = std::get<0>(getValueInfoFromValueId(Record[I])); 7024*0b57cec5SDimitry Andric if (IsOldProfileFormat) { 7025*0b57cec5SDimitry Andric I += 1; // Skip old callsitecount field 7026*0b57cec5SDimitry Andric if (HasProfile) 7027*0b57cec5SDimitry Andric I += 1; // Skip old profilecount field 7028*0b57cec5SDimitry Andric } else if (HasProfile) 7029*0b57cec5SDimitry Andric Hotness = static_cast<CalleeInfo::HotnessType>(Record[++I]); 7030*0b57cec5SDimitry Andric else if (HasRelBF) 7031*0b57cec5SDimitry Andric RelBF = Record[++I]; 7032*0b57cec5SDimitry Andric Ret.push_back(FunctionSummary::EdgeTy{Callee, CalleeInfo(Hotness, RelBF)}); 7033*0b57cec5SDimitry Andric } 7034*0b57cec5SDimitry Andric return Ret; 7035*0b57cec5SDimitry Andric } 7036*0b57cec5SDimitry Andric 7037*0b57cec5SDimitry Andric static void 7038*0b57cec5SDimitry Andric parseWholeProgramDevirtResolutionByArg(ArrayRef<uint64_t> Record, size_t &Slot, 7039*0b57cec5SDimitry Andric WholeProgramDevirtResolution &Wpd) { 7040*0b57cec5SDimitry Andric uint64_t ArgNum = Record[Slot++]; 7041*0b57cec5SDimitry Andric WholeProgramDevirtResolution::ByArg &B = 7042*0b57cec5SDimitry Andric Wpd.ResByArg[{Record.begin() + Slot, Record.begin() + Slot + ArgNum}]; 7043*0b57cec5SDimitry Andric Slot += ArgNum; 7044*0b57cec5SDimitry Andric 7045*0b57cec5SDimitry Andric B.TheKind = 7046*0b57cec5SDimitry Andric static_cast<WholeProgramDevirtResolution::ByArg::Kind>(Record[Slot++]); 7047*0b57cec5SDimitry Andric B.Info = Record[Slot++]; 7048*0b57cec5SDimitry Andric B.Byte = Record[Slot++]; 7049*0b57cec5SDimitry Andric B.Bit = Record[Slot++]; 7050*0b57cec5SDimitry Andric } 7051*0b57cec5SDimitry Andric 7052*0b57cec5SDimitry Andric static void parseWholeProgramDevirtResolution(ArrayRef<uint64_t> Record, 7053*0b57cec5SDimitry Andric StringRef Strtab, size_t &Slot, 7054*0b57cec5SDimitry Andric TypeIdSummary &TypeId) { 7055*0b57cec5SDimitry Andric uint64_t Id = Record[Slot++]; 7056*0b57cec5SDimitry Andric WholeProgramDevirtResolution &Wpd = TypeId.WPDRes[Id]; 7057*0b57cec5SDimitry Andric 7058*0b57cec5SDimitry Andric Wpd.TheKind = static_cast<WholeProgramDevirtResolution::Kind>(Record[Slot++]); 7059*0b57cec5SDimitry Andric Wpd.SingleImplName = {Strtab.data() + Record[Slot], 7060*0b57cec5SDimitry Andric static_cast<size_t>(Record[Slot + 1])}; 7061*0b57cec5SDimitry Andric Slot += 2; 7062*0b57cec5SDimitry Andric 7063*0b57cec5SDimitry Andric uint64_t ResByArgNum = Record[Slot++]; 7064*0b57cec5SDimitry Andric for (uint64_t I = 0; I != ResByArgNum; ++I) 7065*0b57cec5SDimitry Andric parseWholeProgramDevirtResolutionByArg(Record, Slot, Wpd); 7066*0b57cec5SDimitry Andric } 7067*0b57cec5SDimitry Andric 7068*0b57cec5SDimitry Andric static void parseTypeIdSummaryRecord(ArrayRef<uint64_t> Record, 7069*0b57cec5SDimitry Andric StringRef Strtab, 7070*0b57cec5SDimitry Andric ModuleSummaryIndex &TheIndex) { 7071*0b57cec5SDimitry Andric size_t Slot = 0; 7072*0b57cec5SDimitry Andric TypeIdSummary &TypeId = TheIndex.getOrInsertTypeIdSummary( 7073*0b57cec5SDimitry Andric {Strtab.data() + Record[Slot], static_cast<size_t>(Record[Slot + 1])}); 7074*0b57cec5SDimitry Andric Slot += 2; 7075*0b57cec5SDimitry Andric 7076*0b57cec5SDimitry Andric TypeId.TTRes.TheKind = static_cast<TypeTestResolution::Kind>(Record[Slot++]); 7077*0b57cec5SDimitry Andric TypeId.TTRes.SizeM1BitWidth = Record[Slot++]; 7078*0b57cec5SDimitry Andric TypeId.TTRes.AlignLog2 = Record[Slot++]; 7079*0b57cec5SDimitry Andric TypeId.TTRes.SizeM1 = Record[Slot++]; 7080*0b57cec5SDimitry Andric TypeId.TTRes.BitMask = Record[Slot++]; 7081*0b57cec5SDimitry Andric TypeId.TTRes.InlineBits = Record[Slot++]; 7082*0b57cec5SDimitry Andric 7083*0b57cec5SDimitry Andric while (Slot < Record.size()) 7084*0b57cec5SDimitry Andric parseWholeProgramDevirtResolution(Record, Strtab, Slot, TypeId); 7085*0b57cec5SDimitry Andric } 7086*0b57cec5SDimitry Andric 7087e8d8bef9SDimitry Andric std::vector<FunctionSummary::ParamAccess> 7088e8d8bef9SDimitry Andric ModuleSummaryIndexBitcodeReader::parseParamAccesses(ArrayRef<uint64_t> Record) { 70895ffd83dbSDimitry Andric auto ReadRange = [&]() { 70905ffd83dbSDimitry Andric APInt Lower(FunctionSummary::ParamAccess::RangeWidth, 70915ffd83dbSDimitry Andric BitcodeReader::decodeSignRotatedValue(Record.front())); 70925ffd83dbSDimitry Andric Record = Record.drop_front(); 70935ffd83dbSDimitry Andric APInt Upper(FunctionSummary::ParamAccess::RangeWidth, 70945ffd83dbSDimitry Andric BitcodeReader::decodeSignRotatedValue(Record.front())); 70955ffd83dbSDimitry Andric Record = Record.drop_front(); 70965ffd83dbSDimitry Andric ConstantRange Range{Lower, Upper}; 70975ffd83dbSDimitry Andric assert(!Range.isFullSet()); 70985ffd83dbSDimitry Andric assert(!Range.isUpperSignWrapped()); 70995ffd83dbSDimitry Andric return Range; 71005ffd83dbSDimitry Andric }; 71015ffd83dbSDimitry Andric 71025ffd83dbSDimitry Andric std::vector<FunctionSummary::ParamAccess> PendingParamAccesses; 71035ffd83dbSDimitry Andric while (!Record.empty()) { 71045ffd83dbSDimitry Andric PendingParamAccesses.emplace_back(); 71055ffd83dbSDimitry Andric FunctionSummary::ParamAccess &ParamAccess = PendingParamAccesses.back(); 71065ffd83dbSDimitry Andric ParamAccess.ParamNo = Record.front(); 71075ffd83dbSDimitry Andric Record = Record.drop_front(); 71085ffd83dbSDimitry Andric ParamAccess.Use = ReadRange(); 71095ffd83dbSDimitry Andric ParamAccess.Calls.resize(Record.front()); 71105ffd83dbSDimitry Andric Record = Record.drop_front(); 71115ffd83dbSDimitry Andric for (auto &Call : ParamAccess.Calls) { 71125ffd83dbSDimitry Andric Call.ParamNo = Record.front(); 71135ffd83dbSDimitry Andric Record = Record.drop_front(); 7114bdd1243dSDimitry Andric Call.Callee = std::get<0>(getValueInfoFromValueId(Record.front())); 71155ffd83dbSDimitry Andric Record = Record.drop_front(); 71165ffd83dbSDimitry Andric Call.Offsets = ReadRange(); 71175ffd83dbSDimitry Andric } 71185ffd83dbSDimitry Andric } 71195ffd83dbSDimitry Andric return PendingParamAccesses; 71205ffd83dbSDimitry Andric } 71215ffd83dbSDimitry Andric 7122*0b57cec5SDimitry Andric void ModuleSummaryIndexBitcodeReader::parseTypeIdCompatibleVtableInfo( 7123*0b57cec5SDimitry Andric ArrayRef<uint64_t> Record, size_t &Slot, 7124*0b57cec5SDimitry Andric TypeIdCompatibleVtableInfo &TypeId) { 7125*0b57cec5SDimitry Andric uint64_t Offset = Record[Slot++]; 7126bdd1243dSDimitry Andric ValueInfo Callee = std::get<0>(getValueInfoFromValueId(Record[Slot++])); 7127*0b57cec5SDimitry Andric TypeId.push_back({Offset, Callee}); 7128*0b57cec5SDimitry Andric } 7129*0b57cec5SDimitry Andric 7130*0b57cec5SDimitry Andric void ModuleSummaryIndexBitcodeReader::parseTypeIdCompatibleVtableSummaryRecord( 7131*0b57cec5SDimitry Andric ArrayRef<uint64_t> Record) { 7132*0b57cec5SDimitry Andric size_t Slot = 0; 7133*0b57cec5SDimitry Andric TypeIdCompatibleVtableInfo &TypeId = 7134*0b57cec5SDimitry Andric TheIndex.getOrInsertTypeIdCompatibleVtableSummary( 7135*0b57cec5SDimitry Andric {Strtab.data() + Record[Slot], 7136*0b57cec5SDimitry Andric static_cast<size_t>(Record[Slot + 1])}); 7137*0b57cec5SDimitry Andric Slot += 2; 7138*0b57cec5SDimitry Andric 7139*0b57cec5SDimitry Andric while (Slot < Record.size()) 7140*0b57cec5SDimitry Andric parseTypeIdCompatibleVtableInfo(Record, Slot, TypeId); 7141*0b57cec5SDimitry Andric } 7142*0b57cec5SDimitry Andric 7143*0b57cec5SDimitry Andric static void setSpecialRefs(std::vector<ValueInfo> &Refs, unsigned ROCnt, 7144*0b57cec5SDimitry Andric unsigned WOCnt) { 7145*0b57cec5SDimitry Andric // Readonly and writeonly refs are in the end of the refs list. 7146*0b57cec5SDimitry Andric assert(ROCnt + WOCnt <= Refs.size()); 7147*0b57cec5SDimitry Andric unsigned FirstWORef = Refs.size() - WOCnt; 7148*0b57cec5SDimitry Andric unsigned RefNo = FirstWORef - ROCnt; 7149*0b57cec5SDimitry Andric for (; RefNo < FirstWORef; ++RefNo) 7150*0b57cec5SDimitry Andric Refs[RefNo].setReadOnly(); 7151*0b57cec5SDimitry Andric for (; RefNo < Refs.size(); ++RefNo) 7152*0b57cec5SDimitry Andric Refs[RefNo].setWriteOnly(); 7153*0b57cec5SDimitry Andric } 7154*0b57cec5SDimitry Andric 7155*0b57cec5SDimitry Andric // Eagerly parse the entire summary block. This populates the GlobalValueSummary 7156*0b57cec5SDimitry Andric // objects in the index. 7157*0b57cec5SDimitry Andric Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { 7158*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(ID)) 7159*0b57cec5SDimitry Andric return Err; 7160*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 7161*0b57cec5SDimitry Andric 7162*0b57cec5SDimitry Andric // Parse version 7163*0b57cec5SDimitry Andric { 7164*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 7165*0b57cec5SDimitry Andric if (!MaybeEntry) 7166*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 7167*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 7168*0b57cec5SDimitry Andric 7169*0b57cec5SDimitry Andric if (Entry.Kind != BitstreamEntry::Record) 7170*0b57cec5SDimitry Andric return error("Invalid Summary Block: record for version expected"); 7171*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 7172*0b57cec5SDimitry Andric if (!MaybeRecord) 7173*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 7174*0b57cec5SDimitry Andric if (MaybeRecord.get() != bitc::FS_VERSION) 7175*0b57cec5SDimitry Andric return error("Invalid Summary Block: version expected"); 7176*0b57cec5SDimitry Andric } 7177*0b57cec5SDimitry Andric const uint64_t Version = Record[0]; 7178*0b57cec5SDimitry Andric const bool IsOldProfileFormat = Version == 1; 7179480093f4SDimitry Andric if (Version < 1 || Version > ModuleSummaryIndex::BitcodeSummaryVersion) 7180*0b57cec5SDimitry Andric return error("Invalid summary version " + Twine(Version) + 7181480093f4SDimitry Andric ". Version should be in the range [1-" + 7182480093f4SDimitry Andric Twine(ModuleSummaryIndex::BitcodeSummaryVersion) + 7183480093f4SDimitry Andric "]."); 7184*0b57cec5SDimitry Andric Record.clear(); 7185*0b57cec5SDimitry Andric 7186*0b57cec5SDimitry Andric // Keep around the last seen summary to be used when we see an optional 7187*0b57cec5SDimitry Andric // "OriginalName" attachement. 7188*0b57cec5SDimitry Andric GlobalValueSummary *LastSeenSummary = nullptr; 7189*0b57cec5SDimitry Andric GlobalValue::GUID LastSeenGUID = 0; 7190*0b57cec5SDimitry Andric 7191*0b57cec5SDimitry Andric // We can expect to see any number of type ID information records before 7192*0b57cec5SDimitry Andric // each function summary records; these variables store the information 7193*0b57cec5SDimitry Andric // collected so far so that it can be used to create the summary object. 7194*0b57cec5SDimitry Andric std::vector<GlobalValue::GUID> PendingTypeTests; 7195*0b57cec5SDimitry Andric std::vector<FunctionSummary::VFuncId> PendingTypeTestAssumeVCalls, 7196*0b57cec5SDimitry Andric PendingTypeCheckedLoadVCalls; 7197*0b57cec5SDimitry Andric std::vector<FunctionSummary::ConstVCall> PendingTypeTestAssumeConstVCalls, 7198*0b57cec5SDimitry Andric PendingTypeCheckedLoadConstVCalls; 71995ffd83dbSDimitry Andric std::vector<FunctionSummary::ParamAccess> PendingParamAccesses; 7200*0b57cec5SDimitry Andric 7201bdd1243dSDimitry Andric std::vector<CallsiteInfo> PendingCallsites; 7202bdd1243dSDimitry Andric std::vector<AllocInfo> PendingAllocs; 7203bdd1243dSDimitry Andric 7204*0b57cec5SDimitry Andric while (true) { 7205*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 7206*0b57cec5SDimitry Andric if (!MaybeEntry) 7207*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 7208*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 7209*0b57cec5SDimitry Andric 7210*0b57cec5SDimitry Andric switch (Entry.Kind) { 7211*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 7212*0b57cec5SDimitry Andric case BitstreamEntry::Error: 7213*0b57cec5SDimitry Andric return error("Malformed block"); 7214*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 7215*0b57cec5SDimitry Andric return Error::success(); 7216*0b57cec5SDimitry Andric case BitstreamEntry::Record: 7217*0b57cec5SDimitry Andric // The interesting case. 7218*0b57cec5SDimitry Andric break; 7219*0b57cec5SDimitry Andric } 7220*0b57cec5SDimitry Andric 7221*0b57cec5SDimitry Andric // Read a record. The record format depends on whether this 7222*0b57cec5SDimitry Andric // is a per-module index or a combined index file. In the per-module 7223*0b57cec5SDimitry Andric // case the records contain the associated value's ID for correlation 7224*0b57cec5SDimitry Andric // with VST entries. In the combined index the correlation is done 7225*0b57cec5SDimitry Andric // via the bitcode offset of the summary records (which were saved 7226*0b57cec5SDimitry Andric // in the combined index VST entries). The records also contain 7227*0b57cec5SDimitry Andric // information used for ThinLTO renaming and importing. 7228*0b57cec5SDimitry Andric Record.clear(); 7229*0b57cec5SDimitry Andric Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); 7230*0b57cec5SDimitry Andric if (!MaybeBitCode) 7231*0b57cec5SDimitry Andric return MaybeBitCode.takeError(); 7232*0b57cec5SDimitry Andric switch (unsigned BitCode = MaybeBitCode.get()) { 7233*0b57cec5SDimitry Andric default: // Default behavior: ignore. 7234*0b57cec5SDimitry Andric break; 7235*0b57cec5SDimitry Andric case bitc::FS_FLAGS: { // [flags] 72365ffd83dbSDimitry Andric TheIndex.setFlags(Record[0]); 7237*0b57cec5SDimitry Andric break; 7238*0b57cec5SDimitry Andric } 7239*0b57cec5SDimitry Andric case bitc::FS_VALUE_GUID: { // [valueid, refguid] 7240*0b57cec5SDimitry Andric uint64_t ValueID = Record[0]; 7241*0b57cec5SDimitry Andric GlobalValue::GUID RefGUID = Record[1]; 7242bdd1243dSDimitry Andric ValueIdToValueInfoMap[ValueID] = std::make_tuple( 7243bdd1243dSDimitry Andric TheIndex.getOrInsertValueInfo(RefGUID), RefGUID, RefGUID); 7244*0b57cec5SDimitry Andric break; 7245*0b57cec5SDimitry Andric } 7246*0b57cec5SDimitry Andric // FS_PERMODULE: [valueid, flags, instcount, fflags, numrefs, 7247*0b57cec5SDimitry Andric // numrefs x valueid, n x (valueid)] 7248*0b57cec5SDimitry Andric // FS_PERMODULE_PROFILE: [valueid, flags, instcount, fflags, numrefs, 7249*0b57cec5SDimitry Andric // numrefs x valueid, 7250*0b57cec5SDimitry Andric // n x (valueid, hotness)] 7251*0b57cec5SDimitry Andric // FS_PERMODULE_RELBF: [valueid, flags, instcount, fflags, numrefs, 7252*0b57cec5SDimitry Andric // numrefs x valueid, 7253*0b57cec5SDimitry Andric // n x (valueid, relblockfreq)] 7254*0b57cec5SDimitry Andric case bitc::FS_PERMODULE: 7255*0b57cec5SDimitry Andric case bitc::FS_PERMODULE_RELBF: 7256*0b57cec5SDimitry Andric case bitc::FS_PERMODULE_PROFILE: { 7257*0b57cec5SDimitry Andric unsigned ValueID = Record[0]; 7258*0b57cec5SDimitry Andric uint64_t RawFlags = Record[1]; 7259*0b57cec5SDimitry Andric unsigned InstCount = Record[2]; 7260*0b57cec5SDimitry Andric uint64_t RawFunFlags = 0; 7261*0b57cec5SDimitry Andric unsigned NumRefs = Record[3]; 7262*0b57cec5SDimitry Andric unsigned NumRORefs = 0, NumWORefs = 0; 7263*0b57cec5SDimitry Andric int RefListStartIndex = 4; 7264*0b57cec5SDimitry Andric if (Version >= 4) { 7265*0b57cec5SDimitry Andric RawFunFlags = Record[3]; 7266*0b57cec5SDimitry Andric NumRefs = Record[4]; 7267*0b57cec5SDimitry Andric RefListStartIndex = 5; 7268*0b57cec5SDimitry Andric if (Version >= 5) { 7269*0b57cec5SDimitry Andric NumRORefs = Record[5]; 7270*0b57cec5SDimitry Andric RefListStartIndex = 6; 7271*0b57cec5SDimitry Andric if (Version >= 7) { 7272*0b57cec5SDimitry Andric NumWORefs = Record[6]; 7273*0b57cec5SDimitry Andric RefListStartIndex = 7; 7274*0b57cec5SDimitry Andric } 7275*0b57cec5SDimitry Andric } 7276*0b57cec5SDimitry Andric } 7277*0b57cec5SDimitry Andric 7278*0b57cec5SDimitry Andric auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 7279*0b57cec5SDimitry Andric // The module path string ref set in the summary must be owned by the 7280*0b57cec5SDimitry Andric // index's module string table. Since we don't have a module path 7281*0b57cec5SDimitry Andric // string table section in the per-module index, we create a single 7282*0b57cec5SDimitry Andric // module path string table entry with an empty (0) ID to take 7283*0b57cec5SDimitry Andric // ownership. 7284*0b57cec5SDimitry Andric int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs; 7285*0b57cec5SDimitry Andric assert(Record.size() >= RefListStartIndex + NumRefs && 7286*0b57cec5SDimitry Andric "Record size inconsistent with number of references"); 7287*0b57cec5SDimitry Andric std::vector<ValueInfo> Refs = makeRefList( 7288*0b57cec5SDimitry Andric ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs)); 7289*0b57cec5SDimitry Andric bool HasProfile = (BitCode == bitc::FS_PERMODULE_PROFILE); 7290*0b57cec5SDimitry Andric bool HasRelBF = (BitCode == bitc::FS_PERMODULE_RELBF); 7291*0b57cec5SDimitry Andric std::vector<FunctionSummary::EdgeTy> Calls = makeCallList( 7292*0b57cec5SDimitry Andric ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex), 7293*0b57cec5SDimitry Andric IsOldProfileFormat, HasProfile, HasRelBF); 7294*0b57cec5SDimitry Andric setSpecialRefs(Refs, NumRORefs, NumWORefs); 7295bdd1243dSDimitry Andric auto VIAndOriginalGUID = getValueInfoFromValueId(ValueID); 7296bdd1243dSDimitry Andric // In order to save memory, only record the memprof summaries if this is 7297bdd1243dSDimitry Andric // the prevailing copy of a symbol. The linker doesn't resolve local 7298bdd1243dSDimitry Andric // linkage values so don't check whether those are prevailing. 7299bdd1243dSDimitry Andric auto LT = (GlobalValue::LinkageTypes)Flags.Linkage; 7300bdd1243dSDimitry Andric if (IsPrevailing && 7301bdd1243dSDimitry Andric !GlobalValue::isLocalLinkage(LT) && 7302bdd1243dSDimitry Andric !IsPrevailing(std::get<2>(VIAndOriginalGUID))) { 7303bdd1243dSDimitry Andric PendingCallsites.clear(); 7304bdd1243dSDimitry Andric PendingAllocs.clear(); 7305bdd1243dSDimitry Andric } 73068bcb0991SDimitry Andric auto FS = std::make_unique<FunctionSummary>( 7307*0b57cec5SDimitry Andric Flags, InstCount, getDecodedFFlags(RawFunFlags), /*EntryCount=*/0, 7308*0b57cec5SDimitry Andric std::move(Refs), std::move(Calls), std::move(PendingTypeTests), 7309*0b57cec5SDimitry Andric std::move(PendingTypeTestAssumeVCalls), 7310*0b57cec5SDimitry Andric std::move(PendingTypeCheckedLoadVCalls), 7311*0b57cec5SDimitry Andric std::move(PendingTypeTestAssumeConstVCalls), 73125ffd83dbSDimitry Andric std::move(PendingTypeCheckedLoadConstVCalls), 7313bdd1243dSDimitry Andric std::move(PendingParamAccesses), std::move(PendingCallsites), 7314bdd1243dSDimitry Andric std::move(PendingAllocs)); 7315*0b57cec5SDimitry Andric FS->setModulePath(getThisModule()->first()); 7316bdd1243dSDimitry Andric FS->setOriginalName(std::get<1>(VIAndOriginalGUID)); 7317bdd1243dSDimitry Andric TheIndex.addGlobalValueSummary(std::get<0>(VIAndOriginalGUID), 7318bdd1243dSDimitry Andric std::move(FS)); 7319*0b57cec5SDimitry Andric break; 7320*0b57cec5SDimitry Andric } 7321*0b57cec5SDimitry Andric // FS_ALIAS: [valueid, flags, valueid] 7322*0b57cec5SDimitry Andric // Aliases must be emitted (and parsed) after all FS_PERMODULE entries, as 7323*0b57cec5SDimitry Andric // they expect all aliasee summaries to be available. 7324*0b57cec5SDimitry Andric case bitc::FS_ALIAS: { 7325*0b57cec5SDimitry Andric unsigned ValueID = Record[0]; 7326*0b57cec5SDimitry Andric uint64_t RawFlags = Record[1]; 7327*0b57cec5SDimitry Andric unsigned AliaseeID = Record[2]; 7328*0b57cec5SDimitry Andric auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 73298bcb0991SDimitry Andric auto AS = std::make_unique<AliasSummary>(Flags); 7330*0b57cec5SDimitry Andric // The module path string ref set in the summary must be owned by the 7331*0b57cec5SDimitry Andric // index's module string table. Since we don't have a module path 7332*0b57cec5SDimitry Andric // string table section in the per-module index, we create a single 7333*0b57cec5SDimitry Andric // module path string table entry with an empty (0) ID to take 7334*0b57cec5SDimitry Andric // ownership. 7335*0b57cec5SDimitry Andric AS->setModulePath(getThisModule()->first()); 7336*0b57cec5SDimitry Andric 7337bdd1243dSDimitry Andric auto AliaseeVI = std::get<0>(getValueInfoFromValueId(AliaseeID)); 7338*0b57cec5SDimitry Andric auto AliaseeInModule = TheIndex.findSummaryInModule(AliaseeVI, ModulePath); 7339*0b57cec5SDimitry Andric if (!AliaseeInModule) 7340*0b57cec5SDimitry Andric return error("Alias expects aliasee summary to be parsed"); 7341*0b57cec5SDimitry Andric AS->setAliasee(AliaseeVI, AliaseeInModule); 7342*0b57cec5SDimitry Andric 7343*0b57cec5SDimitry Andric auto GUID = getValueInfoFromValueId(ValueID); 7344bdd1243dSDimitry Andric AS->setOriginalName(std::get<1>(GUID)); 7345bdd1243dSDimitry Andric TheIndex.addGlobalValueSummary(std::get<0>(GUID), std::move(AS)); 7346*0b57cec5SDimitry Andric break; 7347*0b57cec5SDimitry Andric } 7348*0b57cec5SDimitry Andric // FS_PERMODULE_GLOBALVAR_INIT_REFS: [valueid, flags, varflags, n x valueid] 7349*0b57cec5SDimitry Andric case bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS: { 7350*0b57cec5SDimitry Andric unsigned ValueID = Record[0]; 7351*0b57cec5SDimitry Andric uint64_t RawFlags = Record[1]; 7352*0b57cec5SDimitry Andric unsigned RefArrayStart = 2; 7353*0b57cec5SDimitry Andric GlobalVarSummary::GVarFlags GVF(/* ReadOnly */ false, 73545ffd83dbSDimitry Andric /* WriteOnly */ false, 73555ffd83dbSDimitry Andric /* Constant */ false, 73565ffd83dbSDimitry Andric GlobalObject::VCallVisibilityPublic); 7357*0b57cec5SDimitry Andric auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 7358*0b57cec5SDimitry Andric if (Version >= 5) { 7359*0b57cec5SDimitry Andric GVF = getDecodedGVarFlags(Record[2]); 7360*0b57cec5SDimitry Andric RefArrayStart = 3; 7361*0b57cec5SDimitry Andric } 7362*0b57cec5SDimitry Andric std::vector<ValueInfo> Refs = 7363*0b57cec5SDimitry Andric makeRefList(ArrayRef<uint64_t>(Record).slice(RefArrayStart)); 7364*0b57cec5SDimitry Andric auto FS = 73658bcb0991SDimitry Andric std::make_unique<GlobalVarSummary>(Flags, GVF, std::move(Refs)); 7366*0b57cec5SDimitry Andric FS->setModulePath(getThisModule()->first()); 7367*0b57cec5SDimitry Andric auto GUID = getValueInfoFromValueId(ValueID); 7368bdd1243dSDimitry Andric FS->setOriginalName(std::get<1>(GUID)); 7369bdd1243dSDimitry Andric TheIndex.addGlobalValueSummary(std::get<0>(GUID), std::move(FS)); 7370*0b57cec5SDimitry Andric break; 7371*0b57cec5SDimitry Andric } 7372*0b57cec5SDimitry Andric // FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS: [valueid, flags, varflags, 7373*0b57cec5SDimitry Andric // numrefs, numrefs x valueid, 7374*0b57cec5SDimitry Andric // n x (valueid, offset)] 7375*0b57cec5SDimitry Andric case bitc::FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS: { 7376*0b57cec5SDimitry Andric unsigned ValueID = Record[0]; 7377*0b57cec5SDimitry Andric uint64_t RawFlags = Record[1]; 7378*0b57cec5SDimitry Andric GlobalVarSummary::GVarFlags GVF = getDecodedGVarFlags(Record[2]); 7379*0b57cec5SDimitry Andric unsigned NumRefs = Record[3]; 7380*0b57cec5SDimitry Andric unsigned RefListStartIndex = 4; 7381*0b57cec5SDimitry Andric unsigned VTableListStartIndex = RefListStartIndex + NumRefs; 7382*0b57cec5SDimitry Andric auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 7383*0b57cec5SDimitry Andric std::vector<ValueInfo> Refs = makeRefList( 7384*0b57cec5SDimitry Andric ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs)); 7385*0b57cec5SDimitry Andric VTableFuncList VTableFuncs; 7386*0b57cec5SDimitry Andric for (unsigned I = VTableListStartIndex, E = Record.size(); I != E; ++I) { 7387bdd1243dSDimitry Andric ValueInfo Callee = std::get<0>(getValueInfoFromValueId(Record[I])); 7388*0b57cec5SDimitry Andric uint64_t Offset = Record[++I]; 7389*0b57cec5SDimitry Andric VTableFuncs.push_back({Callee, Offset}); 7390*0b57cec5SDimitry Andric } 7391*0b57cec5SDimitry Andric auto VS = 73928bcb0991SDimitry Andric std::make_unique<GlobalVarSummary>(Flags, GVF, std::move(Refs)); 7393*0b57cec5SDimitry Andric VS->setModulePath(getThisModule()->first()); 7394*0b57cec5SDimitry Andric VS->setVTableFuncs(VTableFuncs); 7395*0b57cec5SDimitry Andric auto GUID = getValueInfoFromValueId(ValueID); 7396bdd1243dSDimitry Andric VS->setOriginalName(std::get<1>(GUID)); 7397bdd1243dSDimitry Andric TheIndex.addGlobalValueSummary(std::get<0>(GUID), std::move(VS)); 7398*0b57cec5SDimitry Andric break; 7399*0b57cec5SDimitry Andric } 7400*0b57cec5SDimitry Andric // FS_COMBINED: [valueid, modid, flags, instcount, fflags, numrefs, 7401*0b57cec5SDimitry Andric // numrefs x valueid, n x (valueid)] 7402*0b57cec5SDimitry Andric // FS_COMBINED_PROFILE: [valueid, modid, flags, instcount, fflags, numrefs, 7403*0b57cec5SDimitry Andric // numrefs x valueid, n x (valueid, hotness)] 7404*0b57cec5SDimitry Andric case bitc::FS_COMBINED: 7405*0b57cec5SDimitry Andric case bitc::FS_COMBINED_PROFILE: { 7406*0b57cec5SDimitry Andric unsigned ValueID = Record[0]; 7407*0b57cec5SDimitry Andric uint64_t ModuleId = Record[1]; 7408*0b57cec5SDimitry Andric uint64_t RawFlags = Record[2]; 7409*0b57cec5SDimitry Andric unsigned InstCount = Record[3]; 7410*0b57cec5SDimitry Andric uint64_t RawFunFlags = 0; 7411*0b57cec5SDimitry Andric uint64_t EntryCount = 0; 7412*0b57cec5SDimitry Andric unsigned NumRefs = Record[4]; 7413*0b57cec5SDimitry Andric unsigned NumRORefs = 0, NumWORefs = 0; 7414*0b57cec5SDimitry Andric int RefListStartIndex = 5; 7415*0b57cec5SDimitry Andric 7416*0b57cec5SDimitry Andric if (Version >= 4) { 7417*0b57cec5SDimitry Andric RawFunFlags = Record[4]; 7418*0b57cec5SDimitry Andric RefListStartIndex = 6; 7419*0b57cec5SDimitry Andric size_t NumRefsIndex = 5; 7420*0b57cec5SDimitry Andric if (Version >= 5) { 7421*0b57cec5SDimitry Andric unsigned NumRORefsOffset = 1; 7422*0b57cec5SDimitry Andric RefListStartIndex = 7; 7423*0b57cec5SDimitry Andric if (Version >= 6) { 7424*0b57cec5SDimitry Andric NumRefsIndex = 6; 7425*0b57cec5SDimitry Andric EntryCount = Record[5]; 7426*0b57cec5SDimitry Andric RefListStartIndex = 8; 7427*0b57cec5SDimitry Andric if (Version >= 7) { 7428*0b57cec5SDimitry Andric RefListStartIndex = 9; 7429*0b57cec5SDimitry Andric NumWORefs = Record[8]; 7430*0b57cec5SDimitry Andric NumRORefsOffset = 2; 7431*0b57cec5SDimitry Andric } 7432*0b57cec5SDimitry Andric } 7433*0b57cec5SDimitry Andric NumRORefs = Record[RefListStartIndex - NumRORefsOffset]; 7434*0b57cec5SDimitry Andric } 7435*0b57cec5SDimitry Andric NumRefs = Record[NumRefsIndex]; 7436*0b57cec5SDimitry Andric } 7437*0b57cec5SDimitry Andric 7438*0b57cec5SDimitry Andric auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 7439*0b57cec5SDimitry Andric int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs; 7440*0b57cec5SDimitry Andric assert(Record.size() >= RefListStartIndex + NumRefs && 7441*0b57cec5SDimitry Andric "Record size inconsistent with number of references"); 7442*0b57cec5SDimitry Andric std::vector<ValueInfo> Refs = makeRefList( 7443*0b57cec5SDimitry Andric ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs)); 7444*0b57cec5SDimitry Andric bool HasProfile = (BitCode == bitc::FS_COMBINED_PROFILE); 7445*0b57cec5SDimitry Andric std::vector<FunctionSummary::EdgeTy> Edges = makeCallList( 7446*0b57cec5SDimitry Andric ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex), 7447*0b57cec5SDimitry Andric IsOldProfileFormat, HasProfile, false); 7448bdd1243dSDimitry Andric ValueInfo VI = std::get<0>(getValueInfoFromValueId(ValueID)); 7449*0b57cec5SDimitry Andric setSpecialRefs(Refs, NumRORefs, NumWORefs); 74508bcb0991SDimitry Andric auto FS = std::make_unique<FunctionSummary>( 7451*0b57cec5SDimitry Andric Flags, InstCount, getDecodedFFlags(RawFunFlags), EntryCount, 7452*0b57cec5SDimitry Andric std::move(Refs), std::move(Edges), std::move(PendingTypeTests), 7453*0b57cec5SDimitry Andric std::move(PendingTypeTestAssumeVCalls), 7454*0b57cec5SDimitry Andric std::move(PendingTypeCheckedLoadVCalls), 7455*0b57cec5SDimitry Andric std::move(PendingTypeTestAssumeConstVCalls), 74565ffd83dbSDimitry Andric std::move(PendingTypeCheckedLoadConstVCalls), 7457bdd1243dSDimitry Andric std::move(PendingParamAccesses), std::move(PendingCallsites), 7458bdd1243dSDimitry Andric std::move(PendingAllocs)); 7459*0b57cec5SDimitry Andric LastSeenSummary = FS.get(); 7460*0b57cec5SDimitry Andric LastSeenGUID = VI.getGUID(); 7461*0b57cec5SDimitry Andric FS->setModulePath(ModuleIdMap[ModuleId]); 7462*0b57cec5SDimitry Andric TheIndex.addGlobalValueSummary(VI, std::move(FS)); 7463*0b57cec5SDimitry Andric break; 7464*0b57cec5SDimitry Andric } 7465*0b57cec5SDimitry Andric // FS_COMBINED_ALIAS: [valueid, modid, flags, valueid] 7466*0b57cec5SDimitry Andric // Aliases must be emitted (and parsed) after all FS_COMBINED entries, as 7467*0b57cec5SDimitry Andric // they expect all aliasee summaries to be available. 7468*0b57cec5SDimitry Andric case bitc::FS_COMBINED_ALIAS: { 7469*0b57cec5SDimitry Andric unsigned ValueID = Record[0]; 7470*0b57cec5SDimitry Andric uint64_t ModuleId = Record[1]; 7471*0b57cec5SDimitry Andric uint64_t RawFlags = Record[2]; 7472*0b57cec5SDimitry Andric unsigned AliaseeValueId = Record[3]; 7473*0b57cec5SDimitry Andric auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 74748bcb0991SDimitry Andric auto AS = std::make_unique<AliasSummary>(Flags); 7475*0b57cec5SDimitry Andric LastSeenSummary = AS.get(); 7476*0b57cec5SDimitry Andric AS->setModulePath(ModuleIdMap[ModuleId]); 7477*0b57cec5SDimitry Andric 7478bdd1243dSDimitry Andric auto AliaseeVI = std::get<0>(getValueInfoFromValueId(AliaseeValueId)); 7479*0b57cec5SDimitry Andric auto AliaseeInModule = TheIndex.findSummaryInModule(AliaseeVI, AS->modulePath()); 7480*0b57cec5SDimitry Andric AS->setAliasee(AliaseeVI, AliaseeInModule); 7481*0b57cec5SDimitry Andric 7482bdd1243dSDimitry Andric ValueInfo VI = std::get<0>(getValueInfoFromValueId(ValueID)); 7483*0b57cec5SDimitry Andric LastSeenGUID = VI.getGUID(); 7484*0b57cec5SDimitry Andric TheIndex.addGlobalValueSummary(VI, std::move(AS)); 7485*0b57cec5SDimitry Andric break; 7486*0b57cec5SDimitry Andric } 7487*0b57cec5SDimitry Andric // FS_COMBINED_GLOBALVAR_INIT_REFS: [valueid, modid, flags, n x valueid] 7488*0b57cec5SDimitry Andric case bitc::FS_COMBINED_GLOBALVAR_INIT_REFS: { 7489*0b57cec5SDimitry Andric unsigned ValueID = Record[0]; 7490*0b57cec5SDimitry Andric uint64_t ModuleId = Record[1]; 7491*0b57cec5SDimitry Andric uint64_t RawFlags = Record[2]; 7492*0b57cec5SDimitry Andric unsigned RefArrayStart = 3; 7493*0b57cec5SDimitry Andric GlobalVarSummary::GVarFlags GVF(/* ReadOnly */ false, 74945ffd83dbSDimitry Andric /* WriteOnly */ false, 74955ffd83dbSDimitry Andric /* Constant */ false, 74965ffd83dbSDimitry Andric GlobalObject::VCallVisibilityPublic); 7497*0b57cec5SDimitry Andric auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 7498*0b57cec5SDimitry Andric if (Version >= 5) { 7499*0b57cec5SDimitry Andric GVF = getDecodedGVarFlags(Record[3]); 7500*0b57cec5SDimitry Andric RefArrayStart = 4; 7501*0b57cec5SDimitry Andric } 7502*0b57cec5SDimitry Andric std::vector<ValueInfo> Refs = 7503*0b57cec5SDimitry Andric makeRefList(ArrayRef<uint64_t>(Record).slice(RefArrayStart)); 7504*0b57cec5SDimitry Andric auto FS = 75058bcb0991SDimitry Andric std::make_unique<GlobalVarSummary>(Flags, GVF, std::move(Refs)); 7506*0b57cec5SDimitry Andric LastSeenSummary = FS.get(); 7507*0b57cec5SDimitry Andric FS->setModulePath(ModuleIdMap[ModuleId]); 7508bdd1243dSDimitry Andric ValueInfo VI = std::get<0>(getValueInfoFromValueId(ValueID)); 7509*0b57cec5SDimitry Andric LastSeenGUID = VI.getGUID(); 7510*0b57cec5SDimitry Andric TheIndex.addGlobalValueSummary(VI, std::move(FS)); 7511*0b57cec5SDimitry Andric break; 7512*0b57cec5SDimitry Andric } 7513*0b57cec5SDimitry Andric // FS_COMBINED_ORIGINAL_NAME: [original_name] 7514*0b57cec5SDimitry Andric case bitc::FS_COMBINED_ORIGINAL_NAME: { 7515*0b57cec5SDimitry Andric uint64_t OriginalName = Record[0]; 7516*0b57cec5SDimitry Andric if (!LastSeenSummary) 7517*0b57cec5SDimitry Andric return error("Name attachment that does not follow a combined record"); 7518*0b57cec5SDimitry Andric LastSeenSummary->setOriginalName(OriginalName); 7519*0b57cec5SDimitry Andric TheIndex.addOriginalName(LastSeenGUID, OriginalName); 7520*0b57cec5SDimitry Andric // Reset the LastSeenSummary 7521*0b57cec5SDimitry Andric LastSeenSummary = nullptr; 7522*0b57cec5SDimitry Andric LastSeenGUID = 0; 7523*0b57cec5SDimitry Andric break; 7524*0b57cec5SDimitry Andric } 7525*0b57cec5SDimitry Andric case bitc::FS_TYPE_TESTS: 7526*0b57cec5SDimitry Andric assert(PendingTypeTests.empty()); 7527e8d8bef9SDimitry Andric llvm::append_range(PendingTypeTests, Record); 7528*0b57cec5SDimitry Andric break; 7529*0b57cec5SDimitry Andric 7530*0b57cec5SDimitry Andric case bitc::FS_TYPE_TEST_ASSUME_VCALLS: 7531*0b57cec5SDimitry Andric assert(PendingTypeTestAssumeVCalls.empty()); 7532*0b57cec5SDimitry Andric for (unsigned I = 0; I != Record.size(); I += 2) 7533*0b57cec5SDimitry Andric PendingTypeTestAssumeVCalls.push_back({Record[I], Record[I+1]}); 7534*0b57cec5SDimitry Andric break; 7535*0b57cec5SDimitry Andric 7536*0b57cec5SDimitry Andric case bitc::FS_TYPE_CHECKED_LOAD_VCALLS: 7537*0b57cec5SDimitry Andric assert(PendingTypeCheckedLoadVCalls.empty()); 7538*0b57cec5SDimitry Andric for (unsigned I = 0; I != Record.size(); I += 2) 7539*0b57cec5SDimitry Andric PendingTypeCheckedLoadVCalls.push_back({Record[I], Record[I+1]}); 7540*0b57cec5SDimitry Andric break; 7541*0b57cec5SDimitry Andric 7542*0b57cec5SDimitry Andric case bitc::FS_TYPE_TEST_ASSUME_CONST_VCALL: 7543*0b57cec5SDimitry Andric PendingTypeTestAssumeConstVCalls.push_back( 7544*0b57cec5SDimitry Andric {{Record[0], Record[1]}, {Record.begin() + 2, Record.end()}}); 7545*0b57cec5SDimitry Andric break; 7546*0b57cec5SDimitry Andric 7547*0b57cec5SDimitry Andric case bitc::FS_TYPE_CHECKED_LOAD_CONST_VCALL: 7548*0b57cec5SDimitry Andric PendingTypeCheckedLoadConstVCalls.push_back( 7549*0b57cec5SDimitry Andric {{Record[0], Record[1]}, {Record.begin() + 2, Record.end()}}); 7550*0b57cec5SDimitry Andric break; 7551*0b57cec5SDimitry Andric 7552*0b57cec5SDimitry Andric case bitc::FS_CFI_FUNCTION_DEFS: { 7553*0b57cec5SDimitry Andric std::set<std::string> &CfiFunctionDefs = TheIndex.cfiFunctionDefs(); 7554*0b57cec5SDimitry Andric for (unsigned I = 0; I != Record.size(); I += 2) 7555*0b57cec5SDimitry Andric CfiFunctionDefs.insert( 7556*0b57cec5SDimitry Andric {Strtab.data() + Record[I], static_cast<size_t>(Record[I + 1])}); 7557*0b57cec5SDimitry Andric break; 7558*0b57cec5SDimitry Andric } 7559*0b57cec5SDimitry Andric 7560*0b57cec5SDimitry Andric case bitc::FS_CFI_FUNCTION_DECLS: { 7561*0b57cec5SDimitry Andric std::set<std::string> &CfiFunctionDecls = TheIndex.cfiFunctionDecls(); 7562*0b57cec5SDimitry Andric for (unsigned I = 0; I != Record.size(); I += 2) 7563*0b57cec5SDimitry Andric CfiFunctionDecls.insert( 7564*0b57cec5SDimitry Andric {Strtab.data() + Record[I], static_cast<size_t>(Record[I + 1])}); 7565*0b57cec5SDimitry Andric break; 7566*0b57cec5SDimitry Andric } 7567*0b57cec5SDimitry Andric 7568*0b57cec5SDimitry Andric case bitc::FS_TYPE_ID: 7569*0b57cec5SDimitry Andric parseTypeIdSummaryRecord(Record, Strtab, TheIndex); 7570*0b57cec5SDimitry Andric break; 7571*0b57cec5SDimitry Andric 7572*0b57cec5SDimitry Andric case bitc::FS_TYPE_ID_METADATA: 7573*0b57cec5SDimitry Andric parseTypeIdCompatibleVtableSummaryRecord(Record); 7574*0b57cec5SDimitry Andric break; 75755ffd83dbSDimitry Andric 75765ffd83dbSDimitry Andric case bitc::FS_BLOCK_COUNT: 75775ffd83dbSDimitry Andric TheIndex.addBlockCount(Record[0]); 75785ffd83dbSDimitry Andric break; 75795ffd83dbSDimitry Andric 75805ffd83dbSDimitry Andric case bitc::FS_PARAM_ACCESS: { 75815ffd83dbSDimitry Andric PendingParamAccesses = parseParamAccesses(Record); 75825ffd83dbSDimitry Andric break; 75835ffd83dbSDimitry Andric } 7584bdd1243dSDimitry Andric 7585bdd1243dSDimitry Andric case bitc::FS_STACK_IDS: { // [n x stackid] 7586bdd1243dSDimitry Andric // Save stack ids in the reader to consult when adding stack ids from the 7587bdd1243dSDimitry Andric // lists in the stack node and alloc node entries. 7588bdd1243dSDimitry Andric StackIds = ArrayRef<uint64_t>(Record); 7589bdd1243dSDimitry Andric break; 7590bdd1243dSDimitry Andric } 7591bdd1243dSDimitry Andric 7592bdd1243dSDimitry Andric case bitc::FS_PERMODULE_CALLSITE_INFO: { 7593bdd1243dSDimitry Andric unsigned ValueID = Record[0]; 7594bdd1243dSDimitry Andric SmallVector<unsigned> StackIdList; 7595bdd1243dSDimitry Andric for (auto R = Record.begin() + 1; R != Record.end(); R++) { 7596bdd1243dSDimitry Andric assert(*R < StackIds.size()); 7597bdd1243dSDimitry Andric StackIdList.push_back(TheIndex.addOrGetStackIdIndex(StackIds[*R])); 7598bdd1243dSDimitry Andric } 7599bdd1243dSDimitry Andric ValueInfo VI = std::get<0>(getValueInfoFromValueId(ValueID)); 7600bdd1243dSDimitry Andric PendingCallsites.push_back(CallsiteInfo({VI, std::move(StackIdList)})); 7601bdd1243dSDimitry Andric break; 7602bdd1243dSDimitry Andric } 7603bdd1243dSDimitry Andric 7604bdd1243dSDimitry Andric case bitc::FS_COMBINED_CALLSITE_INFO: { 7605bdd1243dSDimitry Andric auto RecordIter = Record.begin(); 7606bdd1243dSDimitry Andric unsigned ValueID = *RecordIter++; 7607bdd1243dSDimitry Andric unsigned NumStackIds = *RecordIter++; 7608bdd1243dSDimitry Andric unsigned NumVersions = *RecordIter++; 7609bdd1243dSDimitry Andric assert(Record.size() == 3 + NumStackIds + NumVersions); 7610bdd1243dSDimitry Andric SmallVector<unsigned> StackIdList; 7611bdd1243dSDimitry Andric for (unsigned J = 0; J < NumStackIds; J++) { 7612bdd1243dSDimitry Andric assert(*RecordIter < StackIds.size()); 7613bdd1243dSDimitry Andric StackIdList.push_back( 7614bdd1243dSDimitry Andric TheIndex.addOrGetStackIdIndex(StackIds[*RecordIter++])); 7615bdd1243dSDimitry Andric } 7616bdd1243dSDimitry Andric SmallVector<unsigned> Versions; 7617bdd1243dSDimitry Andric for (unsigned J = 0; J < NumVersions; J++) 7618bdd1243dSDimitry Andric Versions.push_back(*RecordIter++); 7619bdd1243dSDimitry Andric ValueInfo VI = std::get<0>( 7620bdd1243dSDimitry Andric getValueInfoFromValueId</*AllowNullValueInfo*/ true>(ValueID)); 7621bdd1243dSDimitry Andric PendingCallsites.push_back( 7622bdd1243dSDimitry Andric CallsiteInfo({VI, std::move(Versions), std::move(StackIdList)})); 7623bdd1243dSDimitry Andric break; 7624bdd1243dSDimitry Andric } 7625bdd1243dSDimitry Andric 7626bdd1243dSDimitry Andric case bitc::FS_PERMODULE_ALLOC_INFO: { 7627bdd1243dSDimitry Andric unsigned I = 0; 7628bdd1243dSDimitry Andric std::vector<MIBInfo> MIBs; 7629bdd1243dSDimitry Andric while (I < Record.size()) { 7630bdd1243dSDimitry Andric assert(Record.size() - I >= 2); 7631bdd1243dSDimitry Andric AllocationType AllocType = (AllocationType)Record[I++]; 7632bdd1243dSDimitry Andric unsigned NumStackEntries = Record[I++]; 7633bdd1243dSDimitry Andric assert(Record.size() - I >= NumStackEntries); 7634bdd1243dSDimitry Andric SmallVector<unsigned> StackIdList; 7635bdd1243dSDimitry Andric for (unsigned J = 0; J < NumStackEntries; J++) { 7636bdd1243dSDimitry Andric assert(Record[I] < StackIds.size()); 7637bdd1243dSDimitry Andric StackIdList.push_back( 7638bdd1243dSDimitry Andric TheIndex.addOrGetStackIdIndex(StackIds[Record[I++]])); 7639bdd1243dSDimitry Andric } 7640bdd1243dSDimitry Andric MIBs.push_back(MIBInfo(AllocType, std::move(StackIdList))); 7641bdd1243dSDimitry Andric } 7642bdd1243dSDimitry Andric PendingAllocs.push_back(AllocInfo(std::move(MIBs))); 7643bdd1243dSDimitry Andric break; 7644bdd1243dSDimitry Andric } 7645bdd1243dSDimitry Andric 7646bdd1243dSDimitry Andric case bitc::FS_COMBINED_ALLOC_INFO: { 7647bdd1243dSDimitry Andric unsigned I = 0; 7648bdd1243dSDimitry Andric std::vector<MIBInfo> MIBs; 7649bdd1243dSDimitry Andric unsigned NumMIBs = Record[I++]; 7650bdd1243dSDimitry Andric unsigned NumVersions = Record[I++]; 7651bdd1243dSDimitry Andric unsigned MIBsRead = 0; 7652bdd1243dSDimitry Andric while (MIBsRead++ < NumMIBs) { 7653bdd1243dSDimitry Andric assert(Record.size() - I >= 2); 7654bdd1243dSDimitry Andric AllocationType AllocType = (AllocationType)Record[I++]; 7655bdd1243dSDimitry Andric unsigned NumStackEntries = Record[I++]; 7656bdd1243dSDimitry Andric assert(Record.size() - I >= NumStackEntries); 7657bdd1243dSDimitry Andric SmallVector<unsigned> StackIdList; 7658bdd1243dSDimitry Andric for (unsigned J = 0; J < NumStackEntries; J++) { 7659bdd1243dSDimitry Andric assert(Record[I] < StackIds.size()); 7660bdd1243dSDimitry Andric StackIdList.push_back( 7661bdd1243dSDimitry Andric TheIndex.addOrGetStackIdIndex(StackIds[Record[I++]])); 7662bdd1243dSDimitry Andric } 7663bdd1243dSDimitry Andric MIBs.push_back(MIBInfo(AllocType, std::move(StackIdList))); 7664bdd1243dSDimitry Andric } 7665bdd1243dSDimitry Andric assert(Record.size() - I >= NumVersions); 7666bdd1243dSDimitry Andric SmallVector<uint8_t> Versions; 7667bdd1243dSDimitry Andric for (unsigned J = 0; J < NumVersions; J++) 7668bdd1243dSDimitry Andric Versions.push_back(Record[I++]); 7669bdd1243dSDimitry Andric PendingAllocs.push_back( 7670bdd1243dSDimitry Andric AllocInfo(std::move(Versions), std::move(MIBs))); 7671bdd1243dSDimitry Andric break; 7672bdd1243dSDimitry Andric } 7673*0b57cec5SDimitry Andric } 7674*0b57cec5SDimitry Andric } 7675*0b57cec5SDimitry Andric llvm_unreachable("Exit infinite loop"); 7676*0b57cec5SDimitry Andric } 7677*0b57cec5SDimitry Andric 7678*0b57cec5SDimitry Andric // Parse the module string table block into the Index. 7679*0b57cec5SDimitry Andric // This populates the ModulePathStringTable map in the index. 7680*0b57cec5SDimitry Andric Error ModuleSummaryIndexBitcodeReader::parseModuleStringTable() { 7681*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::MODULE_STRTAB_BLOCK_ID)) 7682*0b57cec5SDimitry Andric return Err; 7683*0b57cec5SDimitry Andric 7684*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 7685*0b57cec5SDimitry Andric 7686*0b57cec5SDimitry Andric SmallString<128> ModulePath; 7687*0b57cec5SDimitry Andric ModuleSummaryIndex::ModuleInfo *LastSeenModule = nullptr; 7688*0b57cec5SDimitry Andric 7689*0b57cec5SDimitry Andric while (true) { 7690*0b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 7691*0b57cec5SDimitry Andric if (!MaybeEntry) 7692*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 7693*0b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get(); 7694*0b57cec5SDimitry Andric 7695*0b57cec5SDimitry Andric switch (Entry.Kind) { 7696*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 7697*0b57cec5SDimitry Andric case BitstreamEntry::Error: 7698*0b57cec5SDimitry Andric return error("Malformed block"); 7699*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 7700*0b57cec5SDimitry Andric return Error::success(); 7701*0b57cec5SDimitry Andric case BitstreamEntry::Record: 7702*0b57cec5SDimitry Andric // The interesting case. 7703*0b57cec5SDimitry Andric break; 7704*0b57cec5SDimitry Andric } 7705*0b57cec5SDimitry Andric 7706*0b57cec5SDimitry Andric Record.clear(); 7707*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 7708*0b57cec5SDimitry Andric if (!MaybeRecord) 7709*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 7710*0b57cec5SDimitry Andric switch (MaybeRecord.get()) { 7711*0b57cec5SDimitry Andric default: // Default behavior: ignore. 7712*0b57cec5SDimitry Andric break; 7713*0b57cec5SDimitry Andric case bitc::MST_CODE_ENTRY: { 7714*0b57cec5SDimitry Andric // MST_ENTRY: [modid, namechar x N] 7715*0b57cec5SDimitry Andric uint64_t ModuleId = Record[0]; 7716*0b57cec5SDimitry Andric 7717*0b57cec5SDimitry Andric if (convertToString(Record, 1, ModulePath)) 7718*0b57cec5SDimitry Andric return error("Invalid record"); 7719*0b57cec5SDimitry Andric 7720*0b57cec5SDimitry Andric LastSeenModule = TheIndex.addModule(ModulePath, ModuleId); 7721*0b57cec5SDimitry Andric ModuleIdMap[ModuleId] = LastSeenModule->first(); 7722*0b57cec5SDimitry Andric 7723*0b57cec5SDimitry Andric ModulePath.clear(); 7724*0b57cec5SDimitry Andric break; 7725*0b57cec5SDimitry Andric } 7726*0b57cec5SDimitry Andric /// MST_CODE_HASH: [5*i32] 7727*0b57cec5SDimitry Andric case bitc::MST_CODE_HASH: { 7728*0b57cec5SDimitry Andric if (Record.size() != 5) 7729*0b57cec5SDimitry Andric return error("Invalid hash length " + Twine(Record.size()).str()); 7730*0b57cec5SDimitry Andric if (!LastSeenModule) 7731*0b57cec5SDimitry Andric return error("Invalid hash that does not follow a module path"); 7732*0b57cec5SDimitry Andric int Pos = 0; 7733*0b57cec5SDimitry Andric for (auto &Val : Record) { 7734*0b57cec5SDimitry Andric assert(!(Val >> 32) && "Unexpected high bits set"); 7735*0b57cec5SDimitry Andric LastSeenModule->second.second[Pos++] = Val; 7736*0b57cec5SDimitry Andric } 7737*0b57cec5SDimitry Andric // Reset LastSeenModule to avoid overriding the hash unexpectedly. 7738*0b57cec5SDimitry Andric LastSeenModule = nullptr; 7739*0b57cec5SDimitry Andric break; 7740*0b57cec5SDimitry Andric } 7741*0b57cec5SDimitry Andric } 7742*0b57cec5SDimitry Andric } 7743*0b57cec5SDimitry Andric llvm_unreachable("Exit infinite loop"); 7744*0b57cec5SDimitry Andric } 7745*0b57cec5SDimitry Andric 7746*0b57cec5SDimitry Andric namespace { 7747*0b57cec5SDimitry Andric 7748*0b57cec5SDimitry Andric // FIXME: This class is only here to support the transition to llvm::Error. It 7749*0b57cec5SDimitry Andric // will be removed once this transition is complete. Clients should prefer to 7750*0b57cec5SDimitry Andric // deal with the Error value directly, rather than converting to error_code. 7751*0b57cec5SDimitry Andric class BitcodeErrorCategoryType : public std::error_category { 7752*0b57cec5SDimitry Andric const char *name() const noexcept override { 7753*0b57cec5SDimitry Andric return "llvm.bitcode"; 7754*0b57cec5SDimitry Andric } 7755*0b57cec5SDimitry Andric 7756*0b57cec5SDimitry Andric std::string message(int IE) const override { 7757*0b57cec5SDimitry Andric BitcodeError E = static_cast<BitcodeError>(IE); 7758*0b57cec5SDimitry Andric switch (E) { 7759*0b57cec5SDimitry Andric case BitcodeError::CorruptedBitcode: 7760*0b57cec5SDimitry Andric return "Corrupted bitcode"; 7761*0b57cec5SDimitry Andric } 7762*0b57cec5SDimitry Andric llvm_unreachable("Unknown error type!"); 7763*0b57cec5SDimitry Andric } 7764*0b57cec5SDimitry Andric }; 7765*0b57cec5SDimitry Andric 7766*0b57cec5SDimitry Andric } // end anonymous namespace 7767*0b57cec5SDimitry Andric 7768*0b57cec5SDimitry Andric const std::error_category &llvm::BitcodeErrorCategory() { 7769753f127fSDimitry Andric static BitcodeErrorCategoryType ErrorCategory; 7770753f127fSDimitry Andric return ErrorCategory; 7771*0b57cec5SDimitry Andric } 7772*0b57cec5SDimitry Andric 7773*0b57cec5SDimitry Andric static Expected<StringRef> readBlobInRecord(BitstreamCursor &Stream, 7774*0b57cec5SDimitry Andric unsigned Block, unsigned RecordID) { 7775*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(Block)) 7776*0b57cec5SDimitry Andric return std::move(Err); 7777*0b57cec5SDimitry Andric 7778*0b57cec5SDimitry Andric StringRef Strtab; 7779*0b57cec5SDimitry Andric while (true) { 7780*0b57cec5SDimitry Andric Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 7781*0b57cec5SDimitry Andric if (!MaybeEntry) 7782*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 7783*0b57cec5SDimitry Andric llvm::BitstreamEntry Entry = MaybeEntry.get(); 7784*0b57cec5SDimitry Andric 7785*0b57cec5SDimitry Andric switch (Entry.Kind) { 7786*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 7787*0b57cec5SDimitry Andric return Strtab; 7788*0b57cec5SDimitry Andric 7789*0b57cec5SDimitry Andric case BitstreamEntry::Error: 7790*0b57cec5SDimitry Andric return error("Malformed block"); 7791*0b57cec5SDimitry Andric 7792*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: 7793*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 7794*0b57cec5SDimitry Andric return std::move(Err); 7795*0b57cec5SDimitry Andric break; 7796*0b57cec5SDimitry Andric 7797*0b57cec5SDimitry Andric case BitstreamEntry::Record: 7798*0b57cec5SDimitry Andric StringRef Blob; 7799*0b57cec5SDimitry Andric SmallVector<uint64_t, 1> Record; 7800*0b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = 7801*0b57cec5SDimitry Andric Stream.readRecord(Entry.ID, Record, &Blob); 7802*0b57cec5SDimitry Andric if (!MaybeRecord) 7803*0b57cec5SDimitry Andric return MaybeRecord.takeError(); 7804*0b57cec5SDimitry Andric if (MaybeRecord.get() == RecordID) 7805*0b57cec5SDimitry Andric Strtab = Blob; 7806*0b57cec5SDimitry Andric break; 7807*0b57cec5SDimitry Andric } 7808*0b57cec5SDimitry Andric } 7809*0b57cec5SDimitry Andric } 7810*0b57cec5SDimitry Andric 7811*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 7812*0b57cec5SDimitry Andric // External interface 7813*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 7814*0b57cec5SDimitry Andric 7815*0b57cec5SDimitry Andric Expected<std::vector<BitcodeModule>> 7816*0b57cec5SDimitry Andric llvm::getBitcodeModuleList(MemoryBufferRef Buffer) { 7817*0b57cec5SDimitry Andric auto FOrErr = getBitcodeFileContents(Buffer); 7818*0b57cec5SDimitry Andric if (!FOrErr) 7819*0b57cec5SDimitry Andric return FOrErr.takeError(); 7820*0b57cec5SDimitry Andric return std::move(FOrErr->Mods); 7821*0b57cec5SDimitry Andric } 7822*0b57cec5SDimitry Andric 7823*0b57cec5SDimitry Andric Expected<BitcodeFileContents> 7824*0b57cec5SDimitry Andric llvm::getBitcodeFileContents(MemoryBufferRef Buffer) { 7825*0b57cec5SDimitry Andric Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); 7826*0b57cec5SDimitry Andric if (!StreamOrErr) 7827*0b57cec5SDimitry Andric return StreamOrErr.takeError(); 7828*0b57cec5SDimitry Andric BitstreamCursor &Stream = *StreamOrErr; 7829*0b57cec5SDimitry Andric 7830*0b57cec5SDimitry Andric BitcodeFileContents F; 7831*0b57cec5SDimitry Andric while (true) { 7832*0b57cec5SDimitry Andric uint64_t BCBegin = Stream.getCurrentByteNo(); 7833*0b57cec5SDimitry Andric 7834*0b57cec5SDimitry Andric // We may be consuming bitcode from a client that leaves garbage at the end 7835*0b57cec5SDimitry Andric // of the bitcode stream (e.g. Apple's ar tool). If we are close enough to 7836*0b57cec5SDimitry Andric // the end that there cannot possibly be another module, stop looking. 7837*0b57cec5SDimitry Andric if (BCBegin + 8 >= Stream.getBitcodeBytes().size()) 7838*0b57cec5SDimitry Andric return F; 7839*0b57cec5SDimitry Andric 7840*0b57cec5SDimitry Andric Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 7841*0b57cec5SDimitry Andric if (!MaybeEntry) 7842*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 7843*0b57cec5SDimitry Andric llvm::BitstreamEntry Entry = MaybeEntry.get(); 7844*0b57cec5SDimitry Andric 7845*0b57cec5SDimitry Andric switch (Entry.Kind) { 7846*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 7847*0b57cec5SDimitry Andric case BitstreamEntry::Error: 7848*0b57cec5SDimitry Andric return error("Malformed block"); 7849*0b57cec5SDimitry Andric 7850*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: { 7851*0b57cec5SDimitry Andric uint64_t IdentificationBit = -1ull; 7852*0b57cec5SDimitry Andric if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) { 7853*0b57cec5SDimitry Andric IdentificationBit = Stream.GetCurrentBitNo() - BCBegin * 8; 7854*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 7855*0b57cec5SDimitry Andric return std::move(Err); 7856*0b57cec5SDimitry Andric 7857*0b57cec5SDimitry Andric { 7858*0b57cec5SDimitry Andric Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 7859*0b57cec5SDimitry Andric if (!MaybeEntry) 7860*0b57cec5SDimitry Andric return MaybeEntry.takeError(); 7861*0b57cec5SDimitry Andric Entry = MaybeEntry.get(); 7862*0b57cec5SDimitry Andric } 7863*0b57cec5SDimitry Andric 7864*0b57cec5SDimitry Andric if (Entry.Kind != BitstreamEntry::SubBlock || 7865*0b57cec5SDimitry Andric Entry.ID != bitc::MODULE_BLOCK_ID) 7866*0b57cec5SDimitry Andric return error("Malformed block"); 7867*0b57cec5SDimitry Andric } 7868*0b57cec5SDimitry Andric 7869*0b57cec5SDimitry Andric if (Entry.ID == bitc::MODULE_BLOCK_ID) { 7870*0b57cec5SDimitry Andric uint64_t ModuleBit = Stream.GetCurrentBitNo() - BCBegin * 8; 7871*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 7872*0b57cec5SDimitry Andric return std::move(Err); 7873*0b57cec5SDimitry Andric 7874*0b57cec5SDimitry Andric F.Mods.push_back({Stream.getBitcodeBytes().slice( 7875*0b57cec5SDimitry Andric BCBegin, Stream.getCurrentByteNo() - BCBegin), 7876*0b57cec5SDimitry Andric Buffer.getBufferIdentifier(), IdentificationBit, 7877*0b57cec5SDimitry Andric ModuleBit}); 7878*0b57cec5SDimitry Andric continue; 7879*0b57cec5SDimitry Andric } 7880*0b57cec5SDimitry Andric 7881*0b57cec5SDimitry Andric if (Entry.ID == bitc::STRTAB_BLOCK_ID) { 7882*0b57cec5SDimitry Andric Expected<StringRef> Strtab = 7883*0b57cec5SDimitry Andric readBlobInRecord(Stream, bitc::STRTAB_BLOCK_ID, bitc::STRTAB_BLOB); 7884*0b57cec5SDimitry Andric if (!Strtab) 7885*0b57cec5SDimitry Andric return Strtab.takeError(); 7886*0b57cec5SDimitry Andric // This string table is used by every preceding bitcode module that does 7887*0b57cec5SDimitry Andric // not have its own string table. A bitcode file may have multiple 7888*0b57cec5SDimitry Andric // string tables if it was created by binary concatenation, for example 7889*0b57cec5SDimitry Andric // with "llvm-cat -b". 78900eae32dcSDimitry Andric for (BitcodeModule &I : llvm::reverse(F.Mods)) { 78910eae32dcSDimitry Andric if (!I.Strtab.empty()) 7892*0b57cec5SDimitry Andric break; 78930eae32dcSDimitry Andric I.Strtab = *Strtab; 7894*0b57cec5SDimitry Andric } 7895*0b57cec5SDimitry Andric // Similarly, the string table is used by every preceding symbol table; 7896*0b57cec5SDimitry Andric // normally there will be just one unless the bitcode file was created 7897*0b57cec5SDimitry Andric // by binary concatenation. 7898*0b57cec5SDimitry Andric if (!F.Symtab.empty() && F.StrtabForSymtab.empty()) 7899*0b57cec5SDimitry Andric F.StrtabForSymtab = *Strtab; 7900*0b57cec5SDimitry Andric continue; 7901*0b57cec5SDimitry Andric } 7902*0b57cec5SDimitry Andric 7903*0b57cec5SDimitry Andric if (Entry.ID == bitc::SYMTAB_BLOCK_ID) { 7904*0b57cec5SDimitry Andric Expected<StringRef> SymtabOrErr = 7905*0b57cec5SDimitry Andric readBlobInRecord(Stream, bitc::SYMTAB_BLOCK_ID, bitc::SYMTAB_BLOB); 7906*0b57cec5SDimitry Andric if (!SymtabOrErr) 7907*0b57cec5SDimitry Andric return SymtabOrErr.takeError(); 7908*0b57cec5SDimitry Andric 7909*0b57cec5SDimitry Andric // We can expect the bitcode file to have multiple symbol tables if it 7910*0b57cec5SDimitry Andric // was created by binary concatenation. In that case we silently 7911*0b57cec5SDimitry Andric // ignore any subsequent symbol tables, which is fine because this is a 7912*0b57cec5SDimitry Andric // low level function. The client is expected to notice that the number 7913*0b57cec5SDimitry Andric // of modules in the symbol table does not match the number of modules 7914*0b57cec5SDimitry Andric // in the input file and regenerate the symbol table. 7915*0b57cec5SDimitry Andric if (F.Symtab.empty()) 7916*0b57cec5SDimitry Andric F.Symtab = *SymtabOrErr; 7917*0b57cec5SDimitry Andric continue; 7918*0b57cec5SDimitry Andric } 7919*0b57cec5SDimitry Andric 7920*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 7921*0b57cec5SDimitry Andric return std::move(Err); 7922*0b57cec5SDimitry Andric continue; 7923*0b57cec5SDimitry Andric } 7924*0b57cec5SDimitry Andric case BitstreamEntry::Record: 7925349cc55cSDimitry Andric if (Error E = Stream.skipRecord(Entry.ID).takeError()) 7926349cc55cSDimitry Andric return std::move(E); 7927*0b57cec5SDimitry Andric continue; 7928*0b57cec5SDimitry Andric } 7929*0b57cec5SDimitry Andric } 7930*0b57cec5SDimitry Andric } 7931*0b57cec5SDimitry Andric 7932*0b57cec5SDimitry Andric /// Get a lazy one-at-time loading module from bitcode. 7933*0b57cec5SDimitry Andric /// 7934*0b57cec5SDimitry Andric /// This isn't always used in a lazy context. In particular, it's also used by 7935*0b57cec5SDimitry Andric /// \a parseModule(). If this is truly lazy, then we need to eagerly pull 7936*0b57cec5SDimitry Andric /// in forward-referenced functions from block address references. 7937*0b57cec5SDimitry Andric /// 7938*0b57cec5SDimitry Andric /// \param[in] MaterializeAll Set to \c true if we should materialize 7939*0b57cec5SDimitry Andric /// everything. 7940*0b57cec5SDimitry Andric Expected<std::unique_ptr<Module>> 7941*0b57cec5SDimitry Andric BitcodeModule::getModuleImpl(LLVMContext &Context, bool MaterializeAll, 79425ffd83dbSDimitry Andric bool ShouldLazyLoadMetadata, bool IsImporting, 7943bdd1243dSDimitry Andric ParserCallbacks Callbacks) { 7944*0b57cec5SDimitry Andric BitstreamCursor Stream(Buffer); 7945*0b57cec5SDimitry Andric 7946*0b57cec5SDimitry Andric std::string ProducerIdentification; 7947*0b57cec5SDimitry Andric if (IdentificationBit != -1ull) { 7948*0b57cec5SDimitry Andric if (Error JumpFailed = Stream.JumpToBit(IdentificationBit)) 7949*0b57cec5SDimitry Andric return std::move(JumpFailed); 7950349cc55cSDimitry Andric if (Error E = 7951349cc55cSDimitry Andric readIdentificationBlock(Stream).moveInto(ProducerIdentification)) 7952349cc55cSDimitry Andric return std::move(E); 7953*0b57cec5SDimitry Andric } 7954*0b57cec5SDimitry Andric 7955*0b57cec5SDimitry Andric if (Error JumpFailed = Stream.JumpToBit(ModuleBit)) 7956*0b57cec5SDimitry Andric return std::move(JumpFailed); 7957*0b57cec5SDimitry Andric auto *R = new BitcodeReader(std::move(Stream), Strtab, ProducerIdentification, 7958*0b57cec5SDimitry Andric Context); 7959*0b57cec5SDimitry Andric 7960*0b57cec5SDimitry Andric std::unique_ptr<Module> M = 79618bcb0991SDimitry Andric std::make_unique<Module>(ModuleIdentifier, Context); 7962*0b57cec5SDimitry Andric M->setMaterializer(R); 7963*0b57cec5SDimitry Andric 7964*0b57cec5SDimitry Andric // Delay parsing Metadata if ShouldLazyLoadMetadata is true. 79655ffd83dbSDimitry Andric if (Error Err = R->parseBitcodeInto(M.get(), ShouldLazyLoadMetadata, 7966bdd1243dSDimitry Andric IsImporting, Callbacks)) 7967*0b57cec5SDimitry Andric return std::move(Err); 7968*0b57cec5SDimitry Andric 7969*0b57cec5SDimitry Andric if (MaterializeAll) { 7970*0b57cec5SDimitry Andric // Read in the entire module, and destroy the BitcodeReader. 7971*0b57cec5SDimitry Andric if (Error Err = M->materializeAll()) 7972*0b57cec5SDimitry Andric return std::move(Err); 7973*0b57cec5SDimitry Andric } else { 7974*0b57cec5SDimitry Andric // Resolve forward references from blockaddresses. 7975*0b57cec5SDimitry Andric if (Error Err = R->materializeForwardReferencedFunctions()) 7976*0b57cec5SDimitry Andric return std::move(Err); 7977*0b57cec5SDimitry Andric } 7978*0b57cec5SDimitry Andric return std::move(M); 7979*0b57cec5SDimitry Andric } 7980*0b57cec5SDimitry Andric 7981*0b57cec5SDimitry Andric Expected<std::unique_ptr<Module>> 7982*0b57cec5SDimitry Andric BitcodeModule::getLazyModule(LLVMContext &Context, bool ShouldLazyLoadMetadata, 7983bdd1243dSDimitry Andric bool IsImporting, ParserCallbacks Callbacks) { 79845ffd83dbSDimitry Andric return getModuleImpl(Context, false, ShouldLazyLoadMetadata, IsImporting, 7985bdd1243dSDimitry Andric Callbacks); 7986*0b57cec5SDimitry Andric } 7987*0b57cec5SDimitry Andric 7988*0b57cec5SDimitry Andric // Parse the specified bitcode buffer and merge the index into CombinedIndex. 7989*0b57cec5SDimitry Andric // We don't use ModuleIdentifier here because the client may need to control the 7990*0b57cec5SDimitry Andric // module path used in the combined summary (e.g. when reading summaries for 7991*0b57cec5SDimitry Andric // regular LTO modules). 7992bdd1243dSDimitry Andric Error BitcodeModule::readSummary( 7993bdd1243dSDimitry Andric ModuleSummaryIndex &CombinedIndex, StringRef ModulePath, uint64_t ModuleId, 7994bdd1243dSDimitry Andric std::function<bool(GlobalValue::GUID)> IsPrevailing) { 7995*0b57cec5SDimitry Andric BitstreamCursor Stream(Buffer); 7996*0b57cec5SDimitry Andric if (Error JumpFailed = Stream.JumpToBit(ModuleBit)) 7997*0b57cec5SDimitry Andric return JumpFailed; 7998*0b57cec5SDimitry Andric 7999*0b57cec5SDimitry Andric ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, CombinedIndex, 8000bdd1243dSDimitry Andric ModulePath, ModuleId, IsPrevailing); 8001*0b57cec5SDimitry Andric return R.parseModule(); 8002*0b57cec5SDimitry Andric } 8003*0b57cec5SDimitry Andric 8004*0b57cec5SDimitry Andric // Parse the specified bitcode buffer, returning the function info index. 8005*0b57cec5SDimitry Andric Expected<std::unique_ptr<ModuleSummaryIndex>> BitcodeModule::getSummary() { 8006*0b57cec5SDimitry Andric BitstreamCursor Stream(Buffer); 8007*0b57cec5SDimitry Andric if (Error JumpFailed = Stream.JumpToBit(ModuleBit)) 8008*0b57cec5SDimitry Andric return std::move(JumpFailed); 8009*0b57cec5SDimitry Andric 80108bcb0991SDimitry Andric auto Index = std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false); 8011*0b57cec5SDimitry Andric ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, *Index, 8012*0b57cec5SDimitry Andric ModuleIdentifier, 0); 8013*0b57cec5SDimitry Andric 8014*0b57cec5SDimitry Andric if (Error Err = R.parseModule()) 8015*0b57cec5SDimitry Andric return std::move(Err); 8016*0b57cec5SDimitry Andric 8017*0b57cec5SDimitry Andric return std::move(Index); 8018*0b57cec5SDimitry Andric } 8019*0b57cec5SDimitry Andric 8020*0b57cec5SDimitry Andric static Expected<bool> getEnableSplitLTOUnitFlag(BitstreamCursor &Stream, 8021*0b57cec5SDimitry Andric unsigned ID) { 8022*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(ID)) 8023*0b57cec5SDimitry Andric return std::move(Err); 8024*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 8025*0b57cec5SDimitry Andric 8026*0b57cec5SDimitry Andric while (true) { 8027349cc55cSDimitry Andric BitstreamEntry Entry; 8028349cc55cSDimitry Andric if (Error E = Stream.advanceSkippingSubblocks().moveInto(Entry)) 8029349cc55cSDimitry Andric return std::move(E); 8030*0b57cec5SDimitry Andric 8031*0b57cec5SDimitry Andric switch (Entry.Kind) { 8032*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 8033*0b57cec5SDimitry Andric case BitstreamEntry::Error: 8034*0b57cec5SDimitry Andric return error("Malformed block"); 8035*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 8036*0b57cec5SDimitry Andric // If no flags record found, conservatively return true to mimic 8037*0b57cec5SDimitry Andric // behavior before this flag was added. 8038*0b57cec5SDimitry Andric return true; 8039*0b57cec5SDimitry Andric case BitstreamEntry::Record: 8040*0b57cec5SDimitry Andric // The interesting case. 8041*0b57cec5SDimitry Andric break; 8042*0b57cec5SDimitry Andric } 8043*0b57cec5SDimitry Andric 8044*0b57cec5SDimitry Andric // Look for the FS_FLAGS record. 8045*0b57cec5SDimitry Andric Record.clear(); 8046*0b57cec5SDimitry Andric Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); 8047*0b57cec5SDimitry Andric if (!MaybeBitCode) 8048*0b57cec5SDimitry Andric return MaybeBitCode.takeError(); 8049*0b57cec5SDimitry Andric switch (MaybeBitCode.get()) { 8050*0b57cec5SDimitry Andric default: // Default behavior: ignore. 8051*0b57cec5SDimitry Andric break; 8052*0b57cec5SDimitry Andric case bitc::FS_FLAGS: { // [flags] 8053*0b57cec5SDimitry Andric uint64_t Flags = Record[0]; 8054*0b57cec5SDimitry Andric // Scan flags. 8055972a253aSDimitry Andric assert(Flags <= 0xff && "Unexpected bits in flag"); 8056*0b57cec5SDimitry Andric 8057*0b57cec5SDimitry Andric return Flags & 0x8; 8058*0b57cec5SDimitry Andric } 8059*0b57cec5SDimitry Andric } 8060*0b57cec5SDimitry Andric } 8061*0b57cec5SDimitry Andric llvm_unreachable("Exit infinite loop"); 8062*0b57cec5SDimitry Andric } 8063*0b57cec5SDimitry Andric 8064*0b57cec5SDimitry Andric // Check if the given bitcode buffer contains a global value summary block. 8065*0b57cec5SDimitry Andric Expected<BitcodeLTOInfo> BitcodeModule::getLTOInfo() { 8066*0b57cec5SDimitry Andric BitstreamCursor Stream(Buffer); 8067*0b57cec5SDimitry Andric if (Error JumpFailed = Stream.JumpToBit(ModuleBit)) 8068*0b57cec5SDimitry Andric return std::move(JumpFailed); 8069*0b57cec5SDimitry Andric 8070*0b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 8071*0b57cec5SDimitry Andric return std::move(Err); 8072*0b57cec5SDimitry Andric 8073*0b57cec5SDimitry Andric while (true) { 8074349cc55cSDimitry Andric llvm::BitstreamEntry Entry; 8075349cc55cSDimitry Andric if (Error E = Stream.advance().moveInto(Entry)) 8076349cc55cSDimitry Andric return std::move(E); 8077*0b57cec5SDimitry Andric 8078*0b57cec5SDimitry Andric switch (Entry.Kind) { 8079*0b57cec5SDimitry Andric case BitstreamEntry::Error: 8080*0b57cec5SDimitry Andric return error("Malformed block"); 8081*0b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 8082*0b57cec5SDimitry Andric return BitcodeLTOInfo{/*IsThinLTO=*/false, /*HasSummary=*/false, 8083*0b57cec5SDimitry Andric /*EnableSplitLTOUnit=*/false}; 8084*0b57cec5SDimitry Andric 8085*0b57cec5SDimitry Andric case BitstreamEntry::SubBlock: 8086*0b57cec5SDimitry Andric if (Entry.ID == bitc::GLOBALVAL_SUMMARY_BLOCK_ID) { 8087*0b57cec5SDimitry Andric Expected<bool> EnableSplitLTOUnit = 8088*0b57cec5SDimitry Andric getEnableSplitLTOUnitFlag(Stream, Entry.ID); 8089*0b57cec5SDimitry Andric if (!EnableSplitLTOUnit) 8090*0b57cec5SDimitry Andric return EnableSplitLTOUnit.takeError(); 8091*0b57cec5SDimitry Andric return BitcodeLTOInfo{/*IsThinLTO=*/true, /*HasSummary=*/true, 8092*0b57cec5SDimitry Andric *EnableSplitLTOUnit}; 8093*0b57cec5SDimitry Andric } 8094*0b57cec5SDimitry Andric 8095*0b57cec5SDimitry Andric if (Entry.ID == bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID) { 8096*0b57cec5SDimitry Andric Expected<bool> EnableSplitLTOUnit = 8097*0b57cec5SDimitry Andric getEnableSplitLTOUnitFlag(Stream, Entry.ID); 8098*0b57cec5SDimitry Andric if (!EnableSplitLTOUnit) 8099*0b57cec5SDimitry Andric return EnableSplitLTOUnit.takeError(); 8100*0b57cec5SDimitry Andric return BitcodeLTOInfo{/*IsThinLTO=*/false, /*HasSummary=*/true, 8101*0b57cec5SDimitry Andric *EnableSplitLTOUnit}; 8102*0b57cec5SDimitry Andric } 8103*0b57cec5SDimitry Andric 8104*0b57cec5SDimitry Andric // Ignore other sub-blocks. 8105*0b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) 8106*0b57cec5SDimitry Andric return std::move(Err); 8107*0b57cec5SDimitry Andric continue; 8108*0b57cec5SDimitry Andric 8109*0b57cec5SDimitry Andric case BitstreamEntry::Record: 8110*0b57cec5SDimitry Andric if (Expected<unsigned> StreamFailed = Stream.skipRecord(Entry.ID)) 8111*0b57cec5SDimitry Andric continue; 8112*0b57cec5SDimitry Andric else 8113*0b57cec5SDimitry Andric return StreamFailed.takeError(); 8114*0b57cec5SDimitry Andric } 8115*0b57cec5SDimitry Andric } 8116*0b57cec5SDimitry Andric } 8117*0b57cec5SDimitry Andric 8118*0b57cec5SDimitry Andric static Expected<BitcodeModule> getSingleModule(MemoryBufferRef Buffer) { 8119*0b57cec5SDimitry Andric Expected<std::vector<BitcodeModule>> MsOrErr = getBitcodeModuleList(Buffer); 8120*0b57cec5SDimitry Andric if (!MsOrErr) 8121*0b57cec5SDimitry Andric return MsOrErr.takeError(); 8122*0b57cec5SDimitry Andric 8123*0b57cec5SDimitry Andric if (MsOrErr->size() != 1) 8124*0b57cec5SDimitry Andric return error("Expected a single module"); 8125*0b57cec5SDimitry Andric 8126*0b57cec5SDimitry Andric return (*MsOrErr)[0]; 8127*0b57cec5SDimitry Andric } 8128*0b57cec5SDimitry Andric 8129*0b57cec5SDimitry Andric Expected<std::unique_ptr<Module>> 8130*0b57cec5SDimitry Andric llvm::getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context, 8131bdd1243dSDimitry Andric bool ShouldLazyLoadMetadata, bool IsImporting, 8132bdd1243dSDimitry Andric ParserCallbacks Callbacks) { 8133*0b57cec5SDimitry Andric Expected<BitcodeModule> BM = getSingleModule(Buffer); 8134*0b57cec5SDimitry Andric if (!BM) 8135*0b57cec5SDimitry Andric return BM.takeError(); 8136*0b57cec5SDimitry Andric 8137bdd1243dSDimitry Andric return BM->getLazyModule(Context, ShouldLazyLoadMetadata, IsImporting, 8138bdd1243dSDimitry Andric Callbacks); 8139*0b57cec5SDimitry Andric } 8140*0b57cec5SDimitry Andric 8141*0b57cec5SDimitry Andric Expected<std::unique_ptr<Module>> llvm::getOwningLazyBitcodeModule( 8142*0b57cec5SDimitry Andric std::unique_ptr<MemoryBuffer> &&Buffer, LLVMContext &Context, 8143bdd1243dSDimitry Andric bool ShouldLazyLoadMetadata, bool IsImporting, ParserCallbacks Callbacks) { 8144*0b57cec5SDimitry Andric auto MOrErr = getLazyBitcodeModule(*Buffer, Context, ShouldLazyLoadMetadata, 8145bdd1243dSDimitry Andric IsImporting, Callbacks); 8146*0b57cec5SDimitry Andric if (MOrErr) 8147*0b57cec5SDimitry Andric (*MOrErr)->setOwnedMemoryBuffer(std::move(Buffer)); 8148*0b57cec5SDimitry Andric return MOrErr; 8149*0b57cec5SDimitry Andric } 8150*0b57cec5SDimitry Andric 8151*0b57cec5SDimitry Andric Expected<std::unique_ptr<Module>> 8152bdd1243dSDimitry Andric BitcodeModule::parseModule(LLVMContext &Context, ParserCallbacks Callbacks) { 8153bdd1243dSDimitry Andric return getModuleImpl(Context, true, false, false, Callbacks); 8154*0b57cec5SDimitry Andric // TODO: Restore the use-lists to the in-memory state when the bitcode was 8155*0b57cec5SDimitry Andric // written. We must defer until the Module has been fully materialized. 8156*0b57cec5SDimitry Andric } 8157*0b57cec5SDimitry Andric 81585ffd83dbSDimitry Andric Expected<std::unique_ptr<Module>> 81595ffd83dbSDimitry Andric llvm::parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context, 8160bdd1243dSDimitry Andric ParserCallbacks Callbacks) { 8161*0b57cec5SDimitry Andric Expected<BitcodeModule> BM = getSingleModule(Buffer); 8162*0b57cec5SDimitry Andric if (!BM) 8163*0b57cec5SDimitry Andric return BM.takeError(); 8164*0b57cec5SDimitry Andric 8165bdd1243dSDimitry Andric return BM->parseModule(Context, Callbacks); 8166*0b57cec5SDimitry Andric } 8167*0b57cec5SDimitry Andric 8168*0b57cec5SDimitry Andric Expected<std::string> llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer) { 8169*0b57cec5SDimitry Andric Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); 8170*0b57cec5SDimitry Andric if (!StreamOrErr) 8171*0b57cec5SDimitry Andric return StreamOrErr.takeError(); 8172*0b57cec5SDimitry Andric 8173*0b57cec5SDimitry Andric return readTriple(*StreamOrErr); 8174*0b57cec5SDimitry Andric } 8175*0b57cec5SDimitry Andric 8176*0b57cec5SDimitry Andric Expected<bool> llvm::isBitcodeContainingObjCCategory(MemoryBufferRef Buffer) { 8177*0b57cec5SDimitry Andric Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); 8178*0b57cec5SDimitry Andric if (!StreamOrErr) 8179*0b57cec5SDimitry Andric return StreamOrErr.takeError(); 8180*0b57cec5SDimitry Andric 8181*0b57cec5SDimitry Andric return hasObjCCategory(*StreamOrErr); 8182*0b57cec5SDimitry Andric } 8183*0b57cec5SDimitry Andric 8184*0b57cec5SDimitry Andric Expected<std::string> llvm::getBitcodeProducerString(MemoryBufferRef Buffer) { 8185*0b57cec5SDimitry Andric Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); 8186*0b57cec5SDimitry Andric if (!StreamOrErr) 8187*0b57cec5SDimitry Andric return StreamOrErr.takeError(); 8188*0b57cec5SDimitry Andric 8189*0b57cec5SDimitry Andric return readIdentificationCode(*StreamOrErr); 8190*0b57cec5SDimitry Andric } 8191*0b57cec5SDimitry Andric 8192*0b57cec5SDimitry Andric Error llvm::readModuleSummaryIndex(MemoryBufferRef Buffer, 8193*0b57cec5SDimitry Andric ModuleSummaryIndex &CombinedIndex, 8194*0b57cec5SDimitry Andric uint64_t ModuleId) { 8195*0b57cec5SDimitry Andric Expected<BitcodeModule> BM = getSingleModule(Buffer); 8196*0b57cec5SDimitry Andric if (!BM) 8197*0b57cec5SDimitry Andric return BM.takeError(); 8198*0b57cec5SDimitry Andric 8199*0b57cec5SDimitry Andric return BM->readSummary(CombinedIndex, BM->getModuleIdentifier(), ModuleId); 8200*0b57cec5SDimitry Andric } 8201*0b57cec5SDimitry Andric 8202*0b57cec5SDimitry Andric Expected<std::unique_ptr<ModuleSummaryIndex>> 8203*0b57cec5SDimitry Andric llvm::getModuleSummaryIndex(MemoryBufferRef Buffer) { 8204*0b57cec5SDimitry Andric Expected<BitcodeModule> BM = getSingleModule(Buffer); 8205*0b57cec5SDimitry Andric if (!BM) 8206*0b57cec5SDimitry Andric return BM.takeError(); 8207*0b57cec5SDimitry Andric 8208*0b57cec5SDimitry Andric return BM->getSummary(); 8209*0b57cec5SDimitry Andric } 8210*0b57cec5SDimitry Andric 8211*0b57cec5SDimitry Andric Expected<BitcodeLTOInfo> llvm::getBitcodeLTOInfo(MemoryBufferRef Buffer) { 8212*0b57cec5SDimitry Andric Expected<BitcodeModule> BM = getSingleModule(Buffer); 8213*0b57cec5SDimitry Andric if (!BM) 8214*0b57cec5SDimitry Andric return BM.takeError(); 8215*0b57cec5SDimitry Andric 8216*0b57cec5SDimitry Andric return BM->getLTOInfo(); 8217*0b57cec5SDimitry Andric } 8218*0b57cec5SDimitry Andric 8219*0b57cec5SDimitry Andric Expected<std::unique_ptr<ModuleSummaryIndex>> 8220*0b57cec5SDimitry Andric llvm::getModuleSummaryIndexForFile(StringRef Path, 8221*0b57cec5SDimitry Andric bool IgnoreEmptyThinLTOIndexFile) { 8222*0b57cec5SDimitry Andric ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = 8223*0b57cec5SDimitry Andric MemoryBuffer::getFileOrSTDIN(Path); 8224*0b57cec5SDimitry Andric if (!FileOrErr) 8225*0b57cec5SDimitry Andric return errorCodeToError(FileOrErr.getError()); 8226*0b57cec5SDimitry Andric if (IgnoreEmptyThinLTOIndexFile && !(*FileOrErr)->getBufferSize()) 8227*0b57cec5SDimitry Andric return nullptr; 8228*0b57cec5SDimitry Andric return getModuleSummaryIndex(**FileOrErr); 8229*0b57cec5SDimitry Andric } 8230