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
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This contains code to emit Expr nodes as CIR code.
//
//===----------------------------------------------------------------------===//
#include "Address.h"
#include "CIRGenConstantEmitter.h"
#include "CIRGenFunction.h"
#include "CIRGenModule.h"
#include "CIRGenValue.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/Value.h"
#include "clang/AST/Attr.h"
#include "clang/AST/CharUnits.h"
#include "clang/AST/Decl.h"
#include "clang/AST/Expr.h"
#include "clang/AST/ExprCXX.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/MissingFeatures.h"
#include <optional>
using namespace clang;
using namespace clang::CIRGen;
using namespace cir;
/// Get the address of a zero-sized field within a record. The resulting address
/// doesn't necessarily have the right type.
Address CIRGenFunction::emitAddrOfFieldStorage(Address base,
const FieldDecl *field,
llvm::StringRef fieldName,
unsigned fieldIndex) {
if (field->isZeroSize(getContext())) {
cgm.errorNYI(field->getSourceRange(),
"emitAddrOfFieldStorage: zero-sized field");
return Address::invalid();
}
mlir::Location loc = getLoc(field->getLocation());
mlir::Type fieldType = convertType(field->getType());
auto fieldPtr = cir::PointerType::get(fieldType);
// For most cases fieldName is the same as field->getName() but for lambdas,
// which do not currently carry the name, so it can be passed down from the
// CaptureStmt.
cir::GetMemberOp memberAddr = builder.createGetMember(
loc, fieldPtr, base.getPointer(), fieldName, fieldIndex);
// Retrieve layout information, compute alignment and return the final
// address.
const RecordDecl *rec = field->getParent();
const CIRGenRecordLayout &layout = cgm.getTypes().getCIRGenRecordLayout(rec);
unsigned idx = layout.getCIRFieldNo(field);
CharUnits offset = CharUnits::fromQuantity(
layout.getCIRType().getElementOffset(cgm.getDataLayout().layout, idx));
return Address(memberAddr, base.getAlignment().alignmentAtOffset(offset));
}
/// Given an expression of pointer type, try to
/// derive a more accurate bound on the alignment of the pointer.
Address CIRGenFunction::emitPointerWithAlignment(const Expr *expr,
LValueBaseInfo *baseInfo) {
// We allow this with ObjC object pointers because of fragile ABIs.
assert(expr->getType()->isPointerType() ||
expr->getType()->isObjCObjectPointerType());
expr = expr->IgnoreParens();
// Casts:
if (auto const *ce = dyn_cast<CastExpr>(expr)) {
if (const auto *ece = dyn_cast<ExplicitCastExpr>(ce))
cgm.emitExplicitCastExprType(ece);
switch (ce->getCastKind()) {
// Non-converting casts (but not C's implicit conversion from void*).
case CK_BitCast:
case CK_NoOp:
case CK_AddressSpaceConversion: {
if (const auto *ptrTy =
ce->getSubExpr()->getType()->getAs<PointerType>()) {
if (ptrTy->getPointeeType()->isVoidType())
break;
LValueBaseInfo innerBaseInfo;
assert(!cir::MissingFeatures::opTBAA());
Address addr =
emitPointerWithAlignment(ce->getSubExpr(), &innerBaseInfo);
if (baseInfo)
*baseInfo = innerBaseInfo;
if (isa<ExplicitCastExpr>(ce)) {
LValueBaseInfo targetTypeBaseInfo;
const QualType pointeeType = expr->getType()->getPointeeType();
const CharUnits align =
cgm.getNaturalTypeAlignment(pointeeType, &targetTypeBaseInfo);
// If the source l-value is opaque, honor the alignment of the
// casted-to type.
if (innerBaseInfo.getAlignmentSource() != AlignmentSource::Decl) {
if (baseInfo)
baseInfo->mergeForCast(targetTypeBaseInfo);
addr = Address(addr.getPointer(), addr.getElementType(), align);
}
}
assert(!cir::MissingFeatures::sanitizers());
const mlir::Type eltTy =
convertTypeForMem(expr->getType()->getPointeeType());
addr = getBuilder().createElementBitCast(getLoc(expr->getSourceRange()),
addr, eltTy);
assert(!cir::MissingFeatures::addressSpace());
return addr;
}
break;
}
// Array-to-pointer decay. TODO(cir): BaseInfo and TBAAInfo.
case CK_ArrayToPointerDecay:
return emitArrayToPointerDecay(ce->getSubExpr(), baseInfo);
case CK_UncheckedDerivedToBase:
case CK_DerivedToBase: {
assert(!cir::MissingFeatures::opTBAA());
assert(!cir::MissingFeatures::addressIsKnownNonNull());
Address addr = emitPointerWithAlignment(ce->getSubExpr(), baseInfo);
const CXXRecordDecl *derived =
ce->getSubExpr()->getType()->getPointeeCXXRecordDecl();
return getAddressOfBaseClass(addr, derived, ce->path(),
shouldNullCheckClassCastValue(ce),
ce->getExprLoc());
}
case CK_AnyPointerToBlockPointerCast:
case CK_BaseToDerived:
case CK_BaseToDerivedMemberPointer:
case CK_BlockPointerToObjCPointerCast:
case CK_BuiltinFnToFnPtr:
case CK_CPointerToObjCPointerCast:
case CK_DerivedToBaseMemberPointer:
case CK_Dynamic:
case CK_FunctionToPointerDecay:
case CK_IntegralToPointer:
case CK_LValueToRValue:
case CK_LValueToRValueBitCast:
case CK_NullToMemberPointer:
case CK_NullToPointer:
case CK_ReinterpretMemberPointer:
// Common pointer conversions, nothing to do here.
// TODO: Is there any reason to treat base-to-derived conversions
// specially?
break;
case CK_ARCConsumeObject:
case CK_ARCExtendBlockObject:
case CK_ARCProduceObject:
case CK_ARCReclaimReturnedObject:
case CK_AtomicToNonAtomic:
case CK_BooleanToSignedIntegral:
case CK_ConstructorConversion:
case CK_CopyAndAutoreleaseBlockObject:
case CK_Dependent:
case CK_FixedPointCast:
case CK_FixedPointToBoolean:
case CK_FixedPointToFloating:
case CK_FixedPointToIntegral:
case CK_FloatingCast:
case CK_FloatingComplexCast:
case CK_FloatingComplexToBoolean:
case CK_FloatingComplexToIntegralComplex:
case CK_FloatingComplexToReal:
case CK_FloatingRealToComplex:
case CK_FloatingToBoolean:
case CK_FloatingToFixedPoint:
case CK_FloatingToIntegral:
case CK_HLSLAggregateSplatCast:
case CK_HLSLArrayRValue:
case CK_HLSLElementwiseCast:
case CK_HLSLVectorTruncation:
case CK_IntToOCLSampler:
case CK_IntegralCast:
case CK_IntegralComplexCast:
case CK_IntegralComplexToBoolean:
case CK_IntegralComplexToFloatingComplex:
case CK_IntegralComplexToReal:
case CK_IntegralRealToComplex:
case CK_IntegralToBoolean:
case CK_IntegralToFixedPoint:
case CK_IntegralToFloating:
case CK_LValueBitCast:
case CK_MatrixCast:
case CK_MemberPointerToBoolean:
case CK_NonAtomicToAtomic:
case CK_ObjCObjectLValueCast:
case CK_PointerToBoolean:
case CK_PointerToIntegral:
case CK_ToUnion:
case CK_ToVoid:
case CK_UserDefinedConversion:
case CK_VectorSplat:
case CK_ZeroToOCLOpaqueType:
llvm_unreachable("unexpected cast for emitPointerWithAlignment");
}
}
// Unary &
if (const UnaryOperator *uo = dyn_cast<UnaryOperator>(expr)) {
// TODO(cir): maybe we should use cir.unary for pointers here instead.
if (uo->getOpcode() == UO_AddrOf) {
LValue lv = emitLValue(uo->getSubExpr());
if (baseInfo)
*baseInfo = lv.getBaseInfo();
assert(!cir::MissingFeatures::opTBAA());
return lv.getAddress();
}
}
// std::addressof and variants.
if (auto const *call = dyn_cast<CallExpr>(expr)) {
switch (call->getBuiltinCallee()) {
default:
break;
case Builtin::BIaddressof:
case Builtin::BI__addressof:
case Builtin::BI__builtin_addressof: {
cgm.errorNYI(expr->getSourceRange(),
"emitPointerWithAlignment: builtin addressof");
return Address::invalid();
}
}
}
// Otherwise, use the alignment of the type.
return makeNaturalAddressForPointer(
emitScalarExpr(expr), expr->getType()->getPointeeType(), CharUnits(),
/*forPointeeType=*/true, baseInfo);
}
void CIRGenFunction::emitStoreThroughLValue(RValue src, LValue dst,
bool isInit) {
if (!dst.isSimple()) {
if (dst.isVectorElt()) {
// Read/modify/write the vector, inserting the new element
const mlir::Location loc = dst.getVectorPointer().getLoc();
const mlir::Value vector =
builder.createLoad(loc, dst.getVectorAddress());
const mlir::Value newVector = builder.create<cir::VecInsertOp>(
loc, vector, src.getValue(), dst.getVectorIdx());
builder.createStore(loc, newVector, dst.getVectorAddress());
return;
}
assert(dst.isBitField() && "Unknown LValue type");
emitStoreThroughBitfieldLValue(src, dst);
return;
cgm.errorNYI(dst.getPointer().getLoc(),
"emitStoreThroughLValue: non-simple lvalue");
return;
}
assert(!cir::MissingFeatures::opLoadStoreObjC());
assert(src.isScalar() && "Can't emit an aggregate store with this method");
emitStoreOfScalar(src.getValue(), dst, isInit);
}
static LValue emitGlobalVarDeclLValue(CIRGenFunction &cgf, const Expr *e,
const VarDecl *vd) {
QualType t = e->getType();
// If it's thread_local, emit a call to its wrapper function instead.
assert(!cir::MissingFeatures::opGlobalThreadLocal());
if (vd->getTLSKind() == VarDecl::TLS_Dynamic)
cgf.cgm.errorNYI(e->getSourceRange(),
"emitGlobalVarDeclLValue: thread_local variable");
// Check if the variable is marked as declare target with link clause in
// device codegen.
if (cgf.getLangOpts().OpenMP)
cgf.cgm.errorNYI(e->getSourceRange(), "emitGlobalVarDeclLValue: OpenMP");
// Traditional LLVM codegen handles thread local separately, CIR handles
// as part of getAddrOfGlobalVar.
mlir::Value v = cgf.cgm.getAddrOfGlobalVar(vd);
assert(!cir::MissingFeatures::addressSpace());
mlir::Type realVarTy = cgf.convertTypeForMem(vd->getType());
cir::PointerType realPtrTy = cgf.getBuilder().getPointerTo(realVarTy);
if (realPtrTy != v.getType())
v = cgf.getBuilder().createBitcast(v.getLoc(), v, realPtrTy);
CharUnits alignment = cgf.getContext().getDeclAlign(vd);
Address addr(v, realVarTy, alignment);
LValue lv;
if (vd->getType()->isReferenceType())
cgf.cgm.errorNYI(e->getSourceRange(),
"emitGlobalVarDeclLValue: reference type");
else
lv = cgf.makeAddrLValue(addr, t, AlignmentSource::Decl);
assert(!cir::MissingFeatures::setObjCGCLValueClass());
return lv;
}
void CIRGenFunction::emitStoreOfScalar(mlir::Value value, Address addr,
bool isVolatile, QualType ty,
bool isInit, bool isNontemporal) {
assert(!cir::MissingFeatures::opLoadStoreThreadLocal());
if (const auto *clangVecTy = ty->getAs<clang::VectorType>()) {
// Boolean vectors use `iN` as storage type.
if (clangVecTy->isExtVectorBoolType())
cgm.errorNYI(addr.getPointer().getLoc(),
"emitStoreOfScalar ExtVectorBoolType");
// Handle vectors of size 3 like size 4 for better performance.
const mlir::Type elementType = addr.getElementType();
const auto vecTy = cast<cir::VectorType>(elementType);
// TODO(CIR): Use `ABIInfo::getOptimalVectorMemoryType` once it upstreamed
assert(!cir::MissingFeatures::cirgenABIInfo());
if (vecTy.getSize() == 3 && !getLangOpts().PreserveVec3Type)
cgm.errorNYI(addr.getPointer().getLoc(),
"emitStoreOfScalar Vec3 & PreserveVec3Type disabled");
}
value = emitToMemory(value, ty);
assert(!cir::MissingFeatures::opLoadStoreAtomic());
// Update the alloca with more info on initialization.
assert(addr.getPointer() && "expected pointer to exist");
auto srcAlloca = addr.getDefiningOp<cir::AllocaOp>();
if (currVarDecl && srcAlloca) {
const VarDecl *vd = currVarDecl;
assert(vd && "VarDecl expected");
if (vd->hasInit())
srcAlloca.setInitAttr(mlir::UnitAttr::get(&getMLIRContext()));
}
assert(currSrcLoc && "must pass in source location");
builder.createStore(*currSrcLoc, value, addr, isVolatile);
if (isNontemporal) {
cgm.errorNYI(addr.getPointer().getLoc(), "emitStoreOfScalar nontemporal");
return;
}
assert(!cir::MissingFeatures::opTBAA());
}
// TODO: Replace this with a proper TargetInfo function call.
/// Helper method to check if the underlying ABI is AAPCS
static bool isAAPCS(const TargetInfo &targetInfo) {
return targetInfo.getABI().starts_with("aapcs");
}
mlir::Value CIRGenFunction::emitStoreThroughBitfieldLValue(RValue src,
LValue dst) {
const CIRGenBitFieldInfo &info = dst.getBitFieldInfo();
mlir::Type resLTy = convertTypeForMem(dst.getType());
Address ptr = dst.getBitFieldAddress();
bool useVoaltile = cgm.getCodeGenOpts().AAPCSBitfieldWidth &&
dst.isVolatileQualified() &&
info.volatileStorageSize != 0 && isAAPCS(cgm.getTarget());
mlir::Value dstAddr = dst.getAddress().getPointer();
return builder.createSetBitfield(dstAddr.getLoc(), resLTy, ptr,
ptr.getElementType(), src.getValue(), info,
dst.isVolatileQualified(), useVoaltile);
}
RValue CIRGenFunction::emitLoadOfBitfieldLValue(LValue lv, SourceLocation loc) {
const CIRGenBitFieldInfo &info = lv.getBitFieldInfo();
// Get the output type.
mlir::Type resLTy = convertType(lv.getType());
Address ptr = lv.getBitFieldAddress();
bool useVoaltile = lv.isVolatileQualified() && info.volatileOffset != 0 &&
isAAPCS(cgm.getTarget());
mlir::Value field =
builder.createGetBitfield(getLoc(loc), resLTy, ptr, ptr.getElementType(),
info, lv.isVolatile(), useVoaltile);
assert(!cir::MissingFeatures::opLoadEmitScalarRangeCheck() && "NYI");
return RValue::get(field);
}
Address CIRGenFunction::getAddrOfBitFieldStorage(LValue base,
const FieldDecl *field,
mlir::Type fieldType,
unsigned index) {
mlir::Location loc = getLoc(field->getLocation());
cir::PointerType fieldPtr = cir::PointerType::get(fieldType);
auto rec = cast<cir::RecordType>(base.getAddress().getElementType());
cir::GetMemberOp sea = getBuilder().createGetMember(
loc, fieldPtr, base.getPointer(), field->getName(),
rec.isUnion() ? field->getFieldIndex() : index);
CharUnits offset = CharUnits::fromQuantity(
rec.getElementOffset(cgm.getDataLayout().layout, index));
return Address(sea, base.getAlignment().alignmentAtOffset(offset));
}
LValue CIRGenFunction::emitLValueForBitField(LValue base,
const FieldDecl *field) {
LValueBaseInfo baseInfo = base.getBaseInfo();
const CIRGenRecordLayout &layout =
cgm.getTypes().getCIRGenRecordLayout(field->getParent());
const CIRGenBitFieldInfo &info = layout.getBitFieldInfo(field);
assert(!cir::MissingFeatures::preservedAccessIndexRegion());
unsigned idx = layout.getCIRFieldNo(field);
Address addr = getAddrOfBitFieldStorage(base, field, info.storageType, idx);
mlir::Location loc = getLoc(field->getLocation());
if (addr.getElementType() != info.storageType)
addr = builder.createElementBitCast(loc, addr, info.storageType);
QualType fieldType =
field->getType().withCVRQualifiers(base.getVRQualifiers());
// TODO(cir): Support TBAA for bit fields.
assert(!cir::MissingFeatures::opTBAA());
LValueBaseInfo fieldBaseInfo(baseInfo.getAlignmentSource());
return LValue::makeBitfield(addr, info, fieldType, fieldBaseInfo);
}
LValue CIRGenFunction::emitLValueForField(LValue base, const FieldDecl *field) {
LValueBaseInfo baseInfo = base.getBaseInfo();
if (field->isBitField())
return emitLValueForBitField(base, field);
QualType fieldType = field->getType();
const RecordDecl *rec = field->getParent();
AlignmentSource baseAlignSource = baseInfo.getAlignmentSource();
LValueBaseInfo fieldBaseInfo(getFieldAlignmentSource(baseAlignSource));
assert(!cir::MissingFeatures::opTBAA());
Address addr = base.getAddress();
if (auto *classDecl = dyn_cast<CXXRecordDecl>(rec)) {
if (cgm.getCodeGenOpts().StrictVTablePointers &&
classDecl->isDynamicClass()) {
cgm.errorNYI(field->getSourceRange(),
"emitLValueForField: strict vtable for dynamic class");
}
}
unsigned recordCVR = base.getVRQualifiers();
llvm::StringRef fieldName = field->getName();
unsigned fieldIndex;
if (cgm.lambdaFieldToName.count(field))
fieldName = cgm.lambdaFieldToName[field];
if (rec->isUnion())
fieldIndex = field->getFieldIndex();
else {
const CIRGenRecordLayout &layout =
cgm.getTypes().getCIRGenRecordLayout(field->getParent());
fieldIndex = layout.getCIRFieldNo(field);
}
addr = emitAddrOfFieldStorage(addr, field, fieldName, fieldIndex);
assert(!cir::MissingFeatures::preservedAccessIndexRegion());
// If this is a reference field, load the reference right now.
if (fieldType->isReferenceType()) {
assert(!cir::MissingFeatures::opTBAA());
LValue refLVal = makeAddrLValue(addr, fieldType, fieldBaseInfo);
if (recordCVR & Qualifiers::Volatile)
refLVal.getQuals().addVolatile();
addr = emitLoadOfReference(refLVal, getLoc(field->getSourceRange()),
&fieldBaseInfo);
// Qualifiers on the struct don't apply to the referencee.
recordCVR = 0;
fieldType = fieldType->getPointeeType();
}
if (field->hasAttr<AnnotateAttr>()) {
cgm.errorNYI(field->getSourceRange(), "emitLValueForField: AnnotateAttr");
return LValue();
}
LValue lv = makeAddrLValue(addr, fieldType, fieldBaseInfo);
lv.getQuals().addCVRQualifiers(recordCVR);
// __weak attribute on a field is ignored.
if (lv.getQuals().getObjCGCAttr() == Qualifiers::Weak) {
cgm.errorNYI(field->getSourceRange(),
"emitLValueForField: __weak attribute");
return LValue();
}
return lv;
}
LValue CIRGenFunction::emitLValueForFieldInitialization(
LValue base, const clang::FieldDecl *field, llvm::StringRef fieldName) {
QualType fieldType = field->getType();
if (!fieldType->isReferenceType())
return emitLValueForField(base, field);
const CIRGenRecordLayout &layout =
cgm.getTypes().getCIRGenRecordLayout(field->getParent());
unsigned fieldIndex = layout.getCIRFieldNo(field);
Address v =
emitAddrOfFieldStorage(base.getAddress(), field, fieldName, fieldIndex);
// Make sure that the address is pointing to the right type.
mlir::Type memTy = convertTypeForMem(fieldType);
v = builder.createElementBitCast(getLoc(field->getSourceRange()), v, memTy);
// TODO: Generate TBAA information that describes this access as a structure
// member access and not just an access to an object of the field's type. This
// should be similar to what we do in EmitLValueForField().
LValueBaseInfo baseInfo = base.getBaseInfo();
AlignmentSource fieldAlignSource = baseInfo.getAlignmentSource();
LValueBaseInfo fieldBaseInfo(getFieldAlignmentSource(fieldAlignSource));
assert(!cir::MissingFeatures::opTBAA());
return makeAddrLValue(v, fieldType, fieldBaseInfo);
}
mlir::Value CIRGenFunction::emitToMemory(mlir::Value value, QualType ty) {
// Bool has a different representation in memory than in registers,
// but in ClangIR, it is simply represented as a cir.bool value.
// This function is here as a placeholder for possible future changes.
return value;
}
void CIRGenFunction::emitStoreOfScalar(mlir::Value value, LValue lvalue,
bool isInit) {
if (lvalue.getType()->isConstantMatrixType()) {
assert(0 && "NYI: emitStoreOfScalar constant matrix type");
return;
}
emitStoreOfScalar(value, lvalue.getAddress(), lvalue.isVolatile(),
lvalue.getType(), isInit, /*isNontemporal=*/false);
}
mlir::Value CIRGenFunction::emitLoadOfScalar(Address addr, bool isVolatile,
QualType ty, SourceLocation loc,
LValueBaseInfo baseInfo) {
assert(!cir::MissingFeatures::opLoadStoreThreadLocal());
mlir::Type eltTy = addr.getElementType();
if (const auto *clangVecTy = ty->getAs<clang::VectorType>()) {
if (clangVecTy->isExtVectorBoolType()) {
cgm.errorNYI(loc, "emitLoadOfScalar: ExtVectorBoolType");
return nullptr;
}
const auto vecTy = cast<cir::VectorType>(eltTy);
// Handle vectors of size 3 like size 4 for better performance.
assert(!cir::MissingFeatures::cirgenABIInfo());
if (vecTy.getSize() == 3 && !getLangOpts().PreserveVec3Type)
cgm.errorNYI(addr.getPointer().getLoc(),
"emitLoadOfScalar Vec3 & PreserveVec3Type disabled");
}
assert(!cir::MissingFeatures::opLoadStoreTbaa());
LValue atomicLValue = LValue::makeAddr(addr, ty, baseInfo);
if (ty->isAtomicType() || isLValueSuitableForInlineAtomic(atomicLValue))
cgm.errorNYI("emitLoadOfScalar: load atomic");
if (mlir::isa<cir::VoidType>(eltTy))
cgm.errorNYI(loc, "emitLoadOfScalar: void type");
assert(!cir::MissingFeatures::opLoadEmitScalarRangeCheck());
mlir::Value loadOp = builder.createLoad(getLoc(loc), addr, isVolatile);
if (!ty->isBooleanType() && ty->hasBooleanRepresentation())
cgm.errorNYI("emitLoadOfScalar: boolean type with boolean representation");
return loadOp;
}
mlir::Value CIRGenFunction::emitLoadOfScalar(LValue lvalue,
SourceLocation loc) {
assert(!cir::MissingFeatures::opLoadStoreNontemporal());
assert(!cir::MissingFeatures::opLoadStoreTbaa());
return emitLoadOfScalar(lvalue.getAddress(), lvalue.isVolatile(),
lvalue.getType(), loc, lvalue.getBaseInfo());
}
/// Given an expression that represents a value lvalue, this
/// method emits the address of the lvalue, then loads the result as an rvalue,
/// returning the rvalue.
RValue CIRGenFunction::emitLoadOfLValue(LValue lv, SourceLocation loc) {
assert(!lv.getType()->isFunctionType());
assert(!(lv.getType()->isConstantMatrixType()) && "not implemented");
if (lv.isBitField())
return emitLoadOfBitfieldLValue(lv, loc);
if (lv.isSimple())
return RValue::get(emitLoadOfScalar(lv, loc));
if (lv.isVectorElt()) {
const mlir::Value load =
builder.createLoad(getLoc(loc), lv.getVectorAddress());
return RValue::get(builder.create<cir::VecExtractOp>(getLoc(loc), load,
lv.getVectorIdx()));
}
cgm.errorNYI(loc, "emitLoadOfLValue");
return RValue::get(nullptr);
}
static cir::FuncOp emitFunctionDeclPointer(CIRGenModule &cgm, GlobalDecl gd) {
assert(!cir::MissingFeatures::weakRefReference());
return cgm.getAddrOfFunction(gd);
}
static LValue emitCapturedFieldLValue(CIRGenFunction &cgf, const FieldDecl *fd,
mlir::Value thisValue) {
return cgf.emitLValueForLambdaField(fd, thisValue);
}
/// Given that we are currently emitting a lambda, emit an l-value for
/// one of its members.
///
LValue CIRGenFunction::emitLValueForLambdaField(const FieldDecl *field,
mlir::Value thisValue) {
bool hasExplicitObjectParameter = false;
const auto *methD = dyn_cast_if_present<CXXMethodDecl>(curCodeDecl);
LValue lambdaLV;
if (methD) {
hasExplicitObjectParameter = methD->isExplicitObjectMemberFunction();
assert(methD->getParent()->isLambda());
assert(methD->getParent() == field->getParent());
}
if (hasExplicitObjectParameter) {
cgm.errorNYI(field->getSourceRange(), "ExplicitObjectMemberFunction");
} else {
QualType lambdaTagType =
getContext().getCanonicalTagType(field->getParent());
lambdaLV = makeNaturalAlignAddrLValue(thisValue, lambdaTagType);
}
return emitLValueForField(lambdaLV, field);
}
LValue CIRGenFunction::emitLValueForLambdaField(const FieldDecl *field) {
return emitLValueForLambdaField(field, cxxabiThisValue);
}
static LValue emitFunctionDeclLValue(CIRGenFunction &cgf, const Expr *e,
GlobalDecl gd) {
const FunctionDecl *fd = cast<FunctionDecl>(gd.getDecl());
cir::FuncOp funcOp = emitFunctionDeclPointer(cgf.cgm, gd);
mlir::Location loc = cgf.getLoc(e->getSourceRange());
CharUnits align = cgf.getContext().getDeclAlign(fd);
assert(!cir::MissingFeatures::sanitizers());
mlir::Type fnTy = funcOp.getFunctionType();
mlir::Type ptrTy = cir::PointerType::get(fnTy);
mlir::Value addr = cgf.getBuilder().create<cir::GetGlobalOp>(
loc, ptrTy, funcOp.getSymName());
if (funcOp.getFunctionType() != cgf.convertType(fd->getType())) {
fnTy = cgf.convertType(fd->getType());
ptrTy = cir::PointerType::get(fnTy);
addr = cir::CastOp::create(cgf.getBuilder(), addr.getLoc(), ptrTy,
cir::CastKind::bitcast, addr);
}
return cgf.makeAddrLValue(Address(addr, fnTy, align), e->getType(),
AlignmentSource::Decl);
}
/// Determine whether we can emit a reference to \p vd from the current
/// context, despite not necessarily having seen an odr-use of the variable in
/// this context.
/// TODO(cir): This could be shared with classic codegen.
static bool canEmitSpuriousReferenceToVariable(CIRGenFunction &cgf,
const DeclRefExpr *e,
const VarDecl *vd) {
// For a variable declared in an enclosing scope, do not emit a spurious
// reference even if we have a capture, as that will emit an unwarranted
// reference to our capture state, and will likely generate worse code than
// emitting a local copy.
if (e->refersToEnclosingVariableOrCapture())
return false;
// For a local declaration declared in this function, we can always reference
// it even if we don't have an odr-use.
if (vd->hasLocalStorage()) {
return vd->getDeclContext() ==
dyn_cast_or_null<DeclContext>(cgf.curCodeDecl);
}
// For a global declaration, we can emit a reference to it if we know
// for sure that we are able to emit a definition of it.
vd = vd->getDefinition(cgf.getContext());
if (!vd)
return false;
// Don't emit a spurious reference if it might be to a variable that only
// exists on a different device / target.
// FIXME: This is unnecessarily broad. Check whether this would actually be a
// cross-target reference.
if (cgf.getLangOpts().OpenMP || cgf.getLangOpts().CUDA ||
cgf.getLangOpts().OpenCL) {
return false;
}
// We can emit a spurious reference only if the linkage implies that we'll
// be emitting a non-interposable symbol that will be retained until link
// time.
switch (cgf.cgm.getCIRLinkageVarDefinition(vd, /*IsConstant=*/false)) {
case cir::GlobalLinkageKind::ExternalLinkage:
case cir::GlobalLinkageKind::LinkOnceODRLinkage:
case cir::GlobalLinkageKind::WeakODRLinkage:
case cir::GlobalLinkageKind::InternalLinkage:
case cir::GlobalLinkageKind::PrivateLinkage:
return true;
default:
return false;
}
}
LValue CIRGenFunction::emitDeclRefLValue(const DeclRefExpr *e) {
const NamedDecl *nd = e->getDecl();
QualType ty = e->getType();
assert(e->isNonOdrUse() != NOUR_Unevaluated &&
"should not emit an unevaluated operand");
if (const auto *vd = dyn_cast<VarDecl>(nd)) {
// Global Named registers access via intrinsics only
if (vd->getStorageClass() == SC_Register && vd->hasAttr<AsmLabelAttr>() &&
!vd->isLocalVarDecl()) {
cgm.errorNYI(e->getSourceRange(),
"emitDeclRefLValue: Global Named registers access");
return LValue();
}
if (e->isNonOdrUse() == NOUR_Constant &&
(vd->getType()->isReferenceType() ||
!canEmitSpuriousReferenceToVariable(*this, e, vd))) {
cgm.errorNYI(e->getSourceRange(), "emitDeclRefLValue: NonOdrUse");
return LValue();
}
// Check for captured variables.
if (e->refersToEnclosingVariableOrCapture()) {
vd = vd->getCanonicalDecl();
if (FieldDecl *fd = lambdaCaptureFields.lookup(vd))
return emitCapturedFieldLValue(*this, fd, cxxabiThisValue);
assert(!cir::MissingFeatures::cgCapturedStmtInfo());
assert(!cir::MissingFeatures::openMP());
}
}
if (const auto *vd = dyn_cast<VarDecl>(nd)) {
// Checks for omitted feature handling
assert(!cir::MissingFeatures::opAllocaStaticLocal());
assert(!cir::MissingFeatures::opAllocaNonGC());
assert(!cir::MissingFeatures::opAllocaImpreciseLifetime());
assert(!cir::MissingFeatures::opAllocaTLS());
assert(!cir::MissingFeatures::opAllocaOpenMPThreadPrivate());
assert(!cir::MissingFeatures::opAllocaEscapeByReference());
// Check if this is a global variable
if (vd->hasLinkage() || vd->isStaticDataMember())
return emitGlobalVarDeclLValue(*this, e, vd);
Address addr = Address::invalid();
// The variable should generally be present in the local decl map.
auto iter = localDeclMap.find(vd);
if (iter != localDeclMap.end()) {
addr = iter->second;
} else {
// Otherwise, it might be static local we haven't emitted yet for some
// reason; most likely, because it's in an outer function.
cgm.errorNYI(e->getSourceRange(), "emitDeclRefLValue: static local");
}
// Drill into reference types.
LValue lv =
vd->getType()->isReferenceType()
? emitLoadOfReferenceLValue(addr, getLoc(e->getSourceRange()),
vd->getType(), AlignmentSource::Decl)
: makeAddrLValue(addr, ty, AlignmentSource::Decl);
// Statics are defined as globals, so they are not include in the function's
// symbol table.
assert((vd->isStaticLocal() || symbolTable.count(vd)) &&
"non-static locals should be already mapped");
return lv;
}
if (const auto *bd = dyn_cast<BindingDecl>(nd)) {
if (e->refersToEnclosingVariableOrCapture()) {
assert(!cir::MissingFeatures::lambdaCaptures());
cgm.errorNYI(e->getSourceRange(), "emitDeclRefLValue: lambda captures");
return LValue();
}
return emitLValue(bd->getBinding());
}
if (const auto *fd = dyn_cast<FunctionDecl>(nd)) {
LValue lv = emitFunctionDeclLValue(*this, e, fd);
// Emit debuginfo for the function declaration if the target wants to.
if (getContext().getTargetInfo().allowDebugInfoForExternalRef())
assert(!cir::MissingFeatures::generateDebugInfo());
return lv;
}
cgm.errorNYI(e->getSourceRange(), "emitDeclRefLValue: unhandled decl type");
return LValue();
}
mlir::Value CIRGenFunction::evaluateExprAsBool(const Expr *e) {
QualType boolTy = getContext().BoolTy;
SourceLocation loc = e->getExprLoc();
assert(!cir::MissingFeatures::pgoUse());
if (e->getType()->getAs<MemberPointerType>()) {
cgm.errorNYI(e->getSourceRange(),
"evaluateExprAsBool: member pointer type");
return createDummyValue(getLoc(loc), boolTy);
}
assert(!cir::MissingFeatures::cgFPOptionsRAII());
if (!e->getType()->isAnyComplexType())
return emitScalarConversion(emitScalarExpr(e), e->getType(), boolTy, loc);
return emitComplexToScalarConversion(emitComplexExpr(e), e->getType(), boolTy,
loc);
}
LValue CIRGenFunction::emitUnaryOpLValue(const UnaryOperator *e) {
UnaryOperatorKind op = e->getOpcode();
// __extension__ doesn't affect lvalue-ness.
if (op == UO_Extension)
return emitLValue(e->getSubExpr());
switch (op) {
case UO_Deref: {
QualType t = e->getSubExpr()->getType()->getPointeeType();
assert(!t.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
assert(!cir::MissingFeatures::opTBAA());
LValueBaseInfo baseInfo;
Address addr = emitPointerWithAlignment(e->getSubExpr(), &baseInfo);
// Tag 'load' with deref attribute.
// FIXME: This misses some derefence cases and has problematic interactions
// with other operators.
if (auto loadOp = addr.getDefiningOp<cir::LoadOp>())
loadOp.setIsDerefAttr(mlir::UnitAttr::get(&getMLIRContext()));
LValue lv = makeAddrLValue(addr, t, baseInfo);
assert(!cir::MissingFeatures::addressSpace());
assert(!cir::MissingFeatures::setNonGC());
return lv;
}
case UO_Real:
case UO_Imag: {
LValue lv = emitLValue(e->getSubExpr());
assert(lv.isSimple() && "real/imag on non-ordinary l-value");
// __real is valid on scalars. This is a faster way of testing that.
// __imag can only produce an rvalue on scalars.
if (e->getOpcode() == UO_Real &&
!mlir::isa<cir::ComplexType>(lv.getAddress().getElementType())) {
assert(e->getSubExpr()->getType()->isArithmeticType());
return lv;
}
QualType exprTy = getContext().getCanonicalType(e->getSubExpr()->getType());
QualType elemTy = exprTy->castAs<clang::ComplexType>()->getElementType();
mlir::Location loc = getLoc(e->getExprLoc());
Address component =
e->getOpcode() == UO_Real
? builder.createComplexRealPtr(loc, lv.getAddress())
: builder.createComplexImagPtr(loc, lv.getAddress());
assert(!cir::MissingFeatures::opTBAA());
LValue elemLV = makeAddrLValue(component, elemTy);
elemLV.getQuals().addQualifiers(lv.getQuals());
return elemLV;
}
case UO_PreInc:
case UO_PreDec: {
cir::UnaryOpKind kind =
e->isIncrementOp() ? cir::UnaryOpKind::Inc : cir::UnaryOpKind::Dec;
LValue lv = emitLValue(e->getSubExpr());
assert(e->isPrefix() && "Prefix operator in unexpected state!");
if (e->getType()->isAnyComplexType()) {
emitComplexPrePostIncDec(e, lv, kind, /*isPre=*/true);
} else {
emitScalarPrePostIncDec(e, lv, kind, /*isPre=*/true);
}
return lv;
}
case UO_Extension:
llvm_unreachable("UnaryOperator extension should be handled above!");
case UO_Plus:
case UO_Minus:
case UO_Not:
case UO_LNot:
case UO_AddrOf:
case UO_PostInc:
case UO_PostDec:
case UO_Coawait:
llvm_unreachable("UnaryOperator of non-lvalue kind!");
}
llvm_unreachable("Unknown unary operator kind!");
}
/// If the specified expr is a simple decay from an array to pointer,
/// return the array subexpression.
/// FIXME: this could be abstracted into a common AST helper.
static const Expr *getSimpleArrayDecayOperand(const Expr *e) {
// If this isn't just an array->pointer decay, bail out.
const auto *castExpr = dyn_cast<CastExpr>(e);
if (!castExpr || castExpr->getCastKind() != CK_ArrayToPointerDecay)
return nullptr;
// If this is a decay from variable width array, bail out.
const Expr *subExpr = castExpr->getSubExpr();
if (subExpr->getType()->isVariableArrayType())
return nullptr;
return subExpr;
}
static cir::IntAttr getConstantIndexOrNull(mlir::Value idx) {
// TODO(cir): should we consider using MLIRs IndexType instead of IntegerAttr?
if (auto constantOp = idx.getDefiningOp<cir::ConstantOp>())
return constantOp.getValueAttr<cir::IntAttr>();
return {};
}
static CharUnits getArrayElementAlign(CharUnits arrayAlign, mlir::Value idx,
CharUnits eltSize) {
// If we have a constant index, we can use the exact offset of the
// element we're accessing.
if (const cir::IntAttr constantIdx = getConstantIndexOrNull(idx)) {
const CharUnits offset = constantIdx.getValue().getZExtValue() * eltSize;
return arrayAlign.alignmentAtOffset(offset);
}
// Otherwise, use the worst-case alignment for any element.
return arrayAlign.alignmentOfArrayElement(eltSize);
}
static QualType getFixedSizeElementType(const ASTContext &astContext,
const VariableArrayType *vla) {
QualType eltType;
do {
eltType = vla->getElementType();
} while ((vla = astContext.getAsVariableArrayType(eltType)));
return eltType;
}
static mlir::Value emitArraySubscriptPtr(CIRGenFunction &cgf,
mlir::Location beginLoc,
mlir::Location endLoc, mlir::Value ptr,
mlir::Type eltTy, mlir::Value idx,
bool shouldDecay) {
CIRGenModule &cgm = cgf.getCIRGenModule();
// TODO(cir): LLVM codegen emits in bound gep check here, is there anything
// that would enhance tracking this later in CIR?
assert(!cir::MissingFeatures::emitCheckedInBoundsGEP());
return cgm.getBuilder().getArrayElement(beginLoc, endLoc, ptr, eltTy, idx,
shouldDecay);
}
static Address emitArraySubscriptPtr(CIRGenFunction &cgf,
mlir::Location beginLoc,
mlir::Location endLoc, Address addr,
QualType eltType, mlir::Value idx,
mlir::Location loc, bool shouldDecay) {
// Determine the element size of the statically-sized base. This is
// the thing that the indices are expressed in terms of.
if (const VariableArrayType *vla =
cgf.getContext().getAsVariableArrayType(eltType)) {
eltType = getFixedSizeElementType(cgf.getContext(), vla);
}
// We can use that to compute the best alignment of the element.
const CharUnits eltSize = cgf.getContext().getTypeSizeInChars(eltType);
const CharUnits eltAlign =
getArrayElementAlign(addr.getAlignment(), idx, eltSize);
assert(!cir::MissingFeatures::preservedAccessIndexRegion());
const mlir::Value eltPtr =
emitArraySubscriptPtr(cgf, beginLoc, endLoc, addr.getPointer(),
addr.getElementType(), idx, shouldDecay);
const mlir::Type elementType = cgf.convertTypeForMem(eltType);
return Address(eltPtr, elementType, eltAlign);
}
LValue
CIRGenFunction::emitArraySubscriptExpr(const clang::ArraySubscriptExpr *e) {
if (isa<ExtVectorElementExpr>(e->getBase())) {
cgm.errorNYI(e->getSourceRange(),
"emitArraySubscriptExpr: ExtVectorElementExpr");
return LValue::makeAddr(Address::invalid(), e->getType(), LValueBaseInfo());
}
if (getContext().getAsVariableArrayType(e->getType())) {
cgm.errorNYI(e->getSourceRange(),
"emitArraySubscriptExpr: VariableArrayType");
return LValue::makeAddr(Address::invalid(), e->getType(), LValueBaseInfo());
}
if (e->getType()->getAs<ObjCObjectType>()) {
cgm.errorNYI(e->getSourceRange(), "emitArraySubscriptExpr: ObjCObjectType");
return LValue::makeAddr(Address::invalid(), e->getType(), LValueBaseInfo());
}
// The index must always be an integer, which is not an aggregate. Emit it
// in lexical order (this complexity is, sadly, required by C++17).
assert((e->getIdx() == e->getLHS() || e->getIdx() == e->getRHS()) &&
"index was neither LHS nor RHS");
auto emitIdxAfterBase = [&](bool promote) -> mlir::Value {
const mlir::Value idx = emitScalarExpr(e->getIdx());
// Extend or truncate the index type to 32 or 64-bits.
auto ptrTy = mlir::dyn_cast<cir::PointerType>(idx.getType());
if (promote && ptrTy && ptrTy.isPtrTo<cir::IntType>())
cgm.errorNYI(e->getSourceRange(),
"emitArraySubscriptExpr: index type cast");
return idx;
};
// If the base is a vector type, then we are forming a vector element
// with this subscript.
if (e->getBase()->getType()->isVectorType() &&
!isa<ExtVectorElementExpr>(e->getBase())) {
const mlir::Value idx = emitIdxAfterBase(/*promote=*/false);
const LValue lhs = emitLValue(e->getBase());
return LValue::makeVectorElt(lhs.getAddress(), idx, e->getBase()->getType(),
lhs.getBaseInfo());
}
const mlir::Value idx = emitIdxAfterBase(/*promote=*/true);
if (const Expr *array = getSimpleArrayDecayOperand(e->getBase())) {
LValue arrayLV;
if (const auto *ase = dyn_cast<ArraySubscriptExpr>(array))
arrayLV = emitArraySubscriptExpr(ase);
else
arrayLV = emitLValue(array);
// Propagate the alignment from the array itself to the result.
const Address addr = emitArraySubscriptPtr(
*this, cgm.getLoc(array->getBeginLoc()), cgm.getLoc(array->getEndLoc()),
arrayLV.getAddress(), e->getType(), idx, cgm.getLoc(e->getExprLoc()),
/*shouldDecay=*/true);
const LValue lv = LValue::makeAddr(addr, e->getType(), LValueBaseInfo());
if (getLangOpts().ObjC && getLangOpts().getGC() != LangOptions::NonGC) {
cgm.errorNYI(e->getSourceRange(), "emitArraySubscriptExpr: ObjC with GC");
}
return lv;
}
// The base must be a pointer; emit it with an estimate of its alignment.
assert(e->getBase()->getType()->isPointerType() &&
"The base must be a pointer");
LValueBaseInfo eltBaseInfo;
const Address ptrAddr = emitPointerWithAlignment(e->getBase(), &eltBaseInfo);
// Propagate the alignment from the array itself to the result.
const Address addxr = emitArraySubscriptPtr(
*this, cgm.getLoc(e->getBeginLoc()), cgm.getLoc(e->getEndLoc()), ptrAddr,
e->getType(), idx, cgm.getLoc(e->getExprLoc()),
/*shouldDecay=*/false);
const LValue lv = LValue::makeAddr(addxr, e->getType(), eltBaseInfo);
if (getLangOpts().ObjC && getLangOpts().getGC() != LangOptions::NonGC) {
cgm.errorNYI(e->getSourceRange(), "emitArraySubscriptExpr: ObjC with GC");
}
return lv;
}
LValue CIRGenFunction::emitStringLiteralLValue(const StringLiteral *e) {
cir::GlobalOp globalOp = cgm.getGlobalForStringLiteral(e);
assert(globalOp.getAlignment() && "expected alignment for string literal");
unsigned align = *(globalOp.getAlignment());
mlir::Value addr =
builder.createGetGlobal(getLoc(e->getSourceRange()), globalOp);
return makeAddrLValue(
Address(addr, globalOp.getSymType(), CharUnits::fromQuantity(align)),
e->getType(), AlignmentSource::Decl);
}
/// Casts are never lvalues unless that cast is to a reference type. If the cast
/// is to a reference, we can have the usual lvalue result, otherwise if a cast
/// is needed by the code generator in an lvalue context, then it must mean that
/// we need the address of an aggregate in order to access one of its members.
/// This can happen for all the reasons that casts are permitted with aggregate
/// result, including noop aggregate casts, and cast from scalar to union.
LValue CIRGenFunction::emitCastLValue(const CastExpr *e) {
switch (e->getCastKind()) {
case CK_ToVoid:
case CK_BitCast:
case CK_LValueToRValueBitCast:
case CK_ArrayToPointerDecay:
case CK_FunctionToPointerDecay:
case CK_NullToMemberPointer:
case CK_NullToPointer:
case CK_IntegralToPointer:
case CK_PointerToIntegral:
case CK_PointerToBoolean:
case CK_IntegralCast:
case CK_BooleanToSignedIntegral:
case CK_IntegralToBoolean:
case CK_IntegralToFloating:
case CK_FloatingToIntegral:
case CK_FloatingToBoolean:
case CK_FloatingCast:
case CK_FloatingRealToComplex:
case CK_FloatingComplexToReal:
case CK_FloatingComplexToBoolean:
case CK_FloatingComplexCast:
case CK_FloatingComplexToIntegralComplex:
case CK_IntegralRealToComplex:
case CK_IntegralComplexToReal:
case CK_IntegralComplexToBoolean:
case CK_IntegralComplexCast:
case CK_IntegralComplexToFloatingComplex:
case CK_DerivedToBaseMemberPointer:
case CK_BaseToDerivedMemberPointer:
case CK_MemberPointerToBoolean:
case CK_ReinterpretMemberPointer:
case CK_AnyPointerToBlockPointerCast:
case CK_ARCProduceObject:
case CK_ARCConsumeObject:
case CK_ARCReclaimReturnedObject:
case CK_ARCExtendBlockObject:
case CK_CopyAndAutoreleaseBlockObject:
case CK_IntToOCLSampler:
case CK_FloatingToFixedPoint:
case CK_FixedPointToFloating:
case CK_FixedPointCast:
case CK_FixedPointToBoolean:
case CK_FixedPointToIntegral:
case CK_IntegralToFixedPoint:
case CK_MatrixCast:
case CK_HLSLVectorTruncation:
case CK_HLSLArrayRValue:
case CK_HLSLElementwiseCast:
case CK_HLSLAggregateSplatCast:
llvm_unreachable("unexpected cast lvalue");
case CK_Dependent:
llvm_unreachable("dependent cast kind in IR gen!");
case CK_BuiltinFnToFnPtr:
llvm_unreachable("builtin functions are handled elsewhere");
// These are never l-values; just use the aggregate emission code.
case CK_NonAtomicToAtomic:
case CK_AtomicToNonAtomic:
case CK_Dynamic:
case CK_ToUnion:
case CK_BaseToDerived:
case CK_AddressSpaceConversion:
case CK_ObjCObjectLValueCast:
case CK_VectorSplat:
case CK_ConstructorConversion:
case CK_UserDefinedConversion:
case CK_CPointerToObjCPointerCast:
case CK_BlockPointerToObjCPointerCast:
case CK_LValueToRValue: {
cgm.errorNYI(e->getSourceRange(),
std::string("emitCastLValue for unhandled cast kind: ") +
e->getCastKindName());
return {};
}
case CK_LValueBitCast: {
// This must be a reinterpret_cast (or c-style equivalent).
const auto *ce = cast<ExplicitCastExpr>(e);
cgm.emitExplicitCastExprType(ce, this);
LValue LV = emitLValue(e->getSubExpr());
Address V = LV.getAddress().withElementType(
builder, convertTypeForMem(ce->getTypeAsWritten()->getPointeeType()));
return makeAddrLValue(V, e->getType(), LV.getBaseInfo());
}
case CK_NoOp: {
// CK_NoOp can model a qualification conversion, which can remove an array
// bound and change the IR type.
LValue lv = emitLValue(e->getSubExpr());
// Propagate the volatile qualifier to LValue, if exists in e.
if (e->changesVolatileQualification())
cgm.errorNYI(e->getSourceRange(),
"emitCastLValue: NoOp changes volatile qual");
if (lv.isSimple()) {
Address v = lv.getAddress();
if (v.isValid()) {
mlir::Type ty = convertTypeForMem(e->getType());
if (v.getElementType() != ty)
cgm.errorNYI(e->getSourceRange(),
"emitCastLValue: NoOp needs bitcast");
}
}
return lv;
}
case CK_UncheckedDerivedToBase:
case CK_DerivedToBase: {
auto *derivedClassDecl = e->getSubExpr()->getType()->castAsCXXRecordDecl();
LValue lv = emitLValue(e->getSubExpr());
Address thisAddr = lv.getAddress();
// Perform the derived-to-base conversion
Address baseAddr =
getAddressOfBaseClass(thisAddr, derivedClassDecl, e->path(),
/*NullCheckValue=*/false, e->getExprLoc());
// TODO: Support accesses to members of base classes in TBAA. For now, we
// conservatively pretend that the complete object is of the base class
// type.
assert(!cir::MissingFeatures::opTBAA());
return makeAddrLValue(baseAddr, e->getType(), lv.getBaseInfo());
}
case CK_ZeroToOCLOpaqueType:
llvm_unreachable("NULL to OpenCL opaque type lvalue cast is not valid");
}
llvm_unreachable("Invalid cast kind");
}
static DeclRefExpr *tryToConvertMemberExprToDeclRefExpr(CIRGenFunction &cgf,
const MemberExpr *me) {
if (auto *vd = dyn_cast<VarDecl>(me->getMemberDecl())) {
// Try to emit static variable member expressions as DREs.
return DeclRefExpr::Create(
cgf.getContext(), NestedNameSpecifierLoc(), SourceLocation(), vd,
/*RefersToEnclosingVariableOrCapture=*/false, me->getExprLoc(),
me->getType(), me->getValueKind(), nullptr, nullptr, me->isNonOdrUse());
}
return nullptr;
}
LValue CIRGenFunction::emitMemberExpr(const MemberExpr *e) {
if (DeclRefExpr *dre = tryToConvertMemberExprToDeclRefExpr(*this, e)) {
emitIgnoredExpr(e->getBase());
return emitDeclRefLValue(dre);
}
Expr *baseExpr = e->getBase();
// If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
LValue baseLV;
if (e->isArrow()) {
LValueBaseInfo baseInfo;
assert(!cir::MissingFeatures::opTBAA());
Address addr = emitPointerWithAlignment(baseExpr, &baseInfo);
QualType ptrTy = baseExpr->getType()->getPointeeType();
assert(!cir::MissingFeatures::typeChecks());
baseLV = makeAddrLValue(addr, ptrTy, baseInfo);
} else {
assert(!cir::MissingFeatures::typeChecks());
baseLV = emitLValue(baseExpr);
}
const NamedDecl *nd = e->getMemberDecl();
if (auto *field = dyn_cast<FieldDecl>(nd)) {
LValue lv = emitLValueForField(baseLV, field);
assert(!cir::MissingFeatures::setObjCGCLValueClass());
if (getLangOpts().OpenMP) {
// If the member was explicitly marked as nontemporal, mark it as
// nontemporal. If the base lvalue is marked as nontemporal, mark access
// to children as nontemporal too.
cgm.errorNYI(e->getSourceRange(), "emitMemberExpr: OpenMP");
}
return lv;
}
if (isa<FunctionDecl>(nd)) {
cgm.errorNYI(e->getSourceRange(), "emitMemberExpr: FunctionDecl");
return LValue();
}
llvm_unreachable("Unhandled member declaration!");
}
/// Evaluate an expression into a given memory location.
void CIRGenFunction::emitAnyExprToMem(const Expr *e, Address location,
Qualifiers quals, bool isInit) {
// FIXME: This function should take an LValue as an argument.
switch (getEvaluationKind(e->getType())) {
case cir::TEK_Complex: {
LValue lv = makeAddrLValue(location, e->getType());
emitComplexExprIntoLValue(e, lv, isInit);
return;
}
case cir::TEK_Aggregate: {
emitAggExpr(e, AggValueSlot::forAddr(location, quals,
AggValueSlot::IsDestructed_t(isInit),
AggValueSlot::IsAliased_t(!isInit),
AggValueSlot::MayOverlap));
return;
}
case cir::TEK_Scalar: {
RValue rv = RValue::get(emitScalarExpr(e));
LValue lv = makeAddrLValue(location, e->getType());
emitStoreThroughLValue(rv, lv);
return;
}
}
llvm_unreachable("bad evaluation kind");
}
static Address createReferenceTemporary(CIRGenFunction &cgf,
const MaterializeTemporaryExpr *m,
const Expr *inner) {
// TODO(cir): cgf.getTargetHooks();
switch (m->getStorageDuration()) {
case SD_FullExpression:
case SD_Automatic: {
QualType ty = inner->getType();
assert(!cir::MissingFeatures::mergeAllConstants());
// The temporary memory should be created in the same scope as the extending
// declaration of the temporary materialization expression.
cir::AllocaOp extDeclAlloca;
if (const ValueDecl *extDecl = m->getExtendingDecl()) {
auto extDeclAddrIter = cgf.localDeclMap.find(extDecl);
if (extDeclAddrIter != cgf.localDeclMap.end())
extDeclAlloca = extDeclAddrIter->second.getDefiningOp<cir::AllocaOp>();
}
mlir::OpBuilder::InsertPoint ip;
if (extDeclAlloca)
ip = {extDeclAlloca->getBlock(), extDeclAlloca->getIterator()};
return cgf.createMemTemp(ty, cgf.getLoc(m->getSourceRange()),
cgf.getCounterRefTmpAsString(), /*alloca=*/nullptr,
ip);
}
case SD_Thread:
case SD_Static: {
cgf.cgm.errorNYI(
m->getSourceRange(),
"createReferenceTemporary: static/thread storage duration");
return Address::invalid();
}
case SD_Dynamic:
llvm_unreachable("temporary can't have dynamic storage duration");
}
llvm_unreachable("unknown storage duration");
}
static void pushTemporaryCleanup(CIRGenFunction &cgf,
const MaterializeTemporaryExpr *m,
const Expr *e, Address referenceTemporary) {
// Objective-C++ ARC:
// If we are binding a reference to a temporary that has ownership, we
// need to perform retain/release operations on the temporary.
//
// FIXME(ogcg): This should be looking at e, not m.
if (m->getType().getObjCLifetime()) {
cgf.cgm.errorNYI(e->getSourceRange(), "pushTemporaryCleanup: ObjCLifetime");
return;
}
const QualType::DestructionKind dk = e->getType().isDestructedType();
if (dk == QualType::DK_none)
return;
switch (m->getStorageDuration()) {
case SD_Static:
case SD_Thread: {
CXXDestructorDecl *referenceTemporaryDtor = nullptr;
if (const auto *classDecl =
e->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
classDecl && !classDecl->hasTrivialDestructor())
// Get the destructor for the reference temporary.
referenceTemporaryDtor = classDecl->getDestructor();
if (!referenceTemporaryDtor)
return;
cgf.cgm.errorNYI(e->getSourceRange(), "pushTemporaryCleanup: static/thread "
"storage duration with destructors");
break;
}
case SD_FullExpression:
cgf.pushDestroy(NormalAndEHCleanup, referenceTemporary, e->getType(),
CIRGenFunction::destroyCXXObject);
break;
case SD_Automatic:
cgf.cgm.errorNYI(e->getSourceRange(),
"pushTemporaryCleanup: automatic storage duration");
break;
case SD_Dynamic:
llvm_unreachable("temporary cannot have dynamic storage duration");
}
}
LValue CIRGenFunction::emitMaterializeTemporaryExpr(
const MaterializeTemporaryExpr *m) {
const Expr *e = m->getSubExpr();
assert((!m->getExtendingDecl() || !isa<VarDecl>(m->getExtendingDecl()) ||
!cast<VarDecl>(m->getExtendingDecl())->isARCPseudoStrong()) &&
"Reference should never be pseudo-strong!");
// FIXME: ideally this would use emitAnyExprToMem, however, we cannot do so
// as that will cause the lifetime adjustment to be lost for ARC
auto ownership = m->getType().getObjCLifetime();
if (ownership != Qualifiers::OCL_None &&
ownership != Qualifiers::OCL_ExplicitNone) {
cgm.errorNYI(e->getSourceRange(),
"emitMaterializeTemporaryExpr: ObjCLifetime");
return {};
}
SmallVector<const Expr *, 2> commaLHSs;
SmallVector<SubobjectAdjustment, 2> adjustments;
e = e->skipRValueSubobjectAdjustments(commaLHSs, adjustments);
for (const Expr *ignored : commaLHSs)
emitIgnoredExpr(ignored);
if (isa<OpaqueValueExpr>(e)) {
cgm.errorNYI(e->getSourceRange(),
"emitMaterializeTemporaryExpr: OpaqueValueExpr");
return {};
}
// Create and initialize the reference temporary.
Address object = createReferenceTemporary(*this, m, e);
if (auto var = object.getPointer().getDefiningOp<cir::GlobalOp>()) {
// TODO(cir): add something akin to stripPointerCasts() to ptr above
cgm.errorNYI(e->getSourceRange(), "emitMaterializeTemporaryExpr: GlobalOp");
return {};
} else {
assert(!cir::MissingFeatures::emitLifetimeMarkers());
emitAnyExprToMem(e, object, Qualifiers(), /*isInitializer=*/true);
}
pushTemporaryCleanup(*this, m, e, object);
// Perform derived-to-base casts and/or field accesses, to get from the
// temporary object we created (and, potentially, for which we extended
// the lifetime) to the subobject we're binding the reference to.
if (!adjustments.empty()) {
cgm.errorNYI(e->getSourceRange(),
"emitMaterializeTemporaryExpr: Adjustments");
return {};
}
return makeAddrLValue(object, m->getType(), AlignmentSource::Decl);
}
LValue
CIRGenFunction::getOrCreateOpaqueLValueMapping(const OpaqueValueExpr *e) {
assert(OpaqueValueMapping::shouldBindAsLValue(e));
auto it = opaqueLValues.find(e);
if (it != opaqueLValues.end())
return it->second;
assert(e->isUnique() && "LValue for a nonunique OVE hasn't been emitted");
return emitLValue(e->getSourceExpr());
}
RValue
CIRGenFunction::getOrCreateOpaqueRValueMapping(const OpaqueValueExpr *e) {
assert(!OpaqueValueMapping::shouldBindAsLValue(e));
auto it = opaqueRValues.find(e);
if (it != opaqueRValues.end())
return it->second;
assert(e->isUnique() && "RValue for a nonunique OVE hasn't been emitted");
return emitAnyExpr(e->getSourceExpr());
}
LValue CIRGenFunction::emitCompoundLiteralLValue(const CompoundLiteralExpr *e) {
if (e->isFileScope()) {
cgm.errorNYI(e->getSourceRange(), "emitCompoundLiteralLValue: FileScope");
return {};
}
if (e->getType()->isVariablyModifiedType()) {
cgm.errorNYI(e->getSourceRange(),
"emitCompoundLiteralLValue: VariablyModifiedType");
return {};
}
Address declPtr = createMemTemp(e->getType(), getLoc(e->getSourceRange()),
".compoundliteral");
const Expr *initExpr = e->getInitializer();
LValue result = makeAddrLValue(declPtr, e->getType(), AlignmentSource::Decl);
emitAnyExprToMem(initExpr, declPtr, e->getType().getQualifiers(),
/*Init*/ true);
// Block-scope compound literals are destroyed at the end of the enclosing
// scope in C.
if (!getLangOpts().CPlusPlus && e->getType().isDestructedType()) {
cgm.errorNYI(e->getSourceRange(),
"emitCompoundLiteralLValue: non C++ DestructedType");
return {};
}
return result;
}
LValue CIRGenFunction::emitCallExprLValue(const CallExpr *e) {
RValue rv = emitCallExpr(e);
if (!rv.isScalar()) {
cgm.errorNYI(e->getSourceRange(), "emitCallExprLValue: non-scalar return");
return {};
}
assert(e->getCallReturnType(getContext())->isReferenceType() &&
"Can't have a scalar return unless the return type is a "
"reference type!");
return makeNaturalAlignPointeeAddrLValue(rv.getValue(), e->getType());
}
LValue CIRGenFunction::emitBinaryOperatorLValue(const BinaryOperator *e) {
// Comma expressions just emit their LHS then their RHS as an l-value.
if (e->getOpcode() == BO_Comma) {
emitIgnoredExpr(e->getLHS());
return emitLValue(e->getRHS());
}
if (e->getOpcode() == BO_PtrMemD || e->getOpcode() == BO_PtrMemI) {
cgm.errorNYI(e->getSourceRange(), "member pointers");
return {};
}
assert(e->getOpcode() == BO_Assign && "unexpected binary l-value");
// Note that in all of these cases, __block variables need the RHS
// evaluated first just in case the variable gets moved by the RHS.
switch (CIRGenFunction::getEvaluationKind(e->getType())) {
case cir::TEK_Scalar: {
assert(!cir::MissingFeatures::objCLifetime());
if (e->getLHS()->getType().getObjCLifetime() !=
clang::Qualifiers::ObjCLifetime::OCL_None) {
cgm.errorNYI(e->getSourceRange(), "objc lifetimes");
return {};
}
RValue rv = emitAnyExpr(e->getRHS());
LValue lv = emitLValue(e->getLHS());
SourceLocRAIIObject loc{*this, getLoc(e->getSourceRange())};
if (lv.isBitField())
emitStoreThroughBitfieldLValue(rv, lv);
else
emitStoreThroughLValue(rv, lv);
if (getLangOpts().OpenMP) {
cgm.errorNYI(e->getSourceRange(), "openmp");
return {};
}
return lv;
}
case cir::TEK_Complex: {
return emitComplexAssignmentLValue(e);
}
case cir::TEK_Aggregate:
cgm.errorNYI(e->getSourceRange(), "aggregate lvalues");
return {};
}
llvm_unreachable("bad evaluation kind");
}
/// Emit code to compute the specified expression which
/// can have any type. The result is returned as an RValue struct.
RValue CIRGenFunction::emitAnyExpr(const Expr *e, AggValueSlot aggSlot) {
switch (CIRGenFunction::getEvaluationKind(e->getType())) {
case cir::TEK_Scalar:
return RValue::get(emitScalarExpr(e));
case cir::TEK_Complex:
return RValue::getComplex(emitComplexExpr(e));
case cir::TEK_Aggregate: {
if (aggSlot.isIgnored())
aggSlot = createAggTemp(e->getType(), getLoc(e->getSourceRange()),
getCounterAggTmpAsString());
emitAggExpr(e, aggSlot);
return aggSlot.asRValue();
}
}
llvm_unreachable("bad evaluation kind");
}
// Detect the unusual situation where an inline version is shadowed by a
// non-inline version. In that case we should pick the external one
// everywhere. That's GCC behavior too.
static bool onlyHasInlineBuiltinDeclaration(const FunctionDecl *fd) {
for (const FunctionDecl *pd = fd; pd; pd = pd->getPreviousDecl())
if (!pd->isInlineBuiltinDeclaration())
return false;
return true;
}
CIRGenCallee CIRGenFunction::emitDirectCallee(const GlobalDecl &gd) {
const auto *fd = cast<FunctionDecl>(gd.getDecl());
if (unsigned builtinID = fd->getBuiltinID()) {
if (fd->getAttr<AsmLabelAttr>()) {
cgm.errorNYI("AsmLabelAttr");
}
StringRef ident = fd->getName();
std::string fdInlineName = (ident + ".inline").str();
bool isPredefinedLibFunction =
cgm.getASTContext().BuiltinInfo.isPredefinedLibFunction(builtinID);
// Assume nobuiltins everywhere until we actually read the attributes.
bool hasAttributeNoBuiltin = true;
assert(!cir::MissingFeatures::attributeNoBuiltin());
// When directing calling an inline builtin, call it through it's mangled
// name to make it clear it's not the actual builtin.
auto fn = cast<cir::FuncOp>(curFn);
if (fn.getName() != fdInlineName && onlyHasInlineBuiltinDeclaration(fd)) {
cgm.errorNYI("Inline only builtin function calls");
}
// Replaceable builtins provide their own implementation of a builtin. If we
// are in an inline builtin implementation, avoid trivial infinite
// recursion. Honor __attribute__((no_builtin("foo"))) or
// __attribute__((no_builtin)) on the current function unless foo is
// not a predefined library function which means we must generate the
// builtin no matter what.
else if (!isPredefinedLibFunction || !hasAttributeNoBuiltin)
return CIRGenCallee::forBuiltin(builtinID, fd);
}
cir::FuncOp callee = emitFunctionDeclPointer(cgm, gd);
assert(!cir::MissingFeatures::hip());
return CIRGenCallee::forDirect(callee, gd);
}
RValue CIRGenFunction::getUndefRValue(QualType ty) {
if (ty->isVoidType())
return RValue::get(nullptr);
cgm.errorNYI("unsupported type for undef rvalue");
return RValue::get(nullptr);
}
RValue CIRGenFunction::emitCall(clang::QualType calleeTy,
const CIRGenCallee &origCallee,
const clang::CallExpr *e,
ReturnValueSlot returnValue) {
// Get the actual function type. The callee type will always be a pointer to
// function type or a block pointer type.
assert(calleeTy->isFunctionPointerType() &&
"Callee must have function pointer type!");
calleeTy = getContext().getCanonicalType(calleeTy);
auto pointeeTy = cast<PointerType>(calleeTy)->getPointeeType();
CIRGenCallee callee = origCallee;
if (getLangOpts().CPlusPlus)
assert(!cir::MissingFeatures::sanitizers());
const auto *fnType = cast<FunctionType>(pointeeTy);
assert(!cir::MissingFeatures::sanitizers());
CallArgList args;
assert(!cir::MissingFeatures::opCallArgEvaluationOrder());
emitCallArgs(args, dyn_cast<FunctionProtoType>(fnType), e->arguments(),
e->getDirectCallee());
const CIRGenFunctionInfo &funcInfo =
cgm.getTypes().arrangeFreeFunctionCall(args, fnType);
// C99 6.5.2.2p6:
// If the expression that denotes the called function has a type that does
// not include a prototype, [the default argument promotions are performed].
// If the number of arguments does not equal the number of parameters, the
// behavior is undefined. If the function is defined with a type that
// includes a prototype, and either the prototype ends with an ellipsis (,
// ...) or the types of the arguments after promotion are not compatible
// with the types of the parameters, the behavior is undefined. If the
// function is defined with a type that does not include a prototype, and
// the types of the arguments after promotion are not compatible with those
// of the parameters after promotion, the behavior is undefined [except in
// some trivial cases].
// That is, in the general case, we should assume that a call through an
// unprototyped function type works like a *non-variadic* call. The way we
// make this work is to cast to the exxact type fo the promoted arguments.
if (isa<FunctionNoProtoType>(fnType)) {
assert(!cir::MissingFeatures::opCallChain());
assert(!cir::MissingFeatures::addressSpace());
cir::FuncType calleeTy = getTypes().getFunctionType(funcInfo);
// get non-variadic function type
calleeTy = cir::FuncType::get(calleeTy.getInputs(),
calleeTy.getReturnType(), false);
auto calleePtrTy = cir::PointerType::get(calleeTy);
mlir::Operation *fn = callee.getFunctionPointer();
mlir::Value addr;
if (auto funcOp = mlir::dyn_cast<cir::FuncOp>(fn)) {
addr = builder.create<cir::GetGlobalOp>(
getLoc(e->getSourceRange()),
cir::PointerType::get(funcOp.getFunctionType()), funcOp.getSymName());
} else {
addr = fn->getResult(0);
}
fn = builder.createBitcast(addr, calleePtrTy).getDefiningOp();
callee.setFunctionPointer(fn);
}
assert(!cir::MissingFeatures::opCallFnInfoOpts());
assert(!cir::MissingFeatures::hip());
assert(!cir::MissingFeatures::opCallMustTail());
cir::CIRCallOpInterface callOp;
RValue callResult = emitCall(funcInfo, callee, returnValue, args, &callOp,
getLoc(e->getExprLoc()));
assert(!cir::MissingFeatures::generateDebugInfo());
return callResult;
}
CIRGenCallee CIRGenFunction::emitCallee(const clang::Expr *e) {
e = e->IgnoreParens();
// Look through function-to-pointer decay.
if (const auto *implicitCast = dyn_cast<ImplicitCastExpr>(e)) {
if (implicitCast->getCastKind() == CK_FunctionToPointerDecay ||
implicitCast->getCastKind() == CK_BuiltinFnToFnPtr) {
return emitCallee(implicitCast->getSubExpr());
}
// When performing an indirect call through a function pointer lvalue, the
// function pointer lvalue is implicitly converted to an rvalue through an
// lvalue-to-rvalue conversion.
assert(implicitCast->getCastKind() == CK_LValueToRValue &&
"unexpected implicit cast on function pointers");
} else if (const auto *declRef = dyn_cast<DeclRefExpr>(e)) {
// Resolve direct calls.
const auto *funcDecl = cast<FunctionDecl>(declRef->getDecl());
return emitDirectCallee(funcDecl);
} else if (isa<MemberExpr>(e)) {
cgm.errorNYI(e->getSourceRange(),
"emitCallee: call to member function is NYI");
return {};
} else if (auto *pde = dyn_cast<CXXPseudoDestructorExpr>(e)) {
return CIRGenCallee::forPseudoDestructor(pde);
}
// Otherwise, we have an indirect reference.
mlir::Value calleePtr;
QualType functionType;
if (const auto *ptrType = e->getType()->getAs<clang::PointerType>()) {
calleePtr = emitScalarExpr(e);
functionType = ptrType->getPointeeType();
} else {
functionType = e->getType();
calleePtr = emitLValue(e).getPointer();
}
assert(functionType->isFunctionType());
GlobalDecl gd;
if (const auto *vd =
dyn_cast_or_null<VarDecl>(e->getReferencedDeclOfCallee()))
gd = GlobalDecl(vd);
CIRGenCalleeInfo calleeInfo(functionType->getAs<FunctionProtoType>(), gd);
CIRGenCallee callee(calleeInfo, calleePtr.getDefiningOp());
return callee;
}
RValue CIRGenFunction::emitCallExpr(const clang::CallExpr *e,
ReturnValueSlot returnValue) {
assert(!cir::MissingFeatures::objCBlocks());
if (const auto *ce = dyn_cast<CXXMemberCallExpr>(e))
return emitCXXMemberCallExpr(ce, returnValue);
if (isa<CUDAKernelCallExpr>(e)) {
cgm.errorNYI(e->getSourceRange(), "call to CUDA kernel");
return RValue::get(nullptr);
}
if (const auto *operatorCall = dyn_cast<CXXOperatorCallExpr>(e)) {
// If the callee decl is a CXXMethodDecl, we need to emit this as a C++
// operator member call.
if (const CXXMethodDecl *md =
dyn_cast_or_null<CXXMethodDecl>(operatorCall->getCalleeDecl()))
return emitCXXOperatorMemberCallExpr(operatorCall, md, returnValue);
// A CXXOperatorCallExpr is created even for explicit object methods, but
// these should be treated like static function calls. Fall through to do
// that.
}
CIRGenCallee callee = emitCallee(e->getCallee());
if (callee.isBuiltin())
return emitBuiltinExpr(callee.getBuiltinDecl(), callee.getBuiltinID(), e,
returnValue);
if (callee.isPseudoDestructor())
return emitCXXPseudoDestructorExpr(callee.getPseudoDestructorExpr());
return emitCall(e->getCallee()->getType(), callee, e, returnValue);
}
/// Emit code to compute the specified expression, ignoring the result.
void CIRGenFunction::emitIgnoredExpr(const Expr *e) {
if (e->isPRValue()) {
assert(!cir::MissingFeatures::aggValueSlot());
emitAnyExpr(e);
return;
}
// Just emit it as an l-value and drop the result.
emitLValue(e);
}
Address CIRGenFunction::emitArrayToPointerDecay(const Expr *e,
LValueBaseInfo *baseInfo) {
assert(!cir::MissingFeatures::opTBAA());
assert(e->getType()->isArrayType() &&
"Array to pointer decay must have array source type!");
// Expressions of array type can't be bitfields or vector elements.
LValue lv = emitLValue(e);
Address addr = lv.getAddress();
// If the array type was an incomplete type, we need to make sure
// the decay ends up being the right type.
auto lvalueAddrTy = mlir::cast<cir::PointerType>(addr.getPointer().getType());
if (e->getType()->isVariableArrayType())
return addr;
[[maybe_unused]] auto pointeeTy =
mlir::cast<cir::ArrayType>(lvalueAddrTy.getPointee());
[[maybe_unused]] mlir::Type arrayTy = convertType(e->getType());
assert(mlir::isa<cir::ArrayType>(arrayTy) && "expected array");
assert(pointeeTy == arrayTy);
// The result of this decay conversion points to an array element within the
// base lvalue. However, since TBAA currently does not support representing
// accesses to elements of member arrays, we conservatively represent accesses
// to the pointee object as if it had no any base lvalue specified.
// TODO: Support TBAA for member arrays.
QualType eltType = e->getType()->castAsArrayTypeUnsafe()->getElementType();
assert(!cir::MissingFeatures::opTBAA());
mlir::Value ptr = builder.maybeBuildArrayDecay(
cgm.getLoc(e->getSourceRange()), addr.getPointer(),
convertTypeForMem(eltType));
return Address(ptr, addr.getAlignment());
}
/// Given the address of a temporary variable, produce an r-value of its type.
RValue CIRGenFunction::convertTempToRValue(Address addr, clang::QualType type,
clang::SourceLocation loc) {
LValue lvalue = makeAddrLValue(addr, type, AlignmentSource::Decl);
switch (getEvaluationKind(type)) {
case cir::TEK_Complex:
return RValue::getComplex(emitLoadOfComplex(lvalue, loc));
case cir::TEK_Aggregate:
cgm.errorNYI(loc, "convertTempToRValue: aggregate type");
return RValue::get(nullptr);
case cir::TEK_Scalar:
return RValue::get(emitLoadOfScalar(lvalue, loc));
}
llvm_unreachable("bad evaluation kind");
}
/// Emit an `if` on a boolean condition, filling `then` and `else` into
/// appropriated regions.
mlir::LogicalResult CIRGenFunction::emitIfOnBoolExpr(const Expr *cond,
const Stmt *thenS,
const Stmt *elseS) {
mlir::Location thenLoc = getLoc(thenS->getSourceRange());
std::optional<mlir::Location> elseLoc;
if (elseS)
elseLoc = getLoc(elseS->getSourceRange());
mlir::LogicalResult resThen = mlir::success(), resElse = mlir::success();
emitIfOnBoolExpr(
cond, /*thenBuilder=*/
[&](mlir::OpBuilder &, mlir::Location) {
LexicalScope lexScope{*this, thenLoc, builder.getInsertionBlock()};
resThen = emitStmt(thenS, /*useCurrentScope=*/true);
},
thenLoc,
/*elseBuilder=*/
[&](mlir::OpBuilder &, mlir::Location) {
assert(elseLoc && "Invalid location for elseS.");
LexicalScope lexScope{*this, *elseLoc, builder.getInsertionBlock()};
resElse = emitStmt(elseS, /*useCurrentScope=*/true);
},
elseLoc);
return mlir::LogicalResult::success(resThen.succeeded() &&
resElse.succeeded());
}
/// Emit an `if` on a boolean condition, filling `then` and `else` into
/// appropriated regions.
cir::IfOp CIRGenFunction::emitIfOnBoolExpr(
const clang::Expr *cond, BuilderCallbackRef thenBuilder,
mlir::Location thenLoc, BuilderCallbackRef elseBuilder,
std::optional<mlir::Location> elseLoc) {
// Attempt to be as accurate as possible with IfOp location, generate
// one fused location that has either 2 or 4 total locations, depending
// on else's availability.
SmallVector<mlir::Location, 2> ifLocs{thenLoc};
if (elseLoc)
ifLocs.push_back(*elseLoc);
mlir::Location loc = mlir::FusedLoc::get(&getMLIRContext(), ifLocs);
// Emit the code with the fully general case.
mlir::Value condV = emitOpOnBoolExpr(loc, cond);
return builder.create<cir::IfOp>(loc, condV, elseLoc.has_value(),
/*thenBuilder=*/thenBuilder,
/*elseBuilder=*/elseBuilder);
}
/// TODO(cir): see EmitBranchOnBoolExpr for extra ideas).
mlir::Value CIRGenFunction::emitOpOnBoolExpr(mlir::Location loc,
const Expr *cond) {
assert(!cir::MissingFeatures::pgoUse());
assert(!cir::MissingFeatures::generateDebugInfo());
cond = cond->IgnoreParens();
// In LLVM the condition is reversed here for efficient codegen.
// This should be done in CIR prior to LLVM lowering, if we do now
// we can make CIR based diagnostics misleading.
// cir.ternary(!x, t, f) -> cir.ternary(x, f, t)
assert(!cir::MissingFeatures::shouldReverseUnaryCondOnBoolExpr());
if (const ConditionalOperator *condOp = dyn_cast<ConditionalOperator>(cond)) {
Expr *trueExpr = condOp->getTrueExpr();
Expr *falseExpr = condOp->getFalseExpr();
mlir::Value condV = emitOpOnBoolExpr(loc, condOp->getCond());
mlir::Value ternaryOpRes =
builder
.create<cir::TernaryOp>(
loc, condV, /*thenBuilder=*/
[this, trueExpr](mlir::OpBuilder &b, mlir::Location loc) {
mlir::Value lhs = emitScalarExpr(trueExpr);
b.create<cir::YieldOp>(loc, lhs);
},
/*elseBuilder=*/
[this, falseExpr](mlir::OpBuilder &b, mlir::Location loc) {
mlir::Value rhs = emitScalarExpr(falseExpr);
b.create<cir::YieldOp>(loc, rhs);
})
.getResult();
return emitScalarConversion(ternaryOpRes, condOp->getType(),
getContext().BoolTy, condOp->getExprLoc());
}
if (isa<CXXThrowExpr>(cond)) {
cgm.errorNYI("NYI");
return createDummyValue(loc, cond->getType());
}
// If the branch has a condition wrapped by __builtin_unpredictable,
// create metadata that specifies that the branch is unpredictable.
// Don't bother if not optimizing because that metadata would not be used.
assert(!cir::MissingFeatures::insertBuiltinUnpredictable());
// Emit the code with the fully general case.
return evaluateExprAsBool(cond);
}
mlir::Value CIRGenFunction::emitAlloca(StringRef name, mlir::Type ty,
mlir::Location loc, CharUnits alignment,
bool insertIntoFnEntryBlock,
mlir::Value arraySize) {
mlir::Block *entryBlock = insertIntoFnEntryBlock
? getCurFunctionEntryBlock()
: curLexScope->getEntryBlock();
// If this is an alloca in the entry basic block of a cir.try and there's
// a surrounding cir.scope, make sure the alloca ends up in the surrounding
// scope instead. This is necessary in order to guarantee all SSA values are
// reachable during cleanups.
assert(!cir::MissingFeatures::tryOp());
return emitAlloca(name, ty, loc, alignment,
builder.getBestAllocaInsertPoint(entryBlock), arraySize);
}
mlir::Value CIRGenFunction::emitAlloca(StringRef name, mlir::Type ty,
mlir::Location loc, CharUnits alignment,
mlir::OpBuilder::InsertPoint ip,
mlir::Value arraySize) {
// CIR uses its own alloca address space rather than follow the target data
// layout like original CodeGen. The data layout awareness should be done in
// the lowering pass instead.
assert(!cir::MissingFeatures::addressSpace());
cir::PointerType localVarPtrTy = builder.getPointerTo(ty);
mlir::IntegerAttr alignIntAttr = cgm.getSize(alignment);
mlir::Value addr;
{
mlir::OpBuilder::InsertionGuard guard(builder);
builder.restoreInsertionPoint(ip);
addr = builder.createAlloca(loc, /*addr type*/ localVarPtrTy,
/*var type*/ ty, name, alignIntAttr);
assert(!cir::MissingFeatures::astVarDeclInterface());
}
return addr;
}
// Note: this function also emit constructor calls to support a MSVC extensions
// allowing explicit constructor function call.
RValue CIRGenFunction::emitCXXMemberCallExpr(const CXXMemberCallExpr *ce,
ReturnValueSlot returnValue) {
const Expr *callee = ce->getCallee()->IgnoreParens();
if (isa<BinaryOperator>(callee)) {
cgm.errorNYI(ce->getSourceRange(),
"emitCXXMemberCallExpr: C++ binary operator");
return RValue::get(nullptr);
}
const auto *me = cast<MemberExpr>(callee);
const auto *md = cast<CXXMethodDecl>(me->getMemberDecl());
if (md->isStatic()) {
cgm.errorNYI(ce->getSourceRange(), "emitCXXMemberCallExpr: static method");
return RValue::get(nullptr);
}
bool hasQualifier = me->hasQualifier();
NestedNameSpecifier qualifier = me->getQualifier();
bool isArrow = me->isArrow();
const Expr *base = me->getBase();
return emitCXXMemberOrOperatorMemberCallExpr(
ce, md, returnValue, hasQualifier, qualifier, isArrow, base);
}
void CIRGenFunction::emitCXXConstructExpr(const CXXConstructExpr *e,
AggValueSlot dest) {
assert(!dest.isIgnored() && "Must have a destination!");
const CXXConstructorDecl *cd = e->getConstructor();
// If we require zero initialization before (or instead of) calling the
// constructor, as can be the case with a non-user-provided default
// constructor, emit the zero initialization now, unless destination is
// already zeroed.
if (e->requiresZeroInitialization() && !dest.isZeroed()) {
switch (e->getConstructionKind()) {
case CXXConstructionKind::Delegating:
case CXXConstructionKind::Complete:
emitNullInitialization(getLoc(e->getSourceRange()), dest.getAddress(),
e->getType());
break;
case CXXConstructionKind::VirtualBase:
case CXXConstructionKind::NonVirtualBase:
cgm.errorNYI(e->getSourceRange(),
"emitCXXConstructExpr: base requires initialization");
break;
}
}
// If this is a call to a trivial default constructor, do nothing.
if (cd->isTrivial() && cd->isDefaultConstructor())
return;
// Elide the constructor if we're constructing from a temporary
if (getLangOpts().ElideConstructors && e->isElidable()) {
// FIXME: This only handles the simplest case, where the source object is
// passed directly as the first argument to the constructor. This
// should also handle stepping through implicit casts and conversion
// sequences which involve two steps, with a conversion operator
// follwed by a converting constructor.
const Expr *srcObj = e->getArg(0);
assert(srcObj->isTemporaryObject(getContext(), cd->getParent()));
assert(
getContext().hasSameUnqualifiedType(e->getType(), srcObj->getType()));
emitAggExpr(srcObj, dest);
return;
}
if (const ArrayType *arrayType = getContext().getAsArrayType(e->getType())) {
assert(!cir::MissingFeatures::sanitizers());
emitCXXAggrConstructorCall(cd, arrayType, dest.getAddress(), e, false);
} else {
clang::CXXCtorType type = Ctor_Complete;
bool forVirtualBase = false;
bool delegating = false;
switch (e->getConstructionKind()) {
case CXXConstructionKind::Complete:
type = Ctor_Complete;
break;
case CXXConstructionKind::Delegating:
// We should be emitting a constructor; GlobalDecl will assert this
type = curGD.getCtorType();
delegating = true;
break;
case CXXConstructionKind::VirtualBase:
forVirtualBase = true;
[[fallthrough]];
case CXXConstructionKind::NonVirtualBase:
type = Ctor_Base;
break;
}
emitCXXConstructorCall(cd, type, forVirtualBase, delegating, dest, e);
}
}
RValue CIRGenFunction::emitReferenceBindingToExpr(const Expr *e) {
// Emit the expression as an lvalue.
LValue lv = emitLValue(e);
assert(lv.isSimple());
mlir::Value value = lv.getPointer();
assert(!cir::MissingFeatures::sanitizers());
return RValue::get(value);
}
Address CIRGenFunction::emitLoadOfReference(LValue refLVal, mlir::Location loc,
LValueBaseInfo *pointeeBaseInfo) {
if (refLVal.isVolatile())
cgm.errorNYI(loc, "load of volatile reference");
cir::LoadOp load =
builder.create<cir::LoadOp>(loc, refLVal.getAddress().getElementType(),
refLVal.getAddress().getPointer());
assert(!cir::MissingFeatures::opTBAA());
QualType pointeeType = refLVal.getType()->getPointeeType();
CharUnits align = cgm.getNaturalTypeAlignment(pointeeType, pointeeBaseInfo);
return Address(load, convertTypeForMem(pointeeType), align);
}
LValue CIRGenFunction::emitLoadOfReferenceLValue(Address refAddr,
mlir::Location loc,
QualType refTy,
AlignmentSource source) {
LValue refLVal = makeAddrLValue(refAddr, refTy, LValueBaseInfo(source));
LValueBaseInfo pointeeBaseInfo;
assert(!cir::MissingFeatures::opTBAA());
Address pointeeAddr = emitLoadOfReference(refLVal, loc, &pointeeBaseInfo);
return makeAddrLValue(pointeeAddr, refLVal.getType()->getPointeeType(),
pointeeBaseInfo);
}
void CIRGenFunction::emitTrap(mlir::Location loc, bool createNewBlock) {
cir::TrapOp::create(builder, loc);
if (createNewBlock)
builder.createBlock(builder.getBlock()->getParent());
}
void CIRGenFunction::emitUnreachable(clang::SourceLocation loc,
bool createNewBlock) {
assert(!cir::MissingFeatures::sanitizers());
cir::UnreachableOp::create(builder, getLoc(loc));
if (createNewBlock)
builder.createBlock(builder.getBlock()->getParent());
}
mlir::Value CIRGenFunction::createDummyValue(mlir::Location loc,
clang::QualType qt) {
mlir::Type t = convertType(qt);
CharUnits alignment = getContext().getTypeAlignInChars(qt);
return builder.createDummyValue(loc, t, alignment);
}
//===----------------------------------------------------------------------===//
// CIR builder helpers
//===----------------------------------------------------------------------===//
Address CIRGenFunction::createMemTemp(QualType ty, mlir::Location loc,
const Twine &name, Address *alloca,
mlir::OpBuilder::InsertPoint ip) {
// FIXME: Should we prefer the preferred type alignment here?
return createMemTemp(ty, getContext().getTypeAlignInChars(ty), loc, name,
alloca, ip);
}
Address CIRGenFunction::createMemTemp(QualType ty, CharUnits align,
mlir::Location loc, const Twine &name,
Address *alloca,
mlir::OpBuilder::InsertPoint ip) {
Address result = createTempAlloca(convertTypeForMem(ty), align, loc, name,
/*ArraySize=*/nullptr, alloca, ip);
if (ty->isConstantMatrixType()) {
assert(!cir::MissingFeatures::matrixType());
cgm.errorNYI(loc, "temporary matrix value");
}
return result;
}
/// This creates a alloca and inserts it into the entry block of the
/// current region.
Address CIRGenFunction::createTempAllocaWithoutCast(
mlir::Type ty, CharUnits align, mlir::Location loc, const Twine &name,
mlir::Value arraySize, mlir::OpBuilder::InsertPoint ip) {
cir::AllocaOp alloca = ip.isSet()
? createTempAlloca(ty, loc, name, ip, arraySize)
: createTempAlloca(ty, loc, name, arraySize);
alloca.setAlignmentAttr(cgm.getSize(align));
return Address(alloca, ty, align);
}
/// This creates a alloca and inserts it into the entry block. The alloca is
/// casted to default address space if necessary.
Address CIRGenFunction::createTempAlloca(mlir::Type ty, CharUnits align,
mlir::Location loc, const Twine &name,
mlir::Value arraySize,
Address *allocaAddr,
mlir::OpBuilder::InsertPoint ip) {
Address alloca =
createTempAllocaWithoutCast(ty, align, loc, name, arraySize, ip);
if (allocaAddr)
*allocaAddr = alloca;
mlir::Value v = alloca.getPointer();
// Alloca always returns a pointer in alloca address space, which may
// be different from the type defined by the language. For example,
// in C++ the auto variables are in the default address space. Therefore
// cast alloca to the default address space when necessary.
assert(!cir::MissingFeatures::addressSpace());
return Address(v, ty, align);
}
/// This creates an alloca and inserts it into the entry block if \p ArraySize
/// is nullptr, otherwise inserts it at the current insertion point of the
/// builder.
cir::AllocaOp CIRGenFunction::createTempAlloca(mlir::Type ty,
mlir::Location loc,
const Twine &name,
mlir::Value arraySize,
bool insertIntoFnEntryBlock) {
return mlir::cast<cir::AllocaOp>(emitAlloca(name.str(), ty, loc, CharUnits(),
insertIntoFnEntryBlock, arraySize)
.getDefiningOp());
}
/// This creates an alloca and inserts it into the provided insertion point
cir::AllocaOp CIRGenFunction::createTempAlloca(mlir::Type ty,
mlir::Location loc,
const Twine &name,
mlir::OpBuilder::InsertPoint ip,
mlir::Value arraySize) {
assert(ip.isSet() && "Insertion point is not set");
return mlir::cast<cir::AllocaOp>(
emitAlloca(name.str(), ty, loc, CharUnits(), ip, arraySize)
.getDefiningOp());
}
/// Try to emit a reference to the given value without producing it as
/// an l-value. For many cases, this is just an optimization, but it avoids
/// us needing to emit global copies of variables if they're named without
/// triggering a formal use in a context where we can't emit a direct
/// reference to them, for instance if a block or lambda or a member of a
/// local class uses a const int variable or constexpr variable from an
/// enclosing function.
///
/// For named members of enums, this is the only way they are emitted.
CIRGenFunction::ConstantEmission
CIRGenFunction::tryEmitAsConstant(const DeclRefExpr *refExpr) {
const ValueDecl *value = refExpr->getDecl();
// There is a lot more to do here, but for now only EnumConstantDecl is
// supported.
assert(!cir::MissingFeatures::tryEmitAsConstant());
// The value needs to be an enum constant or a constant variable.
if (!isa<EnumConstantDecl>(value))
return ConstantEmission();
Expr::EvalResult result;
if (!refExpr->EvaluateAsRValue(result, getContext()))
return ConstantEmission();
QualType resultType = refExpr->getType();
// As long as we're only handling EnumConstantDecl, there should be no
// side-effects.
assert(!result.HasSideEffects);
// Emit as a constant.
// FIXME(cir): have emitAbstract build a TypedAttr instead (this requires
// somewhat heavy refactoring...)
mlir::Attribute c = ConstantEmitter(*this).emitAbstract(
refExpr->getLocation(), result.Val, resultType);
mlir::TypedAttr cstToEmit = mlir::dyn_cast_if_present<mlir::TypedAttr>(c);
assert(cstToEmit && "expected a typed attribute");
assert(!cir::MissingFeatures::generateDebugInfo());
return ConstantEmission::forValue(cstToEmit);
}
CIRGenFunction::ConstantEmission
CIRGenFunction::tryEmitAsConstant(const MemberExpr *me) {
if (DeclRefExpr *dre = tryToConvertMemberExprToDeclRefExpr(*this, me))
return tryEmitAsConstant(dre);
return ConstantEmission();
}
mlir::Value CIRGenFunction::emitScalarConstant(
const CIRGenFunction::ConstantEmission &constant, Expr *e) {
assert(constant && "not a constant");
if (constant.isReference()) {
cgm.errorNYI(e->getSourceRange(), "emitScalarConstant: reference");
return {};
}
return builder.getConstant(getLoc(e->getSourceRange()), constant.getValue());
}
/// An LValue is a candidate for having its loads and stores be made atomic if
/// we are operating under /volatile:ms *and* the LValue itself is volatile and
/// performing such an operation can be performed without a libcall.
bool CIRGenFunction::isLValueSuitableForInlineAtomic(LValue lv) {
if (!cgm.getLangOpts().MSVolatile)
return false;
cgm.errorNYI("LValueSuitableForInlineAtomic LangOpts MSVolatile");
return false;
}
|