Skip to content

Commit e1433b3

Browse files
committed
[SLP][TLI] Add vectorization support for [u|s]cmp
1 parent d4c519e commit e1433b3

File tree

8 files changed

+2878
-1280
lines changed

8 files changed

+2878
-1280
lines changed

llvm/include/llvm/CodeGen/BasicTTIImpl.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,6 +2196,12 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
21962196
case Intrinsic::bitreverse:
21972197
ISD = ISD::BITREVERSE;
21982198
break;
2199+
case Intrinsic::ucmp:
2200+
ISD = ISD::UCMP;
2201+
break;
2202+
case Intrinsic::scmp:
2203+
ISD = ISD::SCMP;
2204+
break;
21992205
}
22002206

22012207
auto *ST = dyn_cast<StructType>(RetTy);
@@ -2433,6 +2439,37 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
24332439
}
24342440
return Cost;
24352441
}
2442+
case Intrinsic::ucmp:
2443+
case Intrinsic::scmp: {
2444+
InstructionCost Cost = 0;
2445+
bool IsSigned = IID == Intrinsic::scmp;
2446+
ICmpInst::Predicate GTPred =
2447+
IsSigned ? CmpInst::ICMP_SGT : CmpInst::ICMP_UGT;
2448+
ICmpInst::Predicate LTPred =
2449+
IsSigned ? CmpInst::ICMP_SLT : CmpInst::ICMP_ULT;
2450+
Type *CmpTy = Tys[0];
2451+
2452+
if (TLI->shouldExpandCmpUsingSelects()) {
2453+
// x < y ? -1 : (x > y ? 1 : 0)
2454+
Cost += thisT()->getCmpSelInstrCost(BinaryOperator::ICmp, CmpTy, RetTy,
2455+
GTPred, CostKind);
2456+
Cost += thisT()->getCmpSelInstrCost(BinaryOperator::ICmp, CmpTy, RetTy,
2457+
LTPred, CostKind);
2458+
} else {
2459+
// zext(x > y) - zext(x < y)
2460+
Type *CondTy = RetTy->getWithNewBitWidth(1);
2461+
Cost += thisT()->getCmpSelInstrCost(BinaryOperator::ICmp, CmpTy, CondTy,
2462+
GTPred, CostKind);
2463+
Cost += thisT()->getCmpSelInstrCost(BinaryOperator::ICmp, CmpTy, CondTy,
2464+
LTPred, CostKind);
2465+
Cost +=
2466+
2 * thisT()->getCastInstrCost(CastInst::ZExt, RetTy, CondTy,
2467+
TTI::CastContextHint::None, CostKind);
2468+
Cost += thisT()->getArithmeticInstrCost(BinaryOperator::Sub, RetTy,
2469+
CostKind);
2470+
}
2471+
return Cost;
2472+
}
24362473
default:
24372474
break;
24382475
}

llvm/lib/Analysis/VectorUtils.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ bool llvm::isTriviallyVectorizable(Intrinsic::ID ID) {
9797
case Intrinsic::fptoui_sat:
9898
case Intrinsic::lrint:
9999
case Intrinsic::llrint:
100+
case Intrinsic::ucmp:
101+
case Intrinsic::scmp:
100102
return true;
101103
default:
102104
return false;
@@ -132,6 +134,8 @@ bool llvm::isVectorIntrinsicWithOverloadTypeAtArg(Intrinsic::ID ID,
132134
case Intrinsic::fptoui_sat:
133135
case Intrinsic::lrint:
134136
case Intrinsic::llrint:
137+
case Intrinsic::ucmp:
138+
case Intrinsic::scmp:
135139
return OpdIdx == -1 || OpdIdx == 0;
136140
case Intrinsic::is_fpclass:
137141
return OpdIdx == 0;

llvm/test/Analysis/CostModel/X86/icmp-codesize.ll

Lines changed: 332 additions & 240 deletions
Large diffs are not rendered by default.

llvm/test/Analysis/CostModel/X86/icmp-latency.ll

Lines changed: 366 additions & 320 deletions
Large diffs are not rendered by default.

llvm/test/Analysis/CostModel/X86/icmp-sizelatency.ll

Lines changed: 332 additions & 240 deletions
Large diffs are not rendered by default.

llvm/test/Analysis/CostModel/X86/icmp.ll

Lines changed: 480 additions & 480 deletions
Large diffs are not rendered by default.

llvm/test/Transforms/SLPVectorizer/X86/arith-scmp.ll

Lines changed: 646 additions & 0 deletions
Large diffs are not rendered by default.

llvm/test/Transforms/SLPVectorizer/X86/arith-ucmp.ll

Lines changed: 681 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)