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
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
|
// 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 DecomposeLongs XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
//
// This file contains code to decompose 64-bit LONG operations on 32-bit platforms
// into multiple single-register operations so individual register usage and requirements
// are explicit for LSRA. The rationale behind this is to avoid adding code complexity
// downstream caused by the introduction of handling longs as special cases,
// especially in LSRA.
//
// Long decomposition happens on a statement immediately prior to more general
// purpose lowering.
//
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
#ifndef LEGACY_BACKEND // This file is ONLY used for the RyuJIT backend that uses the linear scan register allocator
#ifndef _TARGET_64BIT_ // DecomposeLongs is only used on 32-bit platforms
#include "decomposelongs.h"
//------------------------------------------------------------------------
// DecomposeLongs::PrepareForDecomposition:
// Do one-time preparation required for LONG decomposition. Namely,
// promote long variables to multi-register structs.
//
// Arguments:
// None
//
// Return Value:
// None.
//
void DecomposeLongs::PrepareForDecomposition()
{
m_compiler->lvaPromoteLongVars();
}
//------------------------------------------------------------------------
// DecomposeLongs::DecomposeBlock:
// Do LONG decomposition on all the nodes in the given block. This must
// be done before lowering the block, as decomposition can insert
// additional nodes.
//
// Arguments:
// block - the block to process
//
// Return Value:
// None.
//
void DecomposeLongs::DecomposeBlock(BasicBlock* block)
{
assert(block == m_compiler->compCurBB); // compCurBB must already be set.
assert(block->isEmpty() || block->IsLIR());
m_blockWeight = block->getBBWeight(m_compiler);
m_range = &LIR::AsRange(block);
DecomposeRangeHelper();
}
//------------------------------------------------------------------------
// DecomposeLongs::DecomposeRange:
// Do LONG decomposition on all the nodes in the given range. This must
// be done before inserting a range of un-decomposed IR into a block
// that has already been decomposed.
//
// Arguments:
// compiler - The compiler context.
// blockWeight - The weight of the block into which the range will be
// inserted.
// range - The range to decompose.
//
// Return Value:
// None.
//
void DecomposeLongs::DecomposeRange(Compiler* compiler, unsigned blockWeight, LIR::Range& range)
{
assert(compiler != nullptr);
DecomposeLongs decomposer(compiler);
decomposer.m_blockWeight = blockWeight;
decomposer.m_range = ⦥
decomposer.DecomposeRangeHelper();
}
//------------------------------------------------------------------------
// DecomposeLongs::DecomposeRangeHelper:
// Decompiose each node in the current range.
//
// Decomposition is done as an execution-order walk. Decomposition of
// a particular node can create new nodes that need to be further
// decomposed at higher levels. That is, decomposition "bubbles up"
// through dataflow.
//
void DecomposeLongs::DecomposeRangeHelper()
{
assert(m_range != nullptr);
GenTree* node = Range().FirstNonPhiNode();
while (node != nullptr)
{
node = DecomposeNode(node);
}
assert(Range().CheckLIR(m_compiler, true));
}
//------------------------------------------------------------------------
// DecomposeNode: Decompose long-type trees into lower and upper halves.
//
// Arguments:
// use - the LIR::Use object for the def that needs to be decomposed.
//
// Return Value:
// The next node to process.
//
GenTree* DecomposeLongs::DecomposeNode(GenTree* tree)
{
// Handle the case where we are implicitly using the lower half of a long lclVar.
if ((tree->TypeGet() == TYP_INT) && tree->OperIsLocal())
{
LclVarDsc* varDsc = m_compiler->lvaTable + tree->AsLclVarCommon()->gtLclNum;
if (varTypeIsLong(varDsc) && varDsc->lvPromoted)
{
#ifdef DEBUG
if (m_compiler->verbose)
{
printf("Changing implicit reference to lo half of long lclVar to an explicit reference of its promoted "
"half:\n");
m_compiler->gtDispTreeRange(Range(), tree);
}
#endif // DEBUG
m_compiler->lvaDecRefCnts(tree);
unsigned loVarNum = varDsc->lvFieldLclStart;
tree->AsLclVarCommon()->SetLclNum(loVarNum);
m_compiler->lvaIncRefCnts(tree);
return tree->gtNext;
}
}
if (tree->TypeGet() != TYP_LONG)
{
return tree->gtNext;
}
#ifdef DEBUG
if (m_compiler->verbose)
{
printf("Decomposing TYP_LONG tree. BEFORE:\n");
m_compiler->gtDispTreeRange(Range(), tree);
}
#endif // DEBUG
LIR::Use use;
if (!Range().TryGetUse(tree, &use))
{
use = LIR::Use::GetDummyUse(Range(), tree);
}
GenTree* nextNode = nullptr;
switch (tree->OperGet())
{
case GT_LCL_VAR:
nextNode = DecomposeLclVar(use);
break;
case GT_LCL_FLD:
nextNode = DecomposeLclFld(use);
break;
case GT_STORE_LCL_VAR:
nextNode = DecomposeStoreLclVar(use);
break;
case GT_CAST:
nextNode = DecomposeCast(use);
break;
case GT_CNS_LNG:
nextNode = DecomposeCnsLng(use);
break;
case GT_CALL:
nextNode = DecomposeCall(use);
break;
case GT_RETURN:
assert(tree->gtOp.gtOp1->OperGet() == GT_LONG);
break;
case GT_STOREIND:
nextNode = DecomposeStoreInd(use);
break;
case GT_STORE_LCL_FLD:
nextNode = DecomposeStoreLclFld(use);
break;
case GT_IND:
nextNode = DecomposeInd(use);
break;
case GT_NOT:
nextNode = DecomposeNot(use);
break;
case GT_NEG:
nextNode = DecomposeNeg(use);
break;
// Binary operators. Those that require different computation for upper and lower half are
// handled by the use of GetHiOper().
case GT_ADD:
case GT_SUB:
case GT_OR:
case GT_XOR:
case GT_AND:
nextNode = DecomposeArith(use);
break;
case GT_MUL:
nextNode = DecomposeMul(use);
break;
case GT_UMOD:
nextNode = DecomposeUMod(use);
break;
case GT_LSH:
case GT_RSH:
case GT_RSZ:
nextNode = DecomposeShift(use);
break;
case GT_ROL:
case GT_ROR:
nextNode = DecomposeRotate(use);
break;
#ifdef FEATURE_SIMD
case GT_SIMD:
nextNode = DecomposeSimd(use);
break;
#endif // FEATURE_SIMD
case GT_LOCKADD:
case GT_XADD:
case GT_XCHG:
case GT_CMPXCHG:
NYI("Interlocked operations on TYP_LONG");
break;
default:
{
JITDUMP("Illegal TYP_LONG node %s in Decomposition.", GenTree::OpName(tree->OperGet()));
assert(!"Illegal TYP_LONG node in Decomposition.");
break;
}
}
// If we replaced the argument to a GT_FIELD_LIST element with a GT_LONG node, split that field list
// element into two elements: one for each half of the GT_LONG.
if ((use.Def()->OperGet() == GT_LONG) && !use.IsDummyUse() && (use.User()->OperGet() == GT_FIELD_LIST))
{
GenTreeOp* value = use.Def()->AsOp();
Range().Remove(value);
// The node returned by `use.User()` is the head of the field list. We need to find the actual node that uses
// the `GT_LONG` so that we can split it.
GenTreeFieldList* listNode = use.User()->AsFieldList();
for (; listNode != nullptr; listNode = listNode->Rest())
{
if (listNode->Current() == value)
{
break;
}
}
assert(listNode != nullptr);
GenTree* rest = listNode->gtOp2;
GenTreeFieldList* loNode = listNode;
loNode->gtOp1 = value->gtOp1;
loNode->gtFieldType = TYP_INT;
GenTreeFieldList* hiNode =
new (m_compiler, GT_FIELD_LIST) GenTreeFieldList(value->gtOp2, loNode->gtFieldOffset + 4, TYP_INT, loNode);
hiNode->gtOp2 = rest;
}
#ifdef DEBUG
if (m_compiler->verbose)
{
// NOTE: st_lcl_var doesn't dump properly afterwards.
printf("Decomposing TYP_LONG tree. AFTER:\n");
m_compiler->gtDispTreeRange(Range(), use.Def());
}
#endif
return nextNode;
}
//------------------------------------------------------------------------
// FinalizeDecomposition: A helper function to finalize LONG decomposition by
// taking the resulting two halves of the decomposition, and tie them together
// with a new GT_LONG node that will replace the original node.
//
// Arguments:
// use - the LIR::Use object for the def that needs to be decomposed.
// loResult - the decomposed low part
// hiResult - the decomposed high part
// insertResultAfter - the node that the GT_LONG should be inserted after
//
// Return Value:
// The next node to process.
//
GenTree* DecomposeLongs::FinalizeDecomposition(LIR::Use& use,
GenTree* loResult,
GenTree* hiResult,
GenTree* insertResultAfter)
{
assert(use.IsInitialized());
assert(loResult != nullptr);
assert(hiResult != nullptr);
assert(Range().Contains(loResult));
assert(Range().Contains(hiResult));
GenTree* gtLong = new (m_compiler, GT_LONG) GenTreeOp(GT_LONG, TYP_LONG, loResult, hiResult);
if (use.IsDummyUse())
{
gtLong->SetUnusedValue();
}
loResult->ClearUnusedValue();
hiResult->ClearUnusedValue();
Range().InsertAfter(insertResultAfter, gtLong);
use.ReplaceWith(m_compiler, gtLong);
return gtLong->gtNext;
}
//------------------------------------------------------------------------
// DecomposeLclVar: Decompose GT_LCL_VAR.
//
// Arguments:
// use - the LIR::Use object for the def that needs to be decomposed.
//
// Return Value:
// The next node to process.
//
GenTree* DecomposeLongs::DecomposeLclVar(LIR::Use& use)
{
assert(use.IsInitialized());
assert(use.Def()->OperGet() == GT_LCL_VAR);
GenTree* tree = use.Def();
unsigned varNum = tree->AsLclVarCommon()->gtLclNum;
LclVarDsc* varDsc = m_compiler->lvaTable + varNum;
m_compiler->lvaDecRefCnts(tree);
GenTree* loResult = tree;
loResult->gtType = TYP_INT;
GenTree* hiResult = m_compiler->gtNewLclLNode(varNum, TYP_INT);
Range().InsertAfter(loResult, hiResult);
if (varDsc->lvPromoted)
{
assert(varDsc->lvFieldCnt == 2);
unsigned loVarNum = varDsc->lvFieldLclStart;
unsigned hiVarNum = loVarNum + 1;
loResult->AsLclVarCommon()->SetLclNum(loVarNum);
hiResult->AsLclVarCommon()->SetLclNum(hiVarNum);
}
else
{
loResult->SetOper(GT_LCL_FLD);
loResult->AsLclFld()->gtLclOffs = 0;
loResult->AsLclFld()->gtFieldSeq = FieldSeqStore::NotAField();
hiResult->SetOper(GT_LCL_FLD);
hiResult->AsLclFld()->gtLclOffs = 4;
hiResult->AsLclFld()->gtFieldSeq = FieldSeqStore::NotAField();
}
m_compiler->lvaIncRefCnts(loResult);
m_compiler->lvaIncRefCnts(hiResult);
return FinalizeDecomposition(use, loResult, hiResult, hiResult);
}
//------------------------------------------------------------------------
// DecomposeLclFld: Decompose GT_LCL_FLD.
//
// Arguments:
// use - the LIR::Use object for the def that needs to be decomposed.
//
// Return Value:
// The next node to process.
//
GenTree* DecomposeLongs::DecomposeLclFld(LIR::Use& use)
{
assert(use.IsInitialized());
assert(use.Def()->OperGet() == GT_LCL_FLD);
GenTree* tree = use.Def();
GenTreeLclFld* loResult = tree->AsLclFld();
loResult->gtType = TYP_INT;
GenTree* hiResult = m_compiler->gtNewLclFldNode(loResult->gtLclNum, TYP_INT, loResult->gtLclOffs + 4);
Range().InsertAfter(loResult, hiResult);
m_compiler->lvaIncRefCnts(hiResult);
return FinalizeDecomposition(use, loResult, hiResult, hiResult);
}
//------------------------------------------------------------------------
// DecomposeStoreLclVar: Decompose GT_STORE_LCL_VAR.
//
// Arguments:
// use - the LIR::Use object for the def that needs to be decomposed.
//
// Return Value:
// The next node to process.
//
GenTree* DecomposeLongs::DecomposeStoreLclVar(LIR::Use& use)
{
assert(use.IsInitialized());
assert(use.Def()->OperGet() == GT_STORE_LCL_VAR);
GenTree* tree = use.Def();
GenTree* rhs = tree->gtGetOp1();
if ((rhs->OperGet() == GT_PHI) || (rhs->OperGet() == GT_CALL) ||
((rhs->OperGet() == GT_MUL_LONG) && (rhs->gtFlags & GTF_MUL_64RSLT) != 0))
{
// GT_CALLs are not decomposed, so will not be converted to GT_LONG
// GT_STORE_LCL_VAR = GT_CALL are handled in genMultiRegCallStoreToLocal
// GT_MULs are not decomposed, so will not be converted to GT_LONG
return tree->gtNext;
}
noway_assert(rhs->OperGet() == GT_LONG);
unsigned varNum = tree->AsLclVarCommon()->gtLclNum;
LclVarDsc* varDsc = m_compiler->lvaTable + varNum;
if (!varDsc->lvPromoted)
{
// We cannot decompose a st.lclVar that is not promoted because doing so
// changes its liveness semantics. For example, consider the following
// decomposition of a st.lclVar into two st.lclFlds:
//
// Before:
//
// /--* t0 int
// +--* t1 int
// t2 = * gt_long long
//
// /--* t2 long
// * st.lclVar long V0
//
// After:
// /--* t0 int
// * st.lclFld int V0 [+0]
//
// /--* t1 int
// * st.lclFld int V0 [+4]
//
// Before decomposition, the `st.lclVar` is a simple def of `V0`. After
// decomposition, each `st.lclFld` is a partial def of `V0`. This partial
// def is treated as both a use and a def of the appropriate lclVar. This
// difference will affect any situation in which the liveness of a variable
// at a def matters (e.g. dead store elimination, live-in sets, etc.). As
// a result, we leave these stores as-is and generate the decomposed store
// in the code generator.
//
// NOTE: this does extend the lifetime of the low half of the `GT_LONG`
// node as compared to the decomposed form. If we start doing more code
// motion in the backend, this may cause some CQ issues and some sort of
// decomposition could be beneficial.
return tree->gtNext;
}
assert(varDsc->lvFieldCnt == 2);
m_compiler->lvaDecRefCnts(tree);
GenTreeOp* value = rhs->AsOp();
Range().Remove(value);
const unsigned loVarNum = varDsc->lvFieldLclStart;
GenTree* loStore = tree;
loStore->AsLclVarCommon()->SetLclNum(loVarNum);
loStore->gtOp.gtOp1 = value->gtOp1;
loStore->gtType = TYP_INT;
const unsigned hiVarNum = loVarNum + 1;
GenTree* hiStore = m_compiler->gtNewLclLNode(hiVarNum, TYP_INT);
hiStore->SetOper(GT_STORE_LCL_VAR);
hiStore->gtOp.gtOp1 = value->gtOp2;
hiStore->gtFlags |= GTF_VAR_DEF;
m_compiler->lvaIncRefCnts(loStore);
m_compiler->lvaIncRefCnts(hiStore);
Range().InsertAfter(tree, hiStore);
return hiStore->gtNext;
}
//------------------------------------------------------------------------
// DecomposeStoreLclFld: Decompose GT_STORE_LCL_FLD.
//
// Arguments:
// use - the LIR::Use object for the def that needs to be decomposed.
//
// Return Value:
// The next node to process.
//
GenTree* DecomposeLongs::DecomposeStoreLclFld(LIR::Use& use)
{
assert(use.IsInitialized());
assert(use.Def()->OperGet() == GT_STORE_LCL_FLD);
GenTreeLclFld* store = use.Def()->AsLclFld();
GenTreeOp* value = store->gtOp1->AsOp();
assert(value->OperGet() == GT_LONG);
Range().Remove(value);
// The original store node will be repurposed to store the low half of the GT_LONG.
GenTreeLclFld* loStore = store;
loStore->gtOp1 = value->gtOp1;
loStore->gtType = TYP_INT;
loStore->gtFlags |= GTF_VAR_USEASG;
// Create the store for the upper half of the GT_LONG and insert it after the low store.
GenTreeLclFld* hiStore = m_compiler->gtNewLclFldNode(loStore->gtLclNum, TYP_INT, loStore->gtLclOffs + 4);
hiStore->SetOper(GT_STORE_LCL_FLD);
hiStore->gtOp1 = value->gtOp2;
hiStore->gtFlags |= (GTF_VAR_DEF | GTF_VAR_USEASG);
// Bump the ref count for the destination.
m_compiler->lvaIncRefCnts(hiStore);
Range().InsertAfter(loStore, hiStore);
return hiStore->gtNext;
}
//------------------------------------------------------------------------
// DecomposeCast: Decompose GT_CAST.
//
// Arguments:
// use - the LIR::Use object for the def that needs to be decomposed.
//
// Return Value:
// The next node to process.
//
GenTree* DecomposeLongs::DecomposeCast(LIR::Use& use)
{
assert(use.IsInitialized());
assert(use.Def()->OperGet() == GT_CAST);
GenTree* cast = use.Def()->AsCast();
GenTree* loResult = nullptr;
GenTree* hiResult = nullptr;
var_types srcType = cast->CastFromType();
var_types dstType = cast->CastToType();
if ((cast->gtFlags & GTF_UNSIGNED) != 0)
{
srcType = genUnsignedType(srcType);
}
bool skipDecomposition = false;
if (varTypeIsLong(srcType))
{
if (cast->gtOverflow() && (varTypeIsUnsigned(srcType) != varTypeIsUnsigned(dstType)))
{
GenTree* srcOp = cast->gtGetOp1();
noway_assert(srcOp->OperGet() == GT_LONG);
GenTree* loSrcOp = srcOp->gtGetOp1();
GenTree* hiSrcOp = srcOp->gtGetOp2();
//
// When casting between long types an overflow check is needed only if the types
// have different signedness. In both cases (long->ulong and ulong->long) we only
// need to check if the high part is negative or not. Use the existing cast node
// to perform a int->uint cast of the high part to take advantage of the overflow
// check provided by codegen.
//
const bool signExtend = (cast->gtFlags & GTF_UNSIGNED) == 0;
loResult = EnsureIntSized(loSrcOp, signExtend);
hiResult = cast;
hiResult->gtType = TYP_INT;
hiResult->AsCast()->gtCastType = TYP_UINT;
hiResult->gtFlags &= ~GTF_UNSIGNED;
hiResult->gtOp.gtOp1 = hiSrcOp;
Range().Remove(srcOp);
}
else
{
NYI("Unimplemented long->long no-op cast decomposition");
}
}
else if (varTypeIsIntegralOrI(srcType))
{
if (cast->gtOverflow() && !varTypeIsUnsigned(srcType) && varTypeIsUnsigned(dstType))
{
//
// An overflow check is needed only when casting from a signed type to ulong.
// Change the cast type to uint to take advantage of the overflow check provided
// by codegen and then zero extend the resulting uint to ulong.
//
loResult = cast;
loResult->AsCast()->gtCastType = TYP_UINT;
loResult->gtType = TYP_INT;
hiResult = m_compiler->gtNewZeroConNode(TYP_INT);
Range().InsertAfter(loResult, hiResult);
}
else
{
if (!use.IsDummyUse() && (use.User()->OperGet() == GT_MUL))
{
//
// This int->long cast is used by a GT_MUL that will be transformed by DecomposeMul into a
// GT_LONG_MUL and as a result the high operand produced by the cast will become dead.
// Skip cast decomposition so DecomposeMul doesn't need to bother with dead code removal,
// especially in the case of sign extending casts that also introduce new lclvars.
//
assert((use.User()->gtFlags & GTF_MUL_64RSLT) != 0);
skipDecomposition = true;
}
else if (varTypeIsUnsigned(srcType))
{
const bool signExtend = (cast->gtFlags & GTF_UNSIGNED) == 0;
loResult = EnsureIntSized(cast->gtGetOp1(), signExtend);
hiResult = m_compiler->gtNewZeroConNode(TYP_INT);
Range().InsertAfter(cast, hiResult);
Range().Remove(cast);
}
else
{
LIR::Use src(Range(), &(cast->gtOp.gtOp1), cast);
unsigned lclNum = src.ReplaceWithLclVar(m_compiler, m_blockWeight);
loResult = src.Def();
GenTree* loCopy = m_compiler->gtNewLclvNode(lclNum, TYP_INT);
GenTree* shiftBy = m_compiler->gtNewIconNode(31, TYP_INT);
hiResult = m_compiler->gtNewOperNode(GT_RSH, TYP_INT, loCopy, shiftBy);
Range().InsertAfter(cast, loCopy, shiftBy, hiResult);
m_compiler->lvaIncRefCnts(loCopy);
Range().Remove(cast);
}
}
}
else
{
NYI("Unimplemented cast decomposition");
}
if (skipDecomposition)
{
return cast->gtNext;
}
return FinalizeDecomposition(use, loResult, hiResult, hiResult);
}
//------------------------------------------------------------------------
// DecomposeCnsLng: Decompose GT_CNS_LNG.
//
// Arguments:
// use - the LIR::Use object for the def that needs to be decomposed.
//
// Return Value:
// The next node to process.
//
GenTree* DecomposeLongs::DecomposeCnsLng(LIR::Use& use)
{
assert(use.IsInitialized());
assert(use.Def()->OperGet() == GT_CNS_LNG);
GenTree* tree = use.Def();
INT32 hiVal = tree->AsLngCon()->HiVal();
GenTree* loResult = tree;
loResult->ChangeOperConst(GT_CNS_INT);
loResult->gtType = TYP_INT;
GenTree* hiResult = new (m_compiler, GT_CNS_INT) GenTreeIntCon(TYP_INT, hiVal);
Range().InsertAfter(loResult, hiResult);
return FinalizeDecomposition(use, loResult, hiResult, hiResult);
}
//------------------------------------------------------------------------
// DecomposeCall: Decompose GT_CALL.
//
// Arguments:
// use - the LIR::Use object for the def that needs to be decomposed.
//
// Return Value:
// The next node to process.
//
GenTree* DecomposeLongs::DecomposeCall(LIR::Use& use)
{
assert(use.IsInitialized());
assert(use.Def()->OperGet() == GT_CALL);
// We only need to force var = call() if the call's result is used.
return StoreNodeToVar(use);
}
//------------------------------------------------------------------------
// DecomposeStoreInd: Decompose GT_STOREIND.
//
// Arguments:
// use - the LIR::Use object for the def that needs to be decomposed.
//
// Return Value:
// The next node to process.
//
GenTree* DecomposeLongs::DecomposeStoreInd(LIR::Use& use)
{
assert(use.IsInitialized());
assert(use.Def()->OperGet() == GT_STOREIND);
GenTree* tree = use.Def();
assert(tree->gtOp.gtOp2->OperGet() == GT_LONG);
// Example input (address expression omitted):
//
// t51 = const int 0x37C05E7D
// t154 = const int 0x2A0A3C80
// / --* t51 int
// + --* t154 int
// t155 = *gt_long long
// / --* t52 byref
// + --* t155 long
// * storeIndir long
GenTree* gtLong = tree->gtOp.gtOp2;
// Save address to a temp. It is used in storeIndLow and storeIndHigh trees.
LIR::Use address(Range(), &tree->gtOp.gtOp1, tree);
address.ReplaceWithLclVar(m_compiler, m_blockWeight);
JITDUMP("[DecomposeStoreInd]: Saving address tree to a temp var:\n");
DISPTREERANGE(Range(), address.Def());
if (!gtLong->gtOp.gtOp1->OperIsLeaf())
{
LIR::Use op1(Range(), >Long->gtOp.gtOp1, gtLong);
op1.ReplaceWithLclVar(m_compiler, m_blockWeight);
JITDUMP("[DecomposeStoreInd]: Saving low data tree to a temp var:\n");
DISPTREERANGE(Range(), op1.Def());
}
if (!gtLong->gtOp.gtOp2->OperIsLeaf())
{
LIR::Use op2(Range(), >Long->gtOp.gtOp2, gtLong);
op2.ReplaceWithLclVar(m_compiler, m_blockWeight);
JITDUMP("[DecomposeStoreInd]: Saving high data tree to a temp var:\n");
DISPTREERANGE(Range(), op2.Def());
}
GenTree* addrBase = tree->gtOp.gtOp1;
GenTree* dataHigh = gtLong->gtOp.gtOp2;
GenTree* dataLow = gtLong->gtOp.gtOp1;
GenTree* storeIndLow = tree;
Range().Remove(gtLong);
Range().Remove(dataHigh);
storeIndLow->gtOp.gtOp2 = dataLow;
storeIndLow->gtType = TYP_INT;
GenTree* addrBaseHigh = new (m_compiler, GT_LCL_VAR)
GenTreeLclVar(GT_LCL_VAR, addrBase->TypeGet(), addrBase->AsLclVarCommon()->GetLclNum(), BAD_IL_OFFSET);
GenTree* addrHigh =
new (m_compiler, GT_LEA) GenTreeAddrMode(TYP_REF, addrBaseHigh, nullptr, 0, genTypeSize(TYP_INT));
GenTree* storeIndHigh = new (m_compiler, GT_STOREIND) GenTreeStoreInd(TYP_INT, addrHigh, dataHigh);
storeIndHigh->gtFlags = (storeIndLow->gtFlags & (GTF_ALL_EFFECT | GTF_LIVENESS_MASK));
m_compiler->lvaIncRefCnts(addrBaseHigh);
Range().InsertAfter(storeIndLow, dataHigh, addrBaseHigh, addrHigh, storeIndHigh);
return storeIndHigh;
// Example final output:
//
// /--* t52 byref
// * st.lclVar byref V07 rat0
// t158 = lclVar byref V07 rat0
// t51 = const int 0x37C05E7D
// /--* t158 byref
// +--* t51 int
// * storeIndir int
// t154 = const int 0x2A0A3C80
// t159 = lclVar byref V07 rat0
// /--* t159 byref
// t160 = * lea(b + 4) ref
// /--* t154 int
// +--* t160 ref
// * storeIndir int
}
//------------------------------------------------------------------------
// DecomposeInd: Decompose GT_IND.
//
// Arguments:
// use - the LIR::Use object for the def that needs to be decomposed.
//
// Return Value:
// The next node to process.
//
GenTree* DecomposeLongs::DecomposeInd(LIR::Use& use)
{
GenTree* indLow = use.Def();
LIR::Use address(Range(), &indLow->gtOp.gtOp1, indLow);
address.ReplaceWithLclVar(m_compiler, m_blockWeight);
JITDUMP("[DecomposeInd]: Saving addr tree to a temp var:\n");
DISPTREERANGE(Range(), address.Def());
// Change the type of lower ind.
indLow->gtType = TYP_INT;
// Create tree of ind(addr+4)
GenTreePtr addrBase = indLow->gtGetOp1();
GenTreePtr addrBaseHigh = new (m_compiler, GT_LCL_VAR)
GenTreeLclVar(GT_LCL_VAR, addrBase->TypeGet(), addrBase->AsLclVarCommon()->GetLclNum(), BAD_IL_OFFSET);
GenTreePtr addrHigh =
new (m_compiler, GT_LEA) GenTreeAddrMode(TYP_REF, addrBaseHigh, nullptr, 0, genTypeSize(TYP_INT));
GenTreePtr indHigh = new (m_compiler, GT_IND) GenTreeIndir(GT_IND, TYP_INT, addrHigh, nullptr);
indHigh->gtFlags |= (indLow->gtFlags & (GTF_GLOB_REF | GTF_EXCEPT | GTF_IND_FLAGS));
m_compiler->lvaIncRefCnts(addrBaseHigh);
Range().InsertAfter(indLow, addrBaseHigh, addrHigh, indHigh);
return FinalizeDecomposition(use, indLow, indHigh, indHigh);
}
//------------------------------------------------------------------------
// DecomposeNot: Decompose GT_NOT.
//
// Arguments:
// use - the LIR::Use object for the def that needs to be decomposed.
//
// Return Value:
// The next node to process.
//
GenTree* DecomposeLongs::DecomposeNot(LIR::Use& use)
{
assert(use.IsInitialized());
assert(use.Def()->OperGet() == GT_NOT);
GenTree* tree = use.Def();
GenTree* gtLong = tree->gtGetOp1();
noway_assert(gtLong->OperGet() == GT_LONG);
GenTree* loOp1 = gtLong->gtGetOp1();
GenTree* hiOp1 = gtLong->gtGetOp2();
Range().Remove(gtLong);
GenTree* loResult = tree;
loResult->gtType = TYP_INT;
loResult->gtOp.gtOp1 = loOp1;
GenTree* hiResult = new (m_compiler, GT_NOT) GenTreeOp(GT_NOT, TYP_INT, hiOp1, nullptr);
Range().InsertAfter(loResult, hiResult);
return FinalizeDecomposition(use, loResult, hiResult, hiResult);
}
//------------------------------------------------------------------------
// DecomposeNeg: Decompose GT_NEG.
//
// Arguments:
// use - the LIR::Use object for the def that needs to be decomposed.
//
// Return Value:
// The next node to process.
//
GenTree* DecomposeLongs::DecomposeNeg(LIR::Use& use)
{
assert(use.IsInitialized());
assert(use.Def()->OperGet() == GT_NEG);
GenTree* tree = use.Def();
GenTree* gtLong = tree->gtGetOp1();
noway_assert(gtLong->OperGet() == GT_LONG);
GenTree* loOp1 = gtLong->gtGetOp1();
GenTree* hiOp1 = gtLong->gtGetOp2();
Range().Remove(gtLong);
GenTree* loResult = tree;
loResult->gtType = TYP_INT;
loResult->gtOp.gtOp1 = loOp1;
GenTree* zero = m_compiler->gtNewZeroConNode(TYP_INT);
#if defined(_TARGET_X86_)
GenTree* hiAdjust = m_compiler->gtNewOperNode(GT_ADD_HI, TYP_INT, hiOp1, zero);
GenTree* hiResult = m_compiler->gtNewOperNode(GT_NEG, TYP_INT, hiAdjust);
Range().InsertAfter(loResult, zero, hiAdjust, hiResult);
#elif defined(_TARGET_ARM_)
GenTree* hiResult = m_compiler->gtNewOperNode(GT_SUB_HI, TYP_INT, zero, hiOp1);
Range().InsertAfter(loResult, zero, hiResult);
#endif
return FinalizeDecomposition(use, loResult, hiResult, hiResult);
}
//------------------------------------------------------------------------
// DecomposeArith: Decompose GT_ADD, GT_SUB, GT_OR, GT_XOR, GT_AND.
//
// Arguments:
// use - the LIR::Use object for the def that needs to be decomposed.
//
// Return Value:
// The next node to process.
//
GenTree* DecomposeLongs::DecomposeArith(LIR::Use& use)
{
assert(use.IsInitialized());
GenTree* tree = use.Def();
genTreeOps oper = tree->OperGet();
assert((oper == GT_ADD) || (oper == GT_SUB) || (oper == GT_OR) || (oper == GT_XOR) || (oper == GT_AND));
GenTree* op1 = tree->gtGetOp1();
GenTree* op2 = tree->gtGetOp2();
// Both operands must have already been decomposed into GT_LONG operators.
noway_assert((op1->OperGet() == GT_LONG) && (op2->OperGet() == GT_LONG));
// Capture the lo and hi halves of op1 and op2.
GenTree* loOp1 = op1->gtGetOp1();
GenTree* hiOp1 = op1->gtGetOp2();
GenTree* loOp2 = op2->gtGetOp1();
GenTree* hiOp2 = op2->gtGetOp2();
// Now, remove op1 and op2 from the node list.
Range().Remove(op1);
Range().Remove(op2);
// We will reuse "tree" for the loResult, which will now be of TYP_INT, and its operands
// will be the lo halves of op1 from above.
GenTree* loResult = tree;
loResult->SetOper(GetLoOper(oper));
loResult->gtType = TYP_INT;
loResult->gtOp.gtOp1 = loOp1;
loResult->gtOp.gtOp2 = loOp2;
GenTree* hiResult = new (m_compiler, oper) GenTreeOp(GetHiOper(oper), TYP_INT, hiOp1, hiOp2);
Range().InsertAfter(loResult, hiResult);
if ((oper == GT_ADD) || (oper == GT_SUB))
{
loResult->gtFlags |= GTF_SET_FLAGS;
hiResult->gtFlags |= GTF_USE_FLAGS;
if (loResult->gtOverflow())
{
hiResult->gtFlags |= GTF_OVERFLOW | GTF_EXCEPT;
loResult->gtFlags &= ~(GTF_OVERFLOW | GTF_EXCEPT);
}
if (loResult->gtFlags & GTF_UNSIGNED)
{
hiResult->gtFlags |= GTF_UNSIGNED;
}
}
return FinalizeDecomposition(use, loResult, hiResult, hiResult);
}
//------------------------------------------------------------------------
// DecomposeShift: Decompose GT_LSH, GT_RSH, GT_RSZ. For shift nodes being shifted
// by a constant int, we can inspect the shift amount and decompose to the appropriate
// node types, generating a shl/shld pattern for GT_LSH, a shrd/shr pattern for GT_RSZ,
// and a shrd/sar pattern for GT_SHR for most shift amounts. Shifting by 0, >= 32 and
// >= 64 are special cased to produce better code patterns.
//
// For all other shift nodes, we need to use the shift helper functions, so we here convert
// the shift into a helper call by pulling its arguments out of linear order and making
// them the args to a call, then replacing the original node with the new call.
//
// Arguments:
// use - the LIR::Use object for the def that needs to be decomposed.
//
// Return Value:
// The next node to process.
//
GenTree* DecomposeLongs::DecomposeShift(LIR::Use& use)
{
assert(use.IsInitialized());
GenTree* shift = use.Def();
GenTree* gtLong = shift->gtGetOp1();
GenTree* loOp1 = gtLong->gtGetOp1();
GenTree* hiOp1 = gtLong->gtGetOp2();
GenTree* shiftByOp = shift->gtGetOp2();
genTreeOps oper = shift->OperGet();
genTreeOps shiftByOper = shiftByOp->OperGet();
// tLo = ...
// ...
// tHi = ...
// ...
// tLong = long tLo, tHi
// ...
// tShiftAmount = ...
// ...
// tShift = shift tLong, tShiftAmount
assert((oper == GT_LSH) || (oper == GT_RSH) || (oper == GT_RSZ));
// If we are shifting by a constant int, we do not want to use a helper, instead, we decompose.
if (shiftByOper == GT_CNS_INT)
{
unsigned int count = shiftByOp->gtIntCon.gtIconVal;
Range().Remove(shiftByOp);
if (count == 0)
{
GenTree* next = shift->gtNext;
// Remove shift and don't do anything else.
Range().Remove(shift);
use.ReplaceWith(m_compiler, gtLong);
return next;
}
GenTree* loResult;
GenTree* hiResult;
GenTree* insertAfter;
switch (oper)
{
case GT_LSH:
{
if (count < 32)
{
// For shifts of < 32 bits, we transform the code to:
//
// tLo = ...
// st.lclVar vLo, tLo
// ...
// tHi = ...
// ...
// tShiftLo = lsh vLo, tShiftAmountLo
// tShitHiLong = long vLo, tHi
// tShiftHi = lsh_hi tShiftHiLong, tShiftAmountHi
//
// This will produce:
//
// reg1 = lo
// shl lo, shift
// shld hi, reg1, shift
Range().Remove(gtLong);
loOp1 = RepresentOpAsLocalVar(loOp1, gtLong, >Long->gtOp.gtOp1);
unsigned loOp1LclNum = loOp1->AsLclVarCommon()->gtLclNum;
Range().Remove(loOp1);
GenTree* shiftByHi = m_compiler->gtNewIconNode(count, TYP_INT);
GenTree* shiftByLo = m_compiler->gtNewIconNode(count, TYP_INT);
loResult = m_compiler->gtNewOperNode(GT_LSH, TYP_INT, loOp1, shiftByLo);
// Create a GT_LONG that contains loCopy and hiOp1. This will be used in codegen to
// generate the shld instruction
GenTree* loCopy = m_compiler->gtNewLclvNode(loOp1LclNum, TYP_INT);
GenTree* hiOp = new (m_compiler, GT_LONG) GenTreeOp(GT_LONG, TYP_LONG, loCopy, hiOp1);
hiResult = m_compiler->gtNewOperNode(GT_LSH_HI, TYP_INT, hiOp, shiftByHi);
m_compiler->lvaIncRefCnts(loCopy);
Range().InsertBefore(shift, loOp1, shiftByLo, loResult);
Range().InsertBefore(shift, loCopy, hiOp, shiftByHi, hiResult);
insertAfter = hiResult;
}
else
{
assert(count >= 32);
// Since we're left shifting at least 32 bits, we can remove the hi part of the shifted value iff
// it has no side effects.
//
// TODO-CQ: we could go perform this removal transitively (i.e. iteratively remove everything that
// feeds the hi operand while there are no side effects)
if ((hiOp1->gtFlags & GTF_ALL_EFFECT) == 0)
{
Range().Remove(hiOp1, true);
}
else
{
hiOp1->SetUnusedValue();
}
if (count < 64)
{
if (count == 32)
{
// Move loOp1 into hiResult (shift of 32 bits is just a mov of lo to hi)
// We need to make sure that we save lo to a temp variable so that we don't overwrite lo
// before saving it to hi in the case that we are doing an inplace shift. I.e.:
// x = x << 32
LIR::Use loOp1Use(Range(), >Long->gtOp.gtOp1, gtLong);
loOp1Use.ReplaceWithLclVar(m_compiler, m_blockWeight);
hiResult = loOp1Use.Def();
Range().Remove(gtLong);
}
else
{
Range().Remove(gtLong);
assert(count > 32 && count < 64);
// Move loOp1 into hiResult, do a GT_LSH with count - 32.
// We will compute hiResult before loResult in this case, so we don't need to store lo to a
// temp
GenTree* shiftBy = m_compiler->gtNewIconNode(count - 32, TYP_INT);
hiResult = m_compiler->gtNewOperNode(oper, TYP_INT, loOp1, shiftBy);
Range().InsertBefore(shift, shiftBy, hiResult);
}
}
else
{
assert(count >= 64);
Range().Remove(gtLong);
// Since we're left shifting at least 64 bits, we can remove the lo part of the shifted value
// iff it has no side effects.
//
// TODO-CQ: we could go perform this removal transitively (i.e. iteratively remove everything
// that feeds the lo operand while there are no side effects)
if ((loOp1->gtFlags & GTF_ALL_EFFECT) == 0)
{
Range().Remove(loOp1, true);
}
else
{
loOp1->SetUnusedValue();
}
// Zero out hi (shift of >= 64 bits moves all the bits out of the two registers)
hiResult = m_compiler->gtNewZeroConNode(TYP_INT);
Range().InsertBefore(shift, hiResult);
}
// Zero out loResult (shift of >= 32 bits shifts all lo bits to hiResult)
loResult = m_compiler->gtNewZeroConNode(TYP_INT);
Range().InsertBefore(shift, loResult);
insertAfter = loResult;
}
}
break;
case GT_RSZ:
{
Range().Remove(gtLong);
if (count < 32)
{
// Hi is a GT_RSZ, lo is a GT_RSH_LO. Will produce:
// reg1 = hi
// shrd lo, reg1, shift
// shr hi, shift
hiOp1 = RepresentOpAsLocalVar(hiOp1, gtLong, >Long->gtOp.gtOp2);
unsigned hiOp1LclNum = hiOp1->AsLclVarCommon()->gtLclNum;
GenTree* hiCopy = m_compiler->gtNewLclvNode(hiOp1LclNum, TYP_INT);
GenTree* shiftByHi = m_compiler->gtNewIconNode(count, TYP_INT);
GenTree* shiftByLo = m_compiler->gtNewIconNode(count, TYP_INT);
m_compiler->lvaIncRefCnts(hiCopy);
hiResult = m_compiler->gtNewOperNode(GT_RSZ, TYP_INT, hiOp1, shiftByHi);
// Create a GT_LONG that contains loOp1 and hiCopy. This will be used in codegen to
// generate the shrd instruction
GenTree* loOp = new (m_compiler, GT_LONG) GenTreeOp(GT_LONG, TYP_LONG, loOp1, hiCopy);
loResult = m_compiler->gtNewOperNode(GT_RSH_LO, TYP_INT, loOp, shiftByLo);
Range().InsertBefore(shift, hiCopy, loOp);
Range().InsertBefore(shift, shiftByLo, loResult);
Range().InsertBefore(shift, shiftByHi, hiResult);
}
else
{
// Since we're right shifting at least 32 bits, we can remove the lo part of the shifted value iff
// it has no side effects.
//
// TODO-CQ: we could go perform this removal transitively (i.e. iteratively remove everything that
// feeds the lo operand while there are no side effects)
if ((loOp1->gtFlags & GTF_ALL_EFFECT) == 0)
{
Range().Remove(loOp1, true);
}
else
{
loOp1->SetUnusedValue();
}
assert(count >= 32);
if (count < 64)
{
if (count == 32)
{
// Move hiOp1 into loResult.
loResult = hiOp1;
}
else
{
assert(count > 32 && count < 64);
// Move hiOp1 into loResult, do a GT_RSZ with count - 32.
GenTree* shiftBy = m_compiler->gtNewIconNode(count - 32, TYP_INT);
loResult = m_compiler->gtNewOperNode(oper, TYP_INT, hiOp1, shiftBy);
Range().InsertBefore(shift, shiftBy, loResult);
}
}
else
{
assert(count >= 64);
// Since we're right shifting at least 64 bits, we can remove the hi part of the shifted value
// iff it has no side effects.
//
// TODO-CQ: we could go perform this removal transitively (i.e. iteratively remove everything
// that feeds the hi operand while there are no side effects)
if ((hiOp1->gtFlags & GTF_ALL_EFFECT) == 0)
{
Range().Remove(hiOp1, true);
}
else
{
hiOp1->SetUnusedValue();
}
// Zero out lo
loResult = m_compiler->gtNewZeroConNode(TYP_INT);
Range().InsertBefore(shift, loResult);
}
// Zero out hi
hiResult = m_compiler->gtNewZeroConNode(TYP_INT);
Range().InsertBefore(shift, hiResult);
}
insertAfter = hiResult;
}
break;
case GT_RSH:
{
Range().Remove(gtLong);
hiOp1 = RepresentOpAsLocalVar(hiOp1, gtLong, >Long->gtOp.gtOp2);
unsigned hiOp1LclNum = hiOp1->AsLclVarCommon()->gtLclNum;
GenTree* hiCopy = m_compiler->gtNewLclvNode(hiOp1LclNum, TYP_INT);
Range().Remove(hiOp1);
if (count < 32)
{
// Hi is a GT_RSH, lo is a GT_RSH_LO. Will produce:
// reg1 = hi
// shrd lo, reg1, shift
// sar hi, shift
GenTree* shiftByHi = m_compiler->gtNewIconNode(count, TYP_INT);
GenTree* shiftByLo = m_compiler->gtNewIconNode(count, TYP_INT);
m_compiler->lvaIncRefCnts(hiCopy);
hiResult = m_compiler->gtNewOperNode(GT_RSH, TYP_INT, hiOp1, shiftByHi);
// Create a GT_LONG that contains loOp1 and hiCopy. This will be used in codegen to
// generate the shrd instruction
GenTree* loOp = new (m_compiler, GT_LONG) GenTreeOp(GT_LONG, TYP_LONG, loOp1, hiCopy);
loResult = m_compiler->gtNewOperNode(GT_RSH_LO, TYP_INT, loOp, shiftByLo);
Range().InsertBefore(shift, hiCopy, loOp);
Range().InsertBefore(shift, shiftByLo, loResult);
Range().InsertBefore(shift, shiftByHi, hiOp1, hiResult);
}
else
{
assert(count >= 32);
// Since we're right shifting at least 32 bits, we can remove the lo part of the shifted value iff
// it has no side effects.
//
// TODO-CQ: we could go perform this removal transitively (i.e. iteratively remove everything that
// feeds the lo operand while there are no side effects)
if ((loOp1->gtFlags & GTF_ALL_EFFECT) == 0)
{
Range().Remove(loOp1, true);
}
else
{
loOp1->SetUnusedValue();
}
if (count < 64)
{
if (count == 32)
{
// Move hiOp1 into loResult.
loResult = hiOp1;
Range().InsertBefore(shift, loResult);
}
else
{
assert(count > 32 && count < 64);
// Move hiOp1 into loResult, do a GT_RSH with count - 32.
GenTree* shiftBy = m_compiler->gtNewIconNode(count - 32, TYP_INT);
loResult = m_compiler->gtNewOperNode(oper, TYP_INT, hiOp1, shiftBy);
Range().InsertBefore(shift, hiOp1, shiftBy, loResult);
}
// Propagate sign bit in hiResult
GenTree* shiftBy = m_compiler->gtNewIconNode(31, TYP_INT);
hiResult = m_compiler->gtNewOperNode(GT_RSH, TYP_INT, hiCopy, shiftBy);
Range().InsertBefore(shift, shiftBy, hiCopy, hiResult);
m_compiler->lvaIncRefCnts(hiCopy);
}
else
{
assert(count >= 64);
// Propagate sign bit in loResult
GenTree* loShiftBy = m_compiler->gtNewIconNode(31, TYP_INT);
loResult = m_compiler->gtNewOperNode(GT_RSH, TYP_INT, hiCopy, loShiftBy);
Range().InsertBefore(shift, hiCopy, loShiftBy, loResult);
// Propagate sign bit in hiResult
GenTree* shiftBy = m_compiler->gtNewIconNode(31, TYP_INT);
hiResult = m_compiler->gtNewOperNode(GT_RSH, TYP_INT, hiOp1, shiftBy);
Range().InsertBefore(shift, shiftBy, hiOp1, hiResult);
m_compiler->lvaIncRefCnts(hiCopy);
}
}
insertAfter = hiResult;
}
break;
default:
unreached();
}
// Remove shift from Range
Range().Remove(shift);
return FinalizeDecomposition(use, loResult, hiResult, insertAfter);
}
else
{
// Because calls must be created as HIR and lowered to LIR, we need to dump
// any LIR temps into lclVars before using them as arguments.
shiftByOp = RepresentOpAsLocalVar(shiftByOp, shift, &shift->gtOp.gtOp2);
loOp1 = RepresentOpAsLocalVar(loOp1, gtLong, >Long->gtOp.gtOp1);
hiOp1 = RepresentOpAsLocalVar(hiOp1, gtLong, >Long->gtOp.gtOp2);
Range().Remove(shiftByOp);
Range().Remove(gtLong);
Range().Remove(loOp1);
Range().Remove(hiOp1);
unsigned helper;
switch (oper)
{
case GT_LSH:
helper = CORINFO_HELP_LLSH;
break;
case GT_RSH:
helper = CORINFO_HELP_LRSH;
break;
case GT_RSZ:
helper = CORINFO_HELP_LRSZ;
break;
default:
unreached();
}
GenTreeArgList* argList = m_compiler->gtNewArgList(loOp1, hiOp1, shiftByOp);
GenTree* call = m_compiler->gtNewHelperCallNode(helper, TYP_LONG, argList);
call->gtFlags |= shift->gtFlags & GTF_ALL_EFFECT;
if (shift->IsUnusedValue())
{
call->SetUnusedValue();
}
GenTreeCall* callNode = call->AsCall();
ReturnTypeDesc* retTypeDesc = callNode->GetReturnTypeDesc();
retTypeDesc->InitializeLongReturnType(m_compiler);
call = m_compiler->fgMorphArgs(callNode);
Range().InsertAfter(shift, LIR::SeqTree(m_compiler, call));
Range().Remove(shift);
use.ReplaceWith(m_compiler, call);
return call;
}
}
//------------------------------------------------------------------------
// DecomposeRotate: Decompose GT_ROL and GT_ROR with constant shift amounts. We can
// inspect the rotate amount and decompose to the appropriate node types, generating
// a shld/shld pattern for GT_ROL, a shrd/shrd pattern for GT_ROR, for most rotate
// amounts.
//
// Arguments:
// use - the LIR::Use object for the def that needs to be decomposed.
//
// Return Value:
// The next node to process.
//
GenTree* DecomposeLongs::DecomposeRotate(LIR::Use& use)
{
GenTree* tree = use.Def();
GenTree* gtLong = tree->gtGetOp1();
GenTree* rotateByOp = tree->gtGetOp2();
genTreeOps oper = tree->OperGet();
assert((oper == GT_ROL) || (oper == GT_ROR));
assert(rotateByOp->IsCnsIntOrI());
// For longs, we need to change rols into two GT_LSH_HIs and rors into two GT_RSH_LOs
// so we will get:
//
// shld lo, hi, rotateAmount
// shld hi, loCopy, rotateAmount
//
// or:
//
// shrd lo, hi, rotateAmount
// shrd hi, loCopy, rotateAmount
if (oper == GT_ROL)
{
oper = GT_LSH_HI;
}
else
{
oper = GT_RSH_LO;
}
unsigned int count = rotateByOp->gtIntCon.gtIconVal;
Range().Remove(rotateByOp);
// Make sure the rotate amount is between 0 and 63.
assert((count < 64) && (count != 0));
GenTree* loResult;
GenTree* hiResult;
if (count == 32)
{
// If the rotate amount is 32, then swap hi and lo
LIR::Use loOp1Use(Range(), >Long->gtOp.gtOp1, gtLong);
loOp1Use.ReplaceWithLclVar(m_compiler, m_blockWeight);
LIR::Use hiOp1Use(Range(), >Long->gtOp.gtOp2, gtLong);
hiOp1Use.ReplaceWithLclVar(m_compiler, m_blockWeight);
hiResult = loOp1Use.Def();
loResult = hiOp1Use.Def();
gtLong->gtOp.gtOp1 = loResult;
gtLong->gtOp.gtOp2 = hiResult;
GenTree* next = tree->gtNext;
// Remove tree and don't do anything else.
Range().Remove(tree);
use.ReplaceWith(m_compiler, gtLong);
return next;
}
else
{
GenTree* loOp1;
GenTree* hiOp1;
if (count > 32)
{
// If count > 32, we swap hi and lo, and subtract 32 from count
hiOp1 = gtLong->gtGetOp1();
loOp1 = gtLong->gtGetOp2();
Range().Remove(gtLong);
loOp1 = RepresentOpAsLocalVar(loOp1, gtLong, >Long->gtOp.gtOp2);
hiOp1 = RepresentOpAsLocalVar(hiOp1, gtLong, >Long->gtOp.gtOp1);
count -= 32;
}
else
{
loOp1 = gtLong->gtGetOp1();
hiOp1 = gtLong->gtGetOp2();
Range().Remove(gtLong);
loOp1 = RepresentOpAsLocalVar(loOp1, gtLong, >Long->gtOp.gtOp1);
hiOp1 = RepresentOpAsLocalVar(hiOp1, gtLong, >Long->gtOp.gtOp2);
}
unsigned loOp1LclNum = loOp1->AsLclVarCommon()->gtLclNum;
unsigned hiOp1LclNum = hiOp1->AsLclVarCommon()->gtLclNum;
Range().Remove(loOp1);
Range().Remove(hiOp1);
GenTree* rotateByHi = m_compiler->gtNewIconNode(count, TYP_INT);
GenTree* rotateByLo = m_compiler->gtNewIconNode(count, TYP_INT);
// Create a GT_LONG that contains loOp1 and hiCopy. This will be used in codegen to
// generate the shld instruction
GenTree* hiCopy = m_compiler->gtNewLclvNode(hiOp1LclNum, TYP_INT);
GenTree* loOp = new (m_compiler, GT_LONG) GenTreeOp(GT_LONG, TYP_LONG, hiCopy, loOp1);
loResult = m_compiler->gtNewOperNode(oper, TYP_INT, loOp, rotateByLo);
// Create a GT_LONG that contains loCopy and hiOp1. This will be used in codegen to
// generate the shld instruction
GenTree* loCopy = m_compiler->gtNewLclvNode(loOp1LclNum, TYP_INT);
GenTree* hiOp = new (m_compiler, GT_LONG) GenTreeOp(GT_LONG, TYP_LONG, loCopy, hiOp1);
hiResult = m_compiler->gtNewOperNode(oper, TYP_INT, hiOp, rotateByHi);
m_compiler->lvaIncRefCnts(loCopy);
m_compiler->lvaIncRefCnts(hiCopy);
Range().InsertBefore(tree, hiCopy, loOp1, loOp);
Range().InsertBefore(tree, rotateByLo, loResult);
Range().InsertBefore(tree, loCopy, hiOp1, hiOp);
Range().InsertBefore(tree, rotateByHi, hiResult);
Range().Remove(tree);
return FinalizeDecomposition(use, loResult, hiResult, hiResult);
}
}
//------------------------------------------------------------------------
// DecomposeMul: Decompose GT_MUL. The only GT_MULs that make it to decompose are
// those with the GTF_MUL_64RSLT flag set. These muls result in a mul instruction that
// returns its result in two registers like GT_CALLs do. Additionally, these muls are
// guaranteed to be in the form long = (long)int * (long)int. Therefore, to decompose
// these nodes, we convert them into GT_MUL_LONGs, undo the cast from int to long by
// stripping out the lo ops, and force them into the form var = mul, as we do for
// GT_CALLs. In codegen, we then produce a mul instruction that produces the result
// in edx:eax on x86 or in any two chosen by RA registers on arm32, and store those
// registers on the stack in genStoreLongLclVar.
//
// All other GT_MULs have been converted to helper calls in morph.cpp
//
// Arguments:
// use - the LIR::Use object for the def that needs to be decomposed.
//
// Return Value:
// The next node to process.
//
GenTree* DecomposeLongs::DecomposeMul(LIR::Use& use)
{
assert(use.IsInitialized());
GenTree* tree = use.Def();
genTreeOps oper = tree->OperGet();
assert(oper == GT_MUL);
assert((tree->gtFlags & GTF_MUL_64RSLT) != 0);
GenTree* op1 = tree->gtGetOp1();
GenTree* op2 = tree->gtGetOp2();
// We expect both operands to be int->long casts. DecomposeCast specifically
// ignores such casts when they are used by GT_MULs.
assert((op1->OperGet() == GT_CAST) && (op1->TypeGet() == TYP_LONG));
assert((op2->OperGet() == GT_CAST) && (op2->TypeGet() == TYP_LONG));
Range().Remove(op1);
Range().Remove(op2);
tree->gtOp.gtOp1 = op1->gtGetOp1();
tree->gtOp.gtOp2 = op2->gtGetOp1();
tree->SetOper(GT_MUL_LONG);
return StoreNodeToVar(use);
}
//------------------------------------------------------------------------
// DecomposeUMod: Decompose GT_UMOD. The only GT_UMODs that make it to decompose
// are guaranteed to be an unsigned long mod with op2 which is a cast to long from
// a constant int whose value is between 2 and 0x3fffffff. All other GT_UMODs are
// morphed into helper calls. These GT_UMODs will actually return an int value in
// RDX. In decompose, we make the lo operation a TYP_INT GT_UMOD, with op2 as the
// original lo half and op1 as a GT_LONG. We make the hi part 0, so we end up with:
//
// GT_UMOD[TYP_INT] ( GT_LONG [TYP_LONG] (loOp1, hiOp1), loOp2 [TYP_INT] )
//
// With the expectation that we will generate:
//
// EDX = hiOp1
// EAX = loOp1
// reg = loOp2
// idiv reg
// EDX is the remainder, and result of GT_UMOD
// mov hiReg = 0
//
// Arguments:
// use - the LIR::Use object for the def that needs to be decomposed.
//
// Return Value:
// The next node to process.
//
GenTree* DecomposeLongs::DecomposeUMod(LIR::Use& use)
{
assert(use.IsInitialized());
GenTree* tree = use.Def();
genTreeOps oper = tree->OperGet();
assert(oper == GT_UMOD);
GenTree* op1 = tree->gtGetOp1();
GenTree* op2 = tree->gtGetOp2();
assert(op1->OperGet() == GT_LONG);
assert(op2->OperGet() == GT_LONG);
GenTree* loOp2 = op2->gtGetOp1();
GenTree* hiOp2 = op2->gtGetOp2();
assert(loOp2->OperGet() == GT_CNS_INT);
assert(hiOp2->OperGet() == GT_CNS_INT);
assert((loOp2->gtIntCon.gtIconVal >= 2) && (loOp2->gtIntCon.gtIconVal <= 0x3fffffff));
assert(hiOp2->gtIntCon.gtIconVal == 0);
// Get rid of op2's hi part. We don't need it.
Range().Remove(hiOp2);
Range().Remove(op2);
// Lo part is the GT_UMOD
GenTree* loResult = tree;
loResult->gtOp.gtOp2 = loOp2;
loResult->gtType = TYP_INT;
// Set the high part to 0
GenTree* hiResult = m_compiler->gtNewZeroConNode(TYP_INT);
Range().InsertAfter(loResult, hiResult);
return FinalizeDecomposition(use, loResult, hiResult, hiResult);
}
#ifdef FEATURE_SIMD
//------------------------------------------------------------------------
// DecomposeSimd: Decompose GT_SIMD.
//
// Arguments:
// use - the LIR::Use object for the def that needs to be decomposed.
//
// Return Value:
// The next node to process.
//
GenTree* DecomposeLongs::DecomposeSimd(LIR::Use& use)
{
GenTree* tree = use.Def();
genTreeOps oper = tree->OperGet();
assert(oper == GT_SIMD);
GenTreeSIMD* simdTree = tree->AsSIMD();
switch (simdTree->gtSIMDIntrinsicID)
{
case SIMDIntrinsicGetItem:
return DecomposeSimdGetItem(use);
default:
noway_assert(!"unexpected GT_SIMD node in long decomposition");
break;
}
return nullptr;
}
//------------------------------------------------------------------------
// DecomposeSimdGetItem: Decompose GT_SIMD -- SIMDIntrinsicGetItem.
//
// Decompose a get[i] node on Vector<long>. For:
//
// GT_SIMD{get_item}[long](simd_var, index)
//
// create:
//
// tmp_simd_var = simd_var
// tmp_index = index
// loResult = GT_SIMD{get_item}[int](tmp_simd_var, tmp_index * 2)
// hiResult = GT_SIMD{get_item}[int](tmp_simd_var, tmp_index * 2 + 1)
// return: GT_LONG(loResult, hiResult)
//
// This isn't optimal codegen, since SIMDIntrinsicGetItem sometimes requires
// temps that could be shared, for example.
//
// Arguments:
// use - the LIR::Use object for the def that needs to be decomposed.
//
// Return Value:
// The next node to process.
//
GenTree* DecomposeLongs::DecomposeSimdGetItem(LIR::Use& use)
{
GenTree* tree = use.Def();
genTreeOps oper = tree->OperGet();
assert(oper == GT_SIMD);
GenTreeSIMD* simdTree = tree->AsSIMD();
var_types baseType = simdTree->gtSIMDBaseType;
unsigned simdSize = simdTree->gtSIMDSize;
assert(simdTree->gtSIMDIntrinsicID == SIMDIntrinsicGetItem);
assert(varTypeIsLong(baseType));
assert(varTypeIsLong(simdTree));
assert(varTypeIsSIMD(simdTree->gtOp.gtOp1->gtType));
assert(simdTree->gtOp.gtOp2->gtType == TYP_INT);
bool indexIsConst = simdTree->gtOp.gtOp2->IsCnsIntOrI();
ssize_t index = 0;
if (indexIsConst)
{
index = simdTree->gtOp.gtOp2->gtIntCon.gtIconVal;
}
GenTree* simdTmpVar = RepresentOpAsLocalVar(simdTree->gtOp.gtOp1, simdTree, &simdTree->gtOp.gtOp1);
unsigned simdTmpVarNum = simdTmpVar->AsLclVarCommon()->gtLclNum;
JITDUMP("[DecomposeSimdGetItem]: Saving op1 tree to a temp var:\n");
DISPTREERANGE(Range(), simdTmpVar);
Range().Remove(simdTmpVar);
GenTree* indexTmpVar = nullptr;
unsigned indexTmpVarNum = 0;
if (!indexIsConst)
{
indexTmpVar = RepresentOpAsLocalVar(simdTree->gtOp.gtOp2, simdTree, &simdTree->gtOp.gtOp2);
indexTmpVarNum = indexTmpVar->AsLclVarCommon()->gtLclNum;
JITDUMP("[DecomposeSimdGetItem]: Saving op2 tree to a temp var:\n");
DISPTREERANGE(Range(), indexTmpVar);
Range().Remove(indexTmpVar);
}
// Create:
// loResult = GT_SIMD{get_item}[int](tmp_simd_var, index * 2)
GenTree* simdTmpVar1 = simdTmpVar;
GenTree* indexTimesTwo1;
if (indexIsConst)
{
// Reuse the existing index constant node.
indexTimesTwo1 = simdTree->gtOp.gtOp2;
Range().Remove(indexTimesTwo1);
indexTimesTwo1->gtIntCon.gtIconVal = index * 2;
Range().InsertBefore(simdTree, simdTmpVar1, indexTimesTwo1);
}
else
{
GenTree* indexTmpVar1 = indexTmpVar;
GenTree* two1 = m_compiler->gtNewIconNode(2, TYP_INT);
indexTimesTwo1 = m_compiler->gtNewOperNode(GT_MUL, TYP_INT, indexTmpVar1, two1);
Range().InsertBefore(simdTree, simdTmpVar1, indexTmpVar1, two1, indexTimesTwo1);
}
GenTree* loResult =
m_compiler->gtNewSIMDNode(TYP_INT, simdTmpVar1, indexTimesTwo1, SIMDIntrinsicGetItem, TYP_INT, simdSize);
Range().InsertBefore(simdTree, loResult);
// Create:
// hiResult = GT_SIMD{get_item}[int](tmp_simd_var, index * 2 + 1)
GenTree* simdTmpVar2 = m_compiler->gtNewLclLNode(simdTmpVarNum, simdTree->gtOp.gtOp1->gtType);
GenTree* indexTimesTwoPlusOne;
if (indexIsConst)
{
indexTimesTwoPlusOne = m_compiler->gtNewIconNode(index * 2 + 1, TYP_INT);
Range().InsertBefore(simdTree, simdTmpVar2, indexTimesTwoPlusOne);
}
else
{
GenTree* indexTmpVar2 = m_compiler->gtNewLclLNode(indexTmpVarNum, TYP_INT);
GenTree* two2 = m_compiler->gtNewIconNode(2, TYP_INT);
GenTree* indexTimesTwo2 = m_compiler->gtNewOperNode(GT_MUL, TYP_INT, indexTmpVar2, two2);
GenTree* one = m_compiler->gtNewIconNode(1, TYP_INT);
indexTimesTwoPlusOne = m_compiler->gtNewOperNode(GT_ADD, TYP_INT, indexTimesTwo2, one);
Range().InsertBefore(simdTree, simdTmpVar2, indexTmpVar2, two2, indexTimesTwo2);
Range().InsertBefore(simdTree, one, indexTimesTwoPlusOne);
}
GenTree* hiResult =
m_compiler->gtNewSIMDNode(TYP_INT, simdTmpVar2, indexTimesTwoPlusOne, SIMDIntrinsicGetItem, TYP_INT, simdSize);
Range().InsertBefore(simdTree, hiResult);
// Done with the original tree; remove it.
Range().Remove(simdTree);
return FinalizeDecomposition(use, loResult, hiResult, hiResult);
}
#endif // FEATURE_SIMD
//------------------------------------------------------------------------
// StoreNodeToVar: Check if the user is a STORE_LCL_VAR, and if it isn't,
// store the node to a var. Then decompose the new LclVar.
//
// Arguments:
// use - the LIR::Use object for the def that needs to be decomposed.
//
// Return Value:
// The next node to process.
//
GenTree* DecomposeLongs::StoreNodeToVar(LIR::Use& use)
{
if (use.IsDummyUse())
return use.Def()->gtNext;
GenTree* tree = use.Def();
GenTree* user = use.User();
if (user->OperGet() == GT_STORE_LCL_VAR)
{
// If parent is already a STORE_LCL_VAR, we can skip it if
// it is already marked as lvIsMultiRegRet.
unsigned varNum = user->AsLclVarCommon()->gtLclNum;
if (m_compiler->lvaTable[varNum].lvIsMultiRegRet)
{
return tree->gtNext;
}
else if (!m_compiler->lvaTable[varNum].lvPromoted)
{
// If var wasn't promoted, we can just set lvIsMultiRegRet.
m_compiler->lvaTable[varNum].lvIsMultiRegRet = true;
return tree->gtNext;
}
}
// Otherwise, we need to force var = call()
unsigned varNum = use.ReplaceWithLclVar(m_compiler, m_blockWeight);
m_compiler->lvaTable[varNum].lvIsMultiRegRet = true;
// Decompose the new LclVar use
return DecomposeLclVar(use);
}
//------------------------------------------------------------------------
// Check is op already local var, if not store it to local.
//
// Arguments:
// op - GenTree* to represent as local variable
// user - user of op
// edge - edge from user to op
//
// Return Value:
// op represented as local var
//
GenTree* DecomposeLongs::RepresentOpAsLocalVar(GenTree* op, GenTree* user, GenTree** edge)
{
if (op->OperGet() == GT_LCL_VAR)
{
return op;
}
else
{
LIR::Use opUse(Range(), edge, user);
opUse.ReplaceWithLclVar(m_compiler, m_blockWeight);
return *edge;
}
}
//------------------------------------------------------------------------
// DecomposeLongs::EnsureIntSized:
// Checks to see if the given node produces an int-sized value and
// performs the appropriate widening if it does not.
//
// Arguments:
// node - The node that may need to be widened.
// signExtend - True if the value should be sign-extended; false if it
// should be zero-extended.
//
// Return Value:
// The node that produces the widened value.
GenTree* DecomposeLongs::EnsureIntSized(GenTree* node, bool signExtend)
{
assert(node != nullptr);
if (!varTypeIsSmall(node))
{
assert(genTypeSize(node) == genTypeSize(TYP_INT));
return node;
}
if (node->OperIs(GT_LCL_VAR) && !m_compiler->lvaTable[node->AsLclVarCommon()->gtLclNum].lvNormalizeOnLoad())
{
node->gtType = TYP_INT;
return node;
}
GenTree* const cast = m_compiler->gtNewCastNode(TYP_INT, node, node->TypeGet());
if (!signExtend)
{
cast->gtFlags |= GTF_UNSIGNED;
}
Range().InsertAfter(node, cast);
return cast;
}
//------------------------------------------------------------------------
// GetHiOper: Convert arithmetic operator to "high half" operator of decomposed node.
//
// Arguments:
// oper - operator to map
//
// Return Value:
// mapped operator
//
// static
genTreeOps DecomposeLongs::GetHiOper(genTreeOps oper)
{
switch (oper)
{
case GT_ADD:
return GT_ADD_HI;
break;
case GT_SUB:
return GT_SUB_HI;
break;
case GT_OR:
return GT_OR;
break;
case GT_AND:
return GT_AND;
break;
case GT_XOR:
return GT_XOR;
break;
default:
assert(!"GetHiOper called for invalid oper");
return GT_NONE;
}
}
//------------------------------------------------------------------------
// GetLoOper: Convert arithmetic operator to "low half" operator of decomposed node.
//
// Arguments:
// oper - operator to map
//
// Return Value:
// mapped operator
//
// static
genTreeOps DecomposeLongs::GetLoOper(genTreeOps oper)
{
switch (oper)
{
case GT_ADD:
return GT_ADD_LO;
break;
case GT_SUB:
return GT_SUB_LO;
break;
case GT_OR:
return GT_OR;
break;
case GT_AND:
return GT_AND;
break;
case GT_XOR:
return GT_XOR;
break;
default:
assert(!"GetLoOper called for invalid oper");
return GT_NONE;
}
}
#endif // !_TARGET_64BIT_
#endif // !LEGACY_BACKEND
|