blob: 43b9892fc5a6bc80ebd156d9213d3bbea89bdc5f [file] [log] [blame]
Chandler Carruth93dcdc42015-01-31 11:17:59 +00001//===-- PPCTargetTransformInfo.cpp - PPC specific TTI ---------------------===//
Hal Finkel4e5ca9e2013-01-25 23:05:59 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Hal Finkel4e5ca9e2013-01-25 23:05:59 +00009
Chandler Carruth93dcdc42015-01-31 11:17:59 +000010#include "PPCTargetTransformInfo.h"
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000011#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruth705b1852015-01-31 03:43:40 +000012#include "llvm/CodeGen/BasicTTIImpl.h"
Hal Finkel0192cba2014-04-13 23:02:40 +000013#include "llvm/Support/CommandLine.h"
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000014#include "llvm/Support/Debug.h"
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000015#include "llvm/Target/CostTable.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "llvm/Target/TargetLowering.h"
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000017using namespace llvm;
18
Chandler Carruth84e68b22014-04-22 02:41:26 +000019#define DEBUG_TYPE "ppctti"
20
Hal Finkel0192cba2014-04-13 23:02:40 +000021static cl::opt<bool> DisablePPCConstHoist("disable-ppc-constant-hoisting",
22cl::desc("disable constant hoisting on PPC"), cl::init(false), cl::Hidden);
23
Adam Nemetaf761102016-01-21 18:28:36 +000024// This is currently only used for the data prefetch pass which is only enabled
25// for BG/Q by default.
26static cl::opt<unsigned>
27CacheLineSize("ppc-loop-prefetch-cache-line", cl::Hidden, cl::init(64),
28 cl::desc("The loop prefetch cache line size"));
29
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000030//===----------------------------------------------------------------------===//
31//
32// PPC cost model.
33//
34//===----------------------------------------------------------------------===//
35
Chandler Carruth705b1852015-01-31 03:43:40 +000036TargetTransformInfo::PopcntSupportKind
37PPCTTIImpl::getPopcntSupport(unsigned TyWidth) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000038 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
Hal Finkelfa7057a2016-03-29 01:36:01 +000039 if (ST->hasPOPCNTD() != PPCSubtarget::POPCNTD_Unavailable && TyWidth <= 64)
40 return ST->hasPOPCNTD() == PPCSubtarget::POPCNTD_Slow ?
41 TTI::PSK_SlowHardware : TTI::PSK_FastHardware;
Chandler Carruth705b1852015-01-31 03:43:40 +000042 return TTI::PSK_Software;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000043}
44
Chandler Carruth93205eb2015-08-05 18:08:10 +000045int PPCTTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) {
Hal Finkel0192cba2014-04-13 23:02:40 +000046 if (DisablePPCConstHoist)
Chandler Carruth705b1852015-01-31 03:43:40 +000047 return BaseT::getIntImmCost(Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +000048
49 assert(Ty->isIntegerTy());
50
51 unsigned BitSize = Ty->getPrimitiveSizeInBits();
52 if (BitSize == 0)
53 return ~0U;
54
55 if (Imm == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +000056 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +000057
58 if (Imm.getBitWidth() <= 64) {
59 if (isInt<16>(Imm.getSExtValue()))
Chandler Carruth705b1852015-01-31 03:43:40 +000060 return TTI::TCC_Basic;
Hal Finkel0192cba2014-04-13 23:02:40 +000061
62 if (isInt<32>(Imm.getSExtValue())) {
63 // A constant that can be materialized using lis.
64 if ((Imm.getZExtValue() & 0xFFFF) == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +000065 return TTI::TCC_Basic;
Hal Finkel0192cba2014-04-13 23:02:40 +000066
Chandler Carruth705b1852015-01-31 03:43:40 +000067 return 2 * TTI::TCC_Basic;
Hal Finkel0192cba2014-04-13 23:02:40 +000068 }
69 }
70
Chandler Carruth705b1852015-01-31 03:43:40 +000071 return 4 * TTI::TCC_Basic;
Hal Finkel0192cba2014-04-13 23:02:40 +000072}
73
Chandler Carruth93205eb2015-08-05 18:08:10 +000074int PPCTTIImpl::getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
75 Type *Ty) {
Hal Finkel0192cba2014-04-13 23:02:40 +000076 if (DisablePPCConstHoist)
Chandler Carruth705b1852015-01-31 03:43:40 +000077 return BaseT::getIntImmCost(IID, Idx, Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +000078
79 assert(Ty->isIntegerTy());
80
81 unsigned BitSize = Ty->getPrimitiveSizeInBits();
82 if (BitSize == 0)
83 return ~0U;
84
85 switch (IID) {
Chandler Carruth705b1852015-01-31 03:43:40 +000086 default:
87 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +000088 case Intrinsic::sadd_with_overflow:
89 case Intrinsic::uadd_with_overflow:
90 case Intrinsic::ssub_with_overflow:
91 case Intrinsic::usub_with_overflow:
92 if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<16>(Imm.getSExtValue()))
Chandler Carruth705b1852015-01-31 03:43:40 +000093 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +000094 break;
Hal Finkel934361a2015-01-14 01:07:51 +000095 case Intrinsic::experimental_stackmap:
96 if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +000097 return TTI::TCC_Free;
Hal Finkel934361a2015-01-14 01:07:51 +000098 break;
99 case Intrinsic::experimental_patchpoint_void:
100 case Intrinsic::experimental_patchpoint_i64:
101 if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +0000102 return TTI::TCC_Free;
Hal Finkel934361a2015-01-14 01:07:51 +0000103 break;
Hal Finkel0192cba2014-04-13 23:02:40 +0000104 }
Chandler Carruth705b1852015-01-31 03:43:40 +0000105 return PPCTTIImpl::getIntImmCost(Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +0000106}
107
Chandler Carruth93205eb2015-08-05 18:08:10 +0000108int PPCTTIImpl::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
109 Type *Ty) {
Hal Finkel0192cba2014-04-13 23:02:40 +0000110 if (DisablePPCConstHoist)
Chandler Carruth705b1852015-01-31 03:43:40 +0000111 return BaseT::getIntImmCost(Opcode, Idx, Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +0000112
113 assert(Ty->isIntegerTy());
114
115 unsigned BitSize = Ty->getPrimitiveSizeInBits();
116 if (BitSize == 0)
117 return ~0U;
118
119 unsigned ImmIdx = ~0U;
120 bool ShiftedFree = false, RunFree = false, UnsignedFree = false,
121 ZeroFree = false;
122 switch (Opcode) {
Chandler Carruth705b1852015-01-31 03:43:40 +0000123 default:
124 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000125 case Instruction::GetElementPtr:
126 // Always hoist the base address of a GetElementPtr. This prevents the
127 // creation of new constants for every base constant that gets constant
128 // folded with the offset.
129 if (Idx == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +0000130 return 2 * TTI::TCC_Basic;
131 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000132 case Instruction::And:
133 RunFree = true; // (for the rotate-and-mask instructions)
Justin Bognerb03fd122016-08-17 05:10:15 +0000134 LLVM_FALLTHROUGH;
Hal Finkel0192cba2014-04-13 23:02:40 +0000135 case Instruction::Add:
136 case Instruction::Or:
137 case Instruction::Xor:
138 ShiftedFree = true;
Justin Bognerb03fd122016-08-17 05:10:15 +0000139 LLVM_FALLTHROUGH;
Hal Finkel0192cba2014-04-13 23:02:40 +0000140 case Instruction::Sub:
141 case Instruction::Mul:
142 case Instruction::Shl:
143 case Instruction::LShr:
144 case Instruction::AShr:
145 ImmIdx = 1;
146 break;
147 case Instruction::ICmp:
148 UnsignedFree = true;
149 ImmIdx = 1;
Justin Bognerb03fd122016-08-17 05:10:15 +0000150 // Zero comparisons can use record-form instructions.
151 LLVM_FALLTHROUGH;
Hal Finkel0192cba2014-04-13 23:02:40 +0000152 case Instruction::Select:
153 ZeroFree = true;
154 break;
155 case Instruction::PHI:
156 case Instruction::Call:
157 case Instruction::Ret:
158 case Instruction::Load:
159 case Instruction::Store:
160 break;
161 }
162
163 if (ZeroFree && Imm == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +0000164 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000165
166 if (Idx == ImmIdx && Imm.getBitWidth() <= 64) {
167 if (isInt<16>(Imm.getSExtValue()))
Chandler Carruth705b1852015-01-31 03:43:40 +0000168 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000169
170 if (RunFree) {
171 if (Imm.getBitWidth() <= 32 &&
172 (isShiftedMask_32(Imm.getZExtValue()) ||
173 isShiftedMask_32(~Imm.getZExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +0000174 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000175
176 if (ST->isPPC64() &&
177 (isShiftedMask_64(Imm.getZExtValue()) ||
178 isShiftedMask_64(~Imm.getZExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +0000179 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000180 }
181
182 if (UnsignedFree && isUInt<16>(Imm.getZExtValue()))
Chandler Carruth705b1852015-01-31 03:43:40 +0000183 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000184
185 if (ShiftedFree && (Imm.getZExtValue() & 0xFFFF) == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +0000186 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000187 }
188
Chandler Carruth705b1852015-01-31 03:43:40 +0000189 return PPCTTIImpl::getIntImmCost(Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +0000190}
191
Graham Yiu488782e2017-10-19 18:16:31 +0000192unsigned PPCTTIImpl::getUserCost(const User *U,
193 ArrayRef<const Value *> Operands) {
194 if (U->getType()->isVectorTy()) {
195 // Instructions that need to be split should cost more.
196 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, U->getType());
197 return LT.first * BaseT::getUserCost(U, Operands);
198 }
199
200 return BaseT::getUserCost(U, Operands);
201}
202
Geoff Berry66d9bdb2017-06-28 15:53:17 +0000203void PPCTTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
Chandler Carruth705b1852015-01-31 03:43:40 +0000204 TTI::UnrollingPreferences &UP) {
Chandler Carruthc956ab662015-02-01 14:22:17 +0000205 if (ST->getDarwinDirective() == PPC::DIR_A2) {
Hal Finkel71780ec2013-09-11 21:20:40 +0000206 // The A2 is in-order with a deep pipeline, and concatenation unrolling
207 // helps expose latency-hiding opportunities to the instruction scheduler.
208 UP.Partial = UP.Runtime = true;
Hal Finkel3b3c9c32015-05-21 20:30:23 +0000209
210 // We unroll a lot on the A2 (hundreds of instructions), and the benefits
211 // often outweigh the cost of a division to compute the trip count.
212 UP.AllowExpensiveTripCount = true;
Hal Finkel71780ec2013-09-11 21:20:40 +0000213 }
Hal Finkelb359b732015-01-09 15:51:16 +0000214
Geoff Berry66d9bdb2017-06-28 15:53:17 +0000215 BaseT::getUnrollingPreferences(L, SE, UP);
Hal Finkel71780ec2013-09-11 21:20:40 +0000216}
217
Olivier Sallenave049d8032015-03-06 23:12:04 +0000218bool PPCTTIImpl::enableAggressiveInterleaving(bool LoopHasReductions) {
Hal Finkel75afa2b2015-09-03 23:23:00 +0000219 // On the A2, always unroll aggressively. For QPX unaligned loads, we depend
220 // on combining the loads generated for consecutive accesses, and failure to
221 // do so is particularly expensive. This makes it much more likely (compared
222 // to only using concatenation unrolling).
223 if (ST->getDarwinDirective() == PPC::DIR_A2)
224 return true;
225
Olivier Sallenave049d8032015-03-06 23:12:04 +0000226 return LoopHasReductions;
227}
228
Clement Courbetb2c3eb82017-10-30 14:19:33 +0000229const PPCTTIImpl::TTI::MemCmpExpansionOptions *
230PPCTTIImpl::enableMemCmpExpansion(bool IsZeroCmp) const {
231 static const auto Options = []() {
232 TTI::MemCmpExpansionOptions Options;
233 Options.LoadSizes.push_back(8);
234 Options.LoadSizes.push_back(4);
235 Options.LoadSizes.push_back(2);
236 Options.LoadSizes.push_back(1);
237 return Options;
238 }();
239 return &Options;
Zaara Syeda3a7578c2017-05-31 17:12:38 +0000240}
241
Hal Finkel4a7be232015-09-04 00:10:41 +0000242bool PPCTTIImpl::enableInterleavedAccessVectorization() {
243 return true;
244}
245
Chandler Carruth705b1852015-01-31 03:43:40 +0000246unsigned PPCTTIImpl::getNumberOfRegisters(bool Vector) {
Hal Finkelc93a9a22015-02-25 01:06:45 +0000247 if (Vector && !ST->hasAltivec() && !ST->hasQPX())
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000248 return 0;
Hal Finkel27774d92014-03-13 07:58:58 +0000249 return ST->hasVSX() ? 64 : 32;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000250}
251
Daniel Neilsonc0112ae2017-06-12 14:22:21 +0000252unsigned PPCTTIImpl::getRegisterBitWidth(bool Vector) const {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000253 if (Vector) {
Hal Finkelc93a9a22015-02-25 01:06:45 +0000254 if (ST->hasQPX()) return 256;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000255 if (ST->hasAltivec()) return 128;
256 return 0;
257 }
258
259 if (ST->isPPC64())
260 return 64;
261 return 32;
262
263}
264
Adam Nemetaf761102016-01-21 18:28:36 +0000265unsigned PPCTTIImpl::getCacheLineSize() {
Sean Fertile457ddd32017-05-31 18:20:17 +0000266 // Check first if the user specified a custom line size.
267 if (CacheLineSize.getNumOccurrences() > 0)
268 return CacheLineSize;
269
270 // On P7, P8 or P9 we have a cache line size of 128.
271 unsigned Directive = ST->getDarwinDirective();
272 if (Directive == PPC::DIR_PWR7 || Directive == PPC::DIR_PWR8 ||
273 Directive == PPC::DIR_PWR9)
274 return 128;
275
276 // On other processors return a default of 64 bytes.
277 return 64;
Adam Nemetaf761102016-01-21 18:28:36 +0000278}
279
Adam Nemetb81f1e02016-03-29 23:45:56 +0000280unsigned PPCTTIImpl::getPrefetchDistance() {
281 // This seems like a reasonable default for the BG/Q (this pass is enabled, by
282 // default, only on the BG/Q).
283 return 300;
284}
Adam Nemetdadfbb52016-01-27 22:21:25 +0000285
Wei Mi062c7442015-05-06 17:12:25 +0000286unsigned PPCTTIImpl::getMaxInterleaveFactor(unsigned VF) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000287 unsigned Directive = ST->getDarwinDirective();
288 // The 440 has no SIMD support, but floating-point instructions
289 // have a 5-cycle latency, so unroll by 5x for latency hiding.
290 if (Directive == PPC::DIR_440)
291 return 5;
292
293 // The A2 has no SIMD support, but floating-point instructions
294 // have a 6-cycle latency, so unroll by 6x for latency hiding.
295 if (Directive == PPC::DIR_A2)
296 return 6;
297
298 // FIXME: For lack of any better information, do no harm...
299 if (Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500)
300 return 1;
301
Olivier Sallenave05e69152015-02-12 22:57:58 +0000302 // For P7 and P8, floating-point instructions have a 6-cycle latency and
303 // there are two execution units, so unroll by 12x for latency hiding.
Nemanja Ivanovic6e29baf2016-05-09 18:54:58 +0000304 // FIXME: the same for P9 as previous gen until POWER9 scheduling is ready
305 if (Directive == PPC::DIR_PWR7 || Directive == PPC::DIR_PWR8 ||
306 Directive == PPC::DIR_PWR9)
Olivier Sallenave05e69152015-02-12 22:57:58 +0000307 return 12;
308
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000309 // For most things, modern systems have two execution units (and
310 // out-of-order execution).
311 return 2;
312}
313
Chandler Carruth93205eb2015-08-05 18:08:10 +0000314int PPCTTIImpl::getArithmeticInstrCost(
Chandler Carruth705b1852015-01-31 03:43:40 +0000315 unsigned Opcode, Type *Ty, TTI::OperandValueKind Op1Info,
316 TTI::OperandValueKind Op2Info, TTI::OperandValueProperties Opd1PropInfo,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000317 TTI::OperandValueProperties Opd2PropInfo, ArrayRef<const Value *> Args) {
Dmitri Gribenkoc451bdf2013-01-25 23:17:21 +0000318 assert(TLI->InstructionOpcodeToISD(Opcode) && "Invalid opcode");
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000319
320 // Fallback to the default implementation.
Chandler Carruth705b1852015-01-31 03:43:40 +0000321 return BaseT::getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info,
322 Opd1PropInfo, Opd2PropInfo);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000323}
324
Chandler Carruth93205eb2015-08-05 18:08:10 +0000325int PPCTTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index,
326 Type *SubTp) {
Hal Finkel4a7be232015-09-04 00:10:41 +0000327 // Legalize the type.
328 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp);
329
330 // PPC, for both Altivec/VSX and QPX, support cheap arbitrary permutations
331 // (at least in the sense that there need only be one non-loop-invariant
332 // instruction). We need one such shuffle instruction for each actual
333 // register (this is not true for arbitrary shuffles, but is true for the
334 // structured types of shuffles covered by TTI::ShuffleKind).
335 return LT.first;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000336}
337
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000338int PPCTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src,
339 const Instruction *I) {
Dmitri Gribenkoc451bdf2013-01-25 23:17:21 +0000340 assert(TLI->InstructionOpcodeToISD(Opcode) && "Invalid opcode");
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000341
Chandler Carruth705b1852015-01-31 03:43:40 +0000342 return BaseT::getCastInstrCost(Opcode, Dst, Src);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000343}
344
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000345int PPCTTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy,
346 const Instruction *I) {
347 return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, I);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000348}
349
Chandler Carruth93205eb2015-08-05 18:08:10 +0000350int PPCTTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000351 assert(Val->isVectorTy() && "This must be a vector type");
352
Bill Schmidt62fe7a5b2013-02-08 18:19:17 +0000353 int ISD = TLI->InstructionOpcodeToISD(Opcode);
354 assert(ISD && "Invalid opcode");
Bill Schmidtb3cece12013-02-07 20:33:57 +0000355
Hal Finkel27774d92014-03-13 07:58:58 +0000356 if (ST->hasVSX() && Val->getScalarType()->isDoubleTy()) {
357 // Double-precision scalars are already located in index #0.
358 if (Index == 0)
359 return 0;
360
Chandler Carruth705b1852015-01-31 03:43:40 +0000361 return BaseT::getVectorInstrCost(Opcode, Val, Index);
Hal Finkelc93a9a22015-02-25 01:06:45 +0000362 } else if (ST->hasQPX() && Val->getScalarType()->isFloatingPointTy()) {
363 // Floating point scalars are already located in index #0.
364 if (Index == 0)
365 return 0;
366
367 return BaseT::getVectorInstrCost(Opcode, Val, Index);
Hal Finkel27774d92014-03-13 07:58:58 +0000368 }
369
Bill Schmidt62fe7a5b2013-02-08 18:19:17 +0000370 // Estimated cost of a load-hit-store delay. This was obtained
371 // experimentally as a minimum needed to prevent unprofitable
372 // vectorization for the paq8p benchmark. It may need to be
373 // raised further if other unprofitable cases remain.
Hal Finkelde0b4132014-04-04 23:51:18 +0000374 unsigned LHSPenalty = 2;
375 if (ISD == ISD::INSERT_VECTOR_ELT)
376 LHSPenalty += 7;
Bill Schmidtb3cece12013-02-07 20:33:57 +0000377
Bill Schmidt62fe7a5b2013-02-08 18:19:17 +0000378 // Vector element insert/extract with Altivec is very expensive,
379 // because they require store and reload with the attendant
380 // processor stall for load-hit-store. Until VSX is available,
381 // these need to be estimated as very costly.
382 if (ISD == ISD::EXTRACT_VECTOR_ELT ||
383 ISD == ISD::INSERT_VECTOR_ELT)
Chandler Carruth705b1852015-01-31 03:43:40 +0000384 return LHSPenalty + BaseT::getVectorInstrCost(Opcode, Val, Index);
Bill Schmidtb3cece12013-02-07 20:33:57 +0000385
Chandler Carruth705b1852015-01-31 03:43:40 +0000386 return BaseT::getVectorInstrCost(Opcode, Val, Index);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000387}
388
Chandler Carruth93205eb2015-08-05 18:08:10 +0000389int PPCTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000390 unsigned AddressSpace, const Instruction *I) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000391 // Legalize the type.
Chandler Carruth93205eb2015-08-05 18:08:10 +0000392 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Src);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000393 assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
394 "Invalid Opcode");
395
Chandler Carruth93205eb2015-08-05 18:08:10 +0000396 int Cost = BaseT::getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000397
Hal Finkel79dbf5b2015-09-02 21:03:28 +0000398 bool IsAltivecType = ST->hasAltivec() &&
399 (LT.second == MVT::v16i8 || LT.second == MVT::v8i16 ||
400 LT.second == MVT::v4i32 || LT.second == MVT::v4f32);
401 bool IsVSXType = ST->hasVSX() &&
402 (LT.second == MVT::v2f64 || LT.second == MVT::v2i64);
403 bool IsQPXType = ST->hasQPX() &&
404 (LT.second == MVT::v4f64 || LT.second == MVT::v4f32);
405
Guozhi Wei835de1f2016-12-03 00:41:43 +0000406 // VSX has 32b/64b load instructions. Legalization can handle loading of
407 // 32b/64b to VSR correctly and cheaply. But BaseT::getMemoryOpCost and
408 // PPCTargetLowering can't compute the cost appropriately. So here we
409 // explicitly check this case.
410 unsigned MemBytes = Src->getPrimitiveSizeInBits();
411 if (Opcode == Instruction::Load && ST->hasVSX() && IsAltivecType &&
412 (MemBytes == 64 || (ST->hasP8Vector() && MemBytes == 32)))
413 return 1;
414
415 // Aligned loads and stores are easy.
416 unsigned SrcBytes = LT.second.getStoreSize();
417 if (!SrcBytes || !Alignment || Alignment >= SrcBytes)
418 return Cost;
419
Hal Finkelf11bc762015-09-03 21:23:18 +0000420 // If we can use the permutation-based load sequence, then this is also
421 // relatively cheap (not counting loop-invariant instructions): one load plus
422 // one permute (the last load in a series has extra cost, but we're
Hal Finkel69ada2f2016-03-28 22:39:35 +0000423 // neglecting that here). Note that on the P7, we could do unaligned loads
Hal Finkelf11bc762015-09-03 21:23:18 +0000424 // for Altivec types using the VSX instructions, but that's more expensive
425 // than using the permutation-based load sequence. On the P8, that's no
426 // longer true.
427 if (Opcode == Instruction::Load &&
428 ((!ST->hasP8Vector() && IsAltivecType) || IsQPXType) &&
429 Alignment >= LT.second.getScalarType().getStoreSize())
430 return Cost + LT.first; // Add the cost of the permutations.
431
Hal Finkel79dbf5b2015-09-02 21:03:28 +0000432 // For VSX, we can do unaligned loads and stores on Altivec/VSX types. On the
433 // P7, unaligned vector loads are more expensive than the permutation-based
434 // load sequence, so that might be used instead, but regardless, the net cost
435 // is about the same (not counting loop-invariant instructions).
436 if (IsVSXType || (ST->hasVSX() && IsAltivecType))
437 return Cost;
438
Guozhi Wei7ec2c722017-02-17 22:29:39 +0000439 // Newer PPC supports unaligned memory access.
440 if (TLI->allowsMisalignedMemoryAccesses(LT.second, 0))
441 return Cost;
442
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000443 // PPC in general does not support unaligned loads and stores. They'll need
444 // to be decomposed based on the alignment factor.
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000445
Hal Finkel79dbf5b2015-09-02 21:03:28 +0000446 // Add the cost of each scalar load or store.
447 Cost += LT.first*(SrcBytes/Alignment-1);
448
449 // For a vector type, there is also scalarization overhead (only for
450 // stores, loads are expanded using the vector-load + permutation sequence,
451 // which is much less expensive).
452 if (Src->isVectorTy() && Opcode == Instruction::Store)
453 for (int i = 0, e = Src->getVectorNumElements(); i < e; ++i)
454 Cost += getVectorInstrCost(Instruction::ExtractElement, Src, i);
Hal Finkelde0b4132014-04-04 23:51:18 +0000455
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000456 return Cost;
457}
458
Hal Finkel4a7be232015-09-04 00:10:41 +0000459int PPCTTIImpl::getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy,
460 unsigned Factor,
461 ArrayRef<unsigned> Indices,
462 unsigned Alignment,
463 unsigned AddressSpace) {
464 assert(isa<VectorType>(VecTy) &&
465 "Expect a vector type for interleaved memory op");
466
467 // Legalize the type.
468 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, VecTy);
469
470 // Firstly, the cost of load/store operation.
471 int Cost = getMemoryOpCost(Opcode, VecTy, Alignment, AddressSpace);
472
473 // PPC, for both Altivec/VSX and QPX, support cheap arbitrary permutations
474 // (at least in the sense that there need only be one non-loop-invariant
475 // instruction). For each result vector, we need one shuffle per incoming
476 // vector (except that the first shuffle can take two incoming vectors
477 // because it does not need to take itself).
478 Cost += Factor*(LT.first-1);
479
480 return Cost;
481}
482