Debian Codegraph @

js/src/jit/x86-shared/CodeGenerator-x86-shared.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "jit/x86-shared/CodeGenerator-x86-shared.h"
8
9 #include "mozilla/DebugOnly.h"
10 #include "mozilla/MathAlgorithms.h"
11
12 #include "jit/CodeGenerator.h"
13 #include "jit/InlineScriptTree.h"
14 #include "jit/JitRuntime.h"
15 #include "jit/RangeAnalysis.h"
16 #include "jit/ReciprocalMulConstants.h"
17 #include "js/ScalarType.h" // js::Scalar::Type
18
19 #include "jit/MacroAssembler-inl.h"
20 #include "jit/shared/CodeGenerator-shared-inl.h"
21
22 using namespace js;
23 using namespace js::jit;
24
25 using mozilla::Abs;
26 using mozilla::DebugOnly;
27 using mozilla::FloorLog2;
28 using mozilla::NegativeInfinity;
29
30 using JS::GenericNaN;
31
32 namespace js {
33 namespace jit {
34
35 CodeGeneratorX86Shared::CodeGeneratorX86Shared(
36 MIRGenerator* gen, LIRGraph* graph, MacroAssembler* masm,
37 const wasm::CodeMetadata* wasmCodeMeta)
38 : CodeGeneratorShared(gen, graph, masm, wasmCodeMeta) {}
39
40 #ifdef JS_PUNBOX64
41 Operand CodeGeneratorX86Shared::ToOperandOrRegister64(
42 const LInt64Allocation& input) {
43 return ToOperand(input.value());
44 }
45 #else
46 Register64 CodeGeneratorX86Shared::ToOperandOrRegister64(
47 const LInt64Allocation& input) {
48 return ToRegister64(input);
49 }
50 #endif
51
52 void CodeGeneratorX86Shared::emitBranch(Assembler::Condition cond,
53 MBasicBlock* mirTrue,
54 MBasicBlock* mirFalse) {
55 if (isNextBlock(mirFalse->lir())) {
56 jumpToBlock(mirTrue, cond);
57 } else {
58 jumpToBlock(mirFalse, Assembler::InvertCondition(cond));
59 jumpToBlock(mirTrue);
60 }
61 }
62
63 void CodeGeneratorX86Shared::emitBranch(Assembler::DoubleCondition cond,
64 MBasicBlock* ifTrue,
65 MBasicBlock* ifFalse,
66 Assembler::NaNCond ifNaN) {
67 if (ifNaN == Assembler::NaN_IsFalse) {
68 jumpToBlock(ifFalse, Assembler::Parity);
69 } else if (ifNaN == Assembler::NaN_IsTrue) {
70 jumpToBlock(ifTrue, Assembler::Parity);
71 }
72 emitBranch(Assembler::ConditionFromDoubleCondition(cond), ifTrue, ifFalse);
73 }
74
75 void CodeGenerator::visitTestDAndBranch(LTestDAndBranch* test) {
76 const LAllocation* opd = test->input();
77
78 // vucomisd flags:
79 // Z P C
80 // ---------
81 // NaN 1 1 1
82 // > 0 0 0
83 // < 0 0 1
84 // = 1 0 0
85 //
86 // NaN is falsey, so comparing against 0 and then using the Z flag is
87 // enough to determine which branch to take.
88 ScratchDoubleScope scratch(masm);
89 masm.zeroDouble(scratch);
90 masm.vucomisd(scratch, ToFloatRegister(opd));
91 emitBranch(Assembler::NotEqual, test->ifTrue(), test->ifFalse());
92 }
93
94 void CodeGenerator::visitTestFAndBranch(LTestFAndBranch* test) {
95 const LAllocation* opd = test->input();
96 // vucomiss flags are the same as doubles; see comment above
97 {
98 ScratchFloat32Scope scratch(masm);
99 masm.zeroFloat32(scratch);
100 masm.vucomiss(scratch, ToFloatRegister(opd));
101 }
102 emitBranch(Assembler::NotEqual, test->ifTrue(), test->ifFalse());
103 }
104
105 static Assembler::DoubleCondition ToDoubleCondition(FloatRegister lhs,
106 FloatRegister rhs,
107 JSOp op) {
108 if (lhs == rhs) {
109 switch (op) {
110 case JSOp::Eq:
111 case JSOp::StrictEq:
112 case JSOp::Le:
113 case JSOp::Ge:
114 return Assembler::DoubleOrdered;
115 case JSOp::Ne:
116 case JSOp::StrictNe:
117 return Assembler::DoubleUnordered;
118 default:
119 break;
120 }
121 }
122 return JSOpToDoubleCondition(op);
123 }
124
125 void CodeGenerator::visitCompareD(LCompareD* comp) {
126 FloatRegister lhs = ToFloatRegister(comp->left());
127 FloatRegister rhs = ToFloatRegister(comp->right());
128 Register output = ToRegister(comp->output());
129
130 Assembler::DoubleCondition cond =
131 ToDoubleCondition(lhs, rhs, comp->mir()->jsop());
132
133 Assembler::NaNCond nanCond = Assembler::NaNCondFromDoubleCondition(cond);
134 if (comp->mir()->operandsAreNeverNaN()) {
135 nanCond = Assembler::NaN_HandledByCond;
136 }
137
138 bool destIsZero = masm.maybeEmitSetZeroByteRegister(output);
139 masm.compareDouble(cond, lhs, rhs);
140 masm.emitSet(Assembler::ConditionFromDoubleCondition(cond), output,
141 destIsZero, nanCond);
142 }
143
144 void CodeGenerator::visitCompareF(LCompareF* comp) {
145 FloatRegister lhs = ToFloatRegister(comp->left());
146 FloatRegister rhs = ToFloatRegister(comp->right());
147 Register output = ToRegister(comp->output());
148
149 Assembler::DoubleCondition cond =
150 ToDoubleCondition(lhs, rhs, comp->mir()->jsop());
151
152 Assembler::NaNCond nanCond = Assembler::NaNCondFromDoubleCondition(cond);
153 if (comp->mir()->operandsAreNeverNaN()) {
154 nanCond = Assembler::NaN_HandledByCond;
155 }
156
157 bool destIsZero = masm.maybeEmitSetZeroByteRegister(output);
158 masm.compareFloat(cond, lhs, rhs);
159 masm.emitSet(Assembler::ConditionFromDoubleCondition(cond), output,
160 destIsZero, nanCond);
161 }
162
163 void CodeGenerator::visitNotD(LNotD* ins) {
164 FloatRegister opd = ToFloatRegister(ins->input());
165 Register output = ToRegister(ins->output());
166
167 // Not returns true if the input is a NaN. We don't have to worry about
168 // it if we know the input is never NaN though.
169 Assembler::NaNCond nanCond = Assembler::NaN_IsTrue;
170 if (ins->mir()->operandIsNeverNaN()) {
171 nanCond = Assembler::NaN_HandledByCond;
172 }
173
174 bool destIsZero = masm.maybeEmitSetZeroByteRegister(output);
175 ScratchDoubleScope scratch(masm);
176 masm.zeroDouble(scratch);
177 masm.compareDouble(Assembler::DoubleEqualOrUnordered, opd, scratch);
178 masm.emitSet(Assembler::Equal, output, destIsZero, nanCond);
179 }
180
181 void CodeGenerator::visitNotF(LNotF* ins) {
182 FloatRegister opd = ToFloatRegister(ins->input());
183 Register output = ToRegister(ins->output());
184
185 // Not returns true if the input is a NaN. We don't have to worry about
186 // it if we know the input is never NaN though.
187 Assembler::NaNCond nanCond = Assembler::NaN_IsTrue;
188 if (ins->mir()->operandIsNeverNaN()) {
189 nanCond = Assembler::NaN_HandledByCond;
190 }
191
192 bool destIsZero = masm.maybeEmitSetZeroByteRegister(output);
193 ScratchFloat32Scope scratch(masm);
194 masm.zeroFloat32(scratch);
195 masm.compareFloat(Assembler::DoubleEqualOrUnordered, opd, scratch);
196 masm.emitSet(Assembler::Equal, output, destIsZero, nanCond);
197 }
198
199 void CodeGenerator::visitCompareDAndBranch(LCompareDAndBranch* comp) {
200 FloatRegister lhs = ToFloatRegister(comp->left());
201 FloatRegister rhs = ToFloatRegister(comp->right());
202
203 Assembler::DoubleCondition cond =
204 ToDoubleCondition(lhs, rhs, comp->cmpMir()->jsop());
205
206 Assembler::NaNCond nanCond = Assembler::NaNCondFromDoubleCondition(cond);
207 if (comp->cmpMir()->operandsAreNeverNaN()) {
208 nanCond = Assembler::NaN_HandledByCond;
209 }
210
211 masm.compareDouble(cond, lhs, rhs);
212 emitBranch(cond, comp->ifTrue(), comp->ifFalse(), nanCond);
213 }
214
215 void CodeGenerator::visitCompareFAndBranch(LCompareFAndBranch* comp) {
216 FloatRegister lhs = ToFloatRegister(comp->left());
217 FloatRegister rhs = ToFloatRegister(comp->right());
218
219 Assembler::DoubleCondition cond =
220 ToDoubleCondition(lhs, rhs, comp->cmpMir()->jsop());
221
222 Assembler::NaNCond nanCond = Assembler::NaNCondFromDoubleCondition(cond);
223 if (comp->cmpMir()->operandsAreNeverNaN()) {
224 nanCond = Assembler::NaN_HandledByCond;
225 }
226
227 masm.compareFloat(cond, lhs, rhs);
228 emitBranch(cond, comp->ifTrue(), comp->ifFalse(), nanCond);
229 }
230
231 void CodeGenerator::visitWasmStackArg(LWasmStackArg* ins) {
232 const MWasmStackArg* mir = ins->mir();
233 Address dst(StackPointer, mir->spOffset());
234 if (ins->arg()->isConstant()) {
235 masm.storePtr(ImmWord(ToInt32(ins->arg())), dst);
236 } else if (ins->arg()->isGeneralReg()) {
237 masm.storePtr(ToRegister(ins->arg()), dst);
238 } else {
239 switch (mir->input()->type()) {
240 case MIRType::Double:
241 masm.storeDouble(ToFloatRegister(ins->arg()), dst);
242 return;
243 case MIRType::Float32:
244 masm.storeFloat32(ToFloatRegister(ins->arg()), dst);
245 return;
246 #ifdef ENABLE_WASM_SIMD
247 case MIRType::Simd128:
248 masm.storeUnalignedSimd128(ToFloatRegister(ins->arg()), dst);
249 return;
250 #endif
251 default:
252 break;
253 }
254 MOZ_CRASH("unexpected mir type in WasmStackArg");
255 }
256 }
257
258 void CodeGenerator::visitWasmStackArgI64(LWasmStackArgI64* ins) {
259 const MWasmStackArg* mir = ins->mir();
260 Address dst(StackPointer, mir->spOffset());
261 if (IsConstant(ins->arg())) {
262 masm.store64(Imm64(ToInt64(ins->arg())), dst);
263 } else {
264 masm.store64(ToRegister64(ins->arg()), dst);
265 }
266 }
267
268 void CodeGenerator::visitWasmSelect(LWasmSelect* ins) {
269 MIRType mirType = ins->mir()->type();
270
271 Register cond = ToRegister(ins->condExpr());
272 Operand falseExpr = ToOperand(ins->falseExpr());
273
274 masm.test32(cond, cond);
275
276 if (mirType == MIRType::Int32 || mirType == MIRType::WasmAnyRef) {
277 Register out = ToRegister(ins->output());
278 MOZ_ASSERT(ToRegister(ins->trueExpr()) == out,
279 "true expr input is reused for output");
280 if (mirType == MIRType::Int32) {
281 masm.cmovz32(falseExpr, out);
282 } else {
283 masm.cmovzPtr(falseExpr, out);
284 }
285 return;
286 }
287
288 FloatRegister out = ToFloatRegister(ins->output());
289 MOZ_ASSERT(ToFloatRegister(ins->trueExpr()) == out,
290 "true expr input is reused for output");
291
292 Label done;
293 masm.j(Assembler::NonZero, &done);
294
295 if (mirType == MIRType::Float32) {
296 if (falseExpr.kind() == Operand::FPREG) {
297 masm.moveFloat32(ToFloatRegister(ins->falseExpr()), out);
298 } else {
299 masm.loadFloat32(falseExpr, out);
300 }
301 } else if (mirType == MIRType::Double) {
302 if (falseExpr.kind() == Operand::FPREG) {
303 masm.moveDouble(ToFloatRegister(ins->falseExpr()), out);
304 } else {
305 masm.loadDouble(falseExpr, out);
306 }
307 } else if (mirType == MIRType::Simd128) {
308 if (falseExpr.kind() == Operand::FPREG) {
309 masm.moveSimd128(ToFloatRegister(ins->falseExpr()), out);
310 } else {
311 masm.loadUnalignedSimd128(falseExpr, out);
312 }
313 } else {
314 MOZ_CRASH("unhandled type in visitWasmSelect!");
315 }
316
317 masm.bind(&done);
318 }
319
320 void CodeGenerator::visitAsmJSLoadHeap(LAsmJSLoadHeap* ins) {
321 const MAsmJSLoadHeap* mir = ins->mir();
322 MOZ_ASSERT(mir->access().offset64() == 0);
323
324 const LAllocation* ptr = ins->ptr();
325 const LAllocation* boundsCheckLimit = ins->boundsCheckLimit();
326 AnyRegister out = ToAnyRegister(ins->output());
327
328 Scalar::Type accessType = mir->accessType();
329
330 OutOfLineCode* ool = nullptr;
331 if (mir->needsBoundsCheck()) {
332 ool = new (alloc()) LambdaOutOfLineCode([=](OutOfLineCode& ool) {
333 switch (accessType) {
334 case Scalar::Int64:
335 case Scalar::BigInt64:
336 case Scalar::BigUint64:
337 case Scalar::Simd128:
338 case Scalar::Float16:
339 case Scalar::MaxTypedArrayViewType:
340 MOZ_CRASH("unexpected array type");
341 case Scalar::Float32:
342 masm.loadConstantFloat32(float(GenericNaN()), out.fpu());
343 break;
344 case Scalar::Float64:
345 masm.loadConstantDouble(GenericNaN(), out.fpu());
346 break;
347 case Scalar::Int8:
348 case Scalar::Uint8:
349 case Scalar::Int16:
350 case Scalar::Uint16:
351 case Scalar::Int32:
352 case Scalar::Uint32:
353 case Scalar::Uint8Clamped:
354 Register destReg = out.gpr();
355 masm.mov(ImmWord(0), destReg);
356 break;
357 }
358 masm.jmp(ool.rejoin());
359 });
360 addOutOfLineCode(ool, mir);
361
362 masm.wasmBoundsCheck32(Assembler::AboveOrEqual, ToRegister(ptr),
363 ToRegister(boundsCheckLimit), ool->entry());
364 }
365
366 Operand srcAddr = toMemoryAccessOperand(ins, 0);
367 masm.wasmLoad(mir->access(), srcAddr, out);
368
369 if (ool) {
370 masm.bind(ool->rejoin());
371 }
372 }
373
374 void CodeGenerator::visitAsmJSStoreHeap(LAsmJSStoreHeap* ins) {
375 const MAsmJSStoreHeap* mir = ins->mir();
376
377 const LAllocation* ptr = ins->ptr();
378 const LAllocation* value = ins->value();
379 const LAllocation* boundsCheckLimit = ins->boundsCheckLimit();
380
381 Label rejoin;
382 if (mir->needsBoundsCheck()) {
383 masm.wasmBoundsCheck32(Assembler::AboveOrEqual, ToRegister(ptr),
384 ToRegister(boundsCheckLimit), &rejoin);
385 }
386
387 Operand dstAddr = toMemoryAccessOperand(ins, 0);
388 masm.wasmStore(mir->access(), ToAnyRegister(value), dstAddr);
389
390 if (rejoin.used()) {
391 masm.bind(&rejoin);
392 }
393 }
394
395 void CodeGenerator::visitWasmAddOffset(LWasmAddOffset* lir) {
396 MWasmAddOffset* mir = lir->mir();
397 Register base = ToRegister(lir->base());
398 Register out = ToRegister(lir->output());
399
400 if (base != out) {
401 masm.move32(base, out);
402 }
403 masm.add32(Imm32(mir->offset()), out);
404 auto* ool = new (alloc()) LambdaOutOfLineCode([=](OutOfLineCode& ool) {
405 masm.wasmTrap(wasm::Trap::OutOfBounds, mir->trapSiteDesc());
406 });
407 addOutOfLineCode(ool, mir);
408 masm.j(Assembler::CarrySet, ool->entry());
409 }
410
411 void CodeGenerator::visitWasmAddOffset64(LWasmAddOffset64* lir) {
412 MWasmAddOffset* mir = lir->mir();
413 Register64 base = ToRegister64(lir->base());
414 Register64 out = ToOutRegister64(lir);
415
416 if (base != out) {
417 masm.move64(base, out);
418 }
419 masm.add64(Imm64(mir->offset()), out);
420 auto* ool = new (alloc()) LambdaOutOfLineCode([=](OutOfLineCode& ool) {
421 masm.wasmTrap(wasm::Trap::OutOfBounds, mir->trapSiteDesc());
422 });
423 addOutOfLineCode(ool, mir);
424 masm.j(Assembler::CarrySet, ool->entry());
425 }
426
427 void CodeGenerator::visitWasmTruncateToInt32(LWasmTruncateToInt32* lir) {
428 FloatRegister input = ToFloatRegister(lir->input());
429 Register output = ToRegister(lir->output());
430
431 MWasmTruncateToInt32* mir = lir->mir();
432 MIRType inputType = mir->input()->type();
433
434 MOZ_ASSERT(inputType == MIRType::Double || inputType == MIRType::Float32);
435
436 auto* ool = new (alloc()) OutOfLineWasmTruncateCheck(mir, input, output);
437 addOutOfLineCode(ool, mir);
438
439 Label* oolEntry = ool->entry();
440 if (mir->isUnsigned()) {
441 if (inputType == MIRType::Double) {
442 masm.wasmTruncateDoubleToUInt32(input, output, mir->isSaturating(),
443 oolEntry);
444 } else if (inputType == MIRType::Float32) {
445 masm.wasmTruncateFloat32ToUInt32(input, output, mir->isSaturating(),
446 oolEntry);
447 } else {
448 MOZ_CRASH("unexpected type");
449 }
450 if (mir->isSaturating()) {
451 masm.bind(ool->rejoin());
452 }
453 return;
454 }
455
456 if (inputType == MIRType::Double) {
457 masm.wasmTruncateDoubleToInt32(input, output, mir->isSaturating(),
458 oolEntry);
459 } else if (inputType == MIRType::Float32) {
460 masm.wasmTruncateFloat32ToInt32(input, output, mir->isSaturating(),
461 oolEntry);
462 } else {
463 MOZ_CRASH("unexpected type");
464 }
465
466 masm.bind(ool->rejoin());
467 }
468
469 bool CodeGeneratorX86Shared::generateOutOfLineCode() {
470 if (!CodeGeneratorShared::generateOutOfLineCode()) {
471 return false;
472 }
473
474 if (deoptLabel_.used()) {
475 // All non-table-based bailouts will go here.
476 masm.bind(&deoptLabel_);
477
478 // Push the frame size, so the handler can recover the IonScript.
479 masm.push(Imm32(frameSize()));
480
481 TrampolinePtr handler = gen->jitRuntime()->getGenericBailoutHandler();
482 masm.jump(handler);
483 }
484
485 return !masm.oom();
486 }
487
488 class BailoutJump {
489 Assembler::Condition cond_;
490
491 public:
492 explicit BailoutJump(Assembler::Condition cond) : cond_(cond) {}
493 #ifdef JS_CODEGEN_X86
494 void operator()(MacroAssembler& masm, uint8_t* code) const {
495 masm.j(cond_, ImmPtr(code), RelocationKind::HARDCODED);
496 }
497 #endif
498 void operator()(MacroAssembler& masm, Label* label) const {
499 masm.j(cond_, label);
500 }
501 };
502
503 class BailoutLabel {
504 Label* label_;
505
506 public:
507 explicit BailoutLabel(Label* label) : label_(label) {}
508 #ifdef JS_CODEGEN_X86
509 void operator()(MacroAssembler& masm, uint8_t* code) const {
510 masm.retarget(label_, ImmPtr(code), RelocationKind::HARDCODED);
511 }
512 #endif
513 void operator()(MacroAssembler& masm, Label* label) const {
514 masm.retarget(label_, label);
515 }
516 };
517
518 template <typename T>
519 void CodeGeneratorX86Shared::bailout(const T& binder, LSnapshot* snapshot) {
520 encode(snapshot);
521
522 // All bailout code is associated with the bytecodeSite of the block we are
523 // bailing out from.
524 InlineScriptTree* tree = snapshot->mir()->block()->trackedTree();
525 auto* ool = new (alloc()) LambdaOutOfLineCode([=](OutOfLineCode& ool) {
526 masm.push(Imm32(snapshot->snapshotOffset()));
527 masm.jmp(&deoptLabel_);
528 });
529 addOutOfLineCode(ool,
530 new (alloc()) BytecodeSite(tree, tree->script()->code()));
531
532 binder(masm, ool->entry());
533 }
534
535 void CodeGeneratorX86Shared::bailoutIf(Assembler::Condition condition,
536 LSnapshot* snapshot) {
537 bailout(BailoutJump(condition), snapshot);
538 }
539
540 void CodeGeneratorX86Shared::bailoutIf(Assembler::DoubleCondition condition,
541 LSnapshot* snapshot) {
542 MOZ_ASSERT(Assembler::NaNCondFromDoubleCondition(condition) ==
543 Assembler::NaN_HandledByCond);
544 bailoutIf(Assembler::ConditionFromDoubleCondition(condition), snapshot);
545 }
546
547 void CodeGeneratorX86Shared::bailoutFrom(Label* label, LSnapshot* snapshot) {
548 MOZ_ASSERT_IF(!masm.oom(), label->used() && !label->bound());
549 bailout(BailoutLabel(label), snapshot);
550 }
551
552 void CodeGeneratorX86Shared::bailout(LSnapshot* snapshot) {
553 Label label;
554 masm.jump(&label);
555 bailoutFrom(&label, snapshot);
556 }
557
558 void CodeGenerator::visitMinMaxD(LMinMaxD* ins) {
559 FloatRegister first = ToFloatRegister(ins->first());
560 FloatRegister second = ToFloatRegister(ins->second());
561 #ifdef DEBUG
562 FloatRegister output = ToFloatRegister(ins->output());
563 MOZ_ASSERT(first == output);
564 #endif
565
566 bool handleNaN = !ins->mir()->range() || ins->mir()->range()->canBeNaN();
567
568 if (ins->mir()->isMax()) {
569 masm.maxDouble(second, first, handleNaN);
570 } else {
571 masm.minDouble(second, first, handleNaN);
572 }
573 }
574
575 void CodeGenerator::visitMinMaxF(LMinMaxF* ins) {
576 FloatRegister first = ToFloatRegister(ins->first());
577 FloatRegister second = ToFloatRegister(ins->second());
578 #ifdef DEBUG
579 FloatRegister output = ToFloatRegister(ins->output());
580 MOZ_ASSERT(first == output);
581 #endif
582
583 bool handleNaN = !ins->mir()->range() || ins->mir()->range()->canBeNaN();
584
585 if (ins->mir()->isMax()) {
586 masm.maxFloat32(second, first, handleNaN);
587 } else {
588 masm.minFloat32(second, first, handleNaN);
589 }
590 }
591
592 void CodeGenerator::visitPowHalfD(LPowHalfD* ins) {
593 FloatRegister input = ToFloatRegister(ins->input());
594 FloatRegister output = ToFloatRegister(ins->output());
595
596 ScratchDoubleScope scratch(masm);
597
598 Label done, sqrt;
599
600 if (!ins->mir()->operandIsNeverNegativeInfinity()) {
601 // Branch if not -Infinity.
602 masm.loadConstantDouble(NegativeInfinity<double>(), scratch);
603
604 Assembler::DoubleCondition cond = Assembler::DoubleNotEqualOrUnordered;
605 if (ins->mir()->operandIsNeverNaN()) {
606 cond = Assembler::DoubleNotEqual;
607 }
608 masm.branchDouble(cond, input, scratch, &sqrt);
609
610 // Math.pow(-Infinity, 0.5) == Infinity.
611 masm.zeroDouble(output);
612 masm.subDouble(scratch, output);
613 masm.jump(&done);
614
615 masm.bind(&sqrt);
616 }
617
618 if (!ins->mir()->operandIsNeverNegativeZero()) {
619 // Math.pow(-0, 0.5) == 0 == Math.pow(0, 0.5).
620 // Adding 0 converts any -0 to 0.
621 masm.zeroDouble(scratch);
622 masm.addDouble(input, scratch);
623 masm.vsqrtsd(scratch, output, output);
624 } else {
625 masm.vsqrtsd(input, output, output);
626 }
627
628 masm.bind(&done);
629 }
630
631 void CodeGeneratorX86Shared::emitUndoALUOperationOOL(LInstruction* ins) {
632 Register reg = ToRegister(ins->getDef(0));
633
634 DebugOnly<LAllocation*> lhs = ins->getOperand(0);
635 LAllocation* rhs = ins->getOperand(1);
636
637 MOZ_ASSERT(reg == ToRegister(lhs));
638 MOZ_ASSERT_IF(rhs->isGeneralReg(), reg != ToRegister(rhs));
639
640 // Undo the effect of the ALU operation, which was performed on the output
641 // register and overflowed. Writing to the output register clobbered an
642 // input reg, and the original value of the input needs to be recovered
643 // to satisfy the constraint imposed by any RECOVERED_INPUT operands to
644 // the bailout snapshot.
645
646 if (rhs->isConstant()) {
647 Imm32 constant(ToInt32(rhs));
648 if (ins->isAddI()) {
649 masm.subl(constant, reg);
650 } else {
651 masm.addl(constant, reg);
652 }
653 } else {
654 if (ins->isAddI()) {
655 masm.subl(ToOperand(rhs), reg);
656 } else {
657 masm.addl(ToOperand(rhs), reg);
658 }
659 }
660
661 bailout(ins->snapshot());
662 }
663
664 void CodeGenerator::visitAddI(LAddI* ins) {
665 if (ins->rhs()->isConstant()) {
666 masm.addl(Imm32(ToInt32(ins->rhs())), ToOperand(ins->lhs()));
667 } else {
668 masm.addl(ToOperand(ins->rhs()), ToRegister(ins->lhs()));
669 }
670
671 if (ins->snapshot()) {
672 if (ins->recoversInput()) {
673 auto* ool = new (alloc()) LambdaOutOfLineCode(
674 [=](OutOfLineCode& ool) { emitUndoALUOperationOOL(ins); });
675 addOutOfLineCode(ool, ins->mir());
676 masm.j(Assembler::Overflow, ool->entry());
677 } else {
678 bailoutIf(Assembler::Overflow, ins->snapshot());
679 }
680 }
681 }
682
683 void CodeGenerator::visitAddI64(LAddI64* lir) {
684 LInt64Allocation lhs = lir->lhs();
685 LInt64Allocation rhs = lir->rhs();
686
687 MOZ_ASSERT(ToOutRegister64(lir) == ToRegister64(lhs));
688
689 if (IsConstant(rhs)) {
690 masm.add64(Imm64(ToInt64(rhs)), ToRegister64(lhs));
691 return;
692 }
693
694 masm.add64(ToOperandOrRegister64(rhs), ToRegister64(lhs));
695 }
696
697 void CodeGenerator::visitSubI(LSubI* ins) {
698 if (ins->rhs()->isConstant()) {
699 masm.subl(Imm32(ToInt32(ins->rhs())), ToOperand(ins->lhs()));
700 } else {
701 masm.subl(ToOperand(ins->rhs()), ToRegister(ins->lhs()));
702 }
703
704 if (ins->snapshot()) {
705 if (ins->recoversInput()) {
706 auto* ool = new (alloc()) LambdaOutOfLineCode(
707 [=](OutOfLineCode& ool) { emitUndoALUOperationOOL(ins); });
708 addOutOfLineCode(ool, ins->mir());
709 masm.j(Assembler::Overflow, ool->entry());
710 } else {
711 bailoutIf(Assembler::Overflow, ins->snapshot());
712 }
713 }
714 }
715
716 void CodeGenerator::visitSubI64(LSubI64* lir) {
717 LInt64Allocation lhs = lir->lhs();
718 LInt64Allocation rhs = lir->rhs();
719
720 MOZ_ASSERT(ToOutRegister64(lir) == ToRegister64(lhs));
721
722 if (IsConstant(rhs)) {
723 masm.sub64(Imm64(ToInt64(rhs)), ToRegister64(lhs));
724 return;
725 }
726
727 masm.sub64(ToOperandOrRegister64(rhs), ToRegister64(lhs));
728 }
729
730 void CodeGenerator::visitMulI(LMulI* ins) {
731 const LAllocation* lhs = ins->lhs();
732 const LAllocation* rhs = ins->rhs();
733 MMul* mul = ins->mir();
734 MOZ_ASSERT_IF(mul->mode() == MMul::Integer,
735 !mul->canBeNegativeZero() && !mul->canOverflow());
736
737 if (rhs->isConstant()) {
738 // Bailout on -0.0
739 int32_t constant = ToInt32(rhs);
740 if (mul->canBeNegativeZero() && constant <= 0) {
741 Assembler::Condition bailoutCond =
742 (constant == 0) ? Assembler::Signed : Assembler::Equal;
743 masm.test32(ToRegister(lhs), ToRegister(lhs));
744 bailoutIf(bailoutCond, ins->snapshot());
745 }
746
747 switch (constant) {
748 case -1:
749 masm.negl(ToOperand(lhs));
750 break;
751 case 0:
752 masm.xorl(ToOperand(lhs), ToRegister(lhs));
753 return; // escape overflow check;
754 case 1:
755 // nop
756 return; // escape overflow check;
757 case 2:
758 masm.addl(ToOperand(lhs), ToRegister(lhs));
759 break;
760 default:
761 if (!mul->canOverflow() && constant > 0) {
762 // Use shift if cannot overflow and constant is power of 2
763 int32_t shift = FloorLog2(constant);
764 if ((1 << shift) == constant) {
765 masm.shll(Imm32(shift), ToRegister(lhs));
766 return;
767 }
768 }
769 masm.imull(Imm32(ToInt32(rhs)), ToRegister(lhs));
770 }
771
772 // Bailout on overflow
773 if (mul->canOverflow()) {
774 bailoutIf(Assembler::Overflow, ins->snapshot());
775 }
776 } else {
777 masm.imull(ToOperand(rhs), ToRegister(lhs));
778
779 // Bailout on overflow
780 if (mul->canOverflow()) {
781 bailoutIf(Assembler::Overflow, ins->snapshot());
782 }
783
784 if (mul->canBeNegativeZero()) {
785 // Jump to an OOL path if the result is 0.
786 auto* ool = new (alloc()) LambdaOutOfLineCode([=](OutOfLineCode& ool) {
787 Register result = ToRegister(ins->output());
788 Operand lhsCopy = ToOperand(ins->lhsCopy());
789 Operand rhs = ToOperand(ins->rhs());
790 MOZ_ASSERT_IF(lhsCopy.kind() == Operand::REG,
791 lhsCopy.reg() != result.code());
792
793 // Result is -0 if lhs or rhs is negative.
794 masm.movl(lhsCopy, result);
795 masm.orl(rhs, result);
796 bailoutIf(Assembler::Signed, ins->snapshot());
797
798 masm.mov(ImmWord(0), result);
799 masm.jmp(ool.rejoin());
800 });
801 addOutOfLineCode(ool, mul);
802
803 masm.test32(ToRegister(lhs), ToRegister(lhs));
804 masm.j(Assembler::Zero, ool->entry());
805 masm.bind(ool->rejoin());
806 }
807 }
808 }
809
810 void CodeGenerator::visitMulI64(LMulI64* lir) {
811 LInt64Allocation lhs = lir->lhs();
812 LInt64Allocation rhs = lir->rhs();
813
814 MOZ_ASSERT(ToRegister64(lhs) == ToOutRegister64(lir));
815
816 if (IsConstant(rhs)) {
817 int64_t constant = ToInt64(rhs);
818 switch (constant) {
819 case -1:
820 masm.neg64(ToRegister64(lhs));
821 return;
822 case 0:
823 masm.xor64(ToRegister64(lhs), ToRegister64(lhs));
824 return;
825 case 1:
826 // nop
827 return;
828 case 2:
829 masm.add64(ToRegister64(lhs), ToRegister64(lhs));
830 return;
831 default:
832 if (constant > 0) {
833 // Use shift if constant is power of 2.
834 int32_t shift = mozilla::FloorLog2(constant);
835 if (int64_t(1) << shift == constant) {
836 masm.lshift64(Imm32(shift), ToRegister64(lhs));
837 return;
838 }
839 }
840 Register temp = ToTempRegisterOrInvalid(lir->temp0());
841 masm.mul64(Imm64(constant), ToRegister64(lhs), temp);
842 }
843 } else {
844 Register temp = ToTempRegisterOrInvalid(lir->temp0());
845 masm.mul64(ToOperandOrRegister64(rhs), ToRegister64(lhs), temp);
846 }
847 }
848
849 void CodeGenerator::visitUDivOrMod(LUDivOrMod* ins) {
850 Register lhs = ToRegister(ins->lhs());
851 Register rhs = ToRegister(ins->rhs());
852 Register output = ToRegister(ins->output());
853
854 MOZ_ASSERT_IF(lhs != rhs, rhs != eax);
855 MOZ_ASSERT(rhs != edx);
856 MOZ_ASSERT_IF(output == eax, ToRegister(ins->remainder()) == edx);
857
858 OutOfLineCode* ool = nullptr;
859
860 // Put the lhs in eax.
861 if (lhs != eax) {
862 masm.mov(lhs, eax);
863 }
864
865 // Prevent divide by zero.
866 if (ins->canBeDivideByZero()) {
867 masm.test32(rhs, rhs);
868 if (ins->mir()->isTruncated()) {
869 if (ins->trapOnError()) {
870 Label nonZero;
871 masm.j(Assembler::NonZero, &nonZero);
872 masm.wasmTrap(wasm::Trap::IntegerDivideByZero, ins->trapSiteDesc());
873 masm.bind(&nonZero);
874 } else {
875 ool = new (alloc()) LambdaOutOfLineCode([=](OutOfLineCode& ool) {
876 masm.mov(ImmWord(0), output);
877 masm.jmp(ool.rejoin());
878 });
879 masm.j(Assembler::Zero, ool->entry());
880 }
881 } else {
882 bailoutIf(Assembler::Zero, ins->snapshot());
883 }
884 }
885
886 // Zero extend the lhs into edx to make (edx:eax), since udiv is 64-bit.
887 masm.mov(ImmWord(0), edx);
888 masm.udiv(rhs);
889
890 // If the remainder is > 0, bailout since this must be a double.
891 if (ins->mir()->isDiv() && !ins->mir()->toDiv()->canTruncateRemainder()) {
892 Register remainder = ToRegister(ins->remainder());
893 masm.test32(remainder, remainder);
894 bailoutIf(Assembler::NonZero, ins->snapshot());
895 }
896
897 // Unsigned div or mod can return a value that's not a signed int32.
898 // If our users aren't expecting that, bail.
899 if (!ins->mir()->isTruncated()) {
900 masm.test32(output, output);
901 bailoutIf(Assembler::Signed, ins->snapshot());
902 }
903
904 if (ool) {
905 addOutOfLineCode(ool, ins->mir());
906 masm.bind(ool->rejoin());
907 }
908 }
909
910 void CodeGenerator::visitUDivOrModConstant(LUDivOrModConstant* ins) {
911 Register lhs = ToRegister(ins->numerator());
912 Register output = ToRegister(ins->output());
913 uint32_t d = ins->denominator();
914
915 // This emits the division answer into edx or the modulus answer into eax.
916 MOZ_ASSERT(output == eax || output == edx);
917 MOZ_ASSERT(lhs != eax && lhs != edx);
918 bool isDiv = (output == edx);
919
920 if (d == 0) {
921 if (ins->mir()->isTruncated()) {
922 if (ins->trapOnError()) {
923 masm.wasmTrap(wasm::Trap::IntegerDivideByZero, ins->trapSiteDesc());
924 } else {
925 masm.xorl(output, output);
926 }
927 } else {
928 bailout(ins->snapshot());
929 }
930 return;
931 }
932
933 // The denominator isn't a power of 2 (see LDivPowTwoI and LModPowTwoI).
934 MOZ_ASSERT((d & (d - 1)) != 0);
935
936 auto rmc = ReciprocalMulConstants::computeUnsignedDivisionConstants(d);
937
938 // We first compute (M * n) >> 32, where M = rmc.multiplier.
939 masm.movl(Imm32(rmc.multiplier), eax);
940 masm.umull(lhs);
941 if (rmc.multiplier > UINT32_MAX) {
942 // M >= 2^32 and shift == 0 is impossible, as d >= 2 implies that
943 // ((M * n) >> (32 + shift)) >= n > floor(n/d) whenever n >= d,
944 // contradicting the proof of correctness in computeDivisionConstants.
945 MOZ_ASSERT(rmc.shiftAmount > 0);
946 MOZ_ASSERT(rmc.multiplier < (int64_t(1) << 33));
947
948 // We actually computed edx = ((uint32_t(M) * n) >> 32) instead. Since
949 // (M * n) >> (32 + shift) is the same as (edx + n) >> shift, we can
950 // correct for the overflow. This case is a bit trickier than the signed
951 // case, though, as the (edx + n) addition itself can overflow; however,
952 // note that (edx + n) >> shift == (((n - edx) >> 1) + edx) >> (shift - 1),
953 // which is overflow-free. See Hacker's Delight, section 10-8 for details.
954
955 // Compute (n - edx) >> 1 into eax.
956 masm.movl(lhs, eax);
957 masm.subl(edx, eax);
958 masm.shrl(Imm32(1), eax);
959
960 // Finish the computation.
961 masm.addl(eax, edx);
962 masm.shrl(Imm32(rmc.shiftAmount - 1), edx);
963 } else {
964 masm.shrl(Imm32(rmc.shiftAmount), edx);
965 }
966
967 // We now have the truncated division value in edx. If we're
968 // computing a modulus or checking whether the division resulted
969 // in an integer, we need to multiply the obtained value by d and
970 // finish the computation/check.
971 if (!isDiv) {
972 masm.imull(Imm32(d), edx, edx);
973 masm.movl(lhs, eax);
974 masm.subl(edx, eax);
975
976 // The final result of the modulus op, just computed above by the
977 // sub instruction, can be a number in the range [2^31, 2^32). If
978 // this is the case and the modulus is not truncated, we must bail
979 // out.
980 if (!ins->mir()->isTruncated()) {
981 bailoutIf(Assembler::Signed, ins->snapshot());
982 }
983 } else if (!ins->mir()->isTruncated()) {
984 masm.imull(Imm32(d), edx, eax);
985 masm.cmpl(lhs, eax);
986 bailoutIf(Assembler::NotEqual, ins->snapshot());
987 }
988 }
989
990 void CodeGenerator::visitDivPowTwoI(LDivPowTwoI* ins) {
991 Register lhs = ToRegister(ins->numerator());
992 DebugOnly<Register> output = ToRegister(ins->output());
993
994 int32_t shift = ins->shift();
995 bool negativeDivisor = ins->negativeDivisor();
996 MDiv* mir = ins->mir();
997
998 // We use defineReuseInput so these should always be the same, which is
999 // convenient since all of our instructions here are two-address.
1000 MOZ_ASSERT(lhs == output);
1001
1002 if (!mir->isTruncated() && negativeDivisor) {
1003 // 0 divided by a negative number must return a double.
1004 masm.test32(lhs, lhs);
1005 bailoutIf(Assembler::Zero, ins->snapshot());
1006 }
1007
1008 if (shift) {
1009 if (!mir->isTruncated()) {
1010 // If the remainder is != 0, bailout since this must be a double.
1011 masm.test32(lhs, Imm32(UINT32_MAX >> (32 - shift)));
1012 bailoutIf(Assembler::NonZero, ins->snapshot());
1013 }
1014
1015 if (mir->isUnsigned()) {
1016 masm.shrl(Imm32(shift), lhs);
1017 } else {
1018 // Adjust the value so that shifting produces a correctly
1019 // rounded result when the numerator is negative. See 10-1
1020 // "Signed Division by a Known Power of 2" in Henry
1021 // S. Warren, Jr.'s Hacker's Delight.
1022 if (mir->canBeNegativeDividend() && mir->isTruncated()) {
1023 // Note: There is no need to execute this code, which handles how to
1024 // round the signed integer division towards 0, if we previously bailed
1025 // due to a non-zero remainder.
1026 Register lhsCopy = ToRegister(ins->numeratorCopy());
1027 MOZ_ASSERT(lhsCopy != lhs);
1028 if (shift > 1) {
1029 // Copy the sign bit of the numerator. (= (2^32 - 1) or 0)
1030 masm.sarl(Imm32(31), lhs);
1031 }
1032 // Divide by 2^(32 - shift)
1033 // i.e. (= (2^32 - 1) / 2^(32 - shift) or 0)
1034 // i.e. (= (2^shift - 1) or 0)
1035 masm.shrl(Imm32(32 - shift), lhs);
1036 // If signed, make any 1 bit below the shifted bits to bubble up, such
1037 // that once shifted the value would be rounded towards 0.
1038 masm.addl(lhsCopy, lhs);
1039 }
1040 masm.sarl(Imm32(shift), lhs);
1041
1042 if (negativeDivisor) {
1043 masm.negl(lhs);
1044 }
1045 }
1046 return;
1047 }
1048
1049 if (negativeDivisor) {
1050 // INT32_MIN / -1 overflows.
1051 masm.negl(lhs);
1052 if (!mir->isTruncated()) {
1053 bailoutIf(Assembler::Overflow, ins->snapshot());
1054 } else if (mir->trapOnError()) {
1055 Label ok;
1056 masm.j(Assembler::NoOverflow, &ok);
1057 masm.wasmTrap(wasm::Trap::IntegerOverflow, mir->trapSiteDesc());
1058 masm.bind(&ok);
1059 }
1060 } else if (mir->isUnsigned() && !mir->isTruncated()) {
1061 // Unsigned division by 1 can overflow if output is not
1062 // truncated.
1063 masm.test32(lhs, lhs);
1064 bailoutIf(Assembler::Signed, ins->snapshot());
1065 }
1066 }
1067
1068 void CodeGenerator::visitDivOrModConstantI(LDivOrModConstantI* ins) {
1069 Register lhs = ToRegister(ins->numerator());
1070 Register output = ToRegister(ins->output());
1071 int32_t d = ins->denominator();
1072
1073 // This emits the division answer into edx or the modulus answer into eax.
1074 MOZ_ASSERT(output == eax || output == edx);
1075 MOZ_ASSERT(lhs != eax && lhs != edx);
1076 bool isDiv = (output == edx);
1077
1078 // The absolute value of the denominator isn't a power of 2 (see LDivPowTwoI
1079 // and LModPowTwoI).
1080 MOZ_ASSERT((Abs(d) & (Abs(d) - 1)) != 0);
1081
1082 // We will first divide by Abs(d), and negate the answer if d is negative.
1083 // If desired, this can be avoided by generalizing computeDivisionConstants.
1084 auto rmc = ReciprocalMulConstants::computeSignedDivisionConstants(Abs(d));
1085
1086 // We first compute (M * n) >> 32, where M = rmc.multiplier.
1087 masm.movl(Imm32(rmc.multiplier), eax);
1088 masm.imull(lhs);
1089 if (rmc.multiplier > INT32_MAX) {
1090 MOZ_ASSERT(rmc.multiplier < (int64_t(1) << 32));
1091
1092 // We actually computed edx = ((int32_t(M) * n) >> 32) instead. Since
1093 // (M * n) >> 32 is the same as (edx + n), we can correct for the overflow.
1094 // (edx + n) can't overflow, as n and edx have opposite signs because
1095 // int32_t(M) is negative.
1096 masm.addl(lhs, edx);
1097 }
1098 // (M * n) >> (32 + shift) is the truncated division answer if n is
1099 // non-negative, as proved in the comments of computeDivisionConstants. We
1100 // must add 1 later if n is negative to get the right answer in all cases.
1101 masm.sarl(Imm32(rmc.shiftAmount), edx);
1102
1103 // We'll subtract -1 instead of adding 1, because (n < 0 ? -1 : 0) can be
1104 // computed with just a sign-extending shift of 31 bits.
1105 if (ins->canBeNegativeDividend()) {
1106 masm.movl(lhs, eax);
1107 masm.sarl(Imm32(31), eax);
1108 masm.subl(eax, edx);
1109 }
1110
1111 // After this, edx contains the correct truncated division result.
1112 if (d < 0) {
1113 masm.negl(edx);
1114 }
1115
1116 if (!isDiv) {
1117 masm.imull(Imm32(-d), edx, eax);
1118 masm.addl(lhs, eax);
1119 }
1120
1121 if (!ins->mir()->isTruncated()) {
1122 if (isDiv) {
1123 // This is a division op. Multiply the obtained value by d to check if
1124 // the correct answer is an integer. This cannot overflow, since |d| > 1.
1125 masm.imull(Imm32(d), edx, eax);
1126 masm.cmp32(lhs, eax);
1127 bailoutIf(Assembler::NotEqual, ins->snapshot());
1128
1129 // If lhs is zero and the divisor is negative, the answer should have
1130 // been -0.
1131 if (d < 0) {
1132 masm.test32(lhs, lhs);
1133 bailoutIf(Assembler::Zero, ins->snapshot());
1134 }
1135 } else if (ins->canBeNegativeDividend()) {
1136 // This is a mod op. If the computed value is zero and lhs
1137 // is negative, the answer should have been -0.
1138 Label done;
1139
1140 masm.cmp32(lhs, Imm32(0));
1141 masm.j(Assembler::GreaterThanOrEqual, &done);
1142
1143 masm.test32(eax, eax);
1144 bailoutIf(Assembler::Zero, ins->snapshot());
1145
1146 masm.bind(&done);
1147 }
1148 }
1149 }
1150
1151 void CodeGenerator::visitDivI(LDivI* ins) {
1152 Register remainder = ToRegister(ins->temp0());
1153 Register lhs = ToRegister(ins->lhs());
1154 Register rhs = ToRegister(ins->rhs());
1155 Register output = ToRegister(ins->output());
1156
1157 MDiv* mir = ins->mir();
1158
1159 MOZ_ASSERT_IF(lhs != rhs, rhs != eax);
1160 MOZ_ASSERT(rhs != edx);
1161 MOZ_ASSERT(remainder == edx);
1162 MOZ_ASSERT(output == eax);
1163
1164 Label done;
1165 OutOfLineCode* ool = nullptr;
1166
1167 // Put the lhs in eax, for either the negative overflow case or the regular
1168 // divide case.
1169 if (lhs != eax) {
1170 masm.mov(lhs, eax);
1171 }
1172
1173 // Handle divide by zero.
1174 if (mir->canBeDivideByZero()) {
1175 masm.test32(rhs, rhs);
1176 if (mir->trapOnError()) {
1177 Label nonZero;
1178 masm.j(Assembler::NonZero, &nonZero);
1179 masm.wasmTrap(wasm::Trap::IntegerDivideByZero, mir->trapSiteDesc());
1180 masm.bind(&nonZero);
1181 } else if (mir->canTruncateInfinities()) {
1182 // Truncated division by zero is zero (Infinity|0 == 0)
1183 if (!ool) {
1184 ool = new (alloc()) LambdaOutOfLineCode([=](OutOfLineCode& ool) {
1185 masm.mov(ImmWord(0), output);
1186 masm.jmp(ool.rejoin());
1187 });
1188 }
1189 masm.j(Assembler::Zero, ool->entry());
1190 } else {
1191 MOZ_ASSERT(mir->fallible());
1192 bailoutIf(Assembler::Zero, ins->snapshot());
1193 }
1194 }
1195
1196 // Handle an integer overflow exception from -2147483648 / -1.
1197 if (mir->canBeNegativeOverflow()) {
1198 Label notOverflow;
1199 masm.cmp32(lhs, Imm32(INT32_MIN));
1200 masm.j(Assembler::NotEqual, &notOverflow);
1201 masm.cmp32(rhs, Imm32(-1));
1202 if (mir->trapOnError()) {
1203 masm.j(Assembler::NotEqual, &notOverflow);
1204 masm.wasmTrap(wasm::Trap::IntegerOverflow, mir->trapSiteDesc());
1205 } else if (mir->canTruncateOverflow()) {
1206 // (-INT32_MIN)|0 == INT32_MIN and INT32_MIN is already in the
1207 // output register (lhs == eax).
1208 masm.j(Assembler::Equal, &done);
1209 } else {
1210 MOZ_ASSERT(mir->fallible());
1211 bailoutIf(Assembler::Equal, ins->snapshot());
1212 }
1213 masm.bind(&notOverflow);
1214 }
1215
1216 // Handle negative 0.
1217 if (!mir->canTruncateNegativeZero() && mir->canBeNegativeZero()) {
1218 Label nonzero;
1219 masm.test32(lhs, lhs);
1220 masm.j(Assembler::NonZero, &nonzero);
1221 masm.cmp32(rhs, Imm32(0));
1222 bailoutIf(Assembler::LessThan, ins->snapshot());
1223 masm.bind(&nonzero);
1224 }
1225
1226 // Sign extend the lhs into edx to make (edx:eax), since idiv is 64-bit.
1227 if (lhs != eax) {
1228 masm.mov(lhs, eax);
1229 }
1230 masm.cdq();
1231 masm.idiv(rhs);
1232
1233 if (!mir->canTruncateRemainder()) {
1234 // If the remainder is > 0, bailout since this must be a double.
1235 masm.test32(remainder, remainder);
1236 bailoutIf(Assembler::NonZero, ins->snapshot());
1237 }
1238
1239 masm.bind(&done);
1240
1241 if (ool) {
1242 addOutOfLineCode(ool, mir);
1243 masm.bind(ool->rejoin());
1244 }
1245 }
1246
1247 void CodeGenerator::visitModPowTwoI(LModPowTwoI* ins) {
1248 Register lhs = ToRegister(ins->input());
1249 int32_t shift = ins->shift();
1250
1251 Label negative;
1252
1253 if (!ins->mir()->isUnsigned() && ins->mir()->canBeNegativeDividend()) {
1254 // Switch based on sign of the lhs.
1255 // Positive numbers are just a bitmask
1256 masm.branchTest32(Assembler::Signed, lhs, lhs, &negative);
1257 }
1258
1259 masm.andl(Imm32((uint32_t(1) << shift) - 1), lhs);
1260
1261 if (!ins->mir()->isUnsigned() && ins->mir()->canBeNegativeDividend()) {
1262 Label done;
1263 masm.jump(&done);
1264
1265 // Negative numbers need a negate, bitmask, negate
1266 masm.bind(&negative);
1267
1268 // Unlike in the visitModI case, we are not computing the mod by means of a
1269 // division. Therefore, the divisor = -1 case isn't problematic (the andl
1270 // always returns 0, which is what we expect).
1271 //
1272 // The negl instruction overflows if lhs == INT32_MIN, but this is also not
1273 // a problem: shift is at most 31, and so the andl also always returns 0.
1274 masm.negl(lhs);
1275 masm.andl(Imm32((uint32_t(1) << shift) - 1), lhs);
1276 masm.negl(lhs);
1277
1278 // Since a%b has the same sign as b, and a is negative in this branch,
1279 // an answer of 0 means the correct result is actually -0. Bail out.
1280 if (!ins->mir()->isTruncated()) {
1281 bailoutIf(Assembler::Zero, ins->snapshot());
1282 }
1283 masm.bind(&done);
1284 }
1285 }
1286
1287 class ModOverflowCheck : public OutOfLineCodeBase<CodeGeneratorX86Shared> {
1288 Label done_;
1289 LModI* ins_;
1290 Register rhs_;
1291
1292 public:
1293 explicit ModOverflowCheck(LModI* ins, Register rhs) : ins_(ins), rhs_(rhs) {}
1294
1295 virtual void accept(CodeGeneratorX86Shared* codegen) override {
1296 codegen->visitModOverflowCheck(this);
1297 }
1298 Label* done() { return &done_; }
1299 LModI* ins() const { return ins_; }
1300 Register rhs() const { return rhs_; }
1301 };
1302
1303 void CodeGeneratorX86Shared::visitModOverflowCheck(ModOverflowCheck* ool) {
1304 masm.cmp32(ool->rhs(), Imm32(-1));
1305 if (ool->ins()->mir()->isTruncated()) {
1306 masm.j(Assembler::NotEqual, ool->rejoin());
1307 masm.mov(ImmWord(0), edx);
1308 masm.jmp(ool->done());
1309 } else {
1310 bailoutIf(Assembler::Equal, ool->ins()->snapshot());
1311 masm.jmp(ool->rejoin());
1312 }
1313 }
1314
1315 void CodeGenerator::visitModI(LModI* ins) {
1316 Register remainder = ToRegister(ins->output());
1317 Register lhs = ToRegister(ins->lhs());
1318 Register rhs = ToRegister(ins->rhs());
1319
1320 // Required to use idiv.
1321 MOZ_ASSERT_IF(lhs != rhs, rhs != eax);
1322 MOZ_ASSERT(rhs != edx);
1323 MOZ_ASSERT(remainder == edx);
1324 MOZ_ASSERT(ToRegister(ins->temp0()) == eax);
1325
1326 Label done;
1327 OutOfLineCode* ool = nullptr;
1328 ModOverflowCheck* overflow = nullptr;
1329
1330 // Set up eax in preparation for doing a div.
1331 if (lhs != eax) {
1332 masm.mov(lhs, eax);
1333 }
1334
1335 MMod* mir = ins->mir();
1336
1337 // Prevent divide by zero.
1338 if (mir->canBeDivideByZero()) {
1339 masm.test32(rhs, rhs);
1340 if (mir->isTruncated()) {
1341 if (mir->trapOnError()) {
1342 Label nonZero;
1343 masm.j(Assembler::NonZero, &nonZero);
1344 masm.wasmTrap(wasm::Trap::IntegerDivideByZero, mir->trapSiteDesc());
1345 masm.bind(&nonZero);
1346 } else {
1347 if (!ool) {
1348 ool = new (alloc()) LambdaOutOfLineCode([=](OutOfLineCode& ool) {
1349 masm.mov(ImmWord(0), edx);
1350 masm.jmp(ool.rejoin());
1351 });
1352 }
1353 masm.j(Assembler::Zero, ool->entry());
1354 }
1355 } else {
1356 bailoutIf(Assembler::Zero, ins->snapshot());
1357 }
1358 }
1359
1360 Label negative;
1361
1362 // Switch based on sign of the lhs.
1363 if (mir->canBeNegativeDividend()) {
1364 masm.branchTest32(Assembler::Signed, lhs, lhs, &negative);
1365 }
1366
1367 // If lhs >= 0 then remainder = lhs % rhs. The remainder must be positive.
1368 {
1369 // Check if rhs is a power-of-two.
1370 if (mir->canBePowerOfTwoDivisor()) {
1371 MOZ_ASSERT(rhs != remainder);
1372
1373 // Rhs y is a power-of-two if (y & (y-1)) == 0. Note that if
1374 // y is any negative number other than INT32_MIN, both y and
1375 // y-1 will have the sign bit set so these are never optimized
1376 // as powers-of-two. If y is INT32_MIN, y-1 will be INT32_MAX
1377 // and because lhs >= 0 at this point, lhs & INT32_MAX returns
1378 // the correct value.
1379 Label notPowerOfTwo;
1380 masm.mov(rhs, remainder);
1381 masm.subl(Imm32(1), remainder);
1382 masm.branchTest32(Assembler::NonZero, remainder, rhs, &notPowerOfTwo);
1383 {
1384 masm.andl(lhs, remainder);
1385 masm.jmp(&done);
1386 }
1387 masm.bind(&notPowerOfTwo);
1388 }
1389
1390 // Since lhs >= 0, the sign-extension will be 0
1391 masm.mov(ImmWord(0), edx);
1392 masm.idiv(rhs);
1393 }
1394
1395 // Otherwise, we have to beware of two special cases:
1396 if (mir->canBeNegativeDividend()) {
1397 masm.jump(&done);
1398
1399 masm.bind(&negative);
1400
1401 // Prevent an integer overflow exception from -2147483648 % -1
1402 masm.cmp32(lhs, Imm32(INT32_MIN));
1403 overflow = new (alloc()) ModOverflowCheck(ins, rhs);
1404 masm.j(Assembler::Equal, overflow->entry());
1405 masm.bind(overflow->rejoin());
1406 masm.cdq();
1407 masm.idiv(rhs);
1408
1409 if (!mir->isTruncated()) {
1410 // A remainder of 0 means that the rval must be -0, which is a double.
1411 masm.test32(remainder, remainder);
1412 bailoutIf(Assembler::Zero, ins->snapshot());
1413 }
1414 }
1415
1416 masm.bind(&done);
1417
1418 if (overflow) {
1419 addOutOfLineCode(overflow, mir);
1420 masm.bind(overflow->done());
1421 }
1422
1423 if (ool) {
1424 addOutOfLineCode(ool, mir);
1425 masm.bind(ool->rejoin());
1426 }
1427 }
1428
1429 void CodeGenerator::visitBitNotI(LBitNotI* ins) {
1430 const LAllocation* input = ins->input();
1431 MOZ_ASSERT(!input->isConstant());
1432
1433 masm.notl(ToOperand(input));
1434 }
1435
1436 void CodeGenerator::visitBitOpI(LBitOpI* ins) {
1437 const LAllocation* lhs = ins->lhs();
1438 const LAllocation* rhs = ins->rhs();
1439
1440 switch (ins->bitop()) {
1441 case JSOp::BitOr:
1442 if (rhs->isConstant()) {
1443 masm.orl(Imm32(ToInt32(rhs)), ToOperand(lhs));
1444 } else {
1445 masm.orl(ToOperand(rhs), ToRegister(lhs));
1446 }
1447 break;
1448 case JSOp::BitXor:
1449 if (rhs->isConstant()) {
1450 masm.xorl(Imm32(ToInt32(rhs)), ToOperand(lhs));
1451 } else {
1452 masm.xorl(ToOperand(rhs), ToRegister(lhs));
1453 }
1454 break;
1455 case JSOp::BitAnd:
1456 if (rhs->isConstant()) {
1457 masm.andl(Imm32(ToInt32(rhs)), ToOperand(lhs));
1458 } else {
1459 masm.andl(ToOperand(rhs), ToRegister(lhs));
1460 }
1461 break;
1462 default:
1463 MOZ_CRASH("unexpected binary opcode");
1464 }
1465 }
1466
1467 void CodeGenerator::visitBitOpI64(LBitOpI64* lir) {
1468 LInt64Allocation lhs = lir->lhs();
1469 LInt64Allocation rhs = lir->rhs();
1470
1471 MOZ_ASSERT(ToOutRegister64(lir) == ToRegister64(lhs));
1472
1473 switch (lir->bitop()) {
1474 case JSOp::BitOr:
1475 if (IsConstant(rhs)) {
1476 masm.or64(Imm64(ToInt64(rhs)), ToRegister64(lhs));
1477 } else {
1478 masm.or64(ToOperandOrRegister64(rhs), ToRegister64(lhs));
1479 }
1480 break;
1481 case JSOp::BitXor:
1482 if (IsConstant(rhs)) {
1483 masm.xor64(Imm64(ToInt64(rhs)), ToRegister64(lhs));
1484 } else {
1485 masm.xor64(ToOperandOrRegister64(rhs), ToRegister64(lhs));
1486 }
1487 break;
1488 case JSOp::BitAnd:
1489 if (IsConstant(rhs)) {
1490 masm.and64(Imm64(ToInt64(rhs)), ToRegister64(lhs));
1491 } else {
1492 masm.and64(ToOperandOrRegister64(rhs), ToRegister64(lhs));
1493 }
1494 break;
1495 default:
1496 MOZ_CRASH("unexpected binary opcode");
1497 }
1498 }
1499
1500 void CodeGenerator::visitShiftI(LShiftI* ins) {
1501 Register lhs = ToRegister(ins->lhs());
1502 const LAllocation* rhs = ins->rhs();
1503
1504 if (rhs->isConstant()) {
1505 int32_t shift = ToInt32(rhs) & 0x1F;
1506 switch (ins->bitop()) {
1507 case JSOp::Lsh:
1508 if (shift) {
1509 masm.lshift32(Imm32(shift), lhs);
1510 }
1511 break;
1512 case JSOp::Rsh:
1513 if (shift) {
1514 masm.rshift32Arithmetic(Imm32(shift), lhs);
1515 }
1516 break;
1517 case JSOp::Ursh:
1518 if (shift) {
1519 masm.rshift32(Imm32(shift), lhs);
1520 } else if (ins->mir()->toUrsh()->fallible()) {
1521 // x >>> 0 can overflow.
1522 masm.test32(lhs, lhs);
1523 bailoutIf(Assembler::Signed, ins->snapshot());
1524 }
1525 break;
1526 default:
1527 MOZ_CRASH("Unexpected shift op");
1528 }
1529 } else {
1530 Register shift = ToRegister(rhs);
1531 switch (ins->bitop()) {
1532 case JSOp::Lsh:
1533 masm.lshift32(shift, lhs);
1534 break;
1535 case JSOp::Rsh:
1536 masm.rshift32Arithmetic(shift, lhs);
1537 break;
1538 case JSOp::Ursh:
1539 masm.rshift32(shift, lhs);
1540 if (ins->mir()->toUrsh()->fallible()) {
1541 // x >>> 0 can overflow.
1542 masm.test32(lhs, lhs);
1543 bailoutIf(Assembler::Signed, ins->snapshot());
1544 }
1545 break;
1546 default:
1547 MOZ_CRASH("Unexpected shift op");
1548 }
1549 }
1550 }
1551
1552 void CodeGenerator::visitShiftI64(LShiftI64* lir) {
1553 LInt64Allocation lhs = lir->lhs();
1554 const LAllocation* rhs = lir->rhs();
1555
1556 MOZ_ASSERT(ToOutRegister64(lir) == ToRegister64(lhs));
1557
1558 if (rhs->isConstant()) {
1559 int32_t shift = int32_t(rhs->toConstant()->toInt64() & 0x3F);
1560 switch (lir->bitop()) {
1561 case JSOp::Lsh:
1562 if (shift) {
1563 masm.lshift64(Imm32(shift), ToRegister64(lhs));
1564 }
1565 break;
1566 case JSOp::Rsh:
1567 if (shift) {
1568 masm.rshift64Arithmetic(Imm32(shift), ToRegister64(lhs));
1569 }
1570 break;
1571 case JSOp::Ursh:
1572 if (shift) {
1573 masm.rshift64(Imm32(shift), ToRegister64(lhs));
1574 }
1575 break;
1576 default:
1577 MOZ_CRASH("Unexpected shift op");
1578 }
1579 return;
1580 }
1581
1582 Register shift = ToRegister(rhs);
1583 #ifdef JS_CODEGEN_X86
1584 MOZ_ASSERT(shift == ecx);
1585 #endif
1586 switch (lir->bitop()) {
1587 case JSOp::Lsh:
1588 masm.lshift64(shift, ToRegister64(lhs));
1589 break;
1590 case JSOp::Rsh:
1591 masm.rshift64Arithmetic(shift, ToRegister64(lhs));
1592 break;
1593 case JSOp::Ursh:
1594 masm.rshift64(shift, ToRegister64(lhs));
1595 break;
1596 default:
1597 MOZ_CRASH("Unexpected shift op");
1598 }
1599 }
1600
1601 void CodeGenerator::visitUrshD(LUrshD* ins) {
1602 Register lhs = ToRegister(ins->lhs());
1603 MOZ_ASSERT(ToRegister(ins->temp0()) == lhs);
1604
1605 const LAllocation* rhs = ins->rhs();
1606 FloatRegister out = ToFloatRegister(ins->output());
1607
1608 if (rhs->isConstant()) {
1609 int32_t shift = ToInt32(rhs) & 0x1F;
1610 if (shift) {
1611 masm.shrl(Imm32(shift), lhs);
1612 }
1613 } else {
1614 Register shift = ToRegister(rhs);
1615 masm.rshift32(shift, lhs);
1616 }
1617
1618 masm.convertUInt32ToDouble(lhs, out);
1619 }
1620
1621 Operand CodeGeneratorX86Shared::ToOperand(const LAllocation& a) {
1622 if (a.isGeneralReg()) {
1623 return Operand(a.toGeneralReg()->reg());
1624 }
1625 if (a.isFloatReg()) {
1626 return Operand(a.toFloatReg()->reg());
1627 }
1628 return Operand(ToAddress(a));
1629 }
1630
1631 Operand CodeGeneratorX86Shared::ToOperand(const LAllocation* a) {
1632 return ToOperand(*a);
1633 }
1634
1635 Operand CodeGeneratorX86Shared::ToOperand(const LDefinition* def) {
1636 return ToOperand(def->output());
1637 }
1638
1639 MoveOperand CodeGeneratorX86Shared::toMoveOperand(LAllocation a) const {
1640 if (a.isGeneralReg()) {
1641 return MoveOperand(ToRegister(a));
1642 }
1643 if (a.isFloatReg()) {
1644 return MoveOperand(ToFloatRegister(a));
1645 }
1646 MoveOperand::Kind kind = a.isStackArea() ? MoveOperand::Kind::EffectiveAddress
1647 : MoveOperand::Kind::Memory;
1648 return MoveOperand(ToAddress(a), kind);
1649 }
1650
1651 class OutOfLineTableSwitch : public OutOfLineCodeBase<CodeGeneratorX86Shared> {
1652 MTableSwitch* mir_;
1653 CodeLabel jumpLabel_;
1654
1655 void accept(CodeGeneratorX86Shared* codegen) override {
1656 codegen->visitOutOfLineTableSwitch(this);
1657 }
1658
1659 public:
1660 explicit OutOfLineTableSwitch(MTableSwitch* mir) : mir_(mir) {}
1661
1662 MTableSwitch* mir() const { return mir_; }
1663
1664 CodeLabel* jumpLabel() { return &jumpLabel_; }
1665 };
1666
1667 void CodeGeneratorX86Shared::visitOutOfLineTableSwitch(
1668 OutOfLineTableSwitch* ool) {
1669 MTableSwitch* mir = ool->mir();
1670
1671 masm.haltingAlign(sizeof(void*));
1672 masm.bind(ool->jumpLabel());
1673 masm.addCodeLabel(*ool->jumpLabel());
1674
1675 for (size_t i = 0; i < mir->numCases(); i++) {
1676 LBlock* caseblock = skipTrivialBlocks(mir->getCase(i))->lir();
1677 Label* caseheader = caseblock->label();
1678 uint32_t caseoffset = caseheader->offset();
1679
1680 // The entries of the jump table need to be absolute addresses and thus
1681 // must be patched after codegen is finished.
1682 CodeLabel cl;
1683 masm.writeCodePointer(&cl);
1684 cl.target()->bind(caseoffset);
1685 masm.addCodeLabel(cl);
1686 }
1687 }
1688
1689 void CodeGeneratorX86Shared::emitTableSwitchDispatch(MTableSwitch* mir,
1690 Register index,
1691 Register base) {
1692 Label* defaultcase = skipTrivialBlocks(mir->getDefault())->lir()->label();
1693
1694 // Lower value with low value
1695 if (mir->low() != 0) {
1696 masm.subl(Imm32(mir->low()), index);
1697 }
1698
1699 // Jump to default case if input is out of range
1700 int32_t cases = mir->numCases();
1701 masm.cmp32(index, Imm32(cases));
1702 masm.j(AssemblerX86Shared::AboveOrEqual, defaultcase);
1703
1704 // To fill in the CodeLabels for the case entries, we need to first
1705 // generate the case entries (we don't yet know their offsets in the
1706 // instruction stream).
1707 OutOfLineTableSwitch* ool = new (alloc()) OutOfLineTableSwitch(mir);
1708 addOutOfLineCode(ool, mir);
1709
1710 // Compute the position where a pointer to the right case stands.
1711 masm.mov(ool->jumpLabel(), base);
1712 BaseIndex pointer(base, index, ScalePointer);
1713
1714 // Jump to the right case
1715 masm.branchToComputedAddress(pointer);
1716 }
1717
1718 void CodeGenerator::visitMathD(LMathD* math) {
1719 FloatRegister lhs = ToFloatRegister(math->lhs());
1720 Operand rhs = ToOperand(math->rhs());
1721 FloatRegister output = ToFloatRegister(math->output());
1722
1723 switch (math->jsop()) {
1724 case JSOp::Add:
1725 masm.vaddsd(rhs, lhs, output);
1726 break;
1727 case JSOp::Sub:
1728 masm.vsubsd(rhs, lhs, output);
1729 break;
1730 case JSOp::Mul:
1731 masm.vmulsd(rhs, lhs, output);
1732 break;
1733 case JSOp::Div:
1734 masm.vdivsd(rhs, lhs, output);
1735 break;
1736 default:
1737 MOZ_CRASH("unexpected opcode");
1738 }
1739 }
1740
1741 void CodeGenerator::visitMathF(LMathF* math) {
1742 FloatRegister lhs = ToFloatRegister(math->lhs());
1743 Operand rhs = ToOperand(math->rhs());
1744 FloatRegister output = ToFloatRegister(math->output());
1745
1746 switch (math->jsop()) {
1747 case JSOp::Add:
1748 masm.vaddss(rhs, lhs, output);
1749 break;
1750 case JSOp::Sub:
1751 masm.vsubss(rhs, lhs, output);
1752 break;
1753 case JSOp::Mul:
1754 masm.vmulss(rhs, lhs, output);
1755 break;
1756 case JSOp::Div:
1757 masm.vdivss(rhs, lhs, output);
1758 break;
1759 default:
1760 MOZ_CRASH("unexpected opcode");
1761 }
1762 }
1763
1764 void CodeGenerator::visitNearbyInt(LNearbyInt* lir) {
1765 FloatRegister input = ToFloatRegister(lir->input());
1766 FloatRegister output = ToFloatRegister(lir->output());
1767
1768 RoundingMode roundingMode = lir->mir()->roundingMode();
1769 masm.nearbyIntDouble(roundingMode, input, output);
1770 }
1771
1772 void CodeGenerator::visitNearbyIntF(LNearbyIntF* lir) {
1773 FloatRegister input = ToFloatRegister(lir->input());
1774 FloatRegister output = ToFloatRegister(lir->output());
1775
1776 RoundingMode roundingMode = lir->mir()->roundingMode();
1777 masm.nearbyIntFloat32(roundingMode, input, output);
1778 }
1779
1780 void CodeGenerator::visitEffectiveAddress(LEffectiveAddress* ins) {
1781 const MEffectiveAddress* mir = ins->mir();
1782 Register base = ToRegister(ins->base());
1783 Register index = ToRegister(ins->index());
1784 Register output = ToRegister(ins->output());
1785 masm.leal(Operand(base, index, mir->scale(), mir->displacement()), output);
1786 }
1787
1788 void CodeGeneratorX86Shared::generateInvalidateEpilogue() {
1789 // Ensure that there is enough space in the buffer for the OsiPoint
1790 // patching to occur. Otherwise, we could overwrite the invalidation
1791 // epilogue.
1792 for (size_t i = 0; i < sizeof(void*); i += Assembler::NopSize()) {
1793 masm.nop();
1794 }
1795
1796 masm.bind(&invalidate_);
1797
1798 // Push the Ion script onto the stack (when we determine what that pointer
1799 // is).
1800 invalidateEpilogueData_ = masm.pushWithPatch(ImmWord(uintptr_t(-1)));
1801
1802 // Jump to the invalidator which will replace the current frame.
1803 TrampolinePtr thunk = gen->jitRuntime()->getInvalidationThunk();
1804 masm.jump(thunk);
1805 }
1806
1807 void CodeGenerator::visitNegI(LNegI* ins) {
1808 Register input = ToRegister(ins->input());
1809 MOZ_ASSERT(input == ToRegister(ins->output()));
1810
1811 masm.neg32(input);
1812 }
1813
1814 void CodeGenerator::visitNegI64(LNegI64* ins) {
1815 Register64 input = ToRegister64(ins->input());
1816 MOZ_ASSERT(input == ToOutRegister64(ins));
1817 masm.neg64(input);
1818 }
1819
1820 void CodeGenerator::visitNegD(LNegD* ins) {
1821 FloatRegister input = ToFloatRegister(ins->input());
1822 MOZ_ASSERT(input == ToFloatRegister(ins->output()));
1823
1824 masm.negateDouble(input);
1825 }
1826
1827 void CodeGenerator::visitNegF(LNegF* ins) {
1828 FloatRegister input = ToFloatRegister(ins->input());
1829 MOZ_ASSERT(input == ToFloatRegister(ins->output()));
1830
1831 masm.negateFloat(input);
1832 }
1833
1834 void CodeGenerator::visitCompareExchangeTypedArrayElement(
1835 LCompareExchangeTypedArrayElement* lir) {
1836 Register elements = ToRegister(lir->elements());
1837 AnyRegister output = ToAnyRegister(lir->output());
1838 Register temp = ToTempRegisterOrInvalid(lir->temp0());
1839
1840 Register oldval = ToRegister(lir->oldval());
1841 Register newval = ToRegister(lir->newval());
1842
1843 Scalar::Type arrayType = lir->mir()->arrayType();
1844
1845 if (lir->index()->isConstant()) {
1846 Address dest = ToAddress(elements, lir->index(), arrayType);
1847 masm.compareExchangeJS(arrayType, Synchronization::Full(), dest, oldval,
1848 newval, temp, output);
1849 } else {
1850 BaseIndex dest(elements, ToRegister(lir->index()),
1851 ScaleFromScalarType(arrayType));
1852 masm.compareExchangeJS(arrayType, Synchronization::Full(), dest, oldval,
1853 newval, temp, output);
1854 }
1855 }
1856
1857 void CodeGenerator::visitAtomicExchangeTypedArrayElement(
1858 LAtomicExchangeTypedArrayElement* lir) {
1859 Register elements = ToRegister(lir->elements());
1860 AnyRegister output = ToAnyRegister(lir->output());
1861 Register temp = ToTempRegisterOrInvalid(lir->temp0());
1862
1863 Register value = ToRegister(lir->value());
1864
1865 Scalar::Type arrayType = lir->mir()->arrayType();
1866
1867 if (lir->index()->isConstant()) {
1868 Address dest = ToAddress(elements, lir->index(), arrayType);
1869 masm.atomicExchangeJS(arrayType, Synchronization::Full(), dest, value, temp,
1870 output);
1871 } else {
1872 BaseIndex dest(elements, ToRegister(lir->index()),
1873 ScaleFromScalarType(arrayType));
1874 masm.atomicExchangeJS(arrayType, Synchronization::Full(), dest, value, temp,
1875 output);
1876 }
1877 }
1878
1879 template <typename T>
1880 static inline void AtomicBinopToTypedArray(MacroAssembler& masm, AtomicOp op,
1881 Scalar::Type arrayType,
1882 const LAllocation* value,
1883 const T& mem, Register temp1,
1884 Register temp2, AnyRegister output) {
1885 if (value->isConstant()) {
1886 masm.atomicFetchOpJS(arrayType, Synchronization::Full(), op,
1887 Imm32(ToInt32(value)), mem, temp1, temp2, output);
1888 } else {
1889 masm.atomicFetchOpJS(arrayType, Synchronization::Full(), op,
1890 ToRegister(value), mem, temp1, temp2, output);
1891 }
1892 }
1893
1894 void CodeGenerator::visitAtomicTypedArrayElementBinop(
1895 LAtomicTypedArrayElementBinop* lir) {
1896 MOZ_ASSERT(!lir->mir()->isForEffect());
1897
1898 AnyRegister output = ToAnyRegister(lir->output());
1899 Register elements = ToRegister(lir->elements());
1900 Register temp1 = ToTempRegisterOrInvalid(lir->temp0());
1901 Register temp2 = ToTempRegisterOrInvalid(lir->temp1());
1902 const LAllocation* value = lir->value();
1903
1904 Scalar::Type arrayType = lir->mir()->arrayType();
1905
1906 if (lir->index()->isConstant()) {
1907 Address mem = ToAddress(elements, lir->index(), arrayType);
1908 AtomicBinopToTypedArray(masm, lir->mir()->operation(), arrayType, value,
1909 mem, temp1, temp2, output);
1910 } else {
1911 BaseIndex mem(elements, ToRegister(lir->index()),
1912 ScaleFromScalarType(arrayType));
1913 AtomicBinopToTypedArray(masm, lir->mir()->operation(), arrayType, value,
1914 mem, temp1, temp2, output);
1915 }
1916 }
1917
1918 template <typename T>
1919 static inline void AtomicBinopToTypedArray(MacroAssembler& masm,
1920 Scalar::Type arrayType, AtomicOp op,
1921 const LAllocation* value,
1922 const T& mem) {
1923 if (value->isConstant()) {
1924 masm.atomicEffectOpJS(arrayType, Synchronization::Full(), op,
1925 Imm32(ToInt32(value)), mem, InvalidReg);
1926 } else {
1927 masm.atomicEffectOpJS(arrayType, Synchronization::Full(), op,
1928 ToRegister(value), mem, InvalidReg);
1929 }
1930 }
1931
1932 void CodeGenerator::visitAtomicTypedArrayElementBinopForEffect(
1933 LAtomicTypedArrayElementBinopForEffect* lir) {
1934 MOZ_ASSERT(lir->mir()->isForEffect());
1935
1936 Register elements = ToRegister(lir->elements());
1937 const LAllocation* value = lir->value();
1938 Scalar::Type arrayType = lir->mir()->arrayType();
1939
1940 if (lir->index()->isConstant()) {
1941 Address mem = ToAddress(elements, lir->index(), arrayType);
1942 AtomicBinopToTypedArray(masm, arrayType, lir->mir()->operation(), value,
1943 mem);
1944 } else {
1945 BaseIndex mem(elements, ToRegister(lir->index()),
1946 ScaleFromScalarType(arrayType));
1947 AtomicBinopToTypedArray(masm, arrayType, lir->mir()->operation(), value,
1948 mem);
1949 }
1950 }
1951
1952 void CodeGeneratorX86Shared::visitOutOfLineWasmTruncateCheck(
1953 OutOfLineWasmTruncateCheck* ool) {
1954 FloatRegister input = ool->input();
1955 Register output = ool->output();
1956 Register64 output64 = ool->output64();
1957 MIRType fromType = ool->fromType();
1958 MIRType toType = ool->toType();
1959 Label* oolRejoin = ool->rejoin();
1960 TruncFlags flags = ool->flags();
1961 const wasm::TrapSiteDesc& trapSiteDesc = ool->trapSiteDesc();
1962
1963 if (fromType == MIRType::Float32) {
1964 if (toType == MIRType::Int32) {
1965 masm.oolWasmTruncateCheckF32ToI32(input, output, flags, trapSiteDesc,
1966 oolRejoin);
1967 } else if (toType == MIRType::Int64) {
1968 masm.oolWasmTruncateCheckF32ToI64(input, output64, flags, trapSiteDesc,
1969 oolRejoin);
1970 } else {
1971 MOZ_CRASH("unexpected type");
1972 }
1973 } else if (fromType == MIRType::Double) {
1974 if (toType == MIRType::Int32) {
1975 masm.oolWasmTruncateCheckF64ToI32(input, output, flags, trapSiteDesc,
1976 oolRejoin);
1977 } else if (toType == MIRType::Int64) {
1978 masm.oolWasmTruncateCheckF64ToI64(input, output64, flags, trapSiteDesc,
1979 oolRejoin);
1980 } else {
1981 MOZ_CRASH("unexpected type");
1982 }
1983 } else {
1984 MOZ_CRASH("unexpected type");
1985 }
1986 }
1987
1988 template <typename T>
1989 Operand CodeGeneratorX86Shared::toMemoryAccessOperand(T* lir, int32_t disp) {
1990 const LAllocation* ptr = lir->ptr();
1991 const LAllocation* memoryBase = lir->memoryBase();
1992 #ifdef JS_CODEGEN_X86
1993 Operand destAddr = ptr->isBogus() ? Operand(ToRegister(memoryBase), disp)
1994 : Operand(ToRegister(memoryBase),
1995 ToRegister(ptr), TimesOne, disp);
1996 #else
1997 auto baseReg = memoryBase->isBogus() ? HeapReg : ToRegister(memoryBase);
1998 Operand destAddr = ptr->isBogus()
1999 ? Operand(baseReg, disp)
2000 : Operand(baseReg, ToRegister(ptr), TimesOne, disp);
2001 #endif
2002 return destAddr;
2003 }
2004
2005 void CodeGenerator::visitCopySignF(LCopySignF* lir) {
2006 FloatRegister lhs = ToFloatRegister(lir->lhs());
2007 FloatRegister rhs = ToFloatRegister(lir->rhs());
2008
2009 FloatRegister out = ToFloatRegister(lir->output());
2010
2011 if (lhs == rhs) {
2012 if (lhs != out) {
2013 masm.moveFloat32(lhs, out);
2014 }
2015 return;
2016 }
2017
2018 masm.copySignFloat32(lhs, rhs, out);
2019 }
2020
2021 void CodeGenerator::visitCopySignD(LCopySignD* lir) {
2022 FloatRegister lhs = ToFloatRegister(lir->lhs());
2023 FloatRegister rhs = ToFloatRegister(lir->rhs());
2024
2025 FloatRegister out = ToFloatRegister(lir->output());
2026
2027 if (lhs == rhs) {
2028 if (lhs != out) {
2029 masm.moveDouble(lhs, out);
2030 }
2031 return;
2032 }
2033
2034 masm.copySignDouble(lhs, rhs, out);
2035 }
2036
2037 void CodeGenerator::visitSimd128(LSimd128* ins) {
2038 #ifdef ENABLE_WASM_SIMD
2039 const LDefinition* out = ins->output();
2040 masm.loadConstantSimd128(ins->simd128(), ToFloatRegister(out));
2041 #else
2042 MOZ_CRASH("No SIMD");
2043 #endif
2044 }
2045
2046 void CodeGenerator::visitWasmTernarySimd128(LWasmTernarySimd128* ins) {
2047 #ifdef ENABLE_WASM_SIMD
2048 switch (ins->simdOp()) {
2049 case wasm::SimdOp::V128Bitselect: {
2050 FloatRegister lhsDest = ToFloatRegister(ins->v0());
2051 FloatRegister rhs = ToFloatRegister(ins->v1());
2052 FloatRegister control = ToFloatRegister(ins->v2());
2053 FloatRegister temp = ToFloatRegister(ins->temp0());
2054 masm.bitwiseSelectSimd128(control, lhsDest, rhs, lhsDest, temp);
2055 break;
2056 }
2057 case wasm::SimdOp::F32x4RelaxedMadd:
2058 masm.fmaFloat32x4(ToFloatRegister(ins->v0()), ToFloatRegister(ins->v1()),
2059 ToFloatRegister(ins->v2()));
2060 break;
2061 case wasm::SimdOp::F32x4RelaxedNmadd:
2062 masm.fnmaFloat32x4(ToFloatRegister(ins->v0()), ToFloatRegister(ins->v1()),
2063 ToFloatRegister(ins->v2()));
2064 break;
2065 case wasm::SimdOp::F64x2RelaxedMadd:
2066 masm.fmaFloat64x2(ToFloatRegister(ins->v0()), ToFloatRegister(ins->v1()),
2067 ToFloatRegister(ins->v2()));
2068 break;
2069 case wasm::SimdOp::F64x2RelaxedNmadd:
2070 masm.fnmaFloat64x2(ToFloatRegister(ins->v0()), ToFloatRegister(ins->v1()),
2071 ToFloatRegister(ins->v2()));
2072 break;
2073 case wasm::SimdOp::I8x16RelaxedLaneSelect:
2074 case wasm::SimdOp::I16x8RelaxedLaneSelect:
2075 case wasm::SimdOp::I32x4RelaxedLaneSelect:
2076 case wasm::SimdOp::I64x2RelaxedLaneSelect: {
2077 FloatRegister lhs = ToFloatRegister(ins->v0());
2078 FloatRegister rhs = ToFloatRegister(ins->v1());
2079 FloatRegister mask = ToFloatRegister(ins->v2());
2080 FloatRegister dest = ToFloatRegister(ins->output());
2081 masm.laneSelectSimd128(mask, lhs, rhs, dest);
2082 break;
2083 }
2084 case wasm::SimdOp::I32x4RelaxedDotI8x16I7x16AddS:
2085 masm.dotInt8x16Int7x16ThenAdd(ToFloatRegister(ins->v0()),
2086 ToFloatRegister(ins->v1()),
2087 ToFloatRegister(ins->v2()));
2088 break;
2089 default:
2090 MOZ_CRASH("NYI");
2091 }
2092 #else
2093 MOZ_CRASH("No SIMD");
2094 #endif
2095 }
2096
2097 void CodeGenerator::visitWasmBinarySimd128(LWasmBinarySimd128* ins) {
2098 #ifdef ENABLE_WASM_SIMD
2099 FloatRegister lhs = ToFloatRegister(ins->lhs());
2100 FloatRegister rhs = ToFloatRegister(ins->rhs());
2101 FloatRegister temp1 = ToTempFloatRegisterOrInvalid(ins->temp0());
2102 FloatRegister temp2 = ToTempFloatRegisterOrInvalid(ins->temp1());
2103 FloatRegister dest = ToFloatRegister(ins->output());
2104
2105 switch (ins->simdOp()) {
2106 case wasm::SimdOp::V128And:
2107 masm.bitwiseAndSimd128(lhs, rhs, dest);
2108 break;
2109 case wasm::SimdOp::V128Or:
2110 masm.bitwiseOrSimd128(lhs, rhs, dest);
2111 break;
2112 case wasm::SimdOp::V128Xor:
2113 masm.bitwiseXorSimd128(lhs, rhs, dest);
2114 break;
2115 case wasm::SimdOp::V128AndNot:
2116 // x86/x64 specific: The CPU provides ~A & B. The operands were swapped
2117 // during lowering, and we'll compute A & ~B here as desired.
2118 masm.bitwiseNotAndSimd128(lhs, rhs, dest);
2119 break;
2120 case wasm::SimdOp::I8x16AvgrU:
2121 masm.unsignedAverageInt8x16(lhs, rhs, dest);
2122 break;
2123 case wasm::SimdOp::I16x8AvgrU:
2124 masm.unsignedAverageInt16x8(lhs, rhs, dest);
2125 break;
2126 case wasm::SimdOp::I8x16Add:
2127 masm.addInt8x16(lhs, rhs, dest);
2128 break;
2129 case wasm::SimdOp::I8x16AddSatS:
2130 masm.addSatInt8x16(lhs, rhs, dest);
2131 break;
2132 case wasm::SimdOp::I8x16AddSatU:
2133 masm.unsignedAddSatInt8x16(lhs, rhs, dest);
2134 break;
2135 case wasm::SimdOp::I8x16Sub:
2136 masm.subInt8x16(lhs, rhs, dest);
2137 break;
2138 case wasm::SimdOp::I8x16SubSatS:
2139 masm.subSatInt8x16(lhs, rhs, dest);
2140 break;
2141 case wasm::SimdOp::I8x16SubSatU:
2142 masm.unsignedSubSatInt8x16(lhs, rhs, dest);
2143 break;
2144 case wasm::SimdOp::I8x16MinS:
2145 masm.minInt8x16(lhs, rhs, dest);
2146 break;
2147 case wasm::SimdOp::I8x16MinU:
2148 masm.unsignedMinInt8x16(lhs, rhs, dest);
2149 break;
2150 case wasm::SimdOp::I8x16MaxS:
2151 masm.maxInt8x16(lhs, rhs, dest);
2152 break;
2153 case wasm::SimdOp::I8x16MaxU:
2154 masm.unsignedMaxInt8x16(lhs, rhs, dest);
2155 break;
2156 case wasm::SimdOp::I16x8Add:
2157 masm.addInt16x8(lhs, rhs, dest);
2158 break;
2159 case wasm::SimdOp::I16x8AddSatS:
2160 masm.addSatInt16x8(lhs, rhs, dest);
2161 break;
2162 case wasm::SimdOp::I16x8AddSatU:
2163 masm.unsignedAddSatInt16x8(lhs, rhs, dest);
2164 break;
2165 case wasm::SimdOp::I16x8Sub:
2166 masm.subInt16x8(lhs, rhs, dest);
2167 break;
2168 case wasm::SimdOp::I16x8SubSatS:
2169 masm.subSatInt16x8(lhs, rhs, dest);
2170 break;
2171 case wasm::SimdOp::I16x8SubSatU:
2172 masm.unsignedSubSatInt16x8(lhs, rhs, dest);
2173 break;
2174 case wasm::SimdOp::I16x8Mul:
2175 masm.mulInt16x8(lhs, rhs, dest);
2176 break;
2177 case wasm::SimdOp::I16x8MinS:
2178 masm.minInt16x8(lhs, rhs, dest);
2179 break;
2180 case wasm::SimdOp::I16x8MinU:
2181 masm.unsignedMinInt16x8(lhs, rhs, dest);
2182 break;
2183 case wasm::SimdOp::I16x8MaxS:
2184 masm.maxInt16x8(lhs, rhs, dest);
2185 break;
2186 case wasm::SimdOp::I16x8MaxU:
2187 masm.unsignedMaxInt16x8(lhs, rhs, dest);
2188 break;
2189 case wasm::SimdOp::I32x4Add:
2190 masm.addInt32x4(lhs, rhs, dest);
2191 break;
2192 case wasm::SimdOp::I32x4Sub:
2193 masm.subInt32x4(lhs, rhs, dest);
2194 break;
2195 case wasm::SimdOp::I32x4Mul:
2196 masm.mulInt32x4(lhs, rhs, dest);
2197 break;
2198 case wasm::SimdOp::I32x4MinS:
2199 masm.minInt32x4(lhs, rhs, dest);
2200 break;
2201 case wasm::SimdOp::I32x4MinU:
2202 masm.unsignedMinInt32x4(lhs, rhs, dest);
2203 break;
2204 case wasm::SimdOp::I32x4MaxS:
2205 masm.maxInt32x4(lhs, rhs, dest);
2206 break;
2207 case wasm::SimdOp::I32x4MaxU:
2208 masm.unsignedMaxInt32x4(lhs, rhs, dest);
2209 break;
2210 case wasm::SimdOp::I64x2Add:
2211 masm.addInt64x2(lhs, rhs, dest);
2212 break;
2213 case wasm::SimdOp::I64x2Sub:
2214 masm.subInt64x2(lhs, rhs, dest);
2215 break;
2216 case wasm::SimdOp::I64x2Mul:
2217 masm.mulInt64x2(lhs, rhs, dest, temp1);
2218 break;
2219 case wasm::SimdOp::F32x4Add:
2220 masm.addFloat32x4(lhs, rhs, dest);
2221 break;
2222 case wasm::SimdOp::F32x4Sub:
2223 masm.subFloat32x4(lhs, rhs, dest);
2224 break;
2225 case wasm::SimdOp::F32x4Mul:
2226 masm.mulFloat32x4(lhs, rhs, dest);
2227 break;
2228 case wasm::SimdOp::F32x4Div:
2229 masm.divFloat32x4(lhs, rhs, dest);
2230 break;
2231 case wasm::SimdOp::F32x4Min:
2232 masm.minFloat32x4(lhs, rhs, dest, temp1, temp2);
2233 break;
2234 case wasm::SimdOp::F32x4Max:
2235 masm.maxFloat32x4(lhs, rhs, dest, temp1, temp2);
2236 break;
2237 case wasm::SimdOp::F64x2Add:
2238 masm.addFloat64x2(lhs, rhs, dest);
2239 break;
2240 case wasm::SimdOp::F64x2Sub:
2241 masm.subFloat64x2(lhs, rhs, dest);
2242 break;
2243 case wasm::SimdOp::F64x2Mul:
2244 masm.mulFloat64x2(lhs, rhs, dest);
2245 break;
2246 case wasm::SimdOp::F64x2Div:
2247 masm.divFloat64x2(lhs, rhs, dest);
2248 break;
2249 case wasm::SimdOp::F64x2Min:
2250 masm.minFloat64x2(lhs, rhs, dest, temp1, temp2);
2251 break;
2252 case wasm::SimdOp::F64x2Max:
2253 masm.maxFloat64x2(lhs, rhs, dest, temp1, temp2);
2254 break;
2255 case wasm::SimdOp::I8x16Swizzle:
2256 masm.swizzleInt8x16(lhs, rhs, dest);
2257 break;
2258 case wasm::SimdOp::I8x16RelaxedSwizzle:
2259 masm.swizzleInt8x16Relaxed(lhs, rhs, dest);
2260 break;
2261 case wasm::SimdOp::I8x16NarrowI16x8S:
2262 masm.narrowInt16x8(lhs, rhs, dest);
2263 break;
2264 case wasm::SimdOp::I8x16NarrowI16x8U:
2265 masm.unsignedNarrowInt16x8(lhs, rhs, dest);
2266 break;
2267 case wasm::SimdOp::I16x8NarrowI32x4S:
2268 masm.narrowInt32x4(lhs, rhs, dest);
2269 break;
2270 case wasm::SimdOp::I16x8NarrowI32x4U:
2271 masm.unsignedNarrowInt32x4(lhs, rhs, dest);
2272 break;
2273 case wasm::SimdOp::I8x16Eq:
2274 masm.compareInt8x16(Assembler::Equal, lhs, rhs, dest);
2275 break;
2276 case wasm::SimdOp::I8x16Ne:
2277 masm.compareInt8x16(Assembler::NotEqual, lhs, rhs, dest);
2278 break;
2279 case wasm::SimdOp::I8x16LtS:
2280 masm.compareInt8x16(Assembler::LessThan, lhs, rhs, dest);
2281 break;
2282 case wasm::SimdOp::I8x16GtS:
2283 masm.compareInt8x16(Assembler::GreaterThan, lhs, rhs, dest);
2284 break;
2285 case wasm::SimdOp::I8x16LeS:
2286 masm.compareInt8x16(Assembler::LessThanOrEqual, lhs, rhs, dest);
2287 break;
2288 case wasm::SimdOp::I8x16GeS:
2289 masm.compareInt8x16(Assembler::GreaterThanOrEqual, lhs, rhs, dest);
2290 break;
2291 case wasm::SimdOp::I8x16LtU:
2292 masm.compareInt8x16(Assembler::Below, lhs, rhs, dest);
2293 break;
2294 case wasm::SimdOp::I8x16GtU:
2295 masm.compareInt8x16(Assembler::Above, lhs, rhs, dest);
2296 break;
2297 case wasm::SimdOp::I8x16LeU:
2298 masm.compareInt8x16(Assembler::BelowOrEqual, lhs, rhs, dest);
2299 break;
2300 case wasm::SimdOp::I8x16GeU:
2301 masm.compareInt8x16(Assembler::AboveOrEqual, lhs, rhs, dest);
2302 break;
2303 case wasm::SimdOp::I16x8Eq:
2304 masm.compareInt16x8(Assembler::Equal, lhs, rhs, dest);
2305 break;
2306 case wasm::SimdOp::I16x8Ne:
2307 masm.compareInt16x8(Assembler::NotEqual, lhs, rhs, dest);
2308 break;
2309 case wasm::SimdOp::I16x8LtS:
2310 masm.compareInt16x8(Assembler::LessThan, lhs, rhs, dest);
2311 break;
2312 case wasm::SimdOp::I16x8GtS:
2313 masm.compareInt16x8(Assembler::GreaterThan, lhs, rhs, dest);
2314 break;
2315 case wasm::SimdOp::I16x8LeS:
2316 masm.compareInt16x8(Assembler::LessThanOrEqual, lhs, rhs, dest);
2317 break;
2318 case wasm::SimdOp::I16x8GeS:
2319 masm.compareInt16x8(Assembler::GreaterThanOrEqual, lhs, rhs, dest);
2320 break;
2321 case wasm::SimdOp::I16x8LtU:
2322 masm.compareInt16x8(Assembler::Below, lhs, rhs, dest);
2323 break;
2324 case wasm::SimdOp::I16x8GtU:
2325 masm.compareInt16x8(Assembler::Above, lhs, rhs, dest);
2326 break;
2327 case wasm::SimdOp::I16x8LeU:
2328 masm.compareInt16x8(Assembler::BelowOrEqual, lhs, rhs, dest);
2329 break;
2330 case wasm::SimdOp::I16x8GeU:
2331 masm.compareInt16x8(Assembler::AboveOrEqual, lhs, rhs, dest);
2332 break;
2333 case wasm::SimdOp::I32x4Eq:
2334 masm.compareInt32x4(Assembler::Equal, lhs, rhs, dest);
2335 break;
2336 case wasm::SimdOp::I32x4Ne:
2337 masm.compareInt32x4(Assembler::NotEqual, lhs, rhs, dest);
2338 break;
2339 case wasm::SimdOp::I32x4LtS:
2340 masm.compareInt32x4(Assembler::LessThan, lhs, rhs, dest);
2341 break;
2342 case wasm::SimdOp::I32x4GtS:
2343 masm.compareInt32x4(Assembler::GreaterThan, lhs, rhs, dest);
2344 break;
2345 case wasm::SimdOp::I32x4LeS:
2346 masm.compareInt32x4(Assembler::LessThanOrEqual, lhs, rhs, dest);
2347 break;
2348 case wasm::SimdOp::I32x4GeS:
2349 masm.compareInt32x4(Assembler::GreaterThanOrEqual, lhs, rhs, dest);
2350 break;
2351 case wasm::SimdOp::I32x4LtU:
2352 masm.compareInt32x4(Assembler::Below, lhs, rhs, dest);
2353 break;
2354 case wasm::SimdOp::I32x4GtU:
2355 masm.compareInt32x4(Assembler::Above, lhs, rhs, dest);
2356 break;
2357 case wasm::SimdOp::I32x4LeU:
2358 masm.compareInt32x4(Assembler::BelowOrEqual, lhs, rhs, dest);
2359 break;
2360 case wasm::SimdOp::I32x4GeU:
2361 masm.compareInt32x4(Assembler::AboveOrEqual, lhs, rhs, dest);
2362 break;
2363 case wasm::SimdOp::I64x2Eq:
2364 masm.compareForEqualityInt64x2(Assembler::Equal, lhs, rhs, dest);
2365 break;
2366 case wasm::SimdOp::I64x2Ne:
2367 masm.compareForEqualityInt64x2(Assembler::NotEqual, lhs, rhs, dest);
2368 break;
2369 case wasm::SimdOp::I64x2LtS:
2370 masm.compareForOrderingInt64x2(Assembler::LessThan, lhs, rhs, dest, temp1,
2371 temp2);
2372 break;
2373 case wasm::SimdOp::I64x2GtS:
2374 masm.compareForOrderingInt64x2(Assembler::GreaterThan, lhs, rhs, dest,
2375 temp1, temp2);
2376 break;
2377 case wasm::SimdOp::I64x2LeS:
2378 masm.compareForOrderingInt64x2(Assembler::LessThanOrEqual, lhs, rhs, dest,
2379 temp1, temp2);
2380 break;
2381 case wasm::SimdOp::I64x2GeS:
2382 masm.compareForOrderingInt64x2(Assembler::GreaterThanOrEqual, lhs, rhs,
2383 dest, temp1, temp2);
2384 break;
2385 case wasm::SimdOp::F32x4Eq:
2386 masm.compareFloat32x4(Assembler::Equal, lhs, rhs, dest);
2387 break;
2388 case wasm::SimdOp::F32x4Ne:
2389 masm.compareFloat32x4(Assembler::NotEqual, lhs, rhs, dest);
2390 break;
2391 case wasm::SimdOp::F32x4Lt:
2392 masm.compareFloat32x4(Assembler::LessThan, lhs, rhs, dest);
2393 break;
2394 case wasm::SimdOp::F32x4Le:
2395 masm.compareFloat32x4(Assembler::LessThanOrEqual, lhs, rhs, dest);
2396 break;
2397 case wasm::SimdOp::F64x2Eq:
2398 masm.compareFloat64x2(Assembler::Equal, lhs, rhs, dest);
2399 break;
2400 case wasm::SimdOp::F64x2Ne:
2401 masm.compareFloat64x2(Assembler::NotEqual, lhs, rhs, dest);
2402 break;
2403 case wasm::SimdOp::F64x2Lt:
2404 masm.compareFloat64x2(Assembler::LessThan, lhs, rhs, dest);
2405 break;
2406 case wasm::SimdOp::F64x2Le:
2407 masm.compareFloat64x2(Assembler::LessThanOrEqual, lhs, rhs, dest);
2408 break;
2409 case wasm::SimdOp::F32x4PMax:
2410 // `lhs` and `rhs` are swapped, for non-VEX platforms the output is rhs.
2411 masm.pseudoMaxFloat32x4(lhs, rhs, dest);
2412 break;
2413 case wasm::SimdOp::F32x4PMin:
2414 // `lhs` and `rhs` are swapped, for non-VEX platforms the output is rhs.
2415 masm.pseudoMinFloat32x4(lhs, rhs, dest);
2416 break;
2417 case wasm::SimdOp::F64x2PMax:
2418 // `lhs` and `rhs` are swapped, for non-VEX platforms the output is rhs.
2419 masm.pseudoMaxFloat64x2(lhs, rhs, dest);
2420 break;
2421 case wasm::SimdOp::F64x2PMin:
2422 // `lhs` and `rhs` are swapped, for non-VEX platforms the output is rhs.
2423 masm.pseudoMinFloat64x2(lhs, rhs, dest);
2424 break;
2425 case wasm::SimdOp::I32x4DotI16x8S:
2426 masm.widenDotInt16x8(lhs, rhs, dest);
2427 break;
2428 case wasm::SimdOp::I16x8ExtmulLowI8x16S:
2429 masm.extMulLowInt8x16(lhs, rhs, dest);
2430 break;
2431 case wasm::SimdOp::I16x8ExtmulHighI8x16S:
2432 masm.extMulHighInt8x16(lhs, rhs, dest);
2433 break;
2434 case wasm::SimdOp::I16x8ExtmulLowI8x16U:
2435 masm.unsignedExtMulLowInt8x16(lhs, rhs, dest);
2436 break;
2437 case wasm::SimdOp::I16x8ExtmulHighI8x16U:
2438 masm.unsignedExtMulHighInt8x16(lhs, rhs, dest);
2439 break;
2440 case wasm::SimdOp::I32x4ExtmulLowI16x8S:
2441 masm.extMulLowInt16x8(lhs, rhs, dest);
2442 break;
2443 case wasm::SimdOp::I32x4ExtmulHighI16x8S:
2444 masm.extMulHighInt16x8(lhs, rhs, dest);
2445 break;
2446 case wasm::SimdOp::I32x4ExtmulLowI16x8U:
2447 masm.unsignedExtMulLowInt16x8(lhs, rhs, dest);
2448 break;
2449 case wasm::SimdOp::I32x4ExtmulHighI16x8U:
2450 masm.unsignedExtMulHighInt16x8(lhs, rhs, dest);
2451 break;
2452 case wasm::SimdOp::I64x2ExtmulLowI32x4S:
2453 masm.extMulLowInt32x4(lhs, rhs, dest);
2454 break;
2455 case wasm::SimdOp::I64x2ExtmulHighI32x4S:
2456 masm.extMulHighInt32x4(lhs, rhs, dest);
2457 break;
2458 case wasm::SimdOp::I64x2ExtmulLowI32x4U:
2459 masm.unsignedExtMulLowInt32x4(lhs, rhs, dest);
2460 break;
2461 case wasm::SimdOp::I64x2ExtmulHighI32x4U:
2462 masm.unsignedExtMulHighInt32x4(lhs, rhs, dest);
2463 break;
2464 case wasm::SimdOp::I16x8Q15MulrSatS:
2465 masm.q15MulrSatInt16x8(lhs, rhs, dest);
2466 break;
2467 case wasm::SimdOp::F32x4RelaxedMin:
2468 masm.minFloat32x4Relaxed(lhs, rhs, dest);
2469 break;
2470 case wasm::SimdOp::F32x4RelaxedMax:
2471 masm.maxFloat32x4Relaxed(lhs, rhs, dest);
2472 break;
2473 case wasm::SimdOp::F64x2RelaxedMin:
2474 masm.minFloat64x2Relaxed(lhs, rhs, dest);
2475 break;
2476 case wasm::SimdOp::F64x2RelaxedMax:
2477 masm.maxFloat64x2Relaxed(lhs, rhs, dest);
2478 break;
2479 case wasm::SimdOp::I16x8RelaxedQ15MulrS:
2480 masm.q15MulrInt16x8Relaxed(lhs, rhs, dest);
2481 break;
2482 case wasm::SimdOp::I16x8RelaxedDotI8x16I7x16S:
2483 masm.dotInt8x16Int7x16(lhs, rhs, dest);
2484 break;
2485 case wasm::SimdOp::MozPMADDUBSW:
2486 masm.vpmaddubsw(rhs, lhs, dest);
2487 break;
2488 default:
2489 MOZ_CRASH("Binary SimdOp not implemented");
2490 }
2491 #else
2492 MOZ_CRASH("No SIMD");
2493 #endif
2494 }
2495
2496 void CodeGenerator::visitWasmBinarySimd128WithConstant(
2497 LWasmBinarySimd128WithConstant* ins) {
2498 #ifdef ENABLE_WASM_SIMD
2499 FloatRegister lhs = ToFloatRegister(ins->lhs());
2500 const SimdConstant& rhs = ins->rhs();
2501 FloatRegister dest = ToFloatRegister(ins->output());
2502 FloatRegister temp = ToTempFloatRegisterOrInvalid(ins->temp0());
2503
2504 switch (ins->mir()->simdOp()) {
2505 case wasm::SimdOp::I8x16Add:
2506 masm.addInt8x16(lhs, rhs, dest);
2507 break;
2508 case wasm::SimdOp::I16x8Add:
2509 masm.addInt16x8(lhs, rhs, dest);
2510 break;
2511 case wasm::SimdOp::I32x4Add:
2512 masm.addInt32x4(lhs, rhs, dest);
2513 break;
2514 case wasm::SimdOp::I64x2Add:
2515 masm.addInt64x2(lhs, rhs, dest);
2516 break;
2517 case wasm::SimdOp::I8x16Sub:
2518 masm.subInt8x16(lhs, rhs, dest);
2519 break;
2520 case wasm::SimdOp::I16x8Sub:
2521 masm.subInt16x8(lhs, rhs, dest);
2522 break;
2523 case wasm::SimdOp::I32x4Sub:
2524 masm.subInt32x4(lhs, rhs, dest);
2525 break;
2526 case wasm::SimdOp::I64x2Sub:
2527 masm.subInt64x2(lhs, rhs, dest);
2528 break;
2529 case wasm::SimdOp::I16x8Mul:
2530 masm.mulInt16x8(lhs, rhs, dest);
2531 break;
2532 case wasm::SimdOp::I32x4Mul:
2533 masm.mulInt32x4(lhs, rhs, dest);
2534 break;
2535 case wasm::SimdOp::I8x16AddSatS:
2536 masm.addSatInt8x16(lhs, rhs, dest);
2537 break;
2538 case wasm::SimdOp::I8x16AddSatU:
2539 masm.unsignedAddSatInt8x16(lhs, rhs, dest);
2540 break;
2541 case wasm::SimdOp::I16x8AddSatS:
2542 masm.addSatInt16x8(lhs, rhs, dest);
2543 break;
2544 case wasm::SimdOp::I16x8AddSatU:
2545 masm.unsignedAddSatInt16x8(lhs, rhs, dest);
2546 break;
2547 case wasm::SimdOp::I8x16SubSatS:
2548 masm.subSatInt8x16(lhs, rhs, dest);
2549 break;
2550 case wasm::SimdOp::I8x16SubSatU:
2551 masm.unsignedSubSatInt8x16(lhs, rhs, dest);
2552 break;
2553 case wasm::SimdOp::I16x8SubSatS:
2554 masm.subSatInt16x8(lhs, rhs, dest);
2555 break;
2556 case wasm::SimdOp::I16x8SubSatU:
2557 masm.unsignedSubSatInt16x8(lhs, rhs, dest);
2558 break;
2559 case wasm::SimdOp::I8x16MinS:
2560 masm.minInt8x16(lhs, rhs, dest);
2561 break;
2562 case wasm::SimdOp::I8x16MinU:
2563 masm.unsignedMinInt8x16(lhs, rhs, dest);
2564 break;
2565 case wasm::SimdOp::I16x8MinS:
2566 masm.minInt16x8(lhs, rhs, dest);
2567 break;
2568 case wasm::SimdOp::I16x8MinU:
2569 masm.unsignedMinInt16x8(lhs, rhs, dest);
2570 break;
2571 case wasm::SimdOp::I32x4MinS:
2572 masm.minInt32x4(lhs, rhs, dest);
2573 break;
2574 case wasm::SimdOp::I32x4MinU:
2575 masm.unsignedMinInt32x4(lhs, rhs, dest);
2576 break;
2577 case wasm::SimdOp::I8x16MaxS:
2578 masm.maxInt8x16(lhs, rhs, dest);
2579 break;
2580 case wasm::SimdOp::I8x16MaxU:
2581 masm.unsignedMaxInt8x16(lhs, rhs, dest);
2582 break;
2583 case wasm::SimdOp::I16x8MaxS:
2584 masm.maxInt16x8(lhs, rhs, dest);
2585 break;
2586 case wasm::SimdOp::I16x8MaxU:
2587 masm.unsignedMaxInt16x8(lhs, rhs, dest);
2588 break;
2589 case wasm::SimdOp::I32x4MaxS:
2590 masm.maxInt32x4(lhs, rhs, dest);
2591 break;
2592 case wasm::SimdOp::I32x4MaxU:
2593 masm.unsignedMaxInt32x4(lhs, rhs, dest);
2594 break;
2595 case wasm::SimdOp::V128And:
2596 masm.bitwiseAndSimd128(lhs, rhs, dest);
2597 break;
2598 case wasm::SimdOp::V128Or:
2599 masm.bitwiseOrSimd128(lhs, rhs, dest);
2600 break;
2601 case wasm::SimdOp::V128Xor:
2602 masm.bitwiseXorSimd128(lhs, rhs, dest);
2603 break;
2604 case wasm::SimdOp::I8x16Eq:
2605 masm.compareInt8x16(Assembler::Equal, lhs, rhs, dest);
2606 break;
2607 case wasm::SimdOp::I8x16Ne:
2608 masm.compareInt8x16(Assembler::NotEqual, lhs, rhs, dest);
2609 break;
2610 case wasm::SimdOp::I8x16GtS:
2611 masm.compareInt8x16(Assembler::GreaterThan, lhs, rhs, dest);
2612 break;
2613 case wasm::SimdOp::I8x16LeS:
2614 masm.compareInt8x16(Assembler::LessThanOrEqual, lhs, rhs, dest);
2615 break;
2616 case wasm::SimdOp::I16x8Eq:
2617 masm.compareInt16x8(Assembler::Equal, lhs, rhs, dest);
2618 break;
2619 case wasm::SimdOp::I16x8Ne:
2620 masm.compareInt16x8(Assembler::NotEqual, lhs, rhs, dest);
2621 break;
2622 case wasm::SimdOp::I16x8GtS:
2623 masm.compareInt16x8(Assembler::GreaterThan, lhs, rhs, dest);
2624 break;
2625 case wasm::SimdOp::I16x8LeS:
2626 masm.compareInt16x8(Assembler::LessThanOrEqual, lhs, rhs, dest);
2627 break;
2628 case wasm::SimdOp::I32x4Eq:
2629 masm.compareInt32x4(Assembler::Equal, lhs, rhs, dest);
2630 break;
2631 case wasm::SimdOp::I32x4Ne:
2632 masm.compareInt32x4(Assembler::NotEqual, lhs, rhs, dest);
2633 break;
2634 case wasm::SimdOp::I32x4GtS:
2635 masm.compareInt32x4(Assembler::GreaterThan, lhs, rhs, dest);
2636 break;
2637 case wasm::SimdOp::I32x4LeS:
2638 masm.compareInt32x4(Assembler::LessThanOrEqual, lhs, rhs, dest);
2639 break;
2640 case wasm::SimdOp::I64x2Mul:
2641 masm.mulInt64x2(lhs, rhs, dest, temp);
2642 break;
2643 case wasm::SimdOp::F32x4Eq:
2644 masm.compareFloat32x4(Assembler::Equal, lhs, rhs, dest);
2645 break;
2646 case wasm::SimdOp::F32x4Ne:
2647 masm.compareFloat32x4(Assembler::NotEqual, lhs, rhs, dest);
2648 break;
2649 case wasm::SimdOp::F32x4Lt:
2650 masm.compareFloat32x4(Assembler::LessThan, lhs, rhs, dest);
2651 break;
2652 case wasm::SimdOp::F32x4Le:
2653 masm.compareFloat32x4(Assembler::LessThanOrEqual, lhs, rhs, dest);
2654 break;
2655 case wasm::SimdOp::F64x2Eq:
2656 masm.compareFloat64x2(Assembler::Equal, lhs, rhs, dest);
2657 break;
2658 case wasm::SimdOp::F64x2Ne:
2659 masm.compareFloat64x2(Assembler::NotEqual, lhs, rhs, dest);
2660 break;
2661 case wasm::SimdOp::F64x2Lt:
2662 masm.compareFloat64x2(Assembler::LessThan, lhs, rhs, dest);
2663 break;
2664 case wasm::SimdOp::F64x2Le:
2665 masm.compareFloat64x2(Assembler::LessThanOrEqual, lhs, rhs, dest);
2666 break;
2667 case wasm::SimdOp::I32x4DotI16x8S:
2668 masm.widenDotInt16x8(lhs, rhs, dest);
2669 break;
2670 case wasm::SimdOp::F32x4Add:
2671 masm.addFloat32x4(lhs, rhs, dest);
2672 break;
2673 case wasm::SimdOp::F64x2Add:
2674 masm.addFloat64x2(lhs, rhs, dest);
2675 break;
2676 case wasm::SimdOp::F32x4Sub:
2677 masm.subFloat32x4(lhs, rhs, dest);
2678 break;
2679 case wasm::SimdOp::F64x2Sub:
2680 masm.subFloat64x2(lhs, rhs, dest);
2681 break;
2682 case wasm::SimdOp::F32x4Div:
2683 masm.divFloat32x4(lhs, rhs, dest);
2684 break;
2685 case wasm::SimdOp::F64x2Div:
2686 masm.divFloat64x2(lhs, rhs, dest);
2687 break;
2688 case wasm::SimdOp::F32x4Mul:
2689 masm.mulFloat32x4(lhs, rhs, dest);
2690 break;
2691 case wasm::SimdOp::F64x2Mul:
2692 masm.mulFloat64x2(lhs, rhs, dest);
2693 break;
2694 case wasm::SimdOp::I8x16NarrowI16x8S:
2695 masm.narrowInt16x8(lhs, rhs, dest);
2696 break;
2697 case wasm::SimdOp::I8x16NarrowI16x8U:
2698 masm.unsignedNarrowInt16x8(lhs, rhs, dest);
2699 break;
2700 case wasm::SimdOp::I16x8NarrowI32x4S:
2701 masm.narrowInt32x4(lhs, rhs, dest);
2702 break;
2703 case wasm::SimdOp::I16x8NarrowI32x4U:
2704 masm.unsignedNarrowInt32x4(lhs, rhs, dest);
2705 break;
2706 default:
2707 MOZ_CRASH("Binary SimdOp with constant not implemented");
2708 }
2709 #else
2710 MOZ_CRASH("No SIMD");
2711 #endif
2712 }
2713
2714 void CodeGenerator::visitWasmVariableShiftSimd128(
2715 LWasmVariableShiftSimd128* ins) {
2716 #ifdef ENABLE_WASM_SIMD
2717 FloatRegister lhsDest = ToFloatRegister(ins->lhs());
2718 Register rhs = ToRegister(ins->rhs());
2719 FloatRegister temp = ToTempFloatRegisterOrInvalid(ins->temp0());
2720
2721 MOZ_ASSERT(ToFloatRegister(ins->output()) == lhsDest);
2722
2723 switch (ins->mir()->simdOp()) {
2724 case wasm::SimdOp::I8x16Shl:
2725 masm.leftShiftInt8x16(rhs, lhsDest, temp);
2726 break;
2727 case wasm::SimdOp::I8x16ShrS:
2728 masm.rightShiftInt8x16(rhs, lhsDest, temp);
2729 break;
2730 case wasm::SimdOp::I8x16ShrU:
2731 masm.unsignedRightShiftInt8x16(rhs, lhsDest, temp);
2732 break;
2733 case wasm::SimdOp::I16x8Shl:
2734 masm.leftShiftInt16x8(rhs, lhsDest);
2735 break;
2736 case wasm::SimdOp::I16x8ShrS:
2737 masm.rightShiftInt16x8(rhs, lhsDest);
2738 break;
2739 case wasm::SimdOp::I16x8ShrU:
2740 masm.unsignedRightShiftInt16x8(rhs, lhsDest);
2741 break;
2742 case wasm::SimdOp::I32x4Shl:
2743 masm.leftShiftInt32x4(rhs, lhsDest);
2744 break;
2745 case wasm::SimdOp::I32x4ShrS:
2746 masm.rightShiftInt32x4(rhs, lhsDest);
2747 break;
2748 case wasm::SimdOp::I32x4ShrU:
2749 masm.unsignedRightShiftInt32x4(rhs, lhsDest);
2750 break;
2751 case wasm::SimdOp::I64x2Shl:
2752 masm.leftShiftInt64x2(rhs, lhsDest);
2753 break;
2754 case wasm::SimdOp::I64x2ShrS:
2755 masm.rightShiftInt64x2(rhs, lhsDest, temp);
2756 break;
2757 case wasm::SimdOp::I64x2ShrU:
2758 masm.unsignedRightShiftInt64x2(rhs, lhsDest);
2759 break;
2760 default:
2761 MOZ_CRASH("Shift SimdOp not implemented");
2762 }
2763 #else
2764 MOZ_CRASH("No SIMD");
2765 #endif
2766 }
2767
2768 void CodeGenerator::visitWasmConstantShiftSimd128(
2769 LWasmConstantShiftSimd128* ins) {
2770 #ifdef ENABLE_WASM_SIMD
2771 FloatRegister src = ToFloatRegister(ins->src());
2772 FloatRegister dest = ToFloatRegister(ins->output());
2773 int32_t shift = ins->shift();
2774
2775 if (shift == 0) {
2776 masm.moveSimd128(src, dest);
2777 return;
2778 }
2779
2780 switch (ins->mir()->simdOp()) {
2781 case wasm::SimdOp::I8x16Shl:
2782 masm.leftShiftInt8x16(Imm32(shift), src, dest);
2783 break;
2784 case wasm::SimdOp::I8x16ShrS:
2785 masm.rightShiftInt8x16(Imm32(shift), src, dest);
2786 break;
2787 case wasm::SimdOp::I8x16ShrU:
2788 masm.unsignedRightShiftInt8x16(Imm32(shift), src, dest);
2789 break;
2790 case wasm::SimdOp::I16x8Shl:
2791 masm.leftShiftInt16x8(Imm32(shift), src, dest);
2792 break;
2793 case wasm::SimdOp::I16x8ShrS:
2794 masm.rightShiftInt16x8(Imm32(shift), src, dest);
2795 break;
2796 case wasm::SimdOp::I16x8ShrU:
2797 masm.unsignedRightShiftInt16x8(Imm32(shift), src, dest);
2798 break;
2799 case wasm::SimdOp::I32x4Shl:
2800 masm.leftShiftInt32x4(Imm32(shift), src, dest);
2801 break;
2802 case wasm::SimdOp::I32x4ShrS:
2803 masm.rightShiftInt32x4(Imm32(shift), src, dest);
2804 break;
2805 case wasm::SimdOp::I32x4ShrU:
2806 masm.unsignedRightShiftInt32x4(Imm32(shift), src, dest);
2807 break;
2808 case wasm::SimdOp::I64x2Shl:
2809 masm.leftShiftInt64x2(Imm32(shift), src, dest);
2810 break;
2811 case wasm::SimdOp::I64x2ShrS:
2812 masm.rightShiftInt64x2(Imm32(shift), src, dest);
2813 break;
2814 case wasm::SimdOp::I64x2ShrU:
2815 masm.unsignedRightShiftInt64x2(Imm32(shift), src, dest);
2816 break;
2817 default:
2818 MOZ_CRASH("Shift SimdOp not implemented");
2819 }
2820 #else
2821 MOZ_CRASH("No SIMD");
2822 #endif
2823 }
2824
2825 void CodeGenerator::visitWasmSignReplicationSimd128(
2826 LWasmSignReplicationSimd128* ins) {
2827 #ifdef ENABLE_WASM_SIMD
2828 FloatRegister src = ToFloatRegister(ins->src());
2829 FloatRegister dest = ToFloatRegister(ins->output());
2830
2831 switch (ins->mir()->simdOp()) {
2832 case wasm::SimdOp::I8x16ShrS:
2833 masm.signReplicationInt8x16(src, dest);
2834 break;
2835 case wasm::SimdOp::I16x8ShrS:
2836 masm.signReplicationInt16x8(src, dest);
2837 break;
2838 case wasm::SimdOp::I32x4ShrS:
2839 masm.signReplicationInt32x4(src, dest);
2840 break;
2841 case wasm::SimdOp::I64x2ShrS:
2842 masm.signReplicationInt64x2(src, dest);
2843 break;
2844 default:
2845 MOZ_CRASH("Shift SimdOp unsupported sign replication optimization");
2846 }
2847 #else
2848 MOZ_CRASH("No SIMD");
2849 #endif
2850 }
2851
2852 void CodeGenerator::visitWasmShuffleSimd128(LWasmShuffleSimd128* ins) {
2853 #ifdef ENABLE_WASM_SIMD
2854 FloatRegister lhsDest = ToFloatRegister(ins->lhs());
2855 FloatRegister rhs = ToFloatRegister(ins->rhs());
2856 SimdConstant control = ins->control();
2857 FloatRegister output = ToFloatRegister(ins->output());
2858 switch (ins->op()) {
2859 case SimdShuffleOp::BLEND_8x16: {
2860 masm.blendInt8x16(reinterpret_cast<const uint8_t*>(control.asInt8x16()),
2861 lhsDest, rhs, output, ToFloatRegister(ins->temp0()));
2862 break;
2863 }
2864 case SimdShuffleOp::BLEND_16x8: {
2865 MOZ_ASSERT(ins->temp0()->isBogusTemp());
2866 masm.blendInt16x8(reinterpret_cast<const uint16_t*>(control.asInt16x8()),
2867 lhsDest, rhs, output);
2868 break;
2869 }
2870 case SimdShuffleOp::CONCAT_RIGHT_SHIFT_8x16: {
2871 MOZ_ASSERT(ins->temp0()->isBogusTemp());
2872 int8_t count = 16 - control.asInt8x16()[0];
2873 MOZ_ASSERT(count > 0, "Should have been a MOVE operation");
2874 masm.concatAndRightShiftSimd128(lhsDest, rhs, output, count);
2875 break;
2876 }
2877 case SimdShuffleOp::INTERLEAVE_HIGH_8x16: {
2878 MOZ_ASSERT(ins->temp0()->isBogusTemp());
2879 masm.interleaveHighInt8x16(lhsDest, rhs, output);
2880 break;
2881 }
2882 case SimdShuffleOp::INTERLEAVE_HIGH_16x8: {
2883 MOZ_ASSERT(ins->temp0()->isBogusTemp());
2884 masm.interleaveHighInt16x8(lhsDest, rhs, output);
2885 break;
2886 }
2887 case SimdShuffleOp::INTERLEAVE_HIGH_32x4: {
2888 MOZ_ASSERT(ins->temp0()->isBogusTemp());
2889 masm.interleaveHighInt32x4(lhsDest, rhs, output);
2890 break;
2891 }
2892 case SimdShuffleOp::INTERLEAVE_HIGH_64x2: {
2893 MOZ_ASSERT(ins->temp0()->isBogusTemp());
2894 masm.interleaveHighInt64x2(lhsDest, rhs, output);
2895 break;
2896 }
2897 case SimdShuffleOp::INTERLEAVE_LOW_8x16: {
2898 MOZ_ASSERT(ins->temp0()->isBogusTemp());
2899 masm.interleaveLowInt8x16(lhsDest, rhs, output);
2900 break;
2901 }
2902 case SimdShuffleOp::INTERLEAVE_LOW_16x8: {
2903 MOZ_ASSERT(ins->temp0()->isBogusTemp());
2904 masm.interleaveLowInt16x8(lhsDest, rhs, output);
2905 break;
2906 }
2907 case SimdShuffleOp::INTERLEAVE_LOW_32x4: {
2908 MOZ_ASSERT(ins->temp0()->isBogusTemp());
2909 masm.interleaveLowInt32x4(lhsDest, rhs, output);
2910 break;
2911 }
2912 case SimdShuffleOp::INTERLEAVE_LOW_64x2: {
2913 MOZ_ASSERT(ins->temp0()->isBogusTemp());
2914 masm.interleaveLowInt64x2(lhsDest, rhs, output);
2915 break;
2916 }
2917 case SimdShuffleOp::SHUFFLE_BLEND_8x16: {
2918 masm.shuffleInt8x16(reinterpret_cast<const uint8_t*>(control.asInt8x16()),
2919 lhsDest, rhs, output);
2920 break;
2921 }
2922 default: {
2923 MOZ_CRASH("Unsupported SIMD shuffle operation");
2924 }
2925 }
2926 #else
2927 MOZ_CRASH("No SIMD");
2928 #endif
2929 }
2930
2931 #ifdef ENABLE_WASM_SIMD
2932
2933 enum PermuteX64I16x8Action : uint16_t {
2934 UNAVAILABLE = 0,
2935 SWAP_QWORDS = 1, // Swap qwords first
2936 PERM_LOW = 2, // Permute low qword by control_[0..3]
2937 PERM_HIGH = 4 // Permute high qword by control_[4..7]
2938 };
2939
2940 // Skip lanes that equal v starting at i, returning the index just beyond the
2941 // last of those. There is no requirement that the initial lanes[i] == v.
2942 template <typename T>
2943 static int ScanConstant(const T* lanes, int v, int i) {
2944 int len = int(16 / sizeof(T));
2945 MOZ_ASSERT(i <= len);
2946 while (i < len && lanes[i] == v) {
2947 i++;
2948 }
2949 return i;
2950 }
2951
2952 // Apply a transformation to each lane value.
2953 template <typename T>
2954 static void MapLanes(T* result, const T* input, int (*f)(int)) {
2955 // Hazard analysis trips on "IndirectCall: f" error.
2956 // Suppress the check -- `f` is expected to be trivial here.
2957 JS::AutoSuppressGCAnalysis nogc;
2958
2959 int len = int(16 / sizeof(T));
2960 for (int i = 0; i < len; i++) {
2961 result[i] = f(input[i]);
2962 }
2963 }
2964
2965 // Recognize part of an identity permutation starting at start, with
2966 // the first value of the permutation expected to be bias.
2967 template <typename T>
2968 static bool IsIdentity(const T* lanes, int start, int len, int bias) {
2969 if (lanes[start] != bias) {
2970 return false;
2971 }
2972 for (int i = start + 1; i < start + len; i++) {
2973 if (lanes[i] != lanes[i - 1] + 1) {
2974 return false;
2975 }
2976 }
2977 return true;
2978 }
2979
2980 // We can permute by words if the mask is reducible to a word mask, but the x64
2981 // lowering is only efficient if we can permute the high and low quadwords
2982 // separately, possibly after swapping quadwords.
2983 static PermuteX64I16x8Action CalculateX64Permute16x8(SimdConstant* control) {
2984 const SimdConstant::I16x8& lanes = control->asInt16x8();
2985 SimdConstant::I16x8 mapped;
2986 MapLanes(mapped, lanes, [](int x) -> int { return x < 4 ? 0 : 1; });
2987 int i = ScanConstant(mapped, mapped[0], 0);
2988 if (i != 4) {
2989 return PermuteX64I16x8Action::UNAVAILABLE;
2990 }
2991 i = ScanConstant(mapped, mapped[4], 4);
2992 if (i != 8) {
2993 return PermuteX64I16x8Action::UNAVAILABLE;
2994 }
2995 // Now compute the operation bits. `mapped` holds the adjusted lane mask.
2996 memcpy(mapped, lanes, sizeof(mapped));
2997 uint16_t op = 0;
2998 if (mapped[0] > mapped[4]) {
2999 op |= PermuteX64I16x8Action::SWAP_QWORDS;
3000 }
3001 for (auto& m : mapped) {
3002 m &= 3;
3003 }
3004 if (!IsIdentity(mapped, 0, 4, 0)) {
3005 op |= PermuteX64I16x8Action::PERM_LOW;
3006 }
3007 if (!IsIdentity(mapped, 4, 4, 0)) {
3008 op |= PermuteX64I16x8Action::PERM_HIGH;
3009 }
3010 MOZ_ASSERT(op != PermuteX64I16x8Action::UNAVAILABLE);
3011 *control = SimdConstant::CreateX8(mapped);
3012 return (PermuteX64I16x8Action)op;
3013 }
3014
3015 #endif
3016
3017 void CodeGenerator::visitWasmPermuteSimd128(LWasmPermuteSimd128* ins) {
3018 #ifdef ENABLE_WASM_SIMD
3019 FloatRegister src = ToFloatRegister(ins->src());
3020 FloatRegister dest = ToFloatRegister(ins->output());
3021 SimdConstant control = ins->control();
3022 switch (ins->op()) {
3023 // For broadcast, would MOVDDUP be better than PSHUFD for the last step?
3024 case SimdPermuteOp::BROADCAST_8x16: {
3025 const SimdConstant::I8x16& mask = control.asInt8x16();
3026 int8_t source = mask[0];
3027 if (source == 0 && Assembler::HasAVX2()) {
3028 masm.vbroadcastb(Operand(src), dest);
3029 break;
3030 }
3031 MOZ_ASSERT_IF(!Assembler::HasAVX(), src == dest);
3032 if (source < 8) {
3033 masm.interleaveLowInt8x16(src, src, dest);
3034 } else {
3035 masm.interleaveHighInt8x16(src, src, dest);
3036 source -= 8;
3037 }
3038 uint16_t v = uint16_t(source & 3);
3039 uint16_t wordMask[4] = {v, v, v, v};
3040 if (source < 4) {
3041 masm.permuteLowInt16x8(wordMask, dest, dest);
3042 uint32_t dwordMask[4] = {0, 0, 0, 0};
3043 masm.permuteInt32x4(dwordMask, dest, dest);
3044 } else {
3045 masm.permuteHighInt16x8(wordMask, dest, dest);
3046 uint32_t dwordMask[4] = {2, 2, 2, 2};
3047 masm.permuteInt32x4(dwordMask, dest, dest);
3048 }
3049 break;
3050 }
3051 case SimdPermuteOp::BROADCAST_16x8: {
3052 const SimdConstant::I16x8& mask = control.asInt16x8();
3053 int16_t source = mask[0];
3054 if (source == 0 && Assembler::HasAVX2()) {
3055 masm.vbroadcastw(Operand(src), dest);
3056 break;
3057 }
3058 uint16_t v = uint16_t(source & 3);
3059 uint16_t wordMask[4] = {v, v, v, v};
3060 if (source < 4) {
3061 masm.permuteLowInt16x8(wordMask, src, dest);
3062 uint32_t dwordMask[4] = {0, 0, 0, 0};
3063 masm.permuteInt32x4(dwordMask, dest, dest);
3064 } else {
3065 masm.permuteHighInt16x8(wordMask, src, dest);
3066 uint32_t dwordMask[4] = {2, 2, 2, 2};
3067 masm.permuteInt32x4(dwordMask, dest, dest);
3068 }
3069 break;
3070 }
3071 case SimdPermuteOp::MOVE: {
3072 masm.moveSimd128(src, dest);
3073 break;
3074 }
3075 case SimdPermuteOp::PERMUTE_8x16: {
3076 const SimdConstant::I8x16& mask = control.asInt8x16();
3077 # ifdef DEBUG
3078 DebugOnly<int> i;
3079 for (i = 0; i < 16 && mask[i] == i; i++) {
3080 }
3081 MOZ_ASSERT(i < 16, "Should have been a MOVE operation");
3082 # endif
3083 masm.permuteInt8x16(reinterpret_cast<const uint8_t*>(mask), src, dest);
3084 break;
3085 }
3086 case SimdPermuteOp::PERMUTE_16x8: {
3087 # ifdef DEBUG
3088 const SimdConstant::I16x8& mask = control.asInt16x8();
3089 DebugOnly<int> i;
3090 for (i = 0; i < 8 && mask[i] == i; i++) {
3091 }
3092 MOZ_ASSERT(i < 8, "Should have been a MOVE operation");
3093 # endif
3094 PermuteX64I16x8Action op = CalculateX64Permute16x8(&control);
3095 if (op != PermuteX64I16x8Action::UNAVAILABLE) {
3096 const SimdConstant::I16x8& mask = control.asInt16x8();
3097 if (op & PermuteX64I16x8Action::SWAP_QWORDS) {
3098 uint32_t dwordMask[4] = {2, 3, 0, 1};
3099 masm.permuteInt32x4(dwordMask, src, dest);
3100 src = dest;
3101 }
3102 if (op & PermuteX64I16x8Action::PERM_LOW) {
3103 masm.permuteLowInt16x8(reinterpret_cast<const uint16_t*>(mask) + 0,
3104 src, dest);
3105 src = dest;
3106 }
3107 if (op & PermuteX64I16x8Action::PERM_HIGH) {
3108 masm.permuteHighInt16x8(reinterpret_cast<const uint16_t*>(mask) + 4,
3109 src, dest);
3110 src = dest;
3111 }
3112 } else {
3113 const SimdConstant::I16x8& wmask = control.asInt16x8();
3114 uint8_t mask[16];
3115 for (unsigned i = 0; i < 16; i += 2) {
3116 mask[i] = wmask[i / 2] * 2;
3117 mask[i + 1] = wmask[i / 2] * 2 + 1;
3118 }
3119 masm.permuteInt8x16(mask, src, dest);
3120 }
3121 break;
3122 }
3123 case SimdPermuteOp::PERMUTE_32x4: {
3124 const SimdConstant::I32x4& mask = control.asInt32x4();
3125 if (Assembler::HasAVX2() && mask[0] == 0 && mask[1] == 0 &&
3126 mask[2] == 0 && mask[3] == 0) {
3127 masm.vbroadcastd(Operand(src), dest);
3128 break;
3129 }
3130 # ifdef DEBUG
3131 DebugOnly<int> i;
3132 for (i = 0; i < 4 && mask[i] == i; i++) {
3133 }
3134 MOZ_ASSERT(i < 4, "Should have been a MOVE operation");
3135 # endif
3136 masm.permuteInt32x4(reinterpret_cast<const uint32_t*>(mask), src, dest);
3137 break;
3138 }
3139 case SimdPermuteOp::ROTATE_RIGHT_8x16: {
3140 MOZ_ASSERT_IF(!Assembler::HasAVX(), src == dest);
3141 int8_t count = control.asInt8x16()[0];
3142 MOZ_ASSERT(count > 0, "Should have been a MOVE operation");
3143 masm.concatAndRightShiftSimd128(src, src, dest, count);
3144 break;
3145 }
3146 case SimdPermuteOp::SHIFT_LEFT_8x16: {
3147 int8_t count = control.asInt8x16()[0];
3148 MOZ_ASSERT(count > 0, "Should have been a MOVE operation");
3149 masm.leftShiftSimd128(Imm32(count), src, dest);
3150 break;
3151 }
3152 case SimdPermuteOp::SHIFT_RIGHT_8x16: {
3153 int8_t count = control.asInt8x16()[0];
3154 MOZ_ASSERT(count > 0, "Should have been a MOVE operation");
3155 masm.rightShiftSimd128(Imm32(count), src, dest);
3156 break;
3157 }
3158 case SimdPermuteOp::ZERO_EXTEND_8x16_TO_16x8:
3159 masm.zeroExtend8x16To16x8(src, dest);
3160 break;
3161 case SimdPermuteOp::ZERO_EXTEND_8x16_TO_32x4:
3162 masm.zeroExtend8x16To32x4(src, dest);
3163 break;
3164 case SimdPermuteOp::ZERO_EXTEND_8x16_TO_64x2:
3165 masm.zeroExtend8x16To64x2(src, dest);
3166 break;
3167 case SimdPermuteOp::ZERO_EXTEND_16x8_TO_32x4:
3168 masm.zeroExtend16x8To32x4(src, dest);
3169 break;
3170 case SimdPermuteOp::ZERO_EXTEND_16x8_TO_64x2:
3171 masm.zeroExtend16x8To64x2(src, dest);
3172 break;
3173 case SimdPermuteOp::ZERO_EXTEND_32x4_TO_64x2:
3174 masm.zeroExtend32x4To64x2(src, dest);
3175 break;
3176 case SimdPermuteOp::REVERSE_16x8:
3177 masm.reverseInt16x8(src, dest);
3178 break;
3179 case SimdPermuteOp::REVERSE_32x4:
3180 masm.reverseInt32x4(src, dest);
3181 break;
3182 case SimdPermuteOp::REVERSE_64x2:
3183 masm.reverseInt64x2(src, dest);
3184 break;
3185 default: {
3186 MOZ_CRASH("Unsupported SIMD permutation operation");
3187 }
3188 }
3189 #else
3190 MOZ_CRASH("No SIMD");
3191 #endif
3192 }
3193
3194 void CodeGenerator::visitWasmReplaceLaneSimd128(LWasmReplaceLaneSimd128* ins) {
3195 #ifdef ENABLE_WASM_SIMD
3196 FloatRegister lhs = ToFloatRegister(ins->lhs());
3197 FloatRegister dest = ToFloatRegister(ins->output());
3198 const LAllocation* rhs = ins->rhs();
3199 uint32_t laneIndex = ins->mir()->laneIndex();
3200
3201 switch (ins->mir()->simdOp()) {
3202 case wasm::SimdOp::I8x16ReplaceLane:
3203 masm.replaceLaneInt8x16(laneIndex, lhs, ToRegister(rhs), dest);
3204 break;
3205 case wasm::SimdOp::I16x8ReplaceLane:
3206 masm.replaceLaneInt16x8(laneIndex, lhs, ToRegister(rhs), dest);
3207 break;
3208 case wasm::SimdOp::I32x4ReplaceLane:
3209 masm.replaceLaneInt32x4(laneIndex, lhs, ToRegister(rhs), dest);
3210 break;
3211 case wasm::SimdOp::F32x4ReplaceLane:
3212 masm.replaceLaneFloat32x4(laneIndex, lhs, ToFloatRegister(rhs), dest);
3213 break;
3214 case wasm::SimdOp::F64x2ReplaceLane:
3215 masm.replaceLaneFloat64x2(laneIndex, lhs, ToFloatRegister(rhs), dest);
3216 break;
3217 default:
3218 MOZ_CRASH("ReplaceLane SimdOp not implemented");
3219 }
3220 #else
3221 MOZ_CRASH("No SIMD");
3222 #endif
3223 }
3224
3225 void CodeGenerator::visitWasmReplaceInt64LaneSimd128(
3226 LWasmReplaceInt64LaneSimd128* ins) {
3227 #ifdef ENABLE_WASM_SIMD
3228 MOZ_RELEASE_ASSERT(ins->mir()->simdOp() == wasm::SimdOp::I64x2ReplaceLane);
3229 masm.replaceLaneInt64x2(ins->mir()->laneIndex(), ToFloatRegister(ins->lhs()),
3230 ToRegister64(ins->rhs()),
3231 ToFloatRegister(ins->output()));
3232 #else
3233 MOZ_CRASH("No SIMD");
3234 #endif
3235 }
3236
3237 void CodeGenerator::visitWasmScalarToSimd128(LWasmScalarToSimd128* ins) {
3238 #ifdef ENABLE_WASM_SIMD
3239 FloatRegister dest = ToFloatRegister(ins->output());
3240
3241 switch (ins->mir()->simdOp()) {
3242 case wasm::SimdOp::I8x16Splat:
3243 masm.splatX16(ToRegister(ins->src()), dest);
3244 break;
3245 case wasm::SimdOp::I16x8Splat:
3246 masm.splatX8(ToRegister(ins->src()), dest);
3247 break;
3248 case wasm::SimdOp::I32x4Splat:
3249 masm.splatX4(ToRegister(ins->src()), dest);
3250 break;
3251 case wasm::SimdOp::F32x4Splat:
3252 masm.splatX4(ToFloatRegister(ins->src()), dest);
3253 break;
3254 case wasm::SimdOp::F64x2Splat:
3255 masm.splatX2(ToFloatRegister(ins->src()), dest);
3256 break;
3257 default:
3258 MOZ_CRASH("ScalarToSimd128 SimdOp not implemented");
3259 }
3260 #else
3261 MOZ_CRASH("No SIMD");
3262 #endif
3263 }
3264
3265 void CodeGenerator::visitWasmInt64ToSimd128(LWasmInt64ToSimd128* ins) {
3266 #ifdef ENABLE_WASM_SIMD
3267 Register64 src = ToRegister64(ins->src());
3268 FloatRegister dest = ToFloatRegister(ins->output());
3269
3270 switch (ins->mir()->simdOp()) {
3271 case wasm::SimdOp::I64x2Splat:
3272 masm.splatX2(src, dest);
3273 break;
3274 case wasm::SimdOp::V128Load8x8S:
3275 masm.moveGPR64ToDouble(src, dest);
3276 masm.widenLowInt8x16(dest, dest);
3277 break;
3278 case wasm::SimdOp::V128Load8x8U:
3279 masm.moveGPR64ToDouble(src, dest);
3280 masm.unsignedWidenLowInt8x16(dest, dest);
3281 break;
3282 case wasm::SimdOp::V128Load16x4S:
3283 masm.moveGPR64ToDouble(src, dest);
3284 masm.widenLowInt16x8(dest, dest);
3285 break;
3286 case wasm::SimdOp::V128Load16x4U:
3287 masm.moveGPR64ToDouble(src, dest);
3288 masm.unsignedWidenLowInt16x8(dest, dest);
3289 break;
3290 case wasm::SimdOp::V128Load32x2S:
3291 masm.moveGPR64ToDouble(src, dest);
3292 masm.widenLowInt32x4(dest, dest);
3293 break;
3294 case wasm::SimdOp::V128Load32x2U:
3295 masm.moveGPR64ToDouble(src, dest);
3296 masm.unsignedWidenLowInt32x4(dest, dest);
3297 break;
3298 default:
3299 MOZ_CRASH("Int64ToSimd128 SimdOp not implemented");
3300 }
3301 #else
3302 MOZ_CRASH("No SIMD");
3303 #endif
3304 }
3305
3306 void CodeGenerator::visitWasmUnarySimd128(LWasmUnarySimd128* ins) {
3307 #ifdef ENABLE_WASM_SIMD
3308 FloatRegister src = ToFloatRegister(ins->src());
3309 FloatRegister dest = ToFloatRegister(ins->output());
3310
3311 switch (ins->mir()->simdOp()) {
3312 case wasm::SimdOp::I8x16Neg:
3313 masm.negInt8x16(src, dest);
3314 break;
3315 case wasm::SimdOp::I16x8Neg:
3316 masm.negInt16x8(src, dest);
3317 break;
3318 case wasm::SimdOp::I16x8ExtendLowI8x16S:
3319 masm.widenLowInt8x16(src, dest);
3320 break;
3321 case wasm::SimdOp::I16x8ExtendHighI8x16S:
3322 masm.widenHighInt8x16(src, dest);
3323 break;
3324 case wasm::SimdOp::I16x8ExtendLowI8x16U:
3325 masm.unsignedWidenLowInt8x16(src, dest);
3326 break;
3327 case wasm::SimdOp::I16x8ExtendHighI8x16U:
3328 masm.unsignedWidenHighInt8x16(src, dest);
3329 break;
3330 case wasm::SimdOp::I32x4Neg:
3331 masm.negInt32x4(src, dest);
3332 break;
3333 case wasm::SimdOp::I32x4ExtendLowI16x8S:
3334 masm.widenLowInt16x8(src, dest);
3335 break;
3336 case wasm::SimdOp::I32x4ExtendHighI16x8S:
3337 masm.widenHighInt16x8(src, dest);
3338 break;
3339 case wasm::SimdOp::I32x4ExtendLowI16x8U:
3340 masm.unsignedWidenLowInt16x8(src, dest);
3341 break;
3342 case wasm::SimdOp::I32x4ExtendHighI16x8U:
3343 masm.unsignedWidenHighInt16x8(src, dest);
3344 break;
3345 case wasm::SimdOp::I32x4TruncSatF32x4S:
3346 masm.truncSatFloat32x4ToInt32x4(src, dest);
3347 break;
3348 case wasm::SimdOp::I32x4TruncSatF32x4U:
3349 masm.unsignedTruncSatFloat32x4ToInt32x4(src, dest,
3350 ToFloatRegister(ins->temp0()));
3351 break;
3352 case wasm::SimdOp::I64x2Neg:
3353 masm.negInt64x2(src, dest);
3354 break;
3355 case wasm::SimdOp::I64x2ExtendLowI32x4S:
3356 masm.widenLowInt32x4(src, dest);
3357 break;
3358 case wasm::SimdOp::I64x2ExtendHighI32x4S:
3359 masm.widenHighInt32x4(src, dest);
3360 break;
3361 case wasm::SimdOp::I64x2ExtendLowI32x4U:
3362 masm.unsignedWidenLowInt32x4(src, dest);
3363 break;
3364 case wasm::SimdOp::I64x2ExtendHighI32x4U:
3365 masm.unsignedWidenHighInt32x4(src, dest);
3366 break;
3367 case wasm::SimdOp::F32x4Abs:
3368 masm.absFloat32x4(src, dest);
3369 break;
3370 case wasm::SimdOp::F32x4Neg:
3371 masm.negFloat32x4(src, dest);
3372 break;
3373 case wasm::SimdOp::F32x4Sqrt:
3374 masm.sqrtFloat32x4(src, dest);
3375 break;
3376 case wasm::SimdOp::F32x4ConvertI32x4S:
3377 masm.convertInt32x4ToFloat32x4(src, dest);
3378 break;
3379 case wasm::SimdOp::F32x4ConvertI32x4U:
3380 masm.unsignedConvertInt32x4ToFloat32x4(src, dest);
3381 break;
3382 case wasm::SimdOp::F64x2Abs:
3383 masm.absFloat64x2(src, dest);
3384 break;
3385 case wasm::SimdOp::F64x2Neg:
3386 masm.negFloat64x2(src, dest);
3387 break;
3388 case wasm::SimdOp::F64x2Sqrt:
3389 masm.sqrtFloat64x2(src, dest);
3390 break;
3391 case wasm::SimdOp::V128Not:
3392 masm.bitwiseNotSimd128(src, dest);
3393 break;
3394 case wasm::SimdOp::I8x16Popcnt:
3395 masm.popcntInt8x16(src, dest, ToFloatRegister(ins->temp0()));
3396 break;
3397 case wasm::SimdOp::I8x16Abs:
3398 masm.absInt8x16(src, dest);
3399 break;
3400 case wasm::SimdOp::I16x8Abs:
3401 masm.absInt16x8(src, dest);
3402 break;
3403 case wasm::SimdOp::I32x4Abs:
3404 masm.absInt32x4(src, dest);
3405 break;
3406 case wasm::SimdOp::I64x2Abs:
3407 masm.absInt64x2(src, dest);
3408 break;
3409 case wasm::SimdOp::F32x4Ceil:
3410 masm.ceilFloat32x4(src, dest);
3411 break;
3412 case wasm::SimdOp::F32x4Floor:
3413 masm.floorFloat32x4(src, dest);
3414 break;
3415 case wasm::SimdOp::F32x4Trunc:
3416 masm.truncFloat32x4(src, dest);
3417 break;
3418 case wasm::SimdOp::F32x4Nearest:
3419 masm.nearestFloat32x4(src, dest);
3420 break;
3421 case wasm::SimdOp::F64x2Ceil:
3422 masm.ceilFloat64x2(src, dest);
3423 break;
3424 case wasm::SimdOp::F64x2Floor:
3425 masm.floorFloat64x2(src, dest);
3426 break;
3427 case wasm::SimdOp::F64x2Trunc:
3428 masm.truncFloat64x2(src, dest);
3429 break;
3430 case wasm::SimdOp::F64x2Nearest:
3431 masm.nearestFloat64x2(src, dest);
3432 break;
3433 case wasm::SimdOp::F32x4DemoteF64x2Zero:
3434 masm.convertFloat64x2ToFloat32x4(src, dest);
3435 break;
3436 case wasm::SimdOp::F64x2PromoteLowF32x4:
3437 masm.convertFloat32x4ToFloat64x2(src, dest);
3438 break;
3439 case wasm::SimdOp::F64x2ConvertLowI32x4S:
3440 masm.convertInt32x4ToFloat64x2(src, dest);
3441 break;
3442 case wasm::SimdOp::F64x2ConvertLowI32x4U:
3443 masm.unsignedConvertInt32x4ToFloat64x2(src, dest);
3444 break;
3445 case wasm::SimdOp::I32x4TruncSatF64x2SZero:
3446 masm.truncSatFloat64x2ToInt32x4(src, dest, ToFloatRegister(ins->temp0()));
3447 break;
3448 case wasm::SimdOp::I32x4TruncSatF64x2UZero:
3449 masm.unsignedTruncSatFloat64x2ToInt32x4(src, dest,
3450 ToFloatRegister(ins->temp0()));
3451 break;
3452 case wasm::SimdOp::I16x8ExtaddPairwiseI8x16S:
3453 masm.extAddPairwiseInt8x16(src, dest);
3454 break;
3455 case wasm::SimdOp::I16x8ExtaddPairwiseI8x16U:
3456 masm.unsignedExtAddPairwiseInt8x16(src, dest);
3457 break;
3458 case wasm::SimdOp::I32x4ExtaddPairwiseI16x8S:
3459 masm.extAddPairwiseInt16x8(src, dest);
3460 break;
3461 case wasm::SimdOp::I32x4ExtaddPairwiseI16x8U:
3462 masm.unsignedExtAddPairwiseInt16x8(src, dest);
3463 break;
3464 case wasm::SimdOp::I32x4RelaxedTruncF32x4S:
3465 masm.truncFloat32x4ToInt32x4Relaxed(src, dest);
3466 break;
3467 case wasm::SimdOp::I32x4RelaxedTruncF32x4U:
3468 masm.unsignedTruncFloat32x4ToInt32x4Relaxed(src, dest);
3469 break;
3470 case wasm::SimdOp::I32x4RelaxedTruncF64x2SZero:
3471 masm.truncFloat64x2ToInt32x4Relaxed(src, dest);
3472 break;
3473 case wasm::SimdOp::I32x4RelaxedTruncF64x2UZero:
3474 masm.unsignedTruncFloat64x2ToInt32x4Relaxed(src, dest);
3475 break;
3476 default:
3477 MOZ_CRASH("Unary SimdOp not implemented");
3478 }
3479 #else
3480 MOZ_CRASH("No SIMD");
3481 #endif
3482 }
3483
3484 void CodeGenerator::visitWasmReduceSimd128(LWasmReduceSimd128* ins) {
3485 #ifdef ENABLE_WASM_SIMD
3486 FloatRegister src = ToFloatRegister(ins->src());
3487 const LDefinition* dest = ins->output();
3488 uint32_t imm = ins->mir()->imm();
3489
3490 switch (ins->mir()->simdOp()) {
3491 case wasm::SimdOp::V128AnyTrue:
3492 masm.anyTrueSimd128(src, ToRegister(dest));
3493 break;
3494 case wasm::SimdOp::I8x16AllTrue:
3495 masm.allTrueInt8x16(src, ToRegister(dest));
3496 break;
3497 case wasm::SimdOp::I16x8AllTrue:
3498 masm.allTrueInt16x8(src, ToRegister(dest));
3499 break;
3500 case wasm::SimdOp::I32x4AllTrue:
3501 masm.allTrueInt32x4(src, ToRegister(dest));
3502 break;
3503 case wasm::SimdOp::I64x2AllTrue:
3504 masm.allTrueInt64x2(src, ToRegister(dest));
3505 break;
3506 case wasm::SimdOp::I8x16Bitmask:
3507 masm.bitmaskInt8x16(src, ToRegister(dest));
3508 break;
3509 case wasm::SimdOp::I16x8Bitmask:
3510 masm.bitmaskInt16x8(src, ToRegister(dest));
3511 break;
3512 case wasm::SimdOp::I32x4Bitmask:
3513 masm.bitmaskInt32x4(src, ToRegister(dest));
3514 break;
3515 case wasm::SimdOp::I64x2Bitmask:
3516 masm.bitmaskInt64x2(src, ToRegister(dest));
3517 break;
3518 case wasm::SimdOp::I8x16ExtractLaneS:
3519 masm.extractLaneInt8x16(imm, src, ToRegister(dest));
3520 break;
3521 case wasm::SimdOp::I8x16ExtractLaneU:
3522 masm.unsignedExtractLaneInt8x16(imm, src, ToRegister(dest));
3523 break;
3524 case wasm::SimdOp::I16x8ExtractLaneS:
3525 masm.extractLaneInt16x8(imm, src, ToRegister(dest));
3526 break;
3527 case wasm::SimdOp::I16x8ExtractLaneU:
3528 masm.unsignedExtractLaneInt16x8(imm, src, ToRegister(dest));
3529 break;
3530 case wasm::SimdOp::I32x4ExtractLane:
3531 masm.extractLaneInt32x4(imm, src, ToRegister(dest));
3532 break;
3533 case wasm::SimdOp::F32x4ExtractLane:
3534 masm.extractLaneFloat32x4(imm, src, ToFloatRegister(dest));
3535 break;
3536 case wasm::SimdOp::F64x2ExtractLane:
3537 masm.extractLaneFloat64x2(imm, src, ToFloatRegister(dest));
3538 break;
3539 default:
3540 MOZ_CRASH("Reduce SimdOp not implemented");
3541 }
3542 #else
3543 MOZ_CRASH("No SIMD");
3544 #endif
3545 }
3546
3547 void CodeGenerator::visitWasmReduceAndBranchSimd128(
3548 LWasmReduceAndBranchSimd128* ins) {
3549 #ifdef ENABLE_WASM_SIMD
3550 FloatRegister src = ToFloatRegister(ins->src());
3551
3552 switch (ins->simdOp()) {
3553 case wasm::SimdOp::V128AnyTrue:
3554 // Set the zero flag if all of the lanes are zero, and branch on that.
3555 masm.vptest(src, src);
3556 emitBranch(Assembler::NotEqual, ins->ifTrue(), ins->ifFalse());
3557 break;
3558 case wasm::SimdOp::I8x16AllTrue:
3559 case wasm::SimdOp::I16x8AllTrue:
3560 case wasm::SimdOp::I32x4AllTrue:
3561 case wasm::SimdOp::I64x2AllTrue: {
3562 // Compare all lanes to zero, set the zero flag if none of the lanes are
3563 // zero, and branch on that.
3564 ScratchSimd128Scope tmp(masm);
3565 masm.vpxor(tmp, tmp, tmp);
3566 switch (ins->simdOp()) {
3567 case wasm::SimdOp::I8x16AllTrue:
3568 masm.vpcmpeqb(Operand(src), tmp, tmp);
3569 break;
3570 case wasm::SimdOp::I16x8AllTrue:
3571 masm.vpcmpeqw(Operand(src), tmp, tmp);
3572 break;
3573 case wasm::SimdOp::I32x4AllTrue:
3574 masm.vpcmpeqd(Operand(src), tmp, tmp);
3575 break;
3576 case wasm::SimdOp::I64x2AllTrue:
3577 masm.vpcmpeqq(Operand(src), tmp, tmp);
3578 break;
3579 default:
3580 MOZ_CRASH();
3581 }
3582 masm.vptest(tmp, tmp);
3583 emitBranch(Assembler::Equal, ins->ifTrue(), ins->ifFalse());
3584 break;
3585 }
3586 case wasm::SimdOp::I16x8Bitmask: {
3587 masm.bitwiseTestSimd128(SimdConstant::SplatX8(0x8000), src);
3588 emitBranch(Assembler::NotEqual, ins->ifTrue(), ins->ifFalse());
3589 break;
3590 }
3591 default:
3592 MOZ_CRASH("Reduce-and-branch SimdOp not implemented");
3593 }
3594 #else
3595 MOZ_CRASH("No SIMD");
3596 #endif
3597 }
3598
3599 void CodeGenerator::visitWasmReduceSimd128ToInt64(
3600 LWasmReduceSimd128ToInt64* ins) {
3601 #ifdef ENABLE_WASM_SIMD
3602 FloatRegister src = ToFloatRegister(ins->src());
3603 Register64 dest = ToOutRegister64(ins);
3604 uint32_t imm = ins->mir()->imm();
3605
3606 switch (ins->mir()->simdOp()) {
3607 case wasm::SimdOp::I64x2ExtractLane:
3608 masm.extractLaneInt64x2(imm, src, dest);
3609 break;
3610 default:
3611 MOZ_CRASH("Reduce SimdOp not implemented");
3612 }
3613 #else
3614 MOZ_CRASH("No SIMD");
3615 #endif
3616 }
3617
3618 void CodeGenerator::visitWasmLoadLaneSimd128(LWasmLoadLaneSimd128* ins) {
3619 #ifdef ENABLE_WASM_SIMD
3620 const MWasmLoadLaneSimd128* mir = ins->mir();
3621 const wasm::MemoryAccessDesc& access = mir->access();
3622
3623 access.assertOffsetInGuardPages();
3624 uint32_t offset = access.offset32();
3625
3626 const LAllocation* value = ins->src();
3627 Operand srcAddr = toMemoryAccessOperand(ins, offset);
3628
3629 switch (mir->laneSize()) {
3630 case 1: {
3631 masm.append(access, wasm::TrapMachineInsn::Load8,
3632 FaultingCodeOffset(masm.currentOffset()));
3633 masm.vpinsrb(mir->laneIndex(), srcAddr, ToFloatRegister(value),
3634 ToFloatRegister(value));
3635 break;
3636 }
3637 case 2: {
3638 masm.append(access, wasm::TrapMachineInsn::Load16,
3639 FaultingCodeOffset(masm.currentOffset()));
3640 masm.vpinsrw(mir->laneIndex(), srcAddr, ToFloatRegister(value),
3641 ToFloatRegister(value));
3642 break;
3643 }
3644 case 4: {
3645 masm.append(access, wasm::TrapMachineInsn::Load32,
3646 FaultingCodeOffset(masm.currentOffset()));
3647 masm.vinsertps(mir->laneIndex() << 4, srcAddr, ToFloatRegister(value),
3648 ToFloatRegister(value));
3649 break;
3650 }
3651 case 8: {
3652 masm.append(access, wasm::TrapMachineInsn::Load64,
3653 FaultingCodeOffset(masm.currentOffset()));
3654 if (mir->laneIndex() == 0) {
3655 masm.vmovlps(srcAddr, ToFloatRegister(value), ToFloatRegister(value));
3656 } else {
3657 masm.vmovhps(srcAddr, ToFloatRegister(value), ToFloatRegister(value));
3658 }
3659 break;
3660 }
3661 default:
3662 MOZ_CRASH("Unsupported load lane size");
3663 }
3664 #else
3665 MOZ_CRASH("No SIMD");
3666 #endif
3667 }
3668
3669 void CodeGenerator::visitWasmStoreLaneSimd128(LWasmStoreLaneSimd128* ins) {
3670 #ifdef ENABLE_WASM_SIMD
3671 const MWasmStoreLaneSimd128* mir = ins->mir();
3672 const wasm::MemoryAccessDesc& access = mir->access();
3673
3674 access.assertOffsetInGuardPages();
3675 uint32_t offset = access.offset32();
3676
3677 const LAllocation* src = ins->src();
3678 Operand destAddr = toMemoryAccessOperand(ins, offset);
3679
3680 switch (mir->laneSize()) {
3681 case 1: {
3682 masm.append(access, wasm::TrapMachineInsn::Store8,
3683 FaultingCodeOffset(masm.currentOffset()));
3684 masm.vpextrb(mir->laneIndex(), ToFloatRegister(src), destAddr);
3685 break;
3686 }
3687 case 2: {
3688 masm.append(access, wasm::TrapMachineInsn::Store16,
3689 FaultingCodeOffset(masm.currentOffset()));
3690 masm.vpextrw(mir->laneIndex(), ToFloatRegister(src), destAddr);
3691 break;
3692 }
3693 case 4: {
3694 masm.append(access, wasm::TrapMachineInsn::Store32,
3695 FaultingCodeOffset(masm.currentOffset()));
3696 unsigned lane = mir->laneIndex();
3697 if (lane == 0) {
3698 masm.vmovss(ToFloatRegister(src), destAddr);
3699 } else {
3700 masm.vextractps(lane, ToFloatRegister(src), destAddr);
3701 }
3702 break;
3703 }
3704 case 8: {
3705 masm.append(access, wasm::TrapMachineInsn::Store64,
3706 FaultingCodeOffset(masm.currentOffset()));
3707 if (mir->laneIndex() == 0) {
3708 masm.vmovlps(ToFloatRegister(src), destAddr);
3709 } else {
3710 masm.vmovhps(ToFloatRegister(src), destAddr);
3711 }
3712 break;
3713 }
3714 default:
3715 MOZ_CRASH("Unsupported store lane size");
3716 }
3717 #else
3718 MOZ_CRASH("No SIMD");
3719 #endif
3720 }
3721
3722 } // namespace jit
3723 } // namespace js