Skip to content

Commit ee959dd

Browse files
committed
[TTI] Remove getOperationCost
This API call has been used recently with, a very valid, expectation that it would do something useful but it doesn't actually query any backend information. So, remove this method and merge its functionality into getUserCost. As well as that, also use getCastInstrCost to get a proper cost from the backend for the concerned instructions though we only currently return the answer if it's considered free. The default implementation now also checks int/ptr conversions too, as well as truncs and bitcasts. Differential Revision: https://reviews.llvm.org/D76124
1 parent e90fb82 commit ee959dd

File tree

4 files changed

+79
-157
lines changed

4 files changed

+79
-157
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -201,35 +201,11 @@ class TargetTransformInfo {
201201
TCC_Expensive = 4 ///< The cost of a 'div' instruction on x86.
202202
};
203203

204-
/// Estimate the cost of a specific operation when lowered.
205-
///
206-
/// Note that this is designed to work on an arbitrary synthetic opcode, and
207-
/// thus work for hypothetical queries before an instruction has even been
208-
/// formed. However, this does *not* work for GEPs, and must not be called
209-
/// for a GEP instruction. Instead, use the dedicated getGEPCost interface as
210-
/// analyzing a GEP's cost required more information.
211-
///
212-
/// Typically only the result type is required, and the operand type can be
213-
/// omitted. However, if the opcode is one of the cast instructions, the
214-
/// operand type is required.
215-
///
216-
/// The returned cost is defined in terms of \c TargetCostConstants, see its
217-
/// comments for a detailed explanation of the cost values.
218-
int getOperationCost(unsigned Opcode, Type *Ty, Type *OpTy = nullptr) const;
219-
220204
/// Estimate the cost of a GEP operation when lowered.
221-
///
222-
/// The contract for this function is the same as \c getOperationCost except
223-
/// that it supports an interface that provides extra information specific to
224-
/// the GEP operation.
225205
int getGEPCost(Type *PointeeType, const Value *Ptr,
226206
ArrayRef<const Value *> Operands) const;
227207

228208
/// Estimate the cost of a EXT operation when lowered.
229-
///
230-
/// The contract for this function is the same as \c getOperationCost except
231-
/// that it supports an interface that provides extra information specific to
232-
/// the EXT operation.
233209
int getExtCost(const Instruction *I, const Value *Src) const;
234210

