Skip to content

Commit e89faba

Browse files
committed
[DAGCombiner] Fix issue #121372
PR #118823 added a DAG combine for extracting elements of a vector returned from SETCC, however it doesn't correctly deal with the case where the vector element type is not i1. In this case we have to take account of the boolean contents, which are represent differently between vectors and scalars. The code now explicitly performs an inreg sign extend in order to get the same result. Fixes #121372
1 parent 5e6b156 commit e89faba

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22808,9 +22808,26 @@ static SDValue scalarizeExtractedBinOp(SDNode *ExtElt, SelectionDAG &DAG,
2280822808
return SDValue();
2280922809

2281022810
EVT ResVT = ExtElt->getValueType(0);
22811-
if (Opc == ISD::SETCC &&
22812-
(ResVT != Vec.getValueType().getVectorElementType() || LegalTypes))
22813-
return SDValue();
22811+
bool SetCCNeedsSignExt = false;
22812+
if (Opc == ISD::SETCC) {
22813+
EVT VecVT = Vec.getValueType();
22814+
if (ResVT != VecVT.getVectorElementType() || LegalTypes)
22815+
return SDValue();
22816+
22817+
if (ResVT != MVT::i1) {
22818+
bool VecRequiresSignExt = TLI.getBooleanContents(VecVT) ==
22819+
TargetLowering::ZeroOrNegativeOneBooleanContent;
22820+
bool ScalarRequiresSignExt =
22821+
TLI.getBooleanContents(ResVT) ==
22822+
TargetLowering::ZeroOrNegativeOneBooleanContent;
22823+
if (VecRequiresSignExt && !ScalarRequiresSignExt)
22824+
SetCCNeedsSignExt = true;
22825+
else if (!VecRequiresSignExt && ScalarRequiresSignExt) {
22826+
// There are currently no targets with this behaviour.
22827+
return SDValue();
22828+
}
22829+
}
22830+
}
2281422831

2281522832
// Targets may want to avoid this to prevent an expensive register transfer.
2281622833
if (!TLI.shouldScalarizeBinop(Vec))
@@ -22834,8 +22851,14 @@ static SDValue scalarizeExtractedBinOp(SDNode *ExtElt, SelectionDAG &DAG,
2283422851
EVT OpVT = Op0.getValueType().getVectorElementType();
2283522852
Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, OpVT, Op0, Index);
2283622853
Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, OpVT, Op1, Index);
22837-
return DAG.getSetCC(DL, ResVT, Op0, Op1,
22838-
cast<CondCodeSDNode>(Vec->getOperand(2))->get());
22854+
SDValue NewVal = DAG.getSetCC(
22855+
DL, ResVT, Op0, Op1, cast<CondCodeSDNode>(Vec->getOperand(2))->get());
22856+
// We may need to sign-extend the result to match the same behaviour as the
22857+
// vector version of SETCC.
22858+
if (SetCCNeedsSignExt)
22859+
NewVal = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, ResVT, NewVal,
22860+
DAG.getValueType(MVT::i1));
22861+
return NewVal;
2283922862
}
2284022863
Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResVT, Op0, Index);
2284122864
Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResVT, Op1, Index);

llvm/test/CodeGen/AArch64/extract-vector-cmp.ll

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ define i128 @extract_icmp_v1i128(ptr %p) {
5858
; CHECK-LABEL: extract_icmp_v1i128:
5959
; CHECK: // %bb.0:
6060
; CHECK-NEXT: ldp x9, x8, [x0]
61-
; CHECK-NEXT: mov x1, xzr
6261
; CHECK-NEXT: orr x8, x9, x8
6362
; CHECK-NEXT: cmp x8, #0
64-
; CHECK-NEXT: cset w0, eq
63+
; CHECK-NEXT: cset w8, eq
64+
; CHECK-NEXT: sbfx x0, x8, #0, #1
65+
; CHECK-NEXT: mov x1, x0
6566
; CHECK-NEXT: ret
6667
%load = load <1 x i128>, ptr %p, align 16
6768
%cmp = icmp eq <1 x i128> %load, zeroinitializer
@@ -141,12 +142,14 @@ for.cond.cleanup:
141142
}
142143

143144

145+
; TODO: Combine the sbfx(cset) into a csetm
144146
define i32 @issue_121372(<4 x i32> %v) {
145147
; CHECK-LABEL: issue_121372:
146148
; CHECK: // %bb.0:
147149
; CHECK-NEXT: fmov w8, s0
148150
; CHECK-NEXT: cmp w8, #0
149151
; CHECK-NEXT: cset w8, eq
152+
; CHECK-NEXT: sbfx w8, w8, #0, #1
150153
; CHECK-NEXT: cmp w8, #1
151154
; CHECK-NEXT: csetm w0, lt
152155
; CHECK-NEXT: ret

0 commit comments

Comments
 (0)