1 //===- MIRParser.cpp - MIR serialization format parser implementation -----===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the class that parses the optional LLVM IR and machine 10 // functions that are stored in MIR files. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/CodeGen/MIRParser/MIRParser.h" 15 #include "llvm/ADT/DenseMap.h" 16 #include "llvm/ADT/STLExtras.h" 17 #include "llvm/ADT/StringMap.h" 18 #include "llvm/ADT/StringRef.h" 19 #include "llvm/AsmParser/Parser.h" 20 #include "llvm/AsmParser/SlotMapping.h" 21 #include "llvm/CodeGen/GlobalISel/RegisterBank.h" 22 #include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h" 23 #include "llvm/CodeGen/MIRParser/MIParser.h" 24 #include "llvm/CodeGen/MIRYamlMapping.h" 25 #include "llvm/CodeGen/MachineConstantPool.h" 26 #include "llvm/CodeGen/MachineFrameInfo.h" 27 #include "llvm/CodeGen/MachineFunction.h" 28 #include "llvm/CodeGen/MachineModuleInfo.h" 29 #include "llvm/CodeGen/MachineRegisterInfo.h" 30 #include "llvm/CodeGen/TargetFrameLowering.h" 31 #include "llvm/IR/BasicBlock.h" 32 #include "llvm/IR/DebugInfo.h" 33 #include "llvm/IR/DiagnosticInfo.h" 34 #include "llvm/IR/Instructions.h" 35 #include "llvm/IR/LLVMContext.h" 36 #include "llvm/IR/Module.h" 37 #include "llvm/IR/ValueSymbolTable.h" 38 #include "llvm/Support/LineIterator.h" 39 #include "llvm/Support/MemoryBuffer.h" 40 #include "llvm/Support/SMLoc.h" 41 #include "llvm/Support/SourceMgr.h" 42 #include "llvm/Support/YAMLTraits.h" 43 #include "llvm/Target/TargetMachine.h" 44 #include <memory> 45 46 using namespace llvm; 47 48 namespace llvm { 49 50 /// This class implements the parsing of LLVM IR that's embedded inside a MIR 51 /// file. 52 class MIRParserImpl { 53 SourceMgr SM; 54 LLVMContext &Context; 55 yaml::Input In; 56 StringRef Filename; 57 SlotMapping IRSlots; 58 std::unique_ptr<PerTargetMIParsingState> Target; 59 60 /// True when the MIR file doesn't have LLVM IR. Dummy IR functions are 61 /// created and inserted into the given module when this is true. 62 bool NoLLVMIR = false; 63 /// True when a well formed MIR file does not contain any MIR/machine function 64 /// parts. 65 bool NoMIRDocuments = false; 66 67 std::function<void(Function &)> ProcessIRFunction; 68 69 public: 70 MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, StringRef Filename, 71 LLVMContext &Context, 72 std::function<void(Function &)> ProcessIRFunction); 73 74 void reportDiagnostic(const SMDiagnostic &Diag); 75 76 /// Report an error with the given message at unknown location. 77 /// 78 /// Always returns true. 79 bool error(const Twine &Message); 80 81 /// Report an error with the given message at the given location. 82 /// 83 /// Always returns true. 84 bool error(SMLoc Loc, const Twine &Message); 85 86 /// Report a given error with the location translated from the location in an 87 /// embedded string literal to a location in the MIR file. 88 /// 89 /// Always returns true. 90 bool error(const SMDiagnostic &Error, SMRange SourceRange); 91 92 /// Try to parse the optional LLVM module and the machine functions in the MIR 93 /// file. 94 /// 95 /// Return null if an error occurred. 96 std::unique_ptr<Module> 97 parseIRModule(DataLayoutCallbackTy DataLayoutCallback); 98 99 /// Create an empty function with the given name. 100 Function *createDummyFunction(StringRef Name, Module &M); 101 102 bool parseMachineFunctions(Module &M, MachineModuleInfo &MMI); 103 104 /// Parse the machine function in the current YAML document. 105 /// 106 /// 107 /// Return true if an error occurred. 108 bool parseMachineFunction(Module &M, MachineModuleInfo &MMI); 109 110 /// Initialize the machine function to the state that's described in the MIR 111 /// file. 112 /// 113 /// Return true if error occurred. 114 bool initializeMachineFunction(const yaml::MachineFunction &YamlMF, 115 MachineFunction &MF); 116 117 bool parseRegisterInfo(PerFunctionMIParsingState &PFS, 118 const yaml::MachineFunction &YamlMF); 119 120 bool setupRegisterInfo(const PerFunctionMIParsingState &PFS, 121 const yaml::MachineFunction &YamlMF); 122 123 bool initializeFrameInfo(PerFunctionMIParsingState &PFS, 124 const yaml::MachineFunction &YamlMF); 125 126 bool initializeCallSiteInfo(PerFunctionMIParsingState &PFS, 127 const yaml::MachineFunction &YamlMF); 128 129 bool parseCalleeSavedRegister(PerFunctionMIParsingState &PFS, 130 std::vector<CalleeSavedInfo> &CSIInfo, 131 const yaml::StringValue &RegisterSource, 132 bool IsRestored, int FrameIdx); 133 134 template <typename T> 135 bool parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS, 136 const T &Object, 137 int FrameIdx); 138 139 bool initializeConstantPool(PerFunctionMIParsingState &PFS, 140 MachineConstantPool &ConstantPool, 141 const yaml::MachineFunction &YamlMF); 142 143 bool initializeJumpTableInfo(PerFunctionMIParsingState &PFS, 144 const yaml::MachineJumpTable &YamlJTI); 145 146 private: 147 bool parseMDNode(PerFunctionMIParsingState &PFS, MDNode *&Node, 148 const yaml::StringValue &Source); 149 150 bool parseMBBReference(PerFunctionMIParsingState &PFS, 151 MachineBasicBlock *&MBB, 152 const yaml::StringValue &Source); 153 154 /// Return a MIR diagnostic converted from an MI string diagnostic. 155 SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error, 156 SMRange SourceRange); 157 158 /// Return a MIR diagnostic converted from a diagnostic located in a YAML 159 /// block scalar string. 160 SMDiagnostic diagFromBlockStringDiag(const SMDiagnostic &Error, 161 SMRange SourceRange); 162 163 void computeFunctionProperties(MachineFunction &MF); 164 165 void setupDebugValueTracking(MachineFunction &MF, 166 PerFunctionMIParsingState &PFS, const yaml::MachineFunction &YamlMF); 167 }; 168 169 } // end namespace llvm 170 171 static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) { 172 reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag); 173 } 174 175 MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, 176 StringRef Filename, LLVMContext &Context, 177 std::function<void(Function &)> Callback) 178 : SM(), 179 Context(Context), 180 In(SM.getMemoryBuffer(SM.AddNewSourceBuffer(std::move(Contents), SMLoc())) 181 ->getBuffer(), 182 nullptr, handleYAMLDiag, this), 183 Filename(Filename), ProcessIRFunction(Callback) { 184 In.setContext(&In); 185 } 186 187 bool MIRParserImpl::error(const Twine &Message) { 188 Context.diagnose(DiagnosticInfoMIRParser( 189 DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str()))); 190 return true; 191 } 192 193 bool MIRParserImpl::error(SMLoc Loc, const Twine &Message) { 194 Context.diagnose(DiagnosticInfoMIRParser( 195 DS_Error, SM.GetMessage(Loc, SourceMgr::DK_Error, Message))); 196 return true; 197 } 198 199 bool MIRParserImpl::error(const SMDiagnostic &Error, SMRange SourceRange) { 200 assert(Error.getKind() == SourceMgr::DK_Error && "Expected an error"); 201 reportDiagnostic(diagFromMIStringDiag(Error, SourceRange)); 202 return true; 203 } 204 205 void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) { 206 DiagnosticSeverity Kind; 207 switch (Diag.getKind()) { 208 case SourceMgr::DK_Error: 209 Kind = DS_Error; 210 break; 211 case SourceMgr::DK_Warning: 212 Kind = DS_Warning; 213 break; 214 case SourceMgr::DK_Note: 215 Kind = DS_Note; 216 break; 217 case SourceMgr::DK_Remark: 218 llvm_unreachable("remark unexpected"); 219 break; 220 } 221 Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag)); 222 } 223 224 std::unique_ptr<Module> 225 MIRParserImpl::parseIRModule(DataLayoutCallbackTy DataLayoutCallback) { 226 if (!In.setCurrentDocument()) { 227 if (In.error()) 228 return nullptr; 229 // Create an empty module when the MIR file is empty. 230 NoMIRDocuments = true; 231 auto M = std::make_unique<Module>(Filename, Context); 232 if (auto LayoutOverride = DataLayoutCallback(M->getTargetTriple())) 233 M->setDataLayout(*LayoutOverride); 234 return M; 235 } 236 237 std::unique_ptr<Module> M; 238 // Parse the block scalar manually so that we can return unique pointer 239 // without having to go trough YAML traits. 240 if (const auto *BSN = 241 dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) { 242 SMDiagnostic Error; 243 M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error, 244 Context, &IRSlots, DataLayoutCallback); 245 if (!M) { 246 reportDiagnostic(diagFromBlockStringDiag(Error, BSN->getSourceRange())); 247 return nullptr; 248 } 249 In.nextDocument(); 250 if (!In.setCurrentDocument()) 251 NoMIRDocuments = true; 252 } else { 253 // Create an new, empty module. 254 M = std::make_unique<Module>(Filename, Context); 255 if (auto LayoutOverride = DataLayoutCallback(M->getTargetTriple())) 256 M->setDataLayout(*LayoutOverride); 257 NoLLVMIR = true; 258 } 259 return M; 260 } 261 262 bool MIRParserImpl::parseMachineFunctions(Module &M, MachineModuleInfo &MMI) { 263 if (NoMIRDocuments) 264 return false; 265 266 // Parse the machine functions. 267 do { 268 if (parseMachineFunction(M, MMI)) 269 return true; 270 In.nextDocument(); 271 } while (In.setCurrentDocument()); 272 273 return false; 274 } 275 276 Function *MIRParserImpl::createDummyFunction(StringRef Name, Module &M) { 277 auto &Context = M.getContext(); 278 Function *F = 279 Function::Create(FunctionType::get(Type::getVoidTy(Context), false), 280 Function::ExternalLinkage, Name, M); 281 BasicBlock *BB = BasicBlock::Create(Context, "entry", F); 282 new UnreachableInst(Context, BB); 283 284 if (ProcessIRFunction) 285 ProcessIRFunction(*F); 286 287 return F; 288 } 289 290 bool MIRParserImpl::parseMachineFunction(Module &M, MachineModuleInfo &MMI) { 291 // Parse the yaml. 292 yaml::MachineFunction YamlMF; 293 yaml::EmptyContext Ctx; 294 295 const LLVMTargetMachine &TM = MMI.getTarget(); 296 YamlMF.MachineFuncInfo = std::unique_ptr<yaml::MachineFunctionInfo>( 297 TM.createDefaultFuncInfoYAML()); 298 299 yaml::yamlize(In, YamlMF, false, Ctx); 300 if (In.error()) 301 return true; 302 303 // Search for the corresponding IR function. 304 StringRef FunctionName = YamlMF.Name; 305 Function *F = M.getFunction(FunctionName); 306 if (!F) { 307 if (NoLLVMIR) { 308 F = createDummyFunction(FunctionName, M); 309 } else { 310 return error(Twine("function '") + FunctionName + 311 "' isn't defined in the provided LLVM IR"); 312 } 313 } 314 if (MMI.getMachineFunction(*F) != nullptr) 315 return error(Twine("redefinition of machine function '") + FunctionName + 316 "'"); 317 318 // Create the MachineFunction. 319 MachineFunction &MF = MMI.getOrCreateMachineFunction(*F); 320 if (initializeMachineFunction(YamlMF, MF)) 321 return true; 322 323 return false; 324 } 325 326 static bool isSSA(const MachineFunction &MF) { 327 const MachineRegisterInfo &MRI = MF.getRegInfo(); 328 for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) { 329 Register Reg = Register::index2VirtReg(I); 330 if (!MRI.hasOneDef(Reg) && !MRI.def_empty(Reg)) 331 return false; 332 333 // Subregister defs are invalid in SSA. 334 const MachineOperand *RegDef = MRI.getOneDef(Reg); 335 if (RegDef && RegDef->getSubReg() != 0) 336 return false; 337 } 338 return true; 339 } 340 341 void MIRParserImpl::computeFunctionProperties(MachineFunction &MF) { 342 MachineFunctionProperties &Properties = MF.getProperties(); 343 344 bool HasPHI = false; 345 bool HasInlineAsm = false; 346 for (const MachineBasicBlock &MBB : MF) { 347 for (const MachineInstr &MI : MBB) { 348 if (MI.isPHI()) 349 HasPHI = true; 350 if (MI.isInlineAsm()) 351 HasInlineAsm = true; 352 } 353 } 354 if (!HasPHI) 355 Properties.set(MachineFunctionProperties::Property::NoPHIs); 356 MF.setHasInlineAsm(HasInlineAsm); 357 358 if (isSSA(MF)) 359 Properties.set(MachineFunctionProperties::Property::IsSSA); 360 else 361 Properties.reset(MachineFunctionProperties::Property::IsSSA); 362 363 const MachineRegisterInfo &MRI = MF.getRegInfo(); 364 if (MRI.getNumVirtRegs() == 0) 365 Properties.set(MachineFunctionProperties::Property::NoVRegs); 366 } 367 368 bool MIRParserImpl::initializeCallSiteInfo( 369 PerFunctionMIParsingState &PFS, const yaml::MachineFunction &YamlMF) { 370 MachineFunction &MF = PFS.MF; 371 SMDiagnostic Error; 372 const LLVMTargetMachine &TM = MF.getTarget(); 373 for (auto YamlCSInfo : YamlMF.CallSitesInfo) { 374 yaml::CallSiteInfo::MachineInstrLoc MILoc = YamlCSInfo.CallLocation; 375 if (MILoc.BlockNum >= MF.size()) 376 return error(Twine(MF.getName()) + 377 Twine(" call instruction block out of range.") + 378 " Unable to reference bb:" + Twine(MILoc.BlockNum)); 379 auto CallB = std::next(MF.begin(), MILoc.BlockNum); 380 if (MILoc.Offset >= CallB->size()) 381 return error(Twine(MF.getName()) + 382 Twine(" call instruction offset out of range.") + 383 " Unable to reference instruction at bb: " + 384 Twine(MILoc.BlockNum) + " at offset:" + Twine(MILoc.Offset)); 385 auto CallI = std::next(CallB->instr_begin(), MILoc.Offset); 386 if (!CallI->isCall(MachineInstr::IgnoreBundle)) 387 return error(Twine(MF.getName()) + 388 Twine(" call site info should reference call " 389 "instruction. Instruction at bb:") + 390 Twine(MILoc.BlockNum) + " at offset:" + Twine(MILoc.Offset) + 391 " is not a call instruction"); 392 MachineFunction::CallSiteInfo CSInfo; 393 for (auto ArgRegPair : YamlCSInfo.ArgForwardingRegs) { 394 Register Reg; 395 if (parseNamedRegisterReference(PFS, Reg, ArgRegPair.Reg.Value, Error)) 396 return error(Error, ArgRegPair.Reg.SourceRange); 397 CSInfo.emplace_back(Reg, ArgRegPair.ArgNo); 398 } 399 400 if (TM.Options.EmitCallSiteInfo) 401 MF.addCallArgsForwardingRegs(&*CallI, std::move(CSInfo)); 402 } 403 404 if (YamlMF.CallSitesInfo.size() && !TM.Options.EmitCallSiteInfo) 405 return error(Twine("Call site info provided but not used")); 406 return false; 407 } 408 409 void MIRParserImpl::setupDebugValueTracking( 410 MachineFunction &MF, PerFunctionMIParsingState &PFS, 411 const yaml::MachineFunction &YamlMF) { 412 // Compute the value of the "next instruction number" field. 413 unsigned MaxInstrNum = 0; 414 for (auto &MBB : MF) 415 for (auto &MI : MBB) 416 MaxInstrNum = std::max((unsigned)MI.peekDebugInstrNum(), MaxInstrNum); 417 MF.setDebugInstrNumberingCount(MaxInstrNum); 418 419 // Load any substitutions. 420 for (auto &Sub : YamlMF.DebugValueSubstitutions) { 421 MF.makeDebugValueSubstitution(std::make_pair(Sub.SrcInst, Sub.SrcOp), 422 std::make_pair(Sub.DstInst, Sub.DstOp)); 423 } 424 } 425 426 bool 427 MIRParserImpl::initializeMachineFunction(const yaml::MachineFunction &YamlMF, 428 MachineFunction &MF) { 429 // TODO: Recreate the machine function. 430 if (Target) { 431 // Avoid clearing state if we're using the same subtarget again. 432 Target->setTarget(MF.getSubtarget()); 433 } else { 434 Target.reset(new PerTargetMIParsingState(MF.getSubtarget())); 435 } 436 437 MF.setAlignment(YamlMF.Alignment.valueOrOne()); 438 MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice); 439 MF.setHasWinCFI(YamlMF.HasWinCFI); 440 441 if (YamlMF.Legalized) 442 MF.getProperties().set(MachineFunctionProperties::Property::Legalized); 443 if (YamlMF.RegBankSelected) 444 MF.getProperties().set( 445 MachineFunctionProperties::Property::RegBankSelected); 446 if (YamlMF.Selected) 447 MF.getProperties().set(MachineFunctionProperties::Property::Selected); 448 if (YamlMF.FailedISel) 449 MF.getProperties().set(MachineFunctionProperties::Property::FailedISel); 450 451 PerFunctionMIParsingState PFS(MF, SM, IRSlots, *Target); 452 if (parseRegisterInfo(PFS, YamlMF)) 453 return true; 454 if (!YamlMF.Constants.empty()) { 455 auto *ConstantPool = MF.getConstantPool(); 456 assert(ConstantPool && "Constant pool must be created"); 457 if (initializeConstantPool(PFS, *ConstantPool, YamlMF)) 458 return true; 459 } 460 461 StringRef BlockStr = YamlMF.Body.Value.Value; 462 SMDiagnostic Error; 463 SourceMgr BlockSM; 464 BlockSM.AddNewSourceBuffer( 465 MemoryBuffer::getMemBuffer(BlockStr, "",/*RequiresNullTerminator=*/false), 466 SMLoc()); 467 PFS.SM = &BlockSM; 468 if (parseMachineBasicBlockDefinitions(PFS, BlockStr, Error)) { 469 reportDiagnostic( 470 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange)); 471 return true; 472 } 473 // Check Basic Block Section Flags. 474 if (MF.getTarget().getBBSectionsType() == BasicBlockSection::Labels) { 475 MF.setBBSectionsType(BasicBlockSection::Labels); 476 } else if (MF.hasBBSections()) { 477 MF.assignBeginEndSections(); 478 } 479 PFS.SM = &SM; 480 481 // Initialize the frame information after creating all the MBBs so that the 482 // MBB references in the frame information can be resolved. 483 if (initializeFrameInfo(PFS, YamlMF)) 484 return true; 485 // Initialize the jump table after creating all the MBBs so that the MBB 486 // references can be resolved. 487 if (!YamlMF.JumpTableInfo.Entries.empty() && 488 initializeJumpTableInfo(PFS, YamlMF.JumpTableInfo)) 489 return true; 490 // Parse the machine instructions after creating all of the MBBs so that the 491 // parser can resolve the MBB references. 492 StringRef InsnStr = YamlMF.Body.Value.Value; 493 SourceMgr InsnSM; 494 InsnSM.AddNewSourceBuffer( 495 MemoryBuffer::getMemBuffer(InsnStr, "", /*RequiresNullTerminator=*/false), 496 SMLoc()); 497 PFS.SM = &InsnSM; 498 if (parseMachineInstructions(PFS, InsnStr, Error)) { 499 reportDiagnostic( 500 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange)); 501 return true; 502 } 503 PFS.SM = &SM; 504 505 if (setupRegisterInfo(PFS, YamlMF)) 506 return true; 507 508 if (YamlMF.MachineFuncInfo) { 509 const LLVMTargetMachine &TM = MF.getTarget(); 510 // Note this is called after the initial constructor of the 511 // MachineFunctionInfo based on the MachineFunction, which may depend on the 512 // IR. 513 514 SMRange SrcRange; 515 if (TM.parseMachineFunctionInfo(*YamlMF.MachineFuncInfo, PFS, Error, 516 SrcRange)) { 517 return error(Error, SrcRange); 518 } 519 } 520 521 // Set the reserved registers after parsing MachineFuncInfo. The target may 522 // have been recording information used to select the reserved registers 523 // there. 524 // FIXME: This is a temporary workaround until the reserved registers can be 525 // serialized. 526 MachineRegisterInfo &MRI = MF.getRegInfo(); 527 MRI.freezeReservedRegs(MF); 528 529 computeFunctionProperties(MF); 530 531 if (initializeCallSiteInfo(PFS, YamlMF)) 532 return false; 533 534 setupDebugValueTracking(MF, PFS, YamlMF); 535 536 MF.getSubtarget().mirFileLoaded(MF); 537 538 MF.verify(); 539 return false; 540 } 541 542 bool MIRParserImpl::parseRegisterInfo(PerFunctionMIParsingState &PFS, 543 const yaml::MachineFunction &YamlMF) { 544 MachineFunction &MF = PFS.MF; 545 MachineRegisterInfo &RegInfo = MF.getRegInfo(); 546 assert(RegInfo.tracksLiveness()); 547 if (!YamlMF.TracksRegLiveness) 548 RegInfo.invalidateLiveness(); 549 550 SMDiagnostic Error; 551 // Parse the virtual register information. 552 for (const auto &VReg : YamlMF.VirtualRegisters) { 553 VRegInfo &Info = PFS.getVRegInfo(VReg.ID.Value); 554 if (Info.Explicit) 555 return error(VReg.ID.SourceRange.Start, 556 Twine("redefinition of virtual register '%") + 557 Twine(VReg.ID.Value) + "'"); 558 Info.Explicit = true; 559 560 if (StringRef(VReg.Class.Value).equals("_")) { 561 Info.Kind = VRegInfo::GENERIC; 562 Info.D.RegBank = nullptr; 563 } else { 564 const auto *RC = Target->getRegClass(VReg.Class.Value); 565 if (RC) { 566 Info.Kind = VRegInfo::NORMAL; 567 Info.D.RC = RC; 568 } else { 569 const RegisterBank *RegBank = Target->getRegBank(VReg.Class.Value); 570 if (!RegBank) 571 return error( 572 VReg.Class.SourceRange.Start, 573 Twine("use of undefined register class or register bank '") + 574 VReg.Class.Value + "'"); 575 Info.Kind = VRegInfo::REGBANK; 576 Info.D.RegBank = RegBank; 577 } 578 } 579 580 if (!VReg.PreferredRegister.Value.empty()) { 581 if (Info.Kind != VRegInfo::NORMAL) 582 return error(VReg.Class.SourceRange.Start, 583 Twine("preferred register can only be set for normal vregs")); 584 585 if (parseRegisterReference(PFS, Info.PreferredReg, 586 VReg.PreferredRegister.Value, Error)) 587 return error(Error, VReg.PreferredRegister.SourceRange); 588 } 589 } 590 591 // Parse the liveins. 592 for (const auto &LiveIn : YamlMF.LiveIns) { 593 Register Reg; 594 if (parseNamedRegisterReference(PFS, Reg, LiveIn.Register.Value, Error)) 595 return error(Error, LiveIn.Register.SourceRange); 596 Register VReg; 597 if (!LiveIn.VirtualRegister.Value.empty()) { 598 VRegInfo *Info; 599 if (parseVirtualRegisterReference(PFS, Info, LiveIn.VirtualRegister.Value, 600 Error)) 601 return error(Error, LiveIn.VirtualRegister.SourceRange); 602 VReg = Info->VReg; 603 } 604 RegInfo.addLiveIn(Reg, VReg); 605 } 606 607 // Parse the callee saved registers (Registers that will 608 // be saved for the caller). 609 if (YamlMF.CalleeSavedRegisters) { 610 SmallVector<MCPhysReg, 16> CalleeSavedRegisters; 611 for (const auto &RegSource : YamlMF.CalleeSavedRegisters.getValue()) { 612 Register Reg; 613 if (parseNamedRegisterReference(PFS, Reg, RegSource.Value, Error)) 614 return error(Error, RegSource.SourceRange); 615 CalleeSavedRegisters.push_back(Reg); 616 } 617 RegInfo.setCalleeSavedRegs(CalleeSavedRegisters); 618 } 619 620 return false; 621 } 622 623 bool MIRParserImpl::setupRegisterInfo(const PerFunctionMIParsingState &PFS, 624 const yaml::MachineFunction &YamlMF) { 625 MachineFunction &MF = PFS.MF; 626 MachineRegisterInfo &MRI = MF.getRegInfo(); 627 bool Error = false; 628 // Create VRegs 629 auto populateVRegInfo = [&] (const VRegInfo &Info, Twine Name) { 630 Register Reg = Info.VReg; 631 switch (Info.Kind) { 632 case VRegInfo::UNKNOWN: 633 error(Twine("Cannot determine class/bank of virtual register ") + 634 Name + " in function '" + MF.getName() + "'"); 635 Error = true; 636 break; 637 case VRegInfo::NORMAL: 638 MRI.setRegClass(Reg, Info.D.RC); 639 if (Info.PreferredReg != 0) 640 MRI.setSimpleHint(Reg, Info.PreferredReg); 641 break; 642 case VRegInfo::GENERIC: 643 break; 644 case VRegInfo::REGBANK: 645 MRI.setRegBank(Reg, *Info.D.RegBank); 646 break; 647 } 648 }; 649 650 for (const auto &P : PFS.VRegInfosNamed) { 651 const VRegInfo &Info = *P.second; 652 populateVRegInfo(Info, Twine(P.first())); 653 } 654 655 for (auto P : PFS.VRegInfos) { 656 const VRegInfo &Info = *P.second; 657 populateVRegInfo(Info, Twine(P.first)); 658 } 659 660 // Compute MachineRegisterInfo::UsedPhysRegMask 661 for (const MachineBasicBlock &MBB : MF) { 662 // Make sure MRI knows about registers clobbered by unwinder. 663 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 664 if (MBB.isEHPad()) 665 if (auto *RegMask = TRI->getCustomEHPadPreservedMask(MF)) 666 MRI.addPhysRegsUsedFromRegMask(RegMask); 667 668 for (const MachineInstr &MI : MBB) { 669 for (const MachineOperand &MO : MI.operands()) { 670 if (!MO.isRegMask()) 671 continue; 672 MRI.addPhysRegsUsedFromRegMask(MO.getRegMask()); 673 } 674 } 675 } 676 677 return Error; 678 } 679 680 bool MIRParserImpl::initializeFrameInfo(PerFunctionMIParsingState &PFS, 681 const yaml::MachineFunction &YamlMF) { 682 MachineFunction &MF = PFS.MF; 683 MachineFrameInfo &MFI = MF.getFrameInfo(); 684 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering(); 685 const Function &F = MF.getFunction(); 686 const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo; 687 MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken); 688 MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken); 689 MFI.setHasStackMap(YamlMFI.HasStackMap); 690 MFI.setHasPatchPoint(YamlMFI.HasPatchPoint); 691 MFI.setStackSize(YamlMFI.StackSize); 692 MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment); 693 if (YamlMFI.MaxAlignment) 694 MFI.ensureMaxAlignment(Align(YamlMFI.MaxAlignment)); 695 MFI.setAdjustsStack(YamlMFI.AdjustsStack); 696 MFI.setHasCalls(YamlMFI.HasCalls); 697 if (YamlMFI.MaxCallFrameSize != ~0u) 698 MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize); 699 MFI.setCVBytesOfCalleeSavedRegisters(YamlMFI.CVBytesOfCalleeSavedRegisters); 700 MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment); 701 MFI.setHasVAStart(YamlMFI.HasVAStart); 702 MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc); 703 MFI.setLocalFrameSize(YamlMFI.LocalFrameSize); 704 if (!YamlMFI.SavePoint.Value.empty()) { 705 MachineBasicBlock *MBB = nullptr; 706 if (parseMBBReference(PFS, MBB, YamlMFI.SavePoint)) 707 return true; 708 MFI.setSavePoint(MBB); 709 } 710 if (!YamlMFI.RestorePoint.Value.empty()) { 711 MachineBasicBlock *MBB = nullptr; 712 if (parseMBBReference(PFS, MBB, YamlMFI.RestorePoint)) 713 return true; 714 MFI.setRestorePoint(MBB); 715 } 716 717 std::vector<CalleeSavedInfo> CSIInfo; 718 // Initialize the fixed frame objects. 719 for (const auto &Object : YamlMF.FixedStackObjects) { 720 int ObjectIdx; 721 if (Object.Type != yaml::FixedMachineStackObject::SpillSlot) 722 ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset, 723 Object.IsImmutable, Object.IsAliased); 724 else 725 ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset); 726 727 if (!TFI->isSupportedStackID(Object.StackID)) 728 return error(Object.ID.SourceRange.Start, 729 Twine("StackID is not supported by target")); 730 MFI.setStackID(ObjectIdx, Object.StackID); 731 MFI.setObjectAlignment(ObjectIdx, Object.Alignment.valueOrOne()); 732 if (!PFS.FixedStackObjectSlots.insert(std::make_pair(Object.ID.Value, 733 ObjectIdx)) 734 .second) 735 return error(Object.ID.SourceRange.Start, 736 Twine("redefinition of fixed stack object '%fixed-stack.") + 737 Twine(Object.ID.Value) + "'"); 738 if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister, 739 Object.CalleeSavedRestored, ObjectIdx)) 740 return true; 741 if (parseStackObjectsDebugInfo(PFS, Object, ObjectIdx)) 742 return true; 743 } 744 745 // Initialize the ordinary frame objects. 746 for (const auto &Object : YamlMF.StackObjects) { 747 int ObjectIdx; 748 const AllocaInst *Alloca = nullptr; 749 const yaml::StringValue &Name = Object.Name; 750 if (!Name.Value.empty()) { 751 Alloca = dyn_cast_or_null<AllocaInst>( 752 F.getValueSymbolTable()->lookup(Name.Value)); 753 if (!Alloca) 754 return error(Name.SourceRange.Start, 755 "alloca instruction named '" + Name.Value + 756 "' isn't defined in the function '" + F.getName() + 757 "'"); 758 } 759 if (!TFI->isSupportedStackID(Object.StackID)) 760 return error(Object.ID.SourceRange.Start, 761 Twine("StackID is not supported by target")); 762 if (Object.Type == yaml::MachineStackObject::VariableSized) 763 ObjectIdx = 764 MFI.CreateVariableSizedObject(Object.Alignment.valueOrOne(), Alloca); 765 else 766 ObjectIdx = MFI.CreateStackObject( 767 Object.Size, Object.Alignment.valueOrOne(), 768 Object.Type == yaml::MachineStackObject::SpillSlot, Alloca, 769 Object.StackID); 770 MFI.setObjectOffset(ObjectIdx, Object.Offset); 771 772 if (!PFS.StackObjectSlots.insert(std::make_pair(Object.ID.Value, ObjectIdx)) 773 .second) 774 return error(Object.ID.SourceRange.Start, 775 Twine("redefinition of stack object '%stack.") + 776 Twine(Object.ID.Value) + "'"); 777 if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister, 778 Object.CalleeSavedRestored, ObjectIdx)) 779 return true; 780 if (Object.LocalOffset) 781 MFI.mapLocalFrameObject(ObjectIdx, Object.LocalOffset.getValue()); 782 if (parseStackObjectsDebugInfo(PFS, Object, ObjectIdx)) 783 return true; 784 } 785 MFI.setCalleeSavedInfo(CSIInfo); 786 if (!CSIInfo.empty()) 787 MFI.setCalleeSavedInfoValid(true); 788 789 // Initialize the various stack object references after initializing the 790 // stack objects. 791 if (!YamlMFI.StackProtector.Value.empty()) { 792 SMDiagnostic Error; 793 int FI; 794 if (parseStackObjectReference(PFS, FI, YamlMFI.StackProtector.Value, Error)) 795 return error(Error, YamlMFI.StackProtector.SourceRange); 796 MFI.setStackProtectorIndex(FI); 797 } 798 return false; 799 } 800 801 bool MIRParserImpl::parseCalleeSavedRegister(PerFunctionMIParsingState &PFS, 802 std::vector<CalleeSavedInfo> &CSIInfo, 803 const yaml::StringValue &RegisterSource, bool IsRestored, int FrameIdx) { 804 if (RegisterSource.Value.empty()) 805 return false; 806 Register Reg; 807 SMDiagnostic Error; 808 if (parseNamedRegisterReference(PFS, Reg, RegisterSource.Value, Error)) 809 return error(Error, RegisterSource.SourceRange); 810 CalleeSavedInfo CSI(Reg, FrameIdx); 811 CSI.setRestored(IsRestored); 812 CSIInfo.push_back(CSI); 813 return false; 814 } 815 816 /// Verify that given node is of a certain type. Return true on error. 817 template <typename T> 818 static bool typecheckMDNode(T *&Result, MDNode *Node, 819 const yaml::StringValue &Source, 820 StringRef TypeString, MIRParserImpl &Parser) { 821 if (!Node) 822 return false; 823 Result = dyn_cast<T>(Node); 824 if (!Result) 825 return Parser.error(Source.SourceRange.Start, 826 "expected a reference to a '" + TypeString + 827 "' metadata node"); 828 return false; 829 } 830 831 template <typename T> 832 bool MIRParserImpl::parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS, 833 const T &Object, int FrameIdx) { 834 // Debug information can only be attached to stack objects; Fixed stack 835 // objects aren't supported. 836 MDNode *Var = nullptr, *Expr = nullptr, *Loc = nullptr; 837 if (parseMDNode(PFS, Var, Object.DebugVar) || 838 parseMDNode(PFS, Expr, Object.DebugExpr) || 839 parseMDNode(PFS, Loc, Object.DebugLoc)) 840 return true; 841 if (!Var && !Expr && !Loc) 842 return false; 843 DILocalVariable *DIVar = nullptr; 844 DIExpression *DIExpr = nullptr; 845 DILocation *DILoc = nullptr; 846 if (typecheckMDNode(DIVar, Var, Object.DebugVar, "DILocalVariable", *this) || 847 typecheckMDNode(DIExpr, Expr, Object.DebugExpr, "DIExpression", *this) || 848 typecheckMDNode(DILoc, Loc, Object.DebugLoc, "DILocation", *this)) 849 return true; 850 PFS.MF.setVariableDbgInfo(DIVar, DIExpr, FrameIdx, DILoc); 851 return false; 852 } 853 854 bool MIRParserImpl::parseMDNode(PerFunctionMIParsingState &PFS, 855 MDNode *&Node, const yaml::StringValue &Source) { 856 if (Source.Value.empty()) 857 return false; 858 SMDiagnostic Error; 859 if (llvm::parseMDNode(PFS, Node, Source.Value, Error)) 860 return error(Error, Source.SourceRange); 861 return false; 862 } 863 864 bool MIRParserImpl::initializeConstantPool(PerFunctionMIParsingState &PFS, 865 MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF) { 866 DenseMap<unsigned, unsigned> &ConstantPoolSlots = PFS.ConstantPoolSlots; 867 const MachineFunction &MF = PFS.MF; 868 const auto &M = *MF.getFunction().getParent(); 869 SMDiagnostic Error; 870 for (const auto &YamlConstant : YamlMF.Constants) { 871 if (YamlConstant.IsTargetSpecific) 872 // FIXME: Support target-specific constant pools 873 return error(YamlConstant.Value.SourceRange.Start, 874 "Can't parse target-specific constant pool entries yet"); 875 const Constant *Value = dyn_cast_or_null<Constant>( 876 parseConstantValue(YamlConstant.Value.Value, Error, M)); 877 if (!Value) 878 return error(Error, YamlConstant.Value.SourceRange); 879 const Align PrefTypeAlign = 880 M.getDataLayout().getPrefTypeAlign(Value->getType()); 881 const Align Alignment = YamlConstant.Alignment.getValueOr(PrefTypeAlign); 882 unsigned Index = ConstantPool.getConstantPoolIndex(Value, Alignment); 883 if (!ConstantPoolSlots.insert(std::make_pair(YamlConstant.ID.Value, Index)) 884 .second) 885 return error(YamlConstant.ID.SourceRange.Start, 886 Twine("redefinition of constant pool item '%const.") + 887 Twine(YamlConstant.ID.Value) + "'"); 888 } 889 return false; 890 } 891 892 bool MIRParserImpl::initializeJumpTableInfo(PerFunctionMIParsingState &PFS, 893 const yaml::MachineJumpTable &YamlJTI) { 894 MachineJumpTableInfo *JTI = PFS.MF.getOrCreateJumpTableInfo(YamlJTI.Kind); 895 for (const auto &Entry : YamlJTI.Entries) { 896 std::vector<MachineBasicBlock *> Blocks; 897 for (const auto &MBBSource : Entry.Blocks) { 898 MachineBasicBlock *MBB = nullptr; 899 if (parseMBBReference(PFS, MBB, MBBSource.Value)) 900 return true; 901 Blocks.push_back(MBB); 902 } 903 unsigned Index = JTI->createJumpTableIndex(Blocks); 904 if (!PFS.JumpTableSlots.insert(std::make_pair(Entry.ID.Value, Index)) 905 .second) 906 return error(Entry.ID.SourceRange.Start, 907 Twine("redefinition of jump table entry '%jump-table.") + 908 Twine(Entry.ID.Value) + "'"); 909 } 910 return false; 911 } 912 913 bool MIRParserImpl::parseMBBReference(PerFunctionMIParsingState &PFS, 914 MachineBasicBlock *&MBB, 915 const yaml::StringValue &Source) { 916 SMDiagnostic Error; 917 if (llvm::parseMBBReference(PFS, MBB, Source.Value, Error)) 918 return error(Error, Source.SourceRange); 919 return false; 920 } 921 922 SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error, 923 SMRange SourceRange) { 924 assert(SourceRange.isValid() && "Invalid source range"); 925 SMLoc Loc = SourceRange.Start; 926 bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() && 927 *Loc.getPointer() == '\''; 928 // Translate the location of the error from the location in the MI string to 929 // the corresponding location in the MIR file. 930 Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() + 931 (HasQuote ? 1 : 0)); 932 933 // TODO: Translate any source ranges as well. 934 return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None, 935 Error.getFixIts()); 936 } 937 938 SMDiagnostic MIRParserImpl::diagFromBlockStringDiag(const SMDiagnostic &Error, 939 SMRange SourceRange) { 940 assert(SourceRange.isValid()); 941 942 // Translate the location of the error from the location in the llvm IR string 943 // to the corresponding location in the MIR file. 944 auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start); 945 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1; 946 unsigned Column = Error.getColumnNo(); 947 StringRef LineStr = Error.getLineContents(); 948 SMLoc Loc = Error.getLoc(); 949 950 // Get the full line and adjust the column number by taking the indentation of 951 // LLVM IR into account. 952 for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E; 953 L != E; ++L) { 954 if (L.line_number() == Line) { 955 LineStr = *L; 956 Loc = SMLoc::getFromPointer(LineStr.data()); 957 auto Indent = LineStr.find(Error.getLineContents()); 958 if (Indent != StringRef::npos) 959 Column += Indent; 960 break; 961 } 962 } 963 964 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(), 965 Error.getMessage(), LineStr, Error.getRanges(), 966 Error.getFixIts()); 967 } 968 969 MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl) 970 : Impl(std::move(Impl)) {} 971 972 MIRParser::~MIRParser() {} 973 974 std::unique_ptr<Module> 975 MIRParser::parseIRModule(DataLayoutCallbackTy DataLayoutCallback) { 976 return Impl->parseIRModule(DataLayoutCallback); 977 } 978 979 bool MIRParser::parseMachineFunctions(Module &M, MachineModuleInfo &MMI) { 980 return Impl->parseMachineFunctions(M, MMI); 981 } 982 983 std::unique_ptr<MIRParser> llvm::createMIRParserFromFile( 984 StringRef Filename, SMDiagnostic &Error, LLVMContext &Context, 985 std::function<void(Function &)> ProcessIRFunction) { 986 auto FileOrErr = MemoryBuffer::getFileOrSTDIN(Filename); 987 if (std::error_code EC = FileOrErr.getError()) { 988 Error = SMDiagnostic(Filename, SourceMgr::DK_Error, 989 "Could not open input file: " + EC.message()); 990 return nullptr; 991 } 992 return createMIRParser(std::move(FileOrErr.get()), Context, 993 ProcessIRFunction); 994 } 995 996 std::unique_ptr<MIRParser> 997 llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents, 998 LLVMContext &Context, 999 std::function<void(Function &)> ProcessIRFunction) { 1000 auto Filename = Contents->getBufferIdentifier(); 1001 if (Context.shouldDiscardValueNames()) { 1002 Context.diagnose(DiagnosticInfoMIRParser( 1003 DS_Error, 1004 SMDiagnostic( 1005 Filename, SourceMgr::DK_Error, 1006 "Can't read MIR with a Context that discards named Values"))); 1007 return nullptr; 1008 } 1009 return std::make_unique<MIRParser>(std::make_unique<MIRParserImpl>( 1010 std::move(Contents), Filename, Context, ProcessIRFunction)); 1011 } 1012