Debian Codegraph @

js/src/jit/x86/Lowering-x86.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/Lowering-x86.h"
8
9 #include "jit/Lowering.h"
10 #include "jit/MIR-wasm.h"
11 #include "jit/MIR.h"
12 #include "jit/x86/Assembler-x86.h"
13
14 #include "jit/shared/Lowering-shared-inl.h"
15
16 using namespace js;
17 using namespace js::jit;
18
19 LBoxAllocation LIRGeneratorX86::useBoxFixed(MDefinition* mir, Register reg1,
20 Register reg2, bool useAtStart) {
21 MOZ_ASSERT(mir->type() == MIRType::Value);
22 MOZ_ASSERT(reg1 != reg2);
23
24 ensureDefined(mir);
25 return LBoxAllocation(LUse(reg1, mir->virtualRegister(), useAtStart),
26 LUse(reg2, VirtualRegisterOfPayload(mir), useAtStart));
27 }
28
29 LAllocation LIRGeneratorX86::useByteOpRegister(MDefinition* mir) {
30 return useFixed(mir, eax);
31 }
32
33 LAllocation LIRGeneratorX86::useByteOpRegisterAtStart(MDefinition* mir) {
34 return useFixedAtStart(mir, eax);
35 }
36
37 LAllocation LIRGeneratorX86::useByteOpRegisterOrNonDoubleConstant(
38 MDefinition* mir) {
39 return useFixed(mir, eax);
40 }
41
42 LDefinition LIRGeneratorX86::tempByteOpRegister() { return tempFixed(eax); }
43
44 void LIRGenerator::visitBox(MBox* box) {
45 MDefinition* inner = box->getOperand(0);
46
47 // If the box wrapped a double, it needs a new register.
48 if (IsFloatingPointType(inner->type())) {
49 LDefinition spectreTemp =
50 JitOptions.spectreValueMasking ? temp() : LDefinition::BogusTemp();
51 defineBox(new (alloc()) LBoxFloatingPoint(useRegisterAtStart(inner),
52 tempCopy(inner, 0), spectreTemp,
53 inner->type()),
54 box);
55 return;
56 }
57
58 if (box->canEmitAtUses()) {
59 emitAtUses(box);
60 return;
61 }
62
63 if (inner->isConstant()) {
64 defineBox(new (alloc()) LValue(inner->toConstant()->toJSValue()), box);
65 return;
66 }
67
68 LBox* lir = new (alloc()) LBox(use(inner), inner->type());
69
70 // Otherwise, we should not define a new register for the payload portion
71 // of the output, so bypass defineBox().
72 uint32_t vreg = getVirtualRegister();
73
74 // Note that because we're using BogusTemp(), we do not change the type of
75 // the definition. We also do not define the first output as "TYPE",
76 // because it has no corresponding payload at (vreg + 1). Also note that
77 // although we copy the input's original type for the payload half of the
78 // definition, this is only for clarity. BogusTemp() definitions are
79 // ignored.
80 lir->setDef(0, LDefinition(vreg, LDefinition::GENERAL));
81 lir->setDef(1, LDefinition::BogusTemp());
82 box->setVirtualRegister(vreg);
83 add(lir);
84 }
85
86 void LIRGenerator::visitUnbox(MUnbox* unbox) {
87 MDefinition* inner = unbox->getOperand(0);
88
89 // An unbox on x86 reads in a type tag (either in memory or a register) and
90 // a payload. Unlike most instructions consuming a box, we ask for the type
91 // second, so that the result can re-use the first input.
92 MOZ_ASSERT(inner->type() == MIRType::Value);
93
94 ensureDefined(inner);
95
96 if (IsFloatingPointType(unbox->type())) {
97 MOZ_ASSERT(unbox->type() == MIRType::Double);
98 auto* lir = new (alloc()) LUnboxFloatingPoint(useBox(inner));
99 if (unbox->fallible()) {
100 assignSnapshot(lir, unbox->bailoutKind());
101 }
102 define(lir, unbox);
103 return;
104 }
105
106 // Swap the order we use the box pieces so we can re-use the payload register.
107 LUnbox* lir = new (alloc()) LUnbox;
108 bool reusePayloadReg = !JitOptions.spectreValueMasking ||
109 unbox->type() == MIRType::Int32 ||
110 unbox->type() == MIRType::Boolean;
111 if (reusePayloadReg) {
112 lir->setOperand(0, usePayloadInRegisterAtStart(inner));
113 lir->setOperand(1, useType(inner, LUse::ANY));
114 } else {
115 lir->setOperand(0, usePayload(inner, LUse::REGISTER));
116 lir->setOperand(1, useType(inner, LUse::ANY));
117 }
118
119 if (unbox->fallible()) {
120 assignSnapshot(lir, unbox->bailoutKind());
121 }
122
123 // Types and payloads form two separate intervals. If the type becomes dead
124 // before the payload, it could be used as a Value without the type being
125 // recoverable. Unbox's purpose is to eagerly kill the definition of a type
126 // tag, so keeping both alive (for the purpose of gcmaps) is unappealing.
127 // Instead, we create a new virtual register.
128 if (reusePayloadReg) {
129 defineReuseInput(lir, unbox, 0);
130 } else {
131 define(lir, unbox);
132 }
133 }
134
135 void LIRGenerator::visitReturnImpl(MDefinition* opd, bool isGenerator) {
136 MOZ_ASSERT(opd->type() == MIRType::Value);
137
138 LReturn* ins = new (alloc()) LReturn(isGenerator);
139 ins->setOperand(0, LUse(JSReturnReg_Type));
140 ins->setOperand(1, LUse(JSReturnReg_Data));
141 fillBoxUses(ins, 0, opd);
142 add(ins);
143 }
144
145 void LIRGeneratorX86::lowerUntypedPhiInput(MPhi* phi, uint32_t inputPosition,
146 LBlock* block, size_t lirIndex) {
147 MDefinition* operand = phi->getOperand(inputPosition);
148 LPhi* type = block->getPhi(lirIndex + VREG_TYPE_OFFSET);
149 LPhi* payload = block->getPhi(lirIndex + VREG_DATA_OFFSET);
150 type->setOperand(
151 inputPosition,
152 LUse(operand->virtualRegister() + VREG_TYPE_OFFSET, LUse::ANY));
153 payload->setOperand(inputPosition,
154 LUse(VirtualRegisterOfPayload(operand), LUse::ANY));
155 }
156
157 void LIRGeneratorX86::defineInt64Phi(MPhi* phi, size_t lirIndex) {
158 LPhi* low = current->getPhi(lirIndex + INT64LOW_INDEX);
159 LPhi* high = current->getPhi(lirIndex + INT64HIGH_INDEX);
160
161 uint32_t lowVreg = getVirtualRegister();
162
163 phi->setVirtualRegister(lowVreg);
164
165 uint32_t highVreg = getVirtualRegister();
166 MOZ_ASSERT(lowVreg + INT64HIGH_INDEX == highVreg + INT64LOW_INDEX);
167
168 low->setDef(0, LDefinition(lowVreg, LDefinition::INT32));
169 high->setDef(0, LDefinition(highVreg, LDefinition::INT32));
170 annotate(high);
171 annotate(low);
172 }
173
174 void LIRGeneratorX86::lowerInt64PhiInput(MPhi* phi, uint32_t inputPosition,
175 LBlock* block, size_t lirIndex) {
176 MDefinition* operand = phi->getOperand(inputPosition);
177 LPhi* low = block->getPhi(lirIndex + INT64LOW_INDEX);
178 LPhi* high = block->getPhi(lirIndex + INT64HIGH_INDEX);
179 low->setOperand(inputPosition,
180 LUse(operand->virtualRegister() + INT64LOW_INDEX, LUse::ANY));
181 high->setOperand(
182 inputPosition,
183 LUse(operand->virtualRegister() + INT64HIGH_INDEX, LUse::ANY));
184 }
185
186 void LIRGeneratorX86::lowerForALUInt64(
187 LInstructionHelper<INT64_PIECES, INT64_PIECES, 0>* ins, MDefinition* mir,
188 MDefinition* input) {
189 ins->setInt64Operand(0, useInt64RegisterAtStart(input));
190 defineInt64ReuseInput(ins, mir, 0);
191 }
192
193 void LIRGeneratorX86::lowerForALUInt64(
194 LInstructionHelper<INT64_PIECES, 2 * INT64_PIECES, 0>* ins,
195 MDefinition* mir, MDefinition* lhs, MDefinition* rhs) {
196 ins->setInt64Operand(0, useInt64RegisterAtStart(lhs));
197 ins->setInt64Operand(INT64_PIECES, useInt64OrConstant(rhs));
198 defineInt64ReuseInput(ins, mir, 0);
199 }
200
201 void LIRGeneratorX86::lowerForMulInt64(LMulI64* ins, MMul* mir,
202 MDefinition* lhs, MDefinition* rhs) {
203 bool needsTemp = true;
204
205 if (rhs->isConstant()) {
206 int64_t constant = rhs->toConstant()->toInt64();
207 int32_t shift = mozilla::FloorLog2(constant);
208 // See special cases in CodeGeneratorX86Shared::visitMulI64.
209 if (constant >= -1 && constant <= 2) {
210 needsTemp = false;
211 }
212 if (constant > 0 && int64_t(1) << shift == constant) {
213 needsTemp = false;
214 }
215 }
216
217 // MulI64 on x86 needs output to be in edx, eax;
218 ins->setLhs(useInt64Fixed(lhs, Register64(edx, eax), /*useAtStart = */ true));
219 ins->setRhs(useInt64OrConstant(rhs));
220 if (needsTemp) {
221 ins->setTemp0(temp());
222 }
223
224 defineInt64Fixed(ins, mir,
225 LInt64Allocation(LAllocation(AnyRegister(edx)),
226 LAllocation(AnyRegister(eax))));
227 }
228
229 void LIRGenerator::visitCompareExchangeTypedArrayElement(
230 MCompareExchangeTypedArrayElement* ins) {
231 MOZ_ASSERT(ins->elements()->type() == MIRType::Elements);
232 MOZ_ASSERT(ins->index()->type() == MIRType::IntPtr);
233
234 if (Scalar::isBigIntType(ins->arrayType())) {
235 LUse elements = useRegister(ins->elements());
236 LAllocation index =
237 useRegisterOrIndexConstant(ins->index(), ins->arrayType());
238 LInt64Allocation oldval =
239 useInt64FixedAtStart(ins->oldval(), Register64(edx, eax));
240 LInt64Allocation newval =
241 useInt64Fixed(ins->newval(), Register64(ecx, ebx));
242
243 auto* lir = new (alloc())
244 LCompareExchangeTypedArrayElement64(elements, index, oldval, newval);
245 defineInt64Fixed(lir, ins,
246 LInt64Allocation(LAllocation(AnyRegister(edx)),
247 LAllocation(AnyRegister(eax))));
248 return;
249 }
250
251 lowerCompareExchangeTypedArrayElement(ins, /* useI386ByteRegisters = */ true);
252 }
253
254 void LIRGenerator::visitAtomicExchangeTypedArrayElement(
255 MAtomicExchangeTypedArrayElement* ins) {
256 MOZ_ASSERT(ins->elements()->type() == MIRType::Elements);
257 MOZ_ASSERT(ins->index()->type() == MIRType::IntPtr);
258
259 if (Scalar::isBigIntType(ins->arrayType())) {
260 LUse elements = useRegister(ins->elements());
261 LAllocation index =
262 useRegisterOrIndexConstant(ins->index(), ins->arrayType());
263 LInt64Allocation value = useInt64Fixed(ins->value(), Register64(ecx, ebx));
264
265 auto* lir = new (alloc())
266 LAtomicExchangeTypedArrayElement64(elements, index, value);
267 defineInt64Fixed(lir, ins,
268 LInt64Allocation(LAllocation(AnyRegister(edx)),
269 LAllocation(AnyRegister(eax))));
270 return;
271 }
272
273 lowerAtomicExchangeTypedArrayElement(ins, /*useI386ByteRegisters=*/true);
274 }
275
276 void LIRGenerator::visitAtomicTypedArrayElementBinop(
277 MAtomicTypedArrayElementBinop* ins) {
278 MOZ_ASSERT(ins->elements()->type() == MIRType::Elements);
279 MOZ_ASSERT(ins->index()->type() == MIRType::IntPtr);
280
281 if (Scalar::isBigIntType(ins->arrayType())) {
282 LUse elements = useRegister(ins->elements());
283 LAllocation index =
284 useRegisterOrIndexConstant(ins->index(), ins->arrayType());
285 LInt64Allocation value = useInt64Fixed(ins->value(), Register64(ecx, ebx));
286
287 // Case 1: the result of the operation is not used.
288 if (ins->isForEffect()) {
289 LInt64Definition temp = tempInt64Fixed(Register64(edx, eax));
290
291 auto* lir = new (alloc()) LAtomicTypedArrayElementBinopForEffect64(
292 elements, index, value, temp);
293 add(lir, ins);
294 return;
295 }
296
297 // Case 2: the result of the operation is used.
298
299 auto* lir =
300 new (alloc()) LAtomicTypedArrayElementBinop64(elements, index, value);
301 defineInt64Fixed(lir, ins,
302 LInt64Allocation(LAllocation(AnyRegister(edx)),
303 LAllocation(AnyRegister(eax))));
304 return;
305 }
306
307 lowerAtomicTypedArrayElementBinop(ins, /* useI386ByteRegisters = */ true);
308 }
309
310 void LIRGeneratorX86::lowerAtomicLoad64(MLoadUnboxedScalar* ins) {
311 const LUse elements = useRegister(ins->elements());
312 const LAllocation index =
313 useRegisterOrIndexConstant(ins->index(), ins->storageType());
314
315 auto* lir = new (alloc())
316 LAtomicLoad64(elements, index, tempInt64Fixed(Register64(ecx, ebx)));
317 defineInt64Fixed(lir, ins,
318 LInt64Allocation(LAllocation(AnyRegister(edx)),
319 LAllocation(AnyRegister(eax))));
320 }
321
322 void LIRGeneratorX86::lowerAtomicStore64(MStoreUnboxedScalar* ins) {
323 LUse elements = useRegister(ins->elements());
324 LAllocation index =
325 useRegisterOrIndexConstant(ins->index(), ins->writeType());
326 LInt64Allocation value = useInt64Fixed(ins->value(), Register64(ecx, ebx));
327 LInt64Definition temp = tempInt64Fixed(Register64(edx, eax));
328
329 add(new (alloc()) LAtomicStore64(elements, index, value, temp), ins);
330 }
331
332 void LIRGenerator::visitWasmUnsignedToDouble(MWasmUnsignedToDouble* ins) {
333 MOZ_ASSERT(ins->input()->type() == MIRType::Int32);
334 LWasmUint32ToDouble* lir = new (alloc())
335 LWasmUint32ToDouble(useRegisterAtStart(ins->input()), temp());
336 define(lir, ins);
337 }
338
339 void LIRGenerator::visitWasmUnsignedToFloat32(MWasmUnsignedToFloat32* ins) {
340 MOZ_ASSERT(ins->input()->type() == MIRType::Int32);
341 LWasmUint32ToFloat32* lir = new (alloc())
342 LWasmUint32ToFloat32(useRegisterAtStart(ins->input()), temp());
343 define(lir, ins);
344 }
345
346 // If the base is a constant, and it is zero or its offset is zero, then
347 // code generation will fold the values into the access. Allocate the
348 // pointer to a register only if that can't happen.
349
350 static bool OptimizableConstantAccess(MDefinition* base,
351 const wasm::MemoryAccessDesc& access) {
352 MOZ_ASSERT(base->isConstant());
353 MOZ_ASSERT(base->type() == MIRType::Int32);
354
355 if (!(base->toConstant()->isInt32(0) || access.offset32() == 0)) {
356 return false;
357 }
358 if (access.type() == Scalar::Int64) {
359 // For int64 accesses on 32-bit systems we will need to add another offset
360 // of 4 to access the high part of the value; make sure this does not
361 // overflow the value.
362 int32_t v;
363 if (base->toConstant()->isInt32(0)) {
364 v = access.offset32();
365 } else {
366 v = base->toConstant()->toInt32();
367 }
368 return v <= int32_t(INT32_MAX - INT64HIGH_OFFSET);
369 }
370 return true;
371 }
372
373 void LIRGenerator::visitWasmLoad(MWasmLoad* ins) {
374 MDefinition* base = ins->base();
375 MOZ_ASSERT(base->type() == MIRType::Int32);
376
377 MDefinition* memoryBase = ins->memoryBase();
378 MOZ_ASSERT(memoryBase->type() == MIRType::Pointer);
379
380 if (ins->access().type() == Scalar::Int64 && ins->access().isAtomic()) {
381 auto* lir = new (alloc())
382 LWasmAtomicLoadI64(useRegister(base), useRegister(memoryBase),
383 tempInt64Fixed(Register64(ecx, ebx)));
384 defineInt64Fixed(lir, ins,
385 LInt64Allocation(LAllocation(AnyRegister(edx)),
386 LAllocation(AnyRegister(eax))));
387 return;
388 }
389
390 LAllocation baseAlloc;
391 if (!base->isConstant() || !OptimizableConstantAccess(base, ins->access())) {
392 baseAlloc = ins->type() == MIRType::Int64 ? useRegister(base)
393 : useRegisterAtStart(base);
394 }
395
396 if (ins->type() != MIRType::Int64) {
397 auto* lir =
398 new (alloc()) LWasmLoad(baseAlloc, useRegisterAtStart(memoryBase));
399 define(lir, ins);
400 return;
401 }
402
403 // "AtStart" register usage does not work for the 64-bit case because we
404 // clobber two registers for the result and may need two registers for a
405 // scaled address; we can't guarantee non-interference.
406
407 auto* lir = new (alloc()) LWasmLoadI64(baseAlloc, useRegister(memoryBase));
408
409 Scalar::Type accessType = ins->access().type();
410 if (accessType == Scalar::Int8 || accessType == Scalar::Int16 ||
411 accessType == Scalar::Int32) {
412 // We use cdq to sign-extend the result and cdq demands these registers.
413 defineInt64Fixed(lir, ins,
414 LInt64Allocation(LAllocation(AnyRegister(edx)),
415 LAllocation(AnyRegister(eax))));
416 return;
417 }
418
419 defineInt64(lir, ins);
420 }
421
422 void LIRGenerator::visitWasmStore(MWasmStore* ins) {
423 MDefinition* base = ins->base();
424 MOZ_ASSERT(base->type() == MIRType::Int32);
425
426 MDefinition* memoryBase = ins->memoryBase();
427 MOZ_ASSERT(memoryBase->type() == MIRType::Pointer);
428
429 if (ins->access().type() == Scalar::Int64 && ins->access().isAtomic()) {
430 auto* lir = new (alloc()) LWasmAtomicStoreI64(
431 useRegister(base), useInt64Fixed(ins->value(), Register64(ecx, ebx)),
432 useRegister(memoryBase), tempInt64Fixed(Register64(edx, eax)));
433 add(lir, ins);
434 return;
435 }
436
437 LAllocation baseAlloc;
438 if (!base->isConstant() || !OptimizableConstantAccess(base, ins->access())) {
439 baseAlloc = useRegisterAtStart(base);
440 }
441
442 LAllocation valueAlloc;
443 switch (ins->access().type()) {
444 case Scalar::Int8:
445 case Scalar::Uint8:
446 // See comment for LIRGeneratorX86::useByteOpRegister.
447 if (ins->value()->type() != MIRType::Int64) {
448 valueAlloc = useFixed(ins->value(), eax);
449 } else {
450 valueAlloc = useLowWordFixed(ins->value(), eax);
451 }
452 break;
453 case Scalar::Int16:
454 case Scalar::Uint16:
455 case Scalar::Int32:
456 case Scalar::Uint32:
457 case Scalar::Float32:
458 case Scalar::Float64:
459 // For now, don't allow constant values. The immediate operand affects
460 // instruction layout which affects patching.
461 if (ins->value()->type() != MIRType::Int64) {
462 valueAlloc = useRegisterAtStart(ins->value());
463 } else {
464 valueAlloc = useLowWordRegisterAtStart(ins->value());
465 }
466 break;
467 case Scalar::Simd128:
468 #ifdef ENABLE_WASM_SIMD
469 valueAlloc = useRegisterAtStart(ins->value());
470 break;
471 #else
472 MOZ_CRASH("unexpected array type");
473 #endif
474 case Scalar::Int64: {
475 LInt64Allocation valueAlloc = useInt64RegisterAtStart(ins->value());
476 auto* lir = new (alloc())
477 LWasmStoreI64(baseAlloc, valueAlloc, useRegisterAtStart(memoryBase));
478 add(lir, ins);
479 return;
480 }
481 case Scalar::Uint8Clamped:
482 case Scalar::BigInt64:
483 case Scalar::BigUint64:
484 case Scalar::Float16:
485 case Scalar::MaxTypedArrayViewType:
486 MOZ_CRASH("unexpected array type");
487 }
488
489 auto* lir = new (alloc())
490 LWasmStore(baseAlloc, valueAlloc, useRegisterAtStart(memoryBase));
491 add(lir, ins);
492 }
493
494 void LIRGenerator::visitWasmCompareExchangeHeap(MWasmCompareExchangeHeap* ins) {
495 MDefinition* base = ins->base();
496 MOZ_ASSERT(base->type() == MIRType::Int32);
497
498 MDefinition* memoryBase = ins->memoryBase();
499 MOZ_ASSERT(memoryBase->type() == MIRType::Pointer);
500
501 if (ins->access().type() == Scalar::Int64) {
502 auto* lir = new (alloc()) LWasmCompareExchangeI64(
503 useRegisterAtStart(base),
504 useInt64FixedAtStart(ins->oldValue(), Register64(edx, eax)),
505 useInt64FixedAtStart(ins->newValue(), Register64(ecx, ebx)),
506 useRegisterAtStart(memoryBase));
507 defineInt64Fixed(lir, ins,
508 LInt64Allocation(LAllocation(AnyRegister(edx)),
509 LAllocation(AnyRegister(eax))));
510 return;
511 }
512
513 MOZ_ASSERT(ins->access().type() < Scalar::Float32);
514
515 bool byteArray = byteSize(ins->access().type()) == 1;
516
517 // Register allocation:
518 //
519 // The output may not be used, but eax will be clobbered regardless
520 // so pin the output to eax.
521 //
522 // oldval must be in a register.
523 //
524 // newval must be in a register. If the source is a byte array
525 // then newval must be a register that has a byte size: this must
526 // be ebx, ecx, or edx (eax is taken).
527 //
528 // Bug #1077036 describes some optimization opportunities.
529
530 const LAllocation oldval = useRegister(ins->oldValue());
531 const LAllocation newval =
532 byteArray ? useFixed(ins->newValue(), ebx) : useRegister(ins->newValue());
533
534 auto* lir = new (alloc()) LWasmCompareExchangeHeap(
535 useRegister(base), oldval, newval, useRegister(memoryBase), temp());
536 defineFixed(lir, ins, LAllocation(AnyRegister(eax)));
537 }
538
539 void LIRGenerator::visitWasmAtomicExchangeHeap(MWasmAtomicExchangeHeap* ins) {
540 MDefinition* memoryBase = ins->memoryBase();
541 MOZ_ASSERT(memoryBase->type() == MIRType::Pointer);
542
543 if (ins->access().type() == Scalar::Int64) {
544 MDefinition* base = ins->base();
545 auto* lir = new (alloc()) LWasmAtomicExchangeI64(
546 useRegister(base), useInt64Fixed(ins->value(), Register64(ecx, ebx)),
547 useRegister(memoryBase), ins->access());
548 defineInt64Fixed(lir, ins,
549 LInt64Allocation(LAllocation(AnyRegister(edx)),
550 LAllocation(AnyRegister(eax))));
551 return;
552 }
553
554 const LAllocation base = useRegister(ins->base());
555 const LAllocation value = useRegister(ins->value());
556
557 auto* lir = new (alloc())
558 LWasmAtomicExchangeHeap(base, value, useRegister(memoryBase), temp());
559
560 if (byteSize(ins->access().type()) == 1) {
561 defineFixed(lir, ins, LAllocation(AnyRegister(eax)));
562 } else {
563 define(lir, ins);
564 }
565 }
566
567 void LIRGenerator::visitWasmAtomicBinopHeap(MWasmAtomicBinopHeap* ins) {
568 MDefinition* base = ins->base();
569 MOZ_ASSERT(base->type() == MIRType::Int32);
570
571 MDefinition* memoryBase = ins->memoryBase();
572 MOZ_ASSERT(memoryBase->type() == MIRType::Pointer);
573
574 if (ins->access().type() == Scalar::Int64) {
575 auto* lir = new (alloc()) LWasmAtomicBinopI64(
576 useRegister(base), useInt64Fixed(ins->value(), Register64(ecx, ebx)),
577 useRegister(memoryBase), ins->access(), ins->operation());
578 defineInt64Fixed(lir, ins,
579 LInt64Allocation(LAllocation(AnyRegister(edx)),
580 LAllocation(AnyRegister(eax))));
581 return;
582 }
583
584 MOZ_ASSERT(ins->access().type() < Scalar::Float32);
585
586 bool byteArray = byteSize(ins->access().type()) == 1;
587
588 // Case 1: the result of the operation is not used.
589 //
590 // We'll emit a single instruction: LOCK ADD, LOCK SUB, LOCK AND,
591 // LOCK OR, or LOCK XOR. These can all take an immediate.
592
593 if (!ins->hasUses()) {
594 LAllocation value;
595 if (byteArray && !ins->value()->isConstant()) {
596 value = useFixed(ins->value(), ebx);
597 } else {
598 value = useRegisterOrConstant(ins->value());
599 }
600 auto* lir = new (alloc()) LWasmAtomicBinopHeapForEffect(
601 useRegister(base), value, useRegister(memoryBase), temp());
602 add(lir, ins);
603 return;
604 }
605
606 // Case 2: the result of the operation is used.
607 //
608 // For ADD and SUB we'll use XADD:
609 //
610 // movl value, output
611 // lock xaddl output, mem
612 //
613 // For the 8-bit variants XADD needs a byte register for the
614 // output only, we can still set up with movl; just pin the output
615 // to eax (or ebx / ecx / edx).
616 //
617 // For AND/OR/XOR we need to use a CMPXCHG loop:
618 //
619 // movl *mem, eax
620 // L: mov eax, temp
621 // andl value, temp
622 // lock cmpxchg temp, mem ; reads eax also
623 // jnz L
624 // ; result in eax
625 //
626 // Note the placement of L, cmpxchg will update eax with *mem if
627 // *mem does not have the expected value, so reloading it at the
628 // top of the loop would be redundant.
629 //
630 // We want to fix eax as the output. We also need a temp for
631 // the intermediate value.
632 //
633 // For the 8-bit variants the temp must have a byte register.
634 //
635 // There are optimization opportunities:
636 // - better 8-bit register allocation and instruction selection, Bug
637 // #1077036.
638
639 bool bitOp =
640 !(ins->operation() == AtomicOp::Add || ins->operation() == AtomicOp::Sub);
641 LDefinition tempDef = LDefinition::BogusTemp();
642 LAllocation value;
643
644 if (byteArray) {
645 value = useFixed(ins->value(), ebx);
646 if (bitOp) {
647 tempDef = tempFixed(ecx);
648 }
649 } else if (bitOp || ins->value()->isConstant()) {
650 value = useRegisterOrConstant(ins->value());
651 if (bitOp) {
652 tempDef = temp();
653 }
654 } else {
655 value = useRegisterAtStart(ins->value());
656 }
657
658 auto* lir = new (alloc()) LWasmAtomicBinopHeap(
659 useRegister(base), value, useRegister(memoryBase), tempDef, temp());
660 if (byteArray || bitOp) {
661 defineFixed(lir, ins, LAllocation(AnyRegister(eax)));
662 } else if (ins->value()->isConstant()) {
663 define(lir, ins);
664 } else {
665 defineReuseInput(lir, ins, LWasmAtomicBinopHeap::ValueIndex);
666 }
667 }
668
669 void LIRGeneratorX86::lowerDivI64(MDiv* div) {
670 MOZ_CRASH("We use MWasmBuiltinModI64 instead.");
671 }
672
673 void LIRGeneratorX86::lowerWasmBuiltinDivI64(MWasmBuiltinDivI64* div) {
674 MOZ_ASSERT(div->lhs()->type() == div->rhs()->type());
675 MOZ_ASSERT(IsNumberType(div->type()));
676
677 MOZ_ASSERT(div->type() == MIRType::Int64);
678
679 if (div->isUnsigned()) {
680 LUDivOrModI64* lir = new (alloc())
681 LUDivOrModI64(useInt64FixedAtStart(div->lhs(), Register64(eax, ebx)),
682 useInt64FixedAtStart(div->rhs(), Register64(ecx, edx)),
683 useFixedAtStart(div->instance(), InstanceReg));
684 defineReturn(lir, div);
685 return;
686 }
687
688 LDivOrModI64* lir = new (alloc())
689 LDivOrModI64(useInt64FixedAtStart(div->lhs(), Register64(eax, ebx)),
690 useInt64FixedAtStart(div->rhs(), Register64(ecx, edx)),
691 useFixedAtStart(div->instance(), InstanceReg));
692 defineReturn(lir, div);
693 }
694
695 void LIRGeneratorX86::lowerModI64(MMod* mod) {
696 MOZ_CRASH("We use MWasmBuiltinModI64 instead.");
697 }
698
699 void LIRGeneratorX86::lowerWasmBuiltinModI64(MWasmBuiltinModI64* mod) {
700 MDefinition* lhs = mod->lhs();
701 MDefinition* rhs = mod->rhs();
702 MOZ_ASSERT(lhs->type() == rhs->type());
703 MOZ_ASSERT(IsNumberType(mod->type()));
704
705 MOZ_ASSERT(mod->type() == MIRType::Int64);
706 MOZ_ASSERT(mod->type() == MIRType::Int64);
707
708 if (mod->isUnsigned()) {
709 LUDivOrModI64* lir = new (alloc())
710 LUDivOrModI64(useInt64FixedAtStart(lhs, Register64(eax, ebx)),
711 useInt64FixedAtStart(rhs, Register64(ecx, edx)),
712 useFixedAtStart(mod->instance(), InstanceReg));
713 defineReturn(lir, mod);
714 return;
715 }
716
717 LDivOrModI64* lir = new (alloc())
718 LDivOrModI64(useInt64FixedAtStart(lhs, Register64(eax, ebx)),
719 useInt64FixedAtStart(rhs, Register64(ecx, edx)),
720 useFixedAtStart(mod->instance(), InstanceReg));
721 defineReturn(lir, mod);
722 }
723
724 void LIRGeneratorX86::lowerUDivI64(MDiv* div) {
725 MOZ_CRASH("We use MWasmBuiltinDivI64 instead.");
726 }
727
728 void LIRGeneratorX86::lowerUModI64(MMod* mod) {
729 MOZ_CRASH("We use MWasmBuiltinModI64 instead.");
730 }
731
732 void LIRGeneratorX86::lowerBigIntPtrDiv(MBigIntPtrDiv* ins) {
733 auto* lir = new (alloc())
734 LBigIntPtrDiv(useRegister(ins->lhs()), useRegister(ins->rhs()),
735 tempFixed(edx), LDefinition::BogusTemp());
736 assignSnapshot(lir, ins->bailoutKind());
737 defineFixed(lir, ins, LAllocation(AnyRegister(eax)));
738 }
739
740 void LIRGeneratorX86::lowerBigIntPtrMod(MBigIntPtrMod* ins) {
741 auto* lir = new (alloc())
742 LBigIntPtrMod(useRegister(ins->lhs()), useRegister(ins->rhs()),
743 tempFixed(eax), LDefinition::BogusTemp());
744 if (ins->canBeDivideByZero()) {
745 assignSnapshot(lir, ins->bailoutKind());
746 }
747 defineFixed(lir, ins, LAllocation(AnyRegister(edx)));
748 }
749
750 void LIRGeneratorX86::lowerTruncateDToInt32(MTruncateToInt32* ins) {
751 MDefinition* opd = ins->input();
752 MOZ_ASSERT(opd->type() == MIRType::Double);
753
754 LDefinition maybeTemp =
755 Assembler::HasSSE3() ? LDefinition::BogusTemp() : tempDouble();
756 define(new (alloc()) LTruncateDToInt32(useRegister(opd), maybeTemp), ins);
757 }
758
759 void LIRGeneratorX86::lowerTruncateFToInt32(MTruncateToInt32* ins) {
760 MDefinition* opd = ins->input();
761 MOZ_ASSERT(opd->type() == MIRType::Float32);
762
763 LDefinition maybeTemp =
764 Assembler::HasSSE3() ? LDefinition::BogusTemp() : tempFloat32();
765 define(new (alloc()) LTruncateFToInt32(useRegister(opd), maybeTemp), ins);
766 }
767
768 void LIRGenerator::visitSubstr(MSubstr* ins) {
769 // Due to lack of registers on x86, we reuse the string register as
770 // temporary. As a result we only need two temporary registers and take a
771 // bogus temporary as fifth argument.
772 LSubstr* lir = new (alloc())
773 LSubstr(useRegister(ins->string()), useRegister(ins->begin()),
774 useRegister(ins->length()), temp(), LDefinition::BogusTemp(),
775 tempByteOpRegister());
776 define(lir, ins);
777 assignSafepoint(lir, ins);
778 }
779
780 void LIRGeneratorX86::lowerWasmBuiltinTruncateToInt32(
781 MWasmBuiltinTruncateToInt32* ins) {
782 MDefinition* opd = ins->input();
783 MOZ_ASSERT(opd->type() == MIRType::Double || opd->type() == MIRType::Float32);
784
785 LDefinition maybeTemp =
786 Assembler::HasSSE3() ? LDefinition::BogusTemp() : tempDouble();
787 if (opd->type() == MIRType::Double) {
788 define(new (alloc()) LWasmBuiltinTruncateDToInt32(
789 useRegister(opd), useFixed(ins->instance(), InstanceReg),
790 maybeTemp),
791 ins);
792 return;
793 }
794
795 define(
796 new (alloc()) LWasmBuiltinTruncateFToInt32(
797 useRegister(opd), useFixed(ins->instance(), InstanceReg), maybeTemp),
798 ins);
799 }
800
801 void LIRGenerator::visitWasmTruncateToInt64(MWasmTruncateToInt64* ins) {
802 MDefinition* opd = ins->input();
803 MOZ_ASSERT(opd->type() == MIRType::Double || opd->type() == MIRType::Float32);
804
805 LDefinition temp = tempDouble();
806 defineInt64(new (alloc()) LWasmTruncateToInt64(useRegister(opd), temp), ins);
807 }
808
809 void LIRGeneratorX86::lowerWasmBuiltinTruncateToInt64(
810 MWasmBuiltinTruncateToInt64* ins) {
811 MOZ_CRASH("We don't use it for this architecture");
812 }
813
814 void LIRGenerator::visitInt64ToFloatingPoint(MInt64ToFloatingPoint* ins) {
815 MDefinition* opd = ins->input();
816 MOZ_ASSERT(opd->type() == MIRType::Int64);
817 MOZ_ASSERT(IsFloatingPointType(ins->type()));
818
819 LDefinition maybeTemp = ins->isUnsigned() && ins->type() == MIRType::Float32
820 ? temp()
821 : LDefinition::BogusTemp();
822
823 define(new (alloc()) LInt64ToFloatingPoint(useInt64Register(opd), maybeTemp),
824 ins);
825 }
826
827 void LIRGeneratorX86::lowerBuiltinInt64ToFloatingPoint(
828 MBuiltinInt64ToFloatingPoint* ins) {
829 MOZ_CRASH("We don't use it for this architecture");
830 }
831
832 void LIRGenerator::visitExtendInt32ToInt64(MExtendInt32ToInt64* ins) {
833 if (ins->isUnsigned()) {
834 defineInt64(new (alloc())
835 LExtendInt32ToInt64(useRegisterAtStart(ins->input())),
836 ins);
837 } else {
838 LExtendInt32ToInt64* lir =
839 new (alloc()) LExtendInt32ToInt64(useFixedAtStart(ins->input(), eax));
840 defineInt64Fixed(lir, ins,
841 LInt64Allocation(LAllocation(AnyRegister(edx)),
842 LAllocation(AnyRegister(eax))));
843 }
844 }
845
846 void LIRGenerator::visitSignExtendInt64(MSignExtendInt64* ins) {
847 // Here we'll end up using cdq which requires input and output in (edx,eax).
848 LSignExtendInt64* lir = new (alloc()) LSignExtendInt64(
849 useInt64FixedAtStart(ins->input(), Register64(edx, eax)));
850 defineInt64Fixed(lir, ins,
851 LInt64Allocation(LAllocation(AnyRegister(edx)),
852 LAllocation(AnyRegister(eax))));
853 }
854
855 // On x86 we specialize the only cases where compare is {U,}Int32 and select
856 // is {U,}Int32.
857 bool LIRGeneratorShared::canSpecializeWasmCompareAndSelect(
858 MCompare::CompareType compTy, MIRType insTy) {
859 return insTy == MIRType::Int32 && (compTy == MCompare::Compare_Int32 ||
860 compTy == MCompare::Compare_UInt32);
861 }
862
863 void LIRGeneratorShared::lowerWasmCompareAndSelect(MWasmSelect* ins,
864 MDefinition* lhs,
865 MDefinition* rhs,
866 MCompare::CompareType compTy,
867 JSOp jsop) {
868 MOZ_ASSERT(canSpecializeWasmCompareAndSelect(compTy, ins->type()));
869 auto* lir = new (alloc()) LWasmCompareAndSelect(
870 useRegister(lhs), useAny(rhs), useRegisterAtStart(ins->trueExpr()),
871 useAny(ins->falseExpr()), compTy, jsop);
872 defineReuseInput(lir, ins, LWasmCompareAndSelect::IfTrueExprIndex);
873 }