-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathTempTracker.cpp
1763 lines (1604 loc) · 62.9 KB
/
TempTracker.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"
/* ===================================================================================
* TempTracker runs the mark temp algorithm. The template parameter provides information
* what are valid temp use, temp transfer, or temp producing operations and what bit to
* set once a symbol def can be marked temp.
*
* NumberTemp mark temp JavascriptNumber creation for math operations, run during deadstore
*
* ObjectTemp mark temp object allocations, run during backward pass so that it can provide
* information to the globopt to install pre op bailout on implicit call while during stack
* allocation objects.
*
* ObjectTempVerify runs a similar mark temp during deadstore in debug mode to assert
* that globopt have install the pre op necessary and a marked temp def is still valid
* as a mark temp
*
* The basic of the mark temp algorithm is very simple: we keep track if we have seen
* any use of a symbol that is not a valid mark temp (the nonTempSyms bitvector)
* and on definition of the symbol, if the all the use allow temp object (not in nonTempSyms
* bitvector) then it is mark them able.
*
* However, the complication comes when the stack object is transferred to another symbol
* and we are in a loop. We need to make sure that the stack object isn't still referred
* by another symbol when we allocate the number/object in the next iteration
*
* For example:
* Loop top:
* s1 = NewScObject
* = s6
* s6 = s1
* Goto Loop top
*
* We cannot mark them this case because when s1 is created, the object might still be
* referred to by s6 from previous iteration, and thus if we mark them we would have
* change the content of s6 as well.
*
* To detect this dependency, we conservatively collect "all" transfers in the pre pass
* of the loop. We have to be conservative to detect reverse dependencies without
* iterating more than 2 times for the loop.
* =================================================================================== */
JitArenaAllocator *
TempTrackerBase::GetAllocator() const
{
return nonTempSyms.GetAllocator();
}
TempTrackerBase::TempTrackerBase(JitArenaAllocator * alloc, bool inLoop)
: nonTempSyms(alloc), tempTransferredSyms(alloc)
{
if (inLoop)
{
tempTransferDependencies = HashTable<BVSparse<JitArenaAllocator> *>::New(alloc, 16);
}
else
{
tempTransferDependencies = nullptr;
}
}
TempTrackerBase::~TempTrackerBase()
{
if (this->tempTransferDependencies != nullptr)
{
JitArenaAllocator * alloc = this->GetAllocator();
FOREACH_HASHTABLE_ENTRY(BVSparse<JitArenaAllocator> *, bucket, this->tempTransferDependencies)
{
JitAdelete(alloc, bucket.element);
}
NEXT_HASHTABLE_ENTRY;
this->tempTransferDependencies->Delete();
}
}
void
TempTrackerBase::MergeData(TempTrackerBase * fromData, bool deleteData)
{
this->nonTempSyms.Or(&fromData->nonTempSyms);
this->tempTransferredSyms.Or(&fromData->tempTransferredSyms);
this->MergeDependencies(this->tempTransferDependencies, fromData->tempTransferDependencies, deleteData);
if (this->tempTransferDependencies)
{
FOREACH_HASHTABLE_ENTRY(BVSparse<JitArenaAllocator> *, bucket, this->tempTransferDependencies)
{
if (bucket.element->Test(&this->nonTempSyms))
{
this->nonTempSyms.Set(bucket.value);
}
} NEXT_HASHTABLE_ENTRY;
}
}
void
TempTrackerBase::AddTransferDependencies(int sourceId, SymID dstSymID, HashTable<BVSparse<JitArenaAllocator> *> * dependencies)
{
// Add to the transfer dependencies set
BVSparse<JitArenaAllocator> ** pBVSparse = dependencies->FindOrInsertNew(sourceId);
if (*pBVSparse == nullptr)
{
*pBVSparse = JitAnew(this->GetAllocator(), BVSparse<JitArenaAllocator>, this->GetAllocator());
}
AddTransferDependencies(*pBVSparse, dstSymID);
}
void
TempTrackerBase::AddTransferDependencies(BVSparse<JitArenaAllocator> * bv, SymID dstSymID)
{
bv->Set(dstSymID);
// Add the indirect transfers (always from tempTransferDependencies)
BVSparse<JitArenaAllocator> *dstBVSparse = this->tempTransferDependencies->GetAndClear(dstSymID);
if (dstBVSparse != nullptr)
{
bv->Or(dstBVSparse);
JitAdelete(this->GetAllocator(), dstBVSparse);
}
}
template <typename T>
TempTracker<T>::TempTracker(JitArenaAllocator * alloc, bool inLoop):
T(alloc, inLoop)
{
}
template <typename T>
void
TempTracker<T>::MergeData(TempTracker<T> * fromData, bool deleteData)
{
TempTrackerBase::MergeData(fromData, deleteData);
T::MergeData(fromData, deleteData);
if (deleteData)
{
JitAdelete(this->GetAllocator(), fromData);
}
}
void
TempTrackerBase::OrHashTableOfBitVector(HashTable<BVSparse<JitArenaAllocator> *> * toData, HashTable<BVSparse<JitArenaAllocator> *> *& fromData, bool deleteData)
{
Assert(toData != nullptr);
Assert(fromData != nullptr);
toData->Or(fromData,
[=](BVSparse<JitArenaAllocator> * bv1, BVSparse<JitArenaAllocator> * bv2) -> BVSparse<JitArenaAllocator> *
{
if (bv1 == nullptr)
{
if (deleteData)
{
return bv2;
}
return bv2->CopyNew(this->GetAllocator());
}
bv1->Or(bv2);
if (deleteData)
{
JitAdelete(this->GetAllocator(), bv2);
}
return bv1;
});
if (deleteData)
{
fromData->Delete();
fromData = nullptr;
}
}
void
TempTrackerBase::MergeDependencies(HashTable<BVSparse<JitArenaAllocator> *> * toData, HashTable<BVSparse<JitArenaAllocator> *> *& fromData, bool deleteData)
{
if (fromData != nullptr)
{
if (toData != nullptr)
{
OrHashTableOfBitVector(toData, fromData, deleteData);
}
else if (deleteData)
{
FOREACH_HASHTABLE_ENTRY(BVSparse<JitArenaAllocator> *, bucket, fromData)
{
JitAdelete(this->GetAllocator(), bucket.element);
}
NEXT_HASHTABLE_ENTRY;
fromData->Delete();
fromData = nullptr;
}
}
}
#if DBG_DUMP
void
TempTrackerBase::Dump(char16 const * traceName)
{
Output::Print(_u("%s: Non temp syms:"), traceName);
this->nonTempSyms.Dump();
Output::Print(_u("%s: Temp transferred syms:"), traceName);
this->tempTransferredSyms.Dump();
if (this->tempTransferDependencies != nullptr)
{
Output::Print(_u("%s: Temp transfer dependencies:\n"), traceName);
this->tempTransferDependencies->Dump();
}
}
#endif
template <typename T>
void
TempTracker<T>::ProcessUse(StackSym * sym, BackwardPass * backwardPass)
{
// Don't care about type specialized syms
if (!sym->IsVar())
{
return;
}
IR::Instr * instr = backwardPass->currentInstr;
SymID usedSymID = sym->m_id;
bool isTempPropertyTransferStore = T::IsTempPropertyTransferStore(instr, backwardPass);
bool isTempUse = isTempPropertyTransferStore || T::IsTempUse(instr, sym, backwardPass);
if (!isTempUse)
{
this->nonTempSyms.Set(usedSymID);
}
#if DBG
if (T::DoTrace(backwardPass))
{
Output::Print(_u("%s: %8s%4sTemp Use (s%-3d): "), T::GetTraceName(),
backwardPass->IsPrePass() ? _u("Prepass ") : _u(""), isTempUse ? _u("") : _u("Non "), usedSymID);
instr->DumpSimple();
Output::Flush();
}
#endif
if (T::IsTempTransfer(instr))
{
this->tempTransferredSyms.Set(usedSymID);
// Track dependencies if we are in loop only
if (this->tempTransferDependencies != nullptr)
{
IR::Opnd * dstOpnd = instr->GetDst();
if (dstOpnd->IsRegOpnd())
{
SymID dstSymID = dstOpnd->AsRegOpnd()->m_sym->m_id;
if (dstSymID != usedSymID)
{
// Record that the usedSymID may propagate to dstSymID and all the symbols
// that it may propagate to as well
this->AddTransferDependencies(usedSymID, dstSymID, this->tempTransferDependencies);
#if DBG_DUMP
if (T::DoTrace(backwardPass))
{
Output::Print(_u("%s: %8s s%d -> s%d: "), T::GetTraceName(),
backwardPass->IsPrePass() ? _u("Prepass ") : _u(""), dstSymID, usedSymID);
(*this->tempTransferDependencies->Get(usedSymID))->Dump();
}
#endif
}
}
}
}
if (isTempPropertyTransferStore)
{
this->tempTransferredSyms.Set(usedSymID);
PropertySym * propertySym = instr->GetDst()->AsSymOpnd()->m_sym->AsPropertySym();
this->PropagateTempPropertyTransferStoreDependencies(usedSymID, propertySym, backwardPass);
#if DBG_DUMP
if (T::DoTrace(backwardPass) && this->tempTransferDependencies)
{
Output::Print(_u("%s: %8s (PropId:%d)+[] -> s%d: "), T::GetTraceName(),
backwardPass->IsPrePass() ? _u("Prepass ") : _u(""), propertySym->m_propertyId, usedSymID);
BVSparse<JitArenaAllocator> ** transferDependencies = this->tempTransferDependencies->Get(usedSymID);
if (transferDependencies)
{
(*transferDependencies)->Dump();
}
else
{
Output::Print(_u("[]\n"));
}
}
#endif
}
};
template <typename T>
void
TempTracker<T>::DisallowMarkTempAcrossYield(BVSparse<JitArenaAllocator>* bytecodeUpwardExposed)
{
if (bytecodeUpwardExposed != nullptr)
{
this->nonTempSyms.Or(bytecodeUpwardExposed);
}
}
template <typename T>
void
TempTracker<T>::MarkTemp(StackSym * sym, BackwardPass * backwardPass)
{
// Don't care about type specialized syms
Assert(sym->IsVar());
IR::Instr * instr = backwardPass->currentInstr;
BOOLEAN nonTemp = this->nonTempSyms.TestAndClear(sym->m_id);
BOOLEAN isTempTransferred;
BVSparse<JitArenaAllocator> * bvTempTransferDependencies = nullptr;
bool const isTransferOperation =
T::IsTempTransfer(instr)
|| T::IsTempPropertyTransferLoad(instr, backwardPass)
|| T::IsTempIndirTransferLoad(instr, backwardPass);
if (this->tempTransferDependencies != nullptr)
{
// Since we don't iterate "while (!changed)" in loops, we don't have complete accurate dataflow
// for loop carried dependencies. So don't clear the dependency transfer info. WOOB:1121525
// Check if this dst is transferred (assigned) to another symbol
if (isTransferOperation)
{
isTempTransferred = this->tempTransferredSyms.Test(sym->m_id);
}
else
{
isTempTransferred = this->tempTransferredSyms.TestAndClear(sym->m_id);
}
// We only need to look at the dependencies if we are in a loop because of the back edge
// Also we don't need to if we are in pre pass
if (isTempTransferred)
{
if (!backwardPass->IsPrePass())
{
if (isTransferOperation)
{
// Transfer operation, load but not clear the information
BVSparse<JitArenaAllocator> **pBv = this->tempTransferDependencies->Get(sym->m_id);
if (pBv)
{
bvTempTransferDependencies = *pBv;
}
}
else
{
// Non transfer operation, load and clear the information and the dst value is replaced
bvTempTransferDependencies = this->tempTransferDependencies->GetAndClear(sym->m_id);
}
}
else if (!isTransferOperation)
{
// In pre pass, and not a transfer operation (just an assign). We can clear the dependency info
// and not look at it.
this->tempTransferDependencies->Clear(sym->m_id);
}
}
}
else
{
isTempTransferred = this->tempTransferredSyms.TestAndClear(sym->m_id);
}
// Reset the dst is temp bit (we set it optimistically on the loop pre pass)
bool dstIsTemp = false;
bool dstIsTempTransferred = false;
if (nonTemp)
{
#if DBG_DUMP
if (T::DoTrace(backwardPass) && !backwardPass->IsPrePass() && T::CanMarkTemp(instr, backwardPass))
{
Output::Print(_u("%s: Not temp (s%-03d):"), T::GetTraceName(), sym->m_id);
instr->DumpSimple();
}
#endif
}
else if (backwardPass->IsPrePass())
{
// On pre pass, we don't have complete information about whether it is tempable or
// not from the back edge. If we already discovered that it is not a temp (above), then
// we don't mark it, other wise, assume that it is okay to be tempable and have the
// second pass set the bit correctly. The only works on dependency chain that is in order
// e.g.
// s1 = Add
// s2 = s1
// s3 = s2
// The dependencies tracking to catch the case whether the dependency chain is out of order
// e.g
// s1 = Add
// s3 = s2
// s2 = s3
Assert(isTransferOperation == T::IsTempTransfer(instr)
|| T::IsTempPropertyTransferLoad(instr, backwardPass)
|| T::IsTempIndirTransferLoad(instr, backwardPass));
if (isTransferOperation)
{
dstIsTemp = true;
}
}
else if (T::CanMarkTemp(instr, backwardPass))
{
dstIsTemp = true;
if (isTempTransferred)
{
// Track whether the dst is transferred or not, and allocate separate stack slot for them
// so that another dst will not overrides the value
dstIsTempTransferred = true;
// The temp is aliased, need to trace if there is another use of the set of aliased
// sym that is still live so that we won't mark them this symbol and destroy the value
if (bvTempTransferDependencies != nullptr)
{
// Inside a loop we need to track if any of the reg that we transferred to is still live
// s1 = Add
// = s2
// s2 = s1
// Since s2 is still live on the next iteration when we reassign s1, making s1 a temp
// will cause the value of s2 to change before it's use.
// The upwardExposedUses are the live regs, check if it intersect with the set
// of dependency or not.
#if DBG_DUMP
if (T::DoTrace(backwardPass) && Js::Configuration::Global.flags.Verbose)
{
Output::Print(_u("%s: Loop mark temp check instr:\n"), T::GetTraceName());
instr->DumpSimple();
Output::Print(_u("Transfer dependencies: "));
bvTempTransferDependencies->Dump();
Output::Print(_u("Upward exposed Uses : "));
backwardPass->currentBlock->upwardExposedUses->Dump();
Output::Print(_u("\n"));
}
#endif
BVSparse<JitArenaAllocator> * upwardExposedUses = backwardPass->currentBlock->upwardExposedUses;
bool hasExposedDependencies = bvTempTransferDependencies->Test(upwardExposedUses)
|| T::HasExposedFieldDependencies(bvTempTransferDependencies, backwardPass);
if (hasExposedDependencies)
{
#if DBG_DUMP
if (T::DoTrace(backwardPass))
{
Output::Print(_u("%s: Not temp (s%-03d): "), T::GetTraceName(), sym->m_id);
instr->DumpSimple();
Output::Print(_u(" Transferred exposed uses: "));
JitArenaAllocator tempAllocator(_u("temp"), this->GetAllocator()->GetPageAllocator(), Js::Throw::OutOfMemory);
bvTempTransferDependencies->AndNew(upwardExposedUses, &tempAllocator)->Dump();
}
#endif
dstIsTemp = false;
dstIsTempTransferred = false;
#if DBG
if (IsObjectTempVerify<T>())
{
dstIsTemp = ObjectTempVerify::DependencyCheck(instr, bvTempTransferDependencies, backwardPass);
}
#endif
// Only ObjectTmepVerify would do the do anything here. All other returns false
}
}
}
}
T::SetDstIsTemp(dstIsTemp, dstIsTempTransferred, instr, backwardPass);
}
NumberTemp::NumberTemp(JitArenaAllocator * alloc, bool inLoop)
: TempTrackerBase(alloc, inLoop), elemLoadDependencies(alloc), nonTempElemLoad(false),
upwardExposedMarkTempObjectLiveFields(alloc), upwardExposedMarkTempObjectSymsProperties(nullptr)
{
propertyIdsTempTransferDependencies = inLoop ? HashTable<BVSparse<JitArenaAllocator> *>::New(alloc, 16) : nullptr;
}
void
NumberTemp::MergeData(NumberTemp * fromData, bool deleteData)
{
nonTempElemLoad = nonTempElemLoad || fromData->nonTempElemLoad;
if (!nonTempElemLoad) // Don't bother merging other data if we already have a nonTempElemLoad
{
if (IsInLoop())
{
// in loop
elemLoadDependencies.Or(&fromData->elemLoadDependencies);
}
MergeDependencies(propertyIdsTempTransferDependencies, fromData->propertyIdsTempTransferDependencies, deleteData);
if (fromData->upwardExposedMarkTempObjectSymsProperties)
{
if (upwardExposedMarkTempObjectSymsProperties)
{
OrHashTableOfBitVector(upwardExposedMarkTempObjectSymsProperties, fromData->upwardExposedMarkTempObjectSymsProperties, deleteData);
}
else if (deleteData)
{
upwardExposedMarkTempObjectSymsProperties = fromData->upwardExposedMarkTempObjectSymsProperties;
fromData->upwardExposedMarkTempObjectSymsProperties = nullptr;
}
else
{
upwardExposedMarkTempObjectSymsProperties = HashTable<BVSparse<JitArenaAllocator> *>::New(this->GetAllocator(), 16);
OrHashTableOfBitVector(upwardExposedMarkTempObjectSymsProperties, fromData->upwardExposedMarkTempObjectSymsProperties, deleteData);
}
}
upwardExposedMarkTempObjectLiveFields.Or(&fromData->upwardExposedMarkTempObjectLiveFields);
}
}
bool
NumberTemp::IsTempUse(IR::Instr * instr, Sym * sym, BackwardPass * backwardPass)
{
Js::OpCode opcode = instr->m_opcode;
if (OpCodeAttr::NonTempNumberSources(opcode)
|| (OpCodeAttr::TempNumberTransfer(opcode) && !instr->dstIsTempNumber))
{
// For TypedArray stores, we don't store the Var object, so MarkTemp is valid
if (opcode != Js::OpCode::StElemI_A
|| !instr->GetDst()->AsIndirOpnd()->GetBaseOpnd()->GetValueType().IsLikelyOptimizedTypedArray())
{
// Mark the symbol as non-tempable if the instruction doesn't allow temp sources,
// or it is transferred to a non-temp dst
return false;
}
}
return true;
}
bool
NumberTemp::IsTempTransfer(IR::Instr * instr)
{
return OpCodeAttr::TempNumberTransfer(instr->m_opcode);
}
bool
NumberTemp::IsTempProducing(IR::Instr * instr)
{
Js::OpCode opcode = instr->m_opcode;
if (OpCodeAttr::TempNumberProducing(opcode))
{
return true;
}
// Loads from float typedArrays usually require a conversion to Var, which we can MarkTemp.
if (opcode == Js::OpCode::LdElemI_A)
{
const ValueType baseValueType(instr->GetSrc1()->AsIndirOpnd()->GetBaseOpnd()->GetValueType());
if (baseValueType.IsLikelyObject()
&& (baseValueType.GetObjectType() == ObjectType::Float32Array
|| baseValueType.GetObjectType() == ObjectType::Float64Array))
{
return true;
}
}
return false;
}
bool
NumberTemp::CanMarkTemp(IR::Instr * instr, BackwardPass * backwardPass)
{
if (IsTempTransfer(instr) || IsTempProducing(instr))
{
return true;
}
// REVIEW: this is added a long time ago, and I am not sure what this is for any more.
if (OpCodeAttr::InlineCallInstr(instr->m_opcode))
{
return true;
}
if (NumberTemp::IsTempIndirTransferLoad(instr, backwardPass)
|| NumberTemp::IsTempPropertyTransferLoad(instr, backwardPass))
{
return true;
}
// the opcode is not temp producing or a transfer, this is not a tmp
// Also mark calls which may get inlined.
return false;
}
void
NumberTemp::ProcessInstr(IR::Instr * instr, BackwardPass * backwardPass)
{
#if DBG
if (instr->m_opcode == Js::OpCode::BailOnNoProfile)
{
// If we see BailOnNoProfile, we shouldn't have any successor to have any non temp syms
Assert(!this->nonTempElemLoad);
Assert(this->nonTempSyms.IsEmpty());
Assert(this->tempTransferredSyms.IsEmpty());
Assert(this->elemLoadDependencies.IsEmpty());
Assert(this->upwardExposedMarkTempObjectLiveFields.IsEmpty());
}
#endif
// We don't get to process all dst in MarkTemp. Do it here for the upwardExposedMarkTempObjectLiveFields
if (!this->DoMarkTempNumbersOnTempObjects(backwardPass))
{
return;
}
IR::Opnd * dst = instr->GetDst();
if (dst == nullptr || !dst->IsRegOpnd())
{
return;
}
StackSym * dstSym = dst->AsRegOpnd()->m_sym;
if (!dstSym->IsVar())
{
dstSym = dstSym->GetVarEquivSym(nullptr);
if (dstSym == nullptr)
{
return;
}
}
SymID dstSymId = dstSym->m_id;
if (this->upwardExposedMarkTempObjectSymsProperties)
{
// We are assigning to dstSym, it no longer has upward exposed use, get the information and clear it from the hash table
BVSparse<JitArenaAllocator> * dstBv = this->upwardExposedMarkTempObjectSymsProperties->GetAndClear(dstSymId);
if (dstBv)
{
// Clear the upward exposed live fields of all the property sym id associated to dstSym
this->upwardExposedMarkTempObjectLiveFields.Minus(dstBv);
if (ObjectTemp::IsTempTransfer(instr) && instr->GetSrc1()->IsRegOpnd())
{
// If it is transfer, copy the dst info to the src
SymID srcStackSymId = instr->GetSrc1()->AsRegOpnd()->m_sym->AsStackSym()->m_id;
SymTable * symTable = backwardPass->func->m_symTable;
FOREACH_BITSET_IN_SPARSEBV(index, dstBv)
{
PropertySym * propertySym = symTable->FindPropertySym(srcStackSymId, index);
if (propertySym)
{
this->upwardExposedMarkTempObjectLiveFields.Set(propertySym->m_id);
}
}
NEXT_BITSET_IN_SPARSEBV;
BVSparse<JitArenaAllocator> ** srcBv = this->upwardExposedMarkTempObjectSymsProperties->FindOrInsert(dstBv, srcStackSymId);
if (srcBv)
{
(*srcBv)->Or(dstBv);
JitAdelete(this->GetAllocator(), dstBv);
}
}
else
{
JitAdelete(this->GetAllocator(), dstBv);
}
}
}
}
void
NumberTemp::SetDstIsTemp(bool dstIsTemp, bool dstIsTempTransferred, IR::Instr * instr, BackwardPass * backwardPass)
{
Assert(dstIsTemp || !dstIsTempTransferred);
Assert(!instr->dstIsTempNumberTransferred);
instr->dstIsTempNumber = dstIsTemp;
instr->dstIsTempNumberTransferred = dstIsTempTransferred;
#if DBG_DUMP
if (!backwardPass->IsPrePass() && IsTempProducing(instr))
{
backwardPass->numMarkTempNumber += dstIsTemp;
backwardPass->numMarkTempNumberTransferred += dstIsTempTransferred;
}
#endif
}
bool
NumberTemp::IsTempPropertyTransferLoad(IR::Instr * instr, BackwardPass * backwardPass)
{
if (DoMarkTempNumbersOnTempObjects(backwardPass))
{
switch (instr->m_opcode)
{
case Js::OpCode::LdFld:
case Js::OpCode::LdFldForTypeOf:
case Js::OpCode::LdMethodFld:
case Js::OpCode::LdFldForCallApplyTarget:
case Js::OpCode::LdMethodFromFlags:
{
// Only care about load from possible stack allocated object.
return instr->GetSrc1()->CanStoreTemp();
}
};
// All other opcode shouldn't have sym opnd that can store temp, See ObjectTemp::IsTempUseOpCodeSym.
Assert(instr->GetSrc1() == nullptr
|| instr->GetDst() == nullptr // this isn't a value loading instruction
|| instr->GetSrc1()->IsIndirOpnd() // this is detected in IsTempIndirTransferLoad
|| !instr->GetSrc1()->CanStoreTemp());
}
return false;
}
bool
NumberTemp::IsTempPropertyTransferStore(IR::Instr * instr, BackwardPass * backwardPass)
{
if (DoMarkTempNumbersOnTempObjects(backwardPass))
{
switch (instr->m_opcode)
{
case Js::OpCode::InitFld:
case Js::OpCode::StFld:
case Js::OpCode::StFldStrict:
{
IR::Opnd * dst = instr->GetDst();
Assert(dst->IsSymOpnd());
if (!dst->CanStoreTemp())
{
return false;
}
// We don't mark temp store of numeric properties (e.g. object literal { 86: foo });
// This should only happen for InitFld, as StFld should have changed to StElem
PropertySym *propertySym = dst->AsSymOpnd()->m_sym->AsPropertySym();
SymID propertySymId = this->GetRepresentativePropertySymId(propertySym, backwardPass);
return !this->nonTempSyms.Test(propertySymId) &&
!instr->m_func->GetThreadContextInfo()->IsNumericProperty(propertySym->m_propertyId);
}
};
// All other opcode shouldn't have sym opnd that can store temp, see ObjectTemp::IsTempUseOpCodeSym.
// We also never mark the dst indir as can store temp for StElemI_A because we don't know what property
// it is storing in (or it could be an array index).
Assert(instr->GetDst() == nullptr || !instr->GetDst()->CanStoreTemp());
}
return false;
}
bool
NumberTemp::IsTempIndirTransferLoad(IR::Instr * instr, BackwardPass * backwardPass)
{
if (DoMarkTempNumbersOnTempObjects(backwardPass))
{
if (instr->m_opcode == Js::OpCode::LdElemI_A)
{
// If the index is an int, then we don't care about the non-temp use
IR::Opnd * src1Opnd = instr->GetSrc1();
IR::RegOpnd * indexOpnd = src1Opnd->AsIndirOpnd()->GetIndexOpnd();
if (indexOpnd && (indexOpnd->m_sym->m_isNotNumber || !indexOpnd->GetValueType().IsInt()))
{
return src1Opnd->CanStoreTemp();
}
}
else
{
// All other opcode shouldn't have sym opnd that can store temp, See ObjectTemp::IsTempUseOpCodeSym.
Assert(instr->GetSrc1() == nullptr || instr->GetSrc1()->IsSymOpnd()
|| !instr->GetSrc1()->CanStoreTemp());
}
}
return false;
}
void
NumberTemp::PropagateTempPropertyTransferStoreDependencies(SymID usedSymID, PropertySym * propertySym, BackwardPass * backwardPass)
{
Assert(!this->nonTempElemLoad);
upwardExposedMarkTempObjectLiveFields.Clear(propertySym->m_id);
if (!this->IsInLoop())
{
// Don't need to track dependencies outside of loop, as we already marked the
// use as temp transfer already and we won't have a case where the "dst" is reused again (outside of loop)
return;
}
Assert(this->tempTransferDependencies != nullptr);
SymID dstSymID = this->GetRepresentativePropertySymId(propertySym, backwardPass);
AddTransferDependencies(usedSymID, dstSymID, this->tempTransferDependencies);
Js::PropertyId storedPropertyId = propertySym->m_propertyId;
// The symbol this properties are transferred to
BVSparse<JitArenaAllocator> ** pPropertyTransferDependencies = this->propertyIdsTempTransferDependencies->Get(storedPropertyId);
BVSparse<JitArenaAllocator> * transferDependencies = nullptr;
if (pPropertyTransferDependencies == nullptr)
{
if (elemLoadDependencies.IsEmpty())
{
// No dependencies to transfer
return;
}
transferDependencies = &elemLoadDependencies;
}
else
{
transferDependencies = *pPropertyTransferDependencies;
}
BVSparse<JitArenaAllocator> ** pBVSparse = this->tempTransferDependencies->FindOrInsertNew(usedSymID);
if (*pBVSparse == nullptr)
{
*pBVSparse = transferDependencies->CopyNew(this->GetAllocator());
}
else
{
(*pBVSparse)->Or(transferDependencies);
}
if (transferDependencies != &elemLoadDependencies)
{
// Always include the element load dependencies as well
(*pBVSparse)->Or(&elemLoadDependencies);
}
// Track the propertySym as well for the case where the dependence is not carried by the use
// Loop1
// o.x = e
// Loop2
// f = o.x
// = f
// e = e + blah
// Here, although we can detect that e and f has dependent relationship, f's life time doesn't cross with e's.
// But o.x will keep the value of e alive, so e can't be mark temp because o.x is still in use (not f)
// We will add the property sym int he dependency set and check with the upward exposed mark temp object live fields
// that we keep track of in NumberTemp
(*pBVSparse)->Set(propertySym->m_id);
}
SymID
NumberTemp::GetRepresentativePropertySymId(PropertySym * propertySym, BackwardPass * backwardPass)
{
// Since we don't track alias with objects, all property accesses are all grouped together.
// Use a single property sym id to represent a propertyId to track dependencies.
SymID symId = SymID_Invalid;
Js::PropertyId propertyId = propertySym->m_propertyId;
if (!backwardPass->numberTempRepresentativePropertySym->TryGetValue(propertyId, &symId))
{
symId = propertySym->m_id;
backwardPass->numberTempRepresentativePropertySym->Add(propertyId, symId);
}
return symId;
}
void
NumberTemp::ProcessIndirUse(IR::IndirOpnd * indirOpnd, IR::Instr * instr, BackwardPass * backwardPass)
{
Assert(backwardPass->DoMarkTempNumbersOnTempObjects());
if (!NumberTemp::IsTempIndirTransferLoad(instr, backwardPass))
{
return;
}
bool isTempUse = instr->dstIsTempNumber;
if (!isTempUse)
{
nonTempElemLoad = true;
}
else if (this->IsInLoop())
{
// We didn't already detect non temp use of this property id. so we should track the dependencies in loops
IR::Opnd * dstOpnd = instr->GetDst();
Assert(dstOpnd->IsRegOpnd());
SymID dstSymID = dstOpnd->AsRegOpnd()->m_sym->m_id;
// Use the no property id as a place holder for elem dependencies
AddTransferDependencies(&elemLoadDependencies, dstSymID);
#if DBG_DUMP
if (NumberTemp::DoTrace(backwardPass))
{
Output::Print(_u("%s: %8s s%d -> []: "), NumberTemp::GetTraceName(),
backwardPass->IsPrePass() ? _u("Prepass ") : _u(""), dstSymID);
elemLoadDependencies.Dump();
}
#endif
}
#if DBG_DUMP
if (NumberTemp::DoTrace(backwardPass))
{
Output::Print(_u("%s: %8s%4sTemp Use ([] )"), NumberTemp::GetTraceName(),
backwardPass->IsPrePass() ? _u("Prepass ") : _u(""), isTempUse ? _u("") : _u("Non "));
instr->DumpSimple();
}
#endif
}
void
NumberTemp::ProcessPropertySymUse(IR::SymOpnd * symOpnd, IR::Instr * instr, BackwardPass * backwardPass)
{
Assert(backwardPass->DoMarkTempNumbersOnTempObjects());
// We only care about instruction that may transfer the property value
if (!NumberTemp::IsTempPropertyTransferLoad(instr, backwardPass))
{
return;
}
PropertySym * propertySym = symOpnd->m_sym->AsPropertySym();
upwardExposedMarkTempObjectLiveFields.Set(propertySym->m_id);
if (upwardExposedMarkTempObjectSymsProperties == nullptr)
{
upwardExposedMarkTempObjectSymsProperties = HashTable<BVSparse<JitArenaAllocator> *>::New(this->GetAllocator(), 16);
}
BVSparse<JitArenaAllocator> ** bv = upwardExposedMarkTempObjectSymsProperties->FindOrInsertNew(propertySym->m_stackSym->m_id);
if (*bv == nullptr)
{
*bv = JitAnew(this->GetAllocator(), BVSparse<JitArenaAllocator>, this->GetAllocator());
}
(*bv)->Set(propertySym->m_propertyId);
SymID propertySymId = this->GetRepresentativePropertySymId(propertySym, backwardPass);
bool isTempUse = instr->dstIsTempNumber;
if (!isTempUse)
{
// Use of the value is non temp, track the property ID's property representative sym so we don't mark temp
// assignment to this property on stack objects.
this->nonTempSyms.Set(propertySymId);
}
else if (this->IsInLoop() && !this->nonTempSyms.Test(propertySymId))
{
// We didn't already detect non temp use of this property id. so we should track the dependencies in loops
IR::Opnd * dstOpnd = instr->GetDst();
Assert(dstOpnd->IsRegOpnd());
SymID dstSymID = dstOpnd->AsRegOpnd()->m_sym->m_id;
AddTransferDependencies(propertySym->m_propertyId, dstSymID, this->propertyIdsTempTransferDependencies);
#if DBG_DUMP
if (NumberTemp::DoTrace(backwardPass))
{
Output::Print(_u("%s: %8s s%d -> PropId:%d: "), NumberTemp::GetTraceName(),
backwardPass->IsPrePass() ? _u("Prepass ") : _u(""), dstSymID, propertySym->m_propertyId);
(*this->propertyIdsTempTransferDependencies->Get(propertySym->m_propertyId))->Dump();
}
#endif
}
#if DBG_DUMP
if (NumberTemp::DoTrace(backwardPass))
{
Output::Print(_u("%s: %8s%4sTemp Use (PropId:%d)"), NumberTemp::GetTraceName(),
backwardPass->IsPrePass() ? _u("Prepass ") : _u(""), isTempUse ? _u("") : _u("Non "), propertySym->m_propertyId);
instr->DumpSimple();
}
#endif
}
bool
NumberTemp::HasExposedFieldDependencies(BVSparse<JitArenaAllocator> * bvTempTransferDependencies, BackwardPass * backwardPass)
{
if (!DoMarkTempNumbersOnTempObjects(backwardPass))
{
return false;
}
return bvTempTransferDependencies->Test(&upwardExposedMarkTempObjectLiveFields);
}
bool
NumberTemp::DoMarkTempNumbersOnTempObjects(BackwardPass * backwardPass) const
{
return backwardPass->DoMarkTempNumbersOnTempObjects() && !this->nonTempElemLoad;
}
#if DBG
void
NumberTemp::Dump(char16 const * traceName)
{
if (nonTempElemLoad)
{
Output::Print(_u("%s: Has Non Temp Elem Load\n"), traceName);
}
else
{
Output::Print(_u("%s: Non Temp Syms"), traceName);
this->nonTempSyms.Dump();
if (this->propertyIdsTempTransferDependencies != nullptr)
{
Output::Print(_u("%s: Temp transfer propertyId dependencies:\n"), traceName);
this->propertyIdsTempTransferDependencies->Dump();
}
}
}
#endif
//=================================================================================================
// ObjectTemp
//=================================================================================================
bool
ObjectTemp::IsTempUse(IR::Instr * instr, Sym * sym, BackwardPass * backwardPass)
{
Js::OpCode opcode = instr->m_opcode;
// If the opcode has implicit call and the profile say we have implicit call, then it is not a temp use
// TODO: More precise implicit call tracking
if (instr->HasAnyImplicitCalls()