Skip to content

Commit 40574fe

Browse files
committed
[NFC][CostModel] Add TargetCostKind to relevant APIs
Make the kind of cost explicit throughout the cost model which, apart from making the cost clear, will allow the generic parts to calculate better costs. It will also allow some backends to approximate and correlate the different costs if they wish. Another benefit is that it will also help simplify the cost model around immediate and intrinsic costs, where we currently have multiple APIs. RFC thread: http://lists.llvm.org/pipermail/llvm-dev/2020-April/141263.html Differential Revision: https://reviews.llvm.org/D79002
1 parent 5578ec3 commit 40574fe

32 files changed

+1009
-573
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

+150-85
Large diffs are not rendered by default.

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

+48-26
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ class TargetTransformInfoImplBase {
4444
const DataLayout &getDataLayout() const { return DL; }
4545

4646
int getGEPCost(Type *PointeeType, const Value *Ptr,
47-
ArrayRef<const Value *> Operands) {
47+
ArrayRef<const Value *> Operands,
48+
TTI::TargetCostKind CostKind = TTI::TCK_SizeAndLatency) {
4849
// In the basic model, we just assume that all-constant GEPs will be folded
4950
// into their uses via addressing modes.
5051
for (unsigned Idx = 0, Size = Operands.size(); Idx != Size; ++Idx)
@@ -285,15 +286,19 @@ class TargetTransformInfoImplBase {
285286
return 0;
286287
}
287288

288-
unsigned getIntImmCost(const APInt &Imm, Type *Ty) { return TTI::TCC_Basic; }
289+
unsigned getIntImmCost(const APInt &Imm, Type *Ty,
290+
TTI::TargetCostKind CostKind) {
291+
return TTI::TCC_Basic;
292+
}
289293

290294
unsigned getIntImmCostInst(unsigned Opcode, unsigned Idx, const APInt &Imm,
291-
Type *Ty) {
295+
Type *Ty, TTI::TargetCostKind CostKind) {
292296
return TTI::TCC_Free;
293297
}
294298

295299
unsigned getIntImmCostIntrin(Intrinsic::ID IID, unsigned Idx,
296-
const APInt &Imm, Type *Ty) {
300+
const APInt &Imm, Type *Ty,
301+
TTI::TargetCostKind CostKind) {
297302
return TTI::TCC_Free;
298303
}
299304

@@ -366,6 +371,7 @@ class TargetTransformInfoImplBase {
366371
unsigned getMaxInterleaveFactor(unsigned VF) { return 1; }
367372

368373
unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty,
374+
TTI::TargetCostKind CostKind,
369375
TTI::OperandValueKind Opd1Info,
370376
TTI::OperandValueKind Opd2Info,
371377
TTI::OperandValueProperties Opd1PropInfo,
@@ -381,6 +387,7 @@ class TargetTransformInfoImplBase {
381387
}
382388

383389
unsigned getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src,
390+
TTI::TargetCostKind CostKind,
384391
const Instruction *I) {
385392
switch (Opcode) {
386393
default:
@@ -419,10 +426,12 @@ class TargetTransformInfoImplBase {
419426
return 1;
420427
}
421428

422-
unsigned getCFInstrCost(unsigned Opcode) { return 1; }
429+
unsigned getCFInstrCost(unsigned Opcode,
430+
TTI::TargetCostKind CostKind) { return 1; }
423431

424432
unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy,
425-
const Instruction *I) {
433+
TTI::TargetCostKind CostKind,
434+
const Instruction *I) const {
426435
return 1;
427436
}
428437

@@ -431,43 +440,50 @@ class TargetTransformInfoImplBase {
431440
}
432441

433442
unsigned getMemoryOpCost(unsigned Opcode, Type *Src, MaybeAlign Alignment,
434-
unsigned AddressSpace, const Instruction *I) {
443+
unsigned AddressSpace, TTI::TargetCostKind CostKind,
444+
const Instruction *I) const {
435445
return 1;
436446
}
437447

438448
unsigned getMaskedMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
439-
unsigned AddressSpace) {
449+
unsigned AddressSpace,
450+
TTI::TargetCostKind CostKind) {
440451
return 1;
441452
}
442453

443-
unsigned getGatherScatterOpCost(unsigned Opcode, Type *DataTy, Value *Ptr,
444-
bool VariableMask, unsigned Alignment,
445-
const Instruction *I = nullptr) {
454+
unsigned getGatherScatterOpCost(
455+
unsigned Opcode, Type *DataTy, Value *Ptr, bool VariableMask,
456+
unsigned Alignment, TTI::TargetCostKind CostKind,
457+
const Instruction *I = nullptr) {
446458
return 1;
447459
}
448460

449461
unsigned getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy,
450462
unsigned Factor,
451463
ArrayRef<unsigned> Indices,
452464
unsigned Alignment, unsigned AddressSpace,
453-
bool UseMaskForCond = false,
454-
bool UseMaskForGaps = false) {
465+
TTI::TargetCostKind CostKind,
466+
bool UseMaskForCond,
467+
bool UseMaskForGaps) {
455468
return 1;
456469
}
457470

458471
unsigned getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
459472
ArrayRef<Type *> Tys, FastMathFlags FMF,
460473
unsigned ScalarizationCostPassed,
474+
TTI::TargetCostKind CostKind,
461475
const Instruction *I) {
462476
return 1;
463477
}
464478
unsigned getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
465479
ArrayRef<Value *> Args, FastMathFlags FMF,
466-
unsigned VF, const Instruction *I) {
480+
unsigned VF, TTI::TargetCostKind CostKind,
481+
const Instruction *I) {
467482
return 1;
468483
}
469484

