blob: 3e0e09e658e0fea0377321c93c2ff2df34057672 [file] [log] [blame]
Chandler Carruth664e3542013-01-07 01:37:14 +00001//===- 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 Carruth664e3542013-01-07 01:37:14 +000018#include "llvm/CodeGen/Passes.h"
Hal Finkel6532c202014-05-08 09:14:44 +000019#include "llvm/Analysis/LoopInfo.h"
Chandler Carruthd3e73552013-01-07 03:08:10 +000020#include "llvm/Analysis/TargetTransformInfo.h"
Hal Finkel6532c202014-05-08 09:14:44 +000021#include "llvm/Support/CommandLine.h"
Chandler Carruth664e3542013-01-07 01:37:14 +000022#include "llvm/Target/TargetLowering.h"
Hal Finkel6532c202014-05-08 09:14:44 +000023#include "llvm/Target/TargetSubtargetInfo.h"
Chandler Carruth664e3542013-01-07 01:37:14 +000024#include <utility>
Chandler Carruth664e3542013-01-07 01:37:14 +000025using namespace llvm;
26
Hal Finkel6532c202014-05-08 09:14:44 +000027static cl::opt<unsigned>
28PartialUnrollingThreshold("partial-unrolling-threshold", cl::init(0),
29 cl::desc("Threshold for partial unrolling"), cl::Hidden);
30
Chandler Carruth1b9dde02014-04-22 02:02:50 +000031#define DEBUG_TYPE "basictti"
32
Chandler Carruth664e3542013-01-07 01:37:14 +000033namespace {
34
Craig Topper77dfe452014-03-02 08:08:51 +000035class BasicTTI final : public ImmutablePass, public TargetTransformInfo {
Bill Wendlingafc10362013-06-19 20:51:24 +000036 const TargetMachine *TM;
Chandler Carruth664e3542013-01-07 01:37:14 +000037
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 Bhate03a25d2014-06-20 04:32:48 +000042 /// Estimate the cost overhead of SK_Alternate shuffle.
43 unsigned getAltShuffleOverhead(Type *Ty) const;
44
Eric Christopherd9134482014-08-04 21:25:23 +000045 const TargetLoweringBase *getTLI() const {
46 return TM->getSubtargetImpl()->getTargetLowering();
47 }
Bill Wendlingafc10362013-06-19 20:51:24 +000048
Chandler Carruth664e3542013-01-07 01:37:14 +000049public:
Craig Topperc0196b12014-04-14 00:51:57 +000050 BasicTTI() : ImmutablePass(ID), TM(nullptr) {
Chandler Carruth664e3542013-01-07 01:37:14 +000051 llvm_unreachable("This pass cannot be directly constructed");
52 }
53
Bill Wendlingafc10362013-06-19 20:51:24 +000054 BasicTTI(const TargetMachine *TM) : ImmutablePass(ID), TM(TM) {
Chandler Carruth664e3542013-01-07 01:37:14 +000055 initializeBasicTTIPass(*PassRegistry::getPassRegistry());
56 }
57
Craig Topper24e685f2014-03-10 05:29:18 +000058 void initializePass() override {
Chandler Carruth664e3542013-01-07 01:37:14 +000059 pushTTIStack(this);
60 }
61
Craig Topper24e685f2014-03-10 05:29:18 +000062 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruth664e3542013-01-07 01:37:14 +000063 TargetTransformInfo::getAnalysisUsage(AU);
64 }
65
66 /// Pass identification.
67 static char ID;
68
69 /// Provide necessary pointer adjustments for the two base classes.
Craig Topper24e685f2014-03-10 05:29:18 +000070 void *getAdjustedAnalysisPointer(const void *ID) override {
Chandler Carruth664e3542013-01-07 01:37:14 +000071 if (ID == &TargetTransformInfo::ID)
72 return (TargetTransformInfo*)this;
73 return this;
74 }
75
Craig Topper24e685f2014-03-10 05:29:18 +000076 bool hasBranchDivergence() const override;
Tom Stellard8b1e0212013-07-27 00:01:07 +000077
Chandler Carruth664e3542013-01-07 01:37:14 +000078 /// \name Scalar TTI Implementations
79 /// @{
80
Craig Topper24e685f2014-03-10 05:29:18 +000081 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 Carruth664e3542013-01-07 01:37:14 +000097
98 /// @}
99
100 /// \name Vector TTI Implementations
101 /// @{
102
Craig Topper24e685f2014-03-10 05:29:18 +0000103 unsigned getNumberOfRegisters(bool Vector) const override;
Sanjay Patelb653de12014-09-10 17:58:16 +0000104 unsigned getMaxInterleaveFactor() const override;
Craig Topper24e685f2014-03-10 05:29:18 +0000105 unsigned getRegisterBitWidth(bool Vector) const override;
106 unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty, OperandValueKind,
Karthik Bhat7f33ff72014-08-25 04:56:54 +0000107 OperandValueKind, OperandValueProperties,
108 OperandValueProperties) const override;
Craig Topper24e685f2014-03-10 05:29:18 +0000109 unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
110 int Index, Type *SubTp) const override;
111 unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
112 Type *Src) const override;
113 unsigned getCFInstrCost(unsigned Opcode) const override;
114 unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
115 Type *CondTy) const override;
116 unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
117 unsigned Index) const override;
118 unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
119 unsigned AddressSpace) const override;
120 unsigned getIntrinsicInstrCost(Intrinsic::ID, Type *RetTy,
121 ArrayRef<Type*> Tys) const override;
122 unsigned getNumberOfParts(Type *Tp) const override;
123 unsigned getAddressComputationCost( Type *Ty, bool IsComplex) const override;
124 unsigned getReductionCost(unsigned Opcode, Type *Ty,
125 bool IsPairwise) const override;
Chandler Carruth664e3542013-01-07 01:37:14 +0000126
127 /// @}
128};
129
130}
131
132INITIALIZE_AG_PASS(BasicTTI, TargetTransformInfo, "basictti",
133 "Target independent code generator's TTI", true, true, false)
134char BasicTTI::ID = 0;
135
136ImmutablePass *
Bill Wendlingafc10362013-06-19 20:51:24 +0000137llvm::createBasicTargetTransformInfoPass(const TargetMachine *TM) {
138 return new BasicTTI(TM);
Chandler Carruth664e3542013-01-07 01:37:14 +0000139}
140
Tom Stellard8b1e0212013-07-27 00:01:07 +0000141bool BasicTTI::hasBranchDivergence() const { return false; }
Chandler Carruth664e3542013-01-07 01:37:14 +0000142
143bool BasicTTI::isLegalAddImmediate(int64_t imm) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000144 return getTLI()->isLegalAddImmediate(imm);
Chandler Carruth664e3542013-01-07 01:37:14 +0000145}
146
147bool BasicTTI::isLegalICmpImmediate(int64_t imm) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000148 return getTLI()->isLegalICmpImmediate(imm);
Chandler Carruth664e3542013-01-07 01:37:14 +0000149}
150
151bool BasicTTI::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
152 int64_t BaseOffset, bool HasBaseReg,
153 int64_t Scale) const {
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000154 TargetLoweringBase::AddrMode AM;
Chandler Carruth664e3542013-01-07 01:37:14 +0000155 AM.BaseGV = BaseGV;
156 AM.BaseOffs = BaseOffset;
157 AM.HasBaseReg = HasBaseReg;
158 AM.Scale = Scale;
Bill Wendlingafc10362013-06-19 20:51:24 +0000159 return getTLI()->isLegalAddressingMode(AM, Ty);
Chandler Carruth664e3542013-01-07 01:37:14 +0000160}
161
Quentin Colombetbf490d42013-05-31 21:29:03 +0000162int BasicTTI::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
163 int64_t BaseOffset, bool HasBaseReg,
164 int64_t Scale) const {
165 TargetLoweringBase::AddrMode AM;
166 AM.BaseGV = BaseGV;
167 AM.BaseOffs = BaseOffset;
168 AM.HasBaseReg = HasBaseReg;
169 AM.Scale = Scale;
Bill Wendlingafc10362013-06-19 20:51:24 +0000170 return getTLI()->getScalingFactorCost(AM, Ty);
Quentin Colombetbf490d42013-05-31 21:29:03 +0000171}
172
Chandler Carruth664e3542013-01-07 01:37:14 +0000173bool BasicTTI::isTruncateFree(Type *Ty1, Type *Ty2) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000174 return getTLI()->isTruncateFree(Ty1, Ty2);
Chandler Carruth664e3542013-01-07 01:37:14 +0000175}
176
177bool BasicTTI::isTypeLegal(Type *Ty) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000178 EVT T = getTLI()->getValueType(Ty);
179 return getTLI()->isTypeLegal(T);
Chandler Carruth664e3542013-01-07 01:37:14 +0000180}
181
182unsigned BasicTTI::getJumpBufAlignment() const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000183 return getTLI()->getJumpBufAlignment();
Chandler Carruth664e3542013-01-07 01:37:14 +0000184}
185
186unsigned BasicTTI::getJumpBufSize() const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000187 return getTLI()->getJumpBufSize();
Chandler Carruth664e3542013-01-07 01:37:14 +0000188}
189
190bool BasicTTI::shouldBuildLookupTables() const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000191 const TargetLoweringBase *TLI = getTLI();
Eric Christopher79cc1e32014-09-02 22:28:02 +0000192 return TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
193 TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other);
Chandler Carruth664e3542013-01-07 01:37:14 +0000194}
195
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000196bool 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 Finkel6532c202014-05-08 09:14:44 +0000202void BasicTTI::getUnrollingPreferences(Loop *L,
203 UnrollingPreferences &UP) const {
204 // This unrolling functionality is target independent, but to provide some
Hal Finkele8172d82014-05-08 13:42:57 +0000205 // motivation for its intended use, for x86:
Hal Finkel6532c202014-05-08 09:14:44 +0000206
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;
Pete Cooper11759452014-09-02 17:43:54 +0000231 else if (ST->getSchedModel().LoopMicroOpBufferSize > 0)
232 MaxOps = ST->getSchedModel().LoopMicroOpBufferSize;
Hal Finkel6532c202014-05-08 09:14:44 +0000233 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 Finkel8f2e7002013-09-11 19:25:43 +0000257
Chandler Carruth664e3542013-01-07 01:37:14 +0000258//===----------------------------------------------------------------------===//
259//
260// Calls used by the vectorizers.
261//
262//===----------------------------------------------------------------------===//
263
264unsigned 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
279unsigned BasicTTI::getNumberOfRegisters(bool Vector) const {
280 return 1;
281}
282
Nadav Rotemb1791a72013-01-09 22:29:00 +0000283unsigned BasicTTI::getRegisterBitWidth(bool Vector) const {
284 return 32;
285}
286
Sanjay Patelb653de12014-09-10 17:58:16 +0000287unsigned BasicTTI::getMaxInterleaveFactor() const {
Nadav Rotemb696c362013-01-09 01:15:42 +0000288 return 1;
289}
290
Arnold Schwaighoferb9773872013-04-04 23:26:21 +0000291unsigned BasicTTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty,
Karthik Bhat7f33ff72014-08-25 04:56:54 +0000292 OperandValueKind, OperandValueKind,
293 OperandValueProperties,
294 OperandValueProperties) const {
Chandler Carruth664e3542013-01-07 01:37:14 +0000295 // Check if any of the operands are vector operands.
Bill Wendlingafc10362013-06-19 20:51:24 +0000296 const TargetLoweringBase *TLI = getTLI();
Chandler Carruth664e3542013-01-07 01:37:14 +0000297 int ISD = TLI->InstructionOpcodeToISD(Opcode);
298 assert(ISD && "Invalid opcode");
299
300 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
301
Nadav Rotem87a0af62013-04-12 21:15:03 +0000302 bool IsFloat = Ty->getScalarType()->isFloatingPointTy();
Nadav Rotem0db06902013-04-14 05:55:18 +0000303 // Assume that floating point arithmetic operations cost twice as much as
304 // integer operations.
Nadav Rotem87a0af62013-04-12 21:15:03 +0000305 unsigned OpCost = (IsFloat ? 2 : 1);
306
Chandler Carruth664e3542013-01-07 01:37:14 +0000307 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
308 // The operation is legal. Assume it costs 1.
Nadav Rotem0db06902013-04-14 05:55:18 +0000309 // If the type is split to multiple registers, assume that there is some
Chandler Carruth664e3542013-01-07 01:37:14 +0000310 // overhead to this.
311 // TODO: Once we have extract/insert subvector cost we need to use them.
312 if (LT.first > 1)
Nadav Rotem87a0af62013-04-12 21:15:03 +0000313 return LT.first * 2 * OpCost;
314 return LT.first * 1 * OpCost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000315 }
316
317 if (!TLI->isOperationExpand(ISD, LT.second)) {
318 // If the operation is custom lowered then assume
319 // thare the code is twice as expensive.
Nadav Rotem87a0af62013-04-12 21:15:03 +0000320 return LT.first * 2 * OpCost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000321 }
322
323 // Else, assume that we need to scalarize this op.
324 if (Ty->isVectorTy()) {
325 unsigned Num = Ty->getVectorNumElements();
326 unsigned Cost = TopTTI->getArithmeticInstrCost(Opcode, Ty->getScalarType());
327 // return the cost of multiple scalar invocation plus the cost of inserting
328 // and extracting the values.
329 return getScalarizationOverhead(Ty, true, true) + Num * Cost;
330 }
331
332 // We don't know anything about this scalar instruction.
Nadav Rotem87a0af62013-04-12 21:15:03 +0000333 return OpCost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000334}
335
Karthik Bhate03a25d2014-06-20 04:32:48 +0000336unsigned BasicTTI::getAltShuffleOverhead(Type *Ty) const {
337 assert(Ty->isVectorTy() && "Can only shuffle vectors");
338 unsigned Cost = 0;
339 // Shuffle cost is equal to the cost of extracting element from its argument
340 // plus the cost of inserting them onto the result vector.
341
342 // e.g. <4 x float> has a mask of <0,5,2,7> i.e we need to extract from index
343 // 0 of first vector, index 1 of second vector,index 2 of first vector and
344 // finally index 3 of second vector and insert them at index <0,1,2,3> of
345 // result vector.
346 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
347 Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
348 Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
349 }
350 return Cost;
351}
352
Chandler Carruth664e3542013-01-07 01:37:14 +0000353unsigned BasicTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
354 Type *SubTp) const {
Karthik Bhate03a25d2014-06-20 04:32:48 +0000355 if (Kind == SK_Alternate) {
356 return getAltShuffleOverhead(Tp);
357 }
Chandler Carruth664e3542013-01-07 01:37:14 +0000358 return 1;
359}
360
361unsigned BasicTTI::getCastInstrCost(unsigned Opcode, Type *Dst,
362 Type *Src) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000363 const TargetLoweringBase *TLI = getTLI();
Chandler Carruth664e3542013-01-07 01:37:14 +0000364 int ISD = TLI->InstructionOpcodeToISD(Opcode);
365 assert(ISD && "Invalid opcode");
366
367 std::pair<unsigned, MVT> SrcLT = TLI->getTypeLegalizationCost(Src);
368 std::pair<unsigned, MVT> DstLT = TLI->getTypeLegalizationCost(Dst);
369
Nadav Roteme55aa3c2013-01-11 19:54:13 +0000370 // Check for NOOP conversions.
371 if (SrcLT.first == DstLT.first &&
372 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
373
374 // Bitcast between types that are legalized to the same type are free.
375 if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc)
376 return 0;
377 }
378
379 if (Opcode == Instruction::Trunc &&
380 TLI->isTruncateFree(SrcLT.second, DstLT.second))
381 return 0;
382
383 if (Opcode == Instruction::ZExt &&
384 TLI->isZExtFree(SrcLT.second, DstLT.second))
385 return 0;
386
387 // If the cast is marked as legal (or promote) then assume low cost.
Hal Finkel55312de2014-04-02 23:18:54 +0000388 if (SrcLT.first == DstLT.first &&
389 TLI->isOperationLegalOrPromote(ISD, DstLT.second))
Nadav Roteme55aa3c2013-01-11 19:54:13 +0000390 return 1;
391
Chandler Carruth664e3542013-01-07 01:37:14 +0000392 // Handle scalar conversions.
393 if (!Src->isVectorTy() && !Dst->isVectorTy()) {
394
395 // Scalar bitcasts are usually free.
396 if (Opcode == Instruction::BitCast)
397 return 0;
398
Chandler Carruth664e3542013-01-07 01:37:14 +0000399 // Just check the op cost. If the operation is legal then assume it costs 1.
400 if (!TLI->isOperationExpand(ISD, DstLT.second))
401 return 1;
402
403 // Assume that illegal scalar instruction are expensive.
404 return 4;
405 }
406
407 // Check vector-to-vector casts.
408 if (Dst->isVectorTy() && Src->isVectorTy()) {
409
410 // If the cast is between same-sized registers, then the check is simple.
411 if (SrcLT.first == DstLT.first &&
412 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
413
Chandler Carruth664e3542013-01-07 01:37:14 +0000414 // Assume that Zext is done using AND.
415 if (Opcode == Instruction::ZExt)
416 return 1;
417
418 // Assume that sext is done using SHL and SRA.
419 if (Opcode == Instruction::SExt)
420 return 2;
421
422 // Just check the op cost. If the operation is legal then assume it costs
423 // 1 and multiply by the type-legalization overhead.
424 if (!TLI->isOperationExpand(ISD, DstLT.second))
425 return SrcLT.first * 1;
426 }
427
428 // If we are converting vectors and the operation is illegal, or
429 // if the vectors are legalized to different types, estimate the
430 // scalarization costs.
431 unsigned Num = Dst->getVectorNumElements();
432 unsigned Cost = TopTTI->getCastInstrCost(Opcode, Dst->getScalarType(),
433 Src->getScalarType());
434
435 // Return the cost of multiple scalar invocation plus the cost of
436 // inserting and extracting the values.
437 return getScalarizationOverhead(Dst, true, true) + Num * Cost;
438 }
439
440 // We already handled vector-to-vector and scalar-to-scalar conversions. This
441 // is where we handle bitcast between vectors and scalars. We need to assume
442 // that the conversion is scalarized in one way or another.
443 if (Opcode == Instruction::BitCast)
444 // Illegal bitcasts are done by storing and loading from a stack slot.
445 return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) +
446 (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0);
447
448 llvm_unreachable("Unhandled cast");
449 }
450
451unsigned BasicTTI::getCFInstrCost(unsigned Opcode) const {
452 // Branches are assumed to be predicted.
453 return 0;
454}
455
456unsigned BasicTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
457 Type *CondTy) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000458 const TargetLoweringBase *TLI = getTLI();
Chandler Carruth664e3542013-01-07 01:37:14 +0000459 int ISD = TLI->InstructionOpcodeToISD(Opcode);
460 assert(ISD && "Invalid opcode");
461
462 // Selects on vectors are actually vector selects.
463 if (ISD == ISD::SELECT) {
464 assert(CondTy && "CondTy must exist");
465 if (CondTy->isVectorTy())
466 ISD = ISD::VSELECT;
467 }
468
469 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
470
471 if (!TLI->isOperationExpand(ISD, LT.second)) {
472 // The operation is legal. Assume it costs 1. Multiply
473 // by the type-legalization overhead.
474 return LT.first * 1;
475 }
476
477 // Otherwise, assume that the cast is scalarized.
478 if (ValTy->isVectorTy()) {
479 unsigned Num = ValTy->getVectorNumElements();
480 if (CondTy)
481 CondTy = CondTy->getScalarType();
482 unsigned Cost = TopTTI->getCmpSelInstrCost(Opcode, ValTy->getScalarType(),
483 CondTy);
484
485 // Return the cost of multiple scalar invocation plus the cost of inserting
486 // and extracting the values.
487 return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
488 }
489
490 // Unknown scalar opcode.
491 return 1;
492}
493
494unsigned BasicTTI::getVectorInstrCost(unsigned Opcode, Type *Val,
495 unsigned Index) const {
Raul E. Silverace376c02014-03-10 22:59:13 +0000496 std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Val->getScalarType());
497
498 return LT.first;
Chandler Carruth664e3542013-01-07 01:37:14 +0000499}
500
501unsigned BasicTTI::getMemoryOpCost(unsigned Opcode, Type *Src,
502 unsigned Alignment,
503 unsigned AddressSpace) const {
504 assert(!Src->isVoidTy() && "Invalid type");
Bill Wendlingafc10362013-06-19 20:51:24 +0000505 std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Src);
Chandler Carruth664e3542013-01-07 01:37:14 +0000506
Hal Finkel6fd19ab2014-04-03 00:53:59 +0000507 // Assuming that all loads of legal types cost 1.
508 unsigned Cost = LT.first;
509
510 if (Src->isVectorTy() &&
511 Src->getPrimitiveSizeInBits() < LT.second.getSizeInBits()) {
512 // This is a vector load that legalizes to a larger type than the vector
513 // itself. Unless the corresponding extending load or truncating store is
514 // legal, then this will scalarize.
Hal Finkel56bf2972014-04-14 05:59:09 +0000515 TargetLowering::LegalizeAction LA = TargetLowering::Expand;
516 EVT MemVT = getTLI()->getValueType(Src, true);
517 if (MemVT.isSimple() && MemVT != MVT::Other) {
518 if (Opcode == Instruction::Store)
519 LA = getTLI()->getTruncStoreAction(LT.second, MemVT.getSimpleVT());
520 else
521 LA = getTLI()->getLoadExtAction(ISD::EXTLOAD, MemVT.getSimpleVT());
522 }
Hal Finkel6fd19ab2014-04-03 00:53:59 +0000523
524 if (LA != TargetLowering::Legal && LA != TargetLowering::Custom) {
525 // This is a vector load/store for some illegal type that is scalarized.
526 // We must account for the cost of building or decomposing the vector.
527 Cost += getScalarizationOverhead(Src, Opcode != Instruction::Store,
528 Opcode == Instruction::Store);
529 }
530 }
531
532 return Cost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000533}
534
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000535unsigned BasicTTI::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy,
Chandler Carruth664e3542013-01-07 01:37:14 +0000536 ArrayRef<Type *> Tys) const {
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000537 unsigned ISD = 0;
538 switch (IID) {
539 default: {
540 // Assume that we need to scalarize this intrinsic.
541 unsigned ScalarizationCost = 0;
542 unsigned ScalarCalls = 1;
543 if (RetTy->isVectorTy()) {
544 ScalarizationCost = getScalarizationOverhead(RetTy, true, false);
Chandler Carruth664e3542013-01-07 01:37:14 +0000545 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
546 }
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000547 for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) {
548 if (Tys[i]->isVectorTy()) {
549 ScalarizationCost += getScalarizationOverhead(Tys[i], false, true);
550 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
551 }
552 }
553
554 return ScalarCalls + ScalarizationCost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000555 }
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000556 // Look for intrinsics that can be lowered directly or turned into a scalar
557 // intrinsic call.
558 case Intrinsic::sqrt: ISD = ISD::FSQRT; break;
559 case Intrinsic::sin: ISD = ISD::FSIN; break;
560 case Intrinsic::cos: ISD = ISD::FCOS; break;
561 case Intrinsic::exp: ISD = ISD::FEXP; break;
562 case Intrinsic::exp2: ISD = ISD::FEXP2; break;
563 case Intrinsic::log: ISD = ISD::FLOG; break;
564 case Intrinsic::log10: ISD = ISD::FLOG10; break;
565 case Intrinsic::log2: ISD = ISD::FLOG2; break;
566 case Intrinsic::fabs: ISD = ISD::FABS; break;
Hal Finkel0c5c01aa2013-08-19 23:35:46 +0000567 case Intrinsic::copysign: ISD = ISD::FCOPYSIGN; break;
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000568 case Intrinsic::floor: ISD = ISD::FFLOOR; break;
569 case Intrinsic::ceil: ISD = ISD::FCEIL; break;
570 case Intrinsic::trunc: ISD = ISD::FTRUNC; break;
Hal Finkelec474f22013-07-08 03:24:07 +0000571 case Intrinsic::nearbyint:
572 ISD = ISD::FNEARBYINT; break;
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000573 case Intrinsic::rint: ISD = ISD::FRINT; break;
Hal Finkel171817e2013-08-07 22:49:12 +0000574 case Intrinsic::round: ISD = ISD::FROUND; break;
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000575 case Intrinsic::pow: ISD = ISD::FPOW; break;
576 case Intrinsic::fma: ISD = ISD::FMA; break;
Benjamin Kramer1625bfc2014-05-06 18:36:23 +0000577 case Intrinsic::fmuladd: ISD = ISD::FMA; break;
Hal Finkel93046912014-07-25 21:13:35 +0000578 // FIXME: We should return 0 whenever getIntrinsicCost == TCC_Free.
Arnold Schwaighofera7cd6bf2013-08-06 22:37:52 +0000579 case Intrinsic::lifetime_start:
580 case Intrinsic::lifetime_end:
581 return 0;
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000582 }
583
Bill Wendlingafc10362013-06-19 20:51:24 +0000584 const TargetLoweringBase *TLI = getTLI();
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000585 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(RetTy);
586
587 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
588 // The operation is legal. Assume it costs 1.
589 // If the type is split to multiple registers, assume that thre is some
590 // overhead to this.
591 // TODO: Once we have extract/insert subvector cost we need to use them.
592 if (LT.first > 1)
593 return LT.first * 2;
594 return LT.first * 1;
595 }
596
597 if (!TLI->isOperationExpand(ISD, LT.second)) {
598 // If the operation is custom lowered then assume
599 // thare the code is twice as expensive.
600 return LT.first * 2;
601 }
602
Benjamin Kramer1625bfc2014-05-06 18:36:23 +0000603 // If we can't lower fmuladd into an FMA estimate the cost as a floating
604 // point mul followed by an add.
605 if (IID == Intrinsic::fmuladd)
606 return TopTTI->getArithmeticInstrCost(BinaryOperator::FMul, RetTy) +
607 TopTTI->getArithmeticInstrCost(BinaryOperator::FAdd, RetTy);
608
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000609 // Else, assume that we need to scalarize this intrinsic. For math builtins
610 // this will emit a costly libcall, adding call overhead and spills. Make it
611 // very expensive.
612 if (RetTy->isVectorTy()) {
613 unsigned Num = RetTy->getVectorNumElements();
614 unsigned Cost = TopTTI->getIntrinsicInstrCost(IID, RetTy->getScalarType(),
615 Tys);
616 return 10 * Cost * Num;
617 }
618
619 // This is going to be turned into a library call, make it expensive.
620 return 10;
Chandler Carruth664e3542013-01-07 01:37:14 +0000621}
622
623unsigned BasicTTI::getNumberOfParts(Type *Tp) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000624 std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Tp);
Chandler Carruth664e3542013-01-07 01:37:14 +0000625 return LT.first;
626}
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000627
Arnold Schwaighofer9da9a432013-07-12 19:16:02 +0000628unsigned BasicTTI::getAddressComputationCost(Type *Ty, bool IsComplex) const {
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000629 return 0;
630}
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000631
632unsigned BasicTTI::getReductionCost(unsigned Opcode, Type *Ty,
633 bool IsPairwise) const {
634 assert(Ty->isVectorTy() && "Expect a vector type");
635 unsigned NumVecElts = Ty->getVectorNumElements();
636 unsigned NumReduxLevels = Log2_32(NumVecElts);
637 unsigned ArithCost = NumReduxLevels *
638 TopTTI->getArithmeticInstrCost(Opcode, Ty);
639 // Assume the pairwise shuffles add a cost.
640 unsigned ShuffleCost =
641 NumReduxLevels * (IsPairwise + 1) *
642 TopTTI->getShuffleCost(SK_ExtractSubvector, Ty, NumVecElts / 2, Ty);
643 return ShuffleCost + ArithCost + getScalarizationOverhead(Ty, false, true);
644}