From 8f82d8ee760360a68de47d75d6a1851dacbdb8ef Mon Sep 17 00:00:00 2001 From: Simon Pilgrim Date: Sat, 6 May 2023 15:54:24 +0100 Subject: [PATCH] [DAG] visitSUBSAT - fold subsat(x,y) -> sub(x,y) if it never overflows --- llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 8 +++++++- llvm/test/CodeGen/X86/combine-sub-ssat.ll | 14 +++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp index 3adc9910c3827..26eef55dd9ed8 100644 --- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -4042,9 +4042,11 @@ SDValue DAGCombiner::visitSUB(SDNode *N) { } SDValue DAGCombiner::visitSUBSAT(SDNode *N) { + unsigned Opcode = N->getOpcode(); SDValue N0 = N->getOperand(0); SDValue N1 = N->getOperand(1); EVT VT = N0.getValueType(); + bool IsSigned = Opcode == ISD::SSUBSAT; SDLoc DL(N); // fold (sub_sat x, undef) -> 0 @@ -4056,7 +4058,7 @@ SDValue DAGCombiner::visitSUBSAT(SDNode *N) { return DAG.getConstant(0, DL, VT); // fold (sub_sat c1, c2) -> c3 - if (SDValue C = DAG.FoldConstantArithmetic(N->getOpcode(), DL, VT, {N0, N1})) + if (SDValue C = DAG.FoldConstantArithmetic(Opcode, DL, VT, {N0, N1})) return C; // fold vector ops @@ -4073,6 +4075,10 @@ SDValue DAGCombiner::visitSUBSAT(SDNode *N) { if (isNullConstant(N1)) return N0; + // If it cannot overflow, transform into an sub. + if (DAG.computeOverflowForSub(IsSigned, N0, N1) == SelectionDAG::OFK_Never) + return DAG.getNode(ISD::SUB, DL, VT, N0, N1); + return SDValue(); } diff --git a/llvm/test/CodeGen/X86/combine-sub-ssat.ll b/llvm/test/CodeGen/X86/combine-sub-ssat.ll index 15c39dd1b666b..979331faf19ec 100644 --- a/llvm/test/CodeGen/X86/combine-sub-ssat.ll +++ b/llvm/test/CodeGen/X86/combine-sub-ssat.ll @@ -119,14 +119,10 @@ define <8 x i16> @combine_self_v8i16(<8 x i16> %a0) { define i32 @combine_no_overflow_i32(i32 %a0, i32 %a1) { ; CHECK-LABEL: combine_no_overflow_i32: ; CHECK: # %bb.0: -; CHECK-NEXT: sarl $16, %edi +; CHECK-NEXT: movl %edi, %eax +; CHECK-NEXT: sarl $16, %eax ; CHECK-NEXT: shrl $16, %esi -; CHECK-NEXT: xorl %eax, %eax -; CHECK-NEXT: cmpl %esi, %edi -; CHECK-NEXT: setns %al -; CHECK-NEXT: addl $2147483647, %eax # imm = 0x7FFFFFFF -; CHECK-NEXT: subl %esi, %edi -; CHECK-NEXT: cmovnol %edi, %eax +; CHECK-NEXT: subl %esi, %eax ; CHECK-NEXT: retq %1 = ashr i32 %a0, 16 %2 = lshr i32 %a1, 16 @@ -139,14 +135,14 @@ define <8 x i16> @combine_no_overflow_v8i16(<8 x i16> %a0, <8 x i16> %a1) { ; SSE: # %bb.0: ; SSE-NEXT: psraw $10, %xmm0 ; SSE-NEXT: psrlw $10, %xmm1 -; SSE-NEXT: psubsw %xmm1, %xmm0 +; SSE-NEXT: psubw %xmm1, %xmm0 ; SSE-NEXT: retq ; ; AVX-LABEL: combine_no_overflow_v8i16: ; AVX: # %bb.0: ; AVX-NEXT: vpsraw $10, %xmm0, %xmm0 ; AVX-NEXT: vpsrlw $10, %xmm1, %xmm1 -; AVX-NEXT: vpsubsw %xmm1, %xmm0, %xmm0 +; AVX-NEXT: vpsubw %xmm1, %xmm0, %xmm0 ; AVX-NEXT: retq %1 = ashr <8 x i16> %a0, %2 = lshr <8 x i16> %a1,