blob: ba89a9eebdb66e20c5f29e1ea850b451f682ffca [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"
Sam Parker95aee9d2019-10-01 07:53:28 +000012#include "llvm/IR/CFG.h"
Chandler Carruth511aa762013-01-21 01:27:39 +000013#include "llvm/IR/DataLayout.h"
Chandler Carruth511aa762013-01-21 01:27:39 +000014#include "llvm/IR/Instruction.h"
Chandler Carruth511aa762013-01-21 01:27:39 +000015#include "llvm/IR/Instructions.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "llvm/IR/IntrinsicInst.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000017#include "llvm/IR/Module.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000018#include "llvm/IR/Operator.h"
Guozhi Wei62d64142017-09-08 22:29:17 +000019#include "llvm/IR/PatternMatch.h"
Sean Fertile9cd1cdf2017-07-07 02:00:06 +000020#include "llvm/Support/CommandLine.h"
Nadav Rotem5dc203e2012-10-18 23:22:48 +000021#include "llvm/Support/ErrorHandling.h"
Chen Zheng46ce9e42019-06-26 09:12:52 +000022#include "llvm/Analysis/CFG.h"
23#include "llvm/Analysis/LoopIterator.h"
Benjamin Kramer82de7d32016-05-27 14:27:24 +000024#include <utility>
Nadav Rotem5dc203e2012-10-18 23:22:48 +000025
26using namespace llvm;
Guozhi Wei62d64142017-09-08 22:29:17 +000027using namespace PatternMatch;
Nadav Rotem5dc203e2012-10-18 23:22:48 +000028
Chandler Carruthf1221bd2014-04-22 02:48:03 +000029#define DEBUG_TYPE "tti"
30
Guozhi Wei62d64142017-09-08 22:29:17 +000031static cl::opt<bool> EnableReduxCost("costmodel-reduxcost", cl::init(false),
32 cl::Hidden,
33 cl::desc("Recognize reduction patterns."));
34
Chandler Carruth93dcdc42015-01-31 11:17:59 +000035namespace {
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000036/// No-op implementation of the TTI interface using the utility base
Chandler Carruth93dcdc42015-01-31 11:17:59 +000037/// classes.
38///
39/// This is used when no target specific information is available.
40struct NoTTIImpl : TargetTransformInfoImplCRTPBase<NoTTIImpl> {
Mehdi Amini5010ebf2015-07-09 02:08:42 +000041 explicit NoTTIImpl(const DataLayout &DL)
Chandler Carruth93dcdc42015-01-31 11:17:59 +000042 : TargetTransformInfoImplCRTPBase<NoTTIImpl>(DL) {}
43};
44}
45
Chen Zhengaa999522019-06-26 12:02:43 +000046bool HardwareLoopInfo::canAnalyze(LoopInfo &LI) {
47 // If the loop has irreducible control flow, it can not be converted to
48 // Hardware loop.
49 LoopBlocksRPO RPOT(L);
50 RPOT.perform(&LI);
51 if (containsIrreducibleCFG<const BasicBlock *>(RPOT, LI))
52 return false;
53 return true;
54}
55
Chen Zhengc5b918d2019-06-19 01:26:31 +000056bool HardwareLoopInfo::isHardwareLoopCandidate(ScalarEvolution &SE,
57 LoopInfo &LI, DominatorTree &DT,
58 bool ForceNestedLoop,
Jinsong Ji06fef0b2019-07-09 17:53:09 +000059 bool ForceHardwareLoopPHI) {
Chen Zhengc5b918d2019-06-19 01:26:31 +000060 SmallVector<BasicBlock *, 4> ExitingBlocks;
61 L->getExitingBlocks(ExitingBlocks);
62
Sam Parker95aee9d2019-10-01 07:53:28 +000063 for (BasicBlock *BB : ExitingBlocks) {
Chen Zhengc5b918d2019-06-19 01:26:31 +000064 // If we pass the updated counter back through a phi, we need to know
65 // which latch the updated value will be coming from.
66 if (!L->isLoopLatch(BB)) {
67 if (ForceHardwareLoopPHI || CounterInReg)
68 continue;
69 }
70
71 const SCEV *EC = SE.getExitCount(L, BB);
72 if (isa<SCEVCouldNotCompute>(EC))
73 continue;
74 if (const SCEVConstant *ConstEC = dyn_cast<SCEVConstant>(EC)) {
75 if (ConstEC->getValue()->isZero())
76 continue;
77 } else if (!SE.isLoopInvariant(EC, L))
78 continue;
79
80 if (SE.getTypeSizeInBits(EC->getType()) > CountType->getBitWidth())
81 continue;
82
83 // If this exiting block is contained in a nested loop, it is not eligible
84 // for insertion of the branch-and-decrement since the inner loop would
85 // end up messing up the value in the CTR.
86 if (!IsNestingLegal && LI.getLoopFor(BB) != L && !ForceNestedLoop)
87 continue;
88
89 // We now have a loop-invariant count of loop iterations (which is not the
90 // constant zero) for which we know that this loop will not exit via this
91 // existing block.
92
93 // We need to make sure that this block will run on every loop iteration.
94 // For this to be true, we must dominate all blocks with backedges. Such
95 // blocks are in-loop predecessors to the header block.
96 bool NotAlways = false;
Sam Parker95aee9d2019-10-01 07:53:28 +000097 for (BasicBlock *Pred : predecessors(L->getHeader())) {
98 if (!L->contains(Pred))
Chen Zhengc5b918d2019-06-19 01:26:31 +000099 continue;
100
Sam Parker95aee9d2019-10-01 07:53:28 +0000101 if (!DT.dominates(BB, Pred)) {
Chen Zhengc5b918d2019-06-19 01:26:31 +0000102 NotAlways = true;
103 break;
104 }
105 }
106
107 if (NotAlways)
108 continue;
109
110 // Make sure this blocks ends with a conditional branch.
111 Instruction *TI = BB->getTerminator();
112 if (!TI)
113 continue;
114
115 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
116 if (!BI->isConditional())
117 continue;
118
119 ExitBranch = BI;
120 } else
121 continue;
122
123 // Note that this block may not be the loop latch block, even if the loop
124 // has a latch block.
Sam Parker95aee9d2019-10-01 07:53:28 +0000125 ExitBlock = BB;
Chen Zhengc5b918d2019-06-19 01:26:31 +0000126 ExitCount = EC;
127 break;
128 }
129
130 if (!ExitBlock)
131 return false;
132 return true;
133}
134
Mehdi Amini5010ebf2015-07-09 02:08:42 +0000135TargetTransformInfo::TargetTransformInfo(const DataLayout &DL)
Chandler Carruth93dcdc42015-01-31 11:17:59 +0000136 : TTIImpl(new Model<NoTTIImpl>(NoTTIImpl(DL))) {}
137
Chandler Carruth705b1852015-01-31 03:43:40 +0000138TargetTransformInfo::~TargetTransformInfo() {}
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000139
Chandler Carruth705b1852015-01-31 03:43:40 +0000140TargetTransformInfo::TargetTransformInfo(TargetTransformInfo &&Arg)
141 : TTIImpl(std::move(Arg.TTIImpl)) {}
Chandler Carruth539edf42013-01-05 11:43:11 +0000142
Chandler Carruth705b1852015-01-31 03:43:40 +0000143TargetTransformInfo &TargetTransformInfo::operator=(TargetTransformInfo &&RHS) {
144 TTIImpl = std::move(RHS.TTIImpl);
145 return *this;
Chandler Carruth539edf42013-01-05 11:43:11 +0000146}
147
Chandler Carruth93205eb2015-08-05 18:08:10 +0000148int TargetTransformInfo::getOperationCost(unsigned Opcode, Type *Ty,
149 Type *OpTy) const {
150 int Cost = TTIImpl->getOperationCost(Opcode, Ty, OpTy);
151 assert(Cost >= 0 && "TTI should not produce negative costs!");
152 return Cost;
Chandler Carruth511aa762013-01-21 01:27:39 +0000153}
154
Sjoerd Meijer31ff6472019-03-12 09:48:02 +0000155int TargetTransformInfo::getCallCost(FunctionType *FTy, int NumArgs,
156 const User *U) const {
157 int Cost = TTIImpl->getCallCost(FTy, NumArgs, U);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000158 assert(Cost >= 0 && "TTI should not produce negative costs!");
159 return Cost;
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000160}
161
Chandler Carruth93205eb2015-08-05 18:08:10 +0000162int TargetTransformInfo::getCallCost(const Function *F,
Sjoerd Meijer31ff6472019-03-12 09:48:02 +0000163 ArrayRef<const Value *> Arguments,
164 const User *U) const {
165 int Cost = TTIImpl->getCallCost(F, Arguments, U);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000166 assert(Cost >= 0 && "TTI should not produce negative costs!");
167 return Cost;
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000168}
169
Justin Lebar8650a4d2016-04-15 01:38:48 +0000170unsigned TargetTransformInfo::getInliningThresholdMultiplier() const {
171 return TTIImpl->getInliningThresholdMultiplier();
172}
173
Daniil Fukalovd912a9b2019-07-17 16:51:29 +0000174int TargetTransformInfo::getInlinerVectorBonusPercent() const {
175 return TTIImpl->getInlinerVectorBonusPercent();
176}
177
Jingyue Wu15f3e822016-07-08 21:48:05 +0000178int TargetTransformInfo::getGEPCost(Type *PointeeType, const Value *Ptr,
179 ArrayRef<const Value *> Operands) const {
180 return TTIImpl->getGEPCost(PointeeType, Ptr, Operands);
181}
182
Haicheng Wuabdef9e2017-07-15 02:12:16 +0000183int TargetTransformInfo::getExtCost(const Instruction *I,
184 const Value *Src) const {
185 return TTIImpl->getExtCost(I, Src);
186}
187
Chandler Carruth93205eb2015-08-05 18:08:10 +0000188int TargetTransformInfo::getIntrinsicCost(
Sjoerd Meijer31ff6472019-03-12 09:48:02 +0000189 Intrinsic::ID IID, Type *RetTy, ArrayRef<const Value *> Arguments,
190 const User *U) const {
191 int Cost = TTIImpl->getIntrinsicCost(IID, RetTy, Arguments, U);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000192 assert(Cost >= 0 && "TTI should not produce negative costs!");
193 return Cost;
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000194}
195
Jun Bum Lim919f9e82017-04-28 16:04:03 +0000196unsigned
Hiroshi Yamauchi0d987e42019-10-29 11:30:30 -0700197TargetTransformInfo::getEstimatedNumberOfCaseClusters(
198 const SwitchInst &SI, unsigned &JTSize, ProfileSummaryInfo *PSI,
199 BlockFrequencyInfo *BFI) const {
200 return TTIImpl->getEstimatedNumberOfCaseClusters(SI, JTSize, PSI, BFI);
Jun Bum Lim919f9e82017-04-28 16:04:03 +0000201}
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
Matt Arsenaultdbc1f202019-08-14 18:13:00 +0000226bool TargetTransformInfo::collectFlatAddressOperands(
227 SmallVectorImpl<int> &OpIndexes, Intrinsic::ID IID) const {
228 return TTIImpl->collectFlatAddressOperands(OpIndexes, IID);
229}
230
231bool TargetTransformInfo::rewriteIntrinsicWithAddressSpace(
232 IntrinsicInst *II, Value *OldV, Value *NewV) const {
233 return TTIImpl->rewriteIntrinsicWithAddressSpace(II, OldV, NewV);
234}
235
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000236bool TargetTransformInfo::isLoweredToCall(const Function *F) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000237 return TTIImpl->isLoweredToCall(F);
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000238}
239
Sam Parkerc5ef5022019-06-07 07:35:30 +0000240bool TargetTransformInfo::isHardwareLoopProfitable(
Chen Zhengaa999522019-06-26 12:02:43 +0000241 Loop *L, ScalarEvolution &SE, AssumptionCache &AC,
Sam Parkerc5ef5022019-06-07 07:35:30 +0000242 TargetLibraryInfo *LibInfo, HardwareLoopInfo &HWLoopInfo) const {
243 return TTIImpl->isHardwareLoopProfitable(L, SE, AC, LibInfo, HWLoopInfo);
244}
245
Chandler Carruth705b1852015-01-31 03:43:40 +0000246void TargetTransformInfo::getUnrollingPreferences(
Geoff Berry66d9bdb2017-06-28 15:53:17 +0000247 Loop *L, ScalarEvolution &SE, UnrollingPreferences &UP) const {
248 return TTIImpl->getUnrollingPreferences(L, SE, UP);
Hal Finkel8f2e7002013-09-11 19:25:43 +0000249}
250
Chandler Carruth539edf42013-01-05 11:43:11 +0000251bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000252 return TTIImpl->isLegalAddImmediate(Imm);
Chandler Carruth539edf42013-01-05 11:43:11 +0000253}
254
255bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000256 return TTIImpl->isLegalICmpImmediate(Imm);
Chandler Carruth539edf42013-01-05 11:43:11 +0000257}
258
259bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
260 int64_t BaseOffset,
261 bool HasBaseReg,
Matt Arsenaulte83379e2015-06-07 20:12:03 +0000262 int64_t Scale,
Jonas Paulsson024e3192017-07-21 11:59:37 +0000263 unsigned AddrSpace,
264 Instruction *I) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000265 return TTIImpl->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
Jonas Paulsson024e3192017-07-21 11:59:37 +0000266 Scale, AddrSpace, I);
Chandler Carruth539edf42013-01-05 11:43:11 +0000267}
268
Evgeny Stupachenkof2b3b462017-06-05 23:37:00 +0000269bool TargetTransformInfo::isLSRCostLess(LSRCost &C1, LSRCost &C2) const {
270 return TTIImpl->isLSRCostLess(C1, C2);
271}
272
Sanjay Pateld7c702b2018-02-05 23:43:05 +0000273bool TargetTransformInfo::canMacroFuseCmp() const {
274 return TTIImpl->canMacroFuseCmp();
275}
276
Chen Zhengdfdccbb2019-07-03 01:49:03 +0000277bool TargetTransformInfo::canSaveCmp(Loop *L, BranchInst **BI,
278 ScalarEvolution *SE, LoopInfo *LI,
279 DominatorTree *DT, AssumptionCache *AC,
280 TargetLibraryInfo *LibInfo) const {
281 return TTIImpl->canSaveCmp(L, BI, SE, LI, DT, AC, LibInfo);
282}
283
Krzysztof Parzyszek0b377e02018-03-26 13:10:09 +0000284bool TargetTransformInfo::shouldFavorPostInc() const {
285 return TTIImpl->shouldFavorPostInc();
286}
287
Sam Parker67756c02019-02-07 13:32:54 +0000288bool TargetTransformInfo::shouldFavorBackedgeIndex(const Loop *L) const {
289 return TTIImpl->shouldFavorBackedgeIndex(L);
290}
291
Sam Parker527a35e2019-10-14 10:00:21 +0000292bool TargetTransformInfo::isLegalMaskedStore(Type *DataType,
293 MaybeAlign Alignment) const {
294 return TTIImpl->isLegalMaskedStore(DataType, Alignment);
Chandler Carruth705b1852015-01-31 03:43:40 +0000295}
296
Sam Parker527a35e2019-10-14 10:00:21 +0000297bool TargetTransformInfo::isLegalMaskedLoad(Type *DataType,
298 MaybeAlign Alignment) const {
299 return TTIImpl->isLegalMaskedLoad(DataType, Alignment);
Chandler Carruth705b1852015-01-31 03:43:40 +0000300}
301
Warren Ristow6452bdd2019-06-17 17:20:08 +0000302bool TargetTransformInfo::isLegalNTStore(Type *DataType,
Guillaume Chatelet18f805a2019-09-27 12:54:21 +0000303 Align Alignment) const {
Warren Ristow6452bdd2019-06-17 17:20:08 +0000304 return TTIImpl->isLegalNTStore(DataType, Alignment);
305}
306
Guillaume Chatelet18f805a2019-09-27 12:54:21 +0000307bool TargetTransformInfo::isLegalNTLoad(Type *DataType, Align Alignment) const {
Warren Ristow6452bdd2019-06-17 17:20:08 +0000308 return TTIImpl->isLegalNTLoad(DataType, Alignment);
309}
310
Elena Demikhovsky09285852015-10-25 15:37:55 +0000311bool TargetTransformInfo::isLegalMaskedGather(Type *DataType) const {
312 return TTIImpl->isLegalMaskedGather(DataType);
313}
314
315bool TargetTransformInfo::isLegalMaskedScatter(Type *DataType) const {
Mohammed Agabariacef53dc2017-07-27 10:28:16 +0000316 return TTIImpl->isLegalMaskedScatter(DataType);
Elena Demikhovsky09285852015-10-25 15:37:55 +0000317}
318
Craig Topper9f0b17a2019-03-21 17:38:52 +0000319bool TargetTransformInfo::isLegalMaskedCompressStore(Type *DataType) const {
320 return TTIImpl->isLegalMaskedCompressStore(DataType);
321}
322
323bool TargetTransformInfo::isLegalMaskedExpandLoad(Type *DataType) const {
324 return TTIImpl->isLegalMaskedExpandLoad(DataType);
325}
326
Sanjay Patel6fd43912017-09-09 13:38:18 +0000327bool TargetTransformInfo::hasDivRemOp(Type *DataType, bool IsSigned) const {
328 return TTIImpl->hasDivRemOp(DataType, IsSigned);
329}
330
Artem Belevichcb8f6322017-10-24 20:31:44 +0000331bool TargetTransformInfo::hasVolatileVariant(Instruction *I,
332 unsigned AddrSpace) const {
333 return TTIImpl->hasVolatileVariant(I, AddrSpace);
334}
335
Jonas Paulsson8624b7e2017-05-24 13:42:56 +0000336bool TargetTransformInfo::prefersVectorizedAddressing() const {
337 return TTIImpl->prefersVectorizedAddressing();
338}
339
Quentin Colombetbf490d42013-05-31 21:29:03 +0000340int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
341 int64_t BaseOffset,
342 bool HasBaseReg,
Matt Arsenaulte83379e2015-06-07 20:12:03 +0000343 int64_t Scale,
344 unsigned AddrSpace) const {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000345 int Cost = TTIImpl->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg,
346 Scale, AddrSpace);
347 assert(Cost >= 0 && "TTI should not produce negative costs!");
348 return Cost;
Quentin Colombetbf490d42013-05-31 21:29:03 +0000349}
350
Jonas Paulsson024e3192017-07-21 11:59:37 +0000351bool TargetTransformInfo::LSRWithInstrQueries() const {
352 return TTIImpl->LSRWithInstrQueries();
353}
354
Chandler Carruth539edf42013-01-05 11:43:11 +0000355bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000356 return TTIImpl->isTruncateFree(Ty1, Ty2);
Chandler Carruth539edf42013-01-05 11:43:11 +0000357}
358
Chad Rosier54390052015-02-23 19:15:16 +0000359bool TargetTransformInfo::isProfitableToHoist(Instruction *I) const {
360 return TTIImpl->isProfitableToHoist(I);
361}
362
David Blaikie8ad9a972018-03-28 22:28:50 +0000363bool TargetTransformInfo::useAA() const { return TTIImpl->useAA(); }
364
Chandler Carruth539edf42013-01-05 11:43:11 +0000365bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000366 return TTIImpl->isTypeLegal(Ty);
Chandler Carruth539edf42013-01-05 11:43:11 +0000367}
368
Chandler Carruth539edf42013-01-05 11:43:11 +0000369bool TargetTransformInfo::shouldBuildLookupTables() const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000370 return TTIImpl->shouldBuildLookupTables();
Chandler Carruth539edf42013-01-05 11:43:11 +0000371}
Oliver Stannard4df1cc02016-10-07 08:48:24 +0000372bool TargetTransformInfo::shouldBuildLookupTablesForConstant(Constant *C) const {
373 return TTIImpl->shouldBuildLookupTablesForConstant(C);
374}
Chandler Carruth539edf42013-01-05 11:43:11 +0000375
Zaara Syeda1f59ae32018-01-30 16:17:22 +0000376bool TargetTransformInfo::useColdCCForColdCall(Function &F) const {
377 return TTIImpl->useColdCCForColdCall(F);
378}
379
Jonas Paulsson8e2f9482017-01-26 07:03:25 +0000380unsigned TargetTransformInfo::
381getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const {
382 return TTIImpl->getScalarizationOverhead(Ty, Insert, Extract);
383}
384
385unsigned TargetTransformInfo::
386getOperandsScalarizationOverhead(ArrayRef<const Value *> Args,
387 unsigned VF) const {
388 return TTIImpl->getOperandsScalarizationOverhead(Args, VF);
389}
390
Jonas Paulssonda74ed42017-04-12 12:41:37 +0000391bool TargetTransformInfo::supportsEfficientVectorElementLoadStore() const {
392 return TTIImpl->supportsEfficientVectorElementLoadStore();
393}
394
Olivier Sallenave049d8032015-03-06 23:12:04 +0000395bool TargetTransformInfo::enableAggressiveInterleaving(bool LoopHasReductions) const {
396 return TTIImpl->enableAggressiveInterleaving(LoopHasReductions);
397}
398
Clement Courbet3bc5ad52019-06-25 08:04:13 +0000399TargetTransformInfo::MemCmpExpansionOptions
400TargetTransformInfo::enableMemCmpExpansion(bool OptSize, bool IsZeroCmp) const {
401 return TTIImpl->enableMemCmpExpansion(OptSize, IsZeroCmp);
Zaara Syeda3a7578c2017-05-31 17:12:38 +0000402}
403
Silviu Baranga61bdc512015-08-10 14:50:54 +0000404bool TargetTransformInfo::enableInterleavedAccessVectorization() const {
405 return TTIImpl->enableInterleavedAccessVectorization();
406}
407
Dorit Nuzman38bbf812018-10-14 08:50:06 +0000408bool TargetTransformInfo::enableMaskedInterleavedAccessVectorization() const {
409 return TTIImpl->enableMaskedInterleavedAccessVectorization();
410}
411
Renato Golin5cb666a2016-04-14 20:42:18 +0000412bool TargetTransformInfo::isFPVectorizationPotentiallyUnsafe() const {
413 return TTIImpl->isFPVectorizationPotentiallyUnsafe();
414}
415
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000416bool TargetTransformInfo::allowsMisalignedMemoryAccesses(LLVMContext &Context,
417 unsigned BitWidth,
Alina Sbirlea327955e2016-07-11 20:46:17 +0000418 unsigned AddressSpace,
419 unsigned Alignment,
420 bool *Fast) const {
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000421 return TTIImpl->allowsMisalignedMemoryAccesses(Context, BitWidth, AddressSpace,
Alina Sbirlea327955e2016-07-11 20:46:17 +0000422 Alignment, Fast);
423}
424
Chandler Carruth50a36cd2013-01-07 03:16:03 +0000425TargetTransformInfo::PopcntSupportKind
426TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000427 return TTIImpl->getPopcntSupport(IntTyWidthInBit);
Chandler Carruth539edf42013-01-05 11:43:11 +0000428}
429
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000430bool TargetTransformInfo::haveFastSqrt(Type *Ty) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000431 return TTIImpl->haveFastSqrt(Ty);
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000432}
433
Sanjay Patel0de1a4b2017-11-27 21:15:43 +0000434bool TargetTransformInfo::isFCmpOrdCheaperThanFCmpZero(Type *Ty) const {
435 return TTIImpl->isFCmpOrdCheaperThanFCmpZero(Ty);
436}
437
Chandler Carruth93205eb2015-08-05 18:08:10 +0000438int TargetTransformInfo::getFPOpCost(Type *Ty) const {
439 int Cost = TTIImpl->getFPOpCost(Ty);
440 assert(Cost >= 0 && "TTI should not produce negative costs!");
441 return Cost;
Cameron Esfahani17177d12015-02-05 02:09:33 +0000442}
443
Sjoerd Meijer38c2cd02016-07-14 07:44:20 +0000444int TargetTransformInfo::getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx,
445 const APInt &Imm,
446 Type *Ty) const {
447 int Cost = TTIImpl->getIntImmCodeSizeCost(Opcode, Idx, Imm, Ty);
448 assert(Cost >= 0 && "TTI should not produce negative costs!");
449 return Cost;
450}
451
Chandler Carruth93205eb2015-08-05 18:08:10 +0000452int TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
453 int Cost = TTIImpl->getIntImmCost(Imm, Ty);
454 assert(Cost >= 0 && "TTI should not produce negative costs!");
455 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000456}
457
Chandler Carruth93205eb2015-08-05 18:08:10 +0000458int TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx,
459 const APInt &Imm, Type *Ty) const {
460 int Cost = TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty);
461 assert(Cost >= 0 && "TTI should not produce negative costs!");
462 return Cost;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000463}
464
Chandler Carruth93205eb2015-08-05 18:08:10 +0000465int TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
466 const APInt &Imm, Type *Ty) const {
467 int Cost = TTIImpl->getIntImmCost(IID, Idx, Imm, Ty);
468 assert(Cost >= 0 && "TTI should not produce negative costs!");
469 return Cost;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000470}
471
Zi Xuan Wu98022682019-10-12 02:53:04 +0000472unsigned TargetTransformInfo::getNumberOfRegisters(unsigned ClassID) const {
473 return TTIImpl->getNumberOfRegisters(ClassID);
474}
475
476unsigned TargetTransformInfo::getRegisterClassForType(bool Vector, Type *Ty) const {
477 return TTIImpl->getRegisterClassForType(Vector, Ty);
478}
479
480const char* TargetTransformInfo::getRegisterClassName(unsigned ClassID) const {
481 return TTIImpl->getRegisterClassName(ClassID);
Chandler Carruth539edf42013-01-05 11:43:11 +0000482}
483
Nadav Rotemb1791a72013-01-09 22:29:00 +0000484unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000485 return TTIImpl->getRegisterBitWidth(Vector);
Nadav Rotemb1791a72013-01-09 22:29:00 +0000486}
487
Adam Nemete29686e2017-05-15 21:15:01 +0000488unsigned TargetTransformInfo::getMinVectorRegisterBitWidth() const {
489 return TTIImpl->getMinVectorRegisterBitWidth();
490}
491
Krzysztof Parzyszek5d93fdf2018-03-27 16:14:11 +0000492bool TargetTransformInfo::shouldMaximizeVectorBandwidth(bool OptSize) const {
493 return TTIImpl->shouldMaximizeVectorBandwidth(OptSize);
494}
495
Krzysztof Parzyszekdfed9412018-04-13 20:16:32 +0000496unsigned TargetTransformInfo::getMinimumVF(unsigned ElemWidth) const {
497 return TTIImpl->getMinimumVF(ElemWidth);
498}
499
Jun Bum Limdee55652017-04-03 19:20:07 +0000500bool TargetTransformInfo::shouldConsiderAddressTypePromotion(
501 const Instruction &I, bool &AllowPromotionWithoutCommonHeader) const {
502 return TTIImpl->shouldConsiderAddressTypePromotion(
503 I, AllowPromotionWithoutCommonHeader);
504}
505
Adam Nemetaf761102016-01-21 18:28:36 +0000506unsigned TargetTransformInfo::getCacheLineSize() const {
507 return TTIImpl->getCacheLineSize();
508}
509
Tobias Grosserd7eb6192017-08-24 09:46:25 +0000510llvm::Optional<unsigned> TargetTransformInfo::getCacheSize(CacheLevel Level)
511 const {
512 return TTIImpl->getCacheSize(Level);
513}
514
515llvm::Optional<unsigned> TargetTransformInfo::getCacheAssociativity(
516 CacheLevel Level) const {
517 return TTIImpl->getCacheAssociativity(Level);
518}
519
Adam Nemetdadfbb52016-01-27 22:21:25 +0000520unsigned TargetTransformInfo::getPrefetchDistance() const {
521 return TTIImpl->getPrefetchDistance();
522}
523
Adam Nemet6d8beec2016-03-18 00:27:38 +0000524unsigned TargetTransformInfo::getMinPrefetchStride() const {
525 return TTIImpl->getMinPrefetchStride();
526}
527
Adam Nemet709e3042016-03-18 00:27:43 +0000528unsigned TargetTransformInfo::getMaxPrefetchIterationsAhead() const {
529 return TTIImpl->getMaxPrefetchIterationsAhead();
530}
531
Wei Mi062c7442015-05-06 17:12:25 +0000532unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const {
533 return TTIImpl->getMaxInterleaveFactor(VF);
Nadav Rotemb696c362013-01-09 01:15:42 +0000534}
535
Jonas Paulsson29d80f02018-10-05 14:34:04 +0000536TargetTransformInfo::OperandValueKind
Simon Pilgrim077a42c2018-11-13 13:45:10 +0000537TargetTransformInfo::getOperandInfo(Value *V, OperandValueProperties &OpProps) {
Jonas Paulsson29d80f02018-10-05 14:34:04 +0000538 OperandValueKind OpInfo = OK_AnyValue;
539 OpProps = OP_None;
540
541 if (auto *CI = dyn_cast<ConstantInt>(V)) {
542 if (CI->getValue().isPowerOf2())
543 OpProps = OP_PowerOf2;
544 return OK_UniformConstantValue;
545 }
546
Simon Pilgrim2b166c52018-11-14 15:04:08 +0000547 // A broadcast shuffle creates a uniform value.
548 // TODO: Add support for non-zero index broadcasts.
549 // TODO: Add support for different source vector width.
550 if (auto *ShuffleInst = dyn_cast<ShuffleVectorInst>(V))
551 if (ShuffleInst->isZeroEltSplat())
552 OpInfo = OK_UniformValue;
553
Jonas Paulsson29d80f02018-10-05 14:34:04 +0000554 const Value *Splat = getSplatValue(V);
555
556 // Check for a splat of a constant or for a non uniform vector of constants
557 // and check if the constant(s) are all powers of two.
558 if (isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) {
559 OpInfo = OK_NonUniformConstantValue;
560 if (Splat) {
561 OpInfo = OK_UniformConstantValue;
562 if (auto *CI = dyn_cast<ConstantInt>(Splat))
563 if (CI->getValue().isPowerOf2())
564 OpProps = OP_PowerOf2;
565 } else if (auto *CDS = dyn_cast<ConstantDataSequential>(V)) {
566 OpProps = OP_PowerOf2;
567 for (unsigned I = 0, E = CDS->getNumElements(); I != E; ++I) {
568 if (auto *CI = dyn_cast<ConstantInt>(CDS->getElementAsConstant(I)))
569 if (CI->getValue().isPowerOf2())
570 continue;
571 OpProps = OP_None;
572 break;
573 }
574 }
575 }
576
577 // Check for a splat of a uniform value. This is not loop aware, so return
578 // true only for the obviously uniform cases (argument, globalvalue)
579 if (Splat && (isa<Argument>(Splat) || isa<GlobalValue>(Splat)))
580 OpInfo = OK_UniformValue;
581
582 return OpInfo;
583}
584
Chandler Carruth93205eb2015-08-05 18:08:10 +0000585int TargetTransformInfo::getArithmeticInstrCost(
Chandler Carruth705b1852015-01-31 03:43:40 +0000586 unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
587 OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000588 OperandValueProperties Opd2PropInfo,
589 ArrayRef<const Value *> Args) const {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000590 int Cost = TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000591 Opd1PropInfo, Opd2PropInfo, Args);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000592 assert(Cost >= 0 && "TTI should not produce negative costs!");
593 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000594}
595
Chandler Carruth93205eb2015-08-05 18:08:10 +0000596int TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty, int Index,
597 Type *SubTp) const {
598 int Cost = TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
599 assert(Cost >= 0 && "TTI should not produce negative costs!");
600 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000601}
602
Chandler Carruth93205eb2015-08-05 18:08:10 +0000603int TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000604 Type *Src, const Instruction *I) const {
605 assert ((I == nullptr || I->getOpcode() == Opcode) &&
606 "Opcode should reflect passed instruction.");
607 int Cost = TTIImpl->getCastInstrCost(Opcode, Dst, Src, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000608 assert(Cost >= 0 && "TTI should not produce negative costs!");
609 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000610}
611
Matthew Simpsone5dfb082016-04-27 15:20:21 +0000612int TargetTransformInfo::getExtractWithExtendCost(unsigned Opcode, Type *Dst,
613 VectorType *VecTy,
614 unsigned Index) const {
615 int Cost = TTIImpl->getExtractWithExtendCost(Opcode, Dst, VecTy, Index);
616 assert(Cost >= 0 && "TTI should not produce negative costs!");
617 return Cost;
618}
619
Chandler Carruth93205eb2015-08-05 18:08:10 +0000620int TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
621 int Cost = TTIImpl->getCFInstrCost(Opcode);
622 assert(Cost >= 0 && "TTI should not produce negative costs!");
623 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000624}
625
Chandler Carruth93205eb2015-08-05 18:08:10 +0000626int TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000627 Type *CondTy, const Instruction *I) const {
628 assert ((I == nullptr || I->getOpcode() == Opcode) &&
629 "Opcode should reflect passed instruction.");
630 int Cost = TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000631 assert(Cost >= 0 && "TTI should not produce negative costs!");
632 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000633}
634
Chandler Carruth93205eb2015-08-05 18:08:10 +0000635int TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
636 unsigned Index) const {
637 int Cost = TTIImpl->getVectorInstrCost(Opcode, Val, Index);
638 assert(Cost >= 0 && "TTI should not produce negative costs!");
639 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000640}
641
Chandler Carruth93205eb2015-08-05 18:08:10 +0000642int TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
Guillaume Chateleta4783ef2019-10-22 17:16:52 +0200643 MaybeAlign Alignment,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000644 unsigned AddressSpace,
645 const Instruction *I) const {
646 assert ((I == nullptr || I->getOpcode() == Opcode) &&
647 "Opcode should reflect passed instruction.");
648 int Cost = TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000649 assert(Cost >= 0 && "TTI should not produce negative costs!");
650 return Cost;
Elena Demikhovskya3232f72015-01-25 08:44:46 +0000651}
652
Chandler Carruth93205eb2015-08-05 18:08:10 +0000653int TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
654 unsigned Alignment,
655 unsigned AddressSpace) const {
656 int Cost =
657 TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
658 assert(Cost >= 0 && "TTI should not produce negative costs!");
659 return Cost;
Chandler Carruth705b1852015-01-31 03:43:40 +0000660}
661
Elena Demikhovsky54946982015-12-28 20:10:59 +0000662int TargetTransformInfo::getGatherScatterOpCost(unsigned Opcode, Type *DataTy,
663 Value *Ptr, bool VariableMask,
664 unsigned Alignment) const {
665 int Cost = TTIImpl->getGatherScatterOpCost(Opcode, DataTy, Ptr, VariableMask,
666 Alignment);
667 assert(Cost >= 0 && "TTI should not produce negative costs!");
668 return Cost;
669}
670
Chandler Carruth93205eb2015-08-05 18:08:10 +0000671int TargetTransformInfo::getInterleavedMemoryOpCost(
Hao Liu32c05392015-06-08 06:39:56 +0000672 unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
Dorit Nuzman34da6dd2018-10-31 09:57:56 +0000673 unsigned Alignment, unsigned AddressSpace, bool UseMaskForCond,
674 bool UseMaskForGaps) const {
675 int Cost = TTIImpl->getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
676 Alignment, AddressSpace,
677 UseMaskForCond,
678 UseMaskForGaps);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000679 assert(Cost >= 0 && "TTI should not produce negative costs!");
680 return Cost;
Hao Liu32c05392015-06-08 06:39:56 +0000681}
682
Chandler Carruth93205eb2015-08-05 18:08:10 +0000683int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
Jonas Paulssona48ea232017-03-14 06:35:36 +0000684 ArrayRef<Type *> Tys, FastMathFlags FMF,
685 unsigned ScalarizationCostPassed) const {
686 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys, FMF,
687 ScalarizationCostPassed);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000688 assert(Cost >= 0 && "TTI should not produce negative costs!");
689 return Cost;
690}
691
Elena Demikhovsky54946982015-12-28 20:10:59 +0000692int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
Jonas Paulssona48ea232017-03-14 06:35:36 +0000693 ArrayRef<Value *> Args, FastMathFlags FMF, unsigned VF) const {
694 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Args, FMF, VF);
Elena Demikhovsky54946982015-12-28 20:10:59 +0000695 assert(Cost >= 0 && "TTI should not produce negative costs!");
696 return Cost;
697}
698
Chandler Carruth93205eb2015-08-05 18:08:10 +0000699int TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy,
700 ArrayRef<Type *> Tys) const {
701 int Cost = TTIImpl->getCallInstrCost(F, RetTy, Tys);
702 assert(Cost >= 0 && "TTI should not produce negative costs!");
703 return Cost;
Michael Zolotukhin7ed84a82015-03-17 19:26:23 +0000704}
705
Chandler Carruth539edf42013-01-05 11:43:11 +0000706unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000707 return TTIImpl->getNumberOfParts(Tp);
Chandler Carruth539edf42013-01-05 11:43:11 +0000708}
709
Chandler Carruth93205eb2015-08-05 18:08:10 +0000710int TargetTransformInfo::getAddressComputationCost(Type *Tp,
Mohammed Agabaria23599ba2017-01-05 14:03:41 +0000711 ScalarEvolution *SE,
712 const SCEV *Ptr) const {
713 int Cost = TTIImpl->getAddressComputationCost(Tp, SE, Ptr);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000714 assert(Cost >= 0 && "TTI should not produce negative costs!");
715 return Cost;
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000716}
Chandler Carruth539edf42013-01-05 11:43:11 +0000717
Sjoerd Meijerea31ddb2019-04-30 10:28:50 +0000718int TargetTransformInfo::getMemcpyCost(const Instruction *I) const {
719 int Cost = TTIImpl->getMemcpyCost(I);
720 assert(Cost >= 0 && "TTI should not produce negative costs!");
721 return Cost;
722}
723
Alexey Bataev3e9b3eb2017-07-31 14:19:32 +0000724int TargetTransformInfo::getArithmeticReductionCost(unsigned Opcode, Type *Ty,
725 bool IsPairwiseForm) const {
726 int Cost = TTIImpl->getArithmeticReductionCost(Opcode, Ty, IsPairwiseForm);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000727 assert(Cost >= 0 && "TTI should not produce negative costs!");
728 return Cost;
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000729}
730
Alexey Bataev6dd29fc2017-09-08 13:49:36 +0000731int TargetTransformInfo::getMinMaxReductionCost(Type *Ty, Type *CondTy,
732 bool IsPairwiseForm,
733 bool IsUnsigned) const {
734 int Cost =
735 TTIImpl->getMinMaxReductionCost(Ty, CondTy, IsPairwiseForm, IsUnsigned);
736 assert(Cost >= 0 && "TTI should not produce negative costs!");
737 return Cost;
738}
739
Chandler Carruth705b1852015-01-31 03:43:40 +0000740unsigned
741TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
742 return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
Chad Rosierf9327d62015-01-26 22:51:15 +0000743}
744
745bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst,
746 MemIntrinsicInfo &Info) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000747 return TTIImpl->getTgtMemIntrinsic(Inst, Info);
Chad Rosierf9327d62015-01-26 22:51:15 +0000748}
749
Anna Thomasb2a212c2017-06-06 16:45:25 +0000750unsigned TargetTransformInfo::getAtomicMemIntrinsicMaxElementSize() const {
751 return TTIImpl->getAtomicMemIntrinsicMaxElementSize();
752}
753
Chandler Carruth705b1852015-01-31 03:43:40 +0000754Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic(
755 IntrinsicInst *Inst, Type *ExpectedType) const {
756 return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
757}
758
Sean Fertile9cd1cdf2017-07-07 02:00:06 +0000759Type *TargetTransformInfo::getMemcpyLoopLoweringType(LLVMContext &Context,
760 Value *Length,
761 unsigned SrcAlign,
762 unsigned DestAlign) const {
763 return TTIImpl->getMemcpyLoopLoweringType(Context, Length, SrcAlign,
764 DestAlign);
765}
766
767void TargetTransformInfo::getMemcpyLoopResidualLoweringType(
768 SmallVectorImpl<Type *> &OpsOut, LLVMContext &Context,
769 unsigned RemainingBytes, unsigned SrcAlign, unsigned DestAlign) const {
770 TTIImpl->getMemcpyLoopResidualLoweringType(OpsOut, Context, RemainingBytes,
771 SrcAlign, DestAlign);
772}
773
Eric Christopherd566fb12015-07-29 22:09:48 +0000774bool TargetTransformInfo::areInlineCompatible(const Function *Caller,
775 const Function *Callee) const {
776 return TTIImpl->areInlineCompatible(Caller, Callee);
Eric Christopher4371b132015-07-02 01:11:47 +0000777}
778
Tom Stellard3d36e5c2019-01-16 05:15:31 +0000779bool TargetTransformInfo::areFunctionArgsABICompatible(
780 const Function *Caller, const Function *Callee,
781 SmallPtrSetImpl<Argument *> &Args) const {
782 return TTIImpl->areFunctionArgsABICompatible(Caller, Callee, Args);
783}
784
Krzysztof Parzyszek0b377e02018-03-26 13:10:09 +0000785bool TargetTransformInfo::isIndexedLoadLegal(MemIndexedMode Mode,
786 Type *Ty) const {
787 return TTIImpl->isIndexedLoadLegal(Mode, Ty);
788}
789
790bool TargetTransformInfo::isIndexedStoreLegal(MemIndexedMode Mode,
791 Type *Ty) const {
792 return TTIImpl->isIndexedStoreLegal(Mode, Ty);
793}
794
Volkan Keles1c386812016-10-03 10:31:34 +0000795unsigned TargetTransformInfo::getLoadStoreVecRegBitWidth(unsigned AS) const {
796 return TTIImpl->getLoadStoreVecRegBitWidth(AS);
797}
798
799bool TargetTransformInfo::isLegalToVectorizeLoad(LoadInst *LI) const {
800 return TTIImpl->isLegalToVectorizeLoad(LI);
801}
802
803bool TargetTransformInfo::isLegalToVectorizeStore(StoreInst *SI) const {
804 return TTIImpl->isLegalToVectorizeStore(SI);
805}
806
807bool TargetTransformInfo::isLegalToVectorizeLoadChain(
808 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
809 return TTIImpl->isLegalToVectorizeLoadChain(ChainSizeInBytes, Alignment,
810 AddrSpace);
811}
812
813bool TargetTransformInfo::isLegalToVectorizeStoreChain(
814 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
815 return TTIImpl->isLegalToVectorizeStoreChain(ChainSizeInBytes, Alignment,
816 AddrSpace);
817}
818
819unsigned TargetTransformInfo::getLoadVectorFactor(unsigned VF,
820 unsigned LoadSize,
821 unsigned ChainSizeInBytes,
822 VectorType *VecTy) const {
823 return TTIImpl->getLoadVectorFactor(VF, LoadSize, ChainSizeInBytes, VecTy);
824}
825
826unsigned TargetTransformInfo::getStoreVectorFactor(unsigned VF,
827 unsigned StoreSize,
828 unsigned ChainSizeInBytes,
829 VectorType *VecTy) const {
830 return TTIImpl->getStoreVectorFactor(VF, StoreSize, ChainSizeInBytes, VecTy);
831}
832
Amara Emersoncf9daa32017-05-09 10:43:25 +0000833bool TargetTransformInfo::useReductionIntrinsic(unsigned Opcode,
834 Type *Ty, ReductionFlags Flags) const {
835 return TTIImpl->useReductionIntrinsic(Opcode, Ty, Flags);
836}
837
Amara Emerson836b0f42017-05-10 09:42:49 +0000838bool TargetTransformInfo::shouldExpandReduction(const IntrinsicInst *II) const {
839 return TTIImpl->shouldExpandReduction(II);
840}
Amara Emersoncf9daa32017-05-09 10:43:25 +0000841
Amara Emerson14688222019-06-17 23:20:29 +0000842unsigned TargetTransformInfo::getGISelRematGlobalCost() const {
843 return TTIImpl->getGISelRematGlobalCost();
844}
845
Guozhi Wei62d64142017-09-08 22:29:17 +0000846int TargetTransformInfo::getInstructionLatency(const Instruction *I) const {
847 return TTIImpl->getInstructionLatency(I);
848}
849
Guozhi Wei62d64142017-09-08 22:29:17 +0000850static bool matchPairwiseShuffleMask(ShuffleVectorInst *SI, bool IsLeft,
851 unsigned Level) {
852 // We don't need a shuffle if we just want to have element 0 in position 0 of
853 // the vector.
854 if (!SI && Level == 0 && IsLeft)
855 return true;
856 else if (!SI)
857 return false;
858
859 SmallVector<int, 32> Mask(SI->getType()->getVectorNumElements(), -1);
860
861 // Build a mask of 0, 2, ... (left) or 1, 3, ... (right) depending on whether
862 // we look at the left or right side.
863 for (unsigned i = 0, e = (1 << Level), val = !IsLeft; i != e; ++i, val += 2)
864 Mask[i] = val;
865
866 SmallVector<int, 16> ActualMask = SI->getShuffleMask();
867 return Mask == ActualMask;
868}
869
870namespace {
871/// Kind of the reduction data.
872enum ReductionKind {
873 RK_None, /// Not a reduction.
874 RK_Arithmetic, /// Binary reduction data.
875 RK_MinMax, /// Min/max reduction data.
876 RK_UnsignedMinMax, /// Unsigned min/max reduction data.
877};
878/// Contains opcode + LHS/RHS parts of the reduction operations.
879struct ReductionData {
880 ReductionData() = delete;
881 ReductionData(ReductionKind Kind, unsigned Opcode, Value *LHS, Value *RHS)
882 : Opcode(Opcode), LHS(LHS), RHS(RHS), Kind(Kind) {
883 assert(Kind != RK_None && "expected binary or min/max reduction only.");
884 }
885 unsigned Opcode = 0;
886 Value *LHS = nullptr;
887 Value *RHS = nullptr;
888 ReductionKind Kind = RK_None;
889 bool hasSameData(ReductionData &RD) const {
890 return Kind == RD.Kind && Opcode == RD.Opcode;
891 }
892};
893} // namespace
894
895static Optional<ReductionData> getReductionData(Instruction *I) {
896 Value *L, *R;
897 if (m_BinOp(m_Value(L), m_Value(R)).match(I))
Fangrui Songf78650a2018-07-30 19:41:25 +0000898 return ReductionData(RK_Arithmetic, I->getOpcode(), L, R);
Guozhi Wei62d64142017-09-08 22:29:17 +0000899 if (auto *SI = dyn_cast<SelectInst>(I)) {
900 if (m_SMin(m_Value(L), m_Value(R)).match(SI) ||
901 m_SMax(m_Value(L), m_Value(R)).match(SI) ||
902 m_OrdFMin(m_Value(L), m_Value(R)).match(SI) ||
903 m_OrdFMax(m_Value(L), m_Value(R)).match(SI) ||
904 m_UnordFMin(m_Value(L), m_Value(R)).match(SI) ||
905 m_UnordFMax(m_Value(L), m_Value(R)).match(SI)) {
906 auto *CI = cast<CmpInst>(SI->getCondition());
Fangrui Songf78650a2018-07-30 19:41:25 +0000907 return ReductionData(RK_MinMax, CI->getOpcode(), L, R);
908 }
Guozhi Wei62d64142017-09-08 22:29:17 +0000909 if (m_UMin(m_Value(L), m_Value(R)).match(SI) ||
910 m_UMax(m_Value(L), m_Value(R)).match(SI)) {
911 auto *CI = cast<CmpInst>(SI->getCondition());
912 return ReductionData(RK_UnsignedMinMax, CI->getOpcode(), L, R);
913 }
914 }
915 return llvm::None;
916}
917
918static ReductionKind matchPairwiseReductionAtLevel(Instruction *I,
919 unsigned Level,
920 unsigned NumLevels) {
921 // Match one level of pairwise operations.
922 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef,
923 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef>
924 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef,
925 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef>
926 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1
927 if (!I)
928 return RK_None;
929
930 assert(I->getType()->isVectorTy() && "Expecting a vector type");
931
932 Optional<ReductionData> RD = getReductionData(I);
933 if (!RD)
934 return RK_None;
935
936 ShuffleVectorInst *LS = dyn_cast<ShuffleVectorInst>(RD->LHS);
937 if (!LS && Level)
938 return RK_None;
939 ShuffleVectorInst *RS = dyn_cast<ShuffleVectorInst>(RD->RHS);
940 if (!RS && Level)
941 return RK_None;
942
943 // On level 0 we can omit one shufflevector instruction.
944 if (!Level && !RS && !LS)
945 return RK_None;
946
947 // Shuffle inputs must match.
948 Value *NextLevelOpL = LS ? LS->getOperand(0) : nullptr;
949 Value *NextLevelOpR = RS ? RS->getOperand(0) : nullptr;
950 Value *NextLevelOp = nullptr;
951 if (NextLevelOpR && NextLevelOpL) {
952 // If we have two shuffles their operands must match.
953 if (NextLevelOpL != NextLevelOpR)
954 return RK_None;
955
956 NextLevelOp = NextLevelOpL;
957 } else if (Level == 0 && (NextLevelOpR || NextLevelOpL)) {
958 // On the first level we can omit the shufflevector <0, undef,...>. So the
959 // input to the other shufflevector <1, undef> must match with one of the
960 // inputs to the current binary operation.
961 // Example:
962 // %NextLevelOpL = shufflevector %R, <1, undef ...>
963 // %BinOp = fadd %NextLevelOpL, %R
964 if (NextLevelOpL && NextLevelOpL != RD->RHS)
965 return RK_None;
966 else if (NextLevelOpR && NextLevelOpR != RD->LHS)
967 return RK_None;
968
969 NextLevelOp = NextLevelOpL ? RD->RHS : RD->LHS;
970 } else
971 return RK_None;
972
973 // Check that the next levels binary operation exists and matches with the
974 // current one.
975 if (Level + 1 != NumLevels) {
976 Optional<ReductionData> NextLevelRD =
977 getReductionData(cast<Instruction>(NextLevelOp));
978 if (!NextLevelRD || !RD->hasSameData(*NextLevelRD))
979 return RK_None;
980 }
981
982 // Shuffle mask for pairwise operation must match.
983 if (matchPairwiseShuffleMask(LS, /*IsLeft=*/true, Level)) {
984 if (!matchPairwiseShuffleMask(RS, /*IsLeft=*/false, Level))
985 return RK_None;
986 } else if (matchPairwiseShuffleMask(RS, /*IsLeft=*/true, Level)) {
987 if (!matchPairwiseShuffleMask(LS, /*IsLeft=*/false, Level))
988 return RK_None;
989 } else {
990 return RK_None;
991 }
992
993 if (++Level == NumLevels)
994 return RD->Kind;
995
996 // Match next level.
997 return matchPairwiseReductionAtLevel(cast<Instruction>(NextLevelOp), Level,
998 NumLevels);
999}
1000
1001static ReductionKind matchPairwiseReduction(const ExtractElementInst *ReduxRoot,
1002 unsigned &Opcode, Type *&Ty) {
1003 if (!EnableReduxCost)
1004 return RK_None;
1005
1006 // Need to extract the first element.
1007 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1));
1008 unsigned Idx = ~0u;
1009 if (CI)
1010 Idx = CI->getZExtValue();
1011 if (Idx != 0)
1012 return RK_None;
1013
1014 auto *RdxStart = dyn_cast<Instruction>(ReduxRoot->getOperand(0));
1015 if (!RdxStart)
1016 return RK_None;
1017 Optional<ReductionData> RD = getReductionData(RdxStart);
1018 if (!RD)
1019 return RK_None;
1020
1021 Type *VecTy = RdxStart->getType();
1022 unsigned NumVecElems = VecTy->getVectorNumElements();
1023 if (!isPowerOf2_32(NumVecElems))
1024 return RK_None;
1025
1026 // We look for a sequence of shuffle,shuffle,add triples like the following
1027 // that builds a pairwise reduction tree.
Fangrui Songf78650a2018-07-30 19:41:25 +00001028 //
Guozhi Wei62d64142017-09-08 22:29:17 +00001029 // (X0, X1, X2, X3)
1030 // (X0 + X1, X2 + X3, undef, undef)
1031 // ((X0 + X1) + (X2 + X3), undef, undef, undef)
Fangrui Songf78650a2018-07-30 19:41:25 +00001032 //
Guozhi Wei62d64142017-09-08 22:29:17 +00001033 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef,
1034 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef>
1035 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef,
1036 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef>
1037 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1
1038 // %rdx.shuf.1.0 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef,
1039 // <4 x i32> <i32 0, i32 undef, i32 undef, i32 undef>
1040 // %rdx.shuf.1.1 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef,
1041 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
1042 // %bin.rdx8 = fadd <4 x float> %rdx.shuf.1.0, %rdx.shuf.1.1
1043 // %r = extractelement <4 x float> %bin.rdx8, i32 0
1044 if (matchPairwiseReductionAtLevel(RdxStart, 0, Log2_32(NumVecElems)) ==
1045 RK_None)
1046 return RK_None;
1047
1048 Opcode = RD->Opcode;
1049 Ty = VecTy;
1050
1051 return RD->Kind;
1052}
1053
1054static std::pair<Value *, ShuffleVectorInst *>
1055getShuffleAndOtherOprd(Value *L, Value *R) {
1056 ShuffleVectorInst *S = nullptr;
1057
1058 if ((S = dyn_cast<ShuffleVectorInst>(L)))
1059 return std::make_pair(R, S);
1060
1061 S = dyn_cast<ShuffleVectorInst>(R);
1062 return std::make_pair(L, S);
1063}
1064
1065static ReductionKind
1066matchVectorSplittingReduction(const ExtractElementInst *ReduxRoot,
1067 unsigned &Opcode, Type *&Ty) {
1068 if (!EnableReduxCost)
1069 return RK_None;
1070
1071 // Need to extract the first element.
1072 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1));
1073 unsigned Idx = ~0u;
1074 if (CI)
1075 Idx = CI->getZExtValue();
1076 if (Idx != 0)
1077 return RK_None;
1078
1079 auto *RdxStart = dyn_cast<Instruction>(ReduxRoot->getOperand(0));
1080 if (!RdxStart)
1081 return RK_None;
1082 Optional<ReductionData> RD = getReductionData(RdxStart);
1083 if (!RD)
1084 return RK_None;
1085
1086 Type *VecTy = ReduxRoot->getOperand(0)->getType();
1087 unsigned NumVecElems = VecTy->getVectorNumElements();
1088 if (!isPowerOf2_32(NumVecElems))
1089 return RK_None;
1090
1091 // We look for a sequence of shuffles and adds like the following matching one
1092 // fadd, shuffle vector pair at a time.
Fangrui Songf78650a2018-07-30 19:41:25 +00001093 //
Guozhi Wei62d64142017-09-08 22:29:17 +00001094 // %rdx.shuf = shufflevector <4 x float> %rdx, <4 x float> undef,
1095 // <4 x i32> <i32 2, i32 3, i32 undef, i32 undef>
1096 // %bin.rdx = fadd <4 x float> %rdx, %rdx.shuf
1097 // %rdx.shuf7 = shufflevector <4 x float> %bin.rdx, <4 x float> undef,
1098 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
1099 // %bin.rdx8 = fadd <4 x float> %bin.rdx, %rdx.shuf7
1100 // %r = extractelement <4 x float> %bin.rdx8, i32 0
1101
1102 unsigned MaskStart = 1;
1103 Instruction *RdxOp = RdxStart;
Fangrui Songf78650a2018-07-30 19:41:25 +00001104 SmallVector<int, 32> ShuffleMask(NumVecElems, 0);
Guozhi Wei62d64142017-09-08 22:29:17 +00001105 unsigned NumVecElemsRemain = NumVecElems;
1106 while (NumVecElemsRemain - 1) {
1107 // Check for the right reduction operation.
1108 if (!RdxOp)
1109 return RK_None;
1110 Optional<ReductionData> RDLevel = getReductionData(RdxOp);
1111 if (!RDLevel || !RDLevel->hasSameData(*RD))
1112 return RK_None;
1113
1114 Value *NextRdxOp;
1115 ShuffleVectorInst *Shuffle;
1116 std::tie(NextRdxOp, Shuffle) =
1117 getShuffleAndOtherOprd(RDLevel->LHS, RDLevel->RHS);
1118
1119 // Check the current reduction operation and the shuffle use the same value.
1120 if (Shuffle == nullptr)
1121 return RK_None;
1122 if (Shuffle->getOperand(0) != NextRdxOp)
1123 return RK_None;
1124
1125 // Check that shuffle masks matches.
1126 for (unsigned j = 0; j != MaskStart; ++j)
1127 ShuffleMask[j] = MaskStart + j;
1128 // Fill the rest of the mask with -1 for undef.
1129 std::fill(&ShuffleMask[MaskStart], ShuffleMask.end(), -1);
1130
1131 SmallVector<int, 16> Mask = Shuffle->getShuffleMask();
1132 if (ShuffleMask != Mask)
1133 return RK_None;
1134
1135 RdxOp = dyn_cast<Instruction>(NextRdxOp);
1136 NumVecElemsRemain /= 2;
1137 MaskStart *= 2;
1138 }
1139
1140 Opcode = RD->Opcode;
1141 Ty = VecTy;
1142 return RD->Kind;
1143}
1144
1145int TargetTransformInfo::getInstructionThroughput(const Instruction *I) const {
1146 switch (I->getOpcode()) {
1147 case Instruction::GetElementPtr:
1148 return getUserCost(I);
1149
1150 case Instruction::Ret:
1151 case Instruction::PHI:
1152 case Instruction::Br: {
1153 return getCFInstrCost(I->getOpcode());
1154 }
1155 case Instruction::Add:
1156 case Instruction::FAdd:
1157 case Instruction::Sub:
1158 case Instruction::FSub:
1159 case Instruction::Mul:
1160 case Instruction::FMul:
1161 case Instruction::UDiv:
1162 case Instruction::SDiv:
1163 case Instruction::FDiv:
1164 case Instruction::URem:
1165 case Instruction::SRem:
1166 case Instruction::FRem:
1167 case Instruction::Shl:
1168 case Instruction::LShr:
1169 case Instruction::AShr:
1170 case Instruction::And:
1171 case Instruction::Or:
1172 case Instruction::Xor: {
Simon Pilgrim4162d772018-05-22 10:40:09 +00001173 TargetTransformInfo::OperandValueKind Op1VK, Op2VK;
1174 TargetTransformInfo::OperandValueProperties Op1VP, Op2VP;
1175 Op1VK = getOperandInfo(I->getOperand(0), Op1VP);
1176 Op2VK = getOperandInfo(I->getOperand(1), Op2VP);
1177 SmallVector<const Value *, 2> Operands(I->operand_values());
1178 return getArithmeticInstrCost(I->getOpcode(), I->getType(), Op1VK, Op2VK,
1179 Op1VP, Op2VP, Operands);
Guozhi Wei62d64142017-09-08 22:29:17 +00001180 }
Craig Topper50d50282019-05-28 04:09:18 +00001181 case Instruction::FNeg: {
1182 TargetTransformInfo::OperandValueKind Op1VK, Op2VK;
1183 TargetTransformInfo::OperandValueProperties Op1VP, Op2VP;
1184 Op1VK = getOperandInfo(I->getOperand(0), Op1VP);
1185 Op2VK = OK_AnyValue;
1186 Op2VP = OP_None;
1187 SmallVector<const Value *, 2> Operands(I->operand_values());
1188 return getArithmeticInstrCost(I->getOpcode(), I->getType(), Op1VK, Op2VK,
1189 Op1VP, Op2VP, Operands);
1190 }
Guozhi Wei62d64142017-09-08 22:29:17 +00001191 case Instruction::Select: {
1192 const SelectInst *SI = cast<SelectInst>(I);
1193 Type *CondTy = SI->getCondition()->getType();
1194 return getCmpSelInstrCost(I->getOpcode(), I->getType(), CondTy, I);
1195 }
1196 case Instruction::ICmp:
1197 case Instruction::FCmp: {
1198 Type *ValTy = I->getOperand(0)->getType();
1199 return getCmpSelInstrCost(I->getOpcode(), ValTy, I->getType(), I);
1200 }
1201 case Instruction::Store: {
1202 const StoreInst *SI = cast<StoreInst>(I);
1203 Type *ValTy = SI->getValueOperand()->getType();
1204 return getMemoryOpCost(I->getOpcode(), ValTy,
Guillaume Chateleta4783ef2019-10-22 17:16:52 +02001205 MaybeAlign(SI->getAlignment()),
1206 SI->getPointerAddressSpace(), I);
Guozhi Wei62d64142017-09-08 22:29:17 +00001207 }
1208 case Instruction::Load: {
1209 const LoadInst *LI = cast<LoadInst>(I);
1210 return getMemoryOpCost(I->getOpcode(), I->getType(),
Guillaume Chateleta4783ef2019-10-22 17:16:52 +02001211 MaybeAlign(LI->getAlignment()),
1212 LI->getPointerAddressSpace(), I);
Guozhi Wei62d64142017-09-08 22:29:17 +00001213 }
1214 case Instruction::ZExt:
1215 case Instruction::SExt:
1216 case Instruction::FPToUI:
1217 case Instruction::FPToSI:
1218 case Instruction::FPExt:
1219 case Instruction::PtrToInt:
1220 case Instruction::IntToPtr:
1221 case Instruction::SIToFP:
1222 case Instruction::UIToFP:
1223 case Instruction::Trunc:
1224 case Instruction::FPTrunc:
1225 case Instruction::BitCast:
1226 case Instruction::AddrSpaceCast: {
1227 Type *SrcTy = I->getOperand(0)->getType();
1228 return getCastInstrCost(I->getOpcode(), I->getType(), SrcTy, I);
1229 }
1230 case Instruction::ExtractElement: {
1231 const ExtractElementInst * EEI = cast<ExtractElementInst>(I);
1232 ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1));
1233 unsigned Idx = -1;
1234 if (CI)
1235 Idx = CI->getZExtValue();
1236
1237 // Try to match a reduction sequence (series of shufflevector and vector
1238 // adds followed by a extractelement).
1239 unsigned ReduxOpCode;
1240 Type *ReduxType;
1241
1242 switch (matchVectorSplittingReduction(EEI, ReduxOpCode, ReduxType)) {
1243 case RK_Arithmetic:
1244 return getArithmeticReductionCost(ReduxOpCode, ReduxType,
1245 /*IsPairwiseForm=*/false);
1246 case RK_MinMax:
1247 return getMinMaxReductionCost(
1248 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1249 /*IsPairwiseForm=*/false, /*IsUnsigned=*/false);
1250 case RK_UnsignedMinMax:
1251 return getMinMaxReductionCost(
1252 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1253 /*IsPairwiseForm=*/false, /*IsUnsigned=*/true);
1254 case RK_None:
1255 break;
1256 }
1257
1258 switch (matchPairwiseReduction(EEI, ReduxOpCode, ReduxType)) {
1259 case RK_Arithmetic:
1260 return getArithmeticReductionCost(ReduxOpCode, ReduxType,
1261 /*IsPairwiseForm=*/true);
1262 case RK_MinMax:
1263 return getMinMaxReductionCost(
1264 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1265 /*IsPairwiseForm=*/true, /*IsUnsigned=*/false);
1266 case RK_UnsignedMinMax:
1267 return getMinMaxReductionCost(
1268 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1269 /*IsPairwiseForm=*/true, /*IsUnsigned=*/true);
1270 case RK_None:
1271 break;
1272 }
1273
1274 return getVectorInstrCost(I->getOpcode(),
1275 EEI->getOperand(0)->getType(), Idx);
1276 }
1277 case Instruction::InsertElement: {
1278 const InsertElementInst * IE = cast<InsertElementInst>(I);
1279 ConstantInt *CI = dyn_cast<ConstantInt>(IE->getOperand(2));
Fangrui Songf78650a2018-07-30 19:41:25 +00001280 unsigned Idx = -1;
Guozhi Wei62d64142017-09-08 22:29:17 +00001281 if (CI)
1282 Idx = CI->getZExtValue();
1283 return getVectorInstrCost(I->getOpcode(),
1284 IE->getType(), Idx);
1285 }
Roman Lebedevcc95a452019-08-29 11:50:30 +00001286 case Instruction::ExtractValue:
1287 return 0; // Model all ExtractValue nodes as free.
Guozhi Wei62d64142017-09-08 22:29:17 +00001288 case Instruction::ShuffleVector: {
1289 const ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
Simon Pilgrimd0c71602018-11-09 16:28:19 +00001290 Type *Ty = Shuffle->getType();
1291 Type *SrcTy = Shuffle->getOperand(0)->getType();
1292
1293 // TODO: Identify and add costs for insert subvector, etc.
1294 int SubIndex;
1295 if (Shuffle->isExtractSubvectorMask(SubIndex))
Simon Pilgrim26e1c882018-11-09 18:30:59 +00001296 return TTIImpl->getShuffleCost(SK_ExtractSubvector, SrcTy, SubIndex, Ty);
Simon Pilgrimd0c71602018-11-09 16:28:19 +00001297
Sanjay Patel2ca33602018-06-19 18:44:00 +00001298 if (Shuffle->changesLength())
1299 return -1;
Fangrui Songf78650a2018-07-30 19:41:25 +00001300
Sanjay Patel2ca33602018-06-19 18:44:00 +00001301 if (Shuffle->isIdentity())
1302 return 0;
Guozhi Wei62d64142017-09-08 22:29:17 +00001303
Sanjay Patel2ca33602018-06-19 18:44:00 +00001304 if (Shuffle->isReverse())
1305 return TTIImpl->getShuffleCost(SK_Reverse, Ty, 0, nullptr);
Simon Pilgrim07839212018-06-12 14:47:13 +00001306
Sanjay Patel2ca33602018-06-19 18:44:00 +00001307 if (Shuffle->isSelect())
1308 return TTIImpl->getShuffleCost(SK_Select, Ty, 0, nullptr);
Simon Pilgrim07839212018-06-12 14:47:13 +00001309
Sanjay Patel2ca33602018-06-19 18:44:00 +00001310 if (Shuffle->isTranspose())
1311 return TTIImpl->getShuffleCost(SK_Transpose, Ty, 0, nullptr);
Matthew Simpsonb4096eb2018-04-26 13:48:33 +00001312
Sanjay Patel2ca33602018-06-19 18:44:00 +00001313 if (Shuffle->isZeroEltSplat())
1314 return TTIImpl->getShuffleCost(SK_Broadcast, Ty, 0, nullptr);
Guozhi Wei62d64142017-09-08 22:29:17 +00001315
Sanjay Patel2ca33602018-06-19 18:44:00 +00001316 if (Shuffle->isSingleSource())
1317 return TTIImpl->getShuffleCost(SK_PermuteSingleSrc, Ty, 0, nullptr);
Guozhi Wei62d64142017-09-08 22:29:17 +00001318
Sanjay Patel2ca33602018-06-19 18:44:00 +00001319 return TTIImpl->getShuffleCost(SK_PermuteTwoSrc, Ty, 0, nullptr);
Guozhi Wei62d64142017-09-08 22:29:17 +00001320 }
1321 case Instruction::Call:
1322 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1323 SmallVector<Value *, 4> Args(II->arg_operands());
1324
1325 FastMathFlags FMF;
1326 if (auto *FPMO = dyn_cast<FPMathOperator>(II))
1327 FMF = FPMO->getFastMathFlags();
1328
1329 return getIntrinsicInstrCost(II->getIntrinsicID(), II->getType(),
1330 Args, FMF);
1331 }
1332 return -1;
1333 default:
1334 // We don't have any information on this instruction.
1335 return -1;
1336 }
1337}
1338
Chandler Carruth705b1852015-01-31 03:43:40 +00001339TargetTransformInfo::Concept::~Concept() {}
1340
Chandler Carruthe0385522015-02-01 10:11:22 +00001341TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
1342
1343TargetIRAnalysis::TargetIRAnalysis(
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001344 std::function<Result(const Function &)> TTICallback)
Benjamin Kramer82de7d32016-05-27 14:27:24 +00001345 : TTICallback(std::move(TTICallback)) {}
Chandler Carruthe0385522015-02-01 10:11:22 +00001346
Chandler Carruth164a2aa62016-06-17 00:11:01 +00001347TargetIRAnalysis::Result TargetIRAnalysis::run(const Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +00001348 FunctionAnalysisManager &) {
Chandler Carruthe0385522015-02-01 10:11:22 +00001349 return TTICallback(F);
1350}
1351
Chandler Carruthdab4eae2016-11-23 17:53:26 +00001352AnalysisKey TargetIRAnalysis::Key;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +00001353
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001354TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(const Function &F) {
Mehdi Amini5010ebf2015-07-09 02:08:42 +00001355 return Result(F.getParent()->getDataLayout());
Chandler Carruthe0385522015-02-01 10:11:22 +00001356}
1357
Chandler Carruth705b1852015-01-31 03:43:40 +00001358// Register the basic pass.
1359INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
1360 "Target Transform Information", false, true)
1361char TargetTransformInfoWrapperPass::ID = 0;
Chandler Carruth539edf42013-01-05 11:43:11 +00001362
Chandler Carruth705b1852015-01-31 03:43:40 +00001363void TargetTransformInfoWrapperPass::anchor() {}
Chandler Carruth539edf42013-01-05 11:43:11 +00001364
Chandler Carruth705b1852015-01-31 03:43:40 +00001365TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass()
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001366 : ImmutablePass(ID) {
Chandler Carruth705b1852015-01-31 03:43:40 +00001367 initializeTargetTransformInfoWrapperPassPass(
1368 *PassRegistry::getPassRegistry());
1369}
1370
1371TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass(
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001372 TargetIRAnalysis TIRA)
1373 : ImmutablePass(ID), TIRA(std::move(TIRA)) {
Chandler Carruth705b1852015-01-31 03:43:40 +00001374 initializeTargetTransformInfoWrapperPassPass(
1375 *PassRegistry::getPassRegistry());
1376}
1377
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001378TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(const Function &F) {
Sean Silva36e0d012016-08-09 00:28:15 +00001379 FunctionAnalysisManager DummyFAM;
Chandler Carruth164a2aa62016-06-17 00:11:01 +00001380 TTI = TIRA.run(F, DummyFAM);
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001381 return *TTI;
1382}
1383
Chandler Carruth93dcdc42015-01-31 11:17:59 +00001384ImmutablePass *
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001385llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) {
1386 return new TargetTransformInfoWrapperPass(std::move(TIRA));
Chandler Carruth539edf42013-01-05 11:43:11 +00001387}