forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BaselineIC.cpp
3868 lines (3255 loc) · 119 KB
/
BaselineIC.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: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* 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/BaselineIC.h"
#include "mozilla/Casting.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/IntegerPrintfMacros.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/Sprintf.h"
#include "mozilla/TemplateLib.h"
#include "mozilla/Unused.h"
#include "jsfriendapi.h"
#include "jslibmath.h"
#include "jstypes.h"
#include "builtin/Eval.h"
#include "gc/Policy.h"
#include "jit/BaselineCacheIRCompiler.h"
#include "jit/BaselineDebugModeOSR.h"
#include "jit/BaselineJIT.h"
#include "jit/InlinableNatives.h"
#include "jit/JitSpewer.h"
#include "jit/Linker.h"
#include "jit/Lowering.h"
#ifdef JS_ION_PERF
# include "jit/PerfSpewer.h"
#endif
#include "jit/SharedICHelpers.h"
#include "jit/VMFunctions.h"
#include "js/Conversions.h"
#include "js/GCVector.h"
#include "vm/BytecodeIterator.h"
#include "vm/BytecodeLocation.h"
#include "vm/BytecodeUtil.h"
#include "vm/JSFunction.h"
#include "vm/JSScript.h"
#include "vm/Opcodes.h"
#include "vm/SelfHosting.h"
#include "vm/TypedArrayObject.h"
#ifdef MOZ_VTUNE
# include "vtune/VTuneWrapper.h"
#endif
#include "builtin/Boolean-inl.h"
#include "jit/JitFrames-inl.h"
#include "jit/MacroAssembler-inl.h"
#include "jit/shared/Lowering-shared-inl.h"
#include "jit/SharedICHelpers-inl.h"
#include "jit/VMFunctionList-inl.h"
#include "vm/BytecodeIterator-inl.h"
#include "vm/BytecodeLocation-inl.h"
#include "vm/EnvironmentObject-inl.h"
#include "vm/Interpreter-inl.h"
#include "vm/JSScript-inl.h"
#include "vm/StringObject-inl.h"
using mozilla::DebugOnly;
namespace js {
namespace jit {
// Class used to emit all Baseline IC fallback code when initializing the
// JitRuntime.
class MOZ_RAII FallbackICCodeCompiler final : public ICStubCompilerBase {
BaselineICFallbackCode& code;
MacroAssembler& masm;
MOZ_MUST_USE bool emitCall(bool isSpread, bool isConstructing);
MOZ_MUST_USE bool emitGetElem(bool hasReceiver);
MOZ_MUST_USE bool emitGetProp(bool hasReceiver);
public:
FallbackICCodeCompiler(JSContext* cx, BaselineICFallbackCode& code,
MacroAssembler& masm)
: ICStubCompilerBase(cx), code(code), masm(masm) {}
#define DEF_METHOD(kind) MOZ_MUST_USE bool emit_##kind();
IC_BASELINE_FALLBACK_CODE_KIND_LIST(DEF_METHOD)
#undef DEF_METHOD
};
#ifdef JS_JITSPEW
void FallbackICSpew(JSContext* cx, ICFallbackStub* stub, const char* fmt, ...) {
if (JitSpewEnabled(JitSpew_BaselineICFallback)) {
RootedScript script(cx, GetTopJitJSScript(cx));
jsbytecode* pc = stub->icEntry()->pc(script);
char fmtbuf[100];
va_list args;
va_start(args, fmt);
(void)VsprintfLiteral(fmtbuf, fmt, args);
va_end(args);
JitSpew(
JitSpew_BaselineICFallback,
"Fallback hit for (%s:%u:%u) (pc=%zu,line=%d,uses=%d,stubs=%zu): %s",
script->filename(), script->lineno(), script->column(),
script->pcToOffset(pc), PCToLineNumber(script, pc),
script->getWarmUpCount(), stub->numOptimizedStubs(), fmtbuf);
}
}
void TypeFallbackICSpew(JSContext* cx, ICTypeMonitor_Fallback* stub,
const char* fmt, ...) {
if (JitSpewEnabled(JitSpew_BaselineICFallback)) {
RootedScript script(cx, GetTopJitJSScript(cx));
jsbytecode* pc = stub->icEntry()->pc(script);
char fmtbuf[100];
va_list args;
va_start(args, fmt);
(void)VsprintfLiteral(fmtbuf, fmt, args);
va_end(args);
JitSpew(JitSpew_BaselineICFallback,
"Type monitor fallback hit for (%s:%u:%u) "
"(pc=%zu,line=%d,uses=%d,stubs=%d): %s",
script->filename(), script->lineno(), script->column(),
script->pcToOffset(pc), PCToLineNumber(script, pc),
script->getWarmUpCount(), (int)stub->numOptimizedMonitorStubs(),
fmtbuf);
}
}
#endif // JS_JITSPEW
ICFallbackStub* ICEntry::fallbackStub() const {
return firstStub()->getChainFallback();
}
void ICEntry::trace(JSTracer* trc) {
#ifdef JS_64BIT
// If we have filled our padding with a magic value, check it now.
MOZ_DIAGNOSTIC_ASSERT(traceMagic_ == EXPECTED_TRACE_MAGIC);
#endif
for (ICStub* stub = firstStub(); stub; stub = stub->next()) {
stub->trace(trc);
}
}
// Allocator for Baseline IC fallback stubs. These stubs use trampoline code
// stored in JitRuntime.
class MOZ_RAII FallbackStubAllocator {
JSContext* cx_;
ICStubSpace& stubSpace_;
const BaselineICFallbackCode& code_;
public:
FallbackStubAllocator(JSContext* cx, ICStubSpace& stubSpace)
: cx_(cx),
stubSpace_(stubSpace),
code_(cx->runtime()->jitRuntime()->baselineICFallbackCode()) {}
template <typename T, typename... Args>
T* newStub(BaselineICFallbackKind kind, Args&&... args) {
TrampolinePtr addr = code_.addr(kind);
return ICStub::NewFallback<T>(cx_, &stubSpace_, addr,
std::forward<Args>(args)...);
}
};
// Helper method called by lambda expressions `addIC` and `addPrologueIC` in
// `JitScript::initICEntriesAndBytecodeTypeMap`.
static bool AddICImpl(JSContext* cx, JitScript* jitScript, uint32_t offset,
ICStub* stub, uint32_t& icEntryIndex) {
if (!stub) {
MOZ_ASSERT(cx->isExceptionPending());
mozilla::Unused << cx; // Silence -Wunused-lambda-capture in opt builds.
return false;
}
// Initialize the ICEntry.
ICEntry& entryRef = jitScript->icEntry(icEntryIndex);
icEntryIndex++;
new (&entryRef) ICEntry(stub, offset);
// Fix up pointers from fallback stubs to the ICEntry.
if (stub->isFallback()) {
stub->toFallbackStub()->fixupICEntry(&entryRef);
} else {
stub->toTypeMonitor_Fallback()->fixupICEntry(&entryRef);
}
return true;
}
bool JitScript::initICEntriesAndBytecodeTypeMap(JSContext* cx,
JSScript* script) {
MOZ_ASSERT(cx->realm()->jitRealm());
MOZ_ASSERT(jit::IsBaselineInterpreterEnabled());
MOZ_ASSERT(numICEntries() == script->numICEntries());
FallbackStubAllocator alloc(cx, fallbackStubSpace_);
// Index of the next ICEntry to initialize.
uint32_t icEntryIndex = 0;
using Kind = BaselineICFallbackKind;
auto addIC = [cx, this, script, &icEntryIndex](BytecodeLocation loc,
ICStub* stub) {
uint32_t offset = loc.bytecodeToOffset(script);
return AddICImpl(cx, this, offset, stub, icEntryIndex);
};
// Lambda expression for adding ICs for non-op ICs
auto addPrologueIC = [cx, this, &icEntryIndex](ICStub* stub) {
return AddICImpl(cx, this, ICEntry::ProloguePCOffset, stub, icEntryIndex);
};
// Add ICEntries and fallback stubs for this/argument type checks.
// Note: we pass a nullptr pc to indicate this is a non-op IC.
// See ICEntry::NonOpPCOffset.
if (JSFunction* fun = script->function()) {
ICStub* stub =
alloc.newStub<ICTypeMonitor_Fallback>(Kind::TypeMonitor, nullptr, 0);
if (!addPrologueIC(stub)) {
return false;
}
for (size_t i = 0; i < fun->nargs(); i++) {
ICStub* stub = alloc.newStub<ICTypeMonitor_Fallback>(Kind::TypeMonitor,
nullptr, i + 1);
if (!addPrologueIC(stub)) {
return false;
}
}
}
// Index of the next bytecode type map entry to initialize.
uint32_t typeMapIndex = 0;
uint32_t* const typeMap = bytecodeTypeMap();
// For JOF_IC ops: initialize ICEntries and fallback stubs.
// For JOF_TYPESET ops: initialize bytecode type map entries.
for (BytecodeLocation loc : js::AllBytecodesIterable(script)) {
JSOp op = loc.getOp();
// Note: if the script is very large there will be more JOF_TYPESET ops
// than bytecode type sets. See JSScript::MaxBytecodeTypeSets.
if (BytecodeOpHasTypeSet(op) &&
typeMapIndex < JSScript::MaxBytecodeTypeSets) {
typeMap[typeMapIndex] = loc.bytecodeToOffset(script);
typeMapIndex++;
}
// Assert the frontend stored the correct IC index in jump target ops.
MOZ_ASSERT_IF(BytecodeIsJumpTarget(op), loc.icIndex() == icEntryIndex);
if (!BytecodeOpHasIC(op)) {
continue;
}
switch (op) {
case JSOP_NOT:
case JSOP_AND:
case JSOP_OR:
case JSOP_IFEQ:
case JSOP_IFNE: {
ICStub* stub = alloc.newStub<ICToBool_Fallback>(Kind::ToBool);
if (!addIC(loc, stub)) {
return false;
}
break;
}
case JSOP_BITNOT:
case JSOP_NEG:
case JSOP_INC:
case JSOP_DEC: {
ICStub* stub = alloc.newStub<ICUnaryArith_Fallback>(Kind::UnaryArith);
if (!addIC(loc, stub)) {
return false;
}
break;
}
case JSOP_BITOR:
case JSOP_BITXOR:
case JSOP_BITAND:
case JSOP_LSH:
case JSOP_RSH:
case JSOP_URSH:
case JSOP_ADD:
case JSOP_SUB:
case JSOP_MUL:
case JSOP_DIV:
case JSOP_MOD:
case JSOP_POW: {
ICStub* stub = alloc.newStub<ICBinaryArith_Fallback>(Kind::BinaryArith);
if (!addIC(loc, stub)) {
return false;
}
break;
}
case JSOP_EQ:
case JSOP_NE:
case JSOP_LT:
case JSOP_LE:
case JSOP_GT:
case JSOP_GE:
case JSOP_STRICTEQ:
case JSOP_STRICTNE: {
ICStub* stub = alloc.newStub<ICCompare_Fallback>(Kind::Compare);
if (!addIC(loc, stub)) {
return false;
}
break;
}
case JSOP_NEWARRAY: {
ObjectGroup* group = ObjectGroup::allocationSiteGroup(
cx, script, loc.toRawBytecode(), JSProto_Array);
if (!group) {
return false;
}
ICStub* stub =
alloc.newStub<ICNewArray_Fallback>(Kind::NewArray, group);
if (!addIC(loc, stub)) {
return false;
}
break;
}
case JSOP_NEWOBJECT:
case JSOP_NEWINIT: {
ICStub* stub = alloc.newStub<ICNewObject_Fallback>(Kind::NewObject);
if (!addIC(loc, stub)) {
return false;
}
break;
}
case JSOP_INITELEM:
case JSOP_INITHIDDENELEM:
case JSOP_INITELEM_ARRAY:
case JSOP_INITELEM_INC:
case JSOP_SETELEM:
case JSOP_STRICTSETELEM: {
ICStub* stub = alloc.newStub<ICSetElem_Fallback>(Kind::SetElem);
if (!addIC(loc, stub)) {
return false;
}
break;
}
case JSOP_INITPROP:
case JSOP_INITLOCKEDPROP:
case JSOP_INITHIDDENPROP:
case JSOP_INITGLEXICAL:
case JSOP_SETPROP:
case JSOP_STRICTSETPROP:
case JSOP_SETNAME:
case JSOP_STRICTSETNAME:
case JSOP_SETGNAME:
case JSOP_STRICTSETGNAME: {
ICStub* stub = alloc.newStub<ICSetProp_Fallback>(Kind::SetProp);
if (!addIC(loc, stub)) {
return false;
}
break;
}
case JSOP_GETPROP:
case JSOP_CALLPROP:
case JSOP_LENGTH:
case JSOP_GETBOUNDNAME: {
ICStub* stub = alloc.newStub<ICGetProp_Fallback>(Kind::GetProp);
if (!addIC(loc, stub)) {
return false;
}
break;
}
case JSOP_GETPROP_SUPER: {
ICStub* stub = alloc.newStub<ICGetProp_Fallback>(Kind::GetPropSuper);
if (!addIC(loc, stub)) {
return false;
}
break;
}
case JSOP_GETELEM:
case JSOP_CALLELEM: {
ICStub* stub = alloc.newStub<ICGetElem_Fallback>(Kind::GetElem);
if (!addIC(loc, stub)) {
return false;
}
break;
}
case JSOP_GETELEM_SUPER: {
ICStub* stub = alloc.newStub<ICGetElem_Fallback>(Kind::GetElemSuper);
if (!addIC(loc, stub)) {
return false;
}
break;
}
case JSOP_IN: {
ICStub* stub = alloc.newStub<ICIn_Fallback>(Kind::In);
if (!addIC(loc, stub)) {
return false;
}
break;
}
case JSOP_HASOWN: {
ICStub* stub = alloc.newStub<ICHasOwn_Fallback>(Kind::HasOwn);
if (!addIC(loc, stub)) {
return false;
}
break;
}
case JSOP_GETNAME:
case JSOP_GETGNAME: {
ICStub* stub = alloc.newStub<ICGetName_Fallback>(Kind::GetName);
if (!addIC(loc, stub)) {
return false;
}
break;
}
case JSOP_BINDNAME:
case JSOP_BINDGNAME: {
ICStub* stub = alloc.newStub<ICBindName_Fallback>(Kind::BindName);
if (!addIC(loc, stub)) {
return false;
}
break;
}
case JSOP_GETALIASEDVAR:
case JSOP_GETIMPORT: {
ICStub* stub =
alloc.newStub<ICTypeMonitor_Fallback>(Kind::TypeMonitor, nullptr);
if (!addIC(loc, stub)) {
return false;
}
break;
}
case JSOP_GETINTRINSIC: {
ICStub* stub =
alloc.newStub<ICGetIntrinsic_Fallback>(Kind::GetIntrinsic);
if (!addIC(loc, stub)) {
return false;
}
break;
}
case JSOP_CALL:
case JSOP_CALL_IGNORES_RV:
case JSOP_CALLITER:
case JSOP_FUNCALL:
case JSOP_FUNAPPLY:
case JSOP_EVAL:
case JSOP_STRICTEVAL: {
ICStub* stub = alloc.newStub<ICCall_Fallback>(Kind::Call);
if (!addIC(loc, stub)) {
return false;
}
break;
}
case JSOP_SUPERCALL:
case JSOP_NEW: {
ICStub* stub = alloc.newStub<ICCall_Fallback>(Kind::CallConstructing);
if (!addIC(loc, stub)) {
return false;
}
break;
}
case JSOP_SPREADCALL:
case JSOP_SPREADEVAL:
case JSOP_STRICTSPREADEVAL: {
ICStub* stub = alloc.newStub<ICCall_Fallback>(Kind::SpreadCall);
if (!addIC(loc, stub)) {
return false;
}
break;
}
case JSOP_SPREADSUPERCALL:
case JSOP_SPREADNEW: {
ICStub* stub =
alloc.newStub<ICCall_Fallback>(Kind::SpreadCallConstructing);
if (!addIC(loc, stub)) {
return false;
}
break;
}
case JSOP_INSTANCEOF: {
ICStub* stub = alloc.newStub<ICInstanceOf_Fallback>(Kind::InstanceOf);
if (!addIC(loc, stub)) {
return false;
}
break;
}
case JSOP_TYPEOF:
case JSOP_TYPEOFEXPR: {
ICStub* stub = alloc.newStub<ICTypeOf_Fallback>(Kind::TypeOf);
if (!addIC(loc, stub)) {
return false;
}
break;
}
case JSOP_ITER: {
ICStub* stub = alloc.newStub<ICGetIterator_Fallback>(Kind::GetIterator);
if (!addIC(loc, stub)) {
return false;
}
break;
}
case JSOP_REST: {
ArrayObject* templateObject = ObjectGroup::newArrayObject(
cx, nullptr, 0, TenuredObject,
ObjectGroup::NewArrayKind::UnknownIndex);
if (!templateObject) {
return false;
}
ICStub* stub =
alloc.newStub<ICRest_Fallback>(Kind::Rest, templateObject);
if (!addIC(loc, stub)) {
return false;
}
break;
}
default:
MOZ_CRASH("JOF_IC op not handled");
}
}
// Assert all ICEntries and type map entries have been initialized.
MOZ_ASSERT(icEntryIndex == numICEntries());
MOZ_ASSERT(typeMapIndex == script->numBytecodeTypeSets());
return true;
}
ICStubConstIterator& ICStubConstIterator::operator++() {
MOZ_ASSERT(currentStub_ != nullptr);
currentStub_ = currentStub_->next();
return *this;
}
ICStubIterator::ICStubIterator(ICFallbackStub* fallbackStub, bool end)
: icEntry_(fallbackStub->icEntry()),
fallbackStub_(fallbackStub),
previousStub_(nullptr),
currentStub_(end ? fallbackStub : icEntry_->firstStub()),
unlinked_(false) {}
ICStubIterator& ICStubIterator::operator++() {
MOZ_ASSERT(currentStub_->next() != nullptr);
if (!unlinked_) {
previousStub_ = currentStub_;
}
currentStub_ = currentStub_->next();
unlinked_ = false;
return *this;
}
void ICStubIterator::unlink(JSContext* cx) {
MOZ_ASSERT(currentStub_->next() != nullptr);
MOZ_ASSERT(currentStub_ != fallbackStub_);
MOZ_ASSERT(!unlinked_);
fallbackStub_->unlinkStub(cx->zone(), previousStub_, currentStub_);
// Mark the current iterator position as unlinked, so operator++ works
// properly.
unlinked_ = true;
}
/* static */
bool ICStub::NonCacheIRStubMakesGCCalls(Kind kind) {
MOZ_ASSERT(IsValidKind(kind));
MOZ_ASSERT(!IsCacheIRKind(kind));
switch (kind) {
case Call_Fallback:
// These three fallback stubs don't actually make non-tail calls,
// but the fallback code for the bailout path needs to pop the stub frame
// pushed during the bailout.
case GetProp_Fallback:
case SetProp_Fallback:
case GetElem_Fallback:
return true;
default:
return false;
}
}
bool ICStub::makesGCCalls() const {
switch (kind()) {
case CacheIR_Regular:
return toCacheIR_Regular()->stubInfo()->makesGCCalls();
case CacheIR_Monitored:
return toCacheIR_Monitored()->stubInfo()->makesGCCalls();
case CacheIR_Updated:
return toCacheIR_Updated()->stubInfo()->makesGCCalls();
default:
return NonCacheIRStubMakesGCCalls(kind());
}
}
void ICStub::updateCode(JitCode* code) {
// Write barrier on the old code.
JitCode::writeBarrierPre(jitCode());
stubCode_ = code->raw();
}
/* static */
void ICStub::trace(JSTracer* trc) {
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
checkTraceMagic();
#endif
// Fallback stubs use runtime-wide trampoline code we don't need to trace.
if (!usesTrampolineCode()) {
JitCode* stubJitCode = jitCode();
TraceManuallyBarrieredEdge(trc, &stubJitCode, "baseline-ic-stub-code");
}
// If the stub is a monitored fallback stub, then trace the monitor ICs
// hanging off of that stub. We don't need to worry about the regular
// monitored stubs, because the regular monitored stubs will always have a
// monitored fallback stub that references the same stub chain.
if (isMonitoredFallback()) {
ICTypeMonitor_Fallback* lastMonStub =
toMonitoredFallbackStub()->maybeFallbackMonitorStub();
if (lastMonStub) {
for (ICStubConstIterator iter(lastMonStub->firstMonitorStub());
!iter.atEnd(); iter++) {
MOZ_ASSERT_IF(iter->next() == nullptr, *iter == lastMonStub);
iter->trace(trc);
}
}
}
if (isUpdated()) {
for (ICStubConstIterator iter(toUpdatedStub()->firstUpdateStub());
!iter.atEnd(); iter++) {
MOZ_ASSERT_IF(iter->next() == nullptr, iter->isTypeUpdate_Fallback());
iter->trace(trc);
}
}
switch (kind()) {
case ICStub::TypeMonitor_SingleObject: {
ICTypeMonitor_SingleObject* monitorStub = toTypeMonitor_SingleObject();
TraceEdge(trc, &monitorStub->object(), "baseline-monitor-singleton");
break;
}
case ICStub::TypeMonitor_ObjectGroup: {
ICTypeMonitor_ObjectGroup* monitorStub = toTypeMonitor_ObjectGroup();
TraceEdge(trc, &monitorStub->group(), "baseline-monitor-group");
break;
}
case ICStub::TypeUpdate_SingleObject: {
ICTypeUpdate_SingleObject* updateStub = toTypeUpdate_SingleObject();
TraceEdge(trc, &updateStub->object(), "baseline-update-singleton");
break;
}
case ICStub::TypeUpdate_ObjectGroup: {
ICTypeUpdate_ObjectGroup* updateStub = toTypeUpdate_ObjectGroup();
TraceEdge(trc, &updateStub->group(), "baseline-update-group");
break;
}
case ICStub::NewArray_Fallback: {
ICNewArray_Fallback* stub = toNewArray_Fallback();
TraceNullableEdge(trc, &stub->templateObject(),
"baseline-newarray-template");
TraceEdge(trc, &stub->templateGroup(),
"baseline-newarray-template-group");
break;
}
case ICStub::NewObject_Fallback: {
ICNewObject_Fallback* stub = toNewObject_Fallback();
TraceNullableEdge(trc, &stub->templateObject(),
"baseline-newobject-template");
break;
}
case ICStub::Rest_Fallback: {
ICRest_Fallback* stub = toRest_Fallback();
TraceEdge(trc, &stub->templateObject(), "baseline-rest-template");
break;
}
case ICStub::CacheIR_Regular:
TraceCacheIRStub(trc, this, toCacheIR_Regular()->stubInfo());
break;
case ICStub::CacheIR_Monitored:
TraceCacheIRStub(trc, this, toCacheIR_Monitored()->stubInfo());
break;
case ICStub::CacheIR_Updated: {
ICCacheIR_Updated* stub = toCacheIR_Updated();
TraceNullableEdge(trc, &stub->updateStubGroup(),
"baseline-update-stub-group");
TraceEdge(trc, &stub->updateStubId(), "baseline-update-stub-id");
TraceCacheIRStub(trc, this, stub->stubInfo());
break;
}
default:
break;
}
}
// This helper handles ICState updates/transitions while attaching CacheIR
// stubs.
template <typename IRGenerator, typename... Args>
static void TryAttachStub(const char* name, JSContext* cx, BaselineFrame* frame,
ICFallbackStub* stub, BaselineCacheIRStubKind kind,
Args&&... args) {
if (stub->state().maybeTransition()) {
stub->discardStubs(cx);
}
if (stub->state().canAttachStub()) {
RootedScript script(cx, frame->script());
jsbytecode* pc = stub->icEntry()->pc(script);
bool attached = false;
IRGenerator gen(cx, script, pc, stub->state().mode(),
std::forward<Args>(args)...);
switch (gen.tryAttachStub()) {
case AttachDecision::Attach: {
ICStub* newStub =
AttachBaselineCacheIRStub(cx, gen.writerRef(), gen.cacheKind(),
kind, script, stub, &attached);
if (newStub) {
JitSpew(JitSpew_BaselineIC, " Attached %s CacheIR stub", name);
}
} break;
case AttachDecision::NoAction:
break;
case AttachDecision::TemporarilyUnoptimizable:
case AttachDecision::Deferred:
MOZ_ASSERT_UNREACHABLE("Not expected in generic TryAttachStub");
break;
}
if (!attached) {
stub->state().trackNotAttached();
}
}
}
void ICFallbackStub::unlinkStub(Zone* zone, ICStub* prev, ICStub* stub) {
MOZ_ASSERT(stub->next());
// If stub is the last optimized stub, update lastStubPtrAddr.
if (stub->next() == this) {
MOZ_ASSERT(lastStubPtrAddr_ == stub->addressOfNext());
if (prev) {
lastStubPtrAddr_ = prev->addressOfNext();
} else {
lastStubPtrAddr_ = icEntry()->addressOfFirstStub();
}
*lastStubPtrAddr_ = this;
} else {
if (prev) {
MOZ_ASSERT(prev->next() == stub);
prev->setNext(stub->next());
} else {
MOZ_ASSERT(icEntry()->firstStub() == stub);
icEntry()->setFirstStub(stub->next());
}
}
state_.trackUnlinkedStub();
if (zone->needsIncrementalBarrier()) {
// We are removing edges from ICStub to gcthings. Perform one final trace
// of the stub for incremental GC, as it must know about those edges.
stub->trace(zone->barrierTracer());
}
if (stub->makesGCCalls() && stub->isMonitored()) {
// This stub can make calls so we can return to it if it's on the stack.
// We just have to reset its firstMonitorStub_ field to avoid a stale
// pointer when purgeOptimizedStubs destroys all optimized monitor
// stubs (unlinked stubs won't be updated).
ICTypeMonitor_Fallback* monitorFallback =
toMonitoredFallbackStub()->maybeFallbackMonitorStub();
MOZ_ASSERT(monitorFallback);
stub->toMonitoredStub()->resetFirstMonitorStub(monitorFallback);
}
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
stub->checkTraceMagic();
#endif
#ifdef DEBUG
// Poison stub code to ensure we don't call this stub again. However, if
// this stub can make calls, a pointer to it may be stored in a stub frame
// on the stack, so we can't touch the stubCode_ or GC will crash when
// tracing this pointer.
if (!stub->makesGCCalls()) {
stub->stubCode_ = (uint8_t*)0xbad;
}
#endif
}
void ICFallbackStub::unlinkStubsWithKind(JSContext* cx, ICStub::Kind kind) {
for (ICStubIterator iter = beginChain(); !iter.atEnd(); iter++) {
if (iter->kind() == kind) {
iter.unlink(cx);
}
}
}
void ICFallbackStub::discardStubs(JSContext* cx) {
for (ICStubIterator iter = beginChain(); !iter.atEnd(); iter++) {
iter.unlink(cx);
}
}
void ICTypeMonitor_Fallback::resetMonitorStubChain(Zone* zone) {
if (zone->needsIncrementalBarrier()) {
// We are removing edges from monitored stubs to gcthings (JitCode).
// Perform one final trace of all monitor stubs for incremental GC,
// as it must know about those edges.
for (ICStub* s = firstMonitorStub_; !s->isTypeMonitor_Fallback();
s = s->next()) {
s->trace(zone->barrierTracer());
}
}
firstMonitorStub_ = this;
numOptimizedMonitorStubs_ = 0;
if (hasFallbackStub_) {
lastMonitorStubPtrAddr_ = nullptr;
// Reset firstMonitorStub_ field of all monitored stubs.
for (ICStubConstIterator iter = mainFallbackStub_->beginChainConst();
!iter.atEnd(); iter++) {
if (!iter->isMonitored()) {
continue;
}
iter->toMonitoredStub()->resetFirstMonitorStub(this);
}
} else {
icEntry_->setFirstStub(this);
lastMonitorStubPtrAddr_ = icEntry_->addressOfFirstStub();
}
}
void ICCacheIR_Updated::resetUpdateStubChain(Zone* zone) {
while (!firstUpdateStub_->isTypeUpdate_Fallback()) {
if (zone->needsIncrementalBarrier()) {
// We are removing edges from update stubs to gcthings (JitCode).
// Perform one final trace of all update stubs for incremental GC,
// as it must know about those edges.
firstUpdateStub_->trace(zone->barrierTracer());
}
firstUpdateStub_ = firstUpdateStub_->next();
}
numOptimizedStubs_ = 0;
}
ICMonitoredStub::ICMonitoredStub(Kind kind, JitCode* stubCode,
ICStub* firstMonitorStub)
: ICStub(kind, ICStub::Monitored, stubCode),
firstMonitorStub_(firstMonitorStub) {
// In order to silence Coverity - null pointer dereference checker
MOZ_ASSERT(firstMonitorStub_);
// If the first monitored stub is a ICTypeMonitor_Fallback stub, then
// double check that _its_ firstMonitorStub is the same as this one.
MOZ_ASSERT_IF(
firstMonitorStub_->isTypeMonitor_Fallback(),
firstMonitorStub_->toTypeMonitor_Fallback()->firstMonitorStub() ==
firstMonitorStub_);
}
bool ICMonitoredFallbackStub::initMonitoringChain(JSContext* cx,
JSScript* script) {
MOZ_ASSERT(fallbackMonitorStub_ == nullptr);
ICStubSpace* space = script->jitScript()->fallbackStubSpace();
FallbackStubAllocator alloc(cx, *space);
auto* stub = alloc.newStub<ICTypeMonitor_Fallback>(
BaselineICFallbackKind::TypeMonitor, this);
if (!stub) {
return false;
}
fallbackMonitorStub_ = stub;
return true;
}
static void TypeMonitorMagicValue(JSContext* cx, ICTypeMonitor_Fallback* stub,
JSScript* script, jsbytecode* pc,
HandleValue value) {
MOZ_ASSERT(value.isMagic());
// It's possible that we arrived here from bailing out of Ion, and that
// Ion proved that the value is dead and optimized out. In such cases,
// do nothing. However, it's also possible that we have an uninitialized
// this, in which case we should not look for other magic values.
if (value.whyMagic() == JS_OPTIMIZED_OUT) {
MOZ_ASSERT(!stub->monitorsThis());
return;
}
// In derived class constructors (including nested arrows/eval), the
// |this| argument or GETALIASEDVAR can return the magic TDZ value.
MOZ_ASSERT(value.whyMagic() == JS_UNINITIALIZED_LEXICAL);
MOZ_ASSERT(script->function() || script->isForEval());
MOZ_ASSERT(stub->monitorsThis() || *GetNextPc(pc) == JSOP_CHECKTHIS ||
*GetNextPc(pc) == JSOP_CHECKTHISREINIT ||
*GetNextPc(pc) == JSOP_CHECKRETURN);
if (stub->monitorsThis()) {
JitScript::MonitorThisType(cx, script, TypeSet::UnknownType());
} else {
JitScript::MonitorBytecodeType(cx, script, pc, TypeSet::UnknownType());
}
}
bool TypeMonitorResult(JSContext* cx, ICMonitoredFallbackStub* stub,
BaselineFrame* frame, HandleScript script,
jsbytecode* pc, HandleValue val) {
ICTypeMonitor_Fallback* typeMonitorFallback =
stub->getFallbackMonitorStub(cx, script);
if (!typeMonitorFallback) {
return false;
}
if (MOZ_UNLIKELY(val.isMagic())) {
TypeMonitorMagicValue(cx, typeMonitorFallback, script, pc, val);
return true;
}
AutoSweepJitScript sweep(script);
StackTypeSet* types = script->jitScript()->bytecodeTypes(sweep, script, pc);
JitScript::MonitorBytecodeType(cx, script, pc, types, val);
return typeMonitorFallback->addMonitorStubForValue(cx, frame, types, val);
}
bool ICCacheIR_Updated::initUpdatingChain(JSContext* cx, ICStubSpace* space) {
MOZ_ASSERT(firstUpdateStub_ == nullptr);
FallbackStubAllocator alloc(cx, *space);
auto* stub =
alloc.newStub<ICTypeUpdate_Fallback>(BaselineICFallbackKind::TypeUpdate);
if (!stub) {
return false;
}
firstUpdateStub_ = stub;
return true;
}
/* static */
ICStubSpace* ICStubCompiler::StubSpaceForStub(bool makesGCCalls,
JSScript* script) {
if (makesGCCalls) {
return script->jitScript()->fallbackStubSpace();
}
return script->zone()->jitZone()->optimizedStubSpace();
}
static void InitMacroAssemblerForICStub(StackMacroAssembler& masm) {
#ifndef JS_USE_LINK_REGISTER
// The first value contains the return addres,
// which we pull into ICTailCallReg for tail calls.
masm.adjustFrame(sizeof(intptr_t));
#endif
#ifdef JS_CODEGEN_ARM
masm.setSecondScratchReg(BaselineSecondScratchReg);
#endif
}
JitCode* ICStubCompiler::getStubCode() {
JitRealm* realm = cx->realm()->jitRealm();
// Check for existing cached stubcode.
uint32_t stubKey = getKey();
JitCode* stubCode = realm->getStubCode(stubKey);
if (stubCode) {
return stubCode;
}
// Compile new stubcode.
JitContext jctx(cx, nullptr);
StackMacroAssembler masm;
InitMacroAssemblerForICStub(masm);
if (!generateStubCode(masm)) {
return nullptr;
}
Linker linker(masm);
Rooted<JitCode*> newStubCode(cx, linker.newCode(cx, CodeKind::Baseline));
if (!newStubCode) {
return nullptr;
}
// Cache newly compiled stubcode.
if (!realm->putStubCode(cx, stubKey, newStubCode)) {
return nullptr;
}
MOZ_ASSERT(entersStubFrame_ == ICStub::NonCacheIRStubMakesGCCalls(kind));
MOZ_ASSERT(!inStubFrame_);
#ifdef JS_ION_PERF
writePerfSpewerJitCodeProfile(newStubCode, "BaselineIC");
#endif
return newStubCode;
}