Skip to content

Commit 3e6e54e

Browse files
authored
[X86] Fix miscompile in combineShiftRightArithmetic (#86597)
When folding (ashr (shl, x, c1), c2) we need to treat c1 and c2 as unsigned to find out if the combined shift should be a left or right shift. Also do an early out during pre-legalization in case c1 and c2 has differet types, as that otherwise complicated the comparison of c1 and c2 a bit.
1 parent 14e17ea commit 3e6e54e

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47406,10 +47406,13 @@ static SDValue combineShiftRightArithmetic(SDNode *N, SelectionDAG &DAG,
4740647406
return DAG.getNode(X86ISD::VSRAV, DL, N->getVTList(), N0, ShrAmtVal);
4740747407
}
4740847408

47409-
// fold (ashr (shl, a, [56,48,32,24,16]), SarConst)
47410-
// into (shl, (sext (a), [56,48,32,24,16] - SarConst)) or
47411-
// into (lshr, (sext (a), SarConst - [56,48,32,24,16]))
47412-
// depending on sign of (SarConst - [56,48,32,24,16])
47409+
// fold (SRA (SHL X, ShlConst), SraConst)
47410+
// into (SHL (sext_in_reg X), ShlConst - SraConst)
47411+
// or (sext_in_reg X)
47412+
// or (SRA (sext_in_reg X), SraConst - ShlConst)
47413+
// depending on relation between SraConst and ShlConst.
47414+
// We only do this if (Size - ShlConst) is equal to 8, 16 or 32. That allows
47415+
// us to do the sext_in_reg from corresponding bit.
4741347416

4741447417
// sexts in X86 are MOVs. The MOVs have the same code size
4741547418
// as above SHIFTs (only SHIFT on 1 has lower code size).
@@ -47425,29 +47428,29 @@ static SDValue combineShiftRightArithmetic(SDNode *N, SelectionDAG &DAG,
4742547428
SDValue N00 = N0.getOperand(0);
4742647429
SDValue N01 = N0.getOperand(1);
4742747430
APInt ShlConst = N01->getAsAPIntVal();
47428-
APInt SarConst = N1->getAsAPIntVal();
47431+
APInt SraConst = N1->getAsAPIntVal();
4742947432
EVT CVT = N1.getValueType();
4743047433

47431-
if (SarConst.isNegative())
47434+
if (CVT != N01.getValueType())
47435+
return SDValue();
47436+
if (SraConst.isNegative())
4743247437
return SDValue();
4743347438

4743447439
for (MVT SVT : { MVT::i8, MVT::i16, MVT::i32 }) {
4743547440
unsigned ShiftSize = SVT.getSizeInBits();
47436-
// skipping types without corresponding sext/zext and
47437-
// ShlConst that is not one of [56,48,32,24,16]
47441+
// Only deal with (Size - ShlConst) being equal to 8, 16 or 32.
4743847442
if (ShiftSize >= Size || ShlConst != Size - ShiftSize)
4743947443
continue;
4744047444
SDLoc DL(N);
4744147445
SDValue NN =
4744247446
DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, N00, DAG.getValueType(SVT));
47443-
SarConst = SarConst - (Size - ShiftSize);
47444-
if (SarConst == 0)
47447+
if (SraConst.eq(ShlConst))
4744547448
return NN;
47446-
if (SarConst.isNegative())
47449+
if (SraConst.ult(ShlConst))
4744747450
return DAG.getNode(ISD::SHL, DL, VT, NN,
47448-
DAG.getConstant(-SarConst, DL, CVT));
47451+
DAG.getConstant(ShlConst - SraConst, DL, CVT));
4744947452
return DAG.getNode(ISD::SRA, DL, VT, NN,
47450-
DAG.getConstant(SarConst, DL, CVT));
47453+
DAG.getConstant(SraConst - ShlConst, DL, CVT));
4745147454
}
4745247455
return SDValue();
4745347456
}

llvm/test/CodeGen/X86/sar_fold.ll

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,17 @@ define void @shl144sar48(ptr %p) #0 {
6767
ret void
6868
}
6969

70-
; This is incorrect. The 142 least significant bits in the stored value should
71-
; be zero, and but 142-157 should be taken from %a with a sign-extend into the
72-
; two most significant bits.
7370
define void @shl144sar2(ptr %p) #0 {
7471
; CHECK-LABEL: shl144sar2:
7572
; CHECK: # %bb.0:
7673
; CHECK-NEXT: movl {{[0-9]+}}(%esp), %eax
7774
; CHECK-NEXT: movswl (%eax), %ecx
78-
; CHECK-NEXT: sarl $31, %ecx
75+
; CHECK-NEXT: shll $14, %ecx
7976
; CHECK-NEXT: movl %ecx, 16(%eax)
80-
; CHECK-NEXT: movl %ecx, 8(%eax)
81-
; CHECK-NEXT: movl %ecx, 12(%eax)
82-
; CHECK-NEXT: movl %ecx, 4(%eax)
83-
; CHECK-NEXT: movl %ecx, (%eax)
77+
; CHECK-NEXT: movl $0, 8(%eax)
78+
; CHECK-NEXT: movl $0, 12(%eax)
79+
; CHECK-NEXT: movl $0, 4(%eax)
80+
; CHECK-NEXT: movl $0, (%eax)
8481
; CHECK-NEXT: retl
8582
%a = load i160, ptr %p
8683
%1 = shl i160 %a, 144

0 commit comments

Comments
 (0)