xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyTargetTransformInfo.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===-- WebAssemblyTargetTransformInfo.cpp - WebAssembly-specific TTI -----===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric ///
90b57cec5SDimitry Andric /// \file
100b57cec5SDimitry Andric /// This file defines the WebAssembly-specific TargetTransformInfo
110b57cec5SDimitry Andric /// implementation.
120b57cec5SDimitry Andric ///
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "WebAssemblyTargetTransformInfo.h"
160b57cec5SDimitry Andric #include "llvm/CodeGen/CostTable.h"
170b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
180b57cec5SDimitry Andric using namespace llvm;
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric #define DEBUG_TYPE "wasmtti"
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric TargetTransformInfo::PopcntSupportKind
230b57cec5SDimitry Andric WebAssemblyTTIImpl::getPopcntSupport(unsigned TyWidth) const {
240b57cec5SDimitry Andric   assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
250b57cec5SDimitry Andric   return TargetTransformInfo::PSK_FastHardware;
260b57cec5SDimitry Andric }
270b57cec5SDimitry Andric 
288bcb0991SDimitry Andric unsigned WebAssemblyTTIImpl::getNumberOfRegisters(unsigned ClassID) const {
298bcb0991SDimitry Andric   unsigned Result = BaseT::getNumberOfRegisters(ClassID);
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric   // For SIMD, use at least 16 registers, as a rough guess.
328bcb0991SDimitry Andric   bool Vector = (ClassID == 1);
330b57cec5SDimitry Andric   if (Vector)
340b57cec5SDimitry Andric     Result = std::max(Result, 16u);
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric   return Result;
370b57cec5SDimitry Andric }
380b57cec5SDimitry Andric 
39fe6060f1SDimitry Andric TypeSize WebAssemblyTTIImpl::getRegisterBitWidth(
40fe6060f1SDimitry Andric     TargetTransformInfo::RegisterKind K) const {
41fe6060f1SDimitry Andric   switch (K) {
42fe6060f1SDimitry Andric   case TargetTransformInfo::RGK_Scalar:
43fe6060f1SDimitry Andric     return TypeSize::getFixed(64);
44fe6060f1SDimitry Andric   case TargetTransformInfo::RGK_FixedWidthVector:
45fe6060f1SDimitry Andric     return TypeSize::getFixed(getST()->hasSIMD128() ? 128 : 64);
46fe6060f1SDimitry Andric   case TargetTransformInfo::RGK_ScalableVector:
47fe6060f1SDimitry Andric     return TypeSize::getScalable(0);
480b57cec5SDimitry Andric   }
490b57cec5SDimitry Andric 
50fe6060f1SDimitry Andric   llvm_unreachable("Unsupported register kind");
51fe6060f1SDimitry Andric }
52fe6060f1SDimitry Andric 
53fe6060f1SDimitry Andric InstructionCost WebAssemblyTTIImpl::getArithmeticInstrCost(
545ffd83dbSDimitry Andric     unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind,
55bdd1243dSDimitry Andric     TTI::OperandValueInfo Op1Info, TTI::OperandValueInfo Op2Info,
56bdd1243dSDimitry Andric     ArrayRef<const Value *> Args,
57480093f4SDimitry Andric     const Instruction *CxtI) {
580b57cec5SDimitry Andric 
59fe6060f1SDimitry Andric   InstructionCost Cost =
60fe6060f1SDimitry Andric       BasicTTIImplBase<WebAssemblyTTIImpl>::getArithmeticInstrCost(
61bdd1243dSDimitry Andric           Opcode, Ty, CostKind, Op1Info, Op2Info);
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric   if (auto *VTy = dyn_cast<VectorType>(Ty)) {
640b57cec5SDimitry Andric     switch (Opcode) {
650b57cec5SDimitry Andric     case Instruction::LShr:
660b57cec5SDimitry Andric     case Instruction::AShr:
670b57cec5SDimitry Andric     case Instruction::Shl:
680b57cec5SDimitry Andric       // SIMD128's shifts currently only accept a scalar shift count. For each
690b57cec5SDimitry Andric       // element, we'll need to extract, op, insert. The following is a rough
70bdd1243dSDimitry Andric       // approximation.
71bdd1243dSDimitry Andric       if (!Op2Info.isUniform())
725ffd83dbSDimitry Andric         Cost =
735ffd83dbSDimitry Andric             cast<FixedVectorType>(VTy)->getNumElements() *
740b57cec5SDimitry Andric             (TargetTransformInfo::TCC_Basic +
755ffd83dbSDimitry Andric              getArithmeticInstrCost(Opcode, VTy->getElementType(), CostKind) +
760b57cec5SDimitry Andric              TargetTransformInfo::TCC_Basic);
770b57cec5SDimitry Andric       break;
780b57cec5SDimitry Andric     }
790b57cec5SDimitry Andric   }
800b57cec5SDimitry Andric   return Cost;
810b57cec5SDimitry Andric }
820b57cec5SDimitry Andric 
83bdd1243dSDimitry Andric InstructionCost
84bdd1243dSDimitry Andric WebAssemblyTTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val,
85bdd1243dSDimitry Andric                                        TTI::TargetCostKind CostKind,
86bdd1243dSDimitry Andric                                        unsigned Index, Value *Op0, Value *Op1) {
87bdd1243dSDimitry Andric   InstructionCost Cost = BasicTTIImplBase::getVectorInstrCost(
88bdd1243dSDimitry Andric       Opcode, Val, CostKind, Index, Op0, Op1);
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric   // SIMD128's insert/extract currently only take constant indices.
910b57cec5SDimitry Andric   if (Index == -1u)
920b57cec5SDimitry Andric     return Cost + 25 * TargetTransformInfo::TCC_Expensive;
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric   return Cost;
950b57cec5SDimitry Andric }
96e8d8bef9SDimitry Andric 
97*0fca6ea1SDimitry Andric TTI::ReductionShuffle WebAssemblyTTIImpl::getPreferredExpandedReductionShuffle(
98*0fca6ea1SDimitry Andric     const IntrinsicInst *II) const {
99*0fca6ea1SDimitry Andric 
100*0fca6ea1SDimitry Andric   switch (II->getIntrinsicID()) {
101*0fca6ea1SDimitry Andric   default:
102*0fca6ea1SDimitry Andric     break;
103*0fca6ea1SDimitry Andric   case Intrinsic::vector_reduce_fadd:
104*0fca6ea1SDimitry Andric     return TTI::ReductionShuffle::Pairwise;
105*0fca6ea1SDimitry Andric   }
106*0fca6ea1SDimitry Andric   return TTI::ReductionShuffle::SplitHalf;
107*0fca6ea1SDimitry Andric }
108*0fca6ea1SDimitry Andric 
109e8d8bef9SDimitry Andric bool WebAssemblyTTIImpl::areInlineCompatible(const Function *Caller,
110e8d8bef9SDimitry Andric                                              const Function *Callee) const {
111e8d8bef9SDimitry Andric   // Allow inlining only when the Callee has a subset of the Caller's
112e8d8bef9SDimitry Andric   // features. In principle, we should be able to inline regardless of any
113e8d8bef9SDimitry Andric   // features because WebAssembly supports features at module granularity, not
114e8d8bef9SDimitry Andric   // function granularity, but without this restriction it would be possible for
115e8d8bef9SDimitry Andric   // a module to "forget" about features if all the functions that used them
116e8d8bef9SDimitry Andric   // were inlined.
117e8d8bef9SDimitry Andric   const TargetMachine &TM = getTLI()->getTargetMachine();
118e8d8bef9SDimitry Andric 
119e8d8bef9SDimitry Andric   const FeatureBitset &CallerBits =
120e8d8bef9SDimitry Andric       TM.getSubtargetImpl(*Caller)->getFeatureBits();
121e8d8bef9SDimitry Andric   const FeatureBitset &CalleeBits =
122e8d8bef9SDimitry Andric       TM.getSubtargetImpl(*Callee)->getFeatureBits();
123e8d8bef9SDimitry Andric 
124e8d8bef9SDimitry Andric   return (CallerBits & CalleeBits) == CalleeBits;
125e8d8bef9SDimitry Andric }
126fe6060f1SDimitry Andric 
127fe6060f1SDimitry Andric void WebAssemblyTTIImpl::getUnrollingPreferences(
128349cc55cSDimitry Andric     Loop *L, ScalarEvolution &SE, TTI::UnrollingPreferences &UP,
129349cc55cSDimitry Andric     OptimizationRemarkEmitter *ORE) const {
130fe6060f1SDimitry Andric   // Scan the loop: don't unroll loops with calls. This is a standard approach
131fe6060f1SDimitry Andric   // for most (all?) targets.
132fe6060f1SDimitry Andric   for (BasicBlock *BB : L->blocks())
133fe6060f1SDimitry Andric     for (Instruction &I : *BB)
134fe6060f1SDimitry Andric       if (isa<CallInst>(I) || isa<InvokeInst>(I))
135fe6060f1SDimitry Andric         if (const Function *F = cast<CallBase>(I).getCalledFunction())
136fe6060f1SDimitry Andric           if (isLoweredToCall(F))
137fe6060f1SDimitry Andric             return;
138fe6060f1SDimitry Andric 
139fe6060f1SDimitry Andric   // The chosen threshold is within the range of 'LoopMicroOpBufferSize' of
140fe6060f1SDimitry Andric   // the various microarchitectures that use the BasicTTI implementation and
141fe6060f1SDimitry Andric   // has been selected through heuristics across multiple cores and runtimes.
142fe6060f1SDimitry Andric   UP.Partial = UP.Runtime = UP.UpperBound = true;
143fe6060f1SDimitry Andric   UP.PartialThreshold = 30;
144fe6060f1SDimitry Andric 
145fe6060f1SDimitry Andric   // Avoid unrolling when optimizing for size.
146fe6060f1SDimitry Andric   UP.OptSizeThreshold = 0;
147fe6060f1SDimitry Andric   UP.PartialOptSizeThreshold = 0;
148fe6060f1SDimitry Andric 
149fe6060f1SDimitry Andric   // Set number of instructions optimized when "back edge"
150fe6060f1SDimitry Andric   // becomes "fall through" to default value of 2.
151fe6060f1SDimitry Andric   UP.BEInsns = 2;
152fe6060f1SDimitry Andric }
15381ad6265SDimitry Andric 
15481ad6265SDimitry Andric bool WebAssemblyTTIImpl::supportsTailCalls() const {
15581ad6265SDimitry Andric   return getST()->hasTailCall();
15681ad6265SDimitry Andric }
157