.Case("undef", MIToken::kw_undef)
.Case("frame-setup", MIToken::kw_frame_setup)
.Case("debug-location", MIToken::kw_debug_location)
+ .Case(".cfi_offset", MIToken::kw_cfi_offset)
.Case(".cfi_def_cfa_offset", MIToken::kw_cfi_def_cfa_offset)
.Default(MIToken::Identifier);
}
kw_undef,
kw_frame_setup,
kw_debug_location,
+ kw_cfi_offset,
kw_cfi_def_cfa_offset,
// Identifier tokens
bool parseMDNode(MDNode *&Node);
bool parseMetadataOperand(MachineOperand &Dest);
bool parseCFIOffset(int &Offset);
+ bool parseCFIRegister(unsigned &Reg);
bool parseCFIOperand(MachineOperand &Dest);
bool parseMachineOperand(MachineOperand &Dest);
/// Return true if an error occurred.
bool getUnsigned(unsigned &Result);
+ /// If the current token is of the given kind, consume it and return false.
+ /// Otherwise report an error and return true.
+ bool expectAndConsume(MIToken::TokenKind TokenKind);
+
void initNames2InstrOpCodes();
/// Try to convert an instruction name to an opcode. Return true if the
return true;
}
+static const char *toString(MIToken::TokenKind TokenKind) {
+ switch (TokenKind) {
+ case MIToken::comma:
+ return "','";
+ default:
+ return "<unknown token>";
+ }
+}
+
+bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
+ if (Token.isNot(TokenKind))
+ return error(Twine("expected ") + toString(TokenKind));
+ lex();
+ return false;
+}
+
bool MIParser::parse(MachineInstr *&MI) {
lex();
return false;
}
+bool MIParser::parseCFIRegister(unsigned &Reg) {
+ if (Token.isNot(MIToken::NamedRegister))
+ return error("expected a cfi register");
+ unsigned LLVMReg;
+ if (parseRegister(LLVMReg))
+ return true;
+ const auto *TRI = MF.getSubtarget().getRegisterInfo();
+ assert(TRI && "Expected target register info");
+ int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
+ if (DwarfReg < 0)
+ return error("invalid DWARF register");
+ Reg = (unsigned)DwarfReg;
+ lex();
+ return false;
+}
+
bool MIParser::parseCFIOperand(MachineOperand &Dest) {
- // TODO: Parse the other CFI operands.
- assert(Token.is(MIToken::kw_cfi_def_cfa_offset));
+ auto Kind = Token.kind();
lex();
+ auto &MMI = MF.getMMI();
int Offset;
- if (parseCFIOffset(Offset))
- return true;
- // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
- Dest = MachineOperand::CreateCFIIndex(MF.getMMI().addFrameInst(
- MCCFIInstruction::createDefCfaOffset(nullptr, -Offset)));
+ unsigned Reg;
+ unsigned CFIIndex;
+ switch (Kind) {
+ case MIToken::kw_cfi_offset:
+ if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
+ parseCFIOffset(Offset))
+ return true;
+ CFIIndex =
+ MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
+ break;
+ case MIToken::kw_cfi_def_cfa_offset:
+ if (parseCFIOffset(Offset))
+ return true;
+ // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
+ CFIIndex = MMI.addFrameInst(
+ MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
+ break;
+ default:
+ // TODO: Parse the other CFI operands.
+ llvm_unreachable("The current token should be a cfi operand");
+ }
+ Dest = MachineOperand::CreateCFIIndex(CFIIndex);
return false;
}
return parseExternalSymbolOperand(Dest);
case MIToken::exclaim:
return parseMetadataOperand(Dest);
+ case MIToken::kw_cfi_offset:
case MIToken::kw_cfi_def_cfa_offset:
return parseCFIOperand(Dest);
case MIToken::Error:
void printStackObjectReference(int FrameIndex);
void print(const MachineOperand &Op, const TargetRegisterInfo *TRI);
- void print(const MCCFIInstruction &CFI);
+ void print(const MCCFIInstruction &CFI, const TargetRegisterInfo *TRI);
};
} // end anonymous namespace
break;
case MachineOperand::MO_CFIIndex: {
const auto &MMI = Op.getParent()->getParent()->getParent()->getMMI();
- print(MMI.getFrameInstructions()[Op.getCFIIndex()]);
+ print(MMI.getFrameInstructions()[Op.getCFIIndex()], TRI);
break;
}
default:
}
}
-void MIPrinter::print(const MCCFIInstruction &CFI) {
+static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
+ const TargetRegisterInfo *TRI) {
+ int Reg = TRI->getLLVMRegNum(DwarfReg, true);
+ if (Reg == -1) {
+ OS << "<badreg>";
+ return;
+ }
+ printReg(Reg, OS, TRI);
+}
+
+void MIPrinter::print(const MCCFIInstruction &CFI,
+ const TargetRegisterInfo *TRI) {
switch (CFI.getOperation()) {
+ case MCCFIInstruction::OpOffset:
+ OS << ".cfi_offset ";
+ if (CFI.getLabel())
+ OS << "<mcsymbol> ";
+ printCFIRegister(CFI.getRegister(), OS, TRI);
+ OS << ", " << CFI.getOffset();
+ break;
case MCCFIInstruction::OpDefCfaOffset:
OS << ".cfi_def_cfa_offset ";
if (CFI.getLabel())
--- /dev/null
+# RUN: llc -march=x86-64 -start-after branch-folder -stop-after branch-folder -o /dev/null %s | FileCheck %s
+# This test ensures that the MIR parser parses the .cfi_offset operands
+# correctly.
+
+--- |
+
+ declare void @foo(i32)
+
+ define i32 @test(i32 %a, i32 %b, i32 %c, i32 %d) {
+ entry:
+ %add = add nsw i32 %b, %a
+ %add1 = add nsw i32 %add, %c
+ %add2 = add nsw i32 %add1, %d
+ tail call void @foo(i32 %add2)
+ %add6 = add nsw i32 %add2, %add2
+ ret i32 %add6
+ }
+
+...
+---
+name: test
+tracksRegLiveness: true
+frameInfo:
+ stackSize: 8
+ adjustsStack: true
+ hasCalls: true
+fixedStack:
+ - { id: 0, type: spill-slot, offset: -16, size: 8, alignment: 16 }
+body:
+ - id: 0
+ name: entry
+ instructions:
+ - 'PUSH64r killed %rbx, implicit-def %rsp, implicit %rsp'
+ - 'CFI_INSTRUCTION .cfi_def_cfa_offset 16'
+ # CHECK: CFI_INSTRUCTION .cfi_offset %rbx, -16
+ - 'CFI_INSTRUCTION .cfi_offset %rbx, -16'
+ - '%ebx = COPY %edi, implicit-def %rbx'
+ - '%ebx = ADD32rr %ebx, killed %esi, implicit-def dead %eflags'
+ - '%ebx = ADD32rr %ebx, killed %edx, implicit-def dead %eflags'
+ - '%ebx = ADD32rr %ebx, killed %ecx, implicit-def dead %eflags'
+ - '%edi = COPY %ebx'
+ - 'CALL64pcrel32 @foo, csr_64, implicit %rsp, implicit %edi, implicit-def %rsp'
+ - '%eax = LEA64_32r killed %rbx, 1, %rbx, 0, _'
+ - '%rbx = POP64r implicit-def %rsp, implicit %rsp'
+ - 'RETQ %eax'
+...
+
--- /dev/null
+# RUN: not llc -march=x86-64 -start-after branch-folder -stop-after branch-folder -o /dev/null %s 2>&1 | FileCheck %s
+
+--- |
+
+ declare void @foo(i32)
+
+ define i32 @test(i32 %a, i32 %b, i32 %c, i32 %d) {
+ entry:
+ %add = add nsw i32 %b, %a
+ %add1 = add nsw i32 %add, %c
+ %add2 = add nsw i32 %add1, %d
+ tail call void @foo(i32 %add2)
+ %add6 = add nsw i32 %add2, %add2
+ ret i32 %add6
+ }
+
+...
+---
+name: test
+tracksRegLiveness: true
+frameInfo:
+ stackSize: 8
+ adjustsStack: true
+ hasCalls: true
+fixedStack:
+ - { id: 0, type: spill-slot, offset: -16, size: 8, alignment: 16 }
+body:
+ - id: 0
+ name: entry
+ instructions:
+ - 'PUSH64r killed %rbx, implicit-def %rsp, implicit %rsp'
+ - 'CFI_INSTRUCTION .cfi_def_cfa_offset 16'
+ # CHECK: [[@LINE+1]]:43: expected ','
+ - 'CFI_INSTRUCTION .cfi_offset %rbx -16'
+ - '%ebx = COPY %edi, implicit-def %rbx'
+ - '%ebx = ADD32rr %ebx, killed %esi, implicit-def dead %eflags'
+ - '%ebx = ADD32rr %ebx, killed %edx, implicit-def dead %eflags'
+ - '%ebx = ADD32rr %ebx, killed %ecx, implicit-def dead %eflags'
+ - '%edi = COPY %ebx'
+ - 'CALL64pcrel32 @foo, csr_64, implicit %rsp, implicit %edi, implicit-def %rsp'
+ - '%eax = LEA64_32r killed %rbx, 1, %rbx, 0, _'
+ - '%rbx = POP64r implicit-def %rsp, implicit %rsp'
+ - 'RETQ %eax'
+...
--- /dev/null
+# RUN: not llc -march=x86-64 -start-after branch-folder -stop-after branch-folder -o /dev/null %s 2>&1 | FileCheck %s
+
+--- |
+
+ declare void @foo(i32)
+
+ define i32 @test(i32 %a, i32 %b, i32 %c, i32 %d) {
+ entry:
+ %add = add nsw i32 %b, %a
+ %add1 = add nsw i32 %add, %c
+ %add2 = add nsw i32 %add1, %d
+ tail call void @foo(i32 %add2)
+ %add6 = add nsw i32 %add2, %add2
+ ret i32 %add6
+ }
+
+...
+---
+name: test
+tracksRegLiveness: true
+frameInfo:
+ stackSize: 8
+ adjustsStack: true
+ hasCalls: true
+fixedStack:
+ - { id: 0, type: spill-slot, offset: -16, size: 8, alignment: 16 }
+body:
+ - id: 0
+ name: entry
+ instructions:
+ - 'PUSH64r killed %rbx, implicit-def %rsp, implicit %rsp'
+ - 'CFI_INSTRUCTION .cfi_def_cfa_offset 16'
+ # CHECK: [[@LINE+1]]:38: expected a cfi register
+ - 'CFI_INSTRUCTION .cfi_offset %0, -16'
+ - '%ebx = COPY %edi, implicit-def %rbx'
+ - '%ebx = ADD32rr %ebx, killed %esi, implicit-def dead %eflags'
+ - '%ebx = ADD32rr %ebx, killed %edx, implicit-def dead %eflags'
+ - '%ebx = ADD32rr %ebx, killed %ecx, implicit-def dead %eflags'
+ - '%edi = COPY %ebx'
+ - 'CALL64pcrel32 @foo, csr_64, implicit %rsp, implicit %edi, implicit-def %rsp'
+ - '%eax = LEA64_32r killed %rbx, 1, %rbx, 0, _'
+ - '%rbx = POP64r implicit-def %rsp, implicit %rsp'
+ - 'RETQ %eax'
+...