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