Skip to content

Commit 853c43d

Browse files
authored
[TTI] NFC: Port TLI.shouldSinkOperands to TTI (#110564)
Porting to TTI provides direct access to the instruction cost model, which can enable instruction cost based sinking without introducing code duplication.
1 parent 35684fa commit 853c43d

29 files changed

+901
-871
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,21 @@ class TargetTransformInfo {
17471747
bool hasActiveVectorLength(unsigned Opcode, Type *DataType,
17481748
Align Alignment) const;
17491749

1750+
/// Return true if sinking I's operands to the same basic block as I is
1751+
/// profitable, e.g. because the operands can be folded into a target
1752+
/// instruction during instruction selection. After calling the function
1753+
/// \p Ops contains the Uses to sink ordered by dominance (dominating users
1754+
/// come first).
1755+
bool isProfitableToSinkOperands(Instruction *I,
1756+
SmallVectorImpl<Use *> &Ops) const;
1757+
1758+
/// Return true if it's significantly cheaper to shift a vector by a uniform
1759+
/// scalar than by an amount which will vary across each lane. On x86 before
1760+
/// AVX2 for example, there is a "psllw" instruction for the former case, but
1761+
/// no simple instruction for a general "a << b" operation on vectors.
1762+
/// This should also apply to lowering for vector funnel shifts (rotates).
1763+
bool isVectorShiftByScalarCheap(Type *Ty) const;
1764+
17501765
struct VPLegalization {
17511766
enum VPTransform {
17521767
// keep the predicating parameter
@@ -2187,6 +2202,11 @@ class TargetTransformInfo::Concept {
21872202
virtual bool supportsScalableVectors() const = 0;
21882203
virtual bool hasActiveVectorLength(unsigned Opcode, Type *DataType,
21892204
Align Alignment) const = 0;
2205+
virtual bool
2206+
isProfitableToSinkOperands(Instruction *I,
2207+
SmallVectorImpl<Use *> &OpsToSink) const = 0;
2208+
2209+
virtual bool isVectorShiftByScalarCheap(Type *Ty) const = 0;
21902210
virtual VPLegalization
21912211
getVPLegalizationStrategy(const VPIntrinsic &PI) const = 0;
21922212
virtual bool hasArmWideBranch(bool Thumb) const = 0;
@@ -2963,6 +2983,15 @@ class TargetTransformInfo::Model final : public TargetTransformInfo::Concept {
29632983
return Impl.hasActiveVectorLength(Opcode, DataType, Alignment);
29642984
}
29652985

2986+
bool isProfitableToSinkOperands(Instruction *I,
2987+
SmallVectorImpl<Use *> &Ops) const override {
2988+
return Impl.isProfitableToSinkOperands(I, Ops);
2989+
};
2990+
2991+
bool isVectorShiftByScalarCheap(Type *Ty) const override {
2992+
return Impl.isVectorShiftByScalarCheap(Ty);
2993+
}
2994+
29662995
VPLegalization
29672996
getVPLegalizationStrategy(const VPIntrinsic &PI) const override {
29682997
return Impl.getVPLegalizationStrategy(PI);

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,13 @@ class TargetTransformInfoImplBase {
977977
return false;
978978
}
979979

980+
bool isProfitableToSinkOperands(Instruction *I,
981+
SmallVectorImpl<Use *> &Ops) const {
982+
return false;
983+
}
984+
985+
bool isVectorShiftByScalarCheap(Type *Ty) const { return false; }
986+
980987
TargetTransformInfo::VPLegalization
981988
getVPLegalizationStrategy(const VPIntrinsic &PI) const {
982989
return TargetTransformInfo::VPLegalization(

llvm/include/llvm/CodeGen/TargetLowering.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2860,15 +2860,6 @@ class TargetLoweringBase {
28602860
return Value == 0;
28612861
}
28622862

2863-
/// Return true if it's significantly cheaper to shift a vector by a uniform
2864-
/// scalar than by an amount which will vary across each lane. On x86 before
2865-
/// AVX2 for example, there is a "psllw" instruction for the former case, but
2866-
/// no simple instruction for a general "a << b" operation on vectors.
2867-
/// This should also apply to lowering for vector funnel shifts (rotates).
2868-
virtual bool isVectorShiftByScalarCheap(Type *Ty) const {
2869-
return false;
2870-
}
2871-
28722863
/// Given a shuffle vector SVI representing a vector splat, return a new
28732864
/// scalar type of size equal to SVI's scalar type if the new type is more
28742865
/// profitable. Returns nullptr otherwise. For example under MVE float splats
@@ -3085,16 +3076,6 @@ class TargetLoweringBase {
30853076
/// a larger type.
30863077
virtual bool signExtendConstant(const ConstantInt *C) const { return false; }
30873078

3088-
/// Return true if sinking I's operands to the same basic block as I is
3089-
/// profitable, e.g. because the operands can be folded into a target
3090-
/// instruction during instruction selection. After calling the function
3091-
/// \p Ops contains the Uses to sink ordered by dominance (dominating users
3092-
/// come first).
3093-
virtual bool shouldSinkOperands(Instruction *I,
3094-
SmallVectorImpl<Use *> &Ops) const {
3095-
return false;
3096-
}
3097-
30983079
/// Try to optimize extending or truncating conversion instructions (like
30993080
/// zext, trunc, fptoui, uitofp) for the target.
31003081
virtual bool

llvm/lib/Analysis/TargetTransformInfo.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,15 @@ bool TargetTransformInfo::hasActiveVectorLength(unsigned Opcode, Type *DataType,
13591359
return TTIImpl->hasActiveVectorLength(Opcode, DataType, Alignment);
13601360
}
13611361

1362+
bool TargetTransformInfo::isProfitableToSinkOperands(
1363+
Instruction *I, SmallVectorImpl<Use *> &OpsToSink) const {
1364+
return TTIImpl->isProfitableToSinkOperands(I, OpsToSink);
1365+
}
1366+
1367+
bool TargetTransformInfo::isVectorShiftByScalarCheap(Type *Ty) const {
1368+
return TTIImpl->isVectorShiftByScalarCheap(Ty);
1369+
}
1370+
13621371
TargetTransformInfo::Concept::~Concept() = default;
13631372

13641373
TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}

llvm/lib/CodeGen/CodeGenPrepare.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7274,7 +7274,7 @@ bool CodeGenPrepare::optimizeShiftInst(BinaryOperator *Shift) {
72747274
// We can't do this effectively in SDAG because we may not be able to
72757275
// determine if the select operands are splats from within a basic block.
72767276
Type *Ty = Shift->getType();
7277-
if (!Ty->isVectorTy() || !TLI->isVectorShiftByScalarCheap(Ty))
7277+
if (!Ty->isVectorTy() || !TTI->isVectorShiftByScalarCheap(Ty))
72787278
return false;
72797279
Value *Cond, *TVal, *FVal;
72807280
if (!match(Shift->getOperand(1),
@@ -7309,7 +7309,7 @@ bool CodeGenPrepare::optimizeFunnelShift(IntrinsicInst *Fsh) {
73097309
// We can't do this effectively in SDAG because we may not be able to
73107310
// determine if the select operands are splats from within a basic block.
73117311
Type *Ty = Fsh->getType();
7312-
if (!Ty->isVectorTy() || !TLI->isVectorShiftByScalarCheap(Ty))
7312+
if (!Ty->isVectorTy() || !TTI->isVectorShiftByScalarCheap(Ty))
73137313
return false;
73147314
Value *Cond, *TVal, *FVal;
73157315
if (!match(Fsh->getOperand(2),
@@ -7566,7 +7566,7 @@ bool CodeGenPrepare::tryToSinkFreeOperands(Instruction *I) {
75667566
// If the operands of I can be folded into a target instruction together with
75677567
// I, duplicate and sink them.
75687568
SmallVector<Use *, 4> OpsToSink;
7569-
if (!TLI->shouldSinkOperands(I, OpsToSink))
7569+
if (!TTI->isProfitableToSinkOperands(I, OpsToSink))
75707570
return false;
75717571

75727572
// OpsToSink can contain multiple uses in a use chain (e.g.

0 commit comments

Comments
 (0)