[llvm] [AArch64] Have isel just do neg directly (PR #145185)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Jun 21 12:36:29 PDT 2025
https://github.com/AZero13 created https://github.com/llvm/llvm-project/pull/145185
This means we do not have to rely on register coalescing to fix this, which matches gcc outputting things like neg on -O0
>From f738b906e2852ea9c8fa563b8fe9fb678e40fd63 Mon Sep 17 00:00:00 2001
From: Rose <gfunni234 at gmail.com>
Date: Sat, 21 Jun 2025 15:05:23 -0400
Subject: [PATCH 1/2] Pre-commit test (NFC)
---
llvm/test/CodeGen/AArch64/instruct-neg.ll | 12 ++++++++++++
1 file changed, 12 insertions(+)
create mode 100644 llvm/test/CodeGen/AArch64/instruct-neg.ll
diff --git a/llvm/test/CodeGen/AArch64/instruct-neg.ll b/llvm/test/CodeGen/AArch64/instruct-neg.ll
new file mode 100644
index 0000000000000..1edce89fb7d90
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/instruct-neg.ll
@@ -0,0 +1,12 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
+; RUN: llc -mtriple=aarch64-linux-gnu -O0 -fast-isel -fast-isel-abort=1 -verify-machineinstrs < %s | FileCheck %s --check-prefix=CHECK
+
+define i32 @negb(i32 %b) {
+; CHECK-LABEL: negb:
+; CHECK: // %bb.0:
+; CHECK-NEXT: mov w8, wzr
+; CHECK-NEXT: sub w0, w8, w0
+; CHECK-NEXT: ret
+ %sub = sub nsw i32 0, %b
+ ret i32 %sub
+}
>From c4257d359ddc0ad67ae76981f219600f7e8f53f2 Mon Sep 17 00:00:00 2001
From: Rose <gfunni234 at gmail.com>
Date: Sat, 21 Jun 2025 15:36:00 -0400
Subject: [PATCH 2/2] [AArch64] Have isel just do neg directly
This means we do not have to rely on register coalescing to fix this, which matches gcc outputting things like neg on -O0
---
llvm/lib/Target/AArch64/AArch64FastISel.cpp | 13 +++++++++++++
.../AArch64/GISel/AArch64InstructionSelector.cpp | 16 ++++++++++++++++
llvm/test/CodeGen/AArch64/instruct-neg.ll | 3 +--
3 files changed, 30 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Target/AArch64/AArch64FastISel.cpp b/llvm/lib/Target/AArch64/AArch64FastISel.cpp
index 9d74bb5a8661d..85f9140f34e68 100644
--- a/llvm/lib/Target/AArch64/AArch64FastISel.cpp
+++ b/llvm/lib/Target/AArch64/AArch64FastISel.cpp
@@ -1201,6 +1201,19 @@ Register AArch64FastISel::emitAddSub(bool UseAdd, MVT RetVT, const Value *LHS,
SI->getOpcode() == Instruction::AShr )
std::swap(LHS, RHS);
+ // Special case: sub 0, x -> neg x (use zero register directly)
+ if (!UseAdd && isa<Constant>(LHS) && cast<Constant>(LHS)->isNullValue()) {
+ Register RHSReg = getRegForValue(RHS);
+ if (!RHSReg)
+ return Register();
+
+ if (NeedExtend)
+ RHSReg = emitIntExt(SrcVT, RHSReg, RetVT, IsZExt);
+
+ Register ZeroReg = RetVT == MVT::i64 ? AArch64::XZR : AArch64::WZR;
+ return emitAddSub_rr(UseAdd, RetVT, ZeroReg, RHSReg, SetFlags, WantResult);
+ }
+
Register LHSReg = getRegForValue(LHS);
if (!LHSReg)
return Register();
diff --git a/llvm/lib/Target/AArch64/GISel/AArch64InstructionSelector.cpp b/llvm/lib/Target/AArch64/GISel/AArch64InstructionSelector.cpp
index d55ff5acb3dca..cd6e7aa300043 100644
--- a/llvm/lib/Target/AArch64/GISel/AArch64InstructionSelector.cpp
+++ b/llvm/lib/Target/AArch64/GISel/AArch64InstructionSelector.cpp
@@ -4409,6 +4409,22 @@ MachineInstr *AArch64InstructionSelector::emitAddSub(
assert((Size == 32 || Size == 64) && "Expected a 32-bit or 64-bit type only");
bool Is32Bit = Size == 32;
+ // Special case: sub 0, x -> neg x (use zero register directly)
+ // Check if this is a SUB operation by examining the base register-register opcode
+ unsigned BaseOpc = AddrModeAndSizeToOpcode[2][Is32Bit];
+ bool IsSubtraction = (BaseOpc == AArch64::SUBWrr || BaseOpc == AArch64::SUBXrr ||
+ BaseOpc == AArch64::SUBSWrr || BaseOpc == AArch64::SUBSXrr);
+ if (IsSubtraction) {
+ if (auto LHSImm = getIConstantVRegValWithLookThrough(LHS.getReg(), MRI)) {
+ if (LHSImm->Value.isZero()) {
+ // Replace LHS with the appropriate zero register
+ Register ZeroReg = Is32Bit ? AArch64::WZR : AArch64::XZR;
+ MachineOperand ZeroMO = MachineOperand::CreateReg(ZeroReg, false);
+ return emitInstr(BaseOpc, {Dst}, {ZeroMO, RHS}, MIRBuilder);
+ }
+ }
+ }
+
// INSTRri form with positive arithmetic immediate.
if (auto Fns = selectArithImmed(RHS))
return emitInstr(AddrModeAndSizeToOpcode[0][Is32Bit], {Dst}, {LHS},
diff --git a/llvm/test/CodeGen/AArch64/instruct-neg.ll b/llvm/test/CodeGen/AArch64/instruct-neg.ll
index 1edce89fb7d90..4d60fb768901e 100644
--- a/llvm/test/CodeGen/AArch64/instruct-neg.ll
+++ b/llvm/test/CodeGen/AArch64/instruct-neg.ll
@@ -4,8 +4,7 @@
define i32 @negb(i32 %b) {
; CHECK-LABEL: negb:
; CHECK: // %bb.0:
-; CHECK-NEXT: mov w8, wzr
-; CHECK-NEXT: sub w0, w8, w0
+; CHECK-NEXT: neg w0, w0
; CHECK-NEXT: ret
%sub = sub nsw i32 0, %b
ret i32 %sub
More information about the llvm-commits
mailing list