forked from dotnet/coreclr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assertionprop.cpp
4418 lines (3852 loc) · 152 KB
/
assertionprop.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XX XX
XX AssertionProp XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
/*****************************************************************************
*
* Helper passed to Compiler::fgWalkTreePre() to find the Asgn node for optAddCopies()
*/
/* static */
Compiler::fgWalkResult Compiler::optAddCopiesCallback(GenTreePtr * pTree, fgWalkData *data)
{
GenTreePtr tree = *pTree;
if (tree->OperKind() & GTK_ASGOP)
{
GenTreePtr op1 = tree->gtOp.gtOp1;
Compiler * comp = data->compiler;
if ((op1->gtOper == GT_LCL_VAR) &&
(op1->gtLclVarCommon.gtLclNum == comp->optAddCopyLclNum))
{
comp->optAddCopyAsgnNode = tree;
return WALK_ABORT;
}
}
return WALK_CONTINUE;
}
/*****************************************************************************
*
* Add new copies before Assertion Prop.
*/
void Compiler::optAddCopies()
{
unsigned lclNum;
LclVarDsc * varDsc;
#ifdef DEBUG
if (verbose)
printf("\n*************** In optAddCopies()\n\n");
if (verboseTrees)
{
printf("Blocks/Trees at start of phase\n");
fgDispBasicBlocks(true);
}
#endif
// Don't add any copies if we have reached the tracking limit.
if (lvaHaveManyLocals())
return;
for (lclNum = 0, varDsc = lvaTable;
lclNum < lvaCount;
lclNum++ , varDsc++)
{
var_types typ = varDsc->TypeGet();
// We only add copies for non temp local variables
// that have a single def and that can possibly be enregistered
if (varDsc->lvIsTemp || !varDsc->lvSingleDef || !varTypeCanReg(typ))
continue;
/* For lvNormalizeOnLoad(), we need to add a cast to the copy-assignment
like "copyLclNum = int(varDsc)" and optAssertionGen() only
tracks simple assignments. The same goes for lvNormalizedOnStore as
the cast is generated in fgMorphSmpOpAsg. This boils down to not having
a copy until optAssertionGen handles this*/
if (varDsc->lvNormalizeOnLoad() || varDsc->lvNormalizeOnStore() || typ == TYP_BOOL)
continue;
if (varTypeIsSmall(varDsc->TypeGet()) || typ == TYP_BOOL)
continue;
// If locals must be initialized to zero, that initialization counts as a second definition.
// VB in particular allows usage of variables not explicitly initialized.
// Note that this effectively disables this optimization for all local variables
// as C# sets InitLocals all the time starting in Whidbey.
if (!varDsc->lvIsParam && info.compInitMem)
continue;
#ifdef DEBUG
if (lvaTable[lclNum].lvDblWasInt)
continue;
#endif
// On x86 we may want to add a copy for an incoming double parameter
// because we can ensure that the copy we make is double aligned
// where as we can never ensure the alignment of an incoming double parameter
//
// On all other platforms we will never need to make a copy
// for an incoming double parameter
bool isFloatParam = false;
#ifdef _TARGET_X86_
isFloatParam = varDsc->lvIsParam && varTypeIsFloating(typ);
#endif
if (!isFloatParam && !varDsc->lvVolatileHint)
continue;
// We don't want to add a copy for a variable that is part of a struct
if (varDsc->lvIsStructField)
continue;
// We require that the weighted ref count be significant.
if (varDsc->lvRefCntWtd <= (BB_LOOP_WEIGHT*BB_UNITY_WEIGHT/2))
continue;
// For parameters, we only want to add a copy for the heavier-than-average
// uses instead of adding a copy to cover every single use.
// 'paramImportantUseDom' is the set of blocks that dominate the
// heavier-than-average uses of a parameter.
// Initial value is all blocks.
BlockSet BLOCKSET_INIT_NOCOPY(paramImportantUseDom, BlockSetOps::MakeFull(this));
// This will be threshold for determining heavier-than-average uses
unsigned paramAvgWtdRefDiv2 = (varDsc->lvRefCntWtd + varDsc->lvRefCnt/2) / (varDsc->lvRefCnt*2);
bool paramFoundImportantUse = false;
#ifdef DEBUG
if (verbose)
{
printf("Trying to add a copy for V%02u %s, avg_wtd = %s\n",
lclNum,
varDsc->lvIsParam ? "an arg"
: "a local",
refCntWtd2str(paramAvgWtdRefDiv2));
}
#endif
//
// We must have a ref in a block that is dominated only by the entry block
//
if (BlockSetOps::MayBeUninit(varDsc->lvRefBlks))
{
// No references
continue;
}
bool isDominatedByFirstBB = false;
BLOCKSET_ITER_INIT(this, iter, varDsc->lvRefBlks, blkNum);
while (iter.NextElem(this, &blkNum))
{
/* Find the block 'blkNum' */
BasicBlock * block = fgFirstBB;
while (block && (block->bbNum != blkNum))
{
block = block->bbNext;
}
noway_assert(block && (block->bbNum == blkNum));
bool importantUseInBlock = (varDsc->lvIsParam) && (block->getBBWeight(this) > paramAvgWtdRefDiv2);
bool isPreHeaderBlock = ((block->bbFlags & BBF_LOOP_PREHEADER) != 0);
BlockSet BLOCKSET_INIT_NOCOPY(blockDom, BlockSetOps::UninitVal());
BlockSet BLOCKSET_INIT_NOCOPY(blockDomSub0, BlockSetOps::UninitVal());
if (block->bbIDom == nullptr && isPreHeaderBlock)
{
// Loop Preheader blocks that we insert will have a bbDom set that is nullptr
// but we can instead use the bNext successor block's dominator information
noway_assert(block->bbNext != nullptr);
BlockSetOps::AssignNoCopy(this, blockDom, fgGetDominatorSet(block->bbNext));
}
else
{
BlockSetOps::AssignNoCopy(this, blockDom, fgGetDominatorSet(block));
}
if (!BlockSetOps::IsEmpty(this, blockDom))
{
BlockSetOps::Assign(this, blockDomSub0, blockDom);
if (isPreHeaderBlock)
{
// We must clear bbNext block number from the dominator set
BlockSetOps::RemoveElemD(this, blockDomSub0, block->bbNext->bbNum);
}
/* Is this block dominated by fgFirstBB? */
if (BlockSetOps::IsMember(this, blockDomSub0, fgFirstBB->bbNum))
{
isDominatedByFirstBB = true;
}
}
#ifdef DEBUG
if (verbose)
{
printf(" Referenced in BB%02u, bbWeight is %s",
blkNum,
refCntWtd2str(block->getBBWeight(this)));
if (isDominatedByFirstBB)
printf(", which is dominated by BB01");
if (importantUseInBlock)
printf(", ImportantUse");
printf("\n");
}
#endif
/* If this is a heavier-than-average block, then track which
blocks dominate this use of the parameter. */
if (importantUseInBlock)
{
paramFoundImportantUse = true;
BlockSetOps::IntersectionD(this, paramImportantUseDom, blockDomSub0); // Clear blocks that do not dominate
}
}
// We should have found at least one heavier-than-averageDiv2 block.
if (varDsc->lvIsParam)
{
if (!paramFoundImportantUse)
continue;
}
// For us to add a new copy:
// we require that we have a floating point parameter
// or a lvVolatile variable that is always reached from the first BB
// and we have at least one block available in paramImportantUseDom
//
bool doCopy = (isFloatParam || (isDominatedByFirstBB && varDsc->lvVolatileHint))
&& !BlockSetOps::IsEmpty(this, paramImportantUseDom);
// Under stress mode we expand the number of candidates
// to include parameters of any type
// or any variable that is always reached from the first BB
//
if (compStressCompile(STRESS_GENERIC_VARN, 30))
{
// Ensure that we preserve the invariants required by the subsequent code.
if (varDsc->lvIsParam || isDominatedByFirstBB)
doCopy = true;
}
if (!doCopy)
continue;
GenTreePtr stmt;
unsigned copyLclNum = lvaGrabTemp(false DEBUGARG("optAddCopies"));
// Because lvaGrabTemp may have reallocated the lvaTable, ensure varDsc
// is still in sync with lvaTable[lclNum];
varDsc = &lvaTable[lclNum];
// Set lvType on the new Temp Lcl Var
lvaTable[copyLclNum].lvType = typ;
#ifdef DEBUG
if (verbose)
{
printf("\n Finding the best place to insert the assignment V%02i=V%02i\n",
copyLclNum, lclNum);
}
#endif
if (varDsc->lvIsParam)
{
noway_assert(varDsc->lvDefStmt == nullptr || varDsc->lvIsStructField);
// Create a new copy assignment tree
GenTreePtr copyAsgn = gtNewTempAssign(copyLclNum, gtNewLclvNode(lclNum, typ));
/* Find the best block to insert the new assignment */
/* We will choose the lowest weighted block, and within */
/* those block, the highest numbered block which */
/* dominates all the uses of the local variable */
/* Our default is to use the first block */
BasicBlock * bestBlock = fgFirstBB;
unsigned bestWeight = bestBlock->getBBWeight(this);
BasicBlock * block = bestBlock;
#ifdef DEBUG
if (verbose)
{
printf(" Starting at BB%02u, bbWeight is %s",
block->bbNum,
refCntWtd2str(block->getBBWeight(this)));
printf(", bestWeight is %s\n",
refCntWtd2str(bestWeight));
}
#endif
/* We have already calculated paramImportantUseDom above. */
BLOCKSET_ITER_INIT(this, iter, paramImportantUseDom, blkNum);
while (iter.NextElem(this, &blkNum))
{
/* Advance block to point to 'blkNum' */
/* This assumes that the iterator returns block number is increasing lexical order. */
while (block && (block->bbNum != blkNum))
{
block = block->bbNext;
}
noway_assert(block && (block->bbNum == blkNum));
#ifdef DEBUG
if (verbose)
{
printf(" Considering BB%02u, bbWeight is %s",
block->bbNum,
refCntWtd2str(block->getBBWeight(this)));
printf(", bestWeight is %s\n",
refCntWtd2str(bestWeight));
}
#endif
// Does this block have a smaller bbWeight value?
if (block->getBBWeight(this) > bestWeight)
{
#ifdef DEBUG
if (verbose)
printf("bbWeight too high\n");
#endif
continue;
}
// Don't use blocks that are exception handlers because
// inserting a new first statement will interface with
// the CATCHARG
if (handlerGetsXcptnObj(block->bbCatchTyp))
{
#ifdef DEBUG
if (verbose)
printf("Catch block\n");
#endif
continue;
}
// Don't use the BBJ_ALWAYS block marked with BBF_KEEP_BBJ_ALWAYS. These
// are used by EH code. The JIT can not generate code for such a block.
if (block->bbFlags & BBF_KEEP_BBJ_ALWAYS)
{
#if FEATURE_EH_FUNCLETS
// With funclets, this is only used for BBJ_CALLFINALLY/BBJ_ALWAYS pairs. For x86, it is also used as the "final step" block for leaving finallys.
assert((block->bbPrev != nullptr) && block->bbPrev->isBBCallAlwaysPair());
#endif // FEATURE_EH_FUNCLETS
#ifdef DEBUG
if (verbose)
printf("Internal EH BBJ_ALWAYS block\n");
#endif
continue;
}
// This block will be the new candidate for the insert point
// for the new assignment
//
#ifdef DEBUG
if (verbose)
printf("new bestBlock\n");
#endif
bestBlock = block;
bestWeight = block->getBBWeight(this);
}
/* If there is a use of the variable in this block */
/* then we insert the assignment at the beginning */
/* otherwise we insert the statement at the end */
#ifdef DEBUG
if (verbose)
{
printf(" Insert copy at the %s of BB%02u\n",
(BlockSetOps::IsEmpty(this, paramImportantUseDom) || BlockSetOps::IsMember(this, varDsc->lvRefBlks, bestBlock->bbNum)) ?
"start" : "end",
bestBlock->bbNum);
}
#endif
if (BlockSetOps::IsEmpty(this, paramImportantUseDom) || BlockSetOps::IsMember(this, varDsc->lvRefBlks, bestBlock->bbNum))
stmt = fgInsertStmtAtBeg(bestBlock, copyAsgn);
else
stmt = fgInsertStmtNearEnd(bestBlock, copyAsgn);
/* Increment its lvRefCnt and lvRefCntWtd */
lvaTable[lclNum].incRefCnts(fgFirstBB->getBBWeight(this), this);
/* Increment its lvRefCnt and lvRefCntWtd */
lvaTable[copyLclNum].incRefCnts(fgFirstBB->getBBWeight(this), this);
}
else
{
noway_assert(varDsc->lvDefStmt != nullptr);
/* Locate the assignment to varDsc in the lvDefStmt */
stmt = varDsc->lvDefStmt;
noway_assert(stmt->gtOper == GT_STMT);
optAddCopyLclNum = lclNum; // in
optAddCopyAsgnNode = nullptr; // out
fgWalkTreePre(&stmt->gtStmt.gtStmtExpr,
Compiler::optAddCopiesCallback,
(void *) this,
false);
noway_assert(optAddCopyAsgnNode);
GenTreePtr tree = optAddCopyAsgnNode;
GenTreePtr op1 = tree->gtOp.gtOp1;
noway_assert( tree && op1 &&
(tree->OperKind() & GTK_ASGOP) &&
(op1->gtOper == GT_LCL_VAR) &&
(op1->gtLclVarCommon.gtLclNum == lclNum));
/* TODO-Review: BB_UNITY_WEIGHT is not the correct block weight */
unsigned blockWeight = BB_UNITY_WEIGHT;
/* Increment its lvRefCnt and lvRefCntWtd twice */
lvaTable[copyLclNum].incRefCnts(blockWeight, this);
lvaTable[copyLclNum].incRefCnts(blockWeight, this);
/* Assign the old expression into the new temp */
GenTreePtr newAsgn = gtNewTempAssign(copyLclNum, tree->gtOp.gtOp2);
/* Copy the new temp to op1 */
GenTreePtr copyAsgn = gtNewAssignNode(op1, gtNewLclvNode(copyLclNum, typ));
/* Change the tree to a GT_COMMA with the two assignments as child nodes */
tree->gtBashToNOP();
tree->ChangeOper(GT_COMMA);
tree->gtOp.gtOp1 = newAsgn;
tree->gtOp.gtOp2 = copyAsgn;
tree->gtFlags |= ( newAsgn->gtFlags & GTF_ALL_EFFECT);
tree->gtFlags |= (copyAsgn->gtFlags & GTF_ALL_EFFECT);
}
#ifdef DEBUG
if (verbose)
{
printf("\nIntroducing a new copy for V%02u\n", lclNum);
gtDispTree(stmt->gtStmt.gtStmtExpr);
printf("\n");
}
#endif
}
}
/*****************************************************************************
*
* Initialize the assertion prop tracking logic.
*/
void Compiler::optAssertionInit(bool isLocalProp)
{
unsigned lclNum;
LclVarDsc * varDsc;
for (lclNum = 0, varDsc = lvaTable;
lclNum < lvaCount;
lclNum++ , varDsc++)
{
varDsc->lvAssertionDep = 0;
}
optAssertionCount = 0;
optAssertionPropagated = false;
optLocalAssertionProp = isLocalProp;
bbJtrueAssertionOut = nullptr;
if (!isLocalProp)
{
optValueNumToAsserts = new (getAllocator()) ValueNumToAssertsMap(getAllocator());
}
}
#ifdef DEBUG
void Compiler::optPrintAssertion(AssertionDsc* curAssertion, unsigned assertionIndex /* =0 */)
{
if (curAssertion->op1.kind == O1K_EXACT_TYPE)
{
printf("Type ");
}
else if (curAssertion->op1.kind == O1K_ARR_BND)
{
printf("ArrBnds ");
}
else if (curAssertion->op1.kind == O1K_SUBTYPE)
{
printf("Subtype ");
}
else if (curAssertion->op2.kind == O2K_LCLVAR_COPY)
{
printf("Copy ");
}
else if ((curAssertion->op2.kind == O2K_CONST_INT) ||
(curAssertion->op2.kind == O2K_CONST_LONG) ||
(curAssertion->op2.kind == O2K_CONST_DOUBLE) )
{
printf("Constant ");
}
else if (curAssertion->op2.kind == O2K_SUBRANGE)
{
printf("Subrange ");
}
else
{
printf("?assertion classification? ");
}
printf("Assertion: ");
if (!optLocalAssertionProp)
{
printf("(" STR_VN "%x," STR_VN "%x) ", curAssertion->op1.vn, curAssertion->op2.vn);
}
if ((curAssertion->op1.kind == O1K_LCLVAR) ||
(curAssertion->op1.kind == O1K_EXACT_TYPE) ||
(curAssertion->op1.kind == O1K_SUBTYPE) )
{
printf("V%02u", curAssertion->op1.lcl.lclNum);
if (curAssertion->op1.lcl.ssaNum != SsaConfig::RESERVED_SSA_NUM)
{
printf(".%02u", curAssertion->op1.lcl.ssaNum);
}
}
else if (curAssertion->op1.kind == O1K_ARR_BND)
{
printf("[idx:");
vnStore->vnDump(this, curAssertion->op1.bnd.vnIdx);
printf(";len:");
vnStore->vnDump(this, curAssertion->op1.bnd.vnLen);
printf("]");
}
else if (curAssertion->op1.kind == O1K_ARRLEN_OPER_BND)
{
printf("Oper_Bnd");
vnStore->vnDump(this, curAssertion->op1.vn);
}
else if (curAssertion->op1.kind == O1K_ARRLEN_LOOP_BND)
{
printf("Loop_Bnd");
vnStore->vnDump(this, curAssertion->op1.vn);
}
else
{
printf("?op1.kind?");
}
if (curAssertion->assertionKind == OAK_SUBRANGE)
{
printf(" in ");
}
else if (curAssertion->assertionKind == OAK_EQUAL)
{
if (curAssertion->op1.kind == O1K_LCLVAR)
printf(" == ");
else
printf(" is ");
}
else if (curAssertion->assertionKind == OAK_NO_THROW)
{
printf(" in range ");
}
else if (curAssertion->assertionKind == OAK_NOT_EQUAL)
{
if (curAssertion->op1.kind == O1K_LCLVAR)
printf(" != ");
else
printf(" is not ");
}
else
{
printf(" ?assertionKind? ");
}
if (curAssertion->op1.kind != O1K_ARR_BND)
{
switch (curAssertion->op2.kind)
{
case O2K_LCLVAR_COPY:
printf("V%02u", curAssertion->op2.lcl.lclNum);
if (curAssertion->op1.lcl.ssaNum != SsaConfig::RESERVED_SSA_NUM)
{
printf(".%02u", curAssertion->op1.lcl.ssaNum);
}
break;
case O2K_CONST_INT:
case O2K_IND_CNS_INT:
if (curAssertion->op1.kind == O1K_EXACT_TYPE)
{
printf("Exact Type MT(%08X)", dspPtr(curAssertion->op2.u1.iconVal));
assert(curAssertion->op2.u1.iconFlags != 0);
}
else if (curAssertion->op1.kind == O1K_SUBTYPE)
{
printf("MT(%08X)", dspPtr(curAssertion->op2.u1.iconVal));
assert(curAssertion->op2.u1.iconFlags != 0);
}
else if (curAssertion->op1.kind == O1K_ARRLEN_OPER_BND)
{
assert(!optLocalAssertionProp);
vnStore->vnDump(this, curAssertion->op2.vn);
}
else if (curAssertion->op1.kind == O1K_ARRLEN_LOOP_BND)
{
assert(!optLocalAssertionProp);
vnStore->vnDump(this, curAssertion->op2.vn);
}
else
{
unsigned lclNum = curAssertion->op1.lcl.lclNum; assert(lclNum < lvaCount);
LclVarDsc * varDsc = lvaTable + lclNum;
if (varDsc->lvType == TYP_REF)
{
assert(curAssertion->op2.u1.iconVal == 0);
printf("null");
}
else
{
if ((curAssertion->op2.u1.iconFlags & GTF_ICON_HDL_MASK) != 0)
printf("[%08p]", dspPtr(curAssertion->op2.u1.iconVal));
else
printf("%d", curAssertion->op2.u1.iconVal);
}
}
break;
case O2K_CONST_LONG:
printf("0x%016llx", curAssertion->op2.lconVal);
break;
case O2K_CONST_DOUBLE:
if (*((__int64 *)&curAssertion->op2.dconVal) == (__int64)I64(0x8000000000000000))
printf("-0.00000");
else
printf("%#lg", curAssertion->op2.dconVal);
break;
case O2K_SUBRANGE:
printf("[%d..%d]", curAssertion->op2.u2.loBound, curAssertion->op2.u2.hiBound);
break;
default:
printf("?op2.kind?");
break;
}
}
if (assertionIndex > 0)
{
printf(" :: index=#%02u, mask=%s", assertionIndex, genES2str(genCSEnum2bit(assertionIndex)));
}
printf("\n");
}
#endif // DEBUG
/******************************************************************************
*
* Helper to retrieve the "assertIndex" assertion. Note that assertIndex 0
* is NO_ASSERTION_INDEX and "optAssertionCount" is the last valid index.
*
*/
Compiler::AssertionDsc * Compiler::optGetAssertion(unsigned assertIndex)
{
assert(NO_ASSERTION_INDEX == 0);
noway_assert(assertIndex != NO_ASSERTION_INDEX);
if (assertIndex > MAX_ASSERTION_CNT)
{
return nullptr;
}
return &optAssertionTabPrivate[assertIndex - 1];
}
/*****************************************************************************
*
* A simple helper routine so not all callers need to supply a AssertionDsc*
* if they don't care about it. Refer overloaded method optCreateAssertion.
*
*/
unsigned Compiler::optCreateAssertion(GenTreePtr op1, GenTreePtr op2, optAssertionKind assertionKind)
{
AssertionDsc assertionDsc;
return optCreateAssertion(op1, op2, assertionKind, &assertionDsc);
}
/*****************************************************************************
*
* We attempt to create the following assertion:
*
* op1 assertionKind op2
*
* If we can create the assertion then update 'assertion' if we are
* unsuccessful assertion->assertionKind will be OAK_INVALID. If we are
* successful in creating the assertion we call optAddAssertion which adds
* the assertion to our assertion table.
*
* If we are able to create the assertion the return value is the
* assertionIndex for this assertion otherwise the return value is
* NO_ASSERTION_INDEX and we could not create the assertion.
*
*/
unsigned Compiler::optCreateAssertion(GenTreePtr op1, GenTreePtr op2,
optAssertionKind assertionKind,
AssertionDsc* assertion)
{
memset(assertion, 0, sizeof(AssertionDsc));
//
// If we cannot create an assertion using op1 and op2 then the assertionKind
// must be OAK_INVALID, so we initialize it to OAK_INVALID and only change it
// to a valid assertion when everything is good.
//
assertion->assertionKind = OAK_INVALID;
bool haveArgs = false;
var_types toType;
if (op1->gtOper == GT_ARR_BOUNDS_CHECK)
{
if (assertionKind == OAK_NO_THROW)
{
GenTreeBoundsChk* arrBndsChk = op1->AsBoundsChk();
assertion->assertionKind = assertionKind;
assertion->op1.kind = O1K_ARR_BND;
assertion->op1.bnd.vnIdx = arrBndsChk->gtIndex->gtVNPair.GetConservative();
assertion->op1.bnd.vnLen = arrBndsChk->gtArrLen->gtVNPair.GetConservative();
goto DONE_ASSERTION;
}
}
//
// Did we recieve Helper call args?
//
if (op1->gtOper == GT_LIST)
{
if (op2->gtOper != GT_LIST)
{
goto DONE_ASSERTION; // Don't make an assertion
}
op1 = op1->gtOp.gtOp1;
op2 = op2->gtOp.gtOp1;
haveArgs = true;
}
//
// Are we trying to make a non-null assertion?
//
if (op2 == nullptr)
{
assert(haveArgs == false);
//
// Must an OAK_NOT_EQUAL assertion
//
noway_assert(assertionKind == OAK_NOT_EQUAL);
//
// Set op1 to the instance pointer of the indirection
//
if ((op1->gtOper == GT_ADD) && (op1->gtType == TYP_BYREF))
{
op1 = op1->gtOp.gtOp1;
if ((op1->gtOper == GT_ADD) && (op1->gtType == TYP_BYREF))
{
op1 = op1->gtOp.gtOp1;
}
}
if (op1->gtOper != GT_LCL_VAR)
{
goto DONE_ASSERTION; // Don't make an assertion
}
unsigned lclNum = op1->gtLclVarCommon.gtLclNum; noway_assert(lclNum < lvaCount);
LclVarDsc * lclVar = &lvaTable[lclNum];
//
// We only perform null-checks on GC refs
// so only make non-null assertions about GC refs
//
if (lclVar->TypeGet() != TYP_REF)
{
goto DONE_ASSERTION; // Don't make an assertion
}
// If the local variable has its address exposed then bail
if (lclVar->lvAddrExposed)
{
goto DONE_ASSERTION; // Don't make an assertion
}
assertion->op1.kind = O1K_LCLVAR;
assertion->op1.lcl.lclNum = lclNum;
assertion->op1.vn = op1->gtVNPair.GetConservative();
assertion->op1.lcl.ssaNum = op1->AsLclVarCommon()->GetSsaNum();
assertion->assertionKind = assertionKind;
assertion->op2.kind = O2K_CONST_INT;
assertion->op2.vn = ValueNumStore::VNForNull();
assertion->op2.u1.iconVal = 0;
assertion->op2.u1.iconFlags = 0;
#ifdef _TARGET_64BIT_
assertion->op2.u1.iconFlags |= 1; // Signify that this is really TYP_LONG
#endif // _TARGET_64BIT_
}
//
// Are we making an assertion about a local variable?
//
else if (op1->gtOper == GT_LCL_VAR)
{
unsigned lclNum = op1->gtLclVarCommon.gtLclNum; noway_assert(lclNum < lvaCount);
LclVarDsc * lclVar = &lvaTable[lclNum];
// If the local variable has its address exposed then bail
if (lclVar->lvAddrExposed)
{
goto DONE_ASSERTION; // Don't make an assertion
}
#ifdef DEBUG
if (lclVar->lvDblWasInt)
{
goto DONE_ASSERTION; // Don't make an assertion
}
#endif
if (haveArgs)
{
//
// Must either be an OAK_EQUAL or an OAK_NOT_EQUAL assertion
//
if ((assertionKind != OAK_EQUAL) && (assertionKind != OAK_NOT_EQUAL))
{
goto DONE_ASSERTION; // Don't make an assertion
}
if (op2->gtOper == GT_IND)
{
op2 = op2->gtOp.gtOp1;
assertion->op2.kind = O2K_IND_CNS_INT;
}
else
{
assertion->op2.kind = O2K_CONST_INT;
}
if (op2->gtOper != GT_CNS_INT)
{
goto DONE_ASSERTION; // Don't make an assertion
}
//
// TODO-CQ: Check for Sealed class and change kind to O1K_EXACT_TYPE
// And consider the special cases, like CORINFO_FLG_SHAREDINST or CORINFO_FLG_VARIANCE
// where a class can be sealed, but they don't behave as exact types because casts to
// non-base types sometimes still succeed.
//
assertion->op1.kind = O1K_SUBTYPE;
assertion->op1.lcl.lclNum = lclNum;
assertion->op1.vn = op1->gtVNPair.GetConservative();
assertion->op1.lcl.ssaNum = op1->AsLclVarCommon()->GetSsaNum();
assertion->op2.u1.iconVal = op2->gtIntCon.gtIconVal;
assertion->op2.vn = op2->gtVNPair.GetConservative();
assertion->op2.u1.iconFlags = op2->GetIconHandleFlag();
//
// Ok everything has been set and the assertion looks good
//
assertion->assertionKind = assertionKind;
}
else // !haveArgs
{
/* Skip over a GT_COMMA node(s), if necessary */
while (op2->gtOper == GT_COMMA)
{
op2 = op2->gtOp.gtOp2;
}
assertion->op1.kind = O1K_LCLVAR;
assertion->op1.lcl.lclNum = lclNum;
assertion->op1.vn = op1->gtVNPair.GetConservative();
assertion->op1.lcl.ssaNum = op1->AsLclVarCommon()->GetSsaNum();
switch (op2->gtOper)
{
optOp2Kind op2Kind;
//
// No Assertion
//
default:
goto DONE_ASSERTION; // Don't make an assertion
//
// Constant Assertions
//
case GT_CNS_INT:
op2Kind = O2K_CONST_INT;
goto CNS_COMMON;
case GT_CNS_LNG:
op2Kind = O2K_CONST_LONG;
goto CNS_COMMON;
case GT_CNS_DBL:
op2Kind = O2K_CONST_DOUBLE;
goto CNS_COMMON;
CNS_COMMON:
{
//
// Must either be an OAK_EQUAL or an OAK_NOT_EQUAL assertion
//
if ((assertionKind != OAK_EQUAL) && (assertionKind != OAK_NOT_EQUAL))
{
goto DONE_ASSERTION; // Don't make an assertion
}
// If the LclVar is a TYP_LONG then we only make
// assertions where op2 is also TYP_LONG
//
if ((lclVar->TypeGet() == TYP_LONG) && (op2->TypeGet() != TYP_LONG))
{
goto DONE_ASSERTION; // Don't make an assertion
}
assertion->op2.kind = op2Kind;
assertion->op2.lconVal = 0;
assertion->op2.vn = op2->gtVNPair.GetConservative();
if (op2->gtOper == GT_CNS_INT)
{
#ifdef _TARGET_ARM_
// Do not Constant-Prop large constants for ARM
if (!codeGen->validImmForMov(op2->gtIntCon.gtIconVal))
{
goto DONE_ASSERTION; // Don't make an assertion
}
#endif // _TARGET_ARM_
assertion->op2.u1.iconVal = op2->gtIntCon.gtIconVal;
assertion->op2.u1.iconFlags = op2->GetIconHandleFlag();
#ifdef _TARGET_64BIT_
if (op2->TypeGet() == TYP_LONG || op2->TypeGet() == TYP_BYREF)
{
assertion->op2.u1.iconFlags |= 1; // Signify that this is really TYP_LONG
}
#endif // _TARGET_64BIT_
}
else if (op2->gtOper == GT_CNS_LNG)
{
assertion->op2.lconVal = op2->gtLngCon.gtLconVal;
}
else
{
noway_assert(op2->gtOper == GT_CNS_DBL);
/* If we have an NaN value then don't record it */
if (_isnan(op2->gtDblCon.gtDconVal))
{
goto DONE_ASSERTION; // Don't make an assertion
}
assertion->op2.dconVal = op2->gtDblCon.gtDconVal;
}
//
// Ok everything has been set and the assertion looks good
//
assertion->assertionKind = assertionKind;
}
break;
//
// Copy Assertions