[llvm] r243062 - MIR Serialization: Serialize the '.cfi_offset' CFI instruction.
Alex Lorenz
arphaman at gmail.com
Thu Jul 23 16:09:08 PDT 2015
Author: arphaman
Date: Thu Jul 23 18:09:07 2015
New Revision: 243062
URL: http://llvm.org/viewvc/llvm-project?rev=243062&view=rev
Log:
MIR Serialization: Serialize the '.cfi_offset' CFI instruction.
Reviewers: Duncan P. N. Exon Smith
Added:
llvm/trunk/test/CodeGen/MIR/X86/cfi-offset.mir
llvm/trunk/test/CodeGen/MIR/X86/expected-comma-after-cfi-register.mir
llvm/trunk/test/CodeGen/MIR/X86/expected-register-after-cfi-operand.mir
Modified:
llvm/trunk/lib/CodeGen/MIRParser/MILexer.cpp
llvm/trunk/lib/CodeGen/MIRParser/MILexer.h
llvm/trunk/lib/CodeGen/MIRParser/MIParser.cpp
llvm/trunk/lib/CodeGen/MIRPrinter.cpp
Modified: llvm/trunk/lib/CodeGen/MIRParser/MILexer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MIRParser/MILexer.cpp?rev=243062&r1=243061&r2=243062&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/MIRParser/MILexer.cpp (original)
+++ llvm/trunk/lib/CodeGen/MIRParser/MILexer.cpp Thu Jul 23 18:09:07 2015
@@ -124,6 +124,7 @@ static MIToken::TokenKind getIdentifierK
.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);
}
Modified: llvm/trunk/lib/CodeGen/MIRParser/MILexer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MIRParser/MILexer.h?rev=243062&r1=243061&r2=243062&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/MIRParser/MILexer.h (original)
+++ llvm/trunk/lib/CodeGen/MIRParser/MILexer.h Thu Jul 23 18:09:07 2015
@@ -46,6 +46,7 @@ struct MIToken {
kw_undef,
kw_frame_setup,
kw_debug_location,
+ kw_cfi_offset,
kw_cfi_def_cfa_offset,
// Identifier tokens
Modified: llvm/trunk/lib/CodeGen/MIRParser/MIParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MIRParser/MIParser.cpp?rev=243062&r1=243061&r2=243062&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/MIRParser/MIParser.cpp (original)
+++ llvm/trunk/lib/CodeGen/MIRParser/MIParser.cpp Thu Jul 23 18:09:07 2015
@@ -115,6 +115,7 @@ public:
bool parseMDNode(MDNode *&Node);
bool parseMetadataOperand(MachineOperand &Dest);
bool parseCFIOffset(int &Offset);
+ bool parseCFIRegister(unsigned &Reg);
bool parseCFIOperand(MachineOperand &Dest);
bool parseMachineOperand(MachineOperand &Dest);
@@ -124,6 +125,10 @@ private:
/// 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
@@ -181,6 +186,22 @@ bool MIParser::error(StringRef::iterator
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();
@@ -623,16 +644,49 @@ bool MIParser::parseCFIOffset(int &Offse
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;
}
@@ -668,6 +722,7 @@ bool MIParser::parseMachineOperand(Machi
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:
Modified: llvm/trunk/lib/CodeGen/MIRPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MIRPrinter.cpp?rev=243062&r1=243061&r2=243062&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/MIRPrinter.cpp (original)
+++ llvm/trunk/lib/CodeGen/MIRPrinter.cpp Thu Jul 23 18:09:07 2015
@@ -105,7 +105,7 @@ public:
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
@@ -465,7 +465,7 @@ void MIPrinter::print(const MachineOpera
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:
@@ -474,8 +474,26 @@ void MIPrinter::print(const MachineOpera
}
}
-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())
Added: llvm/trunk/test/CodeGen/MIR/X86/cfi-offset.mir
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/MIR/X86/cfi-offset.mir?rev=243062&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/MIR/X86/cfi-offset.mir (added)
+++ llvm/trunk/test/CodeGen/MIR/X86/cfi-offset.mir Thu Jul 23 18:09:07 2015
@@ -0,0 +1,47 @@
+# 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'
+...
+
Added: llvm/trunk/test/CodeGen/MIR/X86/expected-comma-after-cfi-register.mir
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/MIR/X86/expected-comma-after-cfi-register.mir?rev=243062&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/MIR/X86/expected-comma-after-cfi-register.mir (added)
+++ llvm/trunk/test/CodeGen/MIR/X86/expected-comma-after-cfi-register.mir Thu Jul 23 18:09:07 2015
@@ -0,0 +1,44 @@
+# 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'
+...
Added: llvm/trunk/test/CodeGen/MIR/X86/expected-register-after-cfi-operand.mir
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/MIR/X86/expected-register-after-cfi-operand.mir?rev=243062&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/MIR/X86/expected-register-after-cfi-operand.mir (added)
+++ llvm/trunk/test/CodeGen/MIR/X86/expected-register-after-cfi-operand.mir Thu Jul 23 18:09:07 2015
@@ -0,0 +1,44 @@
+# 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'
+...
More information about the llvm-commits
mailing list