blob: 9212e916f59838058635641d6e27bbbb2a6affdb [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 Finkela4d07482013-03-28 13:29:47 +000045 if (ST->hasPOPCNTD() && TyWidth <= 64)
Chandler Carruth705b1852015-01-31 03:43:40 +000046 return TTI::PSK_FastHardware;
47 return TTI::PSK_Software;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000048}
49
Chandler Carruth93205eb2015-08-05 18:08:10 +000050int PPCTTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) {
Hal Finkel0192cba2014-04-13 23:02:40 +000051 if (DisablePPCConstHoist)
Chandler Carruth705b1852015-01-31 03:43:40 +000052 return BaseT::getIntImmCost(Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +000053
54 assert(Ty->isIntegerTy());
55
56 unsigned BitSize = Ty->getPrimitiveSizeInBits();
57 if (BitSize == 0)
58 return ~0U;
59
60 if (Imm == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +000061 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +000062
63 if (Imm.getBitWidth() <= 64) {
64 if (isInt<16>(Imm.getSExtValue()))
Chandler Carruth705b1852015-01-31 03:43:40 +000065 return TTI::TCC_Basic;
Hal Finkel0192cba2014-04-13 23:02:40 +000066
67 if (isInt<32>(Imm.getSExtValue())) {
68 // A constant that can be materialized using lis.
69 if ((Imm.getZExtValue() & 0xFFFF) == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +000070 return TTI::TCC_Basic;
Hal Finkel0192cba2014-04-13 23:02:40 +000071
Chandler Carruth705b1852015-01-31 03:43:40 +000072 return 2 * TTI::TCC_Basic;
Hal Finkel0192cba2014-04-13 23:02:40 +000073 }
74 }
75
Chandler Carruth705b1852015-01-31 03:43:40 +000076 return 4 * TTI::TCC_Basic;
Hal Finkel0192cba2014-04-13 23:02:40 +000077}
78
Chandler Carruth93205eb2015-08-05 18:08:10 +000079int PPCTTIImpl::getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
80 Type *Ty) {
Hal Finkel0192cba2014-04-13 23:02:40 +000081 if (DisablePPCConstHoist)
Chandler Carruth705b1852015-01-31 03:43:40 +000082 return BaseT::getIntImmCost(IID, Idx, Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +000083
84 assert(Ty->isIntegerTy());
85
86 unsigned BitSize = Ty->getPrimitiveSizeInBits();
87 if (BitSize == 0)
88 return ~0U;
89
90 switch (IID) {
Chandler Carruth705b1852015-01-31 03:43:40 +000091 default:
92 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +000093 case Intrinsic::sadd_with_overflow:
94 case Intrinsic::uadd_with_overflow:
95 case Intrinsic::ssub_with_overflow:
96 case Intrinsic::usub_with_overflow:
97 if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<16>(Imm.getSExtValue()))
Chandler Carruth705b1852015-01-31 03:43:40 +000098 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +000099 break;
Hal Finkel934361a2015-01-14 01:07:51 +0000100 case Intrinsic::experimental_stackmap:
101 if ((Idx < 2) || (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;
104 case Intrinsic::experimental_patchpoint_void:
105 case Intrinsic::experimental_patchpoint_i64:
106 if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +0000107 return TTI::TCC_Free;
Hal Finkel934361a2015-01-14 01:07:51 +0000108 break;
Hal Finkel0192cba2014-04-13 23:02:40 +0000109 }
Chandler Carruth705b1852015-01-31 03:43:40 +0000110 return PPCTTIImpl::getIntImmCost(Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +0000111}
112
Chandler Carruth93205eb2015-08-05 18:08:10 +0000113int PPCTTIImpl::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
114 Type *Ty) {
Hal Finkel0192cba2014-04-13 23:02:40 +0000115 if (DisablePPCConstHoist)
Chandler Carruth705b1852015-01-31 03:43:40 +0000116 return BaseT::getIntImmCost(Opcode, Idx, Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +0000117
118 assert(Ty->isIntegerTy());
119
120 unsigned BitSize = Ty->getPrimitiveSizeInBits();
121 if (BitSize == 0)
122 return ~0U;
123
124 unsigned ImmIdx = ~0U;
125 bool ShiftedFree = false, RunFree = false, UnsignedFree = false,
126 ZeroFree = false;
127 switch (Opcode) {
Chandler Carruth705b1852015-01-31 03:43:40 +0000128 default:
129 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000130 case Instruction::GetElementPtr:
131 // Always hoist the base address of a GetElementPtr. This prevents the
132 // creation of new constants for every base constant that gets constant
133 // folded with the offset.
134 if (Idx == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +0000135 return 2 * TTI::TCC_Basic;
136 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000137 case Instruction::And:
138 RunFree = true; // (for the rotate-and-mask instructions)
139 // Fallthrough...
140 case Instruction::Add:
141 case Instruction::Or:
142 case Instruction::Xor:
143 ShiftedFree = true;
144 // Fallthrough...
145 case Instruction::Sub:
146 case Instruction::Mul:
147 case Instruction::Shl:
148 case Instruction::LShr:
149 case Instruction::AShr:
150 ImmIdx = 1;
151 break;
152 case Instruction::ICmp:
153 UnsignedFree = true;
154 ImmIdx = 1;
155 // Fallthrough... (zero comparisons can use record-form instructions)
156 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
Chandler Carruthab5cb362015-02-01 14:31:23 +0000196void PPCTTIImpl::getUnrollingPreferences(Loop *L,
Chandler Carruth705b1852015-01-31 03:43:40 +0000197 TTI::UnrollingPreferences &UP) {
Chandler Carruthc956ab662015-02-01 14:22:17 +0000198 if (ST->getDarwinDirective() == PPC::DIR_A2) {
Hal Finkel71780ec2013-09-11 21:20:40 +0000199 // The A2 is in-order with a deep pipeline, and concatenation unrolling
200 // helps expose latency-hiding opportunities to the instruction scheduler.
201 UP.Partial = UP.Runtime = true;
Hal Finkel3b3c9c32015-05-21 20:30:23 +0000202
203 // We unroll a lot on the A2 (hundreds of instructions), and the benefits
204 // often outweigh the cost of a division to compute the trip count.
205 UP.AllowExpensiveTripCount = true;
Hal Finkel71780ec2013-09-11 21:20:40 +0000206 }
Hal Finkelb359b732015-01-09 15:51:16 +0000207
Chandler Carruthab5cb362015-02-01 14:31:23 +0000208 BaseT::getUnrollingPreferences(L, UP);
Hal Finkel71780ec2013-09-11 21:20:40 +0000209}
210
Olivier Sallenave049d8032015-03-06 23:12:04 +0000211bool PPCTTIImpl::enableAggressiveInterleaving(bool LoopHasReductions) {
Hal Finkel75afa2b2015-09-03 23:23:00 +0000212 // On the A2, always unroll aggressively. For QPX unaligned loads, we depend
213 // on combining the loads generated for consecutive accesses, and failure to
214 // do so is particularly expensive. This makes it much more likely (compared
215 // to only using concatenation unrolling).
216 if (ST->getDarwinDirective() == PPC::DIR_A2)
217 return true;
218
Olivier Sallenave049d8032015-03-06 23:12:04 +0000219 return LoopHasReductions;
220}
221
Hal Finkel4a7be232015-09-04 00:10:41 +0000222bool PPCTTIImpl::enableInterleavedAccessVectorization() {
223 return true;
224}
225
Chandler Carruth705b1852015-01-31 03:43:40 +0000226unsigned PPCTTIImpl::getNumberOfRegisters(bool Vector) {
Hal Finkelc93a9a22015-02-25 01:06:45 +0000227 if (Vector && !ST->hasAltivec() && !ST->hasQPX())
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000228 return 0;
Hal Finkel27774d92014-03-13 07:58:58 +0000229 return ST->hasVSX() ? 64 : 32;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000230}
231
Chandler Carruth705b1852015-01-31 03:43:40 +0000232unsigned PPCTTIImpl::getRegisterBitWidth(bool Vector) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000233 if (Vector) {
Hal Finkelc93a9a22015-02-25 01:06:45 +0000234 if (ST->hasQPX()) return 256;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000235 if (ST->hasAltivec()) return 128;
236 return 0;
237 }
238
239 if (ST->isPPC64())
240 return 64;
241 return 32;
242
243}
244
Adam Nemetaf761102016-01-21 18:28:36 +0000245unsigned PPCTTIImpl::getCacheLineSize() {
246 // This is currently only used for the data prefetch pass which is only
247 // enabled for BG/Q by default.
248 return CacheLineSize;
249}
250
Adam Nemetdadfbb52016-01-27 22:21:25 +0000251unsigned PPCTTIImpl::getPrefetchDistance() { return PrefDist; }
252
Wei Mi062c7442015-05-06 17:12:25 +0000253unsigned PPCTTIImpl::getMaxInterleaveFactor(unsigned VF) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000254 unsigned Directive = ST->getDarwinDirective();
255 // The 440 has no SIMD support, but floating-point instructions
256 // have a 5-cycle latency, so unroll by 5x for latency hiding.
257 if (Directive == PPC::DIR_440)
258 return 5;
259
260 // The A2 has no SIMD support, but floating-point instructions
261 // have a 6-cycle latency, so unroll by 6x for latency hiding.
262 if (Directive == PPC::DIR_A2)
263 return 6;
264
265 // FIXME: For lack of any better information, do no harm...
266 if (Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500)
267 return 1;
268
Olivier Sallenave05e69152015-02-12 22:57:58 +0000269 // For P7 and P8, floating-point instructions have a 6-cycle latency and
270 // there are two execution units, so unroll by 12x for latency hiding.
271 if (Directive == PPC::DIR_PWR7 ||
272 Directive == PPC::DIR_PWR8)
273 return 12;
274
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000275 // For most things, modern systems have two execution units (and
276 // out-of-order execution).
277 return 2;
278}
279
Chandler Carruth93205eb2015-08-05 18:08:10 +0000280int PPCTTIImpl::getArithmeticInstrCost(
Chandler Carruth705b1852015-01-31 03:43:40 +0000281 unsigned Opcode, Type *Ty, TTI::OperandValueKind Op1Info,
282 TTI::OperandValueKind Op2Info, TTI::OperandValueProperties Opd1PropInfo,
283 TTI::OperandValueProperties Opd2PropInfo) {
Dmitri Gribenkoc451bdf2013-01-25 23:17:21 +0000284 assert(TLI->InstructionOpcodeToISD(Opcode) && "Invalid opcode");
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000285
286 // Fallback to the default implementation.
Chandler Carruth705b1852015-01-31 03:43:40 +0000287 return BaseT::getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info,
288 Opd1PropInfo, Opd2PropInfo);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000289}
290
Chandler Carruth93205eb2015-08-05 18:08:10 +0000291int PPCTTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index,
292 Type *SubTp) {
Hal Finkel4a7be232015-09-04 00:10:41 +0000293 // Legalize the type.
294 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp);
295
296 // PPC, for both Altivec/VSX and QPX, support cheap arbitrary permutations
297 // (at least in the sense that there need only be one non-loop-invariant
298 // instruction). We need one such shuffle instruction for each actual
299 // register (this is not true for arbitrary shuffles, but is true for the
300 // structured types of shuffles covered by TTI::ShuffleKind).
301 return LT.first;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000302}
303
Chandler Carruth93205eb2015-08-05 18:08:10 +0000304int PPCTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) {
Dmitri Gribenkoc451bdf2013-01-25 23:17:21 +0000305 assert(TLI->InstructionOpcodeToISD(Opcode) && "Invalid opcode");
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000306
Chandler Carruth705b1852015-01-31 03:43:40 +0000307 return BaseT::getCastInstrCost(Opcode, Dst, Src);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000308}
309
Chandler Carruth93205eb2015-08-05 18:08:10 +0000310int PPCTTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy) {
Chandler Carruth705b1852015-01-31 03:43:40 +0000311 return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000312}
313
Chandler Carruth93205eb2015-08-05 18:08:10 +0000314int PPCTTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000315 assert(Val->isVectorTy() && "This must be a vector type");
316
Bill Schmidt62fe7a5b2013-02-08 18:19:17 +0000317 int ISD = TLI->InstructionOpcodeToISD(Opcode);
318 assert(ISD && "Invalid opcode");
Bill Schmidtb3cece12013-02-07 20:33:57 +0000319
Hal Finkel27774d92014-03-13 07:58:58 +0000320 if (ST->hasVSX() && Val->getScalarType()->isDoubleTy()) {
321 // Double-precision scalars are already located in index #0.
322 if (Index == 0)
323 return 0;
324
Chandler Carruth705b1852015-01-31 03:43:40 +0000325 return BaseT::getVectorInstrCost(Opcode, Val, Index);
Hal Finkelc93a9a22015-02-25 01:06:45 +0000326 } else if (ST->hasQPX() && Val->getScalarType()->isFloatingPointTy()) {
327 // Floating point scalars are already located in index #0.
328 if (Index == 0)
329 return 0;
330
331 return BaseT::getVectorInstrCost(Opcode, Val, Index);
Hal Finkel27774d92014-03-13 07:58:58 +0000332 }
333
Bill Schmidt62fe7a5b2013-02-08 18:19:17 +0000334 // Estimated cost of a load-hit-store delay. This was obtained
335 // experimentally as a minimum needed to prevent unprofitable
336 // vectorization for the paq8p benchmark. It may need to be
337 // raised further if other unprofitable cases remain.
Hal Finkelde0b4132014-04-04 23:51:18 +0000338 unsigned LHSPenalty = 2;
339 if (ISD == ISD::INSERT_VECTOR_ELT)
340 LHSPenalty += 7;
Bill Schmidtb3cece12013-02-07 20:33:57 +0000341
Bill Schmidt62fe7a5b2013-02-08 18:19:17 +0000342 // Vector element insert/extract with Altivec is very expensive,
343 // because they require store and reload with the attendant
344 // processor stall for load-hit-store. Until VSX is available,
345 // these need to be estimated as very costly.
346 if (ISD == ISD::EXTRACT_VECTOR_ELT ||
347 ISD == ISD::INSERT_VECTOR_ELT)
Chandler Carruth705b1852015-01-31 03:43:40 +0000348 return LHSPenalty + BaseT::getVectorInstrCost(Opcode, Val, Index);
Bill Schmidtb3cece12013-02-07 20:33:57 +0000349
Chandler Carruth705b1852015-01-31 03:43:40 +0000350 return BaseT::getVectorInstrCost(Opcode, Val, Index);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000351}
352
Chandler Carruth93205eb2015-08-05 18:08:10 +0000353int PPCTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
354 unsigned AddressSpace) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000355 // Legalize the type.
Chandler Carruth93205eb2015-08-05 18:08:10 +0000356 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Src);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000357 assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
358 "Invalid Opcode");
359
Chandler Carruth93205eb2015-08-05 18:08:10 +0000360 int Cost = BaseT::getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000361
Hal Finkel79dbf5b2015-09-02 21:03:28 +0000362 // Aligned loads and stores are easy.
363 unsigned SrcBytes = LT.second.getStoreSize();
364 if (!SrcBytes || !Alignment || Alignment >= SrcBytes)
365 return Cost;
Hal Finkelde0b4132014-04-04 23:51:18 +0000366
Hal Finkel79dbf5b2015-09-02 21:03:28 +0000367 bool IsAltivecType = ST->hasAltivec() &&
368 (LT.second == MVT::v16i8 || LT.second == MVT::v8i16 ||
369 LT.second == MVT::v4i32 || LT.second == MVT::v4f32);
370 bool IsVSXType = ST->hasVSX() &&
371 (LT.second == MVT::v2f64 || LT.second == MVT::v2i64);
372 bool IsQPXType = ST->hasQPX() &&
373 (LT.second == MVT::v4f64 || LT.second == MVT::v4f32);
374
Hal Finkelf11bc762015-09-03 21:23:18 +0000375 // If we can use the permutation-based load sequence, then this is also
376 // relatively cheap (not counting loop-invariant instructions): one load plus
377 // one permute (the last load in a series has extra cost, but we're
378 // neglecting that here). Note that on the P7, we should do unaligned loads
379 // for Altivec types using the VSX instructions, but that's more expensive
380 // than using the permutation-based load sequence. On the P8, that's no
381 // longer true.
382 if (Opcode == Instruction::Load &&
383 ((!ST->hasP8Vector() && IsAltivecType) || IsQPXType) &&
384 Alignment >= LT.second.getScalarType().getStoreSize())
385 return Cost + LT.first; // Add the cost of the permutations.
386
Hal Finkel79dbf5b2015-09-02 21:03:28 +0000387 // For VSX, we can do unaligned loads and stores on Altivec/VSX types. On the
388 // P7, unaligned vector loads are more expensive than the permutation-based
389 // load sequence, so that might be used instead, but regardless, the net cost
390 // is about the same (not counting loop-invariant instructions).
391 if (IsVSXType || (ST->hasVSX() && IsAltivecType))
392 return Cost;
393
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000394 // PPC in general does not support unaligned loads and stores. They'll need
395 // to be decomposed based on the alignment factor.
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000396
Hal Finkel79dbf5b2015-09-02 21:03:28 +0000397 // Add the cost of each scalar load or store.
398 Cost += LT.first*(SrcBytes/Alignment-1);
399
400 // For a vector type, there is also scalarization overhead (only for
401 // stores, loads are expanded using the vector-load + permutation sequence,
402 // which is much less expensive).
403 if (Src->isVectorTy() && Opcode == Instruction::Store)
404 for (int i = 0, e = Src->getVectorNumElements(); i < e; ++i)
405 Cost += getVectorInstrCost(Instruction::ExtractElement, Src, i);
Hal Finkelde0b4132014-04-04 23:51:18 +0000406
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000407 return Cost;
408}
409
Hal Finkel4a7be232015-09-04 00:10:41 +0000410int PPCTTIImpl::getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy,
411 unsigned Factor,
412 ArrayRef<unsigned> Indices,
413 unsigned Alignment,
414 unsigned AddressSpace) {
415 assert(isa<VectorType>(VecTy) &&
416 "Expect a vector type for interleaved memory op");
417
418 // Legalize the type.
419 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, VecTy);
420
421 // Firstly, the cost of load/store operation.
422 int Cost = getMemoryOpCost(Opcode, VecTy, Alignment, AddressSpace);
423
424 // PPC, for both Altivec/VSX and QPX, support cheap arbitrary permutations
425 // (at least in the sense that there need only be one non-loop-invariant
426 // instruction). For each result vector, we need one shuffle per incoming
427 // vector (except that the first shuffle can take two incoming vectors
428 // because it does not need to take itself).
429 Cost += Factor*(LT.first-1);
430
431 return Cost;
432}
433