blob: c056e1af588686086b491aa2a03ff7ac8a9acfab [file] [log] [blame]
Dan Gohman10e730a2015-06-29 23:51:55 +00001//=- WebAssemblyISelLowering.cpp - WebAssembly DAG Lowering Implementation -==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011/// This file implements the WebAssemblyTargetLowering class.
Dan Gohman10e730a2015-06-29 23:51:55 +000012///
13//===----------------------------------------------------------------------===//
14
15#include "WebAssemblyISelLowering.h"
16#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
17#include "WebAssemblyMachineFunctionInfo.h"
18#include "WebAssemblySubtarget.h"
19#include "WebAssemblyTargetMachine.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000020#include "llvm/CodeGen/Analysis.h"
JF Bastienaf111db2015-08-24 22:16:48 +000021#include "llvm/CodeGen/CallingConvLower.h"
Dan Gohmancdd48b82017-11-28 01:13:40 +000022#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohman950a13c2015-09-16 16:51:30 +000023#include "llvm/CodeGen/MachineJumpTableInfo.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
25#include "llvm/CodeGen/SelectionDAG.h"
Oliver Stannard02fa1c82016-01-28 13:19:47 +000026#include "llvm/IR/DiagnosticInfo.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000027#include "llvm/IR/DiagnosticPrinter.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000028#include "llvm/IR/Function.h"
29#include "llvm/IR/Intrinsics.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000030#include "llvm/Support/Debug.h"
31#include "llvm/Support/ErrorHandling.h"
32#include "llvm/Support/raw_ostream.h"
33#include "llvm/Target/TargetOptions.h"
34using namespace llvm;
35
36#define DEBUG_TYPE "wasm-lower"
37
Heejin Ahn5831e9c2018-08-09 23:58:51 +000038// Emit proposed instructions that may not have been implemented in engines
39cl::opt<bool> EnableUnimplementedWasmSIMDInstrs(
40 "wasm-enable-unimplemented-simd",
41 cl::desc("Emit potentially-unimplemented WebAssembly SIMD instructions"),
42 cl::init(false));
43
Dan Gohman10e730a2015-06-29 23:51:55 +000044WebAssemblyTargetLowering::WebAssemblyTargetLowering(
45 const TargetMachine &TM, const WebAssemblySubtarget &STI)
Dan Gohmanbfaf7e12015-07-02 21:36:25 +000046 : TargetLowering(TM), Subtarget(&STI) {
JF Bastienaf111db2015-08-24 22:16:48 +000047 auto MVTPtr = Subtarget->hasAddr64() ? MVT::i64 : MVT::i32;
48
JF Bastien71d29ac2015-08-12 17:53:29 +000049 // Booleans always contain 0 or 1.
50 setBooleanContents(ZeroOrOneBooleanContent);
Thomas Lively5ea17d42018-10-20 01:35:23 +000051 // Except in SIMD vectors
52 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Dan Gohmanbfaf7e12015-07-02 21:36:25 +000053 // WebAssembly does not produce floating-point exceptions on normal floating
54 // point operations.
55 setHasFloatingPointExceptions(false);
Dan Gohman489abd72015-07-07 22:38:06 +000056 // We don't know the microarchitecture here, so just reduce register pressure.
57 setSchedulingPreference(Sched::RegPressure);
JF Bastienb9073fb2015-07-22 21:28:15 +000058 // Tell ISel that we have a stack pointer.
59 setStackPointerRegisterToSaveRestore(
60 Subtarget->hasAddr64() ? WebAssembly::SP64 : WebAssembly::SP32);
61 // Set up the register classes.
Dan Gohmand0bf9812015-09-26 01:09:44 +000062 addRegisterClass(MVT::i32, &WebAssembly::I32RegClass);
63 addRegisterClass(MVT::i64, &WebAssembly::I64RegClass);
64 addRegisterClass(MVT::f32, &WebAssembly::F32RegClass);
65 addRegisterClass(MVT::f64, &WebAssembly::F64RegClass);
Derek Schuff39bf39f2016-08-02 23:16:09 +000066 if (Subtarget->hasSIMD128()) {
67 addRegisterClass(MVT::v16i8, &WebAssembly::V128RegClass);
68 addRegisterClass(MVT::v8i16, &WebAssembly::V128RegClass);
69 addRegisterClass(MVT::v4i32, &WebAssembly::V128RegClass);
70 addRegisterClass(MVT::v4f32, &WebAssembly::V128RegClass);
Heejin Ahn5831e9c2018-08-09 23:58:51 +000071 if (EnableUnimplementedWasmSIMDInstrs) {
72 addRegisterClass(MVT::v2i64, &WebAssembly::V128RegClass);
73 addRegisterClass(MVT::v2f64, &WebAssembly::V128RegClass);
74 }
Derek Schuff39bf39f2016-08-02 23:16:09 +000075 }
JF Bastienb9073fb2015-07-22 21:28:15 +000076 // Compute derived properties from the register classes.
77 computeRegisterProperties(Subtarget->getRegisterInfo());
78
JF Bastienaf111db2015-08-24 22:16:48 +000079 setOperationAction(ISD::GlobalAddress, MVTPtr, Custom);
Dan Gohman2c8fe6a2015-11-25 16:44:29 +000080 setOperationAction(ISD::ExternalSymbol, MVTPtr, Custom);
Dan Gohman950a13c2015-09-16 16:51:30 +000081 setOperationAction(ISD::JumpTable, MVTPtr, Custom);
Derek Schuff51699a82016-02-12 22:56:03 +000082 setOperationAction(ISD::BlockAddress, MVTPtr, Custom);
83 setOperationAction(ISD::BRIND, MVT::Other, Custom);
JF Bastienaf111db2015-08-24 22:16:48 +000084
Dan Gohman35bfb242015-12-04 23:22:35 +000085 // Take the default expansion for va_arg, va_copy, and va_end. There is no
86 // default action for va_start, so we do that custom.
87 setOperationAction(ISD::VASTART, MVT::Other, Custom);
88 setOperationAction(ISD::VAARG, MVT::Other, Expand);
89 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
90 setOperationAction(ISD::VAEND, MVT::Other, Expand);
91
Thomas Livelyebd4c902018-09-12 17:56:00 +000092 for (auto T : {MVT::f32, MVT::f64, MVT::v4f32, MVT::v2f64}) {
JF Bastienda06bce2015-08-11 21:02:46 +000093 // Don't expand the floating-point types to constant pools.
94 setOperationAction(ISD::ConstantFP, T, Legal);
95 // Expand floating-point comparisons.
96 for (auto CC : {ISD::SETO, ISD::SETUO, ISD::SETUEQ, ISD::SETONE,
97 ISD::SETULT, ISD::SETULE, ISD::SETUGT, ISD::SETUGE})
98 setCondCodeAction(CC, T, Expand);
Dan Gohman32907a62015-08-20 22:57:13 +000099 // Expand floating-point library function operators.
Heejin Ahnf208f632018-09-05 01:27:38 +0000100 for (auto Op :
101 {ISD::FSIN, ISD::FCOS, ISD::FSINCOS, ISD::FPOW, ISD::FREM, ISD::FMA})
Dan Gohman32907a62015-08-20 22:57:13 +0000102 setOperationAction(Op, T, Expand);
Dan Gohman896e53f2015-08-24 18:23:13 +0000103 // Note supported floating-point library function operators that otherwise
104 // default to expand.
Dan Gohman7a6b9822015-11-29 22:32:02 +0000105 for (auto Op :
106 {ISD::FCEIL, ISD::FFLOOR, ISD::FTRUNC, ISD::FNEARBYINT, ISD::FRINT})
Dan Gohman896e53f2015-08-24 18:23:13 +0000107 setOperationAction(Op, T, Legal);
Thomas Lively30f1d692018-10-24 22:49:55 +0000108 // Support minimum and maximum, which otherwise default to expand.
109 setOperationAction(ISD::FMINIMUM, T, Legal);
110 setOperationAction(ISD::FMAXIMUM, T, Legal);
Dan Gohmana63e8eb2017-02-22 16:28:00 +0000111 // WebAssembly currently has no builtin f16 support.
112 setOperationAction(ISD::FP16_TO_FP, T, Expand);
113 setOperationAction(ISD::FP_TO_FP16, T, Expand);
114 setLoadExtAction(ISD::EXTLOAD, T, MVT::f16, Expand);
115 setTruncStoreAction(T, MVT::f16, Expand);
JF Bastienda06bce2015-08-11 21:02:46 +0000116 }
Dan Gohman32907a62015-08-20 22:57:13 +0000117
Thomas Lively0aad98f2018-10-25 19:06:13 +0000118 // Support saturating add for i8x16 and i16x8
119 if (Subtarget->hasSIMD128())
120 for (auto T : {MVT::v16i8, MVT::v8i16})
121 for (auto Op : {ISD::SADDSAT, ISD::UADDSAT})
122 setOperationAction(Op, T, Legal);
123
Dan Gohman32907a62015-08-20 22:57:13 +0000124 for (auto T : {MVT::i32, MVT::i64}) {
125 // Expand unavailable integer operations.
Dan Gohman7a6b9822015-11-29 22:32:02 +0000126 for (auto Op :
Heejin Ahnf208f632018-09-05 01:27:38 +0000127 {ISD::BSWAP, ISD::SMUL_LOHI, ISD::UMUL_LOHI, ISD::MULHS, ISD::MULHU,
128 ISD::SDIVREM, ISD::UDIVREM, ISD::SHL_PARTS, ISD::SRA_PARTS,
129 ISD::SRL_PARTS, ISD::ADDC, ISD::ADDE, ISD::SUBC, ISD::SUBE}) {
Dan Gohman32907a62015-08-20 22:57:13 +0000130 setOperationAction(Op, T, Expand);
131 }
132 }
133
Thomas Lively2ee686d2018-08-22 23:06:27 +0000134 // There is no i64x2.mul instruction
135 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
136
Thomas Livelya0d25812018-09-07 21:54:46 +0000137 // We have custom shuffle lowering to expose the shuffle mask
138 if (Subtarget->hasSIMD128()) {
139 for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v4f32}) {
140 setOperationAction(ISD::VECTOR_SHUFFLE, T, Custom);
141 }
142 if (EnableUnimplementedWasmSIMDInstrs) {
143 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
144 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
145 }
146 }
147
Thomas Lively55735d52018-10-20 01:31:18 +0000148 // Custom lowering to avoid having to emit a wrap for 2xi64 constant shifts
149 if (Subtarget->hasSIMD128() && EnableUnimplementedWasmSIMDInstrs)
150 for (auto Op : {ISD::SHL, ISD::SRA, ISD::SRL})
151 setOperationAction(Op, MVT::v2i64, Custom);
152
Dan Gohman32907a62015-08-20 22:57:13 +0000153 // As a special case, these operators use the type to mean the type to
154 // sign-extend from.
Derek Schuffa519fe52017-09-13 00:29:06 +0000155 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Dan Gohman5d2b9352018-01-19 17:16:24 +0000156 if (!Subtarget->hasSignExt()) {
Derek Schuffa519fe52017-09-13 00:29:06 +0000157 for (auto T : {MVT::i8, MVT::i16, MVT::i32})
158 setOperationAction(ISD::SIGN_EXTEND_INREG, T, Expand);
159 }
Thomas Lively5ea17d42018-10-20 01:35:23 +0000160 for (auto T : MVT::integer_vector_valuetypes())
161 setOperationAction(ISD::SIGN_EXTEND_INREG, T, Expand);
Dan Gohman32907a62015-08-20 22:57:13 +0000162
163 // Dynamic stack allocation: use the default expansion.
164 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
165 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Dan Gohman2683a552015-08-24 22:31:52 +0000166 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVTPtr, Expand);
JF Bastien73ff6af2015-08-31 22:24:11 +0000167
Derek Schuff9769deb2015-12-11 23:49:46 +0000168 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
Derek Schuffaadc89c2016-02-16 18:18:36 +0000169 setOperationAction(ISD::CopyToReg, MVT::Other, Custom);
Derek Schuff9769deb2015-12-11 23:49:46 +0000170
Dan Gohman950a13c2015-09-16 16:51:30 +0000171 // Expand these forms; we pattern-match the forms that we can handle in isel.
172 for (auto T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64})
173 for (auto Op : {ISD::BR_CC, ISD::SELECT_CC})
174 setOperationAction(Op, T, Expand);
175
176 // We have custom switch handling.
177 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
178
JF Bastien73ff6af2015-08-31 22:24:11 +0000179 // WebAssembly doesn't have:
180 // - Floating-point extending loads.
181 // - Floating-point truncating stores.
182 // - i1 extending loads.
Thomas Lively325c9c52018-10-25 01:46:07 +0000183 // - extending/truncating SIMD loads/stores
Dan Gohman60bddf12015-12-10 02:07:53 +0000184 setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand);
JF Bastien73ff6af2015-08-31 22:24:11 +0000185 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
186 for (auto T : MVT::integer_valuetypes())
187 for (auto Ext : {ISD::EXTLOAD, ISD::ZEXTLOAD, ISD::SEXTLOAD})
188 setLoadExtAction(Ext, T, MVT::i1, Promote);
Thomas Lively325c9c52018-10-25 01:46:07 +0000189 if (Subtarget->hasSIMD128()) {
190 for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v2i64, MVT::v4f32,
191 MVT::v2f64}) {
192 for (auto MemT : MVT::vector_valuetypes()) {
193 if (MVT(T) != MemT) {
194 setTruncStoreAction(T, MemT, Expand);
195 for (auto Ext : {ISD::EXTLOAD, ISD::ZEXTLOAD, ISD::SEXTLOAD})
196 setLoadExtAction(Ext, T, MemT, Expand);
197 }
198 }
199 }
200 }
Derek Schuffffa143c2015-11-10 00:30:57 +0000201
202 // Trap lowers to wasm unreachable
203 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Derek Schuff18ba1922017-08-30 18:07:45 +0000204
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000205 // Exception handling intrinsics
206 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
207
Derek Schuff18ba1922017-08-30 18:07:45 +0000208 setMaxAtomicSizeInBitsSupported(64);
Dan Gohmanbfaf7e12015-07-02 21:36:25 +0000209}
Dan Gohman10e730a2015-06-29 23:51:55 +0000210
Heejin Ahne8653bb2018-08-07 00:22:22 +0000211TargetLowering::AtomicExpansionKind
212WebAssemblyTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
213 // We have wasm instructions for these
214 switch (AI->getOperation()) {
215 case AtomicRMWInst::Add:
216 case AtomicRMWInst::Sub:
217 case AtomicRMWInst::And:
218 case AtomicRMWInst::Or:
219 case AtomicRMWInst::Xor:
220 case AtomicRMWInst::Xchg:
221 return AtomicExpansionKind::None;
222 default:
223 break;
224 }
225 return AtomicExpansionKind::CmpXChg;
226}
227
Dan Gohman7b634842015-08-24 18:44:37 +0000228FastISel *WebAssemblyTargetLowering::createFastISel(
229 FunctionLoweringInfo &FuncInfo, const TargetLibraryInfo *LibInfo) const {
230 return WebAssembly::createFastISel(FuncInfo, LibInfo);
231}
232
JF Bastienaf111db2015-08-24 22:16:48 +0000233bool WebAssemblyTargetLowering::isOffsetFoldingLegal(
Dan Gohman7a6b9822015-11-29 22:32:02 +0000234 const GlobalAddressSDNode * /*GA*/) const {
Dan Gohmana4b710a2015-12-06 19:33:32 +0000235 // All offsets can be folded.
236 return true;
JF Bastienaf111db2015-08-24 22:16:48 +0000237}
238
Dan Gohman7a6b9822015-11-29 22:32:02 +0000239MVT WebAssemblyTargetLowering::getScalarShiftAmountTy(const DataLayout & /*DL*/,
JF Bastienfda53372015-08-03 00:00:11 +0000240 EVT VT) const {
Dan Gohmana8483752015-12-10 00:26:26 +0000241 unsigned BitWidth = NextPowerOf2(VT.getSizeInBits() - 1);
Heejin Ahnf208f632018-09-05 01:27:38 +0000242 if (BitWidth > 1 && BitWidth < 8)
243 BitWidth = 8;
Dan Gohman41729532015-12-16 23:25:51 +0000244
245 if (BitWidth > 64) {
Dan Gohmana01e8bd2016-05-14 02:15:47 +0000246 // The shift will be lowered to a libcall, and compiler-rt libcalls expect
247 // the count to be an i32.
248 BitWidth = 32;
Dan Gohman41729532015-12-16 23:25:51 +0000249 assert(BitWidth >= Log2_32_Ceil(VT.getSizeInBits()) &&
Dan Gohmana01e8bd2016-05-14 02:15:47 +0000250 "32-bit shift counts ought to be enough for anyone");
Dan Gohman41729532015-12-16 23:25:51 +0000251 }
252
Dan Gohmana8483752015-12-10 00:26:26 +0000253 MVT Result = MVT::getIntegerVT(BitWidth);
254 assert(Result != MVT::INVALID_SIMPLE_VALUE_TYPE &&
255 "Unable to represent scalar shift amount type");
256 return Result;
JF Bastienfda53372015-08-03 00:00:11 +0000257}
258
Dan Gohmancdd48b82017-11-28 01:13:40 +0000259// Lower an fp-to-int conversion operator from the LLVM opcode, which has an
260// undefined result on invalid/overflow, to the WebAssembly opcode, which
261// traps on invalid/overflow.
Heejin Ahnf208f632018-09-05 01:27:38 +0000262static MachineBasicBlock *LowerFPToInt(MachineInstr &MI, DebugLoc DL,
263 MachineBasicBlock *BB,
264 const TargetInstrInfo &TII,
265 bool IsUnsigned, bool Int64,
266 bool Float64, unsigned LoweredOpcode) {
Dan Gohmancdd48b82017-11-28 01:13:40 +0000267 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
268
269 unsigned OutReg = MI.getOperand(0).getReg();
270 unsigned InReg = MI.getOperand(1).getReg();
271
272 unsigned Abs = Float64 ? WebAssembly::ABS_F64 : WebAssembly::ABS_F32;
273 unsigned FConst = Float64 ? WebAssembly::CONST_F64 : WebAssembly::CONST_F32;
274 unsigned LT = Float64 ? WebAssembly::LT_F64 : WebAssembly::LT_F32;
Dan Gohman580c1022017-11-29 20:20:11 +0000275 unsigned GE = Float64 ? WebAssembly::GE_F64 : WebAssembly::GE_F32;
Dan Gohmancdd48b82017-11-28 01:13:40 +0000276 unsigned IConst = Int64 ? WebAssembly::CONST_I64 : WebAssembly::CONST_I32;
Dan Gohman580c1022017-11-29 20:20:11 +0000277 unsigned Eqz = WebAssembly::EQZ_I32;
278 unsigned And = WebAssembly::AND_I32;
Dan Gohmancdd48b82017-11-28 01:13:40 +0000279 int64_t Limit = Int64 ? INT64_MIN : INT32_MIN;
280 int64_t Substitute = IsUnsigned ? 0 : Limit;
281 double CmpVal = IsUnsigned ? -(double)Limit * 2.0 : -(double)Limit;
David Blaikie21109242017-12-15 23:52:06 +0000282 auto &Context = BB->getParent()->getFunction().getContext();
Dan Gohmancdd48b82017-11-28 01:13:40 +0000283 Type *Ty = Float64 ? Type::getDoubleTy(Context) : Type::getFloatTy(Context);
284
285 const BasicBlock *LLVM_BB = BB->getBasicBlock();
286 MachineFunction *F = BB->getParent();
287 MachineBasicBlock *TrueMBB = F->CreateMachineBasicBlock(LLVM_BB);
288 MachineBasicBlock *FalseMBB = F->CreateMachineBasicBlock(LLVM_BB);
289 MachineBasicBlock *DoneMBB = F->CreateMachineBasicBlock(LLVM_BB);
290
291 MachineFunction::iterator It = ++BB->getIterator();
292 F->insert(It, FalseMBB);
293 F->insert(It, TrueMBB);
294 F->insert(It, DoneMBB);
295
296 // Transfer the remainder of BB and its successor edges to DoneMBB.
297 DoneMBB->splice(DoneMBB->begin(), BB,
Heejin Ahnf208f632018-09-05 01:27:38 +0000298 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohmancdd48b82017-11-28 01:13:40 +0000299 DoneMBB->transferSuccessorsAndUpdatePHIs(BB);
300
301 BB->addSuccessor(TrueMBB);
302 BB->addSuccessor(FalseMBB);
303 TrueMBB->addSuccessor(DoneMBB);
304 FalseMBB->addSuccessor(DoneMBB);
305
Dan Gohman580c1022017-11-29 20:20:11 +0000306 unsigned Tmp0, Tmp1, CmpReg, EqzReg, FalseReg, TrueReg;
Dan Gohmancdd48b82017-11-28 01:13:40 +0000307 Tmp0 = MRI.createVirtualRegister(MRI.getRegClass(InReg));
308 Tmp1 = MRI.createVirtualRegister(MRI.getRegClass(InReg));
Dan Gohman580c1022017-11-29 20:20:11 +0000309 CmpReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
310 EqzReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
311 FalseReg = MRI.createVirtualRegister(MRI.getRegClass(OutReg));
312 TrueReg = MRI.createVirtualRegister(MRI.getRegClass(OutReg));
Dan Gohmancdd48b82017-11-28 01:13:40 +0000313
314 MI.eraseFromParent();
Dan Gohman580c1022017-11-29 20:20:11 +0000315 // For signed numbers, we can do a single comparison to determine whether
316 // fabs(x) is within range.
Dan Gohmancdd48b82017-11-28 01:13:40 +0000317 if (IsUnsigned) {
318 Tmp0 = InReg;
319 } else {
Heejin Ahnf208f632018-09-05 01:27:38 +0000320 BuildMI(BB, DL, TII.get(Abs), Tmp0).addReg(InReg);
Dan Gohmancdd48b82017-11-28 01:13:40 +0000321 }
322 BuildMI(BB, DL, TII.get(FConst), Tmp1)
323 .addFPImm(cast<ConstantFP>(ConstantFP::get(Ty, CmpVal)));
Heejin Ahnf208f632018-09-05 01:27:38 +0000324 BuildMI(BB, DL, TII.get(LT), CmpReg).addReg(Tmp0).addReg(Tmp1);
Dan Gohman580c1022017-11-29 20:20:11 +0000325
326 // For unsigned numbers, we have to do a separate comparison with zero.
327 if (IsUnsigned) {
328 Tmp1 = MRI.createVirtualRegister(MRI.getRegClass(InReg));
Heejin Ahnf208f632018-09-05 01:27:38 +0000329 unsigned SecondCmpReg =
330 MRI.createVirtualRegister(&WebAssembly::I32RegClass);
Dan Gohman580c1022017-11-29 20:20:11 +0000331 unsigned AndReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
332 BuildMI(BB, DL, TII.get(FConst), Tmp1)
333 .addFPImm(cast<ConstantFP>(ConstantFP::get(Ty, 0.0)));
Heejin Ahnf208f632018-09-05 01:27:38 +0000334 BuildMI(BB, DL, TII.get(GE), SecondCmpReg).addReg(Tmp0).addReg(Tmp1);
335 BuildMI(BB, DL, TII.get(And), AndReg).addReg(CmpReg).addReg(SecondCmpReg);
Dan Gohman580c1022017-11-29 20:20:11 +0000336 CmpReg = AndReg;
337 }
338
Heejin Ahnf208f632018-09-05 01:27:38 +0000339 BuildMI(BB, DL, TII.get(Eqz), EqzReg).addReg(CmpReg);
Dan Gohman580c1022017-11-29 20:20:11 +0000340
341 // Create the CFG diamond to select between doing the conversion or using
342 // the substitute value.
Heejin Ahnf208f632018-09-05 01:27:38 +0000343 BuildMI(BB, DL, TII.get(WebAssembly::BR_IF)).addMBB(TrueMBB).addReg(EqzReg);
344 BuildMI(FalseMBB, DL, TII.get(LoweredOpcode), FalseReg).addReg(InReg);
345 BuildMI(FalseMBB, DL, TII.get(WebAssembly::BR)).addMBB(DoneMBB);
346 BuildMI(TrueMBB, DL, TII.get(IConst), TrueReg).addImm(Substitute);
Dan Gohmancdd48b82017-11-28 01:13:40 +0000347 BuildMI(*DoneMBB, DoneMBB->begin(), DL, TII.get(TargetOpcode::PHI), OutReg)
Dan Gohman580c1022017-11-29 20:20:11 +0000348 .addReg(FalseReg)
Dan Gohmancdd48b82017-11-28 01:13:40 +0000349 .addMBB(FalseMBB)
Dan Gohman580c1022017-11-29 20:20:11 +0000350 .addReg(TrueReg)
Dan Gohmancdd48b82017-11-28 01:13:40 +0000351 .addMBB(TrueMBB);
352
353 return DoneMBB;
354}
355
Heejin Ahnf208f632018-09-05 01:27:38 +0000356MachineBasicBlock *WebAssemblyTargetLowering::EmitInstrWithCustomInserter(
357 MachineInstr &MI, MachineBasicBlock *BB) const {
Dan Gohmancdd48b82017-11-28 01:13:40 +0000358 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
359 DebugLoc DL = MI.getDebugLoc();
360
361 switch (MI.getOpcode()) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000362 default:
363 llvm_unreachable("Unexpected instr type to insert");
Dan Gohmancdd48b82017-11-28 01:13:40 +0000364 case WebAssembly::FP_TO_SINT_I32_F32:
365 return LowerFPToInt(MI, DL, BB, TII, false, false, false,
366 WebAssembly::I32_TRUNC_S_F32);
367 case WebAssembly::FP_TO_UINT_I32_F32:
368 return LowerFPToInt(MI, DL, BB, TII, true, false, false,
369 WebAssembly::I32_TRUNC_U_F32);
370 case WebAssembly::FP_TO_SINT_I64_F32:
371 return LowerFPToInt(MI, DL, BB, TII, false, true, false,
372 WebAssembly::I64_TRUNC_S_F32);
373 case WebAssembly::FP_TO_UINT_I64_F32:
374 return LowerFPToInt(MI, DL, BB, TII, true, true, false,
375 WebAssembly::I64_TRUNC_U_F32);
376 case WebAssembly::FP_TO_SINT_I32_F64:
377 return LowerFPToInt(MI, DL, BB, TII, false, false, true,
378 WebAssembly::I32_TRUNC_S_F64);
379 case WebAssembly::FP_TO_UINT_I32_F64:
380 return LowerFPToInt(MI, DL, BB, TII, true, false, true,
381 WebAssembly::I32_TRUNC_U_F64);
382 case WebAssembly::FP_TO_SINT_I64_F64:
383 return LowerFPToInt(MI, DL, BB, TII, false, true, true,
384 WebAssembly::I64_TRUNC_S_F64);
385 case WebAssembly::FP_TO_UINT_I64_F64:
386 return LowerFPToInt(MI, DL, BB, TII, true, true, true,
387 WebAssembly::I64_TRUNC_U_F64);
Heejin Ahnf208f632018-09-05 01:27:38 +0000388 llvm_unreachable("Unexpected instruction to emit with custom inserter");
Dan Gohmancdd48b82017-11-28 01:13:40 +0000389 }
390}
391
Heejin Ahnf208f632018-09-05 01:27:38 +0000392const char *
393WebAssemblyTargetLowering::getTargetNodeName(unsigned Opcode) const {
JF Bastien480c8402015-08-11 20:13:18 +0000394 switch (static_cast<WebAssemblyISD::NodeType>(Opcode)) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000395 case WebAssemblyISD::FIRST_NUMBER:
396 break;
397#define HANDLE_NODETYPE(NODE) \
398 case WebAssemblyISD::NODE: \
JF Bastienaf111db2015-08-24 22:16:48 +0000399 return "WebAssemblyISD::" #NODE;
400#include "WebAssemblyISD.def"
401#undef HANDLE_NODETYPE
JF Bastien480c8402015-08-11 20:13:18 +0000402 }
403 return nullptr;
404}
405
Dan Gohmanf19ed562015-11-13 01:42:29 +0000406std::pair<unsigned, const TargetRegisterClass *>
407WebAssemblyTargetLowering::getRegForInlineAsmConstraint(
408 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
409 // First, see if this is a constraint that directly corresponds to a
410 // WebAssembly register class.
411 if (Constraint.size() == 1) {
412 switch (Constraint[0]) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000413 case 'r':
414 assert(VT != MVT::iPTR && "Pointer MVT not expected here");
415 if (Subtarget->hasSIMD128() && VT.isVector()) {
416 if (VT.getSizeInBits() == 128)
417 return std::make_pair(0U, &WebAssembly::V128RegClass);
418 }
419 if (VT.isInteger() && !VT.isVector()) {
420 if (VT.getSizeInBits() <= 32)
421 return std::make_pair(0U, &WebAssembly::I32RegClass);
422 if (VT.getSizeInBits() <= 64)
423 return std::make_pair(0U, &WebAssembly::I64RegClass);
424 }
425 break;
426 default:
427 break;
Dan Gohmanf19ed562015-11-13 01:42:29 +0000428 }
429 }
430
431 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
432}
433
Dan Gohman3192ddf2015-11-19 23:04:59 +0000434bool WebAssemblyTargetLowering::isCheapToSpeculateCttz() const {
435 // Assume ctz is a relatively cheap operation.
436 return true;
437}
438
439bool WebAssemblyTargetLowering::isCheapToSpeculateCtlz() const {
440 // Assume clz is a relatively cheap operation.
441 return true;
442}
443
Dan Gohman4b9d7912015-12-15 22:01:29 +0000444bool WebAssemblyTargetLowering::isLegalAddressingMode(const DataLayout &DL,
445 const AddrMode &AM,
Heejin Ahnf208f632018-09-05 01:27:38 +0000446 Type *Ty, unsigned AS,
Jonas Paulsson024e3192017-07-21 11:59:37 +0000447 Instruction *I) const {
Dan Gohman4b9d7912015-12-15 22:01:29 +0000448 // WebAssembly offsets are added as unsigned without wrapping. The
449 // isLegalAddressingMode gives us no way to determine if wrapping could be
450 // happening, so we approximate this by accepting only non-negative offsets.
Heejin Ahnf208f632018-09-05 01:27:38 +0000451 if (AM.BaseOffs < 0)
452 return false;
Dan Gohman4b9d7912015-12-15 22:01:29 +0000453
454 // WebAssembly has no scale register operands.
Heejin Ahnf208f632018-09-05 01:27:38 +0000455 if (AM.Scale != 0)
456 return false;
Dan Gohman4b9d7912015-12-15 22:01:29 +0000457
458 // Everything else is legal.
459 return true;
460}
461
Dan Gohmanbb372242016-01-26 03:39:31 +0000462bool WebAssemblyTargetLowering::allowsMisalignedMemoryAccesses(
Derek Schuff3f063292016-02-11 20:57:09 +0000463 EVT /*VT*/, unsigned /*AddrSpace*/, unsigned /*Align*/, bool *Fast) const {
Dan Gohmanbb372242016-01-26 03:39:31 +0000464 // WebAssembly supports unaligned accesses, though it should be declared
465 // with the p2align attribute on loads and stores which do so, and there
466 // may be a performance impact. We tell LLVM they're "fast" because
Dan Gohmanfb619e92016-01-26 14:55:17 +0000467 // for the kinds of things that LLVM uses this for (merging adjacent stores
Dan Gohmanbb372242016-01-26 03:39:31 +0000468 // of constants, etc.), WebAssembly implementations will either want the
469 // unaligned access or they'll split anyway.
Heejin Ahnf208f632018-09-05 01:27:38 +0000470 if (Fast)
471 *Fast = true;
Dan Gohmanbb372242016-01-26 03:39:31 +0000472 return true;
473}
474
Reid Klecknerb5180542017-03-21 16:57:19 +0000475bool WebAssemblyTargetLowering::isIntDivCheap(EVT VT,
476 AttributeList Attr) const {
Dan Gohmanb4c3c382016-05-18 14:29:42 +0000477 // The current thinking is that wasm engines will perform this optimization,
478 // so we can save on code size.
479 return true;
480}
481
Simon Pilgrim99f70162018-06-28 17:27:09 +0000482EVT WebAssemblyTargetLowering::getSetCCResultType(const DataLayout &DL,
483 LLVMContext &C,
484 EVT VT) const {
485 if (VT.isVector())
486 return VT.changeVectorElementTypeToInteger();
487
488 return TargetLowering::getSetCCResultType(DL, C, VT);
489}
490
Heejin Ahn4128cb02018-08-02 21:44:24 +0000491bool WebAssemblyTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
492 const CallInst &I,
493 MachineFunction &MF,
494 unsigned Intrinsic) const {
495 switch (Intrinsic) {
496 case Intrinsic::wasm_atomic_notify:
497 Info.opc = ISD::INTRINSIC_W_CHAIN;
498 Info.memVT = MVT::i32;
499 Info.ptrVal = I.getArgOperand(0);
500 Info.offset = 0;
501 Info.align = 4;
502 // atomic.notify instruction does not really load the memory specified with
503 // this argument, but MachineMemOperand should either be load or store, so
504 // we set this to a load.
505 // FIXME Volatile isn't really correct, but currently all LLVM atomic
506 // instructions are treated as volatiles in the backend, so we should be
507 // consistent. The same applies for wasm_atomic_wait intrinsics too.
508 Info.flags = MachineMemOperand::MOVolatile | MachineMemOperand::MOLoad;
509 return true;
510 case Intrinsic::wasm_atomic_wait_i32:
511 Info.opc = ISD::INTRINSIC_W_CHAIN;
512 Info.memVT = MVT::i32;
513 Info.ptrVal = I.getArgOperand(0);
514 Info.offset = 0;
515 Info.align = 4;
516 Info.flags = MachineMemOperand::MOVolatile | MachineMemOperand::MOLoad;
517 return true;
518 case Intrinsic::wasm_atomic_wait_i64:
519 Info.opc = ISD::INTRINSIC_W_CHAIN;
520 Info.memVT = MVT::i64;
521 Info.ptrVal = I.getArgOperand(0);
522 Info.offset = 0;
523 Info.align = 8;
524 Info.flags = MachineMemOperand::MOVolatile | MachineMemOperand::MOLoad;
525 return true;
526 default:
527 return false;
528 }
529}
530
Dan Gohman10e730a2015-06-29 23:51:55 +0000531//===----------------------------------------------------------------------===//
532// WebAssembly Lowering private implementation.
533//===----------------------------------------------------------------------===//
534
535//===----------------------------------------------------------------------===//
536// Lowering Code
537//===----------------------------------------------------------------------===//
538
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000539static void fail(const SDLoc &DL, SelectionDAG &DAG, const char *msg) {
JF Bastienb9073fb2015-07-22 21:28:15 +0000540 MachineFunction &MF = DAG.getMachineFunction();
541 DAG.getContext()->diagnose(
David Blaikie21109242017-12-15 23:52:06 +0000542 DiagnosticInfoUnsupported(MF.getFunction(), msg, DL.getDebugLoc()));
JF Bastienb9073fb2015-07-22 21:28:15 +0000543}
544
Dan Gohman85dbdda2015-12-04 17:16:07 +0000545// Test whether the given calling convention is supported.
Dan Gohmana3f5ce52015-12-04 17:18:32 +0000546static bool CallingConvSupported(CallingConv::ID CallConv) {
Dan Gohman85dbdda2015-12-04 17:16:07 +0000547 // We currently support the language-independent target-independent
Dan Gohman1ce2b1a2015-12-04 18:27:03 +0000548 // conventions. We don't yet have a way to annotate calls with properties like
549 // "cold", and we don't have any call-clobbered registers, so these are mostly
550 // all handled the same.
Dan Gohmana3f5ce52015-12-04 17:18:32 +0000551 return CallConv == CallingConv::C || CallConv == CallingConv::Fast ||
Dan Gohman1ce2b1a2015-12-04 18:27:03 +0000552 CallConv == CallingConv::Cold ||
553 CallConv == CallingConv::PreserveMost ||
554 CallConv == CallingConv::PreserveAll ||
555 CallConv == CallingConv::CXX_FAST_TLS;
Dan Gohman85dbdda2015-12-04 17:16:07 +0000556}
557
Heejin Ahnf208f632018-09-05 01:27:38 +0000558SDValue
559WebAssemblyTargetLowering::LowerCall(CallLoweringInfo &CLI,
560 SmallVectorImpl<SDValue> &InVals) const {
JF Bastiend8a9d662015-08-24 21:59:51 +0000561 SelectionDAG &DAG = CLI.DAG;
562 SDLoc DL = CLI.DL;
563 SDValue Chain = CLI.Chain;
564 SDValue Callee = CLI.Callee;
565 MachineFunction &MF = DAG.getMachineFunction();
Derek Schuff992d83f2016-02-10 20:14:15 +0000566 auto Layout = MF.getDataLayout();
JF Bastiend8a9d662015-08-24 21:59:51 +0000567
568 CallingConv::ID CallConv = CLI.CallConv;
Dan Gohman85dbdda2015-12-04 17:16:07 +0000569 if (!CallingConvSupported(CallConv))
Dan Gohman9cc692b2015-10-02 20:54:23 +0000570 fail(DL, DAG,
571 "WebAssembly doesn't support language-specific or target-specific "
572 "calling conventions yet");
JF Bastiend8a9d662015-08-24 21:59:51 +0000573 if (CLI.IsPatchPoint)
574 fail(DL, DAG, "WebAssembly doesn't support patch point yet");
575
Dan Gohman9cc692b2015-10-02 20:54:23 +0000576 // WebAssembly doesn't currently support explicit tail calls. If they are
577 // required, fail. Otherwise, just disable them.
578 if ((CallConv == CallingConv::Fast && CLI.IsTailCall &&
579 MF.getTarget().Options.GuaranteedTailCallOpt) ||
Peter Collingbourne081ffe22017-07-26 19:15:29 +0000580 (CLI.CS && CLI.CS.isMustTailCall()))
Dan Gohman9cc692b2015-10-02 20:54:23 +0000581 fail(DL, DAG, "WebAssembly doesn't support tail call yet");
582 CLI.IsTailCall = false;
583
JF Bastiend8a9d662015-08-24 21:59:51 +0000584 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Dan Gohmane590b332015-09-09 01:52:45 +0000585 if (Ins.size() > 1)
586 fail(DL, DAG, "WebAssembly doesn't support more than 1 returned value yet");
587
Dan Gohman2d822e72015-12-04 17:12:52 +0000588 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
Derek Schuff4dd67782016-01-27 21:17:39 +0000589 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
Dan Gohman910ba332018-06-26 03:18:38 +0000590 unsigned NumFixedArgs = 0;
Derek Schuff4dd67782016-01-27 21:17:39 +0000591 for (unsigned i = 0; i < Outs.size(); ++i) {
592 const ISD::OutputArg &Out = Outs[i];
593 SDValue &OutVal = OutVals[i];
Dan Gohman7935fa32015-12-10 00:22:40 +0000594 if (Out.Flags.isNest())
595 fail(DL, DAG, "WebAssembly hasn't implemented nest arguments");
Dan Gohman2d822e72015-12-04 17:12:52 +0000596 if (Out.Flags.isInAlloca())
Dan Gohman7935fa32015-12-10 00:22:40 +0000597 fail(DL, DAG, "WebAssembly hasn't implemented inalloca arguments");
Dan Gohman2d822e72015-12-04 17:12:52 +0000598 if (Out.Flags.isInConsecutiveRegs())
Dan Gohman7935fa32015-12-10 00:22:40 +0000599 fail(DL, DAG, "WebAssembly hasn't implemented cons regs arguments");
Dan Gohman2d822e72015-12-04 17:12:52 +0000600 if (Out.Flags.isInConsecutiveRegsLast())
Dan Gohman7935fa32015-12-10 00:22:40 +0000601 fail(DL, DAG, "WebAssembly hasn't implemented cons regs last arguments");
Dan Gohmana6771b32016-02-12 21:30:18 +0000602 if (Out.Flags.isByVal() && Out.Flags.getByValSize() != 0) {
Matthias Braun941a7052016-07-28 18:40:00 +0000603 auto &MFI = MF.getFrameInfo();
604 int FI = MFI.CreateStackObject(Out.Flags.getByValSize(),
605 Out.Flags.getByValAlign(),
606 /*isSS=*/false);
Derek Schuff4dd67782016-01-27 21:17:39 +0000607 SDValue SizeNode =
608 DAG.getConstant(Out.Flags.getByValSize(), DL, MVT::i32);
Derek Schuff992d83f2016-02-10 20:14:15 +0000609 SDValue FINode = DAG.getFrameIndex(FI, getPointerTy(Layout));
Derek Schuff4dd67782016-01-27 21:17:39 +0000610 Chain = DAG.getMemcpy(
611 Chain, DL, FINode, OutVal, SizeNode, Out.Flags.getByValAlign(),
Dan Gohman476ffce2016-02-17 01:43:37 +0000612 /*isVolatile*/ false, /*AlwaysInline=*/false,
Derek Schuff4dd67782016-01-27 21:17:39 +0000613 /*isTailCall*/ false, MachinePointerInfo(), MachinePointerInfo());
614 OutVal = FINode;
615 }
Dan Gohman910ba332018-06-26 03:18:38 +0000616 // Count the number of fixed args *after* legalization.
617 NumFixedArgs += Out.IsFixed;
Dan Gohman2d822e72015-12-04 17:12:52 +0000618 }
619
JF Bastiend8a9d662015-08-24 21:59:51 +0000620 bool IsVarArg = CLI.IsVarArg;
Derek Schuff992d83f2016-02-10 20:14:15 +0000621 auto PtrVT = getPointerTy(Layout);
Dan Gohmane590b332015-09-09 01:52:45 +0000622
JF Bastiend8a9d662015-08-24 21:59:51 +0000623 // Analyze operands of the call, assigning locations to each operand.
624 SmallVector<CCValAssign, 16> ArgLocs;
625 CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
JF Bastiend8a9d662015-08-24 21:59:51 +0000626
Dan Gohman35bfb242015-12-04 23:22:35 +0000627 if (IsVarArg) {
Derek Schuff27501e22016-02-10 19:51:04 +0000628 // Outgoing non-fixed arguments are placed in a buffer. First
629 // compute their offsets and the total amount of buffer space needed.
Dan Gohman35bfb242015-12-04 23:22:35 +0000630 for (SDValue Arg :
631 make_range(OutVals.begin() + NumFixedArgs, OutVals.end())) {
632 EVT VT = Arg.getValueType();
633 assert(VT != MVT::iPTR && "Legalized args should be concrete");
634 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
Derek Schuff992d83f2016-02-10 20:14:15 +0000635 unsigned Offset = CCInfo.AllocateStack(Layout.getTypeAllocSize(Ty),
636 Layout.getABITypeAlignment(Ty));
Dan Gohman35bfb242015-12-04 23:22:35 +0000637 CCInfo.addLoc(CCValAssign::getMem(ArgLocs.size(), VT.getSimpleVT(),
638 Offset, VT.getSimpleVT(),
639 CCValAssign::Full));
640 }
641 }
642
643 unsigned NumBytes = CCInfo.getAlignedCallFrameSize();
644
Derek Schuff27501e22016-02-10 19:51:04 +0000645 SDValue FINode;
646 if (IsVarArg && NumBytes) {
Dan Gohman35bfb242015-12-04 23:22:35 +0000647 // For non-fixed arguments, next emit stores to store the argument values
Derek Schuff27501e22016-02-10 19:51:04 +0000648 // to the stack buffer at the offsets computed above.
Matthias Braun941a7052016-07-28 18:40:00 +0000649 int FI = MF.getFrameInfo().CreateStackObject(NumBytes,
650 Layout.getStackAlignment(),
651 /*isSS=*/false);
Dan Gohman35bfb242015-12-04 23:22:35 +0000652 unsigned ValNo = 0;
653 SmallVector<SDValue, 8> Chains;
654 for (SDValue Arg :
655 make_range(OutVals.begin() + NumFixedArgs, OutVals.end())) {
656 assert(ArgLocs[ValNo].getValNo() == ValNo &&
657 "ArgLocs should remain in order and only hold varargs args");
658 unsigned Offset = ArgLocs[ValNo++].getLocMemOffset();
Derek Schuff992d83f2016-02-10 20:14:15 +0000659 FINode = DAG.getFrameIndex(FI, getPointerTy(Layout));
Derek Schuff27501e22016-02-10 19:51:04 +0000660 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, FINode,
Dan Gohman35bfb242015-12-04 23:22:35 +0000661 DAG.getConstant(Offset, DL, PtrVT));
Heejin Ahnf208f632018-09-05 01:27:38 +0000662 Chains.push_back(
663 DAG.getStore(Chain, DL, Arg, Add,
664 MachinePointerInfo::getFixedStack(MF, FI, Offset), 0));
Dan Gohman35bfb242015-12-04 23:22:35 +0000665 }
666 if (!Chains.empty())
667 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
Derek Schuff27501e22016-02-10 19:51:04 +0000668 } else if (IsVarArg) {
669 FINode = DAG.getIntPtrConstant(0, DL);
Dan Gohman35bfb242015-12-04 23:22:35 +0000670 }
671
672 // Compute the operands for the CALLn node.
JF Bastiend8a9d662015-08-24 21:59:51 +0000673 SmallVector<SDValue, 16> Ops;
674 Ops.push_back(Chain);
JF Bastienaf111db2015-08-24 22:16:48 +0000675 Ops.push_back(Callee);
Dan Gohman35bfb242015-12-04 23:22:35 +0000676
677 // Add all fixed arguments. Note that for non-varargs calls, NumFixedArgs
678 // isn't reliable.
679 Ops.append(OutVals.begin(),
680 IsVarArg ? OutVals.begin() + NumFixedArgs : OutVals.end());
Derek Schuff27501e22016-02-10 19:51:04 +0000681 // Add a pointer to the vararg buffer.
Heejin Ahnf208f632018-09-05 01:27:38 +0000682 if (IsVarArg)
683 Ops.push_back(FINode);
JF Bastiend8a9d662015-08-24 21:59:51 +0000684
Derek Schuff27501e22016-02-10 19:51:04 +0000685 SmallVector<EVT, 8> InTys;
Dan Gohman2d822e72015-12-04 17:12:52 +0000686 for (const auto &In : Ins) {
Dan Gohman7935fa32015-12-10 00:22:40 +0000687 assert(!In.Flags.isByVal() && "byval is not valid for return values");
688 assert(!In.Flags.isNest() && "nest is not valid for return values");
Dan Gohman2d822e72015-12-04 17:12:52 +0000689 if (In.Flags.isInAlloca())
Dan Gohman7935fa32015-12-10 00:22:40 +0000690 fail(DL, DAG, "WebAssembly hasn't implemented inalloca return values");
Dan Gohman2d822e72015-12-04 17:12:52 +0000691 if (In.Flags.isInConsecutiveRegs())
Dan Gohman7935fa32015-12-10 00:22:40 +0000692 fail(DL, DAG, "WebAssembly hasn't implemented cons regs return values");
Dan Gohman2d822e72015-12-04 17:12:52 +0000693 if (In.Flags.isInConsecutiveRegsLast())
Dan Gohman4b9d7912015-12-15 22:01:29 +0000694 fail(DL, DAG,
695 "WebAssembly hasn't implemented cons regs last return values");
Dan Gohman2d822e72015-12-04 17:12:52 +0000696 // Ignore In.getOrigAlign() because all our arguments are passed in
697 // registers.
Derek Schuff27501e22016-02-10 19:51:04 +0000698 InTys.push_back(In.VT);
Dan Gohman2d822e72015-12-04 17:12:52 +0000699 }
Derek Schuff27501e22016-02-10 19:51:04 +0000700 InTys.push_back(MVT::Other);
701 SDVTList InTyList = DAG.getVTList(InTys);
Dan Gohmanf71abef2015-09-09 16:13:47 +0000702 SDValue Res =
703 DAG.getNode(Ins.empty() ? WebAssemblyISD::CALL0 : WebAssemblyISD::CALL1,
Derek Schuff27501e22016-02-10 19:51:04 +0000704 DL, InTyList, Ops);
JF Bastienaf111db2015-08-24 22:16:48 +0000705 if (Ins.empty()) {
706 Chain = Res;
707 } else {
708 InVals.push_back(Res);
709 Chain = Res.getValue(1);
710 }
JF Bastiend8a9d662015-08-24 21:59:51 +0000711
JF Bastiend8a9d662015-08-24 21:59:51 +0000712 return Chain;
713}
714
JF Bastienb9073fb2015-07-22 21:28:15 +0000715bool WebAssemblyTargetLowering::CanLowerReturn(
Dan Gohman7a6b9822015-11-29 22:32:02 +0000716 CallingConv::ID /*CallConv*/, MachineFunction & /*MF*/, bool /*IsVarArg*/,
717 const SmallVectorImpl<ISD::OutputArg> &Outs,
718 LLVMContext & /*Context*/) const {
JF Bastienb9073fb2015-07-22 21:28:15 +0000719 // WebAssembly can't currently handle returning tuples.
720 return Outs.size() <= 1;
721}
722
723SDValue WebAssemblyTargetLowering::LowerReturn(
Dan Gohman35bfb242015-12-04 23:22:35 +0000724 SDValue Chain, CallingConv::ID CallConv, bool /*IsVarArg*/,
JF Bastienb9073fb2015-07-22 21:28:15 +0000725 const SmallVectorImpl<ISD::OutputArg> &Outs,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000726 const SmallVectorImpl<SDValue> &OutVals, const SDLoc &DL,
JF Bastienb9073fb2015-07-22 21:28:15 +0000727 SelectionDAG &DAG) const {
JF Bastienb9073fb2015-07-22 21:28:15 +0000728 assert(Outs.size() <= 1 && "WebAssembly can only return up to one value");
Dan Gohman85dbdda2015-12-04 17:16:07 +0000729 if (!CallingConvSupported(CallConv))
JF Bastienb9073fb2015-07-22 21:28:15 +0000730 fail(DL, DAG, "WebAssembly doesn't support non-C calling conventions");
731
JF Bastien600aee92015-07-31 17:53:38 +0000732 SmallVector<SDValue, 4> RetOps(1, Chain);
733 RetOps.append(OutVals.begin(), OutVals.end());
JF Bastien4a2d5602015-07-31 21:04:18 +0000734 Chain = DAG.getNode(WebAssemblyISD::RETURN, DL, MVT::Other, RetOps);
JF Bastienb9073fb2015-07-22 21:28:15 +0000735
Dan Gohman754cd112015-11-11 01:33:02 +0000736 // Record the number and types of the return values.
737 for (const ISD::OutputArg &Out : Outs) {
Dan Gohmanac132e92015-12-02 23:40:03 +0000738 assert(!Out.Flags.isByVal() && "byval is not valid for return values");
739 assert(!Out.Flags.isNest() && "nest is not valid for return values");
Dan Gohman35bfb242015-12-04 23:22:35 +0000740 assert(Out.IsFixed && "non-fixed return value is not valid");
Dan Gohman754cd112015-11-11 01:33:02 +0000741 if (Out.Flags.isInAlloca())
742 fail(DL, DAG, "WebAssembly hasn't implemented inalloca results");
Dan Gohman754cd112015-11-11 01:33:02 +0000743 if (Out.Flags.isInConsecutiveRegs())
744 fail(DL, DAG, "WebAssembly hasn't implemented cons regs results");
745 if (Out.Flags.isInConsecutiveRegsLast())
746 fail(DL, DAG, "WebAssembly hasn't implemented cons regs last results");
Dan Gohman754cd112015-11-11 01:33:02 +0000747 }
748
JF Bastienb9073fb2015-07-22 21:28:15 +0000749 return Chain;
750}
751
752SDValue WebAssemblyTargetLowering::LowerFormalArguments(
Derek Schuff27501e22016-02-10 19:51:04 +0000753 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000754 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
755 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman85dbdda2015-12-04 17:16:07 +0000756 if (!CallingConvSupported(CallConv))
JF Bastienb9073fb2015-07-22 21:28:15 +0000757 fail(DL, DAG, "WebAssembly doesn't support non-C calling conventions");
JF Bastienb9073fb2015-07-22 21:28:15 +0000758
Dan Gohman2726b882016-10-06 22:29:32 +0000759 MachineFunction &MF = DAG.getMachineFunction();
760 auto *MFI = MF.getInfo<WebAssemblyFunctionInfo>();
761
Dan Gohmanfb3e0592015-11-25 19:36:19 +0000762 // Set up the incoming ARGUMENTS value, which serves to represent the liveness
763 // of the incoming values before they're represented by virtual registers.
764 MF.getRegInfo().addLiveIn(WebAssembly::ARGUMENTS);
765
JF Bastien600aee92015-07-31 17:53:38 +0000766 for (const ISD::InputArg &In : Ins) {
JF Bastien600aee92015-07-31 17:53:38 +0000767 if (In.Flags.isInAlloca())
768 fail(DL, DAG, "WebAssembly hasn't implemented inalloca arguments");
769 if (In.Flags.isNest())
770 fail(DL, DAG, "WebAssembly hasn't implemented nest arguments");
JF Bastien600aee92015-07-31 17:53:38 +0000771 if (In.Flags.isInConsecutiveRegs())
772 fail(DL, DAG, "WebAssembly hasn't implemented cons regs arguments");
773 if (In.Flags.isInConsecutiveRegsLast())
774 fail(DL, DAG, "WebAssembly hasn't implemented cons regs last arguments");
Dan Gohman9c54d3b2015-11-25 18:13:18 +0000775 // Ignore In.getOrigAlign() because all our arguments are passed in
776 // registers.
Heejin Ahnf208f632018-09-05 01:27:38 +0000777 InVals.push_back(In.Used ? DAG.getNode(WebAssemblyISD::ARGUMENT, DL, In.VT,
778 DAG.getTargetConstant(InVals.size(),
779 DL, MVT::i32))
780 : DAG.getUNDEF(In.VT));
Dan Gohman754cd112015-11-11 01:33:02 +0000781
782 // Record the number and types of arguments.
Derek Schuff27501e22016-02-10 19:51:04 +0000783 MFI->addParam(In.VT);
JF Bastien600aee92015-07-31 17:53:38 +0000784 }
JF Bastienb9073fb2015-07-22 21:28:15 +0000785
Derek Schuff27501e22016-02-10 19:51:04 +0000786 // Varargs are copied into a buffer allocated by the caller, and a pointer to
787 // the buffer is passed as an argument.
788 if (IsVarArg) {
789 MVT PtrVT = getPointerTy(MF.getDataLayout());
790 unsigned VarargVreg =
791 MF.getRegInfo().createVirtualRegister(getRegClassFor(PtrVT));
792 MFI->setVarargBufferVreg(VarargVreg);
793 Chain = DAG.getCopyToReg(
794 Chain, DL, VarargVreg,
795 DAG.getNode(WebAssemblyISD::ARGUMENT, DL, PtrVT,
796 DAG.getTargetConstant(Ins.size(), DL, MVT::i32)));
797 MFI->addParam(PtrVT);
798 }
Dan Gohman35bfb242015-12-04 23:22:35 +0000799
Derek Schuff77a7a382018-10-03 22:22:48 +0000800 // Record the number and types of arguments and results.
Dan Gohman2726b882016-10-06 22:29:32 +0000801 SmallVector<MVT, 4> Params;
802 SmallVector<MVT, 4> Results;
Derek Schuff77a7a382018-10-03 22:22:48 +0000803 ComputeSignatureVTs(MF.getFunction().getFunctionType(), MF.getFunction(),
804 DAG.getTarget(), Params, Results);
Dan Gohman2726b882016-10-06 22:29:32 +0000805 for (MVT VT : Results)
806 MFI->addResult(VT);
Derek Schuff77a7a382018-10-03 22:22:48 +0000807 // TODO: Use signatures in WebAssemblyMachineFunctionInfo too and unify
808 // the param logic here with ComputeSignatureVTs
809 assert(MFI->getParams().size() == Params.size() &&
810 std::equal(MFI->getParams().begin(), MFI->getParams().end(),
811 Params.begin()));
Dan Gohman2726b882016-10-06 22:29:32 +0000812
JF Bastienb9073fb2015-07-22 21:28:15 +0000813 return Chain;
814}
815
Dan Gohman10e730a2015-06-29 23:51:55 +0000816//===----------------------------------------------------------------------===//
JF Bastienaf111db2015-08-24 22:16:48 +0000817// Custom lowering hooks.
Dan Gohman10e730a2015-06-29 23:51:55 +0000818//===----------------------------------------------------------------------===//
819
JF Bastienaf111db2015-08-24 22:16:48 +0000820SDValue WebAssemblyTargetLowering::LowerOperation(SDValue Op,
821 SelectionDAG &DAG) const {
Derek Schuff51699a82016-02-12 22:56:03 +0000822 SDLoc DL(Op);
JF Bastienaf111db2015-08-24 22:16:48 +0000823 switch (Op.getOpcode()) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000824 default:
825 llvm_unreachable("unimplemented operation lowering");
826 return SDValue();
827 case ISD::FrameIndex:
828 return LowerFrameIndex(Op, DAG);
829 case ISD::GlobalAddress:
830 return LowerGlobalAddress(Op, DAG);
831 case ISD::ExternalSymbol:
832 return LowerExternalSymbol(Op, DAG);
833 case ISD::JumpTable:
834 return LowerJumpTable(Op, DAG);
835 case ISD::BR_JT:
836 return LowerBR_JT(Op, DAG);
837 case ISD::VASTART:
838 return LowerVASTART(Op, DAG);
839 case ISD::BlockAddress:
840 case ISD::BRIND:
841 fail(DL, DAG, "WebAssembly hasn't implemented computed gotos");
842 return SDValue();
843 case ISD::RETURNADDR: // Probably nothing meaningful can be returned here.
844 fail(DL, DAG, "WebAssembly hasn't implemented __builtin_return_address");
845 return SDValue();
846 case ISD::FRAMEADDR:
847 return LowerFRAMEADDR(Op, DAG);
848 case ISD::CopyToReg:
849 return LowerCopyToReg(Op, DAG);
850 case ISD::INTRINSIC_WO_CHAIN:
851 return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Thomas Livelya0d25812018-09-07 21:54:46 +0000852 case ISD::VECTOR_SHUFFLE:
853 return LowerVECTOR_SHUFFLE(Op, DAG);
Thomas Lively55735d52018-10-20 01:31:18 +0000854 case ISD::SHL:
855 case ISD::SRA:
856 case ISD::SRL:
857 return LowerShift(Op, DAG);
JF Bastienaf111db2015-08-24 22:16:48 +0000858 }
859}
860
Derek Schuffaadc89c2016-02-16 18:18:36 +0000861SDValue WebAssemblyTargetLowering::LowerCopyToReg(SDValue Op,
862 SelectionDAG &DAG) const {
863 SDValue Src = Op.getOperand(2);
864 if (isa<FrameIndexSDNode>(Src.getNode())) {
865 // CopyToReg nodes don't support FrameIndex operands. Other targets select
866 // the FI to some LEA-like instruction, but since we don't have that, we
867 // need to insert some kind of instruction that can take an FI operand and
868 // produces a value usable by CopyToReg (i.e. in a vreg). So insert a dummy
869 // copy_local between Op and its FI operand.
Dan Gohman02c08712016-02-20 23:09:44 +0000870 SDValue Chain = Op.getOperand(0);
Derek Schuffaadc89c2016-02-16 18:18:36 +0000871 SDLoc DL(Op);
Dan Gohman02c08712016-02-20 23:09:44 +0000872 unsigned Reg = cast<RegisterSDNode>(Op.getOperand(1))->getReg();
Derek Schuffaadc89c2016-02-16 18:18:36 +0000873 EVT VT = Src.getValueType();
Heejin Ahnf208f632018-09-05 01:27:38 +0000874 SDValue Copy(DAG.getMachineNode(VT == MVT::i32 ? WebAssembly::COPY_I32
875 : WebAssembly::COPY_I64,
876 DL, VT, Src),
877 0);
Dan Gohman02c08712016-02-20 23:09:44 +0000878 return Op.getNode()->getNumValues() == 1
879 ? DAG.getCopyToReg(Chain, DL, Reg, Copy)
Heejin Ahnf208f632018-09-05 01:27:38 +0000880 : DAG.getCopyToReg(Chain, DL, Reg, Copy,
881 Op.getNumOperands() == 4 ? Op.getOperand(3)
882 : SDValue());
Derek Schuffaadc89c2016-02-16 18:18:36 +0000883 }
884 return SDValue();
885}
886
Derek Schuff9769deb2015-12-11 23:49:46 +0000887SDValue WebAssemblyTargetLowering::LowerFrameIndex(SDValue Op,
888 SelectionDAG &DAG) const {
889 int FI = cast<FrameIndexSDNode>(Op)->getIndex();
890 return DAG.getTargetFrameIndex(FI, Op.getValueType());
891}
892
Dan Gohman94c65662016-02-16 23:48:04 +0000893SDValue WebAssemblyTargetLowering::LowerFRAMEADDR(SDValue Op,
894 SelectionDAG &DAG) const {
895 // Non-zero depths are not supported by WebAssembly currently. Use the
896 // legalizer's default expansion, which is to return 0 (what this function is
897 // documented to do).
Dan Gohman1d547bf2016-02-17 00:14:03 +0000898 if (Op.getConstantOperandVal(0) > 0)
Dan Gohman94c65662016-02-16 23:48:04 +0000899 return SDValue();
900
Matthias Braun941a7052016-07-28 18:40:00 +0000901 DAG.getMachineFunction().getFrameInfo().setFrameAddressIsTaken(true);
Dan Gohman94c65662016-02-16 23:48:04 +0000902 EVT VT = Op.getValueType();
903 unsigned FP =
904 Subtarget->getRegisterInfo()->getFrameRegister(DAG.getMachineFunction());
905 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), FP, VT);
906}
907
JF Bastienaf111db2015-08-24 22:16:48 +0000908SDValue WebAssemblyTargetLowering::LowerGlobalAddress(SDValue Op,
909 SelectionDAG &DAG) const {
910 SDLoc DL(Op);
911 const auto *GA = cast<GlobalAddressSDNode>(Op);
912 EVT VT = Op.getValueType();
Dan Gohman26c67652016-01-11 23:38:05 +0000913 assert(GA->getTargetFlags() == 0 &&
914 "Unexpected target flags on generic GlobalAddressSDNode");
JF Bastienaf111db2015-08-24 22:16:48 +0000915 if (GA->getAddressSpace() != 0)
916 fail(DL, DAG, "WebAssembly only expects the 0 address space");
Dan Gohman4b9d7912015-12-15 22:01:29 +0000917 return DAG.getNode(
918 WebAssemblyISD::Wrapper, DL, VT,
919 DAG.getTargetGlobalAddress(GA->getGlobal(), DL, VT, GA->getOffset()));
JF Bastienaf111db2015-08-24 22:16:48 +0000920}
921
Heejin Ahnf208f632018-09-05 01:27:38 +0000922SDValue
923WebAssemblyTargetLowering::LowerExternalSymbol(SDValue Op,
924 SelectionDAG &DAG) const {
Dan Gohman2c8fe6a2015-11-25 16:44:29 +0000925 SDLoc DL(Op);
926 const auto *ES = cast<ExternalSymbolSDNode>(Op);
927 EVT VT = Op.getValueType();
Dan Gohman26c67652016-01-11 23:38:05 +0000928 assert(ES->getTargetFlags() == 0 &&
929 "Unexpected target flags on generic ExternalSymbolSDNode");
930 // Set the TargetFlags to 0x1 which indicates that this is a "function"
931 // symbol rather than a data symbol. We do this unconditionally even though
932 // we don't know anything about the symbol other than its name, because all
933 // external symbols used in target-independent SelectionDAG code are for
934 // functions.
Heejin Ahnf208f632018-09-05 01:27:38 +0000935 return DAG.getNode(
936 WebAssemblyISD::Wrapper, DL, VT,
937 DAG.getTargetExternalSymbol(ES->getSymbol(), VT,
938 WebAssemblyII::MO_SYMBOL_FUNCTION));
Dan Gohman2c8fe6a2015-11-25 16:44:29 +0000939}
940
Dan Gohman950a13c2015-09-16 16:51:30 +0000941SDValue WebAssemblyTargetLowering::LowerJumpTable(SDValue Op,
942 SelectionDAG &DAG) const {
943 // There's no need for a Wrapper node because we always incorporate a jump
Dan Gohman14026062016-03-08 03:18:12 +0000944 // table operand into a BR_TABLE instruction, rather than ever
Dan Gohmanbb7ce8e2015-11-20 03:02:49 +0000945 // materializing it in a register.
Dan Gohman950a13c2015-09-16 16:51:30 +0000946 const JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
947 return DAG.getTargetJumpTable(JT->getIndex(), Op.getValueType(),
948 JT->getTargetFlags());
949}
950
951SDValue WebAssemblyTargetLowering::LowerBR_JT(SDValue Op,
952 SelectionDAG &DAG) const {
953 SDLoc DL(Op);
954 SDValue Chain = Op.getOperand(0);
955 const auto *JT = cast<JumpTableSDNode>(Op.getOperand(1));
956 SDValue Index = Op.getOperand(2);
957 assert(JT->getTargetFlags() == 0 && "WebAssembly doesn't set target flags");
958
959 SmallVector<SDValue, 8> Ops;
960 Ops.push_back(Chain);
961 Ops.push_back(Index);
962
963 MachineJumpTableInfo *MJTI = DAG.getMachineFunction().getJumpTableInfo();
964 const auto &MBBs = MJTI->getJumpTables()[JT->getIndex()].MBBs;
965
Dan Gohman14026062016-03-08 03:18:12 +0000966 // Add an operand for each case.
Heejin Ahnf208f632018-09-05 01:27:38 +0000967 for (auto MBB : MBBs)
968 Ops.push_back(DAG.getBasicBlock(MBB));
Dan Gohman14026062016-03-08 03:18:12 +0000969
Dan Gohman950a13c2015-09-16 16:51:30 +0000970 // TODO: For now, we just pick something arbitrary for a default case for now.
971 // We really want to sniff out the guard and put in the real default case (and
972 // delete the guard).
973 Ops.push_back(DAG.getBasicBlock(MBBs[0]));
974
Dan Gohman14026062016-03-08 03:18:12 +0000975 return DAG.getNode(WebAssemblyISD::BR_TABLE, DL, MVT::Other, Ops);
Dan Gohman950a13c2015-09-16 16:51:30 +0000976}
977
Dan Gohman35bfb242015-12-04 23:22:35 +0000978SDValue WebAssemblyTargetLowering::LowerVASTART(SDValue Op,
979 SelectionDAG &DAG) const {
980 SDLoc DL(Op);
981 EVT PtrVT = getPointerTy(DAG.getMachineFunction().getDataLayout());
982
Derek Schuff27501e22016-02-10 19:51:04 +0000983 auto *MFI = DAG.getMachineFunction().getInfo<WebAssemblyFunctionInfo>();
Dan Gohman35bfb242015-12-04 23:22:35 +0000984 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Derek Schuff27501e22016-02-10 19:51:04 +0000985
986 SDValue ArgN = DAG.getCopyFromReg(DAG.getEntryNode(), DL,
987 MFI->getVarargBufferVreg(), PtrVT);
988 return DAG.getStore(Op.getOperand(0), DL, ArgN, Op.getOperand(1),
Derek Schuff1a946e42016-07-15 19:35:43 +0000989 MachinePointerInfo(SV), 0);
Dan Gohman35bfb242015-12-04 23:22:35 +0000990}
991
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000992SDValue
993WebAssemblyTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
994 SelectionDAG &DAG) const {
995 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
996 SDLoc DL(Op);
997 switch (IntNo) {
998 default:
999 return {}; // Don't custom lower most intrinsics.
Thomas Lively5d461c92018-10-03 23:02:23 +00001000
Krasimir Georgiev547d8242018-10-16 18:50:09 +00001001 case Intrinsic::wasm_lsda:
1002 // TODO For now, just return 0 not to crash
1003 return DAG.getConstant(0, DL, Op.getValueType());
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +00001004 }
1005}
1006
Thomas Livelya0d25812018-09-07 21:54:46 +00001007SDValue
1008WebAssemblyTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
1009 SelectionDAG &DAG) const {
1010 SDLoc DL(Op);
1011 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Op.getNode())->getMask();
1012 MVT VecType = Op.getOperand(0).getSimpleValueType();
1013 assert(VecType.is128BitVector() && "Unexpected shuffle vector type");
1014 size_t LaneBytes = VecType.getVectorElementType().getSizeInBits() / 8;
1015
1016 // Space for two vector args and sixteen mask indices
1017 SDValue Ops[18];
1018 size_t OpIdx = 0;
1019 Ops[OpIdx++] = Op.getOperand(0);
1020 Ops[OpIdx++] = Op.getOperand(1);
1021
1022 // Expand mask indices to byte indices and materialize them as operands
1023 for (size_t I = 0, Lanes = Mask.size(); I < Lanes; ++I) {
1024 for (size_t J = 0; J < LaneBytes; ++J) {
Thomas Lively11a332d02018-10-19 19:08:06 +00001025 // Lower undefs (represented by -1 in mask) to zero
1026 uint64_t ByteIndex =
1027 Mask[I] == -1 ? 0 : (uint64_t)Mask[I] * LaneBytes + J;
1028 Ops[OpIdx++] = DAG.getConstant(ByteIndex, DL, MVT::i32);
Thomas Livelya0d25812018-09-07 21:54:46 +00001029 }
1030 }
1031
Thomas Livelyed951342018-10-24 23:27:40 +00001032 return DAG.getNode(WebAssemblyISD::SHUFFLE, DL, Op.getValueType(), Ops);
Thomas Livelya0d25812018-09-07 21:54:46 +00001033}
1034
Thomas Lively55735d52018-10-20 01:31:18 +00001035SDValue WebAssemblyTargetLowering::LowerShift(SDValue Op,
1036 SelectionDAG &DAG) const {
1037 SDLoc DL(Op);
1038 auto *ShiftVec = dyn_cast<BuildVectorSDNode>(Op.getOperand(1).getNode());
1039 APInt SplatValue, SplatUndef;
1040 unsigned SplatBitSize;
1041 bool HasAnyUndefs;
1042 if (!ShiftVec || !ShiftVec->isConstantSplat(SplatValue, SplatUndef,
1043 SplatBitSize, HasAnyUndefs))
1044 return Op;
1045 unsigned Opcode;
1046 switch (Op.getOpcode()) {
1047 case ISD::SHL:
1048 Opcode = WebAssemblyISD::VEC_SHL;
1049 break;
1050 case ISD::SRA:
1051 Opcode = WebAssemblyISD::VEC_SHR_S;
1052 break;
1053 case ISD::SRL:
1054 Opcode = WebAssemblyISD::VEC_SHR_U;
1055 break;
1056 default:
1057 llvm_unreachable("unexpected opcode");
1058 return Op;
1059 }
1060 return DAG.getNode(Opcode, DL, Op.getValueType(), Op.getOperand(0),
1061 DAG.getConstant(SplatValue.trunc(32), DL, MVT::i32));
1062}
1063
Dan Gohman10e730a2015-06-29 23:51:55 +00001064//===----------------------------------------------------------------------===//
1065// WebAssembly Optimization Hooks
1066//===----------------------------------------------------------------------===//