From 05ca9ebc0433b4e19fc67c1dd023414bc6357f09 Mon Sep 17 00:00:00 2001 From: Noah Goldstein Date: Tue, 28 Feb 2023 22:21:53 -0600 Subject: [PATCH] [X86] Make `(shift X (xor/sub N-1, Y))` -> `(shift X, (not Y))` check for one use. `(xor/sub N-1, Y)` -> `(not Y)` is minorly preferable (especially for `(sub N-1, Y)` where it saves an instruction), but isn't worth potentially creating an extra instruction for. So, only do the transformation if `(xor/sub N-1, Y)` has one use. Reviewed By: RKSimon Differential Revision: https://reviews.llvm.org/D144985 --- llvm/lib/Target/X86/X86ISelDAGToDAG.cpp | 2 +- llvm/test/CodeGen/X86/pr61038.ll | 18 +++++++----------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp b/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp index b45b6ee..c13245f 100644 --- a/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp +++ b/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp @@ -4007,7 +4007,7 @@ bool X86DAGToDAGISel::tryShiftAmountMod(SDNode *N) { if (Add1C && Add1C->getAPIntValue().urem(Size) == 0) { NewShiftAmt = Add0; - } else if (ShiftAmt->getOpcode() != ISD::ADD && + } else if (ShiftAmt->getOpcode() != ISD::ADD && ShiftAmt.hasOneUse() && ((Add0C && Add0C->getAPIntValue().urem(Size) == Size - 1) || (Add1C && Add1C->getAPIntValue().urem(Size) == Size - 1))) { // If we are doing a NOT on just the lower bits with (Size*N-1) -/^ X diff --git a/llvm/test/CodeGen/X86/pr61038.ll b/llvm/test/CodeGen/X86/pr61038.ll index 2db7823..82418cf 100644 --- a/llvm/test/CodeGen/X86/pr61038.ll +++ b/llvm/test/CodeGen/X86/pr61038.ll @@ -9,28 +9,24 @@ define void @test_61038(ptr %tmp_buffer) { ; CHECK-BMI2: # %bb.0: # %entry ; CHECK-BMI2-NEXT: tzcntl %eax, %eax ; CHECK-BMI2-NEXT: movabsq $8589934591, %rcx # imm = 0x1FFFFFFFF -; CHECK-BMI2-NEXT: movq $-1, %rdx +; CHECK-BMI2-NEXT: movq %rcx, %rdx ; CHECK-BMI2-NEXT: btcq %rax, %rdx -; CHECK-BMI2-NEXT: shrxq %rdx, %rcx, %rdx -; CHECK-BMI2-NEXT: btcq %rax, %rcx ; CHECK-BMI2-NEXT: xorl %eax, %eax -; CHECK-BMI2-NEXT: cmpq $64, %rcx -; CHECK-BMI2-NEXT: cmovael %eax, %edx -; CHECK-BMI2-NEXT: movl %edx, (%rdi) +; CHECK-BMI2-NEXT: cmpq $64, %rdx +; CHECK-BMI2-NEXT: shrxq %rdx, %rcx, %rcx +; CHECK-BMI2-NEXT: cmovael %eax, %ecx +; CHECK-BMI2-NEXT: movl %ecx, (%rdi) ; CHECK-BMI2-NEXT: retq ; ; CHECK-BMI-LABEL: test_61038: ; CHECK-BMI: # %bb.0: # %entry ; CHECK-BMI-NEXT: tzcntl %eax, %eax ; CHECK-BMI-NEXT: movabsq $8589934591, %rdx # imm = 0x1FFFFFFFF -; CHECK-BMI-NEXT: movq %rdx, %rsi -; CHECK-BMI-NEXT: btcq %rax, %rsi -; CHECK-BMI-NEXT: movq $-1, %rcx +; CHECK-BMI-NEXT: movq %rdx, %rcx ; CHECK-BMI-NEXT: btcq %rax, %rcx -; CHECK-BMI-NEXT: # kill: def $cl killed $cl killed $rcx ; CHECK-BMI-NEXT: shrq %cl, %rdx ; CHECK-BMI-NEXT: xorl %eax, %eax -; CHECK-BMI-NEXT: cmpq $64, %rsi +; CHECK-BMI-NEXT: cmpq $64, %rcx ; CHECK-BMI-NEXT: cmovael %eax, %edx ; CHECK-BMI-NEXT: movl %edx, (%rdi) ; CHECK-BMI-NEXT: retq -- 2.7.4