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