summaryrefslogtreecommitdiff
path: root/packaging/0019-JIT-Fix-value-type-box-optimization.patch
blob: db31e3ff8fde75ce98e04ed0e75127e644982219 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
From dc2e9cc4c21e0cf565dbb1b4d4f029157779ddcc Mon Sep 17 00:00:00 2001
From: Andy Ayers <andya@microsoft.com>
Date: Sat, 22 Jul 2017 09:16:29 -0700
Subject: [PATCH 19/47] JIT: Fix value type box optimization

Boxing a value type produces a non-null result. If the result of the box is
only used to feed a compare against null, the jit tries to optimize the box
away entirely since the result of the comparison is known. Such idiomatic
expressions arise fairly often in generics instantiated over value types.

In the current implementation the box expands into two parts. The first is
an upstream statement to allocate a boxed object and assign a reference to
the boxed object to a local var known as the "box temp". The second is an
expression tree whose value is the box temp that also contains an an
encapsulated copy from the value being boxed to the payload section of the
boxed object. The box node also contains a pointer back to the first
statement (more on this later).

In the examples being discussed here this second tree is a child of a compare
node whose other child is a null pointer. When the optimization fires, the
upstream allocation statement is located via the pointer in the box node and
removed, and the entire compare is replaced with a constant 0 or 1 as
appropriate. Unfortunately the encapsulated copy in the box subtree may
include side effects that should be preserved, and so this transformation is
unsafe.

Note that the copy subtree as a whole will always contain side effects, since
the copy is storing values into the heap, and that copy now will not happen.
But the side effects that happen when producing the value to box must remain.

In the initial example from #12949 the side effects in question were
introduced by the jit's optimizer to capure a CSE definition. #13016 gives
several other examples where the side effects are present in the initial user
code. For instance the value being boxed might come from an array, in which
case the encapsulated copy in the box expression tree would contain the array
null check and bounds check. So removing the entire tree can alter behavior.

This fix attempts to carefully preserve the important side effects by
reworking how a box is imported. The copy is now moved out from under the box
into a second upstream statement. The box itself is then just a trivial
side-effect-free reference to the box temp. To ensure proper ordering of side
effects the jit spills the evaluation stack before appending the copy
statement.

When the optimization fires the jit removes the upstream heap allocation
as before, as well as the now-trivial compare tree. It analyzes the source
side of the upstream copy. If it is side effect free, the copy is removed
entirely. If not, the jit modifies the copy into a minimal load of the
boxed value, and this load should reproduce the necessary side effects.

The optimization is only performed when the tree shape of the copy matches
expected patterns.

There are some expected cases where the tree won't match, for instance if the
optimization is invoked while the jit is inlining. Because this optimization
runs at several points the jit can catch these cases once inlining completes.
There is one case that is not handled that could be -- if the assignment part
of the copy is itself a subtree of a comma. This doesn't happen often.

The optimization is now also extended to handle the case where the comparision
operation is `cgt.un`. This doesn't catch any new cases but causes the
optimization to happen earlier, typically during importation, which should
reduce jit time slightly.

Generally the split of the box into two upstream statements reduces code size,
especially when the box expression is incorporated into a larger tree -- for
example a call. However in some cases where the value being boxed comes from
an array, preserving the array bounds check now causes loop cloning to kick
in and increase code size. Hence the overall size impact on the jit-diff set is
essentially zero.

Added a number of new test cases showing the variety of situations that must
be handled and the need to spill before appending the copy statement.

