blob: 37624ed93d3aec87ff9e49c5824d14c4439a6920 [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 {
Eric Christopherd85ffb12014-09-18 00:34:14 +000041 const TargetMachine *TM;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000042 const PPCSubtarget *ST;
43 const PPCTargetLowering *TLI;
44
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000045public:
Craig Topper062a2ba2014-04-25 05:30:21 +000046 PPCTTI() : ImmutablePass(ID), ST(nullptr), TLI(nullptr) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000047 llvm_unreachable("This pass cannot be directly constructed");
48 }
49
50 PPCTTI(const PPCTargetMachine *TM)
Eric Christopherd85ffb12014-09-18 00:34:14 +000051 : ImmutablePass(ID), TM(TM), ST(TM->getSubtargetImpl()),
Eric Christopherd9134482014-08-04 21:25:23 +000052 TLI(TM->getSubtargetImpl()->getTargetLowering()) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000053 initializePPCTTIPass(*PassRegistry::getPassRegistry());
54 }
55
Craig Topperfd38cbe2014-08-30 16:48:34 +000056 void initializePass() override {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000057 pushTTIStack(this);
58 }
59
Craig Topperfd38cbe2014-08-30 16:48:34 +000060 void getAnalysisUsage(AnalysisUsage &AU) const override {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000061 TargetTransformInfo::getAnalysisUsage(AU);
62 }
63
64 /// Pass identification.
65 static char ID;
66
67 /// Provide necessary pointer adjustments for the two base classes.
Craig Topperfd38cbe2014-08-30 16:48:34 +000068 void *getAdjustedAnalysisPointer(const void *ID) override {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000069 if (ID == &TargetTransformInfo::ID)
70 return (TargetTransformInfo*)this;
71 return this;
72 }
73
74 /// \name Scalar TTI Implementations
75 /// @{
Hal Finkel0192cba2014-04-13 23:02:40 +000076 unsigned getIntImmCost(const APInt &Imm, Type *Ty) const override;
77
78 unsigned getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
79 Type *Ty) const override;
80 unsigned getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
81 Type *Ty) const override;
82
Craig Topperfd38cbe2014-08-30 16:48:34 +000083 PopcntSupportKind getPopcntSupport(unsigned TyWidth) const override;
Eric Christopherd85ffb12014-09-18 00:34:14 +000084 void getUnrollingPreferences(const Function *F, Loop *L,
85 UnrollingPreferences &UP) const override;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000086
87 /// @}
88
89 /// \name Vector TTI Implementations
90 /// @{
91
Craig Topperfd38cbe2014-08-30 16:48:34 +000092 unsigned getNumberOfRegisters(bool Vector) const override;
93 unsigned getRegisterBitWidth(bool Vector) const override;
Sanjay Patelb653de12014-09-10 17:58:16 +000094 unsigned getMaxInterleaveFactor() const override;
Craig Topperfd38cbe2014-08-30 16:48:34 +000095 unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty, OperandValueKind,
96 OperandValueKind, OperandValueProperties,
97 OperandValueProperties) const override;
98 unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
99 int Index, Type *SubTp) const override;
100 unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
101 Type *Src) const override;
102 unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
103 Type *CondTy) const override;
104 unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
105 unsigned Index) const override;
106 unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
107 unsigned AddressSpace) const override;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000108
109 /// @}
110};
111
112} // end anonymous namespace
113
114INITIALIZE_AG_PASS(PPCTTI, TargetTransformInfo, "ppctti",
115 "PPC Target Transform Info", true, true, false)
116char PPCTTI::ID = 0;
117
118ImmutablePass *
119llvm::createPPCTargetTransformInfoPass(const PPCTargetMachine *TM) {
120 return new PPCTTI(TM);
121}
122
123
124//===----------------------------------------------------------------------===//
125//
126// PPC cost model.
127//
128//===----------------------------------------------------------------------===//
129
130PPCTTI::PopcntSupportKind PPCTTI::getPopcntSupport(unsigned TyWidth) const {
131 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
Hal Finkela4d07482013-03-28 13:29:47 +0000132 if (ST->hasPOPCNTD() && TyWidth <= 64)
133 return PSK_FastHardware;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000134 return PSK_Software;
135}
136
Hal Finkel0192cba2014-04-13 23:02:40 +0000137unsigned PPCTTI::getIntImmCost(const APInt &Imm, Type *Ty) const {
138 if (DisablePPCConstHoist)
139 return TargetTransformInfo::getIntImmCost(Imm, Ty);
140
141 assert(Ty->isIntegerTy());
142
143 unsigned BitSize = Ty->getPrimitiveSizeInBits();
144 if (BitSize == 0)
145 return ~0U;
146
147 if (Imm == 0)
148 return TCC_Free;
149
150 if (Imm.getBitWidth() <= 64) {
151 if (isInt<16>(Imm.getSExtValue()))
152 return TCC_Basic;
153
154 if (isInt<32>(Imm.getSExtValue())) {
155 // A constant that can be materialized using lis.
156 if ((Imm.getZExtValue() & 0xFFFF) == 0)
157 return TCC_Basic;
158
159 return 2 * TCC_Basic;
160 }
161 }
162
163 return 4 * TCC_Basic;
164}
165
166unsigned PPCTTI::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
167 const APInt &Imm, Type *Ty) const {
168 if (DisablePPCConstHoist)
169 return TargetTransformInfo::getIntImmCost(IID, Idx, Imm, Ty);
170
171 assert(Ty->isIntegerTy());
172
173 unsigned BitSize = Ty->getPrimitiveSizeInBits();
174 if (BitSize == 0)
175 return ~0U;
176
177 switch (IID) {
178 default: return TCC_Free;
179 case Intrinsic::sadd_with_overflow:
180 case Intrinsic::uadd_with_overflow:
181 case Intrinsic::ssub_with_overflow:
182 case Intrinsic::usub_with_overflow:
183 if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<16>(Imm.getSExtValue()))
184 return TCC_Free;
185 break;
186 }
187 return PPCTTI::getIntImmCost(Imm, Ty);
188}
189
190unsigned PPCTTI::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
191 Type *Ty) const {
192 if (DisablePPCConstHoist)
193 return TargetTransformInfo::getIntImmCost(Opcode, Idx, Imm, Ty);
194
195 assert(Ty->isIntegerTy());
196
197 unsigned BitSize = Ty->getPrimitiveSizeInBits();
198 if (BitSize == 0)
199 return ~0U;
200
201 unsigned ImmIdx = ~0U;
202 bool ShiftedFree = false, RunFree = false, UnsignedFree = false,
203 ZeroFree = false;
204 switch (Opcode) {
205 default: return TCC_Free;
206 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)
211 return 2 * TCC_Basic;
212 return TCC_Free;
213 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)
244 return TCC_Free;
245
246 if (Idx == ImmIdx && Imm.getBitWidth() <= 64) {
247 if (isInt<16>(Imm.getSExtValue()))
248 return TCC_Free;
249
250 if (RunFree) {
251 if (Imm.getBitWidth() <= 32 &&
252 (isShiftedMask_32(Imm.getZExtValue()) ||
253 isShiftedMask_32(~Imm.getZExtValue())))
254 return TCC_Free;
255
256
257 if (ST->isPPC64() &&
258 (isShiftedMask_64(Imm.getZExtValue()) ||
259 isShiftedMask_64(~Imm.getZExtValue())))
260 return TCC_Free;
261 }
262
263 if (UnsignedFree && isUInt<16>(Imm.getZExtValue()))
264 return TCC_Free;
265
266 if (ShiftedFree && (Imm.getZExtValue() & 0xFFFF) == 0)
267 return TCC_Free;
268 }
269
270 return PPCTTI::getIntImmCost(Imm, Ty);
271}
272
Eric Christopherd85ffb12014-09-18 00:34:14 +0000273void PPCTTI::getUnrollingPreferences(const Function *F, Loop *L,
274 UnrollingPreferences &UP) const {
275 if (TM->getSubtarget<PPCSubtarget>(F).getDarwinDirective() == PPC::DIR_A2) {
Hal Finkel71780ec2013-09-11 21:20:40 +0000276 // 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
Sanjay Patelb653de12014-09-10 17:58:16 +0000300unsigned PPCTTI::getMaxInterleaveFactor() const {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000301 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
Karthik Bhat7f33ff72014-08-25 04:56:54 +0000321unsigned PPCTTI::getArithmeticInstrCost(
322 unsigned Opcode, Type *Ty, OperandValueKind Op1Info,
323 OperandValueKind Op2Info, OperandValueProperties Opd1PropInfo,
324 OperandValueProperties Opd2PropInfo) const {
Dmitri Gribenkoc451bdf2013-01-25 23:17:21 +0000325 assert(TLI->InstructionOpcodeToISD(Opcode) && "Invalid opcode");
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000326
327 // Fallback to the default implementation.
Karthik Bhat7f33ff72014-08-25 04:56:54 +0000328 return TargetTransformInfo::getArithmeticInstrCost(
329 Opcode, Ty, Op1Info, Op2Info, Opd1PropInfo, Opd2PropInfo);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000330}
331
332unsigned PPCTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
333 Type *SubTp) const {
334 return TargetTransformInfo::getShuffleCost(Kind, Tp, Index, SubTp);
335}
336
337unsigned PPCTTI::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) const {
Dmitri Gribenkoc451bdf2013-01-25 23:17:21 +0000338 assert(TLI->InstructionOpcodeToISD(Opcode) && "Invalid opcode");
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000339
340 return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src);
341}
342
343unsigned PPCTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
344 Type *CondTy) const {
345 return TargetTransformInfo::getCmpSelInstrCost(Opcode, ValTy, CondTy);
346}
347
348unsigned PPCTTI::getVectorInstrCost(unsigned Opcode, Type *Val,
349 unsigned Index) const {
350 assert(Val->isVectorTy() && "This must be a vector type");
351
Bill Schmidt62fe7a5b2013-02-08 18:19:17 +0000352 int ISD = TLI->InstructionOpcodeToISD(Opcode);
353 assert(ISD && "Invalid opcode");
Bill Schmidtb3cece12013-02-07 20:33:57 +0000354
Hal Finkel27774d92014-03-13 07:58:58 +0000355 if (ST->hasVSX() && Val->getScalarType()->isDoubleTy()) {
356 // Double-precision scalars are already located in index #0.
357 if (Index == 0)
358 return 0;
359
360 return TargetTransformInfo::getVectorInstrCost(Opcode, Val, Index);
361 }
362
Bill Schmidt62fe7a5b2013-02-08 18:19:17 +0000363 // Estimated cost of a load-hit-store delay. This was obtained
364 // experimentally as a minimum needed to prevent unprofitable
365 // vectorization for the paq8p benchmark. It may need to be
366 // raised further if other unprofitable cases remain.
Hal Finkelde0b4132014-04-04 23:51:18 +0000367 unsigned LHSPenalty = 2;
368 if (ISD == ISD::INSERT_VECTOR_ELT)
369 LHSPenalty += 7;
Bill Schmidtb3cece12013-02-07 20:33:57 +0000370
Bill Schmidt62fe7a5b2013-02-08 18:19:17 +0000371 // Vector element insert/extract with Altivec is very expensive,
372 // because they require store and reload with the attendant
373 // processor stall for load-hit-store. Until VSX is available,
374 // these need to be estimated as very costly.
375 if (ISD == ISD::EXTRACT_VECTOR_ELT ||
376 ISD == ISD::INSERT_VECTOR_ELT)
377 return LHSPenalty +
378 TargetTransformInfo::getVectorInstrCost(Opcode, Val, Index);
Bill Schmidtb3cece12013-02-07 20:33:57 +0000379
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000380 return TargetTransformInfo::getVectorInstrCost(Opcode, Val, Index);
381}
382
383unsigned PPCTTI::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
384 unsigned AddressSpace) const {
385 // Legalize the type.
386 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Src);
387 assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
388 "Invalid Opcode");
389
Hal Finkelf8233802014-04-02 22:43:49 +0000390 unsigned Cost =
391 TargetTransformInfo::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