Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit fcfc7b2

Browse files
committed
[CodeGen] Move printing MO_CFIIndex operands to MachineOperand::print
Work towards the unification of MIR and debug output by refactoring the interfaces. Before this patch we printed "<call frame instruction>" in the debug output. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@321084 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 0dbbce2 commit fcfc7b2

File tree

5 files changed

+164
-121
lines changed

5 files changed

+164
-121
lines changed

docs/MIRLangRef.rst

+21-1
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,27 @@ The syntax is:
692692
693693
EH_LABEL <mcsymbol Ltmp1>
694694
695+
CFIIndex Operands
696+
^^^^^^^^^^^^^^^^^
697+
698+
A CFI Index operand is holding an index into a per-function side-table,
699+
``MachineFunction::getFrameInstructions()``, which references all the frame
700+
instructions in a ``MachineFunction``. A ``CFI_INSTRUCTION`` may look like it
701+
contains multiple operands, but the only operand it contains is the CFI Index.
702+
The other operands are tracked by the ``MCCFIInstruction`` object.
703+
704+
The syntax is:
705+
706+
.. code-block:: text
707+
708+
CFI_INSTRUCTION offset %w30, -16
709+
710+
which may be emitted later in the MC layer as:
711+
712+
.. code-block:: text
713+
714+
.cfi_offset w30, -16
715+
695716
.. TODO: Describe the parsers default behaviour when optional YAML attributes
696717
are missing.
697718
.. TODO: Describe the syntax for the bundled instructions.
@@ -702,7 +723,6 @@ The syntax is:
702723
.. TODO: Describe the syntax of the stack object machine operands and their
703724
YAML definitions.
704725
.. TODO: Describe the syntax of the block address machine operands.
705-
.. TODO: Describe the syntax of the CFI index machine operands.
706726
.. TODO: Describe the syntax of the metadata machine operands, and the
707727
instructions debug location attribute.
708728
.. TODO: Describe the syntax of the register live out machine operands.

include/llvm/CodeGen/MachineOperand.h

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class GlobalValue;
2929
class MachineBasicBlock;
3030
class MachineInstr;
3131
class MachineRegisterInfo;
32+
class MCCFIInstruction;
3233
class MDNode;
3334
class ModuleSlotTracker;
3435
class TargetMachine;

lib/CodeGen/MIRPrinter.cpp

