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
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
|
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mir_graph.h"
#include <inttypes.h>
#include <queue>
#include "base/bit_vector-inl.h"
#include "base/stl_util.h"
#include "compiler_internals.h"
#include "dex_file-inl.h"
#include "dex_instruction-inl.h"
#include "dex/global_value_numbering.h"
#include "dex/quick/dex_file_to_method_inliner_map.h"
#include "dex/quick/dex_file_method_inliner.h"
#include "leb128.h"
#include "pass_driver_me_post_opt.h"
#include "stack.h"
#include "utils/scoped_arena_containers.h"
namespace art {
#define MAX_PATTERN_LEN 5
const char* MIRGraph::extended_mir_op_names_[kMirOpLast - kMirOpFirst] = {
"Phi",
"Copy",
"FusedCmplFloat",
"FusedCmpgFloat",
"FusedCmplDouble",
"FusedCmpgDouble",
"FusedCmpLong",
"Nop",
"OpNullCheck",
"OpRangeCheck",
"OpDivZeroCheck",
"Check1",
"Check2",
"Select",
"ConstVector",
"MoveVector",
"PackedMultiply",
"PackedAddition",
"PackedSubtract",
"PackedShiftLeft",
"PackedSignedShiftRight",
"PackedUnsignedShiftRight",
"PackedAnd",
"PackedOr",
"PackedXor",
"PackedAddReduce",
"PackedReduce",
"PackedSet",
"ReserveVectorRegisters",
"ReturnVectorRegisters",
"MemBarrier",
"PackedArrayGet",
"PackedArrayPut",
};
MIRGraph::MIRGraph(CompilationUnit* cu, ArenaAllocator* arena)
: reg_location_(NULL),
block_id_map_(std::less<unsigned int>(), arena->Adapter()),
cu_(cu),
ssa_base_vregs_(NULL),
ssa_subscripts_(NULL),
vreg_to_ssa_map_(NULL),
ssa_last_defs_(NULL),
is_constant_v_(NULL),
constant_values_(NULL),
use_counts_(arena, 256, kGrowableArrayMisc),
raw_use_counts_(arena, 256, kGrowableArrayMisc),
num_reachable_blocks_(0),
max_num_reachable_blocks_(0),
dfs_order_(NULL),
dfs_post_order_(NULL),
dom_post_order_traversal_(NULL),
topological_order_(nullptr),
topological_order_loop_ends_(nullptr),
topological_order_indexes_(nullptr),
topological_order_loop_head_stack_(nullptr),
i_dom_list_(NULL),
def_block_matrix_(NULL),
temp_scoped_alloc_(),
temp_insn_data_(nullptr),
temp_bit_vector_size_(0u),
temp_bit_vector_(nullptr),
temp_gvn_(),
block_list_(arena, 100, kGrowableArrayBlockList),
try_block_addr_(NULL),
entry_block_(NULL),
exit_block_(NULL),
num_blocks_(0),
current_code_item_(NULL),
dex_pc_to_block_map_(arena, 0, kGrowableArrayMisc),
m_units_(arena->Adapter()),
method_stack_(arena->Adapter()),
current_method_(kInvalidEntry),
current_offset_(kInvalidEntry),
def_count_(0),
opcode_count_(NULL),
num_ssa_regs_(0),
extended_basic_blocks_(arena->Adapter()),
method_sreg_(0),
attributes_(METHOD_IS_LEAF), // Start with leaf assumption, change on encountering invoke.
checkstats_(NULL),
arena_(arena),
backward_branches_(0),
forward_branches_(0),
num_non_special_compiler_temps_(0),
max_available_special_compiler_temps_(1), // We only need the method ptr as a special temp for now.
requested_backend_temp_(false),
compiler_temps_committed_(false),
punt_to_interpreter_(false),
merged_df_flags_(0u),
ifield_lowering_infos_(arena, 0u),
sfield_lowering_infos_(arena, 0u),
method_lowering_infos_(arena, 0u),
gen_suspend_test_list_(arena, 0u) {
try_block_addr_ = new (arena_) ArenaBitVector(arena_, 0, true /* expandable */);
if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
// X86 requires a temp to keep track of the method address.
// TODO For x86_64, addressing can be done with RIP. When that is implemented,
// this needs to be updated to reserve 0 temps for BE.
max_available_non_special_compiler_temps_ = cu_->target64 ? 2 : 1;
reserved_temps_for_backend_ = max_available_non_special_compiler_temps_;
} else {
// Other architectures do not have a known lower bound for non-special temps.
// We allow the update of the max to happen at BE initialization stage and simply set 0 for now.
max_available_non_special_compiler_temps_ = 0;
reserved_temps_for_backend_ = 0;
}
}
MIRGraph::~MIRGraph() {
STLDeleteElements(&m_units_);
}
/*
* Parse an instruction, return the length of the instruction
*/
int MIRGraph::ParseInsn(const uint16_t* code_ptr, MIR::DecodedInstruction* decoded_instruction) {
const Instruction* inst = Instruction::At(code_ptr);
decoded_instruction->opcode = inst->Opcode();
decoded_instruction->vA = inst->HasVRegA() ? inst->VRegA() : 0;
decoded_instruction->vB = inst->HasVRegB() ? inst->VRegB() : 0;
decoded_instruction->vB_wide = inst->HasWideVRegB() ? inst->WideVRegB() : 0;
decoded_instruction->vC = inst->HasVRegC() ? inst->VRegC() : 0;
if (inst->HasVarArgs()) {
inst->GetVarArgs(decoded_instruction->arg);
}
return inst->SizeInCodeUnits();
}
/* Split an existing block from the specified code offset into two */
BasicBlock* MIRGraph::SplitBlock(DexOffset code_offset,
BasicBlock* orig_block, BasicBlock** immed_pred_block_p) {
DCHECK_GT(code_offset, orig_block->start_offset);
MIR* insn = orig_block->first_mir_insn;
MIR* prev = NULL;
while (insn) {
if (insn->offset == code_offset) break;
prev = insn;
insn = insn->next;
}
if (insn == NULL) {
LOG(FATAL) << "Break split failed";
}
BasicBlock* bottom_block = NewMemBB(kDalvikByteCode, num_blocks_++);
block_list_.Insert(bottom_block);
bottom_block->start_offset = code_offset;
bottom_block->first_mir_insn = insn;
bottom_block->last_mir_insn = orig_block->last_mir_insn;
/* If this block was terminated by a return, conditional branch or throw,
* the flag needs to go with the bottom block
*/
bottom_block->terminated_by_return = orig_block->terminated_by_return;
orig_block->terminated_by_return = false;
bottom_block->conditional_branch = orig_block->conditional_branch;
orig_block->conditional_branch = false;
bottom_block->explicit_throw = orig_block->explicit_throw;
orig_block->explicit_throw = false;
/* Handle the taken path */
bottom_block->taken = orig_block->taken;
if (bottom_block->taken != NullBasicBlockId) {
orig_block->taken = NullBasicBlockId;
BasicBlock* bb_taken = GetBasicBlock(bottom_block->taken);
bb_taken->predecessors->Delete(orig_block->id);
bb_taken->predecessors->Insert(bottom_block->id);
}
/* Handle the fallthrough path */
bottom_block->fall_through = orig_block->fall_through;
orig_block->fall_through = bottom_block->id;
bottom_block->predecessors->Insert(orig_block->id);
if (bottom_block->fall_through != NullBasicBlockId) {
BasicBlock* bb_fall_through = GetBasicBlock(bottom_block->fall_through);
bb_fall_through->predecessors->Delete(orig_block->id);
bb_fall_through->predecessors->Insert(bottom_block->id);
}
/* Handle the successor list */
if (orig_block->successor_block_list_type != kNotUsed) {
bottom_block->successor_block_list_type = orig_block->successor_block_list_type;
bottom_block->successor_blocks = orig_block->successor_blocks;
orig_block->successor_block_list_type = kNotUsed;
orig_block->successor_blocks = nullptr;
GrowableArray<SuccessorBlockInfo*>::Iterator iterator(bottom_block->successor_blocks);
while (true) {
SuccessorBlockInfo* successor_block_info = iterator.Next();
if (successor_block_info == nullptr) break;
BasicBlock* bb = GetBasicBlock(successor_block_info->block);
if (bb != nullptr) {
bb->predecessors->Delete(orig_block->id);
bb->predecessors->Insert(bottom_block->id);
}
}
}
orig_block->last_mir_insn = prev;
prev->next = nullptr;
/*
* Update the immediate predecessor block pointer so that outgoing edges
* can be applied to the proper block.
*/
if (immed_pred_block_p) {
DCHECK_EQ(*immed_pred_block_p, orig_block);
*immed_pred_block_p = bottom_block;
}
// Associate dex instructions in the bottom block with the new container.
DCHECK(insn != nullptr);
DCHECK(insn != orig_block->first_mir_insn);
DCHECK(insn == bottom_block->first_mir_insn);
DCHECK_EQ(insn->offset, bottom_block->start_offset);
DCHECK(static_cast<int>(insn->dalvikInsn.opcode) == kMirOpCheck ||
!MIR::DecodedInstruction::IsPseudoMirOp(insn->dalvikInsn.opcode));
DCHECK_EQ(dex_pc_to_block_map_.Get(insn->offset), orig_block->id);
MIR* p = insn;
dex_pc_to_block_map_.Put(p->offset, bottom_block->id);
while (p != bottom_block->last_mir_insn) {
p = p->next;
DCHECK(p != nullptr);
p->bb = bottom_block->id;
int opcode = p->dalvikInsn.opcode;
/*
* Some messiness here to ensure that we only enter real opcodes and only the
* first half of a potentially throwing instruction that has been split into
* CHECK and work portions. Since the 2nd half of a split operation is always
* the first in a BasicBlock, we can't hit it here.
*/
if ((opcode == kMirOpCheck) || !MIR::DecodedInstruction::IsPseudoMirOp(opcode)) {
DCHECK_EQ(dex_pc_to_block_map_.Get(p->offset), orig_block->id);
dex_pc_to_block_map_.Put(p->offset, bottom_block->id);
}
}
return bottom_block;
}
/*
* Given a code offset, find out the block that starts with it. If the offset
* is in the middle of an existing block, split it into two. If immed_pred_block_p
* is not non-null and is the block being split, update *immed_pred_block_p to
* point to the bottom block so that outgoing edges can be set up properly
* (by the caller)
* Utilizes a map for fast lookup of the typical cases.
*/
BasicBlock* MIRGraph::FindBlock(DexOffset code_offset, bool split, bool create,
BasicBlock** immed_pred_block_p) {
if (code_offset >= current_code_item_->insns_size_in_code_units_) {
return NULL;
}
int block_id = dex_pc_to_block_map_.Get(code_offset);
BasicBlock* bb = (block_id == 0) ? NULL : block_list_.Get(block_id);
if ((bb != NULL) && (bb->start_offset == code_offset)) {
// Does this containing block start with the desired instruction?
return bb;
}
// No direct hit.
if (!create) {
return NULL;
}
if (bb != NULL) {
// The target exists somewhere in an existing block.
return SplitBlock(code_offset, bb, bb == *immed_pred_block_p ? immed_pred_block_p : NULL);
}
// Create a new block.
bb = NewMemBB(kDalvikByteCode, num_blocks_++);
block_list_.Insert(bb);
bb->start_offset = code_offset;
dex_pc_to_block_map_.Put(bb->start_offset, bb->id);
return bb;
}
/* Identify code range in try blocks and set up the empty catch blocks */
void MIRGraph::ProcessTryCatchBlocks() {
int tries_size = current_code_item_->tries_size_;
DexOffset offset;
if (tries_size == 0) {
return;
}
for (int i = 0; i < tries_size; i++) {
const DexFile::TryItem* pTry =
DexFile::GetTryItems(*current_code_item_, i);
DexOffset start_offset = pTry->start_addr_;
DexOffset end_offset = start_offset + pTry->insn_count_;
for (offset = start_offset; offset < end_offset; offset++) {
try_block_addr_->SetBit(offset);
}
}
// Iterate over each of the handlers to enqueue the empty Catch blocks.
const byte* handlers_ptr = DexFile::GetCatchHandlerData(*current_code_item_, 0);
uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
for (uint32_t idx = 0; idx < handlers_size; idx++) {
CatchHandlerIterator iterator(handlers_ptr);
for (; iterator.HasNext(); iterator.Next()) {
uint32_t address = iterator.GetHandlerAddress();
FindBlock(address, false /* split */, true /*create*/,
/* immed_pred_block_p */ NULL);
}
handlers_ptr = iterator.EndDataPointer();
}
}
bool MIRGraph::IsBadMonitorExitCatch(NarrowDexOffset monitor_exit_offset,
NarrowDexOffset catch_offset) {
// Catches for monitor-exit during stack unwinding have the pattern
// move-exception (move)* (goto)? monitor-exit throw
// In the currently generated dex bytecode we see these catching a bytecode range including
// either its own or an identical monitor-exit, http://b/15745363 . This function checks if
// it's the case for a given monitor-exit and catch block so that we can ignore it.
// (We don't want to ignore all monitor-exit catches since one could enclose a synchronized
// block in a try-block and catch the NPE, Error or Throwable and we should let it through;
// even though a throwing monitor-exit certainly indicates a bytecode error.)
const Instruction* monitor_exit = Instruction::At(current_code_item_->insns_ + monitor_exit_offset);
DCHECK(monitor_exit->Opcode() == Instruction::MONITOR_EXIT);
int monitor_reg = monitor_exit->VRegA_11x();
const Instruction* check_insn = Instruction::At(current_code_item_->insns_ + catch_offset);
DCHECK(check_insn->Opcode() == Instruction::MOVE_EXCEPTION);
if (check_insn->VRegA_11x() == monitor_reg) {
// Unexpected move-exception to the same register. Probably not the pattern we're looking for.
return false;
}
check_insn = check_insn->Next();
while (true) {
int dest = -1;
bool wide = false;
switch (check_insn->Opcode()) {
case Instruction::MOVE_WIDE:
wide = true;
// Intentional fall-through.
case Instruction::MOVE_OBJECT:
case Instruction::MOVE:
dest = check_insn->VRegA_12x();
break;
case Instruction::MOVE_WIDE_FROM16:
wide = true;
// Intentional fall-through.
case Instruction::MOVE_OBJECT_FROM16:
case Instruction::MOVE_FROM16:
dest = check_insn->VRegA_22x();
break;
case Instruction::MOVE_WIDE_16:
wide = true;
// Intentional fall-through.
case Instruction::MOVE_OBJECT_16:
case Instruction::MOVE_16:
dest = check_insn->VRegA_32x();
break;
case Instruction::GOTO:
case Instruction::GOTO_16:
case Instruction::GOTO_32:
check_insn = check_insn->RelativeAt(check_insn->GetTargetOffset());
// Intentional fall-through.
default:
return check_insn->Opcode() == Instruction::MONITOR_EXIT &&
check_insn->VRegA_11x() == monitor_reg;
}
if (dest == monitor_reg || (wide && dest + 1 == monitor_reg)) {
return false;
}
check_insn = check_insn->Next();
}
}
/* Process instructions with the kBranch flag */
BasicBlock* MIRGraph::ProcessCanBranch(BasicBlock* cur_block, MIR* insn, DexOffset cur_offset,
int width, int flags, const uint16_t* code_ptr,
const uint16_t* code_end) {
DexOffset target = cur_offset;
switch (insn->dalvikInsn.opcode) {
case Instruction::GOTO:
case Instruction::GOTO_16:
case Instruction::GOTO_32:
target += insn->dalvikInsn.vA;
break;
case Instruction::IF_EQ:
case Instruction::IF_NE:
case Instruction::IF_LT:
case Instruction::IF_GE:
case Instruction::IF_GT:
case Instruction::IF_LE:
cur_block->conditional_branch = true;
target += insn->dalvikInsn.vC;
break;
case Instruction::IF_EQZ:
case Instruction::IF_NEZ:
case Instruction::IF_LTZ:
case Instruction::IF_GEZ:
case Instruction::IF_GTZ:
case Instruction::IF_LEZ:
cur_block->conditional_branch = true;
target += insn->dalvikInsn.vB;
break;
default:
LOG(FATAL) << "Unexpected opcode(" << insn->dalvikInsn.opcode << ") with kBranch set";
}
CountBranch(target);
BasicBlock* taken_block = FindBlock(target, /* split */ true, /* create */ true,
/* immed_pred_block_p */ &cur_block);
cur_block->taken = taken_block->id;
taken_block->predecessors->Insert(cur_block->id);
/* Always terminate the current block for conditional branches */
if (flags & Instruction::kContinue) {
BasicBlock* fallthrough_block = FindBlock(cur_offset + width,
/*
* If the method is processed
* in sequential order from the
* beginning, we don't need to
* specify split for continue
* blocks. However, this
* routine can be called by
* compileLoop, which starts
* parsing the method from an
* arbitrary address in the
* method body.
*/
true,
/* create */
true,
/* immed_pred_block_p */
&cur_block);
cur_block->fall_through = fallthrough_block->id;
fallthrough_block->predecessors->Insert(cur_block->id);
} else if (code_ptr < code_end) {
FindBlock(cur_offset + width, /* split */ false, /* create */ true,
/* immed_pred_block_p */ NULL);
}
return cur_block;
}
/* Process instructions with the kSwitch flag */
BasicBlock* MIRGraph::ProcessCanSwitch(BasicBlock* cur_block, MIR* insn, DexOffset cur_offset,
int width, int flags) {
const uint16_t* switch_data =
reinterpret_cast<const uint16_t*>(GetCurrentInsns() + cur_offset + insn->dalvikInsn.vB);
int size;
const int* keyTable;
const int* target_table;
int i;
int first_key;
/*
* Packed switch data format:
* ushort ident = 0x0100 magic value
* ushort size number of entries in the table
* int first_key first (and lowest) switch case value
* int targets[size] branch targets, relative to switch opcode
*
* Total size is (4+size*2) 16-bit code units.
*/
if (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) {
DCHECK_EQ(static_cast<int>(switch_data[0]),
static_cast<int>(Instruction::kPackedSwitchSignature));
size = switch_data[1];
first_key = switch_data[2] | (switch_data[3] << 16);
target_table = reinterpret_cast<const int*>(&switch_data[4]);
keyTable = NULL; // Make the compiler happy.
/*
* Sparse switch data format:
* ushort ident = 0x0200 magic value
* ushort size number of entries in the table; > 0
* int keys[size] keys, sorted low-to-high; 32-bit aligned
* int targets[size] branch targets, relative to switch opcode
*
* Total size is (2+size*4) 16-bit code units.
*/
} else {
DCHECK_EQ(static_cast<int>(switch_data[0]),
static_cast<int>(Instruction::kSparseSwitchSignature));
size = switch_data[1];
keyTable = reinterpret_cast<const int*>(&switch_data[2]);
target_table = reinterpret_cast<const int*>(&switch_data[2 + size*2]);
first_key = 0; // To make the compiler happy.
}
if (cur_block->successor_block_list_type != kNotUsed) {
LOG(FATAL) << "Successor block list already in use: "
<< static_cast<int>(cur_block->successor_block_list_type);
}
cur_block->successor_block_list_type =
(insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ? kPackedSwitch : kSparseSwitch;
cur_block->successor_blocks =
new (arena_) GrowableArray<SuccessorBlockInfo*>(arena_, size, kGrowableArraySuccessorBlocks);
for (i = 0; i < size; i++) {
BasicBlock* case_block = FindBlock(cur_offset + target_table[i], /* split */ true,
/* create */ true, /* immed_pred_block_p */ &cur_block);
SuccessorBlockInfo* successor_block_info =
static_cast<SuccessorBlockInfo*>(arena_->Alloc(sizeof(SuccessorBlockInfo),
kArenaAllocSuccessor));
successor_block_info->block = case_block->id;
successor_block_info->key =
(insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ?
first_key + i : keyTable[i];
cur_block->successor_blocks->Insert(successor_block_info);
case_block->predecessors->Insert(cur_block->id);
}
/* Fall-through case */
BasicBlock* fallthrough_block = FindBlock(cur_offset + width, /* split */ false,
/* create */ true, /* immed_pred_block_p */ NULL);
cur_block->fall_through = fallthrough_block->id;
fallthrough_block->predecessors->Insert(cur_block->id);
return cur_block;
}
/* Process instructions with the kThrow flag */
BasicBlock* MIRGraph::ProcessCanThrow(BasicBlock* cur_block, MIR* insn, DexOffset cur_offset,
int width, int flags, ArenaBitVector* try_block_addr,
const uint16_t* code_ptr, const uint16_t* code_end) {
bool in_try_block = try_block_addr->IsBitSet(cur_offset);
bool is_throw = (insn->dalvikInsn.opcode == Instruction::THROW);
bool build_all_edges =
(cu_->disable_opt & (1 << kSuppressExceptionEdges)) || is_throw || in_try_block;
/* In try block */
if (in_try_block) {
CatchHandlerIterator iterator(*current_code_item_, cur_offset);
if (cur_block->successor_block_list_type != kNotUsed) {
LOG(INFO) << PrettyMethod(cu_->method_idx, *cu_->dex_file);
LOG(FATAL) << "Successor block list already in use: "
<< static_cast<int>(cur_block->successor_block_list_type);
}
for (; iterator.HasNext(); iterator.Next()) {
BasicBlock* catch_block = FindBlock(iterator.GetHandlerAddress(), false /* split*/,
false /* creat */, NULL /* immed_pred_block_p */);
if (insn->dalvikInsn.opcode == Instruction::MONITOR_EXIT &&
IsBadMonitorExitCatch(insn->offset, catch_block->start_offset)) {
// Don't allow monitor-exit to catch its own exception, http://b/15745363 .
continue;
}
if (cur_block->successor_block_list_type == kNotUsed) {
cur_block->successor_block_list_type = kCatch;
cur_block->successor_blocks = new (arena_) GrowableArray<SuccessorBlockInfo*>(
arena_, 2, kGrowableArraySuccessorBlocks);
}
catch_block->catch_entry = true;
if (kIsDebugBuild) {
catches_.insert(catch_block->start_offset);
}
SuccessorBlockInfo* successor_block_info = reinterpret_cast<SuccessorBlockInfo*>
(arena_->Alloc(sizeof(SuccessorBlockInfo), kArenaAllocSuccessor));
successor_block_info->block = catch_block->id;
successor_block_info->key = iterator.GetHandlerTypeIndex();
cur_block->successor_blocks->Insert(successor_block_info);
catch_block->predecessors->Insert(cur_block->id);
}
in_try_block = (cur_block->successor_block_list_type != kNotUsed);
}
if (!in_try_block && build_all_edges) {
BasicBlock* eh_block = NewMemBB(kExceptionHandling, num_blocks_++);
cur_block->taken = eh_block->id;
block_list_.Insert(eh_block);
eh_block->start_offset = cur_offset;
eh_block->predecessors->Insert(cur_block->id);
}
if (is_throw) {
cur_block->explicit_throw = true;
if (code_ptr < code_end) {
// Force creation of new block following THROW via side-effect.
FindBlock(cur_offset + width, /* split */ false, /* create */ true,
/* immed_pred_block_p */ NULL);
}
if (!in_try_block) {
// Don't split a THROW that can't rethrow - we're done.
return cur_block;
}
}
if (!build_all_edges) {
/*
* Even though there is an exception edge here, control cannot return to this
* method. Thus, for the purposes of dataflow analysis and optimization, we can
* ignore the edge. Doing this reduces compile time, and increases the scope
* of the basic-block level optimization pass.
*/
return cur_block;
}
/*
* Split the potentially-throwing instruction into two parts.
* The first half will be a pseudo-op that captures the exception
* edges and terminates the basic block. It always falls through.
* Then, create a new basic block that begins with the throwing instruction
* (minus exceptions). Note: this new basic block must NOT be entered into
* the block_map. If the potentially-throwing instruction is the target of a
* future branch, we need to find the check psuedo half. The new
* basic block containing the work portion of the instruction should
* only be entered via fallthrough from the block containing the
* pseudo exception edge MIR. Note also that this new block is
* not automatically terminated after the work portion, and may
* contain following instructions.
*
* Note also that the dex_pc_to_block_map_ entry for the potentially
* throwing instruction will refer to the original basic block.
*/
BasicBlock* new_block = NewMemBB(kDalvikByteCode, num_blocks_++);
block_list_.Insert(new_block);
new_block->start_offset = insn->offset;
cur_block->fall_through = new_block->id;
new_block->predecessors->Insert(cur_block->id);
MIR* new_insn = NewMIR();
*new_insn = *insn;
insn->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpCheck);
// Associate the two halves.
insn->meta.throw_insn = new_insn;
new_block->AppendMIR(new_insn);
return new_block;
}
/* Parse a Dex method and insert it into the MIRGraph at the current insert point. */
void MIRGraph::InlineMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
InvokeType invoke_type, uint16_t class_def_idx,
uint32_t method_idx, jobject class_loader, const DexFile& dex_file) {
current_code_item_ = code_item;
method_stack_.push_back(std::make_pair(current_method_, current_offset_));
current_method_ = m_units_.size();
current_offset_ = 0;
// TODO: will need to snapshot stack image and use that as the mir context identification.
m_units_.push_back(new DexCompilationUnit(cu_, class_loader, Runtime::Current()->GetClassLinker(),
dex_file, current_code_item_, class_def_idx, method_idx, access_flags,
cu_->compiler_driver->GetVerifiedMethod(&dex_file, method_idx)));
const uint16_t* code_ptr = current_code_item_->insns_;
const uint16_t* code_end =
current_code_item_->insns_ + current_code_item_->insns_size_in_code_units_;
// TODO: need to rework expansion of block list & try_block_addr when inlining activated.
// TUNING: use better estimate of basic blocks for following resize.
block_list_.Resize(block_list_.Size() + current_code_item_->insns_size_in_code_units_);
dex_pc_to_block_map_.SetSize(dex_pc_to_block_map_.Size() + current_code_item_->insns_size_in_code_units_);
// TODO: replace with explicit resize routine. Using automatic extension side effect for now.
try_block_addr_->SetBit(current_code_item_->insns_size_in_code_units_);
try_block_addr_->ClearBit(current_code_item_->insns_size_in_code_units_);
// If this is the first method, set up default entry and exit blocks.
if (current_method_ == 0) {
DCHECK(entry_block_ == NULL);
DCHECK(exit_block_ == NULL);
DCHECK_EQ(num_blocks_, 0U);
// Use id 0 to represent a null block.
BasicBlock* null_block = NewMemBB(kNullBlock, num_blocks_++);
DCHECK_EQ(null_block->id, NullBasicBlockId);
null_block->hidden = true;
block_list_.Insert(null_block);
entry_block_ = NewMemBB(kEntryBlock, num_blocks_++);
block_list_.Insert(entry_block_);
exit_block_ = NewMemBB(kExitBlock, num_blocks_++);
block_list_.Insert(exit_block_);
// TODO: deprecate all "cu->" fields; move what's left to wherever CompilationUnit is allocated.
cu_->dex_file = &dex_file;
cu_->class_def_idx = class_def_idx;
cu_->method_idx = method_idx;
cu_->access_flags = access_flags;
cu_->invoke_type = invoke_type;
cu_->shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
cu_->code_item = current_code_item_;
} else {
UNIMPLEMENTED(FATAL) << "Nested inlining not implemented.";
/*
* Will need to manage storage for ins & outs, push prevous state and update
* insert point.
*/
}
/* Current block to record parsed instructions */
BasicBlock* cur_block = NewMemBB(kDalvikByteCode, num_blocks_++);
DCHECK_EQ(current_offset_, 0U);
cur_block->start_offset = current_offset_;
block_list_.Insert(cur_block);
// TODO: for inlining support, insert at the insert point rather than entry block.
entry_block_->fall_through = cur_block->id;
cur_block->predecessors->Insert(entry_block_->id);
/* Identify code range in try blocks and set up the empty catch blocks */
ProcessTryCatchBlocks();
uint64_t merged_df_flags = 0u;
/* Parse all instructions and put them into containing basic blocks */
while (code_ptr < code_end) {
MIR *insn = NewMIR();
insn->offset = current_offset_;
insn->m_unit_index = current_method_;
int width = ParseInsn(code_ptr, &insn->dalvikInsn);
Instruction::Code opcode = insn->dalvikInsn.opcode;
if (opcode_count_ != NULL) {
opcode_count_[static_cast<int>(opcode)]++;
}
int flags = insn->dalvikInsn.FlagsOf();
int verify_flags = Instruction::VerifyFlagsOf(insn->dalvikInsn.opcode);
uint64_t df_flags = GetDataFlowAttributes(insn);
merged_df_flags |= df_flags;
if (df_flags & DF_HAS_DEFS) {
def_count_ += (df_flags & DF_A_WIDE) ? 2 : 1;
}
if (df_flags & DF_LVN) {
cur_block->use_lvn = true; // Run local value numbering on this basic block.
}
// Check for inline data block signatures.
if (opcode == Instruction::NOP) {
// A simple NOP will have a width of 1 at this point, embedded data NOP > 1.
if ((width == 1) && ((current_offset_ & 0x1) == 0x1) && ((code_end - code_ptr) > 1)) {
// Could be an aligning nop. If an embedded data NOP follows, treat pair as single unit.
uint16_t following_raw_instruction = code_ptr[1];
if ((following_raw_instruction == Instruction::kSparseSwitchSignature) ||
(following_raw_instruction == Instruction::kPackedSwitchSignature) ||
(following_raw_instruction == Instruction::kArrayDataSignature)) {
width += Instruction::At(code_ptr + 1)->SizeInCodeUnits();
}
}
if (width == 1) {
// It is a simple nop - treat normally.
cur_block->AppendMIR(insn);
} else {
DCHECK(cur_block->fall_through == NullBasicBlockId);
DCHECK(cur_block->taken == NullBasicBlockId);
// Unreachable instruction, mark for no continuation.
flags &= ~Instruction::kContinue;
}
} else {
cur_block->AppendMIR(insn);
}
// Associate the starting dex_pc for this opcode with its containing basic block.
dex_pc_to_block_map_.Put(insn->offset, cur_block->id);
code_ptr += width;
if (flags & Instruction::kBranch) {
cur_block = ProcessCanBranch(cur_block, insn, current_offset_,
width, flags, code_ptr, code_end);
} else if (flags & Instruction::kReturn) {
cur_block->terminated_by_return = true;
cur_block->fall_through = exit_block_->id;
exit_block_->predecessors->Insert(cur_block->id);
/*
* Terminate the current block if there are instructions
* afterwards.
*/
if (code_ptr < code_end) {
/*
* Create a fallthrough block for real instructions
* (incl. NOP).
*/
FindBlock(current_offset_ + width, /* split */ false, /* create */ true,
/* immed_pred_block_p */ NULL);
}
} else if (flags & Instruction::kThrow) {
cur_block = ProcessCanThrow(cur_block, insn, current_offset_, width, flags, try_block_addr_,
code_ptr, code_end);
} else if (flags & Instruction::kSwitch) {
cur_block = ProcessCanSwitch(cur_block, insn, current_offset_, width, flags);
}
if (verify_flags & Instruction::kVerifyVarArgRange ||
verify_flags & Instruction::kVerifyVarArgRangeNonZero) {
/*
* The Quick backend's runtime model includes a gap between a method's
* argument ("in") vregs and the rest of its vregs. Handling a range instruction
* which spans the gap is somewhat complicated, and should not happen
* in normal usage of dx. Punt to the interpreter.
*/
int first_reg_in_range = insn->dalvikInsn.vC;
int last_reg_in_range = first_reg_in_range + insn->dalvikInsn.vA - 1;
if (IsInVReg(first_reg_in_range) != IsInVReg(last_reg_in_range)) {
punt_to_interpreter_ = true;
}
}
current_offset_ += width;
BasicBlock* next_block = FindBlock(current_offset_, /* split */ false, /* create */
false, /* immed_pred_block_p */ NULL);
if (next_block) {
/*
* The next instruction could be the target of a previously parsed
* forward branch so a block is already created. If the current
* instruction is not an unconditional branch, connect them through
* the fall-through link.
*/
DCHECK(cur_block->fall_through == NullBasicBlockId ||
GetBasicBlock(cur_block->fall_through) == next_block ||
GetBasicBlock(cur_block->fall_through) == exit_block_);
if ((cur_block->fall_through == NullBasicBlockId) && (flags & Instruction::kContinue)) {
cur_block->fall_through = next_block->id;
next_block->predecessors->Insert(cur_block->id);
}
cur_block = next_block;
}
}
merged_df_flags_ = merged_df_flags;
if (cu_->enable_debug & (1 << kDebugDumpCFG)) {
DumpCFG("/sdcard/1_post_parse_cfg/", true);
}
if (cu_->verbose) {
DumpMIRGraph();
}
}
void MIRGraph::ShowOpcodeStats() {
DCHECK(opcode_count_ != NULL);
LOG(INFO) << "Opcode Count";
for (int i = 0; i < kNumPackedOpcodes; i++) {
if (opcode_count_[i] != 0) {
LOG(INFO) << "-C- " << Instruction::Name(static_cast<Instruction::Code>(i))
<< " " << opcode_count_[i];
}
}
}
uint64_t MIRGraph::GetDataFlowAttributes(Instruction::Code opcode) {
DCHECK_LT((size_t) opcode, (sizeof(oat_data_flow_attributes_) / sizeof(oat_data_flow_attributes_[0])));
return oat_data_flow_attributes_[opcode];
}
uint64_t MIRGraph::GetDataFlowAttributes(MIR* mir) {
DCHECK(mir != nullptr);
Instruction::Code opcode = mir->dalvikInsn.opcode;
return GetDataFlowAttributes(opcode);
}
// TODO: use a configurable base prefix, and adjust callers to supply pass name.
/* Dump the CFG into a DOT graph */
void MIRGraph::DumpCFG(const char* dir_prefix, bool all_blocks, const char *suffix) {
FILE* file;
static AtomicInteger cnt(0);
// Increment counter to get a unique file number.
cnt++;
std::string fname(PrettyMethod(cu_->method_idx, *cu_->dex_file));
ReplaceSpecialChars(fname);
fname = StringPrintf("%s%s%x%s_%d.dot", dir_prefix, fname.c_str(),
GetBasicBlock(GetEntryBlock()->fall_through)->start_offset,
suffix == nullptr ? "" : suffix,
cnt.LoadRelaxed());
file = fopen(fname.c_str(), "w");
if (file == NULL) {
return;
}
fprintf(file, "digraph G {\n");
fprintf(file, " rankdir=TB\n");
int num_blocks = all_blocks ? GetNumBlocks() : num_reachable_blocks_;
int idx;
for (idx = 0; idx < num_blocks; idx++) {
int block_idx = all_blocks ? idx : dfs_order_->Get(idx);
BasicBlock* bb = GetBasicBlock(block_idx);
if (bb == NULL) continue;
if (bb->block_type == kDead) continue;
if (bb->hidden) continue;
if (bb->block_type == kEntryBlock) {
fprintf(file, " entry_%d [shape=Mdiamond];\n", bb->id);
} else if (bb->block_type == kExitBlock) {
fprintf(file, " exit_%d [shape=Mdiamond];\n", bb->id);
} else if (bb->block_type == kDalvikByteCode) {
fprintf(file, " block%04x_%d [shape=record,label = \"{ \\\n",
bb->start_offset, bb->id);
const MIR* mir;
fprintf(file, " {block id %d\\l}%s\\\n", bb->id,
bb->first_mir_insn ? " | " : " ");
for (mir = bb->first_mir_insn; mir; mir = mir->next) {
int opcode = mir->dalvikInsn.opcode;
fprintf(file, " {%04x %s %s %s %s %s %s %s\\l}%s\\\n", mir->offset,
mir->ssa_rep ? GetDalvikDisassembly(mir) :
!MIR::DecodedInstruction::IsPseudoMirOp(opcode) ?
Instruction::Name(mir->dalvikInsn.opcode) :
extended_mir_op_names_[opcode - kMirOpFirst],
(mir->optimization_flags & MIR_IGNORE_RANGE_CHECK) != 0 ? " no_rangecheck" : " ",
(mir->optimization_flags & MIR_IGNORE_NULL_CHECK) != 0 ? " no_nullcheck" : " ",
(mir->optimization_flags & MIR_IGNORE_SUSPEND_CHECK) != 0 ? " no_suspendcheck" : " ",
(mir->optimization_flags & MIR_STORE_NON_TEMPORAL) != 0 ? " non_temporal" : " ",
(mir->optimization_flags & MIR_CALLEE) != 0 ? " inlined" : " ",
(mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) != 0 ? " no_clinit" : " ",
mir->next ? " | " : " ");
}
fprintf(file, " }\"];\n\n");
} else if (bb->block_type == kExceptionHandling) {
char block_name[BLOCK_NAME_LEN];
GetBlockName(bb, block_name);
fprintf(file, " %s [shape=invhouse];\n", block_name);
}
char block_name1[BLOCK_NAME_LEN], block_name2[BLOCK_NAME_LEN];
if (bb->taken != NullBasicBlockId) {
GetBlockName(bb, block_name1);
GetBlockName(GetBasicBlock(bb->taken), block_name2);
fprintf(file, " %s:s -> %s:n [style=dotted]\n",
block_name1, block_name2);
}
if (bb->fall_through != NullBasicBlockId) {
GetBlockName(bb, block_name1);
GetBlockName(GetBasicBlock(bb->fall_through), block_name2);
fprintf(file, " %s:s -> %s:n\n", block_name1, block_name2);
}
if (bb->successor_block_list_type != kNotUsed) {
fprintf(file, " succ%04x_%d [shape=%s,label = \"{ \\\n",
bb->start_offset, bb->id,
(bb->successor_block_list_type == kCatch) ? "Mrecord" : "record");
GrowableArray<SuccessorBlockInfo*>::Iterator iterator(bb->successor_blocks);
SuccessorBlockInfo* successor_block_info = iterator.Next();
int succ_id = 0;
while (true) {
if (successor_block_info == NULL) break;
BasicBlock* dest_block = GetBasicBlock(successor_block_info->block);
SuccessorBlockInfo *next_successor_block_info = iterator.Next();
fprintf(file, " {<f%d> %04x: %04x\\l}%s\\\n",
succ_id++,
successor_block_info->key,
dest_block->start_offset,
(next_successor_block_info != NULL) ? " | " : " ");
successor_block_info = next_successor_block_info;
}
fprintf(file, " }\"];\n\n");
GetBlockName(bb, block_name1);
fprintf(file, " %s:s -> succ%04x_%d:n [style=dashed]\n",
block_name1, bb->start_offset, bb->id);
// Link the successor pseudo-block with all of its potential targets.
GrowableArray<SuccessorBlockInfo*>::Iterator iter(bb->successor_blocks);
succ_id = 0;
while (true) {
SuccessorBlockInfo* successor_block_info = iter.Next();
if (successor_block_info == NULL) break;
BasicBlock* dest_block = GetBasicBlock(successor_block_info->block);
GetBlockName(dest_block, block_name2);
fprintf(file, " succ%04x_%d:f%d:e -> %s:n\n", bb->start_offset,
bb->id, succ_id++, block_name2);
}
}
fprintf(file, "\n");
if (cu_->verbose) {
/* Display the dominator tree */
GetBlockName(bb, block_name1);
fprintf(file, " cfg%s [label=\"%s\", shape=none];\n",
block_name1, block_name1);
if (bb->i_dom) {
GetBlockName(GetBasicBlock(bb->i_dom), block_name2);
fprintf(file, " cfg%s:s -> cfg%s:n\n\n", block_name2, block_name1);
}
}
}
fprintf(file, "}\n");
fclose(file);
}
/* Insert an MIR instruction to the end of a basic block. */
void BasicBlock::AppendMIR(MIR* mir) {
// Insert it after the last MIR.
InsertMIRListAfter(last_mir_insn, mir, mir);
}
void BasicBlock::AppendMIRList(MIR* first_list_mir, MIR* last_list_mir) {
// Insert it after the last MIR.
InsertMIRListAfter(last_mir_insn, first_list_mir, last_list_mir);
}
void BasicBlock::AppendMIRList(const std::vector<MIR*>& insns) {
for (std::vector<MIR*>::const_iterator it = insns.begin(); it != insns.end(); it++) {
MIR* new_mir = *it;
// Add a copy of each MIR.
InsertMIRListAfter(last_mir_insn, new_mir, new_mir);
}
}
/* Insert a MIR instruction after the specified MIR. */
void BasicBlock::InsertMIRAfter(MIR* current_mir, MIR* new_mir) {
InsertMIRListAfter(current_mir, new_mir, new_mir);
}
void BasicBlock::InsertMIRListAfter(MIR* insert_after, MIR* first_list_mir, MIR* last_list_mir) {
// If no MIR, we are done.
if (first_list_mir == nullptr || last_list_mir == nullptr) {
return;
}
// If insert_after is null, assume BB is empty.
if (insert_after == nullptr) {
first_mir_insn = first_list_mir;
last_mir_insn = last_list_mir;
last_list_mir->next = nullptr;
} else {
MIR* after_list = insert_after->next;
insert_after->next = first_list_mir;
last_list_mir->next = after_list;
if (after_list == nullptr) {
last_mir_insn = last_list_mir;
}
}
// Set this BB to be the basic block of the MIRs.
MIR* last = last_list_mir->next;
for (MIR* mir = first_list_mir; mir != last; mir = mir->next) {
mir->bb = id;
}
}
/* Insert an MIR instruction to the head of a basic block. */
void BasicBlock::PrependMIR(MIR* mir) {
InsertMIRListBefore(first_mir_insn, mir, mir);
}
void BasicBlock::PrependMIRList(MIR* first_list_mir, MIR* last_list_mir) {
// Insert it before the first MIR.
InsertMIRListBefore(first_mir_insn, first_list_mir, last_list_mir);
}
void BasicBlock::PrependMIRList(const std::vector<MIR*>& to_add) {
for (std::vector<MIR*>::const_iterator it = to_add.begin(); it != to_add.end(); it++) {
MIR* mir = *it;
InsertMIRListBefore(first_mir_insn, mir, mir);
}
}
/* Insert a MIR instruction before the specified MIR. */
void BasicBlock::InsertMIRBefore(MIR* current_mir, MIR* new_mir) {
// Insert as a single element list.
return InsertMIRListBefore(current_mir, new_mir, new_mir);
}
MIR* BasicBlock::FindPreviousMIR(MIR* mir) {
MIR* current = first_mir_insn;
while (current != nullptr) {
MIR* next = current->next;
if (next == mir) {
return current;
}
current = next;
}
return nullptr;
}
void BasicBlock::InsertMIRListBefore(MIR* insert_before, MIR* first_list_mir, MIR* last_list_mir) {
// If no MIR, we are done.
if (first_list_mir == nullptr || last_list_mir == nullptr) {
return;
}
// If insert_before is null, assume BB is empty.
if (insert_before == nullptr) {
first_mir_insn = first_list_mir;
last_mir_insn = last_list_mir;
last_list_mir->next = nullptr;
} else {
if (first_mir_insn == insert_before) {
last_list_mir->next = first_mir_insn;
first_mir_insn = first_list_mir;
} else {
// Find the preceding MIR.
MIR* before_list = FindPreviousMIR(insert_before);
DCHECK(before_list != nullptr);
before_list->next = first_list_mir;
last_list_mir->next = insert_before;
}
}
// Set this BB to be the basic block of the MIRs.
for (MIR* mir = first_list_mir; mir != last_list_mir->next; mir = mir->next) {
mir->bb = id;
}
}
bool BasicBlock::RemoveMIR(MIR* mir) {
// Remove as a single element list.
return RemoveMIRList(mir, mir);
}
bool BasicBlock::RemoveMIRList(MIR* first_list_mir, MIR* last_list_mir) {
if (first_list_mir == nullptr) {
return false;
}
// Try to find the MIR.
MIR* before_list = nullptr;
MIR* after_list = nullptr;
// If we are removing from the beginning of the MIR list.
if (first_mir_insn == first_list_mir) {
before_list = nullptr;
} else {
before_list = FindPreviousMIR(first_list_mir);
if (before_list == nullptr) {
// We did not find the mir.
return false;
}
}
// Remove the BB information and also find the after_list.
for (MIR* mir = first_list_mir; mir != last_list_mir; mir = mir->next) {
mir->bb = NullBasicBlockId;
}
after_list = last_list_mir->next;
// If there is nothing before the list, after_list is the first_mir.
if (before_list == nullptr) {
first_mir_insn = after_list;
} else {
before_list->next = after_list;
}
// If there is nothing after the list, before_list is last_mir.
if (after_list == nullptr) {
last_mir_insn = before_list;
}
return true;
}
MIR* BasicBlock::GetNextUnconditionalMir(MIRGraph* mir_graph, MIR* current) {
MIR* next_mir = nullptr;
if (current != nullptr) {
next_mir = current->next;
}
if (next_mir == nullptr) {
// Only look for next MIR that follows unconditionally.
if ((taken == NullBasicBlockId) && (fall_through != NullBasicBlockId)) {
next_mir = mir_graph->GetBasicBlock(fall_through)->first_mir_insn;
}
}
return next_mir;
}
static void FillTypeSizeString(uint32_t type_size, std::string* decoded_mir) {
DCHECK(decoded_mir != nullptr);
OpSize type = static_cast<OpSize>(type_size >> 16);
uint16_t vect_size = (type_size & 0xFFFF);
// Now print the type and vector size.
std::stringstream ss;
ss << " (type:";
ss << type;
ss << " vectsize:";
ss << vect_size;
ss << ")";
decoded_mir->append(ss.str());
}
void MIRGraph::DisassembleExtendedInstr(const MIR* mir, std::string* decoded_mir) {
DCHECK(decoded_mir != nullptr);
int opcode = mir->dalvikInsn.opcode;
SSARepresentation* ssa_rep = mir->ssa_rep;
int defs = (ssa_rep != nullptr) ? ssa_rep->num_defs : 0;
int uses = (ssa_rep != nullptr) ? ssa_rep->num_uses : 0;
decoded_mir->append(extended_mir_op_names_[opcode - kMirOpFirst]);
switch (opcode) {
case kMirOpPhi: {
if (defs > 0 && uses > 0) {
BasicBlockId* incoming = mir->meta.phi_incoming;
decoded_mir->append(StringPrintf(" %s = (%s",
GetSSANameWithConst(ssa_rep->defs[0], true).c_str(),
GetSSANameWithConst(ssa_rep->uses[0], true).c_str()));
decoded_mir->append(StringPrintf(":%d", incoming[0]));
for (int i = 1; i < uses; i++) {
decoded_mir->append(StringPrintf(", %s:%d", GetSSANameWithConst(ssa_rep->uses[i], true).c_str(), incoming[i]));
}
decoded_mir->append(")");
}
break;
}
case kMirOpCopy:
if (ssa_rep != nullptr) {
decoded_mir->append(" ");
decoded_mir->append(GetSSANameWithConst(ssa_rep->defs[0], false));
if (defs > 1) {
decoded_mir->append(", ");
decoded_mir->append(GetSSANameWithConst(ssa_rep->defs[1], false));
}
decoded_mir->append(" = ");
decoded_mir->append(GetSSANameWithConst(ssa_rep->uses[0], false));
if (uses > 1) {
decoded_mir->append(", ");
decoded_mir->append(GetSSANameWithConst(ssa_rep->uses[1], false));
}
} else {
decoded_mir->append(StringPrintf(" v%d = v%d", mir->dalvikInsn.vA, mir->dalvikInsn.vB));
}
break;
case kMirOpFusedCmplFloat:
case kMirOpFusedCmpgFloat:
case kMirOpFusedCmplDouble:
case kMirOpFusedCmpgDouble:
case kMirOpFusedCmpLong:
if (ssa_rep != nullptr) {
decoded_mir->append(" ");
decoded_mir->append(GetSSANameWithConst(ssa_rep->uses[0], false));
for (int i = 1; i < uses; i++) {
decoded_mir->append(", ");
decoded_mir->append(GetSSANameWithConst(ssa_rep->uses[i], false));
}
} else {
decoded_mir->append(StringPrintf(" v%d, v%d", mir->dalvikInsn.vA, mir->dalvikInsn.vB));
}
break;
case kMirOpMoveVector:
decoded_mir->append(StringPrintf(" vect%d = vect%d", mir->dalvikInsn.vA, mir->dalvikInsn.vB));
FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
break;
case kMirOpPackedAddition:
decoded_mir->append(StringPrintf(" vect%d = vect%d + vect%d", mir->dalvikInsn.vA, mir->dalvikInsn.vA, mir->dalvikInsn.vB));
FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
break;
case kMirOpPackedMultiply:
decoded_mir->append(StringPrintf(" vect%d = vect%d * vect%d", mir->dalvikInsn.vA, mir->dalvikInsn.vA, mir->dalvikInsn.vB));
FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
break;
case kMirOpPackedSubtract:
decoded_mir->append(StringPrintf(" vect%d = vect%d - vect%d", mir->dalvikInsn.vA, mir->dalvikInsn.vA, mir->dalvikInsn.vB));
FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
break;
case kMirOpPackedAnd:
decoded_mir->append(StringPrintf(" vect%d = vect%d & vect%d", mir->dalvikInsn.vA, mir->dalvikInsn.vA, mir->dalvikInsn.vB));
FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
break;
case kMirOpPackedOr:
decoded_mir->append(StringPrintf(" vect%d = vect%d \\| vect%d", mir->dalvikInsn.vA, mir->dalvikInsn.vA, mir->dalvikInsn.vB));
FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
break;
case kMirOpPackedXor:
decoded_mir->append(StringPrintf(" vect%d = vect%d ^ vect%d", mir->dalvikInsn.vA, mir->dalvikInsn.vA, mir->dalvikInsn.vB));
FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
break;
case kMirOpPackedShiftLeft:
decoded_mir->append(StringPrintf(" vect%d = vect%d \\<\\< %d", mir->dalvikInsn.vA, mir->dalvikInsn.vA, mir->dalvikInsn.vB));
FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
break;
case kMirOpPackedUnsignedShiftRight:
decoded_mir->append(StringPrintf(" vect%d = vect%d \\>\\>\\> %d", mir->dalvikInsn.vA, mir->dalvikInsn.vA, mir->dalvikInsn.vB));
FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
break;
case kMirOpPackedSignedShiftRight:
decoded_mir->append(StringPrintf(" vect%d = vect%d \\>\\> %d", mir->dalvikInsn.vA, mir->dalvikInsn.vA, mir->dalvikInsn.vB));
FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
break;
case kMirOpConstVector:
decoded_mir->append(StringPrintf(" vect%d = %x, %x, %x, %x", mir->dalvikInsn.vA, mir->dalvikInsn.arg[0],
mir->dalvikInsn.arg[1], mir->dalvikInsn.arg[2], mir->dalvikInsn.arg[3]));
break;
case kMirOpPackedSet:
if (ssa_rep != nullptr) {
decoded_mir->append(StringPrintf(" vect%d = %s", mir->dalvikInsn.vA,
GetSSANameWithConst(ssa_rep->uses[0], false).c_str()));
if (uses > 1) {
decoded_mir->append(", ");
decoded_mir->append(GetSSANameWithConst(ssa_rep->uses[1], false));
}
} else {
decoded_mir->append(StringPrintf(" vect%d = v%d", mir->dalvikInsn.vA, mir->dalvikInsn.vB));
}
FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
break;
case kMirOpPackedAddReduce:
if (ssa_rep != nullptr) {
decoded_mir->append(" ");
decoded_mir->append(GetSSANameWithConst(ssa_rep->defs[0], false));
if (defs > 1) {
decoded_mir->append(", ");
decoded_mir->append(GetSSANameWithConst(ssa_rep->defs[1], false));
}
decoded_mir->append(StringPrintf(" = vect%d + %s", mir->dalvikInsn.vB,
GetSSANameWithConst(ssa_rep->uses[0], false).c_str()));
if (uses > 1) {
decoded_mir->append(", ");
decoded_mir->append(GetSSANameWithConst(ssa_rep->uses[1], false));
}
} else {
decoded_mir->append(StringPrintf("v%d = vect%d + v%d", mir->dalvikInsn.vA, mir->dalvikInsn.vB, mir->dalvikInsn.vA));
}
FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
break;
case kMirOpPackedReduce:
if (ssa_rep != nullptr) {
decoded_mir->append(" ");
decoded_mir->append(GetSSANameWithConst(ssa_rep->defs[0], false));
if (defs > 1) {
decoded_mir->append(", ");
decoded_mir->append(GetSSANameWithConst(ssa_rep->defs[1], false));
}
decoded_mir->append(StringPrintf(" = vect%d", mir->dalvikInsn.vB));
} else {
decoded_mir->append(StringPrintf(" v%d = vect%d", mir->dalvikInsn.vA, mir->dalvikInsn.vB));
}
FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
break;
case kMirOpReserveVectorRegisters:
case kMirOpReturnVectorRegisters:
decoded_mir->append(StringPrintf(" vect%d - vect%d", mir->dalvikInsn.vA, mir->dalvikInsn.vB));
break;
case kMirOpMemBarrier: {
decoded_mir->append(" type:");
std::stringstream ss;
ss << static_cast<MemBarrierKind>(mir->dalvikInsn.vA);
decoded_mir->append(ss.str());
break;
}
case kMirOpPackedArrayGet:
case kMirOpPackedArrayPut:
decoded_mir->append(StringPrintf(" vect%d", mir->dalvikInsn.vA));
if (ssa_rep != nullptr) {
decoded_mir->append(StringPrintf(", %s[%s]",
GetSSANameWithConst(ssa_rep->uses[0], false).c_str(),
GetSSANameWithConst(ssa_rep->uses[1], false).c_str()));
} else {
decoded_mir->append(StringPrintf(", v%d[v%d]", mir->dalvikInsn.vB, mir->dalvikInsn.vC));
}
FillTypeSizeString(mir->dalvikInsn.arg[0], decoded_mir);
break;
default:
break;
}
}
char* MIRGraph::GetDalvikDisassembly(const MIR* mir) {
MIR::DecodedInstruction insn = mir->dalvikInsn;
std::string str;
int flags = 0;
int opcode = insn.opcode;
char* ret;
bool nop = false;
SSARepresentation* ssa_rep = mir->ssa_rep;
Instruction::Format dalvik_format = Instruction::k10x; // Default to no-operand format.
// Handle special cases that recover the original dalvik instruction.
if ((opcode == kMirOpCheck) || (opcode == kMirOpCheckPart2)) {
str.append(extended_mir_op_names_[opcode - kMirOpFirst]);
str.append(": ");
// Recover the original Dex instruction.
insn = mir->meta.throw_insn->dalvikInsn;
ssa_rep = mir->meta.throw_insn->ssa_rep;
opcode = insn.opcode;
} else if (opcode == kMirOpNop) {
str.append("[");
// Recover original opcode.
insn.opcode = Instruction::At(current_code_item_->insns_ + mir->offset)->Opcode();
opcode = insn.opcode;
nop = true;
}
int defs = (ssa_rep != NULL) ? ssa_rep->num_defs : 0;
int uses = (ssa_rep != NULL) ? ssa_rep->num_uses : 0;
if (MIR::DecodedInstruction::IsPseudoMirOp(opcode)) {
// Note that this does not check the MIR's opcode in all cases. In cases where it
// recovered dalvik instruction, it uses opcode of that instead of the extended one.
DisassembleExtendedInstr(mir, &str);
} else {
dalvik_format = Instruction::FormatOf(insn.opcode);
flags = insn.FlagsOf();
str.append(Instruction::Name(insn.opcode));
// For invokes-style formats, treat wide regs as a pair of singles.
bool show_singles = ((dalvik_format == Instruction::k35c) ||
(dalvik_format == Instruction::k3rc));
if (defs != 0) {
str.append(" ");
str.append(GetSSANameWithConst(ssa_rep->defs[0], false));
if (defs > 1) {
str.append(", ");
str.append(GetSSANameWithConst(ssa_rep->defs[1], false));
}
if (uses != 0) {
str.append(", ");
}
}
for (int i = 0; i < uses; i++) {
str.append(" ");
str.append(GetSSANameWithConst(ssa_rep->uses[i], show_singles));
if (!show_singles && (reg_location_ != NULL) && reg_location_[i].wide) {
// For the listing, skip the high sreg.
i++;
}
if (i != (uses - 1)) {
str.append(",");
}
}
switch (dalvik_format) {
case Instruction::k11n: // Add one immediate from vB.
case Instruction::k21s:
case Instruction::k31i:
case Instruction::k21h:
str.append(StringPrintf(", #0x%x", insn.vB));
break;
case Instruction::k51l: // Add one wide immediate.
str.append(StringPrintf(", #%" PRId64, insn.vB_wide));
break;
case Instruction::k21c: // One register, one string/type/method index.
case Instruction::k31c:
str.append(StringPrintf(", index #0x%x", insn.vB));
break;
case Instruction::k22c: // Two registers, one string/type/method index.
str.append(StringPrintf(", index #0x%x", insn.vC));
break;
case Instruction::k22s: // Add one immediate from vC.
case Instruction::k22b:
str.append(StringPrintf(", #0x%x", insn.vC));
break;
default:
// Nothing left to print.
break;
}
if ((flags & Instruction::kBranch) != 0) {
// For branches, decode the instructions to print out the branch targets.
int offset = 0;
switch (dalvik_format) {
case Instruction::k21t:
offset = insn.vB;
break;
case Instruction::k22t:
offset = insn.vC;
break;
case Instruction::k10t:
case Instruction::k20t:
case Instruction::k30t:
offset = insn.vA;
break;
default:
LOG(FATAL) << "Unexpected branch format " << dalvik_format << " from " << insn.opcode;
break;
}
str.append(StringPrintf(", 0x%x (%c%x)", mir->offset + offset,
offset > 0 ? '+' : '-', offset > 0 ? offset : -offset));
}
if (nop) {
str.append("]--optimized away");
}
}
int length = str.length() + 1;
ret = static_cast<char*>(arena_->Alloc(length, kArenaAllocDFInfo));
strncpy(ret, str.c_str(), length);
return ret;
}
/* Turn method name into a legal Linux file name */
void MIRGraph::ReplaceSpecialChars(std::string& str) {
static const struct { const char before; const char after; } match[] = {
{'/', '-'}, {';', '#'}, {' ', '#'}, {'$', '+'},
{'(', '@'}, {')', '@'}, {'<', '='}, {'>', '='}
};
for (unsigned int i = 0; i < sizeof(match)/sizeof(match[0]); i++) {
std::replace(str.begin(), str.end(), match[i].before, match[i].after);
}
}
std::string MIRGraph::GetSSAName(int ssa_reg) {
// TODO: This value is needed for LLVM and debugging. Currently, we compute this and then copy to
// the arena. We should be smarter and just place straight into the arena, or compute the
// value more lazily.
int vreg = SRegToVReg(ssa_reg);
if (vreg >= static_cast<int>(GetFirstTempVR())) {
return StringPrintf("t%d_%d", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg));
} else {
return StringPrintf("v%d_%d", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg));
}
}
// Similar to GetSSAName, but if ssa name represents an immediate show that as well.
std::string MIRGraph::GetSSANameWithConst(int ssa_reg, bool singles_only) {
if (reg_location_ == NULL) {
// Pre-SSA - just use the standard name.
return GetSSAName(ssa_reg);
}
if (IsConst(reg_location_[ssa_reg])) {
if (!singles_only && reg_location_[ssa_reg].wide) {
return StringPrintf("v%d_%d#0x%" PRIx64, SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg),
ConstantValueWide(reg_location_[ssa_reg]));
} else {
return StringPrintf("v%d_%d#0x%x", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg),
ConstantValue(reg_location_[ssa_reg]));
}
} else {
int vreg = SRegToVReg(ssa_reg);
if (vreg >= static_cast<int>(GetFirstTempVR())) {
return StringPrintf("t%d_%d", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg));
} else {
return StringPrintf("v%d_%d", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg));
}
}
}
void MIRGraph::GetBlockName(BasicBlock* bb, char* name) {
switch (bb->block_type) {
case kEntryBlock:
snprintf(name, BLOCK_NAME_LEN, "entry_%d", bb->id);
break;
case kExitBlock:
snprintf(name, BLOCK_NAME_LEN, "exit_%d", bb->id);
break;
case kDalvikByteCode:
snprintf(name, BLOCK_NAME_LEN, "block%04x_%d", bb->start_offset, bb->id);
break;
case kExceptionHandling:
snprintf(name, BLOCK_NAME_LEN, "exception%04x_%d", bb->start_offset,
bb->id);
break;
default:
snprintf(name, BLOCK_NAME_LEN, "_%d", bb->id);
break;
}
}
const char* MIRGraph::GetShortyFromTargetIdx(int target_idx) {
// TODO: for inlining support, use current code unit.
const DexFile::MethodId& method_id = cu_->dex_file->GetMethodId(target_idx);
return cu_->dex_file->GetShorty(method_id.proto_idx_);
}
/* Debug Utility - dump a compilation unit */
void MIRGraph::DumpMIRGraph() {
BasicBlock* bb;
const char* block_type_names[] = {
"Null Block",
"Entry Block",
"Code Block",
"Exit Block",
"Exception Handling",
"Catch Block"
};
LOG(INFO) << "Compiling " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
LOG(INFO) << GetInsns(0) << " insns";
LOG(INFO) << GetNumBlocks() << " blocks in total";
GrowableArray<BasicBlock*>::Iterator iterator(&block_list_);
while (true) {
bb = iterator.Next();
if (bb == NULL) break;
LOG(INFO) << StringPrintf("Block %d (%s) (insn %04x - %04x%s)",
bb->id,
block_type_names[bb->block_type],
bb->start_offset,
bb->last_mir_insn ? bb->last_mir_insn->offset : bb->start_offset,
bb->last_mir_insn ? "" : " empty");
if (bb->taken != NullBasicBlockId) {
LOG(INFO) << " Taken branch: block " << bb->taken
<< "(0x" << std::hex << GetBasicBlock(bb->taken)->start_offset << ")";
}
if (bb->fall_through != NullBasicBlockId) {
LOG(INFO) << " Fallthrough : block " << bb->fall_through
<< " (0x" << std::hex << GetBasicBlock(bb->fall_through)->start_offset << ")";
}
}
}
/*
* Build an array of location records for the incoming arguments.
* Note: one location record per word of arguments, with dummy
* high-word loc for wide arguments. Also pull up any following
* MOVE_RESULT and incorporate it into the invoke.
*/
CallInfo* MIRGraph::NewMemCallInfo(BasicBlock* bb, MIR* mir, InvokeType type,
bool is_range) {
CallInfo* info = static_cast<CallInfo*>(arena_->Alloc(sizeof(CallInfo),
kArenaAllocMisc));
MIR* move_result_mir = FindMoveResult(bb, mir);
if (move_result_mir == NULL) {
info->result.location = kLocInvalid;
} else {
info->result = GetRawDest(move_result_mir);
move_result_mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
}
info->num_arg_words = mir->ssa_rep->num_uses;
info->args = (info->num_arg_words == 0) ? NULL : static_cast<RegLocation*>
(arena_->Alloc(sizeof(RegLocation) * info->num_arg_words, kArenaAllocMisc));
for (int i = 0; i < info->num_arg_words; i++) {
info->args[i] = GetRawSrc(mir, i);
}
info->opt_flags = mir->optimization_flags;
info->type = type;
info->is_range = is_range;
info->index = mir->dalvikInsn.vB;
info->offset = mir->offset;
info->mir = mir;
return info;
}
// Allocate a new MIR.
MIR* MIRGraph::NewMIR() {
MIR* mir = new (arena_) MIR();
return mir;
}
// Allocate a new basic block.
BasicBlock* MIRGraph::NewMemBB(BBType block_type, int block_id) {
BasicBlock* bb = new (arena_) BasicBlock();
bb->block_type = block_type;
bb->id = block_id;
// TUNING: better estimate of the exit block predecessors?
bb->predecessors = new (arena_) GrowableArray<BasicBlockId>(arena_,
(block_type == kExitBlock) ? 2048 : 2,
kGrowableArrayPredecessors);
bb->successor_block_list_type = kNotUsed;
block_id_map_.Put(block_id, block_id);
return bb;
}
void MIRGraph::InitializeConstantPropagation() {
is_constant_v_ = new (arena_) ArenaBitVector(arena_, GetNumSSARegs(), false);
constant_values_ = static_cast<int*>(arena_->Alloc(sizeof(int) * GetNumSSARegs(), kArenaAllocDFInfo));
}
void MIRGraph::InitializeMethodUses() {
// The gate starts by initializing the use counts.
int num_ssa_regs = GetNumSSARegs();
use_counts_.Resize(num_ssa_regs + 32);
raw_use_counts_.Resize(num_ssa_regs + 32);
// Resize does not actually reset the number of used, so reset before initialization.
use_counts_.Reset();
raw_use_counts_.Reset();
// Initialize list.
for (int i = 0; i < num_ssa_regs; i++) {
use_counts_.Insert(0);
raw_use_counts_.Insert(0);
}
}
void MIRGraph::SSATransformationStart() {
DCHECK(temp_scoped_alloc_.get() == nullptr);
temp_scoped_alloc_.reset(ScopedArenaAllocator::Create(&cu_->arena_stack));
temp_bit_vector_size_ = GetNumOfCodeAndTempVRs();
temp_bit_vector_ = new (temp_scoped_alloc_.get()) ArenaBitVector(
temp_scoped_alloc_.get(), temp_bit_vector_size_, false, kBitMapRegisterV);
// Update the maximum number of reachable blocks.
max_num_reachable_blocks_ = num_reachable_blocks_;
}
void MIRGraph::SSATransformationEnd() {
// Verify the dataflow information after the pass.
if (cu_->enable_debug & (1 << kDebugVerifyDataflow)) {
VerifyDataflow();
}
temp_bit_vector_size_ = 0u;
temp_bit_vector_ = nullptr;
DCHECK(temp_scoped_alloc_.get() != nullptr);
temp_scoped_alloc_.reset();
}
static BasicBlock* SelectTopologicalSortOrderFallBack(
MIRGraph* mir_graph, const ArenaBitVector* current_loop,
const ScopedArenaVector<size_t>* visited_cnt_values, ScopedArenaAllocator* allocator,
ScopedArenaVector<BasicBlockId>* tmp_stack) {
// No true loop head has been found but there may be true loop heads after the mess we need
// to resolve. To avoid taking one of those, pick the candidate with the highest number of
// reachable unvisited nodes. That candidate will surely be a part of a loop.
BasicBlock* fall_back = nullptr;
size_t fall_back_num_reachable = 0u;
// Reuse the same bit vector for each candidate to mark reachable unvisited blocks.
ArenaBitVector candidate_reachable(allocator, mir_graph->GetNumBlocks(), false, kBitMapMisc);
AllNodesIterator iter(mir_graph);
for (BasicBlock* candidate = iter.Next(); candidate != nullptr; candidate = iter.Next()) {
if (candidate->hidden || // Hidden, or
candidate->visited || // already processed, or
(*visited_cnt_values)[candidate->id] == 0u || // no processed predecessors, or
(current_loop != nullptr && // outside current loop.
!current_loop->IsBitSet(candidate->id))) {
continue;
}
DCHECK(tmp_stack->empty());
tmp_stack->push_back(candidate->id);
candidate_reachable.ClearAllBits();
size_t num_reachable = 0u;
while (!tmp_stack->empty()) {
BasicBlockId current_id = tmp_stack->back();
tmp_stack->pop_back();
BasicBlock* current_bb = mir_graph->GetBasicBlock(current_id);
DCHECK(current_bb != nullptr);
ChildBlockIterator child_iter(current_bb, mir_graph);
BasicBlock* child_bb = child_iter.Next();
for ( ; child_bb != nullptr; child_bb = child_iter.Next()) {
DCHECK(!child_bb->hidden);
if (child_bb->visited || // Already processed, or
(current_loop != nullptr && // outside current loop.
!current_loop->IsBitSet(child_bb->id))) {
continue;
}
if (!candidate_reachable.IsBitSet(child_bb->id)) {
candidate_reachable.SetBit(child_bb->id);
tmp_stack->push_back(child_bb->id);
num_reachable += 1u;
}
}
}
if (fall_back_num_reachable < num_reachable) {
fall_back_num_reachable = num_reachable;
fall_back = candidate;
}
}
return fall_back;
}
// Compute from which unvisited blocks is bb_id reachable through unvisited blocks.
static void ComputeUnvisitedReachableFrom(MIRGraph* mir_graph, BasicBlockId bb_id,
ArenaBitVector* reachable,
ScopedArenaVector<BasicBlockId>* tmp_stack) {
// NOTE: Loop heads indicated by the "visited" flag.
DCHECK(tmp_stack->empty());
reachable->ClearAllBits();
tmp_stack->push_back(bb_id);
while (!tmp_stack->empty()) {
BasicBlockId current_id = tmp_stack->back();
tmp_stack->pop_back();
BasicBlock* current_bb = mir_graph->GetBasicBlock(current_id);
DCHECK(current_bb != nullptr);
GrowableArray<BasicBlockId>::Iterator iter(current_bb->predecessors);
BasicBlock* pred_bb = mir_graph->GetBasicBlock(iter.Next());
for ( ; pred_bb != nullptr; pred_bb = mir_graph->GetBasicBlock(iter.Next())) {
if (!pred_bb->visited && !reachable->IsBitSet(pred_bb->id)) {
reachable->SetBit(pred_bb->id);
tmp_stack->push_back(pred_bb->id);
}
}
}
}
void MIRGraph::ComputeTopologicalSortOrder() {
ScopedArenaAllocator allocator(&cu_->arena_stack);
unsigned int num_blocks = GetNumBlocks();
ScopedArenaQueue<BasicBlock*> q(allocator.Adapter());
ScopedArenaVector<size_t> visited_cnt_values(num_blocks, 0u, allocator.Adapter());
ScopedArenaVector<BasicBlockId> loop_head_stack(allocator.Adapter());
size_t max_nested_loops = 0u;
ArenaBitVector loop_exit_blocks(&allocator, num_blocks, false, kBitMapMisc);
loop_exit_blocks.ClearAllBits();
// Count the number of blocks to process and add the entry block(s).
GrowableArray<BasicBlock*>::Iterator iterator(&block_list_);
unsigned int num_blocks_to_process = 0u;
for (BasicBlock* bb = iterator.Next(); bb != nullptr; bb = iterator.Next()) {
if (bb->hidden == true) {
continue;
}
num_blocks_to_process += 1u;
if (bb->predecessors->Size() == 0u) {
// Add entry block to the queue.
q.push(bb);
}
}
// Create the topological order if need be.
if (topological_order_ == nullptr) {
topological_order_ = new (arena_) GrowableArray<BasicBlockId>(arena_, num_blocks);
topological_order_loop_ends_ = new (arena_) GrowableArray<uint16_t>(arena_, num_blocks);
topological_order_indexes_ = new (arena_) GrowableArray<uint16_t>(arena_, num_blocks);
}
topological_order_->Reset();
topological_order_loop_ends_->Reset();
topological_order_indexes_->Reset();
topological_order_loop_ends_->Resize(num_blocks);
topological_order_indexes_->Resize(num_blocks);
for (BasicBlockId i = 0; i != num_blocks; ++i) {
topological_order_loop_ends_->Insert(0u);
topological_order_indexes_->Insert(static_cast<uint16_t>(-1));
}
// Mark all blocks as unvisited.
ClearAllVisitedFlags();
// For loop heads, keep track from which blocks they are reachable not going through other
// loop heads. Other loop heads are excluded to detect the heads of nested loops. The children
// in this set go into the loop body, the other children are jumping over the loop.
ScopedArenaVector<ArenaBitVector*> loop_head_reachable_from(allocator.Adapter());
loop_head_reachable_from.resize(num_blocks, nullptr);
// Reuse the same temp stack whenever calculating a loop_head_reachable_from[loop_head_id].
ScopedArenaVector<BasicBlockId> tmp_stack(allocator.Adapter());
while (num_blocks_to_process != 0u) {
BasicBlock* bb = nullptr;
if (!q.empty()) {
num_blocks_to_process -= 1u;
// Get top.
bb = q.front();
q.pop();
if (bb->visited) {
// Loop head: it was already processed, mark end and copy exit blocks to the queue.
DCHECK(q.empty()) << PrettyMethod(cu_->method_idx, *cu_->dex_file);
uint16_t idx = static_cast<uint16_t>(topological_order_->Size());
topological_order_loop_ends_->Put(topological_order_indexes_->Get(bb->id), idx);
DCHECK_EQ(loop_head_stack.back(), bb->id);
loop_head_stack.pop_back();
ArenaBitVector* reachable =
loop_head_stack.empty() ? nullptr : loop_head_reachable_from[loop_head_stack.back()];
for (BasicBlockId candidate_id : loop_exit_blocks.Indexes()) {
if (reachable == nullptr || reachable->IsBitSet(candidate_id)) {
q.push(GetBasicBlock(candidate_id));
// NOTE: The BitVectorSet::IndexIterator will not check the pointed-to bit again,
// so clearing the bit has no effect on the iterator.
loop_exit_blocks.ClearBit(candidate_id);
}
}
continue;
}
} else {
// Find the new loop head.
AllNodesIterator iter(this);
while (true) {
BasicBlock* candidate = iter.Next();
if (candidate == nullptr) {
// We did not find a true loop head, fall back to a reachable block in any loop.
ArenaBitVector* current_loop =
loop_head_stack.empty() ? nullptr : loop_head_reachable_from[loop_head_stack.back()];
bb = SelectTopologicalSortOrderFallBack(this, current_loop, &visited_cnt_values,
&allocator, &tmp_stack);
DCHECK(bb != nullptr) << PrettyMethod(cu_->method_idx, *cu_->dex_file);
if (kIsDebugBuild && cu_->dex_file != nullptr) {
LOG(INFO) << "Topological sort order: Using fall-back in "
<< PrettyMethod(cu_->method_idx, *cu_->dex_file) << " BB #" << bb->id
<< " @0x" << std::hex << bb->start_offset
<< ", num_blocks = " << std::dec << num_blocks;
}
break;
}
if (candidate->hidden || // Hidden, or
candidate->visited || // already processed, or
visited_cnt_values[candidate->id] == 0u || // no processed predecessors, or
(!loop_head_stack.empty() && // outside current loop.
!loop_head_reachable_from[loop_head_stack.back()]->IsBitSet(candidate->id))) {
continue;
}
GrowableArray<BasicBlockId>::Iterator pred_iter(candidate->predecessors);
BasicBlock* pred_bb = GetBasicBlock(pred_iter.Next());
for ( ; pred_bb != nullptr; pred_bb = GetBasicBlock(pred_iter.Next())) {
if (pred_bb != candidate && !pred_bb->visited &&
!pred_bb->dominators->IsBitSet(candidate->id)) {
break; // Keep non-null pred_bb to indicate failure.
}
}
if (pred_bb == nullptr) {
bb = candidate;
break;
}
}
// Compute blocks from which the loop head is reachable and process those blocks first.
ArenaBitVector* reachable =
new (&allocator) ArenaBitVector(&allocator, num_blocks, false, kBitMapMisc);
loop_head_reachable_from[bb->id] = reachable;
ComputeUnvisitedReachableFrom(this, bb->id, reachable, &tmp_stack);
// Now mark as loop head. (Even if it's only a fall back when we don't find a true loop.)
loop_head_stack.push_back(bb->id);
max_nested_loops = std::max(max_nested_loops, loop_head_stack.size());
}
DCHECK_EQ(bb->hidden, false);
DCHECK_EQ(bb->visited, false);
bb->visited = true;
// Now add the basic block.
uint16_t idx = static_cast<uint16_t>(topological_order_->Size());
topological_order_indexes_->Put(bb->id, idx);
topological_order_->Insert(bb->id);
// Update visited_cnt_values for children.
ChildBlockIterator succIter(bb, this);
BasicBlock* successor = succIter.Next();
for ( ; successor != nullptr; successor = succIter.Next()) {
if (successor->hidden) {
continue;
}
// One more predecessor was visited.
visited_cnt_values[successor->id] += 1u;
if (visited_cnt_values[successor->id] == successor->predecessors->Size()) {
if (loop_head_stack.empty() ||
loop_head_reachable_from[loop_head_stack.back()]->IsBitSet(successor->id)) {
q.push(successor);
} else {
DCHECK(!loop_exit_blocks.IsBitSet(successor->id));
loop_exit_blocks.SetBit(successor->id);
}
}
}
}
// Prepare the loop head stack for iteration.
topological_order_loop_head_stack_ =
new (arena_) GrowableArray<std::pair<uint16_t, bool>>(arena_, max_nested_loops);
}
bool BasicBlock::IsExceptionBlock() const {
if (block_type == kExceptionHandling) {
return true;
}
return false;
}
bool MIRGraph::HasSuspendTestBetween(BasicBlock* source, BasicBlockId target_id) {
BasicBlock* target = GetBasicBlock(target_id);
if (source == nullptr || target == nullptr)
return false;
int idx;
for (idx = gen_suspend_test_list_.Size() - 1; idx >= 0; idx--) {
BasicBlock* bb = gen_suspend_test_list_.Get(idx);
if (bb == source)
return true; // The block has been inserted by a suspend check before.
if (source->dominators->IsBitSet(bb->id) && bb->dominators->IsBitSet(target_id))
return true;
}
return false;
}
ChildBlockIterator::ChildBlockIterator(BasicBlock* bb, MIRGraph* mir_graph)
: basic_block_(bb), mir_graph_(mir_graph), visited_fallthrough_(false),
visited_taken_(false), have_successors_(false) {
// Check if we actually do have successors.
if (basic_block_ != 0 && basic_block_->successor_block_list_type != kNotUsed) {
have_successors_ = true;
successor_iter_.Reset(basic_block_->successor_blocks);
}
}
BasicBlock* ChildBlockIterator::Next() {
// We check if we have a basic block. If we don't we cannot get next child.
if (basic_block_ == nullptr) {
return nullptr;
}
// If we haven't visited fallthrough, return that.
if (visited_fallthrough_ == false) {
visited_fallthrough_ = true;
BasicBlock* result = mir_graph_->GetBasicBlock(basic_block_->fall_through);
if (result != nullptr) {
return result;
}
}
// If we haven't visited taken, return that.
if (visited_taken_ == false) {
visited_taken_ = true;
BasicBlock* result = mir_graph_->GetBasicBlock(basic_block_->taken);
if (result != nullptr) {
return result;
}
}
// We visited both taken and fallthrough. Now check if we have successors we need to visit.
if (have_successors_ == true) {
// Get information about next successor block.
for (SuccessorBlockInfo* successor_block_info = successor_iter_.Next();
successor_block_info != nullptr;
successor_block_info = successor_iter_.Next()) {
// If block was replaced by zero block, take next one.
if (successor_block_info->block != NullBasicBlockId) {
return mir_graph_->GetBasicBlock(successor_block_info->block);
}
}
}
// We do not have anything.
return nullptr;
}
BasicBlock* BasicBlock::Copy(CompilationUnit* c_unit) {
MIRGraph* mir_graph = c_unit->mir_graph.get();
return Copy(mir_graph);
}
BasicBlock* BasicBlock::Copy(MIRGraph* mir_graph) {
BasicBlock* result_bb = mir_graph->CreateNewBB(block_type);
// We don't do a memcpy style copy here because it would lead to a lot of things
// to clean up. Let us do it by hand instead.
// Copy in taken and fallthrough.
result_bb->fall_through = fall_through;
result_bb->taken = taken;
// Copy successor links if needed.
ArenaAllocator* arena = mir_graph->GetArena();
result_bb->successor_block_list_type = successor_block_list_type;
if (result_bb->successor_block_list_type != kNotUsed) {
size_t size = successor_blocks->Size();
result_bb->successor_blocks = new (arena) GrowableArray<SuccessorBlockInfo*>(arena, size, kGrowableArraySuccessorBlocks);
GrowableArray<SuccessorBlockInfo*>::Iterator iterator(successor_blocks);
while (true) {
SuccessorBlockInfo* sbi_old = iterator.Next();
if (sbi_old == nullptr) {
break;
}
SuccessorBlockInfo* sbi_new = static_cast<SuccessorBlockInfo*>(arena->Alloc(sizeof(SuccessorBlockInfo), kArenaAllocSuccessor));
memcpy(sbi_new, sbi_old, sizeof(SuccessorBlockInfo));
result_bb->successor_blocks->Insert(sbi_new);
}
}
// Copy offset, method.
result_bb->start_offset = start_offset;
// Now copy instructions.
for (MIR* mir = first_mir_insn; mir != 0; mir = mir->next) {
// Get a copy first.
MIR* copy = mir->Copy(mir_graph);
// Append it.
result_bb->AppendMIR(copy);
}
return result_bb;
}
MIR* MIR::Copy(MIRGraph* mir_graph) {
MIR* res = mir_graph->NewMIR();
*res = *this;
// Remove links
res->next = nullptr;
res->bb = NullBasicBlockId;
res->ssa_rep = nullptr;
return res;
}
MIR* MIR::Copy(CompilationUnit* c_unit) {
return Copy(c_unit->mir_graph.get());
}
uint32_t SSARepresentation::GetStartUseIndex(Instruction::Code opcode) {
// Default result.
int res = 0;
// We are basically setting the iputs to their igets counterparts.
switch (opcode) {
case Instruction::IPUT:
case Instruction::IPUT_OBJECT:
case Instruction::IPUT_BOOLEAN:
case Instruction::IPUT_BYTE:
case Instruction::IPUT_CHAR:
case Instruction::IPUT_SHORT:
case Instruction::IPUT_QUICK:
case Instruction::IPUT_OBJECT_QUICK:
case Instruction::IPUT_BOOLEAN_QUICK:
case Instruction::IPUT_BYTE_QUICK:
case Instruction::IPUT_CHAR_QUICK:
case Instruction::IPUT_SHORT_QUICK:
case Instruction::APUT:
case Instruction::APUT_OBJECT:
case Instruction::APUT_BOOLEAN:
case Instruction::APUT_BYTE:
case Instruction::APUT_CHAR:
case Instruction::APUT_SHORT:
case Instruction::SPUT:
case Instruction::SPUT_OBJECT:
case Instruction::SPUT_BOOLEAN:
case Instruction::SPUT_BYTE:
case Instruction::SPUT_CHAR:
case Instruction::SPUT_SHORT:
// Skip the VR containing what to store.
res = 1;
break;
case Instruction::IPUT_WIDE:
case Instruction::IPUT_WIDE_QUICK:
case Instruction::APUT_WIDE:
case Instruction::SPUT_WIDE:
// Skip the two VRs containing what to store.
res = 2;
break;
default:
// Do nothing in the general case.
break;
}
return res;
}
/**
* @brief Given a decoded instruction, it checks whether the instruction
* sets a constant and if it does, more information is provided about the
* constant being set.
* @param ptr_value pointer to a 64-bit holder for the constant.
* @param wide Updated by function whether a wide constant is being set by bytecode.
* @return Returns false if the decoded instruction does not represent a constant bytecode.
*/
bool MIR::DecodedInstruction::GetConstant(int64_t* ptr_value, bool* wide) const {
bool sets_const = true;
int64_t value = vB;
DCHECK(ptr_value != nullptr);
DCHECK(wide != nullptr);
switch (opcode) {
case Instruction::CONST_4:
case Instruction::CONST_16:
case Instruction::CONST:
*wide = false;
value <<= 32; // In order to get the sign extend.
value >>= 32;
break;
case Instruction::CONST_HIGH16:
*wide = false;
value <<= 48; // In order to get the sign extend.
value >>= 32;
break;
case Instruction::CONST_WIDE_16:
case Instruction::CONST_WIDE_32:
*wide = true;
value <<= 32; // In order to get the sign extend.
value >>= 32;
break;
case Instruction::CONST_WIDE:
*wide = true;
value = vB_wide;
break;
case Instruction::CONST_WIDE_HIGH16:
*wide = true;
value <<= 48; // In order to get the sign extend.
break;
default:
sets_const = false;
break;
}
if (sets_const) {
*ptr_value = value;
}
return sets_const;
}
void BasicBlock::ResetOptimizationFlags(uint16_t reset_flags) {
// Reset flags for all MIRs in bb.
for (MIR* mir = first_mir_insn; mir != NULL; mir = mir->next) {
mir->optimization_flags &= (~reset_flags);
}
}
void BasicBlock::Hide(CompilationUnit* c_unit) {
// First lets make it a dalvik bytecode block so it doesn't have any special meaning.
block_type = kDalvikByteCode;
// Mark it as hidden.
hidden = true;
// Detach it from its MIRs so we don't generate code for them. Also detached MIRs
// are updated to know that they no longer have a parent.
for (MIR* mir = first_mir_insn; mir != nullptr; mir = mir->next) {
mir->bb = NullBasicBlockId;
}
first_mir_insn = nullptr;
last_mir_insn = nullptr;
GrowableArray<BasicBlockId>::Iterator iterator(predecessors);
MIRGraph* mir_graph = c_unit->mir_graph.get();
while (true) {
BasicBlock* pred_bb = mir_graph->GetBasicBlock(iterator.Next());
if (pred_bb == nullptr) {
break;
}
// Sadly we have to go through the children by hand here.
pred_bb->ReplaceChild(id, NullBasicBlockId);
}
// Iterate through children of bb we are hiding.
ChildBlockIterator successorChildIter(this, mir_graph);
for (BasicBlock* childPtr = successorChildIter.Next(); childPtr != 0; childPtr = successorChildIter.Next()) {
// Replace child with null child.
childPtr->predecessors->Delete(id);
}
// Remove link to children.
taken = NullBasicBlockId;
fall_through = NullBasicBlockId;
successor_block_list_type = kNotUsed;
}
bool BasicBlock::IsSSALiveOut(const CompilationUnit* c_unit, int ssa_reg) {
// In order to determine if the ssa reg is live out, we scan all the MIRs. We remember
// the last SSA number of the same dalvik register. At the end, if it is different than ssa_reg,
// then it is not live out of this BB.
int dalvik_reg = c_unit->mir_graph->SRegToVReg(ssa_reg);
int last_ssa_reg = -1;
// Walk through the MIRs backwards.
for (MIR* mir = first_mir_insn; mir != nullptr; mir = mir->next) {
// Get ssa rep.
SSARepresentation *ssa_rep = mir->ssa_rep;
// Go through the defines for this MIR.
for (int i = 0; i < ssa_rep->num_defs; i++) {
DCHECK(ssa_rep->defs != nullptr);
// Get the ssa reg.
int def_ssa_reg = ssa_rep->defs[i];
// Get dalvik reg.
int def_dalvik_reg = c_unit->mir_graph->SRegToVReg(def_ssa_reg);
// Compare dalvik regs.
if (dalvik_reg == def_dalvik_reg) {
// We found a def of the register that we are being asked about.
// Remember it.
last_ssa_reg = def_ssa_reg;
}
}
}
if (last_ssa_reg == -1) {
// If we get to this point we couldn't find a define of register user asked about.
// Let's assume the user knows what he's doing so we can be safe and say that if we
// couldn't find a def, it is live out.
return true;
}
// If it is not -1, we found a match, is it ssa_reg?
return (ssa_reg == last_ssa_reg);
}
bool BasicBlock::ReplaceChild(BasicBlockId old_bb, BasicBlockId new_bb) {
// We need to check taken, fall_through, and successor_blocks to replace.
bool found = false;
if (taken == old_bb) {
taken = new_bb;
found = true;
}
if (fall_through == old_bb) {
fall_through = new_bb;
found = true;
}
if (successor_block_list_type != kNotUsed) {
GrowableArray<SuccessorBlockInfo*>::Iterator iterator(successor_blocks);
while (true) {
SuccessorBlockInfo* successor_block_info = iterator.Next();
if (successor_block_info == nullptr) {
break;
}
if (successor_block_info->block == old_bb) {
successor_block_info->block = new_bb;
found = true;
}
}
}
return found;
}
void BasicBlock::UpdatePredecessor(BasicBlockId old_parent, BasicBlockId new_parent) {
GrowableArray<BasicBlockId>::Iterator iterator(predecessors);
bool found = false;
while (true) {
BasicBlockId pred_bb_id = iterator.Next();
if (pred_bb_id == NullBasicBlockId) {
break;
}
if (pred_bb_id == old_parent) {
size_t idx = iterator.GetIndex() - 1;
predecessors->Put(idx, new_parent);
found = true;
break;
}
}
// If not found, add it.
if (found == false) {
predecessors->Insert(new_parent);
}
}
// Create a new basic block with block_id as num_blocks_ that is
// post-incremented.
BasicBlock* MIRGraph::CreateNewBB(BBType block_type) {
BasicBlock* res = NewMemBB(block_type, num_blocks_++);
block_list_.Insert(res);
return res;
}
void MIRGraph::CalculateBasicBlockInformation() {
PassDriverMEPostOpt driver(cu_);
driver.Launch();
}
void MIRGraph::InitializeBasicBlockData() {
num_blocks_ = block_list_.Size();
}
int MIR::DecodedInstruction::FlagsOf() const {
// Calculate new index.
int idx = static_cast<int>(opcode) - kNumPackedOpcodes;
// Check if it is an extended or not.
if (idx < 0) {
return Instruction::FlagsOf(opcode);
}
// For extended, we use a switch.
switch (static_cast<int>(opcode)) {
case kMirOpPhi:
return Instruction::kContinue;
case kMirOpCopy:
return Instruction::kContinue;
case kMirOpFusedCmplFloat:
return Instruction::kContinue | Instruction::kBranch;
case kMirOpFusedCmpgFloat:
return Instruction::kContinue | Instruction::kBranch;
case kMirOpFusedCmplDouble:
return Instruction::kContinue | Instruction::kBranch;
case kMirOpFusedCmpgDouble:
return Instruction::kContinue | Instruction::kBranch;
case kMirOpFusedCmpLong:
return Instruction::kContinue | Instruction::kBranch;
case kMirOpNop:
return Instruction::kContinue;
case kMirOpNullCheck:
return Instruction::kContinue | Instruction::kThrow;
case kMirOpRangeCheck:
return Instruction::kContinue | Instruction::kThrow;
case kMirOpDivZeroCheck:
return Instruction::kContinue | Instruction::kThrow;
case kMirOpCheck:
return 0;
case kMirOpCheckPart2:
return 0;
case kMirOpSelect:
return Instruction::kContinue;
case kMirOpConstVector:
return Instruction::kContinue;
case kMirOpMoveVector:
return Instruction::kContinue;
case kMirOpPackedMultiply:
return Instruction::kContinue;
case kMirOpPackedAddition:
return Instruction::kContinue;
case kMirOpPackedSubtract:
return Instruction::kContinue;
case kMirOpPackedShiftLeft:
return Instruction::kContinue;
case kMirOpPackedSignedShiftRight:
return Instruction::kContinue;
case kMirOpPackedUnsignedShiftRight:
return Instruction::kContinue;
case kMirOpPackedAnd:
return Instruction::kContinue;
case kMirOpPackedOr:
return Instruction::kContinue;
case kMirOpPackedXor:
return Instruction::kContinue;
case kMirOpPackedAddReduce:
return Instruction::kContinue;
case kMirOpPackedReduce:
return Instruction::kContinue;
case kMirOpPackedSet:
return Instruction::kContinue;
case kMirOpReserveVectorRegisters:
return Instruction::kContinue;
case kMirOpReturnVectorRegisters:
return Instruction::kContinue;
default:
LOG(WARNING) << "ExtendedFlagsOf: Unhandled case: " << static_cast<int> (opcode);
return 0;
}
}
} // namespace art
|