235211
/// \returns A value by which our inlining threshold should be multiplied.
@@ -277,15 +253,7 @@ class TargetTransformInfo {
277253
/// Estimate the cost of a given IR user when lowered.
278254
///
279255
/// This can estimate the cost of either a ConstantExpr or Instruction when
280-
/// lowered. It has two primary advantages over the \c getOperationCost and
281-
/// \c getGEPCost above, and one significant disadvantage: it can only be
282-
/// used when the IR construct has already been formed.
283-
///
284-
/// The advantages are that it can inspect the SSA use graph to reason more
285-
/// accurately about the cost. For example, all-constant-GEPs can often be
286-
/// folded into a load or other instruction, but if they are used in some
287-
/// other context they may not be folded. This routine can distinguish such
288-
/// cases.
256+
/// lowered.
289257
///
290258
/// \p Operands is a list of operands which can be a result of transformations
291259
/// of the current operands. The number of the operands on the list must equal
@@ -1187,7 +1155,6 @@ class TargetTransformInfo::Concept {
11871155
public:
11881156
virtual ~Concept() = 0;
11891157
virtual const DataLayout &getDataLayout() const = 0;
1190-
virtual int getOperationCost(unsigned Opcode, Type *Ty, Type *OpTy) = 0;
11911158
virtual int getGEPCost(Type *PointeeType, const Value *Ptr,
11921159
ArrayRef<const Value *> Operands) = 0;
11931160
virtual int getExtCost(const Instruction *I, const Value *Src) = 0;
@@ -1429,9 +1396,6 @@ class TargetTransformInfo::Model final : public TargetTransformInfo::Concept {
14291396
return Impl.getDataLayout();
14301397
}
14311398

1432-
int getOperationCost(unsigned Opcode, Type *Ty, Type *OpTy) override {
1433-
return Impl.getOperationCost(Opcode, Ty, OpTy);
1434-
}
14351399
int getGEPCost(Type *PointeeType, const Value *Ptr,
14361400
ArrayRef<const Value *> Operands) override {
14371401
return Impl.getGEPCost(PointeeType, Ptr, Operands);

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

Lines changed: 78 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -44,69 +44,6 @@ class TargetTransformInfoImplBase {
4444

4545
const DataLayout &getDataLayout() const { return DL; }
4646

47-
unsigned getOperationCost(unsigned Opcode, Type *Ty, Type *OpTy) {
48-
switch (Opcode) {
49-
default:
50-
// By default, just classify everything as 'basic'.
51-
return TTI::TCC_Basic;
52-
53-
case Instruction::GetElementPtr:
54-
llvm_unreachable("Use getGEPCost for GEP operations!");
55-
56-
case Instruction::BitCast:
57-
assert(OpTy && "Cast instructions must provide the operand type");
58-
if (Ty == OpTy || (Ty->isPointerTy() && OpTy->isPointerTy()))
59-
// Identity and pointer-to-pointer casts are free.
60-
return TTI::TCC_Free;
61-
62-
// Otherwise, the default basic cost is used.
63-
return TTI::TCC_Basic;
64-
65-
case Instruction::Freeze:
66-
// Freeze operation is free because it should be lowered into a register
67-
// use without any register copy in assembly code.
68-
return TTI::TCC_Free;
69-
70-
case Instruction::FDiv:
71-
case Instruction::FRem:
72-
case Instruction::SDiv:
73-
case Instruction::SRem:
74-
case Instruction::UDiv:
75-
case Instruction::URem:
76-
return TTI::TCC_Expensive;
77-
78-
case Instruction::IntToPtr: {
79-
// An inttoptr cast is free so long as the input is a legal integer type
80-
// which doesn't contain values outside the range of a pointer.
81-
unsigned OpSize = OpTy->getScalarSizeInBits();
82-
if (DL.isLegalInteger(OpSize) &&
83-
OpSize <= DL.getPointerTypeSizeInBits(Ty))
84-
return TTI::TCC_Free;
85-
86-
// Otherwise it's not a no-op.
87-
return TTI::TCC_Basic;
88-
}
89-
case Instruction::PtrToInt: {
90-
// A ptrtoint cast is free so long as the result is large enough to store
91-
// the pointer, and a legal integer type.
92-
unsigned DestSize = Ty->getScalarSizeInBits();
93-
if (DL.isLegalInteger(DestSize) &&
94-
DestSize >= DL.getPointerTypeSizeInBits(OpTy))
95-
return TTI::TCC_Free;
96-
97-
// Otherwise it's not a no-op.
98-
return TTI::TCC_Basic;
99-
}
100-
case Instruction::Trunc:
101-
// trunc to a native type is free (assuming the target has compare and
102-
// shift-right of the same width).
103-
if (DL.isLegalInteger(DL.getTypeSizeInBits(Ty)))
104-
return TTI::TCC_Free;
105-
106-
return TTI::TCC_Basic;
107-
}
108-
}
109-
11047
int getGEPCost(Type *PointeeType, const Value *Ptr,
11148
ArrayRef<const Value *> Operands) {
11249
// In the basic model, we just assume that all-constant GEPs will be folded
@@ -445,6 +382,35 @@ class TargetTransformInfoImplBase {
445382

446383
unsigned getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src,
447384
const Instruction *I) {
385+
switch (Opcode) {
386+
default:
387+
break;
388+
case Instruction::IntToPtr: {
389+
unsigned SrcSize = Src->getScalarSizeInBits();
390+
if (DL.isLegalInteger(SrcSize) &&
391+
SrcSize <= DL.getPointerTypeSizeInBits(Dst))
392+
return 0;
393+
break;
394+
}
395+
case Instruction::PtrToInt: {
396+
unsigned DstSize = Dst->getScalarSizeInBits();
397+
if (DL.isLegalInteger(DstSize) &&
398+
DstSize >= DL.getPointerTypeSizeInBits(Src))
399+
return 0;
400+
break;
401+
}
402+
case Instruction::BitCast:
403+
if (Dst == Src || (Dst->isPointerTy() && Src->isPointerTy()))
404+
// Identity and pointer-to-pointer casts are free.
405+
return 0;
406+
break;
407+
case Instruction::Trunc:
408+
// trunc to a native type is free (assuming the target has compare and
409+
// shift-right of the same width).
410+
if (DL.isLegalInteger(DL.getTypeSizeInBits(Dst)))
411+
return 0;
412+
break;
413+
}
448414
return 1;
449415
}
450416

@@ -828,27 +794,8 @@ class TargetTransformInfoImplCRTPBase : public TargetTransformInfoImplBase {
828794
}
829795

830796
unsigned getUserCost(const User *U, ArrayRef<const Value *> Operands) {
831-
if (isa<PHINode>(U))
832-
return TTI::TCC_Free; // Model all PHI nodes as free.
833-
834-
if (isa<ExtractValueInst>(U))
835-
return TTI::TCC_Free; // Model all ExtractValue nodes as free.
836-
837-
if (isa<FreezeInst>(U))
838-
return TTI::TCC_Free; // Model all Freeze nodes as free.
839-
840-
// Static alloca doesn't generate target instructions.
841-
if (auto *A = dyn_cast<AllocaInst>(U))
842-
if (A->isStaticAlloca())
843-
return TTI::TCC_Free;
844-
845797
auto *TargetTTI = static_cast<T *>(this);
846798

847-
if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U))
848-
return TargetTTI->getGEPCost(GEP->getSourceElementType(),
849-
GEP->getPointerOperand(),
850-
Operands.drop_front());
851-
852799
if (auto CS = ImmutableCallSite(U)) {
853800
const Function *F = CS.getCalledFunction();
854801
if (F) {
@@ -867,14 +814,55 @@ class TargetTransformInfoImplCRTPBase : public TargetTransformInfoImplBase {
867814
return TTI::TCC_Basic * (CS.arg_size() + 1);
868815
}
869816

870-
if (isa<SExtInst>(U) || isa<ZExtInst>(U) || isa<FPExtInst>(U))
871-
// The old behaviour of generally treating extensions of icmp to be free
872-
// has been removed. A target that needs it should override getUserCost().
873-
return TargetTTI->getExtCost(cast<Instruction>(U), Operands.back());
874-
875-
return TargetTTI->getOperationCost(
876-
Operator::getOpcode(U), U->getType(),
877-
U->getNumOperands() == 1 ? U->getOperand(0)->getType() : nullptr);
817+
Type *Ty = U->getType();
818+
Type *OpTy =
819+
U->getNumOperands() == 1 ? U->getOperand(0)->getType() : nullptr;
820+
unsigned Opcode = Operator::getOpcode(U);
821+
auto *I = dyn_cast<Instruction>(U);
822+
switch (Opcode) {
823+
default:
824+
break;
825+
case Instruction::PHI:
826+
case Instruction::ExtractValue:
827+
case Instruction::Freeze:
828+
return TTI::TCC_Free;
829+
case Instruction::Alloca:
830+
if (cast<AllocaInst>(U)->isStaticAlloca())
831+
return TTI::TCC_Free;
832+
break;
833+
case Instruction::GetElementPtr: {
834+
const GEPOperator *GEP = cast<GEPOperator>(U);
835+
return TargetTTI->getGEPCost(GEP->getSourceElementType(),
836+
GEP->getPointerOperand(),
837+
Operands.drop_front());
838+
}
839+
case Instruction::FDiv:
840+
case Instruction::FRem:
841+
case Instruction::SDiv:
842+
case Instruction::SRem:
843+
case Instruction::UDiv:
844+
case Instruction::URem:
845+
return TTI::TCC_Expensive;
846+
case Instruction::IntToPtr:
847+
case Instruction::PtrToInt:
848+
case Instruction::Trunc:
849+
if (getCastInstrCost(Opcode, Ty, OpTy, I) == TTI::TCC_Free ||
850+
TargetTTI->getCastInstrCost(Opcode, Ty, OpTy, I) == TTI::TCC_Free)
851+
return TTI::TCC_Free;
852+
break;
853+
case Instruction::BitCast:
854+
if (getCastInstrCost(Opcode, Ty, OpTy, I) == TTI::TCC_Free)
855+
return TTI::TCC_Free;
856+
break;
857+
case Instruction::FPExt:
858+
case Instruction::SExt:
859+
case Instruction::ZExt:
860+
if (TargetTTI->getExtCost(I, Operands.back()) == TTI::TCC_Free)
861+
return TTI::TCC_Free;
862+
break;
863+
}
864+
// By default, just classify everything as 'basic'.
865+
return TTI::TCC_Basic;
878866
}
879867

880868
int getInstructionLatency(const Instruction *I) {

llvm/include/llvm/CodeGen/BasicTTIImpl.h

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -412,29 +412,6 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
412412
return TargetTransformInfo::TCC_Expensive;
413413
}
414414

415-
unsigned getOperationCost(unsigned Opcode, Type *Ty, Type *OpTy) {
416-
const TargetLoweringBase *TLI = getTLI();
417-
switch (Opcode) {
418-
default: break;
419-
case Instruction::Trunc:
420-
if (TLI->isTruncateFree(OpTy, Ty))
421-
return TargetTransformInfo::TCC_Free;
422-
return TargetTransformInfo::TCC_Basic;
423-
case Instruction::ZExt:
424-
if (TLI->isZExtFree(OpTy, Ty))
425-
return TargetTransformInfo::TCC_Free;
426-
return TargetTransformInfo::TCC_Basic;
427-
428-
case Instruction::AddrSpaceCast:
429-
if (TLI->isFreeAddrSpaceCast(OpTy->getPointerAddressSpace(),
430-
Ty->getPointerAddressSpace()))
431-
return TargetTransformInfo::TCC_Free;
432-
return TargetTransformInfo::TCC_Basic;
433-
}
434-
435-
return BaseT::getOperationCost(Opcode, Ty, OpTy);
436-
}
437-
438415
unsigned getInliningThresholdMultiplier() { return 1; }
439416

440417
int getInlinerVectorBonusPercent() { return 150; }

llvm/lib/Analysis/TargetTransformInfo.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,6 @@ TargetTransformInfo &TargetTransformInfo::operator=(TargetTransformInfo &&RHS) {
146146
return *this;
147147
}
148148

149-
int TargetTransformInfo::getOperationCost(unsigned Opcode, Type *Ty,
150-
Type *OpTy) const {
151-
int Cost = TTIImpl->getOperationCost(Opcode, Ty, OpTy);
152-
assert(Cost >= 0 && "TTI should not produce negative costs!");
153-
return Cost;
154-
}
155-
156149
unsigned TargetTransformInfo::getInliningThresholdMultiplier() const {
157150
return TTIImpl->getInliningThresholdMultiplier();
158151
}

0 commit comments

Comments
 (0)