blob: 43a6e0f26dcbf85feeb27fceda4b0d0d36ba4204 [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;
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 Carruth664e3542013-01-07 01:37:14 +0000125
126 /// @}
127};
128
129}
130
131INITIALIZE_AG_PASS(BasicTTI, TargetTransformInfo, "basictti",
132 "Target independent code generator's TTI", true, true, false)
133char BasicTTI::ID = 0;
134
135ImmutablePass *
Bill Wendlingafc10362013-06-19 20:51:24 +0000136llvm::createBasicTargetTransformInfoPass(const TargetMachine *TM) {
137 return new BasicTTI(TM);
Chandler Carruth664e3542013-01-07 01:37:14 +0000138}
139
Tom Stellard8b1e0212013-07-27 00:01:07 +0000140bool BasicTTI::hasBranchDivergence() const { return false; }
Chandler Carruth664e3542013-01-07 01:37:14 +0000141
142bool BasicTTI::isLegalAddImmediate(int64_t imm) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000143 return getTLI()->isLegalAddImmediate(imm);
Chandler Carruth664e3542013-01-07 01:37:14 +0000144}
145
146bool BasicTTI::isLegalICmpImmediate(int64_t imm) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000147 return getTLI()->isLegalICmpImmediate(imm);
Chandler Carruth664e3542013-01-07 01:37:14 +0000148}
149
150bool BasicTTI::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
151 int64_t BaseOffset, bool HasBaseReg,
152 int64_t Scale) const {
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000153 TargetLoweringBase::AddrMode AM;
Chandler Carruth664e3542013-01-07 01:37:14 +0000154 AM.BaseGV = BaseGV;
155 AM.BaseOffs = BaseOffset;
156 AM.HasBaseReg = HasBaseReg;
157 AM.Scale = Scale;
Bill Wendlingafc10362013-06-19 20:51:24 +0000158 return getTLI()->isLegalAddressingMode(AM, Ty);
Chandler Carruth664e3542013-01-07 01:37:14 +0000159}
160
Quentin Colombetbf490d42013-05-31 21:29:03 +0000161int 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 Wendlingafc10362013-06-19 20:51:24 +0000169 return getTLI()->getScalingFactorCost(AM, Ty);
Quentin Colombetbf490d42013-05-31 21:29:03 +0000170}
171
Chandler Carruth664e3542013-01-07 01:37:14 +0000172bool BasicTTI::isTruncateFree(Type *Ty1, Type *Ty2) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000173 return getTLI()->isTruncateFree(Ty1, Ty2);
Chandler Carruth664e3542013-01-07 01:37:14 +0000174}
175
176bool BasicTTI::isTypeLegal(Type *Ty) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000177 EVT T = getTLI()->getValueType(Ty);
178 return getTLI()->isTypeLegal(T);
Chandler Carruth664e3542013-01-07 01:37:14 +0000179}
180
181unsigned BasicTTI::getJumpBufAlignment() const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000182 return getTLI()->getJumpBufAlignment();
Chandler Carruth664e3542013-01-07 01:37:14 +0000183}
184
185unsigned BasicTTI::getJumpBufSize() const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000186 return getTLI()->getJumpBufSize();
Chandler Carruth664e3542013-01-07 01:37:14 +0000187}
188
189bool BasicTTI::shouldBuildLookupTables() const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000190 const TargetLoweringBase *TLI = getTLI();
Rafael Espindolaf8b27c42014-08-07 14:21:18 +0000191 return TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
192 TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other);
Chandler Carruth664e3542013-01-07 01:37:14 +0000193}
194
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000195bool BasicTTI::haveFastSqrt(Type *Ty) const {
196 const TargetLoweringBase *TLI = getTLI();
197 EVT VT = TLI->getValueType(Ty);
198 return TLI->isTypeLegal(VT) && TLI->isOperationLegalOrCustom(ISD::FSQRT, VT);
199}
200
Hal Finkel6532c202014-05-08 09:14:44 +0000201void BasicTTI::getUnrollingPreferences(Loop *L,
202 UnrollingPreferences &UP) const {
203 // This unrolling functionality is target independent, but to provide some
Hal Finkele8172d82014-05-08 13:42:57 +0000204 // motivation for its intended use, for x86:
Hal Finkel6532c202014-05-08 09:14:44 +0000205
206 // According to the Intel 64 and IA-32 Architectures Optimization Reference
207 // Manual, Intel Core models and later have a loop stream detector
208 // (and associated uop queue) that can benefit from partial unrolling.
209 // The relevant requirements are:
210 // - The loop must have no more than 4 (8 for Nehalem and later) branches
211 // taken, and none of them may be calls.
212 // - The loop can have no more than 18 (28 for Nehalem and later) uops.
213
214 // According to the Software Optimization Guide for AMD Family 15h Processors,
215 // models 30h-4fh (Steamroller and later) have a loop predictor and loop
216 // buffer which can benefit from partial unrolling.
217 // The relevant requirements are:
218 // - The loop must have fewer than 16 branches
219 // - The loop must have less than 40 uops in all executed loop branches
220
221 // The number of taken branches in a loop is hard to estimate here, and
222 // benchmarking has revealed that it is better not to be conservative when
223 // estimating the branch count. As a result, we'll ignore the branch limits
224 // until someone finds a case where it matters in practice.
225
226 unsigned MaxOps;
227 const TargetSubtargetInfo *ST = &TM->getSubtarget<TargetSubtargetInfo>();
228 if (PartialUnrollingThreshold.getNumOccurrences() > 0)
229 MaxOps = PartialUnrollingThreshold;
230 else if (ST->getSchedModel()->LoopMicroOpBufferSize > 0)
231 MaxOps = ST->getSchedModel()->LoopMicroOpBufferSize;
232 else
233 return;
234
235 // Scan the loop: don't unroll loops with calls.
236 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
237 I != E; ++I) {
238 BasicBlock *BB = *I;
239
240 for (BasicBlock::iterator J = BB->begin(), JE = BB->end(); J != JE; ++J)
241 if (isa<CallInst>(J) || isa<InvokeInst>(J)) {
242 ImmutableCallSite CS(J);
243 if (const Function *F = CS.getCalledFunction()) {
244 if (!TopTTI->isLoweredToCall(F))
245 continue;
246 }
247
248 return;
249 }
250 }
251
252 // Enable runtime and partial unrolling up to the specified size.
253 UP.Partial = UP.Runtime = true;
254 UP.PartialThreshold = UP.PartialOptSizeThreshold = MaxOps;
255}
Hal Finkel8f2e7002013-09-11 19:25:43 +0000256
Chandler Carruth664e3542013-01-07 01:37:14 +0000257//===----------------------------------------------------------------------===//
258//
259// Calls used by the vectorizers.
260//
261//===----------------------------------------------------------------------===//
262
263unsigned BasicTTI::getScalarizationOverhead(Type *Ty, bool Insert,
264 bool Extract) const {
265 assert (Ty->isVectorTy() && "Can only scalarize vectors");
266 unsigned Cost = 0;
267
268 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
269 if (Insert)
270 Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
271 if (Extract)
272 Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
273 }
274
275 return Cost;
276}
277
278unsigned BasicTTI::getNumberOfRegisters(bool Vector) const {
279 return 1;
280}
281
Nadav Rotemb1791a72013-01-09 22:29:00 +0000282unsigned BasicTTI::getRegisterBitWidth(bool Vector) const {
283 return 32;
284}
285
Nadav Rotemb696c362013-01-09 01:15:42 +0000286unsigned BasicTTI::getMaximumUnrollFactor() const {
287 return 1;
288}
289
Arnold Schwaighoferb9773872013-04-04 23:26:21 +0000290unsigned BasicTTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty,
291 OperandValueKind,
292 OperandValueKind) const {
Chandler Carruth664e3542013-01-07 01:37:14 +0000293 // Check if any of the operands are vector operands.
Bill Wendlingafc10362013-06-19 20:51:24 +0000294 const TargetLoweringBase *TLI = getTLI();
Chandler Carruth664e3542013-01-07 01:37:14 +0000295 int ISD = TLI->InstructionOpcodeToISD(Opcode);
296 assert(ISD && "Invalid opcode");
297
298 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
299
Nadav Rotem87a0af62013-04-12 21:15:03 +0000300 bool IsFloat = Ty->getScalarType()->isFloatingPointTy();
Nadav Rotem0db06902013-04-14 05:55:18 +0000301 // Assume that floating point arithmetic operations cost twice as much as
302 // integer operations.
Nadav Rotem87a0af62013-04-12 21:15:03 +0000303 unsigned OpCost = (IsFloat ? 2 : 1);
304
Chandler Carruth664e3542013-01-07 01:37:14 +0000305 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
306 // The operation is legal. Assume it costs 1.
Nadav Rotem0db06902013-04-14 05:55:18 +0000307 // If the type is split to multiple registers, assume that there is some
Chandler Carruth664e3542013-01-07 01:37:14 +0000308 // overhead to this.
309 // TODO: Once we have extract/insert subvector cost we need to use them.
310 if (LT.first > 1)
Nadav Rotem87a0af62013-04-12 21:15:03 +0000311 return LT.first * 2 * OpCost;
312 return LT.first * 1 * OpCost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000313 }
314
315 if (!TLI->isOperationExpand(ISD, LT.second)) {
316 // If the operation is custom lowered then assume
317 // thare the code is twice as expensive.
Nadav Rotem87a0af62013-04-12 21:15:03 +0000318 return LT.first * 2 * OpCost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000319 }
320
321 // Else, assume that we need to scalarize this op.
322 if (Ty->isVectorTy()) {
323 unsigned Num = Ty->getVectorNumElements();
324 unsigned Cost = TopTTI->getArithmeticInstrCost(Opcode, Ty->getScalarType());
325 // return the cost of multiple scalar invocation plus the cost of inserting
326 // and extracting the values.
327 return getScalarizationOverhead(Ty, true, true) + Num * Cost;
328 }
329
330 // We don't know anything about this scalar instruction.
Nadav Rotem87a0af62013-04-12 21:15:03 +0000331 return OpCost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000332}
333
Karthik Bhate03a25d2014-06-20 04:32:48 +0000334unsigned BasicTTI::getAltShuffleOverhead(Type *Ty) const {
335 assert(Ty->isVectorTy() && "Can only shuffle vectors");
336 unsigned Cost = 0;
337 // Shuffle cost is equal to the cost of extracting element from its argument
338 // plus the cost of inserting them onto the result vector.
339
340 // e.g. <4 x float> has a mask of <0,5,2,7> i.e we need to extract from index
341 // 0 of first vector, index 1 of second vector,index 2 of first vector and
342 // finally index 3 of second vector and insert them at index <0,1,2,3> of
343 // result vector.
344 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
345 Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
346 Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
347 }
348 return Cost;
349}
350
Chandler Carruth664e3542013-01-07 01:37:14 +0000351unsigned BasicTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
352 Type *SubTp) const {
Karthik Bhate03a25d2014-06-20 04:32:48 +0000353 if (Kind == SK_Alternate) {
354 return getAltShuffleOverhead(Tp);
355 }
Chandler Carruth664e3542013-01-07 01:37:14 +0000356 return 1;
357}
358
359unsigned BasicTTI::getCastInstrCost(unsigned Opcode, Type *Dst,
360 Type *Src) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000361 const TargetLoweringBase *TLI = getTLI();
Chandler Carruth664e3542013-01-07 01:37:14 +0000362 int ISD = TLI->InstructionOpcodeToISD(Opcode);
363 assert(ISD && "Invalid opcode");
364
365 std::pair<unsigned, MVT> SrcLT = TLI->getTypeLegalizationCost(Src);
366 std::pair<unsigned, MVT> DstLT = TLI->getTypeLegalizationCost(Dst);
367
Nadav Roteme55aa3c2013-01-11 19:54:13 +0000368 // Check for NOOP conversions.
369 if (SrcLT.first == DstLT.first &&
370 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
371
372 // Bitcast between types that are legalized to the same type are free.
373 if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc)
374 return 0;
375 }
376
377 if (Opcode == Instruction::Trunc &&
378 TLI->isTruncateFree(SrcLT.second, DstLT.second))
379 return 0;
380
381 if (Opcode == Instruction::ZExt &&
382 TLI->isZExtFree(SrcLT.second, DstLT.second))
383 return 0;
384
385 // If the cast is marked as legal (or promote) then assume low cost.
Hal Finkel55312de2014-04-02 23:18:54 +0000386 if (SrcLT.first == DstLT.first &&
387 TLI->isOperationLegalOrPromote(ISD, DstLT.second))
Nadav Roteme55aa3c2013-01-11 19:54:13 +0000388 return 1;
389
Chandler Carruth664e3542013-01-07 01:37:14 +0000390 // Handle scalar conversions.
391 if (!Src->isVectorTy() && !Dst->isVectorTy()) {
392
393 // Scalar bitcasts are usually free.
394 if (Opcode == Instruction::BitCast)
395 return 0;
396
Chandler Carruth664e3542013-01-07 01:37:14 +0000397 // Just check the op cost. If the operation is legal then assume it costs 1.
398 if (!TLI->isOperationExpand(ISD, DstLT.second))
399 return 1;
400
401 // Assume that illegal scalar instruction are expensive.
402 return 4;
403 }
404
405 // Check vector-to-vector casts.
406 if (Dst->isVectorTy() && Src->isVectorTy()) {
407
408 // If the cast is between same-sized registers, then the check is simple.
409 if (SrcLT.first == DstLT.first &&
410 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
411
Chandler Carruth664e3542013-01-07 01:37:14 +0000412 // Assume that Zext is done using AND.
413 if (Opcode == Instruction::ZExt)
414 return 1;
415
416 // Assume that sext is done using SHL and SRA.
417 if (Opcode == Instruction::SExt)
418 return 2;
419
420 // Just check the op cost. If the operation is legal then assume it costs
421 // 1 and multiply by the type-legalization overhead.
422 if (!TLI->isOperationExpand(ISD, DstLT.second))
423 return SrcLT.first * 1;
424 }
425
426 // If we are converting vectors and the operation is illegal, or
427 // if the vectors are legalized to different types, estimate the
428 // scalarization costs.
429 unsigned Num = Dst->getVectorNumElements();
430 unsigned Cost = TopTTI->getCastInstrCost(Opcode, Dst->getScalarType(),
431 Src->getScalarType());
432
433 // Return the cost of multiple scalar invocation plus the cost of
434 // inserting and extracting the values.
435 return getScalarizationOverhead(Dst, true, true) + Num * Cost;
436 }
437
438 // We already handled vector-to-vector and scalar-to-scalar conversions. This
439 // is where we handle bitcast between vectors and scalars. We need to assume
440 // that the conversion is scalarized in one way or another.
441 if (Opcode == Instruction::BitCast)
442 // Illegal bitcasts are done by storing and loading from a stack slot.
443 return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) +
444 (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0);
445
446 llvm_unreachable("Unhandled cast");
447 }
448
449unsigned BasicTTI::getCFInstrCost(unsigned Opcode) const {
450 // Branches are assumed to be predicted.
451 return 0;
452}
453
454unsigned BasicTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
455 Type *CondTy) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000456 const TargetLoweringBase *TLI = getTLI();
Chandler Carruth664e3542013-01-07 01:37:14 +0000457 int ISD = TLI->InstructionOpcodeToISD(Opcode);
458 assert(ISD && "Invalid opcode");
459
460 // Selects on vectors are actually vector selects.
461 if (ISD == ISD::SELECT) {
462 assert(CondTy && "CondTy must exist");
463 if (CondTy->isVectorTy())
464 ISD = ISD::VSELECT;
465 }
466
467 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
468
469 if (!TLI->isOperationExpand(ISD, LT.second)) {
470 // The operation is legal. Assume it costs 1. Multiply
471 // by the type-legalization overhead.
472 return LT.first * 1;
473 }
474
475 // Otherwise, assume that the cast is scalarized.
476 if (ValTy->isVectorTy()) {
477 unsigned Num = ValTy->getVectorNumElements();
478 if (CondTy)
479 CondTy = CondTy->getScalarType();
480 unsigned Cost = TopTTI->getCmpSelInstrCost(Opcode, ValTy->getScalarType(),
481 CondTy);
482
483 // Return the cost of multiple scalar invocation plus the cost of inserting
484 // and extracting the values.
485 return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
486 }
487
488 // Unknown scalar opcode.
489 return 1;
490}
491
492unsigned BasicTTI::getVectorInstrCost(unsigned Opcode, Type *Val,
493 unsigned Index) const {
Raul E. Silverace376c02014-03-10 22:59:13 +0000494 std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Val->getScalarType());
495
496 return LT.first;
Chandler Carruth664e3542013-01-07 01:37:14 +0000497}
498
499unsigned BasicTTI::getMemoryOpCost(unsigned Opcode, Type *Src,
500 unsigned Alignment,
501 unsigned AddressSpace) const {
502 assert(!Src->isVoidTy() && "Invalid type");
Bill Wendlingafc10362013-06-19 20:51:24 +0000503 std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Src);
Chandler Carruth664e3542013-01-07 01:37:14 +0000504
Hal Finkel6fd19ab2014-04-03 00:53:59 +0000505 // Assuming that all loads of legal types cost 1.
506 unsigned Cost = LT.first;
507
508 if (Src->isVectorTy() &&
509 Src->getPrimitiveSizeInBits() < LT.second.getSizeInBits()) {
510 // This is a vector load that legalizes to a larger type than the vector
511 // itself. Unless the corresponding extending load or truncating store is
512 // legal, then this will scalarize.
Hal Finkel56bf2972014-04-14 05:59:09 +0000513 TargetLowering::LegalizeAction LA = TargetLowering::Expand;
514 EVT MemVT = getTLI()->getValueType(Src, true);
515 if (MemVT.isSimple() && MemVT != MVT::Other) {
516 if (Opcode == Instruction::Store)
517 LA = getTLI()->getTruncStoreAction(LT.second, MemVT.getSimpleVT());
518 else
519 LA = getTLI()->getLoadExtAction(ISD::EXTLOAD, MemVT.getSimpleVT());
520 }
Hal Finkel6fd19ab2014-04-03 00:53:59 +0000521
522 if (LA != TargetLowering::Legal && LA != TargetLowering::Custom) {
523 // This is a vector load/store for some illegal type that is scalarized.
524 // We must account for the cost of building or decomposing the vector.
525 Cost += getScalarizationOverhead(Src, Opcode != Instruction::Store,
526 Opcode == Instruction::Store);
527 }
528 }
529
530 return Cost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000531}
532
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000533unsigned BasicTTI::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy,
Chandler Carruth664e3542013-01-07 01:37:14 +0000534 ArrayRef<Type *> Tys) const {
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000535 unsigned ISD = 0;
536 switch (IID) {
537 default: {
538 // Assume that we need to scalarize this intrinsic.
539 unsigned ScalarizationCost = 0;
540 unsigned ScalarCalls = 1;
541 if (RetTy->isVectorTy()) {
542 ScalarizationCost = getScalarizationOverhead(RetTy, true, false);
Chandler Carruth664e3542013-01-07 01:37:14 +0000543 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
544 }
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000545 for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) {
546 if (Tys[i]->isVectorTy()) {
547 ScalarizationCost += getScalarizationOverhead(Tys[i], false, true);
548 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
549 }
550 }
551
552 return ScalarCalls + ScalarizationCost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000553 }
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000554 // Look for intrinsics that can be lowered directly or turned into a scalar
555 // intrinsic call.
556 case Intrinsic::sqrt: ISD = ISD::FSQRT; break;
557 case Intrinsic::sin: ISD = ISD::FSIN; break;
558 case Intrinsic::cos: ISD = ISD::FCOS; break;
559 case Intrinsic::exp: ISD = ISD::FEXP; break;
560 case Intrinsic::exp2: ISD = ISD::FEXP2; break;
561 case Intrinsic::log: ISD = ISD::FLOG; break;
562 case Intrinsic::log10: ISD = ISD::FLOG10; break;
563 case Intrinsic::log2: ISD = ISD::FLOG2; break;
564 case Intrinsic::fabs: ISD = ISD::FABS; break;
Hal Finkel0c5c01aa2013-08-19 23:35:46 +0000565 case Intrinsic::copysign: ISD = ISD::FCOPYSIGN; break;
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000566 case Intrinsic::floor: ISD = ISD::FFLOOR; break;
567 case Intrinsic::ceil: ISD = ISD::FCEIL; break;
568 case Intrinsic::trunc: ISD = ISD::FTRUNC; break;
Hal Finkelec474f22013-07-08 03:24:07 +0000569 case Intrinsic::nearbyint:
570 ISD = ISD::FNEARBYINT; break;
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000571 case Intrinsic::rint: ISD = ISD::FRINT; break;
Hal Finkel171817e2013-08-07 22:49:12 +0000572 case Intrinsic::round: ISD = ISD::FROUND; break;
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000573 case Intrinsic::pow: ISD = ISD::FPOW; break;
574 case Intrinsic::fma: ISD = ISD::FMA; break;
Benjamin Kramer1625bfc2014-05-06 18:36:23 +0000575 case Intrinsic::fmuladd: ISD = ISD::FMA; break;
Hal Finkel93046912014-07-25 21:13:35 +0000576 // FIXME: We should return 0 whenever getIntrinsicCost == TCC_Free.
Arnold Schwaighofera7cd6bf2013-08-06 22:37:52 +0000577 case Intrinsic::lifetime_start:
578 case Intrinsic::lifetime_end:
579 return 0;
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000580 }
581
Bill Wendlingafc10362013-06-19 20:51:24 +0000582 const TargetLoweringBase *TLI = getTLI();
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000583 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(RetTy);
584
585 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
586 // The operation is legal. Assume it costs 1.
587 // If the type is split to multiple registers, assume that thre is some
588 // overhead to this.
589 // TODO: Once we have extract/insert subvector cost we need to use them.
590 if (LT.first > 1)
591 return LT.first * 2;
592 return LT.first * 1;
593 }
594
595 if (!TLI->isOperationExpand(ISD, LT.second)) {
596 // If the operation is custom lowered then assume
597 // thare the code is twice as expensive.
598 return LT.first * 2;
599 }
600
Benjamin Kramer1625bfc2014-05-06 18:36:23 +0000601 // If we can't lower fmuladd into an FMA estimate the cost as a floating
602 // point mul followed by an add.
603 if (IID == Intrinsic::fmuladd)
604 return TopTTI->getArithmeticInstrCost(BinaryOperator::FMul, RetTy) +
605 TopTTI->getArithmeticInstrCost(BinaryOperator::FAdd, RetTy);
606
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000607 // Else, assume that we need to scalarize this intrinsic. For math builtins
608 // this will emit a costly libcall, adding call overhead and spills. Make it
609 // very expensive.
610 if (RetTy->isVectorTy()) {
611 unsigned Num = RetTy->getVectorNumElements();
612 unsigned Cost = TopTTI->getIntrinsicInstrCost(IID, RetTy->getScalarType(),
613 Tys);
614 return 10 * Cost * Num;
615 }
616
617 // This is going to be turned into a library call, make it expensive.
618 return 10;
Chandler Carruth664e3542013-01-07 01:37:14 +0000619}
620
621unsigned BasicTTI::getNumberOfParts(Type *Tp) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000622 std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Tp);
Chandler Carruth664e3542013-01-07 01:37:14 +0000623 return LT.first;
624}
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000625
Arnold Schwaighofer9da9a432013-07-12 19:16:02 +0000626unsigned BasicTTI::getAddressComputationCost(Type *Ty, bool IsComplex) const {
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000627 return 0;
628}
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000629
630unsigned BasicTTI::getReductionCost(unsigned Opcode, Type *Ty,
631 bool IsPairwise) const {
632 assert(Ty->isVectorTy() && "Expect a vector type");
633 unsigned NumVecElts = Ty->getVectorNumElements();
634 unsigned NumReduxLevels = Log2_32(NumVecElts);
635 unsigned ArithCost = NumReduxLevels *
636 TopTTI->getArithmeticInstrCost(Opcode, Ty);
637 // Assume the pairwise shuffles add a cost.
638 unsigned ShuffleCost =
639 NumReduxLevels * (IsPairwise + 1) *
640 TopTTI->getShuffleCost(SK_ExtractSubvector, Ty, NumVecElts / 2, Ty);
641 return ShuffleCost + ArithCost + getScalarizationOverhead(Ty, false, true);
642}