blob: 51b0fedd2b54f23b45748ddaaed57e8579b21281 [file] [log] [blame]
Chandler Carruth93dcdc42015-01-31 11:17:59 +00001//===-- ARMTargetTransformInfo.cpp - ARM specific TTI ---------------------===//
Chandler Carruth664e3542013-01-07 01:37:14 +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//===----------------------------------------------------------------------===//
Chandler Carruth664e3542013-01-07 01:37:14 +00009
Chandler Carruth93dcdc42015-01-31 11:17:59 +000010#include "ARMTargetTransformInfo.h"
Chandler Carruth664e3542013-01-07 01:37:14 +000011#include "llvm/Support/Debug.h"
Renato Golin5e9d55e2013-01-29 23:31:38 +000012#include "llvm/Target/CostTable.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000013#include "llvm/Target/TargetLowering.h"
Chandler Carruth664e3542013-01-07 01:37:14 +000014using namespace llvm;
15
Chandler Carruth84e68b22014-04-22 02:41:26 +000016#define DEBUG_TYPE "armtti"
17
Florian Hahn4adcfcf2017-07-13 08:26:17 +000018bool ARMTTIImpl::areInlineCompatible(const Function *Caller,
19 const Function *Callee) const {
20 const TargetMachine &TM = getTLI()->getTargetMachine();
21 const FeatureBitset &CallerBits =
22 TM.getSubtargetImpl(*Caller)->getFeatureBits();
23 const FeatureBitset &CalleeBits =
24 TM.getSubtargetImpl(*Callee)->getFeatureBits();
25
26 // To inline a callee, all features not in the whitelist must match exactly.
27 bool MatchExact = (CallerBits & ~InlineFeatureWhitelist) ==
28 (CalleeBits & ~InlineFeatureWhitelist);
29 // For features in the whitelist, the callee's features must be a subset of
30 // the callers'.
31 bool MatchSubset = ((CallerBits & CalleeBits) & InlineFeatureWhitelist) ==
32 (CalleeBits & InlineFeatureWhitelist);
33 return MatchExact && MatchSubset;
34}
35
Chandler Carruth93205eb2015-08-05 18:08:10 +000036int ARMTTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) {
Chandler Carruth664e3542013-01-07 01:37:14 +000037 assert(Ty->isIntegerTy());
38
Tim Northover5c02f9a2016-04-13 23:08:27 +000039 unsigned Bits = Ty->getPrimitiveSizeInBits();
Weiming Zhao5410edd2016-06-28 22:30:45 +000040 if (Bits == 0 || Imm.getActiveBits() >= 64)
Tim Northover5c02f9a2016-04-13 23:08:27 +000041 return 4;
Chandler Carruth664e3542013-01-07 01:37:14 +000042
Tim Northover5c02f9a2016-04-13 23:08:27 +000043 int64_t SImmVal = Imm.getSExtValue();
44 uint64_t ZImmVal = Imm.getZExtValue();
Chandler Carruth664e3542013-01-07 01:37:14 +000045 if (!ST->isThumb()) {
46 if ((SImmVal >= 0 && SImmVal < 65536) ||
47 (ARM_AM::getSOImmVal(ZImmVal) != -1) ||
48 (ARM_AM::getSOImmVal(~ZImmVal) != -1))
49 return 1;
50 return ST->hasV6T2Ops() ? 2 : 3;
Duncan P. N. Exon Smith429d2602014-03-08 15:15:42 +000051 }
52 if (ST->isThumb2()) {
Chandler Carruth664e3542013-01-07 01:37:14 +000053 if ((SImmVal >= 0 && SImmVal < 65536) ||
54 (ARM_AM::getT2SOImmVal(ZImmVal) != -1) ||
55 (ARM_AM::getT2SOImmVal(~ZImmVal) != -1))
56 return 1;
57 return ST->hasV6T2Ops() ? 2 : 3;
Chandler Carruth664e3542013-01-07 01:37:14 +000058 }
Duncan P. N. Exon Smith429d2602014-03-08 15:15:42 +000059 // Thumb1.
60 if (SImmVal >= 0 && SImmVal < 256)
61 return 1;
James Molloy7c7255e2016-09-08 12:58:04 +000062 if ((~SImmVal < 256) || ARM_AM::isThumbImmShiftedVal(ZImmVal))
Duncan P. N. Exon Smith429d2602014-03-08 15:15:42 +000063 return 2;
64 // Load from constantpool.
65 return 3;
Chandler Carruth664e3542013-01-07 01:37:14 +000066}
Renato Golin5e9d55e2013-01-29 23:31:38 +000067
Sjoerd Meijer38c2cd02016-07-14 07:44:20 +000068
69// Constants smaller than 256 fit in the immediate field of
70// Thumb1 instructions so we return a zero cost and 1 otherwise.
71int ARMTTIImpl::getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx,
72 const APInt &Imm, Type *Ty) {
73 if (Imm.isNonNegative() && Imm.getLimitedValue() < 256)
74 return 0;
75
76 return 1;
77}
78
Tim Northover903f81b2016-04-15 18:17:18 +000079int ARMTTIImpl::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
80 Type *Ty) {
81 // Division by a constant can be turned into multiplication, but only if we
82 // know it's constant. So it's not so much that the immediate is cheap (it's
83 // not), but that the alternative is worse.
84 // FIXME: this is probably unneeded with GlobalISel.
85 if ((Opcode == Instruction::SDiv || Opcode == Instruction::UDiv ||
86 Opcode == Instruction::SRem || Opcode == Instruction::URem) &&
87 Idx == 1)
88 return 0;
89
James Molloy753c18f2016-09-08 12:58:12 +000090 if (Opcode == Instruction::And)
91 // Conversion to BIC is free, and means we can use ~Imm instead.
92 return std::min(getIntImmCost(Imm, Ty), getIntImmCost(~Imm, Ty));
93
James Molloy57d9dfa2016-09-09 13:35:36 +000094 if (Opcode == Instruction::Add)
95 // Conversion to SUB is free, and means we can use -Imm instead.
96 return std::min(getIntImmCost(Imm, Ty), getIntImmCost(-Imm, Ty));
97
James Molloy1454e902016-09-09 13:35:28 +000098 if (Opcode == Instruction::ICmp && Imm.isNegative() &&
99 Ty->getIntegerBitWidth() == 32) {
100 int64_t NegImm = -Imm.getSExtValue();
101 if (ST->isThumb2() && NegImm < 1<<12)
102 // icmp X, #-C -> cmn X, #C
103 return 0;
104 if (ST->isThumb() && NegImm < 1<<8)
105 // icmp X, #-C -> adds X, #C
106 return 0;
107 }
108
Tim Northover903f81b2016-04-15 18:17:18 +0000109 return getIntImmCost(Imm, Ty);
110}
111
112
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000113int ARMTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src,
114 const Instruction *I) {
Renato Golin5e9d55e2013-01-29 23:31:38 +0000115 int ISD = TLI->InstructionOpcodeToISD(Opcode);
116 assert(ISD && "Invalid opcode");
117
Arnold Schwaighoferf5284ff2013-03-15 15:10:47 +0000118 // Single to/from double precision conversions.
Craig Topper4b275762015-10-28 04:02:12 +0000119 static const CostTblEntry NEONFltDblTbl[] = {
Arnold Schwaighoferf5284ff2013-03-15 15:10:47 +0000120 // Vector fptrunc/fpext conversions.
121 { ISD::FP_ROUND, MVT::v2f64, 2 },
122 { ISD::FP_EXTEND, MVT::v2f32, 2 },
123 { ISD::FP_EXTEND, MVT::v4f32, 4 }
124 };
125
126 if (Src->isVectorTy() && ST->hasNEON() && (ISD == ISD::FP_ROUND ||
127 ISD == ISD::FP_EXTEND)) {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000128 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Src);
Craig Topperee0c8592015-10-27 04:14:24 +0000129 if (const auto *Entry = CostTableLookup(NEONFltDblTbl, ISD, LT.second))
130 return LT.first * Entry->Cost;
Arnold Schwaighoferf5284ff2013-03-15 15:10:47 +0000131 }
132
Mehdi Amini44ede332015-07-09 02:09:04 +0000133 EVT SrcTy = TLI->getValueType(DL, Src);
134 EVT DstTy = TLI->getValueType(DL, Dst);
Renato Golin5e9d55e2013-01-29 23:31:38 +0000135
136 if (!SrcTy.isSimple() || !DstTy.isSimple())
Chandler Carruth705b1852015-01-31 03:43:40 +0000137 return BaseT::getCastInstrCost(Opcode, Dst, Src);
Renato Golin5e9d55e2013-01-29 23:31:38 +0000138
139 // Some arithmetic, load and store operations have specific instructions
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000140 // to cast up/down their types automatically at no extra cost.
141 // TODO: Get these tables to know at least what the related operations are.
Craig Topper4b275762015-10-28 04:02:12 +0000142 static const TypeConversionCostTblEntry NEONVectorConversionTbl[] = {
Renato Golin5e9d55e2013-01-29 23:31:38 +0000143 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i16, 0 },
144 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i16, 0 },
145 { ISD::SIGN_EXTEND, MVT::v2i64, MVT::v2i32, 1 },
146 { ISD::ZERO_EXTEND, MVT::v2i64, MVT::v2i32, 1 },
147 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 0 },
148 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i32, 1 },
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000149
Renato Golin227eb6f2013-03-19 08:15:38 +0000150 // The number of vmovl instructions for the extension.
151 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
152 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
153 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 3 },
154 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 3 },
155 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i8, 7 },
156 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i8, 7 },
157 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i16, 6 },
158 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i16, 6 },
159 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 6 },
160 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 6 },
161
Jim Grosbach563983c2013-04-21 23:47:41 +0000162 // Operations that we legalize using splitting.
163 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i32, 6 },
164 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 3 },
Arnold Schwaighofer90774f32013-03-12 21:19:22 +0000165
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000166 // Vector float <-> i32 conversions.
167 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 },
168 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 },
Arnold Schwaighoferae0052f2013-03-18 22:47:09 +0000169
170 { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i8, 3 },
171 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i8, 3 },
172 { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i16, 2 },
173 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i16, 2 },
174 { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i32, 1 },
175 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i32, 1 },
176 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i1, 3 },
177 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i1, 3 },
178 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 3 },
179 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 3 },
180 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 },
181 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 },
182 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i16, 4 },
183 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 4 },
184 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i32, 2 },
185 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 2 },
186 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i16, 8 },
187 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i16, 8 },
188 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i32, 4 },
189 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i32, 4 },
190
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000191 { ISD::FP_TO_SINT, MVT::v4i32, MVT::v4f32, 1 },
192 { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f32, 1 },
Arnold Schwaighofer6c9c3a82013-03-18 22:47:06 +0000193 { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 3 },
194 { ISD::FP_TO_UINT, MVT::v4i8, MVT::v4f32, 3 },
195 { ISD::FP_TO_SINT, MVT::v4i16, MVT::v4f32, 2 },
196 { ISD::FP_TO_UINT, MVT::v4i16, MVT::v4f32, 2 },
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000197
198 // Vector double <-> i32 conversions.
199 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 },
200 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 },
Arnold Schwaighoferae0052f2013-03-18 22:47:09 +0000201
202 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i8, 4 },
203 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i8, 4 },
204 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i16, 3 },
205 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i16, 3 },
206 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 },
207 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 },
208
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000209 { ISD::FP_TO_SINT, MVT::v2i32, MVT::v2f64, 2 },
Arnold Schwaighofer6c9c3a82013-03-18 22:47:06 +0000210 { ISD::FP_TO_UINT, MVT::v2i32, MVT::v2f64, 2 },
211 { ISD::FP_TO_SINT, MVT::v8i16, MVT::v8f32, 4 },
212 { ISD::FP_TO_UINT, MVT::v8i16, MVT::v8f32, 4 },
213 { ISD::FP_TO_SINT, MVT::v16i16, MVT::v16f32, 8 },
214 { ISD::FP_TO_UINT, MVT::v16i16, MVT::v16f32, 8 }
Renato Golin5e9d55e2013-01-29 23:31:38 +0000215 };
216
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000217 if (SrcTy.isVector() && ST->hasNEON()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000218 if (const auto *Entry = ConvertCostTableLookup(NEONVectorConversionTbl, ISD,
219 DstTy.getSimpleVT(),
220 SrcTy.getSimpleVT()))
221 return Entry->Cost;
Renato Golin5e9d55e2013-01-29 23:31:38 +0000222 }
223
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000224 // Scalar float to integer conversions.
Craig Topper4b275762015-10-28 04:02:12 +0000225 static const TypeConversionCostTblEntry NEONFloatConversionTbl[] = {
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000226 { ISD::FP_TO_SINT, MVT::i1, MVT::f32, 2 },
227 { ISD::FP_TO_UINT, MVT::i1, MVT::f32, 2 },
228 { ISD::FP_TO_SINT, MVT::i1, MVT::f64, 2 },
229 { ISD::FP_TO_UINT, MVT::i1, MVT::f64, 2 },
230 { ISD::FP_TO_SINT, MVT::i8, MVT::f32, 2 },
231 { ISD::FP_TO_UINT, MVT::i8, MVT::f32, 2 },
232 { ISD::FP_TO_SINT, MVT::i8, MVT::f64, 2 },
233 { ISD::FP_TO_UINT, MVT::i8, MVT::f64, 2 },
234 { ISD::FP_TO_SINT, MVT::i16, MVT::f32, 2 },
235 { ISD::FP_TO_UINT, MVT::i16, MVT::f32, 2 },
236 { ISD::FP_TO_SINT, MVT::i16, MVT::f64, 2 },
237 { ISD::FP_TO_UINT, MVT::i16, MVT::f64, 2 },
238 { ISD::FP_TO_SINT, MVT::i32, MVT::f32, 2 },
239 { ISD::FP_TO_UINT, MVT::i32, MVT::f32, 2 },
240 { ISD::FP_TO_SINT, MVT::i32, MVT::f64, 2 },
241 { ISD::FP_TO_UINT, MVT::i32, MVT::f64, 2 },
242 { ISD::FP_TO_SINT, MVT::i64, MVT::f32, 10 },
243 { ISD::FP_TO_UINT, MVT::i64, MVT::f32, 10 },
244 { ISD::FP_TO_SINT, MVT::i64, MVT::f64, 10 },
245 { ISD::FP_TO_UINT, MVT::i64, MVT::f64, 10 }
246 };
247 if (SrcTy.isFloatingPoint() && ST->hasNEON()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000248 if (const auto *Entry = ConvertCostTableLookup(NEONFloatConversionTbl, ISD,
249 DstTy.getSimpleVT(),
250 SrcTy.getSimpleVT()))
251 return Entry->Cost;
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000252 }
253
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000254 // Scalar integer to float conversions.
Craig Topper4b275762015-10-28 04:02:12 +0000255 static const TypeConversionCostTblEntry NEONIntegerConversionTbl[] = {
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000256 { ISD::SINT_TO_FP, MVT::f32, MVT::i1, 2 },
257 { ISD::UINT_TO_FP, MVT::f32, MVT::i1, 2 },
258 { ISD::SINT_TO_FP, MVT::f64, MVT::i1, 2 },
259 { ISD::UINT_TO_FP, MVT::f64, MVT::i1, 2 },
260 { ISD::SINT_TO_FP, MVT::f32, MVT::i8, 2 },
261 { ISD::UINT_TO_FP, MVT::f32, MVT::i8, 2 },
262 { ISD::SINT_TO_FP, MVT::f64, MVT::i8, 2 },
263 { ISD::UINT_TO_FP, MVT::f64, MVT::i8, 2 },
264 { ISD::SINT_TO_FP, MVT::f32, MVT::i16, 2 },
265 { ISD::UINT_TO_FP, MVT::f32, MVT::i16, 2 },
266 { ISD::SINT_TO_FP, MVT::f64, MVT::i16, 2 },
267 { ISD::UINT_TO_FP, MVT::f64, MVT::i16, 2 },
268 { ISD::SINT_TO_FP, MVT::f32, MVT::i32, 2 },
269 { ISD::UINT_TO_FP, MVT::f32, MVT::i32, 2 },
270 { ISD::SINT_TO_FP, MVT::f64, MVT::i32, 2 },
271 { ISD::UINT_TO_FP, MVT::f64, MVT::i32, 2 },
272 { ISD::SINT_TO_FP, MVT::f32, MVT::i64, 10 },
273 { ISD::UINT_TO_FP, MVT::f32, MVT::i64, 10 },
274 { ISD::SINT_TO_FP, MVT::f64, MVT::i64, 10 },
275 { ISD::UINT_TO_FP, MVT::f64, MVT::i64, 10 }
276 };
277
278 if (SrcTy.isInteger() && ST->hasNEON()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000279 if (const auto *Entry = ConvertCostTableLookup(NEONIntegerConversionTbl,
280 ISD, DstTy.getSimpleVT(),
281 SrcTy.getSimpleVT()))
282 return Entry->Cost;
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000283 }
284
285 // Scalar integer conversion costs.
Craig Topper4b275762015-10-28 04:02:12 +0000286 static const TypeConversionCostTblEntry ARMIntegerConversionTbl[] = {
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000287 // i16 -> i64 requires two dependent operations.
288 { ISD::SIGN_EXTEND, MVT::i64, MVT::i16, 2 },
289
290 // Truncates on i64 are assumed to be free.
291 { ISD::TRUNCATE, MVT::i32, MVT::i64, 0 },
292 { ISD::TRUNCATE, MVT::i16, MVT::i64, 0 },
293 { ISD::TRUNCATE, MVT::i8, MVT::i64, 0 },
294 { ISD::TRUNCATE, MVT::i1, MVT::i64, 0 }
295 };
296
297 if (SrcTy.isInteger()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000298 if (const auto *Entry = ConvertCostTableLookup(ARMIntegerConversionTbl, ISD,
299 DstTy.getSimpleVT(),
300 SrcTy.getSimpleVT()))
301 return Entry->Cost;
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000302 }
303
Chandler Carruth705b1852015-01-31 03:43:40 +0000304 return BaseT::getCastInstrCost(Opcode, Dst, Src);
Renato Golin5e9d55e2013-01-29 23:31:38 +0000305}
Arnold Schwaighofer98f10122013-02-04 02:52:05 +0000306
Chandler Carruth93205eb2015-08-05 18:08:10 +0000307int ARMTTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy,
308 unsigned Index) {
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000309 // Penalize inserting into an D-subregister. We end up with a three times
310 // lower estimated throughput on swift.
Diana Picus4879b052016-07-06 09:22:23 +0000311 if (ST->hasSlowLoadDSubregister() && Opcode == Instruction::InsertElement &&
312 ValTy->isVectorTy() && ValTy->getScalarSizeInBits() <= 32)
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000313 return 3;
Arnold Schwaighofer98f10122013-02-04 02:52:05 +0000314
James Molloya9f47b62014-09-12 13:29:40 +0000315 if ((Opcode == Instruction::InsertElement ||
Silviu Barangad5ac2692015-08-17 15:57:05 +0000316 Opcode == Instruction::ExtractElement)) {
317 // Cross-class copies are expensive on many microarchitectures,
318 // so assume they are expensive by default.
319 if (ValTy->getVectorElementType()->isIntegerTy())
320 return 3;
321
322 // Even if it's not a cross class copy, this likely leads to mixing
323 // of NEON and VFP code and should be therefore penalized.
324 if (ValTy->isVectorTy() &&
325 ValTy->getScalarSizeInBits() <= 32)
326 return std::max(BaseT::getVectorInstrCost(Opcode, ValTy, Index), 2U);
327 }
James Molloya9f47b62014-09-12 13:29:40 +0000328
Chandler Carruth705b1852015-01-31 03:43:40 +0000329 return BaseT::getVectorInstrCost(Opcode, ValTy, Index);
Arnold Schwaighofer98f10122013-02-04 02:52:05 +0000330}
Arnold Schwaighofer213fced2013-02-07 16:10:15 +0000331
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000332int ARMTTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy,
333 const Instruction *I) {
Arnold Schwaighofer213fced2013-02-07 16:10:15 +0000334
335 int ISD = TLI->InstructionOpcodeToISD(Opcode);
336 // On NEON a a vector select gets lowered to vbsl.
337 if (ST->hasNEON() && ValTy->isVectorTy() && ISD == ISD::SELECT) {
Arnold Schwaighofer8070b382013-03-14 19:17:02 +0000338 // Lowering of some vector selects is currently far from perfect.
Craig Topper4b275762015-10-28 04:02:12 +0000339 static const TypeConversionCostTblEntry NEONVectorSelectTbl[] = {
Arnold Schwaighofer8070b382013-03-14 19:17:02 +0000340 { ISD::SELECT, MVT::v4i1, MVT::v4i64, 4*4 + 1*2 + 1 },
341 { ISD::SELECT, MVT::v8i1, MVT::v8i64, 50 },
342 { ISD::SELECT, MVT::v16i1, MVT::v16i64, 100 }
343 };
344
Mehdi Amini44ede332015-07-09 02:09:04 +0000345 EVT SelCondTy = TLI->getValueType(DL, CondTy);
346 EVT SelValTy = TLI->getValueType(DL, ValTy);
Renato Golin0178a252013-08-02 17:10:04 +0000347 if (SelCondTy.isSimple() && SelValTy.isSimple()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000348 if (const auto *Entry = ConvertCostTableLookup(NEONVectorSelectTbl, ISD,
349 SelCondTy.getSimpleVT(),
350 SelValTy.getSimpleVT()))
351 return Entry->Cost;
Renato Golin0178a252013-08-02 17:10:04 +0000352 }
Arnold Schwaighofer8070b382013-03-14 19:17:02 +0000353
Chandler Carruth93205eb2015-08-05 18:08:10 +0000354 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy);
Arnold Schwaighofer213fced2013-02-07 16:10:15 +0000355 return LT.first;
356 }
357
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000358 return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, I);
Arnold Schwaighofer213fced2013-02-07 16:10:15 +0000359}
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000360
Mohammed Agabaria23599ba2017-01-05 14:03:41 +0000361int ARMTTIImpl::getAddressComputationCost(Type *Ty, ScalarEvolution *SE,
362 const SCEV *Ptr) {
Arnold Schwaighoferda2b3112013-07-12 19:16:04 +0000363 // Address computations in vectorized code with non-consecutive addresses will
364 // likely result in more instructions compared to scalar code where the
365 // computation can more often be merged into the index mode. The resulting
366 // extra micro-ops can significantly decrease throughput.
367 unsigned NumVectorInstToHideOverhead = 10;
Mohammed Agabaria23599ba2017-01-05 14:03:41 +0000368 int MaxMergeDistance = 64;
Arnold Schwaighoferda2b3112013-07-12 19:16:04 +0000369
Mohammed Agabaria23599ba2017-01-05 14:03:41 +0000370 if (Ty->isVectorTy() && SE &&
371 !BaseT::isConstantStridedAccessLessThan(SE, Ptr, MaxMergeDistance + 1))
Arnold Schwaighoferda2b3112013-07-12 19:16:04 +0000372 return NumVectorInstToHideOverhead;
373
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000374 // In many cases the address computation is not merged into the instruction
375 // addressing mode.
376 return 1;
377}
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000378
Chandler Carruth93205eb2015-08-05 18:08:10 +0000379int ARMTTIImpl::getFPOpCost(Type *Ty) {
Cameron Esfahani17177d12015-02-05 02:09:33 +0000380 // Use similar logic that's in ARMISelLowering:
381 // Any ARM CPU with VFP2 has floating point, but Thumb1 didn't have access
382 // to VFP.
383
384 if (ST->hasVFP2() && !ST->isThumb1Only()) {
385 if (Ty->isFloatTy()) {
386 return TargetTransformInfo::TCC_Basic;
387 }
388
389 if (Ty->isDoubleTy()) {
390 return ST->isFPOnlySP() ? TargetTransformInfo::TCC_Expensive :
391 TargetTransformInfo::TCC_Basic;
392 }
393 }
394
395 return TargetTransformInfo::TCC_Expensive;
396}
397
Chandler Carruth93205eb2015-08-05 18:08:10 +0000398int ARMTTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index,
399 Type *SubTp) {
Karthik Bhate03a25d2014-06-20 04:32:48 +0000400 // We only handle costs of reverse and alternate shuffles for now.
Chandler Carruth705b1852015-01-31 03:43:40 +0000401 if (Kind != TTI::SK_Reverse && Kind != TTI::SK_Alternate)
402 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000403
Chandler Carruth705b1852015-01-31 03:43:40 +0000404 if (Kind == TTI::SK_Reverse) {
Craig Topper4b275762015-10-28 04:02:12 +0000405 static const CostTblEntry NEONShuffleTbl[] = {
Karthik Bhate03a25d2014-06-20 04:32:48 +0000406 // Reverse shuffle cost one instruction if we are shuffling within a
407 // double word (vrev) or two if we shuffle a quad word (vrev, vext).
408 {ISD::VECTOR_SHUFFLE, MVT::v2i32, 1},
409 {ISD::VECTOR_SHUFFLE, MVT::v2f32, 1},
410 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1},
411 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1},
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000412
Karthik Bhate03a25d2014-06-20 04:32:48 +0000413 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2},
414 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2},
415 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 2},
416 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 2}};
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000417
Chandler Carruth93205eb2015-08-05 18:08:10 +0000418 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp);
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000419
Craig Topperee0c8592015-10-27 04:14:24 +0000420 if (const auto *Entry = CostTableLookup(NEONShuffleTbl, ISD::VECTOR_SHUFFLE,
421 LT.second))
422 return LT.first * Entry->Cost;
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000423
Craig Topperee0c8592015-10-27 04:14:24 +0000424 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
Karthik Bhate03a25d2014-06-20 04:32:48 +0000425 }
Chandler Carruth705b1852015-01-31 03:43:40 +0000426 if (Kind == TTI::SK_Alternate) {
Craig Topper4b275762015-10-28 04:02:12 +0000427 static const CostTblEntry NEONAltShuffleTbl[] = {
Karthik Bhate03a25d2014-06-20 04:32:48 +0000428 // Alt shuffle cost table for ARM. Cost is the number of instructions
429 // required to create the shuffled vector.
430
431 {ISD::VECTOR_SHUFFLE, MVT::v2f32, 1},
432 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1},
433 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1},
434 {ISD::VECTOR_SHUFFLE, MVT::v2i32, 1},
435
436 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2},
437 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2},
438 {ISD::VECTOR_SHUFFLE, MVT::v4i16, 2},
439
440 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 16},
441
442 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 32}};
443
Chandler Carruth93205eb2015-08-05 18:08:10 +0000444 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp);
Craig Topperee0c8592015-10-27 04:14:24 +0000445 if (const auto *Entry = CostTableLookup(NEONAltShuffleTbl,
446 ISD::VECTOR_SHUFFLE, LT.second))
447 return LT.first * Entry->Cost;
448 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
Karthik Bhate03a25d2014-06-20 04:32:48 +0000449 }
Chandler Carruth705b1852015-01-31 03:43:40 +0000450 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000451}
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000452
Chandler Carruth93205eb2015-08-05 18:08:10 +0000453int ARMTTIImpl::getArithmeticInstrCost(
Chandler Carruth705b1852015-01-31 03:43:40 +0000454 unsigned Opcode, Type *Ty, TTI::OperandValueKind Op1Info,
455 TTI::OperandValueKind Op2Info, TTI::OperandValueProperties Opd1PropInfo,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000456 TTI::OperandValueProperties Opd2PropInfo,
457 ArrayRef<const Value *> Args) {
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000458
459 int ISDOpcode = TLI->InstructionOpcodeToISD(Opcode);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000460 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000461
462 const unsigned FunctionCallDivCost = 20;
463 const unsigned ReciprocalDivCost = 10;
Craig Topper4b275762015-10-28 04:02:12 +0000464 static const CostTblEntry CostTbl[] = {
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000465 // Division.
466 // These costs are somewhat random. Choose a cost of 20 to indicate that
467 // vectorizing devision (added function call) is going to be very expensive.
468 // Double registers types.
469 { ISD::SDIV, MVT::v1i64, 1 * FunctionCallDivCost},
470 { ISD::UDIV, MVT::v1i64, 1 * FunctionCallDivCost},
471 { ISD::SREM, MVT::v1i64, 1 * FunctionCallDivCost},
472 { ISD::UREM, MVT::v1i64, 1 * FunctionCallDivCost},
473 { ISD::SDIV, MVT::v2i32, 2 * FunctionCallDivCost},
474 { ISD::UDIV, MVT::v2i32, 2 * FunctionCallDivCost},
475 { ISD::SREM, MVT::v2i32, 2 * FunctionCallDivCost},
476 { ISD::UREM, MVT::v2i32, 2 * FunctionCallDivCost},
477 { ISD::SDIV, MVT::v4i16, ReciprocalDivCost},
478 { ISD::UDIV, MVT::v4i16, ReciprocalDivCost},
479 { ISD::SREM, MVT::v4i16, 4 * FunctionCallDivCost},
480 { ISD::UREM, MVT::v4i16, 4 * FunctionCallDivCost},
481 { ISD::SDIV, MVT::v8i8, ReciprocalDivCost},
482 { ISD::UDIV, MVT::v8i8, ReciprocalDivCost},
483 { ISD::SREM, MVT::v8i8, 8 * FunctionCallDivCost},
484 { ISD::UREM, MVT::v8i8, 8 * FunctionCallDivCost},
485 // Quad register types.
486 { ISD::SDIV, MVT::v2i64, 2 * FunctionCallDivCost},
487 { ISD::UDIV, MVT::v2i64, 2 * FunctionCallDivCost},
488 { ISD::SREM, MVT::v2i64, 2 * FunctionCallDivCost},
489 { ISD::UREM, MVT::v2i64, 2 * FunctionCallDivCost},
490 { ISD::SDIV, MVT::v4i32, 4 * FunctionCallDivCost},
491 { ISD::UDIV, MVT::v4i32, 4 * FunctionCallDivCost},
492 { ISD::SREM, MVT::v4i32, 4 * FunctionCallDivCost},
493 { ISD::UREM, MVT::v4i32, 4 * FunctionCallDivCost},
494 { ISD::SDIV, MVT::v8i16, 8 * FunctionCallDivCost},
495 { ISD::UDIV, MVT::v8i16, 8 * FunctionCallDivCost},
496 { ISD::SREM, MVT::v8i16, 8 * FunctionCallDivCost},
497 { ISD::UREM, MVT::v8i16, 8 * FunctionCallDivCost},
498 { ISD::SDIV, MVT::v16i8, 16 * FunctionCallDivCost},
499 { ISD::UDIV, MVT::v16i8, 16 * FunctionCallDivCost},
500 { ISD::SREM, MVT::v16i8, 16 * FunctionCallDivCost},
501 { ISD::UREM, MVT::v16i8, 16 * FunctionCallDivCost},
502 // Multiplication.
503 };
504
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000505 if (ST->hasNEON())
Craig Topperee0c8592015-10-27 04:14:24 +0000506 if (const auto *Entry = CostTableLookup(CostTbl, ISDOpcode, LT.second))
507 return LT.first * Entry->Cost;
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000508
Chandler Carruth93205eb2015-08-05 18:08:10 +0000509 int Cost = BaseT::getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info,
510 Opd1PropInfo, Opd2PropInfo);
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000511
Arnold Schwaighofer77af0f62013-10-29 01:33:53 +0000512 // This is somewhat of a hack. The problem that we are facing is that SROA
513 // creates a sequence of shift, and, or instructions to construct values.
514 // These sequences are recognized by the ISel and have zero-cost. Not so for
515 // the vectorized code. Because we have support for v2i64 but not i64 those
Alp Tokercb402912014-01-24 17:20:08 +0000516 // sequences look particularly beneficial to vectorize.
Arnold Schwaighofer77af0f62013-10-29 01:33:53 +0000517 // To work around this we increase the cost of v2i64 operations to make them
518 // seem less beneficial.
519 if (LT.second == MVT::v2i64 &&
520 Op2Info == TargetTransformInfo::OK_UniformConstantValue)
521 Cost += 4;
522
523 return Cost;
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000524}
525
Chandler Carruth93205eb2015-08-05 18:08:10 +0000526int ARMTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000527 unsigned AddressSpace, const Instruction *I) {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000528 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Src);
Arnold Schwaighofer89ae2172013-10-29 01:33:57 +0000529
530 if (Src->isVectorTy() && Alignment != 16 &&
531 Src->getVectorElementType()->isDoubleTy()) {
532 // Unaligned loads/stores are extremely inefficient.
533 // We need 4 uops for vst.1/vld.1 vs 1uop for vldr/vstr.
534 return LT.first * 4;
535 }
536 return LT.first;
537}
Hao Liu2cd34bb2015-06-26 02:45:36 +0000538
Chandler Carruth93205eb2015-08-05 18:08:10 +0000539int ARMTTIImpl::getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy,
540 unsigned Factor,
541 ArrayRef<unsigned> Indices,
542 unsigned Alignment,
543 unsigned AddressSpace) {
Hao Liu2cd34bb2015-06-26 02:45:36 +0000544 assert(Factor >= 2 && "Invalid interleave factor");
545 assert(isa<VectorType>(VecTy) && "Expect a vector type");
546
547 // vldN/vstN doesn't support vector types of i64/f64 element.
Ahmed Bougacha97564c32015-12-09 01:19:50 +0000548 bool EltIs64Bits = DL.getTypeSizeInBits(VecTy->getScalarType()) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +0000549
550 if (Factor <= TLI->getMaxSupportedInterleaveFactor() && !EltIs64Bits) {
551 unsigned NumElts = VecTy->getVectorNumElements();
Matthew Simpson1468d3e2017-04-10 18:34:37 +0000552 auto *SubVecTy = VectorType::get(VecTy->getScalarType(), NumElts / Factor);
Hao Liu2cd34bb2015-06-26 02:45:36 +0000553
554 // vldN/vstN only support legal vector types of size 64 or 128 in bits.
Matthew Simpsonaee97712017-03-02 15:15:35 +0000555 // Accesses having vector types that are a multiple of 128 bits can be
556 // matched to more than one vldN/vstN instruction.
Matthew Simpson1468d3e2017-04-10 18:34:37 +0000557 if (NumElts % Factor == 0 &&
558 TLI->isLegalInterleavedAccessType(SubVecTy, DL))
559 return Factor * TLI->getNumInterleavedAccesses(SubVecTy, DL);
Hao Liu2cd34bb2015-06-26 02:45:36 +0000560 }
561
562 return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
563 Alignment, AddressSpace);
564}