blob: c12a90d1f7c0697ddfa0322e78d9dd7f0af91a5d [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
Juergen Ributzka3e752e72014-01-24 18:22:59 +000046 virtual void initializePass() LLVM_OVERRIDE {
Chandler Carruth664e3542013-01-07 01:37:14 +000047 pushTTIStack(this);
48 }
49
50 virtual void finalizePass() {
51 popTTIStack();
52 }
53
Juergen Ributzka3e752e72014-01-24 18:22:59 +000054 virtual void getAnalysisUsage(AnalysisUsage &AU) const LLVM_OVERRIDE {
Chandler Carruth664e3542013-01-07 01:37:14 +000055 TargetTransformInfo::getAnalysisUsage(AU);
56 }
57
58 /// Pass identification.
59 static char ID;
60
61 /// Provide necessary pointer adjustments for the two base classes.
Juergen Ributzka3e752e72014-01-24 18:22:59 +000062 virtual void *getAdjustedAnalysisPointer(const void *ID) LLVM_OVERRIDE {
Chandler Carruth664e3542013-01-07 01:37:14 +000063 if (ID == &TargetTransformInfo::ID)
64 return (TargetTransformInfo*)this;
65 return this;
66 }
67
Juergen Ributzka3e752e72014-01-24 18:22:59 +000068 virtual bool hasBranchDivergence() const LLVM_OVERRIDE;
Tom Stellard8b1e0212013-07-27 00:01:07 +000069
Chandler Carruth664e3542013-01-07 01:37:14 +000070 /// \name Scalar TTI Implementations
71 /// @{
72
Juergen Ributzka3e752e72014-01-24 18:22:59 +000073 virtual bool isLegalAddImmediate(int64_t imm) const LLVM_OVERRIDE;
74 virtual bool isLegalICmpImmediate(int64_t imm) const LLVM_OVERRIDE;
Chandler Carruth664e3542013-01-07 01:37:14 +000075 virtual bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
76 int64_t BaseOffset, bool HasBaseReg,
Juergen Ributzka3e752e72014-01-24 18:22:59 +000077 int64_t Scale) const LLVM_OVERRIDE;
Quentin Colombetbf490d42013-05-31 21:29:03 +000078 virtual int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
79 int64_t BaseOffset, bool HasBaseReg,
Juergen Ributzka3e752e72014-01-24 18:22:59 +000080 int64_t Scale) const LLVM_OVERRIDE;
81 virtual bool isTruncateFree(Type *Ty1, Type *Ty2) const LLVM_OVERRIDE;
82 virtual bool isTypeLegal(Type *Ty) const LLVM_OVERRIDE;
83 virtual unsigned getJumpBufAlignment() const LLVM_OVERRIDE;
84 virtual unsigned getJumpBufSize() const LLVM_OVERRIDE;
85 virtual bool shouldBuildLookupTables() const LLVM_OVERRIDE;
86 virtual bool haveFastSqrt(Type *Ty) const LLVM_OVERRIDE;
87 virtual void getUnrollingPreferences(
88 Loop *L, UnrollingPreferences &UP) const LLVM_OVERRIDE;
Chandler Carruth664e3542013-01-07 01:37:14 +000089
90 /// @}
91
92 /// \name Vector TTI Implementations
93 /// @{
94
Juergen Ributzka3e752e72014-01-24 18:22:59 +000095 virtual unsigned getNumberOfRegisters(bool Vector) const LLVM_OVERRIDE;
96 virtual unsigned getMaximumUnrollFactor() const LLVM_OVERRIDE;
97 virtual unsigned getRegisterBitWidth(bool Vector) const LLVM_OVERRIDE;
Arnold Schwaighoferb9773872013-04-04 23:26:21 +000098 virtual unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty,
99 OperandValueKind,
Juergen Ributzka3e752e72014-01-24 18:22:59 +0000100 OperandValueKind) const LLVM_OVERRIDE;
Chandler Carruth664e3542013-01-07 01:37:14 +0000101 virtual unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
Juergen Ributzka3e752e72014-01-24 18:22:59 +0000102 int Index, Type *SubTp) const LLVM_OVERRIDE;
Chandler Carruth664e3542013-01-07 01:37:14 +0000103 virtual unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
Juergen Ributzka3e752e72014-01-24 18:22:59 +0000104 Type *Src) const LLVM_OVERRIDE;
105 virtual unsigned getCFInstrCost(unsigned Opcode) const LLVM_OVERRIDE;
Chandler Carruth664e3542013-01-07 01:37:14 +0000106 virtual unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
Juergen Ributzka3e752e72014-01-24 18:22:59 +0000107 Type *CondTy) const LLVM_OVERRIDE;
Chandler Carruth664e3542013-01-07 01:37:14 +0000108 virtual unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
Juergen Ributzka3e752e72014-01-24 18:22:59 +0000109 unsigned Index) const LLVM_OVERRIDE;
Chandler Carruth664e3542013-01-07 01:37:14 +0000110 virtual unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
111 unsigned Alignment,
Juergen Ributzka3e752e72014-01-24 18:22:59 +0000112 unsigned AddressSpace) const LLVM_OVERRIDE;
113 virtual unsigned getIntrinsicInstrCost(
114 Intrinsic::ID, Type *RetTy, ArrayRef<Type*> Tys) const LLVM_OVERRIDE;
115 virtual unsigned getNumberOfParts(Type *Tp) const LLVM_OVERRIDE;
116 virtual unsigned getAddressComputationCost(
117 Type *Ty, bool IsComplex) const LLVM_OVERRIDE;
118 virtual unsigned getReductionCost(unsigned Opcode, Type *Ty,
119 bool IsPairwise) const LLVM_OVERRIDE;
Chandler Carruth664e3542013-01-07 01:37:14 +0000120
121 /// @}
122};
123
124}
125
126INITIALIZE_AG_PASS(BasicTTI, TargetTransformInfo, "basictti",
127 "Target independent code generator's TTI", true, true, false)
128char BasicTTI::ID = 0;
129
130ImmutablePass *
Bill Wendlingafc10362013-06-19 20:51:24 +0000131llvm::createBasicTargetTransformInfoPass(const TargetMachine *TM) {
132 return new BasicTTI(TM);
Chandler Carruth664e3542013-01-07 01:37:14 +0000133}
134
Tom Stellard8b1e0212013-07-27 00:01:07 +0000135bool BasicTTI::hasBranchDivergence() const { return false; }
Chandler Carruth664e3542013-01-07 01:37:14 +0000136
137bool BasicTTI::isLegalAddImmediate(int64_t imm) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000138 return getTLI()->isLegalAddImmediate(imm);
Chandler Carruth664e3542013-01-07 01:37:14 +0000139}
140
141bool BasicTTI::isLegalICmpImmediate(int64_t imm) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000142 return getTLI()->isLegalICmpImmediate(imm);
Chandler Carruth664e3542013-01-07 01:37:14 +0000143}
144
145bool BasicTTI::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
146 int64_t BaseOffset, bool HasBaseReg,
147 int64_t Scale) const {
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000148 TargetLoweringBase::AddrMode AM;
Chandler Carruth664e3542013-01-07 01:37:14 +0000149 AM.BaseGV = BaseGV;
150 AM.BaseOffs = BaseOffset;
151 AM.HasBaseReg = HasBaseReg;
152 AM.Scale = Scale;
Bill Wendlingafc10362013-06-19 20:51:24 +0000153 return getTLI()->isLegalAddressingMode(AM, Ty);
Chandler Carruth664e3542013-01-07 01:37:14 +0000154}
155
Quentin Colombetbf490d42013-05-31 21:29:03 +0000156int BasicTTI::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
157 int64_t BaseOffset, bool HasBaseReg,
158 int64_t Scale) const {
159 TargetLoweringBase::AddrMode AM;
160 AM.BaseGV = BaseGV;
161 AM.BaseOffs = BaseOffset;
162 AM.HasBaseReg = HasBaseReg;
163 AM.Scale = Scale;
Bill Wendlingafc10362013-06-19 20:51:24 +0000164 return getTLI()->getScalingFactorCost(AM, Ty);
Quentin Colombetbf490d42013-05-31 21:29:03 +0000165}
166
Chandler Carruth664e3542013-01-07 01:37:14 +0000167bool BasicTTI::isTruncateFree(Type *Ty1, Type *Ty2) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000168 return getTLI()->isTruncateFree(Ty1, Ty2);
Chandler Carruth664e3542013-01-07 01:37:14 +0000169}
170
171bool BasicTTI::isTypeLegal(Type *Ty) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000172 EVT T = getTLI()->getValueType(Ty);
173 return getTLI()->isTypeLegal(T);
Chandler Carruth664e3542013-01-07 01:37:14 +0000174}
175
176unsigned BasicTTI::getJumpBufAlignment() const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000177 return getTLI()->getJumpBufAlignment();
Chandler Carruth664e3542013-01-07 01:37:14 +0000178}
179
180unsigned BasicTTI::getJumpBufSize() const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000181 return getTLI()->getJumpBufSize();
Chandler Carruth664e3542013-01-07 01:37:14 +0000182}
183
184bool BasicTTI::shouldBuildLookupTables() const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000185 const TargetLoweringBase *TLI = getTLI();
Chandler Carruth664e3542013-01-07 01:37:14 +0000186 return TLI->supportJumpTables() &&
187 (TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
188 TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
189}
190
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000191bool BasicTTI::haveFastSqrt(Type *Ty) const {
192 const TargetLoweringBase *TLI = getTLI();
193 EVT VT = TLI->getValueType(Ty);
194 return TLI->isTypeLegal(VT) && TLI->isOperationLegalOrCustom(ISD::FSQRT, VT);
195}
196
Hal Finkel8f2e7002013-09-11 19:25:43 +0000197void BasicTTI::getUnrollingPreferences(Loop *, UnrollingPreferences &) const { }
198
Chandler Carruth664e3542013-01-07 01:37:14 +0000199//===----------------------------------------------------------------------===//
200//
201// Calls used by the vectorizers.
202//
203//===----------------------------------------------------------------------===//
204
205unsigned BasicTTI::getScalarizationOverhead(Type *Ty, bool Insert,
206 bool Extract) const {
207 assert (Ty->isVectorTy() && "Can only scalarize vectors");
208 unsigned Cost = 0;
209
210 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
211 if (Insert)
212 Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
213 if (Extract)
214 Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
215 }
216
217 return Cost;
218}
219
220unsigned BasicTTI::getNumberOfRegisters(bool Vector) const {
221 return 1;
222}
223
Nadav Rotemb1791a72013-01-09 22:29:00 +0000224unsigned BasicTTI::getRegisterBitWidth(bool Vector) const {
225 return 32;
226}
227
Nadav Rotemb696c362013-01-09 01:15:42 +0000228unsigned BasicTTI::getMaximumUnrollFactor() const {
229 return 1;
230}
231
Arnold Schwaighoferb9773872013-04-04 23:26:21 +0000232unsigned BasicTTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty,
233 OperandValueKind,
234 OperandValueKind) const {
Chandler Carruth664e3542013-01-07 01:37:14 +0000235 // Check if any of the operands are vector operands.
Bill Wendlingafc10362013-06-19 20:51:24 +0000236 const TargetLoweringBase *TLI = getTLI();
Chandler Carruth664e3542013-01-07 01:37:14 +0000237 int ISD = TLI->InstructionOpcodeToISD(Opcode);
238 assert(ISD && "Invalid opcode");
239
240 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
241
Nadav Rotem87a0af62013-04-12 21:15:03 +0000242 bool IsFloat = Ty->getScalarType()->isFloatingPointTy();
Nadav Rotem0db06902013-04-14 05:55:18 +0000243 // Assume that floating point arithmetic operations cost twice as much as
244 // integer operations.
Nadav Rotem87a0af62013-04-12 21:15:03 +0000245 unsigned OpCost = (IsFloat ? 2 : 1);
246
Chandler Carruth664e3542013-01-07 01:37:14 +0000247 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
248 // The operation is legal. Assume it costs 1.
Nadav Rotem0db06902013-04-14 05:55:18 +0000249 // If the type is split to multiple registers, assume that there is some
Chandler Carruth664e3542013-01-07 01:37:14 +0000250 // overhead to this.
251 // TODO: Once we have extract/insert subvector cost we need to use them.
252 if (LT.first > 1)
Nadav Rotem87a0af62013-04-12 21:15:03 +0000253 return LT.first * 2 * OpCost;
254 return LT.first * 1 * OpCost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000255 }
256
257 if (!TLI->isOperationExpand(ISD, LT.second)) {
258 // If the operation is custom lowered then assume
259 // thare the code is twice as expensive.
Nadav Rotem87a0af62013-04-12 21:15:03 +0000260 return LT.first * 2 * OpCost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000261 }
262
263 // Else, assume that we need to scalarize this op.
264 if (Ty->isVectorTy()) {
265 unsigned Num = Ty->getVectorNumElements();
266 unsigned Cost = TopTTI->getArithmeticInstrCost(Opcode, Ty->getScalarType());
267 // return the cost of multiple scalar invocation plus the cost of inserting
268 // and extracting the values.
269 return getScalarizationOverhead(Ty, true, true) + Num * Cost;
270 }
271
272 // We don't know anything about this scalar instruction.
Nadav Rotem87a0af62013-04-12 21:15:03 +0000273 return OpCost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000274}
275
276unsigned BasicTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
277 Type *SubTp) const {
278 return 1;
279}
280
281unsigned BasicTTI::getCastInstrCost(unsigned Opcode, Type *Dst,
282 Type *Src) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000283 const TargetLoweringBase *TLI = getTLI();
Chandler Carruth664e3542013-01-07 01:37:14 +0000284 int ISD = TLI->InstructionOpcodeToISD(Opcode);
285 assert(ISD && "Invalid opcode");
286
287 std::pair<unsigned, MVT> SrcLT = TLI->getTypeLegalizationCost(Src);
288 std::pair<unsigned, MVT> DstLT = TLI->getTypeLegalizationCost(Dst);
289
Nadav Roteme55aa3c2013-01-11 19:54:13 +0000290 // Check for NOOP conversions.
291 if (SrcLT.first == DstLT.first &&
292 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
293
294 // Bitcast between types that are legalized to the same type are free.
295 if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc)
296 return 0;
297 }
298
299 if (Opcode == Instruction::Trunc &&
300 TLI->isTruncateFree(SrcLT.second, DstLT.second))
301 return 0;
302
303 if (Opcode == Instruction::ZExt &&
304 TLI->isZExtFree(SrcLT.second, DstLT.second))
305 return 0;
306
307 // If the cast is marked as legal (or promote) then assume low cost.
308 if (TLI->isOperationLegalOrPromote(ISD, DstLT.second))
309 return 1;
310
Chandler Carruth664e3542013-01-07 01:37:14 +0000311 // Handle scalar conversions.
312 if (!Src->isVectorTy() && !Dst->isVectorTy()) {
313
314 // Scalar bitcasts are usually free.
315 if (Opcode == Instruction::BitCast)
316 return 0;
317
Chandler Carruth664e3542013-01-07 01:37:14 +0000318 // Just check the op cost. If the operation is legal then assume it costs 1.
319 if (!TLI->isOperationExpand(ISD, DstLT.second))
320 return 1;
321
322 // Assume that illegal scalar instruction are expensive.
323 return 4;
324 }
325
326 // Check vector-to-vector casts.
327 if (Dst->isVectorTy() && Src->isVectorTy()) {
328
329 // If the cast is between same-sized registers, then the check is simple.
330 if (SrcLT.first == DstLT.first &&
331 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
332
Chandler Carruth664e3542013-01-07 01:37:14 +0000333 // Assume that Zext is done using AND.
334 if (Opcode == Instruction::ZExt)
335 return 1;
336
337 // Assume that sext is done using SHL and SRA.
338 if (Opcode == Instruction::SExt)
339 return 2;
340
341 // Just check the op cost. If the operation is legal then assume it costs
342 // 1 and multiply by the type-legalization overhead.
343 if (!TLI->isOperationExpand(ISD, DstLT.second))
344 return SrcLT.first * 1;
345 }
346
347 // If we are converting vectors and the operation is illegal, or
348 // if the vectors are legalized to different types, estimate the
349 // scalarization costs.
350 unsigned Num = Dst->getVectorNumElements();
351 unsigned Cost = TopTTI->getCastInstrCost(Opcode, Dst->getScalarType(),
352 Src->getScalarType());
353
354 // Return the cost of multiple scalar invocation plus the cost of
355 // inserting and extracting the values.
356 return getScalarizationOverhead(Dst, true, true) + Num * Cost;
357 }
358
359 // We already handled vector-to-vector and scalar-to-scalar conversions. This
360 // is where we handle bitcast between vectors and scalars. We need to assume
361 // that the conversion is scalarized in one way or another.
362 if (Opcode == Instruction::BitCast)
363 // Illegal bitcasts are done by storing and loading from a stack slot.
364 return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) +
365 (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0);
366
367 llvm_unreachable("Unhandled cast");
368 }
369
370unsigned BasicTTI::getCFInstrCost(unsigned Opcode) const {
371 // Branches are assumed to be predicted.
372 return 0;
373}
374
375unsigned BasicTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
376 Type *CondTy) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000377 const TargetLoweringBase *TLI = getTLI();
Chandler Carruth664e3542013-01-07 01:37:14 +0000378 int ISD = TLI->InstructionOpcodeToISD(Opcode);
379 assert(ISD && "Invalid opcode");
380
381 // Selects on vectors are actually vector selects.
382 if (ISD == ISD::SELECT) {
383 assert(CondTy && "CondTy must exist");
384 if (CondTy->isVectorTy())
385 ISD = ISD::VSELECT;
386 }
387
388 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
389
390 if (!TLI->isOperationExpand(ISD, LT.second)) {
391 // The operation is legal. Assume it costs 1. Multiply
392 // by the type-legalization overhead.
393 return LT.first * 1;
394 }
395
396 // Otherwise, assume that the cast is scalarized.
397 if (ValTy->isVectorTy()) {
398 unsigned Num = ValTy->getVectorNumElements();
399 if (CondTy)
400 CondTy = CondTy->getScalarType();
401 unsigned Cost = TopTTI->getCmpSelInstrCost(Opcode, ValTy->getScalarType(),
402 CondTy);
403
404 // Return the cost of multiple scalar invocation plus the cost of inserting
405 // and extracting the values.
406 return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
407 }
408
409 // Unknown scalar opcode.
410 return 1;
411}
412
413unsigned BasicTTI::getVectorInstrCost(unsigned Opcode, Type *Val,
414 unsigned Index) const {
415 return 1;
416}
417
418unsigned BasicTTI::getMemoryOpCost(unsigned Opcode, Type *Src,
419 unsigned Alignment,
420 unsigned AddressSpace) const {
421 assert(!Src->isVoidTy() && "Invalid type");
Bill Wendlingafc10362013-06-19 20:51:24 +0000422 std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Src);
Chandler Carruth664e3542013-01-07 01:37:14 +0000423
424 // Assume that all loads of legal types cost 1.
425 return LT.first;
426}
427
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000428unsigned BasicTTI::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy,
Chandler Carruth664e3542013-01-07 01:37:14 +0000429 ArrayRef<Type *> Tys) const {
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000430 unsigned ISD = 0;
431 switch (IID) {
432 default: {
433 // Assume that we need to scalarize this intrinsic.
434 unsigned ScalarizationCost = 0;
435 unsigned ScalarCalls = 1;
436 if (RetTy->isVectorTy()) {
437 ScalarizationCost = getScalarizationOverhead(RetTy, true, false);
Chandler Carruth664e3542013-01-07 01:37:14 +0000438 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
439 }
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000440 for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) {
441 if (Tys[i]->isVectorTy()) {
442 ScalarizationCost += getScalarizationOverhead(Tys[i], false, true);
443 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
444 }
445 }
446
447 return ScalarCalls + ScalarizationCost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000448 }
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000449 // Look for intrinsics that can be lowered directly or turned into a scalar
450 // intrinsic call.
451 case Intrinsic::sqrt: ISD = ISD::FSQRT; break;
452 case Intrinsic::sin: ISD = ISD::FSIN; break;
453 case Intrinsic::cos: ISD = ISD::FCOS; break;
454 case Intrinsic::exp: ISD = ISD::FEXP; break;
455 case Intrinsic::exp2: ISD = ISD::FEXP2; break;
456 case Intrinsic::log: ISD = ISD::FLOG; break;
457 case Intrinsic::log10: ISD = ISD::FLOG10; break;
458 case Intrinsic::log2: ISD = ISD::FLOG2; break;
459 case Intrinsic::fabs: ISD = ISD::FABS; break;
Hal Finkel0c5c01aa2013-08-19 23:35:46 +0000460 case Intrinsic::copysign: ISD = ISD::FCOPYSIGN; break;
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000461 case Intrinsic::floor: ISD = ISD::FFLOOR; break;
462 case Intrinsic::ceil: ISD = ISD::FCEIL; break;
463 case Intrinsic::trunc: ISD = ISD::FTRUNC; break;
Hal Finkelec474f22013-07-08 03:24:07 +0000464 case Intrinsic::nearbyint:
465 ISD = ISD::FNEARBYINT; break;
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000466 case Intrinsic::rint: ISD = ISD::FRINT; break;
Hal Finkel171817e2013-08-07 22:49:12 +0000467 case Intrinsic::round: ISD = ISD::FROUND; break;
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000468 case Intrinsic::pow: ISD = ISD::FPOW; break;
469 case Intrinsic::fma: ISD = ISD::FMA; break;
470 case Intrinsic::fmuladd: ISD = ISD::FMA; break; // FIXME: mul + add?
Arnold Schwaighofera7cd6bf2013-08-06 22:37:52 +0000471 case Intrinsic::lifetime_start:
472 case Intrinsic::lifetime_end:
473 return 0;
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000474 }
475
Bill Wendlingafc10362013-06-19 20:51:24 +0000476 const TargetLoweringBase *TLI = getTLI();
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000477 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(RetTy);
478
479 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
480 // The operation is legal. Assume it costs 1.
481 // If the type is split to multiple registers, assume that thre is some
482 // overhead to this.
483 // TODO: Once we have extract/insert subvector cost we need to use them.
484 if (LT.first > 1)
485 return LT.first * 2;
486 return LT.first * 1;
487 }
488
489 if (!TLI->isOperationExpand(ISD, LT.second)) {
490 // If the operation is custom lowered then assume
491 // thare the code is twice as expensive.
492 return LT.first * 2;
493 }
494
495 // Else, assume that we need to scalarize this intrinsic. For math builtins
496 // this will emit a costly libcall, adding call overhead and spills. Make it
497 // very expensive.
498 if (RetTy->isVectorTy()) {
499 unsigned Num = RetTy->getVectorNumElements();
500 unsigned Cost = TopTTI->getIntrinsicInstrCost(IID, RetTy->getScalarType(),
501 Tys);
502 return 10 * Cost * Num;
503 }
504
505 // This is going to be turned into a library call, make it expensive.
506 return 10;
Chandler Carruth664e3542013-01-07 01:37:14 +0000507}
508
509unsigned BasicTTI::getNumberOfParts(Type *Tp) const {
Bill Wendlingafc10362013-06-19 20:51:24 +0000510 std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Tp);
Chandler Carruth664e3542013-01-07 01:37:14 +0000511 return LT.first;
512}
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000513
Arnold Schwaighofer9da9a432013-07-12 19:16:02 +0000514unsigned BasicTTI::getAddressComputationCost(Type *Ty, bool IsComplex) const {
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000515 return 0;
516}
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000517
518unsigned BasicTTI::getReductionCost(unsigned Opcode, Type *Ty,
519 bool IsPairwise) const {
520 assert(Ty->isVectorTy() && "Expect a vector type");
521 unsigned NumVecElts = Ty->getVectorNumElements();
522 unsigned NumReduxLevels = Log2_32(NumVecElts);
523 unsigned ArithCost = NumReduxLevels *
524 TopTTI->getArithmeticInstrCost(Opcode, Ty);
525 // Assume the pairwise shuffles add a cost.
526 unsigned ShuffleCost =
527 NumReduxLevels * (IsPairwise + 1) *
528 TopTTI->getShuffleCost(SK_ExtractSubvector, Ty, NumVecElts / 2, Ty);
529 return ShuffleCost + ArithCost + getScalarizationOverhead(Ty, false, true);
530}