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(
4740574fefSSam Parker     unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind,
4840574fefSSam 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(
5440574fefSSam 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)
665e2c7363SChristopher Tetreault         Cost =
675e2c7363SChristopher Tetreault             cast<FixedVectorType>(VTy)->getNumElements() *
6873d7a555SDan Gohman             (TargetTransformInfo::TCC_Basic +
6940574fefSSam Parker              getArithmeticInstrCost(Opcode, VTy->getElementType(), CostKind) +
7073d7a555SDan Gohman              TargetTransformInfo::TCC_Basic);
7173d7a555SDan Gohman       break;
7273d7a555SDan Gohman     }
7373d7a555SDan Gohman   }
7473d7a555SDan Gohman   return Cost;
7573d7a555SDan Gohman }
7673d7a555SDan Gohman 
7773d7a555SDan Gohman unsigned WebAssemblyTTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val,
7873d7a555SDan Gohman                                                 unsigned Index) {
7973d7a555SDan Gohman   unsigned Cost = BasicTTIImplBase::getVectorInstrCost(Opcode, Val, Index);
8073d7a555SDan Gohman 
8173d7a555SDan Gohman   // SIMD128's insert/extract currently only take constant indices.
8273d7a555SDan Gohman   if (Index == -1u)
8373d7a555SDan Gohman     return Cost + 25 * TargetTransformInfo::TCC_Expensive;
8473d7a555SDan Gohman 
8573d7a555SDan Gohman   return Cost;
8673d7a555SDan Gohman }
87*d53d9528SThomas Lively 
88*d53d9528SThomas Lively bool WebAssemblyTTIImpl::areInlineCompatible(const Function *Caller,
89*d53d9528SThomas Lively                                              const Function *Callee) const {
90*d53d9528SThomas Lively   // Allow inlining only when the Callee has a subset of the Caller's
91*d53d9528SThomas Lively   // features. In principle, we should be able to inline regardless of any
92*d53d9528SThomas Lively   // features because WebAssembly supports features at module granularity, not
93*d53d9528SThomas Lively   // function granularity, but without this restriction it would be possible for
94*d53d9528SThomas Lively   // a module to "forget" about features if all the functions that used them
95*d53d9528SThomas Lively   // were inlined.
96*d53d9528SThomas Lively   const TargetMachine &TM = getTLI()->getTargetMachine();
97*d53d9528SThomas Lively 
98*d53d9528SThomas Lively   const FeatureBitset &CallerBits =
99*d53d9528SThomas Lively       TM.getSubtargetImpl(*Caller)->getFeatureBits();
100*d53d9528SThomas Lively   const FeatureBitset &CalleeBits =
101*d53d9528SThomas Lively       TM.getSubtargetImpl(*Callee)->getFeatureBits();
102*d53d9528SThomas Lively 
103*d53d9528SThomas Lively   return (CallerBits & CalleeBits) == CalleeBits;
104*d53d9528SThomas Lively }
105