blob: e505b448bb71e2d6219a3be300c5d1b8ef1d85c2 [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
197TargetTransformInfo::getEstimatedNumberOfCaseClusters(const SwitchInst &SI,
198 unsigned &JTSize) const {
199 return TTIImpl->getEstimatedNumberOfCaseClusters(SI, JTSize);
200}
201
Evgeny Astigeevich70ed78e2017-06-29 13:42:12 +0000202int TargetTransformInfo::getUserCost(const User *U,
203 ArrayRef<const Value *> Operands) const {
204 int Cost = TTIImpl->getUserCost(U, Operands);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000205 assert(Cost >= 0 && "TTI should not produce negative costs!");
206 return Cost;
Chandler Carruth511aa762013-01-21 01:27:39 +0000207}
208
Tom Stellard8b1e0212013-07-27 00:01:07 +0000209bool TargetTransformInfo::hasBranchDivergence() const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000210 return TTIImpl->hasBranchDivergence();
Tom Stellard8b1e0212013-07-27 00:01:07 +0000211}
212
Jingyue Wu5da831c2015-04-10 05:03:50 +0000213bool TargetTransformInfo::isSourceOfDivergence(const Value *V) const {
214 return TTIImpl->isSourceOfDivergence(V);
215}
216
Alexander Timofeev0f9c84c2017-06-15 19:33:10 +0000217bool llvm::TargetTransformInfo::isAlwaysUniform(const Value *V) const {
218 return TTIImpl->isAlwaysUniform(V);
219}
220
Matt Arsenault42b64782017-01-30 23:02:12 +0000221unsigned TargetTransformInfo::getFlatAddressSpace() const {
222 return TTIImpl->getFlatAddressSpace();
223}
224
Matt Arsenaultdbc1f202019-08-14 18:13:00 +0000225bool TargetTransformInfo::collectFlatAddressOperands(
226 SmallVectorImpl<int> &OpIndexes, Intrinsic::ID IID) const {
227 return TTIImpl->collectFlatAddressOperands(OpIndexes, IID);
228}
229
230bool TargetTransformInfo::rewriteIntrinsicWithAddressSpace(
231 IntrinsicInst *II, Value *OldV, Value *NewV) const {
232 return TTIImpl->rewriteIntrinsicWithAddressSpace(II, OldV, NewV);
233}
234
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000235bool TargetTransformInfo::isLoweredToCall(const Function *F) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000236 return TTIImpl->isLoweredToCall(F);
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000237}
238
Sam Parkerc5ef5022019-06-07 07:35:30 +0000239bool TargetTransformInfo::isHardwareLoopProfitable(
Chen Zhengaa999522019-06-26 12:02:43 +0000240 Loop *L, ScalarEvolution &SE, AssumptionCache &AC,
Sam Parkerc5ef5022019-06-07 07:35:30 +0000241 TargetLibraryInfo *LibInfo, HardwareLoopInfo &HWLoopInfo) const {
242 return TTIImpl->isHardwareLoopProfitable(L, SE, AC, LibInfo, HWLoopInfo);
243}
244
Chandler Carruth705b1852015-01-31 03:43:40 +0000245void TargetTransformInfo::getUnrollingPreferences(
Geoff Berry66d9bdb2017-06-28 15:53:17 +0000246 Loop *L, ScalarEvolution &SE, UnrollingPreferences &UP) const {
247 return TTIImpl->getUnrollingPreferences(L, SE, UP);
Hal Finkel8f2e7002013-09-11 19:25:43 +0000248}
249
Chandler Carruth539edf42013-01-05 11:43:11 +0000250bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000251 return TTIImpl->isLegalAddImmediate(Imm);
Chandler Carruth539edf42013-01-05 11:43:11 +0000252}
253
254bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000255 return TTIImpl->isLegalICmpImmediate(Imm);
Chandler Carruth539edf42013-01-05 11:43:11 +0000256}
257
258bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
259 int64_t BaseOffset,
260 bool HasBaseReg,
Matt Arsenaulte83379e2015-06-07 20:12:03 +0000261 int64_t Scale,
Jonas Paulsson024e3192017-07-21 11:59:37 +0000262 unsigned AddrSpace,
263 Instruction *I) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000264 return TTIImpl->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
Jonas Paulsson024e3192017-07-21 11:59:37 +0000265 Scale, AddrSpace, I);
Chandler Carruth539edf42013-01-05 11:43:11 +0000266}
267
Evgeny Stupachenkof2b3b462017-06-05 23:37:00 +0000268bool TargetTransformInfo::isLSRCostLess(LSRCost &C1, LSRCost &C2) const {
269 return TTIImpl->isLSRCostLess(C1, C2);
270}
271
Sanjay Pateld7c702b2018-02-05 23:43:05 +0000272bool TargetTransformInfo::canMacroFuseCmp() const {
273 return TTIImpl->canMacroFuseCmp();
274}
275
Chen Zhengdfdccbb2019-07-03 01:49:03 +0000276bool TargetTransformInfo::canSaveCmp(Loop *L, BranchInst **BI,
277 ScalarEvolution *SE, LoopInfo *LI,
278 DominatorTree *DT, AssumptionCache *AC,
279 TargetLibraryInfo *LibInfo) const {
280 return TTIImpl->canSaveCmp(L, BI, SE, LI, DT, AC, LibInfo);
281}
282
Krzysztof Parzyszek0b377e02018-03-26 13:10:09 +0000283bool TargetTransformInfo::shouldFavorPostInc() const {
284 return TTIImpl->shouldFavorPostInc();
285}
286
Sam Parker67756c02019-02-07 13:32:54 +0000287bool TargetTransformInfo::shouldFavorBackedgeIndex(const Loop *L) const {
288 return TTIImpl->shouldFavorBackedgeIndex(L);
289}
290
Sam Parker527a35e2019-10-14 10:00:21 +0000291bool TargetTransformInfo::isLegalMaskedStore(Type *DataType,
292 MaybeAlign Alignment) const {
293 return TTIImpl->isLegalMaskedStore(DataType, Alignment);
Chandler Carruth705b1852015-01-31 03:43:40 +0000294}
295
Sam Parker527a35e2019-10-14 10:00:21 +0000296bool TargetTransformInfo::isLegalMaskedLoad(Type *DataType,
297 MaybeAlign Alignment) const {
298 return TTIImpl->isLegalMaskedLoad(DataType, Alignment);
Chandler Carruth705b1852015-01-31 03:43:40 +0000299}
300
Warren Ristow6452bdd2019-06-17 17:20:08 +0000301bool TargetTransformInfo::isLegalNTStore(Type *DataType,
Guillaume Chatelet18f805a2019-09-27 12:54:21 +0000302 Align Alignment) const {
Warren Ristow6452bdd2019-06-17 17:20:08 +0000303 return TTIImpl->isLegalNTStore(DataType, Alignment);
304}
305
Guillaume Chatelet18f805a2019-09-27 12:54:21 +0000306bool TargetTransformInfo::isLegalNTLoad(Type *DataType, Align Alignment) const {
Warren Ristow6452bdd2019-06-17 17:20:08 +0000307 return TTIImpl->isLegalNTLoad(DataType, Alignment);
308}
309
Elena Demikhovsky09285852015-10-25 15:37:55 +0000310bool TargetTransformInfo::isLegalMaskedGather(Type *DataType) const {
311 return TTIImpl->isLegalMaskedGather(DataType);
312}
313
314bool TargetTransformInfo::isLegalMaskedScatter(Type *DataType) const {
Mohammed Agabariacef53dc2017-07-27 10:28:16 +0000315 return TTIImpl->isLegalMaskedScatter(DataType);
Elena Demikhovsky09285852015-10-25 15:37:55 +0000316}
317
Craig Topper9f0b17a2019-03-21 17:38:52 +0000318bool TargetTransformInfo::isLegalMaskedCompressStore(Type *DataType) const {
319 return TTIImpl->isLegalMaskedCompressStore(DataType);
320}
321
322bool TargetTransformInfo::isLegalMaskedExpandLoad(Type *DataType) const {
323 return TTIImpl->isLegalMaskedExpandLoad(DataType);
324}
325
Sanjay Patel6fd43912017-09-09 13:38:18 +0000326bool TargetTransformInfo::hasDivRemOp(Type *DataType, bool IsSigned) const {
327 return TTIImpl->hasDivRemOp(DataType, IsSigned);
328}
329
Artem Belevichcb8f6322017-10-24 20:31:44 +0000330bool TargetTransformInfo::hasVolatileVariant(Instruction *I,
331 unsigned AddrSpace) const {
332 return TTIImpl->hasVolatileVariant(I, AddrSpace);
333}
334
Jonas Paulsson8624b7e2017-05-24 13:42:56 +0000335bool TargetTransformInfo::prefersVectorizedAddressing() const {
336 return TTIImpl->prefersVectorizedAddressing();
337}
338
Quentin Colombetbf490d42013-05-31 21:29:03 +0000339int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
340 int64_t BaseOffset,
341 bool HasBaseReg,
Matt Arsenaulte83379e2015-06-07 20:12:03 +0000342 int64_t Scale,
343 unsigned AddrSpace) const {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000344 int Cost = TTIImpl->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg,
345 Scale, AddrSpace);
346 assert(Cost >= 0 && "TTI should not produce negative costs!");
347 return Cost;
Quentin Colombetbf490d42013-05-31 21:29:03 +0000348}
349
Jonas Paulsson024e3192017-07-21 11:59:37 +0000350bool TargetTransformInfo::LSRWithInstrQueries() const {
351 return TTIImpl->LSRWithInstrQueries();
352}
353
Chandler Carruth539edf42013-01-05 11:43:11 +0000354bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000355 return TTIImpl->isTruncateFree(Ty1, Ty2);
Chandler Carruth539edf42013-01-05 11:43:11 +0000356}
357
Chad Rosier54390052015-02-23 19:15:16 +0000358bool TargetTransformInfo::isProfitableToHoist(Instruction *I) const {
359 return TTIImpl->isProfitableToHoist(I);
360}
361
David Blaikie8ad9a972018-03-28 22:28:50 +0000362bool TargetTransformInfo::useAA() const { return TTIImpl->useAA(); }
363
Chandler Carruth539edf42013-01-05 11:43:11 +0000364bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000365 return TTIImpl->isTypeLegal(Ty);
Chandler Carruth539edf42013-01-05 11:43:11 +0000366}
367
Chandler Carruth539edf42013-01-05 11:43:11 +0000368bool TargetTransformInfo::shouldBuildLookupTables() const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000369 return TTIImpl->shouldBuildLookupTables();
Chandler Carruth539edf42013-01-05 11:43:11 +0000370}
Oliver Stannard4df1cc02016-10-07 08:48:24 +0000371bool TargetTransformInfo::shouldBuildLookupTablesForConstant(Constant *C) const {
372 return TTIImpl->shouldBuildLookupTablesForConstant(C);
373}
Chandler Carruth539edf42013-01-05 11:43:11 +0000374
Zaara Syeda1f59ae32018-01-30 16:17:22 +0000375bool TargetTransformInfo::useColdCCForColdCall(Function &F) const {
376 return TTIImpl->useColdCCForColdCall(F);
377}
378
Jonas Paulsson8e2f9482017-01-26 07:03:25 +0000379unsigned TargetTransformInfo::
380getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const {
381 return TTIImpl->getScalarizationOverhead(Ty, Insert, Extract);
382}
383
384unsigned TargetTransformInfo::
385getOperandsScalarizationOverhead(ArrayRef<const Value *> Args,
386 unsigned VF) const {
387 return TTIImpl->getOperandsScalarizationOverhead(Args, VF);
388}
389
Jonas Paulssonda74ed42017-04-12 12:41:37 +0000390bool TargetTransformInfo::supportsEfficientVectorElementLoadStore() const {
391 return TTIImpl->supportsEfficientVectorElementLoadStore();
392}
393
Olivier Sallenave049d8032015-03-06 23:12:04 +0000394bool TargetTransformInfo::enableAggressiveInterleaving(bool LoopHasReductions) const {
395 return TTIImpl->enableAggressiveInterleaving(LoopHasReductions);
396}
397
Clement Courbet3bc5ad52019-06-25 08:04:13 +0000398TargetTransformInfo::MemCmpExpansionOptions
399TargetTransformInfo::enableMemCmpExpansion(bool OptSize, bool IsZeroCmp) const {
400 return TTIImpl->enableMemCmpExpansion(OptSize, IsZeroCmp);
Zaara Syeda3a7578c2017-05-31 17:12:38 +0000401}
402
Silviu Baranga61bdc512015-08-10 14:50:54 +0000403bool TargetTransformInfo::enableInterleavedAccessVectorization() const {
404 return TTIImpl->enableInterleavedAccessVectorization();
405}
406
Dorit Nuzman38bbf812018-10-14 08:50:06 +0000407bool TargetTransformInfo::enableMaskedInterleavedAccessVectorization() const {
408 return TTIImpl->enableMaskedInterleavedAccessVectorization();
409}
410
Renato Golin5cb666a2016-04-14 20:42:18 +0000411bool TargetTransformInfo::isFPVectorizationPotentiallyUnsafe() const {
412 return TTIImpl->isFPVectorizationPotentiallyUnsafe();
413}
414
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000415bool TargetTransformInfo::allowsMisalignedMemoryAccesses(LLVMContext &Context,
416 unsigned BitWidth,
Alina Sbirlea327955e2016-07-11 20:46:17 +0000417 unsigned AddressSpace,
418 unsigned Alignment,
419 bool *Fast) const {
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000420 return TTIImpl->allowsMisalignedMemoryAccesses(Context, BitWidth, AddressSpace,
Alina Sbirlea327955e2016-07-11 20:46:17 +0000421 Alignment, Fast);
422}
423
Chandler Carruth50a36cd2013-01-07 03:16:03 +0000424TargetTransformInfo::PopcntSupportKind
425TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000426 return TTIImpl->getPopcntSupport(IntTyWidthInBit);
Chandler Carruth539edf42013-01-05 11:43:11 +0000427}
428
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000429bool TargetTransformInfo::haveFastSqrt(Type *Ty) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000430 return TTIImpl->haveFastSqrt(Ty);
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000431}
432
Sanjay Patel0de1a4b2017-11-27 21:15:43 +0000433bool TargetTransformInfo::isFCmpOrdCheaperThanFCmpZero(Type *Ty) const {
434 return TTIImpl->isFCmpOrdCheaperThanFCmpZero(Ty);
435}
436
Chandler Carruth93205eb2015-08-05 18:08:10 +0000437int TargetTransformInfo::getFPOpCost(Type *Ty) const {
438 int Cost = TTIImpl->getFPOpCost(Ty);
439 assert(Cost >= 0 && "TTI should not produce negative costs!");
440 return Cost;
Cameron Esfahani17177d12015-02-05 02:09:33 +0000441}
442
Sjoerd Meijer38c2cd02016-07-14 07:44:20 +0000443int TargetTransformInfo::getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx,
444 const APInt &Imm,
445 Type *Ty) const {
446 int Cost = TTIImpl->getIntImmCodeSizeCost(Opcode, Idx, Imm, Ty);
447 assert(Cost >= 0 && "TTI should not produce negative costs!");
448 return Cost;
449}
450
Chandler Carruth93205eb2015-08-05 18:08:10 +0000451int TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
452 int Cost = TTIImpl->getIntImmCost(Imm, Ty);
453 assert(Cost >= 0 && "TTI should not produce negative costs!");
454 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000455}
456
Chandler Carruth93205eb2015-08-05 18:08:10 +0000457int TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx,
458 const APInt &Imm, Type *Ty) const {
459 int Cost = TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty);
460 assert(Cost >= 0 && "TTI should not produce negative costs!");
461 return Cost;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000462}
463
Chandler Carruth93205eb2015-08-05 18:08:10 +0000464int TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
465 const APInt &Imm, Type *Ty) const {
466 int Cost = TTIImpl->getIntImmCost(IID, Idx, Imm, Ty);
467 assert(Cost >= 0 && "TTI should not produce negative costs!");
468 return Cost;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000469}
470
Zi Xuan Wu98022682019-10-12 02:53:04 +0000471unsigned TargetTransformInfo::getNumberOfRegisters(unsigned ClassID) const {
472 return TTIImpl->getNumberOfRegisters(ClassID);
473}
474
475unsigned TargetTransformInfo::getRegisterClassForType(bool Vector, Type *Ty) const {
476 return TTIImpl->getRegisterClassForType(Vector, Ty);
477}
478
479const char* TargetTransformInfo::getRegisterClassName(unsigned ClassID) const {
480 return TTIImpl->getRegisterClassName(ClassID);
Chandler Carruth539edf42013-01-05 11:43:11 +0000481}
482
Nadav Rotemb1791a72013-01-09 22:29:00 +0000483unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000484 return TTIImpl->getRegisterBitWidth(Vector);
Nadav Rotemb1791a72013-01-09 22:29:00 +0000485}
486
Adam Nemete29686e2017-05-15 21:15:01 +0000487unsigned TargetTransformInfo::getMinVectorRegisterBitWidth() const {
488 return TTIImpl->getMinVectorRegisterBitWidth();
489}
490
Krzysztof Parzyszek5d93fdf2018-03-27 16:14:11 +0000491bool TargetTransformInfo::shouldMaximizeVectorBandwidth(bool OptSize) const {
492 return TTIImpl->shouldMaximizeVectorBandwidth(OptSize);
493}
494
Krzysztof Parzyszekdfed9412018-04-13 20:16:32 +0000495unsigned TargetTransformInfo::getMinimumVF(unsigned ElemWidth) const {
496 return TTIImpl->getMinimumVF(ElemWidth);
497}
498
Jun Bum Limdee55652017-04-03 19:20:07 +0000499bool TargetTransformInfo::shouldConsiderAddressTypePromotion(
500 const Instruction &I, bool &AllowPromotionWithoutCommonHeader) const {
501 return TTIImpl->shouldConsiderAddressTypePromotion(
502 I, AllowPromotionWithoutCommonHeader);
503}
504
Adam Nemetaf761102016-01-21 18:28:36 +0000505unsigned TargetTransformInfo::getCacheLineSize() const {
506 return TTIImpl->getCacheLineSize();
507}
508
Tobias Grosserd7eb6192017-08-24 09:46:25 +0000509llvm::Optional<unsigned> TargetTransformInfo::getCacheSize(CacheLevel Level)
510 const {
511 return TTIImpl->getCacheSize(Level);
512}
513
514llvm::Optional<unsigned> TargetTransformInfo::getCacheAssociativity(
515 CacheLevel Level) const {
516 return TTIImpl->getCacheAssociativity(Level);
517}
518
Adam Nemetdadfbb52016-01-27 22:21:25 +0000519unsigned TargetTransformInfo::getPrefetchDistance() const {
520 return TTIImpl->getPrefetchDistance();
521}
522
Adam Nemet6d8beec2016-03-18 00:27:38 +0000523unsigned TargetTransformInfo::getMinPrefetchStride() const {
524 return TTIImpl->getMinPrefetchStride();
525}
526
Adam Nemet709e3042016-03-18 00:27:43 +0000527unsigned TargetTransformInfo::getMaxPrefetchIterationsAhead() const {
528 return TTIImpl->getMaxPrefetchIterationsAhead();
529}
530
Wei Mi062c7442015-05-06 17:12:25 +0000531unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const {
532 return TTIImpl->getMaxInterleaveFactor(VF);
Nadav Rotemb696c362013-01-09 01:15:42 +0000533}
534
Jonas Paulsson29d80f02018-10-05 14:34:04 +0000535TargetTransformInfo::OperandValueKind
Simon Pilgrim077a42c2018-11-13 13:45:10 +0000536TargetTransformInfo::getOperandInfo(Value *V, OperandValueProperties &OpProps) {
Jonas Paulsson29d80f02018-10-05 14:34:04 +0000537 OperandValueKind OpInfo = OK_AnyValue;
538 OpProps = OP_None;
539
540 if (auto *CI = dyn_cast<ConstantInt>(V)) {
541 if (CI->getValue().isPowerOf2())
542 OpProps = OP_PowerOf2;
543 return OK_UniformConstantValue;
544 }
545
Simon Pilgrim2b166c52018-11-14 15:04:08 +0000546 // A broadcast shuffle creates a uniform value.
547 // TODO: Add support for non-zero index broadcasts.
548 // TODO: Add support for different source vector width.
549 if (auto *ShuffleInst = dyn_cast<ShuffleVectorInst>(V))
550 if (ShuffleInst->isZeroEltSplat())
551 OpInfo = OK_UniformValue;
552
Jonas Paulsson29d80f02018-10-05 14:34:04 +0000553 const Value *Splat = getSplatValue(V);
554
555 // Check for a splat of a constant or for a non uniform vector of constants
556 // and check if the constant(s) are all powers of two.
557 if (isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) {
558 OpInfo = OK_NonUniformConstantValue;
559 if (Splat) {
560 OpInfo = OK_UniformConstantValue;
561 if (auto *CI = dyn_cast<ConstantInt>(Splat))
562 if (CI->getValue().isPowerOf2())
563 OpProps = OP_PowerOf2;
564 } else if (auto *CDS = dyn_cast<ConstantDataSequential>(V)) {
565 OpProps = OP_PowerOf2;
566 for (unsigned I = 0, E = CDS->getNumElements(); I != E; ++I) {
567 if (auto *CI = dyn_cast<ConstantInt>(CDS->getElementAsConstant(I)))
568 if (CI->getValue().isPowerOf2())
569 continue;
570 OpProps = OP_None;
571 break;
572 }
573 }
574 }
575
576 // Check for a splat of a uniform value. This is not loop aware, so return
577 // true only for the obviously uniform cases (argument, globalvalue)
578 if (Splat && (isa<Argument>(Splat) || isa<GlobalValue>(Splat)))
579 OpInfo = OK_UniformValue;
580
581 return OpInfo;
582}
583
Chandler Carruth93205eb2015-08-05 18:08:10 +0000584int TargetTransformInfo::getArithmeticInstrCost(
Chandler Carruth705b1852015-01-31 03:43:40 +0000585 unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
586 OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000587 OperandValueProperties Opd2PropInfo,
588 ArrayRef<const Value *> Args) const {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000589 int Cost = TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000590 Opd1PropInfo, Opd2PropInfo, Args);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000591 assert(Cost >= 0 && "TTI should not produce negative costs!");
592 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000593}
594
Chandler Carruth93205eb2015-08-05 18:08:10 +0000595int TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty, int Index,
596 Type *SubTp) const {
597 int Cost = TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
598 assert(Cost >= 0 && "TTI should not produce negative costs!");
599 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000600}
601
Chandler Carruth93205eb2015-08-05 18:08:10 +0000602int TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000603 Type *Src, const Instruction *I) const {
604 assert ((I == nullptr || I->getOpcode() == Opcode) &&
605 "Opcode should reflect passed instruction.");
606 int Cost = TTIImpl->getCastInstrCost(Opcode, Dst, Src, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000607 assert(Cost >= 0 && "TTI should not produce negative costs!");
608 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000609}
610
Matthew Simpsone5dfb082016-04-27 15:20:21 +0000611int TargetTransformInfo::getExtractWithExtendCost(unsigned Opcode, Type *Dst,
612 VectorType *VecTy,
613 unsigned Index) const {
614 int Cost = TTIImpl->getExtractWithExtendCost(Opcode, Dst, VecTy, Index);
615 assert(Cost >= 0 && "TTI should not produce negative costs!");
616 return Cost;
617}
618
Chandler Carruth93205eb2015-08-05 18:08:10 +0000619int TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
620 int Cost = TTIImpl->getCFInstrCost(Opcode);
621 assert(Cost >= 0 && "TTI should not produce negative costs!");
622 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000623}
624
Chandler Carruth93205eb2015-08-05 18:08:10 +0000625int TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000626 Type *CondTy, const Instruction *I) const {
627 assert ((I == nullptr || I->getOpcode() == Opcode) &&
628 "Opcode should reflect passed instruction.");
629 int Cost = TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000630 assert(Cost >= 0 && "TTI should not produce negative costs!");
631 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000632}
633
Chandler Carruth93205eb2015-08-05 18:08:10 +0000634int TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
635 unsigned Index) const {
636 int Cost = TTIImpl->getVectorInstrCost(Opcode, Val, Index);
637 assert(Cost >= 0 && "TTI should not produce negative costs!");
638 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000639}
640
Chandler Carruth93205eb2015-08-05 18:08:10 +0000641int TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
Guillaume Chateleta4783ef2019-10-22 17:16:52 +0200642 MaybeAlign Alignment,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000643 unsigned AddressSpace,
644 const Instruction *I) const {
645 assert ((I == nullptr || I->getOpcode() == Opcode) &&
646 "Opcode should reflect passed instruction.");
647 int Cost = TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000648 assert(Cost >= 0 && "TTI should not produce negative costs!");
649 return Cost;
Elena Demikhovskya3232f72015-01-25 08:44:46 +0000650}
651
Chandler Carruth93205eb2015-08-05 18:08:10 +0000652int TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
653 unsigned Alignment,
654 unsigned AddressSpace) const {
655 int Cost =
656 TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
657 assert(Cost >= 0 && "TTI should not produce negative costs!");
658 return Cost;
Chandler Carruth705b1852015-01-31 03:43:40 +0000659}
660
Elena Demikhovsky54946982015-12-28 20:10:59 +0000661int TargetTransformInfo::getGatherScatterOpCost(unsigned Opcode, Type *DataTy,
662 Value *Ptr, bool VariableMask,
663 unsigned Alignment) const {
664 int Cost = TTIImpl->getGatherScatterOpCost(Opcode, DataTy, Ptr, VariableMask,
665 Alignment);
666 assert(Cost >= 0 && "TTI should not produce negative costs!");
667 return Cost;
668}
669
Chandler Carruth93205eb2015-08-05 18:08:10 +0000670int TargetTransformInfo::getInterleavedMemoryOpCost(
Hao Liu32c05392015-06-08 06:39:56 +0000671 unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
Dorit Nuzman34da6dd2018-10-31 09:57:56 +0000672 unsigned Alignment, unsigned AddressSpace, bool UseMaskForCond,
673 bool UseMaskForGaps) const {
674 int Cost = TTIImpl->getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
675 Alignment, AddressSpace,
676 UseMaskForCond,
677 UseMaskForGaps);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000678 assert(Cost >= 0 && "TTI should not produce negative costs!");
679 return Cost;
Hao Liu32c05392015-06-08 06:39:56 +0000680}
681
Chandler Carruth93205eb2015-08-05 18:08:10 +0000682int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
Jonas Paulssona48ea232017-03-14 06:35:36 +0000683 ArrayRef<Type *> Tys, FastMathFlags FMF,
684 unsigned ScalarizationCostPassed) const {
685 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys, FMF,
686 ScalarizationCostPassed);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000687 assert(Cost >= 0 && "TTI should not produce negative costs!");
688 return Cost;
689}
690
Elena Demikhovsky54946982015-12-28 20:10:59 +0000691int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
Jonas Paulssona48ea232017-03-14 06:35:36 +0000692 ArrayRef<Value *> Args, FastMathFlags FMF, unsigned VF) const {
693 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Args, FMF, VF);
Elena Demikhovsky54946982015-12-28 20:10:59 +0000694 assert(Cost >= 0 && "TTI should not produce negative costs!");
695 return Cost;
696}
697
Chandler Carruth93205eb2015-08-05 18:08:10 +0000698int TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy,
699 ArrayRef<Type *> Tys) const {
700 int Cost = TTIImpl->getCallInstrCost(F, RetTy, Tys);
701 assert(Cost >= 0 && "TTI should not produce negative costs!");
702 return Cost;
Michael Zolotukhin7ed84a82015-03-17 19:26:23 +0000703}
704
Chandler Carruth539edf42013-01-05 11:43:11 +0000705unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000706 return TTIImpl->getNumberOfParts(Tp);
Chandler Carruth539edf42013-01-05 11:43:11 +0000707}
708
Chandler Carruth93205eb2015-08-05 18:08:10 +0000709int TargetTransformInfo::getAddressComputationCost(Type *Tp,
Mohammed Agabaria23599ba2017-01-05 14:03:41 +0000710 ScalarEvolution *SE,
711 const SCEV *Ptr) const {
712 int Cost = TTIImpl->getAddressComputationCost(Tp, SE, Ptr);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000713 assert(Cost >= 0 && "TTI should not produce negative costs!");
714 return Cost;
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000715}
Chandler Carruth539edf42013-01-05 11:43:11 +0000716
Sjoerd Meijerea31ddb2019-04-30 10:28:50 +0000717int TargetTransformInfo::getMemcpyCost(const Instruction *I) const {
718 int Cost = TTIImpl->getMemcpyCost(I);
719 assert(Cost >= 0 && "TTI should not produce negative costs!");
720 return Cost;
721}
722
Alexey Bataev3e9b3eb2017-07-31 14:19:32 +0000723int TargetTransformInfo::getArithmeticReductionCost(unsigned Opcode, Type *Ty,
724 bool IsPairwiseForm) const {
725 int Cost = TTIImpl->getArithmeticReductionCost(Opcode, Ty, IsPairwiseForm);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000726 assert(Cost >= 0 && "TTI should not produce negative costs!");
727 return Cost;
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000728}
729
Alexey Bataev6dd29fc2017-09-08 13:49:36 +0000730int TargetTransformInfo::getMinMaxReductionCost(Type *Ty, Type *CondTy,
731 bool IsPairwiseForm,
732 bool IsUnsigned) const {
733 int Cost =
734 TTIImpl->getMinMaxReductionCost(Ty, CondTy, IsPairwiseForm, IsUnsigned);
735 assert(Cost >= 0 && "TTI should not produce negative costs!");
736 return Cost;
737}
738
Chandler Carruth705b1852015-01-31 03:43:40 +0000739unsigned
740TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
741 return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
Chad Rosierf9327d62015-01-26 22:51:15 +0000742}
743
744bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst,
745 MemIntrinsicInfo &Info) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000746 return TTIImpl->getTgtMemIntrinsic(Inst, Info);
Chad Rosierf9327d62015-01-26 22:51:15 +0000747}
748
Anna Thomasb2a212c2017-06-06 16:45:25 +0000749unsigned TargetTransformInfo::getAtomicMemIntrinsicMaxElementSize() const {
750 return TTIImpl->getAtomicMemIntrinsicMaxElementSize();
751}
752
Chandler Carruth705b1852015-01-31 03:43:40 +0000753Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic(
754 IntrinsicInst *Inst, Type *ExpectedType) const {
755 return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
756}
757
Sean Fertile9cd1cdf2017-07-07 02:00:06 +0000758Type *TargetTransformInfo::getMemcpyLoopLoweringType(LLVMContext &Context,
759 Value *Length,
760 unsigned SrcAlign,
761 unsigned DestAlign) const {
762 return TTIImpl->getMemcpyLoopLoweringType(Context, Length, SrcAlign,
763 DestAlign);
764}
765
766void TargetTransformInfo::getMemcpyLoopResidualLoweringType(
767 SmallVectorImpl<Type *> &OpsOut, LLVMContext &Context,
768 unsigned RemainingBytes, unsigned SrcAlign, unsigned DestAlign) const {
769 TTIImpl->getMemcpyLoopResidualLoweringType(OpsOut, Context, RemainingBytes,
770 SrcAlign, DestAlign);
771}
772
Eric Christopherd566fb12015-07-29 22:09:48 +0000773bool TargetTransformInfo::areInlineCompatible(const Function *Caller,
774 const Function *Callee) const {
775 return TTIImpl->areInlineCompatible(Caller, Callee);
Eric Christopher4371b132015-07-02 01:11:47 +0000776}
777
Tom Stellard3d36e5c2019-01-16 05:15:31 +0000778bool TargetTransformInfo::areFunctionArgsABICompatible(
779 const Function *Caller, const Function *Callee,
780 SmallPtrSetImpl<Argument *> &Args) const {
781 return TTIImpl->areFunctionArgsABICompatible(Caller, Callee, Args);
782}
783
Krzysztof Parzyszek0b377e02018-03-26 13:10:09 +0000784bool TargetTransformInfo::isIndexedLoadLegal(MemIndexedMode Mode,
785 Type *Ty) const {
786 return TTIImpl->isIndexedLoadLegal(Mode, Ty);
787}
788
789bool TargetTransformInfo::isIndexedStoreLegal(MemIndexedMode Mode,
790 Type *Ty) const {
791 return TTIImpl->isIndexedStoreLegal(Mode, Ty);
792}
793
Volkan Keles1c386812016-10-03 10:31:34 +0000794unsigned TargetTransformInfo::getLoadStoreVecRegBitWidth(unsigned AS) const {
795 return TTIImpl->getLoadStoreVecRegBitWidth(AS);
796}
797
798bool TargetTransformInfo::isLegalToVectorizeLoad(LoadInst *LI) const {
799 return TTIImpl->isLegalToVectorizeLoad(LI);
800}
801
802bool TargetTransformInfo::isLegalToVectorizeStore(StoreInst *SI) const {
803 return TTIImpl->isLegalToVectorizeStore(SI);
804}
805
806bool TargetTransformInfo::isLegalToVectorizeLoadChain(
807 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
808 return TTIImpl->isLegalToVectorizeLoadChain(ChainSizeInBytes, Alignment,
809 AddrSpace);
810}
811
812bool TargetTransformInfo::isLegalToVectorizeStoreChain(
813 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
814 return TTIImpl->isLegalToVectorizeStoreChain(ChainSizeInBytes, Alignment,
815 AddrSpace);
816}
817
818unsigned TargetTransformInfo::getLoadVectorFactor(unsigned VF,
819 unsigned LoadSize,
820 unsigned ChainSizeInBytes,
821 VectorType *VecTy) const {
822 return TTIImpl->getLoadVectorFactor(VF, LoadSize, ChainSizeInBytes, VecTy);
823}
824
825unsigned TargetTransformInfo::getStoreVectorFactor(unsigned VF,
826 unsigned StoreSize,
827 unsigned ChainSizeInBytes,
828 VectorType *VecTy) const {
829 return TTIImpl->getStoreVectorFactor(VF, StoreSize, ChainSizeInBytes, VecTy);
830}
831
Amara Emersoncf9daa32017-05-09 10:43:25 +0000832bool TargetTransformInfo::useReductionIntrinsic(unsigned Opcode,
833 Type *Ty, ReductionFlags Flags) const {
834 return TTIImpl->useReductionIntrinsic(Opcode, Ty, Flags);
835}
836
Amara Emerson836b0f42017-05-10 09:42:49 +0000837bool TargetTransformInfo::shouldExpandReduction(const IntrinsicInst *II) const {
838 return TTIImpl->shouldExpandReduction(II);
839}
Amara Emersoncf9daa32017-05-09 10:43:25 +0000840
Amara Emerson14688222019-06-17 23:20:29 +0000841unsigned TargetTransformInfo::getGISelRematGlobalCost() const {
842 return TTIImpl->getGISelRematGlobalCost();
843}
844
Guozhi Wei62d64142017-09-08 22:29:17 +0000845int TargetTransformInfo::getInstructionLatency(const Instruction *I) const {
846 return TTIImpl->getInstructionLatency(I);
847}
848
Guozhi Wei62d64142017-09-08 22:29:17 +0000849static bool matchPairwiseShuffleMask(ShuffleVectorInst *SI, bool IsLeft,
850 unsigned Level) {
851 // We don't need a shuffle if we just want to have element 0 in position 0 of
852 // the vector.
853 if (!SI && Level == 0 && IsLeft)
854 return true;
855 else if (!SI)
856 return false;
857
858 SmallVector<int, 32> Mask(SI->getType()->getVectorNumElements(), -1);
859
860 // Build a mask of 0, 2, ... (left) or 1, 3, ... (right) depending on whether
861 // we look at the left or right side.
862 for (unsigned i = 0, e = (1 << Level), val = !IsLeft; i != e; ++i, val += 2)
863 Mask[i] = val;
864
865 SmallVector<int, 16> ActualMask = SI->getShuffleMask();
866 return Mask == ActualMask;
867}
868
869namespace {
870/// Kind of the reduction data.
871enum ReductionKind {
872 RK_None, /// Not a reduction.
873 RK_Arithmetic, /// Binary reduction data.
874 RK_MinMax, /// Min/max reduction data.
875 RK_UnsignedMinMax, /// Unsigned min/max reduction data.
876};
877/// Contains opcode + LHS/RHS parts of the reduction operations.
878struct ReductionData {
879 ReductionData() = delete;
880 ReductionData(ReductionKind Kind, unsigned Opcode, Value *LHS, Value *RHS)
881 : Opcode(Opcode), LHS(LHS), RHS(RHS), Kind(Kind) {
882 assert(Kind != RK_None && "expected binary or min/max reduction only.");
883 }
884 unsigned Opcode = 0;
885 Value *LHS = nullptr;
886 Value *RHS = nullptr;
887 ReductionKind Kind = RK_None;
888 bool hasSameData(ReductionData &RD) const {
889 return Kind == RD.Kind && Opcode == RD.Opcode;
890 }
891};
892} // namespace
893
894static Optional<ReductionData> getReductionData(Instruction *I) {
895 Value *L, *R;
896 if (m_BinOp(m_Value(L), m_Value(R)).match(I))
Fangrui Songf78650a2018-07-30 19:41:25 +0000897 return ReductionData(RK_Arithmetic, I->getOpcode(), L, R);
Guozhi Wei62d64142017-09-08 22:29:17 +0000898 if (auto *SI = dyn_cast<SelectInst>(I)) {
899 if (m_SMin(m_Value(L), m_Value(R)).match(SI) ||
900 m_SMax(m_Value(L), m_Value(R)).match(SI) ||
901 m_OrdFMin(m_Value(L), m_Value(R)).match(SI) ||
902 m_OrdFMax(m_Value(L), m_Value(R)).match(SI) ||
903 m_UnordFMin(m_Value(L), m_Value(R)).match(SI) ||
904 m_UnordFMax(m_Value(L), m_Value(R)).match(SI)) {
905 auto *CI = cast<CmpInst>(SI->getCondition());
Fangrui Songf78650a2018-07-30 19:41:25 +0000906 return ReductionData(RK_MinMax, CI->getOpcode(), L, R);
907 }
Guozhi Wei62d64142017-09-08 22:29:17 +0000908 if (m_UMin(m_Value(L), m_Value(R)).match(SI) ||
909 m_UMax(m_Value(L), m_Value(R)).match(SI)) {
910 auto *CI = cast<CmpInst>(SI->getCondition());
911 return ReductionData(RK_UnsignedMinMax, CI->getOpcode(), L, R);
912 }
913 }
914 return llvm::None;
915}
916
917static ReductionKind matchPairwiseReductionAtLevel(Instruction *I,
918 unsigned Level,
919 unsigned NumLevels) {
920 // Match one level of pairwise operations.
921 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef,
922 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef>
923 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef,
924 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef>
925 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1
926 if (!I)
927 return RK_None;
928
929 assert(I->getType()->isVectorTy() && "Expecting a vector type");
930
931 Optional<ReductionData> RD = getReductionData(I);
932 if (!RD)
933 return RK_None;
934
935 ShuffleVectorInst *LS = dyn_cast<ShuffleVectorInst>(RD->LHS);
936 if (!LS && Level)
937 return RK_None;
938 ShuffleVectorInst *RS = dyn_cast<ShuffleVectorInst>(RD->RHS);
939 if (!RS && Level)
940 return RK_None;
941
942 // On level 0 we can omit one shufflevector instruction.
943 if (!Level && !RS && !LS)
944 return RK_None;
945
946 // Shuffle inputs must match.
947 Value *NextLevelOpL = LS ? LS->getOperand(0) : nullptr;
948 Value *NextLevelOpR = RS ? RS->getOperand(0) : nullptr;
949 Value *NextLevelOp = nullptr;
950 if (NextLevelOpR && NextLevelOpL) {
951 // If we have two shuffles their operands must match.
952 if (NextLevelOpL != NextLevelOpR)
953 return RK_None;
954
955 NextLevelOp = NextLevelOpL;
956 } else if (Level == 0 && (NextLevelOpR || NextLevelOpL)) {
957 // On the first level we can omit the shufflevector <0, undef,...>. So the
958 // input to the other shufflevector <1, undef> must match with one of the
959 // inputs to the current binary operation.
960 // Example:
961 // %NextLevelOpL = shufflevector %R, <1, undef ...>
962 // %BinOp = fadd %NextLevelOpL, %R
963 if (NextLevelOpL && NextLevelOpL != RD->RHS)
964 return RK_None;
965 else if (NextLevelOpR && NextLevelOpR != RD->LHS)
966 return RK_None;
967
968 NextLevelOp = NextLevelOpL ? RD->RHS : RD->LHS;
969 } else
970 return RK_None;
971
972 // Check that the next levels binary operation exists and matches with the
973 // current one.
974 if (Level + 1 != NumLevels) {
975 Optional<ReductionData> NextLevelRD =
976 getReductionData(cast<Instruction>(NextLevelOp));
977 if (!NextLevelRD || !RD->hasSameData(*NextLevelRD))
978 return RK_None;
979 }
980
981 // Shuffle mask for pairwise operation must match.
982 if (matchPairwiseShuffleMask(LS, /*IsLeft=*/true, Level)) {
983 if (!matchPairwiseShuffleMask(RS, /*IsLeft=*/false, Level))
984 return RK_None;
985 } else if (matchPairwiseShuffleMask(RS, /*IsLeft=*/true, Level)) {
986 if (!matchPairwiseShuffleMask(LS, /*IsLeft=*/false, Level))
987 return RK_None;
988 } else {
989 return RK_None;
990 }
991
992 if (++Level == NumLevels)
993 return RD->Kind;
994
995 // Match next level.
996 return matchPairwiseReductionAtLevel(cast<Instruction>(NextLevelOp), Level,
997 NumLevels);
998}
999
1000static ReductionKind matchPairwiseReduction(const ExtractElementInst *ReduxRoot,
1001 unsigned &Opcode, Type *&Ty) {
1002 if (!EnableReduxCost)
1003 return RK_None;
1004
1005 // Need to extract the first element.
1006 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1));
1007 unsigned Idx = ~0u;
1008 if (CI)
1009 Idx = CI->getZExtValue();
1010 if (Idx != 0)
1011 return RK_None;
1012
1013 auto *RdxStart = dyn_cast<Instruction>(ReduxRoot->getOperand(0));
1014 if (!RdxStart)
1015 return RK_None;
1016 Optional<ReductionData> RD = getReductionData(RdxStart);
1017 if (!RD)
1018 return RK_None;
1019
1020 Type *VecTy = RdxStart->getType();
1021 unsigned NumVecElems = VecTy->getVectorNumElements();
1022 if (!isPowerOf2_32(NumVecElems))
1023 return RK_None;
1024
1025 // We look for a sequence of shuffle,shuffle,add triples like the following
1026 // that builds a pairwise reduction tree.
Fangrui Songf78650a2018-07-30 19:41:25 +00001027 //
Guozhi Wei62d64142017-09-08 22:29:17 +00001028 // (X0, X1, X2, X3)
1029 // (X0 + X1, X2 + X3, undef, undef)
1030 // ((X0 + X1) + (X2 + X3), undef, undef, undef)
Fangrui Songf78650a2018-07-30 19:41:25 +00001031 //
Guozhi Wei62d64142017-09-08 22:29:17 +00001032 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef,
1033 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef>
1034 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef,
1035 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef>
1036 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1
1037 // %rdx.shuf.1.0 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef,
1038 // <4 x i32> <i32 0, i32 undef, i32 undef, i32 undef>
1039 // %rdx.shuf.1.1 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef,
1040 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
1041 // %bin.rdx8 = fadd <4 x float> %rdx.shuf.1.0, %rdx.shuf.1.1
1042 // %r = extractelement <4 x float> %bin.rdx8, i32 0
1043 if (matchPairwiseReductionAtLevel(RdxStart, 0, Log2_32(NumVecElems)) ==
1044 RK_None)
1045 return RK_None;
1046
1047 Opcode = RD->Opcode;
1048 Ty = VecTy;
1049
1050 return RD->Kind;
1051}
1052
1053static std::pair<Value *, ShuffleVectorInst *>
1054getShuffleAndOtherOprd(Value *L, Value *R) {
1055 ShuffleVectorInst *S = nullptr;
1056
1057 if ((S = dyn_cast<ShuffleVectorInst>(L)))
1058 return std::make_pair(R, S);
1059
1060 S = dyn_cast<ShuffleVectorInst>(R);
1061 return std::make_pair(L, S);
1062}
1063
1064static ReductionKind
1065matchVectorSplittingReduction(const ExtractElementInst *ReduxRoot,
1066 unsigned &Opcode, Type *&Ty) {
1067 if (!EnableReduxCost)
1068 return RK_None;
1069
1070 // Need to extract the first element.
1071 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1));
1072 unsigned Idx = ~0u;
1073 if (CI)
1074 Idx = CI->getZExtValue();
1075 if (Idx != 0)
1076 return RK_None;
1077
1078 auto *RdxStart = dyn_cast<Instruction>(ReduxRoot->getOperand(0));
1079 if (!RdxStart)
1080 return RK_None;
1081 Optional<ReductionData> RD = getReductionData(RdxStart);
1082 if (!RD)
1083 return RK_None;
1084
1085 Type *VecTy = ReduxRoot->getOperand(0)->getType();
1086 unsigned NumVecElems = VecTy->getVectorNumElements();
1087 if (!isPowerOf2_32(NumVecElems))
1088 return RK_None;
1089
1090 // We look for a sequence of shuffles and adds like the following matching one
1091 // fadd, shuffle vector pair at a time.
Fangrui Songf78650a2018-07-30 19:41:25 +00001092 //
Guozhi Wei62d64142017-09-08 22:29:17 +00001093 // %rdx.shuf = shufflevector <4 x float> %rdx, <4 x float> undef,
1094 // <4 x i32> <i32 2, i32 3, i32 undef, i32 undef>
1095 // %bin.rdx = fadd <4 x float> %rdx, %rdx.shuf
1096 // %rdx.shuf7 = shufflevector <4 x float> %bin.rdx, <4 x float> undef,
1097 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
1098 // %bin.rdx8 = fadd <4 x float> %bin.rdx, %rdx.shuf7
1099 // %r = extractelement <4 x float> %bin.rdx8, i32 0
1100
1101 unsigned MaskStart = 1;
1102 Instruction *RdxOp = RdxStart;
Fangrui Songf78650a2018-07-30 19:41:25 +00001103 SmallVector<int, 32> ShuffleMask(NumVecElems, 0);
Guozhi Wei62d64142017-09-08 22:29:17 +00001104 unsigned NumVecElemsRemain = NumVecElems;
1105 while (NumVecElemsRemain - 1) {
1106 // Check for the right reduction operation.
1107 if (!RdxOp)
1108 return RK_None;
1109 Optional<ReductionData> RDLevel = getReductionData(RdxOp);
1110 if (!RDLevel || !RDLevel->hasSameData(*RD))
1111 return RK_None;
1112
1113 Value *NextRdxOp;
1114 ShuffleVectorInst *Shuffle;
1115 std::tie(NextRdxOp, Shuffle) =
1116 getShuffleAndOtherOprd(RDLevel->LHS, RDLevel->RHS);
1117
1118 // Check the current reduction operation and the shuffle use the same value.
1119 if (Shuffle == nullptr)
1120 return RK_None;
1121 if (Shuffle->getOperand(0) != NextRdxOp)
1122 return RK_None;
1123
1124 // Check that shuffle masks matches.
1125 for (unsigned j = 0; j != MaskStart; ++j)
1126 ShuffleMask[j] = MaskStart + j;
1127 // Fill the rest of the mask with -1 for undef.
1128 std::fill(&ShuffleMask[MaskStart], ShuffleMask.end(), -1);
1129
1130 SmallVector<int, 16> Mask = Shuffle->getShuffleMask();
1131 if (ShuffleMask != Mask)
1132 return RK_None;
1133
1134 RdxOp = dyn_cast<Instruction>(NextRdxOp);
1135 NumVecElemsRemain /= 2;
1136 MaskStart *= 2;
1137 }
1138
1139 Opcode = RD->Opcode;
1140 Ty = VecTy;
1141 return RD->Kind;
1142}
1143
1144int TargetTransformInfo::getInstructionThroughput(const Instruction *I) const {
1145 switch (I->getOpcode()) {
1146 case Instruction::GetElementPtr:
1147 return getUserCost(I);
1148
1149 case Instruction::Ret:
1150 case Instruction::PHI:
1151 case Instruction::Br: {
1152 return getCFInstrCost(I->getOpcode());
1153 }
1154 case Instruction::Add:
1155 case Instruction::FAdd:
1156 case Instruction::Sub:
1157 case Instruction::FSub:
1158 case Instruction::Mul:
1159 case Instruction::FMul:
1160 case Instruction::UDiv:
1161 case Instruction::SDiv:
1162 case Instruction::FDiv:
1163 case Instruction::URem:
1164 case Instruction::SRem:
1165 case Instruction::FRem:
1166 case Instruction::Shl:
1167 case Instruction::LShr:
1168 case Instruction::AShr:
1169 case Instruction::And:
1170 case Instruction::Or:
1171 case Instruction::Xor: {
Simon Pilgrim4162d772018-05-22 10:40:09 +00001172 TargetTransformInfo::OperandValueKind Op1VK, Op2VK;
1173 TargetTransformInfo::OperandValueProperties Op1VP, Op2VP;
1174 Op1VK = getOperandInfo(I->getOperand(0), Op1VP);
1175 Op2VK = getOperandInfo(I->getOperand(1), Op2VP);
1176 SmallVector<const Value *, 2> Operands(I->operand_values());
1177 return getArithmeticInstrCost(I->getOpcode(), I->getType(), Op1VK, Op2VK,
1178 Op1VP, Op2VP, Operands);
Guozhi Wei62d64142017-09-08 22:29:17 +00001179 }
Craig Topper50d50282019-05-28 04:09:18 +00001180 case Instruction::FNeg: {
1181 TargetTransformInfo::OperandValueKind Op1VK, Op2VK;
1182 TargetTransformInfo::OperandValueProperties Op1VP, Op2VP;
1183 Op1VK = getOperandInfo(I->getOperand(0), Op1VP);
1184 Op2VK = OK_AnyValue;
1185 Op2VP = OP_None;
1186 SmallVector<const Value *, 2> Operands(I->operand_values());
1187 return getArithmeticInstrCost(I->getOpcode(), I->getType(), Op1VK, Op2VK,
1188 Op1VP, Op2VP, Operands);
1189 }
Guozhi Wei62d64142017-09-08 22:29:17 +00001190 case Instruction::Select: {
1191 const SelectInst *SI = cast<SelectInst>(I);
1192 Type *CondTy = SI->getCondition()->getType();
1193 return getCmpSelInstrCost(I->getOpcode(), I->getType(), CondTy, I);
1194 }
1195 case Instruction::ICmp:
1196 case Instruction::FCmp: {
1197 Type *ValTy = I->getOperand(0)->getType();
1198 return getCmpSelInstrCost(I->getOpcode(), ValTy, I->getType(), I);
1199 }
1200 case Instruction::Store: {
1201 const StoreInst *SI = cast<StoreInst>(I);
1202 Type *ValTy = SI->getValueOperand()->getType();
1203 return getMemoryOpCost(I->getOpcode(), ValTy,
Guillaume Chateleta4783ef2019-10-22 17:16:52 +02001204 MaybeAlign(SI->getAlignment()),
1205 SI->getPointerAddressSpace(), I);
Guozhi Wei62d64142017-09-08 22:29:17 +00001206 }
1207 case Instruction::Load: {
1208 const LoadInst *LI = cast<LoadInst>(I);
1209 return getMemoryOpCost(I->getOpcode(), I->getType(),
Guillaume Chateleta4783ef2019-10-22 17:16:52 +02001210 MaybeAlign(LI->getAlignment()),
1211 LI->getPointerAddressSpace(), I);
Guozhi Wei62d64142017-09-08 22:29:17 +00001212 }
1213 case Instruction::ZExt:
1214 case Instruction::SExt:
1215 case Instruction::FPToUI:
1216 case Instruction::FPToSI:
1217 case Instruction::FPExt:
1218 case Instruction::PtrToInt:
1219 case Instruction::IntToPtr:
1220 case Instruction::SIToFP:
1221 case Instruction::UIToFP:
1222 case Instruction::Trunc:
1223 case Instruction::FPTrunc:
1224 case Instruction::BitCast:
1225 case Instruction::AddrSpaceCast: {
1226 Type *SrcTy = I->getOperand(0)->getType();
1227 return getCastInstrCost(I->getOpcode(), I->getType(), SrcTy, I);
1228 }
1229 case Instruction::ExtractElement: {
1230 const ExtractElementInst * EEI = cast<ExtractElementInst>(I);
1231 ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1));
1232 unsigned Idx = -1;
1233 if (CI)
1234 Idx = CI->getZExtValue();
1235
1236 // Try to match a reduction sequence (series of shufflevector and vector
1237 // adds followed by a extractelement).
1238 unsigned ReduxOpCode;
1239 Type *ReduxType;
1240
1241 switch (matchVectorSplittingReduction(EEI, ReduxOpCode, ReduxType)) {
1242 case RK_Arithmetic:
1243 return getArithmeticReductionCost(ReduxOpCode, ReduxType,
1244 /*IsPairwiseForm=*/false);
1245 case RK_MinMax:
1246 return getMinMaxReductionCost(
1247 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1248 /*IsPairwiseForm=*/false, /*IsUnsigned=*/false);
1249 case RK_UnsignedMinMax:
1250 return getMinMaxReductionCost(
1251 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1252 /*IsPairwiseForm=*/false, /*IsUnsigned=*/true);
1253 case RK_None:
1254 break;
1255 }
1256
1257 switch (matchPairwiseReduction(EEI, ReduxOpCode, ReduxType)) {
1258 case RK_Arithmetic:
1259 return getArithmeticReductionCost(ReduxOpCode, ReduxType,
1260 /*IsPairwiseForm=*/true);
1261 case RK_MinMax:
1262 return getMinMaxReductionCost(
1263 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1264 /*IsPairwiseForm=*/true, /*IsUnsigned=*/false);
1265 case RK_UnsignedMinMax:
1266 return getMinMaxReductionCost(
1267 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1268 /*IsPairwiseForm=*/true, /*IsUnsigned=*/true);
1269 case RK_None:
1270 break;
1271 }
1272
1273 return getVectorInstrCost(I->getOpcode(),
1274 EEI->getOperand(0)->getType(), Idx);
1275 }
1276 case Instruction::InsertElement: {
1277 const InsertElementInst * IE = cast<InsertElementInst>(I);
1278 ConstantInt *CI = dyn_cast<ConstantInt>(IE->getOperand(2));
Fangrui Songf78650a2018-07-30 19:41:25 +00001279 unsigned Idx = -1;
Guozhi Wei62d64142017-09-08 22:29:17 +00001280 if (CI)
1281 Idx = CI->getZExtValue();
1282 return getVectorInstrCost(I->getOpcode(),
1283 IE->getType(), Idx);
1284 }
Roman Lebedevcc95a452019-08-29 11:50:30 +00001285 case Instruction::ExtractValue:
1286 return 0; // Model all ExtractValue nodes as free.
Guozhi Wei62d64142017-09-08 22:29:17 +00001287 case Instruction::ShuffleVector: {
1288 const ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
Simon Pilgrimd0c71602018-11-09 16:28:19 +00001289 Type *Ty = Shuffle->getType();
1290 Type *SrcTy = Shuffle->getOperand(0)->getType();
1291
1292 // TODO: Identify and add costs for insert subvector, etc.
1293 int SubIndex;
1294 if (Shuffle->isExtractSubvectorMask(SubIndex))
Simon Pilgrim26e1c882018-11-09 18:30:59 +00001295 return TTIImpl->getShuffleCost(SK_ExtractSubvector, SrcTy, SubIndex, Ty);
Simon Pilgrimd0c71602018-11-09 16:28:19 +00001296
Sanjay Patel2ca33602018-06-19 18:44:00 +00001297 if (Shuffle->changesLength())
1298 return -1;
Fangrui Songf78650a2018-07-30 19:41:25 +00001299
Sanjay Patel2ca33602018-06-19 18:44:00 +00001300 if (Shuffle->isIdentity())
1301 return 0;
Guozhi Wei62d64142017-09-08 22:29:17 +00001302
Sanjay Patel2ca33602018-06-19 18:44:00 +00001303 if (Shuffle->isReverse())
1304 return TTIImpl->getShuffleCost(SK_Reverse, Ty, 0, nullptr);
Simon Pilgrim07839212018-06-12 14:47:13 +00001305
Sanjay Patel2ca33602018-06-19 18:44:00 +00001306 if (Shuffle->isSelect())
1307 return TTIImpl->getShuffleCost(SK_Select, Ty, 0, nullptr);
Simon Pilgrim07839212018-06-12 14:47:13 +00001308
Sanjay Patel2ca33602018-06-19 18:44:00 +00001309 if (Shuffle->isTranspose())
1310 return TTIImpl->getShuffleCost(SK_Transpose, Ty, 0, nullptr);
Matthew Simpsonb4096eb2018-04-26 13:48:33 +00001311
Sanjay Patel2ca33602018-06-19 18:44:00 +00001312 if (Shuffle->isZeroEltSplat())
1313 return TTIImpl->getShuffleCost(SK_Broadcast, Ty, 0, nullptr);
Guozhi Wei62d64142017-09-08 22:29:17 +00001314
Sanjay Patel2ca33602018-06-19 18:44:00 +00001315 if (Shuffle->isSingleSource())
1316 return TTIImpl->getShuffleCost(SK_PermuteSingleSrc, Ty, 0, nullptr);
Guozhi Wei62d64142017-09-08 22:29:17 +00001317
Sanjay Patel2ca33602018-06-19 18:44:00 +00001318 return TTIImpl->getShuffleCost(SK_PermuteTwoSrc, Ty, 0, nullptr);
Guozhi Wei62d64142017-09-08 22:29:17 +00001319 }
1320 case Instruction::Call:
1321 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1322 SmallVector<Value *, 4> Args(II->arg_operands());
1323
1324 FastMathFlags FMF;
1325 if (auto *FPMO = dyn_cast<FPMathOperator>(II))
1326 FMF = FPMO->getFastMathFlags();
1327
1328 return getIntrinsicInstrCost(II->getIntrinsicID(), II->getType(),
1329 Args, FMF);
1330 }
1331 return -1;
1332 default:
1333 // We don't have any information on this instruction.
1334 return -1;
1335 }
1336}
1337
Chandler Carruth705b1852015-01-31 03:43:40 +00001338TargetTransformInfo::Concept::~Concept() {}
1339
Chandler Carruthe0385522015-02-01 10:11:22 +00001340TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
1341
1342TargetIRAnalysis::TargetIRAnalysis(
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001343 std::function<Result(const Function &)> TTICallback)
Benjamin Kramer82de7d32016-05-27 14:27:24 +00001344 : TTICallback(std::move(TTICallback)) {}
Chandler Carruthe0385522015-02-01 10:11:22 +00001345
Chandler Carruth164a2aa62016-06-17 00:11:01 +00001346TargetIRAnalysis::Result TargetIRAnalysis::run(const Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +00001347 FunctionAnalysisManager &) {
Chandler Carruthe0385522015-02-01 10:11:22 +00001348 return TTICallback(F);
1349}
1350
Chandler Carruthdab4eae2016-11-23 17:53:26 +00001351AnalysisKey TargetIRAnalysis::Key;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +00001352
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001353TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(const Function &F) {
Mehdi Amini5010ebf2015-07-09 02:08:42 +00001354 return Result(F.getParent()->getDataLayout());
Chandler Carruthe0385522015-02-01 10:11:22 +00001355}
1356
Chandler Carruth705b1852015-01-31 03:43:40 +00001357// Register the basic pass.
1358INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
1359 "Target Transform Information", false, true)
1360char TargetTransformInfoWrapperPass::ID = 0;
Chandler Carruth539edf42013-01-05 11:43:11 +00001361
Chandler Carruth705b1852015-01-31 03:43:40 +00001362void TargetTransformInfoWrapperPass::anchor() {}
Chandler Carruth539edf42013-01-05 11:43:11 +00001363
Chandler Carruth705b1852015-01-31 03:43:40 +00001364TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass()
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001365 : ImmutablePass(ID) {
Chandler Carruth705b1852015-01-31 03:43:40 +00001366 initializeTargetTransformInfoWrapperPassPass(
1367 *PassRegistry::getPassRegistry());
1368}
1369
1370TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass(
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001371 TargetIRAnalysis TIRA)
1372 : ImmutablePass(ID), TIRA(std::move(TIRA)) {
Chandler Carruth705b1852015-01-31 03:43:40 +00001373 initializeTargetTransformInfoWrapperPassPass(
1374 *PassRegistry::getPassRegistry());
1375}
1376
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001377TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(const Function &F) {
Sean Silva36e0d012016-08-09 00:28:15 +00001378 FunctionAnalysisManager DummyFAM;
Chandler Carruth164a2aa62016-06-17 00:11:01 +00001379 TTI = TIRA.run(F, DummyFAM);
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001380 return *TTI;
1381}
1382
Chandler Carruth93dcdc42015-01-31 11:17:59 +00001383ImmutablePass *
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001384llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) {
1385 return new TargetTransformInfoWrapperPass(std::move(TIRA));
Chandler Carruth539edf42013-01-05 11:43:11 +00001386}