blob: fad918dabb510980487f96b9751d539356b8620b [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
Sean Fertile9cd1cdf2017-07-07 02:00:06 +000029static cl::opt<bool> UseWideMemcpyLoopLowering(
30 "use-wide-memcpy-loop-lowering", cl::init(false),
31 cl::desc("Enables the new wide memcpy loop lowering in Transforms/Utils."),
32 cl::Hidden);
33
Guozhi Wei62d64142017-09-08 22:29:17 +000034static cl::opt<bool> EnableReduxCost("costmodel-reduxcost", cl::init(false),
35 cl::Hidden,
36 cl::desc("Recognize reduction patterns."));
37
Chandler Carruth93dcdc42015-01-31 11:17:59 +000038namespace {
39/// \brief No-op implementation of the TTI interface using the utility base
40/// classes.
41///
42/// This is used when no target specific information is available.
43struct NoTTIImpl : TargetTransformInfoImplCRTPBase<NoTTIImpl> {
Mehdi Amini5010ebf2015-07-09 02:08:42 +000044 explicit NoTTIImpl(const DataLayout &DL)
Chandler Carruth93dcdc42015-01-31 11:17:59 +000045 : TargetTransformInfoImplCRTPBase<NoTTIImpl>(DL) {}
46};
47}
48
Mehdi Amini5010ebf2015-07-09 02:08:42 +000049TargetTransformInfo::TargetTransformInfo(const DataLayout &DL)
Chandler Carruth93dcdc42015-01-31 11:17:59 +000050 : TTIImpl(new Model<NoTTIImpl>(NoTTIImpl(DL))) {}
51
Chandler Carruth705b1852015-01-31 03:43:40 +000052TargetTransformInfo::~TargetTransformInfo() {}
Nadav Rotem5dc203e2012-10-18 23:22:48 +000053
Chandler Carruth705b1852015-01-31 03:43:40 +000054TargetTransformInfo::TargetTransformInfo(TargetTransformInfo &&Arg)
55 : TTIImpl(std::move(Arg.TTIImpl)) {}
Chandler Carruth539edf42013-01-05 11:43:11 +000056
Chandler Carruth705b1852015-01-31 03:43:40 +000057TargetTransformInfo &TargetTransformInfo::operator=(TargetTransformInfo &&RHS) {
58 TTIImpl = std::move(RHS.TTIImpl);
59 return *this;
Chandler Carruth539edf42013-01-05 11:43:11 +000060}
61
Chandler Carruth93205eb2015-08-05 18:08:10 +000062int TargetTransformInfo::getOperationCost(unsigned Opcode, Type *Ty,
63 Type *OpTy) const {
64 int Cost = TTIImpl->getOperationCost(Opcode, Ty, OpTy);
65 assert(Cost >= 0 && "TTI should not produce negative costs!");
66 return Cost;
Chandler Carruth511aa762013-01-21 01:27:39 +000067}
68
Chandler Carruth93205eb2015-08-05 18:08:10 +000069int TargetTransformInfo::getCallCost(FunctionType *FTy, int NumArgs) const {
70 int Cost = TTIImpl->getCallCost(FTy, NumArgs);
71 assert(Cost >= 0 && "TTI should not produce negative costs!");
72 return Cost;
Chandler Carruth0ba8db42013-01-22 11:26:02 +000073}
74
Chandler Carruth93205eb2015-08-05 18:08:10 +000075int TargetTransformInfo::getCallCost(const Function *F,
76 ArrayRef<const Value *> Arguments) const {
77 int Cost = TTIImpl->getCallCost(F, Arguments);
78 assert(Cost >= 0 && "TTI should not produce negative costs!");
79 return Cost;
Chandler Carruth0ba8db42013-01-22 11:26:02 +000080}
81
Justin Lebar8650a4d2016-04-15 01:38:48 +000082unsigned TargetTransformInfo::getInliningThresholdMultiplier() const {
83 return TTIImpl->getInliningThresholdMultiplier();
84}
85
Jingyue Wu15f3e822016-07-08 21:48:05 +000086int TargetTransformInfo::getGEPCost(Type *PointeeType, const Value *Ptr,
87 ArrayRef<const Value *> Operands) const {
88 return TTIImpl->getGEPCost(PointeeType, Ptr, Operands);
89}
90
Haicheng Wuabdef9e2017-07-15 02:12:16 +000091int TargetTransformInfo::getExtCost(const Instruction *I,
92 const Value *Src) const {
93 return TTIImpl->getExtCost(I, Src);
94}
95
Chandler Carruth93205eb2015-08-05 18:08:10 +000096int TargetTransformInfo::getIntrinsicCost(
97 Intrinsic::ID IID, Type *RetTy, ArrayRef<const Value *> Arguments) const {
98 int Cost = TTIImpl->getIntrinsicCost(IID, RetTy, Arguments);
99 assert(Cost >= 0 && "TTI should not produce negative costs!");
100 return Cost;
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000101}
102
Jun Bum Lim919f9e82017-04-28 16:04:03 +0000103unsigned
104TargetTransformInfo::getEstimatedNumberOfCaseClusters(const SwitchInst &SI,
105 unsigned &JTSize) const {
106 return TTIImpl->getEstimatedNumberOfCaseClusters(SI, JTSize);
107}
108
Evgeny Astigeevich70ed78e2017-06-29 13:42:12 +0000109int TargetTransformInfo::getUserCost(const User *U,
110 ArrayRef<const Value *> Operands) const {
111 int Cost = TTIImpl->getUserCost(U, Operands);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000112 assert(Cost >= 0 && "TTI should not produce negative costs!");
113 return Cost;
Chandler Carruth511aa762013-01-21 01:27:39 +0000114}
115
Tom Stellard8b1e0212013-07-27 00:01:07 +0000116bool TargetTransformInfo::hasBranchDivergence() const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000117 return TTIImpl->hasBranchDivergence();
Tom Stellard8b1e0212013-07-27 00:01:07 +0000118}
119
Jingyue Wu5da831c2015-04-10 05:03:50 +0000120bool TargetTransformInfo::isSourceOfDivergence(const Value *V) const {
121 return TTIImpl->isSourceOfDivergence(V);
122}
123
Alexander Timofeev0f9c84c2017-06-15 19:33:10 +0000124bool llvm::TargetTransformInfo::isAlwaysUniform(const Value *V) const {
125 return TTIImpl->isAlwaysUniform(V);
126}
127
Matt Arsenault42b64782017-01-30 23:02:12 +0000128unsigned TargetTransformInfo::getFlatAddressSpace() const {
129 return TTIImpl->getFlatAddressSpace();
130}
131
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000132bool TargetTransformInfo::isLoweredToCall(const Function *F) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000133 return TTIImpl->isLoweredToCall(F);
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000134}
135
Chandler Carruth705b1852015-01-31 03:43:40 +0000136void TargetTransformInfo::getUnrollingPreferences(
Geoff Berry66d9bdb2017-06-28 15:53:17 +0000137 Loop *L, ScalarEvolution &SE, UnrollingPreferences &UP) const {
138 return TTIImpl->getUnrollingPreferences(L, SE, UP);
Hal Finkel8f2e7002013-09-11 19:25:43 +0000139}
140
Chandler Carruth539edf42013-01-05 11:43:11 +0000141bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000142 return TTIImpl->isLegalAddImmediate(Imm);
Chandler Carruth539edf42013-01-05 11:43:11 +0000143}
144
145bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000146 return TTIImpl->isLegalICmpImmediate(Imm);
Chandler Carruth539edf42013-01-05 11:43:11 +0000147}
148
149bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
150 int64_t BaseOffset,
151 bool HasBaseReg,
Matt Arsenaulte83379e2015-06-07 20:12:03 +0000152 int64_t Scale,
Jonas Paulsson024e3192017-07-21 11:59:37 +0000153 unsigned AddrSpace,
154 Instruction *I) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000155 return TTIImpl->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
Jonas Paulsson024e3192017-07-21 11:59:37 +0000156 Scale, AddrSpace, I);
Chandler Carruth539edf42013-01-05 11:43:11 +0000157}
158
Evgeny Stupachenkof2b3b462017-06-05 23:37:00 +0000159bool TargetTransformInfo::isLSRCostLess(LSRCost &C1, LSRCost &C2) const {
160 return TTIImpl->isLSRCostLess(C1, C2);
161}
162
Elena Demikhovsky20662e32015-10-19 07:43:38 +0000163bool TargetTransformInfo::isLegalMaskedStore(Type *DataType) const {
164 return TTIImpl->isLegalMaskedStore(DataType);
Chandler Carruth705b1852015-01-31 03:43:40 +0000165}
166
Elena Demikhovsky20662e32015-10-19 07:43:38 +0000167bool TargetTransformInfo::isLegalMaskedLoad(Type *DataType) const {
168 return TTIImpl->isLegalMaskedLoad(DataType);
Chandler Carruth705b1852015-01-31 03:43:40 +0000169}
170
Elena Demikhovsky09285852015-10-25 15:37:55 +0000171bool TargetTransformInfo::isLegalMaskedGather(Type *DataType) const {
172 return TTIImpl->isLegalMaskedGather(DataType);
173}
174
175bool TargetTransformInfo::isLegalMaskedScatter(Type *DataType) const {
Mohammed Agabariacef53dc2017-07-27 10:28:16 +0000176 return TTIImpl->isLegalMaskedScatter(DataType);
Elena Demikhovsky09285852015-10-25 15:37:55 +0000177}
178
Sanjay Patel6fd43912017-09-09 13:38:18 +0000179bool TargetTransformInfo::hasDivRemOp(Type *DataType, bool IsSigned) const {
180 return TTIImpl->hasDivRemOp(DataType, IsSigned);
181}
182
Jonas Paulsson8624b7e2017-05-24 13:42:56 +0000183bool TargetTransformInfo::prefersVectorizedAddressing() const {
184 return TTIImpl->prefersVectorizedAddressing();
185}
186
Quentin Colombetbf490d42013-05-31 21:29:03 +0000187int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
188 int64_t BaseOffset,
189 bool HasBaseReg,
Matt Arsenaulte83379e2015-06-07 20:12:03 +0000190 int64_t Scale,
191 unsigned AddrSpace) const {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000192 int Cost = TTIImpl->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg,
193 Scale, AddrSpace);
194 assert(Cost >= 0 && "TTI should not produce negative costs!");
195 return Cost;
Quentin Colombetbf490d42013-05-31 21:29:03 +0000196}
197
Jonas Paulsson024e3192017-07-21 11:59:37 +0000198bool TargetTransformInfo::LSRWithInstrQueries() const {
199 return TTIImpl->LSRWithInstrQueries();
200}
201
Chandler Carruth539edf42013-01-05 11:43:11 +0000202bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000203 return TTIImpl->isTruncateFree(Ty1, Ty2);
Chandler Carruth539edf42013-01-05 11:43:11 +0000204}
205
Chad Rosier54390052015-02-23 19:15:16 +0000206bool TargetTransformInfo::isProfitableToHoist(Instruction *I) const {
207 return TTIImpl->isProfitableToHoist(I);
208}
209
Chandler Carruth539edf42013-01-05 11:43:11 +0000210bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000211 return TTIImpl->isTypeLegal(Ty);
Chandler Carruth539edf42013-01-05 11:43:11 +0000212}
213
214unsigned TargetTransformInfo::getJumpBufAlignment() const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000215 return TTIImpl->getJumpBufAlignment();
Chandler Carruth539edf42013-01-05 11:43:11 +0000216}
217
218unsigned TargetTransformInfo::getJumpBufSize() const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000219 return TTIImpl->getJumpBufSize();
Chandler Carruth539edf42013-01-05 11:43:11 +0000220}
221
222bool TargetTransformInfo::shouldBuildLookupTables() const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000223 return TTIImpl->shouldBuildLookupTables();
Chandler Carruth539edf42013-01-05 11:43:11 +0000224}
Oliver Stannard4df1cc02016-10-07 08:48:24 +0000225bool TargetTransformInfo::shouldBuildLookupTablesForConstant(Constant *C) const {
226 return TTIImpl->shouldBuildLookupTablesForConstant(C);
227}
Chandler Carruth539edf42013-01-05 11:43:11 +0000228
Jonas Paulsson8e2f9482017-01-26 07:03:25 +0000229unsigned TargetTransformInfo::
230getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const {
231 return TTIImpl->getScalarizationOverhead(Ty, Insert, Extract);
232}
233
234unsigned TargetTransformInfo::
235getOperandsScalarizationOverhead(ArrayRef<const Value *> Args,
236 unsigned VF) const {
237 return TTIImpl->getOperandsScalarizationOverhead(Args, VF);
238}
239
Jonas Paulssonda74ed42017-04-12 12:41:37 +0000240bool TargetTransformInfo::supportsEfficientVectorElementLoadStore() const {
241 return TTIImpl->supportsEfficientVectorElementLoadStore();
242}
243
Olivier Sallenave049d8032015-03-06 23:12:04 +0000244bool TargetTransformInfo::enableAggressiveInterleaving(bool LoopHasReductions) const {
245 return TTIImpl->enableAggressiveInterleaving(LoopHasReductions);
246}
247
Clement Courbet2807c0a2017-09-25 06:35:16 +0000248bool TargetTransformInfo::enableMemCmpExpansion(unsigned &MaxLoadSize) const {
249 return TTIImpl->enableMemCmpExpansion(MaxLoadSize);
Zaara Syeda3a7578c2017-05-31 17:12:38 +0000250}
251
Silviu Baranga61bdc512015-08-10 14:50:54 +0000252bool TargetTransformInfo::enableInterleavedAccessVectorization() const {
253 return TTIImpl->enableInterleavedAccessVectorization();
254}
255
Renato Golin5cb666a2016-04-14 20:42:18 +0000256bool TargetTransformInfo::isFPVectorizationPotentiallyUnsafe() const {
257 return TTIImpl->isFPVectorizationPotentiallyUnsafe();
258}
259
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000260bool TargetTransformInfo::allowsMisalignedMemoryAccesses(LLVMContext &Context,
261 unsigned BitWidth,
Alina Sbirlea327955e2016-07-11 20:46:17 +0000262 unsigned AddressSpace,
263 unsigned Alignment,
264 bool *Fast) const {
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000265 return TTIImpl->allowsMisalignedMemoryAccesses(Context, BitWidth, AddressSpace,
Alina Sbirlea327955e2016-07-11 20:46:17 +0000266 Alignment, Fast);
267}
268
Chandler Carruth50a36cd2013-01-07 03:16:03 +0000269TargetTransformInfo::PopcntSupportKind
270TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000271 return TTIImpl->getPopcntSupport(IntTyWidthInBit);
Chandler Carruth539edf42013-01-05 11:43:11 +0000272}
273
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000274bool TargetTransformInfo::haveFastSqrt(Type *Ty) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000275 return TTIImpl->haveFastSqrt(Ty);
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000276}
277
Chandler Carruth93205eb2015-08-05 18:08:10 +0000278int TargetTransformInfo::getFPOpCost(Type *Ty) const {
279 int Cost = TTIImpl->getFPOpCost(Ty);
280 assert(Cost >= 0 && "TTI should not produce negative costs!");
281 return Cost;
Cameron Esfahani17177d12015-02-05 02:09:33 +0000282}
283
Sjoerd Meijer38c2cd02016-07-14 07:44:20 +0000284int TargetTransformInfo::getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx,
285 const APInt &Imm,
286 Type *Ty) const {
287 int Cost = TTIImpl->getIntImmCodeSizeCost(Opcode, Idx, Imm, Ty);
288 assert(Cost >= 0 && "TTI should not produce negative costs!");
289 return Cost;
290}
291
Chandler Carruth93205eb2015-08-05 18:08:10 +0000292int TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
293 int Cost = TTIImpl->getIntImmCost(Imm, Ty);
294 assert(Cost >= 0 && "TTI should not produce negative costs!");
295 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000296}
297
Chandler Carruth93205eb2015-08-05 18:08:10 +0000298int TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx,
299 const APInt &Imm, Type *Ty) const {
300 int Cost = TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty);
301 assert(Cost >= 0 && "TTI should not produce negative costs!");
302 return Cost;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000303}
304
Chandler Carruth93205eb2015-08-05 18:08:10 +0000305int TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
306 const APInt &Imm, Type *Ty) const {
307 int Cost = TTIImpl->getIntImmCost(IID, Idx, Imm, Ty);
308 assert(Cost >= 0 && "TTI should not produce negative costs!");
309 return Cost;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000310}
311
Chandler Carruth539edf42013-01-05 11:43:11 +0000312unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000313 return TTIImpl->getNumberOfRegisters(Vector);
Chandler Carruth539edf42013-01-05 11:43:11 +0000314}
315
Nadav Rotemb1791a72013-01-09 22:29:00 +0000316unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000317 return TTIImpl->getRegisterBitWidth(Vector);
Nadav Rotemb1791a72013-01-09 22:29:00 +0000318}
319
Adam Nemete29686e2017-05-15 21:15:01 +0000320unsigned TargetTransformInfo::getMinVectorRegisterBitWidth() const {
321 return TTIImpl->getMinVectorRegisterBitWidth();
322}
323
Jun Bum Limdee55652017-04-03 19:20:07 +0000324bool TargetTransformInfo::shouldConsiderAddressTypePromotion(
325 const Instruction &I, bool &AllowPromotionWithoutCommonHeader) const {
326 return TTIImpl->shouldConsiderAddressTypePromotion(
327 I, AllowPromotionWithoutCommonHeader);
328}
329
Adam Nemetaf761102016-01-21 18:28:36 +0000330unsigned TargetTransformInfo::getCacheLineSize() const {
331 return TTIImpl->getCacheLineSize();
332}
333
Tobias Grosserd7eb6192017-08-24 09:46:25 +0000334llvm::Optional<unsigned> TargetTransformInfo::getCacheSize(CacheLevel Level)
335 const {
336 return TTIImpl->getCacheSize(Level);
337}
338
339llvm::Optional<unsigned> TargetTransformInfo::getCacheAssociativity(
340 CacheLevel Level) const {
341 return TTIImpl->getCacheAssociativity(Level);
342}
343
Adam Nemetdadfbb52016-01-27 22:21:25 +0000344unsigned TargetTransformInfo::getPrefetchDistance() const {
345 return TTIImpl->getPrefetchDistance();
346}
347
Adam Nemet6d8beec2016-03-18 00:27:38 +0000348unsigned TargetTransformInfo::getMinPrefetchStride() const {
349 return TTIImpl->getMinPrefetchStride();
350}
351
Adam Nemet709e3042016-03-18 00:27:43 +0000352unsigned TargetTransformInfo::getMaxPrefetchIterationsAhead() const {
353 return TTIImpl->getMaxPrefetchIterationsAhead();
354}
355
Wei Mi062c7442015-05-06 17:12:25 +0000356unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const {
357 return TTIImpl->getMaxInterleaveFactor(VF);
Nadav Rotemb696c362013-01-09 01:15:42 +0000358}
359
Chandler Carruth93205eb2015-08-05 18:08:10 +0000360int TargetTransformInfo::getArithmeticInstrCost(
Chandler Carruth705b1852015-01-31 03:43:40 +0000361 unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
362 OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000363 OperandValueProperties Opd2PropInfo,
364 ArrayRef<const Value *> Args) const {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000365 int Cost = TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000366 Opd1PropInfo, Opd2PropInfo, Args);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000367 assert(Cost >= 0 && "TTI should not produce negative costs!");
368 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000369}
370
Chandler Carruth93205eb2015-08-05 18:08:10 +0000371int TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty, int Index,
372 Type *SubTp) const {
373 int Cost = TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
374 assert(Cost >= 0 && "TTI should not produce negative costs!");
375 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000376}
377
Chandler Carruth93205eb2015-08-05 18:08:10 +0000378int TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000379 Type *Src, const Instruction *I) const {
380 assert ((I == nullptr || I->getOpcode() == Opcode) &&
381 "Opcode should reflect passed instruction.");
382 int Cost = TTIImpl->getCastInstrCost(Opcode, Dst, Src, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000383 assert(Cost >= 0 && "TTI should not produce negative costs!");
384 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000385}
386
Matthew Simpsone5dfb082016-04-27 15:20:21 +0000387int TargetTransformInfo::getExtractWithExtendCost(unsigned Opcode, Type *Dst,
388 VectorType *VecTy,
389 unsigned Index) const {
390 int Cost = TTIImpl->getExtractWithExtendCost(Opcode, Dst, VecTy, Index);
391 assert(Cost >= 0 && "TTI should not produce negative costs!");
392 return Cost;
393}
394
Chandler Carruth93205eb2015-08-05 18:08:10 +0000395int TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
396 int Cost = TTIImpl->getCFInstrCost(Opcode);
397 assert(Cost >= 0 && "TTI should not produce negative costs!");
398 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000399}
400
Chandler Carruth93205eb2015-08-05 18:08:10 +0000401int TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000402 Type *CondTy, const Instruction *I) const {
403 assert ((I == nullptr || I->getOpcode() == Opcode) &&
404 "Opcode should reflect passed instruction.");
405 int Cost = TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000406 assert(Cost >= 0 && "TTI should not produce negative costs!");
407 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000408}
409
Chandler Carruth93205eb2015-08-05 18:08:10 +0000410int TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
411 unsigned Index) const {
412 int Cost = TTIImpl->getVectorInstrCost(Opcode, Val, Index);
413 assert(Cost >= 0 && "TTI should not produce negative costs!");
414 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000415}
416
Chandler Carruth93205eb2015-08-05 18:08:10 +0000417int TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
418 unsigned Alignment,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000419 unsigned AddressSpace,
420 const Instruction *I) const {
421 assert ((I == nullptr || I->getOpcode() == Opcode) &&
422 "Opcode should reflect passed instruction.");
423 int Cost = TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000424 assert(Cost >= 0 && "TTI should not produce negative costs!");
425 return Cost;
Elena Demikhovskya3232f72015-01-25 08:44:46 +0000426}
427
Chandler Carruth93205eb2015-08-05 18:08:10 +0000428int TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
429 unsigned Alignment,
430 unsigned AddressSpace) const {
431 int Cost =
432 TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
433 assert(Cost >= 0 && "TTI should not produce negative costs!");
434 return Cost;
Chandler Carruth705b1852015-01-31 03:43:40 +0000435}
436
Elena Demikhovsky54946982015-12-28 20:10:59 +0000437int TargetTransformInfo::getGatherScatterOpCost(unsigned Opcode, Type *DataTy,
438 Value *Ptr, bool VariableMask,
439 unsigned Alignment) const {
440 int Cost = TTIImpl->getGatherScatterOpCost(Opcode, DataTy, Ptr, VariableMask,
441 Alignment);
442 assert(Cost >= 0 && "TTI should not produce negative costs!");
443 return Cost;
444}
445
Chandler Carruth93205eb2015-08-05 18:08:10 +0000446int TargetTransformInfo::getInterleavedMemoryOpCost(
Hao Liu32c05392015-06-08 06:39:56 +0000447 unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
448 unsigned Alignment, unsigned AddressSpace) const {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000449 int Cost = TTIImpl->getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
450 Alignment, AddressSpace);
451 assert(Cost >= 0 && "TTI should not produce negative costs!");
452 return Cost;
Hao Liu32c05392015-06-08 06:39:56 +0000453}
454
Chandler Carruth93205eb2015-08-05 18:08:10 +0000455int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
Jonas Paulssona48ea232017-03-14 06:35:36 +0000456 ArrayRef<Type *> Tys, FastMathFlags FMF,
457 unsigned ScalarizationCostPassed) const {
458 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys, FMF,
459 ScalarizationCostPassed);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000460 assert(Cost >= 0 && "TTI should not produce negative costs!");
461 return Cost;
462}
463
Elena Demikhovsky54946982015-12-28 20:10:59 +0000464int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
Jonas Paulssona48ea232017-03-14 06:35:36 +0000465 ArrayRef<Value *> Args, FastMathFlags FMF, unsigned VF) const {
466 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Args, FMF, VF);
Elena Demikhovsky54946982015-12-28 20:10:59 +0000467 assert(Cost >= 0 && "TTI should not produce negative costs!");
468 return Cost;
469}
470
Chandler Carruth93205eb2015-08-05 18:08:10 +0000471int TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy,
472 ArrayRef<Type *> Tys) const {
473 int Cost = TTIImpl->getCallInstrCost(F, RetTy, Tys);
474 assert(Cost >= 0 && "TTI should not produce negative costs!");
475 return Cost;
Michael Zolotukhin7ed84a82015-03-17 19:26:23 +0000476}
477
Chandler Carruth539edf42013-01-05 11:43:11 +0000478unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000479 return TTIImpl->getNumberOfParts(Tp);
Chandler Carruth539edf42013-01-05 11:43:11 +0000480}
481
Chandler Carruth93205eb2015-08-05 18:08:10 +0000482int TargetTransformInfo::getAddressComputationCost(Type *Tp,
Mohammed Agabaria23599ba2017-01-05 14:03:41 +0000483 ScalarEvolution *SE,
484 const SCEV *Ptr) const {
485 int Cost = TTIImpl->getAddressComputationCost(Tp, SE, Ptr);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000486 assert(Cost >= 0 && "TTI should not produce negative costs!");
487 return Cost;
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000488}
Chandler Carruth539edf42013-01-05 11:43:11 +0000489
Alexey Bataev3e9b3eb2017-07-31 14:19:32 +0000490int TargetTransformInfo::getArithmeticReductionCost(unsigned Opcode, Type *Ty,
491 bool IsPairwiseForm) const {
492 int Cost = TTIImpl->getArithmeticReductionCost(Opcode, Ty, IsPairwiseForm);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000493 assert(Cost >= 0 && "TTI should not produce negative costs!");
494 return Cost;
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000495}
496
Alexey Bataev6dd29fc2017-09-08 13:49:36 +0000497int TargetTransformInfo::getMinMaxReductionCost(Type *Ty, Type *CondTy,
498 bool IsPairwiseForm,
499 bool IsUnsigned) const {
500 int Cost =
501 TTIImpl->getMinMaxReductionCost(Ty, CondTy, IsPairwiseForm, IsUnsigned);
502 assert(Cost >= 0 && "TTI should not produce negative costs!");
503 return Cost;
504}
505
Chandler Carruth705b1852015-01-31 03:43:40 +0000506unsigned
507TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
508 return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
Chad Rosierf9327d62015-01-26 22:51:15 +0000509}
510
511bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst,
512 MemIntrinsicInfo &Info) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000513 return TTIImpl->getTgtMemIntrinsic(Inst, Info);
Chad Rosierf9327d62015-01-26 22:51:15 +0000514}
515
Anna Thomasb2a212c2017-06-06 16:45:25 +0000516unsigned TargetTransformInfo::getAtomicMemIntrinsicMaxElementSize() const {
517 return TTIImpl->getAtomicMemIntrinsicMaxElementSize();
518}
519
Chandler Carruth705b1852015-01-31 03:43:40 +0000520Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic(
521 IntrinsicInst *Inst, Type *ExpectedType) const {
522 return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
523}
524
Sean Fertile9cd1cdf2017-07-07 02:00:06 +0000525Type *TargetTransformInfo::getMemcpyLoopLoweringType(LLVMContext &Context,
526 Value *Length,
527 unsigned SrcAlign,
528 unsigned DestAlign) const {
529 return TTIImpl->getMemcpyLoopLoweringType(Context, Length, SrcAlign,
530 DestAlign);
531}
532
533void TargetTransformInfo::getMemcpyLoopResidualLoweringType(
534 SmallVectorImpl<Type *> &OpsOut, LLVMContext &Context,
535 unsigned RemainingBytes, unsigned SrcAlign, unsigned DestAlign) const {
536 TTIImpl->getMemcpyLoopResidualLoweringType(OpsOut, Context, RemainingBytes,
537 SrcAlign, DestAlign);
538}
539
540bool TargetTransformInfo::useWideIRMemcpyLoopLowering() const {
541 return UseWideMemcpyLoopLowering;
542}
543
Eric Christopherd566fb12015-07-29 22:09:48 +0000544bool TargetTransformInfo::areInlineCompatible(const Function *Caller,
545 const Function *Callee) const {
546 return TTIImpl->areInlineCompatible(Caller, Callee);
Eric Christopher4371b132015-07-02 01:11:47 +0000547}
548
Volkan Keles1c386812016-10-03 10:31:34 +0000549unsigned TargetTransformInfo::getLoadStoreVecRegBitWidth(unsigned AS) const {
550 return TTIImpl->getLoadStoreVecRegBitWidth(AS);
551}
552
553bool TargetTransformInfo::isLegalToVectorizeLoad(LoadInst *LI) const {
554 return TTIImpl->isLegalToVectorizeLoad(LI);
555}
556
557bool TargetTransformInfo::isLegalToVectorizeStore(StoreInst *SI) const {
558 return TTIImpl->isLegalToVectorizeStore(SI);
559}
560
561bool TargetTransformInfo::isLegalToVectorizeLoadChain(
562 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
563 return TTIImpl->isLegalToVectorizeLoadChain(ChainSizeInBytes, Alignment,
564 AddrSpace);
565}
566
567bool TargetTransformInfo::isLegalToVectorizeStoreChain(
568 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
569 return TTIImpl->isLegalToVectorizeStoreChain(ChainSizeInBytes, Alignment,
570 AddrSpace);
571}
572
573unsigned TargetTransformInfo::getLoadVectorFactor(unsigned VF,
574 unsigned LoadSize,
575 unsigned ChainSizeInBytes,
576 VectorType *VecTy) const {
577 return TTIImpl->getLoadVectorFactor(VF, LoadSize, ChainSizeInBytes, VecTy);
578}
579
580unsigned TargetTransformInfo::getStoreVectorFactor(unsigned VF,
581 unsigned StoreSize,
582 unsigned ChainSizeInBytes,
583 VectorType *VecTy) const {
584 return TTIImpl->getStoreVectorFactor(VF, StoreSize, ChainSizeInBytes, VecTy);
585}
586
Amara Emersoncf9daa32017-05-09 10:43:25 +0000587bool TargetTransformInfo::useReductionIntrinsic(unsigned Opcode,
588 Type *Ty, ReductionFlags Flags) const {
589 return TTIImpl->useReductionIntrinsic(Opcode, Ty, Flags);
590}
591
Amara Emerson836b0f42017-05-10 09:42:49 +0000592bool TargetTransformInfo::shouldExpandReduction(const IntrinsicInst *II) const {
593 return TTIImpl->shouldExpandReduction(II);
594}
Amara Emersoncf9daa32017-05-09 10:43:25 +0000595
Guozhi Wei62d64142017-09-08 22:29:17 +0000596int TargetTransformInfo::getInstructionLatency(const Instruction *I) const {
597 return TTIImpl->getInstructionLatency(I);
598}
599
600static bool isReverseVectorMask(ArrayRef<int> Mask) {
601 for (unsigned i = 0, MaskSize = Mask.size(); i < MaskSize; ++i)
602 if (Mask[i] >= 0 && Mask[i] != (int)(MaskSize - 1 - i))
603 return false;
604 return true;
605}
606
607static bool isSingleSourceVectorMask(ArrayRef<int> Mask) {
608 bool Vec0 = false;
609 bool Vec1 = false;
610 for (unsigned i = 0, NumVecElts = Mask.size(); i < NumVecElts; ++i) {
611 if (Mask[i] >= 0) {
612 if ((unsigned)Mask[i] >= NumVecElts)
613 Vec1 = true;
614 else
615 Vec0 = true;
616 }
617 }
618 return !(Vec0 && Vec1);
619}
620
621static bool isZeroEltBroadcastVectorMask(ArrayRef<int> Mask) {
622 for (unsigned i = 0; i < Mask.size(); ++i)
623 if (Mask[i] > 0)
624 return false;
625 return true;
626}
627
628static bool isAlternateVectorMask(ArrayRef<int> Mask) {
629 bool isAlternate = true;
630 unsigned MaskSize = Mask.size();
631
632 // Example: shufflevector A, B, <0,5,2,7>
633 for (unsigned i = 0; i < MaskSize && isAlternate; ++i) {
634 if (Mask[i] < 0)
635 continue;
636 isAlternate = Mask[i] == (int)((i & 1) ? MaskSize + i : i);
637 }
638
639 if (isAlternate)
640 return true;
641
642 isAlternate = true;
643 // Example: shufflevector A, B, <4,1,6,3>
644 for (unsigned i = 0; i < MaskSize && isAlternate; ++i) {
645 if (Mask[i] < 0)
646 continue;
647 isAlternate = Mask[i] == (int)((i & 1) ? i : MaskSize + i);
648 }
649
650 return isAlternate;
651}
652
653static TargetTransformInfo::OperandValueKind getOperandInfo(Value *V) {
654 TargetTransformInfo::OperandValueKind OpInfo =
655 TargetTransformInfo::OK_AnyValue;
656
657 // Check for a splat of a constant or for a non uniform vector of constants.
658 if (isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) {
659 OpInfo = TargetTransformInfo::OK_NonUniformConstantValue;
660 if (cast<Constant>(V)->getSplatValue() != nullptr)
661 OpInfo = TargetTransformInfo::OK_UniformConstantValue;
662 }
663
664 // Check for a splat of a uniform value. This is not loop aware, so return
665 // true only for the obviously uniform cases (argument, globalvalue)
666 const Value *Splat = getSplatValue(V);
667 if (Splat && (isa<Argument>(Splat) || isa<GlobalValue>(Splat)))
668 OpInfo = TargetTransformInfo::OK_UniformValue;
669
670 return OpInfo;
671}
672
673static bool matchPairwiseShuffleMask(ShuffleVectorInst *SI, bool IsLeft,
674 unsigned Level) {
675 // We don't need a shuffle if we just want to have element 0 in position 0 of
676 // the vector.
677 if (!SI && Level == 0 && IsLeft)
678 return true;
679 else if (!SI)
680 return false;
681
682 SmallVector<int, 32> Mask(SI->getType()->getVectorNumElements(), -1);
683
684 // Build a mask of 0, 2, ... (left) or 1, 3, ... (right) depending on whether
685 // we look at the left or right side.
686 for (unsigned i = 0, e = (1 << Level), val = !IsLeft; i != e; ++i, val += 2)
687 Mask[i] = val;
688
689 SmallVector<int, 16> ActualMask = SI->getShuffleMask();
690 return Mask == ActualMask;
691}
692
693namespace {
694/// Kind of the reduction data.
695enum ReductionKind {
696 RK_None, /// Not a reduction.
697 RK_Arithmetic, /// Binary reduction data.
698 RK_MinMax, /// Min/max reduction data.
699 RK_UnsignedMinMax, /// Unsigned min/max reduction data.
700};
701/// Contains opcode + LHS/RHS parts of the reduction operations.
702struct ReductionData {
703 ReductionData() = delete;
704 ReductionData(ReductionKind Kind, unsigned Opcode, Value *LHS, Value *RHS)
705 : Opcode(Opcode), LHS(LHS), RHS(RHS), Kind(Kind) {
706 assert(Kind != RK_None && "expected binary or min/max reduction only.");
707 }
708 unsigned Opcode = 0;
709 Value *LHS = nullptr;
710 Value *RHS = nullptr;
711 ReductionKind Kind = RK_None;
712 bool hasSameData(ReductionData &RD) const {
713 return Kind == RD.Kind && Opcode == RD.Opcode;
714 }
715};
716} // namespace
717
718static Optional<ReductionData> getReductionData(Instruction *I) {
719 Value *L, *R;
720 if (m_BinOp(m_Value(L), m_Value(R)).match(I))
721 return ReductionData(RK_Arithmetic, I->getOpcode(), L, R);
722 if (auto *SI = dyn_cast<SelectInst>(I)) {
723 if (m_SMin(m_Value(L), m_Value(R)).match(SI) ||
724 m_SMax(m_Value(L), m_Value(R)).match(SI) ||
725 m_OrdFMin(m_Value(L), m_Value(R)).match(SI) ||
726 m_OrdFMax(m_Value(L), m_Value(R)).match(SI) ||
727 m_UnordFMin(m_Value(L), m_Value(R)).match(SI) ||
728 m_UnordFMax(m_Value(L), m_Value(R)).match(SI)) {
729 auto *CI = cast<CmpInst>(SI->getCondition());
730 return ReductionData(RK_MinMax, CI->getOpcode(), L, R);
731 }
732 if (m_UMin(m_Value(L), m_Value(R)).match(SI) ||
733 m_UMax(m_Value(L), m_Value(R)).match(SI)) {
734 auto *CI = cast<CmpInst>(SI->getCondition());
735 return ReductionData(RK_UnsignedMinMax, CI->getOpcode(), L, R);
736 }
737 }
738 return llvm::None;
739}
740
741static ReductionKind matchPairwiseReductionAtLevel(Instruction *I,
742 unsigned Level,
743 unsigned NumLevels) {
744 // Match one level of pairwise operations.
745 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef,
746 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef>
747 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef,
748 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef>
749 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1
750 if (!I)
751 return RK_None;
752
753 assert(I->getType()->isVectorTy() && "Expecting a vector type");
754
755 Optional<ReductionData> RD = getReductionData(I);
756 if (!RD)
757 return RK_None;
758
759 ShuffleVectorInst *LS = dyn_cast<ShuffleVectorInst>(RD->LHS);
760 if (!LS && Level)
761 return RK_None;
762 ShuffleVectorInst *RS = dyn_cast<ShuffleVectorInst>(RD->RHS);
763 if (!RS && Level)
764 return RK_None;
765
766 // On level 0 we can omit one shufflevector instruction.
767 if (!Level && !RS && !LS)
768 return RK_None;
769
770 // Shuffle inputs must match.
771 Value *NextLevelOpL = LS ? LS->getOperand(0) : nullptr;
772 Value *NextLevelOpR = RS ? RS->getOperand(0) : nullptr;
773 Value *NextLevelOp = nullptr;
774 if (NextLevelOpR && NextLevelOpL) {
775 // If we have two shuffles their operands must match.
776 if (NextLevelOpL != NextLevelOpR)
777 return RK_None;
778
779 NextLevelOp = NextLevelOpL;
780 } else if (Level == 0 && (NextLevelOpR || NextLevelOpL)) {
781 // On the first level we can omit the shufflevector <0, undef,...>. So the
782 // input to the other shufflevector <1, undef> must match with one of the
783 // inputs to the current binary operation.
784 // Example:
785 // %NextLevelOpL = shufflevector %R, <1, undef ...>
786 // %BinOp = fadd %NextLevelOpL, %R
787 if (NextLevelOpL && NextLevelOpL != RD->RHS)
788 return RK_None;
789 else if (NextLevelOpR && NextLevelOpR != RD->LHS)
790 return RK_None;
791
792 NextLevelOp = NextLevelOpL ? RD->RHS : RD->LHS;
793 } else
794 return RK_None;
795
796 // Check that the next levels binary operation exists and matches with the
797 // current one.
798 if (Level + 1 != NumLevels) {
799 Optional<ReductionData> NextLevelRD =
800 getReductionData(cast<Instruction>(NextLevelOp));
801 if (!NextLevelRD || !RD->hasSameData(*NextLevelRD))
802 return RK_None;
803 }
804
805 // Shuffle mask for pairwise operation must match.
806 if (matchPairwiseShuffleMask(LS, /*IsLeft=*/true, Level)) {
807 if (!matchPairwiseShuffleMask(RS, /*IsLeft=*/false, Level))
808 return RK_None;
809 } else if (matchPairwiseShuffleMask(RS, /*IsLeft=*/true, Level)) {
810 if (!matchPairwiseShuffleMask(LS, /*IsLeft=*/false, Level))
811 return RK_None;
812 } else {
813 return RK_None;
814 }
815
816 if (++Level == NumLevels)
817 return RD->Kind;
818
819 // Match next level.
820 return matchPairwiseReductionAtLevel(cast<Instruction>(NextLevelOp), Level,
821 NumLevels);
822}
823
824static ReductionKind matchPairwiseReduction(const ExtractElementInst *ReduxRoot,
825 unsigned &Opcode, Type *&Ty) {
826 if (!EnableReduxCost)
827 return RK_None;
828
829 // Need to extract the first element.
830 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1));
831 unsigned Idx = ~0u;
832 if (CI)
833 Idx = CI->getZExtValue();
834 if (Idx != 0)
835 return RK_None;
836
837 auto *RdxStart = dyn_cast<Instruction>(ReduxRoot->getOperand(0));
838 if (!RdxStart)
839 return RK_None;
840 Optional<ReductionData> RD = getReductionData(RdxStart);
841 if (!RD)
842 return RK_None;
843
844 Type *VecTy = RdxStart->getType();
845 unsigned NumVecElems = VecTy->getVectorNumElements();
846 if (!isPowerOf2_32(NumVecElems))
847 return RK_None;
848
849 // We look for a sequence of shuffle,shuffle,add triples like the following
850 // that builds a pairwise reduction tree.
851 //
852 // (X0, X1, X2, X3)
853 // (X0 + X1, X2 + X3, undef, undef)
854 // ((X0 + X1) + (X2 + X3), undef, undef, undef)
855 //
856 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef,
857 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef>
858 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef,
859 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef>
860 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1
861 // %rdx.shuf.1.0 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef,
862 // <4 x i32> <i32 0, i32 undef, i32 undef, i32 undef>
863 // %rdx.shuf.1.1 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef,
864 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
865 // %bin.rdx8 = fadd <4 x float> %rdx.shuf.1.0, %rdx.shuf.1.1
866 // %r = extractelement <4 x float> %bin.rdx8, i32 0
867 if (matchPairwiseReductionAtLevel(RdxStart, 0, Log2_32(NumVecElems)) ==
868 RK_None)
869 return RK_None;
870
871 Opcode = RD->Opcode;
872 Ty = VecTy;
873
874 return RD->Kind;
875}
876
877static std::pair<Value *, ShuffleVectorInst *>
878getShuffleAndOtherOprd(Value *L, Value *R) {
879 ShuffleVectorInst *S = nullptr;
880
881 if ((S = dyn_cast<ShuffleVectorInst>(L)))
882 return std::make_pair(R, S);
883
884 S = dyn_cast<ShuffleVectorInst>(R);
885 return std::make_pair(L, S);
886}
887
888static ReductionKind
889matchVectorSplittingReduction(const ExtractElementInst *ReduxRoot,
890 unsigned &Opcode, Type *&Ty) {
891 if (!EnableReduxCost)
892 return RK_None;
893
894 // Need to extract the first element.
895 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1));
896 unsigned Idx = ~0u;
897 if (CI)
898 Idx = CI->getZExtValue();
899 if (Idx != 0)
900 return RK_None;
901
902 auto *RdxStart = dyn_cast<Instruction>(ReduxRoot->getOperand(0));
903 if (!RdxStart)
904 return RK_None;
905 Optional<ReductionData> RD = getReductionData(RdxStart);
906 if (!RD)
907 return RK_None;
908
909 Type *VecTy = ReduxRoot->getOperand(0)->getType();
910 unsigned NumVecElems = VecTy->getVectorNumElements();
911 if (!isPowerOf2_32(NumVecElems))
912 return RK_None;
913
914 // We look for a sequence of shuffles and adds like the following matching one
915 // fadd, shuffle vector pair at a time.
916 //
917 // %rdx.shuf = shufflevector <4 x float> %rdx, <4 x float> undef,
918 // <4 x i32> <i32 2, i32 3, i32 undef, i32 undef>
919 // %bin.rdx = fadd <4 x float> %rdx, %rdx.shuf
920 // %rdx.shuf7 = shufflevector <4 x float> %bin.rdx, <4 x float> undef,
921 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
922 // %bin.rdx8 = fadd <4 x float> %bin.rdx, %rdx.shuf7
923 // %r = extractelement <4 x float> %bin.rdx8, i32 0
924
925 unsigned MaskStart = 1;
926 Instruction *RdxOp = RdxStart;
927 SmallVector<int, 32> ShuffleMask(NumVecElems, 0);
928 unsigned NumVecElemsRemain = NumVecElems;
929 while (NumVecElemsRemain - 1) {
930 // Check for the right reduction operation.
931 if (!RdxOp)
932 return RK_None;
933 Optional<ReductionData> RDLevel = getReductionData(RdxOp);
934 if (!RDLevel || !RDLevel->hasSameData(*RD))
935 return RK_None;
936
937 Value *NextRdxOp;
938 ShuffleVectorInst *Shuffle;
939 std::tie(NextRdxOp, Shuffle) =
940 getShuffleAndOtherOprd(RDLevel->LHS, RDLevel->RHS);
941
942 // Check the current reduction operation and the shuffle use the same value.
943 if (Shuffle == nullptr)
944 return RK_None;
945 if (Shuffle->getOperand(0) != NextRdxOp)
946 return RK_None;
947
948 // Check that shuffle masks matches.
949 for (unsigned j = 0; j != MaskStart; ++j)
950 ShuffleMask[j] = MaskStart + j;
951 // Fill the rest of the mask with -1 for undef.
952 std::fill(&ShuffleMask[MaskStart], ShuffleMask.end(), -1);
953
954 SmallVector<int, 16> Mask = Shuffle->getShuffleMask();
955 if (ShuffleMask != Mask)
956 return RK_None;
957
958 RdxOp = dyn_cast<Instruction>(NextRdxOp);
959 NumVecElemsRemain /= 2;
960 MaskStart *= 2;
961 }
962
963 Opcode = RD->Opcode;
964 Ty = VecTy;
965 return RD->Kind;
966}
967
968int TargetTransformInfo::getInstructionThroughput(const Instruction *I) const {
969 switch (I->getOpcode()) {
970 case Instruction::GetElementPtr:
971 return getUserCost(I);
972
973 case Instruction::Ret:
974 case Instruction::PHI:
975 case Instruction::Br: {
976 return getCFInstrCost(I->getOpcode());
977 }
978 case Instruction::Add:
979 case Instruction::FAdd:
980 case Instruction::Sub:
981 case Instruction::FSub:
982 case Instruction::Mul:
983 case Instruction::FMul:
984 case Instruction::UDiv:
985 case Instruction::SDiv:
986 case Instruction::FDiv:
987 case Instruction::URem:
988 case Instruction::SRem:
989 case Instruction::FRem:
990 case Instruction::Shl:
991 case Instruction::LShr:
992 case Instruction::AShr:
993 case Instruction::And:
994 case Instruction::Or:
995 case Instruction::Xor: {
996 TargetTransformInfo::OperandValueKind Op1VK =
997 getOperandInfo(I->getOperand(0));
998 TargetTransformInfo::OperandValueKind Op2VK =
999 getOperandInfo(I->getOperand(1));
1000 SmallVector<const Value*, 2> Operands(I->operand_values());
1001 return getArithmeticInstrCost(I->getOpcode(), I->getType(), Op1VK,
1002 Op2VK, TargetTransformInfo::OP_None,
1003 TargetTransformInfo::OP_None,
1004 Operands);
1005 }
1006 case Instruction::Select: {
1007 const SelectInst *SI = cast<SelectInst>(I);
1008 Type *CondTy = SI->getCondition()->getType();
1009 return getCmpSelInstrCost(I->getOpcode(), I->getType(), CondTy, I);
1010 }
1011 case Instruction::ICmp:
1012 case Instruction::FCmp: {
1013 Type *ValTy = I->getOperand(0)->getType();
1014 return getCmpSelInstrCost(I->getOpcode(), ValTy, I->getType(), I);
1015 }
1016 case Instruction::Store: {
1017 const StoreInst *SI = cast<StoreInst>(I);
1018 Type *ValTy = SI->getValueOperand()->getType();
1019 return getMemoryOpCost(I->getOpcode(), ValTy,
1020 SI->getAlignment(),
1021 SI->getPointerAddressSpace(), I);
1022 }
1023 case Instruction::Load: {
1024 const LoadInst *LI = cast<LoadInst>(I);
1025 return getMemoryOpCost(I->getOpcode(), I->getType(),
1026 LI->getAlignment(),
1027 LI->getPointerAddressSpace(), I);
1028 }
1029 case Instruction::ZExt:
1030 case Instruction::SExt:
1031 case Instruction::FPToUI:
1032 case Instruction::FPToSI:
1033 case Instruction::FPExt:
1034 case Instruction::PtrToInt:
1035 case Instruction::IntToPtr:
1036 case Instruction::SIToFP:
1037 case Instruction::UIToFP:
1038 case Instruction::Trunc:
1039 case Instruction::FPTrunc:
1040 case Instruction::BitCast:
1041 case Instruction::AddrSpaceCast: {
1042 Type *SrcTy = I->getOperand(0)->getType();
1043 return getCastInstrCost(I->getOpcode(), I->getType(), SrcTy, I);
1044 }
1045 case Instruction::ExtractElement: {
1046 const ExtractElementInst * EEI = cast<ExtractElementInst>(I);
1047 ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1));
1048 unsigned Idx = -1;
1049 if (CI)
1050 Idx = CI->getZExtValue();
1051
1052 // Try to match a reduction sequence (series of shufflevector and vector
1053 // adds followed by a extractelement).
1054 unsigned ReduxOpCode;
1055 Type *ReduxType;
1056
1057 switch (matchVectorSplittingReduction(EEI, ReduxOpCode, ReduxType)) {
1058 case RK_Arithmetic:
1059 return getArithmeticReductionCost(ReduxOpCode, ReduxType,
1060 /*IsPairwiseForm=*/false);
1061 case RK_MinMax:
1062 return getMinMaxReductionCost(
1063 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1064 /*IsPairwiseForm=*/false, /*IsUnsigned=*/false);
1065 case RK_UnsignedMinMax:
1066 return getMinMaxReductionCost(
1067 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1068 /*IsPairwiseForm=*/false, /*IsUnsigned=*/true);
1069 case RK_None:
1070 break;
1071 }
1072
1073 switch (matchPairwiseReduction(EEI, ReduxOpCode, ReduxType)) {
1074 case RK_Arithmetic:
1075 return getArithmeticReductionCost(ReduxOpCode, ReduxType,
1076 /*IsPairwiseForm=*/true);
1077 case RK_MinMax:
1078 return getMinMaxReductionCost(
1079 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1080 /*IsPairwiseForm=*/true, /*IsUnsigned=*/false);
1081 case RK_UnsignedMinMax:
1082 return getMinMaxReductionCost(
1083 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1084 /*IsPairwiseForm=*/true, /*IsUnsigned=*/true);
1085 case RK_None:
1086 break;
1087 }
1088
1089 return getVectorInstrCost(I->getOpcode(),
1090 EEI->getOperand(0)->getType(), Idx);
1091 }
1092 case Instruction::InsertElement: {
1093 const InsertElementInst * IE = cast<InsertElementInst>(I);
1094 ConstantInt *CI = dyn_cast<ConstantInt>(IE->getOperand(2));
1095 unsigned Idx = -1;
1096 if (CI)
1097 Idx = CI->getZExtValue();
1098 return getVectorInstrCost(I->getOpcode(),
1099 IE->getType(), Idx);
1100 }
1101 case Instruction::ShuffleVector: {
1102 const ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
1103 Type *VecTypOp0 = Shuffle->getOperand(0)->getType();
1104 unsigned NumVecElems = VecTypOp0->getVectorNumElements();
1105 SmallVector<int, 16> Mask = Shuffle->getShuffleMask();
1106
1107 if (NumVecElems == Mask.size()) {
1108 if (isReverseVectorMask(Mask))
1109 return getShuffleCost(TargetTransformInfo::SK_Reverse, VecTypOp0,
1110 0, nullptr);
1111 if (isAlternateVectorMask(Mask))
1112 return getShuffleCost(TargetTransformInfo::SK_Alternate,
1113 VecTypOp0, 0, nullptr);
1114
1115 if (isZeroEltBroadcastVectorMask(Mask))
1116 return getShuffleCost(TargetTransformInfo::SK_Broadcast,
1117 VecTypOp0, 0, nullptr);
1118
1119 if (isSingleSourceVectorMask(Mask))
1120 return getShuffleCost(TargetTransformInfo::SK_PermuteSingleSrc,
1121 VecTypOp0, 0, nullptr);
1122
1123 return getShuffleCost(TargetTransformInfo::SK_PermuteTwoSrc,
1124 VecTypOp0, 0, nullptr);
1125 }
1126
1127 return -1;
1128 }
1129 case Instruction::Call:
1130 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1131 SmallVector<Value *, 4> Args(II->arg_operands());
1132
1133 FastMathFlags FMF;
1134 if (auto *FPMO = dyn_cast<FPMathOperator>(II))
1135 FMF = FPMO->getFastMathFlags();
1136
1137 return getIntrinsicInstrCost(II->getIntrinsicID(), II->getType(),
1138 Args, FMF);
1139 }
1140 return -1;
1141 default:
1142 // We don't have any information on this instruction.
1143 return -1;
1144 }
1145}
1146
Chandler Carruth705b1852015-01-31 03:43:40 +00001147TargetTransformInfo::Concept::~Concept() {}
1148
Chandler Carruthe0385522015-02-01 10:11:22 +00001149TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
1150
1151TargetIRAnalysis::TargetIRAnalysis(
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001152 std::function<Result(const Function &)> TTICallback)
Benjamin Kramer82de7d32016-05-27 14:27:24 +00001153 : TTICallback(std::move(TTICallback)) {}
Chandler Carruthe0385522015-02-01 10:11:22 +00001154
Chandler Carruth164a2aa62016-06-17 00:11:01 +00001155TargetIRAnalysis::Result TargetIRAnalysis::run(const Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +00001156 FunctionAnalysisManager &) {
Chandler Carruthe0385522015-02-01 10:11:22 +00001157 return TTICallback(F);
1158}
1159
Chandler Carruthdab4eae2016-11-23 17:53:26 +00001160AnalysisKey TargetIRAnalysis::Key;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +00001161
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001162TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(const Function &F) {
Mehdi Amini5010ebf2015-07-09 02:08:42 +00001163 return Result(F.getParent()->getDataLayout());
Chandler Carruthe0385522015-02-01 10:11:22 +00001164}
1165
Chandler Carruth705b1852015-01-31 03:43:40 +00001166// Register the basic pass.
1167INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
1168 "Target Transform Information", false, true)
1169char TargetTransformInfoWrapperPass::ID = 0;
Chandler Carruth539edf42013-01-05 11:43:11 +00001170
Chandler Carruth705b1852015-01-31 03:43:40 +00001171void TargetTransformInfoWrapperPass::anchor() {}
Chandler Carruth539edf42013-01-05 11:43:11 +00001172
Chandler Carruth705b1852015-01-31 03:43:40 +00001173TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass()
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001174 : ImmutablePass(ID) {
Chandler Carruth705b1852015-01-31 03:43:40 +00001175 initializeTargetTransformInfoWrapperPassPass(
1176 *PassRegistry::getPassRegistry());
1177}
1178
1179TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass(
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001180 TargetIRAnalysis TIRA)
1181 : ImmutablePass(ID), TIRA(std::move(TIRA)) {
Chandler Carruth705b1852015-01-31 03:43:40 +00001182 initializeTargetTransformInfoWrapperPassPass(
1183 *PassRegistry::getPassRegistry());
1184}
1185
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001186TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(const Function &F) {
Sean Silva36e0d012016-08-09 00:28:15 +00001187 FunctionAnalysisManager DummyFAM;
Chandler Carruth164a2aa62016-06-17 00:11:01 +00001188 TTI = TIRA.run(F, DummyFAM);
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001189 return *TTI;
1190}
1191
Chandler Carruth93dcdc42015-01-31 11:17:59 +00001192ImmutablePass *
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001193llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) {
1194 return new TargetTransformInfoWrapperPass(std::move(TIRA));
Chandler Carruth539edf42013-01-05 11:43:11 +00001195}