blob: ca80d6f53f4c747fe810022924573cb962fdb3a4 [file] [log] [blame]
Eugene Zelenko076468c2017-09-20 21:35:51 +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"
Eugene Zelenko076468c2017-09-20 21:35:51 +000011#include "ARMSubtarget.h"
12#include "MCTargetDesc/ARMAddressingModes.h"
13#include "llvm/ADT/APInt.h"
14#include "llvm/ADT/SmallVector.h"
15#include "llvm/Analysis/LoopInfo.h"
16#include "llvm/CodeGen/ISDOpcodes.h"
17#include "llvm/CodeGen/MachineValueType.h"
18#include "llvm/CodeGen/ValueTypes.h"
19#include "llvm/IR/BasicBlock.h"
20#include "llvm/IR/CallSite.h"
21#include "llvm/IR/DataLayout.h"
22#include "llvm/IR/DerivedTypes.h"
23#include "llvm/IR/Instruction.h"
24#include "llvm/IR/Instructions.h"
25#include "llvm/IR/Type.h"
26#include "llvm/MC/SubtargetFeature.h"
27#include "llvm/Support/Casting.h"
Renato Golin5e9d55e2013-01-29 23:31:38 +000028#include "llvm/Target/CostTable.h"
Eugene Zelenko076468c2017-09-20 21:35:51 +000029#include "llvm/Target/TargetMachine.h"
30#include <algorithm>
31#include <cassert>
32#include <cstdint>
33#include <utility>
34
Chandler Carruth664e3542013-01-07 01:37:14 +000035using namespace llvm;
36
Chandler Carruth84e68b22014-04-22 02:41:26 +000037#define DEBUG_TYPE "armtti"
38
Florian Hahn4adcfcf2017-07-13 08:26:17 +000039bool ARMTTIImpl::areInlineCompatible(const Function *Caller,
40 const Function *Callee) const {
41 const TargetMachine &TM = getTLI()->getTargetMachine();
42 const FeatureBitset &CallerBits =
43 TM.getSubtargetImpl(*Caller)->getFeatureBits();
44 const FeatureBitset &CalleeBits =
45 TM.getSubtargetImpl(*Callee)->getFeatureBits();
46
47 // To inline a callee, all features not in the whitelist must match exactly.
48 bool MatchExact = (CallerBits & ~InlineFeatureWhitelist) ==
49 (CalleeBits & ~InlineFeatureWhitelist);
50 // For features in the whitelist, the callee's features must be a subset of
51 // the callers'.
52 bool MatchSubset = ((CallerBits & CalleeBits) & InlineFeatureWhitelist) ==
53 (CalleeBits & InlineFeatureWhitelist);
54 return MatchExact && MatchSubset;
55}
56
Chandler Carruth93205eb2015-08-05 18:08:10 +000057int ARMTTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) {
Chandler Carruth664e3542013-01-07 01:37:14 +000058 assert(Ty->isIntegerTy());
59
Tim Northover5c02f9a2016-04-13 23:08:27 +000060 unsigned Bits = Ty->getPrimitiveSizeInBits();
Weiming Zhao5410edd2016-06-28 22:30:45 +000061 if (Bits == 0 || Imm.getActiveBits() >= 64)
Tim Northover5c02f9a2016-04-13 23:08:27 +000062 return 4;
Chandler Carruth664e3542013-01-07 01:37:14 +000063
Tim Northover5c02f9a2016-04-13 23:08:27 +000064 int64_t SImmVal = Imm.getSExtValue();
65 uint64_t ZImmVal = Imm.getZExtValue();
Chandler Carruth664e3542013-01-07 01:37:14 +000066 if (!ST->isThumb()) {
67 if ((SImmVal >= 0 && SImmVal < 65536) ||
68 (ARM_AM::getSOImmVal(ZImmVal) != -1) ||
69 (ARM_AM::getSOImmVal(~ZImmVal) != -1))
70 return 1;
71 return ST->hasV6T2Ops() ? 2 : 3;
Duncan P. N. Exon Smith429d2602014-03-08 15:15:42 +000072 }
73 if (ST->isThumb2()) {
Chandler Carruth664e3542013-01-07 01:37:14 +000074 if ((SImmVal >= 0 && SImmVal < 65536) ||
75 (ARM_AM::getT2SOImmVal(ZImmVal) != -1) ||
76 (ARM_AM::getT2SOImmVal(~ZImmVal) != -1))
77 return 1;
78 return ST->hasV6T2Ops() ? 2 : 3;
Chandler Carruth664e3542013-01-07 01:37:14 +000079 }
Duncan P. N. Exon Smith429d2602014-03-08 15:15:42 +000080 // Thumb1.
81 if (SImmVal >= 0 && SImmVal < 256)
82 return 1;
James Molloy7c7255e2016-09-08 12:58:04 +000083 if ((~SImmVal < 256) || ARM_AM::isThumbImmShiftedVal(ZImmVal))
Duncan P. N. Exon Smith429d2602014-03-08 15:15:42 +000084 return 2;
85 // Load from constantpool.
86 return 3;
Chandler Carruth664e3542013-01-07 01:37:14 +000087}
Renato Golin5e9d55e2013-01-29 23:31:38 +000088
Sjoerd Meijer38c2cd02016-07-14 07:44:20 +000089// Constants smaller than 256 fit in the immediate field of
90// Thumb1 instructions so we return a zero cost and 1 otherwise.
91int ARMTTIImpl::getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx,
92 const APInt &Imm, Type *Ty) {
93 if (Imm.isNonNegative() && Imm.getLimitedValue() < 256)
94 return 0;
95
96 return 1;
97}
98
Tim Northover903f81b2016-04-15 18:17:18 +000099int ARMTTIImpl::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
100 Type *Ty) {
101 // Division by a constant can be turned into multiplication, but only if we
102 // know it's constant. So it's not so much that the immediate is cheap (it's
103 // not), but that the alternative is worse.
104 // FIXME: this is probably unneeded with GlobalISel.
105 if ((Opcode == Instruction::SDiv || Opcode == Instruction::UDiv ||
106 Opcode == Instruction::SRem || Opcode == Instruction::URem) &&
107 Idx == 1)
108 return 0;
109
James Molloy753c18f2016-09-08 12:58:12 +0000110 if (Opcode == Instruction::And)
111 // Conversion to BIC is free, and means we can use ~Imm instead.
112 return std::min(getIntImmCost(Imm, Ty), getIntImmCost(~Imm, Ty));
113
James Molloy57d9dfa2016-09-09 13:35:36 +0000114 if (Opcode == Instruction::Add)
115 // Conversion to SUB is free, and means we can use -Imm instead.
116 return std::min(getIntImmCost(Imm, Ty), getIntImmCost(-Imm, Ty));
117
James Molloy1454e902016-09-09 13:35:28 +0000118 if (Opcode == Instruction::ICmp && Imm.isNegative() &&
119 Ty->getIntegerBitWidth() == 32) {
120 int64_t NegImm = -Imm.getSExtValue();
121 if (ST->isThumb2() && NegImm < 1<<12)
122 // icmp X, #-C -> cmn X, #C
123 return 0;
124 if (ST->isThumb() && NegImm < 1<<8)
125 // icmp X, #-C -> adds X, #C
126 return 0;
127 }
128
Tim Northover903f81b2016-04-15 18:17:18 +0000129 return getIntImmCost(Imm, Ty);
130}
131
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000132int ARMTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src,
133 const Instruction *I) {
Renato Golin5e9d55e2013-01-29 23:31:38 +0000134 int ISD = TLI->InstructionOpcodeToISD(Opcode);
135 assert(ISD && "Invalid opcode");
136
Arnold Schwaighoferf5284ff2013-03-15 15:10:47 +0000137 // Single to/from double precision conversions.
Craig Topper4b275762015-10-28 04:02:12 +0000138 static const CostTblEntry NEONFltDblTbl[] = {
Arnold Schwaighoferf5284ff2013-03-15 15:10:47 +0000139 // Vector fptrunc/fpext conversions.
140 { ISD::FP_ROUND, MVT::v2f64, 2 },
141 { ISD::FP_EXTEND, MVT::v2f32, 2 },
142 { ISD::FP_EXTEND, MVT::v4f32, 4 }
143 };
144
145 if (Src->isVectorTy() && ST->hasNEON() && (ISD == ISD::FP_ROUND ||
146 ISD == ISD::FP_EXTEND)) {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000147 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Src);
Craig Topperee0c8592015-10-27 04:14:24 +0000148 if (const auto *Entry = CostTableLookup(NEONFltDblTbl, ISD, LT.second))
149 return LT.first * Entry->Cost;
Arnold Schwaighoferf5284ff2013-03-15 15:10:47 +0000150 }
151
Mehdi Amini44ede332015-07-09 02:09:04 +0000152 EVT SrcTy = TLI->getValueType(DL, Src);
153 EVT DstTy = TLI->getValueType(DL, Dst);
Renato Golin5e9d55e2013-01-29 23:31:38 +0000154
155 if (!SrcTy.isSimple() || !DstTy.isSimple())
Chandler Carruth705b1852015-01-31 03:43:40 +0000156 return BaseT::getCastInstrCost(Opcode, Dst, Src);
Renato Golin5e9d55e2013-01-29 23:31:38 +0000157
158 // Some arithmetic, load and store operations have specific instructions
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000159 // to cast up/down their types automatically at no extra cost.
160 // TODO: Get these tables to know at least what the related operations are.
Craig Topper4b275762015-10-28 04:02:12 +0000161 static const TypeConversionCostTblEntry NEONVectorConversionTbl[] = {
Renato Golin5e9d55e2013-01-29 23:31:38 +0000162 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i16, 0 },
163 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i16, 0 },
164 { ISD::SIGN_EXTEND, MVT::v2i64, MVT::v2i32, 1 },
165 { ISD::ZERO_EXTEND, MVT::v2i64, MVT::v2i32, 1 },
166 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 0 },
167 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i32, 1 },
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000168
Renato Golin227eb6f2013-03-19 08:15:38 +0000169 // The number of vmovl instructions for the extension.
170 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
171 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
172 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 3 },
173 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 3 },
174 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i8, 7 },
175 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i8, 7 },
176 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i16, 6 },
177 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i16, 6 },
178 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 6 },
179 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 6 },
180
Jim Grosbach563983c2013-04-21 23:47:41 +0000181 // Operations that we legalize using splitting.
182 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i32, 6 },
183 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 3 },
Arnold Schwaighofer90774f32013-03-12 21:19:22 +0000184
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000185 // Vector float <-> i32 conversions.
186 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 },
187 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 },
Arnold Schwaighoferae0052f2013-03-18 22:47:09 +0000188
189 { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i8, 3 },
190 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i8, 3 },
191 { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i16, 2 },
192 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i16, 2 },
193 { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i32, 1 },
194 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i32, 1 },
195 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i1, 3 },
196 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i1, 3 },
197 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 3 },
198 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 3 },
199 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 },
200 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 },
201 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i16, 4 },
202 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 4 },
203 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i32, 2 },
204 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 2 },
205 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i16, 8 },
206 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i16, 8 },
207 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i32, 4 },
208 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i32, 4 },
209
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000210 { ISD::FP_TO_SINT, MVT::v4i32, MVT::v4f32, 1 },
211 { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f32, 1 },
Arnold Schwaighofer6c9c3a82013-03-18 22:47:06 +0000212 { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 3 },
213 { ISD::FP_TO_UINT, MVT::v4i8, MVT::v4f32, 3 },
214 { ISD::FP_TO_SINT, MVT::v4i16, MVT::v4f32, 2 },
215 { ISD::FP_TO_UINT, MVT::v4i16, MVT::v4f32, 2 },
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000216
217 // Vector double <-> i32 conversions.
218 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 },
219 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 },
Arnold Schwaighoferae0052f2013-03-18 22:47:09 +0000220
221 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i8, 4 },
222 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i8, 4 },
223 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i16, 3 },
224 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i16, 3 },
225 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 },
226 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 },
227
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000228 { ISD::FP_TO_SINT, MVT::v2i32, MVT::v2f64, 2 },
Arnold Schwaighofer6c9c3a82013-03-18 22:47:06 +0000229 { ISD::FP_TO_UINT, MVT::v2i32, MVT::v2f64, 2 },
230 { ISD::FP_TO_SINT, MVT::v8i16, MVT::v8f32, 4 },
231 { ISD::FP_TO_UINT, MVT::v8i16, MVT::v8f32, 4 },
232 { ISD::FP_TO_SINT, MVT::v16i16, MVT::v16f32, 8 },
233 { ISD::FP_TO_UINT, MVT::v16i16, MVT::v16f32, 8 }
Renato Golin5e9d55e2013-01-29 23:31:38 +0000234 };
235
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000236 if (SrcTy.isVector() && ST->hasNEON()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000237 if (const auto *Entry = ConvertCostTableLookup(NEONVectorConversionTbl, ISD,
238 DstTy.getSimpleVT(),
239 SrcTy.getSimpleVT()))
240 return Entry->Cost;
Renato Golin5e9d55e2013-01-29 23:31:38 +0000241 }
242
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000243 // Scalar float to integer conversions.
Craig Topper4b275762015-10-28 04:02:12 +0000244 static const TypeConversionCostTblEntry NEONFloatConversionTbl[] = {
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000245 { ISD::FP_TO_SINT, MVT::i1, MVT::f32, 2 },
246 { ISD::FP_TO_UINT, MVT::i1, MVT::f32, 2 },
247 { ISD::FP_TO_SINT, MVT::i1, MVT::f64, 2 },
248 { ISD::FP_TO_UINT, MVT::i1, MVT::f64, 2 },
249 { ISD::FP_TO_SINT, MVT::i8, MVT::f32, 2 },
250 { ISD::FP_TO_UINT, MVT::i8, MVT::f32, 2 },
251 { ISD::FP_TO_SINT, MVT::i8, MVT::f64, 2 },
252 { ISD::FP_TO_UINT, MVT::i8, MVT::f64, 2 },
253 { ISD::FP_TO_SINT, MVT::i16, MVT::f32, 2 },
254 { ISD::FP_TO_UINT, MVT::i16, MVT::f32, 2 },
255 { ISD::FP_TO_SINT, MVT::i16, MVT::f64, 2 },
256 { ISD::FP_TO_UINT, MVT::i16, MVT::f64, 2 },
257 { ISD::FP_TO_SINT, MVT::i32, MVT::f32, 2 },
258 { ISD::FP_TO_UINT, MVT::i32, MVT::f32, 2 },
259 { ISD::FP_TO_SINT, MVT::i32, MVT::f64, 2 },
260 { ISD::FP_TO_UINT, MVT::i32, MVT::f64, 2 },
261 { ISD::FP_TO_SINT, MVT::i64, MVT::f32, 10 },
262 { ISD::FP_TO_UINT, MVT::i64, MVT::f32, 10 },
263 { ISD::FP_TO_SINT, MVT::i64, MVT::f64, 10 },
264 { ISD::FP_TO_UINT, MVT::i64, MVT::f64, 10 }
265 };
266 if (SrcTy.isFloatingPoint() && ST->hasNEON()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000267 if (const auto *Entry = ConvertCostTableLookup(NEONFloatConversionTbl, ISD,
268 DstTy.getSimpleVT(),
269 SrcTy.getSimpleVT()))
270 return Entry->Cost;
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000271 }
272
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000273 // Scalar integer to float conversions.
Craig Topper4b275762015-10-28 04:02:12 +0000274 static const TypeConversionCostTblEntry NEONIntegerConversionTbl[] = {
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000275 { ISD::SINT_TO_FP, MVT::f32, MVT::i1, 2 },
276 { ISD::UINT_TO_FP, MVT::f32, MVT::i1, 2 },
277 { ISD::SINT_TO_FP, MVT::f64, MVT::i1, 2 },
278 { ISD::UINT_TO_FP, MVT::f64, MVT::i1, 2 },
279 { ISD::SINT_TO_FP, MVT::f32, MVT::i8, 2 },
280 { ISD::UINT_TO_FP, MVT::f32, MVT::i8, 2 },
281 { ISD::SINT_TO_FP, MVT::f64, MVT::i8, 2 },
282 { ISD::UINT_TO_FP, MVT::f64, MVT::i8, 2 },
283 { ISD::SINT_TO_FP, MVT::f32, MVT::i16, 2 },
284 { ISD::UINT_TO_FP, MVT::f32, MVT::i16, 2 },
285 { ISD::SINT_TO_FP, MVT::f64, MVT::i16, 2 },
286 { ISD::UINT_TO_FP, MVT::f64, MVT::i16, 2 },
287 { ISD::SINT_TO_FP, MVT::f32, MVT::i32, 2 },
288 { ISD::UINT_TO_FP, MVT::f32, MVT::i32, 2 },
289 { ISD::SINT_TO_FP, MVT::f64, MVT::i32, 2 },
290 { ISD::UINT_TO_FP, MVT::f64, MVT::i32, 2 },
291 { ISD::SINT_TO_FP, MVT::f32, MVT::i64, 10 },
292 { ISD::UINT_TO_FP, MVT::f32, MVT::i64, 10 },
293 { ISD::SINT_TO_FP, MVT::f64, MVT::i64, 10 },
294 { ISD::UINT_TO_FP, MVT::f64, MVT::i64, 10 }
295 };
296
297 if (SrcTy.isInteger() && ST->hasNEON()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000298 if (const auto *Entry = ConvertCostTableLookup(NEONIntegerConversionTbl,
299 ISD, DstTy.getSimpleVT(),
300 SrcTy.getSimpleVT()))
301 return Entry->Cost;
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000302 }
303
304 // Scalar integer conversion costs.
Craig Topper4b275762015-10-28 04:02:12 +0000305 static const TypeConversionCostTblEntry ARMIntegerConversionTbl[] = {
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000306 // i16 -> i64 requires two dependent operations.
307 { ISD::SIGN_EXTEND, MVT::i64, MVT::i16, 2 },
308
309 // Truncates on i64 are assumed to be free.
310 { ISD::TRUNCATE, MVT::i32, MVT::i64, 0 },
311 { ISD::TRUNCATE, MVT::i16, MVT::i64, 0 },
312 { ISD::TRUNCATE, MVT::i8, MVT::i64, 0 },
313 { ISD::TRUNCATE, MVT::i1, MVT::i64, 0 }
314 };
315
316 if (SrcTy.isInteger()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000317 if (const auto *Entry = ConvertCostTableLookup(ARMIntegerConversionTbl, ISD,
318 DstTy.getSimpleVT(),
319 SrcTy.getSimpleVT()))
320 return Entry->Cost;
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000321 }
322
Chandler Carruth705b1852015-01-31 03:43:40 +0000323 return BaseT::getCastInstrCost(Opcode, Dst, Src);
Renato Golin5e9d55e2013-01-29 23:31:38 +0000324}
Arnold Schwaighofer98f10122013-02-04 02:52:05 +0000325
Chandler Carruth93205eb2015-08-05 18:08:10 +0000326int ARMTTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy,
327 unsigned Index) {
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000328 // Penalize inserting into an D-subregister. We end up with a three times
329 // lower estimated throughput on swift.
Diana Picus4879b052016-07-06 09:22:23 +0000330 if (ST->hasSlowLoadDSubregister() && Opcode == Instruction::InsertElement &&
331 ValTy->isVectorTy() && ValTy->getScalarSizeInBits() <= 32)
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000332 return 3;
Arnold Schwaighofer98f10122013-02-04 02:52:05 +0000333
James Molloya9f47b62014-09-12 13:29:40 +0000334 if ((Opcode == Instruction::InsertElement ||
Silviu Barangad5ac2692015-08-17 15:57:05 +0000335 Opcode == Instruction::ExtractElement)) {
336 // Cross-class copies are expensive on many microarchitectures,
337 // so assume they are expensive by default.
338 if (ValTy->getVectorElementType()->isIntegerTy())
339 return 3;
340
341 // Even if it's not a cross class copy, this likely leads to mixing
342 // of NEON and VFP code and should be therefore penalized.
343 if (ValTy->isVectorTy() &&
344 ValTy->getScalarSizeInBits() <= 32)
345 return std::max(BaseT::getVectorInstrCost(Opcode, ValTy, Index), 2U);
346 }
James Molloya9f47b62014-09-12 13:29:40 +0000347
Chandler Carruth705b1852015-01-31 03:43:40 +0000348 return BaseT::getVectorInstrCost(Opcode, ValTy, Index);
Arnold Schwaighofer98f10122013-02-04 02:52:05 +0000349}
Arnold Schwaighofer213fced2013-02-07 16:10:15 +0000350
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000351int ARMTTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy,
352 const Instruction *I) {
Arnold Schwaighofer213fced2013-02-07 16:10:15 +0000353 int ISD = TLI->InstructionOpcodeToISD(Opcode);
354 // On NEON a a vector select gets lowered to vbsl.
355 if (ST->hasNEON() && ValTy->isVectorTy() && ISD == ISD::SELECT) {
Arnold Schwaighofer8070b382013-03-14 19:17:02 +0000356 // Lowering of some vector selects is currently far from perfect.
Craig Topper4b275762015-10-28 04:02:12 +0000357 static const TypeConversionCostTblEntry NEONVectorSelectTbl[] = {
Arnold Schwaighofer8070b382013-03-14 19:17:02 +0000358 { ISD::SELECT, MVT::v4i1, MVT::v4i64, 4*4 + 1*2 + 1 },
359 { ISD::SELECT, MVT::v8i1, MVT::v8i64, 50 },
360 { ISD::SELECT, MVT::v16i1, MVT::v16i64, 100 }
361 };
362
Mehdi Amini44ede332015-07-09 02:09:04 +0000363 EVT SelCondTy = TLI->getValueType(DL, CondTy);
364 EVT SelValTy = TLI->getValueType(DL, ValTy);
Renato Golin0178a252013-08-02 17:10:04 +0000365 if (SelCondTy.isSimple() && SelValTy.isSimple()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000366 if (const auto *Entry = ConvertCostTableLookup(NEONVectorSelectTbl, ISD,
367 SelCondTy.getSimpleVT(),
368 SelValTy.getSimpleVT()))
369 return Entry->Cost;
Renato Golin0178a252013-08-02 17:10:04 +0000370 }
Arnold Schwaighofer8070b382013-03-14 19:17:02 +0000371
Chandler Carruth93205eb2015-08-05 18:08:10 +0000372 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy);
Arnold Schwaighofer213fced2013-02-07 16:10:15 +0000373 return LT.first;
374 }
375
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000376 return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, I);
Arnold Schwaighofer213fced2013-02-07 16:10:15 +0000377}
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000378
Mohammed Agabaria23599ba2017-01-05 14:03:41 +0000379int ARMTTIImpl::getAddressComputationCost(Type *Ty, ScalarEvolution *SE,
380 const SCEV *Ptr) {
Arnold Schwaighoferda2b3112013-07-12 19:16:04 +0000381 // Address computations in vectorized code with non-consecutive addresses will
382 // likely result in more instructions compared to scalar code where the
383 // computation can more often be merged into the index mode. The resulting
384 // extra micro-ops can significantly decrease throughput.
385 unsigned NumVectorInstToHideOverhead = 10;
Mohammed Agabaria23599ba2017-01-05 14:03:41 +0000386 int MaxMergeDistance = 64;
Arnold Schwaighoferda2b3112013-07-12 19:16:04 +0000387
Mohammed Agabaria23599ba2017-01-05 14:03:41 +0000388 if (Ty->isVectorTy() && SE &&
389 !BaseT::isConstantStridedAccessLessThan(SE, Ptr, MaxMergeDistance + 1))
Arnold Schwaighoferda2b3112013-07-12 19:16:04 +0000390 return NumVectorInstToHideOverhead;
391
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000392 // In many cases the address computation is not merged into the instruction
393 // addressing mode.
394 return 1;
395}
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000396
Chandler Carruth93205eb2015-08-05 18:08:10 +0000397int ARMTTIImpl::getFPOpCost(Type *Ty) {
Cameron Esfahani17177d12015-02-05 02:09:33 +0000398 // Use similar logic that's in ARMISelLowering:
399 // Any ARM CPU with VFP2 has floating point, but Thumb1 didn't have access
400 // to VFP.
401
402 if (ST->hasVFP2() && !ST->isThumb1Only()) {
403 if (Ty->isFloatTy()) {
404 return TargetTransformInfo::TCC_Basic;
405 }
406
407 if (Ty->isDoubleTy()) {
408 return ST->isFPOnlySP() ? TargetTransformInfo::TCC_Expensive :
409 TargetTransformInfo::TCC_Basic;
410 }
411 }
412
413 return TargetTransformInfo::TCC_Expensive;
414}
415
Chandler Carruth93205eb2015-08-05 18:08:10 +0000416int ARMTTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index,
417 Type *SubTp) {
Karthik Bhate03a25d2014-06-20 04:32:48 +0000418 // We only handle costs of reverse and alternate shuffles for now.
Chandler Carruth705b1852015-01-31 03:43:40 +0000419 if (Kind != TTI::SK_Reverse && Kind != TTI::SK_Alternate)
420 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000421
Chandler Carruth705b1852015-01-31 03:43:40 +0000422 if (Kind == TTI::SK_Reverse) {
Craig Topper4b275762015-10-28 04:02:12 +0000423 static const CostTblEntry NEONShuffleTbl[] = {
Karthik Bhate03a25d2014-06-20 04:32:48 +0000424 // Reverse shuffle cost one instruction if we are shuffling within a
425 // double word (vrev) or two if we shuffle a quad word (vrev, vext).
426 {ISD::VECTOR_SHUFFLE, MVT::v2i32, 1},
427 {ISD::VECTOR_SHUFFLE, MVT::v2f32, 1},
428 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1},
429 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1},
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000430
Karthik Bhate03a25d2014-06-20 04:32:48 +0000431 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2},
432 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2},
433 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 2},
434 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 2}};
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000435
Chandler Carruth93205eb2015-08-05 18:08:10 +0000436 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp);
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000437
Craig Topperee0c8592015-10-27 04:14:24 +0000438 if (const auto *Entry = CostTableLookup(NEONShuffleTbl, ISD::VECTOR_SHUFFLE,
439 LT.second))
440 return LT.first * Entry->Cost;
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000441
Craig Topperee0c8592015-10-27 04:14:24 +0000442 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
Karthik Bhate03a25d2014-06-20 04:32:48 +0000443 }
Chandler Carruth705b1852015-01-31 03:43:40 +0000444 if (Kind == TTI::SK_Alternate) {
Craig Topper4b275762015-10-28 04:02:12 +0000445 static const CostTblEntry NEONAltShuffleTbl[] = {
Karthik Bhate03a25d2014-06-20 04:32:48 +0000446 // Alt shuffle cost table for ARM. Cost is the number of instructions
447 // required to create the shuffled vector.
448
449 {ISD::VECTOR_SHUFFLE, MVT::v2f32, 1},
450 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1},
451 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1},
452 {ISD::VECTOR_SHUFFLE, MVT::v2i32, 1},
453
454 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2},
455 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2},
456 {ISD::VECTOR_SHUFFLE, MVT::v4i16, 2},
457
458 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 16},
459
460 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 32}};
461
Chandler Carruth93205eb2015-08-05 18:08:10 +0000462 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp);
Craig Topperee0c8592015-10-27 04:14:24 +0000463 if (const auto *Entry = CostTableLookup(NEONAltShuffleTbl,
464 ISD::VECTOR_SHUFFLE, LT.second))
465 return LT.first * Entry->Cost;
466 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
Karthik Bhate03a25d2014-06-20 04:32:48 +0000467 }
Chandler Carruth705b1852015-01-31 03:43:40 +0000468 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000469}
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000470
Chandler Carruth93205eb2015-08-05 18:08:10 +0000471int ARMTTIImpl::getArithmeticInstrCost(
Chandler Carruth705b1852015-01-31 03:43:40 +0000472 unsigned Opcode, Type *Ty, TTI::OperandValueKind Op1Info,
473 TTI::OperandValueKind Op2Info, TTI::OperandValueProperties Opd1PropInfo,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000474 TTI::OperandValueProperties Opd2PropInfo,
475 ArrayRef<const Value *> Args) {
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000476 int ISDOpcode = TLI->InstructionOpcodeToISD(Opcode);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000477 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000478
479 const unsigned FunctionCallDivCost = 20;
480 const unsigned ReciprocalDivCost = 10;
Craig Topper4b275762015-10-28 04:02:12 +0000481 static const CostTblEntry CostTbl[] = {
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000482 // Division.
483 // These costs are somewhat random. Choose a cost of 20 to indicate that
484 // vectorizing devision (added function call) is going to be very expensive.
485 // Double registers types.
486 { ISD::SDIV, MVT::v1i64, 1 * FunctionCallDivCost},
487 { ISD::UDIV, MVT::v1i64, 1 * FunctionCallDivCost},
488 { ISD::SREM, MVT::v1i64, 1 * FunctionCallDivCost},
489 { ISD::UREM, MVT::v1i64, 1 * FunctionCallDivCost},
490 { ISD::SDIV, MVT::v2i32, 2 * FunctionCallDivCost},
491 { ISD::UDIV, MVT::v2i32, 2 * FunctionCallDivCost},
492 { ISD::SREM, MVT::v2i32, 2 * FunctionCallDivCost},
493 { ISD::UREM, MVT::v2i32, 2 * FunctionCallDivCost},
494 { ISD::SDIV, MVT::v4i16, ReciprocalDivCost},
495 { ISD::UDIV, MVT::v4i16, ReciprocalDivCost},
496 { ISD::SREM, MVT::v4i16, 4 * FunctionCallDivCost},
497 { ISD::UREM, MVT::v4i16, 4 * FunctionCallDivCost},
498 { ISD::SDIV, MVT::v8i8, ReciprocalDivCost},
499 { ISD::UDIV, MVT::v8i8, ReciprocalDivCost},
500 { ISD::SREM, MVT::v8i8, 8 * FunctionCallDivCost},
501 { ISD::UREM, MVT::v8i8, 8 * FunctionCallDivCost},
502 // Quad register types.
503 { ISD::SDIV, MVT::v2i64, 2 * FunctionCallDivCost},
504 { ISD::UDIV, MVT::v2i64, 2 * FunctionCallDivCost},
505 { ISD::SREM, MVT::v2i64, 2 * FunctionCallDivCost},
506 { ISD::UREM, MVT::v2i64, 2 * FunctionCallDivCost},
507 { ISD::SDIV, MVT::v4i32, 4 * FunctionCallDivCost},
508 { ISD::UDIV, MVT::v4i32, 4 * FunctionCallDivCost},
509 { ISD::SREM, MVT::v4i32, 4 * FunctionCallDivCost},
510 { ISD::UREM, MVT::v4i32, 4 * FunctionCallDivCost},
511 { ISD::SDIV, MVT::v8i16, 8 * FunctionCallDivCost},
512 { ISD::UDIV, MVT::v8i16, 8 * FunctionCallDivCost},
513 { ISD::SREM, MVT::v8i16, 8 * FunctionCallDivCost},
514 { ISD::UREM, MVT::v8i16, 8 * FunctionCallDivCost},
515 { ISD::SDIV, MVT::v16i8, 16 * FunctionCallDivCost},
516 { ISD::UDIV, MVT::v16i8, 16 * FunctionCallDivCost},
517 { ISD::SREM, MVT::v16i8, 16 * FunctionCallDivCost},
518 { ISD::UREM, MVT::v16i8, 16 * FunctionCallDivCost},
519 // Multiplication.
520 };
521
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000522 if (ST->hasNEON())
Craig Topperee0c8592015-10-27 04:14:24 +0000523 if (const auto *Entry = CostTableLookup(CostTbl, ISDOpcode, LT.second))
524 return LT.first * Entry->Cost;
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000525
Chandler Carruth93205eb2015-08-05 18:08:10 +0000526 int Cost = BaseT::getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info,
527 Opd1PropInfo, Opd2PropInfo);
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000528
Arnold Schwaighofer77af0f62013-10-29 01:33:53 +0000529 // This is somewhat of a hack. The problem that we are facing is that SROA
530 // creates a sequence of shift, and, or instructions to construct values.
531 // These sequences are recognized by the ISel and have zero-cost. Not so for
532 // the vectorized code. Because we have support for v2i64 but not i64 those
Alp Tokercb402912014-01-24 17:20:08 +0000533 // sequences look particularly beneficial to vectorize.
Arnold Schwaighofer77af0f62013-10-29 01:33:53 +0000534 // To work around this we increase the cost of v2i64 operations to make them
535 // seem less beneficial.
536 if (LT.second == MVT::v2i64 &&
537 Op2Info == TargetTransformInfo::OK_UniformConstantValue)
538 Cost += 4;
539
540 return Cost;
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000541}
542
Chandler Carruth93205eb2015-08-05 18:08:10 +0000543int ARMTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000544 unsigned AddressSpace, const Instruction *I) {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000545 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Src);
Arnold Schwaighofer89ae2172013-10-29 01:33:57 +0000546
547 if (Src->isVectorTy() && Alignment != 16 &&
548 Src->getVectorElementType()->isDoubleTy()) {
549 // Unaligned loads/stores are extremely inefficient.
550 // We need 4 uops for vst.1/vld.1 vs 1uop for vldr/vstr.
551 return LT.first * 4;
552 }
553 return LT.first;
554}
Hao Liu2cd34bb2015-06-26 02:45:36 +0000555
Chandler Carruth93205eb2015-08-05 18:08:10 +0000556int ARMTTIImpl::getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy,
557 unsigned Factor,
558 ArrayRef<unsigned> Indices,
559 unsigned Alignment,
560 unsigned AddressSpace) {
Hao Liu2cd34bb2015-06-26 02:45:36 +0000561 assert(Factor >= 2 && "Invalid interleave factor");
562 assert(isa<VectorType>(VecTy) && "Expect a vector type");
563
564 // vldN/vstN doesn't support vector types of i64/f64 element.
Ahmed Bougacha97564c32015-12-09 01:19:50 +0000565 bool EltIs64Bits = DL.getTypeSizeInBits(VecTy->getScalarType()) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +0000566
567 if (Factor <= TLI->getMaxSupportedInterleaveFactor() && !EltIs64Bits) {
568 unsigned NumElts = VecTy->getVectorNumElements();
Matthew Simpson1468d3e2017-04-10 18:34:37 +0000569 auto *SubVecTy = VectorType::get(VecTy->getScalarType(), NumElts / Factor);
Hao Liu2cd34bb2015-06-26 02:45:36 +0000570
571 // vldN/vstN only support legal vector types of size 64 or 128 in bits.
Matthew Simpsonaee97712017-03-02 15:15:35 +0000572 // Accesses having vector types that are a multiple of 128 bits can be
573 // matched to more than one vldN/vstN instruction.
Matthew Simpson1468d3e2017-04-10 18:34:37 +0000574 if (NumElts % Factor == 0 &&
575 TLI->isLegalInterleavedAccessType(SubVecTy, DL))
576 return Factor * TLI->getNumInterleavedAccesses(SubVecTy, DL);
Hao Liu2cd34bb2015-06-26 02:45:36 +0000577 }
578
579 return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
580 Alignment, AddressSpace);
581}
Sam Parker19a08e42017-07-25 08:51:30 +0000582
583void ARMTTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
584 TTI::UnrollingPreferences &UP) {
585 // Only currently enable these preferences for M-Class cores.
Sam Parker84fd0c32017-08-16 07:42:44 +0000586 if (!ST->isMClass())
Sam Parker19a08e42017-07-25 08:51:30 +0000587 return BasicTTIImplBase::getUnrollingPreferences(L, SE, UP);
588
589 // Disable loop unrolling for Oz and Os.
590 UP.OptSizeThreshold = 0;
591 UP.PartialOptSizeThreshold = 0;
Sam Parker487ab862017-10-23 08:05:14 +0000592 if (L->getHeader()->getParent()->optForSize())
593 return;
594
595 // Only enable on Thumb-2 targets.
596 if (!ST->isThumb2())
597 return;
598
599 SmallVector<BasicBlock*, 4> ExitingBlocks;
600 L->getExitingBlocks(ExitingBlocks);
601 DEBUG(dbgs() << "Loop has:\n"
602 << "Blocks: " << L->getNumBlocks() << "\n"
603 << "Exit blocks: " << ExitingBlocks.size() << "\n");
604
605 // Only allow another exit other than the latch. This acts as an early exit
606 // as it mirrors the profitability calculation of the runtime unroller.
607 if (ExitingBlocks.size() > 2)
608 return;
609
610 // Limit the CFG of the loop body for targets with a branch predictor.
611 // Allowing 4 blocks permits if-then-else diamonds in the body.
612 if (ST->hasBranchPredictor() && L->getNumBlocks() > 4)
Sam Parker84fd0c32017-08-16 07:42:44 +0000613 return;
Sam Parker19a08e42017-07-25 08:51:30 +0000614
615 // Scan the loop: don't unroll loops with calls as this could prevent
616 // inlining.
Sam Parker84fd0c32017-08-16 07:42:44 +0000617 unsigned Cost = 0;
Sam Parker487ab862017-10-23 08:05:14 +0000618 for (auto *BB : L->getBlocks()) {
619 for (auto &I : *BB) {
620 if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
621 ImmutableCallSite CS(&I);
622 if (const Function *F = CS.getCalledFunction()) {
623 if (!isLoweredToCall(F))
624 continue;
625 }
626 return;
Sam Parker19a08e42017-07-25 08:51:30 +0000627 }
Sam Parker487ab862017-10-23 08:05:14 +0000628 SmallVector<const Value*, 4> Operands(I.value_op_begin(),
629 I.value_op_end());
630 Cost += getUserCost(&I, Operands);
Sam Parker19a08e42017-07-25 08:51:30 +0000631 }
632 }
633
Sam Parker487ab862017-10-23 08:05:14 +0000634 DEBUG(dbgs() << "Cost of loop: " << Cost << "\n");
635
Sam Parker19a08e42017-07-25 08:51:30 +0000636 UP.Partial = true;
637 UP.Runtime = true;
Sam Parker84fd0c32017-08-16 07:42:44 +0000638 UP.UnrollRemainder = true;
639 UP.DefaultUnrollRuntimeCount = 4;
640
641 // Force unrolling small loops can be very useful because of the branch
642 // taken cost of the backedge.
643 if (Cost < 12)
644 UP.Force = true;
Sam Parker19a08e42017-07-25 08:51:30 +0000645}