Skip to content

Conversation

@omkar-mohanty
Copy link

Fixes #142308 .

@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Jun 13, 2025

@llvm/pr-subscribers-backend-x86

Author: Omkar Mohanty (omkar-mohanty)

Changes

Fixes #142308 .


Full diff: https://github.com/llvm/llvm-project/pull/144066.diff

2 Files Affected:

  • (modified) llvm/lib/Target/X86/X86ISelLowering.cpp (+20)
  • (added) llvm/test/CodeGen/X86/reduce-i64-add.ll (+126)
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index b4670e270141f..5c999639ba860 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -58086,8 +58086,28 @@ static SDValue combineAdd(SDNode *N, SelectionDAG &DAG,
   EVT VT = N->getValueType(0);
   SDValue Op0 = N->getOperand(0);
   SDValue Op1 = N->getOperand(1);
+  unsigned int Opcode = N->getOpcode();
   SDLoc DL(N);
 
+  // Use a 32-bit add+zext if upper 33 bits known zero.
+  if (VT == MVT::i64 && Subtarget.is64Bit()) {
+    APInt HiMask = APInt::getHighBitsSet(64, 33);
+    if (DAG.MaskedValueIsZero(Op0, HiMask) &&
+        DAG.MaskedValueIsZero(Op1, HiMask)) {
+      SDValue LHS = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op0);
+      SDValue RHS = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
+      bool NSW = Op0->getFlags().hasNoSignedWrap();
+      bool NUW = Op0->getFlags().hasNoUnsignedWrap();
+      NSW = NSW & DAG.willNotOverflowAdd(true, LHS, RHS);
+      NUW = NUW & DAG.willNotOverflowAdd(false, LHS, RHS);
+      SDNodeFlags Flags;
+      Flags.setNoUnsignedWrap(NUW);
+      Flags.setNoSignedWrap(NSW);
+      SDValue Sum = DAG.getNode(Opcode, DL, MVT::i32, LHS, RHS, Flags);
+      return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, Sum);
+    }
+  }
+
   if (SDValue Select = pushAddIntoCmovOfConsts(N, DL, DAG, Subtarget))
     return Select;
 
diff --git a/llvm/test/CodeGen/X86/reduce-i64-add.ll b/llvm/test/CodeGen/X86/reduce-i64-add.ll
new file mode 100644
index 0000000000000..41de10bd22524
--- /dev/null
+++ b/llvm/test/CodeGen/X86/reduce-i64-add.ll
@@ -0,0 +1,126 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc < %s -mtriple=i686-unknown-unknown | FileCheck %s --check-prefix=X86
+; RUN: llc < %s -mtriple=x86_64-linux -verify-machineinstrs | FileCheck %s --check-prefix=X64-LINUX
+; RUN: llc < %s -mtriple=x86_64-win32 | FileCheck %s --check-prefix=X64-WIN32
+
+define i64 @test1(i16 %a) {
+; X86-LABEL: test1:
+; X86:       # %bb.0:
+; X86-NEXT:    movzwl {{[0-9]+}}(%esp), %eax
+; X86-NEXT:    addl $42, %eax
+; X86-NEXT:    xorl %edx, %edx
+; X86-NEXT:    retl
+;
+; X64-LINUX-LABEL: test1:
+; X64-LINUX:       # %bb.0:
+; X64-LINUX-NEXT:    movzwl %di, %eax
+; X64-LINUX-NEXT:    addl $42, %eax
+; X64-LINUX-NEXT:    retq
+;
+; X64-WIN32-LABEL: test1:
+; X64-WIN32:       # %bb.0:
+; X64-WIN32-NEXT:    movzwl %cx, %eax
+; X64-WIN32-NEXT:    addl $42, %eax
+; X64-WIN32-NEXT:    retq
+  %zext_a = zext i16 %a to i64
+  %sum = add nuw nsw i64 %zext_a, 42
+  ret i64 %sum
+}
+
+ 
+define i64 @test2(i16 %a, i16 %b) {
+; X86-LABEL: test2:
+; X86:       # %bb.0:
+; X86-NEXT:    movzwl {{[0-9]+}}(%esp), %ecx
+; X86-NEXT:    movzwl {{[0-9]+}}(%esp), %eax
+; X86-NEXT:    addl %ecx, %eax
+; X86-NEXT:    xorl %edx, %edx
+; X86-NEXT:    retl
+;
+; X64-LINUX-LABEL: test2:
+; X64-LINUX:       # %bb.0:
+; X64-LINUX-NEXT:    movzwl %si, %ecx
+; X64-LINUX-NEXT:    movzwl %di, %eax
+; X64-LINUX-NEXT:    addl %ecx, %eax
+; X64-LINUX-NEXT:    retq
+;
+; X64-WIN32-LABEL: test2:
+; X64-WIN32:       # %bb.0:
+; X64-WIN32-NEXT:    movzwl %dx, %edx
+; X64-WIN32-NEXT:    movzwl %cx, %eax
+; X64-WIN32-NEXT:    addl %edx, %eax
+; X64-WIN32-NEXT:    retq
+  %zext_a = zext i16 %a to i64
+  %zext_b = zext i16 %b to i64
+; First 48 bits are all zeros so we can safely truncate to 32 bit additon
+  %sum = add nuw nsw i64 %zext_a, %zext_b
+  ret i64 %sum
+}
+
+define i64 @test3(i16 %a) {
+; X86-LABEL: test3:
+; X86:       # %bb.0:
+; X86-NEXT:    movzwl {{[0-9]+}}(%esp), %eax
+; X86-NEXT:    addl $42, %eax
+; X86-NEXT:    movl $1, %edx
+; X86-NEXT:    retl
+;
+; X64-LINUX-LABEL: test3:
+; X64-LINUX:       # %bb.0:
+; X64-LINUX-NEXT:    movzwl %di, %ecx
+; X64-LINUX-NEXT:    movabsq $4294967338, %rax # imm = 0x10000002A
+; X64-LINUX-NEXT:    addq %rcx, %rax
+; X64-LINUX-NEXT:    retq
+;
+; X64-WIN32-LABEL: test3:
+; X64-WIN32:       # %bb.0:
+; X64-WIN32-NEXT:    movzwl %cx, %ecx
+; X64-WIN32-NEXT:    movabsq $4294967338, %rax # imm = 0x10000002A
+; X64-WIN32-NEXT:    addq %rcx, %rax
+; X64-WIN32-NEXT:    retq
+  %zext_a = zext i16 %a to i64
+; Set the 32nd bit of a to force 64 bit addition, we do not truncate to 32 bit addition in this case
+  %or_a = or i64 %zext_a, 4294967296
+  %sum = add nuw nsw i64 %or_a, 42
+  ret i64 %sum
+}
+
+define i64 @test4(i16 %a, i16 %b) {
+; X86-LABEL: test4:
+; X86:       # %bb.0:
+; X86-NEXT:    pushl %esi
+; X86-NEXT:    .cfi_def_cfa_offset 8
+; X86-NEXT:    .cfi_offset %esi, -8
+; X86-NEXT:    movswl {{[0-9]+}}(%esp), %ecx
+; X86-NEXT:    movl %ecx, %esi
+; X86-NEXT:    sarl $31, %esi
+; X86-NEXT:    movswl {{[0-9]+}}(%esp), %eax
+; X86-NEXT:    movl %eax, %edx
+; X86-NEXT:    sarl $31, %edx
+; X86-NEXT:    addl %ecx, %eax
+; X86-NEXT:    adcl %esi, %edx
+; X86-NEXT:    popl %esi
+; X86-NEXT:    .cfi_def_cfa_offset 4
+; X86-NEXT:    retl
+;
+; X64-LINUX-LABEL: test4:
+; X64-LINUX:       # %bb.0:
+; X64-LINUX-NEXT:    # kill: def $esi killed $esi def $rsi
+; X64-LINUX-NEXT:    # kill: def $edi killed $edi def $rdi
+; X64-LINUX-NEXT:    movswq %di, %rcx
+; X64-LINUX-NEXT:    movswq %si, %rax
+; X64-LINUX-NEXT:    addq %rcx, %rax
+; X64-LINUX-NEXT:    retq
+;
+; X64-WIN32-LABEL: test4:
+; X64-WIN32:       # %bb.0:
+; X64-WIN32-NEXT:    movswq %cx, %rcx
+; X64-WIN32-NEXT:    movswq %dx, %rax
+; X64-WIN32-NEXT:    addq %rcx, %rax
+; X64-WIN32-NEXT:    retq
+  %zext_a = sext i16 %a to i64
+  %zext_b = sext i16 %b to i64
+; We don't truncate to 32 bit addition in case of sign extension 
+  %sum = add nuw nsw i64 %zext_a, %zext_b
+  ret i64 %sum
+}

