110e730a2SDan Gohman //=- WebAssemblyISelLowering.cpp - WebAssembly DAG Lowering Implementation -==// 210e730a2SDan Gohman // 310e730a2SDan Gohman // The LLVM Compiler Infrastructure 410e730a2SDan Gohman // 510e730a2SDan Gohman // This file is distributed under the University of Illinois Open Source 610e730a2SDan Gohman // License. See LICENSE.TXT for details. 710e730a2SDan Gohman // 810e730a2SDan Gohman //===----------------------------------------------------------------------===// 910e730a2SDan Gohman /// 1010e730a2SDan Gohman /// \file 1110e730a2SDan Gohman /// \brief This file implements the WebAssemblyTargetLowering class. 1210e730a2SDan Gohman /// 1310e730a2SDan Gohman //===----------------------------------------------------------------------===// 1410e730a2SDan Gohman 1510e730a2SDan Gohman #include "WebAssemblyISelLowering.h" 1610e730a2SDan Gohman #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" 1710e730a2SDan Gohman #include "WebAssemblyMachineFunctionInfo.h" 1810e730a2SDan Gohman #include "WebAssemblySubtarget.h" 1910e730a2SDan Gohman #include "WebAssemblyTargetMachine.h" 2010e730a2SDan Gohman #include "llvm/CodeGen/Analysis.h" 21af111db8SJF Bastien #include "llvm/CodeGen/CallingConvLower.h" 22950a13cfSDan Gohman #include "llvm/CodeGen/MachineJumpTableInfo.h" 2310e730a2SDan Gohman #include "llvm/CodeGen/MachineRegisterInfo.h" 2410e730a2SDan Gohman #include "llvm/CodeGen/SelectionDAG.h" 25b9073fb2SJF Bastien #include "llvm/IR/DiagnosticInfo.h" 26b9073fb2SJF Bastien #include "llvm/IR/DiagnosticPrinter.h" 2710e730a2SDan Gohman #include "llvm/IR/Function.h" 2810e730a2SDan Gohman #include "llvm/IR/Intrinsics.h" 2910e730a2SDan Gohman #include "llvm/Support/CommandLine.h" 3010e730a2SDan Gohman #include "llvm/Support/Debug.h" 3110e730a2SDan Gohman #include "llvm/Support/ErrorHandling.h" 3210e730a2SDan Gohman #include "llvm/Support/raw_ostream.h" 3310e730a2SDan Gohman #include "llvm/Target/TargetOptions.h" 3410e730a2SDan Gohman using namespace llvm; 3510e730a2SDan Gohman 3610e730a2SDan Gohman #define DEBUG_TYPE "wasm-lower" 3710e730a2SDan Gohman 3810e730a2SDan Gohman WebAssemblyTargetLowering::WebAssemblyTargetLowering( 3910e730a2SDan Gohman const TargetMachine &TM, const WebAssemblySubtarget &STI) 40bfaf7e15SDan Gohman : TargetLowering(TM), Subtarget(&STI) { 41af111db8SJF Bastien auto MVTPtr = Subtarget->hasAddr64() ? MVT::i64 : MVT::i32; 42af111db8SJF Bastien 4371d29aceSJF Bastien // Booleans always contain 0 or 1. 4471d29aceSJF Bastien setBooleanContents(ZeroOrOneBooleanContent); 45bfaf7e15SDan Gohman // WebAssembly does not produce floating-point exceptions on normal floating 46bfaf7e15SDan Gohman // point operations. 47bfaf7e15SDan Gohman setHasFloatingPointExceptions(false); 48489abd70SDan Gohman // We don't know the microarchitecture here, so just reduce register pressure. 49489abd70SDan Gohman setSchedulingPreference(Sched::RegPressure); 50b9073fb2SJF Bastien // Tell ISel that we have a stack pointer. 51b9073fb2SJF Bastien setStackPointerRegisterToSaveRestore( 52b9073fb2SJF Bastien Subtarget->hasAddr64() ? WebAssembly::SP64 : WebAssembly::SP32); 53b9073fb2SJF Bastien // Set up the register classes. 54d0bf9812SDan Gohman addRegisterClass(MVT::i32, &WebAssembly::I32RegClass); 55d0bf9812SDan Gohman addRegisterClass(MVT::i64, &WebAssembly::I64RegClass); 56d0bf9812SDan Gohman addRegisterClass(MVT::f32, &WebAssembly::F32RegClass); 57d0bf9812SDan Gohman addRegisterClass(MVT::f64, &WebAssembly::F64RegClass); 58b9073fb2SJF Bastien // Compute derived properties from the register classes. 59b9073fb2SJF Bastien computeRegisterProperties(Subtarget->getRegisterInfo()); 60b9073fb2SJF Bastien 61af111db8SJF Bastien setOperationAction(ISD::GlobalAddress, MVTPtr, Custom); 622c8fe6a4SDan Gohman setOperationAction(ISD::ExternalSymbol, MVTPtr, Custom); 63950a13cfSDan Gohman setOperationAction(ISD::JumpTable, MVTPtr, Custom); 64af111db8SJF Bastien 6535bfb24cSDan Gohman // Take the default expansion for va_arg, va_copy, and va_end. There is no 6635bfb24cSDan Gohman // default action for va_start, so we do that custom. 6735bfb24cSDan Gohman setOperationAction(ISD::VASTART, MVT::Other, Custom); 6835bfb24cSDan Gohman setOperationAction(ISD::VAARG, MVT::Other, Expand); 6935bfb24cSDan Gohman setOperationAction(ISD::VACOPY, MVT::Other, Expand); 7035bfb24cSDan Gohman setOperationAction(ISD::VAEND, MVT::Other, Expand); 7135bfb24cSDan Gohman 72da06bce8SJF Bastien for (auto T : {MVT::f32, MVT::f64}) { 73da06bce8SJF Bastien // Don't expand the floating-point types to constant pools. 74da06bce8SJF Bastien setOperationAction(ISD::ConstantFP, T, Legal); 75da06bce8SJF Bastien // Expand floating-point comparisons. 76da06bce8SJF Bastien for (auto CC : {ISD::SETO, ISD::SETUO, ISD::SETUEQ, ISD::SETONE, 77da06bce8SJF Bastien ISD::SETULT, ISD::SETULE, ISD::SETUGT, ISD::SETUGE}) 78da06bce8SJF Bastien setCondCodeAction(CC, T, Expand); 7932907a6bSDan Gohman // Expand floating-point library function operators. 80ebb23545SDan Gohman for (auto Op : {ISD::FSIN, ISD::FCOS, ISD::FSINCOS, ISD::FPOWI, ISD::FPOW, 819341c1d4SDan Gohman ISD::FREM, ISD::FMA}) 8232907a6bSDan Gohman setOperationAction(Op, T, Expand); 83896e53faSDan Gohman // Note supported floating-point library function operators that otherwise 84896e53faSDan Gohman // default to expand. 857a6b9825SDan Gohman for (auto Op : 867a6b9825SDan Gohman {ISD::FCEIL, ISD::FFLOOR, ISD::FTRUNC, ISD::FNEARBYINT, ISD::FRINT}) 87896e53faSDan Gohman setOperationAction(Op, T, Legal); 88b84ae9bbSDan Gohman // Support minnan and maxnan, which otherwise default to expand. 89b84ae9bbSDan Gohman setOperationAction(ISD::FMINNAN, T, Legal); 90b84ae9bbSDan Gohman setOperationAction(ISD::FMAXNAN, T, Legal); 91da06bce8SJF Bastien } 9232907a6bSDan Gohman 9332907a6bSDan Gohman for (auto T : {MVT::i32, MVT::i64}) { 9432907a6bSDan Gohman // Expand unavailable integer operations. 957a6b9825SDan Gohman for (auto Op : 967a6b9825SDan Gohman {ISD::BSWAP, ISD::ROTL, ISD::ROTR, ISD::SMUL_LOHI, ISD::UMUL_LOHI, 977a6b9825SDan Gohman ISD::MULHS, ISD::MULHU, ISD::SDIVREM, ISD::UDIVREM, ISD::SHL_PARTS, 987a6b9825SDan Gohman ISD::SRA_PARTS, ISD::SRL_PARTS, ISD::ADDC, ISD::ADDE, ISD::SUBC, 997a6b9825SDan Gohman ISD::SUBE}) { 10032907a6bSDan Gohman setOperationAction(Op, T, Expand); 10132907a6bSDan Gohman } 10232907a6bSDan Gohman } 10332907a6bSDan Gohman 10432907a6bSDan Gohman // As a special case, these operators use the type to mean the type to 10532907a6bSDan Gohman // sign-extend from. 106a5603b83SDan Gohman for (auto T : {MVT::i1, MVT::i8, MVT::i16, MVT::i32}) 10732907a6bSDan Gohman setOperationAction(ISD::SIGN_EXTEND_INREG, T, Expand); 10832907a6bSDan Gohman 10932907a6bSDan Gohman // Dynamic stack allocation: use the default expansion. 11032907a6bSDan Gohman setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); 11132907a6bSDan Gohman setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); 1122683a553SDan Gohman setOperationAction(ISD::DYNAMIC_STACKALLOC, MVTPtr, Expand); 11373ff6afaSJF Bastien 1149769debfSDerek Schuff setOperationAction(ISD::FrameIndex, MVT::i32, Custom); 1159769debfSDerek Schuff 116950a13cfSDan Gohman // Expand these forms; we pattern-match the forms that we can handle in isel. 117950a13cfSDan Gohman for (auto T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64}) 118950a13cfSDan Gohman for (auto Op : {ISD::BR_CC, ISD::SELECT_CC}) 119950a13cfSDan Gohman setOperationAction(Op, T, Expand); 120950a13cfSDan Gohman 121950a13cfSDan Gohman // We have custom switch handling. 122950a13cfSDan Gohman setOperationAction(ISD::BR_JT, MVT::Other, Custom); 123950a13cfSDan Gohman 12473ff6afaSJF Bastien // WebAssembly doesn't have: 12573ff6afaSJF Bastien // - Floating-point extending loads. 12673ff6afaSJF Bastien // - Floating-point truncating stores. 12773ff6afaSJF Bastien // - i1 extending loads. 12860bddf17SDan Gohman setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand); 12973ff6afaSJF Bastien setTruncStoreAction(MVT::f64, MVT::f32, Expand); 13073ff6afaSJF Bastien for (auto T : MVT::integer_valuetypes()) 13173ff6afaSJF Bastien for (auto Ext : {ISD::EXTLOAD, ISD::ZEXTLOAD, ISD::SEXTLOAD}) 13273ff6afaSJF Bastien setLoadExtAction(Ext, T, MVT::i1, Promote); 133ffa143ceSDerek Schuff 134ffa143ceSDerek Schuff // Trap lowers to wasm unreachable 135ffa143ceSDerek Schuff setOperationAction(ISD::TRAP, MVT::Other, Legal); 136bfaf7e15SDan Gohman } 13710e730a2SDan Gohman 1387b63484bSDan Gohman FastISel *WebAssemblyTargetLowering::createFastISel( 1397b63484bSDan Gohman FunctionLoweringInfo &FuncInfo, const TargetLibraryInfo *LibInfo) const { 1407b63484bSDan Gohman return WebAssembly::createFastISel(FuncInfo, LibInfo); 1417b63484bSDan Gohman } 1427b63484bSDan Gohman 143af111db8SJF Bastien bool WebAssemblyTargetLowering::isOffsetFoldingLegal( 1447a6b9825SDan Gohman const GlobalAddressSDNode * /*GA*/) const { 145a4b710a7SDan Gohman // All offsets can be folded. 146a4b710a7SDan Gohman return true; 147af111db8SJF Bastien } 148af111db8SJF Bastien 1497a6b9825SDan Gohman MVT WebAssemblyTargetLowering::getScalarShiftAmountTy(const DataLayout & /*DL*/, 150fda53373SJF Bastien EVT VT) const { 151a8483755SDan Gohman unsigned BitWidth = NextPowerOf2(VT.getSizeInBits() - 1); 152a8483755SDan Gohman if (BitWidth > 1 && BitWidth < 8) 153a8483755SDan Gohman BitWidth = 8; 15441729538SDan Gohman 15541729538SDan Gohman if (BitWidth > 64) { 15641729538SDan Gohman BitWidth = 64; 15741729538SDan Gohman assert(BitWidth >= Log2_32_Ceil(VT.getSizeInBits()) && 15841729538SDan Gohman "64-bit shift counts ought to be enough for anyone"); 15941729538SDan Gohman } 16041729538SDan Gohman 161a8483755SDan Gohman MVT Result = MVT::getIntegerVT(BitWidth); 162a8483755SDan Gohman assert(Result != MVT::INVALID_SIMPLE_VALUE_TYPE && 163a8483755SDan Gohman "Unable to represent scalar shift amount type"); 164a8483755SDan Gohman return Result; 165fda53373SJF Bastien } 166fda53373SJF Bastien 167480c8408SJF Bastien const char * 168480c8408SJF Bastien WebAssemblyTargetLowering::getTargetNodeName(unsigned Opcode) const { 169480c8408SJF Bastien switch (static_cast<WebAssemblyISD::NodeType>(Opcode)) { 170af111db8SJF Bastien case WebAssemblyISD::FIRST_NUMBER: 171af111db8SJF Bastien break; 172af111db8SJF Bastien #define HANDLE_NODETYPE(NODE) \ 173af111db8SJF Bastien case WebAssemblyISD::NODE: \ 174af111db8SJF Bastien return "WebAssemblyISD::" #NODE; 175af111db8SJF Bastien #include "WebAssemblyISD.def" 176af111db8SJF Bastien #undef HANDLE_NODETYPE 177480c8408SJF Bastien } 178480c8408SJF Bastien return nullptr; 179480c8408SJF Bastien } 180480c8408SJF Bastien 181f19ed562SDan Gohman std::pair<unsigned, const TargetRegisterClass *> 182f19ed562SDan Gohman WebAssemblyTargetLowering::getRegForInlineAsmConstraint( 183f19ed562SDan Gohman const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const { 184f19ed562SDan Gohman // First, see if this is a constraint that directly corresponds to a 185f19ed562SDan Gohman // WebAssembly register class. 186f19ed562SDan Gohman if (Constraint.size() == 1) { 187f19ed562SDan Gohman switch (Constraint[0]) { 188f19ed562SDan Gohman case 'r': 189284384b6SDan Gohman assert(VT != MVT::iPTR && "Pointer MVT not expected here"); 190284384b6SDan Gohman if (VT.isInteger() && !VT.isVector()) { 191284384b6SDan Gohman if (VT.getSizeInBits() <= 32) 192f19ed562SDan Gohman return std::make_pair(0U, &WebAssembly::I32RegClass); 193284384b6SDan Gohman if (VT.getSizeInBits() <= 64) 194a774d719SDan Gohman return std::make_pair(0U, &WebAssembly::I64RegClass); 195284384b6SDan Gohman } 196a774d719SDan Gohman break; 197f19ed562SDan Gohman default: 198f19ed562SDan Gohman break; 199f19ed562SDan Gohman } 200f19ed562SDan Gohman } 201f19ed562SDan Gohman 202f19ed562SDan Gohman return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); 203f19ed562SDan Gohman } 204f19ed562SDan Gohman 2053192ddfeSDan Gohman bool WebAssemblyTargetLowering::isCheapToSpeculateCttz() const { 2063192ddfeSDan Gohman // Assume ctz is a relatively cheap operation. 2073192ddfeSDan Gohman return true; 2083192ddfeSDan Gohman } 2093192ddfeSDan Gohman 2103192ddfeSDan Gohman bool WebAssemblyTargetLowering::isCheapToSpeculateCtlz() const { 2113192ddfeSDan Gohman // Assume clz is a relatively cheap operation. 2123192ddfeSDan Gohman return true; 2133192ddfeSDan Gohman } 2143192ddfeSDan Gohman 2154b9d7916SDan Gohman bool WebAssemblyTargetLowering::isLegalAddressingMode(const DataLayout &DL, 2164b9d7916SDan Gohman const AddrMode &AM, 2174b9d7916SDan Gohman Type *Ty, 2184b9d7916SDan Gohman unsigned AS) const { 2194b9d7916SDan Gohman // WebAssembly offsets are added as unsigned without wrapping. The 2204b9d7916SDan Gohman // isLegalAddressingMode gives us no way to determine if wrapping could be 2214b9d7916SDan Gohman // happening, so we approximate this by accepting only non-negative offsets. 2224b9d7916SDan Gohman if (AM.BaseOffs < 0) 2234b9d7916SDan Gohman return false; 2244b9d7916SDan Gohman 2254b9d7916SDan Gohman // WebAssembly has no scale register operands. 2264b9d7916SDan Gohman if (AM.Scale != 0) 2274b9d7916SDan Gohman return false; 2284b9d7916SDan Gohman 2294b9d7916SDan Gohman // Everything else is legal. 2304b9d7916SDan Gohman return true; 2314b9d7916SDan Gohman } 2324b9d7916SDan Gohman 233bb372243SDan Gohman bool WebAssemblyTargetLowering::allowsMisalignedMemoryAccesses( 234bb372243SDan Gohman EVT /*VT*/, unsigned /*AddrSpace*/, unsigned /*Align*/, 235bb372243SDan Gohman bool *Fast) const { 236bb372243SDan Gohman // WebAssembly supports unaligned accesses, though it should be declared 237bb372243SDan Gohman // with the p2align attribute on loads and stores which do so, and there 238bb372243SDan Gohman // may be a performance impact. We tell LLVM they're "fast" because 239fb619e96SDan Gohman // for the kinds of things that LLVM uses this for (merging adjacent stores 240bb372243SDan Gohman // of constants, etc.), WebAssembly implementations will either want the 241bb372243SDan Gohman // unaligned access or they'll split anyway. 242bb372243SDan Gohman if (Fast) 243bb372243SDan Gohman *Fast = true; 244bb372243SDan Gohman return true; 245bb372243SDan Gohman } 246bb372243SDan Gohman 24710e730a2SDan Gohman //===----------------------------------------------------------------------===// 24810e730a2SDan Gohman // WebAssembly Lowering private implementation. 24910e730a2SDan Gohman //===----------------------------------------------------------------------===// 25010e730a2SDan Gohman 25110e730a2SDan Gohman //===----------------------------------------------------------------------===// 25210e730a2SDan Gohman // Lowering Code 25310e730a2SDan Gohman //===----------------------------------------------------------------------===// 25410e730a2SDan Gohman 255b9073fb2SJF Bastien static void fail(SDLoc DL, SelectionDAG &DAG, const char *msg) { 256b9073fb2SJF Bastien MachineFunction &MF = DAG.getMachineFunction(); 257b9073fb2SJF Bastien DAG.getContext()->diagnose( 258*c8be5be9SBenjamin Kramer DiagnosticInfoUnsupported(*MF.getFunction(), msg, DL)); 259b9073fb2SJF Bastien } 260b9073fb2SJF Bastien 26185dbdda1SDan Gohman // Test whether the given calling convention is supported. 262a3f5ce5fSDan Gohman static bool CallingConvSupported(CallingConv::ID CallConv) { 26385dbdda1SDan Gohman // We currently support the language-independent target-independent 2641ce2b1afSDan Gohman // conventions. We don't yet have a way to annotate calls with properties like 2651ce2b1afSDan Gohman // "cold", and we don't have any call-clobbered registers, so these are mostly 2661ce2b1afSDan Gohman // all handled the same. 267a3f5ce5fSDan Gohman return CallConv == CallingConv::C || CallConv == CallingConv::Fast || 2681ce2b1afSDan Gohman CallConv == CallingConv::Cold || 2691ce2b1afSDan Gohman CallConv == CallingConv::PreserveMost || 2701ce2b1afSDan Gohman CallConv == CallingConv::PreserveAll || 2711ce2b1afSDan Gohman CallConv == CallingConv::CXX_FAST_TLS; 27285dbdda1SDan Gohman } 27385dbdda1SDan Gohman 274d8a9d66dSJF Bastien SDValue 275d8a9d66dSJF Bastien WebAssemblyTargetLowering::LowerCall(CallLoweringInfo &CLI, 276d8a9d66dSJF Bastien SmallVectorImpl<SDValue> &InVals) const { 277d8a9d66dSJF Bastien SelectionDAG &DAG = CLI.DAG; 278d8a9d66dSJF Bastien SDLoc DL = CLI.DL; 279d8a9d66dSJF Bastien SDValue Chain = CLI.Chain; 280d8a9d66dSJF Bastien SDValue Callee = CLI.Callee; 281d8a9d66dSJF Bastien MachineFunction &MF = DAG.getMachineFunction(); 282d8a9d66dSJF Bastien 283d8a9d66dSJF Bastien CallingConv::ID CallConv = CLI.CallConv; 28485dbdda1SDan Gohman if (!CallingConvSupported(CallConv)) 2859cc692b0SDan Gohman fail(DL, DAG, 2869cc692b0SDan Gohman "WebAssembly doesn't support language-specific or target-specific " 2879cc692b0SDan Gohman "calling conventions yet"); 288d8a9d66dSJF Bastien if (CLI.IsPatchPoint) 289d8a9d66dSJF Bastien fail(DL, DAG, "WebAssembly doesn't support patch point yet"); 290d8a9d66dSJF Bastien 2919cc692b0SDan Gohman // WebAssembly doesn't currently support explicit tail calls. If they are 2929cc692b0SDan Gohman // required, fail. Otherwise, just disable them. 2939cc692b0SDan Gohman if ((CallConv == CallingConv::Fast && CLI.IsTailCall && 2949cc692b0SDan Gohman MF.getTarget().Options.GuaranteedTailCallOpt) || 2959cc692b0SDan Gohman (CLI.CS && CLI.CS->isMustTailCall())) 2969cc692b0SDan Gohman fail(DL, DAG, "WebAssembly doesn't support tail call yet"); 2979cc692b0SDan Gohman CLI.IsTailCall = false; 2989cc692b0SDan Gohman 299d8a9d66dSJF Bastien SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; 300e590b33bSDan Gohman 301d8a9d66dSJF Bastien SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; 302e590b33bSDan Gohman if (Ins.size() > 1) 303e590b33bSDan Gohman fail(DL, DAG, "WebAssembly doesn't support more than 1 returned value yet"); 304e590b33bSDan Gohman 3052d822e73SDan Gohman SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; 3062d822e73SDan Gohman for (const ISD::OutputArg &Out : Outs) { 3077935fa3dSDan Gohman if (Out.Flags.isByVal()) 3087935fa3dSDan Gohman fail(DL, DAG, "WebAssembly hasn't implemented byval arguments"); 3097935fa3dSDan Gohman if (Out.Flags.isNest()) 3107935fa3dSDan Gohman fail(DL, DAG, "WebAssembly hasn't implemented nest arguments"); 3112d822e73SDan Gohman if (Out.Flags.isInAlloca()) 3127935fa3dSDan Gohman fail(DL, DAG, "WebAssembly hasn't implemented inalloca arguments"); 3132d822e73SDan Gohman if (Out.Flags.isInConsecutiveRegs()) 3147935fa3dSDan Gohman fail(DL, DAG, "WebAssembly hasn't implemented cons regs arguments"); 3152d822e73SDan Gohman if (Out.Flags.isInConsecutiveRegsLast()) 3167935fa3dSDan Gohman fail(DL, DAG, "WebAssembly hasn't implemented cons regs last arguments"); 3172d822e73SDan Gohman } 3182d822e73SDan Gohman 319d8a9d66dSJF Bastien bool IsVarArg = CLI.IsVarArg; 32035bfb24cSDan Gohman unsigned NumFixedArgs = CLI.NumFixedArgs; 32135bfb24cSDan Gohman auto PtrVT = getPointerTy(MF.getDataLayout()); 322e590b33bSDan Gohman 323d8a9d66dSJF Bastien // Analyze operands of the call, assigning locations to each operand. 324d8a9d66dSJF Bastien SmallVector<CCValAssign, 16> ArgLocs; 325d8a9d66dSJF Bastien CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext()); 326d8a9d66dSJF Bastien 32735bfb24cSDan Gohman if (IsVarArg) { 32835bfb24cSDan Gohman // Outgoing non-fixed arguments are placed at the top of the stack. First 32935bfb24cSDan Gohman // compute their offsets and the total amount of argument stack space 33035bfb24cSDan Gohman // needed. 33135bfb24cSDan Gohman for (SDValue Arg : 33235bfb24cSDan Gohman make_range(OutVals.begin() + NumFixedArgs, OutVals.end())) { 33335bfb24cSDan Gohman EVT VT = Arg.getValueType(); 33435bfb24cSDan Gohman assert(VT != MVT::iPTR && "Legalized args should be concrete"); 33535bfb24cSDan Gohman Type *Ty = VT.getTypeForEVT(*DAG.getContext()); 33635bfb24cSDan Gohman unsigned Offset = 33735bfb24cSDan Gohman CCInfo.AllocateStack(MF.getDataLayout().getTypeAllocSize(Ty), 33835bfb24cSDan Gohman MF.getDataLayout().getABITypeAlignment(Ty)); 33935bfb24cSDan Gohman CCInfo.addLoc(CCValAssign::getMem(ArgLocs.size(), VT.getSimpleVT(), 34035bfb24cSDan Gohman Offset, VT.getSimpleVT(), 34135bfb24cSDan Gohman CCValAssign::Full)); 34235bfb24cSDan Gohman } 34335bfb24cSDan Gohman } 34435bfb24cSDan Gohman 34535bfb24cSDan Gohman unsigned NumBytes = CCInfo.getAlignedCallFrameSize(); 34635bfb24cSDan Gohman 3475a143063SDerek Schuff SDValue NB; 3485a143063SDerek Schuff if (NumBytes) { 3495a143063SDerek Schuff NB = DAG.getConstant(NumBytes, DL, PtrVT, true); 350af111db8SJF Bastien Chain = DAG.getCALLSEQ_START(Chain, NB, DL); 3515a143063SDerek Schuff } 352d8a9d66dSJF Bastien 35335bfb24cSDan Gohman if (IsVarArg) { 35435bfb24cSDan Gohman // For non-fixed arguments, next emit stores to store the argument values 35535bfb24cSDan Gohman // to the stack at the offsets computed above. 35635bfb24cSDan Gohman SDValue SP = DAG.getCopyFromReg( 35735bfb24cSDan Gohman Chain, DL, getStackPointerRegisterToSaveRestore(), PtrVT); 35835bfb24cSDan Gohman unsigned ValNo = 0; 35935bfb24cSDan Gohman SmallVector<SDValue, 8> Chains; 36035bfb24cSDan Gohman for (SDValue Arg : 36135bfb24cSDan Gohman make_range(OutVals.begin() + NumFixedArgs, OutVals.end())) { 36235bfb24cSDan Gohman assert(ArgLocs[ValNo].getValNo() == ValNo && 36335bfb24cSDan Gohman "ArgLocs should remain in order and only hold varargs args"); 36435bfb24cSDan Gohman unsigned Offset = ArgLocs[ValNo++].getLocMemOffset(); 36535bfb24cSDan Gohman SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, SP, 36635bfb24cSDan Gohman DAG.getConstant(Offset, DL, PtrVT)); 36735bfb24cSDan Gohman Chains.push_back(DAG.getStore(Chain, DL, Arg, Add, 36835bfb24cSDan Gohman MachinePointerInfo::getStack(MF, Offset), 36935bfb24cSDan Gohman false, false, 0)); 37035bfb24cSDan Gohman } 37135bfb24cSDan Gohman if (!Chains.empty()) 37235bfb24cSDan Gohman Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains); 37335bfb24cSDan Gohman } 37435bfb24cSDan Gohman 37535bfb24cSDan Gohman // Compute the operands for the CALLn node. 376d8a9d66dSJF Bastien SmallVector<SDValue, 16> Ops; 377d8a9d66dSJF Bastien Ops.push_back(Chain); 378af111db8SJF Bastien Ops.push_back(Callee); 37935bfb24cSDan Gohman 38035bfb24cSDan Gohman // Add all fixed arguments. Note that for non-varargs calls, NumFixedArgs 38135bfb24cSDan Gohman // isn't reliable. 38235bfb24cSDan Gohman Ops.append(OutVals.begin(), 38335bfb24cSDan Gohman IsVarArg ? OutVals.begin() + NumFixedArgs : OutVals.end()); 384d8a9d66dSJF Bastien 385d8a9d66dSJF Bastien SmallVector<EVT, 8> Tys; 3862d822e73SDan Gohman for (const auto &In : Ins) { 3877935fa3dSDan Gohman assert(!In.Flags.isByVal() && "byval is not valid for return values"); 3887935fa3dSDan Gohman assert(!In.Flags.isNest() && "nest is not valid for return values"); 3892d822e73SDan Gohman if (In.Flags.isInAlloca()) 3907935fa3dSDan Gohman fail(DL, DAG, "WebAssembly hasn't implemented inalloca return values"); 3912d822e73SDan Gohman if (In.Flags.isInConsecutiveRegs()) 3927935fa3dSDan Gohman fail(DL, DAG, "WebAssembly hasn't implemented cons regs return values"); 3932d822e73SDan Gohman if (In.Flags.isInConsecutiveRegsLast()) 3944b9d7916SDan Gohman fail(DL, DAG, 3954b9d7916SDan Gohman "WebAssembly hasn't implemented cons regs last return values"); 3962d822e73SDan Gohman // Ignore In.getOrigAlign() because all our arguments are passed in 3972d822e73SDan Gohman // registers. 398d8a9d66dSJF Bastien Tys.push_back(In.VT); 3992d822e73SDan Gohman } 400d8a9d66dSJF Bastien Tys.push_back(MVT::Other); 401af111db8SJF Bastien SDVTList TyList = DAG.getVTList(Tys); 402f71abef7SDan Gohman SDValue Res = 403f71abef7SDan Gohman DAG.getNode(Ins.empty() ? WebAssemblyISD::CALL0 : WebAssemblyISD::CALL1, 404f71abef7SDan Gohman DL, TyList, Ops); 405af111db8SJF Bastien if (Ins.empty()) { 406af111db8SJF Bastien Chain = Res; 407af111db8SJF Bastien } else { 408d8a9d66dSJF Bastien InVals.push_back(Res); 409d8a9d66dSJF Bastien Chain = Res.getValue(1); 410af111db8SJF Bastien } 411d8a9d66dSJF Bastien 4125a143063SDerek Schuff if (NumBytes) { 4138bb5f292SDerek Schuff SDValue Unused = DAG.getTargetConstant(0, DL, PtrVT); 41435bfb24cSDan Gohman Chain = DAG.getCALLSEQ_END(Chain, NB, Unused, SDValue(), DL); 4155a143063SDerek Schuff } 416d8a9d66dSJF Bastien 417d8a9d66dSJF Bastien return Chain; 418d8a9d66dSJF Bastien } 419d8a9d66dSJF Bastien 420b9073fb2SJF Bastien bool WebAssemblyTargetLowering::CanLowerReturn( 4217a6b9825SDan Gohman CallingConv::ID /*CallConv*/, MachineFunction & /*MF*/, bool /*IsVarArg*/, 4227a6b9825SDan Gohman const SmallVectorImpl<ISD::OutputArg> &Outs, 4237a6b9825SDan Gohman LLVMContext & /*Context*/) const { 424b9073fb2SJF Bastien // WebAssembly can't currently handle returning tuples. 425b9073fb2SJF Bastien return Outs.size() <= 1; 426b9073fb2SJF Bastien } 427b9073fb2SJF Bastien 428b9073fb2SJF Bastien SDValue WebAssemblyTargetLowering::LowerReturn( 42935bfb24cSDan Gohman SDValue Chain, CallingConv::ID CallConv, bool /*IsVarArg*/, 430b9073fb2SJF Bastien const SmallVectorImpl<ISD::OutputArg> &Outs, 431b9073fb2SJF Bastien const SmallVectorImpl<SDValue> &OutVals, SDLoc DL, 432b9073fb2SJF Bastien SelectionDAG &DAG) const { 433b9073fb2SJF Bastien assert(Outs.size() <= 1 && "WebAssembly can only return up to one value"); 43485dbdda1SDan Gohman if (!CallingConvSupported(CallConv)) 435b9073fb2SJF Bastien fail(DL, DAG, "WebAssembly doesn't support non-C calling conventions"); 436b9073fb2SJF Bastien 437600aee98SJF Bastien SmallVector<SDValue, 4> RetOps(1, Chain); 438600aee98SJF Bastien RetOps.append(OutVals.begin(), OutVals.end()); 4394a2d5604SJF Bastien Chain = DAG.getNode(WebAssemblyISD::RETURN, DL, MVT::Other, RetOps); 440b9073fb2SJF Bastien 441754cd11dSDan Gohman // Record the number and types of the return values. 442754cd11dSDan Gohman for (const ISD::OutputArg &Out : Outs) { 443ac132e93SDan Gohman assert(!Out.Flags.isByVal() && "byval is not valid for return values"); 444ac132e93SDan Gohman assert(!Out.Flags.isNest() && "nest is not valid for return values"); 44535bfb24cSDan Gohman assert(Out.IsFixed && "non-fixed return value is not valid"); 446754cd11dSDan Gohman if (Out.Flags.isInAlloca()) 447754cd11dSDan Gohman fail(DL, DAG, "WebAssembly hasn't implemented inalloca results"); 448754cd11dSDan Gohman if (Out.Flags.isInConsecutiveRegs()) 449754cd11dSDan Gohman fail(DL, DAG, "WebAssembly hasn't implemented cons regs results"); 450754cd11dSDan Gohman if (Out.Flags.isInConsecutiveRegsLast()) 451754cd11dSDan Gohman fail(DL, DAG, "WebAssembly hasn't implemented cons regs last results"); 452754cd11dSDan Gohman } 453754cd11dSDan Gohman 454b9073fb2SJF Bastien return Chain; 455b9073fb2SJF Bastien } 456b9073fb2SJF Bastien 457b9073fb2SJF Bastien SDValue WebAssemblyTargetLowering::LowerFormalArguments( 45835bfb24cSDan Gohman SDValue Chain, CallingConv::ID CallConv, bool /*IsVarArg*/, 459b9073fb2SJF Bastien const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG, 460b9073fb2SJF Bastien SmallVectorImpl<SDValue> &InVals) const { 461b9073fb2SJF Bastien MachineFunction &MF = DAG.getMachineFunction(); 462b9073fb2SJF Bastien 46385dbdda1SDan Gohman if (!CallingConvSupported(CallConv)) 464b9073fb2SJF Bastien fail(DL, DAG, "WebAssembly doesn't support non-C calling conventions"); 465b9073fb2SJF Bastien 466fb3e0594SDan Gohman // Set up the incoming ARGUMENTS value, which serves to represent the liveness 467fb3e0594SDan Gohman // of the incoming values before they're represented by virtual registers. 468fb3e0594SDan Gohman MF.getRegInfo().addLiveIn(WebAssembly::ARGUMENTS); 469fb3e0594SDan Gohman 470600aee98SJF Bastien for (const ISD::InputArg &In : Ins) { 471600aee98SJF Bastien if (In.Flags.isByVal()) 472600aee98SJF Bastien fail(DL, DAG, "WebAssembly hasn't implemented byval arguments"); 473600aee98SJF Bastien if (In.Flags.isInAlloca()) 474600aee98SJF Bastien fail(DL, DAG, "WebAssembly hasn't implemented inalloca arguments"); 475600aee98SJF Bastien if (In.Flags.isNest()) 476600aee98SJF Bastien fail(DL, DAG, "WebAssembly hasn't implemented nest arguments"); 477600aee98SJF Bastien if (In.Flags.isInConsecutiveRegs()) 478600aee98SJF Bastien fail(DL, DAG, "WebAssembly hasn't implemented cons regs arguments"); 479600aee98SJF Bastien if (In.Flags.isInConsecutiveRegsLast()) 480600aee98SJF Bastien fail(DL, DAG, "WebAssembly hasn't implemented cons regs last arguments"); 4819c54d3b4SDan Gohman // Ignore In.getOrigAlign() because all our arguments are passed in 4829c54d3b4SDan Gohman // registers. 483d7fcc6f9SJF Bastien InVals.push_back( 484d7fcc6f9SJF Bastien In.Used 485d7fcc6f9SJF Bastien ? DAG.getNode(WebAssemblyISD::ARGUMENT, DL, In.VT, 4865219ecf0SDan Gohman DAG.getTargetConstant(InVals.size(), DL, MVT::i32)) 487cb7940f9SDan Gohman : DAG.getUNDEF(In.VT)); 488754cd11dSDan Gohman 489754cd11dSDan Gohman // Record the number and types of arguments. 490754cd11dSDan Gohman MF.getInfo<WebAssemblyFunctionInfo>()->addParam(In.VT); 491600aee98SJF Bastien } 492b9073fb2SJF Bastien 49335bfb24cSDan Gohman // Incoming varargs arguments are on the stack and will be accessed through 49435bfb24cSDan Gohman // va_arg, so we don't need to do anything for them here. 49535bfb24cSDan Gohman 496b9073fb2SJF Bastien return Chain; 497b9073fb2SJF Bastien } 498b9073fb2SJF Bastien 49910e730a2SDan Gohman //===----------------------------------------------------------------------===// 500af111db8SJF Bastien // Custom lowering hooks. 50110e730a2SDan Gohman //===----------------------------------------------------------------------===// 50210e730a2SDan Gohman 503af111db8SJF Bastien SDValue WebAssemblyTargetLowering::LowerOperation(SDValue Op, 504af111db8SJF Bastien SelectionDAG &DAG) const { 505af111db8SJF Bastien switch (Op.getOpcode()) { 506af111db8SJF Bastien default: 507af111db8SJF Bastien llvm_unreachable("unimplemented operation lowering"); 508af111db8SJF Bastien return SDValue(); 5099769debfSDerek Schuff case ISD::FrameIndex: 5109769debfSDerek Schuff return LowerFrameIndex(Op, DAG); 511af111db8SJF Bastien case ISD::GlobalAddress: 512af111db8SJF Bastien return LowerGlobalAddress(Op, DAG); 5132c8fe6a4SDan Gohman case ISD::ExternalSymbol: 5142c8fe6a4SDan Gohman return LowerExternalSymbol(Op, DAG); 515950a13cfSDan Gohman case ISD::JumpTable: 516950a13cfSDan Gohman return LowerJumpTable(Op, DAG); 517950a13cfSDan Gohman case ISD::BR_JT: 518950a13cfSDan Gohman return LowerBR_JT(Op, DAG); 51935bfb24cSDan Gohman case ISD::VASTART: 52035bfb24cSDan Gohman return LowerVASTART(Op, DAG); 521af111db8SJF Bastien } 522af111db8SJF Bastien } 523af111db8SJF Bastien 5249769debfSDerek Schuff SDValue WebAssemblyTargetLowering::LowerFrameIndex(SDValue Op, 5259769debfSDerek Schuff SelectionDAG &DAG) const { 5269769debfSDerek Schuff int FI = cast<FrameIndexSDNode>(Op)->getIndex(); 5279769debfSDerek Schuff return DAG.getTargetFrameIndex(FI, Op.getValueType()); 5289769debfSDerek Schuff } 5299769debfSDerek Schuff 530af111db8SJF Bastien SDValue WebAssemblyTargetLowering::LowerGlobalAddress(SDValue Op, 531af111db8SJF Bastien SelectionDAG &DAG) const { 532af111db8SJF Bastien SDLoc DL(Op); 533af111db8SJF Bastien const auto *GA = cast<GlobalAddressSDNode>(Op); 534af111db8SJF Bastien EVT VT = Op.getValueType(); 53526c6765bSDan Gohman assert(GA->getTargetFlags() == 0 && 53626c6765bSDan Gohman "Unexpected target flags on generic GlobalAddressSDNode"); 537af111db8SJF Bastien if (GA->getAddressSpace() != 0) 538af111db8SJF Bastien fail(DL, DAG, "WebAssembly only expects the 0 address space"); 5394b9d7916SDan Gohman return DAG.getNode( 5404b9d7916SDan Gohman WebAssemblyISD::Wrapper, DL, VT, 5414b9d7916SDan Gohman DAG.getTargetGlobalAddress(GA->getGlobal(), DL, VT, GA->getOffset())); 542af111db8SJF Bastien } 543af111db8SJF Bastien 5447a6b9825SDan Gohman SDValue 5457a6b9825SDan Gohman WebAssemblyTargetLowering::LowerExternalSymbol(SDValue Op, 5462c8fe6a4SDan Gohman SelectionDAG &DAG) const { 5472c8fe6a4SDan Gohman SDLoc DL(Op); 5482c8fe6a4SDan Gohman const auto *ES = cast<ExternalSymbolSDNode>(Op); 5492c8fe6a4SDan Gohman EVT VT = Op.getValueType(); 55026c6765bSDan Gohman assert(ES->getTargetFlags() == 0 && 55126c6765bSDan Gohman "Unexpected target flags on generic ExternalSymbolSDNode"); 55226c6765bSDan Gohman // Set the TargetFlags to 0x1 which indicates that this is a "function" 55326c6765bSDan Gohman // symbol rather than a data symbol. We do this unconditionally even though 55426c6765bSDan Gohman // we don't know anything about the symbol other than its name, because all 55526c6765bSDan Gohman // external symbols used in target-independent SelectionDAG code are for 55626c6765bSDan Gohman // functions. 5572c8fe6a4SDan Gohman return DAG.getNode(WebAssemblyISD::Wrapper, DL, VT, 55826c6765bSDan Gohman DAG.getTargetExternalSymbol(ES->getSymbol(), VT, 55926c6765bSDan Gohman /*TargetFlags=*/0x1)); 5602c8fe6a4SDan Gohman } 5612c8fe6a4SDan Gohman 562950a13cfSDan Gohman SDValue WebAssemblyTargetLowering::LowerJumpTable(SDValue Op, 563950a13cfSDan Gohman SelectionDAG &DAG) const { 564950a13cfSDan Gohman // There's no need for a Wrapper node because we always incorporate a jump 565bb7ce8e4SDan Gohman // table operand into a TABLESWITCH instruction, rather than ever 566bb7ce8e4SDan Gohman // materializing it in a register. 567950a13cfSDan Gohman const JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); 568950a13cfSDan Gohman return DAG.getTargetJumpTable(JT->getIndex(), Op.getValueType(), 569950a13cfSDan Gohman JT->getTargetFlags()); 570950a13cfSDan Gohman } 571950a13cfSDan Gohman 572950a13cfSDan Gohman SDValue WebAssemblyTargetLowering::LowerBR_JT(SDValue Op, 573950a13cfSDan Gohman SelectionDAG &DAG) const { 574950a13cfSDan Gohman SDLoc DL(Op); 575950a13cfSDan Gohman SDValue Chain = Op.getOperand(0); 576950a13cfSDan Gohman const auto *JT = cast<JumpTableSDNode>(Op.getOperand(1)); 577950a13cfSDan Gohman SDValue Index = Op.getOperand(2); 578950a13cfSDan Gohman assert(JT->getTargetFlags() == 0 && "WebAssembly doesn't set target flags"); 579950a13cfSDan Gohman 580950a13cfSDan Gohman SmallVector<SDValue, 8> Ops; 581950a13cfSDan Gohman Ops.push_back(Chain); 582950a13cfSDan Gohman Ops.push_back(Index); 583950a13cfSDan Gohman 584950a13cfSDan Gohman MachineJumpTableInfo *MJTI = DAG.getMachineFunction().getJumpTableInfo(); 585950a13cfSDan Gohman const auto &MBBs = MJTI->getJumpTables()[JT->getIndex()].MBBs; 586950a13cfSDan Gohman 587950a13cfSDan Gohman // TODO: For now, we just pick something arbitrary for a default case for now. 588950a13cfSDan Gohman // We really want to sniff out the guard and put in the real default case (and 589950a13cfSDan Gohman // delete the guard). 590950a13cfSDan Gohman Ops.push_back(DAG.getBasicBlock(MBBs[0])); 591950a13cfSDan Gohman 592950a13cfSDan Gohman // Add an operand for each case. 593950a13cfSDan Gohman for (auto MBB : MBBs) 594950a13cfSDan Gohman Ops.push_back(DAG.getBasicBlock(MBB)); 595950a13cfSDan Gohman 596bb7ce8e4SDan Gohman return DAG.getNode(WebAssemblyISD::TABLESWITCH, DL, MVT::Other, Ops); 597950a13cfSDan Gohman } 598950a13cfSDan Gohman 59935bfb24cSDan Gohman SDValue WebAssemblyTargetLowering::LowerVASTART(SDValue Op, 60035bfb24cSDan Gohman SelectionDAG &DAG) const { 60135bfb24cSDan Gohman SDLoc DL(Op); 60235bfb24cSDan Gohman EVT PtrVT = getPointerTy(DAG.getMachineFunction().getDataLayout()); 60335bfb24cSDan Gohman 60435bfb24cSDan Gohman // The incoming non-fixed arguments are placed on the top of the stack, with 60535bfb24cSDan Gohman // natural alignment, at the point of the call, so the base pointer is just 60635bfb24cSDan Gohman // the current frame pointer. 60735bfb24cSDan Gohman DAG.getMachineFunction().getFrameInfo()->setFrameAddressIsTaken(true); 60835bfb24cSDan Gohman unsigned FP = 609fd98ea89SDan Gohman Subtarget->getRegisterInfo()->getFrameRegister(DAG.getMachineFunction()); 61035bfb24cSDan Gohman SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL, FP, PtrVT); 61135bfb24cSDan Gohman const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 61235bfb24cSDan Gohman return DAG.getStore(Op.getOperand(0), DL, FrameAddr, Op.getOperand(1), 61335bfb24cSDan Gohman MachinePointerInfo(SV), false, false, 0); 61435bfb24cSDan Gohman } 61535bfb24cSDan Gohman 61610e730a2SDan Gohman //===----------------------------------------------------------------------===// 61710e730a2SDan Gohman // WebAssembly Optimization Hooks 61810e730a2SDan Gohman //===----------------------------------------------------------------------===// 619