Debian Codegraph @

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