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
getPopcntSupport(unsigned TyWidth) const230b57cec5SDimitry 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 
getNumberOfRegisters(unsigned ClassID) const288bcb0991SDimitry 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 
getRegisterBitWidth(TargetTransformInfo::RegisterKind K) const39*5f7ddb14SDimitry Andric TypeSize WebAssemblyTTIImpl::getRegisterBitWidth(
40*5f7ddb14SDimitry Andric     TargetTransformInfo::RegisterKind K) const {
41*5f7ddb14SDimitry Andric   switch (K) {
42*5f7ddb14SDimitry Andric   case TargetTransformInfo::RGK_Scalar:
43*5f7ddb14SDimitry Andric     return TypeSize::getFixed(64);
44*5f7ddb14SDimitry Andric   case TargetTransformInfo::RGK_FixedWidthVector:
45*5f7ddb14SDimitry Andric     return TypeSize::getFixed(getST()->hasSIMD128() ? 128 : 64);
46*5f7ddb14SDimitry Andric   case TargetTransformInfo::RGK_ScalableVector:
47*5f7ddb14SDimitry Andric     return TypeSize::getScalable(0);
480b57cec5SDimitry Andric   }
490b57cec5SDimitry Andric 
50*5f7ddb14SDimitry Andric   llvm_unreachable("Unsupported register kind");
51*5f7ddb14SDimitry Andric }
52*5f7ddb14SDimitry Andric 
getArithmeticInstrCost(unsigned Opcode,Type * Ty,TTI::TargetCostKind CostKind,TTI::OperandValueKind Opd1Info,TTI::OperandValueKind Opd2Info,TTI::OperandValueProperties Opd1PropInfo,TTI::OperandValueProperties Opd2PropInfo,ArrayRef<const Value * > Args,const Instruction * CxtI)53*5f7ddb14SDimitry Andric InstructionCost WebAssemblyTTIImpl::getArithmeticInstrCost(
545ffd83dbSDimitry Andric     unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind,
55*5f7ddb14SDimitry Andric     TTI::OperandValueKind Opd1Info, TTI::OperandValueKind Opd2Info,
56*5f7ddb14SDimitry Andric     TTI::OperandValueProperties Opd1PropInfo,
57480093f4SDimitry Andric     TTI::OperandValueProperties Opd2PropInfo, ArrayRef<const Value *> Args,
58480093f4SDimitry Andric     const Instruction *CxtI) {
590b57cec5SDimitry Andric 
60*5f7ddb14SDimitry Andric   InstructionCost Cost =
61*5f7ddb14SDimitry Andric       BasicTTIImplBase<WebAssemblyTTIImpl>::getArithmeticInstrCost(
625ffd83dbSDimitry Andric           Opcode, Ty, CostKind, Opd1Info, Opd2Info, Opd1PropInfo, Opd2PropInfo);
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric   if (auto *VTy = dyn_cast<VectorType>(Ty)) {
650b57cec5SDimitry Andric     switch (Opcode) {
660b57cec5SDimitry Andric     case Instruction::LShr:
670b57cec5SDimitry Andric     case Instruction::AShr:
680b57cec5SDimitry Andric     case Instruction::Shl:
690b57cec5SDimitry Andric       // SIMD128's shifts currently only accept a scalar shift count. For each
700b57cec5SDimitry Andric       // element, we'll need to extract, op, insert. The following is a rough
710b57cec5SDimitry Andric       // approxmation.
720b57cec5SDimitry Andric       if (Opd2Info != TTI::OK_UniformValue &&
730b57cec5SDimitry Andric           Opd2Info != TTI::OK_UniformConstantValue)
745ffd83dbSDimitry Andric         Cost =
755ffd83dbSDimitry Andric             cast<FixedVectorType>(VTy)->getNumElements() *
760b57cec5SDimitry Andric             (TargetTransformInfo::TCC_Basic +
775ffd83dbSDimitry Andric              getArithmeticInstrCost(Opcode, VTy->getElementType(), CostKind) +
780b57cec5SDimitry Andric              TargetTransformInfo::TCC_Basic);
790b57cec5SDimitry Andric       break;
800b57cec5SDimitry Andric     }
810b57cec5SDimitry Andric   }
820b57cec5SDimitry Andric   return Cost;
830b57cec5SDimitry Andric }
840b57cec5SDimitry Andric 
getVectorInstrCost(unsigned Opcode,Type * Val,unsigned Index)85*5f7ddb14SDimitry Andric InstructionCost WebAssemblyTTIImpl::getVectorInstrCost(unsigned Opcode,
86*5f7ddb14SDimitry Andric                                                        Type *Val,
870b57cec5SDimitry Andric                                                        unsigned Index) {
88*5f7ddb14SDimitry Andric   InstructionCost Cost =
89*5f7ddb14SDimitry Andric       BasicTTIImplBase::getVectorInstrCost(Opcode, Val, Index);
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric   // SIMD128's insert/extract currently only take constant indices.
920b57cec5SDimitry Andric   if (Index == -1u)
930b57cec5SDimitry Andric     return Cost + 25 * TargetTransformInfo::TCC_Expensive;
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric   return Cost;
960b57cec5SDimitry Andric }
97af732203SDimitry Andric 
areInlineCompatible(const Function * Caller,const Function * Callee) const98af732203SDimitry Andric bool WebAssemblyTTIImpl::areInlineCompatible(const Function *Caller,
99af732203SDimitry Andric                                              const Function *Callee) const {
100af732203SDimitry Andric   // Allow inlining only when the Callee has a subset of the Caller's
101af732203SDimitry Andric   // features. In principle, we should be able to inline regardless of any
102af732203SDimitry Andric   // features because WebAssembly supports features at module granularity, not
103af732203SDimitry Andric   // function granularity, but without this restriction it would be possible for
104af732203SDimitry Andric   // a module to "forget" about features if all the functions that used them
105af732203SDimitry Andric   // were inlined.
106af732203SDimitry Andric   const TargetMachine &TM = getTLI()->getTargetMachine();
107af732203SDimitry Andric 
108af732203SDimitry Andric   const FeatureBitset &CallerBits =
109af732203SDimitry Andric       TM.getSubtargetImpl(*Caller)->getFeatureBits();
110af732203SDimitry Andric   const FeatureBitset &CalleeBits =
111af732203SDimitry Andric       TM.getSubtargetImpl(*Callee)->getFeatureBits();
112af732203SDimitry Andric 
113af732203SDimitry Andric   return (CallerBits & CalleeBits) == CalleeBits;
114af732203SDimitry Andric }
115*5f7ddb14SDimitry Andric 
getUnrollingPreferences(Loop * L,ScalarEvolution & SE,TTI::UnrollingPreferences & UP) const116*5f7ddb14SDimitry Andric void WebAssemblyTTIImpl::getUnrollingPreferences(
117*5f7ddb14SDimitry Andric   Loop *L, ScalarEvolution &SE, TTI::UnrollingPreferences &UP) const {
118*5f7ddb14SDimitry Andric   // Scan the loop: don't unroll loops with calls. This is a standard approach
119*5f7ddb14SDimitry Andric   // for most (all?) targets.
120*5f7ddb14SDimitry Andric   for (BasicBlock *BB : L->blocks())
121*5f7ddb14SDimitry Andric     for (Instruction &I : *BB)
122*5f7ddb14SDimitry Andric       if (isa<CallInst>(I) || isa<InvokeInst>(I))
123*5f7ddb14SDimitry Andric         if (const Function *F = cast<CallBase>(I).getCalledFunction())
124*5f7ddb14SDimitry Andric           if (isLoweredToCall(F))
125*5f7ddb14SDimitry Andric             return;
126*5f7ddb14SDimitry Andric 
127*5f7ddb14SDimitry Andric   // The chosen threshold is within the range of 'LoopMicroOpBufferSize' of
128*5f7ddb14SDimitry Andric   // the various microarchitectures that use the BasicTTI implementation and
129*5f7ddb14SDimitry Andric   // has been selected through heuristics across multiple cores and runtimes.
130*5f7ddb14SDimitry Andric   UP.Partial = UP.Runtime = UP.UpperBound = true;
131*5f7ddb14SDimitry Andric   UP.PartialThreshold = 30;
132*5f7ddb14SDimitry Andric 
133*5f7ddb14SDimitry Andric   // Avoid unrolling when optimizing for size.
134*5f7ddb14SDimitry Andric   UP.OptSizeThreshold = 0;
135*5f7ddb14SDimitry Andric   UP.PartialOptSizeThreshold = 0;
136*5f7ddb14SDimitry Andric 
137*5f7ddb14SDimitry Andric   // Set number of instructions optimized when "back edge"
138*5f7ddb14SDimitry Andric   // becomes "fall through" to default value of 2.
139*5f7ddb14SDimitry Andric   UP.BEInsns = 2;
140*5f7ddb14SDimitry Andric }
141