-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
valuenum.cpp
14413 lines (12685 loc) · 489 KB
/
valuenum.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 ValueNum XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
#include "valuenum.h"
#include "ssaconfig.h"
// We need to use target-specific NaN values when statically compute expressions.
// Otherwise, cross crossgen (e.g. x86_arm) would have different binary outputs
// from native crossgen (i.e. arm_arm) when the NaN got "embedded" into code.
//
// For example, when placing NaN value in r3 register
// x86_arm crossgen would emit
// movw r3, 0x00
// movt r3, 0xfff8
// while arm_arm crossgen (and JIT) output is
// movw r3, 0x00
// movt r3, 0x7ff8
struct FloatTraits
{
//------------------------------------------------------------------------
// NaN: Return target-specific float NaN value
//
// Notes:
// "Default" NaN value returned by expression 0.0f / 0.0f on x86/x64 has
// different binary representation (0xffc00000) than NaN on
// ARM32/ARM64/LoongArch64 (0x7fc00000).
static float NaN()
{
#if defined(TARGET_XARCH)
unsigned bits = 0xFFC00000u;
#elif defined(TARGET_ARMARCH) || defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
unsigned bits = 0x7FC00000u;
#else
#error Unsupported or unset target architecture
#endif
float result;
static_assert(sizeof(bits) == sizeof(result), "sizeof(unsigned) must equal sizeof(float)");
memcpy(&result, &bits, sizeof(result));
return result;
}
};
struct DoubleTraits
{
//------------------------------------------------------------------------
// NaN: Return target-specific double NaN value
//
// Notes:
// "Default" NaN value returned by expression 0.0 / 0.0 on x86/x64 has
// different binary representation (0xfff8000000000000) than NaN on
// ARM32/ARM64/LoongArch64 (0x7ff8000000000000).
static double NaN()
{
#if defined(TARGET_XARCH)
unsigned long long bits = 0xFFF8000000000000ull;
#elif defined(TARGET_ARMARCH) || defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
unsigned long long bits = 0x7FF8000000000000ull;
#else
#error Unsupported or unset target architecture
#endif
double result;
static_assert(sizeof(bits) == sizeof(result), "sizeof(unsigned long long) must equal sizeof(double)");
memcpy(&result, &bits, sizeof(result));
return result;
}
};
//------------------------------------------------------------------------
// FpAdd: Computes value1 + value2
//
// Return Value:
// TFpTraits::NaN() - If target ARM32/ARM64 and result value is NaN
// value1 + value2 - Otherwise
//
// Notes:
// See FloatTraits::NaN() and DoubleTraits::NaN() notes.
template <typename TFp, typename TFpTraits>
TFp FpAdd(TFp value1, TFp value2)
{
#if defined(TARGET_ARMARCH) || defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
// If [value1] is negative infinity and [value2] is positive infinity
// the result is NaN.
// If [value1] is positive infinity and [value2] is negative infinity
// the result is NaN.
if (!FloatingPointUtils::isFinite(value1) && !FloatingPointUtils::isFinite(value2))
{
if (value1 < 0 && value2 > 0)
{
return TFpTraits::NaN();
}
if (value1 > 0 && value2 < 0)
{
return TFpTraits::NaN();
}
}
#endif // TARGET_ARMARCH || TARGET_LOONGARCH64 || TARGET_RISCV64
return value1 + value2;
}
//------------------------------------------------------------------------
// FpSub: Computes value1 - value2
//
// Return Value:
// TFpTraits::NaN() - If target ARM32/ARM64 and result value is NaN
// value1 - value2 - Otherwise
//
// Notes:
// See FloatTraits::NaN() and DoubleTraits::NaN() notes.
template <typename TFp, typename TFpTraits>
TFp FpSub(TFp value1, TFp value2)
{
#if defined(TARGET_ARMARCH) || defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
// If [value1] is positive infinity and [value2] is positive infinity
// the result is NaN.
// If [value1] is negative infinity and [value2] is negative infinity
// the result is NaN.
if (!FloatingPointUtils::isFinite(value1) && !FloatingPointUtils::isFinite(value2))
{
if (value1 > 0 && value2 > 0)
{
return TFpTraits::NaN();
}
if (value1 < 0 && value2 < 0)
{
return TFpTraits::NaN();
}
}
#endif // TARGET_ARMARCH || TARGET_LOONGARCH64 || TARGET_RISCV64
return value1 - value2;
}
//------------------------------------------------------------------------
// FpMul: Computes value1 * value2
//
// Return Value:
// TFpTraits::NaN() - If target ARM32/ARM64 and result value is NaN
// value1 * value2 - Otherwise
//
// Notes:
// See FloatTraits::NaN() and DoubleTraits::NaN() notes.
template <typename TFp, typename TFpTraits>
TFp FpMul(TFp value1, TFp value2)
{
#if defined(TARGET_ARMARCH) || defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
// From the ECMA standard:
//
// If [value1] is zero and [value2] is infinity
// the result is NaN.
// If [value1] is infinity and [value2] is zero
// the result is NaN.
if (value1 == 0 && !FloatingPointUtils::isFinite(value2) && !FloatingPointUtils::isNaN(value2))
{
return TFpTraits::NaN();
}
if (!FloatingPointUtils::isFinite(value1) && !FloatingPointUtils::isNaN(value1) && value2 == 0)
{
return TFpTraits::NaN();
}
#endif // TARGET_ARMARCH || TARGET_LOONGARCH64 || TARGET_RISCV64
return value1 * value2;
}
//------------------------------------------------------------------------
// FpDiv: Computes value1 / value2
//
// Return Value:
// TFpTraits::NaN() - If target ARM32/ARM64 and result value is NaN
// value1 / value2 - Otherwise
//
// Notes:
// See FloatTraits::NaN() and DoubleTraits::NaN() notes.
template <typename TFp, typename TFpTraits>
TFp FpDiv(TFp dividend, TFp divisor)
{
#if defined(TARGET_ARMARCH) || defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
// From the ECMA standard:
//
// If [dividend] is zero and [divisor] is zero
// the result is NaN.
// If [dividend] is infinity and [divisor] is infinity
// the result is NaN.
if (dividend == 0 && divisor == 0)
{
return TFpTraits::NaN();
}
else if (!FloatingPointUtils::isFinite(dividend) && !FloatingPointUtils::isNaN(dividend) &&
!FloatingPointUtils::isFinite(divisor) && !FloatingPointUtils::isNaN(divisor))
{
return TFpTraits::NaN();
}
#endif // TARGET_ARMARCH || TARGET_LOONGARCH64 || TARGET_RISCV64
return dividend / divisor;
}
template <typename TFp, typename TFpTraits>
TFp FpRem(TFp dividend, TFp divisor)
{
// From the ECMA standard:
//
// If [divisor] is zero or [dividend] is infinity
// the result is NaN.
// If [divisor] is infinity,
// the result is [dividend]
if (divisor == 0 || !FloatingPointUtils::isFinite(dividend))
{
return TFpTraits::NaN();
}
else if (!FloatingPointUtils::isFinite(divisor) && !FloatingPointUtils::isNaN(divisor))
{
return dividend;
}
return (TFp)fmod((double)dividend, (double)divisor);
}
//--------------------------------------------------------------------------------
// GetVNFuncForNode: Given a GenTree node, this returns the proper VNFunc to use
// for ValueNumbering
//
// Arguments:
// node - The GenTree node that we need the VNFunc for.
//
// Return Value:
// The VNFunc to use for this GenTree node
//
// Notes:
// Some opers have their semantics affected by GTF flags so they need to be
// replaced by special VNFunc values:
// - relops are affected by GTF_UNSIGNED/GTF_RELOP_NAN_UN
// - ADD/SUB/MUL are affected by GTF_OVERFLOW and GTF_UNSIGNED
//
VNFunc GetVNFuncForNode(GenTree* node)
{
static const VNFunc relopUnFuncs[]{VNF_LT_UN, VNF_LE_UN, VNF_GE_UN, VNF_GT_UN};
static_assert_no_msg(GT_LE - GT_LT == 1);
static_assert_no_msg(GT_GE - GT_LT == 2);
static_assert_no_msg(GT_GT - GT_LT == 3);
static const VNFunc binopOvfFuncs[]{VNF_ADD_OVF, VNF_SUB_OVF, VNF_MUL_OVF};
static const VNFunc binopUnOvfFuncs[]{VNF_ADD_UN_OVF, VNF_SUB_UN_OVF, VNF_MUL_UN_OVF};
static_assert_no_msg(GT_SUB - GT_ADD == 1);
static_assert_no_msg(GT_MUL - GT_ADD == 2);
switch (node->OperGet())
{
case GT_EQ:
if (varTypeIsFloating(node->gtGetOp1()))
{
assert(varTypeIsFloating(node->gtGetOp2()));
assert((node->gtFlags & GTF_RELOP_NAN_UN) == 0);
}
break;
case GT_NE:
if (varTypeIsFloating(node->gtGetOp1()))
{
assert(varTypeIsFloating(node->gtGetOp2()));
assert((node->gtFlags & GTF_RELOP_NAN_UN) != 0);
}
break;
case GT_LT:
case GT_LE:
case GT_GT:
case GT_GE:
if (varTypeIsFloating(node->gtGetOp1()))
{
assert(varTypeIsFloating(node->gtGetOp2()));
if ((node->gtFlags & GTF_RELOP_NAN_UN) != 0)
{
return relopUnFuncs[node->OperGet() - GT_LT];
}
}
else
{
assert(varTypeIsIntegralOrI(node->gtGetOp1()));
assert(varTypeIsIntegralOrI(node->gtGetOp2()));
if (node->IsUnsigned())
{
return relopUnFuncs[node->OperGet() - GT_LT];
}
}
break;
case GT_ADD:
case GT_SUB:
case GT_MUL:
if (varTypeIsIntegralOrI(node->gtGetOp1()) && node->gtOverflow())
{
assert(varTypeIsIntegralOrI(node->gtGetOp2()));
if (node->IsUnsigned())
{
return binopUnOvfFuncs[node->OperGet() - GT_ADD];
}
else
{
return binopOvfFuncs[node->OperGet() - GT_ADD];
}
}
break;
#ifdef FEATURE_HW_INTRINSICS
case GT_HWINTRINSIC:
return VNFunc(VNF_HWI_FIRST + (node->AsHWIntrinsic()->GetHWIntrinsicId() - NI_HW_INTRINSIC_START - 1));
#endif // FEATURE_HW_INTRINSICS
case GT_CAST:
// GT_CAST can overflow but it has special handling and it should not appear here.
unreached();
default:
// Make sure we don't miss an onverflow oper, if a new one is ever added.
assert(!GenTree::OperMayOverflow(node->OperGet()));
break;
}
return VNFunc(node->OperGet());
}
bool ValueNumStore::VNFuncIsOverflowArithmetic(VNFunc vnf)
{
static_assert_no_msg(VNF_ADD_OVF + 1 == VNF_SUB_OVF);
static_assert_no_msg(VNF_SUB_OVF + 1 == VNF_MUL_OVF);
static_assert_no_msg(VNF_MUL_OVF + 1 == VNF_ADD_UN_OVF);
static_assert_no_msg(VNF_ADD_UN_OVF + 1 == VNF_SUB_UN_OVF);
static_assert_no_msg(VNF_SUB_UN_OVF + 1 == VNF_MUL_UN_OVF);
return VNF_ADD_OVF <= vnf && vnf <= VNF_MUL_UN_OVF;
}
bool ValueNumStore::VNFuncIsNumericCast(VNFunc vnf)
{
return (vnf == VNF_Cast) || (vnf == VNF_CastOvf);
}
unsigned ValueNumStore::VNFuncArity(VNFunc vnf)
{
// Read the bit field out of the table...
return (s_vnfOpAttribs[vnf] & VNFOA_ArityMask) >> VNFOA_ArityShift;
}
template <>
bool ValueNumStore::IsOverflowIntDiv(int v0, int v1)
{
return (v1 == -1) && (v0 == INT32_MIN);
}
template <>
bool ValueNumStore::IsOverflowIntDiv(INT64 v0, INT64 v1)
{
return (v1 == -1) && (v0 == INT64_MIN);
}
template <typename T>
bool ValueNumStore::IsOverflowIntDiv(T v0, T v1)
{
return false;
}
template <>
bool ValueNumStore::IsIntZero(int v)
{
return v == 0;
}
template <>
bool ValueNumStore::IsIntZero(unsigned v)
{
return v == 0;
}
template <>
bool ValueNumStore::IsIntZero(INT64 v)
{
return v == 0;
}
template <>
bool ValueNumStore::IsIntZero(UINT64 v)
{
return v == 0;
}
template <typename T>
bool ValueNumStore::IsIntZero(T v)
{
return false;
}
ValueNumStore::ValueNumStore(Compiler* comp, CompAllocator alloc)
: m_pComp(comp)
, m_alloc(alloc)
, m_nextChunkBase(0)
, m_fixedPointMapSels(alloc, 8)
, m_checkedBoundVNs(alloc)
, m_chunks(alloc, 8)
, m_intCnsMap(nullptr)
, m_longCnsMap(nullptr)
, m_handleMap(nullptr)
, m_embeddedToCompileTimeHandleMap(alloc)
, m_fieldAddressToFieldSeqMap(alloc)
, m_floatCnsMap(nullptr)
, m_doubleCnsMap(nullptr)
, m_byrefCnsMap(nullptr)
#if defined(FEATURE_SIMD)
, m_simd8CnsMap(nullptr)
, m_simd12CnsMap(nullptr)
, m_simd16CnsMap(nullptr)
#if defined(TARGET_XARCH)
, m_simd32CnsMap(nullptr)
, m_simd64CnsMap(nullptr)
, m_simdMaskCnsMap(nullptr)
#endif // TARGET_XARCH
#endif // FEATURE_SIMD
, m_VNFunc0Map(nullptr)
, m_VNFunc1Map(nullptr)
, m_VNFunc2Map(nullptr)
, m_VNFunc3Map(nullptr)
, m_VNFunc4Map(nullptr)
#ifdef DEBUG
, m_numMapSels(0)
#endif
{
// We have no current allocation chunks.
for (unsigned i = 0; i < TYP_COUNT; i++)
{
for (unsigned j = CEA_Const; j <= CEA_Count; j++)
{
m_curAllocChunk[i][j] = NoChunk;
}
}
for (unsigned i = 0; i < SmallIntConstNum; i++)
{
m_VNsForSmallIntConsts[i] = NoVN;
}
// We will reserve chunk 0 to hold some special constants.
Chunk* specialConstChunk = new (m_alloc) Chunk(m_alloc, &m_nextChunkBase, TYP_REF, CEA_Const);
specialConstChunk->m_numUsed += SRC_NumSpecialRefConsts;
ChunkNum cn = m_chunks.Push(specialConstChunk);
assert(cn == 0);
m_mapSelectBudget = (int)JitConfig.JitVNMapSelBudget(); // We cast the unsigned DWORD to a signed int.
// This value must be non-negative and non-zero, reset the value to DEFAULT_MAP_SELECT_BUDGET if it isn't.
if (m_mapSelectBudget <= 0)
{
m_mapSelectBudget = DEFAULT_MAP_SELECT_BUDGET;
}
#ifdef DEBUG
if (comp->compStressCompile(Compiler::STRESS_VN_BUDGET, 50))
{
// Bias toward smaller budgets as we want to stress returning
// unexpectedly opaque results.
//
CLRRandom* random = comp->m_inlineStrategy->GetRandom(comp->info.compMethodHash());
double p = random->NextDouble();
if (p <= 0.5)
{
m_mapSelectBudget = random->Next(0, 5);
}
else
{
int limit = random->Next(1, DEFAULT_MAP_SELECT_BUDGET + 1);
m_mapSelectBudget = random->Next(0, limit);
}
JITDUMP("VN Stress: setting select budget to %u\n", m_mapSelectBudget);
}
#endif
}
//
// Unary EvalOp
//
template <typename T>
T ValueNumStore::EvalOp(VNFunc vnf, T v0)
{
genTreeOps oper = genTreeOps(vnf);
// Here we handle unary ops that are the same for all types.
switch (oper)
{
case GT_NEG:
// Note that GT_NEG is the only valid unary floating point operation
return -v0;
default:
break;
}
// Otherwise must be handled by the type specific method
return EvalOpSpecialized(vnf, v0);
}
template <>
double ValueNumStore::EvalOpSpecialized<double>(VNFunc vnf, double v0)
{
// Here we handle specialized double unary ops.
noway_assert(!"EvalOpSpecialized<double> - unary");
return 0.0;
}
template <>
float ValueNumStore::EvalOpSpecialized<float>(VNFunc vnf, float v0)
{
// Here we handle specialized float unary ops.
noway_assert(!"EvalOpSpecialized<float> - unary");
return 0.0f;
}
template <typename T>
T ValueNumStore::EvalOpSpecialized(VNFunc vnf, T v0)
{
if (vnf < VNF_Boundary)
{
genTreeOps oper = genTreeOps(vnf);
switch (oper)
{
case GT_NEG:
return -v0;
case GT_NOT:
return ~v0;
case GT_BSWAP16:
{
UINT16 v0_unsigned = UINT16(v0);
v0_unsigned = ((v0_unsigned >> 8) & 0xFF) | ((v0_unsigned << 8) & 0xFF00);
return T(v0_unsigned);
}
case GT_BSWAP:
if (sizeof(T) == 4)
{
UINT32 v0_unsigned = UINT32(v0);
v0_unsigned = ((v0_unsigned >> 24) & 0xFF) | ((v0_unsigned >> 8) & 0xFF00) |
((v0_unsigned << 8) & 0xFF0000) | ((v0_unsigned << 24) & 0xFF000000);
return T(v0_unsigned);
}
else if (sizeof(T) == 8)
{
UINT64 v0_unsigned = UINT64(v0);
v0_unsigned = ((v0_unsigned >> 56) & 0xFF) | ((v0_unsigned >> 40) & 0xFF00) |
((v0_unsigned >> 24) & 0xFF0000) | ((v0_unsigned >> 8) & 0xFF000000) |
((v0_unsigned << 8) & 0xFF00000000) | ((v0_unsigned << 24) & 0xFF0000000000) |
((v0_unsigned << 40) & 0xFF000000000000) | ((v0_unsigned << 56) & 0xFF00000000000000);
return T(v0_unsigned);
}
else
{
break; // unknown primitive
}
default:
break;
}
}
noway_assert(!"Unhandled operation in EvalOpSpecialized<T> - unary");
return v0;
}
//
// Binary EvalOp
//
template <typename T>
T ValueNumStore::EvalOp(VNFunc vnf, T v0, T v1)
{
// Here we handle the binary ops that are the same for all types.
// Currently there are none (due to floating point NaN representations)
// Otherwise must be handled by the type specific method
return EvalOpSpecialized(vnf, v0, v1);
}
template <>
double ValueNumStore::EvalOpSpecialized<double>(VNFunc vnf, double v0, double v1)
{
// Here we handle specialized double binary ops.
if (vnf < VNF_Boundary)
{
genTreeOps oper = genTreeOps(vnf);
// Here we handle
switch (oper)
{
case GT_ADD:
return FpAdd<double, DoubleTraits>(v0, v1);
case GT_SUB:
return FpSub<double, DoubleTraits>(v0, v1);
case GT_MUL:
return FpMul<double, DoubleTraits>(v0, v1);
case GT_DIV:
return FpDiv<double, DoubleTraits>(v0, v1);
case GT_MOD:
return FpRem<double, DoubleTraits>(v0, v1);
default:
// For any other value of 'oper', we will assert below
break;
}
}
noway_assert(!"EvalOpSpecialized<double> - binary");
return v0;
}
template <>
float ValueNumStore::EvalOpSpecialized<float>(VNFunc vnf, float v0, float v1)
{
// Here we handle specialized float binary ops.
if (vnf < VNF_Boundary)
{
genTreeOps oper = genTreeOps(vnf);
// Here we handle
switch (oper)
{
case GT_ADD:
return FpAdd<float, FloatTraits>(v0, v1);
case GT_SUB:
return FpSub<float, FloatTraits>(v0, v1);
case GT_MUL:
return FpMul<float, FloatTraits>(v0, v1);
case GT_DIV:
return FpDiv<float, FloatTraits>(v0, v1);
case GT_MOD:
return FpRem<float, FloatTraits>(v0, v1);
default:
// For any other value of 'oper', we will assert below
break;
}
}
assert(!"EvalOpSpecialized<float> - binary");
return v0;
}
template <typename T>
T ValueNumStore::EvalOpSpecialized(VNFunc vnf, T v0, T v1)
{
typedef typename std::make_unsigned<T>::type UT;
assert((sizeof(T) == 4) || (sizeof(T) == 8));
// Here we handle binary ops that are the same for all integer types
if (vnf < VNF_Boundary)
{
genTreeOps oper = genTreeOps(vnf);
switch (oper)
{
case GT_ADD:
return v0 + v1;
case GT_SUB:
return v0 - v1;
case GT_MUL:
return v0 * v1;
case GT_DIV:
assert(IsIntZero(v1) == false);
assert(IsOverflowIntDiv(v0, v1) == false);
return v0 / v1;
case GT_MOD:
assert(IsIntZero(v1) == false);
assert(IsOverflowIntDiv(v0, v1) == false);
return v0 % v1;
case GT_UDIV:
assert(IsIntZero(v1) == false);
return T(UT(v0) / UT(v1));
case GT_UMOD:
assert(IsIntZero(v1) == false);
return T(UT(v0) % UT(v1));
case GT_AND:
return v0 & v1;
case GT_OR:
return v0 | v1;
case GT_XOR:
return v0 ^ v1;
case GT_LSH:
if (sizeof(T) == 8)
{
return v0 << (v1 & 0x3F);
}
else
{
return v0 << v1;
}
case GT_RSH:
if (sizeof(T) == 8)
{
return v0 >> (v1 & 0x3F);
}
else
{
return v0 >> v1;
}
case GT_RSZ:
if (sizeof(T) == 8)
{
return UINT64(v0) >> (v1 & 0x3F);
}
else
{
return UINT32(v0) >> v1;
}
case GT_ROL:
if (sizeof(T) == 8)
{
return (v0 << v1) | (UINT64(v0) >> (64 - v1));
}
else
{
return (v0 << v1) | (UINT32(v0) >> (32 - v1));
}
case GT_ROR:
if (sizeof(T) == 8)
{
return (v0 << (64 - v1)) | (UINT64(v0) >> v1);
}
else
{
return (v0 << (32 - v1)) | (UINT32(v0) >> v1);
}
default:
// For any other value of 'oper', we will assert below
break;
}
}
else // must be a VNF_ function
{
switch (vnf)
{
// Here we handle those that are the same for all integer types.
case VNF_ADD_OVF:
case VNF_ADD_UN_OVF:
assert(!CheckedOps::AddOverflows(v0, v1, vnf == VNF_ADD_UN_OVF));
return v0 + v1;
case VNF_SUB_OVF:
case VNF_SUB_UN_OVF:
assert(!CheckedOps::SubOverflows(v0, v1, vnf == VNF_SUB_UN_OVF));
return v0 - v1;
case VNF_MUL_OVF:
case VNF_MUL_UN_OVF:
assert(!CheckedOps::MulOverflows(v0, v1, vnf == VNF_MUL_UN_OVF));
return v0 * v1;
default:
// For any other value of 'vnf', we will assert below
break;
}
}
noway_assert(!"Unhandled operation in EvalOpSpecialized<T> - binary");
return v0;
}
template <>
int ValueNumStore::EvalComparison<double>(VNFunc vnf, double v0, double v1)
{
// Here we handle specialized double comparisons.
// We must check for a NaN argument as they they need special handling
bool hasNanArg = (FloatingPointUtils::isNaN(v0) || FloatingPointUtils::isNaN(v1));
if (vnf < VNF_Boundary)
{
genTreeOps oper = genTreeOps(vnf);
if (hasNanArg)
{
// return false in all cases except for GT_NE;
return (oper == GT_NE);
}
switch (oper)
{
case GT_EQ:
return v0 == v1;
case GT_NE:
return v0 != v1;
case GT_GT:
return v0 > v1;
case GT_GE:
return v0 >= v1;
case GT_LT:
return v0 < v1;
case GT_LE:
return v0 <= v1;
default:
// For any other value of 'oper', we will assert below
break;
}
}
else // must be a VNF_ function
{
if (hasNanArg)
{
// unordered comparisons with NaNs always return true
return true;
}
switch (vnf)
{
case VNF_GT_UN:
return v0 > v1;
case VNF_GE_UN:
return v0 >= v1;
case VNF_LT_UN:
return v0 < v1;
case VNF_LE_UN:
return v0 <= v1;
default:
// For any other value of 'vnf', we will assert below
break;
}
}
noway_assert(!"Unhandled operation in EvalComparison<double>");
return 0;
}
template <>
int ValueNumStore::EvalComparison<float>(VNFunc vnf, float v0, float v1)
{
// Here we handle specialized float comparisons.
// We must check for a NaN argument as they they need special handling
bool hasNanArg = (FloatingPointUtils::isNaN(v0) || FloatingPointUtils::isNaN(v1));
if (vnf < VNF_Boundary)
{
genTreeOps oper = genTreeOps(vnf);
if (hasNanArg)
{
// return false in all cases except for GT_NE;
return (oper == GT_NE);
}
switch (oper)
{
case GT_EQ:
return v0 == v1;
case GT_NE:
return v0 != v1;
case GT_GT:
return v0 > v1;
case GT_GE:
return v0 >= v1;
case GT_LT:
return v0 < v1;
case GT_LE:
return v0 <= v1;
default:
// For any other value of 'oper', we will assert below
break;
}
}
else // must be a VNF_ function
{
if (hasNanArg)
{
// unordered comparisons with NaNs always return true
return true;
}
switch (vnf)
{
case VNF_GT_UN:
return v0 > v1;
case VNF_GE_UN:
return v0 >= v1;
case VNF_LT_UN:
return v0 < v1;
case VNF_LE_UN:
return v0 <= v1;
default:
// For any other value of 'vnf', we will assert below
break;
}
}
noway_assert(!"Unhandled operation in EvalComparison<float>");
return 0;
}
template <typename T>
int ValueNumStore::EvalComparison(VNFunc vnf, T v0, T v1)
{
typedef typename std::make_unsigned<T>::type UT;
// Here we handle the compare ops that are the same for all integer types.
if (vnf < VNF_Boundary)
{
genTreeOps oper = genTreeOps(vnf);
switch (oper)
{
case GT_EQ:
return v0 == v1;
case GT_NE:
return v0 != v1;
case GT_GT:
return v0 > v1;
case GT_GE:
return v0 >= v1;
case GT_LT:
return v0 < v1;
case GT_LE:
return v0 <= v1;
default:
// For any other value of 'oper', we will assert below
break;
}
}
else // must be a VNF_ function
{
switch (vnf)
{
case VNF_GT_UN:
return T(UT(v0) > UT(v1));
case VNF_GE_UN:
return T(UT(v0) >= UT(v1));
case VNF_LT_UN:
return T(UT(v0) < UT(v1));
case VNF_LE_UN:
return T(UT(v0) <= UT(v1));
default:
// For any other value of 'vnf', we will assert below
break;
}
}
noway_assert(!"Unhandled operation in EvalComparison<T>");
return 0;
}
// Create a ValueNum for an exception set singleton for 'x'
//
ValueNum ValueNumStore::VNExcSetSingleton(ValueNum x)
{
return VNForFuncNoFolding(TYP_REF, VNF_ExcSetCons, x, VNForEmptyExcSet());
}
// Create a ValueNumPair for an exception set singleton for 'xp'
//
ValueNumPair ValueNumStore::VNPExcSetSingleton(ValueNumPair xp)
{
return ValueNumPair(VNExcSetSingleton(xp.GetLiberal()), VNExcSetSingleton(xp.GetConservative()));
}
//-------------------------------------------------------------------------------------------
// VNCheckAscending: - Helper method used to verify that elements in an exception set list
// are sorted in ascending order. This method only checks that the
// next value in the list has a greater value number than 'item'.
//
// Arguments: