blob: a6a1a872b658d08c7d101a748706944aec65e615 [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
392TargetTransformInfo::getOperandInfo(Value *V,
393 OperandValueProperties &OpProps) const {
394 OperandValueKind OpInfo = OK_AnyValue;
395 OpProps = OP_None;
396
397 if (auto *CI = dyn_cast<ConstantInt>(V)) {
398 if (CI->getValue().isPowerOf2())
399 OpProps = OP_PowerOf2;
400 return OK_UniformConstantValue;
401 }
402
403 const Value *Splat = getSplatValue(V);
404
405 // Check for a splat of a constant or for a non uniform vector of constants
406 // and check if the constant(s) are all powers of two.
407 if (isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) {
408 OpInfo = OK_NonUniformConstantValue;
409 if (Splat) {
410 OpInfo = OK_UniformConstantValue;
411 if (auto *CI = dyn_cast<ConstantInt>(Splat))
412 if (CI->getValue().isPowerOf2())
413 OpProps = OP_PowerOf2;
414 } else if (auto *CDS = dyn_cast<ConstantDataSequential>(V)) {
415 OpProps = OP_PowerOf2;
416 for (unsigned I = 0, E = CDS->getNumElements(); I != E; ++I) {
417 if (auto *CI = dyn_cast<ConstantInt>(CDS->getElementAsConstant(I)))
418 if (CI->getValue().isPowerOf2())
419 continue;
420 OpProps = OP_None;
421 break;
422 }
423 }
424 }
425
426 // Check for a splat of a uniform value. This is not loop aware, so return
427 // true only for the obviously uniform cases (argument, globalvalue)
428 if (Splat && (isa<Argument>(Splat) || isa<GlobalValue>(Splat)))
429 OpInfo = OK_UniformValue;
430
431 return OpInfo;
432}
433
Chandler Carruth93205eb2015-08-05 18:08:10 +0000434int TargetTransformInfo::getArithmeticInstrCost(
Chandler Carruth705b1852015-01-31 03:43:40 +0000435 unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
436 OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000437 OperandValueProperties Opd2PropInfo,
438 ArrayRef<const Value *> Args) const {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000439 int Cost = TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000440 Opd1PropInfo, Opd2PropInfo, Args);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000441 assert(Cost >= 0 && "TTI should not produce negative costs!");
442 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000443}
444
Chandler Carruth93205eb2015-08-05 18:08:10 +0000445int TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty, int Index,
446 Type *SubTp) const {
447 int Cost = TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
448 assert(Cost >= 0 && "TTI should not produce negative costs!");
449 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000450}
451
Chandler Carruth93205eb2015-08-05 18:08:10 +0000452int TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000453 Type *Src, const Instruction *I) const {
454 assert ((I == nullptr || I->getOpcode() == Opcode) &&
455 "Opcode should reflect passed instruction.");
456 int Cost = TTIImpl->getCastInstrCost(Opcode, Dst, Src, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000457 assert(Cost >= 0 && "TTI should not produce negative costs!");
458 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000459}
460
Matthew Simpsone5dfb082016-04-27 15:20:21 +0000461int TargetTransformInfo::getExtractWithExtendCost(unsigned Opcode, Type *Dst,
462 VectorType *VecTy,
463 unsigned Index) const {
464 int Cost = TTIImpl->getExtractWithExtendCost(Opcode, Dst, VecTy, Index);
465 assert(Cost >= 0 && "TTI should not produce negative costs!");
466 return Cost;
467}
468
Chandler Carruth93205eb2015-08-05 18:08:10 +0000469int TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
470 int Cost = TTIImpl->getCFInstrCost(Opcode);
471 assert(Cost >= 0 && "TTI should not produce negative costs!");
472 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000473}
474
Chandler Carruth93205eb2015-08-05 18:08:10 +0000475int TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000476 Type *CondTy, const Instruction *I) const {
477 assert ((I == nullptr || I->getOpcode() == Opcode) &&
478 "Opcode should reflect passed instruction.");
479 int Cost = TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000480 assert(Cost >= 0 && "TTI should not produce negative costs!");
481 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000482}
483
Chandler Carruth93205eb2015-08-05 18:08:10 +0000484int TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
485 unsigned Index) const {
486 int Cost = TTIImpl->getVectorInstrCost(Opcode, Val, Index);
487 assert(Cost >= 0 && "TTI should not produce negative costs!");
488 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000489}
490
Chandler Carruth93205eb2015-08-05 18:08:10 +0000491int TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
492 unsigned Alignment,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000493 unsigned AddressSpace,
494 const Instruction *I) const {
495 assert ((I == nullptr || I->getOpcode() == Opcode) &&
496 "Opcode should reflect passed instruction.");
497 int Cost = TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000498 assert(Cost >= 0 && "TTI should not produce negative costs!");
499 return Cost;
Elena Demikhovskya3232f72015-01-25 08:44:46 +0000500}
501
Chandler Carruth93205eb2015-08-05 18:08:10 +0000502int TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
503 unsigned Alignment,
504 unsigned AddressSpace) const {
505 int Cost =
506 TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
507 assert(Cost >= 0 && "TTI should not produce negative costs!");
508 return Cost;
Chandler Carruth705b1852015-01-31 03:43:40 +0000509}
510
Elena Demikhovsky54946982015-12-28 20:10:59 +0000511int TargetTransformInfo::getGatherScatterOpCost(unsigned Opcode, Type *DataTy,
512 Value *Ptr, bool VariableMask,
513 unsigned Alignment) const {
514 int Cost = TTIImpl->getGatherScatterOpCost(Opcode, DataTy, Ptr, VariableMask,
515 Alignment);
516 assert(Cost >= 0 && "TTI should not produce negative costs!");
517 return Cost;
518}
519
Chandler Carruth93205eb2015-08-05 18:08:10 +0000520int TargetTransformInfo::getInterleavedMemoryOpCost(
Hao Liu32c05392015-06-08 06:39:56 +0000521 unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
Dorit Nuzman34da6dd2018-10-31 09:57:56 +0000522 unsigned Alignment, unsigned AddressSpace, bool UseMaskForCond,
523 bool UseMaskForGaps) const {
524 int Cost = TTIImpl->getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
525 Alignment, AddressSpace,
526 UseMaskForCond,
527 UseMaskForGaps);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000528 assert(Cost >= 0 && "TTI should not produce negative costs!");
529 return Cost;
Hao Liu32c05392015-06-08 06:39:56 +0000530}
531
Chandler Carruth93205eb2015-08-05 18:08:10 +0000532int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
Jonas Paulssona48ea232017-03-14 06:35:36 +0000533 ArrayRef<Type *> Tys, FastMathFlags FMF,
534 unsigned ScalarizationCostPassed) const {
535 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys, FMF,
536 ScalarizationCostPassed);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000537 assert(Cost >= 0 && "TTI should not produce negative costs!");
538 return Cost;
539}
540
Elena Demikhovsky54946982015-12-28 20:10:59 +0000541int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
Jonas Paulssona48ea232017-03-14 06:35:36 +0000542 ArrayRef<Value *> Args, FastMathFlags FMF, unsigned VF) const {
543 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Args, FMF, VF);
Elena Demikhovsky54946982015-12-28 20:10:59 +0000544 assert(Cost >= 0 && "TTI should not produce negative costs!");
545 return Cost;
546}
547
Chandler Carruth93205eb2015-08-05 18:08:10 +0000548int TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy,
549 ArrayRef<Type *> Tys) const {
550 int Cost = TTIImpl->getCallInstrCost(F, RetTy, Tys);
551 assert(Cost >= 0 && "TTI should not produce negative costs!");
552 return Cost;
Michael Zolotukhin7ed84a82015-03-17 19:26:23 +0000553}
554
Chandler Carruth539edf42013-01-05 11:43:11 +0000555unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000556 return TTIImpl->getNumberOfParts(Tp);
Chandler Carruth539edf42013-01-05 11:43:11 +0000557}
558
Chandler Carruth93205eb2015-08-05 18:08:10 +0000559int TargetTransformInfo::getAddressComputationCost(Type *Tp,
Mohammed Agabaria23599ba2017-01-05 14:03:41 +0000560 ScalarEvolution *SE,
561 const SCEV *Ptr) const {
562 int Cost = TTIImpl->getAddressComputationCost(Tp, SE, Ptr);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000563 assert(Cost >= 0 && "TTI should not produce negative costs!");
564 return Cost;
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000565}
Chandler Carruth539edf42013-01-05 11:43:11 +0000566
Alexey Bataev3e9b3eb2017-07-31 14:19:32 +0000567int TargetTransformInfo::getArithmeticReductionCost(unsigned Opcode, Type *Ty,
568 bool IsPairwiseForm) const {
569 int Cost = TTIImpl->getArithmeticReductionCost(Opcode, Ty, IsPairwiseForm);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000570 assert(Cost >= 0 && "TTI should not produce negative costs!");
571 return Cost;
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000572}
573
Alexey Bataev6dd29fc2017-09-08 13:49:36 +0000574int TargetTransformInfo::getMinMaxReductionCost(Type *Ty, Type *CondTy,
575 bool IsPairwiseForm,
576 bool IsUnsigned) const {
577 int Cost =
578 TTIImpl->getMinMaxReductionCost(Ty, CondTy, IsPairwiseForm, IsUnsigned);
579 assert(Cost >= 0 && "TTI should not produce negative costs!");
580 return Cost;
581}
582
Chandler Carruth705b1852015-01-31 03:43:40 +0000583unsigned
584TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
585 return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
Chad Rosierf9327d62015-01-26 22:51:15 +0000586}
587
588bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst,
589 MemIntrinsicInfo &Info) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000590 return TTIImpl->getTgtMemIntrinsic(Inst, Info);
Chad Rosierf9327d62015-01-26 22:51:15 +0000591}
592
Anna Thomasb2a212c2017-06-06 16:45:25 +0000593unsigned TargetTransformInfo::getAtomicMemIntrinsicMaxElementSize() const {
594 return TTIImpl->getAtomicMemIntrinsicMaxElementSize();
595}
596
Chandler Carruth705b1852015-01-31 03:43:40 +0000597Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic(
598 IntrinsicInst *Inst, Type *ExpectedType) const {
599 return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
600}
601
Sean Fertile9cd1cdf2017-07-07 02:00:06 +0000602Type *TargetTransformInfo::getMemcpyLoopLoweringType(LLVMContext &Context,
603 Value *Length,
604 unsigned SrcAlign,
605 unsigned DestAlign) const {
606 return TTIImpl->getMemcpyLoopLoweringType(Context, Length, SrcAlign,
607 DestAlign);
608}
609
610void TargetTransformInfo::getMemcpyLoopResidualLoweringType(
611 SmallVectorImpl<Type *> &OpsOut, LLVMContext &Context,
612 unsigned RemainingBytes, unsigned SrcAlign, unsigned DestAlign) const {
613 TTIImpl->getMemcpyLoopResidualLoweringType(OpsOut, Context, RemainingBytes,
614 SrcAlign, DestAlign);
615}
616
Eric Christopherd566fb12015-07-29 22:09:48 +0000617bool TargetTransformInfo::areInlineCompatible(const Function *Caller,
618 const Function *Callee) const {
619 return TTIImpl->areInlineCompatible(Caller, Callee);
Eric Christopher4371b132015-07-02 01:11:47 +0000620}
621
Krzysztof Parzyszek0b377e02018-03-26 13:10:09 +0000622bool TargetTransformInfo::isIndexedLoadLegal(MemIndexedMode Mode,
623 Type *Ty) const {
624 return TTIImpl->isIndexedLoadLegal(Mode, Ty);
625}
626
627bool TargetTransformInfo::isIndexedStoreLegal(MemIndexedMode Mode,
628 Type *Ty) const {
629 return TTIImpl->isIndexedStoreLegal(Mode, Ty);
630}
631
Volkan Keles1c386812016-10-03 10:31:34 +0000632unsigned TargetTransformInfo::getLoadStoreVecRegBitWidth(unsigned AS) const {
633 return TTIImpl->getLoadStoreVecRegBitWidth(AS);
634}
635
636bool TargetTransformInfo::isLegalToVectorizeLoad(LoadInst *LI) const {
637 return TTIImpl->isLegalToVectorizeLoad(LI);
638}
639
640bool TargetTransformInfo::isLegalToVectorizeStore(StoreInst *SI) const {
641 return TTIImpl->isLegalToVectorizeStore(SI);
642}
643
644bool TargetTransformInfo::isLegalToVectorizeLoadChain(
645 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
646 return TTIImpl->isLegalToVectorizeLoadChain(ChainSizeInBytes, Alignment,
647 AddrSpace);
648}
649
650bool TargetTransformInfo::isLegalToVectorizeStoreChain(
651 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
652 return TTIImpl->isLegalToVectorizeStoreChain(ChainSizeInBytes, Alignment,
653 AddrSpace);
654}
655
656unsigned TargetTransformInfo::getLoadVectorFactor(unsigned VF,
657 unsigned LoadSize,
658 unsigned ChainSizeInBytes,
659 VectorType *VecTy) const {
660 return TTIImpl->getLoadVectorFactor(VF, LoadSize, ChainSizeInBytes, VecTy);
661}
662
663unsigned TargetTransformInfo::getStoreVectorFactor(unsigned VF,
664 unsigned StoreSize,
665 unsigned ChainSizeInBytes,
666 VectorType *VecTy) const {
667 return TTIImpl->getStoreVectorFactor(VF, StoreSize, ChainSizeInBytes, VecTy);
668}
669
Amara Emersoncf9daa32017-05-09 10:43:25 +0000670bool TargetTransformInfo::useReductionIntrinsic(unsigned Opcode,
671 Type *Ty, ReductionFlags Flags) const {
672 return TTIImpl->useReductionIntrinsic(Opcode, Ty, Flags);
673}
674
Amara Emerson836b0f42017-05-10 09:42:49 +0000675bool TargetTransformInfo::shouldExpandReduction(const IntrinsicInst *II) const {
676 return TTIImpl->shouldExpandReduction(II);
677}
Amara Emersoncf9daa32017-05-09 10:43:25 +0000678
Guozhi Wei62d64142017-09-08 22:29:17 +0000679int TargetTransformInfo::getInstructionLatency(const Instruction *I) const {
680 return TTIImpl->getInstructionLatency(I);
681}
682
Guozhi Wei62d64142017-09-08 22:29:17 +0000683static bool matchPairwiseShuffleMask(ShuffleVectorInst *SI, bool IsLeft,
684 unsigned Level) {
685 // We don't need a shuffle if we just want to have element 0 in position 0 of
686 // the vector.
687 if (!SI && Level == 0 && IsLeft)
688 return true;
689 else if (!SI)
690 return false;
691
692 SmallVector<int, 32> Mask(SI->getType()->getVectorNumElements(), -1);
693
694 // Build a mask of 0, 2, ... (left) or 1, 3, ... (right) depending on whether
695 // we look at the left or right side.
696 for (unsigned i = 0, e = (1 << Level), val = !IsLeft; i != e; ++i, val += 2)
697 Mask[i] = val;
698
699 SmallVector<int, 16> ActualMask = SI->getShuffleMask();
700 return Mask == ActualMask;
701}
702
703namespace {
704/// Kind of the reduction data.
705enum ReductionKind {
706 RK_None, /// Not a reduction.
707 RK_Arithmetic, /// Binary reduction data.
708 RK_MinMax, /// Min/max reduction data.
709 RK_UnsignedMinMax, /// Unsigned min/max reduction data.
710};
711/// Contains opcode + LHS/RHS parts of the reduction operations.
712struct ReductionData {
713 ReductionData() = delete;
714 ReductionData(ReductionKind Kind, unsigned Opcode, Value *LHS, Value *RHS)
715 : Opcode(Opcode), LHS(LHS), RHS(RHS), Kind(Kind) {
716 assert(Kind != RK_None && "expected binary or min/max reduction only.");
717 }
718 unsigned Opcode = 0;
719 Value *LHS = nullptr;
720 Value *RHS = nullptr;
721 ReductionKind Kind = RK_None;
722 bool hasSameData(ReductionData &RD) const {
723 return Kind == RD.Kind && Opcode == RD.Opcode;
724 }
725};
726} // namespace
727
728static Optional<ReductionData> getReductionData(Instruction *I) {
729 Value *L, *R;
730 if (m_BinOp(m_Value(L), m_Value(R)).match(I))
Fangrui Songf78650a2018-07-30 19:41:25 +0000731 return ReductionData(RK_Arithmetic, I->getOpcode(), L, R);
Guozhi Wei62d64142017-09-08 22:29:17 +0000732 if (auto *SI = dyn_cast<SelectInst>(I)) {
733 if (m_SMin(m_Value(L), m_Value(R)).match(SI) ||
734 m_SMax(m_Value(L), m_Value(R)).match(SI) ||
735 m_OrdFMin(m_Value(L), m_Value(R)).match(SI) ||
736 m_OrdFMax(m_Value(L), m_Value(R)).match(SI) ||
737 m_UnordFMin(m_Value(L), m_Value(R)).match(SI) ||
738 m_UnordFMax(m_Value(L), m_Value(R)).match(SI)) {
739 auto *CI = cast<CmpInst>(SI->getCondition());
Fangrui Songf78650a2018-07-30 19:41:25 +0000740 return ReductionData(RK_MinMax, CI->getOpcode(), L, R);
741 }
Guozhi Wei62d64142017-09-08 22:29:17 +0000742 if (m_UMin(m_Value(L), m_Value(R)).match(SI) ||
743 m_UMax(m_Value(L), m_Value(R)).match(SI)) {
744 auto *CI = cast<CmpInst>(SI->getCondition());
745 return ReductionData(RK_UnsignedMinMax, CI->getOpcode(), L, R);
746 }
747 }
748 return llvm::None;
749}
750
751static ReductionKind matchPairwiseReductionAtLevel(Instruction *I,
752 unsigned Level,
753 unsigned NumLevels) {
754 // Match one level of pairwise operations.
755 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef,
756 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef>
757 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef,
758 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef>
759 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1
760 if (!I)
761 return RK_None;
762
763 assert(I->getType()->isVectorTy() && "Expecting a vector type");
764
765 Optional<ReductionData> RD = getReductionData(I);
766 if (!RD)
767 return RK_None;
768
769 ShuffleVectorInst *LS = dyn_cast<ShuffleVectorInst>(RD->LHS);
770 if (!LS && Level)
771 return RK_None;
772 ShuffleVectorInst *RS = dyn_cast<ShuffleVectorInst>(RD->RHS);
773 if (!RS && Level)
774 return RK_None;
775
776 // On level 0 we can omit one shufflevector instruction.
777 if (!Level && !RS && !LS)
778 return RK_None;
779
780 // Shuffle inputs must match.
781 Value *NextLevelOpL = LS ? LS->getOperand(0) : nullptr;
782 Value *NextLevelOpR = RS ? RS->getOperand(0) : nullptr;
783 Value *NextLevelOp = nullptr;
784 if (NextLevelOpR && NextLevelOpL) {
785 // If we have two shuffles their operands must match.
786 if (NextLevelOpL != NextLevelOpR)
787 return RK_None;
788
789 NextLevelOp = NextLevelOpL;
790 } else if (Level == 0 && (NextLevelOpR || NextLevelOpL)) {
791 // On the first level we can omit the shufflevector <0, undef,...>. So the
792 // input to the other shufflevector <1, undef> must match with one of the
793 // inputs to the current binary operation.
794 // Example:
795 // %NextLevelOpL = shufflevector %R, <1, undef ...>
796 // %BinOp = fadd %NextLevelOpL, %R
797 if (NextLevelOpL && NextLevelOpL != RD->RHS)
798 return RK_None;
799 else if (NextLevelOpR && NextLevelOpR != RD->LHS)
800 return RK_None;
801
802 NextLevelOp = NextLevelOpL ? RD->RHS : RD->LHS;
803 } else
804 return RK_None;
805
806 // Check that the next levels binary operation exists and matches with the
807 // current one.
808 if (Level + 1 != NumLevels) {
809 Optional<ReductionData> NextLevelRD =
810 getReductionData(cast<Instruction>(NextLevelOp));
811 if (!NextLevelRD || !RD->hasSameData(*NextLevelRD))
812 return RK_None;
813 }
814
815 // Shuffle mask for pairwise operation must match.
816 if (matchPairwiseShuffleMask(LS, /*IsLeft=*/true, Level)) {
817 if (!matchPairwiseShuffleMask(RS, /*IsLeft=*/false, Level))
818 return RK_None;
819 } else if (matchPairwiseShuffleMask(RS, /*IsLeft=*/true, Level)) {
820 if (!matchPairwiseShuffleMask(LS, /*IsLeft=*/false, Level))
821 return RK_None;
822 } else {
823 return RK_None;
824 }
825
826 if (++Level == NumLevels)
827 return RD->Kind;
828
829 // Match next level.
830 return matchPairwiseReductionAtLevel(cast<Instruction>(NextLevelOp), Level,
831 NumLevels);
832}
833
834static ReductionKind matchPairwiseReduction(const ExtractElementInst *ReduxRoot,
835 unsigned &Opcode, Type *&Ty) {
836 if (!EnableReduxCost)
837 return RK_None;
838
839 // Need to extract the first element.
840 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1));
841 unsigned Idx = ~0u;
842 if (CI)
843 Idx = CI->getZExtValue();
844 if (Idx != 0)
845 return RK_None;
846
847 auto *RdxStart = dyn_cast<Instruction>(ReduxRoot->getOperand(0));
848 if (!RdxStart)
849 return RK_None;
850 Optional<ReductionData> RD = getReductionData(RdxStart);
851 if (!RD)
852 return RK_None;
853
854 Type *VecTy = RdxStart->getType();
855 unsigned NumVecElems = VecTy->getVectorNumElements();
856 if (!isPowerOf2_32(NumVecElems))
857 return RK_None;
858
859 // We look for a sequence of shuffle,shuffle,add triples like the following
860 // that builds a pairwise reduction tree.
Fangrui Songf78650a2018-07-30 19:41:25 +0000861 //
Guozhi Wei62d64142017-09-08 22:29:17 +0000862 // (X0, X1, X2, X3)
863 // (X0 + X1, X2 + X3, undef, undef)
864 // ((X0 + X1) + (X2 + X3), undef, undef, undef)
Fangrui Songf78650a2018-07-30 19:41:25 +0000865 //
Guozhi Wei62d64142017-09-08 22:29:17 +0000866 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef,
867 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef>
868 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef,
869 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef>
870 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1
871 // %rdx.shuf.1.0 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef,
872 // <4 x i32> <i32 0, i32 undef, i32 undef, i32 undef>
873 // %rdx.shuf.1.1 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef,
874 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
875 // %bin.rdx8 = fadd <4 x float> %rdx.shuf.1.0, %rdx.shuf.1.1
876 // %r = extractelement <4 x float> %bin.rdx8, i32 0
877 if (matchPairwiseReductionAtLevel(RdxStart, 0, Log2_32(NumVecElems)) ==
878 RK_None)
879 return RK_None;
880
881 Opcode = RD->Opcode;
882 Ty = VecTy;
883
884 return RD->Kind;
885}
886
887static std::pair<Value *, ShuffleVectorInst *>
888getShuffleAndOtherOprd(Value *L, Value *R) {
889 ShuffleVectorInst *S = nullptr;
890
891 if ((S = dyn_cast<ShuffleVectorInst>(L)))
892 return std::make_pair(R, S);
893
894 S = dyn_cast<ShuffleVectorInst>(R);
895 return std::make_pair(L, S);
896}
897
898static ReductionKind
899matchVectorSplittingReduction(const ExtractElementInst *ReduxRoot,
900 unsigned &Opcode, Type *&Ty) {
901 if (!EnableReduxCost)
902 return RK_None;
903
904 // Need to extract the first element.
905 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1));
906 unsigned Idx = ~0u;
907 if (CI)
908 Idx = CI->getZExtValue();
909 if (Idx != 0)
910 return RK_None;
911
912 auto *RdxStart = dyn_cast<Instruction>(ReduxRoot->getOperand(0));
913 if (!RdxStart)
914 return RK_None;
915 Optional<ReductionData> RD = getReductionData(RdxStart);
916 if (!RD)
917 return RK_None;
918
919 Type *VecTy = ReduxRoot->getOperand(0)->getType();
920 unsigned NumVecElems = VecTy->getVectorNumElements();
921 if (!isPowerOf2_32(NumVecElems))
922 return RK_None;
923
924 // We look for a sequence of shuffles and adds like the following matching one
925 // fadd, shuffle vector pair at a time.
Fangrui Songf78650a2018-07-30 19:41:25 +0000926 //
Guozhi Wei62d64142017-09-08 22:29:17 +0000927 // %rdx.shuf = shufflevector <4 x float> %rdx, <4 x float> undef,
928 // <4 x i32> <i32 2, i32 3, i32 undef, i32 undef>
929 // %bin.rdx = fadd <4 x float> %rdx, %rdx.shuf
930 // %rdx.shuf7 = shufflevector <4 x float> %bin.rdx, <4 x float> undef,
931 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
932 // %bin.rdx8 = fadd <4 x float> %bin.rdx, %rdx.shuf7
933 // %r = extractelement <4 x float> %bin.rdx8, i32 0
934
935 unsigned MaskStart = 1;
936 Instruction *RdxOp = RdxStart;
Fangrui Songf78650a2018-07-30 19:41:25 +0000937 SmallVector<int, 32> ShuffleMask(NumVecElems, 0);
Guozhi Wei62d64142017-09-08 22:29:17 +0000938 unsigned NumVecElemsRemain = NumVecElems;
939 while (NumVecElemsRemain - 1) {
940 // Check for the right reduction operation.
941 if (!RdxOp)
942 return RK_None;
943 Optional<ReductionData> RDLevel = getReductionData(RdxOp);
944 if (!RDLevel || !RDLevel->hasSameData(*RD))
945 return RK_None;
946
947 Value *NextRdxOp;
948 ShuffleVectorInst *Shuffle;
949 std::tie(NextRdxOp, Shuffle) =
950 getShuffleAndOtherOprd(RDLevel->LHS, RDLevel->RHS);
951
952 // Check the current reduction operation and the shuffle use the same value.
953 if (Shuffle == nullptr)
954 return RK_None;
955 if (Shuffle->getOperand(0) != NextRdxOp)
956 return RK_None;
957
958 // Check that shuffle masks matches.
959 for (unsigned j = 0; j != MaskStart; ++j)
960 ShuffleMask[j] = MaskStart + j;
961 // Fill the rest of the mask with -1 for undef.
962 std::fill(&ShuffleMask[MaskStart], ShuffleMask.end(), -1);
963
964 SmallVector<int, 16> Mask = Shuffle->getShuffleMask();
965 if (ShuffleMask != Mask)
966 return RK_None;
967
968 RdxOp = dyn_cast<Instruction>(NextRdxOp);
969 NumVecElemsRemain /= 2;
970 MaskStart *= 2;
971 }
972
973 Opcode = RD->Opcode;
974 Ty = VecTy;
975 return RD->Kind;
976}
977
978int TargetTransformInfo::getInstructionThroughput(const Instruction *I) const {
979 switch (I->getOpcode()) {
980 case Instruction::GetElementPtr:
981 return getUserCost(I);
982
983 case Instruction::Ret:
984 case Instruction::PHI:
985 case Instruction::Br: {
986 return getCFInstrCost(I->getOpcode());
987 }
988 case Instruction::Add:
989 case Instruction::FAdd:
990 case Instruction::Sub:
991 case Instruction::FSub:
992 case Instruction::Mul:
993 case Instruction::FMul:
994 case Instruction::UDiv:
995 case Instruction::SDiv:
996 case Instruction::FDiv:
997 case Instruction::URem:
998 case Instruction::SRem:
999 case Instruction::FRem:
1000 case Instruction::Shl:
1001 case Instruction::LShr:
1002 case Instruction::AShr:
1003 case Instruction::And:
1004 case Instruction::Or:
1005 case Instruction::Xor: {
Simon Pilgrim4162d772018-05-22 10:40:09 +00001006 TargetTransformInfo::OperandValueKind Op1VK, Op2VK;
1007 TargetTransformInfo::OperandValueProperties Op1VP, Op2VP;
1008 Op1VK = getOperandInfo(I->getOperand(0), Op1VP);
1009 Op2VK = getOperandInfo(I->getOperand(1), Op2VP);
1010 SmallVector<const Value *, 2> Operands(I->operand_values());
1011 return getArithmeticInstrCost(I->getOpcode(), I->getType(), Op1VK, Op2VK,
1012 Op1VP, Op2VP, Operands);
Guozhi Wei62d64142017-09-08 22:29:17 +00001013 }
1014 case Instruction::Select: {
1015 const SelectInst *SI = cast<SelectInst>(I);
1016 Type *CondTy = SI->getCondition()->getType();
1017 return getCmpSelInstrCost(I->getOpcode(), I->getType(), CondTy, I);
1018 }
1019 case Instruction::ICmp:
1020 case Instruction::FCmp: {
1021 Type *ValTy = I->getOperand(0)->getType();
1022 return getCmpSelInstrCost(I->getOpcode(), ValTy, I->getType(), I);
1023 }
1024 case Instruction::Store: {
1025 const StoreInst *SI = cast<StoreInst>(I);
1026 Type *ValTy = SI->getValueOperand()->getType();
1027 return getMemoryOpCost(I->getOpcode(), ValTy,
1028 SI->getAlignment(),
1029 SI->getPointerAddressSpace(), I);
1030 }
1031 case Instruction::Load: {
1032 const LoadInst *LI = cast<LoadInst>(I);
1033 return getMemoryOpCost(I->getOpcode(), I->getType(),
1034 LI->getAlignment(),
1035 LI->getPointerAddressSpace(), I);
1036 }
1037 case Instruction::ZExt:
1038 case Instruction::SExt:
1039 case Instruction::FPToUI:
1040 case Instruction::FPToSI:
1041 case Instruction::FPExt:
1042 case Instruction::PtrToInt:
1043 case Instruction::IntToPtr:
1044 case Instruction::SIToFP:
1045 case Instruction::UIToFP:
1046 case Instruction::Trunc:
1047 case Instruction::FPTrunc:
1048 case Instruction::BitCast:
1049 case Instruction::AddrSpaceCast: {
1050 Type *SrcTy = I->getOperand(0)->getType();
1051 return getCastInstrCost(I->getOpcode(), I->getType(), SrcTy, I);
1052 }
1053 case Instruction::ExtractElement: {
1054 const ExtractElementInst * EEI = cast<ExtractElementInst>(I);
1055 ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1));
1056 unsigned Idx = -1;
1057 if (CI)
1058 Idx = CI->getZExtValue();
1059
1060 // Try to match a reduction sequence (series of shufflevector and vector
1061 // adds followed by a extractelement).
1062 unsigned ReduxOpCode;
1063 Type *ReduxType;
1064
1065 switch (matchVectorSplittingReduction(EEI, ReduxOpCode, ReduxType)) {
1066 case RK_Arithmetic:
1067 return getArithmeticReductionCost(ReduxOpCode, ReduxType,
1068 /*IsPairwiseForm=*/false);
1069 case RK_MinMax:
1070 return getMinMaxReductionCost(
1071 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1072 /*IsPairwiseForm=*/false, /*IsUnsigned=*/false);
1073 case RK_UnsignedMinMax:
1074 return getMinMaxReductionCost(
1075 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1076 /*IsPairwiseForm=*/false, /*IsUnsigned=*/true);
1077 case RK_None:
1078 break;
1079 }
1080
1081 switch (matchPairwiseReduction(EEI, ReduxOpCode, ReduxType)) {
1082 case RK_Arithmetic:
1083 return getArithmeticReductionCost(ReduxOpCode, ReduxType,
1084 /*IsPairwiseForm=*/true);
1085 case RK_MinMax:
1086 return getMinMaxReductionCost(
1087 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1088 /*IsPairwiseForm=*/true, /*IsUnsigned=*/false);
1089 case RK_UnsignedMinMax:
1090 return getMinMaxReductionCost(
1091 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1092 /*IsPairwiseForm=*/true, /*IsUnsigned=*/true);
1093 case RK_None:
1094 break;
1095 }
1096
1097 return getVectorInstrCost(I->getOpcode(),
1098 EEI->getOperand(0)->getType(), Idx);
1099 }
1100 case Instruction::InsertElement: {
1101 const InsertElementInst * IE = cast<InsertElementInst>(I);
1102 ConstantInt *CI = dyn_cast<ConstantInt>(IE->getOperand(2));
Fangrui Songf78650a2018-07-30 19:41:25 +00001103 unsigned Idx = -1;
Guozhi Wei62d64142017-09-08 22:29:17 +00001104 if (CI)
1105 Idx = CI->getZExtValue();
1106 return getVectorInstrCost(I->getOpcode(),
1107 IE->getType(), Idx);
1108 }
1109 case Instruction::ShuffleVector: {
1110 const ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
Simon Pilgrimd0c71602018-11-09 16:28:19 +00001111 Type *Ty = Shuffle->getType();
1112 Type *SrcTy = Shuffle->getOperand(0)->getType();
1113
1114 // TODO: Identify and add costs for insert subvector, etc.
1115 int SubIndex;
1116 if (Shuffle->isExtractSubvectorMask(SubIndex))
1117 return TTIImpl->getShuffleCost(SK_ExtractSubvector, Ty, SubIndex, SrcTy);
1118
Sanjay Patel2ca33602018-06-19 18:44:00 +00001119 if (Shuffle->changesLength())
1120 return -1;
Fangrui Songf78650a2018-07-30 19:41:25 +00001121
Sanjay Patel2ca33602018-06-19 18:44:00 +00001122 if (Shuffle->isIdentity())
1123 return 0;
Guozhi Wei62d64142017-09-08 22:29:17 +00001124
Sanjay Patel2ca33602018-06-19 18:44:00 +00001125 if (Shuffle->isReverse())
1126 return TTIImpl->getShuffleCost(SK_Reverse, Ty, 0, nullptr);
Simon Pilgrim07839212018-06-12 14:47:13 +00001127
Sanjay Patel2ca33602018-06-19 18:44:00 +00001128 if (Shuffle->isSelect())
1129 return TTIImpl->getShuffleCost(SK_Select, Ty, 0, nullptr);
Simon Pilgrim07839212018-06-12 14:47:13 +00001130
Sanjay Patel2ca33602018-06-19 18:44:00 +00001131 if (Shuffle->isTranspose())
1132 return TTIImpl->getShuffleCost(SK_Transpose, Ty, 0, nullptr);
Matthew Simpsonb4096eb2018-04-26 13:48:33 +00001133
Sanjay Patel2ca33602018-06-19 18:44:00 +00001134 if (Shuffle->isZeroEltSplat())
1135 return TTIImpl->getShuffleCost(SK_Broadcast, Ty, 0, nullptr);
Guozhi Wei62d64142017-09-08 22:29:17 +00001136
Sanjay Patel2ca33602018-06-19 18:44:00 +00001137 if (Shuffle->isSingleSource())
1138 return TTIImpl->getShuffleCost(SK_PermuteSingleSrc, Ty, 0, nullptr);
Guozhi Wei62d64142017-09-08 22:29:17 +00001139
Sanjay Patel2ca33602018-06-19 18:44:00 +00001140 return TTIImpl->getShuffleCost(SK_PermuteTwoSrc, Ty, 0, nullptr);
Guozhi Wei62d64142017-09-08 22:29:17 +00001141 }
1142 case Instruction::Call:
1143 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1144 SmallVector<Value *, 4> Args(II->arg_operands());
1145
1146 FastMathFlags FMF;
1147 if (auto *FPMO = dyn_cast<FPMathOperator>(II))
1148 FMF = FPMO->getFastMathFlags();
1149
1150 return getIntrinsicInstrCost(II->getIntrinsicID(), II->getType(),
1151 Args, FMF);
1152 }
1153 return -1;
1154 default:
1155 // We don't have any information on this instruction.
1156 return -1;
1157 }
1158}
1159
Chandler Carruth705b1852015-01-31 03:43:40 +00001160TargetTransformInfo::Concept::~Concept() {}
1161
Chandler Carruthe0385522015-02-01 10:11:22 +00001162TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
1163
1164TargetIRAnalysis::TargetIRAnalysis(
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001165 std::function<Result(const Function &)> TTICallback)
Benjamin Kramer82de7d32016-05-27 14:27:24 +00001166 : TTICallback(std::move(TTICallback)) {}
Chandler Carruthe0385522015-02-01 10:11:22 +00001167
Chandler Carruth164a2aa62016-06-17 00:11:01 +00001168TargetIRAnalysis::Result TargetIRAnalysis::run(const Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +00001169 FunctionAnalysisManager &) {
Chandler Carruthe0385522015-02-01 10:11:22 +00001170 return TTICallback(F);
1171}
1172
Chandler Carruthdab4eae2016-11-23 17:53:26 +00001173AnalysisKey TargetIRAnalysis::Key;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +00001174
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001175TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(const Function &F) {
Mehdi Amini5010ebf2015-07-09 02:08:42 +00001176 return Result(F.getParent()->getDataLayout());
Chandler Carruthe0385522015-02-01 10:11:22 +00001177}
1178
Chandler Carruth705b1852015-01-31 03:43:40 +00001179// Register the basic pass.
1180INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
1181 "Target Transform Information", false, true)
1182char TargetTransformInfoWrapperPass::ID = 0;
Chandler Carruth539edf42013-01-05 11:43:11 +00001183
Chandler Carruth705b1852015-01-31 03:43:40 +00001184void TargetTransformInfoWrapperPass::anchor() {}
Chandler Carruth539edf42013-01-05 11:43:11 +00001185
Chandler Carruth705b1852015-01-31 03:43:40 +00001186TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass()
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001187 : ImmutablePass(ID) {
Chandler Carruth705b1852015-01-31 03:43:40 +00001188 initializeTargetTransformInfoWrapperPassPass(
1189 *PassRegistry::getPassRegistry());
1190}
1191
1192TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass(
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001193 TargetIRAnalysis TIRA)
1194 : ImmutablePass(ID), TIRA(std::move(TIRA)) {
Chandler Carruth705b1852015-01-31 03:43:40 +00001195 initializeTargetTransformInfoWrapperPassPass(
1196 *PassRegistry::getPassRegistry());
1197}
1198
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001199TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(const Function &F) {
Sean Silva36e0d012016-08-09 00:28:15 +00001200 FunctionAnalysisManager DummyFAM;
Chandler Carruth164a2aa62016-06-17 00:11:01 +00001201 TTI = TIRA.run(F, DummyFAM);
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001202 return *TTI;
1203}
1204
Chandler Carruth93dcdc42015-01-31 11:17:59 +00001205ImmutablePass *
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001206llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) {
1207 return new TargetTransformInfoWrapperPass(std::move(TIRA));
Chandler Carruth539edf42013-01-05 11:43:11 +00001208}