forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvaluenum.h
More file actions
1586 lines (1338 loc) · 63.8 KB
/
valuenum.h
File metadata and controls
1586 lines (1338 loc) · 63.8 KB
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.
// Defines the class "ValueNumStore", which maintains value numbers for a compilation.
// Recall that "value numbering" assigns an integer value number to each expression. The "value
// number property" is that two expressions with the same value number will evaluate to the same value
// at runtime. Expressions with different value numbers may or may not be equivalent. This property
// of value numbers has obvious applications in redundancy-elimination optimizations.
//
// Since value numbers give us a way of talking about the (immutable) values to which expressions
// evaluate, they provide a good "handle" to use for attributing properties to values. For example,
// we might note that some value number represents some particular integer constant -- which has obvious
// application to constant propagation. Or that we know the exact type of some object reference,
// which might be used in devirtualization.
//
// Finally, we will also use value numbers to express control-flow-dependent assertions. Some test may
// imply that after the test, something new is known about a value: that an object reference is non-null
// after a dereference (since control flow continued because no exception was thrown); that an integer value
// is restricted to some subrange in after a comparison test; etc.
// In addition to classical numbering, this implementation also performs disambiguation of heap writes,
// using memory SSA and the guarantee that two managed field accesses will not alias (if the fields do
// not overlap). This is the sole reason for the existence of field sequences - providing information
// to numbering on which addresses represent "proper", non-aliasing, bases for loads and stores. Note
// that a field sequence is *only* about the address - it is the VN's responsibility to properly "give up"
// on reads and stores that are too wide or of otherwise incompatible types - this logic is encapsulated
// in the methods VNApplySelectorsTypeCheck and VNApplySelectorsAssignTypeCoerce. Note as well that this
// design, based on symbols, not offsets, is why the compiler must take such great care in maintaining the
// field sequences throughout its many transformations - accessing a field of type A "through" a field from
// a distinct type B will result in effectively undefined behavior. We say this is by design for classes
// (i. e. that people using Unsafe.As(object) need to *really* know what they're doing) and fix up places
// where the compiler itself produces these type mismatches with structs (which can arise due to "reinterpret
// casts" or morph's transforms which fold copies of structs with bitwise compatible layouts).
//
// Abstractly, numbering maintains states of memory in "maps", which are indexed into with various "selectors",
// loads reading from said maps and stores recording new states for them (note that as with everything VN, the
// "maps" are immutable, thus an update is performed via deriving a new map from an existing one).
//
// Memory states are represented with value numbers corresponding to the following VNFunc's:
// 1. MemOpaque - for opaque memory states, usually caused by writing to byrefs, or other
// non-analyzable memory operations such as atomics or volatile writes (and reads!).
// 2. PhiMemoryDef - this is simply the PHI function applied to multiple reaching memory
// definitions for a given block.
// 3. MapStore - this is the "update" map, it represents a map after a "set" operation at a
// given index. Note that the value stored can itself be a map (memory state), for example,
// with the store to a field of a struct that itself is a field of a class, the value
// recorded for the struct is a MapStore($StructInClassFieldMap, $ValueField, $Value).
// Because of this, these VNs need to have proper types, those being the types that the
// maps they represent updated states of have (in the example above, the VN would have
// a "struct" type, for example). MapStore VNs naturally "chain" together, the next map
// representing an update of the previous, and VNForMapSelect, when it tries to find
// the value traverses through these chains, as long as the indices are constant (meaning
// that they represent distinct locations, e. g. different fields).
// 4. MapSelect - if MapStore is an "update/assign" operation, MapSelect is the "read" one.
// It represents a "value" (which, as with MapStore, can itself can be a memory region)
// taken from another map at a given index. As such, it must have a type that corresponds
// to the "index/selector", in practical terms - the field's type.
//
// Note that we give "placeholder" types (TYP_UNDEF and TYP_UNKNOWN as TYP_MEM and TYP_HEAP)
// to maps that do not represent values found in IR. This is just to avoid confusion and
// facilitate more precise validating checks.
//
// Let's review the following snippet to demonstrate how the MapSelect/MapStore machinery works
// together to deliver the results that it does. Say we have this snippet of (C#) code:
//
// int Procedure(OneClass obj, AnotherClass subj, int objVal, int subjVal)
// {
// obj.StructField.AnotherStructField.ScalarField = objVal;
// subj.OtherScalarField = subjVal;
//
// return obj.StructField.AnotherStructField.ScalarField + subj.OtherScalarField;
// }
//
// On entry, we assign some VN to the GcHeap (VN mostly only cares about GcHeap, so from now on
// the term "heap" will be used to mean GcHeap), $Heap.
//
// A store to the ScalarField is seen. Now, the value numbering of fields is done in the following
// pattern for maps that it builds: [$Heap][$FirstField][$Object][$SecondField]...[$FieldToStoreInto].
// It may seem odd that the indexing is done first for the field, and only then for the object, but
// the reason for that is the fact that it enables MapStores to $Heap to refer to distinct selectors,
// thus enabling the traversal through the map updates when looking for the values that were stored.
// Were $Object VNs used for this, the traversal could not be performed, as two numerically different VNs
// can, obviously, refer to the same object.
//
// With that in mind, the following maps are first built for the store ("field VNs" - VNs for handles):
//
// $StructFieldMap = MapSelect($Heap, $StructField)
// $StructFieldForObjMap = MapSelect($StructFieldMap, $Obj)
// $AnotherStructFieldMap = MapSelect($StructFieldForObjMap, $AnotherStructField)
// $ScalarFieldMap = MapSelect($AnotherStructFieldMap, $ScalarField)
//
// The building of maps for the individual fields in the sequence [AnotherStructField, ScalarField] is
// usually performed by VNApplySelectors, which is just a convenience method that loops over all the
// fields, calling VNForMapSelect on them. Now that we know where to store, the store maps are built:
//
// $NewAnotherStructFieldMap = MapStore($AnotherStructFieldMap, $ScalarField, $ObjVal)
// $NewStructFieldForObjMap = MapStore($StructFieldForObjMap, $AnotherStructField, $NewAnotherStructFieldMap)
// $NewStructFieldMap = MapStore($StructFieldMap, $Obj, $NewStructFieldForObjMap)
// $NewHeap = MapStore($Heap, $StructField, $NewStructFieldMap)
//
// Notice that the maps are built in the opposite order, as we must first know the value of the "narrower"
// map to store into the "wider" map (this is implemented via recursion in VNApplySelectorsAssign).
//
// Similarly, the numbering is performed for "subj.OtherScalarField = subjVal", and the heap state updated
// (say to $NewHeapWithSubj). Now when we call VNForMapSelect to find out the stored values when numbering
// the reads, the following traversal is performed (the '[]' operator representing VNForMapSelect):
//
// $obj.StructField.AnotherStructField.ScalarField
// = $NewHeapWithSubj[$StructField][$Obj][$AnotherStructField][ScalarField]:
// "$NewHeapWithSubj.Index == $StructField" => false (not the needed map).
// "IsConst($NewHeapWithSubj.Index) && IsConst($StructField)" => true (can continue, non-aliasing indices).
// "$NewHeap.Index == $StructField" => true, Value is $NewStructFieldMap.
// "$NewStructFieldMap.Index == $Obj" => true, Value is $NewStructFieldForObjMap.
// "$NewStructFieldForObjMap.Index == $AnotherStructField" => true, Value is $NewAnotherStructFieldMap.
// "$NewAnotherStructFieldMap.Index == $ScalarField" => true, Value is $ObjVal (found the value!).
//
// And similarly for the $SubjVal - we end up with a nice $Add($ObjVal, $SubjVal) feeding the return.
//
// While the above example focuses on fields, the idea is universal to all supported location types. Statics are
// modeled as straight indicies into the heap (MapSelect($Heap, $Field) returns the value of the field for them),
// arrays - like fields, but with the primiary selector being not the first field, but the "equivalence class" of
// an array, i. e. the type of its elements, taking into account things like "int[]" being legally aliasable as
// "uint[]". MapSelect & MapStore are also used to number local fields, with the primary selectors being (VNs of)
// their local numbers.
/*****************************************************************************/
#ifndef _VALUENUM_H_
#define _VALUENUM_H_
/*****************************************************************************/
#include "vartype.h"
// For "GT_COUNT"
#include "gentree.h"
// Defines the type ValueNum.
#include "valuenumtype.h"
// Defines the type SmallHashTable.
#include "smallhash.h"
// A "ValueNumStore" represents the "universe" of value numbers used in a single
// compilation.
// All members of the enumeration genTreeOps are also members of VNFunc.
// (Though some of these may be labeled "illegal").
enum VNFunc
{
// Implicitly, elements of genTreeOps here.
VNF_Boundary = GT_COUNT,
#define ValueNumFuncDef(nm, arity, commute, knownNonNull, sharedStatic) VNF_##nm,
#include "valuenumfuncs.h"
VNF_COUNT
};
// Given a GenTree node return the VNFunc that should be used when value numbering
//
VNFunc GetVNFuncForNode(GenTree* node);
// An instance of this struct represents an application of the function symbol
// "m_func" to the first "m_arity" (<= 4) argument values in "m_args."
struct VNFuncApp
{
VNFunc m_func;
unsigned m_arity;
ValueNum m_args[4];
bool Equals(const VNFuncApp& funcApp)
{
if (m_func != funcApp.m_func)
{
return false;
}
if (m_arity != funcApp.m_arity)
{
return false;
}
for (unsigned i = 0; i < m_arity; i++)
{
if (m_args[i] != funcApp.m_args[i])
{
return false;
}
}
return true;
}
};
// We use a unique prefix character when printing value numbers in dumps: i.e. $1c0
// This define is used with string concatenation to put this in printf format strings
#define FMT_VN "$%x"
// We will use this placeholder type for memory maps that do not represent IR values ("field maps", etc).
static const var_types TYP_MEM = TYP_UNDEF;
// We will use this placeholder type for memory maps representing "the heap" (GcHeap/ByrefExposed).
static const var_types TYP_HEAP = TYP_UNKNOWN;
class ValueNumStore
{
public:
// We will reserve "max unsigned" to represent "not a value number", for maps that might start uninitialized.
static const ValueNum NoVN = UINT32_MAX;
// A second special value, used to indicate that a function evaluation would cause infinite recursion.
static const ValueNum RecursiveVN = UINT32_MAX - 1;
// ==================================================================================================
// VNMap - map from something to ValueNum, where something is typically a constant value or a VNFunc
// This class has two purposes - to abstract the implementation and to validate the ValueNums
// being stored or retrieved.
template <class fromType, class keyfuncs = JitLargePrimitiveKeyFuncs<fromType>>
class VNMap : public JitHashTable<fromType, keyfuncs, ValueNum>
{
public:
VNMap(CompAllocator alloc) : JitHashTable<fromType, keyfuncs, ValueNum>(alloc)
{
}
~VNMap()
{
~VNMap<fromType, keyfuncs>::JitHashTable();
}
bool Set(fromType k, ValueNum val)
{
assert(val != RecursiveVN);
return JitHashTable<fromType, keyfuncs, ValueNum>::Set(k, val);
}
bool Lookup(fromType k, ValueNum* pVal = nullptr) const
{
bool result = JitHashTable<fromType, keyfuncs, ValueNum>::Lookup(k, pVal);
assert(!result || *pVal != RecursiveVN);
return result;
}
};
private:
Compiler* m_pComp;
// For allocations. (Other things?)
CompAllocator m_alloc;
// TODO-Cleanup: should transform "attribs" into a struct with bit fields. That would be simpler...
enum VNFOpAttrib
{
VNFOA_IllegalGenTreeOp = 0x1, // corresponds to a genTreeOps value that is not a legal VN func.
VNFOA_Commutative = 0x2, // 1 iff the function is commutative.
VNFOA_Arity1 = 0x4, // Bits 2,3,4 encode the arity.
VNFOA_Arity2 = 0x8, // Bits 2,3,4 encode the arity.
VNFOA_Arity4 = 0x10, // Bits 2,3,4 encode the arity.
VNFOA_KnownNonNull = 0x20, // 1 iff the result is known to be non-null.
VNFOA_SharedStatic = 0x40, // 1 iff this VNF is represent one of the shared static jit helpers
};
static const unsigned VNFOA_ArityShift = 2;
static const unsigned VNFOA_ArityBits = 3;
static const unsigned VNFOA_MaxArity = (1 << VNFOA_ArityBits) - 1; // Max arity we can represent.
static const unsigned VNFOA_ArityMask = (VNFOA_Arity4 | VNFOA_Arity2 | VNFOA_Arity1);
// These enum constants are used to encode the cast operation in the lowest bits by VNForCastOper
enum VNFCastAttrib
{
VCA_UnsignedSrc = 0x01,
VCA_BitCount = 1, // the number of reserved bits
VCA_ReservedBits = 0x01, // i.e. (VCA_UnsignedSrc)
};
// An array of length GT_COUNT, mapping genTreeOp values to their VNFOpAttrib.
static UINT8* s_vnfOpAttribs;
// Returns "true" iff gtOper is a legal value number function.
// (Requires InitValueNumStoreStatics to have been run.)
static bool GenTreeOpIsLegalVNFunc(genTreeOps gtOper);
// Returns "true" iff "vnf" is a commutative (and thus binary) operator.
// (Requires InitValueNumStoreStatics to have been run.)
static bool VNFuncIsCommutative(VNFunc vnf);
// Returns "true" iff "vnf" is a comparison (and thus binary) operator.
static bool VNFuncIsComparison(VNFunc vnf);
// Returns "true" iff "vnf" can be evaluated for constant arguments.
static bool CanEvalForConstantArgs(VNFunc vnf);
// Returns "true" iff "vnf" should be folded by evaluating the func with constant arguments.
bool VNEvalShouldFold(var_types typ, VNFunc func, ValueNum arg0VN, ValueNum arg1VN);
// return vnf(v0)
template <typename T>
static T EvalOp(VNFunc vnf, T v0);
// returns vnf(v0, v1).
template <typename T>
T EvalOp(VNFunc vnf, T v0, T v1);
// return vnf(v0) or vnf(v0, v1), respectively (must, of course be unary/binary ops, respectively.)
template <typename T>
static T EvalOpSpecialized(VNFunc vnf, T v0);
template <typename T>
T EvalOpSpecialized(VNFunc vnf, T v0, T v1);
template <typename T>
static int EvalComparison(VNFunc vnf, T v0, T v1);
// Should only instantiate (in a non-trivial way) for "int" and "INT64". Returns true iff dividing "v0" by "v1"
// would produce integer overflow (an ArithmeticException -- *not* division by zero, which is separate.)
template <typename T>
static bool IsOverflowIntDiv(T v0, T v1);
// Should only instantiate (in a non-trivial way) for integral types (signed/unsigned int32/int64).
// Returns true iff v is the zero of the appropriate type.
template <typename T>
static bool IsIntZero(T v);
// Given an constant value number return its value.
int GetConstantInt32(ValueNum argVN);
INT64 GetConstantInt64(ValueNum argVN);
double GetConstantDouble(ValueNum argVN);
float GetConstantSingle(ValueNum argVN);
// Assumes that all the ValueNum arguments of each of these functions have been shown to represent constants.
// Assumes that "vnf" is a operator of the appropriate arity (unary for the first, binary for the second).
// Assume that "CanEvalForConstantArgs(vnf)" is true.
// Returns the result of evaluating the function with those constant arguments.
ValueNum EvalFuncForConstantArgs(var_types typ, VNFunc vnf, ValueNum vn0);
ValueNum EvalFuncForConstantArgs(var_types typ, VNFunc vnf, ValueNum vn0, ValueNum vn1);
ValueNum EvalFuncForConstantFPArgs(var_types typ, VNFunc vnf, ValueNum vn0, ValueNum vn1);
ValueNum EvalCastForConstantArgs(var_types typ, VNFunc vnf, ValueNum vn0, ValueNum vn1);
ValueNum EvalUsingMathIdentity(var_types typ, VNFunc vnf, ValueNum vn0, ValueNum vn1);
// This is the constant value used for the default value of m_mapSelectBudget
#define DEFAULT_MAP_SELECT_BUDGET 100 // used by JitVNMapSelBudget
// This is the maximum number of MapSelect terms that can be "considered" as part of evaluation of a top-level
// MapSelect application.
int m_mapSelectBudget;
template <typename T, typename NumMap>
inline ValueNum VnForConst(T cnsVal, NumMap* numMap, var_types varType);
// returns true iff vn is known to be a constant int32 that is > 0
bool IsVNPositiveInt32Constant(ValueNum vn);
public:
// Initializes any static variables of ValueNumStore.
static void InitValueNumStoreStatics();
// Initialize an empty ValueNumStore.
ValueNumStore(Compiler* comp, CompAllocator allocator);
// Returns "true" iff "vnf" (which may have been created by a cast from an integral value) represents
// a legal value number function.
// (Requires InitValueNumStoreStatics to have been run.)
static bool VNFuncIsLegal(VNFunc vnf)
{
return unsigned(vnf) > VNF_Boundary || GenTreeOpIsLegalVNFunc(static_cast<genTreeOps>(vnf));
}
// Returns "true" iff "vnf" is one of:
// VNF_ADD_OVF, VNF_SUB_OVF, VNF_MUL_OVF,
// VNF_ADD_UN_OVF, VNF_SUB_UN_OVF, VNF_MUL_UN_OVF.
static bool VNFuncIsOverflowArithmetic(VNFunc vnf);
// Returns "true" iff "vnf" is VNF_Cast or VNF_CastOvf.
static bool VNFuncIsNumericCast(VNFunc vnf);
// Returns the arity of "vnf".
static unsigned VNFuncArity(VNFunc vnf);
// Requires "gtOper" to be a genTreeOps legally representing a VNFunc, and returns that
// VNFunc.
// (Requires InitValueNumStoreStatics to have been run.)
static VNFunc GenTreeOpToVNFunc(genTreeOps gtOper)
{
assert(GenTreeOpIsLegalVNFunc(gtOper));
return static_cast<VNFunc>(gtOper);
}
#ifdef DEBUG
static void RunTests(Compiler* comp);
#endif // DEBUG
// This block of methods gets value numbers for constants of primitive types.
ValueNum VNForIntCon(INT32 cnsVal);
ValueNum VNForLongCon(INT64 cnsVal);
ValueNum VNForFloatCon(float cnsVal);
ValueNum VNForDoubleCon(double cnsVal);
ValueNum VNForByrefCon(target_size_t byrefVal);
#ifdef TARGET_64BIT
ValueNum VNForPtrSizeIntCon(INT64 cnsVal)
{
return VNForLongCon(cnsVal);
}
#else
ValueNum VNForPtrSizeIntCon(INT32 cnsVal)
{
return VNForIntCon(cnsVal);
}
#endif
// Packs information about the cast into an integer constant represented by the returned value number,
// to be used as the second operand of VNF_Cast & VNF_CastOvf.
ValueNum VNForCastOper(var_types castToType, bool srcIsUnsigned);
// Unpacks the information stored by VNForCastOper in the constant represented by the value number.
void GetCastOperFromVN(ValueNum vn, var_types* pCastToType, bool* pSrcIsUnsigned);
// We keep handle values in a separate pool, so we don't confuse a handle with an int constant
// that happens to be the same...
ValueNum VNForHandle(ssize_t cnsVal, GenTreeFlags iconFlags);
// And the single constant for an object reference type.
static ValueNum VNForNull()
{
// We reserve Chunk 0 for "special" VNs. SRC_Null (== 0) is the VN of "null".
return ValueNum(SRC_Null);
}
// The ROH map is the map for the "read-only heap". We assume that this is never mutated, and always
// has the same value number.
static ValueNum VNForROH()
{
// We reserve Chunk 0 for "special" VNs. Let SRC_ReadOnlyHeap (== 3) be the read-only heap.
return ValueNum(SRC_ReadOnlyHeap);
}
// A special value number for "void" -- sometimes a type-void thing is an argument,
// and we want the args to be non-NoVN.
static ValueNum VNForVoid()
{
// We reserve Chunk 0 for "special" VNs. Let SRC_Void (== 4) be the value for "void".
return ValueNum(SRC_Void);
}
static ValueNumPair VNPForVoid()
{
return ValueNumPair(VNForVoid(), VNForVoid());
}
// A special value number for the empty set of exceptions.
static ValueNum VNForEmptyExcSet()
{
// We reserve Chunk 0 for "special" VNs. Let SRC_EmptyExcSet (== 5) be the value for the empty set of
// exceptions.
return ValueNum(SRC_EmptyExcSet);
}
static ValueNumPair VNPForEmptyExcSet()
{
return ValueNumPair(VNForEmptyExcSet(), VNForEmptyExcSet());
}
// Returns the value number for zero of the given "typ".
// It has an unreached() for a "typ" that has no zero value, such as TYP_VOID.
ValueNum VNZeroForType(var_types typ);
// Returns the value number for a zero-initialized struct.
ValueNum VNForZeroObj(CORINFO_CLASS_HANDLE structHnd);
// Returns the value number for one of the given "typ".
// It returns NoVN for a "typ" that has no one value, such as TYP_REF.
ValueNum VNOneForType(var_types typ);
#ifdef FEATURE_SIMD
// A helper function for constructing VNF_SimdType VNs.
ValueNum VNForSimdType(unsigned simdSize, var_types simdBaseType);
#endif // FEATURE_SIMD
// Create or return the existimg value number representing a singleton exception set
// for the the exception value "x".
ValueNum VNExcSetSingleton(ValueNum x);
ValueNumPair VNPExcSetSingleton(ValueNumPair x);
// Returns true if the current pair of items are in ascending order and they are not duplicates.
// Used to verify that exception sets are in ascending order when processing them.
bool VNCheckAscending(ValueNum item, ValueNum xs1);
// Returns the VN representing the union of the two exception sets "xs0" and "xs1".
// These must be VNForEmtpyExcSet() or applications of VNF_ExcSetCons, obeying
// the ascending order invariant. (which is preserved in the result)
ValueNum VNExcSetUnion(ValueNum xs0, ValueNum xs1);
ValueNumPair VNPExcSetUnion(ValueNumPair xs0vnp, ValueNumPair xs1vnp);
// Returns the VN representing the intersection of the two exception sets "xs0" and "xs1".
// These must be applications of VNF_ExcSetCons or the empty set. (i.e VNForEmptyExcSet())
// and also must be in ascending order.
ValueNum VNExcSetIntersection(ValueNum xs0, ValueNum xs1);
ValueNumPair VNPExcSetIntersection(ValueNumPair xs0vnp, ValueNumPair xs1vnp);
// Returns true if every exeception singleton in the vnCandidateSet is also present
// in the vnFullSet.
// Both arguments must be either VNForEmptyExcSet() or applications of VNF_ExcSetCons.
bool VNExcIsSubset(ValueNum vnFullSet, ValueNum vnCandidateSet);
// Returns "true" iff "vn" is an application of "VNF_ValWithExc".
bool VNHasExc(ValueNum vn)
{
VNFuncApp funcApp;
return GetVNFunc(vn, &funcApp) && funcApp.m_func == VNF_ValWithExc;
}
// If vn "excSet" is "VNForEmptyExcSet()" we just return "vn"
// otherwise we use VNExcSetUnion to combine the exception sets of both "vn" and "excSet"
// and return that ValueNum
ValueNum VNWithExc(ValueNum vn, ValueNum excSet);
ValueNumPair VNPWithExc(ValueNumPair vnp, ValueNumPair excSetVNP);
// This sets "*pvn" to the Normal value and sets "*pvnx" to Exception set value.
// "pvnx" represents the set of all exceptions that can happen for the expression
void VNUnpackExc(ValueNum vnWx, ValueNum* pvn, ValueNum* pvnx);
void VNPUnpackExc(ValueNumPair vnWx, ValueNumPair* pvn, ValueNumPair* pvnx);
// This returns the Union of exceptions from vnWx and vnExcSet
ValueNum VNUnionExcSet(ValueNum vnWx, ValueNum vnExcSet);
// This returns the Union of exceptions from vnpWx and vnpExcSet
ValueNumPair VNPUnionExcSet(ValueNumPair vnpWx, ValueNumPair vnpExcSet);
// Sets the normal value to a new unique ValueNum
// Keeps any Exception set values
ValueNum VNMakeNormalUnique(ValueNum vn);
// Sets the liberal & conservative
// Keeps any Exception set values
ValueNumPair VNPMakeNormalUniquePair(ValueNumPair vnp);
// If "vn" is a "VNF_ValWithExc(norm, excSet)" value, returns the "norm" argument; otherwise,
// just returns "vn".
// The Normal value is the value number of the expression when no exceptions occurred
ValueNum VNNormalValue(ValueNum vn);
// Given a "vnp", get the ValueNum kind based upon vnk,
// then call VNNormalValue on that ValueNum
// The Normal value is the value number of the expression when no exceptions occurred
ValueNum VNNormalValue(ValueNumPair vnp, ValueNumKind vnk);
// Given a "vnp", get the NormalValuew for the VNK_Liberal part of that ValueNum
// The Normal value is the value number of the expression when no exceptions occurred
inline ValueNum VNLiberalNormalValue(ValueNumPair vnp)
{
return VNNormalValue(vnp, VNK_Liberal);
}
// Given a "vnp", get the NormalValuew for the VNK_Conservative part of that ValueNum
// The Normal value is the value number of the expression when no exceptions occurred
inline ValueNum VNConservativeNormalValue(ValueNumPair vnp)
{
return VNNormalValue(vnp, VNK_Conservative);
}
// Given a "vnp", get the Normal values for both the liberal and conservative parts of "vnp"
// The Normal value is the value number of the expression when no exceptions occurred
ValueNumPair VNPNormalPair(ValueNumPair vnp);
// If "vn" is a "VNF_ValWithExc(norm, excSet)" value, returns the "excSet" argument; otherwise,
// we return a special Value Number representing the empty exception set.
// The exeception set value is the value number of the set of possible exceptions.
ValueNum VNExceptionSet(ValueNum vn);
ValueNumPair VNPExceptionSet(ValueNumPair vn);
// True "iff" vn is a value known to be non-null. (For example, the result of an allocation...)
bool IsKnownNonNull(ValueNum vn);
// True "iff" vn is a value returned by a call to a shared static helper.
bool IsSharedStatic(ValueNum vn);
// VNForFunc: We have five overloads, for arities 0, 1, 2, 3 and 4
ValueNum VNForFunc(var_types typ, VNFunc func);
ValueNum VNForFunc(var_types typ, VNFunc func, ValueNum opVNwx);
// This must not be used for VNF_MapSelect applications; instead use VNForMapSelect, below.
ValueNum VNForFunc(var_types typ, VNFunc func, ValueNum op1VNwx, ValueNum op2VNwx);
ValueNum VNForFunc(var_types typ, VNFunc func, ValueNum op1VNwx, ValueNum op2VNwx, ValueNum op3VNwx);
// The following four-op VNForFunc is used for VNF_PtrToArrElem, elemTypeEqVN, arrVN, inxVN, fldSeqVN
ValueNum VNForFunc(
var_types typ, VNFunc func, ValueNum op1VNwx, ValueNum op2VNwx, ValueNum op3VNwx, ValueNum op4VNwx);
ValueNum VNForMapSelect(ValueNumKind vnk, var_types type, ValueNum map, ValueNum index);
// A method that does the work for VNForMapSelect and may call itself recursively.
ValueNum VNForMapSelectWork(
ValueNumKind vnk, var_types type, ValueNum map, ValueNum index, int* pBudget, bool* pUsedRecursiveVN);
// A specialized version of VNForFunc that is used for VNF_MapStore and provides some logging when verbose is set
ValueNum VNForMapStore(ValueNum map, ValueNum index, ValueNum value);
ValueNum VNForFieldSelector(CORINFO_FIELD_HANDLE fieldHnd, var_types* pFieldType, size_t* pStructSize = nullptr);
// These functions parallel the ones above, except that they take liberal/conservative VN pairs
// as arguments, and return such a pair (the pair of the function applied to the liberal args, and
// the function applied to the conservative args).
ValueNumPair VNPairForFunc(var_types typ, VNFunc func)
{
ValueNumPair res;
res.SetBoth(VNForFunc(typ, func));
return res;
}
ValueNumPair VNPairForFunc(var_types typ, VNFunc func, ValueNumPair opVN)
{
return ValueNumPair(VNForFunc(typ, func, opVN.GetLiberal()), VNForFunc(typ, func, opVN.GetConservative()));
}
ValueNumPair VNPairForFunc(var_types typ, VNFunc func, ValueNumPair op1VN, ValueNumPair op2VN)
{
return ValueNumPair(VNForFunc(typ, func, op1VN.GetLiberal(), op2VN.GetLiberal()),
VNForFunc(typ, func, op1VN.GetConservative(), op2VN.GetConservative()));
}
ValueNumPair VNPairForFunc(var_types typ, VNFunc func, ValueNumPair op1VN, ValueNumPair op2VN, ValueNumPair op3VN)
{
return ValueNumPair(VNForFunc(typ, func, op1VN.GetLiberal(), op2VN.GetLiberal(), op3VN.GetLiberal()),
VNForFunc(typ, func, op1VN.GetConservative(), op2VN.GetConservative(),
op3VN.GetConservative()));
}
ValueNumPair VNPairForFunc(
var_types typ, VNFunc func, ValueNumPair op1VN, ValueNumPair op2VN, ValueNumPair op3VN, ValueNumPair op4VN)
{
return ValueNumPair(VNForFunc(typ, func, op1VN.GetLiberal(), op2VN.GetLiberal(), op3VN.GetLiberal(),
op4VN.GetLiberal()),
VNForFunc(typ, func, op1VN.GetConservative(), op2VN.GetConservative(),
op3VN.GetConservative(), op4VN.GetConservative()));
}
// Get a new, unique value number for an expression that we're not equating to some function,
// which is the value of a tree in the given block.
ValueNum VNForExpr(BasicBlock* block, var_types typ = TYP_UNKNOWN);
// This controls extra tracing of the "evaluation" of "VNF_MapSelect" functions.
#define FEATURE_VN_TRACE_APPLY_SELECTORS 1
ValueNum VNApplySelectors(ValueNumKind vnk,
ValueNum map,
FieldSeqNode* fieldSeq,
size_t* wbFinalStructSize = nullptr);
ValueNum VNApplySelectorsTypeCheck(ValueNum value, var_types indType, size_t valueStructSize);
ValueNum VNApplySelectorsAssign(
ValueNumKind vnk, ValueNum map, FieldSeqNode* fieldSeq, ValueNum value, var_types dstIndType);
ValueNum VNApplySelectorsAssignTypeCoerce(ValueNum value, var_types dstIndType);
ValueNumPair VNPairApplySelectors(ValueNumPair map, FieldSeqNode* fieldSeq, var_types indType);
ValueNumPair VNPairApplySelectorsAssign(ValueNumPair map,
FieldSeqNode* fieldSeq,
ValueNumPair value,
var_types dstIndType)
{
return ValueNumPair(VNApplySelectorsAssign(VNK_Liberal, map.GetLiberal(), fieldSeq, value.GetLiberal(),
dstIndType),
VNApplySelectorsAssign(VNK_Conservative, map.GetConservative(), fieldSeq,
value.GetConservative(), dstIndType));
}
// Compute the ValueNumber for a cast
ValueNum VNForCast(ValueNum srcVN,
var_types castToType,
var_types castFromType,
bool srcIsUnsigned = false,
bool hasOverflowCheck = false);
// Compute the ValueNumberPair for a cast
ValueNumPair VNPairForCast(ValueNumPair srcVNPair,
var_types castToType,
var_types castFromType,
bool srcIsUnsigned = false,
bool hasOverflowCheck = false);
// Returns true iff the VN represents an application of VNF_NotAField.
bool IsVNNotAField(ValueNum vn);
// PtrToLoc values need to express a field sequence as one of their arguments. VN for null represents
// empty sequence, otherwise, "FieldSeq(VN(FieldHandle), restOfSeq)".
ValueNum VNForFieldSeq(FieldSeqNode* fieldSeq);
// Requires that "vn" represents a field sequence, that is, is the result of a call to VNForFieldSeq.
// Returns the FieldSequence it represents.
FieldSeqNode* FieldSeqVNToFieldSeq(ValueNum vn);
// Both argument must represent field sequences; returns the value number representing the
// concatenation "fsVN1 || fsVN2".
ValueNum FieldSeqVNAppend(ValueNum fsVN1, ValueNum fsVN2);
// If "opA" has a PtrToLoc, PtrToArrElem, or PtrToStatic application as its value numbers, and "opB" is an integer
// with a "fieldSeq", returns the VN for the pointer form extended with the field sequence; or else NoVN.
ValueNum ExtendPtrVN(GenTree* opA, GenTree* opB);
// If "opA" has a PtrToLoc, PtrToArrElem, or PtrToStatic application as its value numbers, returns the VN for the
// pointer form extended with "fieldSeq"; or else NoVN.
ValueNum ExtendPtrVN(GenTree* opA, FieldSeqNode* fieldSeq);
// Queries on value numbers.
// All queries taking value numbers require that those value numbers are valid, that is, that
// they have been returned by previous "VNFor..." operations. They can assert false if this is
// not true.
// Returns TYP_UNKNOWN if the given value number has not been given a type.
var_types TypeOfVN(ValueNum vn);
// Returns BasicBlock::MAX_LOOP_NUM if the given value number's loop nest is unknown or ill-defined.
BasicBlock::loopNumber LoopOfVN(ValueNum vn);
// Returns true iff the VN represents a (non-handle) constant.
bool IsVNConstant(ValueNum vn);
// Returns true iff the VN represents an integer constant.
bool IsVNInt32Constant(ValueNum vn);
typedef SmallHashTable<ValueNum, bool, 8U> CheckedBoundVNSet;
// Returns true if the VN is known or likely to appear as the conservative value number
// of the length argument to a GT_ARR_BOUNDS_CHECK node.
bool IsVNCheckedBound(ValueNum vn);
// Record that a VN is known to appear as the conservative value number of the length
// argument to a GT_ARR_BOUNDS_CHECK node.
void SetVNIsCheckedBound(ValueNum vn);
// Information about the individual components of a value number representing an unsigned
// comparison of some value against a checked bound VN.
struct UnsignedCompareCheckedBoundInfo
{
unsigned cmpOper;
ValueNum vnIdx;
ValueNum vnBound;
UnsignedCompareCheckedBoundInfo() : cmpOper(GT_NONE), vnIdx(NoVN), vnBound(NoVN)
{
}
};
struct CompareCheckedBoundArithInfo
{
// (vnBound - 1) > vnOp
// (vnBound arrOper arrOp) cmpOper cmpOp
ValueNum vnBound;
unsigned arrOper;
ValueNum arrOp;
unsigned cmpOper;
ValueNum cmpOp;
CompareCheckedBoundArithInfo() : vnBound(NoVN), arrOper(GT_NONE), arrOp(NoVN), cmpOper(GT_NONE), cmpOp(NoVN)
{
}
#ifdef DEBUG
void dump(ValueNumStore* vnStore)
{
vnStore->vnDump(vnStore->m_pComp, cmpOp);
printf(" ");
printf(vnStore->VNFuncName((VNFunc)cmpOper));
printf(" ");
vnStore->vnDump(vnStore->m_pComp, vnBound);
if (arrOper != GT_NONE)
{
printf(vnStore->VNFuncName((VNFunc)arrOper));
vnStore->vnDump(vnStore->m_pComp, arrOp);
}
}
#endif
};
struct ConstantBoundInfo
{
// 100 > vnOp
int constVal;
unsigned cmpOper;
ValueNum cmpOpVN;
ConstantBoundInfo() : constVal(0), cmpOper(GT_NONE), cmpOpVN(NoVN)
{
}
#ifdef DEBUG
void dump(ValueNumStore* vnStore)
{
vnStore->vnDump(vnStore->m_pComp, cmpOpVN);
printf(" ");
printf(vnStore->VNFuncName((VNFunc)cmpOper));
printf(" ");
printf("%d", constVal);
}
#endif
};
// Check if "vn" is "new [] (type handle, size)"
bool IsVNNewArr(ValueNum vn, VNFuncApp* funcApp);
// Check if "vn" IsVNNewArr and return <= 0 if arr size cannot be determined, else array size.
int GetNewArrSize(ValueNum vn);
// Check if "vn" is "a.len"
bool IsVNArrLen(ValueNum vn);
// If "vn" is VN(a.len) then return VN(a); NoVN if VN(a) can't be determined.
ValueNum GetArrForLenVn(ValueNum vn);
// Return true with any Relop except for == and != and one operand has to be a 32-bit integer constant.
bool IsVNConstantBound(ValueNum vn);
// If "vn" is constant bound, then populate the "info" fields for constVal, cmpOp, cmpOper.
void GetConstantBoundInfo(ValueNum vn, ConstantBoundInfo* info);
// If "vn" is of the form "(uint)var < (uint)len" (or equivalent) return true.
bool IsVNUnsignedCompareCheckedBound(ValueNum vn, UnsignedCompareCheckedBoundInfo* info);
// If "vn" is of the form "var < len" or "len <= var" return true.
bool IsVNCompareCheckedBound(ValueNum vn);
// If "vn" is checked bound, then populate the "info" fields for the boundVn, cmpOp, cmpOper.
void GetCompareCheckedBound(ValueNum vn, CompareCheckedBoundArithInfo* info);
// If "vn" is of the form "len +/- var" return true.
bool IsVNCheckedBoundArith(ValueNum vn);
// If "vn" is checked bound arith, then populate the "info" fields for arrOper, arrVn, arrOp.
void GetCheckedBoundArithInfo(ValueNum vn, CompareCheckedBoundArithInfo* info);
// If "vn" is of the form "var < len +/- k" return true.
bool IsVNCompareCheckedBoundArith(ValueNum vn);
// If "vn" is checked bound arith, then populate the "info" fields for cmpOp, cmpOper.
void GetCompareCheckedBoundArithInfo(ValueNum vn, CompareCheckedBoundArithInfo* info);
// Returns the flags on the current handle. GTF_ICON_SCOPE_HDL for example.
GenTreeFlags GetHandleFlags(ValueNum vn);
// Returns true iff the VN represents a handle constant.
bool IsVNHandle(ValueNum vn);
// Returns true iff the VN represents a relop
bool IsVNRelop(ValueNum vn);
// Given VN(x > y), return VN(y > x), VN(x <= y) or VN(y >= x)
//
// If vn is not a relop, return NoVN.
//
enum class VN_RELATION_KIND
{
VRK_Same, // (x > y)
VRK_Swap, // (y > x)
VRK_Reverse, // (x <= y)
VRK_SwapReverse // (y >= x)
};
#ifdef DEBUG
static const char* VNRelationString(VN_RELATION_KIND vrk);
#endif
ValueNum GetRelatedRelop(ValueNum vn, VN_RELATION_KIND vrk);
// Convert a vartype_t to the value number's storage type for that vartype_t.
// For example, ValueNum of type TYP_LONG are stored in a map of INT64 variables.
// Lang is the language (C++) type for the corresponding vartype_t.
template <int N>
struct VarTypConv
{
};
private:
struct Chunk;
template <typename T>
static T CoerceTypRefToT(Chunk* c, unsigned offset);
// Get the actual value and coerce the actual type c->m_typ to the wanted type T.
template <typename T>
FORCEINLINE T SafeGetConstantValue(Chunk* c, unsigned offset);
template <typename T>
T ConstantValueInternal(ValueNum vn DEBUGARG(bool coerce))
{
Chunk* c = m_chunks.GetNoExpand(GetChunkNum(vn));
assert(c->m_attribs == CEA_Const || c->m_attribs == CEA_Handle);
unsigned offset = ChunkOffset(vn);
switch (c->m_typ)
{
case TYP_REF:
assert(0 <= offset && offset <= 1); // Null or exception.
FALLTHROUGH;
case TYP_BYREF:
#ifdef _MSC_VER
assert(&typeid(T) == &typeid(size_t)); // We represent ref/byref constants as size_t's.
#endif // _MSC_VER
FALLTHROUGH;
case TYP_INT:
case TYP_LONG:
case TYP_FLOAT:
case TYP_DOUBLE:
if (c->m_attribs == CEA_Handle)
{
C_ASSERT(offsetof(VNHandle, m_cnsVal) == 0);
return (T) reinterpret_cast<VNHandle*>(c->m_defs)[offset].m_cnsVal;
}
#ifdef DEBUG
if (!coerce)
{
T val1 = reinterpret_cast<T*>(c->m_defs)[offset];
T val2 = SafeGetConstantValue<T>(c, offset);
// Detect if there is a mismatch between the VN storage type and explicitly
// passed-in type T.
bool mismatch = false;
if (varTypeIsFloating(c->m_typ))
{
mismatch = (memcmp(&val1, &val2, sizeof(val1)) != 0);
}
else
{
mismatch = (val1 != val2);
}
if (mismatch)
{
assert(
!"Called ConstantValue<T>(vn), but type(T) != type(vn); Use CoercedConstantValue instead.");
}
}
#endif
return SafeGetConstantValue<T>(c, offset);
default:
assert(false); // We do not record constants of this typ.
return (T)0;
}
}
public:
// Requires that "vn" is a constant, and that its type is compatible with the explicitly passed
// type "T". Also, note that "T" has to have an accurate storage size of the TypeOfVN(vn).
template <typename T>
T ConstantValue(ValueNum vn)
{
return ConstantValueInternal<T>(vn DEBUGARG(false));
}
// Requires that "vn" is a constant, and that its type can be coerced to the explicitly passed
// type "T".
template <typename T>
T CoercedConstantValue(ValueNum vn)
{
return ConstantValueInternal<T>(vn DEBUGARG(true));
}
// Requires "mthFunc" to be an intrinsic math function (one of the allowable values for the "gtMath" field
// of a GenTreeMath node). For unary ops, return the value number for the application of this function to
// "arg0VN". For binary ops, return the value number for the application of this function to "arg0VN" and
// "arg1VN".
ValueNum EvalMathFuncUnary(var_types typ, NamedIntrinsic mthFunc, ValueNum arg0VN);
ValueNum EvalMathFuncBinary(var_types typ, NamedIntrinsic mthFunc, ValueNum arg0VN, ValueNum arg1VN);
ValueNumPair EvalMathFuncUnary(var_types typ, NamedIntrinsic mthFunc, ValueNumPair arg0VNP)
{
return ValueNumPair(EvalMathFuncUnary(typ, mthFunc, arg0VNP.GetLiberal()),
EvalMathFuncUnary(typ, mthFunc, arg0VNP.GetConservative()));
}
ValueNumPair EvalMathFuncBinary(var_types typ, NamedIntrinsic mthFunc, ValueNumPair arg0VNP, ValueNumPair arg1VNP)
{
return ValueNumPair(EvalMathFuncBinary(typ, mthFunc, arg0VNP.GetLiberal(), arg1VNP.GetLiberal()),
EvalMathFuncBinary(typ, mthFunc, arg0VNP.GetConservative(), arg1VNP.GetConservative()));
}
// Returns "true" iff "vn" represents a function application.
bool IsVNFunc(ValueNum vn);
// If "vn" represents a function application, returns "true" and set "*funcApp" to
// the function application it represents; otherwise, return "false."
bool GetVNFunc(ValueNum vn, VNFuncApp* funcApp);
// Requires that "vn" represents a "GC heap address" the sum of a "TYP_REF" value and some integer
// value. Returns the TYP_REF value.
ValueNum VNForRefInAddr(ValueNum vn);
// Returns "true" iff "vn" is a valid value number -- one that has been previously returned.
bool VNIsValid(ValueNum vn);
#ifdef DEBUG
// This controls whether we recursively call vnDump on function arguments.
#define FEATURE_VN_DUMP_FUNC_ARGS 0
// Prints, to standard out, a representation of "vn".
void vnDump(Compiler* comp, ValueNum vn, bool isPtr = false);
// Requires "fieldSeq" to be a field sequence VNFuncApp.
// Prints a representation (comma-separated list of field names) on standard out.
void vnDumpFieldSeq(Compiler* comp, VNFuncApp* fieldSeq, bool isHead);
// Requires "mapSelect" to be a map select VNFuncApp.