-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathGlobOptIntBounds.cpp
3288 lines (2999 loc) · 128 KB
/
GlobOptIntBounds.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"
#if ENABLE_DEBUG_CONFIG_OPTIONS && DBG_DUMP
#define TRACE_PHASE_VERBOSE(phase, indent, ...) \
if(PHASE_VERBOSE_TRACE(phase, this->func)) \
{ \
for(int i = 0; i < static_cast<int>(indent); ++i) \
{ \
Output::Print(_u(" ")); \
} \
Output::Print(__VA_ARGS__); \
Output::Flush(); \
}
#else
#define TRACE_PHASE_VERBOSE(phase, indent, ...)
#endif
void GlobOpt::AddSubConstantInfo::Set(
StackSym *const srcSym,
Value *const srcValue,
const bool srcValueIsLikelyConstant,
const int32 offset)
{
Assert(srcSym);
Assert(!srcSym->IsTypeSpec());
Assert(srcValue);
Assert(srcValue->GetValueInfo()->IsLikelyInt());
this->srcSym = srcSym;
this->srcValue = srcValue;
this->srcValueIsLikelyConstant = srcValueIsLikelyConstant;
this->offset = offset;
}
void GlobOpt::ArrayLowerBoundCheckHoistInfo::SetCompatibleBoundCheck(
BasicBlock *const compatibleBoundCheckBlock,
StackSym *const indexSym,
const int offset,
const ValueNumber indexValueNumber)
{
Assert(!Loop());
Assert(compatibleBoundCheckBlock);
Assert(indexSym);
Assert(!indexSym->IsTypeSpec());
Assert(indexValueNumber != InvalidValueNumber);
this->compatibleBoundCheckBlock = compatibleBoundCheckBlock;
this->indexSym = indexSym;
this->offset = offset;
this->indexValueNumber = indexValueNumber;
}
void GlobOpt::ArrayLowerBoundCheckHoistInfo::SetLoop(
::Loop *const loop,
const int indexConstantValue,
const bool isLoopCountBasedBound)
{
Assert(!CompatibleBoundCheckBlock());
Assert(loop);
this->loop = loop;
indexSym = nullptr;
offset = 0;
indexValue = nullptr;
indexConstantBounds = IntConstantBounds(indexConstantValue, indexConstantValue);
this->isLoopCountBasedBound = isLoopCountBasedBound;
loopCount = nullptr;
}
void GlobOpt::ArrayLowerBoundCheckHoistInfo::SetLoop(
::Loop *const loop,
StackSym *const indexSym,
const int indexOffset,
const int offset,
Value *const indexValue,
const IntConstantBounds &indexConstantBounds,
const bool isLoopCountBasedBound)
{
Assert(!CompatibleBoundCheckBlock());
Assert(loop);
Assert(indexSym);
Assert(!indexSym->IsTypeSpec());
Assert(indexValue);
this->loop = loop;
this->indexSym = indexSym;
this->indexOffset = indexOffset;
this->offset = offset;
this->indexValueNumber = indexValue->GetValueNumber();
this->indexValue = indexValue;
this->indexConstantBounds = indexConstantBounds;
this->isLoopCountBasedBound = isLoopCountBasedBound;
loopCount = nullptr;
}
void GlobOpt::ArrayLowerBoundCheckHoistInfo::SetLoopCount(::LoopCount *const loopCount, const int maxMagnitudeChange)
{
Assert(Loop());
Assert(loopCount);
Assert(maxMagnitudeChange != 0);
this->loopCount = loopCount;
this->maxMagnitudeChange = maxMagnitudeChange;
}
void GlobOpt::ArrayUpperBoundCheckHoistInfo::SetCompatibleBoundCheck(
BasicBlock *const compatibleBoundCheckBlock,
const int indexConstantValue)
{
Assert(!Loop());
Assert(compatibleBoundCheckBlock);
this->compatibleBoundCheckBlock = compatibleBoundCheckBlock;
indexSym = nullptr;
offset = -1; // simulate < instead of <=
indexConstantBounds = IntConstantBounds(indexConstantValue, indexConstantValue);
}
void GlobOpt::ArrayUpperBoundCheckHoistInfo::SetLoop(
::Loop *const loop,
const int indexConstantValue,
Value *const headSegmentLengthValue,
const IntConstantBounds &headSegmentLengthConstantBounds,
const bool isLoopCountBasedBound)
{
Assert(!CompatibleBoundCheckBlock());
Assert(loop);
Assert(headSegmentLengthValue);
SetLoop(loop, indexConstantValue, isLoopCountBasedBound);
offset = -1; // simulate < instead of <=
this->headSegmentLengthValue = headSegmentLengthValue;
this->headSegmentLengthConstantBounds = headSegmentLengthConstantBounds;
}
void GlobOpt::ArrayUpperBoundCheckHoistInfo::SetLoop(
::Loop *const loop,
StackSym *const indexSym,
const int indexOffset,
const int offset,
Value *const indexValue,
const IntConstantBounds &indexConstantBounds,
Value *const headSegmentLengthValue,
const IntConstantBounds &headSegmentLengthConstantBounds,
const bool isLoopCountBasedBound)
{
Assert(headSegmentLengthValue);
SetLoop(loop, indexSym, indexOffset, offset, indexValue, indexConstantBounds, isLoopCountBasedBound);
this->headSegmentLengthValue = headSegmentLengthValue;
this->headSegmentLengthConstantBounds = headSegmentLengthConstantBounds;
}
void GlobOpt::UpdateIntBoundsForEqualBranch(
Value *const src1Value,
Value *const src2Value,
const int32 src2ConstantValue)
{
Assert(src1Value);
if(!DoPathDependentValues() || (src2Value && src1Value->GetValueNumber() == src2Value->GetValueNumber()))
{
return;
}
#if DBG
if(!IsLoopPrePass() && DoAggressiveIntTypeSpec() && DoConstFold())
{
IntConstantBounds src1ConstantBounds, src2ConstantBounds;
AssertVerify(src1Value->GetValueInfo()->TryGetIntConstantBounds(&src1ConstantBounds, true));
if(src2Value)
{
AssertVerify(src2Value->GetValueInfo()->TryGetIntConstantBounds(&src2ConstantBounds, true));
}
else
{
src2ConstantBounds = IntConstantBounds(src2ConstantValue, src2ConstantValue);
}
Assert(
!ValueInfo::IsEqualTo(
src1Value,
src1ConstantBounds.LowerBound(),
src1ConstantBounds.UpperBound(),
src2Value,
src2ConstantBounds.LowerBound(),
src2ConstantBounds.UpperBound()));
Assert(
!ValueInfo::IsNotEqualTo(
src1Value,
src1ConstantBounds.LowerBound(),
src1ConstantBounds.UpperBound(),
src2Value,
src2ConstantBounds.LowerBound(),
src2ConstantBounds.UpperBound()));
}
#endif
SetPathDependentInfo(
true,
PathDependentInfo(PathDependentRelationship::Equal, src1Value, src2Value, src2ConstantValue));
SetPathDependentInfo(
false,
PathDependentInfo(PathDependentRelationship::NotEqual, src1Value, src2Value, src2ConstantValue));
}
void GlobOpt::UpdateIntBoundsForNotEqualBranch(
Value *const src1Value,
Value *const src2Value,
const int32 src2ConstantValue)
{
Assert(src1Value);
if(!DoPathDependentValues() || (src2Value && src1Value->GetValueNumber() == src2Value->GetValueNumber()))
{
return;
}
#if DBG
if(!IsLoopPrePass() && DoAggressiveIntTypeSpec() && DoConstFold())
{
IntConstantBounds src1ConstantBounds, src2ConstantBounds;
AssertVerify(src1Value->GetValueInfo()->TryGetIntConstantBounds(&src1ConstantBounds, true));
if(src2Value)
{
AssertVerify(src2Value->GetValueInfo()->TryGetIntConstantBounds(&src2ConstantBounds, true));
}
else
{
src2ConstantBounds = IntConstantBounds(src2ConstantValue, src2ConstantValue);
}
Assert(
!ValueInfo::IsEqualTo(
src1Value,
src1ConstantBounds.LowerBound(),
src1ConstantBounds.UpperBound(),
src2Value,
src2ConstantBounds.LowerBound(),
src2ConstantBounds.UpperBound()));
Assert(
!ValueInfo::IsNotEqualTo(
src1Value,
src1ConstantBounds.LowerBound(),
src1ConstantBounds.UpperBound(),
src2Value,
src2ConstantBounds.LowerBound(),
src2ConstantBounds.UpperBound()));
}
#endif
SetPathDependentInfo(
true, PathDependentInfo(PathDependentRelationship::NotEqual, src1Value, src2Value, src2ConstantValue));
SetPathDependentInfo(
false, PathDependentInfo(PathDependentRelationship::Equal, src1Value, src2Value, src2ConstantValue));
}
void GlobOpt::UpdateIntBoundsForGreaterThanOrEqualBranch(Value *const src1Value, Value *const src2Value)
{
Assert(src1Value);
Assert(src2Value);
if(!DoPathDependentValues() || src1Value->GetValueNumber() == src2Value->GetValueNumber())
{
return;
}
#if DBG
if(!IsLoopPrePass() && DoAggressiveIntTypeSpec() && DoConstFold())
{
IntConstantBounds src1ConstantBounds, src2ConstantBounds;
AssertVerify(src1Value->GetValueInfo()->TryGetIntConstantBounds(&src1ConstantBounds, true));
AssertVerify(src2Value->GetValueInfo()->TryGetIntConstantBounds(&src2ConstantBounds, true));
Assert(
!ValueInfo::IsGreaterThanOrEqualTo(
src1Value,
src1ConstantBounds.LowerBound(),
src1ConstantBounds.UpperBound(),
src2Value,
src2ConstantBounds.LowerBound(),
src2ConstantBounds.UpperBound()));
Assert(
!ValueInfo::IsLessThan(
src1Value,
src1ConstantBounds.LowerBound(),
src1ConstantBounds.UpperBound(),
src2Value,
src2ConstantBounds.LowerBound(),
src2ConstantBounds.UpperBound()));
}
#endif
SetPathDependentInfo(true, PathDependentInfo(PathDependentRelationship::GreaterThanOrEqual, src1Value, src2Value));
SetPathDependentInfo(false, PathDependentInfo(PathDependentRelationship::LessThan, src1Value, src2Value));
}
void GlobOpt::UpdateIntBoundsForGreaterThanBranch(Value *const src1Value, Value *const src2Value)
{
return UpdateIntBoundsForLessThanBranch(src2Value, src1Value);
}
void GlobOpt::UpdateIntBoundsForLessThanOrEqualBranch(Value *const src1Value, Value *const src2Value)
{
return UpdateIntBoundsForGreaterThanOrEqualBranch(src2Value, src1Value);
}
void GlobOpt::UpdateIntBoundsForLessThanBranch(Value *const src1Value, Value *const src2Value)
{
Assert(src1Value);
Assert(src2Value);
if(!DoPathDependentValues() || src1Value->GetValueNumber() == src2Value->GetValueNumber())
{
return;
}
#if DBG
if(!IsLoopPrePass() && DoAggressiveIntTypeSpec() && DoConstFold())
{
IntConstantBounds src1ConstantBounds, src2ConstantBounds;
AssertVerify(src1Value->GetValueInfo()->TryGetIntConstantBounds(&src1ConstantBounds, true));
AssertVerify(src2Value->GetValueInfo()->TryGetIntConstantBounds(&src2ConstantBounds, true));
Assert(
!ValueInfo::IsGreaterThanOrEqualTo(
src1Value,
src1ConstantBounds.LowerBound(),
src1ConstantBounds.UpperBound(),
src2Value,
src2ConstantBounds.LowerBound(),
src2ConstantBounds.UpperBound()));
Assert(
!ValueInfo::IsLessThan(
src1Value,
src1ConstantBounds.LowerBound(),
src1ConstantBounds.UpperBound(),
src2Value,
src2ConstantBounds.LowerBound(),
src2ConstantBounds.UpperBound()));
}
#endif
SetPathDependentInfo(true, PathDependentInfo(PathDependentRelationship::LessThan, src1Value, src2Value));
SetPathDependentInfo(false, PathDependentInfo(PathDependentRelationship::GreaterThanOrEqual, src1Value, src2Value));
}
IntBounds *GlobOpt::GetIntBoundsToUpdate(
const ValueInfo *const valueInfo,
const IntConstantBounds &constantBounds,
const bool isSettingNewBound,
const bool isBoundConstant,
const bool isSettingUpperBound,
const bool isExplicit)
{
Assert(valueInfo);
Assert(valueInfo->IsLikelyInt());
if(!DoTrackRelativeIntBounds())
{
return nullptr;
}
if(valueInfo->IsIntBounded())
{
const IntBounds *const bounds = valueInfo->AsIntBounded()->Bounds();
if(bounds->RequiresIntBoundedValueInfo(valueInfo->Type()))
{
return bounds->Clone();
}
}
if(valueInfo->IsInt())
{
if(constantBounds.IsConstant())
{
// Don't start tracking relative bounds for int constant values, just retain existing relative bounds. Will use
// IntConstantValueInfo instead.
return nullptr;
}
if(isBoundConstant)
{
// There are no relative bounds to track
if(!(isSettingUpperBound && isExplicit))
{
// We are not setting a constant upper bound that is established explicitly, will use IntRangeValueInfo instead
return nullptr;
}
}
else if(!isSettingNewBound)
{
// New relative bounds are not being set, will use IntRangeValueInfo instead
return nullptr;
}
return IntBounds::New(constantBounds, false, alloc);
}
return nullptr;
}
ValueInfo *GlobOpt::UpdateIntBoundsForEqual(
Value *const value,
const IntConstantBounds &constantBounds,
Value *const boundValue,
const IntConstantBounds &boundConstantBounds,
const bool isExplicit)
{
Assert(value || constantBounds.IsConstant());
Assert(boundValue || boundConstantBounds.IsConstant());
if(!value)
{
return nullptr;
}
Assert(!boundValue || value->GetValueNumber() != boundValue->GetValueNumber());
ValueInfo *const valueInfo = value->GetValueInfo();
IntBounds *const bounds =
GetIntBoundsToUpdate(valueInfo, constantBounds, true, boundConstantBounds.IsConstant(), true, isExplicit);
if(bounds)
{
if(boundValue)
{
const ValueNumber valueNumber = value->GetValueNumber();
bounds->SetLowerBound(valueNumber, boundValue, isExplicit);
bounds->SetUpperBound(valueNumber, boundValue, isExplicit);
}
else
{
bounds->SetLowerBound(boundConstantBounds.LowerBound());
bounds->SetUpperBound(boundConstantBounds.LowerBound(), isExplicit);
}
if(bounds->RequiresIntBoundedValueInfo(valueInfo->Type()))
{
return NewIntBoundedValueInfo(valueInfo, bounds);
}
bounds->Delete();
}
if(!valueInfo->IsInt())
{
return nullptr;
}
const int32 newMin = max(constantBounds.LowerBound(), boundConstantBounds.LowerBound());
const int32 newMax = min(constantBounds.UpperBound(), boundConstantBounds.UpperBound());
return newMin <= newMax ? NewIntRangeValueInfo(valueInfo, newMin, newMax) : nullptr;
}
ValueInfo *GlobOpt::UpdateIntBoundsForNotEqual(
Value *const value,
const IntConstantBounds &constantBounds,
Value *const boundValue,
const IntConstantBounds &boundConstantBounds,
const bool isExplicit)
{
Assert(value || constantBounds.IsConstant());
Assert(boundValue || boundConstantBounds.IsConstant());
if(!value)
{
return nullptr;
}
Assert(!boundValue || value->GetValueNumber() != boundValue->GetValueNumber());
ValueInfo *const valueInfo = value->GetValueInfo();
IntBounds *const bounds =
GetIntBoundsToUpdate(
valueInfo,
constantBounds,
false,
boundConstantBounds.IsConstant(),
boundConstantBounds.IsConstant() && boundConstantBounds.LowerBound() == constantBounds.UpperBound(),
isExplicit);
if(bounds)
{
if(boundValue
? bounds->SetIsNot(boundValue, isExplicit)
: bounds->SetIsNot(boundConstantBounds.LowerBound(), isExplicit))
{
if(bounds->RequiresIntBoundedValueInfo(valueInfo->Type()))
{
return NewIntBoundedValueInfo(valueInfo, bounds);
}
}
else
{
bounds->Delete();
return nullptr;
}
bounds->Delete();
}
if(!valueInfo->IsInt() || !boundConstantBounds.IsConstant())
{
return nullptr;
}
const int32 constantBound = boundConstantBounds.LowerBound();
// The value is not equal to a constant, so narrow the range if the constant is equal to the value's lower or upper bound
int32 newMin = constantBounds.LowerBound(), newMax = constantBounds.UpperBound();
if(constantBound == newMin)
{
Assert(newMin <= newMax);
if(newMin == newMax)
{
return nullptr;
}
++newMin;
}
else if(constantBound == newMax)
{
Assert(newMin <= newMax);
if(newMin == newMax)
{
return nullptr;
}
--newMax;
}
else
{
return nullptr;
}
return NewIntRangeValueInfo(valueInfo, newMin, newMax);
}
ValueInfo *GlobOpt::UpdateIntBoundsForGreaterThanOrEqual(
Value *const value,
const IntConstantBounds &constantBounds,
Value *const boundValue,
const IntConstantBounds &boundConstantBounds,
const bool isExplicit)
{
return UpdateIntBoundsForGreaterThanOrEqual(value, constantBounds, boundValue, boundConstantBounds, 0, isExplicit);
}
ValueInfo *GlobOpt::UpdateIntBoundsForGreaterThanOrEqual(
Value *const value,
const IntConstantBounds &constantBounds,
Value *const boundValue,
const IntConstantBounds &boundConstantBounds,
const int boundOffset,
const bool isExplicit)
{
Assert(value || constantBounds.IsConstant());
Assert(boundValue || boundConstantBounds.IsConstant());
if(!value)
{
return nullptr;
}
Assert(!boundValue || value->GetValueNumber() != boundValue->GetValueNumber());
ValueInfo *const valueInfo = value->GetValueInfo();
IntBounds *const bounds =
GetIntBoundsToUpdate(valueInfo, constantBounds, true, boundConstantBounds.IsConstant(), false, isExplicit);
if(bounds)
{
if(boundValue)
{
bounds->SetLowerBound(value->GetValueNumber(), boundValue, boundOffset, isExplicit);
}
else
{
bounds->SetLowerBound(boundConstantBounds.LowerBound(), boundOffset);
}
if(bounds->RequiresIntBoundedValueInfo(valueInfo->Type()))
{
return NewIntBoundedValueInfo(valueInfo, bounds);
}
bounds->Delete();
}
if(!valueInfo->IsInt())
{
return nullptr;
}
int32 adjustedBoundMin;
if(boundOffset == 0)
{
adjustedBoundMin = boundConstantBounds.LowerBound();
}
else if(boundOffset == 1)
{
if(boundConstantBounds.LowerBound() + 1 <= boundConstantBounds.LowerBound())
{
return nullptr;
}
adjustedBoundMin = boundConstantBounds.LowerBound() + 1;
}
else if(Int32Math::Add(boundConstantBounds.LowerBound(), boundOffset, &adjustedBoundMin))
{
return nullptr;
}
const int32 newMin = max(constantBounds.LowerBound(), adjustedBoundMin);
return
newMin <= constantBounds.UpperBound()
? NewIntRangeValueInfo(valueInfo, newMin, constantBounds.UpperBound())
: nullptr;
}
ValueInfo *GlobOpt::UpdateIntBoundsForGreaterThan(
Value *const value,
const IntConstantBounds &constantBounds,
Value *const boundValue,
const IntConstantBounds &boundConstantBounds,
const bool isExplicit)
{
return UpdateIntBoundsForGreaterThanOrEqual(value, constantBounds, boundValue, boundConstantBounds, 1, isExplicit);
}
ValueInfo *GlobOpt::UpdateIntBoundsForLessThanOrEqual(
Value *const value,
const IntConstantBounds &constantBounds,
Value *const boundValue,
const IntConstantBounds &boundConstantBounds,
const bool isExplicit)
{
return UpdateIntBoundsForLessThanOrEqual(value, constantBounds, boundValue, boundConstantBounds, 0, isExplicit);
}
ValueInfo *GlobOpt::UpdateIntBoundsForLessThanOrEqual(
Value *const value,
const IntConstantBounds &constantBounds,
Value *const boundValue,
const IntConstantBounds &boundConstantBounds,
const int boundOffset,
const bool isExplicit)
{
Assert(value || constantBounds.IsConstant());
Assert(boundValue || boundConstantBounds.IsConstant());
if(!value)
{
return nullptr;
}
Assert(!boundValue || value->GetValueNumber() != boundValue->GetValueNumber());
ValueInfo *const valueInfo = value->GetValueInfo();
IntBounds *const bounds =
GetIntBoundsToUpdate(valueInfo, constantBounds, true, boundConstantBounds.IsConstant(), true, isExplicit);
if(bounds)
{
if(boundValue)
{
bounds->SetUpperBound(value->GetValueNumber(), boundValue, boundOffset, isExplicit);
}
else
{
bounds->SetUpperBound(boundConstantBounds.LowerBound(), boundOffset, isExplicit);
}
if(bounds->RequiresIntBoundedValueInfo(valueInfo->Type()))
{
return NewIntBoundedValueInfo(valueInfo, bounds);
}
bounds->Delete();
}
if(!valueInfo->IsInt())
{
return nullptr;
}
int32 adjustedBoundMax;
if(boundOffset == 0)
{
adjustedBoundMax = boundConstantBounds.UpperBound();
}
else if(boundOffset == -1)
{
if(boundConstantBounds.UpperBound() - 1 >= boundConstantBounds.UpperBound())
{
return nullptr;
}
adjustedBoundMax = boundConstantBounds.UpperBound() - 1;
}
else if(Int32Math::Add(boundConstantBounds.UpperBound(), boundOffset, &adjustedBoundMax))
{
return nullptr;
}
const int32 newMax = min(constantBounds.UpperBound(), adjustedBoundMax);
return
newMax >= constantBounds.LowerBound()
? NewIntRangeValueInfo(valueInfo, constantBounds.LowerBound(), newMax)
: nullptr;
}
ValueInfo *GlobOpt::UpdateIntBoundsForLessThan(
Value *const value,
const IntConstantBounds &constantBounds,
Value *const boundValue,
const IntConstantBounds &boundConstantBounds,
const bool isExplicit)
{
return UpdateIntBoundsForLessThanOrEqual(value, constantBounds, boundValue, boundConstantBounds, -1, isExplicit);
}
void GlobOpt::TrackIntSpecializedAddSubConstant(
IR::Instr *const instr,
const AddSubConstantInfo *const addSubConstantInfo,
Value *const dstValue,
const bool updateSourceBounds)
{
Assert(instr);
Assert(dstValue);
if(addSubConstantInfo)
{
Assert(addSubConstantInfo->HasInfo());
Assert(!ignoredIntOverflowForCurrentInstr);
do
{
if(!IsLoopPrePass() || !DoBoundCheckHoist())
{
break;
}
Assert(
instr->m_opcode == Js::OpCode::Incr_A ||
instr->m_opcode == Js::OpCode::Decr_A ||
instr->m_opcode == Js::OpCode::Add_A ||
instr->m_opcode == Js::OpCode::Sub_A);
StackSym *sym = instr->GetDst()->AsRegOpnd()->m_sym;
bool isPostfixIncDecPattern = false;
if(addSubConstantInfo->SrcSym() != sym)
{
// Check for the following patterns.
//
// This pattern is used for postfix inc/dec operators:
// s2 = Conv_Num s1
// s1 = Inc s2
//
// This pattern is used for prefix inc/dec operators:
// s2 = Inc s1
// s1 = Ld s2
IR::Instr *const prevInstr = instr->m_prev;
Assert(prevInstr);
if(prevInstr->m_opcode == Js::OpCode::Conv_Num &&
prevInstr->GetSrc1()->IsRegOpnd() &&
prevInstr->GetSrc1()->AsRegOpnd()->m_sym == sym &&
prevInstr->GetDst()->AsRegOpnd()->m_sym == addSubConstantInfo->SrcSym())
{
// s2 will get a new value number, since Conv_Num cannot transfer in the prepass. For the purposes of
// induction variable tracking however, it doesn't matter, so record this case and use s1's value in the
// current block.
isPostfixIncDecPattern = true;
}
else
{
IR::Instr *const nextInstr = instr->m_next;
Assert(nextInstr);
if(nextInstr->m_opcode != Js::OpCode::Ld_A ||
!nextInstr->GetSrc1()->IsRegOpnd() ||
nextInstr->GetSrc1()->AsRegOpnd()->m_sym != sym)
{
break;
}
sym = addSubConstantInfo->SrcSym();
if(nextInstr->GetDst()->AsRegOpnd()->m_sym != sym)
{
break;
}
// In the prefix inc/dec pattern, the result of Ld currently gets a new value number, which will cause the
// induction variable info to become indeterminate. Indicate that the value number should be updated in the
// induction variable info.
// Consider: Remove this once loop prepass value transfer scheme is fixed
updateInductionVariableValueNumber = true;
}
}
// Track induction variable info
ValueNumber srcValueNumber;
if(isPostfixIncDecPattern)
{
Value *const value = this->currentBlock->globOptData.FindValue(sym);
Assert(value);
srcValueNumber = value->GetValueNumber();
}
else
{
srcValueNumber = addSubConstantInfo->SrcValue()->GetValueNumber();
}
InductionVariableSet *const inductionVariables = currentBlock->globOptData.inductionVariables;
Assert(inductionVariables);
InductionVariable *inductionVariable;
if(!inductionVariables->TryGetReference(sym->m_id, &inductionVariable))
{
// Only track changes in the current loop's prepass. In subsequent prepasses, the info is only being propagated
// for use by the parent loop, so changes in the current loop have already been tracked.
if(prePassLoop != currentBlock->loop)
{
updateInductionVariableValueNumber = false;
break;
}
// Ensure that the sym is live in the landing pad, and that its value has not changed in an unknown way yet
Value *const landingPadValue = currentBlock->loop->landingPad->globOptData.FindValue(sym);
if(!landingPadValue || srcValueNumber != landingPadValue->GetValueNumber() || currentBlock->loop->symsDefInLoop->Test(sym->m_id))
{
updateInductionVariableValueNumber = false;
break;
}
inductionVariables->Add(
InductionVariable(sym, dstValue->GetValueNumber(), addSubConstantInfo->Offset()));
break;
}
if(!inductionVariable->IsChangeDeterminate())
{
updateInductionVariableValueNumber = false;
break;
}
if(srcValueNumber != inductionVariable->SymValueNumber())
{
// The sym's value has changed since the last time induction variable info was recorded for it. Due to the
// unknown change, mark the info as indeterminate.
inductionVariable->SetChangeIsIndeterminate();
updateInductionVariableValueNumber = false;
break;
}
// Only track changes in the current loop's prepass. In subsequent prepasses, the info is only being propagated for
// use by the parent loop, so changes in the current loop have already been tracked. Induction variable value
// numbers are updated as changes occur, but their change bounds are preserved from the first prepass over the loop.
inductionVariable->SetSymValueNumber(dstValue->GetValueNumber());
if(prePassLoop != currentBlock->loop)
{
break;
}
if(!inductionVariable->Add(addSubConstantInfo->Offset()))
{
updateInductionVariableValueNumber = false;
}
} while(false);
if(!this->IsLoopPrePass() && updateSourceBounds && addSubConstantInfo->Offset() != IntConstMin)
{
// Track bounds for add or sub with a constant. For instance, consider (b = a + 2). The value of 'b' should track
// that it is equal to (the value of 'a') + 2. That part has been done above. Similarly, the value of 'a' should
// also track that it is equal to (the value of 'b') - 2.
Value *const value = addSubConstantInfo->SrcValue();
const ValueInfo *const valueInfo = value->GetValueInfo();
Assert(valueInfo->IsInt());
IntConstantBounds constantBounds;
AssertVerify(valueInfo->TryGetIntConstantBounds(&constantBounds));
IntBounds *const bounds =
GetIntBoundsToUpdate(
valueInfo,
constantBounds,
true,
dstValue->GetValueInfo()->HasIntConstantValue(),
true,
true);
if(bounds)
{
const ValueNumber valueNumber = value->GetValueNumber();
const int32 dstOffset = -addSubConstantInfo->Offset();
bounds->SetLowerBound(valueNumber, dstValue, dstOffset, true);
bounds->SetUpperBound(valueNumber, dstValue, dstOffset, true);
ChangeValueInfo(nullptr, value, NewIntBoundedValueInfo(valueInfo, bounds));
}
}
return;
}
if(!updateInductionVariableValueNumber)
{
return;
}
// See comment above where this is set to true
// Consider: Remove this once loop prepass value transfer scheme is fixed
updateInductionVariableValueNumber = false;
Assert(IsLoopPrePass());
Assert(instr->m_opcode == Js::OpCode::Ld_A);
Assert(
instr->m_prev->m_opcode == Js::OpCode::Incr_A ||
instr->m_prev->m_opcode == Js::OpCode::Decr_A ||
instr->m_prev->m_opcode == Js::OpCode::Add_A ||
instr->m_prev->m_opcode == Js::OpCode::Sub_A);
Assert(instr->m_prev->GetDst()->AsRegOpnd()->m_sym == instr->GetSrc1()->AsRegOpnd()->m_sym);
InductionVariable *inductionVariable;
AssertVerify(currentBlock->globOptData.inductionVariables->TryGetReference(instr->GetDst()->AsRegOpnd()->m_sym->m_id, &inductionVariable));
inductionVariable->SetSymValueNumber(dstValue->GetValueNumber());
}
void GlobOpt::CloneBoundCheckHoistBlockData(
BasicBlock *const toBlock,
GlobOptBlockData *const toData,
BasicBlock *const fromBlock,
GlobOptBlockData *const fromData)
{
Assert(DoBoundCheckHoist());
Assert(toBlock);
Assert(toData);
//Assert(toData == &toBlock->globOptData || toData == ¤tBlock->globOptData);
Assert(fromBlock);
Assert(fromData);
Assert(fromData == &fromBlock->globOptData);
Assert(fromData->availableIntBoundChecks);
toData->availableIntBoundChecks = fromData->availableIntBoundChecks->Clone();
if(toBlock->isLoopHeader)
{
Assert(fromBlock == toBlock->loop->landingPad);
if(prePassLoop == toBlock->loop)
{
// When the current prepass loop is the current loop, the loop header's induction variable set needs to start off
// empty to track changes in the current loop
toData->inductionVariables = JitAnew(alloc, InductionVariableSet, alloc);
return;
}
if(!IsLoopPrePass())
{
return;
}
// After the prepass on this loop, if we're still in a prepass, this must be an inner loop. Merge the landing pad info
// for use by the parent loop.
Assert(fromBlock->loop);
Assert(fromData->inductionVariables);
toData->inductionVariables = fromData->inductionVariables->Clone();
return;
}
if(!toBlock->loop || !IsLoopPrePass())
{
return;
}
Assert(fromBlock->loop);
Assert(toBlock->loop->IsDescendentOrSelf(fromBlock->loop));
Assert(fromData->inductionVariables);
toData->inductionVariables = fromData->inductionVariables->Clone();
}
void GlobOpt::MergeBoundCheckHoistBlockData(
BasicBlock *const toBlock,
GlobOptBlockData *const toData,
BasicBlock *const fromBlock,
GlobOptBlockData *const fromData)
{
Assert(DoBoundCheckHoist());
Assert(toBlock);
Assert(toData);
//Assert(toData == &toBlock->globOptData || toData == ¤tBlock->globOptData);
Assert(fromBlock);
Assert(fromData);
Assert(fromData == &fromBlock->globOptData);
Assert(toData->availableIntBoundChecks);
for(auto it = toData->availableIntBoundChecks->GetIteratorWithRemovalSupport(); it.IsValid(); it.MoveNext())
{
const IntBoundCheck &toDataIntBoundCheck = it.CurrentValue();
const IntBoundCheck *fromDataIntBoundCheck;
if(!fromData->availableIntBoundChecks->TryGetReference(
toDataIntBoundCheck.CompatibilityId(),
&fromDataIntBoundCheck) ||
fromDataIntBoundCheck->Instr() != toDataIntBoundCheck.Instr())
{
it.RemoveCurrent();
}
}
InductionVariableSet *mergeInductionVariablesInto;
if(toBlock->isLoopHeader)
{
Assert(fromBlock->loop == toBlock->loop); // The flow is such that you cannot have back-edges from an inner loop
if(IsLoopPrePass())
{
// Collect info for the parent loop. Any changes to induction variables in this inner loop need to be expanded in
// the same direction for the parent loop, so merge expanded info from back-edges. Info about induction variables
// that changed before the loop but not inside the loop, can be kept intact because the landing pad dominates the
// loop.
Assert(prePassLoop != toBlock->loop);
Assert(fromData->inductionVariables);
Assert(toData->inductionVariables);
InductionVariableSet *const mergedInductionVariables = toData->inductionVariables;
for(auto it = fromData->inductionVariables->GetIterator(); it.IsValid(); it.MoveNext())
{
InductionVariable backEdgeInductionVariable = it.CurrentValue();
backEdgeInductionVariable.ExpandInnerLoopChange();
StackSym *const sym = backEdgeInductionVariable.Sym();