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