/
Lowering.cpp
executable file
·4302 lines (3676 loc) · 133 KB
/
Lowering.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sts=4 et sw=4 tw=99:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "jit/Lowering.h"
#include "mozilla/DebugOnly.h"
#include "jit/JitSpewer.h"
#include "jit/LIR.h"
#include "jit/MIR.h"
#include "jit/MIRGraph.h"
#include "jsobjinlines.h"
#include "jsopcodeinlines.h"
#include "jit/shared/Lowering-shared-inl.h"
using namespace js;
using namespace jit;
using mozilla::DebugOnly;
using JS::GenericNaN;
void
LIRGenerator::visitCloneLiteral(MCloneLiteral* ins)
{
MOZ_ASSERT(ins->type() == MIRType_Object);
MOZ_ASSERT(ins->input()->type() == MIRType_Object);
LCloneLiteral* lir = new(alloc()) LCloneLiteral(useRegisterAtStart(ins->input()));
defineReturn(lir, ins);
assignSafepoint(lir, ins);
}
void
LIRGenerator::visitParameter(MParameter* param)
{
ptrdiff_t offset;
if (param->index() == MParameter::THIS_SLOT)
offset = THIS_FRAME_ARGSLOT;
else
offset = 1 + param->index();
LParameter* ins = new(alloc()) LParameter;
defineBox(ins, param, LDefinition::FIXED);
offset *= sizeof(Value);
#if defined(JS_NUNBOX32)
# if defined(IS_BIG_ENDIAN)
ins->getDef(0)->setOutput(LArgument(offset));
ins->getDef(1)->setOutput(LArgument(offset + 4));
# else
ins->getDef(0)->setOutput(LArgument(offset + 4));
ins->getDef(1)->setOutput(LArgument(offset));
# endif
#elif defined(JS_PUNBOX64)
ins->getDef(0)->setOutput(LArgument(offset));
#endif
}
void
LIRGenerator::visitCallee(MCallee* ins)
{
define(new(alloc()) LCallee(), ins);
}
void
LIRGenerator::visitIsConstructing(MIsConstructing* ins)
{
define(new(alloc()) LIsConstructing(), ins);
}
void
LIRGenerator::visitGoto(MGoto* ins)
{
add(new(alloc()) LGoto(ins->target()));
}
void
LIRGenerator::visitTableSwitch(MTableSwitch* tableswitch)
{
MDefinition* opd = tableswitch->getOperand(0);
// There should be at least 1 successor. The default case!
MOZ_ASSERT(tableswitch->numSuccessors() > 0);
// If there are no cases, the default case is always taken.
if (tableswitch->numSuccessors() == 1) {
add(new(alloc()) LGoto(tableswitch->getDefault()));
return;
}
// If we don't know the type.
if (opd->type() == MIRType_Value) {
LTableSwitchV* lir = newLTableSwitchV(tableswitch);
useBox(lir, LTableSwitchV::InputValue, opd);
add(lir);
return;
}
// Case indices are numeric, so other types will always go to the default case.
if (opd->type() != MIRType_Int32 && opd->type() != MIRType_Double) {
add(new(alloc()) LGoto(tableswitch->getDefault()));
return;
}
// Return an LTableSwitch, capable of handling either an integer or
// floating-point index.
LAllocation index;
LDefinition tempInt;
if (opd->type() == MIRType_Int32) {
index = useRegisterAtStart(opd);
tempInt = tempCopy(opd, 0);
} else {
index = useRegister(opd);
tempInt = temp(LDefinition::GENERAL);
}
add(newLTableSwitch(index, tempInt, tableswitch));
}
void
LIRGenerator::visitCheckOverRecursed(MCheckOverRecursed* ins)
{
LCheckOverRecursed* lir = new(alloc()) LCheckOverRecursed();
add(lir, ins);
assignSafepoint(lir, ins);
}
void
LIRGenerator::visitDefVar(MDefVar* ins)
{
LDefVar* lir = new(alloc()) LDefVar(useRegisterAtStart(ins->scopeChain()));
add(lir, ins);
assignSafepoint(lir, ins);
}
void
LIRGenerator::visitDefFun(MDefFun* ins)
{
LDefFun* lir = new(alloc()) LDefFun(useRegisterAtStart(ins->scopeChain()));
add(lir, ins);
assignSafepoint(lir, ins);
}
void
LIRGenerator::visitNewArray(MNewArray* ins)
{
LNewArray* lir = new(alloc()) LNewArray(temp());
define(lir, ins);
assignSafepoint(lir, ins);
}
void
LIRGenerator::visitNewArrayCopyOnWrite(MNewArrayCopyOnWrite* ins)
{
LNewArrayCopyOnWrite* lir = new(alloc()) LNewArrayCopyOnWrite(temp());
define(lir, ins);
assignSafepoint(lir, ins);
}
void
LIRGenerator::visitNewArrayDynamicLength(MNewArrayDynamicLength* ins)
{
MDefinition* length = ins->length();
MOZ_ASSERT(length->type() == MIRType_Int32);
LNewArrayDynamicLength* lir = new(alloc()) LNewArrayDynamicLength(useRegister(length), temp());
define(lir, ins);
assignSafepoint(lir, ins);
}
void
LIRGenerator::visitNewObject(MNewObject* ins)
{
LNewObject* lir = new(alloc()) LNewObject(temp());
define(lir, ins);
assignSafepoint(lir, ins);
}
void
LIRGenerator::visitNewTypedObject(MNewTypedObject* ins)
{
LNewTypedObject* lir = new(alloc()) LNewTypedObject(temp());
define(lir, ins);
assignSafepoint(lir, ins);
}
void
LIRGenerator::visitNewDeclEnvObject(MNewDeclEnvObject* ins)
{
LNewDeclEnvObject* lir = new(alloc()) LNewDeclEnvObject(temp());
define(lir, ins);
assignSafepoint(lir, ins);
}
void
LIRGenerator::visitNewCallObject(MNewCallObject* ins)
{
LInstruction* lir;
if (ins->templateObject()->isSingleton()) {
LNewSingletonCallObject* singletonLir = new(alloc()) LNewSingletonCallObject(temp());
define(singletonLir, ins);
lir = singletonLir;
} else {
LNewCallObject* normalLir = new(alloc()) LNewCallObject(temp());
define(normalLir, ins);
lir = normalLir;
}
assignSafepoint(lir, ins);
}
void
LIRGenerator::visitNewRunOnceCallObject(MNewRunOnceCallObject* ins)
{
LNewSingletonCallObject* lir = new(alloc()) LNewSingletonCallObject(temp());
define(lir, ins);
assignSafepoint(lir, ins);
}
void
LIRGenerator::visitNewDerivedTypedObject(MNewDerivedTypedObject* ins)
{
LNewDerivedTypedObject* lir =
new(alloc()) LNewDerivedTypedObject(useRegisterAtStart(ins->type()),
useRegisterAtStart(ins->owner()),
useRegisterAtStart(ins->offset()));
defineReturn(lir, ins);
assignSafepoint(lir, ins);
}
void
LIRGenerator::visitNewStringObject(MNewStringObject* ins)
{
MOZ_ASSERT(ins->input()->type() == MIRType_String);
LNewStringObject* lir = new(alloc()) LNewStringObject(useRegister(ins->input()), temp());
define(lir, ins);
assignSafepoint(lir, ins);
}
void
LIRGenerator::visitInitElem(MInitElem* ins)
{
LInitElem* lir = new(alloc()) LInitElem(useRegisterAtStart(ins->getObject()));
useBoxAtStart(lir, LInitElem::IdIndex, ins->getId());
useBoxAtStart(lir, LInitElem::ValueIndex, ins->getValue());
add(lir, ins);
assignSafepoint(lir, ins);
}
void
LIRGenerator::visitInitElemGetterSetter(MInitElemGetterSetter* ins)
{
LInitElemGetterSetter* lir =
new(alloc()) LInitElemGetterSetter(useRegisterAtStart(ins->object()),
useRegisterAtStart(ins->value()));
useBoxAtStart(lir, LInitElemGetterSetter::IdIndex, ins->idValue());
add(lir, ins);
assignSafepoint(lir, ins);
}
void
LIRGenerator::visitMutateProto(MMutateProto* ins)
{
LMutateProto* lir = new(alloc()) LMutateProto(useRegisterAtStart(ins->getObject()));
useBoxAtStart(lir, LMutateProto::ValueIndex, ins->getValue());
add(lir, ins);
assignSafepoint(lir, ins);
}
void
LIRGenerator::visitInitProp(MInitProp* ins)
{
LInitProp* lir = new(alloc()) LInitProp(useRegisterAtStart(ins->getObject()));
useBoxAtStart(lir, LInitProp::ValueIndex, ins->getValue());
add(lir, ins);
assignSafepoint(lir, ins);
}
void
LIRGenerator::visitInitPropGetterSetter(MInitPropGetterSetter* ins)
{
LInitPropGetterSetter* lir =
new(alloc()) LInitPropGetterSetter(useRegisterAtStart(ins->object()),
useRegisterAtStart(ins->value()));
add(lir, ins);
assignSafepoint(lir, ins);
}
void
LIRGenerator::visitCreateThisWithTemplate(MCreateThisWithTemplate* ins)
{
LCreateThisWithTemplate* lir = new(alloc()) LCreateThisWithTemplate(temp());
define(lir, ins);
assignSafepoint(lir, ins);
}
void
LIRGenerator::visitCreateThisWithProto(MCreateThisWithProto* ins)
{
LCreateThisWithProto* lir =
new(alloc()) LCreateThisWithProto(useRegisterOrConstantAtStart(ins->getCallee()),
useRegisterOrConstantAtStart(ins->getPrototype()));
defineReturn(lir, ins);
assignSafepoint(lir, ins);
}
void
LIRGenerator::visitCreateThis(MCreateThis* ins)
{
LCreateThis* lir = new(alloc()) LCreateThis(useRegisterOrConstantAtStart(ins->getCallee()));
defineReturn(lir, ins);
assignSafepoint(lir, ins);
}
void
LIRGenerator::visitCreateArgumentsObject(MCreateArgumentsObject* ins)
{
// LAllocation callObj = useRegisterAtStart(ins->getCallObject());
LAllocation callObj = useFixed(ins->getCallObject(), CallTempReg0);
LCreateArgumentsObject* lir = new(alloc()) LCreateArgumentsObject(callObj, tempFixed(CallTempReg1));
defineReturn(lir, ins);
assignSafepoint(lir, ins);
}
void
LIRGenerator::visitGetArgumentsObjectArg(MGetArgumentsObjectArg* ins)
{
LAllocation argsObj = useRegister(ins->getArgsObject());
LGetArgumentsObjectArg* lir = new(alloc()) LGetArgumentsObjectArg(argsObj, temp());
defineBox(lir, ins);
}
void
LIRGenerator::visitSetArgumentsObjectArg(MSetArgumentsObjectArg* ins)
{
LAllocation argsObj = useRegister(ins->getArgsObject());
LSetArgumentsObjectArg* lir = new(alloc()) LSetArgumentsObjectArg(argsObj, temp());
useBox(lir, LSetArgumentsObjectArg::ValueIndex, ins->getValue());
add(lir, ins);
}
void
LIRGenerator::visitReturnFromCtor(MReturnFromCtor* ins)
{
LReturnFromCtor* lir = new(alloc()) LReturnFromCtor(useRegister(ins->getObject()));
useBox(lir, LReturnFromCtor::ValueIndex, ins->getValue());
define(lir, ins);
}
void
LIRGenerator::visitComputeThis(MComputeThis* ins)
{
MOZ_ASSERT(ins->type() == MIRType_Object);
MOZ_ASSERT(ins->input()->type() == MIRType_Value);
LComputeThis* lir = new(alloc()) LComputeThis();
// Don't use useBoxAtStart because ComputeThis has a safepoint and needs to
// have its inputs in different registers than its return value so that
// they aren't clobbered.
useBox(lir, LComputeThis::ValueIndex, ins->input());
define(lir, ins);
assignSafepoint(lir, ins);
}
void
LIRGenerator::visitLoadArrowThis(MLoadArrowThis* ins)
{
MOZ_ASSERT(ins->type() == MIRType_Value);
MOZ_ASSERT(ins->callee()->type() == MIRType_Object);
LLoadArrowThis* lir = new(alloc()) LLoadArrowThis(useRegister(ins->callee()));
defineBox(lir, ins);
}
void
LIRGenerator::lowerCallArguments(MCall* call)
{
uint32_t argc = call->numStackArgs();
// Align the arguments of a call such that the callee would keep the same
// alignment as the caller.
uint32_t baseSlot = 0;
static const uint32_t alignment = JitStackAlignment / sizeof(Value);
if (alignment > 1)
baseSlot = AlignBytes(argc, alignment);
else
baseSlot = argc;
// Save the maximum number of argument, such that we can have one unique
// frame size.
if (baseSlot > maxargslots_)
maxargslots_ = baseSlot;
for (size_t i = 0; i < argc; i++) {
MDefinition* arg = call->getArg(i);
uint32_t argslot = baseSlot - i;
// Values take a slow path.
if (arg->type() == MIRType_Value) {
LStackArgV* stack = new(alloc()) LStackArgV(argslot);
useBox(stack, 0, arg);
add(stack);
} else {
// Known types can move constant types and/or payloads.
LStackArgT* stack = new(alloc()) LStackArgT(argslot, arg->type(), useRegisterOrConstant(arg));
add(stack);
}
}
}
void
LIRGenerator::visitCall(MCall* call)
{
MOZ_ASSERT(CallTempReg0 != CallTempReg1);
MOZ_ASSERT(CallTempReg0 != ArgumentsRectifierReg);
MOZ_ASSERT(CallTempReg1 != ArgumentsRectifierReg);
MOZ_ASSERT(call->getFunction()->type() == MIRType_Object);
lowerCallArguments(call);
// Height of the current argument vector.
JSFunction* target = call->getSingleTarget();
LInstruction* lir;
if (call->isCallDOMNative()) {
// Call DOM functions.
MOZ_ASSERT(target && target->isNative());
Register cxReg, objReg, privReg, argsReg;
GetTempRegForIntArg(0, 0, &cxReg);
GetTempRegForIntArg(1, 0, &objReg);
GetTempRegForIntArg(2, 0, &privReg);
mozilla::DebugOnly<bool> ok = GetTempRegForIntArg(3, 0, &argsReg);
MOZ_ASSERT(ok, "How can we not have four temp registers?");
lir = new(alloc()) LCallDOMNative(tempFixed(cxReg), tempFixed(objReg),
tempFixed(privReg), tempFixed(argsReg));
} else if (target) {
// Call known functions.
if (target->isNative()) {
Register cxReg, numReg, vpReg, tmpReg;
GetTempRegForIntArg(0, 0, &cxReg);
GetTempRegForIntArg(1, 0, &numReg);
GetTempRegForIntArg(2, 0, &vpReg);
// Even though this is just a temp reg, use the same API to avoid
// register collisions.
mozilla::DebugOnly<bool> ok = GetTempRegForIntArg(3, 0, &tmpReg);
MOZ_ASSERT(ok, "How can we not have four temp registers?");
lir = new(alloc()) LCallNative(tempFixed(cxReg), tempFixed(numReg),
tempFixed(vpReg), tempFixed(tmpReg));
} else {
lir = new(alloc()) LCallKnown(useFixed(call->getFunction(), CallTempReg0),
tempFixed(CallTempReg2));
}
} else {
// Call anything, using the most generic code.
lir = new(alloc()) LCallGeneric(useFixed(call->getFunction(), CallTempReg0),
tempFixed(ArgumentsRectifierReg),
tempFixed(CallTempReg2));
}
defineReturn(lir, call);
assignSafepoint(lir, call);
}
void
LIRGenerator::visitApplyArgs(MApplyArgs* apply)
{
MOZ_ASSERT(apply->getFunction()->type() == MIRType_Object);
// Assert if we cannot build a rectifier frame.
MOZ_ASSERT(CallTempReg0 != ArgumentsRectifierReg);
MOZ_ASSERT(CallTempReg1 != ArgumentsRectifierReg);
// Assert if the return value is already erased.
MOZ_ASSERT(CallTempReg2 != JSReturnReg_Type);
MOZ_ASSERT(CallTempReg2 != JSReturnReg_Data);
LApplyArgsGeneric* lir = new(alloc()) LApplyArgsGeneric(
useFixed(apply->getFunction(), CallTempReg3),
useFixed(apply->getArgc(), CallTempReg0),
tempFixed(CallTempReg1), // object register
tempFixed(CallTempReg2)); // stack counter register
MDefinition* self = apply->getThis();
useBoxFixed(lir, LApplyArgsGeneric::ThisIndex, self, CallTempReg4, CallTempReg5);
// Bailout is only needed in the case of possible non-JSFunction callee.
if (!apply->getSingleTarget())
assignSnapshot(lir, Bailout_NonJSFunctionCallee);
defineReturn(lir, apply);
assignSafepoint(lir, apply);
}
void
LIRGenerator::visitBail(MBail* bail)
{
LBail* lir = new(alloc()) LBail();
assignSnapshot(lir, bail->bailoutKind());
add(lir, bail);
}
void
LIRGenerator::visitUnreachable(MUnreachable* unreachable)
{
LUnreachable* lir = new(alloc()) LUnreachable();
add(lir, unreachable);
}
void
LIRGenerator::visitAssertFloat32(MAssertFloat32* assertion)
{
MIRType type = assertion->input()->type();
DebugOnly<bool> checkIsFloat32 = assertion->mustBeFloat32();
if (type != MIRType_Value && !js_JitOptions.eagerCompilation) {
MOZ_ASSERT_IF(checkIsFloat32, type == MIRType_Float32);
MOZ_ASSERT_IF(!checkIsFloat32, type != MIRType_Float32);
}
}
void
LIRGenerator::visitArraySplice(MArraySplice* ins)
{
LArraySplice* lir = new(alloc()) LArraySplice(useRegisterAtStart(ins->object()),
useRegisterAtStart(ins->start()),
useRegisterAtStart(ins->deleteCount()));
add(lir, ins);
assignSafepoint(lir, ins);
}
void
LIRGenerator::visitGetDynamicName(MGetDynamicName* ins)
{
MDefinition* scopeChain = ins->getScopeChain();
MOZ_ASSERT(scopeChain->type() == MIRType_Object);
MDefinition* name = ins->getName();
MOZ_ASSERT(name->type() == MIRType_String);
LGetDynamicName* lir = new(alloc()) LGetDynamicName(useFixed(scopeChain, CallTempReg0),
useFixed(name, CallTempReg1),
tempFixed(CallTempReg2),
tempFixed(CallTempReg3),
tempFixed(CallTempReg4));
assignSnapshot(lir, Bailout_DynamicNameNotFound);
defineReturn(lir, ins);
}
void
LIRGenerator::visitCallDirectEval(MCallDirectEval* ins)
{
MDefinition* scopeChain = ins->getScopeChain();
MOZ_ASSERT(scopeChain->type() == MIRType_Object);
MDefinition* string = ins->getString();
MOZ_ASSERT(string->type() == MIRType_String || string->type() == MIRType_Value);
MDefinition* thisValue = ins->getThisValue();
LInstruction* lir;
if (string->type() == MIRType_String) {
lir = new(alloc()) LCallDirectEvalS(useRegisterAtStart(scopeChain),
useRegisterAtStart(string));
} else {
lir = new(alloc()) LCallDirectEvalV(useRegisterAtStart(scopeChain));
useBoxAtStart(lir, LCallDirectEvalV::Argument, string);
}
if (string->type() == MIRType_String)
useBoxAtStart(lir, LCallDirectEvalS::ThisValue, thisValue);
else
useBoxAtStart(lir, LCallDirectEvalV::ThisValue, thisValue);
defineReturn(lir, ins);
assignSafepoint(lir, ins);
}
static JSOp
ReorderComparison(JSOp op, MDefinition** lhsp, MDefinition** rhsp)
{
MDefinition* lhs = *lhsp;
MDefinition* rhs = *rhsp;
if (lhs->isConstantValue()) {
*rhsp = lhs;
*lhsp = rhs;
return ReverseCompareOp(op);
}
return op;
}
void
LIRGenerator::visitTest(MTest* test)
{
MDefinition* opd = test->getOperand(0);
MBasicBlock* ifTrue = test->ifTrue();
MBasicBlock* ifFalse = test->ifFalse();
// String is converted to length of string in the type analysis phase (see
// TestPolicy).
MOZ_ASSERT(opd->type() != MIRType_String);
// Testing a constant.
if (opd->isConstantValue() && !opd->constantValue().isMagic()) {
bool result = opd->constantToBoolean();
add(new(alloc()) LGoto(result ? ifTrue : ifFalse));
return;
}
if (opd->type() == MIRType_Value) {
LDefinition temp0, temp1;
if (test->operandMightEmulateUndefined()) {
temp0 = temp();
temp1 = temp();
} else {
temp0 = LDefinition::BogusTemp();
temp1 = LDefinition::BogusTemp();
}
LTestVAndBranch* lir =
new(alloc()) LTestVAndBranch(ifTrue, ifFalse, tempDouble(), temp0, temp1);
useBox(lir, LTestVAndBranch::Input, opd);
add(lir, test);
return;
}
if (opd->type() == MIRType_ObjectOrNull) {
LDefinition temp0 = test->operandMightEmulateUndefined() ? temp() : LDefinition::BogusTemp();
add(new(alloc()) LTestOAndBranch(useRegister(opd), ifTrue, ifFalse, temp0), test);
return;
}
// Objects are truthy, except if it might emulate undefined.
if (opd->type() == MIRType_Object) {
if (test->operandMightEmulateUndefined())
add(new(alloc()) LTestOAndBranch(useRegister(opd), ifTrue, ifFalse, temp()), test);
else
add(new(alloc()) LGoto(ifTrue));
return;
}
// These must be explicitly sniffed out since they are constants and have
// no payload.
if (opd->type() == MIRType_Undefined || opd->type() == MIRType_Null) {
add(new(alloc()) LGoto(ifFalse));
return;
}
// All symbols are truthy.
if (opd->type() == MIRType_Symbol) {
add(new(alloc()) LGoto(ifTrue));
return;
}
// Check if the operand for this test is a compare operation. If it is, we want
// to emit an LCompare*AndBranch rather than an LTest*AndBranch, to fuse the
// compare and jump instructions.
if (opd->isCompare() && opd->isEmittedAtUses()) {
MCompare* comp = opd->toCompare();
MDefinition* left = comp->lhs();
MDefinition* right = comp->rhs();
// Try to fold the comparison so that we don't have to handle all cases.
bool result;
if (comp->tryFold(&result)) {
add(new(alloc()) LGoto(result ? ifTrue : ifFalse));
return;
}
// Emit LCompare*AndBranch.
// Compare and branch null/undefined.
// The second operand has known null/undefined type,
// so just test the first operand.
if (comp->compareType() == MCompare::Compare_Null ||
comp->compareType() == MCompare::Compare_Undefined)
{
if (left->type() == MIRType_Object || left->type() == MIRType_ObjectOrNull) {
MOZ_ASSERT(left->type() == MIRType_ObjectOrNull ||
comp->operandMightEmulateUndefined(),
"MCompare::tryFold should handle the never-emulates-undefined case");
LDefinition tmp =
comp->operandMightEmulateUndefined() ? temp() : LDefinition::BogusTemp();
LIsNullOrLikeUndefinedAndBranchT* lir =
new(alloc()) LIsNullOrLikeUndefinedAndBranchT(comp, useRegister(left),
ifTrue, ifFalse, tmp);
add(lir, test);
return;
}
LDefinition tmp, tmpToUnbox;
if (comp->operandMightEmulateUndefined()) {
tmp = temp();
tmpToUnbox = tempToUnbox();
} else {
tmp = LDefinition::BogusTemp();
tmpToUnbox = LDefinition::BogusTemp();
}
LIsNullOrLikeUndefinedAndBranchV* lir =
new(alloc()) LIsNullOrLikeUndefinedAndBranchV(comp, ifTrue, ifFalse,
tmp, tmpToUnbox);
useBox(lir, LIsNullOrLikeUndefinedAndBranchV::Value, left);
add(lir, test);
return;
}
// Compare and branch booleans.
if (comp->compareType() == MCompare::Compare_Boolean) {
MOZ_ASSERT(left->type() == MIRType_Value);
MOZ_ASSERT(right->type() == MIRType_Boolean);
LAllocation rhs = useRegisterOrConstant(right);
LCompareBAndBranch* lir = new(alloc()) LCompareBAndBranch(comp, rhs, ifTrue, ifFalse);
useBox(lir, LCompareBAndBranch::Lhs, left);
add(lir, test);
return;
}
// Compare and branch Int32 or Object pointers.
if (comp->isInt32Comparison() ||
comp->compareType() == MCompare::Compare_UInt32 ||
comp->compareType() == MCompare::Compare_Object)
{
JSOp op = ReorderComparison(comp->jsop(), &left, &right);
LAllocation lhs = useRegister(left);
LAllocation rhs;
if (comp->isInt32Comparison() || comp->compareType() == MCompare::Compare_UInt32)
rhs = useAnyOrConstant(right);
else
rhs = useRegister(right);
LCompareAndBranch* lir = new(alloc()) LCompareAndBranch(comp, op, lhs, rhs,
ifTrue, ifFalse);
add(lir, test);
return;
}
// Compare and branch doubles.
if (comp->isDoubleComparison()) {
LAllocation lhs = useRegister(left);
LAllocation rhs = useRegister(right);
LCompareDAndBranch* lir = new(alloc()) LCompareDAndBranch(comp, lhs, rhs,
ifTrue, ifFalse);
add(lir, test);
return;
}
// Compare and branch floats.
if (comp->isFloat32Comparison()) {
LAllocation lhs = useRegister(left);
LAllocation rhs = useRegister(right);
LCompareFAndBranch* lir = new(alloc()) LCompareFAndBranch(comp, lhs, rhs,
ifTrue, ifFalse);
add(lir, test);
return;
}
// Compare values.
if (comp->compareType() == MCompare::Compare_Value) {
LCompareVAndBranch* lir = new(alloc()) LCompareVAndBranch(comp, ifTrue, ifFalse);
useBoxAtStart(lir, LCompareVAndBranch::LhsInput, left);
useBoxAtStart(lir, LCompareVAndBranch::RhsInput, right);
add(lir, test);
return;
}
}
// Check if the operand for this test is a bitand operation. If it is, we want
// to emit an LBitAndAndBranch rather than an LTest*AndBranch.
if (opd->isBitAnd() && opd->isEmittedAtUses()) {
MDefinition* lhs = opd->getOperand(0);
MDefinition* rhs = opd->getOperand(1);
if (lhs->type() == MIRType_Int32 && rhs->type() == MIRType_Int32) {
ReorderCommutative(&lhs, &rhs, test);
lowerForBitAndAndBranch(new(alloc()) LBitAndAndBranch(ifTrue, ifFalse), test, lhs, rhs);
return;
}
}
if (opd->isIsObject() && opd->isEmittedAtUses()) {
MDefinition* input = opd->toIsObject()->input();
MOZ_ASSERT(input->type() == MIRType_Value);
LIsObjectAndBranch* lir = new(alloc()) LIsObjectAndBranch(ifTrue, ifFalse);
useBoxAtStart(lir, LIsObjectAndBranch::Input, input);
add(lir, test);
return;
}
if (opd->isIsNoIter()) {
MOZ_ASSERT(opd->isEmittedAtUses());
MDefinition* input = opd->toIsNoIter()->input();
MOZ_ASSERT(input->type() == MIRType_Value);
LIsNoIterAndBranch* lir = new(alloc()) LIsNoIterAndBranch(ifTrue, ifFalse);
useBox(lir, LIsNoIterAndBranch::Input, input);
add(lir, test);
return;
}
switch (opd->type()) {
case MIRType_Double:
add(new(alloc()) LTestDAndBranch(useRegister(opd), ifTrue, ifFalse));
break;
case MIRType_Float32:
add(new(alloc()) LTestFAndBranch(useRegister(opd), ifTrue, ifFalse));
break;
case MIRType_Int32:
case MIRType_Boolean:
add(new(alloc()) LTestIAndBranch(useRegister(opd), ifTrue, ifFalse));
break;
default:
MOZ_CRASH("Bad type");
}
}
void
LIRGenerator::visitGotoWithFake(MGotoWithFake* gotoWithFake)
{
add(new(alloc()) LGoto(gotoWithFake->target()));
}
void
LIRGenerator::visitFunctionDispatch(MFunctionDispatch* ins)
{
LFunctionDispatch* lir = new(alloc()) LFunctionDispatch(useRegister(ins->input()));
add(lir, ins);
}
void
LIRGenerator::visitObjectGroupDispatch(MObjectGroupDispatch* ins)
{
LObjectGroupDispatch* lir = new(alloc()) LObjectGroupDispatch(useRegister(ins->input()), temp());
add(lir, ins);
}
static inline bool
CanEmitCompareAtUses(MInstruction* ins)
{
if (!ins->canEmitAtUses())
return false;
bool foundTest = false;
for (MUseIterator iter(ins->usesBegin()); iter != ins->usesEnd(); iter++) {
MNode* node = iter->consumer();
if (!node->isDefinition())
return false;
if (!node->toDefinition()->isTest())
return false;
if (foundTest)
return false;
foundTest = true;
}
return true;
}
void
LIRGenerator::visitCompare(MCompare* comp)
{
MDefinition* left = comp->lhs();
MDefinition* right = comp->rhs();
// Try to fold the comparison so that we don't have to handle all cases.
bool result;
if (comp->tryFold(&result)) {
define(new(alloc()) LInteger(result), comp);
return;
}
// Move below the emitAtUses call if we ever implement
// LCompareSAndBranch. Doing this now wouldn't be wrong, but doesn't
// make sense and avoids confusion.
if (comp->compareType() == MCompare::Compare_String) {
LCompareS* lir = new(alloc()) LCompareS(useRegister(left), useRegister(right));
define(lir, comp);
assignSafepoint(lir, comp);
return;
}
// Strict compare between value and string
if (comp->compareType() == MCompare::Compare_StrictString) {
MOZ_ASSERT(left->type() == MIRType_Value);
MOZ_ASSERT(right->type() == MIRType_String);
LCompareStrictS* lir = new(alloc()) LCompareStrictS(useRegister(right), tempToUnbox());
useBox(lir, LCompareStrictS::Lhs, left);
define(lir, comp);
assignSafepoint(lir, comp);
return;
}
// Unknown/unspecialized compare use a VM call.
if (comp->compareType() == MCompare::Compare_Unknown) {
LCompareVM* lir = new(alloc()) LCompareVM();
useBoxAtStart(lir, LCompareVM::LhsInput, left);
useBoxAtStart(lir, LCompareVM::RhsInput, right);
defineReturn(lir, comp);
assignSafepoint(lir, comp);
return;
}
// Sniff out if the output of this compare is used only for a branching.
// If it is, then we will emit an LCompare*AndBranch instruction in place
// of this compare and any test that uses this compare. Thus, we can
// ignore this Compare.
if (CanEmitCompareAtUses(comp)) {
emitAtUses(comp);
return;
}
// Compare Null and Undefined.
if (comp->compareType() == MCompare::Compare_Null ||
comp->compareType() == MCompare::Compare_Undefined)
{
if (left->type() == MIRType_Object || left->type() == MIRType_ObjectOrNull) {
MOZ_ASSERT(left->type() == MIRType_ObjectOrNull ||
comp->operandMightEmulateUndefined(),
"MCompare::tryFold should have folded this away");
define(new(alloc()) LIsNullOrLikeUndefinedT(useRegister(left)), comp);
return;
}
LDefinition tmp, tmpToUnbox;
if (comp->operandMightEmulateUndefined()) {
tmp = temp();
tmpToUnbox = tempToUnbox();
} else {
tmp = LDefinition::BogusTemp();
tmpToUnbox = LDefinition::BogusTemp();
}
LIsNullOrLikeUndefinedV* lir = new(alloc()) LIsNullOrLikeUndefinedV(tmp, tmpToUnbox);
useBox(lir, LIsNullOrLikeUndefinedV::Value, left);
define(lir, comp);
return;
}
// Compare booleans.
if (comp->compareType() == MCompare::Compare_Boolean) {
MOZ_ASSERT(left->type() == MIRType_Value);
MOZ_ASSERT(right->type() == MIRType_Boolean);
LCompareB* lir = new(alloc()) LCompareB(useRegisterOrConstant(right));
useBox(lir, LCompareB::Lhs, left);
define(lir, comp);
return;
}
// Compare Int32 or Object pointers.
if (comp->isInt32Comparison() ||
comp->compareType() == MCompare::Compare_UInt32 ||
comp->compareType() == MCompare::Compare_Object)
{
JSOp op = ReorderComparison(comp->jsop(), &left, &right);
LAllocation lhs = useRegister(left);
LAllocation rhs;
if (comp->isInt32Comparison() ||
comp->compareType() == MCompare::Compare_UInt32)
{
rhs = useAnyOrConstant(right);
} else {
rhs = useRegister(right);
}
define(new(alloc()) LCompare(op, lhs, rhs), comp);
return;
}
// Compare doubles.
if (comp->isDoubleComparison()) {
define(new(alloc()) LCompareD(useRegister(left), useRegister(right)), comp);
return;
}
// Compare float32.
if (comp->isFloat32Comparison()) {
define(new(alloc()) LCompareF(useRegister(left), useRegister(right)), comp);
return;
}
// Compare values.
if (comp->compareType() == MCompare::Compare_Value) {
LCompareV* lir = new(alloc()) LCompareV();
useBoxAtStart(lir, LCompareV::LhsInput, left);
useBoxAtStart(lir, LCompareV::RhsInput, right);
define(lir, comp);
return;
}