Skip to content

Commit cf0a6b3

Browse files
committed
Modify codegen
1 parent 63a351d commit cf0a6b3

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

llvm/include/llvm/CodeGen/TargetLowering.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3390,6 +3390,10 @@ class TargetLoweringBase {
33903390
return isOperationLegalOrCustom(Op, VT);
33913391
}
33923392

3393+
/// Should we expand [US]CMP nodes using two selects and two compares, or by
3394+
/// doing arithmetic on boolean types
3395+
virtual bool shouldExpandCmpUsingSelects() const { return false; }
3396+
33933397
/// Does this target support complex deinterleaving
33943398
virtual bool isComplexDeinterleavingSupported() const { return false; }
33953399

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10381,14 +10381,28 @@ SDValue TargetLowering::expandCMP(SDNode *Node, SelectionDAG &DAG) const {
1038110381

1038210382
auto LTPredicate = (Opcode == ISD::UCMP ? ISD::SETULT : ISD::SETLT);
1038310383
auto GTPredicate = (Opcode == ISD::UCMP ? ISD::SETUGT : ISD::SETGT);
10384-
1038510384
SDValue IsLT = DAG.getSetCC(dl, BoolVT, LHS, RHS, LTPredicate);
1038610385
SDValue IsGT = DAG.getSetCC(dl, BoolVT, LHS, RHS, GTPredicate);
10387-
SDValue SelectZeroOrOne =
10388-
DAG.getSelect(dl, ResVT, IsGT, DAG.getConstant(1, dl, ResVT),
10389-
DAG.getConstant(0, dl, ResVT));
10390-
return DAG.getSelect(dl, ResVT, IsLT, DAG.getConstant(-1, dl, ResVT),
10391-
SelectZeroOrOne);
10386+
10387+
// We can't perform arithmetic on i1 values. Extending them would
10388+
// probably result in worse codegen, so let's just use two selects instead.
10389+
// Some targets are also just better off using selects rather than subtraction
10390+
// because one of the conditions can be merged with one of the selects.
10391+
// And finally, if we don't know the contents of high bits of a boolean value
10392+
// we can't perform any arithmetic either.
10393+
if (shouldExpandCmpUsingSelects() || BoolVT.getScalarSizeInBits() == 1 ||
10394+
getBooleanContents(BoolVT) == UndefinedBooleanContent) {
10395+
SDValue SelectZeroOrOne =
10396+
DAG.getSelect(dl, ResVT, IsGT, DAG.getConstant(1, dl, ResVT),
10397+
DAG.getConstant(0, dl, ResVT));
10398+
return DAG.getSelect(dl, ResVT, IsLT, DAG.getConstant(-1, dl, ResVT),
10399+
SelectZeroOrOne);
10400+
}
10401+
10402+
if (getBooleanContents(BoolVT) == ZeroOrNegativeOneBooleanContent)
10403+
std::swap(LHS, RHS);
10404+
return DAG.getSExtOrTrunc(DAG.getNode(ISD::SUB, dl, BoolVT, IsGT, IsLT), dl,
10405+
ResVT);
1039210406
}
1039310407

1039410408
SDValue TargetLowering::expandShlSat(SDNode *Node, SelectionDAG &DAG) const {

llvm/lib/Target/AArch64/AArch64ISelLowering.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,8 @@ class AArch64TargetLowering : public TargetLowering {
907907

908908
bool shouldConvertFpToSat(unsigned Op, EVT FPVT, EVT VT) const override;
909909

910+
bool shouldExpandCmpUsingSelects() const override { return true; }
911+
910912
bool isComplexDeinterleavingSupported() const override;
911913
bool isComplexDeinterleavingOperationSupported(
912914
ComplexDeinterleavingOperation Operation, Type *Ty) const override;

llvm/lib/Target/SystemZ/SystemZISelLowering.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,8 @@ class SystemZTargetLowering : public TargetLowering {
507507

508508
bool shouldConsiderGEPOffsetSplit() const override { return true; }
509509

510+
bool shouldExpandCmpUsingSelects() const override { return true; }
511+
510512
const char *getTargetNodeName(unsigned Opcode) const override;
511513
std::pair<unsigned, const TargetRegisterClass *>
512514
getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,

0 commit comments

Comments
 (0)