blob: cf36789b3d22a04e12962ef44b49090a1c8f7a41 [file] [log] [blame]
Chandler Carruth93dcdc42015-01-31 11:17:59 +00001//===-- PPCTargetTransformInfo.cpp - PPC specific TTI ---------------------===//
Hal Finkel4e5ca9e2013-01-25 23:05:59 +00002//
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//===----------------------------------------------------------------------===//
Hal Finkel4e5ca9e2013-01-25 23:05:59 +00009
Chandler Carruth93dcdc42015-01-31 11:17:59 +000010#include "PPCTargetTransformInfo.h"
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000011#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruth705b1852015-01-31 03:43:40 +000012#include "llvm/CodeGen/BasicTTIImpl.h"
Hal Finkel0192cba2014-04-13 23:02:40 +000013#include "llvm/Support/CommandLine.h"
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000014#include "llvm/Support/Debug.h"
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000015#include "llvm/Target/CostTable.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "llvm/Target/TargetLowering.h"
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000017using namespace llvm;
18
Chandler Carruth84e68b22014-04-22 02:41:26 +000019#define DEBUG_TYPE "ppctti"
20
Hal Finkel0192cba2014-04-13 23:02:40 +000021static cl::opt<bool> DisablePPCConstHoist("disable-ppc-constant-hoisting",
22cl::desc("disable constant hoisting on PPC"), cl::init(false), cl::Hidden);
23
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000024//===----------------------------------------------------------------------===//
25//
26// PPC cost model.
27//
28//===----------------------------------------------------------------------===//
29
Chandler Carruth705b1852015-01-31 03:43:40 +000030TargetTransformInfo::PopcntSupportKind
31PPCTTIImpl::getPopcntSupport(unsigned TyWidth) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000032 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
Hal Finkela4d07482013-03-28 13:29:47 +000033 if (ST->hasPOPCNTD() && TyWidth <= 64)
Chandler Carruth705b1852015-01-31 03:43:40 +000034 return TTI::PSK_FastHardware;
35 return TTI::PSK_Software;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +000036}
37
Chandler Carruth93205eb2015-08-05 18:08:10 +000038int PPCTTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) {
Hal Finkel0192cba2014-04-13 23:02:40 +000039 if (DisablePPCConstHoist)
Chandler Carruth705b1852015-01-31 03:43:40 +000040 return BaseT::getIntImmCost(Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +000041
42 assert(Ty->isIntegerTy());
43
44 unsigned BitSize = Ty->getPrimitiveSizeInBits();
45 if (BitSize == 0)
46 return ~0U;
47
48 if (Imm == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +000049 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +000050
51 if (Imm.getBitWidth() <= 64) {
52 if (isInt<16>(Imm.getSExtValue()))
Chandler Carruth705b1852015-01-31 03:43:40 +000053 return TTI::TCC_Basic;
Hal Finkel0192cba2014-04-13 23:02:40 +000054
55 if (isInt<32>(Imm.getSExtValue())) {
56 // A constant that can be materialized using lis.
57 if ((Imm.getZExtValue() & 0xFFFF) == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +000058 return TTI::TCC_Basic;
Hal Finkel0192cba2014-04-13 23:02:40 +000059
Chandler Carruth705b1852015-01-31 03:43:40 +000060 return 2 * TTI::TCC_Basic;
Hal Finkel0192cba2014-04-13 23:02:40 +000061 }
62 }
63
Chandler Carruth705b1852015-01-31 03:43:40 +000064 return 4 * TTI::TCC_Basic;
Hal Finkel0192cba2014-04-13 23:02:40 +000065}
66
Chandler Carruth93205eb2015-08-05 18:08:10 +000067int PPCTTIImpl::getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
68 Type *Ty) {
Hal Finkel0192cba2014-04-13 23:02:40 +000069 if (DisablePPCConstHoist)
Chandler Carruth705b1852015-01-31 03:43:40 +000070 return BaseT::getIntImmCost(IID, Idx, Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +000071
72 assert(Ty->isIntegerTy());
73
74 unsigned BitSize = Ty->getPrimitiveSizeInBits();
75 if (BitSize == 0)
76 return ~0U;
77
78 switch (IID) {
Chandler Carruth705b1852015-01-31 03:43:40 +000079 default:
80 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +000081 case Intrinsic::sadd_with_overflow:
82 case Intrinsic::uadd_with_overflow:
83 case Intrinsic::ssub_with_overflow:
84 case Intrinsic::usub_with_overflow:
85 if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<16>(Imm.getSExtValue()))
Chandler Carruth705b1852015-01-31 03:43:40 +000086 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +000087 break;
Hal Finkel934361a2015-01-14 01:07:51 +000088 case Intrinsic::experimental_stackmap:
89 if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +000090 return TTI::TCC_Free;
Hal Finkel934361a2015-01-14 01:07:51 +000091 break;
92 case Intrinsic::experimental_patchpoint_void:
93 case Intrinsic::experimental_patchpoint_i64:
94 if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +000095 return TTI::TCC_Free;
Hal Finkel934361a2015-01-14 01:07:51 +000096 break;
Hal Finkel0192cba2014-04-13 23:02:40 +000097 }
Chandler Carruth705b1852015-01-31 03:43:40 +000098 return PPCTTIImpl::getIntImmCost(Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +000099}
100
Chandler Carruth93205eb2015-08-05 18:08:10 +0000101int PPCTTIImpl::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
102 Type *Ty) {
Hal Finkel0192cba2014-04-13 23:02:40 +0000103 if (DisablePPCConstHoist)
Chandler Carruth705b1852015-01-31 03:43:40 +0000104 return BaseT::getIntImmCost(Opcode, Idx, Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +0000105
106 assert(Ty->isIntegerTy());
107
108 unsigned BitSize = Ty->getPrimitiveSizeInBits();
109 if (BitSize == 0)
110 return ~0U;
111
112 unsigned ImmIdx = ~0U;
113 bool ShiftedFree = false, RunFree = false, UnsignedFree = false,
114 ZeroFree = false;
115 switch (Opcode) {
Chandler Carruth705b1852015-01-31 03:43:40 +0000116 default:
117 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000118 case Instruction::GetElementPtr:
119 // Always hoist the base address of a GetElementPtr. This prevents the
120 // creation of new constants for every base constant that gets constant
121 // folded with the offset.
122 if (Idx == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +0000123 return 2 * TTI::TCC_Basic;
124 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000125 case Instruction::And:
126 RunFree = true; // (for the rotate-and-mask instructions)
127 // Fallthrough...
128 case Instruction::Add:
129 case Instruction::Or:
130 case Instruction::Xor:
131 ShiftedFree = true;
132 // Fallthrough...
133 case Instruction::Sub:
134 case Instruction::Mul:
135 case Instruction::Shl:
136 case Instruction::LShr:
137 case Instruction::AShr:
138 ImmIdx = 1;
139 break;
140 case Instruction::ICmp:
141 UnsignedFree = true;
142 ImmIdx = 1;
143 // Fallthrough... (zero comparisons can use record-form instructions)
144 case Instruction::Select:
145 ZeroFree = true;
146 break;
147 case Instruction::PHI:
148 case Instruction::Call:
149 case Instruction::Ret:
150 case Instruction::Load:
151 case Instruction::Store:
152 break;
153 }
154
155 if (ZeroFree && Imm == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +0000156 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000157
158 if (Idx == ImmIdx && Imm.getBitWidth() <= 64) {
159 if (isInt<16>(Imm.getSExtValue()))
Chandler Carruth705b1852015-01-31 03:43:40 +0000160 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000161
162 if (RunFree) {
163 if (Imm.getBitWidth() <= 32 &&
164 (isShiftedMask_32(Imm.getZExtValue()) ||
165 isShiftedMask_32(~Imm.getZExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +0000166 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000167
168 if (ST->isPPC64() &&
169 (isShiftedMask_64(Imm.getZExtValue()) ||
170 isShiftedMask_64(~Imm.getZExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +0000171 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000172 }
173
174 if (UnsignedFree && isUInt<16>(Imm.getZExtValue()))
Chandler Carruth705b1852015-01-31 03:43:40 +0000175 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000176
177 if (ShiftedFree && (Imm.getZExtValue() & 0xFFFF) == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +0000178 return TTI::TCC_Free;
Hal Finkel0192cba2014-04-13 23:02:40 +0000179 }
180
Chandler Carruth705b1852015-01-31 03:43:40 +0000181 return PPCTTIImpl::getIntImmCost(Imm, Ty);
Hal Finkel0192cba2014-04-13 23:02:40 +0000182}
183
Chandler Carruthab5cb362015-02-01 14:31:23 +0000184void PPCTTIImpl::getUnrollingPreferences(Loop *L,
Chandler Carruth705b1852015-01-31 03:43:40 +0000185 TTI::UnrollingPreferences &UP) {
Chandler Carruthc956ab662015-02-01 14:22:17 +0000186 if (ST->getDarwinDirective() == PPC::DIR_A2) {
Hal Finkel71780ec2013-09-11 21:20:40 +0000187 // The A2 is in-order with a deep pipeline, and concatenation unrolling
188 // helps expose latency-hiding opportunities to the instruction scheduler.
189 UP.Partial = UP.Runtime = true;
Hal Finkel3b3c9c32015-05-21 20:30:23 +0000190
191 // We unroll a lot on the A2 (hundreds of instructions), and the benefits
192 // often outweigh the cost of a division to compute the trip count.
193 UP.AllowExpensiveTripCount = true;
Hal Finkel71780ec2013-09-11 21:20:40 +0000194 }
Hal Finkelb359b732015-01-09 15:51:16 +0000195
Chandler Carruthab5cb362015-02-01 14:31:23 +0000196 BaseT::getUnrollingPreferences(L, UP);
Hal Finkel71780ec2013-09-11 21:20:40 +0000197}
198
Olivier Sallenave049d8032015-03-06 23:12:04 +0000199bool PPCTTIImpl::enableAggressiveInterleaving(bool LoopHasReductions) {
200 return LoopHasReductions;
201}
202
Chandler Carruth705b1852015-01-31 03:43:40 +0000203unsigned PPCTTIImpl::getNumberOfRegisters(bool Vector) {
Hal Finkelc93a9a22015-02-25 01:06:45 +0000204 if (Vector && !ST->hasAltivec() && !ST->hasQPX())
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000205 return 0;
Hal Finkel27774d92014-03-13 07:58:58 +0000206 return ST->hasVSX() ? 64 : 32;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000207}
208
Chandler Carruth705b1852015-01-31 03:43:40 +0000209unsigned PPCTTIImpl::getRegisterBitWidth(bool Vector) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000210 if (Vector) {
Hal Finkelc93a9a22015-02-25 01:06:45 +0000211 if (ST->hasQPX()) return 256;
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000212 if (ST->hasAltivec()) return 128;
213 return 0;
214 }
215
216 if (ST->isPPC64())
217 return 64;
218 return 32;
219
220}
221
Wei Mi062c7442015-05-06 17:12:25 +0000222unsigned PPCTTIImpl::getMaxInterleaveFactor(unsigned VF) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000223 unsigned Directive = ST->getDarwinDirective();
224 // The 440 has no SIMD support, but floating-point instructions
225 // have a 5-cycle latency, so unroll by 5x for latency hiding.
226 if (Directive == PPC::DIR_440)
227 return 5;
228
229 // The A2 has no SIMD support, but floating-point instructions
230 // have a 6-cycle latency, so unroll by 6x for latency hiding.
231 if (Directive == PPC::DIR_A2)
232 return 6;
233
234 // FIXME: For lack of any better information, do no harm...
235 if (Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500)
236 return 1;
237
Olivier Sallenave05e69152015-02-12 22:57:58 +0000238 // For P7 and P8, floating-point instructions have a 6-cycle latency and
239 // there are two execution units, so unroll by 12x for latency hiding.
240 if (Directive == PPC::DIR_PWR7 ||
241 Directive == PPC::DIR_PWR8)
242 return 12;
243
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000244 // For most things, modern systems have two execution units (and
245 // out-of-order execution).
246 return 2;
247}
248
Chandler Carruth93205eb2015-08-05 18:08:10 +0000249int PPCTTIImpl::getArithmeticInstrCost(
Chandler Carruth705b1852015-01-31 03:43:40 +0000250 unsigned Opcode, Type *Ty, TTI::OperandValueKind Op1Info,
251 TTI::OperandValueKind Op2Info, TTI::OperandValueProperties Opd1PropInfo,
252 TTI::OperandValueProperties Opd2PropInfo) {
Dmitri Gribenkoc451bdf2013-01-25 23:17:21 +0000253 assert(TLI->InstructionOpcodeToISD(Opcode) && "Invalid opcode");
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000254
255 // Fallback to the default implementation.
Chandler Carruth705b1852015-01-31 03:43:40 +0000256 return BaseT::getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info,
257 Opd1PropInfo, Opd2PropInfo);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000258}
259
Chandler Carruth93205eb2015-08-05 18:08:10 +0000260int PPCTTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index,
261 Type *SubTp) {
Chandler Carruth705b1852015-01-31 03:43:40 +0000262 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000263}
264
Chandler Carruth93205eb2015-08-05 18:08:10 +0000265int PPCTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) {
Dmitri Gribenkoc451bdf2013-01-25 23:17:21 +0000266 assert(TLI->InstructionOpcodeToISD(Opcode) && "Invalid opcode");
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000267
Chandler Carruth705b1852015-01-31 03:43:40 +0000268 return BaseT::getCastInstrCost(Opcode, Dst, Src);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000269}
270
Chandler Carruth93205eb2015-08-05 18:08:10 +0000271int PPCTTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy) {
Chandler Carruth705b1852015-01-31 03:43:40 +0000272 return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000273}
274
Chandler Carruth93205eb2015-08-05 18:08:10 +0000275int PPCTTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000276 assert(Val->isVectorTy() && "This must be a vector type");
277
Bill Schmidt62fe7a5b2013-02-08 18:19:17 +0000278 int ISD = TLI->InstructionOpcodeToISD(Opcode);
279 assert(ISD && "Invalid opcode");
Bill Schmidtb3cece12013-02-07 20:33:57 +0000280
Hal Finkel27774d92014-03-13 07:58:58 +0000281 if (ST->hasVSX() && Val->getScalarType()->isDoubleTy()) {
282 // Double-precision scalars are already located in index #0.
283 if (Index == 0)
284 return 0;
285
Chandler Carruth705b1852015-01-31 03:43:40 +0000286 return BaseT::getVectorInstrCost(Opcode, Val, Index);
Hal Finkelc93a9a22015-02-25 01:06:45 +0000287 } else if (ST->hasQPX() && Val->getScalarType()->isFloatingPointTy()) {
288 // Floating point scalars are already located in index #0.
289 if (Index == 0)
290 return 0;
291
292 return BaseT::getVectorInstrCost(Opcode, Val, Index);
Hal Finkel27774d92014-03-13 07:58:58 +0000293 }
294
Bill Schmidt62fe7a5b2013-02-08 18:19:17 +0000295 // Estimated cost of a load-hit-store delay. This was obtained
296 // experimentally as a minimum needed to prevent unprofitable
297 // vectorization for the paq8p benchmark. It may need to be
298 // raised further if other unprofitable cases remain.
Hal Finkelde0b4132014-04-04 23:51:18 +0000299 unsigned LHSPenalty = 2;
300 if (ISD == ISD::INSERT_VECTOR_ELT)
301 LHSPenalty += 7;
Bill Schmidtb3cece12013-02-07 20:33:57 +0000302
Bill Schmidt62fe7a5b2013-02-08 18:19:17 +0000303 // Vector element insert/extract with Altivec is very expensive,
304 // because they require store and reload with the attendant
305 // processor stall for load-hit-store. Until VSX is available,
306 // these need to be estimated as very costly.
307 if (ISD == ISD::EXTRACT_VECTOR_ELT ||
308 ISD == ISD::INSERT_VECTOR_ELT)
Chandler Carruth705b1852015-01-31 03:43:40 +0000309 return LHSPenalty + BaseT::getVectorInstrCost(Opcode, Val, Index);
Bill Schmidtb3cece12013-02-07 20:33:57 +0000310
Chandler Carruth705b1852015-01-31 03:43:40 +0000311 return BaseT::getVectorInstrCost(Opcode, Val, Index);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000312}
313
Chandler Carruth93205eb2015-08-05 18:08:10 +0000314int PPCTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
315 unsigned AddressSpace) {
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000316 // Legalize the type.
Chandler Carruth93205eb2015-08-05 18:08:10 +0000317 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Src);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000318 assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
319 "Invalid Opcode");
320
Chandler Carruth93205eb2015-08-05 18:08:10 +0000321 int Cost = BaseT::getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000322
Hal Finkelde0b4132014-04-04 23:51:18 +0000323 // VSX loads/stores support unaligned access.
324 if (ST->hasVSX()) {
325 if (LT.second == MVT::v2f64 || LT.second == MVT::v2i64)
326 return Cost;
327 }
328
329 bool UnalignedAltivec =
330 Src->isVectorTy() &&
331 Src->getPrimitiveSizeInBits() >= LT.second.getSizeInBits() &&
332 LT.second.getSizeInBits() == 128 &&
333 Opcode == Instruction::Load;
Hal Finkel6e28e6a2014-03-26 19:39:09 +0000334
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000335 // PPC in general does not support unaligned loads and stores. They'll need
336 // to be decomposed based on the alignment factor.
337 unsigned SrcBytes = LT.second.getStoreSize();
Hal Finkelde0b4132014-04-04 23:51:18 +0000338 if (SrcBytes && Alignment && Alignment < SrcBytes && !UnalignedAltivec) {
Hal Finkelf8233802014-04-02 22:43:49 +0000339 Cost += LT.first*(SrcBytes/Alignment-1);
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000340
Hal Finkelde0b4132014-04-04 23:51:18 +0000341 // For a vector type, there is also scalarization overhead (only for
342 // stores, loads are expanded using the vector-load + permutation sequence,
343 // which is much less expensive).
344 if (Src->isVectorTy() && Opcode == Instruction::Store)
345 for (int i = 0, e = Src->getVectorNumElements(); i < e; ++i)
346 Cost += getVectorInstrCost(Instruction::ExtractElement, Src, i);
347 }
348
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000349 return Cost;
350}
351