[llvm] 7fa0a3c - [LoongArch] Add an option for MCInstPrinter to print numeric reg names
via llvm-commits
llvm-commits at lists.llvm.org
Thu Jan 19 00:42:19 PST 2023
Author: wanglei
Date: 2023-01-19T16:29:22+08:00
New Revision: 7fa0a3c92377c2ce5b836b43e33a4a7107a3af38
URL: https://github.com/llvm/llvm-project/commit/7fa0a3c92377c2ce5b836b43e33a4a7107a3af38
DIFF: https://github.com/llvm/llvm-project/commit/7fa0a3c92377c2ce5b836b43e33a4a7107a3af38.diff
LOG: [LoongArch] Add an option for MCInstPrinter to print numeric reg names
`-loongarch-numeric-reg` for llvm-mc and llc.
`-M numeric` (which matches GNU objdump) for llvm-objdump and llvm-mc.
Reviewed By: SixWeining
Differential Revision: https://reviews.llvm.org/D141743
Added:
llvm/test/CodeGen/LoongArch/numeric-reg-names.ll
llvm/test/MC/LoongArch/Misc/numeric-reg-names.s
Modified:
llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchInstPrinter.cpp
llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchInstPrinter.h
Removed:
################################################################################
diff --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchInstPrinter.cpp b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchInstPrinter.cpp
index c72af21ade89c..cb2521db5217e 100644
--- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchInstPrinter.cpp
+++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchInstPrinter.cpp
@@ -12,11 +12,13 @@
#include "LoongArchInstPrinter.h"
#include "LoongArchBaseInfo.h"
+#include "LoongArchMCTargetDesc.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/MC/MCSymbol.h"
+#include "llvm/Support/CommandLine.h"
using namespace llvm;
#define DEBUG_TYPE "loongarch-asm-printer"
@@ -25,6 +27,26 @@ using namespace llvm;
#define PRINT_ALIAS_INSTR
#include "LoongArchGenAsmWriter.inc"
+static cl::opt<bool>
+ NumericReg("loongarch-numeric-reg",
+ cl::desc("Print numeric register names rather than the ABI "
+ "names (such as $r0 instead of $zero)"),
+ cl::init(false), cl::Hidden);
+
+// The command-line flag above is used by llvm-mc and llc. It can be used by
+// `llvm-objdump`, but we override the value here to handle options passed to
+// `llvm-objdump` with `-M` (which matches GNU objdump). There did not seem to
+// be an easier way to allow these options in all these tools, without doing it
+// this way.
+bool LoongArchInstPrinter::applyTargetSpecificCLOption(StringRef Opt) {
+ if (Opt == "numeric") {
+ NumericReg = true;
+ return true;
+ }
+
+ return false;
+}
+
void LoongArchInstPrinter::printInst(const MCInst *MI, uint64_t Address,
StringRef Annot,
const MCSubtargetInfo &STI,
@@ -67,5 +89,6 @@ void LoongArchInstPrinter::printAtomicMemOp(const MCInst *MI, unsigned OpNo,
const char *LoongArchInstPrinter::getRegisterName(MCRegister Reg) {
// Default print reg alias name
- return getRegisterName(Reg, LoongArch::RegAliasName);
+ return getRegisterName(Reg, NumericReg ? LoongArch::NoRegAltName
+ : LoongArch::RegAliasName);
}
diff --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchInstPrinter.h b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchInstPrinter.h
index 6308a9f4e679e..4e6092bfcb128 100644
--- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchInstPrinter.h
+++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchInstPrinter.h
@@ -24,6 +24,8 @@ class LoongArchInstPrinter : public MCInstPrinter {
const MCRegisterInfo &MRI)
: MCInstPrinter(MAI, MII, MRI) {}
+ bool applyTargetSpecificCLOption(StringRef Opt) override;
+
void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
const MCSubtargetInfo &STI, raw_ostream &O) override;
void printRegName(raw_ostream &O, MCRegister Reg) const override;
diff --git a/llvm/test/CodeGen/LoongArch/numeric-reg-names.ll b/llvm/test/CodeGen/LoongArch/numeric-reg-names.ll
new file mode 100644
index 0000000000000..153a697a55b9a
--- /dev/null
+++ b/llvm/test/CodeGen/LoongArch/numeric-reg-names.ll
@@ -0,0 +1,42 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc --mtriple=loongarch32 --loongarch-numeric-reg < %s \
+; RUN: | FileCheck %s --check-prefix=LA32
+; RUN: llc --mtriple=loongarch64 --loongarch-numeric-reg < %s \
+; RUN: | FileCheck %s --check-prefix=LA64
+
+ at .str_1 = internal constant [7 x i8] c"hello\0A\00"
+
+declare i32 @printf(ptr, ...)
+
+define i32 @main() {
+; LA32-LABEL: main:
+; LA32: # %bb.0:
+; LA32-NEXT: addi.w $r3, $r3, -16
+; LA32-NEXT: .cfi_def_cfa_offset 16
+; LA32-NEXT: st.w $r1, $r3, 12 # 4-byte Folded Spill
+; LA32-NEXT: .cfi_offset 1, -4
+; LA32-NEXT: pcalau12i $r4, %pc_hi20(.str_1)
+; LA32-NEXT: addi.w $r4, $r4, %pc_lo12(.str_1)
+; LA32-NEXT: bl %plt(printf)
+; LA32-NEXT: move $r4, $r0
+; LA32-NEXT: ld.w $r1, $r3, 12 # 4-byte Folded Reload
+; LA32-NEXT: addi.w $r3, $r3, 16
+; LA32-NEXT: ret
+;
+; LA64-LABEL: main:
+; LA64: # %bb.0:
+; LA64-NEXT: addi.d $r3, $r3, -16
+; LA64-NEXT: .cfi_def_cfa_offset 16
+; LA64-NEXT: st.d $r1, $r3, 8 # 8-byte Folded Spill
+; LA64-NEXT: .cfi_offset 1, -8
+; LA64-NEXT: pcalau12i $r4, %pc_hi20(.str_1)
+; LA64-NEXT: addi.d $r4, $r4, %pc_lo12(.str_1)
+; LA64-NEXT: bl %plt(printf)
+; LA64-NEXT: move $r4, $r0
+; LA64-NEXT: ld.d $r1, $r3, 8 # 8-byte Folded Reload
+; LA64-NEXT: addi.d $r3, $r3, 16
+; LA64-NEXT: ret
+ %s = getelementptr [7 x i8], ptr @.str_1, i64 0, i64 0
+ call i32 (ptr, ...) @printf(ptr %s)
+ ret i32 0
+}
diff --git a/llvm/test/MC/LoongArch/Misc/numeric-reg-names.s b/llvm/test/MC/LoongArch/Misc/numeric-reg-names.s
new file mode 100644
index 0000000000000..b724e47425203
--- /dev/null
+++ b/llvm/test/MC/LoongArch/Misc/numeric-reg-names.s
@@ -0,0 +1,64 @@
+# RUN: llvm-mc --triple=loongarch32 --mattr=+f --loongarch-numeric-reg %s \
+# RUN: | FileCheck %s
+# RUN: llvm-mc --triple=loongarch32 --mattr=+f -M numeric %s \
+# RUN: | FileCheck %s
+# RUN: llvm-mc --triple=loongarch32 --mattr=+f --filetype=obj %s -o %t.32
+# RUN: llvm-objdump -d -M numeric %t.32 | FileCheck %s
+# RUN: llvm-mc --triple=loongarch64 --mattr=+f --loongarch-numeric-reg %s \
+# RUN: | FileCheck %s
+# RUN: llvm-mc --triple=loongarch64 --mattr=+f -M numeric %s \
+# RUN: | FileCheck %s
+# RUN: llvm-mc --triple=loongarch64 --mattr=+f --filetype=obj %s -o %t.64
+# RUN: llvm-objdump -d -M numeric %t.64 | FileCheck %s
+
+addi.w $zero, $ra, 1
+addi.w $tp, $sp, 1
+addi.w $a0, $a1, 1
+addi.w $a2, $a3, 1
+addi.w $a4, $a5, 1
+addi.w $a6, $a7, 1
+addi.w $t0, $t1, 1
+addi.w $t2, $t3, 1
+addi.w $t4, $t5, 1
+addi.w $t6, $t7, 1
+addi.w $t8, $r21, 1
+addi.w $fp, $s0, 1
+addi.w $s1, $s2, 1
+addi.w $s3, $s4, 1
+addi.w $s5, $s6, 1
+addi.w $s7, $s8, 1
+
+# CHECK: addi.w $r0, $r1, 1
+# CHECK-NEXT: addi.w $r2, $r3, 1
+# CHECK-NEXT: addi.w $r4, $r5, 1
+# CHECK-NEXT: addi.w $r6, $r7, 1
+# CHECK-NEXT: addi.w $r8, $r9, 1
+# CHECK-NEXT: addi.w $r10, $r11, 1
+# CHECK-NEXT: addi.w $r12, $r13, 1
+# CHECK-NEXT: addi.w $r14, $r15, 1
+# CHECK-NEXT: addi.w $r16, $r17, 1
+# CHECK-NEXT: addi.w $r18, $r19, 1
+# CHECK-NEXT: addi.w $r20, $r21, 1
+# CHECK-NEXT: addi.w $r22, $r23, 1
+# CHECK-NEXT: addi.w $r24, $r25, 1
+# CHECK-NEXT: addi.w $r26, $r27, 1
+# CHECK-NEXT: addi.w $r28, $r29, 1
+# CHECK-NEXT: addi.w $r30, $r31, 1
+
+fmadd.s $fa0, $fa1, $fa2, $fa3
+fmadd.s $fa4, $fa5, $fa6, $fa7
+fmadd.s $ft0, $ft1, $ft2, $ft3
+fmadd.s $ft4, $ft5, $ft6, $ft7
+fmadd.s $ft8, $ft9, $ft10, $ft11
+fmadd.s $ft12, $ft13, $ft14, $ft15
+fmadd.s $fs0, $fs1, $fs2, $fs3
+fmadd.s $fs4, $fs5, $fs6, $fs7
+
+# CHECK: fmadd.s $f0, $f1, $f2, $f3
+# CHECK-NEXT: fmadd.s $f4, $f5, $f6, $f7
+# CHECK-NEXT: fmadd.s $f8, $f9, $f10, $f11
+# CHECK-NEXT: fmadd.s $f12, $f13, $f14, $f15
+# CHECK-NEXT: fmadd.s $f16, $f17, $f18, $f19
+# CHECK-NEXT: fmadd.s $f20, $f21, $f22, $f23
+# CHECK-NEXT: fmadd.s $f24, $f25, $f26, $f27
+# CHECK-NEXT: fmadd.s $f28, $f29, $f30, $f31
More information about the llvm-commits
mailing list