blob: f5ce735bf94358e0a3143ffdee2998f5cfb20701 [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 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;
Karthik Bhat7f33ff72014-08-25 04:56:54 +000095 virtual unsigned
96 getArithmeticInstrCost(unsigned Opcode, Type *Ty, OperandValueKind,
97 OperandValueKind, OperandValueProperties,
98 OperandValueProperties) const override;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000099 virtual unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
Craig Topper73156022014-03-02 09:09:27 +0000100 int Index, Type *SubTp) const override;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000101 virtual unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
Craig Topper73156022014-03-02 09:09:27 +0000102 Type *Src) const override;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000103 virtual unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
Craig Topper73156022014-03-02 09:09:27 +0000104 Type *CondTy) const override;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000105 virtual unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
Craig Topper73156022014-03-02 09:09:27 +0000106 unsigned Index) const override;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000107 virtual unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
108 unsigned Alignment,
Craig Topper73156022014-03-02 09:09:27 +0000109 unsigned AddressSpace) const override;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000110
111 /// @}
112};
113
114} // end anonymous namespace
115
116INITIALIZE_AG_PASS(PPCTTI, TargetTransformInfo, "ppctti",
117 "PPC Target Transform Info", true, true, false)
118char PPCTTI::ID = 0;
119
120ImmutablePass *
121llvm::createPPCTargetTransformInfoPass(const PPCTargetMachine *TM) {
122 return new PPCTTI(TM);
123}
124
125
126//===----------------------------------------------------------------------===//
127//
128// PPC cost model.
129//
130//===----------------------------------------------------------------------===//
131
132PPCTTI::PopcntSupportKind PPCTTI::getPopcntSupport(unsigned TyWidth) const {
133 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
Hal Finkela4d07482013-03-28 13:29:47 +0000134 if (ST->hasPOPCNTD() && TyWidth <= 64)
135 return PSK_FastHardware;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000136 return PSK_Software;
137}
138
Hal Finkel0192cba2014-04-13 23:02:40 +0000139unsigned PPCTTI::getIntImmCost(const APInt &Imm, Type *Ty) const {
140 if (DisablePPCConstHoist)
141 return TargetTransformInfo::getIntImmCost(Imm, Ty);
142
143 assert(Ty->isIntegerTy());
144
145 unsigned BitSize = Ty->getPrimitiveSizeInBits();
146 if (BitSize == 0)
147 return ~0U;
148
149 if (Imm == 0)
150 return TCC_Free;
151
152 if (Imm.getBitWidth() <= 64) {
153 if (isInt<16>(Imm.getSExtValue()))
154 return TCC_Basic;
155
156 if (isInt<32>(Imm.getSExtValue())) {
157 // A constant that can be materialized using lis.
158 if ((Imm.getZExtValue() & 0xFFFF) == 0)
159 return TCC_Basic;
160
161 return 2 * TCC_Basic;
162 }
163 }
164
165 return 4 * TCC_Basic;
166}
167
168unsigned PPCTTI::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
169 const APInt &Imm, Type *Ty) const {
170 if (DisablePPCConstHoist)
171 return TargetTransformInfo::getIntImmCost(IID, Idx, Imm, Ty);
172
173 assert(Ty->isIntegerTy());
174
175 unsigned BitSize = Ty->getPrimitiveSizeInBits();
176 if (BitSize == 0)
177 return ~0U;
178
179 switch (IID) {
180 default: return TCC_Free;
181 case Intrinsic::sadd_with_overflow:
182 case Intrinsic::uadd_with_overflow:
183 case Intrinsic::ssub_with_overflow:
184 case Intrinsic::usub_with_overflow:
185 if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<16>(Imm.getSExtValue()))
186 return TCC_Free;
187 break;
188 }
189 return PPCTTI::getIntImmCost(Imm, Ty);
190}
191
192unsigned PPCTTI::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
193 Type *Ty) const {
194 if (DisablePPCConstHoist)
195 return TargetTransformInfo::getIntImmCost(Opcode, Idx, Imm, Ty);
196
197 assert(Ty->isIntegerTy());
198
199 unsigned BitSize = Ty->getPrimitiveSizeInBits();
200 if (BitSize == 0)
201 return ~0U;
202
203 unsigned ImmIdx = ~0U;
204 bool ShiftedFree = false, RunFree = false, UnsignedFree = false,
205 ZeroFree = false;
206 switch (Opcode) {
207 default: return TCC_Free;
208 case Instruction::GetElementPtr:
209 // Always hoist the base address of a GetElementPtr. This prevents the
210 // creation of new constants for every base constant that gets constant
211 // folded with the offset.
212 if (Idx == 0)
213 return 2 * TCC_Basic;
214 return TCC_Free;
215 case Instruction::And:
216 RunFree = true; // (for the rotate-and-mask instructions)
217 // Fallthrough...
218 case Instruction::Add:
219 case Instruction::Or:
220 case Instruction::Xor:
221 ShiftedFree = true;
222 // Fallthrough...
223 case Instruction::Sub:
224 case Instruction::Mul:
225 case Instruction::Shl:
226 case Instruction::LShr:
227 case Instruction::AShr:
228 ImmIdx = 1;
229 break;
230 case Instruction::ICmp:
231 UnsignedFree = true;
232 ImmIdx = 1;
233 // Fallthrough... (zero comparisons can use record-form instructions)
234 case Instruction::Select:
235 ZeroFree = true;
236 break;
237 case Instruction::PHI:
238 case Instruction::Call:
239 case Instruction::Ret:
240 case Instruction::Load:
241 case Instruction::Store:
242 break;
243 }
244
245 if (ZeroFree && Imm == 0)
246 return TCC_Free;
247
248 if (Idx == ImmIdx && Imm.getBitWidth() <= 64) {
249 if (isInt<16>(Imm.getSExtValue()))
250 return TCC_Free;
251
252 if (RunFree) {
253 if (Imm.getBitWidth() <= 32 &&
254 (isShiftedMask_32(Imm.getZExtValue()) ||
255 isShiftedMask_32(~Imm.getZExtValue())))
256 return TCC_Free;
257
258
259 if (ST->isPPC64() &&
260 (isShiftedMask_64(Imm.getZExtValue()) ||
261 isShiftedMask_64(~Imm.getZExtValue())))
262 return TCC_Free;
263 }
264
265 if (UnsignedFree && isUInt<16>(Imm.getZExtValue()))
266 return TCC_Free;
267
268 if (ShiftedFree && (Imm.getZExtValue() & 0xFFFF) == 0)
269 return TCC_Free;
270 }
271
272 return PPCTTI::getIntImmCost(Imm, Ty);
273}
274
Hal Finkel71780ec2013-09-11 21:20:40 +0000275void PPCTTI::getUnrollingPreferences(Loop *L, UnrollingPreferences &UP) const {
276 if (ST->getDarwinDirective() == PPC::DIR_A2) {
277 // The A2 is in-order with a deep pipeline, and concatenation unrolling
278 // helps expose latency-hiding opportunities to the instruction scheduler.
279 UP.Partial = UP.Runtime = true;
280 }
281}
282
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000283unsigned PPCTTI::getNumberOfRegisters(bool Vector) const {
284 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
289unsigned PPCTTI::getRegisterBitWidth(bool Vector) const {
290 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
301unsigned PPCTTI::getMaximumUnrollFactor() const {
302 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
Karthik Bhat7f33ff72014-08-25 04:56:54 +0000322unsigned PPCTTI::getArithmeticInstrCost(
323 unsigned Opcode, Type *Ty, OperandValueKind Op1Info,
324 OperandValueKind Op2Info, OperandValueProperties Opd1PropInfo,
325 OperandValueProperties Opd2PropInfo) const {
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.
Karthik Bhat7f33ff72014-08-25 04:56:54 +0000329 return TargetTransformInfo::getArithmeticInstrCost(
330 Opcode, Ty, Op1Info, Op2Info, Opd1PropInfo, Opd2PropInfo);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000331}
332
333unsigned PPCTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
334 Type *SubTp) const {
335 return TargetTransformInfo::getShuffleCost(Kind, Tp, Index, SubTp);
336}
337
338unsigned PPCTTI::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) const {
Dmitri Gribenkoc451bdf2013-01-25 23:17:21 +0000339 assert(TLI->InstructionOpcodeToISD(Opcode) && "Invalid opcode");
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000340
341 return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src);
342}
343
344unsigned PPCTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
345 Type *CondTy) const {
346 return TargetTransformInfo::getCmpSelInstrCost(Opcode, ValTy, CondTy);
347}
348
349unsigned PPCTTI::getVectorInstrCost(unsigned Opcode, Type *Val,
350 unsigned Index) const {
351 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
361 return TargetTransformInfo::getVectorInstrCost(Opcode, Val, Index);
362 }
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)
378 return LHSPenalty +
379 TargetTransformInfo::getVectorInstrCost(Opcode, Val, Index);
Bill Schmidtb3cece12013-02-07 20:33:57 +0000380
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000381 return TargetTransformInfo::getVectorInstrCost(Opcode, Val, Index);
382}
383
384unsigned PPCTTI::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
385 unsigned AddressSpace) const {
386 // Legalize the type.
387 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Src);
388 assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
389 "Invalid Opcode");
390
Hal Finkelf8233802014-04-02 22:43:49 +0000391 unsigned Cost =
392 TargetTransformInfo::getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000393
Hal Finkelde0b4132014-04-04 23:51:18 +0000394 // VSX loads/stores support unaligned access.
395 if (ST->hasVSX()) {
396 if (LT.second == MVT::v2f64 || LT.second == MVT::v2i64)
397 return Cost;
398 }
399
400 bool UnalignedAltivec =
401 Src->isVectorTy() &&
402 Src->getPrimitiveSizeInBits() >= LT.second.getSizeInBits() &&
403 LT.second.getSizeInBits() == 128 &&
404 Opcode == Instruction::Load;
Hal Finkel6e28e6a2014-03-26 19:39:09 +0000405
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000406 // PPC in general does not support unaligned loads and stores. They'll need
407 // to be decomposed based on the alignment factor.
408 unsigned SrcBytes = LT.second.getStoreSize();
Hal Finkelde0b4132014-04-04 23:51:18 +0000409 if (SrcBytes && Alignment && Alignment < SrcBytes && !UnalignedAltivec) {
Hal Finkelf8233802014-04-02 22:43:49 +0000410 Cost += LT.first*(SrcBytes/Alignment-1);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000411
Hal Finkelde0b4132014-04-04 23:51:18 +0000412 // For a vector type, there is also scalarization overhead (only for
413 // stores, loads are expanded using the vector-load + permutation sequence,
414 // which is much less expensive).
415 if (Src->isVectorTy() && Opcode == Instruction::Store)
416 for (int i = 0, e = Src->getVectorNumElements(); i < e; ++i)
417 Cost += getVectorInstrCost(Instruction::ExtractElement, Src, i);
418 }
419
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000420 return Cost;
421}
422