Debian Codegraph @

js/src/jit/x86-shared/Lowering-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/Lowering-x86-shared.h"
8
9 #include "mozilla/MathAlgorithms.h"
10
11 #include "jit/Lowering.h"
12 #include "jit/MIR-wasm.h"
13 #include "jit/MIR.h"
14 #include "wasm/WasmFeatures.h" // for wasm::ReportSimdAnalysis
15
16 #include "jit/shared/Lowering-shared-inl.h"
17
18 using namespace js;
19 using namespace js::jit;
20
21 using mozilla::Abs;
22 using mozilla::FloorLog2;
23 using mozilla::Maybe;
24 using mozilla::Nothing;
25 using mozilla::Some;
26
27 LTableSwitch* LIRGeneratorX86Shared::newLTableSwitch(
28 const LAllocation& in, const LDefinition& inputCopy) {
29 return new (alloc()) LTableSwitch(in, inputCopy, temp());
30 }
31
32 LTableSwitchV* LIRGeneratorX86Shared::newLTableSwitchV(
33 const LBoxAllocation& in) {
34 return new (alloc()) LTableSwitchV(in, temp(), tempDouble(), temp());
35 }
36
37 void LIRGenerator::visitPowHalf(MPowHalf* ins) {
38 MDefinition* input = ins->input();
39 MOZ_ASSERT(input->type() == MIRType::Double);
40 LPowHalfD* lir = new (alloc()) LPowHalfD(useRegisterAtStart(input));
41 define(lir, ins);
42 }
43
44 LUse LIRGeneratorX86Shared::useShiftRegister(MDefinition* mir) {
45 // Unless BMI2 is available, the shift register must be ecx. x86 can't shift a
46 // non-ecx register.
47 if (Assembler::HasBMI2()) {
48 return useRegister(mir);
49 }
50 return useFixed(mir, ecx);
51 }
52
53 LUse LIRGeneratorX86Shared::useShiftRegisterAtStart(MDefinition* mir) {
54 // Unless BMI2 is available, the shift register must be ecx. x86 can't shift a
55 // non-ecx register.
56 if (Assembler::HasBMI2()) {
57 return useRegisterAtStart(mir);
58 }
59 return useFixedAtStart(mir, ecx);
60 }
61
62 LDefinition LIRGeneratorX86Shared::tempShift() {
63 // Unless BMI2 is available, the shift register must be ecx. x86 can't shift a
64 // non-ecx register.
65 if (Assembler::HasBMI2()) {
66 return temp();
67 }
68 return tempFixed(ecx);
69 }
70
71 void LIRGeneratorX86Shared::lowerForShift(LInstructionHelper<1, 2, 0>* ins,
72 MDefinition* mir, MDefinition* lhs,
73 MDefinition* rhs) {
74 ins->setOperand(0, useRegisterAtStart(lhs));
75
76 if (rhs->isConstant()) {
77 ins->setOperand(1, useOrConstantAtStart(rhs));
78 } else if (!mir->isRotate()) {
79 ins->setOperand(1, willHaveDifferentLIRNodes(lhs, rhs)
80 ? useShiftRegister(rhs)
81 : useShiftRegisterAtStart(rhs));
82 } else {
83 ins->setOperand(1, willHaveDifferentLIRNodes(lhs, rhs)
84 ? useFixed(rhs, ecx)
85 : useFixedAtStart(rhs, ecx));
86 }
87
88 defineReuseInput(ins, mir, 0);
89 }
90
91 template <class LInstr>
92 void LIRGeneratorX86Shared::lowerForShiftInt64(LInstr* ins, MDefinition* mir,
93 MDefinition* lhs,
94 MDefinition* rhs) {
95 LAllocation rhsAlloc;
96 if (rhs->isConstant()) {
97 rhsAlloc = useOrConstantAtStart(rhs);
98 #ifdef JS_CODEGEN_X64
99 } else if (std::is_same_v<LInstr, LShiftI64>) {
100 rhsAlloc = useShiftRegister(rhs);
101 } else {
102 rhsAlloc = useFixed(rhs, rcx);
103 }
104 #else
105 } else {
106 // The operands are int64, but we only care about the lower 32 bits of
107 // the RHS. On 32-bit, the code below will load that part in ecx and
108 // will discard the upper half.
109 rhsAlloc = useLowWordFixed(rhs, ecx);
110 }
111 #endif
112
113 if constexpr (std::is_same_v<LInstr, LShiftI64>) {
114 ins->setLhs(useInt64RegisterAtStart(lhs));
115 ins->setRhs(rhsAlloc);
116 defineInt64ReuseInput(ins, mir, LShiftI64::LhsIndex);
117 } else {
118 ins->setInput(useInt64RegisterAtStart(lhs));
119 ins->setCount(rhsAlloc);
120 #if defined(JS_NUNBOX32)
121 ins->setTemp0(temp());
122 #endif
123 defineInt64ReuseInput(ins, mir, LRotateI64::InputIndex);
124 }
125 }
126
127 template void LIRGeneratorX86Shared::lowerForShiftInt64(LShiftI64* ins,
128 MDefinition* mir,
129 MDefinition* lhs,
130 MDefinition* rhs);
131 template void LIRGeneratorX86Shared::lowerForShiftInt64(LRotateI64* ins,
132 MDefinition* mir,
133 MDefinition* lhs,
134 MDefinition* rhs);
135
136 void LIRGeneratorX86Shared::lowerForALU(LInstructionHelper<1, 1, 0>* ins,
137 MDefinition* mir, MDefinition* input) {
138 ins->setOperand(0, useRegisterAtStart(input));
139 defineReuseInput(ins, mir, 0);
140 }
141
142 void LIRGeneratorX86Shared::lowerForALU(LInstructionHelper<1, 2, 0>* ins,
143 MDefinition* mir, MDefinition* lhs,
144 MDefinition* rhs) {
145 ins->setOperand(0, useRegisterAtStart(lhs));
146 ins->setOperand(1, willHaveDifferentLIRNodes(lhs, rhs)
147 ? useOrConstant(rhs)
148 : useOrConstantAtStart(rhs));
149 defineReuseInput(ins, mir, 0);
150 }
151
152 template <size_t Temps>
153 void LIRGeneratorX86Shared::lowerForFPU(LInstructionHelper<1, 2, Temps>* ins,
154 MDefinition* mir, MDefinition* lhs,
155 MDefinition* rhs) {
156 // Without AVX, we'll need to use the x86 encodings where one of the
157 // inputs must be the same location as the output.
158 if (!Assembler::HasAVX()) {
159 ins->setOperand(0, useRegisterAtStart(lhs));
160 ins->setOperand(
161 1, willHaveDifferentLIRNodes(lhs, rhs) ? use(rhs) : useAtStart(rhs));
162 defineReuseInput(ins, mir, 0);
163 } else {
164 ins->setOperand(0, useRegisterAtStart(lhs));
165 ins->setOperand(1, useAtStart(rhs));
166 define(ins, mir);
167 }
168 }
169
170 template void LIRGeneratorX86Shared::lowerForFPU(
171 LInstructionHelper<1, 2, 0>* ins, MDefinition* mir, MDefinition* lhs,
172 MDefinition* rhs);
173 template void LIRGeneratorX86Shared::lowerForFPU(
174 LInstructionHelper<1, 2, 1>* ins, MDefinition* mir, MDefinition* lhs,
175 MDefinition* rhs);
176
177 void LIRGeneratorX86Shared::lowerNegI(MInstruction* ins, MDefinition* input) {
178 defineReuseInput(new (alloc()) LNegI(useRegisterAtStart(input)), ins, 0);
179 }
180
181 void LIRGeneratorX86Shared::lowerNegI64(MInstruction* ins, MDefinition* input) {
182 defineInt64ReuseInput(new (alloc()) LNegI64(useInt64RegisterAtStart(input)),
183 ins, 0);
184 }
185
186 void LIRGenerator::visitAbs(MAbs* ins) {
187 defineReuseInput(allocateAbs(ins, useRegisterAtStart(ins->input())), ins, 0);
188 }
189
190 void LIRGeneratorX86Shared::lowerMulI(MMul* mul, MDefinition* lhs,
191 MDefinition* rhs) {
192 // Note: If we need a negative zero check, lhs is used twice.
193 LAllocation lhsCopy = mul->canBeNegativeZero() ? use(lhs) : LAllocation();
194 LMulI* lir = new (alloc())
195 LMulI(useRegisterAtStart(lhs),
196 willHaveDifferentLIRNodes(lhs, rhs) ? useOrConstant(rhs)
197 : useOrConstantAtStart(rhs),
198 lhsCopy);
199 if (mul->fallible()) {
200 assignSnapshot(lir, mul->bailoutKind());
201 }
202 defineReuseInput(lir, mul, 0);
203 }
204
205 void LIRGeneratorX86Shared::lowerDivI(MDiv* div) {
206 if (div->isUnsigned()) {
207 lowerUDiv(div);
208 return;
209 }
210
211 // Division instructions are slow. Division by constant denominators can be
212 // rewritten to use other instructions.
213 if (div->rhs()->isConstant()) {
214 int32_t rhs = div->rhs()->toConstant()->toInt32();
215
216 // Division by powers of two can be done by shifting, and division by
217 // other numbers can be done by a reciprocal multiplication technique.
218 int32_t shift = FloorLog2(Abs(rhs));
219 if (rhs != 0 && uint32_t(1) << shift == Abs(rhs)) {
220 LAllocation lhs = useRegisterAtStart(div->lhs());
221 LDivPowTwoI* lir;
222 // When truncated with maybe a non-zero remainder, we have to round the
223 // result toward 0. This requires an extra register to round up/down
224 // whether the left-hand-side is signed.
225 bool needRoundNeg = div->canBeNegativeDividend() && div->isTruncated();
226 if (!needRoundNeg) {
227 // Numerator is unsigned, so does not need adjusting.
228 lir = new (alloc()) LDivPowTwoI(lhs, lhs, shift, rhs < 0);
229 } else {
230 // Numerator might be signed, and needs adjusting, and an extra lhs copy
231 // is needed to round the result of the integer division towards zero.
232 lir = new (alloc())
233 LDivPowTwoI(lhs, useRegister(div->lhs()), shift, rhs < 0);
234 }
235 if (div->fallible()) {
236 assignSnapshot(lir, div->bailoutKind());
237 }
238 defineReuseInput(lir, div, 0);
239 return;
240 }
241 if (rhs != 0) {
242 LDivOrModConstantI* lir;
243 lir = new (alloc())
244 LDivOrModConstantI(useRegister(div->lhs()), rhs, tempFixed(eax));
245 if (div->fallible()) {
246 assignSnapshot(lir, div->bailoutKind());
247 }
248 defineFixed(lir, div, LAllocation(AnyRegister(edx)));
249 return;
250 }
251 }
252
253 LDivI* lir = new (alloc())
254 LDivI(useRegister(div->lhs()), useRegister(div->rhs()), tempFixed(edx));
255 if (div->fallible()) {
256 assignSnapshot(lir, div->bailoutKind());
257 }
258 defineFixed(lir, div, LAllocation(AnyRegister(eax)));
259 }
260
261 void LIRGeneratorX86Shared::lowerModI(MMod* mod) {
262 if (mod->isUnsigned()) {
263 lowerUMod(mod);
264 return;
265 }
266
267 if (mod->rhs()->isConstant()) {
268 int32_t rhs = mod->rhs()->toConstant()->toInt32();
269 int32_t shift = FloorLog2(Abs(rhs));
270 if (rhs != 0 && uint32_t(1) << shift == Abs(rhs)) {
271 LModPowTwoI* lir =
272 new (alloc()) LModPowTwoI(useRegisterAtStart(mod->lhs()), shift);
273 if (mod->fallible()) {
274 assignSnapshot(lir, mod->bailoutKind());
275 }
276 defineReuseInput(lir, mod, 0);
277 return;
278 }
279 if (rhs != 0) {
280 LDivOrModConstantI* lir;
281 lir = new (alloc())
282 LDivOrModConstantI(useRegister(mod->lhs()), rhs, tempFixed(edx));
283 if (mod->fallible()) {
284 assignSnapshot(lir, mod->bailoutKind());
285 }
286 defineFixed(lir, mod, LAllocation(AnyRegister(eax)));
287 return;
288 }
289 }
290
291 LModI* lir = new (alloc())
292 LModI(useRegister(mod->lhs()), useRegister(mod->rhs()), tempFixed(eax));
293 if (mod->fallible()) {
294 assignSnapshot(lir, mod->bailoutKind());
295 }
296 defineFixed(lir, mod, LAllocation(AnyRegister(edx)));
297 }
298
299 void LIRGenerator::visitWasmNeg(MWasmNeg* ins) {
300 switch (ins->type()) {
301 case MIRType::Int32:
302 defineReuseInput(new (alloc()) LNegI(useRegisterAtStart(ins->input())),
303 ins, 0);
304 break;
305 case MIRType::Float32:
306 defineReuseInput(new (alloc()) LNegF(useRegisterAtStart(ins->input())),
307 ins, 0);
308 break;
309 case MIRType::Double:
310 defineReuseInput(new (alloc()) LNegD(useRegisterAtStart(ins->input())),
311 ins, 0);
312 break;
313 default:
314 MOZ_CRASH();
315 }
316 }
317
318 void LIRGeneratorX86Shared::lowerWasmSelectI(MWasmSelect* select) {
319 auto* lir = new (alloc())
320 LWasmSelect(useRegisterAtStart(select->trueExpr()),
321 useAny(select->falseExpr()), useRegister(select->condExpr()));
322 defineReuseInput(lir, select, LWasmSelect::TrueExprIndex);
323 }
324
325 void LIRGeneratorX86Shared::lowerWasmSelectI64(MWasmSelect* select) {
326 auto* lir = new (alloc()) LWasmSelectI64(
327 useInt64RegisterAtStart(select->trueExpr()),
328 useInt64(select->falseExpr()), useRegister(select->condExpr()));
329 defineInt64ReuseInput(lir, select, LWasmSelectI64::TrueExprIndex);
330 }
331
332 void LIRGenerator::visitAsmJSLoadHeap(MAsmJSLoadHeap* ins) {
333 MDefinition* base = ins->base();
334 MOZ_ASSERT(base->type() == MIRType::Int32);
335
336 MDefinition* boundsCheckLimit = ins->boundsCheckLimit();
337 MOZ_ASSERT_IF(ins->needsBoundsCheck(),
338 boundsCheckLimit->type() == MIRType::Int32);
339
340 // For simplicity, require a register if we're going to emit a bounds-check
341 // branch, so that we don't have special cases for constants. This should
342 // only happen in rare constant-folding cases since asm.js sets the minimum
343 // heap size based when accessed via constant.
344 LAllocation baseAlloc = ins->needsBoundsCheck()
345 ? useRegisterAtStart(base)
346 : useRegisterOrZeroAtStart(base);
347
348 LAllocation limitAlloc = ins->needsBoundsCheck()
349 ? useRegisterAtStart(boundsCheckLimit)
350 : LAllocation();
351 LAllocation memoryBaseAlloc = ins->hasMemoryBase()
352 ? useRegisterAtStart(ins->memoryBase())
353 : LAllocation();
354
355 auto* lir =
356 new (alloc()) LAsmJSLoadHeap(baseAlloc, limitAlloc, memoryBaseAlloc);
357 define(lir, ins);
358 }
359
360 void LIRGenerator::visitAsmJSStoreHeap(MAsmJSStoreHeap* ins) {
361 MDefinition* base = ins->base();
362 MOZ_ASSERT(base->type() == MIRType::Int32);
363
364 MDefinition* boundsCheckLimit = ins->boundsCheckLimit();
365 MOZ_ASSERT_IF(ins->needsBoundsCheck(),
366 boundsCheckLimit->type() == MIRType::Int32);
367
368 // For simplicity, require a register if we're going to emit a bounds-check
369 // branch, so that we don't have special cases for constants. This should
370 // only happen in rare constant-folding cases since asm.js sets the minimum
371 // heap size based when accessed via constant.
372 LAllocation baseAlloc = ins->needsBoundsCheck()
373 ? useRegisterAtStart(base)
374 : useRegisterOrZeroAtStart(base);
375
376 LAllocation limitAlloc = ins->needsBoundsCheck()
377 ? useRegisterAtStart(boundsCheckLimit)
378 : LAllocation();
379 LAllocation memoryBaseAlloc = ins->hasMemoryBase()
380 ? useRegisterAtStart(ins->memoryBase())
381 : LAllocation();
382
383 LAsmJSStoreHeap* lir = nullptr;
384 switch (ins->access().type()) {
385 case Scalar::Int8:
386 case Scalar::Uint8:
387 #ifdef JS_CODEGEN_X86
388 // See comment for LIRGeneratorX86::useByteOpRegister.
389 lir = new (alloc()) LAsmJSStoreHeap(
390 baseAlloc, useFixed(ins->value(), eax), limitAlloc, memoryBaseAlloc);
391 break;
392 #endif
393 case Scalar::Int16:
394 case Scalar::Uint16:
395 case Scalar::Int32:
396 case Scalar::Uint32:
397 case Scalar::Float32:
398 case Scalar::Float64:
399 // For now, don't allow constant values. The immediate operand affects
400 // instruction layout which affects patching.
401 lir = new (alloc())
402 LAsmJSStoreHeap(baseAlloc, useRegisterAtStart(ins->value()),
403 limitAlloc, memoryBaseAlloc);
404 break;
405 case Scalar::Int64:
406 case Scalar::Simd128:
407 MOZ_CRASH("NYI");
408 case Scalar::Uint8Clamped:
409 case Scalar::BigInt64:
410 case Scalar::BigUint64:
411 case Scalar::Float16:
412 case Scalar::MaxTypedArrayViewType:
413 MOZ_CRASH("unexpected array type");
414 }
415 add(lir, ins);
416 }
417
418 void LIRGeneratorX86Shared::lowerUDiv(MDiv* div) {
419 if (div->rhs()->isConstant()) {
420 // NOTE: the result of toInt32 is coerced to uint32_t.
421 uint32_t rhs = div->rhs()->toConstant()->toInt32();
422 int32_t shift = FloorLog2(rhs);
423
424 LAllocation lhs = useRegisterAtStart(div->lhs());
425 if (rhs != 0 && uint32_t(1) << shift == rhs) {
426 LDivPowTwoI* lir = new (alloc()) LDivPowTwoI(lhs, lhs, shift, false);
427 if (div->fallible()) {
428 assignSnapshot(lir, div->bailoutKind());
429 }
430 defineReuseInput(lir, div, 0);
431 } else {
432 LUDivOrModConstant* lir = new (alloc())
433 LUDivOrModConstant(useRegister(div->lhs()), rhs, tempFixed(eax));
434 if (div->fallible()) {
435 assignSnapshot(lir, div->bailoutKind());
436 }
437 defineFixed(lir, div, LAllocation(AnyRegister(edx)));
438 }
439 return;
440 }
441
442 LUDivOrMod* lir = new (alloc()) LUDivOrMod(
443 useRegister(div->lhs()), useRegister(div->rhs()), tempFixed(edx));
444 if (div->fallible()) {
445 assignSnapshot(lir, div->bailoutKind());
446 }
447 defineFixed(lir, div, LAllocation(AnyRegister(eax)));
448 }
449
450 void LIRGeneratorX86Shared::lowerUMod(MMod* mod) {
451 if (mod->rhs()->isConstant()) {
452 uint32_t rhs = mod->rhs()->toConstant()->toInt32();
453 int32_t shift = FloorLog2(rhs);
454
455 if (rhs != 0 && uint32_t(1) << shift == rhs) {
456 LModPowTwoI* lir =
457 new (alloc()) LModPowTwoI(useRegisterAtStart(mod->lhs()), shift);
458 if (mod->fallible()) {
459 assignSnapshot(lir, mod->bailoutKind());
460 }
461 defineReuseInput(lir, mod, 0);
462 } else {
463 LUDivOrModConstant* lir = new (alloc())
464 LUDivOrModConstant(useRegister(mod->lhs()), rhs, tempFixed(edx));
465 if (mod->fallible()) {
466 assignSnapshot(lir, mod->bailoutKind());
467 }
468 defineFixed(lir, mod, LAllocation(AnyRegister(eax)));
469 }
470 return;
471 }
472
473 LUDivOrMod* lir = new (alloc()) LUDivOrMod(
474 useRegister(mod->lhs()), useRegister(mod->rhs()), tempFixed(eax));
475 if (mod->fallible()) {
476 assignSnapshot(lir, mod->bailoutKind());
477 }
478 defineFixed(lir, mod, LAllocation(AnyRegister(edx)));
479 }
480
481 void LIRGeneratorX86Shared::lowerUrshD(MUrsh* mir) {
482 MDefinition* lhs = mir->lhs();
483 MDefinition* rhs = mir->rhs();
484
485 MOZ_ASSERT(lhs->type() == MIRType::Int32);
486 MOZ_ASSERT(rhs->type() == MIRType::Int32);
487 MOZ_ASSERT(mir->type() == MIRType::Double);
488
489 #ifdef JS_CODEGEN_X64
490 static_assert(ecx == rcx);
491 #endif
492
493 LUse lhsUse = useRegisterAtStart(lhs);
494 LAllocation rhsAlloc;
495 if (rhs->isConstant()) {
496 rhsAlloc = useOrConstant(rhs);
497 } else {
498 rhsAlloc = useShiftRegister(rhs);
499 }
500
501 LUrshD* lir = new (alloc()) LUrshD(lhsUse, rhsAlloc, tempCopy(lhs, 0));
502 define(lir, mir);
503 }
504
505 void LIRGeneratorX86Shared::lowerPowOfTwoI(MPow* mir) {
506 int32_t base = mir->input()->toConstant()->toInt32();
507 MDefinition* power = mir->power();
508
509 auto* lir = new (alloc()) LPowOfTwoI(useShiftRegister(power), base);
510 assignSnapshot(lir, mir->bailoutKind());
511 define(lir, mir);
512 }
513
514 void LIRGeneratorX86Shared::lowerBigIntPtrLsh(MBigIntPtrLsh* ins) {
515 auto* lir = new (alloc()) LBigIntPtrLsh(
516 useRegister(ins->lhs()), useRegister(ins->rhs()), temp(), tempShift());
517 assignSnapshot(lir, ins->bailoutKind());
518 define(lir, ins);
519 }
520
521 void LIRGeneratorX86Shared::lowerBigIntPtrRsh(MBigIntPtrRsh* ins) {
522 auto* lir = new (alloc()) LBigIntPtrRsh(
523 useRegister(ins->lhs()), useRegister(ins->rhs()), temp(), tempShift());
524 assignSnapshot(lir, ins->bailoutKind());
525 define(lir, ins);
526 }
527
528 void LIRGeneratorX86Shared::lowerCompareExchangeTypedArrayElement(
529 MCompareExchangeTypedArrayElement* ins, bool useI386ByteRegisters) {
530 MOZ_ASSERT(!Scalar::isFloatingType(ins->arrayType()));
531 MOZ_ASSERT(ins->elements()->type() == MIRType::Elements);
532 MOZ_ASSERT(ins->index()->type() == MIRType::IntPtr);
533
534 const LUse elements = useRegister(ins->elements());
535 const LAllocation index =
536 useRegisterOrIndexConstant(ins->index(), ins->arrayType());
537
538 // If the target is a floating register then we need a temp at the
539 // lower level; that temp must be eax.
540 //
541 // Otherwise the target (if used) is an integer register, which
542 // must be eax. If the target is not used the machine code will
543 // still clobber eax, so just pretend it's used.
544 //
545 // oldval must be in a register.
546 //
547 // newval must be in a register. If the source is a byte array
548 // then newval must be a register that has a byte size: on x86
549 // this must be ebx, ecx, or edx (eax is taken for the output).
550 //
551 // Bug #1077036 describes some further optimization opportunities.
552
553 bool fixedOutput = false;
554 LDefinition tempDef = LDefinition::BogusTemp();
555 LAllocation newval;
556 if (ins->arrayType() == Scalar::Uint32 && IsFloatingPointType(ins->type())) {
557 tempDef = tempFixed(eax);
558 newval = useRegister(ins->newval());
559 } else {
560 fixedOutput = true;
561 if (useI386ByteRegisters && ins->isByteArray()) {
562 newval = useFixed(ins->newval(), ebx);
563 } else {
564 newval = useRegister(ins->newval());
565 }
566 }
567
568 const LAllocation oldval = useRegister(ins->oldval());
569
570 LCompareExchangeTypedArrayElement* lir =
571 new (alloc()) LCompareExchangeTypedArrayElement(elements, index, oldval,
572 newval, tempDef);
573
574 if (fixedOutput) {
575 defineFixed(lir, ins, LAllocation(AnyRegister(eax)));
576 } else {
577 define(lir, ins);
578 }
579 }
580
581 void LIRGeneratorX86Shared::lowerAtomicExchangeTypedArrayElement(
582 MAtomicExchangeTypedArrayElement* ins, bool useI386ByteRegisters) {
583 MOZ_ASSERT(ins->arrayType() <= Scalar::Uint32);
584
585 MOZ_ASSERT(ins->elements()->type() == MIRType::Elements);
586 MOZ_ASSERT(ins->index()->type() == MIRType::IntPtr);
587
588 const LUse elements = useRegister(ins->elements());
589 const LAllocation index =
590 useRegisterOrIndexConstant(ins->index(), ins->arrayType());
591 const LAllocation value = useRegister(ins->value());
592
593 // The underlying instruction is XCHG, which can operate on any
594 // register.
595 //
596 // If the target is a floating register (for Uint32) then we need
597 // a temp into which to exchange.
598 //
599 // If the source is a byte array then we need a register that has
600 // a byte size; in this case -- on x86 only -- pin the output to
601 // an appropriate register and use that as a temp in the back-end.
602
603 LDefinition tempDef = LDefinition::BogusTemp();
604 if (ins->arrayType() == Scalar::Uint32) {
605 MOZ_ASSERT(ins->type() == MIRType::Double);
606 tempDef = temp();
607 }
608
609 LAtomicExchangeTypedArrayElement* lir = new (alloc())
610 LAtomicExchangeTypedArrayElement(elements, index, value, tempDef);
611
612 if (useI386ByteRegisters && ins->isByteArray()) {
613 defineFixed(lir, ins, LAllocation(AnyRegister(eax)));
614 } else {
615 define(lir, ins);
616 }
617 }
618
619 void LIRGeneratorX86Shared::lowerAtomicTypedArrayElementBinop(
620 MAtomicTypedArrayElementBinop* ins, bool useI386ByteRegisters) {
621 MOZ_ASSERT(ins->arrayType() != Scalar::Uint8Clamped);
622 MOZ_ASSERT(!Scalar::isFloatingType(ins->arrayType()));
623 MOZ_ASSERT(ins->elements()->type() == MIRType::Elements);
624 MOZ_ASSERT(ins->index()->type() == MIRType::IntPtr);
625
626 const LUse elements = useRegister(ins->elements());
627 const LAllocation index =
628 useRegisterOrIndexConstant(ins->index(), ins->arrayType());
629
630 // Case 1: the result of the operation is not used.
631 //
632 // We'll emit a single instruction: LOCK ADD, LOCK SUB, LOCK AND,
633 // LOCK OR, or LOCK XOR. We can do this even for the Uint32 case.
634
635 if (ins->isForEffect()) {
636 LAllocation value;
637 if (useI386ByteRegisters && ins->isByteArray() &&
638 !ins->value()->isConstant()) {
639 value = useFixed(ins->value(), ebx);
640 } else {
641 value = useRegisterOrConstant(ins->value());
642 }
643
644 LAtomicTypedArrayElementBinopForEffect* lir = new (alloc())
645 LAtomicTypedArrayElementBinopForEffect(elements, index, value);
646
647 add(lir, ins);
648 return;
649 }
650
651 // Case 2: the result of the operation is used.
652 //
653 // For ADD and SUB we'll use XADD:
654 //
655 // movl src, output
656 // lock xaddl output, mem
657 //
658 // For the 8-bit variants XADD needs a byte register for the output.
659 //
660 // For AND/OR/XOR we need to use a CMPXCHG loop:
661 //
662 // movl *mem, eax
663 // L: mov eax, temp
664 // andl src, temp
665 // lock cmpxchg temp, mem ; reads eax also
666 // jnz L
667 // ; result in eax
668 //
669 // Note the placement of L, cmpxchg will update eax with *mem if
670 // *mem does not have the expected value, so reloading it at the
671 // top of the loop would be redundant.
672 //
673 // If the array is not a uint32 array then:
674 // - eax should be the output (one result of the cmpxchg)
675 // - there is a temp, which must have a byte register if
676 // the array has 1-byte elements elements
677 //
678 // If the array is a uint32 array then:
679 // - eax is the first temp
680 // - we also need a second temp
681 //
682 // There are optimization opportunities:
683 // - better register allocation in the x86 8-bit case, Bug #1077036.
684
685 bool bitOp =
686 !(ins->operation() == AtomicOp::Add || ins->operation() == AtomicOp::Sub);
687 bool fixedOutput = true;
688 bool reuseInput = false;
689 LDefinition tempDef1 = LDefinition::BogusTemp();
690 LDefinition tempDef2 = LDefinition::BogusTemp();
691 LAllocation value;
692
693 if (ins->arrayType() == Scalar::Uint32 && IsFloatingPointType(ins->type())) {
694 value = useRegisterOrConstant(ins->value());
695 fixedOutput = false;
696 if (bitOp) {
697 tempDef1 = tempFixed(eax);
698 tempDef2 = temp();
699 } else {
700 tempDef1 = temp();
701 }
702 } else if (useI386ByteRegisters && ins->isByteArray()) {
703 if (ins->value()->isConstant()) {
704 value = useRegisterOrConstant(ins->value());
705 } else {
706 value = useFixed(ins->value(), ebx);
707 }
708 if (bitOp) {
709 tempDef1 = tempFixed(ecx);
710 }
711 } else if (bitOp) {
712 value = useRegisterOrConstant(ins->value());
713 tempDef1 = temp();
714 } else if (ins->value()->isConstant()) {
715 fixedOutput = false;
716 value = useRegisterOrConstant(ins->value());
717 } else {
718 fixedOutput = false;
719 reuseInput = true;
720 value = useRegisterAtStart(ins->value());
721 }
722
723 LAtomicTypedArrayElementBinop* lir = new (alloc())
724 LAtomicTypedArrayElementBinop(elements, index, value, tempDef1, tempDef2);
725
726 if (fixedOutput) {
727 defineFixed(lir, ins, LAllocation(AnyRegister(eax)));
728 } else if (reuseInput) {
729 defineReuseInput(lir, ins, LAtomicTypedArrayElementBinop::ValueIndex);
730 } else {
731 define(lir, ins);
732 }
733 }
734
735 void LIRGenerator::visitCopySign(MCopySign* ins) {
736 MDefinition* lhs = ins->lhs();
737 MDefinition* rhs = ins->rhs();
738
739 MOZ_ASSERT(IsFloatingPointType(lhs->type()));
740 MOZ_ASSERT(lhs->type() == rhs->type());
741 MOZ_ASSERT(lhs->type() == ins->type());
742
743 LInstructionHelper<1, 2, 2>* lir;
744 if (lhs->type() == MIRType::Double) {
745 lir = new (alloc()) LCopySignD();
746 } else {
747 lir = new (alloc()) LCopySignF();
748 }
749
750 // As lowerForFPU, but we want rhs to be in a FP register too.
751 lir->setOperand(0, useRegisterAtStart(lhs));
752 if (!Assembler::HasAVX()) {
753 lir->setOperand(1, willHaveDifferentLIRNodes(lhs, rhs)
754 ? useRegister(rhs)
755 : useRegisterAtStart(rhs));
756 defineReuseInput(lir, ins, 0);
757 } else {
758 lir->setOperand(1, useRegisterAtStart(rhs));
759 define(lir, ins);
760 }
761 }
762
763 // These lowerings are really x86-shared but some Masm APIs are not yet
764 // available on x86.
765
766 // Ternary and binary operators require the dest register to be the same as
767 // their first input register, leading to a pattern of useRegisterAtStart +
768 // defineReuseInput.
769
770 void LIRGenerator::visitWasmTernarySimd128(MWasmTernarySimd128* ins) {
771 #ifdef ENABLE_WASM_SIMD
772 MOZ_ASSERT(ins->v0()->type() == MIRType::Simd128);
773 MOZ_ASSERT(ins->v1()->type() == MIRType::Simd128);
774 MOZ_ASSERT(ins->v2()->type() == MIRType::Simd128);
775 MOZ_ASSERT(ins->type() == MIRType::Simd128);
776
777 switch (ins->simdOp()) {
778 case wasm::SimdOp::V128Bitselect: {
779 // Enforcing lhs == output avoids one setup move. We would like to also
780 // enforce merging the control with the temp (with
781 // usRegisterAtStart(control) and tempCopy()), but the register allocator
782 // ignores those constraints at present.
783 auto* lir = new (alloc()) LWasmTernarySimd128(
784 useRegisterAtStart(ins->v0()), useRegister(ins->v1()),
785 useRegister(ins->v2()), tempSimd128(), ins->simdOp());
786 defineReuseInput(lir, ins, LWasmTernarySimd128::V0Index);
787 break;
788 }
789 case wasm::SimdOp::F32x4RelaxedMadd:
790 case wasm::SimdOp::F32x4RelaxedNmadd:
791 case wasm::SimdOp::F64x2RelaxedMadd:
792 case wasm::SimdOp::F64x2RelaxedNmadd: {
793 auto* lir = new (alloc())
794 LWasmTernarySimd128(useRegister(ins->v0()), useRegister(ins->v1()),
795 useRegisterAtStart(ins->v2()),
796 LDefinition::BogusTemp(), ins->simdOp());
797 defineReuseInput(lir, ins, LWasmTernarySimd128::V2Index);
798 break;
799 }
800 case wasm::SimdOp::I32x4RelaxedDotI8x16I7x16AddS: {
801 auto* lir = new (alloc())
802 LWasmTernarySimd128(useRegister(ins->v0()), useRegister(ins->v1()),
803 useRegisterAtStart(ins->v2()),
804 LDefinition::BogusTemp(), ins->simdOp());
805 defineReuseInput(lir, ins, LWasmTernarySimd128::V2Index);
806 break;
807 }
808 case wasm::SimdOp::I8x16RelaxedLaneSelect:
809 case wasm::SimdOp::I16x8RelaxedLaneSelect:
810 case wasm::SimdOp::I32x4RelaxedLaneSelect:
811 case wasm::SimdOp::I64x2RelaxedLaneSelect: {
812 if (Assembler::HasAVX()) {
813 auto* lir = new (alloc()) LWasmTernarySimd128(
814 useRegisterAtStart(ins->v0()), useRegisterAtStart(ins->v1()),
815 useRegisterAtStart(ins->v2()), LDefinition::BogusTemp(),
816 ins->simdOp());
817 define(lir, ins);
818 } else {
819 auto* lir = new (alloc()) LWasmTernarySimd128(
820 useRegister(ins->v0()), useRegisterAtStart(ins->v1()),
821 useFixed(ins->v2(), vmm0), LDefinition::BogusTemp(), ins->simdOp());
822 defineReuseInput(lir, ins, LWasmTernarySimd128::V1Index);
823 }
824 break;
825 }
826 default:
827 MOZ_CRASH("NYI");
828 }
829 #else
830 MOZ_CRASH("No SIMD");
831 #endif
832 }
833
834 void LIRGenerator::visitWasmBinarySimd128(MWasmBinarySimd128* ins) {
835 #ifdef ENABLE_WASM_SIMD
836 MDefinition* lhs = ins->lhs();
837 MDefinition* rhs = ins->rhs();
838 wasm::SimdOp op = ins->simdOp();
839
840 MOZ_ASSERT(lhs->type() == MIRType::Simd128);
841 MOZ_ASSERT(rhs->type() == MIRType::Simd128);
842 MOZ_ASSERT(ins->type() == MIRType::Simd128);
843
844 // Note MWasmBinarySimd128::foldsTo has already specialized operations that
845 // have a constant operand, so this takes care of more general cases of
846 // reordering, see ReorderCommutative.
847 if (ins->isCommutative()) {
848 ReorderCommutative(&lhs, &rhs, ins);
849 }
850
851 // Swap operands and change operation if necessary, these are all x86/x64
852 // dependent transformations. Except where noted, this is about avoiding
853 // unnecessary moves and fixups in the code generator macros.
854 bool swap = false;
855 switch (op) {
856 case wasm::SimdOp::V128AndNot: {
857 // Code generation requires the operands to be reversed.
858 swap = true;
859 break;
860 }
861 case wasm::SimdOp::I8x16LtS: {
862 swap = true;
863 op = wasm::SimdOp::I8x16GtS;
864 break;
865 }
866 case wasm::SimdOp::I8x16GeS: {
867 swap = true;
868 op = wasm::SimdOp::I8x16LeS;
869 break;
870 }
871 case wasm::SimdOp::I16x8LtS: {
872 swap = true;
873 op = wasm::SimdOp::I16x8GtS;
874 break;
875 }
876 case wasm::SimdOp::I16x8GeS: {
877 swap = true;
878 op = wasm::SimdOp::I16x8LeS;
879 break;
880 }
881 case wasm::SimdOp::I32x4LtS: {
882 swap = true;
883 op = wasm::SimdOp::I32x4GtS;
884 break;
885 }
886 case wasm::SimdOp::I32x4GeS: {
887 swap = true;
888 op = wasm::SimdOp::I32x4LeS;
889 break;
890 }
891 case wasm::SimdOp::F32x4Gt: {
892 swap = true;
893 op = wasm::SimdOp::F32x4Lt;
894 break;
895 }
896 case wasm::SimdOp::F32x4Ge: {
897 swap = true;
898 op = wasm::SimdOp::F32x4Le;
899 break;
900 }
901 case wasm::SimdOp::F64x2Gt: {
902 swap = true;
903 op = wasm::SimdOp::F64x2Lt;
904 break;
905 }
906 case wasm::SimdOp::F64x2Ge: {
907 swap = true;
908 op = wasm::SimdOp::F64x2Le;
909 break;
910 }
911 case wasm::SimdOp::F32x4PMin:
912 case wasm::SimdOp::F32x4PMax:
913 case wasm::SimdOp::F64x2PMin:
914 case wasm::SimdOp::F64x2PMax: {
915 // Code generation requires the operations to be reversed (the rhs is the
916 // output register).
917 swap = true;
918 break;
919 }
920 default:
921 break;
922 }
923 if (swap) {
924 MDefinition* tmp = lhs;
925 lhs = rhs;
926 rhs = tmp;
927 }
928
929 // Allocate temp registers
930 LDefinition tempReg0 = LDefinition::BogusTemp();
931 LDefinition tempReg1 = LDefinition::BogusTemp();
932 switch (op) {
933 case wasm::SimdOp::I64x2Mul:
934 tempReg0 = tempSimd128();
935 break;
936 case wasm::SimdOp::F32x4Min:
937 case wasm::SimdOp::F32x4Max:
938 case wasm::SimdOp::F64x2Min:
939 case wasm::SimdOp::F64x2Max:
940 tempReg0 = tempSimd128();
941 tempReg1 = tempSimd128();
942 break;
943 case wasm::SimdOp::I64x2LtS:
944 case wasm::SimdOp::I64x2GtS:
945 case wasm::SimdOp::I64x2LeS:
946 case wasm::SimdOp::I64x2GeS:
947 // The compareForOrderingInt64x2AVX implementation does not require
948 // temps but needs SSE4.2 support. Checking if both AVX and SSE4.2
949 // are enabled.
950 if (!(Assembler::HasAVX() && Assembler::HasSSE42())) {
951 tempReg0 = tempSimd128();
952 tempReg1 = tempSimd128();
953 }
954 break;
955 default:
956 break;
957 }
958
959 // For binary ops, without AVX support, the Masm API always is usually
960 // (rhs, lhsDest) and requires AtStart+ReuseInput for the lhs.
961 //
962 // For a few ops, the API is actually (rhsDest, lhs) and the rules are the
963 // same but the reversed. We swapped operands above; they will be swapped
964 // again in the code generator to emit the right code.
965 //
966 // If AVX support is enabled, some binary ops can use output as destination,
967 // useRegisterAtStart is applied for both operands and no need for ReuseInput.
968
969 switch (op) {
970 case wasm::SimdOp::I8x16AvgrU:
971 case wasm::SimdOp::I16x8AvgrU:
972 case wasm::SimdOp::I8x16Add:
973 case wasm::SimdOp::I8x16AddSatS:
974 case wasm::SimdOp::I8x16AddSatU:
975 case wasm::SimdOp::I8x16Sub:
976 case wasm::SimdOp::I8x16SubSatS:
977 case wasm::SimdOp::I8x16SubSatU:
978 case wasm::SimdOp::I16x8Mul:
979 case wasm::SimdOp::I16x8MinS:
980 case wasm::SimdOp::I16x8MinU:
981 case wasm::SimdOp::I16x8MaxS:
982 case wasm::SimdOp::I16x8MaxU:
983 case wasm::SimdOp::I32x4Add:
984 case wasm::SimdOp::I32x4Sub:
985 case wasm::SimdOp::I32x4Mul:
986 case wasm::SimdOp::I32x4MinS:
987 case wasm::SimdOp::I32x4MinU:
988 case wasm::SimdOp::I32x4MaxS:
989 case wasm::SimdOp::I32x4MaxU:
990 case wasm::SimdOp::I64x2Add:
991 case wasm::SimdOp::I64x2Sub:
992 case wasm::SimdOp::I64x2Mul:
993 case wasm::SimdOp::F32x4Add:
994 case wasm::SimdOp::F32x4Sub:
995 case wasm::SimdOp::F32x4Mul:
996 case wasm::SimdOp::F32x4Div:
997 case wasm::SimdOp::F64x2Add:
998 case wasm::SimdOp::F64x2Sub:
999 case wasm::SimdOp::F64x2Mul:
1000 case wasm::SimdOp::F64x2Div:
1001 case wasm::SimdOp::F32x4Eq:
1002 case wasm::SimdOp::F32x4Ne:
1003 case wasm::SimdOp::F32x4Lt:
1004 case wasm::SimdOp::F32x4Le:
1005 case wasm::SimdOp::F64x2Eq:
1006 case wasm::SimdOp::F64x2Ne:
1007 case wasm::SimdOp::F64x2Lt:
1008 case wasm::SimdOp::F64x2Le:
1009 case wasm::SimdOp::F32x4PMin:
1010 case wasm::SimdOp::F32x4PMax:
1011 case wasm::SimdOp::F64x2PMin:
1012 case wasm::SimdOp::F64x2PMax:
1013 case wasm::SimdOp::I8x16Swizzle:
1014 case wasm::SimdOp::I8x16RelaxedSwizzle:
1015 case wasm::SimdOp::I8x16Eq:
1016 case wasm::SimdOp::I8x16Ne:
1017 case wasm::SimdOp::I8x16GtS:
1018 case wasm::SimdOp::I8x16LeS:
1019 case wasm::SimdOp::I8x16LtU:
1020 case wasm::SimdOp::I8x16GtU:
1021 case wasm::SimdOp::I8x16LeU:
1022 case wasm::SimdOp::I8x16GeU:
1023 case wasm::SimdOp::I16x8Eq:
1024 case wasm::SimdOp::I16x8Ne:
1025 case wasm::SimdOp::I16x8GtS:
1026 case wasm::SimdOp::I16x8LeS:
1027 case wasm::SimdOp::I16x8LtU:
1028 case wasm::SimdOp::I16x8GtU:
1029 case wasm::SimdOp::I16x8LeU:
1030 case wasm::SimdOp::I16x8GeU:
1031 case wasm::SimdOp::I32x4Eq:
1032 case wasm::SimdOp::I32x4Ne:
1033 case wasm::SimdOp::I32x4GtS:
1034 case wasm::SimdOp::I32x4LeS:
1035 case wasm::SimdOp::I32x4LtU:
1036 case wasm::SimdOp::I32x4GtU:
1037 case wasm::SimdOp::I32x4LeU:
1038 case wasm::SimdOp::I32x4GeU:
1039 case wasm::SimdOp::I64x2Eq:
1040 case wasm::SimdOp::I64x2Ne:
1041 case wasm::SimdOp::I64x2LtS:
1042 case wasm::SimdOp::I64x2GtS:
1043 case wasm::SimdOp::I64x2LeS:
1044 case wasm::SimdOp::I64x2GeS:
1045 case wasm::SimdOp::V128And:
1046 case wasm::SimdOp::V128Or:
1047 case wasm::SimdOp::V128Xor:
1048 case wasm::SimdOp::V128AndNot:
1049 case wasm::SimdOp::F32x4Min:
1050 case wasm::SimdOp::F32x4Max:
1051 case wasm::SimdOp::F64x2Min:
1052 case wasm::SimdOp::F64x2Max:
1053 case wasm::SimdOp::I8x16NarrowI16x8S:
1054 case wasm::SimdOp::I8x16NarrowI16x8U:
1055 case wasm::SimdOp::I16x8NarrowI32x4S:
1056 case wasm::SimdOp::I16x8NarrowI32x4U:
1057 case wasm::SimdOp::I32x4DotI16x8S:
1058 case wasm::SimdOp::I16x8ExtmulLowI8x16S:
1059 case wasm::SimdOp::I16x8ExtmulHighI8x16S:
1060 case wasm::SimdOp::I16x8ExtmulLowI8x16U:
1061 case wasm::SimdOp::I16x8ExtmulHighI8x16U:
1062 case wasm::SimdOp::I32x4ExtmulLowI16x8S:
1063 case wasm::SimdOp::I32x4ExtmulHighI16x8S:
1064 case wasm::SimdOp::I32x4ExtmulLowI16x8U:
1065 case wasm::SimdOp::I32x4ExtmulHighI16x8U:
1066 case wasm::SimdOp::I64x2ExtmulLowI32x4S:
1067 case wasm::SimdOp::I64x2ExtmulHighI32x4S:
1068 case wasm::SimdOp::I64x2ExtmulLowI32x4U:
1069 case wasm::SimdOp::I64x2ExtmulHighI32x4U:
1070 case wasm::SimdOp::I16x8Q15MulrSatS:
1071 case wasm::SimdOp::F32x4RelaxedMin:
1072 case wasm::SimdOp::F32x4RelaxedMax:
1073 case wasm::SimdOp::F64x2RelaxedMin:
1074 case wasm::SimdOp::F64x2RelaxedMax:
1075 case wasm::SimdOp::I16x8RelaxedQ15MulrS:
1076 case wasm::SimdOp::I16x8RelaxedDotI8x16I7x16S:
1077 case wasm::SimdOp::MozPMADDUBSW:
1078 if (isThreeOpAllowed()) {
1079 auto* lir = new (alloc())
1080 LWasmBinarySimd128(useRegisterAtStart(lhs), useRegisterAtStart(rhs),
1081 tempReg0, tempReg1, op);
1082 define(lir, ins);
1083 break;
1084 }
1085 [[fallthrough]];
1086 default: {
1087 LAllocation lhsDestAlloc = useRegisterAtStart(lhs);
1088 LAllocation rhsAlloc = willHaveDifferentLIRNodes(lhs, rhs)
1089 ? useRegister(rhs)
1090 : useRegisterAtStart(rhs);
1091 auto* lir = new (alloc())
1092 LWasmBinarySimd128(lhsDestAlloc, rhsAlloc, tempReg0, tempReg1, op);
1093 defineReuseInput(lir, ins, LWasmBinarySimd128::LhsIndex);
1094 break;
1095 }
1096 }
1097 #else
1098 MOZ_CRASH("No SIMD");
1099 #endif
1100 }
1101
1102 #ifdef ENABLE_WASM_SIMD
1103 bool MWasmTernarySimd128::specializeBitselectConstantMaskAsShuffle(
1104 int8_t shuffle[16]) {
1105 if (simdOp() != wasm::SimdOp::V128Bitselect) {
1106 return false;
1107 }
1108
1109 // Optimization when control vector is a mask with all 0 or all 1 per lane.
1110 // On x86, there is no bitselect, blend operations will be a win,
1111 // e.g. via PBLENDVB or PBLENDW.
1112 SimdConstant constant = static_cast<MWasmFloatConstant*>(v2())->toSimd128();
1113 const SimdConstant::I8x16& bytes = constant.asInt8x16();
1114 for (int8_t i = 0; i < 16; i++) {
1115 if (bytes[i] == -1) {
1116 shuffle[i] = i;
1117 } else if (bytes[i] == 0) {
1118 shuffle[i] = i + 16;
1119 } else {
1120 return false;
1121 }
1122 }
1123 return true;
1124 }
1125 bool MWasmTernarySimd128::canRelaxBitselect() {
1126 wasm::SimdOp simdOp;
1127 if (v2()->isWasmBinarySimd128()) {
1128 simdOp = v2()->toWasmBinarySimd128()->simdOp();
1129 } else if (v2()->isWasmBinarySimd128WithConstant()) {
1130 simdOp = v2()->toWasmBinarySimd128WithConstant()->simdOp();
1131 } else {
1132 return false;
1133 }
1134 switch (simdOp) {
1135 case wasm::SimdOp::I8x16Eq:
1136 case wasm::SimdOp::I8x16Ne:
1137 case wasm::SimdOp::I8x16GtS:
1138 case wasm::SimdOp::I8x16GeS:
1139 case wasm::SimdOp::I8x16LtS:
1140 case wasm::SimdOp::I8x16LeS:
1141 case wasm::SimdOp::I8x16GtU:
1142 case wasm::SimdOp::I8x16GeU:
1143 case wasm::SimdOp::I8x16LtU:
1144 case wasm::SimdOp::I8x16LeU:
1145 case wasm::SimdOp::I16x8Eq:
1146 case wasm::SimdOp::I16x8Ne:
1147 case wasm::SimdOp::I16x8GtS:
1148 case wasm::SimdOp::I16x8GeS:
1149 case wasm::SimdOp::I16x8LtS:
1150 case wasm::SimdOp::I16x8LeS:
1151 case wasm::SimdOp::I16x8GtU:
1152 case wasm::SimdOp::I16x8GeU:
1153 case wasm::SimdOp::I16x8LtU:
1154 case wasm::SimdOp::I16x8LeU:
1155 case wasm::SimdOp::I32x4Eq:
1156 case wasm::SimdOp::I32x4Ne:
1157 case wasm::SimdOp::I32x4GtS:
1158 case wasm::SimdOp::I32x4GeS:
1159 case wasm::SimdOp::I32x4LtS:
1160 case wasm::SimdOp::I32x4LeS:
1161 case wasm::SimdOp::I32x4GtU:
1162 case wasm::SimdOp::I32x4GeU:
1163 case wasm::SimdOp::I32x4LtU:
1164 case wasm::SimdOp::I32x4LeU:
1165 case wasm::SimdOp::I64x2Eq:
1166 case wasm::SimdOp::I64x2Ne:
1167 case wasm::SimdOp::I64x2GtS:
1168 case wasm::SimdOp::I64x2GeS:
1169 case wasm::SimdOp::I64x2LtS:
1170 case wasm::SimdOp::I64x2LeS:
1171 case wasm::SimdOp::F32x4Eq:
1172 case wasm::SimdOp::F32x4Ne:
1173 case wasm::SimdOp::F32x4Gt:
1174 case wasm::SimdOp::F32x4Ge:
1175 case wasm::SimdOp::F32x4Lt:
1176 case wasm::SimdOp::F32x4Le:
1177 case wasm::SimdOp::F64x2Eq:
1178 case wasm::SimdOp::F64x2Ne:
1179 case wasm::SimdOp::F64x2Gt:
1180 case wasm::SimdOp::F64x2Ge:
1181 case wasm::SimdOp::F64x2Lt:
1182 case wasm::SimdOp::F64x2Le:
1183 return true;
1184 default:
1185 break;
1186 }
1187 return false;
1188 }
1189
1190 bool MWasmBinarySimd128::canPmaddubsw() {
1191 MOZ_ASSERT(Assembler::HasSSE3());
1192 return true;
1193 }
1194 #endif
1195
1196 bool MWasmBinarySimd128::specializeForConstantRhs() {
1197 // The order follows MacroAssembler.h, generally
1198 switch (simdOp()) {
1199 // Operations implemented by a single native instruction where it is
1200 // plausible that the rhs (after commutation if available) could be a
1201 // constant.
1202 //
1203 // Swizzle is not here because it was handled earlier in the pipeline.
1204 //
1205 // Integer compares >= and < are not here because they are not supported in
1206 // the hardware.
1207 //
1208 // Floating compares are not here because our patching machinery can't
1209 // handle them yet.
1210 //
1211 // Floating-point min and max (including pmin and pmax) are not here because
1212 // they are not straightforward to implement.
1213 case wasm::SimdOp::I8x16Add:
1214 case wasm::SimdOp::I16x8Add:
1215 case wasm::SimdOp::I32x4Add:
1216 case wasm::SimdOp::I64x2Add:
1217 case wasm::SimdOp::I8x16Sub:
1218 case wasm::SimdOp::I16x8Sub:
1219 case wasm::SimdOp::I32x4Sub:
1220 case wasm::SimdOp::I64x2Sub:
1221 case wasm::SimdOp::I16x8Mul:
1222 case wasm::SimdOp::I32x4Mul:
1223 case wasm::SimdOp::I8x16AddSatS:
1224 case wasm::SimdOp::I8x16AddSatU:
1225 case wasm::SimdOp::I16x8AddSatS:
1226 case wasm::SimdOp::I16x8AddSatU:
1227 case wasm::SimdOp::I8x16SubSatS:
1228 case wasm::SimdOp::I8x16SubSatU:
1229 case wasm::SimdOp::I16x8SubSatS:
1230 case wasm::SimdOp::I16x8SubSatU:
1231 case wasm::SimdOp::I8x16MinS:
1232 case wasm::SimdOp::I8x16MinU:
1233 case wasm::SimdOp::I16x8MinS:
1234 case wasm::SimdOp::I16x8MinU:
1235 case wasm::SimdOp::I32x4MinS:
1236 case wasm::SimdOp::I32x4MinU:
1237 case wasm::SimdOp::I8x16MaxS:
1238 case wasm::SimdOp::I8x16MaxU:
1239 case wasm::SimdOp::I16x8MaxS:
1240 case wasm::SimdOp::I16x8MaxU:
1241 case wasm::SimdOp::I32x4MaxS:
1242 case wasm::SimdOp::I32x4MaxU:
1243 case wasm::SimdOp::V128And:
1244 case wasm::SimdOp::V128Or:
1245 case wasm::SimdOp::V128Xor:
1246 case wasm::SimdOp::I8x16Eq:
1247 case wasm::SimdOp::I8x16Ne:
1248 case wasm::SimdOp::I8x16GtS:
1249 case wasm::SimdOp::I8x16LeS:
1250 case wasm::SimdOp::I16x8Eq:
1251 case wasm::SimdOp::I16x8Ne:
1252 case wasm::SimdOp::I16x8GtS:
1253 case wasm::SimdOp::I16x8LeS:
1254 case wasm::SimdOp::I32x4Eq:
1255 case wasm::SimdOp::I32x4Ne:
1256 case wasm::SimdOp::I32x4GtS:
1257 case wasm::SimdOp::I32x4LeS:
1258 case wasm::SimdOp::I64x2Mul:
1259 case wasm::SimdOp::F32x4Eq:
1260 case wasm::SimdOp::F32x4Ne:
1261 case wasm::SimdOp::F32x4Lt:
1262 case wasm::SimdOp::F32x4Le:
1263 case wasm::SimdOp::F64x2Eq:
1264 case wasm::SimdOp::F64x2Ne:
1265 case wasm::SimdOp::F64x2Lt:
1266 case wasm::SimdOp::F64x2Le:
1267 case wasm::SimdOp::I32x4DotI16x8S:
1268 case wasm::SimdOp::F32x4Add:
1269 case wasm::SimdOp::F64x2Add:
1270 case wasm::SimdOp::F32x4Sub:
1271 case wasm::SimdOp::F64x2Sub:
1272 case wasm::SimdOp::F32x4Div:
1273 case wasm::SimdOp::F64x2Div:
1274 case wasm::SimdOp::F32x4Mul:
1275 case wasm::SimdOp::F64x2Mul:
1276 case wasm::SimdOp::I8x16NarrowI16x8S:
1277 case wasm::SimdOp::I8x16NarrowI16x8U:
1278 case wasm::SimdOp::I16x8NarrowI32x4S:
1279 case wasm::SimdOp::I16x8NarrowI32x4U:
1280 return true;
1281 default:
1282 return false;
1283 }
1284 }
1285
1286 void LIRGenerator::visitWasmBinarySimd128WithConstant(
1287 MWasmBinarySimd128WithConstant* ins) {
1288 #ifdef ENABLE_WASM_SIMD
1289 MDefinition* lhs = ins->lhs();
1290
1291 MOZ_ASSERT(lhs->type() == MIRType::Simd128);
1292 MOZ_ASSERT(ins->type() == MIRType::Simd128);
1293
1294 // Allocate temp registers
1295 LDefinition tempReg = LDefinition::BogusTemp();
1296 switch (ins->simdOp()) {
1297 case wasm::SimdOp::I64x2Mul:
1298 tempReg = tempSimd128();
1299 break;
1300 default:
1301 break;
1302 }
1303
1304 if (isThreeOpAllowed()) {
1305 // The non-destructive versions of instructions will be available
1306 // when AVX is enabled.
1307 LAllocation lhsAlloc = useRegisterAtStart(lhs);
1308 auto* lir = new (alloc())
1309 LWasmBinarySimd128WithConstant(lhsAlloc, tempReg, ins->rhs());
1310 define(lir, ins);
1311 } else {
1312 // Always beneficial to reuse the lhs register here, see discussion in
1313 // visitWasmBinarySimd128() and also code in specializeForConstantRhs().
1314 LAllocation lhsDestAlloc = useRegisterAtStart(lhs);
1315 auto* lir = new (alloc())
1316 LWasmBinarySimd128WithConstant(lhsDestAlloc, tempReg, ins->rhs());
1317 defineReuseInput(lir, ins, LWasmBinarySimd128WithConstant::LhsIndex);
1318 }
1319 #else
1320 MOZ_CRASH("No SIMD");
1321 #endif
1322 }
1323
1324 void LIRGenerator::visitWasmShiftSimd128(MWasmShiftSimd128* ins) {
1325 #ifdef ENABLE_WASM_SIMD
1326 MDefinition* lhs = ins->lhs();
1327 MDefinition* rhs = ins->rhs();
1328
1329 MOZ_ASSERT(lhs->type() == MIRType::Simd128);
1330 MOZ_ASSERT(rhs->type() == MIRType::Int32);
1331 MOZ_ASSERT(ins->type() == MIRType::Simd128);
1332
1333 if (rhs->isConstant()) {
1334 int32_t shiftCountMask;
1335 switch (ins->simdOp()) {
1336 case wasm::SimdOp::I8x16Shl:
1337 case wasm::SimdOp::I8x16ShrU:
1338 case wasm::SimdOp::I8x16ShrS:
1339 shiftCountMask = 7;
1340 break;
1341 case wasm::SimdOp::I16x8Shl:
1342 case wasm::SimdOp::I16x8ShrU:
1343 case wasm::SimdOp::I16x8ShrS:
1344 shiftCountMask = 15;
1345 break;
1346 case wasm::SimdOp::I32x4Shl:
1347 case wasm::SimdOp::I32x4ShrU:
1348 case wasm::SimdOp::I32x4ShrS:
1349 shiftCountMask = 31;
1350 break;
1351 case wasm::SimdOp::I64x2Shl:
1352 case wasm::SimdOp::I64x2ShrU:
1353 case wasm::SimdOp::I64x2ShrS:
1354 shiftCountMask = 63;
1355 break;
1356 default:
1357 MOZ_CRASH("Unexpected shift operation");
1358 }
1359
1360 int32_t shiftCount = rhs->toConstant()->toInt32() & shiftCountMask;
1361 if (shiftCount == shiftCountMask) {
1362 // Check if possible to apply sign replication optimization.
1363 // For some ops the input shall be reused.
1364 switch (ins->simdOp()) {
1365 case wasm::SimdOp::I8x16ShrS: {
1366 auto* lir =
1367 new (alloc()) LWasmSignReplicationSimd128(useRegister(lhs));
1368 define(lir, ins);
1369 return;
1370 }
1371 case wasm::SimdOp::I16x8ShrS:
1372 case wasm::SimdOp::I32x4ShrS:
1373 case wasm::SimdOp::I64x2ShrS: {
1374 auto* lir = new (alloc())
1375 LWasmSignReplicationSimd128(useRegisterAtStart(lhs));
1376 if (isThreeOpAllowed()) {
1377 define(lir, ins);
1378 } else {
1379 // For non-AVX, it is always beneficial to reuse the input.
1380 defineReuseInput(lir, ins, LWasmSignReplicationSimd128::SrcIndex);
1381 }
1382 return;
1383 }
1384 default:
1385 break;
1386 }
1387 }
1388
1389 # ifdef DEBUG
1390 js::wasm::ReportSimdAnalysis("shift -> constant shift");
1391 # endif
1392 auto* lir = new (alloc())
1393 LWasmConstantShiftSimd128(useRegisterAtStart(lhs), shiftCount);
1394 if (isThreeOpAllowed()) {
1395 define(lir, ins);
1396 } else {
1397 // For non-AVX, it is always beneficial to reuse the input.
1398 defineReuseInput(lir, ins, LWasmConstantShiftSimd128::SrcIndex);
1399 }
1400 return;
1401 }
1402
1403 # ifdef DEBUG
1404 js::wasm::ReportSimdAnalysis("shift -> variable shift");
1405 # endif
1406
1407 LDefinition tempReg = LDefinition::BogusTemp();
1408 switch (ins->simdOp()) {
1409 case wasm::SimdOp::I8x16Shl:
1410 case wasm::SimdOp::I8x16ShrS:
1411 case wasm::SimdOp::I8x16ShrU:
1412 case wasm::SimdOp::I64x2ShrS:
1413 tempReg = tempSimd128();
1414 break;
1415 default:
1416 break;
1417 }
1418
1419 // Reusing the input if possible is never detrimental.
1420 LAllocation lhsDestAlloc = useRegisterAtStart(lhs);
1421 LAllocation rhsAlloc = useRegisterAtStart(rhs);
1422 auto* lir =
1423 new (alloc()) LWasmVariableShiftSimd128(lhsDestAlloc, rhsAlloc, tempReg);
1424 defineReuseInput(lir, ins, LWasmVariableShiftSimd128::LhsIndex);
1425 #else
1426 MOZ_CRASH("No SIMD");
1427 #endif
1428 }
1429
1430 void LIRGenerator::visitWasmShuffleSimd128(MWasmShuffleSimd128* ins) {
1431 #ifdef ENABLE_WASM_SIMD
1432 MOZ_ASSERT(ins->lhs()->type() == MIRType::Simd128);
1433 MOZ_ASSERT(ins->rhs()->type() == MIRType::Simd128);
1434 MOZ_ASSERT(ins->type() == MIRType::Simd128);
1435
1436 SimdShuffle s = ins->shuffle();
1437 switch (s.opd) {
1438 case SimdShuffle::Operand::LEFT:
1439 case SimdShuffle::Operand::RIGHT: {
1440 LAllocation src;
1441 bool reuse = false;
1442 switch (*s.permuteOp) {
1443 case SimdPermuteOp::MOVE:
1444 reuse = true;
1445 break;
1446 case SimdPermuteOp::BROADCAST_8x16:
1447 case SimdPermuteOp::BROADCAST_16x8:
1448 case SimdPermuteOp::PERMUTE_8x16:
1449 case SimdPermuteOp::PERMUTE_16x8:
1450 case SimdPermuteOp::PERMUTE_32x4:
1451 case SimdPermuteOp::ROTATE_RIGHT_8x16:
1452 case SimdPermuteOp::SHIFT_LEFT_8x16:
1453 case SimdPermuteOp::SHIFT_RIGHT_8x16:
1454 case SimdPermuteOp::REVERSE_16x8:
1455 case SimdPermuteOp::REVERSE_32x4:
1456 case SimdPermuteOp::REVERSE_64x2:
1457 case SimdPermuteOp::ZERO_EXTEND_8x16_TO_16x8:
1458 case SimdPermuteOp::ZERO_EXTEND_8x16_TO_32x4:
1459 case SimdPermuteOp::ZERO_EXTEND_8x16_TO_64x2:
1460 case SimdPermuteOp::ZERO_EXTEND_16x8_TO_32x4:
1461 case SimdPermuteOp::ZERO_EXTEND_16x8_TO_64x2:
1462 case SimdPermuteOp::ZERO_EXTEND_32x4_TO_64x2:
1463 // No need to reuse registers when VEX instructions are enabled.
1464 reuse = !Assembler::HasAVX();
1465 break;
1466 default:
1467 MOZ_CRASH("Unexpected operator");
1468 }
1469 if (s.opd == SimdShuffle::Operand::LEFT) {
1470 src = useRegisterAtStart(ins->lhs());
1471 } else {
1472 src = useRegisterAtStart(ins->rhs());
1473 }
1474 auto* lir =
1475 new (alloc()) LWasmPermuteSimd128(src, *s.permuteOp, s.control);
1476 if (reuse) {
1477 defineReuseInput(lir, ins, LWasmPermuteSimd128::SrcIndex);
1478 } else {
1479 define(lir, ins);
1480 }
1481 break;
1482 }
1483 case SimdShuffle::Operand::BOTH:
1484 case SimdShuffle::Operand::BOTH_SWAPPED: {
1485 LDefinition temp = LDefinition::BogusTemp();
1486 switch (*s.shuffleOp) {
1487 case SimdShuffleOp::BLEND_8x16:
1488 temp = Assembler::HasAVX() ? tempSimd128() : tempFixed(xmm0);
1489 break;
1490 default:
1491 break;
1492 }
1493 if (isThreeOpAllowed()) {
1494 LAllocation lhs;
1495 LAllocation rhs;
1496 if (s.opd == SimdShuffle::Operand::BOTH) {
1497 lhs = useRegisterAtStart(ins->lhs());
1498 rhs = useRegisterAtStart(ins->rhs());
1499 } else {
1500 lhs = useRegisterAtStart(ins->rhs());
1501 rhs = useRegisterAtStart(ins->lhs());
1502 }
1503 auto* lir = new (alloc())
1504 LWasmShuffleSimd128(lhs, rhs, temp, *s.shuffleOp, s.control);
1505 define(lir, ins);
1506 } else {
1507 LAllocation lhs;
1508 LAllocation rhs;
1509 if (s.opd == SimdShuffle::Operand::BOTH) {
1510 lhs = useRegisterAtStart(ins->lhs());
1511 rhs = useRegister(ins->rhs());
1512 } else {
1513 lhs = useRegisterAtStart(ins->rhs());
1514 rhs = useRegister(ins->lhs());
1515 }
1516 auto* lir = new (alloc())
1517 LWasmShuffleSimd128(lhs, rhs, temp, *s.shuffleOp, s.control);
1518 defineReuseInput(lir, ins, LWasmShuffleSimd128::LhsIndex);
1519 }
1520 break;
1521 }
1522 }
1523 #else
1524 MOZ_CRASH("No SIMD");
1525 #endif
1526 }
1527
1528 void LIRGenerator::visitWasmReplaceLaneSimd128(MWasmReplaceLaneSimd128* ins) {
1529 #ifdef ENABLE_WASM_SIMD
1530 MOZ_ASSERT(ins->lhs()->type() == MIRType::Simd128);
1531 MOZ_ASSERT(ins->type() == MIRType::Simd128);
1532
1533 // If AVX support is disabled, the Masm API is (rhs, lhsDest) and requires
1534 // AtStart+ReuseInput for the lhs. For type reasons, the rhs will never be
1535 // the same as the lhs and is therefore a plain Use.
1536 //
1537 // If AVX support is enabled, useRegisterAtStart is preferred.
1538
1539 if (ins->rhs()->type() == MIRType::Int64) {
1540 if (isThreeOpAllowed()) {
1541 auto* lir = new (alloc()) LWasmReplaceInt64LaneSimd128(
1542 useRegisterAtStart(ins->lhs()), useInt64RegisterAtStart(ins->rhs()));
1543 define(lir, ins);
1544 } else {
1545 auto* lir = new (alloc()) LWasmReplaceInt64LaneSimd128(
1546 useRegisterAtStart(ins->lhs()), useInt64Register(ins->rhs()));
1547 defineReuseInput(lir, ins, LWasmReplaceInt64LaneSimd128::LhsIndex);
1548 }
1549 } else {
1550 if (isThreeOpAllowed()) {
1551 auto* lir = new (alloc()) LWasmReplaceLaneSimd128(
1552 useRegisterAtStart(ins->lhs()), useRegisterAtStart(ins->rhs()));
1553 define(lir, ins);
1554 } else {
1555 auto* lir = new (alloc()) LWasmReplaceLaneSimd128(
1556 useRegisterAtStart(ins->lhs()), useRegister(ins->rhs()));
1557 defineReuseInput(lir, ins, LWasmReplaceLaneSimd128::LhsIndex);
1558 }
1559 }
1560 #else
1561 MOZ_CRASH("No SIMD");
1562 #endif
1563 }
1564
1565 void LIRGenerator::visitWasmScalarToSimd128(MWasmScalarToSimd128* ins) {
1566 #ifdef ENABLE_WASM_SIMD
1567 MOZ_ASSERT(ins->type() == MIRType::Simd128);
1568
1569 switch (ins->input()->type()) {
1570 case MIRType::Int64: {
1571 // 64-bit integer splats.
1572 // Load-and-(sign|zero)extend.
1573 auto* lir = new (alloc())
1574 LWasmInt64ToSimd128(useInt64RegisterAtStart(ins->input()));
1575 define(lir, ins);
1576 break;
1577 }
1578 case MIRType::Float32:
1579 case MIRType::Double: {
1580 // Floating-point splats.
1581 // Ideally we save a move on SSE systems by reusing the input register,
1582 // but since the input and output register types differ, we can't.
1583 auto* lir =
1584 new (alloc()) LWasmScalarToSimd128(useRegisterAtStart(ins->input()));
1585 define(lir, ins);
1586 break;
1587 }
1588 default: {
1589 // 32-bit integer splats.
1590 auto* lir =
1591 new (alloc()) LWasmScalarToSimd128(useRegisterAtStart(ins->input()));
1592 define(lir, ins);
1593 break;
1594 }
1595 }
1596 #else
1597 MOZ_CRASH("No SIMD");
1598 #endif
1599 }
1600
1601 void LIRGenerator::visitWasmUnarySimd128(MWasmUnarySimd128* ins) {
1602 #ifdef ENABLE_WASM_SIMD
1603 MOZ_ASSERT(ins->input()->type() == MIRType::Simd128);
1604 MOZ_ASSERT(ins->type() == MIRType::Simd128);
1605
1606 bool useAtStart = false;
1607 bool reuseInput = false;
1608 LDefinition tempReg = LDefinition::BogusTemp();
1609 switch (ins->simdOp()) {
1610 case wasm::SimdOp::I8x16Neg:
1611 case wasm::SimdOp::I16x8Neg:
1612 case wasm::SimdOp::I32x4Neg:
1613 case wasm::SimdOp::I64x2Neg:
1614 case wasm::SimdOp::I16x8ExtaddPairwiseI8x16S:
1615 // Prefer src != dest to avoid an unconditional src->temp move.
1616 MOZ_ASSERT(!reuseInput);
1617 // If AVX is enabled, we prefer useRegisterAtStart.
1618 useAtStart = isThreeOpAllowed();
1619 break;
1620 case wasm::SimdOp::F32x4Neg:
1621 case wasm::SimdOp::F64x2Neg:
1622 case wasm::SimdOp::F32x4Abs:
1623 case wasm::SimdOp::F64x2Abs:
1624 case wasm::SimdOp::V128Not:
1625 case wasm::SimdOp::F32x4Sqrt:
1626 case wasm::SimdOp::F64x2Sqrt:
1627 case wasm::SimdOp::I8x16Abs:
1628 case wasm::SimdOp::I16x8Abs:
1629 case wasm::SimdOp::I32x4Abs:
1630 case wasm::SimdOp::I64x2Abs:
1631 case wasm::SimdOp::I32x4TruncSatF32x4S:
1632 case wasm::SimdOp::F32x4ConvertI32x4U:
1633 case wasm::SimdOp::I16x8ExtaddPairwiseI8x16U:
1634 case wasm::SimdOp::I32x4ExtaddPairwiseI16x8S:
1635 case wasm::SimdOp::I32x4ExtaddPairwiseI16x8U:
1636 case wasm::SimdOp::I32x4RelaxedTruncF32x4S:
1637 case wasm::SimdOp::I32x4RelaxedTruncF32x4U:
1638 case wasm::SimdOp::I32x4RelaxedTruncF64x2SZero:
1639 case wasm::SimdOp::I32x4RelaxedTruncF64x2UZero:
1640 case wasm::SimdOp::I64x2ExtendHighI32x4S:
1641 case wasm::SimdOp::I64x2ExtendHighI32x4U:
1642 // Prefer src == dest to avoid an unconditional src->dest move
1643 // for better performance in non-AVX mode (e.g. non-PSHUFD use).
1644 useAtStart = true;
1645 reuseInput = !isThreeOpAllowed();
1646 break;
1647 case wasm::SimdOp::I32x4TruncSatF32x4U:
1648 case wasm::SimdOp::I32x4TruncSatF64x2SZero:
1649 case wasm::SimdOp::I32x4TruncSatF64x2UZero:
1650 case wasm::SimdOp::I8x16Popcnt:
1651 tempReg = tempSimd128();
1652 // Prefer src == dest to avoid an unconditional src->dest move
1653 // in non-AVX mode.
1654 useAtStart = true;
1655 reuseInput = !isThreeOpAllowed();
1656 break;
1657 case wasm::SimdOp::I16x8ExtendLowI8x16S:
1658 case wasm::SimdOp::I16x8ExtendHighI8x16S:
1659 case wasm::SimdOp::I16x8ExtendLowI8x16U:
1660 case wasm::SimdOp::I16x8ExtendHighI8x16U:
1661 case wasm::SimdOp::I32x4ExtendLowI16x8S:
1662 case wasm::SimdOp::I32x4ExtendHighI16x8S:
1663 case wasm::SimdOp::I32x4ExtendLowI16x8U:
1664 case wasm::SimdOp::I32x4ExtendHighI16x8U:
1665 case wasm::SimdOp::I64x2ExtendLowI32x4S:
1666 case wasm::SimdOp::I64x2ExtendLowI32x4U:
1667 case wasm::SimdOp::F32x4ConvertI32x4S:
1668 case wasm::SimdOp::F32x4Ceil:
1669 case wasm::SimdOp::F32x4Floor:
1670 case wasm::SimdOp::F32x4Trunc:
1671 case wasm::SimdOp::F32x4Nearest:
1672 case wasm::SimdOp::F64x2Ceil:
1673 case wasm::SimdOp::F64x2Floor:
1674 case wasm::SimdOp::F64x2Trunc:
1675 case wasm::SimdOp::F64x2Nearest:
1676 case wasm::SimdOp::F32x4DemoteF64x2Zero:
1677 case wasm::SimdOp::F64x2PromoteLowF32x4:
1678 case wasm::SimdOp::F64x2ConvertLowI32x4S:
1679 case wasm::SimdOp::F64x2ConvertLowI32x4U:
1680 // Prefer src == dest to exert the lowest register pressure on the
1681 // surrounding code.
1682 useAtStart = true;
1683 MOZ_ASSERT(!reuseInput);
1684 break;
1685 default:
1686 MOZ_CRASH("Unary SimdOp not implemented");
1687 }
1688
1689 LUse inputUse =
1690 useAtStart ? useRegisterAtStart(ins->input()) : useRegister(ins->input());
1691 LWasmUnarySimd128* lir = new (alloc()) LWasmUnarySimd128(inputUse, tempReg);
1692 if (reuseInput) {
1693 defineReuseInput(lir, ins, LWasmUnarySimd128::SrcIndex);
1694 } else {
1695 define(lir, ins);
1696 }
1697 #else
1698 MOZ_CRASH("No SIMD");
1699 #endif
1700 }
1701
1702 void LIRGenerator::visitWasmLoadLaneSimd128(MWasmLoadLaneSimd128* ins) {
1703 #ifdef ENABLE_WASM_SIMD
1704 // A trick: On 32-bit systems, the base pointer is 32 bits (it was bounds
1705 // checked and then chopped). On 64-bit systems, it can be 32 bits or 64
1706 // bits. Either way, it fits in a GPR so we can ignore the
1707 // Register/Register64 distinction here.
1708 # ifndef JS_64BIT
1709 MOZ_ASSERT(ins->base()->type() == MIRType::Int32);
1710 # endif
1711 LUse base = useRegisterAtStart(ins->base());
1712 LUse inputUse = useRegisterAtStart(ins->value());
1713 LAllocation memoryBase = ins->hasMemoryBase()
1714 ? useRegisterAtStart(ins->memoryBase())
1715 : LAllocation();
1716 auto* lir = new (alloc()) LWasmLoadLaneSimd128(base, inputUse, memoryBase);
1717 defineReuseInput(lir, ins, LWasmLoadLaneSimd128::SrcIndex);
1718 #else
1719 MOZ_CRASH("No SIMD");
1720 #endif
1721 }
1722
1723 void LIRGenerator::visitWasmStoreLaneSimd128(MWasmStoreLaneSimd128* ins) {
1724 #ifdef ENABLE_WASM_SIMD
1725 // See comment above.
1726 # ifndef JS_64BIT
1727 MOZ_ASSERT(ins->base()->type() == MIRType::Int32);
1728 # endif
1729 LUse base = useRegisterAtStart(ins->base());
1730 LUse input = useRegisterAtStart(ins->value());
1731 LAllocation memoryBase = ins->hasMemoryBase()
1732 ? useRegisterAtStart(ins->memoryBase())
1733 : LAllocation();
1734 auto* lir = new (alloc()) LWasmStoreLaneSimd128(base, input, memoryBase);
1735 add(lir, ins);
1736 #else
1737 MOZ_CRASH("No SIMD");
1738 #endif
1739 }
1740
1741 #ifdef ENABLE_WASM_SIMD
1742
1743 bool LIRGeneratorX86Shared::canFoldReduceSimd128AndBranch(wasm::SimdOp op) {
1744 switch (op) {
1745 case wasm::SimdOp::V128AnyTrue:
1746 case wasm::SimdOp::I8x16AllTrue:
1747 case wasm::SimdOp::I16x8AllTrue:
1748 case wasm::SimdOp::I32x4AllTrue:
1749 case wasm::SimdOp::I64x2AllTrue:
1750 case wasm::SimdOp::I16x8Bitmask:
1751 return true;
1752 default:
1753 return false;
1754 }
1755 }
1756
1757 bool LIRGeneratorX86Shared::canEmitWasmReduceSimd128AtUses(
1758 MWasmReduceSimd128* ins) {
1759 if (!ins->canEmitAtUses()) {
1760 return false;
1761 }
1762 // Only specific ops generating int32.
1763 if (ins->type() != MIRType::Int32) {
1764 return false;
1765 }
1766 if (!canFoldReduceSimd128AndBranch(ins->simdOp())) {
1767 return false;
1768 }
1769 // If never used then defer (it will be removed).
1770 MUseIterator iter(ins->usesBegin());
1771 if (iter == ins->usesEnd()) {
1772 return true;
1773 }
1774 // We require an MTest consumer.
1775 MNode* node = iter->consumer();
1776 if (!node->isDefinition() || !node->toDefinition()->isTest()) {
1777 return false;
1778 }
1779 // Defer only if there's only one use.
1780 iter++;
1781 return iter == ins->usesEnd();
1782 }
1783
1784 #endif // ENABLE_WASM_SIMD
1785
1786 void LIRGenerator::visitWasmReduceSimd128(MWasmReduceSimd128* ins) {
1787 #ifdef ENABLE_WASM_SIMD
1788 if (canEmitWasmReduceSimd128AtUses(ins)) {
1789 emitAtUses(ins);
1790 return;
1791 }
1792
1793 // Reductions (any_true, all_true, bitmask, extract_lane) uniformly prefer
1794 // useRegisterAtStart:
1795 //
1796 // - In most cases, the input type differs from the output type, so there's no
1797 // conflict and it doesn't really matter.
1798 //
1799 // - For extract_lane(0) on F32x4 and F64x2, input == output results in zero
1800 // code being generated.
1801 //
1802 // - For extract_lane(k > 0) on F32x4 and F64x2, allowing the input register
1803 // to be targeted lowers register pressure if it's the last use of the
1804 // input.
1805
1806 if (ins->type() == MIRType::Int64) {
1807 auto* lir = new (alloc())
1808 LWasmReduceSimd128ToInt64(useRegisterAtStart(ins->input()));
1809 defineInt64(lir, ins);
1810 } else {
1811 // Ideally we would reuse the input register for floating extract_lane if
1812 // the lane is zero, but constraints in the register allocator require the
1813 // input and output register types to be the same.
1814 auto* lir =
1815 new (alloc()) LWasmReduceSimd128(useRegisterAtStart(ins->input()));
1816 define(lir, ins);
1817 }
1818 #else
1819 MOZ_CRASH("No SIMD");
1820 #endif
1821 }