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