Index: lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp =================================================================== --- lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp +++ lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp @@ -26,6 +26,10 @@ using namespace llvm; +// Include the auto-generated portion of the compress emitter. +#define GEN_COMPRESS_INSTR +#include "RISCVGenCompressInstEmitter.inc" + namespace { struct RISCVOperand; @@ -595,10 +599,14 @@ switch (MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm)) { default: break; - case Match_Success: + case Match_Success: { + MCInst CInst; + bool Res = compressInst(CInst, Inst, getSTI(), Out.getContext()); + CInst.setLoc(IDLoc); Inst.setLoc(IDLoc); - Out.EmitInstruction(Inst, getSTI()); + Out.EmitInstruction((Res ? CInst : Inst), getSTI()); return false; + } case Match_MissingFeature: return Error(IDLoc, "instruction use requires an option to be enabled"); case Match_MnemonicFail: Index: lib/Target/RISCV/InstPrinter/RISCVInstPrinter.cpp =================================================================== --- lib/Target/RISCV/InstPrinter/RISCVInstPrinter.cpp +++ lib/Target/RISCV/InstPrinter/RISCVInstPrinter.cpp @@ -31,6 +31,10 @@ #define PRINT_ALIAS_INSTR #include "RISCVGenAsmWriter.inc" +// Include the auto-generated portion of the compress emitter. +#define GEN_UNCOMPRESS_INSTR +#include "RISCVGenCompressInstEmitter.inc" + static cl::opt NoAliases("riscv-no-aliases", cl::desc("Disable the emission of assembler pseudo instructions"), @@ -39,8 +43,15 @@ void RISCVInstPrinter::printInst(const MCInst *MI, raw_ostream &O, StringRef Annot, const MCSubtargetInfo &STI) { - if (NoAliases || !printAliasInstr(MI, STI, O)) - printInstruction(MI, STI, O); + bool Res = false; + const MCInst *NewMI = MI; + MCInst UncompressedMI; + if (!NoAliases) + Res = uncompressInst(UncompressedMI, *MI, MRI, STI); + if (Res) + NewMI = const_cast(&UncompressedMI); + if (NoAliases || !printAliasInstr(NewMI, STI, O)) + printInstruction(NewMI, STI, O); printAnnotation(O, Annot); } Index: lib/Target/RISCV/RISCVAsmPrinter.cpp =================================================================== --- lib/Target/RISCV/RISCVAsmPrinter.cpp +++ lib/Target/RISCV/RISCVAsmPrinter.cpp @@ -14,6 +14,7 @@ #include "RISCV.h" #include "InstPrinter/RISCVInstPrinter.h" +#include "MCTargetDesc/RISCVMCExpr.h" #include "RISCVTargetMachine.h" #include "llvm/CodeGen/AsmPrinter.h" #include "llvm/CodeGen/MachineConstantPool.h" @@ -62,14 +63,18 @@ // instructions) auto-generated. #include "RISCVGenMCPseudoLowering.inc" +#define GEN_COMPRESS_INSTR +#include "RISCVGenCompressInstEmitter.inc" void RISCVAsmPrinter::EmitInstruction(const MachineInstr *MI) { // Do any auto-generated pseudo lowerings. if (emitPseudoExpansionLowering(*OutStreamer, MI)) return; - MCInst TmpInst; + MCInst TmpInst, CInst; LowerRISCVMachineInstrToMCInst(MI, TmpInst, *this); - EmitToStreamer(*OutStreamer, TmpInst); + bool Res = compressInst(CInst, TmpInst, *TM.getMCSubtargetInfo(), + OutStreamer->getContext()); + EmitToStreamer(*OutStreamer, Res ? CInst : TmpInst); } bool RISCVAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, Index: test/MC/RISCV/compressed-relocations.s =================================================================== --- /dev/null +++ test/MC/RISCV/compressed-relocations.s @@ -0,0 +1,20 @@ +# RUN: llvm-mc -triple riscv32 -mattr=+c -riscv-no-aliases < %s -show-encoding \ +# RUN: | FileCheck -check-prefix=INSTR -check-prefix=FIXUP %s +# RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=+c < %s \ +# RUN: | llvm-readobj -r | FileCheck -check-prefix=RELOC %s + +# Check prefixes: +# RELOC - Check the relocation in the object. +# FIXUP - Check the fixup on the instruction. +# INSTR - Check the instruction is handled properly by the ASMPrinter +c.jal foo +# A compressed jump (c.j) to an unresolved symbol will be relaxed to a (jal). +# RELOC: R_RISCV_JAL +# INSTR: c.jal foo +# FIXUP: fixup A - offset: 0, value: foo, kind: fixup_riscv_rvc_jump + +c.bnez a0, foo +# A compressed branch (c.bnez) to an unresolved symbol will be relaxed to a (bnez). +# RELOC: R_RISCV_BRANCH +# INSTR: c.bnez a0, foo +# FIXUP: fixup A - offset: 0, value: foo, kind: fixup_riscv_rvc_branch Index: test/MC/RISCV/fixups-compressed.s =================================================================== --- test/MC/RISCV/fixups-compressed.s +++ test/MC/RISCV/fixups-compressed.s @@ -1,7 +1,7 @@ # RUN: llvm-mc %s -triple riscv32 -mattr=+c -show-encoding \ # RUN: | FileCheck -check-prefix=CHECK-FIXUP %s # RUN: llvm-mc -triple riscv32 -filetype=obj -mattr=+c < %s \ -# RUN: | llvm-objdump -d - | FileCheck -check-prefix=CHECK-INSTR %s +# RUN: | llvm-objdump -d -riscv-no-aliases - | FileCheck -check-prefix=CHECK-INSTR %s # RUN: llvm-mc -filetype=obj -mattr=+c -triple=riscv32 %s \ # RUN: | llvm-readobj -r | FileCheck %s -check-prefix=CHECK-REL Index: test/MC/RISCV/relocations.s =================================================================== --- test/MC/RISCV/relocations.s +++ test/MC/RISCV/relocations.s @@ -1,4 +1,4 @@ -# RUN: llvm-mc -triple riscv32 -mattr=+c -riscv-no-aliases < %s -show-encoding \ +# RUN: llvm-mc -triple riscv32 -riscv-no-aliases < %s -show-encoding \ # RUN: | FileCheck -check-prefix=INSTR -check-prefix=FIXUP %s # RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=+c < %s \ # RUN: | llvm-readobj -r | FileCheck -check-prefix=RELOC %s @@ -83,15 +83,3 @@ # RELOC: R_RISCV_BRANCH # INSTR: bgeu a0, a1, foo # FIXUP: fixup A - offset: 0, value: foo, kind: fixup_riscv_branch - -c.jal foo -# A compressed jump (c.j) to an unresolved symbol will be relaxed to a (jal). -# RELOC: R_RISCV_JAL -# INSTR: c.jal foo -# FIXUP: fixup A - offset: 0, value: foo, kind: fixup_riscv_rvc_jump - -c.bnez a0, foo -# A compressed branch (c.bnez) to an unresolved symbol will be relaxed to a (bnez). -# RELOC: R_RISCV_BRANCH -# INSTR: c.bnez a0, foo -# FIXUP: fixup A - offset: 0, value: foo, kind: fixup_riscv_rvc_branch Index: test/MC/RISCV/rv32-relaxation.s =================================================================== --- test/MC/RISCV/rv32-relaxation.s +++ test/MC/RISCV/rv32-relaxation.s @@ -1,5 +1,5 @@ # RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=+c < %s \ -# RUN: | llvm-objdump -d - | FileCheck -check-prefix=INSTR %s +# RUN: | llvm-objdump -d -riscv-no-aliases - | FileCheck -check-prefix=INSTR %s FAR_JUMP_NEGATIVE: c.nop @@ -18,26 +18,26 @@ c.bnez a0, NEAR_NEGATIVE #INSTR: c.bnez a0, -4 c.bnez a0, FAR_BRANCH -#INSTR-NEXT: bnez a0, 326 +#INSTR-NEXT: bne a0, zero, 326 c.bnez a0, FAR_BRANCH_NEGATIVE -#INSTR-NEXT: bnez a0, -268 +#INSTR-NEXT: bne a0, zero, -268 c.bnez a0, FAR_JUMP -#INSTR-NEXT: bnez a0, 2320 +#INSTR-NEXT: bne a0, zero, 2320 c.bnez a0, FAR_JUMP_NEGATIVE -#INSTR-NEXT: bnez a0, -2278 +#INSTR-NEXT: bne a0, zero, -2278 c.beqz a0, NEAR #INSTR-NEXT: c.beqz a0, 52 c.beqz a0, NEAR_NEGATIVE #INSTR-NEXT: c.beqz a0, -24 c.beqz a0, FAR_BRANCH -#INSTR-NEXT: beqz a0, 306 +#INSTR-NEXT: beq a0, zero, 306 c.beqz a0, FAR_BRANCH_NEGATIVE -#INSTR-NEXT: beqz a0, -288 +#INSTR-NEXT: beq a0, zero, -288 c.beqz a0, FAR_JUMP -#INSTR-NEXT: beqz a0, 2300 +#INSTR-NEXT: beq a0, zero, 2300 c.beqz a0, FAR_JUMP_NEGATIVE -#INSTR-NEXT: beqz a0, -2298 +#INSTR-NEXT: beq a0, zero, -2298 c.j NEAR #INSTR-NEXT: c.j 32 @@ -48,9 +48,9 @@ c.j FAR_BRANCH_NEGATIVE #INSTR-NEXT: c.j -306 c.j FAR_JUMP -#INSTR-NEXT: j 2284 +#INSTR-NEXT: jal zero, 2284 c.j FAR_JUMP_NEGATIVE -#INSTR-NEXT: j -2314 +#INSTR-NEXT: jal zero, -2314 c.jal NEAR #INSTR: c.jal 16 @@ -61,9 +61,9 @@ c.jal FAR_BRANCH_NEGATIVE #INSTR-NEXT: c.jal -322 c.jal FAR_JUMP -#INSTR-NEXT: jal 2268 +#INSTR-NEXT: jal ra, 2268 c.jal FAR_JUMP_NEGATIVE -#INSTR-NEXT: jal -2330 +#INSTR-NEXT: jal ra, -2330 NEAR: c.nop Index: test/MC/RISCV/rv64-relaxation.s =================================================================== --- test/MC/RISCV/rv64-relaxation.s +++ test/MC/RISCV/rv64-relaxation.s @@ -1,5 +1,5 @@ # RUN: llvm-mc -filetype=obj -triple riscv64 -mattr=+c < %s \ -# RUN: | llvm-objdump -d - | FileCheck -check-prefix=INSTR %s +# RUN: | llvm-objdump -d -riscv-no-aliases - | FileCheck -check-prefix=INSTR %s FAR_JUMP_NEGATIVE: c.nop @@ -18,26 +18,26 @@ c.bnez a0, NEAR_NEGATIVE #INSTR: c.bnez a0, -4 c.bnez a0, FAR_BRANCH -#INSTR-NEXT: bnez a0, 310 +#INSTR-NEXT: bne a0, zero, 310 c.bnez a0, FAR_BRANCH_NEGATIVE -#INSTR-NEXT: bnez a0, -268 +#INSTR-NEXT: bne a0, zero, -268 c.bnez a0, FAR_JUMP -#INSTR-NEXT: bnez a0, 2304 +#INSTR-NEXT: bne a0, zero, 2304 c.bnez a0, FAR_JUMP_NEGATIVE -#INSTR-NEXT: bnez a0, -2278 +#INSTR-NEXT: bne a0, zero, -2278 c.beqz a0, NEAR #INSTR-NEXT: c.beqz a0, 36 c.beqz a0, NEAR_NEGATIVE #INSTR-NEXT: c.beqz a0, -24 c.beqz a0, FAR_BRANCH -#INSTR-NEXT: beqz a0, 290 +#INSTR-NEXT: beq a0, zero, 290 c.beqz a0, FAR_BRANCH_NEGATIVE -#INSTR-NEXT: beqz a0, -288 +#INSTR-NEXT: beq a0, zero, -288 c.beqz a0, FAR_JUMP -#INSTR-NEXT: beqz a0, 2284 +#INSTR-NEXT: beq a0, zero, 2284 c.beqz a0, FAR_JUMP_NEGATIVE -#INSTR-NEXT: beqz a0, -2298 +#INSTR-NEXT: beq a0, zero, -2298 c.j NEAR #INSTR-NEXT: c.j 16 @@ -48,9 +48,9 @@ c.j FAR_BRANCH_NEGATIVE #INSTR-NEXT: c.j -306 c.j FAR_JUMP -#INSTR-NEXT: j 2268 +#INSTR-NEXT: jal zero, 2268 c.j FAR_JUMP_NEGATIVE -#INSTR-NEXT: j -2314 +#INSTR-NEXT: jal zero, -2314 NEAR: c.nop