Fixes #12949.
---
 src/jit/gentree.cpp                                | 167 +++++++++++++++++++--
 src/jit/gentree.h                                  |  13 +-
 src/jit/importer.cpp                               |  15 +-
 .../JitBlue/GitHub_12949/GitHub_12949_1.cs         |  51 +++++++
 .../JitBlue/GitHub_12949/GitHub_12949_1.csproj     |  43 ++++++
 .../JitBlue/GitHub_12949/GitHub_12949_2.cs         |  43 ++++++
 .../JitBlue/GitHub_12949/GitHub_12949_2.csproj     |  43 ++++++
 .../JitBlue/GitHub_12949/GitHub_12949_3.cs         |  41 +++++
 .../JitBlue/GitHub_12949/GitHub_12949_3.csproj     |  43 ++++++
 .../JitBlue/GitHub_12949/GitHub_12949_4.cs         |  41 +++++
 .../JitBlue/GitHub_12949/GitHub_12949_4.csproj     |  43 ++++++
 .../JitBlue/GitHub_12949/GitHub_12949_5.cs         |  46 ++++++
 .../JitBlue/GitHub_12949/GitHub_12949_5.csproj     |  43 ++++++
 .../JitBlue/GitHub_12949/GitHub_12949_6.cs         |  60 ++++++++
 .../JitBlue/GitHub_12949/GitHub_12949_6.csproj     |  43 ++++++
 .../JitBlue/GitHub_12949/GitHub_12949_7.cs         |  58 +++++++
 .../JitBlue/GitHub_12949/GitHub_12949_7.csproj     |  43 ++++++
 .../JitBlue/GitHub_12949/GitHub_12949_8.cs         |  42 ++++++
 .../JitBlue/GitHub_12949/GitHub_12949_8.csproj     |  43 ++++++
 19 files changed, 902 insertions(+), 19 deletions(-)
 create mode 100644 tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_1.cs
 create mode 100644 tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_1.csproj
 create mode 100644 tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_2.cs
 create mode 100644 tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_2.csproj
 create mode 100644 tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_3.cs
 create mode 100644 tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_3.csproj
 create mode 100644 tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_4.cs
 create mode 100644 tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_4.csproj
 create mode 100644 tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_5.cs
 create mode 100644 tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_5.csproj
 create mode 100644 tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_6.cs
 create mode 100644 tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_6.csproj
 create mode 100644 tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_7.cs
 create mode 100644 tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_7.csproj
 create mode 100644 tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_8.cs
 create mode 100644 tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_8.csproj

