xref: /freebsd/contrib/llvm-project/llvm/lib/Target/Lanai/LanaiTargetTransformInfo.h (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
1 //===-- LanaiTargetTransformInfo.h - Lanai specific TTI ---------*- 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 //===----------------------------------------------------------------------===//
8 //
9 // This file a TargetTransformInfo::Concept conforming object specific to the
10 // Lanai target machine. It uses the target's detailed information to
11 // provide more precise answers to certain TTI queries, while letting the
12 // target independent and default TTI implementations handle the rest.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_LIB_TARGET_LANAI_LANAITARGETTRANSFORMINFO_H
17 #define LLVM_LIB_TARGET_LANAI_LANAITARGETTRANSFORMINFO_H
18 
19 #include "Lanai.h"
20 #include "LanaiSubtarget.h"
21 #include "LanaiTargetMachine.h"
22 #include "llvm/Analysis/TargetTransformInfo.h"
23 #include "llvm/CodeGen/BasicTTIImpl.h"
24 #include "llvm/CodeGen/TargetLowering.h"
25 #include "llvm/Support/MathExtras.h"
26 
27 namespace llvm {
28 class LanaiTTIImpl : public BasicTTIImplBase<LanaiTTIImpl> {
29   typedef BasicTTIImplBase<LanaiTTIImpl> BaseT;
30   typedef TargetTransformInfo TTI;
31   friend BaseT;
32 
33   const LanaiSubtarget *ST;
34   const LanaiTargetLowering *TLI;
35 
36   const LanaiSubtarget *getST() const { return ST; }
37   const LanaiTargetLowering *getTLI() const { return TLI; }
38 
39 public:
40   explicit LanaiTTIImpl(const LanaiTargetMachine *TM, const Function &F)
41       : BaseT(TM, F.getParent()->getDataLayout()), ST(TM->getSubtargetImpl(F)),
42         TLI(ST->getTargetLowering()) {}
43 
44   bool shouldBuildLookupTables() const { return false; }
45 
46   TargetTransformInfo::PopcntSupportKind getPopcntSupport(unsigned TyWidth) {
47     if (TyWidth == 32)
48       return TTI::PSK_FastHardware;
49     return TTI::PSK_Software;
50   }
51 
52   int getIntImmCost(const APInt &Imm, Type *Ty, TTI::TargetCostKind CostKind) {
53     assert(Ty->isIntegerTy());
54     if (Imm == 0)
55       return TTI::TCC_Free;
56     if (isInt<16>(Imm.getSExtValue()))
57       return TTI::TCC_Basic;
58     if (isInt<21>(Imm.getZExtValue()))
59       return TTI::TCC_Basic;
60     if (isInt<32>(Imm.getSExtValue())) {
61       if ((Imm.getSExtValue() & 0xFFFF) == 0)
62         return TTI::TCC_Basic;
63       return 2 * TTI::TCC_Basic;
64     }
65 
66     return 4 * TTI::TCC_Basic;
67   }
68 
69   int getIntImmCostInst(unsigned Opc, unsigned Idx, const APInt &Imm, Type *Ty,
70                         TTI::TargetCostKind CostKind) {
71     return getIntImmCost(Imm, Ty, CostKind);
72   }
73 
74   int getIntImmCostIntrin(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
75                           Type *Ty, TTI::TargetCostKind CostKind) {
76     return getIntImmCost(Imm, Ty, CostKind);
77   }
78 
79   unsigned getArithmeticInstrCost(
80       unsigned Opcode, Type *Ty,
81       TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput,
82       TTI::OperandValueKind Opd1Info = TTI::OK_AnyValue,
83       TTI::OperandValueKind Opd2Info = TTI::OK_AnyValue,
84       TTI::OperandValueProperties Opd1PropInfo = TTI::OP_None,
85       TTI::OperandValueProperties Opd2PropInfo = TTI::OP_None,
86       ArrayRef<const Value *> Args = ArrayRef<const Value *>(),
87       const Instruction *CxtI = nullptr) {
88     int ISD = TLI->InstructionOpcodeToISD(Opcode);
89 
90     switch (ISD) {
91     default:
92       return BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Opd1Info,
93                                            Opd2Info,
94                                            Opd1PropInfo, Opd2PropInfo);
95     case ISD::MUL:
96     case ISD::SDIV:
97     case ISD::UDIV:
98     case ISD::UREM:
99       // This increases the cost associated with multiplication and division
100       // to 64 times what the baseline arithmetic cost is. The arithmetic
101       // instruction cost was arbitrarily chosen to reduce the desirability
102       // of emitting arithmetic instructions that are emulated in software.
103       // TODO: Investigate the performance impact given specialized lowerings.
104       return 64 * BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Opd1Info,
105                                                 Opd2Info,
106                                                 Opd1PropInfo, Opd2PropInfo);
107     }
108   }
109 };
110 
111 } // end namespace llvm
112 
113 #endif // LLVM_LIB_TARGET_LANAI_LANAITARGETTRANSFORMINFO_H
114