110e730a2SDan Gohman //===-- WebAssemblyTargetTransformInfo.cpp - WebAssembly-specific TTI -----===//
210e730a2SDan Gohman //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler 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 
289802268aSZi Xuan Wu unsigned WebAssemblyTTIImpl::getNumberOfRegisters(unsigned ClassID) const {
299802268aSZi Xuan Wu   unsigned Result = BaseT::getNumberOfRegisters(ClassID);
3073d7a555SDan Gohman 
3173d7a555SDan Gohman   // For SIMD, use at least 16 registers, as a rough guess.
329802268aSZi Xuan Wu   bool Vector = (ClassID == 1);
3373d7a555SDan Gohman   if (Vector)
3473d7a555SDan Gohman     Result = std::max(Result, 16u);
3573d7a555SDan Gohman 
3673d7a555SDan Gohman   return Result;
3773d7a555SDan Gohman }
3873d7a555SDan Gohman 
39c0112ae8SDaniel Neilson unsigned WebAssemblyTTIImpl::getRegisterBitWidth(bool Vector) const {
4073d7a555SDan Gohman   if (Vector && getST()->hasSIMD128())
4173d7a555SDan Gohman     return 128;
4273d7a555SDan Gohman 
4373d7a555SDan Gohman   return 64;
4473d7a555SDan Gohman }
4573d7a555SDan Gohman 
4673d7a555SDan Gohman unsigned WebAssemblyTTIImpl::getArithmeticInstrCost(
47*40574fefSSam Parker     unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind,
48*40574fefSSam Parker     TTI::OperandValueKind Opd1Info,
4973d7a555SDan Gohman     TTI::OperandValueKind Opd2Info, TTI::OperandValueProperties Opd1PropInfo,
50be7a1070SDavid Green     TTI::OperandValueProperties Opd2PropInfo, ArrayRef<const Value *> Args,
51be7a1070SDavid Green     const Instruction *CxtI) {
5273d7a555SDan Gohman 
5373d7a555SDan Gohman   unsigned Cost = BasicTTIImplBase<WebAssemblyTTIImpl>::getArithmeticInstrCost(
54*40574fefSSam Parker       Opcode, Ty, CostKind, Opd1Info, Opd2Info, Opd1PropInfo, Opd2PropInfo);
5573d7a555SDan Gohman 
5618c56a07SHeejin Ahn   if (auto *VTy = dyn_cast<VectorType>(Ty)) {
5773d7a555SDan Gohman     switch (Opcode) {
5873d7a555SDan Gohman     case Instruction::LShr:
5973d7a555SDan Gohman     case Instruction::AShr:
6073d7a555SDan Gohman     case Instruction::Shl:
6173d7a555SDan Gohman       // SIMD128's shifts currently only accept a scalar shift count. For each
6273d7a555SDan Gohman       // element, we'll need to extract, op, insert. The following is a rough
6373d7a555SDan Gohman       // approxmation.
6473d7a555SDan Gohman       if (Opd2Info != TTI::OK_UniformValue &&
6573d7a555SDan Gohman           Opd2Info != TTI::OK_UniformConstantValue)
6673d7a555SDan Gohman         Cost = VTy->getNumElements() *
6773d7a555SDan Gohman                (TargetTransformInfo::TCC_Basic +
68*40574fefSSam Parker                 getArithmeticInstrCost(Opcode, VTy->getElementType(), CostKind) +
6973d7a555SDan Gohman                 TargetTransformInfo::TCC_Basic);
7073d7a555SDan Gohman       break;
7173d7a555SDan Gohman     }
7273d7a555SDan Gohman   }
7373d7a555SDan Gohman   return Cost;
7473d7a555SDan Gohman }
7573d7a555SDan Gohman 
7673d7a555SDan Gohman unsigned WebAssemblyTTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val,
7773d7a555SDan Gohman                                                 unsigned Index) {
7873d7a555SDan Gohman   unsigned Cost = BasicTTIImplBase::getVectorInstrCost(Opcode, Val, Index);
7973d7a555SDan Gohman 
8073d7a555SDan Gohman   // SIMD128's insert/extract currently only take constant indices.
8173d7a555SDan Gohman   if (Index == -1u)
8273d7a555SDan Gohman     return Cost + 25 * TargetTransformInfo::TCC_Expensive;
8373d7a555SDan Gohman 
8473d7a555SDan Gohman   return Cost;
8573d7a555SDan Gohman }
86