ARM cost model: Address computation in vector mem ops not free
[oota-llvm.git] / lib / Target / ARM / ARMTargetTransformInfo.cpp
1 //===-- ARMTargetTransformInfo.cpp - ARM specific TTI pass ----------------===//
2 //
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 //===----------------------------------------------------------------------===//
9 /// \file
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.
14 ///
15 //===----------------------------------------------------------------------===//
16
17 #define DEBUG_TYPE "armtti"
18 #include "ARM.h"
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"
24 using namespace llvm;
25
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.
29 namespace llvm {
30 void initializeARMTTIPass(PassRegistry &);
31 }
32
33 namespace {
34
35 class ARMTTI : public ImmutablePass, public TargetTransformInfo {
36   const ARMBaseTargetMachine *TM;
37   const ARMSubtarget *ST;
38   const ARMTargetLowering *TLI;
39
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;
43
44 public:
45   ARMTTI() : ImmutablePass(ID), TM(0), ST(0), TLI(0) {
46     llvm_unreachable("This pass cannot be directly constructed");
47   }
48
49   ARMTTI(const ARMBaseTargetMachine *TM)
50       : ImmutablePass(ID), TM(TM), ST(TM->getSubtargetImpl()),
51         TLI(TM->getTargetLowering()) {
52     initializeARMTTIPass(*PassRegistry::getPassRegistry());
53   }
54
55   virtual void initializePass() {
56     pushTTIStack(this);
57   }
58
59   virtual void finalizePass() {
60     popTTIStack();
61   }
62
63   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
64     TargetTransformInfo::getAnalysisUsage(AU);
65   }
66
67   /// Pass identification.
68   static char ID;
69
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;
74     return this;
75   }
76
77   /// \name Scalar TTI Implementations
78   /// @{
79
80   virtual unsigned getIntImmCost(const APInt &Imm, Type *Ty) const;
81
82   /// @}
83
84
85   /// \name Vector TTI Implementations
86   /// @{
87
88   unsigned getNumberOfRegisters(bool Vector) const {
89     if (Vector) {
90       if (ST->hasNEON())
91         return 16;
92       return 0;
93     }
94
95     if (ST->isThumb1Only())
96       return 8;
97     return 16;
98   }
99
100   unsigned getRegisterBitWidth(bool Vector) const {
101     if (Vector) {
102       if (ST->hasNEON())
103         return 128;
104       return 0;
105     }
106
107     return 32;
108   }
109
110   unsigned getMaximumUnrollFactor() const {
111     // These are out of order CPUs:
112     if (ST->isCortexA15() || ST->isSwift())
113       return 2;
114     return 1;
115   }
116
117   unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
118                                       Type *Src) const;
119
120   unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy) const;
121
122   unsigned getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) const;
123
124   unsigned getAddressComputationCost(Type *Val) const;
125   /// @}
126 };
127
128 } // end anonymous namespace
129
130 INITIALIZE_AG_PASS(ARMTTI, TargetTransformInfo, "armtti",
131                    "ARM Target Transform Info", true, true, false)
132 char ARMTTI::ID = 0;
133
134 ImmutablePass *
135 llvm::createARMTargetTransformInfoPass(const ARMBaseTargetMachine *TM) {
136   return new ARMTTI(TM);
137 }
138
139
140 unsigned ARMTTI::getIntImmCost(const APInt &Imm, Type *Ty) const {
141   assert(Ty->isIntegerTy());
142
143   unsigned Bits = Ty->getPrimitiveSizeInBits();
144   if (Bits == 0 || Bits > 32)
145     return 4;
146
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))
153       return 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))
159       return 1;
160     return ST->hasV6T2Ops() ? 2 : 3;
161   } else /*Thumb1*/ {
162     if (SImmVal >= 0 && SImmVal < 256)
163       return 1;
164     if ((~ZImmVal < 256) || ARM_AM::isThumbImmShiftedVal(ZImmVal))
165       return 2;
166     // Load from constantpool.
167     return 3;
168   }
169   return 2;
170 }
171
172 unsigned ARMTTI::getCastInstrCost(unsigned Opcode, Type *Dst,
173                                     Type *Src) const {
174   int ISD = TLI->InstructionOpcodeToISD(Opcode);
175   assert(ISD && "Invalid opcode");
176
177   EVT SrcTy = TLI->getValueType(Src);
178   EVT DstTy = TLI->getValueType(Dst);
179
180   if (!SrcTy.isSimple() || !DstTy.isSimple())
181     return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src);
182
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 },
193
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 },
199
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 }
205   };
206
207   if (SrcTy.isVector() && ST->hasNEON()) {
208     int Idx = ConvertCostTableLookup<MVT>(NEONVectorConversionTbl,
209                                 array_lengthof(NEONVectorConversionTbl),
210                                 ISD, DstTy.getSimpleVT(), SrcTy.getSimpleVT());
211     if (Idx != -1)
212       return NEONVectorConversionTbl[Idx].Cost;
213   }
214
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 }
237   };
238   if (SrcTy.isFloatingPoint() && ST->hasNEON()) {
239     int Idx = ConvertCostTableLookup<MVT>(NEONFloatConversionTbl,
240                                         array_lengthof(NEONFloatConversionTbl),
241                                         ISD, DstTy.getSimpleVT(),
242                                         SrcTy.getSimpleVT());
243     if (Idx != -1)
244         return NEONFloatConversionTbl[Idx].Cost;
245   }
246
247
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 }
270   };
271
272   if (SrcTy.isInteger() && ST->hasNEON()) {
273     int Idx = ConvertCostTableLookup<MVT>(NEONIntegerConversionTbl,
274                                        array_lengthof(NEONIntegerConversionTbl),
275                                        ISD, DstTy.getSimpleVT(),
276                                        SrcTy.getSimpleVT());
277     if (Idx != -1)
278       return NEONIntegerConversionTbl[Idx].Cost;
279   }
280
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 },
285
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 }
291   };
292
293   if (SrcTy.isInteger()) {
294     int Idx =
295       ConvertCostTableLookup<MVT>(ARMIntegerConversionTbl,
296                                   array_lengthof(ARMIntegerConversionTbl),
297                                   ISD, DstTy.getSimpleVT(),
298                                   SrcTy.getSimpleVT());
299     if (Idx != -1)
300       return ARMIntegerConversionTbl[Idx].Cost;
301   }
302
303
304   return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src);
305 }
306
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.
311   if (ST->isSwift() &&
312       Opcode == Instruction::InsertElement &&
313       ValTy->isVectorTy() &&
314       ValTy->getScalarSizeInBits() <= 32)
315     return 3;
316
317   return TargetTransformInfo::getVectorInstrCost(Opcode, ValTy, Index);
318 }
319
320 unsigned ARMTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
321                                     Type *CondTy) const {
322
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);
327     return LT.first;
328   }
329
330   return TargetTransformInfo::getCmpSelInstrCost(Opcode, ValTy, CondTy);
331 }
332
333 unsigned ARMTTI::getAddressComputationCost(Type *Ty) const {
334   // In many cases the address computation is not merged into the instruction
335   // addressing mode.
336   return 1;
337 }