TargetTransformInfoImpl.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. #ifndef LLVM_ANALYSIS_TARGETTRANSFORMINFOIMPL_H
  15. #define LLVM_ANALYSIS_TARGETTRANSFORMINFOIMPL_H
  16. #include "llvm/Analysis/TargetTransformInfo.h"
  17. #include "llvm/IR/CallSite.h"
  18. #include "llvm/IR/DataLayout.h"
  19. #include "llvm/IR/Function.h"
  20. #include "llvm/IR/Operator.h"
  21. #include "llvm/IR/Type.h"
  22. namespace llvm {
  23. /// \brief Base class for use as a mix-in that aids implementing
  24. /// a TargetTransformInfo-compatible class.
  25. class TargetTransformInfoImplBase {
  26. protected:
  27. typedef TargetTransformInfo TTI;
  28. const DataLayout &DL;
  29. explicit TargetTransformInfoImplBase(const DataLayout &DL) : DL(DL) {}
  30. public:
  31. // Provide value semantics. MSVC requires that we spell all of these out.
  32. TargetTransformInfoImplBase(const TargetTransformInfoImplBase &Arg)
  33. : DL(Arg.DL) {}
  34. TargetTransformInfoImplBase(TargetTransformInfoImplBase &&Arg) : DL(Arg.DL) {}
  35. const DataLayout &getDataLayout() const { return DL; }
  36. unsigned getOperationCost(unsigned Opcode, Type *Ty, Type *OpTy) {
  37. switch (Opcode) {
  38. default:
  39. // By default, just classify everything as 'basic'.
  40. return TTI::TCC_Basic;
  41. case Instruction::GetElementPtr:
  42. llvm_unreachable("Use getGEPCost for GEP operations!");
  43. case Instruction::BitCast:
  44. assert(OpTy && "Cast instructions must provide the operand type");
  45. if (Ty == OpTy || (Ty->isPointerTy() && OpTy->isPointerTy()))
  46. // Identity and pointer-to-pointer casts are free.
  47. return TTI::TCC_Free;
  48. // Otherwise, the default basic cost is used.
  49. return TTI::TCC_Basic;
  50. case Instruction::IntToPtr: {
  51. // An inttoptr cast is free so long as the input is a legal integer type
  52. // which doesn't contain values outside the range of a pointer.
  53. unsigned OpSize = OpTy->getScalarSizeInBits();
  54. if (DL.isLegalInteger(OpSize) &&
  55. OpSize <= DL.getPointerTypeSizeInBits(Ty))
  56. return TTI::TCC_Free;
  57. // Otherwise it's not a no-op.
  58. return TTI::TCC_Basic;
  59. }
  60. case Instruction::PtrToInt: {
  61. // A ptrtoint cast is free so long as the result is large enough to store
  62. // the pointer, and a legal integer type.
  63. unsigned DestSize = Ty->getScalarSizeInBits();
  64. if (DL.isLegalInteger(DestSize) &&
  65. DestSize >= DL.getPointerTypeSizeInBits(OpTy))
  66. return TTI::TCC_Free;
  67. // Otherwise it's not a no-op.
  68. return TTI::TCC_Basic;
  69. }
  70. case Instruction::Trunc:
  71. // trunc to a native type is free (assuming the target has compare and
  72. // shift-right of the same width).
  73. if (DL.isLegalInteger(DL.getTypeSizeInBits(Ty)))
  74. return TTI::TCC_Free;
  75. return TTI::TCC_Basic;
  76. }
  77. }
  78. unsigned getGEPCost(const Value *Ptr, ArrayRef<const Value *> Operands) {
  79. // In the basic model, we just assume that all-constant GEPs will be folded
  80. // into their uses via addressing modes.
  81. for (unsigned Idx = 0, Size = Operands.size(); Idx != Size; ++Idx)
  82. if (!isa<Constant>(Operands[Idx]))
  83. return TTI::TCC_Basic;
  84. return TTI::TCC_Free;
  85. }
  86. unsigned getCallCost(FunctionType *FTy, int NumArgs) {
  87. assert(FTy && "FunctionType must be provided to this routine.");
  88. // The target-independent implementation just measures the size of the
  89. // function by approximating that each argument will take on average one
  90. // instruction to prepare.
  91. if (NumArgs < 0)
  92. // Set the argument number to the number of explicit arguments in the
  93. // function.
  94. NumArgs = FTy->getNumParams();
  95. return TTI::TCC_Basic * (NumArgs + 1);
  96. }
  97. unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
  98. ArrayRef<Type *> ParamTys) {
  99. switch (IID) {
  100. default:
  101. // Intrinsics rarely (if ever) have normal argument setup constraints.
  102. // Model them as having a basic instruction cost.
  103. // FIXME: This is wrong for libc intrinsics.
  104. return TTI::TCC_Basic;
  105. case Intrinsic::annotation:
  106. case Intrinsic::assume:
  107. case Intrinsic::dbg_declare:
  108. case Intrinsic::dbg_value:
  109. case Intrinsic::invariant_start:
  110. case Intrinsic::invariant_end:
  111. case Intrinsic::lifetime_start:
  112. case Intrinsic::lifetime_end:
  113. case Intrinsic::objectsize:
  114. case Intrinsic::ptr_annotation:
  115. case Intrinsic::var_annotation:
  116. case Intrinsic::experimental_gc_result_int:
  117. case Intrinsic::experimental_gc_result_float:
  118. case Intrinsic::experimental_gc_result_ptr:
  119. case Intrinsic::experimental_gc_result:
  120. case Intrinsic::experimental_gc_relocate:
  121. // These intrinsics don't actually represent code after lowering.
  122. return TTI::TCC_Free;
  123. }
  124. }
  125. bool hasBranchDivergence() { return false; }
  126. bool isSourceOfDivergence(const Value *V) { return false; }
  127. bool isLoweredToCall(const Function *F) {
  128. // FIXME: These should almost certainly not be handled here, and instead
  129. // handled with the help of TLI or the target itself. This was largely
  130. // ported from existing analysis heuristics here so that such refactorings
  131. // can take place in the future.
  132. if (F->isIntrinsic())
  133. return false;
  134. if (F->hasLocalLinkage() || !F->hasName())
  135. return true;
  136. StringRef Name = F->getName();
  137. // These will all likely lower to a single selection DAG node.
  138. if (Name == "copysign" || Name == "copysignf" || Name == "copysignl" ||
  139. Name == "fabs" || Name == "fabsf" || Name == "fabsl" || Name == "sin" ||
  140. Name == "fmin" || Name == "fminf" || Name == "fminl" ||
  141. Name == "fmax" || Name == "fmaxf" || Name == "fmaxl" ||
  142. Name == "sinf" || Name == "sinl" || Name == "cos" || Name == "cosf" ||
  143. Name == "cosl" || Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl")
  144. return false;
  145. // These are all likely to be optimized into something smaller.
  146. if (Name == "pow" || Name == "powf" || Name == "powl" || Name == "exp2" ||
  147. Name == "exp2l" || Name == "exp2f" || Name == "floor" ||
  148. Name == "floorf" || Name == "ceil" || Name == "round" ||
  149. Name == "ffs" || Name == "ffsl" || Name == "abs" || Name == "labs" ||
  150. Name == "llabs")
  151. return false;
  152. return true;
  153. }
  154. void getUnrollingPreferences(Loop *, TTI::UnrollingPreferences &) {}
  155. bool isLegalAddImmediate(int64_t Imm) { return false; }
  156. bool isLegalICmpImmediate(int64_t Imm) { return false; }
  157. bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
  158. bool HasBaseReg, int64_t Scale,
  159. unsigned AddrSpace) {
  160. // Guess that only reg and reg+reg addressing is allowed. This heuristic is
  161. // taken from the implementation of LSR.
  162. return !BaseGV && BaseOffset == 0 && (Scale == 0 || Scale == 1);
  163. }
  164. bool isLegalMaskedStore(Type *DataType, int Consecutive) { return false; }
  165. bool isLegalMaskedLoad(Type *DataType, int Consecutive) { return false; }
  166. int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
  167. bool HasBaseReg, int64_t Scale, unsigned AddrSpace) {
  168. // Guess that all legal addressing mode are free.
  169. if (isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
  170. Scale, AddrSpace))
  171. return 0;
  172. return -1;
  173. }
  174. bool isTruncateFree(Type *Ty1, Type *Ty2) { return false; }
  175. bool isProfitableToHoist(Instruction *I) { return true; }
  176. bool isTypeLegal(Type *Ty) { return false; }
  177. unsigned getJumpBufAlignment() { return 0; }
  178. unsigned getJumpBufSize() { return 0; }
  179. bool shouldBuildLookupTables() { return true; }
  180. bool enableAggressiveInterleaving(bool LoopHasReductions) { return false; }
  181. TTI::PopcntSupportKind getPopcntSupport(unsigned IntTyWidthInBit) {
  182. return TTI::PSK_Software;
  183. }
  184. bool haveFastSqrt(Type *Ty) { return false; }
  185. unsigned getFPOpCost(Type *Ty) { return TargetTransformInfo::TCC_Basic; }
  186. unsigned getIntImmCost(const APInt &Imm, Type *Ty) { return TTI::TCC_Basic; }
  187. unsigned getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
  188. Type *Ty) {
  189. return TTI::TCC_Free;
  190. }
  191. unsigned getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
  192. Type *Ty) {
  193. return TTI::TCC_Free;
  194. }
  195. unsigned getNumberOfRegisters(bool Vector) { return 8; }
  196. unsigned getRegisterBitWidth(bool Vector) { return 32; }
  197. unsigned getMaxInterleaveFactor(unsigned VF) { return 1; }
  198. unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty,
  199. TTI::OperandValueKind Opd1Info,
  200. TTI::OperandValueKind Opd2Info,
  201. TTI::OperandValueProperties Opd1PropInfo,
  202. TTI::OperandValueProperties Opd2PropInfo) {
  203. return 1;
  204. }
  205. unsigned getShuffleCost(TTI::ShuffleKind Kind, Type *Ty, int Index,
  206. Type *SubTp) {
  207. return 1;
  208. }
  209. unsigned getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) { return 1; }
  210. unsigned getCFInstrCost(unsigned Opcode) { return 1; }
  211. unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy) {
  212. return 1;
  213. }
  214. unsigned getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) {
  215. return 1;
  216. }
  217. unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
  218. unsigned AddressSpace) {
  219. return 1;
  220. }
  221. unsigned getMaskedMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
  222. unsigned AddressSpace) {
  223. return 1;
  224. }
  225. unsigned getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy,
  226. unsigned Factor,
  227. ArrayRef<unsigned> Indices,
  228. unsigned Alignment,
  229. unsigned AddressSpace) {
  230. return 1;
  231. }
  232. unsigned getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
  233. ArrayRef<Type *> Tys) {
  234. return 1;
  235. }
  236. unsigned getCallInstrCost(Function *F, Type *RetTy, ArrayRef<Type *> Tys) {
  237. return 1;
  238. }
  239. unsigned getNumberOfParts(Type *Tp) { return 0; }
  240. unsigned getAddressComputationCost(Type *Tp, bool) { return 0; }
  241. unsigned getReductionCost(unsigned, Type *, bool) { return 1; }
  242. unsigned getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) { return 0; }
  243. bool getTgtMemIntrinsic(IntrinsicInst *Inst, MemIntrinsicInfo &Info) {
  244. return false;
  245. }
  246. Value *getOrCreateResultFromMemIntrinsic(IntrinsicInst *Inst,
  247. Type *ExpectedType) {
  248. return nullptr;
  249. }
  250. bool hasCompatibleFunctionAttributes(const Function *Caller,
  251. const Function *Callee) const {
  252. return (Caller->getFnAttribute("target-cpu") ==
  253. Callee->getFnAttribute("target-cpu")) &&
  254. (Caller->getFnAttribute("target-features") ==
  255. Callee->getFnAttribute("target-features"));
  256. }
  257. };
  258. /// \brief CRTP base class for use as a mix-in that aids implementing
  259. /// a TargetTransformInfo-compatible class.
  260. template <typename T>
  261. class TargetTransformInfoImplCRTPBase : public TargetTransformInfoImplBase {
  262. private:
  263. typedef TargetTransformInfoImplBase BaseT;
  264. protected:
  265. explicit TargetTransformInfoImplCRTPBase(const DataLayout &DL) : BaseT(DL) {}
  266. public:
  267. // Provide value semantics. MSVC requires that we spell all of these out.
  268. TargetTransformInfoImplCRTPBase(const TargetTransformInfoImplCRTPBase &Arg)
  269. : BaseT(static_cast<const BaseT &>(Arg)) {}
  270. TargetTransformInfoImplCRTPBase(TargetTransformInfoImplCRTPBase &&Arg)
  271. : BaseT(std::move(static_cast<BaseT &>(Arg))) {}
  272. using BaseT::getCallCost;
  273. unsigned getCallCost(const Function *F, int NumArgs) {
  274. assert(F && "A concrete function must be provided to this routine.");
  275. if (NumArgs < 0)
  276. // Set the argument number to the number of explicit arguments in the
  277. // function.
  278. NumArgs = F->arg_size();
  279. if (Intrinsic::ID IID = F->getIntrinsicID()) {
  280. FunctionType *FTy = F->getFunctionType();
  281. SmallVector<Type *, 8> ParamTys(FTy->param_begin(), FTy->param_end());
  282. return static_cast<T *>(this)
  283. ->getIntrinsicCost(IID, FTy->getReturnType(), ParamTys);
  284. }
  285. if (!static_cast<T *>(this)->isLoweredToCall(F))
  286. return TTI::TCC_Basic; // Give a basic cost if it will be lowered
  287. // directly.
  288. return static_cast<T *>(this)->getCallCost(F->getFunctionType(), NumArgs);
  289. }
  290. unsigned getCallCost(const Function *F, ArrayRef<const Value *> Arguments) {
  291. // Simply delegate to generic handling of the call.
  292. // FIXME: We should use instsimplify or something else to catch calls which
  293. // will constant fold with these arguments.
  294. return static_cast<T *>(this)->getCallCost(F, Arguments.size());
  295. }
  296. using BaseT::getIntrinsicCost;
  297. unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
  298. ArrayRef<const Value *> Arguments) {
  299. // Delegate to the generic intrinsic handling code. This mostly provides an
  300. // opportunity for targets to (for example) special case the cost of
  301. // certain intrinsics based on constants used as arguments.
  302. SmallVector<Type *, 8> ParamTys;
  303. ParamTys.reserve(Arguments.size());
  304. for (unsigned Idx = 0, Size = Arguments.size(); Idx != Size; ++Idx)
  305. ParamTys.push_back(Arguments[Idx]->getType());
  306. return static_cast<T *>(this)->getIntrinsicCost(IID, RetTy, ParamTys);
  307. }
  308. unsigned getUserCost(const User *U) {
  309. if (isa<PHINode>(U))
  310. return TTI::TCC_Free; // Model all PHI nodes as free.
  311. if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
  312. SmallVector<const Value *, 4> Indices(GEP->idx_begin(), GEP->idx_end());
  313. return static_cast<T *>(this)
  314. ->getGEPCost(GEP->getPointerOperand(), Indices);
  315. }
  316. if (auto CS = ImmutableCallSite(U)) {
  317. const Function *F = CS.getCalledFunction();
  318. if (!F) {
  319. // Just use the called value type.
  320. Type *FTy = CS.getCalledValue()->getType()->getPointerElementType();
  321. return static_cast<T *>(this)
  322. ->getCallCost(cast<FunctionType>(FTy), CS.arg_size());
  323. }
  324. SmallVector<const Value *, 8> Arguments(CS.arg_begin(), CS.arg_end());
  325. return static_cast<T *>(this)->getCallCost(F, Arguments);
  326. }
  327. if (const CastInst *CI = dyn_cast<CastInst>(U)) {
  328. // Result of a cmp instruction is often extended (to be used by other
  329. // cmp instructions, logical or return instructions). These are usually
  330. // nop on most sane targets.
  331. if (isa<CmpInst>(CI->getOperand(0)))
  332. return TTI::TCC_Free;
  333. }
  334. return static_cast<T *>(this)->getOperationCost(
  335. Operator::getOpcode(U), U->getType(),
  336. U->getNumOperands() == 1 ? U->getOperand(0)->getType() : nullptr);
  337. }
  338. };
  339. }
  340. #endif