11a82d4c0SDimitry Andric //===-- WebAssemblyTargetTransformInfo.cpp - WebAssembly-specific TTI -----===//
21a82d4c0SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
61a82d4c0SDimitry Andric //
71a82d4c0SDimitry Andric //===----------------------------------------------------------------------===//
81a82d4c0SDimitry Andric ///
91a82d4c0SDimitry Andric /// \file
10eb11fae6SDimitry Andric /// This file defines the WebAssembly-specific TargetTransformInfo
111a82d4c0SDimitry Andric /// implementation.
121a82d4c0SDimitry Andric ///
131a82d4c0SDimitry Andric //===----------------------------------------------------------------------===//
141a82d4c0SDimitry Andric
151a82d4c0SDimitry Andric #include "WebAssemblyTargetTransformInfo.h"
16044eb2f6SDimitry Andric #include "llvm/CodeGen/CostTable.h"
171a82d4c0SDimitry Andric #include "llvm/Support/Debug.h"
181a82d4c0SDimitry Andric using namespace llvm;
191a82d4c0SDimitry Andric
201a82d4c0SDimitry Andric #define DEBUG_TYPE "wasmtti"
211a82d4c0SDimitry Andric
221a82d4c0SDimitry Andric TargetTransformInfo::PopcntSupportKind
getPopcntSupport(unsigned TyWidth) const23dd58ef01SDimitry Andric WebAssemblyTTIImpl::getPopcntSupport(unsigned TyWidth) const {
241a82d4c0SDimitry Andric assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
25dd58ef01SDimitry Andric return TargetTransformInfo::PSK_FastHardware;
261a82d4c0SDimitry Andric }
2701095a5dSDimitry Andric
getNumberOfRegisters(unsigned ClassID) const281d5ae102SDimitry Andric unsigned WebAssemblyTTIImpl::getNumberOfRegisters(unsigned ClassID) const {
291d5ae102SDimitry Andric unsigned Result = BaseT::getNumberOfRegisters(ClassID);
3001095a5dSDimitry Andric
3101095a5dSDimitry Andric // For SIMD, use at least 16 registers, as a rough guess.
321d5ae102SDimitry Andric bool Vector = (ClassID == 1);
3301095a5dSDimitry Andric if (Vector)
3401095a5dSDimitry Andric Result = std::max(Result, 16u);
3501095a5dSDimitry Andric
3601095a5dSDimitry Andric return Result;
3701095a5dSDimitry Andric }
3801095a5dSDimitry Andric
getRegisterBitWidth(TargetTransformInfo::RegisterKind K) const39344a3780SDimitry Andric TypeSize WebAssemblyTTIImpl::getRegisterBitWidth(
40344a3780SDimitry Andric TargetTransformInfo::RegisterKind K) const {
41344a3780SDimitry Andric switch (K) {
42344a3780SDimitry Andric case TargetTransformInfo::RGK_Scalar:
43344a3780SDimitry Andric return TypeSize::getFixed(64);
44344a3780SDimitry Andric case TargetTransformInfo::RGK_FixedWidthVector:
45344a3780SDimitry Andric return TypeSize::getFixed(getST()->hasSIMD128() ? 128 : 64);
46344a3780SDimitry Andric case TargetTransformInfo::RGK_ScalableVector:
47344a3780SDimitry Andric return TypeSize::getScalable(0);
4801095a5dSDimitry Andric }
4901095a5dSDimitry Andric
50344a3780SDimitry Andric llvm_unreachable("Unsupported register kind");
51344a3780SDimitry Andric }
52344a3780SDimitry Andric
getArithmeticInstrCost(unsigned Opcode,Type * Ty,TTI::TargetCostKind CostKind,TTI::OperandValueInfo Op1Info,TTI::OperandValueInfo Op2Info,ArrayRef<const Value * > Args,const Instruction * CxtI)53344a3780SDimitry Andric InstructionCost WebAssemblyTTIImpl::getArithmeticInstrCost(
54cfca06d7SDimitry Andric unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind,
55e3b55780SDimitry Andric TTI::OperandValueInfo Op1Info, TTI::OperandValueInfo Op2Info,
56e3b55780SDimitry Andric ArrayRef<const Value *> Args,
57706b4fc4SDimitry Andric const Instruction *CxtI) {
5801095a5dSDimitry Andric
59344a3780SDimitry Andric InstructionCost Cost =
60344a3780SDimitry Andric BasicTTIImplBase<WebAssemblyTTIImpl>::getArithmeticInstrCost(
61e3b55780SDimitry Andric Opcode, Ty, CostKind, Op1Info, Op2Info);
6201095a5dSDimitry Andric
63e6d15924SDimitry Andric if (auto *VTy = dyn_cast<VectorType>(Ty)) {
6401095a5dSDimitry Andric switch (Opcode) {
6501095a5dSDimitry Andric case Instruction::LShr:
6601095a5dSDimitry Andric case Instruction::AShr:
6701095a5dSDimitry Andric case Instruction::Shl:
6801095a5dSDimitry Andric // SIMD128's shifts currently only accept a scalar shift count. For each
6901095a5dSDimitry Andric // element, we'll need to extract, op, insert. The following is a rough
70e3b55780SDimitry Andric // approximation.
71e3b55780SDimitry Andric if (!Op2Info.isUniform())
72cfca06d7SDimitry Andric Cost =
73cfca06d7SDimitry Andric cast<FixedVectorType>(VTy)->getNumElements() *
7401095a5dSDimitry Andric (TargetTransformInfo::TCC_Basic +
75cfca06d7SDimitry Andric getArithmeticInstrCost(Opcode, VTy->getElementType(), CostKind) +
7601095a5dSDimitry Andric TargetTransformInfo::TCC_Basic);
7701095a5dSDimitry Andric break;
7801095a5dSDimitry Andric }
7901095a5dSDimitry Andric }
8001095a5dSDimitry Andric return Cost;
8101095a5dSDimitry Andric }
8201095a5dSDimitry Andric
83e3b55780SDimitry Andric InstructionCost
getVectorInstrCost(unsigned Opcode,Type * Val,TTI::TargetCostKind CostKind,unsigned Index,Value * Op0,Value * Op1)84e3b55780SDimitry Andric WebAssemblyTTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val,
85e3b55780SDimitry Andric TTI::TargetCostKind CostKind,
86e3b55780SDimitry Andric unsigned Index, Value *Op0, Value *Op1) {
87e3b55780SDimitry Andric InstructionCost Cost = BasicTTIImplBase::getVectorInstrCost(
88e3b55780SDimitry Andric Opcode, Val, CostKind, Index, Op0, Op1);
8901095a5dSDimitry Andric
9001095a5dSDimitry Andric // SIMD128's insert/extract currently only take constant indices.
9101095a5dSDimitry Andric if (Index == -1u)
9201095a5dSDimitry Andric return Cost + 25 * TargetTransformInfo::TCC_Expensive;
9301095a5dSDimitry Andric
9401095a5dSDimitry Andric return Cost;
9501095a5dSDimitry Andric }
96b60736ecSDimitry Andric
getPreferredExpandedReductionShuffle(const IntrinsicInst * II) const97ac9a064cSDimitry Andric TTI::ReductionShuffle WebAssemblyTTIImpl::getPreferredExpandedReductionShuffle(
98ac9a064cSDimitry Andric const IntrinsicInst *II) const {
99ac9a064cSDimitry Andric
100ac9a064cSDimitry Andric switch (II->getIntrinsicID()) {
101ac9a064cSDimitry Andric default:
102ac9a064cSDimitry Andric break;
103ac9a064cSDimitry Andric case Intrinsic::vector_reduce_fadd:
104ac9a064cSDimitry Andric return TTI::ReductionShuffle::Pairwise;
105ac9a064cSDimitry Andric }
106ac9a064cSDimitry Andric return TTI::ReductionShuffle::SplitHalf;
107ac9a064cSDimitry Andric }
108ac9a064cSDimitry Andric
areInlineCompatible(const Function * Caller,const Function * Callee) const109b60736ecSDimitry Andric bool WebAssemblyTTIImpl::areInlineCompatible(const Function *Caller,
110b60736ecSDimitry Andric const Function *Callee) const {
111b60736ecSDimitry Andric // Allow inlining only when the Callee has a subset of the Caller's
112b60736ecSDimitry Andric // features. In principle, we should be able to inline regardless of any
113b60736ecSDimitry Andric // features because WebAssembly supports features at module granularity, not
114b60736ecSDimitry Andric // function granularity, but without this restriction it would be possible for
115b60736ecSDimitry Andric // a module to "forget" about features if all the functions that used them
116b60736ecSDimitry Andric // were inlined.
117b60736ecSDimitry Andric const TargetMachine &TM = getTLI()->getTargetMachine();
118b60736ecSDimitry Andric
119b60736ecSDimitry Andric const FeatureBitset &CallerBits =
120b60736ecSDimitry Andric TM.getSubtargetImpl(*Caller)->getFeatureBits();
121b60736ecSDimitry Andric const FeatureBitset &CalleeBits =
122b60736ecSDimitry Andric TM.getSubtargetImpl(*Callee)->getFeatureBits();
123b60736ecSDimitry Andric
124b60736ecSDimitry Andric return (CallerBits & CalleeBits) == CalleeBits;
125b60736ecSDimitry Andric }
126344a3780SDimitry Andric
getUnrollingPreferences(Loop * L,ScalarEvolution & SE,TTI::UnrollingPreferences & UP,OptimizationRemarkEmitter * ORE) const127344a3780SDimitry Andric void WebAssemblyTTIImpl::getUnrollingPreferences(
128c0981da4SDimitry Andric Loop *L, ScalarEvolution &SE, TTI::UnrollingPreferences &UP,
129c0981da4SDimitry Andric OptimizationRemarkEmitter *ORE) const {
130344a3780SDimitry Andric // Scan the loop: don't unroll loops with calls. This is a standard approach
131344a3780SDimitry Andric // for most (all?) targets.
132344a3780SDimitry Andric for (BasicBlock *BB : L->blocks())
133344a3780SDimitry Andric for (Instruction &I : *BB)
134344a3780SDimitry Andric if (isa<CallInst>(I) || isa<InvokeInst>(I))
135344a3780SDimitry Andric if (const Function *F = cast<CallBase>(I).getCalledFunction())
136344a3780SDimitry Andric if (isLoweredToCall(F))
137344a3780SDimitry Andric return;
138344a3780SDimitry Andric
139344a3780SDimitry Andric // The chosen threshold is within the range of 'LoopMicroOpBufferSize' of
140344a3780SDimitry Andric // the various microarchitectures that use the BasicTTI implementation and
141344a3780SDimitry Andric // has been selected through heuristics across multiple cores and runtimes.
142344a3780SDimitry Andric UP.Partial = UP.Runtime = UP.UpperBound = true;
143344a3780SDimitry Andric UP.PartialThreshold = 30;
144344a3780SDimitry Andric
145344a3780SDimitry Andric // Avoid unrolling when optimizing for size.
146344a3780SDimitry Andric UP.OptSizeThreshold = 0;
147344a3780SDimitry Andric UP.PartialOptSizeThreshold = 0;
148344a3780SDimitry Andric
149344a3780SDimitry Andric // Set number of instructions optimized when "back edge"
150344a3780SDimitry Andric // becomes "fall through" to default value of 2.
151344a3780SDimitry Andric UP.BEInsns = 2;
152344a3780SDimitry Andric }
153145449b1SDimitry Andric
supportsTailCalls() const154145449b1SDimitry Andric bool WebAssemblyTTIImpl::supportsTailCalls() const {
155145449b1SDimitry Andric return getST()->hasTailCall();
156145449b1SDimitry Andric }
157