1 //===- TargetTransformInfoImpl.h --------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This file provides helpers for the implementation of
11 /// a TargetTransformInfo-conforming class.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ANALYSIS_TARGETTRANSFORMINFOIMPL_H
16 #define LLVM_ANALYSIS_TARGETTRANSFORMINFOIMPL_H
18 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
19 #include "llvm/Analysis/TargetTransformInfo.h"
20 #include "llvm/Analysis/VectorUtils.h"
21 #include "llvm/IR/CallSite.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/GetElementPtrTypeIterator.h"
25 #include "llvm/IR/Operator.h"
26 #include "llvm/IR/Type.h"
30 /// \brief Base class for use as a mix-in that aids implementing
31 /// a TargetTransformInfo-compatible class.
32 class TargetTransformInfoImplBase {
34 typedef TargetTransformInfo TTI;
38 explicit TargetTransformInfoImplBase(const DataLayout &DL) : DL(DL) {}
41 // Provide value semantics. MSVC requires that we spell all of these out.
42 TargetTransformInfoImplBase(const TargetTransformInfoImplBase &Arg)
44 TargetTransformInfoImplBase(TargetTransformInfoImplBase &&Arg) : DL(Arg.DL) {}
46 const DataLayout &getDataLayout() const { return DL; }
48 unsigned getOperationCost(unsigned Opcode, Type *Ty, Type *OpTy) {
51 // By default, just classify everything as 'basic'.
52 return TTI::TCC_Basic;
54 case Instruction::GetElementPtr:
55 llvm_unreachable("Use getGEPCost for GEP operations!");
57 case Instruction::BitCast:
58 assert(OpTy && "Cast instructions must provide the operand type");
59 if (Ty == OpTy || (Ty->isPointerTy() && OpTy->isPointerTy()))
60 // Identity and pointer-to-pointer casts are free.
63 // Otherwise, the default basic cost is used.
64 return TTI::TCC_Basic;
66 case Instruction::FDiv:
67 case Instruction::FRem:
68 case Instruction::SDiv:
69 case Instruction::SRem:
70 case Instruction::UDiv:
71 case Instruction::URem:
72 return TTI::TCC_Expensive;
74 case Instruction::IntToPtr: {
75 // An inttoptr cast is free so long as the input is a legal integer type
76 // which doesn't contain values outside the range of a pointer.
77 unsigned OpSize = OpTy->getScalarSizeInBits();
78 if (DL.isLegalInteger(OpSize) &&
79 OpSize <= DL.getPointerTypeSizeInBits(Ty))
82 // Otherwise it's not a no-op.
83 return TTI::TCC_Basic;
85 case Instruction::PtrToInt: {
86 // A ptrtoint cast is free so long as the result is large enough to store
87 // the pointer, and a legal integer type.
88 unsigned DestSize = Ty->getScalarSizeInBits();
89 if (DL.isLegalInteger(DestSize) &&
90 DestSize >= DL.getPointerTypeSizeInBits(OpTy))
93 // Otherwise it's not a no-op.
94 return TTI::TCC_Basic;
96 case Instruction::Trunc:
97 // trunc to a native type is free (assuming the target has compare and
98 // shift-right of the same width).
99 if (DL.isLegalInteger(DL.getTypeSizeInBits(Ty)))
100 return TTI::TCC_Free;
102 return TTI::TCC_Basic;
106 int getGEPCost(Type *PointeeType, const Value *Ptr,
107 ArrayRef<const Value *> Operands) {
108 // In the basic model, we just assume that all-constant GEPs will be folded
109 // into their uses via addressing modes.
110 for (unsigned Idx = 0, Size = Operands.size(); Idx != Size; ++Idx)
111 if (!isa<Constant>(Operands[Idx]))
112 return TTI::TCC_Basic;
114 return TTI::TCC_Free;
117 unsigned getEstimatedNumberOfCaseClusters(const SwitchInst &SI,
120 return SI.getNumCases();
123 int getExtCost(const Instruction *I, const Value *Src) {
124 return TTI::TCC_Basic;
127 unsigned getCallCost(FunctionType *FTy, int NumArgs) {
128 assert(FTy && "FunctionType must be provided to this routine.");
130 // The target-independent implementation just measures the size of the
131 // function by approximating that each argument will take on average one
132 // instruction to prepare.
135 // Set the argument number to the number of explicit arguments in the
137 NumArgs = FTy->getNumParams();
139 return TTI::TCC_Basic * (NumArgs + 1);
142 unsigned getInliningThresholdMultiplier() { return 1; }
144 unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
145 ArrayRef<Type *> ParamTys) {
148 // Intrinsics rarely (if ever) have normal argument setup constraints.
149 // Model them as having a basic instruction cost.
150 // FIXME: This is wrong for libc intrinsics.
151 return TTI::TCC_Basic;
153 case Intrinsic::annotation:
154 case Intrinsic::assume:
155 case Intrinsic::dbg_declare:
156 case Intrinsic::dbg_value:
157 case Intrinsic::invariant_start:
158 case Intrinsic::invariant_end:
159 case Intrinsic::lifetime_start:
160 case Intrinsic::lifetime_end:
161 case Intrinsic::objectsize:
162 case Intrinsic::ptr_annotation:
163 case Intrinsic::var_annotation:
164 case Intrinsic::experimental_gc_result:
165 case Intrinsic::experimental_gc_relocate:
166 case Intrinsic::coro_alloc:
167 case Intrinsic::coro_begin:
168 case Intrinsic::coro_free:
169 case Intrinsic::coro_end:
170 case Intrinsic::coro_frame:
171 case Intrinsic::coro_size:
172 case Intrinsic::coro_suspend:
173 case Intrinsic::coro_param:
174 case Intrinsic::coro_subfn_addr:
175 // These intrinsics don't actually represent code after lowering.
176 return TTI::TCC_Free;
180 bool hasBranchDivergence() { return false; }
182 bool isSourceOfDivergence(const Value *V) { return false; }
184 bool isAlwaysUniform(const Value *V) { return false; }
186 unsigned getFlatAddressSpace () {
190 bool isLoweredToCall(const Function *F) {
191 // FIXME: These should almost certainly not be handled here, and instead
192 // handled with the help of TLI or the target itself. This was largely
193 // ported from existing analysis heuristics here so that such refactorings
194 // can take place in the future.
196 if (F->isIntrinsic())
199 if (F->hasLocalLinkage() || !F->hasName())
202 StringRef Name = F->getName();
204 // These will all likely lower to a single selection DAG node.
205 if (Name == "copysign" || Name == "copysignf" || Name == "copysignl" ||
206 Name == "fabs" || Name == "fabsf" || Name == "fabsl" || Name == "sin" ||
207 Name == "fmin" || Name == "fminf" || Name == "fminl" ||
208 Name == "fmax" || Name == "fmaxf" || Name == "fmaxl" ||
209 Name == "sinf" || Name == "sinl" || Name == "cos" || Name == "cosf" ||
210 Name == "cosl" || Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl")
213 // These are all likely to be optimized into something smaller.
214 if (Name == "pow" || Name == "powf" || Name == "powl" || Name == "exp2" ||
215 Name == "exp2l" || Name == "exp2f" || Name == "floor" ||
216 Name == "floorf" || Name == "ceil" || Name == "round" ||
217 Name == "ffs" || Name == "ffsl" || Name == "abs" || Name == "labs" ||
224 void getUnrollingPreferences(Loop *, ScalarEvolution &,
225 TTI::UnrollingPreferences &) {}
227 bool isLegalAddImmediate(int64_t Imm) { return false; }
229 bool isLegalICmpImmediate(int64_t Imm) { return false; }
231 bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
232 bool HasBaseReg, int64_t Scale,
233 unsigned AddrSpace) {
234 // Guess that only reg and reg+reg addressing is allowed. This heuristic is
235 // taken from the implementation of LSR.
236 return !BaseGV && BaseOffset == 0 && (Scale == 0 || Scale == 1);
239 bool isLSRCostLess(TTI::LSRCost &C1, TTI::LSRCost &C2) {
240 return std::tie(C1.NumRegs, C1.AddRecCost, C1.NumIVMuls, C1.NumBaseAdds,
241 C1.ScaleCost, C1.ImmCost, C1.SetupCost) <
242 std::tie(C2.NumRegs, C2.AddRecCost, C2.NumIVMuls, C2.NumBaseAdds,
243 C2.ScaleCost, C2.ImmCost, C2.SetupCost);
246 bool isLegalMaskedStore(Type *DataType) { return false; }
248 bool isLegalMaskedLoad(Type *DataType) { return false; }
250 bool isLegalMaskedScatter(Type *DataType) { return false; }
252 bool isLegalMaskedGather(Type *DataType) { return false; }
254 bool prefersVectorizedAddressing() { return true; }
256 int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
257 bool HasBaseReg, int64_t Scale, unsigned AddrSpace) {
258 // Guess that all legal addressing mode are free.
259 if (isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
265 bool isFoldableMemAccessOffset(Instruction *I, int64_t Offset) { return true; }
267 bool isTruncateFree(Type *Ty1, Type *Ty2) { return false; }
269 bool isProfitableToHoist(Instruction *I) { return true; }
271 bool isTypeLegal(Type *Ty) { return false; }
273 unsigned getJumpBufAlignment() { return 0; }
275 unsigned getJumpBufSize() { return 0; }
277 bool shouldBuildLookupTables() { return true; }
278 bool shouldBuildLookupTablesForConstant(Constant *C) { return true; }
280 unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) {
284 unsigned getOperandsScalarizationOverhead(ArrayRef<const Value *> Args,
285 unsigned VF) { return 0; }
287 bool supportsEfficientVectorElementLoadStore() { return false; }
289 bool enableAggressiveInterleaving(bool LoopHasReductions) { return false; }
291 bool expandMemCmp(Instruction *I, unsigned &MaxLoadSize) { return false; }
293 bool enableInterleavedAccessVectorization() { return false; }
295 bool isFPVectorizationPotentiallyUnsafe() { return false; }
297 bool allowsMisalignedMemoryAccesses(LLVMContext &Context,
299 unsigned AddressSpace,
301 bool *Fast) { return false; }
303 TTI::PopcntSupportKind getPopcntSupport(unsigned IntTyWidthInBit) {
304 return TTI::PSK_Software;
307 bool haveFastSqrt(Type *Ty) { return false; }
309 unsigned getFPOpCost(Type *Ty) { return TargetTransformInfo::TCC_Basic; }
311 int getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
316 unsigned getIntImmCost(const APInt &Imm, Type *Ty) { return TTI::TCC_Basic; }
318 unsigned getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
320 return TTI::TCC_Free;
323 unsigned getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
325 return TTI::TCC_Free;
328 unsigned getNumberOfRegisters(bool Vector) { return 8; }
330 unsigned getRegisterBitWidth(bool Vector) const { return 32; }
332 unsigned getMinVectorRegisterBitWidth() { return 128; }
335 shouldConsiderAddressTypePromotion(const Instruction &I,
336 bool &AllowPromotionWithoutCommonHeader) {
337 AllowPromotionWithoutCommonHeader = false;
341 unsigned getCacheLineSize() { return 0; }
343 unsigned getPrefetchDistance() { return 0; }
345 unsigned getMinPrefetchStride() { return 1; }
347 unsigned getMaxPrefetchIterationsAhead() { return UINT_MAX; }
349 unsigned getMaxInterleaveFactor(unsigned VF) { return 1; }
351 unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty,
352 TTI::OperandValueKind Opd1Info,
353 TTI::OperandValueKind Opd2Info,
354 TTI::OperandValueProperties Opd1PropInfo,
355 TTI::OperandValueProperties Opd2PropInfo,
356 ArrayRef<const Value *> Args) {
360 unsigned getShuffleCost(TTI::ShuffleKind Kind, Type *Ty, int Index,
365 unsigned getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src,
366 const Instruction *I) { return 1; }
368 unsigned getExtractWithExtendCost(unsigned Opcode, Type *Dst,
369 VectorType *VecTy, unsigned Index) {
373 unsigned getCFInstrCost(unsigned Opcode) { return 1; }
375 unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy,
376 const Instruction *I) {
380 unsigned getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) {
384 unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
385 unsigned AddressSpace, const Instruction *I) {
389 unsigned getMaskedMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
390 unsigned AddressSpace) {
394 unsigned getGatherScatterOpCost(unsigned Opcode, Type *DataTy, Value *Ptr,
396 unsigned Alignment) {
400 unsigned getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy,
402 ArrayRef<unsigned> Indices,
404 unsigned AddressSpace) {
408 unsigned getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
409 ArrayRef<Type *> Tys, FastMathFlags FMF,
410 unsigned ScalarizationCostPassed) {
413 unsigned getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
414 ArrayRef<Value *> Args, FastMathFlags FMF, unsigned VF) {
418 unsigned getCallInstrCost(Function *F, Type *RetTy, ArrayRef<Type *> Tys) {
422 unsigned getNumberOfParts(Type *Tp) { return 0; }
424 unsigned getAddressComputationCost(Type *Tp, ScalarEvolution *,
429 unsigned getReductionCost(unsigned, Type *, bool) { return 1; }
431 unsigned getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) { return 0; }
433 bool getTgtMemIntrinsic(IntrinsicInst *Inst, MemIntrinsicInfo &Info) {
437 unsigned getAtomicMemIntrinsicMaxElementSize() const {
438 // Note for overrides: You must ensure for all element unordered-atomic
439 // memory intrinsics that all power-of-2 element sizes up to, and
440 // including, the return value of this method have a corresponding
441 // runtime lib call. These runtime lib call definitions can be found
442 // in RuntimeLibcalls.h
446 Value *getOrCreateResultFromMemIntrinsic(IntrinsicInst *Inst,
447 Type *ExpectedType) {
451 Type *getMemcpyLoopLoweringType(LLVMContext &Context, Value *Length,
452 unsigned SrcAlign, unsigned DestAlign) const {
453 return Type::getInt8Ty(Context);
456 void getMemcpyLoopResidualLoweringType(SmallVectorImpl<Type *> &OpsOut,
457 LLVMContext &Context,
458 unsigned RemainingBytes,
460 unsigned DestAlign) const {
461 for (unsigned i = 0; i != RemainingBytes; ++i)
462 OpsOut.push_back(Type::getInt8Ty(Context));
465 bool areInlineCompatible(const Function *Caller,
466 const Function *Callee) const {
467 return (Caller->getFnAttribute("target-cpu") ==
468 Callee->getFnAttribute("target-cpu")) &&
469 (Caller->getFnAttribute("target-features") ==
470 Callee->getFnAttribute("target-features"));
473 unsigned getLoadStoreVecRegBitWidth(unsigned AddrSpace) const { return 128; }
475 bool isLegalToVectorizeLoad(LoadInst *LI) const { return true; }
477 bool isLegalToVectorizeStore(StoreInst *SI) const { return true; }
479 bool isLegalToVectorizeLoadChain(unsigned ChainSizeInBytes,
481 unsigned AddrSpace) const {
485 bool isLegalToVectorizeStoreChain(unsigned ChainSizeInBytes,
487 unsigned AddrSpace) const {
491 unsigned getLoadVectorFactor(unsigned VF, unsigned LoadSize,
492 unsigned ChainSizeInBytes,
493 VectorType *VecTy) const {
497 unsigned getStoreVectorFactor(unsigned VF, unsigned StoreSize,
498 unsigned ChainSizeInBytes,
499 VectorType *VecTy) const {
503 bool useReductionIntrinsic(unsigned Opcode, Type *Ty,
504 TTI::ReductionFlags Flags) const {
508 bool shouldExpandReduction(const IntrinsicInst *II) const {
513 // Obtain the minimum required size to hold the value (without the sign)
514 // In case of a vector it returns the min required size for one element.
515 unsigned minRequiredElementSize(const Value* Val, bool &isSigned) {
516 if (isa<ConstantDataVector>(Val) || isa<ConstantVector>(Val)) {
517 const auto* VectorValue = cast<Constant>(Val);
519 // In case of a vector need to pick the max between the min
520 // required size for each element
521 auto *VT = cast<VectorType>(Val->getType());
523 // Assume unsigned elements
526 // The max required size is the total vector width divided by num
527 // of elements in the vector
528 unsigned MaxRequiredSize = VT->getBitWidth() / VT->getNumElements();
530 unsigned MinRequiredSize = 0;
531 for(unsigned i = 0, e = VT->getNumElements(); i < e; ++i) {
532 if (auto* IntElement =
533 dyn_cast<ConstantInt>(VectorValue->getAggregateElement(i))) {
534 bool signedElement = IntElement->getValue().isNegative();
535 // Get the element min required size.
536 unsigned ElementMinRequiredSize =
537 IntElement->getValue().getMinSignedBits() - 1;
538 // In case one element is signed then all the vector is signed.
539 isSigned |= signedElement;
540 // Save the max required bit size between all the elements.
541 MinRequiredSize = std::max(MinRequiredSize, ElementMinRequiredSize);
544 // not an int constant element
545 return MaxRequiredSize;
548 return MinRequiredSize;
551 if (const auto* CI = dyn_cast<ConstantInt>(Val)) {
552 isSigned = CI->getValue().isNegative();
553 return CI->getValue().getMinSignedBits() - 1;
556 if (const auto* Cast = dyn_cast<SExtInst>(Val)) {
558 return Cast->getSrcTy()->getScalarSizeInBits() - 1;
561 if (const auto* Cast = dyn_cast<ZExtInst>(Val)) {
563 return Cast->getSrcTy()->getScalarSizeInBits();
567 return Val->getType()->getScalarSizeInBits();
570 bool isStridedAccess(const SCEV *Ptr) {
571 return Ptr && isa<SCEVAddRecExpr>(Ptr);
574 const SCEVConstant *getConstantStrideStep(ScalarEvolution *SE,
576 if (!isStridedAccess(Ptr))
578 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ptr);
579 return dyn_cast<SCEVConstant>(AddRec->getStepRecurrence(*SE));
582 bool isConstantStridedAccessLessThan(ScalarEvolution *SE, const SCEV *Ptr,
583 int64_t MergeDistance) {
584 const SCEVConstant *Step = getConstantStrideStep(SE, Ptr);
587 APInt StrideVal = Step->getAPInt();
588 if (StrideVal.getBitWidth() > 64)
590 // FIXME: need to take absolute value for negtive stride case
591 return StrideVal.getSExtValue() < MergeDistance;
595 /// \brief CRTP base class for use as a mix-in that aids implementing
596 /// a TargetTransformInfo-compatible class.
597 template <typename T>
598 class TargetTransformInfoImplCRTPBase : public TargetTransformInfoImplBase {
600 typedef TargetTransformInfoImplBase BaseT;
603 explicit TargetTransformInfoImplCRTPBase(const DataLayout &DL) : BaseT(DL) {}
606 using BaseT::getCallCost;
608 unsigned getCallCost(const Function *F, int NumArgs) {
609 assert(F && "A concrete function must be provided to this routine.");
612 // Set the argument number to the number of explicit arguments in the
614 NumArgs = F->arg_size();
616 if (Intrinsic::ID IID = F->getIntrinsicID()) {
617 FunctionType *FTy = F->getFunctionType();
618 SmallVector<Type *, 8> ParamTys(FTy->param_begin(), FTy->param_end());
619 return static_cast<T *>(this)
620 ->getIntrinsicCost(IID, FTy->getReturnType(), ParamTys);
623 if (!static_cast<T *>(this)->isLoweredToCall(F))
624 return TTI::TCC_Basic; // Give a basic cost if it will be lowered
627 return static_cast<T *>(this)->getCallCost(F->getFunctionType(), NumArgs);
630 unsigned getCallCost(const Function *F, ArrayRef<const Value *> Arguments) {
631 // Simply delegate to generic handling of the call.
632 // FIXME: We should use instsimplify or something else to catch calls which
633 // will constant fold with these arguments.
634 return static_cast<T *>(this)->getCallCost(F, Arguments.size());
637 using BaseT::getGEPCost;
639 int getGEPCost(Type *PointeeType, const Value *Ptr,
640 ArrayRef<const Value *> Operands) {
641 const GlobalValue *BaseGV = nullptr;
642 if (Ptr != nullptr) {
643 // TODO: will remove this when pointers have an opaque type.
644 assert(Ptr->getType()->getScalarType()->getPointerElementType() ==
646 "explicit pointee type doesn't match operand's pointee type");
647 BaseGV = dyn_cast<GlobalValue>(Ptr->stripPointerCasts());
649 bool HasBaseReg = (BaseGV == nullptr);
650 int64_t BaseOffset = 0;
653 auto GTI = gep_type_begin(PointeeType, Operands);
655 for (auto I = Operands.begin(); I != Operands.end(); ++I, ++GTI) {
656 TargetType = GTI.getIndexedType();
657 // We assume that the cost of Scalar GEP with constant index and the
658 // cost of Vector GEP with splat constant index are the same.
659 const ConstantInt *ConstIdx = dyn_cast<ConstantInt>(*I);
661 if (auto Splat = getSplatValue(*I))
662 ConstIdx = dyn_cast<ConstantInt>(Splat);
663 if (StructType *STy = GTI.getStructTypeOrNull()) {
664 // For structures the index is always splat or scalar constant
665 assert(ConstIdx && "Unexpected GEP index");
666 uint64_t Field = ConstIdx->getZExtValue();
667 BaseOffset += DL.getStructLayout(STy)->getElementOffset(Field);
669 int64_t ElementSize = DL.getTypeAllocSize(GTI.getIndexedType());
671 BaseOffset += ConstIdx->getSExtValue() * ElementSize;
673 // Needs scale register.
675 // No addressing mode takes two scale registers.
676 return TTI::TCC_Basic;
682 // Assumes the address space is 0 when Ptr is nullptr.
684 (Ptr == nullptr ? 0 : Ptr->getType()->getPointerAddressSpace());
685 if (static_cast<T *>(this)->isLegalAddressingMode(
686 TargetType, const_cast<GlobalValue *>(BaseGV), BaseOffset,
687 HasBaseReg, Scale, AS))
688 return TTI::TCC_Free;
689 return TTI::TCC_Basic;
692 using BaseT::getIntrinsicCost;
694 unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
695 ArrayRef<const Value *> Arguments) {
696 // Delegate to the generic intrinsic handling code. This mostly provides an
697 // opportunity for targets to (for example) special case the cost of
698 // certain intrinsics based on constants used as arguments.
699 SmallVector<Type *, 8> ParamTys;
700 ParamTys.reserve(Arguments.size());
701 for (unsigned Idx = 0, Size = Arguments.size(); Idx != Size; ++Idx)
702 ParamTys.push_back(Arguments[Idx]->getType());
703 return static_cast<T *>(this)->getIntrinsicCost(IID, RetTy, ParamTys);
706 unsigned getUserCost(const User *U, ArrayRef<const Value *> Operands) {
708 return TTI::TCC_Free; // Model all PHI nodes as free.
710 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
711 return static_cast<T *>(this)->getGEPCost(GEP->getSourceElementType(),
712 GEP->getPointerOperand(),
713 Operands.drop_front());
716 if (auto CS = ImmutableCallSite(U)) {
717 const Function *F = CS.getCalledFunction();
719 // Just use the called value type.
720 Type *FTy = CS.getCalledValue()->getType()->getPointerElementType();
721 return static_cast<T *>(this)
722 ->getCallCost(cast<FunctionType>(FTy), CS.arg_size());
725 SmallVector<const Value *, 8> Arguments(CS.arg_begin(), CS.arg_end());
726 return static_cast<T *>(this)->getCallCost(F, Arguments);
729 if (const CastInst *CI = dyn_cast<CastInst>(U)) {
730 // Result of a cmp instruction is often extended (to be used by other
731 // cmp instructions, logical or return instructions). These are usually
732 // nop on most sane targets.
733 if (isa<CmpInst>(CI->getOperand(0)))
734 return TTI::TCC_Free;
735 if (isa<SExtInst>(CI) || isa<ZExtInst>(CI) || isa<FPExtInst>(CI))
736 return static_cast<T *>(this)->getExtCost(CI, Operands.back());
739 return static_cast<T *>(this)->getOperationCost(
740 Operator::getOpcode(U), U->getType(),
741 U->getNumOperands() == 1 ? U->getOperand(0)->getType() : nullptr);