-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathGlobOptFields.cpp
2022 lines (1775 loc) · 72.9 KB
/
GlobOptFields.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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "Backend.h"
bool
GlobOpt::DoFieldCopyProp() const
{
BasicBlock *block = this->currentBlock;
Loop *loop = block->loop;
if (this->isRecursiveCallOnLandingPad)
{
// The landing pad at this point only contains load hosted by PRE.
// These need to be copy-prop'd into the loop.
// We want to look at the implicit-call info of the loop, not it's parent.
Assert(block->IsLandingPad());
loop = block->next->loop;
Assert(loop);
}
return DoFieldCopyProp(loop);
}
bool
GlobOpt::DoFunctionFieldCopyProp() const
{
return DoFieldCopyProp(nullptr);
}
bool
GlobOpt::DoFieldCopyProp(Loop * loop) const
{
if (PHASE_OFF(Js::CopyPropPhase, this->func))
{
// Can't do field copy prop without copy prop
return false;
}
if (PHASE_FORCE(Js::FieldCopyPropPhase, this->func))
{
// Force always turns on field copy prop
return true;
}
if (PHASE_OFF(Js::FieldCopyPropPhase, this->func))
{
return false;
}
return this->DoFieldOpts(loop);
}
bool
GlobOpt::DoObjTypeSpec() const
{
return this->DoObjTypeSpec(this->currentBlock->loop);
}
bool
GlobOpt::DoObjTypeSpec(Loop *loop) const
{
if (!this->func->DoFastPaths())
{
return false;
}
if (PHASE_FORCE(Js::ObjTypeSpecPhase, this->func))
{
return true;
}
if (PHASE_OFF(Js::ObjTypeSpecPhase, this->func))
{
return false;
}
if (this->func->IsLoopBody() && this->func->HasProfileInfo() && this->func->GetReadOnlyProfileInfo()->IsObjTypeSpecDisabledInJitLoopBody())
{
return false;
}
if (this->ImplicitCallFlagsAllowOpts(this->func))
{
Assert(loop == nullptr || loop->CanDoFieldCopyProp());
return true;
}
return loop != nullptr && loop->CanDoFieldCopyProp();
}
bool
GlobOpt::DoFieldOpts(Loop * loop) const
{
if (this->ImplicitCallFlagsAllowOpts(this->func))
{
Assert(loop == nullptr || loop->CanDoFieldCopyProp());
return true;
}
return loop != nullptr && loop->CanDoFieldCopyProp();
}
bool GlobOpt::DoFieldPRE() const
{
Loop *loop = this->currentBlock->loop;
return DoFieldPRE(loop);
}
bool
GlobOpt::DoFieldPRE(Loop *loop) const
{
if (PHASE_OFF(Js::FieldPREPhase, this->func))
{
return false;
}
if (PHASE_FORCE(Js::FieldPREPhase, func))
{
// Force always turns on field PRE
return true;
}
if (this->func->HasProfileInfo() && this->func->GetReadOnlyProfileInfo()->IsFieldPREDisabled())
{
return false;
}
return DoFieldOpts(loop);
}
bool GlobOpt::HasMemOp(Loop *loop)
{
#pragma prefast(suppress: 6285, "logical-or of constants is by design")
return (
loop &&
loop->doMemOp &&
(
!PHASE_OFF(Js::MemSetPhase, this->func) ||
!PHASE_OFF(Js::MemCopyPhase, this->func)
) &&
loop->memOpInfo &&
loop->memOpInfo->candidates &&
!loop->memOpInfo->candidates->Empty()
);
}
void
GlobOpt::KillLiveFields(StackSym * stackSym, BVSparse<JitArenaAllocator> * bv)
{
if (stackSym->IsTypeSpec())
{
stackSym = stackSym->GetVarEquivSym(this->func);
}
Assert(stackSym);
// If the sym has no objectSymInfo, it must not represent an object and, hence, has no type sym or
// property syms to kill.
if (!stackSym->HasObjectInfo() || stackSym->IsSingleDef())
{
return;
}
// Note that the m_writeGuardSym is killed here as well, because it is part of the
// m_propertySymList of the object.
ObjectSymInfo * objectSymInfo = stackSym->GetObjectInfo();
PropertySym * propertySym = objectSymInfo->m_propertySymList;
while (propertySym != nullptr)
{
Assert(propertySym->m_stackSym == stackSym);
bv->Clear(propertySym->m_id);
if (this->IsLoopPrePass())
{
for (Loop * loop = this->rootLoopPrePass; loop != nullptr; loop = loop->parent)
{
loop->fieldKilled->Set(propertySym->m_id);
}
}
else if (bv->IsEmpty())
{
// shortcut
break;
}
propertySym = propertySym->m_nextInStackSymList;
}
this->KillObjectType(stackSym, bv);
}
void
GlobOpt::KillLiveFields(PropertySym * propertySym, BVSparse<JitArenaAllocator> * bv)
{
KillLiveFields(propertySym->m_propertyEquivSet, bv);
}
void GlobOpt::KillLiveFields(BVSparse<JitArenaAllocator> *const fieldsToKill, BVSparse<JitArenaAllocator> *const bv) const
{
Assert(bv);
if (fieldsToKill)
{
bv->Minus(fieldsToKill);
if (this->IsLoopPrePass())
{
for (Loop * loop = this->rootLoopPrePass; loop != nullptr; loop = loop->parent)
{
loop->fieldKilled->Or(fieldsToKill);
}
}
}
}
void
GlobOpt::KillLiveElems(IR::IndirOpnd * indirOpnd, BVSparse<JitArenaAllocator> * bv, bool inGlobOpt, Func *func)
{
IR::RegOpnd *indexOpnd = indirOpnd->GetIndexOpnd();
// obj.x = 10;
// obj["x"] = ...; // This needs to kill obj.x... We need to kill all fields...
//
// Also, 'arguments[i] =' needs to kill all slots even if 'i' is an int.
//
// NOTE: we only need to kill slots here, not all fields. It may be good to separate these one day.
//
// Regarding the check for type specialization:
// - Type specialization does not always update the value to a definite type.
// - The loop prepass is conservative on values even when type specialization occurs.
// - We check the type specialization status for the sym as well. For the purpose of doing kills, we can assume that
// if type specialization happened, that fields don't need to be killed. Note that they may be killed in the next
// pass based on the value.
if (func->GetThisOrParentInlinerHasArguments() ||
(
indexOpnd &&
(
indexOpnd->m_sym->m_isNotNumber ||
(inGlobOpt && !indexOpnd->GetValueType().IsNumber() && !currentBlock->globOptData.IsTypeSpecialized(indexOpnd->m_sym))
)
))
{
this->KillAllFields(bv); // This also kills all property type values, as the same bit-vector tracks those stack syms
SetAnyPropertyMayBeWrittenTo();
}
}
void
GlobOpt::KillAllFields(BVSparse<JitArenaAllocator> * bv)
{
bv->And(func->GetStableSlotSyms());
if (this->IsLoopPrePass())
{
for (Loop * loop = this->rootLoopPrePass; loop != nullptr; loop = loop->parent)
{
loop->allFieldsKilled = true;
}
}
}
void
GlobOpt::SetAnyPropertyMayBeWrittenTo()
{
this->func->anyPropertyMayBeWrittenTo = true;
}
void
GlobOpt::AddToPropertiesWrittenTo(Js::PropertyId propertyId)
{
this->func->EnsurePropertiesWrittenTo();
this->func->propertiesWrittenTo->Item(propertyId);
}
void
GlobOpt::ProcessFieldKills(IR::Instr *instr, BVSparse<JitArenaAllocator> *bv, bool inGlobOpt)
{
if (bv->IsEmpty() && (!this->IsLoopPrePass() || this->rootLoopPrePass->allFieldsKilled))
{
return;
}
if (instr->m_opcode == Js::OpCode::FromVar || instr->m_opcode == Js::OpCode::Conv_Prim)
{
return;
}
IR::Opnd * dstOpnd = instr->GetDst();
if (dstOpnd)
{
if (dstOpnd->IsRegOpnd())
{
Sym * sym = dstOpnd->AsRegOpnd()->m_sym;
if (sym->IsStackSym())
{
KillLiveFields(sym->AsStackSym(), bv);
}
}
else if (dstOpnd->IsSymOpnd())
{
Sym * sym = dstOpnd->AsSymOpnd()->m_sym;
if (sym->IsStackSym())
{
KillLiveFields(sym->AsStackSym(), bv);
}
else
{
Assert(sym->IsPropertySym());
if (instr->m_opcode == Js::OpCode::InitLetFld || instr->m_opcode == Js::OpCode::InitConstFld || instr->m_opcode == Js::OpCode::InitFld)
{
// These can grow the aux slot of the activation object.
// We need to kill the slot array sym as well.
PropertySym * slotArraySym = PropertySym::Find(sym->AsPropertySym()->m_stackSym->m_id,
(Js::DynamicObject::GetOffsetOfAuxSlots())/sizeof(Js::Var) /*, PropertyKindSlotArray */, instr->m_func);
if (slotArraySym)
{
bv->Clear(slotArraySym->m_id);
}
}
}
}
}
if (bv->IsEmpty() && (!this->IsLoopPrePass() || this->rootLoopPrePass->allFieldsKilled))
{
return;
}
Sym *sym;
IR::JnHelperMethod fnHelper;
switch(instr->m_opcode)
{
case Js::OpCode::StElemI_A:
case Js::OpCode::StElemI_A_Strict:
Assert(dstOpnd != nullptr);
KillLiveFields(this->lengthEquivBv, bv);
KillLiveElems(dstOpnd->AsIndirOpnd(), bv, inGlobOpt, instr->m_func);
break;
case Js::OpCode::InitComputedProperty:
KillLiveElems(dstOpnd->AsIndirOpnd(), bv, inGlobOpt, instr->m_func);
break;
case Js::OpCode::DeleteElemI_A:
case Js::OpCode::DeleteElemIStrict_A:
Assert(dstOpnd != nullptr);
KillLiveElems(instr->GetSrc1()->AsIndirOpnd(), bv, inGlobOpt, instr->m_func);
break;
case Js::OpCode::DeleteFld:
case Js::OpCode::DeleteRootFld:
case Js::OpCode::DeleteFldStrict:
case Js::OpCode::DeleteRootFldStrict:
sym = instr->GetSrc1()->AsSymOpnd()->m_sym;
KillLiveFields(sym->AsPropertySym(), bv);
if (inGlobOpt)
{
AddToPropertiesWrittenTo(sym->AsPropertySym()->m_propertyId);
this->KillAllObjectTypes(bv);
}
break;
case Js::OpCode::InitSetFld:
case Js::OpCode::InitGetFld:
case Js::OpCode::InitClassMemberGet:
case Js::OpCode::InitClassMemberSet:
sym = instr->GetDst()->AsSymOpnd()->m_sym;
KillLiveFields(sym->AsPropertySym(), bv);
if (inGlobOpt)
{
AddToPropertiesWrittenTo(sym->AsPropertySym()->m_propertyId);
this->KillAllObjectTypes(bv);
}
break;
case Js::OpCode::InitFld:
case Js::OpCode::StFld:
case Js::OpCode::StRootFld:
case Js::OpCode::StFldStrict:
case Js::OpCode::StRootFldStrict:
case Js::OpCode::StSlot:
case Js::OpCode::StSlotChkUndecl:
Assert(dstOpnd != nullptr);
sym = dstOpnd->AsSymOpnd()->m_sym;
if (inGlobOpt)
{
AddToPropertiesWrittenTo(sym->AsPropertySym()->m_propertyId);
}
if ((inGlobOpt && (sym->AsPropertySym()->m_propertyId == Js::PropertyIds::valueOf || sym->AsPropertySym()->m_propertyId == Js::PropertyIds::toString)) ||
instr->CallsAccessor())
{
// If overriding valueof/tostring, we might have expected a previous LdFld to bailout on implicitCalls but didn't.
// CSE's for example would have expected a bailout. Clear all fields to prevent optimizing across.
this->KillAllFields(bv);
}
else
{
KillLiveFields(sym->AsPropertySym(), bv);
}
break;
case Js::OpCode::InlineArrayPush:
case Js::OpCode::InlineArrayPop:
KillLiveFields(this->lengthEquivBv, bv);
break;
case Js::OpCode::InlineeStart:
case Js::OpCode::InlineeEnd:
Assert(!instr->UsesAllFields());
// Kill all live 'arguments' and 'caller' fields, as 'inlineeFunction.arguments' and 'inlineeFunction.caller'
// cannot be copy-propped across different instances of the same inlined function.
KillLiveFields(argumentsEquivBv, bv);
KillLiveFields(callerEquivBv, bv);
break;
case Js::OpCode::CallDirect:
fnHelper = instr->GetSrc1()->AsHelperCallOpnd()->m_fnHelper;
// Kill length field for built-ins that can update it.
if(nullptr != this->lengthEquivBv && (fnHelper == IR::JnHelperMethod::HelperArray_Shift || fnHelper == IR::JnHelperMethod::HelperArray_Splice
|| fnHelper == IR::JnHelperMethod::HelperArray_Unshift))
{
KillLiveFields(this->lengthEquivBv, bv);
}
if ((fnHelper == IR::JnHelperMethod::HelperRegExp_Exec)
|| (fnHelper == IR::JnHelperMethod::HelperString_Match)
|| (fnHelper == IR::JnHelperMethod::HelperString_Replace))
{
// Consider: We may not need to kill all fields here.
this->KillAllFields(bv);
}
break;
case Js::OpCode::LdHeapArguments:
case Js::OpCode::LdLetHeapArguments:
case Js::OpCode::LdHeapArgsCached:
case Js::OpCode::LdLetHeapArgsCached:
if (inGlobOpt) {
this->KillLiveFields(this->slotSyms, bv);
}
break;
default:
if (instr->UsesAllFields())
{
// This also kills all property type values, as the same bit-vector tracks those stack syms.
this->KillAllFields(bv);
}
break;
}
}
void
GlobOpt::ProcessFieldKills(IR::Instr * instr)
{
if (this->currentBlock->globOptData.liveFields->IsEmpty())
{
return;
}
ProcessFieldKills(instr, this->currentBlock->globOptData.liveFields, true);
}
Value *
GlobOpt::CreateFieldSrcValue(PropertySym * sym, PropertySym * originalSym, IR::Opnd ** ppOpnd, IR::Instr * instr)
{
#if DBG
// If the opcode going to kill all field values immediate anyway, we shouldn't be giving it a value
Assert(!instr->UsesAllFields());
AssertCanCopyPropOrCSEFieldLoad(instr);
Assert(instr->GetSrc1() == *ppOpnd);
#endif
// Only give a value to fields if we are doing field copy prop.
// Consider: We should always copy prop local slots, but the only use right now is LdSlot from jit loop body.
// This should have one onus load, and thus no need for copy prop of field itself. We may want to support
// copy prop LdSlot if there are other uses of local slots
if (!this->DoFieldCopyProp())
{
return nullptr;
}
BOOL wasLive = this->currentBlock->globOptData.liveFields->TestAndSet(sym->m_id);
if (sym != originalSym)
{
this->currentBlock->globOptData.liveFields->TestAndSet(originalSym->m_id);
}
if (!wasLive)
{
// We don't clear the value when we kill the field.
// Clear it to make sure we don't use the old value.
this->currentBlock->globOptData.ClearSymValue(sym);
this->currentBlock->globOptData.ClearSymValue(originalSym);
}
Assert((*ppOpnd)->AsSymOpnd()->m_sym == sym || this->IsLoopPrePass());
// We don't use the sym store to do copy prop on hoisted fields, but create a value
// in case it can be copy prop out of the loop.
return this->NewGenericValue(ValueType::Uninitialized, *ppOpnd);
}
bool
GlobOpt::NeedBailOnImplicitCallWithFieldOpts(Loop *loop, bool hasLiveFields) const
{
if (!(((this->DoFieldRefOpts(loop) ||
this->DoFieldCopyProp(loop)) &&
hasLiveFields)))
{
return false;
}
return true;
}
IR::Instr *
GlobOpt::EnsureDisableImplicitCallRegion(Loop * loop)
{
Assert(loop->bailOutInfo != nullptr);
IR::Instr * endDisableImplicitCall = loop->endDisableImplicitCall;
if (endDisableImplicitCall)
{
return endDisableImplicitCall;
}
IR::Instr * bailOutTarget = EnsureBailTarget(loop);
Func * bailOutFunc = loop->GetFunc();
Assert(loop->bailOutInfo->bailOutFunc == bailOutFunc);
IR::MemRefOpnd * disableImplicitCallAddress = IR::MemRefOpnd::New(this->func->GetThreadContextInfo()->GetDisableImplicitFlagsAddr(), TyInt8, bailOutFunc);
IR::IntConstOpnd * disableImplicitCallAndExceptionValue = IR::IntConstOpnd::New(DisableImplicitCallAndExceptionFlag, TyInt8, bailOutFunc, true);
IR::IntConstOpnd * enableImplicitCallAndExceptionValue = IR::IntConstOpnd::New(DisableImplicitNoFlag, TyInt8, bailOutFunc, true);
IR::Opnd * implicitCallFlags = Lowerer::GetImplicitCallFlagsOpnd(bailOutFunc);
IR::IntConstOpnd * noImplicitCall = IR::IntConstOpnd::New(Js::ImplicitCall_None, TyInt8, bailOutFunc, true);
// Consider: if we are already doing implicit call in the outer loop, we don't need to clear the implicit call bit again
IR::Instr * clearImplicitCall = IR::Instr::New(Js::OpCode::Ld_A, implicitCallFlags, noImplicitCall, bailOutFunc);
bailOutTarget->InsertBefore(clearImplicitCall);
IR::Instr * disableImplicitCall = IR::Instr::New(Js::OpCode::Ld_A, disableImplicitCallAddress, disableImplicitCallAndExceptionValue, bailOutFunc);
bailOutTarget->InsertBefore(disableImplicitCall);
endDisableImplicitCall = IR::Instr::New(Js::OpCode::Ld_A, disableImplicitCallAddress, enableImplicitCallAndExceptionValue, bailOutFunc);
bailOutTarget->InsertBefore(endDisableImplicitCall);
IR::BailOutInstr * bailOutInstr = IR::BailOutInstr::New(Js::OpCode::BailOnNotEqual, IR::BailOutOnImplicitCalls, loop->bailOutInfo, loop->bailOutInfo->bailOutFunc);
bailOutInstr->SetSrc1(implicitCallFlags);
bailOutInstr->SetSrc2(noImplicitCall);
bailOutTarget->InsertBefore(bailOutInstr);
loop->endDisableImplicitCall = endDisableImplicitCall;
return endDisableImplicitCall;
}
#if DBG
bool
GlobOpt::IsPropertySymId(SymID symId) const
{
return this->func->m_symTable->Find(symId)->IsPropertySym();
}
void
GlobOpt::AssertCanCopyPropOrCSEFieldLoad(IR::Instr * instr)
{
// Consider: Hoisting LdRootFld may have complication with exception if the field doesn't exist.
// We need to have another opcode for the hoisted version to avoid the exception and bailout.
Assert(instr->m_opcode == Js::OpCode::LdSlot || instr->m_opcode == Js::OpCode::LdSlotArr
|| instr->m_opcode == Js::OpCode::LdFld || instr->m_opcode == Js::OpCode::LdFldForCallApplyTarget
|| instr->m_opcode == Js::OpCode::LdLen_A
|| instr->m_opcode == Js::OpCode::LdRootFld || instr->m_opcode == Js::OpCode::LdSuperFld
|| instr->m_opcode == Js::OpCode::LdFldForTypeOf || instr->m_opcode == Js::OpCode::LdRootFldForTypeOf
|| instr->m_opcode == Js::OpCode::LdMethodFld || instr->m_opcode == Js::OpCode::LdMethodFldPolyInlineMiss
|| instr->m_opcode == Js::OpCode::LdRootMethodFld
|| instr->m_opcode == Js::OpCode::LdMethodFromFlags
|| instr->m_opcode == Js::OpCode::ScopedLdMethodFld
|| instr->m_opcode == Js::OpCode::CheckFixedFld
|| instr->m_opcode == Js::OpCode::CheckPropertyGuardAndLoadType
|| instr->m_opcode == Js::OpCode::ScopedLdFld
|| instr->m_opcode == Js::OpCode::ScopedLdFldForTypeOf);
Assert(instr->m_opcode == Js::OpCode::CheckFixedFld || instr->GetDst()->GetType() == TyVar || instr->m_func->GetJITFunctionBody()->IsAsmJsMode());
Assert(instr->GetSrc1()->GetType() == TyVar || instr->m_func->GetJITFunctionBody()->IsAsmJsMode());
Assert(instr->GetSrc1()->AsSymOpnd()->m_sym->IsPropertySym());
Assert(instr->GetSrc2() == nullptr);
}
#endif
StackSym *
GlobOpt::EnsureObjectTypeSym(StackSym * objectSym)
{
Assert(!objectSym->IsTypeSpec());
objectSym->EnsureObjectInfo(this->func);
if (objectSym->HasObjectTypeSym())
{
Assert(this->objectTypeSyms);
return objectSym->GetObjectTypeSym();
}
if (this->objectTypeSyms == nullptr)
{
this->objectTypeSyms = JitAnew(this->alloc, BVSparse<JitArenaAllocator>, this->alloc);
}
StackSym * typeSym = StackSym::New(TyVar, this->func);
objectSym->GetObjectInfo()->m_typeSym = typeSym;
this->objectTypeSyms->Set(typeSym->m_id);
return typeSym;
}
PropertySym *
GlobOpt::EnsurePropertyWriteGuardSym(PropertySym * propertySym)
{
// Make sure that the PropertySym has a proto cache sym which is chained into the propertySym list.
if (!propertySym->m_writeGuardSym)
{
propertySym->m_writeGuardSym = PropertySym::New(propertySym->m_stackSym, propertySym->m_propertyId, (uint32)-1, (uint)-1, PropertyKindWriteGuard, this->func);
}
return propertySym->m_writeGuardSym;
}
void
GlobOpt::PreparePropertySymForTypeCheckSeq(PropertySym *propertySym)
{
Assert(!propertySym->m_stackSym->IsTypeSpec());
EnsureObjectTypeSym(propertySym->m_stackSym);
EnsurePropertyWriteGuardSym(propertySym);
}
bool
GlobOpt::IsPropertySymPreparedForTypeCheckSeq(PropertySym *propertySym)
{
Assert(!propertySym->m_stackSym->IsTypeSpec());
// The following doesn't need to be true. We may copy prop a constant into an object sym, which has
// previously been prepared for type check sequence optimization.
// Assert(!propertySym->m_stackSym->m_isIntConst || !propertySym->HasObjectTypeSym());
// The following doesn't need to be true. We may copy prop the object sym into a field load or store
// that doesn't have object type spec info and hence the operand wasn't prepared and doesn't have a write
// guard. The object sym, however, may have other field operations which are object type specialized and
// thus the type sym for it has been created.
// Assert(propertySym->HasObjectTypeSym() == propertySym->HasWriteGuardSym());
return propertySym->HasObjectTypeSym();
}
bool
GlobOpt::PreparePropertySymOpndForTypeCheckSeq(IR::PropertySymOpnd * propertySymOpnd, IR::Instr* instr, Loop * loop)
{
if (!DoFieldRefOpts(loop) || !OpCodeAttr::FastFldInstr(instr->m_opcode) || instr->CallsAccessor())
{
return false;
}
if (!propertySymOpnd->HasObjTypeSpecFldInfo())
{
return false;
}
ObjTypeSpecFldInfo* info = propertySymOpnd->GetObjTypeSpecInfo();
if (info->UsesAccessor() || info->IsRootObjectNonConfigurableFieldLoad())
{
return false;
}
if (info->IsPoly() && !info->GetEquivalentTypeSet())
{
return false;
}
PropertySym * propertySym = propertySymOpnd->m_sym->AsPropertySym();
PreparePropertySymForTypeCheckSeq(propertySym);
propertySymOpnd->SetTypeCheckSeqCandidate(true);
propertySymOpnd->SetIsBeingStored(propertySymOpnd == instr->GetDst());
return true;
}
bool
GlobOpt::CheckIfPropOpEmitsTypeCheck(IR::Instr *instr, IR::PropertySymOpnd *opnd)
{
if (!DoFieldRefOpts() || !OpCodeAttr::FastFldInstr(instr->m_opcode))
{
return false;
}
if (!opnd->IsTypeCheckSeqCandidate())
{
return false;
}
return CheckIfInstrInTypeCheckSeqEmitsTypeCheck(instr, opnd);
}
IR::PropertySymOpnd *
GlobOpt::CreateOpndForTypeCheckOnly(IR::PropertySymOpnd* opnd, Func* func)
{
// Used only for CheckObjType instruction today. Future users should make a call
// whether the new operand is jit optimized in their scenario or not.
Assert(!opnd->IsRootObjectNonConfigurableFieldLoad());
IR::PropertySymOpnd *newOpnd = opnd->CopyCommon(func);
newOpnd->SetObjTypeSpecFldInfo(opnd->GetObjTypeSpecInfo());
newOpnd->SetUsesAuxSlot(opnd->UsesAuxSlot());
newOpnd->SetSlotIndex(opnd->GetSlotIndex());
newOpnd->objTypeSpecFlags = opnd->objTypeSpecFlags;
// If we're turning the instruction owning this operand into a CheckObjType, we will do a type check here
// only for the sake of downstream instructions, so the flags pertaining to this property access are
// irrelevant, because we don't do a property access here.
newOpnd->SetTypeCheckOnly(true);
newOpnd->usesFixedValue = false;
newOpnd->finalType = opnd->finalType;
newOpnd->guardedPropOps = opnd->guardedPropOps != nullptr ? opnd->guardedPropOps->CopyNew() : nullptr;
newOpnd->writeGuards = opnd->writeGuards != nullptr ? opnd->writeGuards->CopyNew() : nullptr;
newOpnd->SetIsJITOptimizedReg(true);
return newOpnd;
}
bool
GlobOpt::FinishOptPropOp(IR::Instr *instr, IR::PropertySymOpnd *opnd, BasicBlock* block, bool updateExistingValue, bool* emitsTypeCheckOut, bool* changesTypeValueOut)
{
if (!DoFieldRefOpts() || !OpCodeAttr::FastFldInstr(instr->m_opcode))
{
return false;
}
bool isTypeCheckSeqCandidate = opnd->IsTypeCheckSeqCandidate();
bool isObjTypeSpecialized = false;
bool isObjTypeChecked = false;
if (isTypeCheckSeqCandidate)
{
isObjTypeSpecialized = ProcessPropOpInTypeCheckSeq<true>(instr, opnd, block, updateExistingValue, emitsTypeCheckOut, changesTypeValueOut, &isObjTypeChecked);
}
if (opnd == instr->GetDst() && this->objectTypeSyms)
{
if (block == nullptr)
{
block = this->currentBlock;
}
// This is a property store that may change the layout of the object that it stores to. This means that
// it may change any aliased object. Do two things to address this:
// - Add all object types in this function to the set that may have had a property added. This will prevent
// final type optimization across this instruction. (Only needed here for non-specialized stores.)
// - Kill all type symbols that currently hold object-header-inlined types. Any of them may have their layout
// changed by the addition of a property.
SymID opndId = opnd->HasObjectTypeSym() ? opnd->GetObjectTypeSym()->m_id : -1;
if (!isObjTypeChecked)
{
if (block->globOptData.maybeWrittenTypeSyms == nullptr)
{
block->globOptData.maybeWrittenTypeSyms = JitAnew(this->alloc, BVSparse<JitArenaAllocator>, this->alloc);
}
if (isObjTypeSpecialized)
{
// The current object will be protected by a type check, unless no further accesses to it are
// protected by this access.
Assert(this->objectTypeSyms->Test(opndId));
this->objectTypeSyms->Clear(opndId);
}
block->globOptData.maybeWrittenTypeSyms->Or(this->objectTypeSyms);
if (isObjTypeSpecialized)
{
this->objectTypeSyms->Set(opndId);
}
}
if (!isObjTypeSpecialized || opnd->ChangesObjectLayout())
{
this->KillObjectHeaderInlinedTypeSyms(block, isObjTypeSpecialized, opndId);
this->KillAuxSlotPtrSyms(opnd, block, isObjTypeSpecialized);
}
else if (!isObjTypeChecked && this->HasLiveObjectHeaderInlinedTypeSym(block, true, opndId))
{
opnd->SetTypeCheckRequired(true);
}
}
return isObjTypeSpecialized;
}
StackSym *
GlobOpt::EnsureAuxSlotPtrSym(IR::PropertySymOpnd *opnd)
{
StackSym *auxSlotPtrSym = opnd->EnsureAuxSlotPtrSym(this->func);
this->auxSlotPtrSyms->Set(auxSlotPtrSym->m_id);
return auxSlotPtrSym;
}
void
GlobOpt::KillAuxSlotPtrSyms(IR::PropertySymOpnd *opnd, BasicBlock *block, bool isObjTypeSpecialized)
{
StackSym *auxSlotPtrSym = nullptr;
if (isObjTypeSpecialized)
{
// Kill all aux slot syms other than this one
auxSlotPtrSym = opnd->GetAuxSlotPtrSym();
if (auxSlotPtrSym)
{
Assert(this->auxSlotPtrSyms && this->auxSlotPtrSyms->Test(auxSlotPtrSym->m_id));
this->auxSlotPtrSyms->Clear(auxSlotPtrSym->m_id);
}
}
block->globOptData.liveFields->Minus(this->auxSlotPtrSyms);
if (auxSlotPtrSym)
{
this->auxSlotPtrSyms->Set(auxSlotPtrSym->m_id);
}
}
void
GlobOpt::KillObjectHeaderInlinedTypeSyms(BasicBlock *block, bool isObjTypeSpecialized, SymID opndId)
{
this->MapObjectHeaderInlinedTypeSymsUntil(block, isObjTypeSpecialized, opndId, [&](SymID symId)->bool { this->currentBlock->globOptData.liveFields->Clear(symId); return false; });
}
bool
GlobOpt::HasLiveObjectHeaderInlinedTypeSym(BasicBlock *block, bool isObjTypeSpecialized, SymID opndId)
{
return this->MapObjectHeaderInlinedTypeSymsUntil(block, true, opndId, [&](SymID symId)->bool { return this->currentBlock->globOptData.liveFields->Test(symId); });
}
template<class Fn>
bool
GlobOpt::MapObjectHeaderInlinedTypeSymsUntil(BasicBlock *block, bool isObjTypeSpecialized, SymID opndId, Fn fn)
{
if (this->objectTypeSyms == nullptr)
{
return false;
}
FOREACH_BITSET_IN_SPARSEBV(symId, this->objectTypeSyms)
{
if (symId == opndId && isObjTypeSpecialized)
{
// The current object will be protected by a type check, unless no further accesses to it are
// protected by this access.
continue;
}
Value *value = block->globOptData.FindObjectTypeValue(symId);
if (value)
{
JsTypeValueInfo *valueInfo = value->GetValueInfo()->AsJsType();
Assert(valueInfo);
if (valueInfo->GetJsType() != nullptr)
{
JITTypeHolder type(valueInfo->GetJsType());
if (Js::DynamicType::Is(type->GetTypeId()))
{
if (type->GetTypeHandler()->IsObjectHeaderInlinedTypeHandler())
{
if (fn(symId))
{
return true;
}
}
}
}
else if (valueInfo->GetJsTypeSet())
{
Js::EquivalentTypeSet *typeSet = valueInfo->GetJsTypeSet();
for (uint16 i = 0; i < typeSet->GetCount(); i++)
{
JITTypeHolder type = typeSet->GetType(i);
if (type != nullptr && Js::DynamicType::Is(type->GetTypeId()))
{
if (type->GetTypeHandler()->IsObjectHeaderInlinedTypeHandler())
{
if (fn(symId))
{
return true;
}
break;
}
}
}
}
}
}
NEXT_BITSET_IN_SPARSEBV;
return false;
}
bool
GlobOpt::AreTypeSetsIdentical(Js::EquivalentTypeSet * leftTypeSet, Js::EquivalentTypeSet * rightTypeSet)
{
return Js::EquivalentTypeSet::AreIdentical(leftTypeSet, rightTypeSet);
}
bool
GlobOpt::IsSubsetOf(Js::EquivalentTypeSet * leftTypeSet, Js::EquivalentTypeSet * rightTypeSet)
{
return Js::EquivalentTypeSet::IsSubsetOf(leftTypeSet, rightTypeSet);
}
bool
GlobOpt::CompareCurrentTypesWithExpectedTypes(JsTypeValueInfo *valueInfo, IR::PropertySymOpnd * propertySymOpnd)
{
bool isTypeDead = propertySymOpnd->IsTypeDead();
if (valueInfo == nullptr || (valueInfo->GetJsType() == nullptr && valueInfo->GetJsTypeSet() == nullptr))
{
// No upstream types. Do a type check.
return !isTypeDead;
}
if (!propertySymOpnd->HasEquivalentTypeSet() || propertySymOpnd->NeedsMonoCheck())
{
JITTypeHolder opndType = propertySymOpnd->GetType();
if (valueInfo->GetJsType() != nullptr)
{
if (valueInfo->GetJsType() == propertySymOpnd->GetType())
{
return true;
}
if (propertySymOpnd->HasInitialType() && valueInfo->GetJsType() == propertySymOpnd->GetInitialType())
{
return !isTypeDead;
}
return false;
}
else
{
Assert(valueInfo->GetJsTypeSet());
Js::EquivalentTypeSet *valueTypeSet = valueInfo->GetJsTypeSet();
if (valueTypeSet->Contains(opndType))
{
return !isTypeDead;
}
if (propertySymOpnd->HasInitialType() && valueTypeSet->Contains(propertySymOpnd->GetInitialType()))
{
return !isTypeDead;
}
return false;
}
}
else
{
Js::EquivalentTypeSet * opndTypeSet = propertySymOpnd->GetEquivalentTypeSet();
if (valueInfo->GetJsType() != nullptr)
{
uint16 checkedTypeSetIndex;
if (opndTypeSet->Contains(valueInfo->GetJsType(), &checkedTypeSetIndex))
{
return true;
}
return false;
}
else
{
if (IsSubsetOf(valueInfo->GetJsTypeSet(), opndTypeSet))
{
return true;
}
if (propertySymOpnd->IsMono() ?
valueInfo->GetJsTypeSet()->Contains(propertySymOpnd->GetFirstEquivalentType()) :
IsSubsetOf(opndTypeSet, valueInfo->GetJsTypeSet()))
{
return true;
}
return false;
}
}
}
bool
GlobOpt::ProcessPropOpInTypeCheckSeq(IR::Instr* instr, IR::PropertySymOpnd *opnd)
{
return ProcessPropOpInTypeCheckSeq<true>(instr, opnd, this->currentBlock, false);
}
bool GlobOpt::CheckIfInstrInTypeCheckSeqEmitsTypeCheck(IR::Instr* instr, IR::PropertySymOpnd *opnd)
{
bool emitsTypeCheck;
ProcessPropOpInTypeCheckSeq<false>(instr, opnd, this->currentBlock, false, &emitsTypeCheck);
return emitsTypeCheck;