470-
unsigned getCallInstrCost(Function *F, Type *RetTy, ArrayRef<Type *> Tys) {
485+
unsigned getCallInstrCost(Function *F, Type *RetTy, ArrayRef<Type *> Tys,
486+
TTI::TargetCostKind CostKind) {
471487
return 1;
472488
}
473489

@@ -478,9 +494,11 @@ class TargetTransformInfoImplBase {
478494
return 0;
479495
}
480496

481-
unsigned getArithmeticReductionCost(unsigned, VectorType *, bool) { return 1; }
497+
unsigned getArithmeticReductionCost(unsigned, VectorType *, bool,
498+
TTI::TargetCostKind) { return 1; }
482499

483-
unsigned getMinMaxReductionCost(VectorType *, VectorType *, bool, bool) { return 1; }
500+
unsigned getMinMaxReductionCost(VectorType *, VectorType *, bool, bool,
501+
TTI::TargetCostKind) { return 1; }
484502

485503
unsigned getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) { return 0; }
486504

@@ -680,7 +698,8 @@ class TargetTransformInfoImplCRTPBase : public TargetTransformInfoImplBase {
680698
using BaseT::getGEPCost;
681699

682700
int getGEPCost(Type *PointeeType, const Value *Ptr,
683-
ArrayRef<const Value *> Operands) {
701+
ArrayRef<const Value *> Operands,
702+
TTI::TargetCostKind CostKind = TTI::TCK_SizeAndLatency) {
684703
assert(PointeeType && Ptr && "can't get GEPCost of nullptr");
685704
// TODO: will remove this when pointers have an opaque type.
686705
assert(Ptr->getType()->getScalarType()->getPointerElementType() ==
@@ -738,7 +757,8 @@ class TargetTransformInfoImplCRTPBase : public TargetTransformInfoImplBase {
738757
}
739758

740759
unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
741-
ArrayRef<Type *> ParamTys, const User *U) {
760+
ArrayRef<Type *> ParamTys, const User *U,
761+
TTI::TargetCostKind TCK_SizeAndLatency) {
742762
switch (IID) {
743763
default:
744764
// Intrinsics rarely (if ever) have normal argument setup constraints.
@@ -782,19 +802,21 @@ class TargetTransformInfoImplCRTPBase : public TargetTransformInfoImplBase {
782802
}
783803

784804
unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
785-
ArrayRef<const Value *> Arguments, const User *U) {
805+
ArrayRef<const Value *> Arguments, const User *U,
806+
TTI::TargetCostKind CostKind) {
786807
// Delegate to the generic intrinsic handling code. This mostly provides an
787808
// opportunity for targets to (for example) special case the cost of
788809
// certain intrinsics based on constants used as arguments.
789810
SmallVector<Type *, 8> ParamTys;
790811
ParamTys.reserve(Arguments.size());
791812
for (unsigned Idx = 0, Size = Arguments.size(); Idx != Size; ++Idx)
792813
ParamTys.push_back(Arguments[Idx]->getType());
793-
return static_cast<T *>(this)->getIntrinsicCost(IID, RetTy, ParamTys, U);
814+
return static_cast<T *>(this)->getIntrinsicCost(IID, RetTy, ParamTys, U,
815+
CostKind);
794816
}
795817

796818
unsigned getUserCost(const User *U, ArrayRef<const Value *> Operands,
797-
enum TTI::TargetCostKind CostKind) {
819+
TTI::TargetCostKind CostKind) {
798820
auto *TargetTTI = static_cast<T *>(this);
799821

800822
// FIXME: Unlikely to be true for anything but CodeSize.
@@ -805,7 +827,7 @@ class TargetTransformInfoImplCRTPBase : public TargetTransformInfoImplBase {
805827
if (Intrinsic::ID IID = F->getIntrinsicID()) {
806828
SmallVector<Type *, 8> ParamTys(FTy->param_begin(), FTy->param_end());
807829
return TargetTTI->getIntrinsicCost(IID, FTy->getReturnType(),
808-
ParamTys, U);
830+
ParamTys, U, CostKind);
809831
}
810832

811833
if (!TargetTTI->isLoweredToCall(F))
@@ -849,12 +871,12 @@ class TargetTransformInfoImplCRTPBase : public TargetTransformInfoImplBase {
849871
case Instruction::IntToPtr:
850872
case Instruction::PtrToInt:
851873
case Instruction::Trunc:
852-
if (getCastInstrCost(Opcode, Ty, OpTy, I) == TTI::TCC_Free ||
853-
TargetTTI->getCastInstrCost(Opcode, Ty, OpTy, I) == TTI::TCC_Free)
874+
if (getCastInstrCost(Opcode, Ty, OpTy, CostKind, I) == TTI::TCC_Free ||
875+
TargetTTI->getCastInstrCost(Opcode, Ty, OpTy, CostKind, I) == TTI::TCC_Free)
854876
return TTI::TCC_Free;
855877
break;
856878
case Instruction::BitCast:
857-
if (getCastInstrCost(Opcode, Ty, OpTy, I) == TTI::TCC_Free)
879+
if (getCastInstrCost(Opcode, Ty, OpTy, CostKind, I) == TTI::TCC_Free)
858880
return TTI::TCC_Free;
859881
break;
860882
case Instruction::FPExt:

0 commit comments

Comments
 (0)