[llvm] [SystemZ] Replace SELRMux with COPY in case of identical operands. (PR #125108)
Jonas Paulsson via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 31 04:47:28 PST 2025
https://github.com/JonPsson1 updated https://github.com/llvm/llvm-project/pull/125108
>From 4dfdc2cd054fb569bd423eda3e480f2df95dda65 Mon Sep 17 00:00:00 2001
From: Jonas Paulsson <paulsson at linux.vnet.ibm.com>
Date: Thu, 30 Jan 2025 19:40:35 +0100
Subject: [PATCH 1/2] Replace SELRMux with COPY in case of identical operands.
---
.../lib/Target/SystemZ/SystemZPostRewrite.cpp | 12 +++++++++++
llvm/test/CodeGen/SystemZ/cond-move-10.mir | 21 +++++++++++++++++++
2 files changed, 33 insertions(+)
create mode 100644 llvm/test/CodeGen/SystemZ/cond-move-10.mir
diff --git a/llvm/lib/Target/SystemZ/SystemZPostRewrite.cpp b/llvm/lib/Target/SystemZ/SystemZPostRewrite.cpp
index e15f9027cc2095..6b6ec5b6b9dc81 100644
--- a/llvm/lib/Target/SystemZ/SystemZPostRewrite.cpp
+++ b/llvm/lib/Target/SystemZ/SystemZPostRewrite.cpp
@@ -107,6 +107,18 @@ void SystemZPostRewrite::selectSELRMux(MachineBasicBlock &MBB,
bool Src1IsHigh = SystemZ::isHighReg(Src1Reg);
bool Src2IsHigh = SystemZ::isHighReg(Src2Reg);
+ // In rare cases both sources are the same register (after
+ // machine-cse). This must be handled as it may lead to wrong-code (after
+ // machine-cp) if the kill flag on Src1 isn't cleared (with
+ // expandCondMove()).
+ if (Src1Reg == Src2Reg) {
+ BuildMI(*MBBI->getParent(), MBBI, MBBI->getDebugLoc(),
+ TII->get(SystemZ::COPY), DestReg)
+ .addReg(MBBI->getOperand(1).getReg(), getRegState(MBBI->getOperand(1)));
+ MBBI->eraseFromParent();
+ return;
+ }
+
// If sources and destination aren't all high or all low, we may be able to
// simplify the operation by moving one of the sources to the destination
// first. But only if this doesn't clobber the other source.
diff --git a/llvm/test/CodeGen/SystemZ/cond-move-10.mir b/llvm/test/CodeGen/SystemZ/cond-move-10.mir
new file mode 100644
index 00000000000000..1db960829729ea
--- /dev/null
+++ b/llvm/test/CodeGen/SystemZ/cond-move-10.mir
@@ -0,0 +1,21 @@
+# RUN: llc -o - %s -mtriple=s390x-linux-gnu -mcpu=z15 -run-pass=systemz-post-rewrite \
+# RUN: 2>&1 | FileCheck %s
+
+# The SELRMux has two identical sources - replace with a copy instruction.
+# CHECK: name: fun0
+# CHECK: renamable $r1l = AHIMuxK killed renamable $r1l, -1, implicit-def dead $cc
+# CHECK-NEXT: CHIMux renamable $r5h, 9, implicit-def $cc
+# CHECK-NEXT: $r14h = COPY killed renamable $r1l
+---
+name: fun0
+tracksRegLiveness: true
+body: |
+ bb.0:
+ liveins: $r1l, $r5h
+ renamable $r1l = AHIMuxK killed renamable $r1l, -1, implicit-def dead $cc
+ CHIMux renamable $r5h, 9, implicit-def $cc
+ renamable $r14h = SELRMux killed renamable $r1l, renamable $r1l, 14, 8, implicit $cc
+ $r14l = COPY killed renamable $r14h
+ $r14d = LGFR $r14l
+ Return implicit $r14d
+...
>From 19e0e06dd325589d3ffe6c00206bbe4093afe871 Mon Sep 17 00:00:00 2001
From: Jonas Paulsson <paulson1 at linux.ibm.com>
Date: Fri, 31 Jan 2025 06:46:03 -0600
Subject: [PATCH 2/2] clang-format
---
llvm/lib/Target/SystemZ/SystemZPostRewrite.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/lib/Target/SystemZ/SystemZPostRewrite.cpp b/llvm/lib/Target/SystemZ/SystemZPostRewrite.cpp
index 6b6ec5b6b9dc81..cf3073f0f20904 100644
--- a/llvm/lib/Target/SystemZ/SystemZPostRewrite.cpp
+++ b/llvm/lib/Target/SystemZ/SystemZPostRewrite.cpp
@@ -114,7 +114,7 @@ void SystemZPostRewrite::selectSELRMux(MachineBasicBlock &MBB,
if (Src1Reg == Src2Reg) {
BuildMI(*MBBI->getParent(), MBBI, MBBI->getDebugLoc(),
TII->get(SystemZ::COPY), DestReg)
- .addReg(MBBI->getOperand(1).getReg(), getRegState(MBBI->getOperand(1)));
+ .addReg(MBBI->getOperand(1).getReg(), getRegState(MBBI->getOperand(1)));
MBBI->eraseFromParent();
return;
}
More information about the llvm-commits
mailing list