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 | |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/Passes.h" |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 19 | #include "llvm/Analysis/LoopInfo.h" |
Chandler Carruth | be04929 | 2013-01-07 03:08:10 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/TargetTransformInfo.h" |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 21 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetLowering.h" |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 23 | #include "llvm/Target/TargetSubtargetInfo.h" |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 24 | #include <utility> |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 27 | static cl::opt<unsigned> |
| 28 | PartialUnrollingThreshold("partial-unrolling-threshold", cl::init(0), |
| 29 | cl::desc("Threshold for partial unrolling"), cl::Hidden); |
| 30 | |
| 31 | #define DEBUG_TYPE "basictti" |
| 32 | |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 35 | class BasicTTI final : public ImmutablePass, public TargetTransformInfo { |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 36 | const TargetMachine *TM; |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 37 | |
| 38 | /// Estimate the overhead of scalarizing an instruction. Insert and Extract |
| 39 | /// are set if the result needs to be inserted and/or extracted from vectors. |
| 40 | unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const; |
| 41 | |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 42 | /// Estimate the cost overhead of SK_Alternate shuffle. |
| 43 | unsigned getAltShuffleOverhead(Type *Ty) const; |
| 44 | |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 45 | const TargetLoweringBase *getTLI() const { return TM->getTargetLowering(); } |
| 46 | |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 47 | public: |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 48 | BasicTTI() : ImmutablePass(ID), TM(nullptr) { |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 49 | llvm_unreachable("This pass cannot be directly constructed"); |
| 50 | } |
| 51 | |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 52 | BasicTTI(const TargetMachine *TM) : ImmutablePass(ID), TM(TM) { |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 53 | initializeBasicTTIPass(*PassRegistry::getPassRegistry()); |
| 54 | } |
| 55 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 56 | void initializePass() override { |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 57 | pushTTIStack(this); |
| 58 | } |
| 59 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 60 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 61 | TargetTransformInfo::getAnalysisUsage(AU); |
| 62 | } |
| 63 | |
| 64 | /// Pass identification. |
| 65 | static char ID; |
| 66 | |
| 67 | /// Provide necessary pointer adjustments for the two base classes. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 68 | void *getAdjustedAnalysisPointer(const void *ID) override { |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 69 | if (ID == &TargetTransformInfo::ID) |
| 70 | return (TargetTransformInfo*)this; |
| 71 | return this; |
| 72 | } |
| 73 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 74 | bool hasBranchDivergence() const override; |
Tom Stellard | 57e6b2d | 2013-07-27 00:01:07 +0000 | [diff] [blame] | 75 | |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 76 | /// \name Scalar TTI Implementations |
| 77 | /// @{ |
| 78 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 79 | bool isLegalAddImmediate(int64_t imm) const override; |
| 80 | bool isLegalICmpImmediate(int64_t imm) const override; |
| 81 | bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, |
| 82 | int64_t BaseOffset, bool HasBaseReg, |
| 83 | int64_t Scale) const override; |
| 84 | int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, |
| 85 | int64_t BaseOffset, bool HasBaseReg, |
| 86 | int64_t Scale) const override; |
| 87 | bool isTruncateFree(Type *Ty1, Type *Ty2) const override; |
| 88 | bool isTypeLegal(Type *Ty) const override; |
| 89 | unsigned getJumpBufAlignment() const override; |
| 90 | unsigned getJumpBufSize() const override; |
| 91 | bool shouldBuildLookupTables() const override; |
| 92 | bool haveFastSqrt(Type *Ty) const override; |
| 93 | void getUnrollingPreferences(Loop *L, |
| 94 | UnrollingPreferences &UP) const override; |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 95 | |
| 96 | /// @} |
| 97 | |
| 98 | /// \name Vector TTI Implementations |
| 99 | /// @{ |
| 100 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 101 | unsigned getNumberOfRegisters(bool Vector) const override; |
| 102 | unsigned getMaximumUnrollFactor() const override; |
| 103 | unsigned getRegisterBitWidth(bool Vector) const override; |
| 104 | unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty, OperandValueKind, |
| 105 | OperandValueKind) const override; |
| 106 | unsigned getShuffleCost(ShuffleKind Kind, Type *Tp, |
| 107 | int Index, Type *SubTp) const override; |
| 108 | unsigned getCastInstrCost(unsigned Opcode, Type *Dst, |
| 109 | Type *Src) const override; |
| 110 | unsigned getCFInstrCost(unsigned Opcode) const override; |
| 111 | unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, |
| 112 | Type *CondTy) const override; |
| 113 | unsigned getVectorInstrCost(unsigned Opcode, Type *Val, |
| 114 | unsigned Index) const override; |
| 115 | unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment, |
| 116 | unsigned AddressSpace) const override; |
| 117 | unsigned getIntrinsicInstrCost(Intrinsic::ID, Type *RetTy, |
| 118 | ArrayRef<Type*> Tys) const override; |
| 119 | unsigned getNumberOfParts(Type *Tp) const override; |
| 120 | unsigned getAddressComputationCost( Type *Ty, bool IsComplex) const override; |
| 121 | unsigned getReductionCost(unsigned Opcode, Type *Ty, |
| 122 | bool IsPairwise) const override; |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 123 | |
| 124 | /// @} |
| 125 | }; |
| 126 | |
| 127 | } |
| 128 | |
| 129 | INITIALIZE_AG_PASS(BasicTTI, TargetTransformInfo, "basictti", |
| 130 | "Target independent code generator's TTI", true, true, false) |
| 131 | char BasicTTI::ID = 0; |
| 132 | |
| 133 | ImmutablePass * |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 134 | llvm::createBasicTargetTransformInfoPass(const TargetMachine *TM) { |
| 135 | return new BasicTTI(TM); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Tom Stellard | 57e6b2d | 2013-07-27 00:01:07 +0000 | [diff] [blame] | 138 | bool BasicTTI::hasBranchDivergence() const { return false; } |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 139 | |
| 140 | bool BasicTTI::isLegalAddImmediate(int64_t imm) const { |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 141 | return getTLI()->isLegalAddImmediate(imm); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | bool BasicTTI::isLegalICmpImmediate(int64_t imm) const { |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 145 | return getTLI()->isLegalICmpImmediate(imm); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | bool BasicTTI::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, |
| 149 | int64_t BaseOffset, bool HasBaseReg, |
| 150 | int64_t Scale) const { |
Benjamin Kramer | 69e42db | 2013-01-11 20:05:37 +0000 | [diff] [blame] | 151 | TargetLoweringBase::AddrMode AM; |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 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()->isLegalAddressingMode(AM, Ty); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Quentin Colombet | 06f5ebc | 2013-05-31 21:29:03 +0000 | [diff] [blame] | 159 | int BasicTTI::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, |
| 160 | int64_t BaseOffset, bool HasBaseReg, |
| 161 | int64_t Scale) const { |
| 162 | TargetLoweringBase::AddrMode AM; |
| 163 | AM.BaseGV = BaseGV; |
| 164 | AM.BaseOffs = BaseOffset; |
| 165 | AM.HasBaseReg = HasBaseReg; |
| 166 | AM.Scale = Scale; |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 167 | return getTLI()->getScalingFactorCost(AM, Ty); |
Quentin Colombet | 06f5ebc | 2013-05-31 21:29:03 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 170 | bool BasicTTI::isTruncateFree(Type *Ty1, Type *Ty2) const { |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 171 | return getTLI()->isTruncateFree(Ty1, Ty2); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | bool BasicTTI::isTypeLegal(Type *Ty) const { |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 175 | EVT T = getTLI()->getValueType(Ty); |
| 176 | return getTLI()->isTypeLegal(T); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | unsigned BasicTTI::getJumpBufAlignment() const { |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 180 | return getTLI()->getJumpBufAlignment(); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | unsigned BasicTTI::getJumpBufSize() const { |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 184 | return getTLI()->getJumpBufSize(); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | bool BasicTTI::shouldBuildLookupTables() const { |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 188 | const TargetLoweringBase *TLI = getTLI(); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 189 | return TLI->supportJumpTables() && |
| 190 | (TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) || |
| 191 | TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other)); |
| 192 | } |
| 193 | |
Richard Sandiford | a8a7099 | 2013-08-23 10:27:02 +0000 | [diff] [blame] | 194 | bool BasicTTI::haveFastSqrt(Type *Ty) const { |
| 195 | const TargetLoweringBase *TLI = getTLI(); |
| 196 | EVT VT = TLI->getValueType(Ty); |
| 197 | return TLI->isTypeLegal(VT) && TLI->isOperationLegalOrCustom(ISD::FSQRT, VT); |
| 198 | } |
| 199 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 200 | void BasicTTI::getUnrollingPreferences(Loop *L, |
| 201 | UnrollingPreferences &UP) const { |
| 202 | // This unrolling functionality is target independent, but to provide some |
| 203 | // motivation for its intended use, for x86: |
| 204 | |
| 205 | // According to the Intel 64 and IA-32 Architectures Optimization Reference |
| 206 | // Manual, Intel Core models and later have a loop stream detector |
| 207 | // (and associated uop queue) that can benefit from partial unrolling. |
| 208 | // The relevant requirements are: |
| 209 | // - The loop must have no more than 4 (8 for Nehalem and later) branches |
| 210 | // taken, and none of them may be calls. |
| 211 | // - The loop can have no more than 18 (28 for Nehalem and later) uops. |
| 212 | |
| 213 | // According to the Software Optimization Guide for AMD Family 15h Processors, |
| 214 | // models 30h-4fh (Steamroller and later) have a loop predictor and loop |
| 215 | // buffer which can benefit from partial unrolling. |
| 216 | // The relevant requirements are: |
| 217 | // - The loop must have fewer than 16 branches |
| 218 | // - The loop must have less than 40 uops in all executed loop branches |
| 219 | |
| 220 | // The number of taken branches in a loop is hard to estimate here, and |
| 221 | // benchmarking has revealed that it is better not to be conservative when |
| 222 | // estimating the branch count. As a result, we'll ignore the branch limits |
| 223 | // until someone finds a case where it matters in practice. |
| 224 | |
| 225 | unsigned MaxOps; |
| 226 | const TargetSubtargetInfo *ST = &TM->getSubtarget<TargetSubtargetInfo>(); |
| 227 | if (PartialUnrollingThreshold.getNumOccurrences() > 0) |
| 228 | MaxOps = PartialUnrollingThreshold; |
| 229 | else if (ST->getSchedModel()->LoopMicroOpBufferSize > 0) |
| 230 | MaxOps = ST->getSchedModel()->LoopMicroOpBufferSize; |
| 231 | else |
| 232 | return; |
| 233 | |
| 234 | // Scan the loop: don't unroll loops with calls. |
| 235 | for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); |
| 236 | I != E; ++I) { |
| 237 | BasicBlock *BB = *I; |
| 238 | |
| 239 | for (BasicBlock::iterator J = BB->begin(), JE = BB->end(); J != JE; ++J) |
| 240 | if (isa<CallInst>(J) || isa<InvokeInst>(J)) { |
| 241 | ImmutableCallSite CS(J); |
| 242 | if (const Function *F = CS.getCalledFunction()) { |
| 243 | if (!TopTTI->isLoweredToCall(F)) |
| 244 | continue; |
| 245 | } |
| 246 | |
| 247 | return; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | // Enable runtime and partial unrolling up to the specified size. |
| 252 | UP.Partial = UP.Runtime = true; |
| 253 | UP.PartialThreshold = UP.PartialOptSizeThreshold = MaxOps; |
| 254 | } |
Hal Finkel | 4f7e2c3 | 2013-09-11 19:25:43 +0000 | [diff] [blame] | 255 | |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 256 | //===----------------------------------------------------------------------===// |
| 257 | // |
| 258 | // Calls used by the vectorizers. |
| 259 | // |
| 260 | //===----------------------------------------------------------------------===// |
| 261 | |
| 262 | unsigned BasicTTI::getScalarizationOverhead(Type *Ty, bool Insert, |
| 263 | bool Extract) const { |
| 264 | assert (Ty->isVectorTy() && "Can only scalarize vectors"); |
| 265 | unsigned Cost = 0; |
| 266 | |
| 267 | for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) { |
| 268 | if (Insert) |
| 269 | Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i); |
| 270 | if (Extract) |
| 271 | Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i); |
| 272 | } |
| 273 | |
| 274 | return Cost; |
| 275 | } |
| 276 | |
| 277 | unsigned BasicTTI::getNumberOfRegisters(bool Vector) const { |
| 278 | return 1; |
| 279 | } |
| 280 | |
Nadav Rotem | 14925e6 | 2013-01-09 22:29:00 +0000 | [diff] [blame] | 281 | unsigned BasicTTI::getRegisterBitWidth(bool Vector) const { |
| 282 | return 32; |
| 283 | } |
| 284 | |
Nadav Rotem | 83be7b0 | 2013-01-09 01:15:42 +0000 | [diff] [blame] | 285 | unsigned BasicTTI::getMaximumUnrollFactor() const { |
| 286 | return 1; |
| 287 | } |
| 288 | |
Arnold Schwaighofer | 6bf4f67 | 2013-04-04 23:26:21 +0000 | [diff] [blame] | 289 | unsigned BasicTTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty, |
| 290 | OperandValueKind, |
| 291 | OperandValueKind) const { |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 292 | // Check if any of the operands are vector operands. |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 293 | const TargetLoweringBase *TLI = getTLI(); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 294 | int ISD = TLI->InstructionOpcodeToISD(Opcode); |
| 295 | assert(ISD && "Invalid opcode"); |
| 296 | |
| 297 | std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty); |
| 298 | |
Nadav Rotem | 9eb366a | 2013-04-12 21:15:03 +0000 | [diff] [blame] | 299 | bool IsFloat = Ty->getScalarType()->isFloatingPointTy(); |
Nadav Rotem | 4208200 | 2013-04-14 05:55:18 +0000 | [diff] [blame] | 300 | // Assume that floating point arithmetic operations cost twice as much as |
| 301 | // integer operations. |
Nadav Rotem | 9eb366a | 2013-04-12 21:15:03 +0000 | [diff] [blame] | 302 | unsigned OpCost = (IsFloat ? 2 : 1); |
| 303 | |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 304 | if (TLI->isOperationLegalOrPromote(ISD, LT.second)) { |
| 305 | // The operation is legal. Assume it costs 1. |
Nadav Rotem | 4208200 | 2013-04-14 05:55:18 +0000 | [diff] [blame] | 306 | // 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] | 307 | // overhead to this. |
| 308 | // TODO: Once we have extract/insert subvector cost we need to use them. |
| 309 | if (LT.first > 1) |
Nadav Rotem | 9eb366a | 2013-04-12 21:15:03 +0000 | [diff] [blame] | 310 | return LT.first * 2 * OpCost; |
| 311 | return LT.first * 1 * OpCost; |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | if (!TLI->isOperationExpand(ISD, LT.second)) { |
| 315 | // If the operation is custom lowered then assume |
| 316 | // thare the code is twice as expensive. |
Nadav Rotem | 9eb366a | 2013-04-12 21:15:03 +0000 | [diff] [blame] | 317 | return LT.first * 2 * OpCost; |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | // Else, assume that we need to scalarize this op. |
| 321 | if (Ty->isVectorTy()) { |
| 322 | unsigned Num = Ty->getVectorNumElements(); |
| 323 | unsigned Cost = TopTTI->getArithmeticInstrCost(Opcode, Ty->getScalarType()); |
| 324 | // return the cost of multiple scalar invocation plus the cost of inserting |
| 325 | // and extracting the values. |
| 326 | return getScalarizationOverhead(Ty, true, true) + Num * Cost; |
| 327 | } |
| 328 | |
| 329 | // We don't know anything about this scalar instruction. |
Nadav Rotem | 9eb366a | 2013-04-12 21:15:03 +0000 | [diff] [blame] | 330 | return OpCost; |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 333 | unsigned BasicTTI::getAltShuffleOverhead(Type *Ty) const { |
| 334 | assert(Ty->isVectorTy() && "Can only shuffle vectors"); |
| 335 | unsigned Cost = 0; |
| 336 | // Shuffle cost is equal to the cost of extracting element from its argument |
| 337 | // plus the cost of inserting them onto the result vector. |
| 338 | |
| 339 | // e.g. <4 x float> has a mask of <0,5,2,7> i.e we need to extract from index |
| 340 | // 0 of first vector, index 1 of second vector,index 2 of first vector and |
| 341 | // finally index 3 of second vector and insert them at index <0,1,2,3> of |
| 342 | // result vector. |
| 343 | for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) { |
| 344 | Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i); |
| 345 | Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i); |
| 346 | } |
| 347 | return Cost; |
| 348 | } |
| 349 | |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 350 | unsigned BasicTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index, |
| 351 | Type *SubTp) const { |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 352 | if (Kind == SK_Alternate) { |
| 353 | return getAltShuffleOverhead(Tp); |
| 354 | } |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 355 | return 1; |
| 356 | } |
| 357 | |
| 358 | unsigned BasicTTI::getCastInstrCost(unsigned Opcode, Type *Dst, |
| 359 | Type *Src) 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 | std::pair<unsigned, MVT> SrcLT = TLI->getTypeLegalizationCost(Src); |
| 365 | std::pair<unsigned, MVT> DstLT = TLI->getTypeLegalizationCost(Dst); |
| 366 | |
Nadav Rotem | 3e40d92 | 2013-01-11 19:54:13 +0000 | [diff] [blame] | 367 | // Check for NOOP conversions. |
| 368 | if (SrcLT.first == DstLT.first && |
| 369 | SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) { |
| 370 | |
| 371 | // Bitcast between types that are legalized to the same type are free. |
| 372 | if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc) |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | if (Opcode == Instruction::Trunc && |
| 377 | TLI->isTruncateFree(SrcLT.second, DstLT.second)) |
| 378 | return 0; |
| 379 | |
| 380 | if (Opcode == Instruction::ZExt && |
| 381 | TLI->isZExtFree(SrcLT.second, DstLT.second)) |
| 382 | return 0; |
| 383 | |
| 384 | // 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] | 385 | if (SrcLT.first == DstLT.first && |
| 386 | TLI->isOperationLegalOrPromote(ISD, DstLT.second)) |
Nadav Rotem | 3e40d92 | 2013-01-11 19:54:13 +0000 | [diff] [blame] | 387 | return 1; |
| 388 | |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 389 | // Handle scalar conversions. |
| 390 | if (!Src->isVectorTy() && !Dst->isVectorTy()) { |
| 391 | |
| 392 | // Scalar bitcasts are usually free. |
| 393 | if (Opcode == Instruction::BitCast) |
| 394 | return 0; |
| 395 | |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 396 | // Just check the op cost. If the operation is legal then assume it costs 1. |
| 397 | if (!TLI->isOperationExpand(ISD, DstLT.second)) |
| 398 | return 1; |
| 399 | |
| 400 | // Assume that illegal scalar instruction are expensive. |
| 401 | return 4; |
| 402 | } |
| 403 | |
| 404 | // Check vector-to-vector casts. |
| 405 | if (Dst->isVectorTy() && Src->isVectorTy()) { |
| 406 | |
| 407 | // If the cast is between same-sized registers, then the check is simple. |
| 408 | if (SrcLT.first == DstLT.first && |
| 409 | SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) { |
| 410 | |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 411 | // Assume that Zext is done using AND. |
| 412 | if (Opcode == Instruction::ZExt) |
| 413 | return 1; |
| 414 | |
| 415 | // Assume that sext is done using SHL and SRA. |
| 416 | if (Opcode == Instruction::SExt) |
| 417 | return 2; |
| 418 | |
| 419 | // Just check the op cost. If the operation is legal then assume it costs |
| 420 | // 1 and multiply by the type-legalization overhead. |
| 421 | if (!TLI->isOperationExpand(ISD, DstLT.second)) |
| 422 | return SrcLT.first * 1; |
| 423 | } |
| 424 | |
| 425 | // If we are converting vectors and the operation is illegal, or |
| 426 | // if the vectors are legalized to different types, estimate the |
| 427 | // scalarization costs. |
| 428 | unsigned Num = Dst->getVectorNumElements(); |
| 429 | unsigned Cost = TopTTI->getCastInstrCost(Opcode, Dst->getScalarType(), |
| 430 | Src->getScalarType()); |
| 431 | |
| 432 | // Return the cost of multiple scalar invocation plus the cost of |
| 433 | // inserting and extracting the values. |
| 434 | return getScalarizationOverhead(Dst, true, true) + Num * Cost; |
| 435 | } |
| 436 | |
| 437 | // We already handled vector-to-vector and scalar-to-scalar conversions. This |
| 438 | // is where we handle bitcast between vectors and scalars. We need to assume |
| 439 | // that the conversion is scalarized in one way or another. |
| 440 | if (Opcode == Instruction::BitCast) |
| 441 | // Illegal bitcasts are done by storing and loading from a stack slot. |
| 442 | return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) + |
| 443 | (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0); |
| 444 | |
| 445 | llvm_unreachable("Unhandled cast"); |
| 446 | } |
| 447 | |
| 448 | unsigned BasicTTI::getCFInstrCost(unsigned Opcode) const { |
| 449 | // Branches are assumed to be predicted. |
| 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | unsigned BasicTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, |
| 454 | Type *CondTy) const { |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 455 | const TargetLoweringBase *TLI = getTLI(); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 456 | int ISD = TLI->InstructionOpcodeToISD(Opcode); |
| 457 | assert(ISD && "Invalid opcode"); |
| 458 | |
| 459 | // Selects on vectors are actually vector selects. |
| 460 | if (ISD == ISD::SELECT) { |
| 461 | assert(CondTy && "CondTy must exist"); |
| 462 | if (CondTy->isVectorTy()) |
| 463 | ISD = ISD::VSELECT; |
| 464 | } |
| 465 | |
| 466 | std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy); |
| 467 | |
| 468 | if (!TLI->isOperationExpand(ISD, LT.second)) { |
| 469 | // The operation is legal. Assume it costs 1. Multiply |
| 470 | // by the type-legalization overhead. |
| 471 | return LT.first * 1; |
| 472 | } |
| 473 | |
| 474 | // Otherwise, assume that the cast is scalarized. |
| 475 | if (ValTy->isVectorTy()) { |
| 476 | unsigned Num = ValTy->getVectorNumElements(); |
| 477 | if (CondTy) |
| 478 | CondTy = CondTy->getScalarType(); |
| 479 | unsigned Cost = TopTTI->getCmpSelInstrCost(Opcode, ValTy->getScalarType(), |
| 480 | CondTy); |
| 481 | |
| 482 | // Return the cost of multiple scalar invocation plus the cost of inserting |
| 483 | // and extracting the values. |
| 484 | return getScalarizationOverhead(ValTy, true, false) + Num * Cost; |
| 485 | } |
| 486 | |
| 487 | // Unknown scalar opcode. |
| 488 | return 1; |
| 489 | } |
| 490 | |
| 491 | unsigned BasicTTI::getVectorInstrCost(unsigned Opcode, Type *Val, |
| 492 | unsigned Index) const { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 493 | std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Val->getScalarType()); |
| 494 | |
| 495 | return LT.first; |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | unsigned BasicTTI::getMemoryOpCost(unsigned Opcode, Type *Src, |
| 499 | unsigned Alignment, |
| 500 | unsigned AddressSpace) const { |
| 501 | assert(!Src->isVoidTy() && "Invalid type"); |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 502 | std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Src); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 503 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 504 | // Assuming that all loads of legal types cost 1. |
| 505 | unsigned Cost = LT.first; |
| 506 | |
| 507 | if (Src->isVectorTy() && |
| 508 | Src->getPrimitiveSizeInBits() < LT.second.getSizeInBits()) { |
| 509 | // This is a vector load that legalizes to a larger type than the vector |
| 510 | // itself. Unless the corresponding extending load or truncating store is |
| 511 | // legal, then this will scalarize. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 512 | TargetLowering::LegalizeAction LA = TargetLowering::Expand; |
| 513 | EVT MemVT = getTLI()->getValueType(Src, true); |
| 514 | if (MemVT.isSimple() && MemVT != MVT::Other) { |
| 515 | if (Opcode == Instruction::Store) |
| 516 | LA = getTLI()->getTruncStoreAction(LT.second, MemVT.getSimpleVT()); |
| 517 | else |
| 518 | LA = getTLI()->getLoadExtAction(ISD::EXTLOAD, MemVT.getSimpleVT()); |
| 519 | } |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 520 | |
| 521 | if (LA != TargetLowering::Legal && LA != TargetLowering::Custom) { |
| 522 | // This is a vector load/store for some illegal type that is scalarized. |
| 523 | // We must account for the cost of building or decomposing the vector. |
| 524 | Cost += getScalarizationOverhead(Src, Opcode != Instruction::Store, |
| 525 | Opcode == Instruction::Store); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | return Cost; |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Benjamin Kramer | 8611d44 | 2013-02-28 19:09:33 +0000 | [diff] [blame] | 532 | unsigned BasicTTI::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy, |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 533 | ArrayRef<Type *> Tys) const { |
Benjamin Kramer | 8611d44 | 2013-02-28 19:09:33 +0000 | [diff] [blame] | 534 | unsigned ISD = 0; |
| 535 | switch (IID) { |
| 536 | default: { |
| 537 | // Assume that we need to scalarize this intrinsic. |
| 538 | unsigned ScalarizationCost = 0; |
| 539 | unsigned ScalarCalls = 1; |
| 540 | if (RetTy->isVectorTy()) { |
| 541 | ScalarizationCost = getScalarizationOverhead(RetTy, true, false); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 542 | ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements()); |
| 543 | } |
Benjamin Kramer | 8611d44 | 2013-02-28 19:09:33 +0000 | [diff] [blame] | 544 | for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) { |
| 545 | if (Tys[i]->isVectorTy()) { |
| 546 | ScalarizationCost += getScalarizationOverhead(Tys[i], false, true); |
| 547 | ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements()); |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | return ScalarCalls + ScalarizationCost; |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 552 | } |
Benjamin Kramer | 8611d44 | 2013-02-28 19:09:33 +0000 | [diff] [blame] | 553 | // Look for intrinsics that can be lowered directly or turned into a scalar |
| 554 | // intrinsic call. |
| 555 | case Intrinsic::sqrt: ISD = ISD::FSQRT; break; |
| 556 | case Intrinsic::sin: ISD = ISD::FSIN; break; |
| 557 | case Intrinsic::cos: ISD = ISD::FCOS; break; |
| 558 | case Intrinsic::exp: ISD = ISD::FEXP; break; |
| 559 | case Intrinsic::exp2: ISD = ISD::FEXP2; break; |
| 560 | case Intrinsic::log: ISD = ISD::FLOG; break; |
| 561 | case Intrinsic::log10: ISD = ISD::FLOG10; break; |
| 562 | case Intrinsic::log2: ISD = ISD::FLOG2; break; |
| 563 | case Intrinsic::fabs: ISD = ISD::FABS; break; |
Hal Finkel | 66d1fa6 | 2013-08-19 23:35:46 +0000 | [diff] [blame] | 564 | case Intrinsic::copysign: ISD = ISD::FCOPYSIGN; break; |
Benjamin Kramer | 8611d44 | 2013-02-28 19:09:33 +0000 | [diff] [blame] | 565 | case Intrinsic::floor: ISD = ISD::FFLOOR; break; |
| 566 | case Intrinsic::ceil: ISD = ISD::FCEIL; break; |
| 567 | case Intrinsic::trunc: ISD = ISD::FTRUNC; break; |
Hal Finkel | 04b84c2 | 2013-07-08 03:24:07 +0000 | [diff] [blame] | 568 | case Intrinsic::nearbyint: |
| 569 | ISD = ISD::FNEARBYINT; break; |
Benjamin Kramer | 8611d44 | 2013-02-28 19:09:33 +0000 | [diff] [blame] | 570 | case Intrinsic::rint: ISD = ISD::FRINT; break; |
Hal Finkel | 41418d1 | 2013-08-07 22:49:12 +0000 | [diff] [blame] | 571 | case Intrinsic::round: ISD = ISD::FROUND; break; |
Benjamin Kramer | 8611d44 | 2013-02-28 19:09:33 +0000 | [diff] [blame] | 572 | case Intrinsic::pow: ISD = ISD::FPOW; break; |
| 573 | case Intrinsic::fma: ISD = ISD::FMA; break; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 574 | case Intrinsic::fmuladd: ISD = ISD::FMA; break; |
Arnold Schwaighofer | 2d66d4c | 2013-08-06 22:37:52 +0000 | [diff] [blame] | 575 | case Intrinsic::lifetime_start: |
| 576 | case Intrinsic::lifetime_end: |
| 577 | return 0; |
Benjamin Kramer | 8611d44 | 2013-02-28 19:09:33 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 580 | const TargetLoweringBase *TLI = getTLI(); |
Benjamin Kramer | 8611d44 | 2013-02-28 19:09:33 +0000 | [diff] [blame] | 581 | std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(RetTy); |
| 582 | |
| 583 | if (TLI->isOperationLegalOrPromote(ISD, LT.second)) { |
| 584 | // The operation is legal. Assume it costs 1. |
| 585 | // If the type is split to multiple registers, assume that thre is some |
| 586 | // overhead to this. |
| 587 | // TODO: Once we have extract/insert subvector cost we need to use them. |
| 588 | if (LT.first > 1) |
| 589 | return LT.first * 2; |
| 590 | return LT.first * 1; |
| 591 | } |
| 592 | |
| 593 | if (!TLI->isOperationExpand(ISD, LT.second)) { |
| 594 | // If the operation is custom lowered then assume |
| 595 | // thare the code is twice as expensive. |
| 596 | return LT.first * 2; |
| 597 | } |
| 598 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 599 | // If we can't lower fmuladd into an FMA estimate the cost as a floating |
| 600 | // point mul followed by an add. |
| 601 | if (IID == Intrinsic::fmuladd) |
| 602 | return TopTTI->getArithmeticInstrCost(BinaryOperator::FMul, RetTy) + |
| 603 | TopTTI->getArithmeticInstrCost(BinaryOperator::FAdd, RetTy); |
| 604 | |
Benjamin Kramer | 8611d44 | 2013-02-28 19:09:33 +0000 | [diff] [blame] | 605 | // Else, assume that we need to scalarize this intrinsic. For math builtins |
| 606 | // this will emit a costly libcall, adding call overhead and spills. Make it |
| 607 | // very expensive. |
| 608 | if (RetTy->isVectorTy()) { |
| 609 | unsigned Num = RetTy->getVectorNumElements(); |
| 610 | unsigned Cost = TopTTI->getIntrinsicInstrCost(IID, RetTy->getScalarType(), |
| 611 | Tys); |
| 612 | return 10 * Cost * Num; |
| 613 | } |
| 614 | |
| 615 | // This is going to be turned into a library call, make it expensive. |
| 616 | return 10; |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | unsigned BasicTTI::getNumberOfParts(Type *Tp) const { |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 620 | std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Tp); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 621 | return LT.first; |
| 622 | } |
Arnold Schwaighofer | fb55a8f | 2013-02-08 14:50:48 +0000 | [diff] [blame] | 623 | |
Arnold Schwaighofer | c0a11ed | 2013-07-12 19:16:02 +0000 | [diff] [blame] | 624 | unsigned BasicTTI::getAddressComputationCost(Type *Ty, bool IsComplex) const { |
Arnold Schwaighofer | fb55a8f | 2013-02-08 14:50:48 +0000 | [diff] [blame] | 625 | return 0; |
| 626 | } |
Arnold Schwaighofer | 65457b6 | 2013-09-17 18:06:50 +0000 | [diff] [blame] | 627 | |
| 628 | unsigned BasicTTI::getReductionCost(unsigned Opcode, Type *Ty, |
| 629 | bool IsPairwise) const { |
| 630 | assert(Ty->isVectorTy() && "Expect a vector type"); |
| 631 | unsigned NumVecElts = Ty->getVectorNumElements(); |
| 632 | unsigned NumReduxLevels = Log2_32(NumVecElts); |
| 633 | unsigned ArithCost = NumReduxLevels * |
| 634 | TopTTI->getArithmeticInstrCost(Opcode, Ty); |
| 635 | // Assume the pairwise shuffles add a cost. |
| 636 | unsigned ShuffleCost = |
| 637 | NumReduxLevels * (IsPairwise + 1) * |
| 638 | TopTTI->getShuffleCost(SK_ExtractSubvector, Ty, NumVecElts / 2, Ty); |
| 639 | return ShuffleCost + ArithCost + getScalarizationOverhead(Ty, false, true); |
| 640 | } |