1 //===- BasicTTIImpl.h -------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// This file provides a helper that implements much of the TTI interface in
11 /// terms of the target-independent code generator and TargetLowering
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CODEGEN_BASICTTIIMPL_H
17 #define LLVM_CODEGEN_BASICTTIIMPL_H
19 #include "llvm/ADT/APInt.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/BitVector.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/Analysis/LoopInfo.h"
25 #include "llvm/Analysis/TargetTransformInfo.h"
26 #include "llvm/Analysis/TargetTransformInfoImpl.h"
27 #include "llvm/CodeGen/ISDOpcodes.h"
28 #include "llvm/CodeGen/TargetLowering.h"
29 #include "llvm/CodeGen/TargetSubtargetInfo.h"
30 #include "llvm/CodeGen/ValueTypes.h"
31 #include "llvm/IR/BasicBlock.h"
32 #include "llvm/IR/CallSite.h"
33 #include "llvm/IR/Constant.h"
34 #include "llvm/IR/Constants.h"
35 #include "llvm/IR/DataLayout.h"
36 #include "llvm/IR/DerivedTypes.h"
37 #include "llvm/IR/InstrTypes.h"
38 #include "llvm/IR/Instruction.h"
39 #include "llvm/IR/Instructions.h"
40 #include "llvm/IR/Intrinsics.h"
41 #include "llvm/IR/Operator.h"
42 #include "llvm/IR/Type.h"
43 #include "llvm/IR/Value.h"
44 #include "llvm/MC/MCSchedule.h"
45 #include "llvm/Support/Casting.h"
46 #include "llvm/Support/CommandLine.h"
47 #include "llvm/Support/ErrorHandling.h"
48 #include "llvm/Support/MachineValueType.h"
49 #include "llvm/Support/MathExtras.h"
61 class ScalarEvolution
;
65 extern cl::opt
<unsigned> PartialUnrollingThreshold
;
67 /// Base class which can be used to help build a TTI implementation.
69 /// This class provides as much implementation of the TTI interface as is
70 /// possible using the target independent parts of the code generator.
72 /// In order to subclass it, your class must implement a getST() method to
73 /// return the subtarget, and a getTLI() method to return the target lowering.
74 /// We need these methods implemented in the derived class so that this class
75 /// doesn't have to duplicate storage for them.
77 class BasicTTIImplBase
: public TargetTransformInfoImplCRTPBase
<T
> {
79 using BaseT
= TargetTransformInfoImplCRTPBase
<T
>;
80 using TTI
= TargetTransformInfo
;
82 /// Estimate a cost of Broadcast as an extract and sequence of insert
84 unsigned getBroadcastShuffleOverhead(Type
*Ty
) {
85 assert(Ty
->isVectorTy() && "Can only shuffle vectors");
87 // Broadcast cost is equal to the cost of extracting the zero'th element
88 // plus the cost of inserting it into every element of the result vector.
89 Cost
+= static_cast<T
*>(this)->getVectorInstrCost(
90 Instruction::ExtractElement
, Ty
, 0);
92 for (int i
= 0, e
= Ty
->getVectorNumElements(); i
< e
; ++i
) {
93 Cost
+= static_cast<T
*>(this)->getVectorInstrCost(
94 Instruction::InsertElement
, Ty
, i
);
99 /// Estimate a cost of shuffle as a sequence of extract and insert
101 unsigned getPermuteShuffleOverhead(Type
*Ty
) {
102 assert(Ty
->isVectorTy() && "Can only shuffle vectors");
104 // Shuffle cost is equal to the cost of extracting element from its argument
105 // plus the cost of inserting them onto the result vector.
107 // e.g. <4 x float> has a mask of <0,5,2,7> i.e we need to extract from
108 // index 0 of first vector, index 1 of second vector,index 2 of first
109 // vector and finally index 3 of second vector and insert them at index
110 // <0,1,2,3> of result vector.
111 for (int i
= 0, e
= Ty
->getVectorNumElements(); i
< e
; ++i
) {
112 Cost
+= static_cast<T
*>(this)
113 ->getVectorInstrCost(Instruction::InsertElement
, Ty
, i
);
114 Cost
+= static_cast<T
*>(this)
115 ->getVectorInstrCost(Instruction::ExtractElement
, Ty
, i
);
120 /// Estimate a cost of subvector extraction as a sequence of extract and
121 /// insert operations.
122 unsigned getExtractSubvectorOverhead(Type
*Ty
, int Index
, Type
*SubTy
) {
123 assert(Ty
&& Ty
->isVectorTy() && SubTy
&& SubTy
->isVectorTy() &&
124 "Can only extract subvectors from vectors");
125 int NumSubElts
= SubTy
->getVectorNumElements();
126 assert((Index
+ NumSubElts
) <= (int)Ty
->getVectorNumElements() &&
127 "SK_ExtractSubvector index out of range");
130 // Subvector extraction cost is equal to the cost of extracting element from
131 // the source type plus the cost of inserting them into the result vector
133 for (int i
= 0; i
!= NumSubElts
; ++i
) {
134 Cost
+= static_cast<T
*>(this)->getVectorInstrCost(
135 Instruction::ExtractElement
, Ty
, i
+ Index
);
136 Cost
+= static_cast<T
*>(this)->getVectorInstrCost(
137 Instruction::InsertElement
, SubTy
, i
);
142 /// Estimate a cost of subvector insertion as a sequence of extract and
143 /// insert operations.
144 unsigned getInsertSubvectorOverhead(Type
*Ty
, int Index
, Type
*SubTy
) {
145 assert(Ty
&& Ty
->isVectorTy() && SubTy
&& SubTy
->isVectorTy() &&
146 "Can only insert subvectors into vectors");
147 int NumSubElts
= SubTy
->getVectorNumElements();
148 assert((Index
+ NumSubElts
) <= (int)Ty
->getVectorNumElements() &&
149 "SK_InsertSubvector index out of range");
152 // Subvector insertion cost is equal to the cost of extracting element from
153 // the source type plus the cost of inserting them into the result vector
155 for (int i
= 0; i
!= NumSubElts
; ++i
) {
156 Cost
+= static_cast<T
*>(this)->getVectorInstrCost(
157 Instruction::ExtractElement
, SubTy
, i
);
158 Cost
+= static_cast<T
*>(this)->getVectorInstrCost(
159 Instruction::InsertElement
, Ty
, i
+ Index
);
164 /// Local query method delegates up to T which *must* implement this!
165 const TargetSubtargetInfo
*getST() const {
166 return static_cast<const T
*>(this)->getST();
169 /// Local query method delegates up to T which *must* implement this!
170 const TargetLoweringBase
*getTLI() const {
171 return static_cast<const T
*>(this)->getTLI();
174 static ISD::MemIndexedMode
getISDIndexedMode(TTI::MemIndexedMode M
) {
176 case TTI::MIM_Unindexed
:
177 return ISD::UNINDEXED
;
178 case TTI::MIM_PreInc
:
180 case TTI::MIM_PreDec
:
182 case TTI::MIM_PostInc
:
183 return ISD::POST_INC
;
184 case TTI::MIM_PostDec
:
185 return ISD::POST_DEC
;
187 llvm_unreachable("Unexpected MemIndexedMode");
191 explicit BasicTTIImplBase(const TargetMachine
*TM
, const DataLayout
&DL
)
194 using TargetTransformInfoImplBase::DL
;
197 /// \name Scalar TTI Implementations
199 bool allowsMisalignedMemoryAccesses(LLVMContext
&Context
, unsigned BitWidth
,
200 unsigned AddressSpace
, unsigned Alignment
,
202 EVT E
= EVT::getIntegerVT(Context
, BitWidth
);
203 return getTLI()->allowsMisalignedMemoryAccesses(
204 E
, AddressSpace
, Alignment
, MachineMemOperand::MONone
, Fast
);
207 bool hasBranchDivergence() { return false; }
209 bool isSourceOfDivergence(const Value
*V
) { return false; }
211 bool isAlwaysUniform(const Value
*V
) { return false; }
213 unsigned getFlatAddressSpace() {
214 // Return an invalid address space.
218 bool collectFlatAddressOperands(SmallVectorImpl
<int> &OpIndexes
,
219 Intrinsic::ID IID
) const {
223 bool rewriteIntrinsicWithAddressSpace(IntrinsicInst
*II
,
224 Value
*OldV
, Value
*NewV
) const {
228 bool isLegalAddImmediate(int64_t imm
) {
229 return getTLI()->isLegalAddImmediate(imm
);
232 bool isLegalICmpImmediate(int64_t imm
) {
233 return getTLI()->isLegalICmpImmediate(imm
);
236 bool isLegalAddressingMode(Type
*Ty
, GlobalValue
*BaseGV
, int64_t BaseOffset
,
237 bool HasBaseReg
, int64_t Scale
,
238 unsigned AddrSpace
, Instruction
*I
= nullptr) {
239 TargetLoweringBase::AddrMode AM
;
241 AM
.BaseOffs
= BaseOffset
;
242 AM
.HasBaseReg
= HasBaseReg
;
244 return getTLI()->isLegalAddressingMode(DL
, AM
, Ty
, AddrSpace
, I
);
247 bool isIndexedLoadLegal(TTI::MemIndexedMode M
, Type
*Ty
,
248 const DataLayout
&DL
) const {
249 EVT VT
= getTLI()->getValueType(DL
, Ty
);
250 return getTLI()->isIndexedLoadLegal(getISDIndexedMode(M
), VT
);
253 bool isIndexedStoreLegal(TTI::MemIndexedMode M
, Type
*Ty
,
254 const DataLayout
&DL
) const {
255 EVT VT
= getTLI()->getValueType(DL
, Ty
);
256 return getTLI()->isIndexedStoreLegal(getISDIndexedMode(M
), VT
);
259 bool isLSRCostLess(TTI::LSRCost C1
, TTI::LSRCost C2
) {
260 return TargetTransformInfoImplBase::isLSRCostLess(C1
, C2
);
263 int getScalingFactorCost(Type
*Ty
, GlobalValue
*BaseGV
, int64_t BaseOffset
,
264 bool HasBaseReg
, int64_t Scale
, unsigned AddrSpace
) {
265 TargetLoweringBase::AddrMode AM
;
267 AM
.BaseOffs
= BaseOffset
;
268 AM
.HasBaseReg
= HasBaseReg
;
270 return getTLI()->getScalingFactorCost(DL
, AM
, Ty
, AddrSpace
);
273 bool isTruncateFree(Type
*Ty1
, Type
*Ty2
) {
274 return getTLI()->isTruncateFree(Ty1
, Ty2
);
277 bool isProfitableToHoist(Instruction
*I
) {
278 return getTLI()->isProfitableToHoist(I
);
281 bool useAA() const { return getST()->useAA(); }
283 bool isTypeLegal(Type
*Ty
) {
284 EVT VT
= getTLI()->getValueType(DL
, Ty
);
285 return getTLI()->isTypeLegal(VT
);
288 int getGEPCost(Type
*PointeeType
, const Value
*Ptr
,
289 ArrayRef
<const Value
*> Operands
) {
290 return BaseT::getGEPCost(PointeeType
, Ptr
, Operands
);
293 int getExtCost(const Instruction
*I
, const Value
*Src
) {
294 if (getTLI()->isExtFree(I
))
295 return TargetTransformInfo::TCC_Free
;
297 if (isa
<ZExtInst
>(I
) || isa
<SExtInst
>(I
))
298 if (const LoadInst
*LI
= dyn_cast
<LoadInst
>(Src
))
299 if (getTLI()->isExtLoad(LI
, I
, DL
))
300 return TargetTransformInfo::TCC_Free
;
302 return TargetTransformInfo::TCC_Basic
;
305 unsigned getIntrinsicCost(Intrinsic::ID IID
, Type
*RetTy
,
306 ArrayRef
<const Value
*> Arguments
, const User
*U
) {
307 return BaseT::getIntrinsicCost(IID
, RetTy
, Arguments
, U
);
310 unsigned getIntrinsicCost(Intrinsic::ID IID
, Type
*RetTy
,
311 ArrayRef
<Type
*> ParamTys
, const User
*U
) {
312 if (IID
== Intrinsic::cttz
) {
313 if (getTLI()->isCheapToSpeculateCttz())
314 return TargetTransformInfo::TCC_Basic
;
315 return TargetTransformInfo::TCC_Expensive
;
318 if (IID
== Intrinsic::ctlz
) {
319 if (getTLI()->isCheapToSpeculateCtlz())
320 return TargetTransformInfo::TCC_Basic
;
321 return TargetTransformInfo::TCC_Expensive
;
324 return BaseT::getIntrinsicCost(IID
, RetTy
, ParamTys
, U
);
327 unsigned getEstimatedNumberOfCaseClusters(const SwitchInst
&SI
,
328 unsigned &JumpTableSize
) {
329 /// Try to find the estimated number of clusters. Note that the number of
330 /// clusters identified in this function could be different from the actual
331 /// numbers found in lowering. This function ignore switches that are
332 /// lowered with a mix of jump table / bit test / BTree. This function was
333 /// initially intended to be used when estimating the cost of switch in
334 /// inline cost heuristic, but it's a generic cost model to be used in other
335 /// places (e.g., in loop unrolling).
336 unsigned N
= SI
.getNumCases();
337 const TargetLoweringBase
*TLI
= getTLI();
338 const DataLayout
&DL
= this->getDataLayout();
341 bool IsJTAllowed
= TLI
->areJTsAllowed(SI
.getParent()->getParent());
343 // Early exit if both a jump table and bit test are not allowed.
344 if (N
< 1 || (!IsJTAllowed
&& DL
.getIndexSizeInBits(0u) < N
))
347 APInt MaxCaseVal
= SI
.case_begin()->getCaseValue()->getValue();
348 APInt MinCaseVal
= MaxCaseVal
;
349 for (auto CI
: SI
.cases()) {
350 const APInt
&CaseVal
= CI
.getCaseValue()->getValue();
351 if (CaseVal
.sgt(MaxCaseVal
))
352 MaxCaseVal
= CaseVal
;
353 if (CaseVal
.slt(MinCaseVal
))
354 MinCaseVal
= CaseVal
;
357 // Check if suitable for a bit test
358 if (N
<= DL
.getIndexSizeInBits(0u)) {
359 SmallPtrSet
<const BasicBlock
*, 4> Dests
;
360 for (auto I
: SI
.cases())
361 Dests
.insert(I
.getCaseSuccessor());
363 if (TLI
->isSuitableForBitTests(Dests
.size(), N
, MinCaseVal
, MaxCaseVal
,
368 // Check if suitable for a jump table.
370 if (N
< 2 || N
< TLI
->getMinimumJumpTableEntries())
373 (MaxCaseVal
- MinCaseVal
)
374 .getLimitedValue(std::numeric_limits
<uint64_t>::max() - 1) + 1;
375 // Check whether a range of clusters is dense enough for a jump table
376 if (TLI
->isSuitableForJumpTable(&SI
, N
, Range
)) {
377 JumpTableSize
= Range
;
384 bool shouldBuildLookupTables() {
385 const TargetLoweringBase
*TLI
= getTLI();
386 return TLI
->isOperationLegalOrCustom(ISD::BR_JT
, MVT::Other
) ||
387 TLI
->isOperationLegalOrCustom(ISD::BRIND
, MVT::Other
);
390 bool haveFastSqrt(Type
*Ty
) {
391 const TargetLoweringBase
*TLI
= getTLI();
392 EVT VT
= TLI
->getValueType(DL
, Ty
);
393 return TLI
->isTypeLegal(VT
) &&
394 TLI
->isOperationLegalOrCustom(ISD::FSQRT
, VT
);
397 bool isFCmpOrdCheaperThanFCmpZero(Type
*Ty
) {
401 unsigned getFPOpCost(Type
*Ty
) {
402 // Check whether FADD is available, as a proxy for floating-point in
404 const TargetLoweringBase
*TLI
= getTLI();
405 EVT VT
= TLI
->getValueType(DL
, Ty
);
406 if (TLI
->isOperationLegalOrCustomOrPromote(ISD::FADD
, VT
))
407 return TargetTransformInfo::TCC_Basic
;
408 return TargetTransformInfo::TCC_Expensive
;
411 unsigned getOperationCost(unsigned Opcode
, Type
*Ty
, Type
*OpTy
) {
412 const TargetLoweringBase
*TLI
= getTLI();
415 case Instruction::Trunc
:
416 if (TLI
->isTruncateFree(OpTy
, Ty
))
417 return TargetTransformInfo::TCC_Free
;
418 return TargetTransformInfo::TCC_Basic
;
419 case Instruction::ZExt
:
420 if (TLI
->isZExtFree(OpTy
, Ty
))
421 return TargetTransformInfo::TCC_Free
;
422 return TargetTransformInfo::TCC_Basic
;
424 case Instruction::AddrSpaceCast
:
425 if (TLI
->isFreeAddrSpaceCast(OpTy
->getPointerAddressSpace(),
426 Ty
->getPointerAddressSpace()))
427 return TargetTransformInfo::TCC_Free
;
428 return TargetTransformInfo::TCC_Basic
;
431 return BaseT::getOperationCost(Opcode
, Ty
, OpTy
);
434 unsigned getInliningThresholdMultiplier() { return 1; }
436 int getInlinerVectorBonusPercent() { return 150; }
438 void getUnrollingPreferences(Loop
*L
, ScalarEvolution
&SE
,
439 TTI::UnrollingPreferences
&UP
) {
440 // This unrolling functionality is target independent, but to provide some
441 // motivation for its intended use, for x86:
443 // According to the Intel 64 and IA-32 Architectures Optimization Reference
444 // Manual, Intel Core models and later have a loop stream detector (and
445 // associated uop queue) that can benefit from partial unrolling.
446 // The relevant requirements are:
447 // - The loop must have no more than 4 (8 for Nehalem and later) branches
448 // taken, and none of them may be calls.
449 // - The loop can have no more than 18 (28 for Nehalem and later) uops.
451 // According to the Software Optimization Guide for AMD Family 15h
452 // Processors, models 30h-4fh (Steamroller and later) have a loop predictor
453 // and loop buffer which can benefit from partial unrolling.
454 // The relevant requirements are:
455 // - The loop must have fewer than 16 branches
456 // - The loop must have less than 40 uops in all executed loop branches
458 // The number of taken branches in a loop is hard to estimate here, and
459 // benchmarking has revealed that it is better not to be conservative when
460 // estimating the branch count. As a result, we'll ignore the branch limits
461 // until someone finds a case where it matters in practice.
464 const TargetSubtargetInfo
*ST
= getST();
465 if (PartialUnrollingThreshold
.getNumOccurrences() > 0)
466 MaxOps
= PartialUnrollingThreshold
;
467 else if (ST
->getSchedModel().LoopMicroOpBufferSize
> 0)
468 MaxOps
= ST
->getSchedModel().LoopMicroOpBufferSize
;
472 // Scan the loop: don't unroll loops with calls.
473 for (Loop::block_iterator I
= L
->block_begin(), E
= L
->block_end(); I
!= E
;
477 for (BasicBlock::iterator J
= BB
->begin(), JE
= BB
->end(); J
!= JE
; ++J
)
478 if (isa
<CallInst
>(J
) || isa
<InvokeInst
>(J
)) {
479 ImmutableCallSite
CS(&*J
);
480 if (const Function
*F
= CS
.getCalledFunction()) {
481 if (!static_cast<T
*>(this)->isLoweredToCall(F
))
489 // Enable runtime and partial unrolling up to the specified size.
490 // Enable using trip count upper bound to unroll loops.
491 UP
.Partial
= UP
.Runtime
= UP
.UpperBound
= true;
492 UP
.PartialThreshold
= MaxOps
;
494 // Avoid unrolling when optimizing for size.
495 UP
.OptSizeThreshold
= 0;
496 UP
.PartialOptSizeThreshold
= 0;
498 // Set number of instructions optimized when "back edge"
499 // becomes "fall through" to default value of 2.
503 bool isHardwareLoopProfitable(Loop
*L
, ScalarEvolution
&SE
,
505 TargetLibraryInfo
*LibInfo
,
506 HardwareLoopInfo
&HWLoopInfo
) {
507 return BaseT::isHardwareLoopProfitable(L
, SE
, AC
, LibInfo
, HWLoopInfo
);
510 int getInstructionLatency(const Instruction
*I
) {
511 if (isa
<LoadInst
>(I
))
512 return getST()->getSchedModel().DefaultLoadLatency
;
514 return BaseT::getInstructionLatency(I
);
519 /// \name Vector TTI Implementations
522 unsigned getNumberOfRegisters(bool Vector
) { return Vector
? 0 : 1; }
524 unsigned getRegisterBitWidth(bool Vector
) const { return 32; }
526 /// Estimate the overhead of scalarizing an instruction. Insert and Extract
527 /// are set if the result needs to be inserted and/or extracted from vectors.
528 unsigned getScalarizationOverhead(Type
*Ty
, bool Insert
, bool Extract
) {
529 assert(Ty
->isVectorTy() && "Can only scalarize vectors");
532 for (int i
= 0, e
= Ty
->getVectorNumElements(); i
< e
; ++i
) {
534 Cost
+= static_cast<T
*>(this)
535 ->getVectorInstrCost(Instruction::InsertElement
, Ty
, i
);
537 Cost
+= static_cast<T
*>(this)
538 ->getVectorInstrCost(Instruction::ExtractElement
, Ty
, i
);
544 /// Estimate the overhead of scalarizing an instructions unique
545 /// non-constant operands. The types of the arguments are ordinarily
546 /// scalar, in which case the costs are multiplied with VF.
547 unsigned getOperandsScalarizationOverhead(ArrayRef
<const Value
*> Args
,
550 SmallPtrSet
<const Value
*, 4> UniqueOperands
;
551 for (const Value
*A
: Args
) {
552 if (!isa
<Constant
>(A
) && UniqueOperands
.insert(A
).second
) {
553 Type
*VecTy
= nullptr;
554 if (A
->getType()->isVectorTy()) {
555 VecTy
= A
->getType();
556 // If A is a vector operand, VF should be 1 or correspond to A.
557 assert((VF
== 1 || VF
== VecTy
->getVectorNumElements()) &&
558 "Vector argument does not match VF");
561 VecTy
= VectorType::get(A
->getType(), VF
);
563 Cost
+= getScalarizationOverhead(VecTy
, false, true);
570 unsigned getScalarizationOverhead(Type
*VecTy
, ArrayRef
<const Value
*> Args
) {
571 assert(VecTy
->isVectorTy());
575 Cost
+= getScalarizationOverhead(VecTy
, true, false);
577 Cost
+= getOperandsScalarizationOverhead(Args
,
578 VecTy
->getVectorNumElements());
580 // When no information on arguments is provided, we add the cost
581 // associated with one argument as a heuristic.
582 Cost
+= getScalarizationOverhead(VecTy
, false, true);
587 unsigned getMaxInterleaveFactor(unsigned VF
) { return 1; }
589 unsigned getArithmeticInstrCost(
590 unsigned Opcode
, Type
*Ty
,
591 TTI::OperandValueKind Opd1Info
= TTI::OK_AnyValue
,
592 TTI::OperandValueKind Opd2Info
= TTI::OK_AnyValue
,
593 TTI::OperandValueProperties Opd1PropInfo
= TTI::OP_None
,
594 TTI::OperandValueProperties Opd2PropInfo
= TTI::OP_None
,
595 ArrayRef
<const Value
*> Args
= ArrayRef
<const Value
*>()) {
596 // Check if any of the operands are vector operands.
597 const TargetLoweringBase
*TLI
= getTLI();
598 int ISD
= TLI
->InstructionOpcodeToISD(Opcode
);
599 assert(ISD
&& "Invalid opcode");
601 std::pair
<unsigned, MVT
> LT
= TLI
->getTypeLegalizationCost(DL
, Ty
);
603 bool IsFloat
= Ty
->isFPOrFPVectorTy();
604 // Assume that floating point arithmetic operations cost twice as much as
605 // integer operations.
606 unsigned OpCost
= (IsFloat
? 2 : 1);
608 if (TLI
->isOperationLegalOrPromote(ISD
, LT
.second
)) {
609 // The operation is legal. Assume it costs 1.
610 // TODO: Once we have extract/insert subvector cost we need to use them.
611 return LT
.first
* OpCost
;
614 if (!TLI
->isOperationExpand(ISD
, LT
.second
)) {
615 // If the operation is custom lowered, then assume that the code is twice
617 return LT
.first
* 2 * OpCost
;
620 // Else, assume that we need to scalarize this op.
621 // TODO: If one of the types get legalized by splitting, handle this
622 // similarly to what getCastInstrCost() does.
623 if (Ty
->isVectorTy()) {
624 unsigned Num
= Ty
->getVectorNumElements();
625 unsigned Cost
= static_cast<T
*>(this)
626 ->getArithmeticInstrCost(Opcode
, Ty
->getScalarType());
627 // Return the cost of multiple scalar invocation plus the cost of
628 // inserting and extracting the values.
629 return getScalarizationOverhead(Ty
, Args
) + Num
* Cost
;
632 // We don't know anything about this scalar instruction.
636 unsigned getShuffleCost(TTI::ShuffleKind Kind
, Type
*Tp
, int Index
,
639 case TTI::SK_Broadcast
:
640 return getBroadcastShuffleOverhead(Tp
);
642 case TTI::SK_Reverse
:
643 case TTI::SK_Transpose
:
644 case TTI::SK_PermuteSingleSrc
:
645 case TTI::SK_PermuteTwoSrc
:
646 return getPermuteShuffleOverhead(Tp
);
647 case TTI::SK_ExtractSubvector
:
648 return getExtractSubvectorOverhead(Tp
, Index
, SubTp
);
649 case TTI::SK_InsertSubvector
:
650 return getInsertSubvectorOverhead(Tp
, Index
, SubTp
);
652 llvm_unreachable("Unknown TTI::ShuffleKind");
655 unsigned getCastInstrCost(unsigned Opcode
, Type
*Dst
, Type
*Src
,
656 const Instruction
*I
= nullptr) {
657 const TargetLoweringBase
*TLI
= getTLI();
658 int ISD
= TLI
->InstructionOpcodeToISD(Opcode
);
659 assert(ISD
&& "Invalid opcode");
660 std::pair
<unsigned, MVT
> SrcLT
= TLI
->getTypeLegalizationCost(DL
, Src
);
661 std::pair
<unsigned, MVT
> DstLT
= TLI
->getTypeLegalizationCost(DL
, Dst
);
663 // Check for NOOP conversions.
664 if (SrcLT
.first
== DstLT
.first
&&
665 SrcLT
.second
.getSizeInBits() == DstLT
.second
.getSizeInBits()) {
667 // Bitcast between types that are legalized to the same type are free.
668 if (Opcode
== Instruction::BitCast
|| Opcode
== Instruction::Trunc
)
672 if (Opcode
== Instruction::Trunc
&&
673 TLI
->isTruncateFree(SrcLT
.second
, DstLT
.second
))
676 if (Opcode
== Instruction::ZExt
&&
677 TLI
->isZExtFree(SrcLT
.second
, DstLT
.second
))
680 if (Opcode
== Instruction::AddrSpaceCast
&&
681 TLI
->isFreeAddrSpaceCast(Src
->getPointerAddressSpace(),
682 Dst
->getPointerAddressSpace()))
685 // If this is a zext/sext of a load, return 0 if the corresponding
686 // extending load exists on target.
687 if ((Opcode
== Instruction::ZExt
|| Opcode
== Instruction::SExt
) &&
688 I
&& isa
<LoadInst
>(I
->getOperand(0))) {
689 EVT ExtVT
= EVT::getEVT(Dst
);
690 EVT LoadVT
= EVT::getEVT(Src
);
692 ((Opcode
== Instruction::ZExt
) ? ISD::ZEXTLOAD
: ISD::SEXTLOAD
);
693 if (TLI
->isLoadExtLegal(LType
, ExtVT
, LoadVT
))
697 // If the cast is marked as legal (or promote) then assume low cost.
698 if (SrcLT
.first
== DstLT
.first
&&
699 TLI
->isOperationLegalOrPromote(ISD
, DstLT
.second
))
702 // Handle scalar conversions.
703 if (!Src
->isVectorTy() && !Dst
->isVectorTy()) {
704 // Scalar bitcasts are usually free.
705 if (Opcode
== Instruction::BitCast
)
708 // Just check the op cost. If the operation is legal then assume it costs
710 if (!TLI
->isOperationExpand(ISD
, DstLT
.second
))
713 // Assume that illegal scalar instruction are expensive.
717 // Check vector-to-vector casts.
718 if (Dst
->isVectorTy() && Src
->isVectorTy()) {
719 // If the cast is between same-sized registers, then the check is simple.
720 if (SrcLT
.first
== DstLT
.first
&&
721 SrcLT
.second
.getSizeInBits() == DstLT
.second
.getSizeInBits()) {
723 // Assume that Zext is done using AND.
724 if (Opcode
== Instruction::ZExt
)
727 // Assume that sext is done using SHL and SRA.
728 if (Opcode
== Instruction::SExt
)
731 // Just check the op cost. If the operation is legal then assume it
733 // 1 and multiply by the type-legalization overhead.
734 if (!TLI
->isOperationExpand(ISD
, DstLT
.second
))
735 return SrcLT
.first
* 1;
738 // If we are legalizing by splitting, query the concrete TTI for the cost
739 // of casting the original vector twice. We also need to factor in the
740 // cost of the split itself. Count that as 1, to be consistent with
741 // TLI->getTypeLegalizationCost().
742 if ((TLI
->getTypeAction(Src
->getContext(), TLI
->getValueType(DL
, Src
)) ==
743 TargetLowering::TypeSplitVector
) ||
744 (TLI
->getTypeAction(Dst
->getContext(), TLI
->getValueType(DL
, Dst
)) ==
745 TargetLowering::TypeSplitVector
)) {
746 Type
*SplitDst
= VectorType::get(Dst
->getVectorElementType(),
747 Dst
->getVectorNumElements() / 2);
748 Type
*SplitSrc
= VectorType::get(Src
->getVectorElementType(),
749 Src
->getVectorNumElements() / 2);
750 T
*TTI
= static_cast<T
*>(this);
751 return TTI
->getVectorSplitCost() +
752 (2 * TTI
->getCastInstrCost(Opcode
, SplitDst
, SplitSrc
, I
));
755 // In other cases where the source or destination are illegal, assume
756 // the operation will get scalarized.
757 unsigned Num
= Dst
->getVectorNumElements();
758 unsigned Cost
= static_cast<T
*>(this)->getCastInstrCost(
759 Opcode
, Dst
->getScalarType(), Src
->getScalarType(), I
);
761 // Return the cost of multiple scalar invocation plus the cost of
762 // inserting and extracting the values.
763 return getScalarizationOverhead(Dst
, true, true) + Num
* Cost
;
766 // We already handled vector-to-vector and scalar-to-scalar conversions.
768 // is where we handle bitcast between vectors and scalars. We need to assume
769 // that the conversion is scalarized in one way or another.
770 if (Opcode
== Instruction::BitCast
)
771 // Illegal bitcasts are done by storing and loading from a stack slot.
772 return (Src
->isVectorTy() ? getScalarizationOverhead(Src
, false, true)
774 (Dst
->isVectorTy() ? getScalarizationOverhead(Dst
, true, false)
777 llvm_unreachable("Unhandled cast");
780 unsigned getExtractWithExtendCost(unsigned Opcode
, Type
*Dst
,
781 VectorType
*VecTy
, unsigned Index
) {
782 return static_cast<T
*>(this)->getVectorInstrCost(
783 Instruction::ExtractElement
, VecTy
, Index
) +
784 static_cast<T
*>(this)->getCastInstrCost(Opcode
, Dst
,
785 VecTy
->getElementType());
788 unsigned getCFInstrCost(unsigned Opcode
) {
789 // Branches are assumed to be predicted.
793 unsigned getCmpSelInstrCost(unsigned Opcode
, Type
*ValTy
, Type
*CondTy
,
794 const Instruction
*I
) {
795 const TargetLoweringBase
*TLI
= getTLI();
796 int ISD
= TLI
->InstructionOpcodeToISD(Opcode
);
797 assert(ISD
&& "Invalid opcode");
799 // Selects on vectors are actually vector selects.
800 if (ISD
== ISD::SELECT
) {
801 assert(CondTy
&& "CondTy must exist");
802 if (CondTy
->isVectorTy())
805 std::pair
<unsigned, MVT
> LT
= TLI
->getTypeLegalizationCost(DL
, ValTy
);
807 if (!(ValTy
->isVectorTy() && !LT
.second
.isVector()) &&
808 !TLI
->isOperationExpand(ISD
, LT
.second
)) {
809 // The operation is legal. Assume it costs 1. Multiply
810 // by the type-legalization overhead.
814 // Otherwise, assume that the cast is scalarized.
815 // TODO: If one of the types get legalized by splitting, handle this
816 // similarly to what getCastInstrCost() does.
817 if (ValTy
->isVectorTy()) {
818 unsigned Num
= ValTy
->getVectorNumElements();
820 CondTy
= CondTy
->getScalarType();
821 unsigned Cost
= static_cast<T
*>(this)->getCmpSelInstrCost(
822 Opcode
, ValTy
->getScalarType(), CondTy
, I
);
824 // Return the cost of multiple scalar invocation plus the cost of
825 // inserting and extracting the values.
826 return getScalarizationOverhead(ValTy
, true, false) + Num
* Cost
;
829 // Unknown scalar opcode.
833 unsigned getVectorInstrCost(unsigned Opcode
, Type
*Val
, unsigned Index
) {
834 std::pair
<unsigned, MVT
> LT
=
835 getTLI()->getTypeLegalizationCost(DL
, Val
->getScalarType());
840 unsigned getMemoryOpCost(unsigned Opcode
, Type
*Src
, unsigned Alignment
,
841 unsigned AddressSpace
, const Instruction
*I
= nullptr) {
842 assert(!Src
->isVoidTy() && "Invalid type");
843 std::pair
<unsigned, MVT
> LT
= getTLI()->getTypeLegalizationCost(DL
, Src
);
845 // Assuming that all loads of legal types cost 1.
846 unsigned Cost
= LT
.first
;
848 if (Src
->isVectorTy() &&
849 Src
->getPrimitiveSizeInBits() < LT
.second
.getSizeInBits()) {
850 // This is a vector load that legalizes to a larger type than the vector
851 // itself. Unless the corresponding extending load or truncating store is
852 // legal, then this will scalarize.
853 TargetLowering::LegalizeAction LA
= TargetLowering::Expand
;
854 EVT MemVT
= getTLI()->getValueType(DL
, Src
);
855 if (Opcode
== Instruction::Store
)
856 LA
= getTLI()->getTruncStoreAction(LT
.second
, MemVT
);
858 LA
= getTLI()->getLoadExtAction(ISD::EXTLOAD
, LT
.second
, MemVT
);
860 if (LA
!= TargetLowering::Legal
&& LA
!= TargetLowering::Custom
) {
861 // This is a vector load/store for some illegal type that is scalarized.
862 // We must account for the cost of building or decomposing the vector.
863 Cost
+= getScalarizationOverhead(Src
, Opcode
!= Instruction::Store
,
864 Opcode
== Instruction::Store
);
871 unsigned getInterleavedMemoryOpCost(unsigned Opcode
, Type
*VecTy
,
873 ArrayRef
<unsigned> Indices
,
874 unsigned Alignment
, unsigned AddressSpace
,
875 bool UseMaskForCond
= false,
876 bool UseMaskForGaps
= false) {
877 VectorType
*VT
= dyn_cast
<VectorType
>(VecTy
);
878 assert(VT
&& "Expect a vector type for interleaved memory op");
880 unsigned NumElts
= VT
->getNumElements();
881 assert(Factor
> 1 && NumElts
% Factor
== 0 && "Invalid interleave factor");
883 unsigned NumSubElts
= NumElts
/ Factor
;
884 VectorType
*SubVT
= VectorType::get(VT
->getElementType(), NumSubElts
);
886 // Firstly, the cost of load/store operation.
888 if (UseMaskForCond
|| UseMaskForGaps
)
889 Cost
= static_cast<T
*>(this)->getMaskedMemoryOpCost(
890 Opcode
, VecTy
, Alignment
, AddressSpace
);
892 Cost
= static_cast<T
*>(this)->getMemoryOpCost(Opcode
, VecTy
, Alignment
,
895 // Legalize the vector type, and get the legalized and unlegalized type
897 MVT VecTyLT
= getTLI()->getTypeLegalizationCost(DL
, VecTy
).second
;
899 static_cast<T
*>(this)->getDataLayout().getTypeStoreSize(VecTy
);
900 unsigned VecTyLTSize
= VecTyLT
.getStoreSize();
902 // Return the ceiling of dividing A by B.
903 auto ceil
= [](unsigned A
, unsigned B
) { return (A
+ B
- 1) / B
; };
905 // Scale the cost of the memory operation by the fraction of legalized
906 // instructions that will actually be used. We shouldn't account for the
907 // cost of dead instructions since they will be removed.
909 // E.g., An interleaved load of factor 8:
910 // %vec = load <16 x i64>, <16 x i64>* %ptr
911 // %v0 = shufflevector %vec, undef, <0, 8>
913 // If <16 x i64> is legalized to 8 v2i64 loads, only 2 of the loads will be
914 // used (those corresponding to elements [0:1] and [8:9] of the unlegalized
915 // type). The other loads are unused.
917 // We only scale the cost of loads since interleaved store groups aren't
918 // allowed to have gaps.
919 if (Opcode
== Instruction::Load
&& VecTySize
> VecTyLTSize
) {
920 // The number of loads of a legal type it will take to represent a load
921 // of the unlegalized vector type.
922 unsigned NumLegalInsts
= ceil(VecTySize
, VecTyLTSize
);
924 // The number of elements of the unlegalized type that correspond to a
925 // single legal instruction.
926 unsigned NumEltsPerLegalInst
= ceil(NumElts
, NumLegalInsts
);
928 // Determine which legal instructions will be used.
929 BitVector
UsedInsts(NumLegalInsts
, false);
930 for (unsigned Index
: Indices
)
931 for (unsigned Elt
= 0; Elt
< NumSubElts
; ++Elt
)
932 UsedInsts
.set((Index
+ Elt
* Factor
) / NumEltsPerLegalInst
);
934 // Scale the cost of the load by the fraction of legal instructions that
936 Cost
*= UsedInsts
.count() / NumLegalInsts
;
939 // Then plus the cost of interleave operation.
940 if (Opcode
== Instruction::Load
) {
941 // The interleave cost is similar to extract sub vectors' elements
942 // from the wide vector, and insert them into sub vectors.
944 // E.g. An interleaved load of factor 2 (with one member of index 0):
945 // %vec = load <8 x i32>, <8 x i32>* %ptr
946 // %v0 = shuffle %vec, undef, <0, 2, 4, 6> ; Index 0
947 // The cost is estimated as extract elements at 0, 2, 4, 6 from the
948 // <8 x i32> vector and insert them into a <4 x i32> vector.
950 assert(Indices
.size() <= Factor
&&
951 "Interleaved memory op has too many members");
953 for (unsigned Index
: Indices
) {
954 assert(Index
< Factor
&& "Invalid index for interleaved memory op");
956 // Extract elements from loaded vector for each sub vector.
957 for (unsigned i
= 0; i
< NumSubElts
; i
++)
958 Cost
+= static_cast<T
*>(this)->getVectorInstrCost(
959 Instruction::ExtractElement
, VT
, Index
+ i
* Factor
);
962 unsigned InsSubCost
= 0;
963 for (unsigned i
= 0; i
< NumSubElts
; i
++)
964 InsSubCost
+= static_cast<T
*>(this)->getVectorInstrCost(
965 Instruction::InsertElement
, SubVT
, i
);
967 Cost
+= Indices
.size() * InsSubCost
;
969 // The interleave cost is extract all elements from sub vectors, and
970 // insert them into the wide vector.
972 // E.g. An interleaved store of factor 2:
973 // %v0_v1 = shuffle %v0, %v1, <0, 4, 1, 5, 2, 6, 3, 7>
974 // store <8 x i32> %interleaved.vec, <8 x i32>* %ptr
975 // The cost is estimated as extract all elements from both <4 x i32>
976 // vectors and insert into the <8 x i32> vector.
978 unsigned ExtSubCost
= 0;
979 for (unsigned i
= 0; i
< NumSubElts
; i
++)
980 ExtSubCost
+= static_cast<T
*>(this)->getVectorInstrCost(
981 Instruction::ExtractElement
, SubVT
, i
);
982 Cost
+= ExtSubCost
* Factor
;
984 for (unsigned i
= 0; i
< NumElts
; i
++)
985 Cost
+= static_cast<T
*>(this)
986 ->getVectorInstrCost(Instruction::InsertElement
, VT
, i
);
992 Type
*I8Type
= Type::getInt8Ty(VT
->getContext());
993 VectorType
*MaskVT
= VectorType::get(I8Type
, NumElts
);
994 SubVT
= VectorType::get(I8Type
, NumSubElts
);
996 // The Mask shuffling cost is extract all the elements of the Mask
997 // and insert each of them Factor times into the wide vector:
999 // E.g. an interleaved group with factor 3:
1000 // %mask = icmp ult <8 x i32> %vec1, %vec2
1001 // %interleaved.mask = shufflevector <8 x i1> %mask, <8 x i1> undef,
1002 // <24 x i32> <0,0,0,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7>
1003 // The cost is estimated as extract all mask elements from the <8xi1> mask
1004 // vector and insert them factor times into the <24xi1> shuffled mask
1006 for (unsigned i
= 0; i
< NumSubElts
; i
++)
1007 Cost
+= static_cast<T
*>(this)->getVectorInstrCost(
1008 Instruction::ExtractElement
, SubVT
, i
);
1010 for (unsigned i
= 0; i
< NumElts
; i
++)
1011 Cost
+= static_cast<T
*>(this)->getVectorInstrCost(
1012 Instruction::InsertElement
, MaskVT
, i
);
1014 // The Gaps mask is invariant and created outside the loop, therefore the
1015 // cost of creating it is not accounted for here. However if we have both
1016 // a MaskForGaps and some other mask that guards the execution of the
1017 // memory access, we need to account for the cost of And-ing the two masks
1020 Cost
+= static_cast<T
*>(this)->getArithmeticInstrCost(
1021 BinaryOperator::And
, MaskVT
);
1026 /// Get intrinsic cost based on arguments.
1027 unsigned getIntrinsicInstrCost(Intrinsic::ID IID
, Type
*RetTy
,
1028 ArrayRef
<Value
*> Args
, FastMathFlags FMF
,
1030 unsigned RetVF
= (RetTy
->isVectorTy() ? RetTy
->getVectorNumElements() : 1);
1031 assert((RetVF
== 1 || VF
== 1) && "VF > 1 and RetVF is a vector type");
1032 auto *ConcreteTTI
= static_cast<T
*>(this);
1036 // Assume that we need to scalarize this intrinsic.
1037 SmallVector
<Type
*, 4> Types
;
1038 for (Value
*Op
: Args
) {
1039 Type
*OpTy
= Op
->getType();
1040 assert(VF
== 1 || !OpTy
->isVectorTy());
1041 Types
.push_back(VF
== 1 ? OpTy
: VectorType::get(OpTy
, VF
));
1044 if (VF
> 1 && !RetTy
->isVoidTy())
1045 RetTy
= VectorType::get(RetTy
, VF
);
1047 // Compute the scalarization overhead based on Args for a vector
1048 // intrinsic. A vectorizer will pass a scalar RetTy and VF > 1, while
1049 // CostModel will pass a vector RetTy and VF is 1.
1050 unsigned ScalarizationCost
= std::numeric_limits
<unsigned>::max();
1051 if (RetVF
> 1 || VF
> 1) {
1052 ScalarizationCost
= 0;
1053 if (!RetTy
->isVoidTy())
1054 ScalarizationCost
+= getScalarizationOverhead(RetTy
, true, false);
1055 ScalarizationCost
+= getOperandsScalarizationOverhead(Args
, VF
);
1058 return ConcreteTTI
->getIntrinsicInstrCost(IID
, RetTy
, Types
, FMF
,
1061 case Intrinsic::masked_scatter
: {
1062 assert(VF
== 1 && "Can't vectorize types here.");
1063 Value
*Mask
= Args
[3];
1064 bool VarMask
= !isa
<Constant
>(Mask
);
1065 unsigned Alignment
= cast
<ConstantInt
>(Args
[2])->getZExtValue();
1066 return ConcreteTTI
->getGatherScatterOpCost(
1067 Instruction::Store
, Args
[0]->getType(), Args
[1], VarMask
, Alignment
);
1069 case Intrinsic::masked_gather
: {
1070 assert(VF
== 1 && "Can't vectorize types here.");
1071 Value
*Mask
= Args
[2];
1072 bool VarMask
= !isa
<Constant
>(Mask
);
1073 unsigned Alignment
= cast
<ConstantInt
>(Args
[1])->getZExtValue();
1074 return ConcreteTTI
->getGatherScatterOpCost(Instruction::Load
, RetTy
,
1075 Args
[0], VarMask
, Alignment
);
1077 case Intrinsic::experimental_vector_reduce_add
:
1078 case Intrinsic::experimental_vector_reduce_mul
:
1079 case Intrinsic::experimental_vector_reduce_and
:
1080 case Intrinsic::experimental_vector_reduce_or
:
1081 case Intrinsic::experimental_vector_reduce_xor
:
1082 case Intrinsic::experimental_vector_reduce_v2_fadd
:
1083 case Intrinsic::experimental_vector_reduce_v2_fmul
:
1084 case Intrinsic::experimental_vector_reduce_smax
:
1085 case Intrinsic::experimental_vector_reduce_smin
:
1086 case Intrinsic::experimental_vector_reduce_fmax
:
1087 case Intrinsic::experimental_vector_reduce_fmin
:
1088 case Intrinsic::experimental_vector_reduce_umax
:
1089 case Intrinsic::experimental_vector_reduce_umin
:
1090 return getIntrinsicInstrCost(IID
, RetTy
, Args
[0]->getType(), FMF
);
1091 case Intrinsic::fshl
:
1092 case Intrinsic::fshr
: {
1096 TTI::OperandValueProperties OpPropsX
, OpPropsY
, OpPropsZ
, OpPropsBW
;
1097 TTI::OperandValueKind OpKindX
= TTI::getOperandInfo(X
, OpPropsX
);
1098 TTI::OperandValueKind OpKindY
= TTI::getOperandInfo(Y
, OpPropsY
);
1099 TTI::OperandValueKind OpKindZ
= TTI::getOperandInfo(Z
, OpPropsZ
);
1100 TTI::OperandValueKind OpKindBW
= TTI::OK_UniformConstantValue
;
1101 OpPropsBW
= isPowerOf2_32(RetTy
->getScalarSizeInBits()) ? TTI::OP_PowerOf2
1103 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
1104 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
1106 Cost
+= ConcreteTTI
->getArithmeticInstrCost(BinaryOperator::Or
, RetTy
);
1107 Cost
+= ConcreteTTI
->getArithmeticInstrCost(BinaryOperator::Sub
, RetTy
);
1108 Cost
+= ConcreteTTI
->getArithmeticInstrCost(BinaryOperator::Shl
, RetTy
,
1109 OpKindX
, OpKindZ
, OpPropsX
);
1110 Cost
+= ConcreteTTI
->getArithmeticInstrCost(BinaryOperator::LShr
, RetTy
,
1111 OpKindY
, OpKindZ
, OpPropsY
);
1112 // Non-constant shift amounts requires a modulo.
1113 if (OpKindZ
!= TTI::OK_UniformConstantValue
&&
1114 OpKindZ
!= TTI::OK_NonUniformConstantValue
)
1115 Cost
+= ConcreteTTI
->getArithmeticInstrCost(BinaryOperator::URem
, RetTy
,
1116 OpKindZ
, OpKindBW
, OpPropsZ
,
1118 // For non-rotates (X != Y) we must add shift-by-zero handling costs.
1120 Type
*CondTy
= Type::getInt1Ty(RetTy
->getContext());
1122 CondTy
= VectorType::get(CondTy
, RetVF
);
1123 Cost
+= ConcreteTTI
->getCmpSelInstrCost(BinaryOperator::ICmp
, RetTy
,
1125 Cost
+= ConcreteTTI
->getCmpSelInstrCost(BinaryOperator::Select
, RetTy
,
1133 /// Get intrinsic cost based on argument types.
1134 /// If ScalarizationCostPassed is std::numeric_limits<unsigned>::max(), the
1135 /// cost of scalarizing the arguments and the return value will be computed
1137 unsigned getIntrinsicInstrCost(
1138 Intrinsic::ID IID
, Type
*RetTy
, ArrayRef
<Type
*> Tys
, FastMathFlags FMF
,
1139 unsigned ScalarizationCostPassed
= std::numeric_limits
<unsigned>::max()) {
1140 unsigned RetVF
= (RetTy
->isVectorTy() ? RetTy
->getVectorNumElements() : 1);
1141 auto *ConcreteTTI
= static_cast<T
*>(this);
1143 SmallVector
<unsigned, 2> ISDs
;
1144 unsigned SingleCallCost
= 10; // Library call cost. Make it expensive.
1147 // Assume that we need to scalarize this intrinsic.
1148 unsigned ScalarizationCost
= ScalarizationCostPassed
;
1149 unsigned ScalarCalls
= 1;
1150 Type
*ScalarRetTy
= RetTy
;
1151 if (RetTy
->isVectorTy()) {
1152 if (ScalarizationCostPassed
== std::numeric_limits
<unsigned>::max())
1153 ScalarizationCost
= getScalarizationOverhead(RetTy
, true, false);
1154 ScalarCalls
= std::max(ScalarCalls
, RetTy
->getVectorNumElements());
1155 ScalarRetTy
= RetTy
->getScalarType();
1157 SmallVector
<Type
*, 4> ScalarTys
;
1158 for (unsigned i
= 0, ie
= Tys
.size(); i
!= ie
; ++i
) {
1160 if (Ty
->isVectorTy()) {
1161 if (ScalarizationCostPassed
== std::numeric_limits
<unsigned>::max())
1162 ScalarizationCost
+= getScalarizationOverhead(Ty
, false, true);
1163 ScalarCalls
= std::max(ScalarCalls
, Ty
->getVectorNumElements());
1164 Ty
= Ty
->getScalarType();
1166 ScalarTys
.push_back(Ty
);
1168 if (ScalarCalls
== 1)
1169 return 1; // Return cost of a scalar intrinsic. Assume it to be cheap.
1171 unsigned ScalarCost
=
1172 ConcreteTTI
->getIntrinsicInstrCost(IID
, ScalarRetTy
, ScalarTys
, FMF
);
1174 return ScalarCalls
* ScalarCost
+ ScalarizationCost
;
1176 // Look for intrinsics that can be lowered directly or turned into a scalar
1178 case Intrinsic::sqrt
:
1179 ISDs
.push_back(ISD::FSQRT
);
1181 case Intrinsic::sin
:
1182 ISDs
.push_back(ISD::FSIN
);
1184 case Intrinsic::cos
:
1185 ISDs
.push_back(ISD::FCOS
);
1187 case Intrinsic::exp
:
1188 ISDs
.push_back(ISD::FEXP
);
1190 case Intrinsic::exp2
:
1191 ISDs
.push_back(ISD::FEXP2
);
1193 case Intrinsic::log
:
1194 ISDs
.push_back(ISD::FLOG
);
1196 case Intrinsic::log10
:
1197 ISDs
.push_back(ISD::FLOG10
);
1199 case Intrinsic::log2
:
1200 ISDs
.push_back(ISD::FLOG2
);
1202 case Intrinsic::fabs
:
1203 ISDs
.push_back(ISD::FABS
);
1205 case Intrinsic::canonicalize
:
1206 ISDs
.push_back(ISD::FCANONICALIZE
);
1208 case Intrinsic::minnum
:
1209 ISDs
.push_back(ISD::FMINNUM
);
1211 ISDs
.push_back(ISD::FMINIMUM
);
1213 case Intrinsic::maxnum
:
1214 ISDs
.push_back(ISD::FMAXNUM
);
1216 ISDs
.push_back(ISD::FMAXIMUM
);
1218 case Intrinsic::copysign
:
1219 ISDs
.push_back(ISD::FCOPYSIGN
);
1221 case Intrinsic::floor
:
1222 ISDs
.push_back(ISD::FFLOOR
);
1224 case Intrinsic::ceil
:
1225 ISDs
.push_back(ISD::FCEIL
);
1227 case Intrinsic::trunc
:
1228 ISDs
.push_back(ISD::FTRUNC
);
1230 case Intrinsic::nearbyint
:
1231 ISDs
.push_back(ISD::FNEARBYINT
);
1233 case Intrinsic::rint
:
1234 ISDs
.push_back(ISD::FRINT
);
1236 case Intrinsic::round
:
1237 ISDs
.push_back(ISD::FROUND
);
1239 case Intrinsic::pow
:
1240 ISDs
.push_back(ISD::FPOW
);
1242 case Intrinsic::fma
:
1243 ISDs
.push_back(ISD::FMA
);
1245 case Intrinsic::fmuladd
:
1246 ISDs
.push_back(ISD::FMA
);
1248 // FIXME: We should return 0 whenever getIntrinsicCost == TCC_Free.
1249 case Intrinsic::lifetime_start
:
1250 case Intrinsic::lifetime_end
:
1251 case Intrinsic::sideeffect
:
1253 case Intrinsic::masked_store
:
1254 return ConcreteTTI
->getMaskedMemoryOpCost(Instruction::Store
, Tys
[0], 0,
1256 case Intrinsic::masked_load
:
1257 return ConcreteTTI
->getMaskedMemoryOpCost(Instruction::Load
, RetTy
, 0, 0);
1258 case Intrinsic::experimental_vector_reduce_add
:
1259 return ConcreteTTI
->getArithmeticReductionCost(Instruction::Add
, Tys
[0],
1260 /*IsPairwiseForm=*/false);
1261 case Intrinsic::experimental_vector_reduce_mul
:
1262 return ConcreteTTI
->getArithmeticReductionCost(Instruction::Mul
, Tys
[0],
1263 /*IsPairwiseForm=*/false);
1264 case Intrinsic::experimental_vector_reduce_and
:
1265 return ConcreteTTI
->getArithmeticReductionCost(Instruction::And
, Tys
[0],
1266 /*IsPairwiseForm=*/false);
1267 case Intrinsic::experimental_vector_reduce_or
:
1268 return ConcreteTTI
->getArithmeticReductionCost(Instruction::Or
, Tys
[0],
1269 /*IsPairwiseForm=*/false);
1270 case Intrinsic::experimental_vector_reduce_xor
:
1271 return ConcreteTTI
->getArithmeticReductionCost(Instruction::Xor
, Tys
[0],
1272 /*IsPairwiseForm=*/false);
1273 case Intrinsic::experimental_vector_reduce_v2_fadd
:
1274 return ConcreteTTI
->getArithmeticReductionCost(
1275 Instruction::FAdd
, Tys
[0],
1276 /*IsPairwiseForm=*/false); // FIXME: Add new flag for cost of strict
1278 case Intrinsic::experimental_vector_reduce_v2_fmul
:
1279 return ConcreteTTI
->getArithmeticReductionCost(
1280 Instruction::FMul
, Tys
[0],
1281 /*IsPairwiseForm=*/false); // FIXME: Add new flag for cost of strict
1283 case Intrinsic::experimental_vector_reduce_smax
:
1284 case Intrinsic::experimental_vector_reduce_smin
:
1285 case Intrinsic::experimental_vector_reduce_fmax
:
1286 case Intrinsic::experimental_vector_reduce_fmin
:
1287 return ConcreteTTI
->getMinMaxReductionCost(
1288 Tys
[0], CmpInst::makeCmpResultType(Tys
[0]), /*IsPairwiseForm=*/false,
1289 /*IsUnsigned=*/true);
1290 case Intrinsic::experimental_vector_reduce_umax
:
1291 case Intrinsic::experimental_vector_reduce_umin
:
1292 return ConcreteTTI
->getMinMaxReductionCost(
1293 Tys
[0], CmpInst::makeCmpResultType(Tys
[0]), /*IsPairwiseForm=*/false,
1294 /*IsUnsigned=*/false);
1295 case Intrinsic::sadd_sat
:
1296 case Intrinsic::ssub_sat
: {
1297 Type
*CondTy
= Type::getInt1Ty(RetTy
->getContext());
1299 CondTy
= VectorType::get(CondTy
, RetVF
);
1301 Type
*OpTy
= StructType::create({RetTy
, CondTy
});
1302 Intrinsic::ID OverflowOp
= IID
== Intrinsic::sadd_sat
1303 ? Intrinsic::sadd_with_overflow
1304 : Intrinsic::ssub_with_overflow
;
1306 // SatMax -> Overflow && SumDiff < 0
1307 // SatMin -> Overflow && SumDiff >= 0
1309 Cost
+= ConcreteTTI
->getIntrinsicInstrCost(
1310 OverflowOp
, OpTy
, {RetTy
, RetTy
}, FMF
, ScalarizationCostPassed
);
1311 Cost
+= ConcreteTTI
->getCmpSelInstrCost(BinaryOperator::ICmp
, RetTy
,
1313 Cost
+= 2 * ConcreteTTI
->getCmpSelInstrCost(BinaryOperator::Select
, RetTy
,
1317 case Intrinsic::uadd_sat
:
1318 case Intrinsic::usub_sat
: {
1319 Type
*CondTy
= Type::getInt1Ty(RetTy
->getContext());
1321 CondTy
= VectorType::get(CondTy
, RetVF
);
1323 Type
*OpTy
= StructType::create({RetTy
, CondTy
});
1324 Intrinsic::ID OverflowOp
= IID
== Intrinsic::uadd_sat
1325 ? Intrinsic::uadd_with_overflow
1326 : Intrinsic::usub_with_overflow
;
1329 Cost
+= ConcreteTTI
->getIntrinsicInstrCost(
1330 OverflowOp
, OpTy
, {RetTy
, RetTy
}, FMF
, ScalarizationCostPassed
);
1331 Cost
+= ConcreteTTI
->getCmpSelInstrCost(BinaryOperator::Select
, RetTy
,
1335 case Intrinsic::smul_fix
:
1336 case Intrinsic::umul_fix
: {
1337 unsigned ExtSize
= RetTy
->getScalarSizeInBits() * 2;
1338 Type
*ExtTy
= Type::getIntNTy(RetTy
->getContext(), ExtSize
);
1340 ExtTy
= VectorType::get(ExtTy
, RetVF
);
1343 IID
== Intrinsic::smul_fix
? Instruction::SExt
: Instruction::ZExt
;
1346 Cost
+= 2 * ConcreteTTI
->getCastInstrCost(ExtOp
, ExtTy
, RetTy
);
1347 Cost
+= ConcreteTTI
->getArithmeticInstrCost(Instruction::Mul
, ExtTy
);
1349 2 * ConcreteTTI
->getCastInstrCost(Instruction::Trunc
, RetTy
, ExtTy
);
1350 Cost
+= ConcreteTTI
->getArithmeticInstrCost(Instruction::LShr
, RetTy
,
1352 TTI::OK_UniformConstantValue
);
1353 Cost
+= ConcreteTTI
->getArithmeticInstrCost(Instruction::Shl
, RetTy
,
1355 TTI::OK_UniformConstantValue
);
1356 Cost
+= ConcreteTTI
->getArithmeticInstrCost(Instruction::Or
, RetTy
);
1359 case Intrinsic::sadd_with_overflow
:
1360 case Intrinsic::ssub_with_overflow
: {
1361 Type
*SumTy
= RetTy
->getContainedType(0);
1362 Type
*OverflowTy
= RetTy
->getContainedType(1);
1363 unsigned Opcode
= IID
== Intrinsic::sadd_with_overflow
1364 ? BinaryOperator::Add
1365 : BinaryOperator::Sub
;
1367 // LHSSign -> LHS >= 0
1368 // RHSSign -> RHS >= 0
1369 // SumSign -> Sum >= 0
1372 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
1374 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
1376 Cost
+= ConcreteTTI
->getArithmeticInstrCost(Opcode
, SumTy
);
1377 Cost
+= 3 * ConcreteTTI
->getCmpSelInstrCost(BinaryOperator::ICmp
, SumTy
,
1378 OverflowTy
, nullptr);
1379 Cost
+= 2 * ConcreteTTI
->getCmpSelInstrCost(
1380 BinaryOperator::ICmp
, OverflowTy
, OverflowTy
, nullptr);
1382 ConcreteTTI
->getArithmeticInstrCost(BinaryOperator::And
, OverflowTy
);
1385 case Intrinsic::uadd_with_overflow
:
1386 case Intrinsic::usub_with_overflow
: {
1387 Type
*SumTy
= RetTy
->getContainedType(0);
1388 Type
*OverflowTy
= RetTy
->getContainedType(1);
1389 unsigned Opcode
= IID
== Intrinsic::uadd_with_overflow
1390 ? BinaryOperator::Add
1391 : BinaryOperator::Sub
;
1394 Cost
+= ConcreteTTI
->getArithmeticInstrCost(Opcode
, SumTy
);
1395 Cost
+= ConcreteTTI
->getCmpSelInstrCost(BinaryOperator::ICmp
, SumTy
,
1396 OverflowTy
, nullptr);
1399 case Intrinsic::smul_with_overflow
:
1400 case Intrinsic::umul_with_overflow
: {
1401 Type
*MulTy
= RetTy
->getContainedType(0);
1402 Type
*OverflowTy
= RetTy
->getContainedType(1);
1403 unsigned ExtSize
= MulTy
->getScalarSizeInBits() * 2;
1404 Type
*ExtTy
= Type::getIntNTy(RetTy
->getContext(), ExtSize
);
1405 if (MulTy
->isVectorTy())
1406 ExtTy
= VectorType::get(ExtTy
, MulTy
->getVectorNumElements() );
1409 IID
== Intrinsic::smul_fix
? Instruction::SExt
: Instruction::ZExt
;
1412 Cost
+= 2 * ConcreteTTI
->getCastInstrCost(ExtOp
, ExtTy
, MulTy
);
1413 Cost
+= ConcreteTTI
->getArithmeticInstrCost(Instruction::Mul
, ExtTy
);
1415 2 * ConcreteTTI
->getCastInstrCost(Instruction::Trunc
, MulTy
, ExtTy
);
1416 Cost
+= ConcreteTTI
->getArithmeticInstrCost(Instruction::LShr
, MulTy
,
1418 TTI::OK_UniformConstantValue
);
1420 if (IID
== Intrinsic::smul_with_overflow
)
1421 Cost
+= ConcreteTTI
->getArithmeticInstrCost(
1422 Instruction::AShr
, MulTy
, TTI::OK_AnyValue
,
1423 TTI::OK_UniformConstantValue
);
1425 Cost
+= ConcreteTTI
->getCmpSelInstrCost(BinaryOperator::ICmp
, MulTy
,
1426 OverflowTy
, nullptr);
1429 case Intrinsic::ctpop
:
1430 ISDs
.push_back(ISD::CTPOP
);
1431 // In case of legalization use TCC_Expensive. This is cheaper than a
1432 // library call but still not a cheap instruction.
1433 SingleCallCost
= TargetTransformInfo::TCC_Expensive
;
1435 // FIXME: ctlz, cttz, ...
1438 const TargetLoweringBase
*TLI
= getTLI();
1439 std::pair
<unsigned, MVT
> LT
= TLI
->getTypeLegalizationCost(DL
, RetTy
);
1441 SmallVector
<unsigned, 2> LegalCost
;
1442 SmallVector
<unsigned, 2> CustomCost
;
1443 for (unsigned ISD
: ISDs
) {
1444 if (TLI
->isOperationLegalOrPromote(ISD
, LT
.second
)) {
1445 if (IID
== Intrinsic::fabs
&& LT
.second
.isFloatingPoint() &&
1446 TLI
->isFAbsFree(LT
.second
)) {
1450 // The operation is legal. Assume it costs 1.
1451 // If the type is split to multiple registers, assume that there is some
1452 // overhead to this.
1453 // TODO: Once we have extract/insert subvector cost we need to use them.
1455 LegalCost
.push_back(LT
.first
* 2);
1457 LegalCost
.push_back(LT
.first
* 1);
1458 } else if (!TLI
->isOperationExpand(ISD
, LT
.second
)) {
1459 // If the operation is custom lowered then assume
1460 // that the code is twice as expensive.
1461 CustomCost
.push_back(LT
.first
* 2);
1465 auto MinLegalCostI
= std::min_element(LegalCost
.begin(), LegalCost
.end());
1466 if (MinLegalCostI
!= LegalCost
.end())
1467 return *MinLegalCostI
;
1469 auto MinCustomCostI
=
1470 std::min_element(CustomCost
.begin(), CustomCost
.end());
1471 if (MinCustomCostI
!= CustomCost
.end())
1472 return *MinCustomCostI
;
1474 // If we can't lower fmuladd into an FMA estimate the cost as a floating
1475 // point mul followed by an add.
1476 if (IID
== Intrinsic::fmuladd
)
1477 return ConcreteTTI
->getArithmeticInstrCost(BinaryOperator::FMul
, RetTy
) +
1478 ConcreteTTI
->getArithmeticInstrCost(BinaryOperator::FAdd
, RetTy
);
1480 // Else, assume that we need to scalarize this intrinsic. For math builtins
1481 // this will emit a costly libcall, adding call overhead and spills. Make it
1483 if (RetTy
->isVectorTy()) {
1484 unsigned ScalarizationCost
=
1485 ((ScalarizationCostPassed
!= std::numeric_limits
<unsigned>::max())
1486 ? ScalarizationCostPassed
1487 : getScalarizationOverhead(RetTy
, true, false));
1488 unsigned ScalarCalls
= RetTy
->getVectorNumElements();
1489 SmallVector
<Type
*, 4> ScalarTys
;
1490 for (unsigned i
= 0, ie
= Tys
.size(); i
!= ie
; ++i
) {
1492 if (Ty
->isVectorTy())
1493 Ty
= Ty
->getScalarType();
1494 ScalarTys
.push_back(Ty
);
1496 unsigned ScalarCost
= ConcreteTTI
->getIntrinsicInstrCost(
1497 IID
, RetTy
->getScalarType(), ScalarTys
, FMF
);
1498 for (unsigned i
= 0, ie
= Tys
.size(); i
!= ie
; ++i
) {
1499 if (Tys
[i
]->isVectorTy()) {
1500 if (ScalarizationCostPassed
== std::numeric_limits
<unsigned>::max())
1501 ScalarizationCost
+= getScalarizationOverhead(Tys
[i
], false, true);
1502 ScalarCalls
= std::max(ScalarCalls
, Tys
[i
]->getVectorNumElements());
1506 return ScalarCalls
* ScalarCost
+ ScalarizationCost
;
1509 // This is going to be turned into a library call, make it expensive.
1510 return SingleCallCost
;
1513 /// Compute a cost of the given call instruction.
1515 /// Compute the cost of calling function F with return type RetTy and
1516 /// argument types Tys. F might be nullptr, in this case the cost of an
1517 /// arbitrary call with the specified signature will be returned.
1518 /// This is used, for instance, when we estimate call of a vector
1519 /// counterpart of the given function.
1520 /// \param F Called function, might be nullptr.
1521 /// \param RetTy Return value types.
1522 /// \param Tys Argument types.
1523 /// \returns The cost of Call instruction.
1524 unsigned getCallInstrCost(Function
*F
, Type
*RetTy
, ArrayRef
<Type
*> Tys
) {
1528 unsigned getNumberOfParts(Type
*Tp
) {
1529 std::pair
<unsigned, MVT
> LT
= getTLI()->getTypeLegalizationCost(DL
, Tp
);
1533 unsigned getAddressComputationCost(Type
*Ty
, ScalarEvolution
*,
1538 /// Try to calculate arithmetic and shuffle op costs for reduction operations.
1539 /// We're assuming that reduction operation are performing the following way:
1540 /// 1. Non-pairwise reduction
1541 /// %val1 = shufflevector<n x t> %val, <n x t> %undef,
1542 /// <n x i32> <i32 n/2, i32 n/2 + 1, ..., i32 n, i32 undef, ..., i32 undef>
1543 /// \----------------v-------------/ \----------v------------/
1544 /// n/2 elements n/2 elements
1545 /// %red1 = op <n x t> %val, <n x t> val1
1546 /// After this operation we have a vector %red1 where only the first n/2
1547 /// elements are meaningful, the second n/2 elements are undefined and can be
1548 /// dropped. All other operations are actually working with the vector of
1549 /// length n/2, not n, though the real vector length is still n.
1550 /// %val2 = shufflevector<n x t> %red1, <n x t> %undef,
1551 /// <n x i32> <i32 n/4, i32 n/4 + 1, ..., i32 n/2, i32 undef, ..., i32 undef>
1552 /// \----------------v-------------/ \----------v------------/
1553 /// n/4 elements 3*n/4 elements
1554 /// %red2 = op <n x t> %red1, <n x t> val2 - working with the vector of
1555 /// length n/2, the resulting vector has length n/4 etc.
1556 /// 2. Pairwise reduction:
1557 /// Everything is the same except for an additional shuffle operation which
1558 /// is used to produce operands for pairwise kind of reductions.
1559 /// %val1 = shufflevector<n x t> %val, <n x t> %undef,
1560 /// <n x i32> <i32 0, i32 2, ..., i32 n-2, i32 undef, ..., i32 undef>
1561 /// \-------------v----------/ \----------v------------/
1562 /// n/2 elements n/2 elements
1563 /// %val2 = shufflevector<n x t> %val, <n x t> %undef,
1564 /// <n x i32> <i32 1, i32 3, ..., i32 n-1, i32 undef, ..., i32 undef>
1565 /// \-------------v----------/ \----------v------------/
1566 /// n/2 elements n/2 elements
1567 /// %red1 = op <n x t> %val1, <n x t> val2
1568 /// Again, the operation is performed on <n x t> vector, but the resulting
1569 /// vector %red1 is <n/2 x t> vector.
1571 /// The cost model should take into account that the actual length of the
1572 /// vector is reduced on each iteration.
1573 unsigned getArithmeticReductionCost(unsigned Opcode
, Type
*Ty
,
1575 assert(Ty
->isVectorTy() && "Expect a vector type");
1576 Type
*ScalarTy
= Ty
->getVectorElementType();
1577 unsigned NumVecElts
= Ty
->getVectorNumElements();
1578 unsigned NumReduxLevels
= Log2_32(NumVecElts
);
1579 unsigned ArithCost
= 0;
1580 unsigned ShuffleCost
= 0;
1581 auto *ConcreteTTI
= static_cast<T
*>(this);
1582 std::pair
<unsigned, MVT
> LT
=
1583 ConcreteTTI
->getTLI()->getTypeLegalizationCost(DL
, Ty
);
1584 unsigned LongVectorCount
= 0;
1586 LT
.second
.isVector() ? LT
.second
.getVectorNumElements() : 1;
1587 while (NumVecElts
> MVTLen
) {
1589 Type
*SubTy
= VectorType::get(ScalarTy
, NumVecElts
);
1590 // Assume the pairwise shuffles add a cost.
1591 ShuffleCost
+= (IsPairwise
+ 1) *
1592 ConcreteTTI
->getShuffleCost(TTI::SK_ExtractSubvector
, Ty
,
1594 ArithCost
+= ConcreteTTI
->getArithmeticInstrCost(Opcode
, SubTy
);
1599 NumReduxLevels
-= LongVectorCount
;
1601 // The minimal length of the vector is limited by the real length of vector
1602 // operations performed on the current platform. That's why several final
1603 // reduction operations are performed on the vectors with the same
1604 // architecture-dependent length.
1606 // Non pairwise reductions need one shuffle per reduction level. Pairwise
1607 // reductions need two shuffles on every level, but the last one. On that
1608 // level one of the shuffles is <0, u, u, ...> which is identity.
1609 unsigned NumShuffles
= NumReduxLevels
;
1610 if (IsPairwise
&& NumReduxLevels
>= 1)
1611 NumShuffles
+= NumReduxLevels
- 1;
1612 ShuffleCost
+= NumShuffles
*
1613 ConcreteTTI
->getShuffleCost(TTI::SK_PermuteSingleSrc
, Ty
,
1615 ArithCost
+= NumReduxLevels
*
1616 ConcreteTTI
->getArithmeticInstrCost(Opcode
, Ty
);
1617 return ShuffleCost
+ ArithCost
+
1618 ConcreteTTI
->getVectorInstrCost(Instruction::ExtractElement
, Ty
, 0);
1621 /// Try to calculate op costs for min/max reduction operations.
1622 /// \param CondTy Conditional type for the Select instruction.
1623 unsigned getMinMaxReductionCost(Type
*Ty
, Type
*CondTy
, bool IsPairwise
,
1625 assert(Ty
->isVectorTy() && "Expect a vector type");
1626 Type
*ScalarTy
= Ty
->getVectorElementType();
1627 Type
*ScalarCondTy
= CondTy
->getVectorElementType();
1628 unsigned NumVecElts
= Ty
->getVectorNumElements();
1629 unsigned NumReduxLevels
= Log2_32(NumVecElts
);
1631 if (Ty
->isFPOrFPVectorTy()) {
1632 CmpOpcode
= Instruction::FCmp
;
1634 assert(Ty
->isIntOrIntVectorTy() &&
1635 "expecting floating point or integer type for min/max reduction");
1636 CmpOpcode
= Instruction::ICmp
;
1638 unsigned MinMaxCost
= 0;
1639 unsigned ShuffleCost
= 0;
1640 auto *ConcreteTTI
= static_cast<T
*>(this);
1641 std::pair
<unsigned, MVT
> LT
=
1642 ConcreteTTI
->getTLI()->getTypeLegalizationCost(DL
, Ty
);
1643 unsigned LongVectorCount
= 0;
1645 LT
.second
.isVector() ? LT
.second
.getVectorNumElements() : 1;
1646 while (NumVecElts
> MVTLen
) {
1648 Type
*SubTy
= VectorType::get(ScalarTy
, NumVecElts
);
1649 CondTy
= VectorType::get(ScalarCondTy
, NumVecElts
);
1651 // Assume the pairwise shuffles add a cost.
1652 ShuffleCost
+= (IsPairwise
+ 1) *
1653 ConcreteTTI
->getShuffleCost(TTI::SK_ExtractSubvector
, Ty
,
1656 ConcreteTTI
->getCmpSelInstrCost(CmpOpcode
, SubTy
, CondTy
, nullptr) +
1657 ConcreteTTI
->getCmpSelInstrCost(Instruction::Select
, SubTy
, CondTy
,
1663 NumReduxLevels
-= LongVectorCount
;
1665 // The minimal length of the vector is limited by the real length of vector
1666 // operations performed on the current platform. That's why several final
1667 // reduction opertions are perfomed on the vectors with the same
1668 // architecture-dependent length.
1670 // Non pairwise reductions need one shuffle per reduction level. Pairwise
1671 // reductions need two shuffles on every level, but the last one. On that
1672 // level one of the shuffles is <0, u, u, ...> which is identity.
1673 unsigned NumShuffles
= NumReduxLevels
;
1674 if (IsPairwise
&& NumReduxLevels
>= 1)
1675 NumShuffles
+= NumReduxLevels
- 1;
1676 ShuffleCost
+= NumShuffles
*
1677 ConcreteTTI
->getShuffleCost(TTI::SK_PermuteSingleSrc
, Ty
,
1681 (ConcreteTTI
->getCmpSelInstrCost(CmpOpcode
, Ty
, CondTy
, nullptr) +
1682 ConcreteTTI
->getCmpSelInstrCost(Instruction::Select
, Ty
, CondTy
,
1684 // The last min/max should be in vector registers and we counted it above.
1685 // So just need a single extractelement.
1686 return ShuffleCost
+ MinMaxCost
+
1687 ConcreteTTI
->getVectorInstrCost(Instruction::ExtractElement
, Ty
, 0);
1690 unsigned getVectorSplitCost() { return 1; }
1695 /// Concrete BasicTTIImpl that can be used if no further customization
1697 class BasicTTIImpl
: public BasicTTIImplBase
<BasicTTIImpl
> {
1698 using BaseT
= BasicTTIImplBase
<BasicTTIImpl
>;
1700 friend class BasicTTIImplBase
<BasicTTIImpl
>;
1702 const TargetSubtargetInfo
*ST
;
1703 const TargetLoweringBase
*TLI
;
1705 const TargetSubtargetInfo
*getST() const { return ST
; }
1706 const TargetLoweringBase
*getTLI() const { return TLI
; }
1709 explicit BasicTTIImpl(const TargetMachine
*TM
, const Function
&F
);
1712 } // end namespace llvm
1714 #endif // LLVM_CODEGEN_BASICTTIIMPL_H