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 TargetTransformInfoImplBase 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
27namespace llvm {
28class LanaiTTIImpl final : 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
39public:
40 explicit LanaiTTIImpl(const LanaiTargetMachine *TM, const Function &F)
41 : BaseT(TM, F.getDataLayout()), ST(TM->getSubtargetImpl(F)),
42 TLI(ST->getTargetLowering()) {}
43
44 bool shouldBuildLookupTables() const override { return false; }
45
46 TargetTransformInfo::PopcntSupportKind
47 getPopcntSupport(unsigned TyWidth) const override {
48 if (TyWidth == 32)
49 return TTI::PSK_FastHardware;
50 return TTI::PSK_Software;
51 }
52
53 InstructionCost getIntImmCost(const APInt &Imm, Type *Ty,
54 TTI::TargetCostKind CostKind) const override {
55 assert(Ty->isIntegerTy());
56 unsigned BitSize = Ty->getPrimitiveSizeInBits();
57 // There is no cost model for constants with a bit size of 0. Return
58 // TCC_Free here, so that constant hoisting will ignore this constant.
59 if (BitSize == 0)
60 return TTI::TCC_Free;
61 // No cost model for operations on integers larger than 64 bit implemented
62 // yet.
63 if (BitSize > 64)
64 return TTI::TCC_Free;
65
66 if (Imm == 0)
67 return TTI::TCC_Free;
68 if (isInt<16>(x: Imm.getSExtValue()))
69 return TTI::TCC_Basic;
70 if (isInt<21>(x: Imm.getZExtValue()))
71 return TTI::TCC_Basic;
72 if (isInt<32>(x: Imm.getSExtValue())) {
73 if ((Imm.getSExtValue() & 0xFFFF) == 0)
74 return TTI::TCC_Basic;
75 return 2 * TTI::TCC_Basic;
76 }
77
78 return 4 * TTI::TCC_Basic;
79 }
80
81 InstructionCost
82 getIntImmCostInst(unsigned Opc, unsigned Idx, const APInt &Imm, Type *Ty,
83 TTI::TargetCostKind CostKind,
84 Instruction *Inst = nullptr) const override {
85 return getIntImmCost(Imm, Ty, CostKind);
86 }
87
88 InstructionCost
89 getIntImmCostIntrin(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
90 Type *Ty, TTI::TargetCostKind CostKind) const override {
91 return getIntImmCost(Imm, Ty, CostKind);
92 }
93
94 InstructionCost getArithmeticInstrCost(
95 unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind,
96 TTI::OperandValueInfo Op1Info = {.Kind: TTI::OK_AnyValue, .Properties: TTI::OP_None},
97 TTI::OperandValueInfo Op2Info = {.Kind: TTI::OK_AnyValue, .Properties: TTI::OP_None},
98 ArrayRef<const Value *> Args = {},
99 const Instruction *CxtI = nullptr) const override {
100 int ISD = TLI->InstructionOpcodeToISD(Opcode);
101
102 switch (ISD) {
103 default:
104 return BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Opd1Info: Op1Info,
105 Opd2Info: Op2Info);
106 case ISD::MUL:
107 case ISD::SDIV:
108 case ISD::UDIV:
109 case ISD::UREM:
110 // This increases the cost associated with multiplication and division
111 // to 64 times what the baseline arithmetic cost is. The arithmetic
112 // instruction cost was arbitrarily chosen to reduce the desirability
113 // of emitting arithmetic instructions that are emulated in software.
114 // TODO: Investigate the performance impact given specialized lowerings.
115 return 64 * BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Opd1Info: Op1Info,
116 Opd2Info: Op2Info);
117 }
118 }
119};
120
121} // end namespace llvm
122
123#endif // LLVM_LIB_TARGET_LANAI_LANAITARGETTRANSFORMINFO_H
124