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