This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
gentree.cpp
16748 lines (14636 loc) · 528 KB
/
gentree.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.
// See the LICENSE file in the project root for more information.
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XX XX
XX GenTree XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
#include "jitpch.h"
#include "simd.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
/*****************************************************************************/
const unsigned short GenTree::gtOperKindTable[] = {
#define GTNODE(en, sn, cm, ok) ok + GTK_COMMUTE *cm,
#include "gtlist.h"
};
/*****************************************************************************/
// static
genTreeOps GenTree::OpAsgToOper(genTreeOps op)
{
// Precondition.
assert(OperIsAssignment(op) && op != GT_ASG);
switch (op)
{
case GT_ASG_ADD:
return GT_ADD;
case GT_ASG_SUB:
return GT_SUB;
case GT_ASG_MUL:
return GT_MUL;
case GT_ASG_DIV:
return GT_DIV;
case GT_ASG_MOD:
return GT_MOD;
case GT_ASG_UDIV:
return GT_UDIV;
case GT_ASG_UMOD:
return GT_UMOD;
case GT_ASG_OR:
return GT_OR;
case GT_ASG_XOR:
return GT_XOR;
case GT_ASG_AND:
return GT_AND;
case GT_ASG_LSH:
return GT_LSH;
case GT_ASG_RSH:
return GT_RSH;
case GT_ASG_RSZ:
return GT_RSZ;
case GT_CHS:
return GT_NEG;
default:
unreached(); // Precondition implies we don't get here.
}
}
/*****************************************************************************
*
* The types of different GenTree nodes
*/
#ifdef DEBUG
#define INDENT_SIZE 3
//--------------------------------------------
//
// IndentStack: This struct is used, along with its related enums and strings,
// to control both the indendtation and the printing of arcs.
//
// Notes:
// The mode of printing is set in the Constructor, using its 'compiler' argument.
// Currently it only prints arcs when fgOrder == fgOrderLinear.
// The type of arc to print is specified by the IndentInfo enum, and is controlled
// by the caller of the Push() method.
enum IndentChars
{
ICVertical,
ICBottom,
ICTop,
ICMiddle,
ICDash,
ICEmbedded,
ICTerminal,
ICError,
IndentCharCount
};
// clang-format off
// Sets of strings for different dumping options vert bot top mid dash embedded terminal error
static const char* emptyIndents[IndentCharCount] = { " ", " ", " ", " ", " ", "{", "", "?" };
static const char* asciiIndents[IndentCharCount] = { "|", "\\", "/", "+", "-", "{", "*", "?" };
static const char* unicodeIndents[IndentCharCount] = { "\xe2\x94\x82", "\xe2\x94\x94", "\xe2\x94\x8c", "\xe2\x94\x9c", "\xe2\x94\x80", "{", "\xe2\x96\x8c", "?" };
// clang-format on
typedef ArrayStack<Compiler::IndentInfo> IndentInfoStack;
struct IndentStack
{
IndentInfoStack stack;
const char** indents;
// Constructor for IndentStack. Uses 'compiler' to determine the mode of printing.
IndentStack(Compiler* compiler) : stack(compiler)
{
if (compiler->asciiTrees)
{
indents = asciiIndents;
}
else
{
indents = unicodeIndents;
}
}
// Return the depth of the current indentation.
unsigned Depth()
{
return stack.Height();
}
// Push a new indentation onto the stack, of the given type.
void Push(Compiler::IndentInfo info)
{
stack.Push(info);
}
// Pop the most recent indentation type off the stack.
Compiler::IndentInfo Pop()
{
return stack.Pop();
}
// Print the current indentation and arcs.
void print()
{
unsigned indentCount = Depth();
for (unsigned i = 0; i < indentCount; i++)
{
unsigned index = indentCount - 1 - i;
switch (stack.Index(index))
{
case Compiler::IndentInfo::IINone:
printf(" ");
break;
case Compiler::IndentInfo::IIEmbedded:
printf("%s ", indents[ICEmbedded]);
break;
case Compiler::IndentInfo::IIArc:
if (index == 0)
{
printf("%s%s%s", indents[ICMiddle], indents[ICDash], indents[ICDash]);
}
else
{
printf("%s ", indents[ICVertical]);
}
break;
case Compiler::IndentInfo::IIArcBottom:
printf("%s%s%s", indents[ICBottom], indents[ICDash], indents[ICDash]);
break;
case Compiler::IndentInfo::IIArcTop:
printf("%s%s%s", indents[ICTop], indents[ICDash], indents[ICDash]);
break;
case Compiler::IndentInfo::IIError:
printf("%s%s%s", indents[ICError], indents[ICDash], indents[ICDash]);
break;
default:
unreached();
}
}
printf("%s", indents[ICTerminal]);
}
};
//------------------------------------------------------------------------
// printIndent: This is a static method which simply invokes the 'print'
// method on its 'indentStack' argument.
//
// Arguments:
// indentStack - specifies the information for the indentation & arcs to be printed
//
// Notes:
// This method exists to localize the checking for the case where indentStack is null.
static void printIndent(IndentStack* indentStack)
{
if (indentStack == nullptr)
{
return;
}
indentStack->print();
}
static const char* nodeNames[] = {
#define GTNODE(en, sn, cm, ok) sn,
#include "gtlist.h"
};
const char* GenTree::NodeName(genTreeOps op)
{
assert((unsigned)op < sizeof(nodeNames) / sizeof(nodeNames[0]));
return nodeNames[op];
}
static const char* opNames[] = {
#define GTNODE(en, sn, cm, ok) #en,
#include "gtlist.h"
};
const char* GenTree::OpName(genTreeOps op)
{
assert((unsigned)op < sizeof(opNames) / sizeof(opNames[0]));
return opNames[op];
}
#endif
/*****************************************************************************
*
* When 'SMALL_TREE_NODES' is enabled, we allocate tree nodes in 2 different
* sizes: 'GTF_DEBUG_NODE_SMALL' for most nodes and 'GTF_DEBUG_NODE_LARGE' for
* the few nodes (such as calls and statement list nodes) that have more fields
* and take up a lot more space.
*/
#if SMALL_TREE_NODES
/* GT_COUNT'th oper is overloaded as 'undefined oper', so allocate storage for GT_COUNT'th oper also */
/* static */
unsigned char GenTree::s_gtNodeSizes[GT_COUNT + 1];
/* static */
void GenTree::InitNodeSize()
{
/* 'GT_LCL_VAR' often gets changed to 'GT_REG_VAR' */
assert(GenTree::s_gtNodeSizes[GT_LCL_VAR] >= GenTree::s_gtNodeSizes[GT_REG_VAR]);
/* Set all sizes to 'small' first */
for (unsigned op = 0; op <= GT_COUNT; op++)
{
GenTree::s_gtNodeSizes[op] = TREE_NODE_SZ_SMALL;
}
// Now set all of the appropriate entries to 'large'
CLANG_FORMAT_COMMENT_ANCHOR;
#if defined(FEATURE_HFA) || defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
// On ARM32, ARM64 and System V for struct returning
// there is code that does GT_ASG-tree.CopyObj call.
// CopyObj is a large node and the GT_ASG is small, which triggers an exception.
GenTree::s_gtNodeSizes[GT_ASG] = TREE_NODE_SZ_LARGE;
GenTree::s_gtNodeSizes[GT_RETURN] = TREE_NODE_SZ_LARGE;
#endif // defined(FEATURE_HFA) || defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
GenTree::s_gtNodeSizes[GT_CALL] = TREE_NODE_SZ_LARGE;
GenTree::s_gtNodeSizes[GT_CAST] = TREE_NODE_SZ_LARGE;
GenTree::s_gtNodeSizes[GT_FTN_ADDR] = TREE_NODE_SZ_LARGE;
GenTree::s_gtNodeSizes[GT_BOX] = TREE_NODE_SZ_LARGE;
GenTree::s_gtNodeSizes[GT_INDEX] = TREE_NODE_SZ_LARGE;
GenTree::s_gtNodeSizes[GT_ARR_BOUNDS_CHECK] = TREE_NODE_SZ_LARGE;
#ifdef FEATURE_SIMD
GenTree::s_gtNodeSizes[GT_SIMD_CHK] = TREE_NODE_SZ_LARGE;
#endif // FEATURE_SIMD
GenTree::s_gtNodeSizes[GT_ARR_ELEM] = TREE_NODE_SZ_LARGE;
GenTree::s_gtNodeSizes[GT_ARR_INDEX] = TREE_NODE_SZ_LARGE;
GenTree::s_gtNodeSizes[GT_ARR_OFFSET] = TREE_NODE_SZ_LARGE;
GenTree::s_gtNodeSizes[GT_RET_EXPR] = TREE_NODE_SZ_LARGE;
GenTree::s_gtNodeSizes[GT_OBJ] = TREE_NODE_SZ_LARGE;
GenTree::s_gtNodeSizes[GT_FIELD] = TREE_NODE_SZ_LARGE;
GenTree::s_gtNodeSizes[GT_STMT] = TREE_NODE_SZ_LARGE;
GenTree::s_gtNodeSizes[GT_CMPXCHG] = TREE_NODE_SZ_LARGE;
GenTree::s_gtNodeSizes[GT_QMARK] = TREE_NODE_SZ_LARGE;
GenTree::s_gtNodeSizes[GT_LEA] = TREE_NODE_SZ_LARGE;
GenTree::s_gtNodeSizes[GT_STORE_OBJ] = TREE_NODE_SZ_LARGE;
GenTree::s_gtNodeSizes[GT_DYN_BLK] = TREE_NODE_SZ_LARGE;
GenTree::s_gtNodeSizes[GT_STORE_DYN_BLK] = TREE_NODE_SZ_LARGE;
GenTree::s_gtNodeSizes[GT_INTRINSIC] = TREE_NODE_SZ_LARGE;
GenTree::s_gtNodeSizes[GT_ALLOCOBJ] = TREE_NODE_SZ_LARGE;
#if USE_HELPERS_FOR_INT_DIV
GenTree::s_gtNodeSizes[GT_DIV] = TREE_NODE_SZ_LARGE;
GenTree::s_gtNodeSizes[GT_UDIV] = TREE_NODE_SZ_LARGE;
GenTree::s_gtNodeSizes[GT_MOD] = TREE_NODE_SZ_LARGE;
GenTree::s_gtNodeSizes[GT_UMOD] = TREE_NODE_SZ_LARGE;
#endif
#ifdef FEATURE_UNIX_AMD64_STRUCT_PASSING
GenTree::s_gtNodeSizes[GT_PUTARG_STK] = TREE_NODE_SZ_LARGE;
#endif // FEATURE_UNIX_AMD64_STRUCT_PASSING
assert(GenTree::s_gtNodeSizes[GT_RETURN] == GenTree::s_gtNodeSizes[GT_ASG]);
// This list of assertions should come to contain all GenTree subtypes that are declared
// "small".
assert(sizeof(GenTreeLclFld) <= GenTree::s_gtNodeSizes[GT_LCL_FLD]);
assert(sizeof(GenTreeLclVar) <= GenTree::s_gtNodeSizes[GT_LCL_VAR]);
static_assert_no_msg(sizeof(GenTree) <= TREE_NODE_SZ_SMALL);
static_assert_no_msg(sizeof(GenTreeUnOp) <= TREE_NODE_SZ_SMALL);
static_assert_no_msg(sizeof(GenTreeOp) <= TREE_NODE_SZ_SMALL);
static_assert_no_msg(sizeof(GenTreeVal) <= TREE_NODE_SZ_SMALL);
static_assert_no_msg(sizeof(GenTreeIntConCommon) <= TREE_NODE_SZ_SMALL);
static_assert_no_msg(sizeof(GenTreePhysReg) <= TREE_NODE_SZ_SMALL);
#ifndef LEGACY_BACKEND
static_assert_no_msg(sizeof(GenTreeJumpTable) <= TREE_NODE_SZ_SMALL);
#endif // !LEGACY_BACKEND
static_assert_no_msg(sizeof(GenTreeIntCon) <= TREE_NODE_SZ_SMALL);
static_assert_no_msg(sizeof(GenTreeLngCon) <= TREE_NODE_SZ_SMALL);
static_assert_no_msg(sizeof(GenTreeDblCon) <= TREE_NODE_SZ_SMALL);
static_assert_no_msg(sizeof(GenTreeStrCon) <= TREE_NODE_SZ_SMALL);
static_assert_no_msg(sizeof(GenTreeLclVarCommon) <= TREE_NODE_SZ_SMALL);
static_assert_no_msg(sizeof(GenTreeLclVar) <= TREE_NODE_SZ_SMALL);
static_assert_no_msg(sizeof(GenTreeLclFld) <= TREE_NODE_SZ_SMALL);
static_assert_no_msg(sizeof(GenTreeRegVar) <= TREE_NODE_SZ_SMALL);
static_assert_no_msg(sizeof(GenTreeCast) <= TREE_NODE_SZ_LARGE); // *** large node
static_assert_no_msg(sizeof(GenTreeBox) <= TREE_NODE_SZ_LARGE); // *** large node
static_assert_no_msg(sizeof(GenTreeField) <= TREE_NODE_SZ_LARGE); // *** large node
static_assert_no_msg(sizeof(GenTreeArgList) <= TREE_NODE_SZ_SMALL);
static_assert_no_msg(sizeof(GenTreeColon) <= TREE_NODE_SZ_SMALL);
static_assert_no_msg(sizeof(GenTreeCall) <= TREE_NODE_SZ_LARGE); // *** large node
static_assert_no_msg(sizeof(GenTreeCmpXchg) <= TREE_NODE_SZ_LARGE); // *** large node
static_assert_no_msg(sizeof(GenTreeFptrVal) <= TREE_NODE_SZ_LARGE); // *** large node
static_assert_no_msg(sizeof(GenTreeQmark) <= TREE_NODE_SZ_LARGE); // *** large node
static_assert_no_msg(sizeof(GenTreeIntrinsic) <= TREE_NODE_SZ_LARGE); // *** large node
static_assert_no_msg(sizeof(GenTreeIndex) <= TREE_NODE_SZ_LARGE); // *** large node
static_assert_no_msg(sizeof(GenTreeArrLen) <= TREE_NODE_SZ_LARGE); // *** large node
static_assert_no_msg(sizeof(GenTreeBoundsChk) <= TREE_NODE_SZ_LARGE); // *** large node
static_assert_no_msg(sizeof(GenTreeArrElem) <= TREE_NODE_SZ_LARGE); // *** large node
static_assert_no_msg(sizeof(GenTreeArrIndex) <= TREE_NODE_SZ_LARGE); // *** large node
static_assert_no_msg(sizeof(GenTreeArrOffs) <= TREE_NODE_SZ_LARGE); // *** large node
static_assert_no_msg(sizeof(GenTreeIndir) <= TREE_NODE_SZ_SMALL);
static_assert_no_msg(sizeof(GenTreeStoreInd) <= TREE_NODE_SZ_SMALL);
static_assert_no_msg(sizeof(GenTreeAddrMode) <= TREE_NODE_SZ_SMALL);
static_assert_no_msg(sizeof(GenTreeObj) <= TREE_NODE_SZ_LARGE); // *** large node
static_assert_no_msg(sizeof(GenTreeBlk) <= TREE_NODE_SZ_SMALL);
static_assert_no_msg(sizeof(GenTreeRetExpr) <= TREE_NODE_SZ_LARGE); // *** large node
static_assert_no_msg(sizeof(GenTreeStmt) <= TREE_NODE_SZ_LARGE); // *** large node
static_assert_no_msg(sizeof(GenTreeClsVar) <= TREE_NODE_SZ_SMALL);
static_assert_no_msg(sizeof(GenTreeArgPlace) <= TREE_NODE_SZ_SMALL);
static_assert_no_msg(sizeof(GenTreeLabel) <= TREE_NODE_SZ_SMALL);
static_assert_no_msg(sizeof(GenTreePhiArg) <= TREE_NODE_SZ_SMALL);
static_assert_no_msg(sizeof(GenTreeAllocObj) <= TREE_NODE_SZ_LARGE); // *** large node
#ifndef FEATURE_UNIX_AMD64_STRUCT_PASSING
static_assert_no_msg(sizeof(GenTreePutArgStk) <= TREE_NODE_SZ_SMALL);
#else // FEATURE_UNIX_AMD64_STRUCT_PASSING
static_assert_no_msg(sizeof(GenTreePutArgStk) <= TREE_NODE_SZ_LARGE);
#endif // FEATURE_UNIX_AMD64_STRUCT_PASSING
#ifdef FEATURE_SIMD
static_assert_no_msg(sizeof(GenTreeSIMD) <= TREE_NODE_SZ_SMALL);
#endif // FEATURE_SIMD
}
size_t GenTree::GetNodeSize() const
{
return GenTree::s_gtNodeSizes[gtOper];
}
#ifdef DEBUG
bool GenTree::IsNodeProperlySized() const
{
size_t size;
if (gtDebugFlags & GTF_DEBUG_NODE_SMALL)
{
size = TREE_NODE_SZ_SMALL;
}
else
{
assert(gtDebugFlags & GTF_DEBUG_NODE_LARGE);
size = TREE_NODE_SZ_LARGE;
}
return GenTree::s_gtNodeSizes[gtOper] <= size;
}
#endif
#else // SMALL_TREE_NODES
#ifdef DEBUG
bool GenTree::IsNodeProperlySized() const
{
return true;
}
#endif
#endif // SMALL_TREE_NODES
/*****************************************************************************/
// make sure these get instantiated, because it's not in a header file
// (emulating the c++ 'export' keyword here)
// VC appears to be somewhat unpredictable about whether they end up in the .obj file without this
template Compiler::fgWalkResult Compiler::fgWalkTreePostRec<true>(GenTreePtr* pTree, fgWalkData* fgWalkData);
template Compiler::fgWalkResult Compiler::fgWalkTreePostRec<false>(GenTreePtr* pTree, fgWalkData* fgWalkData);
template Compiler::fgWalkResult Compiler::fgWalkTreePreRec<true>(GenTreePtr* pTree, fgWalkData* fgWalkData);
template Compiler::fgWalkResult Compiler::fgWalkTreePreRec<false>(GenTreePtr* pTree, fgWalkData* fgWalkData);
template Compiler::fgWalkResult Compiler::fgWalkTreeRec<true, true>(GenTreePtr* pTree, fgWalkData* fgWalkData);
template Compiler::fgWalkResult Compiler::fgWalkTreeRec<false, false>(GenTreePtr* pTree, fgWalkData* fgWalkData);
template Compiler::fgWalkResult Compiler::fgWalkTreeRec<true, false>(GenTreePtr* pTree, fgWalkData* fgWalkData);
template Compiler::fgWalkResult Compiler::fgWalkTreeRec<false, true>(GenTreePtr* pTree, fgWalkData* fgWalkData);
//******************************************************************************
// fgWalkTreePreRec - Helper function for fgWalkTreePre.
// walk tree in pre order, executing callback on every node.
// Template parameter 'computeStack' specifies whether to maintain
// a stack of ancestor nodes which can be viewed in the callback.
//
template <bool computeStack>
// static
Compiler::fgWalkResult Compiler::fgWalkTreePreRec(GenTreePtr* pTree, fgWalkData* fgWalkData)
{
fgWalkResult result = WALK_CONTINUE;
GenTreePtr currentParent = fgWalkData->parent;
genTreeOps oper;
unsigned kind;
do
{
GenTreePtr tree = *pTree;
assert(tree);
assert(tree->gtOper != GT_STMT);
GenTreeArgList* args; // For call node arg lists.
if (computeStack)
{
fgWalkData->parentStack->Push(tree);
}
/* Visit this node */
// if we are not in the mode where we only do the callback for local var nodes,
// visit the node unconditionally. Otherwise we will visit it under leaf handling.
if (!fgWalkData->wtprLclsOnly)
{
assert(tree == *pTree);
result = fgWalkData->wtprVisitorFn(pTree, fgWalkData);
if (result != WALK_CONTINUE)
{
break;
}
}
/* Figure out what kind of a node we have */
oper = tree->OperGet();
kind = tree->OperKind();
/* Is this a constant or leaf node? */
if (kind & (GTK_CONST | GTK_LEAF))
{
if (fgWalkData->wtprLclsOnly && (oper == GT_LCL_VAR || oper == GT_LCL_FLD))
{
result = fgWalkData->wtprVisitorFn(pTree, fgWalkData);
}
break;
}
else if (fgWalkData->wtprLclsOnly && GenTree::OperIsLocalStore(oper))
{
result = fgWalkData->wtprVisitorFn(pTree, fgWalkData);
if (result != WALK_CONTINUE)
{
break;
}
}
fgWalkData->parent = tree;
/* Is it a 'simple' unary/binary operator? */
if (kind & GTK_SMPOP)
{
if (tree->gtGetOp2())
{
if (tree->gtOp.gtOp1 != nullptr)
{
result = fgWalkTreePreRec<computeStack>(&tree->gtOp.gtOp1, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
}
else
{
assert(tree->NullOp1Legal());
}
pTree = &tree->gtOp.gtOp2;
continue;
}
else
{
pTree = &tree->gtOp.gtOp1;
if (*pTree)
{
continue;
}
break;
}
}
/* See what kind of a special operator we have here */
switch (oper)
{
case GT_FIELD:
pTree = &tree->gtField.gtFldObj;
break;
case GT_CALL:
assert(tree->gtFlags & GTF_CALL);
/* Is this a call to unmanaged code ? */
if (fgWalkData->wtprLclsOnly && (tree->gtFlags & GTF_CALL_UNMANAGED))
{
result = fgWalkData->wtprVisitorFn(pTree, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
}
if (tree->gtCall.gtCallObjp)
{
result = fgWalkTreePreRec<computeStack>(&tree->gtCall.gtCallObjp, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
}
for (args = tree->gtCall.gtCallArgs; args; args = args->Rest())
{
result = fgWalkTreePreRec<computeStack>(args->pCurrent(), fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
}
for (args = tree->gtCall.gtCallLateArgs; args; args = args->Rest())
{
result = fgWalkTreePreRec<computeStack>(args->pCurrent(), fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
}
if (tree->gtCall.gtControlExpr)
{
result = fgWalkTreePreRec<computeStack>(&tree->gtCall.gtControlExpr, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
}
if (tree->gtCall.gtCallType == CT_INDIRECT)
{
if (tree->gtCall.gtCallCookie)
{
result = fgWalkTreePreRec<computeStack>(&tree->gtCall.gtCallCookie, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
}
pTree = &tree->gtCall.gtCallAddr;
}
else
{
pTree = nullptr;
}
break;
case GT_ARR_ELEM:
result = fgWalkTreePreRec<computeStack>(&tree->gtArrElem.gtArrObj, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
unsigned dim;
for (dim = 0; dim < tree->gtArrElem.gtArrRank; dim++)
{
result = fgWalkTreePreRec<computeStack>(&tree->gtArrElem.gtArrInds[dim], fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
}
pTree = nullptr;
break;
case GT_ARR_OFFSET:
result = fgWalkTreePreRec<computeStack>(&tree->gtArrOffs.gtOffset, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
result = fgWalkTreePreRec<computeStack>(&tree->gtArrOffs.gtIndex, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
result = fgWalkTreePreRec<computeStack>(&tree->gtArrOffs.gtArrObj, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
pTree = nullptr;
break;
case GT_CMPXCHG:
result = fgWalkTreePreRec<computeStack>(&tree->gtCmpXchg.gtOpLocation, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
result = fgWalkTreePreRec<computeStack>(&tree->gtCmpXchg.gtOpValue, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
result = fgWalkTreePreRec<computeStack>(&tree->gtCmpXchg.gtOpComparand, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
pTree = nullptr;
break;
case GT_ARR_BOUNDS_CHECK:
#ifdef FEATURE_SIMD
case GT_SIMD_CHK:
#endif // FEATURE_SIMD
result = fgWalkTreePreRec<computeStack>(&tree->gtBoundsChk.gtArrLen, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
result = fgWalkTreePreRec<computeStack>(&tree->gtBoundsChk.gtIndex, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
pTree = nullptr;
break;
case GT_STORE_DYN_BLK:
result = fgWalkTreePreRec<computeStack>(&tree->gtDynBlk.Data(), fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
__fallthrough;
case GT_DYN_BLK:
result = fgWalkTreePreRec<computeStack>(&tree->gtDynBlk.Addr(), fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
result = fgWalkTreePreRec<computeStack>(&tree->gtDynBlk.gtDynamicSize, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
pTree = nullptr;
break;
default:
#ifdef DEBUG
fgWalkData->compiler->gtDispTree(tree);
#endif
assert(!"unexpected operator");
}
} while (pTree != nullptr && *pTree != nullptr);
if (computeStack)
{
fgWalkData->parentStack->Pop();
}
if (result != WALK_ABORT)
{
//
// Restore fgWalkData->parent
//
fgWalkData->parent = currentParent;
}
return result;
}
/*****************************************************************************
*
* Walk all basic blocks and call the given function pointer for all tree
* nodes contained therein.
*/
void Compiler::fgWalkAllTreesPre(fgWalkPreFn* visitor, void* pCallBackData)
{
BasicBlock* block;
for (block = fgFirstBB; block; block = block->bbNext)
{
GenTreePtr tree;
for (tree = block->bbTreeList; tree; tree = tree->gtNext)
{
assert(tree->gtOper == GT_STMT);
fgWalkTreePre(&tree->gtStmt.gtStmtExpr, visitor, pCallBackData);
}
}
}
//******************************************************************************
// fgWalkTreePostRec - Helper function for fgWalkTreePost.
// Walk tree in post order, executing callback on every node
// template parameter 'computeStack' specifies whether to maintain
// a stack of ancestor nodes which can be viewed in the callback.
//
template <bool computeStack>
// static
Compiler::fgWalkResult Compiler::fgWalkTreePostRec(GenTreePtr* pTree, fgWalkData* fgWalkData)
{
fgWalkResult result;
GenTreePtr currentParent = fgWalkData->parent;
genTreeOps oper;
unsigned kind;
GenTree* tree = *pTree;
assert(tree);
assert(tree->gtOper != GT_STMT);
GenTreeArgList* args;
/* Figure out what kind of a node we have */
oper = tree->OperGet();
kind = tree->OperKind();
if (computeStack)
{
fgWalkData->parentStack->Push(tree);
}
/* Is this a constant or leaf node? */
if (kind & (GTK_CONST | GTK_LEAF))
{
goto DONE;
}
/* Is it a 'simple' unary/binary operator? */
fgWalkData->parent = tree;
/* See what kind of a special operator we have here */
switch (oper)
{
case GT_FIELD:
if (tree->gtField.gtFldObj)
{
result = fgWalkTreePostRec<computeStack>(&tree->gtField.gtFldObj, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
}
break;
case GT_CALL:
assert(tree->gtFlags & GTF_CALL);
if (tree->gtCall.gtCallObjp)
{
result = fgWalkTreePostRec<computeStack>(&tree->gtCall.gtCallObjp, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
}
for (args = tree->gtCall.gtCallArgs; args; args = args->Rest())
{
result = fgWalkTreePostRec<computeStack>(args->pCurrent(), fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
}
for (args = tree->gtCall.gtCallLateArgs; args; args = args->Rest())
{
result = fgWalkTreePostRec<computeStack>(args->pCurrent(), fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
}
if (tree->gtCall.gtCallType == CT_INDIRECT)
{
if (tree->gtCall.gtCallCookie)
{
result = fgWalkTreePostRec<computeStack>(&tree->gtCall.gtCallCookie, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
}
result = fgWalkTreePostRec<computeStack>(&tree->gtCall.gtCallAddr, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
}
if (tree->gtCall.gtControlExpr != nullptr)
{
result = fgWalkTreePostRec<computeStack>(&tree->gtCall.gtControlExpr, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
}
break;
case GT_ARR_ELEM:
result = fgWalkTreePostRec<computeStack>(&tree->gtArrElem.gtArrObj, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
unsigned dim;
for (dim = 0; dim < tree->gtArrElem.gtArrRank; dim++)
{
result = fgWalkTreePostRec<computeStack>(&tree->gtArrElem.gtArrInds[dim], fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
}
break;
case GT_ARR_OFFSET:
result = fgWalkTreePostRec<computeStack>(&tree->gtArrOffs.gtOffset, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
result = fgWalkTreePostRec<computeStack>(&tree->gtArrOffs.gtIndex, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
result = fgWalkTreePostRec<computeStack>(&tree->gtArrOffs.gtArrObj, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
break;
case GT_CMPXCHG:
result = fgWalkTreePostRec<computeStack>(&tree->gtCmpXchg.gtOpComparand, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
result = fgWalkTreePostRec<computeStack>(&tree->gtCmpXchg.gtOpValue, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
result = fgWalkTreePostRec<computeStack>(&tree->gtCmpXchg.gtOpLocation, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
break;
case GT_ARR_BOUNDS_CHECK:
#ifdef FEATURE_SIMD
case GT_SIMD_CHK:
#endif // FEATURE_SIMD
result = fgWalkTreePostRec<computeStack>(&tree->gtBoundsChk.gtArrLen, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
result = fgWalkTreePostRec<computeStack>(&tree->gtBoundsChk.gtIndex, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
break;
case GT_STORE_DYN_BLK:
result = fgWalkTreePostRec<computeStack>(&tree->gtDynBlk.Data(), fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
__fallthrough;
case GT_DYN_BLK:
result = fgWalkTreePostRec<computeStack>(&tree->gtDynBlk.Addr(), fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
result = fgWalkTreePostRec<computeStack>(&tree->gtDynBlk.gtDynamicSize, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
break;
case GT_PHI:
{
GenTreeUnOp* phi = tree->AsUnOp();
if (phi->gtOp1 != nullptr)
{
for (GenTreeArgList* args = phi->gtOp1->AsArgList(); args != nullptr; args = args->Rest())
{
result = fgWalkTreePostRec<computeStack>(&args->gtOp1, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
}
}
}
break;
case GT_LIST:
{
GenTreeArgList* list = tree->AsArgList();
if (list->IsAggregate())
{
for (; list != nullptr; list = list->Rest())
{
result = fgWalkTreePostRec<computeStack>(&list->gtOp1, fgWalkData);
if (result == WALK_ABORT)
{
return result;
}
}
break;
}
// GT_LIST nodes that do not represent aggregate arguments intentionally fall through to the
// default node processing below.
__fallthrough;
}
default:
if (kind & GTK_SMPOP)
{
GenTree** op1Slot = &tree->gtOp.gtOp1;
GenTree** op2Slot;
if (tree->OperIsBinary())
{
if ((tree->gtFlags & GTF_REVERSE_OPS) == 0)
{
op2Slot = &tree->gtOp.gtOp2;
}