blob: 4ad48e351a4a9981b22f8d840cad6d393aa19ea7 [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 Carruthd3e73552013-01-07 03:08:10 +000010#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruth705b1852015-01-31 03:43:40 +000011#include "llvm/Analysis/TargetTransformInfoImpl.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"
Chandler Carruthe0385522015-02-01 10:11:22 +000017#include "llvm/IR/Module.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000018#include "llvm/IR/Operator.h"
Guozhi Wei62d64142017-09-08 22:29:17 +000019#include "llvm/IR/PatternMatch.h"
Sean Fertile9cd1cdf2017-07-07 02:00:06 +000020#include "llvm/Support/CommandLine.h"
Nadav Rotem5dc203e2012-10-18 23:22:48 +000021#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer82de7d32016-05-27 14:27:24 +000022#include <utility>
Nadav Rotem5dc203e2012-10-18 23:22:48 +000023
24using namespace llvm;
Guozhi Wei62d64142017-09-08 22:29:17 +000025using namespace PatternMatch;
Nadav Rotem5dc203e2012-10-18 23:22:48 +000026
Chandler Carruthf1221bd2014-04-22 02:48:03 +000027#define DEBUG_TYPE "tti"
28
Guozhi Wei62d64142017-09-08 22:29:17 +000029static cl::opt<bool> EnableReduxCost("costmodel-reduxcost", cl::init(false),
30 cl::Hidden,
31 cl::desc("Recognize reduction patterns."));
32
Chandler Carruth93dcdc42015-01-31 11:17:59 +000033namespace {
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000034/// No-op implementation of the TTI interface using the utility base
Chandler Carruth93dcdc42015-01-31 11:17:59 +000035/// classes.
36///
37/// This is used when no target specific information is available.
38struct NoTTIImpl : TargetTransformInfoImplCRTPBase<NoTTIImpl> {
Mehdi Amini5010ebf2015-07-09 02:08:42 +000039 explicit NoTTIImpl(const DataLayout &DL)
Chandler Carruth93dcdc42015-01-31 11:17:59 +000040 : TargetTransformInfoImplCRTPBase<NoTTIImpl>(DL) {}
41};
42}
43
Mehdi Amini5010ebf2015-07-09 02:08:42 +000044TargetTransformInfo::TargetTransformInfo(const DataLayout &DL)
Chandler Carruth93dcdc42015-01-31 11:17:59 +000045 : TTIImpl(new Model<NoTTIImpl>(NoTTIImpl(DL))) {}
46
Chandler Carruth705b1852015-01-31 03:43:40 +000047TargetTransformInfo::~TargetTransformInfo() {}
Nadav Rotem5dc203e2012-10-18 23:22:48 +000048
Chandler Carruth705b1852015-01-31 03:43:40 +000049TargetTransformInfo::TargetTransformInfo(TargetTransformInfo &&Arg)
50 : TTIImpl(std::move(Arg.TTIImpl)) {}
Chandler Carruth539edf42013-01-05 11:43:11 +000051
Chandler Carruth705b1852015-01-31 03:43:40 +000052TargetTransformInfo &TargetTransformInfo::operator=(TargetTransformInfo &&RHS) {
53 TTIImpl = std::move(RHS.TTIImpl);
54 return *this;
Chandler Carruth539edf42013-01-05 11:43:11 +000055}
56
Chandler Carruth93205eb2015-08-05 18:08:10 +000057int TargetTransformInfo::getOperationCost(unsigned Opcode, Type *Ty,
58 Type *OpTy) const {
59 int Cost = TTIImpl->getOperationCost(Opcode, Ty, OpTy);
60 assert(Cost >= 0 && "TTI should not produce negative costs!");
61 return Cost;
Chandler Carruth511aa762013-01-21 01:27:39 +000062}
63
Chandler Carruth93205eb2015-08-05 18:08:10 +000064int TargetTransformInfo::getCallCost(FunctionType *FTy, int NumArgs) const {
65 int Cost = TTIImpl->getCallCost(FTy, NumArgs);
66 assert(Cost >= 0 && "TTI should not produce negative costs!");
67 return Cost;
Chandler Carruth0ba8db42013-01-22 11:26:02 +000068}
69
Chandler Carruth93205eb2015-08-05 18:08:10 +000070int TargetTransformInfo::getCallCost(const Function *F,
71 ArrayRef<const Value *> Arguments) const {
72 int Cost = TTIImpl->getCallCost(F, Arguments);
73 assert(Cost >= 0 && "TTI should not produce negative costs!");
74 return Cost;
Chandler Carruth0ba8db42013-01-22 11:26:02 +000075}
76
Justin Lebar8650a4d2016-04-15 01:38:48 +000077unsigned TargetTransformInfo::getInliningThresholdMultiplier() const {
78 return TTIImpl->getInliningThresholdMultiplier();
79}
80
Jingyue Wu15f3e822016-07-08 21:48:05 +000081int TargetTransformInfo::getGEPCost(Type *PointeeType, const Value *Ptr,
82 ArrayRef<const Value *> Operands) const {
83 return TTIImpl->getGEPCost(PointeeType, Ptr, Operands);
84}
85
Haicheng Wuabdef9e2017-07-15 02:12:16 +000086int TargetTransformInfo::getExtCost(const Instruction *I,
87 const Value *Src) const {
88 return TTIImpl->getExtCost(I, Src);
89}
90
Chandler Carruth93205eb2015-08-05 18:08:10 +000091int TargetTransformInfo::getIntrinsicCost(
92 Intrinsic::ID IID, Type *RetTy, ArrayRef<const Value *> Arguments) const {
93 int Cost = TTIImpl->getIntrinsicCost(IID, RetTy, Arguments);
94 assert(Cost >= 0 && "TTI should not produce negative costs!");
95 return Cost;
Chandler Carruth0ba8db42013-01-22 11:26:02 +000096}
97
Jun Bum Lim919f9e82017-04-28 16:04:03 +000098unsigned
99TargetTransformInfo::getEstimatedNumberOfCaseClusters(const SwitchInst &SI,
100 unsigned &JTSize) const {
101 return TTIImpl->getEstimatedNumberOfCaseClusters(SI, JTSize);
102}
103
Evgeny Astigeevich70ed78e2017-06-29 13:42:12 +0000104int TargetTransformInfo::getUserCost(const User *U,
105 ArrayRef<const Value *> Operands) const {
106 int Cost = TTIImpl->getUserCost(U, Operands);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000107 assert(Cost >= 0 && "TTI should not produce negative costs!");
108 return Cost;
Chandler Carruth511aa762013-01-21 01:27:39 +0000109}
110
Tom Stellard8b1e0212013-07-27 00:01:07 +0000111bool TargetTransformInfo::hasBranchDivergence() const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000112 return TTIImpl->hasBranchDivergence();
Tom Stellard8b1e0212013-07-27 00:01:07 +0000113}
114
Jingyue Wu5da831c2015-04-10 05:03:50 +0000115bool TargetTransformInfo::isSourceOfDivergence(const Value *V) const {
116 return TTIImpl->isSourceOfDivergence(V);
117}
118
Alexander Timofeev0f9c84c2017-06-15 19:33:10 +0000119bool llvm::TargetTransformInfo::isAlwaysUniform(const Value *V) const {
120 return TTIImpl->isAlwaysUniform(V);
121}
122
Matt Arsenault42b64782017-01-30 23:02:12 +0000123unsigned TargetTransformInfo::getFlatAddressSpace() const {
124 return TTIImpl->getFlatAddressSpace();
125}
126
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000127bool TargetTransformInfo::isLoweredToCall(const Function *F) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000128 return TTIImpl->isLoweredToCall(F);
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000129}
130
Chandler Carruth705b1852015-01-31 03:43:40 +0000131void TargetTransformInfo::getUnrollingPreferences(
Geoff Berry66d9bdb2017-06-28 15:53:17 +0000132 Loop *L, ScalarEvolution &SE, UnrollingPreferences &UP) const {
133 return TTIImpl->getUnrollingPreferences(L, SE, UP);
Hal Finkel8f2e7002013-09-11 19:25:43 +0000134}
135
Chandler Carruth539edf42013-01-05 11:43:11 +0000136bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000137 return TTIImpl->isLegalAddImmediate(Imm);
Chandler Carruth539edf42013-01-05 11:43:11 +0000138}
139
140bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000141 return TTIImpl->isLegalICmpImmediate(Imm);
Chandler Carruth539edf42013-01-05 11:43:11 +0000142}
143
144bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
145 int64_t BaseOffset,
146 bool HasBaseReg,
Matt Arsenaulte83379e2015-06-07 20:12:03 +0000147 int64_t Scale,
Jonas Paulsson024e3192017-07-21 11:59:37 +0000148 unsigned AddrSpace,
149 Instruction *I) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000150 return TTIImpl->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
Jonas Paulsson024e3192017-07-21 11:59:37 +0000151 Scale, AddrSpace, I);
Chandler Carruth539edf42013-01-05 11:43:11 +0000152}
153
Evgeny Stupachenkof2b3b462017-06-05 23:37:00 +0000154bool TargetTransformInfo::isLSRCostLess(LSRCost &C1, LSRCost &C2) const {
155 return TTIImpl->isLSRCostLess(C1, C2);
156}
157
Sanjay Pateld7c702b2018-02-05 23:43:05 +0000158bool TargetTransformInfo::canMacroFuseCmp() const {
159 return TTIImpl->canMacroFuseCmp();
160}
161
Krzysztof Parzyszek0b377e02018-03-26 13:10:09 +0000162bool TargetTransformInfo::shouldFavorPostInc() const {
163 return TTIImpl->shouldFavorPostInc();
164}
165
Elena Demikhovsky20662e32015-10-19 07:43:38 +0000166bool TargetTransformInfo::isLegalMaskedStore(Type *DataType) const {
167 return TTIImpl->isLegalMaskedStore(DataType);
Chandler Carruth705b1852015-01-31 03:43:40 +0000168}
169
Elena Demikhovsky20662e32015-10-19 07:43:38 +0000170bool TargetTransformInfo::isLegalMaskedLoad(Type *DataType) const {
171 return TTIImpl->isLegalMaskedLoad(DataType);
Chandler Carruth705b1852015-01-31 03:43:40 +0000172}
173
Elena Demikhovsky09285852015-10-25 15:37:55 +0000174bool TargetTransformInfo::isLegalMaskedGather(Type *DataType) const {
175 return TTIImpl->isLegalMaskedGather(DataType);
176}
177
178bool TargetTransformInfo::isLegalMaskedScatter(Type *DataType) const {
Mohammed Agabariacef53dc2017-07-27 10:28:16 +0000179 return TTIImpl->isLegalMaskedScatter(DataType);
Elena Demikhovsky09285852015-10-25 15:37:55 +0000180}
181
Sanjay Patel6fd43912017-09-09 13:38:18 +0000182bool TargetTransformInfo::hasDivRemOp(Type *DataType, bool IsSigned) const {
183 return TTIImpl->hasDivRemOp(DataType, IsSigned);
184}
185
Artem Belevichcb8f6322017-10-24 20:31:44 +0000186bool TargetTransformInfo::hasVolatileVariant(Instruction *I,
187 unsigned AddrSpace) const {
188 return TTIImpl->hasVolatileVariant(I, AddrSpace);
189}
190
Jonas Paulsson8624b7e2017-05-24 13:42:56 +0000191bool TargetTransformInfo::prefersVectorizedAddressing() const {
192 return TTIImpl->prefersVectorizedAddressing();
193}
194
Quentin Colombetbf490d42013-05-31 21:29:03 +0000195int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
196 int64_t BaseOffset,
197 bool HasBaseReg,
Matt Arsenaulte83379e2015-06-07 20:12:03 +0000198 int64_t Scale,
199 unsigned AddrSpace) const {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000200 int Cost = TTIImpl->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg,
201 Scale, AddrSpace);
202 assert(Cost >= 0 && "TTI should not produce negative costs!");
203 return Cost;
Quentin Colombetbf490d42013-05-31 21:29:03 +0000204}
205
Jonas Paulsson024e3192017-07-21 11:59:37 +0000206bool TargetTransformInfo::LSRWithInstrQueries() const {
207 return TTIImpl->LSRWithInstrQueries();
208}
209
Chandler Carruth539edf42013-01-05 11:43:11 +0000210bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000211 return TTIImpl->isTruncateFree(Ty1, Ty2);
Chandler Carruth539edf42013-01-05 11:43:11 +0000212}
213
Chad Rosier54390052015-02-23 19:15:16 +0000214bool TargetTransformInfo::isProfitableToHoist(Instruction *I) const {
215 return TTIImpl->isProfitableToHoist(I);
216}
217
David Blaikie8ad9a972018-03-28 22:28:50 +0000218bool TargetTransformInfo::useAA() const { return TTIImpl->useAA(); }
219
Chandler Carruth539edf42013-01-05 11:43:11 +0000220bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000221 return TTIImpl->isTypeLegal(Ty);
Chandler Carruth539edf42013-01-05 11:43:11 +0000222}
223
224unsigned TargetTransformInfo::getJumpBufAlignment() const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000225 return TTIImpl->getJumpBufAlignment();
Chandler Carruth539edf42013-01-05 11:43:11 +0000226}
227
228unsigned TargetTransformInfo::getJumpBufSize() const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000229 return TTIImpl->getJumpBufSize();
Chandler Carruth539edf42013-01-05 11:43:11 +0000230}
231
232bool TargetTransformInfo::shouldBuildLookupTables() const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000233 return TTIImpl->shouldBuildLookupTables();
Chandler Carruth539edf42013-01-05 11:43:11 +0000234}
Oliver Stannard4df1cc02016-10-07 08:48:24 +0000235bool TargetTransformInfo::shouldBuildLookupTablesForConstant(Constant *C) const {
236 return TTIImpl->shouldBuildLookupTablesForConstant(C);
237}
Chandler Carruth539edf42013-01-05 11:43:11 +0000238
Zaara Syeda1f59ae32018-01-30 16:17:22 +0000239bool TargetTransformInfo::useColdCCForColdCall(Function &F) const {
240 return TTIImpl->useColdCCForColdCall(F);
241}
242
Jonas Paulsson8e2f9482017-01-26 07:03:25 +0000243unsigned TargetTransformInfo::
244getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const {
245 return TTIImpl->getScalarizationOverhead(Ty, Insert, Extract);
246}
247
248unsigned TargetTransformInfo::
249getOperandsScalarizationOverhead(ArrayRef<const Value *> Args,
250 unsigned VF) const {
251 return TTIImpl->getOperandsScalarizationOverhead(Args, VF);
252}
253
Jonas Paulssonda74ed42017-04-12 12:41:37 +0000254bool TargetTransformInfo::supportsEfficientVectorElementLoadStore() const {
255 return TTIImpl->supportsEfficientVectorElementLoadStore();
256}
257
Olivier Sallenave049d8032015-03-06 23:12:04 +0000258bool TargetTransformInfo::enableAggressiveInterleaving(bool LoopHasReductions) const {
259 return TTIImpl->enableAggressiveInterleaving(LoopHasReductions);
260}
261
Clement Courbetb2c3eb82017-10-30 14:19:33 +0000262const TargetTransformInfo::MemCmpExpansionOptions *
263TargetTransformInfo::enableMemCmpExpansion(bool IsZeroCmp) const {
264 return TTIImpl->enableMemCmpExpansion(IsZeroCmp);
Zaara Syeda3a7578c2017-05-31 17:12:38 +0000265}
266
Silviu Baranga61bdc512015-08-10 14:50:54 +0000267bool TargetTransformInfo::enableInterleavedAccessVectorization() const {
268 return TTIImpl->enableInterleavedAccessVectorization();
269}
270
Renato Golin5cb666a2016-04-14 20:42:18 +0000271bool TargetTransformInfo::isFPVectorizationPotentiallyUnsafe() const {
272 return TTIImpl->isFPVectorizationPotentiallyUnsafe();
273}
274
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000275bool TargetTransformInfo::allowsMisalignedMemoryAccesses(LLVMContext &Context,
276 unsigned BitWidth,
Alina Sbirlea327955e2016-07-11 20:46:17 +0000277 unsigned AddressSpace,
278 unsigned Alignment,
279 bool *Fast) const {
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000280 return TTIImpl->allowsMisalignedMemoryAccesses(Context, BitWidth, AddressSpace,
Alina Sbirlea327955e2016-07-11 20:46:17 +0000281 Alignment, Fast);
282}
283
Chandler Carruth50a36cd2013-01-07 03:16:03 +0000284TargetTransformInfo::PopcntSupportKind
285TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000286 return TTIImpl->getPopcntSupport(IntTyWidthInBit);
Chandler Carruth539edf42013-01-05 11:43:11 +0000287}
288
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000289bool TargetTransformInfo::haveFastSqrt(Type *Ty) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000290 return TTIImpl->haveFastSqrt(Ty);
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000291}
292
Sanjay Patel0de1a4b2017-11-27 21:15:43 +0000293bool TargetTransformInfo::isFCmpOrdCheaperThanFCmpZero(Type *Ty) const {
294 return TTIImpl->isFCmpOrdCheaperThanFCmpZero(Ty);
295}
296
Chandler Carruth93205eb2015-08-05 18:08:10 +0000297int TargetTransformInfo::getFPOpCost(Type *Ty) const {
298 int Cost = TTIImpl->getFPOpCost(Ty);
299 assert(Cost >= 0 && "TTI should not produce negative costs!");
300 return Cost;
Cameron Esfahani17177d12015-02-05 02:09:33 +0000301}
302
Sjoerd Meijer38c2cd02016-07-14 07:44:20 +0000303int TargetTransformInfo::getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx,
304 const APInt &Imm,
305 Type *Ty) const {
306 int Cost = TTIImpl->getIntImmCodeSizeCost(Opcode, Idx, Imm, Ty);
307 assert(Cost >= 0 && "TTI should not produce negative costs!");
308 return Cost;
309}
310
Chandler Carruth93205eb2015-08-05 18:08:10 +0000311int TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
312 int Cost = TTIImpl->getIntImmCost(Imm, Ty);
313 assert(Cost >= 0 && "TTI should not produce negative costs!");
314 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000315}
316
Chandler Carruth93205eb2015-08-05 18:08:10 +0000317int TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx,
318 const APInt &Imm, Type *Ty) const {
319 int Cost = TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty);
320 assert(Cost >= 0 && "TTI should not produce negative costs!");
321 return Cost;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000322}
323
Chandler Carruth93205eb2015-08-05 18:08:10 +0000324int TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
325 const APInt &Imm, Type *Ty) const {
326 int Cost = TTIImpl->getIntImmCost(IID, Idx, Imm, Ty);
327 assert(Cost >= 0 && "TTI should not produce negative costs!");
328 return Cost;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000329}
330
Chandler Carruth539edf42013-01-05 11:43:11 +0000331unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000332 return TTIImpl->getNumberOfRegisters(Vector);
Chandler Carruth539edf42013-01-05 11:43:11 +0000333}
334
Nadav Rotemb1791a72013-01-09 22:29:00 +0000335unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000336 return TTIImpl->getRegisterBitWidth(Vector);
Nadav Rotemb1791a72013-01-09 22:29:00 +0000337}
338
Adam Nemete29686e2017-05-15 21:15:01 +0000339unsigned TargetTransformInfo::getMinVectorRegisterBitWidth() const {
340 return TTIImpl->getMinVectorRegisterBitWidth();
341}
342
Krzysztof Parzyszek5d93fdf2018-03-27 16:14:11 +0000343bool TargetTransformInfo::shouldMaximizeVectorBandwidth(bool OptSize) const {
344 return TTIImpl->shouldMaximizeVectorBandwidth(OptSize);
345}
346
Krzysztof Parzyszekdfed9412018-04-13 20:16:32 +0000347unsigned TargetTransformInfo::getMinimumVF(unsigned ElemWidth) const {
348 return TTIImpl->getMinimumVF(ElemWidth);
349}
350
Jun Bum Limdee55652017-04-03 19:20:07 +0000351bool TargetTransformInfo::shouldConsiderAddressTypePromotion(
352 const Instruction &I, bool &AllowPromotionWithoutCommonHeader) const {
353 return TTIImpl->shouldConsiderAddressTypePromotion(
354 I, AllowPromotionWithoutCommonHeader);
355}
356
Adam Nemetaf761102016-01-21 18:28:36 +0000357unsigned TargetTransformInfo::getCacheLineSize() const {
358 return TTIImpl->getCacheLineSize();
359}
360
Tobias Grosserd7eb6192017-08-24 09:46:25 +0000361llvm::Optional<unsigned> TargetTransformInfo::getCacheSize(CacheLevel Level)
362 const {
363 return TTIImpl->getCacheSize(Level);
364}
365
366llvm::Optional<unsigned> TargetTransformInfo::getCacheAssociativity(
367 CacheLevel Level) const {
368 return TTIImpl->getCacheAssociativity(Level);
369}
370
Adam Nemetdadfbb52016-01-27 22:21:25 +0000371unsigned TargetTransformInfo::getPrefetchDistance() const {
372 return TTIImpl->getPrefetchDistance();
373}
374
Adam Nemet6d8beec2016-03-18 00:27:38 +0000375unsigned TargetTransformInfo::getMinPrefetchStride() const {
376 return TTIImpl->getMinPrefetchStride();
377}
378
Adam Nemet709e3042016-03-18 00:27:43 +0000379unsigned TargetTransformInfo::getMaxPrefetchIterationsAhead() const {
380 return TTIImpl->getMaxPrefetchIterationsAhead();
381}
382
Wei Mi062c7442015-05-06 17:12:25 +0000383unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const {
384 return TTIImpl->getMaxInterleaveFactor(VF);
Nadav Rotemb696c362013-01-09 01:15:42 +0000385}
386
Jonas Paulsson29d80f02018-10-05 14:34:04 +0000387TargetTransformInfo::OperandValueKind
388TargetTransformInfo::getOperandInfo(Value *V,
389 OperandValueProperties &OpProps) const {
390 OperandValueKind OpInfo = OK_AnyValue;
391 OpProps = OP_None;
392
393 if (auto *CI = dyn_cast<ConstantInt>(V)) {
394 if (CI->getValue().isPowerOf2())
395 OpProps = OP_PowerOf2;
396 return OK_UniformConstantValue;
397 }
398
399 const Value *Splat = getSplatValue(V);
400
401 // Check for a splat of a constant or for a non uniform vector of constants
402 // and check if the constant(s) are all powers of two.
403 if (isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) {
404 OpInfo = OK_NonUniformConstantValue;
405 if (Splat) {
406 OpInfo = OK_UniformConstantValue;
407 if (auto *CI = dyn_cast<ConstantInt>(Splat))
408 if (CI->getValue().isPowerOf2())
409 OpProps = OP_PowerOf2;
410 } else if (auto *CDS = dyn_cast<ConstantDataSequential>(V)) {
411 OpProps = OP_PowerOf2;
412 for (unsigned I = 0, E = CDS->getNumElements(); I != E; ++I) {
413 if (auto *CI = dyn_cast<ConstantInt>(CDS->getElementAsConstant(I)))
414 if (CI->getValue().isPowerOf2())
415 continue;
416 OpProps = OP_None;
417 break;
418 }
419 }
420 }
421
422 // Check for a splat of a uniform value. This is not loop aware, so return
423 // true only for the obviously uniform cases (argument, globalvalue)
424 if (Splat && (isa<Argument>(Splat) || isa<GlobalValue>(Splat)))
425 OpInfo = OK_UniformValue;
426
427 return OpInfo;
428}
429
Chandler Carruth93205eb2015-08-05 18:08:10 +0000430int TargetTransformInfo::getArithmeticInstrCost(
Chandler Carruth705b1852015-01-31 03:43:40 +0000431 unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
432 OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000433 OperandValueProperties Opd2PropInfo,
434 ArrayRef<const Value *> Args) const {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000435 int Cost = TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000436 Opd1PropInfo, Opd2PropInfo, Args);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000437 assert(Cost >= 0 && "TTI should not produce negative costs!");
438 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000439}
440
Chandler Carruth93205eb2015-08-05 18:08:10 +0000441int TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty, int Index,
442 Type *SubTp) const {
443 int Cost = TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
444 assert(Cost >= 0 && "TTI should not produce negative costs!");
445 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000446}
447
Chandler Carruth93205eb2015-08-05 18:08:10 +0000448int TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000449 Type *Src, const Instruction *I) const {
450 assert ((I == nullptr || I->getOpcode() == Opcode) &&
451 "Opcode should reflect passed instruction.");
452 int Cost = TTIImpl->getCastInstrCost(Opcode, Dst, Src, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000453 assert(Cost >= 0 && "TTI should not produce negative costs!");
454 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000455}
456
Matthew Simpsone5dfb082016-04-27 15:20:21 +0000457int TargetTransformInfo::getExtractWithExtendCost(unsigned Opcode, Type *Dst,
458 VectorType *VecTy,
459 unsigned Index) const {
460 int Cost = TTIImpl->getExtractWithExtendCost(Opcode, Dst, VecTy, Index);
461 assert(Cost >= 0 && "TTI should not produce negative costs!");
462 return Cost;
463}
464
Chandler Carruth93205eb2015-08-05 18:08:10 +0000465int TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
466 int Cost = TTIImpl->getCFInstrCost(Opcode);
467 assert(Cost >= 0 && "TTI should not produce negative costs!");
468 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000469}
470
Chandler Carruth93205eb2015-08-05 18:08:10 +0000471int TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000472 Type *CondTy, const Instruction *I) const {
473 assert ((I == nullptr || I->getOpcode() == Opcode) &&
474 "Opcode should reflect passed instruction.");
475 int Cost = TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000476 assert(Cost >= 0 && "TTI should not produce negative costs!");
477 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000478}
479
Chandler Carruth93205eb2015-08-05 18:08:10 +0000480int TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
481 unsigned Index) const {
482 int Cost = TTIImpl->getVectorInstrCost(Opcode, Val, Index);
483 assert(Cost >= 0 && "TTI should not produce negative costs!");
484 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000485}
486
Chandler Carruth93205eb2015-08-05 18:08:10 +0000487int TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
488 unsigned Alignment,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000489 unsigned AddressSpace,
490 const Instruction *I) const {
491 assert ((I == nullptr || I->getOpcode() == Opcode) &&
492 "Opcode should reflect passed instruction.");
493 int Cost = TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000494 assert(Cost >= 0 && "TTI should not produce negative costs!");
495 return Cost;
Elena Demikhovskya3232f72015-01-25 08:44:46 +0000496}
497
Chandler Carruth93205eb2015-08-05 18:08:10 +0000498int TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
499 unsigned Alignment,
500 unsigned AddressSpace) const {
501 int Cost =
502 TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
503 assert(Cost >= 0 && "TTI should not produce negative costs!");
504 return Cost;
Chandler Carruth705b1852015-01-31 03:43:40 +0000505}
506
Elena Demikhovsky54946982015-12-28 20:10:59 +0000507int TargetTransformInfo::getGatherScatterOpCost(unsigned Opcode, Type *DataTy,
508 Value *Ptr, bool VariableMask,
509 unsigned Alignment) const {
510 int Cost = TTIImpl->getGatherScatterOpCost(Opcode, DataTy, Ptr, VariableMask,
511 Alignment);
512 assert(Cost >= 0 && "TTI should not produce negative costs!");
513 return Cost;
514}
515
Chandler Carruth93205eb2015-08-05 18:08:10 +0000516int TargetTransformInfo::getInterleavedMemoryOpCost(
Hao Liu32c05392015-06-08 06:39:56 +0000517 unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
518 unsigned Alignment, unsigned AddressSpace) const {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000519 int Cost = TTIImpl->getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
520 Alignment, AddressSpace);
521 assert(Cost >= 0 && "TTI should not produce negative costs!");
522 return Cost;
Hao Liu32c05392015-06-08 06:39:56 +0000523}
524
Chandler Carruth93205eb2015-08-05 18:08:10 +0000525int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
Jonas Paulssona48ea232017-03-14 06:35:36 +0000526 ArrayRef<Type *> Tys, FastMathFlags FMF,
527 unsigned ScalarizationCostPassed) const {
528 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys, FMF,
529 ScalarizationCostPassed);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000530 assert(Cost >= 0 && "TTI should not produce negative costs!");
531 return Cost;
532}
533
Elena Demikhovsky54946982015-12-28 20:10:59 +0000534int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
Jonas Paulssona48ea232017-03-14 06:35:36 +0000535 ArrayRef<Value *> Args, FastMathFlags FMF, unsigned VF) const {
536 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Args, FMF, VF);
Elena Demikhovsky54946982015-12-28 20:10:59 +0000537 assert(Cost >= 0 && "TTI should not produce negative costs!");
538 return Cost;
539}
540
Chandler Carruth93205eb2015-08-05 18:08:10 +0000541int TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy,
542 ArrayRef<Type *> Tys) const {
543 int Cost = TTIImpl->getCallInstrCost(F, RetTy, Tys);
544 assert(Cost >= 0 && "TTI should not produce negative costs!");
545 return Cost;
Michael Zolotukhin7ed84a82015-03-17 19:26:23 +0000546}
547
Chandler Carruth539edf42013-01-05 11:43:11 +0000548unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000549 return TTIImpl->getNumberOfParts(Tp);
Chandler Carruth539edf42013-01-05 11:43:11 +0000550}
551
Chandler Carruth93205eb2015-08-05 18:08:10 +0000552int TargetTransformInfo::getAddressComputationCost(Type *Tp,
Mohammed Agabaria23599ba2017-01-05 14:03:41 +0000553 ScalarEvolution *SE,
554 const SCEV *Ptr) const {
555 int Cost = TTIImpl->getAddressComputationCost(Tp, SE, Ptr);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000556 assert(Cost >= 0 && "TTI should not produce negative costs!");
557 return Cost;
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000558}
Chandler Carruth539edf42013-01-05 11:43:11 +0000559
Alexey Bataev3e9b3eb2017-07-31 14:19:32 +0000560int TargetTransformInfo::getArithmeticReductionCost(unsigned Opcode, Type *Ty,
561 bool IsPairwiseForm) const {
562 int Cost = TTIImpl->getArithmeticReductionCost(Opcode, Ty, IsPairwiseForm);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000563 assert(Cost >= 0 && "TTI should not produce negative costs!");
564 return Cost;
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000565}
566
Alexey Bataev6dd29fc2017-09-08 13:49:36 +0000567int TargetTransformInfo::getMinMaxReductionCost(Type *Ty, Type *CondTy,
568 bool IsPairwiseForm,
569 bool IsUnsigned) const {
570 int Cost =
571 TTIImpl->getMinMaxReductionCost(Ty, CondTy, IsPairwiseForm, IsUnsigned);
572 assert(Cost >= 0 && "TTI should not produce negative costs!");
573 return Cost;
574}
575
Chandler Carruth705b1852015-01-31 03:43:40 +0000576unsigned
577TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
578 return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
Chad Rosierf9327d62015-01-26 22:51:15 +0000579}
580
581bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst,
582 MemIntrinsicInfo &Info) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000583 return TTIImpl->getTgtMemIntrinsic(Inst, Info);
Chad Rosierf9327d62015-01-26 22:51:15 +0000584}
585
Anna Thomasb2a212c2017-06-06 16:45:25 +0000586unsigned TargetTransformInfo::getAtomicMemIntrinsicMaxElementSize() const {
587 return TTIImpl->getAtomicMemIntrinsicMaxElementSize();
588}
589
Chandler Carruth705b1852015-01-31 03:43:40 +0000590Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic(
591 IntrinsicInst *Inst, Type *ExpectedType) const {
592 return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
593}
594
Sean Fertile9cd1cdf2017-07-07 02:00:06 +0000595Type *TargetTransformInfo::getMemcpyLoopLoweringType(LLVMContext &Context,
596 Value *Length,
597 unsigned SrcAlign,
598 unsigned DestAlign) const {
599 return TTIImpl->getMemcpyLoopLoweringType(Context, Length, SrcAlign,
600 DestAlign);
601}
602
603void TargetTransformInfo::getMemcpyLoopResidualLoweringType(
604 SmallVectorImpl<Type *> &OpsOut, LLVMContext &Context,
605 unsigned RemainingBytes, unsigned SrcAlign, unsigned DestAlign) const {
606 TTIImpl->getMemcpyLoopResidualLoweringType(OpsOut, Context, RemainingBytes,
607 SrcAlign, DestAlign);
608}
609
Eric Christopherd566fb12015-07-29 22:09:48 +0000610bool TargetTransformInfo::areInlineCompatible(const Function *Caller,
611 const Function *Callee) const {
612 return TTIImpl->areInlineCompatible(Caller, Callee);
Eric Christopher4371b132015-07-02 01:11:47 +0000613}
614
Krzysztof Parzyszek0b377e02018-03-26 13:10:09 +0000615bool TargetTransformInfo::isIndexedLoadLegal(MemIndexedMode Mode,
616 Type *Ty) const {
617 return TTIImpl->isIndexedLoadLegal(Mode, Ty);
618}
619
620bool TargetTransformInfo::isIndexedStoreLegal(MemIndexedMode Mode,
621 Type *Ty) const {
622 return TTIImpl->isIndexedStoreLegal(Mode, Ty);
623}
624
Volkan Keles1c386812016-10-03 10:31:34 +0000625unsigned TargetTransformInfo::getLoadStoreVecRegBitWidth(unsigned AS) const {
626 return TTIImpl->getLoadStoreVecRegBitWidth(AS);
627}
628
629bool TargetTransformInfo::isLegalToVectorizeLoad(LoadInst *LI) const {
630 return TTIImpl->isLegalToVectorizeLoad(LI);
631}
632
633bool TargetTransformInfo::isLegalToVectorizeStore(StoreInst *SI) const {
634 return TTIImpl->isLegalToVectorizeStore(SI);
635}
636
637bool TargetTransformInfo::isLegalToVectorizeLoadChain(
638 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
639 return TTIImpl->isLegalToVectorizeLoadChain(ChainSizeInBytes, Alignment,
640 AddrSpace);
641}
642
643bool TargetTransformInfo::isLegalToVectorizeStoreChain(
644 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
645 return TTIImpl->isLegalToVectorizeStoreChain(ChainSizeInBytes, Alignment,
646 AddrSpace);
647}
648
649unsigned TargetTransformInfo::getLoadVectorFactor(unsigned VF,
650 unsigned LoadSize,
651 unsigned ChainSizeInBytes,
652 VectorType *VecTy) const {
653 return TTIImpl->getLoadVectorFactor(VF, LoadSize, ChainSizeInBytes, VecTy);
654}
655
656unsigned TargetTransformInfo::getStoreVectorFactor(unsigned VF,
657 unsigned StoreSize,
658 unsigned ChainSizeInBytes,
659 VectorType *VecTy) const {
660 return TTIImpl->getStoreVectorFactor(VF, StoreSize, ChainSizeInBytes, VecTy);
661}
662
Amara Emersoncf9daa32017-05-09 10:43:25 +0000663bool TargetTransformInfo::useReductionIntrinsic(unsigned Opcode,
664 Type *Ty, ReductionFlags Flags) const {
665 return TTIImpl->useReductionIntrinsic(Opcode, Ty, Flags);
666}
667
Amara Emerson836b0f42017-05-10 09:42:49 +0000668bool TargetTransformInfo::shouldExpandReduction(const IntrinsicInst *II) const {
669 return TTIImpl->shouldExpandReduction(II);
670}
Amara Emersoncf9daa32017-05-09 10:43:25 +0000671
Guozhi Wei62d64142017-09-08 22:29:17 +0000672int TargetTransformInfo::getInstructionLatency(const Instruction *I) const {
673 return TTIImpl->getInstructionLatency(I);
674}
675
Guozhi Wei62d64142017-09-08 22:29:17 +0000676static bool matchPairwiseShuffleMask(ShuffleVectorInst *SI, bool IsLeft,
677 unsigned Level) {
678 // We don't need a shuffle if we just want to have element 0 in position 0 of
679 // the vector.
680 if (!SI && Level == 0 && IsLeft)
681 return true;
682 else if (!SI)
683 return false;
684
685 SmallVector<int, 32> Mask(SI->getType()->getVectorNumElements(), -1);
686
687 // Build a mask of 0, 2, ... (left) or 1, 3, ... (right) depending on whether
688 // we look at the left or right side.
689 for (unsigned i = 0, e = (1 << Level), val = !IsLeft; i != e; ++i, val += 2)
690 Mask[i] = val;
691
692 SmallVector<int, 16> ActualMask = SI->getShuffleMask();
693 return Mask == ActualMask;
694}
695
696namespace {
697/// Kind of the reduction data.
698enum ReductionKind {
699 RK_None, /// Not a reduction.
700 RK_Arithmetic, /// Binary reduction data.
701 RK_MinMax, /// Min/max reduction data.
702 RK_UnsignedMinMax, /// Unsigned min/max reduction data.
703};
704/// Contains opcode + LHS/RHS parts of the reduction operations.
705struct ReductionData {
706 ReductionData() = delete;
707 ReductionData(ReductionKind Kind, unsigned Opcode, Value *LHS, Value *RHS)
708 : Opcode(Opcode), LHS(LHS), RHS(RHS), Kind(Kind) {
709 assert(Kind != RK_None && "expected binary or min/max reduction only.");
710 }
711 unsigned Opcode = 0;
712 Value *LHS = nullptr;
713 Value *RHS = nullptr;
714 ReductionKind Kind = RK_None;
715 bool hasSameData(ReductionData &RD) const {
716 return Kind == RD.Kind && Opcode == RD.Opcode;
717 }
718};
719} // namespace
720
721static Optional<ReductionData> getReductionData(Instruction *I) {
722 Value *L, *R;
723 if (m_BinOp(m_Value(L), m_Value(R)).match(I))
Fangrui Songf78650a2018-07-30 19:41:25 +0000724 return ReductionData(RK_Arithmetic, I->getOpcode(), L, R);
Guozhi Wei62d64142017-09-08 22:29:17 +0000725 if (auto *SI = dyn_cast<SelectInst>(I)) {
726 if (m_SMin(m_Value(L), m_Value(R)).match(SI) ||
727 m_SMax(m_Value(L), m_Value(R)).match(SI) ||
728 m_OrdFMin(m_Value(L), m_Value(R)).match(SI) ||
729 m_OrdFMax(m_Value(L), m_Value(R)).match(SI) ||
730 m_UnordFMin(m_Value(L), m_Value(R)).match(SI) ||
731 m_UnordFMax(m_Value(L), m_Value(R)).match(SI)) {
732 auto *CI = cast<CmpInst>(SI->getCondition());
Fangrui Songf78650a2018-07-30 19:41:25 +0000733 return ReductionData(RK_MinMax, CI->getOpcode(), L, R);
734 }
Guozhi Wei62d64142017-09-08 22:29:17 +0000735 if (m_UMin(m_Value(L), m_Value(R)).match(SI) ||
736 m_UMax(m_Value(L), m_Value(R)).match(SI)) {
737 auto *CI = cast<CmpInst>(SI->getCondition());
738 return ReductionData(RK_UnsignedMinMax, CI->getOpcode(), L, R);
739 }
740 }
741 return llvm::None;
742}
743
744static ReductionKind matchPairwiseReductionAtLevel(Instruction *I,
745 unsigned Level,
746 unsigned NumLevels) {
747 // Match one level of pairwise operations.
748 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef,
749 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef>
750 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef,
751 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef>
752 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1
753 if (!I)
754 return RK_None;
755
756 assert(I->getType()->isVectorTy() && "Expecting a vector type");
757
758 Optional<ReductionData> RD = getReductionData(I);
759 if (!RD)
760 return RK_None;
761
762 ShuffleVectorInst *LS = dyn_cast<ShuffleVectorInst>(RD->LHS);
763 if (!LS && Level)
764 return RK_None;
765 ShuffleVectorInst *RS = dyn_cast<ShuffleVectorInst>(RD->RHS);
766 if (!RS && Level)
767 return RK_None;
768
769 // On level 0 we can omit one shufflevector instruction.
770 if (!Level && !RS && !LS)
771 return RK_None;
772
773 // Shuffle inputs must match.
774 Value *NextLevelOpL = LS ? LS->getOperand(0) : nullptr;
775 Value *NextLevelOpR = RS ? RS->getOperand(0) : nullptr;
776 Value *NextLevelOp = nullptr;
777 if (NextLevelOpR && NextLevelOpL) {
778 // If we have two shuffles their operands must match.
779 if (NextLevelOpL != NextLevelOpR)
780 return RK_None;
781
782 NextLevelOp = NextLevelOpL;
783 } else if (Level == 0 && (NextLevelOpR || NextLevelOpL)) {
784 // On the first level we can omit the shufflevector <0, undef,...>. So the
785 // input to the other shufflevector <1, undef> must match with one of the
786 // inputs to the current binary operation.
787 // Example:
788 // %NextLevelOpL = shufflevector %R, <1, undef ...>
789 // %BinOp = fadd %NextLevelOpL, %R
790 if (NextLevelOpL && NextLevelOpL != RD->RHS)
791 return RK_None;
792 else if (NextLevelOpR && NextLevelOpR != RD->LHS)
793 return RK_None;
794
795 NextLevelOp = NextLevelOpL ? RD->RHS : RD->LHS;
796 } else
797 return RK_None;
798
799 // Check that the next levels binary operation exists and matches with the
800 // current one.
801 if (Level + 1 != NumLevels) {
802 Optional<ReductionData> NextLevelRD =
803 getReductionData(cast<Instruction>(NextLevelOp));
804 if (!NextLevelRD || !RD->hasSameData(*NextLevelRD))
805 return RK_None;
806 }
807
808 // Shuffle mask for pairwise operation must match.
809 if (matchPairwiseShuffleMask(LS, /*IsLeft=*/true, Level)) {
810 if (!matchPairwiseShuffleMask(RS, /*IsLeft=*/false, Level))
811 return RK_None;
812 } else if (matchPairwiseShuffleMask(RS, /*IsLeft=*/true, Level)) {
813 if (!matchPairwiseShuffleMask(LS, /*IsLeft=*/false, Level))
814 return RK_None;
815 } else {
816 return RK_None;
817 }
818
819 if (++Level == NumLevels)
820 return RD->Kind;
821
822 // Match next level.
823 return matchPairwiseReductionAtLevel(cast<Instruction>(NextLevelOp), Level,
824 NumLevels);
825}
826
827static ReductionKind matchPairwiseReduction(const ExtractElementInst *ReduxRoot,
828 unsigned &Opcode, Type *&Ty) {
829 if (!EnableReduxCost)
830 return RK_None;
831
832 // Need to extract the first element.
833 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1));
834 unsigned Idx = ~0u;
835 if (CI)
836 Idx = CI->getZExtValue();
837 if (Idx != 0)
838 return RK_None;
839
840 auto *RdxStart = dyn_cast<Instruction>(ReduxRoot->getOperand(0));
841 if (!RdxStart)
842 return RK_None;
843 Optional<ReductionData> RD = getReductionData(RdxStart);
844 if (!RD)
845 return RK_None;
846
847 Type *VecTy = RdxStart->getType();
848 unsigned NumVecElems = VecTy->getVectorNumElements();
849 if (!isPowerOf2_32(NumVecElems))
850 return RK_None;
851
852 // We look for a sequence of shuffle,shuffle,add triples like the following
853 // that builds a pairwise reduction tree.
Fangrui Songf78650a2018-07-30 19:41:25 +0000854 //
Guozhi Wei62d64142017-09-08 22:29:17 +0000855 // (X0, X1, X2, X3)
856 // (X0 + X1, X2 + X3, undef, undef)
857 // ((X0 + X1) + (X2 + X3), undef, undef, undef)
Fangrui Songf78650a2018-07-30 19:41:25 +0000858 //
Guozhi Wei62d64142017-09-08 22:29:17 +0000859 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef,
860 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef>
861 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef,
862 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef>
863 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1
864 // %rdx.shuf.1.0 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef,
865 // <4 x i32> <i32 0, i32 undef, i32 undef, i32 undef>
866 // %rdx.shuf.1.1 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef,
867 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
868 // %bin.rdx8 = fadd <4 x float> %rdx.shuf.1.0, %rdx.shuf.1.1
869 // %r = extractelement <4 x float> %bin.rdx8, i32 0
870 if (matchPairwiseReductionAtLevel(RdxStart, 0, Log2_32(NumVecElems)) ==
871 RK_None)
872 return RK_None;
873
874 Opcode = RD->Opcode;
875 Ty = VecTy;
876
877 return RD->Kind;
878}
879
880static std::pair<Value *, ShuffleVectorInst *>
881getShuffleAndOtherOprd(Value *L, Value *R) {
882 ShuffleVectorInst *S = nullptr;
883
884 if ((S = dyn_cast<ShuffleVectorInst>(L)))
885 return std::make_pair(R, S);
886
887 S = dyn_cast<ShuffleVectorInst>(R);
888 return std::make_pair(L, S);
889}
890
891static ReductionKind
892matchVectorSplittingReduction(const ExtractElementInst *ReduxRoot,
893 unsigned &Opcode, Type *&Ty) {
894 if (!EnableReduxCost)
895 return RK_None;
896
897 // Need to extract the first element.
898 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1));
899 unsigned Idx = ~0u;
900 if (CI)
901 Idx = CI->getZExtValue();
902 if (Idx != 0)
903 return RK_None;
904
905 auto *RdxStart = dyn_cast<Instruction>(ReduxRoot->getOperand(0));
906 if (!RdxStart)
907 return RK_None;
908 Optional<ReductionData> RD = getReductionData(RdxStart);
909 if (!RD)
910 return RK_None;
911
912 Type *VecTy = ReduxRoot->getOperand(0)->getType();
913 unsigned NumVecElems = VecTy->getVectorNumElements();
914 if (!isPowerOf2_32(NumVecElems))
915 return RK_None;
916
917 // We look for a sequence of shuffles and adds like the following matching one
918 // fadd, shuffle vector pair at a time.
Fangrui Songf78650a2018-07-30 19:41:25 +0000919 //
Guozhi Wei62d64142017-09-08 22:29:17 +0000920 // %rdx.shuf = shufflevector <4 x float> %rdx, <4 x float> undef,
921 // <4 x i32> <i32 2, i32 3, i32 undef, i32 undef>
922 // %bin.rdx = fadd <4 x float> %rdx, %rdx.shuf
923 // %rdx.shuf7 = shufflevector <4 x float> %bin.rdx, <4 x float> undef,
924 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
925 // %bin.rdx8 = fadd <4 x float> %bin.rdx, %rdx.shuf7
926 // %r = extractelement <4 x float> %bin.rdx8, i32 0
927
928 unsigned MaskStart = 1;
929 Instruction *RdxOp = RdxStart;
Fangrui Songf78650a2018-07-30 19:41:25 +0000930 SmallVector<int, 32> ShuffleMask(NumVecElems, 0);
Guozhi Wei62d64142017-09-08 22:29:17 +0000931 unsigned NumVecElemsRemain = NumVecElems;
932 while (NumVecElemsRemain - 1) {
933 // Check for the right reduction operation.
934 if (!RdxOp)
935 return RK_None;
936 Optional<ReductionData> RDLevel = getReductionData(RdxOp);
937 if (!RDLevel || !RDLevel->hasSameData(*RD))
938 return RK_None;
939
940 Value *NextRdxOp;
941 ShuffleVectorInst *Shuffle;
942 std::tie(NextRdxOp, Shuffle) =
943 getShuffleAndOtherOprd(RDLevel->LHS, RDLevel->RHS);
944
945 // Check the current reduction operation and the shuffle use the same value.
946 if (Shuffle == nullptr)
947 return RK_None;
948 if (Shuffle->getOperand(0) != NextRdxOp)
949 return RK_None;
950
951 // Check that shuffle masks matches.
952 for (unsigned j = 0; j != MaskStart; ++j)
953 ShuffleMask[j] = MaskStart + j;
954 // Fill the rest of the mask with -1 for undef.
955 std::fill(&ShuffleMask[MaskStart], ShuffleMask.end(), -1);
956
957 SmallVector<int, 16> Mask = Shuffle->getShuffleMask();
958 if (ShuffleMask != Mask)
959 return RK_None;
960
961 RdxOp = dyn_cast<Instruction>(NextRdxOp);
962 NumVecElemsRemain /= 2;
963 MaskStart *= 2;
964 }
965
966 Opcode = RD->Opcode;
967 Ty = VecTy;
968 return RD->Kind;
969}
970
971int TargetTransformInfo::getInstructionThroughput(const Instruction *I) const {
972 switch (I->getOpcode()) {
973 case Instruction::GetElementPtr:
974 return getUserCost(I);
975
976 case Instruction::Ret:
977 case Instruction::PHI:
978 case Instruction::Br: {
979 return getCFInstrCost(I->getOpcode());
980 }
981 case Instruction::Add:
982 case Instruction::FAdd:
983 case Instruction::Sub:
984 case Instruction::FSub:
985 case Instruction::Mul:
986 case Instruction::FMul:
987 case Instruction::UDiv:
988 case Instruction::SDiv:
989 case Instruction::FDiv:
990 case Instruction::URem:
991 case Instruction::SRem:
992 case Instruction::FRem:
993 case Instruction::Shl:
994 case Instruction::LShr:
995 case Instruction::AShr:
996 case Instruction::And:
997 case Instruction::Or:
998 case Instruction::Xor: {
Simon Pilgrim4162d772018-05-22 10:40:09 +0000999 TargetTransformInfo::OperandValueKind Op1VK, Op2VK;
1000 TargetTransformInfo::OperandValueProperties Op1VP, Op2VP;
1001 Op1VK = getOperandInfo(I->getOperand(0), Op1VP);
1002 Op2VK = getOperandInfo(I->getOperand(1), Op2VP);
1003 SmallVector<const Value *, 2> Operands(I->operand_values());
1004 return getArithmeticInstrCost(I->getOpcode(), I->getType(), Op1VK, Op2VK,
1005 Op1VP, Op2VP, Operands);
Guozhi Wei62d64142017-09-08 22:29:17 +00001006 }
1007 case Instruction::Select: {
1008 const SelectInst *SI = cast<SelectInst>(I);
1009 Type *CondTy = SI->getCondition()->getType();
1010 return getCmpSelInstrCost(I->getOpcode(), I->getType(), CondTy, I);
1011 }
1012 case Instruction::ICmp:
1013 case Instruction::FCmp: {
1014 Type *ValTy = I->getOperand(0)->getType();
1015 return getCmpSelInstrCost(I->getOpcode(), ValTy, I->getType(), I);
1016 }
1017 case Instruction::Store: {
1018 const StoreInst *SI = cast<StoreInst>(I);
1019 Type *ValTy = SI->getValueOperand()->getType();
1020 return getMemoryOpCost(I->getOpcode(), ValTy,
1021 SI->getAlignment(),
1022 SI->getPointerAddressSpace(), I);
1023 }
1024 case Instruction::Load: {
1025 const LoadInst *LI = cast<LoadInst>(I);
1026 return getMemoryOpCost(I->getOpcode(), I->getType(),
1027 LI->getAlignment(),
1028 LI->getPointerAddressSpace(), I);
1029 }
1030 case Instruction::ZExt:
1031 case Instruction::SExt:
1032 case Instruction::FPToUI:
1033 case Instruction::FPToSI:
1034 case Instruction::FPExt:
1035 case Instruction::PtrToInt:
1036 case Instruction::IntToPtr:
1037 case Instruction::SIToFP:
1038 case Instruction::UIToFP:
1039 case Instruction::Trunc:
1040 case Instruction::FPTrunc:
1041 case Instruction::BitCast:
1042 case Instruction::AddrSpaceCast: {
1043 Type *SrcTy = I->getOperand(0)->getType();
1044 return getCastInstrCost(I->getOpcode(), I->getType(), SrcTy, I);
1045 }
1046 case Instruction::ExtractElement: {
1047 const ExtractElementInst * EEI = cast<ExtractElementInst>(I);
1048 ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1));
1049 unsigned Idx = -1;
1050 if (CI)
1051 Idx = CI->getZExtValue();
1052
1053 // Try to match a reduction sequence (series of shufflevector and vector
1054 // adds followed by a extractelement).
1055 unsigned ReduxOpCode;
1056 Type *ReduxType;
1057
1058 switch (matchVectorSplittingReduction(EEI, ReduxOpCode, ReduxType)) {
1059 case RK_Arithmetic:
1060 return getArithmeticReductionCost(ReduxOpCode, ReduxType,
1061 /*IsPairwiseForm=*/false);
1062 case RK_MinMax:
1063 return getMinMaxReductionCost(
1064 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1065 /*IsPairwiseForm=*/false, /*IsUnsigned=*/false);
1066 case RK_UnsignedMinMax:
1067 return getMinMaxReductionCost(
1068 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1069 /*IsPairwiseForm=*/false, /*IsUnsigned=*/true);
1070 case RK_None:
1071 break;
1072 }
1073
1074 switch (matchPairwiseReduction(EEI, ReduxOpCode, ReduxType)) {
1075 case RK_Arithmetic:
1076 return getArithmeticReductionCost(ReduxOpCode, ReduxType,
1077 /*IsPairwiseForm=*/true);
1078 case RK_MinMax:
1079 return getMinMaxReductionCost(
1080 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1081 /*IsPairwiseForm=*/true, /*IsUnsigned=*/false);
1082 case RK_UnsignedMinMax:
1083 return getMinMaxReductionCost(
1084 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1085 /*IsPairwiseForm=*/true, /*IsUnsigned=*/true);
1086 case RK_None:
1087 break;
1088 }
1089
1090 return getVectorInstrCost(I->getOpcode(),
1091 EEI->getOperand(0)->getType(), Idx);
1092 }
1093 case Instruction::InsertElement: {
1094 const InsertElementInst * IE = cast<InsertElementInst>(I);
1095 ConstantInt *CI = dyn_cast<ConstantInt>(IE->getOperand(2));
Fangrui Songf78650a2018-07-30 19:41:25 +00001096 unsigned Idx = -1;
Guozhi Wei62d64142017-09-08 22:29:17 +00001097 if (CI)
1098 Idx = CI->getZExtValue();
1099 return getVectorInstrCost(I->getOpcode(),
1100 IE->getType(), Idx);
1101 }
1102 case Instruction::ShuffleVector: {
1103 const ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
Sanjay Patel2ca33602018-06-19 18:44:00 +00001104 // TODO: Identify and add costs for insert/extract subvector, etc.
1105 if (Shuffle->changesLength())
1106 return -1;
Fangrui Songf78650a2018-07-30 19:41:25 +00001107
Sanjay Patel2ca33602018-06-19 18:44:00 +00001108 if (Shuffle->isIdentity())
1109 return 0;
Guozhi Wei62d64142017-09-08 22:29:17 +00001110
Sanjay Patel2ca33602018-06-19 18:44:00 +00001111 Type *Ty = Shuffle->getType();
1112 if (Shuffle->isReverse())
1113 return TTIImpl->getShuffleCost(SK_Reverse, Ty, 0, nullptr);
Simon Pilgrim07839212018-06-12 14:47:13 +00001114
Sanjay Patel2ca33602018-06-19 18:44:00 +00001115 if (Shuffle->isSelect())
1116 return TTIImpl->getShuffleCost(SK_Select, Ty, 0, nullptr);
Simon Pilgrim07839212018-06-12 14:47:13 +00001117
Sanjay Patel2ca33602018-06-19 18:44:00 +00001118 if (Shuffle->isTranspose())
1119 return TTIImpl->getShuffleCost(SK_Transpose, Ty, 0, nullptr);
Matthew Simpsonb4096eb2018-04-26 13:48:33 +00001120
Sanjay Patel2ca33602018-06-19 18:44:00 +00001121 if (Shuffle->isZeroEltSplat())
1122 return TTIImpl->getShuffleCost(SK_Broadcast, Ty, 0, nullptr);
Guozhi Wei62d64142017-09-08 22:29:17 +00001123
Sanjay Patel2ca33602018-06-19 18:44:00 +00001124 if (Shuffle->isSingleSource())
1125 return TTIImpl->getShuffleCost(SK_PermuteSingleSrc, Ty, 0, nullptr);
Guozhi Wei62d64142017-09-08 22:29:17 +00001126
Sanjay Patel2ca33602018-06-19 18:44:00 +00001127 return TTIImpl->getShuffleCost(SK_PermuteTwoSrc, Ty, 0, nullptr);
Guozhi Wei62d64142017-09-08 22:29:17 +00001128 }
1129 case Instruction::Call:
1130 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1131 SmallVector<Value *, 4> Args(II->arg_operands());
1132
1133 FastMathFlags FMF;
1134 if (auto *FPMO = dyn_cast<FPMathOperator>(II))
1135 FMF = FPMO->getFastMathFlags();
1136
1137 return getIntrinsicInstrCost(II->getIntrinsicID(), II->getType(),
1138 Args, FMF);
1139 }
1140 return -1;
1141 default:
1142 // We don't have any information on this instruction.
1143 return -1;
1144 }
1145}
1146
Chandler Carruth705b1852015-01-31 03:43:40 +00001147TargetTransformInfo::Concept::~Concept() {}
1148
Chandler Carruthe0385522015-02-01 10:11:22 +00001149TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
1150
1151TargetIRAnalysis::TargetIRAnalysis(
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001152 std::function<Result(const Function &)> TTICallback)
Benjamin Kramer82de7d32016-05-27 14:27:24 +00001153 : TTICallback(std::move(TTICallback)) {}
Chandler Carruthe0385522015-02-01 10:11:22 +00001154
Chandler Carruth164a2aa62016-06-17 00:11:01 +00001155TargetIRAnalysis::Result TargetIRAnalysis::run(const Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +00001156 FunctionAnalysisManager &) {
Chandler Carruthe0385522015-02-01 10:11:22 +00001157 return TTICallback(F);
1158}
1159
Chandler Carruthdab4eae2016-11-23 17:53:26 +00001160AnalysisKey TargetIRAnalysis::Key;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +00001161
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001162TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(const Function &F) {
Mehdi Amini5010ebf2015-07-09 02:08:42 +00001163 return Result(F.getParent()->getDataLayout());
Chandler Carruthe0385522015-02-01 10:11:22 +00001164}
1165
Chandler Carruth705b1852015-01-31 03:43:40 +00001166// Register the basic pass.
1167INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
1168 "Target Transform Information", false, true)
1169char TargetTransformInfoWrapperPass::ID = 0;
Chandler Carruth539edf42013-01-05 11:43:11 +00001170
Chandler Carruth705b1852015-01-31 03:43:40 +00001171void TargetTransformInfoWrapperPass::anchor() {}
Chandler Carruth539edf42013-01-05 11:43:11 +00001172
Chandler Carruth705b1852015-01-31 03:43:40 +00001173TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass()
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001174 : ImmutablePass(ID) {
Chandler Carruth705b1852015-01-31 03:43:40 +00001175 initializeTargetTransformInfoWrapperPassPass(
1176 *PassRegistry::getPassRegistry());
1177}
1178
1179TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass(
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001180 TargetIRAnalysis TIRA)
1181 : ImmutablePass(ID), TIRA(std::move(TIRA)) {
Chandler Carruth705b1852015-01-31 03:43:40 +00001182 initializeTargetTransformInfoWrapperPassPass(
1183 *PassRegistry::getPassRegistry());
1184}
1185
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001186TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(const Function &F) {
Sean Silva36e0d012016-08-09 00:28:15 +00001187 FunctionAnalysisManager DummyFAM;
Chandler Carruth164a2aa62016-06-17 00:11:01 +00001188 TTI = TIRA.run(F, DummyFAM);
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001189 return *TTI;
1190}
1191
Chandler Carruth93dcdc42015-01-31 11:17:59 +00001192ImmutablePass *
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001193llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) {
1194 return new TargetTransformInfoWrapperPass(std::move(TIRA));
Chandler Carruth539edf42013-01-05 11:43:11 +00001195}