Debian Codegraph @

js/src/jit/TypePolicy.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/TypePolicy.h"
8
9 #include "jit/JitAllocPolicy.h"
10 #include "jit/MIR-wasm.h"
11 #include "jit/MIR.h"
12 #include "jit/MIRGraph.h"
13 #include "js/ScalarType.h" // js::Scalar::Type
14 #include "util/DifferentialTesting.h"
15
16 using namespace js;
17 using namespace js::jit;
18
19 static void EnsureOperandNotFloat32(TempAllocator& alloc, MInstruction* def,
20 unsigned op) {
21 MDefinition* in = def->getOperand(op);
22 if (in->type() == MIRType::Float32) {
23 MToDouble* replace = MToDouble::New(alloc, in);
24 def->block()->insertBefore(def, replace);
25 if (def->isRecoveredOnBailout()) {
26 replace->setRecoveredOnBailout();
27 }
28 def->replaceOperand(op, replace);
29 }
30 }
31
32 template <class T>
33 [[nodiscard]] static bool ConvertOperand(TempAllocator& alloc,
34 MInstruction* def, unsigned op,
35 MIRType expected) {
36 MDefinition* in = def->getOperand(op);
37 if (in->type() == expected) {
38 return true;
39 }
40
41 auto* replace = T::New(alloc, in);
42 def->block()->insertBefore(def, replace);
43 def->replaceOperand(op, replace);
44
45 return replace->typePolicy()->adjustInputs(alloc, replace);
46 }
47
48 static void SetTypePolicyBailoutKind(MInstruction* newIns,
49 MInstruction* forIns) {
50 // Infallible ToFloat32 or ToDouble doesn't bail out.
51 if ((newIns->isToFloat32() || newIns->isToDouble()) && !newIns->isGuard()) {
52 return;
53 }
54
55 // Ensure we're not introducing TypePolicy bailouts for transpiled CacheIR
56 // instructions. Unbox operations and other guards should have been inserted
57 // by the transpiler.
58 //
59 // This avoids a performance cliff because frequent TypePolicy bailouts will
60 // disable Warp compilation instead of invalidating the script.
61 // See bug 1850305.
62 MOZ_ASSERT(forIns->bailoutKind() != BailoutKind::TranspiledCacheIR);
63
64 newIns->setBailoutKind(BailoutKind::TypePolicy);
65 }
66
67 [[nodiscard]] static bool UnboxOperand(TempAllocator& alloc, MInstruction* def,
68 unsigned op, MIRType expected) {
69 MDefinition* in = def->getOperand(op);
70 if (in->type() == expected) {
71 return true;
72 }
73
74 auto* replace = MUnbox::New(alloc, in, expected, MUnbox::Fallible);
75 SetTypePolicyBailoutKind(replace, def);
76 def->block()->insertBefore(def, replace);
77 def->replaceOperand(op, replace);
78
79 return replace->typePolicy()->adjustInputs(alloc, replace);
80 }
81
82 MDefinition* js::jit::BoxAt(TempAllocator& alloc, MInstruction* at,
83 MDefinition* operand) {
84 MDefinition* boxedOperand = operand;
85 // Replace Float32 by double
86 if (operand->type() == MIRType::Float32) {
87 MInstruction* replace = MToDouble::New(alloc, operand);
88 at->block()->insertBefore(at, replace);
89 boxedOperand = replace;
90 }
91 MBox* box = MBox::New(alloc, boxedOperand);
92 at->block()->insertBefore(at, box);
93 return box;
94 }
95
96 bool BoxInputsPolicy::staticAdjustInputs(TempAllocator& alloc,
97 MInstruction* ins) {
98 for (size_t i = 0, e = ins->numOperands(); i < e; i++) {
99 MDefinition* in = ins->getOperand(i);
100 if (in->type() == MIRType::Value) {
101 continue;
102 }
103 ins->replaceOperand(i, BoxAt(alloc, ins, in));
104 }
105 return true;
106 }
107
108 bool ArithPolicy::adjustInputs(TempAllocator& alloc, MInstruction* ins) const {
109 MOZ_ASSERT(IsNumberType(ins->type()));
110 MOZ_ASSERT(ins->type() == MIRType::Double || ins->type() == MIRType::Int32 ||
111 ins->type() == MIRType::Float32);
112
113 for (size_t i = 0, e = ins->numOperands(); i < e; i++) {
114 MDefinition* in = ins->getOperand(i);
115 if (in->type() == ins->type()) {
116 continue;
117 }
118
119 MInstruction* replace;
120
121 if (ins->type() == MIRType::Double) {
122 replace = MToDouble::New(alloc, in);
123 } else if (ins->type() == MIRType::Float32) {
124 replace = MToFloat32::New(alloc, in);
125 } else {
126 replace = MToNumberInt32::New(alloc, in);
127 }
128
129 SetTypePolicyBailoutKind(replace, ins);
130 ins->block()->insertBefore(ins, replace);
131 ins->replaceOperand(i, replace);
132
133 if (!replace->typePolicy()->adjustInputs(alloc, replace)) {
134 return false;
135 }
136 }
137
138 return true;
139 }
140
141 bool BigIntArithPolicy::adjustInputs(TempAllocator& alloc,
142 MInstruction* ins) const {
143 MOZ_ASSERT(ins->type() == MIRType::BigInt);
144
145 for (size_t i = 0, e = ins->numOperands(); i < e; i++) {
146 if (!ConvertOperand<MToBigInt>(alloc, ins, i, MIRType::BigInt)) {
147 return false;
148 }
149 }
150
151 return true;
152 }
153
154 bool AllDoublePolicy::staticAdjustInputs(TempAllocator& alloc,
155 MInstruction* ins) {
156 for (size_t i = 0, e = ins->numOperands(); i < e; i++) {
157 MDefinition* in = ins->getOperand(i);
158 if (in->type() == MIRType::Double) {
159 continue;
160 }
161
162 if (!alloc.ensureBallast()) {
163 return false;
164 }
165 MInstruction* replace = MToDouble::New(alloc, in);
166
167 ins->block()->insertBefore(ins, replace);
168 ins->replaceOperand(i, replace);
169
170 if (!replace->typePolicy()->adjustInputs(alloc, replace)) {
171 return false;
172 }
173 }
174
175 return true;
176 }
177
178 bool ComparePolicy::adjustInputs(TempAllocator& alloc,
179 MInstruction* def) const {
180 auto convertOperand = [&](size_t index, MIRType expected) {
181 MDefinition* operand = def->getOperand(index);
182 if (operand->type() == expected) {
183 return true;
184 }
185 MInstruction* replace = nullptr;
186 switch (expected) {
187 case MIRType::Double:
188 replace = MToDouble::New(alloc, operand);
189 break;
190 case MIRType::Int32:
191 replace = MToNumberInt32::New(alloc, operand);
192 break;
193 case MIRType::Float32:
194 replace = MToFloat32::New(alloc, operand);
195 break;
196 case MIRType::String:
197 replace =
198 MUnbox::New(alloc, operand, MIRType::String, MUnbox::Fallible);
199 break;
200 case MIRType::Symbol:
201 replace =
202 MUnbox::New(alloc, operand, MIRType::Symbol, MUnbox::Fallible);
203 break;
204 case MIRType::Object:
205 replace =
206 MUnbox::New(alloc, operand, MIRType::Object, MUnbox::Fallible);
207 break;
208 case MIRType::BigInt:
209 replace =
210 MUnbox::New(alloc, operand, MIRType::BigInt, MUnbox::Fallible);
211 break;
212 default:
213 MOZ_CRASH("Unsupported MIRType");
214 }
215 replace->setBailoutKind(BailoutKind::TypePolicy);
216 def->block()->insertBefore(def, replace);
217 def->replaceOperand(index, replace);
218 return replace->typePolicy()->adjustInputs(alloc, replace);
219 };
220
221 MOZ_ASSERT(def->isCompare());
222 MCompare* compare = def->toCompare();
223 switch (compare->compareType()) {
224 case MCompare::Compare_Undefined:
225 case MCompare::Compare_Null:
226 MOZ_ASSERT(compare->rhs()->type() == MIRType::Undefined ||
227 compare->rhs()->type() == MIRType::Null);
228 // IF the operand is float32, we must convert it to a double.
229 if (compare->lhs()->type() == MIRType::Float32) {
230 MInstruction* replace = MToDouble::New(alloc, compare->lhs());
231 def->block()->insertBefore(def, replace);
232 def->replaceOperand(0, replace);
233 return replace->typePolicy()->adjustInputs(alloc, replace);
234 }
235 // GVN and lowering handle all other types.
236 return true;
237 case MCompare::Compare_Int32:
238 return convertOperand(0, MIRType::Int32) &&
239 convertOperand(1, MIRType::Int32);
240 case MCompare::Compare_IntPtr:
241 case MCompare::Compare_UIntPtr:
242 MOZ_ASSERT(compare->lhs()->type() == MIRType::IntPtr);
243 MOZ_ASSERT(compare->rhs()->type() == MIRType::IntPtr);
244 return true;
245 case MCompare::Compare_Double:
246 return convertOperand(0, MIRType::Double) &&
247 convertOperand(1, MIRType::Double);
248 case MCompare::Compare_Float32:
249 return convertOperand(0, MIRType::Float32) &&
250 convertOperand(1, MIRType::Float32);
251 case MCompare::Compare_String:
252 return convertOperand(0, MIRType::String) &&
253 convertOperand(1, MIRType::String);
254 case MCompare::Compare_Symbol:
255 return convertOperand(0, MIRType::Symbol) &&
256 convertOperand(1, MIRType::Symbol);
257 case MCompare::Compare_Object:
258 return convertOperand(0, MIRType::Object) &&
259 convertOperand(1, MIRType::Object);
260 case MCompare::Compare_BigInt:
261 return convertOperand(0, MIRType::BigInt) &&
262 convertOperand(1, MIRType::BigInt);
263 case MCompare::Compare_BigInt_Int32:
264 return convertOperand(0, MIRType::BigInt) &&
265 convertOperand(1, MIRType::Int32);
266 case MCompare::Compare_BigInt_Double:
267 return convertOperand(0, MIRType::BigInt) &&
268 convertOperand(1, MIRType::Double);
269 case MCompare::Compare_BigInt_String:
270 return convertOperand(0, MIRType::BigInt) &&
271 convertOperand(1, MIRType::String);
272 case MCompare::Compare_UInt32:
273 case MCompare::Compare_Int64:
274 case MCompare::Compare_UInt64:
275 case MCompare::Compare_WasmAnyRef:
276 break;
277 }
278 MOZ_CRASH("Unexpected compare type");
279 }
280
281 bool TestPolicy::adjustInputs(TempAllocator& alloc, MInstruction* ins) const {
282 MDefinition* op = ins->getOperand(0);
283 switch (op->type()) {
284 case MIRType::Value:
285 case MIRType::Null:
286 case MIRType::Undefined:
287 case MIRType::Boolean:
288 case MIRType::Int32:
289 case MIRType::Double:
290 case MIRType::Float32:
291 case MIRType::Symbol:
292 case MIRType::BigInt:
293 case MIRType::Object:
294 break;
295
296 case MIRType::String: {
297 MStringLength* length = MStringLength::New(alloc, op);
298 ins->block()->insertBefore(ins, length);
299 ins->replaceOperand(0, length);
300 break;
301 }
302
303 case MIRType::Int64:
304 case MIRType::IntPtr:
305 MOZ_CRASH("Int64 and IntPtr are only used as input type after GVN");
306
307 default:
308 MOZ_ASSERT(IsMagicType(op->type()));
309 ins->replaceOperand(0, BoxAt(alloc, ins, op));
310 break;
311 }
312 return true;
313 }
314
315 bool BitwisePolicy::adjustInputs(TempAllocator& alloc,
316 MInstruction* ins) const {
317 MOZ_ASSERT(ins->type() == MIRType::Int32 || ins->type() == MIRType::Double);
318
319 // This policy works for both unary and binary bitwise operations.
320 for (size_t i = 0, e = ins->numOperands(); i < e; i++) {
321 if (!ConvertOperand<MTruncateToInt32>(alloc, ins, i, MIRType::Int32)) {
322 return false;
323 }
324 }
325
326 return true;
327 }
328
329 bool PowPolicy::adjustInputs(TempAllocator& alloc, MInstruction* ins) const {
330 MOZ_ASSERT(ins->type() == MIRType::Int32 || ins->type() == MIRType::Double);
331
332 if (ins->type() == MIRType::Int32) {
333 // Both operands must be int32.
334 return UnboxedInt32Policy<0>::staticAdjustInputs(alloc, ins) &&
335 UnboxedInt32Policy<1>::staticAdjustInputs(alloc, ins);
336 }
337
338 // Otherwise, input must be a double.
339 if (!DoublePolicy<0>::staticAdjustInputs(alloc, ins)) {
340 return false;
341 }
342
343 // Power may be an int32 or a double. Integers receive a faster path.
344 MDefinition* power = ins->toPow()->power();
345 if (power->isToDouble()) {
346 MDefinition* input = power->toToDouble()->input();
347 if (input->type() == MIRType::Int32) {
348 power->setImplicitlyUsedUnchecked();
349 ins->replaceOperand(1, input);
350 return true;
351 }
352 }
353 return DoublePolicy<1>::staticAdjustInputs(alloc, ins);
354 }
355
356 bool SignPolicy::adjustInputs(TempAllocator& alloc, MInstruction* ins) const {
357 MOZ_ASSERT(ins->isSign());
358 MIRType specialization = ins->typePolicySpecialization();
359
360 // MSign is specialized for int32 input types.
361 if (specialization == MIRType::Int32) {
362 return UnboxedInt32Policy<0>::staticAdjustInputs(alloc, ins);
363 }
364
365 // Otherwise convert input to double.
366 MOZ_ASSERT(IsFloatingPointType(specialization));
367 return DoublePolicy<0>::staticAdjustInputs(alloc, ins);
368 }
369
370 template <unsigned Op>
371 bool SymbolPolicy<Op>::staticAdjustInputs(TempAllocator& alloc,
372 MInstruction* ins) {
373 return UnboxOperand(alloc, ins, Op, MIRType::Symbol);
374 }
375
376 template bool SymbolPolicy<0>::staticAdjustInputs(TempAllocator& alloc,
377 MInstruction* ins);
378
379 template <unsigned Op>
380 bool BooleanPolicy<Op>::staticAdjustInputs(TempAllocator& alloc,
381 MInstruction* ins) {
382 return UnboxOperand(alloc, ins, Op, MIRType::Boolean);
383 }
384
385 template bool BooleanPolicy<0>::staticAdjustInputs(TempAllocator& alloc,
386 MInstruction* ins);
387
388 template <unsigned Op>
389 bool StringPolicy<Op>::staticAdjustInputs(TempAllocator& alloc,
390 MInstruction* ins) {
391 return UnboxOperand(alloc, ins, Op, MIRType::String);
392 }
393
394 template bool StringPolicy<0>::staticAdjustInputs(TempAllocator& alloc,
395 MInstruction* ins);
396 template bool StringPolicy<1>::staticAdjustInputs(TempAllocator& alloc,
397 MInstruction* ins);
398 template bool StringPolicy<2>::staticAdjustInputs(TempAllocator& alloc,
399 MInstruction* ins);
400
401 template <unsigned Op>
402 bool ConvertToStringPolicy<Op>::staticAdjustInputs(TempAllocator& alloc,
403 MInstruction* ins) {
404 MDefinition* in = ins->getOperand(Op);
405 if (in->type() == MIRType::String) {
406 return true;
407 }
408
409 MToString* replace =
410 MToString::New(alloc, in, MToString::SideEffectHandling::Bailout);
411 ins->block()->insertBefore(ins, replace);
412 ins->replaceOperand(Op, replace);
413
414 return ToStringPolicy::staticAdjustInputs(alloc, replace);
415 }
416
417 template bool ConvertToStringPolicy<0>::staticAdjustInputs(TempAllocator& alloc,
418 MInstruction* ins);
419 template bool ConvertToStringPolicy<1>::staticAdjustInputs(TempAllocator& alloc,
420 MInstruction* ins);
421 template bool ConvertToStringPolicy<2>::staticAdjustInputs(TempAllocator& alloc,
422 MInstruction* ins);
423
424 template <unsigned Op>
425 bool BigIntPolicy<Op>::staticAdjustInputs(TempAllocator& alloc,
426 MInstruction* ins) {
427 return UnboxOperand(alloc, ins, Op, MIRType::BigInt);
428 }
429
430 template bool BigIntPolicy<0>::staticAdjustInputs(TempAllocator& alloc,
431 MInstruction* ins);
432 template bool BigIntPolicy<1>::staticAdjustInputs(TempAllocator& alloc,
433 MInstruction* ins);
434
435 template <unsigned Op>
436 bool UnboxedInt32Policy<Op>::staticAdjustInputs(TempAllocator& alloc,
437 MInstruction* def) {
438 return UnboxOperand(alloc, def, Op, MIRType::Int32);
439 }
440
441 template bool UnboxedInt32Policy<0>::staticAdjustInputs(TempAllocator& alloc,
442 MInstruction* def);
443 template bool UnboxedInt32Policy<1>::staticAdjustInputs(TempAllocator& alloc,
444 MInstruction* def);
445 template bool UnboxedInt32Policy<2>::staticAdjustInputs(TempAllocator& alloc,
446 MInstruction* def);
447 template bool UnboxedInt32Policy<3>::staticAdjustInputs(TempAllocator& alloc,
448 MInstruction* def);
449
450 template <unsigned Op>
451 bool Int32OrIntPtrPolicy<Op>::staticAdjustInputs(TempAllocator& alloc,
452 MInstruction* def) {
453 MDefinition* in = def->getOperand(Op);
454 if (in->type() == MIRType::IntPtr) {
455 return true;
456 }
457
458 return UnboxedInt32Policy<Op>::staticAdjustInputs(alloc, def);
459 }
460
461 template bool Int32OrIntPtrPolicy<0>::staticAdjustInputs(TempAllocator& alloc,
462 MInstruction* def);
463 template bool Int32OrIntPtrPolicy<1>::staticAdjustInputs(TempAllocator& alloc,
464 MInstruction* def);
465
466 template <unsigned Op>
467 bool IntPtrPolicy<Op>::staticAdjustInputs(TempAllocator& alloc,
468 MInstruction* def) {
469 // We don't (yet) support converting other types to IntPtr.
470 MOZ_ASSERT(def->getOperand(Op)->type() == MIRType::IntPtr);
471 return true;
472 }
473
474 template bool IntPtrPolicy<0>::staticAdjustInputs(TempAllocator& alloc,
475 MInstruction* def);
476
477 template <unsigned Op>
478 bool ConvertToInt32Policy<Op>::staticAdjustInputs(TempAllocator& alloc,
479 MInstruction* def) {
480 return ConvertOperand<MToNumberInt32>(alloc, def, Op, MIRType::Int32);
481 }
482
483 template bool ConvertToInt32Policy<0>::staticAdjustInputs(TempAllocator& alloc,
484 MInstruction* def);
485
486 template <unsigned Op>
487 bool TruncateToInt32OrToInt64Policy<Op>::staticAdjustInputs(
488 TempAllocator& alloc, MInstruction* def) {
489 MOZ_ASSERT(def->isCompareExchangeTypedArrayElement() ||
490 def->isAtomicExchangeTypedArrayElement() ||
491 def->isAtomicTypedArrayElementBinop());
492
493 Scalar::Type type;
494 if (def->isCompareExchangeTypedArrayElement()) {
495 type = def->toCompareExchangeTypedArrayElement()->arrayType();
496 } else if (def->isAtomicExchangeTypedArrayElement()) {
497 type = def->toAtomicExchangeTypedArrayElement()->arrayType();
498 } else {
499 type = def->toAtomicTypedArrayElementBinop()->arrayType();
500 }
501
502 if (Scalar::isBigIntType(type)) {
503 return ConvertOperand<MToInt64>(alloc, def, Op, MIRType::Int64);
504 }
505 return ConvertOperand<MTruncateToInt32>(alloc, def, Op, MIRType::Int32);
506 }
507
508 template bool TruncateToInt32OrToInt64Policy<2>::staticAdjustInputs(
509 TempAllocator& alloc, MInstruction* def);
510 template bool TruncateToInt32OrToInt64Policy<3>::staticAdjustInputs(
511 TempAllocator& alloc, MInstruction* def);
512
513 template <unsigned Op>
514 bool DoublePolicy<Op>::staticAdjustInputs(TempAllocator& alloc,
515 MInstruction* def) {
516 return ConvertOperand<MToDouble>(alloc, def, Op, MIRType::Double);
517 }
518
519 template bool DoublePolicy<0>::staticAdjustInputs(TempAllocator& alloc,
520 MInstruction* def);
521 template bool DoublePolicy<1>::staticAdjustInputs(TempAllocator& alloc,
522 MInstruction* def);
523
524 template <unsigned Op>
525 bool Float32Policy<Op>::staticAdjustInputs(TempAllocator& alloc,
526 MInstruction* def) {
527 return ConvertOperand<MToFloat32>(alloc, def, Op, MIRType::Float32);
528 }
529
530 template bool Float32Policy<0>::staticAdjustInputs(TempAllocator& alloc,
531 MInstruction* def);
532 template bool Float32Policy<1>::staticAdjustInputs(TempAllocator& alloc,
533 MInstruction* def);
534 template bool Float32Policy<2>::staticAdjustInputs(TempAllocator& alloc,
535 MInstruction* def);
536
537 template <unsigned Op>
538 bool FloatingPointPolicy<Op>::adjustInputs(TempAllocator& alloc,
539 MInstruction* def) const {
540 MIRType policyType = def->typePolicySpecialization();
541 if (policyType == MIRType::Double) {
542 return DoublePolicy<Op>::staticAdjustInputs(alloc, def);
543 }
544 return Float32Policy<Op>::staticAdjustInputs(alloc, def);
545 }
546
547 template bool FloatingPointPolicy<0>::adjustInputs(TempAllocator& alloc,
548 MInstruction* def) const;
549
550 template <unsigned Op>
551 bool NoFloatPolicy<Op>::staticAdjustInputs(TempAllocator& alloc,
552 MInstruction* def) {
553 EnsureOperandNotFloat32(alloc, def, Op);
554 return true;
555 }
556
557 template bool NoFloatPolicy<0>::staticAdjustInputs(TempAllocator& alloc,
558 MInstruction* def);
559 template bool NoFloatPolicy<1>::staticAdjustInputs(TempAllocator& alloc,
560 MInstruction* def);
561 template bool NoFloatPolicy<2>::staticAdjustInputs(TempAllocator& alloc,
562 MInstruction* def);
563 template bool NoFloatPolicy<3>::staticAdjustInputs(TempAllocator& alloc,
564 MInstruction* def);
565
566 template <unsigned FirstOp>
567 bool NoFloatPolicyAfter<FirstOp>::staticAdjustInputs(TempAllocator& alloc,
568 MInstruction* def) {
569 for (size_t op = FirstOp, e = def->numOperands(); op < e; op++) {
570 EnsureOperandNotFloat32(alloc, def, op);
571 }
572 return true;
573 }
574
575 template bool NoFloatPolicyAfter<0>::staticAdjustInputs(TempAllocator& alloc,
576 MInstruction* def);
577 template bool NoFloatPolicyAfter<1>::staticAdjustInputs(TempAllocator& alloc,
578 MInstruction* def);
579 template bool NoFloatPolicyAfter<2>::staticAdjustInputs(TempAllocator& alloc,
580 MInstruction* def);
581
582 template <unsigned Op>
583 bool BoxPolicy<Op>::staticAdjustInputs(TempAllocator& alloc,
584 MInstruction* ins) {
585 MDefinition* in = ins->getOperand(Op);
586 if (in->type() == MIRType::Value) {
587 return true;
588 }
589
590 ins->replaceOperand(Op, BoxAt(alloc, ins, in));
591 return true;
592 }
593
594 template bool BoxPolicy<0>::staticAdjustInputs(TempAllocator& alloc,
595 MInstruction* ins);
596 template bool BoxPolicy<1>::staticAdjustInputs(TempAllocator& alloc,
597 MInstruction* ins);
598 template bool BoxPolicy<2>::staticAdjustInputs(TempAllocator& alloc,
599 MInstruction* ins);
600
601 template <unsigned Op, MIRType Type>
602 bool BoxExceptPolicy<Op, Type>::staticAdjustInputs(TempAllocator& alloc,
603 MInstruction* ins) {
604 MDefinition* in = ins->getOperand(Op);
605 if (in->type() == Type) {
606 return true;
607 }
608 return BoxPolicy<Op>::staticAdjustInputs(alloc, ins);
609 }
610
611 template bool BoxExceptPolicy<0, MIRType::Object>::staticAdjustInputs(
612 TempAllocator& alloc, MInstruction* ins);
613
614 template <unsigned Op>
615 bool CacheIdPolicy<Op>::staticAdjustInputs(TempAllocator& alloc,
616 MInstruction* ins) {
617 MDefinition* in = ins->getOperand(Op);
618 switch (in->type()) {
619 case MIRType::Int32:
620 case MIRType::String:
621 case MIRType::Symbol:
622 return true;
623 default:
624 return BoxPolicy<Op>::staticAdjustInputs(alloc, ins);
625 }
626 }
627
628 template bool CacheIdPolicy<0>::staticAdjustInputs(TempAllocator& alloc,
629 MInstruction* ins);
630 template bool CacheIdPolicy<1>::staticAdjustInputs(TempAllocator& alloc,
631 MInstruction* ins);
632
633 bool ToDoublePolicy::staticAdjustInputs(TempAllocator& alloc,
634 MInstruction* ins) {
635 MOZ_ASSERT(ins->isToDouble() || ins->isToFloat32() || ins->isToFloat16());
636
637 MDefinition* in = ins->getOperand(0);
638 switch (in->type()) {
639 case MIRType::Int32:
640 case MIRType::Float32:
641 case MIRType::Double:
642 case MIRType::Value:
643 case MIRType::Null:
644 case MIRType::Undefined:
645 case MIRType::Boolean:
646 // No need for boxing for these types.
647 return true;
648 case MIRType::Object:
649 case MIRType::String:
650 case MIRType::Symbol:
651 case MIRType::BigInt:
652 // Objects might be effectful. Symbols and BigInts give TypeError.
653 break;
654 default:
655 break;
656 }
657
658 in = BoxAt(alloc, ins, in);
659 ins->replaceOperand(0, in);
660 return true;
661 }
662
663 bool ToInt32Policy::staticAdjustInputs(TempAllocator& alloc,
664 MInstruction* ins) {
665 MOZ_ASSERT(ins->isToNumberInt32() || ins->isTruncateToInt32());
666
667 IntConversionInputKind conversion = IntConversionInputKind::Any;
668 if (ins->isToNumberInt32()) {
669 conversion = ins->toToNumberInt32()->conversion();
670 }
671
672 MDefinition* in = ins->getOperand(0);
673 switch (in->type()) {
674 case MIRType::Int32:
675 case MIRType::Float32:
676 case MIRType::Double:
677 case MIRType::Value:
678 // No need for boxing for these types.
679 return true;
680 case MIRType::Undefined:
681 // No need for boxing when truncating.
682 if (ins->isTruncateToInt32()) {
683 return true;
684 }
685 break;
686 case MIRType::Null:
687 // No need for boxing, when we will convert.
688 if (conversion == IntConversionInputKind::Any) {
689 return true;
690 }
691 break;
692 case MIRType::Boolean:
693 // No need for boxing, when we will convert.
694 if (conversion == IntConversionInputKind::Any) {
695 return true;
696 }
697 break;
698 case MIRType::Object:
699 case MIRType::String:
700 case MIRType::Symbol:
701 case MIRType::BigInt:
702 // Objects might be effectful. Symbols and BigInts give TypeError.
703 break;
704 default:
705 break;
706 }
707
708 in = BoxAt(alloc, ins, in);
709 ins->replaceOperand(0, in);
710 return true;
711 }
712
713 bool ToBigIntPolicy::staticAdjustInputs(TempAllocator& alloc,
714 MInstruction* ins) {
715 MOZ_ASSERT(ins->isToBigInt());
716
717 MDefinition* in = ins->getOperand(0);
718 switch (in->type()) {
719 case MIRType::BigInt:
720 case MIRType::Value:
721 // No need for boxing for these types.
722 return true;
723 default:
724 // Any other types need to be boxed.
725 break;
726 }
727
728 in = BoxAt(alloc, ins, in);
729 ins->replaceOperand(0, in);
730 return true;
731 }
732
733 bool ToStringPolicy::staticAdjustInputs(TempAllocator& alloc,
734 MInstruction* ins) {
735 MOZ_ASSERT(ins->isToString());
736
737 MIRType type = ins->getOperand(0)->type();
738 if (type == MIRType::Object || type == MIRType::Symbol ||
739 type == MIRType::BigInt) {
740 ins->replaceOperand(0, BoxAt(alloc, ins, ins->getOperand(0)));
741 return true;
742 }
743
744 // TODO remove the following line once 966957 has landed
745 EnsureOperandNotFloat32(alloc, ins, 0);
746
747 return true;
748 }
749
750 bool ToInt64Policy::staticAdjustInputs(TempAllocator& alloc,
751 MInstruction* ins) {
752 MOZ_ASSERT(ins->isToInt64());
753
754 MDefinition* input = ins->getOperand(0);
755 MIRType type = input->type();
756
757 switch (type) {
758 case MIRType::BigInt: {
759 auto* replace = MTruncateBigIntToInt64::New(alloc, input);
760 ins->block()->insertBefore(ins, replace);
761 ins->replaceOperand(0, replace);
762 break;
763 }
764 // No need for boxing for these types, because they are handled specially
765 // when this instruction is lowered to LIR.
766 case MIRType::Boolean:
767 case MIRType::String:
768 case MIRType::Int64:
769 case MIRType::Value:
770 break;
771 default:
772 ins->replaceOperand(0, BoxAt(alloc, ins, ins->getOperand(0)));
773 break;
774 }
775
776 return true;
777 }
778
779 template <unsigned Op>
780 bool ObjectPolicy<Op>::staticAdjustInputs(TempAllocator& alloc,
781 MInstruction* ins) {
782 MOZ_ASSERT(ins->getOperand(Op)->type() != MIRType::Slots);
783 MOZ_ASSERT(ins->getOperand(Op)->type() != MIRType::Elements);
784
785 return UnboxOperand(alloc, ins, Op, MIRType::Object);
786 }
787
788 template bool ObjectPolicy<0>::staticAdjustInputs(TempAllocator& alloc,
789 MInstruction* ins);
790 template bool ObjectPolicy<1>::staticAdjustInputs(TempAllocator& alloc,
791 MInstruction* ins);
792 template bool ObjectPolicy<2>::staticAdjustInputs(TempAllocator& alloc,
793 MInstruction* ins);
794 template bool ObjectPolicy<3>::staticAdjustInputs(TempAllocator& alloc,
795 MInstruction* ins);
796
797 bool CallPolicy::adjustInputs(TempAllocator& alloc, MInstruction* ins) const {
798 MCallBase* call;
799 if (ins->isCall()) {
800 call = ins->toCall();
801 } else {
802 call = ins->toCallClassHook();
803 }
804
805 MDefinition* func = call->getCallee();
806 if (func->type() != MIRType::Object) {
807 MInstruction* unbox =
808 MUnbox::New(alloc, func, MIRType::Object, MUnbox::Fallible);
809 SetTypePolicyBailoutKind(unbox, call);
810 call->block()->insertBefore(call, unbox);
811 call->replaceCallee(unbox);
812
813 if (!unbox->typePolicy()->adjustInputs(alloc, unbox)) {
814 return false;
815 }
816 }
817
818 for (uint32_t i = 0; i < call->numStackArgs(); i++) {
819 if (!alloc.ensureBallast()) {
820 return false;
821 }
822 EnsureOperandNotFloat32(alloc, call, MCallBase::IndexOfStackArg(i));
823 }
824
825 return true;
826 }
827
828 bool MegamorphicSetElementPolicy::adjustInputs(TempAllocator& alloc,
829 MInstruction* ins) const {
830 // The first operand should be an object.
831 if (!SingleObjectPolicy::staticAdjustInputs(alloc, ins)) {
832 return false;
833 }
834
835 // Box the index and value operands.
836 for (size_t i = 1, e = ins->numOperands(); i < e; i++) {
837 MDefinition* in = ins->getOperand(i);
838 if (in->type() == MIRType::Value) {
839 continue;
840 }
841 ins->replaceOperand(i, BoxAt(alloc, ins, in));
842 }
843 return true;
844 }
845
846 bool StoreUnboxedScalarPolicy::adjustValueInput(TempAllocator& alloc,
847 MInstruction* ins,
848 Scalar::Type writeType,
849 MDefinition* value,
850 int valueOperand) {
851 if (Scalar::isBigIntType(writeType)) {
852 return ConvertOperand<MToInt64>(alloc, ins, valueOperand, MIRType::Int64);
853 }
854
855 MDefinition* curValue = value;
856 // First, ensure the value is int32, boolean, double or Value.
857 // The conversion is based on TypedArrayObjectTemplate::setElementTail.
858 switch (value->type()) {
859 case MIRType::Int32:
860 case MIRType::Double:
861 case MIRType::Float32:
862 case MIRType::Boolean:
863 case MIRType::Value:
864 break;
865 case MIRType::Null:
866 value->setImplicitlyUsedUnchecked();
867 value = MConstant::New(alloc, Int32Value(0));
868 ins->block()->insertBefore(ins, value->toInstruction());
869 break;
870 case MIRType::Undefined:
871 value->setImplicitlyUsedUnchecked();
872 value = MConstant::New(alloc, JS::NaNValue());
873 ins->block()->insertBefore(ins, value->toInstruction());
874 break;
875 case MIRType::Object:
876 case MIRType::String:
877 case MIRType::Symbol:
878 case MIRType::BigInt:
879 value = BoxAt(alloc, ins, value);
880 break;
881 default:
882 MOZ_CRASH("Unexpected type");
883 }
884
885 if (value != curValue) {
886 ins->replaceOperand(valueOperand, value);
887 curValue = value;
888 }
889
890 MOZ_ASSERT(
891 value->type() == MIRType::Int32 || value->type() == MIRType::Boolean ||
892 value->type() == MIRType::Double || value->type() == MIRType::Float32 ||
893 value->type() == MIRType::Value);
894
895 switch (writeType) {
896 case Scalar::Int8:
897 case Scalar::Uint8:
898 case Scalar::Int16:
899 case Scalar::Uint16:
900 case Scalar::Int32:
901 case Scalar::Uint32:
902 if (value->type() != MIRType::Int32) {
903 value = MTruncateToInt32::New(alloc, value);
904 ins->block()->insertBefore(ins, value->toInstruction());
905 }
906 break;
907 case Scalar::Uint8Clamped:
908 // The transpiler should have inserted MClampToUint8.
909 MOZ_ASSERT(value->type() == MIRType::Int32);
910 break;
911 case Scalar::Float16:
912 value = MToFloat16::New(alloc, value);
913 ins->block()->insertBefore(ins, value->toInstruction());
914 break;
915 case Scalar::Float32:
916 if (value->type() != MIRType::Float32) {
917 value = MToFloat32::New(alloc, value);
918 ins->block()->insertBefore(ins, value->toInstruction());
919 }
920 break;
921 case Scalar::Float64:
922 if (value->type() != MIRType::Double) {
923 value = MToDouble::New(alloc, value);
924 ins->block()->insertBefore(ins, value->toInstruction());
925 }
926 break;
927 default:
928 MOZ_CRASH("Invalid array type");
929 }
930
931 // Canonicalize floating point values for differential testing.
932 if (Scalar::isFloatingType(writeType) && js::SupportDifferentialTesting()) {
933 value = MCanonicalizeNaN::New(alloc, value);
934 ins->block()->insertBefore(ins, value->toInstruction());
935 }
936
937 if (value != curValue) {
938 ins->replaceOperand(valueOperand, value);
939 }
940
941 return true;
942 }
943
944 bool StoreUnboxedScalarPolicy::adjustInputs(TempAllocator& alloc,
945 MInstruction* ins) const {
946 MStoreUnboxedScalar* store = ins->toStoreUnboxedScalar();
947 MOZ_ASSERT(store->elements()->type() == MIRType::Elements);
948 MOZ_ASSERT(store->index()->type() == MIRType::IntPtr);
949
950 return adjustValueInput(alloc, store, store->writeType(), store->value(), 2);
951 }
952
953 bool StoreDataViewElementPolicy::adjustInputs(TempAllocator& alloc,
954 MInstruction* ins) const {
955 auto* store = ins->toStoreDataViewElement();
956 MOZ_ASSERT(store->elements()->type() == MIRType::Elements);
957 MOZ_ASSERT(store->index()->type() == MIRType::IntPtr);
958 MOZ_ASSERT(store->littleEndian()->type() == MIRType::Boolean);
959
960 return StoreUnboxedScalarPolicy::adjustValueInput(
961 alloc, ins, store->writeType(), store->value(), 2);
962 }
963
964 bool StoreTypedArrayHolePolicy::adjustInputs(TempAllocator& alloc,
965 MInstruction* ins) const {
966 MStoreTypedArrayElementHole* store = ins->toStoreTypedArrayElementHole();
967 MOZ_ASSERT(store->elements()->type() == MIRType::Elements);
968 MOZ_ASSERT(store->index()->type() == MIRType::IntPtr);
969 MOZ_ASSERT(store->length()->type() == MIRType::IntPtr);
970
971 return StoreUnboxedScalarPolicy::adjustValueInput(
972 alloc, ins, store->arrayType(), store->value(), 3);
973 }
974
975 bool ClampPolicy::adjustInputs(TempAllocator& alloc, MInstruction* ins) const {
976 MDefinition* in = ins->toClampToUint8()->input();
977
978 switch (in->type()) {
979 case MIRType::Int32:
980 case MIRType::Double:
981 case MIRType::Value:
982 break;
983 default:
984 ins->replaceOperand(0, BoxAt(alloc, ins, in));
985 break;
986 }
987
988 return true;
989 }
990
991 // Lists of all TypePolicy specializations which are used by MIR Instructions.
992 #define TYPE_POLICY_LIST(_) \
993 _(AllDoublePolicy) \
994 _(ArithPolicy) \
995 _(BigIntArithPolicy) \
996 _(BitwisePolicy) \
997 _(BoxInputsPolicy) \
998 _(CallPolicy) \
999 _(MegamorphicSetElementPolicy) \
1000 _(ClampPolicy) \
1001 _(ComparePolicy) \
1002 _(PowPolicy) \
1003 _(SignPolicy) \
1004 _(StoreDataViewElementPolicy) \
1005 _(StoreTypedArrayHolePolicy) \
1006 _(StoreUnboxedScalarPolicy) \
1007 _(TestPolicy) \
1008 _(ToDoublePolicy) \
1009 _(ToInt32Policy) \
1010 _(ToBigIntPolicy) \
1011 _(ToStringPolicy) \
1012 _(ToInt64Policy)
1013
1014 #define TEMPLATE_TYPE_POLICY_LIST(_) \
1015 _(BigIntPolicy<0>) \
1016 _(BooleanPolicy<0>) \
1017 _(BoxExceptPolicy<0, MIRType::Object>) \
1018 _(BoxPolicy<0>) \
1019 _(ConvertToInt32Policy<0>) \
1020 _(ConvertToStringPolicy<0>) \
1021 _(ConvertToStringPolicy<2>) \
1022 _(DoublePolicy<0>) \
1023 _(FloatingPointPolicy<0>) \
1024 _(UnboxedInt32Policy<0>) \
1025 _(UnboxedInt32Policy<1>) \
1026 _(IntPtrPolicy<0>) \
1027 _(TruncateToInt32OrToInt64Policy<2>) \
1028 _(MixPolicy<ObjectPolicy<0>, StringPolicy<1>, BoxPolicy<2>>) \
1029 _(MixPolicy<ObjectPolicy<0>, BoxPolicy<1>, BoxPolicy<2>>) \
1030 IF_EXPLICIT_RESOURCE_MANAGEMENT( \
1031 _(MixPolicy<ObjectPolicy<0>, BoxPolicy<1>, BoxPolicy<2>, \
1032 BooleanPolicy<3>>)) \
1033 _(MixPolicy<ObjectPolicy<0>, BoxPolicy<1>, ObjectPolicy<2>>) \
1034 _(MixPolicy<ObjectPolicy<0>, BoxPolicy<1>, UnboxedInt32Policy<2>>) \
1035 _(MixPolicy<ObjectPolicy<0>, UnboxedInt32Policy<1>, BoxPolicy<2>>) \
1036 _(MixPolicy<ObjectPolicy<0>, UnboxedInt32Policy<1>, UnboxedInt32Policy<2>>) \
1037 _(MixPolicy<ObjectPolicy<0>, BoxPolicy<2>>) \
1038 _(MixPolicy<ObjectPolicy<0>, ObjectPolicy<1>, UnboxedInt32Policy<2>>) \
1039 _(MixPolicy<ObjectPolicy<0>, ObjectPolicy<1>, ObjectPolicy<2>>) \
1040 _(MixPolicy<ObjectPolicy<0>, ObjectPolicy<1>, BoxPolicy<2>>) \
1041 _(MixPolicy<StringPolicy<0>, UnboxedInt32Policy<1>, UnboxedInt32Policy<2>>) \
1042 _(MixPolicy<StringPolicy<0>, ObjectPolicy<1>, StringPolicy<2>>) \
1043 _(MixPolicy<StringPolicy<0>, StringPolicy<1>, StringPolicy<2>>) \
1044 _(MixPolicy<ObjectPolicy<0>, StringPolicy<1>, UnboxedInt32Policy<2>>) \
1045 _(MixPolicy<ObjectPolicy<0>, UnboxedInt32Policy<1>, BoxPolicy<2>, \
1046 ObjectPolicy<3>>) \
1047 _(MixPolicy<ObjectPolicy<0>, UnboxedInt32Policy<1>, UnboxedInt32Policy<2>, \
1048 UnboxedInt32Policy<3>>) \
1049 _(MixPolicy<TruncateToInt32OrToInt64Policy<2>, \
1050 TruncateToInt32OrToInt64Policy<3>>) \
1051 _(MixPolicy<ObjectPolicy<0>, CacheIdPolicy<1>, NoFloatPolicy<2>>) \
1052 _(MixPolicy<ObjectPolicy<0>, BoxExceptPolicy<1, MIRType::Object>, \
1053 CacheIdPolicy<2>>) \
1054 _(MixPolicy<BoxPolicy<0>, ObjectPolicy<1>>) \
1055 _(MixPolicy<ConvertToStringPolicy<0>, ConvertToStringPolicy<1>>) \
1056 _(MixPolicy<ConvertToStringPolicy<0>, ObjectPolicy<1>>) \
1057 _(MixPolicy<DoublePolicy<0>, DoublePolicy<1>>) \
1058 _(MixPolicy<UnboxedInt32Policy<0>, UnboxedInt32Policy<1>>) \
1059 _(MixPolicy<Int32OrIntPtrPolicy<0>, Int32OrIntPtrPolicy<1>>) \
1060 _(MixPolicy<ObjectPolicy<0>, BoxPolicy<1>>) \
1061 _(MixPolicy<BoxExceptPolicy<0, MIRType::Object>, CacheIdPolicy<1>>) \
1062 _(MixPolicy<CacheIdPolicy<0>, ObjectPolicy<1>>) \
1063 _(MixPolicy<ObjectPolicy<0>, ConvertToStringPolicy<1>>) \
1064 _(MixPolicy<ObjectPolicy<0>, UnboxedInt32Policy<1>>) \
1065 _(MixPolicy<ObjectPolicy<0>, UnboxedInt32Policy<2>>) \
1066 _(MixPolicy<ObjectPolicy<0>, NoFloatPolicy<1>>) \
1067 _(MixPolicy<ObjectPolicy<0>, NoFloatPolicy<2>>) \
1068 _(MixPolicy<ObjectPolicy<0>, NoFloatPolicy<3>>) \
1069 _(MixPolicy<ObjectPolicy<0>, NoFloatPolicyAfter<1>>) \
1070 _(MixPolicy<ObjectPolicy<0>, ObjectPolicy<1>>) \
1071 _(MixPolicy<ObjectPolicy<0>, StringPolicy<1>>) \
1072 _(MixPolicy<ObjectPolicy<0>, ConvertToStringPolicy<2>>) \
1073 _(MixPolicy<ObjectPolicy<1>, ConvertToStringPolicy<0>>) \
1074 _(MixPolicy<StringPolicy<0>, UnboxedInt32Policy<1>>) \
1075 _(MixPolicy<StringPolicy<0>, StringPolicy<1>>) \
1076 _(MixPolicy<BoxPolicy<0>, BoxPolicy<1>>) \
1077 _(MixPolicy<BoxPolicy<0>, BoxPolicy<1>, BoxPolicy<2>>) \
1078 _(MixPolicy<ObjectPolicy<0>, BoxPolicy<2>, ObjectPolicy<3>>) \
1079 _(MixPolicy<BoxExceptPolicy<0, MIRType::Object>, ObjectPolicy<1>>) \
1080 _(MixPolicy<UnboxedInt32Policy<0>, BigIntPolicy<1>>) \
1081 _(MixPolicy<UnboxedInt32Policy<0>, NoFloatPolicyAfter<1>>) \
1082 _(MixPolicy<UnboxedInt32Policy<0>, UnboxedInt32Policy<1>, \
1083 NoFloatPolicyAfter<2>>) \
1084 _(NoFloatPolicy<0>) \
1085 _(NoFloatPolicy<1>) \
1086 _(NoFloatPolicy<2>) \
1087 _(NoFloatPolicyAfter<0>) \
1088 _(NoFloatPolicyAfter<1>) \
1089 _(NoFloatPolicyAfter<2>) \
1090 _(ObjectPolicy<0>) \
1091 _(ObjectPolicy<1>) \
1092 _(ObjectPolicy<3>) \
1093 _(StringPolicy<0>) \
1094 _(SymbolPolicy<0>)
1095
1096 namespace js {
1097 namespace jit {
1098
1099 // Define for all used TypePolicy specialization, the definition for
1100 // |TypePolicy::Data::thisTypePolicy|. This function returns one constant
1101 // instance of the TypePolicy which is shared among all MIR Instructions of the
1102 // same type.
1103 //
1104 // This Macro use __VA_ARGS__ to account for commas of template parameters.
1105 #define DEFINE_TYPE_POLICY_SINGLETON_INSTANCES_(...) \
1106 const TypePolicy* __VA_ARGS__::Data::thisTypePolicy() { \
1107 static constexpr __VA_ARGS__ singletonType; \
1108 return &singletonType; \
1109 }
1110
1111 TYPE_POLICY_LIST(DEFINE_TYPE_POLICY_SINGLETON_INSTANCES_)
1112 TEMPLATE_TYPE_POLICY_LIST(template <> DEFINE_TYPE_POLICY_SINGLETON_INSTANCES_)
1113 #undef DEFINE_TYPE_POLICY_SINGLETON_INSTANCES_
1114
1115 } // namespace jit
1116 } // namespace js
1117
1118 namespace {
1119
1120 // For extra-good measure in case an unqualified use is ever introduced. (The
1121 // main use in the macro below is explicitly qualified so as not to consult
1122 // this scope and find this function.)
1123 inline TypePolicy* thisTypePolicy() = delete;
1124
1125 static MIRType thisTypeSpecialization() {
1126 MOZ_CRASH("TypeSpecialization lacks definition of thisTypeSpecialization.");
1127 }
1128
1129 } // namespace
1130
1131 // For each MIR Instruction, this macro define the |typePolicy| method which is
1132 // using the |thisTypePolicy| method. The |thisTypePolicy| method is either a
1133 // member of the MIR Instruction, such as with MGetElementCache, a member
1134 // inherited from the TypePolicy::Data structure, or a member inherited from
1135 // NoTypePolicy if the MIR instruction has no type policy.
1136 #define DEFINE_MIR_TYPEPOLICY_MEMBERS_(op) \
1137 const TypePolicy* js::jit::M##op::typePolicy() { \
1138 return M##op::thisTypePolicy(); \
1139 } \
1140 \
1141 MIRType js::jit::M##op::typePolicySpecialization() { \
1142 return thisTypeSpecialization(); \
1143 }
1144
1145 MIR_OPCODE_LIST(DEFINE_MIR_TYPEPOLICY_MEMBERS_)
1146 #undef DEFINE_MIR_TYPEPOLICY_MEMBERS_