blob: 02cc31b6d2b19da5a1d9c8e039a70e7bbea39617 [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
18#define DEBUG_TYPE "basictti"
19#include "llvm/CodeGen/Passes.h"
Chandler Carruthd3e73552013-01-07 03:08:10 +000020#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruth664e3542013-01-07 01:37:14 +000021#include "llvm/Target/TargetLowering.h"
Chandler Carruth664e3542013-01-07 01:37:14 +000022#include <utility>
23
24using namespace llvm;
25
26namespace {
27
Craig Topper77dfe452014-03-02 08:08:51 +000028class BasicTTI final : public ImmutablePass, public TargetTransformInfo {
Bill Wendlingafc10362013-06-19 20:51:24 +000029 const TargetMachine *TM;
Chandler Carruth664e3542013-01-07 01:37:14 +000030
31 /// Estimate the overhead of scalarizing an instruction. Insert and Extract
32 /// are set if the result needs to be inserted and/or extracted from vectors.
33 unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const;
34
Bill Wendlingafc10362013-06-19 20:51:24 +000035 const TargetLoweringBase *getTLI() const { return TM->getTargetLowering(); }
36
Chandler Carruth664e3542013-01-07 01:37:14 +000037public:
Bill Wendlingafc10362013-06-19 20:51:24 +000038 BasicTTI() : ImmutablePass(ID), TM(0) {
Chandler Carruth664e3542013-01-07 01:37:14 +000039 llvm_unreachable("This pass cannot be directly constructed");
40 }
41
Bill Wendlingafc10362013-06-19 20:51:24 +000042 BasicTTI(const TargetMachine *TM) : ImmutablePass(ID), TM(TM) {
Chandler Carruth664e3542013-01-07 01:37:14 +000043 initializeBasicTTIPass(*PassRegistry::getPassRegistry());
44 }
45
Craig Topper73156022014-03-02 09:09:27 +000046 virtual void initializePass() override {
Chandler Carruth664e3542013-01-07 01:37:14 +000047 pushTTIStack(this);
48 }
49
Craig Topper73156022014-03-02 09:09:27 +000050 virtual void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruth664e3542013-01-07 01:37:14 +000051 TargetTransformInfo::getAnalysisUsage(AU);
52 }
53
54 /// Pass identification.
55 static char ID;
56
57 /// Provide necessary pointer adjustments for the two base classes.
Craig Topper73156022014-03-02 09:09:27 +000058 virtual void *getAdjustedAnalysisPointer(const void *ID) override {
Chandler Carruth664e3542013-01-07 01:37:14 +000059 if (ID == &TargetTransformInfo::ID)
60 return (TargetTransformInfo*)this;
61 return this;
62 }
63
Craig Topper73156022014-03-02 09:09:27 +000064 virtual bool hasBranchDivergence() const override;
Tom Stellard8b1e0212013-07-27 00:01:07 +000065
Chandler Carruth664e3542013-01-07 01:37:14 +000066 /// \name Scalar TTI Implementations
67 /// @{
68
Craig Topper73156022014-03-02 09:09:27 +000069 virtual bool isLegalAddImmediate(int64_t imm) const override;
70 virtual bool isLegalICmpImmediate(int64_t imm) const override;
Chandler Carruth664e3542013-01-07 01:37:14 +000071 virtual bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
72 int64_t BaseOffset, bool HasBaseReg,
Craig Topper73156022014-03-02 09:09:27 +000073 int64_t Scale) const override;
Quentin Colombetbf490d42013-05-31 21:29:03 +000074 virtual int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
75 int64_t BaseOffset, bool HasBaseReg,
Craig Topper73156022014-03-02 09:09:27 +000076 int64_t Scale) const override;
77 virtual bool isTruncateFree(Type *Ty1, Type *Ty2) const override;
78 virtual bool isTypeLegal(Type *Ty) const override;
79 virtual unsigned getJumpBufAlignment() const override;
80 virtual unsigned getJumpBufSize() const override;
81 virtual bool shouldBuildLookupTables() const override;
82 virtual bool haveFastSqrt(Type *Ty) const override;
Juergen Ributzka3e752e72014-01-24 18:22:59 +000083 virtual void getUnrollingPreferences(
Craig Topper73156022014-03-02 09:09:27 +000084 Loop *L, UnrollingPreferences &UP) const override;
Chandler Carruth664e3542013-01-07 01:37:14 +000085
86 /// @}
87
88 /// \name Vector TTI Implementations
89 /// @{
90
Craig Topper73156022014-03-02 09:09:27 +000091 virtual unsigned getNumberOfRegisters(bool Vector) const override;
92 virtual unsigned getMaximumUnrollFactor() const override;
93 virtual unsigned getRegisterBitWidth(bool Vector) const override;
Arnold Schwaighoferb9773872013-04-04 23:26:21 +000094 virtual unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty,
95 OperandValueKind,
Craig Topper73156022014-03-02 09:09:27 +000096 OperandValueKind) const override;
Chandler Carruth664e3542013-01-07 01:37:14 +000097 virtual unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
Craig Topper73156022014-03-02 09:09:27 +000098 int Index, Type *SubTp) const override;
Chandler Carruth664e3542013-01-07 01:37:14 +000099 virtual unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
Craig Topper73156022014-03-02 09:09:27 +0000100 Type *Src) const override;
101 virtual unsigned getCFInstrCost(unsigned Opcode) const override;
Chandler Carruth664e3542013-01-07 01:37:14 +0000102 virtual unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
Craig Topper73156022014-03-02 09:09:27 +0000103 Type *CondTy) const override;
Chandler Carruth664e3542013-01-07 01:37:14 +0000104 virtual unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
Craig Topper73156022014-03-02 09:09:27 +0000105 unsigned Index) const override;
Chandler Carruth664e3542013-01-07 01:37:14 +0000106 virtual unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
107 unsigned Alignment,
Craig Topper73156022014-03-02 09:09:27 +0000108 unsigned AddressSpace) const override;
Juergen Ributzka3e752e72014-01-24 18:22:59 +0000109 virtual unsigned getIntrinsicInstrCost(
Craig Topper73156022014-03-02 09:09:27 +0000110 Intrinsic::ID, Type *RetTy, ArrayRef<Type*> Tys) const override;
111 virtual unsigned getNumberOfParts(Type *Tp) const override;
Juergen Ributzka3e752e72014-01-24 18:22:59 +0000112 virtual unsigned getAddressComputationCost(
Craig Topper73156022014-03-02 09:09:27 +0000113 Type *Ty, bool IsComplex) const override;
Juergen Ributzka3e752e72014-01-24 18:22:59 +0000114 virtual unsigned getReductionCost(unsigned Opcode, Type *Ty,
Craig Topper73156022014-03-02 09:09:27 +0000115 bool IsPairwise) const override;
Chandler Carruth664e3542013-01-07 01:37:14 +0000116
117 /// @}
118};
119
120}
121
122INITIALIZE_AG_PASS(BasicTTI, TargetTransformInfo, "basictti",
123 "Target independent code generator's TTI", true, true, false)
124char BasicTTI::ID = 0;
125
126ImmutablePass *
Bill Wendlingafc10362013-06-19 20:51:24 +0000127llvm::createBasicTargetTransformInfoPass(const TargetMachine *TM) {
128 return new BasicTTI(TM);
Chandler Carruth664e3542013-01-07 01:37:14 +0000129}
130
Tom Stellard8b1e0212013-07-27 00:01:07 +0000131bool BasicTTI::hasBranchDivergence() const { return false; }
Chandler Carruth664e3542013-01-07 01:37:14 +0000132
133bool BasicTTI::isLegalAddImmediate(int64_t imm) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000134 return getTLI()->isLegalAddImmediate(imm);
Chandler Carruth664e3542013-01-07 01:37:14 +0000135}
136
137bool BasicTTI::isLegalICmpImmediate(int64_t imm) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000138 return getTLI()->isLegalICmpImmediate(imm);
Chandler Carruth664e3542013-01-07 01:37:14 +0000139}
140
141bool BasicTTI::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
142 int64_t BaseOffset, bool HasBaseReg,
143 int64_t Scale) const {
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000144 TargetLoweringBase::AddrMode AM;
Chandler Carruth664e3542013-01-07 01:37:14 +0000145 AM.BaseGV = BaseGV;
146 AM.BaseOffs = BaseOffset;
147 AM.HasBaseReg = HasBaseReg;
148 AM.Scale = Scale;
Bill Wendlingafc10362013-06-19 20:51:24 +0000149 return getTLI()->isLegalAddressingMode(AM, Ty);
Chandler Carruth664e3542013-01-07 01:37:14 +0000150}
151
Quentin Colombetbf490d42013-05-31 21:29:03 +0000152int BasicTTI::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
153 int64_t BaseOffset, bool HasBaseReg,
154 int64_t Scale) const {
155 TargetLoweringBase::AddrMode AM;
156 AM.BaseGV = BaseGV;
157 AM.BaseOffs = BaseOffset;
158 AM.HasBaseReg = HasBaseReg;
159 AM.Scale = Scale;
Bill Wendlingafc10362013-06-19 20:51:24 +0000160 return getTLI()->getScalingFactorCost(AM, Ty);
Quentin Colombetbf490d42013-05-31 21:29:03 +0000161}
162
Chandler Carruth664e3542013-01-07 01:37:14 +0000163bool BasicTTI::isTruncateFree(Type *Ty1, Type *Ty2) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000164 return getTLI()->isTruncateFree(Ty1, Ty2);
Chandler Carruth664e3542013-01-07 01:37:14 +0000165}
166
167bool BasicTTI::isTypeLegal(Type *Ty) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000168 EVT T = getTLI()->getValueType(Ty);
169 return getTLI()->isTypeLegal(T);
Chandler Carruth664e3542013-01-07 01:37:14 +0000170}
171
172unsigned BasicTTI::getJumpBufAlignment() const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000173 return getTLI()->getJumpBufAlignment();
Chandler Carruth664e3542013-01-07 01:37:14 +0000174}
175
176unsigned BasicTTI::getJumpBufSize() const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000177 return getTLI()->getJumpBufSize();
Chandler Carruth664e3542013-01-07 01:37:14 +0000178}
179
180bool BasicTTI::shouldBuildLookupTables() const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000181 const TargetLoweringBase *TLI = getTLI();
Chandler Carruth664e3542013-01-07 01:37:14 +0000182 return TLI->supportJumpTables() &&
183 (TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
184 TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
185}
186
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000187bool BasicTTI::haveFastSqrt(Type *Ty) const {
188 const TargetLoweringBase *TLI = getTLI();
189 EVT VT = TLI->getValueType(Ty);
190 return TLI->isTypeLegal(VT) && TLI->isOperationLegalOrCustom(ISD::FSQRT, VT);
191}
192
Hal Finkel8f2e7002013-09-11 19:25:43 +0000193void BasicTTI::getUnrollingPreferences(Loop *, UnrollingPreferences &) const { }
194
Chandler Carruth664e3542013-01-07 01:37:14 +0000195//===----------------------------------------------------------------------===//
196//
197// Calls used by the vectorizers.
198//
199//===----------------------------------------------------------------------===//
200
201unsigned BasicTTI::getScalarizationOverhead(Type *Ty, bool Insert,
202 bool Extract) const {
203 assert (Ty->isVectorTy() && "Can only scalarize vectors");
204 unsigned Cost = 0;
205
206 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
207 if (Insert)
208 Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
209 if (Extract)
210 Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
211 }
212
213 return Cost;
214}
215
216unsigned BasicTTI::getNumberOfRegisters(bool Vector) const {
217 return 1;
218}
219
Nadav Rotemb1791a72013-01-09 22:29:00 +0000220unsigned BasicTTI::getRegisterBitWidth(bool Vector) const {
221 return 32;
222}
223
Nadav Rotemb696c362013-01-09 01:15:42 +0000224unsigned BasicTTI::getMaximumUnrollFactor() const {
225 return 1;
226}
227
Arnold Schwaighoferb9773872013-04-04 23:26:21 +0000228unsigned BasicTTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty,
229 OperandValueKind,
230 OperandValueKind) const {
Chandler Carruth664e3542013-01-07 01:37:14 +0000231 // Check if any of the operands are vector operands.
Bill Wendlingafc10362013-06-19 20:51:24 +0000232 const TargetLoweringBase *TLI = getTLI();
Chandler Carruth664e3542013-01-07 01:37:14 +0000233 int ISD = TLI->InstructionOpcodeToISD(Opcode);
234 assert(ISD && "Invalid opcode");
235
236 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
237
Nadav Rotem87a0af62013-04-12 21:15:03 +0000238 bool IsFloat = Ty->getScalarType()->isFloatingPointTy();
Nadav Rotem0db06902013-04-14 05:55:18 +0000239 // Assume that floating point arithmetic operations cost twice as much as
240 // integer operations.
Nadav Rotem87a0af62013-04-12 21:15:03 +0000241 unsigned OpCost = (IsFloat ? 2 : 1);
242
Chandler Carruth664e3542013-01-07 01:37:14 +0000243 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
244 // The operation is legal. Assume it costs 1.
Nadav Rotem0db06902013-04-14 05:55:18 +0000245 // If the type is split to multiple registers, assume that there is some
Chandler Carruth664e3542013-01-07 01:37:14 +0000246 // overhead to this.
247 // TODO: Once we have extract/insert subvector cost we need to use them.
248 if (LT.first > 1)
Nadav Rotem87a0af62013-04-12 21:15:03 +0000249 return LT.first * 2 * OpCost;
250 return LT.first * 1 * OpCost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000251 }
252
253 if (!TLI->isOperationExpand(ISD, LT.second)) {
254 // If the operation is custom lowered then assume
255 // thare the code is twice as expensive.
Nadav Rotem87a0af62013-04-12 21:15:03 +0000256 return LT.first * 2 * OpCost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000257 }
258
259 // Else, assume that we need to scalarize this op.
260 if (Ty->isVectorTy()) {
261 unsigned Num = Ty->getVectorNumElements();
262 unsigned Cost = TopTTI->getArithmeticInstrCost(Opcode, Ty->getScalarType());
263 // return the cost of multiple scalar invocation plus the cost of inserting
264 // and extracting the values.
265 return getScalarizationOverhead(Ty, true, true) + Num * Cost;
266 }
267
268 // We don't know anything about this scalar instruction.
Nadav Rotem87a0af62013-04-12 21:15:03 +0000269 return OpCost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000270}
271
272unsigned BasicTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
273 Type *SubTp) const {
274 return 1;
275}
276
277unsigned BasicTTI::getCastInstrCost(unsigned Opcode, Type *Dst,
278 Type *Src) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000279 const TargetLoweringBase *TLI = getTLI();
Chandler Carruth664e3542013-01-07 01:37:14 +0000280 int ISD = TLI->InstructionOpcodeToISD(Opcode);
281 assert(ISD && "Invalid opcode");
282
283 std::pair<unsigned, MVT> SrcLT = TLI->getTypeLegalizationCost(Src);
284 std::pair<unsigned, MVT> DstLT = TLI->getTypeLegalizationCost(Dst);
285
Nadav Roteme55aa3c2013-01-11 19:54:13 +0000286 // Check for NOOP conversions.
287 if (SrcLT.first == DstLT.first &&
288 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
289
290 // Bitcast between types that are legalized to the same type are free.
291 if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc)
292 return 0;
293 }
294
295 if (Opcode == Instruction::Trunc &&
296 TLI->isTruncateFree(SrcLT.second, DstLT.second))
297 return 0;
298
299 if (Opcode == Instruction::ZExt &&
300 TLI->isZExtFree(SrcLT.second, DstLT.second))
301 return 0;
302
303 // If the cast is marked as legal (or promote) then assume low cost.
304 if (TLI->isOperationLegalOrPromote(ISD, DstLT.second))
305 return 1;
306
Chandler Carruth664e3542013-01-07 01:37:14 +0000307 // Handle scalar conversions.
308 if (!Src->isVectorTy() && !Dst->isVectorTy()) {
309
310 // Scalar bitcasts are usually free.
311 if (Opcode == Instruction::BitCast)
312 return 0;
313
Chandler Carruth664e3542013-01-07 01:37:14 +0000314 // Just check the op cost. If the operation is legal then assume it costs 1.
315 if (!TLI->isOperationExpand(ISD, DstLT.second))
316 return 1;
317
318 // Assume that illegal scalar instruction are expensive.
319 return 4;
320 }
321
322 // Check vector-to-vector casts.
323 if (Dst->isVectorTy() && Src->isVectorTy()) {
324
325 // If the cast is between same-sized registers, then the check is simple.
326 if (SrcLT.first == DstLT.first &&
327 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
328
Chandler Carruth664e3542013-01-07 01:37:14 +0000329 // Assume that Zext is done using AND.
330 if (Opcode == Instruction::ZExt)
331 return 1;
332
333 // Assume that sext is done using SHL and SRA.
334 if (Opcode == Instruction::SExt)
335 return 2;
336
337 // Just check the op cost. If the operation is legal then assume it costs
338 // 1 and multiply by the type-legalization overhead.
339 if (!TLI->isOperationExpand(ISD, DstLT.second))
340 return SrcLT.first * 1;
341 }
342
343 // If we are converting vectors and the operation is illegal, or
344 // if the vectors are legalized to different types, estimate the
345 // scalarization costs.
346 unsigned Num = Dst->getVectorNumElements();
347 unsigned Cost = TopTTI->getCastInstrCost(Opcode, Dst->getScalarType(),
348 Src->getScalarType());
349
350 // Return the cost of multiple scalar invocation plus the cost of
351 // inserting and extracting the values.
352 return getScalarizationOverhead(Dst, true, true) + Num * Cost;
353 }
354
355 // We already handled vector-to-vector and scalar-to-scalar conversions. This
356 // is where we handle bitcast between vectors and scalars. We need to assume
357 // that the conversion is scalarized in one way or another.
358 if (Opcode == Instruction::BitCast)
359 // Illegal bitcasts are done by storing and loading from a stack slot.
360 return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) +
361 (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0);
362
363 llvm_unreachable("Unhandled cast");
364 }
365
366unsigned BasicTTI::getCFInstrCost(unsigned Opcode) const {
367 // Branches are assumed to be predicted.
368 return 0;
369}
370
371unsigned BasicTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
372 Type *CondTy) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000373 const TargetLoweringBase *TLI = getTLI();
Chandler Carruth664e3542013-01-07 01:37:14 +0000374 int ISD = TLI->InstructionOpcodeToISD(Opcode);
375 assert(ISD && "Invalid opcode");
376
377 // Selects on vectors are actually vector selects.
378 if (ISD == ISD::SELECT) {
379 assert(CondTy && "CondTy must exist");
380 if (CondTy->isVectorTy())
381 ISD = ISD::VSELECT;
382 }
383
384 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
385
386 if (!TLI->isOperationExpand(ISD, LT.second)) {
387 // The operation is legal. Assume it costs 1. Multiply
388 // by the type-legalization overhead.
389 return LT.first * 1;
390 }
391
392 // Otherwise, assume that the cast is scalarized.
393 if (ValTy->isVectorTy()) {
394 unsigned Num = ValTy->getVectorNumElements();
395 if (CondTy)
396 CondTy = CondTy->getScalarType();
397 unsigned Cost = TopTTI->getCmpSelInstrCost(Opcode, ValTy->getScalarType(),
398 CondTy);
399
400 // Return the cost of multiple scalar invocation plus the cost of inserting
401 // and extracting the values.
402 return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
403 }
404
405 // Unknown scalar opcode.
406 return 1;
407}
408
409unsigned BasicTTI::getVectorInstrCost(unsigned Opcode, Type *Val,
410 unsigned Index) const {
411 return 1;
412}
413
414unsigned BasicTTI::getMemoryOpCost(unsigned Opcode, Type *Src,
415 unsigned Alignment,
416 unsigned AddressSpace) const {
417 assert(!Src->isVoidTy() && "Invalid type");
Bill Wendlingafc10362013-06-19 20:51:24 +0000418 std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Src);
Chandler Carruth664e3542013-01-07 01:37:14 +0000419
420 // Assume that all loads of legal types cost 1.
421 return LT.first;
422}
423
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000424unsigned BasicTTI::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy,
Chandler Carruth664e3542013-01-07 01:37:14 +0000425 ArrayRef<Type *> Tys) const {
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000426 unsigned ISD = 0;
427 switch (IID) {
428 default: {
429 // Assume that we need to scalarize this intrinsic.
430 unsigned ScalarizationCost = 0;
431 unsigned ScalarCalls = 1;
432 if (RetTy->isVectorTy()) {
433 ScalarizationCost = getScalarizationOverhead(RetTy, true, false);
Chandler Carruth664e3542013-01-07 01:37:14 +0000434 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
435 }
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000436 for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) {
437 if (Tys[i]->isVectorTy()) {
438 ScalarizationCost += getScalarizationOverhead(Tys[i], false, true);
439 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
440 }
441 }
442
443 return ScalarCalls + ScalarizationCost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000444 }
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000445 // Look for intrinsics that can be lowered directly or turned into a scalar
446 // intrinsic call.
447 case Intrinsic::sqrt: ISD = ISD::FSQRT; break;
448 case Intrinsic::sin: ISD = ISD::FSIN; break;
449 case Intrinsic::cos: ISD = ISD::FCOS; break;
450 case Intrinsic::exp: ISD = ISD::FEXP; break;
451 case Intrinsic::exp2: ISD = ISD::FEXP2; break;
452 case Intrinsic::log: ISD = ISD::FLOG; break;
453 case Intrinsic::log10: ISD = ISD::FLOG10; break;
454 case Intrinsic::log2: ISD = ISD::FLOG2; break;
455 case Intrinsic::fabs: ISD = ISD::FABS; break;
Hal Finkel0c5c01aa2013-08-19 23:35:46 +0000456 case Intrinsic::copysign: ISD = ISD::FCOPYSIGN; break;
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000457 case Intrinsic::floor: ISD = ISD::FFLOOR; break;
458 case Intrinsic::ceil: ISD = ISD::FCEIL; break;
459 case Intrinsic::trunc: ISD = ISD::FTRUNC; break;
Hal Finkelec474f22013-07-08 03:24:07 +0000460 case Intrinsic::nearbyint:
461 ISD = ISD::FNEARBYINT; break;
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000462 case Intrinsic::rint: ISD = ISD::FRINT; break;
Hal Finkel171817e2013-08-07 22:49:12 +0000463 case Intrinsic::round: ISD = ISD::FROUND; break;
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000464 case Intrinsic::pow: ISD = ISD::FPOW; break;
465 case Intrinsic::fma: ISD = ISD::FMA; break;
466 case Intrinsic::fmuladd: ISD = ISD::FMA; break; // FIXME: mul + add?
Arnold Schwaighofera7cd6bf2013-08-06 22:37:52 +0000467 case Intrinsic::lifetime_start:
468 case Intrinsic::lifetime_end:
469 return 0;
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000470 }
471
Bill Wendlingafc10362013-06-19 20:51:24 +0000472 const TargetLoweringBase *TLI = getTLI();
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000473 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(RetTy);
474
475 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
476 // The operation is legal. Assume it costs 1.
477 // If the type is split to multiple registers, assume that thre is some
478 // overhead to this.
479 // TODO: Once we have extract/insert subvector cost we need to use them.
480 if (LT.first > 1)
481 return LT.first * 2;
482 return LT.first * 1;
483 }
484
485 if (!TLI->isOperationExpand(ISD, LT.second)) {
486 // If the operation is custom lowered then assume
487 // thare the code is twice as expensive.
488 return LT.first * 2;
489 }
490
491 // Else, assume that we need to scalarize this intrinsic. For math builtins
492 // this will emit a costly libcall, adding call overhead and spills. Make it
493 // very expensive.
494 if (RetTy->isVectorTy()) {
495 unsigned Num = RetTy->getVectorNumElements();
496 unsigned Cost = TopTTI->getIntrinsicInstrCost(IID, RetTy->getScalarType(),
497 Tys);
498 return 10 * Cost * Num;
499 }
500
501 // This is going to be turned into a library call, make it expensive.
502 return 10;
Chandler Carruth664e3542013-01-07 01:37:14 +0000503}
504
505unsigned BasicTTI::getNumberOfParts(Type *Tp) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000506 std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Tp);
Chandler Carruth664e3542013-01-07 01:37:14 +0000507 return LT.first;
508}
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000509
Arnold Schwaighofer9da9a432013-07-12 19:16:02 +0000510unsigned BasicTTI::getAddressComputationCost(Type *Ty, bool IsComplex) const {
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000511 return 0;
512}
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000513
514unsigned BasicTTI::getReductionCost(unsigned Opcode, Type *Ty,
515 bool IsPairwise) const {
516 assert(Ty->isVectorTy() && "Expect a vector type");
517 unsigned NumVecElts = Ty->getVectorNumElements();
518 unsigned NumReduxLevels = Log2_32(NumVecElts);
519 unsigned ArithCost = NumReduxLevels *
520 TopTTI->getArithmeticInstrCost(Opcode, Ty);
521 // Assume the pairwise shuffles add a cost.
522 unsigned ShuffleCost =
523 NumReduxLevels * (IsPairwise + 1) *
524 TopTTI->getShuffleCost(SK_ExtractSubvector, Ty, NumVecElts / 2, Ty);
525 return ShuffleCost + ArithCost + getScalarizationOverhead(Ty, false, true);
526}