blob: 04d09f1372a93abf5cc1dac0f9433658aeca0442 [file] [log] [blame]
Chandler Carruthd3e73552013-01-07 03:08:10 +00001//===- llvm/Analysis/TargetTransformInfo.cpp ------------------------------===//
Nadav Rotem5dc203e2012-10-18 23:22:48 +00002//
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
Chandler Carruth664e3542013-01-07 01:37:14 +000010#define DEBUG_TYPE "tti"
Chandler Carruthd3e73552013-01-07 03:08:10 +000011#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000012#include "llvm/IR/CallSite.h"
Chandler Carruth511aa762013-01-21 01:27:39 +000013#include "llvm/IR/DataLayout.h"
Chandler Carruth511aa762013-01-21 01:27:39 +000014#include "llvm/IR/Instruction.h"
Chandler Carruth511aa762013-01-21 01:27:39 +000015#include "llvm/IR/Instructions.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "llvm/IR/IntrinsicInst.h"
17#include "llvm/IR/Operator.h"
Nadav Rotem5dc203e2012-10-18 23:22:48 +000018#include "llvm/Support/ErrorHandling.h"
19
20using namespace llvm;
21
Chandler Carruth539edf42013-01-05 11:43:11 +000022// Setup the analysis group to manage the TargetTransformInfo passes.
23INITIALIZE_ANALYSIS_GROUP(TargetTransformInfo, "Target Information", NoTTI)
Nadav Rotem5dc203e2012-10-18 23:22:48 +000024char TargetTransformInfo::ID = 0;
25
Chandler Carruth539edf42013-01-05 11:43:11 +000026TargetTransformInfo::~TargetTransformInfo() {
27}
28
Chandler Carruth664e3542013-01-07 01:37:14 +000029void TargetTransformInfo::pushTTIStack(Pass *P) {
30 TopTTI = this;
31 PrevTTI = &P->getAnalysis<TargetTransformInfo>();
32
33 // Walk up the chain and update the top TTI pointer.
34 for (TargetTransformInfo *PTTI = PrevTTI; PTTI; PTTI = PTTI->PrevTTI)
35 PTTI->TopTTI = this;
36}
37
Chandler Carruth539edf42013-01-05 11:43:11 +000038void TargetTransformInfo::getAnalysisUsage(AnalysisUsage &AU) const {
39 AU.addRequired<TargetTransformInfo>();
40}
41
Chandler Carruth511aa762013-01-21 01:27:39 +000042unsigned TargetTransformInfo::getOperationCost(unsigned Opcode, Type *Ty,
43 Type *OpTy) const {
44 return PrevTTI->getOperationCost(Opcode, Ty, OpTy);
45}
46
47unsigned TargetTransformInfo::getGEPCost(
48 const Value *Ptr, ArrayRef<const Value *> Operands) const {
49 return PrevTTI->getGEPCost(Ptr, Operands);
50}
51
Chandler Carruth0ba8db42013-01-22 11:26:02 +000052unsigned TargetTransformInfo::getCallCost(FunctionType *FTy,
53 int NumArgs) const {
54 return PrevTTI->getCallCost(FTy, NumArgs);
55}
56
57unsigned TargetTransformInfo::getCallCost(const Function *F,
58 int NumArgs) const {
59 return PrevTTI->getCallCost(F, NumArgs);
60}
61
62unsigned TargetTransformInfo::getCallCost(
63 const Function *F, ArrayRef<const Value *> Arguments) const {
64 return PrevTTI->getCallCost(F, Arguments);
65}
66
67unsigned TargetTransformInfo::getIntrinsicCost(
68 Intrinsic::ID IID, Type *RetTy, ArrayRef<Type *> ParamTys) const {
69 return PrevTTI->getIntrinsicCost(IID, RetTy, ParamTys);
70}
71
72unsigned TargetTransformInfo::getIntrinsicCost(
73 Intrinsic::ID IID, Type *RetTy, ArrayRef<const Value *> Arguments) const {
74 return PrevTTI->getIntrinsicCost(IID, RetTy, Arguments);
75}
76
Chandler Carruth511aa762013-01-21 01:27:39 +000077unsigned TargetTransformInfo::getUserCost(const User *U) const {
78 return PrevTTI->getUserCost(U);
79}
80
Tom Stellard8b1e0212013-07-27 00:01:07 +000081bool TargetTransformInfo::hasBranchDivergence() const {
82 return PrevTTI->hasBranchDivergence();
83}
84
Chandler Carruth0ba8db42013-01-22 11:26:02 +000085bool TargetTransformInfo::isLoweredToCall(const Function *F) const {
86 return PrevTTI->isLoweredToCall(F);
87}
88
Hal Finkel8f2e7002013-09-11 19:25:43 +000089void TargetTransformInfo::getUnrollingPreferences(Loop *L,
90 UnrollingPreferences &UP) const {
91 PrevTTI->getUnrollingPreferences(L, UP);
92}
93
Chandler Carruth539edf42013-01-05 11:43:11 +000094bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const {
95 return PrevTTI->isLegalAddImmediate(Imm);
96}
97
98bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const {
99 return PrevTTI->isLegalICmpImmediate(Imm);
100}
101
102bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
103 int64_t BaseOffset,
104 bool HasBaseReg,
105 int64_t Scale) const {
106 return PrevTTI->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
107 Scale);
108}
109
Quentin Colombetbf490d42013-05-31 21:29:03 +0000110int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
111 int64_t BaseOffset,
112 bool HasBaseReg,
113 int64_t Scale) const {
114 return PrevTTI->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg,
115 Scale);
116}
117
Chandler Carruth539edf42013-01-05 11:43:11 +0000118bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
119 return PrevTTI->isTruncateFree(Ty1, Ty2);
120}
121
122bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
123 return PrevTTI->isTypeLegal(Ty);
124}
125
126unsigned TargetTransformInfo::getJumpBufAlignment() const {
127 return PrevTTI->getJumpBufAlignment();
128}
129
130unsigned TargetTransformInfo::getJumpBufSize() const {
131 return PrevTTI->getJumpBufSize();
132}
133
134bool TargetTransformInfo::shouldBuildLookupTables() const {
135 return PrevTTI->shouldBuildLookupTables();
136}
137
Chandler Carruth50a36cd2013-01-07 03:16:03 +0000138TargetTransformInfo::PopcntSupportKind
139TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
140 return PrevTTI->getPopcntSupport(IntTyWidthInBit);
Chandler Carruth539edf42013-01-05 11:43:11 +0000141}
142
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000143bool TargetTransformInfo::haveFastSqrt(Type *Ty) const {
144 return PrevTTI->haveFastSqrt(Ty);
145}
146
Chandler Carruth539edf42013-01-05 11:43:11 +0000147unsigned TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
148 return PrevTTI->getIntImmCost(Imm, Ty);
149}
150
Juergen Ributzkaf0dff492014-03-21 06:04:45 +0000151unsigned TargetTransformInfo::getIntImmCost(unsigned Opc, unsigned Idx,
152 const APInt &Imm, Type *Ty) const {
153 return PrevTTI->getIntImmCost(Opc, Idx, Imm, Ty);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000154}
155
Juergen Ributzkaf0dff492014-03-21 06:04:45 +0000156unsigned TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
157 const APInt &Imm, Type *Ty) const {
158 return PrevTTI->getIntImmCost(IID, Idx, Imm, Ty);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000159}
160
Chandler Carruth539edf42013-01-05 11:43:11 +0000161unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
162 return PrevTTI->getNumberOfRegisters(Vector);
163}
164
Nadav Rotemb1791a72013-01-09 22:29:00 +0000165unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
166 return PrevTTI->getRegisterBitWidth(Vector);
167}
168
Nadav Rotemb696c362013-01-09 01:15:42 +0000169unsigned TargetTransformInfo::getMaximumUnrollFactor() const {
170 return PrevTTI->getMaximumUnrollFactor();
171}
172
Chandler Carruth539edf42013-01-05 11:43:11 +0000173unsigned TargetTransformInfo::getArithmeticInstrCost(unsigned Opcode,
Arnold Schwaighoferb9773872013-04-04 23:26:21 +0000174 Type *Ty,
175 OperandValueKind Op1Info,
176 OperandValueKind Op2Info) const {
177 return PrevTTI->getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info);
Chandler Carruth539edf42013-01-05 11:43:11 +0000178}
179
180unsigned TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Tp,
181 int Index, Type *SubTp) const {
182 return PrevTTI->getShuffleCost(Kind, Tp, Index, SubTp);
183}
184
185unsigned TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
186 Type *Src) const {
187 return PrevTTI->getCastInstrCost(Opcode, Dst, Src);
188}
189
190unsigned TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
191 return PrevTTI->getCFInstrCost(Opcode);
192}
193
194unsigned TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
195 Type *CondTy) const {
196 return PrevTTI->getCmpSelInstrCost(Opcode, ValTy, CondTy);
197}
198
199unsigned TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
200 unsigned Index) const {
201 return PrevTTI->getVectorInstrCost(Opcode, Val, Index);
202}
203
204unsigned TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
205 unsigned Alignment,
206 unsigned AddressSpace) const {
207 return PrevTTI->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
208 ;
209}
210
211unsigned
212TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID,
213 Type *RetTy,
214 ArrayRef<Type *> Tys) const {
215 return PrevTTI->getIntrinsicInstrCost(ID, RetTy, Tys);
216}
217
218unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
219 return PrevTTI->getNumberOfParts(Tp);
220}
221
Arnold Schwaighofer9da9a432013-07-12 19:16:02 +0000222unsigned TargetTransformInfo::getAddressComputationCost(Type *Tp,
223 bool IsComplex) const {
224 return PrevTTI->getAddressComputationCost(Tp, IsComplex);
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000225}
Chandler Carruth539edf42013-01-05 11:43:11 +0000226
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000227unsigned TargetTransformInfo::getReductionCost(unsigned Opcode, Type *Ty,
228 bool IsPairwise) const {
229 return PrevTTI->getReductionCost(Opcode, Ty, IsPairwise);
230}
231
Chandler Carruth539edf42013-01-05 11:43:11 +0000232namespace {
233
Craig Topper77dfe452014-03-02 08:08:51 +0000234struct NoTTI final : ImmutablePass, TargetTransformInfo {
Chandler Carruth511aa762013-01-21 01:27:39 +0000235 const DataLayout *DL;
236
237 NoTTI() : ImmutablePass(ID), DL(0) {
Chandler Carruth539edf42013-01-05 11:43:11 +0000238 initializeNoTTIPass(*PassRegistry::getPassRegistry());
239 }
240
Craig Topper73156022014-03-02 09:09:27 +0000241 virtual void initializePass() override {
Chandler Carruth664e3542013-01-07 01:37:14 +0000242 // Note that this subclass is special, and must *not* call initializeTTI as
243 // it does not chain.
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000244 TopTTI = this;
Chandler Carruth664e3542013-01-07 01:37:14 +0000245 PrevTTI = 0;
Rafael Espindola93512512014-02-25 17:30:31 +0000246 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
247 DL = DLP ? &DLP->getDataLayout() : 0;
Chandler Carruth664e3542013-01-07 01:37:14 +0000248 }
249
Craig Topper73156022014-03-02 09:09:27 +0000250 virtual void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruth539edf42013-01-05 11:43:11 +0000251 // Note that this subclass is special, and must *not* call
252 // TTI::getAnalysisUsage as it breaks the recursion.
253 }
254
255 /// Pass identification.
256 static char ID;
257
258 /// Provide necessary pointer adjustments for the two base classes.
Craig Topper73156022014-03-02 09:09:27 +0000259 virtual void *getAdjustedAnalysisPointer(const void *ID) override {
Chandler Carruth539edf42013-01-05 11:43:11 +0000260 if (ID == &TargetTransformInfo::ID)
261 return (TargetTransformInfo*)this;
262 return this;
263 }
264
Juergen Ributzka3e752e72014-01-24 18:22:59 +0000265 unsigned getOperationCost(unsigned Opcode, Type *Ty,
Craig Topper73156022014-03-02 09:09:27 +0000266 Type *OpTy) const override {
Chandler Carruth511aa762013-01-21 01:27:39 +0000267 switch (Opcode) {
268 default:
269 // By default, just classify everything as 'basic'.
270 return TCC_Basic;
271
272 case Instruction::GetElementPtr:
273 llvm_unreachable("Use getGEPCost for GEP operations!");
274
275 case Instruction::BitCast:
276 assert(OpTy && "Cast instructions must provide the operand type");
277 if (Ty == OpTy || (Ty->isPointerTy() && OpTy->isPointerTy()))
278 // Identity and pointer-to-pointer casts are free.
279 return TCC_Free;
280
281 // Otherwise, the default basic cost is used.
282 return TCC_Basic;
283
Matt Arsenault54c3cbc2013-08-28 22:41:57 +0000284 case Instruction::IntToPtr: {
285 if (!DL)
286 return TCC_Basic;
287
Chandler Carruth511aa762013-01-21 01:27:39 +0000288 // An inttoptr cast is free so long as the input is a legal integer type
289 // which doesn't contain values outside the range of a pointer.
Matt Arsenault54c3cbc2013-08-28 22:41:57 +0000290 unsigned OpSize = OpTy->getScalarSizeInBits();
291 if (DL->isLegalInteger(OpSize) &&
292 OpSize <= DL->getPointerTypeSizeInBits(Ty))
Chandler Carruth511aa762013-01-21 01:27:39 +0000293 return TCC_Free;
294
295 // Otherwise it's not a no-op.
296 return TCC_Basic;
Matt Arsenault54c3cbc2013-08-28 22:41:57 +0000297 }
298 case Instruction::PtrToInt: {
299 if (!DL)
300 return TCC_Basic;
Chandler Carruth511aa762013-01-21 01:27:39 +0000301
Chandler Carruth511aa762013-01-21 01:27:39 +0000302 // A ptrtoint cast is free so long as the result is large enough to store
303 // the pointer, and a legal integer type.
Matt Arsenault54c3cbc2013-08-28 22:41:57 +0000304 unsigned DestSize = Ty->getScalarSizeInBits();
305 if (DL->isLegalInteger(DestSize) &&
306 DestSize >= DL->getPointerTypeSizeInBits(OpTy))
Chandler Carruth511aa762013-01-21 01:27:39 +0000307 return TCC_Free;
308
309 // Otherwise it's not a no-op.
310 return TCC_Basic;
Matt Arsenault54c3cbc2013-08-28 22:41:57 +0000311 }
Chandler Carruth511aa762013-01-21 01:27:39 +0000312 case Instruction::Trunc:
313 // trunc to a native type is free (assuming the target has compare and
314 // shift-right of the same width).
315 if (DL && DL->isLegalInteger(DL->getTypeSizeInBits(Ty)))
316 return TCC_Free;
317
318 return TCC_Basic;
319 }
320 }
321
322 unsigned getGEPCost(const Value *Ptr,
Craig Topper73156022014-03-02 09:09:27 +0000323 ArrayRef<const Value *> Operands) const override {
Chandler Carruth511aa762013-01-21 01:27:39 +0000324 // In the basic model, we just assume that all-constant GEPs will be folded
325 // into their uses via addressing modes.
326 for (unsigned Idx = 0, Size = Operands.size(); Idx != Size; ++Idx)
327 if (!isa<Constant>(Operands[Idx]))
328 return TCC_Basic;
329
330 return TCC_Free;
331 }
332
Craig Topper73156022014-03-02 09:09:27 +0000333 unsigned getCallCost(FunctionType *FTy, int NumArgs = -1) const override
Juergen Ributzka3e752e72014-01-24 18:22:59 +0000334 {
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000335 assert(FTy && "FunctionType must be provided to this routine.");
336
337 // The target-independent implementation just measures the size of the
338 // function by approximating that each argument will take on average one
339 // instruction to prepare.
340
341 if (NumArgs < 0)
342 // Set the argument number to the number of explicit arguments in the
343 // function.
344 NumArgs = FTy->getNumParams();
345
346 return TCC_Basic * (NumArgs + 1);
347 }
348
Craig Topper73156022014-03-02 09:09:27 +0000349 unsigned getCallCost(const Function *F, int NumArgs = -1) const override
Juergen Ributzka3e752e72014-01-24 18:22:59 +0000350 {
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000351 assert(F && "A concrete function must be provided to this routine.");
352
353 if (NumArgs < 0)
354 // Set the argument number to the number of explicit arguments in the
355 // function.
356 NumArgs = F->arg_size();
357
358 if (Intrinsic::ID IID = (Intrinsic::ID)F->getIntrinsicID()) {
359 FunctionType *FTy = F->getFunctionType();
360 SmallVector<Type *, 8> ParamTys(FTy->param_begin(), FTy->param_end());
361 return TopTTI->getIntrinsicCost(IID, FTy->getReturnType(), ParamTys);
362 }
363
364 if (!TopTTI->isLoweredToCall(F))
365 return TCC_Basic; // Give a basic cost if it will be lowered directly.
366
367 return TopTTI->getCallCost(F->getFunctionType(), NumArgs);
368 }
369
370 unsigned getCallCost(const Function *F,
Craig Topper73156022014-03-02 09:09:27 +0000371 ArrayRef<const Value *> Arguments) const override {
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000372 // Simply delegate to generic handling of the call.
373 // FIXME: We should use instsimplify or something else to catch calls which
374 // will constant fold with these arguments.
375 return TopTTI->getCallCost(F, Arguments.size());
376 }
377
378 unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
Craig Topper73156022014-03-02 09:09:27 +0000379 ArrayRef<Type *> ParamTys) const override {
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000380 switch (IID) {
381 default:
382 // Intrinsics rarely (if ever) have normal argument setup constraints.
383 // Model them as having a basic instruction cost.
384 // FIXME: This is wrong for libc intrinsics.
385 return TCC_Basic;
386
387 case Intrinsic::dbg_declare:
388 case Intrinsic::dbg_value:
389 case Intrinsic::invariant_start:
390 case Intrinsic::invariant_end:
391 case Intrinsic::lifetime_start:
392 case Intrinsic::lifetime_end:
393 case Intrinsic::objectsize:
394 case Intrinsic::ptr_annotation:
395 case Intrinsic::var_annotation:
396 // These intrinsics don't actually represent code after lowering.
397 return TCC_Free;
398 }
399 }
400
Juergen Ributzka3e752e72014-01-24 18:22:59 +0000401 unsigned
402 getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
Craig Topper73156022014-03-02 09:09:27 +0000403 ArrayRef<const Value *> Arguments) const override {
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000404 // Delegate to the generic intrinsic handling code. This mostly provides an
405 // opportunity for targets to (for example) special case the cost of
406 // certain intrinsics based on constants used as arguments.
407 SmallVector<Type *, 8> ParamTys;
408 ParamTys.reserve(Arguments.size());
409 for (unsigned Idx = 0, Size = Arguments.size(); Idx != Size; ++Idx)
410 ParamTys.push_back(Arguments[Idx]->getType());
411 return TopTTI->getIntrinsicCost(IID, RetTy, ParamTys);
412 }
413
Craig Topper73156022014-03-02 09:09:27 +0000414 unsigned getUserCost(const User *U) const override {
Chandler Carruthbb9caa92013-01-21 13:04:33 +0000415 if (isa<PHINode>(U))
416 return TCC_Free; // Model all PHI nodes as free.
417
Hal Finkelb4e001c2014-04-01 18:50:06 +0000418 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
419 SmallVector<const Value *, 4> Indices(GEP->idx_begin(), GEP->idx_end());
420 return TopTTI->getGEPCost(GEP->getPointerOperand(), Indices);
421 }
Chandler Carruth511aa762013-01-21 01:27:39 +0000422
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000423 if (ImmutableCallSite CS = U) {
424 const Function *F = CS.getCalledFunction();
425 if (!F) {
426 // Just use the called value type.
427 Type *FTy = CS.getCalledValue()->getType()->getPointerElementType();
428 return TopTTI->getCallCost(cast<FunctionType>(FTy), CS.arg_size());
Chandler Carruth511aa762013-01-21 01:27:39 +0000429 }
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000430
Benjamin Kramer3c29c072014-02-10 14:17:42 +0000431 SmallVector<const Value *, 8> Arguments(CS.arg_begin(), CS.arg_end());
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000432 return TopTTI->getCallCost(F, Arguments);
Chandler Carruth511aa762013-01-21 01:27:39 +0000433 }
434
435 if (const CastInst *CI = dyn_cast<CastInst>(U)) {
436 // Result of a cmp instruction is often extended (to be used by other
437 // cmp instructions, logical or return instructions). These are usually
438 // nop on most sane targets.
439 if (isa<CmpInst>(CI->getOperand(0)))
440 return TCC_Free;
441 }
442
443 // Otherwise delegate to the fully generic implementations.
444 return getOperationCost(Operator::getOpcode(U), U->getType(),
445 U->getNumOperands() == 1 ?
446 U->getOperand(0)->getType() : 0);
447 }
Chandler Carruth539edf42013-01-05 11:43:11 +0000448
Craig Topper73156022014-03-02 09:09:27 +0000449 bool hasBranchDivergence() const override { return false; }
Tom Stellard8b1e0212013-07-27 00:01:07 +0000450
Craig Topper73156022014-03-02 09:09:27 +0000451 bool isLoweredToCall(const Function *F) const override {
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000452 // FIXME: These should almost certainly not be handled here, and instead
453 // handled with the help of TLI or the target itself. This was largely
454 // ported from existing analysis heuristics here so that such refactorings
455 // can take place in the future.
456
457 if (F->isIntrinsic())
458 return false;
459
460 if (F->hasLocalLinkage() || !F->hasName())
461 return true;
462
463 StringRef Name = F->getName();
464
465 // These will all likely lower to a single selection DAG node.
466 if (Name == "copysign" || Name == "copysignf" || Name == "copysignl" ||
467 Name == "fabs" || Name == "fabsf" || Name == "fabsl" || Name == "sin" ||
468 Name == "sinf" || Name == "sinl" || Name == "cos" || Name == "cosf" ||
469 Name == "cosl" || Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl")
470 return false;
471
472 // These are all likely to be optimized into something smaller.
473 if (Name == "pow" || Name == "powf" || Name == "powl" || Name == "exp2" ||
474 Name == "exp2l" || Name == "exp2f" || Name == "floor" || Name ==
475 "floorf" || Name == "ceil" || Name == "round" || Name == "ffs" ||
476 Name == "ffsl" || Name == "abs" || Name == "labs" || Name == "llabs")
477 return false;
478
479 return true;
480 }
481
Craig Topper73156022014-03-02 09:09:27 +0000482 void getUnrollingPreferences(Loop *, UnrollingPreferences &) const override {
483 }
Hal Finkel8f2e7002013-09-11 19:25:43 +0000484
Craig Topper73156022014-03-02 09:09:27 +0000485 bool isLegalAddImmediate(int64_t Imm) const override {
Chandler Carruth664e3542013-01-07 01:37:14 +0000486 return false;
Chandler Carruth539edf42013-01-05 11:43:11 +0000487 }
488
Craig Topper73156022014-03-02 09:09:27 +0000489 bool isLegalICmpImmediate(int64_t Imm) const override {
Chandler Carruth664e3542013-01-07 01:37:14 +0000490 return false;
Chandler Carruth539edf42013-01-05 11:43:11 +0000491 }
492
493 bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
Craig Topper73156022014-03-02 09:09:27 +0000494 bool HasBaseReg, int64_t Scale) const override
Juergen Ributzka3e752e72014-01-24 18:22:59 +0000495 {
Chandler Carruth26c59fa2013-01-07 14:41:08 +0000496 // Guess that reg+reg addressing is allowed. This heuristic is taken from
497 // the implementation of LSR.
498 return !BaseGV && BaseOffset == 0 && Scale <= 1;
Chandler Carruth539edf42013-01-05 11:43:11 +0000499 }
500
Quentin Colombetbf490d42013-05-31 21:29:03 +0000501 int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
Craig Topper73156022014-03-02 09:09:27 +0000502 bool HasBaseReg, int64_t Scale) const override {
Quentin Colombetbf490d42013-05-31 21:29:03 +0000503 // Guess that all legal addressing mode are free.
504 if(isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg, Scale))
505 return 0;
506 return -1;
507 }
508
Craig Topper73156022014-03-02 09:09:27 +0000509 bool isTruncateFree(Type *Ty1, Type *Ty2) const override {
Chandler Carruth664e3542013-01-07 01:37:14 +0000510 return false;
Chandler Carruth539edf42013-01-05 11:43:11 +0000511 }
512
Craig Topper73156022014-03-02 09:09:27 +0000513 bool isTypeLegal(Type *Ty) const override {
Chandler Carruth664e3542013-01-07 01:37:14 +0000514 return false;
Chandler Carruth539edf42013-01-05 11:43:11 +0000515 }
516
Craig Topper73156022014-03-02 09:09:27 +0000517 unsigned getJumpBufAlignment() const override {
Chandler Carruth664e3542013-01-07 01:37:14 +0000518 return 0;
Chandler Carruth539edf42013-01-05 11:43:11 +0000519 }
520
Craig Topper73156022014-03-02 09:09:27 +0000521 unsigned getJumpBufSize() const override {
Chandler Carruth664e3542013-01-07 01:37:14 +0000522 return 0;
Chandler Carruth539edf42013-01-05 11:43:11 +0000523 }
524
Craig Topper73156022014-03-02 09:09:27 +0000525 bool shouldBuildLookupTables() const override {
Chandler Carruth664e3542013-01-07 01:37:14 +0000526 return true;
Chandler Carruth539edf42013-01-05 11:43:11 +0000527 }
528
Juergen Ributzka3e752e72014-01-24 18:22:59 +0000529 PopcntSupportKind
Craig Topper73156022014-03-02 09:09:27 +0000530 getPopcntSupport(unsigned IntTyWidthInBit) const override {
Chandler Carruth50a36cd2013-01-07 03:16:03 +0000531 return PSK_Software;
Chandler Carruth539edf42013-01-05 11:43:11 +0000532 }
533
Craig Topper73156022014-03-02 09:09:27 +0000534 bool haveFastSqrt(Type *Ty) const override {
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000535 return false;
536 }
537
Craig Topper73156022014-03-02 09:09:27 +0000538 unsigned getIntImmCost(const APInt &Imm, Type *Ty) const override {
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000539 return TCC_Basic;
540 }
541
Juergen Ributzkaf0dff492014-03-21 06:04:45 +0000542 unsigned getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
Craig Topper73156022014-03-02 09:09:27 +0000543 Type *Ty) const override {
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000544 return TCC_Free;
545 }
546
Juergen Ributzkaf0dff492014-03-21 06:04:45 +0000547 unsigned getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
Craig Topper73156022014-03-02 09:09:27 +0000548 Type *Ty) const override {
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000549 return TCC_Free;
Chandler Carruth539edf42013-01-05 11:43:11 +0000550 }
551
Craig Topper73156022014-03-02 09:09:27 +0000552 unsigned getNumberOfRegisters(bool Vector) const override {
Chandler Carruth664e3542013-01-07 01:37:14 +0000553 return 8;
Chandler Carruth539edf42013-01-05 11:43:11 +0000554 }
555
Craig Topper73156022014-03-02 09:09:27 +0000556 unsigned getRegisterBitWidth(bool Vector) const override {
Nadav Rotemb1791a72013-01-09 22:29:00 +0000557 return 32;
558 }
559
Craig Topper73156022014-03-02 09:09:27 +0000560 unsigned getMaximumUnrollFactor() const override {
Nadav Rotemb696c362013-01-09 01:15:42 +0000561 return 1;
562 }
563
Arnold Schwaighoferb9773872013-04-04 23:26:21 +0000564 unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty, OperandValueKind,
Craig Topper73156022014-03-02 09:09:27 +0000565 OperandValueKind) const override {
Chandler Carruth664e3542013-01-07 01:37:14 +0000566 return 1;
Chandler Carruth539edf42013-01-05 11:43:11 +0000567 }
568
Juergen Ributzka3e752e72014-01-24 18:22:59 +0000569 unsigned getShuffleCost(ShuffleKind Kind, Type *Ty,
Craig Topper73156022014-03-02 09:09:27 +0000570 int Index = 0, Type *SubTp = 0) const override {
Chandler Carruth664e3542013-01-07 01:37:14 +0000571 return 1;
Chandler Carruth539edf42013-01-05 11:43:11 +0000572 }
573
574 unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
Craig Topper73156022014-03-02 09:09:27 +0000575 Type *Src) const override {
Chandler Carruth664e3542013-01-07 01:37:14 +0000576 return 1;
Chandler Carruth539edf42013-01-05 11:43:11 +0000577 }
578
Craig Topper73156022014-03-02 09:09:27 +0000579 unsigned getCFInstrCost(unsigned Opcode) const override {
Chandler Carruth664e3542013-01-07 01:37:14 +0000580 return 1;
Chandler Carruth539edf42013-01-05 11:43:11 +0000581 }
582
583 unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
Craig Topper73156022014-03-02 09:09:27 +0000584 Type *CondTy = 0) const override {
Chandler Carruth664e3542013-01-07 01:37:14 +0000585 return 1;
Chandler Carruth539edf42013-01-05 11:43:11 +0000586 }
587
588 unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
Craig Topper73156022014-03-02 09:09:27 +0000589 unsigned Index = -1) const override {
Chandler Carruth664e3542013-01-07 01:37:14 +0000590 return 1;
Chandler Carruth539edf42013-01-05 11:43:11 +0000591 }
592
Craig Topper73156022014-03-02 09:09:27 +0000593 unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
594 unsigned AddressSpace) const override {
Chandler Carruth664e3542013-01-07 01:37:14 +0000595 return 1;
Chandler Carruth539edf42013-01-05 11:43:11 +0000596 }
597
Craig Topper73156022014-03-02 09:09:27 +0000598 unsigned getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
599 ArrayRef<Type*> Tys) const override {
Chandler Carruth664e3542013-01-07 01:37:14 +0000600 return 1;
Chandler Carruth539edf42013-01-05 11:43:11 +0000601 }
602
Craig Topper73156022014-03-02 09:09:27 +0000603 unsigned getNumberOfParts(Type *Tp) const override {
Chandler Carruth664e3542013-01-07 01:37:14 +0000604 return 0;
Chandler Carruth539edf42013-01-05 11:43:11 +0000605 }
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000606
Craig Topper73156022014-03-02 09:09:27 +0000607 unsigned getAddressComputationCost(Type *Tp, bool) const override {
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000608 return 0;
609 }
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000610
Craig Topper73156022014-03-02 09:09:27 +0000611 unsigned getReductionCost(unsigned, Type *, bool) const override {
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000612 return 1;
613 }
Chandler Carruth539edf42013-01-05 11:43:11 +0000614};
615
616} // end anonymous namespace
617
Chandler Carruth664e3542013-01-07 01:37:14 +0000618INITIALIZE_AG_PASS(NoTTI, TargetTransformInfo, "notti",
Chandler Carruth539edf42013-01-05 11:43:11 +0000619 "No target information", true, true, true)
620char NoTTI::ID = 0;
621
Chandler Carruth664e3542013-01-07 01:37:14 +0000622ImmutablePass *llvm::createNoTargetTransformInfoPass() {
623 return new NoTTI();
Chandler Carruth539edf42013-01-05 11:43:11 +0000624}