blob: 3a7b3c4db6d12ea99842cec1c8ab927924a4ad0a [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 {
34/// \brief No-op implementation of the TTI interface using the utility base
35/// 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
Elena Demikhovsky20662e32015-10-19 07:43:38 +0000162bool TargetTransformInfo::isLegalMaskedStore(Type *DataType) const {
163 return TTIImpl->isLegalMaskedStore(DataType);
Chandler Carruth705b1852015-01-31 03:43:40 +0000164}
165
Elena Demikhovsky20662e32015-10-19 07:43:38 +0000166bool TargetTransformInfo::isLegalMaskedLoad(Type *DataType) const {
167 return TTIImpl->isLegalMaskedLoad(DataType);
Chandler Carruth705b1852015-01-31 03:43:40 +0000168}
169
Elena Demikhovsky09285852015-10-25 15:37:55 +0000170bool TargetTransformInfo::isLegalMaskedGather(Type *DataType) const {
171 return TTIImpl->isLegalMaskedGather(DataType);
172}
173
174bool TargetTransformInfo::isLegalMaskedScatter(Type *DataType) const {
Mohammed Agabariacef53dc2017-07-27 10:28:16 +0000175 return TTIImpl->isLegalMaskedScatter(DataType);
Elena Demikhovsky09285852015-10-25 15:37:55 +0000176}
177
Sanjay Patel6fd43912017-09-09 13:38:18 +0000178bool TargetTransformInfo::hasDivRemOp(Type *DataType, bool IsSigned) const {
179 return TTIImpl->hasDivRemOp(DataType, IsSigned);
180}
181
Artem Belevichcb8f6322017-10-24 20:31:44 +0000182bool TargetTransformInfo::hasVolatileVariant(Instruction *I,
183 unsigned AddrSpace) const {
184 return TTIImpl->hasVolatileVariant(I, AddrSpace);
185}
186
Jonas Paulsson8624b7e2017-05-24 13:42:56 +0000187bool TargetTransformInfo::prefersVectorizedAddressing() const {
188 return TTIImpl->prefersVectorizedAddressing();
189}
190
Quentin Colombetbf490d42013-05-31 21:29:03 +0000191int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
192 int64_t BaseOffset,
193 bool HasBaseReg,
Matt Arsenaulte83379e2015-06-07 20:12:03 +0000194 int64_t Scale,
195 unsigned AddrSpace) const {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000196 int Cost = TTIImpl->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg,
197 Scale, AddrSpace);
198 assert(Cost >= 0 && "TTI should not produce negative costs!");
199 return Cost;
Quentin Colombetbf490d42013-05-31 21:29:03 +0000200}
201
Jonas Paulsson024e3192017-07-21 11:59:37 +0000202bool TargetTransformInfo::LSRWithInstrQueries() const {
203 return TTIImpl->LSRWithInstrQueries();
204}
205
Chandler Carruth539edf42013-01-05 11:43:11 +0000206bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000207 return TTIImpl->isTruncateFree(Ty1, Ty2);
Chandler Carruth539edf42013-01-05 11:43:11 +0000208}
209
Chad Rosier54390052015-02-23 19:15:16 +0000210bool TargetTransformInfo::isProfitableToHoist(Instruction *I) const {
211 return TTIImpl->isProfitableToHoist(I);
212}
213
Chandler Carruth539edf42013-01-05 11:43:11 +0000214bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000215 return TTIImpl->isTypeLegal(Ty);
Chandler Carruth539edf42013-01-05 11:43:11 +0000216}
217
218unsigned TargetTransformInfo::getJumpBufAlignment() const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000219 return TTIImpl->getJumpBufAlignment();
Chandler Carruth539edf42013-01-05 11:43:11 +0000220}
221
222unsigned TargetTransformInfo::getJumpBufSize() const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000223 return TTIImpl->getJumpBufSize();
Chandler Carruth539edf42013-01-05 11:43:11 +0000224}
225
226bool TargetTransformInfo::shouldBuildLookupTables() const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000227 return TTIImpl->shouldBuildLookupTables();
Chandler Carruth539edf42013-01-05 11:43:11 +0000228}
Oliver Stannard4df1cc02016-10-07 08:48:24 +0000229bool TargetTransformInfo::shouldBuildLookupTablesForConstant(Constant *C) const {
230 return TTIImpl->shouldBuildLookupTablesForConstant(C);
231}
Chandler Carruth539edf42013-01-05 11:43:11 +0000232
Zaara Syeda1f59ae32018-01-30 16:17:22 +0000233bool TargetTransformInfo::useColdCCForColdCall(Function &F) const {
234 return TTIImpl->useColdCCForColdCall(F);
235}
236
Jonas Paulsson8e2f9482017-01-26 07:03:25 +0000237unsigned TargetTransformInfo::
238getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const {
239 return TTIImpl->getScalarizationOverhead(Ty, Insert, Extract);
240}
241
242unsigned TargetTransformInfo::
243getOperandsScalarizationOverhead(ArrayRef<const Value *> Args,
244 unsigned VF) const {
245 return TTIImpl->getOperandsScalarizationOverhead(Args, VF);
246}
247
Jonas Paulssonda74ed42017-04-12 12:41:37 +0000248bool TargetTransformInfo::supportsEfficientVectorElementLoadStore() const {
249 return TTIImpl->supportsEfficientVectorElementLoadStore();
250}
251
Olivier Sallenave049d8032015-03-06 23:12:04 +0000252bool TargetTransformInfo::enableAggressiveInterleaving(bool LoopHasReductions) const {
253 return TTIImpl->enableAggressiveInterleaving(LoopHasReductions);
254}
255
Clement Courbetb2c3eb82017-10-30 14:19:33 +0000256const TargetTransformInfo::MemCmpExpansionOptions *
257TargetTransformInfo::enableMemCmpExpansion(bool IsZeroCmp) const {
258 return TTIImpl->enableMemCmpExpansion(IsZeroCmp);
Zaara Syeda3a7578c2017-05-31 17:12:38 +0000259}
260
Silviu Baranga61bdc512015-08-10 14:50:54 +0000261bool TargetTransformInfo::enableInterleavedAccessVectorization() const {
262 return TTIImpl->enableInterleavedAccessVectorization();
263}
264
Renato Golin5cb666a2016-04-14 20:42:18 +0000265bool TargetTransformInfo::isFPVectorizationPotentiallyUnsafe() const {
266 return TTIImpl->isFPVectorizationPotentiallyUnsafe();
267}
268
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000269bool TargetTransformInfo::allowsMisalignedMemoryAccesses(LLVMContext &Context,
270 unsigned BitWidth,
Alina Sbirlea327955e2016-07-11 20:46:17 +0000271 unsigned AddressSpace,
272 unsigned Alignment,
273 bool *Fast) const {
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000274 return TTIImpl->allowsMisalignedMemoryAccesses(Context, BitWidth, AddressSpace,
Alina Sbirlea327955e2016-07-11 20:46:17 +0000275 Alignment, Fast);
276}
277
Chandler Carruth50a36cd2013-01-07 03:16:03 +0000278TargetTransformInfo::PopcntSupportKind
279TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000280 return TTIImpl->getPopcntSupport(IntTyWidthInBit);
Chandler Carruth539edf42013-01-05 11:43:11 +0000281}
282
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000283bool TargetTransformInfo::haveFastSqrt(Type *Ty) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000284 return TTIImpl->haveFastSqrt(Ty);
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000285}
286
Sanjay Patel0de1a4b2017-11-27 21:15:43 +0000287bool TargetTransformInfo::isFCmpOrdCheaperThanFCmpZero(Type *Ty) const {
288 return TTIImpl->isFCmpOrdCheaperThanFCmpZero(Ty);
289}
290
Chandler Carruth93205eb2015-08-05 18:08:10 +0000291int TargetTransformInfo::getFPOpCost(Type *Ty) const {
292 int Cost = TTIImpl->getFPOpCost(Ty);
293 assert(Cost >= 0 && "TTI should not produce negative costs!");
294 return Cost;
Cameron Esfahani17177d12015-02-05 02:09:33 +0000295}
296
Sjoerd Meijer38c2cd02016-07-14 07:44:20 +0000297int TargetTransformInfo::getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx,
298 const APInt &Imm,
299 Type *Ty) const {
300 int Cost = TTIImpl->getIntImmCodeSizeCost(Opcode, Idx, Imm, Ty);
301 assert(Cost >= 0 && "TTI should not produce negative costs!");
302 return Cost;
303}
304
Chandler Carruth93205eb2015-08-05 18:08:10 +0000305int TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
306 int Cost = TTIImpl->getIntImmCost(Imm, Ty);
307 assert(Cost >= 0 && "TTI should not produce negative costs!");
308 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000309}
310
Chandler Carruth93205eb2015-08-05 18:08:10 +0000311int TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx,
312 const APInt &Imm, Type *Ty) const {
313 int Cost = TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty);
314 assert(Cost >= 0 && "TTI should not produce negative costs!");
315 return Cost;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000316}
317
Chandler Carruth93205eb2015-08-05 18:08:10 +0000318int TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
319 const APInt &Imm, Type *Ty) const {
320 int Cost = TTIImpl->getIntImmCost(IID, Idx, Imm, Ty);
321 assert(Cost >= 0 && "TTI should not produce negative costs!");
322 return Cost;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000323}
324
Chandler Carruth539edf42013-01-05 11:43:11 +0000325unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000326 return TTIImpl->getNumberOfRegisters(Vector);
Chandler Carruth539edf42013-01-05 11:43:11 +0000327}
328
Nadav Rotemb1791a72013-01-09 22:29:00 +0000329unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000330 return TTIImpl->getRegisterBitWidth(Vector);
Nadav Rotemb1791a72013-01-09 22:29:00 +0000331}
332
Adam Nemete29686e2017-05-15 21:15:01 +0000333unsigned TargetTransformInfo::getMinVectorRegisterBitWidth() const {
334 return TTIImpl->getMinVectorRegisterBitWidth();
335}
336
Jun Bum Limdee55652017-04-03 19:20:07 +0000337bool TargetTransformInfo::shouldConsiderAddressTypePromotion(
338 const Instruction &I, bool &AllowPromotionWithoutCommonHeader) const {
339 return TTIImpl->shouldConsiderAddressTypePromotion(
340 I, AllowPromotionWithoutCommonHeader);
341}
342
Adam Nemetaf761102016-01-21 18:28:36 +0000343unsigned TargetTransformInfo::getCacheLineSize() const {
344 return TTIImpl->getCacheLineSize();
345}
346
Tobias Grosserd7eb6192017-08-24 09:46:25 +0000347llvm::Optional<unsigned> TargetTransformInfo::getCacheSize(CacheLevel Level)
348 const {
349 return TTIImpl->getCacheSize(Level);
350}
351
352llvm::Optional<unsigned> TargetTransformInfo::getCacheAssociativity(
353 CacheLevel Level) const {
354 return TTIImpl->getCacheAssociativity(Level);
355}
356
Adam Nemetdadfbb52016-01-27 22:21:25 +0000357unsigned TargetTransformInfo::getPrefetchDistance() const {
358 return TTIImpl->getPrefetchDistance();
359}
360
Adam Nemet6d8beec2016-03-18 00:27:38 +0000361unsigned TargetTransformInfo::getMinPrefetchStride() const {
362 return TTIImpl->getMinPrefetchStride();
363}
364
Adam Nemet709e3042016-03-18 00:27:43 +0000365unsigned TargetTransformInfo::getMaxPrefetchIterationsAhead() const {
366 return TTIImpl->getMaxPrefetchIterationsAhead();
367}
368
Wei Mi062c7442015-05-06 17:12:25 +0000369unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const {
370 return TTIImpl->getMaxInterleaveFactor(VF);
Nadav Rotemb696c362013-01-09 01:15:42 +0000371}
372
Chandler Carruth93205eb2015-08-05 18:08:10 +0000373int TargetTransformInfo::getArithmeticInstrCost(
Chandler Carruth705b1852015-01-31 03:43:40 +0000374 unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
375 OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000376 OperandValueProperties Opd2PropInfo,
377 ArrayRef<const Value *> Args) const {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000378 int Cost = TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000379 Opd1PropInfo, Opd2PropInfo, Args);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000380 assert(Cost >= 0 && "TTI should not produce negative costs!");
381 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000382}
383
Chandler Carruth93205eb2015-08-05 18:08:10 +0000384int TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty, int Index,
385 Type *SubTp) const {
386 int Cost = TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
387 assert(Cost >= 0 && "TTI should not produce negative costs!");
388 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000389}
390
Chandler Carruth93205eb2015-08-05 18:08:10 +0000391int TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000392 Type *Src, const Instruction *I) const {
393 assert ((I == nullptr || I->getOpcode() == Opcode) &&
394 "Opcode should reflect passed instruction.");
395 int Cost = TTIImpl->getCastInstrCost(Opcode, Dst, Src, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000396 assert(Cost >= 0 && "TTI should not produce negative costs!");
397 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000398}
399
Matthew Simpsone5dfb082016-04-27 15:20:21 +0000400int TargetTransformInfo::getExtractWithExtendCost(unsigned Opcode, Type *Dst,
401 VectorType *VecTy,
402 unsigned Index) const {
403 int Cost = TTIImpl->getExtractWithExtendCost(Opcode, Dst, VecTy, Index);
404 assert(Cost >= 0 && "TTI should not produce negative costs!");
405 return Cost;
406}
407
Chandler Carruth93205eb2015-08-05 18:08:10 +0000408int TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
409 int Cost = TTIImpl->getCFInstrCost(Opcode);
410 assert(Cost >= 0 && "TTI should not produce negative costs!");
411 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000412}
413
Chandler Carruth93205eb2015-08-05 18:08:10 +0000414int TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000415 Type *CondTy, const Instruction *I) const {
416 assert ((I == nullptr || I->getOpcode() == Opcode) &&
417 "Opcode should reflect passed instruction.");
418 int Cost = TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000419 assert(Cost >= 0 && "TTI should not produce negative costs!");
420 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000421}
422
Chandler Carruth93205eb2015-08-05 18:08:10 +0000423int TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
424 unsigned Index) const {
425 int Cost = TTIImpl->getVectorInstrCost(Opcode, Val, Index);
426 assert(Cost >= 0 && "TTI should not produce negative costs!");
427 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000428}
429
Chandler Carruth93205eb2015-08-05 18:08:10 +0000430int TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
431 unsigned Alignment,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000432 unsigned AddressSpace,
433 const Instruction *I) const {
434 assert ((I == nullptr || I->getOpcode() == Opcode) &&
435 "Opcode should reflect passed instruction.");
436 int Cost = TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000437 assert(Cost >= 0 && "TTI should not produce negative costs!");
438 return Cost;
Elena Demikhovskya3232f72015-01-25 08:44:46 +0000439}
440
Chandler Carruth93205eb2015-08-05 18:08:10 +0000441int TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
442 unsigned Alignment,
443 unsigned AddressSpace) const {
444 int Cost =
445 TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
446 assert(Cost >= 0 && "TTI should not produce negative costs!");
447 return Cost;
Chandler Carruth705b1852015-01-31 03:43:40 +0000448}
449
Elena Demikhovsky54946982015-12-28 20:10:59 +0000450int TargetTransformInfo::getGatherScatterOpCost(unsigned Opcode, Type *DataTy,
451 Value *Ptr, bool VariableMask,
452 unsigned Alignment) const {
453 int Cost = TTIImpl->getGatherScatterOpCost(Opcode, DataTy, Ptr, VariableMask,
454 Alignment);
455 assert(Cost >= 0 && "TTI should not produce negative costs!");
456 return Cost;
457}
458
Chandler Carruth93205eb2015-08-05 18:08:10 +0000459int TargetTransformInfo::getInterleavedMemoryOpCost(
Hao Liu32c05392015-06-08 06:39:56 +0000460 unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
461 unsigned Alignment, unsigned AddressSpace) const {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000462 int Cost = TTIImpl->getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
463 Alignment, AddressSpace);
464 assert(Cost >= 0 && "TTI should not produce negative costs!");
465 return Cost;
Hao Liu32c05392015-06-08 06:39:56 +0000466}
467
Chandler Carruth93205eb2015-08-05 18:08:10 +0000468int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
Jonas Paulssona48ea232017-03-14 06:35:36 +0000469 ArrayRef<Type *> Tys, FastMathFlags FMF,
470 unsigned ScalarizationCostPassed) const {
471 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys, FMF,
472 ScalarizationCostPassed);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000473 assert(Cost >= 0 && "TTI should not produce negative costs!");
474 return Cost;
475}
476
Elena Demikhovsky54946982015-12-28 20:10:59 +0000477int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
Jonas Paulssona48ea232017-03-14 06:35:36 +0000478 ArrayRef<Value *> Args, FastMathFlags FMF, unsigned VF) const {
479 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Args, FMF, VF);
Elena Demikhovsky54946982015-12-28 20:10:59 +0000480 assert(Cost >= 0 && "TTI should not produce negative costs!");
481 return Cost;
482}
483
Chandler Carruth93205eb2015-08-05 18:08:10 +0000484int TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy,
485 ArrayRef<Type *> Tys) const {
486 int Cost = TTIImpl->getCallInstrCost(F, RetTy, Tys);
487 assert(Cost >= 0 && "TTI should not produce negative costs!");
488 return Cost;
Michael Zolotukhin7ed84a82015-03-17 19:26:23 +0000489}
490
Chandler Carruth539edf42013-01-05 11:43:11 +0000491unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000492 return TTIImpl->getNumberOfParts(Tp);
Chandler Carruth539edf42013-01-05 11:43:11 +0000493}
494
Chandler Carruth93205eb2015-08-05 18:08:10 +0000495int TargetTransformInfo::getAddressComputationCost(Type *Tp,
Mohammed Agabaria23599ba2017-01-05 14:03:41 +0000496 ScalarEvolution *SE,
497 const SCEV *Ptr) const {
498 int Cost = TTIImpl->getAddressComputationCost(Tp, SE, Ptr);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000499 assert(Cost >= 0 && "TTI should not produce negative costs!");
500 return Cost;
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000501}
Chandler Carruth539edf42013-01-05 11:43:11 +0000502
Alexey Bataev3e9b3eb2017-07-31 14:19:32 +0000503int TargetTransformInfo::getArithmeticReductionCost(unsigned Opcode, Type *Ty,
504 bool IsPairwiseForm) const {
505 int Cost = TTIImpl->getArithmeticReductionCost(Opcode, Ty, IsPairwiseForm);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000506 assert(Cost >= 0 && "TTI should not produce negative costs!");
507 return Cost;
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000508}
509
Alexey Bataev6dd29fc2017-09-08 13:49:36 +0000510int TargetTransformInfo::getMinMaxReductionCost(Type *Ty, Type *CondTy,
511 bool IsPairwiseForm,
512 bool IsUnsigned) const {
513 int Cost =
514 TTIImpl->getMinMaxReductionCost(Ty, CondTy, IsPairwiseForm, IsUnsigned);
515 assert(Cost >= 0 && "TTI should not produce negative costs!");
516 return Cost;
517}
518
Chandler Carruth705b1852015-01-31 03:43:40 +0000519unsigned
520TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
521 return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
Chad Rosierf9327d62015-01-26 22:51:15 +0000522}
523
524bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst,
525 MemIntrinsicInfo &Info) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000526 return TTIImpl->getTgtMemIntrinsic(Inst, Info);
Chad Rosierf9327d62015-01-26 22:51:15 +0000527}
528
Anna Thomasb2a212c2017-06-06 16:45:25 +0000529unsigned TargetTransformInfo::getAtomicMemIntrinsicMaxElementSize() const {
530 return TTIImpl->getAtomicMemIntrinsicMaxElementSize();
531}
532
Chandler Carruth705b1852015-01-31 03:43:40 +0000533Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic(
534 IntrinsicInst *Inst, Type *ExpectedType) const {
535 return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
536}
537
Sean Fertile9cd1cdf2017-07-07 02:00:06 +0000538Type *TargetTransformInfo::getMemcpyLoopLoweringType(LLVMContext &Context,
539 Value *Length,
540 unsigned SrcAlign,
541 unsigned DestAlign) const {
542 return TTIImpl->getMemcpyLoopLoweringType(Context, Length, SrcAlign,
543 DestAlign);
544}
545
546void TargetTransformInfo::getMemcpyLoopResidualLoweringType(
547 SmallVectorImpl<Type *> &OpsOut, LLVMContext &Context,
548 unsigned RemainingBytes, unsigned SrcAlign, unsigned DestAlign) const {
549 TTIImpl->getMemcpyLoopResidualLoweringType(OpsOut, Context, RemainingBytes,
550 SrcAlign, DestAlign);
551}
552
Eric Christopherd566fb12015-07-29 22:09:48 +0000553bool TargetTransformInfo::areInlineCompatible(const Function *Caller,
554 const Function *Callee) const {
555 return TTIImpl->areInlineCompatible(Caller, Callee);
Eric Christopher4371b132015-07-02 01:11:47 +0000556}
557
Volkan Keles1c386812016-10-03 10:31:34 +0000558unsigned TargetTransformInfo::getLoadStoreVecRegBitWidth(unsigned AS) const {
559 return TTIImpl->getLoadStoreVecRegBitWidth(AS);
560}
561
562bool TargetTransformInfo::isLegalToVectorizeLoad(LoadInst *LI) const {
563 return TTIImpl->isLegalToVectorizeLoad(LI);
564}
565
566bool TargetTransformInfo::isLegalToVectorizeStore(StoreInst *SI) const {
567 return TTIImpl->isLegalToVectorizeStore(SI);
568}
569
570bool TargetTransformInfo::isLegalToVectorizeLoadChain(
571 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
572 return TTIImpl->isLegalToVectorizeLoadChain(ChainSizeInBytes, Alignment,
573 AddrSpace);
574}
575
576bool TargetTransformInfo::isLegalToVectorizeStoreChain(
577 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
578 return TTIImpl->isLegalToVectorizeStoreChain(ChainSizeInBytes, Alignment,
579 AddrSpace);
580}
581
582unsigned TargetTransformInfo::getLoadVectorFactor(unsigned VF,
583 unsigned LoadSize,
584 unsigned ChainSizeInBytes,
585 VectorType *VecTy) const {
586 return TTIImpl->getLoadVectorFactor(VF, LoadSize, ChainSizeInBytes, VecTy);
587}
588
589unsigned TargetTransformInfo::getStoreVectorFactor(unsigned VF,
590 unsigned StoreSize,
591 unsigned ChainSizeInBytes,
592 VectorType *VecTy) const {
593 return TTIImpl->getStoreVectorFactor(VF, StoreSize, ChainSizeInBytes, VecTy);
594}
595
Amara Emersoncf9daa32017-05-09 10:43:25 +0000596bool TargetTransformInfo::useReductionIntrinsic(unsigned Opcode,
597 Type *Ty, ReductionFlags Flags) const {
598 return TTIImpl->useReductionIntrinsic(Opcode, Ty, Flags);
599}
600
Amara Emerson836b0f42017-05-10 09:42:49 +0000601bool TargetTransformInfo::shouldExpandReduction(const IntrinsicInst *II) const {
602 return TTIImpl->shouldExpandReduction(II);
603}
Amara Emersoncf9daa32017-05-09 10:43:25 +0000604
Guozhi Wei62d64142017-09-08 22:29:17 +0000605int TargetTransformInfo::getInstructionLatency(const Instruction *I) const {
606 return TTIImpl->getInstructionLatency(I);
607}
608
609static bool isReverseVectorMask(ArrayRef<int> Mask) {
610 for (unsigned i = 0, MaskSize = Mask.size(); i < MaskSize; ++i)
611 if (Mask[i] >= 0 && Mask[i] != (int)(MaskSize - 1 - i))
612 return false;
613 return true;
614}
615
616static bool isSingleSourceVectorMask(ArrayRef<int> Mask) {
617 bool Vec0 = false;
618 bool Vec1 = false;
619 for (unsigned i = 0, NumVecElts = Mask.size(); i < NumVecElts; ++i) {
620 if (Mask[i] >= 0) {
621 if ((unsigned)Mask[i] >= NumVecElts)
622 Vec1 = true;
623 else
624 Vec0 = true;
625 }
626 }
627 return !(Vec0 && Vec1);
628}
629
630static bool isZeroEltBroadcastVectorMask(ArrayRef<int> Mask) {
631 for (unsigned i = 0; i < Mask.size(); ++i)
632 if (Mask[i] > 0)
633 return false;
634 return true;
635}
636
637static bool isAlternateVectorMask(ArrayRef<int> Mask) {
638 bool isAlternate = true;
639 unsigned MaskSize = Mask.size();
640
641 // Example: shufflevector A, B, <0,5,2,7>
642 for (unsigned i = 0; i < MaskSize && isAlternate; ++i) {
643 if (Mask[i] < 0)
644 continue;
645 isAlternate = Mask[i] == (int)((i & 1) ? MaskSize + i : i);
646 }
647
648 if (isAlternate)
649 return true;
650
651 isAlternate = true;
652 // Example: shufflevector A, B, <4,1,6,3>
653 for (unsigned i = 0; i < MaskSize && isAlternate; ++i) {
654 if (Mask[i] < 0)
655 continue;
656 isAlternate = Mask[i] == (int)((i & 1) ? i : MaskSize + i);
657 }
658
659 return isAlternate;
660}
661
662static TargetTransformInfo::OperandValueKind getOperandInfo(Value *V) {
663 TargetTransformInfo::OperandValueKind OpInfo =
664 TargetTransformInfo::OK_AnyValue;
665
666 // Check for a splat of a constant or for a non uniform vector of constants.
667 if (isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) {
668 OpInfo = TargetTransformInfo::OK_NonUniformConstantValue;
669 if (cast<Constant>(V)->getSplatValue() != nullptr)
670 OpInfo = TargetTransformInfo::OK_UniformConstantValue;
671 }
672
673 // Check for a splat of a uniform value. This is not loop aware, so return
674 // true only for the obviously uniform cases (argument, globalvalue)
675 const Value *Splat = getSplatValue(V);
676 if (Splat && (isa<Argument>(Splat) || isa<GlobalValue>(Splat)))
677 OpInfo = TargetTransformInfo::OK_UniformValue;
678
679 return OpInfo;
680}
681
682static bool matchPairwiseShuffleMask(ShuffleVectorInst *SI, bool IsLeft,
683 unsigned Level) {
684 // We don't need a shuffle if we just want to have element 0 in position 0 of
685 // the vector.
686 if (!SI && Level == 0 && IsLeft)
687 return true;
688 else if (!SI)
689 return false;
690
691 SmallVector<int, 32> Mask(SI->getType()->getVectorNumElements(), -1);
692
693 // Build a mask of 0, 2, ... (left) or 1, 3, ... (right) depending on whether
694 // we look at the left or right side.
695 for (unsigned i = 0, e = (1 << Level), val = !IsLeft; i != e; ++i, val += 2)
696 Mask[i] = val;
697
698 SmallVector<int, 16> ActualMask = SI->getShuffleMask();
699 return Mask == ActualMask;
700}
701
702namespace {
703/// Kind of the reduction data.
704enum ReductionKind {
705 RK_None, /// Not a reduction.
706 RK_Arithmetic, /// Binary reduction data.
707 RK_MinMax, /// Min/max reduction data.
708 RK_UnsignedMinMax, /// Unsigned min/max reduction data.
709};
710/// Contains opcode + LHS/RHS parts of the reduction operations.
711struct ReductionData {
712 ReductionData() = delete;
713 ReductionData(ReductionKind Kind, unsigned Opcode, Value *LHS, Value *RHS)
714 : Opcode(Opcode), LHS(LHS), RHS(RHS), Kind(Kind) {
715 assert(Kind != RK_None && "expected binary or min/max reduction only.");
716 }
717 unsigned Opcode = 0;
718 Value *LHS = nullptr;
719 Value *RHS = nullptr;
720 ReductionKind Kind = RK_None;
721 bool hasSameData(ReductionData &RD) const {
722 return Kind == RD.Kind && Opcode == RD.Opcode;
723 }
724};
725} // namespace
726
727static Optional<ReductionData> getReductionData(Instruction *I) {
728 Value *L, *R;
729 if (m_BinOp(m_Value(L), m_Value(R)).match(I))
730 return ReductionData(RK_Arithmetic, I->getOpcode(), L, R);
731 if (auto *SI = dyn_cast<SelectInst>(I)) {
732 if (m_SMin(m_Value(L), m_Value(R)).match(SI) ||
733 m_SMax(m_Value(L), m_Value(R)).match(SI) ||
734 m_OrdFMin(m_Value(L), m_Value(R)).match(SI) ||
735 m_OrdFMax(m_Value(L), m_Value(R)).match(SI) ||
736 m_UnordFMin(m_Value(L), m_Value(R)).match(SI) ||
737 m_UnordFMax(m_Value(L), m_Value(R)).match(SI)) {
738 auto *CI = cast<CmpInst>(SI->getCondition());
739 return ReductionData(RK_MinMax, CI->getOpcode(), L, R);
740 }
741 if (m_UMin(m_Value(L), m_Value(R)).match(SI) ||
742 m_UMax(m_Value(L), m_Value(R)).match(SI)) {
743 auto *CI = cast<CmpInst>(SI->getCondition());
744 return ReductionData(RK_UnsignedMinMax, CI->getOpcode(), L, R);
745 }
746 }
747 return llvm::None;
748}
749
750static ReductionKind matchPairwiseReductionAtLevel(Instruction *I,
751 unsigned Level,
752 unsigned NumLevels) {
753 // Match one level of pairwise operations.
754 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef,
755 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef>
756 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef,
757 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef>
758 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1
759 if (!I)
760 return RK_None;
761
762 assert(I->getType()->isVectorTy() && "Expecting a vector type");
763
764 Optional<ReductionData> RD = getReductionData(I);
765 if (!RD)
766 return RK_None;
767
768 ShuffleVectorInst *LS = dyn_cast<ShuffleVectorInst>(RD->LHS);
769 if (!LS && Level)
770 return RK_None;
771 ShuffleVectorInst *RS = dyn_cast<ShuffleVectorInst>(RD->RHS);
772 if (!RS && Level)
773 return RK_None;
774
775 // On level 0 we can omit one shufflevector instruction.
776 if (!Level && !RS && !LS)
777 return RK_None;
778
779 // Shuffle inputs must match.
780 Value *NextLevelOpL = LS ? LS->getOperand(0) : nullptr;
781 Value *NextLevelOpR = RS ? RS->getOperand(0) : nullptr;
782 Value *NextLevelOp = nullptr;
783 if (NextLevelOpR && NextLevelOpL) {
784 // If we have two shuffles their operands must match.
785 if (NextLevelOpL != NextLevelOpR)
786 return RK_None;
787
788 NextLevelOp = NextLevelOpL;
789 } else if (Level == 0 && (NextLevelOpR || NextLevelOpL)) {
790 // On the first level we can omit the shufflevector <0, undef,...>. So the
791 // input to the other shufflevector <1, undef> must match with one of the
792 // inputs to the current binary operation.
793 // Example:
794 // %NextLevelOpL = shufflevector %R, <1, undef ...>
795 // %BinOp = fadd %NextLevelOpL, %R
796 if (NextLevelOpL && NextLevelOpL != RD->RHS)
797 return RK_None;
798 else if (NextLevelOpR && NextLevelOpR != RD->LHS)
799 return RK_None;
800
801 NextLevelOp = NextLevelOpL ? RD->RHS : RD->LHS;
802 } else
803 return RK_None;
804
805 // Check that the next levels binary operation exists and matches with the
806 // current one.
807 if (Level + 1 != NumLevels) {
808 Optional<ReductionData> NextLevelRD =
809 getReductionData(cast<Instruction>(NextLevelOp));
810 if (!NextLevelRD || !RD->hasSameData(*NextLevelRD))
811 return RK_None;
812 }
813
814 // Shuffle mask for pairwise operation must match.
815 if (matchPairwiseShuffleMask(LS, /*IsLeft=*/true, Level)) {
816 if (!matchPairwiseShuffleMask(RS, /*IsLeft=*/false, Level))
817 return RK_None;
818 } else if (matchPairwiseShuffleMask(RS, /*IsLeft=*/true, Level)) {
819 if (!matchPairwiseShuffleMask(LS, /*IsLeft=*/false, Level))
820 return RK_None;
821 } else {
822 return RK_None;
823 }
824
825 if (++Level == NumLevels)
826 return RD->Kind;
827
828 // Match next level.
829 return matchPairwiseReductionAtLevel(cast<Instruction>(NextLevelOp), Level,
830 NumLevels);
831}
832
833static ReductionKind matchPairwiseReduction(const ExtractElementInst *ReduxRoot,
834 unsigned &Opcode, Type *&Ty) {
835 if (!EnableReduxCost)
836 return RK_None;
837
838 // Need to extract the first element.
839 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1));
840 unsigned Idx = ~0u;
841 if (CI)
842 Idx = CI->getZExtValue();
843 if (Idx != 0)
844 return RK_None;
845
846 auto *RdxStart = dyn_cast<Instruction>(ReduxRoot->getOperand(0));
847 if (!RdxStart)
848 return RK_None;
849 Optional<ReductionData> RD = getReductionData(RdxStart);
850 if (!RD)
851 return RK_None;
852
853 Type *VecTy = RdxStart->getType();
854 unsigned NumVecElems = VecTy->getVectorNumElements();
855 if (!isPowerOf2_32(NumVecElems))
856 return RK_None;
857
858 // We look for a sequence of shuffle,shuffle,add triples like the following
859 // that builds a pairwise reduction tree.
860 //
861 // (X0, X1, X2, X3)
862 // (X0 + X1, X2 + X3, undef, undef)
863 // ((X0 + X1) + (X2 + X3), undef, undef, undef)
864 //
865 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef,
866 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef>
867 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef,
868 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef>
869 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1
870 // %rdx.shuf.1.0 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef,
871 // <4 x i32> <i32 0, i32 undef, i32 undef, i32 undef>
872 // %rdx.shuf.1.1 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef,
873 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
874 // %bin.rdx8 = fadd <4 x float> %rdx.shuf.1.0, %rdx.shuf.1.1
875 // %r = extractelement <4 x float> %bin.rdx8, i32 0
876 if (matchPairwiseReductionAtLevel(RdxStart, 0, Log2_32(NumVecElems)) ==
877 RK_None)
878 return RK_None;
879
880 Opcode = RD->Opcode;
881 Ty = VecTy;
882
883 return RD->Kind;
884}
885
886static std::pair<Value *, ShuffleVectorInst *>
887getShuffleAndOtherOprd(Value *L, Value *R) {
888 ShuffleVectorInst *S = nullptr;
889
890 if ((S = dyn_cast<ShuffleVectorInst>(L)))
891 return std::make_pair(R, S);
892
893 S = dyn_cast<ShuffleVectorInst>(R);
894 return std::make_pair(L, S);
895}
896
897static ReductionKind
898matchVectorSplittingReduction(const ExtractElementInst *ReduxRoot,
899 unsigned &Opcode, Type *&Ty) {
900 if (!EnableReduxCost)
901 return RK_None;
902
903 // Need to extract the first element.
904 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1));
905 unsigned Idx = ~0u;
906 if (CI)
907 Idx = CI->getZExtValue();
908 if (Idx != 0)
909 return RK_None;
910
911 auto *RdxStart = dyn_cast<Instruction>(ReduxRoot->getOperand(0));
912 if (!RdxStart)
913 return RK_None;
914 Optional<ReductionData> RD = getReductionData(RdxStart);
915 if (!RD)
916 return RK_None;
917
918 Type *VecTy = ReduxRoot->getOperand(0)->getType();
919 unsigned NumVecElems = VecTy->getVectorNumElements();
920 if (!isPowerOf2_32(NumVecElems))
921 return RK_None;
922
923 // We look for a sequence of shuffles and adds like the following matching one
924 // fadd, shuffle vector pair at a time.
925 //
926 // %rdx.shuf = shufflevector <4 x float> %rdx, <4 x float> undef,
927 // <4 x i32> <i32 2, i32 3, i32 undef, i32 undef>
928 // %bin.rdx = fadd <4 x float> %rdx, %rdx.shuf
929 // %rdx.shuf7 = shufflevector <4 x float> %bin.rdx, <4 x float> undef,
930 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
931 // %bin.rdx8 = fadd <4 x float> %bin.rdx, %rdx.shuf7
932 // %r = extractelement <4 x float> %bin.rdx8, i32 0
933
934 unsigned MaskStart = 1;
935 Instruction *RdxOp = RdxStart;
936 SmallVector<int, 32> ShuffleMask(NumVecElems, 0);
937 unsigned NumVecElemsRemain = NumVecElems;
938 while (NumVecElemsRemain - 1) {
939 // Check for the right reduction operation.
940 if (!RdxOp)
941 return RK_None;
942 Optional<ReductionData> RDLevel = getReductionData(RdxOp);
943 if (!RDLevel || !RDLevel->hasSameData(*RD))
944 return RK_None;
945
946 Value *NextRdxOp;
947 ShuffleVectorInst *Shuffle;
948 std::tie(NextRdxOp, Shuffle) =
949 getShuffleAndOtherOprd(RDLevel->LHS, RDLevel->RHS);
950
951 // Check the current reduction operation and the shuffle use the same value.
952 if (Shuffle == nullptr)
953 return RK_None;
954 if (Shuffle->getOperand(0) != NextRdxOp)
955 return RK_None;
956
957 // Check that shuffle masks matches.
958 for (unsigned j = 0; j != MaskStart; ++j)
959 ShuffleMask[j] = MaskStart + j;
960 // Fill the rest of the mask with -1 for undef.
961 std::fill(&ShuffleMask[MaskStart], ShuffleMask.end(), -1);
962
963 SmallVector<int, 16> Mask = Shuffle->getShuffleMask();
964 if (ShuffleMask != Mask)
965 return RK_None;
966
967 RdxOp = dyn_cast<Instruction>(NextRdxOp);
968 NumVecElemsRemain /= 2;
969 MaskStart *= 2;
970 }
971
972 Opcode = RD->Opcode;
973 Ty = VecTy;
974 return RD->Kind;
975}
976
977int TargetTransformInfo::getInstructionThroughput(const Instruction *I) const {
978 switch (I->getOpcode()) {
979 case Instruction::GetElementPtr:
980 return getUserCost(I);
981
982 case Instruction::Ret:
983 case Instruction::PHI:
984 case Instruction::Br: {
985 return getCFInstrCost(I->getOpcode());
986 }
987 case Instruction::Add:
988 case Instruction::FAdd:
989 case Instruction::Sub:
990 case Instruction::FSub:
991 case Instruction::Mul:
992 case Instruction::FMul:
993 case Instruction::UDiv:
994 case Instruction::SDiv:
995 case Instruction::FDiv:
996 case Instruction::URem:
997 case Instruction::SRem:
998 case Instruction::FRem:
999 case Instruction::Shl:
1000 case Instruction::LShr:
1001 case Instruction::AShr:
1002 case Instruction::And:
1003 case Instruction::Or:
1004 case Instruction::Xor: {
1005 TargetTransformInfo::OperandValueKind Op1VK =
1006 getOperandInfo(I->getOperand(0));
1007 TargetTransformInfo::OperandValueKind Op2VK =
1008 getOperandInfo(I->getOperand(1));
1009 SmallVector<const Value*, 2> Operands(I->operand_values());
1010 return getArithmeticInstrCost(I->getOpcode(), I->getType(), Op1VK,
1011 Op2VK, TargetTransformInfo::OP_None,
1012 TargetTransformInfo::OP_None,
1013 Operands);
1014 }
1015 case Instruction::Select: {
1016 const SelectInst *SI = cast<SelectInst>(I);
1017 Type *CondTy = SI->getCondition()->getType();
1018 return getCmpSelInstrCost(I->getOpcode(), I->getType(), CondTy, I);
1019 }
1020 case Instruction::ICmp:
1021 case Instruction::FCmp: {
1022 Type *ValTy = I->getOperand(0)->getType();
1023 return getCmpSelInstrCost(I->getOpcode(), ValTy, I->getType(), I);
1024 }
1025 case Instruction::Store: {
1026 const StoreInst *SI = cast<StoreInst>(I);
1027 Type *ValTy = SI->getValueOperand()->getType();
1028 return getMemoryOpCost(I->getOpcode(), ValTy,
1029 SI->getAlignment(),
1030 SI->getPointerAddressSpace(), I);
1031 }
1032 case Instruction::Load: {
1033 const LoadInst *LI = cast<LoadInst>(I);
1034 return getMemoryOpCost(I->getOpcode(), I->getType(),
1035 LI->getAlignment(),
1036 LI->getPointerAddressSpace(), I);
1037 }
1038 case Instruction::ZExt:
1039 case Instruction::SExt:
1040 case Instruction::FPToUI:
1041 case Instruction::FPToSI:
1042 case Instruction::FPExt:
1043 case Instruction::PtrToInt:
1044 case Instruction::IntToPtr:
1045 case Instruction::SIToFP:
1046 case Instruction::UIToFP:
1047 case Instruction::Trunc:
1048 case Instruction::FPTrunc:
1049 case Instruction::BitCast:
1050 case Instruction::AddrSpaceCast: {
1051 Type *SrcTy = I->getOperand(0)->getType();
1052 return getCastInstrCost(I->getOpcode(), I->getType(), SrcTy, I);
1053 }
1054 case Instruction::ExtractElement: {
1055 const ExtractElementInst * EEI = cast<ExtractElementInst>(I);
1056 ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1));
1057 unsigned Idx = -1;
1058 if (CI)
1059 Idx = CI->getZExtValue();
1060
1061 // Try to match a reduction sequence (series of shufflevector and vector
1062 // adds followed by a extractelement).
1063 unsigned ReduxOpCode;
1064 Type *ReduxType;
1065
1066 switch (matchVectorSplittingReduction(EEI, ReduxOpCode, ReduxType)) {
1067 case RK_Arithmetic:
1068 return getArithmeticReductionCost(ReduxOpCode, ReduxType,
1069 /*IsPairwiseForm=*/false);
1070 case RK_MinMax:
1071 return getMinMaxReductionCost(
1072 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1073 /*IsPairwiseForm=*/false, /*IsUnsigned=*/false);
1074 case RK_UnsignedMinMax:
1075 return getMinMaxReductionCost(
1076 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1077 /*IsPairwiseForm=*/false, /*IsUnsigned=*/true);
1078 case RK_None:
1079 break;
1080 }
1081
1082 switch (matchPairwiseReduction(EEI, ReduxOpCode, ReduxType)) {
1083 case RK_Arithmetic:
1084 return getArithmeticReductionCost(ReduxOpCode, ReduxType,
1085 /*IsPairwiseForm=*/true);
1086 case RK_MinMax:
1087 return getMinMaxReductionCost(
1088 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1089 /*IsPairwiseForm=*/true, /*IsUnsigned=*/false);
1090 case RK_UnsignedMinMax:
1091 return getMinMaxReductionCost(
1092 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1093 /*IsPairwiseForm=*/true, /*IsUnsigned=*/true);
1094 case RK_None:
1095 break;
1096 }
1097
1098 return getVectorInstrCost(I->getOpcode(),
1099 EEI->getOperand(0)->getType(), Idx);
1100 }
1101 case Instruction::InsertElement: {
1102 const InsertElementInst * IE = cast<InsertElementInst>(I);
1103 ConstantInt *CI = dyn_cast<ConstantInt>(IE->getOperand(2));
1104 unsigned Idx = -1;
1105 if (CI)
1106 Idx = CI->getZExtValue();
1107 return getVectorInstrCost(I->getOpcode(),
1108 IE->getType(), Idx);
1109 }
1110 case Instruction::ShuffleVector: {
1111 const ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
1112 Type *VecTypOp0 = Shuffle->getOperand(0)->getType();
1113 unsigned NumVecElems = VecTypOp0->getVectorNumElements();
1114 SmallVector<int, 16> Mask = Shuffle->getShuffleMask();
1115
1116 if (NumVecElems == Mask.size()) {
1117 if (isReverseVectorMask(Mask))
1118 return getShuffleCost(TargetTransformInfo::SK_Reverse, VecTypOp0,
1119 0, nullptr);
1120 if (isAlternateVectorMask(Mask))
1121 return getShuffleCost(TargetTransformInfo::SK_Alternate,
1122 VecTypOp0, 0, nullptr);
1123
1124 if (isZeroEltBroadcastVectorMask(Mask))
1125 return getShuffleCost(TargetTransformInfo::SK_Broadcast,
1126 VecTypOp0, 0, nullptr);
1127
1128 if (isSingleSourceVectorMask(Mask))
1129 return getShuffleCost(TargetTransformInfo::SK_PermuteSingleSrc,
1130 VecTypOp0, 0, nullptr);
1131
1132 return getShuffleCost(TargetTransformInfo::SK_PermuteTwoSrc,
1133 VecTypOp0, 0, nullptr);
1134 }
1135
1136 return -1;
1137 }
1138 case Instruction::Call:
1139 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1140 SmallVector<Value *, 4> Args(II->arg_operands());
1141
1142 FastMathFlags FMF;
1143 if (auto *FPMO = dyn_cast<FPMathOperator>(II))
1144 FMF = FPMO->getFastMathFlags();
1145
1146 return getIntrinsicInstrCost(II->getIntrinsicID(), II->getType(),
1147 Args, FMF);
1148 }
1149 return -1;
1150 default:
1151 // We don't have any information on this instruction.
1152 return -1;
1153 }
1154}
1155
Chandler Carruth705b1852015-01-31 03:43:40 +00001156TargetTransformInfo::Concept::~Concept() {}
1157
Chandler Carruthe0385522015-02-01 10:11:22 +00001158TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
1159
1160TargetIRAnalysis::TargetIRAnalysis(
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001161 std::function<Result(const Function &)> TTICallback)
Benjamin Kramer82de7d32016-05-27 14:27:24 +00001162 : TTICallback(std::move(TTICallback)) {}
Chandler Carruthe0385522015-02-01 10:11:22 +00001163
Chandler Carruth164a2aa62016-06-17 00:11:01 +00001164TargetIRAnalysis::Result TargetIRAnalysis::run(const Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +00001165 FunctionAnalysisManager &) {
Chandler Carruthe0385522015-02-01 10:11:22 +00001166 return TTICallback(F);
1167}
1168
Chandler Carruthdab4eae2016-11-23 17:53:26 +00001169AnalysisKey TargetIRAnalysis::Key;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +00001170
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001171TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(const Function &F) {
Mehdi Amini5010ebf2015-07-09 02:08:42 +00001172 return Result(F.getParent()->getDataLayout());
Chandler Carruthe0385522015-02-01 10:11:22 +00001173}
1174
Chandler Carruth705b1852015-01-31 03:43:40 +00001175// Register the basic pass.
1176INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
1177 "Target Transform Information", false, true)
1178char TargetTransformInfoWrapperPass::ID = 0;
Chandler Carruth539edf42013-01-05 11:43:11 +00001179
Chandler Carruth705b1852015-01-31 03:43:40 +00001180void TargetTransformInfoWrapperPass::anchor() {}
Chandler Carruth539edf42013-01-05 11:43:11 +00001181
Chandler Carruth705b1852015-01-31 03:43:40 +00001182TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass()
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001183 : ImmutablePass(ID) {
Chandler Carruth705b1852015-01-31 03:43:40 +00001184 initializeTargetTransformInfoWrapperPassPass(
1185 *PassRegistry::getPassRegistry());
1186}
1187
1188TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass(
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001189 TargetIRAnalysis TIRA)
1190 : ImmutablePass(ID), TIRA(std::move(TIRA)) {
Chandler Carruth705b1852015-01-31 03:43:40 +00001191 initializeTargetTransformInfoWrapperPassPass(
1192 *PassRegistry::getPassRegistry());
1193}
1194
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001195TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(const Function &F) {
Sean Silva36e0d012016-08-09 00:28:15 +00001196 FunctionAnalysisManager DummyFAM;
Chandler Carruth164a2aa62016-06-17 00:11:01 +00001197 TTI = TIRA.run(F, DummyFAM);
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001198 return *TTI;
1199}
1200
Chandler Carruth93dcdc42015-01-31 11:17:59 +00001201ImmutablePass *
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001202llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) {
1203 return new TargetTransformInfoWrapperPass(std::move(TIRA));
Chandler Carruth539edf42013-01-05 11:43:11 +00001204}