blob: 0d58234630b2eb7a16e8a2daaa724ec469eeaa42 [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,
Jinsong Ji06fef0b2019-07-09 17:53:09 +000058 bool ForceHardwareLoopPHI) {
Chen Zhengc5b918d2019-06-19 01:26:31 +000059 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
Daniil Fukalovd912a9b2019-07-17 16:51:29 +0000179int TargetTransformInfo::getInlinerVectorBonusPercent() const {
180 return TTIImpl->getInlinerVectorBonusPercent();
181}
182
Jingyue Wu15f3e822016-07-08 21:48:05 +0000183int TargetTransformInfo::getGEPCost(Type *PointeeType, const Value *Ptr,
184 ArrayRef<const Value *> Operands) const {
185 return TTIImpl->getGEPCost(PointeeType, Ptr, Operands);
186}
187
Haicheng Wuabdef9e2017-07-15 02:12:16 +0000188int TargetTransformInfo::getExtCost(const Instruction *I,
189 const Value *Src) const {
190 return TTIImpl->getExtCost(I, Src);
191}
192
Chandler Carruth93205eb2015-08-05 18:08:10 +0000193int TargetTransformInfo::getIntrinsicCost(
Sjoerd Meijer31ff6472019-03-12 09:48:02 +0000194 Intrinsic::ID IID, Type *RetTy, ArrayRef<const Value *> Arguments,
195 const User *U) const {
196 int Cost = TTIImpl->getIntrinsicCost(IID, RetTy, Arguments, U);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000197 assert(Cost >= 0 && "TTI should not produce negative costs!");
198 return Cost;
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000199}
200
Jun Bum Lim919f9e82017-04-28 16:04:03 +0000201unsigned
202TargetTransformInfo::getEstimatedNumberOfCaseClusters(const SwitchInst &SI,
203 unsigned &JTSize) const {
204 return TTIImpl->getEstimatedNumberOfCaseClusters(SI, JTSize);
205}
206
Evgeny Astigeevich70ed78e2017-06-29 13:42:12 +0000207int TargetTransformInfo::getUserCost(const User *U,
208 ArrayRef<const Value *> Operands) const {
209 int Cost = TTIImpl->getUserCost(U, Operands);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000210 assert(Cost >= 0 && "TTI should not produce negative costs!");
211 return Cost;
Chandler Carruth511aa762013-01-21 01:27:39 +0000212}
213
Tom Stellard8b1e0212013-07-27 00:01:07 +0000214bool TargetTransformInfo::hasBranchDivergence() const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000215 return TTIImpl->hasBranchDivergence();
Tom Stellard8b1e0212013-07-27 00:01:07 +0000216}
217
Jingyue Wu5da831c2015-04-10 05:03:50 +0000218bool TargetTransformInfo::isSourceOfDivergence(const Value *V) const {
219 return TTIImpl->isSourceOfDivergence(V);
220}
221
Alexander Timofeev0f9c84c2017-06-15 19:33:10 +0000222bool llvm::TargetTransformInfo::isAlwaysUniform(const Value *V) const {
223 return TTIImpl->isAlwaysUniform(V);
224}
225
Matt Arsenault42b64782017-01-30 23:02:12 +0000226unsigned TargetTransformInfo::getFlatAddressSpace() const {
227 return TTIImpl->getFlatAddressSpace();
228}
229
Matt Arsenaultdbc1f202019-08-14 18:13:00 +0000230bool TargetTransformInfo::collectFlatAddressOperands(
231 SmallVectorImpl<int> &OpIndexes, Intrinsic::ID IID) const {
232 return TTIImpl->collectFlatAddressOperands(OpIndexes, IID);
233}
234
235bool TargetTransformInfo::rewriteIntrinsicWithAddressSpace(
236 IntrinsicInst *II, Value *OldV, Value *NewV) const {
237 return TTIImpl->rewriteIntrinsicWithAddressSpace(II, OldV, NewV);
238}
239
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000240bool TargetTransformInfo::isLoweredToCall(const Function *F) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000241 return TTIImpl->isLoweredToCall(F);
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000242}
243
Sam Parkerc5ef5022019-06-07 07:35:30 +0000244bool TargetTransformInfo::isHardwareLoopProfitable(
Chen Zhengaa999522019-06-26 12:02:43 +0000245 Loop *L, ScalarEvolution &SE, AssumptionCache &AC,
Sam Parkerc5ef5022019-06-07 07:35:30 +0000246 TargetLibraryInfo *LibInfo, HardwareLoopInfo &HWLoopInfo) const {
247 return TTIImpl->isHardwareLoopProfitable(L, SE, AC, LibInfo, HWLoopInfo);
248}
249
Chandler Carruth705b1852015-01-31 03:43:40 +0000250void TargetTransformInfo::getUnrollingPreferences(
Geoff Berry66d9bdb2017-06-28 15:53:17 +0000251 Loop *L, ScalarEvolution &SE, UnrollingPreferences &UP) const {
252 return TTIImpl->getUnrollingPreferences(L, SE, UP);
Hal Finkel8f2e7002013-09-11 19:25:43 +0000253}
254
Chandler Carruth539edf42013-01-05 11:43:11 +0000255bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000256 return TTIImpl->isLegalAddImmediate(Imm);
Chandler Carruth539edf42013-01-05 11:43:11 +0000257}
258
259bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000260 return TTIImpl->isLegalICmpImmediate(Imm);
Chandler Carruth539edf42013-01-05 11:43:11 +0000261}
262
263bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
264 int64_t BaseOffset,
265 bool HasBaseReg,
Matt Arsenaulte83379e2015-06-07 20:12:03 +0000266 int64_t Scale,
Jonas Paulsson024e3192017-07-21 11:59:37 +0000267 unsigned AddrSpace,
268 Instruction *I) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000269 return TTIImpl->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
Jonas Paulsson024e3192017-07-21 11:59:37 +0000270 Scale, AddrSpace, I);
Chandler Carruth539edf42013-01-05 11:43:11 +0000271}
272
Evgeny Stupachenkof2b3b462017-06-05 23:37:00 +0000273bool TargetTransformInfo::isLSRCostLess(LSRCost &C1, LSRCost &C2) const {
274 return TTIImpl->isLSRCostLess(C1, C2);
275}
276
Sanjay Pateld7c702b2018-02-05 23:43:05 +0000277bool TargetTransformInfo::canMacroFuseCmp() const {
278 return TTIImpl->canMacroFuseCmp();
279}
280
Chen Zhengdfdccbb2019-07-03 01:49:03 +0000281bool TargetTransformInfo::canSaveCmp(Loop *L, BranchInst **BI,
282 ScalarEvolution *SE, LoopInfo *LI,
283 DominatorTree *DT, AssumptionCache *AC,
284 TargetLibraryInfo *LibInfo) const {
285 return TTIImpl->canSaveCmp(L, BI, SE, LI, DT, AC, LibInfo);
286}
287
Krzysztof Parzyszek0b377e02018-03-26 13:10:09 +0000288bool TargetTransformInfo::shouldFavorPostInc() const {
289 return TTIImpl->shouldFavorPostInc();
290}
291
Sam Parker67756c02019-02-07 13:32:54 +0000292bool TargetTransformInfo::shouldFavorBackedgeIndex(const Loop *L) const {
293 return TTIImpl->shouldFavorBackedgeIndex(L);
294}
295
Elena Demikhovsky20662e32015-10-19 07:43:38 +0000296bool TargetTransformInfo::isLegalMaskedStore(Type *DataType) const {
297 return TTIImpl->isLegalMaskedStore(DataType);
Chandler Carruth705b1852015-01-31 03:43:40 +0000298}
299
Elena Demikhovsky20662e32015-10-19 07:43:38 +0000300bool TargetTransformInfo::isLegalMaskedLoad(Type *DataType) const {
301 return TTIImpl->isLegalMaskedLoad(DataType);
Chandler Carruth705b1852015-01-31 03:43:40 +0000302}
303
Warren Ristow6452bdd2019-06-17 17:20:08 +0000304bool TargetTransformInfo::isLegalNTStore(Type *DataType,
Guillaume Chatelet33671ce2019-09-05 13:09:42 +0000305 llvm::Align Alignment) const {
Warren Ristow6452bdd2019-06-17 17:20:08 +0000306 return TTIImpl->isLegalNTStore(DataType, Alignment);
307}
308
309bool TargetTransformInfo::isLegalNTLoad(Type *DataType,
Guillaume Chatelet33671ce2019-09-05 13:09:42 +0000310 llvm::Align Alignment) const {
Warren Ristow6452bdd2019-06-17 17:20:08 +0000311 return TTIImpl->isLegalNTLoad(DataType, Alignment);
312}
313
Elena Demikhovsky09285852015-10-25 15:37:55 +0000314bool TargetTransformInfo::isLegalMaskedGather(Type *DataType) const {
315 return TTIImpl->isLegalMaskedGather(DataType);
316}
317
318bool TargetTransformInfo::isLegalMaskedScatter(Type *DataType) const {
Mohammed Agabariacef53dc2017-07-27 10:28:16 +0000319 return TTIImpl->isLegalMaskedScatter(DataType);
Elena Demikhovsky09285852015-10-25 15:37:55 +0000320}
321
Craig Topper9f0b17a2019-03-21 17:38:52 +0000322bool TargetTransformInfo::isLegalMaskedCompressStore(Type *DataType) const {
323 return TTIImpl->isLegalMaskedCompressStore(DataType);
324}
325
326bool TargetTransformInfo::isLegalMaskedExpandLoad(Type *DataType) const {
327 return TTIImpl->isLegalMaskedExpandLoad(DataType);
328}
329
Sanjay Patel6fd43912017-09-09 13:38:18 +0000330bool TargetTransformInfo::hasDivRemOp(Type *DataType, bool IsSigned) const {
331 return TTIImpl->hasDivRemOp(DataType, IsSigned);
332}
333
Artem Belevichcb8f6322017-10-24 20:31:44 +0000334bool TargetTransformInfo::hasVolatileVariant(Instruction *I,
335 unsigned AddrSpace) const {
336 return TTIImpl->hasVolatileVariant(I, AddrSpace);
337}
338
Jonas Paulsson8624b7e2017-05-24 13:42:56 +0000339bool TargetTransformInfo::prefersVectorizedAddressing() const {
340 return TTIImpl->prefersVectorizedAddressing();
341}
342
Quentin Colombetbf490d42013-05-31 21:29:03 +0000343int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
344 int64_t BaseOffset,
345 bool HasBaseReg,
Matt Arsenaulte83379e2015-06-07 20:12:03 +0000346 int64_t Scale,
347 unsigned AddrSpace) const {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000348 int Cost = TTIImpl->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg,
349 Scale, AddrSpace);
350 assert(Cost >= 0 && "TTI should not produce negative costs!");
351 return Cost;
Quentin Colombetbf490d42013-05-31 21:29:03 +0000352}
353
Jonas Paulsson024e3192017-07-21 11:59:37 +0000354bool TargetTransformInfo::LSRWithInstrQueries() const {
355 return TTIImpl->LSRWithInstrQueries();
356}
357
Chandler Carruth539edf42013-01-05 11:43:11 +0000358bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000359 return TTIImpl->isTruncateFree(Ty1, Ty2);
Chandler Carruth539edf42013-01-05 11:43:11 +0000360}
361
Chad Rosier54390052015-02-23 19:15:16 +0000362bool TargetTransformInfo::isProfitableToHoist(Instruction *I) const {
363 return TTIImpl->isProfitableToHoist(I);
364}
365
David Blaikie8ad9a972018-03-28 22:28:50 +0000366bool TargetTransformInfo::useAA() const { return TTIImpl->useAA(); }
367
Chandler Carruth539edf42013-01-05 11:43:11 +0000368bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000369 return TTIImpl->isTypeLegal(Ty);
Chandler Carruth539edf42013-01-05 11:43:11 +0000370}
371
Chandler Carruth539edf42013-01-05 11:43:11 +0000372bool TargetTransformInfo::shouldBuildLookupTables() const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000373 return TTIImpl->shouldBuildLookupTables();
Chandler Carruth539edf42013-01-05 11:43:11 +0000374}
Oliver Stannard4df1cc02016-10-07 08:48:24 +0000375bool TargetTransformInfo::shouldBuildLookupTablesForConstant(Constant *C) const {
376 return TTIImpl->shouldBuildLookupTablesForConstant(C);
377}
Chandler Carruth539edf42013-01-05 11:43:11 +0000378
Zaara Syeda1f59ae32018-01-30 16:17:22 +0000379bool TargetTransformInfo::useColdCCForColdCall(Function &F) const {
380 return TTIImpl->useColdCCForColdCall(F);
381}
382
Jonas Paulsson8e2f9482017-01-26 07:03:25 +0000383unsigned TargetTransformInfo::
384getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const {
385 return TTIImpl->getScalarizationOverhead(Ty, Insert, Extract);
386}
387
388unsigned TargetTransformInfo::
389getOperandsScalarizationOverhead(ArrayRef<const Value *> Args,
390 unsigned VF) const {
391 return TTIImpl->getOperandsScalarizationOverhead(Args, VF);
392}
393
Jonas Paulssonda74ed42017-04-12 12:41:37 +0000394bool TargetTransformInfo::supportsEfficientVectorElementLoadStore() const {
395 return TTIImpl->supportsEfficientVectorElementLoadStore();
396}
397
Olivier Sallenave049d8032015-03-06 23:12:04 +0000398bool TargetTransformInfo::enableAggressiveInterleaving(bool LoopHasReductions) const {
399 return TTIImpl->enableAggressiveInterleaving(LoopHasReductions);
400}
401
Clement Courbet3bc5ad52019-06-25 08:04:13 +0000402TargetTransformInfo::MemCmpExpansionOptions
403TargetTransformInfo::enableMemCmpExpansion(bool OptSize, bool IsZeroCmp) const {
404 return TTIImpl->enableMemCmpExpansion(OptSize, IsZeroCmp);
Zaara Syeda3a7578c2017-05-31 17:12:38 +0000405}
406
Silviu Baranga61bdc512015-08-10 14:50:54 +0000407bool TargetTransformInfo::enableInterleavedAccessVectorization() const {
408 return TTIImpl->enableInterleavedAccessVectorization();
409}
410
Dorit Nuzman38bbf812018-10-14 08:50:06 +0000411bool TargetTransformInfo::enableMaskedInterleavedAccessVectorization() const {
412 return TTIImpl->enableMaskedInterleavedAccessVectorization();
413}
414
Renato Golin5cb666a2016-04-14 20:42:18 +0000415bool TargetTransformInfo::isFPVectorizationPotentiallyUnsafe() const {
416 return TTIImpl->isFPVectorizationPotentiallyUnsafe();
417}
418
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000419bool TargetTransformInfo::allowsMisalignedMemoryAccesses(LLVMContext &Context,
420 unsigned BitWidth,
Alina Sbirlea327955e2016-07-11 20:46:17 +0000421 unsigned AddressSpace,
422 unsigned Alignment,
423 bool *Fast) const {
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000424 return TTIImpl->allowsMisalignedMemoryAccesses(Context, BitWidth, AddressSpace,
Alina Sbirlea327955e2016-07-11 20:46:17 +0000425 Alignment, Fast);
426}
427
Chandler Carruth50a36cd2013-01-07 03:16:03 +0000428TargetTransformInfo::PopcntSupportKind
429TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000430 return TTIImpl->getPopcntSupport(IntTyWidthInBit);
Chandler Carruth539edf42013-01-05 11:43:11 +0000431}
432
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000433bool TargetTransformInfo::haveFastSqrt(Type *Ty) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000434 return TTIImpl->haveFastSqrt(Ty);
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000435}
436
Sanjay Patel0de1a4b2017-11-27 21:15:43 +0000437bool TargetTransformInfo::isFCmpOrdCheaperThanFCmpZero(Type *Ty) const {
438 return TTIImpl->isFCmpOrdCheaperThanFCmpZero(Ty);
439}
440
Chandler Carruth93205eb2015-08-05 18:08:10 +0000441int TargetTransformInfo::getFPOpCost(Type *Ty) const {
442 int Cost = TTIImpl->getFPOpCost(Ty);
443 assert(Cost >= 0 && "TTI should not produce negative costs!");
444 return Cost;
Cameron Esfahani17177d12015-02-05 02:09:33 +0000445}
446
Sjoerd Meijer38c2cd02016-07-14 07:44:20 +0000447int TargetTransformInfo::getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx,
448 const APInt &Imm,
449 Type *Ty) const {
450 int Cost = TTIImpl->getIntImmCodeSizeCost(Opcode, Idx, Imm, Ty);
451 assert(Cost >= 0 && "TTI should not produce negative costs!");
452 return Cost;
453}
454
Chandler Carruth93205eb2015-08-05 18:08:10 +0000455int TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
456 int Cost = TTIImpl->getIntImmCost(Imm, Ty);
457 assert(Cost >= 0 && "TTI should not produce negative costs!");
458 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000459}
460
Chandler Carruth93205eb2015-08-05 18:08:10 +0000461int TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx,
462 const APInt &Imm, Type *Ty) const {
463 int Cost = TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty);
464 assert(Cost >= 0 && "TTI should not produce negative costs!");
465 return Cost;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000466}
467
Chandler Carruth93205eb2015-08-05 18:08:10 +0000468int TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
469 const APInt &Imm, Type *Ty) const {
470 int Cost = TTIImpl->getIntImmCost(IID, Idx, Imm, Ty);
471 assert(Cost >= 0 && "TTI should not produce negative costs!");
472 return Cost;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000473}
474
Chandler Carruth539edf42013-01-05 11:43:11 +0000475unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000476 return TTIImpl->getNumberOfRegisters(Vector);
Chandler Carruth539edf42013-01-05 11:43:11 +0000477}
478
Nadav Rotemb1791a72013-01-09 22:29:00 +0000479unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000480 return TTIImpl->getRegisterBitWidth(Vector);
Nadav Rotemb1791a72013-01-09 22:29:00 +0000481}
482
Adam Nemete29686e2017-05-15 21:15:01 +0000483unsigned TargetTransformInfo::getMinVectorRegisterBitWidth() const {
484 return TTIImpl->getMinVectorRegisterBitWidth();
485}
486
Krzysztof Parzyszek5d93fdf2018-03-27 16:14:11 +0000487bool TargetTransformInfo::shouldMaximizeVectorBandwidth(bool OptSize) const {
488 return TTIImpl->shouldMaximizeVectorBandwidth(OptSize);
489}
490
Krzysztof Parzyszekdfed9412018-04-13 20:16:32 +0000491unsigned TargetTransformInfo::getMinimumVF(unsigned ElemWidth) const {
492 return TTIImpl->getMinimumVF(ElemWidth);
493}
494
Jun Bum Limdee55652017-04-03 19:20:07 +0000495bool TargetTransformInfo::shouldConsiderAddressTypePromotion(
496 const Instruction &I, bool &AllowPromotionWithoutCommonHeader) const {
497 return TTIImpl->shouldConsiderAddressTypePromotion(
498 I, AllowPromotionWithoutCommonHeader);
499}
500
Adam Nemetaf761102016-01-21 18:28:36 +0000501unsigned TargetTransformInfo::getCacheLineSize() const {
502 return TTIImpl->getCacheLineSize();
503}
504
Tobias Grosserd7eb6192017-08-24 09:46:25 +0000505llvm::Optional<unsigned> TargetTransformInfo::getCacheSize(CacheLevel Level)
506 const {
507 return TTIImpl->getCacheSize(Level);
508}
509
510llvm::Optional<unsigned> TargetTransformInfo::getCacheAssociativity(
511 CacheLevel Level) const {
512 return TTIImpl->getCacheAssociativity(Level);
513}
514
Adam Nemetdadfbb52016-01-27 22:21:25 +0000515unsigned TargetTransformInfo::getPrefetchDistance() const {
516 return TTIImpl->getPrefetchDistance();
517}
518
Adam Nemet6d8beec2016-03-18 00:27:38 +0000519unsigned TargetTransformInfo::getMinPrefetchStride() const {
520 return TTIImpl->getMinPrefetchStride();
521}
522
Adam Nemet709e3042016-03-18 00:27:43 +0000523unsigned TargetTransformInfo::getMaxPrefetchIterationsAhead() const {
524 return TTIImpl->getMaxPrefetchIterationsAhead();
525}
526
Wei Mi062c7442015-05-06 17:12:25 +0000527unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const {
528 return TTIImpl->getMaxInterleaveFactor(VF);
Nadav Rotemb696c362013-01-09 01:15:42 +0000529}
530
Jonas Paulsson29d80f02018-10-05 14:34:04 +0000531TargetTransformInfo::OperandValueKind
Simon Pilgrim077a42c2018-11-13 13:45:10 +0000532TargetTransformInfo::getOperandInfo(Value *V, OperandValueProperties &OpProps) {
Jonas Paulsson29d80f02018-10-05 14:34:04 +0000533 OperandValueKind OpInfo = OK_AnyValue;
534 OpProps = OP_None;
535
536 if (auto *CI = dyn_cast<ConstantInt>(V)) {
537 if (CI->getValue().isPowerOf2())
538 OpProps = OP_PowerOf2;
539 return OK_UniformConstantValue;
540 }
541
Simon Pilgrim2b166c52018-11-14 15:04:08 +0000542 // A broadcast shuffle creates a uniform value.
543 // TODO: Add support for non-zero index broadcasts.
544 // TODO: Add support for different source vector width.
545 if (auto *ShuffleInst = dyn_cast<ShuffleVectorInst>(V))
546 if (ShuffleInst->isZeroEltSplat())
547 OpInfo = OK_UniformValue;
548
Jonas Paulsson29d80f02018-10-05 14:34:04 +0000549 const Value *Splat = getSplatValue(V);
550
551 // Check for a splat of a constant or for a non uniform vector of constants
552 // and check if the constant(s) are all powers of two.
553 if (isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) {
554 OpInfo = OK_NonUniformConstantValue;
555 if (Splat) {
556 OpInfo = OK_UniformConstantValue;
557 if (auto *CI = dyn_cast<ConstantInt>(Splat))
558 if (CI->getValue().isPowerOf2())
559 OpProps = OP_PowerOf2;
560 } else if (auto *CDS = dyn_cast<ConstantDataSequential>(V)) {
561 OpProps = OP_PowerOf2;
562 for (unsigned I = 0, E = CDS->getNumElements(); I != E; ++I) {
563 if (auto *CI = dyn_cast<ConstantInt>(CDS->getElementAsConstant(I)))
564 if (CI->getValue().isPowerOf2())
565 continue;
566 OpProps = OP_None;
567 break;
568 }
569 }
570 }
571
572 // Check for a splat of a uniform value. This is not loop aware, so return
573 // true only for the obviously uniform cases (argument, globalvalue)
574 if (Splat && (isa<Argument>(Splat) || isa<GlobalValue>(Splat)))
575 OpInfo = OK_UniformValue;
576
577 return OpInfo;
578}
579
Chandler Carruth93205eb2015-08-05 18:08:10 +0000580int TargetTransformInfo::getArithmeticInstrCost(
Chandler Carruth705b1852015-01-31 03:43:40 +0000581 unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
582 OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000583 OperandValueProperties Opd2PropInfo,
584 ArrayRef<const Value *> Args) const {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000585 int Cost = TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000586 Opd1PropInfo, Opd2PropInfo, Args);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000587 assert(Cost >= 0 && "TTI should not produce negative costs!");
588 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000589}
590
Chandler Carruth93205eb2015-08-05 18:08:10 +0000591int TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty, int Index,
592 Type *SubTp) const {
593 int Cost = TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
594 assert(Cost >= 0 && "TTI should not produce negative costs!");
595 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000596}
597
Chandler Carruth93205eb2015-08-05 18:08:10 +0000598int TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000599 Type *Src, const Instruction *I) const {
600 assert ((I == nullptr || I->getOpcode() == Opcode) &&
601 "Opcode should reflect passed instruction.");
602 int Cost = TTIImpl->getCastInstrCost(Opcode, Dst, Src, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000603 assert(Cost >= 0 && "TTI should not produce negative costs!");
604 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000605}
606
Matthew Simpsone5dfb082016-04-27 15:20:21 +0000607int TargetTransformInfo::getExtractWithExtendCost(unsigned Opcode, Type *Dst,
608 VectorType *VecTy,
609 unsigned Index) const {
610 int Cost = TTIImpl->getExtractWithExtendCost(Opcode, Dst, VecTy, Index);
611 assert(Cost >= 0 && "TTI should not produce negative costs!");
612 return Cost;
613}
614
Chandler Carruth93205eb2015-08-05 18:08:10 +0000615int TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
616 int Cost = TTIImpl->getCFInstrCost(Opcode);
617 assert(Cost >= 0 && "TTI should not produce negative costs!");
618 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000619}
620
Chandler Carruth93205eb2015-08-05 18:08:10 +0000621int TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000622 Type *CondTy, const Instruction *I) const {
623 assert ((I == nullptr || I->getOpcode() == Opcode) &&
624 "Opcode should reflect passed instruction.");
625 int Cost = TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000626 assert(Cost >= 0 && "TTI should not produce negative costs!");
627 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000628}
629
Chandler Carruth93205eb2015-08-05 18:08:10 +0000630int TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
631 unsigned Index) const {
632 int Cost = TTIImpl->getVectorInstrCost(Opcode, Val, Index);
633 assert(Cost >= 0 && "TTI should not produce negative costs!");
634 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000635}
636
Chandler Carruth93205eb2015-08-05 18:08:10 +0000637int TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
638 unsigned Alignment,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000639 unsigned AddressSpace,
640 const Instruction *I) const {
641 assert ((I == nullptr || I->getOpcode() == Opcode) &&
642 "Opcode should reflect passed instruction.");
643 int Cost = TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000644 assert(Cost >= 0 && "TTI should not produce negative costs!");
645 return Cost;
Elena Demikhovskya3232f72015-01-25 08:44:46 +0000646}
647
Chandler Carruth93205eb2015-08-05 18:08:10 +0000648int TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
649 unsigned Alignment,
650 unsigned AddressSpace) const {
651 int Cost =
652 TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
653 assert(Cost >= 0 && "TTI should not produce negative costs!");
654 return Cost;
Chandler Carruth705b1852015-01-31 03:43:40 +0000655}
656
Elena Demikhovsky54946982015-12-28 20:10:59 +0000657int TargetTransformInfo::getGatherScatterOpCost(unsigned Opcode, Type *DataTy,
658 Value *Ptr, bool VariableMask,
659 unsigned Alignment) const {
660 int Cost = TTIImpl->getGatherScatterOpCost(Opcode, DataTy, Ptr, VariableMask,
661 Alignment);
662 assert(Cost >= 0 && "TTI should not produce negative costs!");
663 return Cost;
664}
665
Chandler Carruth93205eb2015-08-05 18:08:10 +0000666int TargetTransformInfo::getInterleavedMemoryOpCost(
Hao Liu32c05392015-06-08 06:39:56 +0000667 unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
Dorit Nuzman34da6dd2018-10-31 09:57:56 +0000668 unsigned Alignment, unsigned AddressSpace, bool UseMaskForCond,
669 bool UseMaskForGaps) const {
670 int Cost = TTIImpl->getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
671 Alignment, AddressSpace,
672 UseMaskForCond,
673 UseMaskForGaps);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000674 assert(Cost >= 0 && "TTI should not produce negative costs!");
675 return Cost;
Hao Liu32c05392015-06-08 06:39:56 +0000676}
677
Chandler Carruth93205eb2015-08-05 18:08:10 +0000678int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
Jonas Paulssona48ea232017-03-14 06:35:36 +0000679 ArrayRef<Type *> Tys, FastMathFlags FMF,
680 unsigned ScalarizationCostPassed) const {
681 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys, FMF,
682 ScalarizationCostPassed);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000683 assert(Cost >= 0 && "TTI should not produce negative costs!");
684 return Cost;
685}
686
Elena Demikhovsky54946982015-12-28 20:10:59 +0000687int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
Jonas Paulssona48ea232017-03-14 06:35:36 +0000688 ArrayRef<Value *> Args, FastMathFlags FMF, unsigned VF) const {
689 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Args, FMF, VF);
Elena Demikhovsky54946982015-12-28 20:10:59 +0000690 assert(Cost >= 0 && "TTI should not produce negative costs!");
691 return Cost;
692}
693
Chandler Carruth93205eb2015-08-05 18:08:10 +0000694int TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy,
695 ArrayRef<Type *> Tys) const {
696 int Cost = TTIImpl->getCallInstrCost(F, RetTy, Tys);
697 assert(Cost >= 0 && "TTI should not produce negative costs!");
698 return Cost;
Michael Zolotukhin7ed84a82015-03-17 19:26:23 +0000699}
700
Chandler Carruth539edf42013-01-05 11:43:11 +0000701unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000702 return TTIImpl->getNumberOfParts(Tp);
Chandler Carruth539edf42013-01-05 11:43:11 +0000703}
704
Chandler Carruth93205eb2015-08-05 18:08:10 +0000705int TargetTransformInfo::getAddressComputationCost(Type *Tp,
Mohammed Agabaria23599ba2017-01-05 14:03:41 +0000706 ScalarEvolution *SE,
707 const SCEV *Ptr) const {
708 int Cost = TTIImpl->getAddressComputationCost(Tp, SE, Ptr);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000709 assert(Cost >= 0 && "TTI should not produce negative costs!");
710 return Cost;
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000711}
Chandler Carruth539edf42013-01-05 11:43:11 +0000712
Sjoerd Meijerea31ddb2019-04-30 10:28:50 +0000713int TargetTransformInfo::getMemcpyCost(const Instruction *I) const {
714 int Cost = TTIImpl->getMemcpyCost(I);
715 assert(Cost >= 0 && "TTI should not produce negative costs!");
716 return Cost;
717}
718
Alexey Bataev3e9b3eb2017-07-31 14:19:32 +0000719int TargetTransformInfo::getArithmeticReductionCost(unsigned Opcode, Type *Ty,
720 bool IsPairwiseForm) const {
721 int Cost = TTIImpl->getArithmeticReductionCost(Opcode, Ty, IsPairwiseForm);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000722 assert(Cost >= 0 && "TTI should not produce negative costs!");
723 return Cost;
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000724}
725
Alexey Bataev6dd29fc2017-09-08 13:49:36 +0000726int TargetTransformInfo::getMinMaxReductionCost(Type *Ty, Type *CondTy,
727 bool IsPairwiseForm,
728 bool IsUnsigned) const {
729 int Cost =
730 TTIImpl->getMinMaxReductionCost(Ty, CondTy, IsPairwiseForm, IsUnsigned);
731 assert(Cost >= 0 && "TTI should not produce negative costs!");
732 return Cost;
733}
734
Chandler Carruth705b1852015-01-31 03:43:40 +0000735unsigned
736TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
737 return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
Chad Rosierf9327d62015-01-26 22:51:15 +0000738}
739
740bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst,
741 MemIntrinsicInfo &Info) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000742 return TTIImpl->getTgtMemIntrinsic(Inst, Info);
Chad Rosierf9327d62015-01-26 22:51:15 +0000743}
744
Anna Thomasb2a212c2017-06-06 16:45:25 +0000745unsigned TargetTransformInfo::getAtomicMemIntrinsicMaxElementSize() const {
746 return TTIImpl->getAtomicMemIntrinsicMaxElementSize();
747}
748
Chandler Carruth705b1852015-01-31 03:43:40 +0000749Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic(
750 IntrinsicInst *Inst, Type *ExpectedType) const {
751 return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
752}
753
Sean Fertile9cd1cdf2017-07-07 02:00:06 +0000754Type *TargetTransformInfo::getMemcpyLoopLoweringType(LLVMContext &Context,
755 Value *Length,
756 unsigned SrcAlign,
757 unsigned DestAlign) const {
758 return TTIImpl->getMemcpyLoopLoweringType(Context, Length, SrcAlign,
759 DestAlign);
760}
761
762void TargetTransformInfo::getMemcpyLoopResidualLoweringType(
763 SmallVectorImpl<Type *> &OpsOut, LLVMContext &Context,
764 unsigned RemainingBytes, unsigned SrcAlign, unsigned DestAlign) const {
765 TTIImpl->getMemcpyLoopResidualLoweringType(OpsOut, Context, RemainingBytes,
766 SrcAlign, DestAlign);
767}
768
Eric Christopherd566fb12015-07-29 22:09:48 +0000769bool TargetTransformInfo::areInlineCompatible(const Function *Caller,
770 const Function *Callee) const {
771 return TTIImpl->areInlineCompatible(Caller, Callee);
Eric Christopher4371b132015-07-02 01:11:47 +0000772}
773
Tom Stellard3d36e5c2019-01-16 05:15:31 +0000774bool TargetTransformInfo::areFunctionArgsABICompatible(
775 const Function *Caller, const Function *Callee,
776 SmallPtrSetImpl<Argument *> &Args) const {
777 return TTIImpl->areFunctionArgsABICompatible(Caller, Callee, Args);
778}
779
Krzysztof Parzyszek0b377e02018-03-26 13:10:09 +0000780bool TargetTransformInfo::isIndexedLoadLegal(MemIndexedMode Mode,
781 Type *Ty) const {
782 return TTIImpl->isIndexedLoadLegal(Mode, Ty);
783}
784
785bool TargetTransformInfo::isIndexedStoreLegal(MemIndexedMode Mode,
786 Type *Ty) const {
787 return TTIImpl->isIndexedStoreLegal(Mode, Ty);
788}
789
Volkan Keles1c386812016-10-03 10:31:34 +0000790unsigned TargetTransformInfo::getLoadStoreVecRegBitWidth(unsigned AS) const {
791 return TTIImpl->getLoadStoreVecRegBitWidth(AS);
792}
793
794bool TargetTransformInfo::isLegalToVectorizeLoad(LoadInst *LI) const {
795 return TTIImpl->isLegalToVectorizeLoad(LI);
796}
797
798bool TargetTransformInfo::isLegalToVectorizeStore(StoreInst *SI) const {
799 return TTIImpl->isLegalToVectorizeStore(SI);
800}
801
802bool TargetTransformInfo::isLegalToVectorizeLoadChain(
803 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
804 return TTIImpl->isLegalToVectorizeLoadChain(ChainSizeInBytes, Alignment,
805 AddrSpace);
806}
807
808bool TargetTransformInfo::isLegalToVectorizeStoreChain(
809 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
810 return TTIImpl->isLegalToVectorizeStoreChain(ChainSizeInBytes, Alignment,
811 AddrSpace);
812}
813
814unsigned TargetTransformInfo::getLoadVectorFactor(unsigned VF,
815 unsigned LoadSize,
816 unsigned ChainSizeInBytes,
817 VectorType *VecTy) const {
818 return TTIImpl->getLoadVectorFactor(VF, LoadSize, ChainSizeInBytes, VecTy);
819}
820
821unsigned TargetTransformInfo::getStoreVectorFactor(unsigned VF,
822 unsigned StoreSize,
823 unsigned ChainSizeInBytes,
824 VectorType *VecTy) const {
825 return TTIImpl->getStoreVectorFactor(VF, StoreSize, ChainSizeInBytes, VecTy);
826}
827
Amara Emersoncf9daa32017-05-09 10:43:25 +0000828bool TargetTransformInfo::useReductionIntrinsic(unsigned Opcode,
829 Type *Ty, ReductionFlags Flags) const {
830 return TTIImpl->useReductionIntrinsic(Opcode, Ty, Flags);
831}
832
Amara Emerson836b0f42017-05-10 09:42:49 +0000833bool TargetTransformInfo::shouldExpandReduction(const IntrinsicInst *II) const {
834 return TTIImpl->shouldExpandReduction(II);
835}
Amara Emersoncf9daa32017-05-09 10:43:25 +0000836
Amara Emerson14688222019-06-17 23:20:29 +0000837unsigned TargetTransformInfo::getGISelRematGlobalCost() const {
838 return TTIImpl->getGISelRematGlobalCost();
839}
840
Guozhi Wei62d64142017-09-08 22:29:17 +0000841int TargetTransformInfo::getInstructionLatency(const Instruction *I) const {
842 return TTIImpl->getInstructionLatency(I);
843}
844
Guozhi Wei62d64142017-09-08 22:29:17 +0000845static bool matchPairwiseShuffleMask(ShuffleVectorInst *SI, bool IsLeft,
846 unsigned Level) {
847 // We don't need a shuffle if we just want to have element 0 in position 0 of
848 // the vector.
849 if (!SI && Level == 0 && IsLeft)
850 return true;
851 else if (!SI)
852 return false;
853
854 SmallVector<int, 32> Mask(SI->getType()->getVectorNumElements(), -1);
855
856 // Build a mask of 0, 2, ... (left) or 1, 3, ... (right) depending on whether
857 // we look at the left or right side.
858 for (unsigned i = 0, e = (1 << Level), val = !IsLeft; i != e; ++i, val += 2)
859 Mask[i] = val;
860
861 SmallVector<int, 16> ActualMask = SI->getShuffleMask();
862 return Mask == ActualMask;
863}
864
865namespace {
866/// Kind of the reduction data.
867enum ReductionKind {
868 RK_None, /// Not a reduction.
869 RK_Arithmetic, /// Binary reduction data.
870 RK_MinMax, /// Min/max reduction data.
871 RK_UnsignedMinMax, /// Unsigned min/max reduction data.
872};
873/// Contains opcode + LHS/RHS parts of the reduction operations.
874struct ReductionData {
875 ReductionData() = delete;
876 ReductionData(ReductionKind Kind, unsigned Opcode, Value *LHS, Value *RHS)
877 : Opcode(Opcode), LHS(LHS), RHS(RHS), Kind(Kind) {
878 assert(Kind != RK_None && "expected binary or min/max reduction only.");
879 }
880 unsigned Opcode = 0;
881 Value *LHS = nullptr;
882 Value *RHS = nullptr;
883 ReductionKind Kind = RK_None;
884 bool hasSameData(ReductionData &RD) const {
885 return Kind == RD.Kind && Opcode == RD.Opcode;
886 }
887};
888} // namespace
889
890static Optional<ReductionData> getReductionData(Instruction *I) {
891 Value *L, *R;
892 if (m_BinOp(m_Value(L), m_Value(R)).match(I))
Fangrui Songf78650a2018-07-30 19:41:25 +0000893 return ReductionData(RK_Arithmetic, I->getOpcode(), L, R);
Guozhi Wei62d64142017-09-08 22:29:17 +0000894 if (auto *SI = dyn_cast<SelectInst>(I)) {
895 if (m_SMin(m_Value(L), m_Value(R)).match(SI) ||
896 m_SMax(m_Value(L), m_Value(R)).match(SI) ||
897 m_OrdFMin(m_Value(L), m_Value(R)).match(SI) ||
898 m_OrdFMax(m_Value(L), m_Value(R)).match(SI) ||
899 m_UnordFMin(m_Value(L), m_Value(R)).match(SI) ||
900 m_UnordFMax(m_Value(L), m_Value(R)).match(SI)) {
901 auto *CI = cast<CmpInst>(SI->getCondition());
Fangrui Songf78650a2018-07-30 19:41:25 +0000902 return ReductionData(RK_MinMax, CI->getOpcode(), L, R);
903 }
Guozhi Wei62d64142017-09-08 22:29:17 +0000904 if (m_UMin(m_Value(L), m_Value(R)).match(SI) ||
905 m_UMax(m_Value(L), m_Value(R)).match(SI)) {
906 auto *CI = cast<CmpInst>(SI->getCondition());
907 return ReductionData(RK_UnsignedMinMax, CI->getOpcode(), L, R);
908 }
909 }
910 return llvm::None;
911}
912
913static ReductionKind matchPairwiseReductionAtLevel(Instruction *I,
914 unsigned Level,
915 unsigned NumLevels) {
916 // Match one level of pairwise operations.
917 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef,
918 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef>
919 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef,
920 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef>
921 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1
922 if (!I)
923 return RK_None;
924
925 assert(I->getType()->isVectorTy() && "Expecting a vector type");
926
927 Optional<ReductionData> RD = getReductionData(I);
928 if (!RD)
929 return RK_None;
930
931 ShuffleVectorInst *LS = dyn_cast<ShuffleVectorInst>(RD->LHS);
932 if (!LS && Level)
933 return RK_None;
934 ShuffleVectorInst *RS = dyn_cast<ShuffleVectorInst>(RD->RHS);
935 if (!RS && Level)
936 return RK_None;
937
938 // On level 0 we can omit one shufflevector instruction.
939 if (!Level && !RS && !LS)
940 return RK_None;
941
942 // Shuffle inputs must match.
943 Value *NextLevelOpL = LS ? LS->getOperand(0) : nullptr;
944 Value *NextLevelOpR = RS ? RS->getOperand(0) : nullptr;
945 Value *NextLevelOp = nullptr;
946 if (NextLevelOpR && NextLevelOpL) {
947 // If we have two shuffles their operands must match.
948 if (NextLevelOpL != NextLevelOpR)
949 return RK_None;
950
951 NextLevelOp = NextLevelOpL;
952 } else if (Level == 0 && (NextLevelOpR || NextLevelOpL)) {
953 // On the first level we can omit the shufflevector <0, undef,...>. So the
954 // input to the other shufflevector <1, undef> must match with one of the
955 // inputs to the current binary operation.
956 // Example:
957 // %NextLevelOpL = shufflevector %R, <1, undef ...>
958 // %BinOp = fadd %NextLevelOpL, %R
959 if (NextLevelOpL && NextLevelOpL != RD->RHS)
960 return RK_None;
961 else if (NextLevelOpR && NextLevelOpR != RD->LHS)
962 return RK_None;
963
964 NextLevelOp = NextLevelOpL ? RD->RHS : RD->LHS;
965 } else
966 return RK_None;
967
968 // Check that the next levels binary operation exists and matches with the
969 // current one.
970 if (Level + 1 != NumLevels) {
971 Optional<ReductionData> NextLevelRD =
972 getReductionData(cast<Instruction>(NextLevelOp));
973 if (!NextLevelRD || !RD->hasSameData(*NextLevelRD))
974 return RK_None;
975 }
976
977 // Shuffle mask for pairwise operation must match.
978 if (matchPairwiseShuffleMask(LS, /*IsLeft=*/true, Level)) {
979 if (!matchPairwiseShuffleMask(RS, /*IsLeft=*/false, Level))
980 return RK_None;
981 } else if (matchPairwiseShuffleMask(RS, /*IsLeft=*/true, Level)) {
982 if (!matchPairwiseShuffleMask(LS, /*IsLeft=*/false, Level))
983 return RK_None;
984 } else {
985 return RK_None;
986 }
987
988 if (++Level == NumLevels)
989 return RD->Kind;
990
991 // Match next level.
992 return matchPairwiseReductionAtLevel(cast<Instruction>(NextLevelOp), Level,
993 NumLevels);
994}
995
996static ReductionKind matchPairwiseReduction(const ExtractElementInst *ReduxRoot,
997 unsigned &Opcode, Type *&Ty) {
998 if (!EnableReduxCost)
999 return RK_None;
1000
1001 // Need to extract the first element.
1002 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1));
1003 unsigned Idx = ~0u;
1004 if (CI)
1005 Idx = CI->getZExtValue();
1006 if (Idx != 0)
1007 return RK_None;
1008
1009 auto *RdxStart = dyn_cast<Instruction>(ReduxRoot->getOperand(0));
1010 if (!RdxStart)
1011 return RK_None;
1012 Optional<ReductionData> RD = getReductionData(RdxStart);
1013 if (!RD)
1014 return RK_None;
1015
1016 Type *VecTy = RdxStart->getType();
1017 unsigned NumVecElems = VecTy->getVectorNumElements();
1018 if (!isPowerOf2_32(NumVecElems))
1019 return RK_None;
1020
1021 // We look for a sequence of shuffle,shuffle,add triples like the following
1022 // that builds a pairwise reduction tree.
Fangrui Songf78650a2018-07-30 19:41:25 +00001023 //
Guozhi Wei62d64142017-09-08 22:29:17 +00001024 // (X0, X1, X2, X3)
1025 // (X0 + X1, X2 + X3, undef, undef)
1026 // ((X0 + X1) + (X2 + X3), undef, undef, undef)
Fangrui Songf78650a2018-07-30 19:41:25 +00001027 //
Guozhi Wei62d64142017-09-08 22:29:17 +00001028 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef,
1029 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef>
1030 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef,
1031 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef>
1032 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1
1033 // %rdx.shuf.1.0 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef,
1034 // <4 x i32> <i32 0, i32 undef, i32 undef, i32 undef>
1035 // %rdx.shuf.1.1 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef,
1036 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
1037 // %bin.rdx8 = fadd <4 x float> %rdx.shuf.1.0, %rdx.shuf.1.1
1038 // %r = extractelement <4 x float> %bin.rdx8, i32 0
1039 if (matchPairwiseReductionAtLevel(RdxStart, 0, Log2_32(NumVecElems)) ==
1040 RK_None)
1041 return RK_None;
1042
1043 Opcode = RD->Opcode;
1044 Ty = VecTy;
1045
1046 return RD->Kind;
1047}
1048
1049static std::pair<Value *, ShuffleVectorInst *>
1050getShuffleAndOtherOprd(Value *L, Value *R) {
1051 ShuffleVectorInst *S = nullptr;
1052
1053 if ((S = dyn_cast<ShuffleVectorInst>(L)))
1054 return std::make_pair(R, S);
1055
1056 S = dyn_cast<ShuffleVectorInst>(R);
1057 return std::make_pair(L, S);
1058}
1059
1060static ReductionKind
1061matchVectorSplittingReduction(const ExtractElementInst *ReduxRoot,
1062 unsigned &Opcode, Type *&Ty) {
1063 if (!EnableReduxCost)
1064 return RK_None;
1065
1066 // Need to extract the first element.
1067 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1));
1068 unsigned Idx = ~0u;
1069 if (CI)
1070 Idx = CI->getZExtValue();
1071 if (Idx != 0)
1072 return RK_None;
1073
1074 auto *RdxStart = dyn_cast<Instruction>(ReduxRoot->getOperand(0));
1075 if (!RdxStart)
1076 return RK_None;
1077 Optional<ReductionData> RD = getReductionData(RdxStart);
1078 if (!RD)
1079 return RK_None;
1080
1081 Type *VecTy = ReduxRoot->getOperand(0)->getType();
1082 unsigned NumVecElems = VecTy->getVectorNumElements();
1083 if (!isPowerOf2_32(NumVecElems))
1084 return RK_None;
1085
1086 // We look for a sequence of shuffles and adds like the following matching one
1087 // fadd, shuffle vector pair at a time.
Fangrui Songf78650a2018-07-30 19:41:25 +00001088 //
Guozhi Wei62d64142017-09-08 22:29:17 +00001089 // %rdx.shuf = shufflevector <4 x float> %rdx, <4 x float> undef,
1090 // <4 x i32> <i32 2, i32 3, i32 undef, i32 undef>
1091 // %bin.rdx = fadd <4 x float> %rdx, %rdx.shuf
1092 // %rdx.shuf7 = shufflevector <4 x float> %bin.rdx, <4 x float> undef,
1093 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
1094 // %bin.rdx8 = fadd <4 x float> %bin.rdx, %rdx.shuf7
1095 // %r = extractelement <4 x float> %bin.rdx8, i32 0
1096
1097 unsigned MaskStart = 1;
1098 Instruction *RdxOp = RdxStart;
Fangrui Songf78650a2018-07-30 19:41:25 +00001099 SmallVector<int, 32> ShuffleMask(NumVecElems, 0);
Guozhi Wei62d64142017-09-08 22:29:17 +00001100 unsigned NumVecElemsRemain = NumVecElems;
1101 while (NumVecElemsRemain - 1) {
1102 // Check for the right reduction operation.
1103 if (!RdxOp)
1104 return RK_None;
1105 Optional<ReductionData> RDLevel = getReductionData(RdxOp);
1106 if (!RDLevel || !RDLevel->hasSameData(*RD))
1107 return RK_None;
1108
1109 Value *NextRdxOp;
1110 ShuffleVectorInst *Shuffle;
1111 std::tie(NextRdxOp, Shuffle) =
1112 getShuffleAndOtherOprd(RDLevel->LHS, RDLevel->RHS);
1113
1114 // Check the current reduction operation and the shuffle use the same value.
1115 if (Shuffle == nullptr)
1116 return RK_None;
1117 if (Shuffle->getOperand(0) != NextRdxOp)
1118 return RK_None;
1119
1120 // Check that shuffle masks matches.
1121 for (unsigned j = 0; j != MaskStart; ++j)
1122 ShuffleMask[j] = MaskStart + j;
1123 // Fill the rest of the mask with -1 for undef.
1124 std::fill(&ShuffleMask[MaskStart], ShuffleMask.end(), -1);
1125
1126 SmallVector<int, 16> Mask = Shuffle->getShuffleMask();
1127 if (ShuffleMask != Mask)
1128 return RK_None;
1129
1130 RdxOp = dyn_cast<Instruction>(NextRdxOp);
1131 NumVecElemsRemain /= 2;
1132 MaskStart *= 2;
1133 }
1134
1135 Opcode = RD->Opcode;
1136 Ty = VecTy;
1137 return RD->Kind;
1138}
1139
1140int TargetTransformInfo::getInstructionThroughput(const Instruction *I) const {
1141 switch (I->getOpcode()) {
1142 case Instruction::GetElementPtr:
1143 return getUserCost(I);
1144
1145 case Instruction::Ret:
1146 case Instruction::PHI:
1147 case Instruction::Br: {
1148 return getCFInstrCost(I->getOpcode());
1149 }
1150 case Instruction::Add:
1151 case Instruction::FAdd:
1152 case Instruction::Sub:
1153 case Instruction::FSub:
1154 case Instruction::Mul:
1155 case Instruction::FMul:
1156 case Instruction::UDiv:
1157 case Instruction::SDiv:
1158 case Instruction::FDiv:
1159 case Instruction::URem:
1160 case Instruction::SRem:
1161 case Instruction::FRem:
1162 case Instruction::Shl:
1163 case Instruction::LShr:
1164 case Instruction::AShr:
1165 case Instruction::And:
1166 case Instruction::Or:
1167 case Instruction::Xor: {
Simon Pilgrim4162d772018-05-22 10:40:09 +00001168 TargetTransformInfo::OperandValueKind Op1VK, Op2VK;
1169 TargetTransformInfo::OperandValueProperties Op1VP, Op2VP;
1170 Op1VK = getOperandInfo(I->getOperand(0), Op1VP);
1171 Op2VK = getOperandInfo(I->getOperand(1), Op2VP);
1172 SmallVector<const Value *, 2> Operands(I->operand_values());
1173 return getArithmeticInstrCost(I->getOpcode(), I->getType(), Op1VK, Op2VK,
1174 Op1VP, Op2VP, Operands);
Guozhi Wei62d64142017-09-08 22:29:17 +00001175 }
Craig Topper50d50282019-05-28 04:09:18 +00001176 case Instruction::FNeg: {
1177 TargetTransformInfo::OperandValueKind Op1VK, Op2VK;
1178 TargetTransformInfo::OperandValueProperties Op1VP, Op2VP;
1179 Op1VK = getOperandInfo(I->getOperand(0), Op1VP);
1180 Op2VK = OK_AnyValue;
1181 Op2VP = OP_None;
1182 SmallVector<const Value *, 2> Operands(I->operand_values());
1183 return getArithmeticInstrCost(I->getOpcode(), I->getType(), Op1VK, Op2VK,
1184 Op1VP, Op2VP, Operands);
1185 }
Guozhi Wei62d64142017-09-08 22:29:17 +00001186 case Instruction::Select: {
1187 const SelectInst *SI = cast<SelectInst>(I);
1188 Type *CondTy = SI->getCondition()->getType();
1189 return getCmpSelInstrCost(I->getOpcode(), I->getType(), CondTy, I);
1190 }
1191 case Instruction::ICmp:
1192 case Instruction::FCmp: {
1193 Type *ValTy = I->getOperand(0)->getType();
1194 return getCmpSelInstrCost(I->getOpcode(), ValTy, I->getType(), I);
1195 }
1196 case Instruction::Store: {
1197 const StoreInst *SI = cast<StoreInst>(I);
1198 Type *ValTy = SI->getValueOperand()->getType();
1199 return getMemoryOpCost(I->getOpcode(), ValTy,
1200 SI->getAlignment(),
1201 SI->getPointerAddressSpace(), I);
1202 }
1203 case Instruction::Load: {
1204 const LoadInst *LI = cast<LoadInst>(I);
1205 return getMemoryOpCost(I->getOpcode(), I->getType(),
1206 LI->getAlignment(),
1207 LI->getPointerAddressSpace(), I);
1208 }
1209 case Instruction::ZExt:
1210 case Instruction::SExt:
1211 case Instruction::FPToUI:
1212 case Instruction::FPToSI:
1213 case Instruction::FPExt:
1214 case Instruction::PtrToInt:
1215 case Instruction::IntToPtr:
1216 case Instruction::SIToFP:
1217 case Instruction::UIToFP:
1218 case Instruction::Trunc:
1219 case Instruction::FPTrunc:
1220 case Instruction::BitCast:
1221 case Instruction::AddrSpaceCast: {
1222 Type *SrcTy = I->getOperand(0)->getType();
1223 return getCastInstrCost(I->getOpcode(), I->getType(), SrcTy, I);
1224 }
1225 case Instruction::ExtractElement: {
1226 const ExtractElementInst * EEI = cast<ExtractElementInst>(I);
1227 ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1));
1228 unsigned Idx = -1;
1229 if (CI)
1230 Idx = CI->getZExtValue();
1231
1232 // Try to match a reduction sequence (series of shufflevector and vector
1233 // adds followed by a extractelement).
1234 unsigned ReduxOpCode;
1235 Type *ReduxType;
1236
1237 switch (matchVectorSplittingReduction(EEI, ReduxOpCode, ReduxType)) {
1238 case RK_Arithmetic:
1239 return getArithmeticReductionCost(ReduxOpCode, ReduxType,
1240 /*IsPairwiseForm=*/false);
1241 case RK_MinMax:
1242 return getMinMaxReductionCost(
1243 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1244 /*IsPairwiseForm=*/false, /*IsUnsigned=*/false);
1245 case RK_UnsignedMinMax:
1246 return getMinMaxReductionCost(
1247 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1248 /*IsPairwiseForm=*/false, /*IsUnsigned=*/true);
1249 case RK_None:
1250 break;
1251 }
1252
1253 switch (matchPairwiseReduction(EEI, ReduxOpCode, ReduxType)) {
1254 case RK_Arithmetic:
1255 return getArithmeticReductionCost(ReduxOpCode, ReduxType,
1256 /*IsPairwiseForm=*/true);
1257 case RK_MinMax:
1258 return getMinMaxReductionCost(
1259 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1260 /*IsPairwiseForm=*/true, /*IsUnsigned=*/false);
1261 case RK_UnsignedMinMax:
1262 return getMinMaxReductionCost(
1263 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1264 /*IsPairwiseForm=*/true, /*IsUnsigned=*/true);
1265 case RK_None:
1266 break;
1267 }
1268
1269 return getVectorInstrCost(I->getOpcode(),
1270 EEI->getOperand(0)->getType(), Idx);
1271 }
1272 case Instruction::InsertElement: {
1273 const InsertElementInst * IE = cast<InsertElementInst>(I);
1274 ConstantInt *CI = dyn_cast<ConstantInt>(IE->getOperand(2));
Fangrui Songf78650a2018-07-30 19:41:25 +00001275 unsigned Idx = -1;
Guozhi Wei62d64142017-09-08 22:29:17 +00001276 if (CI)
1277 Idx = CI->getZExtValue();
1278 return getVectorInstrCost(I->getOpcode(),
1279 IE->getType(), Idx);
1280 }
Roman Lebedevcc95a452019-08-29 11:50:30 +00001281 case Instruction::ExtractValue:
1282 return 0; // Model all ExtractValue nodes as free.
Guozhi Wei62d64142017-09-08 22:29:17 +00001283 case Instruction::ShuffleVector: {
1284 const ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
Simon Pilgrimd0c71602018-11-09 16:28:19 +00001285 Type *Ty = Shuffle->getType();
1286 Type *SrcTy = Shuffle->getOperand(0)->getType();
1287
1288 // TODO: Identify and add costs for insert subvector, etc.
1289 int SubIndex;
1290 if (Shuffle->isExtractSubvectorMask(SubIndex))
Simon Pilgrim26e1c882018-11-09 18:30:59 +00001291 return TTIImpl->getShuffleCost(SK_ExtractSubvector, SrcTy, SubIndex, Ty);
Simon Pilgrimd0c71602018-11-09 16:28:19 +00001292
Sanjay Patel2ca33602018-06-19 18:44:00 +00001293 if (Shuffle->changesLength())
1294 return -1;
Fangrui Songf78650a2018-07-30 19:41:25 +00001295
Sanjay Patel2ca33602018-06-19 18:44:00 +00001296 if (Shuffle->isIdentity())
1297 return 0;
Guozhi Wei62d64142017-09-08 22:29:17 +00001298
Sanjay Patel2ca33602018-06-19 18:44:00 +00001299 if (Shuffle->isReverse())
1300 return TTIImpl->getShuffleCost(SK_Reverse, Ty, 0, nullptr);
Simon Pilgrim07839212018-06-12 14:47:13 +00001301
Sanjay Patel2ca33602018-06-19 18:44:00 +00001302 if (Shuffle->isSelect())
1303 return TTIImpl->getShuffleCost(SK_Select, Ty, 0, nullptr);
Simon Pilgrim07839212018-06-12 14:47:13 +00001304
Sanjay Patel2ca33602018-06-19 18:44:00 +00001305 if (Shuffle->isTranspose())
1306 return TTIImpl->getShuffleCost(SK_Transpose, Ty, 0, nullptr);
Matthew Simpsonb4096eb2018-04-26 13:48:33 +00001307
Sanjay Patel2ca33602018-06-19 18:44:00 +00001308 if (Shuffle->isZeroEltSplat())
1309 return TTIImpl->getShuffleCost(SK_Broadcast, Ty, 0, nullptr);
Guozhi Wei62d64142017-09-08 22:29:17 +00001310
Sanjay Patel2ca33602018-06-19 18:44:00 +00001311 if (Shuffle->isSingleSource())
1312 return TTIImpl->getShuffleCost(SK_PermuteSingleSrc, Ty, 0, nullptr);
Guozhi Wei62d64142017-09-08 22:29:17 +00001313
Sanjay Patel2ca33602018-06-19 18:44:00 +00001314 return TTIImpl->getShuffleCost(SK_PermuteTwoSrc, Ty, 0, nullptr);
Guozhi Wei62d64142017-09-08 22:29:17 +00001315 }
1316 case Instruction::Call:
1317 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1318 SmallVector<Value *, 4> Args(II->arg_operands());
1319
1320 FastMathFlags FMF;
1321 if (auto *FPMO = dyn_cast<FPMathOperator>(II))
1322 FMF = FPMO->getFastMathFlags();
1323
1324 return getIntrinsicInstrCost(II->getIntrinsicID(), II->getType(),
1325 Args, FMF);
1326 }
1327 return -1;
1328 default:
1329 // We don't have any information on this instruction.
1330 return -1;
1331 }
1332}
1333
Chandler Carruth705b1852015-01-31 03:43:40 +00001334TargetTransformInfo::Concept::~Concept() {}
1335
Chandler Carruthe0385522015-02-01 10:11:22 +00001336TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
1337
1338TargetIRAnalysis::TargetIRAnalysis(
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001339 std::function<Result(const Function &)> TTICallback)
Benjamin Kramer82de7d32016-05-27 14:27:24 +00001340 : TTICallback(std::move(TTICallback)) {}
Chandler Carruthe0385522015-02-01 10:11:22 +00001341
Chandler Carruth164a2aa62016-06-17 00:11:01 +00001342TargetIRAnalysis::Result TargetIRAnalysis::run(const Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +00001343 FunctionAnalysisManager &) {
Chandler Carruthe0385522015-02-01 10:11:22 +00001344 return TTICallback(F);
1345}
1346
Chandler Carruthdab4eae2016-11-23 17:53:26 +00001347AnalysisKey TargetIRAnalysis::Key;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +00001348
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001349TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(const Function &F) {
Mehdi Amini5010ebf2015-07-09 02:08:42 +00001350 return Result(F.getParent()->getDataLayout());
Chandler Carruthe0385522015-02-01 10:11:22 +00001351}
1352
Chandler Carruth705b1852015-01-31 03:43:40 +00001353// Register the basic pass.
1354INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
1355 "Target Transform Information", false, true)
1356char TargetTransformInfoWrapperPass::ID = 0;
Chandler Carruth539edf42013-01-05 11:43:11 +00001357
Chandler Carruth705b1852015-01-31 03:43:40 +00001358void TargetTransformInfoWrapperPass::anchor() {}
Chandler Carruth539edf42013-01-05 11:43:11 +00001359
Chandler Carruth705b1852015-01-31 03:43:40 +00001360TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass()
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001361 : ImmutablePass(ID) {
Chandler Carruth705b1852015-01-31 03:43:40 +00001362 initializeTargetTransformInfoWrapperPassPass(
1363 *PassRegistry::getPassRegistry());
1364}
1365
1366TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass(
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001367 TargetIRAnalysis TIRA)
1368 : ImmutablePass(ID), TIRA(std::move(TIRA)) {
Chandler Carruth705b1852015-01-31 03:43:40 +00001369 initializeTargetTransformInfoWrapperPassPass(
1370 *PassRegistry::getPassRegistry());
1371}
1372
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001373TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(const Function &F) {
Sean Silva36e0d012016-08-09 00:28:15 +00001374 FunctionAnalysisManager DummyFAM;
Chandler Carruth164a2aa62016-06-17 00:11:01 +00001375 TTI = TIRA.run(F, DummyFAM);
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001376 return *TTI;
1377}
1378
Chandler Carruth93dcdc42015-01-31 11:17:59 +00001379ImmutablePass *
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001380llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) {
1381 return new TargetTransformInfoWrapperPass(std::move(TIRA));
Chandler Carruth539edf42013-01-05 11:43:11 +00001382}