1//===- TargetTransformInfoImpl.h --------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// This file provides helpers for the implementation of
11/// a TargetTransformInfo-conforming class.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_ANALYSIS_TARGETTRANSFORMINFOIMPL_H
16#define LLVM_ANALYSIS_TARGETTRANSFORMINFOIMPL_H
17
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"
27
28namespace llvm {
29
30/// \brief Base class for use as a mix-in that aids implementing
31/// a TargetTransformInfo-compatible class.
32class TargetTransformInfoImplBase {
33protected:
34  typedef TargetTransformInfo TTI;
35
36  const DataLayout &DL;
37
38  explicit TargetTransformInfoImplBase(const DataLayout &DL) : DL(DL) {}
39
40public:
41  // Provide value semantics. MSVC requires that we spell all of these out.
42  TargetTransformInfoImplBase(const TargetTransformInfoImplBase &Arg)
43      : DL(Arg.DL) {}
44  TargetTransformInfoImplBase(TargetTransformInfoImplBase &&Arg) : DL(Arg.DL) {}
45
46  const DataLayout &getDataLayout() const { return DL; }
47
48  unsigned getOperationCost(unsigned Opcode, Type *Ty, Type *OpTy) {
49    switch (Opcode) {
50    default:
51      // By default, just classify everything as 'basic'.
52      return TTI::TCC_Basic;
53
54    case Instruction::GetElementPtr:
55      llvm_unreachable("Use getGEPCost for GEP operations!");
56
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.
61        return TTI::TCC_Free;
62
63      // Otherwise, the default basic cost is used.
64      return TTI::TCC_Basic;
65
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;
73
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))
80        return TTI::TCC_Free;
81
82      // Otherwise it's not a no-op.
83      return TTI::TCC_Basic;
84    }
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))
91        return TTI::TCC_Free;
92
93      // Otherwise it's not a no-op.
94      return TTI::TCC_Basic;
95    }
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;
101
102      return TTI::TCC_Basic;
103    }
104  }
105
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;
113
114    return TTI::TCC_Free;
115  }
116
117  unsigned getEstimatedNumberOfCaseClusters(const SwitchInst &SI,
118                                            unsigned &JTSize) {
119    JTSize = 0;
120    return SI.getNumCases();
121  }
122
123  unsigned getCallCost(FunctionType *FTy, int NumArgs) {
124    assert(FTy && "FunctionType must be provided to this routine.");
125
126    // The target-independent implementation just measures the size of the
127    // function by approximating that each argument will take on average one
128    // instruction to prepare.
129
130    if (NumArgs < 0)
131      // Set the argument number to the number of explicit arguments in the
132      // function.
133      NumArgs = FTy->getNumParams();
134
135    return TTI::TCC_Basic * (NumArgs + 1);
136  }
137
138  unsigned getInliningThresholdMultiplier() { return 1; }
139
140  unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
141                            ArrayRef<Type *> ParamTys) {
142    switch (IID) {
143    default:
144      // Intrinsics rarely (if ever) have normal argument setup constraints.
145      // Model them as having a basic instruction cost.
146      // FIXME: This is wrong for libc intrinsics.
147      return TTI::TCC_Basic;
148
149    case Intrinsic::annotation:
150    case Intrinsic::assume:
151    case Intrinsic::dbg_declare:
152    case Intrinsic::dbg_value:
153    case Intrinsic::invariant_start:
154    case Intrinsic::invariant_end:
155    case Intrinsic::lifetime_start:
156    case Intrinsic::lifetime_end:
157    case Intrinsic::objectsize:
158    case Intrinsic::ptr_annotation:
159    case Intrinsic::var_annotation:
160    case Intrinsic::experimental_gc_result:
161    case Intrinsic::experimental_gc_relocate:
162    case Intrinsic::coro_alloc:
163    case Intrinsic::coro_begin:
164    case Intrinsic::coro_free:
165    case Intrinsic::coro_end:
166    case Intrinsic::coro_frame:
167    case Intrinsic::coro_size:
168    case Intrinsic::coro_suspend:
169    case Intrinsic::coro_param:
170    case Intrinsic::coro_subfn_addr:
171      // These intrinsics don't actually represent code after lowering.
172      return TTI::TCC_Free;
173    }
174  }
175
176  bool hasBranchDivergence() { return false; }
177
178  bool isSourceOfDivergence(const Value *V) { return false; }
179
180  bool isAlwaysUniform(const Value *V) { return false; }
181
182  unsigned getFlatAddressSpace () {
183    return -1;
184  }
185
186  bool isLoweredToCall(const Function *F) {
187    // FIXME: These should almost certainly not be handled here, and instead
188    // handled with the help of TLI or the target itself. This was largely
189    // ported from existing analysis heuristics here so that such refactorings
190    // can take place in the future.
191
192    if (F->isIntrinsic())
193      return false;
194
195    if (F->hasLocalLinkage() || !F->hasName())
196      return true;
197
198    StringRef Name = F->getName();
199
200    // These will all likely lower to a single selection DAG node.
201    if (Name == "copysign" || Name == "copysignf" || Name == "copysignl" ||
202        Name == "fabs" || Name == "fabsf" || Name == "fabsl" || Name == "sin" ||
203        Name == "fmin" || Name == "fminf" || Name == "fminl" ||
204        Name == "fmax" || Name == "fmaxf" || Name == "fmaxl" ||
205        Name == "sinf" || Name == "sinl" || Name == "cos" || Name == "cosf" ||
206        Name == "cosl" || Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl")
207      return false;
208
209    // These are all likely to be optimized into something smaller.
210    if (Name == "pow" || Name == "powf" || Name == "powl" || Name == "exp2" ||
211        Name == "exp2l" || Name == "exp2f" || Name == "floor" ||
212        Name == "floorf" || Name == "ceil" || Name == "round" ||
213        Name == "ffs" || Name == "ffsl" || Name == "abs" || Name == "labs" ||
214        Name == "llabs")
215      return false;
216
217    return true;
218  }
219
220  void getUnrollingPreferences(Loop *, TTI::UnrollingPreferences &) {}
221
222  bool isLegalAddImmediate(int64_t Imm) { return false; }
223
224  bool isLegalICmpImmediate(int64_t Imm) { return false; }
225
226  bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
227                             bool HasBaseReg, int64_t Scale,
228                             unsigned AddrSpace) {
229    // Guess that only reg and reg+reg addressing is allowed. This heuristic is
230    // taken from the implementation of LSR.
231    return !BaseGV && BaseOffset == 0 && (Scale == 0 || Scale == 1);
232  }
233
234  bool isLSRCostLess(TTI::LSRCost &C1, TTI::LSRCost &C2) {
235    return std::tie(C1.NumRegs, C1.AddRecCost, C1.NumIVMuls, C1.NumBaseAdds,
236                    C1.ScaleCost, C1.ImmCost, C1.SetupCost) <
237           std::tie(C2.NumRegs, C2.AddRecCost, C2.NumIVMuls, C2.NumBaseAdds,
238                    C2.ScaleCost, C2.ImmCost, C2.SetupCost);
239  }
240
241  bool isLegalMaskedStore(Type *DataType) { return false; }
242
243  bool isLegalMaskedLoad(Type *DataType) { return false; }
244
245  bool isLegalMaskedScatter(Type *DataType) { return false; }
246
247  bool isLegalMaskedGather(Type *DataType) { return false; }
248
249  bool prefersVectorizedAddressing() { return true; }
250
251  int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
252                           bool HasBaseReg, int64_t Scale, unsigned AddrSpace) {
253    // Guess that all legal addressing mode are free.
254    if (isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
255                              Scale, AddrSpace))
256      return 0;
257    return -1;
258  }
259
260  bool isFoldableMemAccessOffset(Instruction *I, int64_t Offset) { return true; }
261
262  bool isTruncateFree(Type *Ty1, Type *Ty2) { return false; }
263
264  bool isProfitableToHoist(Instruction *I) { return true; }
265
266  bool isTypeLegal(Type *Ty) { return false; }
267
268  unsigned getJumpBufAlignment() { return 0; }
269
270  unsigned getJumpBufSize() { return 0; }
271
272  bool shouldBuildLookupTables() { return true; }
273  bool shouldBuildLookupTablesForConstant(Constant *C) { return true; }
274
275  unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) {
276    return 0;
277  }
278
279  unsigned getOperandsScalarizationOverhead(ArrayRef<const Value *> Args,
280                                            unsigned VF) { return 0; }
281
282  bool supportsEfficientVectorElementLoadStore() { return false; }
283
284  bool enableAggressiveInterleaving(bool LoopHasReductions) { return false; }
285
286  bool expandMemCmp(Instruction *I, unsigned &MaxLoadSize) { return false; }
287
288  bool enableInterleavedAccessVectorization() { return false; }
289
290  bool isFPVectorizationPotentiallyUnsafe() { return false; }
291
292  bool allowsMisalignedMemoryAccesses(LLVMContext &Context,
293                                      unsigned BitWidth,
294                                      unsigned AddressSpace,
295                                      unsigned Alignment,
296                                      bool *Fast) { return false; }
297
298  TTI::PopcntSupportKind getPopcntSupport(unsigned IntTyWidthInBit) {
299    return TTI::PSK_Software;
300  }
301
302  bool haveFastSqrt(Type *Ty) { return false; }
303
304  unsigned getFPOpCost(Type *Ty) { return TargetTransformInfo::TCC_Basic; }
305
306  int getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
307                            Type *Ty) {
308    return 0;
309  }
310
311  unsigned getIntImmCost(const APInt &Imm, Type *Ty) { return TTI::TCC_Basic; }
312
313  unsigned getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
314                         Type *Ty) {
315    return TTI::TCC_Free;
316  }
317
318  unsigned getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
319                         Type *Ty) {
320    return TTI::TCC_Free;
321  }
322
323  unsigned getNumberOfRegisters(bool Vector) { return 8; }
324
325  unsigned getRegisterBitWidth(bool Vector) const { return 32; }
326
327  unsigned getMinVectorRegisterBitWidth() { return 128; }
328
329  bool
330  shouldConsiderAddressTypePromotion(const Instruction &I,
331                                     bool &AllowPromotionWithoutCommonHeader) {
332    AllowPromotionWithoutCommonHeader = false;
333    return false;
334  }
335
336  unsigned getCacheLineSize() { return 0; }
337
338  unsigned getPrefetchDistance() { return 0; }
339
340  unsigned getMinPrefetchStride() { return 1; }
341
342  unsigned getMaxPrefetchIterationsAhead() { return UINT_MAX; }
343
344  unsigned getMaxInterleaveFactor(unsigned VF) { return 1; }
345
346  unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty,
347                                  TTI::OperandValueKind Opd1Info,
348                                  TTI::OperandValueKind Opd2Info,
349                                  TTI::OperandValueProperties Opd1PropInfo,
350                                  TTI::OperandValueProperties Opd2PropInfo,
351                                  ArrayRef<const Value *> Args) {
352    return 1;
353  }
354
355  unsigned getShuffleCost(TTI::ShuffleKind Kind, Type *Ty, int Index,
356                          Type *SubTp) {
357    return 1;
358  }
359
360  unsigned getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src,
361                            const Instruction *I) { return 1; }
362
363  unsigned getExtractWithExtendCost(unsigned Opcode, Type *Dst,
364                                    VectorType *VecTy, unsigned Index) {
365    return 1;
366  }
367
368  unsigned getCFInstrCost(unsigned Opcode) { return 1; }
369
370  unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy,
371                              const Instruction *I) {
372    return 1;
373  }
374
375  unsigned getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) {
376    return 1;
377  }
378
379  unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
380                           unsigned AddressSpace, const Instruction *I) {
381    return 1;
382  }
383
384  unsigned getMaskedMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
385                                 unsigned AddressSpace) {
386    return 1;
387  }
388
389  unsigned getGatherScatterOpCost(unsigned Opcode, Type *DataTy, Value *Ptr,
390                                  bool VariableMask,
391                                  unsigned Alignment) {
392    return 1;
393  }
394
395  unsigned getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy,
396                                      unsigned Factor,
397                                      ArrayRef<unsigned> Indices,
398                                      unsigned Alignment,
399                                      unsigned AddressSpace) {
400    return 1;
401  }
402
403  unsigned getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
404                                 ArrayRef<Type *> Tys, FastMathFlags FMF,
405                                 unsigned ScalarizationCostPassed) {
406    return 1;
407  }
408  unsigned getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
409            ArrayRef<Value *> Args, FastMathFlags FMF, unsigned VF) {
410    return 1;
411  }
412
413  unsigned getCallInstrCost(Function *F, Type *RetTy, ArrayRef<Type *> Tys) {
414    return 1;
415  }
416
417  unsigned getNumberOfParts(Type *Tp) { return 0; }
418
419  unsigned getAddressComputationCost(Type *Tp, ScalarEvolution *,
420                                     const SCEV *) {
421    return 0;
422  }
423
424  unsigned getReductionCost(unsigned, Type *, bool) { return 1; }
425
426  unsigned getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) { return 0; }
427
428  bool getTgtMemIntrinsic(IntrinsicInst *Inst, MemIntrinsicInfo &Info) {
429    return false;
430  }
431
432  unsigned getAtomicMemIntrinsicMaxElementSize() const {
433    // Note for overrides: You must ensure for all element unordered-atomic
434    // memory intrinsics that all power-of-2 element sizes up to, and
435    // including, the return value of this method have a corresponding
436    // runtime lib call. These runtime lib call definitions can be found
437    // in RuntimeLibcalls.h
438    return 0;
439  }
440
441  Value *getOrCreateResultFromMemIntrinsic(IntrinsicInst *Inst,
442                                           Type *ExpectedType) {
443    return nullptr;
444  }
445
446  bool areInlineCompatible(const Function *Caller,
447                           const Function *Callee) const {
448    return (Caller->getFnAttribute("target-cpu") ==
449            Callee->getFnAttribute("target-cpu")) &&
450           (Caller->getFnAttribute("target-features") ==
451            Callee->getFnAttribute("target-features"));
452  }
453
454  unsigned getLoadStoreVecRegBitWidth(unsigned AddrSpace) const { return 128; }
455
456  bool isLegalToVectorizeLoad(LoadInst *LI) const { return true; }
457
458  bool isLegalToVectorizeStore(StoreInst *SI) const { return true; }
459
460  bool isLegalToVectorizeLoadChain(unsigned ChainSizeInBytes,
461                                   unsigned Alignment,
462                                   unsigned AddrSpace) const {
463    return true;
464  }
465
466  bool isLegalToVectorizeStoreChain(unsigned ChainSizeInBytes,
467                                    unsigned Alignment,
468                                    unsigned AddrSpace) const {
469    return true;
470  }
471
472  unsigned getLoadVectorFactor(unsigned VF, unsigned LoadSize,
473                               unsigned ChainSizeInBytes,
474                               VectorType *VecTy) const {
475    return VF;
476  }
477
478  unsigned getStoreVectorFactor(unsigned VF, unsigned StoreSize,
479                                unsigned ChainSizeInBytes,
480                                VectorType *VecTy) const {
481    return VF;
482  }
483
484  bool useReductionIntrinsic(unsigned Opcode, Type *Ty,
485                             TTI::ReductionFlags Flags) const {
486    return false;
487  }
488
489  bool shouldExpandReduction(const IntrinsicInst *II) const {
490    return true;
491  }
492
493protected:
494  // Obtain the minimum required size to hold the value (without the sign)
495  // In case of a vector it returns the min required size for one element.
496  unsigned minRequiredElementSize(const Value* Val, bool &isSigned) {
497    if (isa<ConstantDataVector>(Val) || isa<ConstantVector>(Val)) {
498      const auto* VectorValue = cast<Constant>(Val);
499
500      // In case of a vector need to pick the max between the min
501      // required size for each element
502      auto *VT = cast<VectorType>(Val->getType());
503
504      // Assume unsigned elements
505      isSigned = false;
506
507      // The max required size is the total vector width divided by num
508      // of elements in the vector
509      unsigned MaxRequiredSize = VT->getBitWidth() / VT->getNumElements();
510
511      unsigned MinRequiredSize = 0;
512      for(unsigned i = 0, e = VT->getNumElements(); i < e; ++i) {
513        if (auto* IntElement =
514              dyn_cast<ConstantInt>(VectorValue->getAggregateElement(i))) {
515          bool signedElement = IntElement->getValue().isNegative();
516          // Get the element min required size.
517          unsigned ElementMinRequiredSize =
518            IntElement->getValue().getMinSignedBits() - 1;
519          // In case one element is signed then all the vector is signed.
520          isSigned |= signedElement;
521          // Save the max required bit size between all the elements.
522          MinRequiredSize = std::max(MinRequiredSize, ElementMinRequiredSize);
523        }
524        else {
525          // not an int constant element
526          return MaxRequiredSize;
527        }
528      }
529      return MinRequiredSize;
530    }
531
532    if (const auto* CI = dyn_cast<ConstantInt>(Val)) {
533      isSigned = CI->getValue().isNegative();
534      return CI->getValue().getMinSignedBits() - 1;
535    }
536
537    if (const auto* Cast = dyn_cast<SExtInst>(Val)) {
538      isSigned = true;
539      return Cast->getSrcTy()->getScalarSizeInBits() - 1;
540    }
541
542    if (const auto* Cast = dyn_cast<ZExtInst>(Val)) {
543      isSigned = false;
544      return Cast->getSrcTy()->getScalarSizeInBits();
545    }
546
547    isSigned = false;
548    return Val->getType()->getScalarSizeInBits();
549  }
550
551  bool isStridedAccess(const SCEV *Ptr) {
552    return Ptr && isa<SCEVAddRecExpr>(Ptr);
553  }
554
555  const SCEVConstant *getConstantStrideStep(ScalarEvolution *SE,
556                                            const SCEV *Ptr) {
557    if (!isStridedAccess(Ptr))
558      return nullptr;
559    const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ptr);
560    return dyn_cast<SCEVConstant>(AddRec->getStepRecurrence(*SE));
561  }
562
563  bool isConstantStridedAccessLessThan(ScalarEvolution *SE, const SCEV *Ptr,
564                                       int64_t MergeDistance) {
565    const SCEVConstant *Step = getConstantStrideStep(SE, Ptr);
566    if (!Step)
567      return false;
568    APInt StrideVal = Step->getAPInt();
569    if (StrideVal.getBitWidth() > 64)
570      return false;
571    // FIXME: need to take absolute value for negtive stride case
572    return StrideVal.getSExtValue() < MergeDistance;
573  }
574};
575
576/// \brief CRTP base class for use as a mix-in that aids implementing
577/// a TargetTransformInfo-compatible class.
578template <typename T>
579class TargetTransformInfoImplCRTPBase : public TargetTransformInfoImplBase {
580private:
581  typedef TargetTransformInfoImplBase BaseT;
582
583protected:
584  explicit TargetTransformInfoImplCRTPBase(const DataLayout &DL) : BaseT(DL) {}
585
586public:
587  using BaseT::getCallCost;
588
589  unsigned getCallCost(const Function *F, int NumArgs) {
590    assert(F && "A concrete function must be provided to this routine.");
591
592    if (NumArgs < 0)
593      // Set the argument number to the number of explicit arguments in the
594      // function.
595      NumArgs = F->arg_size();
596
597    if (Intrinsic::ID IID = F->getIntrinsicID()) {
598      FunctionType *FTy = F->getFunctionType();
599      SmallVector<Type *, 8> ParamTys(FTy->param_begin(), FTy->param_end());
600      return static_cast<T *>(this)
601          ->getIntrinsicCost(IID, FTy->getReturnType(), ParamTys);
602    }
603
604    if (!static_cast<T *>(this)->isLoweredToCall(F))
605      return TTI::TCC_Basic; // Give a basic cost if it will be lowered
606                             // directly.
607
608    return static_cast<T *>(this)->getCallCost(F->getFunctionType(), NumArgs);
609  }
610
611  unsigned getCallCost(const Function *F, ArrayRef<const Value *> Arguments) {
612    // Simply delegate to generic handling of the call.
613    // FIXME: We should use instsimplify or something else to catch calls which
614    // will constant fold with these arguments.
615    return static_cast<T *>(this)->getCallCost(F, Arguments.size());
616  }
617
618  using BaseT::getGEPCost;
619
620  int getGEPCost(Type *PointeeType, const Value *Ptr,
621                 ArrayRef<const Value *> Operands) {
622    const GlobalValue *BaseGV = nullptr;
623    if (Ptr != nullptr) {
624      // TODO: will remove this when pointers have an opaque type.
625      assert(Ptr->getType()->getScalarType()->getPointerElementType() ==
626                 PointeeType &&
627             "explicit pointee type doesn't match operand's pointee type");
628      BaseGV = dyn_cast<GlobalValue>(Ptr->stripPointerCasts());
629    }
630    bool HasBaseReg = (BaseGV == nullptr);
631    int64_t BaseOffset = 0;
632    int64_t Scale = 0;
633
634    auto GTI = gep_type_begin(PointeeType, Operands);
635    Type *TargetType;
636    for (auto I = Operands.begin(); I != Operands.end(); ++I, ++GTI) {
637      TargetType = GTI.getIndexedType();
638      // We assume that the cost of Scalar GEP with constant index and the
639      // cost of Vector GEP with splat constant index are the same.
640      const ConstantInt *ConstIdx = dyn_cast<ConstantInt>(*I);
641      if (!ConstIdx)
642        if (auto Splat = getSplatValue(*I))
643          ConstIdx = dyn_cast<ConstantInt>(Splat);
644      if (StructType *STy = GTI.getStructTypeOrNull()) {
645        // For structures the index is always splat or scalar constant
646        assert(ConstIdx && "Unexpected GEP index");
647        uint64_t Field = ConstIdx->getZExtValue();
648        BaseOffset += DL.getStructLayout(STy)->getElementOffset(Field);
649      } else {
650        int64_t ElementSize = DL.getTypeAllocSize(GTI.getIndexedType());
651        if (ConstIdx)
652          BaseOffset += ConstIdx->getSExtValue() * ElementSize;
653        else {
654          // Needs scale register.
655          if (Scale != 0)
656            // No addressing mode takes two scale registers.
657            return TTI::TCC_Basic;
658          Scale = ElementSize;
659        }
660      }
661    }
662
663    // Assumes the address space is 0 when Ptr is nullptr.
664    unsigned AS =
665        (Ptr == nullptr ? 0 : Ptr->getType()->getPointerAddressSpace());
666    if (static_cast<T *>(this)->isLegalAddressingMode(
667            TargetType, const_cast<GlobalValue *>(BaseGV), BaseOffset,
668            HasBaseReg, Scale, AS))
669      return TTI::TCC_Free;
670    return TTI::TCC_Basic;
671  }
672
673  using BaseT::getIntrinsicCost;
674
675  unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
676                            ArrayRef<const Value *> Arguments) {
677    // Delegate to the generic intrinsic handling code. This mostly provides an
678    // opportunity for targets to (for example) special case the cost of
679    // certain intrinsics based on constants used as arguments.
680    SmallVector<Type *, 8> ParamTys;
681    ParamTys.reserve(Arguments.size());
682    for (unsigned Idx = 0, Size = Arguments.size(); Idx != Size; ++Idx)
683      ParamTys.push_back(Arguments[Idx]->getType());
684    return static_cast<T *>(this)->getIntrinsicCost(IID, RetTy, ParamTys);
685  }
686
687  unsigned getUserCost(const User *U) {
688    if (isa<PHINode>(U))
689      return TTI::TCC_Free; // Model all PHI nodes as free.
690
691    if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
692      SmallVector<Value *, 4> Indices(GEP->idx_begin(), GEP->idx_end());
693      return static_cast<T *>(this)->getGEPCost(
694          GEP->getSourceElementType(), GEP->getPointerOperand(), Indices);
695    }
696
697    if (auto CS = ImmutableCallSite(U)) {
698      const Function *F = CS.getCalledFunction();
699      if (!F) {
700        // Just use the called value type.
701        Type *FTy = CS.getCalledValue()->getType()->getPointerElementType();
702        return static_cast<T *>(this)
703            ->getCallCost(cast<FunctionType>(FTy), CS.arg_size());
704      }
705
706      SmallVector<const Value *, 8> Arguments(CS.arg_begin(), CS.arg_end());
707      return static_cast<T *>(this)->getCallCost(F, Arguments);
708    }
709
710    if (const CastInst *CI = dyn_cast<CastInst>(U)) {
711      // Result of a cmp instruction is often extended (to be used by other
712      // cmp instructions, logical or return instructions). These are usually
713      // nop on most sane targets.
714      if (isa<CmpInst>(CI->getOperand(0)))
715        return TTI::TCC_Free;
716    }
717
718    return static_cast<T *>(this)->getOperationCost(
719        Operator::getOpcode(U), U->getType(),
720        U->getNumOperands() == 1 ? U->getOperand(0)->getType() : nullptr);
721  }
722};
723}
724
725#endif
726