forked from dotnet/coreclr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
liveness.cpp
2928 lines (2373 loc) · 106 KB
/
liveness.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
// =================================================================================
// Code that works with liveness and related concepts (interference, debug scope)
// =================================================================================
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
/*****************************************************************************
*
* Helper for Compiler::fgPerBlockLocalVarLiveness().
* The goal is to compute the USE and DEF sets for a basic block.
* However with the new improvement to the data flow analysis (DFA),
* we do not mark x as used in x = f(x) when there are no side effects in f(x).
* 'asgdLclVar' is set when 'tree' is part of an expression with no side-effects
* which is assigned to asgdLclVar, ie. asgdLclVar = (... tree ...)
*/
inline
void Compiler::fgMarkUseDef(GenTreeLclVarCommon *tree, GenTree *asgdLclVar)
{
bool rhsUSEDEF = false;
unsigned lclNum;
unsigned lhsLclNum;
LclVarDsc* varDsc;
noway_assert(tree->gtOper == GT_LCL_VAR ||
tree->gtOper == GT_LCL_VAR_ADDR ||
tree->gtOper == GT_LCL_FLD ||
tree->gtOper == GT_STORE_LCL_VAR ||
tree->gtOper == GT_STORE_LCL_FLD);
if (tree->gtOper == GT_LCL_VAR || tree->gtOper == GT_LCL_VAR_ADDR || tree->gtOper == GT_STORE_LCL_VAR)
{
lclNum = tree->gtLclNum;
}
else
{
noway_assert(tree->gtOper == GT_LCL_FLD || tree->gtOper == GT_STORE_LCL_FLD);
lclNum = tree->gtLclFld.gtLclNum;
}
noway_assert(lclNum < lvaCount);
varDsc = lvaTable + lclNum;
// We should never encounter a reference to a lclVar that has a zero refCnt.
if(varDsc->lvRefCnt == 0 && (!varTypeIsPromotable(varDsc) || !varDsc->lvPromoted))
{
JITDUMP("Found reference to V%02u with zero refCnt.\n", lclNum);
assert(!"We should never encounter a reference to a lclVar that has a zero refCnt.");
varDsc->lvRefCnt = 1;
}
if (asgdLclVar)
{
/* we have an assignment to a local var : asgdLclVar = ... tree ...
* check for x = f(x) case */
noway_assert(asgdLclVar->gtOper == GT_LCL_VAR || asgdLclVar->gtOper == GT_STORE_LCL_VAR);
noway_assert(asgdLclVar->gtFlags & GTF_VAR_DEF);
lhsLclNum = asgdLclVar->gtLclVarCommon.gtLclNum;
if ((lhsLclNum == lclNum) &&
((tree->gtFlags & GTF_VAR_DEF) == 0) &&
(tree != asgdLclVar) )
{
/* bingo - we have an x = f(x) case */
noway_assert(lvaTable[lhsLclNum].lvType != TYP_STRUCT || lvaTable[lhsLclNum].lvIsSIMDType());
asgdLclVar->gtFlags |= GTF_VAR_USEDEF;
rhsUSEDEF = true;
}
}
/* Is this a tracked variable? */
if (varDsc->lvTracked)
{
noway_assert(varDsc->lvVarIndex < lvaTrackedCount);
if ((tree->gtFlags & GTF_VAR_DEF) != 0 &&
(tree->gtFlags & (GTF_VAR_USEASG | GTF_VAR_USEDEF)) == 0)
{
// if (!(fgCurUseSet & bitMask)) printf("V%02u,T%02u def at %08p\n", lclNum, varDsc->lvVarIndex, tree);
VarSetOps::AddElemD(this, fgCurDefSet, varDsc->lvVarIndex);
}
else
{
// if (!(fgCurDefSet & bitMask))
// {
// printf("V%02u,T%02u use at ", lclNum, varDsc->lvVarIndex);
// printTreeID(tree);
// printf("\n");
// }
/* We have the following scenarios:
* 1. "x += something" - in this case x is flagged GTF_VAR_USEASG
* 2. "x = ... x ..." - the LHS x is flagged GTF_VAR_USEDEF,
* the RHS x is has rhsUSEDEF = true
* (both set by the code above)
*
* We should not mark an USE of x in the above cases provided the value "x" is not used
* further up in the tree. For example "while (i++)" is required to mark i as used.
*/
/* make sure we don't include USEDEF variables in the USE set
* The first test is for LSH, the second (!rhsUSEDEF) is for any var in the RHS */
if ((tree->gtFlags & (GTF_VAR_USEASG | GTF_VAR_USEDEF)) == 0)
{
/* Not a special flag - check to see if used to assign to itself */
if (rhsUSEDEF)
{
/* assign to itself - do not include it in the USE set */
if (!opts.MinOpts() && !opts.compDbgCode)
return;
}
}
/* Fall through for the "good" cases above - add the variable to the USE set */
if (!VarSetOps::IsMember(this, fgCurDefSet, varDsc->lvVarIndex))
{
VarSetOps::AddElemD(this, fgCurUseSet, varDsc->lvVarIndex);
}
// For defs, also add to the (all) def set.
if ((tree->gtFlags & GTF_VAR_DEF) != 0)
{
VarSetOps::AddElemD(this, fgCurDefSet, varDsc->lvVarIndex);
}
}
}
else if (varDsc->lvType == TYP_STRUCT)
{
noway_assert(!varDsc->lvTracked);
lvaPromotionType promotionType = lvaGetPromotionType(varDsc);
if (promotionType != PROMOTION_TYPE_NONE)
{
VARSET_TP VARSET_INIT_NOCOPY(bitMask, VarSetOps::MakeEmpty(this));
for (unsigned i = varDsc->lvFieldLclStart;
i < varDsc->lvFieldLclStart + varDsc->lvFieldCnt;
++i)
{
noway_assert(lvaTable[i].lvIsStructField);
if (lvaTable[i].lvTracked)
{
noway_assert(lvaTable[i].lvVarIndex < lvaTrackedCount);
VarSetOps::AddElemD(this, bitMask, lvaTable[i].lvVarIndex);
}
}
// Mark as used any struct fields that are not yet defined.
if (!VarSetOps::IsSubset(this, bitMask, fgCurDefSet))
{
VarSetOps::UnionD(this, fgCurUseSet, bitMask);
}
}
}
}
/*****************************************************************************/
void Compiler::fgLocalVarLiveness()
{
#ifdef DEBUG
if (verbose)
{
printf("*************** In fgLocalVarLiveness()\n");
#ifndef LEGACY_BACKEND
if (compRationalIRForm)
{
lvaTableDump();
}
#endif // !LEGACY_BACKEND
}
#endif // DEBUG
// Init liveness data structures.
fgLocalVarLivenessInit();
assert(lvaSortAgain == false); // Set to false by lvaSortOnly()
EndPhase(PHASE_LCLVARLIVENESS_INIT);
// Make sure we haven't noted any partial last uses of promoted structs.
GetPromotedStructDeathVars()->RemoveAll();
// Initialize the per-block var sets.
fgInitBlockVarSets();
fgLocalVarLivenessChanged = false;
do
{
/* Figure out use/def info for all basic blocks */
fgPerBlockLocalVarLiveness();
EndPhase(PHASE_LCLVARLIVENESS_PERBLOCK);
/* Live variable analysis. */
fgStmtRemoved = false;
fgInterBlockLocalVarLiveness();
}
while (fgStmtRemoved && fgLocalVarLivenessChanged);
// If we removed any dead code we will have set 'lvaSortAgain' via decRefCnts
if (lvaSortAgain)
{
JITDUMP("In fgLocalVarLiveness, setting lvaSortAgain back to false (set during dead-code removal)\n");
lvaSortAgain = false; // We don't re-Sort because we just performed LclVar liveness.
}
EndPhase(PHASE_LCLVARLIVENESS_INTERBLOCK);
}
/*****************************************************************************/
void Compiler::fgLocalVarLivenessInit()
{
// If necessary, re-sort the variable table by ref-count...before creating any varsets using this sorting.
if (lvaSortAgain)
{
JITDUMP("In fgLocalVarLivenessInit, sorting locals\n");
lvaSortByRefCount();
assert(lvaSortAgain == false); // Set to false by lvaSortOnly()
}
#ifdef LEGACY_BACKEND // RyuJIT backend does not use interference info
for (unsigned i = 0; i < lclMAX_TRACKED; i++)
{
VarSetOps::AssignNoCopy(this, lvaVarIntf[i], VarSetOps::MakeEmpty(this));
}
/* If we're not optimizing at all, things are simple */
if (opts.MinOpts())
{
VARSET_TP VARSET_INIT_NOCOPY(allOnes, VarSetOps::MakeFull(this));
for (unsigned i = 0; i < lvaTrackedCount; i++)
{
VarSetOps::Assign(this, lvaVarIntf[i], allOnes);
}
return;
}
#endif // LEGACY_BACKEND
// We mark a lcl as must-init in a first pass of local variable
// liveness (Liveness1), then assertion prop eliminates the
// uninit-use of a variable Vk, asserting it will be init'ed to
// null. Then, in a second local-var liveness (Liveness2), the
// variable Vk is no longer live on entry to the method, since its
// uses have been replaced via constant propagation.
//
// This leads to a bug: since Vk is no longer live on entry, the
// register allocator sees Vk and an argument Vj as having
// disjoint lifetimes, and allocates them to the same register.
// But Vk is still marked "must-init", and this initialization (of
// the register) trashes the value in Vj.
//
// Therefore, initialize must-init to false for all variables in
// each liveness phase.
for (unsigned lclNum = 0; lclNum < lvaCount; ++lclNum)
{
lvaTable[lclNum].lvMustInit = false;
}
}
/*****************************************************************************/
// <BUGNUM> VSW 322033 <BUGNUM>
//
// This method calculates the USE and DEF values for a statement.
// It also calls fgSetRngChkTarget for the statement.
//
// We refactor out this code from fgPerBlockLocalVarLiveness
// and add QMARK logics to it.
//
// NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
//
// The usage of this method is very limited.
// We should only call it for the first node in the statement or
// for the node after the GTF_RELOP_QMARK node.
//
// NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
/*
Since a GT_QMARK tree can take two paths (i.e. the thenTree Path or the elseTree path),
when we calculate its fgCurDefSet and fgCurUseSet, we need to combine the results
from both trees.
Note that the GT_QMARK trees are threaded as shown below with nodes 1 to 11
linked by gtNext.
The algorithm we use is:
(1) We walk these nodes according the the evaluation order (i.e. from node 1 to node 11).
(2) When we see the GTF_RELOP_QMARK node, we know we are about to split the path.
We cache copies of current fgCurDefSet and fgCurUseSet.
(The fact that it is recursively calling itself is for nested QMARK case,
where we need to remember multiple copies of fgCurDefSet and fgCurUseSet.)
(3) We walk the thenTree.
(4) When we see GT_COLON node, we know that we just finished the thenTree.
We then make a copy of the current fgCurDefSet and fgCurUseSet,
restore them to the ones before the thenTree, and then continue walking
the elseTree.
(5) When we see the GT_QMARK node, we know we just finished the elseTree.
So we combine the results from the thenTree and elseTree and then return.
+--------------------+
| GT_QMARK 11|
+----------+---------+
|
*
/ \
/ \
/ \
+---------------------+ +--------------------+
| GT_<cond> 3 | | GT_COLON 7 |
| w/ GTF_RELOP_QMARK | | w/ GTF_COLON_COND |
+----------+----------+ +---------+----------+
| |
* *
/ \ / \
/ \ / \
/ \ / \
2 1 thenTree 6 elseTree 10
x | |
/ * *
+----------------+ / / \ / \
|prevExpr->gtNext+------/ / \ / \
+----------------+ / \ / \
5 4 9 8
*/
GenTreePtr Compiler::fgPerStatementLocalVarLiveness(GenTreePtr startNode, // The node to start walking with.
GenTreePtr relopNode, // The node before the startNode.
// (It should either be NULL or
// a GTF_RELOP_QMARK node.)
GenTreePtr lhsNode
)
{
GenTreePtr tree;
VARSET_TP VARSET_INIT(this, defSet_BeforeSplit, fgCurDefSet); // Store the current fgCurDefSet and fgCurUseSet so
VARSET_TP VARSET_INIT(this, useSet_BeforeSplit, fgCurUseSet); // we can restore then before entering the elseTree.
bool heapUse_BeforeSplit = fgCurHeapUse;
bool heapDef_BeforeSplit = fgCurHeapDef;
bool heapHavoc_BeforeSplit = fgCurHeapHavoc;
VARSET_TP VARSET_INIT_NOCOPY(defSet_AfterThenTree, VarSetOps::MakeEmpty(this)); // These two variables will store the USE and DEF sets after
VARSET_TP VARSET_INIT_NOCOPY(useSet_AfterThenTree, VarSetOps::MakeEmpty(this)); // evaluating the thenTree.
bool heapUse_AfterThenTree = fgCurHeapUse;
bool heapDef_AfterThenTree = fgCurHeapDef;
bool heapHavoc_AfterThenTree = fgCurHeapHavoc;
// relopNode is either NULL or a GTF_RELOP_QMARK node.
assert(!relopNode ||
(relopNode->OperKind() & GTK_RELOP) && (relopNode->gtFlags & GTF_RELOP_QMARK)
);
// If relopNode is NULL, then the startNode must be the 1st node of the statement.
// If relopNode is non-NULL, then the startNode must be the node right after the GTF_RELOP_QMARK node.
assert( (!relopNode && startNode == compCurStmt->gtStmt.gtStmtList) ||
(relopNode && startNode == relopNode->gtNext)
);
for (tree = startNode; tree; tree = tree->gtNext)
{
switch (tree->gtOper)
{
case GT_QMARK:
// This must be a GT_QMARK node whose GTF_RELOP_QMARK node is recursively calling us.
noway_assert(relopNode && tree->gtOp.gtOp1 == relopNode);
// By the time we see a GT_QMARK, we must have finished processing the elseTree.
// So it's the time to combine the results
// from the the thenTree and the elseTree, and then return.
VarSetOps::IntersectionD(this, fgCurDefSet, defSet_AfterThenTree);
VarSetOps::UnionD(this, fgCurUseSet, useSet_AfterThenTree);
fgCurHeapDef = fgCurHeapDef && heapDef_AfterThenTree;
fgCurHeapHavoc = fgCurHeapHavoc && heapHavoc_AfterThenTree;
fgCurHeapUse = fgCurHeapUse || heapUse_AfterThenTree;
// Return the GT_QMARK node itself so the caller can continue from there.
// NOTE: the caller will get to the next node by doing the "tree = tree->gtNext"
// in the "for" statement.
goto _return;
case GT_COLON:
// By the time we see GT_COLON, we must have just walked the thenTree.
// So we need to do two things here.
// (1) Save the current fgCurDefSet and fgCurUseSet so that later we can combine them
// with the result from the elseTree.
// (2) Restore fgCurDefSet and fgCurUseSet to the points before the thenTree is walked.
// and then continue walking the elseTree.
VarSetOps::Assign(this, defSet_AfterThenTree, fgCurDefSet);
VarSetOps::Assign(this, useSet_AfterThenTree, fgCurUseSet);
heapDef_AfterThenTree = fgCurHeapDef;
heapHavoc_AfterThenTree = fgCurHeapHavoc;
heapUse_AfterThenTree = fgCurHeapUse;
VarSetOps::Assign(this, fgCurDefSet, defSet_BeforeSplit);
VarSetOps::Assign(this, fgCurUseSet, useSet_BeforeSplit);
fgCurHeapDef = heapDef_BeforeSplit;
fgCurHeapHavoc = heapHavoc_BeforeSplit;
fgCurHeapUse = heapUse_BeforeSplit;
break;
case GT_LCL_VAR:
case GT_LCL_FLD:
case GT_STORE_LCL_VAR:
case GT_STORE_LCL_FLD:
fgMarkUseDef(tree->AsLclVarCommon(), lhsNode);
break;
case GT_CLS_VAR:
// If the GT_CLS_VAR is the lhs of an assignment, we'll handle it as a heap def, when we get to assignment.
// Otherwise, we treat it as a use here.
if (!fgCurHeapDef && (tree->gtFlags & GTF_CLS_VAR_ASG_LHS) == 0)
{
fgCurHeapUse = true;
}
break;
case GT_IND:
// If the GT_IND is the lhs of an assignment, we'll handle it as a heap def, when we get to assignment.
// Otherwise, we treat it as a use here.
if ((tree->gtFlags & GTF_IND_ASG_LHS) == 0)
{
GenTreeLclVarCommon* dummyLclVarTree = NULL;
bool dummyIsEntire = false;
GenTreePtr addrArg = tree->gtOp.gtOp1->gtEffectiveVal(/*commaOnly*/true);
if (!addrArg->DefinesLocalAddr(this, /*width doesn't matter*/0, &dummyLclVarTree, &dummyIsEntire))
{
if (!fgCurHeapDef)
{
fgCurHeapUse = true;
}
}
else
{
// Defines a local addr
assert(dummyLclVarTree != nullptr);
fgMarkUseDef(dummyLclVarTree->AsLclVarCommon(), lhsNode);
}
}
break;
// These should have been morphed away to become GT_INDs:
case GT_FIELD:
case GT_INDEX:
unreached();
break;
// We'll assume these are use-then-defs of the heap.
case GT_LOCKADD:
case GT_XADD:
case GT_XCHG:
case GT_CMPXCHG:
if (!fgCurHeapDef)
{
fgCurHeapUse = true;
}
fgCurHeapDef = true;
fgCurHeapHavoc = true;
break;
// For now, all calls read/write the heap, the latter in its entirety. Might tighten this case later.
case GT_CALL:
{
GenTreeCall* call = tree->AsCall();
bool modHeap = true;
if (call->gtCallType == CT_HELPER)
{
CorInfoHelpFunc helpFunc = eeGetHelperNum(call->gtCallMethHnd);
if ( !s_helperCallProperties.MutatesHeap(helpFunc)
&& !s_helperCallProperties.MayRunCctor(helpFunc))
{
modHeap = false;
}
}
if (modHeap)
{
if (!fgCurHeapDef)
{
fgCurHeapUse = true;
}
fgCurHeapDef = true;
fgCurHeapHavoc = true;
}
}
#if INLINE_NDIRECT
// If this is a p/invoke unmanaged call or if this is a tail-call
// and we have an unmanaged p/invoke call in the method,
// then we're going to run the p/invoke epilog.
// So we mark the FrameRoot as used by this instruction.
// This ensures that the block->bbVarUse will contain
// the FrameRoot local var if is it a tracked variable.
if (tree->gtCall.IsUnmanaged() || (tree->gtCall.IsTailCall() && info.compCallUnmanaged))
{
/* Get the TCB local and mark it as used */
noway_assert(info.compLvFrameListRoot < lvaCount);
LclVarDsc* varDsc = &lvaTable[info.compLvFrameListRoot];
if (varDsc->lvTracked)
{
if (!VarSetOps::IsMember(this, fgCurDefSet, varDsc->lvVarIndex))
{
VarSetOps::AddElemD(this, fgCurUseSet, varDsc->lvVarIndex);
}
}
}
#endif // INLINE_NDIRECT
break;
default:
// Determine whether it defines a heap location.
if (tree->OperIsAssignment() || tree->OperIsBlkOp())
{
GenTreeLclVarCommon* dummyLclVarTree = NULL;
if (!tree->DefinesLocal(this, &dummyLclVarTree))
{
// If it doesn't define a local, then it might update the heap.
fgCurHeapDef = true;
}
}
// Are we seeing a GT_<cond> for a GT_QMARK node?
if ( (tree->OperKind() & GTK_RELOP) &&
(tree->gtFlags & GTF_RELOP_QMARK)
) {
// We are about to enter the parallel paths (i.e. the thenTree and the elseTree).
// Recursively call fgPerStatementLocalVarLiveness.
// At the very beginning of fgPerStatementLocalVarLiveness, we will cache the values of the current
// fgCurDefSet and fgCurUseSet into local variables defSet_BeforeSplit and useSet_BeforeSplit.
// The cached values will be used to restore fgCurDefSet and fgCurUseSet once we see the GT_COLON node.
tree = fgPerStatementLocalVarLiveness(tree->gtNext, tree, lhsNode);
// We must have been returned here after seeing a GT_QMARK node.
noway_assert(tree->gtOper == GT_QMARK);
}
break;
}
}
_return:
return tree;
}
/*****************************************************************************/
void Compiler::fgPerBlockLocalVarLiveness()
{
#ifdef DEBUG
if (verbose)
{
printf("*************** In fgPerBlockLocalVarLiveness()\n");
}
#endif // DEBUG
BasicBlock* block;
#if CAN_DISABLE_DFA
/* If we're not optimizing at all, things are simple */
if (opts.MinOpts())
{
unsigned lclNum;
LclVarDsc* varDsc;
VARSET_TP VARSET_INIT_NOCOPY(liveAll, VarSetOps::MakeEmpty(this));
/* We simply make everything live everywhere */
for (lclNum = 0, varDsc = lvaTable;
lclNum < lvaCount;
lclNum++ , varDsc++)
{
if (varDsc->lvTracked)
{
VarSetOps::AddElemD(this, liveAll, varDsc->lvVarIndex);
}
}
for (block = fgFirstBB; block; block = block->bbNext)
{
// Strictly speaking, the assignments for the "Def" cases aren't necessary here.
// The empty set would do as well. Use means "use-before-def", so as long as that's
// "all", this has the right effect.
VarSetOps::Assign(this, block->bbVarUse, liveAll);
VarSetOps::Assign(this, block->bbVarDef, liveAll);
VarSetOps::Assign(this, block->bbLiveIn, liveAll);
VarSetOps::Assign(this, block->bbLiveOut, liveAll);
block->bbHeapUse = true;
block->bbHeapDef = true;
block->bbHeapLiveIn = true;
block->bbHeapLiveOut = true;
switch (block->bbJumpKind)
{
case BBJ_EHFINALLYRET:
case BBJ_THROW:
case BBJ_RETURN:
VarSetOps::AssignNoCopy(this, block->bbLiveOut, VarSetOps::MakeEmpty(this));
break;
default:
break;
}
}
return;
}
#endif // CAN_DISABLE_DFA
// Avoid allocations in the long case.
VarSetOps::AssignNoCopy(this, fgCurUseSet, VarSetOps::MakeEmpty(this));
VarSetOps::AssignNoCopy(this, fgCurDefSet, VarSetOps::MakeEmpty(this));
for (block = fgFirstBB; block; block = block->bbNext)
{
GenTreePtr stmt;
GenTreePtr tree;
GenTreePtr lhsNode;
VarSetOps::ClearD(this, fgCurUseSet);
VarSetOps::ClearD(this, fgCurDefSet);
fgCurHeapUse = false;
fgCurHeapDef = false;
fgCurHeapHavoc = false;
compCurBB = block;
#if JIT_FEATURE_SSA_SKIP_DEFS
for (stmt = block->FirstNonPhiDef(); stmt; stmt = stmt->gtNext)
#else
for (stmt = block->bbTreeList; stmt; stmt = stmt->gtNext)
#endif
{
noway_assert(stmt->gtOper == GT_STMT);
if (!stmt->gtStmt.gtStmtIsTopLevel())
continue;
compCurStmt = stmt;
lhsNode = nullptr;
tree = stmt->gtStmt.gtStmtExpr;
noway_assert(tree);
// The following code checks if we have an assignment expression
// which may become a GTF_VAR_USEDEF - x=f(x).
// consider if LHS is local var - ignore if RHS contains SIDE_EFFECTS
if ((tree->gtOper == GT_ASG && tree->gtOp.gtOp1->gtOper == GT_LCL_VAR) || tree->gtOper == GT_STORE_LCL_VAR)
{
noway_assert(tree->gtOp.gtOp1);
GenTreePtr rhsNode;
if (tree->gtOper == GT_ASG)
{
noway_assert(tree->gtOp.gtOp2);
lhsNode = tree->gtOp.gtOp1;
rhsNode = tree->gtOp.gtOp2;
}
else
{
lhsNode = tree;
rhsNode = tree->gtOp.gtOp1;
}
// If this is an assignment to local var with no SIDE EFFECTS,
// set lhsNode so that genMarkUseDef will flag potential
// x=f(x) expressions as GTF_VAR_USEDEF.
// Reset the flag before recomputing it - it may have been set before,
// but subsequent optimizations could have removed the rhs reference.
lhsNode->gtFlags &= ~GTF_VAR_USEDEF;
if ((rhsNode->gtFlags & GTF_SIDE_EFFECT) == 0)
{
noway_assert(lhsNode->gtFlags & GTF_VAR_DEF);
}
else
{
lhsNode = nullptr;
}
}
tree = fgPerStatementLocalVarLiveness(stmt->gtStmt.gtStmtList, NULL, lhsNode);
// We must have walked to the end of this statement.
noway_assert(!tree);
}
#if INLINE_NDIRECT
/* Get the TCB local and mark it as used */
if (block->bbJumpKind == BBJ_RETURN && info.compCallUnmanaged)
{
noway_assert(info.compLvFrameListRoot < lvaCount);
LclVarDsc * varDsc = &lvaTable[info.compLvFrameListRoot];
if (varDsc->lvTracked)
{
if (!VarSetOps::IsMember(this, fgCurDefSet, varDsc->lvVarIndex))
{
VarSetOps::AddElemD(this, fgCurUseSet, varDsc->lvVarIndex);
}
}
}
#endif // INLINE_NDIRECT
#ifdef DEBUG
if (verbose)
{
VARSET_TP VARSET_INIT_NOCOPY(allVars, VarSetOps::Union(this, fgCurUseSet, fgCurDefSet));
printf("BB%02u", block->bbNum);
printf( " USE(%d)=", VarSetOps::Count(this, fgCurUseSet));
lvaDispVarSet(fgCurUseSet, allVars);
if (fgCurHeapUse)
printf( " + HEAP");
printf("\n DEF(%d)=", VarSetOps::Count(this, fgCurDefSet));
lvaDispVarSet(fgCurDefSet, allVars);
if (fgCurHeapDef)
printf( " + HEAP");
if (fgCurHeapHavoc)
printf("*");
printf("\n\n");
}
#endif // DEBUG
VarSetOps::Assign(this, block->bbVarUse, fgCurUseSet);
VarSetOps::Assign(this, block->bbVarDef, fgCurDefSet);
block->bbHeapUse = fgCurHeapUse;
block->bbHeapDef = fgCurHeapDef;
block->bbHeapHavoc = fgCurHeapHavoc;
/* also initialize the IN set, just in case we will do multiple DFAs */
VarSetOps::AssignNoCopy(this, block->bbLiveIn, VarSetOps::MakeEmpty(this));
block->bbHeapLiveIn = false;
}
}
/*****************************************************************************/
#ifdef DEBUGGING_SUPPORT
/*****************************************************************************/
// Helper functions to mark variables live over their entire scope
void Compiler::fgBeginScopeLife(VARSET_TP* inScope, VarScopeDsc* var)
{
assert(var);
LclVarDsc * lclVarDsc1 = &lvaTable[var->vsdVarNum];
if (lclVarDsc1->lvTracked)
{
VarSetOps::AddElemD(this, *inScope, lclVarDsc1->lvVarIndex);
}
}
void Compiler::fgEndScopeLife(VARSET_TP* inScope, VarScopeDsc* var)
{
assert(var);
LclVarDsc * lclVarDsc1 = &lvaTable[var->vsdVarNum];
if (lclVarDsc1->lvTracked)
{
VarSetOps::RemoveElemD(this, *inScope, lclVarDsc1->lvVarIndex);
}
}
/*****************************************************************************/
void Compiler::fgMarkInScope(BasicBlock * block, VARSET_VALARG_TP inScope)
{
#ifdef DEBUG
if (verbose)
{
printf("Scope info: block BB%02u marking in scope: ", block->bbNum);
dumpConvertedVarSet(this, inScope);
printf("\n");
}
#endif // DEBUG
/* Record which vars are artifically kept alive for debugging */
VarSetOps::Assign(this, block->bbScope, inScope);
/* Being in scope implies a use of the variable. Add the var to bbVarUse
so that redoing fgLiveVarAnalysis() will work correctly */
VarSetOps::UnionD(this, block->bbVarUse, inScope);
/* Artifically mark all vars in scope as alive */
VarSetOps::UnionD(this, block->bbLiveIn, inScope);
VarSetOps::UnionD(this, block->bbLiveOut, inScope);
}
void Compiler::fgUnmarkInScope(BasicBlock * block, VARSET_VALARG_TP unmarkScope)
{
#ifdef DEBUG
if (verbose)
{
printf("Scope info: block BB%02u UNmarking in scope: ", block->bbNum);
dumpConvertedVarSet(this, unmarkScope);
printf("\n");
}
#endif // DEBUG
assert(VarSetOps::IsSubset(this, unmarkScope, block->bbScope));
VarSetOps::DiffD(this, block->bbScope, unmarkScope);
VarSetOps::DiffD(this, block->bbVarUse, unmarkScope);
VarSetOps::DiffD(this, block->bbLiveIn, unmarkScope);
VarSetOps::DiffD(this, block->bbLiveOut, unmarkScope);
}
#ifdef DEBUG
void Compiler::fgDispDebugScopes()
{
printf("\nDebug scopes:\n");
BasicBlock* block;
for (block = fgFirstBB; block; block = block->bbNext)
{
printf("BB%02u: ", block->bbNum);
dumpConvertedVarSet(this, block->bbScope);
printf("\n");
}
}
#endif // DEBUG
/*****************************************************************************
*
* Mark variables live across their entire scope.
*/
#if FEATURE_EH_FUNCLETS
void Compiler::fgExtendDbgScopes()
{
compResetScopeLists();
#ifdef DEBUG
if (verbose)
printf("\nMarking vars alive over their entire scope :\n\n");
if (verbose)
compDispScopeLists();
#endif // DEBUG
VARSET_TP VARSET_INIT_NOCOPY(inScope, VarSetOps::MakeEmpty(this));
// Mark all tracked LocalVars live over their scope - walk the blocks
// keeping track of the current life, and assign it to the blocks.
for (BasicBlock* block = fgFirstBB; block; block = block->bbNext)
{
// If we get to a funclet, reset the scope lists and start again, since the block
// offsets will be out of order compared to the previous block.
if (block->bbFlags & BBF_FUNCLET_BEG)
{
compResetScopeLists();
VarSetOps::ClearD(this, inScope);
}
// Process all scopes up to the current offset
if (block->bbCodeOffs != BAD_IL_OFFSET)
{
compProcessScopesUntil(block->bbCodeOffs, &inScope, &Compiler::fgBeginScopeLife, &Compiler::fgEndScopeLife);
}
// Assign the current set of variables that are in scope to the block variables tracking this.
fgMarkInScope(block, inScope);
}
#ifdef DEBUG
if (verbose)
{
fgDispDebugScopes();
}
#endif // DEBUG
}
#else // !FEATURE_EH_FUNCLETS
void Compiler::fgExtendDbgScopes()
{
compResetScopeLists();
#ifdef DEBUG
if (verbose)
{
printf("\nMarking vars alive over their entire scope :\n\n");
compDispScopeLists();
}
#endif // DEBUG
VARSET_TP VARSET_INIT_NOCOPY(inScope, VarSetOps::MakeEmpty(this));
compProcessScopesUntil(0, &inScope, &Compiler::fgBeginScopeLife, &Compiler::fgEndScopeLife);
IL_OFFSET lastEndOffs = 0;
// Mark all tracked LocalVars live over their scope - walk the blocks
// keeping track of the current life, and assign it to the blocks.
BasicBlock* block;
for (block = fgFirstBB; block; block = block->bbNext)
{
// Find scopes becoming alive. If there is a gap in the instr
// sequence, we need to process any scopes on those missing offsets.
if (block->bbCodeOffs != BAD_IL_OFFSET)
{
if (lastEndOffs != block->bbCodeOffs)
{
noway_assert(lastEndOffs < block->bbCodeOffs);
compProcessScopesUntil(block->bbCodeOffs, &inScope, &Compiler::fgBeginScopeLife, &Compiler::fgEndScopeLife);
}
else
{
while (VarScopeDsc* varScope = compGetNextEnterScope(block->bbCodeOffs))
{
fgBeginScopeLife(&inScope, varScope);
}
}
}
// Assign the current set of variables that are in scope to the block variables tracking this.
fgMarkInScope(block, inScope);
// Find scopes going dead.
if (block->bbCodeOffsEnd != BAD_IL_OFFSET)
{
VarScopeDsc* varScope;
while ((varScope = compGetNextExitScope(block->bbCodeOffsEnd)) != nullptr)
{
fgEndScopeLife(&inScope, varScope);
}
lastEndOffs = block->bbCodeOffsEnd;
}
}
/* Everything should be out of scope by the end of the method. But if the
last BB got removed, then inScope may not be empty. */
noway_assert(VarSetOps::IsEmpty(this, inScope) || lastEndOffs < info.compILCodeSize);
}
#endif // !FEATURE_EH_FUNCLETS
/*****************************************************************************