@omkar-mohanty omkar-mohanty force-pushed the optimize-x86-i64-add branch from 69b6bf7 to 3182c0d Compare June 13, 2025 12:48
@RKSimon RKSimon requested review from RKSimon and phoebewang June 13, 2025 13:00
@omkar-mohanty omkar-mohanty force-pushed the optimize-x86-i64-add branch from 3182c0d to f926a18 Compare June 17, 2025 09:52
@omkar-mohanty
Copy link
Author

Hi @RKSimon , @phoebewang can I get a review on this PR? Thanks :)

@omkar-mohanty omkar-mohanty force-pushed the optimize-x86-i64-add branch from f926a18 to baf11ed Compare June 21, 2025 11:40
Copy link
Contributor

@phoebewang phoebewang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with 2 nits.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add nounwind to suppress CFI directives.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review! I added nounwind to all the test cases not completely sure if it’s needed for each one, but did it to keep things consistent.

@omkar-mohanty omkar-mohanty force-pushed the optimize-x86-i64-add branch from baf11ed to 91b4591 Compare June 22, 2025 07:23
; X86-NEXT: movl %eax, %edx
; X86-NEXT: sarl $31, %edx
; X86-NEXT: addl %ecx, %eax
; X86-NEXT: adcl %esi, %edx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course :)

@RKSimon
Copy link
Collaborator

RKSimon commented Aug 21, 2025

@omkar-mohanty reverse-ping

@omkar-mohanty
Copy link
Author

@omkar-mohanty reverse-ping

Thanks for pinging. I'll update the PR

@RKSimon
Copy link
Collaborator

RKSimon commented Oct 2, 2025

@omkar-mohanty reverse-ping

@harsh241082
Copy link

Hi!
I would like to work on this issue. Please assign it to me.

RKSimon added a commit to RKSimon/llvm-project that referenced this pull request Nov 7, 2025
RKSimon added a commit that referenced this pull request Nov 7, 2025
vinay-deshmukh pushed a commit to vinay-deshmukh/llvm-project that referenced this pull request Nov 8, 2025
@RKSimon
Copy link
Collaborator

RKSimon commented Nov 10, 2025

closing abandoned PR (it can be reopened if necessary)

@RKSimon RKSimon closed this Nov 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[X86] Failure to reduce extended i64 add/sub/mul arithmetic to i32 with known zeros in the upper 32-bits

5 participants