This repository was 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.6k
/
Copy pathassertionprop.cpp
5265 lines (4669 loc) · 187 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
// 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 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(GenTree** pTree, fgWalkData* data)
{
GenTree* tree = *pTree;
if (tree->OperIs(GT_ASG))
{
GenTree* op1 = tree->AsOp()->gtOp1;
Compiler* comp = data->compiler;
if ((op1->gtOper == GT_LCL_VAR) && (op1->AsLclVarCommon()->GetLclNum() == comp->optAddCopyLclNum))
{
comp->optAddCopyAsgnNode = tree;
return WALK_ABORT;
}
}
return WALK_CONTINUE;
}
/*****************************************************************************
*
* Add new copies before Assertion Prop.
*/
voidCompiler::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 || !varTypeIsEnregisterable(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())
{
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;
}
// 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 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;
BlockSetOps::Iter iter(this, varDsc->lvRefBlks);
unsigned bbNum = 0;
while (iter.NextElem(&bbNum))
{
/* Find the block 'bbNum' */
BasicBlock* block = fgFirstBB;
while (block && (block->bbNum != bbNum))
{
block = block->bbNext;
}
noway_assert(block && (block->bbNum == bbNum));
bool importantUseInBlock = (varDsc->lvIsParam) && (block->getBBWeight(this) > paramAvgWtdRefDiv2);
bool isPreHeaderBlock = ((block->bbFlags & BBF_LOOP_PREHEADER) != 0);
BlockSet blockDom(BlockSetOps::UninitVal());
BlockSet 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 " FMT_BB ", bbWeight is %s", bbNum,
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;
}
Statement* stmt;
unsigned copyLclNum = lvaGrabTemp(falseDEBUGARG("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
GenTree* 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 " FMT_BB ", bbWeight is %s", block->bbNum,
refCntWtd2str(block->getBBWeight(this)));
printf(", bestWeight is %s\n", refCntWtd2str(bestWeight));
}
#endif
/* We have already calculated paramImportantUseDom above. */
BlockSetOps::Iter iter(this, paramImportantUseDom);
unsigned bbNum = 0;
while (iter.NextElem(&bbNum))
{
/* Advance block to point to 'bbNum' */
/* This assumes that the iterator returns block number is increasing lexical order. */
while (block && (block->bbNum != bbNum))
{
block = block->bbNext;
}
noway_assert(block && (block->bbNum == bbNum));
#ifdef DEBUG
if (verbose)
{
printf(" Considering " FMT_BB ", 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 defined(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
CLANG_FORMAT_COMMENT_ANCHOR;
#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
CLANG_FORMAT_COMMENT_ANCHOR;
#ifdef DEBUG
if (verbose)
{
printf(" Insert copy at the %s of " FMT_BB "\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 = fgNewStmtAtBeg(bestBlock, copyAsgn);
}
else
{
stmt = fgNewStmtNearEnd(bestBlock, copyAsgn);
}
}
else
{
noway_assert(varDsc->lvDefStmt != nullptr);
/* Locate the assignment to varDsc in the lvDefStmt */
stmt = varDsc->lvDefStmt;
optAddCopyLclNum = lclNum; // in
optAddCopyAsgnNode = nullptr; // out
fgWalkTreePre(stmt->GetRootNodePointer(), Compiler::optAddCopiesCallback, (void*)this, false);
noway_assert(optAddCopyAsgnNode);
GenTree* tree = optAddCopyAsgnNode;
GenTree* op1 = tree->AsOp()->gtOp1;
noway_assert(tree && op1 && tree->OperIs(GT_ASG) && (op1->gtOper == GT_LCL_VAR) &&
(op1->AsLclVarCommon()->GetLclNum() == lclNum));
/* Assign the old expression into the new temp */
GenTree* newAsgn = gtNewTempAssign(copyLclNum, tree->AsOp()->gtOp2);
/* Copy the new temp to op1 */
GenTree* 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->AsOp()->gtOp1 = newAsgn;
tree->AsOp()->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->GetRootNode());
printf("\n");
}
#endif
}
}
//------------------------------------------------------------------------------
// GetAssertionDep: Retrieve the assertions on this local variable
//
// Arguments:
// lclNum - The local var id.
//
// Return Value:
// The dependent assertions (assertions using the value of the local var)
// of the local var.
//
ASSERT_TP& Compiler::GetAssertionDep(unsigned lclNum)
{
JitExpandArray<ASSERT_TP>& dep = *optAssertionDep;
if (dep[lclNum] == nullptr)
{
dep[lclNum] = BitVecOps::MakeEmpty(apTraits);
}
return dep[lclNum];
}
/*****************************************************************************
*
* Initialize the assertion prop bitset traits and the default bitsets.
*/
voidCompiler::optAssertionTraitsInit(AssertionIndex assertionCount)
{
apTraits = new (this, CMK_AssertionProp) BitVecTraits(assertionCount, this);
apFull = BitVecOps::MakeFull(apTraits);
}
/*****************************************************************************
*
* Initialize the assertion prop tracking logic.
*/
voidCompiler::optAssertionInit(bool isLocalProp)
{
// Use a function countFunc to determine a proper maximum assertion count for the
// method being compiled. The function is linear to the IL size for small and
// moderate methods. For large methods, considering throughput impact, we track no
// more than 64 assertions.
// Note this tracks at most only 256 assertions.
staticconst AssertionIndex countFunc[] = {64, 128, 256, 64};
staticconstunsigned lowerBound = 0;
staticconstunsigned upperBound = _countof(countFunc) - 1;
constunsigned codeSize = info.compILCodeSize / 512;
optMaxAssertionCount = countFunc[isLocalProp ? lowerBound : min(upperBound, codeSize)];
optLocalAssertionProp = isLocalProp;
optAssertionTabPrivate = new (this, CMK_AssertionProp) AssertionDsc[optMaxAssertionCount];
optComplementaryAssertionMap =
new (this, CMK_AssertionProp) AssertionIndex[optMaxAssertionCount + 1](); // zero-inited (NO_ASSERTION_INDEX)
assert(NO_ASSERTION_INDEX == 0);
if (!isLocalProp)
{
optValueNumToAsserts = new (getAllocator()) ValueNumToAssertsMap(getAllocator());
}
if (optAssertionDep == nullptr)
{
optAssertionDep = new (this, CMK_AssertionProp) JitExpandArray<ASSERT_TP>(getAllocator(), max(1, lvaCount));
}
optAssertionTraitsInit(optMaxAssertionCount);
optAssertionCount = 0;
optAssertionPropagated = false;
bbJtrueAssertionOut = nullptr;
}
#ifdef DEBUG
voidCompiler::optPrintAssertion(AssertionDsc* curAssertion, AssertionIndex assertionIndex /* =0 */)
{
if (curAssertion->op1.kind == O1K_EXACT_TYPE)
{
printf("Type ");
}
elseif (curAssertion->op1.kind == O1K_ARR_BND)
{
printf("ArrBnds ");
}
elseif (curAssertion->op1.kind == O1K_SUBTYPE)
{
printf("Subtype ");
}
elseif (curAssertion->op2.kind == O2K_LCLVAR_COPY)
{
printf("Copy ");
}
elseif ((curAssertion->op2.kind == O2K_CONST_INT) || (curAssertion->op2.kind == O2K_CONST_LONG) ||
(curAssertion->op2.kind == O2K_CONST_DOUBLE))
{
printf("Constant ");
}
elseif (curAssertion->op2.kind == O2K_SUBRANGE)
{
printf("Subrange ");
}
else
{
printf("?assertion classification? ");
}
printf("Assertion: ");
if (!optLocalAssertionProp)
{
printf("(%d, %d) ", curAssertion->op1.vn, curAssertion->op2.vn);
}
if (!optLocalAssertionProp)
{
printf("(" FMT_VN "," FMT_VN ") ", 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);
}
}
elseif (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("]");
}
elseif (curAssertion->op1.kind == O1K_BOUND_OPER_BND)
{
printf("Oper_Bnd");
vnStore->vnDump(this, curAssertion->op1.vn);
}
elseif (curAssertion->op1.kind == O1K_BOUND_LOOP_BND)
{
printf("Loop_Bnd");
vnStore->vnDump(this, curAssertion->op1.vn);
}
elseif (curAssertion->op1.kind == O1K_CONSTANT_LOOP_BND)
{
printf("Const_Loop_Bnd");
vnStore->vnDump(this, curAssertion->op1.vn);
}
elseif (curAssertion->op1.kind == O1K_VALUE_NUMBER)
{
printf("Value_Number");
vnStore->vnDump(this, curAssertion->op1.vn);
}
else
{
printf("?op1.kind?");
}
if (curAssertion->assertionKind == OAK_SUBRANGE)
{
printf(" in ");
}
elseif (curAssertion->assertionKind == OAK_EQUAL)
{
if (curAssertion->op1.kind == O1K_LCLVAR)
{
printf(" == ");
}
else
{
printf(" is ");
}
}
elseif (curAssertion->assertionKind == OAK_NO_THROW)
{
printf(" in range ");
}
elseif (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);
}
elseif (curAssertion->op1.kind == O1K_SUBTYPE)
{
printf("MT(%08X)", dspPtr(curAssertion->op2.u1.iconVal));
assert(curAssertion->op2.u1.iconFlags != 0);
}
elseif (curAssertion->op1.kind == O1K_BOUND_OPER_BND)
{
assert(!optLocalAssertionProp);
vnStore->vnDump(this, curAssertion->op2.vn);
}
elseif (curAssertion->op1.kind == O1K_BOUND_LOOP_BND)
{
assert(!optLocalAssertionProp);
vnStore->vnDump(this, curAssertion->op2.vn);
}
elseif (curAssertion->op1.kind == O1K_CONSTANT_LOOP_BND)
{
assert(!optLocalAssertionProp);
vnStore->vnDump(this, curAssertion->op2.vn);
}
else
{
var_types op1Type;
if (curAssertion->op1.kind == O1K_VALUE_NUMBER)
{
op1Type = vnStore->TypeOfVN(curAssertion->op1.vn);
}
else
{
unsigned lclNum = curAssertion->op1.lcl.lclNum;
assert(lclNum < lvaCount);
LclVarDsc* varDsc = lvaTable + lclNum;
op1Type = varDsc->lvType;
}
if (op1Type == 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=", assertionIndex);
printf("%s", BitVecOps::ToString(apTraits, BitVecOps::MakeSingleton(apTraits, assertionIndex - 1)));
}
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(AssertionIndex assertIndex)
{
assert(NO_ASSERTION_INDEX == 0);
assert(assertIndex != NO_ASSERTION_INDEX);
assert(assertIndex <= optAssertionCount);
AssertionDsc* assertion = &optAssertionTabPrivate[assertIndex - 1];
#ifdef DEBUG
optDebugCheckAssertion(assertion);
#endif
return assertion;
}
//------------------------------------------------------------------------
// optCreateAssertion: Create an (op1 assertionKind op2) assertion.
//
// Arguments:
// op1 - the first assertion operand
// op2 - the second assertion operand
// assertionKind - the assertion kind
// helperCallArgs - when true this indicates that the assertion operands
// are the arguments of a type cast helper call such as
// CORINFO_HELP_ISINSTANCEOFCLASS
// Return Value:
// The new assertion index or NO_ASSERTION_INDEX if a new assertion
// was not created.
//
// Notes:
// Assertion creation may fail either because the provided assertion
// operands aren't supported or because the assertion table is full.
//
AssertionIndex Compiler::optCreateAssertion(GenTree* op1,
GenTree* op2,
optAssertionKind assertionKind,
bool helperCallArgs)
{
assert((op1 != nullptr) && !op1->OperIs(GT_LIST));
assert((op2 == nullptr) || !op2->OperIs(GT_LIST));
assert(!helperCallArgs || (op2 != nullptr));
AssertionDsc assertion;
memset(&assertion, 0, sizeof(AssertionDsc));
assert(assertion.assertionKind == OAK_INVALID);
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 = vnStore->VNConservativeNormalValue(arrBndsChk->gtIndex->gtVNPair);
assertion.op1.bnd.vnLen = vnStore->VNConservativeNormalValue(arrBndsChk->gtArrLen->gtVNPair);
goto DONE_ASSERTION;
}
}
//
// Are we trying to make a non-null assertion?
//
if (op2 == nullptr)
{
//
// Must an OAK_NOT_EQUAL assertion
//
noway_assert(assertionKind == OAK_NOT_EQUAL);
//
// Set op1 to the instance pointer of the indirection
//
ssize_t offset = 0;
while ((op1->gtOper == GT_ADD) && (op1->gtType == TYP_BYREF))
{
if (op1->gtGetOp2()->IsCnsIntOrI())
{
offset += op1->gtGetOp2()->AsIntCon()->gtIconVal;
op1 = op1->gtGetOp1();
}
elseif (op1->gtGetOp1()->IsCnsIntOrI())
{
offset += op1->gtGetOp1()->AsIntCon()->gtIconVal;
op1 = op1->gtGetOp2();
}
else
{
break;
}
}
if (fgIsBigOffset(offset) || op1->gtOper != GT_LCL_VAR)
{
goto DONE_ASSERTION; // Don't make an assertion
}
unsigned lclNum = op1->AsLclVarCommon()->GetLclNum();
noway_assert(lclNum < lvaCount);
LclVarDsc* lclVar = &lvaTable[lclNum];
ValueNum vn;
//
// We only perform null-checks on GC refs
// so only make non-null assertions about GC refs or byrefs if we can't determine
// the corresponding ref.
//
if (lclVar->TypeGet() != TYP_REF)
{
if (optLocalAssertionProp || (lclVar->TypeGet() != TYP_BYREF))
{
goto DONE_ASSERTION; // Don't make an assertion
}
vn = vnStore->VNConservativeNormalValue(op1->gtVNPair);
VNFuncApp funcAttr;
// Try to get value number corresponding to the GC ref of the indirection
while (vnStore->GetVNFunc(vn, &funcAttr) && (funcAttr.m_func == (VNFunc)GT_ADD) &&
(vnStore->TypeOfVN(vn) == TYP_BYREF))
{
if (vnStore->IsVNConstant(funcAttr.m_args[1]) &&
varTypeIsIntegral(vnStore->TypeOfVN(funcAttr.m_args[1])))
{
offset += vnStore->CoercedConstantValue<ssize_t>(funcAttr.m_args[1]);
vn = funcAttr.m_args[0];
}
elseif (vnStore->IsVNConstant(funcAttr.m_args[0]) &&
varTypeIsIntegral(vnStore->TypeOfVN(funcAttr.m_args[0])))
{
offset += vnStore->CoercedConstantValue<ssize_t>(funcAttr.m_args[0]);
vn = funcAttr.m_args[1];
}
else
{
break;
}
}
if (fgIsBigOffset(offset))
{
goto DONE_ASSERTION; // Don't make an assertion
}
assertion.op1.kind = O1K_VALUE_NUMBER;
}
else
{
// 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.lcl.ssaNum = op1->AsLclVarCommon()->GetSsaNum();
vn = vnStore->VNConservativeNormalValue(op1->gtVNPair);
}
assertion.op1.vn = vn;
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?
//
elseif (op1->gtOper == GT_LCL_VAR)
{
unsigned lclNum = op1->AsLclVarCommon()->GetLclNum();
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
}
if (helperCallArgs)
{
//
// 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->AsOp()->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