LLVM  10.0.0
HexagonTargetTransformInfo.cpp
Go to the documentation of this file.
1 //===- HexagonTargetTransformInfo.cpp - Hexagon specific TTI pass ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 /// \file
8 /// This file implements a TargetTransformInfo analysis pass specific to the
9 /// Hexagon target machine. It uses the target's detailed information to provide
10 /// more precise answers to certain TTI queries, while letting the target
11 /// independent and default TTI implementations handle the rest.
12 ///
13 //===----------------------------------------------------------------------===//
14 
16 #include "HexagonSubtarget.h"
19 #include "llvm/IR/InstrTypes.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/IR/User.h"
22 #include "llvm/Support/Casting.h"
25 
26 using namespace llvm;
27 
28 #define DEBUG_TYPE "hexagontti"
29 
30 static cl::opt<bool> HexagonAutoHVX("hexagon-autohvx", cl::init(false),
31  cl::Hidden, cl::desc("Enable loop vectorizer for HVX"));
32 
33 static cl::opt<bool> EmitLookupTables("hexagon-emit-lookup-tables",
34  cl::init(true), cl::Hidden,
35  cl::desc("Control lookup table emission on Hexagon target"));
36 
37 // Constant "cost factor" to make floating point operations more expensive
38 // in terms of vectorization cost. This isn't the best way, but it should
39 // do. Ultimately, the cost should use cycles.
40 static const unsigned FloatFactor = 4;
41 
42 bool HexagonTTIImpl::useHVX() const {
43  return ST.useHVXOps() && HexagonAutoHVX;
44 }
45 
46 bool HexagonTTIImpl::isTypeForHVX(Type *VecTy) const {
47  assert(VecTy->isVectorTy());
48  if (cast<VectorType>(VecTy)->isScalable())
49  return false;
50  // Avoid types like <2 x i32*>.
51  if (!cast<VectorType>(VecTy)->getElementType()->isIntegerTy())
52  return false;
53  EVT VecVT = EVT::getEVT(VecTy);
54  if (!VecVT.isSimple() || VecVT.getSizeInBits() <= 64)
55  return false;
56  if (ST.isHVXVectorType(VecVT.getSimpleVT()))
57  return true;
58  auto Action = TLI.getPreferredVectorAction(VecVT.getSimpleVT());
59  return Action == TargetLoweringBase::TypeWidenVector;
60 }
61 
62 unsigned HexagonTTIImpl::getTypeNumElements(Type *Ty) const {
63  if (Ty->isVectorTy())
64  return Ty->getVectorNumElements();
65  assert((Ty->isIntegerTy() || Ty->isFloatingPointTy()) &&
66  "Expecting scalar type");
67  return 1;
68 }
69 
71 HexagonTTIImpl::getPopcntSupport(unsigned IntTyWidthInBit) const {
72  // Return fast hardware support as every input < 64 bits will be promoted
73  // to 64 bits.
75 }
76 
77 // The Hexagon target can unroll loops with run-time trip counts.
80  UP.Runtime = UP.Partial = true;
81  // Only try to peel innermost loops with small runtime trip counts.
82  if (L && L->empty() && canPeel(L) &&
83  SE.getSmallConstantTripCount(L) == 0 &&
84  SE.getSmallConstantMaxTripCount(L) > 0 &&
85  SE.getSmallConstantMaxTripCount(L) <= 5) {
86  UP.PeelCount = 2;
87  }
88 }
89 
91  return true;
92 }
93 
94 /// --- Vector TTI begin ---
95 
97  if (Vector)
98  return useHVX() ? 32 : 0;
99  return 32;
100 }
101 
103  return useHVX() ? 2 : 0;
104 }
105 
107  return Vector ? getMinVectorRegisterBitWidth() : 32;
108 }
109 
111  return useHVX() ? ST.getVectorLength()*8 : 0;
112 }
113 
114 unsigned HexagonTTIImpl::getMinimumVF(unsigned ElemWidth) const {
115  return (8 * ST.getVectorLength()) / ElemWidth;
116 }
117 
119  bool Extract) {
120  return BaseT::getScalarizationOverhead(Ty, Insert, Extract);
121 }
122 
124  ArrayRef<const Value*> Args, unsigned VF) {
126 }
127 
129  ArrayRef<Type*> Tys) {
130  return BaseT::getCallInstrCost(F, RetTy, Tys);
131 }
132 
134  ArrayRef<Value*> Args, FastMathFlags FMF, unsigned VF) {
135  return BaseT::getIntrinsicInstrCost(ID, RetTy, Args, FMF, VF);
136 }
137 
140  unsigned ScalarizationCostPassed) {
141  if (ID == Intrinsic::bswap) {
142  std::pair<int, MVT> LT = TLI.getTypeLegalizationCost(DL, RetTy);
143  return LT.first + 2;
144  }
145  return BaseT::getIntrinsicInstrCost(ID, RetTy, Tys, FMF,
146  ScalarizationCostPassed);
147 }
148 
150  ScalarEvolution *SE, const SCEV *S) {
151  return 0;
152 }
153 
154 unsigned HexagonTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
155  MaybeAlign Alignment,
156  unsigned AddressSpace,
157  const Instruction *I) {
158  assert(Opcode == Instruction::Load || Opcode == Instruction::Store);
159  if (Opcode == Instruction::Store)
160  return BaseT::getMemoryOpCost(Opcode, Src, Alignment, AddressSpace, I);
161 
162  if (Src->isVectorTy()) {
163  VectorType *VecTy = cast<VectorType>(Src);
164  unsigned VecWidth = VecTy->getBitWidth();
165  if (useHVX() && isTypeForHVX(VecTy)) {
166  unsigned RegWidth = getRegisterBitWidth(true);
167  assert(RegWidth && "Non-zero vector register width expected");
168  // Cost of HVX loads.
169  if (VecWidth % RegWidth == 0)
170  return VecWidth / RegWidth;
171  // Cost of constructing HVX vector from scalar loads
172  const Align RegAlign(RegWidth / 8);
173  if (!Alignment || *Alignment > RegAlign)
174  Alignment = RegAlign;
175  assert(Alignment);
176  unsigned AlignWidth = 8 * Alignment->value();
177  unsigned NumLoads = alignTo(VecWidth, AlignWidth) / AlignWidth;
178  return 3 * NumLoads;
179  }
180 
181  // Non-HVX vectors.
182  // Add extra cost for floating point types.
183  unsigned Cost =
184  VecTy->getElementType()->isFloatingPointTy() ? FloatFactor : 1;
185 
186  // At this point unspecified alignment is considered as Align::None().
187  const Align BoundAlignment = std::min(Alignment.valueOrOne(), Align(8));
188  unsigned AlignWidth = 8 * BoundAlignment.value();
189  unsigned NumLoads = alignTo(VecWidth, AlignWidth) / AlignWidth;
190  if (Alignment == Align(4) || Alignment == Align(8))
191  return Cost * NumLoads;
192  // Loads of less than 32 bits will need extra inserts to compose a vector.
193  assert(BoundAlignment <= Align(8));
194  unsigned LogA = Log2(BoundAlignment);
195  return (3 - LogA) * Cost * NumLoads;
196  }
197 
198  return BaseT::getMemoryOpCost(Opcode, Src, Alignment, AddressSpace, I);
199 }
200 
201 unsigned HexagonTTIImpl::getMaskedMemoryOpCost(unsigned Opcode,
202  Type *Src, unsigned Alignment, unsigned AddressSpace) {
203  return BaseT::getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
204 }
205 
207  int Index, Type *SubTp) {
208  return 1;
209 }
210 
211 unsigned HexagonTTIImpl::getGatherScatterOpCost(unsigned Opcode, Type *DataTy,
212  Value *Ptr, bool VariableMask, unsigned Alignment) {
213  return BaseT::getGatherScatterOpCost(Opcode, DataTy, Ptr, VariableMask,
214  Alignment);
215 }
216 
218  Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
219  unsigned Alignment, unsigned AddressSpace, bool UseMaskForCond,
220  bool UseMaskForGaps) {
221  if (Indices.size() != Factor || UseMaskForCond || UseMaskForGaps)
222  return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
223  Alignment, AddressSpace,
224  UseMaskForCond, UseMaskForGaps);
225  return getMemoryOpCost(Opcode, VecTy, MaybeAlign(Alignment), AddressSpace,
226  nullptr);
227 }
228 
229 unsigned HexagonTTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
230  Type *CondTy, const Instruction *I) {
231  if (ValTy->isVectorTy()) {
232  std::pair<int, MVT> LT = TLI.getTypeLegalizationCost(DL, ValTy);
233  if (Opcode == Instruction::FCmp)
234  return LT.first + FloatFactor * getTypeNumElements(ValTy);
235  }
236  return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, I);
237 }
238 
240  unsigned Opcode, Type *Ty, TTI::OperandValueKind Opd1Info,
241  TTI::OperandValueKind Opd2Info, TTI::OperandValueProperties Opd1PropInfo,
243  const Instruction *CxtI) {
244  if (Ty->isVectorTy()) {
245  std::pair<int, MVT> LT = TLI.getTypeLegalizationCost(DL, Ty);
246  if (LT.second.isFloatingPoint())
247  return LT.first + FloatFactor * getTypeNumElements(Ty);
248  }
249  return BaseT::getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
250  Opd1PropInfo, Opd2PropInfo, Args, CxtI);
251 }
252 
253 unsigned HexagonTTIImpl::getCastInstrCost(unsigned Opcode, Type *DstTy,
254  Type *SrcTy, const Instruction *I) {
255  if (SrcTy->isFPOrFPVectorTy() || DstTy->isFPOrFPVectorTy()) {
256  unsigned SrcN = SrcTy->isFPOrFPVectorTy() ? getTypeNumElements(SrcTy) : 0;
257  unsigned DstN = DstTy->isFPOrFPVectorTy() ? getTypeNumElements(DstTy) : 0;
258 
259  std::pair<int, MVT> SrcLT = TLI.getTypeLegalizationCost(DL, SrcTy);
260  std::pair<int, MVT> DstLT = TLI.getTypeLegalizationCost(DL, DstTy);
261  return std::max(SrcLT.first, DstLT.first) + FloatFactor * (SrcN + DstN);
262  }
263  return 1;
264 }
265 
266 unsigned HexagonTTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val,
267  unsigned Index) {
268  Type *ElemTy = Val->isVectorTy() ? cast<VectorType>(Val)->getElementType()
269  : Val;
270  if (Opcode == Instruction::InsertElement) {
271  // Need two rotations for non-zero index.
272  unsigned Cost = (Index != 0) ? 2 : 0;
273  if (ElemTy->isIntegerTy(32))
274  return Cost;
275  // If it's not a 32-bit value, there will need to be an extract.
276  return Cost + getVectorInstrCost(Instruction::ExtractElement, Val, Index);
277  }
278 
279  if (Opcode == Instruction::ExtractElement)
280  return 2;
281 
282  return 1;
283 }
284 
285 /// --- Vector TTI end ---
286 
288  return ST.getL1PrefetchDistance();
289 }
290 
292  return ST.getL1CacheLineSize();
293 }
294 
297  auto isCastFoldedIntoLoad = [this](const CastInst *CI) -> bool {
298  if (!CI->isIntegerCast())
299  return false;
300  // Only extensions from an integer type shorter than 32-bit to i32
301  // can be folded into the load.
302  const DataLayout &DL = getDataLayout();
303  unsigned SBW = DL.getTypeSizeInBits(CI->getSrcTy());
304  unsigned DBW = DL.getTypeSizeInBits(CI->getDestTy());
305  if (DBW != 32 || SBW >= DBW)
306  return false;
307 
308  const LoadInst *LI = dyn_cast<const LoadInst>(CI->getOperand(0));
309  // Technically, this code could allow multiple uses of the load, and
310  // check if all the uses are the same extension operation, but this
311  // should be sufficient for most cases.
312  return LI && LI->hasOneUse();
313  };
314 
315  if (const CastInst *CI = dyn_cast<const CastInst>(U))
316  if (isCastFoldedIntoLoad(CI))
318  return BaseT::getUserCost(U, Operands);
319 }
320 
322  return EmitLookupTables;
323 }
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition: Alignment.h:204
unsigned getOperandsScalarizationOverhead(ArrayRef< const Value *> Args, unsigned VF)
unsigned getSmallConstantTripCount(const Loop *L)
Returns the maximum trip count of the loop if it is a single-exit loop and we can compute a small max...
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
bool Partial
Allow partial unrolling (unrolling of loops to expand the size of the loop body, not only to eliminat...
unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract)
int getUserCost(const User *U, ArrayRef< const Value *> Operands)
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
Definition: Alignment.h:142
unsigned getMinimumVF(unsigned ElemWidth) const
unsigned getGatherScatterOpCost(unsigned Opcode, Type *DataTy, Value *Ptr, bool VariableMask, unsigned Alignment)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract)
Estimate the overhead of scalarizing an instruction.
Definition: BasicTTIImpl.h:569
unsigned getMemoryOpCost(unsigned Opcode, Type *Src, MaybeAlign Alignment, unsigned AddressSpace, const Instruction *I=nullptr)
The main scalar evolution driver.
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition: ValueTypes.h:255
unsigned getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef< unsigned > Indices, unsigned Alignment, unsigned AddressSpace, bool UseMaskForCond=false, bool UseMaskForGaps=false)
F(f)
An instruction for reading from memory.
Definition: Instructions.h:169
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Definition: DataLayout.h:624
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:230
unsigned getL1CacheLineSize() const
unsigned getSmallConstantMaxTripCount(const Loop *L)
Returns the upper bound of the loop trip count as a normal unsigned value.
unsigned getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy, ArrayRef< Value * > Args, FastMathFlags FMF, unsigned VF=1)
Get intrinsic cost based on arguments.
unsigned getBitWidth() const
Return the minimum number of bits in the Vector type.
Definition: DerivedTypes.h:556
unsigned getOperandsScalarizationOverhead(ArrayRef< const Value * > Args, unsigned VF)
Estimate the overhead of scalarizing an instructions unique non-constant operands.
Definition: BasicTTIImpl.h:588
This is the base class for all instructions that perform data casts.
Definition: InstrTypes.h:439
bool isFloatingPointTy() const
Return true if this is one of the six floating-point types.
Definition: Type.h:162
unsigned getGatherScatterOpCost(unsigned Opcode, Type *DataTy, Value *Ptr, bool VariableMask, unsigned Alignment)
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:197
mir Rename Register Operands
unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty, TTI::OperandValueKind Opd1Info=TTI::OK_AnyValue, TTI::OperandValueKind Opd2Info=TTI::OK_AnyValue, TTI::OperandValueProperties Opd1PropInfo=TTI::OP_None, TTI::OperandValueProperties Opd2PropInfo=TTI::OP_None, ArrayRef< const Value *> Args=ArrayRef< const Value *>(), const Instruction *CxtI=nullptr)
unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy, const Instruction *I)
Definition: BasicTTIImpl.h:836
unsigned getL1PrefetchDistance() const
PopcntSupportKind
Flags indicating the kind of support for population count.
unsigned getMinVectorRegisterBitWidth() const
TargetLoweringBase::LegalizeTypeAction getPreferredVectorAction(MVT VT) const override
Return the preferred vector type legalization action.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
unsigned getAddressComputationCost(Type *Tp, ScalarEvolution *SE, const SCEV *S)
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition: Alignment.h:86
This file implements a TargetTransformInfo analysis pass specific to the Hexagon target machine...
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:435
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
unsigned getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef< unsigned > Indices, unsigned Alignment, unsigned AddressSpace, bool UseMaskForCond=false, bool UseMaskForGaps=false)
Definition: BasicTTIImpl.h:915
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:158
Expected to fold away in lowering.
unsigned getUserCost(const User *U, ArrayRef< const Value * > Operands)
unsigned getRegisterBitWidth(bool Vector) const
unsigned getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index, Type *SubTp)
unsigned getCallInstrCost(Function *F, Type *RetTy, ArrayRef< Type *> Tys)
Extended Value Type.
Definition: ValueTypes.h:34
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition: ValueTypes.h:298
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:40
OperandValueProperties
Additional properties of an operand&#39;s values.
unsigned getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index)
bool shouldFavorPostInc() const
Bias LSR towards creating post-increment opportunities.
bool isHVXVectorType(MVT VecTy, bool IncludeBool=false) const
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:390
This struct is a compact representation of a valid (power of two) or undefined (0) alignment...
Definition: Alignment.h:117
AddressSpace
Definition: NVPTXBaseInfo.h:21
static cl::opt< bool > HexagonAutoHVX("hexagon-autohvx", cl::init(false), cl::Hidden, cl::desc("Enable loop vectorizer for HVX"))
unsigned getMemoryOpCost(unsigned Opcode, Type *Src, MaybeAlign Alignment, unsigned AddressSpace, const Instruction *I=nullptr)
Definition: BasicTTIImpl.h:883
unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty, TTI::OperandValueKind Opd1Info=TTI::OK_AnyValue, TTI::OperandValueKind Opd2Info=TTI::OK_AnyValue, TTI::OperandValueProperties Opd1PropInfo=TTI::OP_None, TTI::OperandValueProperties Opd2PropInfo=TTI::OP_None, ArrayRef< const Value * > Args=ArrayRef< const Value * >(), const Instruction *CxtI=nullptr)
Definition: BasicTTIImpl.h:630
bool canPeel(Loop *L)
bool Runtime
Allow runtime unrolling (unrolling of loops to expand the size of the loop body even when the number ...
unsigned getVectorNumElements() const
Definition: DerivedTypes.h:566
Class to represent vector types.
Definition: DerivedTypes.h:432
unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy, const Instruction *I)
unsigned getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy, ArrayRef< Value *> Args, FastMathFlags FMF, unsigned VF)
unsigned getMaskedMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment, unsigned AddressSpace)
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:163
unsigned getVectorLength() const
unsigned PeelCount
A forced peeling factor (the number of bodied of the original loop that should be peeled off before t...
This class represents an analyzed expression in the program.
unsigned getNumberOfRegisters(bool vector) const
— Vector TTI begin —
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:509
Parameters that control the generic loop unrolling transformation.
static cl::opt< bool > EmitLookupTables("hexagon-emit-lookup-tables", cl::init(true), cl::Hidden, cl::desc("Control lookup table emission on Hexagon target"))
#define I(x, y, z)
Definition: MD5.cpp:58
unsigned getMaxInterleaveFactor(unsigned VF)
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
unsigned getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src, const Instruction *I=nullptr)
static EVT getEVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
Definition: ValueTypes.cpp:353
unsigned getMaskedMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment, unsigned AddressSpace)
bool empty() const
Definition: LoopInfo.h:151
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition: Type.h:185
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:74
void getUnrollingPreferences(Loop *L, ScalarEvolution &SE, TTI::UnrollingPreferences &UP)
unsigned getCacheLineSize() const override
Type * getElementType() const
Definition: DerivedTypes.h:399
const DataLayout & getDataLayout() const
bool hasOneUse() const
Return true if there is exactly one user of this value.
Definition: Value.h:433
Convenience struct for specifying and reasoning about fast-math flags.
Definition: Operator.h:160
unsigned getCallInstrCost(Function *F, Type *RetTy, ArrayRef< Type * > Tys)
Compute a cost of the given call instruction.
OperandValueKind
Additional information about an operand&#39;s possible values.
TTI::PopcntSupportKind getPopcntSupport(unsigned IntTyWidthInBit) const
This pass exposes codegen information to IR-level passes.
static const unsigned FloatFactor
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition: ValueTypes.h:126
unsigned getPrefetchDistance() const override
— Vector TTI end —
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
std::pair< int, MVT > getTypeLegalizationCost(const DataLayout &DL, Type *Ty) const
Estimate the cost of type-legalization and the legalized type.
ShuffleKind
The various kinds of shuffle patterns for vector queries.