blob: 6dbdaeeae5f8a94005cceb1800aeea85efcc6c77 [file] [log] [blame]
Chandler Carruthd3e73552013-01-07 03:08:10 +00001//===- llvm/Analysis/TargetTransformInfo.cpp ------------------------------===//
Nadav Rotem5dc203e2012-10-18 23:22:48 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Nadav Rotem5dc203e2012-10-18 23:22:48 +00006//
7//===----------------------------------------------------------------------===//
8
Chandler Carruthd3e73552013-01-07 03:08:10 +00009#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruth705b1852015-01-31 03:43:40 +000010#include "llvm/Analysis/TargetTransformInfoImpl.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000011#include "llvm/IR/CallSite.h"
Chandler Carruth511aa762013-01-21 01:27:39 +000012#include "llvm/IR/DataLayout.h"
Chandler Carruth511aa762013-01-21 01:27:39 +000013#include "llvm/IR/Instruction.h"
Chandler Carruth511aa762013-01-21 01:27:39 +000014#include "llvm/IR/Instructions.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000015#include "llvm/IR/IntrinsicInst.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000016#include "llvm/IR/Module.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000017#include "llvm/IR/Operator.h"
Guozhi Wei62d64142017-09-08 22:29:17 +000018#include "llvm/IR/PatternMatch.h"
Sean Fertile9cd1cdf2017-07-07 02:00:06 +000019#include "llvm/Support/CommandLine.h"
Nadav Rotem5dc203e2012-10-18 23:22:48 +000020#include "llvm/Support/ErrorHandling.h"
Chen Zheng46ce9e42019-06-26 09:12:52 +000021#include "llvm/Analysis/CFG.h"
22#include "llvm/Analysis/LoopIterator.h"
Benjamin Kramer82de7d32016-05-27 14:27:24 +000023#include <utility>
Nadav Rotem5dc203e2012-10-18 23:22:48 +000024
25using namespace llvm;
Guozhi Wei62d64142017-09-08 22:29:17 +000026using namespace PatternMatch;
Nadav Rotem5dc203e2012-10-18 23:22:48 +000027
Chandler Carruthf1221bd2014-04-22 02:48:03 +000028#define DEBUG_TYPE "tti"
29
Guozhi Wei62d64142017-09-08 22:29:17 +000030static cl::opt<bool> EnableReduxCost("costmodel-reduxcost", cl::init(false),
31 cl::Hidden,
32 cl::desc("Recognize reduction patterns."));
33
Chandler Carruth93dcdc42015-01-31 11:17:59 +000034namespace {
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000035/// No-op implementation of the TTI interface using the utility base
Chandler Carruth93dcdc42015-01-31 11:17:59 +000036/// classes.
37///
38/// This is used when no target specific information is available.
39struct NoTTIImpl : TargetTransformInfoImplCRTPBase<NoTTIImpl> {
Mehdi Amini5010ebf2015-07-09 02:08:42 +000040 explicit NoTTIImpl(const DataLayout &DL)
Chandler Carruth93dcdc42015-01-31 11:17:59 +000041 : TargetTransformInfoImplCRTPBase<NoTTIImpl>(DL) {}
42};
43}
44
Chen Zhengaa999522019-06-26 12:02:43 +000045bool HardwareLoopInfo::canAnalyze(LoopInfo &LI) {
46 // If the loop has irreducible control flow, it can not be converted to
47 // Hardware loop.
48 LoopBlocksRPO RPOT(L);
49 RPOT.perform(&LI);
50 if (containsIrreducibleCFG<const BasicBlock *>(RPOT, LI))
51 return false;
52 return true;
53}
54
Chen Zhengc5b918d2019-06-19 01:26:31 +000055bool HardwareLoopInfo::isHardwareLoopCandidate(ScalarEvolution &SE,
56 LoopInfo &LI, DominatorTree &DT,
57 bool ForceNestedLoop,
58 bool ForceHardwareLoopPHI) {
59 SmallVector<BasicBlock *, 4> ExitingBlocks;
60 L->getExitingBlocks(ExitingBlocks);
61
62 for (SmallVectorImpl<BasicBlock *>::iterator I = ExitingBlocks.begin(),
63 IE = ExitingBlocks.end();
64 I != IE; ++I) {
65 BasicBlock *BB = *I;
66
67 // If we pass the updated counter back through a phi, we need to know
68 // which latch the updated value will be coming from.
69 if (!L->isLoopLatch(BB)) {
70 if (ForceHardwareLoopPHI || CounterInReg)
71 continue;
72 }
73
74 const SCEV *EC = SE.getExitCount(L, BB);
75 if (isa<SCEVCouldNotCompute>(EC))
76 continue;
77 if (const SCEVConstant *ConstEC = dyn_cast<SCEVConstant>(EC)) {
78 if (ConstEC->getValue()->isZero())
79 continue;
80 } else if (!SE.isLoopInvariant(EC, L))
81 continue;
82
83 if (SE.getTypeSizeInBits(EC->getType()) > CountType->getBitWidth())
84 continue;
85
86 // If this exiting block is contained in a nested loop, it is not eligible
87 // for insertion of the branch-and-decrement since the inner loop would
88 // end up messing up the value in the CTR.
89 if (!IsNestingLegal && LI.getLoopFor(BB) != L && !ForceNestedLoop)
90 continue;
91
92 // We now have a loop-invariant count of loop iterations (which is not the
93 // constant zero) for which we know that this loop will not exit via this
94 // existing block.
95
96 // We need to make sure that this block will run on every loop iteration.
97 // For this to be true, we must dominate all blocks with backedges. Such
98 // blocks are in-loop predecessors to the header block.
99 bool NotAlways = false;
100 for (pred_iterator PI = pred_begin(L->getHeader()),
101 PIE = pred_end(L->getHeader());
102 PI != PIE; ++PI) {
103 if (!L->contains(*PI))
104 continue;
105
106 if (!DT.dominates(*I, *PI)) {
107 NotAlways = true;
108 break;
109 }
110 }
111
112 if (NotAlways)
113 continue;
114
115 // Make sure this blocks ends with a conditional branch.
116 Instruction *TI = BB->getTerminator();
117 if (!TI)
118 continue;
119
120 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
121 if (!BI->isConditional())
122 continue;
123
124 ExitBranch = BI;
125 } else
126 continue;
127
128 // Note that this block may not be the loop latch block, even if the loop
129 // has a latch block.
130 ExitBlock = *I;
131 ExitCount = EC;
132 break;
133 }
134
135 if (!ExitBlock)
136 return false;
137 return true;
138}
139
Mehdi Amini5010ebf2015-07-09 02:08:42 +0000140TargetTransformInfo::TargetTransformInfo(const DataLayout &DL)
Chandler Carruth93dcdc42015-01-31 11:17:59 +0000141 : TTIImpl(new Model<NoTTIImpl>(NoTTIImpl(DL))) {}
142
Chandler Carruth705b1852015-01-31 03:43:40 +0000143TargetTransformInfo::~TargetTransformInfo() {}
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000144
Chandler Carruth705b1852015-01-31 03:43:40 +0000145TargetTransformInfo::TargetTransformInfo(TargetTransformInfo &&Arg)
146 : TTIImpl(std::move(Arg.TTIImpl)) {}
Chandler Carruth539edf42013-01-05 11:43:11 +0000147
Chandler Carruth705b1852015-01-31 03:43:40 +0000148TargetTransformInfo &TargetTransformInfo::operator=(TargetTransformInfo &&RHS) {
149 TTIImpl = std::move(RHS.TTIImpl);
150 return *this;
Chandler Carruth539edf42013-01-05 11:43:11 +0000151}
152
Chandler Carruth93205eb2015-08-05 18:08:10 +0000153int TargetTransformInfo::getOperationCost(unsigned Opcode, Type *Ty,
154 Type *OpTy) const {
155 int Cost = TTIImpl->getOperationCost(Opcode, Ty, OpTy);
156 assert(Cost >= 0 && "TTI should not produce negative costs!");
157 return Cost;
Chandler Carruth511aa762013-01-21 01:27:39 +0000158}
159
Sjoerd Meijer31ff6472019-03-12 09:48:02 +0000160int TargetTransformInfo::getCallCost(FunctionType *FTy, int NumArgs,
161 const User *U) const {
162 int Cost = TTIImpl->getCallCost(FTy, NumArgs, U);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000163 assert(Cost >= 0 && "TTI should not produce negative costs!");
164 return Cost;
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000165}
166
Chandler Carruth93205eb2015-08-05 18:08:10 +0000167int TargetTransformInfo::getCallCost(const Function *F,
Sjoerd Meijer31ff6472019-03-12 09:48:02 +0000168 ArrayRef<const Value *> Arguments,
169 const User *U) const {
170 int Cost = TTIImpl->getCallCost(F, Arguments, U);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000171 assert(Cost >= 0 && "TTI should not produce negative costs!");
172 return Cost;
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000173}
174
Justin Lebar8650a4d2016-04-15 01:38:48 +0000175unsigned TargetTransformInfo::getInliningThresholdMultiplier() const {
176 return TTIImpl->getInliningThresholdMultiplier();
177}
178
Jingyue Wu15f3e822016-07-08 21:48:05 +0000179int TargetTransformInfo::getGEPCost(Type *PointeeType, const Value *Ptr,
180 ArrayRef<const Value *> Operands) const {
181 return TTIImpl->getGEPCost(PointeeType, Ptr, Operands);
182}
183
Haicheng Wuabdef9e2017-07-15 02:12:16 +0000184int TargetTransformInfo::getExtCost(const Instruction *I,
185 const Value *Src) const {
186 return TTIImpl->getExtCost(I, Src);
187}
188
Chandler Carruth93205eb2015-08-05 18:08:10 +0000189int TargetTransformInfo::getIntrinsicCost(
Sjoerd Meijer31ff6472019-03-12 09:48:02 +0000190 Intrinsic::ID IID, Type *RetTy, ArrayRef<const Value *> Arguments,
191 const User *U) const {
192 int Cost = TTIImpl->getIntrinsicCost(IID, RetTy, Arguments, U);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000193 assert(Cost >= 0 && "TTI should not produce negative costs!");
194 return Cost;
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000195}
196
Jun Bum Lim919f9e82017-04-28 16:04:03 +0000197unsigned
198TargetTransformInfo::getEstimatedNumberOfCaseClusters(const SwitchInst &SI,
199 unsigned &JTSize) const {
200 return TTIImpl->getEstimatedNumberOfCaseClusters(SI, JTSize);
201}
202
Evgeny Astigeevich70ed78e2017-06-29 13:42:12 +0000203int TargetTransformInfo::getUserCost(const User *U,
204 ArrayRef<const Value *> Operands) const {
205 int Cost = TTIImpl->getUserCost(U, Operands);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000206 assert(Cost >= 0 && "TTI should not produce negative costs!");
207 return Cost;
Chandler Carruth511aa762013-01-21 01:27:39 +0000208}
209
Tom Stellard8b1e0212013-07-27 00:01:07 +0000210bool TargetTransformInfo::hasBranchDivergence() const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000211 return TTIImpl->hasBranchDivergence();
Tom Stellard8b1e0212013-07-27 00:01:07 +0000212}
213
Jingyue Wu5da831c2015-04-10 05:03:50 +0000214bool TargetTransformInfo::isSourceOfDivergence(const Value *V) const {
215 return TTIImpl->isSourceOfDivergence(V);
216}
217
Alexander Timofeev0f9c84c2017-06-15 19:33:10 +0000218bool llvm::TargetTransformInfo::isAlwaysUniform(const Value *V) const {
219 return TTIImpl->isAlwaysUniform(V);
220}
221
Matt Arsenault42b64782017-01-30 23:02:12 +0000222unsigned TargetTransformInfo::getFlatAddressSpace() const {
223 return TTIImpl->getFlatAddressSpace();
224}
225
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000226bool TargetTransformInfo::isLoweredToCall(const Function *F) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000227 return TTIImpl->isLoweredToCall(F);
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000228}
229
Sam Parkerc5ef5022019-06-07 07:35:30 +0000230bool TargetTransformInfo::isHardwareLoopProfitable(
Chen Zhengaa999522019-06-26 12:02:43 +0000231 Loop *L, ScalarEvolution &SE, AssumptionCache &AC,
Sam Parkerc5ef5022019-06-07 07:35:30 +0000232 TargetLibraryInfo *LibInfo, HardwareLoopInfo &HWLoopInfo) const {
233 return TTIImpl->isHardwareLoopProfitable(L, SE, AC, LibInfo, HWLoopInfo);
234}
235
Chandler Carruth705b1852015-01-31 03:43:40 +0000236void TargetTransformInfo::getUnrollingPreferences(
Geoff Berry66d9bdb2017-06-28 15:53:17 +0000237 Loop *L, ScalarEvolution &SE, UnrollingPreferences &UP) const {
238 return TTIImpl->getUnrollingPreferences(L, SE, UP);
Hal Finkel8f2e7002013-09-11 19:25:43 +0000239}
240
Chandler Carruth539edf42013-01-05 11:43:11 +0000241bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000242 return TTIImpl->isLegalAddImmediate(Imm);
Chandler Carruth539edf42013-01-05 11:43:11 +0000243}
244
245bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000246 return TTIImpl->isLegalICmpImmediate(Imm);
Chandler Carruth539edf42013-01-05 11:43:11 +0000247}
248
249bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
250 int64_t BaseOffset,
251 bool HasBaseReg,
Matt Arsenaulte83379e2015-06-07 20:12:03 +0000252 int64_t Scale,
Jonas Paulsson024e3192017-07-21 11:59:37 +0000253 unsigned AddrSpace,
254 Instruction *I) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000255 return TTIImpl->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
Jonas Paulsson024e3192017-07-21 11:59:37 +0000256 Scale, AddrSpace, I);
Chandler Carruth539edf42013-01-05 11:43:11 +0000257}
258
Evgeny Stupachenkof2b3b462017-06-05 23:37:00 +0000259bool TargetTransformInfo::isLSRCostLess(LSRCost &C1, LSRCost &C2) const {
260 return TTIImpl->isLSRCostLess(C1, C2);
261}
262
Sanjay Pateld7c702b2018-02-05 23:43:05 +0000263bool TargetTransformInfo::canMacroFuseCmp() const {
264 return TTIImpl->canMacroFuseCmp();
265}
266
Krzysztof Parzyszek0b377e02018-03-26 13:10:09 +0000267bool TargetTransformInfo::shouldFavorPostInc() const {
268 return TTIImpl->shouldFavorPostInc();
269}
270
Sam Parker67756c02019-02-07 13:32:54 +0000271bool TargetTransformInfo::shouldFavorBackedgeIndex(const Loop *L) const {
272 return TTIImpl->shouldFavorBackedgeIndex(L);
273}
274
Elena Demikhovsky20662e32015-10-19 07:43:38 +0000275bool TargetTransformInfo::isLegalMaskedStore(Type *DataType) const {
276 return TTIImpl->isLegalMaskedStore(DataType);
Chandler Carruth705b1852015-01-31 03:43:40 +0000277}
278
Elena Demikhovsky20662e32015-10-19 07:43:38 +0000279bool TargetTransformInfo::isLegalMaskedLoad(Type *DataType) const {
280 return TTIImpl->isLegalMaskedLoad(DataType);
Chandler Carruth705b1852015-01-31 03:43:40 +0000281}
282
Warren Ristow6452bdd2019-06-17 17:20:08 +0000283bool TargetTransformInfo::isLegalNTStore(Type *DataType,
284 unsigned Alignment) const {
285 return TTIImpl->isLegalNTStore(DataType, Alignment);
286}
287
288bool TargetTransformInfo::isLegalNTLoad(Type *DataType,
289 unsigned Alignment) const {
290 return TTIImpl->isLegalNTLoad(DataType, Alignment);
291}
292
Elena Demikhovsky09285852015-10-25 15:37:55 +0000293bool TargetTransformInfo::isLegalMaskedGather(Type *DataType) const {
294 return TTIImpl->isLegalMaskedGather(DataType);
295}
296
297bool TargetTransformInfo::isLegalMaskedScatter(Type *DataType) const {
Mohammed Agabariacef53dc2017-07-27 10:28:16 +0000298 return TTIImpl->isLegalMaskedScatter(DataType);
Elena Demikhovsky09285852015-10-25 15:37:55 +0000299}
300
Craig Topper9f0b17a2019-03-21 17:38:52 +0000301bool TargetTransformInfo::isLegalMaskedCompressStore(Type *DataType) const {
302 return TTIImpl->isLegalMaskedCompressStore(DataType);
303}
304
305bool TargetTransformInfo::isLegalMaskedExpandLoad(Type *DataType) const {
306 return TTIImpl->isLegalMaskedExpandLoad(DataType);
307}
308
Sanjay Patel6fd43912017-09-09 13:38:18 +0000309bool TargetTransformInfo::hasDivRemOp(Type *DataType, bool IsSigned) const {
310 return TTIImpl->hasDivRemOp(DataType, IsSigned);
311}
312
Artem Belevichcb8f6322017-10-24 20:31:44 +0000313bool TargetTransformInfo::hasVolatileVariant(Instruction *I,
314 unsigned AddrSpace) const {
315 return TTIImpl->hasVolatileVariant(I, AddrSpace);
316}
317
Jonas Paulsson8624b7e2017-05-24 13:42:56 +0000318bool TargetTransformInfo::prefersVectorizedAddressing() const {
319 return TTIImpl->prefersVectorizedAddressing();
320}
321
Quentin Colombetbf490d42013-05-31 21:29:03 +0000322int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
323 int64_t BaseOffset,
324 bool HasBaseReg,
Matt Arsenaulte83379e2015-06-07 20:12:03 +0000325 int64_t Scale,
326 unsigned AddrSpace) const {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000327 int Cost = TTIImpl->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg,
328 Scale, AddrSpace);
329 assert(Cost >= 0 && "TTI should not produce negative costs!");
330 return Cost;
Quentin Colombetbf490d42013-05-31 21:29:03 +0000331}
332
Jonas Paulsson024e3192017-07-21 11:59:37 +0000333bool TargetTransformInfo::LSRWithInstrQueries() const {
334 return TTIImpl->LSRWithInstrQueries();
335}
336
Chandler Carruth539edf42013-01-05 11:43:11 +0000337bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000338 return TTIImpl->isTruncateFree(Ty1, Ty2);
Chandler Carruth539edf42013-01-05 11:43:11 +0000339}
340
Chad Rosier54390052015-02-23 19:15:16 +0000341bool TargetTransformInfo::isProfitableToHoist(Instruction *I) const {
342 return TTIImpl->isProfitableToHoist(I);
343}
344
David Blaikie8ad9a972018-03-28 22:28:50 +0000345bool TargetTransformInfo::useAA() const { return TTIImpl->useAA(); }
346
Chandler Carruth539edf42013-01-05 11:43:11 +0000347bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000348 return TTIImpl->isTypeLegal(Ty);
Chandler Carruth539edf42013-01-05 11:43:11 +0000349}
350
351unsigned TargetTransformInfo::getJumpBufAlignment() const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000352 return TTIImpl->getJumpBufAlignment();
Chandler Carruth539edf42013-01-05 11:43:11 +0000353}
354
355unsigned TargetTransformInfo::getJumpBufSize() const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000356 return TTIImpl->getJumpBufSize();
Chandler Carruth539edf42013-01-05 11:43:11 +0000357}
358
359bool TargetTransformInfo::shouldBuildLookupTables() const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000360 return TTIImpl->shouldBuildLookupTables();
Chandler Carruth539edf42013-01-05 11:43:11 +0000361}
Oliver Stannard4df1cc02016-10-07 08:48:24 +0000362bool TargetTransformInfo::shouldBuildLookupTablesForConstant(Constant *C) const {
363 return TTIImpl->shouldBuildLookupTablesForConstant(C);
364}
Chandler Carruth539edf42013-01-05 11:43:11 +0000365
Zaara Syeda1f59ae32018-01-30 16:17:22 +0000366bool TargetTransformInfo::useColdCCForColdCall(Function &F) const {
367 return TTIImpl->useColdCCForColdCall(F);
368}
369
Jonas Paulsson8e2f9482017-01-26 07:03:25 +0000370unsigned TargetTransformInfo::
371getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const {
372 return TTIImpl->getScalarizationOverhead(Ty, Insert, Extract);
373}
374
375unsigned TargetTransformInfo::
376getOperandsScalarizationOverhead(ArrayRef<const Value *> Args,
377 unsigned VF) const {
378 return TTIImpl->getOperandsScalarizationOverhead(Args, VF);
379}
380
Jonas Paulssonda74ed42017-04-12 12:41:37 +0000381bool TargetTransformInfo::supportsEfficientVectorElementLoadStore() const {
382 return TTIImpl->supportsEfficientVectorElementLoadStore();
383}
384
Olivier Sallenave049d8032015-03-06 23:12:04 +0000385bool TargetTransformInfo::enableAggressiveInterleaving(bool LoopHasReductions) const {
386 return TTIImpl->enableAggressiveInterleaving(LoopHasReductions);
387}
388
Clement Courbet3bc5ad52019-06-25 08:04:13 +0000389TargetTransformInfo::MemCmpExpansionOptions
390TargetTransformInfo::enableMemCmpExpansion(bool OptSize, bool IsZeroCmp) const {
391 return TTIImpl->enableMemCmpExpansion(OptSize, IsZeroCmp);
Zaara Syeda3a7578c2017-05-31 17:12:38 +0000392}
393
Silviu Baranga61bdc512015-08-10 14:50:54 +0000394bool TargetTransformInfo::enableInterleavedAccessVectorization() const {
395 return TTIImpl->enableInterleavedAccessVectorization();
396}
397
Dorit Nuzman38bbf812018-10-14 08:50:06 +0000398bool TargetTransformInfo::enableMaskedInterleavedAccessVectorization() const {
399 return TTIImpl->enableMaskedInterleavedAccessVectorization();
400}
401
Renato Golin5cb666a2016-04-14 20:42:18 +0000402bool TargetTransformInfo::isFPVectorizationPotentiallyUnsafe() const {
403 return TTIImpl->isFPVectorizationPotentiallyUnsafe();
404}
405
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000406bool TargetTransformInfo::allowsMisalignedMemoryAccesses(LLVMContext &Context,
407 unsigned BitWidth,
Alina Sbirlea327955e2016-07-11 20:46:17 +0000408 unsigned AddressSpace,
409 unsigned Alignment,
410 bool *Fast) const {
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000411 return TTIImpl->allowsMisalignedMemoryAccesses(Context, BitWidth, AddressSpace,
Alina Sbirlea327955e2016-07-11 20:46:17 +0000412 Alignment, Fast);
413}
414
Chandler Carruth50a36cd2013-01-07 03:16:03 +0000415TargetTransformInfo::PopcntSupportKind
416TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000417 return TTIImpl->getPopcntSupport(IntTyWidthInBit);
Chandler Carruth539edf42013-01-05 11:43:11 +0000418}
419
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000420bool TargetTransformInfo::haveFastSqrt(Type *Ty) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000421 return TTIImpl->haveFastSqrt(Ty);
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000422}
423
Sanjay Patel0de1a4b2017-11-27 21:15:43 +0000424bool TargetTransformInfo::isFCmpOrdCheaperThanFCmpZero(Type *Ty) const {
425 return TTIImpl->isFCmpOrdCheaperThanFCmpZero(Ty);
426}
427
Chandler Carruth93205eb2015-08-05 18:08:10 +0000428int TargetTransformInfo::getFPOpCost(Type *Ty) const {
429 int Cost = TTIImpl->getFPOpCost(Ty);
430 assert(Cost >= 0 && "TTI should not produce negative costs!");
431 return Cost;
Cameron Esfahani17177d12015-02-05 02:09:33 +0000432}
433
Sjoerd Meijer38c2cd02016-07-14 07:44:20 +0000434int TargetTransformInfo::getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx,
435 const APInt &Imm,
436 Type *Ty) const {
437 int Cost = TTIImpl->getIntImmCodeSizeCost(Opcode, Idx, Imm, Ty);
438 assert(Cost >= 0 && "TTI should not produce negative costs!");
439 return Cost;
440}
441
Chandler Carruth93205eb2015-08-05 18:08:10 +0000442int TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
443 int Cost = TTIImpl->getIntImmCost(Imm, Ty);
444 assert(Cost >= 0 && "TTI should not produce negative costs!");
445 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000446}
447
Chandler Carruth93205eb2015-08-05 18:08:10 +0000448int TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx,
449 const APInt &Imm, Type *Ty) const {
450 int Cost = TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty);
451 assert(Cost >= 0 && "TTI should not produce negative costs!");
452 return Cost;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000453}
454
Chandler Carruth93205eb2015-08-05 18:08:10 +0000455int TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
456 const APInt &Imm, Type *Ty) const {
457 int Cost = TTIImpl->getIntImmCost(IID, Idx, Imm, Ty);
458 assert(Cost >= 0 && "TTI should not produce negative costs!");
459 return Cost;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000460}
461
Chandler Carruth539edf42013-01-05 11:43:11 +0000462unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000463 return TTIImpl->getNumberOfRegisters(Vector);
Chandler Carruth539edf42013-01-05 11:43:11 +0000464}
465
Nadav Rotemb1791a72013-01-09 22:29:00 +0000466unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000467 return TTIImpl->getRegisterBitWidth(Vector);
Nadav Rotemb1791a72013-01-09 22:29:00 +0000468}
469
Adam Nemete29686e2017-05-15 21:15:01 +0000470unsigned TargetTransformInfo::getMinVectorRegisterBitWidth() const {
471 return TTIImpl->getMinVectorRegisterBitWidth();
472}
473
Krzysztof Parzyszek5d93fdf2018-03-27 16:14:11 +0000474bool TargetTransformInfo::shouldMaximizeVectorBandwidth(bool OptSize) const {
475 return TTIImpl->shouldMaximizeVectorBandwidth(OptSize);
476}
477
Krzysztof Parzyszekdfed9412018-04-13 20:16:32 +0000478unsigned TargetTransformInfo::getMinimumVF(unsigned ElemWidth) const {
479 return TTIImpl->getMinimumVF(ElemWidth);
480}
481
Jun Bum Limdee55652017-04-03 19:20:07 +0000482bool TargetTransformInfo::shouldConsiderAddressTypePromotion(
483 const Instruction &I, bool &AllowPromotionWithoutCommonHeader) const {
484 return TTIImpl->shouldConsiderAddressTypePromotion(
485 I, AllowPromotionWithoutCommonHeader);
486}
487
Adam Nemetaf761102016-01-21 18:28:36 +0000488unsigned TargetTransformInfo::getCacheLineSize() const {
489 return TTIImpl->getCacheLineSize();
490}
491
Tobias Grosserd7eb6192017-08-24 09:46:25 +0000492llvm::Optional<unsigned> TargetTransformInfo::getCacheSize(CacheLevel Level)
493 const {
494 return TTIImpl->getCacheSize(Level);
495}
496
497llvm::Optional<unsigned> TargetTransformInfo::getCacheAssociativity(
498 CacheLevel Level) const {
499 return TTIImpl->getCacheAssociativity(Level);
500}
501
Adam Nemetdadfbb52016-01-27 22:21:25 +0000502unsigned TargetTransformInfo::getPrefetchDistance() const {
503 return TTIImpl->getPrefetchDistance();
504}
505
Adam Nemet6d8beec2016-03-18 00:27:38 +0000506unsigned TargetTransformInfo::getMinPrefetchStride() const {
507 return TTIImpl->getMinPrefetchStride();
508}
509
Adam Nemet709e3042016-03-18 00:27:43 +0000510unsigned TargetTransformInfo::getMaxPrefetchIterationsAhead() const {
511 return TTIImpl->getMaxPrefetchIterationsAhead();
512}
513
Wei Mi062c7442015-05-06 17:12:25 +0000514unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const {
515 return TTIImpl->getMaxInterleaveFactor(VF);
Nadav Rotemb696c362013-01-09 01:15:42 +0000516}
517
Jonas Paulsson29d80f02018-10-05 14:34:04 +0000518TargetTransformInfo::OperandValueKind
Simon Pilgrim077a42c2018-11-13 13:45:10 +0000519TargetTransformInfo::getOperandInfo(Value *V, OperandValueProperties &OpProps) {
Jonas Paulsson29d80f02018-10-05 14:34:04 +0000520 OperandValueKind OpInfo = OK_AnyValue;
521 OpProps = OP_None;
522
523 if (auto *CI = dyn_cast<ConstantInt>(V)) {
524 if (CI->getValue().isPowerOf2())
525 OpProps = OP_PowerOf2;
526 return OK_UniformConstantValue;
527 }
528
Simon Pilgrim2b166c52018-11-14 15:04:08 +0000529 // A broadcast shuffle creates a uniform value.
530 // TODO: Add support for non-zero index broadcasts.
531 // TODO: Add support for different source vector width.
532 if (auto *ShuffleInst = dyn_cast<ShuffleVectorInst>(V))
533 if (ShuffleInst->isZeroEltSplat())
534 OpInfo = OK_UniformValue;
535
Jonas Paulsson29d80f02018-10-05 14:34:04 +0000536 const Value *Splat = getSplatValue(V);
537
538 // Check for a splat of a constant or for a non uniform vector of constants
539 // and check if the constant(s) are all powers of two.
540 if (isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) {
541 OpInfo = OK_NonUniformConstantValue;
542 if (Splat) {
543 OpInfo = OK_UniformConstantValue;
544 if (auto *CI = dyn_cast<ConstantInt>(Splat))
545 if (CI->getValue().isPowerOf2())
546 OpProps = OP_PowerOf2;
547 } else if (auto *CDS = dyn_cast<ConstantDataSequential>(V)) {
548 OpProps = OP_PowerOf2;
549 for (unsigned I = 0, E = CDS->getNumElements(); I != E; ++I) {
550 if (auto *CI = dyn_cast<ConstantInt>(CDS->getElementAsConstant(I)))
551 if (CI->getValue().isPowerOf2())
552 continue;
553 OpProps = OP_None;
554 break;
555 }
556 }
557 }
558
559 // Check for a splat of a uniform value. This is not loop aware, so return
560 // true only for the obviously uniform cases (argument, globalvalue)
561 if (Splat && (isa<Argument>(Splat) || isa<GlobalValue>(Splat)))
562 OpInfo = OK_UniformValue;
563
564 return OpInfo;
565}
566
Chandler Carruth93205eb2015-08-05 18:08:10 +0000567int TargetTransformInfo::getArithmeticInstrCost(
Chandler Carruth705b1852015-01-31 03:43:40 +0000568 unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
569 OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000570 OperandValueProperties Opd2PropInfo,
571 ArrayRef<const Value *> Args) const {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000572 int Cost = TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000573 Opd1PropInfo, Opd2PropInfo, Args);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000574 assert(Cost >= 0 && "TTI should not produce negative costs!");
575 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000576}
577
Chandler Carruth93205eb2015-08-05 18:08:10 +0000578int TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty, int Index,
579 Type *SubTp) const {
580 int Cost = TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
581 assert(Cost >= 0 && "TTI should not produce negative costs!");
582 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000583}
584
Chandler Carruth93205eb2015-08-05 18:08:10 +0000585int TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000586 Type *Src, const Instruction *I) const {
587 assert ((I == nullptr || I->getOpcode() == Opcode) &&
588 "Opcode should reflect passed instruction.");
589 int Cost = TTIImpl->getCastInstrCost(Opcode, Dst, Src, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000590 assert(Cost >= 0 && "TTI should not produce negative costs!");
591 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000592}
593
Matthew Simpsone5dfb082016-04-27 15:20:21 +0000594int TargetTransformInfo::getExtractWithExtendCost(unsigned Opcode, Type *Dst,
595 VectorType *VecTy,
596 unsigned Index) const {
597 int Cost = TTIImpl->getExtractWithExtendCost(Opcode, Dst, VecTy, Index);
598 assert(Cost >= 0 && "TTI should not produce negative costs!");
599 return Cost;
600}
601
Chandler Carruth93205eb2015-08-05 18:08:10 +0000602int TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
603 int Cost = TTIImpl->getCFInstrCost(Opcode);
604 assert(Cost >= 0 && "TTI should not produce negative costs!");
605 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000606}
607
Chandler Carruth93205eb2015-08-05 18:08:10 +0000608int TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000609 Type *CondTy, const Instruction *I) const {
610 assert ((I == nullptr || I->getOpcode() == Opcode) &&
611 "Opcode should reflect passed instruction.");
612 int Cost = TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000613 assert(Cost >= 0 && "TTI should not produce negative costs!");
614 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000615}
616
Chandler Carruth93205eb2015-08-05 18:08:10 +0000617int TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
618 unsigned Index) const {
619 int Cost = TTIImpl->getVectorInstrCost(Opcode, Val, Index);
620 assert(Cost >= 0 && "TTI should not produce negative costs!");
621 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000622}
623
Chandler Carruth93205eb2015-08-05 18:08:10 +0000624int TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
625 unsigned Alignment,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000626 unsigned AddressSpace,
627 const Instruction *I) const {
628 assert ((I == nullptr || I->getOpcode() == Opcode) &&
629 "Opcode should reflect passed instruction.");
630 int Cost = TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000631 assert(Cost >= 0 && "TTI should not produce negative costs!");
632 return Cost;
Elena Demikhovskya3232f72015-01-25 08:44:46 +0000633}
634
Chandler Carruth93205eb2015-08-05 18:08:10 +0000635int TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
636 unsigned Alignment,
637 unsigned AddressSpace) const {
638 int Cost =
639 TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
640 assert(Cost >= 0 && "TTI should not produce negative costs!");
641 return Cost;
Chandler Carruth705b1852015-01-31 03:43:40 +0000642}
643
Elena Demikhovsky54946982015-12-28 20:10:59 +0000644int TargetTransformInfo::getGatherScatterOpCost(unsigned Opcode, Type *DataTy,
645 Value *Ptr, bool VariableMask,
646 unsigned Alignment) const {
647 int Cost = TTIImpl->getGatherScatterOpCost(Opcode, DataTy, Ptr, VariableMask,
648 Alignment);
649 assert(Cost >= 0 && "TTI should not produce negative costs!");
650 return Cost;
651}
652
Chandler Carruth93205eb2015-08-05 18:08:10 +0000653int TargetTransformInfo::getInterleavedMemoryOpCost(
Hao Liu32c05392015-06-08 06:39:56 +0000654 unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
Dorit Nuzman34da6dd2018-10-31 09:57:56 +0000655 unsigned Alignment, unsigned AddressSpace, bool UseMaskForCond,
656 bool UseMaskForGaps) const {
657 int Cost = TTIImpl->getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
658 Alignment, AddressSpace,
659 UseMaskForCond,
660 UseMaskForGaps);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000661 assert(Cost >= 0 && "TTI should not produce negative costs!");
662 return Cost;
Hao Liu32c05392015-06-08 06:39:56 +0000663}
664
Chandler Carruth93205eb2015-08-05 18:08:10 +0000665int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
Jonas Paulssona48ea232017-03-14 06:35:36 +0000666 ArrayRef<Type *> Tys, FastMathFlags FMF,
667 unsigned ScalarizationCostPassed) const {
668 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys, FMF,
669 ScalarizationCostPassed);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000670 assert(Cost >= 0 && "TTI should not produce negative costs!");
671 return Cost;
672}
673
Elena Demikhovsky54946982015-12-28 20:10:59 +0000674int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
Jonas Paulssona48ea232017-03-14 06:35:36 +0000675 ArrayRef<Value *> Args, FastMathFlags FMF, unsigned VF) const {
676 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Args, FMF, VF);
Elena Demikhovsky54946982015-12-28 20:10:59 +0000677 assert(Cost >= 0 && "TTI should not produce negative costs!");
678 return Cost;
679}
680
Chandler Carruth93205eb2015-08-05 18:08:10 +0000681int TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy,
682 ArrayRef<Type *> Tys) const {
683 int Cost = TTIImpl->getCallInstrCost(F, RetTy, Tys);
684 assert(Cost >= 0 && "TTI should not produce negative costs!");
685 return Cost;
Michael Zolotukhin7ed84a82015-03-17 19:26:23 +0000686}
687
Chandler Carruth539edf42013-01-05 11:43:11 +0000688unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000689 return TTIImpl->getNumberOfParts(Tp);
Chandler Carruth539edf42013-01-05 11:43:11 +0000690}
691
Chandler Carruth93205eb2015-08-05 18:08:10 +0000692int TargetTransformInfo::getAddressComputationCost(Type *Tp,
Mohammed Agabaria23599ba2017-01-05 14:03:41 +0000693 ScalarEvolution *SE,
694 const SCEV *Ptr) const {
695 int Cost = TTIImpl->getAddressComputationCost(Tp, SE, Ptr);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000696 assert(Cost >= 0 && "TTI should not produce negative costs!");
697 return Cost;
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000698}
Chandler Carruth539edf42013-01-05 11:43:11 +0000699
Sjoerd Meijerea31ddb2019-04-30 10:28:50 +0000700int TargetTransformInfo::getMemcpyCost(const Instruction *I) const {
701 int Cost = TTIImpl->getMemcpyCost(I);
702 assert(Cost >= 0 && "TTI should not produce negative costs!");
703 return Cost;
704}
705
Alexey Bataev3e9b3eb2017-07-31 14:19:32 +0000706int TargetTransformInfo::getArithmeticReductionCost(unsigned Opcode, Type *Ty,
707 bool IsPairwiseForm) const {
708 int Cost = TTIImpl->getArithmeticReductionCost(Opcode, Ty, IsPairwiseForm);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000709 assert(Cost >= 0 && "TTI should not produce negative costs!");
710 return Cost;
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000711}
712
Alexey Bataev6dd29fc2017-09-08 13:49:36 +0000713int TargetTransformInfo::getMinMaxReductionCost(Type *Ty, Type *CondTy,
714 bool IsPairwiseForm,
715 bool IsUnsigned) const {
716 int Cost =
717 TTIImpl->getMinMaxReductionCost(Ty, CondTy, IsPairwiseForm, IsUnsigned);
718 assert(Cost >= 0 && "TTI should not produce negative costs!");
719 return Cost;
720}
721
Chandler Carruth705b1852015-01-31 03:43:40 +0000722unsigned
723TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
724 return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
Chad Rosierf9327d62015-01-26 22:51:15 +0000725}
726
727bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst,
728 MemIntrinsicInfo &Info) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000729 return TTIImpl->getTgtMemIntrinsic(Inst, Info);
Chad Rosierf9327d62015-01-26 22:51:15 +0000730}
731
Anna Thomasb2a212c2017-06-06 16:45:25 +0000732unsigned TargetTransformInfo::getAtomicMemIntrinsicMaxElementSize() const {
733 return TTIImpl->getAtomicMemIntrinsicMaxElementSize();
734}
735
Chandler Carruth705b1852015-01-31 03:43:40 +0000736Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic(
737 IntrinsicInst *Inst, Type *ExpectedType) const {
738 return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
739}
740
Sean Fertile9cd1cdf2017-07-07 02:00:06 +0000741Type *TargetTransformInfo::getMemcpyLoopLoweringType(LLVMContext &Context,
742 Value *Length,
743 unsigned SrcAlign,
744 unsigned DestAlign) const {
745 return TTIImpl->getMemcpyLoopLoweringType(Context, Length, SrcAlign,
746 DestAlign);
747}
748
749void TargetTransformInfo::getMemcpyLoopResidualLoweringType(
750 SmallVectorImpl<Type *> &OpsOut, LLVMContext &Context,
751 unsigned RemainingBytes, unsigned SrcAlign, unsigned DestAlign) const {
752 TTIImpl->getMemcpyLoopResidualLoweringType(OpsOut, Context, RemainingBytes,
753 SrcAlign, DestAlign);
754}
755
Eric Christopherd566fb12015-07-29 22:09:48 +0000756bool TargetTransformInfo::areInlineCompatible(const Function *Caller,
757 const Function *Callee) const {
758 return TTIImpl->areInlineCompatible(Caller, Callee);
Eric Christopher4371b132015-07-02 01:11:47 +0000759}
760
Tom Stellard3d36e5c2019-01-16 05:15:31 +0000761bool TargetTransformInfo::areFunctionArgsABICompatible(
762 const Function *Caller, const Function *Callee,
763 SmallPtrSetImpl<Argument *> &Args) const {
764 return TTIImpl->areFunctionArgsABICompatible(Caller, Callee, Args);
765}
766
Krzysztof Parzyszek0b377e02018-03-26 13:10:09 +0000767bool TargetTransformInfo::isIndexedLoadLegal(MemIndexedMode Mode,
768 Type *Ty) const {
769 return TTIImpl->isIndexedLoadLegal(Mode, Ty);
770}
771
772bool TargetTransformInfo::isIndexedStoreLegal(MemIndexedMode Mode,
773 Type *Ty) const {
774 return TTIImpl->isIndexedStoreLegal(Mode, Ty);
775}
776
Volkan Keles1c386812016-10-03 10:31:34 +0000777unsigned TargetTransformInfo::getLoadStoreVecRegBitWidth(unsigned AS) const {
778 return TTIImpl->getLoadStoreVecRegBitWidth(AS);
779}
780
781bool TargetTransformInfo::isLegalToVectorizeLoad(LoadInst *LI) const {
782 return TTIImpl->isLegalToVectorizeLoad(LI);
783}
784
785bool TargetTransformInfo::isLegalToVectorizeStore(StoreInst *SI) const {
786 return TTIImpl->isLegalToVectorizeStore(SI);
787}
788
789bool TargetTransformInfo::isLegalToVectorizeLoadChain(
790 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
791 return TTIImpl->isLegalToVectorizeLoadChain(ChainSizeInBytes, Alignment,
792 AddrSpace);
793}
794
795bool TargetTransformInfo::isLegalToVectorizeStoreChain(
796 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
797 return TTIImpl->isLegalToVectorizeStoreChain(ChainSizeInBytes, Alignment,
798 AddrSpace);
799}
800
801unsigned TargetTransformInfo::getLoadVectorFactor(unsigned VF,
802 unsigned LoadSize,
803 unsigned ChainSizeInBytes,
804 VectorType *VecTy) const {
805 return TTIImpl->getLoadVectorFactor(VF, LoadSize, ChainSizeInBytes, VecTy);
806}
807
808unsigned TargetTransformInfo::getStoreVectorFactor(unsigned VF,
809 unsigned StoreSize,
810 unsigned ChainSizeInBytes,
811 VectorType *VecTy) const {
812 return TTIImpl->getStoreVectorFactor(VF, StoreSize, ChainSizeInBytes, VecTy);
813}
814
Amara Emersoncf9daa32017-05-09 10:43:25 +0000815bool TargetTransformInfo::useReductionIntrinsic(unsigned Opcode,
816 Type *Ty, ReductionFlags Flags) const {
817 return TTIImpl->useReductionIntrinsic(Opcode, Ty, Flags);
818}
819
Amara Emerson836b0f42017-05-10 09:42:49 +0000820bool TargetTransformInfo::shouldExpandReduction(const IntrinsicInst *II) const {
821 return TTIImpl->shouldExpandReduction(II);
822}
Amara Emersoncf9daa32017-05-09 10:43:25 +0000823
Amara Emerson14688222019-06-17 23:20:29 +0000824unsigned TargetTransformInfo::getGISelRematGlobalCost() const {
825 return TTIImpl->getGISelRematGlobalCost();
826}
827
Guozhi Wei62d64142017-09-08 22:29:17 +0000828int TargetTransformInfo::getInstructionLatency(const Instruction *I) const {
829 return TTIImpl->getInstructionLatency(I);
830}
831
Guozhi Wei62d64142017-09-08 22:29:17 +0000832static bool matchPairwiseShuffleMask(ShuffleVectorInst *SI, bool IsLeft,
833 unsigned Level) {
834 // We don't need a shuffle if we just want to have element 0 in position 0 of
835 // the vector.
836 if (!SI && Level == 0 && IsLeft)
837 return true;
838 else if (!SI)
839 return false;
840
841 SmallVector<int, 32> Mask(SI->getType()->getVectorNumElements(), -1);
842
843 // Build a mask of 0, 2, ... (left) or 1, 3, ... (right) depending on whether
844 // we look at the left or right side.
845 for (unsigned i = 0, e = (1 << Level), val = !IsLeft; i != e; ++i, val += 2)
846 Mask[i] = val;
847
848 SmallVector<int, 16> ActualMask = SI->getShuffleMask();
849 return Mask == ActualMask;
850}
851
852namespace {
853/// Kind of the reduction data.
854enum ReductionKind {
855 RK_None, /// Not a reduction.
856 RK_Arithmetic, /// Binary reduction data.
857 RK_MinMax, /// Min/max reduction data.
858 RK_UnsignedMinMax, /// Unsigned min/max reduction data.
859};
860/// Contains opcode + LHS/RHS parts of the reduction operations.
861struct ReductionData {
862 ReductionData() = delete;
863 ReductionData(ReductionKind Kind, unsigned Opcode, Value *LHS, Value *RHS)
864 : Opcode(Opcode), LHS(LHS), RHS(RHS), Kind(Kind) {
865 assert(Kind != RK_None && "expected binary or min/max reduction only.");
866 }
867 unsigned Opcode = 0;
868 Value *LHS = nullptr;
869 Value *RHS = nullptr;
870 ReductionKind Kind = RK_None;
871 bool hasSameData(ReductionData &RD) const {
872 return Kind == RD.Kind && Opcode == RD.Opcode;
873 }
874};
875} // namespace
876
877static Optional<ReductionData> getReductionData(Instruction *I) {
878 Value *L, *R;
879 if (m_BinOp(m_Value(L), m_Value(R)).match(I))
Fangrui Songf78650a2018-07-30 19:41:25 +0000880 return ReductionData(RK_Arithmetic, I->getOpcode(), L, R);
Guozhi Wei62d64142017-09-08 22:29:17 +0000881 if (auto *SI = dyn_cast<SelectInst>(I)) {
882 if (m_SMin(m_Value(L), m_Value(R)).match(SI) ||
883 m_SMax(m_Value(L), m_Value(R)).match(SI) ||
884 m_OrdFMin(m_Value(L), m_Value(R)).match(SI) ||
885 m_OrdFMax(m_Value(L), m_Value(R)).match(SI) ||
886 m_UnordFMin(m_Value(L), m_Value(R)).match(SI) ||
887 m_UnordFMax(m_Value(L), m_Value(R)).match(SI)) {
888 auto *CI = cast<CmpInst>(SI->getCondition());
Fangrui Songf78650a2018-07-30 19:41:25 +0000889 return ReductionData(RK_MinMax, CI->getOpcode(), L, R);
890 }
Guozhi Wei62d64142017-09-08 22:29:17 +0000891 if (m_UMin(m_Value(L), m_Value(R)).match(SI) ||
892 m_UMax(m_Value(L), m_Value(R)).match(SI)) {
893 auto *CI = cast<CmpInst>(SI->getCondition());
894 return ReductionData(RK_UnsignedMinMax, CI->getOpcode(), L, R);
895 }
896 }
897 return llvm::None;
898}
899
900static ReductionKind matchPairwiseReductionAtLevel(Instruction *I,
901 unsigned Level,
902 unsigned NumLevels) {
903 // Match one level of pairwise operations.
904 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef,
905 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef>
906 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef,
907 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef>
908 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1
909 if (!I)
910 return RK_None;
911
912 assert(I->getType()->isVectorTy() && "Expecting a vector type");
913
914 Optional<ReductionData> RD = getReductionData(I);
915 if (!RD)
916 return RK_None;
917
918 ShuffleVectorInst *LS = dyn_cast<ShuffleVectorInst>(RD->LHS);
919 if (!LS && Level)
920 return RK_None;
921 ShuffleVectorInst *RS = dyn_cast<ShuffleVectorInst>(RD->RHS);
922 if (!RS && Level)
923 return RK_None;
924
925 // On level 0 we can omit one shufflevector instruction.
926 if (!Level && !RS && !LS)
927 return RK_None;
928
929 // Shuffle inputs must match.
930 Value *NextLevelOpL = LS ? LS->getOperand(0) : nullptr;
931 Value *NextLevelOpR = RS ? RS->getOperand(0) : nullptr;
932 Value *NextLevelOp = nullptr;
933 if (NextLevelOpR && NextLevelOpL) {
934 // If we have two shuffles their operands must match.
935 if (NextLevelOpL != NextLevelOpR)
936 return RK_None;
937
938 NextLevelOp = NextLevelOpL;
939 } else if (Level == 0 && (NextLevelOpR || NextLevelOpL)) {
940 // On the first level we can omit the shufflevector <0, undef,...>. So the
941 // input to the other shufflevector <1, undef> must match with one of the
942 // inputs to the current binary operation.
943 // Example:
944 // %NextLevelOpL = shufflevector %R, <1, undef ...>
945 // %BinOp = fadd %NextLevelOpL, %R
946 if (NextLevelOpL && NextLevelOpL != RD->RHS)
947 return RK_None;
948 else if (NextLevelOpR && NextLevelOpR != RD->LHS)
949 return RK_None;
950
951 NextLevelOp = NextLevelOpL ? RD->RHS : RD->LHS;
952 } else
953 return RK_None;
954
955 // Check that the next levels binary operation exists and matches with the
956 // current one.
957 if (Level + 1 != NumLevels) {
958 Optional<ReductionData> NextLevelRD =
959 getReductionData(cast<Instruction>(NextLevelOp));
960 if (!NextLevelRD || !RD->hasSameData(*NextLevelRD))
961 return RK_None;
962 }
963
964 // Shuffle mask for pairwise operation must match.
965 if (matchPairwiseShuffleMask(LS, /*IsLeft=*/true, Level)) {
966 if (!matchPairwiseShuffleMask(RS, /*IsLeft=*/false, Level))
967 return RK_None;
968 } else if (matchPairwiseShuffleMask(RS, /*IsLeft=*/true, Level)) {
969 if (!matchPairwiseShuffleMask(LS, /*IsLeft=*/false, Level))
970 return RK_None;
971 } else {
972 return RK_None;
973 }
974
975 if (++Level == NumLevels)
976 return RD->Kind;
977
978 // Match next level.
979 return matchPairwiseReductionAtLevel(cast<Instruction>(NextLevelOp), Level,
980 NumLevels);
981}
982
983static ReductionKind matchPairwiseReduction(const ExtractElementInst *ReduxRoot,
984 unsigned &Opcode, Type *&Ty) {
985 if (!EnableReduxCost)
986 return RK_None;
987
988 // Need to extract the first element.
989 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1));
990 unsigned Idx = ~0u;
991 if (CI)
992 Idx = CI->getZExtValue();
993 if (Idx != 0)
994 return RK_None;
995
996 auto *RdxStart = dyn_cast<Instruction>(ReduxRoot->getOperand(0));
997 if (!RdxStart)
998 return RK_None;
999 Optional<ReductionData> RD = getReductionData(RdxStart);
1000 if (!RD)
1001 return RK_None;
1002
1003 Type *VecTy = RdxStart->getType();
1004 unsigned NumVecElems = VecTy->getVectorNumElements();
1005 if (!isPowerOf2_32(NumVecElems))
1006 return RK_None;
1007
1008 // We look for a sequence of shuffle,shuffle,add triples like the following
1009 // that builds a pairwise reduction tree.
Fangrui Songf78650a2018-07-30 19:41:25 +00001010 //
Guozhi Wei62d64142017-09-08 22:29:17 +00001011 // (X0, X1, X2, X3)
1012 // (X0 + X1, X2 + X3, undef, undef)
1013 // ((X0 + X1) + (X2 + X3), undef, undef, undef)
Fangrui Songf78650a2018-07-30 19:41:25 +00001014 //
Guozhi Wei62d64142017-09-08 22:29:17 +00001015 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef,
1016 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef>
1017 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef,
1018 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef>
1019 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1
1020 // %rdx.shuf.1.0 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef,
1021 // <4 x i32> <i32 0, i32 undef, i32 undef, i32 undef>
1022 // %rdx.shuf.1.1 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef,
1023 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
1024 // %bin.rdx8 = fadd <4 x float> %rdx.shuf.1.0, %rdx.shuf.1.1
1025 // %r = extractelement <4 x float> %bin.rdx8, i32 0
1026 if (matchPairwiseReductionAtLevel(RdxStart, 0, Log2_32(NumVecElems)) ==
1027 RK_None)
1028 return RK_None;
1029
1030 Opcode = RD->Opcode;
1031 Ty = VecTy;
1032
1033 return RD->Kind;
1034}
1035
1036static std::pair<Value *, ShuffleVectorInst *>
1037getShuffleAndOtherOprd(Value *L, Value *R) {
1038 ShuffleVectorInst *S = nullptr;
1039
1040 if ((S = dyn_cast<ShuffleVectorInst>(L)))
1041 return std::make_pair(R, S);
1042
1043 S = dyn_cast<ShuffleVectorInst>(R);
1044 return std::make_pair(L, S);
1045}
1046
1047static ReductionKind
1048matchVectorSplittingReduction(const ExtractElementInst *ReduxRoot,
1049 unsigned &Opcode, Type *&Ty) {
1050 if (!EnableReduxCost)
1051 return RK_None;
1052
1053 // Need to extract the first element.
1054 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1));
1055 unsigned Idx = ~0u;
1056 if (CI)
1057 Idx = CI->getZExtValue();
1058 if (Idx != 0)
1059 return RK_None;
1060
1061 auto *RdxStart = dyn_cast<Instruction>(ReduxRoot->getOperand(0));
1062 if (!RdxStart)
1063 return RK_None;
1064 Optional<ReductionData> RD = getReductionData(RdxStart);
1065 if (!RD)
1066 return RK_None;
1067
1068 Type *VecTy = ReduxRoot->getOperand(0)->getType();
1069 unsigned NumVecElems = VecTy->getVectorNumElements();
1070 if (!isPowerOf2_32(NumVecElems))
1071 return RK_None;
1072
1073 // We look for a sequence of shuffles and adds like the following matching one
1074 // fadd, shuffle vector pair at a time.
Fangrui Songf78650a2018-07-30 19:41:25 +00001075 //
Guozhi Wei62d64142017-09-08 22:29:17 +00001076 // %rdx.shuf = shufflevector <4 x float> %rdx, <4 x float> undef,
1077 // <4 x i32> <i32 2, i32 3, i32 undef, i32 undef>
1078 // %bin.rdx = fadd <4 x float> %rdx, %rdx.shuf
1079 // %rdx.shuf7 = shufflevector <4 x float> %bin.rdx, <4 x float> undef,
1080 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
1081 // %bin.rdx8 = fadd <4 x float> %bin.rdx, %rdx.shuf7
1082 // %r = extractelement <4 x float> %bin.rdx8, i32 0
1083
1084 unsigned MaskStart = 1;
1085 Instruction *RdxOp = RdxStart;
Fangrui Songf78650a2018-07-30 19:41:25 +00001086 SmallVector<int, 32> ShuffleMask(NumVecElems, 0);
Guozhi Wei62d64142017-09-08 22:29:17 +00001087 unsigned NumVecElemsRemain = NumVecElems;
1088 while (NumVecElemsRemain - 1) {
1089 // Check for the right reduction operation.
1090 if (!RdxOp)
1091 return RK_None;
1092 Optional<ReductionData> RDLevel = getReductionData(RdxOp);
1093 if (!RDLevel || !RDLevel->hasSameData(*RD))
1094 return RK_None;
1095
1096 Value *NextRdxOp;
1097 ShuffleVectorInst *Shuffle;
1098 std::tie(NextRdxOp, Shuffle) =
1099 getShuffleAndOtherOprd(RDLevel->LHS, RDLevel->RHS);
1100
1101 // Check the current reduction operation and the shuffle use the same value.
1102 if (Shuffle == nullptr)
1103 return RK_None;
1104 if (Shuffle->getOperand(0) != NextRdxOp)
1105 return RK_None;
1106
1107 // Check that shuffle masks matches.
1108 for (unsigned j = 0; j != MaskStart; ++j)
1109 ShuffleMask[j] = MaskStart + j;
1110 // Fill the rest of the mask with -1 for undef.
1111 std::fill(&ShuffleMask[MaskStart], ShuffleMask.end(), -1);
1112
1113 SmallVector<int, 16> Mask = Shuffle->getShuffleMask();
1114 if (ShuffleMask != Mask)
1115 return RK_None;
1116
1117 RdxOp = dyn_cast<Instruction>(NextRdxOp);
1118 NumVecElemsRemain /= 2;
1119 MaskStart *= 2;
1120 }
1121
1122 Opcode = RD->Opcode;
1123 Ty = VecTy;
1124 return RD->Kind;
1125}
1126
1127int TargetTransformInfo::getInstructionThroughput(const Instruction *I) const {
1128 switch (I->getOpcode()) {
1129 case Instruction::GetElementPtr:
1130 return getUserCost(I);
1131
1132 case Instruction::Ret:
1133 case Instruction::PHI:
1134 case Instruction::Br: {
1135 return getCFInstrCost(I->getOpcode());
1136 }
1137 case Instruction::Add:
1138 case Instruction::FAdd:
1139 case Instruction::Sub:
1140 case Instruction::FSub:
1141 case Instruction::Mul:
1142 case Instruction::FMul:
1143 case Instruction::UDiv:
1144 case Instruction::SDiv:
1145 case Instruction::FDiv:
1146 case Instruction::URem:
1147 case Instruction::SRem:
1148 case Instruction::FRem:
1149 case Instruction::Shl:
1150 case Instruction::LShr:
1151 case Instruction::AShr:
1152 case Instruction::And:
1153 case Instruction::Or:
1154 case Instruction::Xor: {
Simon Pilgrim4162d772018-05-22 10:40:09 +00001155 TargetTransformInfo::OperandValueKind Op1VK, Op2VK;
1156 TargetTransformInfo::OperandValueProperties Op1VP, Op2VP;
1157 Op1VK = getOperandInfo(I->getOperand(0), Op1VP);
1158 Op2VK = getOperandInfo(I->getOperand(1), Op2VP);
1159 SmallVector<const Value *, 2> Operands(I->operand_values());
1160 return getArithmeticInstrCost(I->getOpcode(), I->getType(), Op1VK, Op2VK,
1161 Op1VP, Op2VP, Operands);
Guozhi Wei62d64142017-09-08 22:29:17 +00001162 }
Craig Topper50d50282019-05-28 04:09:18 +00001163 case Instruction::FNeg: {
1164 TargetTransformInfo::OperandValueKind Op1VK, Op2VK;
1165 TargetTransformInfo::OperandValueProperties Op1VP, Op2VP;
1166 Op1VK = getOperandInfo(I->getOperand(0), Op1VP);
1167 Op2VK = OK_AnyValue;
1168 Op2VP = OP_None;
1169 SmallVector<const Value *, 2> Operands(I->operand_values());
1170 return getArithmeticInstrCost(I->getOpcode(), I->getType(), Op1VK, Op2VK,
1171 Op1VP, Op2VP, Operands);
1172 }
Guozhi Wei62d64142017-09-08 22:29:17 +00001173 case Instruction::Select: {
1174 const SelectInst *SI = cast<SelectInst>(I);
1175 Type *CondTy = SI->getCondition()->getType();
1176 return getCmpSelInstrCost(I->getOpcode(), I->getType(), CondTy, I);
1177 }
1178 case Instruction::ICmp:
1179 case Instruction::FCmp: {
1180 Type *ValTy = I->getOperand(0)->getType();
1181 return getCmpSelInstrCost(I->getOpcode(), ValTy, I->getType(), I);
1182 }
1183 case Instruction::Store: {
1184 const StoreInst *SI = cast<StoreInst>(I);
1185 Type *ValTy = SI->getValueOperand()->getType();
1186 return getMemoryOpCost(I->getOpcode(), ValTy,
1187 SI->getAlignment(),
1188 SI->getPointerAddressSpace(), I);
1189 }
1190 case Instruction::Load: {
1191 const LoadInst *LI = cast<LoadInst>(I);
1192 return getMemoryOpCost(I->getOpcode(), I->getType(),
1193 LI->getAlignment(),
1194 LI->getPointerAddressSpace(), I);
1195 }
1196 case Instruction::ZExt:
1197 case Instruction::SExt:
1198 case Instruction::FPToUI:
1199 case Instruction::FPToSI:
1200 case Instruction::FPExt:
1201 case Instruction::PtrToInt:
1202 case Instruction::IntToPtr:
1203 case Instruction::SIToFP:
1204 case Instruction::UIToFP:
1205 case Instruction::Trunc:
1206 case Instruction::FPTrunc:
1207 case Instruction::BitCast:
1208 case Instruction::AddrSpaceCast: {
1209 Type *SrcTy = I->getOperand(0)->getType();
1210 return getCastInstrCost(I->getOpcode(), I->getType(), SrcTy, I);
1211 }
1212 case Instruction::ExtractElement: {
1213 const ExtractElementInst * EEI = cast<ExtractElementInst>(I);
1214 ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1));
1215 unsigned Idx = -1;
1216 if (CI)
1217 Idx = CI->getZExtValue();
1218
1219 // Try to match a reduction sequence (series of shufflevector and vector
1220 // adds followed by a extractelement).
1221 unsigned ReduxOpCode;
1222 Type *ReduxType;
1223
1224 switch (matchVectorSplittingReduction(EEI, ReduxOpCode, ReduxType)) {
1225 case RK_Arithmetic:
1226 return getArithmeticReductionCost(ReduxOpCode, ReduxType,
1227 /*IsPairwiseForm=*/false);
1228 case RK_MinMax:
1229 return getMinMaxReductionCost(
1230 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1231 /*IsPairwiseForm=*/false, /*IsUnsigned=*/false);
1232 case RK_UnsignedMinMax:
1233 return getMinMaxReductionCost(
1234 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1235 /*IsPairwiseForm=*/false, /*IsUnsigned=*/true);
1236 case RK_None:
1237 break;
1238 }
1239
1240 switch (matchPairwiseReduction(EEI, ReduxOpCode, ReduxType)) {
1241 case RK_Arithmetic:
1242 return getArithmeticReductionCost(ReduxOpCode, ReduxType,
1243 /*IsPairwiseForm=*/true);
1244 case RK_MinMax:
1245 return getMinMaxReductionCost(
1246 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1247 /*IsPairwiseForm=*/true, /*IsUnsigned=*/false);
1248 case RK_UnsignedMinMax:
1249 return getMinMaxReductionCost(
1250 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1251 /*IsPairwiseForm=*/true, /*IsUnsigned=*/true);
1252 case RK_None:
1253 break;
1254 }
1255
1256 return getVectorInstrCost(I->getOpcode(),
1257 EEI->getOperand(0)->getType(), Idx);
1258 }
1259 case Instruction::InsertElement: {
1260 const InsertElementInst * IE = cast<InsertElementInst>(I);
1261 ConstantInt *CI = dyn_cast<ConstantInt>(IE->getOperand(2));
Fangrui Songf78650a2018-07-30 19:41:25 +00001262 unsigned Idx = -1;
Guozhi Wei62d64142017-09-08 22:29:17 +00001263 if (CI)
1264 Idx = CI->getZExtValue();
1265 return getVectorInstrCost(I->getOpcode(),
1266 IE->getType(), Idx);
1267 }
1268 case Instruction::ShuffleVector: {
1269 const ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
Simon Pilgrimd0c71602018-11-09 16:28:19 +00001270 Type *Ty = Shuffle->getType();
1271 Type *SrcTy = Shuffle->getOperand(0)->getType();
1272
1273 // TODO: Identify and add costs for insert subvector, etc.
1274 int SubIndex;
1275 if (Shuffle->isExtractSubvectorMask(SubIndex))
Simon Pilgrim26e1c882018-11-09 18:30:59 +00001276 return TTIImpl->getShuffleCost(SK_ExtractSubvector, SrcTy, SubIndex, Ty);
Simon Pilgrimd0c71602018-11-09 16:28:19 +00001277
Sanjay Patel2ca33602018-06-19 18:44:00 +00001278 if (Shuffle->changesLength())
1279 return -1;
Fangrui Songf78650a2018-07-30 19:41:25 +00001280
Sanjay Patel2ca33602018-06-19 18:44:00 +00001281 if (Shuffle->isIdentity())
1282 return 0;
Guozhi Wei62d64142017-09-08 22:29:17 +00001283
Sanjay Patel2ca33602018-06-19 18:44:00 +00001284 if (Shuffle->isReverse())
1285 return TTIImpl->getShuffleCost(SK_Reverse, Ty, 0, nullptr);
Simon Pilgrim07839212018-06-12 14:47:13 +00001286
Sanjay Patel2ca33602018-06-19 18:44:00 +00001287 if (Shuffle->isSelect())
1288 return TTIImpl->getShuffleCost(SK_Select, Ty, 0, nullptr);
Simon Pilgrim07839212018-06-12 14:47:13 +00001289
Sanjay Patel2ca33602018-06-19 18:44:00 +00001290 if (Shuffle->isTranspose())
1291 return TTIImpl->getShuffleCost(SK_Transpose, Ty, 0, nullptr);
Matthew Simpsonb4096eb2018-04-26 13:48:33 +00001292
Sanjay Patel2ca33602018-06-19 18:44:00 +00001293 if (Shuffle->isZeroEltSplat())
1294 return TTIImpl->getShuffleCost(SK_Broadcast, Ty, 0, nullptr);
Guozhi Wei62d64142017-09-08 22:29:17 +00001295
Sanjay Patel2ca33602018-06-19 18:44:00 +00001296 if (Shuffle->isSingleSource())
1297 return TTIImpl->getShuffleCost(SK_PermuteSingleSrc, Ty, 0, nullptr);
Guozhi Wei62d64142017-09-08 22:29:17 +00001298
Sanjay Patel2ca33602018-06-19 18:44:00 +00001299 return TTIImpl->getShuffleCost(SK_PermuteTwoSrc, Ty, 0, nullptr);
Guozhi Wei62d64142017-09-08 22:29:17 +00001300 }
1301 case Instruction::Call:
1302 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1303 SmallVector<Value *, 4> Args(II->arg_operands());
1304
1305 FastMathFlags FMF;
1306 if (auto *FPMO = dyn_cast<FPMathOperator>(II))
1307 FMF = FPMO->getFastMathFlags();
1308
1309 return getIntrinsicInstrCost(II->getIntrinsicID(), II->getType(),
1310 Args, FMF);
1311 }
1312 return -1;
1313 default:
1314 // We don't have any information on this instruction.
1315 return -1;
1316 }
1317}
1318
Chandler Carruth705b1852015-01-31 03:43:40 +00001319TargetTransformInfo::Concept::~Concept() {}
1320
Chandler Carruthe0385522015-02-01 10:11:22 +00001321TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
1322
1323TargetIRAnalysis::TargetIRAnalysis(
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001324 std::function<Result(const Function &)> TTICallback)
Benjamin Kramer82de7d32016-05-27 14:27:24 +00001325 : TTICallback(std::move(TTICallback)) {}
Chandler Carruthe0385522015-02-01 10:11:22 +00001326
Chandler Carruth164a2aa62016-06-17 00:11:01 +00001327TargetIRAnalysis::Result TargetIRAnalysis::run(const Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +00001328 FunctionAnalysisManager &) {
Chandler Carruthe0385522015-02-01 10:11:22 +00001329 return TTICallback(F);
1330}
1331
Chandler Carruthdab4eae2016-11-23 17:53:26 +00001332AnalysisKey TargetIRAnalysis::Key;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +00001333
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001334TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(const Function &F) {
Mehdi Amini5010ebf2015-07-09 02:08:42 +00001335 return Result(F.getParent()->getDataLayout());
Chandler Carruthe0385522015-02-01 10:11:22 +00001336}
1337
Chandler Carruth705b1852015-01-31 03:43:40 +00001338// Register the basic pass.
1339INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
1340 "Target Transform Information", false, true)
1341char TargetTransformInfoWrapperPass::ID = 0;
Chandler Carruth539edf42013-01-05 11:43:11 +00001342
Chandler Carruth705b1852015-01-31 03:43:40 +00001343void TargetTransformInfoWrapperPass::anchor() {}
Chandler Carruth539edf42013-01-05 11:43:11 +00001344
Chandler Carruth705b1852015-01-31 03:43:40 +00001345TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass()
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001346 : ImmutablePass(ID) {
Chandler Carruth705b1852015-01-31 03:43:40 +00001347 initializeTargetTransformInfoWrapperPassPass(
1348 *PassRegistry::getPassRegistry());
1349}
1350
1351TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass(
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001352 TargetIRAnalysis TIRA)
1353 : ImmutablePass(ID), TIRA(std::move(TIRA)) {
Chandler Carruth705b1852015-01-31 03:43:40 +00001354 initializeTargetTransformInfoWrapperPassPass(
1355 *PassRegistry::getPassRegistry());
1356}
1357
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001358TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(const Function &F) {
Sean Silva36e0d012016-08-09 00:28:15 +00001359 FunctionAnalysisManager DummyFAM;
Chandler Carruth164a2aa62016-06-17 00:11:01 +00001360 TTI = TIRA.run(F, DummyFAM);
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001361 return *TTI;
1362}
1363
Chandler Carruth93dcdc42015-01-31 11:17:59 +00001364ImmutablePass *
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001365llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) {
1366 return new TargetTransformInfoWrapperPass(std::move(TIRA));
Chandler Carruth539edf42013-01-05 11:43:11 +00001367}