1 //=- WebAssemblyISelLowering.cpp - WebAssembly DAG Lowering Implementation -==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file implements the WebAssemblyTargetLowering class.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #include "WebAssemblyISelLowering.h"
15 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
16 #include "Utils/WebAssemblyTypeUtilities.h"
17 #include "Utils/WebAssemblyUtilities.h"
18 #include "WebAssemblyMachineFunctionInfo.h"
19 #include "WebAssemblySubtarget.h"
20 #include "WebAssemblyTargetMachine.h"
21 #include "llvm/CodeGen/CallingConvLower.h"
22 #include "llvm/CodeGen/MachineFrameInfo.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/CodeGen/MachineInstrBuilder.h"
25 #include "llvm/CodeGen/MachineJumpTableInfo.h"
26 #include "llvm/CodeGen/MachineModuleInfo.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/CodeGen/SelectionDAG.h"
29 #include "llvm/CodeGen/SelectionDAGNodes.h"
30 #include "llvm/IR/DiagnosticInfo.h"
31 #include "llvm/IR/DiagnosticPrinter.h"
32 #include "llvm/IR/Function.h"
33 #include "llvm/IR/Intrinsics.h"
34 #include "llvm/IR/IntrinsicsWebAssembly.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/KnownBits.h"
38 #include "llvm/Support/MathExtras.h"
39 #include "llvm/Support/raw_ostream.h"
40 #include "llvm/Target/TargetOptions.h"
41 using namespace llvm;
42 
43 #define DEBUG_TYPE "wasm-lower"
44 
45 WebAssemblyTargetLowering::WebAssemblyTargetLowering(
46     const TargetMachine &TM, const WebAssemblySubtarget &STI)
47     : TargetLowering(TM), Subtarget(&STI) {
48   auto MVTPtr = Subtarget->hasAddr64() ? MVT::i64 : MVT::i32;
49 
50   // Booleans always contain 0 or 1.
51   setBooleanContents(ZeroOrOneBooleanContent);
52   // Except in SIMD vectors
53   setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
54   // We don't know the microarchitecture here, so just reduce register pressure.
55   setSchedulingPreference(Sched::RegPressure);
56   // Tell ISel that we have a stack pointer.
57   setStackPointerRegisterToSaveRestore(
58       Subtarget->hasAddr64() ? WebAssembly::SP64 : WebAssembly::SP32);
59   // Set up the register classes.
60   addRegisterClass(MVT::i32, &WebAssembly::I32RegClass);
61   addRegisterClass(MVT::i64, &WebAssembly::I64RegClass);
62   addRegisterClass(MVT::f32, &WebAssembly::F32RegClass);
63   addRegisterClass(MVT::f64, &WebAssembly::F64RegClass);
64   if (Subtarget->hasSIMD128()) {
65     addRegisterClass(MVT::v16i8, &WebAssembly::V128RegClass);
66     addRegisterClass(MVT::v8i16, &WebAssembly::V128RegClass);
67     addRegisterClass(MVT::v4i32, &WebAssembly::V128RegClass);
68     addRegisterClass(MVT::v4f32, &WebAssembly::V128RegClass);
69     addRegisterClass(MVT::v2i64, &WebAssembly::V128RegClass);
70     addRegisterClass(MVT::v2f64, &WebAssembly::V128RegClass);
71   }
72   if (Subtarget->hasReferenceTypes()) {
73     addRegisterClass(MVT::externref, &WebAssembly::EXTERNREFRegClass);
74     addRegisterClass(MVT::funcref, &WebAssembly::FUNCREFRegClass);
75   }
76   // Compute derived properties from the register classes.
77   computeRegisterProperties(Subtarget->getRegisterInfo());
78 
79   // Transform loads and stores to pointers in address space 1 to loads and
80   // stores to WebAssembly global variables, outside linear memory.
81   for (auto T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64}) {
82     setOperationAction(ISD::LOAD, T, Custom);
83     setOperationAction(ISD::STORE, T, Custom);
84   }
85   if (Subtarget->hasSIMD128()) {
86     for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v4f32, MVT::v2i64,
87                    MVT::v2f64}) {
88       setOperationAction(ISD::LOAD, T, Custom);
89       setOperationAction(ISD::STORE, T, Custom);
90     }
91   }
92   if (Subtarget->hasReferenceTypes()) {
93     // We need custom load and store lowering for both externref, funcref and
94     // Other. The MVT::Other here represents tables of reference types.
95     for (auto T : {MVT::externref, MVT::funcref, MVT::Other}) {
96       setOperationAction(ISD::LOAD, T, Custom);
97       setOperationAction(ISD::STORE, T, Custom);
98     }
99   }
100 
101   setOperationAction(ISD::GlobalAddress, MVTPtr, Custom);
102   setOperationAction(ISD::GlobalTLSAddress, MVTPtr, Custom);
103   setOperationAction(ISD::ExternalSymbol, MVTPtr, Custom);
104   setOperationAction(ISD::JumpTable, MVTPtr, Custom);
105   setOperationAction(ISD::BlockAddress, MVTPtr, Custom);
106   setOperationAction(ISD::BRIND, MVT::Other, Custom);
107 
108   // Take the default expansion for va_arg, va_copy, and va_end. There is no
109   // default action for va_start, so we do that custom.
110   setOperationAction(ISD::VASTART, MVT::Other, Custom);
111   setOperationAction(ISD::VAARG, MVT::Other, Expand);
112   setOperationAction(ISD::VACOPY, MVT::Other, Expand);
113   setOperationAction(ISD::VAEND, MVT::Other, Expand);
114 
115   for (auto T : {MVT::f32, MVT::f64, MVT::v4f32, MVT::v2f64}) {
116     // Don't expand the floating-point types to constant pools.
117     setOperationAction(ISD::ConstantFP, T, Legal);
118     // Expand floating-point comparisons.
119     for (auto CC : {ISD::SETO, ISD::SETUO, ISD::SETUEQ, ISD::SETONE,
120                     ISD::SETULT, ISD::SETULE, ISD::SETUGT, ISD::SETUGE})
121       setCondCodeAction(CC, T, Expand);
122     // Expand floating-point library function operators.
123     for (auto Op :
124          {ISD::FSIN, ISD::FCOS, ISD::FSINCOS, ISD::FPOW, ISD::FREM, ISD::FMA})
125       setOperationAction(Op, T, Expand);
126     // Note supported floating-point library function operators that otherwise
127     // default to expand.
128     for (auto Op :
129          {ISD::FCEIL, ISD::FFLOOR, ISD::FTRUNC, ISD::FNEARBYINT, ISD::FRINT})
130       setOperationAction(Op, T, Legal);
131     // Support minimum and maximum, which otherwise default to expand.
132     setOperationAction(ISD::FMINIMUM, T, Legal);
133     setOperationAction(ISD::FMAXIMUM, T, Legal);
134     // WebAssembly currently has no builtin f16 support.
135     setOperationAction(ISD::FP16_TO_FP, T, Expand);
136     setOperationAction(ISD::FP_TO_FP16, T, Expand);
137     setLoadExtAction(ISD::EXTLOAD, T, MVT::f16, Expand);
138     setTruncStoreAction(T, MVT::f16, Expand);
139   }
140 
141   // Expand unavailable integer operations.
142   for (auto Op :
143        {ISD::BSWAP, ISD::SMUL_LOHI, ISD::UMUL_LOHI, ISD::MULHS, ISD::MULHU,
144         ISD::SDIVREM, ISD::UDIVREM, ISD::SHL_PARTS, ISD::SRA_PARTS,
145         ISD::SRL_PARTS, ISD::ADDC, ISD::ADDE, ISD::SUBC, ISD::SUBE}) {
146     for (auto T : {MVT::i32, MVT::i64})
147       setOperationAction(Op, T, Expand);
148     if (Subtarget->hasSIMD128())
149       for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v2i64})
150         setOperationAction(Op, T, Expand);
151   }
152 
153   if (Subtarget->hasNontrappingFPToInt())
154     for (auto Op : {ISD::FP_TO_SINT_SAT, ISD::FP_TO_UINT_SAT})
155       for (auto T : {MVT::i32, MVT::i64})
156         setOperationAction(Op, T, Custom);
157 
158   // SIMD-specific configuration
159   if (Subtarget->hasSIMD128()) {
160     // Hoist bitcasts out of shuffles
161     setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
162 
163     // Combine extends of extract_subvectors into widening ops
164     setTargetDAGCombine({ISD::SIGN_EXTEND, ISD::ZERO_EXTEND});
165 
166     // Combine int_to_fp or fp_extend of extract_vectors and vice versa into
167     // conversions ops
168     setTargetDAGCombine({ISD::SINT_TO_FP, ISD::UINT_TO_FP, ISD::FP_EXTEND,
169                          ISD::EXTRACT_SUBVECTOR});
170 
171     // Combine fp_to_{s,u}int_sat or fp_round of concat_vectors or vice versa
172     // into conversion ops
173     setTargetDAGCombine({ISD::FP_TO_SINT_SAT, ISD::FP_TO_UINT_SAT,
174                          ISD::FP_ROUND, ISD::CONCAT_VECTORS});
175 
176     setTargetDAGCombine(ISD::TRUNCATE);
177 
178     // Support saturating add for i8x16 and i16x8
179     for (auto Op : {ISD::SADDSAT, ISD::UADDSAT})
180       for (auto T : {MVT::v16i8, MVT::v8i16})
181         setOperationAction(Op, T, Legal);
182 
183     // Support integer abs
184     for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v2i64})
185       setOperationAction(ISD::ABS, T, Legal);
186 
187     // Custom lower BUILD_VECTORs to minimize number of replace_lanes
188     for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v4f32, MVT::v2i64,
189                    MVT::v2f64})
190       setOperationAction(ISD::BUILD_VECTOR, T, Custom);
191 
192     // We have custom shuffle lowering to expose the shuffle mask
193     for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v4f32, MVT::v2i64,
194                    MVT::v2f64})
195       setOperationAction(ISD::VECTOR_SHUFFLE, T, Custom);
196 
197     // Custom lowering since wasm shifts must have a scalar shift amount
198     for (auto Op : {ISD::SHL, ISD::SRA, ISD::SRL})
199       for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v2i64})
200         setOperationAction(Op, T, Custom);
201 
202     // Custom lower lane accesses to expand out variable indices
203     for (auto Op : {ISD::EXTRACT_VECTOR_ELT, ISD::INSERT_VECTOR_ELT})
204       for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v4f32, MVT::v2i64,
205                      MVT::v2f64})
206         setOperationAction(Op, T, Custom);
207 
208     // There is no i8x16.mul instruction
209     setOperationAction(ISD::MUL, MVT::v16i8, Expand);
210 
211     // There is no vector conditional select instruction
212     for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v4f32, MVT::v2i64,
213                    MVT::v2f64})
214       setOperationAction(ISD::SELECT_CC, T, Expand);
215 
216     // Expand integer operations supported for scalars but not SIMD
217     for (auto Op :
218          {ISD::SDIV, ISD::UDIV, ISD::SREM, ISD::UREM, ISD::ROTL, ISD::ROTR})
219       for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v2i64})
220         setOperationAction(Op, T, Expand);
221 
222     // But we do have integer min and max operations
223     for (auto Op : {ISD::SMIN, ISD::SMAX, ISD::UMIN, ISD::UMAX})
224       for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32})
225         setOperationAction(Op, T, Legal);
226 
227     // And we have popcnt for i8x16. It can be used to expand ctlz/cttz.
228     setOperationAction(ISD::CTPOP, MVT::v16i8, Legal);
229     setOperationAction(ISD::CTLZ, MVT::v16i8, Expand);
230     setOperationAction(ISD::CTTZ, MVT::v16i8, Expand);
231 
232     // Custom lower bit counting operations for other types to scalarize them.
233     for (auto Op : {ISD::CTLZ, ISD::CTTZ, ISD::CTPOP})
234       for (auto T : {MVT::v8i16, MVT::v4i32, MVT::v2i64})
235         setOperationAction(Op, T, Custom);
236 
237     // Expand float operations supported for scalars but not SIMD
238     for (auto Op : {ISD::FCOPYSIGN, ISD::FLOG, ISD::FLOG2, ISD::FLOG10,
239                     ISD::FEXP, ISD::FEXP2, ISD::FRINT})
240       for (auto T : {MVT::v4f32, MVT::v2f64})
241         setOperationAction(Op, T, Expand);
242 
243     // Unsigned comparison operations are unavailable for i64x2 vectors.
244     for (auto CC : {ISD::SETUGT, ISD::SETUGE, ISD::SETULT, ISD::SETULE})
245       setCondCodeAction(CC, MVT::v2i64, Custom);
246 
247     // 64x2 conversions are not in the spec
248     for (auto Op :
249          {ISD::SINT_TO_FP, ISD::UINT_TO_FP, ISD::FP_TO_SINT, ISD::FP_TO_UINT})
250       for (auto T : {MVT::v2i64, MVT::v2f64})
251         setOperationAction(Op, T, Expand);
252 
253     // But saturating fp_to_int converstions are
254     for (auto Op : {ISD::FP_TO_SINT_SAT, ISD::FP_TO_UINT_SAT})
255       setOperationAction(Op, MVT::v4i32, Custom);
256   }
257 
258   // As a special case, these operators use the type to mean the type to
259   // sign-extend from.
260   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
261   if (!Subtarget->hasSignExt()) {
262     // Sign extends are legal only when extending a vector extract
263     auto Action = Subtarget->hasSIMD128() ? Custom : Expand;
264     for (auto T : {MVT::i8, MVT::i16, MVT::i32})
265       setOperationAction(ISD::SIGN_EXTEND_INREG, T, Action);
266   }
267   for (auto T : MVT::integer_fixedlen_vector_valuetypes())
268     setOperationAction(ISD::SIGN_EXTEND_INREG, T, Expand);
269 
270   // Dynamic stack allocation: use the default expansion.
271   setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
272   setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
273   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVTPtr, Expand);
274 
275   setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
276   setOperationAction(ISD::FrameIndex, MVT::i64, Custom);
277   setOperationAction(ISD::CopyToReg, MVT::Other, Custom);
278 
279   // Expand these forms; we pattern-match the forms that we can handle in isel.
280   for (auto T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64})
281     for (auto Op : {ISD::BR_CC, ISD::SELECT_CC})
282       setOperationAction(Op, T, Expand);
283 
284   // We have custom switch handling.
285   setOperationAction(ISD::BR_JT, MVT::Other, Custom);
286 
287   // WebAssembly doesn't have:
288   //  - Floating-point extending loads.
289   //  - Floating-point truncating stores.
290   //  - i1 extending loads.
291   //  - truncating SIMD stores and most extending loads
292   setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand);
293   setTruncStoreAction(MVT::f64, MVT::f32, Expand);
294   for (auto T : MVT::integer_valuetypes())
295     for (auto Ext : {ISD::EXTLOAD, ISD::ZEXTLOAD, ISD::SEXTLOAD})
296       setLoadExtAction(Ext, T, MVT::i1, Promote);
297   if (Subtarget->hasSIMD128()) {
298     for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v2i64, MVT::v4f32,
299                    MVT::v2f64}) {
300       for (auto MemT : MVT::fixedlen_vector_valuetypes()) {
301         if (MVT(T) != MemT) {
302           setTruncStoreAction(T, MemT, Expand);
303           for (auto Ext : {ISD::EXTLOAD, ISD::ZEXTLOAD, ISD::SEXTLOAD})
304             setLoadExtAction(Ext, T, MemT, Expand);
305         }
306       }
307     }
308     // But some vector extending loads are legal
309     for (auto Ext : {ISD::EXTLOAD, ISD::SEXTLOAD, ISD::ZEXTLOAD}) {
310       setLoadExtAction(Ext, MVT::v8i16, MVT::v8i8, Legal);
311       setLoadExtAction(Ext, MVT::v4i32, MVT::v4i16, Legal);
312       setLoadExtAction(Ext, MVT::v2i64, MVT::v2i32, Legal);
313     }
314     setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f32, Legal);
315   }
316 
317   // Don't do anything clever with build_pairs
318   setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
319 
320   // Trap lowers to wasm unreachable
321   setOperationAction(ISD::TRAP, MVT::Other, Legal);
322   setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal);
323 
324   // Exception handling intrinsics
325   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
326   setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
327   setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
328 
329   setMaxAtomicSizeInBitsSupported(64);
330 
331   // Override the __gnu_f2h_ieee/__gnu_h2f_ieee names so that the f32 name is
332   // consistent with the f64 and f128 names.
333   setLibcallName(RTLIB::FPEXT_F16_F32, "__extendhfsf2");
334   setLibcallName(RTLIB::FPROUND_F32_F16, "__truncsfhf2");
335 
336   // Define the emscripten name for return address helper.
337   // TODO: when implementing other Wasm backends, make this generic or only do
338   // this on emscripten depending on what they end up doing.
339   setLibcallName(RTLIB::RETURN_ADDRESS, "emscripten_return_address");
340 
341   // Always convert switches to br_tables unless there is only one case, which
342   // is equivalent to a simple branch. This reduces code size for wasm, and we
343   // defer possible jump table optimizations to the VM.
344   setMinimumJumpTableEntries(2);
345 }
346 
347 MVT WebAssemblyTargetLowering::getPointerTy(const DataLayout &DL,
348                                             uint32_t AS) const {
349   if (AS == WebAssembly::WasmAddressSpace::WASM_ADDRESS_SPACE_EXTERNREF)
350     return MVT::externref;
351   if (AS == WebAssembly::WasmAddressSpace::WASM_ADDRESS_SPACE_FUNCREF)
352     return MVT::funcref;
353   return TargetLowering::getPointerTy(DL, AS);
354 }
355 
356 MVT WebAssemblyTargetLowering::getPointerMemTy(const DataLayout &DL,
357                                                uint32_t AS) const {
358   if (AS == WebAssembly::WasmAddressSpace::WASM_ADDRESS_SPACE_EXTERNREF)
359     return MVT::externref;
360   if (AS == WebAssembly::WasmAddressSpace::WASM_ADDRESS_SPACE_FUNCREF)
361     return MVT::funcref;
362   return TargetLowering::getPointerMemTy(DL, AS);
363 }
364 
365 TargetLowering::AtomicExpansionKind
366 WebAssemblyTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
367   // We have wasm instructions for these
368   switch (AI->getOperation()) {
369   case AtomicRMWInst::Add:
370   case AtomicRMWInst::Sub:
371   case AtomicRMWInst::And:
372   case AtomicRMWInst::Or:
373   case AtomicRMWInst::Xor:
374   case AtomicRMWInst::Xchg:
375     return AtomicExpansionKind::None;
376   default:
377     break;
378   }
379   return AtomicExpansionKind::CmpXChg;
380 }
381 
382 bool WebAssemblyTargetLowering::shouldScalarizeBinop(SDValue VecOp) const {
383   // Implementation copied from X86TargetLowering.
384   unsigned Opc = VecOp.getOpcode();
385 
386   // Assume target opcodes can't be scalarized.
387   // TODO - do we have any exceptions?
388   if (Opc >= ISD::BUILTIN_OP_END)
389     return false;
390 
391   // If the vector op is not supported, try to convert to scalar.
392   EVT VecVT = VecOp.getValueType();
393   if (!isOperationLegalOrCustomOrPromote(Opc, VecVT))
394     return true;
395 
396   // If the vector op is supported, but the scalar op is not, the transform may
397   // not be worthwhile.
398   EVT ScalarVT = VecVT.getScalarType();
399   return isOperationLegalOrCustomOrPromote(Opc, ScalarVT);
400 }
401 
402 FastISel *WebAssemblyTargetLowering::createFastISel(
403     FunctionLoweringInfo &FuncInfo, const TargetLibraryInfo *LibInfo) const {
404   return WebAssembly::createFastISel(FuncInfo, LibInfo);
405 }
406 
407 MVT WebAssemblyTargetLowering::getScalarShiftAmountTy(const DataLayout & /*DL*/,
408                                                       EVT VT) const {
409   unsigned BitWidth = NextPowerOf2(VT.getSizeInBits() - 1);
410   if (BitWidth > 1 && BitWidth < 8)
411     BitWidth = 8;
412 
413   if (BitWidth > 64) {
414     // The shift will be lowered to a libcall, and compiler-rt libcalls expect
415     // the count to be an i32.
416     BitWidth = 32;
417     assert(BitWidth >= Log2_32_Ceil(VT.getSizeInBits()) &&
418            "32-bit shift counts ought to be enough for anyone");
419   }
420 
421   MVT Result = MVT::getIntegerVT(BitWidth);
422   assert(Result != MVT::INVALID_SIMPLE_VALUE_TYPE &&
423          "Unable to represent scalar shift amount type");
424   return Result;
425 }
426 
427 // Lower an fp-to-int conversion operator from the LLVM opcode, which has an
428 // undefined result on invalid/overflow, to the WebAssembly opcode, which
429 // traps on invalid/overflow.
430 static MachineBasicBlock *LowerFPToInt(MachineInstr &MI, DebugLoc DL,
431                                        MachineBasicBlock *BB,
432                                        const TargetInstrInfo &TII,
433                                        bool IsUnsigned, bool Int64,
434                                        bool Float64, unsigned LoweredOpcode) {
435   MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
436 
437   Register OutReg = MI.getOperand(0).getReg();
438   Register InReg = MI.getOperand(1).getReg();
439 
440   unsigned Abs = Float64 ? WebAssembly::ABS_F64 : WebAssembly::ABS_F32;
441   unsigned FConst = Float64 ? WebAssembly::CONST_F64 : WebAssembly::CONST_F32;
442   unsigned LT = Float64 ? WebAssembly::LT_F64 : WebAssembly::LT_F32;
443   unsigned GE = Float64 ? WebAssembly::GE_F64 : WebAssembly::GE_F32;
444   unsigned IConst = Int64 ? WebAssembly::CONST_I64 : WebAssembly::CONST_I32;
445   unsigned Eqz = WebAssembly::EQZ_I32;
446   unsigned And = WebAssembly::AND_I32;
447   int64_t Limit = Int64 ? INT64_MIN : INT32_MIN;
448   int64_t Substitute = IsUnsigned ? 0 : Limit;
449   double CmpVal = IsUnsigned ? -(double)Limit * 2.0 : -(double)Limit;
450   auto &Context = BB->getParent()->getFunction().getContext();
451   Type *Ty = Float64 ? Type::getDoubleTy(Context) : Type::getFloatTy(Context);
452 
453   const BasicBlock *LLVMBB = BB->getBasicBlock();
454   MachineFunction *F = BB->getParent();
455   MachineBasicBlock *TrueMBB = F->CreateMachineBasicBlock(LLVMBB);
456   MachineBasicBlock *FalseMBB = F->CreateMachineBasicBlock(LLVMBB);
457   MachineBasicBlock *DoneMBB = F->CreateMachineBasicBlock(LLVMBB);
458 
459   MachineFunction::iterator It = ++BB->getIterator();
460   F->insert(It, FalseMBB);
461   F->insert(It, TrueMBB);
462   F->insert(It, DoneMBB);
463 
464   // Transfer the remainder of BB and its successor edges to DoneMBB.
465   DoneMBB->splice(DoneMBB->begin(), BB, std::next(MI.getIterator()), BB->end());
466   DoneMBB->transferSuccessorsAndUpdatePHIs(BB);
467 
468   BB->addSuccessor(TrueMBB);
469   BB->addSuccessor(FalseMBB);
470   TrueMBB->addSuccessor(DoneMBB);
471   FalseMBB->addSuccessor(DoneMBB);
472 
473   unsigned Tmp0, Tmp1, CmpReg, EqzReg, FalseReg, TrueReg;
474   Tmp0 = MRI.createVirtualRegister(MRI.getRegClass(InReg));
475   Tmp1 = MRI.createVirtualRegister(MRI.getRegClass(InReg));
476   CmpReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
477   EqzReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
478   FalseReg = MRI.createVirtualRegister(MRI.getRegClass(OutReg));
479   TrueReg = MRI.createVirtualRegister(MRI.getRegClass(OutReg));
480 
481   MI.eraseFromParent();
482   // For signed numbers, we can do a single comparison to determine whether
483   // fabs(x) is within range.
484   if (IsUnsigned) {
485     Tmp0 = InReg;
486   } else {
487     BuildMI(BB, DL, TII.get(Abs), Tmp0).addReg(InReg);
488   }
489   BuildMI(BB, DL, TII.get(FConst), Tmp1)
490       .addFPImm(cast<ConstantFP>(ConstantFP::get(Ty, CmpVal)));
491   BuildMI(BB, DL, TII.get(LT), CmpReg).addReg(Tmp0).addReg(Tmp1);
492 
493   // For unsigned numbers, we have to do a separate comparison with zero.
494   if (IsUnsigned) {
495     Tmp1 = MRI.createVirtualRegister(MRI.getRegClass(InReg));
496     Register SecondCmpReg =
497         MRI.createVirtualRegister(&WebAssembly::I32RegClass);
498     Register AndReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
499     BuildMI(BB, DL, TII.get(FConst), Tmp1)
500         .addFPImm(cast<ConstantFP>(ConstantFP::get(Ty, 0.0)));
501     BuildMI(BB, DL, TII.get(GE), SecondCmpReg).addReg(Tmp0).addReg(Tmp1);
502     BuildMI(BB, DL, TII.get(And), AndReg).addReg(CmpReg).addReg(SecondCmpReg);
503     CmpReg = AndReg;
504   }
505 
506   BuildMI(BB, DL, TII.get(Eqz), EqzReg).addReg(CmpReg);
507 
508   // Create the CFG diamond to select between doing the conversion or using
509   // the substitute value.
510   BuildMI(BB, DL, TII.get(WebAssembly::BR_IF)).addMBB(TrueMBB).addReg(EqzReg);
511   BuildMI(FalseMBB, DL, TII.get(LoweredOpcode), FalseReg).addReg(InReg);
512   BuildMI(FalseMBB, DL, TII.get(WebAssembly::BR)).addMBB(DoneMBB);
513   BuildMI(TrueMBB, DL, TII.get(IConst), TrueReg).addImm(Substitute);
514   BuildMI(*DoneMBB, DoneMBB->begin(), DL, TII.get(TargetOpcode::PHI), OutReg)
515       .addReg(FalseReg)
516       .addMBB(FalseMBB)
517       .addReg(TrueReg)
518       .addMBB(TrueMBB);
519 
520   return DoneMBB;
521 }
522 
523 static MachineBasicBlock *
524 LowerCallResults(MachineInstr &CallResults, DebugLoc DL, MachineBasicBlock *BB,
525                  const WebAssemblySubtarget *Subtarget,
526                  const TargetInstrInfo &TII) {
527   MachineInstr &CallParams = *CallResults.getPrevNode();
528   assert(CallParams.getOpcode() == WebAssembly::CALL_PARAMS);
529   assert(CallResults.getOpcode() == WebAssembly::CALL_RESULTS ||
530          CallResults.getOpcode() == WebAssembly::RET_CALL_RESULTS);
531 
532   bool IsIndirect = CallParams.getOperand(0).isReg();
533   bool IsRetCall = CallResults.getOpcode() == WebAssembly::RET_CALL_RESULTS;
534 
535   bool IsFuncrefCall = false;
536   if (IsIndirect) {
537     Register Reg = CallParams.getOperand(0).getReg();
538     const MachineFunction *MF = BB->getParent();
539     const MachineRegisterInfo &MRI = MF->getRegInfo();
540     const TargetRegisterClass *TRC = MRI.getRegClass(Reg);
541     IsFuncrefCall = (TRC == &WebAssembly::FUNCREFRegClass);
542     assert(!IsFuncrefCall || Subtarget->hasReferenceTypes());
543   }
544 
545   unsigned CallOp;
546   if (IsIndirect && IsRetCall) {
547     CallOp = WebAssembly::RET_CALL_INDIRECT;
548   } else if (IsIndirect) {
549     CallOp = WebAssembly::CALL_INDIRECT;
550   } else if (IsRetCall) {
551     CallOp = WebAssembly::RET_CALL;
552   } else {
553     CallOp = WebAssembly::CALL;
554   }
555 
556   MachineFunction &MF = *BB->getParent();
557   const MCInstrDesc &MCID = TII.get(CallOp);
558   MachineInstrBuilder MIB(MF, MF.CreateMachineInstr(MCID, DL));
559 
560   // See if we must truncate the function pointer.
561   // CALL_INDIRECT takes an i32, but in wasm64 we represent function pointers
562   // as 64-bit for uniformity with other pointer types.
563   // See also: WebAssemblyFastISel::selectCall
564   if (IsIndirect && MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()) {
565     Register Reg32 =
566         MF.getRegInfo().createVirtualRegister(&WebAssembly::I32RegClass);
567     auto &FnPtr = CallParams.getOperand(0);
568     BuildMI(*BB, CallResults.getIterator(), DL,
569             TII.get(WebAssembly::I32_WRAP_I64), Reg32)
570         .addReg(FnPtr.getReg());
571     FnPtr.setReg(Reg32);
572   }
573 
574   // Move the function pointer to the end of the arguments for indirect calls
575   if (IsIndirect) {
576     auto FnPtr = CallParams.getOperand(0);
577     CallParams.removeOperand(0);
578 
579     // For funcrefs, call_indirect is done through __funcref_call_table and the
580     // funcref is always installed in slot 0 of the table, therefore instead of
581     // having the function pointer added at the end of the params list, a zero
582     // (the index in
583     // __funcref_call_table is added).
584     if (IsFuncrefCall) {
585       Register RegZero =
586           MF.getRegInfo().createVirtualRegister(&WebAssembly::I32RegClass);
587       MachineInstrBuilder MIBC0 =
588           BuildMI(MF, DL, TII.get(WebAssembly::CONST_I32), RegZero).addImm(0);
589 
590       BB->insert(CallResults.getIterator(), MIBC0);
591       MachineInstrBuilder(MF, CallParams).addReg(RegZero);
592     } else
593       CallParams.addOperand(FnPtr);
594   }
595 
596   for (auto Def : CallResults.defs())
597     MIB.add(Def);
598 
599   if (IsIndirect) {
600     // Placeholder for the type index.
601     MIB.addImm(0);
602     // The table into which this call_indirect indexes.
603     MCSymbolWasm *Table = IsFuncrefCall
604                               ? WebAssembly::getOrCreateFuncrefCallTableSymbol(
605                                     MF.getContext(), Subtarget)
606                               : WebAssembly::getOrCreateFunctionTableSymbol(
607                                     MF.getContext(), Subtarget);
608     if (Subtarget->hasReferenceTypes()) {
609       MIB.addSym(Table);
610     } else {
611       // For the MVP there is at most one table whose number is 0, but we can't
612       // write a table symbol or issue relocations.  Instead we just ensure the
613       // table is live and write a zero.
614       Table->setNoStrip();
615       MIB.addImm(0);
616     }
617   }
618 
619   for (auto Use : CallParams.uses())
620     MIB.add(Use);
621 
622   BB->insert(CallResults.getIterator(), MIB);
623   CallParams.eraseFromParent();
624   CallResults.eraseFromParent();
625 
626   // If this is a funcref call, to avoid hidden GC roots, we need to clear the
627   // table slot with ref.null upon call_indirect return.
628   //
629   // This generates the following code, which comes right after a call_indirect
630   // of a funcref:
631   //
632   //    i32.const 0
633   //    ref.null func
634   //    table.set __funcref_call_table
635   if (IsIndirect && IsFuncrefCall) {
636     MCSymbolWasm *Table = WebAssembly::getOrCreateFuncrefCallTableSymbol(
637         MF.getContext(), Subtarget);
638     Register RegZero =
639         MF.getRegInfo().createVirtualRegister(&WebAssembly::I32RegClass);
640     MachineInstr *Const0 =
641         BuildMI(MF, DL, TII.get(WebAssembly::CONST_I32), RegZero).addImm(0);
642     BB->insertAfter(MIB.getInstr()->getIterator(), Const0);
643 
644     Register RegFuncref =
645         MF.getRegInfo().createVirtualRegister(&WebAssembly::FUNCREFRegClass);
646     MachineInstr *RefNull =
647         BuildMI(MF, DL, TII.get(WebAssembly::REF_NULL_FUNCREF), RegFuncref);
648     BB->insertAfter(Const0->getIterator(), RefNull);
649 
650     MachineInstr *TableSet =
651         BuildMI(MF, DL, TII.get(WebAssembly::TABLE_SET_FUNCREF))
652             .addSym(Table)
653             .addReg(RegZero)
654             .addReg(RegFuncref);
655     BB->insertAfter(RefNull->getIterator(), TableSet);
656   }
657 
658   return BB;
659 }
660 
661 MachineBasicBlock *WebAssemblyTargetLowering::EmitInstrWithCustomInserter(
662     MachineInstr &MI, MachineBasicBlock *BB) const {
663   const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
664   DebugLoc DL = MI.getDebugLoc();
665 
666   switch (MI.getOpcode()) {
667   default:
668     llvm_unreachable("Unexpected instr type to insert");
669   case WebAssembly::FP_TO_SINT_I32_F32:
670     return LowerFPToInt(MI, DL, BB, TII, false, false, false,
671                         WebAssembly::I32_TRUNC_S_F32);
672   case WebAssembly::FP_TO_UINT_I32_F32:
673     return LowerFPToInt(MI, DL, BB, TII, true, false, false,
674                         WebAssembly::I32_TRUNC_U_F32);
675   case WebAssembly::FP_TO_SINT_I64_F32:
676     return LowerFPToInt(MI, DL, BB, TII, false, true, false,
677                         WebAssembly::I64_TRUNC_S_F32);
678   case WebAssembly::FP_TO_UINT_I64_F32:
679     return LowerFPToInt(MI, DL, BB, TII, true, true, false,
680                         WebAssembly::I64_TRUNC_U_F32);
681   case WebAssembly::FP_TO_SINT_I32_F64:
682     return LowerFPToInt(MI, DL, BB, TII, false, false, true,
683                         WebAssembly::I32_TRUNC_S_F64);
684   case WebAssembly::FP_TO_UINT_I32_F64:
685     return LowerFPToInt(MI, DL, BB, TII, true, false, true,
686                         WebAssembly::I32_TRUNC_U_F64);
687   case WebAssembly::FP_TO_SINT_I64_F64:
688     return LowerFPToInt(MI, DL, BB, TII, false, true, true,
689                         WebAssembly::I64_TRUNC_S_F64);
690   case WebAssembly::FP_TO_UINT_I64_F64:
691     return LowerFPToInt(MI, DL, BB, TII, true, true, true,
692                         WebAssembly::I64_TRUNC_U_F64);
693   case WebAssembly::CALL_RESULTS:
694   case WebAssembly::RET_CALL_RESULTS:
695     return LowerCallResults(MI, DL, BB, Subtarget, TII);
696   }
697 }
698 
699 const char *
700 WebAssemblyTargetLowering::getTargetNodeName(unsigned Opcode) const {
701   switch (static_cast<WebAssemblyISD::NodeType>(Opcode)) {
702   case WebAssemblyISD::FIRST_NUMBER:
703   case WebAssemblyISD::FIRST_MEM_OPCODE:
704     break;
705 #define HANDLE_NODETYPE(NODE)                                                  \
706   case WebAssemblyISD::NODE:                                                   \
707     return "WebAssemblyISD::" #NODE;
708 #define HANDLE_MEM_NODETYPE(NODE) HANDLE_NODETYPE(NODE)
709 #include "WebAssemblyISD.def"
710 #undef HANDLE_MEM_NODETYPE
711 #undef HANDLE_NODETYPE
712   }
713   return nullptr;
714 }
715 
716 std::pair<unsigned, const TargetRegisterClass *>
717 WebAssemblyTargetLowering::getRegForInlineAsmConstraint(
718     const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
719   // First, see if this is a constraint that directly corresponds to a
720   // WebAssembly register class.
721   if (Constraint.size() == 1) {
722     switch (Constraint[0]) {
723     case 'r':
724       assert(VT != MVT::iPTR && "Pointer MVT not expected here");
725       if (Subtarget->hasSIMD128() && VT.isVector()) {
726         if (VT.getSizeInBits() == 128)
727           return std::make_pair(0U, &WebAssembly::V128RegClass);
728       }
729       if (VT.isInteger() && !VT.isVector()) {
730         if (VT.getSizeInBits() <= 32)
731           return std::make_pair(0U, &WebAssembly::I32RegClass);
732         if (VT.getSizeInBits() <= 64)
733           return std::make_pair(0U, &WebAssembly::I64RegClass);
734       }
735       if (VT.isFloatingPoint() && !VT.isVector()) {
736         switch (VT.getSizeInBits()) {
737         case 32:
738           return std::make_pair(0U, &WebAssembly::F32RegClass);
739         case 64:
740           return std::make_pair(0U, &WebAssembly::F64RegClass);
741         default:
742           break;
743         }
744       }
745       break;
746     default:
747       break;
748     }
749   }
750 
751   return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
752 }
753 
754 bool WebAssemblyTargetLowering::isCheapToSpeculateCttz() const {
755   // Assume ctz is a relatively cheap operation.
756   return true;
757 }
758 
759 bool WebAssemblyTargetLowering::isCheapToSpeculateCtlz() const {
760   // Assume clz is a relatively cheap operation.
761   return true;
762 }
763 
764 bool WebAssemblyTargetLowering::isLegalAddressingMode(const DataLayout &DL,
765                                                       const AddrMode &AM,
766                                                       Type *Ty, unsigned AS,
767                                                       Instruction *I) const {
768   // WebAssembly offsets are added as unsigned without wrapping. The
769   // isLegalAddressingMode gives us no way to determine if wrapping could be
770   // happening, so we approximate this by accepting only non-negative offsets.
771   if (AM.BaseOffs < 0)
772     return false;
773 
774   // WebAssembly has no scale register operands.
775   if (AM.Scale != 0)
776     return false;
777 
778   // Everything else is legal.
779   return true;
780 }
781 
782 bool WebAssemblyTargetLowering::allowsMisalignedMemoryAccesses(
783     EVT /*VT*/, unsigned /*AddrSpace*/, Align /*Align*/,
784     MachineMemOperand::Flags /*Flags*/, bool *Fast) const {
785   // WebAssembly supports unaligned accesses, though it should be declared
786   // with the p2align attribute on loads and stores which do so, and there
787   // may be a performance impact. We tell LLVM they're "fast" because
788   // for the kinds of things that LLVM uses this for (merging adjacent stores
789   // of constants, etc.), WebAssembly implementations will either want the
790   // unaligned access or they'll split anyway.
791   if (Fast)
792     *Fast = true;
793   return true;
794 }
795 
796 bool WebAssemblyTargetLowering::isIntDivCheap(EVT VT,
797                                               AttributeList Attr) const {
798   // The current thinking is that wasm engines will perform this optimization,
799   // so we can save on code size.
800   return true;
801 }
802 
803 bool WebAssemblyTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const {
804   EVT ExtT = ExtVal.getValueType();
805   EVT MemT = cast<LoadSDNode>(ExtVal->getOperand(0))->getValueType(0);
806   return (ExtT == MVT::v8i16 && MemT == MVT::v8i8) ||
807          (ExtT == MVT::v4i32 && MemT == MVT::v4i16) ||
808          (ExtT == MVT::v2i64 && MemT == MVT::v2i32);
809 }
810 
811 bool WebAssemblyTargetLowering::isOffsetFoldingLegal(
812     const GlobalAddressSDNode *GA) const {
813   // Wasm doesn't support function addresses with offsets
814   const GlobalValue *GV = GA->getGlobal();
815   return isa<Function>(GV) ? false : TargetLowering::isOffsetFoldingLegal(GA);
816 }
817 
818 EVT WebAssemblyTargetLowering::getSetCCResultType(const DataLayout &DL,
819                                                   LLVMContext &C,
820                                                   EVT VT) const {
821   if (VT.isVector())
822     return VT.changeVectorElementTypeToInteger();
823 
824   // So far, all branch instructions in Wasm take an I32 condition.
825   // The default TargetLowering::getSetCCResultType returns the pointer size,
826   // which would be useful to reduce instruction counts when testing
827   // against 64-bit pointers/values if at some point Wasm supports that.
828   return EVT::getIntegerVT(C, 32);
829 }
830 
831 bool WebAssemblyTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
832                                                    const CallInst &I,
833                                                    MachineFunction &MF,
834                                                    unsigned Intrinsic) const {
835   switch (Intrinsic) {
836   case Intrinsic::wasm_memory_atomic_notify:
837     Info.opc = ISD::INTRINSIC_W_CHAIN;
838     Info.memVT = MVT::i32;
839     Info.ptrVal = I.getArgOperand(0);
840     Info.offset = 0;
841     Info.align = Align(4);
842     // atomic.notify instruction does not really load the memory specified with
843     // this argument, but MachineMemOperand should either be load or store, so
844     // we set this to a load.
845     // FIXME Volatile isn't really correct, but currently all LLVM atomic
846     // instructions are treated as volatiles in the backend, so we should be
847     // consistent. The same applies for wasm_atomic_wait intrinsics too.
848     Info.flags = MachineMemOperand::MOVolatile | MachineMemOperand::MOLoad;
849     return true;
850   case Intrinsic::wasm_memory_atomic_wait32:
851     Info.opc = ISD::INTRINSIC_W_CHAIN;
852     Info.memVT = MVT::i32;
853     Info.ptrVal = I.getArgOperand(0);
854     Info.offset = 0;
855     Info.align = Align(4);
856     Info.flags = MachineMemOperand::MOVolatile | MachineMemOperand::MOLoad;
857     return true;
858   case Intrinsic::wasm_memory_atomic_wait64:
859     Info.opc = ISD::INTRINSIC_W_CHAIN;
860     Info.memVT = MVT::i64;
861     Info.ptrVal = I.getArgOperand(0);
862     Info.offset = 0;
863     Info.align = Align(8);
864     Info.flags = MachineMemOperand::MOVolatile | MachineMemOperand::MOLoad;
865     return true;
866   default:
867     return false;
868   }
869 }
870 
871 void WebAssemblyTargetLowering::computeKnownBitsForTargetNode(
872     const SDValue Op, KnownBits &Known, const APInt &DemandedElts,
873     const SelectionDAG &DAG, unsigned Depth) const {
874   switch (Op.getOpcode()) {
875   default:
876     break;
877   case ISD::INTRINSIC_WO_CHAIN: {
878     unsigned IntNo = Op.getConstantOperandVal(0);
879     switch (IntNo) {
880     default:
881       break;
882     case Intrinsic::wasm_bitmask: {
883       unsigned BitWidth = Known.getBitWidth();
884       EVT VT = Op.getOperand(1).getSimpleValueType();
885       unsigned PossibleBits = VT.getVectorNumElements();
886       APInt ZeroMask = APInt::getHighBitsSet(BitWidth, BitWidth - PossibleBits);
887       Known.Zero |= ZeroMask;
888       break;
889     }
890     }
891   }
892   }
893 }
894 
895 TargetLoweringBase::LegalizeTypeAction
896 WebAssemblyTargetLowering::getPreferredVectorAction(MVT VT) const {
897   if (VT.isFixedLengthVector()) {
898     MVT EltVT = VT.getVectorElementType();
899     // We have legal vector types with these lane types, so widening the
900     // vector would let us use some of the lanes directly without having to
901     // extend or truncate values.
902     if (EltVT == MVT::i8 || EltVT == MVT::i16 || EltVT == MVT::i32 ||
903         EltVT == MVT::i64 || EltVT == MVT::f32 || EltVT == MVT::f64)
904       return TypeWidenVector;
905   }
906 
907   return TargetLoweringBase::getPreferredVectorAction(VT);
908 }
909 
910 bool WebAssemblyTargetLowering::shouldSimplifyDemandedVectorElts(
911     SDValue Op, const TargetLoweringOpt &TLO) const {
912   // ISel process runs DAGCombiner after legalization; this step is called
913   // SelectionDAG optimization phase. This post-legalization combining process
914   // runs DAGCombiner on each node, and if there was a change to be made,
915   // re-runs legalization again on it and its user nodes to make sure
916   // everythiing is in a legalized state.
917   //
918   // The legalization calls lowering routines, and we do our custom lowering for
919   // build_vectors (LowerBUILD_VECTOR), which converts undef vector elements
920   // into zeros. But there is a set of routines in DAGCombiner that turns unused
921   // (= not demanded) nodes into undef, among which SimplifyDemandedVectorElts
922   // turns unused vector elements into undefs. But this routine does not work
923   // with our custom LowerBUILD_VECTOR, which turns undefs into zeros. This
924   // combination can result in a infinite loop, in which undefs are converted to
925   // zeros in legalization and back to undefs in combining.
926   //
927   // So after DAG is legalized, we prevent SimplifyDemandedVectorElts from
928   // running for build_vectors.
929   if (Op.getOpcode() == ISD::BUILD_VECTOR && TLO.LegalOps && TLO.LegalTys)
930     return false;
931   return true;
932 }
933 
934 //===----------------------------------------------------------------------===//
935 // WebAssembly Lowering private implementation.
936 //===----------------------------------------------------------------------===//
937 
938 //===----------------------------------------------------------------------===//
939 // Lowering Code
940 //===----------------------------------------------------------------------===//
941 
942 static void fail(const SDLoc &DL, SelectionDAG &DAG, const char *Msg) {
943   MachineFunction &MF = DAG.getMachineFunction();
944   DAG.getContext()->diagnose(
945       DiagnosticInfoUnsupported(MF.getFunction(), Msg, DL.getDebugLoc()));
946 }
947 
948 // Test whether the given calling convention is supported.
949 static bool callingConvSupported(CallingConv::ID CallConv) {
950   // We currently support the language-independent target-independent
951   // conventions. We don't yet have a way to annotate calls with properties like
952   // "cold", and we don't have any call-clobbered registers, so these are mostly
953   // all handled the same.
954   return CallConv == CallingConv::C || CallConv == CallingConv::Fast ||
955          CallConv == CallingConv::Cold ||
956          CallConv == CallingConv::PreserveMost ||
957          CallConv == CallingConv::PreserveAll ||
958          CallConv == CallingConv::CXX_FAST_TLS ||
959          CallConv == CallingConv::WASM_EmscriptenInvoke ||
960          CallConv == CallingConv::Swift;
961 }
962 
963 SDValue
964 WebAssemblyTargetLowering::LowerCall(CallLoweringInfo &CLI,
965                                      SmallVectorImpl<SDValue> &InVals) const {
966   SelectionDAG &DAG = CLI.DAG;
967   SDLoc DL = CLI.DL;
968   SDValue Chain = CLI.Chain;
969   SDValue Callee = CLI.Callee;
970   MachineFunction &MF = DAG.getMachineFunction();
971   auto Layout = MF.getDataLayout();
972 
973   CallingConv::ID CallConv = CLI.CallConv;
974   if (!callingConvSupported(CallConv))
975     fail(DL, DAG,
976          "WebAssembly doesn't support language-specific or target-specific "
977          "calling conventions yet");
978   if (CLI.IsPatchPoint)
979     fail(DL, DAG, "WebAssembly doesn't support patch point yet");
980 
981   if (CLI.IsTailCall) {
982     auto NoTail = [&](const char *Msg) {
983       if (CLI.CB && CLI.CB->isMustTailCall())
984         fail(DL, DAG, Msg);
985       CLI.IsTailCall = false;
986     };
987 
988     if (!Subtarget->hasTailCall())
989       NoTail("WebAssembly 'tail-call' feature not enabled");
990 
991     // Varargs calls cannot be tail calls because the buffer is on the stack
992     if (CLI.IsVarArg)
993       NoTail("WebAssembly does not support varargs tail calls");
994 
995     // Do not tail call unless caller and callee return types match
996     const Function &F = MF.getFunction();
997     const TargetMachine &TM = getTargetMachine();
998     Type *RetTy = F.getReturnType();
999     SmallVector<MVT, 4> CallerRetTys;
1000     SmallVector<MVT, 4> CalleeRetTys;
1001     computeLegalValueVTs(F, TM, RetTy, CallerRetTys);
1002     computeLegalValueVTs(F, TM, CLI.RetTy, CalleeRetTys);
1003     bool TypesMatch = CallerRetTys.size() == CalleeRetTys.size() &&
1004                       std::equal(CallerRetTys.begin(), CallerRetTys.end(),
1005                                  CalleeRetTys.begin());
1006     if (!TypesMatch)
1007       NoTail("WebAssembly tail call requires caller and callee return types to "
1008              "match");
1009 
1010     // If pointers to local stack values are passed, we cannot tail call
1011     if (CLI.CB) {
1012       for (auto &Arg : CLI.CB->args()) {
1013         Value *Val = Arg.get();
1014         // Trace the value back through pointer operations
1015         while (true) {
1016           Value *Src = Val->stripPointerCastsAndAliases();
1017           if (auto *GEP = dyn_cast<GetElementPtrInst>(Src))
1018             Src = GEP->getPointerOperand();
1019           if (Val == Src)
1020             break;
1021           Val = Src;
1022         }
1023         if (isa<AllocaInst>(Val)) {
1024           NoTail(
1025               "WebAssembly does not support tail calling with stack arguments");
1026           break;
1027         }
1028       }
1029     }
1030   }
1031 
1032   SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
1033   SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1034   SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1035 
1036   // The generic code may have added an sret argument. If we're lowering an
1037   // invoke function, the ABI requires that the function pointer be the first
1038   // argument, so we may have to swap the arguments.
1039   if (CallConv == CallingConv::WASM_EmscriptenInvoke && Outs.size() >= 2 &&
1040       Outs[0].Flags.isSRet()) {
1041     std::swap(Outs[0], Outs[1]);
1042     std::swap(OutVals[0], OutVals[1]);
1043   }
1044 
1045   bool HasSwiftSelfArg = false;
1046   bool HasSwiftErrorArg = false;
1047   unsigned NumFixedArgs = 0;
1048   for (unsigned I = 0; I < Outs.size(); ++I) {
1049     const ISD::OutputArg &Out = Outs[I];
1050     SDValue &OutVal = OutVals[I];
1051     HasSwiftSelfArg |= Out.Flags.isSwiftSelf();
1052     HasSwiftErrorArg |= Out.Flags.isSwiftError();
1053     if (Out.Flags.isNest())
1054       fail(DL, DAG, "WebAssembly hasn't implemented nest arguments");
1055     if (Out.Flags.isInAlloca())
1056       fail(DL, DAG, "WebAssembly hasn't implemented inalloca arguments");
1057     if (Out.Flags.isInConsecutiveRegs())
1058       fail(DL, DAG, "WebAssembly hasn't implemented cons regs arguments");
1059     if (Out.Flags.isInConsecutiveRegsLast())
1060       fail(DL, DAG, "WebAssembly hasn't implemented cons regs last arguments");
1061     if (Out.Flags.isByVal() && Out.Flags.getByValSize() != 0) {
1062       auto &MFI = MF.getFrameInfo();
1063       int FI = MFI.CreateStackObject(Out.Flags.getByValSize(),
1064                                      Out.Flags.getNonZeroByValAlign(),
1065                                      /*isSS=*/false);
1066       SDValue SizeNode =
1067           DAG.getConstant(Out.Flags.getByValSize(), DL, MVT::i32);
1068       SDValue FINode = DAG.getFrameIndex(FI, getPointerTy(Layout));
1069       Chain = DAG.getMemcpy(
1070           Chain, DL, FINode, OutVal, SizeNode, Out.Flags.getNonZeroByValAlign(),
1071           /*isVolatile*/ false, /*AlwaysInline=*/false,
1072           /*isTailCall*/ false, MachinePointerInfo(), MachinePointerInfo());
1073       OutVal = FINode;
1074     }
1075     // Count the number of fixed args *after* legalization.
1076     NumFixedArgs += Out.IsFixed;
1077   }
1078 
1079   bool IsVarArg = CLI.IsVarArg;
1080   auto PtrVT = getPointerTy(Layout);
1081 
1082   // For swiftcc, emit additional swiftself and swifterror arguments
1083   // if there aren't. These additional arguments are also added for callee
1084   // signature They are necessary to match callee and caller signature for
1085   // indirect call.
1086   if (CallConv == CallingConv::Swift) {
1087     if (!HasSwiftSelfArg) {
1088       NumFixedArgs++;
1089       ISD::OutputArg Arg;
1090       Arg.Flags.setSwiftSelf();
1091       CLI.Outs.push_back(Arg);
1092       SDValue ArgVal = DAG.getUNDEF(PtrVT);
1093       CLI.OutVals.push_back(ArgVal);
1094     }
1095     if (!HasSwiftErrorArg) {
1096       NumFixedArgs++;
1097       ISD::OutputArg Arg;
1098       Arg.Flags.setSwiftError();
1099       CLI.Outs.push_back(Arg);
1100       SDValue ArgVal = DAG.getUNDEF(PtrVT);
1101       CLI.OutVals.push_back(ArgVal);
1102     }
1103   }
1104 
1105   // Analyze operands of the call, assigning locations to each operand.
1106   SmallVector<CCValAssign, 16> ArgLocs;
1107   CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
1108 
1109   if (IsVarArg) {
1110     // Outgoing non-fixed arguments are placed in a buffer. First
1111     // compute their offsets and the total amount of buffer space needed.
1112     for (unsigned I = NumFixedArgs; I < Outs.size(); ++I) {
1113       const ISD::OutputArg &Out = Outs[I];
1114       SDValue &Arg = OutVals[I];
1115       EVT VT = Arg.getValueType();
1116       assert(VT != MVT::iPTR && "Legalized args should be concrete");
1117       Type *Ty = VT.getTypeForEVT(*DAG.getContext());
1118       Align Alignment =
1119           std::max(Out.Flags.getNonZeroOrigAlign(), Layout.getABITypeAlign(Ty));
1120       unsigned Offset =
1121           CCInfo.AllocateStack(Layout.getTypeAllocSize(Ty), Alignment);
1122       CCInfo.addLoc(CCValAssign::getMem(ArgLocs.size(), VT.getSimpleVT(),
1123                                         Offset, VT.getSimpleVT(),
1124                                         CCValAssign::Full));
1125     }
1126   }
1127 
1128   unsigned NumBytes = CCInfo.getAlignedCallFrameSize();
1129 
1130   SDValue FINode;
1131   if (IsVarArg && NumBytes) {
1132     // For non-fixed arguments, next emit stores to store the argument values
1133     // to the stack buffer at the offsets computed above.
1134     int FI = MF.getFrameInfo().CreateStackObject(NumBytes,
1135                                                  Layout.getStackAlignment(),
1136                                                  /*isSS=*/false);
1137     unsigned ValNo = 0;
1138     SmallVector<SDValue, 8> Chains;
1139     for (SDValue Arg : drop_begin(OutVals, NumFixedArgs)) {
1140       assert(ArgLocs[ValNo].getValNo() == ValNo &&
1141              "ArgLocs should remain in order and only hold varargs args");
1142       unsigned Offset = ArgLocs[ValNo++].getLocMemOffset();
1143       FINode = DAG.getFrameIndex(FI, getPointerTy(Layout));
1144       SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, FINode,
1145                                 DAG.getConstant(Offset, DL, PtrVT));
1146       Chains.push_back(
1147           DAG.getStore(Chain, DL, Arg, Add,
1148                        MachinePointerInfo::getFixedStack(MF, FI, Offset)));
1149     }
1150     if (!Chains.empty())
1151       Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
1152   } else if (IsVarArg) {
1153     FINode = DAG.getIntPtrConstant(0, DL);
1154   }
1155 
1156   if (Callee->getOpcode() == ISD::GlobalAddress) {
1157     // If the callee is a GlobalAddress node (quite common, every direct call
1158     // is) turn it into a TargetGlobalAddress node so that LowerGlobalAddress
1159     // doesn't at MO_GOT which is not needed for direct calls.
1160     GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Callee);
1161     Callee = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
1162                                         getPointerTy(DAG.getDataLayout()),
1163                                         GA->getOffset());
1164     Callee = DAG.getNode(WebAssemblyISD::Wrapper, DL,
1165                          getPointerTy(DAG.getDataLayout()), Callee);
1166   }
1167 
1168   // Compute the operands for the CALLn node.
1169   SmallVector<SDValue, 16> Ops;
1170   Ops.push_back(Chain);
1171   Ops.push_back(Callee);
1172 
1173   // Add all fixed arguments. Note that for non-varargs calls, NumFixedArgs
1174   // isn't reliable.
1175   Ops.append(OutVals.begin(),
1176              IsVarArg ? OutVals.begin() + NumFixedArgs : OutVals.end());
1177   // Add a pointer to the vararg buffer.
1178   if (IsVarArg)
1179     Ops.push_back(FINode);
1180 
1181   SmallVector<EVT, 8> InTys;
1182   for (const auto &In : Ins) {
1183     assert(!In.Flags.isByVal() && "byval is not valid for return values");
1184     assert(!In.Flags.isNest() && "nest is not valid for return values");
1185     if (In.Flags.isInAlloca())
1186       fail(DL, DAG, "WebAssembly hasn't implemented inalloca return values");
1187     if (In.Flags.isInConsecutiveRegs())
1188       fail(DL, DAG, "WebAssembly hasn't implemented cons regs return values");
1189     if (In.Flags.isInConsecutiveRegsLast())
1190       fail(DL, DAG,
1191            "WebAssembly hasn't implemented cons regs last return values");
1192     // Ignore In.getNonZeroOrigAlign() because all our arguments are passed in
1193     // registers.
1194     InTys.push_back(In.VT);
1195   }
1196 
1197   // Lastly, if this is a call to a funcref we need to add an instruction
1198   // table.set to the chain and transform the call.
1199   if (CLI.CB &&
1200       WebAssembly::isFuncrefType(CLI.CB->getCalledOperand()->getType())) {
1201     // In the absence of function references proposal where a funcref call is
1202     // lowered to call_ref, using reference types we generate a table.set to set
1203     // the funcref to a special table used solely for this purpose, followed by
1204     // a call_indirect. Here we just generate the table set, and return the
1205     // SDValue of the table.set so that LowerCall can finalize the lowering by
1206     // generating the call_indirect.
1207     SDValue Chain = Ops[0];
1208 
1209     MCSymbolWasm *Table = WebAssembly::getOrCreateFuncrefCallTableSymbol(
1210         MF.getContext(), Subtarget);
1211     SDValue Sym = DAG.getMCSymbol(Table, PtrVT);
1212     SDValue TableSlot = DAG.getConstant(0, DL, MVT::i32);
1213     SDValue TableSetOps[] = {Chain, Sym, TableSlot, Callee};
1214     SDValue TableSet = DAG.getMemIntrinsicNode(
1215         WebAssemblyISD::TABLE_SET, DL, DAG.getVTList(MVT::Other), TableSetOps,
1216         MVT::funcref,
1217         // Machine Mem Operand args
1218         MachinePointerInfo(
1219             WebAssembly::WasmAddressSpace::WASM_ADDRESS_SPACE_FUNCREF),
1220         CLI.CB->getCalledOperand()->getPointerAlignment(DAG.getDataLayout()),
1221         MachineMemOperand::MOStore);
1222 
1223     Ops[0] = TableSet; // The new chain is the TableSet itself
1224   }
1225 
1226   if (CLI.IsTailCall) {
1227     // ret_calls do not return values to the current frame
1228     SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1229     return DAG.getNode(WebAssemblyISD::RET_CALL, DL, NodeTys, Ops);
1230   }
1231 
1232   InTys.push_back(MVT::Other);
1233   SDVTList InTyList = DAG.getVTList(InTys);
1234   SDValue Res = DAG.getNode(WebAssemblyISD::CALL, DL, InTyList, Ops);
1235 
1236   for (size_t I = 0; I < Ins.size(); ++I)
1237     InVals.push_back(Res.getValue(I));
1238 
1239   // Return the chain
1240   return Res.getValue(Ins.size());
1241 }
1242 
1243 bool WebAssemblyTargetLowering::CanLowerReturn(
1244     CallingConv::ID /*CallConv*/, MachineFunction & /*MF*/, bool /*IsVarArg*/,
1245     const SmallVectorImpl<ISD::OutputArg> &Outs,
1246     LLVMContext & /*Context*/) const {
1247   // WebAssembly can only handle returning tuples with multivalue enabled
1248   return Subtarget->hasMultivalue() || Outs.size() <= 1;
1249 }
1250 
1251 SDValue WebAssemblyTargetLowering::LowerReturn(
1252     SDValue Chain, CallingConv::ID CallConv, bool /*IsVarArg*/,
1253     const SmallVectorImpl<ISD::OutputArg> &Outs,
1254     const SmallVectorImpl<SDValue> &OutVals, const SDLoc &DL,
1255     SelectionDAG &DAG) const {
1256   assert((Subtarget->hasMultivalue() || Outs.size() <= 1) &&
1257          "MVP WebAssembly can only return up to one value");
1258   if (!callingConvSupported(CallConv))
1259     fail(DL, DAG, "WebAssembly doesn't support non-C calling conventions");
1260 
1261   SmallVector<SDValue, 4> RetOps(1, Chain);
1262   RetOps.append(OutVals.begin(), OutVals.end());
1263   Chain = DAG.getNode(WebAssemblyISD::RETURN, DL, MVT::Other, RetOps);
1264 
1265   // Record the number and types of the return values.
1266   for (const ISD::OutputArg &Out : Outs) {
1267     assert(!Out.Flags.isByVal() && "byval is not valid for return values");
1268     assert(!Out.Flags.isNest() && "nest is not valid for return values");
1269     assert(Out.IsFixed && "non-fixed return value is not valid");
1270     if (Out.Flags.isInAlloca())
1271       fail(DL, DAG, "WebAssembly hasn't implemented inalloca results");
1272     if (Out.Flags.isInConsecutiveRegs())
1273       fail(DL, DAG, "WebAssembly hasn't implemented cons regs results");
1274     if (Out.Flags.isInConsecutiveRegsLast())
1275       fail(DL, DAG, "WebAssembly hasn't implemented cons regs last results");
1276   }
1277 
1278   return Chain;
1279 }
1280 
1281 SDValue WebAssemblyTargetLowering::LowerFormalArguments(
1282     SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
1283     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
1284     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
1285   if (!callingConvSupported(CallConv))
1286     fail(DL, DAG, "WebAssembly doesn't support non-C calling conventions");
1287 
1288   MachineFunction &MF = DAG.getMachineFunction();
1289   auto *MFI = MF.getInfo<WebAssemblyFunctionInfo>();
1290 
1291   // Set up the incoming ARGUMENTS value, which serves to represent the liveness
1292   // of the incoming values before they're represented by virtual registers.
1293   MF.getRegInfo().addLiveIn(WebAssembly::ARGUMENTS);
1294 
1295   bool HasSwiftErrorArg = false;
1296   bool HasSwiftSelfArg = false;
1297   for (const ISD::InputArg &In : Ins) {
1298     HasSwiftSelfArg |= In.Flags.isSwiftSelf();
1299     HasSwiftErrorArg |= In.Flags.isSwiftError();
1300     if (In.Flags.isInAlloca())
1301       fail(DL, DAG, "WebAssembly hasn't implemented inalloca arguments");
1302     if (In.Flags.isNest())
1303       fail(DL, DAG, "WebAssembly hasn't implemented nest arguments");
1304     if (In.Flags.isInConsecutiveRegs())
1305       fail(DL, DAG, "WebAssembly hasn't implemented cons regs arguments");
1306     if (In.Flags.isInConsecutiveRegsLast())
1307       fail(DL, DAG, "WebAssembly hasn't implemented cons regs last arguments");
1308     // Ignore In.getNonZeroOrigAlign() because all our arguments are passed in
1309     // registers.
1310     InVals.push_back(In.Used ? DAG.getNode(WebAssemblyISD::ARGUMENT, DL, In.VT,
1311                                            DAG.getTargetConstant(InVals.size(),
1312                                                                  DL, MVT::i32))
1313                              : DAG.getUNDEF(In.VT));
1314 
1315     // Record the number and types of arguments.
1316     MFI->addParam(In.VT);
1317   }
1318 
1319   // For swiftcc, emit additional swiftself and swifterror arguments
1320   // if there aren't. These additional arguments are also added for callee
1321   // signature They are necessary to match callee and caller signature for
1322   // indirect call.
1323   auto PtrVT = getPointerTy(MF.getDataLayout());
1324   if (CallConv == CallingConv::Swift) {
1325     if (!HasSwiftSelfArg) {
1326       MFI->addParam(PtrVT);
1327     }
1328     if (!HasSwiftErrorArg) {
1329       MFI->addParam(PtrVT);
1330     }
1331   }
1332   // Varargs are copied into a buffer allocated by the caller, and a pointer to
1333   // the buffer is passed as an argument.
1334   if (IsVarArg) {
1335     MVT PtrVT = getPointerTy(MF.getDataLayout());
1336     Register VarargVreg =
1337         MF.getRegInfo().createVirtualRegister(getRegClassFor(PtrVT));
1338     MFI->setVarargBufferVreg(VarargVreg);
1339     Chain = DAG.getCopyToReg(
1340         Chain, DL, VarargVreg,
1341         DAG.getNode(WebAssemblyISD::ARGUMENT, DL, PtrVT,
1342                     DAG.getTargetConstant(Ins.size(), DL, MVT::i32)));
1343     MFI->addParam(PtrVT);
1344   }
1345 
1346   // Record the number and types of arguments and results.
1347   SmallVector<MVT, 4> Params;
1348   SmallVector<MVT, 4> Results;
1349   computeSignatureVTs(MF.getFunction().getFunctionType(), &MF.getFunction(),
1350                       MF.getFunction(), DAG.getTarget(), Params, Results);
1351   for (MVT VT : Results)
1352     MFI->addResult(VT);
1353   // TODO: Use signatures in WebAssemblyMachineFunctionInfo too and unify
1354   // the param logic here with ComputeSignatureVTs
1355   assert(MFI->getParams().size() == Params.size() &&
1356          std::equal(MFI->getParams().begin(), MFI->getParams().end(),
1357                     Params.begin()));
1358 
1359   return Chain;
1360 }
1361 
1362 void WebAssemblyTargetLowering::ReplaceNodeResults(
1363     SDNode *N, SmallVectorImpl<SDValue> &Results, SelectionDAG &DAG) const {
1364   switch (N->getOpcode()) {
1365   case ISD::SIGN_EXTEND_INREG:
1366     // Do not add any results, signifying that N should not be custom lowered
1367     // after all. This happens because simd128 turns on custom lowering for
1368     // SIGN_EXTEND_INREG, but for non-vector sign extends the result might be an
1369     // illegal type.
1370     break;
1371   default:
1372     llvm_unreachable(
1373         "ReplaceNodeResults not implemented for this op for WebAssembly!");
1374   }
1375 }
1376 
1377 //===----------------------------------------------------------------------===//
1378 //  Custom lowering hooks.
1379 //===----------------------------------------------------------------------===//
1380 
1381 SDValue WebAssemblyTargetLowering::LowerOperation(SDValue Op,
1382                                                   SelectionDAG &DAG) const {
1383   SDLoc DL(Op);
1384   switch (Op.getOpcode()) {
1385   default:
1386     llvm_unreachable("unimplemented operation lowering");
1387     return SDValue();
1388   case ISD::FrameIndex:
1389     return LowerFrameIndex(Op, DAG);
1390   case ISD::GlobalAddress:
1391     return LowerGlobalAddress(Op, DAG);
1392   case ISD::GlobalTLSAddress:
1393     return LowerGlobalTLSAddress(Op, DAG);
1394   case ISD::ExternalSymbol:
1395     return LowerExternalSymbol(Op, DAG);
1396   case ISD::JumpTable:
1397     return LowerJumpTable(Op, DAG);
1398   case ISD::BR_JT:
1399     return LowerBR_JT(Op, DAG);
1400   case ISD::VASTART:
1401     return LowerVASTART(Op, DAG);
1402   case ISD::BlockAddress:
1403   case ISD::BRIND:
1404     fail(DL, DAG, "WebAssembly hasn't implemented computed gotos");
1405     return SDValue();
1406   case ISD::RETURNADDR:
1407     return LowerRETURNADDR(Op, DAG);
1408   case ISD::FRAMEADDR:
1409     return LowerFRAMEADDR(Op, DAG);
1410   case ISD::CopyToReg:
1411     return LowerCopyToReg(Op, DAG);
1412   case ISD::EXTRACT_VECTOR_ELT:
1413   case ISD::INSERT_VECTOR_ELT:
1414     return LowerAccessVectorElement(Op, DAG);
1415   case ISD::INTRINSIC_VOID:
1416   case ISD::INTRINSIC_WO_CHAIN:
1417   case ISD::INTRINSIC_W_CHAIN:
1418     return LowerIntrinsic(Op, DAG);
1419   case ISD::SIGN_EXTEND_INREG:
1420     return LowerSIGN_EXTEND_INREG(Op, DAG);
1421   case ISD::BUILD_VECTOR:
1422     return LowerBUILD_VECTOR(Op, DAG);
1423   case ISD::VECTOR_SHUFFLE:
1424     return LowerVECTOR_SHUFFLE(Op, DAG);
1425   case ISD::SETCC:
1426     return LowerSETCC(Op, DAG);
1427   case ISD::SHL:
1428   case ISD::SRA:
1429   case ISD::SRL:
1430     return LowerShift(Op, DAG);
1431   case ISD::FP_TO_SINT_SAT:
1432   case ISD::FP_TO_UINT_SAT:
1433     return LowerFP_TO_INT_SAT(Op, DAG);
1434   case ISD::LOAD:
1435     return LowerLoad(Op, DAG);
1436   case ISD::STORE:
1437     return LowerStore(Op, DAG);
1438   case ISD::CTPOP:
1439   case ISD::CTLZ:
1440   case ISD::CTTZ:
1441     return DAG.UnrollVectorOp(Op.getNode());
1442   }
1443 }
1444 
1445 static bool IsWebAssemblyGlobal(SDValue Op) {
1446   if (const GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op))
1447     return WebAssembly::isWasmVarAddressSpace(GA->getAddressSpace());
1448 
1449   return false;
1450 }
1451 
1452 static Optional<unsigned> IsWebAssemblyLocal(SDValue Op, SelectionDAG &DAG) {
1453   const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op);
1454   if (!FI)
1455     return None;
1456 
1457   auto &MF = DAG.getMachineFunction();
1458   return WebAssemblyFrameLowering::getLocalForStackObject(MF, FI->getIndex());
1459 }
1460 
1461 static bool IsWebAssemblyTable(SDValue Op) {
1462   const GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1463   if (GA && WebAssembly::isWasmVarAddressSpace(GA->getAddressSpace())) {
1464     const GlobalValue *Value = GA->getGlobal();
1465     const Type *Ty = Value->getValueType();
1466 
1467     if (Ty->isArrayTy() && WebAssembly::isRefType(Ty->getArrayElementType()))
1468       return true;
1469   }
1470   return false;
1471 }
1472 
1473 // This function will accept as Op any access to a table, so Op can
1474 // be the actual table or an offset into the table.
1475 static bool IsWebAssemblyTableWithOffset(SDValue Op) {
1476   if (Op->getOpcode() == ISD::ADD && Op->getNumOperands() == 2)
1477     return (Op->getOperand(1).getSimpleValueType() == MVT::i32 &&
1478             IsWebAssemblyTableWithOffset(Op->getOperand(0))) ||
1479            (Op->getOperand(0).getSimpleValueType() == MVT::i32 &&
1480             IsWebAssemblyTableWithOffset(Op->getOperand(1)));
1481 
1482   return IsWebAssemblyTable(Op);
1483 }
1484 
1485 // Helper for table pattern matching used in LowerStore and LowerLoad
1486 bool WebAssemblyTargetLowering::MatchTableForLowering(SelectionDAG &DAG,
1487                                                       const SDLoc &DL,
1488                                                       const SDValue &Base,
1489                                                       GlobalAddressSDNode *&GA,
1490                                                       SDValue &Idx) const {
1491   // We expect the following graph for a load of the form:
1492   // table[<var> + <constant offset>]
1493   //
1494   // Case 1:
1495   // externref = load t1
1496   // t1: i32 = add t2, i32:<constant offset>
1497   // t2: i32 = add tX, table
1498   //
1499   // This is in some cases simplified to just:
1500   // Case 2:
1501   // externref = load t1
1502   // t1: i32 = add t2, i32:tX
1503   //
1504   // So, unfortunately we need to check for both cases and if we are in the
1505   // first case extract the table GlobalAddressNode and build a new node tY
1506   // that's tY: i32 = add i32:<constant offset>, i32:tX
1507   //
1508   if (IsWebAssemblyTable(Base)) {
1509     GA = cast<GlobalAddressSDNode>(Base);
1510     Idx = DAG.getConstant(0, DL, MVT::i32);
1511   } else {
1512     GA = dyn_cast<GlobalAddressSDNode>(Base->getOperand(0));
1513     if (GA) {
1514       // We are in Case 2 above.
1515       Idx = Base->getOperand(1);
1516       assert(GA->getNumValues() == 1);
1517     } else {
1518       // This might be Case 1 above (or an error)
1519       SDValue V = Base->getOperand(0);
1520       GA = dyn_cast<GlobalAddressSDNode>(V->getOperand(1));
1521 
1522       if (V->getOpcode() != ISD::ADD || V->getNumOperands() != 2 || !GA)
1523         return false;
1524 
1525       SDValue IdxV = DAG.getNode(ISD::ADD, DL, MVT::i32, Base->getOperand(1),
1526                                  V->getOperand(0));
1527       Idx = IdxV;
1528     }
1529   }
1530 
1531   return true;
1532 }
1533 
1534 SDValue WebAssemblyTargetLowering::LowerStore(SDValue Op,
1535                                               SelectionDAG &DAG) const {
1536   SDLoc DL(Op);
1537   StoreSDNode *SN = cast<StoreSDNode>(Op.getNode());
1538   const SDValue &Value = SN->getValue();
1539   const SDValue &Base = SN->getBasePtr();
1540   const SDValue &Offset = SN->getOffset();
1541 
1542   if (IsWebAssemblyTableWithOffset(Base)) {
1543     if (!Offset->isUndef())
1544       report_fatal_error(
1545           "unexpected offset when loading from webassembly table", false);
1546 
1547     SDValue Idx;
1548     GlobalAddressSDNode *GA;
1549 
1550     if (!MatchTableForLowering(DAG, DL, Base, GA, Idx))
1551       report_fatal_error("failed pattern matching for lowering table store",
1552                          false);
1553 
1554     SDVTList Tys = DAG.getVTList(MVT::Other);
1555     SDValue TableSetOps[] = {SN->getChain(), SDValue(GA, 0), Idx, Value};
1556     SDValue TableSet =
1557         DAG.getMemIntrinsicNode(WebAssemblyISD::TABLE_SET, DL, Tys, TableSetOps,
1558                                 SN->getMemoryVT(), SN->getMemOperand());
1559     return TableSet;
1560   }
1561 
1562   if (IsWebAssemblyGlobal(Base)) {
1563     if (!Offset->isUndef())
1564       report_fatal_error("unexpected offset when storing to webassembly global",
1565                          false);
1566 
1567     SDVTList Tys = DAG.getVTList(MVT::Other);
1568     SDValue Ops[] = {SN->getChain(), Value, Base};
1569     return DAG.getMemIntrinsicNode(WebAssemblyISD::GLOBAL_SET, DL, Tys, Ops,
1570                                    SN->getMemoryVT(), SN->getMemOperand());
1571   }
1572 
1573   if (Optional<unsigned> Local = IsWebAssemblyLocal(Base, DAG)) {
1574     if (!Offset->isUndef())
1575       report_fatal_error("unexpected offset when storing to webassembly local",
1576                          false);
1577 
1578     SDValue Idx = DAG.getTargetConstant(*Local, Base, MVT::i32);
1579     SDVTList Tys = DAG.getVTList(MVT::Other); // The chain.
1580     SDValue Ops[] = {SN->getChain(), Idx, Value};
1581     return DAG.getNode(WebAssemblyISD::LOCAL_SET, DL, Tys, Ops);
1582   }
1583 
1584   return Op;
1585 }
1586 
1587 SDValue WebAssemblyTargetLowering::LowerLoad(SDValue Op,
1588                                              SelectionDAG &DAG) const {
1589   SDLoc DL(Op);
1590   LoadSDNode *LN = cast<LoadSDNode>(Op.getNode());
1591   const SDValue &Base = LN->getBasePtr();
1592   const SDValue &Offset = LN->getOffset();
1593 
1594   if (IsWebAssemblyTableWithOffset(Base)) {
1595     if (!Offset->isUndef())
1596       report_fatal_error(
1597           "unexpected offset when loading from webassembly table", false);
1598 
1599     GlobalAddressSDNode *GA;
1600     SDValue Idx;
1601 
1602     if (!MatchTableForLowering(DAG, DL, Base, GA, Idx))
1603       report_fatal_error("failed pattern matching for lowering table load",
1604                          false);
1605 
1606     SDVTList Tys = DAG.getVTList(LN->getValueType(0), MVT::Other);
1607     SDValue TableGetOps[] = {LN->getChain(), SDValue(GA, 0), Idx};
1608     SDValue TableGet =
1609         DAG.getMemIntrinsicNode(WebAssemblyISD::TABLE_GET, DL, Tys, TableGetOps,
1610                                 LN->getMemoryVT(), LN->getMemOperand());
1611     return TableGet;
1612   }
1613 
1614   if (IsWebAssemblyGlobal(Base)) {
1615     if (!Offset->isUndef())
1616       report_fatal_error(
1617           "unexpected offset when loading from webassembly global", false);
1618 
1619     SDVTList Tys = DAG.getVTList(LN->getValueType(0), MVT::Other);
1620     SDValue Ops[] = {LN->getChain(), Base};
1621     return DAG.getMemIntrinsicNode(WebAssemblyISD::GLOBAL_GET, DL, Tys, Ops,
1622                                    LN->getMemoryVT(), LN->getMemOperand());
1623   }
1624 
1625   if (Optional<unsigned> Local = IsWebAssemblyLocal(Base, DAG)) {
1626     if (!Offset->isUndef())
1627       report_fatal_error(
1628           "unexpected offset when loading from webassembly local", false);
1629 
1630     SDValue Idx = DAG.getTargetConstant(*Local, Base, MVT::i32);
1631     EVT LocalVT = LN->getValueType(0);
1632     SDValue LocalGet = DAG.getNode(WebAssemblyISD::LOCAL_GET, DL, LocalVT,
1633                                    {LN->getChain(), Idx});
1634     SDValue Result = DAG.getMergeValues({LocalGet, LN->getChain()}, DL);
1635     assert(Result->getNumValues() == 2 && "Loads must carry a chain!");
1636     return Result;
1637   }
1638 
1639   return Op;
1640 }
1641 
1642 SDValue WebAssemblyTargetLowering::LowerCopyToReg(SDValue Op,
1643                                                   SelectionDAG &DAG) const {
1644   SDValue Src = Op.getOperand(2);
1645   if (isa<FrameIndexSDNode>(Src.getNode())) {
1646     // CopyToReg nodes don't support FrameIndex operands. Other targets select
1647     // the FI to some LEA-like instruction, but since we don't have that, we
1648     // need to insert some kind of instruction that can take an FI operand and
1649     // produces a value usable by CopyToReg (i.e. in a vreg). So insert a dummy
1650     // local.copy between Op and its FI operand.
1651     SDValue Chain = Op.getOperand(0);
1652     SDLoc DL(Op);
1653     Register Reg = cast<RegisterSDNode>(Op.getOperand(1))->getReg();
1654     EVT VT = Src.getValueType();
1655     SDValue Copy(DAG.getMachineNode(VT == MVT::i32 ? WebAssembly::COPY_I32
1656                                                    : WebAssembly::COPY_I64,
1657                                     DL, VT, Src),
1658                  0);
1659     return Op.getNode()->getNumValues() == 1
1660                ? DAG.getCopyToReg(Chain, DL, Reg, Copy)
1661                : DAG.getCopyToReg(Chain, DL, Reg, Copy,
1662                                   Op.getNumOperands() == 4 ? Op.getOperand(3)
1663                                                            : SDValue());
1664   }
1665   return SDValue();
1666 }
1667 
1668 SDValue WebAssemblyTargetLowering::LowerFrameIndex(SDValue Op,
1669                                                    SelectionDAG &DAG) const {
1670   int FI = cast<FrameIndexSDNode>(Op)->getIndex();
1671   return DAG.getTargetFrameIndex(FI, Op.getValueType());
1672 }
1673 
1674 SDValue WebAssemblyTargetLowering::LowerRETURNADDR(SDValue Op,
1675                                                    SelectionDAG &DAG) const {
1676   SDLoc DL(Op);
1677 
1678   if (!Subtarget->getTargetTriple().isOSEmscripten()) {
1679     fail(DL, DAG,
1680          "Non-Emscripten WebAssembly hasn't implemented "
1681          "__builtin_return_address");
1682     return SDValue();
1683   }
1684 
1685   if (verifyReturnAddressArgumentIsConstant(Op, DAG))
1686     return SDValue();
1687 
1688   unsigned Depth = Op.getConstantOperandVal(0);
1689   MakeLibCallOptions CallOptions;
1690   return makeLibCall(DAG, RTLIB::RETURN_ADDRESS, Op.getValueType(),
1691                      {DAG.getConstant(Depth, DL, MVT::i32)}, CallOptions, DL)
1692       .first;
1693 }
1694 
1695 SDValue WebAssemblyTargetLowering::LowerFRAMEADDR(SDValue Op,
1696                                                   SelectionDAG &DAG) const {
1697   // Non-zero depths are not supported by WebAssembly currently. Use the
1698   // legalizer's default expansion, which is to return 0 (what this function is
1699   // documented to do).
1700   if (Op.getConstantOperandVal(0) > 0)
1701     return SDValue();
1702 
1703   DAG.getMachineFunction().getFrameInfo().setFrameAddressIsTaken(true);
1704   EVT VT = Op.getValueType();
1705   Register FP =
1706       Subtarget->getRegisterInfo()->getFrameRegister(DAG.getMachineFunction());
1707   return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), FP, VT);
1708 }
1709 
1710 SDValue
1711 WebAssemblyTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1712                                                  SelectionDAG &DAG) const {
1713   SDLoc DL(Op);
1714   const auto *GA = cast<GlobalAddressSDNode>(Op);
1715 
1716   MachineFunction &MF = DAG.getMachineFunction();
1717   if (!MF.getSubtarget<WebAssemblySubtarget>().hasBulkMemory())
1718     report_fatal_error("cannot use thread-local storage without bulk memory",
1719                        false);
1720 
1721   const GlobalValue *GV = GA->getGlobal();
1722 
1723   // Currently only Emscripten supports dynamic linking with threads. Therefore,
1724   // on other targets, if we have thread-local storage, only the local-exec
1725   // model is possible.
1726   auto model = Subtarget->getTargetTriple().isOSEmscripten()
1727                    ? GV->getThreadLocalMode()
1728                    : GlobalValue::LocalExecTLSModel;
1729 
1730   // Unsupported TLS modes
1731   assert(model != GlobalValue::NotThreadLocal);
1732   assert(model != GlobalValue::InitialExecTLSModel);
1733 
1734   if (model == GlobalValue::LocalExecTLSModel ||
1735       model == GlobalValue::LocalDynamicTLSModel ||
1736       (model == GlobalValue::GeneralDynamicTLSModel &&
1737        getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV))) {
1738     // For DSO-local TLS variables we use offset from __tls_base
1739 
1740     MVT PtrVT = getPointerTy(DAG.getDataLayout());
1741     auto GlobalGet = PtrVT == MVT::i64 ? WebAssembly::GLOBAL_GET_I64
1742                                        : WebAssembly::GLOBAL_GET_I32;
1743     const char *BaseName = MF.createExternalSymbolName("__tls_base");
1744 
1745     SDValue BaseAddr(
1746         DAG.getMachineNode(GlobalGet, DL, PtrVT,
1747                            DAG.getTargetExternalSymbol(BaseName, PtrVT)),
1748         0);
1749 
1750     SDValue TLSOffset = DAG.getTargetGlobalAddress(
1751         GV, DL, PtrVT, GA->getOffset(), WebAssemblyII::MO_TLS_BASE_REL);
1752     SDValue SymOffset =
1753         DAG.getNode(WebAssemblyISD::WrapperREL, DL, PtrVT, TLSOffset);
1754 
1755     return DAG.getNode(ISD::ADD, DL, PtrVT, BaseAddr, SymOffset);
1756   }
1757 
1758   assert(model == GlobalValue::GeneralDynamicTLSModel);
1759 
1760   EVT VT = Op.getValueType();
1761   return DAG.getNode(WebAssemblyISD::Wrapper, DL, VT,
1762                      DAG.getTargetGlobalAddress(GA->getGlobal(), DL, VT,
1763                                                 GA->getOffset(),
1764                                                 WebAssemblyII::MO_GOT_TLS));
1765 }
1766 
1767 SDValue WebAssemblyTargetLowering::LowerGlobalAddress(SDValue Op,
1768                                                       SelectionDAG &DAG) const {
1769   SDLoc DL(Op);
1770   const auto *GA = cast<GlobalAddressSDNode>(Op);
1771   EVT VT = Op.getValueType();
1772   assert(GA->getTargetFlags() == 0 &&
1773          "Unexpected target flags on generic GlobalAddressSDNode");
1774   if (!WebAssembly::isValidAddressSpace(GA->getAddressSpace()))
1775     fail(DL, DAG, "Invalid address space for WebAssembly target");
1776 
1777   unsigned OperandFlags = 0;
1778   if (isPositionIndependent()) {
1779     const GlobalValue *GV = GA->getGlobal();
1780     if (getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV)) {
1781       MachineFunction &MF = DAG.getMachineFunction();
1782       MVT PtrVT = getPointerTy(MF.getDataLayout());
1783       const char *BaseName;
1784       if (GV->getValueType()->isFunctionTy()) {
1785         BaseName = MF.createExternalSymbolName("__table_base");
1786         OperandFlags = WebAssemblyII::MO_TABLE_BASE_REL;
1787       } else {
1788         BaseName = MF.createExternalSymbolName("__memory_base");
1789         OperandFlags = WebAssemblyII::MO_MEMORY_BASE_REL;
1790       }
1791       SDValue BaseAddr =
1792           DAG.getNode(WebAssemblyISD::Wrapper, DL, PtrVT,
1793                       DAG.getTargetExternalSymbol(BaseName, PtrVT));
1794 
1795       SDValue SymAddr = DAG.getNode(
1796           WebAssemblyISD::WrapperREL, DL, VT,
1797           DAG.getTargetGlobalAddress(GA->getGlobal(), DL, VT, GA->getOffset(),
1798                                      OperandFlags));
1799 
1800       return DAG.getNode(ISD::ADD, DL, VT, BaseAddr, SymAddr);
1801     }
1802     OperandFlags = WebAssemblyII::MO_GOT;
1803   }
1804 
1805   return DAG.getNode(WebAssemblyISD::Wrapper, DL, VT,
1806                      DAG.getTargetGlobalAddress(GA->getGlobal(), DL, VT,
1807                                                 GA->getOffset(), OperandFlags));
1808 }
1809 
1810 SDValue
1811 WebAssemblyTargetLowering::LowerExternalSymbol(SDValue Op,
1812                                                SelectionDAG &DAG) const {
1813   SDLoc DL(Op);
1814   const auto *ES = cast<ExternalSymbolSDNode>(Op);
1815   EVT VT = Op.getValueType();
1816   assert(ES->getTargetFlags() == 0 &&
1817          "Unexpected target flags on generic ExternalSymbolSDNode");
1818   return DAG.getNode(WebAssemblyISD::Wrapper, DL, VT,
1819                      DAG.getTargetExternalSymbol(ES->getSymbol(), VT));
1820 }
1821 
1822 SDValue WebAssemblyTargetLowering::LowerJumpTable(SDValue Op,
1823                                                   SelectionDAG &DAG) const {
1824   // There's no need for a Wrapper node because we always incorporate a jump
1825   // table operand into a BR_TABLE instruction, rather than ever
1826   // materializing it in a register.
1827   const JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
1828   return DAG.getTargetJumpTable(JT->getIndex(), Op.getValueType(),
1829                                 JT->getTargetFlags());
1830 }
1831 
1832 SDValue WebAssemblyTargetLowering::LowerBR_JT(SDValue Op,
1833                                               SelectionDAG &DAG) const {
1834   SDLoc DL(Op);
1835   SDValue Chain = Op.getOperand(0);
1836   const auto *JT = cast<JumpTableSDNode>(Op.getOperand(1));
1837   SDValue Index = Op.getOperand(2);
1838   assert(JT->getTargetFlags() == 0 && "WebAssembly doesn't set target flags");
1839 
1840   SmallVector<SDValue, 8> Ops;
1841   Ops.push_back(Chain);
1842   Ops.push_back(Index);
1843 
1844   MachineJumpTableInfo *MJTI = DAG.getMachineFunction().getJumpTableInfo();
1845   const auto &MBBs = MJTI->getJumpTables()[JT->getIndex()].MBBs;
1846 
1847   // Add an operand for each case.
1848   for (auto MBB : MBBs)
1849     Ops.push_back(DAG.getBasicBlock(MBB));
1850 
1851   // Add the first MBB as a dummy default target for now. This will be replaced
1852   // with the proper default target (and the preceding range check eliminated)
1853   // if possible by WebAssemblyFixBrTableDefaults.
1854   Ops.push_back(DAG.getBasicBlock(*MBBs.begin()));
1855   return DAG.getNode(WebAssemblyISD::BR_TABLE, DL, MVT::Other, Ops);
1856 }
1857 
1858 SDValue WebAssemblyTargetLowering::LowerVASTART(SDValue Op,
1859                                                 SelectionDAG &DAG) const {
1860   SDLoc DL(Op);
1861   EVT PtrVT = getPointerTy(DAG.getMachineFunction().getDataLayout());
1862 
1863   auto *MFI = DAG.getMachineFunction().getInfo<WebAssemblyFunctionInfo>();
1864   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
1865 
1866   SDValue ArgN = DAG.getCopyFromReg(DAG.getEntryNode(), DL,
1867                                     MFI->getVarargBufferVreg(), PtrVT);
1868   return DAG.getStore(Op.getOperand(0), DL, ArgN, Op.getOperand(1),
1869                       MachinePointerInfo(SV));
1870 }
1871 
1872 SDValue WebAssemblyTargetLowering::LowerIntrinsic(SDValue Op,
1873                                                   SelectionDAG &DAG) const {
1874   MachineFunction &MF = DAG.getMachineFunction();
1875   unsigned IntNo;
1876   switch (Op.getOpcode()) {
1877   case ISD::INTRINSIC_VOID:
1878   case ISD::INTRINSIC_W_CHAIN:
1879     IntNo = Op.getConstantOperandVal(1);
1880     break;
1881   case ISD::INTRINSIC_WO_CHAIN:
1882     IntNo = Op.getConstantOperandVal(0);
1883     break;
1884   default:
1885     llvm_unreachable("Invalid intrinsic");
1886   }
1887   SDLoc DL(Op);
1888 
1889   switch (IntNo) {
1890   default:
1891     return SDValue(); // Don't custom lower most intrinsics.
1892 
1893   case Intrinsic::wasm_lsda: {
1894     auto PtrVT = getPointerTy(MF.getDataLayout());
1895     const char *SymName = MF.createExternalSymbolName(
1896         "GCC_except_table" + std::to_string(MF.getFunctionNumber()));
1897     if (isPositionIndependent()) {
1898       SDValue Node = DAG.getTargetExternalSymbol(
1899           SymName, PtrVT, WebAssemblyII::MO_MEMORY_BASE_REL);
1900       const char *BaseName = MF.createExternalSymbolName("__memory_base");
1901       SDValue BaseAddr =
1902           DAG.getNode(WebAssemblyISD::Wrapper, DL, PtrVT,
1903                       DAG.getTargetExternalSymbol(BaseName, PtrVT));
1904       SDValue SymAddr =
1905           DAG.getNode(WebAssemblyISD::WrapperREL, DL, PtrVT, Node);
1906       return DAG.getNode(ISD::ADD, DL, PtrVT, BaseAddr, SymAddr);
1907     }
1908     SDValue Node = DAG.getTargetExternalSymbol(SymName, PtrVT);
1909     return DAG.getNode(WebAssemblyISD::Wrapper, DL, PtrVT, Node);
1910   }
1911 
1912   case Intrinsic::wasm_shuffle: {
1913     // Drop in-chain and replace undefs, but otherwise pass through unchanged
1914     SDValue Ops[18];
1915     size_t OpIdx = 0;
1916     Ops[OpIdx++] = Op.getOperand(1);
1917     Ops[OpIdx++] = Op.getOperand(2);
1918     while (OpIdx < 18) {
1919       const SDValue &MaskIdx = Op.getOperand(OpIdx + 1);
1920       if (MaskIdx.isUndef() ||
1921           cast<ConstantSDNode>(MaskIdx.getNode())->getZExtValue() >= 32) {
1922         Ops[OpIdx++] = DAG.getConstant(0, DL, MVT::i32);
1923       } else {
1924         Ops[OpIdx++] = MaskIdx;
1925       }
1926     }
1927     return DAG.getNode(WebAssemblyISD::SHUFFLE, DL, Op.getValueType(), Ops);
1928   }
1929   }
1930 }
1931 
1932 SDValue
1933 WebAssemblyTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
1934                                                   SelectionDAG &DAG) const {
1935   SDLoc DL(Op);
1936   // If sign extension operations are disabled, allow sext_inreg only if operand
1937   // is a vector extract of an i8 or i16 lane. SIMD does not depend on sign
1938   // extension operations, but allowing sext_inreg in this context lets us have
1939   // simple patterns to select extract_lane_s instructions. Expanding sext_inreg
1940   // everywhere would be simpler in this file, but would necessitate large and
1941   // brittle patterns to undo the expansion and select extract_lane_s
1942   // instructions.
1943   assert(!Subtarget->hasSignExt() && Subtarget->hasSIMD128());
1944   if (Op.getOperand(0).getOpcode() != ISD::EXTRACT_VECTOR_ELT)
1945     return SDValue();
1946 
1947   const SDValue &Extract = Op.getOperand(0);
1948   MVT VecT = Extract.getOperand(0).getSimpleValueType();
1949   if (VecT.getVectorElementType().getSizeInBits() > 32)
1950     return SDValue();
1951   MVT ExtractedLaneT =
1952       cast<VTSDNode>(Op.getOperand(1).getNode())->getVT().getSimpleVT();
1953   MVT ExtractedVecT =
1954       MVT::getVectorVT(ExtractedLaneT, 128 / ExtractedLaneT.getSizeInBits());
1955   if (ExtractedVecT == VecT)
1956     return Op;
1957 
1958   // Bitcast vector to appropriate type to ensure ISel pattern coverage
1959   const SDNode *Index = Extract.getOperand(1).getNode();
1960   if (!isa<ConstantSDNode>(Index))
1961     return SDValue();
1962   unsigned IndexVal = cast<ConstantSDNode>(Index)->getZExtValue();
1963   unsigned Scale =
1964       ExtractedVecT.getVectorNumElements() / VecT.getVectorNumElements();
1965   assert(Scale > 1);
1966   SDValue NewIndex =
1967       DAG.getConstant(IndexVal * Scale, DL, Index->getValueType(0));
1968   SDValue NewExtract = DAG.getNode(
1969       ISD::EXTRACT_VECTOR_ELT, DL, Extract.getValueType(),
1970       DAG.getBitcast(ExtractedVecT, Extract.getOperand(0)), NewIndex);
1971   return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, Op.getValueType(), NewExtract,
1972                      Op.getOperand(1));
1973 }
1974 
1975 static SDValue LowerConvertLow(SDValue Op, SelectionDAG &DAG) {
1976   SDLoc DL(Op);
1977   if (Op.getValueType() != MVT::v2f64)
1978     return SDValue();
1979 
1980   auto GetConvertedLane = [](SDValue Op, unsigned &Opcode, SDValue &SrcVec,
1981                              unsigned &Index) -> bool {
1982     switch (Op.getOpcode()) {
1983     case ISD::SINT_TO_FP:
1984       Opcode = WebAssemblyISD::CONVERT_LOW_S;
1985       break;
1986     case ISD::UINT_TO_FP:
1987       Opcode = WebAssemblyISD::CONVERT_LOW_U;
1988       break;
1989     case ISD::FP_EXTEND:
1990       Opcode = WebAssemblyISD::PROMOTE_LOW;
1991       break;
1992     default:
1993       return false;
1994     }
1995 
1996     auto ExtractVector = Op.getOperand(0);
1997     if (ExtractVector.getOpcode() != ISD::EXTRACT_VECTOR_ELT)
1998       return false;
1999 
2000     if (!isa<ConstantSDNode>(ExtractVector.getOperand(1).getNode()))
2001       return false;
2002 
2003     SrcVec = ExtractVector.getOperand(0);
2004     Index = ExtractVector.getConstantOperandVal(1);
2005     return true;
2006   };
2007 
2008   unsigned LHSOpcode, RHSOpcode, LHSIndex, RHSIndex;
2009   SDValue LHSSrcVec, RHSSrcVec;
2010   if (!GetConvertedLane(Op.getOperand(0), LHSOpcode, LHSSrcVec, LHSIndex) ||
2011       !GetConvertedLane(Op.getOperand(1), RHSOpcode, RHSSrcVec, RHSIndex))
2012     return SDValue();
2013 
2014   if (LHSOpcode != RHSOpcode)
2015     return SDValue();
2016 
2017   MVT ExpectedSrcVT;
2018   switch (LHSOpcode) {
2019   case WebAssemblyISD::CONVERT_LOW_S:
2020   case WebAssemblyISD::CONVERT_LOW_U:
2021     ExpectedSrcVT = MVT::v4i32;
2022     break;
2023   case WebAssemblyISD::PROMOTE_LOW:
2024     ExpectedSrcVT = MVT::v4f32;
2025     break;
2026   }
2027   if (LHSSrcVec.getValueType() != ExpectedSrcVT)
2028     return SDValue();
2029 
2030   auto Src = LHSSrcVec;
2031   if (LHSIndex != 0 || RHSIndex != 1 || LHSSrcVec != RHSSrcVec) {
2032     // Shuffle the source vector so that the converted lanes are the low lanes.
2033     Src = DAG.getVectorShuffle(
2034         ExpectedSrcVT, DL, LHSSrcVec, RHSSrcVec,
2035         {static_cast<int>(LHSIndex), static_cast<int>(RHSIndex) + 4, -1, -1});
2036   }
2037   return DAG.getNode(LHSOpcode, DL, MVT::v2f64, Src);
2038 }
2039 
2040 SDValue WebAssemblyTargetLowering::LowerBUILD_VECTOR(SDValue Op,
2041                                                      SelectionDAG &DAG) const {
2042   if (auto ConvertLow = LowerConvertLow(Op, DAG))
2043     return ConvertLow;
2044 
2045   SDLoc DL(Op);
2046   const EVT VecT = Op.getValueType();
2047   const EVT LaneT = Op.getOperand(0).getValueType();
2048   const size_t Lanes = Op.getNumOperands();
2049   bool CanSwizzle = VecT == MVT::v16i8;
2050 
2051   // BUILD_VECTORs are lowered to the instruction that initializes the highest
2052   // possible number of lanes at once followed by a sequence of replace_lane
2053   // instructions to individually initialize any remaining lanes.
2054 
2055   // TODO: Tune this. For example, lanewise swizzling is very expensive, so
2056   // swizzled lanes should be given greater weight.
2057 
2058   // TODO: Investigate looping rather than always extracting/replacing specific
2059   // lanes to fill gaps.
2060 
2061   auto IsConstant = [](const SDValue &V) {
2062     return V.getOpcode() == ISD::Constant || V.getOpcode() == ISD::ConstantFP;
2063   };
2064 
2065   // Returns the source vector and index vector pair if they exist. Checks for:
2066   //   (extract_vector_elt
2067   //     $src,
2068   //     (sign_extend_inreg (extract_vector_elt $indices, $i))
2069   //   )
2070   auto GetSwizzleSrcs = [](size_t I, const SDValue &Lane) {
2071     auto Bail = std::make_pair(SDValue(), SDValue());
2072     if (Lane->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
2073       return Bail;
2074     const SDValue &SwizzleSrc = Lane->getOperand(0);
2075     const SDValue &IndexExt = Lane->getOperand(1);
2076     if (IndexExt->getOpcode() != ISD::SIGN_EXTEND_INREG)
2077       return Bail;
2078     const SDValue &Index = IndexExt->getOperand(0);
2079     if (Index->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
2080       return Bail;
2081     const SDValue &SwizzleIndices = Index->getOperand(0);
2082     if (SwizzleSrc.getValueType() != MVT::v16i8 ||
2083         SwizzleIndices.getValueType() != MVT::v16i8 ||
2084         Index->getOperand(1)->getOpcode() != ISD::Constant ||
2085         Index->getConstantOperandVal(1) != I)
2086       return Bail;
2087     return std::make_pair(SwizzleSrc, SwizzleIndices);
2088   };
2089 
2090   // If the lane is extracted from another vector at a constant index, return
2091   // that vector. The source vector must not have more lanes than the dest
2092   // because the shufflevector indices are in terms of the destination lanes and
2093   // would not be able to address the smaller individual source lanes.
2094   auto GetShuffleSrc = [&](const SDValue &Lane) {
2095     if (Lane->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
2096       return SDValue();
2097     if (!isa<ConstantSDNode>(Lane->getOperand(1).getNode()))
2098       return SDValue();
2099     if (Lane->getOperand(0).getValueType().getVectorNumElements() >
2100         VecT.getVectorNumElements())
2101       return SDValue();
2102     return Lane->getOperand(0);
2103   };
2104 
2105   using ValueEntry = std::pair<SDValue, size_t>;
2106   SmallVector<ValueEntry, 16> SplatValueCounts;
2107 
2108   using SwizzleEntry = std::pair<std::pair<SDValue, SDValue>, size_t>;
2109   SmallVector<SwizzleEntry, 16> SwizzleCounts;
2110 
2111   using ShuffleEntry = std::pair<SDValue, size_t>;
2112   SmallVector<ShuffleEntry, 16> ShuffleCounts;
2113 
2114   auto AddCount = [](auto &Counts, const auto &Val) {
2115     auto CountIt =
2116         llvm::find_if(Counts, [&Val](auto E) { return E.first == Val; });
2117     if (CountIt == Counts.end()) {
2118       Counts.emplace_back(Val, 1);
2119     } else {
2120       CountIt->second++;
2121     }
2122   };
2123 
2124   auto GetMostCommon = [](auto &Counts) {
2125     auto CommonIt =
2126         std::max_element(Counts.begin(), Counts.end(), llvm::less_second());
2127     assert(CommonIt != Counts.end() && "Unexpected all-undef build_vector");
2128     return *CommonIt;
2129   };
2130 
2131   size_t NumConstantLanes = 0;
2132 
2133   // Count eligible lanes for each type of vector creation op
2134   for (size_t I = 0; I < Lanes; ++I) {
2135     const SDValue &Lane = Op->getOperand(I);
2136     if (Lane.isUndef())
2137       continue;
2138 
2139     AddCount(SplatValueCounts, Lane);
2140 
2141     if (IsConstant(Lane))
2142       NumConstantLanes++;
2143     if (auto ShuffleSrc = GetShuffleSrc(Lane))
2144       AddCount(ShuffleCounts, ShuffleSrc);
2145     if (CanSwizzle) {
2146       auto SwizzleSrcs = GetSwizzleSrcs(I, Lane);
2147       if (SwizzleSrcs.first)
2148         AddCount(SwizzleCounts, SwizzleSrcs);
2149     }
2150   }
2151 
2152   SDValue SplatValue;
2153   size_t NumSplatLanes;
2154   std::tie(SplatValue, NumSplatLanes) = GetMostCommon(SplatValueCounts);
2155 
2156   SDValue SwizzleSrc;
2157   SDValue SwizzleIndices;
2158   size_t NumSwizzleLanes = 0;
2159   if (SwizzleCounts.size())
2160     std::forward_as_tuple(std::tie(SwizzleSrc, SwizzleIndices),
2161                           NumSwizzleLanes) = GetMostCommon(SwizzleCounts);
2162 
2163   // Shuffles can draw from up to two vectors, so find the two most common
2164   // sources.
2165   SDValue ShuffleSrc1, ShuffleSrc2;
2166   size_t NumShuffleLanes = 0;
2167   if (ShuffleCounts.size()) {
2168     std::tie(ShuffleSrc1, NumShuffleLanes) = GetMostCommon(ShuffleCounts);
2169     llvm::erase_if(ShuffleCounts,
2170                    [&](const auto &Pair) { return Pair.first == ShuffleSrc1; });
2171   }
2172   if (ShuffleCounts.size()) {
2173     size_t AdditionalShuffleLanes;
2174     std::tie(ShuffleSrc2, AdditionalShuffleLanes) =
2175         GetMostCommon(ShuffleCounts);
2176     NumShuffleLanes += AdditionalShuffleLanes;
2177   }
2178 
2179   // Predicate returning true if the lane is properly initialized by the
2180   // original instruction
2181   std::function<bool(size_t, const SDValue &)> IsLaneConstructed;
2182   SDValue Result;
2183   // Prefer swizzles over shuffles over vector consts over splats
2184   if (NumSwizzleLanes >= NumShuffleLanes &&
2185       NumSwizzleLanes >= NumConstantLanes && NumSwizzleLanes >= NumSplatLanes) {
2186     Result = DAG.getNode(WebAssemblyISD::SWIZZLE, DL, VecT, SwizzleSrc,
2187                          SwizzleIndices);
2188     auto Swizzled = std::make_pair(SwizzleSrc, SwizzleIndices);
2189     IsLaneConstructed = [&, Swizzled](size_t I, const SDValue &Lane) {
2190       return Swizzled == GetSwizzleSrcs(I, Lane);
2191     };
2192   } else if (NumShuffleLanes >= NumConstantLanes &&
2193              NumShuffleLanes >= NumSplatLanes) {
2194     size_t DestLaneSize = VecT.getVectorElementType().getFixedSizeInBits() / 8;
2195     size_t DestLaneCount = VecT.getVectorNumElements();
2196     size_t Scale1 = 1;
2197     size_t Scale2 = 1;
2198     SDValue Src1 = ShuffleSrc1;
2199     SDValue Src2 = ShuffleSrc2 ? ShuffleSrc2 : DAG.getUNDEF(VecT);
2200     if (Src1.getValueType() != VecT) {
2201       size_t LaneSize =
2202           Src1.getValueType().getVectorElementType().getFixedSizeInBits() / 8;
2203       assert(LaneSize > DestLaneSize);
2204       Scale1 = LaneSize / DestLaneSize;
2205       Src1 = DAG.getBitcast(VecT, Src1);
2206     }
2207     if (Src2.getValueType() != VecT) {
2208       size_t LaneSize =
2209           Src2.getValueType().getVectorElementType().getFixedSizeInBits() / 8;
2210       assert(LaneSize > DestLaneSize);
2211       Scale2 = LaneSize / DestLaneSize;
2212       Src2 = DAG.getBitcast(VecT, Src2);
2213     }
2214 
2215     int Mask[16];
2216     assert(DestLaneCount <= 16);
2217     for (size_t I = 0; I < DestLaneCount; ++I) {
2218       const SDValue &Lane = Op->getOperand(I);
2219       SDValue Src = GetShuffleSrc(Lane);
2220       if (Src == ShuffleSrc1) {
2221         Mask[I] = Lane->getConstantOperandVal(1) * Scale1;
2222       } else if (Src && Src == ShuffleSrc2) {
2223         Mask[I] = DestLaneCount + Lane->getConstantOperandVal(1) * Scale2;
2224       } else {
2225         Mask[I] = -1;
2226       }
2227     }
2228     ArrayRef<int> MaskRef(Mask, DestLaneCount);
2229     Result = DAG.getVectorShuffle(VecT, DL, Src1, Src2, MaskRef);
2230     IsLaneConstructed = [&](size_t, const SDValue &Lane) {
2231       auto Src = GetShuffleSrc(Lane);
2232       return Src == ShuffleSrc1 || (Src && Src == ShuffleSrc2);
2233     };
2234   } else if (NumConstantLanes >= NumSplatLanes) {
2235     SmallVector<SDValue, 16> ConstLanes;
2236     for (const SDValue &Lane : Op->op_values()) {
2237       if (IsConstant(Lane)) {
2238         // Values may need to be fixed so that they will sign extend to be
2239         // within the expected range during ISel. Check whether the value is in
2240         // bounds based on the lane bit width and if it is out of bounds, lop
2241         // off the extra bits and subtract 2^n to reflect giving the high bit
2242         // value -2^(n-1) rather than +2^(n-1). Skip the i64 case because it
2243         // cannot possibly be out of range.
2244         auto *Const = dyn_cast<ConstantSDNode>(Lane.getNode());
2245         int64_t Val = Const ? Const->getSExtValue() : 0;
2246         uint64_t LaneBits = 128 / Lanes;
2247         assert((LaneBits == 64 || Val >= -(1ll << (LaneBits - 1))) &&
2248                "Unexpected out of bounds negative value");
2249         if (Const && LaneBits != 64 && Val > (1ll << (LaneBits - 1)) - 1) {
2250           auto NewVal = ((uint64_t)Val % (1ll << LaneBits)) - (1ll << LaneBits);
2251           ConstLanes.push_back(DAG.getConstant(NewVal, SDLoc(Lane), LaneT));
2252         } else {
2253           ConstLanes.push_back(Lane);
2254         }
2255       } else if (LaneT.isFloatingPoint()) {
2256         ConstLanes.push_back(DAG.getConstantFP(0, DL, LaneT));
2257       } else {
2258         ConstLanes.push_back(DAG.getConstant(0, DL, LaneT));
2259       }
2260     }
2261     Result = DAG.getBuildVector(VecT, DL, ConstLanes);
2262     IsLaneConstructed = [&IsConstant](size_t _, const SDValue &Lane) {
2263       return IsConstant(Lane);
2264     };
2265   } else {
2266     // Use a splat, but possibly a load_splat
2267     LoadSDNode *SplattedLoad;
2268     if ((SplattedLoad = dyn_cast<LoadSDNode>(SplatValue)) &&
2269         SplattedLoad->getMemoryVT() == VecT.getVectorElementType()) {
2270       Result = DAG.getMemIntrinsicNode(
2271           WebAssemblyISD::LOAD_SPLAT, DL, DAG.getVTList(VecT),
2272           {SplattedLoad->getChain(), SplattedLoad->getBasePtr(),
2273            SplattedLoad->getOffset()},
2274           SplattedLoad->getMemoryVT(), SplattedLoad->getMemOperand());
2275     } else {
2276       Result = DAG.getSplatBuildVector(VecT, DL, SplatValue);
2277     }
2278     IsLaneConstructed = [&SplatValue](size_t _, const SDValue &Lane) {
2279       return Lane == SplatValue;
2280     };
2281   }
2282 
2283   assert(Result);
2284   assert(IsLaneConstructed);
2285 
2286   // Add replace_lane instructions for any unhandled values
2287   for (size_t I = 0; I < Lanes; ++I) {
2288     const SDValue &Lane = Op->getOperand(I);
2289     if (!Lane.isUndef() && !IsLaneConstructed(I, Lane))
2290       Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VecT, Result, Lane,
2291                            DAG.getConstant(I, DL, MVT::i32));
2292   }
2293 
2294   return Result;
2295 }
2296 
2297 SDValue
2298 WebAssemblyTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
2299                                                SelectionDAG &DAG) const {
2300   SDLoc DL(Op);
2301   ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Op.getNode())->getMask();
2302   MVT VecType = Op.getOperand(0).getSimpleValueType();
2303   assert(VecType.is128BitVector() && "Unexpected shuffle vector type");
2304   size_t LaneBytes = VecType.getVectorElementType().getSizeInBits() / 8;
2305 
2306   // Space for two vector args and sixteen mask indices
2307   SDValue Ops[18];
2308   size_t OpIdx = 0;
2309   Ops[OpIdx++] = Op.getOperand(0);
2310   Ops[OpIdx++] = Op.getOperand(1);
2311 
2312   // Expand mask indices to byte indices and materialize them as operands
2313   for (int M : Mask) {
2314     for (size_t J = 0; J < LaneBytes; ++J) {
2315       // Lower undefs (represented by -1 in mask) to zero
2316       uint64_t ByteIndex = M == -1 ? 0 : (uint64_t)M * LaneBytes + J;
2317       Ops[OpIdx++] = DAG.getConstant(ByteIndex, DL, MVT::i32);
2318     }
2319   }
2320 
2321   return DAG.getNode(WebAssemblyISD::SHUFFLE, DL, Op.getValueType(), Ops);
2322 }
2323 
2324 SDValue WebAssemblyTargetLowering::LowerSETCC(SDValue Op,
2325                                               SelectionDAG &DAG) const {
2326   SDLoc DL(Op);
2327   // The legalizer does not know how to expand the unsupported comparison modes
2328   // of i64x2 vectors, so we manually unroll them here.
2329   assert(Op->getOperand(0)->getSimpleValueType(0) == MVT::v2i64);
2330   SmallVector<SDValue, 2> LHS, RHS;
2331   DAG.ExtractVectorElements(Op->getOperand(0), LHS);
2332   DAG.ExtractVectorElements(Op->getOperand(1), RHS);
2333   const SDValue &CC = Op->getOperand(2);
2334   auto MakeLane = [&](unsigned I) {
2335     return DAG.getNode(ISD::SELECT_CC, DL, MVT::i64, LHS[I], RHS[I],
2336                        DAG.getConstant(uint64_t(-1), DL, MVT::i64),
2337                        DAG.getConstant(uint64_t(0), DL, MVT::i64), CC);
2338   };
2339   return DAG.getBuildVector(Op->getValueType(0), DL,
2340                             {MakeLane(0), MakeLane(1)});
2341 }
2342 
2343 SDValue
2344 WebAssemblyTargetLowering::LowerAccessVectorElement(SDValue Op,
2345                                                     SelectionDAG &DAG) const {
2346   // Allow constant lane indices, expand variable lane indices
2347   SDNode *IdxNode = Op.getOperand(Op.getNumOperands() - 1).getNode();
2348   if (isa<ConstantSDNode>(IdxNode) || IdxNode->isUndef())
2349     return Op;
2350   else
2351     // Perform default expansion
2352     return SDValue();
2353 }
2354 
2355 static SDValue unrollVectorShift(SDValue Op, SelectionDAG &DAG) {
2356   EVT LaneT = Op.getSimpleValueType().getVectorElementType();
2357   // 32-bit and 64-bit unrolled shifts will have proper semantics
2358   if (LaneT.bitsGE(MVT::i32))
2359     return DAG.UnrollVectorOp(Op.getNode());
2360   // Otherwise mask the shift value to get proper semantics from 32-bit shift
2361   SDLoc DL(Op);
2362   size_t NumLanes = Op.getSimpleValueType().getVectorNumElements();
2363   SDValue Mask = DAG.getConstant(LaneT.getSizeInBits() - 1, DL, MVT::i32);
2364   unsigned ShiftOpcode = Op.getOpcode();
2365   SmallVector<SDValue, 16> ShiftedElements;
2366   DAG.ExtractVectorElements(Op.getOperand(0), ShiftedElements, 0, 0, MVT::i32);
2367   SmallVector<SDValue, 16> ShiftElements;
2368   DAG.ExtractVectorElements(Op.getOperand(1), ShiftElements, 0, 0, MVT::i32);
2369   SmallVector<SDValue, 16> UnrolledOps;
2370   for (size_t i = 0; i < NumLanes; ++i) {
2371     SDValue MaskedShiftValue =
2372         DAG.getNode(ISD::AND, DL, MVT::i32, ShiftElements[i], Mask);
2373     SDValue ShiftedValue = ShiftedElements[i];
2374     if (ShiftOpcode == ISD::SRA)
2375       ShiftedValue = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32,
2376                                  ShiftedValue, DAG.getValueType(LaneT));
2377     UnrolledOps.push_back(
2378         DAG.getNode(ShiftOpcode, DL, MVT::i32, ShiftedValue, MaskedShiftValue));
2379   }
2380   return DAG.getBuildVector(Op.getValueType(), DL, UnrolledOps);
2381 }
2382 
2383 SDValue WebAssemblyTargetLowering::LowerShift(SDValue Op,
2384                                               SelectionDAG &DAG) const {
2385   SDLoc DL(Op);
2386 
2387   // Only manually lower vector shifts
2388   assert(Op.getSimpleValueType().isVector());
2389 
2390   auto ShiftVal = DAG.getSplatValue(Op.getOperand(1));
2391   if (!ShiftVal)
2392     return unrollVectorShift(Op, DAG);
2393 
2394   // Use anyext because none of the high bits can affect the shift
2395   ShiftVal = DAG.getAnyExtOrTrunc(ShiftVal, DL, MVT::i32);
2396 
2397   unsigned Opcode;
2398   switch (Op.getOpcode()) {
2399   case ISD::SHL:
2400     Opcode = WebAssemblyISD::VEC_SHL;
2401     break;
2402   case ISD::SRA:
2403     Opcode = WebAssemblyISD::VEC_SHR_S;
2404     break;
2405   case ISD::SRL:
2406     Opcode = WebAssemblyISD::VEC_SHR_U;
2407     break;
2408   default:
2409     llvm_unreachable("unexpected opcode");
2410   }
2411 
2412   return DAG.getNode(Opcode, DL, Op.getValueType(), Op.getOperand(0), ShiftVal);
2413 }
2414 
2415 SDValue WebAssemblyTargetLowering::LowerFP_TO_INT_SAT(SDValue Op,
2416                                                       SelectionDAG &DAG) const {
2417   SDLoc DL(Op);
2418   EVT ResT = Op.getValueType();
2419   EVT SatVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
2420 
2421   if ((ResT == MVT::i32 || ResT == MVT::i64) &&
2422       (SatVT == MVT::i32 || SatVT == MVT::i64))
2423     return Op;
2424 
2425   if (ResT == MVT::v4i32 && SatVT == MVT::i32)
2426     return Op;
2427 
2428   return SDValue();
2429 }
2430 
2431 //===----------------------------------------------------------------------===//
2432 //   Custom DAG combine hooks
2433 //===----------------------------------------------------------------------===//
2434 static SDValue
2435 performVECTOR_SHUFFLECombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
2436   auto &DAG = DCI.DAG;
2437   auto Shuffle = cast<ShuffleVectorSDNode>(N);
2438 
2439   // Hoist vector bitcasts that don't change the number of lanes out of unary
2440   // shuffles, where they are less likely to get in the way of other combines.
2441   // (shuffle (vNxT1 (bitcast (vNxT0 x))), undef, mask) ->
2442   //  (vNxT1 (bitcast (vNxT0 (shuffle x, undef, mask))))
2443   SDValue Bitcast = N->getOperand(0);
2444   if (Bitcast.getOpcode() != ISD::BITCAST)
2445     return SDValue();
2446   if (!N->getOperand(1).isUndef())
2447     return SDValue();
2448   SDValue CastOp = Bitcast.getOperand(0);
2449   MVT SrcType = CastOp.getSimpleValueType();
2450   MVT DstType = Bitcast.getSimpleValueType();
2451   if (!SrcType.is128BitVector() ||
2452       SrcType.getVectorNumElements() != DstType.getVectorNumElements())
2453     return SDValue();
2454   SDValue NewShuffle = DAG.getVectorShuffle(
2455       SrcType, SDLoc(N), CastOp, DAG.getUNDEF(SrcType), Shuffle->getMask());
2456   return DAG.getBitcast(DstType, NewShuffle);
2457 }
2458 
2459 static SDValue
2460 performVectorExtendCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
2461   auto &DAG = DCI.DAG;
2462   assert(N->getOpcode() == ISD::SIGN_EXTEND ||
2463          N->getOpcode() == ISD::ZERO_EXTEND);
2464 
2465   // Combine ({s,z}ext (extract_subvector src, i)) into a widening operation if
2466   // possible before the extract_subvector can be expanded.
2467   auto Extract = N->getOperand(0);
2468   if (Extract.getOpcode() != ISD::EXTRACT_SUBVECTOR)
2469     return SDValue();
2470   auto Source = Extract.getOperand(0);
2471   auto *IndexNode = dyn_cast<ConstantSDNode>(Extract.getOperand(1));
2472   if (IndexNode == nullptr)
2473     return SDValue();
2474   auto Index = IndexNode->getZExtValue();
2475 
2476   // Only v8i8, v4i16, and v2i32 extracts can be widened, and only if the
2477   // extracted subvector is the low or high half of its source.
2478   EVT ResVT = N->getValueType(0);
2479   if (ResVT == MVT::v8i16) {
2480     if (Extract.getValueType() != MVT::v8i8 ||
2481         Source.getValueType() != MVT::v16i8 || (Index != 0 && Index != 8))
2482       return SDValue();
2483   } else if (ResVT == MVT::v4i32) {
2484     if (Extract.getValueType() != MVT::v4i16 ||
2485         Source.getValueType() != MVT::v8i16 || (Index != 0 && Index != 4))
2486       return SDValue();
2487   } else if (ResVT == MVT::v2i64) {
2488     if (Extract.getValueType() != MVT::v2i32 ||
2489         Source.getValueType() != MVT::v4i32 || (Index != 0 && Index != 2))
2490       return SDValue();
2491   } else {
2492     return SDValue();
2493   }
2494 
2495   bool IsSext = N->getOpcode() == ISD::SIGN_EXTEND;
2496   bool IsLow = Index == 0;
2497 
2498   unsigned Op = IsSext ? (IsLow ? WebAssemblyISD::EXTEND_LOW_S
2499                                 : WebAssemblyISD::EXTEND_HIGH_S)
2500                        : (IsLow ? WebAssemblyISD::EXTEND_LOW_U
2501                                 : WebAssemblyISD::EXTEND_HIGH_U);
2502 
2503   return DAG.getNode(Op, SDLoc(N), ResVT, Source);
2504 }
2505 
2506 static SDValue
2507 performVectorTruncZeroCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
2508   auto &DAG = DCI.DAG;
2509 
2510   auto GetWasmConversionOp = [](unsigned Op) {
2511     switch (Op) {
2512     case ISD::FP_TO_SINT_SAT:
2513       return WebAssemblyISD::TRUNC_SAT_ZERO_S;
2514     case ISD::FP_TO_UINT_SAT:
2515       return WebAssemblyISD::TRUNC_SAT_ZERO_U;
2516     case ISD::FP_ROUND:
2517       return WebAssemblyISD::DEMOTE_ZERO;
2518     }
2519     llvm_unreachable("unexpected op");
2520   };
2521 
2522   auto IsZeroSplat = [](SDValue SplatVal) {
2523     auto *Splat = dyn_cast<BuildVectorSDNode>(SplatVal.getNode());
2524     APInt SplatValue, SplatUndef;
2525     unsigned SplatBitSize;
2526     bool HasAnyUndefs;
2527     return Splat &&
2528            Splat->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
2529                                   HasAnyUndefs) &&
2530            SplatValue == 0;
2531   };
2532 
2533   if (N->getOpcode() == ISD::CONCAT_VECTORS) {
2534     // Combine this:
2535     //
2536     //   (concat_vectors (v2i32 (fp_to_{s,u}int_sat $x, 32)), (v2i32 (splat 0)))
2537     //
2538     // into (i32x4.trunc_sat_f64x2_zero_{s,u} $x).
2539     //
2540     // Or this:
2541     //
2542     //   (concat_vectors (v2f32 (fp_round (v2f64 $x))), (v2f32 (splat 0)))
2543     //
2544     // into (f32x4.demote_zero_f64x2 $x).
2545     EVT ResVT;
2546     EVT ExpectedConversionType;
2547     auto Conversion = N->getOperand(0);
2548     auto ConversionOp = Conversion.getOpcode();
2549     switch (ConversionOp) {
2550     case ISD::FP_TO_SINT_SAT:
2551     case ISD::FP_TO_UINT_SAT:
2552       ResVT = MVT::v4i32;
2553       ExpectedConversionType = MVT::v2i32;
2554       break;
2555     case ISD::FP_ROUND:
2556       ResVT = MVT::v4f32;
2557       ExpectedConversionType = MVT::v2f32;
2558       break;
2559     default:
2560       return SDValue();
2561     }
2562 
2563     if (N->getValueType(0) != ResVT)
2564       return SDValue();
2565 
2566     if (Conversion.getValueType() != ExpectedConversionType)
2567       return SDValue();
2568 
2569     auto Source = Conversion.getOperand(0);
2570     if (Source.getValueType() != MVT::v2f64)
2571       return SDValue();
2572 
2573     if (!IsZeroSplat(N->getOperand(1)) ||
2574         N->getOperand(1).getValueType() != ExpectedConversionType)
2575       return SDValue();
2576 
2577     unsigned Op = GetWasmConversionOp(ConversionOp);
2578     return DAG.getNode(Op, SDLoc(N), ResVT, Source);
2579   }
2580 
2581   // Combine this:
2582   //
2583   //   (fp_to_{s,u}int_sat (concat_vectors $x, (v2f64 (splat 0))), 32)
2584   //
2585   // into (i32x4.trunc_sat_f64x2_zero_{s,u} $x).
2586   //
2587   // Or this:
2588   //
2589   //   (v4f32 (fp_round (concat_vectors $x, (v2f64 (splat 0)))))
2590   //
2591   // into (f32x4.demote_zero_f64x2 $x).
2592   EVT ResVT;
2593   auto ConversionOp = N->getOpcode();
2594   switch (ConversionOp) {
2595   case ISD::FP_TO_SINT_SAT:
2596   case ISD::FP_TO_UINT_SAT:
2597     ResVT = MVT::v4i32;
2598     break;
2599   case ISD::FP_ROUND:
2600     ResVT = MVT::v4f32;
2601     break;
2602   default:
2603     llvm_unreachable("unexpected op");
2604   }
2605 
2606   if (N->getValueType(0) != ResVT)
2607     return SDValue();
2608 
2609   auto Concat = N->getOperand(0);
2610   if (Concat.getValueType() != MVT::v4f64)
2611     return SDValue();
2612 
2613   auto Source = Concat.getOperand(0);
2614   if (Source.getValueType() != MVT::v2f64)
2615     return SDValue();
2616 
2617   if (!IsZeroSplat(Concat.getOperand(1)) ||
2618       Concat.getOperand(1).getValueType() != MVT::v2f64)
2619     return SDValue();
2620 
2621   unsigned Op = GetWasmConversionOp(ConversionOp);
2622   return DAG.getNode(Op, SDLoc(N), ResVT, Source);
2623 }
2624 
2625 // Helper to extract VectorWidth bits from Vec, starting from IdxVal.
2626 static SDValue extractSubVector(SDValue Vec, unsigned IdxVal, SelectionDAG &DAG,
2627                                 const SDLoc &DL, unsigned VectorWidth) {
2628   EVT VT = Vec.getValueType();
2629   EVT ElVT = VT.getVectorElementType();
2630   unsigned Factor = VT.getSizeInBits() / VectorWidth;
2631   EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
2632                                   VT.getVectorNumElements() / Factor);
2633 
2634   // Extract the relevant VectorWidth bits.  Generate an EXTRACT_SUBVECTOR
2635   unsigned ElemsPerChunk = VectorWidth / ElVT.getSizeInBits();
2636   assert(isPowerOf2_32(ElemsPerChunk) && "Elements per chunk not power of 2");
2637 
2638   // This is the index of the first element of the VectorWidth-bit chunk
2639   // we want. Since ElemsPerChunk is a power of 2 just need to clear bits.
2640   IdxVal &= ~(ElemsPerChunk - 1);
2641 
2642   // If the input is a buildvector just emit a smaller one.
2643   if (Vec.getOpcode() == ISD::BUILD_VECTOR)
2644     return DAG.getBuildVector(ResultVT, DL,
2645                               Vec->ops().slice(IdxVal, ElemsPerChunk));
2646 
2647   SDValue VecIdx = DAG.getIntPtrConstant(IdxVal, DL);
2648   return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, ResultVT, Vec, VecIdx);
2649 }
2650 
2651 // Helper to recursively truncate vector elements in half with NARROW_U. DstVT
2652 // is the expected destination value type after recursion. In is the initial
2653 // input. Note that the input should have enough leading zero bits to prevent
2654 // NARROW_U from saturating results.
2655 static SDValue truncateVectorWithNARROW(EVT DstVT, SDValue In, const SDLoc &DL,
2656                                         SelectionDAG &DAG) {
2657   EVT SrcVT = In.getValueType();
2658 
2659   // No truncation required, we might get here due to recursive calls.
2660   if (SrcVT == DstVT)
2661     return In;
2662 
2663   unsigned SrcSizeInBits = SrcVT.getSizeInBits();
2664   unsigned NumElems = SrcVT.getVectorNumElements();
2665   if (!isPowerOf2_32(NumElems))
2666     return SDValue();
2667   assert(DstVT.getVectorNumElements() == NumElems && "Illegal truncation");
2668   assert(SrcSizeInBits > DstVT.getSizeInBits() && "Illegal truncation");
2669 
2670   LLVMContext &Ctx = *DAG.getContext();
2671   EVT PackedSVT = EVT::getIntegerVT(Ctx, SrcVT.getScalarSizeInBits() / 2);
2672 
2673   // Narrow to the largest type possible:
2674   // vXi64/vXi32 -> i16x8.narrow_i32x4_u and vXi16 -> i8x16.narrow_i16x8_u.
2675   EVT InVT = MVT::i16, OutVT = MVT::i8;
2676   if (SrcVT.getScalarSizeInBits() > 16) {
2677     InVT = MVT::i32;
2678     OutVT = MVT::i16;
2679   }
2680   unsigned SubSizeInBits = SrcSizeInBits / 2;
2681   InVT = EVT::getVectorVT(Ctx, InVT, SubSizeInBits / InVT.getSizeInBits());
2682   OutVT = EVT::getVectorVT(Ctx, OutVT, SubSizeInBits / OutVT.getSizeInBits());
2683 
2684   // Split lower/upper subvectors.
2685   SDValue Lo = extractSubVector(In, 0, DAG, DL, SubSizeInBits);
2686   SDValue Hi = extractSubVector(In, NumElems / 2, DAG, DL, SubSizeInBits);
2687 
2688   // 256bit -> 128bit truncate - Narrow lower/upper 128-bit subvectors.
2689   if (SrcVT.is256BitVector() && DstVT.is128BitVector()) {
2690     Lo = DAG.getBitcast(InVT, Lo);
2691     Hi = DAG.getBitcast(InVT, Hi);
2692     SDValue Res = DAG.getNode(WebAssemblyISD::NARROW_U, DL, OutVT, Lo, Hi);
2693     return DAG.getBitcast(DstVT, Res);
2694   }
2695 
2696   // Recursively narrow lower/upper subvectors, concat result and narrow again.
2697   EVT PackedVT = EVT::getVectorVT(Ctx, PackedSVT, NumElems / 2);
2698   Lo = truncateVectorWithNARROW(PackedVT, Lo, DL, DAG);
2699   Hi = truncateVectorWithNARROW(PackedVT, Hi, DL, DAG);
2700 
2701   PackedVT = EVT::getVectorVT(Ctx, PackedSVT, NumElems);
2702   SDValue Res = DAG.getNode(ISD::CONCAT_VECTORS, DL, PackedVT, Lo, Hi);
2703   return truncateVectorWithNARROW(DstVT, Res, DL, DAG);
2704 }
2705 
2706 static SDValue performTruncateCombine(SDNode *N,
2707                                       TargetLowering::DAGCombinerInfo &DCI) {
2708   auto &DAG = DCI.DAG;
2709 
2710   SDValue In = N->getOperand(0);
2711   EVT InVT = In.getValueType();
2712   if (!InVT.isSimple())
2713     return SDValue();
2714 
2715   EVT OutVT = N->getValueType(0);
2716   if (!OutVT.isVector())
2717     return SDValue();
2718 
2719   EVT OutSVT = OutVT.getVectorElementType();
2720   EVT InSVT = InVT.getVectorElementType();
2721   // Currently only cover truncate to v16i8 or v8i16.
2722   if (!((InSVT == MVT::i16 || InSVT == MVT::i32 || InSVT == MVT::i64) &&
2723         (OutSVT == MVT::i8 || OutSVT == MVT::i16) && OutVT.is128BitVector()))
2724     return SDValue();
2725 
2726   SDLoc DL(N);
2727   APInt Mask = APInt::getLowBitsSet(InVT.getScalarSizeInBits(),
2728                                     OutVT.getScalarSizeInBits());
2729   In = DAG.getNode(ISD::AND, DL, InVT, In, DAG.getConstant(Mask, DL, InVT));
2730   return truncateVectorWithNARROW(OutVT, In, DL, DAG);
2731 }
2732 
2733 SDValue
2734 WebAssemblyTargetLowering::PerformDAGCombine(SDNode *N,
2735                                              DAGCombinerInfo &DCI) const {
2736   switch (N->getOpcode()) {
2737   default:
2738     return SDValue();
2739   case ISD::VECTOR_SHUFFLE:
2740     return performVECTOR_SHUFFLECombine(N, DCI);
2741   case ISD::SIGN_EXTEND:
2742   case ISD::ZERO_EXTEND:
2743     return performVectorExtendCombine(N, DCI);
2744   case ISD::FP_TO_SINT_SAT:
2745   case ISD::FP_TO_UINT_SAT:
2746   case ISD::FP_ROUND:
2747   case ISD::CONCAT_VECTORS:
2748     return performVectorTruncZeroCombine(N, DCI);
2749   case ISD::TRUNCATE:
2750     return performTruncateCombine(N, DCI);
2751   }
2752 }
2753