Debian Codegraph @

js/src/jit/mips-shared/Lowering-mips-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/mips-shared/Lowering-mips-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
15 #include "jit/shared/Lowering-shared-inl.h"
16
17 using namespace js;
18 using namespace js::jit;
19
20 using mozilla::FloorLog2;
21
22 LAllocation LIRGeneratorMIPSShared::useByteOpRegister(MDefinition* mir) {
23 return useRegister(mir);
24 }
25
26 LAllocation LIRGeneratorMIPSShared::useByteOpRegisterAtStart(MDefinition* mir) {
27 return useRegisterAtStart(mir);
28 }
29
30 LAllocation LIRGeneratorMIPSShared::useByteOpRegisterOrNonDoubleConstant(
31 MDefinition* mir) {
32 return useRegisterOrNonDoubleConstant(mir);
33 }
34
35 LDefinition LIRGeneratorMIPSShared::tempByteOpRegister() { return temp(); }
36
37 // x = !y
38 void LIRGeneratorMIPSShared::lowerForALU(LInstructionHelper<1, 1, 0>* ins,
39 MDefinition* mir, MDefinition* input) {
40 ins->setOperand(0, useRegister(input));
41 define(
42 ins, mir,
43 LDefinition(LDefinition::TypeFrom(mir->type()), LDefinition::REGISTER));
44 }
45
46 // z = x+y
47 void LIRGeneratorMIPSShared::lowerForALU(LInstructionHelper<1, 2, 0>* ins,
48 MDefinition* mir, MDefinition* lhs,
49 MDefinition* rhs) {
50 ins->setOperand(0, useRegister(lhs));
51 ins->setOperand(1, useRegisterOrConstant(rhs));
52 define(
53 ins, mir,
54 LDefinition(LDefinition::TypeFrom(mir->type()), LDefinition::REGISTER));
55 }
56
57 void LIRGeneratorMIPSShared::lowerForALUInt64(
58 LInstructionHelper<INT64_PIECES, INT64_PIECES, 0>* ins, MDefinition* mir,
59 MDefinition* input) {
60 ins->setInt64Operand(0, useInt64RegisterAtStart(input));
61 defineInt64ReuseInput(ins, mir, 0);
62 }
63
64 void LIRGeneratorMIPSShared::lowerForALUInt64(
65 LInstructionHelper<INT64_PIECES, 2 * INT64_PIECES, 0>* ins,
66 MDefinition* mir, MDefinition* lhs, MDefinition* rhs) {
67 ins->setInt64Operand(0, useInt64RegisterAtStart(lhs));
68 ins->setInt64Operand(INT64_PIECES, willHaveDifferentLIRNodes(lhs, rhs)
69 ? useInt64OrConstant(rhs)
70 : useInt64OrConstantAtStart(rhs));
71 defineInt64ReuseInput(ins, mir, 0);
72 }
73
74 void LIRGeneratorMIPSShared::lowerForMulInt64(LMulI64* ins, MMul* mir,
75 MDefinition* lhs,
76 MDefinition* rhs) {
77 bool needsTemp = false;
78 bool cannotAliasRhs = false;
79 bool reuseInput = true;
80
81 ins->setLhs(useInt64RegisterAtStart(lhs));
82 ins->setRhs((willHaveDifferentLIRNodes(lhs, rhs) || cannotAliasRhs)
83 ? useInt64OrConstant(rhs)
84 : useInt64OrConstantAtStart(rhs));
85
86 if (needsTemp) {
87 ins->setTemp0(temp());
88 }
89 if (reuseInput) {
90 defineInt64ReuseInput(ins, mir, 0);
91 } else {
92 defineInt64(ins, mir);
93 }
94 }
95
96 template <class LInstr>
97 void LIRGeneratorMIPSShared::lowerForShiftInt64(LInstr* ins, MDefinition* mir,
98 MDefinition* lhs,
99 MDefinition* rhs) {
100 if constexpr (std::is_same_v<LInstr, LShiftI64>) {
101 ins->setLhs(useInt64RegisterAtStart(lhs));
102 ins->setRhs(useRegisterOrConstant(rhs));
103 defineInt64ReuseInput(ins, mir, LShiftI64::LhsIndex);
104 } else {
105 ins->setInput(useInt64RegisterAtStart(lhs));
106 ins->setCount(useRegisterOrConstant(rhs));
107 defineInt64ReuseInput(ins, mir, LRotateI64::InputIndex);
108 }
109 }
110
111 template void LIRGeneratorMIPSShared::lowerForShiftInt64(LShiftI64* ins,
112 MDefinition* mir,
113 MDefinition* lhs,
114 MDefinition* rhs);
115 template void LIRGeneratorMIPSShared::lowerForShiftInt64(LRotateI64* ins,
116 MDefinition* mir,
117 MDefinition* lhs,
118 MDefinition* rhs);
119
120 void LIRGeneratorMIPSShared::lowerForFPU(LInstructionHelper<1, 1, 0>* ins,
121 MDefinition* mir, MDefinition* input) {
122 ins->setOperand(0, useRegister(input));
123 define(
124 ins, mir,
125 LDefinition(LDefinition::TypeFrom(mir->type()), LDefinition::REGISTER));
126 }
127
128 template <size_t Temps>
129 void LIRGeneratorMIPSShared::lowerForFPU(LInstructionHelper<1, 2, Temps>* ins,
130 MDefinition* mir, MDefinition* lhs,
131 MDefinition* rhs) {
132 ins->setOperand(0, useRegister(lhs));
133 ins->setOperand(1, useRegister(rhs));
134 define(
135 ins, mir,
136 LDefinition(LDefinition::TypeFrom(mir->type()), LDefinition::REGISTER));
137 }
138
139 template void LIRGeneratorMIPSShared::lowerForFPU(
140 LInstructionHelper<1, 2, 0>* ins, MDefinition* mir, MDefinition* lhs,
141 MDefinition* rhs);
142 template void LIRGeneratorMIPSShared::lowerForFPU(
143 LInstructionHelper<1, 2, 1>* ins, MDefinition* mir, MDefinition* lhs,
144 MDefinition* rhs);
145
146 void LIRGeneratorMIPSShared::lowerWasmBuiltinTruncateToInt32(
147 MWasmBuiltinTruncateToInt32* ins) {
148 MDefinition* opd = ins->input();
149 MOZ_ASSERT(opd->type() == MIRType::Double || opd->type() == MIRType::Float32);
150
151 if (opd->type() == MIRType::Double) {
152 define(new (alloc()) LWasmBuiltinTruncateDToInt32(
153 useRegister(opd), useFixed(ins->instance(), InstanceReg),
154 LDefinition::BogusTemp()),
155 ins);
156 return;
157 }
158
159 define(new (alloc()) LWasmBuiltinTruncateFToInt32(
160 useRegister(opd), useFixed(ins->instance(), InstanceReg),
161 LDefinition::BogusTemp()),
162 ins);
163 }
164
165 void LIRGeneratorMIPSShared::lowerForShift(LInstructionHelper<1, 2, 0>* ins,
166 MDefinition* mir, MDefinition* lhs,
167 MDefinition* rhs) {
168 ins->setOperand(0, useRegister(lhs));
169 ins->setOperand(1, useRegisterOrConstant(rhs));
170 define(ins, mir);
171 }
172
173 void LIRGeneratorMIPSShared::lowerDivI(MDiv* div) {
174 if (div->isUnsigned()) {
175 lowerUDiv(div);
176 return;
177 }
178
179 // Division instructions are slow. Division by constant denominators can be
180 // rewritten to use other instructions.
181 if (div->rhs()->isConstant()) {
182 int32_t rhs = div->rhs()->toConstant()->toInt32();
183 // Check for division by a positive power of two, which is an easy and
184 // important case to optimize. Note that other optimizations are also
185 // possible; division by negative powers of two can be optimized in a
186 // similar manner as positive powers of two, and division by other
187 // constants can be optimized by a reciprocal multiplication technique.
188 int32_t shift = FloorLog2(rhs);
189 if (rhs > 0 && 1 << shift == rhs) {
190 LDivPowTwoI* lir =
191 new (alloc()) LDivPowTwoI(useRegister(div->lhs()), temp(), shift);
192 if (div->fallible()) {
193 assignSnapshot(lir, div->bailoutKind());
194 }
195 define(lir, div);
196 return;
197 }
198 }
199
200 LDivI* lir = new (alloc())
201 LDivI(useRegister(div->lhs()), useRegister(div->rhs()), temp());
202 if (div->fallible()) {
203 assignSnapshot(lir, div->bailoutKind());
204 }
205 define(lir, div);
206 }
207
208 void LIRGeneratorMIPSShared::lowerNegI(MInstruction* ins, MDefinition* input) {
209 define(new (alloc()) LNegI(useRegisterAtStart(input)), ins);
210 }
211
212 void LIRGeneratorMIPSShared::lowerNegI64(MInstruction* ins,
213 MDefinition* input) {
214 defineInt64ReuseInput(new (alloc()) LNegI64(useInt64RegisterAtStart(input)),
215 ins, 0);
216 }
217
218 void LIRGenerator::visitAbs(MAbs* ins) {
219 define(allocateAbs(ins, useRegisterAtStart(ins->input())), ins);
220 }
221
222 void LIRGeneratorMIPSShared::lowerMulI(MMul* mul, MDefinition* lhs,
223 MDefinition* rhs) {
224 LMulI* lir = new (alloc()) LMulI;
225 if (mul->fallible()) {
226 assignSnapshot(lir, mul->bailoutKind());
227 }
228
229 lowerForALU(lir, mul, lhs, rhs);
230 }
231
232 void LIRGeneratorMIPSShared::lowerModI(MMod* mod) {
233 if (mod->isUnsigned()) {
234 lowerUMod(mod);
235 return;
236 }
237
238 if (mod->rhs()->isConstant()) {
239 int32_t rhs = mod->rhs()->toConstant()->toInt32();
240 int32_t shift = FloorLog2(rhs);
241 if (rhs > 0 && 1 << shift == rhs) {
242 LModPowTwoI* lir =
243 new (alloc()) LModPowTwoI(useRegister(mod->lhs()), shift);
244 if (mod->fallible()) {
245 assignSnapshot(lir, mod->bailoutKind());
246 }
247 define(lir, mod);
248 return;
249 } else if (shift < 31 && (1 << (shift + 1)) - 1 == rhs) {
250 LModMaskI* lir = new (alloc())
251 LModMaskI(useRegister(mod->lhs()), temp(), temp(), shift + 1);
252 if (mod->fallible()) {
253 assignSnapshot(lir, mod->bailoutKind());
254 }
255 define(lir, mod);
256 return;
257 }
258 }
259 LModI* lir = new (alloc())
260 LModI(useRegister(mod->lhs()), useRegister(mod->rhs()), temp());
261
262 if (mod->fallible()) {
263 assignSnapshot(lir, mod->bailoutKind());
264 }
265 define(lir, mod);
266 }
267
268 void LIRGenerator::visitPowHalf(MPowHalf* ins) {
269 MDefinition* input = ins->input();
270 MOZ_ASSERT(input->type() == MIRType::Double);
271 LPowHalfD* lir = new (alloc()) LPowHalfD(useRegisterAtStart(input));
272 defineReuseInput(lir, ins, 0);
273 }
274
275 void LIRGeneratorMIPSShared::lowerWasmSelectI(MWasmSelect* select) {
276 auto* lir = new (alloc())
277 LWasmSelect(useRegisterAtStart(select->trueExpr()),
278 useAny(select->falseExpr()), useRegister(select->condExpr()));
279 defineReuseInput(lir, select, LWasmSelect::TrueExprIndex);
280 }
281
282 void LIRGeneratorMIPSShared::lowerWasmSelectI64(MWasmSelect* select) {
283 auto* lir = new (alloc()) LWasmSelectI64(
284 useInt64RegisterAtStart(select->trueExpr()),
285 useInt64(select->falseExpr()), useRegister(select->condExpr()));
286 defineInt64ReuseInput(lir, select, LWasmSelectI64::TrueExprIndex);
287 }
288
289 LTableSwitch* LIRGeneratorMIPSShared::newLTableSwitch(
290 const LAllocation& in, const LDefinition& inputCopy) {
291 return new (alloc()) LTableSwitch(in, inputCopy, temp());
292 }
293
294 LTableSwitchV* LIRGeneratorMIPSShared::newLTableSwitchV(
295 const LBoxAllocation& in) {
296 return new (alloc()) LTableSwitchV(in, temp(), tempDouble(), temp());
297 }
298
299 void LIRGeneratorMIPSShared::lowerUrshD(MUrsh* mir) {
300 MDefinition* lhs = mir->lhs();
301 MDefinition* rhs = mir->rhs();
302
303 MOZ_ASSERT(lhs->type() == MIRType::Int32);
304 MOZ_ASSERT(rhs->type() == MIRType::Int32);
305
306 LUrshD* lir = new (alloc())
307 LUrshD(useRegister(lhs), useRegisterOrConstant(rhs), temp());
308 define(lir, mir);
309 }
310
311 void LIRGeneratorMIPSShared::lowerPowOfTwoI(MPow* mir) {
312 int32_t base = mir->input()->toConstant()->toInt32();
313 MDefinition* power = mir->power();
314
315 auto* lir = new (alloc()) LPowOfTwoI(useRegister(power), base);
316 assignSnapshot(lir, mir->bailoutKind());
317 define(lir, mir);
318 }
319
320 void LIRGeneratorMIPSShared::lowerBigIntPtrLsh(MBigIntPtrLsh* ins) {
321 auto* lir = new (alloc()) LBigIntPtrLsh(
322 useRegister(ins->lhs()), useRegister(ins->rhs()), temp(), temp());
323 assignSnapshot(lir, ins->bailoutKind());
324 define(lir, ins);
325 }
326
327 void LIRGeneratorMIPSShared::lowerBigIntPtrRsh(MBigIntPtrRsh* ins) {
328 auto* lir = new (alloc()) LBigIntPtrRsh(
329 useRegister(ins->lhs()), useRegister(ins->rhs()), temp(), temp());
330 assignSnapshot(lir, ins->bailoutKind());
331 define(lir, ins);
332 }
333
334 void LIRGeneratorMIPSShared::lowerBigIntPtrDiv(MBigIntPtrDiv* ins) {
335 auto* lir = new (alloc())
336 LBigIntPtrDiv(useRegister(ins->lhs()), useRegister(ins->rhs()),
337 LDefinition::BogusTemp(), LDefinition::BogusTemp());
338 assignSnapshot(lir, ins->bailoutKind());
339 define(lir, ins);
340 }
341
342 void LIRGeneratorMIPSShared::lowerBigIntPtrMod(MBigIntPtrMod* ins) {
343 auto* lir = new (alloc())
344 LBigIntPtrMod(useRegister(ins->lhs()), useRegister(ins->rhs()), temp(),
345 LDefinition::BogusTemp());
346 if (ins->canBeDivideByZero()) {
347 assignSnapshot(lir, ins->bailoutKind());
348 }
349 define(lir, ins);
350 }
351
352 void LIRGenerator::visitWasmNeg(MWasmNeg* ins) {
353 if (ins->type() == MIRType::Int32) {
354 define(new (alloc()) LNegI(useRegisterAtStart(ins->input())), ins);
355 } else if (ins->type() == MIRType::Float32) {
356 define(new (alloc()) LNegF(useRegisterAtStart(ins->input())), ins);
357 } else {
358 MOZ_ASSERT(ins->type() == MIRType::Double);
359 define(new (alloc()) LNegD(useRegisterAtStart(ins->input())), ins);
360 }
361 }
362
363 void LIRGenerator::visitWasmLoad(MWasmLoad* ins) {
364 MDefinition* base = ins->base();
365 // 'base' is a GPR but may be of either type. If it is 32-bit, it is
366 // sign-extended on mips64 platform and we should explicitly promote it to
367 // 64-bit by zero-extension when use it as an index register in memory
368 // accesses.
369 MOZ_ASSERT(base->type() == MIRType::Int32 || base->type() == MIRType::Int64);
370
371 LAllocation memoryBase =
372 ins->hasMemoryBase() ? LAllocation(useRegisterAtStart(ins->memoryBase()))
373 : LGeneralReg(HeapReg);
374
375 LAllocation ptr = useRegisterAtStart(base);
376
377 LDefinition ptrCopy = LDefinition::BogusTemp();
378 if (ins->access().offset32()) {
379 ptrCopy = tempCopy(base, 0);
380 }
381
382 if (ins->access().type() == Scalar::Int64) {
383 if (IsUnaligned(ins->access())) {
384 auto* lir =
385 new (alloc()) LWasmUnalignedLoadI64(ptr, memoryBase, ptrCopy, temp());
386 defineInt64(lir, ins);
387 return;
388 }
389
390 auto* lir = new (alloc()) LWasmLoadI64(ptr, memoryBase, ptrCopy);
391 defineInt64(lir, ins);
392 return;
393 }
394
395 if (IsUnaligned(ins->access())) {
396 auto* lir =
397 new (alloc()) LWasmUnalignedLoad(ptr, memoryBase, ptrCopy, temp());
398 define(lir, ins);
399 return;
400 }
401
402 auto* lir = new (alloc()) LWasmLoad(ptr, memoryBase, ptrCopy);
403 define(lir, ins);
404 }
405
406 void LIRGenerator::visitWasmStore(MWasmStore* ins) {
407 MDefinition* base = ins->base();
408 // See comment in visitWasmLoad re the type of 'base'.
409 MOZ_ASSERT(base->type() == MIRType::Int32 || base->type() == MIRType::Int64);
410
411 MDefinition* value = ins->value();
412 LAllocation memoryBase =
413 ins->hasMemoryBase() ? LAllocation(useRegisterAtStart(ins->memoryBase()))
414 : LGeneralReg(HeapReg);
415
416 LAllocation baseAlloc = useRegisterAtStart(base);
417
418 LDefinition ptrCopy = LDefinition::BogusTemp();
419 if (ins->access().offset32()) {
420 ptrCopy = tempCopy(base, 0);
421 }
422
423 if (ins->access().type() == Scalar::Int64) {
424 LInt64Allocation valueAlloc = useInt64RegisterAtStart(value);
425
426 if (IsUnaligned(ins->access())) {
427 auto* lir = new (alloc()) LWasmUnalignedStoreI64(
428 baseAlloc, valueAlloc, memoryBase, ptrCopy, temp());
429 add(lir, ins);
430 return;
431 }
432
433 auto* lir =
434 new (alloc()) LWasmStoreI64(baseAlloc, valueAlloc, memoryBase, ptrCopy);
435 add(lir, ins);
436 return;
437 }
438
439 LAllocation valueAlloc = useRegisterAtStart(value);
440
441 if (IsUnaligned(ins->access())) {
442 auto* lir = new (alloc())
443 LWasmUnalignedStore(baseAlloc, valueAlloc, memoryBase, ptrCopy, temp());
444 add(lir, ins);
445 return;
446 }
447
448 auto* lir =
449 new (alloc()) LWasmStore(baseAlloc, valueAlloc, memoryBase, ptrCopy);
450 add(lir, ins);
451 }
452
453 void LIRGeneratorMIPSShared::lowerUDiv(MDiv* div) {
454 MDefinition* lhs = div->getOperand(0);
455 MDefinition* rhs = div->getOperand(1);
456
457 LUDivOrMod* lir = new (alloc()) LUDivOrMod;
458 lir->setOperand(0, useRegister(lhs));
459 lir->setOperand(1, useRegister(rhs));
460 if (div->fallible()) {
461 assignSnapshot(lir, div->bailoutKind());
462 }
463
464 define(lir, div);
465 }
466
467 void LIRGeneratorMIPSShared::lowerUMod(MMod* mod) {
468 MDefinition* lhs = mod->getOperand(0);
469 MDefinition* rhs = mod->getOperand(1);
470
471 LUDivOrMod* lir = new (alloc()) LUDivOrMod;
472 lir->setOperand(0, useRegister(lhs));
473 lir->setOperand(1, useRegister(rhs));
474 if (mod->fallible()) {
475 assignSnapshot(lir, mod->bailoutKind());
476 }
477
478 define(lir, mod);
479 }
480
481 void LIRGenerator::visitWasmUnsignedToDouble(MWasmUnsignedToDouble* ins) {
482 MOZ_ASSERT(ins->input()->type() == MIRType::Int32);
483 LWasmUint32ToDouble* lir =
484 new (alloc()) LWasmUint32ToDouble(useRegisterAtStart(ins->input()));
485 define(lir, ins);
486 }
487
488 void LIRGenerator::visitWasmUnsignedToFloat32(MWasmUnsignedToFloat32* ins) {
489 MOZ_ASSERT(ins->input()->type() == MIRType::Int32);
490 LWasmUint32ToFloat32* lir =
491 new (alloc()) LWasmUint32ToFloat32(useRegisterAtStart(ins->input()));
492 define(lir, ins);
493 }
494
495 void LIRGenerator::visitAsmJSLoadHeap(MAsmJSLoadHeap* ins) {
496 MOZ_ASSERT(ins->access().offset32() == 0);
497
498 MDefinition* base = ins->base();
499 MOZ_ASSERT(base->type() == MIRType::Int32);
500 LAllocation baseAlloc;
501 LAllocation limitAlloc;
502 // For MIPS it is best to keep the 'base' in a register if a bounds check
503 // is needed.
504 if (base->isConstant() && !ins->needsBoundsCheck()) {
505 // A bounds check is only skipped for a positive index.
506 MOZ_ASSERT(base->toConstant()->toInt32() >= 0);
507 baseAlloc = LAllocation(base->toConstant());
508 } else {
509 baseAlloc = useRegisterAtStart(base);
510 if (ins->needsBoundsCheck()) {
511 MDefinition* boundsCheckLimit = ins->boundsCheckLimit();
512 MOZ_ASSERT(boundsCheckLimit->type() == MIRType::Int32);
513 limitAlloc = useRegisterAtStart(boundsCheckLimit);
514 }
515 }
516
517 define(new (alloc()) LAsmJSLoadHeap(baseAlloc, limitAlloc, LAllocation()),
518 ins);
519 }
520
521 void LIRGenerator::visitAsmJSStoreHeap(MAsmJSStoreHeap* ins) {
522 MOZ_ASSERT(ins->access().offset32() == 0);
523
524 MDefinition* base = ins->base();
525 MOZ_ASSERT(base->type() == MIRType::Int32);
526 LAllocation baseAlloc;
527 LAllocation limitAlloc;
528 if (base->isConstant() && !ins->needsBoundsCheck()) {
529 MOZ_ASSERT(base->toConstant()->toInt32() >= 0);
530 baseAlloc = LAllocation(base->toConstant());
531 } else {
532 baseAlloc = useRegisterAtStart(base);
533 if (ins->needsBoundsCheck()) {
534 MDefinition* boundsCheckLimit = ins->boundsCheckLimit();
535 MOZ_ASSERT(boundsCheckLimit->type() == MIRType::Int32);
536 limitAlloc = useRegisterAtStart(boundsCheckLimit);
537 }
538 }
539
540 add(new (alloc()) LAsmJSStoreHeap(baseAlloc, useRegisterAtStart(ins->value()),
541 limitAlloc, LAllocation()),
542 ins);
543 }
544
545 void LIRGenerator::visitSubstr(MSubstr* ins) {
546 LSubstr* lir = new (alloc())
547 LSubstr(useRegister(ins->string()), useRegister(ins->begin()),
548 useRegister(ins->length()), temp(), temp(), tempByteOpRegister());
549 define(lir, ins);
550 assignSafepoint(lir, ins);
551 }
552
553 void LIRGenerator::visitCompareExchangeTypedArrayElement(
554 MCompareExchangeTypedArrayElement* ins) {
555 MOZ_ASSERT(!Scalar::isFloatingType(ins->arrayType()));
556 MOZ_ASSERT(ins->elements()->type() == MIRType::Elements);
557 MOZ_ASSERT(ins->index()->type() == MIRType::IntPtr);
558
559 const LUse elements = useRegister(ins->elements());
560 const LAllocation index =
561 useRegisterOrIndexConstant(ins->index(), ins->arrayType());
562
563 if (Scalar::isBigIntType(ins->arrayType())) {
564 LInt64Allocation oldval = useInt64Register(ins->oldval());
565 LInt64Allocation newval = useInt64Register(ins->newval());
566
567 auto* lir = new (alloc())
568 LCompareExchangeTypedArrayElement64(elements, index, oldval, newval);
569 defineInt64(lir, ins);
570 return;
571 }
572
573 const LAllocation oldval = useRegister(ins->oldval());
574 const LAllocation newval = useRegister(ins->newval());
575
576 // If the target is a floating register then we need a temp at the
577 // CodeGenerator level for creating the result.
578
579 LDefinition outTemp = LDefinition::BogusTemp();
580 LDefinition valueTemp = LDefinition::BogusTemp();
581 LDefinition offsetTemp = LDefinition::BogusTemp();
582 LDefinition maskTemp = LDefinition::BogusTemp();
583
584 if (ins->arrayType() == Scalar::Uint32 && IsFloatingPointType(ins->type())) {
585 outTemp = temp();
586 }
587
588 if (Scalar::byteSize(ins->arrayType()) < 4) {
589 valueTemp = temp();
590 offsetTemp = temp();
591 maskTemp = temp();
592 }
593
594 LCompareExchangeTypedArrayElement* lir = new (alloc())
595 LCompareExchangeTypedArrayElement(elements, index, oldval, newval,
596 outTemp, valueTemp, offsetTemp,
597 maskTemp);
598
599 define(lir, ins);
600 }
601
602 void LIRGenerator::visitAtomicExchangeTypedArrayElement(
603 MAtomicExchangeTypedArrayElement* ins) {
604 MOZ_ASSERT(ins->elements()->type() == MIRType::Elements);
605 MOZ_ASSERT(ins->index()->type() == MIRType::IntPtr);
606
607 const LUse elements = useRegister(ins->elements());
608 const LAllocation index =
609 useRegisterOrIndexConstant(ins->index(), ins->arrayType());
610
611 if (Scalar::isBigIntType(ins->arrayType())) {
612 LInt64Allocation value = useInt64Register(ins->value());
613
614 auto* lir = new (alloc())
615 LAtomicExchangeTypedArrayElement64(elements, index, value);
616 defineInt64(lir, ins);
617 return;
618 }
619
620 // If the target is a floating register then we need a temp at the
621 // CodeGenerator level for creating the result.
622
623 MOZ_ASSERT(ins->arrayType() <= Scalar::Uint32);
624
625 const LAllocation value = useRegister(ins->value());
626
627 LDefinition outTemp = LDefinition::BogusTemp();
628 LDefinition valueTemp = LDefinition::BogusTemp();
629 LDefinition offsetTemp = LDefinition::BogusTemp();
630 LDefinition maskTemp = LDefinition::BogusTemp();
631
632 if (ins->arrayType() == Scalar::Uint32) {
633 MOZ_ASSERT(ins->type() == MIRType::Double);
634 outTemp = temp();
635 }
636
637 if (Scalar::byteSize(ins->arrayType()) < 4) {
638 valueTemp = temp();
639 offsetTemp = temp();
640 maskTemp = temp();
641 }
642
643 LAtomicExchangeTypedArrayElement* lir =
644 new (alloc()) LAtomicExchangeTypedArrayElement(
645 elements, index, value, outTemp, valueTemp, offsetTemp, maskTemp);
646
647 define(lir, ins);
648 }
649
650 void LIRGenerator::visitWasmCompareExchangeHeap(MWasmCompareExchangeHeap* ins) {
651 MDefinition* base = ins->base();
652 // See comment in visitWasmLoad re the type of 'base'.
653 MOZ_ASSERT(base->type() == MIRType::Int32 || base->type() == MIRType::Int64);
654 LAllocation memoryBase =
655 ins->hasMemoryBase() ? LAllocation(useRegisterAtStart(ins->memoryBase()))
656 : LGeneralReg(HeapReg);
657
658 if (ins->access().type() == Scalar::Int64) {
659 auto* lir = new (alloc()) LWasmCompareExchangeI64(
660 useRegister(base), useInt64Register(ins->oldValue()),
661 useInt64Register(ins->newValue()), memoryBase);
662 defineInt64(lir, ins);
663 return;
664 }
665
666 LDefinition valueTemp = LDefinition::BogusTemp();
667 LDefinition offsetTemp = LDefinition::BogusTemp();
668 LDefinition maskTemp = LDefinition::BogusTemp();
669
670 if (ins->access().byteSize() < 4) {
671 valueTemp = temp();
672 offsetTemp = temp();
673 maskTemp = temp();
674 }
675
676 auto* lir = new (alloc())
677 LWasmCompareExchangeHeap(useRegister(base), useRegister(ins->oldValue()),
678 useRegister(ins->newValue()), memoryBase,
679 valueTemp, offsetTemp, maskTemp);
680
681 define(lir, ins);
682 }
683
684 void LIRGenerator::visitWasmAtomicExchangeHeap(MWasmAtomicExchangeHeap* ins) {
685 MDefinition* base = ins->base();
686 // See comment in visitWasmLoad re the type of 'base'.
687 MOZ_ASSERT(base->type() == MIRType::Int32 || base->type() == MIRType::Int64);
688 LAllocation memoryBase =
689 ins->hasMemoryBase() ? LAllocation(useRegisterAtStart(ins->memoryBase()))
690 : LGeneralReg(HeapReg);
691
692 if (ins->access().type() == Scalar::Int64) {
693 auto* lir = new (alloc()) LWasmAtomicExchangeI64(
694 useRegister(base), useInt64Register(ins->value()), memoryBase);
695 defineInt64(lir, ins);
696 return;
697 }
698
699 LDefinition valueTemp = LDefinition::BogusTemp();
700 LDefinition offsetTemp = LDefinition::BogusTemp();
701 LDefinition maskTemp = LDefinition::BogusTemp();
702
703 if (ins->access().byteSize() < 4) {
704 valueTemp = temp();
705 offsetTemp = temp();
706 maskTemp = temp();
707 }
708
709 auto* lir = new (alloc())
710 LWasmAtomicExchangeHeap(useRegister(base), useRegister(ins->value()),
711 memoryBase, valueTemp, offsetTemp, maskTemp);
712 define(lir, ins);
713 }
714
715 void LIRGenerator::visitWasmAtomicBinopHeap(MWasmAtomicBinopHeap* ins) {
716 MDefinition* base = ins->base();
717 // See comment in visitWasmLoad re the type of 'base'.
718 MOZ_ASSERT(base->type() == MIRType::Int32 || base->type() == MIRType::Int64);
719 LAllocation memoryBase =
720 ins->hasMemoryBase() ? LAllocation(useRegisterAtStart(ins->memoryBase()))
721 : LGeneralReg(HeapReg);
722
723 if (ins->access().type() == Scalar::Int64) {
724 auto* lir = new (alloc())
725 LWasmAtomicBinopI64(useRegister(base), useInt64Register(ins->value()),
726 memoryBase, tempInt64());
727 defineInt64(lir, ins);
728 return;
729 }
730
731 LDefinition valueTemp = LDefinition::BogusTemp();
732 LDefinition offsetTemp = LDefinition::BogusTemp();
733 LDefinition maskTemp = LDefinition::BogusTemp();
734
735 if (ins->access().byteSize() < 4) {
736 valueTemp = temp();
737 offsetTemp = temp();
738 maskTemp = temp();
739 }
740
741 if (!ins->hasUses()) {
742 auto* lir = new (alloc()) LWasmAtomicBinopHeapForEffect(
743 useRegister(base), useRegister(ins->value()), memoryBase, valueTemp,
744 offsetTemp, maskTemp);
745 add(lir, ins);
746 return;
747 }
748
749 auto* lir = new (alloc())
750 LWasmAtomicBinopHeap(useRegister(base), useRegister(ins->value()),
751 memoryBase, valueTemp, offsetTemp, maskTemp);
752
753 define(lir, ins);
754 }
755
756 void LIRGenerator::visitAtomicTypedArrayElementBinop(
757 MAtomicTypedArrayElementBinop* ins) {
758 MOZ_ASSERT(ins->arrayType() != Scalar::Uint8Clamped);
759 MOZ_ASSERT(!Scalar::isFloatingType(ins->arrayType()));
760 MOZ_ASSERT(ins->elements()->type() == MIRType::Elements);
761 MOZ_ASSERT(ins->index()->type() == MIRType::IntPtr);
762
763 const LUse elements = useRegister(ins->elements());
764 const LAllocation index =
765 useRegisterOrIndexConstant(ins->index(), ins->arrayType());
766
767 if (Scalar::isBigIntType(ins->arrayType())) {
768 LInt64Allocation value = useInt64Register(ins->value());
769 LInt64Definition temp = tempInt64();
770
771 // Case 1: the result of the operation is not used.
772
773 if (ins->isForEffect()) {
774 auto* lir = new (alloc()) LAtomicTypedArrayElementBinopForEffect64(
775 elements, index, value, temp);
776 add(lir, ins);
777 return;
778 }
779
780 // Case 2: the result of the operation is used.
781
782 auto* lir = new (alloc())
783 LAtomicTypedArrayElementBinop64(elements, index, value, temp);
784 defineInt64(lir, ins);
785 return;
786 }
787
788 LAllocation value = useRegister(ins->value());
789 LDefinition valueTemp = LDefinition::BogusTemp();
790 LDefinition offsetTemp = LDefinition::BogusTemp();
791 LDefinition maskTemp = LDefinition::BogusTemp();
792
793 if (Scalar::byteSize(ins->arrayType()) < 4) {
794 valueTemp = temp();
795 offsetTemp = temp();
796 maskTemp = temp();
797 }
798
799 if (ins->isForEffect()) {
800 LAtomicTypedArrayElementBinopForEffect* lir =
801 new (alloc()) LAtomicTypedArrayElementBinopForEffect(
802 elements, index, value, valueTemp, offsetTemp, maskTemp);
803 add(lir, ins);
804 return;
805 }
806
807 // For a Uint32Array with a known double result we need a temp for
808 // the intermediate output.
809
810 LDefinition outTemp = LDefinition::BogusTemp();
811
812 if (ins->arrayType() == Scalar::Uint32 && IsFloatingPointType(ins->type())) {
813 outTemp = temp();
814 }
815
816 LAtomicTypedArrayElementBinop* lir =
817 new (alloc()) LAtomicTypedArrayElementBinop(
818 elements, index, value, outTemp, valueTemp, offsetTemp, maskTemp);
819 define(lir, ins);
820 }
821
822 void LIRGenerator::visitCopySign(MCopySign* ins) {
823 MDefinition* lhs = ins->lhs();
824 MDefinition* rhs = ins->rhs();
825
826 MOZ_ASSERT(IsFloatingPointType(lhs->type()));
827 MOZ_ASSERT(lhs->type() == rhs->type());
828 MOZ_ASSERT(lhs->type() == ins->type());
829
830 LInstructionHelper<1, 2, 2>* lir;
831 if (lhs->type() == MIRType::Double) {
832 lir = new (alloc()) LCopySignD();
833 } else {
834 lir = new (alloc()) LCopySignF();
835 }
836
837 lir->setTemp(0, temp());
838 lir->setTemp(1, temp());
839
840 lir->setOperand(0, useRegisterAtStart(lhs));
841 lir->setOperand(1, useRegister(rhs));
842 defineReuseInput(lir, ins, 0);
843 }
844
845 void LIRGenerator::visitExtendInt32ToInt64(MExtendInt32ToInt64* ins) {
846 defineInt64(
847 new (alloc()) LExtendInt32ToInt64(useRegisterAtStart(ins->input())), ins);
848 }
849
850 void LIRGenerator::visitSignExtendInt64(MSignExtendInt64* ins) {
851 defineInt64(new (alloc())
852 LSignExtendInt64(useInt64RegisterAtStart(ins->input())),
853 ins);
854 }
855
856 // On mips we specialize the only cases where compare is {U,}Int32 and select
857 // is {U,}Int32.
858 bool LIRGeneratorShared::canSpecializeWasmCompareAndSelect(
859 MCompare::CompareType compTy, MIRType insTy) {
860 return insTy == MIRType::Int32 && (compTy == MCompare::Compare_Int32 ||
861 compTy == MCompare::Compare_UInt32);
862 }
863
864 void LIRGeneratorShared::lowerWasmCompareAndSelect(MWasmSelect* ins,
865 MDefinition* lhs,
866 MDefinition* rhs,
867 MCompare::CompareType compTy,
868 JSOp jsop) {
869 MOZ_ASSERT(canSpecializeWasmCompareAndSelect(compTy, ins->type()));
870 auto* lir = new (alloc()) LWasmCompareAndSelect(
871 useRegister(lhs), useRegister(rhs), useRegisterAtStart(ins->trueExpr()),
872 useRegister(ins->falseExpr()), compTy, jsop);
873 defineReuseInput(lir, ins, LWasmCompareAndSelect::IfTrueExprIndex);
874 }
875
876 void LIRGenerator::visitWasmTernarySimd128(MWasmTernarySimd128* ins) {
877 MOZ_CRASH("ternary SIMD NYI");
878 }
879
880 void LIRGenerator::visitWasmBinarySimd128(MWasmBinarySimd128* ins) {
881 MOZ_CRASH("binary SIMD NYI");
882 }
883
884 #ifdef ENABLE_WASM_SIMD
885 bool MWasmTernarySimd128::specializeBitselectConstantMaskAsShuffle(
886 int8_t shuffle[16]) {
887 return false;
888 }
889 bool MWasmTernarySimd128::canRelaxBitselect() { return false; }
890
891 bool MWasmBinarySimd128::canPmaddubsw() { return false; }
892 #endif
893
894 bool MWasmBinarySimd128::specializeForConstantRhs() {
895 // Probably many we want to do here
896 return false;
897 }
898
899 void LIRGenerator::visitWasmBinarySimd128WithConstant(
900 MWasmBinarySimd128WithConstant* ins) {
901 MOZ_CRASH("binary SIMD with constant NYI");
902 }
903
904 void LIRGenerator::visitWasmShiftSimd128(MWasmShiftSimd128* ins) {
905 MOZ_CRASH("shift SIMD NYI");
906 }
907
908 void LIRGenerator::visitWasmShuffleSimd128(MWasmShuffleSimd128* ins) {
909 MOZ_CRASH("shuffle SIMD NYI");
910 }
911
912 void LIRGenerator::visitWasmReplaceLaneSimd128(MWasmReplaceLaneSimd128* ins) {
913 MOZ_CRASH("replace-lane SIMD NYI");
914 }
915
916 void LIRGenerator::visitWasmScalarToSimd128(MWasmScalarToSimd128* ins) {
917 MOZ_CRASH("scalar-to-SIMD NYI");
918 }
919
920 void LIRGenerator::visitWasmUnarySimd128(MWasmUnarySimd128* ins) {
921 MOZ_CRASH("unary SIMD NYI");
922 }
923
924 void LIRGenerator::visitWasmReduceSimd128(MWasmReduceSimd128* ins) {
925 MOZ_CRASH("reduce-SIMD NYI");
926 }
927
928 void LIRGenerator::visitWasmLoadLaneSimd128(MWasmLoadLaneSimd128* ins) {
929 MOZ_CRASH("load-lane SIMD NYI");
930 }
931
932 void LIRGenerator::visitWasmStoreLaneSimd128(MWasmStoreLaneSimd128* ins) {
933 MOZ_CRASH("store-lane SIMD NYI");
934 }