diff --git a/src/jit/gentree.cpp b/src/jit/gentree.cpp
index 25e9e10..3736f7a 100644
--- a/src/jit/gentree.cpp
+++ b/src/jit/gentree.cpp
@@ -8050,7 +8050,8 @@ GenTreePtr Compiler::gtCloneExpr(
 
             case GT_BOX:
                 copy = new (this, GT_BOX)
-                    GenTreeBox(tree->TypeGet(), tree->gtOp.gtOp1, tree->gtBox.gtAsgStmtWhenInlinedBoxValue);
+                    GenTreeBox(tree->TypeGet(), tree->gtOp.gtOp1, tree->gtBox.gtAsgStmtWhenInlinedBoxValue,
+                               tree->gtBox.gtCopyStmtWhenInlinedBoxValue);
                 break;
 
             case GT_INTRINSIC:
@@ -12459,32 +12460,168 @@ GenTreePtr Compiler::gtFoldExprSpecial(GenTreePtr tree)
 
     switch (oper)
     {
-
         case GT_EQ:
         case GT_NE:
+        case GT_GT:
             // Optimize boxed value classes; these are always false.  This IL is
             // generated when a generic value is tested against null:
             //     <T> ... foo(T x) { ... if ((object)x == null) ...
             if (val == 0 && op->IsBoxedValue())
             {
-                // Change the assignment node so we don't generate any code for it.
+                // The tree under the box must be side effect free
+                // since we drop it if we optimize the compare.
+                assert(!gtTreeHasSideEffects(op->gtBox.gtOp.gtOp1, GTF_SIDE_EFFECT));
 
+                // grab related parts for the optimization
                 GenTreePtr asgStmt = op->gtBox.gtAsgStmtWhenInlinedBoxValue;
                 assert(asgStmt->gtOper == GT_STMT);
-                GenTreePtr asg = asgStmt->gtStmt.gtStmtExpr;
-                assert(asg->gtOper == GT_ASG);
+                GenTreePtr copyStmt = op->gtBox.gtCopyStmtWhenInlinedBoxValue;
+                assert(copyStmt->gtOper == GT_STMT);
 #ifdef DEBUG
                 if (verbose)
                 {
-                    printf("Bashing ");
-                    printTreeID(asg);
-                    printf(" to NOP as part of dead box operation\n");
+                    printf("\nAttempting to optimize BOX(valueType) %s null\n", GenTree::OpName(oper));
                     gtDispTree(tree);
+                    printf("\nWith assign\n");
+                    gtDispTree(asgStmt);
+                    printf("\nAnd copy\n");
+                    gtDispTree(copyStmt);
                 }
 #endif
+
+                // We don't expect GT_GT with signed compares, and we
+                // can't predict the result if we do see it, since the
+                // boxed object addr could have its high bit set.
+                if ((oper == GT_GT) && !tree->IsUnsigned())
+                {
+                    JITDUMP(" bailing; unexpected signed compare via GT_GT\n");
+                    goto FAIL;
+                }
+
+                // If we don't recognize the form of the assign, bail.
+                GenTreePtr asg = asgStmt->gtStmt.gtStmtExpr;
+                if (asg->gtOper != GT_ASG)
+                {
+                    JITDUMP(" bailing; unexpected assignment op %s\n", GenTree::OpName(asg->gtOper));
+                    goto FAIL;
+                }
+
+                // If we don't recognize the form of the copy, bail.
+                GenTree* copy = copyStmt->gtStmt.gtStmtExpr;
+                if (copy->gtOper != GT_ASG)
+                {
+                    // GT_RET_EXPR is a tolerable temporary failure.
+                    // The jit will revisit this optimization after
+                    // inlining is done.
+                    if (copy->gtOper == GT_RET_EXPR)
+                    {
+                        JITDUMP(" bailing; must wait for replacement of copy %s\n", GenTree::OpName(copy->gtOper));
+                    }
+                    else
+                    {
+                        // Anything else is a missed case we should
+                        // figure out how to handle.  One known case
+                        // is GT_COMMAs enclosing the GT_ASG we are
+                        // looking for.
+                        JITDUMP(" bailing; unexpected copy op %s\n", GenTree::OpName(copy->gtOper));
+                    }
+                    goto FAIL;
+                }
+
+                // If the copy is a struct copy, make sure we know how to isolate
+                // any source side effects.
+                GenTreePtr copySrc = copy->gtOp.gtOp2;
+
+                // If the copy source is from a pending inline, wait for it to resolve.
+                if (copySrc->gtOper == GT_RET_EXPR)
+                {
+                    JITDUMP(" bailing; must wait for replacement of copy source %s\n",
+                            GenTree::OpName(copySrc->gtOper));
+                    goto FAIL;
+                }
+
+                bool hasSrcSideEffect = false;
+                bool isStructCopy     = false;
+
+                if (gtTreeHasSideEffects(copySrc, GTF_SIDE_EFFECT))
+                {
+                    hasSrcSideEffect = true;
+
+                    if (copySrc->gtType == TYP_STRUCT)
+                    {
+                        isStructCopy = true;
+
+                        if ((copySrc->gtOper != GT_OBJ) && (copySrc->gtOper != GT_IND) && (copySrc->gtOper != GT_FIELD))
+                        {
+                            // We don't know how to handle other cases, yet.
+                            JITDUMP(" bailing; unexpected copy source struct op with side effect %s\n",
+                                    GenTree::OpName(copySrc->gtOper));
+                            goto FAIL;
+                        }
+                    }
+                }
+
+                // Proceed with the optimization
+                //
+                // Change the assignment expression to a NOP.
+                JITDUMP("\nBashing NEWOBJ [%06u] to NOP\n", dspTreeID(asg));
                 asg->gtBashToNOP();
 
-                op = gtNewIconNode(oper == GT_NE);
+                // Change the copy expression so it preserves key
+                // source side effects.
+                JITDUMP("\nBashing COPY [%06u]", dspTreeID(copy));
+
+                if (!hasSrcSideEffect)
+                {
+                    // If there were no copy source side effects just bash
+                    // the copy to a NOP.
+                    copy->gtBashToNOP();
+                    JITDUMP(" to NOP\n");
+                }
+                else if (!isStructCopy)
+                {
+                    // For scalar types, go ahead and produce the
+                    // value as the copy is fairly cheap and likely
+                    // the optimizer can trim things down to just the
+                    // minimal side effect parts.
+                    copyStmt->gtStmt.gtStmtExpr = copySrc;
+                    JITDUMP(" to scalar read via [%06u]\n", dspTreeID(copySrc));
+                }
+                else
+                {
+                    // For struct types read the first byte of the
+                    // source struct; there's no need to read the
+                    // entire thing, and no place to put it.
+                    assert(copySrc->gtOper == GT_OBJ || copySrc->gtOper == GT_IND || copySrc->gtOper == GT_FIELD);
+                    copySrc->ChangeOper(GT_IND);
+                    copySrc->gtType             = TYP_BYTE;
+                    copyStmt->gtStmt.gtStmtExpr = copySrc;
+                    JITDUMP(" to read first byte of struct via modified [%06u]\n", dspTreeID(copySrc));
+                }
+
+                // Set up the result of the compare.
+                int compareResult = 0;
+                if (oper == GT_GT)
+                {
+                    // GT_GT(null, box) == false
+                    // GT_GT(box, null) == true
+                    compareResult = (op1 == op);
+                }
+                else if (oper == GT_EQ)
+                {
+                    // GT_EQ(box, null) == false
+                    // GT_EQ(null, box) == false
+                    compareResult = 0;
+                }
+                else
+                {
+                    assert(oper == GT_NE);
+                    // GT_NE(box, null) == true
+                    // GT_NE(null, box) == true
+                    compareResult = 1;
+                }
+                op = gtNewIconNode(compareResult);
+
                 if (fgGlobalMorph)
                 {
                     if (!fgIsInlining())
@@ -12497,9 +12634,15 @@ GenTreePtr Compiler::gtFoldExprSpecial(GenTreePtr tree)
                     op->gtNext = tree->gtNext;
                     op->gtPrev = tree->gtPrev;
                 }
-                fgSetStmtSeq(asgStmt);
+
+                if (fgStmtListThreaded)
+                {
+                    fgSetStmtSeq(asgStmt);
+                    fgSetStmtSeq(copyStmt);
+                }
                 return op;
             }
+
             break;
 
         case GT_ADD:
@@ -12667,7 +12810,9 @@ GenTreePtr Compiler::gtFoldExprSpecial(GenTreePtr tree)
             break;
     }
 
-    /* The node is not foldable */
+/* The node is not foldable */
+
+FAIL:
 
     return tree;
 
diff --git a/src/jit/gentree.h b/src/jit/gentree.h
index 1833a39..41f65f0 100644
--- a/src/jit/gentree.h
+++ b/src/jit/gentree.h
@@ -2848,9 +2848,16 @@ struct GenTreeBox : public GenTreeUnOp
     // This is the statement that contains the assignment tree when the node is an inlined GT_BOX on a value
     // type
     GenTreePtr gtAsgStmtWhenInlinedBoxValue;
-
-    GenTreeBox(var_types type, GenTreePtr boxOp, GenTreePtr asgStmtWhenInlinedBoxValue)
-        : GenTreeUnOp(GT_BOX, type, boxOp), gtAsgStmtWhenInlinedBoxValue(asgStmtWhenInlinedBoxValue)
+    // And this is the statement that copies from the value being boxed to the box payload
+    GenTreePtr gtCopyStmtWhenInlinedBoxValue;
+
+    GenTreeBox(var_types  type,
+               GenTreePtr boxOp,
+               GenTreePtr asgStmtWhenInlinedBoxValue,
+               GenTreePtr copyStmtWhenInlinedBoxValue)
+        : GenTreeUnOp(GT_BOX, type, boxOp)
+        , gtAsgStmtWhenInlinedBoxValue(asgStmtWhenInlinedBoxValue)
+        , gtCopyStmtWhenInlinedBoxValue(copyStmtWhenInlinedBoxValue)
     {
     }
 #if DEBUGGABLE_GENTREE
diff --git a/src/jit/importer.cpp b/src/jit/importer.cpp
index 62f1c13..b4ee1d0 100644
--- a/src/jit/importer.cpp
+++ b/src/jit/importer.cpp
@@ -5225,7 +5225,7 @@ void Compiler::impImportAndPushBox(CORINFO_RESOLVED_TOKEN* pResolvedToken)
                                       gtNewArgList(op2));
         }
 
