10b57cec5SDimitry Andric //=- WebAssemblyISelLowering.cpp - WebAssembly DAG Lowering Implementation -==// 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 implements the WebAssemblyTargetLowering class. 110b57cec5SDimitry Andric /// 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #include "WebAssemblyISelLowering.h" 150b57cec5SDimitry Andric #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" 160b57cec5SDimitry Andric #include "WebAssemblyMachineFunctionInfo.h" 170b57cec5SDimitry Andric #include "WebAssemblySubtarget.h" 180b57cec5SDimitry Andric #include "WebAssemblyTargetMachine.h" 190b57cec5SDimitry Andric #include "llvm/CodeGen/Analysis.h" 200b57cec5SDimitry Andric #include "llvm/CodeGen/CallingConvLower.h" 210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineJumpTableInfo.h" 230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h" 240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 250b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAG.h" 260b57cec5SDimitry Andric #include "llvm/CodeGen/WasmEHFuncInfo.h" 270b57cec5SDimitry Andric #include "llvm/IR/DiagnosticInfo.h" 280b57cec5SDimitry Andric #include "llvm/IR/DiagnosticPrinter.h" 290b57cec5SDimitry Andric #include "llvm/IR/Function.h" 300b57cec5SDimitry Andric #include "llvm/IR/Intrinsics.h" 31*480093f4SDimitry Andric #include "llvm/IR/IntrinsicsWebAssembly.h" 320b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 330b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 340b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 350b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h" 360b57cec5SDimitry Andric using namespace llvm; 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric #define DEBUG_TYPE "wasm-lower" 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric WebAssemblyTargetLowering::WebAssemblyTargetLowering( 410b57cec5SDimitry Andric const TargetMachine &TM, const WebAssemblySubtarget &STI) 420b57cec5SDimitry Andric : TargetLowering(TM), Subtarget(&STI) { 430b57cec5SDimitry Andric auto MVTPtr = Subtarget->hasAddr64() ? MVT::i64 : MVT::i32; 440b57cec5SDimitry Andric 450b57cec5SDimitry Andric // Booleans always contain 0 or 1. 460b57cec5SDimitry Andric setBooleanContents(ZeroOrOneBooleanContent); 470b57cec5SDimitry Andric // Except in SIMD vectors 480b57cec5SDimitry Andric setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); 490b57cec5SDimitry Andric // We don't know the microarchitecture here, so just reduce register pressure. 500b57cec5SDimitry Andric setSchedulingPreference(Sched::RegPressure); 510b57cec5SDimitry Andric // Tell ISel that we have a stack pointer. 520b57cec5SDimitry Andric setStackPointerRegisterToSaveRestore( 530b57cec5SDimitry Andric Subtarget->hasAddr64() ? WebAssembly::SP64 : WebAssembly::SP32); 540b57cec5SDimitry Andric // Set up the register classes. 550b57cec5SDimitry Andric addRegisterClass(MVT::i32, &WebAssembly::I32RegClass); 560b57cec5SDimitry Andric addRegisterClass(MVT::i64, &WebAssembly::I64RegClass); 570b57cec5SDimitry Andric addRegisterClass(MVT::f32, &WebAssembly::F32RegClass); 580b57cec5SDimitry Andric addRegisterClass(MVT::f64, &WebAssembly::F64RegClass); 590b57cec5SDimitry Andric if (Subtarget->hasSIMD128()) { 600b57cec5SDimitry Andric addRegisterClass(MVT::v16i8, &WebAssembly::V128RegClass); 610b57cec5SDimitry Andric addRegisterClass(MVT::v8i16, &WebAssembly::V128RegClass); 620b57cec5SDimitry Andric addRegisterClass(MVT::v4i32, &WebAssembly::V128RegClass); 630b57cec5SDimitry Andric addRegisterClass(MVT::v4f32, &WebAssembly::V128RegClass); 640b57cec5SDimitry Andric } 650b57cec5SDimitry Andric if (Subtarget->hasUnimplementedSIMD128()) { 660b57cec5SDimitry Andric addRegisterClass(MVT::v2i64, &WebAssembly::V128RegClass); 670b57cec5SDimitry Andric addRegisterClass(MVT::v2f64, &WebAssembly::V128RegClass); 680b57cec5SDimitry Andric } 690b57cec5SDimitry Andric // Compute derived properties from the register classes. 700b57cec5SDimitry Andric computeRegisterProperties(Subtarget->getRegisterInfo()); 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric setOperationAction(ISD::GlobalAddress, MVTPtr, Custom); 730b57cec5SDimitry Andric setOperationAction(ISD::ExternalSymbol, MVTPtr, Custom); 740b57cec5SDimitry Andric setOperationAction(ISD::JumpTable, MVTPtr, Custom); 750b57cec5SDimitry Andric setOperationAction(ISD::BlockAddress, MVTPtr, Custom); 760b57cec5SDimitry Andric setOperationAction(ISD::BRIND, MVT::Other, Custom); 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric // Take the default expansion for va_arg, va_copy, and va_end. There is no 790b57cec5SDimitry Andric // default action for va_start, so we do that custom. 800b57cec5SDimitry Andric setOperationAction(ISD::VASTART, MVT::Other, Custom); 810b57cec5SDimitry Andric setOperationAction(ISD::VAARG, MVT::Other, Expand); 820b57cec5SDimitry Andric setOperationAction(ISD::VACOPY, MVT::Other, Expand); 830b57cec5SDimitry Andric setOperationAction(ISD::VAEND, MVT::Other, Expand); 840b57cec5SDimitry Andric 850b57cec5SDimitry Andric for (auto T : {MVT::f32, MVT::f64, MVT::v4f32, MVT::v2f64}) { 860b57cec5SDimitry Andric // Don't expand the floating-point types to constant pools. 870b57cec5SDimitry Andric setOperationAction(ISD::ConstantFP, T, Legal); 880b57cec5SDimitry Andric // Expand floating-point comparisons. 890b57cec5SDimitry Andric for (auto CC : {ISD::SETO, ISD::SETUO, ISD::SETUEQ, ISD::SETONE, 900b57cec5SDimitry Andric ISD::SETULT, ISD::SETULE, ISD::SETUGT, ISD::SETUGE}) 910b57cec5SDimitry Andric setCondCodeAction(CC, T, Expand); 920b57cec5SDimitry Andric // Expand floating-point library function operators. 930b57cec5SDimitry Andric for (auto Op : 940b57cec5SDimitry Andric {ISD::FSIN, ISD::FCOS, ISD::FSINCOS, ISD::FPOW, ISD::FREM, ISD::FMA}) 950b57cec5SDimitry Andric setOperationAction(Op, T, Expand); 960b57cec5SDimitry Andric // Note supported floating-point library function operators that otherwise 970b57cec5SDimitry Andric // default to expand. 980b57cec5SDimitry Andric for (auto Op : 990b57cec5SDimitry Andric {ISD::FCEIL, ISD::FFLOOR, ISD::FTRUNC, ISD::FNEARBYINT, ISD::FRINT}) 1000b57cec5SDimitry Andric setOperationAction(Op, T, Legal); 1010b57cec5SDimitry Andric // Support minimum and maximum, which otherwise default to expand. 1020b57cec5SDimitry Andric setOperationAction(ISD::FMINIMUM, T, Legal); 1030b57cec5SDimitry Andric setOperationAction(ISD::FMAXIMUM, T, Legal); 1040b57cec5SDimitry Andric // WebAssembly currently has no builtin f16 support. 1050b57cec5SDimitry Andric setOperationAction(ISD::FP16_TO_FP, T, Expand); 1060b57cec5SDimitry Andric setOperationAction(ISD::FP_TO_FP16, T, Expand); 1070b57cec5SDimitry Andric setLoadExtAction(ISD::EXTLOAD, T, MVT::f16, Expand); 1080b57cec5SDimitry Andric setTruncStoreAction(T, MVT::f16, Expand); 1090b57cec5SDimitry Andric } 1100b57cec5SDimitry Andric 1110b57cec5SDimitry Andric // Expand unavailable integer operations. 1120b57cec5SDimitry Andric for (auto Op : 1130b57cec5SDimitry Andric {ISD::BSWAP, ISD::SMUL_LOHI, ISD::UMUL_LOHI, ISD::MULHS, ISD::MULHU, 1140b57cec5SDimitry Andric ISD::SDIVREM, ISD::UDIVREM, ISD::SHL_PARTS, ISD::SRA_PARTS, 1150b57cec5SDimitry Andric ISD::SRL_PARTS, ISD::ADDC, ISD::ADDE, ISD::SUBC, ISD::SUBE}) { 1160b57cec5SDimitry Andric for (auto T : {MVT::i32, MVT::i64}) 1170b57cec5SDimitry Andric setOperationAction(Op, T, Expand); 1180b57cec5SDimitry Andric if (Subtarget->hasSIMD128()) 1190b57cec5SDimitry Andric for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32}) 1200b57cec5SDimitry Andric setOperationAction(Op, T, Expand); 1210b57cec5SDimitry Andric if (Subtarget->hasUnimplementedSIMD128()) 1220b57cec5SDimitry Andric setOperationAction(Op, MVT::v2i64, Expand); 1230b57cec5SDimitry Andric } 1240b57cec5SDimitry Andric 1250b57cec5SDimitry Andric // SIMD-specific configuration 1260b57cec5SDimitry Andric if (Subtarget->hasSIMD128()) { 1270b57cec5SDimitry Andric // Support saturating add for i8x16 and i16x8 1280b57cec5SDimitry Andric for (auto Op : {ISD::SADDSAT, ISD::UADDSAT}) 1290b57cec5SDimitry Andric for (auto T : {MVT::v16i8, MVT::v8i16}) 1300b57cec5SDimitry Andric setOperationAction(Op, T, Legal); 1310b57cec5SDimitry Andric 1320b57cec5SDimitry Andric // Custom lower BUILD_VECTORs to minimize number of replace_lanes 1330b57cec5SDimitry Andric for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v4f32}) 1340b57cec5SDimitry Andric setOperationAction(ISD::BUILD_VECTOR, T, Custom); 1350b57cec5SDimitry Andric if (Subtarget->hasUnimplementedSIMD128()) 1360b57cec5SDimitry Andric for (auto T : {MVT::v2i64, MVT::v2f64}) 1370b57cec5SDimitry Andric setOperationAction(ISD::BUILD_VECTOR, T, Custom); 1380b57cec5SDimitry Andric 1390b57cec5SDimitry Andric // We have custom shuffle lowering to expose the shuffle mask 1400b57cec5SDimitry Andric for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v4f32}) 1410b57cec5SDimitry Andric setOperationAction(ISD::VECTOR_SHUFFLE, T, Custom); 1420b57cec5SDimitry Andric if (Subtarget->hasUnimplementedSIMD128()) 1430b57cec5SDimitry Andric for (auto T: {MVT::v2i64, MVT::v2f64}) 1440b57cec5SDimitry Andric setOperationAction(ISD::VECTOR_SHUFFLE, T, Custom); 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric // Custom lowering since wasm shifts must have a scalar shift amount 1470b57cec5SDimitry Andric for (auto Op : {ISD::SHL, ISD::SRA, ISD::SRL}) { 1480b57cec5SDimitry Andric for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32}) 1490b57cec5SDimitry Andric setOperationAction(Op, T, Custom); 1500b57cec5SDimitry Andric if (Subtarget->hasUnimplementedSIMD128()) 1510b57cec5SDimitry Andric setOperationAction(Op, MVT::v2i64, Custom); 1520b57cec5SDimitry Andric } 1530b57cec5SDimitry Andric 1540b57cec5SDimitry Andric // Custom lower lane accesses to expand out variable indices 1550b57cec5SDimitry Andric for (auto Op : {ISD::EXTRACT_VECTOR_ELT, ISD::INSERT_VECTOR_ELT}) { 1560b57cec5SDimitry Andric for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v4f32}) 1570b57cec5SDimitry Andric setOperationAction(Op, T, Custom); 1580b57cec5SDimitry Andric if (Subtarget->hasUnimplementedSIMD128()) 1590b57cec5SDimitry Andric for (auto T : {MVT::v2i64, MVT::v2f64}) 1600b57cec5SDimitry Andric setOperationAction(Op, T, Custom); 1610b57cec5SDimitry Andric } 1620b57cec5SDimitry Andric 1630b57cec5SDimitry Andric // There is no i64x2.mul instruction 1640b57cec5SDimitry Andric setOperationAction(ISD::MUL, MVT::v2i64, Expand); 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric // There are no vector select instructions 1670b57cec5SDimitry Andric for (auto Op : {ISD::VSELECT, ISD::SELECT_CC, ISD::SELECT}) { 1680b57cec5SDimitry Andric for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v4f32}) 1690b57cec5SDimitry Andric setOperationAction(Op, T, Expand); 1700b57cec5SDimitry Andric if (Subtarget->hasUnimplementedSIMD128()) 1710b57cec5SDimitry Andric for (auto T : {MVT::v2i64, MVT::v2f64}) 1720b57cec5SDimitry Andric setOperationAction(Op, T, Expand); 1730b57cec5SDimitry Andric } 1740b57cec5SDimitry Andric 1750b57cec5SDimitry Andric // Expand integer operations supported for scalars but not SIMD 1760b57cec5SDimitry Andric for (auto Op : {ISD::CTLZ, ISD::CTTZ, ISD::CTPOP, ISD::SDIV, ISD::UDIV, 1770b57cec5SDimitry Andric ISD::SREM, ISD::UREM, ISD::ROTL, ISD::ROTR}) { 1780b57cec5SDimitry Andric for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32}) 1790b57cec5SDimitry Andric setOperationAction(Op, T, Expand); 1800b57cec5SDimitry Andric if (Subtarget->hasUnimplementedSIMD128()) 1810b57cec5SDimitry Andric setOperationAction(Op, MVT::v2i64, Expand); 1820b57cec5SDimitry Andric } 1830b57cec5SDimitry Andric 184*480093f4SDimitry Andric // But we do have integer min and max operations 185*480093f4SDimitry Andric if (Subtarget->hasUnimplementedSIMD128()) { 186*480093f4SDimitry Andric for (auto Op : {ISD::SMIN, ISD::SMAX, ISD::UMIN, ISD::UMAX}) 187*480093f4SDimitry Andric for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32}) 188*480093f4SDimitry Andric setOperationAction(Op, T, Legal); 189*480093f4SDimitry Andric } 190*480093f4SDimitry Andric 1910b57cec5SDimitry Andric // Expand float operations supported for scalars but not SIMD 1920b57cec5SDimitry Andric for (auto Op : {ISD::FCEIL, ISD::FFLOOR, ISD::FTRUNC, ISD::FNEARBYINT, 1930b57cec5SDimitry Andric ISD::FCOPYSIGN, ISD::FLOG, ISD::FLOG2, ISD::FLOG10, 1940b57cec5SDimitry Andric ISD::FEXP, ISD::FEXP2, ISD::FRINT}) { 1950b57cec5SDimitry Andric setOperationAction(Op, MVT::v4f32, Expand); 1960b57cec5SDimitry Andric if (Subtarget->hasUnimplementedSIMD128()) 1970b57cec5SDimitry Andric setOperationAction(Op, MVT::v2f64, Expand); 1980b57cec5SDimitry Andric } 1990b57cec5SDimitry Andric 200*480093f4SDimitry Andric // Expand operations not supported for i64x2 vectors 201*480093f4SDimitry Andric if (Subtarget->hasUnimplementedSIMD128()) 202*480093f4SDimitry Andric for (unsigned CC = 0; CC < ISD::SETCC_INVALID; ++CC) 203*480093f4SDimitry Andric setCondCodeAction(static_cast<ISD::CondCode>(CC), MVT::v2i64, Custom); 204*480093f4SDimitry Andric 2050b57cec5SDimitry Andric // Expand additional SIMD ops that V8 hasn't implemented yet 2060b57cec5SDimitry Andric if (!Subtarget->hasUnimplementedSIMD128()) { 2070b57cec5SDimitry Andric setOperationAction(ISD::FSQRT, MVT::v4f32, Expand); 2080b57cec5SDimitry Andric setOperationAction(ISD::FDIV, MVT::v4f32, Expand); 2090b57cec5SDimitry Andric } 2100b57cec5SDimitry Andric } 2110b57cec5SDimitry Andric 2120b57cec5SDimitry Andric // As a special case, these operators use the type to mean the type to 2130b57cec5SDimitry Andric // sign-extend from. 2140b57cec5SDimitry Andric setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); 2150b57cec5SDimitry Andric if (!Subtarget->hasSignExt()) { 2160b57cec5SDimitry Andric // Sign extends are legal only when extending a vector extract 2170b57cec5SDimitry Andric auto Action = Subtarget->hasSIMD128() ? Custom : Expand; 2180b57cec5SDimitry Andric for (auto T : {MVT::i8, MVT::i16, MVT::i32}) 2190b57cec5SDimitry Andric setOperationAction(ISD::SIGN_EXTEND_INREG, T, Action); 2200b57cec5SDimitry Andric } 2218bcb0991SDimitry Andric for (auto T : MVT::integer_fixedlen_vector_valuetypes()) 2220b57cec5SDimitry Andric setOperationAction(ISD::SIGN_EXTEND_INREG, T, Expand); 2230b57cec5SDimitry Andric 2240b57cec5SDimitry Andric // Dynamic stack allocation: use the default expansion. 2250b57cec5SDimitry Andric setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); 2260b57cec5SDimitry Andric setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); 2270b57cec5SDimitry Andric setOperationAction(ISD::DYNAMIC_STACKALLOC, MVTPtr, Expand); 2280b57cec5SDimitry Andric 2290b57cec5SDimitry Andric setOperationAction(ISD::FrameIndex, MVT::i32, Custom); 2300b57cec5SDimitry Andric setOperationAction(ISD::CopyToReg, MVT::Other, Custom); 2310b57cec5SDimitry Andric 2320b57cec5SDimitry Andric // Expand these forms; we pattern-match the forms that we can handle in isel. 2330b57cec5SDimitry Andric for (auto T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64}) 2340b57cec5SDimitry Andric for (auto Op : {ISD::BR_CC, ISD::SELECT_CC}) 2350b57cec5SDimitry Andric setOperationAction(Op, T, Expand); 2360b57cec5SDimitry Andric 2370b57cec5SDimitry Andric // We have custom switch handling. 2380b57cec5SDimitry Andric setOperationAction(ISD::BR_JT, MVT::Other, Custom); 2390b57cec5SDimitry Andric 2400b57cec5SDimitry Andric // WebAssembly doesn't have: 2410b57cec5SDimitry Andric // - Floating-point extending loads. 2420b57cec5SDimitry Andric // - Floating-point truncating stores. 2430b57cec5SDimitry Andric // - i1 extending loads. 2448bcb0991SDimitry Andric // - truncating SIMD stores and most extending loads 2450b57cec5SDimitry Andric setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand); 2460b57cec5SDimitry Andric setTruncStoreAction(MVT::f64, MVT::f32, Expand); 2470b57cec5SDimitry Andric for (auto T : MVT::integer_valuetypes()) 2480b57cec5SDimitry Andric for (auto Ext : {ISD::EXTLOAD, ISD::ZEXTLOAD, ISD::SEXTLOAD}) 2490b57cec5SDimitry Andric setLoadExtAction(Ext, T, MVT::i1, Promote); 2500b57cec5SDimitry Andric if (Subtarget->hasSIMD128()) { 2510b57cec5SDimitry Andric for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v2i64, MVT::v4f32, 2520b57cec5SDimitry Andric MVT::v2f64}) { 2538bcb0991SDimitry Andric for (auto MemT : MVT::fixedlen_vector_valuetypes()) { 2540b57cec5SDimitry Andric if (MVT(T) != MemT) { 2550b57cec5SDimitry Andric setTruncStoreAction(T, MemT, Expand); 2560b57cec5SDimitry Andric for (auto Ext : {ISD::EXTLOAD, ISD::ZEXTLOAD, ISD::SEXTLOAD}) 2570b57cec5SDimitry Andric setLoadExtAction(Ext, T, MemT, Expand); 2580b57cec5SDimitry Andric } 2590b57cec5SDimitry Andric } 2600b57cec5SDimitry Andric } 2618bcb0991SDimitry Andric // But some vector extending loads are legal 2628bcb0991SDimitry Andric if (Subtarget->hasUnimplementedSIMD128()) { 2638bcb0991SDimitry Andric for (auto Ext : {ISD::EXTLOAD, ISD::SEXTLOAD, ISD::ZEXTLOAD}) { 2648bcb0991SDimitry Andric setLoadExtAction(Ext, MVT::v8i16, MVT::v8i8, Legal); 2658bcb0991SDimitry Andric setLoadExtAction(Ext, MVT::v4i32, MVT::v4i16, Legal); 2668bcb0991SDimitry Andric setLoadExtAction(Ext, MVT::v2i64, MVT::v2i32, Legal); 2678bcb0991SDimitry Andric } 2688bcb0991SDimitry Andric } 2690b57cec5SDimitry Andric } 2700b57cec5SDimitry Andric 2710b57cec5SDimitry Andric // Don't do anything clever with build_pairs 2720b57cec5SDimitry Andric setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand); 2730b57cec5SDimitry Andric 2740b57cec5SDimitry Andric // Trap lowers to wasm unreachable 2750b57cec5SDimitry Andric setOperationAction(ISD::TRAP, MVT::Other, Legal); 2760b57cec5SDimitry Andric 2770b57cec5SDimitry Andric // Exception handling intrinsics 2780b57cec5SDimitry Andric setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); 2790b57cec5SDimitry Andric setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom); 2800b57cec5SDimitry Andric 2810b57cec5SDimitry Andric setMaxAtomicSizeInBitsSupported(64); 2820b57cec5SDimitry Andric 2830b57cec5SDimitry Andric // Override the __gnu_f2h_ieee/__gnu_h2f_ieee names so that the f32 name is 2840b57cec5SDimitry Andric // consistent with the f64 and f128 names. 2850b57cec5SDimitry Andric setLibcallName(RTLIB::FPEXT_F16_F32, "__extendhfsf2"); 2860b57cec5SDimitry Andric setLibcallName(RTLIB::FPROUND_F32_F16, "__truncsfhf2"); 2870b57cec5SDimitry Andric 2880b57cec5SDimitry Andric // Define the emscripten name for return address helper. 2890b57cec5SDimitry Andric // TODO: when implementing other WASM backends, make this generic or only do 2900b57cec5SDimitry Andric // this on emscripten depending on what they end up doing. 2910b57cec5SDimitry Andric setLibcallName(RTLIB::RETURN_ADDRESS, "emscripten_return_address"); 2920b57cec5SDimitry Andric 2930b57cec5SDimitry Andric // Always convert switches to br_tables unless there is only one case, which 2940b57cec5SDimitry Andric // is equivalent to a simple branch. This reduces code size for wasm, and we 2950b57cec5SDimitry Andric // defer possible jump table optimizations to the VM. 2960b57cec5SDimitry Andric setMinimumJumpTableEntries(2); 2970b57cec5SDimitry Andric } 2980b57cec5SDimitry Andric 2990b57cec5SDimitry Andric TargetLowering::AtomicExpansionKind 3000b57cec5SDimitry Andric WebAssemblyTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const { 3010b57cec5SDimitry Andric // We have wasm instructions for these 3020b57cec5SDimitry Andric switch (AI->getOperation()) { 3030b57cec5SDimitry Andric case AtomicRMWInst::Add: 3040b57cec5SDimitry Andric case AtomicRMWInst::Sub: 3050b57cec5SDimitry Andric case AtomicRMWInst::And: 3060b57cec5SDimitry Andric case AtomicRMWInst::Or: 3070b57cec5SDimitry Andric case AtomicRMWInst::Xor: 3080b57cec5SDimitry Andric case AtomicRMWInst::Xchg: 3090b57cec5SDimitry Andric return AtomicExpansionKind::None; 3100b57cec5SDimitry Andric default: 3110b57cec5SDimitry Andric break; 3120b57cec5SDimitry Andric } 3130b57cec5SDimitry Andric return AtomicExpansionKind::CmpXChg; 3140b57cec5SDimitry Andric } 3150b57cec5SDimitry Andric 3160b57cec5SDimitry Andric FastISel *WebAssemblyTargetLowering::createFastISel( 3170b57cec5SDimitry Andric FunctionLoweringInfo &FuncInfo, const TargetLibraryInfo *LibInfo) const { 3180b57cec5SDimitry Andric return WebAssembly::createFastISel(FuncInfo, LibInfo); 3190b57cec5SDimitry Andric } 3200b57cec5SDimitry Andric 3210b57cec5SDimitry Andric MVT WebAssemblyTargetLowering::getScalarShiftAmountTy(const DataLayout & /*DL*/, 3220b57cec5SDimitry Andric EVT VT) const { 3230b57cec5SDimitry Andric unsigned BitWidth = NextPowerOf2(VT.getSizeInBits() - 1); 3240b57cec5SDimitry Andric if (BitWidth > 1 && BitWidth < 8) 3250b57cec5SDimitry Andric BitWidth = 8; 3260b57cec5SDimitry Andric 3270b57cec5SDimitry Andric if (BitWidth > 64) { 3280b57cec5SDimitry Andric // The shift will be lowered to a libcall, and compiler-rt libcalls expect 3290b57cec5SDimitry Andric // the count to be an i32. 3300b57cec5SDimitry Andric BitWidth = 32; 3310b57cec5SDimitry Andric assert(BitWidth >= Log2_32_Ceil(VT.getSizeInBits()) && 3320b57cec5SDimitry Andric "32-bit shift counts ought to be enough for anyone"); 3330b57cec5SDimitry Andric } 3340b57cec5SDimitry Andric 3350b57cec5SDimitry Andric MVT Result = MVT::getIntegerVT(BitWidth); 3360b57cec5SDimitry Andric assert(Result != MVT::INVALID_SIMPLE_VALUE_TYPE && 3370b57cec5SDimitry Andric "Unable to represent scalar shift amount type"); 3380b57cec5SDimitry Andric return Result; 3390b57cec5SDimitry Andric } 3400b57cec5SDimitry Andric 3410b57cec5SDimitry Andric // Lower an fp-to-int conversion operator from the LLVM opcode, which has an 3420b57cec5SDimitry Andric // undefined result on invalid/overflow, to the WebAssembly opcode, which 3430b57cec5SDimitry Andric // traps on invalid/overflow. 3440b57cec5SDimitry Andric static MachineBasicBlock *LowerFPToInt(MachineInstr &MI, DebugLoc DL, 3450b57cec5SDimitry Andric MachineBasicBlock *BB, 3460b57cec5SDimitry Andric const TargetInstrInfo &TII, 3470b57cec5SDimitry Andric bool IsUnsigned, bool Int64, 3480b57cec5SDimitry Andric bool Float64, unsigned LoweredOpcode) { 3490b57cec5SDimitry Andric MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); 3500b57cec5SDimitry Andric 3518bcb0991SDimitry Andric Register OutReg = MI.getOperand(0).getReg(); 3528bcb0991SDimitry Andric Register InReg = MI.getOperand(1).getReg(); 3530b57cec5SDimitry Andric 3540b57cec5SDimitry Andric unsigned Abs = Float64 ? WebAssembly::ABS_F64 : WebAssembly::ABS_F32; 3550b57cec5SDimitry Andric unsigned FConst = Float64 ? WebAssembly::CONST_F64 : WebAssembly::CONST_F32; 3560b57cec5SDimitry Andric unsigned LT = Float64 ? WebAssembly::LT_F64 : WebAssembly::LT_F32; 3570b57cec5SDimitry Andric unsigned GE = Float64 ? WebAssembly::GE_F64 : WebAssembly::GE_F32; 3580b57cec5SDimitry Andric unsigned IConst = Int64 ? WebAssembly::CONST_I64 : WebAssembly::CONST_I32; 3590b57cec5SDimitry Andric unsigned Eqz = WebAssembly::EQZ_I32; 3600b57cec5SDimitry Andric unsigned And = WebAssembly::AND_I32; 3610b57cec5SDimitry Andric int64_t Limit = Int64 ? INT64_MIN : INT32_MIN; 3620b57cec5SDimitry Andric int64_t Substitute = IsUnsigned ? 0 : Limit; 3630b57cec5SDimitry Andric double CmpVal = IsUnsigned ? -(double)Limit * 2.0 : -(double)Limit; 3640b57cec5SDimitry Andric auto &Context = BB->getParent()->getFunction().getContext(); 3650b57cec5SDimitry Andric Type *Ty = Float64 ? Type::getDoubleTy(Context) : Type::getFloatTy(Context); 3660b57cec5SDimitry Andric 3670b57cec5SDimitry Andric const BasicBlock *LLVMBB = BB->getBasicBlock(); 3680b57cec5SDimitry Andric MachineFunction *F = BB->getParent(); 3690b57cec5SDimitry Andric MachineBasicBlock *TrueMBB = F->CreateMachineBasicBlock(LLVMBB); 3700b57cec5SDimitry Andric MachineBasicBlock *FalseMBB = F->CreateMachineBasicBlock(LLVMBB); 3710b57cec5SDimitry Andric MachineBasicBlock *DoneMBB = F->CreateMachineBasicBlock(LLVMBB); 3720b57cec5SDimitry Andric 3730b57cec5SDimitry Andric MachineFunction::iterator It = ++BB->getIterator(); 3740b57cec5SDimitry Andric F->insert(It, FalseMBB); 3750b57cec5SDimitry Andric F->insert(It, TrueMBB); 3760b57cec5SDimitry Andric F->insert(It, DoneMBB); 3770b57cec5SDimitry Andric 3780b57cec5SDimitry Andric // Transfer the remainder of BB and its successor edges to DoneMBB. 3790b57cec5SDimitry Andric DoneMBB->splice(DoneMBB->begin(), BB, std::next(MI.getIterator()), BB->end()); 3800b57cec5SDimitry Andric DoneMBB->transferSuccessorsAndUpdatePHIs(BB); 3810b57cec5SDimitry Andric 3820b57cec5SDimitry Andric BB->addSuccessor(TrueMBB); 3830b57cec5SDimitry Andric BB->addSuccessor(FalseMBB); 3840b57cec5SDimitry Andric TrueMBB->addSuccessor(DoneMBB); 3850b57cec5SDimitry Andric FalseMBB->addSuccessor(DoneMBB); 3860b57cec5SDimitry Andric 3870b57cec5SDimitry Andric unsigned Tmp0, Tmp1, CmpReg, EqzReg, FalseReg, TrueReg; 3880b57cec5SDimitry Andric Tmp0 = MRI.createVirtualRegister(MRI.getRegClass(InReg)); 3890b57cec5SDimitry Andric Tmp1 = MRI.createVirtualRegister(MRI.getRegClass(InReg)); 3900b57cec5SDimitry Andric CmpReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass); 3910b57cec5SDimitry Andric EqzReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass); 3920b57cec5SDimitry Andric FalseReg = MRI.createVirtualRegister(MRI.getRegClass(OutReg)); 3930b57cec5SDimitry Andric TrueReg = MRI.createVirtualRegister(MRI.getRegClass(OutReg)); 3940b57cec5SDimitry Andric 3950b57cec5SDimitry Andric MI.eraseFromParent(); 3960b57cec5SDimitry Andric // For signed numbers, we can do a single comparison to determine whether 3970b57cec5SDimitry Andric // fabs(x) is within range. 3980b57cec5SDimitry Andric if (IsUnsigned) { 3990b57cec5SDimitry Andric Tmp0 = InReg; 4000b57cec5SDimitry Andric } else { 4010b57cec5SDimitry Andric BuildMI(BB, DL, TII.get(Abs), Tmp0).addReg(InReg); 4020b57cec5SDimitry Andric } 4030b57cec5SDimitry Andric BuildMI(BB, DL, TII.get(FConst), Tmp1) 4040b57cec5SDimitry Andric .addFPImm(cast<ConstantFP>(ConstantFP::get(Ty, CmpVal))); 4050b57cec5SDimitry Andric BuildMI(BB, DL, TII.get(LT), CmpReg).addReg(Tmp0).addReg(Tmp1); 4060b57cec5SDimitry Andric 4070b57cec5SDimitry Andric // For unsigned numbers, we have to do a separate comparison with zero. 4080b57cec5SDimitry Andric if (IsUnsigned) { 4090b57cec5SDimitry Andric Tmp1 = MRI.createVirtualRegister(MRI.getRegClass(InReg)); 4108bcb0991SDimitry Andric Register SecondCmpReg = 4110b57cec5SDimitry Andric MRI.createVirtualRegister(&WebAssembly::I32RegClass); 4128bcb0991SDimitry Andric Register AndReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass); 4130b57cec5SDimitry Andric BuildMI(BB, DL, TII.get(FConst), Tmp1) 4140b57cec5SDimitry Andric .addFPImm(cast<ConstantFP>(ConstantFP::get(Ty, 0.0))); 4150b57cec5SDimitry Andric BuildMI(BB, DL, TII.get(GE), SecondCmpReg).addReg(Tmp0).addReg(Tmp1); 4160b57cec5SDimitry Andric BuildMI(BB, DL, TII.get(And), AndReg).addReg(CmpReg).addReg(SecondCmpReg); 4170b57cec5SDimitry Andric CmpReg = AndReg; 4180b57cec5SDimitry Andric } 4190b57cec5SDimitry Andric 4200b57cec5SDimitry Andric BuildMI(BB, DL, TII.get(Eqz), EqzReg).addReg(CmpReg); 4210b57cec5SDimitry Andric 4220b57cec5SDimitry Andric // Create the CFG diamond to select between doing the conversion or using 4230b57cec5SDimitry Andric // the substitute value. 4240b57cec5SDimitry Andric BuildMI(BB, DL, TII.get(WebAssembly::BR_IF)).addMBB(TrueMBB).addReg(EqzReg); 4250b57cec5SDimitry Andric BuildMI(FalseMBB, DL, TII.get(LoweredOpcode), FalseReg).addReg(InReg); 4260b57cec5SDimitry Andric BuildMI(FalseMBB, DL, TII.get(WebAssembly::BR)).addMBB(DoneMBB); 4270b57cec5SDimitry Andric BuildMI(TrueMBB, DL, TII.get(IConst), TrueReg).addImm(Substitute); 4280b57cec5SDimitry Andric BuildMI(*DoneMBB, DoneMBB->begin(), DL, TII.get(TargetOpcode::PHI), OutReg) 4290b57cec5SDimitry Andric .addReg(FalseReg) 4300b57cec5SDimitry Andric .addMBB(FalseMBB) 4310b57cec5SDimitry Andric .addReg(TrueReg) 4320b57cec5SDimitry Andric .addMBB(TrueMBB); 4330b57cec5SDimitry Andric 4340b57cec5SDimitry Andric return DoneMBB; 4350b57cec5SDimitry Andric } 4360b57cec5SDimitry Andric 4370b57cec5SDimitry Andric MachineBasicBlock *WebAssemblyTargetLowering::EmitInstrWithCustomInserter( 4380b57cec5SDimitry Andric MachineInstr &MI, MachineBasicBlock *BB) const { 4390b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget->getInstrInfo(); 4400b57cec5SDimitry Andric DebugLoc DL = MI.getDebugLoc(); 4410b57cec5SDimitry Andric 4420b57cec5SDimitry Andric switch (MI.getOpcode()) { 4430b57cec5SDimitry Andric default: 4440b57cec5SDimitry Andric llvm_unreachable("Unexpected instr type to insert"); 4450b57cec5SDimitry Andric case WebAssembly::FP_TO_SINT_I32_F32: 4460b57cec5SDimitry Andric return LowerFPToInt(MI, DL, BB, TII, false, false, false, 4470b57cec5SDimitry Andric WebAssembly::I32_TRUNC_S_F32); 4480b57cec5SDimitry Andric case WebAssembly::FP_TO_UINT_I32_F32: 4490b57cec5SDimitry Andric return LowerFPToInt(MI, DL, BB, TII, true, false, false, 4500b57cec5SDimitry Andric WebAssembly::I32_TRUNC_U_F32); 4510b57cec5SDimitry Andric case WebAssembly::FP_TO_SINT_I64_F32: 4520b57cec5SDimitry Andric return LowerFPToInt(MI, DL, BB, TII, false, true, false, 4530b57cec5SDimitry Andric WebAssembly::I64_TRUNC_S_F32); 4540b57cec5SDimitry Andric case WebAssembly::FP_TO_UINT_I64_F32: 4550b57cec5SDimitry Andric return LowerFPToInt(MI, DL, BB, TII, true, true, false, 4560b57cec5SDimitry Andric WebAssembly::I64_TRUNC_U_F32); 4570b57cec5SDimitry Andric case WebAssembly::FP_TO_SINT_I32_F64: 4580b57cec5SDimitry Andric return LowerFPToInt(MI, DL, BB, TII, false, false, true, 4590b57cec5SDimitry Andric WebAssembly::I32_TRUNC_S_F64); 4600b57cec5SDimitry Andric case WebAssembly::FP_TO_UINT_I32_F64: 4610b57cec5SDimitry Andric return LowerFPToInt(MI, DL, BB, TII, true, false, true, 4620b57cec5SDimitry Andric WebAssembly::I32_TRUNC_U_F64); 4630b57cec5SDimitry Andric case WebAssembly::FP_TO_SINT_I64_F64: 4640b57cec5SDimitry Andric return LowerFPToInt(MI, DL, BB, TII, false, true, true, 4650b57cec5SDimitry Andric WebAssembly::I64_TRUNC_S_F64); 4660b57cec5SDimitry Andric case WebAssembly::FP_TO_UINT_I64_F64: 4670b57cec5SDimitry Andric return LowerFPToInt(MI, DL, BB, TII, true, true, true, 4680b57cec5SDimitry Andric WebAssembly::I64_TRUNC_U_F64); 4690b57cec5SDimitry Andric llvm_unreachable("Unexpected instruction to emit with custom inserter"); 4700b57cec5SDimitry Andric } 4710b57cec5SDimitry Andric } 4720b57cec5SDimitry Andric 4730b57cec5SDimitry Andric const char * 4740b57cec5SDimitry Andric WebAssemblyTargetLowering::getTargetNodeName(unsigned Opcode) const { 4750b57cec5SDimitry Andric switch (static_cast<WebAssemblyISD::NodeType>(Opcode)) { 4760b57cec5SDimitry Andric case WebAssemblyISD::FIRST_NUMBER: 477*480093f4SDimitry Andric case WebAssemblyISD::FIRST_MEM_OPCODE: 4780b57cec5SDimitry Andric break; 4790b57cec5SDimitry Andric #define HANDLE_NODETYPE(NODE) \ 4800b57cec5SDimitry Andric case WebAssemblyISD::NODE: \ 4810b57cec5SDimitry Andric return "WebAssemblyISD::" #NODE; 482*480093f4SDimitry Andric #define HANDLE_MEM_NODETYPE(NODE) HANDLE_NODETYPE(NODE) 4830b57cec5SDimitry Andric #include "WebAssemblyISD.def" 484*480093f4SDimitry Andric #undef HANDLE_MEM_NODETYPE 4850b57cec5SDimitry Andric #undef HANDLE_NODETYPE 4860b57cec5SDimitry Andric } 4870b57cec5SDimitry Andric return nullptr; 4880b57cec5SDimitry Andric } 4890b57cec5SDimitry Andric 4900b57cec5SDimitry Andric std::pair<unsigned, const TargetRegisterClass *> 4910b57cec5SDimitry Andric WebAssemblyTargetLowering::getRegForInlineAsmConstraint( 4920b57cec5SDimitry Andric const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const { 4930b57cec5SDimitry Andric // First, see if this is a constraint that directly corresponds to a 4940b57cec5SDimitry Andric // WebAssembly register class. 4950b57cec5SDimitry Andric if (Constraint.size() == 1) { 4960b57cec5SDimitry Andric switch (Constraint[0]) { 4970b57cec5SDimitry Andric case 'r': 4980b57cec5SDimitry Andric assert(VT != MVT::iPTR && "Pointer MVT not expected here"); 4990b57cec5SDimitry Andric if (Subtarget->hasSIMD128() && VT.isVector()) { 5000b57cec5SDimitry Andric if (VT.getSizeInBits() == 128) 5010b57cec5SDimitry Andric return std::make_pair(0U, &WebAssembly::V128RegClass); 5020b57cec5SDimitry Andric } 5030b57cec5SDimitry Andric if (VT.isInteger() && !VT.isVector()) { 5040b57cec5SDimitry Andric if (VT.getSizeInBits() <= 32) 5050b57cec5SDimitry Andric return std::make_pair(0U, &WebAssembly::I32RegClass); 5060b57cec5SDimitry Andric if (VT.getSizeInBits() <= 64) 5070b57cec5SDimitry Andric return std::make_pair(0U, &WebAssembly::I64RegClass); 5080b57cec5SDimitry Andric } 5090b57cec5SDimitry Andric break; 5100b57cec5SDimitry Andric default: 5110b57cec5SDimitry Andric break; 5120b57cec5SDimitry Andric } 5130b57cec5SDimitry Andric } 5140b57cec5SDimitry Andric 5150b57cec5SDimitry Andric return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); 5160b57cec5SDimitry Andric } 5170b57cec5SDimitry Andric 5180b57cec5SDimitry Andric bool WebAssemblyTargetLowering::isCheapToSpeculateCttz() const { 5190b57cec5SDimitry Andric // Assume ctz is a relatively cheap operation. 5200b57cec5SDimitry Andric return true; 5210b57cec5SDimitry Andric } 5220b57cec5SDimitry Andric 5230b57cec5SDimitry Andric bool WebAssemblyTargetLowering::isCheapToSpeculateCtlz() const { 5240b57cec5SDimitry Andric // Assume clz is a relatively cheap operation. 5250b57cec5SDimitry Andric return true; 5260b57cec5SDimitry Andric } 5270b57cec5SDimitry Andric 5280b57cec5SDimitry Andric bool WebAssemblyTargetLowering::isLegalAddressingMode(const DataLayout &DL, 5290b57cec5SDimitry Andric const AddrMode &AM, 5300b57cec5SDimitry Andric Type *Ty, unsigned AS, 5310b57cec5SDimitry Andric Instruction *I) const { 5320b57cec5SDimitry Andric // WebAssembly offsets are added as unsigned without wrapping. The 5330b57cec5SDimitry Andric // isLegalAddressingMode gives us no way to determine if wrapping could be 5340b57cec5SDimitry Andric // happening, so we approximate this by accepting only non-negative offsets. 5350b57cec5SDimitry Andric if (AM.BaseOffs < 0) 5360b57cec5SDimitry Andric return false; 5370b57cec5SDimitry Andric 5380b57cec5SDimitry Andric // WebAssembly has no scale register operands. 5390b57cec5SDimitry Andric if (AM.Scale != 0) 5400b57cec5SDimitry Andric return false; 5410b57cec5SDimitry Andric 5420b57cec5SDimitry Andric // Everything else is legal. 5430b57cec5SDimitry Andric return true; 5440b57cec5SDimitry Andric } 5450b57cec5SDimitry Andric 5460b57cec5SDimitry Andric bool WebAssemblyTargetLowering::allowsMisalignedMemoryAccesses( 5470b57cec5SDimitry Andric EVT /*VT*/, unsigned /*AddrSpace*/, unsigned /*Align*/, 5480b57cec5SDimitry Andric MachineMemOperand::Flags /*Flags*/, bool *Fast) const { 5490b57cec5SDimitry Andric // WebAssembly supports unaligned accesses, though it should be declared 5500b57cec5SDimitry Andric // with the p2align attribute on loads and stores which do so, and there 5510b57cec5SDimitry Andric // may be a performance impact. We tell LLVM they're "fast" because 5520b57cec5SDimitry Andric // for the kinds of things that LLVM uses this for (merging adjacent stores 5530b57cec5SDimitry Andric // of constants, etc.), WebAssembly implementations will either want the 5540b57cec5SDimitry Andric // unaligned access or they'll split anyway. 5550b57cec5SDimitry Andric if (Fast) 5560b57cec5SDimitry Andric *Fast = true; 5570b57cec5SDimitry Andric return true; 5580b57cec5SDimitry Andric } 5590b57cec5SDimitry Andric 5600b57cec5SDimitry Andric bool WebAssemblyTargetLowering::isIntDivCheap(EVT VT, 5610b57cec5SDimitry Andric AttributeList Attr) const { 5620b57cec5SDimitry Andric // The current thinking is that wasm engines will perform this optimization, 5630b57cec5SDimitry Andric // so we can save on code size. 5640b57cec5SDimitry Andric return true; 5650b57cec5SDimitry Andric } 5660b57cec5SDimitry Andric 5678bcb0991SDimitry Andric bool WebAssemblyTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const { 5688bcb0991SDimitry Andric if (!Subtarget->hasUnimplementedSIMD128()) 5698bcb0991SDimitry Andric return false; 5708bcb0991SDimitry Andric MVT ExtT = ExtVal.getSimpleValueType(); 5718bcb0991SDimitry Andric MVT MemT = cast<LoadSDNode>(ExtVal->getOperand(0))->getSimpleValueType(0); 5728bcb0991SDimitry Andric return (ExtT == MVT::v8i16 && MemT == MVT::v8i8) || 5738bcb0991SDimitry Andric (ExtT == MVT::v4i32 && MemT == MVT::v4i16) || 5748bcb0991SDimitry Andric (ExtT == MVT::v2i64 && MemT == MVT::v2i32); 5758bcb0991SDimitry Andric } 5768bcb0991SDimitry Andric 5770b57cec5SDimitry Andric EVT WebAssemblyTargetLowering::getSetCCResultType(const DataLayout &DL, 5780b57cec5SDimitry Andric LLVMContext &C, 5790b57cec5SDimitry Andric EVT VT) const { 5800b57cec5SDimitry Andric if (VT.isVector()) 5810b57cec5SDimitry Andric return VT.changeVectorElementTypeToInteger(); 5820b57cec5SDimitry Andric 5830b57cec5SDimitry Andric return TargetLowering::getSetCCResultType(DL, C, VT); 5840b57cec5SDimitry Andric } 5850b57cec5SDimitry Andric 5860b57cec5SDimitry Andric bool WebAssemblyTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info, 5870b57cec5SDimitry Andric const CallInst &I, 5880b57cec5SDimitry Andric MachineFunction &MF, 5890b57cec5SDimitry Andric unsigned Intrinsic) const { 5900b57cec5SDimitry Andric switch (Intrinsic) { 5910b57cec5SDimitry Andric case Intrinsic::wasm_atomic_notify: 5920b57cec5SDimitry Andric Info.opc = ISD::INTRINSIC_W_CHAIN; 5930b57cec5SDimitry Andric Info.memVT = MVT::i32; 5940b57cec5SDimitry Andric Info.ptrVal = I.getArgOperand(0); 5950b57cec5SDimitry Andric Info.offset = 0; 5968bcb0991SDimitry Andric Info.align = Align(4); 5970b57cec5SDimitry Andric // atomic.notify instruction does not really load the memory specified with 5980b57cec5SDimitry Andric // this argument, but MachineMemOperand should either be load or store, so 5990b57cec5SDimitry Andric // we set this to a load. 6000b57cec5SDimitry Andric // FIXME Volatile isn't really correct, but currently all LLVM atomic 6010b57cec5SDimitry Andric // instructions are treated as volatiles in the backend, so we should be 6020b57cec5SDimitry Andric // consistent. The same applies for wasm_atomic_wait intrinsics too. 6030b57cec5SDimitry Andric Info.flags = MachineMemOperand::MOVolatile | MachineMemOperand::MOLoad; 6040b57cec5SDimitry Andric return true; 6050b57cec5SDimitry Andric case Intrinsic::wasm_atomic_wait_i32: 6060b57cec5SDimitry Andric Info.opc = ISD::INTRINSIC_W_CHAIN; 6070b57cec5SDimitry Andric Info.memVT = MVT::i32; 6080b57cec5SDimitry Andric Info.ptrVal = I.getArgOperand(0); 6090b57cec5SDimitry Andric Info.offset = 0; 6108bcb0991SDimitry Andric Info.align = Align(4); 6110b57cec5SDimitry Andric Info.flags = MachineMemOperand::MOVolatile | MachineMemOperand::MOLoad; 6120b57cec5SDimitry Andric return true; 6130b57cec5SDimitry Andric case Intrinsic::wasm_atomic_wait_i64: 6140b57cec5SDimitry Andric Info.opc = ISD::INTRINSIC_W_CHAIN; 6150b57cec5SDimitry Andric Info.memVT = MVT::i64; 6160b57cec5SDimitry Andric Info.ptrVal = I.getArgOperand(0); 6170b57cec5SDimitry Andric Info.offset = 0; 6188bcb0991SDimitry Andric Info.align = Align(8); 6190b57cec5SDimitry Andric Info.flags = MachineMemOperand::MOVolatile | MachineMemOperand::MOLoad; 6200b57cec5SDimitry Andric return true; 6210b57cec5SDimitry Andric default: 6220b57cec5SDimitry Andric return false; 6230b57cec5SDimitry Andric } 6240b57cec5SDimitry Andric } 6250b57cec5SDimitry Andric 6260b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 6270b57cec5SDimitry Andric // WebAssembly Lowering private implementation. 6280b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 6290b57cec5SDimitry Andric 6300b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 6310b57cec5SDimitry Andric // Lowering Code 6320b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 6330b57cec5SDimitry Andric 6340b57cec5SDimitry Andric static void fail(const SDLoc &DL, SelectionDAG &DAG, const char *Msg) { 6350b57cec5SDimitry Andric MachineFunction &MF = DAG.getMachineFunction(); 6360b57cec5SDimitry Andric DAG.getContext()->diagnose( 6370b57cec5SDimitry Andric DiagnosticInfoUnsupported(MF.getFunction(), Msg, DL.getDebugLoc())); 6380b57cec5SDimitry Andric } 6390b57cec5SDimitry Andric 6400b57cec5SDimitry Andric // Test whether the given calling convention is supported. 6410b57cec5SDimitry Andric static bool callingConvSupported(CallingConv::ID CallConv) { 6420b57cec5SDimitry Andric // We currently support the language-independent target-independent 6430b57cec5SDimitry Andric // conventions. We don't yet have a way to annotate calls with properties like 6440b57cec5SDimitry Andric // "cold", and we don't have any call-clobbered registers, so these are mostly 6450b57cec5SDimitry Andric // all handled the same. 6460b57cec5SDimitry Andric return CallConv == CallingConv::C || CallConv == CallingConv::Fast || 6470b57cec5SDimitry Andric CallConv == CallingConv::Cold || 6480b57cec5SDimitry Andric CallConv == CallingConv::PreserveMost || 6490b57cec5SDimitry Andric CallConv == CallingConv::PreserveAll || 6508bcb0991SDimitry Andric CallConv == CallingConv::CXX_FAST_TLS || 6518bcb0991SDimitry Andric CallConv == CallingConv::WASM_EmscriptenInvoke; 6520b57cec5SDimitry Andric } 6530b57cec5SDimitry Andric 6540b57cec5SDimitry Andric SDValue 6550b57cec5SDimitry Andric WebAssemblyTargetLowering::LowerCall(CallLoweringInfo &CLI, 6560b57cec5SDimitry Andric SmallVectorImpl<SDValue> &InVals) const { 6570b57cec5SDimitry Andric SelectionDAG &DAG = CLI.DAG; 6580b57cec5SDimitry Andric SDLoc DL = CLI.DL; 6590b57cec5SDimitry Andric SDValue Chain = CLI.Chain; 6600b57cec5SDimitry Andric SDValue Callee = CLI.Callee; 6610b57cec5SDimitry Andric MachineFunction &MF = DAG.getMachineFunction(); 6620b57cec5SDimitry Andric auto Layout = MF.getDataLayout(); 6630b57cec5SDimitry Andric 6640b57cec5SDimitry Andric CallingConv::ID CallConv = CLI.CallConv; 6650b57cec5SDimitry Andric if (!callingConvSupported(CallConv)) 6660b57cec5SDimitry Andric fail(DL, DAG, 6670b57cec5SDimitry Andric "WebAssembly doesn't support language-specific or target-specific " 6680b57cec5SDimitry Andric "calling conventions yet"); 6690b57cec5SDimitry Andric if (CLI.IsPatchPoint) 6700b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly doesn't support patch point yet"); 6710b57cec5SDimitry Andric 6728bcb0991SDimitry Andric if (CLI.IsTailCall) { 6738bcb0991SDimitry Andric bool MustTail = CLI.CS && CLI.CS.isMustTailCall(); 6748bcb0991SDimitry Andric if (Subtarget->hasTailCall() && !CLI.IsVarArg) { 6758bcb0991SDimitry Andric // Do not tail call unless caller and callee return types match 6768bcb0991SDimitry Andric const Function &F = MF.getFunction(); 6778bcb0991SDimitry Andric const TargetMachine &TM = getTargetMachine(); 6788bcb0991SDimitry Andric Type *RetTy = F.getReturnType(); 6798bcb0991SDimitry Andric SmallVector<MVT, 4> CallerRetTys; 6808bcb0991SDimitry Andric SmallVector<MVT, 4> CalleeRetTys; 6818bcb0991SDimitry Andric computeLegalValueVTs(F, TM, RetTy, CallerRetTys); 6828bcb0991SDimitry Andric computeLegalValueVTs(F, TM, CLI.RetTy, CalleeRetTys); 6838bcb0991SDimitry Andric bool TypesMatch = CallerRetTys.size() == CalleeRetTys.size() && 6848bcb0991SDimitry Andric std::equal(CallerRetTys.begin(), CallerRetTys.end(), 6858bcb0991SDimitry Andric CalleeRetTys.begin()); 6868bcb0991SDimitry Andric if (!TypesMatch) { 6878bcb0991SDimitry Andric // musttail in this case would be an LLVM IR validation failure 6888bcb0991SDimitry Andric assert(!MustTail); 6890b57cec5SDimitry Andric CLI.IsTailCall = false; 6900b57cec5SDimitry Andric } 6918bcb0991SDimitry Andric } else { 6928bcb0991SDimitry Andric CLI.IsTailCall = false; 6938bcb0991SDimitry Andric if (MustTail) { 6948bcb0991SDimitry Andric if (CLI.IsVarArg) { 6958bcb0991SDimitry Andric // The return would pop the argument buffer 6968bcb0991SDimitry Andric fail(DL, DAG, "WebAssembly does not support varargs tail calls"); 6978bcb0991SDimitry Andric } else { 6988bcb0991SDimitry Andric fail(DL, DAG, "WebAssembly 'tail-call' feature not enabled"); 6998bcb0991SDimitry Andric } 7008bcb0991SDimitry Andric } 7018bcb0991SDimitry Andric } 7028bcb0991SDimitry Andric } 7030b57cec5SDimitry Andric 7040b57cec5SDimitry Andric SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; 7050b57cec5SDimitry Andric if (Ins.size() > 1) 7060b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly doesn't support more than 1 returned value yet"); 7070b57cec5SDimitry Andric 7080b57cec5SDimitry Andric SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; 7090b57cec5SDimitry Andric SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; 7108bcb0991SDimitry Andric 7118bcb0991SDimitry Andric // The generic code may have added an sret argument. If we're lowering an 7128bcb0991SDimitry Andric // invoke function, the ABI requires that the function pointer be the first 7138bcb0991SDimitry Andric // argument, so we may have to swap the arguments. 7148bcb0991SDimitry Andric if (CallConv == CallingConv::WASM_EmscriptenInvoke && Outs.size() >= 2 && 7158bcb0991SDimitry Andric Outs[0].Flags.isSRet()) { 7168bcb0991SDimitry Andric std::swap(Outs[0], Outs[1]); 7178bcb0991SDimitry Andric std::swap(OutVals[0], OutVals[1]); 7188bcb0991SDimitry Andric } 7198bcb0991SDimitry Andric 7200b57cec5SDimitry Andric unsigned NumFixedArgs = 0; 7210b57cec5SDimitry Andric for (unsigned I = 0; I < Outs.size(); ++I) { 7220b57cec5SDimitry Andric const ISD::OutputArg &Out = Outs[I]; 7230b57cec5SDimitry Andric SDValue &OutVal = OutVals[I]; 7240b57cec5SDimitry Andric if (Out.Flags.isNest()) 7250b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented nest arguments"); 7260b57cec5SDimitry Andric if (Out.Flags.isInAlloca()) 7270b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented inalloca arguments"); 7280b57cec5SDimitry Andric if (Out.Flags.isInConsecutiveRegs()) 7290b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented cons regs arguments"); 7300b57cec5SDimitry Andric if (Out.Flags.isInConsecutiveRegsLast()) 7310b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented cons regs last arguments"); 7320b57cec5SDimitry Andric if (Out.Flags.isByVal() && Out.Flags.getByValSize() != 0) { 7330b57cec5SDimitry Andric auto &MFI = MF.getFrameInfo(); 7340b57cec5SDimitry Andric int FI = MFI.CreateStackObject(Out.Flags.getByValSize(), 7350b57cec5SDimitry Andric Out.Flags.getByValAlign(), 7360b57cec5SDimitry Andric /*isSS=*/false); 7370b57cec5SDimitry Andric SDValue SizeNode = 7380b57cec5SDimitry Andric DAG.getConstant(Out.Flags.getByValSize(), DL, MVT::i32); 7390b57cec5SDimitry Andric SDValue FINode = DAG.getFrameIndex(FI, getPointerTy(Layout)); 7400b57cec5SDimitry Andric Chain = DAG.getMemcpy( 7410b57cec5SDimitry Andric Chain, DL, FINode, OutVal, SizeNode, Out.Flags.getByValAlign(), 7420b57cec5SDimitry Andric /*isVolatile*/ false, /*AlwaysInline=*/false, 7430b57cec5SDimitry Andric /*isTailCall*/ false, MachinePointerInfo(), MachinePointerInfo()); 7440b57cec5SDimitry Andric OutVal = FINode; 7450b57cec5SDimitry Andric } 7460b57cec5SDimitry Andric // Count the number of fixed args *after* legalization. 7470b57cec5SDimitry Andric NumFixedArgs += Out.IsFixed; 7480b57cec5SDimitry Andric } 7490b57cec5SDimitry Andric 7500b57cec5SDimitry Andric bool IsVarArg = CLI.IsVarArg; 7510b57cec5SDimitry Andric auto PtrVT = getPointerTy(Layout); 7520b57cec5SDimitry Andric 7530b57cec5SDimitry Andric // Analyze operands of the call, assigning locations to each operand. 7540b57cec5SDimitry Andric SmallVector<CCValAssign, 16> ArgLocs; 7550b57cec5SDimitry Andric CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext()); 7560b57cec5SDimitry Andric 7570b57cec5SDimitry Andric if (IsVarArg) { 7580b57cec5SDimitry Andric // Outgoing non-fixed arguments are placed in a buffer. First 7590b57cec5SDimitry Andric // compute their offsets and the total amount of buffer space needed. 7600b57cec5SDimitry Andric for (unsigned I = NumFixedArgs; I < Outs.size(); ++I) { 7610b57cec5SDimitry Andric const ISD::OutputArg &Out = Outs[I]; 7620b57cec5SDimitry Andric SDValue &Arg = OutVals[I]; 7630b57cec5SDimitry Andric EVT VT = Arg.getValueType(); 7640b57cec5SDimitry Andric assert(VT != MVT::iPTR && "Legalized args should be concrete"); 7650b57cec5SDimitry Andric Type *Ty = VT.getTypeForEVT(*DAG.getContext()); 7660b57cec5SDimitry Andric unsigned Align = std::max(Out.Flags.getOrigAlign(), 7670b57cec5SDimitry Andric Layout.getABITypeAlignment(Ty)); 7680b57cec5SDimitry Andric unsigned Offset = CCInfo.AllocateStack(Layout.getTypeAllocSize(Ty), 7690b57cec5SDimitry Andric Align); 7700b57cec5SDimitry Andric CCInfo.addLoc(CCValAssign::getMem(ArgLocs.size(), VT.getSimpleVT(), 7710b57cec5SDimitry Andric Offset, VT.getSimpleVT(), 7720b57cec5SDimitry Andric CCValAssign::Full)); 7730b57cec5SDimitry Andric } 7740b57cec5SDimitry Andric } 7750b57cec5SDimitry Andric 7760b57cec5SDimitry Andric unsigned NumBytes = CCInfo.getAlignedCallFrameSize(); 7770b57cec5SDimitry Andric 7780b57cec5SDimitry Andric SDValue FINode; 7790b57cec5SDimitry Andric if (IsVarArg && NumBytes) { 7800b57cec5SDimitry Andric // For non-fixed arguments, next emit stores to store the argument values 7810b57cec5SDimitry Andric // to the stack buffer at the offsets computed above. 7820b57cec5SDimitry Andric int FI = MF.getFrameInfo().CreateStackObject(NumBytes, 7830b57cec5SDimitry Andric Layout.getStackAlignment(), 7840b57cec5SDimitry Andric /*isSS=*/false); 7850b57cec5SDimitry Andric unsigned ValNo = 0; 7860b57cec5SDimitry Andric SmallVector<SDValue, 8> Chains; 7870b57cec5SDimitry Andric for (SDValue Arg : 7880b57cec5SDimitry Andric make_range(OutVals.begin() + NumFixedArgs, OutVals.end())) { 7890b57cec5SDimitry Andric assert(ArgLocs[ValNo].getValNo() == ValNo && 7900b57cec5SDimitry Andric "ArgLocs should remain in order and only hold varargs args"); 7910b57cec5SDimitry Andric unsigned Offset = ArgLocs[ValNo++].getLocMemOffset(); 7920b57cec5SDimitry Andric FINode = DAG.getFrameIndex(FI, getPointerTy(Layout)); 7930b57cec5SDimitry Andric SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, FINode, 7940b57cec5SDimitry Andric DAG.getConstant(Offset, DL, PtrVT)); 7950b57cec5SDimitry Andric Chains.push_back( 7960b57cec5SDimitry Andric DAG.getStore(Chain, DL, Arg, Add, 7970b57cec5SDimitry Andric MachinePointerInfo::getFixedStack(MF, FI, Offset), 0)); 7980b57cec5SDimitry Andric } 7990b57cec5SDimitry Andric if (!Chains.empty()) 8000b57cec5SDimitry Andric Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains); 8010b57cec5SDimitry Andric } else if (IsVarArg) { 8020b57cec5SDimitry Andric FINode = DAG.getIntPtrConstant(0, DL); 8030b57cec5SDimitry Andric } 8040b57cec5SDimitry Andric 8050b57cec5SDimitry Andric if (Callee->getOpcode() == ISD::GlobalAddress) { 8060b57cec5SDimitry Andric // If the callee is a GlobalAddress node (quite common, every direct call 8070b57cec5SDimitry Andric // is) turn it into a TargetGlobalAddress node so that LowerGlobalAddress 8080b57cec5SDimitry Andric // doesn't at MO_GOT which is not needed for direct calls. 8090b57cec5SDimitry Andric GlobalAddressSDNode* GA = cast<GlobalAddressSDNode>(Callee); 8100b57cec5SDimitry Andric Callee = DAG.getTargetGlobalAddress(GA->getGlobal(), DL, 8110b57cec5SDimitry Andric getPointerTy(DAG.getDataLayout()), 8120b57cec5SDimitry Andric GA->getOffset()); 8130b57cec5SDimitry Andric Callee = DAG.getNode(WebAssemblyISD::Wrapper, DL, 8140b57cec5SDimitry Andric getPointerTy(DAG.getDataLayout()), Callee); 8150b57cec5SDimitry Andric } 8160b57cec5SDimitry Andric 8170b57cec5SDimitry Andric // Compute the operands for the CALLn node. 8180b57cec5SDimitry Andric SmallVector<SDValue, 16> Ops; 8190b57cec5SDimitry Andric Ops.push_back(Chain); 8200b57cec5SDimitry Andric Ops.push_back(Callee); 8210b57cec5SDimitry Andric 8220b57cec5SDimitry Andric // Add all fixed arguments. Note that for non-varargs calls, NumFixedArgs 8230b57cec5SDimitry Andric // isn't reliable. 8240b57cec5SDimitry Andric Ops.append(OutVals.begin(), 8250b57cec5SDimitry Andric IsVarArg ? OutVals.begin() + NumFixedArgs : OutVals.end()); 8260b57cec5SDimitry Andric // Add a pointer to the vararg buffer. 8270b57cec5SDimitry Andric if (IsVarArg) 8280b57cec5SDimitry Andric Ops.push_back(FINode); 8290b57cec5SDimitry Andric 8300b57cec5SDimitry Andric SmallVector<EVT, 8> InTys; 8310b57cec5SDimitry Andric for (const auto &In : Ins) { 8320b57cec5SDimitry Andric assert(!In.Flags.isByVal() && "byval is not valid for return values"); 8330b57cec5SDimitry Andric assert(!In.Flags.isNest() && "nest is not valid for return values"); 8340b57cec5SDimitry Andric if (In.Flags.isInAlloca()) 8350b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented inalloca return values"); 8360b57cec5SDimitry Andric if (In.Flags.isInConsecutiveRegs()) 8370b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented cons regs return values"); 8380b57cec5SDimitry Andric if (In.Flags.isInConsecutiveRegsLast()) 8390b57cec5SDimitry Andric fail(DL, DAG, 8400b57cec5SDimitry Andric "WebAssembly hasn't implemented cons regs last return values"); 8410b57cec5SDimitry Andric // Ignore In.getOrigAlign() because all our arguments are passed in 8420b57cec5SDimitry Andric // registers. 8430b57cec5SDimitry Andric InTys.push_back(In.VT); 8440b57cec5SDimitry Andric } 8450b57cec5SDimitry Andric 8460b57cec5SDimitry Andric if (CLI.IsTailCall) { 8470b57cec5SDimitry Andric // ret_calls do not return values to the current frame 8480b57cec5SDimitry Andric SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 8490b57cec5SDimitry Andric return DAG.getNode(WebAssemblyISD::RET_CALL, DL, NodeTys, Ops); 8500b57cec5SDimitry Andric } 8510b57cec5SDimitry Andric 8520b57cec5SDimitry Andric InTys.push_back(MVT::Other); 8530b57cec5SDimitry Andric SDVTList InTyList = DAG.getVTList(InTys); 8540b57cec5SDimitry Andric SDValue Res = 8550b57cec5SDimitry Andric DAG.getNode(Ins.empty() ? WebAssemblyISD::CALL0 : WebAssemblyISD::CALL1, 8560b57cec5SDimitry Andric DL, InTyList, Ops); 8570b57cec5SDimitry Andric if (Ins.empty()) { 8580b57cec5SDimitry Andric Chain = Res; 8590b57cec5SDimitry Andric } else { 8600b57cec5SDimitry Andric InVals.push_back(Res); 8610b57cec5SDimitry Andric Chain = Res.getValue(1); 8620b57cec5SDimitry Andric } 8630b57cec5SDimitry Andric 8640b57cec5SDimitry Andric return Chain; 8650b57cec5SDimitry Andric } 8660b57cec5SDimitry Andric 8670b57cec5SDimitry Andric bool WebAssemblyTargetLowering::CanLowerReturn( 8680b57cec5SDimitry Andric CallingConv::ID /*CallConv*/, MachineFunction & /*MF*/, bool /*IsVarArg*/, 8690b57cec5SDimitry Andric const SmallVectorImpl<ISD::OutputArg> &Outs, 8700b57cec5SDimitry Andric LLVMContext & /*Context*/) const { 8718bcb0991SDimitry Andric // WebAssembly can only handle returning tuples with multivalue enabled 8728bcb0991SDimitry Andric return Subtarget->hasMultivalue() || Outs.size() <= 1; 8730b57cec5SDimitry Andric } 8740b57cec5SDimitry Andric 8750b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerReturn( 8760b57cec5SDimitry Andric SDValue Chain, CallingConv::ID CallConv, bool /*IsVarArg*/, 8770b57cec5SDimitry Andric const SmallVectorImpl<ISD::OutputArg> &Outs, 8780b57cec5SDimitry Andric const SmallVectorImpl<SDValue> &OutVals, const SDLoc &DL, 8790b57cec5SDimitry Andric SelectionDAG &DAG) const { 8808bcb0991SDimitry Andric assert((Subtarget->hasMultivalue() || Outs.size() <= 1) && 8818bcb0991SDimitry Andric "MVP WebAssembly can only return up to one value"); 8820b57cec5SDimitry Andric if (!callingConvSupported(CallConv)) 8830b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly doesn't support non-C calling conventions"); 8840b57cec5SDimitry Andric 8850b57cec5SDimitry Andric SmallVector<SDValue, 4> RetOps(1, Chain); 8860b57cec5SDimitry Andric RetOps.append(OutVals.begin(), OutVals.end()); 8870b57cec5SDimitry Andric Chain = DAG.getNode(WebAssemblyISD::RETURN, DL, MVT::Other, RetOps); 8880b57cec5SDimitry Andric 8890b57cec5SDimitry Andric // Record the number and types of the return values. 8900b57cec5SDimitry Andric for (const ISD::OutputArg &Out : Outs) { 8910b57cec5SDimitry Andric assert(!Out.Flags.isByVal() && "byval is not valid for return values"); 8920b57cec5SDimitry Andric assert(!Out.Flags.isNest() && "nest is not valid for return values"); 8930b57cec5SDimitry Andric assert(Out.IsFixed && "non-fixed return value is not valid"); 8940b57cec5SDimitry Andric if (Out.Flags.isInAlloca()) 8950b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented inalloca results"); 8960b57cec5SDimitry Andric if (Out.Flags.isInConsecutiveRegs()) 8970b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented cons regs results"); 8980b57cec5SDimitry Andric if (Out.Flags.isInConsecutiveRegsLast()) 8990b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented cons regs last results"); 9000b57cec5SDimitry Andric } 9010b57cec5SDimitry Andric 9020b57cec5SDimitry Andric return Chain; 9030b57cec5SDimitry Andric } 9040b57cec5SDimitry Andric 9050b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerFormalArguments( 9060b57cec5SDimitry Andric SDValue Chain, CallingConv::ID CallConv, bool IsVarArg, 9070b57cec5SDimitry Andric const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL, 9080b57cec5SDimitry Andric SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { 9090b57cec5SDimitry Andric if (!callingConvSupported(CallConv)) 9100b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly doesn't support non-C calling conventions"); 9110b57cec5SDimitry Andric 9120b57cec5SDimitry Andric MachineFunction &MF = DAG.getMachineFunction(); 9130b57cec5SDimitry Andric auto *MFI = MF.getInfo<WebAssemblyFunctionInfo>(); 9140b57cec5SDimitry Andric 9150b57cec5SDimitry Andric // Set up the incoming ARGUMENTS value, which serves to represent the liveness 9160b57cec5SDimitry Andric // of the incoming values before they're represented by virtual registers. 9170b57cec5SDimitry Andric MF.getRegInfo().addLiveIn(WebAssembly::ARGUMENTS); 9180b57cec5SDimitry Andric 9190b57cec5SDimitry Andric for (const ISD::InputArg &In : Ins) { 9200b57cec5SDimitry Andric if (In.Flags.isInAlloca()) 9210b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented inalloca arguments"); 9220b57cec5SDimitry Andric if (In.Flags.isNest()) 9230b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented nest arguments"); 9240b57cec5SDimitry Andric if (In.Flags.isInConsecutiveRegs()) 9250b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented cons regs arguments"); 9260b57cec5SDimitry Andric if (In.Flags.isInConsecutiveRegsLast()) 9270b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented cons regs last arguments"); 9280b57cec5SDimitry Andric // Ignore In.getOrigAlign() because all our arguments are passed in 9290b57cec5SDimitry Andric // registers. 9300b57cec5SDimitry Andric InVals.push_back(In.Used ? DAG.getNode(WebAssemblyISD::ARGUMENT, DL, In.VT, 9310b57cec5SDimitry Andric DAG.getTargetConstant(InVals.size(), 9320b57cec5SDimitry Andric DL, MVT::i32)) 9330b57cec5SDimitry Andric : DAG.getUNDEF(In.VT)); 9340b57cec5SDimitry Andric 9350b57cec5SDimitry Andric // Record the number and types of arguments. 9360b57cec5SDimitry Andric MFI->addParam(In.VT); 9370b57cec5SDimitry Andric } 9380b57cec5SDimitry Andric 9390b57cec5SDimitry Andric // Varargs are copied into a buffer allocated by the caller, and a pointer to 9400b57cec5SDimitry Andric // the buffer is passed as an argument. 9410b57cec5SDimitry Andric if (IsVarArg) { 9420b57cec5SDimitry Andric MVT PtrVT = getPointerTy(MF.getDataLayout()); 9438bcb0991SDimitry Andric Register VarargVreg = 9440b57cec5SDimitry Andric MF.getRegInfo().createVirtualRegister(getRegClassFor(PtrVT)); 9450b57cec5SDimitry Andric MFI->setVarargBufferVreg(VarargVreg); 9460b57cec5SDimitry Andric Chain = DAG.getCopyToReg( 9470b57cec5SDimitry Andric Chain, DL, VarargVreg, 9480b57cec5SDimitry Andric DAG.getNode(WebAssemblyISD::ARGUMENT, DL, PtrVT, 9490b57cec5SDimitry Andric DAG.getTargetConstant(Ins.size(), DL, MVT::i32))); 9500b57cec5SDimitry Andric MFI->addParam(PtrVT); 9510b57cec5SDimitry Andric } 9520b57cec5SDimitry Andric 9530b57cec5SDimitry Andric // Record the number and types of arguments and results. 9540b57cec5SDimitry Andric SmallVector<MVT, 4> Params; 9550b57cec5SDimitry Andric SmallVector<MVT, 4> Results; 9560b57cec5SDimitry Andric computeSignatureVTs(MF.getFunction().getFunctionType(), MF.getFunction(), 9570b57cec5SDimitry Andric DAG.getTarget(), Params, Results); 9580b57cec5SDimitry Andric for (MVT VT : Results) 9590b57cec5SDimitry Andric MFI->addResult(VT); 9600b57cec5SDimitry Andric // TODO: Use signatures in WebAssemblyMachineFunctionInfo too and unify 9610b57cec5SDimitry Andric // the param logic here with ComputeSignatureVTs 9620b57cec5SDimitry Andric assert(MFI->getParams().size() == Params.size() && 9630b57cec5SDimitry Andric std::equal(MFI->getParams().begin(), MFI->getParams().end(), 9640b57cec5SDimitry Andric Params.begin())); 9650b57cec5SDimitry Andric 9660b57cec5SDimitry Andric return Chain; 9670b57cec5SDimitry Andric } 9680b57cec5SDimitry Andric 9690b57cec5SDimitry Andric void WebAssemblyTargetLowering::ReplaceNodeResults( 9700b57cec5SDimitry Andric SDNode *N, SmallVectorImpl<SDValue> &Results, SelectionDAG &DAG) const { 9710b57cec5SDimitry Andric switch (N->getOpcode()) { 9720b57cec5SDimitry Andric case ISD::SIGN_EXTEND_INREG: 9730b57cec5SDimitry Andric // Do not add any results, signifying that N should not be custom lowered 9740b57cec5SDimitry Andric // after all. This happens because simd128 turns on custom lowering for 9750b57cec5SDimitry Andric // SIGN_EXTEND_INREG, but for non-vector sign extends the result might be an 9760b57cec5SDimitry Andric // illegal type. 9770b57cec5SDimitry Andric break; 9780b57cec5SDimitry Andric default: 9790b57cec5SDimitry Andric llvm_unreachable( 9800b57cec5SDimitry Andric "ReplaceNodeResults not implemented for this op for WebAssembly!"); 9810b57cec5SDimitry Andric } 9820b57cec5SDimitry Andric } 9830b57cec5SDimitry Andric 9840b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 9850b57cec5SDimitry Andric // Custom lowering hooks. 9860b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 9870b57cec5SDimitry Andric 9880b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerOperation(SDValue Op, 9890b57cec5SDimitry Andric SelectionDAG &DAG) const { 9900b57cec5SDimitry Andric SDLoc DL(Op); 9910b57cec5SDimitry Andric switch (Op.getOpcode()) { 9920b57cec5SDimitry Andric default: 9930b57cec5SDimitry Andric llvm_unreachable("unimplemented operation lowering"); 9940b57cec5SDimitry Andric return SDValue(); 9950b57cec5SDimitry Andric case ISD::FrameIndex: 9960b57cec5SDimitry Andric return LowerFrameIndex(Op, DAG); 9970b57cec5SDimitry Andric case ISD::GlobalAddress: 9980b57cec5SDimitry Andric return LowerGlobalAddress(Op, DAG); 9990b57cec5SDimitry Andric case ISD::ExternalSymbol: 10000b57cec5SDimitry Andric return LowerExternalSymbol(Op, DAG); 10010b57cec5SDimitry Andric case ISD::JumpTable: 10020b57cec5SDimitry Andric return LowerJumpTable(Op, DAG); 10030b57cec5SDimitry Andric case ISD::BR_JT: 10040b57cec5SDimitry Andric return LowerBR_JT(Op, DAG); 10050b57cec5SDimitry Andric case ISD::VASTART: 10060b57cec5SDimitry Andric return LowerVASTART(Op, DAG); 10070b57cec5SDimitry Andric case ISD::BlockAddress: 10080b57cec5SDimitry Andric case ISD::BRIND: 10090b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented computed gotos"); 10100b57cec5SDimitry Andric return SDValue(); 10110b57cec5SDimitry Andric case ISD::RETURNADDR: 10120b57cec5SDimitry Andric return LowerRETURNADDR(Op, DAG); 10130b57cec5SDimitry Andric case ISD::FRAMEADDR: 10140b57cec5SDimitry Andric return LowerFRAMEADDR(Op, DAG); 10150b57cec5SDimitry Andric case ISD::CopyToReg: 10160b57cec5SDimitry Andric return LowerCopyToReg(Op, DAG); 10170b57cec5SDimitry Andric case ISD::EXTRACT_VECTOR_ELT: 10180b57cec5SDimitry Andric case ISD::INSERT_VECTOR_ELT: 10190b57cec5SDimitry Andric return LowerAccessVectorElement(Op, DAG); 10200b57cec5SDimitry Andric case ISD::INTRINSIC_VOID: 10210b57cec5SDimitry Andric case ISD::INTRINSIC_WO_CHAIN: 10220b57cec5SDimitry Andric case ISD::INTRINSIC_W_CHAIN: 10230b57cec5SDimitry Andric return LowerIntrinsic(Op, DAG); 10240b57cec5SDimitry Andric case ISD::SIGN_EXTEND_INREG: 10250b57cec5SDimitry Andric return LowerSIGN_EXTEND_INREG(Op, DAG); 10260b57cec5SDimitry Andric case ISD::BUILD_VECTOR: 10270b57cec5SDimitry Andric return LowerBUILD_VECTOR(Op, DAG); 10280b57cec5SDimitry Andric case ISD::VECTOR_SHUFFLE: 10290b57cec5SDimitry Andric return LowerVECTOR_SHUFFLE(Op, DAG); 1030*480093f4SDimitry Andric case ISD::SETCC: 1031*480093f4SDimitry Andric return LowerSETCC(Op, DAG); 10320b57cec5SDimitry Andric case ISD::SHL: 10330b57cec5SDimitry Andric case ISD::SRA: 10340b57cec5SDimitry Andric case ISD::SRL: 10350b57cec5SDimitry Andric return LowerShift(Op, DAG); 10360b57cec5SDimitry Andric } 10370b57cec5SDimitry Andric } 10380b57cec5SDimitry Andric 10390b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerCopyToReg(SDValue Op, 10400b57cec5SDimitry Andric SelectionDAG &DAG) const { 10410b57cec5SDimitry Andric SDValue Src = Op.getOperand(2); 10420b57cec5SDimitry Andric if (isa<FrameIndexSDNode>(Src.getNode())) { 10430b57cec5SDimitry Andric // CopyToReg nodes don't support FrameIndex operands. Other targets select 10440b57cec5SDimitry Andric // the FI to some LEA-like instruction, but since we don't have that, we 10450b57cec5SDimitry Andric // need to insert some kind of instruction that can take an FI operand and 10460b57cec5SDimitry Andric // produces a value usable by CopyToReg (i.e. in a vreg). So insert a dummy 10470b57cec5SDimitry Andric // local.copy between Op and its FI operand. 10480b57cec5SDimitry Andric SDValue Chain = Op.getOperand(0); 10490b57cec5SDimitry Andric SDLoc DL(Op); 10500b57cec5SDimitry Andric unsigned Reg = cast<RegisterSDNode>(Op.getOperand(1))->getReg(); 10510b57cec5SDimitry Andric EVT VT = Src.getValueType(); 10520b57cec5SDimitry Andric SDValue Copy(DAG.getMachineNode(VT == MVT::i32 ? WebAssembly::COPY_I32 10530b57cec5SDimitry Andric : WebAssembly::COPY_I64, 10540b57cec5SDimitry Andric DL, VT, Src), 10550b57cec5SDimitry Andric 0); 10560b57cec5SDimitry Andric return Op.getNode()->getNumValues() == 1 10570b57cec5SDimitry Andric ? DAG.getCopyToReg(Chain, DL, Reg, Copy) 10580b57cec5SDimitry Andric : DAG.getCopyToReg(Chain, DL, Reg, Copy, 10590b57cec5SDimitry Andric Op.getNumOperands() == 4 ? Op.getOperand(3) 10600b57cec5SDimitry Andric : SDValue()); 10610b57cec5SDimitry Andric } 10620b57cec5SDimitry Andric return SDValue(); 10630b57cec5SDimitry Andric } 10640b57cec5SDimitry Andric 10650b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerFrameIndex(SDValue Op, 10660b57cec5SDimitry Andric SelectionDAG &DAG) const { 10670b57cec5SDimitry Andric int FI = cast<FrameIndexSDNode>(Op)->getIndex(); 10680b57cec5SDimitry Andric return DAG.getTargetFrameIndex(FI, Op.getValueType()); 10690b57cec5SDimitry Andric } 10700b57cec5SDimitry Andric 10710b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerRETURNADDR(SDValue Op, 10720b57cec5SDimitry Andric SelectionDAG &DAG) const { 10730b57cec5SDimitry Andric SDLoc DL(Op); 10740b57cec5SDimitry Andric 10750b57cec5SDimitry Andric if (!Subtarget->getTargetTriple().isOSEmscripten()) { 10760b57cec5SDimitry Andric fail(DL, DAG, 10770b57cec5SDimitry Andric "Non-Emscripten WebAssembly hasn't implemented " 10780b57cec5SDimitry Andric "__builtin_return_address"); 10790b57cec5SDimitry Andric return SDValue(); 10800b57cec5SDimitry Andric } 10810b57cec5SDimitry Andric 10820b57cec5SDimitry Andric if (verifyReturnAddressArgumentIsConstant(Op, DAG)) 10830b57cec5SDimitry Andric return SDValue(); 10840b57cec5SDimitry Andric 10850b57cec5SDimitry Andric unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 10868bcb0991SDimitry Andric MakeLibCallOptions CallOptions; 10870b57cec5SDimitry Andric return makeLibCall(DAG, RTLIB::RETURN_ADDRESS, Op.getValueType(), 10888bcb0991SDimitry Andric {DAG.getConstant(Depth, DL, MVT::i32)}, CallOptions, DL) 10890b57cec5SDimitry Andric .first; 10900b57cec5SDimitry Andric } 10910b57cec5SDimitry Andric 10920b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerFRAMEADDR(SDValue Op, 10930b57cec5SDimitry Andric SelectionDAG &DAG) const { 10940b57cec5SDimitry Andric // Non-zero depths are not supported by WebAssembly currently. Use the 10950b57cec5SDimitry Andric // legalizer's default expansion, which is to return 0 (what this function is 10960b57cec5SDimitry Andric // documented to do). 10970b57cec5SDimitry Andric if (Op.getConstantOperandVal(0) > 0) 10980b57cec5SDimitry Andric return SDValue(); 10990b57cec5SDimitry Andric 11000b57cec5SDimitry Andric DAG.getMachineFunction().getFrameInfo().setFrameAddressIsTaken(true); 11010b57cec5SDimitry Andric EVT VT = Op.getValueType(); 11028bcb0991SDimitry Andric Register FP = 11030b57cec5SDimitry Andric Subtarget->getRegisterInfo()->getFrameRegister(DAG.getMachineFunction()); 11040b57cec5SDimitry Andric return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), FP, VT); 11050b57cec5SDimitry Andric } 11060b57cec5SDimitry Andric 11070b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerGlobalAddress(SDValue Op, 11080b57cec5SDimitry Andric SelectionDAG &DAG) const { 11090b57cec5SDimitry Andric SDLoc DL(Op); 11100b57cec5SDimitry Andric const auto *GA = cast<GlobalAddressSDNode>(Op); 11110b57cec5SDimitry Andric EVT VT = Op.getValueType(); 11120b57cec5SDimitry Andric assert(GA->getTargetFlags() == 0 && 11130b57cec5SDimitry Andric "Unexpected target flags on generic GlobalAddressSDNode"); 11140b57cec5SDimitry Andric if (GA->getAddressSpace() != 0) 11150b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly only expects the 0 address space"); 11160b57cec5SDimitry Andric 11170b57cec5SDimitry Andric unsigned OperandFlags = 0; 11180b57cec5SDimitry Andric if (isPositionIndependent()) { 11190b57cec5SDimitry Andric const GlobalValue *GV = GA->getGlobal(); 11200b57cec5SDimitry Andric if (getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV)) { 11210b57cec5SDimitry Andric MachineFunction &MF = DAG.getMachineFunction(); 11220b57cec5SDimitry Andric MVT PtrVT = getPointerTy(MF.getDataLayout()); 11230b57cec5SDimitry Andric const char *BaseName; 11240b57cec5SDimitry Andric if (GV->getValueType()->isFunctionTy()) { 11250b57cec5SDimitry Andric BaseName = MF.createExternalSymbolName("__table_base"); 11260b57cec5SDimitry Andric OperandFlags = WebAssemblyII::MO_TABLE_BASE_REL; 11270b57cec5SDimitry Andric } 11280b57cec5SDimitry Andric else { 11290b57cec5SDimitry Andric BaseName = MF.createExternalSymbolName("__memory_base"); 11300b57cec5SDimitry Andric OperandFlags = WebAssemblyII::MO_MEMORY_BASE_REL; 11310b57cec5SDimitry Andric } 11320b57cec5SDimitry Andric SDValue BaseAddr = 11330b57cec5SDimitry Andric DAG.getNode(WebAssemblyISD::Wrapper, DL, PtrVT, 11340b57cec5SDimitry Andric DAG.getTargetExternalSymbol(BaseName, PtrVT)); 11350b57cec5SDimitry Andric 11360b57cec5SDimitry Andric SDValue SymAddr = DAG.getNode( 11370b57cec5SDimitry Andric WebAssemblyISD::WrapperPIC, DL, VT, 11380b57cec5SDimitry Andric DAG.getTargetGlobalAddress(GA->getGlobal(), DL, VT, GA->getOffset(), 11390b57cec5SDimitry Andric OperandFlags)); 11400b57cec5SDimitry Andric 11410b57cec5SDimitry Andric return DAG.getNode(ISD::ADD, DL, VT, BaseAddr, SymAddr); 11420b57cec5SDimitry Andric } else { 11430b57cec5SDimitry Andric OperandFlags = WebAssemblyII::MO_GOT; 11440b57cec5SDimitry Andric } 11450b57cec5SDimitry Andric } 11460b57cec5SDimitry Andric 11470b57cec5SDimitry Andric return DAG.getNode(WebAssemblyISD::Wrapper, DL, VT, 11480b57cec5SDimitry Andric DAG.getTargetGlobalAddress(GA->getGlobal(), DL, VT, 11490b57cec5SDimitry Andric GA->getOffset(), OperandFlags)); 11500b57cec5SDimitry Andric } 11510b57cec5SDimitry Andric 11520b57cec5SDimitry Andric SDValue 11530b57cec5SDimitry Andric WebAssemblyTargetLowering::LowerExternalSymbol(SDValue Op, 11540b57cec5SDimitry Andric SelectionDAG &DAG) const { 11550b57cec5SDimitry Andric SDLoc DL(Op); 11560b57cec5SDimitry Andric const auto *ES = cast<ExternalSymbolSDNode>(Op); 11570b57cec5SDimitry Andric EVT VT = Op.getValueType(); 11580b57cec5SDimitry Andric assert(ES->getTargetFlags() == 0 && 11590b57cec5SDimitry Andric "Unexpected target flags on generic ExternalSymbolSDNode"); 11600b57cec5SDimitry Andric return DAG.getNode(WebAssemblyISD::Wrapper, DL, VT, 11610b57cec5SDimitry Andric DAG.getTargetExternalSymbol(ES->getSymbol(), VT)); 11620b57cec5SDimitry Andric } 11630b57cec5SDimitry Andric 11640b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerJumpTable(SDValue Op, 11650b57cec5SDimitry Andric SelectionDAG &DAG) const { 11660b57cec5SDimitry Andric // There's no need for a Wrapper node because we always incorporate a jump 11670b57cec5SDimitry Andric // table operand into a BR_TABLE instruction, rather than ever 11680b57cec5SDimitry Andric // materializing it in a register. 11690b57cec5SDimitry Andric const JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); 11700b57cec5SDimitry Andric return DAG.getTargetJumpTable(JT->getIndex(), Op.getValueType(), 11710b57cec5SDimitry Andric JT->getTargetFlags()); 11720b57cec5SDimitry Andric } 11730b57cec5SDimitry Andric 11740b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerBR_JT(SDValue Op, 11750b57cec5SDimitry Andric SelectionDAG &DAG) const { 11760b57cec5SDimitry Andric SDLoc DL(Op); 11770b57cec5SDimitry Andric SDValue Chain = Op.getOperand(0); 11780b57cec5SDimitry Andric const auto *JT = cast<JumpTableSDNode>(Op.getOperand(1)); 11790b57cec5SDimitry Andric SDValue Index = Op.getOperand(2); 11800b57cec5SDimitry Andric assert(JT->getTargetFlags() == 0 && "WebAssembly doesn't set target flags"); 11810b57cec5SDimitry Andric 11820b57cec5SDimitry Andric SmallVector<SDValue, 8> Ops; 11830b57cec5SDimitry Andric Ops.push_back(Chain); 11840b57cec5SDimitry Andric Ops.push_back(Index); 11850b57cec5SDimitry Andric 11860b57cec5SDimitry Andric MachineJumpTableInfo *MJTI = DAG.getMachineFunction().getJumpTableInfo(); 11870b57cec5SDimitry Andric const auto &MBBs = MJTI->getJumpTables()[JT->getIndex()].MBBs; 11880b57cec5SDimitry Andric 11890b57cec5SDimitry Andric // Add an operand for each case. 11900b57cec5SDimitry Andric for (auto MBB : MBBs) 11910b57cec5SDimitry Andric Ops.push_back(DAG.getBasicBlock(MBB)); 11920b57cec5SDimitry Andric 11930b57cec5SDimitry Andric // TODO: For now, we just pick something arbitrary for a default case for now. 11940b57cec5SDimitry Andric // We really want to sniff out the guard and put in the real default case (and 11950b57cec5SDimitry Andric // delete the guard). 11960b57cec5SDimitry Andric Ops.push_back(DAG.getBasicBlock(MBBs[0])); 11970b57cec5SDimitry Andric 11980b57cec5SDimitry Andric return DAG.getNode(WebAssemblyISD::BR_TABLE, DL, MVT::Other, Ops); 11990b57cec5SDimitry Andric } 12000b57cec5SDimitry Andric 12010b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerVASTART(SDValue Op, 12020b57cec5SDimitry Andric SelectionDAG &DAG) const { 12030b57cec5SDimitry Andric SDLoc DL(Op); 12040b57cec5SDimitry Andric EVT PtrVT = getPointerTy(DAG.getMachineFunction().getDataLayout()); 12050b57cec5SDimitry Andric 12060b57cec5SDimitry Andric auto *MFI = DAG.getMachineFunction().getInfo<WebAssemblyFunctionInfo>(); 12070b57cec5SDimitry Andric const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 12080b57cec5SDimitry Andric 12090b57cec5SDimitry Andric SDValue ArgN = DAG.getCopyFromReg(DAG.getEntryNode(), DL, 12100b57cec5SDimitry Andric MFI->getVarargBufferVreg(), PtrVT); 12110b57cec5SDimitry Andric return DAG.getStore(Op.getOperand(0), DL, ArgN, Op.getOperand(1), 12120b57cec5SDimitry Andric MachinePointerInfo(SV), 0); 12130b57cec5SDimitry Andric } 12140b57cec5SDimitry Andric 12150b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerIntrinsic(SDValue Op, 12160b57cec5SDimitry Andric SelectionDAG &DAG) const { 12170b57cec5SDimitry Andric MachineFunction &MF = DAG.getMachineFunction(); 12180b57cec5SDimitry Andric unsigned IntNo; 12190b57cec5SDimitry Andric switch (Op.getOpcode()) { 12200b57cec5SDimitry Andric case ISD::INTRINSIC_VOID: 12210b57cec5SDimitry Andric case ISD::INTRINSIC_W_CHAIN: 12220b57cec5SDimitry Andric IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 12230b57cec5SDimitry Andric break; 12240b57cec5SDimitry Andric case ISD::INTRINSIC_WO_CHAIN: 12250b57cec5SDimitry Andric IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 12260b57cec5SDimitry Andric break; 12270b57cec5SDimitry Andric default: 12280b57cec5SDimitry Andric llvm_unreachable("Invalid intrinsic"); 12290b57cec5SDimitry Andric } 12300b57cec5SDimitry Andric SDLoc DL(Op); 12310b57cec5SDimitry Andric 12320b57cec5SDimitry Andric switch (IntNo) { 12330b57cec5SDimitry Andric default: 12340b57cec5SDimitry Andric return SDValue(); // Don't custom lower most intrinsics. 12350b57cec5SDimitry Andric 12360b57cec5SDimitry Andric case Intrinsic::wasm_lsda: { 12370b57cec5SDimitry Andric EVT VT = Op.getValueType(); 12380b57cec5SDimitry Andric const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 12390b57cec5SDimitry Andric MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout()); 12400b57cec5SDimitry Andric auto &Context = MF.getMMI().getContext(); 12410b57cec5SDimitry Andric MCSymbol *S = Context.getOrCreateSymbol(Twine("GCC_except_table") + 12420b57cec5SDimitry Andric Twine(MF.getFunctionNumber())); 12430b57cec5SDimitry Andric return DAG.getNode(WebAssemblyISD::Wrapper, DL, VT, 12440b57cec5SDimitry Andric DAG.getMCSymbol(S, PtrVT)); 12450b57cec5SDimitry Andric } 12460b57cec5SDimitry Andric 12470b57cec5SDimitry Andric case Intrinsic::wasm_throw: { 12480b57cec5SDimitry Andric // We only support C++ exceptions for now 12490b57cec5SDimitry Andric int Tag = cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue(); 12500b57cec5SDimitry Andric if (Tag != CPP_EXCEPTION) 12510b57cec5SDimitry Andric llvm_unreachable("Invalid tag!"); 12520b57cec5SDimitry Andric const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 12530b57cec5SDimitry Andric MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout()); 12540b57cec5SDimitry Andric const char *SymName = MF.createExternalSymbolName("__cpp_exception"); 12550b57cec5SDimitry Andric SDValue SymNode = DAG.getNode(WebAssemblyISD::Wrapper, DL, PtrVT, 12560b57cec5SDimitry Andric DAG.getTargetExternalSymbol(SymName, PtrVT)); 12570b57cec5SDimitry Andric return DAG.getNode(WebAssemblyISD::THROW, DL, 12580b57cec5SDimitry Andric MVT::Other, // outchain type 12590b57cec5SDimitry Andric { 12600b57cec5SDimitry Andric Op.getOperand(0), // inchain 12610b57cec5SDimitry Andric SymNode, // exception symbol 12620b57cec5SDimitry Andric Op.getOperand(3) // thrown value 12630b57cec5SDimitry Andric }); 12640b57cec5SDimitry Andric } 12650b57cec5SDimitry Andric } 12660b57cec5SDimitry Andric } 12670b57cec5SDimitry Andric 12680b57cec5SDimitry Andric SDValue 12690b57cec5SDimitry Andric WebAssemblyTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, 12700b57cec5SDimitry Andric SelectionDAG &DAG) const { 12710b57cec5SDimitry Andric SDLoc DL(Op); 12720b57cec5SDimitry Andric // If sign extension operations are disabled, allow sext_inreg only if operand 12730b57cec5SDimitry Andric // is a vector extract. SIMD does not depend on sign extension operations, but 12740b57cec5SDimitry Andric // allowing sext_inreg in this context lets us have simple patterns to select 12750b57cec5SDimitry Andric // extract_lane_s instructions. Expanding sext_inreg everywhere would be 12760b57cec5SDimitry Andric // simpler in this file, but would necessitate large and brittle patterns to 12770b57cec5SDimitry Andric // undo the expansion and select extract_lane_s instructions. 12780b57cec5SDimitry Andric assert(!Subtarget->hasSignExt() && Subtarget->hasSIMD128()); 12790b57cec5SDimitry Andric if (Op.getOperand(0).getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 12800b57cec5SDimitry Andric const SDValue &Extract = Op.getOperand(0); 12810b57cec5SDimitry Andric MVT VecT = Extract.getOperand(0).getSimpleValueType(); 12820b57cec5SDimitry Andric MVT ExtractedLaneT = static_cast<VTSDNode *>(Op.getOperand(1).getNode()) 12830b57cec5SDimitry Andric ->getVT() 12840b57cec5SDimitry Andric .getSimpleVT(); 12850b57cec5SDimitry Andric MVT ExtractedVecT = 12860b57cec5SDimitry Andric MVT::getVectorVT(ExtractedLaneT, 128 / ExtractedLaneT.getSizeInBits()); 12870b57cec5SDimitry Andric if (ExtractedVecT == VecT) 12880b57cec5SDimitry Andric return Op; 12890b57cec5SDimitry Andric // Bitcast vector to appropriate type to ensure ISel pattern coverage 12900b57cec5SDimitry Andric const SDValue &Index = Extract.getOperand(1); 12910b57cec5SDimitry Andric unsigned IndexVal = 12920b57cec5SDimitry Andric static_cast<ConstantSDNode *>(Index.getNode())->getZExtValue(); 12930b57cec5SDimitry Andric unsigned Scale = 12940b57cec5SDimitry Andric ExtractedVecT.getVectorNumElements() / VecT.getVectorNumElements(); 12950b57cec5SDimitry Andric assert(Scale > 1); 12960b57cec5SDimitry Andric SDValue NewIndex = 12970b57cec5SDimitry Andric DAG.getConstant(IndexVal * Scale, DL, Index.getValueType()); 12980b57cec5SDimitry Andric SDValue NewExtract = DAG.getNode( 12990b57cec5SDimitry Andric ISD::EXTRACT_VECTOR_ELT, DL, Extract.getValueType(), 13000b57cec5SDimitry Andric DAG.getBitcast(ExtractedVecT, Extract.getOperand(0)), NewIndex); 13010b57cec5SDimitry Andric return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, Op.getValueType(), 13020b57cec5SDimitry Andric NewExtract, Op.getOperand(1)); 13030b57cec5SDimitry Andric } 13040b57cec5SDimitry Andric // Otherwise expand 13050b57cec5SDimitry Andric return SDValue(); 13060b57cec5SDimitry Andric } 13070b57cec5SDimitry Andric 13080b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerBUILD_VECTOR(SDValue Op, 13090b57cec5SDimitry Andric SelectionDAG &DAG) const { 13100b57cec5SDimitry Andric SDLoc DL(Op); 13110b57cec5SDimitry Andric const EVT VecT = Op.getValueType(); 13120b57cec5SDimitry Andric const EVT LaneT = Op.getOperand(0).getValueType(); 13130b57cec5SDimitry Andric const size_t Lanes = Op.getNumOperands(); 13148bcb0991SDimitry Andric bool CanSwizzle = Subtarget->hasUnimplementedSIMD128() && VecT == MVT::v16i8; 13158bcb0991SDimitry Andric 13168bcb0991SDimitry Andric // BUILD_VECTORs are lowered to the instruction that initializes the highest 13178bcb0991SDimitry Andric // possible number of lanes at once followed by a sequence of replace_lane 13188bcb0991SDimitry Andric // instructions to individually initialize any remaining lanes. 13198bcb0991SDimitry Andric 13208bcb0991SDimitry Andric // TODO: Tune this. For example, lanewise swizzling is very expensive, so 13218bcb0991SDimitry Andric // swizzled lanes should be given greater weight. 13228bcb0991SDimitry Andric 13238bcb0991SDimitry Andric // TODO: Investigate building vectors by shuffling together vectors built by 13248bcb0991SDimitry Andric // separately specialized means. 13258bcb0991SDimitry Andric 13260b57cec5SDimitry Andric auto IsConstant = [](const SDValue &V) { 13270b57cec5SDimitry Andric return V.getOpcode() == ISD::Constant || V.getOpcode() == ISD::ConstantFP; 13280b57cec5SDimitry Andric }; 13290b57cec5SDimitry Andric 13308bcb0991SDimitry Andric // Returns the source vector and index vector pair if they exist. Checks for: 13318bcb0991SDimitry Andric // (extract_vector_elt 13328bcb0991SDimitry Andric // $src, 13338bcb0991SDimitry Andric // (sign_extend_inreg (extract_vector_elt $indices, $i)) 13348bcb0991SDimitry Andric // ) 13358bcb0991SDimitry Andric auto GetSwizzleSrcs = [](size_t I, const SDValue &Lane) { 13368bcb0991SDimitry Andric auto Bail = std::make_pair(SDValue(), SDValue()); 13378bcb0991SDimitry Andric if (Lane->getOpcode() != ISD::EXTRACT_VECTOR_ELT) 13388bcb0991SDimitry Andric return Bail; 13398bcb0991SDimitry Andric const SDValue &SwizzleSrc = Lane->getOperand(0); 13408bcb0991SDimitry Andric const SDValue &IndexExt = Lane->getOperand(1); 13418bcb0991SDimitry Andric if (IndexExt->getOpcode() != ISD::SIGN_EXTEND_INREG) 13428bcb0991SDimitry Andric return Bail; 13438bcb0991SDimitry Andric const SDValue &Index = IndexExt->getOperand(0); 13448bcb0991SDimitry Andric if (Index->getOpcode() != ISD::EXTRACT_VECTOR_ELT) 13458bcb0991SDimitry Andric return Bail; 13468bcb0991SDimitry Andric const SDValue &SwizzleIndices = Index->getOperand(0); 13478bcb0991SDimitry Andric if (SwizzleSrc.getValueType() != MVT::v16i8 || 13488bcb0991SDimitry Andric SwizzleIndices.getValueType() != MVT::v16i8 || 13498bcb0991SDimitry Andric Index->getOperand(1)->getOpcode() != ISD::Constant || 13508bcb0991SDimitry Andric Index->getConstantOperandVal(1) != I) 13518bcb0991SDimitry Andric return Bail; 13528bcb0991SDimitry Andric return std::make_pair(SwizzleSrc, SwizzleIndices); 13538bcb0991SDimitry Andric }; 13548bcb0991SDimitry Andric 13558bcb0991SDimitry Andric using ValueEntry = std::pair<SDValue, size_t>; 13568bcb0991SDimitry Andric SmallVector<ValueEntry, 16> SplatValueCounts; 13578bcb0991SDimitry Andric 13588bcb0991SDimitry Andric using SwizzleEntry = std::pair<std::pair<SDValue, SDValue>, size_t>; 13598bcb0991SDimitry Andric SmallVector<SwizzleEntry, 16> SwizzleCounts; 13608bcb0991SDimitry Andric 13618bcb0991SDimitry Andric auto AddCount = [](auto &Counts, const auto &Val) { 13628bcb0991SDimitry Andric auto CountIt = std::find_if(Counts.begin(), Counts.end(), 13638bcb0991SDimitry Andric [&Val](auto E) { return E.first == Val; }); 13648bcb0991SDimitry Andric if (CountIt == Counts.end()) { 13658bcb0991SDimitry Andric Counts.emplace_back(Val, 1); 13660b57cec5SDimitry Andric } else { 13670b57cec5SDimitry Andric CountIt->second++; 13680b57cec5SDimitry Andric } 13698bcb0991SDimitry Andric }; 13700b57cec5SDimitry Andric 13718bcb0991SDimitry Andric auto GetMostCommon = [](auto &Counts) { 13728bcb0991SDimitry Andric auto CommonIt = 13738bcb0991SDimitry Andric std::max_element(Counts.begin(), Counts.end(), 13748bcb0991SDimitry Andric [](auto A, auto B) { return A.second < B.second; }); 13758bcb0991SDimitry Andric assert(CommonIt != Counts.end() && "Unexpected all-undef build_vector"); 13768bcb0991SDimitry Andric return *CommonIt; 13778bcb0991SDimitry Andric }; 13788bcb0991SDimitry Andric 13798bcb0991SDimitry Andric size_t NumConstantLanes = 0; 13808bcb0991SDimitry Andric 13818bcb0991SDimitry Andric // Count eligible lanes for each type of vector creation op 13828bcb0991SDimitry Andric for (size_t I = 0; I < Lanes; ++I) { 13838bcb0991SDimitry Andric const SDValue &Lane = Op->getOperand(I); 13848bcb0991SDimitry Andric if (Lane.isUndef()) 13858bcb0991SDimitry Andric continue; 13868bcb0991SDimitry Andric 13878bcb0991SDimitry Andric AddCount(SplatValueCounts, Lane); 13888bcb0991SDimitry Andric 13898bcb0991SDimitry Andric if (IsConstant(Lane)) { 13908bcb0991SDimitry Andric NumConstantLanes++; 13918bcb0991SDimitry Andric } else if (CanSwizzle) { 13928bcb0991SDimitry Andric auto SwizzleSrcs = GetSwizzleSrcs(I, Lane); 13938bcb0991SDimitry Andric if (SwizzleSrcs.first) 13948bcb0991SDimitry Andric AddCount(SwizzleCounts, SwizzleSrcs); 13958bcb0991SDimitry Andric } 13968bcb0991SDimitry Andric } 13978bcb0991SDimitry Andric 13988bcb0991SDimitry Andric SDValue SplatValue; 13998bcb0991SDimitry Andric size_t NumSplatLanes; 14008bcb0991SDimitry Andric std::tie(SplatValue, NumSplatLanes) = GetMostCommon(SplatValueCounts); 14018bcb0991SDimitry Andric 14028bcb0991SDimitry Andric SDValue SwizzleSrc; 14038bcb0991SDimitry Andric SDValue SwizzleIndices; 14048bcb0991SDimitry Andric size_t NumSwizzleLanes = 0; 14058bcb0991SDimitry Andric if (SwizzleCounts.size()) 14068bcb0991SDimitry Andric std::forward_as_tuple(std::tie(SwizzleSrc, SwizzleIndices), 14078bcb0991SDimitry Andric NumSwizzleLanes) = GetMostCommon(SwizzleCounts); 14088bcb0991SDimitry Andric 14098bcb0991SDimitry Andric // Predicate returning true if the lane is properly initialized by the 14108bcb0991SDimitry Andric // original instruction 14118bcb0991SDimitry Andric std::function<bool(size_t, const SDValue &)> IsLaneConstructed; 14128bcb0991SDimitry Andric SDValue Result; 14130b57cec5SDimitry Andric if (Subtarget->hasUnimplementedSIMD128()) { 14148bcb0991SDimitry Andric // Prefer swizzles over vector consts over splats 14158bcb0991SDimitry Andric if (NumSwizzleLanes >= NumSplatLanes && 14168bcb0991SDimitry Andric NumSwizzleLanes >= NumConstantLanes) { 14178bcb0991SDimitry Andric Result = DAG.getNode(WebAssemblyISD::SWIZZLE, DL, VecT, SwizzleSrc, 14188bcb0991SDimitry Andric SwizzleIndices); 14198bcb0991SDimitry Andric auto Swizzled = std::make_pair(SwizzleSrc, SwizzleIndices); 14208bcb0991SDimitry Andric IsLaneConstructed = [&, Swizzled](size_t I, const SDValue &Lane) { 14218bcb0991SDimitry Andric return Swizzled == GetSwizzleSrcs(I, Lane); 14228bcb0991SDimitry Andric }; 14238bcb0991SDimitry Andric } else if (NumConstantLanes >= NumSplatLanes) { 14240b57cec5SDimitry Andric SmallVector<SDValue, 16> ConstLanes; 14250b57cec5SDimitry Andric for (const SDValue &Lane : Op->op_values()) { 14260b57cec5SDimitry Andric if (IsConstant(Lane)) { 14270b57cec5SDimitry Andric ConstLanes.push_back(Lane); 14280b57cec5SDimitry Andric } else if (LaneT.isFloatingPoint()) { 14290b57cec5SDimitry Andric ConstLanes.push_back(DAG.getConstantFP(0, DL, LaneT)); 14300b57cec5SDimitry Andric } else { 14310b57cec5SDimitry Andric ConstLanes.push_back(DAG.getConstant(0, DL, LaneT)); 14320b57cec5SDimitry Andric } 14330b57cec5SDimitry Andric } 14348bcb0991SDimitry Andric Result = DAG.getBuildVector(VecT, DL, ConstLanes); 14358bcb0991SDimitry Andric IsLaneConstructed = [&](size_t _, const SDValue &Lane) { 14368bcb0991SDimitry Andric return IsConstant(Lane); 14378bcb0991SDimitry Andric }; 14388bcb0991SDimitry Andric } 14398bcb0991SDimitry Andric } 14408bcb0991SDimitry Andric if (!Result) { 14418bcb0991SDimitry Andric // Use a splat, but possibly a load_splat 14428bcb0991SDimitry Andric LoadSDNode *SplattedLoad; 14438bcb0991SDimitry Andric if (Subtarget->hasUnimplementedSIMD128() && 14448bcb0991SDimitry Andric (SplattedLoad = dyn_cast<LoadSDNode>(SplatValue)) && 14458bcb0991SDimitry Andric SplattedLoad->getMemoryVT() == VecT.getVectorElementType()) { 1446*480093f4SDimitry Andric Result = DAG.getMemIntrinsicNode( 1447*480093f4SDimitry Andric WebAssemblyISD::LOAD_SPLAT, DL, DAG.getVTList(VecT), 1448*480093f4SDimitry Andric {SplattedLoad->getChain(), SplattedLoad->getBasePtr(), 1449*480093f4SDimitry Andric SplattedLoad->getOffset()}, 1450*480093f4SDimitry Andric SplattedLoad->getMemoryVT(), SplattedLoad->getMemOperand()); 14518bcb0991SDimitry Andric } else { 14528bcb0991SDimitry Andric Result = DAG.getSplatBuildVector(VecT, DL, SplatValue); 14538bcb0991SDimitry Andric } 14548bcb0991SDimitry Andric IsLaneConstructed = [&](size_t _, const SDValue &Lane) { 14558bcb0991SDimitry Andric return Lane == SplatValue; 14568bcb0991SDimitry Andric }; 14578bcb0991SDimitry Andric } 14588bcb0991SDimitry Andric 14598bcb0991SDimitry Andric // Add replace_lane instructions for any unhandled values 14600b57cec5SDimitry Andric for (size_t I = 0; I < Lanes; ++I) { 14610b57cec5SDimitry Andric const SDValue &Lane = Op->getOperand(I); 14628bcb0991SDimitry Andric if (!Lane.isUndef() && !IsLaneConstructed(I, Lane)) 14630b57cec5SDimitry Andric Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VecT, Result, Lane, 14640b57cec5SDimitry Andric DAG.getConstant(I, DL, MVT::i32)); 14650b57cec5SDimitry Andric } 14668bcb0991SDimitry Andric 14670b57cec5SDimitry Andric return Result; 14680b57cec5SDimitry Andric } 14690b57cec5SDimitry Andric 14700b57cec5SDimitry Andric SDValue 14710b57cec5SDimitry Andric WebAssemblyTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, 14720b57cec5SDimitry Andric SelectionDAG &DAG) const { 14730b57cec5SDimitry Andric SDLoc DL(Op); 14740b57cec5SDimitry Andric ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Op.getNode())->getMask(); 14750b57cec5SDimitry Andric MVT VecType = Op.getOperand(0).getSimpleValueType(); 14760b57cec5SDimitry Andric assert(VecType.is128BitVector() && "Unexpected shuffle vector type"); 14770b57cec5SDimitry Andric size_t LaneBytes = VecType.getVectorElementType().getSizeInBits() / 8; 14780b57cec5SDimitry Andric 14790b57cec5SDimitry Andric // Space for two vector args and sixteen mask indices 14800b57cec5SDimitry Andric SDValue Ops[18]; 14810b57cec5SDimitry Andric size_t OpIdx = 0; 14820b57cec5SDimitry Andric Ops[OpIdx++] = Op.getOperand(0); 14830b57cec5SDimitry Andric Ops[OpIdx++] = Op.getOperand(1); 14840b57cec5SDimitry Andric 14850b57cec5SDimitry Andric // Expand mask indices to byte indices and materialize them as operands 14860b57cec5SDimitry Andric for (int M : Mask) { 14870b57cec5SDimitry Andric for (size_t J = 0; J < LaneBytes; ++J) { 14880b57cec5SDimitry Andric // Lower undefs (represented by -1 in mask) to zero 14890b57cec5SDimitry Andric uint64_t ByteIndex = M == -1 ? 0 : (uint64_t)M * LaneBytes + J; 14900b57cec5SDimitry Andric Ops[OpIdx++] = DAG.getConstant(ByteIndex, DL, MVT::i32); 14910b57cec5SDimitry Andric } 14920b57cec5SDimitry Andric } 14930b57cec5SDimitry Andric 14940b57cec5SDimitry Andric return DAG.getNode(WebAssemblyISD::SHUFFLE, DL, Op.getValueType(), Ops); 14950b57cec5SDimitry Andric } 14960b57cec5SDimitry Andric 1497*480093f4SDimitry Andric SDValue WebAssemblyTargetLowering::LowerSETCC(SDValue Op, 1498*480093f4SDimitry Andric SelectionDAG &DAG) const { 1499*480093f4SDimitry Andric SDLoc DL(Op); 1500*480093f4SDimitry Andric // The legalizer does not know how to expand the comparison modes of i64x2 1501*480093f4SDimitry Andric // vectors because no comparison modes are supported. We could solve this by 1502*480093f4SDimitry Andric // expanding all i64x2 SETCC nodes, but that seems to expand f64x2 SETCC nodes 1503*480093f4SDimitry Andric // (which return i64x2 results) as well. So instead we manually unroll i64x2 1504*480093f4SDimitry Andric // comparisons here. 1505*480093f4SDimitry Andric assert(Subtarget->hasUnimplementedSIMD128()); 1506*480093f4SDimitry Andric assert(Op->getOperand(0)->getSimpleValueType(0) == MVT::v2i64); 1507*480093f4SDimitry Andric SmallVector<SDValue, 2> LHS, RHS; 1508*480093f4SDimitry Andric DAG.ExtractVectorElements(Op->getOperand(0), LHS); 1509*480093f4SDimitry Andric DAG.ExtractVectorElements(Op->getOperand(1), RHS); 1510*480093f4SDimitry Andric const SDValue &CC = Op->getOperand(2); 1511*480093f4SDimitry Andric auto MakeLane = [&](unsigned I) { 1512*480093f4SDimitry Andric return DAG.getNode(ISD::SELECT_CC, DL, MVT::i64, LHS[I], RHS[I], 1513*480093f4SDimitry Andric DAG.getConstant(uint64_t(-1), DL, MVT::i64), 1514*480093f4SDimitry Andric DAG.getConstant(uint64_t(0), DL, MVT::i64), CC); 1515*480093f4SDimitry Andric }; 1516*480093f4SDimitry Andric return DAG.getBuildVector(Op->getValueType(0), DL, 1517*480093f4SDimitry Andric {MakeLane(0), MakeLane(1)}); 1518*480093f4SDimitry Andric } 1519*480093f4SDimitry Andric 15200b57cec5SDimitry Andric SDValue 15210b57cec5SDimitry Andric WebAssemblyTargetLowering::LowerAccessVectorElement(SDValue Op, 15220b57cec5SDimitry Andric SelectionDAG &DAG) const { 15230b57cec5SDimitry Andric // Allow constant lane indices, expand variable lane indices 15240b57cec5SDimitry Andric SDNode *IdxNode = Op.getOperand(Op.getNumOperands() - 1).getNode(); 15250b57cec5SDimitry Andric if (isa<ConstantSDNode>(IdxNode) || IdxNode->isUndef()) 15260b57cec5SDimitry Andric return Op; 15270b57cec5SDimitry Andric else 15280b57cec5SDimitry Andric // Perform default expansion 15290b57cec5SDimitry Andric return SDValue(); 15300b57cec5SDimitry Andric } 15310b57cec5SDimitry Andric 15320b57cec5SDimitry Andric static SDValue unrollVectorShift(SDValue Op, SelectionDAG &DAG) { 15330b57cec5SDimitry Andric EVT LaneT = Op.getSimpleValueType().getVectorElementType(); 15340b57cec5SDimitry Andric // 32-bit and 64-bit unrolled shifts will have proper semantics 15350b57cec5SDimitry Andric if (LaneT.bitsGE(MVT::i32)) 15360b57cec5SDimitry Andric return DAG.UnrollVectorOp(Op.getNode()); 15370b57cec5SDimitry Andric // Otherwise mask the shift value to get proper semantics from 32-bit shift 15380b57cec5SDimitry Andric SDLoc DL(Op); 15390b57cec5SDimitry Andric SDValue ShiftVal = Op.getOperand(1); 15400b57cec5SDimitry Andric uint64_t MaskVal = LaneT.getSizeInBits() - 1; 15410b57cec5SDimitry Andric SDValue MaskedShiftVal = DAG.getNode( 15420b57cec5SDimitry Andric ISD::AND, // mask opcode 15430b57cec5SDimitry Andric DL, ShiftVal.getValueType(), // masked value type 15440b57cec5SDimitry Andric ShiftVal, // original shift value operand 15450b57cec5SDimitry Andric DAG.getConstant(MaskVal, DL, ShiftVal.getValueType()) // mask operand 15460b57cec5SDimitry Andric ); 15470b57cec5SDimitry Andric 15480b57cec5SDimitry Andric return DAG.UnrollVectorOp( 15490b57cec5SDimitry Andric DAG.getNode(Op.getOpcode(), // original shift opcode 15500b57cec5SDimitry Andric DL, Op.getValueType(), // original return type 15510b57cec5SDimitry Andric Op.getOperand(0), // original vector operand, 15520b57cec5SDimitry Andric MaskedShiftVal // new masked shift value operand 15530b57cec5SDimitry Andric ) 15540b57cec5SDimitry Andric .getNode()); 15550b57cec5SDimitry Andric } 15560b57cec5SDimitry Andric 15570b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerShift(SDValue Op, 15580b57cec5SDimitry Andric SelectionDAG &DAG) const { 15590b57cec5SDimitry Andric SDLoc DL(Op); 15600b57cec5SDimitry Andric 15610b57cec5SDimitry Andric // Only manually lower vector shifts 15620b57cec5SDimitry Andric assert(Op.getSimpleValueType().isVector()); 15630b57cec5SDimitry Andric 15640b57cec5SDimitry Andric // Unroll non-splat vector shifts 15650b57cec5SDimitry Andric BuildVectorSDNode *ShiftVec; 15660b57cec5SDimitry Andric SDValue SplatVal; 15670b57cec5SDimitry Andric if (!(ShiftVec = dyn_cast<BuildVectorSDNode>(Op.getOperand(1).getNode())) || 15680b57cec5SDimitry Andric !(SplatVal = ShiftVec->getSplatValue())) 15690b57cec5SDimitry Andric return unrollVectorShift(Op, DAG); 15700b57cec5SDimitry Andric 15710b57cec5SDimitry Andric // All splats except i64x2 const splats are handled by patterns 15720b57cec5SDimitry Andric auto *SplatConst = dyn_cast<ConstantSDNode>(SplatVal); 15730b57cec5SDimitry Andric if (!SplatConst || Op.getSimpleValueType() != MVT::v2i64) 15740b57cec5SDimitry Andric return Op; 15750b57cec5SDimitry Andric 15760b57cec5SDimitry Andric // i64x2 const splats are custom lowered to avoid unnecessary wraps 15770b57cec5SDimitry Andric unsigned Opcode; 15780b57cec5SDimitry Andric switch (Op.getOpcode()) { 15790b57cec5SDimitry Andric case ISD::SHL: 15800b57cec5SDimitry Andric Opcode = WebAssemblyISD::VEC_SHL; 15810b57cec5SDimitry Andric break; 15820b57cec5SDimitry Andric case ISD::SRA: 15830b57cec5SDimitry Andric Opcode = WebAssemblyISD::VEC_SHR_S; 15840b57cec5SDimitry Andric break; 15850b57cec5SDimitry Andric case ISD::SRL: 15860b57cec5SDimitry Andric Opcode = WebAssemblyISD::VEC_SHR_U; 15870b57cec5SDimitry Andric break; 15880b57cec5SDimitry Andric default: 15890b57cec5SDimitry Andric llvm_unreachable("unexpected opcode"); 15900b57cec5SDimitry Andric } 15910b57cec5SDimitry Andric APInt Shift = SplatConst->getAPIntValue().zextOrTrunc(32); 15920b57cec5SDimitry Andric return DAG.getNode(Opcode, DL, Op.getValueType(), Op.getOperand(0), 15930b57cec5SDimitry Andric DAG.getConstant(Shift, DL, MVT::i32)); 15940b57cec5SDimitry Andric } 15950b57cec5SDimitry Andric 15960b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 15970b57cec5SDimitry Andric // WebAssembly Optimization Hooks 15980b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1599