blob: 0cb88c111bf2ac49f3427a016fa6282b4ece1542 [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
Eric Christopher89f18802014-05-22 01:21:44 +000032// don't have a target-wide initialization entry point, and so we rely on the
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000033// 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:
Craig Topper062a2ba2014-04-25 05:30:21 +000045 PPCTTI() : ImmutablePass(ID), ST(nullptr), TLI(nullptr) {
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()),
Eric Christopherd9134482014-08-04 21:25:23 +000051 TLI(TM->getSubtargetImpl()->getTargetLowering()) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000052 initializePPCTTIPass(*PassRegistry::getPassRegistry());
53 }
54
Craig Topperfd38cbe2014-08-30 16:48:34 +000055 void initializePass() override {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000056 pushTTIStack(this);
57 }
58
Craig Topperfd38cbe2014-08-30 16:48:34 +000059 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 Topperfd38cbe2014-08-30 16:48:34 +000067 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
Craig Topperfd38cbe2014-08-30 16:48:34 +000082 PopcntSupportKind getPopcntSupport(unsigned TyWidth) const override;
83 void getUnrollingPreferences(
Craig Topper73156022014-03-02 09:09:27 +000084 Loop *L, UnrollingPreferences &UP) const override;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000085
86 /// @}
87
88 /// \name Vector TTI Implementations
89 /// @{
90
Craig Topperfd38cbe2014-08-30 16:48:34 +000091 unsigned getNumberOfRegisters(bool Vector) const override;
92 unsigned getRegisterBitWidth(bool Vector) const override;
Sanjay Patelb653de12014-09-10 17:58:16 +000093 unsigned getMaxInterleaveFactor() const override;
Craig Topperfd38cbe2014-08-30 16:48:34 +000094 unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty, OperandValueKind,
95 OperandValueKind, OperandValueProperties,
96 OperandValueProperties) const override;
97 unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
98 int Index, Type *SubTp) const override;
99 unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
100 Type *Src) const override;
101 unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
102 Type *CondTy) const override;
103 unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
104 unsigned Index) const override;
105 unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
106 unsigned AddressSpace) const override;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000107
108 /// @}
109};
110
111} // end anonymous namespace
112
113INITIALIZE_AG_PASS(PPCTTI, TargetTransformInfo, "ppctti",
114 "PPC Target Transform Info", true, true, false)
115char PPCTTI::ID = 0;
116
117ImmutablePass *
118llvm::createPPCTargetTransformInfoPass(const PPCTargetMachine *TM) {
119 return new PPCTTI(TM);
120}
121
122
123//===----------------------------------------------------------------------===//
124//
125// PPC cost model.
126//
127//===----------------------------------------------------------------------===//
128
129PPCTTI::PopcntSupportKind PPCTTI::getPopcntSupport(unsigned TyWidth) const {
130 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
Hal Finkela4d07482013-03-28 13:29:47 +0000131 if (ST->hasPOPCNTD() && TyWidth <= 64)
132 return PSK_FastHardware;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000133 return PSK_Software;
134}
135
Hal Finkel0192cba2014-04-13 23:02:40 +0000136unsigned PPCTTI::getIntImmCost(const APInt &Imm, Type *Ty) const {
137 if (DisablePPCConstHoist)
138 return TargetTransformInfo::getIntImmCost(Imm, Ty);
139
140 assert(Ty->isIntegerTy());
141
142 unsigned BitSize = Ty->getPrimitiveSizeInBits();
143 if (BitSize == 0)
144 return ~0U;
145
146 if (Imm == 0)
147 return TCC_Free;
148
149 if (Imm.getBitWidth() <= 64) {
150 if (isInt<16>(Imm.getSExtValue()))
151 return TCC_Basic;
152
153 if (isInt<32>(Imm.getSExtValue())) {
154 // A constant that can be materialized using lis.
155 if ((Imm.getZExtValue() & 0xFFFF) == 0)
156 return TCC_Basic;
157
158 return 2 * TCC_Basic;
159 }
160 }
161
162 return 4 * TCC_Basic;
163}
164
165unsigned PPCTTI::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
166 const APInt &Imm, Type *Ty) const {
167 if (DisablePPCConstHoist)
168 return TargetTransformInfo::getIntImmCost(IID, Idx, Imm, Ty);
169
170 assert(Ty->isIntegerTy());
171
172 unsigned BitSize = Ty->getPrimitiveSizeInBits();
173 if (BitSize == 0)
174 return ~0U;
175
176 switch (IID) {
177 default: return TCC_Free;
178 case Intrinsic::sadd_with_overflow:
179 case Intrinsic::uadd_with_overflow:
180 case Intrinsic::ssub_with_overflow:
181 case Intrinsic::usub_with_overflow:
182 if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<16>(Imm.getSExtValue()))
183 return TCC_Free;
184 break;
185 }
186 return PPCTTI::getIntImmCost(Imm, Ty);
187}
188
189unsigned PPCTTI::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
190 Type *Ty) const {
191 if (DisablePPCConstHoist)
192 return TargetTransformInfo::getIntImmCost(Opcode, Idx, Imm, Ty);
193
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) {
204 default: return TCC_Free;
205 case Instruction::GetElementPtr:
206 // Always hoist the base address of a GetElementPtr. This prevents the
207 // creation of new constants for every base constant that gets constant
208 // folded with the offset.
209 if (Idx == 0)
210 return 2 * TCC_Basic;
211 return TCC_Free;
212 case Instruction::And:
213 RunFree = true; // (for the rotate-and-mask instructions)
214 // Fallthrough...
215 case Instruction::Add:
216 case Instruction::Or:
217 case Instruction::Xor:
218 ShiftedFree = true;
219 // Fallthrough...
220 case Instruction::Sub:
221 case Instruction::Mul:
222 case Instruction::Shl:
223 case Instruction::LShr:
224 case Instruction::AShr:
225 ImmIdx = 1;
226 break;
227 case Instruction::ICmp:
228 UnsignedFree = true;
229 ImmIdx = 1;
230 // Fallthrough... (zero comparisons can use record-form instructions)
231 case Instruction::Select:
232 ZeroFree = true;
233 break;
234 case Instruction::PHI:
235 case Instruction::Call:
236 case Instruction::Ret:
237 case Instruction::Load:
238 case Instruction::Store:
239 break;
240 }
241
242 if (ZeroFree && Imm == 0)
243 return TCC_Free;
244
245 if (Idx == ImmIdx && Imm.getBitWidth() <= 64) {
246 if (isInt<16>(Imm.getSExtValue()))
247 return TCC_Free;
248
249 if (RunFree) {
250 if (Imm.getBitWidth() <= 32 &&
251 (isShiftedMask_32(Imm.getZExtValue()) ||
252 isShiftedMask_32(~Imm.getZExtValue())))
253 return TCC_Free;
254
255
256 if (ST->isPPC64() &&
257 (isShiftedMask_64(Imm.getZExtValue()) ||
258 isShiftedMask_64(~Imm.getZExtValue())))
259 return TCC_Free;
260 }
261
262 if (UnsignedFree && isUInt<16>(Imm.getZExtValue()))
263 return TCC_Free;
264
265 if (ShiftedFree && (Imm.getZExtValue() & 0xFFFF) == 0)
266 return TCC_Free;
267 }
268
269 return PPCTTI::getIntImmCost(Imm, Ty);
270}
271
Hal Finkel71780ec2013-09-11 21:20:40 +0000272void PPCTTI::getUnrollingPreferences(Loop *L, UnrollingPreferences &UP) const {
273 if (ST->getDarwinDirective() == PPC::DIR_A2) {
274 // The A2 is in-order with a deep pipeline, and concatenation unrolling
275 // helps expose latency-hiding opportunities to the instruction scheduler.
276 UP.Partial = UP.Runtime = true;
277 }
278}
279
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000280unsigned PPCTTI::getNumberOfRegisters(bool Vector) const {
281 if (Vector && !ST->hasAltivec())
282 return 0;
Hal Finkel27774d92014-03-13 07:58:58 +0000283 return ST->hasVSX() ? 64 : 32;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000284}
285
286unsigned PPCTTI::getRegisterBitWidth(bool Vector) const {
287 if (Vector) {
288 if (ST->hasAltivec()) return 128;
289 return 0;
290 }
291
292 if (ST->isPPC64())
293 return 64;
294 return 32;
295
296}
297
Sanjay Patelb653de12014-09-10 17:58:16 +0000298unsigned PPCTTI::getMaxInterleaveFactor() const {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000299 unsigned Directive = ST->getDarwinDirective();
300 // The 440 has no SIMD support, but floating-point instructions
301 // have a 5-cycle latency, so unroll by 5x for latency hiding.
302 if (Directive == PPC::DIR_440)
303 return 5;
304
305 // The A2 has no SIMD support, but floating-point instructions
306 // have a 6-cycle latency, so unroll by 6x for latency hiding.
307 if (Directive == PPC::DIR_A2)
308 return 6;
309
310 // FIXME: For lack of any better information, do no harm...
311 if (Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500)
312 return 1;
313
314 // For most things, modern systems have two execution units (and
315 // out-of-order execution).
316 return 2;
317}
318
Karthik Bhat7f33ff72014-08-25 04:56:54 +0000319unsigned PPCTTI::getArithmeticInstrCost(
320 unsigned Opcode, Type *Ty, OperandValueKind Op1Info,
321 OperandValueKind Op2Info, OperandValueProperties Opd1PropInfo,
322 OperandValueProperties Opd2PropInfo) const {
Dmitri Gribenkoc451bdf2013-01-25 23:17:21 +0000323 assert(TLI->InstructionOpcodeToISD(Opcode) && "Invalid opcode");
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000324
325 // Fallback to the default implementation.
Karthik Bhat7f33ff72014-08-25 04:56:54 +0000326 return TargetTransformInfo::getArithmeticInstrCost(
327 Opcode, Ty, Op1Info, Op2Info, Opd1PropInfo, Opd2PropInfo);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000328}
329
330unsigned PPCTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
331 Type *SubTp) const {
332 return TargetTransformInfo::getShuffleCost(Kind, Tp, Index, SubTp);
333}
334
335unsigned PPCTTI::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) const {
Dmitri Gribenkoc451bdf2013-01-25 23:17:21 +0000336 assert(TLI->InstructionOpcodeToISD(Opcode) && "Invalid opcode");
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000337
338 return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src);
339}
340
341unsigned PPCTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
342 Type *CondTy) const {
343 return TargetTransformInfo::getCmpSelInstrCost(Opcode, ValTy, CondTy);
344}
345
346unsigned PPCTTI::getVectorInstrCost(unsigned Opcode, Type *Val,
347 unsigned Index) const {
348 assert(Val->isVectorTy() && "This must be a vector type");
349
Bill Schmidt62fe7a5b2013-02-08 18:19:17 +0000350 int ISD = TLI->InstructionOpcodeToISD(Opcode);
351 assert(ISD && "Invalid opcode");
Bill Schmidtb3cece12013-02-07 20:33:57 +0000352
Hal Finkel27774d92014-03-13 07:58:58 +0000353 if (ST->hasVSX() && Val->getScalarType()->isDoubleTy()) {
354 // Double-precision scalars are already located in index #0.
355 if (Index == 0)
356 return 0;
357
358 return TargetTransformInfo::getVectorInstrCost(Opcode, Val, Index);
359 }
360
Bill Schmidt62fe7a5b2013-02-08 18:19:17 +0000361 // Estimated cost of a load-hit-store delay. This was obtained
362 // experimentally as a minimum needed to prevent unprofitable
363 // vectorization for the paq8p benchmark. It may need to be
364 // raised further if other unprofitable cases remain.
Hal Finkelde0b4132014-04-04 23:51:18 +0000365 unsigned LHSPenalty = 2;
366 if (ISD == ISD::INSERT_VECTOR_ELT)
367 LHSPenalty += 7;
Bill Schmidtb3cece12013-02-07 20:33:57 +0000368
Bill Schmidt62fe7a5b2013-02-08 18:19:17 +0000369 // Vector element insert/extract with Altivec is very expensive,
370 // because they require store and reload with the attendant
371 // processor stall for load-hit-store. Until VSX is available,
372 // these need to be estimated as very costly.
373 if (ISD == ISD::EXTRACT_VECTOR_ELT ||
374 ISD == ISD::INSERT_VECTOR_ELT)
375 return LHSPenalty +
376 TargetTransformInfo::getVectorInstrCost(Opcode, Val, Index);
Bill Schmidtb3cece12013-02-07 20:33:57 +0000377
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000378 return TargetTransformInfo::getVectorInstrCost(Opcode, Val, Index);
379}
380
381unsigned PPCTTI::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
382 unsigned AddressSpace) const {
383 // Legalize the type.
384 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Src);
385 assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
386 "Invalid Opcode");
387
Hal Finkelf8233802014-04-02 22:43:49 +0000388 unsigned Cost =
389 TargetTransformInfo::getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000390
Hal Finkelde0b4132014-04-04 23:51:18 +0000391 // VSX loads/stores support unaligned access.
392 if (ST->hasVSX()) {
393 if (LT.second == MVT::v2f64 || LT.second == MVT::v2i64)
394 return Cost;
395 }
396
397 bool UnalignedAltivec =
398 Src->isVectorTy() &&
399 Src->getPrimitiveSizeInBits() >= LT.second.getSizeInBits() &&
400 LT.second.getSizeInBits() == 128 &&
401 Opcode == Instruction::Load;
Hal Finkel6e28e6a2014-03-26 19:39:09 +0000402
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000403 // PPC in general does not support unaligned loads and stores. They'll need
404 // to be decomposed based on the alignment factor.
405 unsigned SrcBytes = LT.second.getStoreSize();
Hal Finkelde0b4132014-04-04 23:51:18 +0000406 if (SrcBytes && Alignment && Alignment < SrcBytes && !UnalignedAltivec) {
Hal Finkelf8233802014-04-02 22:43:49 +0000407 Cost += LT.first*(SrcBytes/Alignment-1);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000408
Hal Finkelde0b4132014-04-04 23:51:18 +0000409 // For a vector type, there is also scalarization overhead (only for
410 // stores, loads are expanded using the vector-load + permutation sequence,
411 // which is much less expensive).
412 if (Src->isVectorTy() && Opcode == Instruction::Store)
413 for (int i = 0, e = Src->getVectorNumElements(); i < e; ++i)
414 Cost += getVectorInstrCost(Instruction::ExtractElement, Src, i);
415 }
416
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000417 return Cost;
418}
419