LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ARMTargetTransformInfo.cpp
Go to the documentation of this file.
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"
21 #include "llvm/Support/Debug.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 {
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()) {
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 {
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 getShuffleCost(ShuffleKind Kind, Type *Tp,
118  int Index, Type *SubTp) const;
119 
120  unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
121  Type *Src) const;
122 
123  unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy) const;
124 
125  unsigned getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) const;
126 
127  unsigned getAddressComputationCost(Type *Val, bool IsComplex) const;
128 
129  unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty,
130  OperandValueKind Op1Info = OK_AnyValue,
131  OperandValueKind Op2Info = OK_AnyValue) const;
132 
133  unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
134  unsigned AddressSpace) const;
135  /// @}
136 };
137 
138 } // end anonymous namespace
139 
140 INITIALIZE_AG_PASS(ARMTTI, TargetTransformInfo, "armtti",
141  "ARM Target Transform Info", true, true, false)
142 char ARMTTI::ID = 0;
143 
146  return new ARMTTI(TM);
147 }
148 
149 
150 unsigned ARMTTI::getIntImmCost(const APInt &Imm, Type *Ty) const {
151  assert(Ty->isIntegerTy());
152 
153  unsigned Bits = Ty->getPrimitiveSizeInBits();
154  if (Bits == 0 || Bits > 32)
155  return 4;
156 
157  int32_t SImmVal = Imm.getSExtValue();
158  uint32_t ZImmVal = Imm.getZExtValue();
159  if (!ST->isThumb()) {
160  if ((SImmVal >= 0 && SImmVal < 65536) ||
161  (ARM_AM::getSOImmVal(ZImmVal) != -1) ||
162  (ARM_AM::getSOImmVal(~ZImmVal) != -1))
163  return 1;
164  return ST->hasV6T2Ops() ? 2 : 3;
165  } else if (ST->isThumb2()) {
166  if ((SImmVal >= 0 && SImmVal < 65536) ||
167  (ARM_AM::getT2SOImmVal(ZImmVal) != -1) ||
168  (ARM_AM::getT2SOImmVal(~ZImmVal) != -1))
169  return 1;
170  return ST->hasV6T2Ops() ? 2 : 3;
171  } else /*Thumb1*/ {
172  if (SImmVal >= 0 && SImmVal < 256)
173  return 1;
174  if ((~ZImmVal < 256) || ARM_AM::isThumbImmShiftedVal(ZImmVal))
175  return 2;
176  // Load from constantpool.
177  return 3;
178  }
179  return 2;
180 }
181 
182 unsigned ARMTTI::getCastInstrCost(unsigned Opcode, Type *Dst,
183  Type *Src) const {
184  int ISD = TLI->InstructionOpcodeToISD(Opcode);
185  assert(ISD && "Invalid opcode");
186 
187  // Single to/from double precision conversions.
188  static const CostTblEntry<MVT::SimpleValueType> NEONFltDblTbl[] = {
189  // Vector fptrunc/fpext conversions.
190  { ISD::FP_ROUND, MVT::v2f64, 2 },
191  { ISD::FP_EXTEND, MVT::v2f32, 2 },
192  { ISD::FP_EXTEND, MVT::v4f32, 4 }
193  };
194 
195  if (Src->isVectorTy() && ST->hasNEON() && (ISD == ISD::FP_ROUND ||
196  ISD == ISD::FP_EXTEND)) {
197  std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Src);
198  int Idx = CostTableLookup(NEONFltDblTbl, ISD, LT.second);
199  if (Idx != -1)
200  return LT.first * NEONFltDblTbl[Idx].Cost;
201  }
202 
203  EVT SrcTy = TLI->getValueType(Src);
204  EVT DstTy = TLI->getValueType(Dst);
205 
206  if (!SrcTy.isSimple() || !DstTy.isSimple())
207  return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src);
208 
209  // Some arithmetic, load and store operations have specific instructions
210  // to cast up/down their types automatically at no extra cost.
211  // TODO: Get these tables to know at least what the related operations are.
213  NEONVectorConversionTbl[] = {
220 
221  // The number of vmovl instructions for the extension.
232 
233  // Operations that we legalize using splitting.
236 
237  // Vector float <-> i32 conversions.
240 
261 
268 
269  // Vector double <-> i32 conversions.
272 
279 
286  };
287 
288  if (SrcTy.isVector() && ST->hasNEON()) {
289  int Idx = ConvertCostTableLookup(NEONVectorConversionTbl, ISD,
290  DstTy.getSimpleVT(), SrcTy.getSimpleVT());
291  if (Idx != -1)
292  return NEONVectorConversionTbl[Idx].Cost;
293  }
294 
295  // Scalar float to integer conversions.
297  NEONFloatConversionTbl[] = {
318  };
319  if (SrcTy.isFloatingPoint() && ST->hasNEON()) {
320  int Idx = ConvertCostTableLookup(NEONFloatConversionTbl, ISD,
321  DstTy.getSimpleVT(), SrcTy.getSimpleVT());
322  if (Idx != -1)
323  return NEONFloatConversionTbl[Idx].Cost;
324  }
325 
326  // Scalar integer to float conversions.
328  NEONIntegerConversionTbl[] = {
349  };
350 
351  if (SrcTy.isInteger() && ST->hasNEON()) {
352  int Idx = ConvertCostTableLookup(NEONIntegerConversionTbl, ISD,
353  DstTy.getSimpleVT(), SrcTy.getSimpleVT());
354  if (Idx != -1)
355  return NEONIntegerConversionTbl[Idx].Cost;
356  }
357 
358  // Scalar integer conversion costs.
360  ARMIntegerConversionTbl[] = {
361  // i16 -> i64 requires two dependent operations.
363 
364  // Truncates on i64 are assumed to be free.
367  { ISD::TRUNCATE, MVT::i8, MVT::i64, 0 },
369  };
370 
371  if (SrcTy.isInteger()) {
372  int Idx = ConvertCostTableLookup(ARMIntegerConversionTbl, ISD,
373  DstTy.getSimpleVT(), SrcTy.getSimpleVT());
374  if (Idx != -1)
375  return ARMIntegerConversionTbl[Idx].Cost;
376  }
377 
378  return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src);
379 }
380 
381 unsigned ARMTTI::getVectorInstrCost(unsigned Opcode, Type *ValTy,
382  unsigned Index) const {
383  // Penalize inserting into an D-subregister. We end up with a three times
384  // lower estimated throughput on swift.
385  if (ST->isSwift() &&
386  Opcode == Instruction::InsertElement &&
387  ValTy->isVectorTy() &&
388  ValTy->getScalarSizeInBits() <= 32)
389  return 3;
390 
391  return TargetTransformInfo::getVectorInstrCost(Opcode, ValTy, Index);
392 }
393 
394 unsigned ARMTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
395  Type *CondTy) const {
396 
397  int ISD = TLI->InstructionOpcodeToISD(Opcode);
398  // On NEON a a vector select gets lowered to vbsl.
399  if (ST->hasNEON() && ValTy->isVectorTy() && ISD == ISD::SELECT) {
400  // Lowering of some vector selects is currently far from perfect.
402  NEONVectorSelectTbl[] = {
403  { ISD::SELECT, MVT::v16i1, MVT::v16i16, 2*16 + 1 + 3*1 + 4*1 },
404  { ISD::SELECT, MVT::v8i1, MVT::v8i32, 4*8 + 1*3 + 1*4 + 1*2 },
405  { ISD::SELECT, MVT::v16i1, MVT::v16i32, 4*16 + 1*6 + 1*8 + 1*4 },
406  { ISD::SELECT, MVT::v4i1, MVT::v4i64, 4*4 + 1*2 + 1 },
407  { ISD::SELECT, MVT::v8i1, MVT::v8i64, 50 },
409  };
410 
411  EVT SelCondTy = TLI->getValueType(CondTy);
412  EVT SelValTy = TLI->getValueType(ValTy);
413  if (SelCondTy.isSimple() && SelValTy.isSimple()) {
414  int Idx = ConvertCostTableLookup(NEONVectorSelectTbl, ISD,
415  SelCondTy.getSimpleVT(),
416  SelValTy.getSimpleVT());
417  if (Idx != -1)
418  return NEONVectorSelectTbl[Idx].Cost;
419  }
420 
421  std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
422  return LT.first;
423  }
424 
425  return TargetTransformInfo::getCmpSelInstrCost(Opcode, ValTy, CondTy);
426 }
427 
428 unsigned ARMTTI::getAddressComputationCost(Type *Ty, bool IsComplex) const {
429  // Address computations in vectorized code with non-consecutive addresses will
430  // likely result in more instructions compared to scalar code where the
431  // computation can more often be merged into the index mode. The resulting
432  // extra micro-ops can significantly decrease throughput.
433  unsigned NumVectorInstToHideOverhead = 10;
434 
435  if (Ty->isVectorTy() && IsComplex)
436  return NumVectorInstToHideOverhead;
437 
438  // In many cases the address computation is not merged into the instruction
439  // addressing mode.
440  return 1;
441 }
442 
443 unsigned ARMTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
444  Type *SubTp) const {
445  // We only handle costs of reverse shuffles for now.
446  if (Kind != SK_Reverse)
447  return TargetTransformInfo::getShuffleCost(Kind, Tp, Index, SubTp);
448 
449  static const CostTblEntry<MVT::SimpleValueType> NEONShuffleTbl[] = {
450  // Reverse shuffle cost one instruction if we are shuffling within a double
451  // word (vrev) or two if we shuffle a quad word (vrev, vext).
456 
461  };
462 
463  std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Tp);
464 
465  int Idx = CostTableLookup(NEONShuffleTbl, ISD::VECTOR_SHUFFLE, LT.second);
466  if (Idx == -1)
467  return TargetTransformInfo::getShuffleCost(Kind, Tp, Index, SubTp);
468 
469  return LT.first * NEONShuffleTbl[Idx].Cost;
470 }
471 
472 unsigned ARMTTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty, OperandValueKind Op1Info,
473  OperandValueKind Op2Info) const {
474 
475  int ISDOpcode = TLI->InstructionOpcodeToISD(Opcode);
476  std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
477 
478  const unsigned FunctionCallDivCost = 20;
479  const unsigned ReciprocalDivCost = 10;
480  static const CostTblEntry<MVT::SimpleValueType> CostTbl[] = {
481  // Division.
482  // These costs are somewhat random. Choose a cost of 20 to indicate that
483  // vectorizing devision (added function call) is going to be very expensive.
484  // Double registers types.
485  { ISD::SDIV, MVT::v1i64, 1 * FunctionCallDivCost},
486  { ISD::UDIV, MVT::v1i64, 1 * FunctionCallDivCost},
487  { ISD::SREM, MVT::v1i64, 1 * FunctionCallDivCost},
488  { ISD::UREM, MVT::v1i64, 1 * FunctionCallDivCost},
489  { ISD::SDIV, MVT::v2i32, 2 * FunctionCallDivCost},
490  { ISD::UDIV, MVT::v2i32, 2 * FunctionCallDivCost},
491  { ISD::SREM, MVT::v2i32, 2 * FunctionCallDivCost},
492  { ISD::UREM, MVT::v2i32, 2 * FunctionCallDivCost},
493  { ISD::SDIV, MVT::v4i16, ReciprocalDivCost},
494  { ISD::UDIV, MVT::v4i16, ReciprocalDivCost},
495  { ISD::SREM, MVT::v4i16, 4 * FunctionCallDivCost},
496  { ISD::UREM, MVT::v4i16, 4 * FunctionCallDivCost},
497  { ISD::SDIV, MVT::v8i8, ReciprocalDivCost},
498  { ISD::UDIV, MVT::v8i8, ReciprocalDivCost},
499  { ISD::SREM, MVT::v8i8, 8 * FunctionCallDivCost},
500  { ISD::UREM, MVT::v8i8, 8 * FunctionCallDivCost},
501  // Quad register types.
502  { ISD::SDIV, MVT::v2i64, 2 * FunctionCallDivCost},
503  { ISD::UDIV, MVT::v2i64, 2 * FunctionCallDivCost},
504  { ISD::SREM, MVT::v2i64, 2 * FunctionCallDivCost},
505  { ISD::UREM, MVT::v2i64, 2 * FunctionCallDivCost},
506  { ISD::SDIV, MVT::v4i32, 4 * FunctionCallDivCost},
507  { ISD::UDIV, MVT::v4i32, 4 * FunctionCallDivCost},
508  { ISD::SREM, MVT::v4i32, 4 * FunctionCallDivCost},
509  { ISD::UREM, MVT::v4i32, 4 * FunctionCallDivCost},
510  { ISD::SDIV, MVT::v8i16, 8 * FunctionCallDivCost},
511  { ISD::UDIV, MVT::v8i16, 8 * FunctionCallDivCost},
512  { ISD::SREM, MVT::v8i16, 8 * FunctionCallDivCost},
513  { ISD::UREM, MVT::v8i16, 8 * FunctionCallDivCost},
514  { ISD::SDIV, MVT::v16i8, 16 * FunctionCallDivCost},
515  { ISD::UDIV, MVT::v16i8, 16 * FunctionCallDivCost},
516  { ISD::SREM, MVT::v16i8, 16 * FunctionCallDivCost},
517  { ISD::UREM, MVT::v16i8, 16 * FunctionCallDivCost},
518  // Multiplication.
519  };
520 
521  int Idx = -1;
522 
523  if (ST->hasNEON())
524  Idx = CostTableLookup(CostTbl, ISDOpcode, LT.second);
525 
526  if (Idx != -1)
527  return LT.first * CostTbl[Idx].Cost;
528 
529  unsigned Cost =
530  TargetTransformInfo::getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info);
531 
532  // This is somewhat of a hack. The problem that we are facing is that SROA
533  // creates a sequence of shift, and, or instructions to construct values.
534  // These sequences are recognized by the ISel and have zero-cost. Not so for
535  // the vectorized code. Because we have support for v2i64 but not i64 those
536  // sequences look particularily beneficial to vectorize.
537  // To work around this we increase the cost of v2i64 operations to make them
538  // seem less beneficial.
539  if (LT.second == MVT::v2i64 &&
541  Cost += 4;
542 
543  return Cost;
544 }
545 
546 unsigned ARMTTI::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
547  unsigned AddressSpace) const {
548  std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Src);
549 
550  if (Src->isVectorTy() && Alignment != 16 &&
551  Src->getVectorElementType()->isDoubleTy()) {
552  // Unaligned loads/stores are extremely inefficient.
553  // We need 4 uops for vst.1/vld.1 vs 1uop for vldr/vstr.
554  return LT.first * 4;
555  }
556  return LT.first;
557 }
ImmutablePass * createARMTargetTransformInfoPass(const ARMBaseTargetMachine *TM)
Creates an ARM-specific Target Transformation Info pass.
static PassRegistry * getPassRegistry()
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1306
unsigned getScalarSizeInBits()
Definition: Type.cpp:135
Cost tables and simple lookup functions.
int CostTableLookup(const CostTblEntry< TypeTy > *Tbl, unsigned len, int ISD, CompareTy Ty)
Find in cost table, TypeTy must be comparable to CompareTy by ==.
Definition: CostTable.h:30
bool isDoubleTy() const
isDoubleTy - Return true if this is 'double', a 64-bit IEEE fp type.
Definition: Type.h:149
Type Conversion Cost Table.
Definition: CostTable.h:49
Cost Table Entry.
Definition: CostTable.h:22
bool isVector() const
isVector - Return true if this is a vector value type.
Definition: ValueTypes.h:661
#define llvm_unreachable(msg)
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
Type * getVectorElementType() const
Definition: Type.h:371
bool isInteger() const
isInteger - Return true if this is an integer, or a vector integer type.
Definition: ValueTypes.h:656
static int getT2SOImmVal(unsigned Arg)
virtual unsigned getShuffleCost(ShuffleKind Kind, Type *Tp, int Index=0, Type *SubTp=0) const
static bool isThumbImmShiftedVal(unsigned V)
void initializeARMTTIPass(PassRegistry &)
bool isVectorTy() const
Definition: Type.h:229
virtual unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy=0) const
int64_t getSExtValue() const
Get sign extended value.
Definition: APInt.h:1318
static char ID
Analysis group identification.
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition: ISDOpcodes.h:411
virtual unsigned getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) const
#define INITIALIZE_AG_PASS(passName, agName, arg, name, cfg, analysis, def)
Definition: PassSupport.h:268
AddressSpace
Definition: NVPTXBaseInfo.h:22
Class for arbitrary precision integers.
Definition: APInt.h:75
bool isIntegerTy() const
Definition: Type.h:196
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition: ISDOpcodes.h:357
static int getSOImmVal(unsigned Arg)
virtual void getAnalysisUsage(AnalysisUsage &AU) const
All pass subclasses must call TargetTransformInfo::getAnalysisUsage.
unsigned getPrimitiveSizeInBits() const
Definition: Type.cpp:117
bool isFloatingPoint() const
isFloatingPoint - Return true if this is a FP, or a vector FP type.
Definition: ValueTypes.h:651
bool isSimple() const
Definition: ValueTypes.h:640
int ConvertCostTableLookup(const TypeConversionCostTblEntry< TypeTy > *Tbl, unsigned len, int ISD, CompareTy Dst, CompareTy Src)
Definition: CostTable.h:59
virtual unsigned getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index=-1) const
virtual unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty, OperandValueKind Opd1Info=OK_AnyValue, OperandValueKind Opd2Info=OK_AnyValue) const
TRUNCATE - Completely drop the high bits.
Definition: ISDOpcodes.h:363
MVT getSimpleVT() const
Definition: ValueTypes.h:749