110e730a2SDan Gohman //===-- WebAssemblyTargetTransformInfo.cpp - WebAssembly-specific TTI -----===//
210e730a2SDan Gohman //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
610e730a2SDan Gohman //
710e730a2SDan Gohman //===----------------------------------------------------------------------===//
810e730a2SDan Gohman ///
910e730a2SDan Gohman /// \file
105f8f34e4SAdrian Prantl /// This file defines the WebAssembly-specific TargetTransformInfo
1110e730a2SDan Gohman /// implementation.
1210e730a2SDan Gohman ///
1310e730a2SDan Gohman //===----------------------------------------------------------------------===//
1410e730a2SDan Gohman 
1510e730a2SDan Gohman #include "WebAssemblyTargetTransformInfo.h"
16b3bde2eaSDavid Blaikie #include "llvm/CodeGen/CostTable.h"
1710e730a2SDan Gohman #include "llvm/Support/Debug.h"
1810e730a2SDan Gohman using namespace llvm;
1910e730a2SDan Gohman 
2010e730a2SDan Gohman #define DEBUG_TYPE "wasmtti"
2110e730a2SDan Gohman 
2210e730a2SDan Gohman TargetTransformInfo::PopcntSupportKind
2301612f62SDan Gohman WebAssemblyTTIImpl::getPopcntSupport(unsigned TyWidth) const {
2410e730a2SDan Gohman   assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
2501612f62SDan Gohman   return TargetTransformInfo::PSK_FastHardware;
2601612f62SDan Gohman }
2773d7a555SDan Gohman 
2873d7a555SDan Gohman unsigned WebAssemblyTTIImpl::getNumberOfRegisters(bool Vector) {
2973d7a555SDan Gohman   unsigned Result = BaseT::getNumberOfRegisters(Vector);
3073d7a555SDan Gohman 
3173d7a555SDan Gohman   // For SIMD, use at least 16 registers, as a rough guess.
3273d7a555SDan Gohman   if (Vector)
3373d7a555SDan Gohman     Result = std::max(Result, 16u);
3473d7a555SDan Gohman 
3573d7a555SDan Gohman   return Result;
3673d7a555SDan Gohman }
3773d7a555SDan Gohman 
38c0112ae8SDaniel Neilson unsigned WebAssemblyTTIImpl::getRegisterBitWidth(bool Vector) const {
3973d7a555SDan Gohman   if (Vector && getST()->hasSIMD128())
4073d7a555SDan Gohman     return 128;
4173d7a555SDan Gohman 
4273d7a555SDan Gohman   return 64;
4373d7a555SDan Gohman }
4473d7a555SDan Gohman 
4573d7a555SDan Gohman unsigned WebAssemblyTTIImpl::getArithmeticInstrCost(
4673d7a555SDan Gohman     unsigned Opcode, Type *Ty, TTI::OperandValueKind Opd1Info,
4773d7a555SDan Gohman     TTI::OperandValueKind Opd2Info, TTI::OperandValueProperties Opd1PropInfo,
482c96c433SMohammed Agabaria     TTI::OperandValueProperties Opd2PropInfo, ArrayRef<const Value *> Args) {
4973d7a555SDan Gohman 
5073d7a555SDan Gohman   unsigned Cost = BasicTTIImplBase<WebAssemblyTTIImpl>::getArithmeticInstrCost(
5173d7a555SDan Gohman       Opcode, Ty, Opd1Info, Opd2Info, Opd1PropInfo, Opd2PropInfo);
5273d7a555SDan Gohman 
5373d7a555SDan Gohman   if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
5473d7a555SDan Gohman     switch (Opcode) {
5573d7a555SDan Gohman     case Instruction::LShr:
5673d7a555SDan Gohman     case Instruction::AShr:
5773d7a555SDan Gohman     case Instruction::Shl:
5873d7a555SDan Gohman       // SIMD128's shifts currently only accept a scalar shift count. For each
5973d7a555SDan Gohman       // element, we'll need to extract, op, insert. The following is a rough
6073d7a555SDan Gohman       // approxmation.
6173d7a555SDan Gohman       if (Opd2Info != TTI::OK_UniformValue &&
6273d7a555SDan Gohman           Opd2Info != TTI::OK_UniformConstantValue)
6373d7a555SDan Gohman         Cost = VTy->getNumElements() *
6473d7a555SDan Gohman                (TargetTransformInfo::TCC_Basic +
6573d7a555SDan Gohman                 getArithmeticInstrCost(Opcode, VTy->getElementType()) +
6673d7a555SDan Gohman                 TargetTransformInfo::TCC_Basic);
6773d7a555SDan Gohman       break;
6873d7a555SDan Gohman     }
6973d7a555SDan Gohman   }
7073d7a555SDan Gohman   return Cost;
7173d7a555SDan Gohman }
7273d7a555SDan Gohman 
7373d7a555SDan Gohman unsigned WebAssemblyTTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val,
7473d7a555SDan Gohman                                                 unsigned Index) {
7573d7a555SDan Gohman   unsigned Cost = BasicTTIImplBase::getVectorInstrCost(Opcode, Val, Index);
7673d7a555SDan Gohman 
7773d7a555SDan Gohman   // SIMD128's insert/extract currently only take constant indices.
7873d7a555SDan Gohman   if (Index == -1u)
7973d7a555SDan Gohman     return Cost + 25 * TargetTransformInfo::TCC_Expensive;
8073d7a555SDan Gohman 
8173d7a555SDan Gohman   return Cost;
8273d7a555SDan Gohman }
83