xref: /freebsd/contrib/llvm-project/llvm/lib/Target/Hexagon/HexagonTargetTransformInfo.h (revision 525fe93dc7487a1e63a90f6a2b956abc601963c1)
1 //==- HexagonTargetTransformInfo.cpp - Hexagon specific TTI pass -*- C++ -*-==//
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 
15 #ifndef LLVM_LIB_TARGET_HEXAGON_HEXAGONTARGETTRANSFORMINFO_H
16 #define LLVM_LIB_TARGET_HEXAGON_HEXAGONTARGETTRANSFORMINFO_H
17 
18 #include "Hexagon.h"
19 #include "HexagonSubtarget.h"
20 #include "HexagonTargetMachine.h"
21 #include "llvm/ADT/ArrayRef.h"
22 #include "llvm/Analysis/TargetTransformInfo.h"
23 #include "llvm/CodeGen/BasicTTIImpl.h"
24 #include "llvm/IR/Function.h"
25 
26 namespace llvm {
27 
28 class Loop;
29 class ScalarEvolution;
30 class User;
31 class Value;
32 
33 class HexagonTTIImpl : public BasicTTIImplBase<HexagonTTIImpl> {
34   using BaseT = BasicTTIImplBase<HexagonTTIImpl>;
35   using TTI = TargetTransformInfo;
36 
37   friend BaseT;
38 
39   const HexagonSubtarget &ST;
40   const HexagonTargetLowering &TLI;
41 
42   const HexagonSubtarget *getST() const { return &ST; }
43   const HexagonTargetLowering *getTLI() const { return &TLI; }
44 
45   bool useHVX() const;
46   bool isHVXVectorType(Type *Ty) const;
47 
48   // Returns the number of vector elements of Ty, if Ty is a vector type,
49   // or 1 if Ty is a scalar type. It is incorrect to call this function
50   // with any other type.
51   unsigned getTypeNumElements(Type *Ty) const;
52 
53 public:
54   explicit HexagonTTIImpl(const HexagonTargetMachine *TM, const Function &F)
55       : BaseT(TM, F.getParent()->getDataLayout()),
56         ST(*TM->getSubtargetImpl(F)), TLI(*ST.getTargetLowering()) {}
57 
58   /// \name Scalar TTI Implementations
59   /// @{
60 
61   TTI::PopcntSupportKind getPopcntSupport(unsigned IntTyWidthInBit) const;
62 
63   // The Hexagon target can unroll loops with run-time trip counts.
64   void getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
65                                TTI::UnrollingPreferences &UP,
66                                OptimizationRemarkEmitter *ORE);
67 
68   void getPeelingPreferences(Loop *L, ScalarEvolution &SE,
69                              TTI::PeelingPreferences &PP);
70 
71   /// Bias LSR towards creating post-increment opportunities.
72   TTI::AddressingModeKind
73     getPreferredAddressingMode(const Loop *L, ScalarEvolution *SE) const;
74 
75   // L1 cache prefetch.
76   unsigned getPrefetchDistance() const override;
77   unsigned getCacheLineSize() const override;
78 
79   /// @}
80 
81   /// \name Vector TTI Implementations
82   /// @{
83 
84   unsigned getNumberOfRegisters(bool vector) const;
85   unsigned getMaxInterleaveFactor(unsigned VF);
86   TypeSize getRegisterBitWidth(TargetTransformInfo::RegisterKind K) const;
87   unsigned getMinVectorRegisterBitWidth() const;
88   ElementCount getMinimumVF(unsigned ElemWidth, bool IsScalable) const;
89 
90   bool
91   shouldMaximizeVectorBandwidth(TargetTransformInfo::RegisterKind K) const {
92     return true;
93   }
94   bool supportsEfficientVectorElementLoadStore() { return false; }
95   bool hasBranchDivergence() {
96     return false;
97   }
98   bool enableAggressiveInterleaving(bool LoopHasReductions) {
99     return false;
100   }
101   bool prefersVectorizedAddressing() {
102     return false;
103   }
104   bool enableInterleavedAccessVectorization() {
105     return true;
106   }
107 
108   InstructionCost getScalarizationOverhead(VectorType *Ty,
109                                            const APInt &DemandedElts,
110                                            bool Insert, bool Extract,
111                                            TTI::TargetCostKind CostKind);
112   InstructionCost
113   getOperandsScalarizationOverhead(ArrayRef<const Value *> Args,
114                                    ArrayRef<Type *> Tys,
115                                    TTI::TargetCostKind CostKind);
116   InstructionCost getCallInstrCost(Function *F, Type *RetTy,
117                                    ArrayRef<Type *> Tys,
118                                    TTI::TargetCostKind CostKind);
119   InstructionCost getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
120                                         TTI::TargetCostKind CostKind);
121   InstructionCost getAddressComputationCost(Type *Tp, ScalarEvolution *SE,
122                                             const SCEV *S);
123   InstructionCost
124   getMemoryOpCost(unsigned Opcode, Type *Src, MaybeAlign Alignment,
125                   unsigned AddressSpace, TTI::TargetCostKind CostKind,
126                   TTI::OperandValueInfo OpInfo = {TTI::OK_AnyValue, TTI::OP_None},
127                   const Instruction *I = nullptr);
128   InstructionCost getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
129                                         Align Alignment, unsigned AddressSpace,
130                                         TTI::TargetCostKind CostKind);
131   InstructionCost getShuffleCost(TTI::ShuffleKind Kind, Type *Tp,
132                                  ArrayRef<int> Mask,
133                                  TTI::TargetCostKind CostKind, int Index,
134                                  Type *SubTp,
135                                  ArrayRef<const Value *> Args = std::nullopt);
136   InstructionCost getGatherScatterOpCost(unsigned Opcode, Type *DataTy,
137                                          const Value *Ptr, bool VariableMask,
138                                          Align Alignment,
139                                          TTI::TargetCostKind CostKind,
140                                          const Instruction *I);
141   InstructionCost getInterleavedMemoryOpCost(
142       unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
143       Align Alignment, unsigned AddressSpace, TTI::TargetCostKind CostKind,
144       bool UseMaskForCond = false, bool UseMaskForGaps = false);
145   InstructionCost getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy,
146                                      CmpInst::Predicate VecPred,
147                                      TTI::TargetCostKind CostKind,
148                                      const Instruction *I = nullptr);
149   InstructionCost getArithmeticInstrCost(
150       unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind,
151       TTI::OperandValueInfo Op1Info = {TTI::OK_AnyValue, TTI::OP_None},
152       TTI::OperandValueInfo Op2Info = {TTI::OK_AnyValue, TTI::OP_None},
153       ArrayRef<const Value *> Args = ArrayRef<const Value *>(),
154       const Instruction *CxtI = nullptr);
155   InstructionCost getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src,
156                                    TTI::CastContextHint CCH,
157                                    TTI::TargetCostKind CostKind,
158                                    const Instruction *I = nullptr);
159   using BaseT::getVectorInstrCost;
160   InstructionCost getVectorInstrCost(unsigned Opcode, Type *Val,
161                                      TTI::TargetCostKind CostKind,
162                                      unsigned Index, Value *Op0, Value *Op1);
163 
164   InstructionCost getCFInstrCost(unsigned Opcode, TTI::TargetCostKind CostKind,
165                                  const Instruction *I = nullptr) {
166     return 1;
167   }
168 
169   bool isLegalMaskedStore(Type *DataType, Align Alignment);
170   bool isLegalMaskedLoad(Type *DataType, Align Alignment);
171 
172   /// @}
173 
174   InstructionCost getInstructionCost(const User *U,
175                                      ArrayRef<const Value *> Operands,
176                                      TTI::TargetCostKind CostKind);
177 
178   // Hexagon specific decision to generate a lookup table.
179   bool shouldBuildLookupTables() const;
180 };
181 
182 } // end namespace llvm
183 #endif // LLVM_LIB_TARGET_HEXAGON_HEXAGONTARGETTRANSFORMINFO_H
184