[PATCH] D122754: [DAG] Allow XOR(X,MIN_SIGNED_VALUE) to perform AddLike folds
Simon Pilgrim via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Mar 30 11:36:20 PDT 2022
RKSimon created this revision.
RKSimon added reviewers: spatel, lebedev.ri, nikic, craig.topper.
Herald added subscribers: StephenFan, ecnelises, pengfei, hiraditya.
Herald added a project: All.
RKSimon requested review of this revision.
Herald added a project: LLVM.
As raised on PR52267, XOR(X,MIN_SIGNED_VALUE) can be treated as ADD(X,MIN_SIGNED_VALUE), so let these cases use the 'AddLike' folds, similar to how we perform no-common-bits OR(X,Y) cases.
define i8 @src(i8 %x) {
%0:
%r = xor i8 %x, 128
ret i8 %r
}
=>
define i8 @tgt(i8 %x) {
%0:
%r = add i8 %x, 128
ret i8 %r
}
Transformation seems to be correct!
https://alive2.llvm.org/ce/z/qV46E2
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D122754
Files:
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
llvm/test/CodeGen/X86/xor-lea.ll
Index: llvm/test/CodeGen/X86/xor-lea.ll
===================================================================
--- llvm/test/CodeGen/X86/xor-lea.ll
+++ llvm/test/CodeGen/X86/xor-lea.ll
@@ -143,17 +143,15 @@
define i16 @xor_sub_sminval_i16(i16 %x) {
; X86-LABEL: xor_sub_sminval_i16:
; X86: # %bb.0:
-; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
-; X86-NEXT: addl $-2, %eax
-; X86-NEXT: xorl $32768, %eax # imm = 0x8000
+; X86-NEXT: movl $32766, %eax # imm = 0x7FFE
+; X86-NEXT: addl {{[0-9]+}}(%esp), %eax
; X86-NEXT: # kill: def $ax killed $ax killed $eax
; X86-NEXT: retl
;
; X64-LABEL: xor_sub_sminval_i16:
; X64: # %bb.0:
; X64-NEXT: # kill: def $edi killed $edi def $rdi
-; X64-NEXT: leal -2(%rdi), %eax
-; X64-NEXT: xorl $32768, %eax # imm = 0x8000
+; X64-NEXT: leal 32766(%rdi), %eax
; X64-NEXT: # kill: def $ax killed $ax killed $eax
; X64-NEXT: retq
%s = sub i16 %x, 2
@@ -164,16 +162,14 @@
define i32 @xor_add_sminval_i32(i32 %x) {
; X86-LABEL: xor_add_sminval_i32:
; X86: # %bb.0:
-; X86-NEXT: movl $512, %eax # imm = 0x200
+; X86-NEXT: movl $-2147483136, %eax # imm = 0x80000200
; X86-NEXT: addl {{[0-9]+}}(%esp), %eax
-; X86-NEXT: xorl $-2147483648, %eax # imm = 0x80000000
; X86-NEXT: retl
;
; X64-LABEL: xor_add_sminval_i32:
; X64: # %bb.0:
; X64-NEXT: # kill: def $edi killed $edi def $rdi
-; X64-NEXT: leal 512(%rdi), %eax
-; X64-NEXT: xorl $-2147483648, %eax # imm = 0x80000000
+; X64-NEXT: leal -2147483136(%rdi), %eax
; X64-NEXT: retq
%s = add i32 %x, 512
%r = xor i32 %s, 2147483648
Index: llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
===================================================================
--- llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -8297,6 +8297,13 @@
if (SDValue RXOR = reassociateOps(ISD::XOR, DL, N0, N1, N->getFlags()))
return RXOR;
+ // look for AddLike folds:
+ // XOR(N0,MIN_SIGNED_VALUE) == ADD(N0,MIN_SIGNED_VALUE)
+ if (auto *N1C = dyn_cast<ConstantSDNode>(N1))
+ if (N1C->isMinSignedValue())
+ if (SDValue Combined = visitADDLike(N))
+ return Combined;
+
// fold !(x cc y) -> (x !cc y)
unsigned N0Opcode = N0.getOpcode();
SDValue LHS, RHS, CC;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D122754.419231.patch
Type: text/x-patch
Size: 2303 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220330/12fb1c16/attachment.bin>
More information about the llvm-commits
mailing list