blob: 79fe6dc7d87cb26dc4fa2b5989fbf3a6493f434c [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
Dorit Nuzman38bbf812018-10-14 08:50:06 +0000271bool TargetTransformInfo::enableMaskedInterleavedAccessVectorization() const {
272 return TTIImpl->enableMaskedInterleavedAccessVectorization();
273}
274
Renato Golin5cb666a2016-04-14 20:42:18 +0000275bool TargetTransformInfo::isFPVectorizationPotentiallyUnsafe() const {
276 return TTIImpl->isFPVectorizationPotentiallyUnsafe();
277}
278
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000279bool TargetTransformInfo::allowsMisalignedMemoryAccesses(LLVMContext &Context,
280 unsigned BitWidth,
Alina Sbirlea327955e2016-07-11 20:46:17 +0000281 unsigned AddressSpace,
282 unsigned Alignment,
283 bool *Fast) const {
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000284 return TTIImpl->allowsMisalignedMemoryAccesses(Context, BitWidth, AddressSpace,
Alina Sbirlea327955e2016-07-11 20:46:17 +0000285 Alignment, Fast);
286}
287
Chandler Carruth50a36cd2013-01-07 03:16:03 +0000288TargetTransformInfo::PopcntSupportKind
289TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000290 return TTIImpl->getPopcntSupport(IntTyWidthInBit);
Chandler Carruth539edf42013-01-05 11:43:11 +0000291}
292
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000293bool TargetTransformInfo::haveFastSqrt(Type *Ty) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000294 return TTIImpl->haveFastSqrt(Ty);
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000295}
296
Sanjay Patel0de1a4b2017-11-27 21:15:43 +0000297bool TargetTransformInfo::isFCmpOrdCheaperThanFCmpZero(Type *Ty) const {
298 return TTIImpl->isFCmpOrdCheaperThanFCmpZero(Ty);
299}
300
Chandler Carruth93205eb2015-08-05 18:08:10 +0000301int TargetTransformInfo::getFPOpCost(Type *Ty) const {
302 int Cost = TTIImpl->getFPOpCost(Ty);
303 assert(Cost >= 0 && "TTI should not produce negative costs!");
304 return Cost;
Cameron Esfahani17177d12015-02-05 02:09:33 +0000305}
306
Sjoerd Meijer38c2cd02016-07-14 07:44:20 +0000307int TargetTransformInfo::getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx,
308 const APInt &Imm,
309 Type *Ty) const {
310 int Cost = TTIImpl->getIntImmCodeSizeCost(Opcode, Idx, Imm, Ty);
311 assert(Cost >= 0 && "TTI should not produce negative costs!");
312 return Cost;
313}
314
Chandler Carruth93205eb2015-08-05 18:08:10 +0000315int TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
316 int Cost = TTIImpl->getIntImmCost(Imm, Ty);
317 assert(Cost >= 0 && "TTI should not produce negative costs!");
318 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000319}
320
Chandler Carruth93205eb2015-08-05 18:08:10 +0000321int TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx,
322 const APInt &Imm, Type *Ty) const {
323 int Cost = TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty);
324 assert(Cost >= 0 && "TTI should not produce negative costs!");
325 return Cost;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000326}
327
Chandler Carruth93205eb2015-08-05 18:08:10 +0000328int TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
329 const APInt &Imm, Type *Ty) const {
330 int Cost = TTIImpl->getIntImmCost(IID, Idx, Imm, Ty);
331 assert(Cost >= 0 && "TTI should not produce negative costs!");
332 return Cost;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000333}
334
Chandler Carruth539edf42013-01-05 11:43:11 +0000335unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000336 return TTIImpl->getNumberOfRegisters(Vector);
Chandler Carruth539edf42013-01-05 11:43:11 +0000337}
338
Nadav Rotemb1791a72013-01-09 22:29:00 +0000339unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000340 return TTIImpl->getRegisterBitWidth(Vector);
Nadav Rotemb1791a72013-01-09 22:29:00 +0000341}
342
Adam Nemete29686e2017-05-15 21:15:01 +0000343unsigned TargetTransformInfo::getMinVectorRegisterBitWidth() const {
344 return TTIImpl->getMinVectorRegisterBitWidth();
345}
346
Krzysztof Parzyszek5d93fdf2018-03-27 16:14:11 +0000347bool TargetTransformInfo::shouldMaximizeVectorBandwidth(bool OptSize) const {
348 return TTIImpl->shouldMaximizeVectorBandwidth(OptSize);
349}
350
Krzysztof Parzyszekdfed9412018-04-13 20:16:32 +0000351unsigned TargetTransformInfo::getMinimumVF(unsigned ElemWidth) const {
352 return TTIImpl->getMinimumVF(ElemWidth);
353}
354
Jun Bum Limdee55652017-04-03 19:20:07 +0000355bool TargetTransformInfo::shouldConsiderAddressTypePromotion(
356 const Instruction &I, bool &AllowPromotionWithoutCommonHeader) const {
357 return TTIImpl->shouldConsiderAddressTypePromotion(
358 I, AllowPromotionWithoutCommonHeader);
359}
360
Adam Nemetaf761102016-01-21 18:28:36 +0000361unsigned TargetTransformInfo::getCacheLineSize() const {
362 return TTIImpl->getCacheLineSize();
363}
364
Tobias Grosserd7eb6192017-08-24 09:46:25 +0000365llvm::Optional<unsigned> TargetTransformInfo::getCacheSize(CacheLevel Level)
366 const {
367 return TTIImpl->getCacheSize(Level);
368}
369
370llvm::Optional<unsigned> TargetTransformInfo::getCacheAssociativity(
371 CacheLevel Level) const {
372 return TTIImpl->getCacheAssociativity(Level);
373}
374
Adam Nemetdadfbb52016-01-27 22:21:25 +0000375unsigned TargetTransformInfo::getPrefetchDistance() const {
376 return TTIImpl->getPrefetchDistance();
377}
378
Adam Nemet6d8beec2016-03-18 00:27:38 +0000379unsigned TargetTransformInfo::getMinPrefetchStride() const {
380 return TTIImpl->getMinPrefetchStride();
381}
382
Adam Nemet709e3042016-03-18 00:27:43 +0000383unsigned TargetTransformInfo::getMaxPrefetchIterationsAhead() const {
384 return TTIImpl->getMaxPrefetchIterationsAhead();
385}
386
Wei Mi062c7442015-05-06 17:12:25 +0000387unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const {
388 return TTIImpl->getMaxInterleaveFactor(VF);
Nadav Rotemb696c362013-01-09 01:15:42 +0000389}
390
Jonas Paulsson29d80f02018-10-05 14:34:04 +0000391TargetTransformInfo::OperandValueKind
Simon Pilgrim077a42c2018-11-13 13:45:10 +0000392TargetTransformInfo::getOperandInfo(Value *V, OperandValueProperties &OpProps) {
Jonas Paulsson29d80f02018-10-05 14:34:04 +0000393 OperandValueKind OpInfo = OK_AnyValue;
394 OpProps = OP_None;
395
396 if (auto *CI = dyn_cast<ConstantInt>(V)) {
397 if (CI->getValue().isPowerOf2())
398 OpProps = OP_PowerOf2;
399 return OK_UniformConstantValue;
400 }
401
Simon Pilgrim2b166c52018-11-14 15:04:08 +0000402 // A broadcast shuffle creates a uniform value.
403 // TODO: Add support for non-zero index broadcasts.
404 // TODO: Add support for different source vector width.
405 if (auto *ShuffleInst = dyn_cast<ShuffleVectorInst>(V))
406 if (ShuffleInst->isZeroEltSplat())
407 OpInfo = OK_UniformValue;
408
Jonas Paulsson29d80f02018-10-05 14:34:04 +0000409 const Value *Splat = getSplatValue(V);
410
411 // Check for a splat of a constant or for a non uniform vector of constants
412 // and check if the constant(s) are all powers of two.
413 if (isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) {
414 OpInfo = OK_NonUniformConstantValue;
415 if (Splat) {
416 OpInfo = OK_UniformConstantValue;
417 if (auto *CI = dyn_cast<ConstantInt>(Splat))
418 if (CI->getValue().isPowerOf2())
419 OpProps = OP_PowerOf2;
420 } else if (auto *CDS = dyn_cast<ConstantDataSequential>(V)) {
421 OpProps = OP_PowerOf2;
422 for (unsigned I = 0, E = CDS->getNumElements(); I != E; ++I) {
423 if (auto *CI = dyn_cast<ConstantInt>(CDS->getElementAsConstant(I)))
424 if (CI->getValue().isPowerOf2())
425 continue;
426 OpProps = OP_None;
427 break;
428 }
429 }
430 }
431
432 // Check for a splat of a uniform value. This is not loop aware, so return
433 // true only for the obviously uniform cases (argument, globalvalue)
434 if (Splat && (isa<Argument>(Splat) || isa<GlobalValue>(Splat)))
435 OpInfo = OK_UniformValue;
436
437 return OpInfo;
438}
439
Chandler Carruth93205eb2015-08-05 18:08:10 +0000440int TargetTransformInfo::getArithmeticInstrCost(
Chandler Carruth705b1852015-01-31 03:43:40 +0000441 unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
442 OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000443 OperandValueProperties Opd2PropInfo,
444 ArrayRef<const Value *> Args) const {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000445 int Cost = TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000446 Opd1PropInfo, Opd2PropInfo, Args);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000447 assert(Cost >= 0 && "TTI should not produce negative costs!");
448 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000449}
450
Chandler Carruth93205eb2015-08-05 18:08:10 +0000451int TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty, int Index,
452 Type *SubTp) const {
453 int Cost = TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
454 assert(Cost >= 0 && "TTI should not produce negative costs!");
455 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000456}
457
Chandler Carruth93205eb2015-08-05 18:08:10 +0000458int TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000459 Type *Src, const Instruction *I) const {
460 assert ((I == nullptr || I->getOpcode() == Opcode) &&
461 "Opcode should reflect passed instruction.");
462 int Cost = TTIImpl->getCastInstrCost(Opcode, Dst, Src, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000463 assert(Cost >= 0 && "TTI should not produce negative costs!");
464 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000465}
466
Matthew Simpsone5dfb082016-04-27 15:20:21 +0000467int TargetTransformInfo::getExtractWithExtendCost(unsigned Opcode, Type *Dst,
468 VectorType *VecTy,
469 unsigned Index) const {
470 int Cost = TTIImpl->getExtractWithExtendCost(Opcode, Dst, VecTy, Index);
471 assert(Cost >= 0 && "TTI should not produce negative costs!");
472 return Cost;
473}
474
Chandler Carruth93205eb2015-08-05 18:08:10 +0000475int TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
476 int Cost = TTIImpl->getCFInstrCost(Opcode);
477 assert(Cost >= 0 && "TTI should not produce negative costs!");
478 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000479}
480
Chandler Carruth93205eb2015-08-05 18:08:10 +0000481int TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000482 Type *CondTy, const Instruction *I) const {
483 assert ((I == nullptr || I->getOpcode() == Opcode) &&
484 "Opcode should reflect passed instruction.");
485 int Cost = TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000486 assert(Cost >= 0 && "TTI should not produce negative costs!");
487 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000488}
489
Chandler Carruth93205eb2015-08-05 18:08:10 +0000490int TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
491 unsigned Index) const {
492 int Cost = TTIImpl->getVectorInstrCost(Opcode, Val, Index);
493 assert(Cost >= 0 && "TTI should not produce negative costs!");
494 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000495}
496
Chandler Carruth93205eb2015-08-05 18:08:10 +0000497int TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
498 unsigned Alignment,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000499 unsigned AddressSpace,
500 const Instruction *I) const {
501 assert ((I == nullptr || I->getOpcode() == Opcode) &&
502 "Opcode should reflect passed instruction.");
503 int Cost = TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000504 assert(Cost >= 0 && "TTI should not produce negative costs!");
505 return Cost;
Elena Demikhovskya3232f72015-01-25 08:44:46 +0000506}
507
Chandler Carruth93205eb2015-08-05 18:08:10 +0000508int TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
509 unsigned Alignment,
510 unsigned AddressSpace) const {
511 int Cost =
512 TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
513 assert(Cost >= 0 && "TTI should not produce negative costs!");
514 return Cost;
Chandler Carruth705b1852015-01-31 03:43:40 +0000515}
516
Elena Demikhovsky54946982015-12-28 20:10:59 +0000517int TargetTransformInfo::getGatherScatterOpCost(unsigned Opcode, Type *DataTy,
518 Value *Ptr, bool VariableMask,
519 unsigned Alignment) const {
520 int Cost = TTIImpl->getGatherScatterOpCost(Opcode, DataTy, Ptr, VariableMask,
521 Alignment);
522 assert(Cost >= 0 && "TTI should not produce negative costs!");
523 return Cost;
524}
525
Chandler Carruth93205eb2015-08-05 18:08:10 +0000526int TargetTransformInfo::getInterleavedMemoryOpCost(
Hao Liu32c05392015-06-08 06:39:56 +0000527 unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
Dorit Nuzman34da6dd2018-10-31 09:57:56 +0000528 unsigned Alignment, unsigned AddressSpace, bool UseMaskForCond,
529 bool UseMaskForGaps) const {
530 int Cost = TTIImpl->getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
531 Alignment, AddressSpace,
532 UseMaskForCond,
533 UseMaskForGaps);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000534 assert(Cost >= 0 && "TTI should not produce negative costs!");
535 return Cost;
Hao Liu32c05392015-06-08 06:39:56 +0000536}
537
Chandler Carruth93205eb2015-08-05 18:08:10 +0000538int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
Jonas Paulssona48ea232017-03-14 06:35:36 +0000539 ArrayRef<Type *> Tys, FastMathFlags FMF,
540 unsigned ScalarizationCostPassed) const {
541 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys, FMF,
542 ScalarizationCostPassed);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000543 assert(Cost >= 0 && "TTI should not produce negative costs!");
544 return Cost;
545}
546
Elena Demikhovsky54946982015-12-28 20:10:59 +0000547int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
Jonas Paulssona48ea232017-03-14 06:35:36 +0000548 ArrayRef<Value *> Args, FastMathFlags FMF, unsigned VF) const {
549 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Args, FMF, VF);
Elena Demikhovsky54946982015-12-28 20:10:59 +0000550 assert(Cost >= 0 && "TTI should not produce negative costs!");
551 return Cost;
552}
553
Chandler Carruth93205eb2015-08-05 18:08:10 +0000554int TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy,
555 ArrayRef<Type *> Tys) const {
556 int Cost = TTIImpl->getCallInstrCost(F, RetTy, Tys);
557 assert(Cost >= 0 && "TTI should not produce negative costs!");
558 return Cost;
Michael Zolotukhin7ed84a82015-03-17 19:26:23 +0000559}
560
Chandler Carruth539edf42013-01-05 11:43:11 +0000561unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000562 return TTIImpl->getNumberOfParts(Tp);
Chandler Carruth539edf42013-01-05 11:43:11 +0000563}
564
Chandler Carruth93205eb2015-08-05 18:08:10 +0000565int TargetTransformInfo::getAddressComputationCost(Type *Tp,
Mohammed Agabaria23599ba2017-01-05 14:03:41 +0000566 ScalarEvolution *SE,
567 const SCEV *Ptr) const {
568 int Cost = TTIImpl->getAddressComputationCost(Tp, SE, Ptr);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000569 assert(Cost >= 0 && "TTI should not produce negative costs!");
570 return Cost;
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000571}
Chandler Carruth539edf42013-01-05 11:43:11 +0000572
Alexey Bataev3e9b3eb2017-07-31 14:19:32 +0000573int TargetTransformInfo::getArithmeticReductionCost(unsigned Opcode, Type *Ty,
574 bool IsPairwiseForm) const {
575 int Cost = TTIImpl->getArithmeticReductionCost(Opcode, Ty, IsPairwiseForm);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000576 assert(Cost >= 0 && "TTI should not produce negative costs!");
577 return Cost;
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000578}
579
Alexey Bataev6dd29fc2017-09-08 13:49:36 +0000580int TargetTransformInfo::getMinMaxReductionCost(Type *Ty, Type *CondTy,
581 bool IsPairwiseForm,
582 bool IsUnsigned) const {
583 int Cost =
584 TTIImpl->getMinMaxReductionCost(Ty, CondTy, IsPairwiseForm, IsUnsigned);
585 assert(Cost >= 0 && "TTI should not produce negative costs!");
586 return Cost;
587}
588
Chandler Carruth705b1852015-01-31 03:43:40 +0000589unsigned
590TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
591 return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
Chad Rosierf9327d62015-01-26 22:51:15 +0000592}
593
594bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst,
595 MemIntrinsicInfo &Info) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000596 return TTIImpl->getTgtMemIntrinsic(Inst, Info);
Chad Rosierf9327d62015-01-26 22:51:15 +0000597}
598
Anna Thomasb2a212c2017-06-06 16:45:25 +0000599unsigned TargetTransformInfo::getAtomicMemIntrinsicMaxElementSize() const {
600 return TTIImpl->getAtomicMemIntrinsicMaxElementSize();
601}
602
Chandler Carruth705b1852015-01-31 03:43:40 +0000603Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic(
604 IntrinsicInst *Inst, Type *ExpectedType) const {
605 return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
606}
607
Sean Fertile9cd1cdf2017-07-07 02:00:06 +0000608Type *TargetTransformInfo::getMemcpyLoopLoweringType(LLVMContext &Context,
609 Value *Length,
610 unsigned SrcAlign,
611 unsigned DestAlign) const {
612 return TTIImpl->getMemcpyLoopLoweringType(Context, Length, SrcAlign,
613 DestAlign);
614}
615
616void TargetTransformInfo::getMemcpyLoopResidualLoweringType(
617 SmallVectorImpl<Type *> &OpsOut, LLVMContext &Context,
618 unsigned RemainingBytes, unsigned SrcAlign, unsigned DestAlign) const {
619 TTIImpl->getMemcpyLoopResidualLoweringType(OpsOut, Context, RemainingBytes,
620 SrcAlign, DestAlign);
621}
622
Eric Christopherd566fb12015-07-29 22:09:48 +0000623bool TargetTransformInfo::areInlineCompatible(const Function *Caller,
624 const Function *Callee) const {
625 return TTIImpl->areInlineCompatible(Caller, Callee);
Eric Christopher4371b132015-07-02 01:11:47 +0000626}
627
Krzysztof Parzyszek0b377e02018-03-26 13:10:09 +0000628bool TargetTransformInfo::isIndexedLoadLegal(MemIndexedMode Mode,
629 Type *Ty) const {
630 return TTIImpl->isIndexedLoadLegal(Mode, Ty);
631}
632
633bool TargetTransformInfo::isIndexedStoreLegal(MemIndexedMode Mode,
634 Type *Ty) const {
635 return TTIImpl->isIndexedStoreLegal(Mode, Ty);
636}
637
Volkan Keles1c386812016-10-03 10:31:34 +0000638unsigned TargetTransformInfo::getLoadStoreVecRegBitWidth(unsigned AS) const {
639 return TTIImpl->getLoadStoreVecRegBitWidth(AS);
640}
641
642bool TargetTransformInfo::isLegalToVectorizeLoad(LoadInst *LI) const {
643 return TTIImpl->isLegalToVectorizeLoad(LI);
644}
645
646bool TargetTransformInfo::isLegalToVectorizeStore(StoreInst *SI) const {
647 return TTIImpl->isLegalToVectorizeStore(SI);
648}
649
650bool TargetTransformInfo::isLegalToVectorizeLoadChain(
651 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
652 return TTIImpl->isLegalToVectorizeLoadChain(ChainSizeInBytes, Alignment,
653 AddrSpace);
654}
655
656bool TargetTransformInfo::isLegalToVectorizeStoreChain(
657 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
658 return TTIImpl->isLegalToVectorizeStoreChain(ChainSizeInBytes, Alignment,
659 AddrSpace);
660}
661
662unsigned TargetTransformInfo::getLoadVectorFactor(unsigned VF,
663 unsigned LoadSize,
664 unsigned ChainSizeInBytes,
665 VectorType *VecTy) const {
666 return TTIImpl->getLoadVectorFactor(VF, LoadSize, ChainSizeInBytes, VecTy);
667}
668
669unsigned TargetTransformInfo::getStoreVectorFactor(unsigned VF,
670 unsigned StoreSize,
671 unsigned ChainSizeInBytes,
672 VectorType *VecTy) const {
673 return TTIImpl->getStoreVectorFactor(VF, StoreSize, ChainSizeInBytes, VecTy);
674}
675
Amara Emersoncf9daa32017-05-09 10:43:25 +0000676bool TargetTransformInfo::useReductionIntrinsic(unsigned Opcode,
677 Type *Ty, ReductionFlags Flags) const {
678 return TTIImpl->useReductionIntrinsic(Opcode, Ty, Flags);
679}
680
Amara Emerson836b0f42017-05-10 09:42:49 +0000681bool TargetTransformInfo::shouldExpandReduction(const IntrinsicInst *II) const {
682 return TTIImpl->shouldExpandReduction(II);
683}
Amara Emersoncf9daa32017-05-09 10:43:25 +0000684
Guozhi Wei62d64142017-09-08 22:29:17 +0000685int TargetTransformInfo::getInstructionLatency(const Instruction *I) const {
686 return TTIImpl->getInstructionLatency(I);
687}
688
Guozhi Wei62d64142017-09-08 22:29:17 +0000689static bool matchPairwiseShuffleMask(ShuffleVectorInst *SI, bool IsLeft,
690 unsigned Level) {
691 // We don't need a shuffle if we just want to have element 0 in position 0 of
692 // the vector.
693 if (!SI && Level == 0 && IsLeft)
694 return true;
695 else if (!SI)
696 return false;
697
698 SmallVector<int, 32> Mask(SI->getType()->getVectorNumElements(), -1);
699
700 // Build a mask of 0, 2, ... (left) or 1, 3, ... (right) depending on whether
701 // we look at the left or right side.
702 for (unsigned i = 0, e = (1 << Level), val = !IsLeft; i != e; ++i, val += 2)
703 Mask[i] = val;
704
705 SmallVector<int, 16> ActualMask = SI->getShuffleMask();
706 return Mask == ActualMask;
707}
708
709namespace {
710/// Kind of the reduction data.
711enum ReductionKind {
712 RK_None, /// Not a reduction.
713 RK_Arithmetic, /// Binary reduction data.
714 RK_MinMax, /// Min/max reduction data.
715 RK_UnsignedMinMax, /// Unsigned min/max reduction data.
716};
717/// Contains opcode + LHS/RHS parts of the reduction operations.
718struct ReductionData {
719 ReductionData() = delete;
720 ReductionData(ReductionKind Kind, unsigned Opcode, Value *LHS, Value *RHS)
721 : Opcode(Opcode), LHS(LHS), RHS(RHS), Kind(Kind) {
722 assert(Kind != RK_None && "expected binary or min/max reduction only.");
723 }
724 unsigned Opcode = 0;
725 Value *LHS = nullptr;
726 Value *RHS = nullptr;
727 ReductionKind Kind = RK_None;
728 bool hasSameData(ReductionData &RD) const {
729 return Kind == RD.Kind && Opcode == RD.Opcode;
730 }
731};
732} // namespace
733
734static Optional<ReductionData> getReductionData(Instruction *I) {
735 Value *L, *R;
736 if (m_BinOp(m_Value(L), m_Value(R)).match(I))
Fangrui Songf78650a2018-07-30 19:41:25 +0000737 return ReductionData(RK_Arithmetic, I->getOpcode(), L, R);
Guozhi Wei62d64142017-09-08 22:29:17 +0000738 if (auto *SI = dyn_cast<SelectInst>(I)) {
739 if (m_SMin(m_Value(L), m_Value(R)).match(SI) ||
740 m_SMax(m_Value(L), m_Value(R)).match(SI) ||
741 m_OrdFMin(m_Value(L), m_Value(R)).match(SI) ||
742 m_OrdFMax(m_Value(L), m_Value(R)).match(SI) ||
743 m_UnordFMin(m_Value(L), m_Value(R)).match(SI) ||
744 m_UnordFMax(m_Value(L), m_Value(R)).match(SI)) {
745 auto *CI = cast<CmpInst>(SI->getCondition());
Fangrui Songf78650a2018-07-30 19:41:25 +0000746 return ReductionData(RK_MinMax, CI->getOpcode(), L, R);
747 }
Guozhi Wei62d64142017-09-08 22:29:17 +0000748 if (m_UMin(m_Value(L), m_Value(R)).match(SI) ||
749 m_UMax(m_Value(L), m_Value(R)).match(SI)) {
750 auto *CI = cast<CmpInst>(SI->getCondition());
751 return ReductionData(RK_UnsignedMinMax, CI->getOpcode(), L, R);
752 }
753 }
754 return llvm::None;
755}
756
757static ReductionKind matchPairwiseReductionAtLevel(Instruction *I,
758 unsigned Level,
759 unsigned NumLevels) {
760 // Match one level of pairwise operations.
761 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef,
762 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef>
763 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef,
764 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef>
765 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1
766 if (!I)
767 return RK_None;
768
769 assert(I->getType()->isVectorTy() && "Expecting a vector type");
770
771 Optional<ReductionData> RD = getReductionData(I);
772 if (!RD)
773 return RK_None;
774
775 ShuffleVectorInst *LS = dyn_cast<ShuffleVectorInst>(RD->LHS);
776 if (!LS && Level)
777 return RK_None;
778 ShuffleVectorInst *RS = dyn_cast<ShuffleVectorInst>(RD->RHS);
779 if (!RS && Level)
780 return RK_None;
781
782 // On level 0 we can omit one shufflevector instruction.
783 if (!Level && !RS && !LS)
784 return RK_None;
785
786 // Shuffle inputs must match.
787 Value *NextLevelOpL = LS ? LS->getOperand(0) : nullptr;
788 Value *NextLevelOpR = RS ? RS->getOperand(0) : nullptr;
789 Value *NextLevelOp = nullptr;
790 if (NextLevelOpR && NextLevelOpL) {
791 // If we have two shuffles their operands must match.
792 if (NextLevelOpL != NextLevelOpR)
793 return RK_None;
794
795 NextLevelOp = NextLevelOpL;
796 } else if (Level == 0 && (NextLevelOpR || NextLevelOpL)) {
797 // On the first level we can omit the shufflevector <0, undef,...>. So the
798 // input to the other shufflevector <1, undef> must match with one of the
799 // inputs to the current binary operation.
800 // Example:
801 // %NextLevelOpL = shufflevector %R, <1, undef ...>
802 // %BinOp = fadd %NextLevelOpL, %R
803 if (NextLevelOpL && NextLevelOpL != RD->RHS)
804 return RK_None;
805 else if (NextLevelOpR && NextLevelOpR != RD->LHS)
806 return RK_None;
807
808 NextLevelOp = NextLevelOpL ? RD->RHS : RD->LHS;
809 } else
810 return RK_None;
811
812 // Check that the next levels binary operation exists and matches with the
813 // current one.
814 if (Level + 1 != NumLevels) {
815 Optional<ReductionData> NextLevelRD =
816 getReductionData(cast<Instruction>(NextLevelOp));
817 if (!NextLevelRD || !RD->hasSameData(*NextLevelRD))
818 return RK_None;
819 }
820
821 // Shuffle mask for pairwise operation must match.
822 if (matchPairwiseShuffleMask(LS, /*IsLeft=*/true, Level)) {
823 if (!matchPairwiseShuffleMask(RS, /*IsLeft=*/false, Level))
824 return RK_None;
825 } else if (matchPairwiseShuffleMask(RS, /*IsLeft=*/true, Level)) {
826 if (!matchPairwiseShuffleMask(LS, /*IsLeft=*/false, Level))
827 return RK_None;
828 } else {
829 return RK_None;
830 }
831
832 if (++Level == NumLevels)
833 return RD->Kind;
834
835 // Match next level.
836 return matchPairwiseReductionAtLevel(cast<Instruction>(NextLevelOp), Level,
837 NumLevels);
838}
839
840static ReductionKind matchPairwiseReduction(const ExtractElementInst *ReduxRoot,
841 unsigned &Opcode, Type *&Ty) {
842 if (!EnableReduxCost)
843 return RK_None;
844
845 // Need to extract the first element.
846 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1));
847 unsigned Idx = ~0u;
848 if (CI)
849 Idx = CI->getZExtValue();
850 if (Idx != 0)
851 return RK_None;
852
853 auto *RdxStart = dyn_cast<Instruction>(ReduxRoot->getOperand(0));
854 if (!RdxStart)
855 return RK_None;
856 Optional<ReductionData> RD = getReductionData(RdxStart);
857 if (!RD)
858 return RK_None;
859
860 Type *VecTy = RdxStart->getType();
861 unsigned NumVecElems = VecTy->getVectorNumElements();
862 if (!isPowerOf2_32(NumVecElems))
863 return RK_None;
864
865 // We look for a sequence of shuffle,shuffle,add triples like the following
866 // that builds a pairwise reduction tree.
Fangrui Songf78650a2018-07-30 19:41:25 +0000867 //
Guozhi Wei62d64142017-09-08 22:29:17 +0000868 // (X0, X1, X2, X3)
869 // (X0 + X1, X2 + X3, undef, undef)
870 // ((X0 + X1) + (X2 + X3), undef, undef, undef)
Fangrui Songf78650a2018-07-30 19:41:25 +0000871 //
Guozhi Wei62d64142017-09-08 22:29:17 +0000872 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef,
873 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef>
874 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef,
875 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef>
876 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1
877 // %rdx.shuf.1.0 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef,
878 // <4 x i32> <i32 0, i32 undef, i32 undef, i32 undef>
879 // %rdx.shuf.1.1 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef,
880 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
881 // %bin.rdx8 = fadd <4 x float> %rdx.shuf.1.0, %rdx.shuf.1.1
882 // %r = extractelement <4 x float> %bin.rdx8, i32 0
883 if (matchPairwiseReductionAtLevel(RdxStart, 0, Log2_32(NumVecElems)) ==
884 RK_None)
885 return RK_None;
886
887 Opcode = RD->Opcode;
888 Ty = VecTy;
889
890 return RD->Kind;
891}
892
893static std::pair<Value *, ShuffleVectorInst *>
894getShuffleAndOtherOprd(Value *L, Value *R) {
895 ShuffleVectorInst *S = nullptr;
896
897 if ((S = dyn_cast<ShuffleVectorInst>(L)))
898 return std::make_pair(R, S);
899
900 S = dyn_cast<ShuffleVectorInst>(R);
901 return std::make_pair(L, S);
902}
903
904static ReductionKind
905matchVectorSplittingReduction(const ExtractElementInst *ReduxRoot,
906 unsigned &Opcode, Type *&Ty) {
907 if (!EnableReduxCost)
908 return RK_None;
909
910 // Need to extract the first element.
911 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1));
912 unsigned Idx = ~0u;
913 if (CI)
914 Idx = CI->getZExtValue();
915 if (Idx != 0)
916 return RK_None;
917
918 auto *RdxStart = dyn_cast<Instruction>(ReduxRoot->getOperand(0));
919 if (!RdxStart)
920 return RK_None;
921 Optional<ReductionData> RD = getReductionData(RdxStart);
922 if (!RD)
923 return RK_None;
924
925 Type *VecTy = ReduxRoot->getOperand(0)->getType();
926 unsigned NumVecElems = VecTy->getVectorNumElements();
927 if (!isPowerOf2_32(NumVecElems))
928 return RK_None;
929
930 // We look for a sequence of shuffles and adds like the following matching one
931 // fadd, shuffle vector pair at a time.
Fangrui Songf78650a2018-07-30 19:41:25 +0000932 //
Guozhi Wei62d64142017-09-08 22:29:17 +0000933 // %rdx.shuf = shufflevector <4 x float> %rdx, <4 x float> undef,
934 // <4 x i32> <i32 2, i32 3, i32 undef, i32 undef>
935 // %bin.rdx = fadd <4 x float> %rdx, %rdx.shuf
936 // %rdx.shuf7 = shufflevector <4 x float> %bin.rdx, <4 x float> undef,
937 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
938 // %bin.rdx8 = fadd <4 x float> %bin.rdx, %rdx.shuf7
939 // %r = extractelement <4 x float> %bin.rdx8, i32 0
940
941 unsigned MaskStart = 1;
942 Instruction *RdxOp = RdxStart;
Fangrui Songf78650a2018-07-30 19:41:25 +0000943 SmallVector<int, 32> ShuffleMask(NumVecElems, 0);
Guozhi Wei62d64142017-09-08 22:29:17 +0000944 unsigned NumVecElemsRemain = NumVecElems;
945 while (NumVecElemsRemain - 1) {
946 // Check for the right reduction operation.
947 if (!RdxOp)
948 return RK_None;
949 Optional<ReductionData> RDLevel = getReductionData(RdxOp);
950 if (!RDLevel || !RDLevel->hasSameData(*RD))
951 return RK_None;
952
953 Value *NextRdxOp;
954 ShuffleVectorInst *Shuffle;
955 std::tie(NextRdxOp, Shuffle) =
956 getShuffleAndOtherOprd(RDLevel->LHS, RDLevel->RHS);
957
958 // Check the current reduction operation and the shuffle use the same value.
959 if (Shuffle == nullptr)
960 return RK_None;
961 if (Shuffle->getOperand(0) != NextRdxOp)
962 return RK_None;
963
964 // Check that shuffle masks matches.
965 for (unsigned j = 0; j != MaskStart; ++j)
966 ShuffleMask[j] = MaskStart + j;
967 // Fill the rest of the mask with -1 for undef.
968 std::fill(&ShuffleMask[MaskStart], ShuffleMask.end(), -1);
969
970 SmallVector<int, 16> Mask = Shuffle->getShuffleMask();
971 if (ShuffleMask != Mask)
972 return RK_None;
973
974 RdxOp = dyn_cast<Instruction>(NextRdxOp);
975 NumVecElemsRemain /= 2;
976 MaskStart *= 2;
977 }
978
979 Opcode = RD->Opcode;
980 Ty = VecTy;
981 return RD->Kind;
982}
983
984int TargetTransformInfo::getInstructionThroughput(const Instruction *I) const {
985 switch (I->getOpcode()) {
986 case Instruction::GetElementPtr:
987 return getUserCost(I);
988
989 case Instruction::Ret:
990 case Instruction::PHI:
991 case Instruction::Br: {
992 return getCFInstrCost(I->getOpcode());
993 }
994 case Instruction::Add:
995 case Instruction::FAdd:
996 case Instruction::Sub:
997 case Instruction::FSub:
998 case Instruction::Mul:
999 case Instruction::FMul:
1000 case Instruction::UDiv:
1001 case Instruction::SDiv:
1002 case Instruction::FDiv:
1003 case Instruction::URem:
1004 case Instruction::SRem:
1005 case Instruction::FRem:
1006 case Instruction::Shl:
1007 case Instruction::LShr:
1008 case Instruction::AShr:
1009 case Instruction::And:
1010 case Instruction::Or:
1011 case Instruction::Xor: {
Simon Pilgrim4162d772018-05-22 10:40:09 +00001012 TargetTransformInfo::OperandValueKind Op1VK, Op2VK;
1013 TargetTransformInfo::OperandValueProperties Op1VP, Op2VP;
1014 Op1VK = getOperandInfo(I->getOperand(0), Op1VP);
1015 Op2VK = getOperandInfo(I->getOperand(1), Op2VP);
1016 SmallVector<const Value *, 2> Operands(I->operand_values());
1017 return getArithmeticInstrCost(I->getOpcode(), I->getType(), Op1VK, Op2VK,
1018 Op1VP, Op2VP, Operands);
Guozhi Wei62d64142017-09-08 22:29:17 +00001019 }
1020 case Instruction::Select: {
1021 const SelectInst *SI = cast<SelectInst>(I);
1022 Type *CondTy = SI->getCondition()->getType();
1023 return getCmpSelInstrCost(I->getOpcode(), I->getType(), CondTy, I);
1024 }
1025 case Instruction::ICmp:
1026 case Instruction::FCmp: {
1027 Type *ValTy = I->getOperand(0)->getType();
1028 return getCmpSelInstrCost(I->getOpcode(), ValTy, I->getType(), I);
1029 }
1030 case Instruction::Store: {
1031 const StoreInst *SI = cast<StoreInst>(I);
1032 Type *ValTy = SI->getValueOperand()->getType();
1033 return getMemoryOpCost(I->getOpcode(), ValTy,
1034 SI->getAlignment(),
1035 SI->getPointerAddressSpace(), I);
1036 }
1037 case Instruction::Load: {
1038 const LoadInst *LI = cast<LoadInst>(I);
1039 return getMemoryOpCost(I->getOpcode(), I->getType(),
1040 LI->getAlignment(),
1041 LI->getPointerAddressSpace(), I);
1042 }
1043 case Instruction::ZExt:
1044 case Instruction::SExt:
1045 case Instruction::FPToUI:
1046 case Instruction::FPToSI:
1047 case Instruction::FPExt:
1048 case Instruction::PtrToInt:
1049 case Instruction::IntToPtr:
1050 case Instruction::SIToFP:
1051 case Instruction::UIToFP:
1052 case Instruction::Trunc:
1053 case Instruction::FPTrunc:
1054 case Instruction::BitCast:
1055 case Instruction::AddrSpaceCast: {
1056 Type *SrcTy = I->getOperand(0)->getType();
1057 return getCastInstrCost(I->getOpcode(), I->getType(), SrcTy, I);
1058 }
1059 case Instruction::ExtractElement: {
1060 const ExtractElementInst * EEI = cast<ExtractElementInst>(I);
1061 ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1));
1062 unsigned Idx = -1;
1063 if (CI)
1064 Idx = CI->getZExtValue();
1065
1066 // Try to match a reduction sequence (series of shufflevector and vector
1067 // adds followed by a extractelement).
1068 unsigned ReduxOpCode;
1069 Type *ReduxType;
1070
1071 switch (matchVectorSplittingReduction(EEI, ReduxOpCode, ReduxType)) {
1072 case RK_Arithmetic:
1073 return getArithmeticReductionCost(ReduxOpCode, ReduxType,
1074 /*IsPairwiseForm=*/false);
1075 case RK_MinMax:
1076 return getMinMaxReductionCost(
1077 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1078 /*IsPairwiseForm=*/false, /*IsUnsigned=*/false);
1079 case RK_UnsignedMinMax:
1080 return getMinMaxReductionCost(
1081 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1082 /*IsPairwiseForm=*/false, /*IsUnsigned=*/true);
1083 case RK_None:
1084 break;
1085 }
1086
1087 switch (matchPairwiseReduction(EEI, ReduxOpCode, ReduxType)) {
1088 case RK_Arithmetic:
1089 return getArithmeticReductionCost(ReduxOpCode, ReduxType,
1090 /*IsPairwiseForm=*/true);
1091 case RK_MinMax:
1092 return getMinMaxReductionCost(
1093 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1094 /*IsPairwiseForm=*/true, /*IsUnsigned=*/false);
1095 case RK_UnsignedMinMax:
1096 return getMinMaxReductionCost(
1097 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1098 /*IsPairwiseForm=*/true, /*IsUnsigned=*/true);
1099 case RK_None:
1100 break;
1101 }
1102
1103 return getVectorInstrCost(I->getOpcode(),
1104 EEI->getOperand(0)->getType(), Idx);
1105 }
1106 case Instruction::InsertElement: {
1107 const InsertElementInst * IE = cast<InsertElementInst>(I);
1108 ConstantInt *CI = dyn_cast<ConstantInt>(IE->getOperand(2));
Fangrui Songf78650a2018-07-30 19:41:25 +00001109 unsigned Idx = -1;
Guozhi Wei62d64142017-09-08 22:29:17 +00001110 if (CI)
1111 Idx = CI->getZExtValue();
1112 return getVectorInstrCost(I->getOpcode(),
1113 IE->getType(), Idx);
1114 }
1115 case Instruction::ShuffleVector: {
1116 const ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
Simon Pilgrimd0c71602018-11-09 16:28:19 +00001117 Type *Ty = Shuffle->getType();
1118 Type *SrcTy = Shuffle->getOperand(0)->getType();
1119
1120 // TODO: Identify and add costs for insert subvector, etc.
1121 int SubIndex;
1122 if (Shuffle->isExtractSubvectorMask(SubIndex))
Simon Pilgrim26e1c882018-11-09 18:30:59 +00001123 return TTIImpl->getShuffleCost(SK_ExtractSubvector, SrcTy, SubIndex, Ty);
Simon Pilgrimd0c71602018-11-09 16:28:19 +00001124
Sanjay Patel2ca33602018-06-19 18:44:00 +00001125 if (Shuffle->changesLength())
1126 return -1;
Fangrui Songf78650a2018-07-30 19:41:25 +00001127
Sanjay Patel2ca33602018-06-19 18:44:00 +00001128 if (Shuffle->isIdentity())
1129 return 0;
Guozhi Wei62d64142017-09-08 22:29:17 +00001130
Sanjay Patel2ca33602018-06-19 18:44:00 +00001131 if (Shuffle->isReverse())
1132 return TTIImpl->getShuffleCost(SK_Reverse, Ty, 0, nullptr);
Simon Pilgrim07839212018-06-12 14:47:13 +00001133
Sanjay Patel2ca33602018-06-19 18:44:00 +00001134 if (Shuffle->isSelect())
1135 return TTIImpl->getShuffleCost(SK_Select, Ty, 0, nullptr);
Simon Pilgrim07839212018-06-12 14:47:13 +00001136
Sanjay Patel2ca33602018-06-19 18:44:00 +00001137 if (Shuffle->isTranspose())
1138 return TTIImpl->getShuffleCost(SK_Transpose, Ty, 0, nullptr);
Matthew Simpsonb4096eb2018-04-26 13:48:33 +00001139
Sanjay Patel2ca33602018-06-19 18:44:00 +00001140 if (Shuffle->isZeroEltSplat())
1141 return TTIImpl->getShuffleCost(SK_Broadcast, Ty, 0, nullptr);
Guozhi Wei62d64142017-09-08 22:29:17 +00001142
Sanjay Patel2ca33602018-06-19 18:44:00 +00001143 if (Shuffle->isSingleSource())
1144 return TTIImpl->getShuffleCost(SK_PermuteSingleSrc, Ty, 0, nullptr);
Guozhi Wei62d64142017-09-08 22:29:17 +00001145
Sanjay Patel2ca33602018-06-19 18:44:00 +00001146 return TTIImpl->getShuffleCost(SK_PermuteTwoSrc, Ty, 0, nullptr);
Guozhi Wei62d64142017-09-08 22:29:17 +00001147 }
1148 case Instruction::Call:
1149 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1150 SmallVector<Value *, 4> Args(II->arg_operands());
1151
1152 FastMathFlags FMF;
1153 if (auto *FPMO = dyn_cast<FPMathOperator>(II))
1154 FMF = FPMO->getFastMathFlags();
1155
1156 return getIntrinsicInstrCost(II->getIntrinsicID(), II->getType(),
1157 Args, FMF);
1158 }
1159 return -1;
1160 default:
1161 // We don't have any information on this instruction.
1162 return -1;
1163 }
1164}
1165
Chandler Carruth705b1852015-01-31 03:43:40 +00001166TargetTransformInfo::Concept::~Concept() {}
1167
Chandler Carruthe0385522015-02-01 10:11:22 +00001168TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
1169
1170TargetIRAnalysis::TargetIRAnalysis(
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001171 std::function<Result(const Function &)> TTICallback)
Benjamin Kramer82de7d32016-05-27 14:27:24 +00001172 : TTICallback(std::move(TTICallback)) {}
Chandler Carruthe0385522015-02-01 10:11:22 +00001173
Chandler Carruth164a2aa62016-06-17 00:11:01 +00001174TargetIRAnalysis::Result TargetIRAnalysis::run(const Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +00001175 FunctionAnalysisManager &) {
Chandler Carruthe0385522015-02-01 10:11:22 +00001176 return TTICallback(F);
1177}
1178
Chandler Carruthdab4eae2016-11-23 17:53:26 +00001179AnalysisKey TargetIRAnalysis::Key;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +00001180
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001181TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(const Function &F) {
Mehdi Amini5010ebf2015-07-09 02:08:42 +00001182 return Result(F.getParent()->getDataLayout());
Chandler Carruthe0385522015-02-01 10:11:22 +00001183}
1184
Chandler Carruth705b1852015-01-31 03:43:40 +00001185// Register the basic pass.
1186INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
1187 "Target Transform Information", false, true)
1188char TargetTransformInfoWrapperPass::ID = 0;
Chandler Carruth539edf42013-01-05 11:43:11 +00001189
Chandler Carruth705b1852015-01-31 03:43:40 +00001190void TargetTransformInfoWrapperPass::anchor() {}
Chandler Carruth539edf42013-01-05 11:43:11 +00001191
Chandler Carruth705b1852015-01-31 03:43:40 +00001192TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass()
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001193 : ImmutablePass(ID) {
Chandler Carruth705b1852015-01-31 03:43:40 +00001194 initializeTargetTransformInfoWrapperPassPass(
1195 *PassRegistry::getPassRegistry());
1196}
1197
1198TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass(
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001199 TargetIRAnalysis TIRA)
1200 : ImmutablePass(ID), TIRA(std::move(TIRA)) {
Chandler Carruth705b1852015-01-31 03:43:40 +00001201 initializeTargetTransformInfoWrapperPassPass(
1202 *PassRegistry::getPassRegistry());
1203}
1204
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001205TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(const Function &F) {
Sean Silva36e0d012016-08-09 00:28:15 +00001206 FunctionAnalysisManager DummyFAM;
Chandler Carruth164a2aa62016-06-17 00:11:01 +00001207 TTI = TIRA.run(F, DummyFAM);
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001208 return *TTI;
1209}
1210
Chandler Carruth93dcdc42015-01-31 11:17:59 +00001211ImmutablePass *
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001212llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) {
1213 return new TargetTransformInfoWrapperPass(std::move(TIRA));
Chandler Carruth539edf42013-01-05 11:43:11 +00001214}