blob: 6185355f419f4250aa8dbf7f570bd81c6e413826 [file] [log] [blame]
Chandler Carruth93dcdc42015-01-31 11:17:59 +00001//===-- PPCTargetTransformInfo.cpp - PPC specific TTI ---------------------===//
Hal Finkel4e5ca9e2013-01-25 23:05:59 +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
Hal Finkel4e5ca9e2013-01-25 23:05:59 +00006//
7//===----------------------------------------------------------------------===//
Hal Finkel4e5ca9e2013-01-25 23:05:59 +00008
Chandler Carruth93dcdc42015-01-31 11:17:59 +00009#include "PPCTargetTransformInfo.h"
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000010#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruth705b1852015-01-31 03:43:40 +000011#include "llvm/CodeGen/BasicTTIImpl.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000012#include "llvm/CodeGen/CostTable.h"
13#include "llvm/CodeGen/TargetLowering.h"
Hal Finkel0192cba2014-04-13 23:02:40 +000014#include "llvm/Support/CommandLine.h"
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000015#include "llvm/Support/Debug.h"
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000016using namespace llvm;
17
Chandler Carruth84e68b22014-04-22 02:41:26 +000018#define DEBUG_TYPE "ppctti"
19
Hal Finkel0192cba2014-04-13 23:02:40 +000020static cl::opt<bool> DisablePPCConstHoist("disable-ppc-constant-hoisting",
21cl::desc("disable constant hoisting on PPC"), cl::init(false), cl::Hidden);
22
Adam Nemetaf761102016-01-21 18:28:36 +000023// This is currently only used for the data prefetch pass which is only enabled
24// for BG/Q by default.
25static cl::opt<unsigned>
26CacheLineSize("ppc-loop-prefetch-cache-line", cl::Hidden, cl::init(64),
27 cl::desc("The loop prefetch cache line size"));
28
Zaara Syeda1f59ae32018-01-30 16:17:22 +000029static cl::opt<bool>
30EnablePPCColdCC("ppc-enable-coldcc", cl::Hidden, cl::init(false),
31 cl::desc("Enable using coldcc calling conv for cold "
32 "internal functions"));
33
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000034//===----------------------------------------------------------------------===//
35//
36// PPC cost model.
37//
38//===----------------------------------------------------------------------===//
39
Chandler Carruth705b1852015-01-31 03:43:40 +000040TargetTransformInfo::PopcntSupportKind
41PPCTTIImpl::getPopcntSupport(unsigned TyWidth) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000042 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
Hal Finkelfa7057a2016-03-29 01:36:01 +000043 if (ST->hasPOPCNTD() != PPCSubtarget::POPCNTD_Unavailable && TyWidth <= 64)
44 return ST->hasPOPCNTD() == PPCSubtarget::POPCNTD_Slow ?
45 TTI::PSK_SlowHardware : TTI::PSK_FastHardware;
Chandler Carruth705b1852015-01-31 03:43:40 +000046 return TTI::PSK_Software;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000047}
48
Chandler Carruth93205eb2015-08-05 18:08:10 +000049int PPCTTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) {
Hal Finkel0192cba2014-04-13 23:02:40 +000050 if (DisablePPCConstHoist)
Chandler Carruth705b1852015-01-31 03:43:40 +000051 return BaseT::getIntImmCost(Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +000052
53 assert(Ty->isIntegerTy());
54
55 unsigned BitSize = Ty->getPrimitiveSizeInBits();
56 if (BitSize == 0)
57 return ~0U;
58
59 if (Imm == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +000060 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +000061
62 if (Imm.getBitWidth() <= 64) {
63 if (isInt<16>(Imm.getSExtValue()))
Chandler Carruth705b1852015-01-31 03:43:40 +000064 return TTI::TCC_Basic;
Hal Finkel0192cba2014-04-13 23:02:40 +000065
66 if (isInt<32>(Imm.getSExtValue())) {
67 // A constant that can be materialized using lis.
68 if ((Imm.getZExtValue() & 0xFFFF) == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +000069 return TTI::TCC_Basic;
Hal Finkel0192cba2014-04-13 23:02:40 +000070
Chandler Carruth705b1852015-01-31 03:43:40 +000071 return 2 * TTI::TCC_Basic;
Hal Finkel0192cba2014-04-13 23:02:40 +000072 }
73 }
74
Chandler Carruth705b1852015-01-31 03:43:40 +000075 return 4 * TTI::TCC_Basic;
Hal Finkel0192cba2014-04-13 23:02:40 +000076}
77
Chandler Carruth93205eb2015-08-05 18:08:10 +000078int PPCTTIImpl::getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
79 Type *Ty) {
Hal Finkel0192cba2014-04-13 23:02:40 +000080 if (DisablePPCConstHoist)
Chandler Carruth705b1852015-01-31 03:43:40 +000081 return BaseT::getIntImmCost(IID, Idx, Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +000082
83 assert(Ty->isIntegerTy());
84
85 unsigned BitSize = Ty->getPrimitiveSizeInBits();
86 if (BitSize == 0)
87 return ~0U;
88
89 switch (IID) {
Chandler Carruth705b1852015-01-31 03:43:40 +000090 default:
91 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +000092 case Intrinsic::sadd_with_overflow:
93 case Intrinsic::uadd_with_overflow:
94 case Intrinsic::ssub_with_overflow:
95 case Intrinsic::usub_with_overflow:
96 if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<16>(Imm.getSExtValue()))
Chandler Carruth705b1852015-01-31 03:43:40 +000097 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +000098 break;
Hal Finkel934361a2015-01-14 01:07:51 +000099 case Intrinsic::experimental_stackmap:
100 if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +0000101 return TTI::TCC_Free;
Hal Finkel934361a2015-01-14 01:07:51 +0000102 break;
103 case Intrinsic::experimental_patchpoint_void:
104 case Intrinsic::experimental_patchpoint_i64:
105 if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +0000106 return TTI::TCC_Free;
Hal Finkel934361a2015-01-14 01:07:51 +0000107 break;
Hal Finkel0192cba2014-04-13 23:02:40 +0000108 }
Chandler Carruth705b1852015-01-31 03:43:40 +0000109 return PPCTTIImpl::getIntImmCost(Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +0000110}
111
Chandler Carruth93205eb2015-08-05 18:08:10 +0000112int PPCTTIImpl::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
113 Type *Ty) {
Hal Finkel0192cba2014-04-13 23:02:40 +0000114 if (DisablePPCConstHoist)
Chandler Carruth705b1852015-01-31 03:43:40 +0000115 return BaseT::getIntImmCost(Opcode, Idx, Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +0000116
117 assert(Ty->isIntegerTy());
118
119 unsigned BitSize = Ty->getPrimitiveSizeInBits();
120 if (BitSize == 0)
121 return ~0U;
122
123 unsigned ImmIdx = ~0U;
124 bool ShiftedFree = false, RunFree = false, UnsignedFree = false,
125 ZeroFree = false;
126 switch (Opcode) {
Chandler Carruth705b1852015-01-31 03:43:40 +0000127 default:
128 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000129 case Instruction::GetElementPtr:
130 // Always hoist the base address of a GetElementPtr. This prevents the
131 // creation of new constants for every base constant that gets constant
132 // folded with the offset.
133 if (Idx == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +0000134 return 2 * TTI::TCC_Basic;
135 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000136 case Instruction::And:
137 RunFree = true; // (for the rotate-and-mask instructions)
Justin Bognerb03fd122016-08-17 05:10:15 +0000138 LLVM_FALLTHROUGH;
Hal Finkel0192cba2014-04-13 23:02:40 +0000139 case Instruction::Add:
140 case Instruction::Or:
141 case Instruction::Xor:
142 ShiftedFree = true;
Justin Bognerb03fd122016-08-17 05:10:15 +0000143 LLVM_FALLTHROUGH;
Hal Finkel0192cba2014-04-13 23:02:40 +0000144 case Instruction::Sub:
145 case Instruction::Mul:
146 case Instruction::Shl:
147 case Instruction::LShr:
148 case Instruction::AShr:
149 ImmIdx = 1;
150 break;
151 case Instruction::ICmp:
152 UnsignedFree = true;
153 ImmIdx = 1;
Justin Bognerb03fd122016-08-17 05:10:15 +0000154 // Zero comparisons can use record-form instructions.
155 LLVM_FALLTHROUGH;
Hal Finkel0192cba2014-04-13 23:02:40 +0000156 case Instruction::Select:
157 ZeroFree = true;
158 break;
159 case Instruction::PHI:
160 case Instruction::Call:
161 case Instruction::Ret:
162 case Instruction::Load:
163 case Instruction::Store:
164 break;
165 }
166
167 if (ZeroFree && Imm == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +0000168 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000169
170 if (Idx == ImmIdx && Imm.getBitWidth() <= 64) {
171 if (isInt<16>(Imm.getSExtValue()))
Chandler Carruth705b1852015-01-31 03:43:40 +0000172 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000173
174 if (RunFree) {
175 if (Imm.getBitWidth() <= 32 &&
176 (isShiftedMask_32(Imm.getZExtValue()) ||
177 isShiftedMask_32(~Imm.getZExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +0000178 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000179
180 if (ST->isPPC64() &&
181 (isShiftedMask_64(Imm.getZExtValue()) ||
182 isShiftedMask_64(~Imm.getZExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +0000183 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000184 }
185
186 if (UnsignedFree && isUInt<16>(Imm.getZExtValue()))
Chandler Carruth705b1852015-01-31 03:43:40 +0000187 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000188
189 if (ShiftedFree && (Imm.getZExtValue() & 0xFFFF) == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +0000190 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000191 }
192
Chandler Carruth705b1852015-01-31 03:43:40 +0000193 return PPCTTIImpl::getIntImmCost(Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +0000194}
195
Graham Yiu488782e2017-10-19 18:16:31 +0000196unsigned PPCTTIImpl::getUserCost(const User *U,
197 ArrayRef<const Value *> Operands) {
198 if (U->getType()->isVectorTy()) {
199 // Instructions that need to be split should cost more.
200 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, U->getType());
201 return LT.first * BaseT::getUserCost(U, Operands);
202 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000203
Graham Yiu488782e2017-10-19 18:16:31 +0000204 return BaseT::getUserCost(U, Operands);
205}
206
Geoff Berry66d9bdb2017-06-28 15:53:17 +0000207void PPCTTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
Chandler Carruth705b1852015-01-31 03:43:40 +0000208 TTI::UnrollingPreferences &UP) {
Chandler Carruthc956ab662015-02-01 14:22:17 +0000209 if (ST->getDarwinDirective() == PPC::DIR_A2) {
Hal Finkel71780ec2013-09-11 21:20:40 +0000210 // The A2 is in-order with a deep pipeline, and concatenation unrolling
211 // helps expose latency-hiding opportunities to the instruction scheduler.
212 UP.Partial = UP.Runtime = true;
Hal Finkel3b3c9c32015-05-21 20:30:23 +0000213
214 // We unroll a lot on the A2 (hundreds of instructions), and the benefits
215 // often outweigh the cost of a division to compute the trip count.
216 UP.AllowExpensiveTripCount = true;
Hal Finkel71780ec2013-09-11 21:20:40 +0000217 }
Hal Finkelb359b732015-01-09 15:51:16 +0000218
Geoff Berry66d9bdb2017-06-28 15:53:17 +0000219 BaseT::getUnrollingPreferences(L, SE, UP);
Hal Finkel71780ec2013-09-11 21:20:40 +0000220}
221
Zaara Syeda1f59ae32018-01-30 16:17:22 +0000222// This function returns true to allow using coldcc calling convention.
223// Returning true results in coldcc being used for functions which are cold at
224// all call sites when the callers of the functions are not calling any other
225// non coldcc functions.
226bool PPCTTIImpl::useColdCCForColdCall(Function &F) {
227 return EnablePPCColdCC;
228}
229
Olivier Sallenave049d8032015-03-06 23:12:04 +0000230bool PPCTTIImpl::enableAggressiveInterleaving(bool LoopHasReductions) {
Hal Finkel75afa2b2015-09-03 23:23:00 +0000231 // On the A2, always unroll aggressively. For QPX unaligned loads, we depend
232 // on combining the loads generated for consecutive accesses, and failure to
233 // do so is particularly expensive. This makes it much more likely (compared
234 // to only using concatenation unrolling).
235 if (ST->getDarwinDirective() == PPC::DIR_A2)
236 return true;
237
Olivier Sallenave049d8032015-03-06 23:12:04 +0000238 return LoopHasReductions;
239}
240
Clement Courbetb2c3eb82017-10-30 14:19:33 +0000241const PPCTTIImpl::TTI::MemCmpExpansionOptions *
242PPCTTIImpl::enableMemCmpExpansion(bool IsZeroCmp) const {
243 static const auto Options = []() {
244 TTI::MemCmpExpansionOptions Options;
245 Options.LoadSizes.push_back(8);
246 Options.LoadSizes.push_back(4);
247 Options.LoadSizes.push_back(2);
248 Options.LoadSizes.push_back(1);
249 return Options;
250 }();
251 return &Options;
Zaara Syeda3a7578c2017-05-31 17:12:38 +0000252}
253
Hal Finkel4a7be232015-09-04 00:10:41 +0000254bool PPCTTIImpl::enableInterleavedAccessVectorization() {
255 return true;
256}
257
Chandler Carruth705b1852015-01-31 03:43:40 +0000258unsigned PPCTTIImpl::getNumberOfRegisters(bool Vector) {
Hal Finkelc93a9a22015-02-25 01:06:45 +0000259 if (Vector && !ST->hasAltivec() && !ST->hasQPX())
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000260 return 0;
Hal Finkel27774d92014-03-13 07:58:58 +0000261 return ST->hasVSX() ? 64 : 32;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000262}
263
Daniel Neilsonc0112ae2017-06-12 14:22:21 +0000264unsigned PPCTTIImpl::getRegisterBitWidth(bool Vector) const {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000265 if (Vector) {
Hal Finkelc93a9a22015-02-25 01:06:45 +0000266 if (ST->hasQPX()) return 256;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000267 if (ST->hasAltivec()) return 128;
268 return 0;
269 }
270
271 if (ST->isPPC64())
272 return 64;
273 return 32;
274
275}
276
Adam Nemetaf761102016-01-21 18:28:36 +0000277unsigned PPCTTIImpl::getCacheLineSize() {
Sean Fertile457ddd32017-05-31 18:20:17 +0000278 // Check first if the user specified a custom line size.
279 if (CacheLineSize.getNumOccurrences() > 0)
280 return CacheLineSize;
281
282 // On P7, P8 or P9 we have a cache line size of 128.
283 unsigned Directive = ST->getDarwinDirective();
284 if (Directive == PPC::DIR_PWR7 || Directive == PPC::DIR_PWR8 ||
285 Directive == PPC::DIR_PWR9)
286 return 128;
287
288 // On other processors return a default of 64 bytes.
289 return 64;
Adam Nemetaf761102016-01-21 18:28:36 +0000290}
291
Adam Nemetb81f1e02016-03-29 23:45:56 +0000292unsigned PPCTTIImpl::getPrefetchDistance() {
293 // This seems like a reasonable default for the BG/Q (this pass is enabled, by
294 // default, only on the BG/Q).
295 return 300;
296}
Adam Nemetdadfbb52016-01-27 22:21:25 +0000297
Wei Mi062c7442015-05-06 17:12:25 +0000298unsigned PPCTTIImpl::getMaxInterleaveFactor(unsigned VF) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000299 unsigned Directive = ST->getDarwinDirective();
300 // The 440 has no SIMD support, but floating-point instructions
301 // have a 5-cycle latency, so unroll by 5x for latency hiding.
302 if (Directive == PPC::DIR_440)
303 return 5;
304
305 // The A2 has no SIMD support, but floating-point instructions
306 // have a 6-cycle latency, so unroll by 6x for latency hiding.
307 if (Directive == PPC::DIR_A2)
308 return 6;
309
310 // FIXME: For lack of any better information, do no harm...
311 if (Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500)
312 return 1;
313
Olivier Sallenave05e69152015-02-12 22:57:58 +0000314 // For P7 and P8, floating-point instructions have a 6-cycle latency and
315 // there are two execution units, so unroll by 12x for latency hiding.
Nemanja Ivanovic6e29baf2016-05-09 18:54:58 +0000316 // FIXME: the same for P9 as previous gen until POWER9 scheduling is ready
317 if (Directive == PPC::DIR_PWR7 || Directive == PPC::DIR_PWR8 ||
318 Directive == PPC::DIR_PWR9)
Olivier Sallenave05e69152015-02-12 22:57:58 +0000319 return 12;
320
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000321 // For most things, modern systems have two execution units (and
322 // out-of-order execution).
323 return 2;
324}
325
Nemanja Ivanovic7d007dd2019-01-26 01:18:48 +0000326// Adjust the cost of vector instructions on targets which there is overlap
327// between the vector and scalar units, thereby reducing the overall throughput
328// of vector code wrt. scalar code.
329int PPCTTIImpl::vectorCostAdjustment(int Cost, unsigned Opcode, Type *Ty1,
330 Type *Ty2) {
331 if (!ST->vectorsUseTwoUnits() || !Ty1->isVectorTy())
332 return Cost;
333
334 std::pair<int, MVT> LT1 = TLI->getTypeLegalizationCost(DL, Ty1);
335 // If type legalization involves splitting the vector, we don't want to
336 // double the cost at every step - only the last step.
337 if (LT1.first != 1 || !LT1.second.isVector())
338 return Cost;
Roland Froese7f291952019-02-01 18:55:43 +0000339
Nemanja Ivanovic7d007dd2019-01-26 01:18:48 +0000340 int ISD = TLI->InstructionOpcodeToISD(Opcode);
341 if (TLI->isOperationExpand(ISD, LT1.second))
342 return Cost;
343
344 if (Ty2) {
345 std::pair<int, MVT> LT2 = TLI->getTypeLegalizationCost(DL, Ty2);
346 if (LT2.first != 1 || !LT2.second.isVector())
347 return Cost;
348 }
349
350 return Cost * 2;
351}
352
Chandler Carruth93205eb2015-08-05 18:08:10 +0000353int PPCTTIImpl::getArithmeticInstrCost(
Chandler Carruth705b1852015-01-31 03:43:40 +0000354 unsigned Opcode, Type *Ty, TTI::OperandValueKind Op1Info,
355 TTI::OperandValueKind Op2Info, TTI::OperandValueProperties Opd1PropInfo,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000356 TTI::OperandValueProperties Opd2PropInfo, ArrayRef<const Value *> Args) {
Dmitri Gribenkoc451bdf2013-01-25 23:17:21 +0000357 assert(TLI->InstructionOpcodeToISD(Opcode) && "Invalid opcode");
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000358
359 // Fallback to the default implementation.
Nemanja Ivanovic7d007dd2019-01-26 01:18:48 +0000360 int Cost = BaseT::getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info,
361 Opd1PropInfo, Opd2PropInfo);
362 return vectorCostAdjustment(Cost, Opcode, Ty, nullptr);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000363}
364
Chandler Carruth93205eb2015-08-05 18:08:10 +0000365int PPCTTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index,
366 Type *SubTp) {
Hal Finkel4a7be232015-09-04 00:10:41 +0000367 // Legalize the type.
368 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp);
369
370 // PPC, for both Altivec/VSX and QPX, support cheap arbitrary permutations
371 // (at least in the sense that there need only be one non-loop-invariant
372 // instruction). We need one such shuffle instruction for each actual
373 // register (this is not true for arbitrary shuffles, but is true for the
374 // structured types of shuffles covered by TTI::ShuffleKind).
Nemanja Ivanovic7d007dd2019-01-26 01:18:48 +0000375 return vectorCostAdjustment(LT.first, Instruction::ShuffleVector, Tp,
376 nullptr);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000377}
378
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000379int PPCTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src,
380 const Instruction *I) {
Dmitri Gribenkoc451bdf2013-01-25 23:17:21 +0000381 assert(TLI->InstructionOpcodeToISD(Opcode) && "Invalid opcode");
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000382
Nemanja Ivanovic7d007dd2019-01-26 01:18:48 +0000383 int Cost = BaseT::getCastInstrCost(Opcode, Dst, Src);
384 return vectorCostAdjustment(Cost, Opcode, Dst, Src);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000385}
386
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000387int PPCTTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy,
388 const Instruction *I) {
Nemanja Ivanovic7d007dd2019-01-26 01:18:48 +0000389 int Cost = BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, I);
390 return vectorCostAdjustment(Cost, Opcode, ValTy, nullptr);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000391}
392
Chandler Carruth93205eb2015-08-05 18:08:10 +0000393int PPCTTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000394 assert(Val->isVectorTy() && "This must be a vector type");
395
Bill Schmidt62fe7a5b2013-02-08 18:19:17 +0000396 int ISD = TLI->InstructionOpcodeToISD(Opcode);
397 assert(ISD && "Invalid opcode");
Bill Schmidtb3cece12013-02-07 20:33:57 +0000398
Nemanja Ivanovic7d007dd2019-01-26 01:18:48 +0000399 int Cost = BaseT::getVectorInstrCost(Opcode, Val, Index);
400 Cost = vectorCostAdjustment(Cost, Opcode, Val, nullptr);
401
Hal Finkel27774d92014-03-13 07:58:58 +0000402 if (ST->hasVSX() && Val->getScalarType()->isDoubleTy()) {
Nemanja Ivanovic7d007dd2019-01-26 01:18:48 +0000403 // Double-precision scalars are already located in index #0 (or #1 if LE).
404 if (ISD == ISD::EXTRACT_VECTOR_ELT && Index == ST->isLittleEndian() ? 1 : 0)
Hal Finkel27774d92014-03-13 07:58:58 +0000405 return 0;
406
Nemanja Ivanovic7d007dd2019-01-26 01:18:48 +0000407 return Cost;
408
Hal Finkelc93a9a22015-02-25 01:06:45 +0000409 } else if (ST->hasQPX() && Val->getScalarType()->isFloatingPointTy()) {
410 // Floating point scalars are already located in index #0.
411 if (Index == 0)
412 return 0;
413
Nemanja Ivanovic7d007dd2019-01-26 01:18:48 +0000414 return Cost;
Hal Finkel27774d92014-03-13 07:58:58 +0000415 }
416
Bill Schmidt62fe7a5b2013-02-08 18:19:17 +0000417 // Estimated cost of a load-hit-store delay. This was obtained
418 // experimentally as a minimum needed to prevent unprofitable
419 // vectorization for the paq8p benchmark. It may need to be
420 // raised further if other unprofitable cases remain.
Hal Finkelde0b4132014-04-04 23:51:18 +0000421 unsigned LHSPenalty = 2;
422 if (ISD == ISD::INSERT_VECTOR_ELT)
423 LHSPenalty += 7;
Bill Schmidtb3cece12013-02-07 20:33:57 +0000424
Bill Schmidt62fe7a5b2013-02-08 18:19:17 +0000425 // Vector element insert/extract with Altivec is very expensive,
426 // because they require store and reload with the attendant
427 // processor stall for load-hit-store. Until VSX is available,
428 // these need to be estimated as very costly.
429 if (ISD == ISD::EXTRACT_VECTOR_ELT ||
430 ISD == ISD::INSERT_VECTOR_ELT)
Nemanja Ivanovic7d007dd2019-01-26 01:18:48 +0000431 return LHSPenalty + Cost;
Bill Schmidtb3cece12013-02-07 20:33:57 +0000432
Nemanja Ivanovic7d007dd2019-01-26 01:18:48 +0000433 return Cost;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000434}
435
Chandler Carruth93205eb2015-08-05 18:08:10 +0000436int PPCTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000437 unsigned AddressSpace, const Instruction *I) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000438 // Legalize the type.
Chandler Carruth93205eb2015-08-05 18:08:10 +0000439 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Src);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000440 assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
441 "Invalid Opcode");
442
Chandler Carruth93205eb2015-08-05 18:08:10 +0000443 int Cost = BaseT::getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
Nemanja Ivanovic7d007dd2019-01-26 01:18:48 +0000444 Cost = vectorCostAdjustment(Cost, Opcode, Src, nullptr);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000445
Hal Finkel79dbf5b2015-09-02 21:03:28 +0000446 bool IsAltivecType = ST->hasAltivec() &&
447 (LT.second == MVT::v16i8 || LT.second == MVT::v8i16 ||
448 LT.second == MVT::v4i32 || LT.second == MVT::v4f32);
449 bool IsVSXType = ST->hasVSX() &&
450 (LT.second == MVT::v2f64 || LT.second == MVT::v2i64);
451 bool IsQPXType = ST->hasQPX() &&
452 (LT.second == MVT::v4f64 || LT.second == MVT::v4f32);
453
Guozhi Wei835de1f2016-12-03 00:41:43 +0000454 // VSX has 32b/64b load instructions. Legalization can handle loading of
455 // 32b/64b to VSR correctly and cheaply. But BaseT::getMemoryOpCost and
456 // PPCTargetLowering can't compute the cost appropriately. So here we
457 // explicitly check this case.
458 unsigned MemBytes = Src->getPrimitiveSizeInBits();
459 if (Opcode == Instruction::Load && ST->hasVSX() && IsAltivecType &&
460 (MemBytes == 64 || (ST->hasP8Vector() && MemBytes == 32)))
461 return 1;
462
463 // Aligned loads and stores are easy.
464 unsigned SrcBytes = LT.second.getStoreSize();
465 if (!SrcBytes || !Alignment || Alignment >= SrcBytes)
466 return Cost;
467
Hal Finkelf11bc762015-09-03 21:23:18 +0000468 // If we can use the permutation-based load sequence, then this is also
469 // relatively cheap (not counting loop-invariant instructions): one load plus
470 // one permute (the last load in a series has extra cost, but we're
Hal Finkel69ada2f2016-03-28 22:39:35 +0000471 // neglecting that here). Note that on the P7, we could do unaligned loads
Hal Finkelf11bc762015-09-03 21:23:18 +0000472 // for Altivec types using the VSX instructions, but that's more expensive
473 // than using the permutation-based load sequence. On the P8, that's no
474 // longer true.
475 if (Opcode == Instruction::Load &&
476 ((!ST->hasP8Vector() && IsAltivecType) || IsQPXType) &&
477 Alignment >= LT.second.getScalarType().getStoreSize())
478 return Cost + LT.first; // Add the cost of the permutations.
479
Hal Finkel79dbf5b2015-09-02 21:03:28 +0000480 // For VSX, we can do unaligned loads and stores on Altivec/VSX types. On the
481 // P7, unaligned vector loads are more expensive than the permutation-based
482 // load sequence, so that might be used instead, but regardless, the net cost
483 // is about the same (not counting loop-invariant instructions).
484 if (IsVSXType || (ST->hasVSX() && IsAltivecType))
485 return Cost;
486
Guozhi Wei7ec2c722017-02-17 22:29:39 +0000487 // Newer PPC supports unaligned memory access.
488 if (TLI->allowsMisalignedMemoryAccesses(LT.second, 0))
489 return Cost;
490
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000491 // PPC in general does not support unaligned loads and stores. They'll need
492 // to be decomposed based on the alignment factor.
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000493
Hal Finkel79dbf5b2015-09-02 21:03:28 +0000494 // Add the cost of each scalar load or store.
495 Cost += LT.first*(SrcBytes/Alignment-1);
496
497 // For a vector type, there is also scalarization overhead (only for
498 // stores, loads are expanded using the vector-load + permutation sequence,
499 // which is much less expensive).
500 if (Src->isVectorTy() && Opcode == Instruction::Store)
501 for (int i = 0, e = Src->getVectorNumElements(); i < e; ++i)
502 Cost += getVectorInstrCost(Instruction::ExtractElement, Src, i);
Hal Finkelde0b4132014-04-04 23:51:18 +0000503
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000504 return Cost;
505}
506
Hal Finkel4a7be232015-09-04 00:10:41 +0000507int PPCTTIImpl::getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy,
508 unsigned Factor,
509 ArrayRef<unsigned> Indices,
510 unsigned Alignment,
Dorit Nuzman38bbf812018-10-14 08:50:06 +0000511 unsigned AddressSpace,
Dorit Nuzman34da6dd2018-10-31 09:57:56 +0000512 bool UseMaskForCond,
513 bool UseMaskForGaps) {
514 if (UseMaskForCond || UseMaskForGaps)
Dorit Nuzman38bbf812018-10-14 08:50:06 +0000515 return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
Dorit Nuzman34da6dd2018-10-31 09:57:56 +0000516 Alignment, AddressSpace,
517 UseMaskForCond, UseMaskForGaps);
Dorit Nuzman38bbf812018-10-14 08:50:06 +0000518
Hal Finkel4a7be232015-09-04 00:10:41 +0000519 assert(isa<VectorType>(VecTy) &&
520 "Expect a vector type for interleaved memory op");
521
522 // Legalize the type.
523 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, VecTy);
524
525 // Firstly, the cost of load/store operation.
526 int Cost = getMemoryOpCost(Opcode, VecTy, Alignment, AddressSpace);
527
528 // PPC, for both Altivec/VSX and QPX, support cheap arbitrary permutations
529 // (at least in the sense that there need only be one non-loop-invariant
530 // instruction). For each result vector, we need one shuffle per incoming
531 // vector (except that the first shuffle can take two incoming vectors
532 // because it does not need to take itself).
533 Cost += Factor*(LT.first-1);
534
535 return Cost;
536}
537