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
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
|
/*
** FOLD: Constant Folding, Algebraic Simplifications and Reassociation.
** ABCelim: Array Bounds Check Elimination.
** CSE: Common-Subexpression Elimination.
** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
*/
#define lj_opt_fold_c
#define LUA_CORE
#include <math.h>
#include "lj_obj.h"
#if LJ_HASJIT
#include "lj_str.h"
#include "lj_tab.h"
#include "lj_ir.h"
#include "lj_jit.h"
#include "lj_iropt.h"
#include "lj_trace.h"
#if LJ_HASFFI
#include "lj_ctype.h"
#endif
#include "lj_carith.h"
#include "lj_vm.h"
#include "lj_strscan.h"
/* Here's a short description how the FOLD engine processes instructions:
**
** The FOLD engine receives a single instruction stored in fins (J->fold.ins).
** The instruction and its operands are used to select matching fold rules.
** These are applied iteratively until a fixed point is reached.
**
** The 8 bit opcode of the instruction itself plus the opcodes of the
** two instructions referenced by its operands form a 24 bit key
** 'ins left right' (unused operands -> 0, literals -> lowest 8 bits).
**
** This key is used for partial matching against the fold rules. The
** left/right operand fields of the key are successively masked with
** the 'any' wildcard, from most specific to least specific:
**
** ins left right
** ins any right
** ins left any
** ins any any
**
** The masked key is used to lookup a matching fold rule in a semi-perfect
** hash table. If a matching rule is found, the related fold function is run.
** Multiple rules can share the same fold function. A fold rule may return
** one of several special values:
**
** - NEXTFOLD means no folding was applied, because an additional test
** inside the fold function failed. Matching continues against less
** specific fold rules. Finally the instruction is passed on to CSE.
**
** - RETRYFOLD means the instruction was modified in-place. Folding is
** retried as if this instruction had just been received.
**
** All other return values are terminal actions -- no further folding is
** applied:
**
** - INTFOLD(i) returns a reference to the integer constant i.
**
** - LEFTFOLD and RIGHTFOLD return the left/right operand reference
** without emitting an instruction.
**
** - CSEFOLD and EMITFOLD pass the instruction directly to CSE or emit
** it without passing through any further optimizations.
**
** - FAILFOLD, DROPFOLD and CONDFOLD only apply to instructions which have
** no result (e.g. guarded assertions): FAILFOLD means the guard would
** always fail, i.e. the current trace is pointless. DROPFOLD means
** the guard is always true and has been eliminated. CONDFOLD is a
** shortcut for FAILFOLD + cond (i.e. drop if true, otherwise fail).
**
** - Any other return value is interpreted as an IRRef or TRef. This
** can be a reference to an existing or a newly created instruction.
** Only the least-significant 16 bits (IRRef1) are used to form a TRef
** which is finally returned to the caller.
**
** The FOLD engine receives instructions both from the trace recorder and
** substituted instructions from LOOP unrolling. This means all types
** of instructions may end up here, even though the recorder bypasses
** FOLD in some cases. Thus all loads, stores and allocations must have
** an any/any rule to avoid being passed on to CSE.
**
** Carefully read the following requirements before adding or modifying
** any fold rules:
**
** Requirement #1: All fold rules must preserve their destination type.
**
** Consistently use INTFOLD() (KINT result) or lj_ir_knum() (KNUM result).
** Never use lj_ir_knumint() which can have either a KINT or KNUM result.
**
** Requirement #2: Fold rules should not create *new* instructions which
** reference operands *across* PHIs.
**
** E.g. a RETRYFOLD with 'fins->op1 = fleft->op1' is invalid if the
** left operand is a PHI. Then fleft->op1 would point across the PHI
** frontier to an invariant instruction. Adding a PHI for this instruction
** would be counterproductive. The solution is to add a barrier which
** prevents folding across PHIs, i.e. 'PHIBARRIER(fleft)' in this case.
** The only exception is for recurrences with high latencies like
** repeated int->num->int conversions.
**
** One could relax this condition a bit if the referenced instruction is
** a PHI, too. But this often leads to worse code due to excessive
** register shuffling.
**
** Note: returning *existing* instructions (e.g. LEFTFOLD) is ok, though.
** Even returning fleft->op1 would be ok, because a new PHI will added,
** if needed. But again, this leads to excessive register shuffling and
** should be avoided.
**
** Requirement #3: The set of all fold rules must be monotonic to guarantee
** termination.
**
** The goal is optimization, so one primarily wants to add strength-reducing
** rules. This means eliminating an instruction or replacing an instruction
** with one or more simpler instructions. Don't add fold rules which point
** into the other direction.
**
** Some rules (like commutativity) do not directly reduce the strength of
** an instruction, but enable other fold rules (e.g. by moving constants
** to the right operand). These rules must be made unidirectional to avoid
** cycles.
**
** Rule of thumb: the trace recorder expands the IR and FOLD shrinks it.
*/
/* Some local macros to save typing. Undef'd at the end. */
#define IR(ref) (&J->cur.ir[(ref)])
#define fins (&J->fold.ins)
#define fleft (&J->fold.left)
#define fright (&J->fold.right)
#define knumleft (ir_knum(fleft)->n)
#define knumright (ir_knum(fright)->n)
/* Pass IR on to next optimization in chain (FOLD). */
#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J))
/* Fold function type. Fastcall on x86 significantly reduces their size. */
typedef IRRef (LJ_FASTCALL *FoldFunc)(jit_State *J);
/* Macros for the fold specs, so buildvm can recognize them. */
#define LJFOLD(x)
#define LJFOLDX(x)
#define LJFOLDF(name) static TRef LJ_FASTCALL fold_##name(jit_State *J)
/* Note: They must be at the start of a line or buildvm ignores them! */
/* Barrier to prevent using operands across PHIs. */
#define PHIBARRIER(ir) if (irt_isphi((ir)->t)) return NEXTFOLD
/* Barrier to prevent folding across a GC step.
** GC steps can only happen at the head of a trace and at LOOP.
** And the GC is only driven forward if there is at least one allocation.
*/
#define gcstep_barrier(J, ref) \
((ref) < J->chain[IR_LOOP] && \
(J->chain[IR_SNEW] || J->chain[IR_XSNEW] || \
J->chain[IR_TNEW] || J->chain[IR_TDUP] || \
J->chain[IR_CNEW] || J->chain[IR_CNEWI] || J->chain[IR_TOSTR]))
/* -- Constant folding for FP numbers ------------------------------------- */
LJFOLD(ADD KNUM KNUM)
LJFOLD(SUB KNUM KNUM)
LJFOLD(MUL KNUM KNUM)
LJFOLD(DIV KNUM KNUM)
LJFOLD(NEG KNUM KNUM)
LJFOLD(ABS KNUM KNUM)
LJFOLD(ATAN2 KNUM KNUM)
LJFOLD(LDEXP KNUM KNUM)
LJFOLD(MIN KNUM KNUM)
LJFOLD(MAX KNUM KNUM)
LJFOLDF(kfold_numarith)
{
lua_Number a = knumleft;
lua_Number b = knumright;
lua_Number y = lj_vm_foldarith(a, b, fins->o - IR_ADD);
return lj_ir_knum(J, y);
}
LJFOLD(LDEXP KNUM KINT)
LJFOLDF(kfold_ldexp)
{
#if LJ_TARGET_X86ORX64
UNUSED(J);
return NEXTFOLD;
#else
return lj_ir_knum(J, ldexp(knumleft, fright->i));
#endif
}
LJFOLD(FPMATH KNUM any)
LJFOLDF(kfold_fpmath)
{
lua_Number a = knumleft;
lua_Number y = lj_vm_foldfpm(a, fins->op2);
return lj_ir_knum(J, y);
}
LJFOLD(POW KNUM KINT)
LJFOLDF(kfold_numpow)
{
lua_Number a = knumleft;
lua_Number b = (lua_Number)fright->i;
lua_Number y = lj_vm_foldarith(a, b, IR_POW - IR_ADD);
return lj_ir_knum(J, y);
}
/* Must not use kfold_kref for numbers (could be NaN). */
LJFOLD(EQ KNUM KNUM)
LJFOLD(NE KNUM KNUM)
LJFOLD(LT KNUM KNUM)
LJFOLD(GE KNUM KNUM)
LJFOLD(LE KNUM KNUM)
LJFOLD(GT KNUM KNUM)
LJFOLD(ULT KNUM KNUM)
LJFOLD(UGE KNUM KNUM)
LJFOLD(ULE KNUM KNUM)
LJFOLD(UGT KNUM KNUM)
LJFOLDF(kfold_numcomp)
{
return CONDFOLD(lj_ir_numcmp(knumleft, knumright, (IROp)fins->o));
}
/* -- Constant folding for 32 bit integers -------------------------------- */
static int32_t kfold_intop(int32_t k1, int32_t k2, IROp op)
{
switch (op) {
case IR_ADD: k1 += k2; break;
case IR_SUB: k1 -= k2; break;
case IR_MUL: k1 *= k2; break;
case IR_MOD: k1 = lj_vm_modi(k1, k2); break;
case IR_NEG: k1 = -k1; break;
case IR_BAND: k1 &= k2; break;
case IR_BOR: k1 |= k2; break;
case IR_BXOR: k1 ^= k2; break;
case IR_BSHL: k1 <<= (k2 & 31); break;
case IR_BSHR: k1 = (int32_t)((uint32_t)k1 >> (k2 & 31)); break;
case IR_BSAR: k1 >>= (k2 & 31); break;
case IR_BROL: k1 = (int32_t)lj_rol((uint32_t)k1, (k2 & 31)); break;
case IR_BROR: k1 = (int32_t)lj_ror((uint32_t)k1, (k2 & 31)); break;
case IR_MIN: k1 = k1 < k2 ? k1 : k2; break;
case IR_MAX: k1 = k1 > k2 ? k1 : k2; break;
default: lua_assert(0); break;
}
return k1;
}
LJFOLD(ADD KINT KINT)
LJFOLD(SUB KINT KINT)
LJFOLD(MUL KINT KINT)
LJFOLD(MOD KINT KINT)
LJFOLD(NEG KINT KINT)
LJFOLD(BAND KINT KINT)
LJFOLD(BOR KINT KINT)
LJFOLD(BXOR KINT KINT)
LJFOLD(BSHL KINT KINT)
LJFOLD(BSHR KINT KINT)
LJFOLD(BSAR KINT KINT)
LJFOLD(BROL KINT KINT)
LJFOLD(BROR KINT KINT)
LJFOLD(MIN KINT KINT)
LJFOLD(MAX KINT KINT)
LJFOLDF(kfold_intarith)
{
return INTFOLD(kfold_intop(fleft->i, fright->i, (IROp)fins->o));
}
LJFOLD(ADDOV KINT KINT)
LJFOLD(SUBOV KINT KINT)
LJFOLD(MULOV KINT KINT)
LJFOLDF(kfold_intovarith)
{
lua_Number n = lj_vm_foldarith((lua_Number)fleft->i, (lua_Number)fright->i,
fins->o - IR_ADDOV);
int32_t k = lj_num2int(n);
if (n != (lua_Number)k)
return FAILFOLD;
return INTFOLD(k);
}
LJFOLD(BNOT KINT)
LJFOLDF(kfold_bnot)
{
return INTFOLD(~fleft->i);
}
LJFOLD(BSWAP KINT)
LJFOLDF(kfold_bswap)
{
return INTFOLD((int32_t)lj_bswap((uint32_t)fleft->i));
}
LJFOLD(LT KINT KINT)
LJFOLD(GE KINT KINT)
LJFOLD(LE KINT KINT)
LJFOLD(GT KINT KINT)
LJFOLD(ULT KINT KINT)
LJFOLD(UGE KINT KINT)
LJFOLD(ULE KINT KINT)
LJFOLD(UGT KINT KINT)
LJFOLD(ABC KINT KINT)
LJFOLDF(kfold_intcomp)
{
int32_t a = fleft->i, b = fright->i;
switch ((IROp)fins->o) {
case IR_LT: return CONDFOLD(a < b);
case IR_GE: return CONDFOLD(a >= b);
case IR_LE: return CONDFOLD(a <= b);
case IR_GT: return CONDFOLD(a > b);
case IR_ULT: return CONDFOLD((uint32_t)a < (uint32_t)b);
case IR_UGE: return CONDFOLD((uint32_t)a >= (uint32_t)b);
case IR_ULE: return CONDFOLD((uint32_t)a <= (uint32_t)b);
case IR_ABC:
case IR_UGT: return CONDFOLD((uint32_t)a > (uint32_t)b);
default: lua_assert(0); return FAILFOLD;
}
}
LJFOLD(UGE any KINT)
LJFOLDF(kfold_intcomp0)
{
if (fright->i == 0)
return DROPFOLD;
return NEXTFOLD;
}
/* -- Constant folding for 64 bit integers -------------------------------- */
static uint64_t kfold_int64arith(uint64_t k1, uint64_t k2, IROp op)
{
switch (op) {
#if LJ_64 || LJ_HASFFI
case IR_ADD: k1 += k2; break;
case IR_SUB: k1 -= k2; break;
#endif
#if LJ_HASFFI
case IR_MUL: k1 *= k2; break;
case IR_BAND: k1 &= k2; break;
case IR_BOR: k1 |= k2; break;
case IR_BXOR: k1 ^= k2; break;
#endif
default: UNUSED(k2); lua_assert(0); break;
}
return k1;
}
LJFOLD(ADD KINT64 KINT64)
LJFOLD(SUB KINT64 KINT64)
LJFOLD(MUL KINT64 KINT64)
LJFOLD(BAND KINT64 KINT64)
LJFOLD(BOR KINT64 KINT64)
LJFOLD(BXOR KINT64 KINT64)
LJFOLDF(kfold_int64arith)
{
return INT64FOLD(kfold_int64arith(ir_k64(fleft)->u64,
ir_k64(fright)->u64, (IROp)fins->o));
}
LJFOLD(DIV KINT64 KINT64)
LJFOLD(MOD KINT64 KINT64)
LJFOLD(POW KINT64 KINT64)
LJFOLDF(kfold_int64arith2)
{
#if LJ_HASFFI
uint64_t k1 = ir_k64(fleft)->u64, k2 = ir_k64(fright)->u64;
if (irt_isi64(fins->t)) {
k1 = fins->o == IR_DIV ? lj_carith_divi64((int64_t)k1, (int64_t)k2) :
fins->o == IR_MOD ? lj_carith_modi64((int64_t)k1, (int64_t)k2) :
lj_carith_powi64((int64_t)k1, (int64_t)k2);
} else {
k1 = fins->o == IR_DIV ? lj_carith_divu64(k1, k2) :
fins->o == IR_MOD ? lj_carith_modu64(k1, k2) :
lj_carith_powu64(k1, k2);
}
return INT64FOLD(k1);
#else
UNUSED(J); lua_assert(0); return FAILFOLD;
#endif
}
LJFOLD(BSHL KINT64 KINT)
LJFOLD(BSHR KINT64 KINT)
LJFOLD(BSAR KINT64 KINT)
LJFOLD(BROL KINT64 KINT)
LJFOLD(BROR KINT64 KINT)
LJFOLDF(kfold_int64shift)
{
#if LJ_HASFFI || LJ_64
uint64_t k = ir_k64(fleft)->u64;
int32_t sh = (fright->i & 63);
switch ((IROp)fins->o) {
case IR_BSHL: k <<= sh; break;
#if LJ_HASFFI
case IR_BSHR: k >>= sh; break;
case IR_BSAR: k = (uint64_t)((int64_t)k >> sh); break;
case IR_BROL: k = lj_rol(k, sh); break;
case IR_BROR: k = lj_ror(k, sh); break;
#endif
default: lua_assert(0); break;
}
return INT64FOLD(k);
#else
UNUSED(J); lua_assert(0); return FAILFOLD;
#endif
}
LJFOLD(BNOT KINT64)
LJFOLDF(kfold_bnot64)
{
#if LJ_HASFFI
return INT64FOLD(~ir_k64(fleft)->u64);
#else
UNUSED(J); lua_assert(0); return FAILFOLD;
#endif
}
LJFOLD(BSWAP KINT64)
LJFOLDF(kfold_bswap64)
{
#if LJ_HASFFI
return INT64FOLD(lj_bswap64(ir_k64(fleft)->u64));
#else
UNUSED(J); lua_assert(0); return FAILFOLD;
#endif
}
LJFOLD(LT KINT64 KINT64)
LJFOLD(GE KINT64 KINT64)
LJFOLD(LE KINT64 KINT64)
LJFOLD(GT KINT64 KINT64)
LJFOLD(ULT KINT64 KINT64)
LJFOLD(UGE KINT64 KINT64)
LJFOLD(ULE KINT64 KINT64)
LJFOLD(UGT KINT64 KINT64)
LJFOLDF(kfold_int64comp)
{
#if LJ_HASFFI
uint64_t a = ir_k64(fleft)->u64, b = ir_k64(fright)->u64;
switch ((IROp)fins->o) {
case IR_LT: return CONDFOLD(a < b);
case IR_GE: return CONDFOLD(a >= b);
case IR_LE: return CONDFOLD(a <= b);
case IR_GT: return CONDFOLD(a > b);
case IR_ULT: return CONDFOLD((uint64_t)a < (uint64_t)b);
case IR_UGE: return CONDFOLD((uint64_t)a >= (uint64_t)b);
case IR_ULE: return CONDFOLD((uint64_t)a <= (uint64_t)b);
case IR_UGT: return CONDFOLD((uint64_t)a > (uint64_t)b);
default: lua_assert(0); return FAILFOLD;
}
#else
UNUSED(J); lua_assert(0); return FAILFOLD;
#endif
}
LJFOLD(UGE any KINT64)
LJFOLDF(kfold_int64comp0)
{
#if LJ_HASFFI
if (ir_k64(fright)->u64 == 0)
return DROPFOLD;
return NEXTFOLD;
#else
UNUSED(J); lua_assert(0); return FAILFOLD;
#endif
}
/* -- Constant folding for strings ---------------------------------------- */
LJFOLD(SNEW KKPTR KINT)
LJFOLDF(kfold_snew_kptr)
{
GCstr *s = lj_str_new(J->L, (const char *)ir_kptr(fleft), (size_t)fright->i);
return lj_ir_kstr(J, s);
}
LJFOLD(SNEW any KINT)
LJFOLDF(kfold_snew_empty)
{
if (fright->i == 0)
return lj_ir_kstr(J, &J2G(J)->strempty);
return NEXTFOLD;
}
LJFOLD(STRREF KGC KINT)
LJFOLDF(kfold_strref)
{
GCstr *str = ir_kstr(fleft);
lua_assert((MSize)fright->i <= str->len);
return lj_ir_kkptr(J, (char *)strdata(str) + fright->i);
}
LJFOLD(STRREF SNEW any)
LJFOLDF(kfold_strref_snew)
{
PHIBARRIER(fleft);
if (irref_isk(fins->op2) && fright->i == 0) {
return fleft->op1; /* strref(snew(ptr, len), 0) ==> ptr */
} else {
/* Reassociate: strref(snew(strref(str, a), len), b) ==> strref(str, a+b) */
IRIns *ir = IR(fleft->op1);
if (ir->o == IR_STRREF) {
IRRef1 str = ir->op1; /* IRIns * is not valid across emitir. */
PHIBARRIER(ir);
fins->op2 = emitir(IRTI(IR_ADD), ir->op2, fins->op2); /* Clobbers fins! */
fins->op1 = str;
fins->ot = IRT(IR_STRREF, IRT_P32);
return RETRYFOLD;
}
}
return NEXTFOLD;
}
LJFOLD(CALLN CARG IRCALL_lj_str_cmp)
LJFOLDF(kfold_strcmp)
{
if (irref_isk(fleft->op1) && irref_isk(fleft->op2)) {
GCstr *a = ir_kstr(IR(fleft->op1));
GCstr *b = ir_kstr(IR(fleft->op2));
return INTFOLD(lj_str_cmp(a, b));
}
return NEXTFOLD;
}
/* -- Constant folding of pointer arithmetic ------------------------------ */
LJFOLD(ADD KGC KINT)
LJFOLD(ADD KGC KINT64)
LJFOLDF(kfold_add_kgc)
{
GCobj *o = ir_kgc(fleft);
#if LJ_64
ptrdiff_t ofs = (ptrdiff_t)ir_kint64(fright)->u64;
#else
ptrdiff_t ofs = fright->i;
#endif
#if LJ_HASFFI
if (irt_iscdata(fleft->t)) {
CType *ct = ctype_raw(ctype_ctsG(J2G(J)), gco2cd(o)->ctypeid);
if (ctype_isnum(ct->info) || ctype_isenum(ct->info) ||
ctype_isptr(ct->info) || ctype_isfunc(ct->info) ||
ctype_iscomplex(ct->info) || ctype_isvector(ct->info))
return lj_ir_kkptr(J, (char *)o + ofs);
}
#endif
return lj_ir_kptr(J, (char *)o + ofs);
}
LJFOLD(ADD KPTR KINT)
LJFOLD(ADD KPTR KINT64)
LJFOLD(ADD KKPTR KINT)
LJFOLD(ADD KKPTR KINT64)
LJFOLDF(kfold_add_kptr)
{
void *p = ir_kptr(fleft);
#if LJ_64
ptrdiff_t ofs = (ptrdiff_t)ir_kint64(fright)->u64;
#else
ptrdiff_t ofs = fright->i;
#endif
return lj_ir_kptr_(J, fleft->o, (char *)p + ofs);
}
LJFOLD(ADD any KGC)
LJFOLD(ADD any KPTR)
LJFOLD(ADD any KKPTR)
LJFOLDF(kfold_add_kright)
{
if (fleft->o == IR_KINT || fleft->o == IR_KINT64) {
IRRef1 tmp = fins->op1; fins->op1 = fins->op2; fins->op2 = tmp;
return RETRYFOLD;
}
return NEXTFOLD;
}
/* -- Constant folding of conversions ------------------------------------- */
LJFOLD(TOBIT KNUM KNUM)
LJFOLDF(kfold_tobit)
{
return INTFOLD(lj_num2bit(knumleft));
}
LJFOLD(CONV KINT IRCONV_NUM_INT)
LJFOLDF(kfold_conv_kint_num)
{
return lj_ir_knum(J, (lua_Number)fleft->i);
}
LJFOLD(CONV KINT IRCONV_NUM_U32)
LJFOLDF(kfold_conv_kintu32_num)
{
return lj_ir_knum(J, (lua_Number)(uint32_t)fleft->i);
}
LJFOLD(CONV KINT IRCONV_INT_I8)
LJFOLD(CONV KINT IRCONV_INT_U8)
LJFOLD(CONV KINT IRCONV_INT_I16)
LJFOLD(CONV KINT IRCONV_INT_U16)
LJFOLDF(kfold_conv_kint_ext)
{
int32_t k = fleft->i;
if ((fins->op2 & IRCONV_SRCMASK) == IRT_I8) k = (int8_t)k;
else if ((fins->op2 & IRCONV_SRCMASK) == IRT_U8) k = (uint8_t)k;
else if ((fins->op2 & IRCONV_SRCMASK) == IRT_I16) k = (int16_t)k;
else k = (uint16_t)k;
return INTFOLD(k);
}
LJFOLD(CONV KINT IRCONV_I64_INT)
LJFOLD(CONV KINT IRCONV_U64_INT)
LJFOLD(CONV KINT IRCONV_I64_U32)
LJFOLD(CONV KINT IRCONV_U64_U32)
LJFOLDF(kfold_conv_kint_i64)
{
if ((fins->op2 & IRCONV_SEXT))
return INT64FOLD((uint64_t)(int64_t)fleft->i);
else
return INT64FOLD((uint64_t)(int64_t)(uint32_t)fleft->i);
}
LJFOLD(CONV KINT64 IRCONV_NUM_I64)
LJFOLDF(kfold_conv_kint64_num_i64)
{
return lj_ir_knum(J, (lua_Number)(int64_t)ir_kint64(fleft)->u64);
}
LJFOLD(CONV KINT64 IRCONV_NUM_U64)
LJFOLDF(kfold_conv_kint64_num_u64)
{
return lj_ir_knum(J, (lua_Number)ir_kint64(fleft)->u64);
}
LJFOLD(CONV KINT64 IRCONV_INT_I64)
LJFOLD(CONV KINT64 IRCONV_U32_I64)
LJFOLDF(kfold_conv_kint64_int_i64)
{
return INTFOLD((int32_t)ir_kint64(fleft)->u64);
}
LJFOLD(CONV KNUM IRCONV_INT_NUM)
LJFOLDF(kfold_conv_knum_int_num)
{
lua_Number n = knumleft;
if (!(fins->op2 & IRCONV_TRUNC)) {
int32_t k = lj_num2int(n);
if (irt_isguard(fins->t) && n != (lua_Number)k) {
/* We're about to create a guard which always fails, like CONV +1.5.
** Some pathological loops cause this during LICM, e.g.:
** local x,k,t = 0,1.5,{1,[1.5]=2}
** for i=1,200 do x = x+ t[k]; k = k == 1 and 1.5 or 1 end
** assert(x == 300)
*/
return FAILFOLD;
}
return INTFOLD(k);
} else {
return INTFOLD((int32_t)n);
}
}
LJFOLD(CONV KNUM IRCONV_U32_NUM)
LJFOLDF(kfold_conv_knum_u32_num)
{
lua_assert((fins->op2 & IRCONV_TRUNC));
#ifdef _MSC_VER
{ /* Workaround for MSVC bug. */
volatile uint32_t u = (uint32_t)knumleft;
return INTFOLD((int32_t)u);
}
#else
return INTFOLD((int32_t)(uint32_t)knumleft);
#endif
}
LJFOLD(CONV KNUM IRCONV_I64_NUM)
LJFOLDF(kfold_conv_knum_i64_num)
{
lua_assert((fins->op2 & IRCONV_TRUNC));
return INT64FOLD((uint64_t)(int64_t)knumleft);
}
LJFOLD(CONV KNUM IRCONV_U64_NUM)
LJFOLDF(kfold_conv_knum_u64_num)
{
lua_assert((fins->op2 & IRCONV_TRUNC));
return INT64FOLD(lj_num2u64(knumleft));
}
LJFOLD(TOSTR KNUM)
LJFOLDF(kfold_tostr_knum)
{
return lj_ir_kstr(J, lj_str_fromnum(J->L, &knumleft));
}
LJFOLD(TOSTR KINT)
LJFOLDF(kfold_tostr_kint)
{
return lj_ir_kstr(J, lj_str_fromint(J->L, fleft->i));
}
LJFOLD(STRTO KGC)
LJFOLDF(kfold_strto)
{
TValue n;
if (lj_strscan_num(ir_kstr(fleft), &n))
return lj_ir_knum(J, numV(&n));
return FAILFOLD;
}
/* -- Constant folding of equality checks --------------------------------- */
/* Don't constant-fold away FLOAD checks against KNULL. */
LJFOLD(EQ FLOAD KNULL)
LJFOLD(NE FLOAD KNULL)
LJFOLDX(lj_opt_cse)
/* But fold all other KNULL compares, since only KNULL is equal to KNULL. */
LJFOLD(EQ any KNULL)
LJFOLD(NE any KNULL)
LJFOLD(EQ KNULL any)
LJFOLD(NE KNULL any)
LJFOLD(EQ KINT KINT) /* Constants are unique, so same refs <==> same value. */
LJFOLD(NE KINT KINT)
LJFOLD(EQ KINT64 KINT64)
LJFOLD(NE KINT64 KINT64)
LJFOLD(EQ KGC KGC)
LJFOLD(NE KGC KGC)
LJFOLDF(kfold_kref)
{
return CONDFOLD((fins->op1 == fins->op2) ^ (fins->o == IR_NE));
}
/* -- Algebraic shortcuts ------------------------------------------------- */
LJFOLD(FPMATH FPMATH IRFPM_FLOOR)
LJFOLD(FPMATH FPMATH IRFPM_CEIL)
LJFOLD(FPMATH FPMATH IRFPM_TRUNC)
LJFOLDF(shortcut_round)
{
IRFPMathOp op = (IRFPMathOp)fleft->op2;
if (op == IRFPM_FLOOR || op == IRFPM_CEIL || op == IRFPM_TRUNC)
return LEFTFOLD; /* round(round_left(x)) = round_left(x) */
return NEXTFOLD;
}
LJFOLD(ABS ABS KNUM)
LJFOLDF(shortcut_left)
{
return LEFTFOLD; /* f(g(x)) ==> g(x) */
}
LJFOLD(ABS NEG KNUM)
LJFOLDF(shortcut_dropleft)
{
PHIBARRIER(fleft);
fins->op1 = fleft->op1; /* abs(neg(x)) ==> abs(x) */
return RETRYFOLD;
}
/* Note: no safe shortcuts with STRTO and TOSTR ("1e2" ==> +100 ==> "100"). */
LJFOLD(NEG NEG any)
LJFOLD(BNOT BNOT)
LJFOLD(BSWAP BSWAP)
LJFOLDF(shortcut_leftleft)
{
PHIBARRIER(fleft); /* See above. Fold would be ok, but not beneficial. */
return fleft->op1; /* f(g(x)) ==> x */
}
/* -- FP algebraic simplifications ---------------------------------------- */
/* FP arithmetic is tricky -- there's not much to simplify.
** Please note the following common pitfalls before sending "improvements":
** x+0 ==> x is INVALID for x=-0
** 0-x ==> -x is INVALID for x=+0
** x*0 ==> 0 is INVALID for x=-0, x=+-Inf or x=NaN
*/
LJFOLD(ADD NEG any)
LJFOLDF(simplify_numadd_negx)
{
PHIBARRIER(fleft);
fins->o = IR_SUB; /* (-a) + b ==> b - a */
fins->op1 = fins->op2;
fins->op2 = fleft->op1;
return RETRYFOLD;
}
LJFOLD(ADD any NEG)
LJFOLDF(simplify_numadd_xneg)
{
PHIBARRIER(fright);
fins->o = IR_SUB; /* a + (-b) ==> a - b */
fins->op2 = fright->op1;
return RETRYFOLD;
}
LJFOLD(SUB any KNUM)
LJFOLDF(simplify_numsub_k)
{
lua_Number n = knumright;
if (n == 0.0) /* x - (+-0) ==> x */
return LEFTFOLD;
return NEXTFOLD;
}
LJFOLD(SUB NEG KNUM)
LJFOLDF(simplify_numsub_negk)
{
PHIBARRIER(fleft);
fins->op2 = fleft->op1; /* (-x) - k ==> (-k) - x */
fins->op1 = (IRRef1)lj_ir_knum(J, -knumright);
return RETRYFOLD;
}
LJFOLD(SUB any NEG)
LJFOLDF(simplify_numsub_xneg)
{
PHIBARRIER(fright);
fins->o = IR_ADD; /* a - (-b) ==> a + b */
fins->op2 = fright->op1;
return RETRYFOLD;
}
LJFOLD(MUL any KNUM)
LJFOLD(DIV any KNUM)
LJFOLDF(simplify_nummuldiv_k)
{
lua_Number n = knumright;
if (n == 1.0) { /* x o 1 ==> x */
return LEFTFOLD;
} else if (n == -1.0) { /* x o -1 ==> -x */
fins->o = IR_NEG;
fins->op2 = (IRRef1)lj_ir_knum_neg(J);
return RETRYFOLD;
} else if (fins->o == IR_MUL && n == 2.0) { /* x * 2 ==> x + x */
fins->o = IR_ADD;
fins->op2 = fins->op1;
return RETRYFOLD;
} else if (fins->o == IR_DIV) { /* x / 2^k ==> x * 2^-k */
uint64_t u = ir_knum(fright)->u64;
uint32_t ex = ((uint32_t)(u >> 52) & 0x7ff);
if ((u & U64x(000fffff,ffffffff)) == 0 && ex - 1 < 0x7fd) {
u = (u & ((uint64_t)1 << 63)) | ((uint64_t)(0x7fe - ex) << 52);
fins->o = IR_MUL; /* Multiply by exact reciprocal. */
fins->op2 = lj_ir_knum_u64(J, u);
return RETRYFOLD;
}
}
return NEXTFOLD;
}
LJFOLD(MUL NEG KNUM)
LJFOLD(DIV NEG KNUM)
LJFOLDF(simplify_nummuldiv_negk)
{
PHIBARRIER(fleft);
fins->op1 = fleft->op1; /* (-a) o k ==> a o (-k) */
fins->op2 = (IRRef1)lj_ir_knum(J, -knumright);
return RETRYFOLD;
}
LJFOLD(MUL NEG NEG)
LJFOLD(DIV NEG NEG)
LJFOLDF(simplify_nummuldiv_negneg)
{
PHIBARRIER(fleft);
PHIBARRIER(fright);
fins->op1 = fleft->op1; /* (-a) o (-b) ==> a o b */
fins->op2 = fright->op1;
return RETRYFOLD;
}
LJFOLD(POW any KINT)
LJFOLDF(simplify_numpow_xk)
{
int32_t k = fright->i;
TRef ref = fins->op1;
if (k == 0) /* x ^ 0 ==> 1 */
return lj_ir_knum_one(J); /* Result must be a number, not an int. */
if (k == 1) /* x ^ 1 ==> x */
return LEFTFOLD;
if ((uint32_t)(k+65536) > 2*65536u) /* Limit code explosion. */
return NEXTFOLD;
if (k < 0) { /* x ^ (-k) ==> (1/x) ^ k. */
ref = emitir(IRTN(IR_DIV), lj_ir_knum_one(J), ref);
k = -k;
}
/* Unroll x^k for 1 <= k <= 65536. */
for (; (k & 1) == 0; k >>= 1) /* Handle leading zeros. */
ref = emitir(IRTN(IR_MUL), ref, ref);
if ((k >>= 1) != 0) { /* Handle trailing bits. */
TRef tmp = emitir(IRTN(IR_MUL), ref, ref);
for (; k != 1; k >>= 1) {
if (k & 1)
ref = emitir(IRTN(IR_MUL), ref, tmp);
tmp = emitir(IRTN(IR_MUL), tmp, tmp);
}
ref = emitir(IRTN(IR_MUL), ref, tmp);
}
return ref;
}
LJFOLD(POW KNUM any)
LJFOLDF(simplify_numpow_kx)
{
lua_Number n = knumleft;
if (n == 2.0) { /* 2.0 ^ i ==> ldexp(1.0, tonum(i)) */
fins->o = IR_CONV;
#if LJ_TARGET_X86ORX64
fins->op1 = fins->op2;
fins->op2 = IRCONV_NUM_INT;
fins->op2 = (IRRef1)lj_opt_fold(J);
#endif
fins->op1 = (IRRef1)lj_ir_knum_one(J);
fins->o = IR_LDEXP;
return RETRYFOLD;
}
return NEXTFOLD;
}
/* -- Simplify conversions ------------------------------------------------ */
LJFOLD(CONV CONV IRCONV_NUM_INT) /* _NUM */
LJFOLDF(shortcut_conv_num_int)
{
PHIBARRIER(fleft);
/* Only safe with a guarded conversion to int. */
if ((fleft->op2 & IRCONV_SRCMASK) == IRT_NUM && irt_isguard(fleft->t))
return fleft->op1; /* f(g(x)) ==> x */
return NEXTFOLD;
}
LJFOLD(CONV CONV IRCONV_INT_NUM) /* _INT */
LJFOLD(CONV CONV IRCONV_U32_NUM) /* _U32*/
LJFOLDF(simplify_conv_int_num)
{
/* Fold even across PHI to avoid expensive num->int conversions in loop. */
if ((fleft->op2 & IRCONV_SRCMASK) ==
((fins->op2 & IRCONV_DSTMASK) >> IRCONV_DSH))
return fleft->op1;
return NEXTFOLD;
}
LJFOLD(CONV CONV IRCONV_I64_NUM) /* _INT or _U32 */
LJFOLD(CONV CONV IRCONV_U64_NUM) /* _INT or _U32 */
LJFOLDF(simplify_conv_i64_num)
{
PHIBARRIER(fleft);
if ((fleft->op2 & IRCONV_SRCMASK) == IRT_INT) {
/* Reduce to a sign-extension. */
fins->op1 = fleft->op1;
fins->op2 = ((IRT_I64<<5)|IRT_INT|IRCONV_SEXT);
return RETRYFOLD;
} else if ((fleft->op2 & IRCONV_SRCMASK) == IRT_U32) {
#if LJ_TARGET_X64
return fleft->op1;
#else
/* Reduce to a zero-extension. */
fins->op1 = fleft->op1;
fins->op2 = (IRT_I64<<5)|IRT_U32;
return RETRYFOLD;
#endif
}
return NEXTFOLD;
}
LJFOLD(CONV CONV IRCONV_INT_I64) /* _INT or _U32 */
LJFOLD(CONV CONV IRCONV_INT_U64) /* _INT or _U32 */
LJFOLD(CONV CONV IRCONV_U32_I64) /* _INT or _U32 */
LJFOLD(CONV CONV IRCONV_U32_U64) /* _INT or _U32 */
LJFOLDF(simplify_conv_int_i64)
{
int src;
PHIBARRIER(fleft);
src = (fleft->op2 & IRCONV_SRCMASK);
if (src == IRT_INT || src == IRT_U32) {
if (src == ((fins->op2 & IRCONV_DSTMASK) >> IRCONV_DSH)) {
return fleft->op1;
} else {
fins->op2 = ((fins->op2 & IRCONV_DSTMASK) | src);
fins->op1 = fleft->op1;
return RETRYFOLD;
}
}
return NEXTFOLD;
}
LJFOLD(CONV CONV IRCONV_FLOAT_NUM) /* _FLOAT */
LJFOLDF(simplify_conv_flt_num)
{
PHIBARRIER(fleft);
if ((fleft->op2 & IRCONV_SRCMASK) == IRT_FLOAT)
return fleft->op1;
return NEXTFOLD;
}
/* Shortcut TOBIT + IRT_NUM <- IRT_INT/IRT_U32 conversion. */
LJFOLD(TOBIT CONV KNUM)
LJFOLDF(simplify_tobit_conv)
{
/* Fold even across PHI to avoid expensive num->int conversions in loop. */
if ((fleft->op2 & IRCONV_SRCMASK) == IRT_INT) {
lua_assert(irt_isnum(fleft->t));
return fleft->op1;
} else if ((fleft->op2 & IRCONV_SRCMASK) == IRT_U32) {
lua_assert(irt_isnum(fleft->t));
fins->o = IR_CONV;
fins->op1 = fleft->op1;
fins->op2 = (IRT_INT<<5)|IRT_U32;
return RETRYFOLD;
}
return NEXTFOLD;
}
/* Shortcut floor/ceil/round + IRT_NUM <- IRT_INT/IRT_U32 conversion. */
LJFOLD(FPMATH CONV IRFPM_FLOOR)
LJFOLD(FPMATH CONV IRFPM_CEIL)
LJFOLD(FPMATH CONV IRFPM_TRUNC)
LJFOLDF(simplify_floor_conv)
{
if ((fleft->op2 & IRCONV_SRCMASK) == IRT_INT ||
(fleft->op2 & IRCONV_SRCMASK) == IRT_U32)
return LEFTFOLD;
return NEXTFOLD;
}
/* Strength reduction of widening. */
LJFOLD(CONV any IRCONV_I64_INT)
LJFOLD(CONV any IRCONV_U64_INT)
LJFOLDF(simplify_conv_sext)
{
IRRef ref = fins->op1;
int64_t ofs = 0;
if (!(fins->op2 & IRCONV_SEXT))
return NEXTFOLD;
PHIBARRIER(fleft);
if (fleft->o == IR_XLOAD && (irt_isu8(fleft->t) || irt_isu16(fleft->t)))
goto ok_reduce;
if (fleft->o == IR_ADD && irref_isk(fleft->op2)) {
ofs = (int64_t)IR(fleft->op2)->i;
ref = fleft->op1;
}
/* Use scalar evolution analysis results to strength-reduce sign-extension. */
if (ref == J->scev.idx) {
IRRef lo = J->scev.dir ? J->scev.start : J->scev.stop;
lua_assert(irt_isint(J->scev.t));
if (lo && IR(lo)->i + ofs >= 0) {
ok_reduce:
#if LJ_TARGET_X64
/* Eliminate widening. All 32 bit ops do an implicit zero-extension. */
return LEFTFOLD;
#else
/* Reduce to a (cheaper) zero-extension. */
fins->op2 &= ~IRCONV_SEXT;
return RETRYFOLD;
#endif
}
}
return NEXTFOLD;
}
/* Strength reduction of narrowing. */
LJFOLD(CONV ADD IRCONV_INT_I64)
LJFOLD(CONV SUB IRCONV_INT_I64)
LJFOLD(CONV MUL IRCONV_INT_I64)
LJFOLD(CONV ADD IRCONV_INT_U64)
LJFOLD(CONV SUB IRCONV_INT_U64)
LJFOLD(CONV MUL IRCONV_INT_U64)
LJFOLD(CONV ADD IRCONV_U32_I64)
LJFOLD(CONV SUB IRCONV_U32_I64)
LJFOLD(CONV MUL IRCONV_U32_I64)
LJFOLD(CONV ADD IRCONV_U32_U64)
LJFOLD(CONV SUB IRCONV_U32_U64)
LJFOLD(CONV MUL IRCONV_U32_U64)
LJFOLDF(simplify_conv_narrow)
{
IROp op = (IROp)fleft->o;
IRType t = irt_type(fins->t);
IRRef op1 = fleft->op1, op2 = fleft->op2, mode = fins->op2;
PHIBARRIER(fleft);
op1 = emitir(IRTI(IR_CONV), op1, mode);
op2 = emitir(IRTI(IR_CONV), op2, mode);
fins->ot = IRT(op, t);
fins->op1 = op1;
fins->op2 = op2;
return RETRYFOLD;
}
/* Special CSE rule for CONV. */
LJFOLD(CONV any any)
LJFOLDF(cse_conv)
{
if (LJ_LIKELY(J->flags & JIT_F_OPT_CSE)) {
IRRef op1 = fins->op1, op2 = (fins->op2 & IRCONV_MODEMASK);
uint8_t guard = irt_isguard(fins->t);
IRRef ref = J->chain[IR_CONV];
while (ref > op1) {
IRIns *ir = IR(ref);
/* Commoning with stronger checks is ok. */
if (ir->op1 == op1 && (ir->op2 & IRCONV_MODEMASK) == op2 &&
irt_isguard(ir->t) >= guard)
return ref;
ref = ir->prev;
}
}
return EMITFOLD; /* No fallthrough to regular CSE. */
}
/* FP conversion narrowing. */
LJFOLD(TOBIT ADD KNUM)
LJFOLD(TOBIT SUB KNUM)
LJFOLD(CONV ADD IRCONV_INT_NUM)
LJFOLD(CONV SUB IRCONV_INT_NUM)
LJFOLD(CONV ADD IRCONV_I64_NUM)
LJFOLD(CONV SUB IRCONV_I64_NUM)
LJFOLDF(narrow_convert)
{
PHIBARRIER(fleft);
/* Narrowing ignores PHIs and repeating it inside the loop is not useful. */
if (J->chain[IR_LOOP])
return NEXTFOLD;
lua_assert(fins->o != IR_CONV || (fins->op2&IRCONV_CONVMASK) != IRCONV_TOBIT);
return lj_opt_narrow_convert(J);
}
/* -- Integer algebraic simplifications ----------------------------------- */
LJFOLD(ADD any KINT)
LJFOLD(ADDOV any KINT)
LJFOLD(SUBOV any KINT)
LJFOLDF(simplify_intadd_k)
{
if (fright->i == 0) /* i o 0 ==> i */
return LEFTFOLD;
return NEXTFOLD;
}
LJFOLD(MULOV any KINT)
LJFOLDF(simplify_intmul_k)
{
if (fright->i == 0) /* i * 0 ==> 0 */
return RIGHTFOLD;
if (fright->i == 1) /* i * 1 ==> i */
return LEFTFOLD;
if (fright->i == 2) { /* i * 2 ==> i + i */
fins->o = IR_ADDOV;
fins->op2 = fins->op1;
return RETRYFOLD;
}
return NEXTFOLD;
}
LJFOLD(SUB any KINT)
LJFOLDF(simplify_intsub_k)
{
if (fright->i == 0) /* i - 0 ==> i */
return LEFTFOLD;
fins->o = IR_ADD; /* i - k ==> i + (-k) */
fins->op2 = (IRRef1)lj_ir_kint(J, -fright->i); /* Overflow for -2^31 ok. */
return RETRYFOLD;
}
LJFOLD(SUB KINT any)
LJFOLD(SUB KINT64 any)
LJFOLDF(simplify_intsub_kleft)
{
if (fleft->o == IR_KINT ? (fleft->i == 0) : (ir_kint64(fleft)->u64 == 0)) {
fins->o = IR_NEG; /* 0 - i ==> -i */
fins->op1 = fins->op2;
return RETRYFOLD;
}
return NEXTFOLD;
}
LJFOLD(ADD any KINT64)
LJFOLDF(simplify_intadd_k64)
{
if (ir_kint64(fright)->u64 == 0) /* i + 0 ==> i */
return LEFTFOLD;
return NEXTFOLD;
}
LJFOLD(SUB any KINT64)
LJFOLDF(simplify_intsub_k64)
{
uint64_t k = ir_kint64(fright)->u64;
if (k == 0) /* i - 0 ==> i */
return LEFTFOLD;
fins->o = IR_ADD; /* i - k ==> i + (-k) */
fins->op2 = (IRRef1)lj_ir_kint64(J, (uint64_t)-(int64_t)k);
return RETRYFOLD;
}
static TRef simplify_intmul_k(jit_State *J, int32_t k)
{
/* Note: many more simplifications are possible, e.g. 2^k1 +- 2^k2.
** But this is mainly intended for simple address arithmetic.
** Also it's easier for the backend to optimize the original multiplies.
*/
if (k == 1) { /* i * 1 ==> i */
return LEFTFOLD;
} else if ((k & (k-1)) == 0) { /* i * 2^k ==> i << k */
fins->o = IR_BSHL;
fins->op2 = lj_ir_kint(J, lj_fls((uint32_t)k));
return RETRYFOLD;
}
return NEXTFOLD;
}
LJFOLD(MUL any KINT)
LJFOLDF(simplify_intmul_k32)
{
if (fright->i == 0) /* i * 0 ==> 0 */
return INTFOLD(0);
else if (fright->i > 0)
return simplify_intmul_k(J, fright->i);
return NEXTFOLD;
}
LJFOLD(MUL any KINT64)
LJFOLDF(simplify_intmul_k64)
{
if (ir_kint64(fright)->u64 == 0) /* i * 0 ==> 0 */
return INT64FOLD(0);
#if LJ_64
/* NYI: SPLIT for BSHL and 32 bit backend support. */
else if (ir_kint64(fright)->u64 < 0x80000000u)
return simplify_intmul_k(J, (int32_t)ir_kint64(fright)->u64);
#endif
return NEXTFOLD;
}
LJFOLD(MOD any KINT)
LJFOLDF(simplify_intmod_k)
{
int32_t k = fright->i;
lua_assert(k != 0);
if (k > 0 && (k & (k-1)) == 0) { /* i % (2^k) ==> i & (2^k-1) */
fins->o = IR_BAND;
fins->op2 = lj_ir_kint(J, k-1);
return RETRYFOLD;
}
return NEXTFOLD;
}
LJFOLD(MOD KINT any)
LJFOLDF(simplify_intmod_kleft)
{
if (fleft->i == 0)
return INTFOLD(0);
return NEXTFOLD;
}
LJFOLD(SUB any any)
LJFOLD(SUBOV any any)
LJFOLDF(simplify_intsub)
{
if (fins->op1 == fins->op2 && !irt_isnum(fins->t)) /* i - i ==> 0 */
return irt_is64(fins->t) ? INT64FOLD(0) : INTFOLD(0);
return NEXTFOLD;
}
LJFOLD(SUB ADD any)
LJFOLDF(simplify_intsubadd_leftcancel)
{
if (!irt_isnum(fins->t)) {
PHIBARRIER(fleft);
if (fins->op2 == fleft->op1) /* (i + j) - i ==> j */
return fleft->op2;
if (fins->op2 == fleft->op2) /* (i + j) - j ==> i */
return fleft->op1;
}
return NEXTFOLD;
}
LJFOLD(SUB SUB any)
LJFOLDF(simplify_intsubsub_leftcancel)
{
if (!irt_isnum(fins->t)) {
PHIBARRIER(fleft);
if (fins->op2 == fleft->op1) { /* (i - j) - i ==> 0 - j */
fins->op1 = (IRRef1)lj_ir_kint(J, 0);
fins->op2 = fleft->op2;
return RETRYFOLD;
}
}
return NEXTFOLD;
}
LJFOLD(SUB any SUB)
LJFOLDF(simplify_intsubsub_rightcancel)
{
if (!irt_isnum(fins->t)) {
PHIBARRIER(fright);
if (fins->op1 == fright->op1) /* i - (i - j) ==> j */
return fright->op2;
}
return NEXTFOLD;
}
LJFOLD(SUB any ADD)
LJFOLDF(simplify_intsubadd_rightcancel)
{
if (!irt_isnum(fins->t)) {
PHIBARRIER(fright);
if (fins->op1 == fright->op1) { /* i - (i + j) ==> 0 - j */
fins->op2 = fright->op2;
fins->op1 = (IRRef1)lj_ir_kint(J, 0);
return RETRYFOLD;
}
if (fins->op1 == fright->op2) { /* i - (j + i) ==> 0 - j */
fins->op2 = fright->op1;
fins->op1 = (IRRef1)lj_ir_kint(J, 0);
return RETRYFOLD;
}
}
return NEXTFOLD;
}
LJFOLD(SUB ADD ADD)
LJFOLDF(simplify_intsubaddadd_cancel)
{
if (!irt_isnum(fins->t)) {
PHIBARRIER(fleft);
PHIBARRIER(fright);
if (fleft->op1 == fright->op1) { /* (i + j1) - (i + j2) ==> j1 - j2 */
fins->op1 = fleft->op2;
fins->op2 = fright->op2;
return RETRYFOLD;
}
if (fleft->op1 == fright->op2) { /* (i + j1) - (j2 + i) ==> j1 - j2 */
fins->op1 = fleft->op2;
fins->op2 = fright->op1;
return RETRYFOLD;
}
if (fleft->op2 == fright->op1) { /* (j1 + i) - (i + j2) ==> j1 - j2 */
fins->op1 = fleft->op1;
fins->op2 = fright->op2;
return RETRYFOLD;
}
if (fleft->op2 == fright->op2) { /* (j1 + i) - (j2 + i) ==> j1 - j2 */
fins->op1 = fleft->op1;
fins->op2 = fright->op1;
return RETRYFOLD;
}
}
return NEXTFOLD;
}
LJFOLD(BAND any KINT)
LJFOLD(BAND any KINT64)
LJFOLDF(simplify_band_k)
{
int64_t k = fright->o == IR_KINT ? (int64_t)fright->i :
(int64_t)ir_k64(fright)->u64;
if (k == 0) /* i & 0 ==> 0 */
return RIGHTFOLD;
if (k == -1) /* i & -1 ==> i */
return LEFTFOLD;
return NEXTFOLD;
}
LJFOLD(BOR any KINT)
LJFOLD(BOR any KINT64)
LJFOLDF(simplify_bor_k)
{
int64_t k = fright->o == IR_KINT ? (int64_t)fright->i :
(int64_t)ir_k64(fright)->u64;
if (k == 0) /* i | 0 ==> i */
return LEFTFOLD;
if (k == -1) /* i | -1 ==> -1 */
return RIGHTFOLD;
return NEXTFOLD;
}
LJFOLD(BXOR any KINT)
LJFOLD(BXOR any KINT64)
LJFOLDF(simplify_bxor_k)
{
int64_t k = fright->o == IR_KINT ? (int64_t)fright->i :
(int64_t)ir_k64(fright)->u64;
if (k == 0) /* i xor 0 ==> i */
return LEFTFOLD;
if (k == -1) { /* i xor -1 ==> ~i */
fins->o = IR_BNOT;
fins->op2 = 0;
return RETRYFOLD;
}
return NEXTFOLD;
}
LJFOLD(BSHL any KINT)
LJFOLD(BSHR any KINT)
LJFOLD(BSAR any KINT)
LJFOLD(BROL any KINT)
LJFOLD(BROR any KINT)
LJFOLDF(simplify_shift_ik)
{
int32_t mask = irt_is64(fins->t) ? 63 : 31;
int32_t k = (fright->i & mask);
if (k == 0) /* i o 0 ==> i */
return LEFTFOLD;
if (k == 1 && fins->o == IR_BSHL) { /* i << 1 ==> i + i */
fins->o = IR_ADD;
fins->op2 = fins->op1;
return RETRYFOLD;
}
if (k != fright->i) { /* i o k ==> i o (k & mask) */
fins->op2 = (IRRef1)lj_ir_kint(J, k);
return RETRYFOLD;
}
#ifndef LJ_TARGET_UNIFYROT
if (fins->o == IR_BROR) { /* bror(i, k) ==> brol(i, (-k)&mask) */
fins->o = IR_BROL;
fins->op2 = (IRRef1)lj_ir_kint(J, (-k)&mask);
return RETRYFOLD;
}
#endif
return NEXTFOLD;
}
LJFOLD(BSHL any BAND)
LJFOLD(BSHR any BAND)
LJFOLD(BSAR any BAND)
LJFOLD(BROL any BAND)
LJFOLD(BROR any BAND)
LJFOLDF(simplify_shift_andk)
{
IRIns *irk = IR(fright->op2);
PHIBARRIER(fright);
if ((fins->o < IR_BROL ? LJ_TARGET_MASKSHIFT : LJ_TARGET_MASKROT) &&
irk->o == IR_KINT) { /* i o (j & mask) ==> i o j */
int32_t mask = irt_is64(fins->t) ? 63 : 31;
int32_t k = irk->i & mask;
if (k == mask) {
fins->op2 = fright->op1;
return RETRYFOLD;
}
}
return NEXTFOLD;
}
LJFOLD(BSHL KINT any)
LJFOLD(BSHR KINT any)
LJFOLD(BSHL KINT64 any)
LJFOLD(BSHR KINT64 any)
LJFOLDF(simplify_shift1_ki)
{
int64_t k = fleft->o == IR_KINT ? (int64_t)fleft->i :
(int64_t)ir_k64(fleft)->u64;
if (k == 0) /* 0 o i ==> 0 */
return LEFTFOLD;
return NEXTFOLD;
}
LJFOLD(BSAR KINT any)
LJFOLD(BROL KINT any)
LJFOLD(BROR KINT any)
LJFOLD(BSAR KINT64 any)
LJFOLD(BROL KINT64 any)
LJFOLD(BROR KINT64 any)
LJFOLDF(simplify_shift2_ki)
{
int64_t k = fleft->o == IR_KINT ? (int64_t)fleft->i :
(int64_t)ir_k64(fleft)->u64;
if (k == 0 || k == -1) /* 0 o i ==> 0; -1 o i ==> -1 */
return LEFTFOLD;
return NEXTFOLD;
}
LJFOLD(BSHL BAND KINT)
LJFOLD(BSHR BAND KINT)
LJFOLD(BROL BAND KINT)
LJFOLD(BROR BAND KINT)
LJFOLDF(simplify_shiftk_andk)
{
IRIns *irk = IR(fleft->op2);
PHIBARRIER(fleft);
if (irk->o == IR_KINT) { /* (i & k1) o k2 ==> (i o k2) & (k1 o k2) */
int32_t k = kfold_intop(irk->i, fright->i, (IROp)fins->o);
fins->op1 = fleft->op1;
fins->op1 = (IRRef1)lj_opt_fold(J);
fins->op2 = (IRRef1)lj_ir_kint(J, k);
fins->ot = IRTI(IR_BAND);
return RETRYFOLD;
}
return NEXTFOLD;
}
LJFOLD(BAND BSHL KINT)
LJFOLD(BAND BSHR KINT)
LJFOLDF(simplify_andk_shiftk)
{
IRIns *irk = IR(fleft->op2);
if (irk->o == IR_KINT &&
kfold_intop(-1, irk->i, (IROp)fleft->o) == fright->i)
return LEFTFOLD; /* (i o k1) & k2 ==> i, if (-1 o k1) == k2 */
return NEXTFOLD;
}
/* -- Reassociation ------------------------------------------------------- */
LJFOLD(ADD ADD KINT)
LJFOLD(MUL MUL KINT)
LJFOLD(BAND BAND KINT)
LJFOLD(BOR BOR KINT)
LJFOLD(BXOR BXOR KINT)
LJFOLDF(reassoc_intarith_k)
{
IRIns *irk = IR(fleft->op2);
if (irk->o == IR_KINT) {
int32_t k = kfold_intop(irk->i, fright->i, (IROp)fins->o);
if (k == irk->i) /* (i o k1) o k2 ==> i o k1, if (k1 o k2) == k1. */
return LEFTFOLD;
PHIBARRIER(fleft);
fins->op1 = fleft->op1;
fins->op2 = (IRRef1)lj_ir_kint(J, k);
return RETRYFOLD; /* (i o k1) o k2 ==> i o (k1 o k2) */
}
return NEXTFOLD;
}
LJFOLD(ADD ADD KINT64)
LJFOLD(MUL MUL KINT64)
LJFOLD(BAND BAND KINT64)
LJFOLD(BOR BOR KINT64)
LJFOLD(BXOR BXOR KINT64)
LJFOLDF(reassoc_intarith_k64)
{
#if LJ_HASFFI || LJ_64
IRIns *irk = IR(fleft->op2);
if (irk->o == IR_KINT64) {
uint64_t k = kfold_int64arith(ir_k64(irk)->u64,
ir_k64(fright)->u64, (IROp)fins->o);
PHIBARRIER(fleft);
fins->op1 = fleft->op1;
fins->op2 = (IRRef1)lj_ir_kint64(J, k);
return RETRYFOLD; /* (i o k1) o k2 ==> i o (k1 o k2) */
}
return NEXTFOLD;
#else
UNUSED(J); lua_assert(0); return FAILFOLD;
#endif
}
LJFOLD(MIN MIN any)
LJFOLD(MAX MAX any)
LJFOLD(BAND BAND any)
LJFOLD(BOR BOR any)
LJFOLDF(reassoc_dup)
{
if (fins->op2 == fleft->op1 || fins->op2 == fleft->op2)
return LEFTFOLD; /* (a o b) o a ==> a o b; (a o b) o b ==> a o b */
return NEXTFOLD;
}
LJFOLD(BXOR BXOR any)
LJFOLDF(reassoc_bxor)
{
PHIBARRIER(fleft);
if (fins->op2 == fleft->op1) /* (a xor b) xor a ==> b */
return fleft->op2;
if (fins->op2 == fleft->op2) /* (a xor b) xor b ==> a */
return fleft->op1;
return NEXTFOLD;
}
LJFOLD(BSHL BSHL KINT)
LJFOLD(BSHR BSHR KINT)
LJFOLD(BSAR BSAR KINT)
LJFOLD(BROL BROL KINT)
LJFOLD(BROR BROR KINT)
LJFOLDF(reassoc_shift)
{
IRIns *irk = IR(fleft->op2);
PHIBARRIER(fleft); /* The (shift any KINT) rule covers k2 == 0 and more. */
if (irk->o == IR_KINT) { /* (i o k1) o k2 ==> i o (k1 + k2) */
int32_t mask = irt_is64(fins->t) ? 63 : 31;
int32_t k = (irk->i & mask) + (fright->i & mask);
if (k > mask) { /* Combined shift too wide? */
if (fins->o == IR_BSHL || fins->o == IR_BSHR)
return mask == 31 ? INTFOLD(0) : INT64FOLD(0);
else if (fins->o == IR_BSAR)
k = mask;
else
k &= mask;
}
fins->op1 = fleft->op1;
fins->op2 = (IRRef1)lj_ir_kint(J, k);
return RETRYFOLD;
}
return NEXTFOLD;
}
LJFOLD(MIN MIN KNUM)
LJFOLD(MAX MAX KNUM)
LJFOLD(MIN MIN KINT)
LJFOLD(MAX MAX KINT)
LJFOLDF(reassoc_minmax_k)
{
IRIns *irk = IR(fleft->op2);
if (irk->o == IR_KNUM) {
lua_Number a = ir_knum(irk)->n;
lua_Number y = lj_vm_foldarith(a, knumright, fins->o - IR_ADD);
if (a == y) /* (x o k1) o k2 ==> x o k1, if (k1 o k2) == k1. */
return LEFTFOLD;
PHIBARRIER(fleft);
fins->op1 = fleft->op1;
fins->op2 = (IRRef1)lj_ir_knum(J, y);
return RETRYFOLD; /* (x o k1) o k2 ==> x o (k1 o k2) */
} else if (irk->o == IR_KINT) {
int32_t a = irk->i;
int32_t y = kfold_intop(a, fright->i, fins->o);
if (a == y) /* (x o k1) o k2 ==> x o k1, if (k1 o k2) == k1. */
return LEFTFOLD;
PHIBARRIER(fleft);
fins->op1 = fleft->op1;
fins->op2 = (IRRef1)lj_ir_kint(J, y);
return RETRYFOLD; /* (x o k1) o k2 ==> x o (k1 o k2) */
}
return NEXTFOLD;
}
LJFOLD(MIN MAX any)
LJFOLD(MAX MIN any)
LJFOLDF(reassoc_minmax_left)
{
if (fins->op2 == fleft->op1 || fins->op2 == fleft->op2)
return RIGHTFOLD; /* (b o1 a) o2 b ==> b; (a o1 b) o2 b ==> b */
return NEXTFOLD;
}
LJFOLD(MIN any MAX)
LJFOLD(MAX any MIN)
LJFOLDF(reassoc_minmax_right)
{
if (fins->op1 == fright->op1 || fins->op1 == fright->op2)
return LEFTFOLD; /* a o2 (a o1 b) ==> a; a o2 (b o1 a) ==> a */
return NEXTFOLD;
}
/* -- Array bounds check elimination -------------------------------------- */
/* Eliminate ABC across PHIs to handle t[i-1] forwarding case.
** ABC(asize, (i+k)+(-k)) ==> ABC(asize, i), but only if it already exists.
** Could be generalized to (i+k1)+k2 ==> i+(k1+k2), but needs better disambig.
*/
LJFOLD(ABC any ADD)
LJFOLDF(abc_fwd)
{
if (LJ_LIKELY(J->flags & JIT_F_OPT_ABC)) {
if (irref_isk(fright->op2)) {
IRIns *add2 = IR(fright->op1);
if (add2->o == IR_ADD && irref_isk(add2->op2) &&
IR(fright->op2)->i == -IR(add2->op2)->i) {
IRRef ref = J->chain[IR_ABC];
IRRef lim = add2->op1;
if (fins->op1 > lim) lim = fins->op1;
while (ref > lim) {
IRIns *ir = IR(ref);
if (ir->op1 == fins->op1 && ir->op2 == add2->op1)
return DROPFOLD;
ref = ir->prev;
}
}
}
}
return NEXTFOLD;
}
/* Eliminate ABC for constants.
** ABC(asize, k1), ABC(asize k2) ==> ABC(asize, max(k1, k2))
** Drop second ABC if k2 is lower. Otherwise patch first ABC with k2.
*/
LJFOLD(ABC any KINT)
LJFOLDF(abc_k)
{
if (LJ_LIKELY(J->flags & JIT_F_OPT_ABC)) {
IRRef ref = J->chain[IR_ABC];
IRRef asize = fins->op1;
while (ref > asize) {
IRIns *ir = IR(ref);
if (ir->op1 == asize && irref_isk(ir->op2)) {
int32_t k = IR(ir->op2)->i;
if (fright->i > k)
ir->op2 = fins->op2;
return DROPFOLD;
}
ref = ir->prev;
}
return EMITFOLD; /* Already performed CSE. */
}
return NEXTFOLD;
}
/* Eliminate invariant ABC inside loop. */
LJFOLD(ABC any any)
LJFOLDF(abc_invar)
{
/* Invariant ABC marked as PTR. Drop if op1 is invariant, too. */
if (!irt_isint(fins->t) && fins->op1 < J->chain[IR_LOOP] &&
!irt_isphi(IR(fins->op1)->t))
return DROPFOLD;
return NEXTFOLD;
}
/* -- Commutativity ------------------------------------------------------- */
/* The refs of commutative ops are canonicalized. Lower refs go to the right.
** Rationale behind this:
** - It (also) moves constants to the right.
** - It reduces the number of FOLD rules (e.g. (BOR any KINT) suffices).
** - It helps CSE to find more matches.
** - The assembler generates better code with constants at the right.
*/
LJFOLD(ADD any any)
LJFOLD(MUL any any)
LJFOLD(ADDOV any any)
LJFOLD(MULOV any any)
LJFOLDF(comm_swap)
{
if (fins->op1 < fins->op2) { /* Move lower ref to the right. */
IRRef1 tmp = fins->op1;
fins->op1 = fins->op2;
fins->op2 = tmp;
return RETRYFOLD;
}
return NEXTFOLD;
}
LJFOLD(EQ any any)
LJFOLD(NE any any)
LJFOLDF(comm_equal)
{
/* For non-numbers only: x == x ==> drop; x ~= x ==> fail */
if (fins->op1 == fins->op2 && !irt_isnum(fins->t))
return CONDFOLD(fins->o == IR_EQ);
return fold_comm_swap(J);
}
LJFOLD(LT any any)
LJFOLD(GE any any)
LJFOLD(LE any any)
LJFOLD(GT any any)
LJFOLD(ULT any any)
LJFOLD(UGE any any)
LJFOLD(ULE any any)
LJFOLD(UGT any any)
LJFOLDF(comm_comp)
{
/* For non-numbers only: x <=> x ==> drop; x <> x ==> fail */
if (fins->op1 == fins->op2 && !irt_isnum(fins->t))
return CONDFOLD((fins->o ^ (fins->o >> 1)) & 1);
if (fins->op1 < fins->op2) { /* Move lower ref to the right. */
IRRef1 tmp = fins->op1;
fins->op1 = fins->op2;
fins->op2 = tmp;
fins->o ^= 3; /* GT <-> LT, GE <-> LE, does not affect U */
return RETRYFOLD;
}
return NEXTFOLD;
}
LJFOLD(BAND any any)
LJFOLD(BOR any any)
LJFOLD(MIN any any)
LJFOLD(MAX any any)
LJFOLDF(comm_dup)
{
if (fins->op1 == fins->op2) /* x o x ==> x */
return LEFTFOLD;
return fold_comm_swap(J);
}
LJFOLD(BXOR any any)
LJFOLDF(comm_bxor)
{
if (fins->op1 == fins->op2) /* i xor i ==> 0 */
return irt_is64(fins->t) ? INT64FOLD(0) : INTFOLD(0);
return fold_comm_swap(J);
}
/* -- Simplification of compound expressions ------------------------------ */
static TRef kfold_xload(jit_State *J, IRIns *ir, const void *p)
{
int32_t k;
switch (irt_type(ir->t)) {
case IRT_NUM: return lj_ir_knum_u64(J, *(uint64_t *)p);
case IRT_I8: k = (int32_t)*(int8_t *)p; break;
case IRT_U8: k = (int32_t)*(uint8_t *)p; break;
case IRT_I16: k = (int32_t)(int16_t)lj_getu16(p); break;
case IRT_U16: k = (int32_t)(uint16_t)lj_getu16(p); break;
case IRT_INT: case IRT_U32: k = (int32_t)lj_getu32(p); break;
case IRT_I64: case IRT_U64: return lj_ir_kint64(J, *(uint64_t *)p);
default: return 0;
}
return lj_ir_kint(J, k);
}
/* Turn: string.sub(str, a, b) == kstr
** into: string.byte(str, a) == string.byte(kstr, 1) etc.
** Note: this creates unaligned XLOADs on x86/x64.
*/
LJFOLD(EQ SNEW KGC)
LJFOLD(NE SNEW KGC)
LJFOLDF(merge_eqne_snew_kgc)
{
GCstr *kstr = ir_kstr(fright);
int32_t len = (int32_t)kstr->len;
lua_assert(irt_isstr(fins->t));
#if LJ_TARGET_UNALIGNED
#define FOLD_SNEW_MAX_LEN 4 /* Handle string lengths 0, 1, 2, 3, 4. */
#define FOLD_SNEW_TYPE8 IRT_I8 /* Creates shorter immediates. */
#else
#define FOLD_SNEW_MAX_LEN 1 /* Handle string lengths 0 or 1. */
#define FOLD_SNEW_TYPE8 IRT_U8 /* Prefer unsigned loads. */
#endif
PHIBARRIER(fleft);
if (len <= FOLD_SNEW_MAX_LEN) {
IROp op = (IROp)fins->o;
IRRef strref = fleft->op1;
if (IR(strref)->o != IR_STRREF)
return NEXTFOLD;
if (op == IR_EQ) {
emitir(IRTGI(IR_EQ), fleft->op2, lj_ir_kint(J, len));
/* Caveat: fins/fleft/fright is no longer valid after emitir. */
} else {
/* NE is not expanded since this would need an OR of two conds. */
if (!irref_isk(fleft->op2)) /* Only handle the constant length case. */
return NEXTFOLD;
if (IR(fleft->op2)->i != len)
return DROPFOLD;
}
if (len > 0) {
/* A 4 byte load for length 3 is ok -- all strings have an extra NUL. */
uint16_t ot = (uint16_t)(len == 1 ? IRT(IR_XLOAD, FOLD_SNEW_TYPE8) :
len == 2 ? IRT(IR_XLOAD, IRT_U16) :
IRTI(IR_XLOAD));
TRef tmp = emitir(ot, strref,
IRXLOAD_READONLY | (len > 1 ? IRXLOAD_UNALIGNED : 0));
TRef val = kfold_xload(J, IR(tref_ref(tmp)), strdata(kstr));
if (len == 3)
tmp = emitir(IRTI(IR_BAND), tmp,
lj_ir_kint(J, LJ_ENDIAN_SELECT(0x00ffffff, 0xffffff00)));
fins->op1 = (IRRef1)tmp;
fins->op2 = (IRRef1)val;
fins->ot = (IROpT)IRTGI(op);
return RETRYFOLD;
} else {
return DROPFOLD;
}
}
return NEXTFOLD;
}
/* -- Loads --------------------------------------------------------------- */
/* Loads cannot be folded or passed on to CSE in general.
** Alias analysis is needed to check for forwarding opportunities.
**
** Caveat: *all* loads must be listed here or they end up at CSE!
*/
LJFOLD(ALOAD any)
LJFOLDX(lj_opt_fwd_aload)
/* From HREF fwd (see below). Must eliminate, not supported by fwd/backend. */
LJFOLD(HLOAD KKPTR)
LJFOLDF(kfold_hload_kkptr)
{
UNUSED(J);
lua_assert(ir_kptr(fleft) == niltvg(J2G(J)));
return TREF_NIL;
}
LJFOLD(HLOAD any)
LJFOLDX(lj_opt_fwd_hload)
LJFOLD(ULOAD any)
LJFOLDX(lj_opt_fwd_uload)
LJFOLD(CALLL any IRCALL_lj_tab_len)
LJFOLDX(lj_opt_fwd_tab_len)
/* Upvalue refs are really loads, but there are no corresponding stores.
** So CSE is ok for them, except for UREFO across a GC step (see below).
** If the referenced function is const, its upvalue addresses are const, too.
** This can be used to improve CSE by looking for the same address,
** even if the upvalues originate from a different function.
*/
LJFOLD(UREFO KGC any)
LJFOLD(UREFC KGC any)
LJFOLDF(cse_uref)
{
if (LJ_LIKELY(J->flags & JIT_F_OPT_CSE)) {
IRRef ref = J->chain[fins->o];
GCfunc *fn = ir_kfunc(fleft);
GCupval *uv = gco2uv(gcref(fn->l.uvptr[(fins->op2 >> 8)]));
while (ref > 0) {
IRIns *ir = IR(ref);
if (irref_isk(ir->op1)) {
GCfunc *fn2 = ir_kfunc(IR(ir->op1));
if (gco2uv(gcref(fn2->l.uvptr[(ir->op2 >> 8)])) == uv) {
if (fins->o == IR_UREFO && gcstep_barrier(J, ref))
break;
return ref;
}
}
ref = ir->prev;
}
}
return EMITFOLD;
}
LJFOLD(HREFK any any)
LJFOLDX(lj_opt_fwd_hrefk)
LJFOLD(HREF TNEW any)
LJFOLDF(fwd_href_tnew)
{
if (lj_opt_fwd_href_nokey(J))
return lj_ir_kkptr(J, niltvg(J2G(J)));
return NEXTFOLD;
}
LJFOLD(HREF TDUP KPRI)
LJFOLD(HREF TDUP KGC)
LJFOLD(HREF TDUP KNUM)
LJFOLDF(fwd_href_tdup)
{
TValue keyv;
lj_ir_kvalue(J->L, &keyv, fright);
if (lj_tab_get(J->L, ir_ktab(IR(fleft->op1)), &keyv) == niltvg(J2G(J)) &&
lj_opt_fwd_href_nokey(J))
return lj_ir_kkptr(J, niltvg(J2G(J)));
return NEXTFOLD;
}
/* We can safely FOLD/CSE array/hash refs and field loads, since there
** are no corresponding stores. But we need to check for any NEWREF with
** an aliased table, as it may invalidate all of the pointers and fields.
** Only HREF needs the NEWREF check -- AREF and HREFK already depend on
** FLOADs. And NEWREF itself is treated like a store (see below).
*/
LJFOLD(FLOAD TNEW IRFL_TAB_ASIZE)
LJFOLDF(fload_tab_tnew_asize)
{
if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && lj_opt_fwd_tptr(J, fins->op1))
return INTFOLD(fleft->op1);
return NEXTFOLD;
}
LJFOLD(FLOAD TNEW IRFL_TAB_HMASK)
LJFOLDF(fload_tab_tnew_hmask)
{
if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && lj_opt_fwd_tptr(J, fins->op1))
return INTFOLD((1 << fleft->op2)-1);
return NEXTFOLD;
}
LJFOLD(FLOAD TDUP IRFL_TAB_ASIZE)
LJFOLDF(fload_tab_tdup_asize)
{
if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && lj_opt_fwd_tptr(J, fins->op1))
return INTFOLD((int32_t)ir_ktab(IR(fleft->op1))->asize);
return NEXTFOLD;
}
LJFOLD(FLOAD TDUP IRFL_TAB_HMASK)
LJFOLDF(fload_tab_tdup_hmask)
{
if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && lj_opt_fwd_tptr(J, fins->op1))
return INTFOLD((int32_t)ir_ktab(IR(fleft->op1))->hmask);
return NEXTFOLD;
}
LJFOLD(HREF any any)
LJFOLD(FLOAD any IRFL_TAB_ARRAY)
LJFOLD(FLOAD any IRFL_TAB_NODE)
LJFOLD(FLOAD any IRFL_TAB_ASIZE)
LJFOLD(FLOAD any IRFL_TAB_HMASK)
LJFOLDF(fload_tab_ah)
{
TRef tr = lj_opt_cse(J);
return lj_opt_fwd_tptr(J, tref_ref(tr)) ? tr : EMITFOLD;
}
/* Strings are immutable, so we can safely FOLD/CSE the related FLOAD. */
LJFOLD(FLOAD KGC IRFL_STR_LEN)
LJFOLDF(fload_str_len_kgc)
{
if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD))
return INTFOLD((int32_t)ir_kstr(fleft)->len);
return NEXTFOLD;
}
LJFOLD(FLOAD SNEW IRFL_STR_LEN)
LJFOLDF(fload_str_len_snew)
{
if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD)) {
PHIBARRIER(fleft);
return fleft->op2;
}
return NEXTFOLD;
}
/* The C type ID of cdata objects is immutable. */
LJFOLD(FLOAD KGC IRFL_CDATA_CTYPEID)
LJFOLDF(fload_cdata_typeid_kgc)
{
if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD))
return INTFOLD((int32_t)ir_kcdata(fleft)->ctypeid);
return NEXTFOLD;
}
/* Get the contents of immutable cdata objects. */
LJFOLD(FLOAD KGC IRFL_CDATA_PTR)
LJFOLD(FLOAD KGC IRFL_CDATA_INT)
LJFOLD(FLOAD KGC IRFL_CDATA_INT64)
LJFOLDF(fload_cdata_int64_kgc)
{
if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD)) {
void *p = cdataptr(ir_kcdata(fleft));
if (irt_is64(fins->t))
return INT64FOLD(*(uint64_t *)p);
else
return INTFOLD(*(int32_t *)p);
}
return NEXTFOLD;
}
LJFOLD(FLOAD CNEW IRFL_CDATA_CTYPEID)
LJFOLD(FLOAD CNEWI IRFL_CDATA_CTYPEID)
LJFOLDF(fload_cdata_typeid_cnew)
{
if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD))
return fleft->op1; /* No PHI barrier needed. CNEW/CNEWI op1 is const. */
return NEXTFOLD;
}
/* Pointer, int and int64 cdata objects are immutable. */
LJFOLD(FLOAD CNEWI IRFL_CDATA_PTR)
LJFOLD(FLOAD CNEWI IRFL_CDATA_INT)
LJFOLD(FLOAD CNEWI IRFL_CDATA_INT64)
LJFOLDF(fload_cdata_ptr_int64_cnew)
{
if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD))
return fleft->op2; /* Fold even across PHI to avoid allocations. */
return NEXTFOLD;
}
LJFOLD(FLOAD any IRFL_STR_LEN)
LJFOLD(FLOAD any IRFL_CDATA_CTYPEID)
LJFOLD(FLOAD any IRFL_CDATA_PTR)
LJFOLD(FLOAD any IRFL_CDATA_INT)
LJFOLD(FLOAD any IRFL_CDATA_INT64)
LJFOLD(VLOAD any any) /* Vararg loads have no corresponding stores. */
LJFOLDX(lj_opt_cse)
/* All other field loads need alias analysis. */
LJFOLD(FLOAD any any)
LJFOLDX(lj_opt_fwd_fload)
/* This is for LOOP only. Recording handles SLOADs internally. */
LJFOLD(SLOAD any any)
LJFOLDF(fwd_sload)
{
if ((fins->op2 & IRSLOAD_FRAME)) {
TRef tr = lj_opt_cse(J);
return tref_ref(tr) < J->chain[IR_RETF] ? EMITFOLD : tr;
} else {
lua_assert(J->slot[fins->op1] != 0);
return J->slot[fins->op1];
}
}
/* Only fold for KKPTR. The pointer _and_ the contents must be const. */
LJFOLD(XLOAD KKPTR any)
LJFOLDF(xload_kptr)
{
TRef tr = kfold_xload(J, fins, ir_kptr(fleft));
return tr ? tr : NEXTFOLD;
}
LJFOLD(XLOAD any any)
LJFOLDX(lj_opt_fwd_xload)
/* -- Write barriers ------------------------------------------------------ */
/* Write barriers are amenable to CSE, but not across any incremental
** GC steps.
**
** The same logic applies to open upvalue references, because a stack
** may be resized during a GC step (not the current stack, but maybe that
** of a coroutine).
*/
LJFOLD(TBAR any)
LJFOLD(OBAR any any)
LJFOLD(UREFO any any)
LJFOLDF(barrier_tab)
{
TRef tr = lj_opt_cse(J);
if (gcstep_barrier(J, tref_ref(tr))) /* CSE across GC step? */
return EMITFOLD; /* Raw emit. Assumes fins is left intact by CSE. */
return tr;
}
LJFOLD(TBAR TNEW)
LJFOLD(TBAR TDUP)
LJFOLDF(barrier_tnew_tdup)
{
/* New tables are always white and never need a barrier. */
if (fins->op1 < J->chain[IR_LOOP]) /* Except across a GC step. */
return NEXTFOLD;
return DROPFOLD;
}
/* -- Stores and allocations ---------------------------------------------- */
/* Stores and allocations cannot be folded or passed on to CSE in general.
** But some stores can be eliminated with dead-store elimination (DSE).
**
** Caveat: *all* stores and allocs must be listed here or they end up at CSE!
*/
LJFOLD(ASTORE any any)
LJFOLD(HSTORE any any)
LJFOLDX(lj_opt_dse_ahstore)
LJFOLD(USTORE any any)
LJFOLDX(lj_opt_dse_ustore)
LJFOLD(FSTORE any any)
LJFOLDX(lj_opt_dse_fstore)
LJFOLD(XSTORE any any)
LJFOLDX(lj_opt_dse_xstore)
LJFOLD(NEWREF any any) /* Treated like a store. */
LJFOLD(CALLS any any)
LJFOLD(CALLL any any) /* Safeguard fallback. */
LJFOLD(CALLXS any any)
LJFOLD(XBAR)
LJFOLD(RETF any any) /* Modifies BASE. */
LJFOLD(TNEW any any)
LJFOLD(TDUP any)
LJFOLD(CNEW any any)
LJFOLD(XSNEW any any)
LJFOLDX(lj_ir_emit)
/* ------------------------------------------------------------------------ */
/* Every entry in the generated hash table is a 32 bit pattern:
**
** xxxxxxxx iiiiiii lllllll rrrrrrrrrr
**
** xxxxxxxx = 8 bit index into fold function table
** iiiiiii = 7 bit folded instruction opcode
** lllllll = 7 bit left instruction opcode
** rrrrrrrrrr = 8 bit right instruction opcode or 10 bits from literal field
*/
#include "lj_folddef.h"
/* ------------------------------------------------------------------------ */
/* Fold IR instruction. */
TRef LJ_FASTCALL lj_opt_fold(jit_State *J)
{
uint32_t key, any;
IRRef ref;
if (LJ_UNLIKELY((J->flags & JIT_F_OPT_MASK) != JIT_F_OPT_DEFAULT)) {
lua_assert(((JIT_F_OPT_FOLD|JIT_F_OPT_FWD|JIT_F_OPT_CSE|JIT_F_OPT_DSE) |
JIT_F_OPT_DEFAULT) == JIT_F_OPT_DEFAULT);
/* Folding disabled? Chain to CSE, but not for loads/stores/allocs. */
if (!(J->flags & JIT_F_OPT_FOLD) && irm_kind(lj_ir_mode[fins->o]) == IRM_N)
return lj_opt_cse(J);
/* No FOLD, forwarding or CSE? Emit raw IR for loads, except for SLOAD. */
if ((J->flags & (JIT_F_OPT_FOLD|JIT_F_OPT_FWD|JIT_F_OPT_CSE)) !=
(JIT_F_OPT_FOLD|JIT_F_OPT_FWD|JIT_F_OPT_CSE) &&
irm_kind(lj_ir_mode[fins->o]) == IRM_L && fins->o != IR_SLOAD)
return lj_ir_emit(J);
/* No FOLD or DSE? Emit raw IR for stores. */
if ((J->flags & (JIT_F_OPT_FOLD|JIT_F_OPT_DSE)) !=
(JIT_F_OPT_FOLD|JIT_F_OPT_DSE) &&
irm_kind(lj_ir_mode[fins->o]) == IRM_S)
return lj_ir_emit(J);
}
/* Fold engine start/retry point. */
retry:
/* Construct key from opcode and operand opcodes (unless literal/none). */
key = ((uint32_t)fins->o << 17);
if (fins->op1 >= J->cur.nk) {
key += (uint32_t)IR(fins->op1)->o << 10;
*fleft = *IR(fins->op1);
}
if (fins->op2 >= J->cur.nk) {
key += (uint32_t)IR(fins->op2)->o;
*fright = *IR(fins->op2);
} else {
key += (fins->op2 & 0x3ffu); /* Literal mask. Must include IRCONV_*MASK. */
}
/* Check for a match in order from most specific to least specific. */
any = 0;
for (;;) {
uint32_t k = key | (any & 0x1ffff);
uint32_t h = fold_hashkey(k);
uint32_t fh = fold_hash[h]; /* Lookup key in semi-perfect hash table. */
if ((fh & 0xffffff) == k || (fh = fold_hash[h+1], (fh & 0xffffff) == k)) {
ref = (IRRef)tref_ref(fold_func[fh >> 24](J));
if (ref != NEXTFOLD)
break;
}
if (any == 0xfffff) /* Exhausted folding. Pass on to CSE. */
return lj_opt_cse(J);
any = (any | (any >> 10)) ^ 0xffc00;
}
/* Return value processing, ordered by frequency. */
if (LJ_LIKELY(ref >= MAX_FOLD))
return TREF(ref, irt_t(IR(ref)->t));
if (ref == RETRYFOLD)
goto retry;
if (ref == KINTFOLD)
return lj_ir_kint(J, fins->i);
if (ref == FAILFOLD)
lj_trace_err(J, LJ_TRERR_GFAIL);
lua_assert(ref == DROPFOLD);
return REF_DROP;
}
/* -- Common-Subexpression Elimination ------------------------------------ */
/* CSE an IR instruction. This is very fast due to the skip-list chains. */
TRef LJ_FASTCALL lj_opt_cse(jit_State *J)
{
/* Avoid narrow to wide store-to-load forwarding stall */
IRRef2 op12 = (IRRef2)fins->op1 + ((IRRef2)fins->op2 << 16);
IROp op = fins->o;
if (LJ_LIKELY(J->flags & JIT_F_OPT_CSE)) {
/* Limited search for same operands in per-opcode chain. */
IRRef ref = J->chain[op];
IRRef lim = fins->op1;
if (fins->op2 > lim) lim = fins->op2; /* Relies on lit < REF_BIAS. */
while (ref > lim) {
if (IR(ref)->op12 == op12)
return TREF(ref, irt_t(IR(ref)->t)); /* Common subexpression found. */
ref = IR(ref)->prev;
}
}
/* Otherwise emit IR (inlined for speed). */
{
IRRef ref = lj_ir_nextins(J);
IRIns *ir = IR(ref);
ir->prev = J->chain[op];
ir->op12 = op12;
J->chain[op] = (IRRef1)ref;
ir->o = fins->o;
J->guardemit.irt |= fins->t.irt;
return TREF(ref, irt_t((ir->t = fins->t)));
}
}
/* CSE with explicit search limit. */
TRef LJ_FASTCALL lj_opt_cselim(jit_State *J, IRRef lim)
{
IRRef ref = J->chain[fins->o];
IRRef2 op12 = (IRRef2)fins->op1 + ((IRRef2)fins->op2 << 16);
while (ref > lim) {
if (IR(ref)->op12 == op12)
return ref;
ref = IR(ref)->prev;
}
return lj_ir_emit(J);
}
/* ------------------------------------------------------------------------ */
#undef IR
#undef fins
#undef fleft
#undef fright
#undef knumleft
#undef knumright
#undef emitir
#endif
|