blob: b2737bf754f9e39761683e73239016a209f5524f [file] [log] [blame]
Chandler Carruthaeef83c2013-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 Carruthaeef83c2013-01-07 01:37:14 +000018#include "llvm/CodeGen/Passes.h"
Stephen Hinesdce4a402014-05-29 02:49:00 -070019#include "llvm/Analysis/LoopInfo.h"
Chandler Carruthbe049292013-01-07 03:08:10 +000020#include "llvm/Analysis/TargetTransformInfo.h"
Stephen Hinesdce4a402014-05-29 02:49:00 -070021#include "llvm/Support/CommandLine.h"
Chandler Carruthaeef83c2013-01-07 01:37:14 +000022#include "llvm/Target/TargetLowering.h"
Stephen Hinesdce4a402014-05-29 02:49:00 -070023#include "llvm/Target/TargetSubtargetInfo.h"
Chandler Carruthaeef83c2013-01-07 01:37:14 +000024#include <utility>
Chandler Carruthaeef83c2013-01-07 01:37:14 +000025using namespace llvm;
26
Stephen Hinesdce4a402014-05-29 02:49:00 -070027static cl::opt<unsigned>
28PartialUnrollingThreshold("partial-unrolling-threshold", cl::init(0),
29 cl::desc("Threshold for partial unrolling"), cl::Hidden);
30
31#define DEBUG_TYPE "basictti"
32
Chandler Carruthaeef83c2013-01-07 01:37:14 +000033namespace {
34
Stephen Hines36b56882014-04-23 16:57:46 -070035class BasicTTI final : public ImmutablePass, public TargetTransformInfo {
Bill Wendlingea442812013-06-19 20:51:24 +000036 const TargetMachine *TM;
Chandler Carruthaeef83c2013-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
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070042 /// Estimate the cost overhead of SK_Alternate shuffle.
43 unsigned getAltShuffleOverhead(Type *Ty) const;
44
Bill Wendlingea442812013-06-19 20:51:24 +000045 const TargetLoweringBase *getTLI() const { return TM->getTargetLowering(); }
46
Chandler Carruthaeef83c2013-01-07 01:37:14 +000047public:
Stephen Hinesdce4a402014-05-29 02:49:00 -070048 BasicTTI() : ImmutablePass(ID), TM(nullptr) {
Chandler Carruthaeef83c2013-01-07 01:37:14 +000049 llvm_unreachable("This pass cannot be directly constructed");
50 }
51
Bill Wendlingea442812013-06-19 20:51:24 +000052 BasicTTI(const TargetMachine *TM) : ImmutablePass(ID), TM(TM) {
Chandler Carruthaeef83c2013-01-07 01:37:14 +000053 initializeBasicTTIPass(*PassRegistry::getPassRegistry());
54 }
55
Stephen Hines36b56882014-04-23 16:57:46 -070056 void initializePass() override {
Chandler Carruthaeef83c2013-01-07 01:37:14 +000057 pushTTIStack(this);
58 }
59
Stephen Hines36b56882014-04-23 16:57:46 -070060 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruthaeef83c2013-01-07 01:37:14 +000061 TargetTransformInfo::getAnalysisUsage(AU);
62 }
63
64 /// Pass identification.
65 static char ID;
66
67 /// Provide necessary pointer adjustments for the two base classes.
Stephen Hines36b56882014-04-23 16:57:46 -070068 void *getAdjustedAnalysisPointer(const void *ID) override {
Chandler Carruthaeef83c2013-01-07 01:37:14 +000069 if (ID == &TargetTransformInfo::ID)
70 return (TargetTransformInfo*)this;
71 return this;
72 }
73
Stephen Hines36b56882014-04-23 16:57:46 -070074 bool hasBranchDivergence() const override;
Tom Stellard57e6b2d2013-07-27 00:01:07 +000075
Chandler Carruthaeef83c2013-01-07 01:37:14 +000076 /// \name Scalar TTI Implementations
77 /// @{
78
Stephen Hines36b56882014-04-23 16:57:46 -070079 bool isLegalAddImmediate(int64_t imm) const override;
80 bool isLegalICmpImmediate(int64_t imm) const override;
81 bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
82 int64_t BaseOffset, bool HasBaseReg,
83 int64_t Scale) const override;
84 int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
85 int64_t BaseOffset, bool HasBaseReg,
86 int64_t Scale) const override;
87 bool isTruncateFree(Type *Ty1, Type *Ty2) const override;
88 bool isTypeLegal(Type *Ty) const override;
89 unsigned getJumpBufAlignment() const override;
90 unsigned getJumpBufSize() const override;
91 bool shouldBuildLookupTables() const override;
92 bool haveFastSqrt(Type *Ty) const override;
93 void getUnrollingPreferences(Loop *L,
94 UnrollingPreferences &UP) const override;
Chandler Carruthaeef83c2013-01-07 01:37:14 +000095
96 /// @}
97
98 /// \name Vector TTI Implementations
99 /// @{
100
Stephen Hines36b56882014-04-23 16:57:46 -0700101 unsigned getNumberOfRegisters(bool Vector) const override;
102 unsigned getMaximumUnrollFactor() const override;
103 unsigned getRegisterBitWidth(bool Vector) const override;
104 unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty, OperandValueKind,
105 OperandValueKind) const override;
106 unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
107 int Index, Type *SubTp) const override;
108 unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
109 Type *Src) const override;
110 unsigned getCFInstrCost(unsigned Opcode) const override;
111 unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
112 Type *CondTy) const override;
113 unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
114 unsigned Index) const override;
115 unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
116 unsigned AddressSpace) const override;
117 unsigned getIntrinsicInstrCost(Intrinsic::ID, Type *RetTy,
118 ArrayRef<Type*> Tys) const override;
119 unsigned getNumberOfParts(Type *Tp) const override;
120 unsigned getAddressComputationCost( Type *Ty, bool IsComplex) const override;
121 unsigned getReductionCost(unsigned Opcode, Type *Ty,
122 bool IsPairwise) const override;
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000123
124 /// @}
125};
126
127}
128
129INITIALIZE_AG_PASS(BasicTTI, TargetTransformInfo, "basictti",
130 "Target independent code generator's TTI", true, true, false)
131char BasicTTI::ID = 0;
132
133ImmutablePass *
Bill Wendlingea442812013-06-19 20:51:24 +0000134llvm::createBasicTargetTransformInfoPass(const TargetMachine *TM) {
135 return new BasicTTI(TM);
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000136}
137
Tom Stellard57e6b2d2013-07-27 00:01:07 +0000138bool BasicTTI::hasBranchDivergence() const { return false; }
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000139
140bool BasicTTI::isLegalAddImmediate(int64_t imm) const {
Bill Wendlingea442812013-06-19 20:51:24 +0000141 return getTLI()->isLegalAddImmediate(imm);
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000142}
143
144bool BasicTTI::isLegalICmpImmediate(int64_t imm) const {
Bill Wendlingea442812013-06-19 20:51:24 +0000145 return getTLI()->isLegalICmpImmediate(imm);
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000146}
147
148bool BasicTTI::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
149 int64_t BaseOffset, bool HasBaseReg,
150 int64_t Scale) const {
Benjamin Kramer69e42db2013-01-11 20:05:37 +0000151 TargetLoweringBase::AddrMode AM;
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000152 AM.BaseGV = BaseGV;
153 AM.BaseOffs = BaseOffset;
154 AM.HasBaseReg = HasBaseReg;
155 AM.Scale = Scale;
Bill Wendlingea442812013-06-19 20:51:24 +0000156 return getTLI()->isLegalAddressingMode(AM, Ty);
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000157}
158
Quentin Colombet06f5ebc2013-05-31 21:29:03 +0000159int BasicTTI::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
160 int64_t BaseOffset, bool HasBaseReg,
161 int64_t Scale) const {
162 TargetLoweringBase::AddrMode AM;
163 AM.BaseGV = BaseGV;
164 AM.BaseOffs = BaseOffset;
165 AM.HasBaseReg = HasBaseReg;
166 AM.Scale = Scale;
Bill Wendlingea442812013-06-19 20:51:24 +0000167 return getTLI()->getScalingFactorCost(AM, Ty);
Quentin Colombet06f5ebc2013-05-31 21:29:03 +0000168}
169
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000170bool BasicTTI::isTruncateFree(Type *Ty1, Type *Ty2) const {
Bill Wendlingea442812013-06-19 20:51:24 +0000171 return getTLI()->isTruncateFree(Ty1, Ty2);
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000172}
173
174bool BasicTTI::isTypeLegal(Type *Ty) const {
Bill Wendlingea442812013-06-19 20:51:24 +0000175 EVT T = getTLI()->getValueType(Ty);
176 return getTLI()->isTypeLegal(T);
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000177}
178
179unsigned BasicTTI::getJumpBufAlignment() const {
Bill Wendlingea442812013-06-19 20:51:24 +0000180 return getTLI()->getJumpBufAlignment();
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000181}
182
183unsigned BasicTTI::getJumpBufSize() const {
Bill Wendlingea442812013-06-19 20:51:24 +0000184 return getTLI()->getJumpBufSize();
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000185}
186
187bool BasicTTI::shouldBuildLookupTables() const {
Bill Wendlingea442812013-06-19 20:51:24 +0000188 const TargetLoweringBase *TLI = getTLI();
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000189 return TLI->supportJumpTables() &&
190 (TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
191 TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
192}
193
Richard Sandiforda8a70992013-08-23 10:27:02 +0000194bool BasicTTI::haveFastSqrt(Type *Ty) const {
195 const TargetLoweringBase *TLI = getTLI();
196 EVT VT = TLI->getValueType(Ty);
197 return TLI->isTypeLegal(VT) && TLI->isOperationLegalOrCustom(ISD::FSQRT, VT);
198}
199
Stephen Hinesdce4a402014-05-29 02:49:00 -0700200void BasicTTI::getUnrollingPreferences(Loop *L,
201 UnrollingPreferences &UP) const {
202 // This unrolling functionality is target independent, but to provide some
203 // motivation for its intended use, for x86:
204
205 // According to the Intel 64 and IA-32 Architectures Optimization Reference
206 // Manual, Intel Core models and later have a loop stream detector
207 // (and associated uop queue) that can benefit from partial unrolling.
208 // The relevant requirements are:
209 // - The loop must have no more than 4 (8 for Nehalem and later) branches
210 // taken, and none of them may be calls.
211 // - The loop can have no more than 18 (28 for Nehalem and later) uops.
212
213 // According to the Software Optimization Guide for AMD Family 15h Processors,
214 // models 30h-4fh (Steamroller and later) have a loop predictor and loop
215 // buffer which can benefit from partial unrolling.
216 // The relevant requirements are:
217 // - The loop must have fewer than 16 branches
218 // - The loop must have less than 40 uops in all executed loop branches
219
220 // The number of taken branches in a loop is hard to estimate here, and
221 // benchmarking has revealed that it is better not to be conservative when
222 // estimating the branch count. As a result, we'll ignore the branch limits
223 // until someone finds a case where it matters in practice.
224
225 unsigned MaxOps;
226 const TargetSubtargetInfo *ST = &TM->getSubtarget<TargetSubtargetInfo>();
227 if (PartialUnrollingThreshold.getNumOccurrences() > 0)
228 MaxOps = PartialUnrollingThreshold;
229 else if (ST->getSchedModel()->LoopMicroOpBufferSize > 0)
230 MaxOps = ST->getSchedModel()->LoopMicroOpBufferSize;
231 else
232 return;
233
234 // Scan the loop: don't unroll loops with calls.
235 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
236 I != E; ++I) {
237 BasicBlock *BB = *I;
238
239 for (BasicBlock::iterator J = BB->begin(), JE = BB->end(); J != JE; ++J)
240 if (isa<CallInst>(J) || isa<InvokeInst>(J)) {
241 ImmutableCallSite CS(J);
242 if (const Function *F = CS.getCalledFunction()) {
243 if (!TopTTI->isLoweredToCall(F))
244 continue;
245 }
246
247 return;
248 }
249 }
250
251 // Enable runtime and partial unrolling up to the specified size.
252 UP.Partial = UP.Runtime = true;
253 UP.PartialThreshold = UP.PartialOptSizeThreshold = MaxOps;
254}
Hal Finkel4f7e2c32013-09-11 19:25:43 +0000255
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000256//===----------------------------------------------------------------------===//
257//
258// Calls used by the vectorizers.
259//
260//===----------------------------------------------------------------------===//
261
262unsigned BasicTTI::getScalarizationOverhead(Type *Ty, bool Insert,
263 bool Extract) const {
264 assert (Ty->isVectorTy() && "Can only scalarize vectors");
265 unsigned Cost = 0;
266
267 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
268 if (Insert)
269 Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
270 if (Extract)
271 Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
272 }
273
274 return Cost;
275}
276
277unsigned BasicTTI::getNumberOfRegisters(bool Vector) const {
278 return 1;
279}
280
Nadav Rotem14925e62013-01-09 22:29:00 +0000281unsigned BasicTTI::getRegisterBitWidth(bool Vector) const {
282 return 32;
283}
284
Nadav Rotem83be7b02013-01-09 01:15:42 +0000285unsigned BasicTTI::getMaximumUnrollFactor() const {
286 return 1;
287}
288
Arnold Schwaighofer6bf4f672013-04-04 23:26:21 +0000289unsigned BasicTTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty,
290 OperandValueKind,
291 OperandValueKind) const {
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000292 // Check if any of the operands are vector operands.
Bill Wendlingea442812013-06-19 20:51:24 +0000293 const TargetLoweringBase *TLI = getTLI();
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000294 int ISD = TLI->InstructionOpcodeToISD(Opcode);
295 assert(ISD && "Invalid opcode");
296
297 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
298
Nadav Rotem9eb366a2013-04-12 21:15:03 +0000299 bool IsFloat = Ty->getScalarType()->isFloatingPointTy();
Nadav Rotem42082002013-04-14 05:55:18 +0000300 // Assume that floating point arithmetic operations cost twice as much as
301 // integer operations.
Nadav Rotem9eb366a2013-04-12 21:15:03 +0000302 unsigned OpCost = (IsFloat ? 2 : 1);
303
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000304 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
305 // The operation is legal. Assume it costs 1.
Nadav Rotem42082002013-04-14 05:55:18 +0000306 // If the type is split to multiple registers, assume that there is some
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000307 // overhead to this.
308 // TODO: Once we have extract/insert subvector cost we need to use them.
309 if (LT.first > 1)
Nadav Rotem9eb366a2013-04-12 21:15:03 +0000310 return LT.first * 2 * OpCost;
311 return LT.first * 1 * OpCost;
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000312 }
313
314 if (!TLI->isOperationExpand(ISD, LT.second)) {
315 // If the operation is custom lowered then assume
316 // thare the code is twice as expensive.
Nadav Rotem9eb366a2013-04-12 21:15:03 +0000317 return LT.first * 2 * OpCost;
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000318 }
319
320 // Else, assume that we need to scalarize this op.
321 if (Ty->isVectorTy()) {
322 unsigned Num = Ty->getVectorNumElements();
323 unsigned Cost = TopTTI->getArithmeticInstrCost(Opcode, Ty->getScalarType());
324 // return the cost of multiple scalar invocation plus the cost of inserting
325 // and extracting the values.
326 return getScalarizationOverhead(Ty, true, true) + Num * Cost;
327 }
328
329 // We don't know anything about this scalar instruction.
Nadav Rotem9eb366a2013-04-12 21:15:03 +0000330 return OpCost;
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000331}
332
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700333unsigned BasicTTI::getAltShuffleOverhead(Type *Ty) const {
334 assert(Ty->isVectorTy() && "Can only shuffle vectors");
335 unsigned Cost = 0;
336 // Shuffle cost is equal to the cost of extracting element from its argument
337 // plus the cost of inserting them onto the result vector.
338
339 // e.g. <4 x float> has a mask of <0,5,2,7> i.e we need to extract from index
340 // 0 of first vector, index 1 of second vector,index 2 of first vector and
341 // finally index 3 of second vector and insert them at index <0,1,2,3> of
342 // result vector.
343 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
344 Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
345 Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
346 }
347 return Cost;
348}
349
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000350unsigned BasicTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
351 Type *SubTp) const {
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700352 if (Kind == SK_Alternate) {
353 return getAltShuffleOverhead(Tp);
354 }
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000355 return 1;
356}
357
358unsigned BasicTTI::getCastInstrCost(unsigned Opcode, Type *Dst,
359 Type *Src) const {
Bill Wendlingea442812013-06-19 20:51:24 +0000360 const TargetLoweringBase *TLI = getTLI();
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000361 int ISD = TLI->InstructionOpcodeToISD(Opcode);
362 assert(ISD && "Invalid opcode");
363
364 std::pair<unsigned, MVT> SrcLT = TLI->getTypeLegalizationCost(Src);
365 std::pair<unsigned, MVT> DstLT = TLI->getTypeLegalizationCost(Dst);
366
Nadav Rotem3e40d922013-01-11 19:54:13 +0000367 // Check for NOOP conversions.
368 if (SrcLT.first == DstLT.first &&
369 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
370
371 // Bitcast between types that are legalized to the same type are free.
372 if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc)
373 return 0;
374 }
375
376 if (Opcode == Instruction::Trunc &&
377 TLI->isTruncateFree(SrcLT.second, DstLT.second))
378 return 0;
379
380 if (Opcode == Instruction::ZExt &&
381 TLI->isZExtFree(SrcLT.second, DstLT.second))
382 return 0;
383
384 // If the cast is marked as legal (or promote) then assume low cost.
Stephen Hines36b56882014-04-23 16:57:46 -0700385 if (SrcLT.first == DstLT.first &&
386 TLI->isOperationLegalOrPromote(ISD, DstLT.second))
Nadav Rotem3e40d922013-01-11 19:54:13 +0000387 return 1;
388
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000389 // Handle scalar conversions.
390 if (!Src->isVectorTy() && !Dst->isVectorTy()) {
391
392 // Scalar bitcasts are usually free.
393 if (Opcode == Instruction::BitCast)
394 return 0;
395
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000396 // Just check the op cost. If the operation is legal then assume it costs 1.
397 if (!TLI->isOperationExpand(ISD, DstLT.second))
398 return 1;
399
400 // Assume that illegal scalar instruction are expensive.
401 return 4;
402 }
403
404 // Check vector-to-vector casts.
405 if (Dst->isVectorTy() && Src->isVectorTy()) {
406
407 // If the cast is between same-sized registers, then the check is simple.
408 if (SrcLT.first == DstLT.first &&
409 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
410
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000411 // Assume that Zext is done using AND.
412 if (Opcode == Instruction::ZExt)
413 return 1;
414
415 // Assume that sext is done using SHL and SRA.
416 if (Opcode == Instruction::SExt)
417 return 2;
418
419 // Just check the op cost. If the operation is legal then assume it costs
420 // 1 and multiply by the type-legalization overhead.
421 if (!TLI->isOperationExpand(ISD, DstLT.second))
422 return SrcLT.first * 1;
423 }
424
425 // If we are converting vectors and the operation is illegal, or
426 // if the vectors are legalized to different types, estimate the
427 // scalarization costs.
428 unsigned Num = Dst->getVectorNumElements();
429 unsigned Cost = TopTTI->getCastInstrCost(Opcode, Dst->getScalarType(),
430 Src->getScalarType());
431
432 // Return the cost of multiple scalar invocation plus the cost of
433 // inserting and extracting the values.
434 return getScalarizationOverhead(Dst, true, true) + Num * Cost;
435 }
436
437 // We already handled vector-to-vector and scalar-to-scalar conversions. This
438 // is where we handle bitcast between vectors and scalars. We need to assume
439 // that the conversion is scalarized in one way or another.
440 if (Opcode == Instruction::BitCast)
441 // Illegal bitcasts are done by storing and loading from a stack slot.
442 return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) +
443 (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0);
444
445 llvm_unreachable("Unhandled cast");
446 }
447
448unsigned BasicTTI::getCFInstrCost(unsigned Opcode) const {
449 // Branches are assumed to be predicted.
450 return 0;
451}
452
453unsigned BasicTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
454 Type *CondTy) const {
Bill Wendlingea442812013-06-19 20:51:24 +0000455 const TargetLoweringBase *TLI = getTLI();
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000456 int ISD = TLI->InstructionOpcodeToISD(Opcode);
457 assert(ISD && "Invalid opcode");
458
459 // Selects on vectors are actually vector selects.
460 if (ISD == ISD::SELECT) {
461 assert(CondTy && "CondTy must exist");
462 if (CondTy->isVectorTy())
463 ISD = ISD::VSELECT;
464 }
465
466 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
467
468 if (!TLI->isOperationExpand(ISD, LT.second)) {
469 // The operation is legal. Assume it costs 1. Multiply
470 // by the type-legalization overhead.
471 return LT.first * 1;
472 }
473
474 // Otherwise, assume that the cast is scalarized.
475 if (ValTy->isVectorTy()) {
476 unsigned Num = ValTy->getVectorNumElements();
477 if (CondTy)
478 CondTy = CondTy->getScalarType();
479 unsigned Cost = TopTTI->getCmpSelInstrCost(Opcode, ValTy->getScalarType(),
480 CondTy);
481
482 // Return the cost of multiple scalar invocation plus the cost of inserting
483 // and extracting the values.
484 return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
485 }
486
487 // Unknown scalar opcode.
488 return 1;
489}
490
491unsigned BasicTTI::getVectorInstrCost(unsigned Opcode, Type *Val,
492 unsigned Index) const {
Stephen Hines36b56882014-04-23 16:57:46 -0700493 std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Val->getScalarType());
494
495 return LT.first;
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000496}
497
498unsigned BasicTTI::getMemoryOpCost(unsigned Opcode, Type *Src,
499 unsigned Alignment,
500 unsigned AddressSpace) const {
501 assert(!Src->isVoidTy() && "Invalid type");
Bill Wendlingea442812013-06-19 20:51:24 +0000502 std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Src);
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000503
Stephen Hines36b56882014-04-23 16:57:46 -0700504 // Assuming that all loads of legal types cost 1.
505 unsigned Cost = LT.first;
506
507 if (Src->isVectorTy() &&
508 Src->getPrimitiveSizeInBits() < LT.second.getSizeInBits()) {
509 // This is a vector load that legalizes to a larger type than the vector
510 // itself. Unless the corresponding extending load or truncating store is
511 // legal, then this will scalarize.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700512 TargetLowering::LegalizeAction LA = TargetLowering::Expand;
513 EVT MemVT = getTLI()->getValueType(Src, true);
514 if (MemVT.isSimple() && MemVT != MVT::Other) {
515 if (Opcode == Instruction::Store)
516 LA = getTLI()->getTruncStoreAction(LT.second, MemVT.getSimpleVT());
517 else
518 LA = getTLI()->getLoadExtAction(ISD::EXTLOAD, MemVT.getSimpleVT());
519 }
Stephen Hines36b56882014-04-23 16:57:46 -0700520
521 if (LA != TargetLowering::Legal && LA != TargetLowering::Custom) {
522 // This is a vector load/store for some illegal type that is scalarized.
523 // We must account for the cost of building or decomposing the vector.
524 Cost += getScalarizationOverhead(Src, Opcode != Instruction::Store,
525 Opcode == Instruction::Store);
526 }
527 }
528
529 return Cost;
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000530}
531
Benjamin Kramer8611d442013-02-28 19:09:33 +0000532unsigned BasicTTI::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy,
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000533 ArrayRef<Type *> Tys) const {
Benjamin Kramer8611d442013-02-28 19:09:33 +0000534 unsigned ISD = 0;
535 switch (IID) {
536 default: {
537 // Assume that we need to scalarize this intrinsic.
538 unsigned ScalarizationCost = 0;
539 unsigned ScalarCalls = 1;
540 if (RetTy->isVectorTy()) {
541 ScalarizationCost = getScalarizationOverhead(RetTy, true, false);
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000542 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
543 }
Benjamin Kramer8611d442013-02-28 19:09:33 +0000544 for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) {
545 if (Tys[i]->isVectorTy()) {
546 ScalarizationCost += getScalarizationOverhead(Tys[i], false, true);
547 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
548 }
549 }
550
551 return ScalarCalls + ScalarizationCost;
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000552 }
Benjamin Kramer8611d442013-02-28 19:09:33 +0000553 // Look for intrinsics that can be lowered directly or turned into a scalar
554 // intrinsic call.
555 case Intrinsic::sqrt: ISD = ISD::FSQRT; break;
556 case Intrinsic::sin: ISD = ISD::FSIN; break;
557 case Intrinsic::cos: ISD = ISD::FCOS; break;
558 case Intrinsic::exp: ISD = ISD::FEXP; break;
559 case Intrinsic::exp2: ISD = ISD::FEXP2; break;
560 case Intrinsic::log: ISD = ISD::FLOG; break;
561 case Intrinsic::log10: ISD = ISD::FLOG10; break;
562 case Intrinsic::log2: ISD = ISD::FLOG2; break;
563 case Intrinsic::fabs: ISD = ISD::FABS; break;
Hal Finkel66d1fa62013-08-19 23:35:46 +0000564 case Intrinsic::copysign: ISD = ISD::FCOPYSIGN; break;
Benjamin Kramer8611d442013-02-28 19:09:33 +0000565 case Intrinsic::floor: ISD = ISD::FFLOOR; break;
566 case Intrinsic::ceil: ISD = ISD::FCEIL; break;
567 case Intrinsic::trunc: ISD = ISD::FTRUNC; break;
Hal Finkel04b84c22013-07-08 03:24:07 +0000568 case Intrinsic::nearbyint:
569 ISD = ISD::FNEARBYINT; break;
Benjamin Kramer8611d442013-02-28 19:09:33 +0000570 case Intrinsic::rint: ISD = ISD::FRINT; break;
Hal Finkel41418d12013-08-07 22:49:12 +0000571 case Intrinsic::round: ISD = ISD::FROUND; break;
Benjamin Kramer8611d442013-02-28 19:09:33 +0000572 case Intrinsic::pow: ISD = ISD::FPOW; break;
573 case Intrinsic::fma: ISD = ISD::FMA; break;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700574 case Intrinsic::fmuladd: ISD = ISD::FMA; break;
Arnold Schwaighofer2d66d4c2013-08-06 22:37:52 +0000575 case Intrinsic::lifetime_start:
576 case Intrinsic::lifetime_end:
577 return 0;
Benjamin Kramer8611d442013-02-28 19:09:33 +0000578 }
579
Bill Wendlingea442812013-06-19 20:51:24 +0000580 const TargetLoweringBase *TLI = getTLI();
Benjamin Kramer8611d442013-02-28 19:09:33 +0000581 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(RetTy);
582
583 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
584 // The operation is legal. Assume it costs 1.
585 // If the type is split to multiple registers, assume that thre is some
586 // overhead to this.
587 // TODO: Once we have extract/insert subvector cost we need to use them.
588 if (LT.first > 1)
589 return LT.first * 2;
590 return LT.first * 1;
591 }
592
593 if (!TLI->isOperationExpand(ISD, LT.second)) {
594 // If the operation is custom lowered then assume
595 // thare the code is twice as expensive.
596 return LT.first * 2;
597 }
598
Stephen Hinesdce4a402014-05-29 02:49:00 -0700599 // If we can't lower fmuladd into an FMA estimate the cost as a floating
600 // point mul followed by an add.
601 if (IID == Intrinsic::fmuladd)
602 return TopTTI->getArithmeticInstrCost(BinaryOperator::FMul, RetTy) +
603 TopTTI->getArithmeticInstrCost(BinaryOperator::FAdd, RetTy);
604
Benjamin Kramer8611d442013-02-28 19:09:33 +0000605 // Else, assume that we need to scalarize this intrinsic. For math builtins
606 // this will emit a costly libcall, adding call overhead and spills. Make it
607 // very expensive.
608 if (RetTy->isVectorTy()) {
609 unsigned Num = RetTy->getVectorNumElements();
610 unsigned Cost = TopTTI->getIntrinsicInstrCost(IID, RetTy->getScalarType(),
611 Tys);
612 return 10 * Cost * Num;
613 }
614
615 // This is going to be turned into a library call, make it expensive.
616 return 10;
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000617}
618
619unsigned BasicTTI::getNumberOfParts(Type *Tp) const {
Bill Wendlingea442812013-06-19 20:51:24 +0000620 std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Tp);
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000621 return LT.first;
622}
Arnold Schwaighoferfb55a8f2013-02-08 14:50:48 +0000623
Arnold Schwaighoferc0a11ed2013-07-12 19:16:02 +0000624unsigned BasicTTI::getAddressComputationCost(Type *Ty, bool IsComplex) const {
Arnold Schwaighoferfb55a8f2013-02-08 14:50:48 +0000625 return 0;
626}
Arnold Schwaighofer65457b62013-09-17 18:06:50 +0000627
628unsigned BasicTTI::getReductionCost(unsigned Opcode, Type *Ty,
629 bool IsPairwise) const {
630 assert(Ty->isVectorTy() && "Expect a vector type");
631 unsigned NumVecElts = Ty->getVectorNumElements();
632 unsigned NumReduxLevels = Log2_32(NumVecElts);
633 unsigned ArithCost = NumReduxLevels *
634 TopTTI->getArithmeticInstrCost(Opcode, Ty);
635 // Assume the pairwise shuffles add a cost.
636 unsigned ShuffleCost =
637 NumReduxLevels * (IsPairwise + 1) *
638 TopTTI->getShuffleCost(SK_ExtractSubvector, Ty, NumVecElts / 2, Ty);
639 return ShuffleCost + ArithCost + getScalarizationOverhead(Ty, false, true);
640}