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
|
/*
* Copyright © 2014 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Authors:
* Connor Abbott (cwabbott0@gmail.com)
*
*/
#include "nir.h"
#include <assert.h>
nir_shader *
nir_shader_create(void *mem_ctx)
{
nir_shader *shader = ralloc(mem_ctx, nir_shader);
shader->uniforms = _mesa_hash_table_create(shader, _mesa_key_hash_string,
_mesa_key_string_equal);
shader->inputs = _mesa_hash_table_create(shader, _mesa_key_hash_string,
_mesa_key_string_equal);
shader->outputs = _mesa_hash_table_create(shader, _mesa_key_hash_string,
_mesa_key_string_equal);
shader->num_user_structures = 0;
shader->user_structures = NULL;
exec_list_make_empty(&shader->functions);
exec_list_make_empty(&shader->registers);
exec_list_make_empty(&shader->globals);
exec_list_make_empty(&shader->system_values);
shader->reg_alloc = 0;
shader->num_inputs = 0;
shader->num_outputs = 0;
shader->num_uniforms = 0;
return shader;
}
static nir_register *
reg_create(void *mem_ctx, struct exec_list *list)
{
nir_register *reg = ralloc(mem_ctx, nir_register);
reg->uses = _mesa_set_create(mem_ctx, _mesa_key_pointer_equal);
reg->defs = _mesa_set_create(mem_ctx, _mesa_key_pointer_equal);
reg->if_uses = _mesa_set_create(mem_ctx, _mesa_key_pointer_equal);
reg->num_components = 0;
reg->num_array_elems = 0;
reg->is_packed = false;
reg->name = NULL;
exec_list_push_tail(list, ®->node);
return reg;
}
nir_register *
nir_global_reg_create(nir_shader *shader)
{
nir_register *reg = reg_create(shader, &shader->registers);
reg->index = shader->reg_alloc++;
reg->is_global = true;
return reg;
}
nir_register *
nir_local_reg_create(nir_function_impl *impl)
{
nir_register *reg = reg_create(ralloc_parent(impl), &impl->registers);
reg->index = impl->reg_alloc++;
reg->is_global = false;
return reg;
}
void
nir_reg_remove(nir_register *reg)
{
exec_node_remove(®->node);
}
nir_function *
nir_function_create(nir_shader *shader, const char *name)
{
nir_function *func = ralloc(shader, nir_function);
exec_list_push_tail(&shader->functions, &func->node);
exec_list_make_empty(&func->overload_list);
func->name = name;
func->shader = shader;
return func;
}
nir_function_overload *
nir_function_overload_create(nir_function *func)
{
void *mem_ctx = ralloc_parent(func);
nir_function_overload *overload = ralloc(mem_ctx, nir_function_overload);
overload->num_params = 0;
overload->params = NULL;
overload->return_type = glsl_void_type();
overload->impl = NULL;
exec_list_push_tail(&func->overload_list, &overload->node);
overload->function = func;
return overload;
}
nir_src nir_src_copy(nir_src src, void *mem_ctx)
{
nir_src ret;
ret.is_ssa = src.is_ssa;
if (ret.is_ssa) {
ret.ssa = src.ssa;
} else {
ret.reg.base_offset = src.reg.base_offset;
ret.reg.reg = src.reg.reg;
if (src.reg.indirect) {
ret.reg.indirect = ralloc(mem_ctx, nir_src);
*ret.reg.indirect = *src.reg.indirect;
} else {
ret.reg.indirect = NULL;
}
}
return ret;
}
nir_dest nir_dest_copy(nir_dest dest, void *mem_ctx)
{
nir_dest ret;
ret.is_ssa = dest.is_ssa;
if (ret.is_ssa) {
ret.ssa = dest.ssa;
} else {
ret.reg.base_offset = dest.reg.base_offset;
ret.reg.reg = dest.reg.reg;
if (dest.reg.indirect) {
ret.reg.indirect = ralloc(mem_ctx, nir_src);
*ret.reg.indirect = *dest.reg.indirect;
} else {
ret.reg.indirect = NULL;
}
}
return ret;
}
static inline void
block_add_pred(nir_block *block, nir_block *pred)
{
_mesa_set_add(block->predecessors, _mesa_hash_pointer(pred), pred);
}
static void
cf_init(nir_cf_node *node, nir_cf_node_type type)
{
exec_node_init(&node->node);
node->parent = NULL;
node->type = type;
}
static void
link_blocks(nir_block *pred, nir_block *succ1, nir_block *succ2)
{
pred->successors[0] = succ1;
block_add_pred(succ1, pred);
pred->successors[1] = succ2;
if (succ2 != NULL)
block_add_pred(succ2, pred);
}
static void
unlink_blocks(nir_block *pred, nir_block *succ)
{
if (pred->successors[0] == succ) {
pred->successors[0] = pred->successors[1];
pred->successors[1] = NULL;
} else {
assert(pred->successors[1] == succ);
pred->successors[1] = NULL;
}
struct set_entry *entry = _mesa_set_search(succ->predecessors,
_mesa_hash_pointer(pred), pred);
assert(entry);
_mesa_set_remove(succ->predecessors, entry);
}
static void
unlink_block_successors(nir_block *block)
{
if (block->successors[0] != NULL)
unlink_blocks(block, block->successors[0]);
if (block->successors[1] != NULL)
unlink_blocks(block, block->successors[1]);
}
nir_function_impl *
nir_function_impl_create(nir_function_overload *overload)
{
assert(overload->impl == NULL);
void *mem_ctx = ralloc_parent(overload);
nir_function_impl *impl = ralloc(mem_ctx, nir_function_impl);
overload->impl = impl;
impl->overload = overload;
cf_init(&impl->cf_node, nir_cf_node_function);
exec_list_make_empty(&impl->body);
exec_list_make_empty(&impl->registers);
exec_list_make_empty(&impl->locals);
impl->num_params = 0;
impl->params = NULL;
impl->return_var = NULL;
impl->reg_alloc = 0;
impl->ssa_alloc = 0;
impl->valid_metadata = nir_metadata_none;
/* create start & end blocks */
nir_block *start_block = nir_block_create(mem_ctx);
nir_block *end_block = nir_block_create(mem_ctx);
start_block->cf_node.parent = &impl->cf_node;
end_block->cf_node.parent = &impl->cf_node;
impl->start_block = start_block;
impl->end_block = end_block;
exec_list_push_tail(&impl->body, &start_block->cf_node.node);
start_block->successors[0] = end_block;
block_add_pred(end_block, start_block);
return impl;
}
nir_block *
nir_block_create(void *mem_ctx)
{
nir_block *block = ralloc(mem_ctx, nir_block);
cf_init(&block->cf_node, nir_cf_node_block);
block->successors[0] = block->successors[1] = NULL;
block->predecessors = _mesa_set_create(mem_ctx, _mesa_key_pointer_equal);
block->imm_dom = NULL;
block->dom_frontier = _mesa_set_create(mem_ctx, _mesa_key_pointer_equal);
exec_list_make_empty(&block->instr_list);
return block;
}
static inline void
src_init(nir_src *src)
{
src->is_ssa = false;
src->reg.reg = NULL;
src->reg.indirect = NULL;
src->reg.base_offset = 0;
}
nir_if *
nir_if_create(void *mem_ctx)
{
nir_if *if_stmt = ralloc(mem_ctx, nir_if);
cf_init(&if_stmt->cf_node, nir_cf_node_if);
src_init(&if_stmt->condition);
nir_block *then = nir_block_create(mem_ctx);
exec_list_make_empty(&if_stmt->then_list);
exec_list_push_tail(&if_stmt->then_list, &then->cf_node.node);
then->cf_node.parent = &if_stmt->cf_node;
nir_block *else_stmt = nir_block_create(mem_ctx);
exec_list_make_empty(&if_stmt->else_list);
exec_list_push_tail(&if_stmt->else_list, &else_stmt->cf_node.node);
else_stmt->cf_node.parent = &if_stmt->cf_node;
return if_stmt;
}
nir_loop *
nir_loop_create(void *mem_ctx)
{
nir_loop *loop = ralloc(mem_ctx, nir_loop);
cf_init(&loop->cf_node, nir_cf_node_loop);
nir_block *body = nir_block_create(mem_ctx);
exec_list_make_empty(&loop->body);
exec_list_push_tail(&loop->body, &body->cf_node.node);
body->cf_node.parent = &loop->cf_node;
body->successors[0] = body;
block_add_pred(body, body);
return loop;
}
static void
instr_init(nir_instr *instr, nir_instr_type type)
{
instr->type = type;
instr->block = NULL;
exec_node_init(&instr->node);
}
static void
dest_init(nir_dest *dest)
{
dest->is_ssa = false;
dest->reg.reg = NULL;
dest->reg.indirect = NULL;
dest->reg.base_offset = 0;
}
static void
alu_dest_init(nir_alu_dest *dest)
{
dest_init(&dest->dest);
dest->saturate = false;
dest->write_mask = 0xf;
}
static void
alu_src_init(nir_alu_src *src)
{
src_init(&src->src);
src->abs = src->negate = false;
src->swizzle[0] = 0;
src->swizzle[1] = 1;
src->swizzle[2] = 2;
src->swizzle[3] = 3;
}
nir_alu_instr *
nir_alu_instr_create(void *mem_ctx, nir_op op)
{
unsigned num_srcs = nir_op_infos[op].num_inputs;
nir_alu_instr *instr =
ralloc_size(mem_ctx,
sizeof(nir_alu_instr) + num_srcs * sizeof(nir_alu_src));
instr_init(&instr->instr, nir_instr_type_alu);
instr->op = op;
alu_dest_init(&instr->dest);
for (unsigned i = 0; i < num_srcs; i++)
alu_src_init(&instr->src[i]);
instr->has_predicate = false;
src_init(&instr->predicate);
return instr;
}
nir_jump_instr *
nir_jump_instr_create(void *mem_ctx, nir_jump_type type)
{
nir_jump_instr *instr = ralloc(mem_ctx, nir_jump_instr);
instr_init(&instr->instr, nir_instr_type_jump);
instr->type = type;
return instr;
}
nir_load_const_instr *
nir_load_const_instr_create(void *mem_ctx)
{
nir_load_const_instr *instr = ralloc(mem_ctx, nir_load_const_instr);
instr_init(&instr->instr, nir_instr_type_load_const);
dest_init(&instr->dest);
instr->num_components = 0;
instr->array_elems = 0;
instr->has_predicate = false;
src_init(&instr->predicate);
return instr;
}
nir_intrinsic_instr *
nir_intrinsic_instr_create(void *mem_ctx, nir_intrinsic_op op)
{
unsigned num_srcs = nir_intrinsic_infos[op].num_srcs;
nir_intrinsic_instr *instr =
ralloc_size(mem_ctx,
sizeof(nir_intrinsic_instr) + num_srcs * sizeof(nir_src));
instr_init(&instr->instr, nir_instr_type_intrinsic);
instr->intrinsic = op;
if (nir_intrinsic_infos[op].has_dest)
dest_init(&instr->dest);
for (unsigned i = 0; i < num_srcs; i++)
src_init(&instr->src[i]);
instr->has_predicate = false;
src_init(&instr->predicate);
return instr;
}
nir_call_instr *
nir_call_instr_create(void *mem_ctx, nir_function_overload *callee)
{
nir_call_instr *instr = ralloc(mem_ctx, nir_call_instr);
instr_init(&instr->instr, nir_instr_type_call);
instr->callee = callee;
instr->num_params = callee->num_params;
instr->params = ralloc_array(mem_ctx, nir_deref_var *, instr->num_params);
instr->return_deref = NULL;
instr->has_predicate = false;
src_init(&instr->predicate);
return instr;
}
nir_tex_instr *
nir_tex_instr_create(void *mem_ctx, unsigned num_srcs)
{
nir_tex_instr *instr = ralloc(mem_ctx, nir_tex_instr);
instr_init(&instr->instr, nir_instr_type_texture);
dest_init(&instr->dest);
instr->num_srcs = num_srcs;
for (unsigned i = 0; i < num_srcs; i++)
src_init(&instr->src[i]);
instr->has_predicate = false;
src_init(&instr->predicate);
return instr;
}
nir_phi_instr *
nir_phi_instr_create(void *mem_ctx)
{
nir_phi_instr *instr = ralloc(mem_ctx, nir_phi_instr);
instr_init(&instr->instr, nir_instr_type_phi);
dest_init(&instr->dest);
exec_list_make_empty(&instr->srcs);
return instr;
}
nir_parallel_copy_instr *
nir_parallel_copy_instr_create(void *mem_ctx)
{
nir_parallel_copy_instr *instr = ralloc(mem_ctx, nir_parallel_copy_instr);
instr_init(&instr->instr, nir_instr_type_parallel_copy);
instr->at_end = false;
exec_list_make_empty(&instr->copies);
return instr;
}
nir_ssa_undef_instr *
nir_ssa_undef_instr_create(void *mem_ctx)
{
nir_ssa_undef_instr *instr = ralloc(mem_ctx, nir_ssa_undef_instr);
instr_init(&instr->instr, nir_instr_type_ssa_undef);
instr->def.name = NULL;
instr->def.parent_instr = &instr->instr;
return instr;
}
nir_deref_var *
nir_deref_var_create(void *mem_ctx, nir_variable *var)
{
nir_deref_var *deref = ralloc(mem_ctx, nir_deref_var);
deref->deref.deref_type = nir_deref_type_var;
deref->deref.child = NULL;
deref->deref.type = var->type;
deref->var = var;
return deref;
}
nir_deref_array *
nir_deref_array_create(void *mem_ctx)
{
nir_deref_array *deref = ralloc(mem_ctx, nir_deref_array);
deref->deref.deref_type = nir_deref_type_array;
deref->deref.child = NULL;
deref->has_indirect = false;
src_init(&deref->indirect);
deref->base_offset = 0;
return deref;
}
nir_deref_struct *
nir_deref_struct_create(void *mem_ctx, const char *field)
{
nir_deref_struct *deref = ralloc(mem_ctx, nir_deref_struct);
deref->deref.deref_type = nir_deref_type_struct;
deref->deref.child = NULL;
deref->elem = ralloc_strdup(deref, field);
return deref;
}
static nir_deref_var *
copy_deref_var(void *mem_ctx, nir_deref_var *deref)
{
nir_deref_var *ret = nir_deref_var_create(mem_ctx, deref->var);
if (deref->deref.child)
ret->deref.child = nir_copy_deref(mem_ctx, deref->deref.child);
return ret;
}
static nir_deref_array *
copy_deref_array(void *mem_ctx, nir_deref_array *deref)
{
nir_deref_array *ret = nir_deref_array_create(mem_ctx);
ret->base_offset = deref->base_offset;
if (deref->has_indirect)
ret->indirect = deref->indirect;
ret->deref.type = deref->deref.type;
if (deref->deref.child)
ret->deref.child = nir_copy_deref(mem_ctx, deref->deref.child);
return ret;
}
static nir_deref_struct *
copy_deref_struct(void *mem_ctx, nir_deref_struct *deref)
{
nir_deref_struct *ret = nir_deref_struct_create(mem_ctx, deref->elem);
ret->deref.type = deref->deref.type;
if (deref->deref.child)
ret->deref.child = nir_copy_deref(mem_ctx, deref->deref.child);
return ret;
}
nir_deref *
nir_copy_deref(void *mem_ctx, nir_deref *deref)
{
switch (deref->deref_type) {
case nir_deref_type_var:
return ©_deref_var(mem_ctx, nir_deref_as_var(deref))->deref;
case nir_deref_type_array:
return ©_deref_array(mem_ctx, nir_deref_as_array(deref))->deref;
case nir_deref_type_struct:
return ©_deref_struct(mem_ctx, nir_deref_as_struct(deref))->deref;
default:
unreachable("Invalid dereference type");
}
return NULL;
}
/**
* \name Control flow modification
*
* These functions modify the control flow tree while keeping the control flow
* graph up-to-date. The invariants respected are:
* 1. Each then statement, else statement, or loop body must have at least one
* control flow node.
* 2. Each if-statement and loop must have one basic block before it and one
* after.
* 3. Two basic blocks cannot be directly next to each other.
* 4. If a basic block has a jump instruction, there must be only one and it
* must be at the end of the block.
* 5. The CFG must always be connected - this means that we must insert a fake
* CFG edge for loops with no break statement.
*
* The purpose of the second one is so that we have places to insert code during
* GCM, as well as eliminating the possibility of critical edges.
*/
/*@{*/
static void
link_non_block_to_block(nir_cf_node *node, nir_block *block)
{
if (node->type == nir_cf_node_if) {
/*
* We're trying to link an if to a block after it; this just means linking
* the last block of the then and else branches.
*/
nir_if *if_stmt = nir_cf_node_as_if(node);
nir_cf_node *last_then = nir_if_last_then_node(if_stmt);
assert(last_then->type == nir_cf_node_block);
nir_block *last_then_block = nir_cf_node_as_block(last_then);
nir_cf_node *last_else = nir_if_last_else_node(if_stmt);
assert(last_else->type == nir_cf_node_block);
nir_block *last_else_block = nir_cf_node_as_block(last_else);
if (exec_list_is_empty(&last_then_block->instr_list) ||
nir_block_last_instr(last_then_block)->type != nir_instr_type_jump) {
unlink_block_successors(last_then_block);
link_blocks(last_then_block, block, NULL);
}
if (exec_list_is_empty(&last_else_block->instr_list) ||
nir_block_last_instr(last_else_block)->type != nir_instr_type_jump) {
unlink_block_successors(last_else_block);
link_blocks(last_else_block, block, NULL);
}
} else {
assert(node->type == nir_cf_node_loop);
/*
* We can only get to this codepath if we're inserting a new loop, or
* at least a loop with no break statements; we can't insert break
* statements into a loop when we haven't inserted it into the CFG
* because we wouldn't know which block comes after the loop
* and therefore, which block should be the successor of the block with
* the break). Therefore, we need to insert a fake edge (see invariant
* #5).
*/
nir_loop *loop = nir_cf_node_as_loop(node);
nir_cf_node *last = nir_loop_last_cf_node(loop);
assert(last->type == nir_cf_node_block);
nir_block *last_block = nir_cf_node_as_block(last);
last_block->successors[1] = block;
block_add_pred(block, last_block);
}
}
static void
link_block_to_non_block(nir_block *block, nir_cf_node *node)
{
if (node->type == nir_cf_node_if) {
/*
* We're trying to link a block to an if after it; this just means linking
* the block to the first block of the then and else branches.
*/
nir_if *if_stmt = nir_cf_node_as_if(node);
nir_cf_node *first_then = nir_if_first_then_node(if_stmt);
assert(first_then->type == nir_cf_node_block);
nir_block *first_then_block = nir_cf_node_as_block(first_then);
nir_cf_node *first_else = nir_if_first_else_node(if_stmt);
assert(first_else->type == nir_cf_node_block);
nir_block *first_else_block = nir_cf_node_as_block(first_else);
unlink_block_successors(block);
link_blocks(block, first_then_block, first_else_block);
} else {
/*
* For similar reasons as the corresponding case in
* link_non_block_to_block(), don't worry about if the loop header has
* any predecessors that need to be unlinked.
*/
assert(node->type == nir_cf_node_loop);
nir_loop *loop = nir_cf_node_as_loop(node);
nir_cf_node *loop_header = nir_loop_first_cf_node(loop);
assert(loop_header->type == nir_cf_node_block);
nir_block *loop_header_block = nir_cf_node_as_block(loop_header);
unlink_block_successors(block);
link_blocks(block, loop_header_block, NULL);
}
}
/**
* Takes a basic block and inserts a new empty basic block before it, making its
* predecessors point to the new block. This essentially splits the block into
* an empty header and a body so that another non-block CF node can be inserted
* between the two. Note that this does *not* link the two basic blocks, so
* some kind of cleanup *must* be performed after this call.
*/
static nir_block *
split_block_beginning(nir_block *block)
{
nir_block *new_block = nir_block_create(ralloc_parent(block));
new_block->cf_node.parent = block->cf_node.parent;
exec_node_insert_node_before(&block->cf_node.node, &new_block->cf_node.node);
struct set_entry *entry;
set_foreach(block->predecessors, entry) {
nir_block *pred = (nir_block *) entry->key;
unlink_blocks(pred, block);
link_blocks(pred, new_block, NULL);
}
return new_block;
}
/**
* Moves the successors of source to the successors of dest, leaving both
* successors of source NULL.
*/
static void
move_successors(nir_block *source, nir_block *dest)
{
nir_block *succ1 = source->successors[0];
if (succ1)
unlink_blocks(source, succ1);
nir_block *succ2 = source->successors[1];
if (succ2)
unlink_blocks(source, succ2);
unlink_block_successors(dest);
link_blocks(dest, succ1, succ2);
}
static nir_block *
split_block_end(nir_block *block)
{
nir_block *new_block = nir_block_create(ralloc_parent(block));
new_block->cf_node.parent = block->cf_node.parent;
exec_node_insert_after(&block->cf_node.node, &new_block->cf_node.node);
move_successors(block, new_block);
return new_block;
}
/**
* Inserts a non-basic block between two basic blocks and links them together.
*/
static void
insert_non_block(nir_block *before, nir_cf_node *node, nir_block *after)
{
node->parent = before->cf_node.parent;
exec_node_insert_after(&before->cf_node.node, &node->node);
link_block_to_non_block(before, node);
link_non_block_to_block(node, after);
}
/**
* Inserts a non-basic block before a basic block.
*/
static void
insert_non_block_before_block(nir_cf_node *node, nir_block *block)
{
/* split off the beginning of block into new_block */
nir_block *new_block = split_block_beginning(block);
/* insert our node in between new_block and block */
insert_non_block(new_block, node, block);
}
static void
insert_non_block_after_block(nir_block *block, nir_cf_node *node)
{
/* split off the end of block into new_block */
nir_block *new_block = split_block_end(block);
/* insert our node in between block and new_block */
insert_non_block(block, node, new_block);
}
/* walk up the control flow tree to find the innermost enclosed loop */
static nir_loop *
nearest_loop(nir_cf_node *node)
{
while (node->type != nir_cf_node_loop) {
node = node->parent;
}
return nir_cf_node_as_loop(node);
}
nir_function_impl *
nir_cf_node_get_function(nir_cf_node *node)
{
while (node->type != nir_cf_node_function) {
node = node->parent;
}
return nir_cf_node_as_function(node);
}
/*
* update the CFG after a jump instruction has been added to the end of a block
*/
static void
handle_jump(nir_block *block)
{
nir_instr *instr = nir_block_last_instr(block);
nir_jump_instr *jump_instr = nir_instr_as_jump(instr);
unlink_block_successors(block);
nir_function_impl *impl = nir_cf_node_get_function(&block->cf_node);
nir_metadata_dirty(impl, nir_metadata_none);
if (jump_instr->type == nir_jump_break ||
jump_instr->type == nir_jump_continue) {
nir_loop *loop = nearest_loop(&block->cf_node);
if (jump_instr->type == nir_jump_continue) {
nir_cf_node *first_node = nir_loop_first_cf_node(loop);
assert(first_node->type == nir_cf_node_block);
nir_block *first_block = nir_cf_node_as_block(first_node);
link_blocks(block, first_block, NULL);
} else {
nir_cf_node *after = nir_cf_node_next(&loop->cf_node);
assert(after->type == nir_cf_node_block);
nir_block *after_block = nir_cf_node_as_block(after);
link_blocks(block, after_block, NULL);
/* If we inserted a fake link, remove it */
nir_cf_node *last = nir_loop_last_cf_node(loop);
assert(last->type == nir_cf_node_block);
nir_block *last_block = nir_cf_node_as_block(last);
if (last_block->successors[1] != NULL)
unlink_blocks(last_block, after_block);
}
} else {
assert(jump_instr->type == nir_jump_return);
link_blocks(block, impl->end_block, NULL);
}
}
static void
handle_remove_jump(nir_block *block, nir_jump_type type)
{
unlink_block_successors(block);
if (exec_node_is_tail_sentinel(block->cf_node.node.next)) {
nir_cf_node *parent = block->cf_node.parent;
if (parent->type == nir_cf_node_if) {
nir_cf_node *next = nir_cf_node_next(parent);
assert(next->type == nir_cf_node_block);
nir_block *next_block = nir_cf_node_as_block(next);
link_blocks(block, next_block, NULL);
} else {
assert(parent->type == nir_cf_node_loop);
nir_loop *loop = nir_cf_node_as_loop(parent);
nir_cf_node *head = nir_loop_first_cf_node(loop);
assert(head->type == nir_cf_node_block);
nir_block *head_block = nir_cf_node_as_block(head);
link_blocks(block, head_block, NULL);
}
} else {
nir_cf_node *next = nir_cf_node_next(&block->cf_node);
if (next->type == nir_cf_node_if) {
nir_if *next_if = nir_cf_node_as_if(next);
nir_cf_node *first_then = nir_if_first_then_node(next_if);
assert(first_then->type == nir_cf_node_block);
nir_block *first_then_block = nir_cf_node_as_block(first_then);
nir_cf_node *first_else = nir_if_first_else_node(next_if);
assert(first_else->type == nir_cf_node_block);
nir_block *first_else_block = nir_cf_node_as_block(first_else);
link_blocks(block, first_then_block, first_else_block);
} else {
assert(next->type == nir_cf_node_loop);
nir_loop *next_loop = nir_cf_node_as_loop(next);
nir_cf_node *first = nir_loop_first_cf_node(next_loop);
assert(first->type == nir_cf_node_block);
nir_block *first_block = nir_cf_node_as_block(first);
link_blocks(block, first_block, NULL);
}
}
if (type == nir_jump_break) {
nir_loop *loop = nearest_loop(&block->cf_node);
nir_cf_node *next = nir_cf_node_next(&loop->cf_node);
assert(next->type == nir_cf_node_block);
nir_block *next_block = nir_cf_node_as_block(next);
if (next_block->predecessors->entries == 0) {
/* insert fake link */
nir_cf_node *last = nir_loop_last_cf_node(loop);
assert(last->type == nir_cf_node_block);
nir_block *last_block = nir_cf_node_as_block(last);
last_block->successors[1] = next_block;
block_add_pred(next_block, last_block);
}
}
nir_function_impl *impl = nir_cf_node_get_function(&block->cf_node);
nir_metadata_dirty(impl, nir_metadata_none);
}
/**
* Inserts a basic block before another by merging the instructions.
*
* @param block the target of the insertion
* @param before the block to be inserted - must not have been inserted before
* @param has_jump whether \before has a jump instruction at the end
*/
static void
insert_block_before_block(nir_block *block, nir_block *before, bool has_jump)
{
assert(!has_jump || exec_list_is_empty(&block->instr_list));
foreach_list_typed(nir_instr, instr, node, &before->instr_list) {
instr->block = block;
}
exec_list_prepend(&block->instr_list, &before->instr_list);
if (has_jump)
handle_jump(block);
}
/**
* Inserts a basic block after another by merging the instructions.
*
* @param block the target of the insertion
* @param after the block to be inserted - must not have been inserted before
* @param has_jump whether \after has a jump instruction at the end
*/
static void
insert_block_after_block(nir_block *block, nir_block *after, bool has_jump)
{
foreach_list_typed(nir_instr, instr, node, &after->instr_list) {
instr->block = block;
}
exec_list_append(&block->instr_list, &after->instr_list);
if (has_jump)
handle_jump(block);
}
static void
update_if_uses(nir_cf_node *node)
{
if (node->type != nir_cf_node_if)
return;
nir_if *if_stmt = nir_cf_node_as_if(node);
if (if_stmt->condition.is_ssa)
return;
nir_register *reg = if_stmt->condition.reg.reg;
assert(reg != NULL);
_mesa_set_add(reg->if_uses, _mesa_hash_pointer(if_stmt), if_stmt);
}
void
nir_cf_node_insert_after(nir_cf_node *node, nir_cf_node *after)
{
update_if_uses(after);
if (after->type == nir_cf_node_block) {
/*
* either node or the one after it must be a basic block, by invariant #2;
* in either case, just merge the blocks together.
*/
nir_block *after_block = nir_cf_node_as_block(after);
bool has_jump = !exec_list_is_empty(&after_block->instr_list) &&
nir_block_last_instr(after_block)->type == nir_instr_type_jump;
if (node->type == nir_cf_node_block) {
insert_block_after_block(nir_cf_node_as_block(node), after_block,
has_jump);
} else {
nir_cf_node *next = nir_cf_node_next(node);
assert(next->type == nir_cf_node_block);
nir_block *next_block = nir_cf_node_as_block(next);
insert_block_before_block(next_block, after_block, has_jump);
}
} else {
if (node->type == nir_cf_node_block) {
insert_non_block_after_block(nir_cf_node_as_block(node), after);
} else {
/*
* We have to insert a non-basic block after a non-basic block. Since
* every non-basic block has a basic block after it, this is equivalent
* to inserting a non-basic block before a basic block.
*/
nir_cf_node *next = nir_cf_node_next(node);
assert(next->type == nir_cf_node_block);
nir_block *next_block = nir_cf_node_as_block(next);
insert_non_block_before_block(after, next_block);
}
}
nir_function_impl *impl = nir_cf_node_get_function(node);
nir_metadata_dirty(impl, nir_metadata_none);
}
void
nir_cf_node_insert_before(nir_cf_node *node, nir_cf_node *before)
{
update_if_uses(before);
if (before->type == nir_cf_node_block) {
nir_block *before_block = nir_cf_node_as_block(before);
bool has_jump = !exec_list_is_empty(&before_block->instr_list) &&
nir_block_last_instr(before_block)->type == nir_instr_type_jump;
if (node->type == nir_cf_node_block) {
insert_block_before_block(nir_cf_node_as_block(node), before_block,
has_jump);
} else {
nir_cf_node *prev = nir_cf_node_prev(node);
assert(prev->type == nir_cf_node_block);
nir_block *prev_block = nir_cf_node_as_block(prev);
insert_block_after_block(prev_block, before_block, has_jump);
}
} else {
if (node->type == nir_cf_node_block) {
insert_non_block_before_block(before, nir_cf_node_as_block(node));
} else {
/*
* We have to insert a non-basic block before a non-basic block. This
* is equivalent to inserting a non-basic block after a basic block.
*/
nir_cf_node *prev_node = nir_cf_node_prev(node);
assert(prev_node->type == nir_cf_node_block);
nir_block *prev_block = nir_cf_node_as_block(prev_node);
insert_non_block_after_block(prev_block, before);
}
}
nir_function_impl *impl = nir_cf_node_get_function(node);
nir_metadata_dirty(impl, nir_metadata_none);
}
void
nir_cf_node_insert_begin(struct exec_list *list, nir_cf_node *node)
{
nir_cf_node *begin = exec_node_data(nir_cf_node, list->head, node);
nir_cf_node_insert_before(begin, node);
}
void
nir_cf_node_insert_end(struct exec_list *list, nir_cf_node *node)
{
nir_cf_node *end = exec_node_data(nir_cf_node, list->tail_pred, node);
nir_cf_node_insert_after(end, node);
}
/**
* Stitch two basic blocks together into one. The aggregate must have the same
* predecessors as the first and the same successors as the second.
*/
static void
stitch_blocks(nir_block *before, nir_block *after)
{
/*
* We move after into before, so we have to deal with up to 2 successors vs.
* possibly a large number of predecessors.
*
* TODO: special case when before is empty and after isn't?
*/
move_successors(after, before);
foreach_list_typed(nir_instr, instr, node, &after->instr_list) {
instr->block = before;
}
exec_list_append(&before->instr_list, &after->instr_list);
exec_node_remove(&after->cf_node.node);
}
void
nir_cf_node_remove(nir_cf_node *node)
{
nir_function_impl *impl = nir_cf_node_get_function(node);
nir_metadata_dirty(impl, nir_metadata_none);
if (node->type == nir_cf_node_block) {
/*
* Basic blocks can't really be removed by themselves, since they act as
* padding between the non-basic blocks. So all we do here is empty the
* block of instructions.
*
* TODO: could we assert here?
*/
exec_list_make_empty(&nir_cf_node_as_block(node)->instr_list);
} else {
nir_cf_node *before = nir_cf_node_prev(node);
assert(before->type == nir_cf_node_block);
nir_block *before_block = nir_cf_node_as_block(before);
nir_cf_node *after = nir_cf_node_next(node);
assert(after->type == nir_cf_node_block);
nir_block *after_block = nir_cf_node_as_block(after);
exec_node_remove(&node->node);
stitch_blocks(before_block, after_block);
}
}
static bool
add_use_cb(nir_src *src, void *state)
{
nir_instr *instr = (nir_instr *) state;
struct set *uses_set = src->is_ssa ? src->ssa->uses : src->reg.reg->uses;
_mesa_set_add(uses_set, _mesa_hash_pointer(instr), instr);
return true;
}
static bool
add_def_cb(nir_dest *dest, void *state)
{
nir_instr *instr = (nir_instr *) state;
if (dest->is_ssa)
return true;
nir_register *reg = dest->reg.reg;
_mesa_set_add(reg->defs, _mesa_hash_pointer(instr), instr);
return true;
}
static void
add_defs_uses(nir_instr *instr)
{
nir_foreach_src(instr, add_use_cb, instr);
nir_foreach_dest(instr, add_def_cb, instr);
}
void
nir_instr_insert_before(nir_instr *instr, nir_instr *before)
{
assert(before->type != nir_instr_type_jump);
before->block = instr->block;
add_defs_uses(before);
exec_node_insert_node_before(&instr->node, &before->node);
}
void
nir_instr_insert_after(nir_instr *instr, nir_instr *after)
{
if (after->type == nir_instr_type_jump) {
assert(instr == nir_block_last_instr(instr->block));
assert(instr->type != nir_instr_type_jump);
}
after->block = instr->block;
add_defs_uses(after);
exec_node_insert_after(&instr->node, &after->node);
if (after->type == nir_instr_type_jump)
handle_jump(after->block);
}
void
nir_instr_insert_before_block(nir_block *block, nir_instr *before)
{
if (before->type == nir_instr_type_jump)
assert(exec_list_is_empty(&block->instr_list));
before->block = block;
add_defs_uses(before);
exec_list_push_head(&block->instr_list, &before->node);
if (before->type == nir_instr_type_jump)
handle_jump(block);
}
void
nir_instr_insert_after_block(nir_block *block, nir_instr *after)
{
if (after->type == nir_instr_type_jump) {
assert(exec_list_is_empty(&block->instr_list) ||
nir_block_last_instr(block)->type != nir_instr_type_jump);
}
after->block = block;
add_defs_uses(after);
exec_list_push_tail(&block->instr_list, &after->node);
if (after->type == nir_instr_type_jump)
handle_jump(block);
}
void
nir_instr_insert_before_cf(nir_cf_node *node, nir_instr *before)
{
if (node->type == nir_cf_node_block) {
nir_instr_insert_before_block(nir_cf_node_as_block(node), before);
} else {
nir_cf_node *prev = nir_cf_node_prev(node);
assert(prev->type == nir_cf_node_block);
nir_block *prev_block = nir_cf_node_as_block(prev);
nir_instr_insert_before_block(prev_block, before);
}
}
void
nir_instr_insert_after_cf(nir_cf_node *node, nir_instr *after)
{
if (node->type == nir_cf_node_block) {
nir_instr_insert_after_block(nir_cf_node_as_block(node), after);
} else {
nir_cf_node *next = nir_cf_node_next(node);
assert(next->type == nir_cf_node_block);
nir_block *next_block = nir_cf_node_as_block(next);
nir_instr_insert_before_block(next_block, after);
}
}
void
nir_instr_insert_before_cf_list(struct exec_list *list, nir_instr *before)
{
nir_cf_node *first_node = exec_node_data(nir_cf_node,
exec_list_get_head(list), node);
nir_instr_insert_before_cf(first_node, before);
}
void
nir_instr_insert_after_cf_list(struct exec_list *list, nir_instr *after)
{
nir_cf_node *last_node = exec_node_data(nir_cf_node,
exec_list_get_tail(list), node);
nir_instr_insert_after_cf(last_node, after);
}
static bool
remove_use_cb(nir_src *src, void *state)
{
nir_instr *instr = (nir_instr *) state;
struct set *uses_set = src->is_ssa ? src->ssa->uses : src->reg.reg->uses;
struct set_entry *entry = _mesa_set_search(uses_set,
_mesa_hash_pointer(instr),
instr);
if (entry)
_mesa_set_remove(uses_set, entry);
return true;
}
static bool
remove_def_cb(nir_dest *dest, void *state)
{
nir_instr *instr = (nir_instr *) state;
if (dest->is_ssa)
return true;
nir_register *reg = dest->reg.reg;
struct set_entry *entry = _mesa_set_search(reg->defs,
_mesa_hash_pointer(instr),
instr);
if (entry)
_mesa_set_remove(reg->defs, entry);
return true;
}
static void
remove_defs_uses(nir_instr *instr)
{
nir_foreach_dest(instr, remove_def_cb, instr);
nir_foreach_src(instr, remove_use_cb, instr);
}
void nir_instr_remove(nir_instr *instr)
{
remove_defs_uses(instr);
exec_node_remove(&instr->node);
if (instr->type == nir_instr_type_jump) {
nir_jump_instr *jump_instr = nir_instr_as_jump(instr);
handle_remove_jump(instr->block, jump_instr->type);
}
}
/*@}*/
void
nir_index_local_regs(nir_function_impl *impl)
{
unsigned index = 0;
foreach_list_typed(nir_register, reg, node, &impl->registers) {
reg->index = index++;
}
impl->reg_alloc = index;
}
void
nir_index_global_regs(nir_shader *shader)
{
unsigned index = 0;
foreach_list_typed(nir_register, reg, node, &shader->registers) {
reg->index = index++;
}
shader->reg_alloc = index;
}
static bool
visit_alu_dest(nir_alu_instr *instr, nir_foreach_dest_cb cb, void *state)
{
return cb(&instr->dest.dest, state);
}
static bool
visit_intrinsic_dest(nir_intrinsic_instr *instr, nir_foreach_dest_cb cb,
void *state)
{
if (nir_intrinsic_infos[instr->intrinsic].has_dest)
return cb(&instr->dest, state);
return true;
}
static bool
visit_texture_dest(nir_tex_instr *instr, nir_foreach_dest_cb cb,
void *state)
{
return cb(&instr->dest, state);
}
static bool
visit_load_const_dest(nir_load_const_instr *instr, nir_foreach_dest_cb cb,
void *state)
{
return cb(&instr->dest, state);
}
static bool
visit_phi_dest(nir_phi_instr *instr, nir_foreach_dest_cb cb, void *state)
{
return cb(&instr->dest, state);
}
static bool
visit_parallel_copy_dest(nir_parallel_copy_instr *instr,
nir_foreach_dest_cb cb, void *state)
{
foreach_list_typed(nir_parallel_copy_copy, copy, node, &instr->copies) {
if (!cb(©->dest, state))
return false;
}
return true;
}
bool
nir_foreach_dest(nir_instr *instr, nir_foreach_dest_cb cb, void *state)
{
switch (instr->type) {
case nir_instr_type_alu:
return visit_alu_dest(nir_instr_as_alu(instr), cb, state);
case nir_instr_type_intrinsic:
return visit_intrinsic_dest(nir_instr_as_intrinsic(instr), cb, state);
case nir_instr_type_texture:
return visit_texture_dest(nir_instr_as_texture(instr), cb, state);
case nir_instr_type_load_const:
return visit_load_const_dest(nir_instr_as_load_const(instr), cb, state);
case nir_instr_type_phi:
return visit_phi_dest(nir_instr_as_phi(instr), cb, state);
case nir_instr_type_parallel_copy:
return visit_parallel_copy_dest(nir_instr_as_parallel_copy(instr),
cb, state);
case nir_instr_type_ssa_undef:
case nir_instr_type_call:
case nir_instr_type_jump:
break;
default:
unreachable("Invalid instruction type");
break;
}
return true;
}
static bool
visit_src(nir_src *src, nir_foreach_src_cb cb, void *state)
{
if (!cb(src, state))
return false;
if (!src->is_ssa && src->reg.indirect)
return cb(src->reg.indirect, state);
return true;
}
static bool
visit_deref_array_src(nir_deref_array *deref, nir_foreach_src_cb cb,
void *state)
{
if (deref->has_indirect)
return visit_src(&deref->indirect, cb, state);
return true;
}
static bool
visit_deref_src(nir_deref_var *deref, nir_foreach_src_cb cb, void *state)
{
nir_deref *cur = &deref->deref;
while (cur != NULL) {
if (cur->deref_type == nir_deref_type_array)
if (!visit_deref_array_src(nir_deref_as_array(cur), cb, state))
return false;
cur = cur->child;
}
return true;
}
static bool
visit_alu_src(nir_alu_instr *instr, nir_foreach_src_cb cb, void *state)
{
for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
if (!visit_src(&instr->src[i].src, cb, state))
return false;
if (instr->has_predicate)
if (!visit_src(&instr->predicate, cb, state))
return false;
return true;
}
static bool
visit_tex_src(nir_tex_instr *instr, nir_foreach_src_cb cb, void *state)
{
for (unsigned i = 0; i < instr->num_srcs; i++)
if (!visit_src(&instr->src[i], cb, state))
return false;
if (instr->has_predicate)
if (!visit_src(&instr->predicate, cb, state))
return false;
if (instr->sampler != NULL)
if (!visit_deref_src(instr->sampler, cb, state))
return false;
return true;
}
static bool
visit_intrinsic_src(nir_intrinsic_instr *instr, nir_foreach_src_cb cb,
void *state)
{
unsigned num_srcs = nir_intrinsic_infos[instr->intrinsic].num_srcs;
for (unsigned i = 0; i < num_srcs; i++)
if (!visit_src(&instr->src[i], cb, state))
return false;
unsigned num_vars =
nir_intrinsic_infos[instr->intrinsic].num_variables;
for (unsigned i = 0; i < num_vars; i++)
if (!visit_deref_src(instr->variables[i], cb, state))
return false;
if (instr->has_predicate)
if (!visit_src(&instr->predicate, cb, state))
return false;
return true;
}
static bool
visit_call_src(nir_call_instr *instr, nir_foreach_src_cb cb, void *state)
{
if (instr->has_predicate)
if (!visit_src(&instr->predicate, cb, state))
return false;
return true;
}
static bool
visit_load_const_src(nir_load_const_instr *instr, nir_foreach_src_cb cb,
void *state)
{
if (instr->has_predicate)
if (!visit_src(&instr->predicate, cb, state))
return false;
return true;
}
static bool
visit_phi_src(nir_phi_instr *instr, nir_foreach_src_cb cb, void *state)
{
foreach_list_typed(nir_phi_src, src, node, &instr->srcs) {
if (!visit_src(&src->src, cb, state))
return false;
}
return true;
}
static bool
visit_parallel_copy_src(nir_parallel_copy_instr *instr,
nir_foreach_src_cb cb, void *state)
{
foreach_list_typed(nir_parallel_copy_copy, copy, node, &instr->copies) {
if (!visit_src(©->src, cb, state))
return false;
}
return true;
}
typedef struct {
void *state;
nir_foreach_src_cb cb;
} visit_dest_indirect_state;
static bool
visit_dest_indirect(nir_dest *dest, void *_state)
{
visit_dest_indirect_state *state = (visit_dest_indirect_state *) _state;
if (!dest->is_ssa && dest->reg.indirect)
return state->cb(dest->reg.indirect, state->state);
return true;
}
bool
nir_foreach_src(nir_instr *instr, nir_foreach_src_cb cb, void *state)
{
switch (instr->type) {
case nir_instr_type_alu:
if (!visit_alu_src(nir_instr_as_alu(instr), cb, state))
return false;
break;
case nir_instr_type_intrinsic:
if (!visit_intrinsic_src(nir_instr_as_intrinsic(instr), cb, state))
return false;
break;
case nir_instr_type_texture:
if (!visit_tex_src(nir_instr_as_texture(instr), cb, state))
return false;
break;
case nir_instr_type_call:
if (!visit_call_src(nir_instr_as_call(instr), cb, state))
return false;
break;
case nir_instr_type_load_const:
if (!visit_load_const_src(nir_instr_as_load_const(instr), cb, state))
return false;
break;
case nir_instr_type_phi:
if (!visit_phi_src(nir_instr_as_phi(instr), cb, state))
return false;
break;
case nir_instr_type_parallel_copy:
if (!visit_parallel_copy_src(nir_instr_as_parallel_copy(instr),
cb, state))
return false;
break;
case nir_instr_type_jump:
case nir_instr_type_ssa_undef:
return true;
default:
unreachable("Invalid instruction type");
break;
}
visit_dest_indirect_state dest_state;
dest_state.state = state;
dest_state.cb = cb;
return nir_foreach_dest(instr, visit_dest_indirect, &dest_state);
}
void
nir_ssa_def_init(nir_function_impl *impl, nir_instr *instr, nir_ssa_def *def,
unsigned num_components, const char *name)
{
void *mem_ctx = ralloc_parent(instr);
def->name = name;
def->index = impl->ssa_alloc++;
def->parent_instr = instr;
def->uses = _mesa_set_create(mem_ctx, _mesa_key_pointer_equal);
def->if_uses = _mesa_set_create(mem_ctx, _mesa_key_pointer_equal);
def->num_components = num_components;
}
struct ssa_def_rewrite_state {
void *mem_ctx;
nir_ssa_def *old;
nir_src new_src;
};
static bool
ssa_def_rewrite_uses_src(nir_src *src, void *void_state)
{
struct ssa_def_rewrite_state *state = void_state;
if (src->is_ssa && src->ssa == state->old)
*src = nir_src_copy(state->new_src, state->mem_ctx);
return true;
}
void
nir_ssa_def_rewrite_uses(nir_ssa_def *def, nir_src new_src, void *mem_ctx)
{
struct ssa_def_rewrite_state state;
state.mem_ctx = mem_ctx;
state.old = def;
state.new_src = new_src;
assert(!new_src.is_ssa || def != new_src.ssa);
struct set *new_uses, *new_if_uses;
if (new_src.is_ssa) {
new_uses = new_src.ssa->uses;
new_if_uses = new_src.ssa->if_uses;
} else {
new_uses = new_src.reg.reg->uses;
new_if_uses = new_src.reg.reg->if_uses;
}
struct set_entry *entry;
set_foreach(def->uses, entry) {
nir_instr *instr = (nir_instr *)entry->key;
_mesa_set_remove(def->uses, entry);
nir_foreach_src(instr, ssa_def_rewrite_uses_src, &state);
_mesa_set_add(new_uses, _mesa_hash_pointer(instr), instr);
}
set_foreach(def->if_uses, entry) {
nir_if *if_use = (nir_if *)entry->key;
_mesa_set_remove(def->if_uses, entry);
if_use->condition = nir_src_copy(new_src, mem_ctx);
_mesa_set_add(new_if_uses, _mesa_hash_pointer(if_use), if_use);
}
}
static bool foreach_cf_node(nir_cf_node *node, nir_foreach_block_cb cb,
bool reverse, void *state);
static inline bool
foreach_if(nir_if *if_stmt, nir_foreach_block_cb cb, bool reverse, void *state)
{
if (reverse) {
foreach_list_typed_reverse(nir_cf_node, node, node, &if_stmt->else_list) {
if (!foreach_cf_node(node, cb, reverse, state))
return false;
}
foreach_list_typed_reverse(nir_cf_node, node, node, &if_stmt->then_list) {
if (!foreach_cf_node(node, cb, reverse, state))
return false;
}
} else {
foreach_list_typed(nir_cf_node, node, node, &if_stmt->then_list) {
if (!foreach_cf_node(node, cb, reverse, state))
return false;
}
foreach_list_typed(nir_cf_node, node, node, &if_stmt->else_list) {
if (!foreach_cf_node(node, cb, reverse, state))
return false;
}
}
return true;
}
static inline bool
foreach_loop(nir_loop *loop, nir_foreach_block_cb cb, bool reverse, void *state)
{
if (reverse) {
foreach_list_typed_reverse(nir_cf_node, node, node, &loop->body) {
if (!foreach_cf_node(node, cb, reverse, state))
return false;
}
} else {
foreach_list_typed(nir_cf_node, node, node, &loop->body) {
if (!foreach_cf_node(node, cb, reverse, state))
return false;
}
}
return true;
}
static bool
foreach_cf_node(nir_cf_node *node, nir_foreach_block_cb cb,
bool reverse, void *state)
{
switch (node->type) {
case nir_cf_node_block:
return cb(nir_cf_node_as_block(node), state);
case nir_cf_node_if:
return foreach_if(nir_cf_node_as_if(node), cb, reverse, state);
case nir_cf_node_loop:
return foreach_loop(nir_cf_node_as_loop(node), cb, reverse, state);
break;
default:
unreachable("Invalid CFG node type");
break;
}
return false;
}
bool
nir_foreach_block(nir_function_impl *impl, nir_foreach_block_cb cb, void *state)
{
foreach_list_typed(nir_cf_node, node, node, &impl->body) {
if (!foreach_cf_node(node, cb, false, state))
return false;
}
return cb(impl->end_block, state);
}
bool
nir_foreach_block_reverse(nir_function_impl *impl, nir_foreach_block_cb cb,
void *state)
{
if (!cb(impl->end_block, state))
return false;
foreach_list_typed_reverse(nir_cf_node, node, node, &impl->body) {
if (!foreach_cf_node(node, cb, true, state))
return false;
}
return true;
}
nir_if *
nir_block_following_if(nir_block *block)
{
if (exec_node_is_tail_sentinel(&block->cf_node.node))
return NULL;
if (nir_cf_node_is_last(&block->cf_node))
return NULL;
nir_cf_node *next_node = nir_cf_node_next(&block->cf_node);
if (next_node->type != nir_cf_node_if)
return NULL;
return nir_cf_node_as_if(next_node);
}
static bool
index_block(nir_block *block, void *state)
{
unsigned *index = (unsigned *) state;
block->index = (*index)++;
return true;
}
void
nir_index_blocks(nir_function_impl *impl)
{
unsigned index = 0;
if (impl->valid_metadata & nir_metadata_block_index)
return;
nir_foreach_block(impl, index_block, &index);
impl->num_blocks = index;
}
static void
index_ssa_def(nir_ssa_def *def, unsigned *index)
{
def->index = (*index)++;
}
static bool
index_ssa_def_cb(nir_dest *dest, void *state)
{
unsigned *index = (unsigned *) state;
if (dest->is_ssa)
index_ssa_def(&dest->ssa, index);
return true;
}
static void
index_ssa_undef(nir_ssa_undef_instr *instr, unsigned *index)
{
index_ssa_def(&instr->def, index);
}
static bool
index_ssa_block(nir_block *block, void *state)
{
unsigned *index = (unsigned *) state;
nir_foreach_instr(block, instr) {
if (instr->type == nir_instr_type_ssa_undef)
index_ssa_undef(nir_instr_as_ssa_undef(instr), index);
else
nir_foreach_dest(instr, index_ssa_def_cb, state);
}
return true;
}
void
nir_index_ssa_defs(nir_function_impl *impl)
{
unsigned index = 0;
nir_foreach_block(impl, index_ssa_block, &index);
impl->ssa_alloc = index;
}
|