1*9880d681SAndroid Build Coastguard Worker //=- WebAssemblyISelLowering.cpp - WebAssembly DAG Lowering Implementation -==//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker ///
10*9880d681SAndroid Build Coastguard Worker /// \file
11*9880d681SAndroid Build Coastguard Worker /// \brief This file implements the WebAssemblyTargetLowering class.
12*9880d681SAndroid Build Coastguard Worker ///
13*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
14*9880d681SAndroid Build Coastguard Worker
15*9880d681SAndroid Build Coastguard Worker #include "WebAssemblyISelLowering.h"
16*9880d681SAndroid Build Coastguard Worker #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
17*9880d681SAndroid Build Coastguard Worker #include "WebAssemblyMachineFunctionInfo.h"
18*9880d681SAndroid Build Coastguard Worker #include "WebAssemblySubtarget.h"
19*9880d681SAndroid Build Coastguard Worker #include "WebAssemblyTargetMachine.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/Analysis.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/CallingConvLower.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineJumpTableInfo.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/SelectionDAG.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DiagnosticInfo.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DiagnosticPrinter.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Intrinsics.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetOptions.h"
33*9880d681SAndroid Build Coastguard Worker using namespace llvm;
34*9880d681SAndroid Build Coastguard Worker
35*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "wasm-lower"
36*9880d681SAndroid Build Coastguard Worker
WebAssemblyTargetLowering(const TargetMachine & TM,const WebAssemblySubtarget & STI)37*9880d681SAndroid Build Coastguard Worker WebAssemblyTargetLowering::WebAssemblyTargetLowering(
38*9880d681SAndroid Build Coastguard Worker const TargetMachine &TM, const WebAssemblySubtarget &STI)
39*9880d681SAndroid Build Coastguard Worker : TargetLowering(TM), Subtarget(&STI) {
40*9880d681SAndroid Build Coastguard Worker auto MVTPtr = Subtarget->hasAddr64() ? MVT::i64 : MVT::i32;
41*9880d681SAndroid Build Coastguard Worker
42*9880d681SAndroid Build Coastguard Worker // Booleans always contain 0 or 1.
43*9880d681SAndroid Build Coastguard Worker setBooleanContents(ZeroOrOneBooleanContent);
44*9880d681SAndroid Build Coastguard Worker // WebAssembly does not produce floating-point exceptions on normal floating
45*9880d681SAndroid Build Coastguard Worker // point operations.
46*9880d681SAndroid Build Coastguard Worker setHasFloatingPointExceptions(false);
47*9880d681SAndroid Build Coastguard Worker // We don't know the microarchitecture here, so just reduce register pressure.
48*9880d681SAndroid Build Coastguard Worker setSchedulingPreference(Sched::RegPressure);
49*9880d681SAndroid Build Coastguard Worker // Tell ISel that we have a stack pointer.
50*9880d681SAndroid Build Coastguard Worker setStackPointerRegisterToSaveRestore(
51*9880d681SAndroid Build Coastguard Worker Subtarget->hasAddr64() ? WebAssembly::SP64 : WebAssembly::SP32);
52*9880d681SAndroid Build Coastguard Worker // Set up the register classes.
53*9880d681SAndroid Build Coastguard Worker addRegisterClass(MVT::i32, &WebAssembly::I32RegClass);
54*9880d681SAndroid Build Coastguard Worker addRegisterClass(MVT::i64, &WebAssembly::I64RegClass);
55*9880d681SAndroid Build Coastguard Worker addRegisterClass(MVT::f32, &WebAssembly::F32RegClass);
56*9880d681SAndroid Build Coastguard Worker addRegisterClass(MVT::f64, &WebAssembly::F64RegClass);
57*9880d681SAndroid Build Coastguard Worker // Compute derived properties from the register classes.
58*9880d681SAndroid Build Coastguard Worker computeRegisterProperties(Subtarget->getRegisterInfo());
59*9880d681SAndroid Build Coastguard Worker
60*9880d681SAndroid Build Coastguard Worker setOperationAction(ISD::GlobalAddress, MVTPtr, Custom);
61*9880d681SAndroid Build Coastguard Worker setOperationAction(ISD::ExternalSymbol, MVTPtr, Custom);
62*9880d681SAndroid Build Coastguard Worker setOperationAction(ISD::JumpTable, MVTPtr, Custom);
63*9880d681SAndroid Build Coastguard Worker setOperationAction(ISD::BlockAddress, MVTPtr, Custom);
64*9880d681SAndroid Build Coastguard Worker setOperationAction(ISD::BRIND, MVT::Other, Custom);
65*9880d681SAndroid Build Coastguard Worker
66*9880d681SAndroid Build Coastguard Worker // Take the default expansion for va_arg, va_copy, and va_end. There is no
67*9880d681SAndroid Build Coastguard Worker // default action for va_start, so we do that custom.
68*9880d681SAndroid Build Coastguard Worker setOperationAction(ISD::VASTART, MVT::Other, Custom);
69*9880d681SAndroid Build Coastguard Worker setOperationAction(ISD::VAARG, MVT::Other, Expand);
70*9880d681SAndroid Build Coastguard Worker setOperationAction(ISD::VACOPY, MVT::Other, Expand);
71*9880d681SAndroid Build Coastguard Worker setOperationAction(ISD::VAEND, MVT::Other, Expand);
72*9880d681SAndroid Build Coastguard Worker
73*9880d681SAndroid Build Coastguard Worker for (auto T : {MVT::f32, MVT::f64}) {
74*9880d681SAndroid Build Coastguard Worker // Don't expand the floating-point types to constant pools.
75*9880d681SAndroid Build Coastguard Worker setOperationAction(ISD::ConstantFP, T, Legal);
76*9880d681SAndroid Build Coastguard Worker // Expand floating-point comparisons.
77*9880d681SAndroid Build Coastguard Worker for (auto CC : {ISD::SETO, ISD::SETUO, ISD::SETUEQ, ISD::SETONE,
78*9880d681SAndroid Build Coastguard Worker ISD::SETULT, ISD::SETULE, ISD::SETUGT, ISD::SETUGE})
79*9880d681SAndroid Build Coastguard Worker setCondCodeAction(CC, T, Expand);
80*9880d681SAndroid Build Coastguard Worker // Expand floating-point library function operators.
81*9880d681SAndroid Build Coastguard Worker for (auto Op : {ISD::FSIN, ISD::FCOS, ISD::FSINCOS, ISD::FPOWI, ISD::FPOW,
82*9880d681SAndroid Build Coastguard Worker ISD::FREM, ISD::FMA})
83*9880d681SAndroid Build Coastguard Worker setOperationAction(Op, T, Expand);
84*9880d681SAndroid Build Coastguard Worker // Note supported floating-point library function operators that otherwise
85*9880d681SAndroid Build Coastguard Worker // default to expand.
86*9880d681SAndroid Build Coastguard Worker for (auto Op :
87*9880d681SAndroid Build Coastguard Worker {ISD::FCEIL, ISD::FFLOOR, ISD::FTRUNC, ISD::FNEARBYINT, ISD::FRINT})
88*9880d681SAndroid Build Coastguard Worker setOperationAction(Op, T, Legal);
89*9880d681SAndroid Build Coastguard Worker // Support minnan and maxnan, which otherwise default to expand.
90*9880d681SAndroid Build Coastguard Worker setOperationAction(ISD::FMINNAN, T, Legal);
91*9880d681SAndroid Build Coastguard Worker setOperationAction(ISD::FMAXNAN, T, Legal);
92*9880d681SAndroid Build Coastguard Worker }
93*9880d681SAndroid Build Coastguard Worker
94*9880d681SAndroid Build Coastguard Worker for (auto T : {MVT::i32, MVT::i64}) {
95*9880d681SAndroid Build Coastguard Worker // Expand unavailable integer operations.
96*9880d681SAndroid Build Coastguard Worker for (auto Op :
97*9880d681SAndroid Build Coastguard Worker {ISD::BSWAP, ISD::SMUL_LOHI, ISD::UMUL_LOHI,
98*9880d681SAndroid Build Coastguard Worker ISD::MULHS, ISD::MULHU, ISD::SDIVREM, ISD::UDIVREM, ISD::SHL_PARTS,
99*9880d681SAndroid Build Coastguard Worker ISD::SRA_PARTS, ISD::SRL_PARTS, ISD::ADDC, ISD::ADDE, ISD::SUBC,
100*9880d681SAndroid Build Coastguard Worker ISD::SUBE}) {
101*9880d681SAndroid Build Coastguard Worker setOperationAction(Op, T, Expand);
102*9880d681SAndroid Build Coastguard Worker }
103*9880d681SAndroid Build Coastguard Worker }
104*9880d681SAndroid Build Coastguard Worker
105*9880d681SAndroid Build Coastguard Worker // As a special case, these operators use the type to mean the type to
106*9880d681SAndroid Build Coastguard Worker // sign-extend from.
107*9880d681SAndroid Build Coastguard Worker for (auto T : {MVT::i1, MVT::i8, MVT::i16, MVT::i32})
108*9880d681SAndroid Build Coastguard Worker setOperationAction(ISD::SIGN_EXTEND_INREG, T, Expand);
109*9880d681SAndroid Build Coastguard Worker
110*9880d681SAndroid Build Coastguard Worker // Dynamic stack allocation: use the default expansion.
111*9880d681SAndroid Build Coastguard Worker setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
112*9880d681SAndroid Build Coastguard Worker setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
113*9880d681SAndroid Build Coastguard Worker setOperationAction(ISD::DYNAMIC_STACKALLOC, MVTPtr, Expand);
114*9880d681SAndroid Build Coastguard Worker
115*9880d681SAndroid Build Coastguard Worker setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
116*9880d681SAndroid Build Coastguard Worker setOperationAction(ISD::CopyToReg, MVT::Other, Custom);
117*9880d681SAndroid Build Coastguard Worker
118*9880d681SAndroid Build Coastguard Worker // Expand these forms; we pattern-match the forms that we can handle in isel.
119*9880d681SAndroid Build Coastguard Worker for (auto T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64})
120*9880d681SAndroid Build Coastguard Worker for (auto Op : {ISD::BR_CC, ISD::SELECT_CC})
121*9880d681SAndroid Build Coastguard Worker setOperationAction(Op, T, Expand);
122*9880d681SAndroid Build Coastguard Worker
123*9880d681SAndroid Build Coastguard Worker // We have custom switch handling.
124*9880d681SAndroid Build Coastguard Worker setOperationAction(ISD::BR_JT, MVT::Other, Custom);
125*9880d681SAndroid Build Coastguard Worker
126*9880d681SAndroid Build Coastguard Worker // WebAssembly doesn't have:
127*9880d681SAndroid Build Coastguard Worker // - Floating-point extending loads.
128*9880d681SAndroid Build Coastguard Worker // - Floating-point truncating stores.
129*9880d681SAndroid Build Coastguard Worker // - i1 extending loads.
130*9880d681SAndroid Build Coastguard Worker setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand);
131*9880d681SAndroid Build Coastguard Worker setTruncStoreAction(MVT::f64, MVT::f32, Expand);
132*9880d681SAndroid Build Coastguard Worker for (auto T : MVT::integer_valuetypes())
133*9880d681SAndroid Build Coastguard Worker for (auto Ext : {ISD::EXTLOAD, ISD::ZEXTLOAD, ISD::SEXTLOAD})
134*9880d681SAndroid Build Coastguard Worker setLoadExtAction(Ext, T, MVT::i1, Promote);
135*9880d681SAndroid Build Coastguard Worker
136*9880d681SAndroid Build Coastguard Worker // Trap lowers to wasm unreachable
137*9880d681SAndroid Build Coastguard Worker setOperationAction(ISD::TRAP, MVT::Other, Legal);
138*9880d681SAndroid Build Coastguard Worker }
139*9880d681SAndroid Build Coastguard Worker
createFastISel(FunctionLoweringInfo & FuncInfo,const TargetLibraryInfo * LibInfo) const140*9880d681SAndroid Build Coastguard Worker FastISel *WebAssemblyTargetLowering::createFastISel(
141*9880d681SAndroid Build Coastguard Worker FunctionLoweringInfo &FuncInfo, const TargetLibraryInfo *LibInfo) const {
142*9880d681SAndroid Build Coastguard Worker return WebAssembly::createFastISel(FuncInfo, LibInfo);
143*9880d681SAndroid Build Coastguard Worker }
144*9880d681SAndroid Build Coastguard Worker
isOffsetFoldingLegal(const GlobalAddressSDNode *) const145*9880d681SAndroid Build Coastguard Worker bool WebAssemblyTargetLowering::isOffsetFoldingLegal(
146*9880d681SAndroid Build Coastguard Worker const GlobalAddressSDNode * /*GA*/) const {
147*9880d681SAndroid Build Coastguard Worker // All offsets can be folded.
148*9880d681SAndroid Build Coastguard Worker return true;
149*9880d681SAndroid Build Coastguard Worker }
150*9880d681SAndroid Build Coastguard Worker
getScalarShiftAmountTy(const DataLayout &,EVT VT) const151*9880d681SAndroid Build Coastguard Worker MVT WebAssemblyTargetLowering::getScalarShiftAmountTy(const DataLayout & /*DL*/,
152*9880d681SAndroid Build Coastguard Worker EVT VT) const {
153*9880d681SAndroid Build Coastguard Worker unsigned BitWidth = NextPowerOf2(VT.getSizeInBits() - 1);
154*9880d681SAndroid Build Coastguard Worker if (BitWidth > 1 && BitWidth < 8) BitWidth = 8;
155*9880d681SAndroid Build Coastguard Worker
156*9880d681SAndroid Build Coastguard Worker if (BitWidth > 64) {
157*9880d681SAndroid Build Coastguard Worker // The shift will be lowered to a libcall, and compiler-rt libcalls expect
158*9880d681SAndroid Build Coastguard Worker // the count to be an i32.
159*9880d681SAndroid Build Coastguard Worker BitWidth = 32;
160*9880d681SAndroid Build Coastguard Worker assert(BitWidth >= Log2_32_Ceil(VT.getSizeInBits()) &&
161*9880d681SAndroid Build Coastguard Worker "32-bit shift counts ought to be enough for anyone");
162*9880d681SAndroid Build Coastguard Worker }
163*9880d681SAndroid Build Coastguard Worker
164*9880d681SAndroid Build Coastguard Worker MVT Result = MVT::getIntegerVT(BitWidth);
165*9880d681SAndroid Build Coastguard Worker assert(Result != MVT::INVALID_SIMPLE_VALUE_TYPE &&
166*9880d681SAndroid Build Coastguard Worker "Unable to represent scalar shift amount type");
167*9880d681SAndroid Build Coastguard Worker return Result;
168*9880d681SAndroid Build Coastguard Worker }
169*9880d681SAndroid Build Coastguard Worker
getTargetNodeName(unsigned Opcode) const170*9880d681SAndroid Build Coastguard Worker const char *WebAssemblyTargetLowering::getTargetNodeName(
171*9880d681SAndroid Build Coastguard Worker unsigned Opcode) const {
172*9880d681SAndroid Build Coastguard Worker switch (static_cast<WebAssemblyISD::NodeType>(Opcode)) {
173*9880d681SAndroid Build Coastguard Worker case WebAssemblyISD::FIRST_NUMBER:
174*9880d681SAndroid Build Coastguard Worker break;
175*9880d681SAndroid Build Coastguard Worker #define HANDLE_NODETYPE(NODE) \
176*9880d681SAndroid Build Coastguard Worker case WebAssemblyISD::NODE: \
177*9880d681SAndroid Build Coastguard Worker return "WebAssemblyISD::" #NODE;
178*9880d681SAndroid Build Coastguard Worker #include "WebAssemblyISD.def"
179*9880d681SAndroid Build Coastguard Worker #undef HANDLE_NODETYPE
180*9880d681SAndroid Build Coastguard Worker }
181*9880d681SAndroid Build Coastguard Worker return nullptr;
182*9880d681SAndroid Build Coastguard Worker }
183*9880d681SAndroid Build Coastguard Worker
184*9880d681SAndroid Build Coastguard Worker std::pair<unsigned, const TargetRegisterClass *>
getRegForInlineAsmConstraint(const TargetRegisterInfo * TRI,StringRef Constraint,MVT VT) const185*9880d681SAndroid Build Coastguard Worker WebAssemblyTargetLowering::getRegForInlineAsmConstraint(
186*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
187*9880d681SAndroid Build Coastguard Worker // First, see if this is a constraint that directly corresponds to a
188*9880d681SAndroid Build Coastguard Worker // WebAssembly register class.
189*9880d681SAndroid Build Coastguard Worker if (Constraint.size() == 1) {
190*9880d681SAndroid Build Coastguard Worker switch (Constraint[0]) {
191*9880d681SAndroid Build Coastguard Worker case 'r':
192*9880d681SAndroid Build Coastguard Worker assert(VT != MVT::iPTR && "Pointer MVT not expected here");
193*9880d681SAndroid Build Coastguard Worker if (VT.isInteger() && !VT.isVector()) {
194*9880d681SAndroid Build Coastguard Worker if (VT.getSizeInBits() <= 32)
195*9880d681SAndroid Build Coastguard Worker return std::make_pair(0U, &WebAssembly::I32RegClass);
196*9880d681SAndroid Build Coastguard Worker if (VT.getSizeInBits() <= 64)
197*9880d681SAndroid Build Coastguard Worker return std::make_pair(0U, &WebAssembly::I64RegClass);
198*9880d681SAndroid Build Coastguard Worker }
199*9880d681SAndroid Build Coastguard Worker break;
200*9880d681SAndroid Build Coastguard Worker default:
201*9880d681SAndroid Build Coastguard Worker break;
202*9880d681SAndroid Build Coastguard Worker }
203*9880d681SAndroid Build Coastguard Worker }
204*9880d681SAndroid Build Coastguard Worker
205*9880d681SAndroid Build Coastguard Worker return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
206*9880d681SAndroid Build Coastguard Worker }
207*9880d681SAndroid Build Coastguard Worker
isCheapToSpeculateCttz() const208*9880d681SAndroid Build Coastguard Worker bool WebAssemblyTargetLowering::isCheapToSpeculateCttz() const {
209*9880d681SAndroid Build Coastguard Worker // Assume ctz is a relatively cheap operation.
210*9880d681SAndroid Build Coastguard Worker return true;
211*9880d681SAndroid Build Coastguard Worker }
212*9880d681SAndroid Build Coastguard Worker
isCheapToSpeculateCtlz() const213*9880d681SAndroid Build Coastguard Worker bool WebAssemblyTargetLowering::isCheapToSpeculateCtlz() const {
214*9880d681SAndroid Build Coastguard Worker // Assume clz is a relatively cheap operation.
215*9880d681SAndroid Build Coastguard Worker return true;
216*9880d681SAndroid Build Coastguard Worker }
217*9880d681SAndroid Build Coastguard Worker
isLegalAddressingMode(const DataLayout & DL,const AddrMode & AM,Type * Ty,unsigned AS) const218*9880d681SAndroid Build Coastguard Worker bool WebAssemblyTargetLowering::isLegalAddressingMode(const DataLayout &DL,
219*9880d681SAndroid Build Coastguard Worker const AddrMode &AM,
220*9880d681SAndroid Build Coastguard Worker Type *Ty,
221*9880d681SAndroid Build Coastguard Worker unsigned AS) const {
222*9880d681SAndroid Build Coastguard Worker // WebAssembly offsets are added as unsigned without wrapping. The
223*9880d681SAndroid Build Coastguard Worker // isLegalAddressingMode gives us no way to determine if wrapping could be
224*9880d681SAndroid Build Coastguard Worker // happening, so we approximate this by accepting only non-negative offsets.
225*9880d681SAndroid Build Coastguard Worker if (AM.BaseOffs < 0) return false;
226*9880d681SAndroid Build Coastguard Worker
227*9880d681SAndroid Build Coastguard Worker // WebAssembly has no scale register operands.
228*9880d681SAndroid Build Coastguard Worker if (AM.Scale != 0) return false;
229*9880d681SAndroid Build Coastguard Worker
230*9880d681SAndroid Build Coastguard Worker // Everything else is legal.
231*9880d681SAndroid Build Coastguard Worker return true;
232*9880d681SAndroid Build Coastguard Worker }
233*9880d681SAndroid Build Coastguard Worker
allowsMisalignedMemoryAccesses(EVT,unsigned,unsigned,bool * Fast) const234*9880d681SAndroid Build Coastguard Worker bool WebAssemblyTargetLowering::allowsMisalignedMemoryAccesses(
235*9880d681SAndroid Build Coastguard Worker EVT /*VT*/, unsigned /*AddrSpace*/, unsigned /*Align*/, bool *Fast) const {
236*9880d681SAndroid Build Coastguard Worker // WebAssembly supports unaligned accesses, though it should be declared
237*9880d681SAndroid Build Coastguard Worker // with the p2align attribute on loads and stores which do so, and there
238*9880d681SAndroid Build Coastguard Worker // may be a performance impact. We tell LLVM they're "fast" because
239*9880d681SAndroid Build Coastguard Worker // for the kinds of things that LLVM uses this for (merging adjacent stores
240*9880d681SAndroid Build Coastguard Worker // of constants, etc.), WebAssembly implementations will either want the
241*9880d681SAndroid Build Coastguard Worker // unaligned access or they'll split anyway.
242*9880d681SAndroid Build Coastguard Worker if (Fast) *Fast = true;
243*9880d681SAndroid Build Coastguard Worker return true;
244*9880d681SAndroid Build Coastguard Worker }
245*9880d681SAndroid Build Coastguard Worker
isIntDivCheap(EVT VT,AttributeSet Attr) const246*9880d681SAndroid Build Coastguard Worker bool WebAssemblyTargetLowering::isIntDivCheap(EVT VT, AttributeSet Attr) const {
247*9880d681SAndroid Build Coastguard Worker // The current thinking is that wasm engines will perform this optimization,
248*9880d681SAndroid Build Coastguard Worker // so we can save on code size.
249*9880d681SAndroid Build Coastguard Worker return true;
250*9880d681SAndroid Build Coastguard Worker }
251*9880d681SAndroid Build Coastguard Worker
252*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
253*9880d681SAndroid Build Coastguard Worker // WebAssembly Lowering private implementation.
254*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
255*9880d681SAndroid Build Coastguard Worker
256*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
257*9880d681SAndroid Build Coastguard Worker // Lowering Code
258*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
259*9880d681SAndroid Build Coastguard Worker
fail(const SDLoc & DL,SelectionDAG & DAG,const char * msg)260*9880d681SAndroid Build Coastguard Worker static void fail(const SDLoc &DL, SelectionDAG &DAG, const char *msg) {
261*9880d681SAndroid Build Coastguard Worker MachineFunction &MF = DAG.getMachineFunction();
262*9880d681SAndroid Build Coastguard Worker DAG.getContext()->diagnose(
263*9880d681SAndroid Build Coastguard Worker DiagnosticInfoUnsupported(*MF.getFunction(), msg, DL.getDebugLoc()));
264*9880d681SAndroid Build Coastguard Worker }
265*9880d681SAndroid Build Coastguard Worker
266*9880d681SAndroid Build Coastguard Worker // Test whether the given calling convention is supported.
CallingConvSupported(CallingConv::ID CallConv)267*9880d681SAndroid Build Coastguard Worker static bool CallingConvSupported(CallingConv::ID CallConv) {
268*9880d681SAndroid Build Coastguard Worker // We currently support the language-independent target-independent
269*9880d681SAndroid Build Coastguard Worker // conventions. We don't yet have a way to annotate calls with properties like
270*9880d681SAndroid Build Coastguard Worker // "cold", and we don't have any call-clobbered registers, so these are mostly
271*9880d681SAndroid Build Coastguard Worker // all handled the same.
272*9880d681SAndroid Build Coastguard Worker return CallConv == CallingConv::C || CallConv == CallingConv::Fast ||
273*9880d681SAndroid Build Coastguard Worker CallConv == CallingConv::Cold ||
274*9880d681SAndroid Build Coastguard Worker CallConv == CallingConv::PreserveMost ||
275*9880d681SAndroid Build Coastguard Worker CallConv == CallingConv::PreserveAll ||
276*9880d681SAndroid Build Coastguard Worker CallConv == CallingConv::CXX_FAST_TLS;
277*9880d681SAndroid Build Coastguard Worker }
278*9880d681SAndroid Build Coastguard Worker
LowerCall(CallLoweringInfo & CLI,SmallVectorImpl<SDValue> & InVals) const279*9880d681SAndroid Build Coastguard Worker SDValue WebAssemblyTargetLowering::LowerCall(
280*9880d681SAndroid Build Coastguard Worker CallLoweringInfo &CLI, SmallVectorImpl<SDValue> &InVals) const {
281*9880d681SAndroid Build Coastguard Worker SelectionDAG &DAG = CLI.DAG;
282*9880d681SAndroid Build Coastguard Worker SDLoc DL = CLI.DL;
283*9880d681SAndroid Build Coastguard Worker SDValue Chain = CLI.Chain;
284*9880d681SAndroid Build Coastguard Worker SDValue Callee = CLI.Callee;
285*9880d681SAndroid Build Coastguard Worker MachineFunction &MF = DAG.getMachineFunction();
286*9880d681SAndroid Build Coastguard Worker auto Layout = MF.getDataLayout();
287*9880d681SAndroid Build Coastguard Worker
288*9880d681SAndroid Build Coastguard Worker CallingConv::ID CallConv = CLI.CallConv;
289*9880d681SAndroid Build Coastguard Worker if (!CallingConvSupported(CallConv))
290*9880d681SAndroid Build Coastguard Worker fail(DL, DAG,
291*9880d681SAndroid Build Coastguard Worker "WebAssembly doesn't support language-specific or target-specific "
292*9880d681SAndroid Build Coastguard Worker "calling conventions yet");
293*9880d681SAndroid Build Coastguard Worker if (CLI.IsPatchPoint)
294*9880d681SAndroid Build Coastguard Worker fail(DL, DAG, "WebAssembly doesn't support patch point yet");
295*9880d681SAndroid Build Coastguard Worker
296*9880d681SAndroid Build Coastguard Worker // WebAssembly doesn't currently support explicit tail calls. If they are
297*9880d681SAndroid Build Coastguard Worker // required, fail. Otherwise, just disable them.
298*9880d681SAndroid Build Coastguard Worker if ((CallConv == CallingConv::Fast && CLI.IsTailCall &&
299*9880d681SAndroid Build Coastguard Worker MF.getTarget().Options.GuaranteedTailCallOpt) ||
300*9880d681SAndroid Build Coastguard Worker (CLI.CS && CLI.CS->isMustTailCall()))
301*9880d681SAndroid Build Coastguard Worker fail(DL, DAG, "WebAssembly doesn't support tail call yet");
302*9880d681SAndroid Build Coastguard Worker CLI.IsTailCall = false;
303*9880d681SAndroid Build Coastguard Worker
304*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
305*9880d681SAndroid Build Coastguard Worker if (Ins.size() > 1)
306*9880d681SAndroid Build Coastguard Worker fail(DL, DAG, "WebAssembly doesn't support more than 1 returned value yet");
307*9880d681SAndroid Build Coastguard Worker
308*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
309*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
310*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i < Outs.size(); ++i) {
311*9880d681SAndroid Build Coastguard Worker const ISD::OutputArg &Out = Outs[i];
312*9880d681SAndroid Build Coastguard Worker SDValue &OutVal = OutVals[i];
313*9880d681SAndroid Build Coastguard Worker if (Out.Flags.isNest())
314*9880d681SAndroid Build Coastguard Worker fail(DL, DAG, "WebAssembly hasn't implemented nest arguments");
315*9880d681SAndroid Build Coastguard Worker if (Out.Flags.isInAlloca())
316*9880d681SAndroid Build Coastguard Worker fail(DL, DAG, "WebAssembly hasn't implemented inalloca arguments");
317*9880d681SAndroid Build Coastguard Worker if (Out.Flags.isInConsecutiveRegs())
318*9880d681SAndroid Build Coastguard Worker fail(DL, DAG, "WebAssembly hasn't implemented cons regs arguments");
319*9880d681SAndroid Build Coastguard Worker if (Out.Flags.isInConsecutiveRegsLast())
320*9880d681SAndroid Build Coastguard Worker fail(DL, DAG, "WebAssembly hasn't implemented cons regs last arguments");
321*9880d681SAndroid Build Coastguard Worker if (Out.Flags.isByVal() && Out.Flags.getByValSize() != 0) {
322*9880d681SAndroid Build Coastguard Worker auto *MFI = MF.getFrameInfo();
323*9880d681SAndroid Build Coastguard Worker int FI = MFI->CreateStackObject(Out.Flags.getByValSize(),
324*9880d681SAndroid Build Coastguard Worker Out.Flags.getByValAlign(),
325*9880d681SAndroid Build Coastguard Worker /*isSS=*/false);
326*9880d681SAndroid Build Coastguard Worker SDValue SizeNode =
327*9880d681SAndroid Build Coastguard Worker DAG.getConstant(Out.Flags.getByValSize(), DL, MVT::i32);
328*9880d681SAndroid Build Coastguard Worker SDValue FINode = DAG.getFrameIndex(FI, getPointerTy(Layout));
329*9880d681SAndroid Build Coastguard Worker Chain = DAG.getMemcpy(
330*9880d681SAndroid Build Coastguard Worker Chain, DL, FINode, OutVal, SizeNode, Out.Flags.getByValAlign(),
331*9880d681SAndroid Build Coastguard Worker /*isVolatile*/ false, /*AlwaysInline=*/false,
332*9880d681SAndroid Build Coastguard Worker /*isTailCall*/ false, MachinePointerInfo(), MachinePointerInfo());
333*9880d681SAndroid Build Coastguard Worker OutVal = FINode;
334*9880d681SAndroid Build Coastguard Worker }
335*9880d681SAndroid Build Coastguard Worker }
336*9880d681SAndroid Build Coastguard Worker
337*9880d681SAndroid Build Coastguard Worker bool IsVarArg = CLI.IsVarArg;
338*9880d681SAndroid Build Coastguard Worker unsigned NumFixedArgs = CLI.NumFixedArgs;
339*9880d681SAndroid Build Coastguard Worker
340*9880d681SAndroid Build Coastguard Worker auto PtrVT = getPointerTy(Layout);
341*9880d681SAndroid Build Coastguard Worker
342*9880d681SAndroid Build Coastguard Worker // Analyze operands of the call, assigning locations to each operand.
343*9880d681SAndroid Build Coastguard Worker SmallVector<CCValAssign, 16> ArgLocs;
344*9880d681SAndroid Build Coastguard Worker CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
345*9880d681SAndroid Build Coastguard Worker
346*9880d681SAndroid Build Coastguard Worker if (IsVarArg) {
347*9880d681SAndroid Build Coastguard Worker // Outgoing non-fixed arguments are placed in a buffer. First
348*9880d681SAndroid Build Coastguard Worker // compute their offsets and the total amount of buffer space needed.
349*9880d681SAndroid Build Coastguard Worker for (SDValue Arg :
350*9880d681SAndroid Build Coastguard Worker make_range(OutVals.begin() + NumFixedArgs, OutVals.end())) {
351*9880d681SAndroid Build Coastguard Worker EVT VT = Arg.getValueType();
352*9880d681SAndroid Build Coastguard Worker assert(VT != MVT::iPTR && "Legalized args should be concrete");
353*9880d681SAndroid Build Coastguard Worker Type *Ty = VT.getTypeForEVT(*DAG.getContext());
354*9880d681SAndroid Build Coastguard Worker unsigned Offset = CCInfo.AllocateStack(Layout.getTypeAllocSize(Ty),
355*9880d681SAndroid Build Coastguard Worker Layout.getABITypeAlignment(Ty));
356*9880d681SAndroid Build Coastguard Worker CCInfo.addLoc(CCValAssign::getMem(ArgLocs.size(), VT.getSimpleVT(),
357*9880d681SAndroid Build Coastguard Worker Offset, VT.getSimpleVT(),
358*9880d681SAndroid Build Coastguard Worker CCValAssign::Full));
359*9880d681SAndroid Build Coastguard Worker }
360*9880d681SAndroid Build Coastguard Worker }
361*9880d681SAndroid Build Coastguard Worker
362*9880d681SAndroid Build Coastguard Worker unsigned NumBytes = CCInfo.getAlignedCallFrameSize();
363*9880d681SAndroid Build Coastguard Worker
364*9880d681SAndroid Build Coastguard Worker SDValue FINode;
365*9880d681SAndroid Build Coastguard Worker if (IsVarArg && NumBytes) {
366*9880d681SAndroid Build Coastguard Worker // For non-fixed arguments, next emit stores to store the argument values
367*9880d681SAndroid Build Coastguard Worker // to the stack buffer at the offsets computed above.
368*9880d681SAndroid Build Coastguard Worker int FI = MF.getFrameInfo()->CreateStackObject(NumBytes,
369*9880d681SAndroid Build Coastguard Worker Layout.getStackAlignment(),
370*9880d681SAndroid Build Coastguard Worker /*isSS=*/false);
371*9880d681SAndroid Build Coastguard Worker unsigned ValNo = 0;
372*9880d681SAndroid Build Coastguard Worker SmallVector<SDValue, 8> Chains;
373*9880d681SAndroid Build Coastguard Worker for (SDValue Arg :
374*9880d681SAndroid Build Coastguard Worker make_range(OutVals.begin() + NumFixedArgs, OutVals.end())) {
375*9880d681SAndroid Build Coastguard Worker assert(ArgLocs[ValNo].getValNo() == ValNo &&
376*9880d681SAndroid Build Coastguard Worker "ArgLocs should remain in order and only hold varargs args");
377*9880d681SAndroid Build Coastguard Worker unsigned Offset = ArgLocs[ValNo++].getLocMemOffset();
378*9880d681SAndroid Build Coastguard Worker FINode = DAG.getFrameIndex(FI, getPointerTy(Layout));
379*9880d681SAndroid Build Coastguard Worker SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, FINode,
380*9880d681SAndroid Build Coastguard Worker DAG.getConstant(Offset, DL, PtrVT));
381*9880d681SAndroid Build Coastguard Worker Chains.push_back(DAG.getStore(
382*9880d681SAndroid Build Coastguard Worker Chain, DL, Arg, Add,
383*9880d681SAndroid Build Coastguard Worker MachinePointerInfo::getFixedStack(MF, FI, Offset), false, false, 0));
384*9880d681SAndroid Build Coastguard Worker }
385*9880d681SAndroid Build Coastguard Worker if (!Chains.empty())
386*9880d681SAndroid Build Coastguard Worker Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
387*9880d681SAndroid Build Coastguard Worker } else if (IsVarArg) {
388*9880d681SAndroid Build Coastguard Worker FINode = DAG.getIntPtrConstant(0, DL);
389*9880d681SAndroid Build Coastguard Worker }
390*9880d681SAndroid Build Coastguard Worker
391*9880d681SAndroid Build Coastguard Worker // Compute the operands for the CALLn node.
392*9880d681SAndroid Build Coastguard Worker SmallVector<SDValue, 16> Ops;
393*9880d681SAndroid Build Coastguard Worker Ops.push_back(Chain);
394*9880d681SAndroid Build Coastguard Worker Ops.push_back(Callee);
395*9880d681SAndroid Build Coastguard Worker
396*9880d681SAndroid Build Coastguard Worker // Add all fixed arguments. Note that for non-varargs calls, NumFixedArgs
397*9880d681SAndroid Build Coastguard Worker // isn't reliable.
398*9880d681SAndroid Build Coastguard Worker Ops.append(OutVals.begin(),
399*9880d681SAndroid Build Coastguard Worker IsVarArg ? OutVals.begin() + NumFixedArgs : OutVals.end());
400*9880d681SAndroid Build Coastguard Worker // Add a pointer to the vararg buffer.
401*9880d681SAndroid Build Coastguard Worker if (IsVarArg) Ops.push_back(FINode);
402*9880d681SAndroid Build Coastguard Worker
403*9880d681SAndroid Build Coastguard Worker SmallVector<EVT, 8> InTys;
404*9880d681SAndroid Build Coastguard Worker for (const auto &In : Ins) {
405*9880d681SAndroid Build Coastguard Worker assert(!In.Flags.isByVal() && "byval is not valid for return values");
406*9880d681SAndroid Build Coastguard Worker assert(!In.Flags.isNest() && "nest is not valid for return values");
407*9880d681SAndroid Build Coastguard Worker if (In.Flags.isInAlloca())
408*9880d681SAndroid Build Coastguard Worker fail(DL, DAG, "WebAssembly hasn't implemented inalloca return values");
409*9880d681SAndroid Build Coastguard Worker if (In.Flags.isInConsecutiveRegs())
410*9880d681SAndroid Build Coastguard Worker fail(DL, DAG, "WebAssembly hasn't implemented cons regs return values");
411*9880d681SAndroid Build Coastguard Worker if (In.Flags.isInConsecutiveRegsLast())
412*9880d681SAndroid Build Coastguard Worker fail(DL, DAG,
413*9880d681SAndroid Build Coastguard Worker "WebAssembly hasn't implemented cons regs last return values");
414*9880d681SAndroid Build Coastguard Worker // Ignore In.getOrigAlign() because all our arguments are passed in
415*9880d681SAndroid Build Coastguard Worker // registers.
416*9880d681SAndroid Build Coastguard Worker InTys.push_back(In.VT);
417*9880d681SAndroid Build Coastguard Worker }
418*9880d681SAndroid Build Coastguard Worker InTys.push_back(MVT::Other);
419*9880d681SAndroid Build Coastguard Worker SDVTList InTyList = DAG.getVTList(InTys);
420*9880d681SAndroid Build Coastguard Worker SDValue Res =
421*9880d681SAndroid Build Coastguard Worker DAG.getNode(Ins.empty() ? WebAssemblyISD::CALL0 : WebAssemblyISD::CALL1,
422*9880d681SAndroid Build Coastguard Worker DL, InTyList, Ops);
423*9880d681SAndroid Build Coastguard Worker if (Ins.empty()) {
424*9880d681SAndroid Build Coastguard Worker Chain = Res;
425*9880d681SAndroid Build Coastguard Worker } else {
426*9880d681SAndroid Build Coastguard Worker InVals.push_back(Res);
427*9880d681SAndroid Build Coastguard Worker Chain = Res.getValue(1);
428*9880d681SAndroid Build Coastguard Worker }
429*9880d681SAndroid Build Coastguard Worker
430*9880d681SAndroid Build Coastguard Worker return Chain;
431*9880d681SAndroid Build Coastguard Worker }
432*9880d681SAndroid Build Coastguard Worker
CanLowerReturn(CallingConv::ID,MachineFunction &,bool,const SmallVectorImpl<ISD::OutputArg> & Outs,LLVMContext &) const433*9880d681SAndroid Build Coastguard Worker bool WebAssemblyTargetLowering::CanLowerReturn(
434*9880d681SAndroid Build Coastguard Worker CallingConv::ID /*CallConv*/, MachineFunction & /*MF*/, bool /*IsVarArg*/,
435*9880d681SAndroid Build Coastguard Worker const SmallVectorImpl<ISD::OutputArg> &Outs,
436*9880d681SAndroid Build Coastguard Worker LLVMContext & /*Context*/) const {
437*9880d681SAndroid Build Coastguard Worker // WebAssembly can't currently handle returning tuples.
438*9880d681SAndroid Build Coastguard Worker return Outs.size() <= 1;
439*9880d681SAndroid Build Coastguard Worker }
440*9880d681SAndroid Build Coastguard Worker
LowerReturn(SDValue Chain,CallingConv::ID CallConv,bool,const SmallVectorImpl<ISD::OutputArg> & Outs,const SmallVectorImpl<SDValue> & OutVals,const SDLoc & DL,SelectionDAG & DAG) const441*9880d681SAndroid Build Coastguard Worker SDValue WebAssemblyTargetLowering::LowerReturn(
442*9880d681SAndroid Build Coastguard Worker SDValue Chain, CallingConv::ID CallConv, bool /*IsVarArg*/,
443*9880d681SAndroid Build Coastguard Worker const SmallVectorImpl<ISD::OutputArg> &Outs,
444*9880d681SAndroid Build Coastguard Worker const SmallVectorImpl<SDValue> &OutVals, const SDLoc &DL,
445*9880d681SAndroid Build Coastguard Worker SelectionDAG &DAG) const {
446*9880d681SAndroid Build Coastguard Worker assert(Outs.size() <= 1 && "WebAssembly can only return up to one value");
447*9880d681SAndroid Build Coastguard Worker if (!CallingConvSupported(CallConv))
448*9880d681SAndroid Build Coastguard Worker fail(DL, DAG, "WebAssembly doesn't support non-C calling conventions");
449*9880d681SAndroid Build Coastguard Worker
450*9880d681SAndroid Build Coastguard Worker SmallVector<SDValue, 4> RetOps(1, Chain);
451*9880d681SAndroid Build Coastguard Worker RetOps.append(OutVals.begin(), OutVals.end());
452*9880d681SAndroid Build Coastguard Worker Chain = DAG.getNode(WebAssemblyISD::RETURN, DL, MVT::Other, RetOps);
453*9880d681SAndroid Build Coastguard Worker
454*9880d681SAndroid Build Coastguard Worker // Record the number and types of the return values.
455*9880d681SAndroid Build Coastguard Worker for (const ISD::OutputArg &Out : Outs) {
456*9880d681SAndroid Build Coastguard Worker assert(!Out.Flags.isByVal() && "byval is not valid for return values");
457*9880d681SAndroid Build Coastguard Worker assert(!Out.Flags.isNest() && "nest is not valid for return values");
458*9880d681SAndroid Build Coastguard Worker assert(Out.IsFixed && "non-fixed return value is not valid");
459*9880d681SAndroid Build Coastguard Worker if (Out.Flags.isInAlloca())
460*9880d681SAndroid Build Coastguard Worker fail(DL, DAG, "WebAssembly hasn't implemented inalloca results");
461*9880d681SAndroid Build Coastguard Worker if (Out.Flags.isInConsecutiveRegs())
462*9880d681SAndroid Build Coastguard Worker fail(DL, DAG, "WebAssembly hasn't implemented cons regs results");
463*9880d681SAndroid Build Coastguard Worker if (Out.Flags.isInConsecutiveRegsLast())
464*9880d681SAndroid Build Coastguard Worker fail(DL, DAG, "WebAssembly hasn't implemented cons regs last results");
465*9880d681SAndroid Build Coastguard Worker }
466*9880d681SAndroid Build Coastguard Worker
467*9880d681SAndroid Build Coastguard Worker return Chain;
468*9880d681SAndroid Build Coastguard Worker }
469*9880d681SAndroid Build Coastguard Worker
LowerFormalArguments(SDValue Chain,CallingConv::ID CallConv,bool IsVarArg,const SmallVectorImpl<ISD::InputArg> & Ins,const SDLoc & DL,SelectionDAG & DAG,SmallVectorImpl<SDValue> & InVals) const470*9880d681SAndroid Build Coastguard Worker SDValue WebAssemblyTargetLowering::LowerFormalArguments(
471*9880d681SAndroid Build Coastguard Worker SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
472*9880d681SAndroid Build Coastguard Worker const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
473*9880d681SAndroid Build Coastguard Worker SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
474*9880d681SAndroid Build Coastguard Worker MachineFunction &MF = DAG.getMachineFunction();
475*9880d681SAndroid Build Coastguard Worker auto *MFI = MF.getInfo<WebAssemblyFunctionInfo>();
476*9880d681SAndroid Build Coastguard Worker
477*9880d681SAndroid Build Coastguard Worker if (!CallingConvSupported(CallConv))
478*9880d681SAndroid Build Coastguard Worker fail(DL, DAG, "WebAssembly doesn't support non-C calling conventions");
479*9880d681SAndroid Build Coastguard Worker
480*9880d681SAndroid Build Coastguard Worker // Set up the incoming ARGUMENTS value, which serves to represent the liveness
481*9880d681SAndroid Build Coastguard Worker // of the incoming values before they're represented by virtual registers.
482*9880d681SAndroid Build Coastguard Worker MF.getRegInfo().addLiveIn(WebAssembly::ARGUMENTS);
483*9880d681SAndroid Build Coastguard Worker
484*9880d681SAndroid Build Coastguard Worker for (const ISD::InputArg &In : Ins) {
485*9880d681SAndroid Build Coastguard Worker if (In.Flags.isInAlloca())
486*9880d681SAndroid Build Coastguard Worker fail(DL, DAG, "WebAssembly hasn't implemented inalloca arguments");
487*9880d681SAndroid Build Coastguard Worker if (In.Flags.isNest())
488*9880d681SAndroid Build Coastguard Worker fail(DL, DAG, "WebAssembly hasn't implemented nest arguments");
489*9880d681SAndroid Build Coastguard Worker if (In.Flags.isInConsecutiveRegs())
490*9880d681SAndroid Build Coastguard Worker fail(DL, DAG, "WebAssembly hasn't implemented cons regs arguments");
491*9880d681SAndroid Build Coastguard Worker if (In.Flags.isInConsecutiveRegsLast())
492*9880d681SAndroid Build Coastguard Worker fail(DL, DAG, "WebAssembly hasn't implemented cons regs last arguments");
493*9880d681SAndroid Build Coastguard Worker // Ignore In.getOrigAlign() because all our arguments are passed in
494*9880d681SAndroid Build Coastguard Worker // registers.
495*9880d681SAndroid Build Coastguard Worker InVals.push_back(
496*9880d681SAndroid Build Coastguard Worker In.Used
497*9880d681SAndroid Build Coastguard Worker ? DAG.getNode(WebAssemblyISD::ARGUMENT, DL, In.VT,
498*9880d681SAndroid Build Coastguard Worker DAG.getTargetConstant(InVals.size(), DL, MVT::i32))
499*9880d681SAndroid Build Coastguard Worker : DAG.getUNDEF(In.VT));
500*9880d681SAndroid Build Coastguard Worker
501*9880d681SAndroid Build Coastguard Worker // Record the number and types of arguments.
502*9880d681SAndroid Build Coastguard Worker MFI->addParam(In.VT);
503*9880d681SAndroid Build Coastguard Worker }
504*9880d681SAndroid Build Coastguard Worker
505*9880d681SAndroid Build Coastguard Worker // Varargs are copied into a buffer allocated by the caller, and a pointer to
506*9880d681SAndroid Build Coastguard Worker // the buffer is passed as an argument.
507*9880d681SAndroid Build Coastguard Worker if (IsVarArg) {
508*9880d681SAndroid Build Coastguard Worker MVT PtrVT = getPointerTy(MF.getDataLayout());
509*9880d681SAndroid Build Coastguard Worker unsigned VarargVreg =
510*9880d681SAndroid Build Coastguard Worker MF.getRegInfo().createVirtualRegister(getRegClassFor(PtrVT));
511*9880d681SAndroid Build Coastguard Worker MFI->setVarargBufferVreg(VarargVreg);
512*9880d681SAndroid Build Coastguard Worker Chain = DAG.getCopyToReg(
513*9880d681SAndroid Build Coastguard Worker Chain, DL, VarargVreg,
514*9880d681SAndroid Build Coastguard Worker DAG.getNode(WebAssemblyISD::ARGUMENT, DL, PtrVT,
515*9880d681SAndroid Build Coastguard Worker DAG.getTargetConstant(Ins.size(), DL, MVT::i32)));
516*9880d681SAndroid Build Coastguard Worker MFI->addParam(PtrVT);
517*9880d681SAndroid Build Coastguard Worker }
518*9880d681SAndroid Build Coastguard Worker
519*9880d681SAndroid Build Coastguard Worker return Chain;
520*9880d681SAndroid Build Coastguard Worker }
521*9880d681SAndroid Build Coastguard Worker
522*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
523*9880d681SAndroid Build Coastguard Worker // Custom lowering hooks.
524*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
525*9880d681SAndroid Build Coastguard Worker
LowerOperation(SDValue Op,SelectionDAG & DAG) const526*9880d681SAndroid Build Coastguard Worker SDValue WebAssemblyTargetLowering::LowerOperation(SDValue Op,
527*9880d681SAndroid Build Coastguard Worker SelectionDAG &DAG) const {
528*9880d681SAndroid Build Coastguard Worker SDLoc DL(Op);
529*9880d681SAndroid Build Coastguard Worker switch (Op.getOpcode()) {
530*9880d681SAndroid Build Coastguard Worker default:
531*9880d681SAndroid Build Coastguard Worker llvm_unreachable("unimplemented operation lowering");
532*9880d681SAndroid Build Coastguard Worker return SDValue();
533*9880d681SAndroid Build Coastguard Worker case ISD::FrameIndex:
534*9880d681SAndroid Build Coastguard Worker return LowerFrameIndex(Op, DAG);
535*9880d681SAndroid Build Coastguard Worker case ISD::GlobalAddress:
536*9880d681SAndroid Build Coastguard Worker return LowerGlobalAddress(Op, DAG);
537*9880d681SAndroid Build Coastguard Worker case ISD::ExternalSymbol:
538*9880d681SAndroid Build Coastguard Worker return LowerExternalSymbol(Op, DAG);
539*9880d681SAndroid Build Coastguard Worker case ISD::JumpTable:
540*9880d681SAndroid Build Coastguard Worker return LowerJumpTable(Op, DAG);
541*9880d681SAndroid Build Coastguard Worker case ISD::BR_JT:
542*9880d681SAndroid Build Coastguard Worker return LowerBR_JT(Op, DAG);
543*9880d681SAndroid Build Coastguard Worker case ISD::VASTART:
544*9880d681SAndroid Build Coastguard Worker return LowerVASTART(Op, DAG);
545*9880d681SAndroid Build Coastguard Worker case ISD::BlockAddress:
546*9880d681SAndroid Build Coastguard Worker case ISD::BRIND:
547*9880d681SAndroid Build Coastguard Worker fail(DL, DAG, "WebAssembly hasn't implemented computed gotos");
548*9880d681SAndroid Build Coastguard Worker return SDValue();
549*9880d681SAndroid Build Coastguard Worker case ISD::RETURNADDR: // Probably nothing meaningful can be returned here.
550*9880d681SAndroid Build Coastguard Worker fail(DL, DAG, "WebAssembly hasn't implemented __builtin_return_address");
551*9880d681SAndroid Build Coastguard Worker return SDValue();
552*9880d681SAndroid Build Coastguard Worker case ISD::FRAMEADDR:
553*9880d681SAndroid Build Coastguard Worker return LowerFRAMEADDR(Op, DAG);
554*9880d681SAndroid Build Coastguard Worker case ISD::CopyToReg:
555*9880d681SAndroid Build Coastguard Worker return LowerCopyToReg(Op, DAG);
556*9880d681SAndroid Build Coastguard Worker }
557*9880d681SAndroid Build Coastguard Worker }
558*9880d681SAndroid Build Coastguard Worker
LowerCopyToReg(SDValue Op,SelectionDAG & DAG) const559*9880d681SAndroid Build Coastguard Worker SDValue WebAssemblyTargetLowering::LowerCopyToReg(SDValue Op,
560*9880d681SAndroid Build Coastguard Worker SelectionDAG &DAG) const {
561*9880d681SAndroid Build Coastguard Worker SDValue Src = Op.getOperand(2);
562*9880d681SAndroid Build Coastguard Worker if (isa<FrameIndexSDNode>(Src.getNode())) {
563*9880d681SAndroid Build Coastguard Worker // CopyToReg nodes don't support FrameIndex operands. Other targets select
564*9880d681SAndroid Build Coastguard Worker // the FI to some LEA-like instruction, but since we don't have that, we
565*9880d681SAndroid Build Coastguard Worker // need to insert some kind of instruction that can take an FI operand and
566*9880d681SAndroid Build Coastguard Worker // produces a value usable by CopyToReg (i.e. in a vreg). So insert a dummy
567*9880d681SAndroid Build Coastguard Worker // copy_local between Op and its FI operand.
568*9880d681SAndroid Build Coastguard Worker SDValue Chain = Op.getOperand(0);
569*9880d681SAndroid Build Coastguard Worker SDLoc DL(Op);
570*9880d681SAndroid Build Coastguard Worker unsigned Reg = cast<RegisterSDNode>(Op.getOperand(1))->getReg();
571*9880d681SAndroid Build Coastguard Worker EVT VT = Src.getValueType();
572*9880d681SAndroid Build Coastguard Worker SDValue Copy(
573*9880d681SAndroid Build Coastguard Worker DAG.getMachineNode(VT == MVT::i32 ? WebAssembly::COPY_LOCAL_I32
574*9880d681SAndroid Build Coastguard Worker : WebAssembly::COPY_LOCAL_I64,
575*9880d681SAndroid Build Coastguard Worker DL, VT, Src),
576*9880d681SAndroid Build Coastguard Worker 0);
577*9880d681SAndroid Build Coastguard Worker return Op.getNode()->getNumValues() == 1
578*9880d681SAndroid Build Coastguard Worker ? DAG.getCopyToReg(Chain, DL, Reg, Copy)
579*9880d681SAndroid Build Coastguard Worker : DAG.getCopyToReg(Chain, DL, Reg, Copy, Op.getNumOperands() == 4
580*9880d681SAndroid Build Coastguard Worker ? Op.getOperand(3)
581*9880d681SAndroid Build Coastguard Worker : SDValue());
582*9880d681SAndroid Build Coastguard Worker }
583*9880d681SAndroid Build Coastguard Worker return SDValue();
584*9880d681SAndroid Build Coastguard Worker }
585*9880d681SAndroid Build Coastguard Worker
LowerFrameIndex(SDValue Op,SelectionDAG & DAG) const586*9880d681SAndroid Build Coastguard Worker SDValue WebAssemblyTargetLowering::LowerFrameIndex(SDValue Op,
587*9880d681SAndroid Build Coastguard Worker SelectionDAG &DAG) const {
588*9880d681SAndroid Build Coastguard Worker int FI = cast<FrameIndexSDNode>(Op)->getIndex();
589*9880d681SAndroid Build Coastguard Worker return DAG.getTargetFrameIndex(FI, Op.getValueType());
590*9880d681SAndroid Build Coastguard Worker }
591*9880d681SAndroid Build Coastguard Worker
LowerFRAMEADDR(SDValue Op,SelectionDAG & DAG) const592*9880d681SAndroid Build Coastguard Worker SDValue WebAssemblyTargetLowering::LowerFRAMEADDR(SDValue Op,
593*9880d681SAndroid Build Coastguard Worker SelectionDAG &DAG) const {
594*9880d681SAndroid Build Coastguard Worker // Non-zero depths are not supported by WebAssembly currently. Use the
595*9880d681SAndroid Build Coastguard Worker // legalizer's default expansion, which is to return 0 (what this function is
596*9880d681SAndroid Build Coastguard Worker // documented to do).
597*9880d681SAndroid Build Coastguard Worker if (Op.getConstantOperandVal(0) > 0)
598*9880d681SAndroid Build Coastguard Worker return SDValue();
599*9880d681SAndroid Build Coastguard Worker
600*9880d681SAndroid Build Coastguard Worker DAG.getMachineFunction().getFrameInfo()->setFrameAddressIsTaken(true);
601*9880d681SAndroid Build Coastguard Worker EVT VT = Op.getValueType();
602*9880d681SAndroid Build Coastguard Worker unsigned FP =
603*9880d681SAndroid Build Coastguard Worker Subtarget->getRegisterInfo()->getFrameRegister(DAG.getMachineFunction());
604*9880d681SAndroid Build Coastguard Worker return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), FP, VT);
605*9880d681SAndroid Build Coastguard Worker }
606*9880d681SAndroid Build Coastguard Worker
LowerGlobalAddress(SDValue Op,SelectionDAG & DAG) const607*9880d681SAndroid Build Coastguard Worker SDValue WebAssemblyTargetLowering::LowerGlobalAddress(SDValue Op,
608*9880d681SAndroid Build Coastguard Worker SelectionDAG &DAG) const {
609*9880d681SAndroid Build Coastguard Worker SDLoc DL(Op);
610*9880d681SAndroid Build Coastguard Worker const auto *GA = cast<GlobalAddressSDNode>(Op);
611*9880d681SAndroid Build Coastguard Worker EVT VT = Op.getValueType();
612*9880d681SAndroid Build Coastguard Worker assert(GA->getTargetFlags() == 0 &&
613*9880d681SAndroid Build Coastguard Worker "Unexpected target flags on generic GlobalAddressSDNode");
614*9880d681SAndroid Build Coastguard Worker if (GA->getAddressSpace() != 0)
615*9880d681SAndroid Build Coastguard Worker fail(DL, DAG, "WebAssembly only expects the 0 address space");
616*9880d681SAndroid Build Coastguard Worker return DAG.getNode(
617*9880d681SAndroid Build Coastguard Worker WebAssemblyISD::Wrapper, DL, VT,
618*9880d681SAndroid Build Coastguard Worker DAG.getTargetGlobalAddress(GA->getGlobal(), DL, VT, GA->getOffset()));
619*9880d681SAndroid Build Coastguard Worker }
620*9880d681SAndroid Build Coastguard Worker
LowerExternalSymbol(SDValue Op,SelectionDAG & DAG) const621*9880d681SAndroid Build Coastguard Worker SDValue WebAssemblyTargetLowering::LowerExternalSymbol(
622*9880d681SAndroid Build Coastguard Worker SDValue Op, SelectionDAG &DAG) const {
623*9880d681SAndroid Build Coastguard Worker SDLoc DL(Op);
624*9880d681SAndroid Build Coastguard Worker const auto *ES = cast<ExternalSymbolSDNode>(Op);
625*9880d681SAndroid Build Coastguard Worker EVT VT = Op.getValueType();
626*9880d681SAndroid Build Coastguard Worker assert(ES->getTargetFlags() == 0 &&
627*9880d681SAndroid Build Coastguard Worker "Unexpected target flags on generic ExternalSymbolSDNode");
628*9880d681SAndroid Build Coastguard Worker // Set the TargetFlags to 0x1 which indicates that this is a "function"
629*9880d681SAndroid Build Coastguard Worker // symbol rather than a data symbol. We do this unconditionally even though
630*9880d681SAndroid Build Coastguard Worker // we don't know anything about the symbol other than its name, because all
631*9880d681SAndroid Build Coastguard Worker // external symbols used in target-independent SelectionDAG code are for
632*9880d681SAndroid Build Coastguard Worker // functions.
633*9880d681SAndroid Build Coastguard Worker return DAG.getNode(WebAssemblyISD::Wrapper, DL, VT,
634*9880d681SAndroid Build Coastguard Worker DAG.getTargetExternalSymbol(ES->getSymbol(), VT,
635*9880d681SAndroid Build Coastguard Worker /*TargetFlags=*/0x1));
636*9880d681SAndroid Build Coastguard Worker }
637*9880d681SAndroid Build Coastguard Worker
LowerJumpTable(SDValue Op,SelectionDAG & DAG) const638*9880d681SAndroid Build Coastguard Worker SDValue WebAssemblyTargetLowering::LowerJumpTable(SDValue Op,
639*9880d681SAndroid Build Coastguard Worker SelectionDAG &DAG) const {
640*9880d681SAndroid Build Coastguard Worker // There's no need for a Wrapper node because we always incorporate a jump
641*9880d681SAndroid Build Coastguard Worker // table operand into a BR_TABLE instruction, rather than ever
642*9880d681SAndroid Build Coastguard Worker // materializing it in a register.
643*9880d681SAndroid Build Coastguard Worker const JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
644*9880d681SAndroid Build Coastguard Worker return DAG.getTargetJumpTable(JT->getIndex(), Op.getValueType(),
645*9880d681SAndroid Build Coastguard Worker JT->getTargetFlags());
646*9880d681SAndroid Build Coastguard Worker }
647*9880d681SAndroid Build Coastguard Worker
LowerBR_JT(SDValue Op,SelectionDAG & DAG) const648*9880d681SAndroid Build Coastguard Worker SDValue WebAssemblyTargetLowering::LowerBR_JT(SDValue Op,
649*9880d681SAndroid Build Coastguard Worker SelectionDAG &DAG) const {
650*9880d681SAndroid Build Coastguard Worker SDLoc DL(Op);
651*9880d681SAndroid Build Coastguard Worker SDValue Chain = Op.getOperand(0);
652*9880d681SAndroid Build Coastguard Worker const auto *JT = cast<JumpTableSDNode>(Op.getOperand(1));
653*9880d681SAndroid Build Coastguard Worker SDValue Index = Op.getOperand(2);
654*9880d681SAndroid Build Coastguard Worker assert(JT->getTargetFlags() == 0 && "WebAssembly doesn't set target flags");
655*9880d681SAndroid Build Coastguard Worker
656*9880d681SAndroid Build Coastguard Worker SmallVector<SDValue, 8> Ops;
657*9880d681SAndroid Build Coastguard Worker Ops.push_back(Chain);
658*9880d681SAndroid Build Coastguard Worker Ops.push_back(Index);
659*9880d681SAndroid Build Coastguard Worker
660*9880d681SAndroid Build Coastguard Worker MachineJumpTableInfo *MJTI = DAG.getMachineFunction().getJumpTableInfo();
661*9880d681SAndroid Build Coastguard Worker const auto &MBBs = MJTI->getJumpTables()[JT->getIndex()].MBBs;
662*9880d681SAndroid Build Coastguard Worker
663*9880d681SAndroid Build Coastguard Worker // Add an operand for each case.
664*9880d681SAndroid Build Coastguard Worker for (auto MBB : MBBs) Ops.push_back(DAG.getBasicBlock(MBB));
665*9880d681SAndroid Build Coastguard Worker
666*9880d681SAndroid Build Coastguard Worker // TODO: For now, we just pick something arbitrary for a default case for now.
667*9880d681SAndroid Build Coastguard Worker // We really want to sniff out the guard and put in the real default case (and
668*9880d681SAndroid Build Coastguard Worker // delete the guard).
669*9880d681SAndroid Build Coastguard Worker Ops.push_back(DAG.getBasicBlock(MBBs[0]));
670*9880d681SAndroid Build Coastguard Worker
671*9880d681SAndroid Build Coastguard Worker return DAG.getNode(WebAssemblyISD::BR_TABLE, DL, MVT::Other, Ops);
672*9880d681SAndroid Build Coastguard Worker }
673*9880d681SAndroid Build Coastguard Worker
LowerVASTART(SDValue Op,SelectionDAG & DAG) const674*9880d681SAndroid Build Coastguard Worker SDValue WebAssemblyTargetLowering::LowerVASTART(SDValue Op,
675*9880d681SAndroid Build Coastguard Worker SelectionDAG &DAG) const {
676*9880d681SAndroid Build Coastguard Worker SDLoc DL(Op);
677*9880d681SAndroid Build Coastguard Worker EVT PtrVT = getPointerTy(DAG.getMachineFunction().getDataLayout());
678*9880d681SAndroid Build Coastguard Worker
679*9880d681SAndroid Build Coastguard Worker auto *MFI = DAG.getMachineFunction().getInfo<WebAssemblyFunctionInfo>();
680*9880d681SAndroid Build Coastguard Worker const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
681*9880d681SAndroid Build Coastguard Worker
682*9880d681SAndroid Build Coastguard Worker SDValue ArgN = DAG.getCopyFromReg(DAG.getEntryNode(), DL,
683*9880d681SAndroid Build Coastguard Worker MFI->getVarargBufferVreg(), PtrVT);
684*9880d681SAndroid Build Coastguard Worker return DAG.getStore(Op.getOperand(0), DL, ArgN, Op.getOperand(1),
685*9880d681SAndroid Build Coastguard Worker MachinePointerInfo(SV), false, false, 0);
686*9880d681SAndroid Build Coastguard Worker }
687*9880d681SAndroid Build Coastguard Worker
688*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
689*9880d681SAndroid Build Coastguard Worker // WebAssembly Optimization Hooks
690*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
691