-        /* Remember that this basic block contains 'new' of an array */
+        /* Remember that this basic block contains 'new' of an object */
         compCurBB->bbFlags |= BBF_HAS_NEWOBJ;
 
         GenTreePtr asg = gtNewTempAssign(impBoxTemp, op1);
@@ -5267,11 +5267,16 @@ void Compiler::impImportAndPushBox(CORINFO_RESOLVED_TOKEN* pResolvedToken)
             op1 = gtNewAssignNode(gtNewOperNode(GT_IND, lclTyp, op1), exprToBox);
         }
 
-        op2 = gtNewLclvNode(impBoxTemp, TYP_REF);
-        op1 = gtNewOperNode(GT_COMMA, TYP_REF, op1, op2);
+        // Spill eval stack to flush out any pending side effects.
+        impSpillSideEffects(true, (unsigned)CHECK_SPILL_ALL DEBUGARG("impImportAndPushBox"));
 
-        // Record that this is a "box" node.
-        op1 = new (this, GT_BOX) GenTreeBox(TYP_REF, op1, asgStmt);
+        // Set up this copy as a second assignment.
+        GenTreePtr copyStmt = impAppendTree(op1, (unsigned)CHECK_SPILL_NONE, impCurStmtOffs);
+
+        op1 = gtNewLclvNode(impBoxTemp, TYP_REF);
+
+        // Record that this is a "box" node and keep track of the matching parts.
+        op1 = new (this, GT_BOX) GenTreeBox(TYP_REF, op1, asgStmt, copyStmt);
 
         // If it is a value class, mark the "box" node.  We can use this information
         // to optimise several cases:
