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