[llvm] apply mulx optimization for two-wide mul instruction (mull, mulq) (PR #185127)
Takashi Idobe via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 6 14:54:19 PST 2026
https://github.com/Takashiidobe created https://github.com/llvm/llvm-project/pull/185127
References: https://github.com/llvm/llvm-project/pull/184462
In the discussion for the linked PR, which removes unnecessary register to register moves when one operand is in %rdx for mulx, the point was brought up that this pattern also happens for mull and mulq.
The IR below:
```llvm
declare i32 @foo32()
declare i64 @foo64()
define i32 @mul32_no_implicit_copy(i32 %a0) {
%a1 = call i32 @foo32()
%a2 = call { i32, i1 } @llvm.umul.with.overflow.i32(i32 %a0, i32 %a1)
%a3 = extractvalue { i32, i1 } %a2, 0
ret i32 %a3
}
define i64 @mul64_no_implicit_copy(i64 %a0) {
%a1 = call i64 @foo64()
%a2 = call { i64, i1 } @llvm.umul.with.overflow.i64(i64 %a0, i64 %a1)
%a3 = extractvalue { i64, i1 } %a2, 0
ret i64 %a3
}
```
Generates this code on current HEAD:
```asm
mul32_no_implicit_copy: # @mul32_no_implicit_copy
push rbx
mov ebx, edi
call foo32 at PLT
mov ecx, eax
mov eax, ebx
mul ecx
pop rbx
ret
mul64_no_implicit_copy: # @mul64_no_implicit_copy
push rbx
mov rbx, rdi
call foo64 at PLT
mov rcx, rax
mov rax, rbx
mul rcx
pop rbx
ret
```
Where the register shuffling before the mul is the same pattern as for mulx in the previous PR.
With this branch it generates this code now:
```asm
mul32_no_implicit_copy:
pushq %rbx
movl %edi, %ebx
callq foo32 at PLT
mull %ebx
popq %rbx
retq
mul64_no_implicit_copy:
pushq %rbx
movq %rdi, %rbx
callq foo64 at PLT
mulq %rbx
popq %rbx
retq
```
>From 32500267d02eef82f1f2421ef4e193de09615e9c Mon Sep 17 00:00:00 2001
From: Takashiidobe <idobetakashi at gmail.com>
Date: Fri, 6 Mar 2026 17:40:12 -0500
Subject: [PATCH] apply mulx optimization for two-wide mul instruction (mull,
mulq)
---
llvm/lib/Target/X86/X86ISelDAGToDAG.cpp | 41 ++++++++++-------
.../CodeGen/X86/mul-lohi-no-implicit-copy.ll | 45 +++++++++++++++++++
2 files changed, 69 insertions(+), 17 deletions(-)
create mode 100644 llvm/test/CodeGen/X86/mul-lohi-no-implicit-copy.ll
diff --git a/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp b/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp
index cc846e0d1492e..ad7dbbc508549 100644
--- a/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp
+++ b/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp
@@ -5050,6 +5050,21 @@ VPTESTM_CASE(v32i16, WZ##SUFFIX)
#undef VPTESTM_CASE
}
+static void orderRegForMul(SDValue &N0, SDValue &N1, const unsigned LoReg,
+ const MachineRegisterInfo &MRI) {
+ auto GetPhysReg = [&](SDValue V) -> Register {
+ if (V.getOpcode() != ISD::CopyFromReg)
+ return Register();
+ Register Reg = cast<RegisterSDNode>(V.getOperand(1))->getReg();
+ if (Reg.isVirtual())
+ return MRI.getLiveInPhysReg(Reg);
+ return Reg;
+ };
+
+ if (GetPhysReg(N1) == LoReg && GetPhysReg(N0) != LoReg)
+ std::swap(N0, N1);
+}
+
// Try to create VPTESTM instruction. If InMask is not null, it will be used
// to form a masked operation.
bool X86DAGToDAGISel::tryVPTESTM(SDNode *Root, SDValue Setcc,
@@ -5796,6 +5811,11 @@ void X86DAGToDAGISel::Select(SDNode *Node) {
std::swap(N0, N1);
}
+ // UMUL/SMUL have an implicit source in LoReg (AL/AX/EAX/RAX). Prefer the
+ // operand that's already there to avoid an extra register-to-register move.
+ if (!FoldedLoad)
+ orderRegForMul(N0, N1, LoReg, CurDAG->getMachineFunction().getRegInfo());
+
SDValue InGlue = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
N0, SDValue()).getValue(1);
@@ -5882,23 +5902,10 @@ void X86DAGToDAGISel::Select(SDNode *Node) {
std::swap(N0, N1);
}
- // For MULX, the implicit source must be in RDX (LoReg). If N1 is
- // already a CopyFromReg of LoReg and N0 is not, flip so that N0
- // (which feeds the CopyToReg below) is the operand already in LoReg,
- // avoiding an unnecessary register-to-register copy before the multiply.
- if (UseMULX && !foldedLoad) {
- MachineRegisterInfo &MRI = CurDAG->getMachineFunction().getRegInfo();
- auto GetPhysReg = [&](SDValue V) -> Register {
- if (V.getOpcode() != ISD::CopyFromReg)
- return Register();
- Register Reg = cast<RegisterSDNode>(V.getOperand(1))->getReg();
- if (Reg.isVirtual())
- return MRI.getLiveInPhysReg(Reg);
- return Reg;
- };
- if (GetPhysReg(N1) == LoReg && GetPhysReg(N0) != LoReg)
- std::swap(N0, N1);
- }
+ // UMUL/SMUL_LOHI has an implicit source in LoReg (RDX for MULX, RAX for
+ // MUL/IMUL). Prefer the operand that's already there.
+ if (!foldedLoad)
+ orderRegForMul(N0, N1, LoReg, CurDAG->getMachineFunction().getRegInfo());
SDValue InGlue = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
N0, SDValue()).getValue(1);
diff --git a/llvm/test/CodeGen/X86/mul-lohi-no-implicit-copy.ll b/llvm/test/CodeGen/X86/mul-lohi-no-implicit-copy.ll
new file mode 100644
index 0000000000000..68ecf08778550
--- /dev/null
+++ b/llvm/test/CodeGen/X86/mul-lohi-no-implicit-copy.ll
@@ -0,0 +1,45 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mattr=-bmi2 | FileCheck %s
+;
+; For UMUL_LOHI lowering without BMI2, MUL/IMUL use RAX as an implicit source.
+; If one operand already lives in RAX (e.g. call result), avoid shuffling it out
+; and back before the multiply.
+
+declare i32 @foo32()
+declare i64 @foo64()
+
+define i32 @mul32_no_implicit_copy(i32 %a0) {
+; CHECK-LABEL: mul32_no_implicit_copy:
+; CHECK: # %bb.0:
+; CHECK-NEXT: pushq %rbx
+; CHECK-NEXT: .cfi_def_cfa_offset 16
+; CHECK-NEXT: .cfi_offset %rbx, -16
+; CHECK-NEXT: movl %edi, %ebx
+; CHECK-NEXT: callq foo32 at PLT
+; CHECK-NEXT: mull %ebx
+; CHECK-NEXT: popq %rbx
+; CHECK-NEXT: .cfi_def_cfa_offset 8
+; CHECK-NEXT: retq
+ %a1 = call i32 @foo32()
+ %a2 = call { i32, i1 } @llvm.umul.with.overflow.i32(i32 %a0, i32 %a1)
+ %a3 = extractvalue { i32, i1 } %a2, 0
+ ret i32 %a3
+}
+
+define i64 @mul64_no_implicit_copy(i64 %a0) {
+; CHECK-LABEL: mul64_no_implicit_copy:
+; CHECK: # %bb.0:
+; CHECK-NEXT: pushq %rbx
+; CHECK-NEXT: .cfi_def_cfa_offset 16
+; CHECK-NEXT: .cfi_offset %rbx, -16
+; CHECK-NEXT: movq %rdi, %rbx
+; CHECK-NEXT: callq foo64 at PLT
+; CHECK-NEXT: mulq %rbx
+; CHECK-NEXT: popq %rbx
+; CHECK-NEXT: .cfi_def_cfa_offset 8
+; CHECK-NEXT: retq
+ %a1 = call i64 @foo64()
+ %a2 = call { i64, i1 } @llvm.umul.with.overflow.i64(i64 %a0, i64 %a1)
+ %a3 = extractvalue { i64, i1 } %a2, 0
+ ret i64 %a3
+}
More information about the llvm-commits
mailing list