blob: 1d1d92c99a33f4f8da9fc02cc65771b0a9b91419 [file] [log] [blame]
Eugene Zelenko076468c2017-09-20 21:35:51 +00001//===- ARMTargetTransformInfo.cpp - ARM specific TTI ----------------------===//
Chandler Carruth664e3542013-01-07 01:37:14 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chandler Carruth664e3542013-01-07 01:37:14 +00006//
7//===----------------------------------------------------------------------===//
Chandler Carruth664e3542013-01-07 01:37:14 +00008
Chandler Carruth93dcdc42015-01-31 11:17:59 +00009#include "ARMTargetTransformInfo.h"
Eugene Zelenko076468c2017-09-20 21:35:51 +000010#include "ARMSubtarget.h"
11#include "MCTargetDesc/ARMAddressingModes.h"
12#include "llvm/ADT/APInt.h"
13#include "llvm/ADT/SmallVector.h"
14#include "llvm/Analysis/LoopInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000015#include "llvm/CodeGen/CostTable.h"
Eugene Zelenko076468c2017-09-20 21:35:51 +000016#include "llvm/CodeGen/ISDOpcodes.h"
Craig Topper2fa14362018-03-29 17:21:10 +000017#include "llvm/CodeGen/ValueTypes.h"
Eugene Zelenko076468c2017-09-20 21:35:51 +000018#include "llvm/IR/BasicBlock.h"
19#include "llvm/IR/CallSite.h"
20#include "llvm/IR/DataLayout.h"
21#include "llvm/IR/DerivedTypes.h"
22#include "llvm/IR/Instruction.h"
23#include "llvm/IR/Instructions.h"
24#include "llvm/IR/Type.h"
25#include "llvm/MC/SubtargetFeature.h"
26#include "llvm/Support/Casting.h"
David Blaikie13e77db2018-03-23 23:58:25 +000027#include "llvm/Support/MachineValueType.h"
Eugene Zelenko076468c2017-09-20 21:35:51 +000028#include "llvm/Target/TargetMachine.h"
29#include <algorithm>
30#include <cassert>
31#include <cstdint>
32#include <utility>
33
Chandler Carruth664e3542013-01-07 01:37:14 +000034using namespace llvm;
35
Chandler Carruth84e68b22014-04-22 02:41:26 +000036#define DEBUG_TYPE "armtti"
37
Florian Hahn4adcfcf2017-07-13 08:26:17 +000038bool ARMTTIImpl::areInlineCompatible(const Function *Caller,
39 const Function *Callee) const {
40 const TargetMachine &TM = getTLI()->getTargetMachine();
41 const FeatureBitset &CallerBits =
42 TM.getSubtargetImpl(*Caller)->getFeatureBits();
43 const FeatureBitset &CalleeBits =
44 TM.getSubtargetImpl(*Callee)->getFeatureBits();
45
46 // To inline a callee, all features not in the whitelist must match exactly.
47 bool MatchExact = (CallerBits & ~InlineFeatureWhitelist) ==
48 (CalleeBits & ~InlineFeatureWhitelist);
49 // For features in the whitelist, the callee's features must be a subset of
50 // the callers'.
51 bool MatchSubset = ((CallerBits & CalleeBits) & InlineFeatureWhitelist) ==
52 (CalleeBits & InlineFeatureWhitelist);
53 return MatchExact && MatchSubset;
54}
55
Chandler Carruth93205eb2015-08-05 18:08:10 +000056int ARMTTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) {
Chandler Carruth664e3542013-01-07 01:37:14 +000057 assert(Ty->isIntegerTy());
58
Tim Northover5c02f9a2016-04-13 23:08:27 +000059 unsigned Bits = Ty->getPrimitiveSizeInBits();
Weiming Zhao5410edd2016-06-28 22:30:45 +000060 if (Bits == 0 || Imm.getActiveBits() >= 64)
Tim Northover5c02f9a2016-04-13 23:08:27 +000061 return 4;
Chandler Carruth664e3542013-01-07 01:37:14 +000062
Tim Northover5c02f9a2016-04-13 23:08:27 +000063 int64_t SImmVal = Imm.getSExtValue();
64 uint64_t ZImmVal = Imm.getZExtValue();
Chandler Carruth664e3542013-01-07 01:37:14 +000065 if (!ST->isThumb()) {
66 if ((SImmVal >= 0 && SImmVal < 65536) ||
67 (ARM_AM::getSOImmVal(ZImmVal) != -1) ||
68 (ARM_AM::getSOImmVal(~ZImmVal) != -1))
69 return 1;
70 return ST->hasV6T2Ops() ? 2 : 3;
Duncan P. N. Exon Smith429d2602014-03-08 15:15:42 +000071 }
72 if (ST->isThumb2()) {
Chandler Carruth664e3542013-01-07 01:37:14 +000073 if ((SImmVal >= 0 && SImmVal < 65536) ||
74 (ARM_AM::getT2SOImmVal(ZImmVal) != -1) ||
75 (ARM_AM::getT2SOImmVal(~ZImmVal) != -1))
76 return 1;
77 return ST->hasV6T2Ops() ? 2 : 3;
Chandler Carruth664e3542013-01-07 01:37:14 +000078 }
Zhaoshi Zheng05b46dc2018-09-24 16:15:23 +000079 // Thumb1, any i8 imm cost 1.
80 if (Bits == 8 || (SImmVal >= 0 && SImmVal < 256))
Duncan P. N. Exon Smith429d2602014-03-08 15:15:42 +000081 return 1;
James Molloy7c7255e2016-09-08 12:58:04 +000082 if ((~SImmVal < 256) || ARM_AM::isThumbImmShiftedVal(ZImmVal))
Duncan P. N. Exon Smith429d2602014-03-08 15:15:42 +000083 return 2;
84 // Load from constantpool.
85 return 3;
Chandler Carruth664e3542013-01-07 01:37:14 +000086}
Renato Golin5e9d55e2013-01-29 23:31:38 +000087
Sjoerd Meijer38c2cd02016-07-14 07:44:20 +000088// Constants smaller than 256 fit in the immediate field of
89// Thumb1 instructions so we return a zero cost and 1 otherwise.
90int ARMTTIImpl::getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx,
91 const APInt &Imm, Type *Ty) {
92 if (Imm.isNonNegative() && Imm.getLimitedValue() < 256)
93 return 0;
94
95 return 1;
96}
97
Tim Northover903f81b2016-04-15 18:17:18 +000098int ARMTTIImpl::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
99 Type *Ty) {
100 // Division by a constant can be turned into multiplication, but only if we
101 // know it's constant. So it's not so much that the immediate is cheap (it's
102 // not), but that the alternative is worse.
103 // FIXME: this is probably unneeded with GlobalISel.
104 if ((Opcode == Instruction::SDiv || Opcode == Instruction::UDiv ||
105 Opcode == Instruction::SRem || Opcode == Instruction::URem) &&
106 Idx == 1)
107 return 0;
108
David Greenb4f36a22019-02-04 11:58:48 +0000109 if (Opcode == Instruction::And) {
110 // UXTB/UXTH
111 if (Imm == 255 || Imm == 65535)
112 return 0;
113 // Conversion to BIC is free, and means we can use ~Imm instead.
114 return std::min(getIntImmCost(Imm, Ty), getIntImmCost(~Imm, Ty));
115 }
James Molloy753c18f2016-09-08 12:58:12 +0000116
James Molloy57d9dfa2016-09-09 13:35:36 +0000117 if (Opcode == Instruction::Add)
118 // Conversion to SUB is free, and means we can use -Imm instead.
119 return std::min(getIntImmCost(Imm, Ty), getIntImmCost(-Imm, Ty));
120
James Molloy1454e902016-09-09 13:35:28 +0000121 if (Opcode == Instruction::ICmp && Imm.isNegative() &&
122 Ty->getIntegerBitWidth() == 32) {
123 int64_t NegImm = -Imm.getSExtValue();
124 if (ST->isThumb2() && NegImm < 1<<12)
125 // icmp X, #-C -> cmn X, #C
126 return 0;
127 if (ST->isThumb() && NegImm < 1<<8)
128 // icmp X, #-C -> adds X, #C
129 return 0;
130 }
131
David Green05647642018-02-20 11:07:35 +0000132 // xor a, -1 can always be folded to MVN
David Green01e0f252018-02-22 09:38:57 +0000133 if (Opcode == Instruction::Xor && Imm.isAllOnesValue())
134 return 0;
David Green05647642018-02-20 11:07:35 +0000135
Tim Northover903f81b2016-04-15 18:17:18 +0000136 return getIntImmCost(Imm, Ty);
137}
138
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000139int ARMTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src,
140 const Instruction *I) {
Renato Golin5e9d55e2013-01-29 23:31:38 +0000141 int ISD = TLI->InstructionOpcodeToISD(Opcode);
142 assert(ISD && "Invalid opcode");
143
Arnold Schwaighoferf5284ff2013-03-15 15:10:47 +0000144 // Single to/from double precision conversions.
Craig Topper4b275762015-10-28 04:02:12 +0000145 static const CostTblEntry NEONFltDblTbl[] = {
Arnold Schwaighoferf5284ff2013-03-15 15:10:47 +0000146 // Vector fptrunc/fpext conversions.
147 { ISD::FP_ROUND, MVT::v2f64, 2 },
148 { ISD::FP_EXTEND, MVT::v2f32, 2 },
149 { ISD::FP_EXTEND, MVT::v4f32, 4 }
150 };
151
152 if (Src->isVectorTy() && ST->hasNEON() && (ISD == ISD::FP_ROUND ||
153 ISD == ISD::FP_EXTEND)) {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000154 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Src);
Craig Topperee0c8592015-10-27 04:14:24 +0000155 if (const auto *Entry = CostTableLookup(NEONFltDblTbl, ISD, LT.second))
156 return LT.first * Entry->Cost;
Arnold Schwaighoferf5284ff2013-03-15 15:10:47 +0000157 }
158
Mehdi Amini44ede332015-07-09 02:09:04 +0000159 EVT SrcTy = TLI->getValueType(DL, Src);
160 EVT DstTy = TLI->getValueType(DL, Dst);
Renato Golin5e9d55e2013-01-29 23:31:38 +0000161
162 if (!SrcTy.isSimple() || !DstTy.isSimple())
Chandler Carruth705b1852015-01-31 03:43:40 +0000163 return BaseT::getCastInstrCost(Opcode, Dst, Src);
Renato Golin5e9d55e2013-01-29 23:31:38 +0000164
165 // Some arithmetic, load and store operations have specific instructions
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000166 // to cast up/down their types automatically at no extra cost.
167 // TODO: Get these tables to know at least what the related operations are.
Craig Topper4b275762015-10-28 04:02:12 +0000168 static const TypeConversionCostTblEntry NEONVectorConversionTbl[] = {
Renato Golin5e9d55e2013-01-29 23:31:38 +0000169 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i16, 0 },
170 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i16, 0 },
171 { ISD::SIGN_EXTEND, MVT::v2i64, MVT::v2i32, 1 },
172 { ISD::ZERO_EXTEND, MVT::v2i64, MVT::v2i32, 1 },
173 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 0 },
174 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i32, 1 },
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000175
Renato Golin227eb6f2013-03-19 08:15:38 +0000176 // The number of vmovl instructions for the extension.
177 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
178 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
179 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 3 },
180 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 3 },
181 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i8, 7 },
182 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i8, 7 },
183 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i16, 6 },
184 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i16, 6 },
185 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 6 },
186 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 6 },
187
Jim Grosbach563983c2013-04-21 23:47:41 +0000188 // Operations that we legalize using splitting.
189 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i32, 6 },
190 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 3 },
Arnold Schwaighofer90774f32013-03-12 21:19:22 +0000191
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000192 // Vector float <-> i32 conversions.
193 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 },
194 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 },
Arnold Schwaighoferae0052f2013-03-18 22:47:09 +0000195
196 { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i8, 3 },
197 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i8, 3 },
198 { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i16, 2 },
199 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i16, 2 },
200 { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i32, 1 },
201 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i32, 1 },
202 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i1, 3 },
203 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i1, 3 },
204 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 3 },
205 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 3 },
206 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 },
207 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 },
208 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i16, 4 },
209 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 4 },
210 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i32, 2 },
211 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 2 },
212 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i16, 8 },
213 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i16, 8 },
214 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i32, 4 },
215 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i32, 4 },
216
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000217 { ISD::FP_TO_SINT, MVT::v4i32, MVT::v4f32, 1 },
218 { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f32, 1 },
Arnold Schwaighofer6c9c3a82013-03-18 22:47:06 +0000219 { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 3 },
220 { ISD::FP_TO_UINT, MVT::v4i8, MVT::v4f32, 3 },
221 { ISD::FP_TO_SINT, MVT::v4i16, MVT::v4f32, 2 },
222 { ISD::FP_TO_UINT, MVT::v4i16, MVT::v4f32, 2 },
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000223
224 // Vector double <-> i32 conversions.
225 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 },
226 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 },
Arnold Schwaighoferae0052f2013-03-18 22:47:09 +0000227
228 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i8, 4 },
229 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i8, 4 },
230 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i16, 3 },
231 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i16, 3 },
232 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 },
233 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 },
234
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000235 { ISD::FP_TO_SINT, MVT::v2i32, MVT::v2f64, 2 },
Arnold Schwaighofer6c9c3a82013-03-18 22:47:06 +0000236 { ISD::FP_TO_UINT, MVT::v2i32, MVT::v2f64, 2 },
237 { ISD::FP_TO_SINT, MVT::v8i16, MVT::v8f32, 4 },
238 { ISD::FP_TO_UINT, MVT::v8i16, MVT::v8f32, 4 },
239 { ISD::FP_TO_SINT, MVT::v16i16, MVT::v16f32, 8 },
240 { ISD::FP_TO_UINT, MVT::v16i16, MVT::v16f32, 8 }
Renato Golin5e9d55e2013-01-29 23:31:38 +0000241 };
242
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000243 if (SrcTy.isVector() && ST->hasNEON()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000244 if (const auto *Entry = ConvertCostTableLookup(NEONVectorConversionTbl, ISD,
245 DstTy.getSimpleVT(),
246 SrcTy.getSimpleVT()))
247 return Entry->Cost;
Renato Golin5e9d55e2013-01-29 23:31:38 +0000248 }
249
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000250 // Scalar float to integer conversions.
Craig Topper4b275762015-10-28 04:02:12 +0000251 static const TypeConversionCostTblEntry NEONFloatConversionTbl[] = {
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000252 { ISD::FP_TO_SINT, MVT::i1, MVT::f32, 2 },
253 { ISD::FP_TO_UINT, MVT::i1, MVT::f32, 2 },
254 { ISD::FP_TO_SINT, MVT::i1, MVT::f64, 2 },
255 { ISD::FP_TO_UINT, MVT::i1, MVT::f64, 2 },
256 { ISD::FP_TO_SINT, MVT::i8, MVT::f32, 2 },
257 { ISD::FP_TO_UINT, MVT::i8, MVT::f32, 2 },
258 { ISD::FP_TO_SINT, MVT::i8, MVT::f64, 2 },
259 { ISD::FP_TO_UINT, MVT::i8, MVT::f64, 2 },
260 { ISD::FP_TO_SINT, MVT::i16, MVT::f32, 2 },
261 { ISD::FP_TO_UINT, MVT::i16, MVT::f32, 2 },
262 { ISD::FP_TO_SINT, MVT::i16, MVT::f64, 2 },
263 { ISD::FP_TO_UINT, MVT::i16, MVT::f64, 2 },
264 { ISD::FP_TO_SINT, MVT::i32, MVT::f32, 2 },
265 { ISD::FP_TO_UINT, MVT::i32, MVT::f32, 2 },
266 { ISD::FP_TO_SINT, MVT::i32, MVT::f64, 2 },
267 { ISD::FP_TO_UINT, MVT::i32, MVT::f64, 2 },
268 { ISD::FP_TO_SINT, MVT::i64, MVT::f32, 10 },
269 { ISD::FP_TO_UINT, MVT::i64, MVT::f32, 10 },
270 { ISD::FP_TO_SINT, MVT::i64, MVT::f64, 10 },
271 { ISD::FP_TO_UINT, MVT::i64, MVT::f64, 10 }
272 };
273 if (SrcTy.isFloatingPoint() && ST->hasNEON()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000274 if (const auto *Entry = ConvertCostTableLookup(NEONFloatConversionTbl, ISD,
275 DstTy.getSimpleVT(),
276 SrcTy.getSimpleVT()))
277 return Entry->Cost;
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000278 }
279
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000280 // Scalar integer to float conversions.
Craig Topper4b275762015-10-28 04:02:12 +0000281 static const TypeConversionCostTblEntry NEONIntegerConversionTbl[] = {
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000282 { ISD::SINT_TO_FP, MVT::f32, MVT::i1, 2 },
283 { ISD::UINT_TO_FP, MVT::f32, MVT::i1, 2 },
284 { ISD::SINT_TO_FP, MVT::f64, MVT::i1, 2 },
285 { ISD::UINT_TO_FP, MVT::f64, MVT::i1, 2 },
286 { ISD::SINT_TO_FP, MVT::f32, MVT::i8, 2 },
287 { ISD::UINT_TO_FP, MVT::f32, MVT::i8, 2 },
288 { ISD::SINT_TO_FP, MVT::f64, MVT::i8, 2 },
289 { ISD::UINT_TO_FP, MVT::f64, MVT::i8, 2 },
290 { ISD::SINT_TO_FP, MVT::f32, MVT::i16, 2 },
291 { ISD::UINT_TO_FP, MVT::f32, MVT::i16, 2 },
292 { ISD::SINT_TO_FP, MVT::f64, MVT::i16, 2 },
293 { ISD::UINT_TO_FP, MVT::f64, MVT::i16, 2 },
294 { ISD::SINT_TO_FP, MVT::f32, MVT::i32, 2 },
295 { ISD::UINT_TO_FP, MVT::f32, MVT::i32, 2 },
296 { ISD::SINT_TO_FP, MVT::f64, MVT::i32, 2 },
297 { ISD::UINT_TO_FP, MVT::f64, MVT::i32, 2 },
298 { ISD::SINT_TO_FP, MVT::f32, MVT::i64, 10 },
299 { ISD::UINT_TO_FP, MVT::f32, MVT::i64, 10 },
300 { ISD::SINT_TO_FP, MVT::f64, MVT::i64, 10 },
301 { ISD::UINT_TO_FP, MVT::f64, MVT::i64, 10 }
302 };
303
304 if (SrcTy.isInteger() && ST->hasNEON()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000305 if (const auto *Entry = ConvertCostTableLookup(NEONIntegerConversionTbl,
306 ISD, DstTy.getSimpleVT(),
307 SrcTy.getSimpleVT()))
308 return Entry->Cost;
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000309 }
310
311 // Scalar integer conversion costs.
Craig Topper4b275762015-10-28 04:02:12 +0000312 static const TypeConversionCostTblEntry ARMIntegerConversionTbl[] = {
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000313 // i16 -> i64 requires two dependent operations.
314 { ISD::SIGN_EXTEND, MVT::i64, MVT::i16, 2 },
315
316 // Truncates on i64 are assumed to be free.
317 { ISD::TRUNCATE, MVT::i32, MVT::i64, 0 },
318 { ISD::TRUNCATE, MVT::i16, MVT::i64, 0 },
319 { ISD::TRUNCATE, MVT::i8, MVT::i64, 0 },
320 { ISD::TRUNCATE, MVT::i1, MVT::i64, 0 }
321 };
322
323 if (SrcTy.isInteger()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000324 if (const auto *Entry = ConvertCostTableLookup(ARMIntegerConversionTbl, ISD,
325 DstTy.getSimpleVT(),
326 SrcTy.getSimpleVT()))
327 return Entry->Cost;
Arnold Schwaighofera804bbe2013-02-05 14:05:55 +0000328 }
329
Chandler Carruth705b1852015-01-31 03:43:40 +0000330 return BaseT::getCastInstrCost(Opcode, Dst, Src);
Renato Golin5e9d55e2013-01-29 23:31:38 +0000331}
Arnold Schwaighofer98f10122013-02-04 02:52:05 +0000332
Chandler Carruth93205eb2015-08-05 18:08:10 +0000333int ARMTTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy,
334 unsigned Index) {
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000335 // Penalize inserting into an D-subregister. We end up with a three times
336 // lower estimated throughput on swift.
Diana Picus4879b052016-07-06 09:22:23 +0000337 if (ST->hasSlowLoadDSubregister() && Opcode == Instruction::InsertElement &&
338 ValTy->isVectorTy() && ValTy->getScalarSizeInBits() <= 32)
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000339 return 3;
Arnold Schwaighofer98f10122013-02-04 02:52:05 +0000340
James Molloya9f47b62014-09-12 13:29:40 +0000341 if ((Opcode == Instruction::InsertElement ||
Silviu Barangad5ac2692015-08-17 15:57:05 +0000342 Opcode == Instruction::ExtractElement)) {
343 // Cross-class copies are expensive on many microarchitectures,
344 // so assume they are expensive by default.
345 if (ValTy->getVectorElementType()->isIntegerTy())
346 return 3;
347
348 // Even if it's not a cross class copy, this likely leads to mixing
349 // of NEON and VFP code and should be therefore penalized.
350 if (ValTy->isVectorTy() &&
351 ValTy->getScalarSizeInBits() <= 32)
352 return std::max(BaseT::getVectorInstrCost(Opcode, ValTy, Index), 2U);
353 }
James Molloya9f47b62014-09-12 13:29:40 +0000354
Chandler Carruth705b1852015-01-31 03:43:40 +0000355 return BaseT::getVectorInstrCost(Opcode, ValTy, Index);
Arnold Schwaighofer98f10122013-02-04 02:52:05 +0000356}
Arnold Schwaighofer213fced2013-02-07 16:10:15 +0000357
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000358int ARMTTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy,
359 const Instruction *I) {
Arnold Schwaighofer213fced2013-02-07 16:10:15 +0000360 int ISD = TLI->InstructionOpcodeToISD(Opcode);
Hiroshi Inoue7f9f92f2018-02-22 07:48:29 +0000361 // On NEON a vector select gets lowered to vbsl.
Arnold Schwaighofer213fced2013-02-07 16:10:15 +0000362 if (ST->hasNEON() && ValTy->isVectorTy() && ISD == ISD::SELECT) {
Arnold Schwaighofer8070b382013-03-14 19:17:02 +0000363 // Lowering of some vector selects is currently far from perfect.
Craig Topper4b275762015-10-28 04:02:12 +0000364 static const TypeConversionCostTblEntry NEONVectorSelectTbl[] = {
Arnold Schwaighofer8070b382013-03-14 19:17:02 +0000365 { ISD::SELECT, MVT::v4i1, MVT::v4i64, 4*4 + 1*2 + 1 },
366 { ISD::SELECT, MVT::v8i1, MVT::v8i64, 50 },
367 { ISD::SELECT, MVT::v16i1, MVT::v16i64, 100 }
368 };
369
Mehdi Amini44ede332015-07-09 02:09:04 +0000370 EVT SelCondTy = TLI->getValueType(DL, CondTy);
371 EVT SelValTy = TLI->getValueType(DL, ValTy);
Renato Golin0178a252013-08-02 17:10:04 +0000372 if (SelCondTy.isSimple() && SelValTy.isSimple()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000373 if (const auto *Entry = ConvertCostTableLookup(NEONVectorSelectTbl, ISD,
374 SelCondTy.getSimpleVT(),
375 SelValTy.getSimpleVT()))
376 return Entry->Cost;
Renato Golin0178a252013-08-02 17:10:04 +0000377 }
Arnold Schwaighofer8070b382013-03-14 19:17:02 +0000378
Chandler Carruth93205eb2015-08-05 18:08:10 +0000379 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy);
Arnold Schwaighofer213fced2013-02-07 16:10:15 +0000380 return LT.first;
381 }
382
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000383 return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, I);
Arnold Schwaighofer213fced2013-02-07 16:10:15 +0000384}
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000385
Mohammed Agabaria23599ba2017-01-05 14:03:41 +0000386int ARMTTIImpl::getAddressComputationCost(Type *Ty, ScalarEvolution *SE,
387 const SCEV *Ptr) {
Arnold Schwaighoferda2b3112013-07-12 19:16:04 +0000388 // Address computations in vectorized code with non-consecutive addresses will
389 // likely result in more instructions compared to scalar code where the
390 // computation can more often be merged into the index mode. The resulting
391 // extra micro-ops can significantly decrease throughput.
392 unsigned NumVectorInstToHideOverhead = 10;
Mohammed Agabaria23599ba2017-01-05 14:03:41 +0000393 int MaxMergeDistance = 64;
Arnold Schwaighoferda2b3112013-07-12 19:16:04 +0000394
Fangrui Songf78650a2018-07-30 19:41:25 +0000395 if (Ty->isVectorTy() && SE &&
Mohammed Agabaria23599ba2017-01-05 14:03:41 +0000396 !BaseT::isConstantStridedAccessLessThan(SE, Ptr, MaxMergeDistance + 1))
Arnold Schwaighoferda2b3112013-07-12 19:16:04 +0000397 return NumVectorInstToHideOverhead;
398
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000399 // In many cases the address computation is not merged into the instruction
400 // addressing mode.
401 return 1;
402}
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000403
Chandler Carruth93205eb2015-08-05 18:08:10 +0000404int ARMTTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index,
405 Type *SubTp) {
Simon Pilgrim071e8222018-10-25 10:52:36 +0000406 if (Kind == TTI::SK_Broadcast) {
407 static const CostTblEntry NEONDupTbl[] = {
408 // VDUP handles these cases.
409 {ISD::VECTOR_SHUFFLE, MVT::v2i32, 1},
410 {ISD::VECTOR_SHUFFLE, MVT::v2f32, 1},
411 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1},
412 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1},
413 {ISD::VECTOR_SHUFFLE, MVT::v4i16, 1},
414 {ISD::VECTOR_SHUFFLE, MVT::v8i8, 1},
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000415
Simon Pilgrim071e8222018-10-25 10:52:36 +0000416 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 1},
417 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 1},
418 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 1},
419 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 1}};
420
421 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp);
422
423 if (const auto *Entry = CostTableLookup(NEONDupTbl, ISD::VECTOR_SHUFFLE,
424 LT.second))
425 return LT.first * Entry->Cost;
426
427 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
428 }
Chandler Carruth705b1852015-01-31 03:43:40 +0000429 if (Kind == TTI::SK_Reverse) {
Craig Topper4b275762015-10-28 04:02:12 +0000430 static const CostTblEntry NEONShuffleTbl[] = {
Karthik Bhate03a25d2014-06-20 04:32:48 +0000431 // Reverse shuffle cost one instruction if we are shuffling within a
432 // double word (vrev) or two if we shuffle a quad word (vrev, vext).
433 {ISD::VECTOR_SHUFFLE, MVT::v2i32, 1},
434 {ISD::VECTOR_SHUFFLE, MVT::v2f32, 1},
435 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1},
436 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1},
Simon Pilgrim816e57b2018-10-23 09:42:10 +0000437 {ISD::VECTOR_SHUFFLE, MVT::v4i16, 1},
438 {ISD::VECTOR_SHUFFLE, MVT::v8i8, 1},
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000439
Karthik Bhate03a25d2014-06-20 04:32:48 +0000440 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2},
441 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2},
442 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 2},
443 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 2}};
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000444
Chandler Carruth93205eb2015-08-05 18:08:10 +0000445 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp);
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000446
Craig Topperee0c8592015-10-27 04:14:24 +0000447 if (const auto *Entry = CostTableLookup(NEONShuffleTbl, ISD::VECTOR_SHUFFLE,
448 LT.second))
449 return LT.first * Entry->Cost;
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000450
Craig Topperee0c8592015-10-27 04:14:24 +0000451 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
Karthik Bhate03a25d2014-06-20 04:32:48 +0000452 }
Simon Pilgrime39fa6c2018-06-12 16:12:29 +0000453 if (Kind == TTI::SK_Select) {
454 static const CostTblEntry NEONSelShuffleTbl[] = {
455 // Select shuffle cost table for ARM. Cost is the number of instructions
Karthik Bhate03a25d2014-06-20 04:32:48 +0000456 // required to create the shuffled vector.
457
458 {ISD::VECTOR_SHUFFLE, MVT::v2f32, 1},
459 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1},
460 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1},
461 {ISD::VECTOR_SHUFFLE, MVT::v2i32, 1},
462
463 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2},
464 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2},
465 {ISD::VECTOR_SHUFFLE, MVT::v4i16, 2},
466
467 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 16},
468
469 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 32}};
470
Chandler Carruth93205eb2015-08-05 18:08:10 +0000471 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp);
Simon Pilgrime39fa6c2018-06-12 16:12:29 +0000472 if (const auto *Entry = CostTableLookup(NEONSelShuffleTbl,
Craig Topperee0c8592015-10-27 04:14:24 +0000473 ISD::VECTOR_SHUFFLE, LT.second))
474 return LT.first * Entry->Cost;
475 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
Karthik Bhate03a25d2014-06-20 04:32:48 +0000476 }
Chandler Carruth705b1852015-01-31 03:43:40 +0000477 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
Arnold Schwaighofer89aef932013-02-12 02:40:39 +0000478}
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000479
Chandler Carruth93205eb2015-08-05 18:08:10 +0000480int ARMTTIImpl::getArithmeticInstrCost(
Chandler Carruth705b1852015-01-31 03:43:40 +0000481 unsigned Opcode, Type *Ty, TTI::OperandValueKind Op1Info,
482 TTI::OperandValueKind Op2Info, TTI::OperandValueProperties Opd1PropInfo,
Mohammed Agabaria2c96c432017-01-11 08:23:37 +0000483 TTI::OperandValueProperties Opd2PropInfo,
484 ArrayRef<const Value *> Args) {
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000485 int ISDOpcode = TLI->InstructionOpcodeToISD(Opcode);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000486 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000487
488 const unsigned FunctionCallDivCost = 20;
489 const unsigned ReciprocalDivCost = 10;
Craig Topper4b275762015-10-28 04:02:12 +0000490 static const CostTblEntry CostTbl[] = {
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000491 // Division.
492 // These costs are somewhat random. Choose a cost of 20 to indicate that
493 // vectorizing devision (added function call) is going to be very expensive.
494 // Double registers types.
495 { ISD::SDIV, MVT::v1i64, 1 * FunctionCallDivCost},
496 { ISD::UDIV, MVT::v1i64, 1 * FunctionCallDivCost},
497 { ISD::SREM, MVT::v1i64, 1 * FunctionCallDivCost},
498 { ISD::UREM, MVT::v1i64, 1 * FunctionCallDivCost},
499 { ISD::SDIV, MVT::v2i32, 2 * FunctionCallDivCost},
500 { ISD::UDIV, MVT::v2i32, 2 * FunctionCallDivCost},
501 { ISD::SREM, MVT::v2i32, 2 * FunctionCallDivCost},
502 { ISD::UREM, MVT::v2i32, 2 * FunctionCallDivCost},
503 { ISD::SDIV, MVT::v4i16, ReciprocalDivCost},
504 { ISD::UDIV, MVT::v4i16, ReciprocalDivCost},
505 { ISD::SREM, MVT::v4i16, 4 * FunctionCallDivCost},
506 { ISD::UREM, MVT::v4i16, 4 * FunctionCallDivCost},
507 { ISD::SDIV, MVT::v8i8, ReciprocalDivCost},
508 { ISD::UDIV, MVT::v8i8, ReciprocalDivCost},
509 { ISD::SREM, MVT::v8i8, 8 * FunctionCallDivCost},
510 { ISD::UREM, MVT::v8i8, 8 * FunctionCallDivCost},
511 // Quad register types.
512 { ISD::SDIV, MVT::v2i64, 2 * FunctionCallDivCost},
513 { ISD::UDIV, MVT::v2i64, 2 * FunctionCallDivCost},
514 { ISD::SREM, MVT::v2i64, 2 * FunctionCallDivCost},
515 { ISD::UREM, MVT::v2i64, 2 * FunctionCallDivCost},
516 { ISD::SDIV, MVT::v4i32, 4 * FunctionCallDivCost},
517 { ISD::UDIV, MVT::v4i32, 4 * FunctionCallDivCost},
518 { ISD::SREM, MVT::v4i32, 4 * FunctionCallDivCost},
519 { ISD::UREM, MVT::v4i32, 4 * FunctionCallDivCost},
520 { ISD::SDIV, MVT::v8i16, 8 * FunctionCallDivCost},
521 { ISD::UDIV, MVT::v8i16, 8 * FunctionCallDivCost},
522 { ISD::SREM, MVT::v8i16, 8 * FunctionCallDivCost},
523 { ISD::UREM, MVT::v8i16, 8 * FunctionCallDivCost},
524 { ISD::SDIV, MVT::v16i8, 16 * FunctionCallDivCost},
525 { ISD::UDIV, MVT::v16i8, 16 * FunctionCallDivCost},
526 { ISD::SREM, MVT::v16i8, 16 * FunctionCallDivCost},
527 { ISD::UREM, MVT::v16i8, 16 * FunctionCallDivCost},
528 // Multiplication.
529 };
530
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000531 if (ST->hasNEON())
Craig Topperee0c8592015-10-27 04:14:24 +0000532 if (const auto *Entry = CostTableLookup(CostTbl, ISDOpcode, LT.second))
533 return LT.first * Entry->Cost;
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000534
Chandler Carruth93205eb2015-08-05 18:08:10 +0000535 int Cost = BaseT::getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info,
536 Opd1PropInfo, Opd2PropInfo);
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000537
Arnold Schwaighofer77af0f62013-10-29 01:33:53 +0000538 // This is somewhat of a hack. The problem that we are facing is that SROA
539 // creates a sequence of shift, and, or instructions to construct values.
540 // These sequences are recognized by the ISel and have zero-cost. Not so for
541 // the vectorized code. Because we have support for v2i64 but not i64 those
Alp Tokercb402912014-01-24 17:20:08 +0000542 // sequences look particularly beneficial to vectorize.
Arnold Schwaighofer77af0f62013-10-29 01:33:53 +0000543 // To work around this we increase the cost of v2i64 operations to make them
544 // seem less beneficial.
545 if (LT.second == MVT::v2i64 &&
546 Op2Info == TargetTransformInfo::OK_UniformConstantValue)
547 Cost += 4;
548
549 return Cost;
Arnold Schwaighofer9881dcf2013-04-25 21:16:18 +0000550}
551
Chandler Carruth93205eb2015-08-05 18:08:10 +0000552int ARMTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000553 unsigned AddressSpace, const Instruction *I) {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000554 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Src);
Arnold Schwaighofer89ae2172013-10-29 01:33:57 +0000555
556 if (Src->isVectorTy() && Alignment != 16 &&
557 Src->getVectorElementType()->isDoubleTy()) {
558 // Unaligned loads/stores are extremely inefficient.
559 // We need 4 uops for vst.1/vld.1 vs 1uop for vldr/vstr.
560 return LT.first * 4;
561 }
562 return LT.first;
563}
Hao Liu2cd34bb2015-06-26 02:45:36 +0000564
Chandler Carruth93205eb2015-08-05 18:08:10 +0000565int ARMTTIImpl::getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy,
566 unsigned Factor,
567 ArrayRef<unsigned> Indices,
568 unsigned Alignment,
Dorit Nuzman38bbf812018-10-14 08:50:06 +0000569 unsigned AddressSpace,
Dorit Nuzman34da6dd2018-10-31 09:57:56 +0000570 bool UseMaskForCond,
571 bool UseMaskForGaps) {
Hao Liu2cd34bb2015-06-26 02:45:36 +0000572 assert(Factor >= 2 && "Invalid interleave factor");
573 assert(isa<VectorType>(VecTy) && "Expect a vector type");
574
575 // vldN/vstN doesn't support vector types of i64/f64 element.
Ahmed Bougacha97564c32015-12-09 01:19:50 +0000576 bool EltIs64Bits = DL.getTypeSizeInBits(VecTy->getScalarType()) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +0000577
Dorit Nuzman38bbf812018-10-14 08:50:06 +0000578 if (Factor <= TLI->getMaxSupportedInterleaveFactor() && !EltIs64Bits &&
Dorit Nuzman34da6dd2018-10-31 09:57:56 +0000579 !UseMaskForCond && !UseMaskForGaps) {
Hao Liu2cd34bb2015-06-26 02:45:36 +0000580 unsigned NumElts = VecTy->getVectorNumElements();
Matthew Simpson1468d3e2017-04-10 18:34:37 +0000581 auto *SubVecTy = VectorType::get(VecTy->getScalarType(), NumElts / Factor);
Hao Liu2cd34bb2015-06-26 02:45:36 +0000582
583 // vldN/vstN only support legal vector types of size 64 or 128 in bits.
Matthew Simpsonaee97712017-03-02 15:15:35 +0000584 // Accesses having vector types that are a multiple of 128 bits can be
585 // matched to more than one vldN/vstN instruction.
Matthew Simpson1468d3e2017-04-10 18:34:37 +0000586 if (NumElts % Factor == 0 &&
587 TLI->isLegalInterleavedAccessType(SubVecTy, DL))
588 return Factor * TLI->getNumInterleavedAccesses(SubVecTy, DL);
Hao Liu2cd34bb2015-06-26 02:45:36 +0000589 }
590
591 return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
Dorit Nuzman34da6dd2018-10-31 09:57:56 +0000592 Alignment, AddressSpace,
593 UseMaskForCond, UseMaskForGaps);
Hao Liu2cd34bb2015-06-26 02:45:36 +0000594}
Sam Parker19a08e42017-07-25 08:51:30 +0000595
596void ARMTTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
597 TTI::UnrollingPreferences &UP) {
598 // Only currently enable these preferences for M-Class cores.
Sam Parker84fd0c32017-08-16 07:42:44 +0000599 if (!ST->isMClass())
Sam Parker19a08e42017-07-25 08:51:30 +0000600 return BasicTTIImplBase::getUnrollingPreferences(L, SE, UP);
601
602 // Disable loop unrolling for Oz and Os.
603 UP.OptSizeThreshold = 0;
604 UP.PartialOptSizeThreshold = 0;
Sam Parker487ab862017-10-23 08:05:14 +0000605 if (L->getHeader()->getParent()->optForSize())
606 return;
607
608 // Only enable on Thumb-2 targets.
609 if (!ST->isThumb2())
610 return;
611
612 SmallVector<BasicBlock*, 4> ExitingBlocks;
613 L->getExitingBlocks(ExitingBlocks);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000614 LLVM_DEBUG(dbgs() << "Loop has:\n"
615 << "Blocks: " << L->getNumBlocks() << "\n"
616 << "Exit blocks: " << ExitingBlocks.size() << "\n");
Sam Parker487ab862017-10-23 08:05:14 +0000617
618 // Only allow another exit other than the latch. This acts as an early exit
619 // as it mirrors the profitability calculation of the runtime unroller.
620 if (ExitingBlocks.size() > 2)
621 return;
622
623 // Limit the CFG of the loop body for targets with a branch predictor.
624 // Allowing 4 blocks permits if-then-else diamonds in the body.
625 if (ST->hasBranchPredictor() && L->getNumBlocks() > 4)
Sam Parker84fd0c32017-08-16 07:42:44 +0000626 return;
Sam Parker19a08e42017-07-25 08:51:30 +0000627
628 // Scan the loop: don't unroll loops with calls as this could prevent
629 // inlining.
Sam Parker84fd0c32017-08-16 07:42:44 +0000630 unsigned Cost = 0;
Sam Parker487ab862017-10-23 08:05:14 +0000631 for (auto *BB : L->getBlocks()) {
632 for (auto &I : *BB) {
633 if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
634 ImmutableCallSite CS(&I);
635 if (const Function *F = CS.getCalledFunction()) {
636 if (!isLoweredToCall(F))
637 continue;
638 }
639 return;
Sam Parker19a08e42017-07-25 08:51:30 +0000640 }
Sam Parker487ab862017-10-23 08:05:14 +0000641 SmallVector<const Value*, 4> Operands(I.value_op_begin(),
642 I.value_op_end());
643 Cost += getUserCost(&I, Operands);
Sam Parker19a08e42017-07-25 08:51:30 +0000644 }
645 }
646
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000647 LLVM_DEBUG(dbgs() << "Cost of loop: " << Cost << "\n");
Sam Parker487ab862017-10-23 08:05:14 +0000648
Sam Parker19a08e42017-07-25 08:51:30 +0000649 UP.Partial = true;
650 UP.Runtime = true;
Sam Parker84fd0c32017-08-16 07:42:44 +0000651 UP.UnrollRemainder = true;
652 UP.DefaultUnrollRuntimeCount = 4;
David Green963401d2018-07-01 12:47:30 +0000653 UP.UnrollAndJam = true;
654 UP.UnrollAndJamInnerLoopThreshold = 60;
Sam Parker84fd0c32017-08-16 07:42:44 +0000655
656 // Force unrolling small loops can be very useful because of the branch
657 // taken cost of the backedge.
658 if (Cost < 12)
659 UP.Force = true;
Sam Parker19a08e42017-07-25 08:51:30 +0000660}