blob: c1520119ef216afb3ea7d3e07288efec382eda64 [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
Chandler Carruth93205eb2015-08-05 18:08:10 +000018int ARMTTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) {
Chandler Carruth664e3542013-01-07 01:37:14 +000019 assert(Ty->isIntegerTy());
20
21 unsigned Bits = Ty->getPrimitiveSizeInBits();
22 if (Bits == 0 || Bits > 32)
23 return 4;
24
25 int32_t SImmVal = Imm.getSExtValue();
26 uint32_t ZImmVal = Imm.getZExtValue();
27 if (!ST->isThumb()) {
28 if ((SImmVal >= 0 && SImmVal < 65536) ||
29 (ARM_AM::getSOImmVal(ZImmVal) != -1) ||
30 (ARM_AM::getSOImmVal(~ZImmVal) != -1))
31 return 1;
32 return ST->hasV6T2Ops() ? 2 : 3;
Duncan P. N. Exon Smith429d2602014-03-08 15:15:42 +000033 }
34 if (ST->isThumb2()) {
Chandler Carruth664e3542013-01-07 01:37:14 +000035 if ((SImmVal >= 0 && SImmVal < 65536) ||
36 (ARM_AM::getT2SOImmVal(ZImmVal) != -1) ||
37 (ARM_AM::getT2SOImmVal(~ZImmVal) != -1))
38 return 1;
39 return ST->hasV6T2Ops() ? 2 : 3;
Chandler Carruth664e3542013-01-07 01:37:14 +000040 }
Duncan P. N. Exon Smith429d2602014-03-08 15:15:42 +000041 // Thumb1.
42 if (SImmVal >= 0 && SImmVal < 256)
43 return 1;
44 if ((~ZImmVal < 256) || ARM_AM::isThumbImmShiftedVal(ZImmVal))
45 return 2;
46 // Load from constantpool.
47 return 3;
Chandler Carruth664e3542013-01-07 01:37:14 +000048}
Renato Golin5e9d55e2013-01-29 23:31:38 +000049
Chandler Carruth93205eb2015-08-05 18:08:10 +000050int ARMTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) {
Renato Golin5e9d55e2013-01-29 23:31:38 +000051 int ISD = TLI->InstructionOpcodeToISD(Opcode);
52 assert(ISD && "Invalid opcode");
53
Arnold Schwaighoferf5284ff2013-03-15 15:10:47 +000054 // Single to/from double precision conversions.
Craig Topper4b275762015-10-28 04:02:12 +000055 static const CostTblEntry NEONFltDblTbl[] = {
Arnold Schwaighoferf5284ff2013-03-15 15:10:47 +000056 // Vector fptrunc/fpext conversions.
57 { ISD::FP_ROUND, MVT::v2f64, 2 },
58 { ISD::FP_EXTEND, MVT::v2f32, 2 },
59 { ISD::FP_EXTEND, MVT::v4f32, 4 }
60 };
61
62 if (Src->isVectorTy() && ST->hasNEON() && (ISD == ISD::FP_ROUND ||
63 ISD == ISD::FP_EXTEND)) {
Chandler Carruth93205eb2015-08-05 18:08:10 +000064 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Src);
Craig Topperee0c8592015-10-27 04:14:24 +000065 if (const auto *Entry = CostTableLookup(NEONFltDblTbl, ISD, LT.second))
66 return LT.first * Entry->Cost;
Arnold Schwaighoferf5284ff2013-03-15 15:10:47 +000067 }
68
Mehdi Amini44ede332015-07-09 02:09:04 +000069 EVT SrcTy = TLI->getValueType(DL, Src);
70 EVT DstTy = TLI->getValueType(DL, Dst);
Renato Golin5e9d55e2013-01-29 23:31:38 +000071
72 if (!SrcTy.isSimple() || !DstTy.isSimple())
Chandler Carruth705b1852015-01-31 03:43:40 +000073 return BaseT::getCastInstrCost(Opcode, Dst, Src);
Renato Golin5e9d55e2013-01-29 23:31:38 +000074
75 // Some arithmetic, load and store operations have specific instructions
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +000076 // to cast up/down their types automatically at no extra cost.
77 // TODO: Get these tables to know at least what the related operations are.
Craig Topper4b275762015-10-28 04:02:12 +000078 static const TypeConversionCostTblEntry NEONVectorConversionTbl[] = {
Renato Golin5e9d55e2013-01-29 23:31:38 +000079 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i16, 0 },
80 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i16, 0 },
81 { ISD::SIGN_EXTEND, MVT::v2i64, MVT::v2i32, 1 },
82 { ISD::ZERO_EXTEND, MVT::v2i64, MVT::v2i32, 1 },
83 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 0 },
84 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i32, 1 },
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +000085
Renato Golin227eb6f2013-03-19 08:15:38 +000086 // The number of vmovl instructions for the extension.
87 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
88 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
89 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 3 },
90 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 3 },
91 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i8, 7 },
92 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i8, 7 },
93 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i16, 6 },
94 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i16, 6 },
95 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 6 },
96 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 6 },
97
Jim Grosbach563983c2013-04-21 23:47:41 +000098 // Operations that we legalize using splitting.
99 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i32, 6 },
100 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 3 },
Arnold Schwaighofer90774f32013-03-12 21:19:22 +0000101
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000102 // Vector float <-> i32 conversions.
103 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 },
104 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 },
Arnold Schwaighoferae0052f2013-03-18 22:47:09 +0000105
106 { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i8, 3 },
107 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i8, 3 },
108 { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i16, 2 },
109 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i16, 2 },
110 { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i32, 1 },
111 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i32, 1 },
112 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i1, 3 },
113 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i1, 3 },
114 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 3 },
115 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 3 },
116 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 },
117 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 },
118 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i16, 4 },
119 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 4 },
120 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i32, 2 },
121 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 2 },
122 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i16, 8 },
123 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i16, 8 },
124 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i32, 4 },
125 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i32, 4 },
126
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000127 { ISD::FP_TO_SINT, MVT::v4i32, MVT::v4f32, 1 },
128 { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f32, 1 },
Arnold Schwaighofer6c9c3a82013-03-18 22:47:06 +0000129 { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 3 },
130 { ISD::FP_TO_UINT, MVT::v4i8, MVT::v4f32, 3 },
131 { ISD::FP_TO_SINT, MVT::v4i16, MVT::v4f32, 2 },
132 { ISD::FP_TO_UINT, MVT::v4i16, MVT::v4f32, 2 },
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000133
134 // Vector double <-> i32 conversions.
135 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 },
136 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 },
Arnold Schwaighoferae0052f2013-03-18 22:47:09 +0000137
138 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i8, 4 },
139 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i8, 4 },
140 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i16, 3 },
141 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i16, 3 },
142 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 },
143 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 },
144
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000145 { ISD::FP_TO_SINT, MVT::v2i32, MVT::v2f64, 2 },
Arnold Schwaighofer6c9c3a82013-03-18 22:47:06 +0000146 { ISD::FP_TO_UINT, MVT::v2i32, MVT::v2f64, 2 },
147 { ISD::FP_TO_SINT, MVT::v8i16, MVT::v8f32, 4 },
148 { ISD::FP_TO_UINT, MVT::v8i16, MVT::v8f32, 4 },
149 { ISD::FP_TO_SINT, MVT::v16i16, MVT::v16f32, 8 },
150 { ISD::FP_TO_UINT, MVT::v16i16, MVT::v16f32, 8 }
Renato Golin5e9d55e2013-01-29 23:31:38 +0000151 };
152
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000153 if (SrcTy.isVector() && ST->hasNEON()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000154 if (const auto *Entry = ConvertCostTableLookup(NEONVectorConversionTbl, ISD,
155 DstTy.getSimpleVT(),
156 SrcTy.getSimpleVT()))
157 return Entry->Cost;
Renato Golin5e9d55e2013-01-29 23:31:38 +0000158 }
159
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000160 // Scalar float to integer conversions.
Craig Topper4b275762015-10-28 04:02:12 +0000161 static const TypeConversionCostTblEntry NEONFloatConversionTbl[] = {
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000162 { ISD::FP_TO_SINT, MVT::i1, MVT::f32, 2 },
163 { ISD::FP_TO_UINT, MVT::i1, MVT::f32, 2 },
164 { ISD::FP_TO_SINT, MVT::i1, MVT::f64, 2 },
165 { ISD::FP_TO_UINT, MVT::i1, MVT::f64, 2 },
166 { ISD::FP_TO_SINT, MVT::i8, MVT::f32, 2 },
167 { ISD::FP_TO_UINT, MVT::i8, MVT::f32, 2 },
168 { ISD::FP_TO_SINT, MVT::i8, MVT::f64, 2 },
169 { ISD::FP_TO_UINT, MVT::i8, MVT::f64, 2 },
170 { ISD::FP_TO_SINT, MVT::i16, MVT::f32, 2 },
171 { ISD::FP_TO_UINT, MVT::i16, MVT::f32, 2 },
172 { ISD::FP_TO_SINT, MVT::i16, MVT::f64, 2 },
173 { ISD::FP_TO_UINT, MVT::i16, MVT::f64, 2 },
174 { ISD::FP_TO_SINT, MVT::i32, MVT::f32, 2 },
175 { ISD::FP_TO_UINT, MVT::i32, MVT::f32, 2 },
176 { ISD::FP_TO_SINT, MVT::i32, MVT::f64, 2 },
177 { ISD::FP_TO_UINT, MVT::i32, MVT::f64, 2 },
178 { ISD::FP_TO_SINT, MVT::i64, MVT::f32, 10 },
179 { ISD::FP_TO_UINT, MVT::i64, MVT::f32, 10 },
180 { ISD::FP_TO_SINT, MVT::i64, MVT::f64, 10 },
181 { ISD::FP_TO_UINT, MVT::i64, MVT::f64, 10 }
182 };
183 if (SrcTy.isFloatingPoint() && ST->hasNEON()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000184 if (const auto *Entry = ConvertCostTableLookup(NEONFloatConversionTbl, ISD,
185 DstTy.getSimpleVT(),
186 SrcTy.getSimpleVT()))
187 return Entry->Cost;
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000188 }
189
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000190 // Scalar integer to float conversions.
Craig Topper4b275762015-10-28 04:02:12 +0000191 static const TypeConversionCostTblEntry NEONIntegerConversionTbl[] = {
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000192 { ISD::SINT_TO_FP, MVT::f32, MVT::i1, 2 },
193 { ISD::UINT_TO_FP, MVT::f32, MVT::i1, 2 },
194 { ISD::SINT_TO_FP, MVT::f64, MVT::i1, 2 },
195 { ISD::UINT_TO_FP, MVT::f64, MVT::i1, 2 },
196 { ISD::SINT_TO_FP, MVT::f32, MVT::i8, 2 },
197 { ISD::UINT_TO_FP, MVT::f32, MVT::i8, 2 },
198 { ISD::SINT_TO_FP, MVT::f64, MVT::i8, 2 },
199 { ISD::UINT_TO_FP, MVT::f64, MVT::i8, 2 },
200 { ISD::SINT_TO_FP, MVT::f32, MVT::i16, 2 },
201 { ISD::UINT_TO_FP, MVT::f32, MVT::i16, 2 },
202 { ISD::SINT_TO_FP, MVT::f64, MVT::i16, 2 },
203 { ISD::UINT_TO_FP, MVT::f64, MVT::i16, 2 },
204 { ISD::SINT_TO_FP, MVT::f32, MVT::i32, 2 },
205 { ISD::UINT_TO_FP, MVT::f32, MVT::i32, 2 },
206 { ISD::SINT_TO_FP, MVT::f64, MVT::i32, 2 },
207 { ISD::UINT_TO_FP, MVT::f64, MVT::i32, 2 },
208 { ISD::SINT_TO_FP, MVT::f32, MVT::i64, 10 },
209 { ISD::UINT_TO_FP, MVT::f32, MVT::i64, 10 },
210 { ISD::SINT_TO_FP, MVT::f64, MVT::i64, 10 },
211 { ISD::UINT_TO_FP, MVT::f64, MVT::i64, 10 }
212 };
213
214 if (SrcTy.isInteger() && ST->hasNEON()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000215 if (const auto *Entry = ConvertCostTableLookup(NEONIntegerConversionTbl,
216 ISD, DstTy.getSimpleVT(),
217 SrcTy.getSimpleVT()))
218 return Entry->Cost;
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000219 }
220
221 // Scalar integer conversion costs.
Craig Topper4b275762015-10-28 04:02:12 +0000222 static const TypeConversionCostTblEntry ARMIntegerConversionTbl[] = {
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000223 // i16 -> i64 requires two dependent operations.
224 { ISD::SIGN_EXTEND, MVT::i64, MVT::i16, 2 },
225
226 // Truncates on i64 are assumed to be free.
227 { ISD::TRUNCATE, MVT::i32, MVT::i64, 0 },
228 { ISD::TRUNCATE, MVT::i16, MVT::i64, 0 },
229 { ISD::TRUNCATE, MVT::i8, MVT::i64, 0 },
230 { ISD::TRUNCATE, MVT::i1, MVT::i64, 0 }
231 };
232
233 if (SrcTy.isInteger()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000234 if (const auto *Entry = ConvertCostTableLookup(ARMIntegerConversionTbl, ISD,
235 DstTy.getSimpleVT(),
236 SrcTy.getSimpleVT()))
237 return Entry->Cost;
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000238 }
239
Chandler Carruth705b1852015-01-31 03:43:40 +0000240 return BaseT::getCastInstrCost(Opcode, Dst, Src);
Renato Golin5e9d55e2013-01-29 23:31:38 +0000241}
Arnold Schwaighofer98f10122013-02-04 02:52:05 +0000242
Chandler Carruth93205eb2015-08-05 18:08:10 +0000243int ARMTTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy,
244 unsigned Index) {
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000245 // Penalize inserting into an D-subregister. We end up with a three times
246 // lower estimated throughput on swift.
Arnold Schwaighofer98f10122013-02-04 02:52:05 +0000247 if (ST->isSwift() &&
248 Opcode == Instruction::InsertElement &&
249 ValTy->isVectorTy() &&
250 ValTy->getScalarSizeInBits() <= 32)
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000251 return 3;
Arnold Schwaighofer98f10122013-02-04 02:52:05 +0000252
James Molloya9f47b62014-09-12 13:29:40 +0000253 if ((Opcode == Instruction::InsertElement ||
Silviu Barangad5ac2692015-08-17 15:57:05 +0000254 Opcode == Instruction::ExtractElement)) {
255 // Cross-class copies are expensive on many microarchitectures,
256 // so assume they are expensive by default.
257 if (ValTy->getVectorElementType()->isIntegerTy())
258 return 3;
259
260 // Even if it's not a cross class copy, this likely leads to mixing
261 // of NEON and VFP code and should be therefore penalized.
262 if (ValTy->isVectorTy() &&
263 ValTy->getScalarSizeInBits() <= 32)
264 return std::max(BaseT::getVectorInstrCost(Opcode, ValTy, Index), 2U);
265 }
James Molloya9f47b62014-09-12 13:29:40 +0000266
Chandler Carruth705b1852015-01-31 03:43:40 +0000267 return BaseT::getVectorInstrCost(Opcode, ValTy, Index);
Arnold Schwaighofer98f10122013-02-04 02:52:05 +0000268}
Arnold Schwaighofer213fced2013-02-07 16:10:15 +0000269
Chandler Carruth93205eb2015-08-05 18:08:10 +0000270int ARMTTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy) {
Arnold Schwaighofer213fced2013-02-07 16:10:15 +0000271
272 int ISD = TLI->InstructionOpcodeToISD(Opcode);
273 // On NEON a a vector select gets lowered to vbsl.
274 if (ST->hasNEON() && ValTy->isVectorTy() && ISD == ISD::SELECT) {
Arnold Schwaighofer8070b382013-03-14 19:17:02 +0000275 // Lowering of some vector selects is currently far from perfect.
Craig Topper4b275762015-10-28 04:02:12 +0000276 static const TypeConversionCostTblEntry NEONVectorSelectTbl[] = {
Arnold Schwaighofer8070b382013-03-14 19:17:02 +0000277 { ISD::SELECT, MVT::v4i1, MVT::v4i64, 4*4 + 1*2 + 1 },
278 { ISD::SELECT, MVT::v8i1, MVT::v8i64, 50 },
279 { ISD::SELECT, MVT::v16i1, MVT::v16i64, 100 }
280 };
281
Mehdi Amini44ede332015-07-09 02:09:04 +0000282 EVT SelCondTy = TLI->getValueType(DL, CondTy);
283 EVT SelValTy = TLI->getValueType(DL, ValTy);
Renato Golin0178a252013-08-02 17:10:04 +0000284 if (SelCondTy.isSimple() && SelValTy.isSimple()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000285 if (const auto *Entry = ConvertCostTableLookup(NEONVectorSelectTbl, ISD,
286 SelCondTy.getSimpleVT(),
287 SelValTy.getSimpleVT()))
288 return Entry->Cost;
Renato Golin0178a252013-08-02 17:10:04 +0000289 }
Arnold Schwaighofer8070b382013-03-14 19:17:02 +0000290
Chandler Carruth93205eb2015-08-05 18:08:10 +0000291 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy);
Arnold Schwaighofer213fced2013-02-07 16:10:15 +0000292 return LT.first;
293 }
294
Chandler Carruth705b1852015-01-31 03:43:40 +0000295 return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy);
Arnold Schwaighofer213fced2013-02-07 16:10:15 +0000296}
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000297
Chandler Carruth93205eb2015-08-05 18:08:10 +0000298int ARMTTIImpl::getAddressComputationCost(Type *Ty, bool IsComplex) {
Arnold Schwaighoferda2b3112013-07-12 19:16:04 +0000299 // Address computations in vectorized code with non-consecutive addresses will
300 // likely result in more instructions compared to scalar code where the
301 // computation can more often be merged into the index mode. The resulting
302 // extra micro-ops can significantly decrease throughput.
303 unsigned NumVectorInstToHideOverhead = 10;
304
305 if (Ty->isVectorTy() && IsComplex)
306 return NumVectorInstToHideOverhead;
307
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000308 // In many cases the address computation is not merged into the instruction
309 // addressing mode.
310 return 1;
311}
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000312
Chandler Carruth93205eb2015-08-05 18:08:10 +0000313int ARMTTIImpl::getFPOpCost(Type *Ty) {
Cameron Esfahani17177d12015-02-05 02:09:33 +0000314 // Use similar logic that's in ARMISelLowering:
315 // Any ARM CPU with VFP2 has floating point, but Thumb1 didn't have access
316 // to VFP.
317
318 if (ST->hasVFP2() && !ST->isThumb1Only()) {
319 if (Ty->isFloatTy()) {
320 return TargetTransformInfo::TCC_Basic;
321 }
322
323 if (Ty->isDoubleTy()) {
324 return ST->isFPOnlySP() ? TargetTransformInfo::TCC_Expensive :
325 TargetTransformInfo::TCC_Basic;
326 }
327 }
328
329 return TargetTransformInfo::TCC_Expensive;
330}
331
Chandler Carruth93205eb2015-08-05 18:08:10 +0000332int ARMTTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index,
333 Type *SubTp) {
Karthik Bhate03a25d2014-06-20 04:32:48 +0000334 // We only handle costs of reverse and alternate shuffles for now.
Chandler Carruth705b1852015-01-31 03:43:40 +0000335 if (Kind != TTI::SK_Reverse && Kind != TTI::SK_Alternate)
336 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000337
Chandler Carruth705b1852015-01-31 03:43:40 +0000338 if (Kind == TTI::SK_Reverse) {
Craig Topper4b275762015-10-28 04:02:12 +0000339 static const CostTblEntry NEONShuffleTbl[] = {
Karthik Bhate03a25d2014-06-20 04:32:48 +0000340 // Reverse shuffle cost one instruction if we are shuffling within a
341 // double word (vrev) or two if we shuffle a quad word (vrev, vext).
342 {ISD::VECTOR_SHUFFLE, MVT::v2i32, 1},
343 {ISD::VECTOR_SHUFFLE, MVT::v2f32, 1},
344 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1},
345 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1},
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000346
Karthik Bhate03a25d2014-06-20 04:32:48 +0000347 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2},
348 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2},
349 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 2},
350 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 2}};
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000351
Chandler Carruth93205eb2015-08-05 18:08:10 +0000352 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp);
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000353
Craig Topperee0c8592015-10-27 04:14:24 +0000354 if (const auto *Entry = CostTableLookup(NEONShuffleTbl, ISD::VECTOR_SHUFFLE,
355 LT.second))
356 return LT.first * Entry->Cost;
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000357
Craig Topperee0c8592015-10-27 04:14:24 +0000358 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
Karthik Bhate03a25d2014-06-20 04:32:48 +0000359 }
Chandler Carruth705b1852015-01-31 03:43:40 +0000360 if (Kind == TTI::SK_Alternate) {
Craig Topper4b275762015-10-28 04:02:12 +0000361 static const CostTblEntry NEONAltShuffleTbl[] = {
Karthik Bhate03a25d2014-06-20 04:32:48 +0000362 // Alt shuffle cost table for ARM. Cost is the number of instructions
363 // required to create the shuffled vector.
364
365 {ISD::VECTOR_SHUFFLE, MVT::v2f32, 1},
366 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1},
367 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1},
368 {ISD::VECTOR_SHUFFLE, MVT::v2i32, 1},
369
370 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2},
371 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2},
372 {ISD::VECTOR_SHUFFLE, MVT::v4i16, 2},
373
374 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 16},
375
376 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 32}};
377
Chandler Carruth93205eb2015-08-05 18:08:10 +0000378 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp);
Craig Topperee0c8592015-10-27 04:14:24 +0000379 if (const auto *Entry = CostTableLookup(NEONAltShuffleTbl,
380 ISD::VECTOR_SHUFFLE, LT.second))
381 return LT.first * Entry->Cost;
382 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
Karthik Bhate03a25d2014-06-20 04:32:48 +0000383 }
Chandler Carruth705b1852015-01-31 03:43:40 +0000384 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000385}
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000386
Chandler Carruth93205eb2015-08-05 18:08:10 +0000387int ARMTTIImpl::getArithmeticInstrCost(
Chandler Carruth705b1852015-01-31 03:43:40 +0000388 unsigned Opcode, Type *Ty, TTI::OperandValueKind Op1Info,
389 TTI::OperandValueKind Op2Info, TTI::OperandValueProperties Opd1PropInfo,
390 TTI::OperandValueProperties Opd2PropInfo) {
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000391
392 int ISDOpcode = TLI->InstructionOpcodeToISD(Opcode);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000393 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000394
395 const unsigned FunctionCallDivCost = 20;
396 const unsigned ReciprocalDivCost = 10;
Craig Topper4b275762015-10-28 04:02:12 +0000397 static const CostTblEntry CostTbl[] = {
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000398 // Division.
399 // These costs are somewhat random. Choose a cost of 20 to indicate that
400 // vectorizing devision (added function call) is going to be very expensive.
401 // Double registers types.
402 { ISD::SDIV, MVT::v1i64, 1 * FunctionCallDivCost},
403 { ISD::UDIV, MVT::v1i64, 1 * FunctionCallDivCost},
404 { ISD::SREM, MVT::v1i64, 1 * FunctionCallDivCost},
405 { ISD::UREM, MVT::v1i64, 1 * FunctionCallDivCost},
406 { ISD::SDIV, MVT::v2i32, 2 * FunctionCallDivCost},
407 { ISD::UDIV, MVT::v2i32, 2 * FunctionCallDivCost},
408 { ISD::SREM, MVT::v2i32, 2 * FunctionCallDivCost},
409 { ISD::UREM, MVT::v2i32, 2 * FunctionCallDivCost},
410 { ISD::SDIV, MVT::v4i16, ReciprocalDivCost},
411 { ISD::UDIV, MVT::v4i16, ReciprocalDivCost},
412 { ISD::SREM, MVT::v4i16, 4 * FunctionCallDivCost},
413 { ISD::UREM, MVT::v4i16, 4 * FunctionCallDivCost},
414 { ISD::SDIV, MVT::v8i8, ReciprocalDivCost},
415 { ISD::UDIV, MVT::v8i8, ReciprocalDivCost},
416 { ISD::SREM, MVT::v8i8, 8 * FunctionCallDivCost},
417 { ISD::UREM, MVT::v8i8, 8 * FunctionCallDivCost},
418 // Quad register types.
419 { ISD::SDIV, MVT::v2i64, 2 * FunctionCallDivCost},
420 { ISD::UDIV, MVT::v2i64, 2 * FunctionCallDivCost},
421 { ISD::SREM, MVT::v2i64, 2 * FunctionCallDivCost},
422 { ISD::UREM, MVT::v2i64, 2 * FunctionCallDivCost},
423 { ISD::SDIV, MVT::v4i32, 4 * FunctionCallDivCost},
424 { ISD::UDIV, MVT::v4i32, 4 * FunctionCallDivCost},
425 { ISD::SREM, MVT::v4i32, 4 * FunctionCallDivCost},
426 { ISD::UREM, MVT::v4i32, 4 * FunctionCallDivCost},
427 { ISD::SDIV, MVT::v8i16, 8 * FunctionCallDivCost},
428 { ISD::UDIV, MVT::v8i16, 8 * FunctionCallDivCost},
429 { ISD::SREM, MVT::v8i16, 8 * FunctionCallDivCost},
430 { ISD::UREM, MVT::v8i16, 8 * FunctionCallDivCost},
431 { ISD::SDIV, MVT::v16i8, 16 * FunctionCallDivCost},
432 { ISD::UDIV, MVT::v16i8, 16 * FunctionCallDivCost},
433 { ISD::SREM, MVT::v16i8, 16 * FunctionCallDivCost},
434 { ISD::UREM, MVT::v16i8, 16 * FunctionCallDivCost},
435 // Multiplication.
436 };
437
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000438 if (ST->hasNEON())
Craig Topperee0c8592015-10-27 04:14:24 +0000439 if (const auto *Entry = CostTableLookup(CostTbl, ISDOpcode, LT.second))
440 return LT.first * Entry->Cost;
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000441
Chandler Carruth93205eb2015-08-05 18:08:10 +0000442 int Cost = BaseT::getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info,
443 Opd1PropInfo, Opd2PropInfo);
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000444
Arnold Schwaighofer77af0f62013-10-29 01:33:53 +0000445 // This is somewhat of a hack. The problem that we are facing is that SROA
446 // creates a sequence of shift, and, or instructions to construct values.
447 // These sequences are recognized by the ISel and have zero-cost. Not so for
448 // the vectorized code. Because we have support for v2i64 but not i64 those
Alp Tokercb402912014-01-24 17:20:08 +0000449 // sequences look particularly beneficial to vectorize.
Arnold Schwaighofer77af0f62013-10-29 01:33:53 +0000450 // To work around this we increase the cost of v2i64 operations to make them
451 // seem less beneficial.
452 if (LT.second == MVT::v2i64 &&
453 Op2Info == TargetTransformInfo::OK_UniformConstantValue)
454 Cost += 4;
455
456 return Cost;
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000457}
458
Chandler Carruth93205eb2015-08-05 18:08:10 +0000459int ARMTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
460 unsigned AddressSpace) {
461 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Src);
Arnold Schwaighofer89ae2172013-10-29 01:33:57 +0000462
463 if (Src->isVectorTy() && Alignment != 16 &&
464 Src->getVectorElementType()->isDoubleTy()) {
465 // Unaligned loads/stores are extremely inefficient.
466 // We need 4 uops for vst.1/vld.1 vs 1uop for vldr/vstr.
467 return LT.first * 4;
468 }
469 return LT.first;
470}
Hao Liu2cd34bb2015-06-26 02:45:36 +0000471
Chandler Carruth93205eb2015-08-05 18:08:10 +0000472int ARMTTIImpl::getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy,
473 unsigned Factor,
474 ArrayRef<unsigned> Indices,
475 unsigned Alignment,
476 unsigned AddressSpace) {
Hao Liu2cd34bb2015-06-26 02:45:36 +0000477 assert(Factor >= 2 && "Invalid interleave factor");
478 assert(isa<VectorType>(VecTy) && "Expect a vector type");
479
480 // vldN/vstN doesn't support vector types of i64/f64 element.
Ahmed Bougacha97564c32015-12-09 01:19:50 +0000481 bool EltIs64Bits = DL.getTypeSizeInBits(VecTy->getScalarType()) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +0000482
483 if (Factor <= TLI->getMaxSupportedInterleaveFactor() && !EltIs64Bits) {
484 unsigned NumElts = VecTy->getVectorNumElements();
485 Type *SubVecTy = VectorType::get(VecTy->getScalarType(), NumElts / Factor);
Ahmed Bougacha97564c32015-12-09 01:19:50 +0000486 unsigned SubVecSize = DL.getTypeSizeInBits(SubVecTy);
Hao Liu2cd34bb2015-06-26 02:45:36 +0000487
488 // vldN/vstN only support legal vector types of size 64 or 128 in bits.
489 if (NumElts % Factor == 0 && (SubVecSize == 64 || SubVecSize == 128))
490 return Factor;
491 }
492
493 return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
494 Alignment, AddressSpace);
495}