blob: 6bdee25477c5706e46dee7de07f44988ccf552de [file] [log] [blame]
Hal Finkel4e5ca9e2013-01-25 23:05:59 +00001//===-- PPCTargetTransformInfo.cpp - PPC specific TTI pass ----------------===//
2//
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//===----------------------------------------------------------------------===//
9/// \file
10/// This file implements a TargetTransformInfo analysis pass specific to the
11/// PPC target machine. It uses the target's detailed information to provide
12/// more precise answers to certain TTI queries, while letting the target
13/// independent and default TTI implementations handle the rest.
14///
15//===----------------------------------------------------------------------===//
16
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000017#include "PPC.h"
18#include "PPCTargetMachine.h"
19#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruth705b1852015-01-31 03:43:40 +000020#include "llvm/CodeGen/BasicTTIImpl.h"
Hal Finkel0192cba2014-04-13 23:02:40 +000021#include "llvm/Support/CommandLine.h"
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000022#include "llvm/Support/Debug.h"
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000023#include "llvm/Target/CostTable.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000024#include "llvm/Target/TargetLowering.h"
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000025using namespace llvm;
26
Chandler Carruth84e68b22014-04-22 02:41:26 +000027#define DEBUG_TYPE "ppctti"
28
Hal Finkel0192cba2014-04-13 23:02:40 +000029static cl::opt<bool> DisablePPCConstHoist("disable-ppc-constant-hoisting",
30cl::desc("disable constant hoisting on PPC"), cl::init(false), cl::Hidden);
31
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000032namespace {
33
Chandler Carruth705b1852015-01-31 03:43:40 +000034class PPCTTIImpl : public BasicTTIImplBase<PPCTTIImpl> {
35 typedef BasicTTIImplBase<PPCTTIImpl> BaseT;
36 typedef TargetTransformInfo TTI;
37
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000038 const PPCSubtarget *ST;
39 const PPCTargetLowering *TLI;
40
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000041public:
Chandler Carruth705b1852015-01-31 03:43:40 +000042 explicit PPCTTIImpl(const PPCTargetMachine *TM = nullptr)
43 : BaseT(TM), ST(TM->getSubtargetImpl()), TLI(ST->getTargetLowering()) {}
44
45 // Provide value semantics. MSVC requires that we spell all of these out.
46 PPCTTIImpl(const PPCTTIImpl &Arg)
47 : BaseT(static_cast<const BaseT &>(Arg)), ST(Arg.ST), TLI(Arg.TLI) {}
48 PPCTTIImpl(PPCTTIImpl &&Arg)
49 : BaseT(std::move(static_cast<BaseT &>(Arg))), ST(std::move(Arg.ST)),
50 TLI(std::move(Arg.TLI)) {}
51 PPCTTIImpl &operator=(const PPCTTIImpl &RHS) {
52 BaseT::operator=(static_cast<const BaseT &>(RHS));
53 ST = RHS.ST;
54 TLI = RHS.TLI;
55 return *this;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000056 }
Chandler Carruth705b1852015-01-31 03:43:40 +000057 PPCTTIImpl &operator=(PPCTTIImpl &&RHS) {
58 BaseT::operator=(std::move(static_cast<BaseT &>(RHS)));
59 ST = std::move(RHS.ST);
60 TLI = std::move(RHS.TLI);
61 return *this;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000062 }
63
64 /// \name Scalar TTI Implementations
65 /// @{
Chandler Carruth705b1852015-01-31 03:43:40 +000066
67 using BaseT::getIntImmCost;
68 unsigned getIntImmCost(const APInt &Imm, Type *Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +000069
70 unsigned getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
Chandler Carruth705b1852015-01-31 03:43:40 +000071 Type *Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +000072 unsigned getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
Chandler Carruth705b1852015-01-31 03:43:40 +000073 Type *Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +000074
Chandler Carruth705b1852015-01-31 03:43:40 +000075 TTI::PopcntSupportKind getPopcntSupport(unsigned TyWidth);
Eric Christopherd85ffb12014-09-18 00:34:14 +000076 void getUnrollingPreferences(const Function *F, Loop *L,
Chandler Carruth705b1852015-01-31 03:43:40 +000077 TTI::UnrollingPreferences &UP);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000078
79 /// @}
80
81 /// \name Vector TTI Implementations
82 /// @{
83
Chandler Carruth705b1852015-01-31 03:43:40 +000084 unsigned getNumberOfRegisters(bool Vector);
85 unsigned getRegisterBitWidth(bool Vector);
86 unsigned getMaxInterleaveFactor();
87 unsigned getArithmeticInstrCost(
88 unsigned Opcode, Type *Ty,
89 TTI::OperandValueKind Opd1Info = TTI::OK_AnyValue,
90 TTI::OperandValueKind Opd2Info = TTI::OK_AnyValue,
91 TTI::OperandValueProperties Opd1PropInfo = TTI::OP_None,
92 TTI::OperandValueProperties Opd2PropInfo = TTI::OP_None);
93 unsigned getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index,
94 Type *SubTp);
95 unsigned getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src);
96 unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy);
97 unsigned getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index);
Craig Topperfd38cbe2014-08-30 16:48:34 +000098 unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
Chandler Carruth705b1852015-01-31 03:43:40 +000099 unsigned AddressSpace);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000100
101 /// @}
102};
103
104} // end anonymous namespace
105
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000106ImmutablePass *
107llvm::createPPCTargetTransformInfoPass(const PPCTargetMachine *TM) {
Chandler Carruth705b1852015-01-31 03:43:40 +0000108 return new TargetTransformInfoWrapperPass(PPCTTIImpl(TM));
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000109}
110
111
112//===----------------------------------------------------------------------===//
113//
114// PPC cost model.
115//
116//===----------------------------------------------------------------------===//
117
Chandler Carruth705b1852015-01-31 03:43:40 +0000118TargetTransformInfo::PopcntSupportKind
119PPCTTIImpl::getPopcntSupport(unsigned TyWidth) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000120 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
Hal Finkela4d07482013-03-28 13:29:47 +0000121 if (ST->hasPOPCNTD() && TyWidth <= 64)
Chandler Carruth705b1852015-01-31 03:43:40 +0000122 return TTI::PSK_FastHardware;
123 return TTI::PSK_Software;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000124}
125
Chandler Carruth705b1852015-01-31 03:43:40 +0000126unsigned PPCTTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) {
Hal Finkel0192cba2014-04-13 23:02:40 +0000127 if (DisablePPCConstHoist)
Chandler Carruth705b1852015-01-31 03:43:40 +0000128 return BaseT::getIntImmCost(Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +0000129
130 assert(Ty->isIntegerTy());
131
132 unsigned BitSize = Ty->getPrimitiveSizeInBits();
133 if (BitSize == 0)
134 return ~0U;
135
136 if (Imm == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +0000137 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000138
139 if (Imm.getBitWidth() <= 64) {
140 if (isInt<16>(Imm.getSExtValue()))
Chandler Carruth705b1852015-01-31 03:43:40 +0000141 return TTI::TCC_Basic;
Hal Finkel0192cba2014-04-13 23:02:40 +0000142
143 if (isInt<32>(Imm.getSExtValue())) {
144 // A constant that can be materialized using lis.
145 if ((Imm.getZExtValue() & 0xFFFF) == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +0000146 return TTI::TCC_Basic;
Hal Finkel0192cba2014-04-13 23:02:40 +0000147
Chandler Carruth705b1852015-01-31 03:43:40 +0000148 return 2 * TTI::TCC_Basic;
Hal Finkel0192cba2014-04-13 23:02:40 +0000149 }
150 }
151
Chandler Carruth705b1852015-01-31 03:43:40 +0000152 return 4 * TTI::TCC_Basic;
Hal Finkel0192cba2014-04-13 23:02:40 +0000153}
154
Chandler Carruth705b1852015-01-31 03:43:40 +0000155unsigned PPCTTIImpl::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
156 const APInt &Imm, Type *Ty) {
Hal Finkel0192cba2014-04-13 23:02:40 +0000157 if (DisablePPCConstHoist)
Chandler Carruth705b1852015-01-31 03:43:40 +0000158 return BaseT::getIntImmCost(IID, Idx, Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +0000159
160 assert(Ty->isIntegerTy());
161
162 unsigned BitSize = Ty->getPrimitiveSizeInBits();
163 if (BitSize == 0)
164 return ~0U;
165
166 switch (IID) {
Chandler Carruth705b1852015-01-31 03:43:40 +0000167 default:
168 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000169 case Intrinsic::sadd_with_overflow:
170 case Intrinsic::uadd_with_overflow:
171 case Intrinsic::ssub_with_overflow:
172 case Intrinsic::usub_with_overflow:
173 if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<16>(Imm.getSExtValue()))
Chandler Carruth705b1852015-01-31 03:43:40 +0000174 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000175 break;
Hal Finkel934361a2015-01-14 01:07:51 +0000176 case Intrinsic::experimental_stackmap:
177 if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +0000178 return TTI::TCC_Free;
Hal Finkel934361a2015-01-14 01:07:51 +0000179 break;
180 case Intrinsic::experimental_patchpoint_void:
181 case Intrinsic::experimental_patchpoint_i64:
182 if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +0000183 return TTI::TCC_Free;
Hal Finkel934361a2015-01-14 01:07:51 +0000184 break;
Hal Finkel0192cba2014-04-13 23:02:40 +0000185 }
Chandler Carruth705b1852015-01-31 03:43:40 +0000186 return PPCTTIImpl::getIntImmCost(Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +0000187}
188
Chandler Carruth705b1852015-01-31 03:43:40 +0000189unsigned PPCTTIImpl::getIntImmCost(unsigned Opcode, unsigned Idx,
190 const APInt &Imm, Type *Ty) {
Hal Finkel0192cba2014-04-13 23:02:40 +0000191 if (DisablePPCConstHoist)
Chandler Carruth705b1852015-01-31 03:43:40 +0000192 return BaseT::getIntImmCost(Opcode, Idx, Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +0000193
194 assert(Ty->isIntegerTy());
195
196 unsigned BitSize = Ty->getPrimitiveSizeInBits();
197 if (BitSize == 0)
198 return ~0U;
199
200 unsigned ImmIdx = ~0U;
201 bool ShiftedFree = false, RunFree = false, UnsignedFree = false,
202 ZeroFree = false;
203 switch (Opcode) {
Chandler Carruth705b1852015-01-31 03:43:40 +0000204 default:
205 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000206 case Instruction::GetElementPtr:
207 // Always hoist the base address of a GetElementPtr. This prevents the
208 // creation of new constants for every base constant that gets constant
209 // folded with the offset.
210 if (Idx == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +0000211 return 2 * TTI::TCC_Basic;
212 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000213 case Instruction::And:
214 RunFree = true; // (for the rotate-and-mask instructions)
215 // Fallthrough...
216 case Instruction::Add:
217 case Instruction::Or:
218 case Instruction::Xor:
219 ShiftedFree = true;
220 // Fallthrough...
221 case Instruction::Sub:
222 case Instruction::Mul:
223 case Instruction::Shl:
224 case Instruction::LShr:
225 case Instruction::AShr:
226 ImmIdx = 1;
227 break;
228 case Instruction::ICmp:
229 UnsignedFree = true;
230 ImmIdx = 1;
231 // Fallthrough... (zero comparisons can use record-form instructions)
232 case Instruction::Select:
233 ZeroFree = true;
234 break;
235 case Instruction::PHI:
236 case Instruction::Call:
237 case Instruction::Ret:
238 case Instruction::Load:
239 case Instruction::Store:
240 break;
241 }
242
243 if (ZeroFree && Imm == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +0000244 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000245
246 if (Idx == ImmIdx && Imm.getBitWidth() <= 64) {
247 if (isInt<16>(Imm.getSExtValue()))
Chandler Carruth705b1852015-01-31 03:43:40 +0000248 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000249
250 if (RunFree) {
251 if (Imm.getBitWidth() <= 32 &&
252 (isShiftedMask_32(Imm.getZExtValue()) ||
253 isShiftedMask_32(~Imm.getZExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +0000254 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000255
256 if (ST->isPPC64() &&
257 (isShiftedMask_64(Imm.getZExtValue()) ||
258 isShiftedMask_64(~Imm.getZExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +0000259 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000260 }
261
262 if (UnsignedFree && isUInt<16>(Imm.getZExtValue()))
Chandler Carruth705b1852015-01-31 03:43:40 +0000263 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000264
265 if (ShiftedFree && (Imm.getZExtValue() & 0xFFFF) == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +0000266 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000267 }
268
Chandler Carruth705b1852015-01-31 03:43:40 +0000269 return PPCTTIImpl::getIntImmCost(Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +0000270}
271
Chandler Carruth705b1852015-01-31 03:43:40 +0000272void PPCTTIImpl::getUnrollingPreferences(const Function *F, Loop *L,
273 TTI::UnrollingPreferences &UP) {
Eric Christopherd85ffb12014-09-18 00:34:14 +0000274 if (TM->getSubtarget<PPCSubtarget>(F).getDarwinDirective() == PPC::DIR_A2) {
Hal Finkel71780ec2013-09-11 21:20:40 +0000275 // The A2 is in-order with a deep pipeline, and concatenation unrolling
276 // helps expose latency-hiding opportunities to the instruction scheduler.
277 UP.Partial = UP.Runtime = true;
278 }
Hal Finkelb359b732015-01-09 15:51:16 +0000279
Chandler Carruth705b1852015-01-31 03:43:40 +0000280 BaseT::getUnrollingPreferences(F, L, UP);
Hal Finkel71780ec2013-09-11 21:20:40 +0000281}
282
Chandler Carruth705b1852015-01-31 03:43:40 +0000283unsigned PPCTTIImpl::getNumberOfRegisters(bool Vector) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000284 if (Vector && !ST->hasAltivec())
285 return 0;
Hal Finkel27774d92014-03-13 07:58:58 +0000286 return ST->hasVSX() ? 64 : 32;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000287}
288
Chandler Carruth705b1852015-01-31 03:43:40 +0000289unsigned PPCTTIImpl::getRegisterBitWidth(bool Vector) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000290 if (Vector) {
291 if (ST->hasAltivec()) return 128;
292 return 0;
293 }
294
295 if (ST->isPPC64())
296 return 64;
297 return 32;
298
299}
300
Chandler Carruth705b1852015-01-31 03:43:40 +0000301unsigned PPCTTIImpl::getMaxInterleaveFactor() {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000302 unsigned Directive = ST->getDarwinDirective();
303 // The 440 has no SIMD support, but floating-point instructions
304 // have a 5-cycle latency, so unroll by 5x for latency hiding.
305 if (Directive == PPC::DIR_440)
306 return 5;
307
308 // The A2 has no SIMD support, but floating-point instructions
309 // have a 6-cycle latency, so unroll by 6x for latency hiding.
310 if (Directive == PPC::DIR_A2)
311 return 6;
312
313 // FIXME: For lack of any better information, do no harm...
314 if (Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500)
315 return 1;
316
317 // For most things, modern systems have two execution units (and
318 // out-of-order execution).
319 return 2;
320}
321
Chandler Carruth705b1852015-01-31 03:43:40 +0000322unsigned PPCTTIImpl::getArithmeticInstrCost(
323 unsigned Opcode, Type *Ty, TTI::OperandValueKind Op1Info,
324 TTI::OperandValueKind Op2Info, TTI::OperandValueProperties Opd1PropInfo,
325 TTI::OperandValueProperties Opd2PropInfo) {
Dmitri Gribenkoc451bdf2013-01-25 23:17:21 +0000326 assert(TLI->InstructionOpcodeToISD(Opcode) && "Invalid opcode");
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000327
328 // Fallback to the default implementation.
Chandler Carruth705b1852015-01-31 03:43:40 +0000329 return BaseT::getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info,
330 Opd1PropInfo, Opd2PropInfo);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000331}
332
Chandler Carruth705b1852015-01-31 03:43:40 +0000333unsigned PPCTTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index,
334 Type *SubTp) {
335 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000336}
337
Chandler Carruth705b1852015-01-31 03:43:40 +0000338unsigned PPCTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) {
Dmitri Gribenkoc451bdf2013-01-25 23:17:21 +0000339 assert(TLI->InstructionOpcodeToISD(Opcode) && "Invalid opcode");
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000340
Chandler Carruth705b1852015-01-31 03:43:40 +0000341 return BaseT::getCastInstrCost(Opcode, Dst, Src);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000342}
343
Chandler Carruth705b1852015-01-31 03:43:40 +0000344unsigned PPCTTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
345 Type *CondTy) {
346 return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000347}
348
Chandler Carruth705b1852015-01-31 03:43:40 +0000349unsigned PPCTTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val,
350 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 Finkel27774d92014-03-13 07:58:58 +0000362 }
363
Bill Schmidt62fe7a5b2013-02-08 18:19:17 +0000364 // Estimated cost of a load-hit-store delay. This was obtained
365 // experimentally as a minimum needed to prevent unprofitable
366 // vectorization for the paq8p benchmark. It may need to be
367 // raised further if other unprofitable cases remain.
Hal Finkelde0b4132014-04-04 23:51:18 +0000368 unsigned LHSPenalty = 2;
369 if (ISD == ISD::INSERT_VECTOR_ELT)
370 LHSPenalty += 7;
Bill Schmidtb3cece12013-02-07 20:33:57 +0000371
Bill Schmidt62fe7a5b2013-02-08 18:19:17 +0000372 // Vector element insert/extract with Altivec is very expensive,
373 // because they require store and reload with the attendant
374 // processor stall for load-hit-store. Until VSX is available,
375 // these need to be estimated as very costly.
376 if (ISD == ISD::EXTRACT_VECTOR_ELT ||
377 ISD == ISD::INSERT_VECTOR_ELT)
Chandler Carruth705b1852015-01-31 03:43:40 +0000378 return LHSPenalty + BaseT::getVectorInstrCost(Opcode, Val, Index);
Bill Schmidtb3cece12013-02-07 20:33:57 +0000379
Chandler Carruth705b1852015-01-31 03:43:40 +0000380 return BaseT::getVectorInstrCost(Opcode, Val, Index);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000381}
382
Chandler Carruth705b1852015-01-31 03:43:40 +0000383unsigned PPCTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
384 unsigned Alignment,
385 unsigned AddressSpace) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000386 // Legalize the type.
387 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Src);
388 assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
389 "Invalid Opcode");
390
Chandler Carruth705b1852015-01-31 03:43:40 +0000391 unsigned Cost = BaseT::getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000392
Hal Finkelde0b4132014-04-04 23:51:18 +0000393 // VSX loads/stores support unaligned access.
394 if (ST->hasVSX()) {
395 if (LT.second == MVT::v2f64 || LT.second == MVT::v2i64)
396 return Cost;
397 }
398
399 bool UnalignedAltivec =
400 Src->isVectorTy() &&
401 Src->getPrimitiveSizeInBits() >= LT.second.getSizeInBits() &&
402 LT.second.getSizeInBits() == 128 &&
403 Opcode == Instruction::Load;
Hal Finkel6e28e6a2014-03-26 19:39:09 +0000404
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000405 // PPC in general does not support unaligned loads and stores. They'll need
406 // to be decomposed based on the alignment factor.
407 unsigned SrcBytes = LT.second.getStoreSize();
Hal Finkelde0b4132014-04-04 23:51:18 +0000408 if (SrcBytes && Alignment && Alignment < SrcBytes && !UnalignedAltivec) {
Hal Finkelf8233802014-04-02 22:43:49 +0000409 Cost += LT.first*(SrcBytes/Alignment-1);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000410
Hal Finkelde0b4132014-04-04 23:51:18 +0000411 // For a vector type, there is also scalarization overhead (only for
412 // stores, loads are expanded using the vector-load + permutation sequence,
413 // which is much less expensive).
414 if (Src->isVectorTy() && Opcode == Instruction::Store)
415 for (int i = 0, e = Src->getVectorNumElements(); i < e; ++i)
416 Cost += getVectorInstrCost(Instruction::ExtractElement, Src, i);
417 }
418
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000419 return Cost;
420}
421