+2-118
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,8 @@ void MIPrinter::print(const MachineInstr &MI, unsigned OpIdx,
795795
case MachineOperand::MO_GlobalAddress:
796796
case MachineOperand::MO_RegisterLiveOut:
797797
case MachineOperand::MO_Metadata:
798-
case MachineOperand::MO_MCSymbol: {
798+
case MachineOperand::MO_MCSymbol:
799+
case MachineOperand::MO_CFIIndex: {
799800
unsigned TiedOperandIdx = 0;
800801
if (ShouldPrintRegisterTies && Op.isReg() && Op.isTied() && !Op.isDef())
801802
TiedOperandIdx = Op.getParent()->findTiedOperandIdx(OpIdx);
@@ -827,11 +828,6 @@ void MIPrinter::print(const MachineInstr &MI, unsigned OpIdx,
827828
printCustomRegMask(Op.getRegMask(), OS, TRI);
828829
break;
829830
}
830-
case MachineOperand::MO_CFIIndex: {
831-
const MachineFunction &MF = *Op.getParent()->getMF();
832-
print(MF.getFrameInstructions()[Op.getCFIIndex()], TRI);
833-
break;
834-
}
835831
case MachineOperand::MO_IntrinsicID: {
836832
Intrinsic::ID ID = Op.getIntrinsicID();
837833
if (ID < Intrinsic::num_intrinsics)
@@ -978,118 +974,6 @@ void MIPrinter::printSyncScope(const LLVMContext &Context, SyncScope::ID SSID) {
978974
}
979975
}
980976

981-
static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
982-
const TargetRegisterInfo *TRI) {
983-
int Reg = TRI->getLLVMRegNum(DwarfReg, true);
984-
if (Reg == -1) {
985-
OS << "<badreg>";
986-
return;
987-
}
988-
OS << printReg(Reg, TRI);
989-
}
990-
991-
void MIPrinter::print(const MCCFIInstruction &CFI,
992-
const TargetRegisterInfo *TRI) {
993-
switch (CFI.getOperation()) {
994-
case MCCFIInstruction::OpSameValue:
995-
OS << "same_value ";
996-
if (MCSymbol *Label = CFI.getLabel())
997-
MachineOperand::printSymbol(OS, *Label);
998-
printCFIRegister(CFI.getRegister(), OS, TRI);
999-
break;
1000-
case MCCFIInstruction::OpRememberState:
1001-
OS << "remember_state ";
1002-
if (MCSymbol *Label = CFI.getLabel())
1003-
MachineOperand::printSymbol(OS, *Label);
1004-
break;
1005-
case MCCFIInstruction::OpRestoreState:
1006-
OS << "restore_state ";
1007-
if (MCSymbol *Label = CFI.getLabel())
1008-
MachineOperand::printSymbol(OS, *Label);
1009-
break;
1010-
case MCCFIInstruction::OpOffset:
1011-
OS << "offset ";
1012-
if (MCSymbol *Label = CFI.getLabel())
1013-
MachineOperand::printSymbol(OS, *Label);
1014-
printCFIRegister(CFI.getRegister(), OS, TRI);
1015-
OS << ", " << CFI.getOffset();
1016-
break;
1017-
case MCCFIInstruction::OpDefCfaRegister:
1018-
OS << "def_cfa_register ";
1019-
if (MCSymbol *Label = CFI.getLabel())
1020-
MachineOperand::printSymbol(OS, *Label);
1021-
printCFIRegister(CFI.getRegister(), OS, TRI);
1022-
break;
1023-
case MCCFIInstruction::OpDefCfaOffset:
1024-
OS << "def_cfa_offset ";
1025-
if (MCSymbol *Label = CFI.getLabel())
1026-
MachineOperand::printSymbol(OS, *Label);
1027-
OS << CFI.getOffset();
1028-
break;
1029-
case MCCFIInstruction::OpDefCfa:
1030-
OS << "def_cfa ";
1031-
if (MCSymbol *Label = CFI.getLabel())
1032-
MachineOperand::printSymbol(OS, *Label);
1033-
printCFIRegister(CFI.getRegister(), OS, TRI);
1034-
OS << ", " << CFI.getOffset();
1035-
break;
1036-
case MCCFIInstruction::OpRelOffset:
1037-
OS << "rel_offset ";
1038-
if (MCSymbol *Label = CFI.getLabel())
1039-
MachineOperand::printSymbol(OS, *Label);
1040-
printCFIRegister(CFI.getRegister(), OS, TRI);
1041-
OS << ", " << CFI.getOffset();
1042-
break;
1043-
case MCCFIInstruction::OpAdjustCfaOffset:
1044-
OS << "adjust_cfa_offset ";
1045-
if (MCSymbol *Label = CFI.getLabel())
1046-
MachineOperand::printSymbol(OS, *Label);
1047-
OS << CFI.getOffset();
1048-
break;
1049-
case MCCFIInstruction::OpRestore:
1050-
OS << "restore ";
1051-
if (MCSymbol *Label = CFI.getLabel())
1052-
MachineOperand::printSymbol(OS, *Label);
1053-
printCFIRegister(CFI.getRegister(), OS, TRI);
1054-
break;
1055-
case MCCFIInstruction::OpEscape: {
1056-
OS << "escape ";
1057-
if (MCSymbol *Label = CFI.getLabel())
1058-
MachineOperand::printSymbol(OS, *Label);
1059-
if (!CFI.getValues().empty()) {
1060-
size_t e = CFI.getValues().size() - 1;
1061-
for (size_t i = 0; i < e; ++i)
1062-
OS << format("0x%02x", uint8_t(CFI.getValues()[i])) << ", ";
1063-
OS << format("0x%02x", uint8_t(CFI.getValues()[e])) << ", ";
1064-
}
1065-
break;
1066-
}
1067-
case MCCFIInstruction::OpUndefined:
1068-
OS << "undefined ";
1069-
if (MCSymbol *Label = CFI.getLabel())
1070-
MachineOperand::printSymbol(OS, *Label);
1071-
printCFIRegister(CFI.getRegister(), OS, TRI);
1072-
break;
1073-
case MCCFIInstruction::OpRegister:
1074-
OS << "register ";
1075-
if (MCSymbol *Label = CFI.getLabel())
1076-
MachineOperand::printSymbol(OS, *Label);
1077-
printCFIRegister(CFI.getRegister(), OS, TRI);
1078-
OS << ", ";
1079-
printCFIRegister(CFI.getRegister2(), OS, TRI);
1080-
break;
1081-
case MCCFIInstruction::OpWindowSave:
1082-
OS << "window_save ";
1083-
if (MCSymbol *Label = CFI.getLabel())
1084-
MachineOperand::printSymbol(OS, *Label);
1085-
break;
1086-
default:
1087-
// TODO: Print the other CFI Operations.
1088-
OS << "<unserializable cfi operation>";
1089-
break;
1090-
}
1091-
}
1092-
1093977
void llvm::printMIR(raw_ostream &OS, const Module &M) {
1094978
yaml::Output Out(OS);
1095979
Out << const_cast<Module &>(M);

lib/CodeGen/MachineOperand.cpp

+123-2
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,21 @@ static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
412412
return nullptr;
413413
}
414414

415+
static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
416+
const TargetRegisterInfo *TRI) {
417+
if (!TRI) {
418+
OS << "%dwarfreg." << DwarfReg;
419+
return;
420+
}
421+
422+
int Reg = TRI->getLLVMRegNum(DwarfReg, true);
423+
if (Reg == -1) {
424+
OS << "<badreg>";
425+
return;
426+
}
427+
OS << printReg(Reg, TRI);
428+
}
429+
415430
void MachineOperand::printSubregIdx(raw_ostream &OS, uint64_t Index,
416431
const TargetRegisterInfo *TRI) {
417432
OS << "%subreg.";
@@ -490,6 +505,108 @@ void MachineOperand::printStackObjectReference(raw_ostream &OS,
490505
OS << '.' << Name;
491506
}
492507

508+
static void printCFI(raw_ostream &OS, const MCCFIInstruction &CFI,
509+
const TargetRegisterInfo *TRI) {
510+
switch (CFI.getOperation()) {
511+
case MCCFIInstruction::OpSameValue:
512+
OS << "same_value ";
513+
if (MCSymbol *Label = CFI.getLabel())
514+
MachineOperand::printSymbol(OS, *Label);
515+
printCFIRegister(CFI.getRegister(), OS, TRI);
516+
break;
517+
case MCCFIInstruction::OpRememberState:
518+
OS << "remember_state ";
519+
if (MCSymbol *Label = CFI.getLabel())
520+
MachineOperand::printSymbol(OS, *Label);
521+
break;
522+
case MCCFIInstruction::OpRestoreState:
523+
OS << "restore_state ";
524+
if (MCSymbol *Label = CFI.getLabel())
525+
MachineOperand::printSymbol(OS, *Label);
526+
break;
527+
case MCCFIInstruction::OpOffset:
528+
OS << "offset ";
529+
if (MCSymbol *Label = CFI.getLabel())
530+
MachineOperand::printSymbol(OS, *Label);
531+
printCFIRegister(CFI.getRegister(), OS, TRI);
532+
OS << ", " << CFI.getOffset();
533+
break;
534+
case MCCFIInstruction::OpDefCfaRegister:
535+
OS << "def_cfa_register ";
536+
if (MCSymbol *Label = CFI.getLabel())
537+
MachineOperand::printSymbol(OS, *Label);
538+
printCFIRegister(CFI.getRegister(), OS, TRI);
539+
break;
540+
case MCCFIInstruction::OpDefCfaOffset:
541+
OS << "def_cfa_offset ";
542+
if (MCSymbol *Label = CFI.getLabel())
543+
MachineOperand::printSymbol(OS, *Label);
544+
OS << CFI.getOffset();
545+
break;
546+
case MCCFIInstruction::OpDefCfa:
547+
OS << "def_cfa ";
548+
if (MCSymbol *Label = CFI.getLabel())
549+
MachineOperand::printSymbol(OS, *Label);
550+
printCFIRegister(CFI.getRegister(), OS, TRI);
551+
OS << ", " << CFI.getOffset();
552+
break;
553+
case MCCFIInstruction::OpRelOffset:
554+
OS << "rel_offset ";
555+
if (MCSymbol *Label = CFI.getLabel())
556+
MachineOperand::printSymbol(OS, *Label);
557+
printCFIRegister(CFI.getRegister(), OS, TRI);
558+
OS << ", " << CFI.getOffset();
559+
break;
560+
case MCCFIInstruction::OpAdjustCfaOffset:
561+
OS << "adjust_cfa_offset ";
562+
if (MCSymbol *Label = CFI.getLabel())
563+
MachineOperand::printSymbol(OS, *Label);
564+
OS << CFI.getOffset();
565+
break;
566+
case MCCFIInstruction::OpRestore:
567+
OS << "restore ";
568+
if (MCSymbol *Label = CFI.getLabel())
569+
MachineOperand::printSymbol(OS, *Label);
570+
printCFIRegister(CFI.getRegister(), OS, TRI);
571+
break;
572+
case MCCFIInstruction::OpEscape: {
573+
OS << "escape ";
574+
if (MCSymbol *Label = CFI.getLabel())
575+
MachineOperand::printSymbol(OS, *Label);
576+
if (!CFI.getValues().empty()) {
577+
size_t e = CFI.getValues().size() - 1;
578+
for (size_t i = 0; i < e; ++i)
579+
OS << format("0x%02x", uint8_t(CFI.getValues()[i])) << ", ";
580+
OS << format("0x%02x", uint8_t(CFI.getValues()[e])) << ", ";
581+
}
582+
break;
583+
}
584+
case MCCFIInstruction::OpUndefined:
585+
OS << "undefined ";
586+
if (MCSymbol *Label = CFI.getLabel())
587+
MachineOperand::printSymbol(OS, *Label);
588+
printCFIRegister(CFI.getRegister(), OS, TRI);
589+
break;
590+
case MCCFIInstruction::OpRegister:
591+
OS << "register ";
592+
if (MCSymbol *Label = CFI.getLabel())
593+
MachineOperand::printSymbol(OS, *Label);
594+
printCFIRegister(CFI.getRegister(), OS, TRI);
595+
OS << ", ";
596+
printCFIRegister(CFI.getRegister2(), OS, TRI);
597+
break;
598+
case MCCFIInstruction::OpWindowSave:
599+
OS << "window_save ";
600+
if (MCSymbol *Label = CFI.getLabel())
601+
MachineOperand::printSymbol(OS, *Label);
602+
break;
603+
default:
604+
// TODO: Print the other CFI Operations.
605+
OS << "<unserializable cfi directive>";
606+
break;
607+
}
608+
}
609+
493610
void MachineOperand::print(raw_ostream &OS, const TargetRegisterInfo *TRI,
494611
const TargetIntrinsicInfo *IntrinsicInfo) const {
495612
tryToGetTargetInfo(*this, TRI, IntrinsicInfo);
@@ -693,9 +810,13 @@ void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
693810
case MachineOperand::MO_MCSymbol:
694811
printSymbol(OS, *getMCSymbol());
695812
break;
696-
case MachineOperand::MO_CFIIndex:
697-
OS << "<call frame instruction>";
813+
case MachineOperand::MO_CFIIndex: {
814+
if (const MachineFunction *MF = getMFIfAvailable(*this))
815+
printCFI(OS, MF->getFrameInstructions()[getCFIIndex()], TRI);
816+
else
817+
OS << "<cfi directive>";
698818
break;
819+
}
699820
case MachineOperand::MO_IntrinsicID: {
700821
Intrinsic::ID ID = getIntrinsicID();
701822
if (ID < Intrinsic::num_intrinsics)

unittests/CodeGen/MachineOperandTest.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -336,4 +336,21 @@ TEST(MachineOperandTest, PrintMCSymbol) {
336336
ASSERT_TRUE(OS.str() == "<mcsymbol foo>");
337337
}
338338

339+
TEST(MachineOperandTest, PrintCFI) {
340+
// Create a MachineOperand with a CFI index but no function and print it.
341+
MachineOperand MO = MachineOperand::CreateCFIIndex(8);
342+
343+
// Checking some preconditions on the newly created
344+
// MachineOperand.
345+
ASSERT_TRUE(MO.isCFIIndex());
346+
ASSERT_TRUE(MO.getCFIIndex() == 8);
347+
348+
std::string str;
349+
// Print a MachineOperand containing a CFI Index node but no machine function
350+
// attached to it.
351+
raw_string_ostream OS(str);
352+
MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
353+
ASSERT_TRUE(OS.str() == "<cfi directive>");
354+
}
355+
339356
} // end namespace

0 commit comments

Comments
 (0)