blob: 910dde96faa1fad1deeec4b919c72add8567fbbe [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"
Hal Finkel0192cba2014-04-13 23:02:40 +000020#include "llvm/Support/CommandLine.h"
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000021#include "llvm/Support/Debug.h"
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000022#include "llvm/Target/CostTable.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000023#include "llvm/Target/TargetLowering.h"
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000024using namespace llvm;
25
Chandler Carruth84e68b22014-04-22 02:41:26 +000026#define DEBUG_TYPE "ppctti"
27
Hal Finkel0192cba2014-04-13 23:02:40 +000028static cl::opt<bool> DisablePPCConstHoist("disable-ppc-constant-hoisting",
29cl::desc("disable constant hoisting on PPC"), cl::init(false), cl::Hidden);
30
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000031// Declare the pass initialization routine locally as target-specific passes
32// don't havve a target-wide initialization entry point, and so we rely on the
33// pass constructor initialization.
34namespace llvm {
35void initializePPCTTIPass(PassRegistry &);
36}
37
38namespace {
39
Craig Topper77dfe452014-03-02 08:08:51 +000040class PPCTTI final : public ImmutablePass, public TargetTransformInfo {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000041 const PPCSubtarget *ST;
42 const PPCTargetLowering *TLI;
43
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000044public:
Hal Finkel41e9b1d2014-04-05 00:16:28 +000045 PPCTTI() : ImmutablePass(ID), ST(0), TLI(0) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000046 llvm_unreachable("This pass cannot be directly constructed");
47 }
48
49 PPCTTI(const PPCTargetMachine *TM)
Hal Finkel41e9b1d2014-04-05 00:16:28 +000050 : ImmutablePass(ID), ST(TM->getSubtargetImpl()),
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000051 TLI(TM->getTargetLowering()) {
52 initializePPCTTIPass(*PassRegistry::getPassRegistry());
53 }
54
Craig Topper73156022014-03-02 09:09:27 +000055 virtual void initializePass() override {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000056 pushTTIStack(this);
57 }
58
Craig Topper73156022014-03-02 09:09:27 +000059 virtual void getAnalysisUsage(AnalysisUsage &AU) const override {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000060 TargetTransformInfo::getAnalysisUsage(AU);
61 }
62
63 /// Pass identification.
64 static char ID;
65
66 /// Provide necessary pointer adjustments for the two base classes.
Craig Topper73156022014-03-02 09:09:27 +000067 virtual void *getAdjustedAnalysisPointer(const void *ID) override {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000068 if (ID == &TargetTransformInfo::ID)
69 return (TargetTransformInfo*)this;
70 return this;
71 }
72
73 /// \name Scalar TTI Implementations
74 /// @{
Hal Finkel0192cba2014-04-13 23:02:40 +000075 unsigned getIntImmCost(const APInt &Imm, Type *Ty) const override;
76
77 unsigned getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
78 Type *Ty) const override;
79 unsigned getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
80 Type *Ty) const override;
81
Juergen Ributzka3e752e72014-01-24 18:22:59 +000082 virtual PopcntSupportKind
Craig Topper73156022014-03-02 09:09:27 +000083 getPopcntSupport(unsigned TyWidth) const override;
Juergen Ributzka3e752e72014-01-24 18:22:59 +000084 virtual void getUnrollingPreferences(
Craig Topper73156022014-03-02 09:09:27 +000085 Loop *L, UnrollingPreferences &UP) const override;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000086
87 /// @}
88
89 /// \name Vector TTI Implementations
90 /// @{
91
Craig Topper73156022014-03-02 09:09:27 +000092 virtual unsigned getNumberOfRegisters(bool Vector) const override;
93 virtual unsigned getRegisterBitWidth(bool Vector) const override;
94 virtual unsigned getMaximumUnrollFactor() const override;
Arnold Schwaighoferb9773872013-04-04 23:26:21 +000095 virtual unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty,
96 OperandValueKind,
Craig Topper73156022014-03-02 09:09:27 +000097 OperandValueKind) const override;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000098 virtual unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
Craig Topper73156022014-03-02 09:09:27 +000099 int Index, Type *SubTp) const override;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000100 virtual unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
Craig Topper73156022014-03-02 09:09:27 +0000101 Type *Src) const override;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000102 virtual unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
Craig Topper73156022014-03-02 09:09:27 +0000103 Type *CondTy) const override;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000104 virtual unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
Craig Topper73156022014-03-02 09:09:27 +0000105 unsigned Index) const override;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000106 virtual unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
107 unsigned Alignment,
Craig Topper73156022014-03-02 09:09:27 +0000108 unsigned AddressSpace) const override;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000109
110 /// @}
111};
112
113} // end anonymous namespace
114
115INITIALIZE_AG_PASS(PPCTTI, TargetTransformInfo, "ppctti",
116 "PPC Target Transform Info", true, true, false)
117char PPCTTI::ID = 0;
118
119ImmutablePass *
120llvm::createPPCTargetTransformInfoPass(const PPCTargetMachine *TM) {
121 return new PPCTTI(TM);
122}
123
124
125//===----------------------------------------------------------------------===//
126//
127// PPC cost model.
128//
129//===----------------------------------------------------------------------===//
130
131PPCTTI::PopcntSupportKind PPCTTI::getPopcntSupport(unsigned TyWidth) const {
132 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
Hal Finkela4d07482013-03-28 13:29:47 +0000133 if (ST->hasPOPCNTD() && TyWidth <= 64)
134 return PSK_FastHardware;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000135 return PSK_Software;
136}
137
Hal Finkel0192cba2014-04-13 23:02:40 +0000138unsigned PPCTTI::getIntImmCost(const APInt &Imm, Type *Ty) const {
139 if (DisablePPCConstHoist)
140 return TargetTransformInfo::getIntImmCost(Imm, Ty);
141
142 assert(Ty->isIntegerTy());
143
144 unsigned BitSize = Ty->getPrimitiveSizeInBits();
145 if (BitSize == 0)
146 return ~0U;
147
148 if (Imm == 0)
149 return TCC_Free;
150
151 if (Imm.getBitWidth() <= 64) {
152 if (isInt<16>(Imm.getSExtValue()))
153 return TCC_Basic;
154
155 if (isInt<32>(Imm.getSExtValue())) {
156 // A constant that can be materialized using lis.
157 if ((Imm.getZExtValue() & 0xFFFF) == 0)
158 return TCC_Basic;
159
160 return 2 * TCC_Basic;
161 }
162 }
163
164 return 4 * TCC_Basic;
165}
166
167unsigned PPCTTI::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
168 const APInt &Imm, Type *Ty) const {
169 if (DisablePPCConstHoist)
170 return TargetTransformInfo::getIntImmCost(IID, Idx, Imm, Ty);
171
172 assert(Ty->isIntegerTy());
173
174 unsigned BitSize = Ty->getPrimitiveSizeInBits();
175 if (BitSize == 0)
176 return ~0U;
177
178 switch (IID) {
179 default: return TCC_Free;
180 case Intrinsic::sadd_with_overflow:
181 case Intrinsic::uadd_with_overflow:
182 case Intrinsic::ssub_with_overflow:
183 case Intrinsic::usub_with_overflow:
184 if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<16>(Imm.getSExtValue()))
185 return TCC_Free;
186 break;
187 }
188 return PPCTTI::getIntImmCost(Imm, Ty);
189}
190
191unsigned PPCTTI::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
192 Type *Ty) const {
193 if (DisablePPCConstHoist)
194 return TargetTransformInfo::getIntImmCost(Opcode, Idx, Imm, Ty);
195
196 assert(Ty->isIntegerTy());
197
198 unsigned BitSize = Ty->getPrimitiveSizeInBits();
199 if (BitSize == 0)
200 return ~0U;
201
202 unsigned ImmIdx = ~0U;
203 bool ShiftedFree = false, RunFree = false, UnsignedFree = false,
204 ZeroFree = false;
205 switch (Opcode) {
206 default: return TCC_Free;
207 case Instruction::GetElementPtr:
208 // Always hoist the base address of a GetElementPtr. This prevents the
209 // creation of new constants for every base constant that gets constant
210 // folded with the offset.
211 if (Idx == 0)
212 return 2 * TCC_Basic;
213 return TCC_Free;
214 case Instruction::And:
215 RunFree = true; // (for the rotate-and-mask instructions)
216 // Fallthrough...
217 case Instruction::Add:
218 case Instruction::Or:
219 case Instruction::Xor:
220 ShiftedFree = true;
221 // Fallthrough...
222 case Instruction::Sub:
223 case Instruction::Mul:
224 case Instruction::Shl:
225 case Instruction::LShr:
226 case Instruction::AShr:
227 ImmIdx = 1;
228 break;
229 case Instruction::ICmp:
230 UnsignedFree = true;
231 ImmIdx = 1;
232 // Fallthrough... (zero comparisons can use record-form instructions)
233 case Instruction::Select:
234 ZeroFree = true;
235 break;
236 case Instruction::PHI:
237 case Instruction::Call:
238 case Instruction::Ret:
239 case Instruction::Load:
240 case Instruction::Store:
241 break;
242 }
243
244 if (ZeroFree && Imm == 0)
245 return TCC_Free;
246
247 if (Idx == ImmIdx && Imm.getBitWidth() <= 64) {
248 if (isInt<16>(Imm.getSExtValue()))
249 return TCC_Free;
250
251 if (RunFree) {
252 if (Imm.getBitWidth() <= 32 &&
253 (isShiftedMask_32(Imm.getZExtValue()) ||
254 isShiftedMask_32(~Imm.getZExtValue())))
255 return TCC_Free;
256
257
258 if (ST->isPPC64() &&
259 (isShiftedMask_64(Imm.getZExtValue()) ||
260 isShiftedMask_64(~Imm.getZExtValue())))
261 return TCC_Free;
262 }
263
264 if (UnsignedFree && isUInt<16>(Imm.getZExtValue()))
265 return TCC_Free;
266
267 if (ShiftedFree && (Imm.getZExtValue() & 0xFFFF) == 0)
268 return TCC_Free;
269 }
270
271 return PPCTTI::getIntImmCost(Imm, Ty);
272}
273
Hal Finkel71780ec2013-09-11 21:20:40 +0000274void PPCTTI::getUnrollingPreferences(Loop *L, UnrollingPreferences &UP) const {
275 if (ST->getDarwinDirective() == PPC::DIR_A2) {
276 // The A2 is in-order with a deep pipeline, and concatenation unrolling
277 // helps expose latency-hiding opportunities to the instruction scheduler.
278 UP.Partial = UP.Runtime = true;
279 }
280}
281
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000282unsigned PPCTTI::getNumberOfRegisters(bool Vector) const {
283 if (Vector && !ST->hasAltivec())
284 return 0;
Hal Finkel27774d92014-03-13 07:58:58 +0000285 return ST->hasVSX() ? 64 : 32;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000286}
287
288unsigned PPCTTI::getRegisterBitWidth(bool Vector) const {
289 if (Vector) {
290 if (ST->hasAltivec()) return 128;
291 return 0;
292 }
293
294 if (ST->isPPC64())
295 return 64;
296 return 32;
297
298}
299
300unsigned PPCTTI::getMaximumUnrollFactor() const {
301 unsigned Directive = ST->getDarwinDirective();
302 // The 440 has no SIMD support, but floating-point instructions
303 // have a 5-cycle latency, so unroll by 5x for latency hiding.
304 if (Directive == PPC::DIR_440)
305 return 5;
306
307 // The A2 has no SIMD support, but floating-point instructions
308 // have a 6-cycle latency, so unroll by 6x for latency hiding.
309 if (Directive == PPC::DIR_A2)
310 return 6;
311
312 // FIXME: For lack of any better information, do no harm...
313 if (Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500)
314 return 1;
315
316 // For most things, modern systems have two execution units (and
317 // out-of-order execution).
318 return 2;
319}
320
Arnold Schwaighoferb9773872013-04-04 23:26:21 +0000321unsigned PPCTTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty,
322 OperandValueKind Op1Info,
323 OperandValueKind Op2Info) const {
Dmitri Gribenkoc451bdf2013-01-25 23:17:21 +0000324 assert(TLI->InstructionOpcodeToISD(Opcode) && "Invalid opcode");
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000325
326 // Fallback to the default implementation.
Arnold Schwaighoferb9773872013-04-04 23:26:21 +0000327 return TargetTransformInfo::getArithmeticInstrCost(Opcode, Ty, Op1Info,
328 Op2Info);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000329}
330
331unsigned PPCTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
332 Type *SubTp) const {
333 return TargetTransformInfo::getShuffleCost(Kind, Tp, Index, SubTp);
334}
335
336unsigned PPCTTI::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) const {
Dmitri Gribenkoc451bdf2013-01-25 23:17:21 +0000337 assert(TLI->InstructionOpcodeToISD(Opcode) && "Invalid opcode");
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000338
339 return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src);
340}
341
342unsigned PPCTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
343 Type *CondTy) const {
344 return TargetTransformInfo::getCmpSelInstrCost(Opcode, ValTy, CondTy);
345}
346
347unsigned PPCTTI::getVectorInstrCost(unsigned Opcode, Type *Val,
348 unsigned Index) const {
349 assert(Val->isVectorTy() && "This must be a vector type");
350
Bill Schmidt62fe7a5b2013-02-08 18:19:17 +0000351 int ISD = TLI->InstructionOpcodeToISD(Opcode);
352 assert(ISD && "Invalid opcode");
Bill Schmidtb3cece12013-02-07 20:33:57 +0000353
Hal Finkel27774d92014-03-13 07:58:58 +0000354 if (ST->hasVSX() && Val->getScalarType()->isDoubleTy()) {
355 // Double-precision scalars are already located in index #0.
356 if (Index == 0)
357 return 0;
358
359 return TargetTransformInfo::getVectorInstrCost(Opcode, Val, Index);
360 }
361
Bill Schmidt62fe7a5b2013-02-08 18:19:17 +0000362 // Estimated cost of a load-hit-store delay. This was obtained
363 // experimentally as a minimum needed to prevent unprofitable
364 // vectorization for the paq8p benchmark. It may need to be
365 // raised further if other unprofitable cases remain.
Hal Finkelde0b4132014-04-04 23:51:18 +0000366 unsigned LHSPenalty = 2;
367 if (ISD == ISD::INSERT_VECTOR_ELT)
368 LHSPenalty += 7;
Bill Schmidtb3cece12013-02-07 20:33:57 +0000369
Bill Schmidt62fe7a5b2013-02-08 18:19:17 +0000370 // Vector element insert/extract with Altivec is very expensive,
371 // because they require store and reload with the attendant
372 // processor stall for load-hit-store. Until VSX is available,
373 // these need to be estimated as very costly.
374 if (ISD == ISD::EXTRACT_VECTOR_ELT ||
375 ISD == ISD::INSERT_VECTOR_ELT)
376 return LHSPenalty +
377 TargetTransformInfo::getVectorInstrCost(Opcode, Val, Index);
Bill Schmidtb3cece12013-02-07 20:33:57 +0000378
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000379 return TargetTransformInfo::getVectorInstrCost(Opcode, Val, Index);
380}
381
382unsigned PPCTTI::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
383 unsigned AddressSpace) const {
384 // Legalize the type.
385 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Src);
386 assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
387 "Invalid Opcode");
388
Hal Finkelf8233802014-04-02 22:43:49 +0000389 unsigned Cost =
390 TargetTransformInfo::getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000391
Hal Finkelde0b4132014-04-04 23:51:18 +0000392 // VSX loads/stores support unaligned access.
393 if (ST->hasVSX()) {
394 if (LT.second == MVT::v2f64 || LT.second == MVT::v2i64)
395 return Cost;
396 }
397
398 bool UnalignedAltivec =
399 Src->isVectorTy() &&
400 Src->getPrimitiveSizeInBits() >= LT.second.getSizeInBits() &&
401 LT.second.getSizeInBits() == 128 &&
402 Opcode == Instruction::Load;
Hal Finkel6e28e6a2014-03-26 19:39:09 +0000403
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000404 // PPC in general does not support unaligned loads and stores. They'll need
405 // to be decomposed based on the alignment factor.
406 unsigned SrcBytes = LT.second.getStoreSize();
Hal Finkelde0b4132014-04-04 23:51:18 +0000407 if (SrcBytes && Alignment && Alignment < SrcBytes && !UnalignedAltivec) {
Hal Finkelf8233802014-04-02 22:43:49 +0000408 Cost += LT.first*(SrcBytes/Alignment-1);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000409
Hal Finkelde0b4132014-04-04 23:51:18 +0000410 // For a vector type, there is also scalarization overhead (only for
411 // stores, loads are expanded using the vector-load + permutation sequence,
412 // which is much less expensive).
413 if (Src->isVectorTy() && Opcode == Instruction::Store)
414 for (int i = 0, e = Src->getVectorNumElements(); i < e; ++i)
415 Cost += getVectorInstrCost(Instruction::ExtractElement, Src, i);
416 }
417
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000418 return Cost;
419}
420