diff --git a/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_1.cs b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_1.cs
new file mode 100644
index 0000000..c436802
--- /dev/null
+++ b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_1.cs
@@ -0,0 +1,51 @@
+// 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.
+
+using System;
+
+public struct S<K>
+{
+    public int x;
+    public int y;
+    public K val;
+}
+
+public class X<K,V> 
+{
+    public X(K k)
+    {
+        a = new S<K>[2];
+        a[1].val = k;
+        a[1].x = 3;
+        a[1].y = 4;
+    }
+
+    public void Assert(bool b)
+    {
+        if (!b) throw new Exception("bad!");
+    }
+
+    public int Test()
+    {
+        int r = 0;
+        for (int i = 0; i < a.Length; i++)
+        {
+            Assert(a[i].val != null);
+            r += a[i].val.GetHashCode();
+        }
+        return r;
+    }
+
+    S<K>[] a;
+}
+
+class B
+{
+    public static int Main()
+    {
+        var a = new X<int, string>(11);
+        int z = a.Test();
+        return (z == 11 ? 100 : 0);
+    }
+}
diff --git a/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_1.csproj b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_1.csproj
new file mode 100644
index 0000000..5cd573d
--- /dev/null
+++ b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_1.csproj
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <AssemblyName>$(MSBuildProjectName)</AssemblyName>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{7B521917-193E-48BB-86C6-FE013F3DFF35}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <FileAlignment>512</FileAlignment>
+    <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+    <ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages</ReferencePath>
+    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
+
+    <NuGetPackageImportStamp>7a9bfb7d</NuGetPackageImportStamp>
+  </PropertyGroup>
+  <!-- Default configurations to help VS understand the configurations -->
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+  </PropertyGroup>
+  <ItemGroup>
+    <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+      <Visible>False</Visible>
+    </CodeAnalysisDependentAssemblyPaths>
+  </ItemGroup>
+  <PropertyGroup>
+    <DebugType></DebugType>
+    <Optimize>True</Optimize>
+    <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
+  </PropertyGroup>
+  <ItemGroup>
+    <Compile Include="$(MSBuildProjectName).cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
+  </ItemGroup>
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+  <PropertyGroup Condition=" '$(MsBuildProjectDirOverride)' != '' ">
+  </PropertyGroup>
+</Project>
diff --git a/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_2.cs b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_2.cs
new file mode 100644
index 0000000..bbdc67d
--- /dev/null
+++ b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_2.cs
@@ -0,0 +1,43 @@
+// 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.
+
+using System;
+using System.Runtime.CompilerServices;
+
+public class X<K> 
+{
+    public X(K k1)
+    {
+        k = k1;
+    }
+
+    [MethodImpl(MethodImplOptions.NoInlining)]
+    public K Get()
+    {
+        Console.WriteLine("Get called");
+        count++;
+        return k;
+    }
+
+    public bool Test()
+    {
+        bool x = Get() != null;
+        bool y = (count == 1);
+        return x && y;
+    }
+
+    K k;
+    int count;
+}
+
+class B
+{
+    public static int Main()
+    {
+        var a = new X<int>(11);
+        bool result = a.Test();
+        Console.WriteLine("Passed: {0}", result);
+        return result ? 100 : 0;
+    }
+}
diff --git a/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_2.csproj b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_2.csproj
new file mode 100644
index 0000000..5cd573d
--- /dev/null
+++ b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_2.csproj
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <AssemblyName>$(MSBuildProjectName)</AssemblyName>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{7B521917-193E-48BB-86C6-FE013F3DFF35}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <FileAlignment>512</FileAlignment>
+    <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+    <ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages</ReferencePath>
+    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
+
+    <NuGetPackageImportStamp>7a9bfb7d</NuGetPackageImportStamp>
+  </PropertyGroup>
+  <!-- Default configurations to help VS understand the configurations -->
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+  </PropertyGroup>
+  <ItemGroup>
+    <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+      <Visible>False</Visible>
+    </CodeAnalysisDependentAssemblyPaths>
+  </ItemGroup>
+  <PropertyGroup>
+    <DebugType></DebugType>
+    <Optimize>True</Optimize>
+    <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
+  </PropertyGroup>
+  <ItemGroup>
+    <Compile Include="$(MSBuildProjectName).cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
+  </ItemGroup>
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+  <PropertyGroup Condition=" '$(MsBuildProjectDirOverride)' != '' ">
+  </PropertyGroup>
+</Project>
diff --git a/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_3.cs b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_3.cs
new file mode 100644
index 0000000..b7247c1
--- /dev/null
+++ b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_3.cs
@@ -0,0 +1,41 @@
+// 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.
+
+using System;
+using System.Runtime.CompilerServices;
+
+public class X<K> 
+{
+    public X(K k1)
+    {
+        k = k1;
+    }
+
+    [MethodImpl(MethodImplOptions.NoInlining)]
+    public static bool Test(X<K> a)
+    {
+        return (a.k != null);
+    }
+
+    public K k;
+}
+
+class B
+{
+    public static int Main()
+    {
+        X<int> a = null;
+        bool result = false;
+        try 
+        {
+            X<int>.Test(a);
+        }
+        catch (Exception)
+        {
+            result = true;
+        }
+        Console.WriteLine("Passed: {0}", result);
+        return result ? 100 : 0;
+    }
+}
diff --git a/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_3.csproj b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_3.csproj
new file mode 100644
index 0000000..5cd573d
--- /dev/null
+++ b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_3.csproj
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <AssemblyName>$(MSBuildProjectName)</AssemblyName>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{7B521917-193E-48BB-86C6-FE013F3DFF35}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <FileAlignment>512</FileAlignment>
+    <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+    <ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages</ReferencePath>
+    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
+
+    <NuGetPackageImportStamp>7a9bfb7d</NuGetPackageImportStamp>
+  </PropertyGroup>
+  <!-- Default configurations to help VS understand the configurations -->
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+  </PropertyGroup>
+  <ItemGroup>
+    <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+      <Visible>False</Visible>
+    </CodeAnalysisDependentAssemblyPaths>
+  </ItemGroup>
+  <PropertyGroup>
+    <DebugType></DebugType>
+    <Optimize>True</Optimize>
+    <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
+  </PropertyGroup>
+  <ItemGroup>
+    <Compile Include="$(MSBuildProjectName).cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
+  </ItemGroup>
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+  <PropertyGroup Condition=" '$(MsBuildProjectDirOverride)' != '' ">
+  </PropertyGroup>
+</Project>
diff --git a/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_4.cs b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_4.cs
new file mode 100644
index 0000000..126cdb9
--- /dev/null
+++ b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_4.cs
@@ -0,0 +1,41 @@
+// 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.
+
+using System;
+using System.Runtime.CompilerServices;
+
+public class X<K> 
+{
+    public X(K k1)
+    {
+        k = k1;
+    }
+
+    [MethodImpl(MethodImplOptions.NoInlining)]
+    public static bool Test(X<K> a)
+    {
+        return (a.k == null);
+    }
+
+    public K k;
+}
+
+class B
+{
+    public static int Main()
+    {
+        X<int> a = null;
+        bool result = false;
+        try 
+        {
+            X<int>.Test(a);
+        }
+        catch (Exception)
+        {
+            result = true;
+        }
+        Console.WriteLine("Passed: {0}", result);
+        return result ? 100 : 0;
+    }
+}
diff --git a/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_4.csproj b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_4.csproj
new file mode 100644
index 0000000..5cd573d
--- /dev/null
+++ b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_4.csproj
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <AssemblyName>$(MSBuildProjectName)</AssemblyName>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{7B521917-193E-48BB-86C6-FE013F3DFF35}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <FileAlignment>512</FileAlignment>
+    <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+    <ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages</ReferencePath>
+    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
+
+    <NuGetPackageImportStamp>7a9bfb7d</NuGetPackageImportStamp>
+  </PropertyGroup>
+  <!-- Default configurations to help VS understand the configurations -->
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+  </PropertyGroup>
+  <ItemGroup>
+    <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+      <Visible>False</Visible>
+    </CodeAnalysisDependentAssemblyPaths>
+  </ItemGroup>
+  <PropertyGroup>
+    <DebugType></DebugType>
+    <Optimize>True</Optimize>
+    <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
+  </PropertyGroup>
+  <ItemGroup>
+    <Compile Include="$(MSBuildProjectName).cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
+  </ItemGroup>
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+  <PropertyGroup Condition=" '$(MsBuildProjectDirOverride)' != '' ">
+  </PropertyGroup>
+</Project>
diff --git a/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_5.cs b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_5.cs
new file mode 100644
index 0000000..95a8945
--- /dev/null
+++ b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_5.cs
@@ -0,0 +1,46 @@
+// 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.
+
+using System;
+using System.Runtime.CompilerServices;
+
+struct R
+{
+    int a;
+}
+
+public class X<K> 
+{
+    public X(K k1)
+    {
+        k = k1;
+    }
+
+    [MethodImpl(MethodImplOptions.NoInlining)]
+    public static bool Test(X<K> a)
+    {
+        return (a.k != null);
+    }
+
+    public K k;
+}
+
+class B
+{
+    public static int Main()
+    {
+        X<R> a = null;
+        bool result = false;
+        try 
+        {
+            X<R>.Test(a);
+        }
+        catch (Exception)
+        {
+            result = true;
+        }
+        Console.WriteLine("Passed: {0}", result);
+        return result ? 100 : 0;
+    }
+}
diff --git a/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_5.csproj b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_5.csproj
new file mode 100644
index 0000000..5cd573d
--- /dev/null
+++ b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_5.csproj
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <AssemblyName>$(MSBuildProjectName)</AssemblyName>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{7B521917-193E-48BB-86C6-FE013F3DFF35}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <FileAlignment>512</FileAlignment>
+    <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+    <ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages</ReferencePath>
+    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
+
+    <NuGetPackageImportStamp>7a9bfb7d</NuGetPackageImportStamp>
+  </PropertyGroup>
+  <!-- Default configurations to help VS understand the configurations -->
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+  </PropertyGroup>
+  <ItemGroup>
+    <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+      <Visible>False</Visible>
+    </CodeAnalysisDependentAssemblyPaths>
+  </ItemGroup>
+  <PropertyGroup>
+    <DebugType></DebugType>
+    <Optimize>True</Optimize>
+    <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
+  </PropertyGroup>
+  <ItemGroup>
+    <Compile Include="$(MSBuildProjectName).cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
+  </ItemGroup>
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+  <PropertyGroup Condition=" '$(MsBuildProjectDirOverride)' != '' ">
+  </PropertyGroup>
+</Project>
diff --git a/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_6.cs b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_6.cs
new file mode 100644
index 0000000..1371dbb
--- /dev/null
+++ b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_6.cs
@@ -0,0 +1,60 @@
+// 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.
+
+using System;
+using System.Runtime.CompilerServices;
+
+public interface IGet 
+{
+    int Get();
+}
+
+public struct R : IGet
+{
+    public double d;
+    public int a;
+
+    public int Get() { return a; }
+}
+
+public class X<K> where K: IGet
+{
+    public X(K r)
+    {
+        a = new K[2];
+        a[0] = r;
+    }
+
+    public void Assert(bool b)
+    {
+        if (!b) throw new Exception("bad!");
+    }
+
+    public int Test()
+    {
+        int r = 0;
+        for (int i = 0; i < a.Length; i++)
+        {
+            Assert(a[i] != null);
+            r += a[i].Get();
+        }
+        return r;
+    }
+
+    K[] a;
+}
+
+class B
+{
+    public static int Main()
+    {
+        var r = new R();
+        r.a = 3;
+        var a = new X<R>(r);
+        int result = a.Test();
+        bool passed = result == 3;
+        Console.WriteLine("Passed: {0}", passed);
+        return passed ? 100 : 0;
+    }
+}
diff --git a/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_6.csproj b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_6.csproj
new file mode 100644
index 0000000..5cd573d
--- /dev/null
+++ b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_6.csproj
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <AssemblyName>$(MSBuildProjectName)</AssemblyName>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{7B521917-193E-48BB-86C6-FE013F3DFF35}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <FileAlignment>512</FileAlignment>
+    <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+    <ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages</ReferencePath>
+    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
+
+    <NuGetPackageImportStamp>7a9bfb7d</NuGetPackageImportStamp>
+  </PropertyGroup>
+  <!-- Default configurations to help VS understand the configurations -->
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+  </PropertyGroup>
+  <ItemGroup>
+    <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+      <Visible>False</Visible>
+    </CodeAnalysisDependentAssemblyPaths>
+  </ItemGroup>
+  <PropertyGroup>
+    <DebugType></DebugType>
+    <Optimize>True</Optimize>
+    <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
+  </PropertyGroup>
+  <ItemGroup>
+    <Compile Include="$(MSBuildProjectName).cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
+  </ItemGroup>
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+  <PropertyGroup Condition=" '$(MsBuildProjectDirOverride)' != '' ">
+  </PropertyGroup>
+</Project>
diff --git a/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_7.cs b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_7.cs
new file mode 100644
index 0000000..3ba6cd2
--- /dev/null
+++ b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_7.cs
@@ -0,0 +1,58 @@
+// 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.
+
+using System;
+
+public struct V
+{
+    public V(int x)
+    {
+        Token = x;
+    }
+
+    public int Token;
+}
+
+class M
+{
+    static int F(int x, object a)
+    {
+        int result = 0;
+ 
+        if (a is V)
+        {
+            int token = ((V)a).Token;
+            Console.WriteLine("F: Token is {0}", token);
+            result = x + token;
+        }
+
+        return result;
+    }
+
+    static int G(object a, int x)
+    {
+        return F(x, a);
+    }
+
+    static int Trouble(ref V v)
+    {
+        Console.WriteLine("T: Token is {0}", v.Token);
+        int result = v.Token;
+        v.Token++;
+        return result;
+    }
+
+    public static int Main()
+    {
+        // Ensure we get right order of side effects from boxes
+        // now that we are splitting them into multiple statments.
+        V v1 = new V(11);
+        int result1 = F(Trouble(ref v1), v1);
+        V v2 = new V(11);
+        int result2 = G(v2, Trouble(ref v2));
+        Console.WriteLine("Result1 = {0}; Result2 = {1}", result1, result2);
+        return result1 + result2 + 55;
+    }
+}
+
diff --git a/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_7.csproj b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_7.csproj
new file mode 100644
index 0000000..5cd573d
--- /dev/null
+++ b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_7.csproj
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <AssemblyName>$(MSBuildProjectName)</AssemblyName>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{7B521917-193E-48BB-86C6-FE013F3DFF35}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <FileAlignment>512</FileAlignment>
+    <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+    <ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages</ReferencePath>
+    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
+
+    <NuGetPackageImportStamp>7a9bfb7d</NuGetPackageImportStamp>
+  </PropertyGroup>
+  <!-- Default configurations to help VS understand the configurations -->
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+  </PropertyGroup>
+  <ItemGroup>
+    <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+      <Visible>False</Visible>
+    </CodeAnalysisDependentAssemblyPaths>
+  </ItemGroup>
+  <PropertyGroup>
+    <DebugType></DebugType>
+    <Optimize>True</Optimize>
+    <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
+  </PropertyGroup>
+  <ItemGroup>
+    <Compile Include="$(MSBuildProjectName).cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
+  </ItemGroup>
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+  <PropertyGroup Condition=" '$(MsBuildProjectDirOverride)' != '' ">
+  </PropertyGroup>
+</Project>
diff --git a/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_8.cs b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_8.cs
new file mode 100644
index 0000000..d5daa43
--- /dev/null
+++ b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_8.cs
@@ -0,0 +1,42 @@
+// 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.
+
+using System;
+using System.Runtime.CompilerServices;
+using System.Numerics;
+
+public class X<K> 
+{
+    public X(K k1)
+    {
+        k = k1;
+    }
+
+    [MethodImpl(MethodImplOptions.NoInlining)]
+    public static bool Test(X<K> a)
+    {
+        return (a.k != null);
+    }
+
+    public K k;
+}
+
+class B
+{
+    public static int Main()
+    {
+        X<Vector3> a = null;
+        bool result = false;
+        try 
+        {
+            X<Vector3>.Test(a);
+        }
+        catch (Exception)
+        {
+            result = true;
+        }
+        Console.WriteLine("Passed: {0}", result);
+        return result ? 100 : 0;
+    }
+}
diff --git a/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_8.csproj b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_8.csproj
new file mode 100644
index 0000000..5cd573d
--- /dev/null
+++ b/tests/src/JIT/Regression/JitBlue/GitHub_12949/GitHub_12949_8.csproj
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <AssemblyName>$(MSBuildProjectName)</AssemblyName>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{7B521917-193E-48BB-86C6-FE013F3DFF35}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <FileAlignment>512</FileAlignment>
+    <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+    <ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages</ReferencePath>
+    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
+
+    <NuGetPackageImportStamp>7a9bfb7d</NuGetPackageImportStamp>
+  </PropertyGroup>
+  <!-- Default configurations to help VS understand the configurations -->
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+  </PropertyGroup>
+  <ItemGroup>
+    <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+      <Visible>False</Visible>
+    </CodeAnalysisDependentAssemblyPaths>
+  </ItemGroup>
+  <PropertyGroup>
+    <DebugType></DebugType>
+    <Optimize>True</Optimize>
+    <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
+  </PropertyGroup>
+  <ItemGroup>
+    <Compile Include="$(MSBuildProjectName).cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
+  </ItemGroup>
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+  <PropertyGroup Condition=" '$(MsBuildProjectDirOverride)' != '' ">
+  </PropertyGroup>
+</Project>
-- 
2.7.4