Skip to content

Commit 92a0389

Browse files
committed
[x86] add special-case lowering for usubsat for pre-SSE4
usubsat X, SMIN --> (X ^ SMIN) & (X s>> BW-1) This would be a regression with D112085 where we combine to usubsat more aggressively, so avoid that by matching the special-case where we are subtracting SMIN (signmask): https://alive2.llvm.org/ce/z/4_3gBD Differential Revision: https://reviews.llvm.org/D112095
1 parent 17386cb commit 92a0389

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28135,7 +28135,19 @@ static SDValue LowerADDSAT_SUBSAT(SDValue Op, SelectionDAG &DAG,
2813528135
EVT SetCCResultType =
2813628136
TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
2813728137

28138+
unsigned BitWidth = VT.getScalarSizeInBits();
2813828139
if (Opcode == ISD::USUBSAT && !TLI.isOperationLegal(ISD::UMAX, VT)) {
28140+
// Handle a special-case with a bit-hack instead of cmp+select:
28141+
// usubsat X, SMIN --> (X ^ SMIN) & (X s>> BW-1)
28142+
ConstantSDNode *C = isConstOrConstSplat(Y, true);
28143+
if (C && C->getAPIntValue().isSignMask()) {
28144+
SDValue SignMask = DAG.getConstant(C->getAPIntValue(), DL, VT);
28145+
SDValue ShiftAmt = DAG.getConstant(BitWidth - 1, DL, VT);
28146+
SDValue Xor = DAG.getNode(ISD::XOR, DL, VT, X, SignMask);
28147+
SDValue Sra = DAG.getNode(ISD::SRA, DL, VT, X, ShiftAmt);
28148+
return DAG.getNode(ISD::AND, DL, VT, Xor, Sra);
28149+
}
28150+
2813928151
// usubsat X, Y --> (X >u Y) ? X - Y : 0
2814028152
SDValue Sub = DAG.getNode(ISD::SUB, DL, VT, X, Y);
2814128153
SDValue Cmp = DAG.getSetCC(DL, SetCCResultType, X, Y, ISD::SETUGT);
@@ -28148,7 +28160,6 @@ static SDValue LowerADDSAT_SUBSAT(SDValue Op, SelectionDAG &DAG,
2814828160

2814928161
if ((Opcode == ISD::SADDSAT || Opcode == ISD::SSUBSAT) &&
2815028162
(!VT.isVector() || VT == MVT::v2i64)) {
28151-
unsigned BitWidth = VT.getScalarSizeInBits();
2815228163
APInt MinVal = APInt::getSignedMinValue(BitWidth);
2815328164
APInt MaxVal = APInt::getSignedMaxValue(BitWidth);
2815428165
SDValue Zero = DAG.getConstant(0, DL, VT);

llvm/test/CodeGen/X86/psubus.ll

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ vector.ph:
2929
}
3030

3131
; This is logically equivalent to the above.
32+
; usubsat X, (1 << (BW-1)) <--> (X ^ (1 << (BW-1))) & (ashr X, (BW-1))
3233

3334
define <8 x i16> @ashr_xor_and(<8 x i16> %x) nounwind {
3435
; SSE-LABEL: ashr_xor_and:
@@ -127,14 +128,14 @@ define <4 x i32> @ashr_xor_and_custom(<4 x i32> %x) nounwind {
127128
ret <4 x i32> %res
128129
}
129130

131+
; usubsat X, (1 << (BW-1)) <--> (X ^ (1 << (BW-1))) & (ashr X, (BW-1))
132+
130133
define <4 x i32> @usubsat_custom(<4 x i32> %x) nounwind {
131134
; SSE2OR3-LABEL: usubsat_custom:
132135
; SSE2OR3: # %bb.0:
133-
; SSE2OR3-NEXT: movdqa {{.*#+}} xmm1 = [2147483648,2147483648,2147483648,2147483648]
134-
; SSE2OR3-NEXT: pxor %xmm0, %xmm1
135-
; SSE2OR3-NEXT: pxor %xmm2, %xmm2
136-
; SSE2OR3-NEXT: pcmpgtd %xmm2, %xmm1
137-
; SSE2OR3-NEXT: psubd {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
136+
; SSE2OR3-NEXT: movdqa %xmm0, %xmm1
137+
; SSE2OR3-NEXT: psrad $31, %xmm1
138+
; SSE2OR3-NEXT: pxor {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
138139
; SSE2OR3-NEXT: pand %xmm1, %xmm0
139140
; SSE2OR3-NEXT: retq
140141
;

0 commit comments

Comments
 (0)