blob: a1f6528daea21e88680620831866ad42ccfe3b25 [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
Adam Nemetdadfbb52016-01-27 22:21:25 +000030// This seems like a reasonable default for the BG/Q (this pass is enabled, by
31// default, only on the BG/Q).
32static cl::opt<unsigned>
33PrefDist("ppc-loop-prefetch-distance", cl::Hidden, cl::init(300),
34 cl::desc("The loop prefetch distance"));
35
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000036//===----------------------------------------------------------------------===//
37//
38// PPC cost model.
39//
40//===----------------------------------------------------------------------===//
41
Chandler Carruth705b1852015-01-31 03:43:40 +000042TargetTransformInfo::PopcntSupportKind
43PPCTTIImpl::getPopcntSupport(unsigned TyWidth) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000044 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
Hal Finkelfa7057a2016-03-29 01:36:01 +000045 if (ST->hasPOPCNTD() != PPCSubtarget::POPCNTD_Unavailable && TyWidth <= 64)
46 return ST->hasPOPCNTD() == PPCSubtarget::POPCNTD_Slow ?
47 TTI::PSK_SlowHardware : TTI::PSK_FastHardware;
Chandler Carruth705b1852015-01-31 03:43:40 +000048 return TTI::PSK_Software;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000049}
50
Chandler Carruth93205eb2015-08-05 18:08:10 +000051int PPCTTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) {
Hal Finkel0192cba2014-04-13 23:02:40 +000052 if (DisablePPCConstHoist)
Chandler Carruth705b1852015-01-31 03:43:40 +000053 return BaseT::getIntImmCost(Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +000054
55 assert(Ty->isIntegerTy());
56
57 unsigned BitSize = Ty->getPrimitiveSizeInBits();
58 if (BitSize == 0)
59 return ~0U;
60
61 if (Imm == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +000062 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +000063
64 if (Imm.getBitWidth() <= 64) {
65 if (isInt<16>(Imm.getSExtValue()))
Chandler Carruth705b1852015-01-31 03:43:40 +000066 return TTI::TCC_Basic;
Hal Finkel0192cba2014-04-13 23:02:40 +000067
68 if (isInt<32>(Imm.getSExtValue())) {
69 // A constant that can be materialized using lis.
70 if ((Imm.getZExtValue() & 0xFFFF) == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +000071 return TTI::TCC_Basic;
Hal Finkel0192cba2014-04-13 23:02:40 +000072
Chandler Carruth705b1852015-01-31 03:43:40 +000073 return 2 * TTI::TCC_Basic;
Hal Finkel0192cba2014-04-13 23:02:40 +000074 }
75 }
76
Chandler Carruth705b1852015-01-31 03:43:40 +000077 return 4 * TTI::TCC_Basic;
Hal Finkel0192cba2014-04-13 23:02:40 +000078}
79
Chandler Carruth93205eb2015-08-05 18:08:10 +000080int PPCTTIImpl::getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
81 Type *Ty) {
Hal Finkel0192cba2014-04-13 23:02:40 +000082 if (DisablePPCConstHoist)
Chandler Carruth705b1852015-01-31 03:43:40 +000083 return BaseT::getIntImmCost(IID, Idx, Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +000084
85 assert(Ty->isIntegerTy());
86
87 unsigned BitSize = Ty->getPrimitiveSizeInBits();
88 if (BitSize == 0)
89 return ~0U;
90
91 switch (IID) {
Chandler Carruth705b1852015-01-31 03:43:40 +000092 default:
93 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +000094 case Intrinsic::sadd_with_overflow:
95 case Intrinsic::uadd_with_overflow:
96 case Intrinsic::ssub_with_overflow:
97 case Intrinsic::usub_with_overflow:
98 if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<16>(Imm.getSExtValue()))
Chandler Carruth705b1852015-01-31 03:43:40 +000099 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000100 break;
Hal Finkel934361a2015-01-14 01:07:51 +0000101 case Intrinsic::experimental_stackmap:
102 if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +0000103 return TTI::TCC_Free;
Hal Finkel934361a2015-01-14 01:07:51 +0000104 break;
105 case Intrinsic::experimental_patchpoint_void:
106 case Intrinsic::experimental_patchpoint_i64:
107 if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +0000108 return TTI::TCC_Free;
Hal Finkel934361a2015-01-14 01:07:51 +0000109 break;
Hal Finkel0192cba2014-04-13 23:02:40 +0000110 }
Chandler Carruth705b1852015-01-31 03:43:40 +0000111 return PPCTTIImpl::getIntImmCost(Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +0000112}
113
Chandler Carruth93205eb2015-08-05 18:08:10 +0000114int PPCTTIImpl::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
115 Type *Ty) {
Hal Finkel0192cba2014-04-13 23:02:40 +0000116 if (DisablePPCConstHoist)
Chandler Carruth705b1852015-01-31 03:43:40 +0000117 return BaseT::getIntImmCost(Opcode, Idx, Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +0000118
119 assert(Ty->isIntegerTy());
120
121 unsigned BitSize = Ty->getPrimitiveSizeInBits();
122 if (BitSize == 0)
123 return ~0U;
124
125 unsigned ImmIdx = ~0U;
126 bool ShiftedFree = false, RunFree = false, UnsignedFree = false,
127 ZeroFree = false;
128 switch (Opcode) {
Chandler Carruth705b1852015-01-31 03:43:40 +0000129 default:
130 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000131 case Instruction::GetElementPtr:
132 // Always hoist the base address of a GetElementPtr. This prevents the
133 // creation of new constants for every base constant that gets constant
134 // folded with the offset.
135 if (Idx == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +0000136 return 2 * TTI::TCC_Basic;
137 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000138 case Instruction::And:
139 RunFree = true; // (for the rotate-and-mask instructions)
140 // Fallthrough...
141 case Instruction::Add:
142 case Instruction::Or:
143 case Instruction::Xor:
144 ShiftedFree = true;
145 // Fallthrough...
146 case Instruction::Sub:
147 case Instruction::Mul:
148 case Instruction::Shl:
149 case Instruction::LShr:
150 case Instruction::AShr:
151 ImmIdx = 1;
152 break;
153 case Instruction::ICmp:
154 UnsignedFree = true;
155 ImmIdx = 1;
156 // Fallthrough... (zero comparisons can use record-form instructions)
157 case Instruction::Select:
158 ZeroFree = true;
159 break;
160 case Instruction::PHI:
161 case Instruction::Call:
162 case Instruction::Ret:
163 case Instruction::Load:
164 case Instruction::Store:
165 break;
166 }
167
168 if (ZeroFree && Imm == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +0000169 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000170
171 if (Idx == ImmIdx && Imm.getBitWidth() <= 64) {
172 if (isInt<16>(Imm.getSExtValue()))
Chandler Carruth705b1852015-01-31 03:43:40 +0000173 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000174
175 if (RunFree) {
176 if (Imm.getBitWidth() <= 32 &&
177 (isShiftedMask_32(Imm.getZExtValue()) ||
178 isShiftedMask_32(~Imm.getZExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +0000179 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000180
181 if (ST->isPPC64() &&
182 (isShiftedMask_64(Imm.getZExtValue()) ||
183 isShiftedMask_64(~Imm.getZExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +0000184 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000185 }
186
187 if (UnsignedFree && isUInt<16>(Imm.getZExtValue()))
Chandler Carruth705b1852015-01-31 03:43:40 +0000188 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000189
190 if (ShiftedFree && (Imm.getZExtValue() & 0xFFFF) == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +0000191 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000192 }
193
Chandler Carruth705b1852015-01-31 03:43:40 +0000194 return PPCTTIImpl::getIntImmCost(Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +0000195}
196
Chandler Carruthab5cb362015-02-01 14:31:23 +0000197void PPCTTIImpl::getUnrollingPreferences(Loop *L,
Chandler Carruth705b1852015-01-31 03:43:40 +0000198 TTI::UnrollingPreferences &UP) {
Chandler Carruthc956ab662015-02-01 14:22:17 +0000199 if (ST->getDarwinDirective() == PPC::DIR_A2) {
Hal Finkel71780ec2013-09-11 21:20:40 +0000200 // The A2 is in-order with a deep pipeline, and concatenation unrolling
201 // helps expose latency-hiding opportunities to the instruction scheduler.
202 UP.Partial = UP.Runtime = true;
Hal Finkel3b3c9c32015-05-21 20:30:23 +0000203
204 // We unroll a lot on the A2 (hundreds of instructions), and the benefits
205 // often outweigh the cost of a division to compute the trip count.
206 UP.AllowExpensiveTripCount = true;
Hal Finkel71780ec2013-09-11 21:20:40 +0000207 }
Hal Finkelb359b732015-01-09 15:51:16 +0000208
Chandler Carruthab5cb362015-02-01 14:31:23 +0000209 BaseT::getUnrollingPreferences(L, UP);
Hal Finkel71780ec2013-09-11 21:20:40 +0000210}
211
Olivier Sallenave049d8032015-03-06 23:12:04 +0000212bool PPCTTIImpl::enableAggressiveInterleaving(bool LoopHasReductions) {
Hal Finkel75afa2b2015-09-03 23:23:00 +0000213 // On the A2, always unroll aggressively. For QPX unaligned loads, we depend
214 // on combining the loads generated for consecutive accesses, and failure to
215 // do so is particularly expensive. This makes it much more likely (compared
216 // to only using concatenation unrolling).
217 if (ST->getDarwinDirective() == PPC::DIR_A2)
218 return true;
219
Olivier Sallenave049d8032015-03-06 23:12:04 +0000220 return LoopHasReductions;
221}
222
Hal Finkel4a7be232015-09-04 00:10:41 +0000223bool PPCTTIImpl::enableInterleavedAccessVectorization() {
224 return true;
225}
226
Chandler Carruth705b1852015-01-31 03:43:40 +0000227unsigned PPCTTIImpl::getNumberOfRegisters(bool Vector) {
Hal Finkelc93a9a22015-02-25 01:06:45 +0000228 if (Vector && !ST->hasAltivec() && !ST->hasQPX())
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000229 return 0;
Hal Finkel27774d92014-03-13 07:58:58 +0000230 return ST->hasVSX() ? 64 : 32;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000231}
232
Chandler Carruth705b1852015-01-31 03:43:40 +0000233unsigned PPCTTIImpl::getRegisterBitWidth(bool Vector) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000234 if (Vector) {
Hal Finkelc93a9a22015-02-25 01:06:45 +0000235 if (ST->hasQPX()) return 256;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000236 if (ST->hasAltivec()) return 128;
237 return 0;
238 }
239
240 if (ST->isPPC64())
241 return 64;
242 return 32;
243
244}
245
Adam Nemetaf761102016-01-21 18:28:36 +0000246unsigned PPCTTIImpl::getCacheLineSize() {
247 // This is currently only used for the data prefetch pass which is only
248 // enabled for BG/Q by default.
249 return CacheLineSize;
250}
251
Adam Nemetdadfbb52016-01-27 22:21:25 +0000252unsigned PPCTTIImpl::getPrefetchDistance() { return PrefDist; }
253
Wei Mi062c7442015-05-06 17:12:25 +0000254unsigned PPCTTIImpl::getMaxInterleaveFactor(unsigned VF) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000255 unsigned Directive = ST->getDarwinDirective();
256 // The 440 has no SIMD support, but floating-point instructions
257 // have a 5-cycle latency, so unroll by 5x for latency hiding.
258 if (Directive == PPC::DIR_440)
259 return 5;
260
261 // The A2 has no SIMD support, but floating-point instructions
262 // have a 6-cycle latency, so unroll by 6x for latency hiding.
263 if (Directive == PPC::DIR_A2)
264 return 6;
265
266 // FIXME: For lack of any better information, do no harm...
267 if (Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500)
268 return 1;
269
Olivier Sallenave05e69152015-02-12 22:57:58 +0000270 // For P7 and P8, floating-point instructions have a 6-cycle latency and
271 // there are two execution units, so unroll by 12x for latency hiding.
272 if (Directive == PPC::DIR_PWR7 ||
273 Directive == PPC::DIR_PWR8)
274 return 12;
275
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000276 // For most things, modern systems have two execution units (and
277 // out-of-order execution).
278 return 2;
279}
280
Chandler Carruth93205eb2015-08-05 18:08:10 +0000281int PPCTTIImpl::getArithmeticInstrCost(
Chandler Carruth705b1852015-01-31 03:43:40 +0000282 unsigned Opcode, Type *Ty, TTI::OperandValueKind Op1Info,
283 TTI::OperandValueKind Op2Info, TTI::OperandValueProperties Opd1PropInfo,
284 TTI::OperandValueProperties Opd2PropInfo) {
Dmitri Gribenkoc451bdf2013-01-25 23:17:21 +0000285 assert(TLI->InstructionOpcodeToISD(Opcode) && "Invalid opcode");
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000286
287 // Fallback to the default implementation.
Chandler Carruth705b1852015-01-31 03:43:40 +0000288 return BaseT::getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info,
289 Opd1PropInfo, Opd2PropInfo);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000290}
291
Chandler Carruth93205eb2015-08-05 18:08:10 +0000292int PPCTTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index,
293 Type *SubTp) {
Hal Finkel4a7be232015-09-04 00:10:41 +0000294 // Legalize the type.
295 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp);
296
297 // PPC, for both Altivec/VSX and QPX, support cheap arbitrary permutations
298 // (at least in the sense that there need only be one non-loop-invariant
299 // instruction). We need one such shuffle instruction for each actual
300 // register (this is not true for arbitrary shuffles, but is true for the
301 // structured types of shuffles covered by TTI::ShuffleKind).
302 return LT.first;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000303}
304
Chandler Carruth93205eb2015-08-05 18:08:10 +0000305int PPCTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) {
Dmitri Gribenkoc451bdf2013-01-25 23:17:21 +0000306 assert(TLI->InstructionOpcodeToISD(Opcode) && "Invalid opcode");
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000307
Chandler Carruth705b1852015-01-31 03:43:40 +0000308 return BaseT::getCastInstrCost(Opcode, Dst, Src);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000309}
310
Chandler Carruth93205eb2015-08-05 18:08:10 +0000311int PPCTTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy) {
Chandler Carruth705b1852015-01-31 03:43:40 +0000312 return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000313}
314
Chandler Carruth93205eb2015-08-05 18:08:10 +0000315int PPCTTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000316 assert(Val->isVectorTy() && "This must be a vector type");
317
Bill Schmidt62fe7a5b2013-02-08 18:19:17 +0000318 int ISD = TLI->InstructionOpcodeToISD(Opcode);
319 assert(ISD && "Invalid opcode");
Bill Schmidtb3cece12013-02-07 20:33:57 +0000320
Hal Finkel27774d92014-03-13 07:58:58 +0000321 if (ST->hasVSX() && Val->getScalarType()->isDoubleTy()) {
322 // Double-precision scalars are already located in index #0.
323 if (Index == 0)
324 return 0;
325
Chandler Carruth705b1852015-01-31 03:43:40 +0000326 return BaseT::getVectorInstrCost(Opcode, Val, Index);
Hal Finkelc93a9a22015-02-25 01:06:45 +0000327 } else if (ST->hasQPX() && Val->getScalarType()->isFloatingPointTy()) {
328 // Floating point scalars are already located in index #0.
329 if (Index == 0)
330 return 0;
331
332 return BaseT::getVectorInstrCost(Opcode, Val, Index);
Hal Finkel27774d92014-03-13 07:58:58 +0000333 }
334
Bill Schmidt62fe7a5b2013-02-08 18:19:17 +0000335 // Estimated cost of a load-hit-store delay. This was obtained
336 // experimentally as a minimum needed to prevent unprofitable
337 // vectorization for the paq8p benchmark. It may need to be
338 // raised further if other unprofitable cases remain.
Hal Finkelde0b4132014-04-04 23:51:18 +0000339 unsigned LHSPenalty = 2;
340 if (ISD == ISD::INSERT_VECTOR_ELT)
341 LHSPenalty += 7;
Bill Schmidtb3cece12013-02-07 20:33:57 +0000342
Bill Schmidt62fe7a5b2013-02-08 18:19:17 +0000343 // Vector element insert/extract with Altivec is very expensive,
344 // because they require store and reload with the attendant
345 // processor stall for load-hit-store. Until VSX is available,
346 // these need to be estimated as very costly.
347 if (ISD == ISD::EXTRACT_VECTOR_ELT ||
348 ISD == ISD::INSERT_VECTOR_ELT)
Chandler Carruth705b1852015-01-31 03:43:40 +0000349 return LHSPenalty + BaseT::getVectorInstrCost(Opcode, Val, Index);
Bill Schmidtb3cece12013-02-07 20:33:57 +0000350
Chandler Carruth705b1852015-01-31 03:43:40 +0000351 return BaseT::getVectorInstrCost(Opcode, Val, Index);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000352}
353
Chandler Carruth93205eb2015-08-05 18:08:10 +0000354int PPCTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
355 unsigned AddressSpace) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000356 // Legalize the type.
Chandler Carruth93205eb2015-08-05 18:08:10 +0000357 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Src);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000358 assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
359 "Invalid Opcode");
360
Chandler Carruth93205eb2015-08-05 18:08:10 +0000361 int Cost = BaseT::getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000362
Hal Finkel79dbf5b2015-09-02 21:03:28 +0000363 // Aligned loads and stores are easy.
364 unsigned SrcBytes = LT.second.getStoreSize();
365 if (!SrcBytes || !Alignment || Alignment >= SrcBytes)
366 return Cost;
Hal Finkelde0b4132014-04-04 23:51:18 +0000367
Hal Finkel79dbf5b2015-09-02 21:03:28 +0000368 bool IsAltivecType = ST->hasAltivec() &&
369 (LT.second == MVT::v16i8 || LT.second == MVT::v8i16 ||
370 LT.second == MVT::v4i32 || LT.second == MVT::v4f32);
371 bool IsVSXType = ST->hasVSX() &&
372 (LT.second == MVT::v2f64 || LT.second == MVT::v2i64);
373 bool IsQPXType = ST->hasQPX() &&
374 (LT.second == MVT::v4f64 || LT.second == MVT::v4f32);
375
Hal Finkelf11bc762015-09-03 21:23:18 +0000376 // If we can use the permutation-based load sequence, then this is also
377 // relatively cheap (not counting loop-invariant instructions): one load plus
378 // one permute (the last load in a series has extra cost, but we're
Hal Finkel69ada2f2016-03-28 22:39:35 +0000379 // neglecting that here). Note that on the P7, we could do unaligned loads
Hal Finkelf11bc762015-09-03 21:23:18 +0000380 // for Altivec types using the VSX instructions, but that's more expensive
381 // than using the permutation-based load sequence. On the P8, that's no
382 // longer true.
383 if (Opcode == Instruction::Load &&
384 ((!ST->hasP8Vector() && IsAltivecType) || IsQPXType) &&
385 Alignment >= LT.second.getScalarType().getStoreSize())
386 return Cost + LT.first; // Add the cost of the permutations.
387
Hal Finkel79dbf5b2015-09-02 21:03:28 +0000388 // For VSX, we can do unaligned loads and stores on Altivec/VSX types. On the
389 // P7, unaligned vector loads are more expensive than the permutation-based
390 // load sequence, so that might be used instead, but regardless, the net cost
391 // is about the same (not counting loop-invariant instructions).
392 if (IsVSXType || (ST->hasVSX() && IsAltivecType))
393 return Cost;
394
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000395 // PPC in general does not support unaligned loads and stores. They'll need
396 // to be decomposed based on the alignment factor.
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000397
Hal Finkel79dbf5b2015-09-02 21:03:28 +0000398 // Add the cost of each scalar load or store.
399 Cost += LT.first*(SrcBytes/Alignment-1);
400
401 // For a vector type, there is also scalarization overhead (only for
402 // stores, loads are expanded using the vector-load + permutation sequence,
403 // which is much less expensive).
404 if (Src->isVectorTy() && Opcode == Instruction::Store)
405 for (int i = 0, e = Src->getVectorNumElements(); i < e; ++i)
406 Cost += getVectorInstrCost(Instruction::ExtractElement, Src, i);
Hal Finkelde0b4132014-04-04 23:51:18 +0000407
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000408 return Cost;
409}
410
Hal Finkel4a7be232015-09-04 00:10:41 +0000411int PPCTTIImpl::getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy,
412 unsigned Factor,
413 ArrayRef<unsigned> Indices,
414 unsigned Alignment,
415 unsigned AddressSpace) {
416 assert(isa<VectorType>(VecTy) &&
417 "Expect a vector type for interleaved memory op");
418
419 // Legalize the type.
420 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, VecTy);
421
422 // Firstly, the cost of load/store operation.
423 int Cost = getMemoryOpCost(Opcode, VecTy, Alignment, AddressSpace);
424
425 // PPC, for both Altivec/VSX and QPX, support cheap arbitrary permutations
426 // (at least in the sense that there need only be one non-loop-invariant
427 // instruction). For each result vector, we need one shuffle per incoming
428 // vector (except that the first shuffle can take two incoming vectors
429 // because it does not need to take itself).
430 Cost += Factor*(LT.first-1);
431
432 return Cost;
433}
434