[Alignment][NFC] Migrate Instructions to Align
[llvm-core.git] / include / llvm / Analysis / TargetTransformInfoImpl.h
blobae2f5b8fcc9f89b3a1eb870c34e73debbf308301
1 //===- TargetTransformInfoImpl.h --------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 /// This file provides helpers for the implementation of
10 /// a TargetTransformInfo-conforming class.
11 ///
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_TARGETTRANSFORMINFOIMPL_H
15 #define LLVM_ANALYSIS_TARGETTRANSFORMINFOIMPL_H
17 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
18 #include "llvm/Analysis/TargetTransformInfo.h"
19 #include "llvm/Analysis/VectorUtils.h"
20 #include "llvm/IR/CallSite.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/IR/GetElementPtrTypeIterator.h"
24 #include "llvm/IR/Operator.h"
25 #include "llvm/IR/Type.h"
27 namespace llvm {
29 /// Base class for use as a mix-in that aids implementing
30 /// a TargetTransformInfo-compatible class.
31 class TargetTransformInfoImplBase {
32 protected:
33 typedef TargetTransformInfo TTI;
35 const DataLayout &DL;
37 explicit TargetTransformInfoImplBase(const DataLayout &DL) : DL(DL) {}
39 public:
40 // Provide value semantics. MSVC requires that we spell all of these out.
41 TargetTransformInfoImplBase(const TargetTransformInfoImplBase &Arg)
42 : DL(Arg.DL) {}
43 TargetTransformInfoImplBase(TargetTransformInfoImplBase &&Arg) : DL(Arg.DL) {}
45 const DataLayout &getDataLayout() const { return DL; }
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;
53 case Instruction::GetElementPtr:
54 llvm_unreachable("Use getGEPCost for GEP operations!");
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;
62 // Otherwise, the default basic cost is used.
63 return TTI::TCC_Basic;
65 case Instruction::FDiv:
66 case Instruction::FRem:
67 case Instruction::SDiv:
68 case Instruction::SRem:
69 case Instruction::UDiv:
70 case Instruction::URem:
71 return TTI::TCC_Expensive;
73 case Instruction::IntToPtr: {
74 // An inttoptr cast is free so long as the input is a legal integer type
75 // which doesn't contain values outside the range of a pointer.
76 unsigned OpSize = OpTy->getScalarSizeInBits();
77 if (DL.isLegalInteger(OpSize) &&
78 OpSize <= DL.getPointerTypeSizeInBits(Ty))
79 return TTI::TCC_Free;
81 // Otherwise it's not a no-op.
82 return TTI::TCC_Basic;
84 case Instruction::PtrToInt: {
85 // A ptrtoint cast is free so long as the result is large enough to store
86 // the pointer, and a legal integer type.
87 unsigned DestSize = Ty->getScalarSizeInBits();
88 if (DL.isLegalInteger(DestSize) &&
89 DestSize >= DL.getPointerTypeSizeInBits(OpTy))
90 return TTI::TCC_Free;
92 // Otherwise it's not a no-op.
93 return TTI::TCC_Basic;
95 case Instruction::Trunc:
96 // trunc to a native type is free (assuming the target has compare and
97 // shift-right of the same width).
98 if (DL.isLegalInteger(DL.getTypeSizeInBits(Ty)))
99 return TTI::TCC_Free;
101 return TTI::TCC_Basic;
105 int getGEPCost(Type *PointeeType, const Value *Ptr,
106 ArrayRef<const Value *> Operands) {
107 // In the basic model, we just assume that all-constant GEPs will be folded
108 // into their uses via addressing modes.
109 for (unsigned Idx = 0, Size = Operands.size(); Idx != Size; ++Idx)
110 if (!isa<Constant>(Operands[Idx]))
111 return TTI::TCC_Basic;
113 return TTI::TCC_Free;
116 unsigned getEstimatedNumberOfCaseClusters(const SwitchInst &SI,
117 unsigned &JTSize) {
118 JTSize = 0;
119 return SI.getNumCases();
122 int getExtCost(const Instruction *I, const Value *Src) {
123 return TTI::TCC_Basic;
126 unsigned getCallCost(FunctionType *FTy, int NumArgs, const User *U) {
127 assert(FTy && "FunctionType must be provided to this routine.");
129 // The target-independent implementation just measures the size of the
130 // function by approximating that each argument will take on average one
131 // instruction to prepare.
133 if (NumArgs < 0)
134 // Set the argument number to the number of explicit arguments in the
135 // function.
136 NumArgs = FTy->getNumParams();
138 return TTI::TCC_Basic * (NumArgs + 1);
141 unsigned getInliningThresholdMultiplier() { return 1; }
143 int getInlinerVectorBonusPercent() { return 150; }
145 unsigned getMemcpyCost(const Instruction *I) {
146 return TTI::TCC_Expensive;
149 bool hasBranchDivergence() { return false; }
151 bool isSourceOfDivergence(const Value *V) { return false; }
153 bool isAlwaysUniform(const Value *V) { return false; }
155 unsigned getFlatAddressSpace () {
156 return -1;
159 bool collectFlatAddressOperands(SmallVectorImpl<int> &OpIndexes,
160 Intrinsic::ID IID) const {
161 return false;
164 bool rewriteIntrinsicWithAddressSpace(IntrinsicInst *II,
165 Value *OldV, Value *NewV) const {
166 return false;
169 bool isLoweredToCall(const Function *F) {
170 assert(F && "A concrete function must be provided to this routine.");
172 // FIXME: These should almost certainly not be handled here, and instead
173 // handled with the help of TLI or the target itself. This was largely
174 // ported from existing analysis heuristics here so that such refactorings
175 // can take place in the future.
177 if (F->isIntrinsic())
178 return false;
180 if (F->hasLocalLinkage() || !F->hasName())
181 return true;
183 StringRef Name = F->getName();
185 // These will all likely lower to a single selection DAG node.
186 if (Name == "copysign" || Name == "copysignf" || Name == "copysignl" ||
187 Name == "fabs" || Name == "fabsf" || Name == "fabsl" || Name == "sin" ||
188 Name == "fmin" || Name == "fminf" || Name == "fminl" ||
189 Name == "fmax" || Name == "fmaxf" || Name == "fmaxl" ||
190 Name == "sinf" || Name == "sinl" || Name == "cos" || Name == "cosf" ||
191 Name == "cosl" || Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl")
192 return false;
194 // These are all likely to be optimized into something smaller.
195 if (Name == "pow" || Name == "powf" || Name == "powl" || Name == "exp2" ||
196 Name == "exp2l" || Name == "exp2f" || Name == "floor" ||
197 Name == "floorf" || Name == "ceil" || Name == "round" ||
198 Name == "ffs" || Name == "ffsl" || Name == "abs" || Name == "labs" ||
199 Name == "llabs")
200 return false;
202 return true;
205 bool isHardwareLoopProfitable(Loop *L, ScalarEvolution &SE,
206 AssumptionCache &AC,
207 TargetLibraryInfo *LibInfo,
208 HardwareLoopInfo &HWLoopInfo) {
209 return false;
212 void getUnrollingPreferences(Loop *, ScalarEvolution &,
213 TTI::UnrollingPreferences &) {}
215 bool isLegalAddImmediate(int64_t Imm) { return false; }
217 bool isLegalICmpImmediate(int64_t Imm) { return false; }
219 bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
220 bool HasBaseReg, int64_t Scale,
221 unsigned AddrSpace, Instruction *I = nullptr) {
222 // Guess that only reg and reg+reg addressing is allowed. This heuristic is
223 // taken from the implementation of LSR.
224 return !BaseGV && BaseOffset == 0 && (Scale == 0 || Scale == 1);
227 bool isLSRCostLess(TTI::LSRCost &C1, TTI::LSRCost &C2) {
228 return std::tie(C1.NumRegs, C1.AddRecCost, C1.NumIVMuls, C1.NumBaseAdds,
229 C1.ScaleCost, C1.ImmCost, C1.SetupCost) <
230 std::tie(C2.NumRegs, C2.AddRecCost, C2.NumIVMuls, C2.NumBaseAdds,
231 C2.ScaleCost, C2.ImmCost, C2.SetupCost);
234 bool canMacroFuseCmp() { return false; }
236 bool canSaveCmp(Loop *L, BranchInst **BI, ScalarEvolution *SE, LoopInfo *LI,
237 DominatorTree *DT, AssumptionCache *AC,
238 TargetLibraryInfo *LibInfo) {
239 return false;
242 bool shouldFavorPostInc() const { return false; }
244 bool shouldFavorBackedgeIndex(const Loop *L) const { return false; }
246 bool isLegalMaskedStore(Type *DataType) { return false; }
248 bool isLegalMaskedLoad(Type *DataType) { return false; }
250 bool isLegalNTStore(Type *DataType, llvm::Align Alignment) {
251 // By default, assume nontemporal memory stores are available for stores
252 // that are aligned and have a size that is a power of 2.
253 unsigned DataSize = DL.getTypeStoreSize(DataType);
254 return Alignment >= DataSize && isPowerOf2_32(DataSize);
257 bool isLegalNTLoad(Type *DataType, llvm::Align Alignment) {
258 // By default, assume nontemporal memory loads are available for loads that
259 // are aligned and have a size that is a power of 2.
260 unsigned DataSize = DL.getTypeStoreSize(DataType);
261 return Alignment >= DataSize && isPowerOf2_32(DataSize);
264 bool isLegalMaskedScatter(Type *DataType) { return false; }
266 bool isLegalMaskedGather(Type *DataType) { return false; }
268 bool isLegalMaskedCompressStore(Type *DataType) { return false; }
270 bool isLegalMaskedExpandLoad(Type *DataType) { return false; }
272 bool hasDivRemOp(Type *DataType, bool IsSigned) { return false; }
274 bool hasVolatileVariant(Instruction *I, unsigned AddrSpace) { return false; }
276 bool prefersVectorizedAddressing() { return true; }
278 int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
279 bool HasBaseReg, int64_t Scale, unsigned AddrSpace) {
280 // Guess that all legal addressing mode are free.
281 if (isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
282 Scale, AddrSpace))
283 return 0;
284 return -1;
287 bool LSRWithInstrQueries() { return false; }
289 bool isTruncateFree(Type *Ty1, Type *Ty2) { return false; }
291 bool isProfitableToHoist(Instruction *I) { return true; }
293 bool useAA() { return false; }
295 bool isTypeLegal(Type *Ty) { return false; }
297 bool shouldBuildLookupTables() { return true; }
298 bool shouldBuildLookupTablesForConstant(Constant *C) { return true; }
300 bool useColdCCForColdCall(Function &F) { return false; }
302 unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) {
303 return 0;
306 unsigned getOperandsScalarizationOverhead(ArrayRef<const Value *> Args,
307 unsigned VF) { return 0; }
309 bool supportsEfficientVectorElementLoadStore() { return false; }
311 bool enableAggressiveInterleaving(bool LoopHasReductions) { return false; }
313 TTI::MemCmpExpansionOptions enableMemCmpExpansion(bool OptSize,
314 bool IsZeroCmp) const {
315 return {};
318 bool enableInterleavedAccessVectorization() { return false; }
320 bool enableMaskedInterleavedAccessVectorization() { return false; }
322 bool isFPVectorizationPotentiallyUnsafe() { return false; }
324 bool allowsMisalignedMemoryAccesses(LLVMContext &Context,
325 unsigned BitWidth,
326 unsigned AddressSpace,
327 unsigned Alignment,
328 bool *Fast) { return false; }
330 TTI::PopcntSupportKind getPopcntSupport(unsigned IntTyWidthInBit) {
331 return TTI::PSK_Software;
334 bool haveFastSqrt(Type *Ty) { return false; }
336 bool isFCmpOrdCheaperThanFCmpZero(Type *Ty) { return true; }
338 unsigned getFPOpCost(Type *Ty) { return TargetTransformInfo::TCC_Basic; }
340 int getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
341 Type *Ty) {
342 return 0;
345 unsigned getIntImmCost(const APInt &Imm, Type *Ty) { return TTI::TCC_Basic; }
347 unsigned getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
348 Type *Ty) {
349 return TTI::TCC_Free;
352 unsigned getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
353 Type *Ty) {
354 return TTI::TCC_Free;
357 unsigned getNumberOfRegisters(bool Vector) { return 8; }
359 unsigned getRegisterBitWidth(bool Vector) const { return 32; }
361 unsigned getMinVectorRegisterBitWidth() { return 128; }
363 bool shouldMaximizeVectorBandwidth(bool OptSize) const { return false; }
365 unsigned getMinimumVF(unsigned ElemWidth) const { return 0; }
367 bool
368 shouldConsiderAddressTypePromotion(const Instruction &I,
369 bool &AllowPromotionWithoutCommonHeader) {
370 AllowPromotionWithoutCommonHeader = false;
371 return false;
374 unsigned getCacheLineSize() { return 0; }
376 llvm::Optional<unsigned> getCacheSize(TargetTransformInfo::CacheLevel Level) {
377 switch (Level) {
378 case TargetTransformInfo::CacheLevel::L1D:
379 LLVM_FALLTHROUGH;
380 case TargetTransformInfo::CacheLevel::L2D:
381 return llvm::Optional<unsigned>();
384 llvm_unreachable("Unknown TargetTransformInfo::CacheLevel");
387 llvm::Optional<unsigned> getCacheAssociativity(
388 TargetTransformInfo::CacheLevel Level) {
389 switch (Level) {
390 case TargetTransformInfo::CacheLevel::L1D:
391 LLVM_FALLTHROUGH;
392 case TargetTransformInfo::CacheLevel::L2D:
393 return llvm::Optional<unsigned>();
396 llvm_unreachable("Unknown TargetTransformInfo::CacheLevel");
399 unsigned getPrefetchDistance() { return 0; }
401 unsigned getMinPrefetchStride() { return 1; }
403 unsigned getMaxPrefetchIterationsAhead() { return UINT_MAX; }
405 unsigned getMaxInterleaveFactor(unsigned VF) { return 1; }
407 unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty,
408 TTI::OperandValueKind Opd1Info,
409 TTI::OperandValueKind Opd2Info,
410 TTI::OperandValueProperties Opd1PropInfo,
411 TTI::OperandValueProperties Opd2PropInfo,
412 ArrayRef<const Value *> Args) {
413 return 1;
416 unsigned getShuffleCost(TTI::ShuffleKind Kind, Type *Ty, int Index,
417 Type *SubTp) {
418 return 1;
421 unsigned getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src,
422 const Instruction *I) { return 1; }
424 unsigned getExtractWithExtendCost(unsigned Opcode, Type *Dst,
425 VectorType *VecTy, unsigned Index) {
426 return 1;
429 unsigned getCFInstrCost(unsigned Opcode) { return 1; }
431 unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy,
432 const Instruction *I) {
433 return 1;
436 unsigned getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) {
437 return 1;
440 unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
441 unsigned AddressSpace, const Instruction *I) {
442 return 1;
445 unsigned getMaskedMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
446 unsigned AddressSpace) {
447 return 1;
450 unsigned getGatherScatterOpCost(unsigned Opcode, Type *DataTy, Value *Ptr,
451 bool VariableMask,
452 unsigned Alignment) {
453 return 1;
456 unsigned getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy,
457 unsigned Factor,
458 ArrayRef<unsigned> Indices,
459 unsigned Alignment, unsigned AddressSpace,
460 bool UseMaskForCond = false,
461 bool UseMaskForGaps = false) {
462 return 1;
465 unsigned getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
466 ArrayRef<Type *> Tys, FastMathFlags FMF,
467 unsigned ScalarizationCostPassed) {
468 return 1;
470 unsigned getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
471 ArrayRef<Value *> Args, FastMathFlags FMF, unsigned VF) {
472 return 1;
475 unsigned getCallInstrCost(Function *F, Type *RetTy, ArrayRef<Type *> Tys) {
476 return 1;
479 unsigned getNumberOfParts(Type *Tp) { return 0; }
481 unsigned getAddressComputationCost(Type *Tp, ScalarEvolution *,
482 const SCEV *) {
483 return 0;
486 unsigned getArithmeticReductionCost(unsigned, Type *, bool) { return 1; }
488 unsigned getMinMaxReductionCost(Type *, Type *, bool, bool) { return 1; }
490 unsigned getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) { return 0; }
492 bool getTgtMemIntrinsic(IntrinsicInst *Inst, MemIntrinsicInfo &Info) {
493 return false;
496 unsigned getAtomicMemIntrinsicMaxElementSize() const {
497 // Note for overrides: You must ensure for all element unordered-atomic
498 // memory intrinsics that all power-of-2 element sizes up to, and
499 // including, the return value of this method have a corresponding
500 // runtime lib call. These runtime lib call definitions can be found
501 // in RuntimeLibcalls.h
502 return 0;
505 Value *getOrCreateResultFromMemIntrinsic(IntrinsicInst *Inst,
506 Type *ExpectedType) {
507 return nullptr;
510 Type *getMemcpyLoopLoweringType(LLVMContext &Context, Value *Length,
511 unsigned SrcAlign, unsigned DestAlign) const {
512 return Type::getInt8Ty(Context);
515 void getMemcpyLoopResidualLoweringType(SmallVectorImpl<Type *> &OpsOut,
516 LLVMContext &Context,
517 unsigned RemainingBytes,
518 unsigned SrcAlign,
519 unsigned DestAlign) const {
520 for (unsigned i = 0; i != RemainingBytes; ++i)
521 OpsOut.push_back(Type::getInt8Ty(Context));
524 bool areInlineCompatible(const Function *Caller,
525 const Function *Callee) const {
526 return (Caller->getFnAttribute("target-cpu") ==
527 Callee->getFnAttribute("target-cpu")) &&
528 (Caller->getFnAttribute("target-features") ==
529 Callee->getFnAttribute("target-features"));
532 bool areFunctionArgsABICompatible(const Function *Caller, const Function *Callee,
533 SmallPtrSetImpl<Argument *> &Args) const {
534 return (Caller->getFnAttribute("target-cpu") ==
535 Callee->getFnAttribute("target-cpu")) &&
536 (Caller->getFnAttribute("target-features") ==
537 Callee->getFnAttribute("target-features"));
540 bool isIndexedLoadLegal(TTI::MemIndexedMode Mode, Type *Ty,
541 const DataLayout &DL) const {
542 return false;
545 bool isIndexedStoreLegal(TTI::MemIndexedMode Mode, Type *Ty,
546 const DataLayout &DL) const {
547 return false;
550 unsigned getLoadStoreVecRegBitWidth(unsigned AddrSpace) const { return 128; }
552 bool isLegalToVectorizeLoad(LoadInst *LI) const { return true; }
554 bool isLegalToVectorizeStore(StoreInst *SI) const { return true; }
556 bool isLegalToVectorizeLoadChain(unsigned ChainSizeInBytes,
557 unsigned Alignment,
558 unsigned AddrSpace) const {
559 return true;
562 bool isLegalToVectorizeStoreChain(unsigned ChainSizeInBytes,
563 unsigned Alignment,
564 unsigned AddrSpace) const {
565 return true;
568 unsigned getLoadVectorFactor(unsigned VF, unsigned LoadSize,
569 unsigned ChainSizeInBytes,
570 VectorType *VecTy) const {
571 return VF;
574 unsigned getStoreVectorFactor(unsigned VF, unsigned StoreSize,
575 unsigned ChainSizeInBytes,
576 VectorType *VecTy) const {
577 return VF;
580 bool useReductionIntrinsic(unsigned Opcode, Type *Ty,
581 TTI::ReductionFlags Flags) const {
582 return false;
585 bool shouldExpandReduction(const IntrinsicInst *II) const {
586 return true;
589 unsigned getGISelRematGlobalCost() const {
590 return 1;
593 protected:
594 // Obtain the minimum required size to hold the value (without the sign)
595 // In case of a vector it returns the min required size for one element.
596 unsigned minRequiredElementSize(const Value* Val, bool &isSigned) {
597 if (isa<ConstantDataVector>(Val) || isa<ConstantVector>(Val)) {
598 const auto* VectorValue = cast<Constant>(Val);
600 // In case of a vector need to pick the max between the min
601 // required size for each element
602 auto *VT = cast<VectorType>(Val->getType());
604 // Assume unsigned elements
605 isSigned = false;
607 // The max required size is the total vector width divided by num
608 // of elements in the vector
609 unsigned MaxRequiredSize = VT->getBitWidth() / VT->getNumElements();
611 unsigned MinRequiredSize = 0;
612 for(unsigned i = 0, e = VT->getNumElements(); i < e; ++i) {
613 if (auto* IntElement =
614 dyn_cast<ConstantInt>(VectorValue->getAggregateElement(i))) {
615 bool signedElement = IntElement->getValue().isNegative();
616 // Get the element min required size.
617 unsigned ElementMinRequiredSize =
618 IntElement->getValue().getMinSignedBits() - 1;
619 // In case one element is signed then all the vector is signed.
620 isSigned |= signedElement;
621 // Save the max required bit size between all the elements.
622 MinRequiredSize = std::max(MinRequiredSize, ElementMinRequiredSize);
624 else {
625 // not an int constant element
626 return MaxRequiredSize;
629 return MinRequiredSize;
632 if (const auto* CI = dyn_cast<ConstantInt>(Val)) {
633 isSigned = CI->getValue().isNegative();
634 return CI->getValue().getMinSignedBits() - 1;
637 if (const auto* Cast = dyn_cast<SExtInst>(Val)) {
638 isSigned = true;
639 return Cast->getSrcTy()->getScalarSizeInBits() - 1;
642 if (const auto* Cast = dyn_cast<ZExtInst>(Val)) {
643 isSigned = false;
644 return Cast->getSrcTy()->getScalarSizeInBits();
647 isSigned = false;
648 return Val->getType()->getScalarSizeInBits();
651 bool isStridedAccess(const SCEV *Ptr) {
652 return Ptr && isa<SCEVAddRecExpr>(Ptr);
655 const SCEVConstant *getConstantStrideStep(ScalarEvolution *SE,
656 const SCEV *Ptr) {
657 if (!isStridedAccess(Ptr))
658 return nullptr;
659 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ptr);
660 return dyn_cast<SCEVConstant>(AddRec->getStepRecurrence(*SE));
663 bool isConstantStridedAccessLessThan(ScalarEvolution *SE, const SCEV *Ptr,
664 int64_t MergeDistance) {
665 const SCEVConstant *Step = getConstantStrideStep(SE, Ptr);
666 if (!Step)
667 return false;
668 APInt StrideVal = Step->getAPInt();
669 if (StrideVal.getBitWidth() > 64)
670 return false;
671 // FIXME: Need to take absolute value for negative stride case.
672 return StrideVal.getSExtValue() < MergeDistance;
676 /// CRTP base class for use as a mix-in that aids implementing
677 /// a TargetTransformInfo-compatible class.
678 template <typename T>
679 class TargetTransformInfoImplCRTPBase : public TargetTransformInfoImplBase {
680 private:
681 typedef TargetTransformInfoImplBase BaseT;
683 protected:
684 explicit TargetTransformInfoImplCRTPBase(const DataLayout &DL) : BaseT(DL) {}
686 public:
687 using BaseT::getCallCost;
689 unsigned getCallCost(const Function *F, int NumArgs, const User *U) {
690 assert(F && "A concrete function must be provided to this routine.");
692 if (NumArgs < 0)
693 // Set the argument number to the number of explicit arguments in the
694 // function.
695 NumArgs = F->arg_size();
697 if (Intrinsic::ID IID = F->getIntrinsicID()) {
698 FunctionType *FTy = F->getFunctionType();
699 SmallVector<Type *, 8> ParamTys(FTy->param_begin(), FTy->param_end());
700 return static_cast<T *>(this)
701 ->getIntrinsicCost(IID, FTy->getReturnType(), ParamTys, U);
704 if (!static_cast<T *>(this)->isLoweredToCall(F))
705 return TTI::TCC_Basic; // Give a basic cost if it will be lowered
706 // directly.
708 return static_cast<T *>(this)->getCallCost(F->getFunctionType(), NumArgs, U);
711 unsigned getCallCost(const Function *F, ArrayRef<const Value *> Arguments,
712 const User *U) {
713 // Simply delegate to generic handling of the call.
714 // FIXME: We should use instsimplify or something else to catch calls which
715 // will constant fold with these arguments.
716 return static_cast<T *>(this)->getCallCost(F, Arguments.size(), U);
719 using BaseT::getGEPCost;
721 int getGEPCost(Type *PointeeType, const Value *Ptr,
722 ArrayRef<const Value *> Operands) {
723 assert(PointeeType && Ptr && "can't get GEPCost of nullptr");
724 // TODO: will remove this when pointers have an opaque type.
725 assert(Ptr->getType()->getScalarType()->getPointerElementType() ==
726 PointeeType &&
727 "explicit pointee type doesn't match operand's pointee type");
728 auto *BaseGV = dyn_cast<GlobalValue>(Ptr->stripPointerCasts());
729 bool HasBaseReg = (BaseGV == nullptr);
731 auto PtrSizeBits = DL.getPointerTypeSizeInBits(Ptr->getType());
732 APInt BaseOffset(PtrSizeBits, 0);
733 int64_t Scale = 0;
735 auto GTI = gep_type_begin(PointeeType, Operands);
736 Type *TargetType = nullptr;
738 // Handle the case where the GEP instruction has a single operand,
739 // the basis, therefore TargetType is a nullptr.
740 if (Operands.empty())
741 return !BaseGV ? TTI::TCC_Free : TTI::TCC_Basic;
743 for (auto I = Operands.begin(); I != Operands.end(); ++I, ++GTI) {
744 TargetType = GTI.getIndexedType();
745 // We assume that the cost of Scalar GEP with constant index and the
746 // cost of Vector GEP with splat constant index are the same.
747 const ConstantInt *ConstIdx = dyn_cast<ConstantInt>(*I);
748 if (!ConstIdx)
749 if (auto Splat = getSplatValue(*I))
750 ConstIdx = dyn_cast<ConstantInt>(Splat);
751 if (StructType *STy = GTI.getStructTypeOrNull()) {
752 // For structures the index is always splat or scalar constant
753 assert(ConstIdx && "Unexpected GEP index");
754 uint64_t Field = ConstIdx->getZExtValue();
755 BaseOffset += DL.getStructLayout(STy)->getElementOffset(Field);
756 } else {
757 int64_t ElementSize = DL.getTypeAllocSize(GTI.getIndexedType());
758 if (ConstIdx) {
759 BaseOffset +=
760 ConstIdx->getValue().sextOrTrunc(PtrSizeBits) * ElementSize;
761 } else {
762 // Needs scale register.
763 if (Scale != 0)
764 // No addressing mode takes two scale registers.
765 return TTI::TCC_Basic;
766 Scale = ElementSize;
771 if (static_cast<T *>(this)->isLegalAddressingMode(
772 TargetType, const_cast<GlobalValue *>(BaseGV),
773 BaseOffset.sextOrTrunc(64).getSExtValue(), HasBaseReg, Scale,
774 Ptr->getType()->getPointerAddressSpace()))
775 return TTI::TCC_Free;
776 return TTI::TCC_Basic;
779 unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
780 ArrayRef<Type *> ParamTys, const User *U) {
781 switch (IID) {
782 default:
783 // Intrinsics rarely (if ever) have normal argument setup constraints.
784 // Model them as having a basic instruction cost.
785 return TTI::TCC_Basic;
787 // TODO: other libc intrinsics.
788 case Intrinsic::memcpy:
789 return static_cast<T *>(this)->getMemcpyCost(dyn_cast<Instruction>(U));
791 case Intrinsic::annotation:
792 case Intrinsic::assume:
793 case Intrinsic::sideeffect:
794 case Intrinsic::dbg_declare:
795 case Intrinsic::dbg_value:
796 case Intrinsic::dbg_label:
797 case Intrinsic::invariant_start:
798 case Intrinsic::invariant_end:
799 case Intrinsic::launder_invariant_group:
800 case Intrinsic::strip_invariant_group:
801 case Intrinsic::is_constant:
802 case Intrinsic::lifetime_start:
803 case Intrinsic::lifetime_end:
804 case Intrinsic::objectsize:
805 case Intrinsic::ptr_annotation:
806 case Intrinsic::var_annotation:
807 case Intrinsic::experimental_gc_result:
808 case Intrinsic::experimental_gc_relocate:
809 case Intrinsic::coro_alloc:
810 case Intrinsic::coro_begin:
811 case Intrinsic::coro_free:
812 case Intrinsic::coro_end:
813 case Intrinsic::coro_frame:
814 case Intrinsic::coro_size:
815 case Intrinsic::coro_suspend:
816 case Intrinsic::coro_param:
817 case Intrinsic::coro_subfn_addr:
818 // These intrinsics don't actually represent code after lowering.
819 return TTI::TCC_Free;
823 unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
824 ArrayRef<const Value *> Arguments, const User *U) {
825 // Delegate to the generic intrinsic handling code. This mostly provides an
826 // opportunity for targets to (for example) special case the cost of
827 // certain intrinsics based on constants used as arguments.
828 SmallVector<Type *, 8> ParamTys;
829 ParamTys.reserve(Arguments.size());
830 for (unsigned Idx = 0, Size = Arguments.size(); Idx != Size; ++Idx)
831 ParamTys.push_back(Arguments[Idx]->getType());
832 return static_cast<T *>(this)->getIntrinsicCost(IID, RetTy, ParamTys, U);
835 unsigned getUserCost(const User *U, ArrayRef<const Value *> Operands) {
836 if (isa<PHINode>(U))
837 return TTI::TCC_Free; // Model all PHI nodes as free.
839 if (isa<ExtractValueInst>(U))
840 return TTI::TCC_Free; // Model all ExtractValue nodes as free.
842 // Static alloca doesn't generate target instructions.
843 if (auto *A = dyn_cast<AllocaInst>(U))
844 if (A->isStaticAlloca())
845 return TTI::TCC_Free;
847 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
848 return static_cast<T *>(this)->getGEPCost(GEP->getSourceElementType(),
849 GEP->getPointerOperand(),
850 Operands.drop_front());
853 if (auto CS = ImmutableCallSite(U)) {
854 const Function *F = CS.getCalledFunction();
855 if (!F) {
856 // Just use the called value type.
857 Type *FTy = CS.getCalledValue()->getType()->getPointerElementType();
858 return static_cast<T *>(this)
859 ->getCallCost(cast<FunctionType>(FTy), CS.arg_size(), U);
862 SmallVector<const Value *, 8> Arguments(CS.arg_begin(), CS.arg_end());
863 return static_cast<T *>(this)->getCallCost(F, Arguments, U);
866 if (isa<SExtInst>(U) || isa<ZExtInst>(U) || isa<FPExtInst>(U))
867 // The old behaviour of generally treating extensions of icmp to be free
868 // has been removed. A target that needs it should override getUserCost().
869 return static_cast<T *>(this)->getExtCost(cast<Instruction>(U),
870 Operands.back());
872 return static_cast<T *>(this)->getOperationCost(
873 Operator::getOpcode(U), U->getType(),
874 U->getNumOperands() == 1 ? U->getOperand(0)->getType() : nullptr);
877 int getInstructionLatency(const Instruction *I) {
878 SmallVector<const Value *, 4> Operands(I->value_op_begin(),
879 I->value_op_end());
880 if (getUserCost(I, Operands) == TTI::TCC_Free)
881 return 0;
883 if (isa<LoadInst>(I))
884 return 4;
886 Type *DstTy = I->getType();
888 // Usually an intrinsic is a simple instruction.
889 // A real function call is much slower.
890 if (auto *CI = dyn_cast<CallInst>(I)) {
891 const Function *F = CI->getCalledFunction();
892 if (!F || static_cast<T *>(this)->isLoweredToCall(F))
893 return 40;
894 // Some intrinsics return a value and a flag, we use the value type
895 // to decide its latency.
896 if (StructType* StructTy = dyn_cast<StructType>(DstTy))
897 DstTy = StructTy->getElementType(0);
898 // Fall through to simple instructions.
901 if (VectorType *VectorTy = dyn_cast<VectorType>(DstTy))
902 DstTy = VectorTy->getElementType();
903 if (DstTy->isFloatingPointTy())
904 return 3;
906 return 1;
911 #endif