blob: 88c98865bbc6d28da571327b967fd8d8c8ba4bfc [file] [log] [blame]
Chandler Carruth93dcdc42015-01-31 11:17:59 +00001//===-- AArch64TargetTransformInfo.cpp - AArch64 specific TTI -------------===//
Tim Northover3b0846e2014-05-24 12:50:23 +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//===----------------------------------------------------------------------===//
Tim Northover3b0846e2014-05-24 12:50:23 +00009
Chandler Carruth93dcdc42015-01-31 11:17:59 +000010#include "AArch64TargetTransformInfo.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000011#include "MCTargetDesc/AArch64AddressingModes.h"
12#include "llvm/Analysis/TargetTransformInfo.h"
Kevin Qinaef68412015-03-09 06:14:28 +000013#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth705b1852015-01-31 03:43:40 +000014#include "llvm/CodeGen/BasicTTIImpl.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000015#include "llvm/Support/Debug.h"
16#include "llvm/Target/CostTable.h"
17#include "llvm/Target/TargetLowering.h"
18#include <algorithm>
19using namespace llvm;
20
21#define DEBUG_TYPE "aarch64tti"
22
Tim Northover3b0846e2014-05-24 12:50:23 +000023/// \brief Calculate the cost of materializing a 64-bit value. This helper
24/// method might only calculate a fraction of a larger immediate. Therefore it
25/// is valid to return a cost of ZERO.
Chandler Carruth93205eb2015-08-05 18:08:10 +000026int AArch64TTIImpl::getIntImmCost(int64_t Val) {
Tim Northover3b0846e2014-05-24 12:50:23 +000027 // Check if the immediate can be encoded within an instruction.
28 if (Val == 0 || AArch64_AM::isLogicalImmediate(Val, 64))
29 return 0;
30
31 if (Val < 0)
32 Val = ~Val;
33
34 // Calculate how many moves we will need to materialize this constant.
35 unsigned LZ = countLeadingZeros((uint64_t)Val);
36 return (64 - LZ + 15) / 16;
37}
38
39/// \brief Calculate the cost of materializing the given constant.
Chandler Carruth93205eb2015-08-05 18:08:10 +000040int AArch64TTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) {
Tim Northover3b0846e2014-05-24 12:50:23 +000041 assert(Ty->isIntegerTy());
42
43 unsigned BitSize = Ty->getPrimitiveSizeInBits();
44 if (BitSize == 0)
45 return ~0U;
46
47 // Sign-extend all constants to a multiple of 64-bit.
48 APInt ImmVal = Imm;
49 if (BitSize & 0x3f)
50 ImmVal = Imm.sext((BitSize + 63) & ~0x3fU);
51
52 // Split the constant into 64-bit chunks and calculate the cost for each
53 // chunk.
Chandler Carruth93205eb2015-08-05 18:08:10 +000054 int Cost = 0;
Tim Northover3b0846e2014-05-24 12:50:23 +000055 for (unsigned ShiftVal = 0; ShiftVal < BitSize; ShiftVal += 64) {
56 APInt Tmp = ImmVal.ashr(ShiftVal).sextOrTrunc(64);
57 int64_t Val = Tmp.getSExtValue();
58 Cost += getIntImmCost(Val);
59 }
60 // We need at least one instruction to materialze the constant.
Chandler Carruth93205eb2015-08-05 18:08:10 +000061 return std::max(1, Cost);
Tim Northover3b0846e2014-05-24 12:50:23 +000062}
63
Chandler Carruth93205eb2015-08-05 18:08:10 +000064int AArch64TTIImpl::getIntImmCost(unsigned Opcode, unsigned Idx,
65 const APInt &Imm, Type *Ty) {
Tim Northover3b0846e2014-05-24 12:50:23 +000066 assert(Ty->isIntegerTy());
67
68 unsigned BitSize = Ty->getPrimitiveSizeInBits();
69 // There is no cost model for constants with a bit size of 0. Return TCC_Free
70 // here, so that constant hoisting will ignore this constant.
71 if (BitSize == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +000072 return TTI::TCC_Free;
Tim Northover3b0846e2014-05-24 12:50:23 +000073
74 unsigned ImmIdx = ~0U;
75 switch (Opcode) {
76 default:
Chandler Carruth705b1852015-01-31 03:43:40 +000077 return TTI::TCC_Free;
Tim Northover3b0846e2014-05-24 12:50:23 +000078 case Instruction::GetElementPtr:
79 // Always hoist the base address of a GetElementPtr.
80 if (Idx == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +000081 return 2 * TTI::TCC_Basic;
82 return TTI::TCC_Free;
Tim Northover3b0846e2014-05-24 12:50:23 +000083 case Instruction::Store:
84 ImmIdx = 0;
85 break;
86 case Instruction::Add:
87 case Instruction::Sub:
88 case Instruction::Mul:
89 case Instruction::UDiv:
90 case Instruction::SDiv:
91 case Instruction::URem:
92 case Instruction::SRem:
93 case Instruction::And:
94 case Instruction::Or:
95 case Instruction::Xor:
96 case Instruction::ICmp:
97 ImmIdx = 1;
98 break;
99 // Always return TCC_Free for the shift value of a shift instruction.
100 case Instruction::Shl:
101 case Instruction::LShr:
102 case Instruction::AShr:
103 if (Idx == 1)
Chandler Carruth705b1852015-01-31 03:43:40 +0000104 return TTI::TCC_Free;
Tim Northover3b0846e2014-05-24 12:50:23 +0000105 break;
106 case Instruction::Trunc:
107 case Instruction::ZExt:
108 case Instruction::SExt:
109 case Instruction::IntToPtr:
110 case Instruction::PtrToInt:
111 case Instruction::BitCast:
112 case Instruction::PHI:
113 case Instruction::Call:
114 case Instruction::Select:
115 case Instruction::Ret:
116 case Instruction::Load:
117 break;
118 }
119
120 if (Idx == ImmIdx) {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000121 int NumConstants = (BitSize + 63) / 64;
122 int Cost = AArch64TTIImpl::getIntImmCost(Imm, Ty);
Chandler Carruth705b1852015-01-31 03:43:40 +0000123 return (Cost <= NumConstants * TTI::TCC_Basic)
Chandler Carruth93205eb2015-08-05 18:08:10 +0000124 ? static_cast<int>(TTI::TCC_Free)
Chandler Carruth705b1852015-01-31 03:43:40 +0000125 : Cost;
Tim Northover3b0846e2014-05-24 12:50:23 +0000126 }
Chandler Carruth705b1852015-01-31 03:43:40 +0000127 return AArch64TTIImpl::getIntImmCost(Imm, Ty);
Tim Northover3b0846e2014-05-24 12:50:23 +0000128}
129
Chandler Carruth93205eb2015-08-05 18:08:10 +0000130int AArch64TTIImpl::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
131 const APInt &Imm, Type *Ty) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000132 assert(Ty->isIntegerTy());
133
134 unsigned BitSize = Ty->getPrimitiveSizeInBits();
135 // There is no cost model for constants with a bit size of 0. Return TCC_Free
136 // here, so that constant hoisting will ignore this constant.
137 if (BitSize == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +0000138 return TTI::TCC_Free;
Tim Northover3b0846e2014-05-24 12:50:23 +0000139
140 switch (IID) {
141 default:
Chandler Carruth705b1852015-01-31 03:43:40 +0000142 return TTI::TCC_Free;
Tim Northover3b0846e2014-05-24 12:50:23 +0000143 case Intrinsic::sadd_with_overflow:
144 case Intrinsic::uadd_with_overflow:
145 case Intrinsic::ssub_with_overflow:
146 case Intrinsic::usub_with_overflow:
147 case Intrinsic::smul_with_overflow:
148 case Intrinsic::umul_with_overflow:
149 if (Idx == 1) {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000150 int NumConstants = (BitSize + 63) / 64;
151 int Cost = AArch64TTIImpl::getIntImmCost(Imm, Ty);
Chandler Carruth705b1852015-01-31 03:43:40 +0000152 return (Cost <= NumConstants * TTI::TCC_Basic)
Chandler Carruth93205eb2015-08-05 18:08:10 +0000153 ? static_cast<int>(TTI::TCC_Free)
Chandler Carruth705b1852015-01-31 03:43:40 +0000154 : Cost;
Tim Northover3b0846e2014-05-24 12:50:23 +0000155 }
156 break;
157 case Intrinsic::experimental_stackmap:
158 if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +0000159 return TTI::TCC_Free;
Tim Northover3b0846e2014-05-24 12:50:23 +0000160 break;
161 case Intrinsic::experimental_patchpoint_void:
162 case Intrinsic::experimental_patchpoint_i64:
163 if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +0000164 return TTI::TCC_Free;
Tim Northover3b0846e2014-05-24 12:50:23 +0000165 break;
166 }
Chandler Carruth705b1852015-01-31 03:43:40 +0000167 return AArch64TTIImpl::getIntImmCost(Imm, Ty);
Tim Northover3b0846e2014-05-24 12:50:23 +0000168}
169
Chandler Carruth705b1852015-01-31 03:43:40 +0000170TargetTransformInfo::PopcntSupportKind
171AArch64TTIImpl::getPopcntSupport(unsigned TyWidth) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000172 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
173 if (TyWidth == 32 || TyWidth == 64)
Chandler Carruth705b1852015-01-31 03:43:40 +0000174 return TTI::PSK_FastHardware;
Tim Northover3b0846e2014-05-24 12:50:23 +0000175 // TODO: AArch64TargetLowering::LowerCTPOP() supports 128bit popcount.
Chandler Carruth705b1852015-01-31 03:43:40 +0000176 return TTI::PSK_Software;
Tim Northover3b0846e2014-05-24 12:50:23 +0000177}
178
Chandler Carruth93205eb2015-08-05 18:08:10 +0000179int AArch64TTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000180 int ISD = TLI->InstructionOpcodeToISD(Opcode);
181 assert(ISD && "Invalid opcode");
182
Mehdi Amini44ede332015-07-09 02:09:04 +0000183 EVT SrcTy = TLI->getValueType(DL, Src);
184 EVT DstTy = TLI->getValueType(DL, Dst);
Tim Northover3b0846e2014-05-24 12:50:23 +0000185
186 if (!SrcTy.isSimple() || !DstTy.isSimple())
Chandler Carruth705b1852015-01-31 03:43:40 +0000187 return BaseT::getCastInstrCost(Opcode, Dst, Src);
Tim Northover3b0846e2014-05-24 12:50:23 +0000188
Craig Topper4b275762015-10-28 04:02:12 +0000189 static const TypeConversionCostTblEntry
Craig Topper7bf52c92015-10-25 00:27:14 +0000190 ConversionTbl[] = {
Matthew Simpson343af072015-11-18 18:03:06 +0000191 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i32, 1 },
192 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 0 },
193 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 3 },
194 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i32, 6 },
Silviu Barangab322aa62015-08-17 16:05:09 +0000195
196 // The number of shll instructions for the extension.
Matthew Simpson343af072015-11-18 18:03:06 +0000197 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
198 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
199 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 2 },
200 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 2 },
201 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 3 },
202 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 3 },
203 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 2 },
204 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 2 },
205 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i8, 7 },
206 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i8, 7 },
207 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i16, 6 },
208 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i16, 6 },
209 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 2 },
210 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 2 },
Silviu Barangab322aa62015-08-17 16:05:09 +0000211 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 6 },
212 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 6 },
213
Tim Northover3b0846e2014-05-24 12:50:23 +0000214 // LowerVectorINT_TO_FP:
215 { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i32, 1 },
Tim Northoveref0d7602014-06-15 09:27:06 +0000216 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 },
Tim Northover3b0846e2014-05-24 12:50:23 +0000217 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i64, 1 },
218 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i32, 1 },
Tim Northoveref0d7602014-06-15 09:27:06 +0000219 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 },
Tim Northover3b0846e2014-05-24 12:50:23 +0000220 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 1 },
Tim Northoveref0d7602014-06-15 09:27:06 +0000221
222 // Complex: to v2f32
223 { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i8, 3 },
224 { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i16, 3 },
Tim Northoverdbecc3b2014-06-15 09:27:15 +0000225 { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i64, 2 },
Tim Northoveref0d7602014-06-15 09:27:06 +0000226 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i8, 3 },
227 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i16, 3 },
Tim Northoverdbecc3b2014-06-15 09:27:15 +0000228 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i64, 2 },
Tim Northoveref0d7602014-06-15 09:27:06 +0000229
230 // Complex: to v4f32
231 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 4 },
232 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 },
233 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 3 },
234 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 },
235
Silviu Barangab322aa62015-08-17 16:05:09 +0000236 // Complex: to v8f32
237 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i8, 10 },
238 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i16, 4 },
239 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 10 },
240 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 4 },
241
242 // Complex: to v16f32
243 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i8, 21 },
244 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i8, 21 },
245
Tim Northoveref0d7602014-06-15 09:27:06 +0000246 // Complex: to v2f64
247 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i8, 4 },
248 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i16, 4 },
249 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 },
250 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i8, 4 },
251 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i16, 4 },
252 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 },
253
254
Tim Northover3b0846e2014-05-24 12:50:23 +0000255 // LowerVectorFP_TO_INT
Tim Northoveref0d7602014-06-15 09:27:06 +0000256 { ISD::FP_TO_SINT, MVT::v2i32, MVT::v2f32, 1 },
Tim Northover3b0846e2014-05-24 12:50:23 +0000257 { ISD::FP_TO_SINT, MVT::v4i32, MVT::v4f32, 1 },
258 { ISD::FP_TO_SINT, MVT::v2i64, MVT::v2f64, 1 },
Tim Northoveref0d7602014-06-15 09:27:06 +0000259 { ISD::FP_TO_UINT, MVT::v2i32, MVT::v2f32, 1 },
Tim Northover3b0846e2014-05-24 12:50:23 +0000260 { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f32, 1 },
261 { ISD::FP_TO_UINT, MVT::v2i64, MVT::v2f64, 1 },
Tim Northoveref0d7602014-06-15 09:27:06 +0000262
Tim Northoverdbecc3b2014-06-15 09:27:15 +0000263 // Complex, from v2f32: legal type is v2i32 (no cost) or v2i64 (1 ext).
Tim Northoveref0d7602014-06-15 09:27:06 +0000264 { ISD::FP_TO_SINT, MVT::v2i64, MVT::v2f32, 2 },
Tim Northoverdbecc3b2014-06-15 09:27:15 +0000265 { ISD::FP_TO_SINT, MVT::v2i16, MVT::v2f32, 1 },
266 { ISD::FP_TO_SINT, MVT::v2i8, MVT::v2f32, 1 },
Tim Northoveref0d7602014-06-15 09:27:06 +0000267 { ISD::FP_TO_UINT, MVT::v2i64, MVT::v2f32, 2 },
Tim Northoverdbecc3b2014-06-15 09:27:15 +0000268 { ISD::FP_TO_UINT, MVT::v2i16, MVT::v2f32, 1 },
269 { ISD::FP_TO_UINT, MVT::v2i8, MVT::v2f32, 1 },
270
271 // Complex, from v4f32: legal type is v4i16, 1 narrowing => ~2
272 { ISD::FP_TO_SINT, MVT::v4i16, MVT::v4f32, 2 },
273 { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 2 },
Tim Northoveref0d7602014-06-15 09:27:06 +0000274 { ISD::FP_TO_UINT, MVT::v4i16, MVT::v4f32, 2 },
Tim Northoverdbecc3b2014-06-15 09:27:15 +0000275 { ISD::FP_TO_UINT, MVT::v4i8, MVT::v4f32, 2 },
276
277 // Complex, from v2f64: legal type is v2i32, 1 narrowing => ~2.
278 { ISD::FP_TO_SINT, MVT::v2i32, MVT::v2f64, 2 },
279 { ISD::FP_TO_SINT, MVT::v2i16, MVT::v2f64, 2 },
280 { ISD::FP_TO_SINT, MVT::v2i8, MVT::v2f64, 2 },
281 { ISD::FP_TO_UINT, MVT::v2i32, MVT::v2f64, 2 },
282 { ISD::FP_TO_UINT, MVT::v2i16, MVT::v2f64, 2 },
283 { ISD::FP_TO_UINT, MVT::v2i8, MVT::v2f64, 2 },
Tim Northover3b0846e2014-05-24 12:50:23 +0000284 };
285
Craig Topperee0c8592015-10-27 04:14:24 +0000286 if (const auto *Entry = ConvertCostTableLookup(ConversionTbl, ISD,
287 DstTy.getSimpleVT(),
288 SrcTy.getSimpleVT()))
289 return Entry->Cost;
Tim Northover3b0846e2014-05-24 12:50:23 +0000290
Chandler Carruth705b1852015-01-31 03:43:40 +0000291 return BaseT::getCastInstrCost(Opcode, Dst, Src);
Tim Northover3b0846e2014-05-24 12:50:23 +0000292}
293
Matthew Simpsone5dfb082016-04-27 15:20:21 +0000294int AArch64TTIImpl::getExtractWithExtendCost(unsigned Opcode, Type *Dst,
295 VectorType *VecTy,
296 unsigned Index) {
297
298 // Make sure we were given a valid extend opcode.
Matthew Simpson47bd3992016-04-27 16:25:04 +0000299 assert((Opcode == Instruction::SExt || Opcode == Instruction::ZExt) &&
300 "Invalid opcode");
Matthew Simpsone5dfb082016-04-27 15:20:21 +0000301
302 // We are extending an element we extract from a vector, so the source type
303 // of the extend is the element type of the vector.
304 auto *Src = VecTy->getElementType();
305
306 // Sign- and zero-extends are for integer types only.
307 assert(isa<IntegerType>(Dst) && isa<IntegerType>(Src) && "Invalid type");
308
309 // Get the cost for the extract. We compute the cost (if any) for the extend
310 // below.
311 auto Cost = getVectorInstrCost(Instruction::ExtractElement, VecTy, Index);
312
313 // Legalize the types.
314 auto VecLT = TLI->getTypeLegalizationCost(DL, VecTy);
315 auto DstVT = TLI->getValueType(DL, Dst);
316 auto SrcVT = TLI->getValueType(DL, Src);
317
318 // If the resulting type is still a vector and the destination type is legal,
319 // we may get the extension for free. If not, get the default cost for the
320 // extend.
321 if (!VecLT.second.isVector() || !TLI->isTypeLegal(DstVT))
322 return Cost + getCastInstrCost(Opcode, Dst, Src);
323
324 // The destination type should be larger than the element type. If not, get
325 // the default cost for the extend.
326 if (DstVT.getSizeInBits() < SrcVT.getSizeInBits())
327 return Cost + getCastInstrCost(Opcode, Dst, Src);
328
329 switch (Opcode) {
330 default:
331 llvm_unreachable("Opcode should be either SExt or ZExt");
332
333 // For sign-extends, we only need a smov, which performs the extension
334 // automatically.
335 case Instruction::SExt:
336 return Cost;
337
338 // For zero-extends, the extend is performed automatically by a umov unless
339 // the destination type is i64 and the element type is i8 or i16.
340 case Instruction::ZExt:
341 if (DstVT.getSizeInBits() != 64u || SrcVT.getSizeInBits() == 32u)
342 return Cost;
343 }
344
345 // If we are unable to perform the extend for free, get the default cost.
346 return Cost + getCastInstrCost(Opcode, Dst, Src);
347}
348
Chandler Carruth93205eb2015-08-05 18:08:10 +0000349int AArch64TTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val,
350 unsigned Index) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000351 assert(Val->isVectorTy() && "This must be a vector type");
352
353 if (Index != -1U) {
354 // Legalize the type.
Chandler Carruth93205eb2015-08-05 18:08:10 +0000355 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Val);
Tim Northover3b0846e2014-05-24 12:50:23 +0000356
357 // This type is legalized to a scalar type.
358 if (!LT.second.isVector())
359 return 0;
360
361 // The type may be split. Normalize the index to the new type.
362 unsigned Width = LT.second.getVectorNumElements();
363 Index = Index % Width;
364
365 // The element at index zero is already inside the vector.
366 if (Index == 0)
367 return 0;
368 }
369
370 // All other insert/extracts cost this much.
Matthias Braun651cff42016-06-02 18:03:53 +0000371 return ST->getVectorInsertExtractBaseCost();
Tim Northover3b0846e2014-05-24 12:50:23 +0000372}
373
Chandler Carruth93205eb2015-08-05 18:08:10 +0000374int AArch64TTIImpl::getArithmeticInstrCost(
Chandler Carruth705b1852015-01-31 03:43:40 +0000375 unsigned Opcode, Type *Ty, TTI::OperandValueKind Opd1Info,
376 TTI::OperandValueKind Opd2Info, TTI::OperandValueProperties Opd1PropInfo,
377 TTI::OperandValueProperties Opd2PropInfo) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000378 // Legalize the type.
Chandler Carruth93205eb2015-08-05 18:08:10 +0000379 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
Tim Northover3b0846e2014-05-24 12:50:23 +0000380
381 int ISD = TLI->InstructionOpcodeToISD(Opcode);
382
Chad Rosier70d54ac2014-09-29 13:59:31 +0000383 if (ISD == ISD::SDIV &&
384 Opd2Info == TargetTransformInfo::OK_UniformConstantValue &&
385 Opd2PropInfo == TargetTransformInfo::OP_PowerOf2) {
386 // On AArch64, scalar signed division by constants power-of-two are
387 // normally expanded to the sequence ADD + CMP + SELECT + SRA.
388 // The OperandValue properties many not be same as that of previous
389 // operation; conservatively assume OP_None.
Chandler Carruth93205eb2015-08-05 18:08:10 +0000390 int Cost = getArithmeticInstrCost(Instruction::Add, Ty, Opd1Info, Opd2Info,
391 TargetTransformInfo::OP_None,
392 TargetTransformInfo::OP_None);
Chad Rosier70d54ac2014-09-29 13:59:31 +0000393 Cost += getArithmeticInstrCost(Instruction::Sub, Ty, Opd1Info, Opd2Info,
394 TargetTransformInfo::OP_None,
395 TargetTransformInfo::OP_None);
396 Cost += getArithmeticInstrCost(Instruction::Select, Ty, Opd1Info, Opd2Info,
397 TargetTransformInfo::OP_None,
398 TargetTransformInfo::OP_None);
399 Cost += getArithmeticInstrCost(Instruction::AShr, Ty, Opd1Info, Opd2Info,
400 TargetTransformInfo::OP_None,
401 TargetTransformInfo::OP_None);
402 return Cost;
403 }
404
Tim Northover3b0846e2014-05-24 12:50:23 +0000405 switch (ISD) {
406 default:
Chandler Carruth705b1852015-01-31 03:43:40 +0000407 return BaseT::getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
408 Opd1PropInfo, Opd2PropInfo);
Tim Northover3b0846e2014-05-24 12:50:23 +0000409 case ISD::ADD:
410 case ISD::MUL:
411 case ISD::XOR:
412 case ISD::OR:
413 case ISD::AND:
414 // These nodes are marked as 'custom' for combining purposes only.
415 // We know that they are legal. See LowerAdd in ISelLowering.
416 return 1 * LT.first;
417 }
418}
419
Chandler Carruth93205eb2015-08-05 18:08:10 +0000420int AArch64TTIImpl::getAddressComputationCost(Type *Ty, bool IsComplex) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000421 // Address computations in vectorized code with non-consecutive addresses will
422 // likely result in more instructions compared to scalar code where the
423 // computation can more often be merged into the index mode. The resulting
424 // extra micro-ops can significantly decrease throughput.
425 unsigned NumVectorInstToHideOverhead = 10;
426
427 if (Ty->isVectorTy() && IsComplex)
428 return NumVectorInstToHideOverhead;
429
430 // In many cases the address computation is not merged into the instruction
431 // addressing mode.
432 return 1;
433}
434
Chandler Carruth93205eb2015-08-05 18:08:10 +0000435int AArch64TTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
436 Type *CondTy) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000437
438 int ISD = TLI->InstructionOpcodeToISD(Opcode);
Silviu Barangaa3e27ed2015-09-09 15:35:02 +0000439 // We don't lower some vector selects well that are wider than the register
440 // width.
Tim Northover3b0846e2014-05-24 12:50:23 +0000441 if (ValTy->isVectorTy() && ISD == ISD::SELECT) {
442 // We would need this many instructions to hide the scalarization happening.
Chandler Carruth93205eb2015-08-05 18:08:10 +0000443 const int AmortizationCost = 20;
Craig Topper4b275762015-10-28 04:02:12 +0000444 static const TypeConversionCostTblEntry
Tim Northover3b0846e2014-05-24 12:50:23 +0000445 VectorSelectTbl[] = {
Silviu Barangaa3e27ed2015-09-09 15:35:02 +0000446 { ISD::SELECT, MVT::v16i1, MVT::v16i16, 16 },
447 { ISD::SELECT, MVT::v8i1, MVT::v8i32, 8 },
448 { ISD::SELECT, MVT::v16i1, MVT::v16i32, 16 },
Tim Northover3b0846e2014-05-24 12:50:23 +0000449 { ISD::SELECT, MVT::v4i1, MVT::v4i64, 4 * AmortizationCost },
450 { ISD::SELECT, MVT::v8i1, MVT::v8i64, 8 * AmortizationCost },
451 { ISD::SELECT, MVT::v16i1, MVT::v16i64, 16 * AmortizationCost }
452 };
453
Mehdi Amini44ede332015-07-09 02:09:04 +0000454 EVT SelCondTy = TLI->getValueType(DL, CondTy);
455 EVT SelValTy = TLI->getValueType(DL, ValTy);
Tim Northover3b0846e2014-05-24 12:50:23 +0000456 if (SelCondTy.isSimple() && SelValTy.isSimple()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000457 if (const auto *Entry = ConvertCostTableLookup(VectorSelectTbl, ISD,
458 SelCondTy.getSimpleVT(),
459 SelValTy.getSimpleVT()))
460 return Entry->Cost;
Tim Northover3b0846e2014-05-24 12:50:23 +0000461 }
462 }
Chandler Carruth705b1852015-01-31 03:43:40 +0000463 return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy);
Tim Northover3b0846e2014-05-24 12:50:23 +0000464}
465
Chandler Carruth93205eb2015-08-05 18:08:10 +0000466int AArch64TTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
467 unsigned Alignment, unsigned AddressSpace) {
468 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Src);
Tim Northover3b0846e2014-05-24 12:50:23 +0000469
Matthew Simpson2c8de192016-12-15 18:36:59 +0000470 if (ST->isMisaligned128StoreSlow() && Opcode == Instruction::Store &&
471 Src->isVectorTy() && Alignment != 16 &&
Tim Northover3b0846e2014-05-24 12:50:23 +0000472 Src->getVectorElementType()->isIntegerTy(64)) {
473 // Unaligned stores are extremely inefficient. We don't split
474 // unaligned v2i64 stores because the negative impact that has shown in
475 // practice on inlined memcpy code.
476 // We make v2i64 stores expensive so that we will only vectorize if there
477 // are 6 other instructions getting vectorized.
Chandler Carruth93205eb2015-08-05 18:08:10 +0000478 int AmortizationCost = 6;
Tim Northover3b0846e2014-05-24 12:50:23 +0000479
480 return LT.first * 2 * AmortizationCost;
481 }
482
483 if (Src->isVectorTy() && Src->getVectorElementType()->isIntegerTy(8) &&
484 Src->getVectorNumElements() < 8) {
485 // We scalarize the loads/stores because there is not v.4b register and we
486 // have to promote the elements to v.4h.
487 unsigned NumVecElts = Src->getVectorNumElements();
488 unsigned NumVectorizableInstsToAmortize = NumVecElts * 2;
489 // We generate 2 instructions per vector element.
490 return NumVectorizableInstsToAmortize * NumVecElts * 2;
491 }
492
493 return LT.first;
494}
James Molloy2b8933c2014-08-05 12:30:34 +0000495
Chandler Carruth93205eb2015-08-05 18:08:10 +0000496int AArch64TTIImpl::getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy,
497 unsigned Factor,
498 ArrayRef<unsigned> Indices,
499 unsigned Alignment,
500 unsigned AddressSpace) {
Hao Liu7ec8ee32015-06-26 02:32:07 +0000501 assert(Factor >= 2 && "Invalid interleave factor");
502 assert(isa<VectorType>(VecTy) && "Expect a vector type");
503
504 if (Factor <= TLI->getMaxSupportedInterleaveFactor()) {
505 unsigned NumElts = VecTy->getVectorNumElements();
506 Type *SubVecTy = VectorType::get(VecTy->getScalarType(), NumElts / Factor);
Ahmed Bougacha97564c32015-12-09 01:19:50 +0000507 unsigned SubVecSize = DL.getTypeSizeInBits(SubVecTy);
Hao Liu7ec8ee32015-06-26 02:32:07 +0000508
509 // ldN/stN only support legal vector types of size 64 or 128 in bits.
510 if (NumElts % Factor == 0 && (SubVecSize == 64 || SubVecSize == 128))
511 return Factor;
512 }
513
514 return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
515 Alignment, AddressSpace);
516}
517
Chandler Carruth93205eb2015-08-05 18:08:10 +0000518int AArch64TTIImpl::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) {
519 int Cost = 0;
James Molloy2b8933c2014-08-05 12:30:34 +0000520 for (auto *I : Tys) {
521 if (!I->isVectorTy())
522 continue;
523 if (I->getScalarSizeInBits() * I->getVectorNumElements() == 128)
524 Cost += getMemoryOpCost(Instruction::Store, I, 128, 0) +
525 getMemoryOpCost(Instruction::Load, I, 128, 0);
526 }
527 return Cost;
528}
James Molloya88896b2014-08-21 00:02:51 +0000529
Wei Mi062c7442015-05-06 17:12:25 +0000530unsigned AArch64TTIImpl::getMaxInterleaveFactor(unsigned VF) {
Matthias Braun651cff42016-06-02 18:03:53 +0000531 return ST->getMaxInterleaveFactor();
James Molloya88896b2014-08-21 00:02:51 +0000532}
Kevin Qin72a799a2014-10-09 10:13:27 +0000533
Chandler Carruthab5cb362015-02-01 14:31:23 +0000534void AArch64TTIImpl::getUnrollingPreferences(Loop *L,
Chandler Carruth705b1852015-01-31 03:43:40 +0000535 TTI::UnrollingPreferences &UP) {
Kevin Qinaef68412015-03-09 06:14:28 +0000536 // Enable partial unrolling and runtime unrolling.
537 BaseT::getUnrollingPreferences(L, UP);
538
539 // For inner loop, it is more likely to be a hot one, and the runtime check
540 // can be promoted out from LICM pass, so the overhead is less, let's try
541 // a larger threshold to unroll more loops.
542 if (L->getLoopDepth() > 1)
543 UP.PartialThreshold *= 2;
544
Kevin Qin72a799a2014-10-09 10:13:27 +0000545 // Disable partial & runtime unrolling on -Os.
546 UP.PartialOptSizeThreshold = 0;
547}
Chad Rosierf9327d62015-01-26 22:51:15 +0000548
Chandler Carruth705b1852015-01-31 03:43:40 +0000549Value *AArch64TTIImpl::getOrCreateResultFromMemIntrinsic(IntrinsicInst *Inst,
550 Type *ExpectedType) {
Chad Rosierf9327d62015-01-26 22:51:15 +0000551 switch (Inst->getIntrinsicID()) {
552 default:
553 return nullptr;
554 case Intrinsic::aarch64_neon_st2:
555 case Intrinsic::aarch64_neon_st3:
556 case Intrinsic::aarch64_neon_st4: {
557 // Create a struct type
558 StructType *ST = dyn_cast<StructType>(ExpectedType);
559 if (!ST)
560 return nullptr;
561 unsigned NumElts = Inst->getNumArgOperands() - 1;
562 if (ST->getNumElements() != NumElts)
563 return nullptr;
564 for (unsigned i = 0, e = NumElts; i != e; ++i) {
565 if (Inst->getArgOperand(i)->getType() != ST->getElementType(i))
566 return nullptr;
567 }
568 Value *Res = UndefValue::get(ExpectedType);
569 IRBuilder<> Builder(Inst);
570 for (unsigned i = 0, e = NumElts; i != e; ++i) {
571 Value *L = Inst->getArgOperand(i);
572 Res = Builder.CreateInsertValue(Res, L, i);
573 }
574 return Res;
575 }
576 case Intrinsic::aarch64_neon_ld2:
577 case Intrinsic::aarch64_neon_ld3:
578 case Intrinsic::aarch64_neon_ld4:
579 if (Inst->getType() == ExpectedType)
580 return Inst;
581 return nullptr;
582 }
583}
584
Chandler Carruth705b1852015-01-31 03:43:40 +0000585bool AArch64TTIImpl::getTgtMemIntrinsic(IntrinsicInst *Inst,
586 MemIntrinsicInfo &Info) {
Chad Rosierf9327d62015-01-26 22:51:15 +0000587 switch (Inst->getIntrinsicID()) {
588 default:
589 break;
590 case Intrinsic::aarch64_neon_ld2:
591 case Intrinsic::aarch64_neon_ld3:
592 case Intrinsic::aarch64_neon_ld4:
593 Info.ReadMem = true;
594 Info.WriteMem = false;
Philip Reames7c6692de2015-12-05 00:18:33 +0000595 Info.IsSimple = true;
Chad Rosierf9327d62015-01-26 22:51:15 +0000596 Info.NumMemRefs = 1;
597 Info.PtrVal = Inst->getArgOperand(0);
598 break;
599 case Intrinsic::aarch64_neon_st2:
600 case Intrinsic::aarch64_neon_st3:
601 case Intrinsic::aarch64_neon_st4:
602 Info.ReadMem = false;
603 Info.WriteMem = true;
Philip Reames7c6692de2015-12-05 00:18:33 +0000604 Info.IsSimple = true;
Chad Rosierf9327d62015-01-26 22:51:15 +0000605 Info.NumMemRefs = 1;
606 Info.PtrVal = Inst->getArgOperand(Inst->getNumArgOperands() - 1);
607 break;
608 }
609
610 switch (Inst->getIntrinsicID()) {
611 default:
612 return false;
613 case Intrinsic::aarch64_neon_ld2:
614 case Intrinsic::aarch64_neon_st2:
615 Info.MatchingId = VECTOR_LDST_TWO_ELEMENTS;
616 break;
617 case Intrinsic::aarch64_neon_ld3:
618 case Intrinsic::aarch64_neon_st3:
619 Info.MatchingId = VECTOR_LDST_THREE_ELEMENTS;
620 break;
621 case Intrinsic::aarch64_neon_ld4:
622 case Intrinsic::aarch64_neon_st4:
623 Info.MatchingId = VECTOR_LDST_FOUR_ELEMENTS;
624 break;
625 }
626 return true;
627}
Adam Nemet53e758f2016-03-18 00:27:29 +0000628
629unsigned AArch64TTIImpl::getCacheLineSize() {
Matthias Braun651cff42016-06-02 18:03:53 +0000630 return ST->getCacheLineSize();
Adam Nemet53e758f2016-03-18 00:27:29 +0000631}
632
633unsigned AArch64TTIImpl::getPrefetchDistance() {
Matthias Braun651cff42016-06-02 18:03:53 +0000634 return ST->getPrefetchDistance();
Adam Nemet53e758f2016-03-18 00:27:29 +0000635}
Adam Nemet6d8beec2016-03-18 00:27:38 +0000636
637unsigned AArch64TTIImpl::getMinPrefetchStride() {
Matthias Braun651cff42016-06-02 18:03:53 +0000638 return ST->getMinPrefetchStride();
Adam Nemet6d8beec2016-03-18 00:27:38 +0000639}
Adam Nemet709e3042016-03-18 00:27:43 +0000640
641unsigned AArch64TTIImpl::getMaxPrefetchIterationsAhead() {
Matthias Braun651cff42016-06-02 18:03:53 +0000642 return ST->getMaxPrefetchIterationsAhead();
Adam Nemet709e3042016-03-18 00:27:43 +0000643}