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