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" 16fe6060f1SDimitry Andric #include "Utils/WebAssemblyTypeUtilities.h" 17fe6060f1SDimitry Andric #include "Utils/WebAssemblyUtilities.h" 180b57cec5SDimitry Andric #include "WebAssemblyMachineFunctionInfo.h" 190b57cec5SDimitry Andric #include "WebAssemblySubtarget.h" 200b57cec5SDimitry Andric #include "WebAssemblyTargetMachine.h" 210b57cec5SDimitry Andric #include "llvm/CodeGen/CallingConvLower.h" 2281ad6265SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 2381ad6265SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineJumpTableInfo.h" 260b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h" 270b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 280b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAG.h" 29fe6060f1SDimitry Andric #include "llvm/CodeGen/SelectionDAGNodes.h" 300b57cec5SDimitry Andric #include "llvm/IR/DiagnosticInfo.h" 310b57cec5SDimitry Andric #include "llvm/IR/DiagnosticPrinter.h" 320b57cec5SDimitry Andric #include "llvm/IR/Function.h" 330b57cec5SDimitry Andric #include "llvm/IR/Intrinsics.h" 34480093f4SDimitry Andric #include "llvm/IR/IntrinsicsWebAssembly.h" 350b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 360b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 37349cc55cSDimitry Andric #include "llvm/Support/KnownBits.h" 38e8d8bef9SDimitry Andric #include "llvm/Support/MathExtras.h" 390b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 400b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h" 410b57cec5SDimitry Andric using namespace llvm; 420b57cec5SDimitry Andric 430b57cec5SDimitry Andric #define DEBUG_TYPE "wasm-lower" 440b57cec5SDimitry Andric 450b57cec5SDimitry Andric WebAssemblyTargetLowering::WebAssemblyTargetLowering( 460b57cec5SDimitry Andric const TargetMachine &TM, const WebAssemblySubtarget &STI) 470b57cec5SDimitry Andric : TargetLowering(TM), Subtarget(&STI) { 480b57cec5SDimitry Andric auto MVTPtr = Subtarget->hasAddr64() ? MVT::i64 : MVT::i32; 490b57cec5SDimitry Andric 500b57cec5SDimitry Andric // Booleans always contain 0 or 1. 510b57cec5SDimitry Andric setBooleanContents(ZeroOrOneBooleanContent); 520b57cec5SDimitry Andric // Except in SIMD vectors 530b57cec5SDimitry Andric setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); 540b57cec5SDimitry Andric // We don't know the microarchitecture here, so just reduce register pressure. 550b57cec5SDimitry Andric setSchedulingPreference(Sched::RegPressure); 560b57cec5SDimitry Andric // Tell ISel that we have a stack pointer. 570b57cec5SDimitry Andric setStackPointerRegisterToSaveRestore( 580b57cec5SDimitry Andric Subtarget->hasAddr64() ? WebAssembly::SP64 : WebAssembly::SP32); 590b57cec5SDimitry Andric // Set up the register classes. 600b57cec5SDimitry Andric addRegisterClass(MVT::i32, &WebAssembly::I32RegClass); 610b57cec5SDimitry Andric addRegisterClass(MVT::i64, &WebAssembly::I64RegClass); 620b57cec5SDimitry Andric addRegisterClass(MVT::f32, &WebAssembly::F32RegClass); 630b57cec5SDimitry Andric addRegisterClass(MVT::f64, &WebAssembly::F64RegClass); 640b57cec5SDimitry Andric if (Subtarget->hasSIMD128()) { 650b57cec5SDimitry Andric addRegisterClass(MVT::v16i8, &WebAssembly::V128RegClass); 660b57cec5SDimitry Andric addRegisterClass(MVT::v8i16, &WebAssembly::V128RegClass); 670b57cec5SDimitry Andric addRegisterClass(MVT::v4i32, &WebAssembly::V128RegClass); 680b57cec5SDimitry Andric addRegisterClass(MVT::v4f32, &WebAssembly::V128RegClass); 690b57cec5SDimitry Andric addRegisterClass(MVT::v2i64, &WebAssembly::V128RegClass); 700b57cec5SDimitry Andric addRegisterClass(MVT::v2f64, &WebAssembly::V128RegClass); 710b57cec5SDimitry Andric } 72fe6060f1SDimitry Andric if (Subtarget->hasReferenceTypes()) { 73fe6060f1SDimitry Andric addRegisterClass(MVT::externref, &WebAssembly::EXTERNREFRegClass); 74fe6060f1SDimitry Andric addRegisterClass(MVT::funcref, &WebAssembly::FUNCREFRegClass); 75fe6060f1SDimitry Andric } 760b57cec5SDimitry Andric // Compute derived properties from the register classes. 770b57cec5SDimitry Andric computeRegisterProperties(Subtarget->getRegisterInfo()); 780b57cec5SDimitry Andric 79fe6060f1SDimitry Andric // Transform loads and stores to pointers in address space 1 to loads and 80fe6060f1SDimitry Andric // stores to WebAssembly global variables, outside linear memory. 81fe6060f1SDimitry Andric for (auto T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64}) { 82fe6060f1SDimitry Andric setOperationAction(ISD::LOAD, T, Custom); 83fe6060f1SDimitry Andric setOperationAction(ISD::STORE, T, Custom); 84fe6060f1SDimitry Andric } 85fe6060f1SDimitry Andric if (Subtarget->hasSIMD128()) { 86fe6060f1SDimitry Andric for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v4f32, MVT::v2i64, 87fe6060f1SDimitry Andric MVT::v2f64}) { 88fe6060f1SDimitry Andric setOperationAction(ISD::LOAD, T, Custom); 89fe6060f1SDimitry Andric setOperationAction(ISD::STORE, T, Custom); 90fe6060f1SDimitry Andric } 91fe6060f1SDimitry Andric } 92fe6060f1SDimitry Andric if (Subtarget->hasReferenceTypes()) { 93349cc55cSDimitry Andric // We need custom load and store lowering for both externref, funcref and 94349cc55cSDimitry Andric // Other. The MVT::Other here represents tables of reference types. 95349cc55cSDimitry Andric for (auto T : {MVT::externref, MVT::funcref, MVT::Other}) { 96fe6060f1SDimitry Andric setOperationAction(ISD::LOAD, T, Custom); 97fe6060f1SDimitry Andric setOperationAction(ISD::STORE, T, Custom); 98fe6060f1SDimitry Andric } 99fe6060f1SDimitry Andric } 100fe6060f1SDimitry Andric 1010b57cec5SDimitry Andric setOperationAction(ISD::GlobalAddress, MVTPtr, Custom); 102e8d8bef9SDimitry Andric setOperationAction(ISD::GlobalTLSAddress, MVTPtr, Custom); 1030b57cec5SDimitry Andric setOperationAction(ISD::ExternalSymbol, MVTPtr, Custom); 1040b57cec5SDimitry Andric setOperationAction(ISD::JumpTable, MVTPtr, Custom); 1050b57cec5SDimitry Andric setOperationAction(ISD::BlockAddress, MVTPtr, Custom); 1060b57cec5SDimitry Andric setOperationAction(ISD::BRIND, MVT::Other, Custom); 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric // Take the default expansion for va_arg, va_copy, and va_end. There is no 1090b57cec5SDimitry Andric // default action for va_start, so we do that custom. 1100b57cec5SDimitry Andric setOperationAction(ISD::VASTART, MVT::Other, Custom); 1110b57cec5SDimitry Andric setOperationAction(ISD::VAARG, MVT::Other, Expand); 1120b57cec5SDimitry Andric setOperationAction(ISD::VACOPY, MVT::Other, Expand); 1130b57cec5SDimitry Andric setOperationAction(ISD::VAEND, MVT::Other, Expand); 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric for (auto T : {MVT::f32, MVT::f64, MVT::v4f32, MVT::v2f64}) { 1160b57cec5SDimitry Andric // Don't expand the floating-point types to constant pools. 1170b57cec5SDimitry Andric setOperationAction(ISD::ConstantFP, T, Legal); 1180b57cec5SDimitry Andric // Expand floating-point comparisons. 1190b57cec5SDimitry Andric for (auto CC : {ISD::SETO, ISD::SETUO, ISD::SETUEQ, ISD::SETONE, 1200b57cec5SDimitry Andric ISD::SETULT, ISD::SETULE, ISD::SETUGT, ISD::SETUGE}) 1210b57cec5SDimitry Andric setCondCodeAction(CC, T, Expand); 1220b57cec5SDimitry Andric // Expand floating-point library function operators. 1230b57cec5SDimitry Andric for (auto Op : 1240b57cec5SDimitry Andric {ISD::FSIN, ISD::FCOS, ISD::FSINCOS, ISD::FPOW, ISD::FREM, ISD::FMA}) 1250b57cec5SDimitry Andric setOperationAction(Op, T, Expand); 1260b57cec5SDimitry Andric // Note supported floating-point library function operators that otherwise 1270b57cec5SDimitry Andric // default to expand. 1280b57cec5SDimitry Andric for (auto Op : 1290b57cec5SDimitry Andric {ISD::FCEIL, ISD::FFLOOR, ISD::FTRUNC, ISD::FNEARBYINT, ISD::FRINT}) 1300b57cec5SDimitry Andric setOperationAction(Op, T, Legal); 1310b57cec5SDimitry Andric // Support minimum and maximum, which otherwise default to expand. 1320b57cec5SDimitry Andric setOperationAction(ISD::FMINIMUM, T, Legal); 1330b57cec5SDimitry Andric setOperationAction(ISD::FMAXIMUM, T, Legal); 1340b57cec5SDimitry Andric // WebAssembly currently has no builtin f16 support. 1350b57cec5SDimitry Andric setOperationAction(ISD::FP16_TO_FP, T, Expand); 1360b57cec5SDimitry Andric setOperationAction(ISD::FP_TO_FP16, T, Expand); 1370b57cec5SDimitry Andric setLoadExtAction(ISD::EXTLOAD, T, MVT::f16, Expand); 1380b57cec5SDimitry Andric setTruncStoreAction(T, MVT::f16, Expand); 1390b57cec5SDimitry Andric } 1400b57cec5SDimitry Andric 1410b57cec5SDimitry Andric // Expand unavailable integer operations. 1420b57cec5SDimitry Andric for (auto Op : 1430b57cec5SDimitry Andric {ISD::BSWAP, ISD::SMUL_LOHI, ISD::UMUL_LOHI, ISD::MULHS, ISD::MULHU, 1440b57cec5SDimitry Andric ISD::SDIVREM, ISD::UDIVREM, ISD::SHL_PARTS, ISD::SRA_PARTS, 1450b57cec5SDimitry Andric ISD::SRL_PARTS, ISD::ADDC, ISD::ADDE, ISD::SUBC, ISD::SUBE}) { 1460b57cec5SDimitry Andric for (auto T : {MVT::i32, MVT::i64}) 1470b57cec5SDimitry Andric setOperationAction(Op, T, Expand); 1480b57cec5SDimitry Andric if (Subtarget->hasSIMD128()) 1495ffd83dbSDimitry Andric for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v2i64}) 1500b57cec5SDimitry Andric setOperationAction(Op, T, Expand); 1510b57cec5SDimitry Andric } 1520b57cec5SDimitry Andric 153fe6060f1SDimitry Andric if (Subtarget->hasNontrappingFPToInt()) 154fe6060f1SDimitry Andric for (auto Op : {ISD::FP_TO_SINT_SAT, ISD::FP_TO_UINT_SAT}) 155fe6060f1SDimitry Andric for (auto T : {MVT::i32, MVT::i64}) 156fe6060f1SDimitry Andric setOperationAction(Op, T, Custom); 157fe6060f1SDimitry Andric 1580b57cec5SDimitry Andric // SIMD-specific configuration 1590b57cec5SDimitry Andric if (Subtarget->hasSIMD128()) { 1605ffd83dbSDimitry Andric // Hoist bitcasts out of shuffles 1615ffd83dbSDimitry Andric setTargetDAGCombine(ISD::VECTOR_SHUFFLE); 1625ffd83dbSDimitry Andric 163e8d8bef9SDimitry Andric // Combine extends of extract_subvectors into widening ops 16481ad6265SDimitry Andric setTargetDAGCombine({ISD::SIGN_EXTEND, ISD::ZERO_EXTEND}); 165e8d8bef9SDimitry Andric 166fe6060f1SDimitry Andric // Combine int_to_fp or fp_extend of extract_vectors and vice versa into 167fe6060f1SDimitry Andric // conversions ops 16881ad6265SDimitry Andric setTargetDAGCombine({ISD::SINT_TO_FP, ISD::UINT_TO_FP, ISD::FP_EXTEND, 16981ad6265SDimitry Andric ISD::EXTRACT_SUBVECTOR}); 170fe6060f1SDimitry Andric 171fe6060f1SDimitry Andric // Combine fp_to_{s,u}int_sat or fp_round of concat_vectors or vice versa 172fe6060f1SDimitry Andric // into conversion ops 17381ad6265SDimitry Andric setTargetDAGCombine({ISD::FP_TO_SINT_SAT, ISD::FP_TO_UINT_SAT, 17481ad6265SDimitry Andric ISD::FP_ROUND, ISD::CONCAT_VECTORS}); 175fe6060f1SDimitry Andric 1760eae32dcSDimitry Andric setTargetDAGCombine(ISD::TRUNCATE); 1770eae32dcSDimitry Andric 1780b57cec5SDimitry Andric // Support saturating add for i8x16 and i16x8 1790b57cec5SDimitry Andric for (auto Op : {ISD::SADDSAT, ISD::UADDSAT}) 1800b57cec5SDimitry Andric for (auto T : {MVT::v16i8, MVT::v8i16}) 1810b57cec5SDimitry Andric setOperationAction(Op, T, Legal); 1820b57cec5SDimitry Andric 1835ffd83dbSDimitry Andric // Support integer abs 184fe6060f1SDimitry Andric for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v2i64}) 1855ffd83dbSDimitry Andric setOperationAction(ISD::ABS, T, Legal); 1865ffd83dbSDimitry Andric 1870b57cec5SDimitry Andric // Custom lower BUILD_VECTORs to minimize number of replace_lanes 1885ffd83dbSDimitry Andric for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v4f32, MVT::v2i64, 1895ffd83dbSDimitry Andric MVT::v2f64}) 1900b57cec5SDimitry Andric setOperationAction(ISD::BUILD_VECTOR, T, Custom); 1910b57cec5SDimitry Andric 1920b57cec5SDimitry Andric // We have custom shuffle lowering to expose the shuffle mask 1935ffd83dbSDimitry Andric for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v4f32, MVT::v2i64, 1945ffd83dbSDimitry Andric MVT::v2f64}) 1950b57cec5SDimitry Andric setOperationAction(ISD::VECTOR_SHUFFLE, T, Custom); 1960b57cec5SDimitry Andric 197*bdd1243dSDimitry Andric // Support splatting 198*bdd1243dSDimitry Andric for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v4f32, MVT::v2i64, 199*bdd1243dSDimitry Andric MVT::v2f64}) 200*bdd1243dSDimitry Andric setOperationAction(ISD::SPLAT_VECTOR, T, Legal); 201*bdd1243dSDimitry Andric 2020b57cec5SDimitry Andric // Custom lowering since wasm shifts must have a scalar shift amount 2035ffd83dbSDimitry Andric for (auto Op : {ISD::SHL, ISD::SRA, ISD::SRL}) 2045ffd83dbSDimitry Andric for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v2i64}) 2050b57cec5SDimitry Andric setOperationAction(Op, T, Custom); 2060b57cec5SDimitry Andric 2070b57cec5SDimitry Andric // Custom lower lane accesses to expand out variable indices 2085ffd83dbSDimitry Andric for (auto Op : {ISD::EXTRACT_VECTOR_ELT, ISD::INSERT_VECTOR_ELT}) 2095ffd83dbSDimitry Andric for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v4f32, MVT::v2i64, 2105ffd83dbSDimitry Andric MVT::v2f64}) 2110b57cec5SDimitry Andric setOperationAction(Op, T, Custom); 2120b57cec5SDimitry Andric 2135ffd83dbSDimitry Andric // There is no i8x16.mul instruction 2145ffd83dbSDimitry Andric setOperationAction(ISD::MUL, MVT::v16i8, Expand); 2150b57cec5SDimitry Andric 216e8d8bef9SDimitry Andric // There is no vector conditional select instruction 2175ffd83dbSDimitry Andric for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v4f32, MVT::v2i64, 2185ffd83dbSDimitry Andric MVT::v2f64}) 219e8d8bef9SDimitry Andric setOperationAction(ISD::SELECT_CC, T, Expand); 2200b57cec5SDimitry Andric 2210b57cec5SDimitry Andric // Expand integer operations supported for scalars but not SIMD 222349cc55cSDimitry Andric for (auto Op : 223349cc55cSDimitry Andric {ISD::SDIV, ISD::UDIV, ISD::SREM, ISD::UREM, ISD::ROTL, ISD::ROTR}) 2245ffd83dbSDimitry Andric for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v2i64}) 2250b57cec5SDimitry Andric setOperationAction(Op, T, Expand); 2260b57cec5SDimitry Andric 227480093f4SDimitry Andric // But we do have integer min and max operations 228480093f4SDimitry Andric for (auto Op : {ISD::SMIN, ISD::SMAX, ISD::UMIN, ISD::UMAX}) 229480093f4SDimitry Andric for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32}) 230480093f4SDimitry Andric setOperationAction(Op, T, Legal); 231480093f4SDimitry Andric 232349cc55cSDimitry Andric // And we have popcnt for i8x16. It can be used to expand ctlz/cttz. 233fe6060f1SDimitry Andric setOperationAction(ISD::CTPOP, MVT::v16i8, Legal); 234349cc55cSDimitry Andric setOperationAction(ISD::CTLZ, MVT::v16i8, Expand); 235349cc55cSDimitry Andric setOperationAction(ISD::CTTZ, MVT::v16i8, Expand); 236349cc55cSDimitry Andric 237349cc55cSDimitry Andric // Custom lower bit counting operations for other types to scalarize them. 238349cc55cSDimitry Andric for (auto Op : {ISD::CTLZ, ISD::CTTZ, ISD::CTPOP}) 239349cc55cSDimitry Andric for (auto T : {MVT::v8i16, MVT::v4i32, MVT::v2i64}) 240349cc55cSDimitry Andric setOperationAction(Op, T, Custom); 241fe6060f1SDimitry Andric 2420b57cec5SDimitry Andric // Expand float operations supported for scalars but not SIMD 243fe6060f1SDimitry Andric for (auto Op : {ISD::FCOPYSIGN, ISD::FLOG, ISD::FLOG2, ISD::FLOG10, 2445ffd83dbSDimitry Andric ISD::FEXP, ISD::FEXP2, ISD::FRINT}) 2455ffd83dbSDimitry Andric for (auto T : {MVT::v4f32, MVT::v2f64}) 2465ffd83dbSDimitry Andric setOperationAction(Op, T, Expand); 2470b57cec5SDimitry Andric 248fe6060f1SDimitry Andric // Unsigned comparison operations are unavailable for i64x2 vectors. 249fe6060f1SDimitry Andric for (auto CC : {ISD::SETUGT, ISD::SETUGE, ISD::SETULT, ISD::SETULE}) 250fe6060f1SDimitry Andric setCondCodeAction(CC, MVT::v2i64, Custom); 251480093f4SDimitry Andric 2525ffd83dbSDimitry Andric // 64x2 conversions are not in the spec 2535ffd83dbSDimitry Andric for (auto Op : 2545ffd83dbSDimitry Andric {ISD::SINT_TO_FP, ISD::UINT_TO_FP, ISD::FP_TO_SINT, ISD::FP_TO_UINT}) 2555ffd83dbSDimitry Andric for (auto T : {MVT::v2i64, MVT::v2f64}) 2565ffd83dbSDimitry Andric setOperationAction(Op, T, Expand); 257fe6060f1SDimitry Andric 258fe6060f1SDimitry Andric // But saturating fp_to_int converstions are 259fe6060f1SDimitry Andric for (auto Op : {ISD::FP_TO_SINT_SAT, ISD::FP_TO_UINT_SAT}) 260fe6060f1SDimitry Andric setOperationAction(Op, MVT::v4i32, Custom); 2610b57cec5SDimitry Andric } 2620b57cec5SDimitry Andric 2630b57cec5SDimitry Andric // As a special case, these operators use the type to mean the type to 2640b57cec5SDimitry Andric // sign-extend from. 2650b57cec5SDimitry Andric setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); 2660b57cec5SDimitry Andric if (!Subtarget->hasSignExt()) { 2670b57cec5SDimitry Andric // Sign extends are legal only when extending a vector extract 2680b57cec5SDimitry Andric auto Action = Subtarget->hasSIMD128() ? Custom : Expand; 2690b57cec5SDimitry Andric for (auto T : {MVT::i8, MVT::i16, MVT::i32}) 2700b57cec5SDimitry Andric setOperationAction(ISD::SIGN_EXTEND_INREG, T, Action); 2710b57cec5SDimitry Andric } 2728bcb0991SDimitry Andric for (auto T : MVT::integer_fixedlen_vector_valuetypes()) 2730b57cec5SDimitry Andric setOperationAction(ISD::SIGN_EXTEND_INREG, T, Expand); 2740b57cec5SDimitry Andric 2750b57cec5SDimitry Andric // Dynamic stack allocation: use the default expansion. 2760b57cec5SDimitry Andric setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); 2770b57cec5SDimitry Andric setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); 2780b57cec5SDimitry Andric setOperationAction(ISD::DYNAMIC_STACKALLOC, MVTPtr, Expand); 2790b57cec5SDimitry Andric 2800b57cec5SDimitry Andric setOperationAction(ISD::FrameIndex, MVT::i32, Custom); 2815ffd83dbSDimitry Andric setOperationAction(ISD::FrameIndex, MVT::i64, Custom); 2820b57cec5SDimitry Andric setOperationAction(ISD::CopyToReg, MVT::Other, Custom); 2830b57cec5SDimitry Andric 2840b57cec5SDimitry Andric // Expand these forms; we pattern-match the forms that we can handle in isel. 2850b57cec5SDimitry Andric for (auto T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64}) 2860b57cec5SDimitry Andric for (auto Op : {ISD::BR_CC, ISD::SELECT_CC}) 2870b57cec5SDimitry Andric setOperationAction(Op, T, Expand); 2880b57cec5SDimitry Andric 2890b57cec5SDimitry Andric // We have custom switch handling. 2900b57cec5SDimitry Andric setOperationAction(ISD::BR_JT, MVT::Other, Custom); 2910b57cec5SDimitry Andric 2920b57cec5SDimitry Andric // WebAssembly doesn't have: 2930b57cec5SDimitry Andric // - Floating-point extending loads. 2940b57cec5SDimitry Andric // - Floating-point truncating stores. 2950b57cec5SDimitry Andric // - i1 extending loads. 2968bcb0991SDimitry Andric // - truncating SIMD stores and most extending loads 2970b57cec5SDimitry Andric setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand); 2980b57cec5SDimitry Andric setTruncStoreAction(MVT::f64, MVT::f32, Expand); 2990b57cec5SDimitry Andric for (auto T : MVT::integer_valuetypes()) 3000b57cec5SDimitry Andric for (auto Ext : {ISD::EXTLOAD, ISD::ZEXTLOAD, ISD::SEXTLOAD}) 3010b57cec5SDimitry Andric setLoadExtAction(Ext, T, MVT::i1, Promote); 3020b57cec5SDimitry Andric if (Subtarget->hasSIMD128()) { 3030b57cec5SDimitry Andric for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v2i64, MVT::v4f32, 3040b57cec5SDimitry Andric MVT::v2f64}) { 3058bcb0991SDimitry Andric for (auto MemT : MVT::fixedlen_vector_valuetypes()) { 3060b57cec5SDimitry Andric if (MVT(T) != MemT) { 3070b57cec5SDimitry Andric setTruncStoreAction(T, MemT, Expand); 3080b57cec5SDimitry Andric for (auto Ext : {ISD::EXTLOAD, ISD::ZEXTLOAD, ISD::SEXTLOAD}) 3090b57cec5SDimitry Andric setLoadExtAction(Ext, T, MemT, Expand); 3100b57cec5SDimitry Andric } 3110b57cec5SDimitry Andric } 3120b57cec5SDimitry Andric } 3138bcb0991SDimitry Andric // But some vector extending loads are legal 3148bcb0991SDimitry Andric for (auto Ext : {ISD::EXTLOAD, ISD::SEXTLOAD, ISD::ZEXTLOAD}) { 3158bcb0991SDimitry Andric setLoadExtAction(Ext, MVT::v8i16, MVT::v8i8, Legal); 3168bcb0991SDimitry Andric setLoadExtAction(Ext, MVT::v4i32, MVT::v4i16, Legal); 3178bcb0991SDimitry Andric setLoadExtAction(Ext, MVT::v2i64, MVT::v2i32, Legal); 3188bcb0991SDimitry Andric } 319349cc55cSDimitry Andric setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f32, Legal); 3208bcb0991SDimitry Andric } 3210b57cec5SDimitry Andric 3220b57cec5SDimitry Andric // Don't do anything clever with build_pairs 3230b57cec5SDimitry Andric setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand); 3240b57cec5SDimitry Andric 3250b57cec5SDimitry Andric // Trap lowers to wasm unreachable 3260b57cec5SDimitry Andric setOperationAction(ISD::TRAP, MVT::Other, Legal); 3275ffd83dbSDimitry Andric setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal); 3280b57cec5SDimitry Andric 3290b57cec5SDimitry Andric // Exception handling intrinsics 3300b57cec5SDimitry Andric setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); 331e8d8bef9SDimitry Andric setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom); 3320b57cec5SDimitry Andric setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom); 3330b57cec5SDimitry Andric 3340b57cec5SDimitry Andric setMaxAtomicSizeInBitsSupported(64); 3350b57cec5SDimitry Andric 3360b57cec5SDimitry Andric // Override the __gnu_f2h_ieee/__gnu_h2f_ieee names so that the f32 name is 3370b57cec5SDimitry Andric // consistent with the f64 and f128 names. 3380b57cec5SDimitry Andric setLibcallName(RTLIB::FPEXT_F16_F32, "__extendhfsf2"); 3390b57cec5SDimitry Andric setLibcallName(RTLIB::FPROUND_F32_F16, "__truncsfhf2"); 3400b57cec5SDimitry Andric 3410b57cec5SDimitry Andric // Define the emscripten name for return address helper. 342e8d8bef9SDimitry Andric // TODO: when implementing other Wasm backends, make this generic or only do 3430b57cec5SDimitry Andric // this on emscripten depending on what they end up doing. 3440b57cec5SDimitry Andric setLibcallName(RTLIB::RETURN_ADDRESS, "emscripten_return_address"); 3450b57cec5SDimitry Andric 3460b57cec5SDimitry Andric // Always convert switches to br_tables unless there is only one case, which 3470b57cec5SDimitry Andric // is equivalent to a simple branch. This reduces code size for wasm, and we 3480b57cec5SDimitry Andric // defer possible jump table optimizations to the VM. 3490b57cec5SDimitry Andric setMinimumJumpTableEntries(2); 3500b57cec5SDimitry Andric } 3510b57cec5SDimitry Andric 352349cc55cSDimitry Andric MVT WebAssemblyTargetLowering::getPointerTy(const DataLayout &DL, 353349cc55cSDimitry Andric uint32_t AS) const { 354349cc55cSDimitry Andric if (AS == WebAssembly::WasmAddressSpace::WASM_ADDRESS_SPACE_EXTERNREF) 355349cc55cSDimitry Andric return MVT::externref; 356349cc55cSDimitry Andric if (AS == WebAssembly::WasmAddressSpace::WASM_ADDRESS_SPACE_FUNCREF) 357349cc55cSDimitry Andric return MVT::funcref; 358349cc55cSDimitry Andric return TargetLowering::getPointerTy(DL, AS); 359349cc55cSDimitry Andric } 360349cc55cSDimitry Andric 361349cc55cSDimitry Andric MVT WebAssemblyTargetLowering::getPointerMemTy(const DataLayout &DL, 362349cc55cSDimitry Andric uint32_t AS) const { 363349cc55cSDimitry Andric if (AS == WebAssembly::WasmAddressSpace::WASM_ADDRESS_SPACE_EXTERNREF) 364349cc55cSDimitry Andric return MVT::externref; 365349cc55cSDimitry Andric if (AS == WebAssembly::WasmAddressSpace::WASM_ADDRESS_SPACE_FUNCREF) 366349cc55cSDimitry Andric return MVT::funcref; 367349cc55cSDimitry Andric return TargetLowering::getPointerMemTy(DL, AS); 368349cc55cSDimitry Andric } 369349cc55cSDimitry Andric 3700b57cec5SDimitry Andric TargetLowering::AtomicExpansionKind 3710b57cec5SDimitry Andric WebAssemblyTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const { 3720b57cec5SDimitry Andric // We have wasm instructions for these 3730b57cec5SDimitry Andric switch (AI->getOperation()) { 3740b57cec5SDimitry Andric case AtomicRMWInst::Add: 3750b57cec5SDimitry Andric case AtomicRMWInst::Sub: 3760b57cec5SDimitry Andric case AtomicRMWInst::And: 3770b57cec5SDimitry Andric case AtomicRMWInst::Or: 3780b57cec5SDimitry Andric case AtomicRMWInst::Xor: 3790b57cec5SDimitry Andric case AtomicRMWInst::Xchg: 3800b57cec5SDimitry Andric return AtomicExpansionKind::None; 3810b57cec5SDimitry Andric default: 3820b57cec5SDimitry Andric break; 3830b57cec5SDimitry Andric } 3840b57cec5SDimitry Andric return AtomicExpansionKind::CmpXChg; 3850b57cec5SDimitry Andric } 3860b57cec5SDimitry Andric 387fe6060f1SDimitry Andric bool WebAssemblyTargetLowering::shouldScalarizeBinop(SDValue VecOp) const { 388fe6060f1SDimitry Andric // Implementation copied from X86TargetLowering. 389fe6060f1SDimitry Andric unsigned Opc = VecOp.getOpcode(); 390fe6060f1SDimitry Andric 391fe6060f1SDimitry Andric // Assume target opcodes can't be scalarized. 392fe6060f1SDimitry Andric // TODO - do we have any exceptions? 393fe6060f1SDimitry Andric if (Opc >= ISD::BUILTIN_OP_END) 394fe6060f1SDimitry Andric return false; 395fe6060f1SDimitry Andric 396fe6060f1SDimitry Andric // If the vector op is not supported, try to convert to scalar. 397fe6060f1SDimitry Andric EVT VecVT = VecOp.getValueType(); 398fe6060f1SDimitry Andric if (!isOperationLegalOrCustomOrPromote(Opc, VecVT)) 399fe6060f1SDimitry Andric return true; 400fe6060f1SDimitry Andric 401fe6060f1SDimitry Andric // If the vector op is supported, but the scalar op is not, the transform may 402fe6060f1SDimitry Andric // not be worthwhile. 403fe6060f1SDimitry Andric EVT ScalarVT = VecVT.getScalarType(); 404fe6060f1SDimitry Andric return isOperationLegalOrCustomOrPromote(Opc, ScalarVT); 405fe6060f1SDimitry Andric } 406fe6060f1SDimitry Andric 4070b57cec5SDimitry Andric FastISel *WebAssemblyTargetLowering::createFastISel( 4080b57cec5SDimitry Andric FunctionLoweringInfo &FuncInfo, const TargetLibraryInfo *LibInfo) const { 4090b57cec5SDimitry Andric return WebAssembly::createFastISel(FuncInfo, LibInfo); 4100b57cec5SDimitry Andric } 4110b57cec5SDimitry Andric 4120b57cec5SDimitry Andric MVT WebAssemblyTargetLowering::getScalarShiftAmountTy(const DataLayout & /*DL*/, 4130b57cec5SDimitry Andric EVT VT) const { 4140b57cec5SDimitry Andric unsigned BitWidth = NextPowerOf2(VT.getSizeInBits() - 1); 4150b57cec5SDimitry Andric if (BitWidth > 1 && BitWidth < 8) 4160b57cec5SDimitry Andric BitWidth = 8; 4170b57cec5SDimitry Andric 4180b57cec5SDimitry Andric if (BitWidth > 64) { 4190b57cec5SDimitry Andric // The shift will be lowered to a libcall, and compiler-rt libcalls expect 4200b57cec5SDimitry Andric // the count to be an i32. 4210b57cec5SDimitry Andric BitWidth = 32; 4220b57cec5SDimitry Andric assert(BitWidth >= Log2_32_Ceil(VT.getSizeInBits()) && 4230b57cec5SDimitry Andric "32-bit shift counts ought to be enough for anyone"); 4240b57cec5SDimitry Andric } 4250b57cec5SDimitry Andric 4260b57cec5SDimitry Andric MVT Result = MVT::getIntegerVT(BitWidth); 4270b57cec5SDimitry Andric assert(Result != MVT::INVALID_SIMPLE_VALUE_TYPE && 4280b57cec5SDimitry Andric "Unable to represent scalar shift amount type"); 4290b57cec5SDimitry Andric return Result; 4300b57cec5SDimitry Andric } 4310b57cec5SDimitry Andric 4320b57cec5SDimitry Andric // Lower an fp-to-int conversion operator from the LLVM opcode, which has an 4330b57cec5SDimitry Andric // undefined result on invalid/overflow, to the WebAssembly opcode, which 4340b57cec5SDimitry Andric // traps on invalid/overflow. 4350b57cec5SDimitry Andric static MachineBasicBlock *LowerFPToInt(MachineInstr &MI, DebugLoc DL, 4360b57cec5SDimitry Andric MachineBasicBlock *BB, 4370b57cec5SDimitry Andric const TargetInstrInfo &TII, 4380b57cec5SDimitry Andric bool IsUnsigned, bool Int64, 4390b57cec5SDimitry Andric bool Float64, unsigned LoweredOpcode) { 4400b57cec5SDimitry Andric MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); 4410b57cec5SDimitry Andric 4428bcb0991SDimitry Andric Register OutReg = MI.getOperand(0).getReg(); 4438bcb0991SDimitry Andric Register InReg = MI.getOperand(1).getReg(); 4440b57cec5SDimitry Andric 4450b57cec5SDimitry Andric unsigned Abs = Float64 ? WebAssembly::ABS_F64 : WebAssembly::ABS_F32; 4460b57cec5SDimitry Andric unsigned FConst = Float64 ? WebAssembly::CONST_F64 : WebAssembly::CONST_F32; 4470b57cec5SDimitry Andric unsigned LT = Float64 ? WebAssembly::LT_F64 : WebAssembly::LT_F32; 4480b57cec5SDimitry Andric unsigned GE = Float64 ? WebAssembly::GE_F64 : WebAssembly::GE_F32; 4490b57cec5SDimitry Andric unsigned IConst = Int64 ? WebAssembly::CONST_I64 : WebAssembly::CONST_I32; 4500b57cec5SDimitry Andric unsigned Eqz = WebAssembly::EQZ_I32; 4510b57cec5SDimitry Andric unsigned And = WebAssembly::AND_I32; 4520b57cec5SDimitry Andric int64_t Limit = Int64 ? INT64_MIN : INT32_MIN; 4530b57cec5SDimitry Andric int64_t Substitute = IsUnsigned ? 0 : Limit; 4540b57cec5SDimitry Andric double CmpVal = IsUnsigned ? -(double)Limit * 2.0 : -(double)Limit; 4550b57cec5SDimitry Andric auto &Context = BB->getParent()->getFunction().getContext(); 4560b57cec5SDimitry Andric Type *Ty = Float64 ? Type::getDoubleTy(Context) : Type::getFloatTy(Context); 4570b57cec5SDimitry Andric 4580b57cec5SDimitry Andric const BasicBlock *LLVMBB = BB->getBasicBlock(); 4590b57cec5SDimitry Andric MachineFunction *F = BB->getParent(); 4600b57cec5SDimitry Andric MachineBasicBlock *TrueMBB = F->CreateMachineBasicBlock(LLVMBB); 4610b57cec5SDimitry Andric MachineBasicBlock *FalseMBB = F->CreateMachineBasicBlock(LLVMBB); 4620b57cec5SDimitry Andric MachineBasicBlock *DoneMBB = F->CreateMachineBasicBlock(LLVMBB); 4630b57cec5SDimitry Andric 4640b57cec5SDimitry Andric MachineFunction::iterator It = ++BB->getIterator(); 4650b57cec5SDimitry Andric F->insert(It, FalseMBB); 4660b57cec5SDimitry Andric F->insert(It, TrueMBB); 4670b57cec5SDimitry Andric F->insert(It, DoneMBB); 4680b57cec5SDimitry Andric 4690b57cec5SDimitry Andric // Transfer the remainder of BB and its successor edges to DoneMBB. 4700b57cec5SDimitry Andric DoneMBB->splice(DoneMBB->begin(), BB, std::next(MI.getIterator()), BB->end()); 4710b57cec5SDimitry Andric DoneMBB->transferSuccessorsAndUpdatePHIs(BB); 4720b57cec5SDimitry Andric 4730b57cec5SDimitry Andric BB->addSuccessor(TrueMBB); 4740b57cec5SDimitry Andric BB->addSuccessor(FalseMBB); 4750b57cec5SDimitry Andric TrueMBB->addSuccessor(DoneMBB); 4760b57cec5SDimitry Andric FalseMBB->addSuccessor(DoneMBB); 4770b57cec5SDimitry Andric 4780b57cec5SDimitry Andric unsigned Tmp0, Tmp1, CmpReg, EqzReg, FalseReg, TrueReg; 4790b57cec5SDimitry Andric Tmp0 = MRI.createVirtualRegister(MRI.getRegClass(InReg)); 4800b57cec5SDimitry Andric Tmp1 = MRI.createVirtualRegister(MRI.getRegClass(InReg)); 4810b57cec5SDimitry Andric CmpReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass); 4820b57cec5SDimitry Andric EqzReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass); 4830b57cec5SDimitry Andric FalseReg = MRI.createVirtualRegister(MRI.getRegClass(OutReg)); 4840b57cec5SDimitry Andric TrueReg = MRI.createVirtualRegister(MRI.getRegClass(OutReg)); 4850b57cec5SDimitry Andric 4860b57cec5SDimitry Andric MI.eraseFromParent(); 4870b57cec5SDimitry Andric // For signed numbers, we can do a single comparison to determine whether 4880b57cec5SDimitry Andric // fabs(x) is within range. 4890b57cec5SDimitry Andric if (IsUnsigned) { 4900b57cec5SDimitry Andric Tmp0 = InReg; 4910b57cec5SDimitry Andric } else { 4920b57cec5SDimitry Andric BuildMI(BB, DL, TII.get(Abs), Tmp0).addReg(InReg); 4930b57cec5SDimitry Andric } 4940b57cec5SDimitry Andric BuildMI(BB, DL, TII.get(FConst), Tmp1) 4950b57cec5SDimitry Andric .addFPImm(cast<ConstantFP>(ConstantFP::get(Ty, CmpVal))); 4960b57cec5SDimitry Andric BuildMI(BB, DL, TII.get(LT), CmpReg).addReg(Tmp0).addReg(Tmp1); 4970b57cec5SDimitry Andric 4980b57cec5SDimitry Andric // For unsigned numbers, we have to do a separate comparison with zero. 4990b57cec5SDimitry Andric if (IsUnsigned) { 5000b57cec5SDimitry Andric Tmp1 = MRI.createVirtualRegister(MRI.getRegClass(InReg)); 5018bcb0991SDimitry Andric Register SecondCmpReg = 5020b57cec5SDimitry Andric MRI.createVirtualRegister(&WebAssembly::I32RegClass); 5038bcb0991SDimitry Andric Register AndReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass); 5040b57cec5SDimitry Andric BuildMI(BB, DL, TII.get(FConst), Tmp1) 5050b57cec5SDimitry Andric .addFPImm(cast<ConstantFP>(ConstantFP::get(Ty, 0.0))); 5060b57cec5SDimitry Andric BuildMI(BB, DL, TII.get(GE), SecondCmpReg).addReg(Tmp0).addReg(Tmp1); 5070b57cec5SDimitry Andric BuildMI(BB, DL, TII.get(And), AndReg).addReg(CmpReg).addReg(SecondCmpReg); 5080b57cec5SDimitry Andric CmpReg = AndReg; 5090b57cec5SDimitry Andric } 5100b57cec5SDimitry Andric 5110b57cec5SDimitry Andric BuildMI(BB, DL, TII.get(Eqz), EqzReg).addReg(CmpReg); 5120b57cec5SDimitry Andric 5130b57cec5SDimitry Andric // Create the CFG diamond to select between doing the conversion or using 5140b57cec5SDimitry Andric // the substitute value. 5150b57cec5SDimitry Andric BuildMI(BB, DL, TII.get(WebAssembly::BR_IF)).addMBB(TrueMBB).addReg(EqzReg); 5160b57cec5SDimitry Andric BuildMI(FalseMBB, DL, TII.get(LoweredOpcode), FalseReg).addReg(InReg); 5170b57cec5SDimitry Andric BuildMI(FalseMBB, DL, TII.get(WebAssembly::BR)).addMBB(DoneMBB); 5180b57cec5SDimitry Andric BuildMI(TrueMBB, DL, TII.get(IConst), TrueReg).addImm(Substitute); 5190b57cec5SDimitry Andric BuildMI(*DoneMBB, DoneMBB->begin(), DL, TII.get(TargetOpcode::PHI), OutReg) 5200b57cec5SDimitry Andric .addReg(FalseReg) 5210b57cec5SDimitry Andric .addMBB(FalseMBB) 5220b57cec5SDimitry Andric .addReg(TrueReg) 5230b57cec5SDimitry Andric .addMBB(TrueMBB); 5240b57cec5SDimitry Andric 5250b57cec5SDimitry Andric return DoneMBB; 5260b57cec5SDimitry Andric } 5270b57cec5SDimitry Andric 528fe6060f1SDimitry Andric static MachineBasicBlock * 529fe6060f1SDimitry Andric LowerCallResults(MachineInstr &CallResults, DebugLoc DL, MachineBasicBlock *BB, 530fe6060f1SDimitry Andric const WebAssemblySubtarget *Subtarget, 5315ffd83dbSDimitry Andric const TargetInstrInfo &TII) { 5325ffd83dbSDimitry Andric MachineInstr &CallParams = *CallResults.getPrevNode(); 5335ffd83dbSDimitry Andric assert(CallParams.getOpcode() == WebAssembly::CALL_PARAMS); 5345ffd83dbSDimitry Andric assert(CallResults.getOpcode() == WebAssembly::CALL_RESULTS || 5355ffd83dbSDimitry Andric CallResults.getOpcode() == WebAssembly::RET_CALL_RESULTS); 5365ffd83dbSDimitry Andric 5375ffd83dbSDimitry Andric bool IsIndirect = CallParams.getOperand(0).isReg(); 5385ffd83dbSDimitry Andric bool IsRetCall = CallResults.getOpcode() == WebAssembly::RET_CALL_RESULTS; 5395ffd83dbSDimitry Andric 540fe6060f1SDimitry Andric bool IsFuncrefCall = false; 541fe6060f1SDimitry Andric if (IsIndirect) { 542fe6060f1SDimitry Andric Register Reg = CallParams.getOperand(0).getReg(); 543fe6060f1SDimitry Andric const MachineFunction *MF = BB->getParent(); 544fe6060f1SDimitry Andric const MachineRegisterInfo &MRI = MF->getRegInfo(); 545fe6060f1SDimitry Andric const TargetRegisterClass *TRC = MRI.getRegClass(Reg); 546fe6060f1SDimitry Andric IsFuncrefCall = (TRC == &WebAssembly::FUNCREFRegClass); 547fe6060f1SDimitry Andric assert(!IsFuncrefCall || Subtarget->hasReferenceTypes()); 548fe6060f1SDimitry Andric } 549fe6060f1SDimitry Andric 5505ffd83dbSDimitry Andric unsigned CallOp; 5515ffd83dbSDimitry Andric if (IsIndirect && IsRetCall) { 5525ffd83dbSDimitry Andric CallOp = WebAssembly::RET_CALL_INDIRECT; 5535ffd83dbSDimitry Andric } else if (IsIndirect) { 5545ffd83dbSDimitry Andric CallOp = WebAssembly::CALL_INDIRECT; 5555ffd83dbSDimitry Andric } else if (IsRetCall) { 5565ffd83dbSDimitry Andric CallOp = WebAssembly::RET_CALL; 5575ffd83dbSDimitry Andric } else { 5585ffd83dbSDimitry Andric CallOp = WebAssembly::CALL; 5595ffd83dbSDimitry Andric } 5605ffd83dbSDimitry Andric 5615ffd83dbSDimitry Andric MachineFunction &MF = *BB->getParent(); 5625ffd83dbSDimitry Andric const MCInstrDesc &MCID = TII.get(CallOp); 5635ffd83dbSDimitry Andric MachineInstrBuilder MIB(MF, MF.CreateMachineInstr(MCID, DL)); 5645ffd83dbSDimitry Andric 565e8d8bef9SDimitry Andric // See if we must truncate the function pointer. 566e8d8bef9SDimitry Andric // CALL_INDIRECT takes an i32, but in wasm64 we represent function pointers 567e8d8bef9SDimitry Andric // as 64-bit for uniformity with other pointer types. 568fe6060f1SDimitry Andric // See also: WebAssemblyFastISel::selectCall 569e8d8bef9SDimitry Andric if (IsIndirect && MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()) { 570e8d8bef9SDimitry Andric Register Reg32 = 571e8d8bef9SDimitry Andric MF.getRegInfo().createVirtualRegister(&WebAssembly::I32RegClass); 572e8d8bef9SDimitry Andric auto &FnPtr = CallParams.getOperand(0); 573e8d8bef9SDimitry Andric BuildMI(*BB, CallResults.getIterator(), DL, 574e8d8bef9SDimitry Andric TII.get(WebAssembly::I32_WRAP_I64), Reg32) 575e8d8bef9SDimitry Andric .addReg(FnPtr.getReg()); 576e8d8bef9SDimitry Andric FnPtr.setReg(Reg32); 577e8d8bef9SDimitry Andric } 578e8d8bef9SDimitry Andric 5795ffd83dbSDimitry Andric // Move the function pointer to the end of the arguments for indirect calls 5805ffd83dbSDimitry Andric if (IsIndirect) { 5815ffd83dbSDimitry Andric auto FnPtr = CallParams.getOperand(0); 58281ad6265SDimitry Andric CallParams.removeOperand(0); 583349cc55cSDimitry Andric 584349cc55cSDimitry Andric // For funcrefs, call_indirect is done through __funcref_call_table and the 585972a253aSDimitry Andric // funcref is always installed in slot 0 of the table, therefore instead of 586972a253aSDimitry Andric // having the function pointer added at the end of the params list, a zero 587972a253aSDimitry Andric // (the index in 588349cc55cSDimitry Andric // __funcref_call_table is added). 589349cc55cSDimitry Andric if (IsFuncrefCall) { 590349cc55cSDimitry Andric Register RegZero = 591349cc55cSDimitry Andric MF.getRegInfo().createVirtualRegister(&WebAssembly::I32RegClass); 592349cc55cSDimitry Andric MachineInstrBuilder MIBC0 = 593349cc55cSDimitry Andric BuildMI(MF, DL, TII.get(WebAssembly::CONST_I32), RegZero).addImm(0); 594349cc55cSDimitry Andric 595349cc55cSDimitry Andric BB->insert(CallResults.getIterator(), MIBC0); 596349cc55cSDimitry Andric MachineInstrBuilder(MF, CallParams).addReg(RegZero); 597349cc55cSDimitry Andric } else 5985ffd83dbSDimitry Andric CallParams.addOperand(FnPtr); 5995ffd83dbSDimitry Andric } 6005ffd83dbSDimitry Andric 6015ffd83dbSDimitry Andric for (auto Def : CallResults.defs()) 6025ffd83dbSDimitry Andric MIB.add(Def); 6035ffd83dbSDimitry Andric 6045ffd83dbSDimitry Andric if (IsIndirect) { 605fe6060f1SDimitry Andric // Placeholder for the type index. 6065ffd83dbSDimitry Andric MIB.addImm(0); 607fe6060f1SDimitry Andric // The table into which this call_indirect indexes. 608fe6060f1SDimitry Andric MCSymbolWasm *Table = IsFuncrefCall 609fe6060f1SDimitry Andric ? WebAssembly::getOrCreateFuncrefCallTableSymbol( 610fe6060f1SDimitry Andric MF.getContext(), Subtarget) 611fe6060f1SDimitry Andric : WebAssembly::getOrCreateFunctionTableSymbol( 612fe6060f1SDimitry Andric MF.getContext(), Subtarget); 613fe6060f1SDimitry Andric if (Subtarget->hasReferenceTypes()) { 614fe6060f1SDimitry Andric MIB.addSym(Table); 615fe6060f1SDimitry Andric } else { 616fe6060f1SDimitry Andric // For the MVP there is at most one table whose number is 0, but we can't 617fe6060f1SDimitry Andric // write a table symbol or issue relocations. Instead we just ensure the 618fe6060f1SDimitry Andric // table is live and write a zero. 619fe6060f1SDimitry Andric Table->setNoStrip(); 6205ffd83dbSDimitry Andric MIB.addImm(0); 621fe6060f1SDimitry Andric } 6225ffd83dbSDimitry Andric } 6235ffd83dbSDimitry Andric 6245ffd83dbSDimitry Andric for (auto Use : CallParams.uses()) 6255ffd83dbSDimitry Andric MIB.add(Use); 6265ffd83dbSDimitry Andric 6275ffd83dbSDimitry Andric BB->insert(CallResults.getIterator(), MIB); 6285ffd83dbSDimitry Andric CallParams.eraseFromParent(); 6295ffd83dbSDimitry Andric CallResults.eraseFromParent(); 6305ffd83dbSDimitry Andric 631fe6060f1SDimitry Andric // If this is a funcref call, to avoid hidden GC roots, we need to clear the 632fe6060f1SDimitry Andric // table slot with ref.null upon call_indirect return. 633fe6060f1SDimitry Andric // 634fe6060f1SDimitry Andric // This generates the following code, which comes right after a call_indirect 635fe6060f1SDimitry Andric // of a funcref: 636fe6060f1SDimitry Andric // 637fe6060f1SDimitry Andric // i32.const 0 638fe6060f1SDimitry Andric // ref.null func 639fe6060f1SDimitry Andric // table.set __funcref_call_table 640fe6060f1SDimitry Andric if (IsIndirect && IsFuncrefCall) { 641fe6060f1SDimitry Andric MCSymbolWasm *Table = WebAssembly::getOrCreateFuncrefCallTableSymbol( 642fe6060f1SDimitry Andric MF.getContext(), Subtarget); 643fe6060f1SDimitry Andric Register RegZero = 644fe6060f1SDimitry Andric MF.getRegInfo().createVirtualRegister(&WebAssembly::I32RegClass); 645fe6060f1SDimitry Andric MachineInstr *Const0 = 646fe6060f1SDimitry Andric BuildMI(MF, DL, TII.get(WebAssembly::CONST_I32), RegZero).addImm(0); 647fe6060f1SDimitry Andric BB->insertAfter(MIB.getInstr()->getIterator(), Const0); 648fe6060f1SDimitry Andric 649fe6060f1SDimitry Andric Register RegFuncref = 650fe6060f1SDimitry Andric MF.getRegInfo().createVirtualRegister(&WebAssembly::FUNCREFRegClass); 651fe6060f1SDimitry Andric MachineInstr *RefNull = 6520eae32dcSDimitry Andric BuildMI(MF, DL, TII.get(WebAssembly::REF_NULL_FUNCREF), RegFuncref); 653fe6060f1SDimitry Andric BB->insertAfter(Const0->getIterator(), RefNull); 654fe6060f1SDimitry Andric 655fe6060f1SDimitry Andric MachineInstr *TableSet = 656fe6060f1SDimitry Andric BuildMI(MF, DL, TII.get(WebAssembly::TABLE_SET_FUNCREF)) 657fe6060f1SDimitry Andric .addSym(Table) 658fe6060f1SDimitry Andric .addReg(RegZero) 659fe6060f1SDimitry Andric .addReg(RegFuncref); 660fe6060f1SDimitry Andric BB->insertAfter(RefNull->getIterator(), TableSet); 661fe6060f1SDimitry Andric } 662fe6060f1SDimitry Andric 6635ffd83dbSDimitry Andric return BB; 6645ffd83dbSDimitry Andric } 6655ffd83dbSDimitry Andric 6660b57cec5SDimitry Andric MachineBasicBlock *WebAssemblyTargetLowering::EmitInstrWithCustomInserter( 6670b57cec5SDimitry Andric MachineInstr &MI, MachineBasicBlock *BB) const { 6680b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget->getInstrInfo(); 6690b57cec5SDimitry Andric DebugLoc DL = MI.getDebugLoc(); 6700b57cec5SDimitry Andric 6710b57cec5SDimitry Andric switch (MI.getOpcode()) { 6720b57cec5SDimitry Andric default: 6730b57cec5SDimitry Andric llvm_unreachable("Unexpected instr type to insert"); 6740b57cec5SDimitry Andric case WebAssembly::FP_TO_SINT_I32_F32: 6750b57cec5SDimitry Andric return LowerFPToInt(MI, DL, BB, TII, false, false, false, 6760b57cec5SDimitry Andric WebAssembly::I32_TRUNC_S_F32); 6770b57cec5SDimitry Andric case WebAssembly::FP_TO_UINT_I32_F32: 6780b57cec5SDimitry Andric return LowerFPToInt(MI, DL, BB, TII, true, false, false, 6790b57cec5SDimitry Andric WebAssembly::I32_TRUNC_U_F32); 6800b57cec5SDimitry Andric case WebAssembly::FP_TO_SINT_I64_F32: 6810b57cec5SDimitry Andric return LowerFPToInt(MI, DL, BB, TII, false, true, false, 6820b57cec5SDimitry Andric WebAssembly::I64_TRUNC_S_F32); 6830b57cec5SDimitry Andric case WebAssembly::FP_TO_UINT_I64_F32: 6840b57cec5SDimitry Andric return LowerFPToInt(MI, DL, BB, TII, true, true, false, 6850b57cec5SDimitry Andric WebAssembly::I64_TRUNC_U_F32); 6860b57cec5SDimitry Andric case WebAssembly::FP_TO_SINT_I32_F64: 6870b57cec5SDimitry Andric return LowerFPToInt(MI, DL, BB, TII, false, false, true, 6880b57cec5SDimitry Andric WebAssembly::I32_TRUNC_S_F64); 6890b57cec5SDimitry Andric case WebAssembly::FP_TO_UINT_I32_F64: 6900b57cec5SDimitry Andric return LowerFPToInt(MI, DL, BB, TII, true, false, true, 6910b57cec5SDimitry Andric WebAssembly::I32_TRUNC_U_F64); 6920b57cec5SDimitry Andric case WebAssembly::FP_TO_SINT_I64_F64: 6930b57cec5SDimitry Andric return LowerFPToInt(MI, DL, BB, TII, false, true, true, 6940b57cec5SDimitry Andric WebAssembly::I64_TRUNC_S_F64); 6950b57cec5SDimitry Andric case WebAssembly::FP_TO_UINT_I64_F64: 6960b57cec5SDimitry Andric return LowerFPToInt(MI, DL, BB, TII, true, true, true, 6970b57cec5SDimitry Andric WebAssembly::I64_TRUNC_U_F64); 6985ffd83dbSDimitry Andric case WebAssembly::CALL_RESULTS: 6995ffd83dbSDimitry Andric case WebAssembly::RET_CALL_RESULTS: 700fe6060f1SDimitry Andric return LowerCallResults(MI, DL, BB, Subtarget, TII); 7010b57cec5SDimitry Andric } 7020b57cec5SDimitry Andric } 7030b57cec5SDimitry Andric 7040b57cec5SDimitry Andric const char * 7050b57cec5SDimitry Andric WebAssemblyTargetLowering::getTargetNodeName(unsigned Opcode) const { 7060b57cec5SDimitry Andric switch (static_cast<WebAssemblyISD::NodeType>(Opcode)) { 7070b57cec5SDimitry Andric case WebAssemblyISD::FIRST_NUMBER: 708480093f4SDimitry Andric case WebAssemblyISD::FIRST_MEM_OPCODE: 7090b57cec5SDimitry Andric break; 7100b57cec5SDimitry Andric #define HANDLE_NODETYPE(NODE) \ 7110b57cec5SDimitry Andric case WebAssemblyISD::NODE: \ 7120b57cec5SDimitry Andric return "WebAssemblyISD::" #NODE; 713480093f4SDimitry Andric #define HANDLE_MEM_NODETYPE(NODE) HANDLE_NODETYPE(NODE) 7140b57cec5SDimitry Andric #include "WebAssemblyISD.def" 715480093f4SDimitry Andric #undef HANDLE_MEM_NODETYPE 7160b57cec5SDimitry Andric #undef HANDLE_NODETYPE 7170b57cec5SDimitry Andric } 7180b57cec5SDimitry Andric return nullptr; 7190b57cec5SDimitry Andric } 7200b57cec5SDimitry Andric 7210b57cec5SDimitry Andric std::pair<unsigned, const TargetRegisterClass *> 7220b57cec5SDimitry Andric WebAssemblyTargetLowering::getRegForInlineAsmConstraint( 7230b57cec5SDimitry Andric const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const { 7240b57cec5SDimitry Andric // First, see if this is a constraint that directly corresponds to a 7250b57cec5SDimitry Andric // WebAssembly register class. 7260b57cec5SDimitry Andric if (Constraint.size() == 1) { 7270b57cec5SDimitry Andric switch (Constraint[0]) { 7280b57cec5SDimitry Andric case 'r': 7290b57cec5SDimitry Andric assert(VT != MVT::iPTR && "Pointer MVT not expected here"); 7300b57cec5SDimitry Andric if (Subtarget->hasSIMD128() && VT.isVector()) { 7310b57cec5SDimitry Andric if (VT.getSizeInBits() == 128) 7320b57cec5SDimitry Andric return std::make_pair(0U, &WebAssembly::V128RegClass); 7330b57cec5SDimitry Andric } 7340b57cec5SDimitry Andric if (VT.isInteger() && !VT.isVector()) { 7350b57cec5SDimitry Andric if (VT.getSizeInBits() <= 32) 7360b57cec5SDimitry Andric return std::make_pair(0U, &WebAssembly::I32RegClass); 7370b57cec5SDimitry Andric if (VT.getSizeInBits() <= 64) 7380b57cec5SDimitry Andric return std::make_pair(0U, &WebAssembly::I64RegClass); 7390b57cec5SDimitry Andric } 740e8d8bef9SDimitry Andric if (VT.isFloatingPoint() && !VT.isVector()) { 741e8d8bef9SDimitry Andric switch (VT.getSizeInBits()) { 742e8d8bef9SDimitry Andric case 32: 743e8d8bef9SDimitry Andric return std::make_pair(0U, &WebAssembly::F32RegClass); 744e8d8bef9SDimitry Andric case 64: 745e8d8bef9SDimitry Andric return std::make_pair(0U, &WebAssembly::F64RegClass); 746e8d8bef9SDimitry Andric default: 747e8d8bef9SDimitry Andric break; 748e8d8bef9SDimitry Andric } 749e8d8bef9SDimitry Andric } 7500b57cec5SDimitry Andric break; 7510b57cec5SDimitry Andric default: 7520b57cec5SDimitry Andric break; 7530b57cec5SDimitry Andric } 7540b57cec5SDimitry Andric } 7550b57cec5SDimitry Andric 7560b57cec5SDimitry Andric return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); 7570b57cec5SDimitry Andric } 7580b57cec5SDimitry Andric 759*bdd1243dSDimitry Andric bool WebAssemblyTargetLowering::isCheapToSpeculateCttz(Type *Ty) const { 7600b57cec5SDimitry Andric // Assume ctz is a relatively cheap operation. 7610b57cec5SDimitry Andric return true; 7620b57cec5SDimitry Andric } 7630b57cec5SDimitry Andric 764*bdd1243dSDimitry Andric bool WebAssemblyTargetLowering::isCheapToSpeculateCtlz(Type *Ty) const { 7650b57cec5SDimitry Andric // Assume clz is a relatively cheap operation. 7660b57cec5SDimitry Andric return true; 7670b57cec5SDimitry Andric } 7680b57cec5SDimitry Andric 7690b57cec5SDimitry Andric bool WebAssemblyTargetLowering::isLegalAddressingMode(const DataLayout &DL, 7700b57cec5SDimitry Andric const AddrMode &AM, 7710b57cec5SDimitry Andric Type *Ty, unsigned AS, 7720b57cec5SDimitry Andric Instruction *I) const { 7730b57cec5SDimitry Andric // WebAssembly offsets are added as unsigned without wrapping. The 7740b57cec5SDimitry Andric // isLegalAddressingMode gives us no way to determine if wrapping could be 7750b57cec5SDimitry Andric // happening, so we approximate this by accepting only non-negative offsets. 7760b57cec5SDimitry Andric if (AM.BaseOffs < 0) 7770b57cec5SDimitry Andric return false; 7780b57cec5SDimitry Andric 7790b57cec5SDimitry Andric // WebAssembly has no scale register operands. 7800b57cec5SDimitry Andric if (AM.Scale != 0) 7810b57cec5SDimitry Andric return false; 7820b57cec5SDimitry Andric 7830b57cec5SDimitry Andric // Everything else is legal. 7840b57cec5SDimitry Andric return true; 7850b57cec5SDimitry Andric } 7860b57cec5SDimitry Andric 7870b57cec5SDimitry Andric bool WebAssemblyTargetLowering::allowsMisalignedMemoryAccesses( 788fe6060f1SDimitry Andric EVT /*VT*/, unsigned /*AddrSpace*/, Align /*Align*/, 789*bdd1243dSDimitry Andric MachineMemOperand::Flags /*Flags*/, unsigned *Fast) const { 7900b57cec5SDimitry Andric // WebAssembly supports unaligned accesses, though it should be declared 7910b57cec5SDimitry Andric // with the p2align attribute on loads and stores which do so, and there 7920b57cec5SDimitry Andric // may be a performance impact. We tell LLVM they're "fast" because 7930b57cec5SDimitry Andric // for the kinds of things that LLVM uses this for (merging adjacent stores 7940b57cec5SDimitry Andric // of constants, etc.), WebAssembly implementations will either want the 7950b57cec5SDimitry Andric // unaligned access or they'll split anyway. 7960b57cec5SDimitry Andric if (Fast) 797*bdd1243dSDimitry Andric *Fast = 1; 7980b57cec5SDimitry Andric return true; 7990b57cec5SDimitry Andric } 8000b57cec5SDimitry Andric 8010b57cec5SDimitry Andric bool WebAssemblyTargetLowering::isIntDivCheap(EVT VT, 8020b57cec5SDimitry Andric AttributeList Attr) const { 8030b57cec5SDimitry Andric // The current thinking is that wasm engines will perform this optimization, 8040b57cec5SDimitry Andric // so we can save on code size. 8050b57cec5SDimitry Andric return true; 8060b57cec5SDimitry Andric } 8070b57cec5SDimitry Andric 8088bcb0991SDimitry Andric bool WebAssemblyTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const { 80916d6b3b3SDimitry Andric EVT ExtT = ExtVal.getValueType(); 81016d6b3b3SDimitry Andric EVT MemT = cast<LoadSDNode>(ExtVal->getOperand(0))->getValueType(0); 8118bcb0991SDimitry Andric return (ExtT == MVT::v8i16 && MemT == MVT::v8i8) || 8128bcb0991SDimitry Andric (ExtT == MVT::v4i32 && MemT == MVT::v4i16) || 8138bcb0991SDimitry Andric (ExtT == MVT::v2i64 && MemT == MVT::v2i32); 8148bcb0991SDimitry Andric } 8158bcb0991SDimitry Andric 816349cc55cSDimitry Andric bool WebAssemblyTargetLowering::isOffsetFoldingLegal( 817349cc55cSDimitry Andric const GlobalAddressSDNode *GA) const { 818349cc55cSDimitry Andric // Wasm doesn't support function addresses with offsets 819349cc55cSDimitry Andric const GlobalValue *GV = GA->getGlobal(); 820349cc55cSDimitry Andric return isa<Function>(GV) ? false : TargetLowering::isOffsetFoldingLegal(GA); 821349cc55cSDimitry Andric } 822349cc55cSDimitry Andric 8230b57cec5SDimitry Andric EVT WebAssemblyTargetLowering::getSetCCResultType(const DataLayout &DL, 8240b57cec5SDimitry Andric LLVMContext &C, 8250b57cec5SDimitry Andric EVT VT) const { 8260b57cec5SDimitry Andric if (VT.isVector()) 8270b57cec5SDimitry Andric return VT.changeVectorElementTypeToInteger(); 8280b57cec5SDimitry Andric 8295ffd83dbSDimitry Andric // So far, all branch instructions in Wasm take an I32 condition. 8305ffd83dbSDimitry Andric // The default TargetLowering::getSetCCResultType returns the pointer size, 8315ffd83dbSDimitry Andric // which would be useful to reduce instruction counts when testing 8325ffd83dbSDimitry Andric // against 64-bit pointers/values if at some point Wasm supports that. 8335ffd83dbSDimitry Andric return EVT::getIntegerVT(C, 32); 8340b57cec5SDimitry Andric } 8350b57cec5SDimitry Andric 8360b57cec5SDimitry Andric bool WebAssemblyTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info, 8370b57cec5SDimitry Andric const CallInst &I, 8380b57cec5SDimitry Andric MachineFunction &MF, 8390b57cec5SDimitry Andric unsigned Intrinsic) const { 8400b57cec5SDimitry Andric switch (Intrinsic) { 841e8d8bef9SDimitry Andric case Intrinsic::wasm_memory_atomic_notify: 8420b57cec5SDimitry Andric Info.opc = ISD::INTRINSIC_W_CHAIN; 8430b57cec5SDimitry Andric Info.memVT = MVT::i32; 8440b57cec5SDimitry Andric Info.ptrVal = I.getArgOperand(0); 8450b57cec5SDimitry Andric Info.offset = 0; 8468bcb0991SDimitry Andric Info.align = Align(4); 8470b57cec5SDimitry Andric // atomic.notify instruction does not really load the memory specified with 8480b57cec5SDimitry Andric // this argument, but MachineMemOperand should either be load or store, so 8490b57cec5SDimitry Andric // we set this to a load. 8500b57cec5SDimitry Andric // FIXME Volatile isn't really correct, but currently all LLVM atomic 8510b57cec5SDimitry Andric // instructions are treated as volatiles in the backend, so we should be 8520b57cec5SDimitry Andric // consistent. The same applies for wasm_atomic_wait intrinsics too. 8530b57cec5SDimitry Andric Info.flags = MachineMemOperand::MOVolatile | MachineMemOperand::MOLoad; 8540b57cec5SDimitry Andric return true; 855e8d8bef9SDimitry Andric case Intrinsic::wasm_memory_atomic_wait32: 8560b57cec5SDimitry Andric Info.opc = ISD::INTRINSIC_W_CHAIN; 8570b57cec5SDimitry Andric Info.memVT = MVT::i32; 8580b57cec5SDimitry Andric Info.ptrVal = I.getArgOperand(0); 8590b57cec5SDimitry Andric Info.offset = 0; 8608bcb0991SDimitry Andric Info.align = Align(4); 8610b57cec5SDimitry Andric Info.flags = MachineMemOperand::MOVolatile | MachineMemOperand::MOLoad; 8620b57cec5SDimitry Andric return true; 863e8d8bef9SDimitry Andric case Intrinsic::wasm_memory_atomic_wait64: 8640b57cec5SDimitry Andric Info.opc = ISD::INTRINSIC_W_CHAIN; 8650b57cec5SDimitry Andric Info.memVT = MVT::i64; 8660b57cec5SDimitry Andric Info.ptrVal = I.getArgOperand(0); 8670b57cec5SDimitry Andric Info.offset = 0; 8688bcb0991SDimitry Andric Info.align = Align(8); 8690b57cec5SDimitry Andric Info.flags = MachineMemOperand::MOVolatile | MachineMemOperand::MOLoad; 8700b57cec5SDimitry Andric return true; 8710b57cec5SDimitry Andric default: 8720b57cec5SDimitry Andric return false; 8730b57cec5SDimitry Andric } 8740b57cec5SDimitry Andric } 8750b57cec5SDimitry Andric 876349cc55cSDimitry Andric void WebAssemblyTargetLowering::computeKnownBitsForTargetNode( 877349cc55cSDimitry Andric const SDValue Op, KnownBits &Known, const APInt &DemandedElts, 878349cc55cSDimitry Andric const SelectionDAG &DAG, unsigned Depth) const { 879349cc55cSDimitry Andric switch (Op.getOpcode()) { 880349cc55cSDimitry Andric default: 881349cc55cSDimitry Andric break; 882349cc55cSDimitry Andric case ISD::INTRINSIC_WO_CHAIN: { 883349cc55cSDimitry Andric unsigned IntNo = Op.getConstantOperandVal(0); 884349cc55cSDimitry Andric switch (IntNo) { 885349cc55cSDimitry Andric default: 886349cc55cSDimitry Andric break; 887349cc55cSDimitry Andric case Intrinsic::wasm_bitmask: { 888349cc55cSDimitry Andric unsigned BitWidth = Known.getBitWidth(); 889349cc55cSDimitry Andric EVT VT = Op.getOperand(1).getSimpleValueType(); 890349cc55cSDimitry Andric unsigned PossibleBits = VT.getVectorNumElements(); 891349cc55cSDimitry Andric APInt ZeroMask = APInt::getHighBitsSet(BitWidth, BitWidth - PossibleBits); 892349cc55cSDimitry Andric Known.Zero |= ZeroMask; 893349cc55cSDimitry Andric break; 894349cc55cSDimitry Andric } 895349cc55cSDimitry Andric } 896349cc55cSDimitry Andric } 897349cc55cSDimitry Andric } 898349cc55cSDimitry Andric } 899349cc55cSDimitry Andric 900349cc55cSDimitry Andric TargetLoweringBase::LegalizeTypeAction 901349cc55cSDimitry Andric WebAssemblyTargetLowering::getPreferredVectorAction(MVT VT) const { 902349cc55cSDimitry Andric if (VT.isFixedLengthVector()) { 903349cc55cSDimitry Andric MVT EltVT = VT.getVectorElementType(); 904349cc55cSDimitry Andric // We have legal vector types with these lane types, so widening the 905349cc55cSDimitry Andric // vector would let us use some of the lanes directly without having to 906349cc55cSDimitry Andric // extend or truncate values. 907349cc55cSDimitry Andric if (EltVT == MVT::i8 || EltVT == MVT::i16 || EltVT == MVT::i32 || 908349cc55cSDimitry Andric EltVT == MVT::i64 || EltVT == MVT::f32 || EltVT == MVT::f64) 909349cc55cSDimitry Andric return TypeWidenVector; 910349cc55cSDimitry Andric } 911349cc55cSDimitry Andric 912349cc55cSDimitry Andric return TargetLoweringBase::getPreferredVectorAction(VT); 913349cc55cSDimitry Andric } 914349cc55cSDimitry Andric 91581ad6265SDimitry Andric bool WebAssemblyTargetLowering::shouldSimplifyDemandedVectorElts( 91681ad6265SDimitry Andric SDValue Op, const TargetLoweringOpt &TLO) const { 91781ad6265SDimitry Andric // ISel process runs DAGCombiner after legalization; this step is called 91881ad6265SDimitry Andric // SelectionDAG optimization phase. This post-legalization combining process 91981ad6265SDimitry Andric // runs DAGCombiner on each node, and if there was a change to be made, 92081ad6265SDimitry Andric // re-runs legalization again on it and its user nodes to make sure 92181ad6265SDimitry Andric // everythiing is in a legalized state. 92281ad6265SDimitry Andric // 92381ad6265SDimitry Andric // The legalization calls lowering routines, and we do our custom lowering for 92481ad6265SDimitry Andric // build_vectors (LowerBUILD_VECTOR), which converts undef vector elements 92581ad6265SDimitry Andric // into zeros. But there is a set of routines in DAGCombiner that turns unused 92681ad6265SDimitry Andric // (= not demanded) nodes into undef, among which SimplifyDemandedVectorElts 92781ad6265SDimitry Andric // turns unused vector elements into undefs. But this routine does not work 92881ad6265SDimitry Andric // with our custom LowerBUILD_VECTOR, which turns undefs into zeros. This 92981ad6265SDimitry Andric // combination can result in a infinite loop, in which undefs are converted to 93081ad6265SDimitry Andric // zeros in legalization and back to undefs in combining. 93181ad6265SDimitry Andric // 93281ad6265SDimitry Andric // So after DAG is legalized, we prevent SimplifyDemandedVectorElts from 93381ad6265SDimitry Andric // running for build_vectors. 93481ad6265SDimitry Andric if (Op.getOpcode() == ISD::BUILD_VECTOR && TLO.LegalOps && TLO.LegalTys) 93581ad6265SDimitry Andric return false; 93681ad6265SDimitry Andric return true; 93781ad6265SDimitry Andric } 93881ad6265SDimitry Andric 9390b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 9400b57cec5SDimitry Andric // WebAssembly Lowering private implementation. 9410b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 9420b57cec5SDimitry Andric 9430b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 9440b57cec5SDimitry Andric // Lowering Code 9450b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 9460b57cec5SDimitry Andric 9470b57cec5SDimitry Andric static void fail(const SDLoc &DL, SelectionDAG &DAG, const char *Msg) { 9480b57cec5SDimitry Andric MachineFunction &MF = DAG.getMachineFunction(); 9490b57cec5SDimitry Andric DAG.getContext()->diagnose( 9500b57cec5SDimitry Andric DiagnosticInfoUnsupported(MF.getFunction(), Msg, DL.getDebugLoc())); 9510b57cec5SDimitry Andric } 9520b57cec5SDimitry Andric 9530b57cec5SDimitry Andric // Test whether the given calling convention is supported. 9540b57cec5SDimitry Andric static bool callingConvSupported(CallingConv::ID CallConv) { 9550b57cec5SDimitry Andric // We currently support the language-independent target-independent 9560b57cec5SDimitry Andric // conventions. We don't yet have a way to annotate calls with properties like 9570b57cec5SDimitry Andric // "cold", and we don't have any call-clobbered registers, so these are mostly 9580b57cec5SDimitry Andric // all handled the same. 9590b57cec5SDimitry Andric return CallConv == CallingConv::C || CallConv == CallingConv::Fast || 9600b57cec5SDimitry Andric CallConv == CallingConv::Cold || 9610b57cec5SDimitry Andric CallConv == CallingConv::PreserveMost || 9620b57cec5SDimitry Andric CallConv == CallingConv::PreserveAll || 9638bcb0991SDimitry Andric CallConv == CallingConv::CXX_FAST_TLS || 9645ffd83dbSDimitry Andric CallConv == CallingConv::WASM_EmscriptenInvoke || 9655ffd83dbSDimitry Andric CallConv == CallingConv::Swift; 9660b57cec5SDimitry Andric } 9670b57cec5SDimitry Andric 9680b57cec5SDimitry Andric SDValue 9690b57cec5SDimitry Andric WebAssemblyTargetLowering::LowerCall(CallLoweringInfo &CLI, 9700b57cec5SDimitry Andric SmallVectorImpl<SDValue> &InVals) const { 9710b57cec5SDimitry Andric SelectionDAG &DAG = CLI.DAG; 9720b57cec5SDimitry Andric SDLoc DL = CLI.DL; 9730b57cec5SDimitry Andric SDValue Chain = CLI.Chain; 9740b57cec5SDimitry Andric SDValue Callee = CLI.Callee; 9750b57cec5SDimitry Andric MachineFunction &MF = DAG.getMachineFunction(); 9760b57cec5SDimitry Andric auto Layout = MF.getDataLayout(); 9770b57cec5SDimitry Andric 9780b57cec5SDimitry Andric CallingConv::ID CallConv = CLI.CallConv; 9790b57cec5SDimitry Andric if (!callingConvSupported(CallConv)) 9800b57cec5SDimitry Andric fail(DL, DAG, 9810b57cec5SDimitry Andric "WebAssembly doesn't support language-specific or target-specific " 9820b57cec5SDimitry Andric "calling conventions yet"); 9830b57cec5SDimitry Andric if (CLI.IsPatchPoint) 9840b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly doesn't support patch point yet"); 9850b57cec5SDimitry Andric 9868bcb0991SDimitry Andric if (CLI.IsTailCall) { 9875ffd83dbSDimitry Andric auto NoTail = [&](const char *Msg) { 9885ffd83dbSDimitry Andric if (CLI.CB && CLI.CB->isMustTailCall()) 9895ffd83dbSDimitry Andric fail(DL, DAG, Msg); 9905ffd83dbSDimitry Andric CLI.IsTailCall = false; 9915ffd83dbSDimitry Andric }; 9925ffd83dbSDimitry Andric 9935ffd83dbSDimitry Andric if (!Subtarget->hasTailCall()) 9945ffd83dbSDimitry Andric NoTail("WebAssembly 'tail-call' feature not enabled"); 9955ffd83dbSDimitry Andric 9965ffd83dbSDimitry Andric // Varargs calls cannot be tail calls because the buffer is on the stack 9975ffd83dbSDimitry Andric if (CLI.IsVarArg) 9985ffd83dbSDimitry Andric NoTail("WebAssembly does not support varargs tail calls"); 9995ffd83dbSDimitry Andric 10008bcb0991SDimitry Andric // Do not tail call unless caller and callee return types match 10018bcb0991SDimitry Andric const Function &F = MF.getFunction(); 10028bcb0991SDimitry Andric const TargetMachine &TM = getTargetMachine(); 10038bcb0991SDimitry Andric Type *RetTy = F.getReturnType(); 10048bcb0991SDimitry Andric SmallVector<MVT, 4> CallerRetTys; 10058bcb0991SDimitry Andric SmallVector<MVT, 4> CalleeRetTys; 10068bcb0991SDimitry Andric computeLegalValueVTs(F, TM, RetTy, CallerRetTys); 10078bcb0991SDimitry Andric computeLegalValueVTs(F, TM, CLI.RetTy, CalleeRetTys); 10088bcb0991SDimitry Andric bool TypesMatch = CallerRetTys.size() == CalleeRetTys.size() && 10098bcb0991SDimitry Andric std::equal(CallerRetTys.begin(), CallerRetTys.end(), 10108bcb0991SDimitry Andric CalleeRetTys.begin()); 10115ffd83dbSDimitry Andric if (!TypesMatch) 10125ffd83dbSDimitry Andric NoTail("WebAssembly tail call requires caller and callee return types to " 10135ffd83dbSDimitry Andric "match"); 10145ffd83dbSDimitry Andric 10155ffd83dbSDimitry Andric // If pointers to local stack values are passed, we cannot tail call 10165ffd83dbSDimitry Andric if (CLI.CB) { 10175ffd83dbSDimitry Andric for (auto &Arg : CLI.CB->args()) { 10185ffd83dbSDimitry Andric Value *Val = Arg.get(); 10195ffd83dbSDimitry Andric // Trace the value back through pointer operations 10205ffd83dbSDimitry Andric while (true) { 10215ffd83dbSDimitry Andric Value *Src = Val->stripPointerCastsAndAliases(); 10225ffd83dbSDimitry Andric if (auto *GEP = dyn_cast<GetElementPtrInst>(Src)) 10235ffd83dbSDimitry Andric Src = GEP->getPointerOperand(); 10245ffd83dbSDimitry Andric if (Val == Src) 10255ffd83dbSDimitry Andric break; 10265ffd83dbSDimitry Andric Val = Src; 10270b57cec5SDimitry Andric } 10285ffd83dbSDimitry Andric if (isa<AllocaInst>(Val)) { 10295ffd83dbSDimitry Andric NoTail( 10305ffd83dbSDimitry Andric "WebAssembly does not support tail calling with stack arguments"); 10315ffd83dbSDimitry Andric break; 10328bcb0991SDimitry Andric } 10338bcb0991SDimitry Andric } 10348bcb0991SDimitry Andric } 10358bcb0991SDimitry Andric } 10360b57cec5SDimitry Andric 10370b57cec5SDimitry Andric SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; 10380b57cec5SDimitry Andric SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; 10390b57cec5SDimitry Andric SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; 10408bcb0991SDimitry Andric 10418bcb0991SDimitry Andric // The generic code may have added an sret argument. If we're lowering an 10428bcb0991SDimitry Andric // invoke function, the ABI requires that the function pointer be the first 10438bcb0991SDimitry Andric // argument, so we may have to swap the arguments. 10448bcb0991SDimitry Andric if (CallConv == CallingConv::WASM_EmscriptenInvoke && Outs.size() >= 2 && 10458bcb0991SDimitry Andric Outs[0].Flags.isSRet()) { 10468bcb0991SDimitry Andric std::swap(Outs[0], Outs[1]); 10478bcb0991SDimitry Andric std::swap(OutVals[0], OutVals[1]); 10488bcb0991SDimitry Andric } 10498bcb0991SDimitry Andric 10505ffd83dbSDimitry Andric bool HasSwiftSelfArg = false; 10515ffd83dbSDimitry Andric bool HasSwiftErrorArg = false; 10520b57cec5SDimitry Andric unsigned NumFixedArgs = 0; 10530b57cec5SDimitry Andric for (unsigned I = 0; I < Outs.size(); ++I) { 10540b57cec5SDimitry Andric const ISD::OutputArg &Out = Outs[I]; 10550b57cec5SDimitry Andric SDValue &OutVal = OutVals[I]; 10565ffd83dbSDimitry Andric HasSwiftSelfArg |= Out.Flags.isSwiftSelf(); 10575ffd83dbSDimitry Andric HasSwiftErrorArg |= Out.Flags.isSwiftError(); 10580b57cec5SDimitry Andric if (Out.Flags.isNest()) 10590b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented nest arguments"); 10600b57cec5SDimitry Andric if (Out.Flags.isInAlloca()) 10610b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented inalloca arguments"); 10620b57cec5SDimitry Andric if (Out.Flags.isInConsecutiveRegs()) 10630b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented cons regs arguments"); 10640b57cec5SDimitry Andric if (Out.Flags.isInConsecutiveRegsLast()) 10650b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented cons regs last arguments"); 10660b57cec5SDimitry Andric if (Out.Flags.isByVal() && Out.Flags.getByValSize() != 0) { 10670b57cec5SDimitry Andric auto &MFI = MF.getFrameInfo(); 10680b57cec5SDimitry Andric int FI = MFI.CreateStackObject(Out.Flags.getByValSize(), 10695ffd83dbSDimitry Andric Out.Flags.getNonZeroByValAlign(), 10700b57cec5SDimitry Andric /*isSS=*/false); 10710b57cec5SDimitry Andric SDValue SizeNode = 10720b57cec5SDimitry Andric DAG.getConstant(Out.Flags.getByValSize(), DL, MVT::i32); 10730b57cec5SDimitry Andric SDValue FINode = DAG.getFrameIndex(FI, getPointerTy(Layout)); 10740b57cec5SDimitry Andric Chain = DAG.getMemcpy( 10755ffd83dbSDimitry Andric Chain, DL, FINode, OutVal, SizeNode, Out.Flags.getNonZeroByValAlign(), 10760b57cec5SDimitry Andric /*isVolatile*/ false, /*AlwaysInline=*/false, 10770b57cec5SDimitry Andric /*isTailCall*/ false, MachinePointerInfo(), MachinePointerInfo()); 10780b57cec5SDimitry Andric OutVal = FINode; 10790b57cec5SDimitry Andric } 10800b57cec5SDimitry Andric // Count the number of fixed args *after* legalization. 10810b57cec5SDimitry Andric NumFixedArgs += Out.IsFixed; 10820b57cec5SDimitry Andric } 10830b57cec5SDimitry Andric 10840b57cec5SDimitry Andric bool IsVarArg = CLI.IsVarArg; 10850b57cec5SDimitry Andric auto PtrVT = getPointerTy(Layout); 10860b57cec5SDimitry Andric 10875ffd83dbSDimitry Andric // For swiftcc, emit additional swiftself and swifterror arguments 10885ffd83dbSDimitry Andric // if there aren't. These additional arguments are also added for callee 10895ffd83dbSDimitry Andric // signature They are necessary to match callee and caller signature for 10905ffd83dbSDimitry Andric // indirect call. 10915ffd83dbSDimitry Andric if (CallConv == CallingConv::Swift) { 10925ffd83dbSDimitry Andric if (!HasSwiftSelfArg) { 10935ffd83dbSDimitry Andric NumFixedArgs++; 10945ffd83dbSDimitry Andric ISD::OutputArg Arg; 10955ffd83dbSDimitry Andric Arg.Flags.setSwiftSelf(); 10965ffd83dbSDimitry Andric CLI.Outs.push_back(Arg); 10975ffd83dbSDimitry Andric SDValue ArgVal = DAG.getUNDEF(PtrVT); 10985ffd83dbSDimitry Andric CLI.OutVals.push_back(ArgVal); 10995ffd83dbSDimitry Andric } 11005ffd83dbSDimitry Andric if (!HasSwiftErrorArg) { 11015ffd83dbSDimitry Andric NumFixedArgs++; 11025ffd83dbSDimitry Andric ISD::OutputArg Arg; 11035ffd83dbSDimitry Andric Arg.Flags.setSwiftError(); 11045ffd83dbSDimitry Andric CLI.Outs.push_back(Arg); 11055ffd83dbSDimitry Andric SDValue ArgVal = DAG.getUNDEF(PtrVT); 11065ffd83dbSDimitry Andric CLI.OutVals.push_back(ArgVal); 11075ffd83dbSDimitry Andric } 11085ffd83dbSDimitry Andric } 11095ffd83dbSDimitry Andric 11100b57cec5SDimitry Andric // Analyze operands of the call, assigning locations to each operand. 11110b57cec5SDimitry Andric SmallVector<CCValAssign, 16> ArgLocs; 11120b57cec5SDimitry Andric CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext()); 11130b57cec5SDimitry Andric 11140b57cec5SDimitry Andric if (IsVarArg) { 11150b57cec5SDimitry Andric // Outgoing non-fixed arguments are placed in a buffer. First 11160b57cec5SDimitry Andric // compute their offsets and the total amount of buffer space needed. 11170b57cec5SDimitry Andric for (unsigned I = NumFixedArgs; I < Outs.size(); ++I) { 11180b57cec5SDimitry Andric const ISD::OutputArg &Out = Outs[I]; 11190b57cec5SDimitry Andric SDValue &Arg = OutVals[I]; 11200b57cec5SDimitry Andric EVT VT = Arg.getValueType(); 11210b57cec5SDimitry Andric assert(VT != MVT::iPTR && "Legalized args should be concrete"); 11220b57cec5SDimitry Andric Type *Ty = VT.getTypeForEVT(*DAG.getContext()); 11235ffd83dbSDimitry Andric Align Alignment = 11245ffd83dbSDimitry Andric std::max(Out.Flags.getNonZeroOrigAlign(), Layout.getABITypeAlign(Ty)); 11255ffd83dbSDimitry Andric unsigned Offset = 11265ffd83dbSDimitry Andric CCInfo.AllocateStack(Layout.getTypeAllocSize(Ty), Alignment); 11270b57cec5SDimitry Andric CCInfo.addLoc(CCValAssign::getMem(ArgLocs.size(), VT.getSimpleVT(), 11280b57cec5SDimitry Andric Offset, VT.getSimpleVT(), 11290b57cec5SDimitry Andric CCValAssign::Full)); 11300b57cec5SDimitry Andric } 11310b57cec5SDimitry Andric } 11320b57cec5SDimitry Andric 11330b57cec5SDimitry Andric unsigned NumBytes = CCInfo.getAlignedCallFrameSize(); 11340b57cec5SDimitry Andric 11350b57cec5SDimitry Andric SDValue FINode; 11360b57cec5SDimitry Andric if (IsVarArg && NumBytes) { 11370b57cec5SDimitry Andric // For non-fixed arguments, next emit stores to store the argument values 11380b57cec5SDimitry Andric // to the stack buffer at the offsets computed above. 11390b57cec5SDimitry Andric int FI = MF.getFrameInfo().CreateStackObject(NumBytes, 11400b57cec5SDimitry Andric Layout.getStackAlignment(), 11410b57cec5SDimitry Andric /*isSS=*/false); 11420b57cec5SDimitry Andric unsigned ValNo = 0; 11430b57cec5SDimitry Andric SmallVector<SDValue, 8> Chains; 1144e8d8bef9SDimitry Andric for (SDValue Arg : drop_begin(OutVals, NumFixedArgs)) { 11450b57cec5SDimitry Andric assert(ArgLocs[ValNo].getValNo() == ValNo && 11460b57cec5SDimitry Andric "ArgLocs should remain in order and only hold varargs args"); 11470b57cec5SDimitry Andric unsigned Offset = ArgLocs[ValNo++].getLocMemOffset(); 11480b57cec5SDimitry Andric FINode = DAG.getFrameIndex(FI, getPointerTy(Layout)); 11490b57cec5SDimitry Andric SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, FINode, 11500b57cec5SDimitry Andric DAG.getConstant(Offset, DL, PtrVT)); 11510b57cec5SDimitry Andric Chains.push_back( 11520b57cec5SDimitry Andric DAG.getStore(Chain, DL, Arg, Add, 1153e8d8bef9SDimitry Andric MachinePointerInfo::getFixedStack(MF, FI, Offset))); 11540b57cec5SDimitry Andric } 11550b57cec5SDimitry Andric if (!Chains.empty()) 11560b57cec5SDimitry Andric Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains); 11570b57cec5SDimitry Andric } else if (IsVarArg) { 11580b57cec5SDimitry Andric FINode = DAG.getIntPtrConstant(0, DL); 11590b57cec5SDimitry Andric } 11600b57cec5SDimitry Andric 11610b57cec5SDimitry Andric if (Callee->getOpcode() == ISD::GlobalAddress) { 11620b57cec5SDimitry Andric // If the callee is a GlobalAddress node (quite common, every direct call 11630b57cec5SDimitry Andric // is) turn it into a TargetGlobalAddress node so that LowerGlobalAddress 11640b57cec5SDimitry Andric // doesn't at MO_GOT which is not needed for direct calls. 11650b57cec5SDimitry Andric GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Callee); 11660b57cec5SDimitry Andric Callee = DAG.getTargetGlobalAddress(GA->getGlobal(), DL, 11670b57cec5SDimitry Andric getPointerTy(DAG.getDataLayout()), 11680b57cec5SDimitry Andric GA->getOffset()); 11690b57cec5SDimitry Andric Callee = DAG.getNode(WebAssemblyISD::Wrapper, DL, 11700b57cec5SDimitry Andric getPointerTy(DAG.getDataLayout()), Callee); 11710b57cec5SDimitry Andric } 11720b57cec5SDimitry Andric 11730b57cec5SDimitry Andric // Compute the operands for the CALLn node. 11740b57cec5SDimitry Andric SmallVector<SDValue, 16> Ops; 11750b57cec5SDimitry Andric Ops.push_back(Chain); 11760b57cec5SDimitry Andric Ops.push_back(Callee); 11770b57cec5SDimitry Andric 11780b57cec5SDimitry Andric // Add all fixed arguments. Note that for non-varargs calls, NumFixedArgs 11790b57cec5SDimitry Andric // isn't reliable. 11800b57cec5SDimitry Andric Ops.append(OutVals.begin(), 11810b57cec5SDimitry Andric IsVarArg ? OutVals.begin() + NumFixedArgs : OutVals.end()); 11820b57cec5SDimitry Andric // Add a pointer to the vararg buffer. 11830b57cec5SDimitry Andric if (IsVarArg) 11840b57cec5SDimitry Andric Ops.push_back(FINode); 11850b57cec5SDimitry Andric 11860b57cec5SDimitry Andric SmallVector<EVT, 8> InTys; 11870b57cec5SDimitry Andric for (const auto &In : Ins) { 11880b57cec5SDimitry Andric assert(!In.Flags.isByVal() && "byval is not valid for return values"); 11890b57cec5SDimitry Andric assert(!In.Flags.isNest() && "nest is not valid for return values"); 11900b57cec5SDimitry Andric if (In.Flags.isInAlloca()) 11910b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented inalloca return values"); 11920b57cec5SDimitry Andric if (In.Flags.isInConsecutiveRegs()) 11930b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented cons regs return values"); 11940b57cec5SDimitry Andric if (In.Flags.isInConsecutiveRegsLast()) 11950b57cec5SDimitry Andric fail(DL, DAG, 11960b57cec5SDimitry Andric "WebAssembly hasn't implemented cons regs last return values"); 11975ffd83dbSDimitry Andric // Ignore In.getNonZeroOrigAlign() because all our arguments are passed in 11980b57cec5SDimitry Andric // registers. 11990b57cec5SDimitry Andric InTys.push_back(In.VT); 12000b57cec5SDimitry Andric } 12010b57cec5SDimitry Andric 1202fe6060f1SDimitry Andric // Lastly, if this is a call to a funcref we need to add an instruction 1203fe6060f1SDimitry Andric // table.set to the chain and transform the call. 1204349cc55cSDimitry Andric if (CLI.CB && 1205349cc55cSDimitry Andric WebAssembly::isFuncrefType(CLI.CB->getCalledOperand()->getType())) { 1206fe6060f1SDimitry Andric // In the absence of function references proposal where a funcref call is 1207fe6060f1SDimitry Andric // lowered to call_ref, using reference types we generate a table.set to set 1208fe6060f1SDimitry Andric // the funcref to a special table used solely for this purpose, followed by 1209fe6060f1SDimitry Andric // a call_indirect. Here we just generate the table set, and return the 1210fe6060f1SDimitry Andric // SDValue of the table.set so that LowerCall can finalize the lowering by 1211fe6060f1SDimitry Andric // generating the call_indirect. 1212fe6060f1SDimitry Andric SDValue Chain = Ops[0]; 1213fe6060f1SDimitry Andric 1214fe6060f1SDimitry Andric MCSymbolWasm *Table = WebAssembly::getOrCreateFuncrefCallTableSymbol( 1215fe6060f1SDimitry Andric MF.getContext(), Subtarget); 1216fe6060f1SDimitry Andric SDValue Sym = DAG.getMCSymbol(Table, PtrVT); 1217fe6060f1SDimitry Andric SDValue TableSlot = DAG.getConstant(0, DL, MVT::i32); 1218fe6060f1SDimitry Andric SDValue TableSetOps[] = {Chain, Sym, TableSlot, Callee}; 1219fe6060f1SDimitry Andric SDValue TableSet = DAG.getMemIntrinsicNode( 1220fe6060f1SDimitry Andric WebAssemblyISD::TABLE_SET, DL, DAG.getVTList(MVT::Other), TableSetOps, 1221fe6060f1SDimitry Andric MVT::funcref, 1222fe6060f1SDimitry Andric // Machine Mem Operand args 1223349cc55cSDimitry Andric MachinePointerInfo( 1224349cc55cSDimitry Andric WebAssembly::WasmAddressSpace::WASM_ADDRESS_SPACE_FUNCREF), 1225fe6060f1SDimitry Andric CLI.CB->getCalledOperand()->getPointerAlignment(DAG.getDataLayout()), 1226fe6060f1SDimitry Andric MachineMemOperand::MOStore); 1227fe6060f1SDimitry Andric 1228fe6060f1SDimitry Andric Ops[0] = TableSet; // The new chain is the TableSet itself 1229fe6060f1SDimitry Andric } 1230fe6060f1SDimitry Andric 12310b57cec5SDimitry Andric if (CLI.IsTailCall) { 12320b57cec5SDimitry Andric // ret_calls do not return values to the current frame 12330b57cec5SDimitry Andric SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 12340b57cec5SDimitry Andric return DAG.getNode(WebAssemblyISD::RET_CALL, DL, NodeTys, Ops); 12350b57cec5SDimitry Andric } 12360b57cec5SDimitry Andric 12370b57cec5SDimitry Andric InTys.push_back(MVT::Other); 12380b57cec5SDimitry Andric SDVTList InTyList = DAG.getVTList(InTys); 12395ffd83dbSDimitry Andric SDValue Res = DAG.getNode(WebAssemblyISD::CALL, DL, InTyList, Ops); 12400b57cec5SDimitry Andric 12415ffd83dbSDimitry Andric for (size_t I = 0; I < Ins.size(); ++I) 12425ffd83dbSDimitry Andric InVals.push_back(Res.getValue(I)); 12435ffd83dbSDimitry Andric 12445ffd83dbSDimitry Andric // Return the chain 12455ffd83dbSDimitry Andric return Res.getValue(Ins.size()); 12460b57cec5SDimitry Andric } 12470b57cec5SDimitry Andric 12480b57cec5SDimitry Andric bool WebAssemblyTargetLowering::CanLowerReturn( 12490b57cec5SDimitry Andric CallingConv::ID /*CallConv*/, MachineFunction & /*MF*/, bool /*IsVarArg*/, 12500b57cec5SDimitry Andric const SmallVectorImpl<ISD::OutputArg> &Outs, 12510b57cec5SDimitry Andric LLVMContext & /*Context*/) const { 12528bcb0991SDimitry Andric // WebAssembly can only handle returning tuples with multivalue enabled 12538bcb0991SDimitry Andric return Subtarget->hasMultivalue() || Outs.size() <= 1; 12540b57cec5SDimitry Andric } 12550b57cec5SDimitry Andric 12560b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerReturn( 12570b57cec5SDimitry Andric SDValue Chain, CallingConv::ID CallConv, bool /*IsVarArg*/, 12580b57cec5SDimitry Andric const SmallVectorImpl<ISD::OutputArg> &Outs, 12590b57cec5SDimitry Andric const SmallVectorImpl<SDValue> &OutVals, const SDLoc &DL, 12600b57cec5SDimitry Andric SelectionDAG &DAG) const { 12618bcb0991SDimitry Andric assert((Subtarget->hasMultivalue() || Outs.size() <= 1) && 12628bcb0991SDimitry Andric "MVP WebAssembly can only return up to one value"); 12630b57cec5SDimitry Andric if (!callingConvSupported(CallConv)) 12640b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly doesn't support non-C calling conventions"); 12650b57cec5SDimitry Andric 12660b57cec5SDimitry Andric SmallVector<SDValue, 4> RetOps(1, Chain); 12670b57cec5SDimitry Andric RetOps.append(OutVals.begin(), OutVals.end()); 12680b57cec5SDimitry Andric Chain = DAG.getNode(WebAssemblyISD::RETURN, DL, MVT::Other, RetOps); 12690b57cec5SDimitry Andric 12700b57cec5SDimitry Andric // Record the number and types of the return values. 12710b57cec5SDimitry Andric for (const ISD::OutputArg &Out : Outs) { 12720b57cec5SDimitry Andric assert(!Out.Flags.isByVal() && "byval is not valid for return values"); 12730b57cec5SDimitry Andric assert(!Out.Flags.isNest() && "nest is not valid for return values"); 12740b57cec5SDimitry Andric assert(Out.IsFixed && "non-fixed return value is not valid"); 12750b57cec5SDimitry Andric if (Out.Flags.isInAlloca()) 12760b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented inalloca results"); 12770b57cec5SDimitry Andric if (Out.Flags.isInConsecutiveRegs()) 12780b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented cons regs results"); 12790b57cec5SDimitry Andric if (Out.Flags.isInConsecutiveRegsLast()) 12800b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented cons regs last results"); 12810b57cec5SDimitry Andric } 12820b57cec5SDimitry Andric 12830b57cec5SDimitry Andric return Chain; 12840b57cec5SDimitry Andric } 12850b57cec5SDimitry Andric 12860b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerFormalArguments( 12870b57cec5SDimitry Andric SDValue Chain, CallingConv::ID CallConv, bool IsVarArg, 12880b57cec5SDimitry Andric const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL, 12890b57cec5SDimitry Andric SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { 12900b57cec5SDimitry Andric if (!callingConvSupported(CallConv)) 12910b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly doesn't support non-C calling conventions"); 12920b57cec5SDimitry Andric 12930b57cec5SDimitry Andric MachineFunction &MF = DAG.getMachineFunction(); 12940b57cec5SDimitry Andric auto *MFI = MF.getInfo<WebAssemblyFunctionInfo>(); 12950b57cec5SDimitry Andric 12960b57cec5SDimitry Andric // Set up the incoming ARGUMENTS value, which serves to represent the liveness 12970b57cec5SDimitry Andric // of the incoming values before they're represented by virtual registers. 12980b57cec5SDimitry Andric MF.getRegInfo().addLiveIn(WebAssembly::ARGUMENTS); 12990b57cec5SDimitry Andric 13005ffd83dbSDimitry Andric bool HasSwiftErrorArg = false; 13015ffd83dbSDimitry Andric bool HasSwiftSelfArg = false; 13020b57cec5SDimitry Andric for (const ISD::InputArg &In : Ins) { 13035ffd83dbSDimitry Andric HasSwiftSelfArg |= In.Flags.isSwiftSelf(); 13045ffd83dbSDimitry Andric HasSwiftErrorArg |= In.Flags.isSwiftError(); 13050b57cec5SDimitry Andric if (In.Flags.isInAlloca()) 13060b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented inalloca arguments"); 13070b57cec5SDimitry Andric if (In.Flags.isNest()) 13080b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented nest arguments"); 13090b57cec5SDimitry Andric if (In.Flags.isInConsecutiveRegs()) 13100b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented cons regs arguments"); 13110b57cec5SDimitry Andric if (In.Flags.isInConsecutiveRegsLast()) 13120b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented cons regs last arguments"); 13135ffd83dbSDimitry Andric // Ignore In.getNonZeroOrigAlign() because all our arguments are passed in 13140b57cec5SDimitry Andric // registers. 13150b57cec5SDimitry Andric InVals.push_back(In.Used ? DAG.getNode(WebAssemblyISD::ARGUMENT, DL, In.VT, 13160b57cec5SDimitry Andric DAG.getTargetConstant(InVals.size(), 13170b57cec5SDimitry Andric DL, MVT::i32)) 13180b57cec5SDimitry Andric : DAG.getUNDEF(In.VT)); 13190b57cec5SDimitry Andric 13200b57cec5SDimitry Andric // Record the number and types of arguments. 13210b57cec5SDimitry Andric MFI->addParam(In.VT); 13220b57cec5SDimitry Andric } 13230b57cec5SDimitry Andric 13245ffd83dbSDimitry Andric // For swiftcc, emit additional swiftself and swifterror arguments 13255ffd83dbSDimitry Andric // if there aren't. These additional arguments are also added for callee 13265ffd83dbSDimitry Andric // signature They are necessary to match callee and caller signature for 13275ffd83dbSDimitry Andric // indirect call. 13285ffd83dbSDimitry Andric auto PtrVT = getPointerTy(MF.getDataLayout()); 13295ffd83dbSDimitry Andric if (CallConv == CallingConv::Swift) { 13305ffd83dbSDimitry Andric if (!HasSwiftSelfArg) { 13315ffd83dbSDimitry Andric MFI->addParam(PtrVT); 13325ffd83dbSDimitry Andric } 13335ffd83dbSDimitry Andric if (!HasSwiftErrorArg) { 13345ffd83dbSDimitry Andric MFI->addParam(PtrVT); 13355ffd83dbSDimitry Andric } 13365ffd83dbSDimitry Andric } 13370b57cec5SDimitry Andric // Varargs are copied into a buffer allocated by the caller, and a pointer to 13380b57cec5SDimitry Andric // the buffer is passed as an argument. 13390b57cec5SDimitry Andric if (IsVarArg) { 13400b57cec5SDimitry Andric MVT PtrVT = getPointerTy(MF.getDataLayout()); 13418bcb0991SDimitry Andric Register VarargVreg = 13420b57cec5SDimitry Andric MF.getRegInfo().createVirtualRegister(getRegClassFor(PtrVT)); 13430b57cec5SDimitry Andric MFI->setVarargBufferVreg(VarargVreg); 13440b57cec5SDimitry Andric Chain = DAG.getCopyToReg( 13450b57cec5SDimitry Andric Chain, DL, VarargVreg, 13460b57cec5SDimitry Andric DAG.getNode(WebAssemblyISD::ARGUMENT, DL, PtrVT, 13470b57cec5SDimitry Andric DAG.getTargetConstant(Ins.size(), DL, MVT::i32))); 13480b57cec5SDimitry Andric MFI->addParam(PtrVT); 13490b57cec5SDimitry Andric } 13500b57cec5SDimitry Andric 13510b57cec5SDimitry Andric // Record the number and types of arguments and results. 13520b57cec5SDimitry Andric SmallVector<MVT, 4> Params; 13530b57cec5SDimitry Andric SmallVector<MVT, 4> Results; 13545ffd83dbSDimitry Andric computeSignatureVTs(MF.getFunction().getFunctionType(), &MF.getFunction(), 13555ffd83dbSDimitry Andric MF.getFunction(), DAG.getTarget(), Params, Results); 13560b57cec5SDimitry Andric for (MVT VT : Results) 13570b57cec5SDimitry Andric MFI->addResult(VT); 13580b57cec5SDimitry Andric // TODO: Use signatures in WebAssemblyMachineFunctionInfo too and unify 13590b57cec5SDimitry Andric // the param logic here with ComputeSignatureVTs 13600b57cec5SDimitry Andric assert(MFI->getParams().size() == Params.size() && 13610b57cec5SDimitry Andric std::equal(MFI->getParams().begin(), MFI->getParams().end(), 13620b57cec5SDimitry Andric Params.begin())); 13630b57cec5SDimitry Andric 13640b57cec5SDimitry Andric return Chain; 13650b57cec5SDimitry Andric } 13660b57cec5SDimitry Andric 13670b57cec5SDimitry Andric void WebAssemblyTargetLowering::ReplaceNodeResults( 13680b57cec5SDimitry Andric SDNode *N, SmallVectorImpl<SDValue> &Results, SelectionDAG &DAG) const { 13690b57cec5SDimitry Andric switch (N->getOpcode()) { 13700b57cec5SDimitry Andric case ISD::SIGN_EXTEND_INREG: 13710b57cec5SDimitry Andric // Do not add any results, signifying that N should not be custom lowered 13720b57cec5SDimitry Andric // after all. This happens because simd128 turns on custom lowering for 13730b57cec5SDimitry Andric // SIGN_EXTEND_INREG, but for non-vector sign extends the result might be an 13740b57cec5SDimitry Andric // illegal type. 13750b57cec5SDimitry Andric break; 13760b57cec5SDimitry Andric default: 13770b57cec5SDimitry Andric llvm_unreachable( 13780b57cec5SDimitry Andric "ReplaceNodeResults not implemented for this op for WebAssembly!"); 13790b57cec5SDimitry Andric } 13800b57cec5SDimitry Andric } 13810b57cec5SDimitry Andric 13820b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 13830b57cec5SDimitry Andric // Custom lowering hooks. 13840b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 13850b57cec5SDimitry Andric 13860b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerOperation(SDValue Op, 13870b57cec5SDimitry Andric SelectionDAG &DAG) const { 13880b57cec5SDimitry Andric SDLoc DL(Op); 13890b57cec5SDimitry Andric switch (Op.getOpcode()) { 13900b57cec5SDimitry Andric default: 13910b57cec5SDimitry Andric llvm_unreachable("unimplemented operation lowering"); 13920b57cec5SDimitry Andric return SDValue(); 13930b57cec5SDimitry Andric case ISD::FrameIndex: 13940b57cec5SDimitry Andric return LowerFrameIndex(Op, DAG); 13950b57cec5SDimitry Andric case ISD::GlobalAddress: 13960b57cec5SDimitry Andric return LowerGlobalAddress(Op, DAG); 1397e8d8bef9SDimitry Andric case ISD::GlobalTLSAddress: 1398e8d8bef9SDimitry Andric return LowerGlobalTLSAddress(Op, DAG); 13990b57cec5SDimitry Andric case ISD::ExternalSymbol: 14000b57cec5SDimitry Andric return LowerExternalSymbol(Op, DAG); 14010b57cec5SDimitry Andric case ISD::JumpTable: 14020b57cec5SDimitry Andric return LowerJumpTable(Op, DAG); 14030b57cec5SDimitry Andric case ISD::BR_JT: 14040b57cec5SDimitry Andric return LowerBR_JT(Op, DAG); 14050b57cec5SDimitry Andric case ISD::VASTART: 14060b57cec5SDimitry Andric return LowerVASTART(Op, DAG); 14070b57cec5SDimitry Andric case ISD::BlockAddress: 14080b57cec5SDimitry Andric case ISD::BRIND: 14090b57cec5SDimitry Andric fail(DL, DAG, "WebAssembly hasn't implemented computed gotos"); 14100b57cec5SDimitry Andric return SDValue(); 14110b57cec5SDimitry Andric case ISD::RETURNADDR: 14120b57cec5SDimitry Andric return LowerRETURNADDR(Op, DAG); 14130b57cec5SDimitry Andric case ISD::FRAMEADDR: 14140b57cec5SDimitry Andric return LowerFRAMEADDR(Op, DAG); 14150b57cec5SDimitry Andric case ISD::CopyToReg: 14160b57cec5SDimitry Andric return LowerCopyToReg(Op, DAG); 14170b57cec5SDimitry Andric case ISD::EXTRACT_VECTOR_ELT: 14180b57cec5SDimitry Andric case ISD::INSERT_VECTOR_ELT: 14190b57cec5SDimitry Andric return LowerAccessVectorElement(Op, DAG); 14200b57cec5SDimitry Andric case ISD::INTRINSIC_VOID: 14210b57cec5SDimitry Andric case ISD::INTRINSIC_WO_CHAIN: 14220b57cec5SDimitry Andric case ISD::INTRINSIC_W_CHAIN: 14230b57cec5SDimitry Andric return LowerIntrinsic(Op, DAG); 14240b57cec5SDimitry Andric case ISD::SIGN_EXTEND_INREG: 14250b57cec5SDimitry Andric return LowerSIGN_EXTEND_INREG(Op, DAG); 14260b57cec5SDimitry Andric case ISD::BUILD_VECTOR: 14270b57cec5SDimitry Andric return LowerBUILD_VECTOR(Op, DAG); 14280b57cec5SDimitry Andric case ISD::VECTOR_SHUFFLE: 14290b57cec5SDimitry Andric return LowerVECTOR_SHUFFLE(Op, DAG); 1430480093f4SDimitry Andric case ISD::SETCC: 1431480093f4SDimitry Andric return LowerSETCC(Op, DAG); 14320b57cec5SDimitry Andric case ISD::SHL: 14330b57cec5SDimitry Andric case ISD::SRA: 14340b57cec5SDimitry Andric case ISD::SRL: 14350b57cec5SDimitry Andric return LowerShift(Op, DAG); 1436fe6060f1SDimitry Andric case ISD::FP_TO_SINT_SAT: 1437fe6060f1SDimitry Andric case ISD::FP_TO_UINT_SAT: 1438fe6060f1SDimitry Andric return LowerFP_TO_INT_SAT(Op, DAG); 1439fe6060f1SDimitry Andric case ISD::LOAD: 1440fe6060f1SDimitry Andric return LowerLoad(Op, DAG); 1441fe6060f1SDimitry Andric case ISD::STORE: 1442fe6060f1SDimitry Andric return LowerStore(Op, DAG); 1443349cc55cSDimitry Andric case ISD::CTPOP: 1444349cc55cSDimitry Andric case ISD::CTLZ: 1445349cc55cSDimitry Andric case ISD::CTTZ: 1446349cc55cSDimitry Andric return DAG.UnrollVectorOp(Op.getNode()); 14470b57cec5SDimitry Andric } 14480b57cec5SDimitry Andric } 14490b57cec5SDimitry Andric 1450fe6060f1SDimitry Andric static bool IsWebAssemblyGlobal(SDValue Op) { 1451fe6060f1SDimitry Andric if (const GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op)) 1452fe6060f1SDimitry Andric return WebAssembly::isWasmVarAddressSpace(GA->getAddressSpace()); 1453fe6060f1SDimitry Andric 1454fe6060f1SDimitry Andric return false; 1455fe6060f1SDimitry Andric } 1456fe6060f1SDimitry Andric 1457*bdd1243dSDimitry Andric static std::optional<unsigned> IsWebAssemblyLocal(SDValue Op, 1458*bdd1243dSDimitry Andric SelectionDAG &DAG) { 1459fe6060f1SDimitry Andric const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op); 1460fe6060f1SDimitry Andric if (!FI) 1461*bdd1243dSDimitry Andric return std::nullopt; 1462fe6060f1SDimitry Andric 1463fe6060f1SDimitry Andric auto &MF = DAG.getMachineFunction(); 1464fe6060f1SDimitry Andric return WebAssemblyFrameLowering::getLocalForStackObject(MF, FI->getIndex()); 1465fe6060f1SDimitry Andric } 1466fe6060f1SDimitry Andric 1467fe6060f1SDimitry Andric SDValue WebAssemblyTargetLowering::LowerStore(SDValue Op, 1468fe6060f1SDimitry Andric SelectionDAG &DAG) const { 1469fe6060f1SDimitry Andric SDLoc DL(Op); 1470fe6060f1SDimitry Andric StoreSDNode *SN = cast<StoreSDNode>(Op.getNode()); 1471fe6060f1SDimitry Andric const SDValue &Value = SN->getValue(); 1472fe6060f1SDimitry Andric const SDValue &Base = SN->getBasePtr(); 1473fe6060f1SDimitry Andric const SDValue &Offset = SN->getOffset(); 1474fe6060f1SDimitry Andric 1475fe6060f1SDimitry Andric if (IsWebAssemblyGlobal(Base)) { 1476fe6060f1SDimitry Andric if (!Offset->isUndef()) 1477fe6060f1SDimitry Andric report_fatal_error("unexpected offset when storing to webassembly global", 1478fe6060f1SDimitry Andric false); 1479fe6060f1SDimitry Andric 1480fe6060f1SDimitry Andric SDVTList Tys = DAG.getVTList(MVT::Other); 1481fe6060f1SDimitry Andric SDValue Ops[] = {SN->getChain(), Value, Base}; 1482fe6060f1SDimitry Andric return DAG.getMemIntrinsicNode(WebAssemblyISD::GLOBAL_SET, DL, Tys, Ops, 1483fe6060f1SDimitry Andric SN->getMemoryVT(), SN->getMemOperand()); 1484fe6060f1SDimitry Andric } 1485fe6060f1SDimitry Andric 1486*bdd1243dSDimitry Andric if (std::optional<unsigned> Local = IsWebAssemblyLocal(Base, DAG)) { 1487fe6060f1SDimitry Andric if (!Offset->isUndef()) 1488fe6060f1SDimitry Andric report_fatal_error("unexpected offset when storing to webassembly local", 1489fe6060f1SDimitry Andric false); 1490fe6060f1SDimitry Andric 1491fe6060f1SDimitry Andric SDValue Idx = DAG.getTargetConstant(*Local, Base, MVT::i32); 1492fe6060f1SDimitry Andric SDVTList Tys = DAG.getVTList(MVT::Other); // The chain. 1493fe6060f1SDimitry Andric SDValue Ops[] = {SN->getChain(), Idx, Value}; 1494fe6060f1SDimitry Andric return DAG.getNode(WebAssemblyISD::LOCAL_SET, DL, Tys, Ops); 1495fe6060f1SDimitry Andric } 1496fe6060f1SDimitry Andric 1497*bdd1243dSDimitry Andric if (WebAssembly::isWasmVarAddressSpace(SN->getAddressSpace())) 1498*bdd1243dSDimitry Andric report_fatal_error( 1499*bdd1243dSDimitry Andric "Encountered an unlowerable store to the wasm_var address space", 1500*bdd1243dSDimitry Andric false); 1501*bdd1243dSDimitry Andric 1502fe6060f1SDimitry Andric return Op; 1503fe6060f1SDimitry Andric } 1504fe6060f1SDimitry Andric 1505fe6060f1SDimitry Andric SDValue WebAssemblyTargetLowering::LowerLoad(SDValue Op, 1506fe6060f1SDimitry Andric SelectionDAG &DAG) const { 1507fe6060f1SDimitry Andric SDLoc DL(Op); 1508fe6060f1SDimitry Andric LoadSDNode *LN = cast<LoadSDNode>(Op.getNode()); 1509fe6060f1SDimitry Andric const SDValue &Base = LN->getBasePtr(); 1510fe6060f1SDimitry Andric const SDValue &Offset = LN->getOffset(); 1511fe6060f1SDimitry Andric 1512fe6060f1SDimitry Andric if (IsWebAssemblyGlobal(Base)) { 1513fe6060f1SDimitry Andric if (!Offset->isUndef()) 1514fe6060f1SDimitry Andric report_fatal_error( 1515fe6060f1SDimitry Andric "unexpected offset when loading from webassembly global", false); 1516fe6060f1SDimitry Andric 1517fe6060f1SDimitry Andric SDVTList Tys = DAG.getVTList(LN->getValueType(0), MVT::Other); 1518fe6060f1SDimitry Andric SDValue Ops[] = {LN->getChain(), Base}; 1519fe6060f1SDimitry Andric return DAG.getMemIntrinsicNode(WebAssemblyISD::GLOBAL_GET, DL, Tys, Ops, 1520fe6060f1SDimitry Andric LN->getMemoryVT(), LN->getMemOperand()); 1521fe6060f1SDimitry Andric } 1522fe6060f1SDimitry Andric 1523*bdd1243dSDimitry Andric if (std::optional<unsigned> Local = IsWebAssemblyLocal(Base, DAG)) { 1524fe6060f1SDimitry Andric if (!Offset->isUndef()) 1525fe6060f1SDimitry Andric report_fatal_error( 1526fe6060f1SDimitry Andric "unexpected offset when loading from webassembly local", false); 1527fe6060f1SDimitry Andric 1528fe6060f1SDimitry Andric SDValue Idx = DAG.getTargetConstant(*Local, Base, MVT::i32); 1529fe6060f1SDimitry Andric EVT LocalVT = LN->getValueType(0); 1530fe6060f1SDimitry Andric SDValue LocalGet = DAG.getNode(WebAssemblyISD::LOCAL_GET, DL, LocalVT, 1531fe6060f1SDimitry Andric {LN->getChain(), Idx}); 1532fe6060f1SDimitry Andric SDValue Result = DAG.getMergeValues({LocalGet, LN->getChain()}, DL); 1533fe6060f1SDimitry Andric assert(Result->getNumValues() == 2 && "Loads must carry a chain!"); 1534fe6060f1SDimitry Andric return Result; 1535fe6060f1SDimitry Andric } 1536fe6060f1SDimitry Andric 1537*bdd1243dSDimitry Andric if (WebAssembly::isWasmVarAddressSpace(LN->getAddressSpace())) 1538*bdd1243dSDimitry Andric report_fatal_error( 1539*bdd1243dSDimitry Andric "Encountered an unlowerable load from the wasm_var address space", 1540*bdd1243dSDimitry Andric false); 1541*bdd1243dSDimitry Andric 1542fe6060f1SDimitry Andric return Op; 1543fe6060f1SDimitry Andric } 1544fe6060f1SDimitry Andric 15450b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerCopyToReg(SDValue Op, 15460b57cec5SDimitry Andric SelectionDAG &DAG) const { 15470b57cec5SDimitry Andric SDValue Src = Op.getOperand(2); 15480b57cec5SDimitry Andric if (isa<FrameIndexSDNode>(Src.getNode())) { 15490b57cec5SDimitry Andric // CopyToReg nodes don't support FrameIndex operands. Other targets select 15500b57cec5SDimitry Andric // the FI to some LEA-like instruction, but since we don't have that, we 15510b57cec5SDimitry Andric // need to insert some kind of instruction that can take an FI operand and 15520b57cec5SDimitry Andric // produces a value usable by CopyToReg (i.e. in a vreg). So insert a dummy 15530b57cec5SDimitry Andric // local.copy between Op and its FI operand. 15540b57cec5SDimitry Andric SDValue Chain = Op.getOperand(0); 15550b57cec5SDimitry Andric SDLoc DL(Op); 155604eeddc0SDimitry Andric Register Reg = cast<RegisterSDNode>(Op.getOperand(1))->getReg(); 15570b57cec5SDimitry Andric EVT VT = Src.getValueType(); 15580b57cec5SDimitry Andric SDValue Copy(DAG.getMachineNode(VT == MVT::i32 ? WebAssembly::COPY_I32 15590b57cec5SDimitry Andric : WebAssembly::COPY_I64, 15600b57cec5SDimitry Andric DL, VT, Src), 15610b57cec5SDimitry Andric 0); 15620b57cec5SDimitry Andric return Op.getNode()->getNumValues() == 1 15630b57cec5SDimitry Andric ? DAG.getCopyToReg(Chain, DL, Reg, Copy) 15640b57cec5SDimitry Andric : DAG.getCopyToReg(Chain, DL, Reg, Copy, 15650b57cec5SDimitry Andric Op.getNumOperands() == 4 ? Op.getOperand(3) 15660b57cec5SDimitry Andric : SDValue()); 15670b57cec5SDimitry Andric } 15680b57cec5SDimitry Andric return SDValue(); 15690b57cec5SDimitry Andric } 15700b57cec5SDimitry Andric 15710b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerFrameIndex(SDValue Op, 15720b57cec5SDimitry Andric SelectionDAG &DAG) const { 15730b57cec5SDimitry Andric int FI = cast<FrameIndexSDNode>(Op)->getIndex(); 15740b57cec5SDimitry Andric return DAG.getTargetFrameIndex(FI, Op.getValueType()); 15750b57cec5SDimitry Andric } 15760b57cec5SDimitry Andric 15770b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerRETURNADDR(SDValue Op, 15780b57cec5SDimitry Andric SelectionDAG &DAG) const { 15790b57cec5SDimitry Andric SDLoc DL(Op); 15800b57cec5SDimitry Andric 15810b57cec5SDimitry Andric if (!Subtarget->getTargetTriple().isOSEmscripten()) { 15820b57cec5SDimitry Andric fail(DL, DAG, 15830b57cec5SDimitry Andric "Non-Emscripten WebAssembly hasn't implemented " 15840b57cec5SDimitry Andric "__builtin_return_address"); 15850b57cec5SDimitry Andric return SDValue(); 15860b57cec5SDimitry Andric } 15870b57cec5SDimitry Andric 15880b57cec5SDimitry Andric if (verifyReturnAddressArgumentIsConstant(Op, DAG)) 15890b57cec5SDimitry Andric return SDValue(); 15900b57cec5SDimitry Andric 1591349cc55cSDimitry Andric unsigned Depth = Op.getConstantOperandVal(0); 15928bcb0991SDimitry Andric MakeLibCallOptions CallOptions; 15930b57cec5SDimitry Andric return makeLibCall(DAG, RTLIB::RETURN_ADDRESS, Op.getValueType(), 15948bcb0991SDimitry Andric {DAG.getConstant(Depth, DL, MVT::i32)}, CallOptions, DL) 15950b57cec5SDimitry Andric .first; 15960b57cec5SDimitry Andric } 15970b57cec5SDimitry Andric 15980b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerFRAMEADDR(SDValue Op, 15990b57cec5SDimitry Andric SelectionDAG &DAG) const { 16000b57cec5SDimitry Andric // Non-zero depths are not supported by WebAssembly currently. Use the 16010b57cec5SDimitry Andric // legalizer's default expansion, which is to return 0 (what this function is 16020b57cec5SDimitry Andric // documented to do). 16030b57cec5SDimitry Andric if (Op.getConstantOperandVal(0) > 0) 16040b57cec5SDimitry Andric return SDValue(); 16050b57cec5SDimitry Andric 16060b57cec5SDimitry Andric DAG.getMachineFunction().getFrameInfo().setFrameAddressIsTaken(true); 16070b57cec5SDimitry Andric EVT VT = Op.getValueType(); 16088bcb0991SDimitry Andric Register FP = 16090b57cec5SDimitry Andric Subtarget->getRegisterInfo()->getFrameRegister(DAG.getMachineFunction()); 16100b57cec5SDimitry Andric return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), FP, VT); 16110b57cec5SDimitry Andric } 16120b57cec5SDimitry Andric 1613e8d8bef9SDimitry Andric SDValue 1614e8d8bef9SDimitry Andric WebAssemblyTargetLowering::LowerGlobalTLSAddress(SDValue Op, 1615e8d8bef9SDimitry Andric SelectionDAG &DAG) const { 1616e8d8bef9SDimitry Andric SDLoc DL(Op); 1617e8d8bef9SDimitry Andric const auto *GA = cast<GlobalAddressSDNode>(Op); 1618e8d8bef9SDimitry Andric 1619e8d8bef9SDimitry Andric MachineFunction &MF = DAG.getMachineFunction(); 1620e8d8bef9SDimitry Andric if (!MF.getSubtarget<WebAssemblySubtarget>().hasBulkMemory()) 1621e8d8bef9SDimitry Andric report_fatal_error("cannot use thread-local storage without bulk memory", 1622e8d8bef9SDimitry Andric false); 1623e8d8bef9SDimitry Andric 1624e8d8bef9SDimitry Andric const GlobalValue *GV = GA->getGlobal(); 1625e8d8bef9SDimitry Andric 1626972a253aSDimitry Andric // Currently only Emscripten supports dynamic linking with threads. Therefore, 1627972a253aSDimitry Andric // on other targets, if we have thread-local storage, only the local-exec 1628972a253aSDimitry Andric // model is possible. 1629972a253aSDimitry Andric auto model = Subtarget->getTargetTriple().isOSEmscripten() 1630972a253aSDimitry Andric ? GV->getThreadLocalMode() 1631972a253aSDimitry Andric : GlobalValue::LocalExecTLSModel; 1632349cc55cSDimitry Andric 1633349cc55cSDimitry Andric // Unsupported TLS modes 1634349cc55cSDimitry Andric assert(model != GlobalValue::NotThreadLocal); 1635349cc55cSDimitry Andric assert(model != GlobalValue::InitialExecTLSModel); 1636349cc55cSDimitry Andric 1637349cc55cSDimitry Andric if (model == GlobalValue::LocalExecTLSModel || 1638349cc55cSDimitry Andric model == GlobalValue::LocalDynamicTLSModel || 1639349cc55cSDimitry Andric (model == GlobalValue::GeneralDynamicTLSModel && 1640349cc55cSDimitry Andric getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV))) { 1641349cc55cSDimitry Andric // For DSO-local TLS variables we use offset from __tls_base 1642349cc55cSDimitry Andric 1643349cc55cSDimitry Andric MVT PtrVT = getPointerTy(DAG.getDataLayout()); 1644e8d8bef9SDimitry Andric auto GlobalGet = PtrVT == MVT::i64 ? WebAssembly::GLOBAL_GET_I64 1645e8d8bef9SDimitry Andric : WebAssembly::GLOBAL_GET_I32; 1646e8d8bef9SDimitry Andric const char *BaseName = MF.createExternalSymbolName("__tls_base"); 1647e8d8bef9SDimitry Andric 1648e8d8bef9SDimitry Andric SDValue BaseAddr( 1649e8d8bef9SDimitry Andric DAG.getMachineNode(GlobalGet, DL, PtrVT, 1650e8d8bef9SDimitry Andric DAG.getTargetExternalSymbol(BaseName, PtrVT)), 1651e8d8bef9SDimitry Andric 0); 1652e8d8bef9SDimitry Andric 1653e8d8bef9SDimitry Andric SDValue TLSOffset = DAG.getTargetGlobalAddress( 1654e8d8bef9SDimitry Andric GV, DL, PtrVT, GA->getOffset(), WebAssemblyII::MO_TLS_BASE_REL); 1655349cc55cSDimitry Andric SDValue SymOffset = 1656349cc55cSDimitry Andric DAG.getNode(WebAssemblyISD::WrapperREL, DL, PtrVT, TLSOffset); 1657e8d8bef9SDimitry Andric 1658349cc55cSDimitry Andric return DAG.getNode(ISD::ADD, DL, PtrVT, BaseAddr, SymOffset); 1659349cc55cSDimitry Andric } 1660349cc55cSDimitry Andric 1661349cc55cSDimitry Andric assert(model == GlobalValue::GeneralDynamicTLSModel); 1662349cc55cSDimitry Andric 1663349cc55cSDimitry Andric EVT VT = Op.getValueType(); 1664349cc55cSDimitry Andric return DAG.getNode(WebAssemblyISD::Wrapper, DL, VT, 1665349cc55cSDimitry Andric DAG.getTargetGlobalAddress(GA->getGlobal(), DL, VT, 1666349cc55cSDimitry Andric GA->getOffset(), 1667349cc55cSDimitry Andric WebAssemblyII::MO_GOT_TLS)); 1668e8d8bef9SDimitry Andric } 1669e8d8bef9SDimitry Andric 16700b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerGlobalAddress(SDValue Op, 16710b57cec5SDimitry Andric SelectionDAG &DAG) const { 16720b57cec5SDimitry Andric SDLoc DL(Op); 16730b57cec5SDimitry Andric const auto *GA = cast<GlobalAddressSDNode>(Op); 16740b57cec5SDimitry Andric EVT VT = Op.getValueType(); 16750b57cec5SDimitry Andric assert(GA->getTargetFlags() == 0 && 16760b57cec5SDimitry Andric "Unexpected target flags on generic GlobalAddressSDNode"); 1677fe6060f1SDimitry Andric if (!WebAssembly::isValidAddressSpace(GA->getAddressSpace())) 1678fe6060f1SDimitry Andric fail(DL, DAG, "Invalid address space for WebAssembly target"); 16790b57cec5SDimitry Andric 16800b57cec5SDimitry Andric unsigned OperandFlags = 0; 16810b57cec5SDimitry Andric if (isPositionIndependent()) { 16820b57cec5SDimitry Andric const GlobalValue *GV = GA->getGlobal(); 16830b57cec5SDimitry Andric if (getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV)) { 16840b57cec5SDimitry Andric MachineFunction &MF = DAG.getMachineFunction(); 16850b57cec5SDimitry Andric MVT PtrVT = getPointerTy(MF.getDataLayout()); 16860b57cec5SDimitry Andric const char *BaseName; 16870b57cec5SDimitry Andric if (GV->getValueType()->isFunctionTy()) { 16880b57cec5SDimitry Andric BaseName = MF.createExternalSymbolName("__table_base"); 16890b57cec5SDimitry Andric OperandFlags = WebAssemblyII::MO_TABLE_BASE_REL; 1690972a253aSDimitry Andric } else { 16910b57cec5SDimitry Andric BaseName = MF.createExternalSymbolName("__memory_base"); 16920b57cec5SDimitry Andric OperandFlags = WebAssemblyII::MO_MEMORY_BASE_REL; 16930b57cec5SDimitry Andric } 16940b57cec5SDimitry Andric SDValue BaseAddr = 16950b57cec5SDimitry Andric DAG.getNode(WebAssemblyISD::Wrapper, DL, PtrVT, 16960b57cec5SDimitry Andric DAG.getTargetExternalSymbol(BaseName, PtrVT)); 16970b57cec5SDimitry Andric 16980b57cec5SDimitry Andric SDValue SymAddr = DAG.getNode( 1699349cc55cSDimitry Andric WebAssemblyISD::WrapperREL, DL, VT, 17000b57cec5SDimitry Andric DAG.getTargetGlobalAddress(GA->getGlobal(), DL, VT, GA->getOffset(), 17010b57cec5SDimitry Andric OperandFlags)); 17020b57cec5SDimitry Andric 17030b57cec5SDimitry Andric return DAG.getNode(ISD::ADD, DL, VT, BaseAddr, SymAddr); 17040b57cec5SDimitry Andric } 1705349cc55cSDimitry Andric OperandFlags = WebAssemblyII::MO_GOT; 17060b57cec5SDimitry Andric } 17070b57cec5SDimitry Andric 17080b57cec5SDimitry Andric return DAG.getNode(WebAssemblyISD::Wrapper, DL, VT, 17090b57cec5SDimitry Andric DAG.getTargetGlobalAddress(GA->getGlobal(), DL, VT, 17100b57cec5SDimitry Andric GA->getOffset(), OperandFlags)); 17110b57cec5SDimitry Andric } 17120b57cec5SDimitry Andric 17130b57cec5SDimitry Andric SDValue 17140b57cec5SDimitry Andric WebAssemblyTargetLowering::LowerExternalSymbol(SDValue Op, 17150b57cec5SDimitry Andric SelectionDAG &DAG) const { 17160b57cec5SDimitry Andric SDLoc DL(Op); 17170b57cec5SDimitry Andric const auto *ES = cast<ExternalSymbolSDNode>(Op); 17180b57cec5SDimitry Andric EVT VT = Op.getValueType(); 17190b57cec5SDimitry Andric assert(ES->getTargetFlags() == 0 && 17200b57cec5SDimitry Andric "Unexpected target flags on generic ExternalSymbolSDNode"); 17210b57cec5SDimitry Andric return DAG.getNode(WebAssemblyISD::Wrapper, DL, VT, 17220b57cec5SDimitry Andric DAG.getTargetExternalSymbol(ES->getSymbol(), VT)); 17230b57cec5SDimitry Andric } 17240b57cec5SDimitry Andric 17250b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerJumpTable(SDValue Op, 17260b57cec5SDimitry Andric SelectionDAG &DAG) const { 17270b57cec5SDimitry Andric // There's no need for a Wrapper node because we always incorporate a jump 17280b57cec5SDimitry Andric // table operand into a BR_TABLE instruction, rather than ever 17290b57cec5SDimitry Andric // materializing it in a register. 17300b57cec5SDimitry Andric const JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); 17310b57cec5SDimitry Andric return DAG.getTargetJumpTable(JT->getIndex(), Op.getValueType(), 17320b57cec5SDimitry Andric JT->getTargetFlags()); 17330b57cec5SDimitry Andric } 17340b57cec5SDimitry Andric 17350b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerBR_JT(SDValue Op, 17360b57cec5SDimitry Andric SelectionDAG &DAG) const { 17370b57cec5SDimitry Andric SDLoc DL(Op); 17380b57cec5SDimitry Andric SDValue Chain = Op.getOperand(0); 17390b57cec5SDimitry Andric const auto *JT = cast<JumpTableSDNode>(Op.getOperand(1)); 17400b57cec5SDimitry Andric SDValue Index = Op.getOperand(2); 17410b57cec5SDimitry Andric assert(JT->getTargetFlags() == 0 && "WebAssembly doesn't set target flags"); 17420b57cec5SDimitry Andric 17430b57cec5SDimitry Andric SmallVector<SDValue, 8> Ops; 17440b57cec5SDimitry Andric Ops.push_back(Chain); 17450b57cec5SDimitry Andric Ops.push_back(Index); 17460b57cec5SDimitry Andric 17470b57cec5SDimitry Andric MachineJumpTableInfo *MJTI = DAG.getMachineFunction().getJumpTableInfo(); 17480b57cec5SDimitry Andric const auto &MBBs = MJTI->getJumpTables()[JT->getIndex()].MBBs; 17490b57cec5SDimitry Andric 17500b57cec5SDimitry Andric // Add an operand for each case. 1751*bdd1243dSDimitry Andric for (auto *MBB : MBBs) 17520b57cec5SDimitry Andric Ops.push_back(DAG.getBasicBlock(MBB)); 17530b57cec5SDimitry Andric 17545ffd83dbSDimitry Andric // Add the first MBB as a dummy default target for now. This will be replaced 17555ffd83dbSDimitry Andric // with the proper default target (and the preceding range check eliminated) 17565ffd83dbSDimitry Andric // if possible by WebAssemblyFixBrTableDefaults. 17575ffd83dbSDimitry Andric Ops.push_back(DAG.getBasicBlock(*MBBs.begin())); 17580b57cec5SDimitry Andric return DAG.getNode(WebAssemblyISD::BR_TABLE, DL, MVT::Other, Ops); 17590b57cec5SDimitry Andric } 17600b57cec5SDimitry Andric 17610b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerVASTART(SDValue Op, 17620b57cec5SDimitry Andric SelectionDAG &DAG) const { 17630b57cec5SDimitry Andric SDLoc DL(Op); 17640b57cec5SDimitry Andric EVT PtrVT = getPointerTy(DAG.getMachineFunction().getDataLayout()); 17650b57cec5SDimitry Andric 17660b57cec5SDimitry Andric auto *MFI = DAG.getMachineFunction().getInfo<WebAssemblyFunctionInfo>(); 17670b57cec5SDimitry Andric const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 17680b57cec5SDimitry Andric 17690b57cec5SDimitry Andric SDValue ArgN = DAG.getCopyFromReg(DAG.getEntryNode(), DL, 17700b57cec5SDimitry Andric MFI->getVarargBufferVreg(), PtrVT); 17710b57cec5SDimitry Andric return DAG.getStore(Op.getOperand(0), DL, ArgN, Op.getOperand(1), 1772e8d8bef9SDimitry Andric MachinePointerInfo(SV)); 1773e8d8bef9SDimitry Andric } 1774e8d8bef9SDimitry Andric 17750b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerIntrinsic(SDValue Op, 17760b57cec5SDimitry Andric SelectionDAG &DAG) const { 17770b57cec5SDimitry Andric MachineFunction &MF = DAG.getMachineFunction(); 17780b57cec5SDimitry Andric unsigned IntNo; 17790b57cec5SDimitry Andric switch (Op.getOpcode()) { 17800b57cec5SDimitry Andric case ISD::INTRINSIC_VOID: 17810b57cec5SDimitry Andric case ISD::INTRINSIC_W_CHAIN: 1782349cc55cSDimitry Andric IntNo = Op.getConstantOperandVal(1); 17830b57cec5SDimitry Andric break; 17840b57cec5SDimitry Andric case ISD::INTRINSIC_WO_CHAIN: 1785349cc55cSDimitry Andric IntNo = Op.getConstantOperandVal(0); 17860b57cec5SDimitry Andric break; 17870b57cec5SDimitry Andric default: 17880b57cec5SDimitry Andric llvm_unreachable("Invalid intrinsic"); 17890b57cec5SDimitry Andric } 17900b57cec5SDimitry Andric SDLoc DL(Op); 17910b57cec5SDimitry Andric 17920b57cec5SDimitry Andric switch (IntNo) { 17930b57cec5SDimitry Andric default: 17940b57cec5SDimitry Andric return SDValue(); // Don't custom lower most intrinsics. 17950b57cec5SDimitry Andric 17960b57cec5SDimitry Andric case Intrinsic::wasm_lsda: { 1797349cc55cSDimitry Andric auto PtrVT = getPointerTy(MF.getDataLayout()); 1798349cc55cSDimitry Andric const char *SymName = MF.createExternalSymbolName( 1799349cc55cSDimitry Andric "GCC_except_table" + std::to_string(MF.getFunctionNumber())); 1800349cc55cSDimitry Andric if (isPositionIndependent()) { 1801349cc55cSDimitry Andric SDValue Node = DAG.getTargetExternalSymbol( 1802349cc55cSDimitry Andric SymName, PtrVT, WebAssemblyII::MO_MEMORY_BASE_REL); 1803349cc55cSDimitry Andric const char *BaseName = MF.createExternalSymbolName("__memory_base"); 1804349cc55cSDimitry Andric SDValue BaseAddr = 1805349cc55cSDimitry Andric DAG.getNode(WebAssemblyISD::Wrapper, DL, PtrVT, 1806349cc55cSDimitry Andric DAG.getTargetExternalSymbol(BaseName, PtrVT)); 1807349cc55cSDimitry Andric SDValue SymAddr = 1808349cc55cSDimitry Andric DAG.getNode(WebAssemblyISD::WrapperREL, DL, PtrVT, Node); 1809349cc55cSDimitry Andric return DAG.getNode(ISD::ADD, DL, PtrVT, BaseAddr, SymAddr); 18100b57cec5SDimitry Andric } 1811349cc55cSDimitry Andric SDValue Node = DAG.getTargetExternalSymbol(SymName, PtrVT); 1812349cc55cSDimitry Andric return DAG.getNode(WebAssemblyISD::Wrapper, DL, PtrVT, Node); 1813e8d8bef9SDimitry Andric } 1814e8d8bef9SDimitry Andric 18155ffd83dbSDimitry Andric case Intrinsic::wasm_shuffle: { 18165ffd83dbSDimitry Andric // Drop in-chain and replace undefs, but otherwise pass through unchanged 18175ffd83dbSDimitry Andric SDValue Ops[18]; 18185ffd83dbSDimitry Andric size_t OpIdx = 0; 18195ffd83dbSDimitry Andric Ops[OpIdx++] = Op.getOperand(1); 18205ffd83dbSDimitry Andric Ops[OpIdx++] = Op.getOperand(2); 18215ffd83dbSDimitry Andric while (OpIdx < 18) { 18225ffd83dbSDimitry Andric const SDValue &MaskIdx = Op.getOperand(OpIdx + 1); 18235ffd83dbSDimitry Andric if (MaskIdx.isUndef() || 18245ffd83dbSDimitry Andric cast<ConstantSDNode>(MaskIdx.getNode())->getZExtValue() >= 32) { 18255ffd83dbSDimitry Andric Ops[OpIdx++] = DAG.getConstant(0, DL, MVT::i32); 18265ffd83dbSDimitry Andric } else { 18275ffd83dbSDimitry Andric Ops[OpIdx++] = MaskIdx; 18285ffd83dbSDimitry Andric } 18295ffd83dbSDimitry Andric } 18305ffd83dbSDimitry Andric return DAG.getNode(WebAssemblyISD::SHUFFLE, DL, Op.getValueType(), Ops); 18315ffd83dbSDimitry Andric } 18320b57cec5SDimitry Andric } 18330b57cec5SDimitry Andric } 18340b57cec5SDimitry Andric 18350b57cec5SDimitry Andric SDValue 18360b57cec5SDimitry Andric WebAssemblyTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, 18370b57cec5SDimitry Andric SelectionDAG &DAG) const { 18380b57cec5SDimitry Andric SDLoc DL(Op); 18390b57cec5SDimitry Andric // If sign extension operations are disabled, allow sext_inreg only if operand 18405ffd83dbSDimitry Andric // is a vector extract of an i8 or i16 lane. SIMD does not depend on sign 18415ffd83dbSDimitry Andric // extension operations, but allowing sext_inreg in this context lets us have 18425ffd83dbSDimitry Andric // simple patterns to select extract_lane_s instructions. Expanding sext_inreg 18435ffd83dbSDimitry Andric // everywhere would be simpler in this file, but would necessitate large and 18445ffd83dbSDimitry Andric // brittle patterns to undo the expansion and select extract_lane_s 18455ffd83dbSDimitry Andric // instructions. 18460b57cec5SDimitry Andric assert(!Subtarget->hasSignExt() && Subtarget->hasSIMD128()); 18475ffd83dbSDimitry Andric if (Op.getOperand(0).getOpcode() != ISD::EXTRACT_VECTOR_ELT) 18485ffd83dbSDimitry Andric return SDValue(); 18495ffd83dbSDimitry Andric 18500b57cec5SDimitry Andric const SDValue &Extract = Op.getOperand(0); 18510b57cec5SDimitry Andric MVT VecT = Extract.getOperand(0).getSimpleValueType(); 18525ffd83dbSDimitry Andric if (VecT.getVectorElementType().getSizeInBits() > 32) 18535ffd83dbSDimitry Andric return SDValue(); 18545ffd83dbSDimitry Andric MVT ExtractedLaneT = 18555ffd83dbSDimitry Andric cast<VTSDNode>(Op.getOperand(1).getNode())->getVT().getSimpleVT(); 18560b57cec5SDimitry Andric MVT ExtractedVecT = 18570b57cec5SDimitry Andric MVT::getVectorVT(ExtractedLaneT, 128 / ExtractedLaneT.getSizeInBits()); 18580b57cec5SDimitry Andric if (ExtractedVecT == VecT) 18590b57cec5SDimitry Andric return Op; 18605ffd83dbSDimitry Andric 18610b57cec5SDimitry Andric // Bitcast vector to appropriate type to ensure ISel pattern coverage 18625ffd83dbSDimitry Andric const SDNode *Index = Extract.getOperand(1).getNode(); 18635ffd83dbSDimitry Andric if (!isa<ConstantSDNode>(Index)) 18645ffd83dbSDimitry Andric return SDValue(); 18655ffd83dbSDimitry Andric unsigned IndexVal = cast<ConstantSDNode>(Index)->getZExtValue(); 18660b57cec5SDimitry Andric unsigned Scale = 18670b57cec5SDimitry Andric ExtractedVecT.getVectorNumElements() / VecT.getVectorNumElements(); 18680b57cec5SDimitry Andric assert(Scale > 1); 18690b57cec5SDimitry Andric SDValue NewIndex = 18705ffd83dbSDimitry Andric DAG.getConstant(IndexVal * Scale, DL, Index->getValueType(0)); 18710b57cec5SDimitry Andric SDValue NewExtract = DAG.getNode( 18720b57cec5SDimitry Andric ISD::EXTRACT_VECTOR_ELT, DL, Extract.getValueType(), 18730b57cec5SDimitry Andric DAG.getBitcast(ExtractedVecT, Extract.getOperand(0)), NewIndex); 18745ffd83dbSDimitry Andric return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, Op.getValueType(), NewExtract, 18755ffd83dbSDimitry Andric Op.getOperand(1)); 18760b57cec5SDimitry Andric } 18770b57cec5SDimitry Andric 1878349cc55cSDimitry Andric static SDValue LowerConvertLow(SDValue Op, SelectionDAG &DAG) { 1879349cc55cSDimitry Andric SDLoc DL(Op); 1880349cc55cSDimitry Andric if (Op.getValueType() != MVT::v2f64) 1881349cc55cSDimitry Andric return SDValue(); 1882349cc55cSDimitry Andric 1883349cc55cSDimitry Andric auto GetConvertedLane = [](SDValue Op, unsigned &Opcode, SDValue &SrcVec, 1884349cc55cSDimitry Andric unsigned &Index) -> bool { 1885349cc55cSDimitry Andric switch (Op.getOpcode()) { 1886349cc55cSDimitry Andric case ISD::SINT_TO_FP: 1887349cc55cSDimitry Andric Opcode = WebAssemblyISD::CONVERT_LOW_S; 1888349cc55cSDimitry Andric break; 1889349cc55cSDimitry Andric case ISD::UINT_TO_FP: 1890349cc55cSDimitry Andric Opcode = WebAssemblyISD::CONVERT_LOW_U; 1891349cc55cSDimitry Andric break; 1892349cc55cSDimitry Andric case ISD::FP_EXTEND: 1893349cc55cSDimitry Andric Opcode = WebAssemblyISD::PROMOTE_LOW; 1894349cc55cSDimitry Andric break; 1895349cc55cSDimitry Andric default: 1896349cc55cSDimitry Andric return false; 1897349cc55cSDimitry Andric } 1898349cc55cSDimitry Andric 1899349cc55cSDimitry Andric auto ExtractVector = Op.getOperand(0); 1900349cc55cSDimitry Andric if (ExtractVector.getOpcode() != ISD::EXTRACT_VECTOR_ELT) 1901349cc55cSDimitry Andric return false; 1902349cc55cSDimitry Andric 1903349cc55cSDimitry Andric if (!isa<ConstantSDNode>(ExtractVector.getOperand(1).getNode())) 1904349cc55cSDimitry Andric return false; 1905349cc55cSDimitry Andric 1906349cc55cSDimitry Andric SrcVec = ExtractVector.getOperand(0); 1907349cc55cSDimitry Andric Index = ExtractVector.getConstantOperandVal(1); 1908349cc55cSDimitry Andric return true; 1909349cc55cSDimitry Andric }; 1910349cc55cSDimitry Andric 1911349cc55cSDimitry Andric unsigned LHSOpcode, RHSOpcode, LHSIndex, RHSIndex; 1912349cc55cSDimitry Andric SDValue LHSSrcVec, RHSSrcVec; 1913349cc55cSDimitry Andric if (!GetConvertedLane(Op.getOperand(0), LHSOpcode, LHSSrcVec, LHSIndex) || 1914349cc55cSDimitry Andric !GetConvertedLane(Op.getOperand(1), RHSOpcode, RHSSrcVec, RHSIndex)) 1915349cc55cSDimitry Andric return SDValue(); 1916349cc55cSDimitry Andric 1917349cc55cSDimitry Andric if (LHSOpcode != RHSOpcode) 1918349cc55cSDimitry Andric return SDValue(); 1919349cc55cSDimitry Andric 1920349cc55cSDimitry Andric MVT ExpectedSrcVT; 1921349cc55cSDimitry Andric switch (LHSOpcode) { 1922349cc55cSDimitry Andric case WebAssemblyISD::CONVERT_LOW_S: 1923349cc55cSDimitry Andric case WebAssemblyISD::CONVERT_LOW_U: 1924349cc55cSDimitry Andric ExpectedSrcVT = MVT::v4i32; 1925349cc55cSDimitry Andric break; 1926349cc55cSDimitry Andric case WebAssemblyISD::PROMOTE_LOW: 1927349cc55cSDimitry Andric ExpectedSrcVT = MVT::v4f32; 1928349cc55cSDimitry Andric break; 1929349cc55cSDimitry Andric } 1930349cc55cSDimitry Andric if (LHSSrcVec.getValueType() != ExpectedSrcVT) 1931349cc55cSDimitry Andric return SDValue(); 1932349cc55cSDimitry Andric 1933349cc55cSDimitry Andric auto Src = LHSSrcVec; 1934349cc55cSDimitry Andric if (LHSIndex != 0 || RHSIndex != 1 || LHSSrcVec != RHSSrcVec) { 1935349cc55cSDimitry Andric // Shuffle the source vector so that the converted lanes are the low lanes. 1936349cc55cSDimitry Andric Src = DAG.getVectorShuffle( 1937349cc55cSDimitry Andric ExpectedSrcVT, DL, LHSSrcVec, RHSSrcVec, 1938349cc55cSDimitry Andric {static_cast<int>(LHSIndex), static_cast<int>(RHSIndex) + 4, -1, -1}); 1939349cc55cSDimitry Andric } 1940349cc55cSDimitry Andric return DAG.getNode(LHSOpcode, DL, MVT::v2f64, Src); 1941349cc55cSDimitry Andric } 1942349cc55cSDimitry Andric 19430b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerBUILD_VECTOR(SDValue Op, 19440b57cec5SDimitry Andric SelectionDAG &DAG) const { 1945349cc55cSDimitry Andric if (auto ConvertLow = LowerConvertLow(Op, DAG)) 1946349cc55cSDimitry Andric return ConvertLow; 1947349cc55cSDimitry Andric 19480b57cec5SDimitry Andric SDLoc DL(Op); 19490b57cec5SDimitry Andric const EVT VecT = Op.getValueType(); 19500b57cec5SDimitry Andric const EVT LaneT = Op.getOperand(0).getValueType(); 19510b57cec5SDimitry Andric const size_t Lanes = Op.getNumOperands(); 19525ffd83dbSDimitry Andric bool CanSwizzle = VecT == MVT::v16i8; 19538bcb0991SDimitry Andric 19548bcb0991SDimitry Andric // BUILD_VECTORs are lowered to the instruction that initializes the highest 19558bcb0991SDimitry Andric // possible number of lanes at once followed by a sequence of replace_lane 19568bcb0991SDimitry Andric // instructions to individually initialize any remaining lanes. 19578bcb0991SDimitry Andric 19588bcb0991SDimitry Andric // TODO: Tune this. For example, lanewise swizzling is very expensive, so 19598bcb0991SDimitry Andric // swizzled lanes should be given greater weight. 19608bcb0991SDimitry Andric 1961fe6060f1SDimitry Andric // TODO: Investigate looping rather than always extracting/replacing specific 1962fe6060f1SDimitry Andric // lanes to fill gaps. 19638bcb0991SDimitry Andric 19640b57cec5SDimitry Andric auto IsConstant = [](const SDValue &V) { 19650b57cec5SDimitry Andric return V.getOpcode() == ISD::Constant || V.getOpcode() == ISD::ConstantFP; 19660b57cec5SDimitry Andric }; 19670b57cec5SDimitry Andric 19688bcb0991SDimitry Andric // Returns the source vector and index vector pair if they exist. Checks for: 19698bcb0991SDimitry Andric // (extract_vector_elt 19708bcb0991SDimitry Andric // $src, 19718bcb0991SDimitry Andric // (sign_extend_inreg (extract_vector_elt $indices, $i)) 19728bcb0991SDimitry Andric // ) 19738bcb0991SDimitry Andric auto GetSwizzleSrcs = [](size_t I, const SDValue &Lane) { 19748bcb0991SDimitry Andric auto Bail = std::make_pair(SDValue(), SDValue()); 19758bcb0991SDimitry Andric if (Lane->getOpcode() != ISD::EXTRACT_VECTOR_ELT) 19768bcb0991SDimitry Andric return Bail; 19778bcb0991SDimitry Andric const SDValue &SwizzleSrc = Lane->getOperand(0); 19788bcb0991SDimitry Andric const SDValue &IndexExt = Lane->getOperand(1); 19798bcb0991SDimitry Andric if (IndexExt->getOpcode() != ISD::SIGN_EXTEND_INREG) 19808bcb0991SDimitry Andric return Bail; 19818bcb0991SDimitry Andric const SDValue &Index = IndexExt->getOperand(0); 19828bcb0991SDimitry Andric if (Index->getOpcode() != ISD::EXTRACT_VECTOR_ELT) 19838bcb0991SDimitry Andric return Bail; 19848bcb0991SDimitry Andric const SDValue &SwizzleIndices = Index->getOperand(0); 19858bcb0991SDimitry Andric if (SwizzleSrc.getValueType() != MVT::v16i8 || 19868bcb0991SDimitry Andric SwizzleIndices.getValueType() != MVT::v16i8 || 19878bcb0991SDimitry Andric Index->getOperand(1)->getOpcode() != ISD::Constant || 19888bcb0991SDimitry Andric Index->getConstantOperandVal(1) != I) 19898bcb0991SDimitry Andric return Bail; 19908bcb0991SDimitry Andric return std::make_pair(SwizzleSrc, SwizzleIndices); 19918bcb0991SDimitry Andric }; 19928bcb0991SDimitry Andric 1993fe6060f1SDimitry Andric // If the lane is extracted from another vector at a constant index, return 1994fe6060f1SDimitry Andric // that vector. The source vector must not have more lanes than the dest 1995fe6060f1SDimitry Andric // because the shufflevector indices are in terms of the destination lanes and 1996fe6060f1SDimitry Andric // would not be able to address the smaller individual source lanes. 1997fe6060f1SDimitry Andric auto GetShuffleSrc = [&](const SDValue &Lane) { 1998fe6060f1SDimitry Andric if (Lane->getOpcode() != ISD::EXTRACT_VECTOR_ELT) 1999fe6060f1SDimitry Andric return SDValue(); 2000fe6060f1SDimitry Andric if (!isa<ConstantSDNode>(Lane->getOperand(1).getNode())) 2001fe6060f1SDimitry Andric return SDValue(); 2002fe6060f1SDimitry Andric if (Lane->getOperand(0).getValueType().getVectorNumElements() > 2003fe6060f1SDimitry Andric VecT.getVectorNumElements()) 2004fe6060f1SDimitry Andric return SDValue(); 2005fe6060f1SDimitry Andric return Lane->getOperand(0); 2006fe6060f1SDimitry Andric }; 2007fe6060f1SDimitry Andric 20088bcb0991SDimitry Andric using ValueEntry = std::pair<SDValue, size_t>; 20098bcb0991SDimitry Andric SmallVector<ValueEntry, 16> SplatValueCounts; 20108bcb0991SDimitry Andric 20118bcb0991SDimitry Andric using SwizzleEntry = std::pair<std::pair<SDValue, SDValue>, size_t>; 20128bcb0991SDimitry Andric SmallVector<SwizzleEntry, 16> SwizzleCounts; 20138bcb0991SDimitry Andric 2014fe6060f1SDimitry Andric using ShuffleEntry = std::pair<SDValue, size_t>; 2015fe6060f1SDimitry Andric SmallVector<ShuffleEntry, 16> ShuffleCounts; 2016fe6060f1SDimitry Andric 20178bcb0991SDimitry Andric auto AddCount = [](auto &Counts, const auto &Val) { 2018e8d8bef9SDimitry Andric auto CountIt = 2019e8d8bef9SDimitry Andric llvm::find_if(Counts, [&Val](auto E) { return E.first == Val; }); 20208bcb0991SDimitry Andric if (CountIt == Counts.end()) { 20218bcb0991SDimitry Andric Counts.emplace_back(Val, 1); 20220b57cec5SDimitry Andric } else { 20230b57cec5SDimitry Andric CountIt->second++; 20240b57cec5SDimitry Andric } 20258bcb0991SDimitry Andric }; 20260b57cec5SDimitry Andric 20278bcb0991SDimitry Andric auto GetMostCommon = [](auto &Counts) { 20288bcb0991SDimitry Andric auto CommonIt = 202981ad6265SDimitry Andric std::max_element(Counts.begin(), Counts.end(), llvm::less_second()); 20308bcb0991SDimitry Andric assert(CommonIt != Counts.end() && "Unexpected all-undef build_vector"); 20318bcb0991SDimitry Andric return *CommonIt; 20328bcb0991SDimitry Andric }; 20338bcb0991SDimitry Andric 20348bcb0991SDimitry Andric size_t NumConstantLanes = 0; 20358bcb0991SDimitry Andric 20368bcb0991SDimitry Andric // Count eligible lanes for each type of vector creation op 20378bcb0991SDimitry Andric for (size_t I = 0; I < Lanes; ++I) { 20388bcb0991SDimitry Andric const SDValue &Lane = Op->getOperand(I); 20398bcb0991SDimitry Andric if (Lane.isUndef()) 20408bcb0991SDimitry Andric continue; 20418bcb0991SDimitry Andric 20428bcb0991SDimitry Andric AddCount(SplatValueCounts, Lane); 20438bcb0991SDimitry Andric 2044fe6060f1SDimitry Andric if (IsConstant(Lane)) 20458bcb0991SDimitry Andric NumConstantLanes++; 2046fe6060f1SDimitry Andric if (auto ShuffleSrc = GetShuffleSrc(Lane)) 2047fe6060f1SDimitry Andric AddCount(ShuffleCounts, ShuffleSrc); 2048fe6060f1SDimitry Andric if (CanSwizzle) { 20498bcb0991SDimitry Andric auto SwizzleSrcs = GetSwizzleSrcs(I, Lane); 20508bcb0991SDimitry Andric if (SwizzleSrcs.first) 20518bcb0991SDimitry Andric AddCount(SwizzleCounts, SwizzleSrcs); 20528bcb0991SDimitry Andric } 20538bcb0991SDimitry Andric } 20548bcb0991SDimitry Andric 20558bcb0991SDimitry Andric SDValue SplatValue; 20568bcb0991SDimitry Andric size_t NumSplatLanes; 20578bcb0991SDimitry Andric std::tie(SplatValue, NumSplatLanes) = GetMostCommon(SplatValueCounts); 20588bcb0991SDimitry Andric 20598bcb0991SDimitry Andric SDValue SwizzleSrc; 20608bcb0991SDimitry Andric SDValue SwizzleIndices; 20618bcb0991SDimitry Andric size_t NumSwizzleLanes = 0; 20628bcb0991SDimitry Andric if (SwizzleCounts.size()) 20638bcb0991SDimitry Andric std::forward_as_tuple(std::tie(SwizzleSrc, SwizzleIndices), 20648bcb0991SDimitry Andric NumSwizzleLanes) = GetMostCommon(SwizzleCounts); 20658bcb0991SDimitry Andric 2066fe6060f1SDimitry Andric // Shuffles can draw from up to two vectors, so find the two most common 2067fe6060f1SDimitry Andric // sources. 2068fe6060f1SDimitry Andric SDValue ShuffleSrc1, ShuffleSrc2; 2069fe6060f1SDimitry Andric size_t NumShuffleLanes = 0; 2070fe6060f1SDimitry Andric if (ShuffleCounts.size()) { 2071fe6060f1SDimitry Andric std::tie(ShuffleSrc1, NumShuffleLanes) = GetMostCommon(ShuffleCounts); 2072349cc55cSDimitry Andric llvm::erase_if(ShuffleCounts, 2073349cc55cSDimitry Andric [&](const auto &Pair) { return Pair.first == ShuffleSrc1; }); 2074fe6060f1SDimitry Andric } 2075fe6060f1SDimitry Andric if (ShuffleCounts.size()) { 2076fe6060f1SDimitry Andric size_t AdditionalShuffleLanes; 2077fe6060f1SDimitry Andric std::tie(ShuffleSrc2, AdditionalShuffleLanes) = 2078fe6060f1SDimitry Andric GetMostCommon(ShuffleCounts); 2079fe6060f1SDimitry Andric NumShuffleLanes += AdditionalShuffleLanes; 2080fe6060f1SDimitry Andric } 2081fe6060f1SDimitry Andric 20828bcb0991SDimitry Andric // Predicate returning true if the lane is properly initialized by the 20838bcb0991SDimitry Andric // original instruction 20848bcb0991SDimitry Andric std::function<bool(size_t, const SDValue &)> IsLaneConstructed; 20858bcb0991SDimitry Andric SDValue Result; 2086fe6060f1SDimitry Andric // Prefer swizzles over shuffles over vector consts over splats 2087fe6060f1SDimitry Andric if (NumSwizzleLanes >= NumShuffleLanes && 2088fe6060f1SDimitry Andric NumSwizzleLanes >= NumConstantLanes && NumSwizzleLanes >= NumSplatLanes) { 20898bcb0991SDimitry Andric Result = DAG.getNode(WebAssemblyISD::SWIZZLE, DL, VecT, SwizzleSrc, 20908bcb0991SDimitry Andric SwizzleIndices); 20918bcb0991SDimitry Andric auto Swizzled = std::make_pair(SwizzleSrc, SwizzleIndices); 20928bcb0991SDimitry Andric IsLaneConstructed = [&, Swizzled](size_t I, const SDValue &Lane) { 20938bcb0991SDimitry Andric return Swizzled == GetSwizzleSrcs(I, Lane); 20948bcb0991SDimitry Andric }; 2095fe6060f1SDimitry Andric } else if (NumShuffleLanes >= NumConstantLanes && 2096fe6060f1SDimitry Andric NumShuffleLanes >= NumSplatLanes) { 2097fe6060f1SDimitry Andric size_t DestLaneSize = VecT.getVectorElementType().getFixedSizeInBits() / 8; 2098fe6060f1SDimitry Andric size_t DestLaneCount = VecT.getVectorNumElements(); 2099fe6060f1SDimitry Andric size_t Scale1 = 1; 2100fe6060f1SDimitry Andric size_t Scale2 = 1; 2101fe6060f1SDimitry Andric SDValue Src1 = ShuffleSrc1; 2102fe6060f1SDimitry Andric SDValue Src2 = ShuffleSrc2 ? ShuffleSrc2 : DAG.getUNDEF(VecT); 2103fe6060f1SDimitry Andric if (Src1.getValueType() != VecT) { 2104fe6060f1SDimitry Andric size_t LaneSize = 2105fe6060f1SDimitry Andric Src1.getValueType().getVectorElementType().getFixedSizeInBits() / 8; 2106fe6060f1SDimitry Andric assert(LaneSize > DestLaneSize); 2107fe6060f1SDimitry Andric Scale1 = LaneSize / DestLaneSize; 2108fe6060f1SDimitry Andric Src1 = DAG.getBitcast(VecT, Src1); 2109fe6060f1SDimitry Andric } 2110fe6060f1SDimitry Andric if (Src2.getValueType() != VecT) { 2111fe6060f1SDimitry Andric size_t LaneSize = 2112fe6060f1SDimitry Andric Src2.getValueType().getVectorElementType().getFixedSizeInBits() / 8; 2113fe6060f1SDimitry Andric assert(LaneSize > DestLaneSize); 2114fe6060f1SDimitry Andric Scale2 = LaneSize / DestLaneSize; 2115fe6060f1SDimitry Andric Src2 = DAG.getBitcast(VecT, Src2); 2116fe6060f1SDimitry Andric } 2117fe6060f1SDimitry Andric 2118fe6060f1SDimitry Andric int Mask[16]; 2119fe6060f1SDimitry Andric assert(DestLaneCount <= 16); 2120fe6060f1SDimitry Andric for (size_t I = 0; I < DestLaneCount; ++I) { 2121fe6060f1SDimitry Andric const SDValue &Lane = Op->getOperand(I); 2122fe6060f1SDimitry Andric SDValue Src = GetShuffleSrc(Lane); 2123fe6060f1SDimitry Andric if (Src == ShuffleSrc1) { 2124fe6060f1SDimitry Andric Mask[I] = Lane->getConstantOperandVal(1) * Scale1; 2125fe6060f1SDimitry Andric } else if (Src && Src == ShuffleSrc2) { 2126fe6060f1SDimitry Andric Mask[I] = DestLaneCount + Lane->getConstantOperandVal(1) * Scale2; 2127fe6060f1SDimitry Andric } else { 2128fe6060f1SDimitry Andric Mask[I] = -1; 2129fe6060f1SDimitry Andric } 2130fe6060f1SDimitry Andric } 2131fe6060f1SDimitry Andric ArrayRef<int> MaskRef(Mask, DestLaneCount); 2132fe6060f1SDimitry Andric Result = DAG.getVectorShuffle(VecT, DL, Src1, Src2, MaskRef); 2133fe6060f1SDimitry Andric IsLaneConstructed = [&](size_t, const SDValue &Lane) { 2134fe6060f1SDimitry Andric auto Src = GetShuffleSrc(Lane); 2135fe6060f1SDimitry Andric return Src == ShuffleSrc1 || (Src && Src == ShuffleSrc2); 2136fe6060f1SDimitry Andric }; 2137fe6060f1SDimitry Andric } else if (NumConstantLanes >= NumSplatLanes) { 21380b57cec5SDimitry Andric SmallVector<SDValue, 16> ConstLanes; 21390b57cec5SDimitry Andric for (const SDValue &Lane : Op->op_values()) { 21400b57cec5SDimitry Andric if (IsConstant(Lane)) { 2141349cc55cSDimitry Andric // Values may need to be fixed so that they will sign extend to be 2142349cc55cSDimitry Andric // within the expected range during ISel. Check whether the value is in 2143349cc55cSDimitry Andric // bounds based on the lane bit width and if it is out of bounds, lop 2144349cc55cSDimitry Andric // off the extra bits and subtract 2^n to reflect giving the high bit 2145349cc55cSDimitry Andric // value -2^(n-1) rather than +2^(n-1). Skip the i64 case because it 2146349cc55cSDimitry Andric // cannot possibly be out of range. 2147349cc55cSDimitry Andric auto *Const = dyn_cast<ConstantSDNode>(Lane.getNode()); 2148349cc55cSDimitry Andric int64_t Val = Const ? Const->getSExtValue() : 0; 2149349cc55cSDimitry Andric uint64_t LaneBits = 128 / Lanes; 2150349cc55cSDimitry Andric assert((LaneBits == 64 || Val >= -(1ll << (LaneBits - 1))) && 2151349cc55cSDimitry Andric "Unexpected out of bounds negative value"); 2152349cc55cSDimitry Andric if (Const && LaneBits != 64 && Val > (1ll << (LaneBits - 1)) - 1) { 2153349cc55cSDimitry Andric auto NewVal = ((uint64_t)Val % (1ll << LaneBits)) - (1ll << LaneBits); 2154349cc55cSDimitry Andric ConstLanes.push_back(DAG.getConstant(NewVal, SDLoc(Lane), LaneT)); 2155349cc55cSDimitry Andric } else { 21560b57cec5SDimitry Andric ConstLanes.push_back(Lane); 2157349cc55cSDimitry Andric } 21580b57cec5SDimitry Andric } else if (LaneT.isFloatingPoint()) { 21590b57cec5SDimitry Andric ConstLanes.push_back(DAG.getConstantFP(0, DL, LaneT)); 21600b57cec5SDimitry Andric } else { 21610b57cec5SDimitry Andric ConstLanes.push_back(DAG.getConstant(0, DL, LaneT)); 21620b57cec5SDimitry Andric } 21630b57cec5SDimitry Andric } 21648bcb0991SDimitry Andric Result = DAG.getBuildVector(VecT, DL, ConstLanes); 2165e8d8bef9SDimitry Andric IsLaneConstructed = [&IsConstant](size_t _, const SDValue &Lane) { 21668bcb0991SDimitry Andric return IsConstant(Lane); 21678bcb0991SDimitry Andric }; 2168e8d8bef9SDimitry Andric } else { 2169*bdd1243dSDimitry Andric // Use a splat (which might be selected as a load splat) 21708bcb0991SDimitry Andric Result = DAG.getSplatBuildVector(VecT, DL, SplatValue); 2171e8d8bef9SDimitry Andric IsLaneConstructed = [&SplatValue](size_t _, const SDValue &Lane) { 21728bcb0991SDimitry Andric return Lane == SplatValue; 21738bcb0991SDimitry Andric }; 21748bcb0991SDimitry Andric } 21758bcb0991SDimitry Andric 2176e8d8bef9SDimitry Andric assert(Result); 2177e8d8bef9SDimitry Andric assert(IsLaneConstructed); 2178e8d8bef9SDimitry Andric 21798bcb0991SDimitry Andric // Add replace_lane instructions for any unhandled values 21800b57cec5SDimitry Andric for (size_t I = 0; I < Lanes; ++I) { 21810b57cec5SDimitry Andric const SDValue &Lane = Op->getOperand(I); 21828bcb0991SDimitry Andric if (!Lane.isUndef() && !IsLaneConstructed(I, Lane)) 21830b57cec5SDimitry Andric Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VecT, Result, Lane, 21840b57cec5SDimitry Andric DAG.getConstant(I, DL, MVT::i32)); 21850b57cec5SDimitry Andric } 21868bcb0991SDimitry Andric 21870b57cec5SDimitry Andric return Result; 21880b57cec5SDimitry Andric } 21890b57cec5SDimitry Andric 21900b57cec5SDimitry Andric SDValue 21910b57cec5SDimitry Andric WebAssemblyTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, 21920b57cec5SDimitry Andric SelectionDAG &DAG) const { 21930b57cec5SDimitry Andric SDLoc DL(Op); 21940b57cec5SDimitry Andric ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Op.getNode())->getMask(); 21950b57cec5SDimitry Andric MVT VecType = Op.getOperand(0).getSimpleValueType(); 21960b57cec5SDimitry Andric assert(VecType.is128BitVector() && "Unexpected shuffle vector type"); 21970b57cec5SDimitry Andric size_t LaneBytes = VecType.getVectorElementType().getSizeInBits() / 8; 21980b57cec5SDimitry Andric 21990b57cec5SDimitry Andric // Space for two vector args and sixteen mask indices 22000b57cec5SDimitry Andric SDValue Ops[18]; 22010b57cec5SDimitry Andric size_t OpIdx = 0; 22020b57cec5SDimitry Andric Ops[OpIdx++] = Op.getOperand(0); 22030b57cec5SDimitry Andric Ops[OpIdx++] = Op.getOperand(1); 22040b57cec5SDimitry Andric 22050b57cec5SDimitry Andric // Expand mask indices to byte indices and materialize them as operands 22060b57cec5SDimitry Andric for (int M : Mask) { 22070b57cec5SDimitry Andric for (size_t J = 0; J < LaneBytes; ++J) { 2208*bdd1243dSDimitry Andric // Lower undefs (represented by -1 in mask) to {0..J}, which use a 2209*bdd1243dSDimitry Andric // whole lane of vector input, to allow further reduction at VM. E.g. 2210*bdd1243dSDimitry Andric // match an 8x16 byte shuffle to an equivalent cheaper 32x4 shuffle. 2211*bdd1243dSDimitry Andric uint64_t ByteIndex = M == -1 ? J : (uint64_t)M * LaneBytes + J; 22120b57cec5SDimitry Andric Ops[OpIdx++] = DAG.getConstant(ByteIndex, DL, MVT::i32); 22130b57cec5SDimitry Andric } 22140b57cec5SDimitry Andric } 22150b57cec5SDimitry Andric 22160b57cec5SDimitry Andric return DAG.getNode(WebAssemblyISD::SHUFFLE, DL, Op.getValueType(), Ops); 22170b57cec5SDimitry Andric } 22180b57cec5SDimitry Andric 2219480093f4SDimitry Andric SDValue WebAssemblyTargetLowering::LowerSETCC(SDValue Op, 2220480093f4SDimitry Andric SelectionDAG &DAG) const { 2221480093f4SDimitry Andric SDLoc DL(Op); 2222fe6060f1SDimitry Andric // The legalizer does not know how to expand the unsupported comparison modes 2223fe6060f1SDimitry Andric // of i64x2 vectors, so we manually unroll them here. 2224480093f4SDimitry Andric assert(Op->getOperand(0)->getSimpleValueType(0) == MVT::v2i64); 2225480093f4SDimitry Andric SmallVector<SDValue, 2> LHS, RHS; 2226480093f4SDimitry Andric DAG.ExtractVectorElements(Op->getOperand(0), LHS); 2227480093f4SDimitry Andric DAG.ExtractVectorElements(Op->getOperand(1), RHS); 2228480093f4SDimitry Andric const SDValue &CC = Op->getOperand(2); 2229480093f4SDimitry Andric auto MakeLane = [&](unsigned I) { 2230480093f4SDimitry Andric return DAG.getNode(ISD::SELECT_CC, DL, MVT::i64, LHS[I], RHS[I], 2231480093f4SDimitry Andric DAG.getConstant(uint64_t(-1), DL, MVT::i64), 2232480093f4SDimitry Andric DAG.getConstant(uint64_t(0), DL, MVT::i64), CC); 2233480093f4SDimitry Andric }; 2234480093f4SDimitry Andric return DAG.getBuildVector(Op->getValueType(0), DL, 2235480093f4SDimitry Andric {MakeLane(0), MakeLane(1)}); 2236480093f4SDimitry Andric } 2237480093f4SDimitry Andric 22380b57cec5SDimitry Andric SDValue 22390b57cec5SDimitry Andric WebAssemblyTargetLowering::LowerAccessVectorElement(SDValue Op, 22400b57cec5SDimitry Andric SelectionDAG &DAG) const { 22410b57cec5SDimitry Andric // Allow constant lane indices, expand variable lane indices 22420b57cec5SDimitry Andric SDNode *IdxNode = Op.getOperand(Op.getNumOperands() - 1).getNode(); 2243*bdd1243dSDimitry Andric if (isa<ConstantSDNode>(IdxNode) || IdxNode->isUndef()) { 2244*bdd1243dSDimitry Andric // Ensure the index type is i32 to match the tablegen patterns 2245*bdd1243dSDimitry Andric uint64_t Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue(); 2246*bdd1243dSDimitry Andric SmallVector<SDValue, 3> Ops(Op.getNode()->ops()); 2247*bdd1243dSDimitry Andric Ops[Op.getNumOperands() - 1] = 2248*bdd1243dSDimitry Andric DAG.getConstant(Idx, SDLoc(IdxNode), MVT::i32); 2249*bdd1243dSDimitry Andric return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(), Ops); 2250*bdd1243dSDimitry Andric } 22510b57cec5SDimitry Andric // Perform default expansion 22520b57cec5SDimitry Andric return SDValue(); 22530b57cec5SDimitry Andric } 22540b57cec5SDimitry Andric 22550b57cec5SDimitry Andric static SDValue unrollVectorShift(SDValue Op, SelectionDAG &DAG) { 22560b57cec5SDimitry Andric EVT LaneT = Op.getSimpleValueType().getVectorElementType(); 22570b57cec5SDimitry Andric // 32-bit and 64-bit unrolled shifts will have proper semantics 22580b57cec5SDimitry Andric if (LaneT.bitsGE(MVT::i32)) 22590b57cec5SDimitry Andric return DAG.UnrollVectorOp(Op.getNode()); 22600b57cec5SDimitry Andric // Otherwise mask the shift value to get proper semantics from 32-bit shift 22610b57cec5SDimitry Andric SDLoc DL(Op); 22625ffd83dbSDimitry Andric size_t NumLanes = Op.getSimpleValueType().getVectorNumElements(); 22635ffd83dbSDimitry Andric SDValue Mask = DAG.getConstant(LaneT.getSizeInBits() - 1, DL, MVT::i32); 22645ffd83dbSDimitry Andric unsigned ShiftOpcode = Op.getOpcode(); 22655ffd83dbSDimitry Andric SmallVector<SDValue, 16> ShiftedElements; 22665ffd83dbSDimitry Andric DAG.ExtractVectorElements(Op.getOperand(0), ShiftedElements, 0, 0, MVT::i32); 22675ffd83dbSDimitry Andric SmallVector<SDValue, 16> ShiftElements; 22685ffd83dbSDimitry Andric DAG.ExtractVectorElements(Op.getOperand(1), ShiftElements, 0, 0, MVT::i32); 22695ffd83dbSDimitry Andric SmallVector<SDValue, 16> UnrolledOps; 22705ffd83dbSDimitry Andric for (size_t i = 0; i < NumLanes; ++i) { 22715ffd83dbSDimitry Andric SDValue MaskedShiftValue = 22725ffd83dbSDimitry Andric DAG.getNode(ISD::AND, DL, MVT::i32, ShiftElements[i], Mask); 22735ffd83dbSDimitry Andric SDValue ShiftedValue = ShiftedElements[i]; 22745ffd83dbSDimitry Andric if (ShiftOpcode == ISD::SRA) 22755ffd83dbSDimitry Andric ShiftedValue = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, 22765ffd83dbSDimitry Andric ShiftedValue, DAG.getValueType(LaneT)); 22775ffd83dbSDimitry Andric UnrolledOps.push_back( 22785ffd83dbSDimitry Andric DAG.getNode(ShiftOpcode, DL, MVT::i32, ShiftedValue, MaskedShiftValue)); 22795ffd83dbSDimitry Andric } 22805ffd83dbSDimitry Andric return DAG.getBuildVector(Op.getValueType(), DL, UnrolledOps); 22810b57cec5SDimitry Andric } 22820b57cec5SDimitry Andric 22830b57cec5SDimitry Andric SDValue WebAssemblyTargetLowering::LowerShift(SDValue Op, 22840b57cec5SDimitry Andric SelectionDAG &DAG) const { 22850b57cec5SDimitry Andric SDLoc DL(Op); 22860b57cec5SDimitry Andric 22870b57cec5SDimitry Andric // Only manually lower vector shifts 22880b57cec5SDimitry Andric assert(Op.getSimpleValueType().isVector()); 22890b57cec5SDimitry Andric 22905ffd83dbSDimitry Andric auto ShiftVal = DAG.getSplatValue(Op.getOperand(1)); 22915ffd83dbSDimitry Andric if (!ShiftVal) 22920b57cec5SDimitry Andric return unrollVectorShift(Op, DAG); 22930b57cec5SDimitry Andric 22945ffd83dbSDimitry Andric // Use anyext because none of the high bits can affect the shift 22955ffd83dbSDimitry Andric ShiftVal = DAG.getAnyExtOrTrunc(ShiftVal, DL, MVT::i32); 22960b57cec5SDimitry Andric 22970b57cec5SDimitry Andric unsigned Opcode; 22980b57cec5SDimitry Andric switch (Op.getOpcode()) { 22990b57cec5SDimitry Andric case ISD::SHL: 23000b57cec5SDimitry Andric Opcode = WebAssemblyISD::VEC_SHL; 23010b57cec5SDimitry Andric break; 23020b57cec5SDimitry Andric case ISD::SRA: 23030b57cec5SDimitry Andric Opcode = WebAssemblyISD::VEC_SHR_S; 23040b57cec5SDimitry Andric break; 23050b57cec5SDimitry Andric case ISD::SRL: 23060b57cec5SDimitry Andric Opcode = WebAssemblyISD::VEC_SHR_U; 23070b57cec5SDimitry Andric break; 23080b57cec5SDimitry Andric default: 23090b57cec5SDimitry Andric llvm_unreachable("unexpected opcode"); 23100b57cec5SDimitry Andric } 23115ffd83dbSDimitry Andric 23125ffd83dbSDimitry Andric return DAG.getNode(Opcode, DL, Op.getValueType(), Op.getOperand(0), ShiftVal); 23130b57cec5SDimitry Andric } 23140b57cec5SDimitry Andric 2315fe6060f1SDimitry Andric SDValue WebAssemblyTargetLowering::LowerFP_TO_INT_SAT(SDValue Op, 2316fe6060f1SDimitry Andric SelectionDAG &DAG) const { 2317fe6060f1SDimitry Andric SDLoc DL(Op); 2318fe6060f1SDimitry Andric EVT ResT = Op.getValueType(); 2319fe6060f1SDimitry Andric EVT SatVT = cast<VTSDNode>(Op.getOperand(1))->getVT(); 2320fe6060f1SDimitry Andric 2321fe6060f1SDimitry Andric if ((ResT == MVT::i32 || ResT == MVT::i64) && 2322fe6060f1SDimitry Andric (SatVT == MVT::i32 || SatVT == MVT::i64)) 2323fe6060f1SDimitry Andric return Op; 2324fe6060f1SDimitry Andric 2325fe6060f1SDimitry Andric if (ResT == MVT::v4i32 && SatVT == MVT::i32) 2326fe6060f1SDimitry Andric return Op; 2327fe6060f1SDimitry Andric 2328fe6060f1SDimitry Andric return SDValue(); 2329fe6060f1SDimitry Andric } 2330fe6060f1SDimitry Andric 23310b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 23325ffd83dbSDimitry Andric // Custom DAG combine hooks 23330b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 23345ffd83dbSDimitry Andric static SDValue 23355ffd83dbSDimitry Andric performVECTOR_SHUFFLECombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) { 23365ffd83dbSDimitry Andric auto &DAG = DCI.DAG; 23375ffd83dbSDimitry Andric auto Shuffle = cast<ShuffleVectorSDNode>(N); 23385ffd83dbSDimitry Andric 23395ffd83dbSDimitry Andric // Hoist vector bitcasts that don't change the number of lanes out of unary 23405ffd83dbSDimitry Andric // shuffles, where they are less likely to get in the way of other combines. 23415ffd83dbSDimitry Andric // (shuffle (vNxT1 (bitcast (vNxT0 x))), undef, mask) -> 23425ffd83dbSDimitry Andric // (vNxT1 (bitcast (vNxT0 (shuffle x, undef, mask)))) 23435ffd83dbSDimitry Andric SDValue Bitcast = N->getOperand(0); 23445ffd83dbSDimitry Andric if (Bitcast.getOpcode() != ISD::BITCAST) 23455ffd83dbSDimitry Andric return SDValue(); 23465ffd83dbSDimitry Andric if (!N->getOperand(1).isUndef()) 23475ffd83dbSDimitry Andric return SDValue(); 23485ffd83dbSDimitry Andric SDValue CastOp = Bitcast.getOperand(0); 23495ffd83dbSDimitry Andric MVT SrcType = CastOp.getSimpleValueType(); 23505ffd83dbSDimitry Andric MVT DstType = Bitcast.getSimpleValueType(); 23515ffd83dbSDimitry Andric if (!SrcType.is128BitVector() || 23525ffd83dbSDimitry Andric SrcType.getVectorNumElements() != DstType.getVectorNumElements()) 23535ffd83dbSDimitry Andric return SDValue(); 23545ffd83dbSDimitry Andric SDValue NewShuffle = DAG.getVectorShuffle( 23555ffd83dbSDimitry Andric SrcType, SDLoc(N), CastOp, DAG.getUNDEF(SrcType), Shuffle->getMask()); 23565ffd83dbSDimitry Andric return DAG.getBitcast(DstType, NewShuffle); 23575ffd83dbSDimitry Andric } 23585ffd83dbSDimitry Andric 2359*bdd1243dSDimitry Andric /// Convert ({u,s}itofp vec) --> ({u,s}itofp ({s,z}ext vec)) so it doesn't get 2360*bdd1243dSDimitry Andric /// split up into scalar instructions during legalization, and the vector 2361*bdd1243dSDimitry Andric /// extending instructions are selected in performVectorExtendCombine below. 2362*bdd1243dSDimitry Andric static SDValue 2363*bdd1243dSDimitry Andric performVectorExtendToFPCombine(SDNode *N, 2364*bdd1243dSDimitry Andric TargetLowering::DAGCombinerInfo &DCI) { 2365*bdd1243dSDimitry Andric auto &DAG = DCI.DAG; 2366*bdd1243dSDimitry Andric assert(N->getOpcode() == ISD::UINT_TO_FP || 2367*bdd1243dSDimitry Andric N->getOpcode() == ISD::SINT_TO_FP); 2368*bdd1243dSDimitry Andric 2369*bdd1243dSDimitry Andric EVT InVT = N->getOperand(0)->getValueType(0); 2370*bdd1243dSDimitry Andric EVT ResVT = N->getValueType(0); 2371*bdd1243dSDimitry Andric MVT ExtVT; 2372*bdd1243dSDimitry Andric if (ResVT == MVT::v4f32 && (InVT == MVT::v4i16 || InVT == MVT::v4i8)) 2373*bdd1243dSDimitry Andric ExtVT = MVT::v4i32; 2374*bdd1243dSDimitry Andric else if (ResVT == MVT::v2f64 && (InVT == MVT::v2i16 || InVT == MVT::v2i8)) 2375*bdd1243dSDimitry Andric ExtVT = MVT::v2i32; 2376*bdd1243dSDimitry Andric else 2377*bdd1243dSDimitry Andric return SDValue(); 2378*bdd1243dSDimitry Andric 2379*bdd1243dSDimitry Andric unsigned Op = 2380*bdd1243dSDimitry Andric N->getOpcode() == ISD::UINT_TO_FP ? ISD::ZERO_EXTEND : ISD::SIGN_EXTEND; 2381*bdd1243dSDimitry Andric SDValue Conv = DAG.getNode(Op, SDLoc(N), ExtVT, N->getOperand(0)); 2382*bdd1243dSDimitry Andric return DAG.getNode(N->getOpcode(), SDLoc(N), ResVT, Conv); 2383*bdd1243dSDimitry Andric } 2384*bdd1243dSDimitry Andric 2385fe6060f1SDimitry Andric static SDValue 2386fe6060f1SDimitry Andric performVectorExtendCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) { 2387e8d8bef9SDimitry Andric auto &DAG = DCI.DAG; 2388e8d8bef9SDimitry Andric assert(N->getOpcode() == ISD::SIGN_EXTEND || 2389e8d8bef9SDimitry Andric N->getOpcode() == ISD::ZERO_EXTEND); 2390e8d8bef9SDimitry Andric 2391e8d8bef9SDimitry Andric // Combine ({s,z}ext (extract_subvector src, i)) into a widening operation if 2392e8d8bef9SDimitry Andric // possible before the extract_subvector can be expanded. 2393e8d8bef9SDimitry Andric auto Extract = N->getOperand(0); 2394e8d8bef9SDimitry Andric if (Extract.getOpcode() != ISD::EXTRACT_SUBVECTOR) 2395e8d8bef9SDimitry Andric return SDValue(); 2396e8d8bef9SDimitry Andric auto Source = Extract.getOperand(0); 2397e8d8bef9SDimitry Andric auto *IndexNode = dyn_cast<ConstantSDNode>(Extract.getOperand(1)); 2398e8d8bef9SDimitry Andric if (IndexNode == nullptr) 2399e8d8bef9SDimitry Andric return SDValue(); 2400e8d8bef9SDimitry Andric auto Index = IndexNode->getZExtValue(); 2401e8d8bef9SDimitry Andric 2402fe6060f1SDimitry Andric // Only v8i8, v4i16, and v2i32 extracts can be widened, and only if the 2403fe6060f1SDimitry Andric // extracted subvector is the low or high half of its source. 2404e8d8bef9SDimitry Andric EVT ResVT = N->getValueType(0); 2405e8d8bef9SDimitry Andric if (ResVT == MVT::v8i16) { 2406e8d8bef9SDimitry Andric if (Extract.getValueType() != MVT::v8i8 || 2407e8d8bef9SDimitry Andric Source.getValueType() != MVT::v16i8 || (Index != 0 && Index != 8)) 2408e8d8bef9SDimitry Andric return SDValue(); 2409e8d8bef9SDimitry Andric } else if (ResVT == MVT::v4i32) { 2410e8d8bef9SDimitry Andric if (Extract.getValueType() != MVT::v4i16 || 2411e8d8bef9SDimitry Andric Source.getValueType() != MVT::v8i16 || (Index != 0 && Index != 4)) 2412e8d8bef9SDimitry Andric return SDValue(); 2413fe6060f1SDimitry Andric } else if (ResVT == MVT::v2i64) { 2414fe6060f1SDimitry Andric if (Extract.getValueType() != MVT::v2i32 || 2415fe6060f1SDimitry Andric Source.getValueType() != MVT::v4i32 || (Index != 0 && Index != 2)) 2416fe6060f1SDimitry Andric return SDValue(); 2417e8d8bef9SDimitry Andric } else { 2418e8d8bef9SDimitry Andric return SDValue(); 2419e8d8bef9SDimitry Andric } 2420e8d8bef9SDimitry Andric 2421e8d8bef9SDimitry Andric bool IsSext = N->getOpcode() == ISD::SIGN_EXTEND; 2422e8d8bef9SDimitry Andric bool IsLow = Index == 0; 2423e8d8bef9SDimitry Andric 2424fe6060f1SDimitry Andric unsigned Op = IsSext ? (IsLow ? WebAssemblyISD::EXTEND_LOW_S 2425fe6060f1SDimitry Andric : WebAssemblyISD::EXTEND_HIGH_S) 2426fe6060f1SDimitry Andric : (IsLow ? WebAssemblyISD::EXTEND_LOW_U 2427fe6060f1SDimitry Andric : WebAssemblyISD::EXTEND_HIGH_U); 2428e8d8bef9SDimitry Andric 2429e8d8bef9SDimitry Andric return DAG.getNode(Op, SDLoc(N), ResVT, Source); 2430e8d8bef9SDimitry Andric } 2431e8d8bef9SDimitry Andric 2432fe6060f1SDimitry Andric static SDValue 2433fe6060f1SDimitry Andric performVectorTruncZeroCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) { 2434fe6060f1SDimitry Andric auto &DAG = DCI.DAG; 2435fe6060f1SDimitry Andric 2436fe6060f1SDimitry Andric auto GetWasmConversionOp = [](unsigned Op) { 2437fe6060f1SDimitry Andric switch (Op) { 2438fe6060f1SDimitry Andric case ISD::FP_TO_SINT_SAT: 2439fe6060f1SDimitry Andric return WebAssemblyISD::TRUNC_SAT_ZERO_S; 2440fe6060f1SDimitry Andric case ISD::FP_TO_UINT_SAT: 2441fe6060f1SDimitry Andric return WebAssemblyISD::TRUNC_SAT_ZERO_U; 2442fe6060f1SDimitry Andric case ISD::FP_ROUND: 2443fe6060f1SDimitry Andric return WebAssemblyISD::DEMOTE_ZERO; 2444fe6060f1SDimitry Andric } 2445fe6060f1SDimitry Andric llvm_unreachable("unexpected op"); 2446fe6060f1SDimitry Andric }; 2447fe6060f1SDimitry Andric 2448fe6060f1SDimitry Andric auto IsZeroSplat = [](SDValue SplatVal) { 2449fe6060f1SDimitry Andric auto *Splat = dyn_cast<BuildVectorSDNode>(SplatVal.getNode()); 2450fe6060f1SDimitry Andric APInt SplatValue, SplatUndef; 2451fe6060f1SDimitry Andric unsigned SplatBitSize; 2452fe6060f1SDimitry Andric bool HasAnyUndefs; 2453fe6060f1SDimitry Andric return Splat && 2454fe6060f1SDimitry Andric Splat->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, 2455fe6060f1SDimitry Andric HasAnyUndefs) && 2456fe6060f1SDimitry Andric SplatValue == 0; 2457fe6060f1SDimitry Andric }; 2458fe6060f1SDimitry Andric 2459fe6060f1SDimitry Andric if (N->getOpcode() == ISD::CONCAT_VECTORS) { 2460fe6060f1SDimitry Andric // Combine this: 2461fe6060f1SDimitry Andric // 2462fe6060f1SDimitry Andric // (concat_vectors (v2i32 (fp_to_{s,u}int_sat $x, 32)), (v2i32 (splat 0))) 2463fe6060f1SDimitry Andric // 2464fe6060f1SDimitry Andric // into (i32x4.trunc_sat_f64x2_zero_{s,u} $x). 2465fe6060f1SDimitry Andric // 2466fe6060f1SDimitry Andric // Or this: 2467fe6060f1SDimitry Andric // 2468fe6060f1SDimitry Andric // (concat_vectors (v2f32 (fp_round (v2f64 $x))), (v2f32 (splat 0))) 2469fe6060f1SDimitry Andric // 2470fe6060f1SDimitry Andric // into (f32x4.demote_zero_f64x2 $x). 2471fe6060f1SDimitry Andric EVT ResVT; 2472fe6060f1SDimitry Andric EVT ExpectedConversionType; 2473fe6060f1SDimitry Andric auto Conversion = N->getOperand(0); 2474fe6060f1SDimitry Andric auto ConversionOp = Conversion.getOpcode(); 2475fe6060f1SDimitry Andric switch (ConversionOp) { 2476fe6060f1SDimitry Andric case ISD::FP_TO_SINT_SAT: 2477fe6060f1SDimitry Andric case ISD::FP_TO_UINT_SAT: 2478fe6060f1SDimitry Andric ResVT = MVT::v4i32; 2479fe6060f1SDimitry Andric ExpectedConversionType = MVT::v2i32; 2480fe6060f1SDimitry Andric break; 2481fe6060f1SDimitry Andric case ISD::FP_ROUND: 2482fe6060f1SDimitry Andric ResVT = MVT::v4f32; 2483fe6060f1SDimitry Andric ExpectedConversionType = MVT::v2f32; 2484fe6060f1SDimitry Andric break; 2485fe6060f1SDimitry Andric default: 2486fe6060f1SDimitry Andric return SDValue(); 2487fe6060f1SDimitry Andric } 2488fe6060f1SDimitry Andric 2489fe6060f1SDimitry Andric if (N->getValueType(0) != ResVT) 2490fe6060f1SDimitry Andric return SDValue(); 2491fe6060f1SDimitry Andric 2492fe6060f1SDimitry Andric if (Conversion.getValueType() != ExpectedConversionType) 2493fe6060f1SDimitry Andric return SDValue(); 2494fe6060f1SDimitry Andric 2495fe6060f1SDimitry Andric auto Source = Conversion.getOperand(0); 2496fe6060f1SDimitry Andric if (Source.getValueType() != MVT::v2f64) 2497fe6060f1SDimitry Andric return SDValue(); 2498fe6060f1SDimitry Andric 2499fe6060f1SDimitry Andric if (!IsZeroSplat(N->getOperand(1)) || 2500fe6060f1SDimitry Andric N->getOperand(1).getValueType() != ExpectedConversionType) 2501fe6060f1SDimitry Andric return SDValue(); 2502fe6060f1SDimitry Andric 2503fe6060f1SDimitry Andric unsigned Op = GetWasmConversionOp(ConversionOp); 2504fe6060f1SDimitry Andric return DAG.getNode(Op, SDLoc(N), ResVT, Source); 2505fe6060f1SDimitry Andric } 2506fe6060f1SDimitry Andric 2507fe6060f1SDimitry Andric // Combine this: 2508fe6060f1SDimitry Andric // 2509fe6060f1SDimitry Andric // (fp_to_{s,u}int_sat (concat_vectors $x, (v2f64 (splat 0))), 32) 2510fe6060f1SDimitry Andric // 2511fe6060f1SDimitry Andric // into (i32x4.trunc_sat_f64x2_zero_{s,u} $x). 2512fe6060f1SDimitry Andric // 2513fe6060f1SDimitry Andric // Or this: 2514fe6060f1SDimitry Andric // 2515fe6060f1SDimitry Andric // (v4f32 (fp_round (concat_vectors $x, (v2f64 (splat 0))))) 2516fe6060f1SDimitry Andric // 2517fe6060f1SDimitry Andric // into (f32x4.demote_zero_f64x2 $x). 2518fe6060f1SDimitry Andric EVT ResVT; 2519fe6060f1SDimitry Andric auto ConversionOp = N->getOpcode(); 2520fe6060f1SDimitry Andric switch (ConversionOp) { 2521fe6060f1SDimitry Andric case ISD::FP_TO_SINT_SAT: 2522fe6060f1SDimitry Andric case ISD::FP_TO_UINT_SAT: 2523fe6060f1SDimitry Andric ResVT = MVT::v4i32; 2524fe6060f1SDimitry Andric break; 2525fe6060f1SDimitry Andric case ISD::FP_ROUND: 2526fe6060f1SDimitry Andric ResVT = MVT::v4f32; 2527fe6060f1SDimitry Andric break; 2528fe6060f1SDimitry Andric default: 2529fe6060f1SDimitry Andric llvm_unreachable("unexpected op"); 2530fe6060f1SDimitry Andric } 2531fe6060f1SDimitry Andric 2532fe6060f1SDimitry Andric if (N->getValueType(0) != ResVT) 2533fe6060f1SDimitry Andric return SDValue(); 2534fe6060f1SDimitry Andric 2535fe6060f1SDimitry Andric auto Concat = N->getOperand(0); 2536fe6060f1SDimitry Andric if (Concat.getValueType() != MVT::v4f64) 2537fe6060f1SDimitry Andric return SDValue(); 2538fe6060f1SDimitry Andric 2539fe6060f1SDimitry Andric auto Source = Concat.getOperand(0); 2540fe6060f1SDimitry Andric if (Source.getValueType() != MVT::v2f64) 2541fe6060f1SDimitry Andric return SDValue(); 2542fe6060f1SDimitry Andric 2543fe6060f1SDimitry Andric if (!IsZeroSplat(Concat.getOperand(1)) || 2544fe6060f1SDimitry Andric Concat.getOperand(1).getValueType() != MVT::v2f64) 2545fe6060f1SDimitry Andric return SDValue(); 2546fe6060f1SDimitry Andric 2547fe6060f1SDimitry Andric unsigned Op = GetWasmConversionOp(ConversionOp); 2548fe6060f1SDimitry Andric return DAG.getNode(Op, SDLoc(N), ResVT, Source); 2549fe6060f1SDimitry Andric } 2550fe6060f1SDimitry Andric 25510eae32dcSDimitry Andric // Helper to extract VectorWidth bits from Vec, starting from IdxVal. 25520eae32dcSDimitry Andric static SDValue extractSubVector(SDValue Vec, unsigned IdxVal, SelectionDAG &DAG, 25530eae32dcSDimitry Andric const SDLoc &DL, unsigned VectorWidth) { 25540eae32dcSDimitry Andric EVT VT = Vec.getValueType(); 25550eae32dcSDimitry Andric EVT ElVT = VT.getVectorElementType(); 25560eae32dcSDimitry Andric unsigned Factor = VT.getSizeInBits() / VectorWidth; 25570eae32dcSDimitry Andric EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT, 25580eae32dcSDimitry Andric VT.getVectorNumElements() / Factor); 25590eae32dcSDimitry Andric 25600eae32dcSDimitry Andric // Extract the relevant VectorWidth bits. Generate an EXTRACT_SUBVECTOR 25610eae32dcSDimitry Andric unsigned ElemsPerChunk = VectorWidth / ElVT.getSizeInBits(); 25620eae32dcSDimitry Andric assert(isPowerOf2_32(ElemsPerChunk) && "Elements per chunk not power of 2"); 25630eae32dcSDimitry Andric 25640eae32dcSDimitry Andric // This is the index of the first element of the VectorWidth-bit chunk 25650eae32dcSDimitry Andric // we want. Since ElemsPerChunk is a power of 2 just need to clear bits. 25660eae32dcSDimitry Andric IdxVal &= ~(ElemsPerChunk - 1); 25670eae32dcSDimitry Andric 25680eae32dcSDimitry Andric // If the input is a buildvector just emit a smaller one. 25690eae32dcSDimitry Andric if (Vec.getOpcode() == ISD::BUILD_VECTOR) 25700eae32dcSDimitry Andric return DAG.getBuildVector(ResultVT, DL, 25710eae32dcSDimitry Andric Vec->ops().slice(IdxVal, ElemsPerChunk)); 25720eae32dcSDimitry Andric 25730eae32dcSDimitry Andric SDValue VecIdx = DAG.getIntPtrConstant(IdxVal, DL); 25740eae32dcSDimitry Andric return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, ResultVT, Vec, VecIdx); 25750eae32dcSDimitry Andric } 25760eae32dcSDimitry Andric 25770eae32dcSDimitry Andric // Helper to recursively truncate vector elements in half with NARROW_U. DstVT 25780eae32dcSDimitry Andric // is the expected destination value type after recursion. In is the initial 25790eae32dcSDimitry Andric // input. Note that the input should have enough leading zero bits to prevent 25800eae32dcSDimitry Andric // NARROW_U from saturating results. 25810eae32dcSDimitry Andric static SDValue truncateVectorWithNARROW(EVT DstVT, SDValue In, const SDLoc &DL, 25820eae32dcSDimitry Andric SelectionDAG &DAG) { 25830eae32dcSDimitry Andric EVT SrcVT = In.getValueType(); 25840eae32dcSDimitry Andric 25850eae32dcSDimitry Andric // No truncation required, we might get here due to recursive calls. 25860eae32dcSDimitry Andric if (SrcVT == DstVT) 25870eae32dcSDimitry Andric return In; 25880eae32dcSDimitry Andric 25890eae32dcSDimitry Andric unsigned SrcSizeInBits = SrcVT.getSizeInBits(); 25900eae32dcSDimitry Andric unsigned NumElems = SrcVT.getVectorNumElements(); 25910eae32dcSDimitry Andric if (!isPowerOf2_32(NumElems)) 25920eae32dcSDimitry Andric return SDValue(); 25930eae32dcSDimitry Andric assert(DstVT.getVectorNumElements() == NumElems && "Illegal truncation"); 25940eae32dcSDimitry Andric assert(SrcSizeInBits > DstVT.getSizeInBits() && "Illegal truncation"); 25950eae32dcSDimitry Andric 25960eae32dcSDimitry Andric LLVMContext &Ctx = *DAG.getContext(); 25970eae32dcSDimitry Andric EVT PackedSVT = EVT::getIntegerVT(Ctx, SrcVT.getScalarSizeInBits() / 2); 25980eae32dcSDimitry Andric 25990eae32dcSDimitry Andric // Narrow to the largest type possible: 26000eae32dcSDimitry Andric // vXi64/vXi32 -> i16x8.narrow_i32x4_u and vXi16 -> i8x16.narrow_i16x8_u. 26010eae32dcSDimitry Andric EVT InVT = MVT::i16, OutVT = MVT::i8; 26020eae32dcSDimitry Andric if (SrcVT.getScalarSizeInBits() > 16) { 26030eae32dcSDimitry Andric InVT = MVT::i32; 26040eae32dcSDimitry Andric OutVT = MVT::i16; 26050eae32dcSDimitry Andric } 26060eae32dcSDimitry Andric unsigned SubSizeInBits = SrcSizeInBits / 2; 26070eae32dcSDimitry Andric InVT = EVT::getVectorVT(Ctx, InVT, SubSizeInBits / InVT.getSizeInBits()); 26080eae32dcSDimitry Andric OutVT = EVT::getVectorVT(Ctx, OutVT, SubSizeInBits / OutVT.getSizeInBits()); 26090eae32dcSDimitry Andric 26100eae32dcSDimitry Andric // Split lower/upper subvectors. 26110eae32dcSDimitry Andric SDValue Lo = extractSubVector(In, 0, DAG, DL, SubSizeInBits); 26120eae32dcSDimitry Andric SDValue Hi = extractSubVector(In, NumElems / 2, DAG, DL, SubSizeInBits); 26130eae32dcSDimitry Andric 26140eae32dcSDimitry Andric // 256bit -> 128bit truncate - Narrow lower/upper 128-bit subvectors. 26150eae32dcSDimitry Andric if (SrcVT.is256BitVector() && DstVT.is128BitVector()) { 26160eae32dcSDimitry Andric Lo = DAG.getBitcast(InVT, Lo); 26170eae32dcSDimitry Andric Hi = DAG.getBitcast(InVT, Hi); 26180eae32dcSDimitry Andric SDValue Res = DAG.getNode(WebAssemblyISD::NARROW_U, DL, OutVT, Lo, Hi); 26190eae32dcSDimitry Andric return DAG.getBitcast(DstVT, Res); 26200eae32dcSDimitry Andric } 26210eae32dcSDimitry Andric 26220eae32dcSDimitry Andric // Recursively narrow lower/upper subvectors, concat result and narrow again. 26230eae32dcSDimitry Andric EVT PackedVT = EVT::getVectorVT(Ctx, PackedSVT, NumElems / 2); 26240eae32dcSDimitry Andric Lo = truncateVectorWithNARROW(PackedVT, Lo, DL, DAG); 26250eae32dcSDimitry Andric Hi = truncateVectorWithNARROW(PackedVT, Hi, DL, DAG); 26260eae32dcSDimitry Andric 26270eae32dcSDimitry Andric PackedVT = EVT::getVectorVT(Ctx, PackedSVT, NumElems); 26280eae32dcSDimitry Andric SDValue Res = DAG.getNode(ISD::CONCAT_VECTORS, DL, PackedVT, Lo, Hi); 26290eae32dcSDimitry Andric return truncateVectorWithNARROW(DstVT, Res, DL, DAG); 26300eae32dcSDimitry Andric } 26310eae32dcSDimitry Andric 26320eae32dcSDimitry Andric static SDValue performTruncateCombine(SDNode *N, 26330eae32dcSDimitry Andric TargetLowering::DAGCombinerInfo &DCI) { 26340eae32dcSDimitry Andric auto &DAG = DCI.DAG; 26350eae32dcSDimitry Andric 26360eae32dcSDimitry Andric SDValue In = N->getOperand(0); 26370eae32dcSDimitry Andric EVT InVT = In.getValueType(); 26380eae32dcSDimitry Andric if (!InVT.isSimple()) 26390eae32dcSDimitry Andric return SDValue(); 26400eae32dcSDimitry Andric 26410eae32dcSDimitry Andric EVT OutVT = N->getValueType(0); 26420eae32dcSDimitry Andric if (!OutVT.isVector()) 26430eae32dcSDimitry Andric return SDValue(); 26440eae32dcSDimitry Andric 26450eae32dcSDimitry Andric EVT OutSVT = OutVT.getVectorElementType(); 26460eae32dcSDimitry Andric EVT InSVT = InVT.getVectorElementType(); 26470eae32dcSDimitry Andric // Currently only cover truncate to v16i8 or v8i16. 26480eae32dcSDimitry Andric if (!((InSVT == MVT::i16 || InSVT == MVT::i32 || InSVT == MVT::i64) && 26490eae32dcSDimitry Andric (OutSVT == MVT::i8 || OutSVT == MVT::i16) && OutVT.is128BitVector())) 26500eae32dcSDimitry Andric return SDValue(); 26510eae32dcSDimitry Andric 26520eae32dcSDimitry Andric SDLoc DL(N); 26530eae32dcSDimitry Andric APInt Mask = APInt::getLowBitsSet(InVT.getScalarSizeInBits(), 26540eae32dcSDimitry Andric OutVT.getScalarSizeInBits()); 26550eae32dcSDimitry Andric In = DAG.getNode(ISD::AND, DL, InVT, In, DAG.getConstant(Mask, DL, InVT)); 26560eae32dcSDimitry Andric return truncateVectorWithNARROW(OutVT, In, DL, DAG); 26570eae32dcSDimitry Andric } 26580eae32dcSDimitry Andric 26595ffd83dbSDimitry Andric SDValue 26605ffd83dbSDimitry Andric WebAssemblyTargetLowering::PerformDAGCombine(SDNode *N, 26615ffd83dbSDimitry Andric DAGCombinerInfo &DCI) const { 26625ffd83dbSDimitry Andric switch (N->getOpcode()) { 26635ffd83dbSDimitry Andric default: 26645ffd83dbSDimitry Andric return SDValue(); 26655ffd83dbSDimitry Andric case ISD::VECTOR_SHUFFLE: 26665ffd83dbSDimitry Andric return performVECTOR_SHUFFLECombine(N, DCI); 2667e8d8bef9SDimitry Andric case ISD::SIGN_EXTEND: 2668e8d8bef9SDimitry Andric case ISD::ZERO_EXTEND: 2669fe6060f1SDimitry Andric return performVectorExtendCombine(N, DCI); 2670*bdd1243dSDimitry Andric case ISD::UINT_TO_FP: 2671*bdd1243dSDimitry Andric case ISD::SINT_TO_FP: 2672*bdd1243dSDimitry Andric return performVectorExtendToFPCombine(N, DCI); 2673fe6060f1SDimitry Andric case ISD::FP_TO_SINT_SAT: 2674fe6060f1SDimitry Andric case ISD::FP_TO_UINT_SAT: 2675fe6060f1SDimitry Andric case ISD::FP_ROUND: 2676fe6060f1SDimitry Andric case ISD::CONCAT_VECTORS: 2677fe6060f1SDimitry Andric return performVectorTruncZeroCombine(N, DCI); 26780eae32dcSDimitry Andric case ISD::TRUNCATE: 26790eae32dcSDimitry Andric return performTruncateCombine(N, DCI); 26805ffd83dbSDimitry Andric } 26815ffd83dbSDimitry Andric } 2682