ARM cost model: Penalize insertelement into D subregisters
[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 getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) const;
121   /// @}
122 };
123
124 } // end anonymous namespace
125
126 INITIALIZE_AG_PASS(ARMTTI, TargetTransformInfo, "armtti",
127                    "ARM Target Transform Info", true, true, false)
128 char ARMTTI::ID = 0;
129
130 ImmutablePass *
131 llvm::createARMTargetTransformInfoPass(const ARMBaseTargetMachine *TM) {
132   return new ARMTTI(TM);
133 }
134
135
136 unsigned ARMTTI::getIntImmCost(const APInt &Imm, Type *Ty) const {
137   assert(Ty->isIntegerTy());
138
139   unsigned Bits = Ty->getPrimitiveSizeInBits();
140   if (Bits == 0 || Bits > 32)
141     return 4;
142
143   int32_t SImmVal = Imm.getSExtValue();
144   uint32_t ZImmVal = Imm.getZExtValue();
145   if (!ST->isThumb()) {
146     if ((SImmVal >= 0 && SImmVal < 65536) ||
147         (ARM_AM::getSOImmVal(ZImmVal) != -1) ||
148         (ARM_AM::getSOImmVal(~ZImmVal) != -1))
149       return 1;
150     return ST->hasV6T2Ops() ? 2 : 3;
151   } else if (ST->isThumb2()) {
152     if ((SImmVal >= 0 && SImmVal < 65536) ||
153         (ARM_AM::getT2SOImmVal(ZImmVal) != -1) ||
154         (ARM_AM::getT2SOImmVal(~ZImmVal) != -1))
155       return 1;
156     return ST->hasV6T2Ops() ? 2 : 3;
157   } else /*Thumb1*/ {
158     if (SImmVal >= 0 && SImmVal < 256)
159       return 1;
160     if ((~ZImmVal < 256) || ARM_AM::isThumbImmShiftedVal(ZImmVal))
161       return 2;
162     // Load from constantpool.
163     return 3;
164   }
165   return 2;
166 }
167
168 unsigned ARMTTI::getCastInstrCost(unsigned Opcode, Type *Dst,
169                                     Type *Src) const {
170   int ISD = TLI->InstructionOpcodeToISD(Opcode);
171   assert(ISD && "Invalid opcode");
172
173   EVT SrcTy = TLI->getValueType(Src);
174   EVT DstTy = TLI->getValueType(Dst);
175
176   if (!SrcTy.isSimple() || !DstTy.isSimple())
177     return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src);
178
179   // Some arithmetic, load and store operations have specific instructions
180   // to cast up/down their types automatically at no extra cost
181   // TODO: Get these tables to know at least what the related operations are
182   static const TypeConversionCostTblEntry<MVT> NEONConversionTbl[] = {
183     { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i16, 0 },
184     { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i16, 0 },
185     { ISD::SIGN_EXTEND, MVT::v2i64, MVT::v2i32, 1 },
186     { ISD::ZERO_EXTEND, MVT::v2i64, MVT::v2i32, 1 },
187     { ISD::TRUNCATE,    MVT::v4i32, MVT::v4i64, 0 },
188     { ISD::TRUNCATE,    MVT::v4i16, MVT::v4i32, 1 },
189   };
190
191   if (ST->hasNEON()) {
192     int Idx = ConvertCostTableLookup<MVT>(NEONConversionTbl,
193                                 array_lengthof(NEONConversionTbl),
194                                 ISD, DstTy.getSimpleVT(), SrcTy.getSimpleVT());
195     if (Idx != -1)
196       return NEONConversionTbl[Idx].Cost;
197   }
198
199   return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src);
200 }
201
202 unsigned ARMTTI::getVectorInstrCost(unsigned Opcode, Type *ValTy,
203                                     unsigned Index) const {
204   // Penalize inserting into an D-subregister.
205   if (ST->isSwift() &&
206       Opcode == Instruction::InsertElement &&
207       ValTy->isVectorTy() &&
208       ValTy->getScalarSizeInBits() <= 32)
209     return 2;
210
211   return TargetTransformInfo::getVectorInstrCost(Opcode, ValTy, Index);
212 }