-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
assertionprop.cpp
6734 lines (5980 loc) · 236 KB
/
assertionprop.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XX XX
XX AssertionProp XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
//------------------------------------------------------------------------
// Contains: Whether the range contains a given integral value, inclusive.
//
// Arguments:
// value - the integral value in question
//
// Return Value:
// "true" if the value is within the range's bounds, "false" otherwise.
//
bool IntegralRange::Contains(int64_t value) const
{
int64_t lowerBound = SymbolicToRealValue(m_lowerBound);
int64_t upperBound = SymbolicToRealValue(m_upperBound);
return (lowerBound <= value) && (value <= upperBound);
}
//------------------------------------------------------------------------
// SymbolicToRealValue: Convert a symbolic value to a 64-bit signed integer.
//
// Arguments:
// value - the symbolic value in question
//
// Return Value:
// Integer corresponding to the symbolic value.
//
/* static */ int64_t IntegralRange::SymbolicToRealValue(SymbolicIntegerValue value)
{
static const int64_t SymbolicToRealMap[]{
INT64_MIN, // SymbolicIntegerValue::LongMin
INT32_MIN, // SymbolicIntegerValue::IntMin
INT16_MIN, // SymbolicIntegerValue::ShortMin
INT8_MIN, // SymbolicIntegerValue::ByteMin
0, // SymbolicIntegerValue::Zero
1, // SymbolicIntegerValue::One
INT8_MAX, // SymbolicIntegerValue::ByteMax
UINT8_MAX, // SymbolicIntegerValue::UByteMax
INT16_MAX, // SymbolicIntegerValue::ShortMax
UINT16_MAX, // SymbolicIntegerValue::UShortMax
CORINFO_Array_MaxLength, // SymbolicIntegerValue::ArrayLenMax
INT32_MAX, // SymbolicIntegerValue::IntMax
UINT32_MAX, // SymbolicIntegerValue::UIntMax
INT64_MAX // SymbolicIntegerValue::LongMax
};
assert(sizeof(SymbolicIntegerValue) == sizeof(int32_t));
assert(SymbolicToRealMap[static_cast<int32_t>(SymbolicIntegerValue::LongMin)] == INT64_MIN);
assert(SymbolicToRealMap[static_cast<int32_t>(SymbolicIntegerValue::Zero)] == 0);
assert(SymbolicToRealMap[static_cast<int32_t>(SymbolicIntegerValue::LongMax)] == INT64_MAX);
return SymbolicToRealMap[static_cast<int32_t>(value)];
}
//------------------------------------------------------------------------
// LowerBoundForType: Get the symbolic lower bound for a type.
//
// Arguments:
// type - the integral type in question
//
// Return Value:
// Symbolic value representing the smallest possible value "type" can represent.
//
/* static */ SymbolicIntegerValue IntegralRange::LowerBoundForType(var_types type)
{
switch (type)
{
case TYP_UBYTE:
case TYP_USHORT:
return SymbolicIntegerValue::Zero;
case TYP_BYTE:
return SymbolicIntegerValue::ByteMin;
case TYP_SHORT:
return SymbolicIntegerValue::ShortMin;
case TYP_INT:
return SymbolicIntegerValue::IntMin;
case TYP_LONG:
return SymbolicIntegerValue::LongMin;
default:
unreached();
}
}
//------------------------------------------------------------------------
// UpperBoundForType: Get the symbolic upper bound for a type.
//
// Arguments:
// type - the integral type in question
//
// Return Value:
// Symbolic value representing the largest possible value "type" can represent.
//
/* static */ SymbolicIntegerValue IntegralRange::UpperBoundForType(var_types type)
{
switch (type)
{
case TYP_BYTE:
return SymbolicIntegerValue::ByteMax;
case TYP_UBYTE:
return SymbolicIntegerValue::UByteMax;
case TYP_SHORT:
return SymbolicIntegerValue::ShortMax;
case TYP_USHORT:
return SymbolicIntegerValue::UShortMax;
case TYP_INT:
return SymbolicIntegerValue::IntMax;
case TYP_UINT:
return SymbolicIntegerValue::UIntMax;
case TYP_LONG:
return SymbolicIntegerValue::LongMax;
default:
unreached();
}
}
//------------------------------------------------------------------------
// ForNode: Compute the integral range for a node.
//
// Arguments:
// node - the node, of an integral type, in question
// compiler - the Compiler, used to retrieve additional info
//
// Return Value:
// The integral range this node produces.
//
/* static */ IntegralRange IntegralRange::ForNode(GenTree* node, Compiler* compiler)
{
assert(varTypeIsIntegral(node));
var_types rangeType = node->TypeGet();
switch (node->OperGet())
{
case GT_EQ:
case GT_NE:
case GT_LT:
case GT_LE:
case GT_GE:
case GT_GT:
return {SymbolicIntegerValue::Zero, SymbolicIntegerValue::One};
case GT_ARR_LENGTH:
case GT_MDARR_LENGTH:
return {SymbolicIntegerValue::Zero, SymbolicIntegerValue::ArrayLenMax};
case GT_CALL:
if (node->AsCall()->NormalizesSmallTypesOnReturn())
{
rangeType = static_cast<var_types>(node->AsCall()->gtReturnType);
}
break;
case GT_IND:
{
GenTree* const addr = node->AsIndir()->Addr();
if (node->TypeIs(TYP_INT) && addr->OperIs(GT_ADD) && addr->gtGetOp1()->OperIs(GT_LCL_VAR) &&
addr->gtGetOp2()->IsIntegralConst(OFFSETOF__CORINFO_Span__length))
{
GenTreeLclVar* const lclVar = addr->gtGetOp1()->AsLclVar();
if (compiler->lvaGetDesc(lclVar->GetLclNum())->IsSpan())
{
assert(compiler->lvaIsImplicitByRefLocal(lclVar->GetLclNum()));
return {SymbolicIntegerValue::Zero, UpperBoundForType(rangeType)};
}
}
break;
}
case GT_LCL_FLD:
{
GenTreeLclFld* const lclFld = node->AsLclFld();
LclVarDsc* const varDsc = compiler->lvaGetDesc(lclFld);
if (node->TypeIs(TYP_INT) && varDsc->IsSpan() && lclFld->GetLclOffs() == OFFSETOF__CORINFO_Span__length)
{
return {SymbolicIntegerValue::Zero, UpperBoundForType(rangeType)};
}
break;
}
case GT_LCL_VAR:
{
LclVarDsc* const varDsc = compiler->lvaGetDesc(node->AsLclVar());
if (varDsc->lvNormalizeOnStore())
{
rangeType = compiler->lvaGetDesc(node->AsLclVar())->TypeGet();
}
if (varDsc->IsNeverNegative())
{
return {SymbolicIntegerValue::Zero, UpperBoundForType(rangeType)};
}
break;
}
case GT_CNS_INT:
if (node->IsIntegralConst(0) || node->IsIntegralConst(1))
{
return {SymbolicIntegerValue::Zero, SymbolicIntegerValue::One};
}
break;
case GT_QMARK:
return Union(ForNode(node->AsQmark()->ThenNode(), compiler),
ForNode(node->AsQmark()->ElseNode(), compiler));
case GT_CAST:
return ForCastOutput(node->AsCast(), compiler);
#if defined(FEATURE_HW_INTRINSICS)
case GT_HWINTRINSIC:
switch (node->AsHWIntrinsic()->GetHWIntrinsicId())
{
#if defined(TARGET_XARCH)
case NI_Vector128_op_Equality:
case NI_Vector128_op_Inequality:
case NI_Vector256_op_Equality:
case NI_Vector256_op_Inequality:
case NI_Vector512_op_Equality:
case NI_Vector512_op_Inequality:
case NI_SSE_CompareScalarOrderedEqual:
case NI_SSE_CompareScalarOrderedNotEqual:
case NI_SSE_CompareScalarOrderedLessThan:
case NI_SSE_CompareScalarOrderedLessThanOrEqual:
case NI_SSE_CompareScalarOrderedGreaterThan:
case NI_SSE_CompareScalarOrderedGreaterThanOrEqual:
case NI_SSE_CompareScalarUnorderedEqual:
case NI_SSE_CompareScalarUnorderedNotEqual:
case NI_SSE_CompareScalarUnorderedLessThanOrEqual:
case NI_SSE_CompareScalarUnorderedLessThan:
case NI_SSE_CompareScalarUnorderedGreaterThanOrEqual:
case NI_SSE_CompareScalarUnorderedGreaterThan:
case NI_SSE2_CompareScalarOrderedEqual:
case NI_SSE2_CompareScalarOrderedNotEqual:
case NI_SSE2_CompareScalarOrderedLessThan:
case NI_SSE2_CompareScalarOrderedLessThanOrEqual:
case NI_SSE2_CompareScalarOrderedGreaterThan:
case NI_SSE2_CompareScalarOrderedGreaterThanOrEqual:
case NI_SSE2_CompareScalarUnorderedEqual:
case NI_SSE2_CompareScalarUnorderedNotEqual:
case NI_SSE2_CompareScalarUnorderedLessThanOrEqual:
case NI_SSE2_CompareScalarUnorderedLessThan:
case NI_SSE2_CompareScalarUnorderedGreaterThanOrEqual:
case NI_SSE2_CompareScalarUnorderedGreaterThan:
case NI_SSE41_TestC:
case NI_SSE41_TestZ:
case NI_SSE41_TestNotZAndNotC:
case NI_AVX_TestC:
case NI_AVX_TestZ:
case NI_AVX_TestNotZAndNotC:
return {SymbolicIntegerValue::Zero, SymbolicIntegerValue::One};
case NI_SSE2_Extract:
case NI_SSE41_Extract:
case NI_SSE41_X64_Extract:
case NI_Vector128_ToScalar:
case NI_Vector256_ToScalar:
case NI_Vector512_ToScalar:
case NI_Vector128_GetElement:
case NI_Vector256_GetElement:
case NI_Vector512_GetElement:
if (varTypeIsSmall(node->AsHWIntrinsic()->GetSimdBaseType()))
{
return ForType(node->AsHWIntrinsic()->GetSimdBaseType());
}
break;
case NI_BMI1_TrailingZeroCount:
case NI_BMI1_X64_TrailingZeroCount:
case NI_LZCNT_LeadingZeroCount:
case NI_LZCNT_X64_LeadingZeroCount:
case NI_POPCNT_PopCount:
case NI_POPCNT_X64_PopCount:
// Note: No advantage in using a precise range for IntegralRange.
// Example: IntCns = 42 gives [0..127] with a non -precise range, [42,42] with a precise range.
return {SymbolicIntegerValue::Zero, SymbolicIntegerValue::ByteMax};
#elif defined(TARGET_ARM64)
case NI_Vector64_op_Equality:
case NI_Vector64_op_Inequality:
case NI_Vector128_op_Equality:
case NI_Vector128_op_Inequality:
return {SymbolicIntegerValue::Zero, SymbolicIntegerValue::One};
case NI_AdvSimd_Extract:
case NI_Vector64_ToScalar:
case NI_Vector128_ToScalar:
case NI_Vector64_GetElement:
case NI_Vector128_GetElement:
if (varTypeIsSmall(node->AsHWIntrinsic()->GetSimdBaseType()))
{
return ForType(node->AsHWIntrinsic()->GetSimdBaseType());
}
break;
case NI_AdvSimd_PopCount:
case NI_AdvSimd_LeadingZeroCount:
case NI_AdvSimd_LeadingSignCount:
case NI_ArmBase_LeadingZeroCount:
case NI_ArmBase_Arm64_LeadingZeroCount:
case NI_ArmBase_Arm64_LeadingSignCount:
// Note: No advantage in using a precise range for IntegralRange.
// Example: IntCns = 42 gives [0..127] with a non -precise range, [42,42] with a precise range.
return {SymbolicIntegerValue::Zero, SymbolicIntegerValue::ByteMax};
#else
#error Unsupported platform
#endif
default:
break;
}
break;
#endif // defined(FEATURE_HW_INTRINSICS)
default:
break;
}
return ForType(rangeType);
}
//------------------------------------------------------------------------
// ForCastInput: Get the non-overflowing input range for a cast.
//
// This routine computes the input range for a cast from
// an integer to an integer for which it will not overflow.
// See also the specification comment for IntegralRange.
//
// Arguments:
// cast - the cast node for which the range will be computed
//
// Return Value:
// The range this cast consumes without overflowing - see description.
//
/* static */ IntegralRange IntegralRange::ForCastInput(GenTreeCast* cast)
{
var_types fromType = genActualType(cast->CastOp());
var_types toType = cast->CastToType();
bool fromUnsigned = cast->IsUnsigned();
assert((fromType == TYP_INT) || (fromType == TYP_LONG) || varTypeIsGC(fromType));
assert(varTypeIsIntegral(toType));
// Cast from a GC type is the same as a cast from TYP_I_IMPL for our purposes.
if (varTypeIsGC(fromType))
{
fromType = TYP_I_IMPL;
}
if (!cast->gtOverflow())
{
// CAST(small type <- uint/int/ulong/long) - [TO_TYPE_MIN..TO_TYPE_MAX]
if (varTypeIsSmall(toType))
{
return {LowerBoundForType(toType), UpperBoundForType(toType)};
}
// We choose to say here that representation-changing casts never overflow.
// It does not really matter what we do here because representation-changing
// non-overflowing casts cannot be deleted from the IR in any case.
// CAST(uint/int <- uint/int) - [INT_MIN..INT_MAX]
// CAST(uint/int <- ulong/long) - [LONG_MIN..LONG_MAX]
// CAST(ulong/long <- uint/int) - [INT_MIN..INT_MAX]
// CAST(ulong/long <- ulong/long) - [LONG_MIN..LONG_MAX]
return ForType(fromType);
}
SymbolicIntegerValue lowerBound;
SymbolicIntegerValue upperBound;
// CAST_OVF(small type <- int/long) - [TO_TYPE_MIN..TO_TYPE_MAX]
// CAST_OVF(small type <- uint/ulong) - [0..TO_TYPE_MAX]
if (varTypeIsSmall(toType))
{
lowerBound = fromUnsigned ? SymbolicIntegerValue::Zero : LowerBoundForType(toType);
upperBound = UpperBoundForType(toType);
}
else
{
switch (toType)
{
// CAST_OVF(uint <- uint) - [INT_MIN..INT_MAX]
// CAST_OVF(uint <- int) - [0..INT_MAX]
// CAST_OVF(uint <- ulong/long) - [0..UINT_MAX]
case TYP_UINT:
if (fromType == TYP_LONG)
{
lowerBound = SymbolicIntegerValue::Zero;
upperBound = SymbolicIntegerValue::UIntMax;
}
else
{
lowerBound = fromUnsigned ? SymbolicIntegerValue::IntMin : SymbolicIntegerValue::Zero;
upperBound = SymbolicIntegerValue::IntMax;
}
break;
// CAST_OVF(int <- uint/ulong) - [0..INT_MAX]
// CAST_OVF(int <- int/long) - [INT_MIN..INT_MAX]
case TYP_INT:
lowerBound = fromUnsigned ? SymbolicIntegerValue::Zero : SymbolicIntegerValue::IntMin;
upperBound = SymbolicIntegerValue::IntMax;
break;
// CAST_OVF(ulong <- uint) - [INT_MIN..INT_MAX]
// CAST_OVF(ulong <- int) - [0..INT_MAX]
// CAST_OVF(ulong <- ulong) - [LONG_MIN..LONG_MAX]
// CAST_OVF(ulong <- long) - [0..LONG_MAX]
case TYP_ULONG:
lowerBound = fromUnsigned ? LowerBoundForType(fromType) : SymbolicIntegerValue::Zero;
upperBound = UpperBoundForType(fromType);
break;
// CAST_OVF(long <- uint/int) - [INT_MIN..INT_MAX]
// CAST_OVF(long <- ulong) - [0..LONG_MAX]
// CAST_OVF(long <- long) - [LONG_MIN..LONG_MAX]
case TYP_LONG:
if (fromUnsigned && (fromType == TYP_LONG))
{
lowerBound = SymbolicIntegerValue::Zero;
}
else
{
lowerBound = LowerBoundForType(fromType);
}
upperBound = UpperBoundForType(fromType);
break;
default:
unreached();
}
}
return {lowerBound, upperBound};
}
//------------------------------------------------------------------------
// ForCastOutput: Get the output range for a cast.
//
// This method is the "output" counterpart to ForCastInput, it returns
// a range produced by a cast (by definition, non-overflowing one).
// The output range is the same for representation-preserving casts, but
// can be different for others. One example is CAST_OVF(uint <- long).
// The input range is [0..UINT_MAX], while the output is [INT_MIN..INT_MAX].
// Unlike ForCastInput, this method supports casts from floating point types.
//
// Arguments:
// cast - the cast node for which the range will be computed
// compiler - Compiler object
//
// Return Value:
// The range this cast produces - see description.
//
/* static */ IntegralRange IntegralRange::ForCastOutput(GenTreeCast* cast, Compiler* compiler)
{
var_types fromType = genActualType(cast->CastOp());
var_types toType = cast->CastToType();
bool fromUnsigned = cast->IsUnsigned();
assert((fromType == TYP_INT) || (fromType == TYP_LONG) || varTypeIsFloating(fromType) || varTypeIsGC(fromType));
assert(varTypeIsIntegral(toType));
// CAST/CAST_OVF(small type <- float/double) - [TO_TYPE_MIN..TO_TYPE_MAX]
// CAST/CAST_OVF(uint/int <- float/double) - [INT_MIN..INT_MAX]
// CAST/CAST_OVF(ulong/long <- float/double) - [LONG_MIN..LONG_MAX]
if (varTypeIsFloating(fromType))
{
if (!varTypeIsSmall(toType))
{
toType = genActualType(toType);
}
return IntegralRange::ForType(toType);
}
// Cast from a GC type is the same as a cast from TYP_I_IMPL for our purposes.
if (varTypeIsGC(fromType))
{
fromType = TYP_I_IMPL;
}
if (varTypeIsSmall(toType) || (genActualType(toType) == fromType))
{
return ForCastInput(cast);
}
// if we're upcasting and the cast op is a known non-negative - consider
// this cast unsigned
if (!fromUnsigned && (genTypeSize(toType) >= genTypeSize(fromType)))
{
fromUnsigned = cast->CastOp()->IsNeverNegative(compiler);
}
// CAST(uint/int <- ulong/long) - [INT_MIN..INT_MAX]
// CAST(ulong/long <- uint) - [0..UINT_MAX]
// CAST(ulong/long <- int) - [INT_MIN..INT_MAX]
if (!cast->gtOverflow())
{
if ((fromType == TYP_INT) && fromUnsigned)
{
return {SymbolicIntegerValue::Zero, SymbolicIntegerValue::UIntMax};
}
return {SymbolicIntegerValue::IntMin, SymbolicIntegerValue::IntMax};
}
SymbolicIntegerValue lowerBound;
SymbolicIntegerValue upperBound;
switch (toType)
{
// CAST_OVF(uint <- ulong) - [INT_MIN..INT_MAX]
// CAST_OVF(uint <- long) - [INT_MIN..INT_MAX]
case TYP_UINT:
lowerBound = SymbolicIntegerValue::IntMin;
upperBound = SymbolicIntegerValue::IntMax;
break;
// CAST_OVF(int <- ulong) - [0..INT_MAX]
// CAST_OVF(int <- long) - [INT_MIN..INT_MAX]
case TYP_INT:
lowerBound = fromUnsigned ? SymbolicIntegerValue::Zero : SymbolicIntegerValue::IntMin;
upperBound = SymbolicIntegerValue::IntMax;
break;
// CAST_OVF(ulong <- uint) - [0..UINT_MAX]
// CAST_OVF(ulong <- int) - [0..INT_MAX]
case TYP_ULONG:
lowerBound = SymbolicIntegerValue::Zero;
upperBound = fromUnsigned ? SymbolicIntegerValue::UIntMax : SymbolicIntegerValue::IntMax;
break;
// CAST_OVF(long <- uint) - [0..UINT_MAX]
// CAST_OVF(long <- int) - [INT_MIN..INT_MAX]
case TYP_LONG:
lowerBound = fromUnsigned ? SymbolicIntegerValue::Zero : SymbolicIntegerValue::IntMin;
upperBound = fromUnsigned ? SymbolicIntegerValue::UIntMax : SymbolicIntegerValue::IntMax;
break;
default:
unreached();
}
return {lowerBound, upperBound};
}
/* static */ IntegralRange IntegralRange::Union(IntegralRange range1, IntegralRange range2)
{
return IntegralRange(min(range1.GetLowerBound(), range2.GetLowerBound()),
max(range1.GetUpperBound(), range2.GetUpperBound()));
}
#ifdef DEBUG
/* static */ void IntegralRange::Print(IntegralRange range)
{
printf("[%lld", SymbolicToRealValue(range.m_lowerBound));
printf("..");
printf("%lld]", SymbolicToRealValue(range.m_upperBound));
}
#endif // DEBUG
//------------------------------------------------------------------------------
// GetAssertionDep: Retrieve the assertions on this local variable
//
// Arguments:
// lclNum - The local var id.
//
// Return Value:
// The dependent assertions (assertions using the value of the local var)
// of the local var.
//
ASSERT_TP& Compiler::GetAssertionDep(unsigned lclNum)
{
JitExpandArray<ASSERT_TP>& dep = *optAssertionDep;
if (dep[lclNum] == nullptr)
{
dep[lclNum] = BitVecOps::MakeEmpty(apTraits);
}
return dep[lclNum];
}
/*****************************************************************************
*
* Initialize the assertion prop bitset traits and the default bitsets.
*/
void Compiler::optAssertionTraitsInit(AssertionIndex assertionCount)
{
apTraits = new (this, CMK_AssertionProp) BitVecTraits(assertionCount, this);
apFull = BitVecOps::MakeFull(apTraits);
}
/*****************************************************************************
*
* Initialize the assertion prop tracking logic.
*/
void Compiler::optAssertionInit(bool isLocalProp)
{
assert(NO_ASSERTION_INDEX == 0);
const unsigned maxTrackedLocals = (unsigned)JitConfig.JitMaxLocalsToTrack();
// We initialize differently for local prop / global prop
//
if (isLocalProp)
{
optLocalAssertionProp = true;
optCrossBlockLocalAssertionProp = true;
// Disable via config
//
if (JitConfig.JitEnableCrossBlockLocalAssertionProp() == 0)
{
JITDUMP("Disabling cross-block assertion prop by config setting\n");
optCrossBlockLocalAssertionProp = false;
}
#ifdef DEBUG
// Disable per method via range
//
static ConfigMethodRange s_range;
s_range.EnsureInit(JitConfig.JitEnableCrossBlockLocalAssertionPropRange());
if (!s_range.Contains(info.compMethodHash()))
{
JITDUMP("Disabling cross-block assertion prop by config range\n");
optCrossBlockLocalAssertionProp = false;
}
#endif
// Disable if too many locals
//
// The typical number of local assertions is roughly proportional
// to the number of locals. So when we have huge numbers of locals,
// just do within-block local assertion prop.
//
if (lvaCount > maxTrackedLocals)
{
JITDUMP("Disabling cross-block assertion prop: too many locals\n");
optCrossBlockLocalAssertionProp = false;
}
if (optCrossBlockLocalAssertionProp)
{
// We may need a fairly large table. Keep size a multiple of 64.
// Empirical studies show about 1.16 asserions/ tracked local.
//
if (lvaTrackedCount < 24)
{
optMaxAssertionCount = 64;
}
else if (lvaTrackedCount < 64)
{
optMaxAssertionCount = 128;
}
else
{
optMaxAssertionCount = (AssertionIndex)min(maxTrackedLocals, ((3 * lvaTrackedCount / 128) + 1) * 64);
}
}
else
{
// The assertion table will be reset for each block, so it can be smaller.
//
optMaxAssertionCount = 64;
}
// Local assertion prop keeps mappings from each local var to the assertions about that var.
//
optAssertionDep =
new (this, CMK_AssertionProp) JitExpandArray<ASSERT_TP>(getAllocator(CMK_AssertionProp), max(1u, lvaCount));
if (optCrossBlockLocalAssertionProp)
{
optComplementaryAssertionMap = new (this, CMK_AssertionProp)
AssertionIndex[optMaxAssertionCount + 1](); // zero-inited (NO_ASSERTION_INDEX)
}
}
else
{
// General assertion prop.
//
optLocalAssertionProp = false;
optCrossBlockLocalAssertionProp = false;
// Use a function countFunc to determine a proper maximum assertion count for the
// method being compiled. The function is linear to the IL size for small and
// moderate methods. For large methods, considering throughput impact, we track no
// more than 64 assertions.
// Note this tracks at most only 256 assertions.
//
static const AssertionIndex countFunc[] = {64, 128, 256, 128, 64};
static const unsigned upperBound = ArrLen(countFunc) - 1;
const unsigned codeSize = info.compILCodeSize / 512;
optMaxAssertionCount = countFunc[min(upperBound, codeSize)];
optValueNumToAsserts =
new (getAllocator(CMK_AssertionProp)) ValueNumToAssertsMap(getAllocator(CMK_AssertionProp));
optComplementaryAssertionMap = new (this, CMK_AssertionProp)
AssertionIndex[optMaxAssertionCount + 1](); // zero-inited (NO_ASSERTION_INDEX)
}
optAssertionTabPrivate = new (this, CMK_AssertionProp) AssertionDsc[optMaxAssertionCount];
optAssertionTraitsInit(optMaxAssertionCount);
optAssertionCount = 0;
optAssertionOverflow = 0;
optAssertionPropagated = false;
bbJtrueAssertionOut = nullptr;
optCanPropLclVar = false;
optCanPropEqual = false;
optCanPropNonNull = false;
optCanPropBndsChk = false;
optCanPropSubRange = false;
}
#ifdef DEBUG
void Compiler::optPrintAssertion(AssertionDsc* curAssertion, AssertionIndex assertionIndex /* = 0 */)
{
if (curAssertion->op1.kind == O1K_EXACT_TYPE)
{
printf("Type ");
}
else if (curAssertion->op1.kind == O1K_ARR_BND)
{
printf("ArrBnds ");
}
else if (curAssertion->op1.kind == O1K_SUBTYPE)
{
printf("Subtype ");
}
else if (curAssertion->op2.kind == O2K_LCLVAR_COPY)
{
printf("Copy ");
}
else if ((curAssertion->op2.kind == O2K_CONST_INT) || (curAssertion->op2.kind == O2K_CONST_LONG) ||
(curAssertion->op2.kind == O2K_CONST_DOUBLE) || (curAssertion->op2.kind == O2K_ZEROOBJ))
{
printf("Constant ");
}
else if (curAssertion->op2.kind == O2K_SUBRANGE)
{
printf("Subrange ");
}
else
{
printf("?assertion classification? ");
}
printf("Assertion: ");
if (!optLocalAssertionProp)
{
printf("(" FMT_VN "," FMT_VN ") ", curAssertion->op1.vn, curAssertion->op2.vn);
}
if ((curAssertion->op1.kind == O1K_LCLVAR) || (curAssertion->op1.kind == O1K_EXACT_TYPE) ||
(curAssertion->op1.kind == O1K_SUBTYPE))
{
printf("V%02u", curAssertion->op1.lcl.lclNum);
if (curAssertion->op1.lcl.ssaNum != SsaConfig::RESERVED_SSA_NUM)
{
printf(".%02u", curAssertion->op1.lcl.ssaNum);
}
}
else if (curAssertion->op1.kind == O1K_ARR_BND)
{
printf("[idx: " FMT_VN, curAssertion->op1.bnd.vnIdx);
vnStore->vnDump(this, curAssertion->op1.bnd.vnIdx);
printf("; len: " FMT_VN, curAssertion->op1.bnd.vnLen);
vnStore->vnDump(this, curAssertion->op1.bnd.vnLen);
printf("]");
}
else if (curAssertion->op1.kind == O1K_BOUND_OPER_BND)
{
printf("Oper_Bnd");
vnStore->vnDump(this, curAssertion->op1.vn);
}
else if (curAssertion->op1.kind == O1K_BOUND_LOOP_BND)
{
printf("Loop_Bnd");
vnStore->vnDump(this, curAssertion->op1.vn);
}
else if (curAssertion->op1.kind == O1K_CONSTANT_LOOP_BND)
{
printf("Const_Loop_Bnd");
vnStore->vnDump(this, curAssertion->op1.vn);
}
else if (curAssertion->op1.kind == O1K_CONSTANT_LOOP_BND_UN)
{
printf("Const_Loop_Bnd_Un");
vnStore->vnDump(this, curAssertion->op1.vn);
}
else
{
printf("?op1.kind?");
}
if (curAssertion->assertionKind == OAK_SUBRANGE)
{
printf(" in ");
}
else if (curAssertion->assertionKind == OAK_EQUAL)
{
if (curAssertion->op1.kind == O1K_LCLVAR)
{
printf(" == ");
}
else
{
printf(" is ");
}
}
else if (curAssertion->assertionKind == OAK_NO_THROW)
{
printf(" in range ");
}
else if (curAssertion->assertionKind == OAK_NOT_EQUAL)
{
if (curAssertion->op1.kind == O1K_LCLVAR)
{
printf(" != ");
}
else
{
printf(" is not ");
}
}
else
{
printf(" ?assertionKind? ");
}
if (curAssertion->op1.kind != O1K_ARR_BND)
{
switch (curAssertion->op2.kind)
{
case O2K_LCLVAR_COPY:
printf("V%02u", curAssertion->op2.lcl.lclNum);
if (curAssertion->op1.lcl.ssaNum != SsaConfig::RESERVED_SSA_NUM)
{
printf(".%02u", curAssertion->op1.lcl.ssaNum);
}
break;
case O2K_CONST_INT:
case O2K_IND_CNS_INT:
if (curAssertion->op1.kind == O1K_EXACT_TYPE)
{
ssize_t iconVal = curAssertion->op2.u1.iconVal;
if (IsTargetAbi(CORINFO_NATIVEAOT_ABI) || opts.IsReadyToRun())
{
printf("Exact Type MT(0x%p)", dspPtr(iconVal));
}
else
{
printf("Exact Type MT(0x%p %s)", dspPtr(iconVal),
eeGetClassName((CORINFO_CLASS_HANDLE)iconVal));
}
// We might want to assert:
// assert(curAssertion->op2.HasIconFlag());
// However, if we run CSE with shared constant mode, we may end up with an expression instead
// of the original handle value. If we then use JitOptRepeat to re-build value numbers, we lose
// knowledge that the constant was ever a handle, as the expression creating the original value
// was not (and can't be) assigned a handle flag.
}
else if (curAssertion->op1.kind == O1K_SUBTYPE)
{
ssize_t iconVal = curAssertion->op2.u1.iconVal;
if (IsTargetAbi(CORINFO_NATIVEAOT_ABI) || opts.IsReadyToRun())
{
printf("MT(0x%p)", dspPtr(iconVal));
}
else
{
printf("MT(0x%p %s)", dspPtr(iconVal), eeGetClassName((CORINFO_CLASS_HANDLE)iconVal));
}
assert(curAssertion->op2.HasIconFlag());
}
else if ((curAssertion->op1.kind == O1K_BOUND_OPER_BND) ||
(curAssertion->op1.kind == O1K_BOUND_LOOP_BND) ||
(curAssertion->op1.kind == O1K_CONSTANT_LOOP_BND) ||
(curAssertion->op1.kind == O1K_CONSTANT_LOOP_BND_UN))
{
assert(!optLocalAssertionProp);
vnStore->vnDump(this, curAssertion->op2.vn);
}
else
{
var_types op1Type = lvaGetDesc(curAssertion->op1.lcl.lclNum)->lvType;
if (op1Type == TYP_REF)
{
if (curAssertion->op2.u1.iconVal == 0)
{
printf("null");
}
else
{
printf("[%08p]", dspPtr(curAssertion->op2.u1.iconVal));
}
}
else
{
if (curAssertion->op2.HasIconFlag())
{
printf("[%08p]", dspPtr(curAssertion->op2.u1.iconVal));
}
else
{
printf("%d", curAssertion->op2.u1.iconVal);
}
}
}
break;
case O2K_CONST_LONG:
printf("0x%016llx", curAssertion->op2.lconVal);
break;
case O2K_CONST_DOUBLE:
if (FloatingPointUtils::isNegativeZero(curAssertion->op2.dconVal))
{
printf("-0.00000");
}
else
{
printf("%#lg", curAssertion->op2.dconVal);
}
break;
case O2K_ZEROOBJ:
printf("ZeroObj");
break;
case O2K_SUBRANGE:
IntegralRange::Print(curAssertion->op2.u2);
break;
default:
printf("?op2.kind?");
break;
}
}
if (assertionIndex > 0)
{
printf(", index = ");
optPrintAssertionIndex(assertionIndex);
}
printf("\n");
}
void Compiler::optPrintAssertionIndex(AssertionIndex index)
{
if (index == NO_ASSERTION_INDEX)
{
printf("#NA");
return;
}
printf("#%02u", index);
}
void Compiler::optPrintAssertionIndices(ASSERT_TP assertions)
{
if (BitVecOps::IsEmpty(apTraits, assertions))
{
optPrintAssertionIndex(NO_ASSERTION_INDEX);
return;
}
BitVecOps::Iter iter(apTraits, assertions);
unsigned bitIndex = 0;
if (iter.NextElem(&bitIndex))
{
optPrintAssertionIndex(static_cast<AssertionIndex>(bitIndex + 1));
while (iter.NextElem(&bitIndex))
{
printf(" ");
optPrintAssertionIndex(static_cast<AssertionIndex>(bitIndex + 1));
}
}
}