Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 1 | //===- BasicTargetTransformInfo.cpp - Basic target-independent TTI impl ---===// |
| 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 the implementation of a basic TargetTransformInfo pass |
| 11 | /// predicated on the target abstractions present in the target independent |
| 12 | /// code generator. It uses these (primarily TargetLowering) to model as much |
| 13 | /// of the TTI query interface as possible. It is included by most targets so |
| 14 | /// that they can specialize only a small subset of the query space. |
| 15 | /// |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #define DEBUG_TYPE "basictti" |
| 19 | #include "llvm/CodeGen/Passes.h" |
Chandler Carruth | be04929 | 2013-01-07 03:08:10 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetLowering.h" |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 22 | #include <utility> |
| 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | class BasicTTI : public ImmutablePass, public TargetTransformInfo { |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 29 | const TargetMachine *TM; |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 30 | |
| 31 | /// Estimate the overhead of scalarizing an instruction. Insert and Extract |
| 32 | /// are set if the result needs to be inserted and/or extracted from vectors. |
| 33 | unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const; |
| 34 | |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 35 | const TargetLoweringBase *getTLI() const { return TM->getTargetLowering(); } |
| 36 | |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 37 | public: |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 38 | BasicTTI() : ImmutablePass(ID), TM(0) { |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 39 | llvm_unreachable("This pass cannot be directly constructed"); |
| 40 | } |
| 41 | |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 42 | BasicTTI(const TargetMachine *TM) : ImmutablePass(ID), TM(TM) { |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 43 | initializeBasicTTIPass(*PassRegistry::getPassRegistry()); |
| 44 | } |
| 45 | |
| 46 | virtual void initializePass() { |
| 47 | pushTTIStack(this); |
| 48 | } |
| 49 | |
| 50 | virtual void finalizePass() { |
| 51 | popTTIStack(); |
| 52 | } |
| 53 | |
| 54 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 55 | TargetTransformInfo::getAnalysisUsage(AU); |
| 56 | } |
| 57 | |
| 58 | /// Pass identification. |
| 59 | static char ID; |
| 60 | |
| 61 | /// Provide necessary pointer adjustments for the two base classes. |
| 62 | virtual void *getAdjustedAnalysisPointer(const void *ID) { |
| 63 | if (ID == &TargetTransformInfo::ID) |
| 64 | return (TargetTransformInfo*)this; |
| 65 | return this; |
| 66 | } |
| 67 | |
| 68 | /// \name Scalar TTI Implementations |
| 69 | /// @{ |
| 70 | |
| 71 | virtual bool isLegalAddImmediate(int64_t imm) const; |
| 72 | virtual bool isLegalICmpImmediate(int64_t imm) const; |
| 73 | virtual bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, |
| 74 | int64_t BaseOffset, bool HasBaseReg, |
| 75 | int64_t Scale) const; |
Quentin Colombet | 06f5ebc | 2013-05-31 21:29:03 +0000 | [diff] [blame] | 76 | virtual int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, |
| 77 | int64_t BaseOffset, bool HasBaseReg, |
| 78 | int64_t Scale) const; |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 79 | virtual bool isTruncateFree(Type *Ty1, Type *Ty2) const; |
| 80 | virtual bool isTypeLegal(Type *Ty) const; |
| 81 | virtual unsigned getJumpBufAlignment() const; |
| 82 | virtual unsigned getJumpBufSize() const; |
| 83 | virtual bool shouldBuildLookupTables() const; |
| 84 | |
| 85 | /// @} |
| 86 | |
| 87 | /// \name Vector TTI Implementations |
| 88 | /// @{ |
| 89 | |
| 90 | virtual unsigned getNumberOfRegisters(bool Vector) const; |
Nadav Rotem | 83be7b0 | 2013-01-09 01:15:42 +0000 | [diff] [blame] | 91 | virtual unsigned getMaximumUnrollFactor() const; |
Nadav Rotem | 14925e6 | 2013-01-09 22:29:00 +0000 | [diff] [blame] | 92 | virtual unsigned getRegisterBitWidth(bool Vector) const; |
Arnold Schwaighofer | 6bf4f67 | 2013-04-04 23:26:21 +0000 | [diff] [blame] | 93 | virtual unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty, |
| 94 | OperandValueKind, |
| 95 | OperandValueKind) const; |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 96 | virtual unsigned getShuffleCost(ShuffleKind Kind, Type *Tp, |
| 97 | int Index, Type *SubTp) const; |
| 98 | virtual unsigned getCastInstrCost(unsigned Opcode, Type *Dst, |
| 99 | Type *Src) const; |
| 100 | virtual unsigned getCFInstrCost(unsigned Opcode) const; |
| 101 | virtual unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, |
| 102 | Type *CondTy) const; |
| 103 | virtual unsigned getVectorInstrCost(unsigned Opcode, Type *Val, |
| 104 | unsigned Index) const; |
| 105 | virtual unsigned getMemoryOpCost(unsigned Opcode, Type *Src, |
| 106 | unsigned Alignment, |
| 107 | unsigned AddressSpace) const; |
| 108 | virtual unsigned getIntrinsicInstrCost(Intrinsic::ID, Type *RetTy, |
| 109 | ArrayRef<Type*> Tys) const; |
| 110 | virtual unsigned getNumberOfParts(Type *Tp) const; |
Arnold Schwaighofer | c0a11ed | 2013-07-12 19:16:02 +0000 | [diff] [blame] | 111 | virtual unsigned getAddressComputationCost(Type *Ty, bool IsComplex) const; |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 112 | |
| 113 | /// @} |
| 114 | }; |
| 115 | |
| 116 | } |
| 117 | |
| 118 | INITIALIZE_AG_PASS(BasicTTI, TargetTransformInfo, "basictti", |
| 119 | "Target independent code generator's TTI", true, true, false) |
| 120 | char BasicTTI::ID = 0; |
| 121 | |
| 122 | ImmutablePass * |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 123 | llvm::createBasicTargetTransformInfoPass(const TargetMachine *TM) { |
| 124 | return new BasicTTI(TM); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | |
| 128 | bool BasicTTI::isLegalAddImmediate(int64_t imm) const { |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 129 | return getTLI()->isLegalAddImmediate(imm); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | bool BasicTTI::isLegalICmpImmediate(int64_t imm) const { |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 133 | return getTLI()->isLegalICmpImmediate(imm); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | bool BasicTTI::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, |
| 137 | int64_t BaseOffset, bool HasBaseReg, |
| 138 | int64_t Scale) const { |
Benjamin Kramer | 69e42db | 2013-01-11 20:05:37 +0000 | [diff] [blame] | 139 | TargetLoweringBase::AddrMode AM; |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 140 | AM.BaseGV = BaseGV; |
| 141 | AM.BaseOffs = BaseOffset; |
| 142 | AM.HasBaseReg = HasBaseReg; |
| 143 | AM.Scale = Scale; |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 144 | return getTLI()->isLegalAddressingMode(AM, Ty); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Quentin Colombet | 06f5ebc | 2013-05-31 21:29:03 +0000 | [diff] [blame] | 147 | int BasicTTI::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, |
| 148 | int64_t BaseOffset, bool HasBaseReg, |
| 149 | int64_t Scale) const { |
| 150 | TargetLoweringBase::AddrMode AM; |
| 151 | AM.BaseGV = BaseGV; |
| 152 | AM.BaseOffs = BaseOffset; |
| 153 | AM.HasBaseReg = HasBaseReg; |
| 154 | AM.Scale = Scale; |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 155 | return getTLI()->getScalingFactorCost(AM, Ty); |
Quentin Colombet | 06f5ebc | 2013-05-31 21:29:03 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 158 | bool BasicTTI::isTruncateFree(Type *Ty1, Type *Ty2) const { |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 159 | return getTLI()->isTruncateFree(Ty1, Ty2); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | bool BasicTTI::isTypeLegal(Type *Ty) const { |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 163 | EVT T = getTLI()->getValueType(Ty); |
| 164 | return getTLI()->isTypeLegal(T); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | unsigned BasicTTI::getJumpBufAlignment() const { |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 168 | return getTLI()->getJumpBufAlignment(); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | unsigned BasicTTI::getJumpBufSize() const { |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 172 | return getTLI()->getJumpBufSize(); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | bool BasicTTI::shouldBuildLookupTables() const { |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 176 | const TargetLoweringBase *TLI = getTLI(); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 177 | return TLI->supportJumpTables() && |
| 178 | (TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) || |
| 179 | TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other)); |
| 180 | } |
| 181 | |
| 182 | //===----------------------------------------------------------------------===// |
| 183 | // |
| 184 | // Calls used by the vectorizers. |
| 185 | // |
| 186 | //===----------------------------------------------------------------------===// |
| 187 | |
| 188 | unsigned BasicTTI::getScalarizationOverhead(Type *Ty, bool Insert, |
| 189 | bool Extract) const { |
| 190 | assert (Ty->isVectorTy() && "Can only scalarize vectors"); |
| 191 | unsigned Cost = 0; |
| 192 | |
| 193 | for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) { |
| 194 | if (Insert) |
| 195 | Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i); |
| 196 | if (Extract) |
| 197 | Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i); |
| 198 | } |
| 199 | |
| 200 | return Cost; |
| 201 | } |
| 202 | |
| 203 | unsigned BasicTTI::getNumberOfRegisters(bool Vector) const { |
| 204 | return 1; |
| 205 | } |
| 206 | |
Nadav Rotem | 14925e6 | 2013-01-09 22:29:00 +0000 | [diff] [blame] | 207 | unsigned BasicTTI::getRegisterBitWidth(bool Vector) const { |
| 208 | return 32; |
| 209 | } |
| 210 | |
Nadav Rotem | 83be7b0 | 2013-01-09 01:15:42 +0000 | [diff] [blame] | 211 | unsigned BasicTTI::getMaximumUnrollFactor() const { |
| 212 | return 1; |
| 213 | } |
| 214 | |
Arnold Schwaighofer | 6bf4f67 | 2013-04-04 23:26:21 +0000 | [diff] [blame] | 215 | unsigned BasicTTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty, |
| 216 | OperandValueKind, |
| 217 | OperandValueKind) const { |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 218 | // Check if any of the operands are vector operands. |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 219 | const TargetLoweringBase *TLI = getTLI(); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 220 | int ISD = TLI->InstructionOpcodeToISD(Opcode); |
| 221 | assert(ISD && "Invalid opcode"); |
| 222 | |
| 223 | std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty); |
| 224 | |
Nadav Rotem | 9eb366a | 2013-04-12 21:15:03 +0000 | [diff] [blame] | 225 | bool IsFloat = Ty->getScalarType()->isFloatingPointTy(); |
Nadav Rotem | 4208200 | 2013-04-14 05:55:18 +0000 | [diff] [blame] | 226 | // Assume that floating point arithmetic operations cost twice as much as |
| 227 | // integer operations. |
Nadav Rotem | 9eb366a | 2013-04-12 21:15:03 +0000 | [diff] [blame] | 228 | unsigned OpCost = (IsFloat ? 2 : 1); |
| 229 | |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 230 | if (TLI->isOperationLegalOrPromote(ISD, LT.second)) { |
| 231 | // The operation is legal. Assume it costs 1. |
Nadav Rotem | 4208200 | 2013-04-14 05:55:18 +0000 | [diff] [blame] | 232 | // If the type is split to multiple registers, assume that there is some |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 233 | // overhead to this. |
| 234 | // TODO: Once we have extract/insert subvector cost we need to use them. |
| 235 | if (LT.first > 1) |
Nadav Rotem | 9eb366a | 2013-04-12 21:15:03 +0000 | [diff] [blame] | 236 | return LT.first * 2 * OpCost; |
| 237 | return LT.first * 1 * OpCost; |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | if (!TLI->isOperationExpand(ISD, LT.second)) { |
| 241 | // If the operation is custom lowered then assume |
| 242 | // thare the code is twice as expensive. |
Nadav Rotem | 9eb366a | 2013-04-12 21:15:03 +0000 | [diff] [blame] | 243 | return LT.first * 2 * OpCost; |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | // Else, assume that we need to scalarize this op. |
| 247 | if (Ty->isVectorTy()) { |
| 248 | unsigned Num = Ty->getVectorNumElements(); |
| 249 | unsigned Cost = TopTTI->getArithmeticInstrCost(Opcode, Ty->getScalarType()); |
| 250 | // return the cost of multiple scalar invocation plus the cost of inserting |
| 251 | // and extracting the values. |
| 252 | return getScalarizationOverhead(Ty, true, true) + Num * Cost; |
| 253 | } |
| 254 | |
| 255 | // We don't know anything about this scalar instruction. |
Nadav Rotem | 9eb366a | 2013-04-12 21:15:03 +0000 | [diff] [blame] | 256 | return OpCost; |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | unsigned BasicTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index, |
| 260 | Type *SubTp) const { |
| 261 | return 1; |
| 262 | } |
| 263 | |
| 264 | unsigned BasicTTI::getCastInstrCost(unsigned Opcode, Type *Dst, |
| 265 | Type *Src) const { |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 266 | const TargetLoweringBase *TLI = getTLI(); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 267 | int ISD = TLI->InstructionOpcodeToISD(Opcode); |
| 268 | assert(ISD && "Invalid opcode"); |
| 269 | |
| 270 | std::pair<unsigned, MVT> SrcLT = TLI->getTypeLegalizationCost(Src); |
| 271 | std::pair<unsigned, MVT> DstLT = TLI->getTypeLegalizationCost(Dst); |
| 272 | |
Nadav Rotem | 3e40d92 | 2013-01-11 19:54:13 +0000 | [diff] [blame] | 273 | // Check for NOOP conversions. |
| 274 | if (SrcLT.first == DstLT.first && |
| 275 | SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) { |
| 276 | |
| 277 | // Bitcast between types that are legalized to the same type are free. |
| 278 | if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc) |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | if (Opcode == Instruction::Trunc && |
| 283 | TLI->isTruncateFree(SrcLT.second, DstLT.second)) |
| 284 | return 0; |
| 285 | |
| 286 | if (Opcode == Instruction::ZExt && |
| 287 | TLI->isZExtFree(SrcLT.second, DstLT.second)) |
| 288 | return 0; |
| 289 | |
| 290 | // If the cast is marked as legal (or promote) then assume low cost. |
| 291 | if (TLI->isOperationLegalOrPromote(ISD, DstLT.second)) |
| 292 | return 1; |
| 293 | |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 294 | // Handle scalar conversions. |
| 295 | if (!Src->isVectorTy() && !Dst->isVectorTy()) { |
| 296 | |
| 297 | // Scalar bitcasts are usually free. |
| 298 | if (Opcode == Instruction::BitCast) |
| 299 | return 0; |
| 300 | |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 301 | // Just check the op cost. If the operation is legal then assume it costs 1. |
| 302 | if (!TLI->isOperationExpand(ISD, DstLT.second)) |
| 303 | return 1; |
| 304 | |
| 305 | // Assume that illegal scalar instruction are expensive. |
| 306 | return 4; |
| 307 | } |
| 308 | |
| 309 | // Check vector-to-vector casts. |
| 310 | if (Dst->isVectorTy() && Src->isVectorTy()) { |
| 311 | |
| 312 | // If the cast is between same-sized registers, then the check is simple. |
| 313 | if (SrcLT.first == DstLT.first && |
| 314 | SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) { |
| 315 | |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 316 | // Assume that Zext is done using AND. |
| 317 | if (Opcode == Instruction::ZExt) |
| 318 | return 1; |
| 319 | |
| 320 | // Assume that sext is done using SHL and SRA. |
| 321 | if (Opcode == Instruction::SExt) |
| 322 | return 2; |
| 323 | |
| 324 | // Just check the op cost. If the operation is legal then assume it costs |
| 325 | // 1 and multiply by the type-legalization overhead. |
| 326 | if (!TLI->isOperationExpand(ISD, DstLT.second)) |
| 327 | return SrcLT.first * 1; |
| 328 | } |
| 329 | |
| 330 | // If we are converting vectors and the operation is illegal, or |
| 331 | // if the vectors are legalized to different types, estimate the |
| 332 | // scalarization costs. |
| 333 | unsigned Num = Dst->getVectorNumElements(); |
| 334 | unsigned Cost = TopTTI->getCastInstrCost(Opcode, Dst->getScalarType(), |
| 335 | Src->getScalarType()); |
| 336 | |
| 337 | // Return the cost of multiple scalar invocation plus the cost of |
| 338 | // inserting and extracting the values. |
| 339 | return getScalarizationOverhead(Dst, true, true) + Num * Cost; |
| 340 | } |
| 341 | |
| 342 | // We already handled vector-to-vector and scalar-to-scalar conversions. This |
| 343 | // is where we handle bitcast between vectors and scalars. We need to assume |
| 344 | // that the conversion is scalarized in one way or another. |
| 345 | if (Opcode == Instruction::BitCast) |
| 346 | // Illegal bitcasts are done by storing and loading from a stack slot. |
| 347 | return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) + |
| 348 | (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0); |
| 349 | |
| 350 | llvm_unreachable("Unhandled cast"); |
| 351 | } |
| 352 | |
| 353 | unsigned BasicTTI::getCFInstrCost(unsigned Opcode) const { |
| 354 | // Branches are assumed to be predicted. |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | unsigned BasicTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, |
| 359 | Type *CondTy) const { |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 360 | const TargetLoweringBase *TLI = getTLI(); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 361 | int ISD = TLI->InstructionOpcodeToISD(Opcode); |
| 362 | assert(ISD && "Invalid opcode"); |
| 363 | |
| 364 | // Selects on vectors are actually vector selects. |
| 365 | if (ISD == ISD::SELECT) { |
| 366 | assert(CondTy && "CondTy must exist"); |
| 367 | if (CondTy->isVectorTy()) |
| 368 | ISD = ISD::VSELECT; |
| 369 | } |
| 370 | |
| 371 | std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy); |
| 372 | |
| 373 | if (!TLI->isOperationExpand(ISD, LT.second)) { |
| 374 | // The operation is legal. Assume it costs 1. Multiply |
| 375 | // by the type-legalization overhead. |
| 376 | return LT.first * 1; |
| 377 | } |
| 378 | |
| 379 | // Otherwise, assume that the cast is scalarized. |
| 380 | if (ValTy->isVectorTy()) { |
| 381 | unsigned Num = ValTy->getVectorNumElements(); |
| 382 | if (CondTy) |
| 383 | CondTy = CondTy->getScalarType(); |
| 384 | unsigned Cost = TopTTI->getCmpSelInstrCost(Opcode, ValTy->getScalarType(), |
| 385 | CondTy); |
| 386 | |
| 387 | // Return the cost of multiple scalar invocation plus the cost of inserting |
| 388 | // and extracting the values. |
| 389 | return getScalarizationOverhead(ValTy, true, false) + Num * Cost; |
| 390 | } |
| 391 | |
| 392 | // Unknown scalar opcode. |
| 393 | return 1; |
| 394 | } |
| 395 | |
| 396 | unsigned BasicTTI::getVectorInstrCost(unsigned Opcode, Type *Val, |
| 397 | unsigned Index) const { |
| 398 | return 1; |
| 399 | } |
| 400 | |
| 401 | unsigned BasicTTI::getMemoryOpCost(unsigned Opcode, Type *Src, |
| 402 | unsigned Alignment, |
| 403 | unsigned AddressSpace) const { |
| 404 | assert(!Src->isVoidTy() && "Invalid type"); |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 405 | std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Src); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 406 | |
| 407 | // Assume that all loads of legal types cost 1. |
| 408 | return LT.first; |
| 409 | } |
| 410 | |
Benjamin Kramer | 8611d44 | 2013-02-28 19:09:33 +0000 | [diff] [blame] | 411 | unsigned BasicTTI::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy, |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 412 | ArrayRef<Type *> Tys) const { |
Benjamin Kramer | 8611d44 | 2013-02-28 19:09:33 +0000 | [diff] [blame] | 413 | unsigned ISD = 0; |
| 414 | switch (IID) { |
| 415 | default: { |
| 416 | // Assume that we need to scalarize this intrinsic. |
| 417 | unsigned ScalarizationCost = 0; |
| 418 | unsigned ScalarCalls = 1; |
| 419 | if (RetTy->isVectorTy()) { |
| 420 | ScalarizationCost = getScalarizationOverhead(RetTy, true, false); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 421 | ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements()); |
| 422 | } |
Benjamin Kramer | 8611d44 | 2013-02-28 19:09:33 +0000 | [diff] [blame] | 423 | for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) { |
| 424 | if (Tys[i]->isVectorTy()) { |
| 425 | ScalarizationCost += getScalarizationOverhead(Tys[i], false, true); |
| 426 | ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements()); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | return ScalarCalls + ScalarizationCost; |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 431 | } |
Benjamin Kramer | 8611d44 | 2013-02-28 19:09:33 +0000 | [diff] [blame] | 432 | // Look for intrinsics that can be lowered directly or turned into a scalar |
| 433 | // intrinsic call. |
| 434 | case Intrinsic::sqrt: ISD = ISD::FSQRT; break; |
| 435 | case Intrinsic::sin: ISD = ISD::FSIN; break; |
| 436 | case Intrinsic::cos: ISD = ISD::FCOS; break; |
| 437 | case Intrinsic::exp: ISD = ISD::FEXP; break; |
| 438 | case Intrinsic::exp2: ISD = ISD::FEXP2; break; |
| 439 | case Intrinsic::log: ISD = ISD::FLOG; break; |
| 440 | case Intrinsic::log10: ISD = ISD::FLOG10; break; |
| 441 | case Intrinsic::log2: ISD = ISD::FLOG2; break; |
| 442 | case Intrinsic::fabs: ISD = ISD::FABS; break; |
| 443 | case Intrinsic::floor: ISD = ISD::FFLOOR; break; |
| 444 | case Intrinsic::ceil: ISD = ISD::FCEIL; break; |
| 445 | case Intrinsic::trunc: ISD = ISD::FTRUNC; break; |
Hal Finkel | 04b84c2 | 2013-07-08 03:24:07 +0000 | [diff] [blame] | 446 | case Intrinsic::nearbyint: |
| 447 | ISD = ISD::FNEARBYINT; break; |
Benjamin Kramer | 8611d44 | 2013-02-28 19:09:33 +0000 | [diff] [blame] | 448 | case Intrinsic::rint: ISD = ISD::FRINT; break; |
| 449 | case Intrinsic::pow: ISD = ISD::FPOW; break; |
| 450 | case Intrinsic::fma: ISD = ISD::FMA; break; |
| 451 | case Intrinsic::fmuladd: ISD = ISD::FMA; break; // FIXME: mul + add? |
| 452 | } |
| 453 | |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 454 | const TargetLoweringBase *TLI = getTLI(); |
Benjamin Kramer | 8611d44 | 2013-02-28 19:09:33 +0000 | [diff] [blame] | 455 | std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(RetTy); |
| 456 | |
| 457 | if (TLI->isOperationLegalOrPromote(ISD, LT.second)) { |
| 458 | // The operation is legal. Assume it costs 1. |
| 459 | // If the type is split to multiple registers, assume that thre is some |
| 460 | // overhead to this. |
| 461 | // TODO: Once we have extract/insert subvector cost we need to use them. |
| 462 | if (LT.first > 1) |
| 463 | return LT.first * 2; |
| 464 | return LT.first * 1; |
| 465 | } |
| 466 | |
| 467 | if (!TLI->isOperationExpand(ISD, LT.second)) { |
| 468 | // If the operation is custom lowered then assume |
| 469 | // thare the code is twice as expensive. |
| 470 | return LT.first * 2; |
| 471 | } |
| 472 | |
| 473 | // Else, assume that we need to scalarize this intrinsic. For math builtins |
| 474 | // this will emit a costly libcall, adding call overhead and spills. Make it |
| 475 | // very expensive. |
| 476 | if (RetTy->isVectorTy()) { |
| 477 | unsigned Num = RetTy->getVectorNumElements(); |
| 478 | unsigned Cost = TopTTI->getIntrinsicInstrCost(IID, RetTy->getScalarType(), |
| 479 | Tys); |
| 480 | return 10 * Cost * Num; |
| 481 | } |
| 482 | |
| 483 | // This is going to be turned into a library call, make it expensive. |
| 484 | return 10; |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | unsigned BasicTTI::getNumberOfParts(Type *Tp) const { |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 488 | std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Tp); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 489 | return LT.first; |
| 490 | } |
Arnold Schwaighofer | fb55a8f | 2013-02-08 14:50:48 +0000 | [diff] [blame] | 491 | |
Arnold Schwaighofer | c0a11ed | 2013-07-12 19:16:02 +0000 | [diff] [blame] | 492 | unsigned BasicTTI::getAddressComputationCost(Type *Ty, bool IsComplex) const { |
Arnold Schwaighofer | fb55a8f | 2013-02-08 14:50:48 +0000 | [diff] [blame] | 493 | return 0; |
| 494 | } |