1*0b57cec5SDimitry Andric //===-- WebAssemblyTargetTransformInfo.cpp - WebAssembly-specific TTI -----===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric ///
9*0b57cec5SDimitry Andric /// \file
10*0b57cec5SDimitry Andric /// This file defines the WebAssembly-specific TargetTransformInfo
11*0b57cec5SDimitry Andric /// implementation.
12*0b57cec5SDimitry Andric ///
13*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
14*0b57cec5SDimitry Andric
15*0b57cec5SDimitry Andric #include "WebAssemblyTargetTransformInfo.h"
16*0b57cec5SDimitry Andric #include "llvm/CodeGen/CostTable.h"
17*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
18*0b57cec5SDimitry Andric using namespace llvm;
19*0b57cec5SDimitry Andric
20*0b57cec5SDimitry Andric #define DEBUG_TYPE "wasmtti"
21*0b57cec5SDimitry Andric
22*0b57cec5SDimitry Andric TargetTransformInfo::PopcntSupportKind
getPopcntSupport(unsigned TyWidth) const23*0b57cec5SDimitry Andric WebAssemblyTTIImpl::getPopcntSupport(unsigned TyWidth) const {
24*0b57cec5SDimitry Andric assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
25*0b57cec5SDimitry Andric return TargetTransformInfo::PSK_FastHardware;
26*0b57cec5SDimitry Andric }
27*0b57cec5SDimitry Andric
getNumberOfRegisters(unsigned ClassID) const28*0b57cec5SDimitry Andric unsigned WebAssemblyTTIImpl::getNumberOfRegisters(unsigned ClassID) const {
29*0b57cec5SDimitry Andric unsigned Result = BaseT::getNumberOfRegisters(ClassID);
30*0b57cec5SDimitry Andric
31*0b57cec5SDimitry Andric // For SIMD, use at least 16 registers, as a rough guess.
32*0b57cec5SDimitry Andric bool Vector = (ClassID == 1);
33*0b57cec5SDimitry Andric if (Vector)
34*0b57cec5SDimitry Andric Result = std::max(Result, 16u);
35*0b57cec5SDimitry Andric
36*0b57cec5SDimitry Andric return Result;
37*0b57cec5SDimitry Andric }
38*0b57cec5SDimitry Andric
getRegisterBitWidth(TargetTransformInfo::RegisterKind K) const39*0b57cec5SDimitry Andric TypeSize WebAssemblyTTIImpl::getRegisterBitWidth(
40*0b57cec5SDimitry Andric TargetTransformInfo::RegisterKind K) const {
41*0b57cec5SDimitry Andric switch (K) {
42*0b57cec5SDimitry Andric case TargetTransformInfo::RGK_Scalar:
43*0b57cec5SDimitry Andric return TypeSize::getFixed(64);
44*0b57cec5SDimitry Andric case TargetTransformInfo::RGK_FixedWidthVector:
45*0b57cec5SDimitry Andric return TypeSize::getFixed(getST()->hasSIMD128() ? 128 : 64);
46*0b57cec5SDimitry Andric case TargetTransformInfo::RGK_ScalableVector:
47*0b57cec5SDimitry Andric return TypeSize::getScalable(0);
48*0b57cec5SDimitry Andric }
49*0b57cec5SDimitry Andric
50*0b57cec5SDimitry Andric llvm_unreachable("Unsupported register kind");
51*0b57cec5SDimitry Andric }
52*0b57cec5SDimitry Andric
getArithmeticInstrCost(unsigned Opcode,Type * Ty,TTI::TargetCostKind CostKind,TTI::OperandValueInfo Op1Info,TTI::OperandValueInfo Op2Info,ArrayRef<const Value * > Args,const Instruction * CxtI)53*0b57cec5SDimitry Andric InstructionCost WebAssemblyTTIImpl::getArithmeticInstrCost(
54*0b57cec5SDimitry Andric unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind,
55*0b57cec5SDimitry Andric TTI::OperandValueInfo Op1Info, TTI::OperandValueInfo Op2Info,
56*0b57cec5SDimitry Andric ArrayRef<const Value *> Args,
57*0b57cec5SDimitry Andric const Instruction *CxtI) {
58*0b57cec5SDimitry Andric
59*0b57cec5SDimitry Andric InstructionCost Cost =
60*0b57cec5SDimitry Andric BasicTTIImplBase<WebAssemblyTTIImpl>::getArithmeticInstrCost(
61*0b57cec5SDimitry Andric Opcode, Ty, CostKind, Op1Info, Op2Info);
62*0b57cec5SDimitry Andric
63*0b57cec5SDimitry Andric if (auto *VTy = dyn_cast<VectorType>(Ty)) {
64*0b57cec5SDimitry Andric switch (Opcode) {
65*0b57cec5SDimitry Andric case Instruction::LShr:
66*0b57cec5SDimitry Andric case Instruction::AShr:
67*0b57cec5SDimitry Andric case Instruction::Shl:
68*0b57cec5SDimitry Andric // SIMD128's shifts currently only accept a scalar shift count. For each
69*0b57cec5SDimitry Andric // element, we'll need to extract, op, insert. The following is a rough
70*0b57cec5SDimitry Andric // approximation.
71*0b57cec5SDimitry Andric if (!Op2Info.isUniform())
72*0b57cec5SDimitry Andric Cost =
73*0b57cec5SDimitry Andric cast<FixedVectorType>(VTy)->getNumElements() *
74*0b57cec5SDimitry Andric (TargetTransformInfo::TCC_Basic +
75*0b57cec5SDimitry Andric getArithmeticInstrCost(Opcode, VTy->getElementType(), CostKind) +
76*0b57cec5SDimitry Andric TargetTransformInfo::TCC_Basic);
77*0b57cec5SDimitry Andric break;
78*0b57cec5SDimitry Andric }
79*0b57cec5SDimitry Andric }
80*0b57cec5SDimitry Andric return Cost;
81*0b57cec5SDimitry Andric }
82*0b57cec5SDimitry Andric
83 InstructionCost
getVectorInstrCost(unsigned Opcode,Type * Val,TTI::TargetCostKind CostKind,unsigned Index,Value * Op0,Value * Op1)84 WebAssemblyTTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val,
85 TTI::TargetCostKind CostKind,
86 unsigned Index, Value *Op0, Value *Op1) {
87 InstructionCost Cost = BasicTTIImplBase::getVectorInstrCost(
88 Opcode, Val, CostKind, Index, Op0, Op1);
89
90 // SIMD128's insert/extract currently only take constant indices.
91 if (Index == -1u)
92 return Cost + 25 * TargetTransformInfo::TCC_Expensive;
93
94 return Cost;
95 }
96
areInlineCompatible(const Function * Caller,const Function * Callee) const97 bool WebAssemblyTTIImpl::areInlineCompatible(const Function *Caller,
98 const Function *Callee) const {
99 // Allow inlining only when the Callee has a subset of the Caller's
100 // features. In principle, we should be able to inline regardless of any
101 // features because WebAssembly supports features at module granularity, not
102 // function granularity, but without this restriction it would be possible for
103 // a module to "forget" about features if all the functions that used them
104 // were inlined.
105 const TargetMachine &TM = getTLI()->getTargetMachine();
106
107 const FeatureBitset &CallerBits =
108 TM.getSubtargetImpl(*Caller)->getFeatureBits();
109 const FeatureBitset &CalleeBits =
110 TM.getSubtargetImpl(*Callee)->getFeatureBits();
111
112 return (CallerBits & CalleeBits) == CalleeBits;
113 }
114
getUnrollingPreferences(Loop * L,ScalarEvolution & SE,TTI::UnrollingPreferences & UP,OptimizationRemarkEmitter * ORE) const115 void WebAssemblyTTIImpl::getUnrollingPreferences(
116 Loop *L, ScalarEvolution &SE, TTI::UnrollingPreferences &UP,
117 OptimizationRemarkEmitter *ORE) const {
118 // Scan the loop: don't unroll loops with calls. This is a standard approach
119 // for most (all?) targets.
120 for (BasicBlock *BB : L->blocks())
121 for (Instruction &I : *BB)
122 if (isa<CallInst>(I) || isa<InvokeInst>(I))
123 if (const Function *F = cast<CallBase>(I).getCalledFunction())
124 if (isLoweredToCall(F))
125 return;
126
127 // The chosen threshold is within the range of 'LoopMicroOpBufferSize' of
128 // the various microarchitectures that use the BasicTTI implementation and
129 // has been selected through heuristics across multiple cores and runtimes.
130 UP.Partial = UP.Runtime = UP.UpperBound = true;
131 UP.PartialThreshold = 30;
132
133 // Avoid unrolling when optimizing for size.
134 UP.OptSizeThreshold = 0;
135 UP.PartialOptSizeThreshold = 0;
136
137 // Set number of instructions optimized when "back edge"
138 // becomes "fall through" to default value of 2.
139 UP.BEInsns = 2;
140 }
141
supportsTailCalls() const142 bool WebAssemblyTTIImpl::supportsTailCalls() const {
143 return getST()->hasTailCall();
144 }
145