1 //===-- ARMTargetTransformInfo.cpp - ARM specific TTI pass ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This file implements a TargetTransformInfo analysis pass specific to the
11 /// ARM target machine. It uses the target's detailed information to provide
12 /// more precise answers to certain TTI queries, while letting the target
13 /// independent and default TTI implementations handle the rest.
15 //===----------------------------------------------------------------------===//
17 #define DEBUG_TYPE "armtti"
19 #include "ARMTargetMachine.h"
20 #include "llvm/Analysis/TargetTransformInfo.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Target/TargetLowering.h"
23 #include "llvm/Target/CostTable.h"
26 // Declare the pass initialization routine locally as target-specific passes
27 // don't havve a target-wide initialization entry point, and so we rely on the
28 // pass constructor initialization.
30 void initializeARMTTIPass(PassRegistry &);
35 class ARMTTI : public ImmutablePass, public TargetTransformInfo {
36 const ARMBaseTargetMachine *TM;
37 const ARMSubtarget *ST;
38 const ARMTargetLowering *TLI;
40 /// Estimate the overhead of scalarizing an instruction. Insert and Extract
41 /// are set if the result needs to be inserted and/or extracted from vectors.
42 unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const;
45 ARMTTI() : ImmutablePass(ID), TM(0), ST(0), TLI(0) {
46 llvm_unreachable("This pass cannot be directly constructed");
49 ARMTTI(const ARMBaseTargetMachine *TM)
50 : ImmutablePass(ID), TM(TM), ST(TM->getSubtargetImpl()),
51 TLI(TM->getTargetLowering()) {
52 initializeARMTTIPass(*PassRegistry::getPassRegistry());
55 virtual void initializePass() {
59 virtual void finalizePass() {
63 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
64 TargetTransformInfo::getAnalysisUsage(AU);
67 /// Pass identification.
70 /// Provide necessary pointer adjustments for the two base classes.
71 virtual void *getAdjustedAnalysisPointer(const void *ID) {
72 if (ID == &TargetTransformInfo::ID)
73 return (TargetTransformInfo*)this;
77 /// \name Scalar TTI Implementations
80 virtual unsigned getIntImmCost(const APInt &Imm, Type *Ty) const;
85 /// \name Vector TTI Implementations
88 unsigned getNumberOfRegisters(bool Vector) const {
95 if (ST->isThumb1Only())
100 unsigned getRegisterBitWidth(bool Vector) const {
110 unsigned getMaximumUnrollFactor() const {
111 // These are out of order CPUs:
112 if (ST->isCortexA15() || ST->isSwift())
117 unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
120 unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy) const;
122 unsigned getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) const;
124 unsigned getAddressComputationCost(Type *Val) const;
128 } // end anonymous namespace
130 INITIALIZE_AG_PASS(ARMTTI, TargetTransformInfo, "armtti",
131 "ARM Target Transform Info", true, true, false)
135 llvm::createARMTargetTransformInfoPass(const ARMBaseTargetMachine *TM) {
136 return new ARMTTI(TM);
140 unsigned ARMTTI::getIntImmCost(const APInt &Imm, Type *Ty) const {
141 assert(Ty->isIntegerTy());
143 unsigned Bits = Ty->getPrimitiveSizeInBits();
144 if (Bits == 0 || Bits > 32)
147 int32_t SImmVal = Imm.getSExtValue();
148 uint32_t ZImmVal = Imm.getZExtValue();
149 if (!ST->isThumb()) {
150 if ((SImmVal >= 0 && SImmVal < 65536) ||
151 (ARM_AM::getSOImmVal(ZImmVal) != -1) ||
152 (ARM_AM::getSOImmVal(~ZImmVal) != -1))
154 return ST->hasV6T2Ops() ? 2 : 3;
155 } else if (ST->isThumb2()) {
156 if ((SImmVal >= 0 && SImmVal < 65536) ||
157 (ARM_AM::getT2SOImmVal(ZImmVal) != -1) ||
158 (ARM_AM::getT2SOImmVal(~ZImmVal) != -1))
160 return ST->hasV6T2Ops() ? 2 : 3;
162 if (SImmVal >= 0 && SImmVal < 256)
164 if ((~ZImmVal < 256) || ARM_AM::isThumbImmShiftedVal(ZImmVal))
166 // Load from constantpool.
172 unsigned ARMTTI::getCastInstrCost(unsigned Opcode, Type *Dst,
174 int ISD = TLI->InstructionOpcodeToISD(Opcode);
175 assert(ISD && "Invalid opcode");
177 EVT SrcTy = TLI->getValueType(Src);
178 EVT DstTy = TLI->getValueType(Dst);
180 if (!SrcTy.isSimple() || !DstTy.isSimple())
181 return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src);
183 // Some arithmetic, load and store operations have specific instructions
184 // to cast up/down their types automatically at no extra cost.
185 // TODO: Get these tables to know at least what the related operations are.
186 static const TypeConversionCostTblEntry<MVT> NEONVectorConversionTbl[] = {
187 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i16, 0 },
188 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i16, 0 },
189 { ISD::SIGN_EXTEND, MVT::v2i64, MVT::v2i32, 1 },
190 { ISD::ZERO_EXTEND, MVT::v2i64, MVT::v2i32, 1 },
191 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 0 },
192 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i32, 1 },
194 // Vector float <-> i32 conversions.
195 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 },
196 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 },
197 { ISD::FP_TO_SINT, MVT::v4i32, MVT::v4f32, 1 },
198 { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f32, 1 },
200 // Vector double <-> i32 conversions.
201 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 },
202 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 },
203 { ISD::FP_TO_SINT, MVT::v2i32, MVT::v2f64, 2 },
204 { ISD::FP_TO_UINT, MVT::v2i32, MVT::v2f64, 2 }
207 if (SrcTy.isVector() && ST->hasNEON()) {
208 int Idx = ConvertCostTableLookup<MVT>(NEONVectorConversionTbl,
209 array_lengthof(NEONVectorConversionTbl),
210 ISD, DstTy.getSimpleVT(), SrcTy.getSimpleVT());
212 return NEONVectorConversionTbl[Idx].Cost;
215 // Scalar float to integer conversions.
216 static const TypeConversionCostTblEntry<MVT> NEONFloatConversionTbl[] = {
217 { ISD::FP_TO_SINT, MVT::i1, MVT::f32, 2 },
218 { ISD::FP_TO_UINT, MVT::i1, MVT::f32, 2 },
219 { ISD::FP_TO_SINT, MVT::i1, MVT::f64, 2 },
220 { ISD::FP_TO_UINT, MVT::i1, MVT::f64, 2 },
221 { ISD::FP_TO_SINT, MVT::i8, MVT::f32, 2 },
222 { ISD::FP_TO_UINT, MVT::i8, MVT::f32, 2 },
223 { ISD::FP_TO_SINT, MVT::i8, MVT::f64, 2 },
224 { ISD::FP_TO_UINT, MVT::i8, MVT::f64, 2 },
225 { ISD::FP_TO_SINT, MVT::i16, MVT::f32, 2 },
226 { ISD::FP_TO_UINT, MVT::i16, MVT::f32, 2 },
227 { ISD::FP_TO_SINT, MVT::i16, MVT::f64, 2 },
228 { ISD::FP_TO_UINT, MVT::i16, MVT::f64, 2 },
229 { ISD::FP_TO_SINT, MVT::i32, MVT::f32, 2 },
230 { ISD::FP_TO_UINT, MVT::i32, MVT::f32, 2 },
231 { ISD::FP_TO_SINT, MVT::i32, MVT::f64, 2 },
232 { ISD::FP_TO_UINT, MVT::i32, MVT::f64, 2 },
233 { ISD::FP_TO_SINT, MVT::i64, MVT::f32, 10 },
234 { ISD::FP_TO_UINT, MVT::i64, MVT::f32, 10 },
235 { ISD::FP_TO_SINT, MVT::i64, MVT::f64, 10 },
236 { ISD::FP_TO_UINT, MVT::i64, MVT::f64, 10 }
238 if (SrcTy.isFloatingPoint() && ST->hasNEON()) {
239 int Idx = ConvertCostTableLookup<MVT>(NEONFloatConversionTbl,
240 array_lengthof(NEONFloatConversionTbl),
241 ISD, DstTy.getSimpleVT(),
242 SrcTy.getSimpleVT());
244 return NEONFloatConversionTbl[Idx].Cost;
248 // Scalar integer to float conversions.
249 static const TypeConversionCostTblEntry<MVT> NEONIntegerConversionTbl[] = {
250 { ISD::SINT_TO_FP, MVT::f32, MVT::i1, 2 },
251 { ISD::UINT_TO_FP, MVT::f32, MVT::i1, 2 },
252 { ISD::SINT_TO_FP, MVT::f64, MVT::i1, 2 },
253 { ISD::UINT_TO_FP, MVT::f64, MVT::i1, 2 },
254 { ISD::SINT_TO_FP, MVT::f32, MVT::i8, 2 },
255 { ISD::UINT_TO_FP, MVT::f32, MVT::i8, 2 },
256 { ISD::SINT_TO_FP, MVT::f64, MVT::i8, 2 },
257 { ISD::UINT_TO_FP, MVT::f64, MVT::i8, 2 },
258 { ISD::SINT_TO_FP, MVT::f32, MVT::i16, 2 },
259 { ISD::UINT_TO_FP, MVT::f32, MVT::i16, 2 },
260 { ISD::SINT_TO_FP, MVT::f64, MVT::i16, 2 },
261 { ISD::UINT_TO_FP, MVT::f64, MVT::i16, 2 },
262 { ISD::SINT_TO_FP, MVT::f32, MVT::i32, 2 },
263 { ISD::UINT_TO_FP, MVT::f32, MVT::i32, 2 },
264 { ISD::SINT_TO_FP, MVT::f64, MVT::i32, 2 },
265 { ISD::UINT_TO_FP, MVT::f64, MVT::i32, 2 },
266 { ISD::SINT_TO_FP, MVT::f32, MVT::i64, 10 },
267 { ISD::UINT_TO_FP, MVT::f32, MVT::i64, 10 },
268 { ISD::SINT_TO_FP, MVT::f64, MVT::i64, 10 },
269 { ISD::UINT_TO_FP, MVT::f64, MVT::i64, 10 }
272 if (SrcTy.isInteger() && ST->hasNEON()) {
273 int Idx = ConvertCostTableLookup<MVT>(NEONIntegerConversionTbl,
274 array_lengthof(NEONIntegerConversionTbl),
275 ISD, DstTy.getSimpleVT(),
276 SrcTy.getSimpleVT());
278 return NEONIntegerConversionTbl[Idx].Cost;
281 // Scalar integer conversion costs.
282 static const TypeConversionCostTblEntry<MVT> ARMIntegerConversionTbl[] = {
283 // i16 -> i64 requires two dependent operations.
284 { ISD::SIGN_EXTEND, MVT::i64, MVT::i16, 2 },
286 // Truncates on i64 are assumed to be free.
287 { ISD::TRUNCATE, MVT::i32, MVT::i64, 0 },
288 { ISD::TRUNCATE, MVT::i16, MVT::i64, 0 },
289 { ISD::TRUNCATE, MVT::i8, MVT::i64, 0 },
290 { ISD::TRUNCATE, MVT::i1, MVT::i64, 0 }
293 if (SrcTy.isInteger()) {
295 ConvertCostTableLookup<MVT>(ARMIntegerConversionTbl,
296 array_lengthof(ARMIntegerConversionTbl),
297 ISD, DstTy.getSimpleVT(),
298 SrcTy.getSimpleVT());
300 return ARMIntegerConversionTbl[Idx].Cost;
304 return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src);
307 unsigned ARMTTI::getVectorInstrCost(unsigned Opcode, Type *ValTy,
308 unsigned Index) const {
309 // Penalize inserting into an D-subregister. We end up with a three times
310 // lower estimated throughput on swift.
312 Opcode == Instruction::InsertElement &&
313 ValTy->isVectorTy() &&
314 ValTy->getScalarSizeInBits() <= 32)
317 return TargetTransformInfo::getVectorInstrCost(Opcode, ValTy, Index);
320 unsigned ARMTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
321 Type *CondTy) const {
323 int ISD = TLI->InstructionOpcodeToISD(Opcode);
324 // On NEON a a vector select gets lowered to vbsl.
325 if (ST->hasNEON() && ValTy->isVectorTy() && ISD == ISD::SELECT) {
326 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
330 return TargetTransformInfo::getCmpSelInstrCost(Opcode, ValTy, CondTy);
333 unsigned ARMTTI::getAddressComputationCost(Type *Ty) const {
334 // In many cases the address computation is not merged into the instruction