blob: 0b409840351de314cca30d74cc079daa2e5edf0e [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
Sjoerd Meijer6c2a4f52019-11-06 09:58:36 +0000246bool TargetTransformInfo::preferPredicateOverEpilogue(Loop *L, LoopInfo *LI,
247 ScalarEvolution &SE, AssumptionCache &AC, TargetLibraryInfo *TLI,
248 DominatorTree *DT, const LoopAccessInfo *LAI) const {
249 return TTIImpl->preferPredicateOverEpilogue(L, LI, SE, AC, TLI, DT, LAI);
250}
251
Chandler Carruth705b1852015-01-31 03:43:40 +0000252void TargetTransformInfo::getUnrollingPreferences(
Geoff Berry66d9bdb2017-06-28 15:53:17 +0000253 Loop *L, ScalarEvolution &SE, UnrollingPreferences &UP) const {
254 return TTIImpl->getUnrollingPreferences(L, SE, UP);
Hal Finkel8f2e7002013-09-11 19:25:43 +0000255}
256
Chandler Carruth539edf42013-01-05 11:43:11 +0000257bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000258 return TTIImpl->isLegalAddImmediate(Imm);
Chandler Carruth539edf42013-01-05 11:43:11 +0000259}
260
261bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000262 return TTIImpl->isLegalICmpImmediate(Imm);
Chandler Carruth539edf42013-01-05 11:43:11 +0000263}
264
265bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
266 int64_t BaseOffset,
267 bool HasBaseReg,
Matt Arsenaulte83379e2015-06-07 20:12:03 +0000268 int64_t Scale,
Jonas Paulsson024e3192017-07-21 11:59:37 +0000269 unsigned AddrSpace,
270 Instruction *I) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000271 return TTIImpl->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
Jonas Paulsson024e3192017-07-21 11:59:37 +0000272 Scale, AddrSpace, I);
Chandler Carruth539edf42013-01-05 11:43:11 +0000273}
274
Evgeny Stupachenkof2b3b462017-06-05 23:37:00 +0000275bool TargetTransformInfo::isLSRCostLess(LSRCost &C1, LSRCost &C2) const {
276 return TTIImpl->isLSRCostLess(C1, C2);
277}
278
Sanjay Pateld7c702b2018-02-05 23:43:05 +0000279bool TargetTransformInfo::canMacroFuseCmp() const {
280 return TTIImpl->canMacroFuseCmp();
281}
282
Chen Zhengdfdccbb2019-07-03 01:49:03 +0000283bool TargetTransformInfo::canSaveCmp(Loop *L, BranchInst **BI,
284 ScalarEvolution *SE, LoopInfo *LI,
285 DominatorTree *DT, AssumptionCache *AC,
286 TargetLibraryInfo *LibInfo) const {
287 return TTIImpl->canSaveCmp(L, BI, SE, LI, DT, AC, LibInfo);
288}
289
Krzysztof Parzyszek0b377e02018-03-26 13:10:09 +0000290bool TargetTransformInfo::shouldFavorPostInc() const {
291 return TTIImpl->shouldFavorPostInc();
292}
293
Sam Parker67756c02019-02-07 13:32:54 +0000294bool TargetTransformInfo::shouldFavorBackedgeIndex(const Loop *L) const {
295 return TTIImpl->shouldFavorBackedgeIndex(L);
296}
297
Sam Parker527a35e2019-10-14 10:00:21 +0000298bool TargetTransformInfo::isLegalMaskedStore(Type *DataType,
299 MaybeAlign Alignment) const {
300 return TTIImpl->isLegalMaskedStore(DataType, Alignment);
Chandler Carruth705b1852015-01-31 03:43:40 +0000301}
302
Sam Parker527a35e2019-10-14 10:00:21 +0000303bool TargetTransformInfo::isLegalMaskedLoad(Type *DataType,
304 MaybeAlign Alignment) const {
305 return TTIImpl->isLegalMaskedLoad(DataType, Alignment);
Chandler Carruth705b1852015-01-31 03:43:40 +0000306}
307
Warren Ristow6452bdd2019-06-17 17:20:08 +0000308bool TargetTransformInfo::isLegalNTStore(Type *DataType,
Guillaume Chatelet18f805a2019-09-27 12:54:21 +0000309 Align Alignment) const {
Warren Ristow6452bdd2019-06-17 17:20:08 +0000310 return TTIImpl->isLegalNTStore(DataType, Alignment);
311}
312
Guillaume Chatelet18f805a2019-09-27 12:54:21 +0000313bool TargetTransformInfo::isLegalNTLoad(Type *DataType, Align Alignment) const {
Warren Ristow6452bdd2019-06-17 17:20:08 +0000314 return TTIImpl->isLegalNTLoad(DataType, Alignment);
315}
316
Elena Demikhovsky09285852015-10-25 15:37:55 +0000317bool TargetTransformInfo::isLegalMaskedGather(Type *DataType) const {
318 return TTIImpl->isLegalMaskedGather(DataType);
319}
320
321bool TargetTransformInfo::isLegalMaskedScatter(Type *DataType) const {
Mohammed Agabariacef53dc2017-07-27 10:28:16 +0000322 return TTIImpl->isLegalMaskedScatter(DataType);
Elena Demikhovsky09285852015-10-25 15:37:55 +0000323}
324
Craig Topper9f0b17a2019-03-21 17:38:52 +0000325bool TargetTransformInfo::isLegalMaskedCompressStore(Type *DataType) const {
326 return TTIImpl->isLegalMaskedCompressStore(DataType);
327}
328
329bool TargetTransformInfo::isLegalMaskedExpandLoad(Type *DataType) const {
330 return TTIImpl->isLegalMaskedExpandLoad(DataType);
331}
332
Sanjay Patel6fd43912017-09-09 13:38:18 +0000333bool TargetTransformInfo::hasDivRemOp(Type *DataType, bool IsSigned) const {
334 return TTIImpl->hasDivRemOp(DataType, IsSigned);
335}
336
Artem Belevichcb8f6322017-10-24 20:31:44 +0000337bool TargetTransformInfo::hasVolatileVariant(Instruction *I,
338 unsigned AddrSpace) const {
339 return TTIImpl->hasVolatileVariant(I, AddrSpace);
340}
341
Jonas Paulsson8624b7e2017-05-24 13:42:56 +0000342bool TargetTransformInfo::prefersVectorizedAddressing() const {
343 return TTIImpl->prefersVectorizedAddressing();
344}
345
Quentin Colombetbf490d42013-05-31 21:29:03 +0000346int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
347 int64_t BaseOffset,
348 bool HasBaseReg,
Matt Arsenaulte83379e2015-06-07 20:12:03 +0000349 int64_t Scale,
350 unsigned AddrSpace) const {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000351 int Cost = TTIImpl->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg,
352 Scale, AddrSpace);
353 assert(Cost >= 0 && "TTI should not produce negative costs!");
354 return Cost;
Quentin Colombetbf490d42013-05-31 21:29:03 +0000355}
356
Jonas Paulsson024e3192017-07-21 11:59:37 +0000357bool TargetTransformInfo::LSRWithInstrQueries() const {
358 return TTIImpl->LSRWithInstrQueries();
359}
360
Chandler Carruth539edf42013-01-05 11:43:11 +0000361bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000362 return TTIImpl->isTruncateFree(Ty1, Ty2);
Chandler Carruth539edf42013-01-05 11:43:11 +0000363}
364
Chad Rosier54390052015-02-23 19:15:16 +0000365bool TargetTransformInfo::isProfitableToHoist(Instruction *I) const {
366 return TTIImpl->isProfitableToHoist(I);
367}
368
David Blaikie8ad9a972018-03-28 22:28:50 +0000369bool TargetTransformInfo::useAA() const { return TTIImpl->useAA(); }
370
Chandler Carruth539edf42013-01-05 11:43:11 +0000371bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000372 return TTIImpl->isTypeLegal(Ty);
Chandler Carruth539edf42013-01-05 11:43:11 +0000373}
374
Chandler Carruth539edf42013-01-05 11:43:11 +0000375bool TargetTransformInfo::shouldBuildLookupTables() const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000376 return TTIImpl->shouldBuildLookupTables();
Chandler Carruth539edf42013-01-05 11:43:11 +0000377}
Oliver Stannard4df1cc02016-10-07 08:48:24 +0000378bool TargetTransformInfo::shouldBuildLookupTablesForConstant(Constant *C) const {
379 return TTIImpl->shouldBuildLookupTablesForConstant(C);
380}
Chandler Carruth539edf42013-01-05 11:43:11 +0000381
Zaara Syeda1f59ae32018-01-30 16:17:22 +0000382bool TargetTransformInfo::useColdCCForColdCall(Function &F) const {
383 return TTIImpl->useColdCCForColdCall(F);
384}
385
Jonas Paulsson8e2f9482017-01-26 07:03:25 +0000386unsigned TargetTransformInfo::
387getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const {
388 return TTIImpl->getScalarizationOverhead(Ty, Insert, Extract);
389}
390
391unsigned TargetTransformInfo::
392getOperandsScalarizationOverhead(ArrayRef<const Value *> Args,
393 unsigned VF) const {
394 return TTIImpl->getOperandsScalarizationOverhead(Args, VF);
395}
396
Jonas Paulssonda74ed42017-04-12 12:41:37 +0000397bool TargetTransformInfo::supportsEfficientVectorElementLoadStore() const {
398 return TTIImpl->supportsEfficientVectorElementLoadStore();
399}
400
Olivier Sallenave049d8032015-03-06 23:12:04 +0000401bool TargetTransformInfo::enableAggressiveInterleaving(bool LoopHasReductions) const {
402 return TTIImpl->enableAggressiveInterleaving(LoopHasReductions);
403}
404
Clement Courbet3bc5ad52019-06-25 08:04:13 +0000405TargetTransformInfo::MemCmpExpansionOptions
406TargetTransformInfo::enableMemCmpExpansion(bool OptSize, bool IsZeroCmp) const {
407 return TTIImpl->enableMemCmpExpansion(OptSize, IsZeroCmp);
Zaara Syeda3a7578c2017-05-31 17:12:38 +0000408}
409
Silviu Baranga61bdc512015-08-10 14:50:54 +0000410bool TargetTransformInfo::enableInterleavedAccessVectorization() const {
411 return TTIImpl->enableInterleavedAccessVectorization();
412}
413
Dorit Nuzman38bbf812018-10-14 08:50:06 +0000414bool TargetTransformInfo::enableMaskedInterleavedAccessVectorization() const {
415 return TTIImpl->enableMaskedInterleavedAccessVectorization();
416}
417
Renato Golin5cb666a2016-04-14 20:42:18 +0000418bool TargetTransformInfo::isFPVectorizationPotentiallyUnsafe() const {
419 return TTIImpl->isFPVectorizationPotentiallyUnsafe();
420}
421
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000422bool TargetTransformInfo::allowsMisalignedMemoryAccesses(LLVMContext &Context,
423 unsigned BitWidth,
Alina Sbirlea327955e2016-07-11 20:46:17 +0000424 unsigned AddressSpace,
425 unsigned Alignment,
426 bool *Fast) const {
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000427 return TTIImpl->allowsMisalignedMemoryAccesses(Context, BitWidth, AddressSpace,
Alina Sbirlea327955e2016-07-11 20:46:17 +0000428 Alignment, Fast);
429}
430
Chandler Carruth50a36cd2013-01-07 03:16:03 +0000431TargetTransformInfo::PopcntSupportKind
432TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000433 return TTIImpl->getPopcntSupport(IntTyWidthInBit);
Chandler Carruth539edf42013-01-05 11:43:11 +0000434}
435
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000436bool TargetTransformInfo::haveFastSqrt(Type *Ty) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000437 return TTIImpl->haveFastSqrt(Ty);
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000438}
439
Sanjay Patel0de1a4b2017-11-27 21:15:43 +0000440bool TargetTransformInfo::isFCmpOrdCheaperThanFCmpZero(Type *Ty) const {
441 return TTIImpl->isFCmpOrdCheaperThanFCmpZero(Ty);
442}
443
Chandler Carruth93205eb2015-08-05 18:08:10 +0000444int TargetTransformInfo::getFPOpCost(Type *Ty) const {
445 int Cost = TTIImpl->getFPOpCost(Ty);
446 assert(Cost >= 0 && "TTI should not produce negative costs!");
447 return Cost;
Cameron Esfahani17177d12015-02-05 02:09:33 +0000448}
449
Sjoerd Meijer38c2cd02016-07-14 07:44:20 +0000450int TargetTransformInfo::getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx,
451 const APInt &Imm,
452 Type *Ty) const {
453 int Cost = TTIImpl->getIntImmCodeSizeCost(Opcode, Idx, Imm, Ty);
454 assert(Cost >= 0 && "TTI should not produce negative costs!");
455 return Cost;
456}
457
Chandler Carruth93205eb2015-08-05 18:08:10 +0000458int TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
459 int Cost = TTIImpl->getIntImmCost(Imm, Ty);
460 assert(Cost >= 0 && "TTI should not produce negative costs!");
461 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000462}
463
Chandler Carruth93205eb2015-08-05 18:08:10 +0000464int TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx,
465 const APInt &Imm, Type *Ty) const {
466 int Cost = TTIImpl->getIntImmCost(Opcode, 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
Chandler Carruth93205eb2015-08-05 18:08:10 +0000471int TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
472 const APInt &Imm, Type *Ty) const {
473 int Cost = TTIImpl->getIntImmCost(IID, Idx, Imm, Ty);
474 assert(Cost >= 0 && "TTI should not produce negative costs!");
475 return Cost;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000476}
477
Zi Xuan Wu98022682019-10-12 02:53:04 +0000478unsigned TargetTransformInfo::getNumberOfRegisters(unsigned ClassID) const {
479 return TTIImpl->getNumberOfRegisters(ClassID);
480}
481
482unsigned TargetTransformInfo::getRegisterClassForType(bool Vector, Type *Ty) const {
483 return TTIImpl->getRegisterClassForType(Vector, Ty);
484}
485
486const char* TargetTransformInfo::getRegisterClassName(unsigned ClassID) const {
487 return TTIImpl->getRegisterClassName(ClassID);
Chandler Carruth539edf42013-01-05 11:43:11 +0000488}
489
Nadav Rotemb1791a72013-01-09 22:29:00 +0000490unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000491 return TTIImpl->getRegisterBitWidth(Vector);
Nadav Rotemb1791a72013-01-09 22:29:00 +0000492}
493
Adam Nemete29686e2017-05-15 21:15:01 +0000494unsigned TargetTransformInfo::getMinVectorRegisterBitWidth() const {
495 return TTIImpl->getMinVectorRegisterBitWidth();
496}
497
Krzysztof Parzyszek5d93fdf2018-03-27 16:14:11 +0000498bool TargetTransformInfo::shouldMaximizeVectorBandwidth(bool OptSize) const {
499 return TTIImpl->shouldMaximizeVectorBandwidth(OptSize);
500}
501
Krzysztof Parzyszekdfed9412018-04-13 20:16:32 +0000502unsigned TargetTransformInfo::getMinimumVF(unsigned ElemWidth) const {
503 return TTIImpl->getMinimumVF(ElemWidth);
504}
505
Jun Bum Limdee55652017-04-03 19:20:07 +0000506bool TargetTransformInfo::shouldConsiderAddressTypePromotion(
507 const Instruction &I, bool &AllowPromotionWithoutCommonHeader) const {
508 return TTIImpl->shouldConsiderAddressTypePromotion(
509 I, AllowPromotionWithoutCommonHeader);
510}
511
Adam Nemetaf761102016-01-21 18:28:36 +0000512unsigned TargetTransformInfo::getCacheLineSize() const {
513 return TTIImpl->getCacheLineSize();
514}
515
Tobias Grosserd7eb6192017-08-24 09:46:25 +0000516llvm::Optional<unsigned> TargetTransformInfo::getCacheSize(CacheLevel Level)
517 const {
518 return TTIImpl->getCacheSize(Level);
519}
520
521llvm::Optional<unsigned> TargetTransformInfo::getCacheAssociativity(
522 CacheLevel Level) const {
523 return TTIImpl->getCacheAssociativity(Level);
524}
525
Adam Nemetdadfbb52016-01-27 22:21:25 +0000526unsigned TargetTransformInfo::getPrefetchDistance() const {
527 return TTIImpl->getPrefetchDistance();
528}
529
Adam Nemet6d8beec2016-03-18 00:27:38 +0000530unsigned TargetTransformInfo::getMinPrefetchStride() const {
531 return TTIImpl->getMinPrefetchStride();
532}
533
Adam Nemet709e3042016-03-18 00:27:43 +0000534unsigned TargetTransformInfo::getMaxPrefetchIterationsAhead() const {
535 return TTIImpl->getMaxPrefetchIterationsAhead();
536}
537
Wei Mi062c7442015-05-06 17:12:25 +0000538unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const {
539 return TTIImpl->getMaxInterleaveFactor(VF);
Nadav Rotemb696c362013-01-09 01:15:42 +0000540}
541
Jonas Paulsson29d80f02018-10-05 14:34:04 +0000542TargetTransformInfo::OperandValueKind
Simon Pilgrim077a42c2018-11-13 13:45:10 +0000543TargetTransformInfo::getOperandInfo(Value *V, OperandValueProperties &OpProps) {
Jonas Paulsson29d80f02018-10-05 14:34:04 +0000544 OperandValueKind OpInfo = OK_AnyValue;
545 OpProps = OP_None;
546
547 if (auto *CI = dyn_cast<ConstantInt>(V)) {
548 if (CI->getValue().isPowerOf2())
549 OpProps = OP_PowerOf2;
550 return OK_UniformConstantValue;
551 }
552
Simon Pilgrim2b166c52018-11-14 15:04:08 +0000553 // A broadcast shuffle creates a uniform value.
554 // TODO: Add support for non-zero index broadcasts.
555 // TODO: Add support for different source vector width.
556 if (auto *ShuffleInst = dyn_cast<ShuffleVectorInst>(V))
557 if (ShuffleInst->isZeroEltSplat())
558 OpInfo = OK_UniformValue;
559
Jonas Paulsson29d80f02018-10-05 14:34:04 +0000560 const Value *Splat = getSplatValue(V);
561
562 // Check for a splat of a constant or for a non uniform vector of constants
563 // and check if the constant(s) are all powers of two.
564 if (isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) {
565 OpInfo = OK_NonUniformConstantValue;
566 if (Splat) {
567 OpInfo = OK_UniformConstantValue;
568 if (auto *CI = dyn_cast<ConstantInt>(Splat))
569 if (CI->getValue().isPowerOf2())
570 OpProps = OP_PowerOf2;
571 } else if (auto *CDS = dyn_cast<ConstantDataSequential>(V)) {
572 OpProps = OP_PowerOf2;
573 for (unsigned I = 0, E = CDS->getNumElements(); I != E; ++I) {
574 if (auto *CI = dyn_cast<ConstantInt>(CDS->getElementAsConstant(I)))
575 if (CI->getValue().isPowerOf2())
576 continue;
577 OpProps = OP_None;
578 break;
579 }
580 }
581 }
582
583 // Check for a splat of a uniform value. This is not loop aware, so return
584 // true only for the obviously uniform cases (argument, globalvalue)
585 if (Splat && (isa<Argument>(Splat) || isa<GlobalValue>(Splat)))
586 OpInfo = OK_UniformValue;
587
588 return OpInfo;
589}
590
Chandler Carruth93205eb2015-08-05 18:08:10 +0000591int TargetTransformInfo::getArithmeticInstrCost(
Chandler Carruth705b1852015-01-31 03:43:40 +0000592 unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
593 OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000594 OperandValueProperties Opd2PropInfo,
595 ArrayRef<const Value *> Args) const {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000596 int Cost = TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000597 Opd1PropInfo, Opd2PropInfo, Args);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000598 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::getShuffleCost(ShuffleKind Kind, Type *Ty, int Index,
603 Type *SubTp) const {
604 int Cost = TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
605 assert(Cost >= 0 && "TTI should not produce negative costs!");
606 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000607}
608
Chandler Carruth93205eb2015-08-05 18:08:10 +0000609int TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000610 Type *Src, const Instruction *I) const {
611 assert ((I == nullptr || I->getOpcode() == Opcode) &&
612 "Opcode should reflect passed instruction.");
613 int Cost = TTIImpl->getCastInstrCost(Opcode, Dst, Src, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000614 assert(Cost >= 0 && "TTI should not produce negative costs!");
615 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000616}
617
Matthew Simpsone5dfb082016-04-27 15:20:21 +0000618int TargetTransformInfo::getExtractWithExtendCost(unsigned Opcode, Type *Dst,
619 VectorType *VecTy,
620 unsigned Index) const {
621 int Cost = TTIImpl->getExtractWithExtendCost(Opcode, Dst, VecTy, Index);
622 assert(Cost >= 0 && "TTI should not produce negative costs!");
623 return Cost;
624}
625
Chandler Carruth93205eb2015-08-05 18:08:10 +0000626int TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
627 int Cost = TTIImpl->getCFInstrCost(Opcode);
628 assert(Cost >= 0 && "TTI should not produce negative costs!");
629 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000630}
631
Chandler Carruth93205eb2015-08-05 18:08:10 +0000632int TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000633 Type *CondTy, const Instruction *I) const {
634 assert ((I == nullptr || I->getOpcode() == Opcode) &&
635 "Opcode should reflect passed instruction.");
636 int Cost = TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000637 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::getVectorInstrCost(unsigned Opcode, Type *Val,
642 unsigned Index) const {
643 int Cost = TTIImpl->getVectorInstrCost(Opcode, Val, Index);
644 assert(Cost >= 0 && "TTI should not produce negative costs!");
645 return Cost;
Chandler Carruth539edf42013-01-05 11:43:11 +0000646}
647
Chandler Carruth93205eb2015-08-05 18:08:10 +0000648int TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
Guillaume Chateleta4783ef2019-10-22 17:16:52 +0200649 MaybeAlign Alignment,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000650 unsigned AddressSpace,
651 const Instruction *I) const {
652 assert ((I == nullptr || I->getOpcode() == Opcode) &&
653 "Opcode should reflect passed instruction.");
654 int Cost = TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace, I);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000655 assert(Cost >= 0 && "TTI should not produce negative costs!");
656 return Cost;
Elena Demikhovskya3232f72015-01-25 08:44:46 +0000657}
658
Chandler Carruth93205eb2015-08-05 18:08:10 +0000659int TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
660 unsigned Alignment,
661 unsigned AddressSpace) const {
662 int Cost =
663 TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
664 assert(Cost >= 0 && "TTI should not produce negative costs!");
665 return Cost;
Chandler Carruth705b1852015-01-31 03:43:40 +0000666}
667
Elena Demikhovsky54946982015-12-28 20:10:59 +0000668int TargetTransformInfo::getGatherScatterOpCost(unsigned Opcode, Type *DataTy,
669 Value *Ptr, bool VariableMask,
670 unsigned Alignment) const {
671 int Cost = TTIImpl->getGatherScatterOpCost(Opcode, DataTy, Ptr, VariableMask,
672 Alignment);
673 assert(Cost >= 0 && "TTI should not produce negative costs!");
674 return Cost;
675}
676
Chandler Carruth93205eb2015-08-05 18:08:10 +0000677int TargetTransformInfo::getInterleavedMemoryOpCost(
Hao Liu32c05392015-06-08 06:39:56 +0000678 unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
Dorit Nuzman34da6dd2018-10-31 09:57:56 +0000679 unsigned Alignment, unsigned AddressSpace, bool UseMaskForCond,
680 bool UseMaskForGaps) const {
681 int Cost = TTIImpl->getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
682 Alignment, AddressSpace,
683 UseMaskForCond,
684 UseMaskForGaps);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000685 assert(Cost >= 0 && "TTI should not produce negative costs!");
686 return Cost;
Hao Liu32c05392015-06-08 06:39:56 +0000687}
688
Chandler Carruth93205eb2015-08-05 18:08:10 +0000689int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
Jonas Paulssona48ea232017-03-14 06:35:36 +0000690 ArrayRef<Type *> Tys, FastMathFlags FMF,
691 unsigned ScalarizationCostPassed) const {
692 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys, FMF,
693 ScalarizationCostPassed);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000694 assert(Cost >= 0 && "TTI should not produce negative costs!");
695 return Cost;
696}
697
Elena Demikhovsky54946982015-12-28 20:10:59 +0000698int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
Jonas Paulssona48ea232017-03-14 06:35:36 +0000699 ArrayRef<Value *> Args, FastMathFlags FMF, unsigned VF) const {
700 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Args, FMF, VF);
Elena Demikhovsky54946982015-12-28 20:10:59 +0000701 assert(Cost >= 0 && "TTI should not produce negative costs!");
702 return Cost;
703}
704
Chandler Carruth93205eb2015-08-05 18:08:10 +0000705int TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy,
706 ArrayRef<Type *> Tys) const {
707 int Cost = TTIImpl->getCallInstrCost(F, RetTy, Tys);
708 assert(Cost >= 0 && "TTI should not produce negative costs!");
709 return Cost;
Michael Zolotukhin7ed84a82015-03-17 19:26:23 +0000710}
711
Chandler Carruth539edf42013-01-05 11:43:11 +0000712unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000713 return TTIImpl->getNumberOfParts(Tp);
Chandler Carruth539edf42013-01-05 11:43:11 +0000714}
715
Chandler Carruth93205eb2015-08-05 18:08:10 +0000716int TargetTransformInfo::getAddressComputationCost(Type *Tp,
Mohammed Agabaria23599ba2017-01-05 14:03:41 +0000717 ScalarEvolution *SE,
718 const SCEV *Ptr) const {
719 int Cost = TTIImpl->getAddressComputationCost(Tp, SE, Ptr);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000720 assert(Cost >= 0 && "TTI should not produce negative costs!");
721 return Cost;
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000722}
Chandler Carruth539edf42013-01-05 11:43:11 +0000723
Sjoerd Meijerea31ddb2019-04-30 10:28:50 +0000724int TargetTransformInfo::getMemcpyCost(const Instruction *I) const {
725 int Cost = TTIImpl->getMemcpyCost(I);
726 assert(Cost >= 0 && "TTI should not produce negative costs!");
727 return Cost;
728}
729
Alexey Bataev3e9b3eb2017-07-31 14:19:32 +0000730int TargetTransformInfo::getArithmeticReductionCost(unsigned Opcode, Type *Ty,
731 bool IsPairwiseForm) const {
732 int Cost = TTIImpl->getArithmeticReductionCost(Opcode, Ty, IsPairwiseForm);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000733 assert(Cost >= 0 && "TTI should not produce negative costs!");
734 return Cost;
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000735}
736
Alexey Bataev6dd29fc2017-09-08 13:49:36 +0000737int TargetTransformInfo::getMinMaxReductionCost(Type *Ty, Type *CondTy,
738 bool IsPairwiseForm,
739 bool IsUnsigned) const {
740 int Cost =
741 TTIImpl->getMinMaxReductionCost(Ty, CondTy, IsPairwiseForm, IsUnsigned);
742 assert(Cost >= 0 && "TTI should not produce negative costs!");
743 return Cost;
744}
745
Chandler Carruth705b1852015-01-31 03:43:40 +0000746unsigned
747TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
748 return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
Chad Rosierf9327d62015-01-26 22:51:15 +0000749}
750
751bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst,
752 MemIntrinsicInfo &Info) const {
Chandler Carruth705b1852015-01-31 03:43:40 +0000753 return TTIImpl->getTgtMemIntrinsic(Inst, Info);
Chad Rosierf9327d62015-01-26 22:51:15 +0000754}
755
Anna Thomasb2a212c2017-06-06 16:45:25 +0000756unsigned TargetTransformInfo::getAtomicMemIntrinsicMaxElementSize() const {
757 return TTIImpl->getAtomicMemIntrinsicMaxElementSize();
758}
759
Chandler Carruth705b1852015-01-31 03:43:40 +0000760Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic(
761 IntrinsicInst *Inst, Type *ExpectedType) const {
762 return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
763}
764
Sean Fertile9cd1cdf2017-07-07 02:00:06 +0000765Type *TargetTransformInfo::getMemcpyLoopLoweringType(LLVMContext &Context,
766 Value *Length,
767 unsigned SrcAlign,
768 unsigned DestAlign) const {
769 return TTIImpl->getMemcpyLoopLoweringType(Context, Length, SrcAlign,
770 DestAlign);
771}
772
773void TargetTransformInfo::getMemcpyLoopResidualLoweringType(
774 SmallVectorImpl<Type *> &OpsOut, LLVMContext &Context,
775 unsigned RemainingBytes, unsigned SrcAlign, unsigned DestAlign) const {
776 TTIImpl->getMemcpyLoopResidualLoweringType(OpsOut, Context, RemainingBytes,
777 SrcAlign, DestAlign);
778}
779
Eric Christopherd566fb12015-07-29 22:09:48 +0000780bool TargetTransformInfo::areInlineCompatible(const Function *Caller,
781 const Function *Callee) const {
782 return TTIImpl->areInlineCompatible(Caller, Callee);
Eric Christopher4371b132015-07-02 01:11:47 +0000783}
784
Tom Stellard3d36e5c2019-01-16 05:15:31 +0000785bool TargetTransformInfo::areFunctionArgsABICompatible(
786 const Function *Caller, const Function *Callee,
787 SmallPtrSetImpl<Argument *> &Args) const {
788 return TTIImpl->areFunctionArgsABICompatible(Caller, Callee, Args);
789}
790
Krzysztof Parzyszek0b377e02018-03-26 13:10:09 +0000791bool TargetTransformInfo::isIndexedLoadLegal(MemIndexedMode Mode,
792 Type *Ty) const {
793 return TTIImpl->isIndexedLoadLegal(Mode, Ty);
794}
795
796bool TargetTransformInfo::isIndexedStoreLegal(MemIndexedMode Mode,
797 Type *Ty) const {
798 return TTIImpl->isIndexedStoreLegal(Mode, Ty);
799}
800
Volkan Keles1c386812016-10-03 10:31:34 +0000801unsigned TargetTransformInfo::getLoadStoreVecRegBitWidth(unsigned AS) const {
802 return TTIImpl->getLoadStoreVecRegBitWidth(AS);
803}
804
805bool TargetTransformInfo::isLegalToVectorizeLoad(LoadInst *LI) const {
806 return TTIImpl->isLegalToVectorizeLoad(LI);
807}
808
809bool TargetTransformInfo::isLegalToVectorizeStore(StoreInst *SI) const {
810 return TTIImpl->isLegalToVectorizeStore(SI);
811}
812
813bool TargetTransformInfo::isLegalToVectorizeLoadChain(
814 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
815 return TTIImpl->isLegalToVectorizeLoadChain(ChainSizeInBytes, Alignment,
816 AddrSpace);
817}
818
819bool TargetTransformInfo::isLegalToVectorizeStoreChain(
820 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
821 return TTIImpl->isLegalToVectorizeStoreChain(ChainSizeInBytes, Alignment,
822 AddrSpace);
823}
824
825unsigned TargetTransformInfo::getLoadVectorFactor(unsigned VF,
826 unsigned LoadSize,
827 unsigned ChainSizeInBytes,
828 VectorType *VecTy) const {
829 return TTIImpl->getLoadVectorFactor(VF, LoadSize, ChainSizeInBytes, VecTy);
830}
831
832unsigned TargetTransformInfo::getStoreVectorFactor(unsigned VF,
833 unsigned StoreSize,
834 unsigned ChainSizeInBytes,
835 VectorType *VecTy) const {
836 return TTIImpl->getStoreVectorFactor(VF, StoreSize, ChainSizeInBytes, VecTy);
837}
838
Amara Emersoncf9daa32017-05-09 10:43:25 +0000839bool TargetTransformInfo::useReductionIntrinsic(unsigned Opcode,
840 Type *Ty, ReductionFlags Flags) const {
841 return TTIImpl->useReductionIntrinsic(Opcode, Ty, Flags);
842}
843
Amara Emerson836b0f42017-05-10 09:42:49 +0000844bool TargetTransformInfo::shouldExpandReduction(const IntrinsicInst *II) const {
845 return TTIImpl->shouldExpandReduction(II);
846}
Amara Emersoncf9daa32017-05-09 10:43:25 +0000847
Amara Emerson14688222019-06-17 23:20:29 +0000848unsigned TargetTransformInfo::getGISelRematGlobalCost() const {
849 return TTIImpl->getGISelRematGlobalCost();
850}
851
Guozhi Wei62d64142017-09-08 22:29:17 +0000852int TargetTransformInfo::getInstructionLatency(const Instruction *I) const {
853 return TTIImpl->getInstructionLatency(I);
854}
855
Guozhi Wei62d64142017-09-08 22:29:17 +0000856static bool matchPairwiseShuffleMask(ShuffleVectorInst *SI, bool IsLeft,
857 unsigned Level) {
858 // We don't need a shuffle if we just want to have element 0 in position 0 of
859 // the vector.
860 if (!SI && Level == 0 && IsLeft)
861 return true;
862 else if (!SI)
863 return false;
864
865 SmallVector<int, 32> Mask(SI->getType()->getVectorNumElements(), -1);
866
867 // Build a mask of 0, 2, ... (left) or 1, 3, ... (right) depending on whether
868 // we look at the left or right side.
869 for (unsigned i = 0, e = (1 << Level), val = !IsLeft; i != e; ++i, val += 2)
870 Mask[i] = val;
871
872 SmallVector<int, 16> ActualMask = SI->getShuffleMask();
873 return Mask == ActualMask;
874}
875
876namespace {
877/// Kind of the reduction data.
878enum ReductionKind {
879 RK_None, /// Not a reduction.
880 RK_Arithmetic, /// Binary reduction data.
881 RK_MinMax, /// Min/max reduction data.
882 RK_UnsignedMinMax, /// Unsigned min/max reduction data.
883};
884/// Contains opcode + LHS/RHS parts of the reduction operations.
885struct ReductionData {
886 ReductionData() = delete;
887 ReductionData(ReductionKind Kind, unsigned Opcode, Value *LHS, Value *RHS)
888 : Opcode(Opcode), LHS(LHS), RHS(RHS), Kind(Kind) {
889 assert(Kind != RK_None && "expected binary or min/max reduction only.");
890 }
891 unsigned Opcode = 0;
892 Value *LHS = nullptr;
893 Value *RHS = nullptr;
894 ReductionKind Kind = RK_None;
895 bool hasSameData(ReductionData &RD) const {
896 return Kind == RD.Kind && Opcode == RD.Opcode;
897 }
898};
899} // namespace
900
901static Optional<ReductionData> getReductionData(Instruction *I) {
902 Value *L, *R;
903 if (m_BinOp(m_Value(L), m_Value(R)).match(I))
Fangrui Songf78650a2018-07-30 19:41:25 +0000904 return ReductionData(RK_Arithmetic, I->getOpcode(), L, R);
Guozhi Wei62d64142017-09-08 22:29:17 +0000905 if (auto *SI = dyn_cast<SelectInst>(I)) {
906 if (m_SMin(m_Value(L), m_Value(R)).match(SI) ||
907 m_SMax(m_Value(L), m_Value(R)).match(SI) ||
908 m_OrdFMin(m_Value(L), m_Value(R)).match(SI) ||
909 m_OrdFMax(m_Value(L), m_Value(R)).match(SI) ||
910 m_UnordFMin(m_Value(L), m_Value(R)).match(SI) ||
911 m_UnordFMax(m_Value(L), m_Value(R)).match(SI)) {
912 auto *CI = cast<CmpInst>(SI->getCondition());
Fangrui Songf78650a2018-07-30 19:41:25 +0000913 return ReductionData(RK_MinMax, CI->getOpcode(), L, R);
914 }
Guozhi Wei62d64142017-09-08 22:29:17 +0000915 if (m_UMin(m_Value(L), m_Value(R)).match(SI) ||
916 m_UMax(m_Value(L), m_Value(R)).match(SI)) {
917 auto *CI = cast<CmpInst>(SI->getCondition());
918 return ReductionData(RK_UnsignedMinMax, CI->getOpcode(), L, R);
919 }
920 }
921 return llvm::None;
922}
923
924static ReductionKind matchPairwiseReductionAtLevel(Instruction *I,
925 unsigned Level,
926 unsigned NumLevels) {
927 // Match one level of pairwise operations.
928 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef,
929 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef>
930 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef,
931 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef>
932 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1
933 if (!I)
934 return RK_None;
935
936 assert(I->getType()->isVectorTy() && "Expecting a vector type");
937
938 Optional<ReductionData> RD = getReductionData(I);
939 if (!RD)
940 return RK_None;
941
942 ShuffleVectorInst *LS = dyn_cast<ShuffleVectorInst>(RD->LHS);
943 if (!LS && Level)
944 return RK_None;
945 ShuffleVectorInst *RS = dyn_cast<ShuffleVectorInst>(RD->RHS);
946 if (!RS && Level)
947 return RK_None;
948
949 // On level 0 we can omit one shufflevector instruction.
950 if (!Level && !RS && !LS)
951 return RK_None;
952
953 // Shuffle inputs must match.
954 Value *NextLevelOpL = LS ? LS->getOperand(0) : nullptr;
955 Value *NextLevelOpR = RS ? RS->getOperand(0) : nullptr;
956 Value *NextLevelOp = nullptr;
957 if (NextLevelOpR && NextLevelOpL) {
958 // If we have two shuffles their operands must match.
959 if (NextLevelOpL != NextLevelOpR)
960 return RK_None;
961
962 NextLevelOp = NextLevelOpL;
963 } else if (Level == 0 && (NextLevelOpR || NextLevelOpL)) {
964 // On the first level we can omit the shufflevector <0, undef,...>. So the
965 // input to the other shufflevector <1, undef> must match with one of the
966 // inputs to the current binary operation.
967 // Example:
968 // %NextLevelOpL = shufflevector %R, <1, undef ...>
969 // %BinOp = fadd %NextLevelOpL, %R
970 if (NextLevelOpL && NextLevelOpL != RD->RHS)
971 return RK_None;
972 else if (NextLevelOpR && NextLevelOpR != RD->LHS)
973 return RK_None;
974
975 NextLevelOp = NextLevelOpL ? RD->RHS : RD->LHS;
976 } else
977 return RK_None;
978
979 // Check that the next levels binary operation exists and matches with the
980 // current one.
981 if (Level + 1 != NumLevels) {
982 Optional<ReductionData> NextLevelRD =
983 getReductionData(cast<Instruction>(NextLevelOp));
984 if (!NextLevelRD || !RD->hasSameData(*NextLevelRD))
985 return RK_None;
986 }
987
988 // Shuffle mask for pairwise operation must match.
989 if (matchPairwiseShuffleMask(LS, /*IsLeft=*/true, Level)) {
990 if (!matchPairwiseShuffleMask(RS, /*IsLeft=*/false, Level))
991 return RK_None;
992 } else if (matchPairwiseShuffleMask(RS, /*IsLeft=*/true, Level)) {
993 if (!matchPairwiseShuffleMask(LS, /*IsLeft=*/false, Level))
994 return RK_None;
995 } else {
996 return RK_None;
997 }
998
999 if (++Level == NumLevels)
1000 return RD->Kind;
1001
1002 // Match next level.
1003 return matchPairwiseReductionAtLevel(cast<Instruction>(NextLevelOp), Level,
1004 NumLevels);
1005}
1006
1007static ReductionKind matchPairwiseReduction(const ExtractElementInst *ReduxRoot,
1008 unsigned &Opcode, Type *&Ty) {
1009 if (!EnableReduxCost)
1010 return RK_None;
1011
1012 // Need to extract the first element.
1013 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1));
1014 unsigned Idx = ~0u;
1015 if (CI)
1016 Idx = CI->getZExtValue();
1017 if (Idx != 0)
1018 return RK_None;
1019
1020 auto *RdxStart = dyn_cast<Instruction>(ReduxRoot->getOperand(0));
1021 if (!RdxStart)
1022 return RK_None;
1023 Optional<ReductionData> RD = getReductionData(RdxStart);
1024 if (!RD)
1025 return RK_None;
1026
1027 Type *VecTy = RdxStart->getType();
1028 unsigned NumVecElems = VecTy->getVectorNumElements();
1029 if (!isPowerOf2_32(NumVecElems))
1030 return RK_None;
1031
1032 // We look for a sequence of shuffle,shuffle,add triples like the following
1033 // that builds a pairwise reduction tree.
Fangrui Songf78650a2018-07-30 19:41:25 +00001034 //
Guozhi Wei62d64142017-09-08 22:29:17 +00001035 // (X0, X1, X2, X3)
1036 // (X0 + X1, X2 + X3, undef, undef)
1037 // ((X0 + X1) + (X2 + X3), undef, undef, undef)
Fangrui Songf78650a2018-07-30 19:41:25 +00001038 //
Guozhi Wei62d64142017-09-08 22:29:17 +00001039 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef,
1040 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef>
1041 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef,
1042 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef>
1043 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1
1044 // %rdx.shuf.1.0 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef,
1045 // <4 x i32> <i32 0, i32 undef, i32 undef, i32 undef>
1046 // %rdx.shuf.1.1 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef,
1047 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
1048 // %bin.rdx8 = fadd <4 x float> %rdx.shuf.1.0, %rdx.shuf.1.1
1049 // %r = extractelement <4 x float> %bin.rdx8, i32 0
1050 if (matchPairwiseReductionAtLevel(RdxStart, 0, Log2_32(NumVecElems)) ==
1051 RK_None)
1052 return RK_None;
1053
1054 Opcode = RD->Opcode;
1055 Ty = VecTy;
1056
1057 return RD->Kind;
1058}
1059
1060static std::pair<Value *, ShuffleVectorInst *>
1061getShuffleAndOtherOprd(Value *L, Value *R) {
1062 ShuffleVectorInst *S = nullptr;
1063
1064 if ((S = dyn_cast<ShuffleVectorInst>(L)))
1065 return std::make_pair(R, S);
1066
1067 S = dyn_cast<ShuffleVectorInst>(R);
1068 return std::make_pair(L, S);
1069}
1070
1071static ReductionKind
1072matchVectorSplittingReduction(const ExtractElementInst *ReduxRoot,
1073 unsigned &Opcode, Type *&Ty) {
1074 if (!EnableReduxCost)
1075 return RK_None;
1076
1077 // Need to extract the first element.
1078 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1));
1079 unsigned Idx = ~0u;
1080 if (CI)
1081 Idx = CI->getZExtValue();
1082 if (Idx != 0)
1083 return RK_None;
1084
1085 auto *RdxStart = dyn_cast<Instruction>(ReduxRoot->getOperand(0));
1086 if (!RdxStart)
1087 return RK_None;
1088 Optional<ReductionData> RD = getReductionData(RdxStart);
1089 if (!RD)
1090 return RK_None;
1091
1092 Type *VecTy = ReduxRoot->getOperand(0)->getType();
1093 unsigned NumVecElems = VecTy->getVectorNumElements();
1094 if (!isPowerOf2_32(NumVecElems))
1095 return RK_None;
1096
1097 // We look for a sequence of shuffles and adds like the following matching one
1098 // fadd, shuffle vector pair at a time.
Fangrui Songf78650a2018-07-30 19:41:25 +00001099 //
Guozhi Wei62d64142017-09-08 22:29:17 +00001100 // %rdx.shuf = shufflevector <4 x float> %rdx, <4 x float> undef,
1101 // <4 x i32> <i32 2, i32 3, i32 undef, i32 undef>
1102 // %bin.rdx = fadd <4 x float> %rdx, %rdx.shuf
1103 // %rdx.shuf7 = shufflevector <4 x float> %bin.rdx, <4 x float> undef,
1104 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
1105 // %bin.rdx8 = fadd <4 x float> %bin.rdx, %rdx.shuf7
1106 // %r = extractelement <4 x float> %bin.rdx8, i32 0
1107
1108 unsigned MaskStart = 1;
1109 Instruction *RdxOp = RdxStart;
Fangrui Songf78650a2018-07-30 19:41:25 +00001110 SmallVector<int, 32> ShuffleMask(NumVecElems, 0);
Guozhi Wei62d64142017-09-08 22:29:17 +00001111 unsigned NumVecElemsRemain = NumVecElems;
1112 while (NumVecElemsRemain - 1) {
1113 // Check for the right reduction operation.
1114 if (!RdxOp)
1115 return RK_None;
1116 Optional<ReductionData> RDLevel = getReductionData(RdxOp);
1117 if (!RDLevel || !RDLevel->hasSameData(*RD))
1118 return RK_None;
1119
1120 Value *NextRdxOp;
1121 ShuffleVectorInst *Shuffle;
1122 std::tie(NextRdxOp, Shuffle) =
1123 getShuffleAndOtherOprd(RDLevel->LHS, RDLevel->RHS);
1124
1125 // Check the current reduction operation and the shuffle use the same value.
1126 if (Shuffle == nullptr)
1127 return RK_None;
1128 if (Shuffle->getOperand(0) != NextRdxOp)
1129 return RK_None;
1130
1131 // Check that shuffle masks matches.
1132 for (unsigned j = 0; j != MaskStart; ++j)
1133 ShuffleMask[j] = MaskStart + j;
1134 // Fill the rest of the mask with -1 for undef.
1135 std::fill(&ShuffleMask[MaskStart], ShuffleMask.end(), -1);
1136
1137 SmallVector<int, 16> Mask = Shuffle->getShuffleMask();
1138 if (ShuffleMask != Mask)
1139 return RK_None;
1140
1141 RdxOp = dyn_cast<Instruction>(NextRdxOp);
1142 NumVecElemsRemain /= 2;
1143 MaskStart *= 2;
1144 }
1145
1146 Opcode = RD->Opcode;
1147 Ty = VecTy;
1148 return RD->Kind;
1149}
1150
1151int TargetTransformInfo::getInstructionThroughput(const Instruction *I) const {
1152 switch (I->getOpcode()) {
1153 case Instruction::GetElementPtr:
1154 return getUserCost(I);
1155
1156 case Instruction::Ret:
1157 case Instruction::PHI:
1158 case Instruction::Br: {
1159 return getCFInstrCost(I->getOpcode());
1160 }
1161 case Instruction::Add:
1162 case Instruction::FAdd:
1163 case Instruction::Sub:
1164 case Instruction::FSub:
1165 case Instruction::Mul:
1166 case Instruction::FMul:
1167 case Instruction::UDiv:
1168 case Instruction::SDiv:
1169 case Instruction::FDiv:
1170 case Instruction::URem:
1171 case Instruction::SRem:
1172 case Instruction::FRem:
1173 case Instruction::Shl:
1174 case Instruction::LShr:
1175 case Instruction::AShr:
1176 case Instruction::And:
1177 case Instruction::Or:
1178 case Instruction::Xor: {
Simon Pilgrim4162d772018-05-22 10:40:09 +00001179 TargetTransformInfo::OperandValueKind Op1VK, Op2VK;
1180 TargetTransformInfo::OperandValueProperties Op1VP, Op2VP;
1181 Op1VK = getOperandInfo(I->getOperand(0), Op1VP);
1182 Op2VK = getOperandInfo(I->getOperand(1), Op2VP);
1183 SmallVector<const Value *, 2> Operands(I->operand_values());
1184 return getArithmeticInstrCost(I->getOpcode(), I->getType(), Op1VK, Op2VK,
1185 Op1VP, Op2VP, Operands);
Guozhi Wei62d64142017-09-08 22:29:17 +00001186 }
Craig Topper50d50282019-05-28 04:09:18 +00001187 case Instruction::FNeg: {
1188 TargetTransformInfo::OperandValueKind Op1VK, Op2VK;
1189 TargetTransformInfo::OperandValueProperties Op1VP, Op2VP;
1190 Op1VK = getOperandInfo(I->getOperand(0), Op1VP);
1191 Op2VK = OK_AnyValue;
1192 Op2VP = OP_None;
1193 SmallVector<const Value *, 2> Operands(I->operand_values());
1194 return getArithmeticInstrCost(I->getOpcode(), I->getType(), Op1VK, Op2VK,
1195 Op1VP, Op2VP, Operands);
1196 }
Guozhi Wei62d64142017-09-08 22:29:17 +00001197 case Instruction::Select: {
1198 const SelectInst *SI = cast<SelectInst>(I);
1199 Type *CondTy = SI->getCondition()->getType();
1200 return getCmpSelInstrCost(I->getOpcode(), I->getType(), CondTy, I);
1201 }
1202 case Instruction::ICmp:
1203 case Instruction::FCmp: {
1204 Type *ValTy = I->getOperand(0)->getType();
1205 return getCmpSelInstrCost(I->getOpcode(), ValTy, I->getType(), I);
1206 }
1207 case Instruction::Store: {
1208 const StoreInst *SI = cast<StoreInst>(I);
1209 Type *ValTy = SI->getValueOperand()->getType();
1210 return getMemoryOpCost(I->getOpcode(), ValTy,
Guillaume Chateleta4783ef2019-10-22 17:16:52 +02001211 MaybeAlign(SI->getAlignment()),
1212 SI->getPointerAddressSpace(), I);
Guozhi Wei62d64142017-09-08 22:29:17 +00001213 }
1214 case Instruction::Load: {
1215 const LoadInst *LI = cast<LoadInst>(I);
1216 return getMemoryOpCost(I->getOpcode(), I->getType(),
Guillaume Chateleta4783ef2019-10-22 17:16:52 +02001217 MaybeAlign(LI->getAlignment()),
1218 LI->getPointerAddressSpace(), I);
Guozhi Wei62d64142017-09-08 22:29:17 +00001219 }
1220 case Instruction::ZExt:
1221 case Instruction::SExt:
1222 case Instruction::FPToUI:
1223 case Instruction::FPToSI:
1224 case Instruction::FPExt:
1225 case Instruction::PtrToInt:
1226 case Instruction::IntToPtr:
1227 case Instruction::SIToFP:
1228 case Instruction::UIToFP:
1229 case Instruction::Trunc:
1230 case Instruction::FPTrunc:
1231 case Instruction::BitCast:
1232 case Instruction::AddrSpaceCast: {
1233 Type *SrcTy = I->getOperand(0)->getType();
1234 return getCastInstrCost(I->getOpcode(), I->getType(), SrcTy, I);
1235 }
1236 case Instruction::ExtractElement: {
1237 const ExtractElementInst * EEI = cast<ExtractElementInst>(I);
1238 ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1));
1239 unsigned Idx = -1;
1240 if (CI)
1241 Idx = CI->getZExtValue();
1242
1243 // Try to match a reduction sequence (series of shufflevector and vector
1244 // adds followed by a extractelement).
1245 unsigned ReduxOpCode;
1246 Type *ReduxType;
1247
1248 switch (matchVectorSplittingReduction(EEI, ReduxOpCode, ReduxType)) {
1249 case RK_Arithmetic:
1250 return getArithmeticReductionCost(ReduxOpCode, ReduxType,
1251 /*IsPairwiseForm=*/false);
1252 case RK_MinMax:
1253 return getMinMaxReductionCost(
1254 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1255 /*IsPairwiseForm=*/false, /*IsUnsigned=*/false);
1256 case RK_UnsignedMinMax:
1257 return getMinMaxReductionCost(
1258 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1259 /*IsPairwiseForm=*/false, /*IsUnsigned=*/true);
1260 case RK_None:
1261 break;
1262 }
1263
1264 switch (matchPairwiseReduction(EEI, ReduxOpCode, ReduxType)) {
1265 case RK_Arithmetic:
1266 return getArithmeticReductionCost(ReduxOpCode, ReduxType,
1267 /*IsPairwiseForm=*/true);
1268 case RK_MinMax:
1269 return getMinMaxReductionCost(
1270 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1271 /*IsPairwiseForm=*/true, /*IsUnsigned=*/false);
1272 case RK_UnsignedMinMax:
1273 return getMinMaxReductionCost(
1274 ReduxType, CmpInst::makeCmpResultType(ReduxType),
1275 /*IsPairwiseForm=*/true, /*IsUnsigned=*/true);
1276 case RK_None:
1277 break;
1278 }
1279
1280 return getVectorInstrCost(I->getOpcode(),
1281 EEI->getOperand(0)->getType(), Idx);
1282 }
1283 case Instruction::InsertElement: {
1284 const InsertElementInst * IE = cast<InsertElementInst>(I);
1285 ConstantInt *CI = dyn_cast<ConstantInt>(IE->getOperand(2));
Fangrui Songf78650a2018-07-30 19:41:25 +00001286 unsigned Idx = -1;
Guozhi Wei62d64142017-09-08 22:29:17 +00001287 if (CI)
1288 Idx = CI->getZExtValue();
1289 return getVectorInstrCost(I->getOpcode(),
1290 IE->getType(), Idx);
1291 }
Roman Lebedevcc95a452019-08-29 11:50:30 +00001292 case Instruction::ExtractValue:
1293 return 0; // Model all ExtractValue nodes as free.
Guozhi Wei62d64142017-09-08 22:29:17 +00001294 case Instruction::ShuffleVector: {
1295 const ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
Simon Pilgrimd0c71602018-11-09 16:28:19 +00001296 Type *Ty = Shuffle->getType();
1297 Type *SrcTy = Shuffle->getOperand(0)->getType();
1298
1299 // TODO: Identify and add costs for insert subvector, etc.
1300 int SubIndex;
1301 if (Shuffle->isExtractSubvectorMask(SubIndex))
Simon Pilgrim26e1c882018-11-09 18:30:59 +00001302 return TTIImpl->getShuffleCost(SK_ExtractSubvector, SrcTy, SubIndex, Ty);
Simon Pilgrimd0c71602018-11-09 16:28:19 +00001303
Sanjay Patel2ca33602018-06-19 18:44:00 +00001304 if (Shuffle->changesLength())
1305 return -1;
Fangrui Songf78650a2018-07-30 19:41:25 +00001306
Sanjay Patel2ca33602018-06-19 18:44:00 +00001307 if (Shuffle->isIdentity())
1308 return 0;
Guozhi Wei62d64142017-09-08 22:29:17 +00001309
Sanjay Patel2ca33602018-06-19 18:44:00 +00001310 if (Shuffle->isReverse())
1311 return TTIImpl->getShuffleCost(SK_Reverse, Ty, 0, nullptr);
Simon Pilgrim07839212018-06-12 14:47:13 +00001312
Sanjay Patel2ca33602018-06-19 18:44:00 +00001313 if (Shuffle->isSelect())
1314 return TTIImpl->getShuffleCost(SK_Select, Ty, 0, nullptr);
Simon Pilgrim07839212018-06-12 14:47:13 +00001315
Sanjay Patel2ca33602018-06-19 18:44:00 +00001316 if (Shuffle->isTranspose())
1317 return TTIImpl->getShuffleCost(SK_Transpose, Ty, 0, nullptr);
Matthew Simpsonb4096eb2018-04-26 13:48:33 +00001318
Sanjay Patel2ca33602018-06-19 18:44:00 +00001319 if (Shuffle->isZeroEltSplat())
1320 return TTIImpl->getShuffleCost(SK_Broadcast, Ty, 0, nullptr);
Guozhi Wei62d64142017-09-08 22:29:17 +00001321
Sanjay Patel2ca33602018-06-19 18:44:00 +00001322 if (Shuffle->isSingleSource())
1323 return TTIImpl->getShuffleCost(SK_PermuteSingleSrc, Ty, 0, nullptr);
Guozhi Wei62d64142017-09-08 22:29:17 +00001324
Sanjay Patel2ca33602018-06-19 18:44:00 +00001325 return TTIImpl->getShuffleCost(SK_PermuteTwoSrc, Ty, 0, nullptr);
Guozhi Wei62d64142017-09-08 22:29:17 +00001326 }
1327 case Instruction::Call:
1328 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1329 SmallVector<Value *, 4> Args(II->arg_operands());
1330
1331 FastMathFlags FMF;
1332 if (auto *FPMO = dyn_cast<FPMathOperator>(II))
1333 FMF = FPMO->getFastMathFlags();
1334
1335 return getIntrinsicInstrCost(II->getIntrinsicID(), II->getType(),
1336 Args, FMF);
1337 }
1338 return -1;
1339 default:
1340 // We don't have any information on this instruction.
1341 return -1;
1342 }
1343}
1344
Chandler Carruth705b1852015-01-31 03:43:40 +00001345TargetTransformInfo::Concept::~Concept() {}
1346
Chandler Carruthe0385522015-02-01 10:11:22 +00001347TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
1348
1349TargetIRAnalysis::TargetIRAnalysis(
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001350 std::function<Result(const Function &)> TTICallback)
Benjamin Kramer82de7d32016-05-27 14:27:24 +00001351 : TTICallback(std::move(TTICallback)) {}
Chandler Carruthe0385522015-02-01 10:11:22 +00001352
Chandler Carruth164a2aa62016-06-17 00:11:01 +00001353TargetIRAnalysis::Result TargetIRAnalysis::run(const Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +00001354 FunctionAnalysisManager &) {
Chandler Carruthe0385522015-02-01 10:11:22 +00001355 return TTICallback(F);
1356}
1357
Chandler Carruthdab4eae2016-11-23 17:53:26 +00001358AnalysisKey TargetIRAnalysis::Key;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +00001359
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001360TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(const Function &F) {
Mehdi Amini5010ebf2015-07-09 02:08:42 +00001361 return Result(F.getParent()->getDataLayout());
Chandler Carruthe0385522015-02-01 10:11:22 +00001362}
1363
Chandler Carruth705b1852015-01-31 03:43:40 +00001364// Register the basic pass.
1365INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
1366 "Target Transform Information", false, true)
1367char TargetTransformInfoWrapperPass::ID = 0;
Chandler Carruth539edf42013-01-05 11:43:11 +00001368
Chandler Carruth705b1852015-01-31 03:43:40 +00001369void TargetTransformInfoWrapperPass::anchor() {}
Chandler Carruth539edf42013-01-05 11:43:11 +00001370
Chandler Carruth705b1852015-01-31 03:43:40 +00001371TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass()
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001372 : ImmutablePass(ID) {
Chandler Carruth705b1852015-01-31 03:43:40 +00001373 initializeTargetTransformInfoWrapperPassPass(
1374 *PassRegistry::getPassRegistry());
1375}
1376
1377TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass(
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001378 TargetIRAnalysis TIRA)
1379 : ImmutablePass(ID), TIRA(std::move(TIRA)) {
Chandler Carruth705b1852015-01-31 03:43:40 +00001380 initializeTargetTransformInfoWrapperPassPass(
1381 *PassRegistry::getPassRegistry());
1382}
1383
Eric Christophera4e5d3c2015-09-16 23:38:13 +00001384TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(const Function &F) {
Sean Silva36e0d012016-08-09 00:28:15 +00001385 FunctionAnalysisManager DummyFAM;
Chandler Carruth164a2aa62016-06-17 00:11:01 +00001386 TTI = TIRA.run(F, DummyFAM);
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001387 return *TTI;
1388}
1389
Chandler Carruth93dcdc42015-01-31 11:17:59 +00001390ImmutablePass *
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +00001391llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) {
1392 return new TargetTransformInfoWrapperPass(std::move(TIRA));
Chandler Carruth539edf42013-01-05 11:43:11 +00001393}