forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInlineCache.cpp
More file actions
1243 lines (1109 loc) · 48.6 KB
/
Copy pathInlineCache.cpp
File metadata and controls
1243 lines (1109 loc) · 48.6 KB
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 "RuntimeLanguagePch.h"
#if ENABLE_NATIVE_CODEGEN
#include "JITType.h"
#endif
namespace Js
{
void InlineCache::CacheLocal(
Type *const type,
const PropertyId propertyId,
const PropertyIndex propertyIndex,
const bool isInlineSlot,
Type *const typeWithoutProperty,
int requiredAuxSlotCapacity,
ScriptContext *const requestContext)
{
Assert(type);
Assert(propertyId != Constants::NoProperty);
Assert(propertyIndex != Constants::NoSlot);
Assert(requestContext);
Assert(type->GetScriptContext() == requestContext);
DebugOnly(VerifyRegistrationForInvalidation(this, requestContext, propertyId));
Assert(requiredAuxSlotCapacity >= 0 && requiredAuxSlotCapacity < 0x01 << RequiredAuxSlotCapacityBitCount);
// Store field and load field caches are never shared so we should never have a prototype cache morphing into an add property cache.
// We may, however, have a flags cache (setter) change to add property cache.
Assert(typeWithoutProperty == nullptr || !IsProto());
requestContext->SetHasUsedInlineCache(true);
// Add cache into a store field cache list if required, but not there yet.
if (typeWithoutProperty != nullptr && invalidationListSlotPtr == nullptr)
{
// Note, this can throw due to OOM, so we need to do it before the inline cache is set below.
requestContext->RegisterStoreFieldInlineCache(this, propertyId);
}
if (isInlineSlot)
{
u.local.type = type;
u.local.typeWithoutProperty = typeWithoutProperty;
}
else
{
u.local.type = TypeWithAuxSlotTag(type);
u.local.typeWithoutProperty = typeWithoutProperty ? TypeWithAuxSlotTag(typeWithoutProperty) : nullptr;
}
u.local.isLocal = true;
u.local.slotIndex = propertyIndex;
u.local.requiredAuxSlotCapacity = requiredAuxSlotCapacity;
type->SetHasBeenCached();
if (typeWithoutProperty)
{
typeWithoutProperty->SetHasBeenCached();
}
DebugOnly(VerifyRegistrationForInvalidation(this, requestContext, propertyId));
#if DBG_DUMP
if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
{
Output::Print(_u("IC::CacheLocal, %s: "), requestContext->GetPropertyName(propertyId)->GetBuffer());
Dump();
Output::Print(_u("\n"));
Output::Flush();
}
#endif
}
void InlineCache::CacheProto(
DynamicObject *const prototypeObjectWithProperty,
const PropertyId propertyId,
const PropertyIndex propertyIndex,
const bool isInlineSlot,
const bool isMissing,
Type *const type,
ScriptContext *const requestContext)
{
Assert(prototypeObjectWithProperty);
Assert(propertyId != Constants::NoProperty);
Assert(propertyIndex != Constants::NoSlot);
Assert(type);
Assert(requestContext);
Assert(prototypeObjectWithProperty->GetScriptContext() == requestContext);
DebugOnly(VerifyRegistrationForInvalidation(this, requestContext, propertyId));
// This is an interesting quirk. In the browser Chakra's global object cannot be used directly as a prototype, because
// the global object (referenced either as window or as this) always points to the host object. Thus, when we retrieve
// a property from Chakra's global object the prototypeObjectWithProperty != info->GetInstance() and we will never cache
// such property loads (see CacheOperators::CachePropertyRead). However, in jc.exe or jshost.exe the only global object
// is Chakra's global object, and so prototypeObjectWithProperty == info->GetInstance() and we can cache. Hence, the
// assert below is only correct when running in the browser.
// Assert(prototypeObjectWithProperty != prototypeObjectWithProperty->type->GetLibrary()->GetGlobalObject());
// Store field and load field caches are never shared so we should never have an add property cache morphing into a prototype cache.
Assert(!IsLocal() || u.local.typeWithoutProperty == nullptr);
requestContext->SetHasUsedInlineCache(true);
// Add cache into a proto cache list if not there yet.
if (invalidationListSlotPtr == nullptr)
{
// Note, this can throw due to OOM, so we need to do it before the inline cache is set below.
requestContext->RegisterProtoInlineCache(this, propertyId);
}
u.proto.prototypeObject = prototypeObjectWithProperty;
u.proto.isProto = true;
u.proto.isMissing = isMissing;
u.proto.slotIndex = propertyIndex;
if (isInlineSlot)
{
u.proto.type = type;
}
else
{
u.proto.type = TypeWithAuxSlotTag(type);
}
prototypeObjectWithProperty->GetType()->SetHasBeenCached();
DebugOnly(VerifyRegistrationForInvalidation(this, requestContext, propertyId));
Assert(u.proto.isMissing == (uint16)(u.proto.prototypeObject == requestContext->GetLibrary()->GetMissingPropertyHolder()));
#if DBG_DUMP
if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
{
Output::Print(_u("IC::CacheProto, %s: "), requestContext->GetPropertyName(propertyId)->GetBuffer());
Dump();
Output::Print(_u("\n"));
Output::Flush();
}
#endif
}
// TODO (InlineCacheCleanup): When simplifying inline caches due to not sharing between loads and stores, create two
// separate methods CacheSetter and CacheGetter.
void InlineCache::CacheAccessor(
const bool isGetter,
const PropertyId propertyId,
const PropertyIndex propertyIndex,
const bool isInlineSlot,
Type *const type,
DynamicObject *const object,
const bool isOnProto,
ScriptContext *const requestContext)
{
Assert(propertyId != Constants::NoProperty);
Assert(propertyIndex != Constants::NoSlot);
Assert(type);
Assert(object);
Assert(requestContext);
DebugOnly(VerifyRegistrationForInvalidation(this, requestContext, propertyId));
// It is possible that prototype is from a different scriptContext than the original instance. We don't want to cache
// in this case.
Assert(type->GetScriptContext() == requestContext);
requestContext->SetHasUsedInlineCache(true);
if (isOnProto && invalidationListSlotPtr == nullptr)
{
// Note, this can throw due to OOM, so we need to do it before the inline cache is set below.
if (!isGetter)
{
// If the setter is on a prototype, this cache must be invalidated whenever proto
// caches are invalidated, so we must register it here. Note that store field inline
// caches are invalidated any time proto caches are invalidated.
requestContext->RegisterStoreFieldInlineCache(this, propertyId);
}
else
{
requestContext->RegisterProtoInlineCache(this, propertyId);
}
}
u.accessor.isAccessor = true;
// TODO (PersistentInlineCaches): Consider removing the flag altogether and just have a bit indicating
// whether the cache itself is a store field cache (isStore?).
u.accessor.flags = isGetter ? InlineCacheGetterFlag : InlineCacheSetterFlag;
u.accessor.isOnProto = isOnProto;
u.accessor.type = isInlineSlot ? type : TypeWithAuxSlotTag(type);
u.accessor.slotIndex = propertyIndex;
u.accessor.object = object;
type->SetHasBeenCached();
DebugOnly(VerifyRegistrationForInvalidation(this, requestContext, propertyId));
#if DBG_DUMP
if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
{
Output::Print(_u("IC::CacheAccessor, %s: "), requestContext->GetPropertyName(propertyId)->GetBuffer());
Dump();
Output::Print(_u("\n"));
Output::Flush();
}
#endif
}
bool InlineCache::PretendTryGetProperty(Type *const type, PropertyCacheOperationInfo * operationInfo) const
{
if (type == u.local.type)
{
operationInfo->cacheType = CacheType_Local;
operationInfo->slotType = SlotType_Inline;
return true;
}
if (TypeWithAuxSlotTag(type) == u.local.type)
{
operationInfo->cacheType = CacheType_Local;
operationInfo->slotType = SlotType_Aux;
return true;
}
if (type == u.proto.type)
{
operationInfo->cacheType = CacheType_Proto;
operationInfo->slotType = SlotType_Inline;
return true;
}
if (TypeWithAuxSlotTag(type) == u.proto.type)
{
operationInfo->cacheType = CacheType_Proto;
operationInfo->slotType = SlotType_Aux;
return true;
}
if (type == u.accessor.type)
{
Assert(u.accessor.flags & InlineCacheGetterFlag);
operationInfo->cacheType = CacheType_Getter;
operationInfo->slotType = SlotType_Inline;
return true;
}
if (TypeWithAuxSlotTag(type) == u.accessor.type)
{
Assert(u.accessor.flags & InlineCacheGetterFlag);
operationInfo->cacheType = CacheType_Getter;
operationInfo->slotType = SlotType_Aux;
return true;
}
return false;
}
bool InlineCache::PretendTrySetProperty(Type *const type, Type *const oldType, PropertyCacheOperationInfo * operationInfo) const
{
if (oldType == u.local.typeWithoutProperty)
{
operationInfo->cacheType = CacheType_LocalWithoutProperty;
operationInfo->slotType = SlotType_Inline;
return true;
}
if (TypeWithAuxSlotTag(oldType) == u.local.typeWithoutProperty)
{
operationInfo->cacheType = CacheType_LocalWithoutProperty;
operationInfo->slotType = SlotType_Aux;
return true;
}
if (type == u.local.type)
{
operationInfo->cacheType = CacheType_Local;
operationInfo->slotType = SlotType_Inline;
return true;
}
if (TypeWithAuxSlotTag(type) == u.local.type)
{
operationInfo->cacheType = CacheType_Local;
operationInfo->slotType = SlotType_Aux;
return true;
}
if (type == u.accessor.type)
{
if (u.accessor.flags & InlineCacheSetterFlag)
{
operationInfo->cacheType = CacheType_Setter;
operationInfo->slotType = SlotType_Inline;
return true;
}
}
if (TypeWithAuxSlotTag(type) == u.accessor.type)
{
if (u.accessor.flags & InlineCacheSetterFlag)
{
operationInfo->cacheType = CacheType_Setter;
operationInfo->slotType = SlotType_Aux;
return true;
}
}
return false;
}
bool InlineCache::GetGetterSetter(RecyclableObject *object, RecyclableObject **callee)
{
*callee = nullptr;
if (u.accessor.flags & (InlineCacheGetterFlag | InlineCacheSetterFlag))
{
Type *type = object->GetType();
Type *const taggedType = TypeWithAuxSlotTag(type);
if (u.accessor.isOnProto)
{
object = u.accessor.object;
}
if (type == u.accessor.type)
{
*callee = VarTo<RecyclableObject>(VarTo<DynamicObject>(object)->GetInlineSlot(u.accessor.slotIndex));
return true;
}
else if (taggedType == u.accessor.type)
{
*callee = VarTo<RecyclableObject>(VarTo<DynamicObject>(object)->GetAuxSlot(u.accessor.slotIndex));
return true;
}
}
return false;
}
bool InlineCache::GetCallApplyTarget(RecyclableObject* obj, RecyclableObject **callee)
{
Type *const type = obj->GetType();
Type *const taggedType = TypeWithAuxSlotTag(type);
*callee = nullptr;
if (IsLocal())
{
if (type == u.local.type)
{
const Var objectAtInlineSlot = VarTo<DynamicObject>(obj)->GetInlineSlot(u.local.slotIndex);
if (!Js::TaggedNumber::Is(objectAtInlineSlot))
{
*callee = VarTo<RecyclableObject>(objectAtInlineSlot);
return true;
}
}
else if (taggedType == u.local.type)
{
const Var objectAtAuxSlot = VarTo<DynamicObject>(obj)->GetAuxSlot(u.local.slotIndex);
if (!Js::TaggedNumber::Is(objectAtAuxSlot))
{
*callee = VarTo<RecyclableObject>(VarTo<DynamicObject>(obj)->GetAuxSlot(u.local.slotIndex));
return true;
}
}
return false;
}
else if (IsProto())
{
if (type == u.proto.type)
{
const Var objectAtInlineSlot = u.proto.prototypeObject->GetInlineSlot(u.proto.slotIndex);
if (!Js::TaggedNumber::Is(objectAtInlineSlot))
{
*callee = VarTo<RecyclableObject>(objectAtInlineSlot);
return true;
}
}
else if (taggedType == u.proto.type)
{
const Var objectAtAuxSlot = u.proto.prototypeObject->GetAuxSlot(u.proto.slotIndex);
if (!Js::TaggedNumber::Is(objectAtAuxSlot))
{
*callee = VarTo<RecyclableObject>(objectAtAuxSlot);
return true;
}
}
return false;
}
return false;
}
void InlineCache::Clear()
{
#if DBG
if (!IsAll((byte*)this, sizeof(InlineCache), 0))
#endif
{
memset(this, 0, sizeof(InlineCache));
}
}
void InlineCache::RemoveFromInvalidationListAndClear(ThreadContext* threadContext)
{
if (this->RemoveFromInvalidationList())
{
threadContext->NotifyInlineCacheBatchUnregistered(1);
}
this->Clear();
}
InlineCache *InlineCache::Clone(Js::PropertyId propertyId, ScriptContext* scriptContext)
{
Assert(scriptContext);
InlineCacheAllocator* allocator = scriptContext->GetInlineCacheAllocator();
// Important to zero the allocated cache to be sure CopyTo doesn't see garbage
// when it uses the next pointer.
InlineCache* clone = AllocatorNewZ(InlineCacheAllocator, allocator, InlineCache);
CopyTo(propertyId, scriptContext, clone);
return clone;
}
#if ENABLE_FIXED_FIELDS
bool InlineCache::TryGetFixedMethodFromCache(Js::FunctionBody* functionBody, uint cacheId, Js::JavascriptFunction** pFixedMethod)
{
Assert(pFixedMethod);
if (IsEmpty())
{
return false;
}
Js::Type * propertyOwnerType = nullptr;
bool isLocal = IsLocal();
bool isProto = IsProto();
if (isLocal)
{
propertyOwnerType = TypeWithoutAuxSlotTag(this->u.local.type);
}
else if (isProto)
{
// TODO (InlineCacheCleanup): For loads from proto, we could at least grab the value from protoObject's slot
// (given by the cache) and see if its a function. Only then, does it make sense to check with the type handler.
propertyOwnerType = this->u.proto.prototypeObject->GetType();
}
else if (u.accessor.isOnProto)
{
propertyOwnerType = this->u.accessor.object->GetType();
}
else
{
propertyOwnerType = TypeWithoutAuxSlotTag(this->u.accessor.type);
}
Assert(propertyOwnerType != nullptr);
if (Js::DynamicType::Is(propertyOwnerType))
{
Js::DynamicTypeHandler* propertyOwnerTypeHandler = ((Js::DynamicType*)propertyOwnerType)->GetTypeHandler();
Js::PropertyId propertyId = functionBody->GetPropertyIdFromCacheId(cacheId);
Js::PropertyRecord const * const methodPropertyRecord = functionBody->GetScriptContext()->GetPropertyName(propertyId);
Var fixedMethod = nullptr;
bool isUseFixedProperty;
if (isLocal || isProto)
{
isUseFixedProperty = propertyOwnerTypeHandler->TryUseFixedProperty(methodPropertyRecord, &fixedMethod, Js::FixedPropertyKind::FixedMethodProperty, functionBody->GetScriptContext());
}
else
{
isUseFixedProperty = propertyOwnerTypeHandler->TryUseFixedAccessor(methodPropertyRecord, &fixedMethod, Js::FixedPropertyKind::FixedAccessorProperty, this->IsGetterAccessor(), functionBody->GetScriptContext());
}
AssertMsg(fixedMethod == nullptr || Js::VarIs<Js::JavascriptFunction>(fixedMethod), "The fixed value should have been a Method !!!");
*pFixedMethod = reinterpret_cast<JavascriptFunction*>(fixedMethod);
return isUseFixedProperty;
}
return false;
}
#endif
void InlineCache::CopyTo(PropertyId propertyId, ScriptContext * scriptContext, InlineCache * const clone)
{
DebugOnly(VerifyRegistrationForInvalidation(this, scriptContext, propertyId));
DebugOnly(VerifyRegistrationForInvalidation(clone, scriptContext, propertyId));
Assert(clone != nullptr);
// Note, the Register methods can throw due to OOM, so we need to do it before the inline cache is copied below.
if (this->invalidationListSlotPtr != nullptr && clone->invalidationListSlotPtr == nullptr)
{
if (this->NeedsToBeRegisteredForProtoInvalidation())
{
scriptContext->RegisterProtoInlineCache(clone, propertyId);
}
else if (this->NeedsToBeRegisteredForStoreFieldInvalidation())
{
scriptContext->RegisterStoreFieldInlineCache(clone, propertyId);
}
}
#if DBG
// inline cache pages might been locked after ZeroAll
if (memcmp(&clone->u, &this->u, sizeof(this->u)) != 0)
#endif
{
clone->u = this->u;
}
DebugOnly(VerifyRegistrationForInvalidation(clone, scriptContext, propertyId));
}
template <bool isAccessor>
bool InlineCache::HasDifferentType(const bool isProto, const Type * type, const Type * typeWithoutProperty) const
{
Assert(!isAccessor && !isProto || !typeWithoutProperty);
if (isAccessor)
{
return !IsEmpty() && u.accessor.type != type && u.accessor.type != TypeWithAuxSlotTag(type);
}
if (isProto)
{
return !IsEmpty() && u.proto.type != type && u.proto.type != TypeWithAuxSlotTag(type);
}
// If the new type matches the cached type, the types without property must also match (unless one of them is null).
Assert((u.local.typeWithoutProperty == nullptr || typeWithoutProperty == nullptr) ||
((u.local.type != type || u.local.typeWithoutProperty == typeWithoutProperty) &&
(u.local.type != TypeWithAuxSlotTag(type) || u.local.typeWithoutProperty == TypeWithAuxSlotTag(typeWithoutProperty))));
// Don't consider a cache polymorphic, if it differs only by the typeWithoutProperty. We can handle this case with
// the monomorphic cache.
return !IsEmpty() && (u.local.type != type && u.local.type != TypeWithAuxSlotTag(type));
}
// explicit instantiation
template bool InlineCache::HasDifferentType<true>(const bool isProto, const Type * type, const Type * typeWithoutProperty) const;
template bool InlineCache::HasDifferentType<false>(const bool isProto, const Type * type, const Type * typeWithoutProperty) const;
bool InlineCache::NeedsToBeRegisteredForProtoInvalidation() const
{
return (IsProto() || IsGetterAccessorOnProto());
}
bool InlineCache::NeedsToBeRegisteredForStoreFieldInvalidation() const
{
return (IsLocal() && this->u.local.typeWithoutProperty != nullptr) || IsSetterAccessorOnProto();
}
#if DEBUG
bool InlineCache::NeedsToBeRegisteredForInvalidation() const
{
int howManyInvalidationsNeeded =
(int)NeedsToBeRegisteredForProtoInvalidation() +
(int)NeedsToBeRegisteredForStoreFieldInvalidation();
Assert(howManyInvalidationsNeeded <= 1);
return howManyInvalidationsNeeded > 0;
}
void InlineCache::VerifyRegistrationForInvalidation(const InlineCache* cache, ScriptContext* scriptContext, Js::PropertyId propertyId)
{
bool needsProtoInvalidation = cache->NeedsToBeRegisteredForProtoInvalidation();
bool needsStoreFieldInvalidation = cache->NeedsToBeRegisteredForStoreFieldInvalidation();
int howManyInvalidationsNeeded = (int)needsProtoInvalidation + (int)needsStoreFieldInvalidation;
bool hasListSlotPtr = cache->invalidationListSlotPtr != nullptr;
bool isProtoRegistered = hasListSlotPtr ? scriptContext->GetThreadContext()->IsProtoInlineCacheRegistered(cache, propertyId) : false;
bool isStoreFieldRegistered = hasListSlotPtr ? scriptContext->GetThreadContext()->IsStoreFieldInlineCacheRegistered(cache, propertyId) : false;
int howManyRegistrations = (int)isProtoRegistered + (int)isStoreFieldRegistered;
Assert(howManyInvalidationsNeeded <= 1);
Assert((howManyInvalidationsNeeded == 0) || hasListSlotPtr);
Assert(!needsProtoInvalidation || isProtoRegistered);
Assert(!needsStoreFieldInvalidation || isStoreFieldRegistered);
Assert(!hasListSlotPtr || howManyRegistrations > 0);
Assert(!hasListSlotPtr || (*cache->invalidationListSlotPtr) == cache);
}
// Confirm inline cache miss against instance property lookup info.
bool InlineCache::ConfirmCacheMiss(const Type * oldType, const PropertyValueInfo* info) const
{
return u.local.type != oldType
&& u.proto.type != oldType
&& (u.accessor.type != oldType || info == NULL || u.accessor.flags != info->GetFlags());
}
#endif
#if DBG_DUMP
void InlineCache::Dump()
{
if (this->u.local.isLocal)
{
Output::Print(_u("LOCAL { types: 0x%X -> 0x%X, slot = %d, list slot ptr = 0x%X }"),
this->u.local.typeWithoutProperty,
this->u.local.type,
this->u.local.slotIndex,
this->invalidationListSlotPtr
);
}
else if (this->u.proto.isProto)
{
Output::Print(_u("PROTO { type = 0x%X, prototype = 0x%X, slot = %d, list slot ptr = 0x%X }"),
this->u.proto.type,
this->u.proto.prototypeObject,
this->u.proto.slotIndex,
this->invalidationListSlotPtr
);
}
else if (this->u.accessor.isAccessor)
{
Output::Print(_u("FLAGS { type = 0x%X, object = 0x%X, flag = 0x%X, slot = %d, list slot ptr = 0x%X }"),
this->u.accessor.type,
this->u.accessor.object,
this->u.accessor.slotIndex,
this->u.accessor.flags,
this->invalidationListSlotPtr
);
}
else
{
Assert(this->u.accessor.type == 0);
Assert(this->u.accessor.slotIndex == 0);
Output::Print(_u("uninitialized"));
}
}
#endif
template<bool isAccessor>
bool PolymorphicInlineCache::HasDifferentType(
const bool isProto,
const Type * type,
const Type * typeWithoutProperty) const
{
Assert(!isAccessor && !isProto || !typeWithoutProperty);
uint inlineCacheIndex = GetInlineCacheIndexForType(type);
if (inlineCaches[inlineCacheIndex].HasDifferentType<isAccessor>(isProto, type, typeWithoutProperty))
{
return true;
}
if (!isAccessor && !isProto && typeWithoutProperty)
{
inlineCacheIndex = GetInlineCacheIndexForType(typeWithoutProperty);
return inlineCaches[inlineCacheIndex].HasDifferentType<isAccessor>(isProto, type, typeWithoutProperty);
}
return false;
}
// explicit instantiation
template bool PolymorphicInlineCache::HasDifferentType<true>(const bool isProto, const Type * type, const Type * typeWithoutProperty) const;
template bool PolymorphicInlineCache::HasDifferentType<false>(const bool isProto, const Type * type, const Type * typeWithoutProperty) const;
bool PolymorphicInlineCache::HasType_Flags(const Type * type) const
{
uint inlineCacheIndex = GetInlineCacheIndexForType(type);
return inlineCaches[inlineCacheIndex].HasType_Flags(type);
}
void PolymorphicInlineCache::UpdateInlineCachesFillInfo(uint index, bool set)
{
Assert(index < 0x20);
if (set)
{
this->inlineCachesFillInfo |= 1 << index;
}
else
{
this->inlineCachesFillInfo &= ~(1 << index);
}
}
bool PolymorphicInlineCache::IsFull()
{
Assert(this->size <= 0x20);
return this->inlineCachesFillInfo == ((1 << (this->size - 1)) << 1) - 1;
}
void PolymorphicInlineCache::CacheLocal(
Type *const type,
const PropertyId propertyId,
const PropertyIndex propertyIndex,
const bool isInlineSlot,
Type *const typeWithoutProperty,
int requiredAuxSlotCapacity,
ScriptContext *const requestContext)
{
// Let's not waste polymorphic cache slots by caching both the type without property and type with property. If the
// cache is used for both adding a property and setting the existing property, then those instances will cause both
// types to be cached. Until then, caching both types proactively here can unnecessarily trash useful cached info
// because the types use different slots, unlike a monomorphic inline cache.
if (!typeWithoutProperty)
{
uint inlineCacheIndex = GetInlineCacheIndexForType(type);
#if INTRUSIVE_TESTTRACE_PolymorphicInlineCache
bool collision = !inlineCaches[inlineCacheIndex].IsEmpty();
#endif
if (!PHASE_OFF1(Js::CloneCacheInCollisionPhase))
{
if (!inlineCaches[inlineCacheIndex].IsEmpty() && !inlineCaches[inlineCacheIndex].NeedsToBeRegisteredForStoreFieldInvalidation() && GetSize() != 1)
{
if (inlineCaches[inlineCacheIndex].IsLocal())
{
CloneInlineCacheToEmptySlotInCollision<true, false, false>(type, inlineCacheIndex);
}
else if (inlineCaches[inlineCacheIndex].IsProto())
{
CloneInlineCacheToEmptySlotInCollision<false, true, false>(type, inlineCacheIndex);
}
else
{
CloneInlineCacheToEmptySlotInCollision<false, false, true>(type, inlineCacheIndex);
}
}
}
inlineCaches[inlineCacheIndex].CacheLocal(
type, propertyId, propertyIndex, isInlineSlot, nullptr, requiredAuxSlotCapacity, requestContext);
UpdateInlineCachesFillInfo(inlineCacheIndex, true /*set*/);
#if DBG_DUMP
if (PHASE_VERBOSE_TRACE1(Js::PolymorphicInlineCachePhase))
{
Output::Print(_u("PIC::CacheLocal, %s, %d: "), requestContext->GetPropertyName(propertyId)->GetBuffer(), inlineCacheIndex);
inlineCaches[inlineCacheIndex].Dump();
Output::Print(_u("\n"));
Output::Flush();
}
#endif
PHASE_PRINT_INTRUSIVE_TESTTRACE1(
Js::PolymorphicInlineCachePhase,
_u("TestTrace PIC: CacheLocal, 0x%x, entryIndex = %d, collision = %s, entries = %d\n"), this, inlineCacheIndex, collision ? _u("true") : _u("false"), GetEntryCount());
}
else
{
uint inlineCacheIndex = GetInlineCacheIndexForType(typeWithoutProperty);
#if INTRUSIVE_TESTTRACE_PolymorphicInlineCache
bool collision = !inlineCaches[inlineCacheIndex].IsEmpty();
#endif
inlineCaches[inlineCacheIndex].CacheLocal(
type, propertyId, propertyIndex, isInlineSlot, typeWithoutProperty, requiredAuxSlotCapacity, requestContext);
UpdateInlineCachesFillInfo(inlineCacheIndex, true /*set*/);
#if DBG_DUMP
if (PHASE_VERBOSE_TRACE1(Js::PolymorphicInlineCachePhase))
{
Output::Print(_u("PIC::CacheLocal, %s, %d: "), requestContext->GetPropertyName(propertyId)->GetBuffer(), inlineCacheIndex);
inlineCaches[inlineCacheIndex].Dump();
Output::Print(_u("\n"));
Output::Flush();
}
#endif
PHASE_PRINT_INTRUSIVE_TESTTRACE1(
Js::PolymorphicInlineCachePhase,
_u("TestTrace PIC: CacheLocal, 0x%x, entryIndex = %d, collision = %s, entries = %d\n"), this, inlineCacheIndex, collision ? _u("true") : _u("false"), GetEntryCount());
}
}
void PolymorphicInlineCache::CacheProto(
DynamicObject *const prototypeObjectWithProperty,
const PropertyId propertyId,
const PropertyIndex propertyIndex,
const bool isInlineSlot,
const bool isMissing,
Type *const type,
ScriptContext *const requestContext)
{
uint inlineCacheIndex = GetInlineCacheIndexForType(type);
#if INTRUSIVE_TESTTRACE_PolymorphicInlineCache
bool collision = !inlineCaches[inlineCacheIndex].IsEmpty();
#endif
if (!PHASE_OFF1(Js::CloneCacheInCollisionPhase))
{
if (!inlineCaches[inlineCacheIndex].IsEmpty() && !inlineCaches[inlineCacheIndex].NeedsToBeRegisteredForStoreFieldInvalidation() && GetSize() != 1)
{
if (inlineCaches[inlineCacheIndex].IsLocal())
{
CloneInlineCacheToEmptySlotInCollision<true, false, false>(type, inlineCacheIndex);
}
else if (inlineCaches[inlineCacheIndex].IsProto())
{
CloneInlineCacheToEmptySlotInCollision<false, true, false>(type, inlineCacheIndex);
}
else
{
CloneInlineCacheToEmptySlotInCollision<false, false, true>(type, inlineCacheIndex);
}
}
}
inlineCaches[inlineCacheIndex].CacheProto(
prototypeObjectWithProperty, propertyId, propertyIndex, isInlineSlot, isMissing, type, requestContext);
UpdateInlineCachesFillInfo(inlineCacheIndex, true /*set*/);
#if DBG_DUMP
if (PHASE_VERBOSE_TRACE1(Js::PolymorphicInlineCachePhase))
{
Output::Print(_u("PIC::CacheProto, %s, %d: "), requestContext->GetPropertyName(propertyId)->GetBuffer(), inlineCacheIndex);
inlineCaches[inlineCacheIndex].Dump();
Output::Print(_u("\n"));
Output::Flush();
}
#endif
PHASE_PRINT_INTRUSIVE_TESTTRACE1(
Js::PolymorphicInlineCachePhase,
_u("TestTrace PIC: CacheProto, 0x%x, entryIndex = %d, collision = %s, entries = %d\n"), this, inlineCacheIndex, collision ? _u("true") : _u("false"), GetEntryCount());
}
void PolymorphicInlineCache::CacheAccessor(
const bool isGetter,
const PropertyId propertyId,
const PropertyIndex propertyIndex,
const bool isInlineSlot,
Type *const type,
DynamicObject *const object,
const bool isOnProto,
ScriptContext *const requestContext)
{
uint inlineCacheIndex = GetInlineCacheIndexForType(type);
#if INTRUSIVE_TESTTRACE_PolymorphicInlineCache
bool collision = !inlineCaches[inlineCacheIndex].IsEmpty();
#endif
if (!PHASE_OFF1(Js::CloneCacheInCollisionPhase))
{
if (!inlineCaches[inlineCacheIndex].IsEmpty() && !inlineCaches[inlineCacheIndex].NeedsToBeRegisteredForStoreFieldInvalidation() && GetSize() != 1)
{
if (inlineCaches[inlineCacheIndex].IsLocal())
{
CloneInlineCacheToEmptySlotInCollision<true, false, false>(type, inlineCacheIndex);
}
else if (inlineCaches[inlineCacheIndex].IsProto())
{
CloneInlineCacheToEmptySlotInCollision<false, true, false>(type, inlineCacheIndex);
}
else
{
CloneInlineCacheToEmptySlotInCollision<false, false, true>(type, inlineCacheIndex);
}
}
}
inlineCaches[inlineCacheIndex].CacheAccessor(isGetter, propertyId, propertyIndex, isInlineSlot, type, object, isOnProto, requestContext);
UpdateInlineCachesFillInfo(inlineCacheIndex, true /*set*/);
#if DBG_DUMP
if (PHASE_VERBOSE_TRACE1(Js::PolymorphicInlineCachePhase))
{
Output::Print(_u("PIC::CacheAccessor, %s, %d: "), requestContext->GetPropertyName(propertyId)->GetBuffer(), inlineCacheIndex);
inlineCaches[inlineCacheIndex].Dump();
Output::Print(_u("\n"));
Output::Flush();
}
#endif
PHASE_PRINT_INTRUSIVE_TESTTRACE1(
Js::PolymorphicInlineCachePhase,
_u("TestTrace PIC: CacheAccessor, 0x%x, entryIndex = %d, collision = %s, entries = %d\n"), this, inlineCacheIndex, collision ? _u("true") : _u("false"), GetEntryCount());
}
bool PolymorphicInlineCache::PretendTryGetProperty(
Type *const type,
PropertyCacheOperationInfo * operationInfo)
{
uint inlineCacheIndex = GetInlineCacheIndexForType(type);
return inlineCaches[inlineCacheIndex].PretendTryGetProperty(type, operationInfo);
}
bool PolymorphicInlineCache::PretendTrySetProperty(
Type *const type,
Type *const oldType,
PropertyCacheOperationInfo * operationInfo)
{
uint inlineCacheIndex = GetInlineCacheIndexForType(type);
return inlineCaches[inlineCacheIndex].PretendTrySetProperty(type, oldType, operationInfo);
}
void PolymorphicInlineCache::CopyTo(PropertyId propertyId, ScriptContext* scriptContext, PolymorphicInlineCache *const clone)
{
Assert(clone);
clone->ignoreForEquivalentObjTypeSpec = this->ignoreForEquivalentObjTypeSpec;
clone->cloneForJitTimeUse = this->cloneForJitTimeUse;
for (uint i = 0; i < GetSize(); ++i)
{
Type * type = inlineCaches[i].GetType();
if (type)
{
uint inlineCacheIndex = clone->GetInlineCacheIndexForType(type);
// When copying inline caches from one polymorphic cache to another, types are again hashed to get the corresponding indices in the new polymorphic cache.
// This might lead to collision in the new cache. We need to try to resolve that collision.
if (!PHASE_OFF1(Js::CloneCacheInCollisionPhase))
{
if (!clone->inlineCaches[inlineCacheIndex].IsEmpty() && !clone->inlineCaches[inlineCacheIndex].NeedsToBeRegisteredForStoreFieldInvalidation() && GetSize() != 1)
{
if (clone->inlineCaches[inlineCacheIndex].IsLocal())
{
clone->CloneInlineCacheToEmptySlotInCollision<true, false, false>(type, inlineCacheIndex);
}
else if (clone->inlineCaches[inlineCacheIndex].IsProto())
{
clone->CloneInlineCacheToEmptySlotInCollision<false, true, false>(type, inlineCacheIndex);
}
else
{
clone->CloneInlineCacheToEmptySlotInCollision<false, false, true>(type, inlineCacheIndex);
}
}
}
inlineCaches[i].CopyTo(propertyId, scriptContext, &clone->inlineCaches[inlineCacheIndex]);
clone->UpdateInlineCachesFillInfo(inlineCacheIndex, true /*set*/);
}
}
}
#if DBG_DUMP
void PolymorphicInlineCache::Dump()
{
for (uint i = 0; i < size; ++i)
{
if (!inlineCaches[i].IsEmpty())
{
Output::Print(_u(" %d: "), i);
inlineCaches[i].Dump();
Output::Print(_u("\n"));
}
}
}
#endif
FunctionBodyPolymorphicInlineCache * FunctionBodyPolymorphicInlineCache::New(uint16 size, FunctionBody * functionBody)
{
ScriptContext * scriptContext = functionBody->GetScriptContext();
InlineCache * inlineCaches = AllocatorNewArrayZ(InlineCacheAllocator, scriptContext->GetInlineCacheAllocator(), InlineCache, size);
#ifdef POLY_INLINE_CACHE_SIZE_STATS
scriptContext->GetInlineCacheAllocator()->LogPolyCacheAlloc(size * sizeof(InlineCache));
#endif
FunctionBodyPolymorphicInlineCache * polymorphicInlineCache = RecyclerNewFinalizedLeaf(scriptContext->GetRecycler(), FunctionBodyPolymorphicInlineCache, inlineCaches, size, functionBody);
polymorphicInlineCache->prev = nullptr;
polymorphicInlineCache->next = polymorphicInlineCache->functionBody->GetPolymorphicInlineCachesHead();
if (polymorphicInlineCache->next)
{
polymorphicInlineCache->next->prev = polymorphicInlineCache;
}
polymorphicInlineCache->functionBody->SetPolymorphicInlineCachesHead(polymorphicInlineCache);
return polymorphicInlineCache;
}
void FunctionBodyPolymorphicInlineCache::Finalize(bool isShutdown)
{
if (size == 0)
{
// Already finalized
Assert(!inlineCaches && !prev && !next);
return;
}
if (this->functionBody->GetScriptContext() == nullptr)
{
// If caches were made during ScriptContext initialization, and initialization failed the ScriptContext
// and InlineCacheAllocator will be freed, so we don't need to do anything during finalization
return;
}
uint unregisteredInlineCacheCount = 0;
Assert(inlineCaches && size > 0);
// If we're not shutting down (as in closing the script context), we need to remove our inline caches from
// thread context's invalidation lists, and release memory back to the arena. During script context shutdown,
// we leave everything in place, because the inline cache arena will stay alive until script context is destroyed
// (as in destructor has been called) and thus the invalidation lists are safe to keep references to caches from this
// script context. We will, however, zero all inline caches so that we don't have to process them on subsequent
// collections, which may still happen from other script contexts.
if (isShutdown)
{
#if DBG
for (int i = 0; i < size; i++)
{
inlineCaches[i].Clear();
}
#else
memset(inlineCaches, 0, size * sizeof(InlineCache));
#endif
}
else
{
for (int i = 0; i < size; i++)
{
if (inlineCaches[i].RemoveFromInvalidationList())
{
unregisteredInlineCacheCount++;
}
}
AllocatorDeleteArray(InlineCacheAllocator, this->functionBody->GetScriptContext()->GetInlineCacheAllocator(), size, inlineCaches);
#ifdef POLY_INLINE_CACHE_SIZE_STATS
this->functionBody->GetScriptContext()->GetInlineCacheAllocator()->LogPolyCacheFree(size * sizeof(InlineCache));