Skip to content

Commit 151a80b

Browse files
authored
[TargetLowering][ExpandABD] Prefer selects over usubo if we do the same for ucmp (llvm#159889)
Same deal we use for determining ucmp vs scmp. Using selects on platforms that like selects is better than using usubo. Rename function to be more general fitting this new description.
1 parent a3d7c46 commit 151a80b

File tree

11 files changed

+57
-65
lines changed

11 files changed

+57
-65
lines changed

llvm/include/llvm/CodeGen/BasicTTIImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2929,7 +2929,7 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
29292929
CostKind);
29302930

29312931
EVT VT = TLI->getValueType(DL, CmpTy, true);
2932-
if (TLI->shouldExpandCmpUsingSelects(VT)) {
2932+
if (TLI->preferSelectsOverBooleanArithmetic(VT)) {
29332933
// x < y ? -1 : (x > y ? 1 : 0)
29342934
Cost += 2 * thisT()->getCmpSelInstrCost(
29352935
BinaryOperator::Select, RetTy, CondTy,

llvm/include/llvm/CodeGen/TargetLowering.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3505,9 +3505,10 @@ class LLVM_ABI TargetLoweringBase {
35053505
return isOperationLegalOrCustom(Op, VT);
35063506
}
35073507

3508-
/// Should we expand [US]CMP nodes using two selects and two compares, or by
3509-
/// doing arithmetic on boolean types
3510-
virtual bool shouldExpandCmpUsingSelects(EVT VT) const { return false; }
3508+
/// Should we prefer selects to doing arithmetic on boolean types
3509+
virtual bool preferSelectsOverBooleanArithmetic(EVT VT) const {
3510+
return false;
3511+
}
35113512

35123513
/// True if target has some particular form of dealing with pointer arithmetic
35133514
/// semantics for pointers with the given value type. False if pointer

llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8598,7 +8598,8 @@ LegalizerHelper::lowerThreewayCompare(MachineInstr &MI) {
85988598

85998599
auto &Ctx = MIRBuilder.getMF().getFunction().getContext();
86008600
auto BC = TLI.getBooleanContents(DstTy.isVector(), /*isFP=*/false);
8601-
if (TLI.shouldExpandCmpUsingSelects(getApproximateEVTForLLT(SrcTy, Ctx)) ||
8601+
if (TLI.preferSelectsOverBooleanArithmetic(
8602+
getApproximateEVTForLLT(SrcTy, Ctx)) ||
86028603
BC == TargetLowering::UndefinedBooleanContent) {
86038604
auto One = MIRBuilder.buildConstant(DstTy, 1);
86048605
auto SelectZeroOrOne = MIRBuilder.buildSelect(DstTy, IsGT, One, Zero);

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9775,11 +9775,12 @@ SDValue TargetLowering::expandABD(SDNode *N, SelectionDAG &DAG) const {
97759775
return DAG.getNode(ISD::SUB, dl, VT, Cmp, Xor);
97769776
}
97779777

9778-
// Similar to the branchless expansion, use the (sign-extended) usubo overflow
9779-
// flag if the (scalar) type is illegal as this is more likely to legalize
9780-
// cleanly:
9781-
// abdu(lhs, rhs) -> sub(xor(sub(lhs, rhs), uof(lhs, rhs)), uof(lhs, rhs))
9782-
if (!IsSigned && VT.isScalarInteger() && !isTypeLegal(VT)) {
9778+
// Similar to the branchless expansion, if we don't prefer selects, use the
9779+
// (sign-extended) usubo overflow flag if the (scalar) type is illegal as this
9780+
// is more likely to legalize cleanly: abdu(lhs, rhs) -> sub(xor(sub(lhs,
9781+
// rhs), uof(lhs, rhs)), uof(lhs, rhs))
9782+
if (!IsSigned && VT.isScalarInteger() && !isTypeLegal(VT) &&
9783+
!preferSelectsOverBooleanArithmetic(VT)) {
97839784
SDValue USubO =
97849785
DAG.getNode(ISD::USUBO, dl, DAG.getVTList(VT, MVT::i1), {LHS, RHS});
97859786
SDValue Cmp = DAG.getNode(ISD::SIGN_EXTEND, dl, VT, USubO.getValue(1));
@@ -10974,7 +10975,8 @@ SDValue TargetLowering::expandCMP(SDNode *Node, SelectionDAG &DAG) const {
1097410975
// because one of the conditions can be merged with one of the selects.
1097510976
// And finally, if we don't know the contents of high bits of a boolean value
1097610977
// we can't perform any arithmetic either.
10977-
if (shouldExpandCmpUsingSelects(VT) || BoolVT.getScalarSizeInBits() == 1 ||
10978+
if (preferSelectsOverBooleanArithmetic(VT) ||
10979+
BoolVT.getScalarSizeInBits() == 1 ||
1097810980
getBooleanContents(BoolVT) == UndefinedBooleanContent) {
1097910981
SDValue SelectZeroOrOne =
1098010982
DAG.getSelect(dl, ResVT, IsGT, DAG.getConstant(1, dl, ResVT),

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29388,7 +29388,7 @@ bool AArch64TargetLowering::shouldConvertFpToSat(unsigned Op, EVT FPVT,
2938829388
return TargetLowering::shouldConvertFpToSat(Op, FPVT, VT);
2938929389
}
2939029390

29391-
bool AArch64TargetLowering::shouldExpandCmpUsingSelects(EVT VT) const {
29391+
bool AArch64TargetLowering::preferSelectsOverBooleanArithmetic(EVT VT) const {
2939229392
// Expand scalar and SVE operations using selects. Neon vectors prefer sub to
2939329393
// avoid vselect becoming bsl / unrolling.
2939429394
return !VT.isFixedLengthVector();

llvm/lib/Target/AArch64/AArch64ISelLowering.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ class AArch64TargetLowering : public TargetLowering {
435435

436436
bool shouldConvertFpToSat(unsigned Op, EVT FPVT, EVT VT) const override;
437437

438-
bool shouldExpandCmpUsingSelects(EVT VT) const override;
438+
bool preferSelectsOverBooleanArithmetic(EVT VT) const override;
439439

440440
bool isComplexDeinterleavingSupported() const override;
441441
bool isComplexDeinterleavingOperationSupported(

llvm/lib/Target/ARM/ARMISelLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1482,7 +1482,7 @@ bool ARMTargetLowering::useSoftFloat() const {
14821482
return Subtarget->useSoftFloat();
14831483
}
14841484

1485-
bool ARMTargetLowering::shouldExpandCmpUsingSelects(EVT VT) const {
1485+
bool ARMTargetLowering::preferSelectsOverBooleanArithmetic(EVT VT) const {
14861486
return !Subtarget->isThumb1Only() && VT.getSizeInBits() <= 32;
14871487
}
14881488

llvm/lib/Target/ARM/ARMISelLowering.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ class VectorType;
605605

606606
bool preferZeroCompareBranch() const override { return true; }
607607

608-
bool shouldExpandCmpUsingSelects(EVT VT) const override;
608+
bool preferSelectsOverBooleanArithmetic(EVT VT) const override;
609609

610610
bool isMaskAndCmp0FoldingBeneficial(const Instruction &AndI) const override;
611611

llvm/lib/Target/SystemZ/SystemZISelLowering.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,9 @@ class SystemZTargetLowering : public TargetLowering {
528528

529529
bool shouldConsiderGEPOffsetSplit() const override { return true; }
530530

531-
bool shouldExpandCmpUsingSelects(EVT VT) const override { return true; }
531+
bool preferSelectsOverBooleanArithmetic(EVT VT) const override {
532+
return true;
533+
}
532534

533535
const char *getTargetNodeName(unsigned Opcode) const override;
534536
std::pair<unsigned, const TargetRegisterClass *>

llvm/test/CodeGen/AArch64/abdu-neg.ll

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,11 @@ define i128 @abd_ext_i128(i128 %a, i128 %b) nounwind {
180180
; CHECK-LABEL: abd_ext_i128:
181181
; CHECK: // %bb.0:
182182
; CHECK-NEXT: subs x8, x0, x2
183-
; CHECK-NEXT: sbcs x9, x1, x3
184-
; CHECK-NEXT: cset w10, lo
185-
; CHECK-NEXT: sbfx x10, x10, #0, #1
186-
; CHECK-NEXT: eor x8, x8, x10
187-
; CHECK-NEXT: eor x9, x9, x10
188-
; CHECK-NEXT: subs x8, x8, x10
189-
; CHECK-NEXT: sbc x9, x9, x10
183+
; CHECK-NEXT: sbc x9, x1, x3
184+
; CHECK-NEXT: subs x10, x2, x0
185+
; CHECK-NEXT: sbcs x11, x3, x1
186+
; CHECK-NEXT: csel x8, x8, x10, lo
187+
; CHECK-NEXT: csel x9, x9, x11, lo
190188
; CHECK-NEXT: negs x0, x8
191189
; CHECK-NEXT: ngc x1, x9
192190
; CHECK-NEXT: ret
@@ -203,13 +201,11 @@ define i128 @abd_ext_i128_undef(i128 %a, i128 %b) nounwind {
203201
; CHECK-LABEL: abd_ext_i128_undef:
204202
; CHECK: // %bb.0:
205203
; CHECK-NEXT: subs x8, x0, x2
206-
; CHECK-NEXT: sbcs x9, x1, x3
207-
; CHECK-NEXT: cset w10, lo
208-
; CHECK-NEXT: sbfx x10, x10, #0, #1
209-
; CHECK-NEXT: eor x8, x8, x10
210-
; CHECK-NEXT: eor x9, x9, x10
211-
; CHECK-NEXT: subs x8, x8, x10
212-
; CHECK-NEXT: sbc x9, x9, x10
204+
; CHECK-NEXT: sbc x9, x1, x3
205+
; CHECK-NEXT: subs x10, x2, x0
206+
; CHECK-NEXT: sbcs x11, x3, x1
207+
; CHECK-NEXT: csel x8, x8, x10, lo
208+
; CHECK-NEXT: csel x9, x9, x11, lo
213209
; CHECK-NEXT: negs x0, x8
214210
; CHECK-NEXT: ngc x1, x9
215211
; CHECK-NEXT: ret

0 commit comments

Comments
 (0)