-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathGlobOptBlockData.cpp
1770 lines (1568 loc) · 62.6 KB
/
GlobOptBlockData.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 Corporation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "Backend.h"
void
GlobOptBlockData::NullOutBlockData(GlobOpt* globOpt, Func* func)
{
this->globOpt = globOpt;
this->symToValueMap = nullptr;
this->exprToValueMap = nullptr;
this->liveFields = nullptr;
this->maybeWrittenTypeSyms = nullptr;
this->isTempSrc = nullptr;
this->liveVarSyms = nullptr;
this->liveInt32Syms = nullptr;
this->liveLossyInt32Syms = nullptr;
this->liveFloat64Syms = nullptr;
this->argObjSyms = nullptr;
this->maybeTempObjectSyms = nullptr;
this->canStoreTempObjectSyms = nullptr;
this->valuesToKillOnCalls = nullptr;
this->inductionVariables = nullptr;
this->availableIntBoundChecks = nullptr;
this->callSequence = nullptr;
this->startCallCount = 0;
this->argOutCount = 0;
this->totalOutParamCount = 0;
this->inlinedArgOutSize = 0;
this->hasCSECandidates = false;
this->curFunc = func;
this->stackLiteralInitFldDataMap = nullptr;
if (this->capturedValues)
{
this->capturedValues->DecrementRefCount();
}
this->capturedValues = nullptr;
this->changedSyms = nullptr;
this->OnDataUnreferenced();
}
void
GlobOptBlockData::InitBlockData(GlobOpt* globOpt, Func* func)
{
this->globOpt = globOpt;
JitArenaAllocator *const alloc = this->globOpt->alloc;
this->symToValueMap = GlobHashTable::New(alloc, 64);
this->exprToValueMap = ExprHashTable::New(alloc, 64);
this->liveFields = JitAnew(alloc, BVSparse<JitArenaAllocator>, alloc);
this->liveArrayValues = JitAnew(alloc, BVSparse<JitArenaAllocator>, alloc);
this->isTempSrc = JitAnew(alloc, BVSparse<JitArenaAllocator>, alloc);
this->liveVarSyms = JitAnew(alloc, BVSparse<JitArenaAllocator>, alloc);
this->liveInt32Syms = JitAnew(alloc, BVSparse<JitArenaAllocator>, alloc);
this->liveLossyInt32Syms = JitAnew(alloc, BVSparse<JitArenaAllocator>, alloc);
this->liveFloat64Syms = JitAnew(alloc, BVSparse<JitArenaAllocator>, alloc);
this->argObjSyms = JitAnew(alloc, BVSparse<JitArenaAllocator>, alloc);
this->maybeTempObjectSyms = nullptr;
this->canStoreTempObjectSyms = nullptr;
this->valuesToKillOnCalls = JitAnew(alloc, ValueSet, alloc);
if(this->globOpt->DoBoundCheckHoist())
{
this->inductionVariables = this->globOpt->IsLoopPrePass() ? JitAnew(alloc, InductionVariableSet, alloc) : nullptr;
this->availableIntBoundChecks = JitAnew(alloc, IntBoundCheckSet, alloc);
}
this->maybeWrittenTypeSyms = nullptr;
this->callSequence = nullptr;
this->startCallCount = 0;
this->argOutCount = 0;
this->totalOutParamCount = 0;
this->inlinedArgOutSize = 0;
this->hasCSECandidates = false;
this->curFunc = func;
this->stackLiteralInitFldDataMap = nullptr;
this->changedSyms = JitAnew(alloc, BVSparse<JitArenaAllocator>, alloc);
this->OnDataInitialized(alloc);
}
void
GlobOptBlockData::ReuseBlockData(GlobOptBlockData *fromData)
{
this->globOpt = fromData->globOpt;
// Reuse dead map
this->symToValueMap = fromData->symToValueMap;
this->exprToValueMap = fromData->exprToValueMap;
this->liveFields = fromData->liveFields;
this->liveArrayValues = fromData->liveArrayValues;
this->maybeWrittenTypeSyms = fromData->maybeWrittenTypeSyms;
this->isTempSrc = fromData->isTempSrc;
this->liveVarSyms = fromData->liveVarSyms;
this->liveInt32Syms = fromData->liveInt32Syms;
this->liveLossyInt32Syms = fromData->liveLossyInt32Syms;
this->liveFloat64Syms = fromData->liveFloat64Syms;
if (this->globOpt->TrackArgumentsObject())
{
this->argObjSyms = fromData->argObjSyms;
}
this->maybeTempObjectSyms = fromData->maybeTempObjectSyms;
this->canStoreTempObjectSyms = fromData->canStoreTempObjectSyms;
this->curFunc = fromData->curFunc;
this->valuesToKillOnCalls = fromData->valuesToKillOnCalls;
this->inductionVariables = fromData->inductionVariables;
this->availableIntBoundChecks = fromData->availableIntBoundChecks;
this->callSequence = fromData->callSequence;
this->startCallCount = fromData->startCallCount;
this->argOutCount = fromData->argOutCount;
this->totalOutParamCount = fromData->totalOutParamCount;
this->inlinedArgOutSize = fromData->inlinedArgOutSize;
this->hasCSECandidates = fromData->hasCSECandidates;
this->stackLiteralInitFldDataMap = fromData->stackLiteralInitFldDataMap;
this->changedSyms = fromData->changedSyms;
this->capturedValues = fromData->capturedValues;
if (this->capturedValues)
{
this->capturedValues->IncrementRefCount();
}
this->OnDataReused(fromData);
}
void
GlobOptBlockData::CopyBlockData(GlobOptBlockData *fromData)
{
this->globOpt = fromData->globOpt;
this->symToValueMap = fromData->symToValueMap;
this->exprToValueMap = fromData->exprToValueMap;
this->liveFields = fromData->liveFields;
this->liveArrayValues = fromData->liveArrayValues;
this->maybeWrittenTypeSyms = fromData->maybeWrittenTypeSyms;
this->isTempSrc = fromData->isTempSrc;
this->liveVarSyms = fromData->liveVarSyms;
this->liveInt32Syms = fromData->liveInt32Syms;
this->liveLossyInt32Syms = fromData->liveLossyInt32Syms;
this->liveFloat64Syms = fromData->liveFloat64Syms;
this->argObjSyms = fromData->argObjSyms;
this->maybeTempObjectSyms = fromData->maybeTempObjectSyms;
this->canStoreTempObjectSyms = fromData->canStoreTempObjectSyms;
this->curFunc = fromData->curFunc;
this->valuesToKillOnCalls = fromData->valuesToKillOnCalls;
this->inductionVariables = fromData->inductionVariables;
this->availableIntBoundChecks = fromData->availableIntBoundChecks;
this->callSequence = fromData->callSequence;
this->startCallCount = fromData->startCallCount;
this->argOutCount = fromData->argOutCount;
this->totalOutParamCount = fromData->totalOutParamCount;
this->inlinedArgOutSize = fromData->inlinedArgOutSize;
this->hasCSECandidates = fromData->hasCSECandidates;
this->changedSyms = fromData->changedSyms;
this->capturedValues = fromData->capturedValues;
this->stackLiteralInitFldDataMap = fromData->stackLiteralInitFldDataMap;
this->OnDataReused(fromData);
}
void
GlobOptBlockData::DeleteBlockData()
{
JitArenaAllocator *const alloc = this->globOpt->alloc;
this->symToValueMap->Delete();
this->exprToValueMap->Delete();
JitAdelete(alloc, this->liveFields);
JitAdelete(alloc, this->liveArrayValues);
if (this->maybeWrittenTypeSyms)
{
JitAdelete(alloc, this->maybeWrittenTypeSyms);
}
JitAdelete(alloc, this->isTempSrc);
JitAdelete(alloc, this->liveVarSyms);
JitAdelete(alloc, this->liveInt32Syms);
JitAdelete(alloc, this->liveLossyInt32Syms);
JitAdelete(alloc, this->liveFloat64Syms);
if (this->argObjSyms)
{
JitAdelete(alloc, this->argObjSyms);
}
if (this->maybeTempObjectSyms)
{
JitAdelete(alloc, this->maybeTempObjectSyms);
if (this->canStoreTempObjectSyms)
{
JitAdelete(alloc, this->canStoreTempObjectSyms);
}
}
else
{
Assert(!this->canStoreTempObjectSyms);
}
JitAdelete(alloc, this->valuesToKillOnCalls);
if(this->inductionVariables)
{
JitAdelete(alloc, this->inductionVariables);
}
if(this->availableIntBoundChecks)
{
JitAdelete(alloc, this->availableIntBoundChecks);
}
if (this->stackLiteralInitFldDataMap)
{
JitAdelete(alloc, this->stackLiteralInitFldDataMap);
}
JitAdelete(alloc, this->changedSyms);
this->changedSyms = nullptr;
if (this->capturedValues && this->capturedValues->DecrementRefCount() == 0)
{
JitAdelete(this->curFunc->m_alloc, this->capturedValues);
this->capturedValues = nullptr;
}
this->OnDataDeleted();
}
void GlobOptBlockData::CloneBlockData(BasicBlock *const toBlockContext, BasicBlock *const fromBlock)
{
GlobOptBlockData *const fromData = &fromBlock->globOptData;
this->globOpt = fromData->globOpt;
JitArenaAllocator *const alloc = this->globOpt->alloc;
this->symToValueMap = fromData->symToValueMap->Copy();
this->exprToValueMap = fromData->exprToValueMap->Copy();
// Clone the values as well to allow for flow-sensitive ValueInfo
this->globOpt->CloneValues(toBlockContext, this, fromData);
if(this->globOpt->DoBoundCheckHoist())
{
this->globOpt->CloneBoundCheckHoistBlockData(toBlockContext, this, fromBlock, fromData);
}
this->liveFields = JitAnew(alloc, BVSparse<JitArenaAllocator>, alloc);
this->liveFields->Copy(fromData->liveFields);
this->liveArrayValues = JitAnew(alloc, BVSparse<JitArenaAllocator>, alloc);
this->liveArrayValues->Copy(fromData->liveArrayValues);
if (fromData->maybeWrittenTypeSyms)
{
this->maybeWrittenTypeSyms = JitAnew(alloc, BVSparse<JitArenaAllocator>, alloc);
this->maybeWrittenTypeSyms->Copy(fromData->maybeWrittenTypeSyms);
}
this->isTempSrc = JitAnew(alloc, BVSparse<JitArenaAllocator>, alloc);
this->isTempSrc->Copy(fromData->isTempSrc);
this->liveVarSyms = JitAnew(alloc, BVSparse<JitArenaAllocator>, alloc);
this->liveVarSyms->Copy(fromData->liveVarSyms);
this->liveInt32Syms = JitAnew(alloc, BVSparse<JitArenaAllocator>, alloc);
this->liveInt32Syms->Copy(fromData->liveInt32Syms);
this->liveLossyInt32Syms = JitAnew(alloc, BVSparse<JitArenaAllocator>, alloc);
this->liveLossyInt32Syms->Copy(fromData->liveLossyInt32Syms);
this->liveFloat64Syms = JitAnew(alloc, BVSparse<JitArenaAllocator>, alloc);
this->liveFloat64Syms->Copy(fromData->liveFloat64Syms);
if (this->globOpt->TrackArgumentsObject() && fromData->argObjSyms)
{
this->argObjSyms = fromData->argObjSyms->CopyNew(alloc);
}
if (fromData->maybeTempObjectSyms && !fromData->maybeTempObjectSyms->IsEmpty())
{
this->maybeTempObjectSyms = fromData->maybeTempObjectSyms->CopyNew(alloc);
if (fromData->canStoreTempObjectSyms && !fromData->canStoreTempObjectSyms->IsEmpty())
{
this->canStoreTempObjectSyms = fromData->canStoreTempObjectSyms->CopyNew(alloc);
}
}
else
{
Assert(fromData->canStoreTempObjectSyms == nullptr || fromData->canStoreTempObjectSyms->IsEmpty());
}
this->curFunc = fromData->curFunc;
if (fromData->callSequence != nullptr)
{
this->callSequence = JitAnew(alloc, SListBase<IR::Opnd *>);
fromData->callSequence->CopyTo(alloc, *(this->callSequence));
}
else
{
this->callSequence = nullptr;
}
this->startCallCount = fromData->startCallCount;
this->argOutCount = fromData->argOutCount;
this->totalOutParamCount = fromData->totalOutParamCount;
this->inlinedArgOutSize = fromData->inlinedArgOutSize;
this->hasCSECandidates = fromData->hasCSECandidates;
// Although we don't need the data on loop pre pass, we need to do it for the loop header
// because we capture the loop header bailout on loop prepass
if (fromData->stackLiteralInitFldDataMap != nullptr &&
(!this->globOpt->IsLoopPrePass() || (toBlockContext->isLoopHeader && toBlockContext->loop == this->globOpt->rootLoopPrePass)))
{
this->stackLiteralInitFldDataMap = fromData->stackLiteralInitFldDataMap->Clone();
}
else
{
this->stackLiteralInitFldDataMap = nullptr;
}
this->changedSyms = JitAnew(alloc, BVSparse<JitArenaAllocator>, alloc);
this->changedSyms->Copy(fromData->changedSyms);
this->capturedValues = fromData->capturedValues;
if (this->capturedValues)
{
this->capturedValues->IncrementRefCount();
}
Assert(fromData->HasData());
this->OnDataInitialized(alloc);
}
void GlobOptBlockData::RemoveUnavailableCandidates(PRECandidates * candidates)
{
// In case of multiple back-edges to the loop, make sure the candidates are still valid.
FOREACH_SLIST_ENTRY_EDITING(GlobHashBucket*, candidate, candidates->candidatesList, iter)
{
Value *candidateValue = candidate->element;
PropertySym *candidatePropertySym = candidate->value->AsPropertySym();
ValueNumber valueNumber = candidateValue->GetValueNumber();
Sym *symStore = candidateValue->GetValueInfo()->GetSymStore();
Value *blockValue = this->FindValue(candidatePropertySym);
if (blockValue && blockValue->GetValueNumber() == valueNumber
&& blockValue->GetValueInfo()->GetSymStore() == symStore)
{
Value *symStoreValue = this->FindValue(symStore);
if (symStoreValue && symStoreValue->GetValueNumber() == valueNumber)
{
continue;
}
}
iter.RemoveCurrent();
Assert(candidates->candidatesToProcess->Test(candidatePropertySym->m_id));
candidates->candidatesToProcess->Clear(candidatePropertySym->m_id);
} NEXT_SLIST_ENTRY_EDITING;
}
template <typename CapturedList, typename CapturedItemsAreEqual>
void
GlobOptBlockData::MergeCapturedValues(
SListBase<CapturedList> * toList,
SListBase<CapturedList> * fromList,
CapturedItemsAreEqual itemsAreEqual)
{
typename SListBase<CapturedList>::Iterator iterTo(toList);
typename SListBase<CapturedList>::Iterator iterFrom(fromList);
bool hasTo = iterTo.Next();
bool hasFrom = fromList == nullptr ? false : iterFrom.Next();
// to be conservative, only copy the captured value for common sym Ids
// in from and to CapturedList, mark all non-common sym Ids for re-capture
while (hasFrom && hasTo)
{
Sym * symFrom = iterFrom.Data().Key();
Sym * symTo = iterTo.Data().Key();
if (symFrom->m_id < symTo->m_id)
{
this->changedSyms->Set(symFrom->m_id);
hasFrom = iterFrom.Next();
}
else if(symFrom->m_id > symTo->m_id)
{
this->changedSyms->Set(symTo->m_id);
hasTo = iterTo.Next();
}
else
{
if (!itemsAreEqual(&iterFrom.Data(), &iterTo.Data()))
{
this->changedSyms->Set(symTo->m_id);
}
hasFrom = iterFrom.Next();
hasTo = iterTo.Next();
}
}
bool hasRemain = hasFrom || hasTo;
if (hasRemain)
{
typename SListBase<CapturedList>::Iterator iterRemain(hasFrom ? iterFrom : iterTo);
do
{
Sym * symRemain = iterRemain.Data().Key();
this->changedSyms->Set(symRemain->m_id);
hasRemain = iterRemain.Next();
} while (hasRemain);
}
}
void
GlobOptBlockData::MergeBlockData(
BasicBlock *toBlock,
BasicBlock *fromBlock,
BVSparse<JitArenaAllocator> *const symsRequiringCompensation,
BVSparse<JitArenaAllocator> *const symsCreatedForMerge,
bool forceTypeSpecOnLoopHeader)
{
GlobOptBlockData *fromData = &(fromBlock->globOptData);
if(this->globOpt->DoBoundCheckHoist())
{
// Do this before merging values so that it can see whether a sym's value was changed on one side or the other
this->globOpt->MergeBoundCheckHoistBlockData(toBlock, this, fromBlock, fromData);
}
bool isLoopBackEdge = toBlock->isLoopHeader;
this->MergeValueMaps(toBlock, fromBlock, symsRequiringCompensation, symsCreatedForMerge);
this->globOpt->InsertCloneStrs(toBlock, this, fromData);
this->liveFields->And(fromData->liveFields);
this->liveArrayValues->And(fromData->liveArrayValues);
this->isTempSrc->And(fromData->isTempSrc);
this->hasCSECandidates &= fromData->hasCSECandidates;
this->changedSyms->Or(fromData->changedSyms);
if (this->capturedValues == nullptr)
{
this->capturedValues = fromData->capturedValues;
if (this->capturedValues)
{
this->capturedValues->IncrementRefCount();
}
}
else
{
this->MergeCapturedValues(
&this->capturedValues->constantValues,
fromData->capturedValues == nullptr ? nullptr : &fromData->capturedValues->constantValues,
[&](ConstantStackSymValue * symValueFrom, ConstantStackSymValue * symValueTo)
{
Assert(symValueFrom->Key()->m_id == symValueTo->Key()->m_id);
return symValueFrom->Value().IsEqual(symValueTo->Value());
});
this->MergeCapturedValues(
&this->capturedValues->copyPropSyms,
fromData->capturedValues == nullptr ? nullptr : &fromData->capturedValues->copyPropSyms,
[&](CopyPropSyms * copyPropSymFrom, CopyPropSyms * copyPropSymTo)
{
Assert(copyPropSymFrom->Key()->m_id == copyPropSymTo->Key()->m_id);
if (copyPropSymFrom->Value()->m_id == copyPropSymTo->Value()->m_id)
{
Value * fromVal = fromData->FindValue(copyPropSymFrom->Key());
Value * toVal = this->FindValue(copyPropSymFrom->Key());
return fromVal && toVal && fromVal->IsEqualTo(toVal);
}
return false;
});
}
if (fromData->maybeWrittenTypeSyms)
{
if (this->maybeWrittenTypeSyms == nullptr)
{
this->maybeWrittenTypeSyms = JitAnew(this->globOpt->alloc, BVSparse<JitArenaAllocator>, this->globOpt->alloc);
this->maybeWrittenTypeSyms->Copy(fromData->maybeWrittenTypeSyms);
}
else
{
this->maybeWrittenTypeSyms->Or(fromData->maybeWrittenTypeSyms);
}
}
{
// - Keep the var sym live if any of the following is true:
// - The var sym is live on both sides
// - The var sym is the only live sym that contains the lossless value of the sym on a side (that is, the lossless
// int32 sym is not live, and the float64 sym is not live on that side), and the sym of any type is live on the
// other side
// - On a side, the var and float64 syms are live, the lossless int32 sym is not live, the sym's merged value is
// likely int, and the sym of any type is live on the other side. Since the value is likely int, it may be
// int-specialized (with lossless conversion) later. Keeping only the float64 sym live requires doing a lossless
// conversion from float64 to int32, with bailout if the value of the float is not a true 32-bit integer. Checking
// that is costly, and if the float64 sym is converted back to var, it does not become a tagged int, causing a
// guaranteed bailout if a lossless conversion to int happens later. Keep the var sym live to preserve its
// tagged-ness so that it can be int-specialized while avoiding unnecessary bailouts.
// - Keep the int32 sym live if it's live on both sides
// - Mark the sym as lossy if it's lossy on any side
// - Keep the float64 sym live if it's live on a side and the sym of a specialized lossless type is live on the other
// side
//
// fromData.temp =
// (fromData.var - (fromData.int32 - fromData.lossyInt32)) &
// (this.var | this.int32 | this.float64)
// this.temp =
// (this.var - (this.int32 - this.lossyInt32)) &
// (fromData.var | fromData.int32 | fromData.float64)
// this.var =
// (fromData.var & this.var) |
// (fromData.temp - fromData.float64) |
// (this.temp - this.float64) |
// (fromData.temp & fromData.float64 | this.temp & this.float64) & (value ~ int)
//
// this.float64 =
// fromData.float64 & ((this.int32 - this.lossyInt32) | this.float64) |
// this.float64 & ((fromData.int32 - fromData.lossyInt32) | fromData.float64)
// this.int32 &= fromData.int32
// this.lossyInt32 = (fromData.lossyInt32 | this.lossyInt32) & this.int32
BVSparse<JitArenaAllocator> tempBv1(this->globOpt->tempAlloc);
BVSparse<JitArenaAllocator> tempBv2(this->globOpt->tempAlloc);
if (isLoopBackEdge && forceTypeSpecOnLoopHeader)
{
Loop *const loop = toBlock->loop;
// Force to lossless int32:
// forceLosslessInt32 =
// ((fromData.int32 - fromData.lossyInt32) - (this.int32 - this.lossyInt32)) &
// loop.likelyIntSymsUsedBeforeDefined &
// this.var
tempBv1.Minus(fromData->liveInt32Syms, fromData->liveLossyInt32Syms);
tempBv2.Minus(this->liveInt32Syms, this->liveLossyInt32Syms);
tempBv1.Minus(&tempBv2);
tempBv1.And(loop->likelyIntSymsUsedBeforeDefined);
tempBv1.And(this->liveVarSyms);
this->liveInt32Syms->Or(&tempBv1);
this->liveLossyInt32Syms->Minus(&tempBv1);
if(this->globOpt->DoLossyIntTypeSpec())
{
// Force to lossy int32:
// forceLossyInt32 = (fromData.int32 - this.int32) & loop.symsUsedBeforeDefined & this.var
tempBv1.Minus(fromData->liveInt32Syms, this->liveInt32Syms);
tempBv1.And(loop->symsUsedBeforeDefined);
tempBv1.And(this->liveVarSyms);
this->liveInt32Syms->Or(&tempBv1);
this->liveLossyInt32Syms->Or(&tempBv1);
}
// Force to float64:
// forceFloat64 =
// fromData.float64 & loop.forceFloat64 |
// (fromData.float64 - this.float64) & loop.likelyNumberSymsUsedBeforeDefined
tempBv1.And(fromData->liveFloat64Syms, loop->forceFloat64SymsOnEntry);
this->liveFloat64Syms->Or(&tempBv1);
tempBv1.Minus(fromData->liveFloat64Syms, this->liveFloat64Syms);
tempBv1.And(loop->likelyNumberSymsUsedBeforeDefined);
this->liveFloat64Syms->Or(&tempBv1);
}
{
BVSparse<JitArenaAllocator> tempBv3(this->globOpt->tempAlloc);
// fromData.temp =
// (fromData.var - (fromData.int32 - fromData.lossyInt32)) &
// (this.var | this.int32 | this.float64)
tempBv2.Minus(fromData->liveInt32Syms, fromData->liveLossyInt32Syms);
tempBv1.Minus(fromData->liveVarSyms, &tempBv2);
tempBv2.Or(this->liveVarSyms, this->liveInt32Syms);
tempBv2.Or(this->liveFloat64Syms);
tempBv1.And(&tempBv2);
// this.temp =
// (this.var - (this.int32 - this.lossyInt32)) &
// (fromData.var | fromData.int32 | fromData.float64)
tempBv3.Minus(this->liveInt32Syms, this->liveLossyInt32Syms);
tempBv2.Minus(this->liveVarSyms, &tempBv3);
tempBv3.Or(fromData->liveVarSyms, fromData->liveInt32Syms);
tempBv3.Or(fromData->liveFloat64Syms);
tempBv2.And(&tempBv3);
{
BVSparse<JitArenaAllocator> tempBv4(this->globOpt->tempAlloc);
// fromData.temp & fromData.float64 | this.temp & this.float64
tempBv3.And(&tempBv1, fromData->liveFloat64Syms);
tempBv4.And(&tempBv2, this->liveFloat64Syms);
tempBv3.Or(&tempBv4);
}
// (fromData.temp - fromData.float64) |
// (this.temp - this.float64)
tempBv1.Minus(fromData->liveFloat64Syms);
tempBv2.Minus(this->liveFloat64Syms);
tempBv1.Or(&tempBv2);
// this.var =
// (fromData.var & this.var) |
// (fromData.temp - fromData.float64) |
// (this.temp - this.float64)
this->liveVarSyms->And(fromData->liveVarSyms);
this->liveVarSyms->Or(&tempBv1);
// this.var |=
// (fromData.temp & fromData.float64 | this.temp & this.float64) & (value ~ int)
FOREACH_BITSET_IN_SPARSEBV(id, &tempBv3)
{
StackSym *const stackSym = this->globOpt->func->m_symTable->FindStackSym(id);
Assert(stackSym);
Value *const value = this->FindValue(stackSym);
if(value)
{
ValueInfo *const valueInfo = value->GetValueInfo();
if(valueInfo->IsInt() || (valueInfo->IsLikelyInt() && this->globOpt->DoAggressiveIntTypeSpec()))
{
this->liveVarSyms->Set(id);
}
}
} NEXT_BITSET_IN_SPARSEBV;
}
// fromData.float64 & ((this.int32 - this.lossyInt32) | this.float64)
tempBv1.Minus(this->liveInt32Syms, this->liveLossyInt32Syms);
tempBv1.Or(this->liveFloat64Syms);
tempBv1.And(fromData->liveFloat64Syms);
// this.float64 & ((fromData.int32 - fromData.lossyInt32) | fromData.float64)
tempBv2.Minus(fromData->liveInt32Syms, fromData->liveLossyInt32Syms);
tempBv2.Or(fromData->liveFloat64Syms);
tempBv2.And(this->liveFloat64Syms);
// this.float64 =
// fromData.float64 & ((this.int32 - this.lossyInt32) | this.float64) |
// this.float64 & ((fromData.int32 - fromData.lossyInt32) | fromData.float64)
this->liveFloat64Syms->Or(&tempBv1, &tempBv2);
// this.int32 &= fromData.int32
// this.lossyInt32 = (fromData.lossyInt32 | this.lossyInt32) & this.int32
this->liveInt32Syms->And(fromData->liveInt32Syms);
this->liveLossyInt32Syms->Or(fromData->liveLossyInt32Syms);
this->liveLossyInt32Syms->And(this->liveInt32Syms);
}
if (this->globOpt->TrackArgumentsObject())
{
if (!this->argObjSyms->Equal(fromData->argObjSyms))
{
this->globOpt->CannotAllocateArgumentsObjectOnStack(nullptr);
}
}
if (fromData->maybeTempObjectSyms && !fromData->maybeTempObjectSyms->IsEmpty())
{
if (this->maybeTempObjectSyms)
{
this->maybeTempObjectSyms->Or(fromData->maybeTempObjectSyms);
}
else
{
this->maybeTempObjectSyms = fromData->maybeTempObjectSyms->CopyNew(this->globOpt->alloc);
}
if (fromData->canStoreTempObjectSyms && !fromData->canStoreTempObjectSyms->IsEmpty())
{
if (this->canStoreTempObjectSyms)
{
// Both need to be temp object
this->canStoreTempObjectSyms->And(fromData->canStoreTempObjectSyms);
}
}
else if (this->canStoreTempObjectSyms)
{
this->canStoreTempObjectSyms->ClearAll();
}
}
else
{
Assert(!fromData->canStoreTempObjectSyms || fromData->canStoreTempObjectSyms->IsEmpty());
if (this->canStoreTempObjectSyms)
{
this->canStoreTempObjectSyms->ClearAll();
}
}
Assert(this->curFunc == fromData->curFunc);
Assert((this->callSequence == nullptr && fromData->callSequence == nullptr) || this->callSequence->Equals(*(fromData->callSequence)));
Assert(this->startCallCount == fromData->startCallCount);
Assert(this->argOutCount == fromData->argOutCount);
Assert(this->totalOutParamCount == fromData->totalOutParamCount);
Assert(this->inlinedArgOutSize == fromData->inlinedArgOutSize);
// stackLiteralInitFldDataMap is a union of the stack literal from two path.
// Although we don't need the data on loop prepass, we need to do it for the loop header
// because we capture the loop header bailout on loop prepass.
if (fromData->stackLiteralInitFldDataMap != nullptr &&
(!this->globOpt->IsLoopPrePass() || (toBlock->isLoopHeader && toBlock->loop == this->globOpt->rootLoopPrePass)))
{
if (this->stackLiteralInitFldDataMap == nullptr)
{
this->stackLiteralInitFldDataMap = fromData->stackLiteralInitFldDataMap->Clone();
}
else
{
StackLiteralInitFldDataMap * toMap = this->stackLiteralInitFldDataMap;
fromData->stackLiteralInitFldDataMap->Map([toMap](StackSym * stackSym, StackLiteralInitFldData const& data)
{
if (toMap->AddNew(stackSym, data) == -1)
{
// If there is an existing data for the stackSym, both path should match
DebugOnly(StackLiteralInitFldData const * currentData);
Assert(toMap->TryGetReference(stackSym, ¤tData));
Assert(currentData->currentInitFldCount == data.currentInitFldCount);
Assert(currentData->propIds == data.propIds);
}
});
}
}
}
void
GlobOptBlockData::MergeValueMaps(
BasicBlock *toBlock,
BasicBlock *fromBlock,
BVSparse<JitArenaAllocator> *const symsRequiringCompensation,
BVSparse<JitArenaAllocator> *const symsCreatedForMerge)
{
GlobOptBlockData *fromData = &(fromBlock->globOptData);
bool isLoopBackEdge = toBlock->isLoopHeader;
Loop *loop = toBlock->loop;
bool isLoopPrepass = (loop && this->globOpt->prePassLoop == loop);
Assert(this->globOpt->valuesCreatedForMerge->Count() == 0);
DebugOnly(ValueSetByValueNumber mergedValues(this->globOpt->tempAlloc, 64));
BVSparse<JitArenaAllocator> *const mergedValueTypesTrackedForKills = this->globOpt->tempBv;
Assert(mergedValueTypesTrackedForKills->IsEmpty());
this->valuesToKillOnCalls->Clear(); // the tracking will be reevaluated based on merged value types
GlobHashTable *thisTable = this->symToValueMap;
GlobHashTable *otherTable = fromData->symToValueMap;
for (uint i = 0; i < thisTable->tableSize; i++)
{
SListBase<GlobHashBucket>::Iterator iter2(&otherTable->table[i]);
iter2.Next();
FOREACH_SLISTBASE_ENTRY_EDITING(GlobHashBucket, bucket, &thisTable->table[i], iter)
{
while (iter2.IsValid() && bucket.value->m_id < iter2.Data().value->m_id)
{
iter2.Next();
}
Value *newValue = nullptr;
if (iter2.IsValid() && bucket.value->m_id == iter2.Data().value->m_id)
{
// Syms that are assigned to within the loop must have unique
// value numbers in the loop header after merging; a single
// prepass is not adequate to determine that sym values are
// equivalent through all possible loop paths.
bool forceUniqueValue =
isLoopBackEdge &&
!this->globOpt->IsLoopPrePass() &&
loop &&
loop->symsAssignedToInLoop->Test(bucket.value->m_id);
newValue =
this->MergeValues(
bucket.element,
iter2.Data().element,
iter2.Data().value,
isLoopBackEdge,
forceUniqueValue,
symsRequiringCompensation,
symsCreatedForMerge);
}
if (newValue == nullptr)
{
iter.RemoveCurrent(thisTable->alloc);
continue;
}
else
{
#if DBG
// Ensure that only one value per value number is produced by merge. Byte-code constant values are reused in
// multiple blocks without cloning, so exclude those value numbers.
{
Value *const previouslyMergedValue = mergedValues.Lookup(newValue->GetValueNumber());
if (previouslyMergedValue)
{
if (!this->globOpt->byteCodeConstantValueNumbersBv->Test(newValue->GetValueNumber()))
{
Assert(newValue == previouslyMergedValue);
}
}
else
{
mergedValues.Add(newValue);
}
}
#endif
this->globOpt->TrackMergedValueForKills(newValue, this, mergedValueTypesTrackedForKills);
bucket.element = newValue;
}
iter2.Next();
} NEXT_SLISTBASE_ENTRY_EDITING;
if (isLoopPrepass && !this->globOpt->rootLoopPrePass->allFieldsKilled)
{
while (iter2.IsValid())
{
iter2.Next();
}
}
}
this->globOpt->valuesCreatedForMerge->Clear();
DebugOnly(mergedValues.Reset());
mergedValueTypesTrackedForKills->ClearAll();
this->exprToValueMap->And(fromData->exprToValueMap);
this->globOpt->ProcessValueKills(toBlock, this);
bool isLastLoopBackEdge = false;
if (isLoopBackEdge)
{
this->globOpt->ProcessValueKillsForLoopHeaderAfterBackEdgeMerge(toBlock, this);
BasicBlock *lastBlock = nullptr;
FOREACH_PREDECESSOR_BLOCK(pred, toBlock)
{
Assert(!lastBlock || pred->GetBlockNum() > lastBlock->GetBlockNum());
lastBlock = pred;
}NEXT_PREDECESSOR_BLOCK;
isLastLoopBackEdge = (lastBlock == fromBlock);
}
}
Value *
GlobOptBlockData::MergeValues(
Value *toDataValue,
Value *fromDataValue,
Sym *fromDataSym,
bool isLoopBackEdge,
bool forceUniqueValue,
BVSparse<JitArenaAllocator> *const symsRequiringCompensation,
BVSparse<JitArenaAllocator> *const symsCreatedForMerge)
{
// Same map
if (toDataValue == fromDataValue)
{
return toDataValue;
}
const ValueNumberPair sourceValueNumberPair(toDataValue->GetValueNumber(), fromDataValue->GetValueNumber());
const bool sameValueNumber = sourceValueNumberPair.First() == sourceValueNumberPair.Second();
ValueInfo *newValueInfo =
this->MergeValueInfo(
toDataValue,
fromDataValue,
fromDataSym,
isLoopBackEdge,
sameValueNumber,
symsRequiringCompensation,
symsCreatedForMerge);
if (newValueInfo == nullptr)
{
return nullptr;
}
if (sameValueNumber && newValueInfo == toDataValue->GetValueInfo())
{
return toDataValue;
}
Value *newValue = nullptr;
if (forceUniqueValue)
{
newValue = this->globOpt->NewValue(newValueInfo);
}
else
{
// There may be other syms in toData that haven't been merged yet, referring to the current toData value for this sym. If
// the merge produced a new value info, don't corrupt the value info for the other sym by changing the same value. Instead,
// create one value per source value number pair per merge and reuse that for new value infos.
newValue = this->globOpt->valuesCreatedForMerge->Lookup(sourceValueNumberPair, nullptr);
if (newValue)
{
Assert(sameValueNumber == (newValue->GetValueNumber() == toDataValue->GetValueNumber()));
// This is an exception where Value::SetValueInfo is called directly instead of GlobOpt::ChangeValueInfo, because we're
// actually generating new value info through merges.
newValue->SetValueInfo(newValueInfo);
}
else
{
newValue = this->globOpt->NewValue(sameValueNumber ? sourceValueNumberPair.First() : this->globOpt->NewValueNumber(), newValueInfo);
this->globOpt->valuesCreatedForMerge->Add(sourceValueNumberPair, newValue);
}
}
// Set symStore if same on both paths.
if (toDataValue->GetValueInfo()->GetSymStore() == fromDataValue->GetValueInfo()->GetSymStore())
{
this->globOpt->SetSymStoreDirect(newValueInfo, toDataValue->GetValueInfo()->GetSymStore());
}
return newValue;
}
ValueInfo *
GlobOptBlockData::MergeValueInfo(
Value *toDataVal,
Value *fromDataVal,
Sym *fromDataSym,
bool isLoopBackEdge,
bool sameValueNumber,
BVSparse<JitArenaAllocator> *const symsRequiringCompensation,
BVSparse<JitArenaAllocator> *const symsCreatedForMerge)
{
ValueInfo *const toDataValueInfo = toDataVal->GetValueInfo();
ValueInfo *const fromDataValueInfo = fromDataVal->GetValueInfo();
if (toDataValueInfo->IsJsType() || fromDataValueInfo->IsJsType())
{
Assert(toDataValueInfo->IsJsType() && fromDataValueInfo->IsJsType());
return this->MergeJsTypeValueInfo(toDataValueInfo->AsJsType(), fromDataValueInfo->AsJsType(), isLoopBackEdge, sameValueNumber);
}
// Same value
if (toDataValueInfo == fromDataValueInfo)
{
return toDataValueInfo;
}
ValueType newValueType(toDataValueInfo->Type().Merge(fromDataValueInfo->Type()));
if (newValueType.IsLikelyInt())
{
return ValueInfo::MergeLikelyIntValueInfo(this->globOpt->alloc, toDataVal, fromDataVal, newValueType);
}
if(newValueType.IsLikelyAnyOptimizedArray())
{
if(newValueType.IsLikelyArrayOrObjectWithArray() &&
toDataValueInfo->IsLikelyArrayOrObjectWithArray() &&
fromDataValueInfo->IsLikelyArrayOrObjectWithArray())
{
// Value type merge for missing values is aggressive by default (for profile data) - if either side likely has no
// missing values, then the merged value type also likely has no missing values. This is because arrays often start
// off having missing values but are eventually filled up. In GlobOpt however, we need to be conservative because
// the existence of a value type that likely has missing values indicates that it is more likely for it to have
// missing values than not. Also, StElems that are likely to create missing values are tracked in profile data and
// will update value types to say they are now likely to have missing values, and that needs to be propagated
// conservatively.
newValueType =
newValueType.SetHasNoMissingValues(
toDataValueInfo->HasNoMissingValues() && fromDataValueInfo->HasNoMissingValues());
if(toDataValueInfo->HasIntElements() != fromDataValueInfo->HasIntElements() ||
toDataValueInfo->HasFloatElements() != fromDataValueInfo->HasFloatElements())
{
// When merging arrays with different native storage types, make the merged value type a likely version to force
// array checks to be done again and cause a conversion and/or bailout as necessary
newValueType = newValueType.ToLikely();
}
}
if(!(newValueType.IsObject() && toDataValueInfo->IsArrayValueInfo() && fromDataValueInfo->IsArrayValueInfo()))
{
return ValueInfo::New(this->globOpt->alloc, newValueType);
}
return
this->MergeArrayValueInfo(
newValueType,
toDataValueInfo->AsArrayValueInfo(),
fromDataValueInfo->AsArrayValueInfo(),
fromDataSym,
symsRequiringCompensation,
symsCreatedForMerge,
isLoopBackEdge);
}